2010-01-07 Doug Kwan <dougkwan@google.com>
[external/binutils.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 // This file also contains borrowed and adapted code from
7 // bfd/elf32-arm.c.
8
9 // This file is part of gold.
10
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
15
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
25
26 #include "gold.h"
27
28 #include <cstring>
29 #include <limits>
30 #include <cstdio>
31 #include <string>
32 #include <algorithm>
33
34 #include "elfcpp.h"
35 #include "parameters.h"
36 #include "reloc.h"
37 #include "arm.h"
38 #include "object.h"
39 #include "symtab.h"
40 #include "layout.h"
41 #include "output.h"
42 #include "copy-relocs.h"
43 #include "target.h"
44 #include "target-reloc.h"
45 #include "target-select.h"
46 #include "tls.h"
47 #include "defstd.h"
48 #include "gc.h"
49 #include "attributes.h"
50
51 namespace
52 {
53
54 using namespace gold;
55
56 template<bool big_endian>
57 class Output_data_plt_arm;
58
59 template<bool big_endian>
60 class Stub_table;
61
62 template<bool big_endian>
63 class Arm_input_section;
64
65 template<bool big_endian>
66 class Arm_output_section;
67
68 template<bool big_endian>
69 class Arm_relobj;
70
71 template<bool big_endian>
72 class Target_arm;
73
74 // For convenience.
75 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
76
77 // Maximum branch offsets for ARM, THUMB and THUMB2.
78 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
79 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
80 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
81 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
82 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
83 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
84
85 // The arm target class.
86 //
87 // This is a very simple port of gold for ARM-EABI.  It is intended for
88 // supporting Android only for the time being.  Only these relocation types
89 // are supported.
90 //
91 // R_ARM_NONE
92 // R_ARM_ABS32
93 // R_ARM_ABS32_NOI
94 // R_ARM_ABS16
95 // R_ARM_ABS12
96 // R_ARM_ABS8
97 // R_ARM_THM_ABS5
98 // R_ARM_BASE_ABS
99 // R_ARM_REL32
100 // R_ARM_THM_CALL
101 // R_ARM_COPY
102 // R_ARM_GLOB_DAT
103 // R_ARM_BASE_PREL
104 // R_ARM_JUMP_SLOT
105 // R_ARM_RELATIVE
106 // R_ARM_GOTOFF32
107 // R_ARM_GOT_BREL
108 // R_ARM_GOT_PREL
109 // R_ARM_PLT32
110 // R_ARM_CALL
111 // R_ARM_JUMP24
112 // R_ARM_TARGET1
113 // R_ARM_PREL31
114 // R_ARM_ABS8
115 // R_ARM_MOVW_ABS_NC
116 // R_ARM_MOVT_ABS
117 // R_ARM_THM_MOVW_ABS_NC
118 // R_ARM_THM_MOVT_ABS
119 // R_ARM_MOVW_PREL_NC
120 // R_ARM_MOVT_PREL
121 // R_ARM_THM_MOVW_PREL_NC
122 // R_ARM_THM_MOVT_PREL
123 // 
124 // TODOs:
125 // - Support more relocation types as needed. 
126 // - Make PLTs more flexible for different architecture features like
127 //   Thumb-2 and BE8.
128 // There are probably a lot more.
129
130 // Instruction template class.  This class is similar to the insn_sequence
131 // struct in bfd/elf32-arm.c.
132
133 class Insn_template
134 {
135  public:
136   // Types of instruction templates.
137   enum Type
138     {
139       THUMB16_TYPE = 1,
140       // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction 
141       // templates with class-specific semantics.  Currently this is used
142       // only by the Cortex_a8_stub class for handling condition codes in
143       // conditional branches.
144       THUMB16_SPECIAL_TYPE,
145       THUMB32_TYPE,
146       ARM_TYPE,
147       DATA_TYPE
148     };
149
150   // Factory methods to create instruction templates in different formats.
151
152   static const Insn_template
153   thumb16_insn(uint32_t data)
154   { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); } 
155
156   // A Thumb conditional branch, in which the proper condition is inserted
157   // when we build the stub.
158   static const Insn_template
159   thumb16_bcond_insn(uint32_t data)
160   { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); } 
161
162   static const Insn_template
163   thumb32_insn(uint32_t data)
164   { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); } 
165
166   static const Insn_template
167   thumb32_b_insn(uint32_t data, int reloc_addend)
168   {
169     return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
170                          reloc_addend);
171   } 
172
173   static const Insn_template
174   arm_insn(uint32_t data)
175   { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
176
177   static const Insn_template
178   arm_rel_insn(unsigned data, int reloc_addend)
179   { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
180
181   static const Insn_template
182   data_word(unsigned data, unsigned int r_type, int reloc_addend)
183   { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); } 
184
185   // Accessors.  This class is used for read-only objects so no modifiers
186   // are provided.
187
188   uint32_t
189   data() const
190   { return this->data_; }
191
192   // Return the instruction sequence type of this.
193   Type
194   type() const
195   { return this->type_; }
196
197   // Return the ARM relocation type of this.
198   unsigned int
199   r_type() const
200   { return this->r_type_; }
201
202   int32_t
203   reloc_addend() const
204   { return this->reloc_addend_; }
205
206   // Return size of instruction template in bytes.
207   size_t
208   size() const;
209
210   // Return byte-alignment of instruction template.
211   unsigned
212   alignment() const;
213
214  private:
215   // We make the constructor private to ensure that only the factory
216   // methods are used.
217   inline
218   Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
219     : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
220   { }
221
222   // Instruction specific data.  This is used to store information like
223   // some of the instruction bits.
224   uint32_t data_;
225   // Instruction template type.
226   Type type_;
227   // Relocation type if there is a relocation or R_ARM_NONE otherwise.
228   unsigned int r_type_;
229   // Relocation addend.
230   int32_t reloc_addend_;
231 };
232
233 // Macro for generating code to stub types. One entry per long/short
234 // branch stub
235
236 #define DEF_STUBS \
237   DEF_STUB(long_branch_any_any) \
238   DEF_STUB(long_branch_v4t_arm_thumb) \
239   DEF_STUB(long_branch_thumb_only) \
240   DEF_STUB(long_branch_v4t_thumb_thumb) \
241   DEF_STUB(long_branch_v4t_thumb_arm) \
242   DEF_STUB(short_branch_v4t_thumb_arm) \
243   DEF_STUB(long_branch_any_arm_pic) \
244   DEF_STUB(long_branch_any_thumb_pic) \
245   DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
246   DEF_STUB(long_branch_v4t_arm_thumb_pic) \
247   DEF_STUB(long_branch_v4t_thumb_arm_pic) \
248   DEF_STUB(long_branch_thumb_only_pic) \
249   DEF_STUB(a8_veneer_b_cond) \
250   DEF_STUB(a8_veneer_b) \
251   DEF_STUB(a8_veneer_bl) \
252   DEF_STUB(a8_veneer_blx)
253
254 // Stub types.
255
256 #define DEF_STUB(x) arm_stub_##x,
257 typedef enum
258   {
259     arm_stub_none,
260     DEF_STUBS
261
262     // First reloc stub type.
263     arm_stub_reloc_first = arm_stub_long_branch_any_any,
264     // Last  reloc stub type.
265     arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
266
267     // First Cortex-A8 stub type.
268     arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
269     // Last Cortex-A8 stub type.
270     arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
271     
272     // Last stub type.
273     arm_stub_type_last = arm_stub_a8_veneer_blx
274   } Stub_type;
275 #undef DEF_STUB
276
277 // Stub template class.  Templates are meant to be read-only objects.
278 // A stub template for a stub type contains all read-only attributes
279 // common to all stubs of the same type.
280
281 class Stub_template
282 {
283  public:
284   Stub_template(Stub_type, const Insn_template*, size_t);
285
286   ~Stub_template()
287   { }
288
289   // Return stub type.
290   Stub_type
291   type() const
292   { return this->type_; }
293
294   // Return an array of instruction templates.
295   const Insn_template*
296   insns() const
297   { return this->insns_; }
298
299   // Return size of template in number of instructions.
300   size_t
301   insn_count() const
302   { return this->insn_count_; }
303
304   // Return size of template in bytes.
305   size_t
306   size() const
307   { return this->size_; }
308
309   // Return alignment of the stub template.
310   unsigned
311   alignment() const
312   { return this->alignment_; }
313   
314   // Return whether entry point is in thumb mode.
315   bool
316   entry_in_thumb_mode() const
317   { return this->entry_in_thumb_mode_; }
318
319   // Return number of relocations in this template.
320   size_t
321   reloc_count() const
322   { return this->relocs_.size(); }
323
324   // Return index of the I-th instruction with relocation.
325   size_t
326   reloc_insn_index(size_t i) const
327   {
328     gold_assert(i < this->relocs_.size());
329     return this->relocs_[i].first;
330   }
331
332   // Return the offset of the I-th instruction with relocation from the
333   // beginning of the stub.
334   section_size_type
335   reloc_offset(size_t i) const
336   {
337     gold_assert(i < this->relocs_.size());
338     return this->relocs_[i].second;
339   }
340
341  private:
342   // This contains information about an instruction template with a relocation
343   // and its offset from start of stub.
344   typedef std::pair<size_t, section_size_type> Reloc;
345
346   // A Stub_template may not be copied.  We want to share templates as much
347   // as possible.
348   Stub_template(const Stub_template&);
349   Stub_template& operator=(const Stub_template&);
350   
351   // Stub type.
352   Stub_type type_;
353   // Points to an array of Insn_templates.
354   const Insn_template* insns_;
355   // Number of Insn_templates in insns_[].
356   size_t insn_count_;
357   // Size of templated instructions in bytes.
358   size_t size_;
359   // Alignment of templated instructions.
360   unsigned alignment_;
361   // Flag to indicate if entry is in thumb mode.
362   bool entry_in_thumb_mode_;
363   // A table of reloc instruction indices and offsets.  We can find these by
364   // looking at the instruction templates but we pre-compute and then stash
365   // them here for speed. 
366   std::vector<Reloc> relocs_;
367 };
368
369 //
370 // A class for code stubs.  This is a base class for different type of
371 // stubs used in the ARM target.
372 //
373
374 class Stub
375 {
376  private:
377   static const section_offset_type invalid_offset =
378     static_cast<section_offset_type>(-1);
379
380  public:
381   Stub(const Stub_template* stub_template)
382     : stub_template_(stub_template), offset_(invalid_offset)
383   { }
384
385   virtual
386    ~Stub()
387   { }
388
389   // Return the stub template.
390   const Stub_template*
391   stub_template() const
392   { return this->stub_template_; }
393
394   // Return offset of code stub from beginning of its containing stub table.
395   section_offset_type
396   offset() const
397   {
398     gold_assert(this->offset_ != invalid_offset);
399     return this->offset_;
400   }
401
402   // Set offset of code stub from beginning of its containing stub table.
403   void
404   set_offset(section_offset_type offset)
405   { this->offset_ = offset; }
406   
407   // Return the relocation target address of the i-th relocation in the
408   // stub.  This must be defined in a child class.
409   Arm_address
410   reloc_target(size_t i)
411   { return this->do_reloc_target(i); }
412
413   // Write a stub at output VIEW.  BIG_ENDIAN select how a stub is written.
414   void
415   write(unsigned char* view, section_size_type view_size, bool big_endian)
416   { this->do_write(view, view_size, big_endian); }
417
418   // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
419   // for the i-th instruction.
420   uint16_t
421   thumb16_special(size_t i)
422   { return this->do_thumb16_special(i); }
423
424  protected:
425   // This must be defined in the child class.
426   virtual Arm_address
427   do_reloc_target(size_t) = 0;
428
429   // This may be overridden in the child class.
430   virtual void
431   do_write(unsigned char* view, section_size_type view_size, bool big_endian)
432   {
433     if (big_endian)
434       this->do_fixed_endian_write<true>(view, view_size);
435     else
436       this->do_fixed_endian_write<false>(view, view_size);
437   }
438   
439   // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
440   // instruction template.
441   virtual uint16_t
442   do_thumb16_special(size_t)
443   { gold_unreachable(); }
444
445  private:
446   // A template to implement do_write.
447   template<bool big_endian>
448   void inline
449   do_fixed_endian_write(unsigned char*, section_size_type);
450
451   // Its template.
452   const Stub_template* stub_template_;
453   // Offset within the section of containing this stub.
454   section_offset_type offset_;
455 };
456
457 // Reloc stub class.  These are stubs we use to fix up relocation because
458 // of limited branch ranges.
459
460 class Reloc_stub : public Stub
461 {
462  public:
463   static const unsigned int invalid_index = static_cast<unsigned int>(-1);
464   // We assume we never jump to this address.
465   static const Arm_address invalid_address = static_cast<Arm_address>(-1);
466
467   // Return destination address.
468   Arm_address
469   destination_address() const
470   {
471     gold_assert(this->destination_address_ != this->invalid_address);
472     return this->destination_address_;
473   }
474
475   // Set destination address.
476   void
477   set_destination_address(Arm_address address)
478   {
479     gold_assert(address != this->invalid_address);
480     this->destination_address_ = address;
481   }
482
483   // Reset destination address.
484   void
485   reset_destination_address()
486   { this->destination_address_ = this->invalid_address; }
487
488   // Determine stub type for a branch of a relocation of R_TYPE going
489   // from BRANCH_ADDRESS to BRANCH_TARGET.  If TARGET_IS_THUMB is set,
490   // the branch target is a thumb instruction.  TARGET is used for look
491   // up ARM-specific linker settings.
492   static Stub_type
493   stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
494                       Arm_address branch_target, bool target_is_thumb);
495
496   // Reloc_stub key.  A key is logically a triplet of a stub type, a symbol
497   // and an addend.  Since we treat global and local symbol differently, we
498   // use a Symbol object for a global symbol and a object-index pair for
499   // a local symbol.
500   class Key
501   {
502    public:
503     // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
504     // R_SYM.  Otherwise, this is a local symbol and RELOBJ must non-NULL
505     // and R_SYM must not be invalid_index.
506     Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
507         unsigned int r_sym, int32_t addend)
508       : stub_type_(stub_type), addend_(addend)
509     {
510       if (symbol != NULL)
511         {
512           this->r_sym_ = Reloc_stub::invalid_index;
513           this->u_.symbol = symbol;
514         }
515       else
516         {
517           gold_assert(relobj != NULL && r_sym != invalid_index);
518           this->r_sym_ = r_sym;
519           this->u_.relobj = relobj;
520         }
521     }
522
523     ~Key()
524     { }
525
526     // Accessors: Keys are meant to be read-only object so no modifiers are
527     // provided.
528
529     // Return stub type.
530     Stub_type
531     stub_type() const
532     { return this->stub_type_; }
533
534     // Return the local symbol index or invalid_index.
535     unsigned int
536     r_sym() const
537     { return this->r_sym_; }
538
539     // Return the symbol if there is one.
540     const Symbol*
541     symbol() const
542     { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
543
544     // Return the relobj if there is one.
545     const Relobj*
546     relobj() const
547     { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
548
549     // Whether this equals to another key k.
550     bool
551     eq(const Key& k) const 
552     {
553       return ((this->stub_type_ == k.stub_type_)
554               && (this->r_sym_ == k.r_sym_)
555               && ((this->r_sym_ != Reloc_stub::invalid_index)
556                   ? (this->u_.relobj == k.u_.relobj)
557                   : (this->u_.symbol == k.u_.symbol))
558               && (this->addend_ == k.addend_));
559     }
560
561     // Return a hash value.
562     size_t
563     hash_value() const
564     {
565       return (this->stub_type_
566               ^ this->r_sym_
567               ^ gold::string_hash<char>(
568                     (this->r_sym_ != Reloc_stub::invalid_index)
569                     ? this->u_.relobj->name().c_str()
570                     : this->u_.symbol->name())
571               ^ this->addend_);
572     }
573
574     // Functors for STL associative containers.
575     struct hash
576     {
577       size_t
578       operator()(const Key& k) const
579       { return k.hash_value(); }
580     };
581
582     struct equal_to
583     {
584       bool
585       operator()(const Key& k1, const Key& k2) const
586       { return k1.eq(k2); }
587     };
588
589     // Name of key.  This is mainly for debugging.
590     std::string
591     name() const;
592
593    private:
594     // Stub type.
595     Stub_type stub_type_;
596     // If this is a local symbol, this is the index in the defining object.
597     // Otherwise, it is invalid_index for a global symbol.
598     unsigned int r_sym_;
599     // If r_sym_ is invalid index.  This points to a global symbol.
600     // Otherwise, this points a relobj.  We used the unsized and target
601     // independent Symbol and Relobj classes instead of Sized_symbol<32> and  
602     // Arm_relobj.  This is done to avoid making the stub class a template
603     // as most of the stub machinery is endianity-neutral.  However, it
604     // may require a bit of casting done by users of this class.
605     union
606     {
607       const Symbol* symbol;
608       const Relobj* relobj;
609     } u_;
610     // Addend associated with a reloc.
611     int32_t addend_;
612   };
613
614  protected:
615   // Reloc_stubs are created via a stub factory.  So these are protected.
616   Reloc_stub(const Stub_template* stub_template)
617     : Stub(stub_template), destination_address_(invalid_address)
618   { }
619
620   ~Reloc_stub()
621   { }
622
623   friend class Stub_factory;
624
625   // Return the relocation target address of the i-th relocation in the
626   // stub.
627   Arm_address
628   do_reloc_target(size_t i)
629   {
630     // All reloc stub have only one relocation.
631     gold_assert(i == 0);
632     return this->destination_address_;
633   }
634
635  private:
636   // Address of destination.
637   Arm_address destination_address_;
638 };
639
640 // Cortex-A8 stub class.  We need a Cortex-A8 stub to redirect any 32-bit
641 // THUMB branch that meets the following conditions:
642 // 
643 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
644 //    branch address is 0xffe.
645 // 2. The branch target address is in the same page as the first word of the
646 //    branch.
647 // 3. The branch follows a 32-bit instruction which is not a branch.
648 //
649 // To do the fix up, we need to store the address of the branch instruction
650 // and its target at least.  We also need to store the original branch
651 // instruction bits for the condition code in a conditional branch.  The
652 // condition code is used in a special instruction template.  We also want
653 // to identify input sections needing Cortex-A8 workaround quickly.  We store
654 // extra information about object and section index of the code section
655 // containing a branch being fixed up.  The information is used to mark
656 // the code section when we finalize the Cortex-A8 stubs.
657 //
658
659 class Cortex_a8_stub : public Stub
660 {
661  public:
662   ~Cortex_a8_stub()
663   { }
664
665   // Return the object of the code section containing the branch being fixed
666   // up.
667   Relobj*
668   relobj() const
669   { return this->relobj_; }
670
671   // Return the section index of the code section containing the branch being
672   // fixed up.
673   unsigned int
674   shndx() const
675   { return this->shndx_; }
676
677   // Return the source address of stub.  This is the address of the original
678   // branch instruction.  LSB is 1 always set to indicate that it is a THUMB
679   // instruction.
680   Arm_address
681   source_address() const
682   { return this->source_address_; }
683
684   // Return the destination address of the stub.  This is the branch taken
685   // address of the original branch instruction.  LSB is 1 if it is a THUMB
686   // instruction address.
687   Arm_address
688   destination_address() const
689   { return this->destination_address_; }
690
691   // Return the instruction being fixed up.
692   uint32_t
693   original_insn() const
694   { return this->original_insn_; }
695
696  protected:
697   // Cortex_a8_stubs are created via a stub factory.  So these are protected.
698   Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
699                  unsigned int shndx, Arm_address source_address,
700                  Arm_address destination_address, uint32_t original_insn)
701     : Stub(stub_template), relobj_(relobj), shndx_(shndx),
702       source_address_(source_address | 1U),
703       destination_address_(destination_address),
704       original_insn_(original_insn)
705   { }
706
707   friend class Stub_factory;
708
709   // Return the relocation target address of the i-th relocation in the
710   // stub.
711   Arm_address
712   do_reloc_target(size_t i)
713   {
714     if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
715       {
716         // The conditional branch veneer has two relocations.
717         gold_assert(i < 2);
718         return i == 0 ? this->source_address_ + 4 : this->destination_address_;
719       }
720     else
721       {
722         // All other Cortex-A8 stubs have only one relocation.
723         gold_assert(i == 0);
724         return this->destination_address_;
725       }
726   }
727
728   // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
729   uint16_t
730   do_thumb16_special(size_t);
731
732  private:
733   // Object of the code section containing the branch being fixed up.
734   Relobj* relobj_;
735   // Section index of the code section containing the branch begin fixed up.
736   unsigned int shndx_;
737   // Source address of original branch.
738   Arm_address source_address_;
739   // Destination address of the original branch.
740   Arm_address destination_address_;
741   // Original branch instruction.  This is needed for copying the condition
742   // code from a condition branch to its stub.
743   uint32_t original_insn_;
744 };
745
746 // Stub factory class.
747
748 class Stub_factory
749 {
750  public:
751   // Return the unique instance of this class.
752   static const Stub_factory&
753   get_instance()
754   {
755     static Stub_factory singleton;
756     return singleton;
757   }
758
759   // Make a relocation stub.
760   Reloc_stub*
761   make_reloc_stub(Stub_type stub_type) const
762   {
763     gold_assert(stub_type >= arm_stub_reloc_first
764                 && stub_type <= arm_stub_reloc_last);
765     return new Reloc_stub(this->stub_templates_[stub_type]);
766   }
767
768   // Make a Cortex-A8 stub.
769   Cortex_a8_stub*
770   make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
771                       Arm_address source, Arm_address destination,
772                       uint32_t original_insn) const
773   {
774     gold_assert(stub_type >= arm_stub_cortex_a8_first
775                 && stub_type <= arm_stub_cortex_a8_last);
776     return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
777                               source, destination, original_insn);
778   }
779
780  private:
781   // Constructor and destructor are protected since we only return a single
782   // instance created in Stub_factory::get_instance().
783   
784   Stub_factory();
785
786   // A Stub_factory may not be copied since it is a singleton.
787   Stub_factory(const Stub_factory&);
788   Stub_factory& operator=(Stub_factory&);
789   
790   // Stub templates.  These are initialized in the constructor.
791   const Stub_template* stub_templates_[arm_stub_type_last+1];
792 };
793
794 // A class to hold stubs for the ARM target.
795
796 template<bool big_endian>
797 class Stub_table : public Output_data
798 {
799  public:
800   Stub_table(Arm_input_section<big_endian>* owner)
801     : Output_data(), addralign_(1), owner_(owner), has_been_changed_(false),
802       reloc_stubs_()
803   { }
804
805   ~Stub_table()
806   { }
807
808   // Owner of this stub table.
809   Arm_input_section<big_endian>*
810   owner() const
811   { return this->owner_; }
812
813   // Whether this stub table is empty.
814   bool
815   empty() const
816   { return this->reloc_stubs_.empty(); }
817
818   // Whether this has been changed.
819   bool
820   has_been_changed() const
821   { return this->has_been_changed_; }
822
823   // Set the has-been-changed flag.
824   void
825   set_has_been_changed(bool value)
826   { this->has_been_changed_ = value; }
827
828   // Return the current data size.
829   off_t
830   current_data_size() const
831   { return this->current_data_size_for_child(); }
832
833   // Add a STUB with using KEY.  Caller is reponsible for avoid adding
834   // if already a STUB with the same key has been added. 
835   void
836   add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key);
837
838   // Look up a relocation stub using KEY.  Return NULL if there is none.
839   Reloc_stub*
840   find_reloc_stub(const Reloc_stub::Key& key) const
841   {
842     typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
843     return (p != this->reloc_stubs_.end()) ? p->second : NULL;
844   }
845
846   // Relocate stubs in this stub table.
847   void
848   relocate_stubs(const Relocate_info<32, big_endian>*,
849                  Target_arm<big_endian>*, Output_section*,
850                  unsigned char*, Arm_address, section_size_type);
851
852  protected:
853   // Write out section contents.
854   void
855   do_write(Output_file*);
856  
857   // Return the required alignment.
858   uint64_t
859   do_addralign() const
860   { return this->addralign_; }
861
862   // Finalize data size.
863   void
864   set_final_data_size()
865   { this->set_data_size(this->current_data_size_for_child()); }
866
867   // Reset address and file offset.
868   void
869   do_reset_address_and_file_offset();
870
871  private:
872   // Unordered map of stubs.
873   typedef
874     Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
875                   Reloc_stub::Key::equal_to>
876     Reloc_stub_map;
877
878   // Address alignment
879   uint64_t addralign_;
880   // Owner of this stub table.
881   Arm_input_section<big_endian>* owner_;
882   // This is set to true during relaxiong if the size of the stub table
883   // has been changed.
884   bool has_been_changed_;
885   // The relocation stubs.
886   Reloc_stub_map reloc_stubs_;
887 };
888
889 // A class to wrap an ordinary input section containing executable code.
890
891 template<bool big_endian>
892 class Arm_input_section : public Output_relaxed_input_section
893 {
894  public:
895   Arm_input_section(Relobj* relobj, unsigned int shndx)
896     : Output_relaxed_input_section(relobj, shndx, 1),
897       original_addralign_(1), original_size_(0), stub_table_(NULL)
898   { }
899
900   ~Arm_input_section()
901   { }
902
903   // Initialize.
904   void
905   init();
906   
907   // Whether this is a stub table owner.
908   bool
909   is_stub_table_owner() const
910   { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
911
912   // Return the stub table.
913   Stub_table<big_endian>*
914   stub_table() const
915   { return this->stub_table_; }
916
917   // Set the stub_table.
918   void
919   set_stub_table(Stub_table<big_endian>* stub_table)
920   { this->stub_table_ = stub_table; }
921
922   // Downcast a base pointer to an Arm_input_section pointer.  This is
923   // not type-safe but we only use Arm_input_section not the base class.
924   static Arm_input_section<big_endian>*
925   as_arm_input_section(Output_relaxed_input_section* poris)
926   { return static_cast<Arm_input_section<big_endian>*>(poris); }
927
928  protected:
929   // Write data to output file.
930   void
931   do_write(Output_file*);
932
933   // Return required alignment of this.
934   uint64_t
935   do_addralign() const
936   {
937     if (this->is_stub_table_owner())
938       return std::max(this->stub_table_->addralign(),
939                       this->original_addralign_);
940     else
941       return this->original_addralign_;
942   }
943
944   // Finalize data size.
945   void
946   set_final_data_size();
947
948   // Reset address and file offset.
949   void
950   do_reset_address_and_file_offset();
951
952   // Output offset.
953   bool
954   do_output_offset(const Relobj* object, unsigned int shndx,
955                    section_offset_type offset,
956                    section_offset_type* poutput) const
957   {
958     if ((object == this->relobj())
959         && (shndx == this->shndx())
960         && (offset >= 0)
961         && (convert_types<uint64_t, section_offset_type>(offset)
962             <= this->original_size_))
963       {
964         *poutput = offset;
965         return true;
966       }
967     else
968       return false;
969   }
970
971  private:
972   // Copying is not allowed.
973   Arm_input_section(const Arm_input_section&);
974   Arm_input_section& operator=(const Arm_input_section&);
975
976   // Address alignment of the original input section.
977   uint64_t original_addralign_;
978   // Section size of the original input section.
979   uint64_t original_size_;
980   // Stub table.
981   Stub_table<big_endian>* stub_table_;
982 };
983
984 // Arm output section class.  This is defined mainly to add a number of
985 // stub generation methods.
986
987 template<bool big_endian>
988 class Arm_output_section : public Output_section
989 {
990  public:
991   Arm_output_section(const char* name, elfcpp::Elf_Word type,
992                      elfcpp::Elf_Xword flags)
993     : Output_section(name, type, flags)
994   { }
995
996   ~Arm_output_section()
997   { }
998   
999   // Group input sections for stub generation.
1000   void
1001   group_sections(section_size_type, bool, Target_arm<big_endian>*);
1002
1003   // Downcast a base pointer to an Arm_output_section pointer.  This is
1004   // not type-safe but we only use Arm_output_section not the base class.
1005   static Arm_output_section<big_endian>*
1006   as_arm_output_section(Output_section* os)
1007   { return static_cast<Arm_output_section<big_endian>*>(os); }
1008
1009  private:
1010   // For convenience.
1011   typedef Output_section::Input_section Input_section;
1012   typedef Output_section::Input_section_list Input_section_list;
1013
1014   // Create a stub group.
1015   void create_stub_group(Input_section_list::const_iterator,
1016                          Input_section_list::const_iterator,
1017                          Input_section_list::const_iterator,
1018                          Target_arm<big_endian>*,
1019                          std::vector<Output_relaxed_input_section*>*);
1020 };
1021
1022 // Arm_relobj class.
1023
1024 template<bool big_endian>
1025 class Arm_relobj : public Sized_relobj<32, big_endian>
1026 {
1027  public:
1028   static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1029
1030   Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1031              const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1032     : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1033       stub_tables_(), local_symbol_is_thumb_function_(),
1034       attributes_section_data_(NULL)
1035   { }
1036
1037   ~Arm_relobj()
1038   { delete this->attributes_section_data_; }
1039  
1040   // Return the stub table of the SHNDX-th section if there is one.
1041   Stub_table<big_endian>*
1042   stub_table(unsigned int shndx) const
1043   {
1044     gold_assert(shndx < this->stub_tables_.size());
1045     return this->stub_tables_[shndx];
1046   }
1047
1048   // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1049   void
1050   set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1051   {
1052     gold_assert(shndx < this->stub_tables_.size());
1053     this->stub_tables_[shndx] = stub_table;
1054   }
1055
1056   // Whether a local symbol is a THUMB function.  R_SYM is the symbol table
1057   // index.  This is only valid after do_count_local_symbol is called.
1058   bool
1059   local_symbol_is_thumb_function(unsigned int r_sym) const
1060   {
1061     gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1062     return this->local_symbol_is_thumb_function_[r_sym];
1063   }
1064   
1065   // Scan all relocation sections for stub generation.
1066   void
1067   scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1068                           const Layout*);
1069
1070   // Convert regular input section with index SHNDX to a relaxed section.
1071   void
1072   convert_input_section_to_relaxed_section(unsigned shndx)
1073   {
1074     // The stubs have relocations and we need to process them after writing
1075     // out the stubs.  So relocation now must follow section write.
1076     this->invalidate_section_offset(shndx);
1077     this->set_relocs_must_follow_section_writes();
1078   }
1079
1080   // Downcast a base pointer to an Arm_relobj pointer.  This is
1081   // not type-safe but we only use Arm_relobj not the base class.
1082   static Arm_relobj<big_endian>*
1083   as_arm_relobj(Relobj* relobj)
1084   { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1085
1086   // Processor-specific flags in ELF file header.  This is valid only after
1087   // reading symbols.
1088   elfcpp::Elf_Word
1089   processor_specific_flags() const
1090   { return this->processor_specific_flags_; }
1091
1092   // Attribute section data  This is the contents of the .ARM.attribute section
1093   // if there is one.
1094   const Attributes_section_data*
1095   attributes_section_data() const
1096   { return this->attributes_section_data_; }
1097
1098  protected:
1099   // Post constructor setup.
1100   void
1101   do_setup()
1102   {
1103     // Call parent's setup method.
1104     Sized_relobj<32, big_endian>::do_setup();
1105
1106     // Initialize look-up tables.
1107     Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1108     this->stub_tables_.swap(empty_stub_table_list);
1109   }
1110
1111   // Count the local symbols.
1112   void
1113   do_count_local_symbols(Stringpool_template<char>*,
1114                          Stringpool_template<char>*);
1115
1116   void
1117   do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1118                        const unsigned char* pshdrs,
1119                        typename Sized_relobj<32, big_endian>::Views* pivews);
1120
1121   // Read the symbol information.
1122   void
1123   do_read_symbols(Read_symbols_data* sd);
1124
1125  private:
1126   // List of stub tables.
1127   typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1128   Stub_table_list stub_tables_;
1129   // Bit vector to tell if a local symbol is a thumb function or not.
1130   // This is only valid after do_count_local_symbol is called.
1131   std::vector<bool> local_symbol_is_thumb_function_;
1132   // processor-specific flags in ELF file header.
1133   elfcpp::Elf_Word processor_specific_flags_;
1134   // Object attributes if there is an .ARM.attributes section or NULL.
1135   Attributes_section_data* attributes_section_data_;
1136 };
1137
1138 // Arm_dynobj class.
1139
1140 template<bool big_endian>
1141 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1142 {
1143  public:
1144   Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1145              const elfcpp::Ehdr<32, big_endian>& ehdr)
1146     : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1147       processor_specific_flags_(0), attributes_section_data_(NULL)
1148   { }
1149  
1150   ~Arm_dynobj()
1151   { delete this->attributes_section_data_; }
1152
1153   // Downcast a base pointer to an Arm_relobj pointer.  This is
1154   // not type-safe but we only use Arm_relobj not the base class.
1155   static Arm_dynobj<big_endian>*
1156   as_arm_dynobj(Dynobj* dynobj)
1157   { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1158
1159   // Processor-specific flags in ELF file header.  This is valid only after
1160   // reading symbols.
1161   elfcpp::Elf_Word
1162   processor_specific_flags() const
1163   { return this->processor_specific_flags_; }
1164
1165   // Attributes section data.
1166   const Attributes_section_data*
1167   attributes_section_data() const
1168   { return this->attributes_section_data_; }
1169
1170  protected:
1171   // Read the symbol information.
1172   void
1173   do_read_symbols(Read_symbols_data* sd);
1174
1175  private:
1176   // processor-specific flags in ELF file header.
1177   elfcpp::Elf_Word processor_specific_flags_;
1178   // Object attributes if there is an .ARM.attributes section or NULL.
1179   Attributes_section_data* attributes_section_data_;
1180 };
1181
1182 // Functor to read reloc addends during stub generation.
1183
1184 template<int sh_type, bool big_endian>
1185 struct Stub_addend_reader
1186 {
1187   // Return the addend for a relocation of a particular type.  Depending
1188   // on whether this is a REL or RELA relocation, read the addend from a
1189   // view or from a Reloc object.
1190   elfcpp::Elf_types<32>::Elf_Swxword
1191   operator()(
1192     unsigned int /* r_type */,
1193     const unsigned char* /* view */,
1194     const typename Reloc_types<sh_type,
1195                                32, big_endian>::Reloc& /* reloc */) const;
1196 };
1197
1198 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1199
1200 template<bool big_endian>
1201 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1202 {
1203   elfcpp::Elf_types<32>::Elf_Swxword
1204   operator()(
1205     unsigned int,
1206     const unsigned char*,
1207     const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1208 };
1209
1210 // Specialized Stub_addend_reader for RELA type relocation sections.
1211 // We currently do not handle RELA type relocation sections but it is trivial
1212 // to implement the addend reader.  This is provided for completeness and to
1213 // make it easier to add support for RELA relocation sections in the future.
1214
1215 template<bool big_endian>
1216 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1217 {
1218   elfcpp::Elf_types<32>::Elf_Swxword
1219   operator()(
1220     unsigned int,
1221     const unsigned char*,
1222     const typename Reloc_types<elfcpp::SHT_RELA, 32,
1223                                big_endian>::Reloc& reloc) const
1224   { return reloc.get_r_addend(); }
1225 };
1226
1227 // Utilities for manipulating integers of up to 32-bits
1228
1229 namespace utils
1230 {
1231   // Sign extend an n-bit unsigned integer stored in an uint32_t into
1232   // an int32_t.  NO_BITS must be between 1 to 32.
1233   template<int no_bits>
1234   static inline int32_t
1235   sign_extend(uint32_t bits)
1236   {
1237     gold_assert(no_bits >= 0 && no_bits <= 32);
1238     if (no_bits == 32)
1239       return static_cast<int32_t>(bits);
1240     uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1241     bits &= mask;
1242     uint32_t top_bit = 1U << (no_bits - 1);
1243     int32_t as_signed = static_cast<int32_t>(bits);
1244     return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1245   }
1246
1247   // Detects overflow of an NO_BITS integer stored in a uint32_t.
1248   template<int no_bits>
1249   static inline bool
1250   has_overflow(uint32_t bits)
1251   {
1252     gold_assert(no_bits >= 0 && no_bits <= 32);
1253     if (no_bits == 32)
1254       return false;
1255     int32_t max = (1 << (no_bits - 1)) - 1;
1256     int32_t min = -(1 << (no_bits - 1));
1257     int32_t as_signed = static_cast<int32_t>(bits);
1258     return as_signed > max || as_signed < min;
1259   }
1260
1261   // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1262   // fits in the given number of bits as either a signed or unsigned value.
1263   // For example, has_signed_unsigned_overflow<8> would check
1264   // -128 <= bits <= 255
1265   template<int no_bits>
1266   static inline bool
1267   has_signed_unsigned_overflow(uint32_t bits)
1268   {
1269     gold_assert(no_bits >= 2 && no_bits <= 32);
1270     if (no_bits == 32)
1271       return false;
1272     int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1273     int32_t min = -(1 << (no_bits - 1));
1274     int32_t as_signed = static_cast<int32_t>(bits);
1275     return as_signed > max || as_signed < min;
1276   }
1277
1278   // Select bits from A and B using bits in MASK.  For each n in [0..31],
1279   // the n-th bit in the result is chosen from the n-th bits of A and B.
1280   // A zero selects A and a one selects B.
1281   static inline uint32_t
1282   bit_select(uint32_t a, uint32_t b, uint32_t mask)
1283   { return (a & ~mask) | (b & mask); }
1284 };
1285
1286 template<bool big_endian>
1287 class Target_arm : public Sized_target<32, big_endian>
1288 {
1289  public:
1290   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1291     Reloc_section;
1292
1293   // When were are relocating a stub, we pass this as the relocation number.
1294   static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
1295
1296   Target_arm()
1297     : Sized_target<32, big_endian>(&arm_info),
1298       got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1299       copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
1300       stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
1301       should_force_pic_veneer_(false), arm_input_section_map_(),
1302       attributes_section_data_(NULL)
1303   { }
1304
1305   // Whether we can use BLX.
1306   bool
1307   may_use_blx() const
1308   { return this->may_use_blx_; }
1309
1310   // Set use-BLX flag.
1311   void
1312   set_may_use_blx(bool value)
1313   { this->may_use_blx_ = value; }
1314   
1315   // Whether we force PCI branch veneers.
1316   bool
1317   should_force_pic_veneer() const
1318   { return this->should_force_pic_veneer_; }
1319
1320   // Set PIC veneer flag.
1321   void
1322   set_should_force_pic_veneer(bool value)
1323   { this->should_force_pic_veneer_ = value; }
1324   
1325   // Whether we use THUMB-2 instructions.
1326   bool
1327   using_thumb2() const
1328   {
1329     Object_attribute* attr =
1330       this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1331     int arch = attr->int_value();
1332     return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
1333   }
1334
1335   // Whether we use THUMB/THUMB-2 instructions only.
1336   bool
1337   using_thumb_only() const
1338   {
1339     Object_attribute* attr =
1340       this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1341     if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
1342         && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
1343       return false;
1344     attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
1345     return attr->int_value() == 'M';
1346   }
1347
1348   // Whether we have an NOP instruction.  If not, use mov r0, r0 instead.
1349   bool
1350   may_use_arm_nop() const
1351   {
1352     Object_attribute* attr =
1353       this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1354     int arch = attr->int_value();
1355     return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1356             || arch == elfcpp::TAG_CPU_ARCH_V6K
1357             || arch == elfcpp::TAG_CPU_ARCH_V7
1358             || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1359   }
1360
1361   // Whether we have THUMB-2 NOP.W instruction.
1362   bool
1363   may_use_thumb2_nop() const
1364   {
1365     Object_attribute* attr =
1366       this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
1367     int arch = attr->int_value();
1368     return (arch == elfcpp::TAG_CPU_ARCH_V6T2
1369             || arch == elfcpp::TAG_CPU_ARCH_V7
1370             || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
1371   }
1372   
1373   // Process the relocations to determine unreferenced sections for 
1374   // garbage collection.
1375   void
1376   gc_process_relocs(Symbol_table* symtab,
1377                     Layout* layout,
1378                     Sized_relobj<32, big_endian>* object,
1379                     unsigned int data_shndx,
1380                     unsigned int sh_type,
1381                     const unsigned char* prelocs,
1382                     size_t reloc_count,
1383                     Output_section* output_section,
1384                     bool needs_special_offset_handling,
1385                     size_t local_symbol_count,
1386                     const unsigned char* plocal_symbols);
1387
1388   // Scan the relocations to look for symbol adjustments.
1389   void
1390   scan_relocs(Symbol_table* symtab,
1391               Layout* layout,
1392               Sized_relobj<32, big_endian>* object,
1393               unsigned int data_shndx,
1394               unsigned int sh_type,
1395               const unsigned char* prelocs,
1396               size_t reloc_count,
1397               Output_section* output_section,
1398               bool needs_special_offset_handling,
1399               size_t local_symbol_count,
1400               const unsigned char* plocal_symbols);
1401
1402   // Finalize the sections.
1403   void
1404   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
1405
1406   // Return the value to use for a dynamic symbol which requires special
1407   // treatment.
1408   uint64_t
1409   do_dynsym_value(const Symbol*) const;
1410
1411   // Relocate a section.
1412   void
1413   relocate_section(const Relocate_info<32, big_endian>*,
1414                    unsigned int sh_type,
1415                    const unsigned char* prelocs,
1416                    size_t reloc_count,
1417                    Output_section* output_section,
1418                    bool needs_special_offset_handling,
1419                    unsigned char* view,
1420                    Arm_address view_address,
1421                    section_size_type view_size,
1422                    const Reloc_symbol_changes*);
1423
1424   // Scan the relocs during a relocatable link.
1425   void
1426   scan_relocatable_relocs(Symbol_table* symtab,
1427                           Layout* layout,
1428                           Sized_relobj<32, big_endian>* object,
1429                           unsigned int data_shndx,
1430                           unsigned int sh_type,
1431                           const unsigned char* prelocs,
1432                           size_t reloc_count,
1433                           Output_section* output_section,
1434                           bool needs_special_offset_handling,
1435                           size_t local_symbol_count,
1436                           const unsigned char* plocal_symbols,
1437                           Relocatable_relocs*);
1438
1439   // Relocate a section during a relocatable link.
1440   void
1441   relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1442                            unsigned int sh_type,
1443                            const unsigned char* prelocs,
1444                            size_t reloc_count,
1445                            Output_section* output_section,
1446                            off_t offset_in_output_section,
1447                            const Relocatable_relocs*,
1448                            unsigned char* view,
1449                            Arm_address view_address,
1450                            section_size_type view_size,
1451                            unsigned char* reloc_view,
1452                            section_size_type reloc_view_size);
1453
1454   // Return whether SYM is defined by the ABI.
1455   bool
1456   do_is_defined_by_abi(Symbol* sym) const
1457   { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1458
1459   // Return the size of the GOT section.
1460   section_size_type
1461   got_size()
1462   {
1463     gold_assert(this->got_ != NULL);
1464     return this->got_->data_size();
1465   }
1466
1467   // Map platform-specific reloc types
1468   static unsigned int
1469   get_real_reloc_type (unsigned int r_type);
1470
1471   //
1472   // Methods to support stub-generations.
1473   //
1474   
1475   // Return the stub factory
1476   const Stub_factory&
1477   stub_factory() const
1478   { return this->stub_factory_; }
1479
1480   // Make a new Arm_input_section object.
1481   Arm_input_section<big_endian>*
1482   new_arm_input_section(Relobj*, unsigned int);
1483
1484   // Find the Arm_input_section object corresponding to the SHNDX-th input
1485   // section of RELOBJ.
1486   Arm_input_section<big_endian>*
1487   find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
1488
1489   // Make a new Stub_table
1490   Stub_table<big_endian>*
1491   new_stub_table(Arm_input_section<big_endian>*);
1492
1493   // Scan a section for stub generation.
1494   void
1495   scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
1496                          const unsigned char*, size_t, Output_section*,
1497                          bool, const unsigned char*, Arm_address,
1498                          section_size_type);
1499
1500   // Relocate a stub. 
1501   void
1502   relocate_stub(Reloc_stub*, const Relocate_info<32, big_endian>*,
1503                 Output_section*, unsigned char*, Arm_address,
1504                 section_size_type);
1505  
1506   // Get the default ARM target.
1507   static Target_arm<big_endian>*
1508   default_target()
1509   {
1510     gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1511                 && parameters->target().is_big_endian() == big_endian);
1512     return static_cast<Target_arm<big_endian>*>(
1513              parameters->sized_target<32, big_endian>());
1514   }
1515
1516   // Whether relocation type uses LSB to distinguish THUMB addresses.
1517   static bool
1518   reloc_uses_thumb_bit(unsigned int r_type);
1519
1520  protected:
1521   // Make an ELF object.
1522   Object*
1523   do_make_elf_object(const std::string&, Input_file*, off_t,
1524                      const elfcpp::Ehdr<32, big_endian>& ehdr);
1525
1526   Object*
1527   do_make_elf_object(const std::string&, Input_file*, off_t,
1528                      const elfcpp::Ehdr<32, !big_endian>&)
1529   { gold_unreachable(); }
1530
1531   Object*
1532   do_make_elf_object(const std::string&, Input_file*, off_t,
1533                       const elfcpp::Ehdr<64, false>&)
1534   { gold_unreachable(); }
1535
1536   Object*
1537   do_make_elf_object(const std::string&, Input_file*, off_t,
1538                      const elfcpp::Ehdr<64, true>&)
1539   { gold_unreachable(); }
1540
1541   // Make an output section.
1542   Output_section*
1543   do_make_output_section(const char* name, elfcpp::Elf_Word type,
1544                          elfcpp::Elf_Xword flags)
1545   { return new Arm_output_section<big_endian>(name, type, flags); }
1546
1547   void
1548   do_adjust_elf_header(unsigned char* view, int len) const;
1549
1550   // We only need to generate stubs, and hence perform relaxation if we are
1551   // not doing relocatable linking.
1552   bool
1553   do_may_relax() const
1554   { return !parameters->options().relocatable(); }
1555
1556   bool
1557   do_relax(int, const Input_objects*, Symbol_table*, Layout*);
1558
1559   // Determine whether an object attribute tag takes an integer, a
1560   // string or both.
1561   int
1562   do_attribute_arg_type(int tag) const;
1563
1564   // Reorder tags during output.
1565   int
1566   do_attributes_order(int num) const;
1567
1568  private:
1569   // The class which scans relocations.
1570   class Scan
1571   {
1572    public:
1573     Scan()
1574       : issued_non_pic_error_(false)
1575     { }
1576
1577     inline void
1578     local(Symbol_table* symtab, Layout* layout, Target_arm* target,
1579           Sized_relobj<32, big_endian>* object,
1580           unsigned int data_shndx,
1581           Output_section* output_section,
1582           const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1583           const elfcpp::Sym<32, big_endian>& lsym);
1584
1585     inline void
1586     global(Symbol_table* symtab, Layout* layout, Target_arm* target,
1587            Sized_relobj<32, big_endian>* object,
1588            unsigned int data_shndx,
1589            Output_section* output_section,
1590            const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1591            Symbol* gsym);
1592
1593    private:
1594     static void
1595     unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1596                             unsigned int r_type);
1597
1598     static void
1599     unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1600                              unsigned int r_type, Symbol*);
1601
1602     void
1603     check_non_pic(Relobj*, unsigned int r_type);
1604
1605     // Almost identical to Symbol::needs_plt_entry except that it also
1606     // handles STT_ARM_TFUNC.
1607     static bool
1608     symbol_needs_plt_entry(const Symbol* sym)
1609     {
1610       // An undefined symbol from an executable does not need a PLT entry.
1611       if (sym->is_undefined() && !parameters->options().shared())
1612         return false;
1613
1614       return (!parameters->doing_static_link()
1615               && (sym->type() == elfcpp::STT_FUNC
1616                   || sym->type() == elfcpp::STT_ARM_TFUNC)
1617               && (sym->is_from_dynobj()
1618                   || sym->is_undefined()
1619                   || sym->is_preemptible()));
1620     }
1621
1622     // Whether we have issued an error about a non-PIC compilation.
1623     bool issued_non_pic_error_;
1624   };
1625
1626   // The class which implements relocation.
1627   class Relocate
1628   {
1629    public:
1630     Relocate()
1631     { }
1632
1633     ~Relocate()
1634     { }
1635
1636     // Return whether the static relocation needs to be applied.
1637     inline bool
1638     should_apply_static_reloc(const Sized_symbol<32>* gsym,
1639                               int ref_flags,
1640                               bool is_32bit,
1641                               Output_section* output_section);
1642
1643     // Do a relocation.  Return false if the caller should not issue
1644     // any warnings about this relocation.
1645     inline bool
1646     relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1647              Output_section*,  size_t relnum,
1648              const elfcpp::Rel<32, big_endian>&,
1649              unsigned int r_type, const Sized_symbol<32>*,
1650              const Symbol_value<32>*,
1651              unsigned char*, Arm_address,
1652              section_size_type);
1653
1654     // Return whether we want to pass flag NON_PIC_REF for this
1655     // reloc.  This means the relocation type accesses a symbol not via
1656     // GOT or PLT.
1657     static inline bool
1658     reloc_is_non_pic (unsigned int r_type)
1659     {
1660       switch (r_type)
1661         {
1662         // These relocation types reference GOT or PLT entries explicitly.
1663         case elfcpp::R_ARM_GOT_BREL:
1664         case elfcpp::R_ARM_GOT_ABS:
1665         case elfcpp::R_ARM_GOT_PREL:
1666         case elfcpp::R_ARM_GOT_BREL12:
1667         case elfcpp::R_ARM_PLT32_ABS:
1668         case elfcpp::R_ARM_TLS_GD32:
1669         case elfcpp::R_ARM_TLS_LDM32:
1670         case elfcpp::R_ARM_TLS_IE32:
1671         case elfcpp::R_ARM_TLS_IE12GP:
1672
1673         // These relocate types may use PLT entries.
1674         case elfcpp::R_ARM_CALL:
1675         case elfcpp::R_ARM_THM_CALL:
1676         case elfcpp::R_ARM_JUMP24:
1677         case elfcpp::R_ARM_THM_JUMP24:
1678         case elfcpp::R_ARM_THM_JUMP19:
1679         case elfcpp::R_ARM_PLT32:
1680         case elfcpp::R_ARM_THM_XPC22:
1681           return false;
1682
1683         default:
1684           return true;
1685         }
1686     }
1687   };
1688
1689   // A class which returns the size required for a relocation type,
1690   // used while scanning relocs during a relocatable link.
1691   class Relocatable_size_for_reloc
1692   {
1693    public:
1694     unsigned int
1695     get_size_for_reloc(unsigned int, Relobj*);
1696   };
1697
1698   // Get the GOT section, creating it if necessary.
1699   Output_data_got<32, big_endian>*
1700   got_section(Symbol_table*, Layout*);
1701
1702   // Get the GOT PLT section.
1703   Output_data_space*
1704   got_plt_section() const
1705   {
1706     gold_assert(this->got_plt_ != NULL);
1707     return this->got_plt_;
1708   }
1709
1710   // Create a PLT entry for a global symbol.
1711   void
1712   make_plt_entry(Symbol_table*, Layout*, Symbol*);
1713
1714   // Get the PLT section.
1715   const Output_data_plt_arm<big_endian>*
1716   plt_section() const
1717   {
1718     gold_assert(this->plt_ != NULL);
1719     return this->plt_;
1720   }
1721
1722   // Get the dynamic reloc section, creating it if necessary.
1723   Reloc_section*
1724   rel_dyn_section(Layout*);
1725
1726   // Return true if the symbol may need a COPY relocation.
1727   // References from an executable object to non-function symbols
1728   // defined in a dynamic object may need a COPY relocation.
1729   bool
1730   may_need_copy_reloc(Symbol* gsym)
1731   {
1732     return (gsym->type() != elfcpp::STT_ARM_TFUNC
1733             && gsym->may_need_copy_reloc());
1734   }
1735
1736   // Add a potential copy relocation.
1737   void
1738   copy_reloc(Symbol_table* symtab, Layout* layout,
1739              Sized_relobj<32, big_endian>* object,
1740              unsigned int shndx, Output_section* output_section,
1741              Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1742   {
1743     this->copy_relocs_.copy_reloc(symtab, layout,
1744                                   symtab->get_sized_symbol<32>(sym),
1745                                   object, shndx, output_section, reloc,
1746                                   this->rel_dyn_section(layout));
1747   }
1748
1749   // Whether two EABI versions are compatible.
1750   static bool
1751   are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1752
1753   // Merge processor-specific flags from input object and those in the ELF
1754   // header of the output.
1755   void
1756   merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1757
1758   // Get the secondary compatible architecture.
1759   static int
1760   get_secondary_compatible_arch(const Attributes_section_data*);
1761
1762   // Set the secondary compatible architecture.
1763   static void
1764   set_secondary_compatible_arch(Attributes_section_data*, int);
1765
1766   static int
1767   tag_cpu_arch_combine(const char*, int, int*, int, int);
1768
1769   // Helper to print AEABI enum tag value.
1770   static std::string
1771   aeabi_enum_name(unsigned int);
1772
1773   // Return string value for TAG_CPU_name.
1774   static std::string
1775   tag_cpu_name_value(unsigned int);
1776
1777   // Merge object attributes from input object and those in the output.
1778   void
1779   merge_object_attributes(const char*, const Attributes_section_data*);
1780
1781   // Helper to get an AEABI object attribute
1782   Object_attribute*
1783   get_aeabi_object_attribute(int tag) const
1784   {
1785     Attributes_section_data* pasd = this->attributes_section_data_;
1786     gold_assert(pasd != NULL);
1787     Object_attribute* attr =
1788       pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
1789     gold_assert(attr != NULL);
1790     return attr;
1791   }
1792
1793   //
1794   // Methods to support stub-generations.
1795   //
1796
1797   // Group input sections for stub generation.
1798   void
1799   group_sections(Layout*, section_size_type, bool);
1800
1801   // Scan a relocation for stub generation.
1802   void
1803   scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
1804                       const Sized_symbol<32>*, unsigned int,
1805                       const Symbol_value<32>*,
1806                       elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
1807
1808   // Scan a relocation section for stub.
1809   template<int sh_type>
1810   void
1811   scan_reloc_section_for_stubs(
1812       const Relocate_info<32, big_endian>* relinfo,
1813       const unsigned char* prelocs,
1814       size_t reloc_count,
1815       Output_section* output_section,
1816       bool needs_special_offset_handling,
1817       const unsigned char* view,
1818       elfcpp::Elf_types<32>::Elf_Addr view_address,
1819       section_size_type);
1820
1821   // Information about this specific target which we pass to the
1822   // general Target structure.
1823   static const Target::Target_info arm_info;
1824
1825   // The types of GOT entries needed for this platform.
1826   enum Got_type
1827   {
1828     GOT_TYPE_STANDARD = 0       // GOT entry for a regular symbol
1829   };
1830
1831   typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
1832
1833   // Map input section to Arm_input_section.
1834   typedef Unordered_map<Input_section_specifier,
1835                         Arm_input_section<big_endian>*,
1836                         Input_section_specifier::hash,
1837                         Input_section_specifier::equal_to>
1838           Arm_input_section_map;
1839     
1840   // The GOT section.
1841   Output_data_got<32, big_endian>* got_;
1842   // The PLT section.
1843   Output_data_plt_arm<big_endian>* plt_;
1844   // The GOT PLT section.
1845   Output_data_space* got_plt_;
1846   // The dynamic reloc section.
1847   Reloc_section* rel_dyn_;
1848   // Relocs saved to avoid a COPY reloc.
1849   Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1850   // Space for variables copied with a COPY reloc.
1851   Output_data_space* dynbss_;
1852   // Vector of Stub_tables created.
1853   Stub_table_list stub_tables_;
1854   // Stub factory.
1855   const Stub_factory &stub_factory_;
1856   // Whether we can use BLX.
1857   bool may_use_blx_;
1858   // Whether we force PIC branch veneers.
1859   bool should_force_pic_veneer_;
1860   // Map for locating Arm_input_sections.
1861   Arm_input_section_map arm_input_section_map_;
1862   // Attributes section data in output.
1863   Attributes_section_data* attributes_section_data_;
1864 };
1865
1866 template<bool big_endian>
1867 const Target::Target_info Target_arm<big_endian>::arm_info =
1868 {
1869   32,                   // size
1870   big_endian,           // is_big_endian
1871   elfcpp::EM_ARM,       // machine_code
1872   false,                // has_make_symbol
1873   false,                // has_resolve
1874   false,                // has_code_fill
1875   true,                 // is_default_stack_executable
1876   '\0',                 // wrap_char
1877   "/usr/lib/libc.so.1", // dynamic_linker
1878   0x8000,               // default_text_segment_address
1879   0x1000,               // abi_pagesize (overridable by -z max-page-size)
1880   0x1000,               // common_pagesize (overridable by -z common-page-size)
1881   elfcpp::SHN_UNDEF,    // small_common_shndx
1882   elfcpp::SHN_UNDEF,    // large_common_shndx
1883   0,                    // small_common_section_flags
1884   0,                    // large_common_section_flags
1885   ".ARM.attributes",    // attributes_section
1886   "aeabi"               // attributes_vendor
1887 };
1888
1889 // Arm relocate functions class
1890 //
1891
1892 template<bool big_endian>
1893 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1894 {
1895  public:
1896   typedef enum
1897   {
1898     STATUS_OKAY,        // No error during relocation.
1899     STATUS_OVERFLOW,    // Relocation oveflow.
1900     STATUS_BAD_RELOC    // Relocation cannot be applied.
1901   } Status;
1902
1903  private:
1904   typedef Relocate_functions<32, big_endian> Base;
1905   typedef Arm_relocate_functions<big_endian> This;
1906
1907   // Encoding of imm16 argument for movt and movw ARM instructions
1908   // from ARM ARM:
1909   //     
1910   //     imm16 := imm4 | imm12
1911   //
1912   //  f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0 
1913   // +-------+---------------+-------+-------+-----------------------+
1914   // |       |               |imm4   |       |imm12                  |
1915   // +-------+---------------+-------+-------+-----------------------+
1916
1917   // Extract the relocation addend from VAL based on the ARM
1918   // instruction encoding described above.
1919   static inline typename elfcpp::Swap<32, big_endian>::Valtype
1920   extract_arm_movw_movt_addend(
1921       typename elfcpp::Swap<32, big_endian>::Valtype val)
1922   {
1923     // According to the Elf ABI for ARM Architecture the immediate
1924     // field is sign-extended to form the addend.
1925     return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1926   }
1927
1928   // Insert X into VAL based on the ARM instruction encoding described
1929   // above.
1930   static inline typename elfcpp::Swap<32, big_endian>::Valtype
1931   insert_val_arm_movw_movt(
1932       typename elfcpp::Swap<32, big_endian>::Valtype val,
1933       typename elfcpp::Swap<32, big_endian>::Valtype x)
1934   {
1935     val &= 0xfff0f000;
1936     val |= x & 0x0fff;
1937     val |= (x & 0xf000) << 4;
1938     return val;
1939   }
1940
1941   // Encoding of imm16 argument for movt and movw Thumb2 instructions
1942   // from ARM ARM:
1943   //     
1944   //     imm16 := imm4 | i | imm3 | imm8
1945   //
1946   //  f e d c b a 9 8 7 6 5 4 3 2 1 0  f e d c b a 9 8 7 6 5 4 3 2 1 0 
1947   // +---------+-+-----------+-------++-+-----+-------+---------------+
1948   // |         |i|           |imm4   || |imm3 |       |imm8           |
1949   // +---------+-+-----------+-------++-+-----+-------+---------------+
1950
1951   // Extract the relocation addend from VAL based on the Thumb2
1952   // instruction encoding described above.
1953   static inline typename elfcpp::Swap<32, big_endian>::Valtype
1954   extract_thumb_movw_movt_addend(
1955       typename elfcpp::Swap<32, big_endian>::Valtype val)
1956   {
1957     // According to the Elf ABI for ARM Architecture the immediate
1958     // field is sign-extended to form the addend.
1959     return utils::sign_extend<16>(((val >> 4) & 0xf000)
1960                                   | ((val >> 15) & 0x0800)
1961                                   | ((val >> 4) & 0x0700)
1962                                   | (val & 0x00ff));
1963   }
1964
1965   // Insert X into VAL based on the Thumb2 instruction encoding
1966   // described above.
1967   static inline typename elfcpp::Swap<32, big_endian>::Valtype
1968   insert_val_thumb_movw_movt(
1969       typename elfcpp::Swap<32, big_endian>::Valtype val,
1970       typename elfcpp::Swap<32, big_endian>::Valtype x)
1971   {
1972     val &= 0xfbf08f00;
1973     val |= (x & 0xf000) << 4;
1974     val |= (x & 0x0800) << 15;
1975     val |= (x & 0x0700) << 4;
1976     val |= (x & 0x00ff);
1977     return val;
1978   }
1979
1980   // Handle ARM long branches.
1981   static typename This::Status
1982   arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
1983                     unsigned char *, const Sized_symbol<32>*,
1984                     const Arm_relobj<big_endian>*, unsigned int,
1985                     const Symbol_value<32>*, Arm_address, Arm_address, bool);
1986
1987   // Handle THUMB long branches.
1988   static typename This::Status
1989   thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
1990                       unsigned char *, const Sized_symbol<32>*,
1991                       const Arm_relobj<big_endian>*, unsigned int,
1992                       const Symbol_value<32>*, Arm_address, Arm_address, bool);
1993
1994  public:
1995
1996   // R_ARM_ABS8: S + A
1997   static inline typename This::Status
1998   abs8(unsigned char *view,
1999        const Sized_relobj<32, big_endian>* object,
2000        const Symbol_value<32>* psymval)
2001   {
2002     typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
2003     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2004     Valtype* wv = reinterpret_cast<Valtype*>(view);
2005     Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
2006     Reltype addend = utils::sign_extend<8>(val);
2007     Reltype x = psymval->value(object, addend);
2008     val = utils::bit_select(val, x, 0xffU);
2009     elfcpp::Swap<8, big_endian>::writeval(wv, val);
2010     return (utils::has_signed_unsigned_overflow<8>(x)
2011             ? This::STATUS_OVERFLOW
2012             : This::STATUS_OKAY);
2013   }
2014
2015   // R_ARM_THM_ABS5: S + A
2016   static inline typename This::Status
2017   thm_abs5(unsigned char *view,
2018        const Sized_relobj<32, big_endian>* object,
2019        const Symbol_value<32>* psymval)
2020   {
2021     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2022     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2023     Valtype* wv = reinterpret_cast<Valtype*>(view);
2024     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2025     Reltype addend = (val & 0x7e0U) >> 6;
2026     Reltype x = psymval->value(object, addend);
2027     val = utils::bit_select(val, x << 6, 0x7e0U);
2028     elfcpp::Swap<16, big_endian>::writeval(wv, val);
2029     return (utils::has_overflow<5>(x)
2030             ? This::STATUS_OVERFLOW
2031             : This::STATUS_OKAY);
2032   }
2033
2034   // R_ARM_ABS12: S + A
2035   static inline typename This::Status
2036   abs12(unsigned char *view,
2037         const Sized_relobj<32, big_endian>* object,
2038         const Symbol_value<32>* psymval)
2039   {
2040     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2041     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2042     Valtype* wv = reinterpret_cast<Valtype*>(view);
2043     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2044     Reltype addend = val & 0x0fffU;
2045     Reltype x = psymval->value(object, addend);
2046     val = utils::bit_select(val, x, 0x0fffU);
2047     elfcpp::Swap<32, big_endian>::writeval(wv, val);
2048     return (utils::has_overflow<12>(x)
2049             ? This::STATUS_OVERFLOW
2050             : This::STATUS_OKAY);
2051   }
2052
2053   // R_ARM_ABS16: S + A
2054   static inline typename This::Status
2055   abs16(unsigned char *view,
2056         const Sized_relobj<32, big_endian>* object,
2057         const Symbol_value<32>* psymval)
2058   {
2059     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2060     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2061     Valtype* wv = reinterpret_cast<Valtype*>(view);
2062     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
2063     Reltype addend = utils::sign_extend<16>(val);
2064     Reltype x = psymval->value(object, addend);
2065     val = utils::bit_select(val, x, 0xffffU);
2066     elfcpp::Swap<16, big_endian>::writeval(wv, val);
2067     return (utils::has_signed_unsigned_overflow<16>(x)
2068             ? This::STATUS_OVERFLOW
2069             : This::STATUS_OKAY);
2070   }
2071
2072   // R_ARM_ABS32: (S + A) | T
2073   static inline typename This::Status
2074   abs32(unsigned char *view,
2075         const Sized_relobj<32, big_endian>* object,
2076         const Symbol_value<32>* psymval,
2077         Arm_address thumb_bit)
2078   {
2079     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2080     Valtype* wv = reinterpret_cast<Valtype*>(view);
2081     Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2082     Valtype x = psymval->value(object, addend) | thumb_bit;
2083     elfcpp::Swap<32, big_endian>::writeval(wv, x);
2084     return This::STATUS_OKAY;
2085   }
2086
2087   // R_ARM_REL32: (S + A) | T - P
2088   static inline typename This::Status
2089   rel32(unsigned char *view,
2090         const Sized_relobj<32, big_endian>* object,
2091         const Symbol_value<32>* psymval,
2092         Arm_address address,
2093         Arm_address thumb_bit)
2094   {
2095     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2096     Valtype* wv = reinterpret_cast<Valtype*>(view);
2097     Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
2098     Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2099     elfcpp::Swap<32, big_endian>::writeval(wv, x);
2100     return This::STATUS_OKAY;
2101   }
2102
2103   // R_ARM_THM_CALL: (S + A) | T - P
2104   static inline typename This::Status
2105   thm_call(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2106            const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2107            unsigned int r_sym, const Symbol_value<32>* psymval,
2108            Arm_address address, Arm_address thumb_bit,
2109            bool is_weakly_undefined_without_plt)
2110   {
2111     return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym,
2112                                object, r_sym, psymval, address, thumb_bit,
2113                                is_weakly_undefined_without_plt);
2114   }
2115
2116   // R_ARM_THM_JUMP24: (S + A) | T - P
2117   static inline typename This::Status
2118   thm_jump24(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2119              const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2120              unsigned int r_sym, const Symbol_value<32>* psymval,
2121              Arm_address address, Arm_address thumb_bit,
2122              bool is_weakly_undefined_without_plt)
2123   {
2124     return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym,
2125                                object, r_sym, psymval, address, thumb_bit,
2126                                is_weakly_undefined_without_plt);
2127   }
2128
2129   // R_ARM_THM_XPC22: (S + A) | T - P
2130   static inline typename This::Status
2131   thm_xpc22(const Relocate_info<32, big_endian>* relinfo, unsigned char *view,
2132             const Sized_symbol<32>* gsym, const Arm_relobj<big_endian>* object,
2133             unsigned int r_sym, const Symbol_value<32>* psymval,
2134             Arm_address address, Arm_address thumb_bit,
2135             bool is_weakly_undefined_without_plt)
2136   {
2137     return thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym,
2138                                object, r_sym, psymval, address, thumb_bit,
2139                                is_weakly_undefined_without_plt);
2140   }
2141
2142   // R_ARM_BASE_PREL: B(S) + A - P
2143   static inline typename This::Status
2144   base_prel(unsigned char* view,
2145             Arm_address origin,
2146             Arm_address address)
2147   {
2148     Base::rel32(view, origin - address);
2149     return STATUS_OKAY;
2150   }
2151
2152   // R_ARM_BASE_ABS: B(S) + A
2153   static inline typename This::Status
2154   base_abs(unsigned char* view,
2155            Arm_address origin)
2156   {
2157     Base::rel32(view, origin);
2158     return STATUS_OKAY;
2159   }
2160
2161   // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
2162   static inline typename This::Status
2163   got_brel(unsigned char* view,
2164            typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
2165   {
2166     Base::rel32(view, got_offset);
2167     return This::STATUS_OKAY;
2168   }
2169
2170   // R_ARM_GOT_PREL: GOT(S) + A - P
2171   static inline typename This::Status
2172   got_prel(unsigned char *view,
2173            Arm_address got_entry,
2174            Arm_address address)
2175   {
2176     Base::rel32(view, got_entry - address);
2177     return This::STATUS_OKAY;
2178   }
2179
2180   // R_ARM_PLT32: (S + A) | T - P
2181   static inline typename This::Status
2182   plt32(const Relocate_info<32, big_endian>* relinfo,
2183         unsigned char *view,
2184         const Sized_symbol<32>* gsym,
2185         const Arm_relobj<big_endian>* object,
2186         unsigned int r_sym,
2187         const Symbol_value<32>* psymval,
2188         Arm_address address,
2189         Arm_address thumb_bit,
2190         bool is_weakly_undefined_without_plt)
2191   {
2192     return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym,
2193                              object, r_sym, psymval, address, thumb_bit,
2194                              is_weakly_undefined_without_plt);
2195   }
2196
2197   // R_ARM_XPC25: (S + A) | T - P
2198   static inline typename This::Status
2199   xpc25(const Relocate_info<32, big_endian>* relinfo,
2200         unsigned char *view,
2201         const Sized_symbol<32>* gsym,
2202         const Arm_relobj<big_endian>* object,
2203         unsigned int r_sym,
2204         const Symbol_value<32>* psymval,
2205         Arm_address address,
2206         Arm_address thumb_bit,
2207         bool is_weakly_undefined_without_plt)
2208   {
2209     return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym,
2210                              object, r_sym, psymval, address, thumb_bit,
2211                              is_weakly_undefined_without_plt);
2212   }
2213
2214   // R_ARM_CALL: (S + A) | T - P
2215   static inline typename This::Status
2216   call(const Relocate_info<32, big_endian>* relinfo,
2217        unsigned char *view,
2218        const Sized_symbol<32>* gsym,
2219        const Arm_relobj<big_endian>* object,
2220        unsigned int r_sym,
2221        const Symbol_value<32>* psymval,
2222        Arm_address address,
2223        Arm_address thumb_bit,
2224        bool is_weakly_undefined_without_plt)
2225   {
2226     return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym,
2227                              object, r_sym, psymval, address, thumb_bit,
2228                              is_weakly_undefined_without_plt);
2229   }
2230
2231   // R_ARM_JUMP24: (S + A) | T - P
2232   static inline typename This::Status
2233   jump24(const Relocate_info<32, big_endian>* relinfo,
2234          unsigned char *view,
2235          const Sized_symbol<32>* gsym,
2236          const Arm_relobj<big_endian>* object,
2237          unsigned int r_sym,
2238          const Symbol_value<32>* psymval,
2239          Arm_address address,
2240          Arm_address thumb_bit,
2241          bool is_weakly_undefined_without_plt)
2242   {
2243     return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym,
2244                              object, r_sym, psymval, address, thumb_bit,
2245                              is_weakly_undefined_without_plt);
2246   }
2247
2248   // R_ARM_PREL: (S + A) | T - P
2249   static inline typename This::Status
2250   prel31(unsigned char *view,
2251          const Sized_relobj<32, big_endian>* object,
2252          const Symbol_value<32>* psymval,
2253          Arm_address address,
2254          Arm_address thumb_bit)
2255   {
2256     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2257     Valtype* wv = reinterpret_cast<Valtype*>(view);
2258     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2259     Valtype addend = utils::sign_extend<31>(val);
2260     Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2261     val = utils::bit_select(val, x, 0x7fffffffU);
2262     elfcpp::Swap<32, big_endian>::writeval(wv, val);
2263     return (utils::has_overflow<31>(x) ?
2264             This::STATUS_OVERFLOW : This::STATUS_OKAY);
2265   }
2266
2267   // R_ARM_MOVW_ABS_NC: (S + A) | T
2268   static inline typename This::Status 
2269   movw_abs_nc(unsigned char *view,
2270               const Sized_relobj<32, big_endian>* object,
2271               const Symbol_value<32>* psymval,
2272               Arm_address thumb_bit)
2273   {
2274     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2275     Valtype* wv = reinterpret_cast<Valtype*>(view);
2276     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2277     Valtype addend =  This::extract_arm_movw_movt_addend(val);
2278     Valtype x = psymval->value(object, addend) | thumb_bit;
2279     val = This::insert_val_arm_movw_movt(val, x);
2280     elfcpp::Swap<32, big_endian>::writeval(wv, val);
2281     return This::STATUS_OKAY;
2282   }
2283
2284   // R_ARM_MOVT_ABS: S + A
2285   static inline typename This::Status
2286   movt_abs(unsigned char *view,
2287            const Sized_relobj<32, big_endian>* object,
2288            const Symbol_value<32>* psymval)
2289   {
2290     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2291     Valtype* wv = reinterpret_cast<Valtype*>(view);
2292     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2293     Valtype addend = This::extract_arm_movw_movt_addend(val);
2294     Valtype x = psymval->value(object, addend) >> 16;
2295     val = This::insert_val_arm_movw_movt(val, x);
2296     elfcpp::Swap<32, big_endian>::writeval(wv, val);
2297     return This::STATUS_OKAY;
2298   }
2299
2300   //  R_ARM_THM_MOVW_ABS_NC: S + A | T
2301   static inline typename This::Status 
2302   thm_movw_abs_nc(unsigned char *view,
2303                   const Sized_relobj<32, big_endian>* object,
2304                   const Symbol_value<32>* psymval,
2305                   Arm_address thumb_bit)
2306   {
2307     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2308     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2309     Valtype* wv = reinterpret_cast<Valtype*>(view);
2310     Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2311                    | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2312     Reltype addend = extract_thumb_movw_movt_addend(val);
2313     Reltype x = psymval->value(object, addend) | thumb_bit;
2314     val = This::insert_val_thumb_movw_movt(val, x);
2315     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2316     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2317     return This::STATUS_OKAY;
2318   }
2319
2320   //  R_ARM_THM_MOVT_ABS: S + A
2321   static inline typename This::Status 
2322   thm_movt_abs(unsigned char *view,
2323                const Sized_relobj<32, big_endian>* object,
2324                const Symbol_value<32>* psymval)
2325   {
2326     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2327     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2328     Valtype* wv = reinterpret_cast<Valtype*>(view);
2329     Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2330                    | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2331     Reltype addend = This::extract_thumb_movw_movt_addend(val);
2332     Reltype x = psymval->value(object, addend) >> 16;
2333     val = This::insert_val_thumb_movw_movt(val, x);
2334     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2335     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2336     return This::STATUS_OKAY;
2337   }
2338
2339   // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2340   static inline typename This::Status
2341   movw_prel_nc(unsigned char *view,
2342                const Sized_relobj<32, big_endian>* object,
2343                const Symbol_value<32>* psymval,
2344                Arm_address address,
2345                Arm_address thumb_bit)
2346   {
2347     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2348     Valtype* wv = reinterpret_cast<Valtype*>(view);
2349     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2350     Valtype addend = This::extract_arm_movw_movt_addend(val);
2351     Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
2352     val = This::insert_val_arm_movw_movt(val, x);
2353     elfcpp::Swap<32, big_endian>::writeval(wv, val);
2354     return This::STATUS_OKAY;
2355   }
2356
2357   // R_ARM_MOVT_PREL: S + A - P
2358   static inline typename This::Status
2359   movt_prel(unsigned char *view,
2360             const Sized_relobj<32, big_endian>* object,
2361             const Symbol_value<32>* psymval,
2362             Arm_address address)
2363   {
2364     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2365     Valtype* wv = reinterpret_cast<Valtype*>(view);
2366     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2367     Valtype addend = This::extract_arm_movw_movt_addend(val);
2368     Valtype x = (psymval->value(object, addend) - address) >> 16;
2369     val = This::insert_val_arm_movw_movt(val, x);
2370     elfcpp::Swap<32, big_endian>::writeval(wv, val);
2371     return This::STATUS_OKAY;
2372   }
2373
2374   // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2375   static inline typename This::Status
2376   thm_movw_prel_nc(unsigned char *view,
2377                    const Sized_relobj<32, big_endian>* object,
2378                    const Symbol_value<32>* psymval,
2379                    Arm_address address,
2380                    Arm_address thumb_bit)
2381   {
2382     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2383     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2384     Valtype* wv = reinterpret_cast<Valtype*>(view);
2385     Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2386                   | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2387     Reltype addend = This::extract_thumb_movw_movt_addend(val);
2388     Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
2389     val = This::insert_val_thumb_movw_movt(val, x);
2390     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2391     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2392     return This::STATUS_OKAY;
2393   }
2394
2395   // R_ARM_THM_MOVT_PREL: S + A - P
2396   static inline typename This::Status
2397   thm_movt_prel(unsigned char *view,
2398                 const Sized_relobj<32, big_endian>* object,
2399                 const Symbol_value<32>* psymval,
2400                 Arm_address address)
2401   {
2402     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2403     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2404     Valtype* wv = reinterpret_cast<Valtype*>(view);
2405     Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2406                   | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2407     Reltype addend = This::extract_thumb_movw_movt_addend(val);
2408     Reltype x = (psymval->value(object, addend) - address) >> 16;
2409     val = This::insert_val_thumb_movw_movt(val, x);
2410     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2411     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2412     return This::STATUS_OKAY;
2413   }
2414 };
2415
2416 // Relocate ARM long branches.  This handles relocation types
2417 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
2418 // If IS_WEAK_UNDEFINED_WITH_PLT is true.  The target symbol is weakly
2419 // undefined and we do not use PLT in this relocation.  In such a case,
2420 // the branch is converted into an NOP.
2421
2422 template<bool big_endian>
2423 typename Arm_relocate_functions<big_endian>::Status
2424 Arm_relocate_functions<big_endian>::arm_branch_common(
2425     unsigned int r_type,
2426     const Relocate_info<32, big_endian>* relinfo,
2427     unsigned char *view,
2428     const Sized_symbol<32>* gsym,
2429     const Arm_relobj<big_endian>* object,
2430     unsigned int r_sym,
2431     const Symbol_value<32>* psymval,
2432     Arm_address address,
2433     Arm_address thumb_bit,
2434     bool is_weakly_undefined_without_plt)
2435 {
2436   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2437   Valtype* wv = reinterpret_cast<Valtype*>(view);
2438   Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2439      
2440   bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
2441                     && ((val & 0x0f000000UL) == 0x0a000000UL);
2442   bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
2443   bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
2444                           && ((val & 0x0f000000UL) == 0x0b000000UL);
2445   bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
2446   bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
2447
2448   // Check that the instruction is valid.
2449   if (r_type == elfcpp::R_ARM_CALL)
2450     {
2451       if (!insn_is_uncond_bl && !insn_is_blx)
2452         return This::STATUS_BAD_RELOC;
2453     }
2454   else if (r_type == elfcpp::R_ARM_JUMP24)
2455     {
2456       if (!insn_is_b && !insn_is_cond_bl)
2457         return This::STATUS_BAD_RELOC;
2458     }
2459   else if (r_type == elfcpp::R_ARM_PLT32)
2460     {
2461       if (!insn_is_any_branch)
2462         return This::STATUS_BAD_RELOC;
2463     }
2464   else if (r_type == elfcpp::R_ARM_XPC25)
2465     {
2466       // FIXME: AAELF document IH0044C does not say much about it other
2467       // than it being obsolete.
2468       if (!insn_is_any_branch)
2469         return This::STATUS_BAD_RELOC;
2470     }
2471   else
2472     gold_unreachable();
2473
2474   // A branch to an undefined weak symbol is turned into a jump to
2475   // the next instruction unless a PLT entry will be created.
2476   // Do the same for local undefined symbols.
2477   // The jump to the next instruction is optimized as a NOP depending
2478   // on the architecture.
2479   const Target_arm<big_endian>* arm_target =
2480     Target_arm<big_endian>::default_target();
2481   if (is_weakly_undefined_without_plt)
2482     {
2483       Valtype cond = val & 0xf0000000U;
2484       if (arm_target->may_use_arm_nop())
2485         val = cond | 0x0320f000;
2486       else
2487         val = cond | 0x01a00000;        // Using pre-UAL nop: mov r0, r0.
2488       elfcpp::Swap<32, big_endian>::writeval(wv, val);
2489       return This::STATUS_OKAY;
2490     }
2491  
2492   Valtype addend = utils::sign_extend<26>(val << 2);
2493   Valtype branch_target = psymval->value(object, addend);
2494   int32_t branch_offset = branch_target - address;
2495
2496   // We need a stub if the branch offset is too large or if we need
2497   // to switch mode.
2498   bool may_use_blx = arm_target->may_use_blx();
2499   Reloc_stub* stub = NULL;
2500   if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
2501       || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2502       || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
2503     {
2504       Stub_type stub_type =
2505         Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2506                                         (thumb_bit != 0));
2507       if (stub_type != arm_stub_none)
2508         {
2509           Stub_table<big_endian>* stub_table =
2510             object->stub_table(relinfo->data_shndx);
2511           gold_assert(stub_table != NULL);
2512
2513           Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2514           stub = stub_table->find_reloc_stub(stub_key);
2515           gold_assert(stub != NULL);
2516           thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2517           branch_target = stub_table->address() + stub->offset() + addend;
2518           branch_offset = branch_target - address;
2519           gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
2520                       && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
2521         }
2522     }
2523
2524   // At this point, if we still need to switch mode, the instruction
2525   // must either be a BLX or a BL that can be converted to a BLX.
2526   if (thumb_bit != 0)
2527     {
2528       // Turn BL to BLX.
2529       gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
2530       val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
2531     }
2532
2533   val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
2534   elfcpp::Swap<32, big_endian>::writeval(wv, val);
2535   return (utils::has_overflow<26>(branch_offset)
2536           ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
2537 }
2538
2539 // Relocate THUMB long branches.  This handles relocation types
2540 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
2541 // If IS_WEAK_UNDEFINED_WITH_PLT is true.  The target symbol is weakly
2542 // undefined and we do not use PLT in this relocation.  In such a case,
2543 // the branch is converted into an NOP.
2544
2545 template<bool big_endian>
2546 typename Arm_relocate_functions<big_endian>::Status
2547 Arm_relocate_functions<big_endian>::thumb_branch_common(
2548     unsigned int r_type,
2549     const Relocate_info<32, big_endian>* relinfo,
2550     unsigned char *view,
2551     const Sized_symbol<32>* gsym,
2552     const Arm_relobj<big_endian>* object,
2553     unsigned int r_sym,
2554     const Symbol_value<32>* psymval,
2555     Arm_address address,
2556     Arm_address thumb_bit,
2557     bool is_weakly_undefined_without_plt)
2558 {
2559   typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2560   Valtype* wv = reinterpret_cast<Valtype*>(view);
2561   uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
2562   uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
2563
2564   // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
2565   // into account.
2566   bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
2567   bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
2568      
2569   // Check that the instruction is valid.
2570   if (r_type == elfcpp::R_ARM_THM_CALL)
2571     {
2572       if (!is_bl_insn && !is_blx_insn)
2573         return This::STATUS_BAD_RELOC;
2574     }
2575   else if (r_type == elfcpp::R_ARM_THM_JUMP24)
2576     {
2577       // This cannot be a BLX.
2578       if (!is_bl_insn)
2579         return This::STATUS_BAD_RELOC;
2580     }
2581   else if (r_type == elfcpp::R_ARM_THM_XPC22)
2582     {
2583       // Check for Thumb to Thumb call.
2584       if (!is_blx_insn)
2585         return This::STATUS_BAD_RELOC;
2586       if (thumb_bit != 0)
2587         {
2588           gold_warning(_("%s: Thumb BLX instruction targets "
2589                          "thumb function '%s'."),
2590                          object->name().c_str(),
2591                          (gsym ? gsym->name() : "(local)")); 
2592           // Convert BLX to BL.
2593           lower_insn |= 0x1000U;
2594         }
2595     }
2596   else
2597     gold_unreachable();
2598
2599   // A branch to an undefined weak symbol is turned into a jump to
2600   // the next instruction unless a PLT entry will be created.
2601   // The jump to the next instruction is optimized as a NOP.W for
2602   // Thumb-2 enabled architectures.
2603   const Target_arm<big_endian>* arm_target =
2604     Target_arm<big_endian>::default_target();
2605   if (is_weakly_undefined_without_plt)
2606     {
2607       if (arm_target->may_use_thumb2_nop())
2608         {
2609           elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
2610           elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
2611         }
2612       else
2613         {
2614           elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
2615           elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
2616         }
2617       return This::STATUS_OKAY;
2618     }
2619  
2620   // Fetch the addend.  We use the Thumb-2 encoding (backwards compatible
2621   // with Thumb-1) involving the J1 and J2 bits.
2622   uint32_t s = (upper_insn & (1 << 10)) >> 10;
2623   uint32_t upper = upper_insn & 0x3ff;
2624   uint32_t lower = lower_insn & 0x7ff;
2625   uint32_t j1 = (lower_insn & (1 << 13)) >> 13;
2626   uint32_t j2 = (lower_insn & (1 << 11)) >> 11;
2627   uint32_t i1 = j1 ^ s ? 0 : 1;
2628   uint32_t i2 = j2 ^ s ? 0 : 1;
2629  
2630   int32_t addend = (i1 << 23) | (i2 << 22) | (upper << 12) | (lower << 1);
2631   // Sign extend.
2632   addend = (addend | ((s ? 0 : 1) << 24)) - (1 << 24);
2633
2634   Arm_address branch_target = psymval->value(object, addend);
2635   int32_t branch_offset = branch_target - address;
2636
2637   // We need a stub if the branch offset is too large or if we need
2638   // to switch mode.
2639   bool may_use_blx = arm_target->may_use_blx();
2640   bool thumb2 = arm_target->using_thumb2();
2641   if ((!thumb2
2642        && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2643            || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2644       || (thumb2
2645           && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2646               || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2647       || ((thumb_bit == 0)
2648           && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2649               || r_type == elfcpp::R_ARM_THM_JUMP24)))
2650     {
2651       Stub_type stub_type =
2652         Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
2653                                         (thumb_bit != 0));
2654       if (stub_type != arm_stub_none)
2655         {
2656           Stub_table<big_endian>* stub_table =
2657             object->stub_table(relinfo->data_shndx);
2658           gold_assert(stub_table != NULL);
2659
2660           Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
2661           Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
2662           gold_assert(stub != NULL);
2663           thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
2664           branch_target = stub_table->address() + stub->offset() + addend;
2665           branch_offset = branch_target - address;
2666         }
2667     }
2668
2669   // At this point, if we still need to switch mode, the instruction
2670   // must either be a BLX or a BL that can be converted to a BLX.
2671   if (thumb_bit == 0)
2672     {
2673       gold_assert(may_use_blx
2674                   && (r_type == elfcpp::R_ARM_THM_CALL
2675                       || r_type == elfcpp::R_ARM_THM_XPC22));
2676       // Make sure this is a BLX.
2677       lower_insn &= ~0x1000U;
2678     }
2679   else
2680     {
2681       // Make sure this is a BL.
2682       lower_insn |= 0x1000U;
2683     }
2684
2685   uint32_t reloc_sign = (branch_offset < 0) ? 1 : 0;
2686   uint32_t relocation = static_cast<uint32_t>(branch_offset);
2687
2688   if ((lower_insn & 0x5000U) == 0x4000U)
2689     // For a BLX instruction, make sure that the relocation is rounded up
2690     // to a word boundary.  This follows the semantics of the instruction
2691     // which specifies that bit 1 of the target address will come from bit
2692     // 1 of the base address.
2693     relocation = (relocation + 2U) & ~3U;
2694
2695   // Put BRANCH_OFFSET back into the insn.  Assumes two's complement.
2696   // We use the Thumb-2 encoding, which is safe even if dealing with
2697   // a Thumb-1 instruction by virtue of our overflow check above.  */
2698   upper_insn = (upper_insn & ~0x7ffU)
2699                 | ((relocation >> 12) & 0x3ffU)
2700                 | (reloc_sign << 10);
2701   lower_insn = (lower_insn & ~0x2fffU)
2702                 | (((!((relocation >> 23) & 1U)) ^ reloc_sign) << 13)
2703                 | (((!((relocation >> 22) & 1U)) ^ reloc_sign) << 11)
2704                 | ((relocation >> 1) & 0x7ffU);
2705
2706   elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
2707   elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
2708
2709   return ((thumb2
2710            ? utils::has_overflow<25>(relocation)
2711            : utils::has_overflow<23>(relocation))
2712           ? This::STATUS_OVERFLOW
2713           : This::STATUS_OKAY);
2714 }
2715
2716 // Get the GOT section, creating it if necessary.
2717
2718 template<bool big_endian>
2719 Output_data_got<32, big_endian>*
2720 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
2721 {
2722   if (this->got_ == NULL)
2723     {
2724       gold_assert(symtab != NULL && layout != NULL);
2725
2726       this->got_ = new Output_data_got<32, big_endian>();
2727
2728       Output_section* os;
2729       os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2730                                            (elfcpp::SHF_ALLOC
2731                                             | elfcpp::SHF_WRITE),
2732                                            this->got_, false, true, true,
2733                                            false);
2734
2735       // The old GNU linker creates a .got.plt section.  We just
2736       // create another set of data in the .got section.  Note that we
2737       // always create a PLT if we create a GOT, although the PLT
2738       // might be empty.
2739       this->got_plt_ = new Output_data_space(4, "** GOT PLT");
2740       os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2741                                            (elfcpp::SHF_ALLOC
2742                                             | elfcpp::SHF_WRITE),
2743                                            this->got_plt_, false, false,
2744                                            false, true);
2745
2746       // The first three entries are reserved.
2747       this->got_plt_->set_current_data_size(3 * 4);
2748
2749       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2750       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2751                                     Symbol_table::PREDEFINED,
2752                                     this->got_plt_,
2753                                     0, 0, elfcpp::STT_OBJECT,
2754                                     elfcpp::STB_LOCAL,
2755                                     elfcpp::STV_HIDDEN, 0,
2756                                     false, false);
2757     }
2758   return this->got_;
2759 }
2760
2761 // Get the dynamic reloc section, creating it if necessary.
2762
2763 template<bool big_endian>
2764 typename Target_arm<big_endian>::Reloc_section*
2765 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
2766 {
2767   if (this->rel_dyn_ == NULL)
2768     {
2769       gold_assert(layout != NULL);
2770       this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
2771       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
2772                                       elfcpp::SHF_ALLOC, this->rel_dyn_, true,
2773                                       false, false, false);
2774     }
2775   return this->rel_dyn_;
2776 }
2777
2778 // Insn_template methods.
2779
2780 // Return byte size of an instruction template.
2781
2782 size_t
2783 Insn_template::size() const
2784 {
2785   switch (this->type())
2786     {
2787     case THUMB16_TYPE:
2788       return 2;
2789     case ARM_TYPE:
2790     case THUMB32_TYPE:
2791     case DATA_TYPE:
2792       return 4;
2793     default:
2794       gold_unreachable();
2795     }
2796 }
2797
2798 // Return alignment of an instruction template.
2799
2800 unsigned
2801 Insn_template::alignment() const
2802 {
2803   switch (this->type())
2804     {
2805     case THUMB16_TYPE:
2806     case THUMB32_TYPE:
2807       return 2;
2808     case ARM_TYPE:
2809     case DATA_TYPE:
2810       return 4;
2811     default:
2812       gold_unreachable();
2813     }
2814 }
2815
2816 // Stub_template methods.
2817
2818 Stub_template::Stub_template(
2819     Stub_type type, const Insn_template* insns,
2820      size_t insn_count)
2821   : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
2822     entry_in_thumb_mode_(false), relocs_()
2823 {
2824   off_t offset = 0;
2825
2826   // Compute byte size and alignment of stub template.
2827   for (size_t i = 0; i < insn_count; i++)
2828     {
2829       unsigned insn_alignment = insns[i].alignment();
2830       size_t insn_size = insns[i].size();
2831       gold_assert((offset & (insn_alignment - 1)) == 0);
2832       this->alignment_ = std::max(this->alignment_, insn_alignment);
2833       switch (insns[i].type())
2834         {
2835         case Insn_template::THUMB16_TYPE:
2836           if (i == 0)
2837             this->entry_in_thumb_mode_ = true;
2838           break;
2839
2840         case Insn_template::THUMB32_TYPE:
2841           if (insns[i].r_type() != elfcpp::R_ARM_NONE)
2842             this->relocs_.push_back(Reloc(i, offset));
2843           if (i == 0)
2844             this->entry_in_thumb_mode_ = true;
2845           break;
2846
2847         case Insn_template::ARM_TYPE:
2848           // Handle cases where the target is encoded within the
2849           // instruction.
2850           if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
2851             this->relocs_.push_back(Reloc(i, offset));
2852           break;
2853
2854         case Insn_template::DATA_TYPE:
2855           // Entry point cannot be data.
2856           gold_assert(i != 0);
2857           this->relocs_.push_back(Reloc(i, offset));
2858           break;
2859
2860         default:
2861           gold_unreachable();
2862         }
2863       offset += insn_size; 
2864     }
2865   this->size_ = offset;
2866 }
2867
2868 // Stub methods.
2869
2870 // Template to implement do_write for a specific target endianity.
2871
2872 template<bool big_endian>
2873 void inline
2874 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
2875 {
2876   const Stub_template* stub_template = this->stub_template();
2877   const Insn_template* insns = stub_template->insns();
2878
2879   // FIXME:  We do not handle BE8 encoding yet.
2880   unsigned char* pov = view;
2881   for (size_t i = 0; i < stub_template->insn_count(); i++)
2882     {
2883       switch (insns[i].type())
2884         {
2885         case Insn_template::THUMB16_TYPE:
2886           elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2887           break;
2888         case Insn_template::THUMB16_SPECIAL_TYPE:
2889           elfcpp::Swap<16, big_endian>::writeval(
2890               pov,
2891               this->thumb16_special(i));
2892           break;
2893         case Insn_template::THUMB32_TYPE:
2894           {
2895             uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2896             uint32_t lo = insns[i].data() & 0xffff;
2897             elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2898             elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2899           }
2900           break;
2901         case Insn_template::ARM_TYPE:
2902         case Insn_template::DATA_TYPE:
2903           elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2904           break;
2905         default:
2906           gold_unreachable();
2907         }
2908       pov += insns[i].size();
2909     }
2910   gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2911
2912
2913 // Reloc_stub::Key methods.
2914
2915 // Dump a Key as a string for debugging.
2916
2917 std::string
2918 Reloc_stub::Key::name() const
2919 {
2920   if (this->r_sym_ == invalid_index)
2921     {
2922       // Global symbol key name
2923       // <stub-type>:<symbol name>:<addend>.
2924       const std::string sym_name = this->u_.symbol->name();
2925       // We need to print two hex number and two colons.  So just add 100 bytes
2926       // to the symbol name size.
2927       size_t len = sym_name.size() + 100;
2928       char* buffer = new char[len];
2929       int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
2930                        sym_name.c_str(), this->addend_);
2931       gold_assert(c > 0 && c < static_cast<int>(len));
2932       delete[] buffer;
2933       return std::string(buffer);
2934     }
2935   else
2936     {
2937       // local symbol key name
2938       // <stub-type>:<object>:<r_sym>:<addend>.
2939       const size_t len = 200;
2940       char buffer[len];
2941       int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
2942                        this->u_.relobj, this->r_sym_, this->addend_);
2943       gold_assert(c > 0 && c < static_cast<int>(len));
2944       return std::string(buffer);
2945     }
2946 }
2947
2948 // Reloc_stub methods.
2949
2950 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2951 // LOCATION to DESTINATION.
2952 // This code is based on the arm_type_of_stub function in
2953 // bfd/elf32-arm.c.  We have changed the interface a liitle to keep the Stub
2954 // class simple.
2955
2956 Stub_type
2957 Reloc_stub::stub_type_for_reloc(
2958    unsigned int r_type,
2959    Arm_address location,
2960    Arm_address destination,
2961    bool target_is_thumb)
2962 {
2963   Stub_type stub_type = arm_stub_none;
2964
2965   // This is a bit ugly but we want to avoid using a templated class for
2966   // big and little endianities.
2967   bool may_use_blx;
2968   bool should_force_pic_veneer;
2969   bool thumb2;
2970   bool thumb_only;
2971   if (parameters->target().is_big_endian())
2972     {
2973       const Target_arm<true>* big_endian_target =
2974         Target_arm<true>::default_target();
2975       may_use_blx = big_endian_target->may_use_blx();
2976       should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
2977       thumb2 = big_endian_target->using_thumb2();
2978       thumb_only = big_endian_target->using_thumb_only();
2979     }
2980   else
2981     {
2982       const Target_arm<false>* little_endian_target =
2983         Target_arm<false>::default_target();
2984       may_use_blx = little_endian_target->may_use_blx();
2985       should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
2986       thumb2 = little_endian_target->using_thumb2();
2987       thumb_only = little_endian_target->using_thumb_only();
2988     }
2989
2990   int64_t branch_offset = (int64_t)destination - location;
2991
2992   if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
2993     {
2994       // Handle cases where:
2995       // - this call goes too far (different Thumb/Thumb2 max
2996       //   distance)
2997       // - it's a Thumb->Arm call and blx is not available, or it's a
2998       //   Thumb->Arm branch (not bl). A stub is needed in this case.
2999       if ((!thumb2
3000             && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
3001                 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
3002           || (thumb2
3003               && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
3004                   || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
3005           || ((!target_is_thumb)
3006               && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
3007                   || (r_type == elfcpp::R_ARM_THM_JUMP24))))
3008         {
3009           if (target_is_thumb)
3010             {
3011               // Thumb to thumb.
3012               if (!thumb_only)
3013                 {
3014                   stub_type = (parameters->options().shared()
3015                                || should_force_pic_veneer)
3016                     // PIC stubs.
3017                     ? ((may_use_blx
3018                         && (r_type == elfcpp::R_ARM_THM_CALL))
3019                        // V5T and above. Stub starts with ARM code, so
3020                        // we must be able to switch mode before
3021                        // reaching it, which is only possible for 'bl'
3022                        // (ie R_ARM_THM_CALL relocation).
3023                        ? arm_stub_long_branch_any_thumb_pic
3024                        // On V4T, use Thumb code only.
3025                        : arm_stub_long_branch_v4t_thumb_thumb_pic)
3026
3027                     // non-PIC stubs.
3028                     : ((may_use_blx
3029                         && (r_type == elfcpp::R_ARM_THM_CALL))
3030                        ? arm_stub_long_branch_any_any // V5T and above.
3031                        : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
3032                 }
3033               else
3034                 {
3035                   stub_type = (parameters->options().shared()
3036                                || should_force_pic_veneer)
3037                     ? arm_stub_long_branch_thumb_only_pic       // PIC stub.
3038                     : arm_stub_long_branch_thumb_only;  // non-PIC stub.
3039                 }
3040             }
3041           else
3042             {
3043               // Thumb to arm.
3044              
3045               // FIXME: We should check that the input section is from an
3046               // object that has interwork enabled.
3047
3048               stub_type = (parameters->options().shared()
3049                            || should_force_pic_veneer)
3050                 // PIC stubs.
3051                 ? ((may_use_blx
3052                     && (r_type == elfcpp::R_ARM_THM_CALL))
3053                    ? arm_stub_long_branch_any_arm_pic   // V5T and above.
3054                    : arm_stub_long_branch_v4t_thumb_arm_pic)    // V4T.
3055
3056                 // non-PIC stubs.
3057                 : ((may_use_blx
3058                     && (r_type == elfcpp::R_ARM_THM_CALL))
3059                    ? arm_stub_long_branch_any_any       // V5T and above.
3060                    : arm_stub_long_branch_v4t_thumb_arm);       // V4T.
3061
3062               // Handle v4t short branches.
3063               if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
3064                   && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
3065                   && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
3066                 stub_type = arm_stub_short_branch_v4t_thumb_arm;
3067             }
3068         }
3069     }
3070   else if (r_type == elfcpp::R_ARM_CALL
3071            || r_type == elfcpp::R_ARM_JUMP24
3072            || r_type == elfcpp::R_ARM_PLT32)
3073     {
3074       if (target_is_thumb)
3075         {
3076           // Arm to thumb.
3077
3078           // FIXME: We should check that the input section is from an
3079           // object that has interwork enabled.
3080
3081           // We have an extra 2-bytes reach because of
3082           // the mode change (bit 24 (H) of BLX encoding).
3083           if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
3084               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
3085               || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
3086               || (r_type == elfcpp::R_ARM_JUMP24)
3087               || (r_type == elfcpp::R_ARM_PLT32))
3088             {
3089               stub_type = (parameters->options().shared()
3090                            || should_force_pic_veneer)
3091                 // PIC stubs.
3092                 ? (may_use_blx
3093                    ? arm_stub_long_branch_any_thumb_pic// V5T and above.
3094                    : arm_stub_long_branch_v4t_arm_thumb_pic)    // V4T stub.
3095
3096                 // non-PIC stubs.
3097                 : (may_use_blx
3098                    ? arm_stub_long_branch_any_any       // V5T and above.
3099                    : arm_stub_long_branch_v4t_arm_thumb);       // V4T.
3100             }
3101         }
3102       else
3103         {
3104           // Arm to arm.
3105           if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
3106               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
3107             {
3108               stub_type = (parameters->options().shared()
3109                            || should_force_pic_veneer)
3110                 ? arm_stub_long_branch_any_arm_pic      // PIC stubs.
3111                 : arm_stub_long_branch_any_any;         /// non-PIC.
3112             }
3113         }
3114     }
3115
3116   return stub_type;
3117 }
3118
3119 // Cortex_a8_stub methods.
3120
3121 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
3122 // I is the position of the instruction template in the stub template.
3123
3124 uint16_t
3125 Cortex_a8_stub::do_thumb16_special(size_t i)
3126 {
3127   // The only use of this is to copy condition code from a conditional
3128   // branch being worked around to the corresponding conditional branch in
3129   // to the stub.
3130   gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
3131               && i == 0);
3132   uint16_t data = this->stub_template()->insns()[i].data();
3133   gold_assert((data & 0xff00U) == 0xd000U);
3134   data |= ((this->original_insn_ >> 22) & 0xf) << 8;
3135   return data;
3136 }
3137
3138 // Stub_factory methods.
3139
3140 Stub_factory::Stub_factory()
3141 {
3142   // The instruction template sequences are declared as static
3143   // objects and initialized first time the constructor runs.
3144  
3145   // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
3146   // to reach the stub if necessary.
3147   static const Insn_template elf32_arm_stub_long_branch_any_any[] =
3148     {
3149       Insn_template::arm_insn(0xe51ff004),      // ldr   pc, [pc, #-4]
3150       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3151                                                 // dcd   R_ARM_ABS32(X)
3152     };
3153   
3154   // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
3155   // available.
3156   static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
3157     {
3158       Insn_template::arm_insn(0xe59fc000),      // ldr   ip, [pc, #0]
3159       Insn_template::arm_insn(0xe12fff1c),      // bx    ip
3160       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3161                                                 // dcd   R_ARM_ABS32(X)
3162     };
3163   
3164   // Thumb -> Thumb long branch stub. Used on M-profile architectures.
3165   static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
3166     {
3167       Insn_template::thumb16_insn(0xb401),      // push {r0}
3168       Insn_template::thumb16_insn(0x4802),      // ldr  r0, [pc, #8]
3169       Insn_template::thumb16_insn(0x4684),      // mov  ip, r0
3170       Insn_template::thumb16_insn(0xbc01),      // pop  {r0}
3171       Insn_template::thumb16_insn(0x4760),      // bx   ip
3172       Insn_template::thumb16_insn(0xbf00),      // nop
3173       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3174                                                 // dcd  R_ARM_ABS32(X)
3175     };
3176   
3177   // V4T Thumb -> Thumb long branch stub. Using the stack is not
3178   // allowed.
3179   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
3180     {
3181       Insn_template::thumb16_insn(0x4778),      // bx   pc
3182       Insn_template::thumb16_insn(0x46c0),      // nop
3183       Insn_template::arm_insn(0xe59fc000),      // ldr  ip, [pc, #0]
3184       Insn_template::arm_insn(0xe12fff1c),      // bx   ip
3185       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3186                                                 // dcd  R_ARM_ABS32(X)
3187     };
3188   
3189   // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
3190   // available.
3191   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
3192     {
3193       Insn_template::thumb16_insn(0x4778),      // bx   pc
3194       Insn_template::thumb16_insn(0x46c0),      // nop
3195       Insn_template::arm_insn(0xe51ff004),      // ldr   pc, [pc, #-4]
3196       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
3197                                                 // dcd   R_ARM_ABS32(X)
3198     };
3199   
3200   // V4T Thumb -> ARM short branch stub. Shorter variant of the above
3201   // one, when the destination is close enough.
3202   static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
3203     {
3204       Insn_template::thumb16_insn(0x4778),              // bx   pc
3205       Insn_template::thumb16_insn(0x46c0),              // nop
3206       Insn_template::arm_rel_insn(0xea000000, -8),      // b    (X-8)
3207     };
3208   
3209   // ARM/Thumb -> ARM long branch stub, PIC.  On V5T and above, use
3210   // blx to reach the stub if necessary.
3211   static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
3212     {
3213       Insn_template::arm_insn(0xe59fc000),      // ldr   r12, [pc]
3214       Insn_template::arm_insn(0xe08ff00c),      // add   pc, pc, ip
3215       Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3216                                                 // dcd   R_ARM_REL32(X-4)
3217     };
3218   
3219   // ARM/Thumb -> Thumb long branch stub, PIC.  On V5T and above, use
3220   // blx to reach the stub if necessary.  We can not add into pc;
3221   // it is not guaranteed to mode switch (different in ARMv6 and
3222   // ARMv7).
3223   static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
3224     {
3225       Insn_template::arm_insn(0xe59fc004),      // ldr   r12, [pc, #4]
3226       Insn_template::arm_insn(0xe08fc00c),      // add   ip, pc, ip
3227       Insn_template::arm_insn(0xe12fff1c),      // bx    ip
3228       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3229                                                 // dcd   R_ARM_REL32(X)
3230     };
3231   
3232   // V4T ARM -> ARM long branch stub, PIC.
3233   static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
3234     {
3235       Insn_template::arm_insn(0xe59fc004),      // ldr   ip, [pc, #4]
3236       Insn_template::arm_insn(0xe08fc00c),      // add   ip, pc, ip
3237       Insn_template::arm_insn(0xe12fff1c),      // bx    ip
3238       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3239                                                 // dcd   R_ARM_REL32(X)
3240     };
3241   
3242   // V4T Thumb -> ARM long branch stub, PIC.
3243   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
3244     {
3245       Insn_template::thumb16_insn(0x4778),      // bx   pc
3246       Insn_template::thumb16_insn(0x46c0),      // nop
3247       Insn_template::arm_insn(0xe59fc000),      // ldr  ip, [pc, #0]
3248       Insn_template::arm_insn(0xe08cf00f),      // add  pc, ip, pc
3249       Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
3250                                                 // dcd  R_ARM_REL32(X)
3251     };
3252   
3253   // Thumb -> Thumb long branch stub, PIC. Used on M-profile
3254   // architectures.
3255   static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
3256     {
3257       Insn_template::thumb16_insn(0xb401),      // push {r0}
3258       Insn_template::thumb16_insn(0x4802),      // ldr  r0, [pc, #8]
3259       Insn_template::thumb16_insn(0x46fc),      // mov  ip, pc
3260       Insn_template::thumb16_insn(0x4484),      // add  ip, r0
3261       Insn_template::thumb16_insn(0xbc01),      // pop  {r0}
3262       Insn_template::thumb16_insn(0x4760),      // bx   ip
3263       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
3264                                                 // dcd  R_ARM_REL32(X)
3265     };
3266   
3267   // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
3268   // allowed.
3269   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
3270     {
3271       Insn_template::thumb16_insn(0x4778),      // bx   pc
3272       Insn_template::thumb16_insn(0x46c0),      // nop
3273       Insn_template::arm_insn(0xe59fc004),      // ldr  ip, [pc, #4]
3274       Insn_template::arm_insn(0xe08fc00c),      // add   ip, pc, ip
3275       Insn_template::arm_insn(0xe12fff1c),      // bx   ip
3276       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
3277                                                 // dcd  R_ARM_REL32(X)
3278     };
3279   
3280   // Cortex-A8 erratum-workaround stubs.
3281   
3282   // Stub used for conditional branches (which may be beyond +/-1MB away,
3283   // so we can't use a conditional branch to reach this stub).
3284   
3285   // original code:
3286   //
3287   //    b<cond> X
3288   // after:
3289   //
3290   static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
3291     {
3292       Insn_template::thumb16_bcond_insn(0xd001),        //      b<cond>.n true
3293       Insn_template::thumb32_b_insn(0xf000b800, -4),    //      b.w after
3294       Insn_template::thumb32_b_insn(0xf000b800, -4)     // true:
3295                                                         //      b.w X
3296     };
3297   
3298   // Stub used for b.w and bl.w instructions.
3299   
3300   static const Insn_template elf32_arm_stub_a8_veneer_b[] =
3301     {
3302       Insn_template::thumb32_b_insn(0xf000b800, -4)     // b.w dest
3303     };
3304   
3305   static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
3306     {
3307       Insn_template::thumb32_b_insn(0xf000b800, -4)     // b.w dest
3308     };
3309   
3310   // Stub used for Thumb-2 blx.w instructions.  We modified the original blx.w
3311   // instruction (which switches to ARM mode) to point to this stub.  Jump to
3312   // the real destination using an ARM-mode branch.
3313   static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
3314     {
3315       Insn_template::arm_rel_insn(0xea000000, -8)       // b dest
3316     };
3317
3318   // Fill in the stub template look-up table.  Stub templates are constructed
3319   // per instance of Stub_factory for fast look-up without locking
3320   // in a thread-enabled environment.
3321
3322   this->stub_templates_[arm_stub_none] =
3323     new Stub_template(arm_stub_none, NULL, 0);
3324
3325 #define DEF_STUB(x)     \
3326   do \
3327     { \
3328       size_t array_size \
3329         = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
3330       Stub_type type = arm_stub_##x; \
3331       this->stub_templates_[type] = \
3332         new Stub_template(type, elf32_arm_stub_##x, array_size); \
3333     } \
3334   while (0);
3335
3336   DEF_STUBS
3337 #undef DEF_STUB
3338 }
3339
3340 // Stub_table methods.
3341
3342 // Add a STUB with using KEY.  Caller is reponsible for avoid adding
3343 // if already a STUB with the same key has been added. 
3344
3345 template<bool big_endian>
3346 void
3347 Stub_table<big_endian>::add_reloc_stub(
3348     Reloc_stub* stub,
3349     const Reloc_stub::Key& key)
3350 {
3351   const Stub_template* stub_template = stub->stub_template();
3352   gold_assert(stub_template->type() == key.stub_type());
3353   this->reloc_stubs_[key] = stub;
3354   if (this->addralign_ < stub_template->alignment())
3355     this->addralign_ = stub_template->alignment();
3356   this->has_been_changed_ = true;
3357 }
3358
3359 template<bool big_endian>
3360 void
3361 Stub_table<big_endian>::relocate_stubs(
3362     const Relocate_info<32, big_endian>* relinfo,
3363     Target_arm<big_endian>* arm_target,
3364     Output_section* output_section,
3365     unsigned char* view,
3366     Arm_address address,
3367     section_size_type view_size)
3368 {
3369   // If we are passed a view bigger than the stub table's.  we need to
3370   // adjust the view.
3371   gold_assert(address == this->address()
3372               && (view_size
3373                   == static_cast<section_size_type>(this->data_size())));
3374
3375   for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3376       p != this->reloc_stubs_.end();
3377       ++p)
3378     {
3379       Reloc_stub* stub = p->second;
3380       const Stub_template* stub_template = stub->stub_template();
3381       if (stub_template->reloc_count() != 0)
3382         {
3383           // Adjust view to cover the stub only.
3384           section_size_type offset = stub->offset();
3385           section_size_type stub_size = stub_template->size();
3386           gold_assert(offset + stub_size <= view_size);
3387
3388           arm_target->relocate_stub(stub, relinfo, output_section,
3389                                     view + offset, address + offset,
3390                                     stub_size);
3391         }
3392     }
3393 }
3394
3395 // Reset address and file offset.
3396
3397 template<bool big_endian>
3398 void
3399 Stub_table<big_endian>::do_reset_address_and_file_offset()
3400 {
3401   off_t off = 0;
3402   uint64_t max_addralign = 1;
3403   for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3404       p != this->reloc_stubs_.end();
3405       ++p)
3406     {
3407       Reloc_stub* stub = p->second;
3408       const Stub_template* stub_template = stub->stub_template();
3409       uint64_t stub_addralign = stub_template->alignment();
3410       max_addralign = std::max(max_addralign, stub_addralign);
3411       off = align_address(off, stub_addralign);
3412       stub->set_offset(off);
3413       stub->reset_destination_address();
3414       off += stub_template->size();
3415     }
3416
3417   this->addralign_ = max_addralign;
3418   this->set_current_data_size_for_child(off);
3419 }
3420
3421 // Write out the stubs to file.
3422
3423 template<bool big_endian>
3424 void
3425 Stub_table<big_endian>::do_write(Output_file* of)
3426 {
3427   off_t offset = this->offset();
3428   const section_size_type oview_size =
3429     convert_to_section_size_type(this->data_size());
3430   unsigned char* const oview = of->get_output_view(offset, oview_size);
3431
3432   for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
3433       p != this->reloc_stubs_.end();
3434       ++p)
3435     {
3436       Reloc_stub* stub = p->second;
3437       Arm_address address = this->address() + stub->offset();
3438       gold_assert(address
3439                   == align_address(address,
3440                                    stub->stub_template()->alignment()));
3441       stub->write(oview + stub->offset(), stub->stub_template()->size(),
3442                   big_endian);
3443     } 
3444   of->write_output_view(this->offset(), oview_size, oview);
3445 }
3446
3447 // Arm_input_section methods.
3448
3449 // Initialize an Arm_input_section.
3450
3451 template<bool big_endian>
3452 void
3453 Arm_input_section<big_endian>::init()
3454 {
3455   Relobj* relobj = this->relobj();
3456   unsigned int shndx = this->shndx();
3457
3458   // Cache these to speed up size and alignment queries.  It is too slow
3459   // to call section_addraglin and section_size every time.
3460   this->original_addralign_ = relobj->section_addralign(shndx);
3461   this->original_size_ = relobj->section_size(shndx);
3462
3463   // We want to make this look like the original input section after
3464   // output sections are finalized.
3465   Output_section* os = relobj->output_section(shndx);
3466   off_t offset = relobj->output_section_offset(shndx);
3467   gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
3468   this->set_address(os->address() + offset);
3469   this->set_file_offset(os->offset() + offset);
3470
3471   this->set_current_data_size(this->original_size_);
3472   this->finalize_data_size();
3473 }
3474
3475 template<bool big_endian>
3476 void
3477 Arm_input_section<big_endian>::do_write(Output_file* of)
3478 {
3479   // We have to write out the original section content.
3480   section_size_type section_size;
3481   const unsigned char* section_contents =
3482     this->relobj()->section_contents(this->shndx(), &section_size, false); 
3483   of->write(this->offset(), section_contents, section_size); 
3484
3485   // If this owns a stub table and it is not empty, write it.
3486   if (this->is_stub_table_owner() && !this->stub_table_->empty())
3487     this->stub_table_->write(of);
3488 }
3489
3490 // Finalize data size.
3491
3492 template<bool big_endian>
3493 void
3494 Arm_input_section<big_endian>::set_final_data_size()
3495 {
3496   // If this owns a stub table, finalize its data size as well.
3497   if (this->is_stub_table_owner())
3498     {
3499       uint64_t address = this->address();
3500
3501       // The stub table comes after the original section contents.
3502       address += this->original_size_;
3503       address = align_address(address, this->stub_table_->addralign());
3504       off_t offset = this->offset() + (address - this->address());
3505       this->stub_table_->set_address_and_file_offset(address, offset);
3506       address += this->stub_table_->data_size();
3507       gold_assert(address == this->address() + this->current_data_size());
3508     }
3509
3510   this->set_data_size(this->current_data_size());
3511 }
3512
3513 // Reset address and file offset.
3514
3515 template<bool big_endian>
3516 void
3517 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
3518 {
3519   // Size of the original input section contents.
3520   off_t off = convert_types<off_t, uint64_t>(this->original_size_);
3521
3522   // If this is a stub table owner, account for the stub table size.
3523   if (this->is_stub_table_owner())
3524     {
3525       Stub_table<big_endian>* stub_table = this->stub_table_;
3526
3527       // Reset the stub table's address and file offset.  The
3528       // current data size for child will be updated after that.
3529       stub_table_->reset_address_and_file_offset();
3530       off = align_address(off, stub_table_->addralign());
3531       off += stub_table->current_data_size();
3532     }
3533
3534   this->set_current_data_size(off);
3535 }
3536
3537 // Arm_output_section methods.
3538
3539 // Create a stub group for input sections from BEGIN to END.  OWNER
3540 // points to the input section to be the owner a new stub table.
3541
3542 template<bool big_endian>
3543 void
3544 Arm_output_section<big_endian>::create_stub_group(
3545   Input_section_list::const_iterator begin,
3546   Input_section_list::const_iterator end,
3547   Input_section_list::const_iterator owner,
3548   Target_arm<big_endian>* target,
3549   std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
3550 {
3551   // Currently we convert ordinary input sections into relaxed sections only
3552   // at this point but we may want to support creating relaxed input section
3553   // very early.  So we check here to see if owner is already a relaxed
3554   // section.
3555   
3556   Arm_input_section<big_endian>* arm_input_section;
3557   if (owner->is_relaxed_input_section())
3558     {
3559       arm_input_section =
3560         Arm_input_section<big_endian>::as_arm_input_section(
3561           owner->relaxed_input_section());
3562     }
3563   else
3564     {
3565       gold_assert(owner->is_input_section());
3566       // Create a new relaxed input section.
3567       arm_input_section =
3568         target->new_arm_input_section(owner->relobj(), owner->shndx());
3569       new_relaxed_sections->push_back(arm_input_section);
3570     }
3571
3572   // Create a stub table.
3573   Stub_table<big_endian>* stub_table =
3574     target->new_stub_table(arm_input_section);
3575
3576   arm_input_section->set_stub_table(stub_table);
3577   
3578   Input_section_list::const_iterator p = begin;
3579   Input_section_list::const_iterator prev_p;
3580
3581   // Look for input sections or relaxed input sections in [begin ... end].
3582   do
3583     {
3584       if (p->is_input_section() || p->is_relaxed_input_section())
3585         {
3586           // The stub table information for input sections live
3587           // in their objects.
3588           Arm_relobj<big_endian>* arm_relobj =
3589             Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
3590           arm_relobj->set_stub_table(p->shndx(), stub_table);
3591         }
3592       prev_p = p++;
3593     }
3594   while (prev_p != end);
3595 }
3596
3597 // Group input sections for stub generation.  GROUP_SIZE is roughly the limit
3598 // of stub groups.  We grow a stub group by adding input section until the
3599 // size is just below GROUP_SIZE.  The last input section will be converted
3600 // into a stub table.  If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3601 // input section after the stub table, effectively double the group size.
3602 // 
3603 // This is similar to the group_sections() function in elf32-arm.c but is
3604 // implemented differently.
3605
3606 template<bool big_endian>
3607 void
3608 Arm_output_section<big_endian>::group_sections(
3609     section_size_type group_size,
3610     bool stubs_always_after_branch,
3611     Target_arm<big_endian>* target)
3612 {
3613   // We only care about sections containing code.
3614   if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
3615     return;
3616
3617   // States for grouping.
3618   typedef enum
3619   {
3620     // No group is being built.
3621     NO_GROUP,
3622     // A group is being built but the stub table is not found yet.
3623     // We keep group a stub group until the size is just under GROUP_SIZE.
3624     // The last input section in the group will be used as the stub table.
3625     FINDING_STUB_SECTION,
3626     // A group is being built and we have already found a stub table.
3627     // We enter this state to grow a stub group by adding input section
3628     // after the stub table.  This effectively doubles the group size.
3629     HAS_STUB_SECTION
3630   } State;
3631
3632   // Any newly created relaxed sections are stored here.
3633   std::vector<Output_relaxed_input_section*> new_relaxed_sections;
3634
3635   State state = NO_GROUP;
3636   section_size_type off = 0;
3637   section_size_type group_begin_offset = 0;
3638   section_size_type group_end_offset = 0;
3639   section_size_type stub_table_end_offset = 0;
3640   Input_section_list::const_iterator group_begin =
3641     this->input_sections().end();
3642   Input_section_list::const_iterator stub_table =
3643     this->input_sections().end();
3644   Input_section_list::const_iterator group_end = this->input_sections().end();
3645   for (Input_section_list::const_iterator p = this->input_sections().begin();
3646        p != this->input_sections().end();
3647        ++p)
3648     {
3649       section_size_type section_begin_offset =
3650         align_address(off, p->addralign());
3651       section_size_type section_end_offset =
3652         section_begin_offset + p->data_size(); 
3653       
3654       // Check to see if we should group the previously seens sections.
3655       switch (state)
3656         {
3657         case NO_GROUP:
3658           break;
3659
3660         case FINDING_STUB_SECTION:
3661           // Adding this section makes the group larger than GROUP_SIZE.
3662           if (section_end_offset - group_begin_offset >= group_size)
3663             {
3664               if (stubs_always_after_branch)
3665                 {       
3666                   gold_assert(group_end != this->input_sections().end());
3667                   this->create_stub_group(group_begin, group_end, group_end,
3668                                           target, &new_relaxed_sections);
3669                   state = NO_GROUP;
3670                 }
3671               else
3672                 {
3673                   // But wait, there's more!  Input sections up to
3674                   // stub_group_size bytes after the stub table can be
3675                   // handled by it too.
3676                   state = HAS_STUB_SECTION;
3677                   stub_table = group_end;
3678                   stub_table_end_offset = group_end_offset;
3679                 }
3680             }
3681             break;
3682
3683         case HAS_STUB_SECTION:
3684           // Adding this section makes the post stub-section group larger
3685           // than GROUP_SIZE.
3686           if (section_end_offset - stub_table_end_offset >= group_size)
3687            {
3688              gold_assert(group_end != this->input_sections().end());
3689              this->create_stub_group(group_begin, group_end, stub_table,
3690                                      target, &new_relaxed_sections);
3691              state = NO_GROUP;
3692            }
3693            break;
3694
3695           default:
3696             gold_unreachable();
3697         }       
3698
3699       // If we see an input section and currently there is no group, start
3700       // a new one.  Skip any empty sections.
3701       if ((p->is_input_section() || p->is_relaxed_input_section())
3702           && (p->relobj()->section_size(p->shndx()) != 0))
3703         {
3704           if (state == NO_GROUP)
3705             {
3706               state = FINDING_STUB_SECTION;
3707               group_begin = p;
3708               group_begin_offset = section_begin_offset;
3709             }
3710
3711           // Keep track of the last input section seen.
3712           group_end = p;
3713           group_end_offset = section_end_offset;
3714         }
3715
3716       off = section_end_offset;
3717     }
3718
3719   // Create a stub group for any ungrouped sections.
3720   if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
3721     {
3722       gold_assert(group_end != this->input_sections().end());
3723       this->create_stub_group(group_begin, group_end,
3724                               (state == FINDING_STUB_SECTION
3725                                ? group_end
3726                                : stub_table),
3727                                target, &new_relaxed_sections);
3728     }
3729
3730   // Convert input section into relaxed input section in a batch.
3731   if (!new_relaxed_sections.empty())
3732     this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
3733
3734   // Update the section offsets
3735   for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
3736     {
3737       Arm_relobj<big_endian>* arm_relobj =
3738         Arm_relobj<big_endian>::as_arm_relobj(
3739           new_relaxed_sections[i]->relobj());
3740       unsigned int shndx = new_relaxed_sections[i]->shndx();
3741       // Tell Arm_relobj that this input section is converted.
3742       arm_relobj->convert_input_section_to_relaxed_section(shndx);
3743     }
3744 }
3745
3746 // Arm_relobj methods.
3747
3748 // Scan relocations for stub generation.
3749
3750 template<bool big_endian>
3751 void
3752 Arm_relobj<big_endian>::scan_sections_for_stubs(
3753     Target_arm<big_endian>* arm_target,
3754     const Symbol_table* symtab,
3755     const Layout* layout)
3756 {
3757   unsigned int shnum = this->shnum();
3758   const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
3759
3760   // Read the section headers.
3761   const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
3762                                                shnum * shdr_size,
3763                                                true, true);
3764
3765   // To speed up processing, we set up hash tables for fast lookup of
3766   // input offsets to output addresses.
3767   this->initialize_input_to_output_maps();
3768
3769   const Relobj::Output_sections& out_sections(this->output_sections());
3770
3771   Relocate_info<32, big_endian> relinfo;
3772   relinfo.symtab = symtab;
3773   relinfo.layout = layout;
3774   relinfo.object = this;
3775
3776   const unsigned char* p = pshdrs + shdr_size;
3777   for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
3778     {
3779       typename elfcpp::Shdr<32, big_endian> shdr(p);
3780
3781       unsigned int sh_type = shdr.get_sh_type();
3782       if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
3783         continue;
3784
3785       off_t sh_size = shdr.get_sh_size();
3786       if (sh_size == 0)
3787         continue;
3788
3789       unsigned int index = this->adjust_shndx(shdr.get_sh_info());
3790       if (index >= this->shnum())
3791         {
3792           // Ignore reloc section with bad info.  This error will be
3793           // reported in the final link.
3794           continue;
3795         }
3796
3797       Output_section* os = out_sections[index];
3798       if (os == NULL
3799           || symtab->is_section_folded(this, index))
3800         {
3801           // This relocation section is against a section which we
3802           // discarded or if the section is folded into another
3803           // section due to ICF.
3804           continue;
3805         }
3806       Arm_address output_offset = this->get_output_section_offset(index);
3807
3808       if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
3809         {
3810           // Ignore reloc section with unexpected symbol table.  The
3811           // error will be reported in the final link.
3812           continue;
3813         }
3814
3815       const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
3816                                                     sh_size, true, false);
3817
3818       unsigned int reloc_size;
3819       if (sh_type == elfcpp::SHT_REL)
3820         reloc_size = elfcpp::Elf_sizes<32>::rel_size;
3821       else
3822         reloc_size = elfcpp::Elf_sizes<32>::rela_size;
3823
3824       if (reloc_size != shdr.get_sh_entsize())
3825         {
3826           // Ignore reloc section with unexpected entsize.  The error
3827           // will be reported in the final link.
3828           continue;
3829         }
3830
3831       size_t reloc_count = sh_size / reloc_size;
3832       if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
3833         {
3834           // Ignore reloc section with uneven size.  The error will be
3835           // reported in the final link.
3836           continue;
3837         }
3838
3839       gold_assert(output_offset != invalid_address
3840                   || this->relocs_must_follow_section_writes());
3841
3842       // Get the section contents.  This does work for the case in which
3843       // we modify the contents of an input section.  We need to pass the
3844       // output view under such circumstances.
3845       section_size_type input_view_size = 0;
3846       const unsigned char* input_view =
3847         this->section_contents(index, &input_view_size, false);
3848
3849       relinfo.reloc_shndx = i;
3850       relinfo.data_shndx = index;
3851       arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
3852                                          reloc_count, os,
3853                                          output_offset == invalid_address,
3854                                          input_view,
3855                                          os->address(),
3856                                          input_view_size);
3857     }
3858
3859   // After we've done the relocations, we release the hash tables,
3860   // since we no longer need them.
3861   this->free_input_to_output_maps();
3862 }
3863
3864 // Count the local symbols.  The ARM backend needs to know if a symbol
3865 // is a THUMB function or not.  For global symbols, it is easy because
3866 // the Symbol object keeps the ELF symbol type.  For local symbol it is
3867 // harder because we cannot access this information.   So we override the
3868 // do_count_local_symbol in parent and scan local symbols to mark
3869 // THUMB functions.  This is not the most efficient way but I do not want to
3870 // slow down other ports by calling a per symbol targer hook inside
3871 // Sized_relobj<size, big_endian>::do_count_local_symbols. 
3872
3873 template<bool big_endian>
3874 void
3875 Arm_relobj<big_endian>::do_count_local_symbols(
3876     Stringpool_template<char>* pool,
3877     Stringpool_template<char>* dynpool)
3878 {
3879   // We need to fix-up the values of any local symbols whose type are
3880   // STT_ARM_TFUNC.
3881   
3882   // Ask parent to count the local symbols.
3883   Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
3884   const unsigned int loccount = this->local_symbol_count();
3885   if (loccount == 0)
3886     return;
3887
3888   // Intialize the thumb function bit-vector.
3889   std::vector<bool> empty_vector(loccount, false);
3890   this->local_symbol_is_thumb_function_.swap(empty_vector);
3891
3892   // Read the symbol table section header.
3893   const unsigned int symtab_shndx = this->symtab_shndx();
3894   elfcpp::Shdr<32, big_endian>
3895       symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
3896   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
3897
3898   // Read the local symbols.
3899   const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
3900   gold_assert(loccount == symtabshdr.get_sh_info());
3901   off_t locsize = loccount * sym_size;
3902   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
3903                                               locsize, true, true);
3904
3905   // Loop over the local symbols and mark any local symbols pointing
3906   // to THUMB functions.
3907
3908   // Skip the first dummy symbol.
3909   psyms += sym_size;
3910   typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
3911     this->local_values();
3912   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
3913     {
3914       elfcpp::Sym<32, big_endian> sym(psyms);
3915       elfcpp::STT st_type = sym.get_st_type();
3916       Symbol_value<32>& lv((*plocal_values)[i]);
3917       Arm_address input_value = lv.input_value();
3918
3919       if (st_type == elfcpp::STT_ARM_TFUNC
3920           || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
3921         {
3922           // This is a THUMB function.  Mark this and canonicalize the
3923           // symbol value by setting LSB.
3924           this->local_symbol_is_thumb_function_[i] = true;
3925           if ((input_value & 1) == 0)
3926             lv.set_input_value(input_value | 1);
3927         }
3928     }
3929 }
3930
3931 // Relocate sections.
3932 template<bool big_endian>
3933 void
3934 Arm_relobj<big_endian>::do_relocate_sections(
3935     const Symbol_table* symtab,
3936     const Layout* layout,
3937     const unsigned char* pshdrs,
3938     typename Sized_relobj<32, big_endian>::Views* pviews)
3939 {
3940   // Call parent to relocate sections.
3941   Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
3942                                                      pviews); 
3943
3944   // We do not generate stubs if doing a relocatable link.
3945   if (parameters->options().relocatable())
3946     return;
3947
3948   // Relocate stub tables.
3949   unsigned int shnum = this->shnum();
3950
3951   Target_arm<big_endian>* arm_target =
3952     Target_arm<big_endian>::default_target();
3953
3954   Relocate_info<32, big_endian> relinfo;
3955   relinfo.symtab = symtab;
3956   relinfo.layout = layout;
3957   relinfo.object = this;
3958
3959   for (unsigned int i = 1; i < shnum; ++i)
3960     {
3961       Arm_input_section<big_endian>* arm_input_section =
3962         arm_target->find_arm_input_section(this, i);
3963
3964       if (arm_input_section == NULL
3965           || !arm_input_section->is_stub_table_owner()
3966           || arm_input_section->stub_table()->empty())
3967         continue;
3968
3969       // We cannot discard a section if it owns a stub table.
3970       Output_section* os = this->output_section(i);
3971       gold_assert(os != NULL);
3972
3973       relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
3974       relinfo.reloc_shdr = NULL;
3975       relinfo.data_shndx = i;
3976       relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
3977
3978       gold_assert((*pviews)[i].view != NULL);
3979
3980       // We are passed the output section view.  Adjust it to cover the
3981       // stub table only.
3982       Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
3983       gold_assert((stub_table->address() >= (*pviews)[i].address)
3984                   && ((stub_table->address() + stub_table->data_size())
3985                       <= (*pviews)[i].address + (*pviews)[i].view_size));
3986
3987       off_t offset = stub_table->address() - (*pviews)[i].address;
3988       unsigned char* view = (*pviews)[i].view + offset;
3989       Arm_address address = stub_table->address();
3990       section_size_type view_size = stub_table->data_size();
3991  
3992       stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
3993                                  view_size);
3994     }
3995 }
3996
3997 // Helper functions for both Arm_relobj and Arm_dynobj to read ARM
3998 // ABI information.
3999
4000 template<bool big_endian>
4001 Attributes_section_data*
4002 read_arm_attributes_section(
4003     Object* object,
4004     Read_symbols_data *sd)
4005 {
4006   // Read the attributes section if there is one.
4007   // We read from the end because gas seems to put it near the end of
4008   // the section headers.
4009   const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
4010   const unsigned char *ps =
4011     sd->section_headers->data() + shdr_size * (object->shnum() - 1);
4012   for (unsigned int i = object->shnum(); i > 0; --i, ps -= shdr_size)
4013     {
4014       elfcpp::Shdr<32, big_endian> shdr(ps);
4015       if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
4016         {
4017           section_offset_type section_offset = shdr.get_sh_offset();
4018           section_size_type section_size =
4019             convert_to_section_size_type(shdr.get_sh_size());
4020           File_view* view = object->get_lasting_view(section_offset,
4021                                                      section_size, true, false);
4022           return new Attributes_section_data(view->data(), section_size);
4023         }
4024     }
4025   return NULL;
4026 }
4027
4028 // Read the symbol information.
4029
4030 template<bool big_endian>
4031 void
4032 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4033 {
4034   // Call parent class to read symbol information.
4035   Sized_relobj<32, big_endian>::do_read_symbols(sd);
4036
4037   // Read processor-specific flags in ELF file header.
4038   const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4039                                               elfcpp::Elf_sizes<32>::ehdr_size,
4040                                               true, false);
4041   elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4042   this->processor_specific_flags_ = ehdr.get_e_flags();
4043   this->attributes_section_data_ =
4044     read_arm_attributes_section<big_endian>(this, sd); 
4045 }
4046
4047 // Arm_dynobj methods.
4048
4049 // Read the symbol information.
4050
4051 template<bool big_endian>
4052 void
4053 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
4054 {
4055   // Call parent class to read symbol information.
4056   Sized_dynobj<32, big_endian>::do_read_symbols(sd);
4057
4058   // Read processor-specific flags in ELF file header.
4059   const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
4060                                               elfcpp::Elf_sizes<32>::ehdr_size,
4061                                               true, false);
4062   elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
4063   this->processor_specific_flags_ = ehdr.get_e_flags();
4064   this->attributes_section_data_ =
4065     read_arm_attributes_section<big_endian>(this, sd); 
4066 }
4067
4068 // Stub_addend_reader methods.
4069
4070 // Read the addend of a REL relocation of type R_TYPE at VIEW.
4071
4072 template<bool big_endian>
4073 elfcpp::Elf_types<32>::Elf_Swxword
4074 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
4075     unsigned int r_type,
4076     const unsigned char* view,
4077     const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
4078 {
4079   switch (r_type)
4080     {
4081     case elfcpp::R_ARM_CALL:
4082     case elfcpp::R_ARM_JUMP24:
4083     case elfcpp::R_ARM_PLT32:
4084       {
4085         typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4086         const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4087         Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4088         return utils::sign_extend<26>(val << 2);
4089       }
4090
4091     case elfcpp::R_ARM_THM_CALL:
4092     case elfcpp::R_ARM_THM_JUMP24:
4093     case elfcpp::R_ARM_THM_XPC22:
4094       {
4095         // Fetch the addend.  We use the Thumb-2 encoding (backwards
4096         // compatible with Thumb-1) involving the J1 and J2 bits.
4097         typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4098         const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4099         Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4100         Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4101
4102         uint32_t s = (upper_insn & (1 << 10)) >> 10;
4103         uint32_t upper = upper_insn & 0x3ff;
4104         uint32_t lower = lower_insn & 0x7ff;
4105         uint32_t j1 = (lower_insn & (1 << 13)) >> 13;
4106         uint32_t j2 = (lower_insn & (1 << 11)) >> 11;
4107         uint32_t i1 = j1 ^ s ? 0 : 1;
4108         uint32_t i2 = j2 ^ s ? 0 : 1;
4109
4110         return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
4111                                       | (upper << 12) | (lower << 1));
4112       }
4113
4114     case elfcpp::R_ARM_THM_JUMP19:
4115       {
4116         typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4117         const Valtype* wv = reinterpret_cast<const Valtype*>(view);
4118         Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4119         Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4120
4121         // Reconstruct the top three bits and squish the two 11 bit pieces
4122         // together.
4123         uint32_t S = (upper_insn & 0x0400) >> 10;
4124         uint32_t J1 = (lower_insn & 0x2000) >> 13;
4125         uint32_t J2 = (lower_insn & 0x0800) >> 11;
4126         uint32_t upper =
4127           (S << 8) | (J2 << 7) | (J1 << 6) | (upper_insn & 0x003f);
4128         uint32_t lower = (lower_insn & 0x07ff);
4129         return utils::sign_extend<23>((upper << 12) | (lower << 1));
4130       }
4131
4132     default:
4133       gold_unreachable();
4134     }
4135 }
4136
4137 // A class to handle the PLT data.
4138
4139 template<bool big_endian>
4140 class Output_data_plt_arm : public Output_section_data
4141 {
4142  public:
4143   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
4144     Reloc_section;
4145
4146   Output_data_plt_arm(Layout*, Output_data_space*);
4147
4148   // Add an entry to the PLT.
4149   void
4150   add_entry(Symbol* gsym);
4151
4152   // Return the .rel.plt section data.
4153   const Reloc_section*
4154   rel_plt() const
4155   { return this->rel_; }
4156
4157  protected:
4158   void
4159   do_adjust_output_section(Output_section* os);
4160
4161   // Write to a map file.
4162   void
4163   do_print_to_mapfile(Mapfile* mapfile) const
4164   { mapfile->print_output_data(this, _("** PLT")); }
4165
4166  private:
4167   // Template for the first PLT entry.
4168   static const uint32_t first_plt_entry[5];
4169
4170   // Template for subsequent PLT entries. 
4171   static const uint32_t plt_entry[3];
4172
4173   // Set the final size.
4174   void
4175   set_final_data_size()
4176   {
4177     this->set_data_size(sizeof(first_plt_entry)
4178                         + this->count_ * sizeof(plt_entry));
4179   }
4180
4181   // Write out the PLT data.
4182   void
4183   do_write(Output_file*);
4184
4185   // The reloc section.
4186   Reloc_section* rel_;
4187   // The .got.plt section.
4188   Output_data_space* got_plt_;
4189   // The number of PLT entries.
4190   unsigned int count_;
4191 };
4192
4193 // Create the PLT section.  The ordinary .got section is an argument,
4194 // since we need to refer to the start.  We also create our own .got
4195 // section just for PLT entries.
4196
4197 template<bool big_endian>
4198 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
4199                                                      Output_data_space* got_plt)
4200   : Output_section_data(4), got_plt_(got_plt), count_(0)
4201 {
4202   this->rel_ = new Reloc_section(false);
4203   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
4204                                   elfcpp::SHF_ALLOC, this->rel_, true, false,
4205                                   false, false);
4206 }
4207
4208 template<bool big_endian>
4209 void
4210 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
4211 {
4212   os->set_entsize(0);
4213 }
4214
4215 // Add an entry to the PLT.
4216
4217 template<bool big_endian>
4218 void
4219 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
4220 {
4221   gold_assert(!gsym->has_plt_offset());
4222
4223   // Note that when setting the PLT offset we skip the initial
4224   // reserved PLT entry.
4225   gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
4226                        + sizeof(first_plt_entry));
4227
4228   ++this->count_;
4229
4230   section_offset_type got_offset = this->got_plt_->current_data_size();
4231
4232   // Every PLT entry needs a GOT entry which points back to the PLT
4233   // entry (this will be changed by the dynamic linker, normally
4234   // lazily when the function is called).
4235   this->got_plt_->set_current_data_size(got_offset + 4);
4236
4237   // Every PLT entry needs a reloc.
4238   gsym->set_needs_dynsym_entry();
4239   this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
4240                          got_offset);
4241
4242   // Note that we don't need to save the symbol.  The contents of the
4243   // PLT are independent of which symbols are used.  The symbols only
4244   // appear in the relocations.
4245 }
4246
4247 // ARM PLTs.
4248 // FIXME:  This is not very flexible.  Right now this has only been tested
4249 // on armv5te.  If we are to support additional architecture features like
4250 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
4251
4252 // The first entry in the PLT.
4253 template<bool big_endian>
4254 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
4255 {
4256   0xe52de004,   // str   lr, [sp, #-4]!
4257   0xe59fe004,   // ldr   lr, [pc, #4]
4258   0xe08fe00e,   // add   lr, pc, lr 
4259   0xe5bef008,   // ldr   pc, [lr, #8]!
4260   0x00000000,   // &GOT[0] - .
4261 };
4262
4263 // Subsequent entries in the PLT.
4264
4265 template<bool big_endian>
4266 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
4267 {
4268   0xe28fc600,   // add   ip, pc, #0xNN00000
4269   0xe28cca00,   // add   ip, ip, #0xNN000
4270   0xe5bcf000,   // ldr   pc, [ip, #0xNNN]!
4271 };
4272
4273 // Write out the PLT.  This uses the hand-coded instructions above,
4274 // and adjusts them as needed.  This is all specified by the arm ELF
4275 // Processor Supplement.
4276
4277 template<bool big_endian>
4278 void
4279 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
4280 {
4281   const off_t offset = this->offset();
4282   const section_size_type oview_size =
4283     convert_to_section_size_type(this->data_size());
4284   unsigned char* const oview = of->get_output_view(offset, oview_size);
4285
4286   const off_t got_file_offset = this->got_plt_->offset();
4287   const section_size_type got_size =
4288     convert_to_section_size_type(this->got_plt_->data_size());
4289   unsigned char* const got_view = of->get_output_view(got_file_offset,
4290                                                       got_size);
4291   unsigned char* pov = oview;
4292
4293   Arm_address plt_address = this->address();
4294   Arm_address got_address = this->got_plt_->address();
4295
4296   // Write first PLT entry.  All but the last word are constants.
4297   const size_t num_first_plt_words = (sizeof(first_plt_entry)
4298                                       / sizeof(plt_entry[0]));
4299   for (size_t i = 0; i < num_first_plt_words - 1; i++)
4300     elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
4301   // Last word in first PLT entry is &GOT[0] - .
4302   elfcpp::Swap<32, big_endian>::writeval(pov + 16,
4303                                          got_address - (plt_address + 16));
4304   pov += sizeof(first_plt_entry);
4305
4306   unsigned char* got_pov = got_view;
4307
4308   memset(got_pov, 0, 12);
4309   got_pov += 12;
4310
4311   const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
4312   unsigned int plt_offset = sizeof(first_plt_entry);
4313   unsigned int plt_rel_offset = 0;
4314   unsigned int got_offset = 12;
4315   const unsigned int count = this->count_;
4316   for (unsigned int i = 0;
4317        i < count;
4318        ++i,
4319          pov += sizeof(plt_entry),
4320          got_pov += 4,
4321          plt_offset += sizeof(plt_entry),
4322          plt_rel_offset += rel_size,
4323          got_offset += 4)
4324     {
4325       // Set and adjust the PLT entry itself.
4326       int32_t offset = ((got_address + got_offset)
4327                          - (plt_address + plt_offset + 8));
4328
4329       gold_assert(offset >= 0 && offset < 0x0fffffff);
4330       uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
4331       elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
4332       uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
4333       elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
4334       uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
4335       elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
4336
4337       // Set the entry in the GOT.
4338       elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
4339     }
4340
4341   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4342   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4343
4344   of->write_output_view(offset, oview_size, oview);
4345   of->write_output_view(got_file_offset, got_size, got_view);
4346 }
4347
4348 // Create a PLT entry for a global symbol.
4349
4350 template<bool big_endian>
4351 void
4352 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
4353                                        Symbol* gsym)
4354 {
4355   if (gsym->has_plt_offset())
4356     return;
4357
4358   if (this->plt_ == NULL)
4359     {
4360       // Create the GOT sections first.
4361       this->got_section(symtab, layout);
4362
4363       this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
4364       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
4365                                       (elfcpp::SHF_ALLOC
4366                                        | elfcpp::SHF_EXECINSTR),
4367                                       this->plt_, false, false, false, false);
4368     }
4369   this->plt_->add_entry(gsym);
4370 }
4371
4372 // Report an unsupported relocation against a local symbol.
4373
4374 template<bool big_endian>
4375 void
4376 Target_arm<big_endian>::Scan::unsupported_reloc_local(
4377     Sized_relobj<32, big_endian>* object,
4378     unsigned int r_type)
4379 {
4380   gold_error(_("%s: unsupported reloc %u against local symbol"),
4381              object->name().c_str(), r_type);
4382 }
4383
4384 // We are about to emit a dynamic relocation of type R_TYPE.  If the
4385 // dynamic linker does not support it, issue an error.  The GNU linker
4386 // only issues a non-PIC error for an allocated read-only section.
4387 // Here we know the section is allocated, but we don't know that it is
4388 // read-only.  But we check for all the relocation types which the
4389 // glibc dynamic linker supports, so it seems appropriate to issue an
4390 // error even if the section is not read-only.
4391
4392 template<bool big_endian>
4393 void
4394 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
4395                                             unsigned int r_type)
4396 {
4397   switch (r_type)
4398     {
4399     // These are the relocation types supported by glibc for ARM.
4400     case elfcpp::R_ARM_RELATIVE:
4401     case elfcpp::R_ARM_COPY:
4402     case elfcpp::R_ARM_GLOB_DAT:
4403     case elfcpp::R_ARM_JUMP_SLOT:
4404     case elfcpp::R_ARM_ABS32:
4405     case elfcpp::R_ARM_ABS32_NOI:
4406     case elfcpp::R_ARM_PC24:
4407     // FIXME: The following 3 types are not supported by Android's dynamic
4408     // linker.
4409     case elfcpp::R_ARM_TLS_DTPMOD32:
4410     case elfcpp::R_ARM_TLS_DTPOFF32:
4411     case elfcpp::R_ARM_TLS_TPOFF32:
4412       return;
4413
4414     default:
4415       // This prevents us from issuing more than one error per reloc
4416       // section.  But we can still wind up issuing more than one
4417       // error per object file.
4418       if (this->issued_non_pic_error_)
4419         return;
4420       object->error(_("requires unsupported dynamic reloc; "
4421                       "recompile with -fPIC"));
4422       this->issued_non_pic_error_ = true;
4423       return;
4424
4425     case elfcpp::R_ARM_NONE:
4426       gold_unreachable();
4427     }
4428 }
4429
4430 // Scan a relocation for a local symbol.
4431 // FIXME: This only handles a subset of relocation types used by Android
4432 // on ARM v5te devices.
4433
4434 template<bool big_endian>
4435 inline void
4436 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
4437                                     Layout* layout,
4438                                     Target_arm* target,
4439                                     Sized_relobj<32, big_endian>* object,
4440                                     unsigned int data_shndx,
4441                                     Output_section* output_section,
4442                                     const elfcpp::Rel<32, big_endian>& reloc,
4443                                     unsigned int r_type,
4444                                     const elfcpp::Sym<32, big_endian>&)
4445 {
4446   r_type = get_real_reloc_type(r_type);
4447   switch (r_type)
4448     {
4449     case elfcpp::R_ARM_NONE:
4450       break;
4451
4452     case elfcpp::R_ARM_ABS32:
4453     case elfcpp::R_ARM_ABS32_NOI:
4454       // If building a shared library (or a position-independent
4455       // executable), we need to create a dynamic relocation for
4456       // this location. The relocation applied at link time will
4457       // apply the link-time value, so we flag the location with
4458       // an R_ARM_RELATIVE relocation so the dynamic loader can
4459       // relocate it easily.
4460       if (parameters->options().output_is_position_independent())
4461         {
4462           Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4463           unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4464           // If we are to add more other reloc types than R_ARM_ABS32,
4465           // we need to add check_non_pic(object, r_type) here.
4466           rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
4467                                       output_section, data_shndx,
4468                                       reloc.get_r_offset());
4469         }
4470       break;
4471
4472     case elfcpp::R_ARM_REL32:
4473     case elfcpp::R_ARM_THM_CALL:
4474     case elfcpp::R_ARM_CALL:
4475     case elfcpp::R_ARM_PREL31:
4476     case elfcpp::R_ARM_JUMP24:
4477     case elfcpp::R_ARM_PLT32:
4478     case elfcpp::R_ARM_THM_ABS5:
4479     case elfcpp::R_ARM_ABS8:
4480     case elfcpp::R_ARM_ABS12:
4481     case elfcpp::R_ARM_ABS16:
4482     case elfcpp::R_ARM_BASE_ABS:
4483     case elfcpp::R_ARM_MOVW_ABS_NC:
4484     case elfcpp::R_ARM_MOVT_ABS:
4485     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4486     case elfcpp::R_ARM_THM_MOVT_ABS:
4487     case elfcpp::R_ARM_MOVW_PREL_NC:
4488     case elfcpp::R_ARM_MOVT_PREL:
4489     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4490     case elfcpp::R_ARM_THM_MOVT_PREL:
4491       break;
4492
4493     case elfcpp::R_ARM_GOTOFF32:
4494       // We need a GOT section:
4495       target->got_section(symtab, layout);
4496       break;
4497
4498     case elfcpp::R_ARM_BASE_PREL:
4499       // FIXME: What about this?
4500       break;
4501
4502     case elfcpp::R_ARM_GOT_BREL:
4503     case elfcpp::R_ARM_GOT_PREL:
4504       {
4505         // The symbol requires a GOT entry.
4506         Output_data_got<32, big_endian>* got =
4507           target->got_section(symtab, layout);
4508         unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4509         if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
4510           {
4511             // If we are generating a shared object, we need to add a
4512             // dynamic RELATIVE relocation for this symbol's GOT entry.
4513             if (parameters->options().output_is_position_independent())
4514               {
4515                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4516                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
4517                 rel_dyn->add_local_relative(
4518                     object, r_sym, elfcpp::R_ARM_RELATIVE, got,
4519                     object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
4520               }
4521           }
4522       }
4523       break;
4524
4525     case elfcpp::R_ARM_TARGET1:
4526       // This should have been mapped to another type already.
4527       // Fall through.
4528     case elfcpp::R_ARM_COPY:
4529     case elfcpp::R_ARM_GLOB_DAT:
4530     case elfcpp::R_ARM_JUMP_SLOT:
4531     case elfcpp::R_ARM_RELATIVE:
4532       // These are relocations which should only be seen by the
4533       // dynamic linker, and should never be seen here.
4534       gold_error(_("%s: unexpected reloc %u in object file"),
4535                  object->name().c_str(), r_type);
4536       break;
4537
4538     default:
4539       unsupported_reloc_local(object, r_type);
4540       break;
4541     }
4542 }
4543
4544 // Report an unsupported relocation against a global symbol.
4545
4546 template<bool big_endian>
4547 void
4548 Target_arm<big_endian>::Scan::unsupported_reloc_global(
4549     Sized_relobj<32, big_endian>* object,
4550     unsigned int r_type,
4551     Symbol* gsym)
4552 {
4553   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4554              object->name().c_str(), r_type, gsym->demangled_name().c_str());
4555 }
4556
4557 // Scan a relocation for a global symbol.
4558 // FIXME: This only handles a subset of relocation types used by Android
4559 // on ARM v5te devices.
4560
4561 template<bool big_endian>
4562 inline void
4563 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
4564                                      Layout* layout,
4565                                      Target_arm* target,
4566                                      Sized_relobj<32, big_endian>* object,
4567                                      unsigned int data_shndx,
4568                                      Output_section* output_section,
4569                                      const elfcpp::Rel<32, big_endian>& reloc,
4570                                      unsigned int r_type,
4571                                      Symbol* gsym)
4572 {
4573   r_type = get_real_reloc_type(r_type);
4574   switch (r_type)
4575     {
4576     case elfcpp::R_ARM_NONE:
4577       break;
4578
4579     case elfcpp::R_ARM_ABS32:
4580     case elfcpp::R_ARM_ABS32_NOI:
4581       {
4582         // Make a dynamic relocation if necessary.
4583         if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
4584           {
4585             if (target->may_need_copy_reloc(gsym))
4586               {
4587                 target->copy_reloc(symtab, layout, object,
4588                                    data_shndx, output_section, gsym, reloc);
4589               }
4590             else if (gsym->can_use_relative_reloc(false))
4591               {
4592                 // If we are to add more other reloc types than R_ARM_ABS32,
4593                 // we need to add check_non_pic(object, r_type) here.
4594                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4595                 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
4596                                              output_section, object,
4597                                              data_shndx, reloc.get_r_offset());
4598               }
4599             else
4600               {
4601                 // If we are to add more other reloc types than R_ARM_ABS32,
4602                 // we need to add check_non_pic(object, r_type) here.
4603                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4604                 rel_dyn->add_global(gsym, r_type, output_section, object,
4605                                     data_shndx, reloc.get_r_offset());
4606               }
4607           }
4608       }
4609       break;
4610
4611     case elfcpp::R_ARM_MOVW_ABS_NC:
4612     case elfcpp::R_ARM_MOVT_ABS:
4613     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4614     case elfcpp::R_ARM_THM_MOVT_ABS:
4615     case elfcpp::R_ARM_MOVW_PREL_NC:
4616     case elfcpp::R_ARM_MOVT_PREL:
4617     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4618     case elfcpp::R_ARM_THM_MOVT_PREL:
4619       break;
4620
4621     case elfcpp::R_ARM_THM_ABS5:
4622     case elfcpp::R_ARM_ABS8:
4623     case elfcpp::R_ARM_ABS12:
4624     case elfcpp::R_ARM_ABS16:
4625     case elfcpp::R_ARM_BASE_ABS:
4626       {
4627         // No dynamic relocs of this kinds.
4628         // Report the error in case of PIC.
4629         int flags = Symbol::NON_PIC_REF;
4630         if (gsym->type() == elfcpp::STT_FUNC
4631             || gsym->type() == elfcpp::STT_ARM_TFUNC)
4632           flags |= Symbol::FUNCTION_CALL;
4633         if (gsym->needs_dynamic_reloc(flags))
4634           check_non_pic(object, r_type);
4635       }
4636       break;
4637
4638     case elfcpp::R_ARM_REL32:
4639     case elfcpp::R_ARM_PREL31:
4640       {
4641         // Make a dynamic relocation if necessary.
4642         int flags = Symbol::NON_PIC_REF;
4643         if (gsym->needs_dynamic_reloc(flags))
4644           {
4645             if (target->may_need_copy_reloc(gsym))
4646               {
4647                 target->copy_reloc(symtab, layout, object,
4648                                    data_shndx, output_section, gsym, reloc);
4649               }
4650             else
4651               {
4652                 check_non_pic(object, r_type);
4653                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4654                 rel_dyn->add_global(gsym, r_type, output_section, object,
4655                                     data_shndx, reloc.get_r_offset());
4656               }
4657           }
4658       }
4659       break;
4660
4661     case elfcpp::R_ARM_JUMP24:
4662     case elfcpp::R_ARM_THM_JUMP24:
4663     case elfcpp::R_ARM_CALL:
4664     case elfcpp::R_ARM_THM_CALL:
4665
4666       if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
4667         target->make_plt_entry(symtab, layout, gsym);
4668       else
4669         {
4670            // Check to see if this is a function that would need a PLT
4671            // but does not get one because the function symbol is untyped.
4672            // This happens in assembly code missing a proper .type directive.
4673           if ((!gsym->is_undefined() || parameters->options().shared())
4674               && !parameters->doing_static_link()
4675               && gsym->type() == elfcpp::STT_NOTYPE
4676               && (gsym->is_from_dynobj()
4677                   || gsym->is_undefined()
4678                   || gsym->is_preemptible()))
4679             gold_error(_("%s is not a function."),
4680                        gsym->demangled_name().c_str());
4681         }
4682       break;
4683
4684     case elfcpp::R_ARM_PLT32:
4685       // If the symbol is fully resolved, this is just a relative
4686       // local reloc.  Otherwise we need a PLT entry.
4687       if (gsym->final_value_is_known())
4688         break;
4689       // If building a shared library, we can also skip the PLT entry
4690       // if the symbol is defined in the output file and is protected
4691       // or hidden.
4692       if (gsym->is_defined()
4693           && !gsym->is_from_dynobj()
4694           && !gsym->is_preemptible())
4695         break;
4696       target->make_plt_entry(symtab, layout, gsym);
4697       break;
4698
4699     case elfcpp::R_ARM_GOTOFF32:
4700       // We need a GOT section.
4701       target->got_section(symtab, layout);
4702       break;
4703
4704     case elfcpp::R_ARM_BASE_PREL:
4705       // FIXME: What about this?
4706       break;
4707       
4708     case elfcpp::R_ARM_GOT_BREL:
4709     case elfcpp::R_ARM_GOT_PREL:
4710       {
4711         // The symbol requires a GOT entry.
4712         Output_data_got<32, big_endian>* got =
4713           target->got_section(symtab, layout);
4714         if (gsym->final_value_is_known())
4715           got->add_global(gsym, GOT_TYPE_STANDARD);
4716         else
4717           {
4718             // If this symbol is not fully resolved, we need to add a
4719             // GOT entry with a dynamic relocation.
4720             Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4721             if (gsym->is_from_dynobj()
4722                 || gsym->is_undefined()
4723                 || gsym->is_preemptible())
4724               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
4725                                        rel_dyn, elfcpp::R_ARM_GLOB_DAT);
4726             else
4727               {
4728                 if (got->add_global(gsym, GOT_TYPE_STANDARD))
4729                   rel_dyn->add_global_relative(
4730                       gsym, elfcpp::R_ARM_RELATIVE, got,
4731                       gsym->got_offset(GOT_TYPE_STANDARD));
4732               }
4733           }
4734       }
4735       break;
4736
4737     case elfcpp::R_ARM_TARGET1:
4738       // This should have been mapped to another type already.
4739       // Fall through.
4740     case elfcpp::R_ARM_COPY:
4741     case elfcpp::R_ARM_GLOB_DAT:
4742     case elfcpp::R_ARM_JUMP_SLOT:
4743     case elfcpp::R_ARM_RELATIVE:
4744       // These are relocations which should only be seen by the
4745       // dynamic linker, and should never be seen here.
4746       gold_error(_("%s: unexpected reloc %u in object file"),
4747                  object->name().c_str(), r_type);
4748       break;
4749
4750     default:
4751       unsupported_reloc_global(object, r_type, gsym);
4752       break;
4753     }
4754 }
4755
4756 // Process relocations for gc.
4757
4758 template<bool big_endian>
4759 void
4760 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
4761                                           Layout* layout,
4762                                           Sized_relobj<32, big_endian>* object,
4763                                           unsigned int data_shndx,
4764                                           unsigned int,
4765                                           const unsigned char* prelocs,
4766                                           size_t reloc_count,
4767                                           Output_section* output_section,
4768                                           bool needs_special_offset_handling,
4769                                           size_t local_symbol_count,
4770                                           const unsigned char* plocal_symbols)
4771 {
4772   typedef Target_arm<big_endian> Arm;
4773   typedef typename Target_arm<big_endian>::Scan Scan;
4774
4775   gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
4776     symtab,
4777     layout,
4778     this,
4779     object,
4780     data_shndx,
4781     prelocs,
4782     reloc_count,
4783     output_section,
4784     needs_special_offset_handling,
4785     local_symbol_count,
4786     plocal_symbols);
4787 }
4788
4789 // Scan relocations for a section.
4790
4791 template<bool big_endian>
4792 void
4793 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
4794                                     Layout* layout,
4795                                     Sized_relobj<32, big_endian>* object,
4796                                     unsigned int data_shndx,
4797                                     unsigned int sh_type,
4798                                     const unsigned char* prelocs,
4799                                     size_t reloc_count,
4800                                     Output_section* output_section,
4801                                     bool needs_special_offset_handling,
4802                                     size_t local_symbol_count,
4803                                     const unsigned char* plocal_symbols)
4804 {
4805   typedef typename Target_arm<big_endian>::Scan Scan;
4806   if (sh_type == elfcpp::SHT_RELA)
4807     {
4808       gold_error(_("%s: unsupported RELA reloc section"),
4809                  object->name().c_str());
4810       return;
4811     }
4812
4813   gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
4814     symtab,
4815     layout,
4816     this,
4817     object,
4818     data_shndx,
4819     prelocs,
4820     reloc_count,
4821     output_section,
4822     needs_special_offset_handling,
4823     local_symbol_count,
4824     plocal_symbols);
4825 }
4826
4827 // Finalize the sections.
4828
4829 template<bool big_endian>
4830 void
4831 Target_arm<big_endian>::do_finalize_sections(
4832     Layout* layout,
4833     const Input_objects* input_objects,
4834     Symbol_table* symtab)
4835 {
4836   // Merge processor-specific flags.
4837   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4838        p != input_objects->relobj_end();
4839        ++p)
4840     {
4841       Arm_relobj<big_endian>* arm_relobj =
4842         Arm_relobj<big_endian>::as_arm_relobj(*p);
4843       this->merge_processor_specific_flags(
4844           arm_relobj->name(),
4845           arm_relobj->processor_specific_flags());
4846       this->merge_object_attributes(arm_relobj->name().c_str(),
4847                                     arm_relobj->attributes_section_data());
4848
4849     } 
4850
4851   for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
4852        p != input_objects->dynobj_end();
4853        ++p)
4854     {
4855       Arm_dynobj<big_endian>* arm_dynobj =
4856         Arm_dynobj<big_endian>::as_arm_dynobj(*p);
4857       this->merge_processor_specific_flags(
4858           arm_dynobj->name(),
4859           arm_dynobj->processor_specific_flags());
4860       this->merge_object_attributes(arm_dynobj->name().c_str(),
4861                                     arm_dynobj->attributes_section_data());
4862     }
4863
4864   // Check BLX use.
4865   Object_attribute* attr =
4866     this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
4867   if (attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
4868     this->set_may_use_blx(true);
4869  
4870   // Fill in some more dynamic tags.
4871   Output_data_dynamic* const odyn = layout->dynamic_data();
4872   if (odyn != NULL)
4873     {
4874       if (this->got_plt_ != NULL
4875           && this->got_plt_->output_section() != NULL)
4876         odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
4877
4878       if (this->plt_ != NULL
4879           && this->plt_->output_section() != NULL)
4880         {
4881           const Output_data* od = this->plt_->rel_plt();
4882           odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
4883           odyn->add_section_address(elfcpp::DT_JMPREL, od);
4884           odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
4885         }
4886
4887       if (this->rel_dyn_ != NULL
4888           && this->rel_dyn_->output_section() != NULL)
4889         {
4890           const Output_data* od = this->rel_dyn_;
4891           odyn->add_section_address(elfcpp::DT_REL, od);
4892           odyn->add_section_size(elfcpp::DT_RELSZ, od);
4893           odyn->add_constant(elfcpp::DT_RELENT,
4894                              elfcpp::Elf_sizes<32>::rel_size);
4895         }
4896
4897       if (!parameters->options().shared())
4898         {
4899           // The value of the DT_DEBUG tag is filled in by the dynamic
4900           // linker at run time, and used by the debugger.
4901           odyn->add_constant(elfcpp::DT_DEBUG, 0);
4902         }
4903     }
4904
4905   // Emit any relocs we saved in an attempt to avoid generating COPY
4906   // relocs.
4907   if (this->copy_relocs_.any_saved_relocs())
4908     this->copy_relocs_.emit(this->rel_dyn_section(layout));
4909
4910   // Handle the .ARM.exidx section.
4911   Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
4912   if (exidx_section != NULL
4913       && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
4914       && !parameters->options().relocatable())
4915     {
4916       // Create __exidx_start and __exdix_end symbols.
4917       symtab->define_in_output_data("__exidx_start", NULL,
4918                                     Symbol_table::PREDEFINED,
4919                                     exidx_section, 0, 0, elfcpp::STT_OBJECT,
4920                                     elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
4921                                     false, false);
4922       symtab->define_in_output_data("__exidx_end", NULL,
4923                                     Symbol_table::PREDEFINED,
4924                                     exidx_section, 0, 0, elfcpp::STT_OBJECT,
4925                                     elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
4926                                     true, false);
4927
4928       // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4929       // the .ARM.exidx section.
4930       if (!layout->script_options()->saw_phdrs_clause())
4931         {
4932           gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
4933                       == NULL);
4934           Output_segment*  exidx_segment =
4935             layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
4936           exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
4937                                             false);
4938         }
4939     }
4940
4941   // Create an .ARM.attributes section if there is not one already.
4942   Output_attributes_section_data* attributes_section =
4943     new Output_attributes_section_data(*this->attributes_section_data_);
4944   layout->add_output_section_data(".ARM.attributes",
4945                                   elfcpp::SHT_ARM_ATTRIBUTES, 0,
4946                                   attributes_section, false, false, false,
4947                                   false);
4948 }
4949
4950 // Return whether a direct absolute static relocation needs to be applied.
4951 // In cases where Scan::local() or Scan::global() has created
4952 // a dynamic relocation other than R_ARM_RELATIVE, the addend
4953 // of the relocation is carried in the data, and we must not
4954 // apply the static relocation.
4955
4956 template<bool big_endian>
4957 inline bool
4958 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
4959     const Sized_symbol<32>* gsym,
4960     int ref_flags,
4961     bool is_32bit,
4962     Output_section* output_section)
4963 {
4964   // If the output section is not allocated, then we didn't call
4965   // scan_relocs, we didn't create a dynamic reloc, and we must apply
4966   // the reloc here.
4967   if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
4968       return true;
4969
4970   // For local symbols, we will have created a non-RELATIVE dynamic
4971   // relocation only if (a) the output is position independent,
4972   // (b) the relocation is absolute (not pc- or segment-relative), and
4973   // (c) the relocation is not 32 bits wide.
4974   if (gsym == NULL)
4975     return !(parameters->options().output_is_position_independent()
4976              && (ref_flags & Symbol::ABSOLUTE_REF)
4977              && !is_32bit);
4978
4979   // For global symbols, we use the same helper routines used in the
4980   // scan pass.  If we did not create a dynamic relocation, or if we
4981   // created a RELATIVE dynamic relocation, we should apply the static
4982   // relocation.
4983   bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
4984   bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
4985                  && gsym->can_use_relative_reloc(ref_flags
4986                                                  & Symbol::FUNCTION_CALL);
4987   return !has_dyn || is_rel;
4988 }
4989
4990 // Perform a relocation.
4991
4992 template<bool big_endian>
4993 inline bool
4994 Target_arm<big_endian>::Relocate::relocate(
4995     const Relocate_info<32, big_endian>* relinfo,
4996     Target_arm* target,
4997     Output_section *output_section,
4998     size_t relnum,
4999     const elfcpp::Rel<32, big_endian>& rel,
5000     unsigned int r_type,
5001     const Sized_symbol<32>* gsym,
5002     const Symbol_value<32>* psymval,
5003     unsigned char* view,
5004     Arm_address address,
5005     section_size_type /* view_size */ )
5006 {
5007   typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
5008
5009   r_type = get_real_reloc_type(r_type);
5010
5011   const Arm_relobj<big_endian>* object =
5012     Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
5013
5014   // If the final branch target of a relocation is THUMB instruction, this
5015   // is 1.  Otherwise it is 0.
5016   Arm_address thumb_bit = 0;
5017   Symbol_value<32> symval;
5018   bool is_weakly_undefined_without_plt = false;
5019   if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
5020     {
5021       if (gsym != NULL)
5022         {
5023           // This is a global symbol.  Determine if we use PLT and if the
5024           // final target is THUMB.
5025           if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
5026             {
5027               // This uses a PLT, change the symbol value.
5028               symval.set_output_value(target->plt_section()->address()
5029                                       + gsym->plt_offset());
5030               psymval = &symval;
5031             }
5032           else if (gsym->is_weak_undefined())
5033             {
5034               // This is a weakly undefined symbol and we do not use PLT
5035               // for this relocation.  A branch targeting this symbol will
5036               // be converted into an NOP.
5037               is_weakly_undefined_without_plt = true;
5038             }
5039           else
5040             {
5041               // Set thumb bit if symbol:
5042               // -Has type STT_ARM_TFUNC or
5043               // -Has type STT_FUNC, is defined and with LSB in value set.
5044               thumb_bit =
5045                 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
5046                  || (gsym->type() == elfcpp::STT_FUNC
5047                      && !gsym->is_undefined()
5048                      && ((psymval->value(object, 0) & 1) != 0)))
5049                 ? 1
5050                 : 0);
5051             }
5052         }
5053       else
5054         {
5055           // This is a local symbol.  Determine if the final target is THUMB.
5056           // We saved this information when all the local symbols were read.
5057           elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
5058           unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
5059           thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
5060         }
5061     }
5062   else
5063     {
5064       // This is a fake relocation synthesized for a stub.  It does not have
5065       // a real symbol.  We just look at the LSB of the symbol value to
5066       // determine if the target is THUMB or not.
5067       thumb_bit = ((psymval->value(object, 0) & 1) != 0);
5068     }
5069
5070   // Strip LSB if this points to a THUMB target.
5071   if (thumb_bit != 0
5072       && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type) 
5073       && ((psymval->value(object, 0) & 1) != 0))
5074     {
5075       Arm_address stripped_value =
5076         psymval->value(object, 0) & ~static_cast<Arm_address>(1);
5077       symval.set_output_value(stripped_value);
5078       psymval = &symval;
5079     } 
5080
5081   // Get the GOT offset if needed.
5082   // The GOT pointer points to the end of the GOT section.
5083   // We need to subtract the size of the GOT section to get
5084   // the actual offset to use in the relocation.
5085   bool have_got_offset = false;
5086   unsigned int got_offset = 0;
5087   switch (r_type)
5088     {
5089     case elfcpp::R_ARM_GOT_BREL:
5090     case elfcpp::R_ARM_GOT_PREL:
5091       if (gsym != NULL)
5092         {
5093           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
5094           got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
5095                         - target->got_size());
5096         }
5097       else
5098         {
5099           unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5100           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
5101           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
5102                         - target->got_size());
5103         }
5104       have_got_offset = true;
5105       break;
5106
5107     default:
5108       break;
5109     }
5110
5111   // To look up relocation stubs, we need to pass the symbol table index of
5112   // a local symbol.
5113   unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
5114
5115   typename Arm_relocate_functions::Status reloc_status =
5116         Arm_relocate_functions::STATUS_OKAY;
5117   switch (r_type)
5118     {
5119     case elfcpp::R_ARM_NONE:
5120       break;
5121
5122     case elfcpp::R_ARM_ABS8:
5123       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5124                                     output_section))
5125         reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
5126       break;
5127
5128     case elfcpp::R_ARM_ABS12:
5129       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5130                                     output_section))
5131         reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
5132       break;
5133
5134     case elfcpp::R_ARM_ABS16:
5135       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5136                                     output_section))
5137         reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
5138       break;
5139
5140     case elfcpp::R_ARM_ABS32:
5141       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5142                                     output_section))
5143         reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
5144                                                      thumb_bit);
5145       break;
5146
5147     case elfcpp::R_ARM_ABS32_NOI:
5148       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5149                                     output_section))
5150         // No thumb bit for this relocation: (S + A)
5151         reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
5152                                                      0);
5153       break;
5154
5155     case elfcpp::R_ARM_MOVW_ABS_NC:
5156       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5157                                     output_section))
5158         reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
5159                                                            psymval,
5160                                                            thumb_bit);
5161       else
5162         gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
5163                      "a shared object; recompile with -fPIC"));
5164       break;
5165
5166     case elfcpp::R_ARM_MOVT_ABS:
5167       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5168                                     output_section))
5169         reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
5170       else
5171         gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
5172                      "a shared object; recompile with -fPIC"));
5173       break;
5174
5175     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5176       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5177                                     output_section))
5178         reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
5179                                                                psymval,
5180                                                                thumb_bit);
5181       else
5182         gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
5183                      "making a shared object; recompile with -fPIC"));
5184       break;
5185
5186     case elfcpp::R_ARM_THM_MOVT_ABS:
5187       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5188                                     output_section))
5189         reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
5190                                                             psymval);
5191       else
5192         gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
5193                      "making a shared object; recompile with -fPIC"));
5194       break;
5195
5196     case elfcpp::R_ARM_MOVW_PREL_NC:
5197       reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
5198                                                           psymval, address,
5199                                                           thumb_bit);
5200       break;
5201
5202     case elfcpp::R_ARM_MOVT_PREL:
5203       reloc_status = Arm_relocate_functions::movt_prel(view, object,
5204                                                        psymval, address);
5205       break;
5206
5207     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5208       reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
5209                                                               psymval, address,
5210                                                               thumb_bit);
5211       break;
5212
5213     case elfcpp::R_ARM_THM_MOVT_PREL:
5214       reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
5215                                                            psymval, address);
5216       break;
5217         
5218     case elfcpp::R_ARM_REL32:
5219       reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
5220                                                    address, thumb_bit);
5221       break;
5222
5223     case elfcpp::R_ARM_THM_ABS5:
5224       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
5225                                     output_section))
5226         reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
5227       break;
5228
5229     case elfcpp::R_ARM_THM_CALL:
5230       reloc_status =
5231         Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
5232                                          psymval, address, thumb_bit,
5233                                          is_weakly_undefined_without_plt);
5234       break;
5235
5236     case elfcpp::R_ARM_XPC25:
5237       reloc_status =
5238         Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
5239                                       psymval, address, thumb_bit,
5240                                       is_weakly_undefined_without_plt);
5241       break;
5242
5243     case elfcpp::R_ARM_THM_XPC22:
5244       reloc_status =
5245         Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
5246                                           psymval, address, thumb_bit,
5247                                           is_weakly_undefined_without_plt);
5248       break;
5249
5250     case elfcpp::R_ARM_GOTOFF32:
5251       {
5252         Arm_address got_origin;
5253         got_origin = target->got_plt_section()->address();
5254         reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
5255                                                      got_origin, thumb_bit);
5256       }
5257       break;
5258
5259     case elfcpp::R_ARM_BASE_PREL:
5260       {
5261         uint32_t origin;
5262         // Get the addressing origin of the output segment defining the 
5263         // symbol gsym (AAELF 4.6.1.2 Relocation types)
5264         gold_assert(gsym != NULL); 
5265         if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5266           origin = gsym->output_segment()->vaddr();
5267         else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5268           origin = gsym->output_data()->address();
5269         else
5270           {
5271             gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5272                                    _("cannot find origin of R_ARM_BASE_PREL"));
5273             return true;
5274           }
5275         reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
5276       }
5277       break;
5278
5279     case elfcpp::R_ARM_BASE_ABS:
5280       {
5281         if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
5282                                       output_section))
5283           break;
5284
5285         uint32_t origin;
5286         // Get the addressing origin of the output segment defining
5287         // the symbol gsym (AAELF 4.6.1.2 Relocation types).
5288         if (gsym == NULL)
5289           // R_ARM_BASE_ABS with the NULL symbol will give the
5290           // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
5291           // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
5292           origin = target->got_plt_section()->address();
5293         else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
5294           origin = gsym->output_segment()->vaddr();
5295         else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
5296           origin = gsym->output_data()->address();
5297         else
5298           {
5299             gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5300                                    _("cannot find origin of R_ARM_BASE_ABS"));
5301             return true;
5302           }
5303
5304         reloc_status = Arm_relocate_functions::base_abs(view, origin);
5305       }
5306       break;
5307
5308     case elfcpp::R_ARM_GOT_BREL:
5309       gold_assert(have_got_offset);
5310       reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
5311       break;
5312
5313     case elfcpp::R_ARM_GOT_PREL:
5314       gold_assert(have_got_offset);
5315       // Get the address origin for GOT PLT, which is allocated right
5316       // after the GOT section, to calculate an absolute address of
5317       // the symbol GOT entry (got_origin + got_offset).
5318       Arm_address got_origin;
5319       got_origin = target->got_plt_section()->address();
5320       reloc_status = Arm_relocate_functions::got_prel(view,
5321                                                       got_origin + got_offset,
5322                                                       address);
5323       break;
5324
5325     case elfcpp::R_ARM_PLT32:
5326       gold_assert(gsym == NULL
5327                   || gsym->has_plt_offset()
5328                   || gsym->final_value_is_known()
5329                   || (gsym->is_defined()
5330                       && !gsym->is_from_dynobj()
5331                       && !gsym->is_preemptible()));
5332       reloc_status =
5333         Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
5334                                       psymval, address, thumb_bit,
5335                                       is_weakly_undefined_without_plt);
5336       break;
5337
5338     case elfcpp::R_ARM_CALL:
5339       reloc_status =
5340         Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
5341                                      psymval, address, thumb_bit,
5342                                      is_weakly_undefined_without_plt);
5343       break;
5344
5345     case elfcpp::R_ARM_JUMP24:
5346       reloc_status =
5347         Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
5348                                        psymval, address, thumb_bit,
5349                                        is_weakly_undefined_without_plt);
5350       break;
5351
5352     case elfcpp::R_ARM_THM_JUMP24:
5353       reloc_status =
5354         Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
5355                                            psymval, address, thumb_bit,
5356                                            is_weakly_undefined_without_plt);
5357       break;
5358
5359     case elfcpp::R_ARM_PREL31:
5360       reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
5361                                                     address, thumb_bit);
5362       break;
5363
5364     case elfcpp::R_ARM_TARGET1:
5365       // This should have been mapped to another type already.
5366       // Fall through.
5367     case elfcpp::R_ARM_COPY:
5368     case elfcpp::R_ARM_GLOB_DAT:
5369     case elfcpp::R_ARM_JUMP_SLOT:
5370     case elfcpp::R_ARM_RELATIVE:
5371       // These are relocations which should only be seen by the
5372       // dynamic linker, and should never be seen here.
5373       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5374                              _("unexpected reloc %u in object file"),
5375                              r_type);
5376       break;
5377
5378     default:
5379       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5380                              _("unsupported reloc %u"),
5381                              r_type);
5382       break;
5383     }
5384
5385   // Report any errors.
5386   switch (reloc_status)
5387     {
5388     case Arm_relocate_functions::STATUS_OKAY:
5389       break;
5390     case Arm_relocate_functions::STATUS_OVERFLOW:
5391       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
5392                              _("relocation overflow in relocation %u"),
5393                              r_type);
5394       break;
5395     case Arm_relocate_functions::STATUS_BAD_RELOC:
5396       gold_error_at_location(
5397         relinfo,
5398         relnum,
5399         rel.get_r_offset(),
5400         _("unexpected opcode while processing relocation %u"),
5401         r_type);
5402       break;
5403     default:
5404       gold_unreachable();
5405     }
5406
5407   return true;
5408 }
5409
5410 // Relocate section data.
5411
5412 template<bool big_endian>
5413 void
5414 Target_arm<big_endian>::relocate_section(
5415     const Relocate_info<32, big_endian>* relinfo,
5416     unsigned int sh_type,
5417     const unsigned char* prelocs,
5418     size_t reloc_count,
5419     Output_section* output_section,
5420     bool needs_special_offset_handling,
5421     unsigned char* view,
5422     Arm_address address,
5423     section_size_type view_size,
5424     const Reloc_symbol_changes* reloc_symbol_changes)
5425 {
5426   typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
5427   gold_assert(sh_type == elfcpp::SHT_REL);
5428
5429   Arm_input_section<big_endian>* arm_input_section =
5430     this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
5431
5432   // This is an ARM input section and the view covers the whole output
5433   // section.
5434   if (arm_input_section != NULL)
5435     {
5436       gold_assert(needs_special_offset_handling);
5437       Arm_address section_address = arm_input_section->address();
5438       section_size_type section_size = arm_input_section->data_size();
5439
5440       gold_assert((arm_input_section->address() >= address)
5441                   && ((arm_input_section->address()
5442                        + arm_input_section->data_size())
5443                       <= (address + view_size)));
5444
5445       off_t offset = section_address - address;
5446       view += offset;
5447       address += offset;
5448       view_size = section_size;
5449     }
5450
5451   gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
5452                          Arm_relocate>(
5453     relinfo,
5454     this,
5455     prelocs,
5456     reloc_count,
5457     output_section,
5458     needs_special_offset_handling,
5459     view,
5460     address,
5461     view_size,
5462     reloc_symbol_changes);
5463 }
5464
5465 // Return the size of a relocation while scanning during a relocatable
5466 // link.
5467
5468 template<bool big_endian>
5469 unsigned int
5470 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
5471     unsigned int r_type,
5472     Relobj* object)
5473 {
5474   r_type = get_real_reloc_type(r_type);
5475   switch (r_type)
5476     {
5477     case elfcpp::R_ARM_NONE:
5478       return 0;
5479
5480     case elfcpp::R_ARM_ABS8:
5481       return 1;
5482
5483     case elfcpp::R_ARM_ABS16:
5484     case elfcpp::R_ARM_THM_ABS5:
5485       return 2;
5486
5487     case elfcpp::R_ARM_ABS32:
5488     case elfcpp::R_ARM_ABS32_NOI:
5489     case elfcpp::R_ARM_ABS12:
5490     case elfcpp::R_ARM_BASE_ABS:
5491     case elfcpp::R_ARM_REL32:
5492     case elfcpp::R_ARM_THM_CALL:
5493     case elfcpp::R_ARM_GOTOFF32:
5494     case elfcpp::R_ARM_BASE_PREL:
5495     case elfcpp::R_ARM_GOT_BREL:
5496     case elfcpp::R_ARM_GOT_PREL:
5497     case elfcpp::R_ARM_PLT32:
5498     case elfcpp::R_ARM_CALL:
5499     case elfcpp::R_ARM_JUMP24:
5500     case elfcpp::R_ARM_PREL31:
5501     case elfcpp::R_ARM_MOVW_ABS_NC:
5502     case elfcpp::R_ARM_MOVT_ABS:
5503     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5504     case elfcpp::R_ARM_THM_MOVT_ABS:
5505     case elfcpp::R_ARM_MOVW_PREL_NC:
5506     case elfcpp::R_ARM_MOVT_PREL:
5507     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5508     case elfcpp::R_ARM_THM_MOVT_PREL:
5509       return 4;
5510
5511     case elfcpp::R_ARM_TARGET1:
5512       // This should have been mapped to another type already.
5513       // Fall through.
5514     case elfcpp::R_ARM_COPY:
5515     case elfcpp::R_ARM_GLOB_DAT:
5516     case elfcpp::R_ARM_JUMP_SLOT:
5517     case elfcpp::R_ARM_RELATIVE:
5518       // These are relocations which should only be seen by the
5519       // dynamic linker, and should never be seen here.
5520       gold_error(_("%s: unexpected reloc %u in object file"),
5521                  object->name().c_str(), r_type);
5522       return 0;
5523
5524     default:
5525       object->error(_("unsupported reloc %u in object file"), r_type);
5526       return 0;
5527     }
5528 }
5529
5530 // Scan the relocs during a relocatable link.
5531
5532 template<bool big_endian>
5533 void
5534 Target_arm<big_endian>::scan_relocatable_relocs(
5535     Symbol_table* symtab,
5536     Layout* layout,
5537     Sized_relobj<32, big_endian>* object,
5538     unsigned int data_shndx,
5539     unsigned int sh_type,
5540     const unsigned char* prelocs,
5541     size_t reloc_count,
5542     Output_section* output_section,
5543     bool needs_special_offset_handling,
5544     size_t local_symbol_count,
5545     const unsigned char* plocal_symbols,
5546     Relocatable_relocs* rr)
5547 {
5548   gold_assert(sh_type == elfcpp::SHT_REL);
5549
5550   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
5551     Relocatable_size_for_reloc> Scan_relocatable_relocs;
5552
5553   gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
5554       Scan_relocatable_relocs>(
5555     symtab,
5556     layout,
5557     object,
5558     data_shndx,
5559     prelocs,
5560     reloc_count,
5561     output_section,
5562     needs_special_offset_handling,
5563     local_symbol_count,
5564     plocal_symbols,
5565     rr);
5566 }
5567
5568 // Relocate a section during a relocatable link.
5569
5570 template<bool big_endian>
5571 void
5572 Target_arm<big_endian>::relocate_for_relocatable(
5573     const Relocate_info<32, big_endian>* relinfo,
5574     unsigned int sh_type,
5575     const unsigned char* prelocs,
5576     size_t reloc_count,
5577     Output_section* output_section,
5578     off_t offset_in_output_section,
5579     const Relocatable_relocs* rr,
5580     unsigned char* view,
5581     Arm_address view_address,
5582     section_size_type view_size,
5583     unsigned char* reloc_view,
5584     section_size_type reloc_view_size)
5585 {
5586   gold_assert(sh_type == elfcpp::SHT_REL);
5587
5588   gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
5589     relinfo,
5590     prelocs,
5591     reloc_count,
5592     output_section,
5593     offset_in_output_section,
5594     rr,
5595     view,
5596     view_address,
5597     view_size,
5598     reloc_view,
5599     reloc_view_size);
5600 }
5601
5602 // Return the value to use for a dynamic symbol which requires special
5603 // treatment.  This is how we support equality comparisons of function
5604 // pointers across shared library boundaries, as described in the
5605 // processor specific ABI supplement.
5606
5607 template<bool big_endian>
5608 uint64_t
5609 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
5610 {
5611   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5612   return this->plt_section()->address() + gsym->plt_offset();
5613 }
5614
5615 // Map platform-specific relocs to real relocs
5616 //
5617 template<bool big_endian>
5618 unsigned int
5619 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
5620 {
5621   switch (r_type)
5622     {
5623     case elfcpp::R_ARM_TARGET1:
5624       // This is either R_ARM_ABS32 or R_ARM_REL32;
5625       return elfcpp::R_ARM_ABS32;
5626
5627     case elfcpp::R_ARM_TARGET2:
5628       // This can be any reloc type but ususally is R_ARM_GOT_PREL
5629       return elfcpp::R_ARM_GOT_PREL;
5630
5631     default:
5632       return r_type;
5633     }
5634 }
5635
5636 // Whether if two EABI versions V1 and V2 are compatible.
5637
5638 template<bool big_endian>
5639 bool
5640 Target_arm<big_endian>::are_eabi_versions_compatible(
5641     elfcpp::Elf_Word v1,
5642     elfcpp::Elf_Word v2)
5643 {
5644   // v4 and v5 are the same spec before and after it was released,
5645   // so allow mixing them.
5646   if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
5647       || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
5648     return true;
5649
5650   return v1 == v2;
5651 }
5652
5653 // Combine FLAGS from an input object called NAME and the processor-specific
5654 // flags in the ELF header of the output.  Much of this is adapted from the
5655 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
5656 // in bfd/elf32-arm.c.
5657
5658 template<bool big_endian>
5659 void
5660 Target_arm<big_endian>::merge_processor_specific_flags(
5661     const std::string& name,
5662     elfcpp::Elf_Word flags)
5663 {
5664   if (this->are_processor_specific_flags_set())
5665     {
5666       elfcpp::Elf_Word out_flags = this->processor_specific_flags();
5667
5668       // Nothing to merge if flags equal to those in output.
5669       if (flags == out_flags)
5670         return;
5671
5672       // Complain about various flag mismatches.
5673       elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
5674       elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
5675       if (!this->are_eabi_versions_compatible(version1, version2))
5676         gold_error(_("Source object %s has EABI version %d but output has "
5677                      "EABI version %d."),
5678                    name.c_str(),
5679                    (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
5680                    (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
5681     }
5682   else
5683     {
5684       // If the input is the default architecture and had the default
5685       // flags then do not bother setting the flags for the output
5686       // architecture, instead allow future merges to do this.  If no
5687       // future merges ever set these flags then they will retain their
5688       // uninitialised values, which surprise surprise, correspond
5689       // to the default values.
5690       if (flags == 0)
5691         return;
5692
5693       // This is the first time, just copy the flags.
5694       // We only copy the EABI version for now.
5695       this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
5696     }
5697 }
5698
5699 // Adjust ELF file header.
5700 template<bool big_endian>
5701 void
5702 Target_arm<big_endian>::do_adjust_elf_header(
5703     unsigned char* view,
5704     int len) const
5705 {
5706   gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
5707
5708   elfcpp::Ehdr<32, big_endian> ehdr(view);
5709   unsigned char e_ident[elfcpp::EI_NIDENT];
5710   memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
5711
5712   if (elfcpp::arm_eabi_version(this->processor_specific_flags())
5713       == elfcpp::EF_ARM_EABI_UNKNOWN)
5714     e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
5715   else
5716     e_ident[elfcpp::EI_OSABI] = 0;
5717   e_ident[elfcpp::EI_ABIVERSION] = 0;
5718
5719   // FIXME: Do EF_ARM_BE8 adjustment.
5720
5721   elfcpp::Ehdr_write<32, big_endian> oehdr(view);
5722   oehdr.put_e_ident(e_ident);
5723 }
5724
5725 // do_make_elf_object to override the same function in the base class.
5726 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
5727 // to store ARM specific information.  Hence we need to have our own
5728 // ELF object creation.
5729
5730 template<bool big_endian>
5731 Object*
5732 Target_arm<big_endian>::do_make_elf_object(
5733     const std::string& name,
5734     Input_file* input_file,
5735     off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
5736 {
5737   int et = ehdr.get_e_type();
5738   if (et == elfcpp::ET_REL)
5739     {
5740       Arm_relobj<big_endian>* obj =
5741         new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
5742       obj->setup();
5743       return obj;
5744     }
5745   else if (et == elfcpp::ET_DYN)
5746     {
5747       Sized_dynobj<32, big_endian>* obj =
5748         new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
5749       obj->setup();
5750       return obj;
5751     }
5752   else
5753     {
5754       gold_error(_("%s: unsupported ELF file type %d"),
5755                  name.c_str(), et);
5756       return NULL;
5757     }
5758 }
5759
5760 // Read the architecture from the Tag_also_compatible_with attribute, if any.
5761 // Returns -1 if no architecture could be read.
5762 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
5763
5764 template<bool big_endian>
5765 int
5766 Target_arm<big_endian>::get_secondary_compatible_arch(
5767     const Attributes_section_data* pasd)
5768 {
5769   const Object_attribute *known_attributes =
5770     pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
5771
5772   // Note: the tag and its argument below are uleb128 values, though
5773   // currently-defined values fit in one byte for each.
5774   const std::string& sv =
5775     known_attributes[elfcpp::Tag_also_compatible_with].string_value();
5776   if (sv.size() == 2
5777       && sv.data()[0] == elfcpp::Tag_CPU_arch
5778       && (sv.data()[1] & 128) != 128)
5779    return sv.data()[1];
5780
5781   // This tag is "safely ignorable", so don't complain if it looks funny.
5782   return -1;
5783 }
5784
5785 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
5786 // The tag is removed if ARCH is -1.
5787 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
5788
5789 template<bool big_endian>
5790 void
5791 Target_arm<big_endian>::set_secondary_compatible_arch(
5792     Attributes_section_data* pasd,
5793     int arch)
5794 {
5795   Object_attribute *known_attributes =
5796     pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
5797
5798   if (arch == -1)
5799     {
5800       known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
5801       return;
5802     }
5803
5804   // Note: the tag and its argument below are uleb128 values, though
5805   // currently-defined values fit in one byte for each.
5806   char sv[3];
5807   sv[0] = elfcpp::Tag_CPU_arch;
5808   gold_assert(arch != 0);
5809   sv[1] = arch;
5810   sv[2] = '\0';
5811
5812   known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
5813 }
5814
5815 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
5816 // into account.
5817 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
5818
5819 template<bool big_endian>
5820 int
5821 Target_arm<big_endian>::tag_cpu_arch_combine(
5822     const char* name,
5823     int oldtag,
5824     int* secondary_compat_out,
5825     int newtag,
5826     int secondary_compat)
5827 {
5828 #define T(X) elfcpp::TAG_CPU_ARCH_##X
5829   static const int v6t2[] =
5830     {
5831       T(V6T2),   // PRE_V4.
5832       T(V6T2),   // V4.
5833       T(V6T2),   // V4T.
5834       T(V6T2),   // V5T.
5835       T(V6T2),   // V5TE.
5836       T(V6T2),   // V5TEJ.
5837       T(V6T2),   // V6.
5838       T(V7),     // V6KZ.
5839       T(V6T2)    // V6T2.
5840     };
5841   static const int v6k[] =
5842     {
5843       T(V6K),    // PRE_V4.
5844       T(V6K),    // V4.
5845       T(V6K),    // V4T.
5846       T(V6K),    // V5T.
5847       T(V6K),    // V5TE.
5848       T(V6K),    // V5TEJ.
5849       T(V6K),    // V6.
5850       T(V6KZ),   // V6KZ.
5851       T(V7),     // V6T2.
5852       T(V6K)     // V6K.
5853     };
5854   static const int v7[] =
5855     {
5856       T(V7),     // PRE_V4.
5857       T(V7),     // V4.
5858       T(V7),     // V4T.
5859       T(V7),     // V5T.
5860       T(V7),     // V5TE.
5861       T(V7),     // V5TEJ.
5862       T(V7),     // V6.
5863       T(V7),     // V6KZ.
5864       T(V7),     // V6T2.
5865       T(V7),     // V6K.
5866       T(V7)      // V7.
5867     };
5868   static const int v6_m[] =
5869     {
5870       -1,        // PRE_V4.
5871       -1,        // V4.
5872       T(V6K),    // V4T.
5873       T(V6K),    // V5T.
5874       T(V6K),    // V5TE.
5875       T(V6K),    // V5TEJ.
5876       T(V6K),    // V6.
5877       T(V6KZ),   // V6KZ.
5878       T(V7),     // V6T2.
5879       T(V6K),    // V6K.
5880       T(V7),     // V7.
5881       T(V6_M)    // V6_M.
5882     };
5883   static const int v6s_m[] =
5884     {
5885       -1,        // PRE_V4.
5886       -1,        // V4.
5887       T(V6K),    // V4T.
5888       T(V6K),    // V5T.
5889       T(V6K),    // V5TE.
5890       T(V6K),    // V5TEJ.
5891       T(V6K),    // V6.
5892       T(V6KZ),   // V6KZ.
5893       T(V7),     // V6T2.
5894       T(V6K),    // V6K.
5895       T(V7),     // V7.
5896       T(V6S_M),  // V6_M.
5897       T(V6S_M)   // V6S_M.
5898     };
5899   static const int v7e_m[] =
5900     {
5901       -1,       // PRE_V4.
5902       -1,       // V4.
5903       T(V7E_M), // V4T.
5904       T(V7E_M), // V5T.
5905       T(V7E_M), // V5TE.
5906       T(V7E_M), // V5TEJ.
5907       T(V7E_M), // V6.
5908       T(V7E_M), // V6KZ.
5909       T(V7E_M), // V6T2.
5910       T(V7E_M), // V6K.
5911       T(V7E_M), // V7.
5912       T(V7E_M), // V6_M.
5913       T(V7E_M), // V6S_M.
5914       T(V7E_M)  // V7E_M.
5915     };
5916   static const int v4t_plus_v6_m[] =
5917     {
5918       -1,               // PRE_V4.
5919       -1,               // V4.
5920       T(V4T),           // V4T.
5921       T(V5T),           // V5T.
5922       T(V5TE),          // V5TE.
5923       T(V5TEJ),         // V5TEJ.
5924       T(V6),            // V6.
5925       T(V6KZ),          // V6KZ.
5926       T(V6T2),          // V6T2.
5927       T(V6K),           // V6K.
5928       T(V7),            // V7.
5929       T(V6_M),          // V6_M.
5930       T(V6S_M),         // V6S_M.
5931       T(V7E_M),         // V7E_M.
5932       T(V4T_PLUS_V6_M)  // V4T plus V6_M.
5933     };
5934   static const int *comb[] =
5935     {
5936       v6t2,
5937       v6k,
5938       v7,
5939       v6_m,
5940       v6s_m,
5941       v7e_m,
5942       // Pseudo-architecture.
5943       v4t_plus_v6_m
5944     };
5945
5946   // Check we've not got a higher architecture than we know about.
5947
5948   if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
5949     {
5950       gold_error(_("%s: unknown CPU architecture"), name);
5951       return -1;
5952     }
5953
5954   // Override old tag if we have a Tag_also_compatible_with on the output.
5955
5956   if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
5957       || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
5958     oldtag = T(V4T_PLUS_V6_M);
5959
5960   // And override the new tag if we have a Tag_also_compatible_with on the
5961   // input.
5962
5963   if ((newtag == T(V6_M) && secondary_compat == T(V4T))
5964       || (newtag == T(V4T) && secondary_compat == T(V6_M)))
5965     newtag = T(V4T_PLUS_V6_M);
5966
5967   // Architectures before V6KZ add features monotonically.
5968   int tagh = std::max(oldtag, newtag);
5969   if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
5970     return tagh;
5971
5972   int tagl = std::min(oldtag, newtag);
5973   int result = comb[tagh - T(V6T2)][tagl];
5974
5975   // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
5976   // as the canonical version.
5977   if (result == T(V4T_PLUS_V6_M))
5978     {
5979       result = T(V4T);
5980       *secondary_compat_out = T(V6_M);
5981     }
5982   else
5983     *secondary_compat_out = -1;
5984
5985   if (result == -1)
5986     {
5987       gold_error(_("%s: conflicting CPU architectures %d/%d"),
5988                  name, oldtag, newtag);
5989       return -1;
5990     }
5991
5992   return result;
5993 #undef T
5994 }
5995
5996 // Helper to print AEABI enum tag value.
5997
5998 template<bool big_endian>
5999 std::string
6000 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
6001 {
6002   static const char *aeabi_enum_names[] =
6003     { "", "variable-size", "32-bit", "" };
6004   const size_t aeabi_enum_names_size =
6005     sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
6006
6007   if (value < aeabi_enum_names_size)
6008     return std::string(aeabi_enum_names[value]);
6009   else
6010     {
6011       char buffer[100];
6012       sprintf(buffer, "<unknown value %u>", value);
6013       return std::string(buffer);
6014     }
6015 }
6016
6017 // Return the string value to store in TAG_CPU_name.
6018
6019 template<bool big_endian>
6020 std::string
6021 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
6022 {
6023   static const char *name_table[] = {
6024     // These aren't real CPU names, but we can't guess
6025     // that from the architecture version alone.
6026    "Pre v4",
6027    "ARM v4",
6028    "ARM v4T",
6029    "ARM v5T",
6030    "ARM v5TE",
6031    "ARM v5TEJ",
6032    "ARM v6",
6033    "ARM v6KZ",
6034    "ARM v6T2",
6035    "ARM v6K",
6036    "ARM v7",
6037    "ARM v6-M",
6038    "ARM v6S-M",
6039    "ARM v7E-M"
6040  };
6041  const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
6042
6043   if (value < name_table_size)
6044     return std::string(name_table[value]);
6045   else
6046     {
6047       char buffer[100];
6048       sprintf(buffer, "<unknown CPU value %u>", value);
6049       return std::string(buffer);
6050     } 
6051 }
6052
6053 // Merge object attributes from input file called NAME with those of the
6054 // output.  The input object attributes are in the object pointed by PASD.
6055
6056 template<bool big_endian>
6057 void
6058 Target_arm<big_endian>::merge_object_attributes(
6059     const char* name,
6060     const Attributes_section_data* pasd)
6061 {
6062   // Return if there is no attributes section data.
6063   if (pasd == NULL)
6064     return;
6065
6066   // If output has no object attributes, just copy.
6067   if (this->attributes_section_data_ == NULL)
6068     {
6069       this->attributes_section_data_ = new Attributes_section_data(*pasd);
6070       return;
6071     }
6072
6073   const int vendor = Object_attribute::OBJ_ATTR_PROC;
6074   const Object_attribute* in_attr = pasd->known_attributes(vendor);
6075   Object_attribute* out_attr =
6076     this->attributes_section_data_->known_attributes(vendor);
6077
6078   // This needs to happen before Tag_ABI_FP_number_model is merged.  */
6079   if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
6080       != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
6081     {
6082       // Ignore mismatches if the object doesn't use floating point.  */
6083       if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
6084         out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
6085             in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
6086       else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
6087         gold_error(_("%s uses VFP register arguments, output does not"),
6088                    name);
6089     }
6090
6091   for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
6092     {
6093       // Merge this attribute with existing attributes.
6094       switch (i)
6095         {
6096         case elfcpp::Tag_CPU_raw_name:
6097         case elfcpp::Tag_CPU_name:
6098           // These are merged after Tag_CPU_arch.
6099           break;
6100
6101         case elfcpp::Tag_ABI_optimization_goals:
6102         case elfcpp::Tag_ABI_FP_optimization_goals:
6103           // Use the first value seen.
6104           break;
6105
6106         case elfcpp::Tag_CPU_arch:
6107           {
6108             unsigned int saved_out_attr = out_attr->int_value();
6109             // Merge Tag_CPU_arch and Tag_also_compatible_with.
6110             int secondary_compat =
6111               this->get_secondary_compatible_arch(pasd);
6112             int secondary_compat_out =
6113               this->get_secondary_compatible_arch(
6114                   this->attributes_section_data_);
6115             out_attr[i].set_int_value(
6116                 tag_cpu_arch_combine(name, out_attr[i].int_value(),
6117                                      &secondary_compat_out,
6118                                      in_attr[i].int_value(),
6119                                      secondary_compat));
6120             this->set_secondary_compatible_arch(this->attributes_section_data_,
6121                                                 secondary_compat_out);
6122
6123             // Merge Tag_CPU_name and Tag_CPU_raw_name.
6124             if (out_attr[i].int_value() == saved_out_attr)
6125               ; // Leave the names alone.
6126             else if (out_attr[i].int_value() == in_attr[i].int_value())
6127               {
6128                 // The output architecture has been changed to match the
6129                 // input architecture.  Use the input names.
6130                 out_attr[elfcpp::Tag_CPU_name].set_string_value(
6131                     in_attr[elfcpp::Tag_CPU_name].string_value());
6132                 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
6133                     in_attr[elfcpp::Tag_CPU_raw_name].string_value());
6134               }
6135             else
6136               {
6137                 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
6138                 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
6139               }
6140
6141             // If we still don't have a value for Tag_CPU_name,
6142             // make one up now.  Tag_CPU_raw_name remains blank.
6143             if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
6144               {
6145                 const std::string cpu_name =
6146                   this->tag_cpu_name_value(out_attr[i].int_value());
6147                 // FIXME:  If we see an unknown CPU, this will be set
6148                 // to "<unknown CPU n>", where n is the attribute value.
6149                 // This is different from BFD, which leaves the name alone.
6150                 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
6151               }
6152           }
6153           break;
6154
6155         case elfcpp::Tag_ARM_ISA_use:
6156         case elfcpp::Tag_THUMB_ISA_use:
6157         case elfcpp::Tag_WMMX_arch:
6158         case elfcpp::Tag_Advanced_SIMD_arch:
6159           // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
6160         case elfcpp::Tag_ABI_FP_rounding:
6161         case elfcpp::Tag_ABI_FP_exceptions:
6162         case elfcpp::Tag_ABI_FP_user_exceptions:
6163         case elfcpp::Tag_ABI_FP_number_model:
6164         case elfcpp::Tag_VFP_HP_extension:
6165         case elfcpp::Tag_CPU_unaligned_access:
6166         case elfcpp::Tag_T2EE_use:
6167         case elfcpp::Tag_Virtualization_use:
6168         case elfcpp::Tag_MPextension_use:
6169           // Use the largest value specified.
6170           if (in_attr[i].int_value() > out_attr[i].int_value())
6171             out_attr[i].set_int_value(in_attr[i].int_value());
6172           break;
6173
6174         case elfcpp::Tag_ABI_align8_preserved:
6175         case elfcpp::Tag_ABI_PCS_RO_data:
6176           // Use the smallest value specified.
6177           if (in_attr[i].int_value() < out_attr[i].int_value())
6178             out_attr[i].set_int_value(in_attr[i].int_value());
6179           break;
6180
6181         case elfcpp::Tag_ABI_align8_needed:
6182           if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
6183               && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
6184                   || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
6185                       == 0)))
6186             {
6187               // This error message should be enabled once all non-conformant
6188               // binaries in the toolchain have had the attributes set
6189               // properly.
6190               // gold_error(_("output 8-byte data alignment conflicts with %s"),
6191               //            name);
6192             }
6193           // Fall through.
6194         case elfcpp::Tag_ABI_FP_denormal:
6195         case elfcpp::Tag_ABI_PCS_GOT_use:
6196           {
6197             // These tags have 0 = don't care, 1 = strong requirement,
6198             // 2 = weak requirement.
6199             static const int order_021[3] = {0, 2, 1};
6200
6201             // Use the "greatest" from the sequence 0, 2, 1, or the largest
6202             // value if greater than 2 (for future-proofing).
6203             if ((in_attr[i].int_value() > 2
6204                  && in_attr[i].int_value() > out_attr[i].int_value())
6205                 || (in_attr[i].int_value() <= 2
6206                     && out_attr[i].int_value() <= 2
6207                     && (order_021[in_attr[i].int_value()]
6208                         > order_021[out_attr[i].int_value()])))
6209               out_attr[i].set_int_value(in_attr[i].int_value());
6210           }
6211           break;
6212
6213         case elfcpp::Tag_CPU_arch_profile:
6214           if (out_attr[i].int_value() != in_attr[i].int_value())
6215             {
6216               // 0 will merge with anything.
6217               // 'A' and 'S' merge to 'A'.
6218               // 'R' and 'S' merge to 'R'.
6219               // 'M' and 'A|R|S' is an error.
6220               if (out_attr[i].int_value() == 0
6221                   || (out_attr[i].int_value() == 'S'
6222                       && (in_attr[i].int_value() == 'A'
6223                           || in_attr[i].int_value() == 'R')))
6224                 out_attr[i].set_int_value(in_attr[i].int_value());
6225               else if (in_attr[i].int_value() == 0
6226                        || (in_attr[i].int_value() == 'S'
6227                            && (out_attr[i].int_value() == 'A'
6228                                || out_attr[i].int_value() == 'R')))
6229                 ; // Do nothing.
6230               else
6231                 {
6232                   gold_error
6233                     (_("conflicting architecture profiles %c/%c"),
6234                      in_attr[i].int_value() ? in_attr[i].int_value() : '0',
6235                      out_attr[i].int_value() ? out_attr[i].int_value() : '0');
6236                 }
6237             }
6238           break;
6239         case elfcpp::Tag_VFP_arch:
6240             {
6241               static const struct
6242               {
6243                   int ver;
6244                   int regs;
6245               } vfp_versions[7] =
6246                 {
6247                   {0, 0},
6248                   {1, 16},
6249                   {2, 16},
6250                   {3, 32},
6251                   {3, 16},
6252                   {4, 32},
6253                   {4, 16}
6254                 };
6255
6256               // Values greater than 6 aren't defined, so just pick the
6257               // biggest.
6258               if (in_attr[i].int_value() > 6
6259                   && in_attr[i].int_value() > out_attr[i].int_value())
6260                 {
6261                   *out_attr = *in_attr;
6262                   break;
6263                 }
6264               // The output uses the superset of input features
6265               // (ISA version) and registers.
6266               int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
6267                                  vfp_versions[out_attr[i].int_value()].ver);
6268               int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
6269                                   vfp_versions[out_attr[i].int_value()].regs);
6270               // This assumes all possible supersets are also a valid
6271               // options.
6272               int newval;
6273               for (newval = 6; newval > 0; newval--)
6274                 {
6275                   if (regs == vfp_versions[newval].regs
6276                       && ver == vfp_versions[newval].ver)
6277                     break;
6278                 }
6279               out_attr[i].set_int_value(newval);
6280             }
6281           break;
6282         case elfcpp::Tag_PCS_config:
6283           if (out_attr[i].int_value() == 0)
6284             out_attr[i].set_int_value(in_attr[i].int_value());
6285           else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6286             {
6287               // It's sometimes ok to mix different configs, so this is only
6288               // a warning.
6289               gold_warning(_("%s: conflicting platform configuration"), name);
6290             }
6291           break;
6292         case elfcpp::Tag_ABI_PCS_R9_use:
6293           if (in_attr[i].int_value() != out_attr[i].int_value()
6294               && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
6295               && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
6296             {
6297               gold_error(_("%s: conflicting use of R9"), name);
6298             }
6299           if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
6300             out_attr[i].set_int_value(in_attr[i].int_value());
6301           break;
6302         case elfcpp::Tag_ABI_PCS_RW_data:
6303           if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
6304               && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6305                   != elfcpp::AEABI_R9_SB)
6306               && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
6307                   != elfcpp::AEABI_R9_unused))
6308             {
6309               gold_error(_("%s: SB relative addressing conflicts with use "
6310                            "of R9"),
6311                          name);
6312             }
6313           // Use the smallest value specified.
6314           if (in_attr[i].int_value() < out_attr[i].int_value())
6315             out_attr[i].set_int_value(in_attr[i].int_value());
6316           break;
6317         case elfcpp::Tag_ABI_PCS_wchar_t:
6318           // FIXME: Make it possible to turn off this warning.
6319           if (out_attr[i].int_value()
6320               && in_attr[i].int_value()
6321               && out_attr[i].int_value() != in_attr[i].int_value())
6322             {
6323               gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
6324                              "use %u-byte wchar_t; use of wchar_t values "
6325                              "across objects may fail"),
6326                            name, in_attr[i].int_value(),
6327                            out_attr[i].int_value());
6328             }
6329           else if (in_attr[i].int_value() && !out_attr[i].int_value())
6330             out_attr[i].set_int_value(in_attr[i].int_value());
6331           break;
6332         case elfcpp::Tag_ABI_enum_size:
6333           if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
6334             {
6335               if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
6336                   || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
6337                 {
6338                   // The existing object is compatible with anything.
6339                   // Use whatever requirements the new object has.
6340                   out_attr[i].set_int_value(in_attr[i].int_value());
6341                 }
6342               // FIXME: Make it possible to turn off this warning.
6343               else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
6344                        && out_attr[i].int_value() != in_attr[i].int_value())
6345                 {
6346                   unsigned int in_value = in_attr[i].int_value();
6347                   unsigned int out_value = out_attr[i].int_value();
6348                   gold_warning(_("%s uses %s enums yet the output is to use "
6349                                  "%s enums; use of enum values across objects "
6350                                  "may fail"),
6351                                name,
6352                                this->aeabi_enum_name(in_value).c_str(),
6353                                this->aeabi_enum_name(out_value).c_str());
6354                 }
6355             }
6356           break;
6357         case elfcpp::Tag_ABI_VFP_args:
6358           // Aready done.
6359           break;
6360         case elfcpp::Tag_ABI_WMMX_args:
6361           if (in_attr[i].int_value() != out_attr[i].int_value())
6362             {
6363               gold_error(_("%s uses iWMMXt register arguments, output does "
6364                            "not"),
6365                          name);
6366             }
6367           break;
6368         case Object_attribute::Tag_compatibility:
6369           // Merged in target-independent code.
6370           break;
6371         case elfcpp::Tag_ABI_HardFP_use:
6372           // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
6373           if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
6374               || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
6375             out_attr[i].set_int_value(3);
6376           else if (in_attr[i].int_value() > out_attr[i].int_value())
6377             out_attr[i].set_int_value(in_attr[i].int_value());
6378           break;
6379         case elfcpp::Tag_ABI_FP_16bit_format:
6380           if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
6381             {
6382               if (in_attr[i].int_value() != out_attr[i].int_value())
6383                 gold_error(_("fp16 format mismatch between %s and output"),
6384                            name);
6385             }
6386           if (in_attr[i].int_value() != 0)
6387             out_attr[i].set_int_value(in_attr[i].int_value());
6388           break;
6389
6390         case elfcpp::Tag_nodefaults:
6391           // This tag is set if it exists, but the value is unused (and is
6392           // typically zero).  We don't actually need to do anything here -
6393           // the merge happens automatically when the type flags are merged
6394           // below.
6395           break;
6396         case elfcpp::Tag_also_compatible_with:
6397           // Already done in Tag_CPU_arch.
6398           break;
6399         case elfcpp::Tag_conformance:
6400           // Keep the attribute if it matches.  Throw it away otherwise.
6401           // No attribute means no claim to conform.
6402           if (in_attr[i].string_value() != out_attr[i].string_value())
6403             out_attr[i].set_string_value("");
6404           break;
6405
6406         default:
6407           {
6408             const char* err_object = NULL;
6409
6410             // The "known_obj_attributes" table does contain some undefined
6411             // attributes.  Ensure that there are unused.
6412             if (out_attr[i].int_value() != 0
6413                 || out_attr[i].string_value() != "")
6414               err_object = "output";
6415             else if (in_attr[i].int_value() != 0
6416                      || in_attr[i].string_value() != "")
6417               err_object = name;
6418
6419             if (err_object != NULL)
6420               {
6421                 // Attribute numbers >=64 (mod 128) can be safely ignored.
6422                 if ((i & 127) < 64)
6423                   gold_error(_("%s: unknown mandatory EABI object attribute "
6424                                "%d"),
6425                              err_object, i);
6426                 else
6427                   gold_warning(_("%s: unknown EABI object attribute %d"),
6428                                err_object, i);
6429               }
6430
6431             // Only pass on attributes that match in both inputs.
6432             if (!in_attr[i].matches(out_attr[i]))
6433               {
6434                 out_attr[i].set_int_value(0);
6435                 out_attr[i].set_string_value("");
6436               }
6437           }
6438         }
6439
6440       // If out_attr was copied from in_attr then it won't have a type yet.
6441       if (in_attr[i].type() && !out_attr[i].type())
6442         out_attr[i].set_type(in_attr[i].type());
6443     }
6444
6445   // Merge Tag_compatibility attributes and any common GNU ones.
6446   this->attributes_section_data_->merge(name, pasd);
6447
6448   // Check for any attributes not known on ARM.
6449   typedef Vendor_object_attributes::Other_attributes Other_attributes;
6450   const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
6451   Other_attributes::const_iterator in_iter = in_other_attributes->begin();
6452   Other_attributes* out_other_attributes =
6453     this->attributes_section_data_->other_attributes(vendor);
6454   Other_attributes::iterator out_iter = out_other_attributes->begin();
6455
6456   while (in_iter != in_other_attributes->end()
6457          || out_iter != out_other_attributes->end())
6458     {
6459       const char* err_object = NULL;
6460       int err_tag = 0;
6461
6462       // The tags for each list are in numerical order.
6463       // If the tags are equal, then merge.
6464       if (out_iter != out_other_attributes->end()
6465           && (in_iter == in_other_attributes->end()
6466               || in_iter->first > out_iter->first))
6467         {
6468           // This attribute only exists in output.  We can't merge, and we
6469           // don't know what the tag means, so delete it.
6470           err_object = "output";
6471           err_tag = out_iter->first;
6472           int saved_tag = out_iter->first;
6473           delete out_iter->second;
6474           out_other_attributes->erase(out_iter); 
6475           out_iter = out_other_attributes->upper_bound(saved_tag);
6476         }
6477       else if (in_iter != in_other_attributes->end()
6478                && (out_iter != out_other_attributes->end()
6479                    || in_iter->first < out_iter->first))
6480         {
6481           // This attribute only exists in input. We can't merge, and we
6482           // don't know what the tag means, so ignore it.
6483           err_object = name;
6484           err_tag = in_iter->first;
6485           ++in_iter;
6486         }
6487       else // The tags are equal.
6488         {
6489           // As present, all attributes in the list are unknown, and
6490           // therefore can't be merged meaningfully.
6491           err_object = "output";
6492           err_tag = out_iter->first;
6493
6494           //  Only pass on attributes that match in both inputs.
6495           if (!in_iter->second->matches(*(out_iter->second)))
6496             {
6497               // No match.  Delete the attribute.
6498               int saved_tag = out_iter->first;
6499               delete out_iter->second;
6500               out_other_attributes->erase(out_iter);
6501               out_iter = out_other_attributes->upper_bound(saved_tag);
6502             }
6503           else
6504             {
6505               // Matched.  Keep the attribute and move to the next.
6506               ++out_iter;
6507               ++in_iter;
6508             }
6509         }
6510
6511       if (err_object)
6512         {
6513           // Attribute numbers >=64 (mod 128) can be safely ignored.  */
6514           if ((err_tag & 127) < 64)
6515             {
6516               gold_error(_("%s: unknown mandatory EABI object attribute %d"),
6517                          err_object, err_tag);
6518             }
6519           else
6520             {
6521               gold_warning(_("%s: unknown EABI object attribute %d"),
6522                            err_object, err_tag);
6523             }
6524         }
6525     }
6526 }
6527
6528 // Return whether a relocation type used the LSB to distinguish THUMB
6529 // addresses.
6530 template<bool big_endian>
6531 bool
6532 Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
6533 {
6534   switch (r_type)
6535     {
6536     case elfcpp::R_ARM_PC24:
6537     case elfcpp::R_ARM_ABS32:
6538     case elfcpp::R_ARM_REL32:
6539     case elfcpp::R_ARM_SBREL32:
6540     case elfcpp::R_ARM_THM_CALL:
6541     case elfcpp::R_ARM_GLOB_DAT:
6542     case elfcpp::R_ARM_JUMP_SLOT:
6543     case elfcpp::R_ARM_GOTOFF32:
6544     case elfcpp::R_ARM_PLT32:
6545     case elfcpp::R_ARM_CALL:
6546     case elfcpp::R_ARM_JUMP24:
6547     case elfcpp::R_ARM_THM_JUMP24:
6548     case elfcpp::R_ARM_SBREL31:
6549     case elfcpp::R_ARM_PREL31:
6550     case elfcpp::R_ARM_MOVW_ABS_NC:
6551     case elfcpp::R_ARM_MOVW_PREL_NC:
6552     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
6553     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
6554     case elfcpp::R_ARM_THM_JUMP19:
6555     case elfcpp::R_ARM_THM_ALU_PREL_11_0:
6556     case elfcpp::R_ARM_ALU_PC_G0_NC:
6557     case elfcpp::R_ARM_ALU_PC_G0:
6558     case elfcpp::R_ARM_ALU_PC_G1_NC:
6559     case elfcpp::R_ARM_ALU_PC_G1:
6560     case elfcpp::R_ARM_ALU_PC_G2:
6561     case elfcpp::R_ARM_ALU_SB_G0_NC:
6562     case elfcpp::R_ARM_ALU_SB_G0:
6563     case elfcpp::R_ARM_ALU_SB_G1_NC:
6564     case elfcpp::R_ARM_ALU_SB_G1:
6565     case elfcpp::R_ARM_ALU_SB_G2:
6566     case elfcpp::R_ARM_MOVW_BREL_NC:
6567     case elfcpp::R_ARM_MOVW_BREL:
6568     case elfcpp::R_ARM_THM_MOVW_BREL_NC:
6569     case elfcpp::R_ARM_THM_MOVW_BREL:
6570       return true;
6571     default:
6572       return false;
6573     }
6574 }
6575
6576 // Stub-generation methods for Target_arm.
6577
6578 // Make a new Arm_input_section object.
6579
6580 template<bool big_endian>
6581 Arm_input_section<big_endian>*
6582 Target_arm<big_endian>::new_arm_input_section(
6583     Relobj* relobj,
6584     unsigned int shndx)
6585 {
6586   Input_section_specifier iss(relobj, shndx);
6587
6588   Arm_input_section<big_endian>* arm_input_section =
6589     new Arm_input_section<big_endian>(relobj, shndx);
6590   arm_input_section->init();
6591
6592   // Register new Arm_input_section in map for look-up.
6593   std::pair<typename Arm_input_section_map::iterator, bool> ins =
6594     this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
6595
6596   // Make sure that it we have not created another Arm_input_section
6597   // for this input section already.
6598   gold_assert(ins.second);
6599
6600   return arm_input_section; 
6601 }
6602
6603 // Find the Arm_input_section object corresponding to the SHNDX-th input
6604 // section of RELOBJ.
6605
6606 template<bool big_endian>
6607 Arm_input_section<big_endian>*
6608 Target_arm<big_endian>::find_arm_input_section(
6609     Relobj* relobj,
6610     unsigned int shndx) const
6611 {
6612   Input_section_specifier iss(relobj, shndx);
6613   typename Arm_input_section_map::const_iterator p =
6614     this->arm_input_section_map_.find(iss);
6615   return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
6616 }
6617
6618 // Make a new stub table.
6619
6620 template<bool big_endian>
6621 Stub_table<big_endian>*
6622 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
6623 {
6624   Stub_table<big_endian>* stub_table =
6625     new Stub_table<big_endian>(owner);
6626   this->stub_tables_.push_back(stub_table);
6627
6628   stub_table->set_address(owner->address() + owner->data_size());
6629   stub_table->set_file_offset(owner->offset() + owner->data_size());
6630   stub_table->finalize_data_size();
6631
6632   return stub_table;
6633 }
6634
6635 // Scan a relocation for stub generation.
6636
6637 template<bool big_endian>
6638 void
6639 Target_arm<big_endian>::scan_reloc_for_stub(
6640     const Relocate_info<32, big_endian>* relinfo,
6641     unsigned int r_type,
6642     const Sized_symbol<32>* gsym,
6643     unsigned int r_sym,
6644     const Symbol_value<32>* psymval,
6645     elfcpp::Elf_types<32>::Elf_Swxword addend,
6646     Arm_address address)
6647 {
6648   typedef typename Target_arm<big_endian>::Relocate Relocate;
6649
6650   const Arm_relobj<big_endian>* arm_relobj =
6651     Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
6652
6653   bool target_is_thumb;
6654   Symbol_value<32> symval;
6655   if (gsym != NULL)
6656     {
6657       // This is a global symbol.  Determine if we use PLT and if the
6658       // final target is THUMB.
6659       if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
6660         {
6661           // This uses a PLT, change the symbol value.
6662           symval.set_output_value(this->plt_section()->address()
6663                                   + gsym->plt_offset());
6664           psymval = &symval;
6665           target_is_thumb = false;
6666         }
6667       else if (gsym->is_undefined())
6668         // There is no need to generate a stub symbol is undefined.
6669         return;
6670       else
6671         {
6672           target_is_thumb =
6673             ((gsym->type() == elfcpp::STT_ARM_TFUNC)
6674              || (gsym->type() == elfcpp::STT_FUNC
6675                  && !gsym->is_undefined()
6676                  && ((psymval->value(arm_relobj, 0) & 1) != 0)));
6677         }
6678     }
6679   else
6680     {
6681       // This is a local symbol.  Determine if the final target is THUMB.
6682       target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
6683     }
6684
6685   // Strip LSB if this points to a THUMB target.
6686   if (target_is_thumb
6687       && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
6688       && ((psymval->value(arm_relobj, 0) & 1) != 0))
6689     {
6690       Arm_address stripped_value =
6691         psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
6692       symval.set_output_value(stripped_value);
6693       psymval = &symval;
6694     } 
6695
6696   // Get the symbol value.
6697   Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
6698
6699   // Owing to pipelining, the PC relative branches below actually skip
6700   // two instructions when the branch offset is 0.
6701   Arm_address destination;
6702   switch (r_type)
6703     {
6704     case elfcpp::R_ARM_CALL:
6705     case elfcpp::R_ARM_JUMP24:
6706     case elfcpp::R_ARM_PLT32:
6707       // ARM branches.
6708       destination = value + addend + 8;
6709       break;
6710     case elfcpp::R_ARM_THM_CALL:
6711     case elfcpp::R_ARM_THM_XPC22:
6712     case elfcpp::R_ARM_THM_JUMP24:
6713     case elfcpp::R_ARM_THM_JUMP19:
6714       // THUMB branches.
6715       destination = value + addend + 4;
6716       break;
6717     default:
6718       gold_unreachable();
6719     }
6720
6721   Stub_type stub_type =
6722     Reloc_stub::stub_type_for_reloc(r_type, address, destination,
6723                                     target_is_thumb);
6724
6725   // This reloc does not need a stub.
6726   if (stub_type == arm_stub_none)
6727     return;
6728
6729   // Try looking up an existing stub from a stub table.
6730   Stub_table<big_endian>* stub_table = 
6731     arm_relobj->stub_table(relinfo->data_shndx);
6732   gold_assert(stub_table != NULL);
6733    
6734   // Locate stub by destination.
6735   Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
6736
6737   // Create a stub if there is not one already
6738   Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
6739   if (stub == NULL)
6740     {
6741       // create a new stub and add it to stub table.
6742       stub = this->stub_factory().make_reloc_stub(stub_type);
6743       stub_table->add_reloc_stub(stub, stub_key);
6744     }
6745
6746   // Record the destination address.
6747   stub->set_destination_address(destination
6748                                 | (target_is_thumb ? 1 : 0));
6749 }
6750
6751 // This function scans a relocation sections for stub generation.
6752 // The template parameter Relocate must be a class type which provides
6753 // a single function, relocate(), which implements the machine
6754 // specific part of a relocation.
6755
6756 // BIG_ENDIAN is the endianness of the data.  SH_TYPE is the section type:
6757 // SHT_REL or SHT_RELA.
6758
6759 // PRELOCS points to the relocation data.  RELOC_COUNT is the number
6760 // of relocs.  OUTPUT_SECTION is the output section.
6761 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
6762 // mapped to output offsets.
6763
6764 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
6765 // VIEW_SIZE is the size.  These refer to the input section, unless
6766 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
6767 // the output section.
6768
6769 template<bool big_endian>
6770 template<int sh_type>
6771 void inline
6772 Target_arm<big_endian>::scan_reloc_section_for_stubs(
6773     const Relocate_info<32, big_endian>* relinfo,
6774     const unsigned char* prelocs,
6775     size_t reloc_count,
6776     Output_section* output_section,
6777     bool needs_special_offset_handling,
6778     const unsigned char* view,
6779     elfcpp::Elf_types<32>::Elf_Addr view_address,
6780     section_size_type)
6781 {
6782   typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
6783   const int reloc_size =
6784     Reloc_types<sh_type, 32, big_endian>::reloc_size;
6785
6786   Arm_relobj<big_endian>* arm_object =
6787     Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
6788   unsigned int local_count = arm_object->local_symbol_count();
6789
6790   Comdat_behavior comdat_behavior = CB_UNDETERMINED;
6791
6792   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6793     {
6794       Reltype reloc(prelocs);
6795
6796       typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
6797       unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6798       unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
6799
6800       r_type = this->get_real_reloc_type(r_type);
6801
6802       // Only a few relocation types need stubs.
6803       if ((r_type != elfcpp::R_ARM_CALL)
6804          && (r_type != elfcpp::R_ARM_JUMP24)
6805          && (r_type != elfcpp::R_ARM_PLT32)
6806          && (r_type != elfcpp::R_ARM_THM_CALL)
6807          && (r_type != elfcpp::R_ARM_THM_XPC22)
6808          && (r_type != elfcpp::R_ARM_THM_JUMP24)
6809          && (r_type != elfcpp::R_ARM_THM_JUMP19))
6810         continue;
6811
6812       section_offset_type offset =
6813         convert_to_section_size_type(reloc.get_r_offset());
6814
6815       if (needs_special_offset_handling)
6816         {
6817           offset = output_section->output_offset(relinfo->object,
6818                                                  relinfo->data_shndx,
6819                                                  offset);
6820           if (offset == -1)
6821             continue;
6822         }
6823
6824       // Get the addend.
6825       Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
6826       elfcpp::Elf_types<32>::Elf_Swxword addend =
6827         stub_addend_reader(r_type, view + offset, reloc);
6828
6829       const Sized_symbol<32>* sym;
6830
6831       Symbol_value<32> symval;
6832       const Symbol_value<32> *psymval;
6833       if (r_sym < local_count)
6834         {
6835           sym = NULL;
6836           psymval = arm_object->local_symbol(r_sym);
6837
6838           // If the local symbol belongs to a section we are discarding,
6839           // and that section is a debug section, try to find the
6840           // corresponding kept section and map this symbol to its
6841           // counterpart in the kept section.  The symbol must not 
6842           // correspond to a section we are folding.
6843           bool is_ordinary;
6844           unsigned int shndx = psymval->input_shndx(&is_ordinary);
6845           if (is_ordinary
6846               && shndx != elfcpp::SHN_UNDEF
6847               && !arm_object->is_section_included(shndx) 
6848               && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
6849             {
6850               if (comdat_behavior == CB_UNDETERMINED)
6851                 {
6852                   std::string name =
6853                     arm_object->section_name(relinfo->data_shndx);
6854                   comdat_behavior = get_comdat_behavior(name.c_str());
6855                 }
6856               if (comdat_behavior == CB_PRETEND)
6857                 {
6858                   bool found;
6859                   typename elfcpp::Elf_types<32>::Elf_Addr value =
6860                     arm_object->map_to_kept_section(shndx, &found);
6861                   if (found)
6862                     symval.set_output_value(value + psymval->input_value());
6863                   else
6864                     symval.set_output_value(0);
6865                 }
6866               else
6867                 {
6868                   symval.set_output_value(0);
6869                 }
6870               symval.set_no_output_symtab_entry();
6871               psymval = &symval;
6872             }
6873         }
6874       else
6875         {
6876           const Symbol* gsym = arm_object->global_symbol(r_sym);
6877           gold_assert(gsym != NULL);
6878           if (gsym->is_forwarder())
6879             gsym = relinfo->symtab->resolve_forwards(gsym);
6880
6881           sym = static_cast<const Sized_symbol<32>*>(gsym);
6882           if (sym->has_symtab_index())
6883             symval.set_output_symtab_index(sym->symtab_index());
6884           else
6885             symval.set_no_output_symtab_entry();
6886
6887           // We need to compute the would-be final value of this global
6888           // symbol.
6889           const Symbol_table* symtab = relinfo->symtab;
6890           const Sized_symbol<32>* sized_symbol =
6891             symtab->get_sized_symbol<32>(gsym);
6892           Symbol_table::Compute_final_value_status status;
6893           Arm_address value =
6894             symtab->compute_final_value<32>(sized_symbol, &status);
6895
6896           // Skip this if the symbol has not output section.
6897           if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
6898             continue;
6899
6900           symval.set_output_value(value);
6901           psymval = &symval;
6902         }
6903
6904       // If symbol is a section symbol, we don't know the actual type of
6905       // destination.  Give up.
6906       if (psymval->is_section_symbol())
6907         continue;
6908
6909       this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
6910                                 addend, view_address + offset);
6911     }
6912 }
6913
6914 // Scan an input section for stub generation.
6915
6916 template<bool big_endian>
6917 void
6918 Target_arm<big_endian>::scan_section_for_stubs(
6919     const Relocate_info<32, big_endian>* relinfo,
6920     unsigned int sh_type,
6921     const unsigned char* prelocs,
6922     size_t reloc_count,
6923     Output_section* output_section,
6924     bool needs_special_offset_handling,
6925     const unsigned char* view,
6926     Arm_address view_address,
6927     section_size_type view_size)
6928 {
6929   if (sh_type == elfcpp::SHT_REL)
6930     this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
6931         relinfo,
6932         prelocs,
6933         reloc_count,
6934         output_section,
6935         needs_special_offset_handling,
6936         view,
6937         view_address,
6938         view_size);
6939   else if (sh_type == elfcpp::SHT_RELA)
6940     // We do not support RELA type relocations yet.  This is provided for
6941     // completeness.
6942     this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
6943         relinfo,
6944         prelocs,
6945         reloc_count,
6946         output_section,
6947         needs_special_offset_handling,
6948         view,
6949         view_address,
6950         view_size);
6951   else
6952     gold_unreachable();
6953 }
6954
6955 // Group input sections for stub generation.
6956 //
6957 // We goup input sections in an output sections so that the total size,
6958 // including any padding space due to alignment is smaller than GROUP_SIZE
6959 // unless the only input section in group is bigger than GROUP_SIZE already.
6960 // Then an ARM stub table is created to follow the last input section
6961 // in group.  For each group an ARM stub table is created an is placed
6962 // after the last group.  If STUB_ALWATS_AFTER_BRANCH is false, we further
6963 // extend the group after the stub table.
6964
6965 template<bool big_endian>
6966 void
6967 Target_arm<big_endian>::group_sections(
6968     Layout* layout,
6969     section_size_type group_size,
6970     bool stubs_always_after_branch)
6971 {
6972   // Group input sections and insert stub table
6973   Layout::Section_list section_list;
6974   layout->get_allocated_sections(&section_list);
6975   for (Layout::Section_list::const_iterator p = section_list.begin();
6976        p != section_list.end();
6977        ++p)
6978     {
6979       Arm_output_section<big_endian>* output_section =
6980         Arm_output_section<big_endian>::as_arm_output_section(*p);
6981       output_section->group_sections(group_size, stubs_always_after_branch,
6982                                      this);
6983     }
6984 }
6985
6986 // Relaxation hook.  This is where we do stub generation.
6987
6988 template<bool big_endian>
6989 bool
6990 Target_arm<big_endian>::do_relax(
6991     int pass,
6992     const Input_objects* input_objects,
6993     Symbol_table* symtab,
6994     Layout* layout)
6995 {
6996   // No need to generate stubs if this is a relocatable link.
6997   gold_assert(!parameters->options().relocatable());
6998
6999   // If this is the first pass, we need to group input sections into
7000   // stub groups.
7001   if (pass == 1)
7002     {
7003       // Determine the stub group size.  The group size is the absolute
7004       // value of the parameter --stub-group-size.  If --stub-group-size
7005       // is passed a negative value, we restict stubs to be always after
7006       // the stubbed branches.
7007       int32_t stub_group_size_param =
7008         parameters->options().stub_group_size();
7009       bool stubs_always_after_branch = stub_group_size_param < 0;
7010       section_size_type stub_group_size = abs(stub_group_size_param);
7011
7012       if (stub_group_size == 1)
7013         {
7014           // Default value.
7015           // Thumb branch range is +-4MB has to be used as the default
7016           // maximum size (a given section can contain both ARM and Thumb
7017           // code, so the worst case has to be taken into account).
7018           //
7019           // This value is 24K less than that, which allows for 2025
7020           // 12-byte stubs.  If we exceed that, then we will fail to link.
7021           // The user will have to relink with an explicit group size
7022           // option.
7023           stub_group_size = 4170000;
7024         }
7025
7026       group_sections(layout, stub_group_size, stubs_always_after_branch);
7027     }
7028
7029   // clear changed flags for all stub_tables
7030   typedef typename Stub_table_list::iterator Stub_table_iterator;
7031   for (Stub_table_iterator sp = this->stub_tables_.begin();
7032        sp != this->stub_tables_.end();
7033        ++sp)
7034     (*sp)->set_has_been_changed(false);
7035
7036   // scan relocs for stubs
7037   for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
7038        op != input_objects->relobj_end();
7039        ++op)
7040     {
7041       Arm_relobj<big_endian>* arm_relobj =
7042         Arm_relobj<big_endian>::as_arm_relobj(*op);
7043       arm_relobj->scan_sections_for_stubs(this, symtab, layout);
7044     }
7045
7046   bool any_stub_table_changed = false;
7047   for (Stub_table_iterator sp = this->stub_tables_.begin();
7048        (sp != this->stub_tables_.end()) && !any_stub_table_changed;
7049        ++sp)
7050     {
7051       if ((*sp)->has_been_changed())
7052         any_stub_table_changed = true;
7053     }
7054
7055   return any_stub_table_changed;
7056 }
7057
7058 // Relocate a stub.
7059
7060 template<bool big_endian>
7061 void
7062 Target_arm<big_endian>::relocate_stub(
7063     Reloc_stub* stub,
7064     const Relocate_info<32, big_endian>* relinfo,
7065     Output_section* output_section,
7066     unsigned char* view,
7067     Arm_address address,
7068     section_size_type view_size)
7069 {
7070   Relocate relocate;
7071   const Stub_template* stub_template = stub->stub_template();
7072   for (size_t i = 0; i < stub_template->reloc_count(); i++)
7073     {
7074       size_t reloc_insn_index = stub_template->reloc_insn_index(i);
7075       const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
7076
7077       unsigned int r_type = insn->r_type();
7078       section_size_type reloc_offset = stub_template->reloc_offset(i);
7079       section_size_type reloc_size = insn->size();
7080       gold_assert(reloc_offset + reloc_size <= view_size);
7081
7082       // This is the address of the stub destination.
7083       Arm_address target = stub->reloc_target(i);
7084       Symbol_value<32> symval;
7085       symval.set_output_value(target);
7086
7087       // Synthesize a fake reloc just in case.  We don't have a symbol so
7088       // we use 0.
7089       unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
7090       memset(reloc_buffer, 0, sizeof(reloc_buffer));
7091       elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
7092       reloc_write.put_r_offset(reloc_offset);
7093       reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
7094       elfcpp::Rel<32, big_endian> rel(reloc_buffer);
7095
7096       relocate.relocate(relinfo, this, output_section,
7097                         this->fake_relnum_for_stubs, rel, r_type,
7098                         NULL, &symval, view + reloc_offset,
7099                         address + reloc_offset, reloc_size);
7100     }
7101 }
7102
7103 // Determine whether an object attribute tag takes an integer, a
7104 // string or both.
7105
7106 template<bool big_endian>
7107 int
7108 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
7109 {
7110   if (tag == Object_attribute::Tag_compatibility)
7111     return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7112             | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
7113   else if (tag == elfcpp::Tag_nodefaults)
7114     return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
7115             | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
7116   else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
7117     return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
7118   else if (tag < 32)
7119     return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
7120   else
7121     return ((tag & 1) != 0
7122             ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
7123             : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
7124 }
7125
7126 // Reorder attributes.
7127 //
7128 // The ABI defines that Tag_conformance should be emitted first, and that
7129 // Tag_nodefaults should be second (if either is defined).  This sets those
7130 // two positions, and bumps up the position of all the remaining tags to
7131 // compensate.
7132
7133 template<bool big_endian>
7134 int
7135 Target_arm<big_endian>::do_attributes_order(int num) const
7136 {
7137   // Reorder the known object attributes in output.  We want to move
7138   // Tag_conformance to position 4 and Tag_conformance to position 5
7139   // and shift eveything between 4 .. Tag_conformance - 1 to make room.
7140   if (num == 4)
7141     return elfcpp::Tag_conformance;
7142   if (num == 5)
7143     return elfcpp::Tag_nodefaults;
7144   if ((num - 2) < elfcpp::Tag_nodefaults)
7145     return num - 2;
7146   if ((num - 1) < elfcpp::Tag_conformance)
7147     return num - 1;
7148   return num;
7149 }
7150
7151 template<bool big_endian>
7152 class Target_selector_arm : public Target_selector
7153 {
7154  public:
7155   Target_selector_arm()
7156     : Target_selector(elfcpp::EM_ARM, 32, big_endian,
7157                       (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
7158   { }
7159
7160   Target*
7161   do_instantiate_target()
7162   { return new Target_arm<big_endian>(); }
7163 };
7164
7165 Target_selector_arm<false> target_selector_arm;
7166 Target_selector_arm<true> target_selector_armbe;
7167
7168 } // End anonymous namespace.