OSDN Git Service

2009-11-03 Doug Kwan <dougkwan@google.com>
[pf3gnuchains/pf3gnuchains3x.git] / gold / arm.cc
1 // arm.cc -- arm target support for gold.
2
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6 // 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
50 namespace
51 {
52
53 using namespace gold;
54
55 template<bool big_endian>
56 class Output_data_plt_arm;
57
58 template<bool big_endian>
59 class Stub_table;
60
61 template<bool big_endian>
62 class Arm_input_section;
63
64 template<bool big_endian>
65 class Arm_output_section;
66
67 template<bool big_endian>
68 class Arm_relobj;
69
70 template<bool big_endian>
71 class Target_arm;
72
73 // For convenience.
74 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
75
76 // Maximum branch offsets for ARM, THUMB and THUMB2.
77 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
78 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
79 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
80 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
81 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
82 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
83
84 // The arm target class.
85 //
86 // This is a very simple port of gold for ARM-EABI.  It is intended for
87 // supporting Android only for the time being.  Only these relocation types
88 // are supported.
89 //
90 // R_ARM_NONE
91 // R_ARM_ABS32
92 // R_ARM_ABS32_NOI
93 // R_ARM_ABS16
94 // R_ARM_ABS12
95 // R_ARM_ABS8
96 // R_ARM_THM_ABS5
97 // R_ARM_BASE_ABS
98 // R_ARM_REL32
99 // R_ARM_THM_CALL
100 // R_ARM_COPY
101 // R_ARM_GLOB_DAT
102 // R_ARM_BASE_PREL
103 // R_ARM_JUMP_SLOT
104 // R_ARM_RELATIVE
105 // R_ARM_GOTOFF32
106 // R_ARM_GOT_BREL
107 // R_ARM_GOT_PREL
108 // R_ARM_PLT32
109 // R_ARM_CALL
110 // R_ARM_JUMP24
111 // R_ARM_TARGET1
112 // R_ARM_PREL31
113 // R_ARM_ABS8
114 // R_ARM_MOVW_ABS_NC
115 // R_ARM_MOVT_ABS
116 // R_ARM_THM_MOVW_ABS_NC
117 // R_ARM_THM_MOVT_ABS
118 // R_ARM_MOVW_PREL_NC
119 // R_ARM_MOVT_PREL
120 // R_ARM_THM_MOVW_PREL_NC
121 // R_ARM_THM_MOVT_PREL
122 // 
123 // TODOs:
124 // - Generate various branch stubs.
125 // - Support interworking.
126 // - Define section symbols __exidx_start and __exidx_stop.
127 // - Support more relocation types as needed. 
128 // - Make PLTs more flexible for different architecture features like
129 //   Thumb-2 and BE8.
130 // There are probably a lot more.
131
132 // Instruction template class.  This class is similar to the insn_sequence
133 // struct in bfd/elf32-arm.c.
134
135 class Insn_template
136 {
137  public:
138   // Types of instruction templates.
139   enum Type
140     {
141       THUMB16_TYPE = 1,
142       THUMB32_TYPE,
143       ARM_TYPE,
144       DATA_TYPE
145     };
146
147   // Factory methods to create instrunction templates in different formats.
148
149   static const Insn_template
150   thumb16_insn(uint32_t data)
151   { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); } 
152
153   // A bit of a hack.  A Thumb conditional branch, in which the proper
154   // condition is inserted when we build the stub.
155   static const Insn_template
156   thumb16_bcond_insn(uint32_t data)
157   { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 1); } 
158
159   static const Insn_template
160   thumb32_insn(uint32_t data)
161   { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); } 
162
163   static const Insn_template
164   thumb32_b_insn(uint32_t data, int reloc_addend)
165   {
166     return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
167                          reloc_addend);
168   } 
169
170   static const Insn_template
171   arm_insn(uint32_t data)
172   { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
173
174   static const Insn_template
175   arm_rel_insn(unsigned data, int reloc_addend)
176   { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
177
178   static const Insn_template
179   data_word(unsigned data, unsigned int r_type, int reloc_addend)
180   { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); } 
181
182   // Accessors.  This class is used for read-only objects so no modifiers
183   // are provided.
184
185   uint32_t
186   data() const
187   { return this->data_; }
188
189   // Return the instruction sequence type of this.
190   Type
191   type() const
192   { return this->type_; }
193
194   // Return the ARM relocation type of this.
195   unsigned int
196   r_type() const
197   { return this->r_type_; }
198
199   int32_t
200   reloc_addend() const
201   { return this->reloc_addend_; }
202
203   // Return size of instrunction template in bytes.
204   size_t
205   size() const;
206
207   // Return byte-alignment of instrunction template.
208   unsigned
209   alignment() const;
210
211  private:
212   // We make the constructor private to ensure that only the factory
213   // methods are used.
214   inline
215   Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
216     : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
217   { }
218
219   // Instruction specific data.  This is used to store information like
220   // some of the instruction bits.
221   uint32_t data_;
222   // Instruction template type.
223   Type type_;
224   // Relocation type if there is a relocation or R_ARM_NONE otherwise.
225   unsigned int r_type_;
226   // Relocation addend.
227   int32_t reloc_addend_;
228 };
229
230 // Macro for generating code to stub types. One entry per long/short
231 // branch stub
232
233 #define DEF_STUBS \
234   DEF_STUB(long_branch_any_any) \
235   DEF_STUB(long_branch_v4t_arm_thumb) \
236   DEF_STUB(long_branch_thumb_only) \
237   DEF_STUB(long_branch_v4t_thumb_thumb) \
238   DEF_STUB(long_branch_v4t_thumb_arm) \
239   DEF_STUB(short_branch_v4t_thumb_arm) \
240   DEF_STUB(long_branch_any_arm_pic) \
241   DEF_STUB(long_branch_any_thumb_pic) \
242   DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
243   DEF_STUB(long_branch_v4t_arm_thumb_pic) \
244   DEF_STUB(long_branch_v4t_thumb_arm_pic) \
245   DEF_STUB(long_branch_thumb_only_pic) \
246   DEF_STUB(a8_veneer_b_cond) \
247   DEF_STUB(a8_veneer_b) \
248   DEF_STUB(a8_veneer_bl) \
249   DEF_STUB(a8_veneer_blx)
250
251 // Stub types.
252
253 #define DEF_STUB(x) arm_stub_##x,
254 typedef enum
255   {
256     arm_stub_none,
257     DEF_STUBS
258
259     // First reloc stub type.
260     arm_stub_reloc_first = arm_stub_long_branch_any_any,
261     // Last  reloc stub type.
262     arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
263
264     // First Cortex-A8 stub type.
265     arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
266     // Last Cortex-A8 stub type.
267     arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
268     
269     // Last stub type.
270     arm_stub_type_last = arm_stub_a8_veneer_blx
271   } Stub_type;
272 #undef DEF_STUB
273
274 // Stub template class.  Templates are meant to be read-only objects.
275 // A stub template for a stub type contains all read-only attributes
276 // common to all stubs of the same type.
277
278 class Stub_template
279 {
280  public:
281   Stub_template(Stub_type, const Insn_template*, size_t);
282
283   ~Stub_template()
284   { }
285
286   // Return stub type.
287   Stub_type
288   type() const
289   { return this->type_; }
290
291   // Return an array of instruction templates.
292   const Insn_template*
293   insns() const
294   { return this->insns_; }
295
296   // Return size of template in number of instructions.
297   size_t
298   insn_count() const
299   { return this->insn_count_; }
300
301   // Return size of template in bytes.
302   size_t
303   size() const
304   { return this->size_; }
305
306   // Return alignment of the stub template.
307   unsigned
308   alignment() const
309   { return this->alignment_; }
310   
311   // Return whether entry point is in thumb mode.
312   bool
313   entry_in_thumb_mode() const
314   { return this->entry_in_thumb_mode_; }
315
316   // Return number of relocations in this template.
317   size_t
318   reloc_count() const
319   { return this->relocs_.size(); }
320
321   // Return index of the I-th instruction with relocation.
322   size_t
323   reloc_insn_index(size_t i) const
324   {
325     gold_assert(i < this->relocs_.size());
326     return this->relocs_[i].first;
327   }
328
329   // Return the offset of the I-th instruction with relocation from the
330   // beginning of the stub.
331   section_size_type
332   reloc_offset(size_t i) const
333   {
334     gold_assert(i < this->relocs_.size());
335     return this->relocs_[i].second;
336   }
337
338  private:
339   // This contains information about an instruction template with a relocation
340   // and its offset from start of stub.
341   typedef std::pair<size_t, section_size_type> Reloc;
342
343   // A Stub_template may not be copied.  We want to share templates as much
344   // as possible.
345   Stub_template(const Stub_template&);
346   Stub_template& operator=(const Stub_template&);
347   
348   // Stub type.
349   Stub_type type_;
350   // Points to an array of Insn_templates.
351   const Insn_template* insns_;
352   // Number of Insn_templates in insns_[].
353   size_t insn_count_;
354   // Size of templated instructions in bytes.
355   size_t size_;
356   // Alignment of templated instructions.
357   unsigned alignment_;
358   // Flag to indicate if entry is in thumb mode.
359   bool entry_in_thumb_mode_;
360   // A table of reloc instruction indices and offsets.  We can find these by
361   // looking at the instruction templates but we pre-compute and then stash
362   // them here for speed. 
363   std::vector<Reloc> relocs_;
364 };
365
366 //
367 // A class for code stubs.  This is a base class for different type of
368 // stubs used in the ARM target.
369 //
370
371 class Stub
372 {
373  private:
374   static const section_offset_type invalid_offset =
375     static_cast<section_offset_type>(-1);
376
377  public:
378   Stub(const Stub_template* stub_template)
379     : stub_template_(stub_template), offset_(invalid_offset)
380   { }
381
382   virtual
383    ~Stub()
384   { }
385
386   // Return the stub template.
387   const Stub_template*
388   stub_template() const
389   { return this->stub_template_; }
390
391   // Return offset of code stub from beginning of its containing stub table.
392   section_offset_type
393   offset() const
394   {
395     gold_assert(this->offset_ != invalid_offset);
396     return this->offset_;
397   }
398
399   // Set offset of code stub from beginning of its containing stub table.
400   void
401   set_offset(section_offset_type offset)
402   { this->offset_ = offset; }
403   
404   // Return the relocation target address of the i-th relocation in the
405   // stub.  This must be defined in a child class.
406   Arm_address
407   reloc_target(size_t i)
408   { return this->do_reloc_target(i); }
409
410   // Write a stub at output VIEW.  BIG_ENDIAN select how a stub is written.
411   void
412   write(unsigned char* view, section_size_type view_size, bool big_endian)
413   { this->do_write(view, view_size, big_endian); }
414
415  protected:
416   // This must be defined in the child class.
417   virtual Arm_address
418   do_reloc_target(size_t) = 0;
419
420   // This must be defined in the child class.
421   virtual void
422   do_write(unsigned char*, section_size_type, bool) = 0;
423   
424  private:
425   // Its template.
426   const Stub_template* stub_template_;
427   // Offset within the section of containing this stub.
428   section_offset_type offset_;
429 };
430
431 // Reloc stub class.  These are stubs we use to fix up relocation because
432 // of limited branch ranges.
433
434 class Reloc_stub : public Stub
435 {
436  public:
437   static const unsigned int invalid_index = static_cast<unsigned int>(-1);
438   // We assume we never jump to this address.
439   static const Arm_address invalid_address = static_cast<Arm_address>(-1);
440
441   // Return destination address.
442   Arm_address
443   destination_address() const
444   {
445     gold_assert(this->destination_address_ != this->invalid_address);
446     return this->destination_address_;
447   }
448
449   // Set destination address.
450   void
451   set_destination_address(Arm_address address)
452   {
453     gold_assert(address != this->invalid_address);
454     this->destination_address_ = address;
455   }
456
457   // Reset destination address.
458   void
459   reset_destination_address()
460   { this->destination_address_ = this->invalid_address; }
461
462   // Determine stub type for a branch of a relocation of R_TYPE going
463   // from BRANCH_ADDRESS to BRANCH_TARGET.  If TARGET_IS_THUMB is set,
464   // the branch target is a thumb instruction.  TARGET is used for look
465   // up ARM-specific linker settings.
466   static Stub_type
467   stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
468                       Arm_address branch_target, bool target_is_thumb);
469
470   // Reloc_stub key.  A key is logically a triplet of a stub type, a symbol
471   // and an addend.  Since we treat global and local symbol differently, we
472   // use a Symbol object for a global symbol and a object-index pair for
473   // a local symbol.
474   class Key
475   {
476    public:
477     // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
478     // R_SYM.  Otherwise, this is a local symbol and RELOBJ must non-NULL
479     // and R_SYM must not be invalid_index.
480     Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
481         unsigned int r_sym, int32_t addend)
482       : stub_type_(stub_type), addend_(addend)
483     {
484       if (symbol != NULL)
485         {
486           this->r_sym_ = Reloc_stub::invalid_index;
487           this->u_.symbol = symbol;
488         }
489       else
490         {
491           gold_assert(relobj != NULL && r_sym != invalid_index);
492           this->r_sym_ = r_sym;
493           this->u_.relobj = relobj;
494         }
495     }
496
497     ~Key()
498     { }
499
500     // Accessors: Keys are meant to be read-only object so no modifiers are
501     // provided.
502
503     // Return stub type.
504     Stub_type
505     stub_type() const
506     { return this->stub_type_; }
507
508     // Return the local symbol index or invalid_index.
509     unsigned int
510     r_sym() const
511     { return this->r_sym_; }
512
513     // Return the symbol if there is one.
514     const Symbol*
515     symbol() const
516     { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
517
518     // Return the relobj if there is one.
519     const Relobj*
520     relobj() const
521     { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
522
523     // Whether this equals to another key k.
524     bool
525     eq(const Key& k) const 
526     {
527       return ((this->stub_type_ == k.stub_type_)
528               && (this->r_sym_ == k.r_sym_)
529               && ((this->r_sym_ != Reloc_stub::invalid_index)
530                   ? (this->u_.relobj == k.u_.relobj)
531                   : (this->u_.symbol == k.u_.symbol))
532               && (this->addend_ == k.addend_));
533     }
534
535     // Return a hash value.
536     size_t
537     hash_value() const
538     {
539       return (this->stub_type_
540               ^ this->r_sym_
541               ^ gold::string_hash<char>(
542                     (this->r_sym_ != Reloc_stub::invalid_index)
543                     ? this->u_.relobj->name().c_str()
544                     : this->u_.symbol->name())
545               ^ this->addend_);
546     }
547
548     // Functors for STL associative containers.
549     struct hash
550     {
551       size_t
552       operator()(const Key& k) const
553       { return k.hash_value(); }
554     };
555
556     struct equal_to
557     {
558       bool
559       operator()(const Key& k1, const Key& k2) const
560       { return k1.eq(k2); }
561     };
562
563     // Name of key.  This is mainly for debugging.
564     std::string
565     name() const;
566
567    private:
568     // Stub type.
569     Stub_type stub_type_;
570     // If this is a local symbol, this is the index in the defining object.
571     // Otherwise, it is invalid_index for a global symbol.
572     unsigned int r_sym_;
573     // If r_sym_ is invalid index.  This points to a global symbol.
574     // Otherwise, this points a relobj.  We used the unsized and target
575     // independent Symbol and Relobj classes instead of Arm_symbol and  
576     // Arm_relobj.  This is done to avoid making the stub class a template
577     // as most of the stub machinery is endianity-neutral.  However, it
578     // may require a bit of casting done by users of this class.
579     union
580     {
581       const Symbol* symbol;
582       const Relobj* relobj;
583     } u_;
584     // Addend associated with a reloc.
585     int32_t addend_;
586   };
587
588  protected:
589   // Reloc_stubs are created via a stub factory.  So these are protected.
590   Reloc_stub(const Stub_template* stub_template)
591     : Stub(stub_template), destination_address_(invalid_address)
592   { }
593
594   ~Reloc_stub()
595   { }
596
597   friend class Stub_factory;
598
599  private:
600   // Return the relocation target address of the i-th relocation in the
601   // stub.
602   Arm_address
603   do_reloc_target(size_t i)
604   {
605     // All reloc stub have only one relocation.
606     gold_assert(i == 0);
607     return this->destination_address_;
608   }
609
610   // A template to implement do_write below.
611   template<bool big_endian>
612   void inline
613   do_fixed_endian_write(unsigned char*, section_size_type);
614
615   // Write a stub.
616   void
617   do_write(unsigned char* view, section_size_type view_size, bool big_endian);
618
619   // Address of destination.
620   Arm_address destination_address_;
621 };
622
623 // Stub factory class.
624
625 class Stub_factory
626 {
627  public:
628   // Return the unique instance of this class.
629   static const Stub_factory&
630   get_instance()
631   {
632     static Stub_factory singleton;
633     return singleton;
634   }
635
636   // Make a relocation stub.
637   Reloc_stub*
638   make_reloc_stub(Stub_type stub_type) const
639   {
640     gold_assert(stub_type >= arm_stub_reloc_first
641                 && stub_type <= arm_stub_reloc_last);
642     return new Reloc_stub(this->stub_templates_[stub_type]);
643   }
644
645  private:
646   // Constructor and destructor are protected since we only return a single
647   // instance created in Stub_factory::get_instance().
648   
649   Stub_factory();
650
651   // A Stub_factory may not be copied since it is a singleton.
652   Stub_factory(const Stub_factory&);
653   Stub_factory& operator=(Stub_factory&);
654   
655   // Stub templates.  These are initialized in the constructor.
656   const Stub_template* stub_templates_[arm_stub_type_last+1];
657 };
658
659 // A class to hold stubs for the ARM target.
660
661 template<bool big_endian>
662 class Stub_table : public Output_data
663 {
664  public:
665   Stub_table(Arm_input_section<big_endian>* owner)
666     : Output_data(), addralign_(1), owner_(owner), has_been_changed_(false),
667       reloc_stubs_()
668   { }
669
670   ~Stub_table()
671   { }
672
673   // Owner of this stub table.
674   Arm_input_section<big_endian>*
675   owner() const
676   { return this->owner_; }
677
678   // Whether this stub table is empty.
679   bool
680   empty() const
681   { return this->reloc_stubs_.empty(); }
682
683   // Whether this has been changed.
684   bool
685   has_been_changed() const
686   { return this->has_been_changed_; }
687
688   // Set the has-been-changed flag.
689   void
690   set_has_been_changed(bool value)
691   { this->has_been_changed_ = value; }
692
693   // Return the current data size.
694   off_t
695   current_data_size() const
696   { return this->current_data_size_for_child(); }
697
698   // Add a STUB with using KEY.  Caller is reponsible for avoid adding
699   // if already a STUB with the same key has been added. 
700   void
701   add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key);
702
703   // Look up a relocation stub using KEY.  Return NULL if there is none.
704   Reloc_stub*
705   find_reloc_stub(const Reloc_stub::Key& key) const
706   {
707     typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
708     return (p != this->reloc_stubs_.end()) ? p->second : NULL;
709   }
710
711   // Relocate stubs in this stub table.
712   void
713   relocate_stubs(const Relocate_info<32, big_endian>*,
714                  Target_arm<big_endian>*, Output_section*,
715                  unsigned char*, Arm_address, section_size_type);
716
717  protected:
718   // Write out section contents.
719   void
720   do_write(Output_file*);
721  
722   // Return the required alignment.
723   uint64_t
724   do_addralign() const
725   { return this->addralign_; }
726
727   // Finalize data size.
728   void
729   set_final_data_size()
730   { this->set_data_size(this->current_data_size_for_child()); }
731
732   // Reset address and file offset.
733   void
734   do_reset_address_and_file_offset();
735
736  private:
737   // Unordered map of stubs.
738   typedef
739     Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
740                   Reloc_stub::Key::equal_to>
741     Reloc_stub_map;
742
743   // Address alignment
744   uint64_t addralign_;
745   // Owner of this stub table.
746   Arm_input_section<big_endian>* owner_;
747   // This is set to true during relaxiong if the size of the stub table
748   // has been changed.
749   bool has_been_changed_;
750   // The relocation stubs.
751   Reloc_stub_map reloc_stubs_;
752 };
753
754 // A class to wrap an ordinary input section containing executable code.
755
756 template<bool big_endian>
757 class Arm_input_section : public Output_relaxed_input_section
758 {
759  public:
760   Arm_input_section(Relobj* relobj, unsigned int shndx)
761     : Output_relaxed_input_section(relobj, shndx, 1),
762       original_addralign_(1), original_size_(0), stub_table_(NULL)
763   { }
764
765   ~Arm_input_section()
766   { }
767
768   // Initialize.
769   void
770   init();
771   
772   // Whether this is a stub table owner.
773   bool
774   is_stub_table_owner() const
775   { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
776
777   // Return the stub table.
778   Stub_table<big_endian>*
779   stub_table() const
780   { return this->stub_table_; }
781
782   // Set the stub_table.
783   void
784   set_stub_table(Stub_table<big_endian>* stub_table)
785   { this->stub_table_ = stub_table; }
786
787   // Downcast a base pointer to an Arm_input_section pointer.  This is
788   // not type-safe but we only use Arm_input_section not the base class.
789   static Arm_input_section<big_endian>*
790   as_arm_input_section(Output_relaxed_input_section* poris)
791   { return static_cast<Arm_input_section<big_endian>*>(poris); }
792
793  protected:
794   // Write data to output file.
795   void
796   do_write(Output_file*);
797
798   // Return required alignment of this.
799   uint64_t
800   do_addralign() const
801   {
802     if (this->is_stub_table_owner())
803       return std::max(this->stub_table_->addralign(),
804                       this->original_addralign_);
805     else
806       return this->original_addralign_;
807   }
808
809   // Finalize data size.
810   void
811   set_final_data_size();
812
813   // Reset address and file offset.
814   void
815   do_reset_address_and_file_offset();
816
817   // Output offset.
818   bool
819   do_output_offset(const Relobj* object, unsigned int shndx,
820                    section_offset_type offset,
821                    section_offset_type* poutput) const
822   {
823     if ((object == this->relobj())
824         && (shndx == this->shndx())
825         && (offset >= 0)
826         && (convert_types<uint64_t, section_offset_type>(offset)
827             <= this->original_size_))
828       {
829         *poutput = offset;
830         return true;
831       }
832     else
833       return false;
834   }
835
836  private:
837   // Copying is not allowed.
838   Arm_input_section(const Arm_input_section&);
839   Arm_input_section& operator=(const Arm_input_section&);
840
841   // Address alignment of the original input section.
842   uint64_t original_addralign_;
843   // Section size of the original input section.
844   uint64_t original_size_;
845   // Stub table.
846   Stub_table<big_endian>* stub_table_;
847 };
848
849 // Arm output section class.  This is defined mainly to add a number of
850 // stub generation methods.
851
852 template<bool big_endian>
853 class Arm_output_section : public Output_section
854 {
855  public:
856   Arm_output_section(const char* name, elfcpp::Elf_Word type,
857                      elfcpp::Elf_Xword flags)
858     : Output_section(name, type, flags)
859   { }
860
861   ~Arm_output_section()
862   { }
863   
864   // Group input sections for stub generation.
865   void
866   group_sections(section_size_type, bool, Target_arm<big_endian>*);
867
868   // Downcast a base pointer to an Arm_output_section pointer.  This is
869   // not type-safe but we only use Arm_output_section not the base class.
870   static Arm_output_section<big_endian>*
871   as_arm_output_section(Output_section* os)
872   { return static_cast<Arm_output_section<big_endian>*>(os); }
873
874  private:
875   // For convenience.
876   typedef Output_section::Input_section Input_section;
877   typedef Output_section::Input_section_list Input_section_list;
878
879   // Create a stub group.
880   void create_stub_group(Input_section_list::const_iterator,
881                          Input_section_list::const_iterator,
882                          Input_section_list::const_iterator,
883                          Target_arm<big_endian>*,
884                          std::vector<Output_relaxed_input_section*>*);
885 };
886
887 // Arm_relobj class.
888
889 template<bool big_endian>
890 class Arm_relobj : public Sized_relobj<32, big_endian>
891 {
892  public:
893   static const Arm_address invalid_address = static_cast<Arm_address>(-1);
894
895   Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
896              const typename elfcpp::Ehdr<32, big_endian>& ehdr)
897     : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
898       stub_tables_(), local_symbol_is_thumb_function_()
899   { }
900
901   ~Arm_relobj()
902   { }
903  
904   // Return the stub table of the SHNDX-th section if there is one.
905   Stub_table<big_endian>*
906   stub_table(unsigned int shndx) const
907   {
908     gold_assert(shndx < this->stub_tables_.size());
909     return this->stub_tables_[shndx];
910   }
911
912   // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
913   void
914   set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
915   {
916     gold_assert(shndx < this->stub_tables_.size());
917     this->stub_tables_[shndx] = stub_table;
918   }
919
920   // Whether a local symbol is a THUMB function.  R_SYM is the symbol table
921   // index.  This is only valid after do_count_local_symbol is called.
922   bool
923   local_symbol_is_thumb_function(unsigned int r_sym) const
924   {
925     gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
926     return this->local_symbol_is_thumb_function_[r_sym];
927   }
928   
929   // Scan all relocation sections for stub generation.
930   void
931   scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
932                           const Layout*);
933
934   // Convert regular input section with index SHNDX to a relaxed section.
935   void
936   convert_input_section_to_relaxed_section(unsigned shndx)
937   {
938     // The stubs have relocations and we need to process them after writing
939     // out the stubs.  So relocation now must follow section write.
940     this->invalidate_section_offset(shndx);
941     this->set_relocs_must_follow_section_writes();
942   }
943
944   // Downcast a base pointer to an Arm_relobj pointer.  This is
945   // not type-safe but we only use Arm_relobj not the base class.
946   static Arm_relobj<big_endian>*
947   as_arm_relobj(Relobj* relobj)
948   { return static_cast<Arm_relobj<big_endian>*>(relobj); }
949
950   // Processor-specific flags in ELF file header.  This is valid only after
951   // reading symbols.
952   elfcpp::Elf_Word
953   processor_specific_flags() const
954   { return this->processor_specific_flags_; }
955
956  protected:
957   // Post constructor setup.
958   void
959   do_setup()
960   {
961     // Call parent's setup method.
962     Sized_relobj<32, big_endian>::do_setup();
963
964     // Initialize look-up tables.
965     Stub_table_list empty_stub_table_list(this->shnum(), NULL);
966     this->stub_tables_.swap(empty_stub_table_list);
967   }
968
969   // Count the local symbols.
970   void
971   do_count_local_symbols(Stringpool_template<char>*,
972                          Stringpool_template<char>*);
973
974   void
975   do_relocate_sections(const General_options& options,
976                        const Symbol_table* symtab, const Layout* layout,
977                        const unsigned char* pshdrs,
978                        typename Sized_relobj<32, big_endian>::Views* pivews);
979
980   // Read the symbol information.
981   void
982   do_read_symbols(Read_symbols_data* sd);
983
984  private:
985   // List of stub tables.
986   typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
987   Stub_table_list stub_tables_;
988   // Bit vector to tell if a local symbol is a thumb function or not.
989   // This is only valid after do_count_local_symbol is called.
990   std::vector<bool> local_symbol_is_thumb_function_;
991   // processor-specific flags in ELF file header.
992   elfcpp::Elf_Word processor_specific_flags_;
993 };
994
995 // Arm_dynobj class.
996
997 template<bool big_endian>
998 class Arm_dynobj : public Sized_dynobj<32, big_endian>
999 {
1000  public:
1001   Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1002              const elfcpp::Ehdr<32, big_endian>& ehdr)
1003     : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1004       processor_specific_flags_(0)
1005   { }
1006  
1007   ~Arm_dynobj()
1008   { }
1009
1010   // Downcast a base pointer to an Arm_relobj pointer.  This is
1011   // not type-safe but we only use Arm_relobj not the base class.
1012   static Arm_dynobj<big_endian>*
1013   as_arm_dynobj(Dynobj* dynobj)
1014   { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1015
1016   // Processor-specific flags in ELF file header.  This is valid only after
1017   // reading symbols.
1018   elfcpp::Elf_Word
1019   processor_specific_flags() const
1020   { return this->processor_specific_flags_; }
1021
1022  protected:
1023   // Read the symbol information.
1024   void
1025   do_read_symbols(Read_symbols_data* sd);
1026
1027  private:
1028   // processor-specific flags in ELF file header.
1029   elfcpp::Elf_Word processor_specific_flags_;
1030 };
1031
1032 // Functor to read reloc addends during stub generation.
1033
1034 template<int sh_type, bool big_endian>
1035 struct Stub_addend_reader
1036 {
1037   // Return the addend for a relocation of a particular type.  Depending
1038   // on whether this is a REL or RELA relocation, read the addend from a
1039   // view or from a Reloc object.
1040   elfcpp::Elf_types<32>::Elf_Swxword
1041   operator()(
1042     unsigned int /* r_type */,
1043     const unsigned char* /* view */,
1044     const typename Reloc_types<sh_type,
1045                                32, big_endian>::Reloc& /* reloc */) const;
1046 };
1047
1048 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1049
1050 template<bool big_endian>
1051 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1052 {
1053   elfcpp::Elf_types<32>::Elf_Swxword
1054   operator()(
1055     unsigned int,
1056     const unsigned char*,
1057     const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1058 };
1059
1060 // Specialized Stub_addend_reader for RELA type relocation sections.
1061 // We currently do not handle RELA type relocation sections but it is trivial
1062 // to implement the addend reader.  This is provided for completeness and to
1063 // make it easier to add support for RELA relocation sections in the future.
1064
1065 template<bool big_endian>
1066 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1067 {
1068   elfcpp::Elf_types<32>::Elf_Swxword
1069   operator()(
1070     unsigned int,
1071     const unsigned char*,
1072     const typename Reloc_types<elfcpp::SHT_RELA, 32,
1073                                big_endian>::Reloc& reloc) const
1074   { return reloc.get_r_addend(); }
1075 };
1076
1077 // Utilities for manipulating integers of up to 32-bits
1078
1079 namespace utils
1080 {
1081   // Sign extend an n-bit unsigned integer stored in an uint32_t into
1082   // an int32_t.  NO_BITS must be between 1 to 32.
1083   template<int no_bits>
1084   static inline int32_t
1085   sign_extend(uint32_t bits)
1086   {
1087     gold_assert(no_bits >= 0 && no_bits <= 32);
1088     if (no_bits == 32)
1089       return static_cast<int32_t>(bits);
1090     uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
1091     bits &= mask;
1092     uint32_t top_bit = 1U << (no_bits - 1);
1093     int32_t as_signed = static_cast<int32_t>(bits);
1094     return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1095   }
1096
1097   // Detects overflow of an NO_BITS integer stored in a uint32_t.
1098   template<int no_bits>
1099   static inline bool
1100   has_overflow(uint32_t bits)
1101   {
1102     gold_assert(no_bits >= 0 && no_bits <= 32);
1103     if (no_bits == 32)
1104       return false;
1105     int32_t max = (1 << (no_bits - 1)) - 1;
1106     int32_t min = -(1 << (no_bits - 1));
1107     int32_t as_signed = static_cast<int32_t>(bits);
1108     return as_signed > max || as_signed < min;
1109   }
1110
1111   // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1112   // fits in the given number of bits as either a signed or unsigned value.
1113   // For example, has_signed_unsigned_overflow<8> would check
1114   // -128 <= bits <= 255
1115   template<int no_bits>
1116   static inline bool
1117   has_signed_unsigned_overflow(uint32_t bits)
1118   {
1119     gold_assert(no_bits >= 2 && no_bits <= 32);
1120     if (no_bits == 32)
1121       return false;
1122     int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1123     int32_t min = -(1 << (no_bits - 1));
1124     int32_t as_signed = static_cast<int32_t>(bits);
1125     return as_signed > max || as_signed < min;
1126   }
1127
1128   // Select bits from A and B using bits in MASK.  For each n in [0..31],
1129   // the n-th bit in the result is chosen from the n-th bits of A and B.
1130   // A zero selects A and a one selects B.
1131   static inline uint32_t
1132   bit_select(uint32_t a, uint32_t b, uint32_t mask)
1133   { return (a & ~mask) | (b & mask); }
1134 };
1135
1136 template<bool big_endian>
1137 class Target_arm : public Sized_target<32, big_endian>
1138 {
1139  public:
1140   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1141     Reloc_section;
1142
1143   Target_arm()
1144     : Sized_target<32, big_endian>(&arm_info),
1145       got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1146       copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
1147       stub_factory_(Stub_factory::get_instance()),
1148       may_use_blx_(true), should_force_pic_veneer_(false)
1149   { }
1150
1151   // Whether we can use BLX.
1152   bool
1153   may_use_blx() const
1154   { return this->may_use_blx_; }
1155
1156   // Set use-BLX flag.
1157   void
1158   set_may_use_blx(bool value)
1159   { this->may_use_blx_ = value; }
1160   
1161   // Whether we force PCI branch veneers.
1162   bool
1163   should_force_pic_veneer() const
1164   { return this->should_force_pic_veneer_; }
1165
1166   // Set PIC veneer flag.
1167   void
1168   set_should_force_pic_veneer(bool value)
1169   { this->should_force_pic_veneer_ = value; }
1170   
1171   // Whether we use THUMB-2 instructions.
1172   bool
1173   using_thumb2() const
1174   {
1175     // FIXME:  This should not hard-coded.
1176     return false;
1177   }
1178
1179   // Whether we use THUMB/THUMB-2 instructions only.
1180   bool
1181   using_thumb_only() const
1182   {
1183     // FIXME:  This should not hard-coded.
1184     return false;
1185   }
1186
1187   // Process the relocations to determine unreferenced sections for 
1188   // garbage collection.
1189   void
1190   gc_process_relocs(Symbol_table* symtab,
1191                     Layout* layout,
1192                     Sized_relobj<32, big_endian>* object,
1193                     unsigned int data_shndx,
1194                     unsigned int sh_type,
1195                     const unsigned char* prelocs,
1196                     size_t reloc_count,
1197                     Output_section* output_section,
1198                     bool needs_special_offset_handling,
1199                     size_t local_symbol_count,
1200                     const unsigned char* plocal_symbols);
1201
1202   // Scan the relocations to look for symbol adjustments.
1203   void
1204   scan_relocs(Symbol_table* symtab,
1205               Layout* layout,
1206               Sized_relobj<32, big_endian>* object,
1207               unsigned int data_shndx,
1208               unsigned int sh_type,
1209               const unsigned char* prelocs,
1210               size_t reloc_count,
1211               Output_section* output_section,
1212               bool needs_special_offset_handling,
1213               size_t local_symbol_count,
1214               const unsigned char* plocal_symbols);
1215
1216   // Finalize the sections.
1217   void
1218   do_finalize_sections(Layout*, const Input_objects*);
1219
1220   // Return the value to use for a dynamic symbol which requires special
1221   // treatment.
1222   uint64_t
1223   do_dynsym_value(const Symbol*) const;
1224
1225   // Relocate a section.
1226   void
1227   relocate_section(const Relocate_info<32, big_endian>*,
1228                    unsigned int sh_type,
1229                    const unsigned char* prelocs,
1230                    size_t reloc_count,
1231                    Output_section* output_section,
1232                    bool needs_special_offset_handling,
1233                    unsigned char* view,
1234                    Arm_address view_address,
1235                    section_size_type view_size,
1236                    const Reloc_symbol_changes*);
1237
1238   // Scan the relocs during a relocatable link.
1239   void
1240   scan_relocatable_relocs(Symbol_table* symtab,
1241                           Layout* layout,
1242                           Sized_relobj<32, big_endian>* object,
1243                           unsigned int data_shndx,
1244                           unsigned int sh_type,
1245                           const unsigned char* prelocs,
1246                           size_t reloc_count,
1247                           Output_section* output_section,
1248                           bool needs_special_offset_handling,
1249                           size_t local_symbol_count,
1250                           const unsigned char* plocal_symbols,
1251                           Relocatable_relocs*);
1252
1253   // Relocate a section during a relocatable link.
1254   void
1255   relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1256                            unsigned int sh_type,
1257                            const unsigned char* prelocs,
1258                            size_t reloc_count,
1259                            Output_section* output_section,
1260                            off_t offset_in_output_section,
1261                            const Relocatable_relocs*,
1262                            unsigned char* view,
1263                            Arm_address view_address,
1264                            section_size_type view_size,
1265                            unsigned char* reloc_view,
1266                            section_size_type reloc_view_size);
1267
1268   // Return whether SYM is defined by the ABI.
1269   bool
1270   do_is_defined_by_abi(Symbol* sym) const
1271   { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1272
1273   // Return the size of the GOT section.
1274   section_size_type
1275   got_size()
1276   {
1277     gold_assert(this->got_ != NULL);
1278     return this->got_->data_size();
1279   }
1280
1281   // Map platform-specific reloc types
1282   static unsigned int
1283   get_real_reloc_type (unsigned int r_type);
1284
1285   //
1286   // Methods to support stub-generations.
1287   //
1288   
1289   // Return the stub factory
1290   const Stub_factory&
1291   stub_factory() const
1292   { return this->stub_factory_; }
1293
1294   // Make a new Arm_input_section object.
1295   Arm_input_section<big_endian>*
1296   new_arm_input_section(Relobj*, unsigned int);
1297
1298   // Find the Arm_input_section object corresponding to the SHNDX-th input
1299   // section of RELOBJ.
1300   Arm_input_section<big_endian>*
1301   find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
1302
1303   // Make a new Stub_table
1304   Stub_table<big_endian>*
1305   new_stub_table(Arm_input_section<big_endian>*);
1306
1307   // Get the default ARM target.
1308   static const Target_arm<big_endian>&
1309   default_target()
1310   {
1311     gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1312                 && parameters->target().is_big_endian() == big_endian);
1313     return static_cast<const Target_arm<big_endian>&>(parameters->target());
1314   }
1315
1316   // Whether relocation type uses LSB to distinguish THUMB addresses.
1317   static bool
1318   reloc_uses_thumb_bit(unsigned int r_type);
1319
1320  protected:
1321   void
1322   do_adjust_elf_header(unsigned char* view, int len) const;
1323
1324  private:
1325   // The class which scans relocations.
1326   class Scan
1327   {
1328    public:
1329     Scan()
1330       : issued_non_pic_error_(false)
1331     { }
1332
1333     inline void
1334     local(Symbol_table* symtab, Layout* layout, Target_arm* target,
1335           Sized_relobj<32, big_endian>* object,
1336           unsigned int data_shndx,
1337           Output_section* output_section,
1338           const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1339           const elfcpp::Sym<32, big_endian>& lsym);
1340
1341     inline void
1342     global(Symbol_table* symtab, Layout* layout, Target_arm* target,
1343            Sized_relobj<32, big_endian>* object,
1344            unsigned int data_shndx,
1345            Output_section* output_section,
1346            const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1347            Symbol* gsym);
1348
1349    private:
1350     static void
1351     unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1352                             unsigned int r_type);
1353
1354     static void
1355     unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1356                              unsigned int r_type, Symbol*);
1357
1358     void
1359     check_non_pic(Relobj*, unsigned int r_type);
1360
1361     // Almost identical to Symbol::needs_plt_entry except that it also
1362     // handles STT_ARM_TFUNC.
1363     static bool
1364     symbol_needs_plt_entry(const Symbol* sym)
1365     {
1366       // An undefined symbol from an executable does not need a PLT entry.
1367       if (sym->is_undefined() && !parameters->options().shared())
1368         return false;
1369
1370       return (!parameters->doing_static_link()
1371               && (sym->type() == elfcpp::STT_FUNC
1372                   || sym->type() == elfcpp::STT_ARM_TFUNC)
1373               && (sym->is_from_dynobj()
1374                   || sym->is_undefined()
1375                   || sym->is_preemptible()));
1376     }
1377
1378     // Whether we have issued an error about a non-PIC compilation.
1379     bool issued_non_pic_error_;
1380   };
1381
1382   // The class which implements relocation.
1383   class Relocate
1384   {
1385    public:
1386     Relocate()
1387     { }
1388
1389     ~Relocate()
1390     { }
1391
1392     // Return whether the static relocation needs to be applied.
1393     inline bool
1394     should_apply_static_reloc(const Sized_symbol<32>* gsym,
1395                               int ref_flags,
1396                               bool is_32bit,
1397                               Output_section* output_section);
1398
1399     // Do a relocation.  Return false if the caller should not issue
1400     // any warnings about this relocation.
1401     inline bool
1402     relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1403              Output_section*,  size_t relnum,
1404              const elfcpp::Rel<32, big_endian>&,
1405              unsigned int r_type, const Sized_symbol<32>*,
1406              const Symbol_value<32>*,
1407              unsigned char*, Arm_address,
1408              section_size_type);
1409
1410     // Return whether we want to pass flag NON_PIC_REF for this
1411     // reloc.
1412     static inline bool
1413     reloc_is_non_pic (unsigned int r_type)
1414     {
1415       switch (r_type)
1416         {
1417         case elfcpp::R_ARM_REL32:
1418         case elfcpp::R_ARM_THM_CALL:
1419         case elfcpp::R_ARM_CALL:
1420         case elfcpp::R_ARM_JUMP24:
1421         case elfcpp::R_ARM_PREL31:
1422         case elfcpp::R_ARM_THM_ABS5:
1423         case elfcpp::R_ARM_ABS8:
1424         case elfcpp::R_ARM_ABS12:
1425         case elfcpp::R_ARM_ABS16:
1426         case elfcpp::R_ARM_BASE_ABS:
1427           return true;
1428         default:
1429           return false;
1430         }
1431     }
1432   };
1433
1434   // A class which returns the size required for a relocation type,
1435   // used while scanning relocs during a relocatable link.
1436   class Relocatable_size_for_reloc
1437   {
1438    public:
1439     unsigned int
1440     get_size_for_reloc(unsigned int, Relobj*);
1441   };
1442
1443   // Get the GOT section, creating it if necessary.
1444   Output_data_got<32, big_endian>*
1445   got_section(Symbol_table*, Layout*);
1446
1447   // Get the GOT PLT section.
1448   Output_data_space*
1449   got_plt_section() const
1450   {
1451     gold_assert(this->got_plt_ != NULL);
1452     return this->got_plt_;
1453   }
1454
1455   // Create a PLT entry for a global symbol.
1456   void
1457   make_plt_entry(Symbol_table*, Layout*, Symbol*);
1458
1459   // Get the PLT section.
1460   const Output_data_plt_arm<big_endian>*
1461   plt_section() const
1462   {
1463     gold_assert(this->plt_ != NULL);
1464     return this->plt_;
1465   }
1466
1467   // Get the dynamic reloc section, creating it if necessary.
1468   Reloc_section*
1469   rel_dyn_section(Layout*);
1470
1471   // Return true if the symbol may need a COPY relocation.
1472   // References from an executable object to non-function symbols
1473   // defined in a dynamic object may need a COPY relocation.
1474   bool
1475   may_need_copy_reloc(Symbol* gsym)
1476   {
1477     return (gsym->type() != elfcpp::STT_ARM_TFUNC
1478             && gsym->may_need_copy_reloc());
1479   }
1480
1481   // Add a potential copy relocation.
1482   void
1483   copy_reloc(Symbol_table* symtab, Layout* layout,
1484              Sized_relobj<32, big_endian>* object,
1485              unsigned int shndx, Output_section* output_section,
1486              Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1487   {
1488     this->copy_relocs_.copy_reloc(symtab, layout,
1489                                   symtab->get_sized_symbol<32>(sym),
1490                                   object, shndx, output_section, reloc,
1491                                   this->rel_dyn_section(layout));
1492   }
1493
1494   // Whether two EABI versions are compatible.
1495   static bool
1496   are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
1497
1498   // Merge processor-specific flags from input object and those in the ELF
1499   // header of the output.
1500   void
1501   merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
1502
1503   Object*
1504   do_make_elf_object(const std::string&, Input_file*, off_t,
1505                      const elfcpp::Ehdr<32, big_endian>& ehdr);
1506
1507   Object*
1508   do_make_elf_object(const std::string&, Input_file*, off_t,
1509                      const elfcpp::Ehdr<32, !big_endian>&)
1510   { gold_unreachable(); }
1511
1512   Object*
1513   do_make_elf_object(const std::string&, Input_file*, off_t,
1514                       const elfcpp::Ehdr<64, false>&)
1515   { gold_unreachable(); }
1516
1517   Object*
1518   do_make_elf_object(const std::string&, Input_file*, off_t,
1519                      const elfcpp::Ehdr<64, true>&)
1520   { gold_unreachable(); }
1521
1522   // Information about this specific target which we pass to the
1523   // general Target structure.
1524   static const Target::Target_info arm_info;
1525
1526   // The types of GOT entries needed for this platform.
1527   enum Got_type
1528   {
1529     GOT_TYPE_STANDARD = 0       // GOT entry for a regular symbol
1530   };
1531
1532   typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
1533
1534   // Map input section to Arm_input_section.
1535   typedef Unordered_map<Input_section_specifier,
1536                         Arm_input_section<big_endian>*,
1537                         Input_section_specifier::hash,
1538                         Input_section_specifier::equal_to>
1539           Arm_input_section_map;
1540     
1541   // The GOT section.
1542   Output_data_got<32, big_endian>* got_;
1543   // The PLT section.
1544   Output_data_plt_arm<big_endian>* plt_;
1545   // The GOT PLT section.
1546   Output_data_space* got_plt_;
1547   // The dynamic reloc section.
1548   Reloc_section* rel_dyn_;
1549   // Relocs saved to avoid a COPY reloc.
1550   Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1551   // Space for variables copied with a COPY reloc.
1552   Output_data_space* dynbss_;
1553   // Vector of Stub_tables created.
1554   Stub_table_list stub_tables_;
1555   // Stub factory.
1556   const Stub_factory &stub_factory_;
1557   // Whether we can use BLX.
1558   bool may_use_blx_;
1559   // Whether we force PIC branch veneers.
1560   bool should_force_pic_veneer_;
1561 };
1562
1563 template<bool big_endian>
1564 const Target::Target_info Target_arm<big_endian>::arm_info =
1565 {
1566   32,                   // size
1567   big_endian,           // is_big_endian
1568   elfcpp::EM_ARM,       // machine_code
1569   false,                // has_make_symbol
1570   false,                // has_resolve
1571   false,                // has_code_fill
1572   true,                 // is_default_stack_executable
1573   '\0',                 // wrap_char
1574   "/usr/lib/libc.so.1", // dynamic_linker
1575   0x8000,               // default_text_segment_address
1576   0x1000,               // abi_pagesize (overridable by -z max-page-size)
1577   0x1000,               // common_pagesize (overridable by -z common-page-size)
1578   elfcpp::SHN_UNDEF,    // small_common_shndx
1579   elfcpp::SHN_UNDEF,    // large_common_shndx
1580   0,                    // small_common_section_flags
1581   0                     // large_common_section_flags
1582 };
1583
1584 // Arm relocate functions class
1585 //
1586
1587 template<bool big_endian>
1588 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1589 {
1590  public:
1591   typedef enum
1592   {
1593     STATUS_OKAY,        // No error during relocation.
1594     STATUS_OVERFLOW,    // Relocation oveflow.
1595     STATUS_BAD_RELOC    // Relocation cannot be applied.
1596   } Status;
1597
1598  private:
1599   typedef Relocate_functions<32, big_endian> Base;
1600   typedef Arm_relocate_functions<big_endian> This;
1601
1602   // Get an symbol value of *PSYMVAL with an ADDEND.  This is a wrapper
1603   // to Symbol_value::value().  If HAS_THUMB_BIT is true, that LSB is used
1604   // to distinguish ARM and THUMB functions and it is treated specially.
1605   static inline Symbol_value<32>::Value
1606   arm_symbol_value (const Sized_relobj<32, big_endian> *object,
1607                     const Symbol_value<32>* psymval,
1608                     Symbol_value<32>::Value addend,
1609                     bool has_thumb_bit)
1610   {
1611     typedef Symbol_value<32>::Value Valtype;
1612
1613     if (has_thumb_bit)
1614       {
1615         Valtype raw = psymval->value(object, 0);
1616         Valtype thumb_bit = raw & 1;
1617         return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
1618       }
1619     else
1620       return psymval->value(object, addend);
1621   }
1622
1623   // Encoding of imm16 argument for movt and movw ARM instructions
1624   // from ARM ARM:
1625   //     
1626   //     imm16 := imm4 | imm12
1627   //
1628   //  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 
1629   // +-------+---------------+-------+-------+-----------------------+
1630   // |       |               |imm4   |       |imm12                  |
1631   // +-------+---------------+-------+-------+-----------------------+
1632
1633   // Extract the relocation addend from VAL based on the ARM
1634   // instruction encoding described above.
1635   static inline typename elfcpp::Swap<32, big_endian>::Valtype
1636   extract_arm_movw_movt_addend(
1637       typename elfcpp::Swap<32, big_endian>::Valtype val)
1638   {
1639     // According to the Elf ABI for ARM Architecture the immediate
1640     // field is sign-extended to form the addend.
1641     return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1642   }
1643
1644   // Insert X into VAL based on the ARM instruction encoding described
1645   // above.
1646   static inline typename elfcpp::Swap<32, big_endian>::Valtype
1647   insert_val_arm_movw_movt(
1648       typename elfcpp::Swap<32, big_endian>::Valtype val,
1649       typename elfcpp::Swap<32, big_endian>::Valtype x)
1650   {
1651     val &= 0xfff0f000;
1652     val |= x & 0x0fff;
1653     val |= (x & 0xf000) << 4;
1654     return val;
1655   }
1656
1657   // Encoding of imm16 argument for movt and movw Thumb2 instructions
1658   // from ARM ARM:
1659   //     
1660   //     imm16 := imm4 | i | imm3 | imm8
1661   //
1662   //  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 
1663   // +---------+-+-----------+-------++-+-----+-------+---------------+
1664   // |         |i|           |imm4   || |imm3 |       |imm8           |
1665   // +---------+-+-----------+-------++-+-----+-------+---------------+
1666
1667   // Extract the relocation addend from VAL based on the Thumb2
1668   // instruction encoding described above.
1669   static inline typename elfcpp::Swap<32, big_endian>::Valtype
1670   extract_thumb_movw_movt_addend(
1671       typename elfcpp::Swap<32, big_endian>::Valtype val)
1672   {
1673     // According to the Elf ABI for ARM Architecture the immediate
1674     // field is sign-extended to form the addend.
1675     return utils::sign_extend<16>(((val >> 4) & 0xf000)
1676                                   | ((val >> 15) & 0x0800)
1677                                   | ((val >> 4) & 0x0700)
1678                                   | (val & 0x00ff));
1679   }
1680
1681   // Insert X into VAL based on the Thumb2 instruction encoding
1682   // described above.
1683   static inline typename elfcpp::Swap<32, big_endian>::Valtype
1684   insert_val_thumb_movw_movt(
1685       typename elfcpp::Swap<32, big_endian>::Valtype val,
1686       typename elfcpp::Swap<32, big_endian>::Valtype x)
1687   {
1688     val &= 0xfbf08f00;
1689     val |= (x & 0xf000) << 4;
1690     val |= (x & 0x0800) << 15;
1691     val |= (x & 0x0700) << 4;
1692     val |= (x & 0x00ff);
1693     return val;
1694   }
1695
1696   // FIXME: This probably only works for Android on ARM v5te. We should
1697   // following GNU ld for the general case.
1698   template<unsigned r_type>
1699   static inline typename This::Status
1700   arm_branch_common(unsigned char *view,
1701                     const Sized_relobj<32, big_endian>* object,
1702                     const Symbol_value<32>* psymval,
1703                     Arm_address address,
1704                     bool has_thumb_bit)
1705   {
1706     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1707     Valtype* wv = reinterpret_cast<Valtype*>(view);
1708     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1709      
1710     bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
1711                       && ((val & 0x0f000000UL) == 0x0a000000UL);
1712     bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
1713     bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
1714                             && ((val & 0x0f000000UL) == 0x0b000000UL);
1715     bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
1716     bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
1717
1718     if (r_type == elfcpp::R_ARM_CALL)
1719       {
1720         if (!insn_is_uncond_bl && !insn_is_blx)
1721           return This::STATUS_BAD_RELOC;
1722       }
1723     else if (r_type == elfcpp::R_ARM_JUMP24)
1724       {
1725         if (!insn_is_b && !insn_is_cond_bl)
1726           return This::STATUS_BAD_RELOC;
1727       }
1728     else if (r_type == elfcpp::R_ARM_PLT32)
1729       {
1730         if (!insn_is_any_branch)
1731           return This::STATUS_BAD_RELOC;
1732       }
1733     else
1734       gold_unreachable();
1735
1736     Valtype addend = utils::sign_extend<26>(val << 2);
1737     Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1738                  - address);
1739
1740     // If target has thumb bit set, we need to either turn the BL
1741     // into a BLX (for ARMv5 or above) or generate a stub.
1742     if (x & 1)
1743       {
1744         // Turn BL to BLX.
1745         if (insn_is_uncond_bl)
1746           val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
1747         else
1748           return This::STATUS_BAD_RELOC;
1749       }
1750     else
1751       gold_assert(!insn_is_blx);
1752
1753     val = utils::bit_select(val, (x >> 2), 0xffffffUL);
1754     elfcpp::Swap<32, big_endian>::writeval(wv, val);
1755     return (utils::has_overflow<26>(x)
1756             ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
1757   }
1758
1759  public:
1760
1761   // R_ARM_ABS8: S + A
1762   static inline typename This::Status
1763   abs8(unsigned char *view,
1764        const Sized_relobj<32, big_endian>* object,
1765        const Symbol_value<32>* psymval)
1766   {
1767     typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
1768     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1769     Valtype* wv = reinterpret_cast<Valtype*>(view);
1770     Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
1771     Reltype addend = utils::sign_extend<8>(val);
1772     Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1773     val = utils::bit_select(val, x, 0xffU);
1774     elfcpp::Swap<8, big_endian>::writeval(wv, val);
1775     return (utils::has_signed_unsigned_overflow<8>(x)
1776             ? This::STATUS_OVERFLOW
1777             : This::STATUS_OKAY);
1778   }
1779
1780   // R_ARM_THM_ABS5: S + A
1781   static inline typename This::Status
1782   thm_abs5(unsigned char *view,
1783        const Sized_relobj<32, big_endian>* object,
1784        const Symbol_value<32>* psymval)
1785   {
1786     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1787     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1788     Valtype* wv = reinterpret_cast<Valtype*>(view);
1789     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1790     Reltype addend = (val & 0x7e0U) >> 6;
1791     Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1792     val = utils::bit_select(val, x << 6, 0x7e0U);
1793     elfcpp::Swap<16, big_endian>::writeval(wv, val);
1794     return (utils::has_overflow<5>(x)
1795             ? This::STATUS_OVERFLOW
1796             : This::STATUS_OKAY);
1797   }
1798
1799   // R_ARM_ABS12: S + A
1800   static inline typename This::Status
1801   abs12(unsigned char *view,
1802        const Sized_relobj<32, big_endian>* object,
1803        const Symbol_value<32>* psymval)
1804   {
1805     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1806     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1807     Valtype* wv = reinterpret_cast<Valtype*>(view);
1808     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1809     Reltype addend = val & 0x0fffU;
1810     Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1811     val = utils::bit_select(val, x, 0x0fffU);
1812     elfcpp::Swap<32, big_endian>::writeval(wv, val);
1813     return (utils::has_overflow<12>(x)
1814             ? This::STATUS_OVERFLOW
1815             : This::STATUS_OKAY);
1816   }
1817
1818   // R_ARM_ABS16: S + A
1819   static inline typename This::Status
1820   abs16(unsigned char *view,
1821        const Sized_relobj<32, big_endian>* object,
1822        const Symbol_value<32>* psymval)
1823   {
1824     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1825     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1826     Valtype* wv = reinterpret_cast<Valtype*>(view);
1827     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1828     Reltype addend = utils::sign_extend<16>(val);
1829     Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1830     val = utils::bit_select(val, x, 0xffffU);
1831     elfcpp::Swap<16, big_endian>::writeval(wv, val);
1832     return (utils::has_signed_unsigned_overflow<16>(x)
1833             ? This::STATUS_OVERFLOW
1834             : This::STATUS_OKAY);
1835   }
1836
1837   // R_ARM_ABS32: (S + A) | T
1838   static inline typename This::Status
1839   abs32(unsigned char *view,
1840         const Sized_relobj<32, big_endian>* object,
1841         const Symbol_value<32>* psymval,
1842         bool has_thumb_bit)
1843   {
1844     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1845     Valtype* wv = reinterpret_cast<Valtype*>(view);
1846     Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1847     Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1848     elfcpp::Swap<32, big_endian>::writeval(wv, x);
1849     return This::STATUS_OKAY;
1850   }
1851
1852   // R_ARM_REL32: (S + A) | T - P
1853   static inline typename This::Status
1854   rel32(unsigned char *view,
1855         const Sized_relobj<32, big_endian>* object,
1856         const Symbol_value<32>* psymval,
1857         Arm_address address,
1858         bool has_thumb_bit)
1859   {
1860     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1861     Valtype* wv = reinterpret_cast<Valtype*>(view);
1862     Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1863     Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit) 
1864                  - address);
1865     elfcpp::Swap<32, big_endian>::writeval(wv, x);
1866     return This::STATUS_OKAY;
1867   }
1868
1869   // R_ARM_THM_CALL: (S + A) | T - P
1870   static inline typename This::Status
1871   thm_call(unsigned char *view,
1872            const Sized_relobj<32, big_endian>* object,
1873            const Symbol_value<32>* psymval,
1874            Arm_address address,
1875            bool has_thumb_bit)
1876   {
1877     // A thumb call consists of two instructions.
1878     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1879     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1880     Valtype* wv = reinterpret_cast<Valtype*>(view);
1881     Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
1882     Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
1883     // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1884     gold_assert((lo & 0xf800) == 0xf800);
1885     Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
1886                                            | ((lo & 0x7ff) << 1));
1887     Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1888                  - address);
1889
1890     // If target has no thumb bit set, we need to either turn the BL
1891     // into a BLX (for ARMv5 or above) or generate a stub.
1892     if ((x & 1) == 0)
1893       {
1894         // This only works for ARMv5 and above with interworking enabled.
1895         lo &= 0xefff;
1896       }
1897     hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
1898     lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
1899     elfcpp::Swap<16, big_endian>::writeval(wv, hi);
1900     elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
1901     return (utils::has_overflow<23>(x)
1902             ? This::STATUS_OVERFLOW
1903             : This::STATUS_OKAY);
1904   }
1905
1906   // R_ARM_BASE_PREL: B(S) + A - P
1907   static inline typename This::Status
1908   base_prel(unsigned char* view,
1909             Arm_address origin,
1910             Arm_address address)
1911   {
1912     Base::rel32(view, origin - address);
1913     return STATUS_OKAY;
1914   }
1915
1916   // R_ARM_BASE_ABS: B(S) + A
1917   static inline typename This::Status
1918   base_abs(unsigned char* view,
1919             Arm_address origin)
1920   {
1921     Base::rel32(view, origin);
1922     return STATUS_OKAY;
1923   }
1924
1925   // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1926   static inline typename This::Status
1927   got_brel(unsigned char* view,
1928            typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
1929   {
1930     Base::rel32(view, got_offset);
1931     return This::STATUS_OKAY;
1932   }
1933
1934   // R_ARM_GOT_PREL: GOT(S) + A – P
1935   static inline typename This::Status
1936   got_prel(unsigned char* view,
1937            typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
1938            Arm_address address)
1939   {
1940     Base::rel32(view, got_offset - address);
1941     return This::STATUS_OKAY;
1942   }
1943
1944   // R_ARM_PLT32: (S + A) | T - P
1945   static inline typename This::Status
1946   plt32(unsigned char *view,
1947         const Sized_relobj<32, big_endian>* object,
1948         const Symbol_value<32>* psymval,
1949         Arm_address address,
1950         bool has_thumb_bit)
1951   {
1952     return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
1953                                                   address, has_thumb_bit);
1954   }
1955
1956   // R_ARM_CALL: (S + A) | T - P
1957   static inline typename This::Status
1958   call(unsigned char *view,
1959        const Sized_relobj<32, big_endian>* object,
1960        const Symbol_value<32>* psymval,
1961        Arm_address address,
1962        bool has_thumb_bit)
1963   {
1964     return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
1965                                                  address, has_thumb_bit);
1966   }
1967
1968   // R_ARM_JUMP24: (S + A) | T - P
1969   static inline typename This::Status
1970   jump24(unsigned char *view,
1971          const Sized_relobj<32, big_endian>* object,
1972          const Symbol_value<32>* psymval,
1973          Arm_address address,
1974          bool has_thumb_bit)
1975   {
1976     return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
1977                                                    address, has_thumb_bit);
1978   }
1979
1980   // R_ARM_PREL: (S + A) | T - P
1981   static inline typename This::Status
1982   prel31(unsigned char *view,
1983          const Sized_relobj<32, big_endian>* object,
1984          const Symbol_value<32>* psymval,
1985          Arm_address address,
1986          bool has_thumb_bit)
1987   {
1988     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1989     Valtype* wv = reinterpret_cast<Valtype*>(view);
1990     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1991     Valtype addend = utils::sign_extend<31>(val);
1992     Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1993                  - address);
1994     val = utils::bit_select(val, x, 0x7fffffffU);
1995     elfcpp::Swap<32, big_endian>::writeval(wv, val);
1996     return (utils::has_overflow<31>(x) ?
1997             This::STATUS_OVERFLOW : This::STATUS_OKAY);
1998   }
1999
2000   // R_ARM_MOVW_ABS_NC: (S + A) | T
2001   static inline typename This::Status 
2002   movw_abs_nc(unsigned char *view,
2003               const Sized_relobj<32, big_endian>* object,
2004               const Symbol_value<32>* psymval,
2005               bool has_thumb_bit)
2006   {
2007     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2008     Valtype* wv = reinterpret_cast<Valtype*>(view);
2009     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2010     Valtype addend =  This::extract_arm_movw_movt_addend(val);
2011     Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
2012     val = This::insert_val_arm_movw_movt(val, x);
2013     elfcpp::Swap<32, big_endian>::writeval(wv, val);
2014     return This::STATUS_OKAY;
2015   }
2016
2017   // R_ARM_MOVT_ABS: S + A
2018   static inline typename This::Status
2019   movt_abs(unsigned char *view,
2020            const Sized_relobj<32, big_endian>* object,
2021            const Symbol_value<32>* psymval)
2022   {
2023     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2024     Valtype* wv = reinterpret_cast<Valtype*>(view);
2025     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2026     Valtype addend = This::extract_arm_movw_movt_addend(val);
2027     Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
2028     val = This::insert_val_arm_movw_movt(val, x);
2029     elfcpp::Swap<32, big_endian>::writeval(wv, val);
2030     return This::STATUS_OKAY;
2031   }
2032
2033   //  R_ARM_THM_MOVW_ABS_NC: S + A | T
2034   static inline typename This::Status 
2035   thm_movw_abs_nc(unsigned char *view,
2036                   const Sized_relobj<32, big_endian>* object,
2037                   const Symbol_value<32>* psymval,
2038                   bool has_thumb_bit)
2039   {
2040     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2041     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2042     Valtype* wv = reinterpret_cast<Valtype*>(view);
2043     Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2044                    | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2045     Reltype addend = extract_thumb_movw_movt_addend(val);
2046     Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
2047     val = This::insert_val_thumb_movw_movt(val, x);
2048     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2049     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2050     return This::STATUS_OKAY;
2051   }
2052
2053   //  R_ARM_THM_MOVT_ABS: S + A
2054   static inline typename This::Status 
2055   thm_movt_abs(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     Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2063                    | elfcpp::Swap<16, big_endian>::readval(wv + 1));
2064     Reltype addend = This::extract_thumb_movw_movt_addend(val);
2065     Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
2066     val = This::insert_val_thumb_movw_movt(val, x);
2067     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2068     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2069     return This::STATUS_OKAY;
2070   }
2071
2072   // R_ARM_MOVW_PREL_NC: (S + A) | T - P
2073   static inline typename This::Status
2074   movw_prel_nc(unsigned char *view,
2075                const Sized_relobj<32, big_endian>* object,
2076                const Symbol_value<32>* psymval,
2077                Arm_address address,
2078                bool has_thumb_bit)
2079   {
2080     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2081     Valtype* wv = reinterpret_cast<Valtype*>(view);
2082     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2083     Valtype addend = This::extract_arm_movw_movt_addend(val);
2084     Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
2085                  - address);
2086     val = This::insert_val_arm_movw_movt(val, x);
2087     elfcpp::Swap<32, big_endian>::writeval(wv, val);
2088     return This::STATUS_OKAY;
2089   }
2090
2091   // R_ARM_MOVT_PREL: S + A - P
2092   static inline typename This::Status
2093   movt_prel(unsigned char *view,
2094             const Sized_relobj<32, big_endian>* object,
2095             const Symbol_value<32>* psymval,
2096             Arm_address address)
2097   {
2098     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2099     Valtype* wv = reinterpret_cast<Valtype*>(view);
2100     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2101     Valtype addend = This::extract_arm_movw_movt_addend(val);
2102     Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
2103                  - address) >> 16;
2104     val = This::insert_val_arm_movw_movt(val, x);
2105     elfcpp::Swap<32, big_endian>::writeval(wv, val);
2106     return This::STATUS_OKAY;
2107   }
2108
2109   // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
2110   static inline typename This::Status
2111   thm_movw_prel_nc(unsigned char *view,
2112                    const Sized_relobj<32, big_endian>* object,
2113                    const Symbol_value<32>* psymval,
2114                    Arm_address address,
2115                    bool has_thumb_bit)
2116   {
2117     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2118     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2119     Valtype* wv = reinterpret_cast<Valtype*>(view);
2120     Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2121                   | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2122     Reltype addend = This::extract_thumb_movw_movt_addend(val);
2123     Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
2124                  - address);
2125     val = This::insert_val_thumb_movw_movt(val, x);
2126     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2127     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2128     return This::STATUS_OKAY;
2129   }
2130
2131   // R_ARM_THM_MOVT_PREL: S + A - P
2132   static inline typename This::Status
2133   thm_movt_prel(unsigned char *view,
2134                 const Sized_relobj<32, big_endian>* object,
2135                 const Symbol_value<32>* psymval,
2136                 Arm_address address)
2137   {
2138     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
2139     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
2140     Valtype* wv = reinterpret_cast<Valtype*>(view);
2141     Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
2142                   | elfcpp::Swap<16, big_endian>::readval(wv + 1);
2143     Reltype addend = This::extract_thumb_movw_movt_addend(val);
2144     Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
2145                  - address) >> 16;
2146     val = This::insert_val_thumb_movw_movt(val, x);
2147     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
2148     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
2149     return This::STATUS_OKAY;
2150   }
2151 };
2152
2153 // Get the GOT section, creating it if necessary.
2154
2155 template<bool big_endian>
2156 Output_data_got<32, big_endian>*
2157 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
2158 {
2159   if (this->got_ == NULL)
2160     {
2161       gold_assert(symtab != NULL && layout != NULL);
2162
2163       this->got_ = new Output_data_got<32, big_endian>();
2164
2165       Output_section* os;
2166       os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2167                                            (elfcpp::SHF_ALLOC
2168                                             | elfcpp::SHF_WRITE),
2169                                            this->got_);
2170       os->set_is_relro();
2171
2172       // The old GNU linker creates a .got.plt section.  We just
2173       // create another set of data in the .got section.  Note that we
2174       // always create a PLT if we create a GOT, although the PLT
2175       // might be empty.
2176       this->got_plt_ = new Output_data_space(4, "** GOT PLT");
2177       os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2178                                            (elfcpp::SHF_ALLOC
2179                                             | elfcpp::SHF_WRITE),
2180                                            this->got_plt_);
2181       os->set_is_relro();
2182
2183       // The first three entries are reserved.
2184       this->got_plt_->set_current_data_size(3 * 4);
2185
2186       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2187       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2188                                     this->got_plt_,
2189                                     0, 0, elfcpp::STT_OBJECT,
2190                                     elfcpp::STB_LOCAL,
2191                                     elfcpp::STV_HIDDEN, 0,
2192                                     false, false);
2193     }
2194   return this->got_;
2195 }
2196
2197 // Get the dynamic reloc section, creating it if necessary.
2198
2199 template<bool big_endian>
2200 typename Target_arm<big_endian>::Reloc_section*
2201 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
2202 {
2203   if (this->rel_dyn_ == NULL)
2204     {
2205       gold_assert(layout != NULL);
2206       this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
2207       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
2208                                       elfcpp::SHF_ALLOC, this->rel_dyn_);
2209     }
2210   return this->rel_dyn_;
2211 }
2212
2213 // Insn_template methods.
2214
2215 // Return byte size of an instruction template.
2216
2217 size_t
2218 Insn_template::size() const
2219 {
2220   switch (this->type())
2221     {
2222     case THUMB16_TYPE:
2223       return 2;
2224     case ARM_TYPE:
2225     case THUMB32_TYPE:
2226     case DATA_TYPE:
2227       return 4;
2228     default:
2229       gold_unreachable();
2230     }
2231 }
2232
2233 // Return alignment of an instruction template.
2234
2235 unsigned
2236 Insn_template::alignment() const
2237 {
2238   switch (this->type())
2239     {
2240     case THUMB16_TYPE:
2241     case THUMB32_TYPE:
2242       return 2;
2243     case ARM_TYPE:
2244     case DATA_TYPE:
2245       return 4;
2246     default:
2247       gold_unreachable();
2248     }
2249 }
2250
2251 // Stub_template methods.
2252
2253 Stub_template::Stub_template(
2254     Stub_type type, const Insn_template* insns,
2255      size_t insn_count)
2256   : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
2257     entry_in_thumb_mode_(false), relocs_()
2258 {
2259   off_t offset = 0;
2260
2261   // Compute byte size and alignment of stub template.
2262   for (size_t i = 0; i < insn_count; i++)
2263     {
2264       unsigned insn_alignment = insns[i].alignment();
2265       size_t insn_size = insns[i].size();
2266       gold_assert((offset & (insn_alignment - 1)) == 0);
2267       this->alignment_ = std::max(this->alignment_, insn_alignment);
2268       switch (insns[i].type())
2269         {
2270         case Insn_template::THUMB16_TYPE:
2271           if (i == 0)
2272             this->entry_in_thumb_mode_ = true;
2273           break;
2274
2275         case Insn_template::THUMB32_TYPE:
2276           if (insns[i].r_type() != elfcpp::R_ARM_NONE)
2277             this->relocs_.push_back(Reloc(i, offset));
2278           if (i == 0)
2279             this->entry_in_thumb_mode_ = true;
2280           break;
2281
2282         case Insn_template::ARM_TYPE:
2283           // Handle cases where the target is encoded within the
2284           // instruction.
2285           if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
2286             this->relocs_.push_back(Reloc(i, offset));
2287           break;
2288
2289         case Insn_template::DATA_TYPE:
2290           // Entry point cannot be data.
2291           gold_assert(i != 0);
2292           this->relocs_.push_back(Reloc(i, offset));
2293           break;
2294
2295         default:
2296           gold_unreachable();
2297         }
2298       offset += insn_size; 
2299     }
2300   this->size_ = offset;
2301 }
2302
2303 // Reloc_stub::Key methods.
2304
2305 // Dump a Key as a string for debugging.
2306
2307 std::string
2308 Reloc_stub::Key::name() const
2309 {
2310   if (this->r_sym_ == invalid_index)
2311     {
2312       // Global symbol key name
2313       // <stub-type>:<symbol name>:<addend>.
2314       const std::string sym_name = this->u_.symbol->name();
2315       // We need to print two hex number and two colons.  So just add 100 bytes
2316       // to the symbol name size.
2317       size_t len = sym_name.size() + 100;
2318       char* buffer = new char[len];
2319       int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
2320                        sym_name.c_str(), this->addend_);
2321       gold_assert(c > 0 && c < static_cast<int>(len));
2322       delete[] buffer;
2323       return std::string(buffer);
2324     }
2325   else
2326     {
2327       // local symbol key name
2328       // <stub-type>:<object>:<r_sym>:<addend>.
2329       const size_t len = 200;
2330       char buffer[len];
2331       int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
2332                        this->u_.relobj, this->r_sym_, this->addend_);
2333       gold_assert(c > 0 && c < static_cast<int>(len));
2334       return std::string(buffer);
2335     }
2336 }
2337
2338 // Reloc_stub methods.
2339
2340 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2341 // LOCATION to DESTINATION.
2342 // This code is based on the arm_type_of_stub function in
2343 // bfd/elf32-arm.c.  We have changed the interface a liitle to keep the Stub
2344 // class simple.
2345
2346 Stub_type
2347 Reloc_stub::stub_type_for_reloc(
2348    unsigned int r_type,
2349    Arm_address location,
2350    Arm_address destination,
2351    bool target_is_thumb)
2352 {
2353   Stub_type stub_type = arm_stub_none;
2354
2355   // This is a bit ugly but we want to avoid using a templated class for
2356   // big and little endianities.
2357   bool may_use_blx;
2358   bool should_force_pic_veneer;
2359   bool thumb2;
2360   bool thumb_only;
2361   if (parameters->target().is_big_endian())
2362     {
2363       const Target_arm<true>& big_endian_target =
2364         Target_arm<true>::default_target();
2365       may_use_blx = big_endian_target.may_use_blx();
2366       should_force_pic_veneer = big_endian_target.should_force_pic_veneer();
2367       thumb2 = big_endian_target.using_thumb2();
2368       thumb_only = big_endian_target.using_thumb_only();
2369     }
2370   else
2371     {
2372       const Target_arm<false>& little_endian_target =
2373         Target_arm<false>::default_target();
2374       may_use_blx = little_endian_target.may_use_blx();
2375       should_force_pic_veneer = little_endian_target.should_force_pic_veneer();
2376       thumb2 = little_endian_target.using_thumb2();
2377       thumb_only = little_endian_target.using_thumb_only();
2378     }
2379
2380   int64_t branch_offset = (int64_t)destination - location;
2381
2382   if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
2383     {
2384       // Handle cases where:
2385       // - this call goes too far (different Thumb/Thumb2 max
2386       //   distance)
2387       // - it's a Thumb->Arm call and blx is not available, or it's a
2388       //   Thumb->Arm branch (not bl). A stub is needed in this case.
2389       if ((!thumb2
2390             && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2391                 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2392           || (thumb2
2393               && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2394                   || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2395           || ((!target_is_thumb)
2396               && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2397                   || (r_type == elfcpp::R_ARM_THM_JUMP24))))
2398         {
2399           if (target_is_thumb)
2400             {
2401               // Thumb to thumb.
2402               if (!thumb_only)
2403                 {
2404                   stub_type = (parameters->options().shared() | should_force_pic_veneer)
2405                     // PIC stubs.
2406                     ? ((may_use_blx
2407                         && (r_type == elfcpp::R_ARM_THM_CALL))
2408                        // V5T and above. Stub starts with ARM code, so
2409                        // we must be able to switch mode before
2410                        // reaching it, which is only possible for 'bl'
2411                        // (ie R_ARM_THM_CALL relocation).
2412                        ? arm_stub_long_branch_any_thumb_pic
2413                        // On V4T, use Thumb code only.
2414                        : arm_stub_long_branch_v4t_thumb_thumb_pic)
2415
2416                     // non-PIC stubs.
2417                     : ((may_use_blx
2418                         && (r_type == elfcpp::R_ARM_THM_CALL))
2419                        ? arm_stub_long_branch_any_any // V5T and above.
2420                        : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
2421                 }
2422               else
2423                 {
2424                   stub_type = (parameters->options().shared() | should_force_pic_veneer)
2425                     ? arm_stub_long_branch_thumb_only_pic       // PIC stub.
2426                     : arm_stub_long_branch_thumb_only;  // non-PIC stub.
2427                 }
2428             }
2429           else
2430             {
2431               // Thumb to arm.
2432              
2433               // FIXME: We should check that the input section is from an
2434               // object that has interwork enabled.
2435
2436               stub_type = (parameters->options().shared()
2437                            || should_force_pic_veneer)
2438                 // PIC stubs.
2439                 ? ((may_use_blx
2440                     && (r_type == elfcpp::R_ARM_THM_CALL))
2441                    ? arm_stub_long_branch_any_arm_pic   // V5T and above.
2442                    : arm_stub_long_branch_v4t_thumb_arm_pic)    // V4T.
2443
2444                 // non-PIC stubs.
2445                 : ((may_use_blx
2446                     && (r_type == elfcpp::R_ARM_THM_CALL))
2447                    ? arm_stub_long_branch_any_any       // V5T and above.
2448                    : arm_stub_long_branch_v4t_thumb_arm);       // V4T.
2449
2450               // Handle v4t short branches.
2451               if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
2452                   && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
2453                   && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
2454                 stub_type = arm_stub_short_branch_v4t_thumb_arm;
2455             }
2456         }
2457     }
2458   else if (r_type == elfcpp::R_ARM_CALL
2459            || r_type == elfcpp::R_ARM_JUMP24
2460            || r_type == elfcpp::R_ARM_PLT32)
2461     {
2462       if (target_is_thumb)
2463         {
2464           // Arm to thumb.
2465
2466           // FIXME: We should check that the input section is from an
2467           // object that has interwork enabled.
2468
2469           // We have an extra 2-bytes reach because of
2470           // the mode change (bit 24 (H) of BLX encoding).
2471           if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
2472               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2473               || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
2474               || (r_type == elfcpp::R_ARM_JUMP24)
2475               || (r_type == elfcpp::R_ARM_PLT32))
2476             {
2477               stub_type = (parameters->options().shared()
2478                            || should_force_pic_veneer)
2479                 // PIC stubs.
2480                 ? (may_use_blx
2481                    ? arm_stub_long_branch_any_thumb_pic// V5T and above.
2482                    : arm_stub_long_branch_v4t_arm_thumb_pic)    // V4T stub.
2483
2484                 // non-PIC stubs.
2485                 : (may_use_blx
2486                    ? arm_stub_long_branch_any_any       // V5T and above.
2487                    : arm_stub_long_branch_v4t_arm_thumb);       // V4T.
2488             }
2489         }
2490       else
2491         {
2492           // Arm to arm.
2493           if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
2494               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
2495             {
2496               stub_type = (parameters->options().shared()
2497                            || should_force_pic_veneer)
2498                 ? arm_stub_long_branch_any_arm_pic      // PIC stubs.
2499                 : arm_stub_long_branch_any_any;         /// non-PIC.
2500             }
2501         }
2502     }
2503
2504   return stub_type;
2505 }
2506
2507 // Template to implement do_write for a specific target endianity.
2508
2509 template<bool big_endian>
2510 void inline
2511 Reloc_stub::do_fixed_endian_write(unsigned char* view,
2512                                   section_size_type view_size)
2513 {
2514   const Stub_template* stub_template = this->stub_template();
2515   const Insn_template* insns = stub_template->insns();
2516
2517   // FIXME:  We do not handle BE8 encoding yet.
2518   unsigned char* pov = view;
2519   for (size_t i = 0; i < stub_template->insn_count(); i++)
2520     {
2521       switch (insns[i].type())
2522         {
2523         case Insn_template::THUMB16_TYPE:
2524           // Non-zero reloc addends are only used in Cortex-A8 stubs. 
2525           gold_assert(insns[i].reloc_addend() == 0);
2526           elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2527           break;
2528         case Insn_template::THUMB32_TYPE:
2529           {
2530             uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2531             uint32_t lo = insns[i].data() & 0xffff;
2532             elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2533             elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2534           }
2535           break;
2536         case Insn_template::ARM_TYPE:
2537         case Insn_template::DATA_TYPE:
2538           elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2539           break;
2540         default:
2541           gold_unreachable();
2542         }
2543       pov += insns[i].size();
2544     }
2545   gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2546
2547
2548 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2549
2550 void
2551 Reloc_stub::do_write(unsigned char* view, section_size_type view_size,
2552                      bool big_endian)
2553 {
2554   if (big_endian)
2555     this->do_fixed_endian_write<true>(view, view_size);
2556   else
2557     this->do_fixed_endian_write<false>(view, view_size);
2558 }
2559
2560 // Stub_factory methods.
2561
2562 Stub_factory::Stub_factory()
2563 {
2564   // The instruction template sequences are declared as static
2565   // objects and initialized first time the constructor runs.
2566  
2567   // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2568   // to reach the stub if necessary.
2569   static const Insn_template elf32_arm_stub_long_branch_any_any[] =
2570     {
2571       Insn_template::arm_insn(0xe51ff004),      // ldr   pc, [pc, #-4]
2572       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2573                                                 // dcd   R_ARM_ABS32(X)
2574     };
2575   
2576   // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2577   // available.
2578   static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2579     {
2580       Insn_template::arm_insn(0xe59fc000),      // ldr   ip, [pc, #0]
2581       Insn_template::arm_insn(0xe12fff1c),      // bx    ip
2582       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2583                                                 // dcd   R_ARM_ABS32(X)
2584     };
2585   
2586   // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2587   static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
2588     {
2589       Insn_template::thumb16_insn(0xb401),      // push {r0}
2590       Insn_template::thumb16_insn(0x4802),      // ldr  r0, [pc, #8]
2591       Insn_template::thumb16_insn(0x4684),      // mov  ip, r0
2592       Insn_template::thumb16_insn(0xbc01),      // pop  {r0}
2593       Insn_template::thumb16_insn(0x4760),      // bx   ip
2594       Insn_template::thumb16_insn(0xbf00),      // nop
2595       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2596                                                 // dcd  R_ARM_ABS32(X)
2597     };
2598   
2599   // V4T Thumb -> Thumb long branch stub. Using the stack is not
2600   // allowed.
2601   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2602     {
2603       Insn_template::thumb16_insn(0x4778),      // bx   pc
2604       Insn_template::thumb16_insn(0x46c0),      // nop
2605       Insn_template::arm_insn(0xe59fc000),      // ldr  ip, [pc, #0]
2606       Insn_template::arm_insn(0xe12fff1c),      // bx   ip
2607       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2608                                                 // dcd  R_ARM_ABS32(X)
2609     };
2610   
2611   // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2612   // available.
2613   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2614     {
2615       Insn_template::thumb16_insn(0x4778),      // bx   pc
2616       Insn_template::thumb16_insn(0x46c0),      // nop
2617       Insn_template::arm_insn(0xe51ff004),      // ldr   pc, [pc, #-4]
2618       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2619                                                 // dcd   R_ARM_ABS32(X)
2620     };
2621   
2622   // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2623   // one, when the destination is close enough.
2624   static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2625     {
2626       Insn_template::thumb16_insn(0x4778),              // bx   pc
2627       Insn_template::thumb16_insn(0x46c0),              // nop
2628       Insn_template::arm_rel_insn(0xea000000, -8),      // b    (X-8)
2629     };
2630   
2631   // ARM/Thumb -> ARM long branch stub, PIC.  On V5T and above, use
2632   // blx to reach the stub if necessary.
2633   static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
2634     {
2635       Insn_template::arm_insn(0xe59fc000),      // ldr   r12, [pc]
2636       Insn_template::arm_insn(0xe08ff00c),      // add   pc, pc, ip
2637       Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2638                                                 // dcd   R_ARM_REL32(X-4)
2639     };
2640   
2641   // ARM/Thumb -> Thumb long branch stub, PIC.  On V5T and above, use
2642   // blx to reach the stub if necessary.  We can not add into pc;
2643   // it is not guaranteed to mode switch (different in ARMv6 and
2644   // ARMv7).
2645   static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
2646     {
2647       Insn_template::arm_insn(0xe59fc004),      // ldr   r12, [pc, #4]
2648       Insn_template::arm_insn(0xe08fc00c),      // add   ip, pc, ip
2649       Insn_template::arm_insn(0xe12fff1c),      // bx    ip
2650       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2651                                                 // dcd   R_ARM_REL32(X)
2652     };
2653   
2654   // V4T ARM -> ARM long branch stub, PIC.
2655   static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2656     {
2657       Insn_template::arm_insn(0xe59fc004),      // ldr   ip, [pc, #4]
2658       Insn_template::arm_insn(0xe08fc00c),      // add   ip, pc, ip
2659       Insn_template::arm_insn(0xe12fff1c),      // bx    ip
2660       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2661                                                 // dcd   R_ARM_REL32(X)
2662     };
2663   
2664   // V4T Thumb -> ARM long branch stub, PIC.
2665   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2666     {
2667       Insn_template::thumb16_insn(0x4778),      // bx   pc
2668       Insn_template::thumb16_insn(0x46c0),      // nop
2669       Insn_template::arm_insn(0xe59fc000),      // ldr  ip, [pc, #0]
2670       Insn_template::arm_insn(0xe08cf00f),      // add  pc, ip, pc
2671       Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2672                                                 // dcd  R_ARM_REL32(X)
2673     };
2674   
2675   // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2676   // architectures.
2677   static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
2678     {
2679       Insn_template::thumb16_insn(0xb401),      // push {r0}
2680       Insn_template::thumb16_insn(0x4802),      // ldr  r0, [pc, #8]
2681       Insn_template::thumb16_insn(0x46fc),      // mov  ip, pc
2682       Insn_template::thumb16_insn(0x4484),      // add  ip, r0
2683       Insn_template::thumb16_insn(0xbc01),      // pop  {r0}
2684       Insn_template::thumb16_insn(0x4760),      // bx   ip
2685       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
2686                                                 // dcd  R_ARM_REL32(X)
2687     };
2688   
2689   // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2690   // allowed.
2691   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2692     {
2693       Insn_template::thumb16_insn(0x4778),      // bx   pc
2694       Insn_template::thumb16_insn(0x46c0),      // nop
2695       Insn_template::arm_insn(0xe59fc004),      // ldr  ip, [pc, #4]
2696       Insn_template::arm_insn(0xe08fc00c),      // add   ip, pc, ip
2697       Insn_template::arm_insn(0xe12fff1c),      // bx   ip
2698       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2699                                                 // dcd  R_ARM_REL32(X)
2700     };
2701   
2702   // Cortex-A8 erratum-workaround stubs.
2703   
2704   // Stub used for conditional branches (which may be beyond +/-1MB away,
2705   // so we can't use a conditional branch to reach this stub).
2706   
2707   // original code:
2708   //
2709   //    b<cond> X
2710   // after:
2711   //
2712   static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
2713     {
2714       Insn_template::thumb16_bcond_insn(0xd001),        //      b<cond>.n true
2715       Insn_template::thumb32_b_insn(0xf000b800, -4),    //      b.w after
2716       Insn_template::thumb32_b_insn(0xf000b800, -4)     // true:
2717                                                         //      b.w X
2718     };
2719   
2720   // Stub used for b.w and bl.w instructions.
2721   
2722   static const Insn_template elf32_arm_stub_a8_veneer_b[] =
2723     {
2724       Insn_template::thumb32_b_insn(0xf000b800, -4)     // b.w dest
2725     };
2726   
2727   static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
2728     {
2729       Insn_template::thumb32_b_insn(0xf000b800, -4)     // b.w dest
2730     };
2731   
2732   // Stub used for Thumb-2 blx.w instructions.  We modified the original blx.w
2733   // instruction (which switches to ARM mode) to point to this stub.  Jump to
2734   // the real destination using an ARM-mode branch.
2735   const Insn_template elf32_arm_stub_a8_veneer_blx[] =
2736     {
2737       Insn_template::arm_rel_insn(0xea000000, -8)       // b dest
2738     };
2739
2740   // Fill in the stub template look-up table.  Stub templates are constructed
2741   // per instance of Stub_factory for fast look-up without locking
2742   // in a thread-enabled environment.
2743
2744   this->stub_templates_[arm_stub_none] =
2745     new Stub_template(arm_stub_none, NULL, 0);
2746
2747 #define DEF_STUB(x)     \
2748   do \
2749     { \
2750       size_t array_size \
2751         = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2752       Stub_type type = arm_stub_##x; \
2753       this->stub_templates_[type] = \
2754         new Stub_template(type, elf32_arm_stub_##x, array_size); \
2755     } \
2756   while (0);
2757
2758   DEF_STUBS
2759 #undef DEF_STUB
2760 }
2761
2762 // Stub_table methods.
2763
2764 // Add a STUB with using KEY.  Caller is reponsible for avoid adding
2765 // if already a STUB with the same key has been added. 
2766
2767 template<bool big_endian>
2768 void
2769 Stub_table<big_endian>::add_reloc_stub(
2770     Reloc_stub* stub,
2771     const Reloc_stub::Key& key)
2772 {
2773   const Stub_template* stub_template = stub->stub_template();
2774   gold_assert(stub_template->type() == key.stub_type());
2775   this->reloc_stubs_[key] = stub;
2776   if (this->addralign_ < stub_template->alignment())
2777     this->addralign_ = stub_template->alignment();
2778   this->has_been_changed_ = true;
2779 }
2780
2781 template<bool big_endian>
2782 void
2783 Stub_table<big_endian>::relocate_stubs(
2784     const Relocate_info<32, big_endian>* relinfo,
2785     Target_arm<big_endian>* arm_target,
2786     Output_section* output_section,
2787     unsigned char* view,
2788     Arm_address address,
2789     section_size_type view_size)
2790 {
2791   // If we are passed a view bigger than the stub table's.  we need to
2792   // adjust the view.
2793   gold_assert(address == this->address()
2794               && (view_size
2795                   == static_cast<section_size_type>(this->data_size())));
2796
2797   for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2798       p != this->reloc_stubs_.end();
2799       ++p)
2800     {
2801       Reloc_stub* stub = p->second;
2802       const Stub_template* stub_template = stub->stub_template();
2803       if (stub_template->reloc_count() != 0)
2804         {
2805           // Adjust view to cover the stub only.
2806           section_size_type offset = stub->offset();
2807           section_size_type stub_size = stub_template->size();
2808           gold_assert(offset + stub_size <= view_size);
2809
2810           arm_target->relocate_stub(stub, relinfo, output_section,
2811                                     view + offset, address + offset,
2812                                     stub_size);
2813         }
2814     }
2815 }
2816
2817 // Reset address and file offset.
2818
2819 template<bool big_endian>
2820 void
2821 Stub_table<big_endian>::do_reset_address_and_file_offset()
2822 {
2823   off_t off = 0;
2824   uint64_t max_addralign = 1;
2825   for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2826       p != this->reloc_stubs_.end();
2827       ++p)
2828     {
2829       Reloc_stub* stub = p->second;
2830       const Stub_template* stub_template = stub->stub_template();
2831       uint64_t stub_addralign = stub_template->alignment();
2832       max_addralign = std::max(max_addralign, stub_addralign);
2833       off = align_address(off, stub_addralign);
2834       stub->set_offset(off);
2835       stub->reset_destination_address();
2836       off += stub_template->size();
2837     }
2838
2839   this->addralign_ = max_addralign;
2840   this->set_current_data_size_for_child(off);
2841 }
2842
2843 // Write out the stubs to file.
2844
2845 template<bool big_endian>
2846 void
2847 Stub_table<big_endian>::do_write(Output_file* of)
2848 {
2849   off_t offset = this->offset();
2850   const section_size_type oview_size =
2851     convert_to_section_size_type(this->data_size());
2852   unsigned char* const oview = of->get_output_view(offset, oview_size);
2853
2854   for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2855       p != this->reloc_stubs_.end();
2856       ++p)
2857     {
2858       Reloc_stub* stub = p->second;
2859       Arm_address address = this->address() + stub->offset();
2860       gold_assert(address
2861                   == align_address(address,
2862                                    stub->stub_template()->alignment()));
2863       stub->write(oview + stub->offset(), stub->stub_template()->size(),
2864                   big_endian);
2865     } 
2866   of->write_output_view(this->offset(), oview_size, oview);
2867 }
2868
2869 // Arm_input_section methods.
2870
2871 // Initialize an Arm_input_section.
2872
2873 template<bool big_endian>
2874 void
2875 Arm_input_section<big_endian>::init()
2876 {
2877   Relobj* relobj = this->relobj();
2878   unsigned int shndx = this->shndx();
2879
2880   // Cache these to speed up size and alignment queries.  It is too slow
2881   // to call section_addraglin and section_size every time.
2882   this->original_addralign_ = relobj->section_addralign(shndx);
2883   this->original_size_ = relobj->section_size(shndx);
2884
2885   // We want to make this look like the original input section after
2886   // output sections are finalized.
2887   Output_section* os = relobj->output_section(shndx);
2888   off_t offset = relobj->output_section_offset(shndx);
2889   gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2890   this->set_address(os->address() + offset);
2891   this->set_file_offset(os->offset() + offset);
2892
2893   this->set_current_data_size(this->original_size_);
2894   this->finalize_data_size();
2895 }
2896
2897 template<bool big_endian>
2898 void
2899 Arm_input_section<big_endian>::do_write(Output_file* of)
2900 {
2901   // We have to write out the original section content.
2902   section_size_type section_size;
2903   const unsigned char* section_contents =
2904     this->relobj()->section_contents(this->shndx(), &section_size, false); 
2905   of->write(this->offset(), section_contents, section_size); 
2906
2907   // If this owns a stub table and it is not empty, write it.
2908   if (this->is_stub_table_owner() && !this->stub_table_->empty())
2909     this->stub_table_->write(of);
2910 }
2911
2912 // Finalize data size.
2913
2914 template<bool big_endian>
2915 void
2916 Arm_input_section<big_endian>::set_final_data_size()
2917 {
2918   // If this owns a stub table, finalize its data size as well.
2919   if (this->is_stub_table_owner())
2920     {
2921       uint64_t address = this->address();
2922
2923       // The stub table comes after the original section contents.
2924       address += this->original_size_;
2925       address = align_address(address, this->stub_table_->addralign());
2926       off_t offset = this->offset() + (address - this->address());
2927       this->stub_table_->set_address_and_file_offset(address, offset);
2928       address += this->stub_table_->data_size();
2929       gold_assert(address == this->address() + this->current_data_size());
2930     }
2931
2932   this->set_data_size(this->current_data_size());
2933 }
2934
2935 // Reset address and file offset.
2936
2937 template<bool big_endian>
2938 void
2939 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
2940 {
2941   // Size of the original input section contents.
2942   off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2943
2944   // If this is a stub table owner, account for the stub table size.
2945   if (this->is_stub_table_owner())
2946     {
2947       Stub_table<big_endian>* stub_table = this->stub_table_;
2948
2949       // Reset the stub table's address and file offset.  The
2950       // current data size for child will be updated after that.
2951       stub_table_->reset_address_and_file_offset();
2952       off = align_address(off, stub_table_->addralign());
2953       off += stub_table->current_data_size();
2954     }
2955
2956   this->set_current_data_size(off);
2957 }
2958
2959 // Arm_output_section methods.
2960
2961 // Create a stub group for input sections from BEGIN to END.  OWNER
2962 // points to the input section to be the owner a new stub table.
2963
2964 template<bool big_endian>
2965 void
2966 Arm_output_section<big_endian>::create_stub_group(
2967   Input_section_list::const_iterator begin,
2968   Input_section_list::const_iterator end,
2969   Input_section_list::const_iterator owner,
2970   Target_arm<big_endian>* target,
2971   std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
2972 {
2973   // Currently we convert ordinary input sections into relaxed sections only
2974   // at this point but we may want to support creating relaxed input section
2975   // very early.  So we check here to see if owner is already a relaxed
2976   // section.
2977   
2978   Arm_input_section<big_endian>* arm_input_section;
2979   if (owner->is_relaxed_input_section())
2980     {
2981       arm_input_section =
2982         Arm_input_section<big_endian>::as_arm_input_section(
2983           owner->relaxed_input_section());
2984     }
2985   else
2986     {
2987       gold_assert(owner->is_input_section());
2988       // Create a new relaxed input section.
2989       arm_input_section =
2990         target->new_arm_input_section(owner->relobj(), owner->shndx());
2991       new_relaxed_sections->push_back(arm_input_section);
2992     }
2993
2994   // Create a stub table.
2995   Stub_table<big_endian>* stub_table =
2996     target->new_stub_table(arm_input_section);
2997
2998   arm_input_section->set_stub_table(stub_table);
2999   
3000   Input_section_list::const_iterator p = begin;
3001   Input_section_list::const_iterator prev_p;
3002
3003   // Look for input sections or relaxed input sections in [begin ... end].
3004   do
3005     {
3006       if (p->is_input_section() || p->is_relaxed_input_section())
3007         {
3008           // The stub table information for input sections live
3009           // in their objects.
3010           Arm_relobj<big_endian>* arm_relobj =
3011             Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
3012           arm_relobj->set_stub_table(p->shndx(), stub_table);
3013         }
3014       prev_p = p++;
3015     }
3016   while (prev_p != end);
3017 }
3018
3019 // Group input sections for stub generation.  GROUP_SIZE is roughly the limit
3020 // of stub groups.  We grow a stub group by adding input section until the
3021 // size is just below GROUP_SIZE.  The last input section will be converted
3022 // into a stub table.  If STUB_ALWAYS_AFTER_BRANCH is false, we also add
3023 // input section after the stub table, effectively double the group size.
3024 // 
3025 // This is similar to the group_sections() function in elf32-arm.c but is
3026 // implemented differently.
3027
3028 template<bool big_endian>
3029 void
3030 Arm_output_section<big_endian>::group_sections(
3031     section_size_type group_size,
3032     bool stubs_always_after_branch,
3033     Target_arm<big_endian>* target)
3034 {
3035   // We only care about sections containing code.
3036   if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
3037     return;
3038
3039   // States for grouping.
3040   typedef enum
3041   {
3042     // No group is being built.
3043     NO_GROUP,
3044     // A group is being built but the stub table is not found yet.
3045     // We keep group a stub group until the size is just under GROUP_SIZE.
3046     // The last input section in the group will be used as the stub table.
3047     FINDING_STUB_SECTION,
3048     // A group is being built and we have already found a stub table.
3049     // We enter this state to grow a stub group by adding input section
3050     // after the stub table.  This effectively doubles the group size.
3051     HAS_STUB_SECTION
3052   } State;
3053
3054   // Any newly created relaxed sections are stored here.
3055   std::vector<Output_relaxed_input_section*> new_relaxed_sections;
3056
3057   State state = NO_GROUP;
3058   section_size_type off = 0;
3059   section_size_type group_begin_offset = 0;
3060   section_size_type group_end_offset = 0;
3061   section_size_type stub_table_end_offset = 0;
3062   Input_section_list::const_iterator group_begin =
3063     this->input_sections().end();
3064   Input_section_list::const_iterator stub_table =
3065     this->input_sections().end();
3066   Input_section_list::const_iterator group_end = this->input_sections().end();
3067   for (Input_section_list::const_iterator p = this->input_sections().begin();
3068        p != this->input_sections().end();
3069        ++p)
3070     {
3071       section_size_type section_begin_offset =
3072         align_address(off, p->addralign());
3073       section_size_type section_end_offset =
3074         section_begin_offset + p->data_size(); 
3075       
3076       // Check to see if we should group the previously seens sections.
3077       switch (state)
3078         {
3079         case NO_GROUP:
3080           break;
3081
3082         case FINDING_STUB_SECTION:
3083           // Adding this section makes the group larger than GROUP_SIZE.
3084           if (section_end_offset - group_begin_offset >= group_size)
3085             {
3086               if (stubs_always_after_branch)
3087                 {       
3088                   gold_assert(group_end != this->input_sections().end());
3089                   this->create_stub_group(group_begin, group_end, group_end,
3090                                           target, &new_relaxed_sections);
3091                   state = NO_GROUP;
3092                 }
3093               else
3094                 {
3095                   // But wait, there's more!  Input sections up to
3096                   // stub_group_size bytes after the stub table can be
3097                   // handled by it too.
3098                   state = HAS_STUB_SECTION;
3099                   stub_table = group_end;
3100                   stub_table_end_offset = group_end_offset;
3101                 }
3102             }
3103             break;
3104
3105         case HAS_STUB_SECTION:
3106           // Adding this section makes the post stub-section group larger
3107           // than GROUP_SIZE.
3108           if (section_end_offset - stub_table_end_offset >= group_size)
3109            {
3110              gold_assert(group_end != this->input_sections().end());
3111              this->create_stub_group(group_begin, group_end, stub_table,
3112                                      target, &new_relaxed_sections);
3113              state = NO_GROUP;
3114            }
3115            break;
3116
3117           default:
3118             gold_unreachable();
3119         }       
3120
3121       // If we see an input section and currently there is no group, start
3122       // a new one.  Skip any empty sections.
3123       if ((p->is_input_section() || p->is_relaxed_input_section())
3124           && (p->relobj()->section_size(p->shndx()) != 0))
3125         {
3126           if (state == NO_GROUP)
3127             {
3128               state = FINDING_STUB_SECTION;
3129               group_begin = p;
3130               group_begin_offset = section_begin_offset;
3131             }
3132
3133           // Keep track of the last input section seen.
3134           group_end = p;
3135           group_end_offset = section_end_offset;
3136         }
3137
3138       off = section_end_offset;
3139     }
3140
3141   // Create a stub group for any ungrouped sections.
3142   if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
3143     {
3144       gold_assert(group_end != this->input_sections().end());
3145       this->create_stub_group(group_begin, group_end,
3146                               (state == FINDING_STUB_SECTION
3147                                ? group_end
3148                                : stub_table),
3149                                target, &new_relaxed_sections);
3150     }
3151
3152   // Convert input section into relaxed input section in a batch.
3153   if (!new_relaxed_sections.empty())
3154     this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
3155
3156   // Update the section offsets
3157   for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
3158     {
3159       Arm_relobj<big_endian>* arm_relobj =
3160         Arm_relobj<big_endian>::as_arm_relobj(
3161           new_relaxed_sections[i]->relobj());
3162       unsigned int shndx = new_relaxed_sections[i]->shndx();
3163       // Tell Arm_relobj that this input section is converted.
3164       arm_relobj->convert_input_section_to_relaxed_section(shndx);
3165     }
3166 }
3167
3168 // Arm_relobj methods.
3169
3170 // Scan relocations for stub generation.
3171
3172 template<bool big_endian>
3173 void
3174 Arm_relobj<big_endian>::scan_sections_for_stubs(
3175     Target_arm<big_endian>* arm_target,
3176     const Symbol_table* symtab,
3177     const Layout* layout)
3178 {
3179   unsigned int shnum = this->shnum();
3180   const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
3181
3182   // Read the section headers.
3183   const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
3184                                                shnum * shdr_size,
3185                                                true, true);
3186
3187   // To speed up processing, we set up hash tables for fast lookup of
3188   // input offsets to output addresses.
3189   this->initialize_input_to_output_maps();
3190
3191   const Relobj::Output_sections& out_sections(this->output_sections());
3192
3193   Relocate_info<32, big_endian> relinfo;
3194   relinfo.symtab = symtab;
3195   relinfo.layout = layout;
3196   relinfo.object = this;
3197
3198   const unsigned char* p = pshdrs + shdr_size;
3199   for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
3200     {
3201       typename elfcpp::Shdr<32, big_endian> shdr(p);
3202
3203       unsigned int sh_type = shdr.get_sh_type();
3204       if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
3205         continue;
3206
3207       off_t sh_size = shdr.get_sh_size();
3208       if (sh_size == 0)
3209         continue;
3210
3211       unsigned int index = this->adjust_shndx(shdr.get_sh_info());
3212       if (index >= this->shnum())
3213         {
3214           // Ignore reloc section with bad info.  This error will be
3215           // reported in the final link.
3216           continue;
3217         }
3218
3219       Output_section* os = out_sections[index];
3220       if (os == NULL)
3221         {
3222           // This relocation section is against a section which we
3223           // discarded.
3224           continue;
3225         }
3226       Arm_address output_offset = this->get_output_section_offset(index);
3227
3228       if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
3229         {
3230           // Ignore reloc section with unexpected symbol table.  The
3231           // error will be reported in the final link.
3232           continue;
3233         }
3234
3235       const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
3236                                                     sh_size, true, false);
3237
3238       unsigned int reloc_size;
3239       if (sh_type == elfcpp::SHT_REL)
3240         reloc_size = elfcpp::Elf_sizes<32>::rel_size;
3241       else
3242         reloc_size = elfcpp::Elf_sizes<32>::rela_size;
3243
3244       if (reloc_size != shdr.get_sh_entsize())
3245         {
3246           // Ignore reloc section with unexpected entsize.  The error
3247           // will be reported in the final link.
3248           continue;
3249         }
3250
3251       size_t reloc_count = sh_size / reloc_size;
3252       if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
3253         {
3254           // Ignore reloc section with uneven size.  The error will be
3255           // reported in the final link.
3256           continue;
3257         }
3258
3259       gold_assert(output_offset != invalid_address
3260                   || this->relocs_must_follow_section_writes());
3261
3262       // Get the section contents.  This does work for the case in which
3263       // we modify the contents of an input section.  We need to pass the
3264       // output view under such circumstances.
3265       section_size_type input_view_size = 0;
3266       const unsigned char* input_view =
3267         this->section_contents(index, &input_view_size, false);
3268
3269       relinfo.reloc_shndx = i;
3270       relinfo.data_shndx = index;
3271       arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
3272                                          reloc_count, os,
3273                                          output_offset == invalid_address,
3274                                          input_view,
3275                                          os->address(),
3276                                          input_view_size);
3277     }
3278
3279   // After we've done the relocations, we release the hash tables,
3280   // since we no longer need them.
3281   this->free_input_to_output_maps();
3282 }
3283
3284 // Count the local symbols.  The ARM backend needs to know if a symbol
3285 // is a THUMB function or not.  For global symbols, it is easy because
3286 // the Symbol object keeps the ELF symbol type.  For local symbol it is
3287 // harder because we cannot access this information.   So we override the
3288 // do_count_local_symbol in parent and scan local symbols to mark
3289 // THUMB functions.  This is not the most efficient way but I do not want to
3290 // slow down other ports by calling a per symbol targer hook inside
3291 // Sized_relobj<size, big_endian>::do_count_local_symbols. 
3292
3293 template<bool big_endian>
3294 void
3295 Arm_relobj<big_endian>::do_count_local_symbols(
3296     Stringpool_template<char>* pool,
3297     Stringpool_template<char>* dynpool)
3298 {
3299   // We need to fix-up the values of any local symbols whose type are
3300   // STT_ARM_TFUNC.
3301   
3302   // Ask parent to count the local symbols.
3303   Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
3304   const unsigned int loccount = this->local_symbol_count();
3305   if (loccount == 0)
3306     return;
3307
3308   // Intialize the thumb function bit-vector.
3309   std::vector<bool> empty_vector(loccount, false);
3310   this->local_symbol_is_thumb_function_.swap(empty_vector);
3311
3312   // Read the symbol table section header.
3313   const unsigned int symtab_shndx = this->symtab_shndx();
3314   elfcpp::Shdr<32, big_endian>
3315       symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
3316   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
3317
3318   // Read the local symbols.
3319   const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
3320   gold_assert(loccount == symtabshdr.get_sh_info());
3321   off_t locsize = loccount * sym_size;
3322   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
3323                                               locsize, true, true);
3324
3325   // Loop over the local symbols and mark any local symbols pointing
3326   // to THUMB functions.
3327
3328   // Skip the first dummy symbol.
3329   psyms += sym_size;
3330   typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
3331     this->local_values();
3332   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
3333     {
3334       elfcpp::Sym<32, big_endian> sym(psyms);
3335       elfcpp::STT st_type = sym.get_st_type();
3336       Symbol_value<32>& lv((*plocal_values)[i]);
3337       Arm_address input_value = lv.input_value();
3338
3339       if (st_type == elfcpp::STT_ARM_TFUNC
3340           || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
3341         {
3342           // This is a THUMB function.  Mark this and canonicalize the
3343           // symbol value by setting LSB.
3344           this->local_symbol_is_thumb_function_[i] = true;
3345           if ((input_value & 1) == 0)
3346             lv.set_input_value(input_value | 1);
3347         }
3348     }
3349 }
3350
3351 // Relocate sections.
3352 template<bool big_endian>
3353 void
3354 Arm_relobj<big_endian>::do_relocate_sections(
3355     const General_options& options,
3356     const Symbol_table* symtab,
3357     const Layout* layout,
3358     const unsigned char* pshdrs,
3359     typename Sized_relobj<32, big_endian>::Views* pviews)
3360 {
3361   // Call parent to relocate sections.
3362   Sized_relobj<32, big_endian>::do_relocate_sections(options, symtab, layout,
3363                                                      pshdrs, pviews); 
3364
3365   // We do not generate stubs if doing a relocatable link.
3366   if (parameters->options().relocatable())
3367     return;
3368
3369   // Relocate stub tables.
3370   unsigned int shnum = this->shnum();
3371
3372   Target_arm<big_endian>* arm_target =
3373     Target_arm<big_endian>::default_target();
3374
3375   Relocate_info<32, big_endian> relinfo;
3376   relinfo.options = &options;
3377   relinfo.symtab = symtab;
3378   relinfo.layout = layout;
3379   relinfo.object = this;
3380
3381   for (unsigned int i = 1; i < shnum; ++i)
3382     {
3383       Arm_input_section<big_endian>* arm_input_section =
3384         arm_target->find_arm_input_section(this, i);
3385
3386       if (arm_input_section == NULL
3387           || !arm_input_section->is_stub_table_owner()
3388           || arm_input_section->stub_table()->empty())
3389         continue;
3390
3391       // We cannot discard a section if it owns a stub table.
3392       Output_section* os = this->output_section(i);
3393       gold_assert(os != NULL);
3394
3395       relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
3396       relinfo.reloc_shdr = NULL;
3397       relinfo.data_shndx = i;
3398       relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
3399
3400       gold_assert((*pviews)[i].view != NULL);
3401
3402       // We are passed the output section view.  Adjust it to cover the
3403       // stub table only.
3404       Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
3405       gold_assert((stub_table->address() >= (*pviews)[i].address)
3406                   && ((stub_table->address() + stub_table->data_size())
3407                       <= (*pviews)[i].address + (*pviews)[i].view_size));
3408
3409       off_t offset = stub_table->address() - (*pviews)[i].address;
3410       unsigned char* view = (*pviews)[i].view + offset;
3411       Arm_address address = stub_table->address();
3412       section_size_type view_size = stub_table->data_size();
3413  
3414       stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
3415                                  view_size);
3416     }
3417 }
3418
3419 // Read the symbol information.
3420
3421 template<bool big_endian>
3422 void
3423 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
3424 {
3425   // Call parent class to read symbol information.
3426   Sized_relobj<32, big_endian>::do_read_symbols(sd);
3427
3428   // Read processor-specific flags in ELF file header.
3429   const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
3430                                               elfcpp::Elf_sizes<32>::ehdr_size,
3431                                               true, false);
3432   elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
3433   this->processor_specific_flags_ = ehdr.get_e_flags();
3434 }
3435
3436 // Arm_dynobj methods.
3437
3438 // Read the symbol information.
3439
3440 template<bool big_endian>
3441 void
3442 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
3443 {
3444   // Call parent class to read symbol information.
3445   Sized_dynobj<32, big_endian>::do_read_symbols(sd);
3446
3447   // Read processor-specific flags in ELF file header.
3448   const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
3449                                               elfcpp::Elf_sizes<32>::ehdr_size,
3450                                               true, false);
3451   elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
3452   this->processor_specific_flags_ = ehdr.get_e_flags();
3453 }
3454
3455 // Stub_addend_reader methods.
3456
3457 // Read the addend of a REL relocation of type R_TYPE at VIEW.
3458
3459 template<bool big_endian>
3460 elfcpp::Elf_types<32>::Elf_Swxword
3461 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
3462     unsigned int r_type,
3463     const unsigned char* view,
3464     const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
3465 {
3466   switch (r_type)
3467     {
3468     case elfcpp::R_ARM_CALL:
3469     case elfcpp::R_ARM_JUMP24:
3470     case elfcpp::R_ARM_PLT32:
3471       {
3472         typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3473         const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3474         Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3475         return utils::sign_extend<26>(val << 2);
3476       }
3477
3478     case elfcpp::R_ARM_THM_CALL:
3479     case elfcpp::R_ARM_THM_JUMP24:
3480     case elfcpp::R_ARM_THM_XPC22:
3481       {
3482         // Fetch the addend.  We use the Thumb-2 encoding (backwards
3483         // compatible with Thumb-1) involving the J1 and J2 bits.
3484         typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3485         const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3486         Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3487         Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3488
3489         uint32_t s = (upper_insn & (1 << 10)) >> 10;
3490         uint32_t upper = upper_insn & 0x3ff;
3491         uint32_t lower = lower_insn & 0x7ff;
3492         uint32_t j1 = (lower_insn & (1 << 13)) >> 13;
3493         uint32_t j2 = (lower_insn & (1 << 11)) >> 11;
3494         uint32_t i1 = j1 ^ s ? 0 : 1;
3495         uint32_t i2 = j2 ^ s ? 0 : 1;
3496
3497         return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
3498                                       | (upper << 12) | (lower << 1));
3499       }
3500
3501     case elfcpp::R_ARM_THM_JUMP19:
3502       {
3503         typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3504         const Valtype* wv = reinterpret_cast<const Valtype*>(view);
3505         Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3506         Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3507
3508         // Reconstruct the top three bits and squish the two 11 bit pieces
3509         // together.
3510         uint32_t S = (upper_insn & 0x0400) >> 10;
3511         uint32_t J1 = (lower_insn & 0x2000) >> 13;
3512         uint32_t J2 = (lower_insn & 0x0800) >> 11;
3513         uint32_t upper =
3514           (S << 8) | (J2 << 7) | (J1 << 6) | (upper_insn & 0x003f);
3515         uint32_t lower = (lower_insn & 0x07ff);
3516         return utils::sign_extend<23>((upper << 12) | (lower << 1));
3517       }
3518
3519     default:
3520       gold_unreachable();
3521     }
3522 }
3523
3524 // A class to handle the PLT data.
3525
3526 template<bool big_endian>
3527 class Output_data_plt_arm : public Output_section_data
3528 {
3529  public:
3530   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
3531     Reloc_section;
3532
3533   Output_data_plt_arm(Layout*, Output_data_space*);
3534
3535   // Add an entry to the PLT.
3536   void
3537   add_entry(Symbol* gsym);
3538
3539   // Return the .rel.plt section data.
3540   const Reloc_section*
3541   rel_plt() const
3542   { return this->rel_; }
3543
3544  protected:
3545   void
3546   do_adjust_output_section(Output_section* os);
3547
3548   // Write to a map file.
3549   void
3550   do_print_to_mapfile(Mapfile* mapfile) const
3551   { mapfile->print_output_data(this, _("** PLT")); }
3552
3553  private:
3554   // Template for the first PLT entry.
3555   static const uint32_t first_plt_entry[5];
3556
3557   // Template for subsequent PLT entries. 
3558   static const uint32_t plt_entry[3];
3559
3560   // Set the final size.
3561   void
3562   set_final_data_size()
3563   {
3564     this->set_data_size(sizeof(first_plt_entry)
3565                         + this->count_ * sizeof(plt_entry));
3566   }
3567
3568   // Write out the PLT data.
3569   void
3570   do_write(Output_file*);
3571
3572   // The reloc section.
3573   Reloc_section* rel_;
3574   // The .got.plt section.
3575   Output_data_space* got_plt_;
3576   // The number of PLT entries.
3577   unsigned int count_;
3578 };
3579
3580 // Create the PLT section.  The ordinary .got section is an argument,
3581 // since we need to refer to the start.  We also create our own .got
3582 // section just for PLT entries.
3583
3584 template<bool big_endian>
3585 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
3586                                                      Output_data_space* got_plt)
3587   : Output_section_data(4), got_plt_(got_plt), count_(0)
3588 {
3589   this->rel_ = new Reloc_section(false);
3590   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
3591                                   elfcpp::SHF_ALLOC, this->rel_);
3592 }
3593
3594 template<bool big_endian>
3595 void
3596 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
3597 {
3598   os->set_entsize(0);
3599 }
3600
3601 // Add an entry to the PLT.
3602
3603 template<bool big_endian>
3604 void
3605 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
3606 {
3607   gold_assert(!gsym->has_plt_offset());
3608
3609   // Note that when setting the PLT offset we skip the initial
3610   // reserved PLT entry.
3611   gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
3612                        + sizeof(first_plt_entry));
3613
3614   ++this->count_;
3615
3616   section_offset_type got_offset = this->got_plt_->current_data_size();
3617
3618   // Every PLT entry needs a GOT entry which points back to the PLT
3619   // entry (this will be changed by the dynamic linker, normally
3620   // lazily when the function is called).
3621   this->got_plt_->set_current_data_size(got_offset + 4);
3622
3623   // Every PLT entry needs a reloc.
3624   gsym->set_needs_dynsym_entry();
3625   this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
3626                          got_offset);
3627
3628   // Note that we don't need to save the symbol.  The contents of the
3629   // PLT are independent of which symbols are used.  The symbols only
3630   // appear in the relocations.
3631 }
3632
3633 // ARM PLTs.
3634 // FIXME:  This is not very flexible.  Right now this has only been tested
3635 // on armv5te.  If we are to support additional architecture features like
3636 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
3637
3638 // The first entry in the PLT.
3639 template<bool big_endian>
3640 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
3641 {
3642   0xe52de004,   // str   lr, [sp, #-4]!
3643   0xe59fe004,   // ldr   lr, [pc, #4]
3644   0xe08fe00e,   // add   lr, pc, lr 
3645   0xe5bef008,   // ldr   pc, [lr, #8]!
3646   0x00000000,   // &GOT[0] - .
3647 };
3648
3649 // Subsequent entries in the PLT.
3650
3651 template<bool big_endian>
3652 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
3653 {
3654   0xe28fc600,   // add   ip, pc, #0xNN00000
3655   0xe28cca00,   // add   ip, ip, #0xNN000
3656   0xe5bcf000,   // ldr   pc, [ip, #0xNNN]!
3657 };
3658
3659 // Write out the PLT.  This uses the hand-coded instructions above,
3660 // and adjusts them as needed.  This is all specified by the arm ELF
3661 // Processor Supplement.
3662
3663 template<bool big_endian>
3664 void
3665 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
3666 {
3667   const off_t offset = this->offset();
3668   const section_size_type oview_size =
3669     convert_to_section_size_type(this->data_size());
3670   unsigned char* const oview = of->get_output_view(offset, oview_size);
3671
3672   const off_t got_file_offset = this->got_plt_->offset();
3673   const section_size_type got_size =
3674     convert_to_section_size_type(this->got_plt_->data_size());
3675   unsigned char* const got_view = of->get_output_view(got_file_offset,
3676                                                       got_size);
3677   unsigned char* pov = oview;
3678
3679   Arm_address plt_address = this->address();
3680   Arm_address got_address = this->got_plt_->address();
3681
3682   // Write first PLT entry.  All but the last word are constants.
3683   const size_t num_first_plt_words = (sizeof(first_plt_entry)
3684                                       / sizeof(plt_entry[0]));
3685   for (size_t i = 0; i < num_first_plt_words - 1; i++)
3686     elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
3687   // Last word in first PLT entry is &GOT[0] - .
3688   elfcpp::Swap<32, big_endian>::writeval(pov + 16,
3689                                          got_address - (plt_address + 16));
3690   pov += sizeof(first_plt_entry);
3691
3692   unsigned char* got_pov = got_view;
3693
3694   memset(got_pov, 0, 12);
3695   got_pov += 12;
3696
3697   const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
3698   unsigned int plt_offset = sizeof(first_plt_entry);
3699   unsigned int plt_rel_offset = 0;
3700   unsigned int got_offset = 12;
3701   const unsigned int count = this->count_;
3702   for (unsigned int i = 0;
3703        i < count;
3704        ++i,
3705          pov += sizeof(plt_entry),
3706          got_pov += 4,
3707          plt_offset += sizeof(plt_entry),
3708          plt_rel_offset += rel_size,
3709          got_offset += 4)
3710     {
3711       // Set and adjust the PLT entry itself.
3712       int32_t offset = ((got_address + got_offset)
3713                          - (plt_address + plt_offset + 8));
3714
3715       gold_assert(offset >= 0 && offset < 0x0fffffff);
3716       uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
3717       elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
3718       uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
3719       elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
3720       uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
3721       elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
3722
3723       // Set the entry in the GOT.
3724       elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
3725     }
3726
3727   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
3728   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
3729
3730   of->write_output_view(offset, oview_size, oview);
3731   of->write_output_view(got_file_offset, got_size, got_view);
3732 }
3733
3734 // Create a PLT entry for a global symbol.
3735
3736 template<bool big_endian>
3737 void
3738 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
3739                                        Symbol* gsym)
3740 {
3741   if (gsym->has_plt_offset())
3742     return;
3743
3744   if (this->plt_ == NULL)
3745     {
3746       // Create the GOT sections first.
3747       this->got_section(symtab, layout);
3748
3749       this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
3750       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
3751                                       (elfcpp::SHF_ALLOC
3752                                        | elfcpp::SHF_EXECINSTR),
3753                                       this->plt_);
3754     }
3755   this->plt_->add_entry(gsym);
3756 }
3757
3758 // Report an unsupported relocation against a local symbol.
3759
3760 template<bool big_endian>
3761 void
3762 Target_arm<big_endian>::Scan::unsupported_reloc_local(
3763     Sized_relobj<32, big_endian>* object,
3764     unsigned int r_type)
3765 {
3766   gold_error(_("%s: unsupported reloc %u against local symbol"),
3767              object->name().c_str(), r_type);
3768 }
3769
3770 // We are about to emit a dynamic relocation of type R_TYPE.  If the
3771 // dynamic linker does not support it, issue an error.  The GNU linker
3772 // only issues a non-PIC error for an allocated read-only section.
3773 // Here we know the section is allocated, but we don't know that it is
3774 // read-only.  But we check for all the relocation types which the
3775 // glibc dynamic linker supports, so it seems appropriate to issue an
3776 // error even if the section is not read-only.
3777
3778 template<bool big_endian>
3779 void
3780 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
3781                                             unsigned int r_type)
3782 {
3783   switch (r_type)
3784     {
3785     // These are the relocation types supported by glibc for ARM.
3786     case elfcpp::R_ARM_RELATIVE:
3787     case elfcpp::R_ARM_COPY:
3788     case elfcpp::R_ARM_GLOB_DAT:
3789     case elfcpp::R_ARM_JUMP_SLOT:
3790     case elfcpp::R_ARM_ABS32:
3791     case elfcpp::R_ARM_ABS32_NOI:
3792     case elfcpp::R_ARM_PC24:
3793     // FIXME: The following 3 types are not supported by Android's dynamic
3794     // linker.
3795     case elfcpp::R_ARM_TLS_DTPMOD32:
3796     case elfcpp::R_ARM_TLS_DTPOFF32:
3797     case elfcpp::R_ARM_TLS_TPOFF32:
3798       return;
3799
3800     default:
3801       // This prevents us from issuing more than one error per reloc
3802       // section.  But we can still wind up issuing more than one
3803       // error per object file.
3804       if (this->issued_non_pic_error_)
3805         return;
3806       object->error(_("requires unsupported dynamic reloc; "
3807                       "recompile with -fPIC"));
3808       this->issued_non_pic_error_ = true;
3809       return;
3810
3811     case elfcpp::R_ARM_NONE:
3812       gold_unreachable();
3813     }
3814 }
3815
3816 // Scan a relocation for a local symbol.
3817 // FIXME: This only handles a subset of relocation types used by Android
3818 // on ARM v5te devices.
3819
3820 template<bool big_endian>
3821 inline void
3822 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
3823                                     Layout* layout,
3824                                     Target_arm* target,
3825                                     Sized_relobj<32, big_endian>* object,
3826                                     unsigned int data_shndx,
3827                                     Output_section* output_section,
3828                                     const elfcpp::Rel<32, big_endian>& reloc,
3829                                     unsigned int r_type,
3830                                     const elfcpp::Sym<32, big_endian>&)
3831 {
3832   r_type = get_real_reloc_type(r_type);
3833   switch (r_type)
3834     {
3835     case elfcpp::R_ARM_NONE:
3836       break;
3837
3838     case elfcpp::R_ARM_ABS32:
3839     case elfcpp::R_ARM_ABS32_NOI:
3840       // If building a shared library (or a position-independent
3841       // executable), we need to create a dynamic relocation for
3842       // this location. The relocation applied at link time will
3843       // apply the link-time value, so we flag the location with
3844       // an R_ARM_RELATIVE relocation so the dynamic loader can
3845       // relocate it easily.
3846       if (parameters->options().output_is_position_independent())
3847         {
3848           Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3849           unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3850           // If we are to add more other reloc types than R_ARM_ABS32,
3851           // we need to add check_non_pic(object, r_type) here.
3852           rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
3853                                       output_section, data_shndx,
3854                                       reloc.get_r_offset());
3855         }
3856       break;
3857
3858     case elfcpp::R_ARM_REL32:
3859     case elfcpp::R_ARM_THM_CALL:
3860     case elfcpp::R_ARM_CALL:
3861     case elfcpp::R_ARM_PREL31:
3862     case elfcpp::R_ARM_JUMP24:
3863     case elfcpp::R_ARM_PLT32:
3864     case elfcpp::R_ARM_THM_ABS5:
3865     case elfcpp::R_ARM_ABS8:
3866     case elfcpp::R_ARM_ABS12:
3867     case elfcpp::R_ARM_ABS16:
3868     case elfcpp::R_ARM_BASE_ABS:
3869     case elfcpp::R_ARM_MOVW_ABS_NC:
3870     case elfcpp::R_ARM_MOVT_ABS:
3871     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3872     case elfcpp::R_ARM_THM_MOVT_ABS:
3873     case elfcpp::R_ARM_MOVW_PREL_NC:
3874     case elfcpp::R_ARM_MOVT_PREL:
3875     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3876     case elfcpp::R_ARM_THM_MOVT_PREL:
3877       break;
3878
3879     case elfcpp::R_ARM_GOTOFF32:
3880       // We need a GOT section:
3881       target->got_section(symtab, layout);
3882       break;
3883
3884     case elfcpp::R_ARM_BASE_PREL:
3885       // FIXME: What about this?
3886       break;
3887
3888     case elfcpp::R_ARM_GOT_BREL:
3889     case elfcpp::R_ARM_GOT_PREL:
3890       {
3891         // The symbol requires a GOT entry.
3892         Output_data_got<32, big_endian>* got =
3893           target->got_section(symtab, layout);
3894         unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3895         if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
3896           {
3897             // If we are generating a shared object, we need to add a
3898             // dynamic RELATIVE relocation for this symbol's GOT entry.
3899             if (parameters->options().output_is_position_independent())
3900               {
3901                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3902                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3903                 rel_dyn->add_local_relative(
3904                     object, r_sym, elfcpp::R_ARM_RELATIVE, got,
3905                     object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
3906               }
3907           }
3908       }
3909       break;
3910
3911     case elfcpp::R_ARM_TARGET1:
3912       // This should have been mapped to another type already.
3913       // Fall through.
3914     case elfcpp::R_ARM_COPY:
3915     case elfcpp::R_ARM_GLOB_DAT:
3916     case elfcpp::R_ARM_JUMP_SLOT:
3917     case elfcpp::R_ARM_RELATIVE:
3918       // These are relocations which should only be seen by the
3919       // dynamic linker, and should never be seen here.
3920       gold_error(_("%s: unexpected reloc %u in object file"),
3921                  object->name().c_str(), r_type);
3922       break;
3923
3924     default:
3925       unsupported_reloc_local(object, r_type);
3926       break;
3927     }
3928 }
3929
3930 // Report an unsupported relocation against a global symbol.
3931
3932 template<bool big_endian>
3933 void
3934 Target_arm<big_endian>::Scan::unsupported_reloc_global(
3935     Sized_relobj<32, big_endian>* object,
3936     unsigned int r_type,
3937     Symbol* gsym)
3938 {
3939   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3940              object->name().c_str(), r_type, gsym->demangled_name().c_str());
3941 }
3942
3943 // Scan a relocation for a global symbol.
3944 // FIXME: This only handles a subset of relocation types used by Android
3945 // on ARM v5te devices.
3946
3947 template<bool big_endian>
3948 inline void
3949 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
3950                                      Layout* layout,
3951                                      Target_arm* target,
3952                                      Sized_relobj<32, big_endian>* object,
3953                                      unsigned int data_shndx,
3954                                      Output_section* output_section,
3955                                      const elfcpp::Rel<32, big_endian>& reloc,
3956                                      unsigned int r_type,
3957                                      Symbol* gsym)
3958 {
3959   r_type = get_real_reloc_type(r_type);
3960   switch (r_type)
3961     {
3962     case elfcpp::R_ARM_NONE:
3963       break;
3964
3965     case elfcpp::R_ARM_ABS32:
3966     case elfcpp::R_ARM_ABS32_NOI:
3967       {
3968         // Make a dynamic relocation if necessary.
3969         if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
3970           {
3971             if (target->may_need_copy_reloc(gsym))
3972               {
3973                 target->copy_reloc(symtab, layout, object,
3974                                    data_shndx, output_section, gsym, reloc);
3975               }
3976             else if (gsym->can_use_relative_reloc(false))
3977               {
3978                 // If we are to add more other reloc types than R_ARM_ABS32,
3979                 // we need to add check_non_pic(object, r_type) here.
3980                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3981                 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
3982                                              output_section, object,
3983                                              data_shndx, reloc.get_r_offset());
3984               }
3985             else
3986               {
3987                 // If we are to add more other reloc types than R_ARM_ABS32,
3988                 // we need to add check_non_pic(object, r_type) here.
3989                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3990                 rel_dyn->add_global(gsym, r_type, output_section, object,
3991                                     data_shndx, reloc.get_r_offset());
3992               }
3993           }
3994       }
3995       break;
3996
3997     case elfcpp::R_ARM_MOVW_ABS_NC:
3998     case elfcpp::R_ARM_MOVT_ABS:
3999     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4000     case elfcpp::R_ARM_THM_MOVT_ABS:
4001     case elfcpp::R_ARM_MOVW_PREL_NC:
4002     case elfcpp::R_ARM_MOVT_PREL:
4003     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4004     case elfcpp::R_ARM_THM_MOVT_PREL:
4005       break;
4006
4007     case elfcpp::R_ARM_THM_ABS5:
4008     case elfcpp::R_ARM_ABS8:
4009     case elfcpp::R_ARM_ABS12:
4010     case elfcpp::R_ARM_ABS16:
4011     case elfcpp::R_ARM_BASE_ABS:
4012       {
4013         // No dynamic relocs of this kinds.
4014         // Report the error in case of PIC.
4015         int flags = Symbol::NON_PIC_REF;
4016         if (gsym->type() == elfcpp::STT_FUNC
4017             || gsym->type() == elfcpp::STT_ARM_TFUNC)
4018           flags |= Symbol::FUNCTION_CALL;
4019         if (gsym->needs_dynamic_reloc(flags))
4020           check_non_pic(object, r_type);
4021       }
4022       break;
4023
4024     case elfcpp::R_ARM_REL32:
4025     case elfcpp::R_ARM_PREL31:
4026       {
4027         // Make a dynamic relocation if necessary.
4028         int flags = Symbol::NON_PIC_REF;
4029         if (gsym->needs_dynamic_reloc(flags))
4030           {
4031             if (target->may_need_copy_reloc(gsym))
4032               {
4033                 target->copy_reloc(symtab, layout, object,
4034                                    data_shndx, output_section, gsym, reloc);
4035               }
4036             else
4037               {
4038                 check_non_pic(object, r_type);
4039                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4040                 rel_dyn->add_global(gsym, r_type, output_section, object,
4041                                     data_shndx, reloc.get_r_offset());
4042               }
4043           }
4044       }
4045       break;
4046
4047     case elfcpp::R_ARM_JUMP24:
4048     case elfcpp::R_ARM_THM_CALL:
4049     case elfcpp::R_ARM_CALL:
4050       {
4051         if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
4052           target->make_plt_entry(symtab, layout, gsym);
4053         // Make a dynamic relocation if necessary.
4054         int flags = Symbol::NON_PIC_REF;
4055         if (gsym->type() == elfcpp::STT_FUNC
4056             || gsym->type() == elfcpp::STT_ARM_TFUNC)
4057           flags |= Symbol::FUNCTION_CALL;
4058         if (gsym->needs_dynamic_reloc(flags))
4059           {
4060             if (target->may_need_copy_reloc(gsym))
4061               {
4062                 target->copy_reloc(symtab, layout, object,
4063                                    data_shndx, output_section, gsym,
4064                                    reloc);
4065               }
4066             else
4067               {
4068                 check_non_pic(object, r_type);
4069                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4070                 rel_dyn->add_global(gsym, r_type, output_section, object,
4071                                     data_shndx, reloc.get_r_offset());
4072               }
4073           }
4074       }
4075       break;
4076
4077     case elfcpp::R_ARM_PLT32:
4078       // If the symbol is fully resolved, this is just a relative
4079       // local reloc.  Otherwise we need a PLT entry.
4080       if (gsym->final_value_is_known())
4081         break;
4082       // If building a shared library, we can also skip the PLT entry
4083       // if the symbol is defined in the output file and is protected
4084       // or hidden.
4085       if (gsym->is_defined()
4086           && !gsym->is_from_dynobj()
4087           && !gsym->is_preemptible())
4088         break;
4089       target->make_plt_entry(symtab, layout, gsym);
4090       break;
4091
4092     case elfcpp::R_ARM_GOTOFF32:
4093       // We need a GOT section.
4094       target->got_section(symtab, layout);
4095       break;
4096
4097     case elfcpp::R_ARM_BASE_PREL:
4098       // FIXME: What about this?
4099       break;
4100       
4101     case elfcpp::R_ARM_GOT_BREL:
4102     case elfcpp::R_ARM_GOT_PREL:
4103       {
4104         // The symbol requires a GOT entry.
4105         Output_data_got<32, big_endian>* got =
4106           target->got_section(symtab, layout);
4107         if (gsym->final_value_is_known())
4108           got->add_global(gsym, GOT_TYPE_STANDARD);
4109         else
4110           {
4111             // If this symbol is not fully resolved, we need to add a
4112             // GOT entry with a dynamic relocation.
4113             Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4114             if (gsym->is_from_dynobj()
4115                 || gsym->is_undefined()
4116                 || gsym->is_preemptible())
4117               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
4118                                        rel_dyn, elfcpp::R_ARM_GLOB_DAT);
4119             else
4120               {
4121                 if (got->add_global(gsym, GOT_TYPE_STANDARD))
4122                   rel_dyn->add_global_relative(
4123                       gsym, elfcpp::R_ARM_RELATIVE, got,
4124                       gsym->got_offset(GOT_TYPE_STANDARD));
4125               }
4126           }
4127       }
4128       break;
4129
4130     case elfcpp::R_ARM_TARGET1:
4131       // This should have been mapped to another type already.
4132       // Fall through.
4133     case elfcpp::R_ARM_COPY:
4134     case elfcpp::R_ARM_GLOB_DAT:
4135     case elfcpp::R_ARM_JUMP_SLOT:
4136     case elfcpp::R_ARM_RELATIVE:
4137       // These are relocations which should only be seen by the
4138       // dynamic linker, and should never be seen here.
4139       gold_error(_("%s: unexpected reloc %u in object file"),
4140                  object->name().c_str(), r_type);
4141       break;
4142
4143     default:
4144       unsupported_reloc_global(object, r_type, gsym);
4145       break;
4146     }
4147 }
4148
4149 // Process relocations for gc.
4150
4151 template<bool big_endian>
4152 void
4153 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
4154                                           Layout* layout,
4155                                           Sized_relobj<32, big_endian>* object,
4156                                           unsigned int data_shndx,
4157                                           unsigned int,
4158                                           const unsigned char* prelocs,
4159                                           size_t reloc_count,
4160                                           Output_section* output_section,
4161                                           bool needs_special_offset_handling,
4162                                           size_t local_symbol_count,
4163                                           const unsigned char* plocal_symbols)
4164 {
4165   typedef Target_arm<big_endian> Arm;
4166   typedef typename Target_arm<big_endian>::Scan Scan;
4167
4168   gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
4169     symtab,
4170     layout,
4171     this,
4172     object,
4173     data_shndx,
4174     prelocs,
4175     reloc_count,
4176     output_section,
4177     needs_special_offset_handling,
4178     local_symbol_count,
4179     plocal_symbols);
4180 }
4181
4182 // Scan relocations for a section.
4183
4184 template<bool big_endian>
4185 void
4186 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
4187                                     Layout* layout,
4188                                     Sized_relobj<32, big_endian>* object,
4189                                     unsigned int data_shndx,
4190                                     unsigned int sh_type,
4191                                     const unsigned char* prelocs,
4192                                     size_t reloc_count,
4193                                     Output_section* output_section,
4194                                     bool needs_special_offset_handling,
4195                                     size_t local_symbol_count,
4196                                     const unsigned char* plocal_symbols)
4197 {
4198   typedef typename Target_arm<big_endian>::Scan Scan;
4199   if (sh_type == elfcpp::SHT_RELA)
4200     {
4201       gold_error(_("%s: unsupported RELA reloc section"),
4202                  object->name().c_str());
4203       return;
4204     }
4205
4206   gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
4207     symtab,
4208     layout,
4209     this,
4210     object,
4211     data_shndx,
4212     prelocs,
4213     reloc_count,
4214     output_section,
4215     needs_special_offset_handling,
4216     local_symbol_count,
4217     plocal_symbols);
4218 }
4219
4220 // Finalize the sections.
4221
4222 template<bool big_endian>
4223 void
4224 Target_arm<big_endian>::do_finalize_sections(
4225     Layout* layout,
4226     const Input_objects* input_objects)
4227 {
4228   // Merge processor-specific flags.
4229   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4230        p != input_objects->relobj_end();
4231        ++p)
4232     {
4233       Arm_relobj<big_endian>* arm_relobj =
4234         Arm_relobj<big_endian>::as_arm_relobj(*p);
4235       this->merge_processor_specific_flags(
4236           arm_relobj->name(),
4237           arm_relobj->processor_specific_flags());
4238     } 
4239
4240   for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
4241        p != input_objects->dynobj_end();
4242        ++p)
4243     {
4244       Arm_dynobj<big_endian>* arm_dynobj =
4245         Arm_dynobj<big_endian>::as_arm_dynobj(*p);
4246       this->merge_processor_specific_flags(
4247           arm_dynobj->name(),
4248           arm_dynobj->processor_specific_flags());
4249     }
4250
4251   // Fill in some more dynamic tags.
4252   Output_data_dynamic* const odyn = layout->dynamic_data();
4253   if (odyn != NULL)
4254     {
4255       if (this->got_plt_ != NULL)
4256         odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
4257
4258       if (this->plt_ != NULL)
4259         {
4260           const Output_data* od = this->plt_->rel_plt();
4261           odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
4262           odyn->add_section_address(elfcpp::DT_JMPREL, od);
4263           odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
4264         }
4265
4266       if (this->rel_dyn_ != NULL)
4267         {
4268           const Output_data* od = this->rel_dyn_;
4269           odyn->add_section_address(elfcpp::DT_REL, od);
4270           odyn->add_section_size(elfcpp::DT_RELSZ, od);
4271           odyn->add_constant(elfcpp::DT_RELENT,
4272                              elfcpp::Elf_sizes<32>::rel_size);
4273         }
4274
4275       if (!parameters->options().shared())
4276         {
4277           // The value of the DT_DEBUG tag is filled in by the dynamic
4278           // linker at run time, and used by the debugger.
4279           odyn->add_constant(elfcpp::DT_DEBUG, 0);
4280         }
4281     }
4282
4283   // Emit any relocs we saved in an attempt to avoid generating COPY
4284   // relocs.
4285   if (this->copy_relocs_.any_saved_relocs())
4286     this->copy_relocs_.emit(this->rel_dyn_section(layout));
4287
4288   // For the ARM target, we need to add a PT_ARM_EXIDX segment for
4289   // the .ARM.exidx section.
4290   if (!layout->script_options()->saw_phdrs_clause()
4291       && !parameters->options().relocatable())
4292     {
4293       Output_section* exidx_section =
4294         layout->find_output_section(".ARM.exidx");
4295
4296       if (exidx_section != NULL
4297           && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
4298         {
4299           gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
4300                       == NULL);
4301           Output_segment*  exidx_segment =
4302             layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
4303           exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
4304         }
4305     }
4306 }
4307
4308 // Return whether a direct absolute static relocation needs to be applied.
4309 // In cases where Scan::local() or Scan::global() has created
4310 // a dynamic relocation other than R_ARM_RELATIVE, the addend
4311 // of the relocation is carried in the data, and we must not
4312 // apply the static relocation.
4313
4314 template<bool big_endian>
4315 inline bool
4316 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
4317     const Sized_symbol<32>* gsym,
4318     int ref_flags,
4319     bool is_32bit,
4320     Output_section* output_section)
4321 {
4322   // If the output section is not allocated, then we didn't call
4323   // scan_relocs, we didn't create a dynamic reloc, and we must apply
4324   // the reloc here.
4325   if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
4326       return true;
4327
4328   // For local symbols, we will have created a non-RELATIVE dynamic
4329   // relocation only if (a) the output is position independent,
4330   // (b) the relocation is absolute (not pc- or segment-relative), and
4331   // (c) the relocation is not 32 bits wide.
4332   if (gsym == NULL)
4333     return !(parameters->options().output_is_position_independent()
4334              && (ref_flags & Symbol::ABSOLUTE_REF)
4335              && !is_32bit);
4336
4337   // For global symbols, we use the same helper routines used in the
4338   // scan pass.  If we did not create a dynamic relocation, or if we
4339   // created a RELATIVE dynamic relocation, we should apply the static
4340   // relocation.
4341   bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
4342   bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
4343                  && gsym->can_use_relative_reloc(ref_flags
4344                                                  & Symbol::FUNCTION_CALL);
4345   return !has_dyn || is_rel;
4346 }
4347
4348 // Perform a relocation.
4349
4350 template<bool big_endian>
4351 inline bool
4352 Target_arm<big_endian>::Relocate::relocate(
4353     const Relocate_info<32, big_endian>* relinfo,
4354     Target_arm* target,
4355     Output_section *output_section,
4356     size_t relnum,
4357     const elfcpp::Rel<32, big_endian>& rel,
4358     unsigned int r_type,
4359     const Sized_symbol<32>* gsym,
4360     const Symbol_value<32>* psymval,
4361     unsigned char* view,
4362     Arm_address address,
4363     section_size_type /* view_size */ )
4364 {
4365   typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
4366
4367   r_type = get_real_reloc_type(r_type);
4368
4369   // If this the symbol may be a Thumb function, set thumb bit to 1.
4370   bool has_thumb_bit = ((gsym != NULL)
4371                         && (gsym->type() == elfcpp::STT_FUNC
4372                             || gsym->type() == elfcpp::STT_ARM_TFUNC));
4373
4374   // Pick the value to use for symbols defined in shared objects.
4375   Symbol_value<32> symval;
4376   if (gsym != NULL
4377       && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
4378     {
4379       symval.set_output_value(target->plt_section()->address()
4380                               + gsym->plt_offset());
4381       psymval = &symval;
4382       has_thumb_bit = 0;
4383     }
4384
4385   const Sized_relobj<32, big_endian>* object = relinfo->object;
4386   
4387   // Get the GOT offset if needed.
4388   // The GOT pointer points to the end of the GOT section.
4389   // We need to subtract the size of the GOT section to get
4390   // the actual offset to use in the relocation.
4391   bool have_got_offset = false;
4392   unsigned int got_offset = 0;
4393   switch (r_type)
4394     {
4395     case elfcpp::R_ARM_GOT_BREL:
4396     case elfcpp::R_ARM_GOT_PREL:
4397       if (gsym != NULL)
4398         {
4399           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4400           got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4401                         - target->got_size());
4402         }
4403       else
4404         {
4405           unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
4406           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4407           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4408                         - target->got_size());
4409         }
4410       have_got_offset = true;
4411       break;
4412
4413     default:
4414       break;
4415     }
4416
4417   typename Arm_relocate_functions::Status reloc_status =
4418         Arm_relocate_functions::STATUS_OKAY;
4419   switch (r_type)
4420     {
4421     case elfcpp::R_ARM_NONE:
4422       break;
4423
4424     case elfcpp::R_ARM_ABS8:
4425       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4426                                     output_section))
4427         reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
4428       break;
4429
4430     case elfcpp::R_ARM_ABS12:
4431       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4432                                     output_section))
4433         reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
4434       break;
4435
4436     case elfcpp::R_ARM_ABS16:
4437       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4438                                     output_section))
4439         reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
4440       break;
4441
4442     case elfcpp::R_ARM_ABS32:
4443       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4444                                     output_section))
4445         reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4446                                                      has_thumb_bit);
4447       break;
4448
4449     case elfcpp::R_ARM_ABS32_NOI:
4450       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4451                                     output_section))
4452         // No thumb bit for this relocation: (S + A)
4453         reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4454                                                      false);
4455       break;
4456
4457     case elfcpp::R_ARM_MOVW_ABS_NC:
4458       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4459                                     output_section))
4460         reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
4461                                                            psymval,
4462                                                            has_thumb_bit);
4463       else
4464         gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
4465                      "a shared object; recompile with -fPIC"));
4466       break;
4467
4468     case elfcpp::R_ARM_MOVT_ABS:
4469       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4470                                     output_section))
4471         reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
4472       else
4473         gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
4474                      "a shared object; recompile with -fPIC"));
4475       break;
4476
4477     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4478       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4479                                     output_section))
4480         reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
4481                                                                psymval,
4482                                                                has_thumb_bit);
4483       else
4484         gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
4485                      "making a shared object; recompile with -fPIC"));
4486       break;
4487
4488     case elfcpp::R_ARM_THM_MOVT_ABS:
4489       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4490                                     output_section))
4491         reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
4492                                                             psymval);
4493       else
4494         gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
4495                      "making a shared object; recompile with -fPIC"));
4496       break;
4497
4498     case elfcpp::R_ARM_MOVW_PREL_NC:
4499       reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
4500                                                           psymval, address,
4501                                                           has_thumb_bit);
4502       break;
4503
4504     case elfcpp::R_ARM_MOVT_PREL:
4505       reloc_status = Arm_relocate_functions::movt_prel(view, object,
4506                                                        psymval, address);
4507       break;
4508
4509     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4510       reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
4511                                                               psymval, address,
4512                                                               has_thumb_bit);
4513       break;
4514
4515     case elfcpp::R_ARM_THM_MOVT_PREL:
4516       reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
4517                                                            psymval, address);
4518       break;
4519         
4520     case elfcpp::R_ARM_REL32:
4521       reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
4522                                                    address, has_thumb_bit);
4523       break;
4524
4525     case elfcpp::R_ARM_THM_ABS5:
4526       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4527                                     output_section))
4528         reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
4529       break;
4530
4531     case elfcpp::R_ARM_THM_CALL:
4532       reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
4533                                                       address, has_thumb_bit);
4534       break;
4535
4536     case elfcpp::R_ARM_GOTOFF32:
4537       {
4538         Arm_address got_origin;
4539         got_origin = target->got_plt_section()->address();
4540         reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
4541                                                      got_origin, has_thumb_bit);
4542       }
4543       break;
4544
4545     case elfcpp::R_ARM_BASE_PREL:
4546       {
4547         uint32_t origin;
4548         // Get the addressing origin of the output segment defining the 
4549         // symbol gsym (AAELF 4.6.1.2 Relocation types)
4550         gold_assert(gsym != NULL); 
4551         if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4552           origin = gsym->output_segment()->vaddr();
4553         else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4554           origin = gsym->output_data()->address();
4555         else
4556           {
4557             gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4558                                    _("cannot find origin of R_ARM_BASE_PREL"));
4559             return true;
4560           }
4561         reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
4562       }
4563       break;
4564
4565     case elfcpp::R_ARM_BASE_ABS:
4566       {
4567         if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4568                                       output_section))
4569           break;
4570
4571         uint32_t origin;
4572         // Get the addressing origin of the output segment defining
4573         // the symbol gsym (AAELF 4.6.1.2 Relocation types).
4574         if (gsym == NULL)
4575           // R_ARM_BASE_ABS with the NULL symbol will give the
4576           // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
4577           // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
4578           origin = target->got_plt_section()->address();
4579         else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4580           origin = gsym->output_segment()->vaddr();
4581         else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4582           origin = gsym->output_data()->address();
4583         else
4584           {
4585             gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4586                                    _("cannot find origin of R_ARM_BASE_ABS"));
4587             return true;
4588           }
4589
4590         reloc_status = Arm_relocate_functions::base_abs(view, origin);
4591       }
4592       break;
4593
4594     case elfcpp::R_ARM_GOT_BREL:
4595       gold_assert(have_got_offset);
4596       reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
4597       break;
4598
4599     case elfcpp::R_ARM_GOT_PREL:
4600       gold_assert(have_got_offset);
4601       // Get the address origin for GOT PLT, which is allocated right
4602       // after the GOT section, to calculate an absolute address of
4603       // the symbol GOT entry (got_origin + got_offset).
4604       Arm_address got_origin;
4605       got_origin = target->got_plt_section()->address();
4606       reloc_status = Arm_relocate_functions::got_prel(view,
4607                                                       got_origin + got_offset,
4608                                                       address);
4609       break;
4610
4611     case elfcpp::R_ARM_PLT32:
4612       gold_assert(gsym == NULL
4613                   || gsym->has_plt_offset()
4614                   || gsym->final_value_is_known()
4615                   || (gsym->is_defined()
4616                       && !gsym->is_from_dynobj()
4617                       && !gsym->is_preemptible()));
4618       reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
4619                                                    address, has_thumb_bit);
4620       break;
4621
4622     case elfcpp::R_ARM_CALL:
4623       reloc_status = Arm_relocate_functions::call(view, object, psymval,
4624                                                   address, has_thumb_bit);
4625       break;
4626
4627     case elfcpp::R_ARM_JUMP24:
4628       reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
4629                                                     address, has_thumb_bit);
4630       break;
4631
4632     case elfcpp::R_ARM_PREL31:
4633       reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
4634                                                     address, has_thumb_bit);
4635       break;
4636
4637     case elfcpp::R_ARM_TARGET1:
4638       // This should have been mapped to another type already.
4639       // Fall through.
4640     case elfcpp::R_ARM_COPY:
4641     case elfcpp::R_ARM_GLOB_DAT:
4642     case elfcpp::R_ARM_JUMP_SLOT:
4643     case elfcpp::R_ARM_RELATIVE:
4644       // These are relocations which should only be seen by the
4645       // dynamic linker, and should never be seen here.
4646       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4647                              _("unexpected reloc %u in object file"),
4648                              r_type);
4649       break;
4650
4651     default:
4652       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4653                              _("unsupported reloc %u"),
4654                              r_type);
4655       break;
4656     }
4657
4658   // Report any errors.
4659   switch (reloc_status)
4660     {
4661     case Arm_relocate_functions::STATUS_OKAY:
4662       break;
4663     case Arm_relocate_functions::STATUS_OVERFLOW:
4664       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4665                              _("relocation overflow in relocation %u"),
4666                              r_type);
4667       break;
4668     case Arm_relocate_functions::STATUS_BAD_RELOC:
4669       gold_error_at_location(
4670         relinfo,
4671         relnum,
4672         rel.get_r_offset(),
4673         _("unexpected opcode while processing relocation %u"),
4674         r_type);
4675       break;
4676     default:
4677       gold_unreachable();
4678     }
4679
4680   return true;
4681 }
4682
4683 // Relocate section data.
4684
4685 template<bool big_endian>
4686 void
4687 Target_arm<big_endian>::relocate_section(
4688     const Relocate_info<32, big_endian>* relinfo,
4689     unsigned int sh_type,
4690     const unsigned char* prelocs,
4691     size_t reloc_count,
4692     Output_section* output_section,
4693     bool needs_special_offset_handling,
4694     unsigned char* view,
4695     Arm_address address,
4696     section_size_type view_size,
4697     const Reloc_symbol_changes* reloc_symbol_changes)
4698 {
4699   typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
4700   gold_assert(sh_type == elfcpp::SHT_REL);
4701
4702   gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
4703                          Arm_relocate>(
4704     relinfo,
4705     this,
4706     prelocs,
4707     reloc_count,
4708     output_section,
4709     needs_special_offset_handling,
4710     view,
4711     address,
4712     view_size,
4713     reloc_symbol_changes);
4714 }
4715
4716 // Return the size of a relocation while scanning during a relocatable
4717 // link.
4718
4719 template<bool big_endian>
4720 unsigned int
4721 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
4722     unsigned int r_type,
4723     Relobj* object)
4724 {
4725   r_type = get_real_reloc_type(r_type);
4726   switch (r_type)
4727     {
4728     case elfcpp::R_ARM_NONE:
4729       return 0;
4730
4731     case elfcpp::R_ARM_ABS8:
4732       return 1;
4733
4734     case elfcpp::R_ARM_ABS16:
4735     case elfcpp::R_ARM_THM_ABS5:
4736       return 2;
4737
4738     case elfcpp::R_ARM_ABS32:
4739     case elfcpp::R_ARM_ABS32_NOI:
4740     case elfcpp::R_ARM_ABS12:
4741     case elfcpp::R_ARM_BASE_ABS:
4742     case elfcpp::R_ARM_REL32:
4743     case elfcpp::R_ARM_THM_CALL:
4744     case elfcpp::R_ARM_GOTOFF32:
4745     case elfcpp::R_ARM_BASE_PREL:
4746     case elfcpp::R_ARM_GOT_BREL:
4747     case elfcpp::R_ARM_GOT_PREL:
4748     case elfcpp::R_ARM_PLT32:
4749     case elfcpp::R_ARM_CALL:
4750     case elfcpp::R_ARM_JUMP24:
4751     case elfcpp::R_ARM_PREL31:
4752     case elfcpp::R_ARM_MOVW_ABS_NC:
4753     case elfcpp::R_ARM_MOVT_ABS:
4754     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4755     case elfcpp::R_ARM_THM_MOVT_ABS:
4756     case elfcpp::R_ARM_MOVW_PREL_NC:
4757     case elfcpp::R_ARM_MOVT_PREL:
4758     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4759     case elfcpp::R_ARM_THM_MOVT_PREL:
4760       return 4;
4761
4762     case elfcpp::R_ARM_TARGET1:
4763       // This should have been mapped to another type already.
4764       // Fall through.
4765     case elfcpp::R_ARM_COPY:
4766     case elfcpp::R_ARM_GLOB_DAT:
4767     case elfcpp::R_ARM_JUMP_SLOT:
4768     case elfcpp::R_ARM_RELATIVE:
4769       // These are relocations which should only be seen by the
4770       // dynamic linker, and should never be seen here.
4771       gold_error(_("%s: unexpected reloc %u in object file"),
4772                  object->name().c_str(), r_type);
4773       return 0;
4774
4775     default:
4776       object->error(_("unsupported reloc %u in object file"), r_type);
4777       return 0;
4778     }
4779 }
4780
4781 // Scan the relocs during a relocatable link.
4782
4783 template<bool big_endian>
4784 void
4785 Target_arm<big_endian>::scan_relocatable_relocs(
4786     Symbol_table* symtab,
4787     Layout* layout,
4788     Sized_relobj<32, big_endian>* object,
4789     unsigned int data_shndx,
4790     unsigned int sh_type,
4791     const unsigned char* prelocs,
4792     size_t reloc_count,
4793     Output_section* output_section,
4794     bool needs_special_offset_handling,
4795     size_t local_symbol_count,
4796     const unsigned char* plocal_symbols,
4797     Relocatable_relocs* rr)
4798 {
4799   gold_assert(sh_type == elfcpp::SHT_REL);
4800
4801   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
4802     Relocatable_size_for_reloc> Scan_relocatable_relocs;
4803
4804   gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
4805       Scan_relocatable_relocs>(
4806     symtab,
4807     layout,
4808     object,
4809     data_shndx,
4810     prelocs,
4811     reloc_count,
4812     output_section,
4813     needs_special_offset_handling,
4814     local_symbol_count,
4815     plocal_symbols,
4816     rr);
4817 }
4818
4819 // Relocate a section during a relocatable link.
4820
4821 template<bool big_endian>
4822 void
4823 Target_arm<big_endian>::relocate_for_relocatable(
4824     const Relocate_info<32, big_endian>* relinfo,
4825     unsigned int sh_type,
4826     const unsigned char* prelocs,
4827     size_t reloc_count,
4828     Output_section* output_section,
4829     off_t offset_in_output_section,
4830     const Relocatable_relocs* rr,
4831     unsigned char* view,
4832     Arm_address view_address,
4833     section_size_type view_size,
4834     unsigned char* reloc_view,
4835     section_size_type reloc_view_size)
4836 {
4837   gold_assert(sh_type == elfcpp::SHT_REL);
4838
4839   gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
4840     relinfo,
4841     prelocs,
4842     reloc_count,
4843     output_section,
4844     offset_in_output_section,
4845     rr,
4846     view,
4847     view_address,
4848     view_size,
4849     reloc_view,
4850     reloc_view_size);
4851 }
4852
4853 // Return the value to use for a dynamic symbol which requires special
4854 // treatment.  This is how we support equality comparisons of function
4855 // pointers across shared library boundaries, as described in the
4856 // processor specific ABI supplement.
4857
4858 template<bool big_endian>
4859 uint64_t
4860 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4861 {
4862   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4863   return this->plt_section()->address() + gsym->plt_offset();
4864 }
4865
4866 // Map platform-specific relocs to real relocs
4867 //
4868 template<bool big_endian>
4869 unsigned int
4870 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
4871 {
4872   switch (r_type)
4873     {
4874     case elfcpp::R_ARM_TARGET1:
4875       // This is either R_ARM_ABS32 or R_ARM_REL32;
4876       return elfcpp::R_ARM_ABS32;
4877
4878     case elfcpp::R_ARM_TARGET2:
4879       // This can be any reloc type but ususally is R_ARM_GOT_PREL
4880       return elfcpp::R_ARM_GOT_PREL;
4881
4882     default:
4883       return r_type;
4884     }
4885 }
4886
4887 // Whether if two EABI versions V1 and V2 are compatible.
4888
4889 template<bool big_endian>
4890 bool
4891 Target_arm<big_endian>::are_eabi_versions_compatible(
4892     elfcpp::Elf_Word v1,
4893     elfcpp::Elf_Word v2)
4894 {
4895   // v4 and v5 are the same spec before and after it was released,
4896   // so allow mixing them.
4897   if ((v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
4898       || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
4899     return true;
4900
4901   return v1 == v2;
4902 }
4903
4904 // Combine FLAGS from an input object called NAME and the processor-specific
4905 // flags in the ELF header of the output.  Much of this is adapted from the
4906 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
4907 // in bfd/elf32-arm.c.
4908
4909 template<bool big_endian>
4910 void
4911 Target_arm<big_endian>::merge_processor_specific_flags(
4912     const std::string& name,
4913     elfcpp::Elf_Word flags)
4914 {
4915   if (this->are_processor_specific_flags_set())
4916     {
4917       elfcpp::Elf_Word out_flags = this->processor_specific_flags();
4918
4919       // Nothing to merge if flags equal to those in output.
4920       if (flags == out_flags)
4921         return;
4922
4923       // Complain about various flag mismatches.
4924       elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
4925       elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
4926       if (!this->are_eabi_versions_compatible(version1, version2))
4927         gold_error(_("Source object %s has EABI version %d but output has "
4928                      "EABI version %d."),
4929                    name.c_str(),
4930                    (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
4931                    (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
4932     }
4933   else
4934     {
4935       // If the input is the default architecture and had the default
4936       // flags then do not bother setting the flags for the output
4937       // architecture, instead allow future merges to do this.  If no
4938       // future merges ever set these flags then they will retain their
4939       // uninitialised values, which surprise surprise, correspond
4940       // to the default values.
4941       if (flags == 0)
4942         return;
4943
4944       // This is the first time, just copy the flags.
4945       // We only copy the EABI version for now.
4946       this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
4947     }
4948 }
4949
4950 // Adjust ELF file header.
4951 template<bool big_endian>
4952 void
4953 Target_arm<big_endian>::do_adjust_elf_header(
4954     unsigned char* view,
4955     int len) const
4956 {
4957   gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
4958
4959   elfcpp::Ehdr<32, big_endian> ehdr(view);
4960   unsigned char e_ident[elfcpp::EI_NIDENT];
4961   memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
4962
4963   if (elfcpp::arm_eabi_version(this->processor_specific_flags())
4964       == elfcpp::EF_ARM_EABI_UNKNOWN)
4965     e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
4966   else
4967     e_ident[elfcpp::EI_OSABI] = 0;
4968   e_ident[elfcpp::EI_ABIVERSION] = 0;
4969
4970   // FIXME: Do EF_ARM_BE8 adjustment.
4971
4972   elfcpp::Ehdr_write<32, big_endian> oehdr(view);
4973   oehdr.put_e_ident(e_ident);
4974 }
4975
4976 // do_make_elf_object to override the same function in the base class.
4977 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
4978 // to store ARM specific information.  Hence we need to have our own
4979 // ELF object creation.
4980
4981 template<bool big_endian>
4982 Object*
4983 Target_arm<big_endian>::do_make_elf_object(
4984     const std::string& name,
4985     Input_file* input_file,
4986     off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
4987 {
4988   int et = ehdr.get_e_type();
4989   if (et == elfcpp::ET_REL)
4990     {
4991       Arm_relobj<big_endian>* obj =
4992         new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
4993       obj->setup();
4994       return obj;
4995     }
4996   else if (et == elfcpp::ET_DYN)
4997     {
4998       Sized_dynobj<32, big_endian>* obj =
4999         new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
5000       obj->setup();
5001       return obj;
5002     }
5003   else
5004     {
5005       gold_error(_("%s: unsupported ELF file type %d"),
5006                  name.c_str(), et);
5007       return NULL;
5008     }
5009 }
5010
5011 // Return whether a relocation type used the LSB to distinguish THUMB
5012 // addresses.
5013 template<bool big_endian>
5014 bool
5015 Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
5016 {
5017   switch (r_type)
5018     {
5019     case elfcpp::R_ARM_PC24:
5020     case elfcpp::R_ARM_ABS32:
5021     case elfcpp::R_ARM_REL32:
5022     case elfcpp::R_ARM_SBREL32:
5023     case elfcpp::R_ARM_THM_CALL:
5024     case elfcpp::R_ARM_GLOB_DAT:
5025     case elfcpp::R_ARM_JUMP_SLOT:
5026     case elfcpp::R_ARM_GOTOFF32:
5027     case elfcpp::R_ARM_PLT32:
5028     case elfcpp::R_ARM_CALL:
5029     case elfcpp::R_ARM_JUMP24:
5030     case elfcpp::R_ARM_THM_JUMP24:
5031     case elfcpp::R_ARM_SBREL31:
5032     case elfcpp::R_ARM_PREL31:
5033     case elfcpp::R_ARM_MOVW_ABS_NC:
5034     case elfcpp::R_ARM_MOVW_PREL_NC:
5035     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
5036     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
5037     case elfcpp::R_ARM_THM_JUMP19:
5038     case elfcpp::R_ARM_THM_ALU_PREL_11_0:
5039     case elfcpp::R_ARM_ALU_PC_G0_NC:
5040     case elfcpp::R_ARM_ALU_PC_G0:
5041     case elfcpp::R_ARM_ALU_PC_G1_NC:
5042     case elfcpp::R_ARM_ALU_PC_G1:
5043     case elfcpp::R_ARM_ALU_PC_G2:
5044     case elfcpp::R_ARM_ALU_SB_G0_NC:
5045     case elfcpp::R_ARM_ALU_SB_G0:
5046     case elfcpp::R_ARM_ALU_SB_G1_NC:
5047     case elfcpp::R_ARM_ALU_SB_G1:
5048     case elfcpp::R_ARM_ALU_SB_G2:
5049     case elfcpp::R_ARM_MOVW_BREL_NC:
5050     case elfcpp::R_ARM_MOVW_BREL:
5051     case elfcpp::R_ARM_THM_MOVW_BREL_NC:
5052     case elfcpp::R_ARM_THM_MOVW_BREL:
5053       return true;
5054     default:
5055       return false;
5056     }
5057 }
5058
5059 // Stub-generation methods for Target_arm.
5060
5061 // Make a new Arm_input_section object.
5062
5063 template<bool big_endian>
5064 Arm_input_section<big_endian>*
5065 Target_arm<big_endian>::new_arm_input_section(
5066     Relobj* relobj,
5067     unsigned int shndx)
5068 {
5069   Input_section_specifier iss(relobj, shndx);
5070
5071   Arm_input_section<big_endian>* arm_input_section =
5072     new Arm_input_section<big_endian>(relobj, shndx);
5073   arm_input_section->init();
5074
5075   // Register new Arm_input_section in map for look-up.
5076   std::pair<typename Arm_input_section_map::iterator, bool> ins =
5077     this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
5078
5079   // Make sure that it we have not created another Arm_input_section
5080   // for this input section already.
5081   gold_assert(ins.second);
5082
5083   return arm_input_section; 
5084 }
5085
5086 // Find the Arm_input_section object corresponding to the SHNDX-th input
5087 // section of RELOBJ.
5088
5089 template<bool big_endian>
5090 Arm_input_section<big_endian>*
5091 Target_arm<big_endian>::find_arm_input_section(
5092     Relobj* relobj,
5093     unsigned int shndx) const
5094 {
5095   Input_section_specifier iss(relobj, shndx);
5096   typename Arm_input_section_map::const_iterator p =
5097     this->arm_input_section_map_.find(iss);
5098   return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
5099 }
5100
5101 // Make a new stub table.
5102
5103 template<bool big_endian>
5104 Stub_table<big_endian>*
5105 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
5106 {
5107   Stub_table<big_endian>* stub_table =
5108     new Stub_table<big_endian>(owner);
5109   this->stub_tables_.push_back(stub_table);
5110
5111   stub_table->set_address(owner->address() + owner->data_size());
5112   stub_table->set_file_offset(owner->offset() + owner->data_size());
5113   stub_table->finalize_data_size();
5114
5115   return stub_table;
5116 }
5117
5118 // The selector for arm object files.
5119
5120 template<bool big_endian>
5121 class Target_selector_arm : public Target_selector
5122 {
5123  public:
5124   Target_selector_arm()
5125     : Target_selector(elfcpp::EM_ARM, 32, big_endian,
5126                       (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
5127   { }
5128
5129   Target*
5130   do_instantiate_target()
5131   { return new Target_arm<big_endian>(); }
5132 };
5133
5134 Target_selector_arm<false> target_selector_arm;
5135 Target_selector_arm<true> target_selector_armbe;
5136
5137 } // End anonymous namespace.