2009-10-30 Doug Kwan <dougkwan@google.com>
[external/binutils.git] / gold / arm.cc
1 // arm.cc -- arm target support for gold.
2
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
6 // This file also contains borrowed and adapted code from
7 // bfd/elf32-arm.c.
8
9 // This file is part of gold.
10
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 3 of the License, or
14 // (at your option) any later version.
15
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
24 // MA 02110-1301, USA.
25
26 #include "gold.h"
27
28 #include <cstring>
29 #include <limits>
30 #include <cstdio>
31 #include <string>
32 #include <algorithm>
33
34 #include "elfcpp.h"
35 #include "parameters.h"
36 #include "reloc.h"
37 #include "arm.h"
38 #include "object.h"
39 #include "symtab.h"
40 #include "layout.h"
41 #include "output.h"
42 #include "copy-relocs.h"
43 #include "target.h"
44 #include "target-reloc.h"
45 #include "target-select.h"
46 #include "tls.h"
47 #include "defstd.h"
48 #include "gc.h"
49
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  protected:
951   // Post constructor setup.
952   void
953   do_setup()
954   {
955     // Call parent's setup method.
956     Sized_relobj<32, big_endian>::do_setup();
957
958     // Initialize look-up tables.
959     Stub_table_list empty_stub_table_list(this->shnum(), NULL);
960     this->stub_tables_.swap(empty_stub_table_list);
961   }
962
963   // Count the local symbols.
964   void
965   do_count_local_symbols(Stringpool_template<char>*,
966                          Stringpool_template<char>*);
967
968   void
969   do_relocate_sections(const General_options& options,
970                        const Symbol_table* symtab, const Layout* layout,
971                        const unsigned char* pshdrs,
972                        typename Sized_relobj<32, big_endian>::Views* pivews);
973
974  private:
975   // List of stub tables.
976   typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
977   Stub_table_list stub_tables_;
978   // Bit vector to tell if a local symbol is a thumb function or not.
979   // This is only valid after do_count_local_symbol is called.
980   std::vector<bool> local_symbol_is_thumb_function_;
981 };
982
983 // Utilities for manipulating integers of up to 32-bits
984
985 namespace utils
986 {
987   // Sign extend an n-bit unsigned integer stored in an uint32_t into
988   // an int32_t.  NO_BITS must be between 1 to 32.
989   template<int no_bits>
990   static inline int32_t
991   sign_extend(uint32_t bits)
992   {
993     gold_assert(no_bits >= 0 && no_bits <= 32);
994     if (no_bits == 32)
995       return static_cast<int32_t>(bits);
996     uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
997     bits &= mask;
998     uint32_t top_bit = 1U << (no_bits - 1);
999     int32_t as_signed = static_cast<int32_t>(bits);
1000     return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
1001   }
1002
1003   // Detects overflow of an NO_BITS integer stored in a uint32_t.
1004   template<int no_bits>
1005   static inline bool
1006   has_overflow(uint32_t bits)
1007   {
1008     gold_assert(no_bits >= 0 && no_bits <= 32);
1009     if (no_bits == 32)
1010       return false;
1011     int32_t max = (1 << (no_bits - 1)) - 1;
1012     int32_t min = -(1 << (no_bits - 1));
1013     int32_t as_signed = static_cast<int32_t>(bits);
1014     return as_signed > max || as_signed < min;
1015   }
1016
1017   // Detects overflow of an NO_BITS integer stored in a uint32_t when it
1018   // fits in the given number of bits as either a signed or unsigned value.
1019   // For example, has_signed_unsigned_overflow<8> would check
1020   // -128 <= bits <= 255
1021   template<int no_bits>
1022   static inline bool
1023   has_signed_unsigned_overflow(uint32_t bits)
1024   {
1025     gold_assert(no_bits >= 2 && no_bits <= 32);
1026     if (no_bits == 32)
1027       return false;
1028     int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
1029     int32_t min = -(1 << (no_bits - 1));
1030     int32_t as_signed = static_cast<int32_t>(bits);
1031     return as_signed > max || as_signed < min;
1032   }
1033
1034   // Select bits from A and B using bits in MASK.  For each n in [0..31],
1035   // the n-th bit in the result is chosen from the n-th bits of A and B.
1036   // A zero selects A and a one selects B.
1037   static inline uint32_t
1038   bit_select(uint32_t a, uint32_t b, uint32_t mask)
1039   { return (a & ~mask) | (b & mask); }
1040 };
1041
1042 template<bool big_endian>
1043 class Target_arm : public Sized_target<32, big_endian>
1044 {
1045  public:
1046   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1047     Reloc_section;
1048
1049   Target_arm()
1050     : Sized_target<32, big_endian>(&arm_info),
1051       got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
1052       copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
1053       may_use_blx_(true), should_force_pic_veneer_(false)
1054   { }
1055
1056   // Whether we can use BLX.
1057   bool
1058   may_use_blx() const
1059   { return this->may_use_blx_; }
1060
1061   // Set use-BLX flag.
1062   void
1063   set_may_use_blx(bool value)
1064   { this->may_use_blx_ = value; }
1065   
1066   // Whether we force PCI branch veneers.
1067   bool
1068   should_force_pic_veneer() const
1069   { return this->should_force_pic_veneer_; }
1070
1071   // Set PIC veneer flag.
1072   void
1073   set_should_force_pic_veneer(bool value)
1074   { this->should_force_pic_veneer_ = value; }
1075   
1076   // Whether we use THUMB-2 instructions.
1077   bool
1078   using_thumb2() const
1079   {
1080     // FIXME:  This should not hard-coded.
1081     return false;
1082   }
1083
1084   // Whether we use THUMB/THUMB-2 instructions only.
1085   bool
1086   using_thumb_only() const
1087   {
1088     // FIXME:  This should not hard-coded.
1089     return false;
1090   }
1091
1092   // Process the relocations to determine unreferenced sections for 
1093   // garbage collection.
1094   void
1095   gc_process_relocs(Symbol_table* symtab,
1096                     Layout* layout,
1097                     Sized_relobj<32, big_endian>* object,
1098                     unsigned int data_shndx,
1099                     unsigned int sh_type,
1100                     const unsigned char* prelocs,
1101                     size_t reloc_count,
1102                     Output_section* output_section,
1103                     bool needs_special_offset_handling,
1104                     size_t local_symbol_count,
1105                     const unsigned char* plocal_symbols);
1106
1107   // Scan the relocations to look for symbol adjustments.
1108   void
1109   scan_relocs(Symbol_table* symtab,
1110               Layout* layout,
1111               Sized_relobj<32, big_endian>* object,
1112               unsigned int data_shndx,
1113               unsigned int sh_type,
1114               const unsigned char* prelocs,
1115               size_t reloc_count,
1116               Output_section* output_section,
1117               bool needs_special_offset_handling,
1118               size_t local_symbol_count,
1119               const unsigned char* plocal_symbols);
1120
1121   // Finalize the sections.
1122   void
1123   do_finalize_sections(Layout*);
1124
1125   // Return the value to use for a dynamic symbol which requires special
1126   // treatment.
1127   uint64_t
1128   do_dynsym_value(const Symbol*) const;
1129
1130   // Relocate a section.
1131   void
1132   relocate_section(const Relocate_info<32, big_endian>*,
1133                    unsigned int sh_type,
1134                    const unsigned char* prelocs,
1135                    size_t reloc_count,
1136                    Output_section* output_section,
1137                    bool needs_special_offset_handling,
1138                    unsigned char* view,
1139                    Arm_address view_address,
1140                    section_size_type view_size,
1141                    const Reloc_symbol_changes*);
1142
1143   // Scan the relocs during a relocatable link.
1144   void
1145   scan_relocatable_relocs(Symbol_table* symtab,
1146                           Layout* layout,
1147                           Sized_relobj<32, big_endian>* object,
1148                           unsigned int data_shndx,
1149                           unsigned int sh_type,
1150                           const unsigned char* prelocs,
1151                           size_t reloc_count,
1152                           Output_section* output_section,
1153                           bool needs_special_offset_handling,
1154                           size_t local_symbol_count,
1155                           const unsigned char* plocal_symbols,
1156                           Relocatable_relocs*);
1157
1158   // Relocate a section during a relocatable link.
1159   void
1160   relocate_for_relocatable(const Relocate_info<32, big_endian>*,
1161                            unsigned int sh_type,
1162                            const unsigned char* prelocs,
1163                            size_t reloc_count,
1164                            Output_section* output_section,
1165                            off_t offset_in_output_section,
1166                            const Relocatable_relocs*,
1167                            unsigned char* view,
1168                            Arm_address view_address,
1169                            section_size_type view_size,
1170                            unsigned char* reloc_view,
1171                            section_size_type reloc_view_size);
1172
1173   // Return whether SYM is defined by the ABI.
1174   bool
1175   do_is_defined_by_abi(Symbol* sym) const
1176   { return strcmp(sym->name(), "__tls_get_addr") == 0; }
1177
1178   // Return the size of the GOT section.
1179   section_size_type
1180   got_size()
1181   {
1182     gold_assert(this->got_ != NULL);
1183     return this->got_->data_size();
1184   }
1185
1186   // Map platform-specific reloc types
1187   static unsigned int
1188   get_real_reloc_type (unsigned int r_type);
1189
1190   // Get the default ARM target.
1191   static const Target_arm<big_endian>&
1192   default_target()
1193   {
1194     gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
1195                 && parameters->target().is_big_endian() == big_endian);
1196     return static_cast<const Target_arm<big_endian>&>(parameters->target());
1197   }
1198
1199  private:
1200   // The class which scans relocations.
1201   class Scan
1202   {
1203    public:
1204     Scan()
1205       : issued_non_pic_error_(false)
1206     { }
1207
1208     inline void
1209     local(Symbol_table* symtab, Layout* layout, Target_arm* target,
1210           Sized_relobj<32, big_endian>* object,
1211           unsigned int data_shndx,
1212           Output_section* output_section,
1213           const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1214           const elfcpp::Sym<32, big_endian>& lsym);
1215
1216     inline void
1217     global(Symbol_table* symtab, Layout* layout, Target_arm* target,
1218            Sized_relobj<32, big_endian>* object,
1219            unsigned int data_shndx,
1220            Output_section* output_section,
1221            const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
1222            Symbol* gsym);
1223
1224    private:
1225     static void
1226     unsupported_reloc_local(Sized_relobj<32, big_endian>*,
1227                             unsigned int r_type);
1228
1229     static void
1230     unsupported_reloc_global(Sized_relobj<32, big_endian>*,
1231                              unsigned int r_type, Symbol*);
1232
1233     void
1234     check_non_pic(Relobj*, unsigned int r_type);
1235
1236     // Almost identical to Symbol::needs_plt_entry except that it also
1237     // handles STT_ARM_TFUNC.
1238     static bool
1239     symbol_needs_plt_entry(const Symbol* sym)
1240     {
1241       // An undefined symbol from an executable does not need a PLT entry.
1242       if (sym->is_undefined() && !parameters->options().shared())
1243         return false;
1244
1245       return (!parameters->doing_static_link()
1246               && (sym->type() == elfcpp::STT_FUNC
1247                   || sym->type() == elfcpp::STT_ARM_TFUNC)
1248               && (sym->is_from_dynobj()
1249                   || sym->is_undefined()
1250                   || sym->is_preemptible()));
1251     }
1252
1253     // Whether we have issued an error about a non-PIC compilation.
1254     bool issued_non_pic_error_;
1255   };
1256
1257   // The class which implements relocation.
1258   class Relocate
1259   {
1260    public:
1261     Relocate()
1262     { }
1263
1264     ~Relocate()
1265     { }
1266
1267     // Return whether the static relocation needs to be applied.
1268     inline bool
1269     should_apply_static_reloc(const Sized_symbol<32>* gsym,
1270                               int ref_flags,
1271                               bool is_32bit,
1272                               Output_section* output_section);
1273
1274     // Do a relocation.  Return false if the caller should not issue
1275     // any warnings about this relocation.
1276     inline bool
1277     relocate(const Relocate_info<32, big_endian>*, Target_arm*,
1278              Output_section*,  size_t relnum,
1279              const elfcpp::Rel<32, big_endian>&,
1280              unsigned int r_type, const Sized_symbol<32>*,
1281              const Symbol_value<32>*,
1282              unsigned char*, Arm_address,
1283              section_size_type);
1284
1285     // Return whether we want to pass flag NON_PIC_REF for this
1286     // reloc.
1287     static inline bool
1288     reloc_is_non_pic (unsigned int r_type)
1289     {
1290       switch (r_type)
1291         {
1292         case elfcpp::R_ARM_REL32:
1293         case elfcpp::R_ARM_THM_CALL:
1294         case elfcpp::R_ARM_CALL:
1295         case elfcpp::R_ARM_JUMP24:
1296         case elfcpp::R_ARM_PREL31:
1297         case elfcpp::R_ARM_THM_ABS5:
1298         case elfcpp::R_ARM_ABS8:
1299         case elfcpp::R_ARM_ABS12:
1300         case elfcpp::R_ARM_ABS16:
1301         case elfcpp::R_ARM_BASE_ABS:
1302           return true;
1303         default:
1304           return false;
1305         }
1306     }
1307   };
1308
1309   // A class which returns the size required for a relocation type,
1310   // used while scanning relocs during a relocatable link.
1311   class Relocatable_size_for_reloc
1312   {
1313    public:
1314     unsigned int
1315     get_size_for_reloc(unsigned int, Relobj*);
1316   };
1317
1318   // Get the GOT section, creating it if necessary.
1319   Output_data_got<32, big_endian>*
1320   got_section(Symbol_table*, Layout*);
1321
1322   // Get the GOT PLT section.
1323   Output_data_space*
1324   got_plt_section() const
1325   {
1326     gold_assert(this->got_plt_ != NULL);
1327     return this->got_plt_;
1328   }
1329
1330   // Create a PLT entry for a global symbol.
1331   void
1332   make_plt_entry(Symbol_table*, Layout*, Symbol*);
1333
1334   // Get the PLT section.
1335   const Output_data_plt_arm<big_endian>*
1336   plt_section() const
1337   {
1338     gold_assert(this->plt_ != NULL);
1339     return this->plt_;
1340   }
1341
1342   // Get the dynamic reloc section, creating it if necessary.
1343   Reloc_section*
1344   rel_dyn_section(Layout*);
1345
1346   // Return true if the symbol may need a COPY relocation.
1347   // References from an executable object to non-function symbols
1348   // defined in a dynamic object may need a COPY relocation.
1349   bool
1350   may_need_copy_reloc(Symbol* gsym)
1351   {
1352     return (gsym->type() != elfcpp::STT_ARM_TFUNC
1353             && gsym->may_need_copy_reloc());
1354   }
1355
1356   // Add a potential copy relocation.
1357   void
1358   copy_reloc(Symbol_table* symtab, Layout* layout,
1359              Sized_relobj<32, big_endian>* object,
1360              unsigned int shndx, Output_section* output_section,
1361              Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
1362   {
1363     this->copy_relocs_.copy_reloc(symtab, layout,
1364                                   symtab->get_sized_symbol<32>(sym),
1365                                   object, shndx, output_section, reloc,
1366                                   this->rel_dyn_section(layout));
1367   }
1368
1369   // Information about this specific target which we pass to the
1370   // general Target structure.
1371   static const Target::Target_info arm_info;
1372
1373   // The types of GOT entries needed for this platform.
1374   enum Got_type
1375   {
1376     GOT_TYPE_STANDARD = 0       // GOT entry for a regular symbol
1377   };
1378
1379   // The GOT section.
1380   Output_data_got<32, big_endian>* got_;
1381   // The PLT section.
1382   Output_data_plt_arm<big_endian>* plt_;
1383   // The GOT PLT section.
1384   Output_data_space* got_plt_;
1385   // The dynamic reloc section.
1386   Reloc_section* rel_dyn_;
1387   // Relocs saved to avoid a COPY reloc.
1388   Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
1389   // Space for variables copied with a COPY reloc.
1390   Output_data_space* dynbss_;
1391   // Whether we can use BLX.
1392   bool may_use_blx_;
1393   // Whether we force PIC branch veneers.
1394   bool should_force_pic_veneer_;
1395 };
1396
1397 template<bool big_endian>
1398 const Target::Target_info Target_arm<big_endian>::arm_info =
1399 {
1400   32,                   // size
1401   big_endian,           // is_big_endian
1402   elfcpp::EM_ARM,       // machine_code
1403   false,                // has_make_symbol
1404   false,                // has_resolve
1405   false,                // has_code_fill
1406   true,                 // is_default_stack_executable
1407   '\0',                 // wrap_char
1408   "/usr/lib/libc.so.1", // dynamic_linker
1409   0x8000,               // default_text_segment_address
1410   0x1000,               // abi_pagesize (overridable by -z max-page-size)
1411   0x1000,               // common_pagesize (overridable by -z common-page-size)
1412   elfcpp::SHN_UNDEF,    // small_common_shndx
1413   elfcpp::SHN_UNDEF,    // large_common_shndx
1414   0,                    // small_common_section_flags
1415   0                     // large_common_section_flags
1416 };
1417
1418 // Arm relocate functions class
1419 //
1420
1421 template<bool big_endian>
1422 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
1423 {
1424  public:
1425   typedef enum
1426   {
1427     STATUS_OKAY,        // No error during relocation.
1428     STATUS_OVERFLOW,    // Relocation oveflow.
1429     STATUS_BAD_RELOC    // Relocation cannot be applied.
1430   } Status;
1431
1432  private:
1433   typedef Relocate_functions<32, big_endian> Base;
1434   typedef Arm_relocate_functions<big_endian> This;
1435
1436   // Get an symbol value of *PSYMVAL with an ADDEND.  This is a wrapper
1437   // to Symbol_value::value().  If HAS_THUMB_BIT is true, that LSB is used
1438   // to distinguish ARM and THUMB functions and it is treated specially.
1439   static inline Symbol_value<32>::Value
1440   arm_symbol_value (const Sized_relobj<32, big_endian> *object,
1441                     const Symbol_value<32>* psymval,
1442                     Symbol_value<32>::Value addend,
1443                     bool has_thumb_bit)
1444   {
1445     typedef Symbol_value<32>::Value Valtype;
1446
1447     if (has_thumb_bit)
1448       {
1449         Valtype raw = psymval->value(object, 0);
1450         Valtype thumb_bit = raw & 1;
1451         return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
1452       }
1453     else
1454       return psymval->value(object, addend);
1455   }
1456
1457   // Encoding of imm16 argument for movt and movw ARM instructions
1458   // from ARM ARM:
1459   //     
1460   //     imm16 := imm4 | imm12
1461   //
1462   //  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 
1463   // +-------+---------------+-------+-------+-----------------------+
1464   // |       |               |imm4   |       |imm12                  |
1465   // +-------+---------------+-------+-------+-----------------------+
1466
1467   // Extract the relocation addend from VAL based on the ARM
1468   // instruction encoding described above.
1469   static inline typename elfcpp::Swap<32, big_endian>::Valtype
1470   extract_arm_movw_movt_addend(
1471       typename elfcpp::Swap<32, big_endian>::Valtype val)
1472   {
1473     // According to the Elf ABI for ARM Architecture the immediate
1474     // field is sign-extended to form the addend.
1475     return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
1476   }
1477
1478   // Insert X into VAL based on the ARM instruction encoding described
1479   // above.
1480   static inline typename elfcpp::Swap<32, big_endian>::Valtype
1481   insert_val_arm_movw_movt(
1482       typename elfcpp::Swap<32, big_endian>::Valtype val,
1483       typename elfcpp::Swap<32, big_endian>::Valtype x)
1484   {
1485     val &= 0xfff0f000;
1486     val |= x & 0x0fff;
1487     val |= (x & 0xf000) << 4;
1488     return val;
1489   }
1490
1491   // Encoding of imm16 argument for movt and movw Thumb2 instructions
1492   // from ARM ARM:
1493   //     
1494   //     imm16 := imm4 | i | imm3 | imm8
1495   //
1496   //  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 
1497   // +---------+-+-----------+-------++-+-----+-------+---------------+
1498   // |         |i|           |imm4   || |imm3 |       |imm8           |
1499   // +---------+-+-----------+-------++-+-----+-------+---------------+
1500
1501   // Extract the relocation addend from VAL based on the Thumb2
1502   // instruction encoding described above.
1503   static inline typename elfcpp::Swap<32, big_endian>::Valtype
1504   extract_thumb_movw_movt_addend(
1505       typename elfcpp::Swap<32, big_endian>::Valtype val)
1506   {
1507     // According to the Elf ABI for ARM Architecture the immediate
1508     // field is sign-extended to form the addend.
1509     return utils::sign_extend<16>(((val >> 4) & 0xf000)
1510                                   | ((val >> 15) & 0x0800)
1511                                   | ((val >> 4) & 0x0700)
1512                                   | (val & 0x00ff));
1513   }
1514
1515   // Insert X into VAL based on the Thumb2 instruction encoding
1516   // described above.
1517   static inline typename elfcpp::Swap<32, big_endian>::Valtype
1518   insert_val_thumb_movw_movt(
1519       typename elfcpp::Swap<32, big_endian>::Valtype val,
1520       typename elfcpp::Swap<32, big_endian>::Valtype x)
1521   {
1522     val &= 0xfbf08f00;
1523     val |= (x & 0xf000) << 4;
1524     val |= (x & 0x0800) << 15;
1525     val |= (x & 0x0700) << 4;
1526     val |= (x & 0x00ff);
1527     return val;
1528   }
1529
1530   // FIXME: This probably only works for Android on ARM v5te. We should
1531   // following GNU ld for the general case.
1532   template<unsigned r_type>
1533   static inline typename This::Status
1534   arm_branch_common(unsigned char *view,
1535                     const Sized_relobj<32, big_endian>* object,
1536                     const Symbol_value<32>* psymval,
1537                     Arm_address address,
1538                     bool has_thumb_bit)
1539   {
1540     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1541     Valtype* wv = reinterpret_cast<Valtype*>(view);
1542     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1543      
1544     bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
1545                       && ((val & 0x0f000000UL) == 0x0a000000UL);
1546     bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
1547     bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
1548                             && ((val & 0x0f000000UL) == 0x0b000000UL);
1549     bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
1550     bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
1551
1552     if (r_type == elfcpp::R_ARM_CALL)
1553       {
1554         if (!insn_is_uncond_bl && !insn_is_blx)
1555           return This::STATUS_BAD_RELOC;
1556       }
1557     else if (r_type == elfcpp::R_ARM_JUMP24)
1558       {
1559         if (!insn_is_b && !insn_is_cond_bl)
1560           return This::STATUS_BAD_RELOC;
1561       }
1562     else if (r_type == elfcpp::R_ARM_PLT32)
1563       {
1564         if (!insn_is_any_branch)
1565           return This::STATUS_BAD_RELOC;
1566       }
1567     else
1568       gold_unreachable();
1569
1570     Valtype addend = utils::sign_extend<26>(val << 2);
1571     Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1572                  - address);
1573
1574     // If target has thumb bit set, we need to either turn the BL
1575     // into a BLX (for ARMv5 or above) or generate a stub.
1576     if (x & 1)
1577       {
1578         // Turn BL to BLX.
1579         if (insn_is_uncond_bl)
1580           val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
1581         else
1582           return This::STATUS_BAD_RELOC;
1583       }
1584     else
1585       gold_assert(!insn_is_blx);
1586
1587     val = utils::bit_select(val, (x >> 2), 0xffffffUL);
1588     elfcpp::Swap<32, big_endian>::writeval(wv, val);
1589     return (utils::has_overflow<26>(x)
1590             ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
1591   }
1592
1593  public:
1594
1595   // R_ARM_ABS8: S + A
1596   static inline typename This::Status
1597   abs8(unsigned char *view,
1598        const Sized_relobj<32, big_endian>* object,
1599        const Symbol_value<32>* psymval)
1600   {
1601     typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
1602     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1603     Valtype* wv = reinterpret_cast<Valtype*>(view);
1604     Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
1605     Reltype addend = utils::sign_extend<8>(val);
1606     Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1607     val = utils::bit_select(val, x, 0xffU);
1608     elfcpp::Swap<8, big_endian>::writeval(wv, val);
1609     return (utils::has_signed_unsigned_overflow<8>(x)
1610             ? This::STATUS_OVERFLOW
1611             : This::STATUS_OKAY);
1612   }
1613
1614   // R_ARM_THM_ABS5: S + A
1615   static inline typename This::Status
1616   thm_abs5(unsigned char *view,
1617        const Sized_relobj<32, big_endian>* object,
1618        const Symbol_value<32>* psymval)
1619   {
1620     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1621     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1622     Valtype* wv = reinterpret_cast<Valtype*>(view);
1623     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1624     Reltype addend = (val & 0x7e0U) >> 6;
1625     Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1626     val = utils::bit_select(val, x << 6, 0x7e0U);
1627     elfcpp::Swap<16, big_endian>::writeval(wv, val);
1628     return (utils::has_overflow<5>(x)
1629             ? This::STATUS_OVERFLOW
1630             : This::STATUS_OKAY);
1631   }
1632
1633   // R_ARM_ABS12: S + A
1634   static inline typename This::Status
1635   abs12(unsigned char *view,
1636        const Sized_relobj<32, big_endian>* object,
1637        const Symbol_value<32>* psymval)
1638   {
1639     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1640     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1641     Valtype* wv = reinterpret_cast<Valtype*>(view);
1642     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1643     Reltype addend = val & 0x0fffU;
1644     Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1645     val = utils::bit_select(val, x, 0x0fffU);
1646     elfcpp::Swap<32, big_endian>::writeval(wv, val);
1647     return (utils::has_overflow<12>(x)
1648             ? This::STATUS_OVERFLOW
1649             : This::STATUS_OKAY);
1650   }
1651
1652   // R_ARM_ABS16: S + A
1653   static inline typename This::Status
1654   abs16(unsigned char *view,
1655        const Sized_relobj<32, big_endian>* object,
1656        const Symbol_value<32>* psymval)
1657   {
1658     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1659     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1660     Valtype* wv = reinterpret_cast<Valtype*>(view);
1661     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
1662     Reltype addend = utils::sign_extend<16>(val);
1663     Reltype x = This::arm_symbol_value(object, psymval, addend, false);
1664     val = utils::bit_select(val, x, 0xffffU);
1665     elfcpp::Swap<16, big_endian>::writeval(wv, val);
1666     return (utils::has_signed_unsigned_overflow<16>(x)
1667             ? This::STATUS_OVERFLOW
1668             : This::STATUS_OKAY);
1669   }
1670
1671   // R_ARM_ABS32: (S + A) | T
1672   static inline typename This::Status
1673   abs32(unsigned char *view,
1674         const Sized_relobj<32, big_endian>* object,
1675         const Symbol_value<32>* psymval,
1676         bool has_thumb_bit)
1677   {
1678     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1679     Valtype* wv = reinterpret_cast<Valtype*>(view);
1680     Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1681     Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1682     elfcpp::Swap<32, big_endian>::writeval(wv, x);
1683     return This::STATUS_OKAY;
1684   }
1685
1686   // R_ARM_REL32: (S + A) | T - P
1687   static inline typename This::Status
1688   rel32(unsigned char *view,
1689         const Sized_relobj<32, big_endian>* object,
1690         const Symbol_value<32>* psymval,
1691         Arm_address address,
1692         bool has_thumb_bit)
1693   {
1694     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1695     Valtype* wv = reinterpret_cast<Valtype*>(view);
1696     Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
1697     Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit) 
1698                  - address);
1699     elfcpp::Swap<32, big_endian>::writeval(wv, x);
1700     return This::STATUS_OKAY;
1701   }
1702
1703   // R_ARM_THM_CALL: (S + A) | T - P
1704   static inline typename This::Status
1705   thm_call(unsigned char *view,
1706            const Sized_relobj<32, big_endian>* object,
1707            const Symbol_value<32>* psymval,
1708            Arm_address address,
1709            bool has_thumb_bit)
1710   {
1711     // A thumb call consists of two instructions.
1712     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1713     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1714     Valtype* wv = reinterpret_cast<Valtype*>(view);
1715     Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
1716     Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
1717     // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
1718     gold_assert((lo & 0xf800) == 0xf800);
1719     Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
1720                                            | ((lo & 0x7ff) << 1));
1721     Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1722                  - address);
1723
1724     // If target has no thumb bit set, we need to either turn the BL
1725     // into a BLX (for ARMv5 or above) or generate a stub.
1726     if ((x & 1) == 0)
1727       {
1728         // This only works for ARMv5 and above with interworking enabled.
1729         lo &= 0xefff;
1730       }
1731     hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
1732     lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
1733     elfcpp::Swap<16, big_endian>::writeval(wv, hi);
1734     elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
1735     return (utils::has_overflow<23>(x)
1736             ? This::STATUS_OVERFLOW
1737             : This::STATUS_OKAY);
1738   }
1739
1740   // R_ARM_BASE_PREL: B(S) + A - P
1741   static inline typename This::Status
1742   base_prel(unsigned char* view,
1743             Arm_address origin,
1744             Arm_address address)
1745   {
1746     Base::rel32(view, origin - address);
1747     return STATUS_OKAY;
1748   }
1749
1750   // R_ARM_BASE_ABS: B(S) + A
1751   static inline typename This::Status
1752   base_abs(unsigned char* view,
1753             Arm_address origin)
1754   {
1755     Base::rel32(view, origin);
1756     return STATUS_OKAY;
1757   }
1758
1759   // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
1760   static inline typename This::Status
1761   got_brel(unsigned char* view,
1762            typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
1763   {
1764     Base::rel32(view, got_offset);
1765     return This::STATUS_OKAY;
1766   }
1767
1768   // R_ARM_GOT_PREL: GOT(S) + A â€“ P
1769   static inline typename This::Status
1770   got_prel(unsigned char* view,
1771            typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
1772            Arm_address address)
1773   {
1774     Base::rel32(view, got_offset - address);
1775     return This::STATUS_OKAY;
1776   }
1777
1778   // R_ARM_PLT32: (S + A) | T - P
1779   static inline typename This::Status
1780   plt32(unsigned char *view,
1781         const Sized_relobj<32, big_endian>* object,
1782         const Symbol_value<32>* psymval,
1783         Arm_address address,
1784         bool has_thumb_bit)
1785   {
1786     return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
1787                                                   address, has_thumb_bit);
1788   }
1789
1790   // R_ARM_CALL: (S + A) | T - P
1791   static inline typename This::Status
1792   call(unsigned char *view,
1793        const Sized_relobj<32, big_endian>* object,
1794        const Symbol_value<32>* psymval,
1795        Arm_address address,
1796        bool has_thumb_bit)
1797   {
1798     return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
1799                                                  address, has_thumb_bit);
1800   }
1801
1802   // R_ARM_JUMP24: (S + A) | T - P
1803   static inline typename This::Status
1804   jump24(unsigned char *view,
1805          const Sized_relobj<32, big_endian>* object,
1806          const Symbol_value<32>* psymval,
1807          Arm_address address,
1808          bool has_thumb_bit)
1809   {
1810     return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
1811                                                    address, has_thumb_bit);
1812   }
1813
1814   // R_ARM_PREL: (S + A) | T - P
1815   static inline typename This::Status
1816   prel31(unsigned char *view,
1817          const Sized_relobj<32, big_endian>* object,
1818          const Symbol_value<32>* psymval,
1819          Arm_address address,
1820          bool has_thumb_bit)
1821   {
1822     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1823     Valtype* wv = reinterpret_cast<Valtype*>(view);
1824     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1825     Valtype addend = utils::sign_extend<31>(val);
1826     Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1827                  - address);
1828     val = utils::bit_select(val, x, 0x7fffffffU);
1829     elfcpp::Swap<32, big_endian>::writeval(wv, val);
1830     return (utils::has_overflow<31>(x) ?
1831             This::STATUS_OVERFLOW : This::STATUS_OKAY);
1832   }
1833
1834   // R_ARM_MOVW_ABS_NC: (S + A) | T
1835   static inline typename This::Status 
1836   movw_abs_nc(unsigned char *view,
1837               const Sized_relobj<32, big_endian>* object,
1838               const Symbol_value<32>* psymval,
1839               bool has_thumb_bit)
1840   {
1841     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1842     Valtype* wv = reinterpret_cast<Valtype*>(view);
1843     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1844     Valtype addend =  This::extract_arm_movw_movt_addend(val);
1845     Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1846     val = This::insert_val_arm_movw_movt(val, x);
1847     elfcpp::Swap<32, big_endian>::writeval(wv, val);
1848     return This::STATUS_OKAY;
1849   }
1850
1851   // R_ARM_MOVT_ABS: S + A
1852   static inline typename This::Status
1853   movt_abs(unsigned char *view,
1854            const Sized_relobj<32, big_endian>* object,
1855            const Symbol_value<32>* psymval)
1856   {
1857     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1858     Valtype* wv = reinterpret_cast<Valtype*>(view);
1859     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1860     Valtype addend = This::extract_arm_movw_movt_addend(val);
1861     Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1862     val = This::insert_val_arm_movw_movt(val, x);
1863     elfcpp::Swap<32, big_endian>::writeval(wv, val);
1864     return This::STATUS_OKAY;
1865   }
1866
1867   //  R_ARM_THM_MOVW_ABS_NC: S + A | T
1868   static inline typename This::Status 
1869   thm_movw_abs_nc(unsigned char *view,
1870                   const Sized_relobj<32, big_endian>* object,
1871                   const Symbol_value<32>* psymval,
1872                   bool has_thumb_bit)
1873   {
1874     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1875     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1876     Valtype* wv = reinterpret_cast<Valtype*>(view);
1877     Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1878                    | elfcpp::Swap<16, big_endian>::readval(wv + 1));
1879     Reltype addend = extract_thumb_movw_movt_addend(val);
1880     Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
1881     val = This::insert_val_thumb_movw_movt(val, x);
1882     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1883     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1884     return This::STATUS_OKAY;
1885   }
1886
1887   //  R_ARM_THM_MOVT_ABS: S + A
1888   static inline typename This::Status 
1889   thm_movt_abs(unsigned char *view,
1890                const Sized_relobj<32, big_endian>* object,
1891                const Symbol_value<32>* psymval)
1892   {
1893     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1894     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1895     Valtype* wv = reinterpret_cast<Valtype*>(view);
1896     Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1897                    | elfcpp::Swap<16, big_endian>::readval(wv + 1));
1898     Reltype addend = This::extract_thumb_movw_movt_addend(val);
1899     Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
1900     val = This::insert_val_thumb_movw_movt(val, x);
1901     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1902     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1903     return This::STATUS_OKAY;
1904   }
1905
1906   // R_ARM_MOVW_PREL_NC: (S + A) | T - P
1907   static inline typename This::Status
1908   movw_prel_nc(unsigned char *view,
1909                const Sized_relobj<32, big_endian>* object,
1910                const Symbol_value<32>* psymval,
1911                Arm_address address,
1912                bool has_thumb_bit)
1913   {
1914     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1915     Valtype* wv = reinterpret_cast<Valtype*>(view);
1916     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1917     Valtype addend = This::extract_arm_movw_movt_addend(val);
1918     Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1919                  - address);
1920     val = This::insert_val_arm_movw_movt(val, x);
1921     elfcpp::Swap<32, big_endian>::writeval(wv, val);
1922     return This::STATUS_OKAY;
1923   }
1924
1925   // R_ARM_MOVT_PREL: S + A - P
1926   static inline typename This::Status
1927   movt_prel(unsigned char *view,
1928             const Sized_relobj<32, big_endian>* object,
1929             const Symbol_value<32>* psymval,
1930             Arm_address address)
1931   {
1932     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1933     Valtype* wv = reinterpret_cast<Valtype*>(view);
1934     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1935     Valtype addend = This::extract_arm_movw_movt_addend(val);
1936     Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
1937                  - address) >> 16;
1938     val = This::insert_val_arm_movw_movt(val, x);
1939     elfcpp::Swap<32, big_endian>::writeval(wv, val);
1940     return This::STATUS_OKAY;
1941   }
1942
1943   // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
1944   static inline typename This::Status
1945   thm_movw_prel_nc(unsigned char *view,
1946                    const Sized_relobj<32, big_endian>* object,
1947                    const Symbol_value<32>* psymval,
1948                    Arm_address address,
1949                    bool has_thumb_bit)
1950   {
1951     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1952     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1953     Valtype* wv = reinterpret_cast<Valtype*>(view);
1954     Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1955                   | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1956     Reltype addend = This::extract_thumb_movw_movt_addend(val);
1957     Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1958                  - address);
1959     val = This::insert_val_thumb_movw_movt(val, x);
1960     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1961     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1962     return This::STATUS_OKAY;
1963   }
1964
1965   // R_ARM_THM_MOVT_PREL: S + A - P
1966   static inline typename This::Status
1967   thm_movt_prel(unsigned char *view,
1968                 const Sized_relobj<32, big_endian>* object,
1969                 const Symbol_value<32>* psymval,
1970                 Arm_address address)
1971   {
1972     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1973     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1974     Valtype* wv = reinterpret_cast<Valtype*>(view);
1975     Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1976                   | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1977     Reltype addend = This::extract_thumb_movw_movt_addend(val);
1978     Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
1979                  - address) >> 16;
1980     val = This::insert_val_thumb_movw_movt(val, x);
1981     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1982     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1983     return This::STATUS_OKAY;
1984   }
1985 };
1986
1987 // Get the GOT section, creating it if necessary.
1988
1989 template<bool big_endian>
1990 Output_data_got<32, big_endian>*
1991 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
1992 {
1993   if (this->got_ == NULL)
1994     {
1995       gold_assert(symtab != NULL && layout != NULL);
1996
1997       this->got_ = new Output_data_got<32, big_endian>();
1998
1999       Output_section* os;
2000       os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2001                                            (elfcpp::SHF_ALLOC
2002                                             | elfcpp::SHF_WRITE),
2003                                            this->got_);
2004       os->set_is_relro();
2005
2006       // The old GNU linker creates a .got.plt section.  We just
2007       // create another set of data in the .got section.  Note that we
2008       // always create a PLT if we create a GOT, although the PLT
2009       // might be empty.
2010       this->got_plt_ = new Output_data_space(4, "** GOT PLT");
2011       os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2012                                            (elfcpp::SHF_ALLOC
2013                                             | elfcpp::SHF_WRITE),
2014                                            this->got_plt_);
2015       os->set_is_relro();
2016
2017       // The first three entries are reserved.
2018       this->got_plt_->set_current_data_size(3 * 4);
2019
2020       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2021       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2022                                     this->got_plt_,
2023                                     0, 0, elfcpp::STT_OBJECT,
2024                                     elfcpp::STB_LOCAL,
2025                                     elfcpp::STV_HIDDEN, 0,
2026                                     false, false);
2027     }
2028   return this->got_;
2029 }
2030
2031 // Get the dynamic reloc section, creating it if necessary.
2032
2033 template<bool big_endian>
2034 typename Target_arm<big_endian>::Reloc_section*
2035 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
2036 {
2037   if (this->rel_dyn_ == NULL)
2038     {
2039       gold_assert(layout != NULL);
2040       this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
2041       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
2042                                       elfcpp::SHF_ALLOC, this->rel_dyn_);
2043     }
2044   return this->rel_dyn_;
2045 }
2046
2047 // Insn_template methods.
2048
2049 // Return byte size of an instruction template.
2050
2051 size_t
2052 Insn_template::size() const
2053 {
2054   switch (this->type())
2055     {
2056     case THUMB16_TYPE:
2057       return 2;
2058     case ARM_TYPE:
2059     case THUMB32_TYPE:
2060     case DATA_TYPE:
2061       return 4;
2062     default:
2063       gold_unreachable();
2064     }
2065 }
2066
2067 // Return alignment of an instruction template.
2068
2069 unsigned
2070 Insn_template::alignment() const
2071 {
2072   switch (this->type())
2073     {
2074     case THUMB16_TYPE:
2075     case THUMB32_TYPE:
2076       return 2;
2077     case ARM_TYPE:
2078     case DATA_TYPE:
2079       return 4;
2080     default:
2081       gold_unreachable();
2082     }
2083 }
2084
2085 // Stub_template methods.
2086
2087 Stub_template::Stub_template(
2088     Stub_type type, const Insn_template* insns,
2089      size_t insn_count)
2090   : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
2091     entry_in_thumb_mode_(false), relocs_()
2092 {
2093   off_t offset = 0;
2094
2095   // Compute byte size and alignment of stub template.
2096   for (size_t i = 0; i < insn_count; i++)
2097     {
2098       unsigned insn_alignment = insns[i].alignment();
2099       size_t insn_size = insns[i].size();
2100       gold_assert((offset & (insn_alignment - 1)) == 0);
2101       this->alignment_ = std::max(this->alignment_, insn_alignment);
2102       switch (insns[i].type())
2103         {
2104         case Insn_template::THUMB16_TYPE:
2105           if (i == 0)
2106             this->entry_in_thumb_mode_ = true;
2107           break;
2108
2109         case Insn_template::THUMB32_TYPE:
2110           if (insns[i].r_type() != elfcpp::R_ARM_NONE)
2111             this->relocs_.push_back(Reloc(i, offset));
2112           if (i == 0)
2113             this->entry_in_thumb_mode_ = true;
2114           break;
2115
2116         case Insn_template::ARM_TYPE:
2117           // Handle cases where the target is encoded within the
2118           // instruction.
2119           if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
2120             this->relocs_.push_back(Reloc(i, offset));
2121           break;
2122
2123         case Insn_template::DATA_TYPE:
2124           // Entry point cannot be data.
2125           gold_assert(i != 0);
2126           this->relocs_.push_back(Reloc(i, offset));
2127           break;
2128
2129         default:
2130           gold_unreachable();
2131         }
2132       offset += insn_size; 
2133     }
2134   this->size_ = offset;
2135 }
2136
2137 // Reloc_stub::Key methods.
2138
2139 // Dump a Key as a string for debugging.
2140
2141 std::string
2142 Reloc_stub::Key::name() const
2143 {
2144   if (this->r_sym_ == invalid_index)
2145     {
2146       // Global symbol key name
2147       // <stub-type>:<symbol name>:<addend>.
2148       const std::string sym_name = this->u_.symbol->name();
2149       // We need to print two hex number and two colons.  So just add 100 bytes
2150       // to the symbol name size.
2151       size_t len = sym_name.size() + 100;
2152       char* buffer = new char[len];
2153       int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
2154                        sym_name.c_str(), this->addend_);
2155       gold_assert(c > 0 && c < static_cast<int>(len));
2156       delete[] buffer;
2157       return std::string(buffer);
2158     }
2159   else
2160     {
2161       // local symbol key name
2162       // <stub-type>:<object>:<r_sym>:<addend>.
2163       const size_t len = 200;
2164       char buffer[len];
2165       int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
2166                        this->u_.relobj, this->r_sym_, this->addend_);
2167       gold_assert(c > 0 && c < static_cast<int>(len));
2168       return std::string(buffer);
2169     }
2170 }
2171
2172 // Reloc_stub methods.
2173
2174 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
2175 // LOCATION to DESTINATION.
2176 // This code is based on the arm_type_of_stub function in
2177 // bfd/elf32-arm.c.  We have changed the interface a liitle to keep the Stub
2178 // class simple.
2179
2180 Stub_type
2181 Reloc_stub::stub_type_for_reloc(
2182    unsigned int r_type,
2183    Arm_address location,
2184    Arm_address destination,
2185    bool target_is_thumb)
2186 {
2187   Stub_type stub_type = arm_stub_none;
2188
2189   // This is a bit ugly but we want to avoid using a templated class for
2190   // big and little endianities.
2191   bool may_use_blx;
2192   bool should_force_pic_veneer;
2193   bool thumb2;
2194   bool thumb_only;
2195   if (parameters->target().is_big_endian())
2196     {
2197       const Target_arm<true>& big_endian_target =
2198         Target_arm<true>::default_target();
2199       may_use_blx = big_endian_target.may_use_blx();
2200       should_force_pic_veneer = big_endian_target.should_force_pic_veneer();
2201       thumb2 = big_endian_target.using_thumb2();
2202       thumb_only = big_endian_target.using_thumb_only();
2203     }
2204   else
2205     {
2206       const Target_arm<false>& little_endian_target =
2207         Target_arm<false>::default_target();
2208       may_use_blx = little_endian_target.may_use_blx();
2209       should_force_pic_veneer = little_endian_target.should_force_pic_veneer();
2210       thumb2 = little_endian_target.using_thumb2();
2211       thumb_only = little_endian_target.using_thumb_only();
2212     }
2213
2214   int64_t branch_offset = (int64_t)destination - location;
2215
2216   if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
2217     {
2218       // Handle cases where:
2219       // - this call goes too far (different Thumb/Thumb2 max
2220       //   distance)
2221       // - it's a Thumb->Arm call and blx is not available, or it's a
2222       //   Thumb->Arm branch (not bl). A stub is needed in this case.
2223       if ((!thumb2
2224             && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
2225                 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
2226           || (thumb2
2227               && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
2228                   || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
2229           || ((!target_is_thumb)
2230               && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
2231                   || (r_type == elfcpp::R_ARM_THM_JUMP24))))
2232         {
2233           if (target_is_thumb)
2234             {
2235               // Thumb to thumb.
2236               if (!thumb_only)
2237                 {
2238                   stub_type = (parameters->options().shared() | should_force_pic_veneer)
2239                     // PIC stubs.
2240                     ? ((may_use_blx
2241                         && (r_type == elfcpp::R_ARM_THM_CALL))
2242                        // V5T and above. Stub starts with ARM code, so
2243                        // we must be able to switch mode before
2244                        // reaching it, which is only possible for 'bl'
2245                        // (ie R_ARM_THM_CALL relocation).
2246                        ? arm_stub_long_branch_any_thumb_pic
2247                        // On V4T, use Thumb code only.
2248                        : arm_stub_long_branch_v4t_thumb_thumb_pic)
2249
2250                     // non-PIC stubs.
2251                     : ((may_use_blx
2252                         && (r_type == elfcpp::R_ARM_THM_CALL))
2253                        ? arm_stub_long_branch_any_any // V5T and above.
2254                        : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
2255                 }
2256               else
2257                 {
2258                   stub_type = (parameters->options().shared() | should_force_pic_veneer)
2259                     ? arm_stub_long_branch_thumb_only_pic       // PIC stub.
2260                     : arm_stub_long_branch_thumb_only;  // non-PIC stub.
2261                 }
2262             }
2263           else
2264             {
2265               // Thumb to arm.
2266              
2267               // FIXME: We should check that the input section is from an
2268               // object that has interwork enabled.
2269
2270               stub_type = (parameters->options().shared()
2271                            || should_force_pic_veneer)
2272                 // PIC stubs.
2273                 ? ((may_use_blx
2274                     && (r_type == elfcpp::R_ARM_THM_CALL))
2275                    ? arm_stub_long_branch_any_arm_pic   // V5T and above.
2276                    : arm_stub_long_branch_v4t_thumb_arm_pic)    // V4T.
2277
2278                 // non-PIC stubs.
2279                 : ((may_use_blx
2280                     && (r_type == elfcpp::R_ARM_THM_CALL))
2281                    ? arm_stub_long_branch_any_any       // V5T and above.
2282                    : arm_stub_long_branch_v4t_thumb_arm);       // V4T.
2283
2284               // Handle v4t short branches.
2285               if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
2286                   && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
2287                   && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
2288                 stub_type = arm_stub_short_branch_v4t_thumb_arm;
2289             }
2290         }
2291     }
2292   else if (r_type == elfcpp::R_ARM_CALL
2293            || r_type == elfcpp::R_ARM_JUMP24
2294            || r_type == elfcpp::R_ARM_PLT32)
2295     {
2296       if (target_is_thumb)
2297         {
2298           // Arm to thumb.
2299
2300           // FIXME: We should check that the input section is from an
2301           // object that has interwork enabled.
2302
2303           // We have an extra 2-bytes reach because of
2304           // the mode change (bit 24 (H) of BLX encoding).
2305           if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
2306               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
2307               || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
2308               || (r_type == elfcpp::R_ARM_JUMP24)
2309               || (r_type == elfcpp::R_ARM_PLT32))
2310             {
2311               stub_type = (parameters->options().shared()
2312                            || should_force_pic_veneer)
2313                 // PIC stubs.
2314                 ? (may_use_blx
2315                    ? arm_stub_long_branch_any_thumb_pic// V5T and above.
2316                    : arm_stub_long_branch_v4t_arm_thumb_pic)    // V4T stub.
2317
2318                 // non-PIC stubs.
2319                 : (may_use_blx
2320                    ? arm_stub_long_branch_any_any       // V5T and above.
2321                    : arm_stub_long_branch_v4t_arm_thumb);       // V4T.
2322             }
2323         }
2324       else
2325         {
2326           // Arm to arm.
2327           if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
2328               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
2329             {
2330               stub_type = (parameters->options().shared()
2331                            || should_force_pic_veneer)
2332                 ? arm_stub_long_branch_any_arm_pic      // PIC stubs.
2333                 : arm_stub_long_branch_any_any;         /// non-PIC.
2334             }
2335         }
2336     }
2337
2338   return stub_type;
2339 }
2340
2341 // Template to implement do_write for a specific target endianity.
2342
2343 template<bool big_endian>
2344 void inline
2345 Reloc_stub::do_fixed_endian_write(unsigned char* view,
2346                                   section_size_type view_size)
2347 {
2348   const Stub_template* stub_template = this->stub_template();
2349   const Insn_template* insns = stub_template->insns();
2350
2351   // FIXME:  We do not handle BE8 encoding yet.
2352   unsigned char* pov = view;
2353   for (size_t i = 0; i < stub_template->insn_count(); i++)
2354     {
2355       switch (insns[i].type())
2356         {
2357         case Insn_template::THUMB16_TYPE:
2358           // Non-zero reloc addends are only used in Cortex-A8 stubs. 
2359           gold_assert(insns[i].reloc_addend() == 0);
2360           elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
2361           break;
2362         case Insn_template::THUMB32_TYPE:
2363           {
2364             uint32_t hi = (insns[i].data() >> 16) & 0xffff;
2365             uint32_t lo = insns[i].data() & 0xffff;
2366             elfcpp::Swap<16, big_endian>::writeval(pov, hi);
2367             elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
2368           }
2369           break;
2370         case Insn_template::ARM_TYPE:
2371         case Insn_template::DATA_TYPE:
2372           elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
2373           break;
2374         default:
2375           gold_unreachable();
2376         }
2377       pov += insns[i].size();
2378     }
2379   gold_assert(static_cast<section_size_type>(pov - view) == view_size);
2380
2381
2382 // Write a reloc stub to VIEW with endianity specified by BIG_ENDIAN.
2383
2384 void
2385 Reloc_stub::do_write(unsigned char* view, section_size_type view_size,
2386                      bool big_endian)
2387 {
2388   if (big_endian)
2389     this->do_fixed_endian_write<true>(view, view_size);
2390   else
2391     this->do_fixed_endian_write<false>(view, view_size);
2392 }
2393
2394 // Stub_factory methods.
2395
2396 Stub_factory::Stub_factory()
2397 {
2398   // The instruction template sequences are declared as static
2399   // objects and initialized first time the constructor runs.
2400  
2401   // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
2402   // to reach the stub if necessary.
2403   static const Insn_template elf32_arm_stub_long_branch_any_any[] =
2404     {
2405       Insn_template::arm_insn(0xe51ff004),      // ldr   pc, [pc, #-4]
2406       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2407                                                 // dcd   R_ARM_ABS32(X)
2408     };
2409   
2410   // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
2411   // available.
2412   static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
2413     {
2414       Insn_template::arm_insn(0xe59fc000),      // ldr   ip, [pc, #0]
2415       Insn_template::arm_insn(0xe12fff1c),      // bx    ip
2416       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2417                                                 // dcd   R_ARM_ABS32(X)
2418     };
2419   
2420   // Thumb -> Thumb long branch stub. Used on M-profile architectures.
2421   static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
2422     {
2423       Insn_template::thumb16_insn(0xb401),      // push {r0}
2424       Insn_template::thumb16_insn(0x4802),      // ldr  r0, [pc, #8]
2425       Insn_template::thumb16_insn(0x4684),      // mov  ip, r0
2426       Insn_template::thumb16_insn(0xbc01),      // pop  {r0}
2427       Insn_template::thumb16_insn(0x4760),      // bx   ip
2428       Insn_template::thumb16_insn(0xbf00),      // nop
2429       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2430                                                 // dcd  R_ARM_ABS32(X)
2431     };
2432   
2433   // V4T Thumb -> Thumb long branch stub. Using the stack is not
2434   // allowed.
2435   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
2436     {
2437       Insn_template::thumb16_insn(0x4778),      // bx   pc
2438       Insn_template::thumb16_insn(0x46c0),      // nop
2439       Insn_template::arm_insn(0xe59fc000),      // ldr  ip, [pc, #0]
2440       Insn_template::arm_insn(0xe12fff1c),      // bx   ip
2441       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2442                                                 // dcd  R_ARM_ABS32(X)
2443     };
2444   
2445   // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
2446   // available.
2447   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
2448     {
2449       Insn_template::thumb16_insn(0x4778),      // bx   pc
2450       Insn_template::thumb16_insn(0x46c0),      // nop
2451       Insn_template::arm_insn(0xe51ff004),      // ldr   pc, [pc, #-4]
2452       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
2453                                                 // dcd   R_ARM_ABS32(X)
2454     };
2455   
2456   // V4T Thumb -> ARM short branch stub. Shorter variant of the above
2457   // one, when the destination is close enough.
2458   static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
2459     {
2460       Insn_template::thumb16_insn(0x4778),              // bx   pc
2461       Insn_template::thumb16_insn(0x46c0),              // nop
2462       Insn_template::arm_rel_insn(0xea000000, -8),      // b    (X-8)
2463     };
2464   
2465   // ARM/Thumb -> ARM long branch stub, PIC.  On V5T and above, use
2466   // blx to reach the stub if necessary.
2467   static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
2468     {
2469       Insn_template::arm_insn(0xe59fc000),      // ldr   r12, [pc]
2470       Insn_template::arm_insn(0xe08ff00c),      // add   pc, pc, ip
2471       Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2472                                                 // dcd   R_ARM_REL32(X-4)
2473     };
2474   
2475   // ARM/Thumb -> Thumb long branch stub, PIC.  On V5T and above, use
2476   // blx to reach the stub if necessary.  We can not add into pc;
2477   // it is not guaranteed to mode switch (different in ARMv6 and
2478   // ARMv7).
2479   static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
2480     {
2481       Insn_template::arm_insn(0xe59fc004),      // ldr   r12, [pc, #4]
2482       Insn_template::arm_insn(0xe08fc00c),      // add   ip, pc, ip
2483       Insn_template::arm_insn(0xe12fff1c),      // bx    ip
2484       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2485                                                 // dcd   R_ARM_REL32(X)
2486     };
2487   
2488   // V4T ARM -> ARM long branch stub, PIC.
2489   static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
2490     {
2491       Insn_template::arm_insn(0xe59fc004),      // ldr   ip, [pc, #4]
2492       Insn_template::arm_insn(0xe08fc00c),      // add   ip, pc, ip
2493       Insn_template::arm_insn(0xe12fff1c),      // bx    ip
2494       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2495                                                 // dcd   R_ARM_REL32(X)
2496     };
2497   
2498   // V4T Thumb -> ARM long branch stub, PIC.
2499   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
2500     {
2501       Insn_template::thumb16_insn(0x4778),      // bx   pc
2502       Insn_template::thumb16_insn(0x46c0),      // nop
2503       Insn_template::arm_insn(0xe59fc000),      // ldr  ip, [pc, #0]
2504       Insn_template::arm_insn(0xe08cf00f),      // add  pc, ip, pc
2505       Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
2506                                                 // dcd  R_ARM_REL32(X)
2507     };
2508   
2509   // Thumb -> Thumb long branch stub, PIC. Used on M-profile
2510   // architectures.
2511   static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
2512     {
2513       Insn_template::thumb16_insn(0xb401),      // push {r0}
2514       Insn_template::thumb16_insn(0x4802),      // ldr  r0, [pc, #8]
2515       Insn_template::thumb16_insn(0x46fc),      // mov  ip, pc
2516       Insn_template::thumb16_insn(0x4484),      // add  ip, r0
2517       Insn_template::thumb16_insn(0xbc01),      // pop  {r0}
2518       Insn_template::thumb16_insn(0x4760),      // bx   ip
2519       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
2520                                                 // dcd  R_ARM_REL32(X)
2521     };
2522   
2523   // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
2524   // allowed.
2525   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
2526     {
2527       Insn_template::thumb16_insn(0x4778),      // bx   pc
2528       Insn_template::thumb16_insn(0x46c0),      // nop
2529       Insn_template::arm_insn(0xe59fc004),      // ldr  ip, [pc, #4]
2530       Insn_template::arm_insn(0xe08fc00c),      // add   ip, pc, ip
2531       Insn_template::arm_insn(0xe12fff1c),      // bx   ip
2532       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
2533                                                 // dcd  R_ARM_REL32(X)
2534     };
2535   
2536   // Cortex-A8 erratum-workaround stubs.
2537   
2538   // Stub used for conditional branches (which may be beyond +/-1MB away,
2539   // so we can't use a conditional branch to reach this stub).
2540   
2541   // original code:
2542   //
2543   //    b<cond> X
2544   // after:
2545   //
2546   static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
2547     {
2548       Insn_template::thumb16_bcond_insn(0xd001),        //      b<cond>.n true
2549       Insn_template::thumb32_b_insn(0xf000b800, -4),    //      b.w after
2550       Insn_template::thumb32_b_insn(0xf000b800, -4)     // true:
2551                                                         //      b.w X
2552     };
2553   
2554   // Stub used for b.w and bl.w instructions.
2555   
2556   static const Insn_template elf32_arm_stub_a8_veneer_b[] =
2557     {
2558       Insn_template::thumb32_b_insn(0xf000b800, -4)     // b.w dest
2559     };
2560   
2561   static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
2562     {
2563       Insn_template::thumb32_b_insn(0xf000b800, -4)     // b.w dest
2564     };
2565   
2566   // Stub used for Thumb-2 blx.w instructions.  We modified the original blx.w
2567   // instruction (which switches to ARM mode) to point to this stub.  Jump to
2568   // the real destination using an ARM-mode branch.
2569   const Insn_template elf32_arm_stub_a8_veneer_blx[] =
2570     {
2571       Insn_template::arm_rel_insn(0xea000000, -8)       // b dest
2572     };
2573
2574   // Fill in the stub template look-up table.  Stub templates are constructed
2575   // per instance of Stub_factory for fast look-up without locking
2576   // in a thread-enabled environment.
2577
2578   this->stub_templates_[arm_stub_none] =
2579     new Stub_template(arm_stub_none, NULL, 0);
2580
2581 #define DEF_STUB(x)     \
2582   do \
2583     { \
2584       size_t array_size \
2585         = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
2586       Stub_type type = arm_stub_##x; \
2587       this->stub_templates_[type] = \
2588         new Stub_template(type, elf32_arm_stub_##x, array_size); \
2589     } \
2590   while (0);
2591
2592   DEF_STUBS
2593 #undef DEF_STUB
2594 }
2595
2596 // Stub_table methods.
2597
2598 // Add a STUB with using KEY.  Caller is reponsible for avoid adding
2599 // if already a STUB with the same key has been added. 
2600
2601 template<bool big_endian>
2602 void
2603 Stub_table<big_endian>::add_reloc_stub(
2604     Reloc_stub* stub,
2605     const Reloc_stub::Key& key)
2606 {
2607   const Stub_template* stub_template = stub->stub_template();
2608   gold_assert(stub_template->type() == key.stub_type());
2609   this->reloc_stubs_[key] = stub;
2610   if (this->addralign_ < stub_template->alignment())
2611     this->addralign_ = stub_template->alignment();
2612   this->has_been_changed_ = true;
2613 }
2614
2615 template<bool big_endian>
2616 void
2617 Stub_table<big_endian>::relocate_stubs(
2618     const Relocate_info<32, big_endian>* relinfo,
2619     Target_arm<big_endian>* arm_target,
2620     Output_section* output_section,
2621     unsigned char* view,
2622     Arm_address address,
2623     section_size_type view_size)
2624 {
2625   // If we are passed a view bigger than the stub table's.  we need to
2626   // adjust the view.
2627   gold_assert(address == this->address()
2628               && (view_size
2629                   == static_cast<section_size_type>(this->data_size())));
2630
2631   for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2632       p != this->reloc_stubs_.end();
2633       ++p)
2634     {
2635       Reloc_stub* stub = p->second;
2636       const Stub_template* stub_template = stub->stub_template();
2637       if (stub_template->reloc_count() != 0)
2638         {
2639           // Adjust view to cover the stub only.
2640           section_size_type offset = stub->offset();
2641           section_size_type stub_size = stub_template->size();
2642           gold_assert(offset + stub_size <= view_size);
2643
2644           arm_target->relocate_stub(stub, relinfo, output_section,
2645                                     view + offset, address + offset,
2646                                     stub_size);
2647         }
2648     }
2649 }
2650
2651 // Reset address and file offset.
2652
2653 template<bool big_endian>
2654 void
2655 Stub_table<big_endian>::do_reset_address_and_file_offset()
2656 {
2657   off_t off = 0;
2658   uint64_t max_addralign = 1;
2659   for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2660       p != this->reloc_stubs_.end();
2661       ++p)
2662     {
2663       Reloc_stub* stub = p->second;
2664       const Stub_template* stub_template = stub->stub_template();
2665       uint64_t stub_addralign = stub_template->alignment();
2666       max_addralign = std::max(max_addralign, stub_addralign);
2667       off = align_address(off, stub_addralign);
2668       stub->set_offset(off);
2669       stub->reset_destination_address();
2670       off += stub_template->size();
2671     }
2672
2673   this->addralign_ = max_addralign;
2674   this->set_current_data_size_for_child(off);
2675 }
2676
2677 // Write out the stubs to file.
2678
2679 template<bool big_endian>
2680 void
2681 Stub_table<big_endian>::do_write(Output_file* of)
2682 {
2683   off_t offset = this->offset();
2684   const section_size_type oview_size =
2685     convert_to_section_size_type(this->data_size());
2686   unsigned char* const oview = of->get_output_view(offset, oview_size);
2687
2688   for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
2689       p != this->reloc_stubs_.end();
2690       ++p)
2691     {
2692       Reloc_stub* stub = p->second;
2693       Arm_address address = this->address() + stub->offset();
2694       gold_assert(address
2695                   == align_address(address,
2696                                    stub->stub_template()->alignment()));
2697       stub->write(oview + stub->offset(), stub->stub_template()->size(),
2698                   big_endian);
2699     } 
2700   of->write_output_view(this->offset(), oview_size, oview);
2701 }
2702
2703 // Arm_input_section methods.
2704
2705 // Initialize an Arm_input_section.
2706
2707 template<bool big_endian>
2708 void
2709 Arm_input_section<big_endian>::init()
2710 {
2711   Relobj* relobj = this->relobj();
2712   unsigned int shndx = this->shndx();
2713
2714   // Cache these to speed up size and alignment queries.  It is too slow
2715   // to call section_addraglin and section_size every time.
2716   this->original_addralign_ = relobj->section_addralign(shndx);
2717   this->original_size_ = relobj->section_size(shndx);
2718
2719   // We want to make this look like the original input section after
2720   // output sections are finalized.
2721   Output_section* os = relobj->output_section(shndx);
2722   off_t offset = relobj->output_section_offset(shndx);
2723   gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2724   this->set_address(os->address() + offset);
2725   this->set_file_offset(os->offset() + offset);
2726
2727   this->set_current_data_size(this->original_size_);
2728   this->finalize_data_size();
2729 }
2730
2731 template<bool big_endian>
2732 void
2733 Arm_input_section<big_endian>::do_write(Output_file* of)
2734 {
2735   // We have to write out the original section content.
2736   section_size_type section_size;
2737   const unsigned char* section_contents =
2738     this->relobj()->section_contents(this->shndx(), &section_size, false); 
2739   of->write(this->offset(), section_contents, section_size); 
2740
2741   // If this owns a stub table and it is not empty, write it.
2742   if (this->is_stub_table_owner() && !this->stub_table_->empty())
2743     this->stub_table_->write(of);
2744 }
2745
2746 // Finalize data size.
2747
2748 template<bool big_endian>
2749 void
2750 Arm_input_section<big_endian>::set_final_data_size()
2751 {
2752   // If this owns a stub table, finalize its data size as well.
2753   if (this->is_stub_table_owner())
2754     {
2755       uint64_t address = this->address();
2756
2757       // The stub table comes after the original section contents.
2758       address += this->original_size_;
2759       address = align_address(address, this->stub_table_->addralign());
2760       off_t offset = this->offset() + (address - this->address());
2761       this->stub_table_->set_address_and_file_offset(address, offset);
2762       address += this->stub_table_->data_size();
2763       gold_assert(address == this->address() + this->current_data_size());
2764     }
2765
2766   this->set_data_size(this->current_data_size());
2767 }
2768
2769 // Reset address and file offset.
2770
2771 template<bool big_endian>
2772 void
2773 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
2774 {
2775   // Size of the original input section contents.
2776   off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2777
2778   // If this is a stub table owner, account for the stub table size.
2779   if (this->is_stub_table_owner())
2780     {
2781       Stub_table<big_endian>* stub_table = this->stub_table_;
2782
2783       // Reset the stub table's address and file offset.  The
2784       // current data size for child will be updated after that.
2785       stub_table_->reset_address_and_file_offset();
2786       off = align_address(off, stub_table_->addralign());
2787       off += stub_table->current_data_size();
2788     }
2789
2790   this->set_current_data_size(off);
2791 }
2792
2793 // Arm_output_section methods.
2794
2795 // Create a stub group for input sections from BEGIN to END.  OWNER
2796 // points to the input section to be the owner a new stub table.
2797
2798 template<bool big_endian>
2799 void
2800 Arm_output_section<big_endian>::create_stub_group(
2801   Input_section_list::const_iterator begin,
2802   Input_section_list::const_iterator end,
2803   Input_section_list::const_iterator owner,
2804   Target_arm<big_endian>* target,
2805   std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
2806 {
2807   // Currently we convert ordinary input sections into relaxed sections only
2808   // at this point but we may want to support creating relaxed input section
2809   // very early.  So we check here to see if owner is already a relaxed
2810   // section.
2811   
2812   Arm_input_section<big_endian>* arm_input_section;
2813   if (owner->is_relaxed_input_section())
2814     {
2815       arm_input_section =
2816         Arm_input_section<big_endian>::as_arm_input_section(
2817           owner->relaxed_input_section());
2818     }
2819   else
2820     {
2821       gold_assert(owner->is_input_section());
2822       // Create a new relaxed input section.
2823       arm_input_section =
2824         target->new_arm_input_section(owner->relobj(), owner->shndx());
2825       new_relaxed_sections->push_back(arm_input_section);
2826     }
2827
2828   // Create a stub table.
2829   Stub_table<big_endian>* stub_table =
2830     target->new_stub_table(arm_input_section);
2831
2832   arm_input_section->set_stub_table(stub_table);
2833   
2834   Input_section_list::const_iterator p = begin;
2835   Input_section_list::const_iterator prev_p;
2836
2837   // Look for input sections or relaxed input sections in [begin ... end].
2838   do
2839     {
2840       if (p->is_input_section() || p->is_relaxed_input_section())
2841         {
2842           // The stub table information for input sections live
2843           // in their objects.
2844           Arm_relobj<big_endian>* arm_relobj =
2845             Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
2846           arm_relobj->set_stub_table(p->shndx(), stub_table);
2847         }
2848       prev_p = p++;
2849     }
2850   while (prev_p != end);
2851 }
2852
2853 // Group input sections for stub generation.  GROUP_SIZE is roughly the limit
2854 // of stub groups.  We grow a stub group by adding input section until the
2855 // size is just below GROUP_SIZE.  The last input section will be converted
2856 // into a stub table.  If STUB_ALWAYS_AFTER_BRANCH is false, we also add
2857 // input section after the stub table, effectively double the group size.
2858 // 
2859 // This is similar to the group_sections() function in elf32-arm.c but is
2860 // implemented differently.
2861
2862 template<bool big_endian>
2863 void
2864 Arm_output_section<big_endian>::group_sections(
2865     section_size_type group_size,
2866     bool stubs_always_after_branch,
2867     Target_arm<big_endian>* target)
2868 {
2869   // We only care about sections containing code.
2870   if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
2871     return;
2872
2873   // States for grouping.
2874   typedef enum
2875   {
2876     // No group is being built.
2877     NO_GROUP,
2878     // A group is being built but the stub table is not found yet.
2879     // We keep group a stub group until the size is just under GROUP_SIZE.
2880     // The last input section in the group will be used as the stub table.
2881     FINDING_STUB_SECTION,
2882     // A group is being built and we have already found a stub table.
2883     // We enter this state to grow a stub group by adding input section
2884     // after the stub table.  This effectively doubles the group size.
2885     HAS_STUB_SECTION
2886   } State;
2887
2888   // Any newly created relaxed sections are stored here.
2889   std::vector<Output_relaxed_input_section*> new_relaxed_sections;
2890
2891   State state = NO_GROUP;
2892   section_size_type off = 0;
2893   section_size_type group_begin_offset = 0;
2894   section_size_type group_end_offset = 0;
2895   section_size_type stub_table_end_offset = 0;
2896   Input_section_list::const_iterator group_begin =
2897     this->input_sections().end();
2898   Input_section_list::const_iterator stub_table =
2899     this->input_sections().end();
2900   Input_section_list::const_iterator group_end = this->input_sections().end();
2901   for (Input_section_list::const_iterator p = this->input_sections().begin();
2902        p != this->input_sections().end();
2903        ++p)
2904     {
2905       section_size_type section_begin_offset =
2906         align_address(off, p->addralign());
2907       section_size_type section_end_offset =
2908         section_begin_offset + p->data_size(); 
2909       
2910       // Check to see if we should group the previously seens sections.
2911       switch(state)
2912         {
2913         case NO_GROUP:
2914           break;
2915
2916         case FINDING_STUB_SECTION:
2917           // Adding this section makes the group larger than GROUP_SIZE.
2918           if (section_end_offset - group_begin_offset >= group_size)
2919             {
2920               if (stubs_always_after_branch)
2921                 {       
2922                   gold_assert(group_end != this->input_sections().end());
2923                   this->create_stub_group(group_begin, group_end, group_end,
2924                                           target, &new_relaxed_sections);
2925                   state = NO_GROUP;
2926                 }
2927               else
2928                 {
2929                   // But wait, there's more!  Input sections up to
2930                   // stub_group_size bytes after the stub table can be
2931                   // handled by it too.
2932                   state = HAS_STUB_SECTION;
2933                   stub_table = group_end;
2934                   stub_table_end_offset = group_end_offset;
2935                 }
2936             }
2937             break;
2938
2939         case HAS_STUB_SECTION:
2940           // Adding this section makes the post stub-section group larger
2941           // than GROUP_SIZE.
2942           if (section_end_offset - stub_table_end_offset >= group_size)
2943            {
2944              gold_assert(group_end != this->input_sections().end());
2945              this->create_stub_group(group_begin, group_end, stub_table,
2946                                      target, &new_relaxed_sections);
2947              state = NO_GROUP;
2948            }
2949            break;
2950
2951           default:
2952             gold_unreachable();
2953         }       
2954
2955       // If we see an input section and currently there is no group, start
2956       // a new one.  Skip any empty sections.
2957       if ((p->is_input_section() || p->is_relaxed_input_section())
2958           && (p->relobj()->section_size(p->shndx()) != 0))
2959         {
2960           if (state == NO_GROUP)
2961             {
2962               state = FINDING_STUB_SECTION;
2963               group_begin = p;
2964               group_begin_offset = section_begin_offset;
2965             }
2966
2967           // Keep track of the last input section seen.
2968           group_end = p;
2969           group_end_offset = section_end_offset;
2970         }
2971
2972       off = section_end_offset;
2973     }
2974
2975   // Create a stub group for any ungrouped sections.
2976   if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
2977     {
2978       gold_assert(group_end != this->input_sections().end());
2979       this->create_stub_group(group_begin, group_end,
2980                               (state == FINDING_STUB_SECTION
2981                                ? group_end
2982                                : stub_table),
2983                                target, &new_relaxed_sections);
2984     }
2985
2986   // Convert input section into relaxed input section in a batch.
2987   if (!new_relaxed_sections.empty())
2988     this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
2989
2990   // Update the section offsets
2991   for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
2992     {
2993       Arm_relobj<big_endian>* arm_relobj =
2994         Arm_relobj<big_endian>::as_arm_relobj(
2995           new_relaxed_sections[i]->relobj());
2996       unsigned int shndx = new_relaxed_sections[i]->shndx();
2997       // Tell Arm_relobj that this input section is converted.
2998       arm_relobj->convert_input_section_to_relaxed_section(shndx);
2999     }
3000 }
3001
3002 // Arm_relobj methods.
3003
3004 // Scan relocations for stub generation.
3005
3006 template<bool big_endian>
3007 void
3008 Arm_relobj<big_endian>::scan_sections_for_stubs(
3009     Target_arm<big_endian>* arm_target,
3010     const Symbol_table* symtab,
3011     const Layout* layout)
3012 {
3013   unsigned int shnum = this->shnum();
3014   const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
3015
3016   // Read the section headers.
3017   const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
3018                                                shnum * shdr_size,
3019                                                true, true);
3020
3021   // To speed up processing, we set up hash tables for fast lookup of
3022   // input offsets to output addresses.
3023   this->initialize_input_to_output_maps();
3024
3025   const Relobj::Output_sections& out_sections(this->output_sections());
3026
3027   Relocate_info<32, big_endian> relinfo;
3028   relinfo.symtab = symtab;
3029   relinfo.layout = layout;
3030   relinfo.object = this;
3031
3032   const unsigned char* p = pshdrs + shdr_size;
3033   for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
3034     {
3035       typename elfcpp::Shdr<32, big_endian> shdr(p);
3036
3037       unsigned int sh_type = shdr.get_sh_type();
3038       if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
3039         continue;
3040
3041       off_t sh_size = shdr.get_sh_size();
3042       if (sh_size == 0)
3043         continue;
3044
3045       unsigned int index = this->adjust_shndx(shdr.get_sh_info());
3046       if (index >= this->shnum())
3047         {
3048           // Ignore reloc section with bad info.  This error will be
3049           // reported in the final link.
3050           continue;
3051         }
3052
3053       Output_section* os = out_sections[index];
3054       if (os == NULL)
3055         {
3056           // This relocation section is against a section which we
3057           // discarded.
3058           continue;
3059         }
3060       Arm_address output_offset = this->get_output_section_offset(index);
3061
3062       if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
3063         {
3064           // Ignore reloc section with unexpected symbol table.  The
3065           // error will be reported in the final link.
3066           continue;
3067         }
3068
3069       const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
3070                                                     sh_size, true, false);
3071
3072       unsigned int reloc_size;
3073       if (sh_type == elfcpp::SHT_REL)
3074         reloc_size = elfcpp::Elf_sizes<32>::rel_size;
3075       else
3076         reloc_size = elfcpp::Elf_sizes<32>::rela_size;
3077
3078       if (reloc_size != shdr.get_sh_entsize())
3079         {
3080           // Ignore reloc section with unexpected entsize.  The error
3081           // will be reported in the final link.
3082           continue;
3083         }
3084
3085       size_t reloc_count = sh_size / reloc_size;
3086       if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
3087         {
3088           // Ignore reloc section with uneven size.  The error will be
3089           // reported in the final link.
3090           continue;
3091         }
3092
3093       gold_assert(output_offset != invalid_address
3094                   || this->relocs_must_follow_section_writes());
3095
3096       // Get the section contents.  This does work for the case in which
3097       // we modify the contents of an input section.  We need to pass the
3098       // output view under such circumstances.
3099       section_size_type input_view_size = 0;
3100       const unsigned char* input_view =
3101         this->section_contents(index, &input_view_size, false);
3102
3103       relinfo.reloc_shndx = i;
3104       relinfo.data_shndx = index;
3105       arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
3106                                          reloc_count, os,
3107                                          output_offset == invalid_address,
3108                                          input_view,
3109                                          os->address(),
3110                                          input_view_size);
3111     }
3112
3113   // After we've done the relocations, we release the hash tables,
3114   // since we no longer need them.
3115   this->free_input_to_output_maps();
3116 }
3117
3118 // Count the local symbols.  The ARM backend needs to know if a symbol
3119 // is a THUMB function or not.  For global symbols, it is easy because
3120 // the Symbol object keeps the ELF symbol type.  For local symbol it is
3121 // harder because we cannot access this information.   So we override the
3122 // do_count_local_symbol in parent and scan local symbols to mark
3123 // THUMB functions.  This is not the most efficient way but I do not want to
3124 // slow down other ports by calling a per symbol targer hook inside
3125 // Sized_relobj<size, big_endian>::do_count_local_symbols. 
3126
3127 template<bool big_endian>
3128 void
3129 Arm_relobj<big_endian>::do_count_local_symbols(
3130     Stringpool_template<char>* pool,
3131     Stringpool_template<char>* dynpool)
3132 {
3133   // We need to fix-up the values of any local symbols whose type are
3134   // STT_ARM_TFUNC.
3135   
3136   // Ask parent to count the local symbols.
3137   Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
3138   const unsigned int loccount = this->local_symbol_count();
3139   if (loccount == 0)
3140     return;
3141
3142   // Intialize the thumb function bit-vector.
3143   std::vector<bool> empty_vector(loccount, false);
3144   this->local_symbol_is_thumb_function_.swap(empty_vector);
3145
3146   // Read the symbol table section header.
3147   const unsigned int symtab_shndx = this->symtab_shndx();
3148   elfcpp::Shdr<32, big_endian>
3149       symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
3150   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
3151
3152   // Read the local symbols.
3153   const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
3154   gold_assert(loccount == symtabshdr.get_sh_info());
3155   off_t locsize = loccount * sym_size;
3156   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
3157                                               locsize, true, true);
3158
3159   // Loop over the local symbols and mark any local symbols pointing
3160   // to THUMB functions.
3161
3162   // Skip the first dummy symbol.
3163   psyms += sym_size;
3164   typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
3165     this->local_values();
3166   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
3167     {
3168       elfcpp::Sym<32, big_endian> sym(psyms);
3169       elfcpp::STT st_type = sym.get_st_type();
3170       Symbol_value<32>& lv((*plocal_values)[i]);
3171       Arm_address input_value = lv.input_value();
3172
3173       if (st_type == elfcpp::STT_ARM_TFUNC
3174           || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
3175         {
3176           // This is a THUMB function.  Mark this and canonicalize the
3177           // symbol value by setting LSB.
3178           this->local_symbol_is_thumb_function_[i] = true;
3179           if ((input_value & 1) == 0)
3180             lv.set_input_value(input_value | 1);
3181         }
3182     }
3183 }
3184
3185 // Relocate sections.
3186 template<bool big_endian>
3187 void
3188 Arm_relobj<big_endian>::do_relocate_sections(
3189     const General_options& options,
3190     const Symbol_table* symtab,
3191     const Layout* layout,
3192     const unsigned char* pshdrs,
3193     typename Sized_relobj<32, big_endian>::Views* pviews)
3194 {
3195   // Call parent to relocate sections.
3196   Sized_relobj<32, big_endian>::do_relocate_sections(options, symtab, layout,
3197                                                      pshdrs, pviews); 
3198
3199   // We do not generate stubs if doing a relocatable link.
3200   if (parameters->options().relocatable())
3201     return;
3202
3203   // Relocate stub tables.
3204   unsigned int shnum = this->shnum();
3205
3206   Target_arm<big_endian>* arm_target =
3207     Target_arm<big_endian>::default_target();
3208
3209   Relocate_info<32, big_endian> relinfo;
3210   relinfo.options = &options;
3211   relinfo.symtab = symtab;
3212   relinfo.layout = layout;
3213   relinfo.object = this;
3214
3215   for (unsigned int i = 1; i < shnum; ++i)
3216     {
3217       Arm_input_section<big_endian>* arm_input_section =
3218         arm_target->find_arm_input_section(this, i);
3219
3220       if (arm_input_section == NULL
3221           || !arm_input_section->is_stub_table_owner()
3222           || arm_input_section->stub_table()->empty())
3223         continue;
3224
3225       // We cannot discard a section if it owns a stub table.
3226       Output_section* os = this->output_section(i);
3227       gold_assert(os != NULL);
3228
3229       relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
3230       relinfo.reloc_shdr = NULL;
3231       relinfo.data_shndx = i;
3232       relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
3233
3234       gold_assert((*pviews)[i].view != NULL);
3235
3236       // We are passed the output section view.  Adjust it to cover the
3237       // stub table only.
3238       Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
3239       gold_assert((stub_table->address() >= (*pviews)[i].address)
3240                   && ((stub_table->address() + stub_table->data_size())
3241                       <= (*pviews)[i].address + (*pviews)[i].view_size));
3242
3243       off_t offset = stub_table->address() - (*pviews)[i].address;
3244       unsigned char* view = (*pviews)[i].view + offset;
3245       Arm_address address = stub_table->address();
3246       section_size_type view_size = stub_table->data_size();
3247  
3248       stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
3249                                  view_size);
3250     }
3251 }
3252
3253 // A class to handle the PLT data.
3254
3255 template<bool big_endian>
3256 class Output_data_plt_arm : public Output_section_data
3257 {
3258  public:
3259   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
3260     Reloc_section;
3261
3262   Output_data_plt_arm(Layout*, Output_data_space*);
3263
3264   // Add an entry to the PLT.
3265   void
3266   add_entry(Symbol* gsym);
3267
3268   // Return the .rel.plt section data.
3269   const Reloc_section*
3270   rel_plt() const
3271   { return this->rel_; }
3272
3273  protected:
3274   void
3275   do_adjust_output_section(Output_section* os);
3276
3277   // Write to a map file.
3278   void
3279   do_print_to_mapfile(Mapfile* mapfile) const
3280   { mapfile->print_output_data(this, _("** PLT")); }
3281
3282  private:
3283   // Template for the first PLT entry.
3284   static const uint32_t first_plt_entry[5];
3285
3286   // Template for subsequent PLT entries. 
3287   static const uint32_t plt_entry[3];
3288
3289   // Set the final size.
3290   void
3291   set_final_data_size()
3292   {
3293     this->set_data_size(sizeof(first_plt_entry)
3294                         + this->count_ * sizeof(plt_entry));
3295   }
3296
3297   // Write out the PLT data.
3298   void
3299   do_write(Output_file*);
3300
3301   // The reloc section.
3302   Reloc_section* rel_;
3303   // The .got.plt section.
3304   Output_data_space* got_plt_;
3305   // The number of PLT entries.
3306   unsigned int count_;
3307 };
3308
3309 // Create the PLT section.  The ordinary .got section is an argument,
3310 // since we need to refer to the start.  We also create our own .got
3311 // section just for PLT entries.
3312
3313 template<bool big_endian>
3314 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
3315                                                      Output_data_space* got_plt)
3316   : Output_section_data(4), got_plt_(got_plt), count_(0)
3317 {
3318   this->rel_ = new Reloc_section(false);
3319   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
3320                                   elfcpp::SHF_ALLOC, this->rel_);
3321 }
3322
3323 template<bool big_endian>
3324 void
3325 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
3326 {
3327   os->set_entsize(0);
3328 }
3329
3330 // Add an entry to the PLT.
3331
3332 template<bool big_endian>
3333 void
3334 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
3335 {
3336   gold_assert(!gsym->has_plt_offset());
3337
3338   // Note that when setting the PLT offset we skip the initial
3339   // reserved PLT entry.
3340   gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
3341                        + sizeof(first_plt_entry));
3342
3343   ++this->count_;
3344
3345   section_offset_type got_offset = this->got_plt_->current_data_size();
3346
3347   // Every PLT entry needs a GOT entry which points back to the PLT
3348   // entry (this will be changed by the dynamic linker, normally
3349   // lazily when the function is called).
3350   this->got_plt_->set_current_data_size(got_offset + 4);
3351
3352   // Every PLT entry needs a reloc.
3353   gsym->set_needs_dynsym_entry();
3354   this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
3355                          got_offset);
3356
3357   // Note that we don't need to save the symbol.  The contents of the
3358   // PLT are independent of which symbols are used.  The symbols only
3359   // appear in the relocations.
3360 }
3361
3362 // ARM PLTs.
3363 // FIXME:  This is not very flexible.  Right now this has only been tested
3364 // on armv5te.  If we are to support additional architecture features like
3365 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
3366
3367 // The first entry in the PLT.
3368 template<bool big_endian>
3369 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
3370 {
3371   0xe52de004,   // str   lr, [sp, #-4]!
3372   0xe59fe004,   // ldr   lr, [pc, #4]
3373   0xe08fe00e,   // add   lr, pc, lr 
3374   0xe5bef008,   // ldr   pc, [lr, #8]!
3375   0x00000000,   // &GOT[0] - .
3376 };
3377
3378 // Subsequent entries in the PLT.
3379
3380 template<bool big_endian>
3381 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
3382 {
3383   0xe28fc600,   // add   ip, pc, #0xNN00000
3384   0xe28cca00,   // add   ip, ip, #0xNN000
3385   0xe5bcf000,   // ldr   pc, [ip, #0xNNN]!
3386 };
3387
3388 // Write out the PLT.  This uses the hand-coded instructions above,
3389 // and adjusts them as needed.  This is all specified by the arm ELF
3390 // Processor Supplement.
3391
3392 template<bool big_endian>
3393 void
3394 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
3395 {
3396   const off_t offset = this->offset();
3397   const section_size_type oview_size =
3398     convert_to_section_size_type(this->data_size());
3399   unsigned char* const oview = of->get_output_view(offset, oview_size);
3400
3401   const off_t got_file_offset = this->got_plt_->offset();
3402   const section_size_type got_size =
3403     convert_to_section_size_type(this->got_plt_->data_size());
3404   unsigned char* const got_view = of->get_output_view(got_file_offset,
3405                                                       got_size);
3406   unsigned char* pov = oview;
3407
3408   Arm_address plt_address = this->address();
3409   Arm_address got_address = this->got_plt_->address();
3410
3411   // Write first PLT entry.  All but the last word are constants.
3412   const size_t num_first_plt_words = (sizeof(first_plt_entry)
3413                                       / sizeof(plt_entry[0]));
3414   for (size_t i = 0; i < num_first_plt_words - 1; i++)
3415     elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
3416   // Last word in first PLT entry is &GOT[0] - .
3417   elfcpp::Swap<32, big_endian>::writeval(pov + 16,
3418                                          got_address - (plt_address + 16));
3419   pov += sizeof(first_plt_entry);
3420
3421   unsigned char* got_pov = got_view;
3422
3423   memset(got_pov, 0, 12);
3424   got_pov += 12;
3425
3426   const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
3427   unsigned int plt_offset = sizeof(first_plt_entry);
3428   unsigned int plt_rel_offset = 0;
3429   unsigned int got_offset = 12;
3430   const unsigned int count = this->count_;
3431   for (unsigned int i = 0;
3432        i < count;
3433        ++i,
3434          pov += sizeof(plt_entry),
3435          got_pov += 4,
3436          plt_offset += sizeof(plt_entry),
3437          plt_rel_offset += rel_size,
3438          got_offset += 4)
3439     {
3440       // Set and adjust the PLT entry itself.
3441       int32_t offset = ((got_address + got_offset)
3442                          - (plt_address + plt_offset + 8));
3443
3444       gold_assert(offset >= 0 && offset < 0x0fffffff);
3445       uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
3446       elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
3447       uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
3448       elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
3449       uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
3450       elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
3451
3452       // Set the entry in the GOT.
3453       elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
3454     }
3455
3456   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
3457   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
3458
3459   of->write_output_view(offset, oview_size, oview);
3460   of->write_output_view(got_file_offset, got_size, got_view);
3461 }
3462
3463 // Create a PLT entry for a global symbol.
3464
3465 template<bool big_endian>
3466 void
3467 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
3468                                        Symbol* gsym)
3469 {
3470   if (gsym->has_plt_offset())
3471     return;
3472
3473   if (this->plt_ == NULL)
3474     {
3475       // Create the GOT sections first.
3476       this->got_section(symtab, layout);
3477
3478       this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
3479       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
3480                                       (elfcpp::SHF_ALLOC
3481                                        | elfcpp::SHF_EXECINSTR),
3482                                       this->plt_);
3483     }
3484   this->plt_->add_entry(gsym);
3485 }
3486
3487 // Report an unsupported relocation against a local symbol.
3488
3489 template<bool big_endian>
3490 void
3491 Target_arm<big_endian>::Scan::unsupported_reloc_local(
3492     Sized_relobj<32, big_endian>* object,
3493     unsigned int r_type)
3494 {
3495   gold_error(_("%s: unsupported reloc %u against local symbol"),
3496              object->name().c_str(), r_type);
3497 }
3498
3499 // We are about to emit a dynamic relocation of type R_TYPE.  If the
3500 // dynamic linker does not support it, issue an error.  The GNU linker
3501 // only issues a non-PIC error for an allocated read-only section.
3502 // Here we know the section is allocated, but we don't know that it is
3503 // read-only.  But we check for all the relocation types which the
3504 // glibc dynamic linker supports, so it seems appropriate to issue an
3505 // error even if the section is not read-only.
3506
3507 template<bool big_endian>
3508 void
3509 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
3510                                             unsigned int r_type)
3511 {
3512   switch (r_type)
3513     {
3514     // These are the relocation types supported by glibc for ARM.
3515     case elfcpp::R_ARM_RELATIVE:
3516     case elfcpp::R_ARM_COPY:
3517     case elfcpp::R_ARM_GLOB_DAT:
3518     case elfcpp::R_ARM_JUMP_SLOT:
3519     case elfcpp::R_ARM_ABS32:
3520     case elfcpp::R_ARM_ABS32_NOI:
3521     case elfcpp::R_ARM_PC24:
3522     // FIXME: The following 3 types are not supported by Android's dynamic
3523     // linker.
3524     case elfcpp::R_ARM_TLS_DTPMOD32:
3525     case elfcpp::R_ARM_TLS_DTPOFF32:
3526     case elfcpp::R_ARM_TLS_TPOFF32:
3527       return;
3528
3529     default:
3530       // This prevents us from issuing more than one error per reloc
3531       // section.  But we can still wind up issuing more than one
3532       // error per object file.
3533       if (this->issued_non_pic_error_)
3534         return;
3535       object->error(_("requires unsupported dynamic reloc; "
3536                       "recompile with -fPIC"));
3537       this->issued_non_pic_error_ = true;
3538       return;
3539
3540     case elfcpp::R_ARM_NONE:
3541       gold_unreachable();
3542     }
3543 }
3544
3545 // Scan a relocation for a local symbol.
3546 // FIXME: This only handles a subset of relocation types used by Android
3547 // on ARM v5te devices.
3548
3549 template<bool big_endian>
3550 inline void
3551 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
3552                                     Layout* layout,
3553                                     Target_arm* target,
3554                                     Sized_relobj<32, big_endian>* object,
3555                                     unsigned int data_shndx,
3556                                     Output_section* output_section,
3557                                     const elfcpp::Rel<32, big_endian>& reloc,
3558                                     unsigned int r_type,
3559                                     const elfcpp::Sym<32, big_endian>&)
3560 {
3561   r_type = get_real_reloc_type(r_type);
3562   switch (r_type)
3563     {
3564     case elfcpp::R_ARM_NONE:
3565       break;
3566
3567     case elfcpp::R_ARM_ABS32:
3568     case elfcpp::R_ARM_ABS32_NOI:
3569       // If building a shared library (or a position-independent
3570       // executable), we need to create a dynamic relocation for
3571       // this location. The relocation applied at link time will
3572       // apply the link-time value, so we flag the location with
3573       // an R_ARM_RELATIVE relocation so the dynamic loader can
3574       // relocate it easily.
3575       if (parameters->options().output_is_position_independent())
3576         {
3577           Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3578           unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3579           // If we are to add more other reloc types than R_ARM_ABS32,
3580           // we need to add check_non_pic(object, r_type) here.
3581           rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
3582                                       output_section, data_shndx,
3583                                       reloc.get_r_offset());
3584         }
3585       break;
3586
3587     case elfcpp::R_ARM_REL32:
3588     case elfcpp::R_ARM_THM_CALL:
3589     case elfcpp::R_ARM_CALL:
3590     case elfcpp::R_ARM_PREL31:
3591     case elfcpp::R_ARM_JUMP24:
3592     case elfcpp::R_ARM_PLT32:
3593     case elfcpp::R_ARM_THM_ABS5:
3594     case elfcpp::R_ARM_ABS8:
3595     case elfcpp::R_ARM_ABS12:
3596     case elfcpp::R_ARM_ABS16:
3597     case elfcpp::R_ARM_BASE_ABS:
3598     case elfcpp::R_ARM_MOVW_ABS_NC:
3599     case elfcpp::R_ARM_MOVT_ABS:
3600     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3601     case elfcpp::R_ARM_THM_MOVT_ABS:
3602     case elfcpp::R_ARM_MOVW_PREL_NC:
3603     case elfcpp::R_ARM_MOVT_PREL:
3604     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3605     case elfcpp::R_ARM_THM_MOVT_PREL:
3606       break;
3607
3608     case elfcpp::R_ARM_GOTOFF32:
3609       // We need a GOT section:
3610       target->got_section(symtab, layout);
3611       break;
3612
3613     case elfcpp::R_ARM_BASE_PREL:
3614       // FIXME: What about this?
3615       break;
3616
3617     case elfcpp::R_ARM_GOT_BREL:
3618     case elfcpp::R_ARM_GOT_PREL:
3619       {
3620         // The symbol requires a GOT entry.
3621         Output_data_got<32, big_endian>* got =
3622           target->got_section(symtab, layout);
3623         unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3624         if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
3625           {
3626             // If we are generating a shared object, we need to add a
3627             // dynamic RELATIVE relocation for this symbol's GOT entry.
3628             if (parameters->options().output_is_position_independent())
3629               {
3630                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3631                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
3632                 rel_dyn->add_local_relative(
3633                     object, r_sym, elfcpp::R_ARM_RELATIVE, got,
3634                     object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
3635               }
3636           }
3637       }
3638       break;
3639
3640     case elfcpp::R_ARM_TARGET1:
3641       // This should have been mapped to another type already.
3642       // Fall through.
3643     case elfcpp::R_ARM_COPY:
3644     case elfcpp::R_ARM_GLOB_DAT:
3645     case elfcpp::R_ARM_JUMP_SLOT:
3646     case elfcpp::R_ARM_RELATIVE:
3647       // These are relocations which should only be seen by the
3648       // dynamic linker, and should never be seen here.
3649       gold_error(_("%s: unexpected reloc %u in object file"),
3650                  object->name().c_str(), r_type);
3651       break;
3652
3653     default:
3654       unsupported_reloc_local(object, r_type);
3655       break;
3656     }
3657 }
3658
3659 // Report an unsupported relocation against a global symbol.
3660
3661 template<bool big_endian>
3662 void
3663 Target_arm<big_endian>::Scan::unsupported_reloc_global(
3664     Sized_relobj<32, big_endian>* object,
3665     unsigned int r_type,
3666     Symbol* gsym)
3667 {
3668   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3669              object->name().c_str(), r_type, gsym->demangled_name().c_str());
3670 }
3671
3672 // Scan a relocation for a global symbol.
3673 // FIXME: This only handles a subset of relocation types used by Android
3674 // on ARM v5te devices.
3675
3676 template<bool big_endian>
3677 inline void
3678 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
3679                                      Layout* layout,
3680                                      Target_arm* target,
3681                                      Sized_relobj<32, big_endian>* object,
3682                                      unsigned int data_shndx,
3683                                      Output_section* output_section,
3684                                      const elfcpp::Rel<32, big_endian>& reloc,
3685                                      unsigned int r_type,
3686                                      Symbol* gsym)
3687 {
3688   r_type = get_real_reloc_type(r_type);
3689   switch (r_type)
3690     {
3691     case elfcpp::R_ARM_NONE:
3692       break;
3693
3694     case elfcpp::R_ARM_ABS32:
3695     case elfcpp::R_ARM_ABS32_NOI:
3696       {
3697         // Make a dynamic relocation if necessary.
3698         if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
3699           {
3700             if (target->may_need_copy_reloc(gsym))
3701               {
3702                 target->copy_reloc(symtab, layout, object,
3703                                    data_shndx, output_section, gsym, reloc);
3704               }
3705             else if (gsym->can_use_relative_reloc(false))
3706               {
3707                 // If we are to add more other reloc types than R_ARM_ABS32,
3708                 // we need to add check_non_pic(object, r_type) here.
3709                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3710                 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
3711                                              output_section, object,
3712                                              data_shndx, reloc.get_r_offset());
3713               }
3714             else
3715               {
3716                 // If we are to add more other reloc types than R_ARM_ABS32,
3717                 // we need to add check_non_pic(object, r_type) here.
3718                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3719                 rel_dyn->add_global(gsym, r_type, output_section, object,
3720                                     data_shndx, reloc.get_r_offset());
3721               }
3722           }
3723       }
3724       break;
3725
3726     case elfcpp::R_ARM_MOVW_ABS_NC:
3727     case elfcpp::R_ARM_MOVT_ABS:
3728     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
3729     case elfcpp::R_ARM_THM_MOVT_ABS:
3730     case elfcpp::R_ARM_MOVW_PREL_NC:
3731     case elfcpp::R_ARM_MOVT_PREL:
3732     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
3733     case elfcpp::R_ARM_THM_MOVT_PREL:
3734       break;
3735
3736     case elfcpp::R_ARM_THM_ABS5:
3737     case elfcpp::R_ARM_ABS8:
3738     case elfcpp::R_ARM_ABS12:
3739     case elfcpp::R_ARM_ABS16:
3740     case elfcpp::R_ARM_BASE_ABS:
3741       {
3742         // No dynamic relocs of this kinds.
3743         // Report the error in case of PIC.
3744         int flags = Symbol::NON_PIC_REF;
3745         if (gsym->type() == elfcpp::STT_FUNC
3746             || gsym->type() == elfcpp::STT_ARM_TFUNC)
3747           flags |= Symbol::FUNCTION_CALL;
3748         if (gsym->needs_dynamic_reloc(flags))
3749           check_non_pic(object, r_type);
3750       }
3751       break;
3752
3753     case elfcpp::R_ARM_REL32:
3754     case elfcpp::R_ARM_PREL31:
3755       {
3756         // Make a dynamic relocation if necessary.
3757         int flags = Symbol::NON_PIC_REF;
3758         if (gsym->needs_dynamic_reloc(flags))
3759           {
3760             if (target->may_need_copy_reloc(gsym))
3761               {
3762                 target->copy_reloc(symtab, layout, object,
3763                                    data_shndx, output_section, gsym, reloc);
3764               }
3765             else
3766               {
3767                 check_non_pic(object, r_type);
3768                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3769                 rel_dyn->add_global(gsym, r_type, output_section, object,
3770                                     data_shndx, reloc.get_r_offset());
3771               }
3772           }
3773       }
3774       break;
3775
3776     case elfcpp::R_ARM_JUMP24:
3777     case elfcpp::R_ARM_THM_CALL:
3778     case elfcpp::R_ARM_CALL:
3779       {
3780         if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
3781           target->make_plt_entry(symtab, layout, gsym);
3782         // Make a dynamic relocation if necessary.
3783         int flags = Symbol::NON_PIC_REF;
3784         if (gsym->type() == elfcpp::STT_FUNC
3785             || gsym->type() == elfcpp::STT_ARM_TFUNC)
3786           flags |= Symbol::FUNCTION_CALL;
3787         if (gsym->needs_dynamic_reloc(flags))
3788           {
3789             if (target->may_need_copy_reloc(gsym))
3790               {
3791                 target->copy_reloc(symtab, layout, object,
3792                                    data_shndx, output_section, gsym,
3793                                    reloc);
3794               }
3795             else
3796               {
3797                 check_non_pic(object, r_type);
3798                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3799                 rel_dyn->add_global(gsym, r_type, output_section, object,
3800                                     data_shndx, reloc.get_r_offset());
3801               }
3802           }
3803       }
3804       break;
3805
3806     case elfcpp::R_ARM_PLT32:
3807       // If the symbol is fully resolved, this is just a relative
3808       // local reloc.  Otherwise we need a PLT entry.
3809       if (gsym->final_value_is_known())
3810         break;
3811       // If building a shared library, we can also skip the PLT entry
3812       // if the symbol is defined in the output file and is protected
3813       // or hidden.
3814       if (gsym->is_defined()
3815           && !gsym->is_from_dynobj()
3816           && !gsym->is_preemptible())
3817         break;
3818       target->make_plt_entry(symtab, layout, gsym);
3819       break;
3820
3821     case elfcpp::R_ARM_GOTOFF32:
3822       // We need a GOT section.
3823       target->got_section(symtab, layout);
3824       break;
3825
3826     case elfcpp::R_ARM_BASE_PREL:
3827       // FIXME: What about this?
3828       break;
3829       
3830     case elfcpp::R_ARM_GOT_BREL:
3831     case elfcpp::R_ARM_GOT_PREL:
3832       {
3833         // The symbol requires a GOT entry.
3834         Output_data_got<32, big_endian>* got =
3835           target->got_section(symtab, layout);
3836         if (gsym->final_value_is_known())
3837           got->add_global(gsym, GOT_TYPE_STANDARD);
3838         else
3839           {
3840             // If this symbol is not fully resolved, we need to add a
3841             // GOT entry with a dynamic relocation.
3842             Reloc_section* rel_dyn = target->rel_dyn_section(layout);
3843             if (gsym->is_from_dynobj()
3844                 || gsym->is_undefined()
3845                 || gsym->is_preemptible())
3846               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
3847                                        rel_dyn, elfcpp::R_ARM_GLOB_DAT);
3848             else
3849               {
3850                 if (got->add_global(gsym, GOT_TYPE_STANDARD))
3851                   rel_dyn->add_global_relative(
3852                       gsym, elfcpp::R_ARM_RELATIVE, got,
3853                       gsym->got_offset(GOT_TYPE_STANDARD));
3854               }
3855           }
3856       }
3857       break;
3858
3859     case elfcpp::R_ARM_TARGET1:
3860       // This should have been mapped to another type already.
3861       // Fall through.
3862     case elfcpp::R_ARM_COPY:
3863     case elfcpp::R_ARM_GLOB_DAT:
3864     case elfcpp::R_ARM_JUMP_SLOT:
3865     case elfcpp::R_ARM_RELATIVE:
3866       // These are relocations which should only be seen by the
3867       // dynamic linker, and should never be seen here.
3868       gold_error(_("%s: unexpected reloc %u in object file"),
3869                  object->name().c_str(), r_type);
3870       break;
3871
3872     default:
3873       unsupported_reloc_global(object, r_type, gsym);
3874       break;
3875     }
3876 }
3877
3878 // Process relocations for gc.
3879
3880 template<bool big_endian>
3881 void
3882 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
3883                                           Layout* layout,
3884                                           Sized_relobj<32, big_endian>* object,
3885                                           unsigned int data_shndx,
3886                                           unsigned int,
3887                                           const unsigned char* prelocs,
3888                                           size_t reloc_count,
3889                                           Output_section* output_section,
3890                                           bool needs_special_offset_handling,
3891                                           size_t local_symbol_count,
3892                                           const unsigned char* plocal_symbols)
3893 {
3894   typedef Target_arm<big_endian> Arm;
3895   typedef typename Target_arm<big_endian>::Scan Scan;
3896
3897   gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
3898     symtab,
3899     layout,
3900     this,
3901     object,
3902     data_shndx,
3903     prelocs,
3904     reloc_count,
3905     output_section,
3906     needs_special_offset_handling,
3907     local_symbol_count,
3908     plocal_symbols);
3909 }
3910
3911 // Scan relocations for a section.
3912
3913 template<bool big_endian>
3914 void
3915 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
3916                                     Layout* layout,
3917                                     Sized_relobj<32, big_endian>* object,
3918                                     unsigned int data_shndx,
3919                                     unsigned int sh_type,
3920                                     const unsigned char* prelocs,
3921                                     size_t reloc_count,
3922                                     Output_section* output_section,
3923                                     bool needs_special_offset_handling,
3924                                     size_t local_symbol_count,
3925                                     const unsigned char* plocal_symbols)
3926 {
3927   typedef typename Target_arm<big_endian>::Scan Scan;
3928   if (sh_type == elfcpp::SHT_RELA)
3929     {
3930       gold_error(_("%s: unsupported RELA reloc section"),
3931                  object->name().c_str());
3932       return;
3933     }
3934
3935   gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
3936     symtab,
3937     layout,
3938     this,
3939     object,
3940     data_shndx,
3941     prelocs,
3942     reloc_count,
3943     output_section,
3944     needs_special_offset_handling,
3945     local_symbol_count,
3946     plocal_symbols);
3947 }
3948
3949 // Finalize the sections.
3950
3951 template<bool big_endian>
3952 void
3953 Target_arm<big_endian>::do_finalize_sections(Layout* layout)
3954 {
3955   // Fill in some more dynamic tags.
3956   Output_data_dynamic* const odyn = layout->dynamic_data();
3957   if (odyn != NULL)
3958     {
3959       if (this->got_plt_ != NULL)
3960         odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
3961
3962       if (this->plt_ != NULL)
3963         {
3964           const Output_data* od = this->plt_->rel_plt();
3965           odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
3966           odyn->add_section_address(elfcpp::DT_JMPREL, od);
3967           odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
3968         }
3969
3970       if (this->rel_dyn_ != NULL)
3971         {
3972           const Output_data* od = this->rel_dyn_;
3973           odyn->add_section_address(elfcpp::DT_REL, od);
3974           odyn->add_section_size(elfcpp::DT_RELSZ, od);
3975           odyn->add_constant(elfcpp::DT_RELENT,
3976                              elfcpp::Elf_sizes<32>::rel_size);
3977         }
3978
3979       if (!parameters->options().shared())
3980         {
3981           // The value of the DT_DEBUG tag is filled in by the dynamic
3982           // linker at run time, and used by the debugger.
3983           odyn->add_constant(elfcpp::DT_DEBUG, 0);
3984         }
3985     }
3986
3987   // Emit any relocs we saved in an attempt to avoid generating COPY
3988   // relocs.
3989   if (this->copy_relocs_.any_saved_relocs())
3990     this->copy_relocs_.emit(this->rel_dyn_section(layout));
3991
3992   // For the ARM target, we need to add a PT_ARM_EXIDX segment for
3993   // the .ARM.exidx section.
3994   if (!layout->script_options()->saw_phdrs_clause()
3995       && !parameters->options().relocatable())
3996     {
3997       Output_section* exidx_section =
3998         layout->find_output_section(".ARM.exidx");
3999
4000       if (exidx_section != NULL
4001           && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
4002         {
4003           gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
4004                       == NULL);
4005           Output_segment*  exidx_segment =
4006             layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
4007           exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
4008         }
4009     }
4010 }
4011
4012 // Return whether a direct absolute static relocation needs to be applied.
4013 // In cases where Scan::local() or Scan::global() has created
4014 // a dynamic relocation other than R_ARM_RELATIVE, the addend
4015 // of the relocation is carried in the data, and we must not
4016 // apply the static relocation.
4017
4018 template<bool big_endian>
4019 inline bool
4020 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
4021     const Sized_symbol<32>* gsym,
4022     int ref_flags,
4023     bool is_32bit,
4024     Output_section* output_section)
4025 {
4026   // If the output section is not allocated, then we didn't call
4027   // scan_relocs, we didn't create a dynamic reloc, and we must apply
4028   // the reloc here.
4029   if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
4030       return true;
4031
4032   // For local symbols, we will have created a non-RELATIVE dynamic
4033   // relocation only if (a) the output is position independent,
4034   // (b) the relocation is absolute (not pc- or segment-relative), and
4035   // (c) the relocation is not 32 bits wide.
4036   if (gsym == NULL)
4037     return !(parameters->options().output_is_position_independent()
4038              && (ref_flags & Symbol::ABSOLUTE_REF)
4039              && !is_32bit);
4040
4041   // For global symbols, we use the same helper routines used in the
4042   // scan pass.  If we did not create a dynamic relocation, or if we
4043   // created a RELATIVE dynamic relocation, we should apply the static
4044   // relocation.
4045   bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
4046   bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
4047                  && gsym->can_use_relative_reloc(ref_flags
4048                                                  & Symbol::FUNCTION_CALL);
4049   return !has_dyn || is_rel;
4050 }
4051
4052 // Perform a relocation.
4053
4054 template<bool big_endian>
4055 inline bool
4056 Target_arm<big_endian>::Relocate::relocate(
4057     const Relocate_info<32, big_endian>* relinfo,
4058     Target_arm* target,
4059     Output_section *output_section,
4060     size_t relnum,
4061     const elfcpp::Rel<32, big_endian>& rel,
4062     unsigned int r_type,
4063     const Sized_symbol<32>* gsym,
4064     const Symbol_value<32>* psymval,
4065     unsigned char* view,
4066     Arm_address address,
4067     section_size_type /* view_size */ )
4068 {
4069   typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
4070
4071   r_type = get_real_reloc_type(r_type);
4072
4073   // If this the symbol may be a Thumb function, set thumb bit to 1.
4074   bool has_thumb_bit = ((gsym != NULL)
4075                         && (gsym->type() == elfcpp::STT_FUNC
4076                             || gsym->type() == elfcpp::STT_ARM_TFUNC));
4077
4078   // Pick the value to use for symbols defined in shared objects.
4079   Symbol_value<32> symval;
4080   if (gsym != NULL
4081       && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
4082     {
4083       symval.set_output_value(target->plt_section()->address()
4084                               + gsym->plt_offset());
4085       psymval = &symval;
4086       has_thumb_bit = 0;
4087     }
4088
4089   const Sized_relobj<32, big_endian>* object = relinfo->object;
4090   
4091   // Get the GOT offset if needed.
4092   // The GOT pointer points to the end of the GOT section.
4093   // We need to subtract the size of the GOT section to get
4094   // the actual offset to use in the relocation.
4095   bool have_got_offset = false;
4096   unsigned int got_offset = 0;
4097   switch (r_type)
4098     {
4099     case elfcpp::R_ARM_GOT_BREL:
4100     case elfcpp::R_ARM_GOT_PREL:
4101       if (gsym != NULL)
4102         {
4103           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4104           got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4105                         - target->got_size());
4106         }
4107       else
4108         {
4109           unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
4110           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4111           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4112                         - target->got_size());
4113         }
4114       have_got_offset = true;
4115       break;
4116
4117     default:
4118       break;
4119     }
4120
4121   typename Arm_relocate_functions::Status reloc_status =
4122         Arm_relocate_functions::STATUS_OKAY;
4123   switch (r_type)
4124     {
4125     case elfcpp::R_ARM_NONE:
4126       break;
4127
4128     case elfcpp::R_ARM_ABS8:
4129       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4130                                     output_section))
4131         reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
4132       break;
4133
4134     case elfcpp::R_ARM_ABS12:
4135       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4136                                     output_section))
4137         reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
4138       break;
4139
4140     case elfcpp::R_ARM_ABS16:
4141       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4142                                     output_section))
4143         reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
4144       break;
4145
4146     case elfcpp::R_ARM_ABS32:
4147       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4148                                     output_section))
4149         reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4150                                                      has_thumb_bit);
4151       break;
4152
4153     case elfcpp::R_ARM_ABS32_NOI:
4154       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4155                                     output_section))
4156         // No thumb bit for this relocation: (S + A)
4157         reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
4158                                                      false);
4159       break;
4160
4161     case elfcpp::R_ARM_MOVW_ABS_NC:
4162       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4163                                     output_section))
4164         reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
4165                                                            psymval,
4166                                                            has_thumb_bit);
4167       else
4168         gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
4169                      "a shared object; recompile with -fPIC"));
4170       break;
4171
4172     case elfcpp::R_ARM_MOVT_ABS:
4173       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4174                                     output_section))
4175         reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
4176       else
4177         gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
4178                      "a shared object; recompile with -fPIC"));
4179       break;
4180
4181     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4182       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4183                                     output_section))
4184         reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
4185                                                                psymval,
4186                                                                has_thumb_bit);
4187       else
4188         gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
4189                      "making a shared object; recompile with -fPIC"));
4190       break;
4191
4192     case elfcpp::R_ARM_THM_MOVT_ABS:
4193       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4194                                     output_section))
4195         reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
4196                                                             psymval);
4197       else
4198         gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
4199                      "making a shared object; recompile with -fPIC"));
4200       break;
4201
4202     case elfcpp::R_ARM_MOVW_PREL_NC:
4203       reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
4204                                                           psymval, address,
4205                                                           has_thumb_bit);
4206       break;
4207
4208     case elfcpp::R_ARM_MOVT_PREL:
4209       reloc_status = Arm_relocate_functions::movt_prel(view, object,
4210                                                        psymval, address);
4211       break;
4212
4213     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4214       reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
4215                                                               psymval, address,
4216                                                               has_thumb_bit);
4217       break;
4218
4219     case elfcpp::R_ARM_THM_MOVT_PREL:
4220       reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
4221                                                            psymval, address);
4222       break;
4223         
4224     case elfcpp::R_ARM_REL32:
4225       reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
4226                                                    address, has_thumb_bit);
4227       break;
4228
4229     case elfcpp::R_ARM_THM_ABS5:
4230       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
4231                                     output_section))
4232         reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
4233       break;
4234
4235     case elfcpp::R_ARM_THM_CALL:
4236       reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
4237                                                       address, has_thumb_bit);
4238       break;
4239
4240     case elfcpp::R_ARM_GOTOFF32:
4241       {
4242         Arm_address got_origin;
4243         got_origin = target->got_plt_section()->address();
4244         reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
4245                                                      got_origin, has_thumb_bit);
4246       }
4247       break;
4248
4249     case elfcpp::R_ARM_BASE_PREL:
4250       {
4251         uint32_t origin;
4252         // Get the addressing origin of the output segment defining the 
4253         // symbol gsym (AAELF 4.6.1.2 Relocation types)
4254         gold_assert(gsym != NULL); 
4255         if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4256           origin = gsym->output_segment()->vaddr();
4257         else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4258           origin = gsym->output_data()->address();
4259         else
4260           {
4261             gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4262                                    _("cannot find origin of R_ARM_BASE_PREL"));
4263             return true;
4264           }
4265         reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
4266       }
4267       break;
4268
4269     case elfcpp::R_ARM_BASE_ABS:
4270       {
4271         if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
4272                                       output_section))
4273           break;
4274
4275         uint32_t origin;
4276         // Get the addressing origin of the output segment defining
4277         // the symbol gsym (AAELF 4.6.1.2 Relocation types).
4278         if (gsym == NULL)
4279           // R_ARM_BASE_ABS with the NULL symbol will give the
4280           // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
4281           // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
4282           origin = target->got_plt_section()->address();
4283         else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
4284           origin = gsym->output_segment()->vaddr();
4285         else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
4286           origin = gsym->output_data()->address();
4287         else
4288           {
4289             gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4290                                    _("cannot find origin of R_ARM_BASE_ABS"));
4291             return true;
4292           }
4293
4294         reloc_status = Arm_relocate_functions::base_abs(view, origin);
4295       }
4296       break;
4297
4298     case elfcpp::R_ARM_GOT_BREL:
4299       gold_assert(have_got_offset);
4300       reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
4301       break;
4302
4303     case elfcpp::R_ARM_GOT_PREL:
4304       gold_assert(have_got_offset);
4305       // Get the address origin for GOT PLT, which is allocated right
4306       // after the GOT section, to calculate an absolute address of
4307       // the symbol GOT entry (got_origin + got_offset).
4308       Arm_address got_origin;
4309       got_origin = target->got_plt_section()->address();
4310       reloc_status = Arm_relocate_functions::got_prel(view,
4311                                                       got_origin + got_offset,
4312                                                       address);
4313       break;
4314
4315     case elfcpp::R_ARM_PLT32:
4316       gold_assert(gsym == NULL
4317                   || gsym->has_plt_offset()
4318                   || gsym->final_value_is_known()
4319                   || (gsym->is_defined()
4320                       && !gsym->is_from_dynobj()
4321                       && !gsym->is_preemptible()));
4322       reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
4323                                                    address, has_thumb_bit);
4324       break;
4325
4326     case elfcpp::R_ARM_CALL:
4327       reloc_status = Arm_relocate_functions::call(view, object, psymval,
4328                                                   address, has_thumb_bit);
4329       break;
4330
4331     case elfcpp::R_ARM_JUMP24:
4332       reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
4333                                                     address, has_thumb_bit);
4334       break;
4335
4336     case elfcpp::R_ARM_PREL31:
4337       reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
4338                                                     address, has_thumb_bit);
4339       break;
4340
4341     case elfcpp::R_ARM_TARGET1:
4342       // This should have been mapped to another type already.
4343       // Fall through.
4344     case elfcpp::R_ARM_COPY:
4345     case elfcpp::R_ARM_GLOB_DAT:
4346     case elfcpp::R_ARM_JUMP_SLOT:
4347     case elfcpp::R_ARM_RELATIVE:
4348       // These are relocations which should only be seen by the
4349       // dynamic linker, and should never be seen here.
4350       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4351                              _("unexpected reloc %u in object file"),
4352                              r_type);
4353       break;
4354
4355     default:
4356       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4357                              _("unsupported reloc %u"),
4358                              r_type);
4359       break;
4360     }
4361
4362   // Report any errors.
4363   switch (reloc_status)
4364     {
4365     case Arm_relocate_functions::STATUS_OKAY:
4366       break;
4367     case Arm_relocate_functions::STATUS_OVERFLOW:
4368       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
4369                              _("relocation overflow in relocation %u"),
4370                              r_type);
4371       break;
4372     case Arm_relocate_functions::STATUS_BAD_RELOC:
4373       gold_error_at_location(
4374         relinfo,
4375         relnum,
4376         rel.get_r_offset(),
4377         _("unexpected opcode while processing relocation %u"),
4378         r_type);
4379       break;
4380     default:
4381       gold_unreachable();
4382     }
4383
4384   return true;
4385 }
4386
4387 // Relocate section data.
4388
4389 template<bool big_endian>
4390 void
4391 Target_arm<big_endian>::relocate_section(
4392     const Relocate_info<32, big_endian>* relinfo,
4393     unsigned int sh_type,
4394     const unsigned char* prelocs,
4395     size_t reloc_count,
4396     Output_section* output_section,
4397     bool needs_special_offset_handling,
4398     unsigned char* view,
4399     Arm_address address,
4400     section_size_type view_size,
4401     const Reloc_symbol_changes* reloc_symbol_changes)
4402 {
4403   typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
4404   gold_assert(sh_type == elfcpp::SHT_REL);
4405
4406   gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
4407                          Arm_relocate>(
4408     relinfo,
4409     this,
4410     prelocs,
4411     reloc_count,
4412     output_section,
4413     needs_special_offset_handling,
4414     view,
4415     address,
4416     view_size,
4417     reloc_symbol_changes);
4418 }
4419
4420 // Return the size of a relocation while scanning during a relocatable
4421 // link.
4422
4423 template<bool big_endian>
4424 unsigned int
4425 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
4426     unsigned int r_type,
4427     Relobj* object)
4428 {
4429   r_type = get_real_reloc_type(r_type);
4430   switch (r_type)
4431     {
4432     case elfcpp::R_ARM_NONE:
4433       return 0;
4434
4435     case elfcpp::R_ARM_ABS8:
4436       return 1;
4437
4438     case elfcpp::R_ARM_ABS16:
4439     case elfcpp::R_ARM_THM_ABS5:
4440       return 2;
4441
4442     case elfcpp::R_ARM_ABS32:
4443     case elfcpp::R_ARM_ABS32_NOI:
4444     case elfcpp::R_ARM_ABS12:
4445     case elfcpp::R_ARM_BASE_ABS:
4446     case elfcpp::R_ARM_REL32:
4447     case elfcpp::R_ARM_THM_CALL:
4448     case elfcpp::R_ARM_GOTOFF32:
4449     case elfcpp::R_ARM_BASE_PREL:
4450     case elfcpp::R_ARM_GOT_BREL:
4451     case elfcpp::R_ARM_GOT_PREL:
4452     case elfcpp::R_ARM_PLT32:
4453     case elfcpp::R_ARM_CALL:
4454     case elfcpp::R_ARM_JUMP24:
4455     case elfcpp::R_ARM_PREL31:
4456     case elfcpp::R_ARM_MOVW_ABS_NC:
4457     case elfcpp::R_ARM_MOVT_ABS:
4458     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
4459     case elfcpp::R_ARM_THM_MOVT_ABS:
4460     case elfcpp::R_ARM_MOVW_PREL_NC:
4461     case elfcpp::R_ARM_MOVT_PREL:
4462     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
4463     case elfcpp::R_ARM_THM_MOVT_PREL:
4464       return 4;
4465
4466     case elfcpp::R_ARM_TARGET1:
4467       // This should have been mapped to another type already.
4468       // Fall through.
4469     case elfcpp::R_ARM_COPY:
4470     case elfcpp::R_ARM_GLOB_DAT:
4471     case elfcpp::R_ARM_JUMP_SLOT:
4472     case elfcpp::R_ARM_RELATIVE:
4473       // These are relocations which should only be seen by the
4474       // dynamic linker, and should never be seen here.
4475       gold_error(_("%s: unexpected reloc %u in object file"),
4476                  object->name().c_str(), r_type);
4477       return 0;
4478
4479     default:
4480       object->error(_("unsupported reloc %u in object file"), r_type);
4481       return 0;
4482     }
4483 }
4484
4485 // Scan the relocs during a relocatable link.
4486
4487 template<bool big_endian>
4488 void
4489 Target_arm<big_endian>::scan_relocatable_relocs(
4490     Symbol_table* symtab,
4491     Layout* layout,
4492     Sized_relobj<32, big_endian>* object,
4493     unsigned int data_shndx,
4494     unsigned int sh_type,
4495     const unsigned char* prelocs,
4496     size_t reloc_count,
4497     Output_section* output_section,
4498     bool needs_special_offset_handling,
4499     size_t local_symbol_count,
4500     const unsigned char* plocal_symbols,
4501     Relocatable_relocs* rr)
4502 {
4503   gold_assert(sh_type == elfcpp::SHT_REL);
4504
4505   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
4506     Relocatable_size_for_reloc> Scan_relocatable_relocs;
4507
4508   gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
4509       Scan_relocatable_relocs>(
4510     symtab,
4511     layout,
4512     object,
4513     data_shndx,
4514     prelocs,
4515     reloc_count,
4516     output_section,
4517     needs_special_offset_handling,
4518     local_symbol_count,
4519     plocal_symbols,
4520     rr);
4521 }
4522
4523 // Relocate a section during a relocatable link.
4524
4525 template<bool big_endian>
4526 void
4527 Target_arm<big_endian>::relocate_for_relocatable(
4528     const Relocate_info<32, big_endian>* relinfo,
4529     unsigned int sh_type,
4530     const unsigned char* prelocs,
4531     size_t reloc_count,
4532     Output_section* output_section,
4533     off_t offset_in_output_section,
4534     const Relocatable_relocs* rr,
4535     unsigned char* view,
4536     Arm_address view_address,
4537     section_size_type view_size,
4538     unsigned char* reloc_view,
4539     section_size_type reloc_view_size)
4540 {
4541   gold_assert(sh_type == elfcpp::SHT_REL);
4542
4543   gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
4544     relinfo,
4545     prelocs,
4546     reloc_count,
4547     output_section,
4548     offset_in_output_section,
4549     rr,
4550     view,
4551     view_address,
4552     view_size,
4553     reloc_view,
4554     reloc_view_size);
4555 }
4556
4557 // Return the value to use for a dynamic symbol which requires special
4558 // treatment.  This is how we support equality comparisons of function
4559 // pointers across shared library boundaries, as described in the
4560 // processor specific ABI supplement.
4561
4562 template<bool big_endian>
4563 uint64_t
4564 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
4565 {
4566   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4567   return this->plt_section()->address() + gsym->plt_offset();
4568 }
4569
4570 // Map platform-specific relocs to real relocs
4571 //
4572 template<bool big_endian>
4573 unsigned int
4574 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
4575 {
4576   switch (r_type)
4577     {
4578     case elfcpp::R_ARM_TARGET1:
4579       // This is either R_ARM_ABS32 or R_ARM_REL32;
4580       return elfcpp::R_ARM_ABS32;
4581
4582     case elfcpp::R_ARM_TARGET2:
4583       // This can be any reloc type but ususally is R_ARM_GOT_PREL
4584       return elfcpp::R_ARM_GOT_PREL;
4585
4586     default:
4587       return r_type;
4588     }
4589 }
4590
4591 // The selector for arm object files.
4592
4593 template<bool big_endian>
4594 class Target_selector_arm : public Target_selector
4595 {
4596  public:
4597   Target_selector_arm()
4598     : Target_selector(elfcpp::EM_ARM, 32, big_endian,
4599                       (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
4600   { }
4601
4602   Target*
4603   do_instantiate_target()
4604   { return new Target_arm<big_endian>(); }
4605 };
4606
4607 Target_selector_arm<false> target_selector_arm;
4608 Target_selector_arm<true> target_selector_armbe;
4609
4610 } // End anonymous namespace.