OSDN Git Service

* arm.cc (Arm_relocate_functions::got_prel) New function.
[pf3gnuchains/pf3gnuchains3x.git] / gold / arm.cc
1 // arm.cc -- arm target support for gold.
2
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6
7 // This file is part of gold.
8
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
13
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
23
24 #include "gold.h"
25
26 #include <cstring>
27 #include <limits>
28 #include <cstdio>
29 #include <string>
30
31 #include "elfcpp.h"
32 #include "parameters.h"
33 #include "reloc.h"
34 #include "arm.h"
35 #include "object.h"
36 #include "symtab.h"
37 #include "layout.h"
38 #include "output.h"
39 #include "copy-relocs.h"
40 #include "target.h"
41 #include "target-reloc.h"
42 #include "target-select.h"
43 #include "tls.h"
44 #include "defstd.h"
45
46 namespace
47 {
48
49 using namespace gold;
50
51 template<bool big_endian>
52 class Output_data_plt_arm;
53
54 // The arm target class.
55 //
56 // This is a very simple port of gold for ARM-EABI.  It is intended for
57 // supporting Android only for the time being.  Only these relocation types
58 // are supported.
59 //
60 // R_ARM_NONE
61 // R_ARM_ABS32
62 // R_ARM_REL32
63 // R_ARM_THM_CALL
64 // R_ARM_COPY
65 // R_ARM_GLOB_DAT
66 // R_ARM_BASE_PREL
67 // R_ARM_JUMP_SLOT
68 // R_ARM_RELATIVE
69 // R_ARM_GOTOFF32
70 // R_ARM_GOT_BREL
71 // R_ARM_GOT_PREL
72 // R_ARM_PLT32
73 // R_ARM_CALL
74 // R_ARM_JUMP24
75 // R_ARM_TARGET1
76 // R_ARM_PREL31
77 // R_ARM_ABS8
78 // 
79 // TODOs:
80 // - Generate various branch stubs.
81 // - Support interworking.
82 // - Define section symbols __exidx_start and __exidx_stop.
83 // - Support more relocation types as needed. 
84 // - Make PLTs more flexible for different architecture features like
85 //   Thumb-2 and BE8.
86 // There are probably a lot more.
87
88 // Utilities for manipulating integers of up to 32-bits
89
90 namespace utils
91 {
92   // Sign extend an n-bit unsigned integer stored in an uint32_t into
93   // an int32_t.  NO_BITS must be between 1 to 32.
94   template<int no_bits>
95   static inline int32_t
96   sign_extend(uint32_t bits)
97   {
98     gold_assert(no_bits >= 0 && no_bits <= 32);
99     if (no_bits == 32)
100       return static_cast<int32_t>(bits);
101     uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
102     bits &= mask;
103     uint32_t top_bit = 1U << (no_bits - 1);
104     int32_t as_signed = static_cast<int32_t>(bits);
105     return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
106   }
107
108   // Detects overflow of an NO_BITS integer stored in a uint32_t.
109   template<int no_bits>
110   static inline bool
111   has_overflow(uint32_t bits)
112   {
113     gold_assert(no_bits >= 0 && no_bits <= 32);
114     if (no_bits == 32)
115       return false;
116     int32_t max = (1 << (no_bits - 1)) - 1;
117     int32_t min = -(1 << (no_bits - 1));
118     int32_t as_signed = static_cast<int32_t>(bits);
119     return as_signed > max || as_signed < min;
120   }
121
122   // Detects overflow of an NO_BITS integer stored in a uint32_t when it
123   // fits in the given number of bits as either a signed or unsigned value.
124   // For example, has_signed_unsigned_overflow<8> would check
125   // -128 <= bits <= 255
126   template<int no_bits>
127   static inline bool
128   has_signed_unsigned_overflow(uint32_t bits)
129   {
130     gold_assert(no_bits >= 2 && no_bits <= 32);
131     if (no_bits == 32)
132       return false;
133     int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
134     int32_t min = -(1 << (no_bits - 1));
135     int32_t as_signed = static_cast<int32_t>(bits);
136     return as_signed > max || as_signed < min;
137   }
138
139   // Select bits from A and B using bits in MASK.  For each n in [0..31],
140   // the n-th bit in the result is chosen from the n-th bits of A and B.
141   // A zero selects A and a one selects B.
142   static inline uint32_t
143   bit_select(uint32_t a, uint32_t b, uint32_t mask)
144   { return (a & ~mask) | (b & mask); }
145 };
146
147 template<bool big_endian>
148 class Target_arm : public Sized_target<32, big_endian>
149 {
150  public:
151   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
152     Reloc_section;
153
154   Target_arm()
155     : Sized_target<32, big_endian>(&arm_info),
156       got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
157       copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL)
158   { }
159
160   // Process the relocations to determine unreferenced sections for 
161   // garbage collection.
162   void
163   gc_process_relocs(const General_options& options,
164                     Symbol_table* symtab,
165                     Layout* layout,
166                     Sized_relobj<32, big_endian>* object,
167                     unsigned int data_shndx,
168                     unsigned int sh_type,
169                     const unsigned char* prelocs,
170                     size_t reloc_count,
171                     Output_section* output_section,
172                     bool needs_special_offset_handling,
173                     size_t local_symbol_count,
174                     const unsigned char* plocal_symbols);
175
176   // Scan the relocations to look for symbol adjustments.
177   void
178   scan_relocs(const General_options& options,
179               Symbol_table* symtab,
180               Layout* layout,
181               Sized_relobj<32, big_endian>* object,
182               unsigned int data_shndx,
183               unsigned int sh_type,
184               const unsigned char* prelocs,
185               size_t reloc_count,
186               Output_section* output_section,
187               bool needs_special_offset_handling,
188               size_t local_symbol_count,
189               const unsigned char* plocal_symbols);
190
191   // Finalize the sections.
192   void
193   do_finalize_sections(Layout*);
194
195   // Return the value to use for a dynamic symbol which requires special
196   // treatment.
197   uint64_t
198   do_dynsym_value(const Symbol*) const;
199
200   // Relocate a section.
201   void
202   relocate_section(const Relocate_info<32, big_endian>*,
203                    unsigned int sh_type,
204                    const unsigned char* prelocs,
205                    size_t reloc_count,
206                    Output_section* output_section,
207                    bool needs_special_offset_handling,
208                    unsigned char* view,
209                    elfcpp::Elf_types<32>::Elf_Addr view_address,
210                    section_size_type view_size,
211                    const Reloc_symbol_changes*);
212
213   // Scan the relocs during a relocatable link.
214   void
215   scan_relocatable_relocs(const General_options& options,
216                           Symbol_table* symtab,
217                           Layout* layout,
218                           Sized_relobj<32, big_endian>* object,
219                           unsigned int data_shndx,
220                           unsigned int sh_type,
221                           const unsigned char* prelocs,
222                           size_t reloc_count,
223                           Output_section* output_section,
224                           bool needs_special_offset_handling,
225                           size_t local_symbol_count,
226                           const unsigned char* plocal_symbols,
227                           Relocatable_relocs*);
228
229   // Relocate a section during a relocatable link.
230   void
231   relocate_for_relocatable(const Relocate_info<32, big_endian>*,
232                            unsigned int sh_type,
233                            const unsigned char* prelocs,
234                            size_t reloc_count,
235                            Output_section* output_section,
236                            off_t offset_in_output_section,
237                            const Relocatable_relocs*,
238                            unsigned char* view,
239                            elfcpp::Elf_types<32>::Elf_Addr view_address,
240                            section_size_type view_size,
241                            unsigned char* reloc_view,
242                            section_size_type reloc_view_size);
243
244   // Return whether SYM is defined by the ABI.
245   bool
246   do_is_defined_by_abi(Symbol* sym) const
247   { return strcmp(sym->name(), "__tls_get_addr") == 0; }
248
249   // Return the size of the GOT section.
250   section_size_type
251   got_size()
252   {
253     gold_assert(this->got_ != NULL);
254     return this->got_->data_size();
255   }
256
257   // Map platform-specific reloc types
258   static unsigned int
259   get_real_reloc_type (unsigned int r_type);
260
261  private:
262   // The class which scans relocations.
263   class Scan
264   {
265    public:
266     Scan()
267       : issued_non_pic_error_(false)
268     { }
269
270     inline void
271     local(const General_options& options, Symbol_table* symtab,
272           Layout* layout, Target_arm* target,
273           Sized_relobj<32, big_endian>* object,
274           unsigned int data_shndx,
275           Output_section* output_section,
276           const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
277           const elfcpp::Sym<32, big_endian>& lsym);
278
279     inline void
280     global(const General_options& options, Symbol_table* symtab,
281            Layout* layout, Target_arm* target,
282            Sized_relobj<32, big_endian>* object,
283            unsigned int data_shndx,
284            Output_section* output_section,
285            const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
286            Symbol* gsym);
287
288    private:
289     static void
290     unsupported_reloc_local(Sized_relobj<32, big_endian>*,
291                             unsigned int r_type);
292
293     static void
294     unsupported_reloc_global(Sized_relobj<32, big_endian>*,
295                              unsigned int r_type, Symbol*);
296
297     void
298     check_non_pic(Relobj*, unsigned int r_type);
299
300     // Almost identical to Symbol::needs_plt_entry except that it also
301     // handles STT_ARM_TFUNC.
302     static bool
303     symbol_needs_plt_entry(const Symbol* sym)
304     {
305       // An undefined symbol from an executable does not need a PLT entry.
306       if (sym->is_undefined() && !parameters->options().shared())
307         return false;
308
309       return (!parameters->doing_static_link()
310               && (sym->type() == elfcpp::STT_FUNC
311                   || sym->type() == elfcpp::STT_ARM_TFUNC)
312               && (sym->is_from_dynobj()
313                   || sym->is_undefined()
314                   || sym->is_preemptible()));
315     }
316
317     // Whether we have issued an error about a non-PIC compilation.
318     bool issued_non_pic_error_;
319   };
320
321   // The class which implements relocation.
322   class Relocate
323   {
324    public:
325     Relocate()
326     { }
327
328     ~Relocate()
329     { }
330
331     // Return whether the static relocation needs to be applied.
332     inline bool
333     should_apply_static_reloc(const Sized_symbol<32>* gsym,
334                               int ref_flags,
335                               bool is_32bit,
336                               Output_section* output_section);
337
338     // Do a relocation.  Return false if the caller should not issue
339     // any warnings about this relocation.
340     inline bool
341     relocate(const Relocate_info<32, big_endian>*, Target_arm*,
342              Output_section*,  size_t relnum,
343              const elfcpp::Rel<32, big_endian>&,
344              unsigned int r_type, const Sized_symbol<32>*,
345              const Symbol_value<32>*,
346              unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
347              section_size_type);
348
349     // Return whether we want to pass flag NON_PIC_REF for this
350     // reloc.
351     static inline bool
352     reloc_is_non_pic (unsigned int r_type)
353     {
354       switch (r_type)
355         {
356         case elfcpp::R_ARM_REL32:
357         case elfcpp::R_ARM_THM_CALL:
358         case elfcpp::R_ARM_CALL:
359         case elfcpp::R_ARM_JUMP24:
360         case elfcpp::R_ARM_PREL31:
361           return true;
362         default:
363           return false;
364         }
365     }
366   };
367
368   // A class which returns the size required for a relocation type,
369   // used while scanning relocs during a relocatable link.
370   class Relocatable_size_for_reloc
371   {
372    public:
373     unsigned int
374     get_size_for_reloc(unsigned int, Relobj*);
375   };
376
377   // Get the GOT section, creating it if necessary.
378   Output_data_got<32, big_endian>*
379   got_section(Symbol_table*, Layout*);
380
381   // Get the GOT PLT section.
382   Output_data_space*
383   got_plt_section() const
384   {
385     gold_assert(this->got_plt_ != NULL);
386     return this->got_plt_;
387   }
388
389   // Create a PLT entry for a global symbol.
390   void
391   make_plt_entry(Symbol_table*, Layout*, Symbol*);
392
393   // Get the PLT section.
394   const Output_data_plt_arm<big_endian>*
395   plt_section() const
396   {
397     gold_assert(this->plt_ != NULL);
398     return this->plt_;
399   }
400
401   // Get the dynamic reloc section, creating it if necessary.
402   Reloc_section*
403   rel_dyn_section(Layout*);
404
405   // Return true if the symbol may need a COPY relocation.
406   // References from an executable object to non-function symbols
407   // defined in a dynamic object may need a COPY relocation.
408   bool
409   may_need_copy_reloc(Symbol* gsym)
410   {
411     return (gsym->type() != elfcpp::STT_ARM_TFUNC
412             && gsym->may_need_copy_reloc());
413   }
414
415   // Add a potential copy relocation.
416   void
417   copy_reloc(Symbol_table* symtab, Layout* layout,
418              Sized_relobj<32, big_endian>* object,
419              unsigned int shndx, Output_section* output_section,
420              Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
421   {
422     this->copy_relocs_.copy_reloc(symtab, layout,
423                                   symtab->get_sized_symbol<32>(sym),
424                                   object, shndx, output_section, reloc,
425                                   this->rel_dyn_section(layout));
426   }
427
428   // Information about this specific target which we pass to the
429   // general Target structure.
430   static const Target::Target_info arm_info;
431
432   // The types of GOT entries needed for this platform.
433   enum Got_type
434   {
435     GOT_TYPE_STANDARD = 0       // GOT entry for a regular symbol
436   };
437
438   // The GOT section.
439   Output_data_got<32, big_endian>* got_;
440   // The PLT section.
441   Output_data_plt_arm<big_endian>* plt_;
442   // The GOT PLT section.
443   Output_data_space* got_plt_;
444   // The dynamic reloc section.
445   Reloc_section* rel_dyn_;
446   // Relocs saved to avoid a COPY reloc.
447   Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
448   // Space for variables copied with a COPY reloc.
449   Output_data_space* dynbss_;
450 };
451
452 template<bool big_endian>
453 const Target::Target_info Target_arm<big_endian>::arm_info =
454 {
455   32,                   // size
456   big_endian,           // is_big_endian
457   elfcpp::EM_ARM,       // machine_code
458   false,                // has_make_symbol
459   false,                // has_resolve
460   false,                // has_code_fill
461   true,                 // is_default_stack_executable
462   '\0',                 // wrap_char
463   "/usr/lib/libc.so.1", // dynamic_linker
464   0x8000,               // default_text_segment_address
465   0x1000,               // abi_pagesize (overridable by -z max-page-size)
466   0x1000,               // common_pagesize (overridable by -z common-page-size)
467   elfcpp::SHN_UNDEF,    // small_common_shndx
468   elfcpp::SHN_UNDEF,    // large_common_shndx
469   0,                    // small_common_section_flags
470   0                     // large_common_section_flags
471 };
472
473 // Arm relocate functions class
474 //
475
476 template<bool big_endian>
477 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
478 {
479  public:
480   typedef enum
481   {
482     STATUS_OKAY,        // No error during relocation.
483     STATUS_OVERFLOW,    // Relocation oveflow.
484     STATUS_BAD_RELOC    // Relocation cannot be applied.
485   } Status;
486
487  private:
488   typedef Relocate_functions<32, big_endian> Base;
489   typedef Arm_relocate_functions<big_endian> This;
490
491   // Get an symbol value of *PSYMVAL with an ADDEND.  This is a wrapper
492   // to Symbol_value::value().  If HAS_THUMB_BIT is true, that LSB is used
493   // to distinguish ARM and THUMB functions and it is treated specially.
494   static inline Symbol_value<32>::Value
495   arm_symbol_value (const Sized_relobj<32, big_endian> *object,
496                     const Symbol_value<32>* psymval,
497                     Symbol_value<32>::Value addend,
498                     bool has_thumb_bit)
499   {
500     typedef Symbol_value<32>::Value Valtype;
501
502     if (has_thumb_bit)
503       {
504         Valtype raw = psymval->value(object, 0);
505         Valtype thumb_bit = raw & 1;
506         return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
507       }
508     else
509       return psymval->value(object, addend);
510   }
511
512   // FIXME: This probably only works for Android on ARM v5te. We should
513   // following GNU ld for the general case.
514   template<unsigned r_type>
515   static inline typename This::Status
516   arm_branch_common(unsigned char *view,
517                     const Sized_relobj<32, big_endian>* object,
518                     const Symbol_value<32>* psymval,
519                     elfcpp::Elf_types<32>::Elf_Addr address,
520                     bool has_thumb_bit)
521   {
522     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
523     Valtype* wv = reinterpret_cast<Valtype*>(view);
524     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
525      
526     bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
527                       && ((val & 0x0f000000UL) == 0x0a000000UL);
528     bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
529     bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
530                             && ((val & 0x0f000000UL) == 0x0b000000UL);
531     bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
532     bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
533
534     if (r_type == elfcpp::R_ARM_CALL)
535       {
536         if (!insn_is_uncond_bl && !insn_is_blx)
537           return This::STATUS_BAD_RELOC;
538       }
539     else if (r_type == elfcpp::R_ARM_JUMP24)
540       {
541         if (!insn_is_b && !insn_is_cond_bl)
542           return This::STATUS_BAD_RELOC;
543       }
544     else if (r_type == elfcpp::R_ARM_PLT32)
545       {
546         if (!insn_is_any_branch)
547           return This::STATUS_BAD_RELOC;
548       }
549     else
550       gold_unreachable();
551
552     Valtype addend = utils::sign_extend<26>(val << 2);
553     Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
554                  - address);
555
556     // If target has thumb bit set, we need to either turn the BL
557     // into a BLX (for ARMv5 or above) or generate a stub.
558     if (x & 1)
559       {
560         // Turn BL to BLX.
561         if (insn_is_uncond_bl)
562           val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
563         else
564           return This::STATUS_BAD_RELOC;
565       }
566     else
567       gold_assert(!insn_is_blx);
568
569     val = utils::bit_select(val, (x >> 2), 0xffffffUL);
570     elfcpp::Swap<32, big_endian>::writeval(wv, val);
571     return (utils::has_overflow<26>(x)
572             ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
573   }
574
575  public:
576
577   // R_ARM_ABS8: S + A
578   static inline typename This::Status
579   abs8(unsigned char *view,
580        const Sized_relobj<32, big_endian>* object,
581        const Symbol_value<32>* psymval, bool has_thumb_bit)
582   {
583     typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
584     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
585     Valtype* wv = reinterpret_cast<Valtype*>(view);
586     Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
587     Reltype addend = utils::sign_extend<8>(val);
588     Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
589     val = utils::bit_select(val, x, 0xffU);
590     elfcpp::Swap<8, big_endian>::writeval(wv, val);
591     return (utils::has_signed_unsigned_overflow<8>(x)
592             ? This::STATUS_OVERFLOW
593             : This::STATUS_OKAY);
594   }
595
596   // R_ARM_ABS32: (S + A) | T
597   static inline typename This::Status
598   abs32(unsigned char *view,
599         const Sized_relobj<32, big_endian>* object,
600         const Symbol_value<32>* psymval,
601         bool has_thumb_bit)
602   {
603     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
604     Valtype* wv = reinterpret_cast<Valtype*>(view);
605     Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
606     Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
607     elfcpp::Swap<32, big_endian>::writeval(wv, x);
608     return This::STATUS_OKAY;
609   }
610
611   // R_ARM_REL32: (S + A) | T - P
612   static inline typename This::Status
613   rel32(unsigned char *view,
614         const Sized_relobj<32, big_endian>* object,
615         const Symbol_value<32>* psymval,
616         elfcpp::Elf_types<32>::Elf_Addr address,
617         bool has_thumb_bit)
618   {
619     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
620     Valtype* wv = reinterpret_cast<Valtype*>(view);
621     Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
622     Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit) 
623                  - address);
624     elfcpp::Swap<32, big_endian>::writeval(wv, x);
625     return This::STATUS_OKAY;
626   }
627
628   // R_ARM_THM_CALL: (S + A) | T - P
629   static inline typename This::Status
630   thm_call(unsigned char *view,
631            const Sized_relobj<32, big_endian>* object,
632            const Symbol_value<32>* psymval,
633            elfcpp::Elf_types<32>::Elf_Addr address,
634            bool has_thumb_bit)
635   {
636     // A thumb call consists of two instructions.
637     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
638     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
639     Valtype* wv = reinterpret_cast<Valtype*>(view);
640     Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
641     Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
642     // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
643     gold_assert((lo & 0xf800) == 0xf800);
644     Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
645                                            | ((lo & 0x7ff) << 1));
646     Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
647                  - address);
648
649     // If target has no thumb bit set, we need to either turn the BL
650     // into a BLX (for ARMv5 or above) or generate a stub.
651     if ((x & 1) == 0)
652       {
653         // This only works for ARMv5 and above with interworking enabled.
654         lo &= 0xefff;
655       }
656     hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
657     lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
658     elfcpp::Swap<16, big_endian>::writeval(wv, hi);
659     elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
660     return (utils::has_overflow<23>(x)
661             ? This::STATUS_OVERFLOW
662             : This::STATUS_OKAY);
663   }
664
665   // R_ARM_BASE_PREL: B(S) + A - P
666   static inline typename This::Status
667   base_prel(unsigned char* view,
668             elfcpp::Elf_types<32>::Elf_Addr origin,
669             elfcpp::Elf_types<32>::Elf_Addr address)
670   {
671     Base::rel32(view, origin - address);
672     return STATUS_OKAY;
673   }
674
675   // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
676   static inline typename This::Status
677   got_brel(unsigned char* view,
678            typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
679   {
680     Base::rel32(view, got_offset);
681     return This::STATUS_OKAY;
682   }
683
684   // R_ARM_GOT_PREL: GOT(S) + A – P
685   static inline typename This::Status
686   got_prel(unsigned char* view,
687            typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
688            elfcpp::Elf_types<32>::Elf_Addr address)
689   {
690     Base::rel32(view, got_offset - address);
691     return This::STATUS_OKAY;
692   }
693
694   // R_ARM_PLT32: (S + A) | T - P
695   static inline typename This::Status
696   plt32(unsigned char *view,
697         const Sized_relobj<32, big_endian>* object,
698         const Symbol_value<32>* psymval,
699         elfcpp::Elf_types<32>::Elf_Addr address,
700         bool has_thumb_bit)
701   {
702     return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
703                                                   address, has_thumb_bit);
704   }
705
706   // R_ARM_CALL: (S + A) | T - P
707   static inline typename This::Status
708   call(unsigned char *view,
709        const Sized_relobj<32, big_endian>* object,
710        const Symbol_value<32>* psymval,
711        elfcpp::Elf_types<32>::Elf_Addr address,
712        bool has_thumb_bit)
713   {
714     return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
715                                                  address, has_thumb_bit);
716   }
717
718   // R_ARM_JUMP24: (S + A) | T - P
719   static inline typename This::Status
720   jump24(unsigned char *view,
721          const Sized_relobj<32, big_endian>* object,
722          const Symbol_value<32>* psymval,
723          elfcpp::Elf_types<32>::Elf_Addr address,
724          bool has_thumb_bit)
725   {
726     return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
727                                                    address, has_thumb_bit);
728   }
729
730   // R_ARM_PREL: (S + A) | T - P
731   static inline typename This::Status
732   prel31(unsigned char *view,
733          const Sized_relobj<32, big_endian>* object,
734          const Symbol_value<32>* psymval,
735          elfcpp::Elf_types<32>::Elf_Addr address,
736          bool has_thumb_bit)
737   {
738     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
739     Valtype* wv = reinterpret_cast<Valtype*>(view);
740     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
741     Valtype addend = utils::sign_extend<31>(val);
742     Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
743                  - address);
744     val = utils::bit_select(val, x, 0x7fffffffU);
745     elfcpp::Swap<32, big_endian>::writeval(wv, val);
746     return (utils::has_overflow<31>(x) ?
747             This::STATUS_OVERFLOW : This::STATUS_OKAY);
748   }
749 };
750
751 // Get the GOT section, creating it if necessary.
752
753 template<bool big_endian>
754 Output_data_got<32, big_endian>*
755 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
756 {
757   if (this->got_ == NULL)
758     {
759       gold_assert(symtab != NULL && layout != NULL);
760
761       this->got_ = new Output_data_got<32, big_endian>();
762
763       Output_section* os;
764       os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
765                                            (elfcpp::SHF_ALLOC
766                                             | elfcpp::SHF_WRITE),
767                                            this->got_);
768       os->set_is_relro();
769
770       // The old GNU linker creates a .got.plt section.  We just
771       // create another set of data in the .got section.  Note that we
772       // always create a PLT if we create a GOT, although the PLT
773       // might be empty.
774       this->got_plt_ = new Output_data_space(4, "** GOT PLT");
775       os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
776                                            (elfcpp::SHF_ALLOC
777                                             | elfcpp::SHF_WRITE),
778                                            this->got_plt_);
779       os->set_is_relro();
780
781       // The first three entries are reserved.
782       this->got_plt_->set_current_data_size(3 * 4);
783
784       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
785       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
786                                     this->got_plt_,
787                                     0, 0, elfcpp::STT_OBJECT,
788                                     elfcpp::STB_LOCAL,
789                                     elfcpp::STV_HIDDEN, 0,
790                                     false, false);
791     }
792   return this->got_;
793 }
794
795 // Get the dynamic reloc section, creating it if necessary.
796
797 template<bool big_endian>
798 typename Target_arm<big_endian>::Reloc_section*
799 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
800 {
801   if (this->rel_dyn_ == NULL)
802     {
803       gold_assert(layout != NULL);
804       this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
805       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
806                                       elfcpp::SHF_ALLOC, this->rel_dyn_);
807     }
808   return this->rel_dyn_;
809 }
810
811 // A class to handle the PLT data.
812
813 template<bool big_endian>
814 class Output_data_plt_arm : public Output_section_data
815 {
816  public:
817   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
818     Reloc_section;
819
820   Output_data_plt_arm(Layout*, Output_data_space*);
821
822   // Add an entry to the PLT.
823   void
824   add_entry(Symbol* gsym);
825
826   // Return the .rel.plt section data.
827   const Reloc_section*
828   rel_plt() const
829   { return this->rel_; }
830
831  protected:
832   void
833   do_adjust_output_section(Output_section* os);
834
835   // Write to a map file.
836   void
837   do_print_to_mapfile(Mapfile* mapfile) const
838   { mapfile->print_output_data(this, _("** PLT")); }
839
840  private:
841   // Template for the first PLT entry.
842   static const uint32_t first_plt_entry[5];
843
844   // Template for subsequent PLT entries. 
845   static const uint32_t plt_entry[3];
846
847   // Set the final size.
848   void
849   set_final_data_size()
850   {
851     this->set_data_size(sizeof(first_plt_entry)
852                         + this->count_ * sizeof(plt_entry));
853   }
854
855   // Write out the PLT data.
856   void
857   do_write(Output_file*);
858
859   // The reloc section.
860   Reloc_section* rel_;
861   // The .got.plt section.
862   Output_data_space* got_plt_;
863   // The number of PLT entries.
864   unsigned int count_;
865 };
866
867 // Create the PLT section.  The ordinary .got section is an argument,
868 // since we need to refer to the start.  We also create our own .got
869 // section just for PLT entries.
870
871 template<bool big_endian>
872 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
873                                                      Output_data_space* got_plt)
874   : Output_section_data(4), got_plt_(got_plt), count_(0)
875 {
876   this->rel_ = new Reloc_section(false);
877   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
878                                   elfcpp::SHF_ALLOC, this->rel_);
879 }
880
881 template<bool big_endian>
882 void
883 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
884 {
885   os->set_entsize(0);
886 }
887
888 // Add an entry to the PLT.
889
890 template<bool big_endian>
891 void
892 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
893 {
894   gold_assert(!gsym->has_plt_offset());
895
896   // Note that when setting the PLT offset we skip the initial
897   // reserved PLT entry.
898   gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
899                        + sizeof(first_plt_entry));
900
901   ++this->count_;
902
903   section_offset_type got_offset = this->got_plt_->current_data_size();
904
905   // Every PLT entry needs a GOT entry which points back to the PLT
906   // entry (this will be changed by the dynamic linker, normally
907   // lazily when the function is called).
908   this->got_plt_->set_current_data_size(got_offset + 4);
909
910   // Every PLT entry needs a reloc.
911   gsym->set_needs_dynsym_entry();
912   this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
913                          got_offset);
914
915   // Note that we don't need to save the symbol.  The contents of the
916   // PLT are independent of which symbols are used.  The symbols only
917   // appear in the relocations.
918 }
919
920 // ARM PLTs.
921 // FIXME:  This is not very flexible.  Right now this has only been tested
922 // on armv5te.  If we are to support additional architecture features like
923 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
924
925 // The first entry in the PLT.
926 template<bool big_endian>
927 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
928 {
929   0xe52de004,   // str   lr, [sp, #-4]!
930   0xe59fe004,   // ldr   lr, [pc, #4]
931   0xe08fe00e,   // add   lr, pc, lr 
932   0xe5bef008,   // ldr   pc, [lr, #8]!
933   0x00000000,   // &GOT[0] - .
934 };
935
936 // Subsequent entries in the PLT.
937
938 template<bool big_endian>
939 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
940 {
941   0xe28fc600,   // add   ip, pc, #0xNN00000
942   0xe28cca00,   // add   ip, ip, #0xNN000
943   0xe5bcf000,   // ldr   pc, [ip, #0xNNN]!
944 };
945
946 // Write out the PLT.  This uses the hand-coded instructions above,
947 // and adjusts them as needed.  This is all specified by the arm ELF
948 // Processor Supplement.
949
950 template<bool big_endian>
951 void
952 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
953 {
954   const off_t offset = this->offset();
955   const section_size_type oview_size =
956     convert_to_section_size_type(this->data_size());
957   unsigned char* const oview = of->get_output_view(offset, oview_size);
958
959   const off_t got_file_offset = this->got_plt_->offset();
960   const section_size_type got_size =
961     convert_to_section_size_type(this->got_plt_->data_size());
962   unsigned char* const got_view = of->get_output_view(got_file_offset,
963                                                       got_size);
964   unsigned char* pov = oview;
965
966   elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
967   elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
968
969   // Write first PLT entry.  All but the last word are constants.
970   const size_t num_first_plt_words = (sizeof(first_plt_entry)
971                                       / sizeof(plt_entry[0]));
972   for (size_t i = 0; i < num_first_plt_words - 1; i++)
973     elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
974   // Last word in first PLT entry is &GOT[0] - .
975   elfcpp::Swap<32, big_endian>::writeval(pov + 16,
976                                          got_address - (plt_address + 16));
977   pov += sizeof(first_plt_entry);
978
979   unsigned char* got_pov = got_view;
980
981   memset(got_pov, 0, 12);
982   got_pov += 12;
983
984   const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
985   unsigned int plt_offset = sizeof(first_plt_entry);
986   unsigned int plt_rel_offset = 0;
987   unsigned int got_offset = 12;
988   const unsigned int count = this->count_;
989   for (unsigned int i = 0;
990        i < count;
991        ++i,
992          pov += sizeof(plt_entry),
993          got_pov += 4,
994          plt_offset += sizeof(plt_entry),
995          plt_rel_offset += rel_size,
996          got_offset += 4)
997     {
998       // Set and adjust the PLT entry itself.
999       int32_t offset = ((got_address + got_offset)
1000                          - (plt_address + plt_offset + 8));
1001
1002       gold_assert(offset >= 0 && offset < 0x0fffffff);
1003       uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
1004       elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
1005       uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
1006       elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
1007       uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
1008       elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
1009
1010       // Set the entry in the GOT.
1011       elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
1012     }
1013
1014   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1015   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1016
1017   of->write_output_view(offset, oview_size, oview);
1018   of->write_output_view(got_file_offset, got_size, got_view);
1019 }
1020
1021 // Create a PLT entry for a global symbol.
1022
1023 template<bool big_endian>
1024 void
1025 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1026                                        Symbol* gsym)
1027 {
1028   if (gsym->has_plt_offset())
1029     return;
1030
1031   if (this->plt_ == NULL)
1032     {
1033       // Create the GOT sections first.
1034       this->got_section(symtab, layout);
1035
1036       this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
1037       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1038                                       (elfcpp::SHF_ALLOC
1039                                        | elfcpp::SHF_EXECINSTR),
1040                                       this->plt_);
1041     }
1042   this->plt_->add_entry(gsym);
1043 }
1044
1045 // Report an unsupported relocation against a local symbol.
1046
1047 template<bool big_endian>
1048 void
1049 Target_arm<big_endian>::Scan::unsupported_reloc_local(
1050     Sized_relobj<32, big_endian>* object,
1051     unsigned int r_type)
1052 {
1053   gold_error(_("%s: unsupported reloc %u against local symbol"),
1054              object->name().c_str(), r_type);
1055 }
1056
1057 // We are about to emit a dynamic relocation of type R_TYPE.  If the
1058 // dynamic linker does not support it, issue an error.  The GNU linker
1059 // only issues a non-PIC error for an allocated read-only section.
1060 // Here we know the section is allocated, but we don't know that it is
1061 // read-only.  But we check for all the relocation types which the
1062 // glibc dynamic linker supports, so it seems appropriate to issue an
1063 // error even if the section is not read-only.
1064
1065 template<bool big_endian>
1066 void
1067 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
1068                                             unsigned int r_type)
1069 {
1070   switch (r_type)
1071     {
1072     // These are the relocation types supported by glibc for ARM.
1073     case elfcpp::R_ARM_RELATIVE:
1074     case elfcpp::R_ARM_COPY:
1075     case elfcpp::R_ARM_GLOB_DAT:
1076     case elfcpp::R_ARM_JUMP_SLOT:
1077     case elfcpp::R_ARM_ABS32:
1078     case elfcpp::R_ARM_PC24:
1079     // FIXME: The following 3 types are not supported by Android's dynamic
1080     // linker.
1081     case elfcpp::R_ARM_TLS_DTPMOD32:
1082     case elfcpp::R_ARM_TLS_DTPOFF32:
1083     case elfcpp::R_ARM_TLS_TPOFF32:
1084       return;
1085
1086     default:
1087       // This prevents us from issuing more than one error per reloc
1088       // section.  But we can still wind up issuing more than one
1089       // error per object file.
1090       if (this->issued_non_pic_error_)
1091         return;
1092       object->error(_("requires unsupported dynamic reloc; "
1093                       "recompile with -fPIC"));
1094       this->issued_non_pic_error_ = true;
1095       return;
1096
1097     case elfcpp::R_ARM_NONE:
1098       gold_unreachable();
1099     }
1100 }
1101
1102 // Scan a relocation for a local symbol.
1103 // FIXME: This only handles a subset of relocation types used by Android
1104 // on ARM v5te devices.
1105
1106 template<bool big_endian>
1107 inline void
1108 Target_arm<big_endian>::Scan::local(const General_options&,
1109                                     Symbol_table* symtab,
1110                                     Layout* layout,
1111                                     Target_arm* target,
1112                                     Sized_relobj<32, big_endian>* object,
1113                                     unsigned int data_shndx,
1114                                     Output_section* output_section,
1115                                     const elfcpp::Rel<32, big_endian>& reloc,
1116                                     unsigned int r_type,
1117                                     const elfcpp::Sym<32, big_endian>&)
1118 {
1119   r_type = get_real_reloc_type(r_type);
1120   switch (r_type)
1121     {
1122     case elfcpp::R_ARM_NONE:
1123       break;
1124
1125     case elfcpp::R_ARM_ABS8:
1126       if (parameters->options().output_is_position_independent())
1127         {
1128           // FIXME: Create a dynamic relocation for this location.
1129           gold_error(_("%s: gold bug: need dynamic ABS8 reloc"),
1130                      object->name().c_str());
1131         }
1132       break;
1133
1134     case elfcpp::R_ARM_ABS32:
1135       // If building a shared library (or a position-independent
1136       // executable), we need to create a dynamic relocation for
1137       // this location. The relocation applied at link time will
1138       // apply the link-time value, so we flag the location with
1139       // an R_ARM_RELATIVE relocation so the dynamic loader can
1140       // relocate it easily.
1141       if (parameters->options().output_is_position_independent())
1142         {
1143           Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1144           unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1145           // If we are to add more other reloc types than R_ARM_ABS32,
1146           // we need to add check_non_pic(object, r_type) here.
1147           rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
1148                                       output_section, data_shndx,
1149                                       reloc.get_r_offset());
1150         }
1151       break;
1152
1153     case elfcpp::R_ARM_REL32:
1154     case elfcpp::R_ARM_THM_CALL:
1155     case elfcpp::R_ARM_CALL:
1156     case elfcpp::R_ARM_PREL31:
1157     case elfcpp::R_ARM_JUMP24:
1158     case elfcpp::R_ARM_PLT32:
1159       break;
1160
1161     case elfcpp::R_ARM_GOTOFF32:
1162       // We need a GOT section:
1163       target->got_section(symtab, layout);
1164       break;
1165
1166     case elfcpp::R_ARM_BASE_PREL:
1167       // FIXME: What about this?
1168       break;
1169
1170     case elfcpp::R_ARM_GOT_BREL:
1171     case elfcpp::R_ARM_GOT_PREL:
1172       {
1173         // The symbol requires a GOT entry.
1174         Output_data_got<32, big_endian>* got =
1175           target->got_section(symtab, layout);
1176         unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1177         if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1178           {
1179             // If we are generating a shared object, we need to add a
1180             // dynamic RELATIVE relocation for this symbol's GOT entry.
1181             if (parameters->options().output_is_position_independent())
1182               {
1183                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1184                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1185                 rel_dyn->add_local_relative(
1186                     object, r_sym, elfcpp::R_ARM_RELATIVE, got,
1187                     object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1188               }
1189           }
1190       }
1191       break;
1192
1193     case elfcpp::R_ARM_TARGET1:
1194       // This should have been mapped to another type already.
1195       // Fall through.
1196     case elfcpp::R_ARM_COPY:
1197     case elfcpp::R_ARM_GLOB_DAT:
1198     case elfcpp::R_ARM_JUMP_SLOT:
1199     case elfcpp::R_ARM_RELATIVE:
1200       // These are relocations which should only be seen by the
1201       // dynamic linker, and should never be seen here.
1202       gold_error(_("%s: unexpected reloc %u in object file"),
1203                  object->name().c_str(), r_type);
1204       break;
1205
1206     default:
1207       unsupported_reloc_local(object, r_type);
1208       break;
1209     }
1210 }
1211
1212 // Report an unsupported relocation against a global symbol.
1213
1214 template<bool big_endian>
1215 void
1216 Target_arm<big_endian>::Scan::unsupported_reloc_global(
1217     Sized_relobj<32, big_endian>* object,
1218     unsigned int r_type,
1219     Symbol* gsym)
1220 {
1221   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1222              object->name().c_str(), r_type, gsym->demangled_name().c_str());
1223 }
1224
1225 // Scan a relocation for a global symbol.
1226 // FIXME: This only handles a subset of relocation types used by Android
1227 // on ARM v5te devices.
1228
1229 template<bool big_endian>
1230 inline void
1231 Target_arm<big_endian>::Scan::global(const General_options&,
1232                                      Symbol_table* symtab,
1233                                      Layout* layout,
1234                                      Target_arm* target,
1235                                      Sized_relobj<32, big_endian>* object,
1236                                      unsigned int data_shndx,
1237                                      Output_section* output_section,
1238                                      const elfcpp::Rel<32, big_endian>& reloc,
1239                                      unsigned int r_type,
1240                                      Symbol* gsym)
1241 {
1242   r_type = get_real_reloc_type(r_type);
1243   switch (r_type)
1244     {
1245     case elfcpp::R_ARM_NONE:
1246       break;
1247
1248     case elfcpp::R_ARM_ABS8:
1249       // Make a dynamic relocation if necessary.
1250       if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1251         {
1252           // FIXME: Create a dynamic relocation for this location.
1253           gold_error(_("%s: gold bug: need dynamic ABS8 reloc for %s"),
1254                      object->name().c_str(), gsym->demangled_name().c_str());
1255         }
1256       break;
1257
1258     case elfcpp::R_ARM_ABS32:
1259       {
1260         // Make a dynamic relocation if necessary.
1261         if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1262           {
1263             if (target->may_need_copy_reloc(gsym))
1264               {
1265                 target->copy_reloc(symtab, layout, object,
1266                                    data_shndx, output_section, gsym, reloc);
1267               }
1268             else if (gsym->can_use_relative_reloc(false))
1269               {
1270                 // If we are to add more other reloc types than R_ARM_ABS32,
1271                 // we need to add check_non_pic(object, r_type) here.
1272                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1273                 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
1274                                              output_section, object,
1275                                              data_shndx, reloc.get_r_offset());
1276               }
1277             else
1278               {
1279                 // If we are to add more other reloc types than R_ARM_ABS32,
1280                 // we need to add check_non_pic(object, r_type) here.
1281                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1282                 rel_dyn->add_global(gsym, r_type, output_section, object,
1283                                     data_shndx, reloc.get_r_offset());
1284               }
1285           }
1286       }
1287       break;
1288
1289     case elfcpp::R_ARM_REL32:
1290     case elfcpp::R_ARM_PREL31:
1291       {
1292         // Make a dynamic relocation if necessary.
1293         int flags = Symbol::NON_PIC_REF;
1294         if (gsym->needs_dynamic_reloc(flags))
1295           {
1296             if (target->may_need_copy_reloc(gsym))
1297               {
1298                 target->copy_reloc(symtab, layout, object,
1299                                    data_shndx, output_section, gsym, reloc);
1300               }
1301             else
1302               {
1303                 check_non_pic(object, r_type);
1304                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1305                 rel_dyn->add_global(gsym, r_type, output_section, object,
1306                                     data_shndx, reloc.get_r_offset());
1307               }
1308           }
1309       }
1310       break;
1311
1312     case elfcpp::R_ARM_JUMP24:
1313     case elfcpp::R_ARM_THM_CALL:
1314     case elfcpp::R_ARM_CALL:
1315       {
1316         if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
1317           target->make_plt_entry(symtab, layout, gsym);
1318         // Make a dynamic relocation if necessary.
1319         int flags = Symbol::NON_PIC_REF;
1320         if (gsym->type() == elfcpp::STT_FUNC
1321             || gsym->type() == elfcpp::STT_ARM_TFUNC)
1322           flags |= Symbol::FUNCTION_CALL;
1323         if (gsym->needs_dynamic_reloc(flags))
1324           {
1325             if (target->may_need_copy_reloc(gsym))
1326               {
1327                 target->copy_reloc(symtab, layout, object,
1328                                    data_shndx, output_section, gsym,
1329                                    reloc);
1330               }
1331             else
1332               {
1333                 check_non_pic(object, r_type);
1334                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1335                 rel_dyn->add_global(gsym, r_type, output_section, object,
1336                                     data_shndx, reloc.get_r_offset());
1337               }
1338           }
1339       }
1340       break;
1341
1342     case elfcpp::R_ARM_PLT32:
1343       // If the symbol is fully resolved, this is just a relative
1344       // local reloc.  Otherwise we need a PLT entry.
1345       if (gsym->final_value_is_known())
1346         break;
1347       // If building a shared library, we can also skip the PLT entry
1348       // if the symbol is defined in the output file and is protected
1349       // or hidden.
1350       if (gsym->is_defined()
1351           && !gsym->is_from_dynobj()
1352           && !gsym->is_preemptible())
1353         break;
1354       target->make_plt_entry(symtab, layout, gsym);
1355       break;
1356
1357     case elfcpp::R_ARM_GOTOFF32:
1358       // We need a GOT section.
1359       target->got_section(symtab, layout);
1360       break;
1361
1362     case elfcpp::R_ARM_BASE_PREL:
1363       // FIXME: What about this?
1364       break;
1365       
1366     case elfcpp::R_ARM_GOT_BREL:
1367     case elfcpp::R_ARM_GOT_PREL:
1368       {
1369         // The symbol requires a GOT entry.
1370         Output_data_got<32, big_endian>* got =
1371           target->got_section(symtab, layout);
1372         if (gsym->final_value_is_known())
1373           got->add_global(gsym, GOT_TYPE_STANDARD);
1374         else
1375           {
1376             // If this symbol is not fully resolved, we need to add a
1377             // GOT entry with a dynamic relocation.
1378             Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1379             if (gsym->is_from_dynobj()
1380                 || gsym->is_undefined()
1381                 || gsym->is_preemptible())
1382               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1383                                        rel_dyn, elfcpp::R_ARM_GLOB_DAT);
1384             else
1385               {
1386                 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1387                   rel_dyn->add_global_relative(
1388                       gsym, elfcpp::R_ARM_RELATIVE, got,
1389                       gsym->got_offset(GOT_TYPE_STANDARD));
1390               }
1391           }
1392       }
1393       break;
1394
1395     case elfcpp::R_ARM_TARGET1:
1396       // This should have been mapped to another type already.
1397       // Fall through.
1398     case elfcpp::R_ARM_COPY:
1399     case elfcpp::R_ARM_GLOB_DAT:
1400     case elfcpp::R_ARM_JUMP_SLOT:
1401     case elfcpp::R_ARM_RELATIVE:
1402       // These are relocations which should only be seen by the
1403       // dynamic linker, and should never be seen here.
1404       gold_error(_("%s: unexpected reloc %u in object file"),
1405                  object->name().c_str(), r_type);
1406       break;
1407
1408     default:
1409       unsupported_reloc_global(object, r_type, gsym);
1410       break;
1411     }
1412 }
1413
1414 // Process relocations for gc.
1415
1416 template<bool big_endian>
1417 void
1418 Target_arm<big_endian>::gc_process_relocs(const General_options& options,
1419                                           Symbol_table* symtab,
1420                                           Layout* layout,
1421                                           Sized_relobj<32, big_endian>* object,
1422                                           unsigned int data_shndx,
1423                                           unsigned int,
1424                                           const unsigned char* prelocs,
1425                                           size_t reloc_count,
1426                                           Output_section* output_section,
1427                                           bool needs_special_offset_handling,
1428                                           size_t local_symbol_count,
1429                                           const unsigned char* plocal_symbols)
1430 {
1431   typedef Target_arm<big_endian> Arm;
1432   typedef typename Target_arm<big_endian>::Scan Scan;
1433
1434   gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
1435     options,
1436     symtab,
1437     layout,
1438     this,
1439     object,
1440     data_shndx,
1441     prelocs,
1442     reloc_count,
1443     output_section,
1444     needs_special_offset_handling,
1445     local_symbol_count,
1446     plocal_symbols);
1447 }
1448
1449 // Scan relocations for a section.
1450
1451 template<bool big_endian>
1452 void
1453 Target_arm<big_endian>::scan_relocs(const General_options& options,
1454                                     Symbol_table* symtab,
1455                                     Layout* layout,
1456                                     Sized_relobj<32, big_endian>* object,
1457                                     unsigned int data_shndx,
1458                                     unsigned int sh_type,
1459                                     const unsigned char* prelocs,
1460                                     size_t reloc_count,
1461                                     Output_section* output_section,
1462                                     bool needs_special_offset_handling,
1463                                     size_t local_symbol_count,
1464                                     const unsigned char* plocal_symbols)
1465 {
1466   typedef typename Target_arm<big_endian>::Scan Scan;
1467   if (sh_type == elfcpp::SHT_RELA)
1468     {
1469       gold_error(_("%s: unsupported RELA reloc section"),
1470                  object->name().c_str());
1471       return;
1472     }
1473
1474   gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
1475     options,
1476     symtab,
1477     layout,
1478     this,
1479     object,
1480     data_shndx,
1481     prelocs,
1482     reloc_count,
1483     output_section,
1484     needs_special_offset_handling,
1485     local_symbol_count,
1486     plocal_symbols);
1487 }
1488
1489 // Finalize the sections.
1490
1491 template<bool big_endian>
1492 void
1493 Target_arm<big_endian>::do_finalize_sections(Layout* layout)
1494 {
1495   // Fill in some more dynamic tags.
1496   Output_data_dynamic* const odyn = layout->dynamic_data();
1497   if (odyn != NULL)
1498     {
1499       if (this->got_plt_ != NULL)
1500         odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1501
1502       if (this->plt_ != NULL)
1503         {
1504           const Output_data* od = this->plt_->rel_plt();
1505           odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1506           odyn->add_section_address(elfcpp::DT_JMPREL, od);
1507           odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1508         }
1509
1510       if (this->rel_dyn_ != NULL)
1511         {
1512           const Output_data* od = this->rel_dyn_;
1513           odyn->add_section_address(elfcpp::DT_REL, od);
1514           odyn->add_section_size(elfcpp::DT_RELSZ, od);
1515           odyn->add_constant(elfcpp::DT_RELENT,
1516                              elfcpp::Elf_sizes<32>::rel_size);
1517         }
1518
1519       if (!parameters->options().shared())
1520         {
1521           // The value of the DT_DEBUG tag is filled in by the dynamic
1522           // linker at run time, and used by the debugger.
1523           odyn->add_constant(elfcpp::DT_DEBUG, 0);
1524         }
1525     }
1526
1527   // Emit any relocs we saved in an attempt to avoid generating COPY
1528   // relocs.
1529   if (this->copy_relocs_.any_saved_relocs())
1530     this->copy_relocs_.emit(this->rel_dyn_section(layout));
1531
1532   // For the ARM target, we need to add a PT_ARM_EXIDX segment for
1533   // the .ARM.exidx section.
1534   if (!layout->script_options()->saw_phdrs_clause()
1535       && !parameters->options().relocatable())
1536     {
1537       Output_section* exidx_section =
1538         layout->find_output_section(".ARM.exidx");
1539
1540       if (exidx_section != NULL
1541           && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
1542         {
1543           gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
1544                       == NULL);
1545           Output_segment*  exidx_segment =
1546             layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
1547           exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
1548         }
1549     }
1550 }
1551
1552 // Return whether a direct absolute static relocation needs to be applied.
1553 // In cases where Scan::local() or Scan::global() has created
1554 // a dynamic relocation other than R_ARM_RELATIVE, the addend
1555 // of the relocation is carried in the data, and we must not
1556 // apply the static relocation.
1557
1558 template<bool big_endian>
1559 inline bool
1560 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
1561     const Sized_symbol<32>* gsym,
1562     int ref_flags,
1563     bool is_32bit,
1564     Output_section* output_section)
1565 {
1566   // If the output section is not allocated, then we didn't call
1567   // scan_relocs, we didn't create a dynamic reloc, and we must apply
1568   // the reloc here.
1569   if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1570       return true;
1571
1572   // For local symbols, we will have created a non-RELATIVE dynamic
1573   // relocation only if (a) the output is position independent,
1574   // (b) the relocation is absolute (not pc- or segment-relative), and
1575   // (c) the relocation is not 32 bits wide.
1576   if (gsym == NULL)
1577     return !(parameters->options().output_is_position_independent()
1578              && (ref_flags & Symbol::ABSOLUTE_REF)
1579              && !is_32bit);
1580
1581   // For global symbols, we use the same helper routines used in the
1582   // scan pass.  If we did not create a dynamic relocation, or if we
1583   // created a RELATIVE dynamic relocation, we should apply the static
1584   // relocation.
1585   bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1586   bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1587                  && gsym->can_use_relative_reloc(ref_flags
1588                                                  & Symbol::FUNCTION_CALL);
1589   return !has_dyn || is_rel;
1590 }
1591
1592 // Perform a relocation.
1593
1594 template<bool big_endian>
1595 inline bool
1596 Target_arm<big_endian>::Relocate::relocate(
1597     const Relocate_info<32, big_endian>* relinfo,
1598     Target_arm* target,
1599     Output_section *output_section,
1600     size_t relnum,
1601     const elfcpp::Rel<32, big_endian>& rel,
1602     unsigned int r_type,
1603     const Sized_symbol<32>* gsym,
1604     const Symbol_value<32>* psymval,
1605     unsigned char* view,
1606     elfcpp::Elf_types<32>::Elf_Addr address,
1607     section_size_type /* view_size */ )
1608 {
1609   typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
1610
1611   r_type = get_real_reloc_type(r_type);
1612
1613   // If this the symbol may be a Thumb function, set thumb bit to 1.
1614   bool has_thumb_bit = ((gsym != NULL)
1615                         && (gsym->type() == elfcpp::STT_FUNC
1616                             || gsym->type() == elfcpp::STT_ARM_TFUNC));
1617
1618   // Pick the value to use for symbols defined in shared objects.
1619   Symbol_value<32> symval;
1620   if (gsym != NULL
1621       && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
1622     {
1623       symval.set_output_value(target->plt_section()->address()
1624                               + gsym->plt_offset());
1625       psymval = &symval;
1626       has_thumb_bit = 0;
1627     }
1628
1629   const Sized_relobj<32, big_endian>* object = relinfo->object;
1630   
1631   // Get the GOT offset if needed.
1632   // The GOT pointer points to the end of the GOT section.
1633   // We need to subtract the size of the GOT section to get
1634   // the actual offset to use in the relocation.
1635   bool have_got_offset = false;
1636   unsigned int got_offset = 0;
1637   switch (r_type)
1638     {
1639     case elfcpp::R_ARM_GOT_BREL:
1640     case elfcpp::R_ARM_GOT_PREL:
1641       if (gsym != NULL)
1642         {
1643           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1644           got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1645                         - target->got_size());
1646         }
1647       else
1648         {
1649           unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1650           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1651           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1652                         - target->got_size());
1653         }
1654       have_got_offset = true;
1655       break;
1656
1657     default:
1658       break;
1659     }
1660
1661   typename Arm_relocate_functions::Status reloc_status =
1662         Arm_relocate_functions::STATUS_OKAY;
1663   switch (r_type)
1664     {
1665     case elfcpp::R_ARM_NONE:
1666       break;
1667
1668     case elfcpp::R_ARM_ABS8:
1669       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1670                                     output_section))
1671         reloc_status = Arm_relocate_functions::abs8(view, object, psymval,
1672                                                     has_thumb_bit);
1673       break;
1674
1675     case elfcpp::R_ARM_ABS32:
1676       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
1677                                     output_section))
1678         reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
1679                                                      has_thumb_bit);
1680       break;
1681
1682     case elfcpp::R_ARM_REL32:
1683       reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
1684                                                    address, has_thumb_bit);
1685       break;
1686
1687     case elfcpp::R_ARM_THM_CALL:
1688       reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
1689                                                       address, has_thumb_bit);
1690       break;
1691
1692     case elfcpp::R_ARM_GOTOFF32:
1693       {
1694         elfcpp::Elf_types<32>::Elf_Addr got_origin;
1695         got_origin = target->got_plt_section()->address();
1696         reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
1697                                                      got_origin, has_thumb_bit);
1698       }
1699       break;
1700
1701     case elfcpp::R_ARM_BASE_PREL:
1702       {
1703         uint32_t origin;
1704         // Get the addressing origin of the output segment defining the 
1705         // symbol gsym (AAELF 4.6.1.2 Relocation types)
1706         gold_assert(gsym != NULL); 
1707         if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
1708           origin = gsym->output_segment()->vaddr();
1709         else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
1710           origin = gsym->output_data()->address();
1711         else
1712           {
1713             gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1714                                    _("cannot find origin of R_ARM_BASE_PREL"));
1715             return true;
1716           }
1717         reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
1718       }
1719       break;
1720
1721     case elfcpp::R_ARM_GOT_BREL:
1722       gold_assert(have_got_offset);
1723       reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
1724       break;
1725
1726     case elfcpp::R_ARM_GOT_PREL:
1727       gold_assert(have_got_offset);
1728       // Get the address origin for GOT PLT, which is allocated right
1729       // after the GOT section, to calculate an absolute address of
1730       // the symbol GOT entry (got_origin + got_offset).
1731       elfcpp::Elf_types<32>::Elf_Addr got_origin;
1732       got_origin = target->got_plt_section()->address();
1733       reloc_status = Arm_relocate_functions::got_prel(view,
1734                                                       got_origin + got_offset,
1735                                                       address);
1736       break;
1737
1738     case elfcpp::R_ARM_PLT32:
1739       gold_assert(gsym == NULL
1740                   || gsym->has_plt_offset()
1741                   || gsym->final_value_is_known()
1742                   || (gsym->is_defined()
1743                       && !gsym->is_from_dynobj()
1744                       && !gsym->is_preemptible()));
1745       reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
1746                                                    address, has_thumb_bit);
1747       break;
1748
1749     case elfcpp::R_ARM_CALL:
1750       reloc_status = Arm_relocate_functions::call(view, object, psymval,
1751                                                   address, has_thumb_bit);
1752       break;
1753
1754     case elfcpp::R_ARM_JUMP24:
1755       reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
1756                                                     address, has_thumb_bit);
1757       break;
1758
1759     case elfcpp::R_ARM_PREL31:
1760       reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
1761                                                     address, has_thumb_bit);
1762       break;
1763
1764     case elfcpp::R_ARM_TARGET1:
1765       // This should have been mapped to another type already.
1766       // Fall through.
1767     case elfcpp::R_ARM_COPY:
1768     case elfcpp::R_ARM_GLOB_DAT:
1769     case elfcpp::R_ARM_JUMP_SLOT:
1770     case elfcpp::R_ARM_RELATIVE:
1771       // These are relocations which should only be seen by the
1772       // dynamic linker, and should never be seen here.
1773       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1774                              _("unexpected reloc %u in object file"),
1775                              r_type);
1776       break;
1777
1778     default:
1779       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1780                              _("unsupported reloc %u"),
1781                              r_type);
1782       break;
1783     }
1784
1785   // Report any errors.
1786   switch (reloc_status)
1787     {
1788     case Arm_relocate_functions::STATUS_OKAY:
1789       break;
1790     case Arm_relocate_functions::STATUS_OVERFLOW:
1791       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1792                              _("relocation overflow in relocation %u"),
1793                              r_type);
1794       break;
1795     case Arm_relocate_functions::STATUS_BAD_RELOC:
1796       gold_error_at_location(
1797         relinfo,
1798         relnum,
1799         rel.get_r_offset(),
1800         _("unexpected opcode while processing relocation %u"),
1801         r_type);
1802       break;
1803     default:
1804       gold_unreachable();
1805     }
1806
1807   return true;
1808 }
1809
1810 // Relocate section data.
1811
1812 template<bool big_endian>
1813 void
1814 Target_arm<big_endian>::relocate_section(
1815     const Relocate_info<32, big_endian>* relinfo,
1816     unsigned int sh_type,
1817     const unsigned char* prelocs,
1818     size_t reloc_count,
1819     Output_section* output_section,
1820     bool needs_special_offset_handling,
1821     unsigned char* view,
1822     elfcpp::Elf_types<32>::Elf_Addr address,
1823     section_size_type view_size,
1824     const Reloc_symbol_changes* reloc_symbol_changes)
1825 {
1826   typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
1827   gold_assert(sh_type == elfcpp::SHT_REL);
1828
1829   gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
1830                          Arm_relocate>(
1831     relinfo,
1832     this,
1833     prelocs,
1834     reloc_count,
1835     output_section,
1836     needs_special_offset_handling,
1837     view,
1838     address,
1839     view_size,
1840     reloc_symbol_changes);
1841 }
1842
1843 // Return the size of a relocation while scanning during a relocatable
1844 // link.
1845
1846 template<bool big_endian>
1847 unsigned int
1848 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1849     unsigned int r_type,
1850     Relobj* object)
1851 {
1852   r_type = get_real_reloc_type(r_type);
1853   switch (r_type)
1854     {
1855     case elfcpp::R_ARM_NONE:
1856       return 0;
1857
1858     case elfcpp::R_ARM_ABS8:
1859       return 1;
1860
1861     case elfcpp::R_ARM_ABS32:
1862     case elfcpp::R_ARM_REL32:
1863     case elfcpp::R_ARM_THM_CALL:
1864     case elfcpp::R_ARM_GOTOFF32:
1865     case elfcpp::R_ARM_BASE_PREL:
1866     case elfcpp::R_ARM_GOT_BREL:
1867     case elfcpp::R_ARM_GOT_PREL:
1868     case elfcpp::R_ARM_PLT32:
1869     case elfcpp::R_ARM_CALL:
1870     case elfcpp::R_ARM_JUMP24:
1871     case elfcpp::R_ARM_PREL31:
1872       return 4;
1873
1874     case elfcpp::R_ARM_TARGET1:
1875       // This should have been mapped to another type already.
1876       // Fall through.
1877     case elfcpp::R_ARM_COPY:
1878     case elfcpp::R_ARM_GLOB_DAT:
1879     case elfcpp::R_ARM_JUMP_SLOT:
1880     case elfcpp::R_ARM_RELATIVE:
1881       // These are relocations which should only be seen by the
1882       // dynamic linker, and should never be seen here.
1883       gold_error(_("%s: unexpected reloc %u in object file"),
1884                  object->name().c_str(), r_type);
1885       return 0;
1886
1887     default:
1888       object->error(_("unsupported reloc %u in object file"), r_type);
1889       return 0;
1890     }
1891 }
1892
1893 // Scan the relocs during a relocatable link.
1894
1895 template<bool big_endian>
1896 void
1897 Target_arm<big_endian>::scan_relocatable_relocs(
1898     const General_options& options,
1899     Symbol_table* symtab,
1900     Layout* layout,
1901     Sized_relobj<32, big_endian>* object,
1902     unsigned int data_shndx,
1903     unsigned int sh_type,
1904     const unsigned char* prelocs,
1905     size_t reloc_count,
1906     Output_section* output_section,
1907     bool needs_special_offset_handling,
1908     size_t local_symbol_count,
1909     const unsigned char* plocal_symbols,
1910     Relocatable_relocs* rr)
1911 {
1912   gold_assert(sh_type == elfcpp::SHT_REL);
1913
1914   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
1915     Relocatable_size_for_reloc> Scan_relocatable_relocs;
1916
1917   gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
1918       Scan_relocatable_relocs>(
1919     options,
1920     symtab,
1921     layout,
1922     object,
1923     data_shndx,
1924     prelocs,
1925     reloc_count,
1926     output_section,
1927     needs_special_offset_handling,
1928     local_symbol_count,
1929     plocal_symbols,
1930     rr);
1931 }
1932
1933 // Relocate a section during a relocatable link.
1934
1935 template<bool big_endian>
1936 void
1937 Target_arm<big_endian>::relocate_for_relocatable(
1938     const Relocate_info<32, big_endian>* relinfo,
1939     unsigned int sh_type,
1940     const unsigned char* prelocs,
1941     size_t reloc_count,
1942     Output_section* output_section,
1943     off_t offset_in_output_section,
1944     const Relocatable_relocs* rr,
1945     unsigned char* view,
1946     elfcpp::Elf_types<32>::Elf_Addr view_address,
1947     section_size_type view_size,
1948     unsigned char* reloc_view,
1949     section_size_type reloc_view_size)
1950 {
1951   gold_assert(sh_type == elfcpp::SHT_REL);
1952
1953   gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
1954     relinfo,
1955     prelocs,
1956     reloc_count,
1957     output_section,
1958     offset_in_output_section,
1959     rr,
1960     view,
1961     view_address,
1962     view_size,
1963     reloc_view,
1964     reloc_view_size);
1965 }
1966
1967 // Return the value to use for a dynamic symbol which requires special
1968 // treatment.  This is how we support equality comparisons of function
1969 // pointers across shared library boundaries, as described in the
1970 // processor specific ABI supplement.
1971
1972 template<bool big_endian>
1973 uint64_t
1974 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
1975 {
1976   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1977   return this->plt_section()->address() + gsym->plt_offset();
1978 }
1979
1980 // Map platform-specific relocs to real relocs
1981 //
1982 template<bool big_endian>
1983 unsigned int
1984 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
1985 {
1986   switch (r_type)
1987     {
1988     case elfcpp::R_ARM_TARGET1:
1989       // This is either R_ARM_ABS32 or R_ARM_REL32;
1990       return elfcpp::R_ARM_ABS32;
1991
1992     case elfcpp::R_ARM_TARGET2:
1993       // This can be any reloc type but ususally is R_ARM_GOT_PREL
1994       return elfcpp::R_ARM_GOT_PREL;
1995
1996     default:
1997       return r_type;
1998     }
1999 }
2000
2001 // The selector for arm object files.
2002
2003 template<bool big_endian>
2004 class Target_selector_arm : public Target_selector
2005 {
2006  public:
2007   Target_selector_arm()
2008     : Target_selector(elfcpp::EM_ARM, 32, big_endian,
2009                       (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
2010   { }
2011
2012   Target*
2013   do_instantiate_target()
2014   { return new Target_arm<big_endian>(); }
2015 };
2016
2017 Target_selector_arm<false> target_selector_arm;
2018 Target_selector_arm<true> target_selector_armbe;
2019
2020 } // End anonymous namespace.