OSDN Git Service

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