* arm.cc (Target_arm<big_endian>::gc_process_relocs): Add template
[external/binutils.git] / gold / arm.cc
1 // arm.cc -- arm target support for gold.
2
3 // Copyright 2009, 2010 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 #include <map>
34 #include <utility>
35 #include <set>
36
37 #include "elfcpp.h"
38 #include "parameters.h"
39 #include "reloc.h"
40 #include "arm.h"
41 #include "object.h"
42 #include "symtab.h"
43 #include "layout.h"
44 #include "output.h"
45 #include "copy-relocs.h"
46 #include "target.h"
47 #include "target-reloc.h"
48 #include "target-select.h"
49 #include "tls.h"
50 #include "defstd.h"
51 #include "gc.h"
52 #include "attributes.h"
53 #include "arm-reloc-property.h"
54
55 namespace
56 {
57
58 using namespace gold;
59
60 template<bool big_endian>
61 class Output_data_plt_arm;
62
63 template<bool big_endian>
64 class Stub_table;
65
66 template<bool big_endian>
67 class Arm_input_section;
68
69 class Arm_exidx_cantunwind;
70
71 class Arm_exidx_merged_section;
72
73 class Arm_exidx_fixup;
74
75 template<bool big_endian>
76 class Arm_output_section;
77
78 class Arm_exidx_input_section;
79
80 template<bool big_endian>
81 class Arm_relobj;
82
83 template<bool big_endian>
84 class Arm_relocate_functions;
85
86 template<bool big_endian>
87 class Arm_output_data_got;
88
89 template<bool big_endian>
90 class Target_arm;
91
92 // For convenience.
93 typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
94
95 // Maximum branch offsets for ARM, THUMB and THUMB2.
96 const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
97 const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
98 const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
99 const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
100 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
101 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
102
103 // Thread Control Block size.
104 const size_t ARM_TCB_SIZE = 8;
105
106 // The arm target class.
107 //
108 // This is a very simple port of gold for ARM-EABI.  It is intended for
109 // supporting Android only for the time being.
110 // 
111 // TODOs:
112 // - Implement all static relocation types documented in arm-reloc.def.
113 // - Make PLTs more flexible for different architecture features like
114 //   Thumb-2 and BE8.
115 // There are probably a lot more.
116
117 // Ideally we would like to avoid using global variables but this is used
118 // very in many places and sometimes in loops.  If we use a function
119 // returning a static instance of Arm_reloc_property_table, it will very
120 // slow in an threaded environment since the static instance needs to be
121 // locked.  The pointer is below initialized in the
122 // Target::do_select_as_default_target() hook so that we do not spend time
123 // building the table if we are not linking ARM objects.
124 //
125 // An alternative is to to process the information in arm-reloc.def in
126 // compilation time and generate a representation of it in PODs only.  That
127 // way we can avoid initialization when the linker starts.
128
129 Arm_reloc_property_table *arm_reloc_property_table = NULL;
130
131 // Instruction template class.  This class is similar to the insn_sequence
132 // struct in bfd/elf32-arm.c.
133
134 class Insn_template
135 {
136  public:
137   // Types of instruction templates.
138   enum Type
139     {
140       THUMB16_TYPE = 1,
141       // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction 
142       // templates with class-specific semantics.  Currently this is used
143       // only by the Cortex_a8_stub class for handling condition codes in
144       // conditional branches.
145       THUMB16_SPECIAL_TYPE,
146       THUMB32_TYPE,
147       ARM_TYPE,
148       DATA_TYPE
149     };
150
151   // Factory methods to create instruction templates in different formats.
152
153   static const Insn_template
154   thumb16_insn(uint32_t data)
155   { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); } 
156
157   // A Thumb conditional branch, in which the proper condition is inserted
158   // when we build the stub.
159   static const Insn_template
160   thumb16_bcond_insn(uint32_t data)
161   { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); } 
162
163   static const Insn_template
164   thumb32_insn(uint32_t data)
165   { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); } 
166
167   static const Insn_template
168   thumb32_b_insn(uint32_t data, int reloc_addend)
169   {
170     return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
171                          reloc_addend);
172   } 
173
174   static const Insn_template
175   arm_insn(uint32_t data)
176   { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
177
178   static const Insn_template
179   arm_rel_insn(unsigned data, int reloc_addend)
180   { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
181
182   static const Insn_template
183   data_word(unsigned data, unsigned int r_type, int reloc_addend)
184   { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); } 
185
186   // Accessors.  This class is used for read-only objects so no modifiers
187   // are provided.
188
189   uint32_t
190   data() const
191   { return this->data_; }
192
193   // Return the instruction sequence type of this.
194   Type
195   type() const
196   { return this->type_; }
197
198   // Return the ARM relocation type of this.
199   unsigned int
200   r_type() const
201   { return this->r_type_; }
202
203   int32_t
204   reloc_addend() const
205   { return this->reloc_addend_; }
206
207   // Return size of instruction template in bytes.
208   size_t
209   size() const;
210
211   // Return byte-alignment of instruction template.
212   unsigned
213   alignment() const;
214
215  private:
216   // We make the constructor private to ensure that only the factory
217   // methods are used.
218   inline
219   Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
220     : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
221   { }
222
223   // Instruction specific data.  This is used to store information like
224   // some of the instruction bits.
225   uint32_t data_;
226   // Instruction template type.
227   Type type_;
228   // Relocation type if there is a relocation or R_ARM_NONE otherwise.
229   unsigned int r_type_;
230   // Relocation addend.
231   int32_t reloc_addend_;
232 };
233
234 // Macro for generating code to stub types. One entry per long/short
235 // branch stub
236
237 #define DEF_STUBS \
238   DEF_STUB(long_branch_any_any) \
239   DEF_STUB(long_branch_v4t_arm_thumb) \
240   DEF_STUB(long_branch_thumb_only) \
241   DEF_STUB(long_branch_v4t_thumb_thumb) \
242   DEF_STUB(long_branch_v4t_thumb_arm) \
243   DEF_STUB(short_branch_v4t_thumb_arm) \
244   DEF_STUB(long_branch_any_arm_pic) \
245   DEF_STUB(long_branch_any_thumb_pic) \
246   DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
247   DEF_STUB(long_branch_v4t_arm_thumb_pic) \
248   DEF_STUB(long_branch_v4t_thumb_arm_pic) \
249   DEF_STUB(long_branch_thumb_only_pic) \
250   DEF_STUB(a8_veneer_b_cond) \
251   DEF_STUB(a8_veneer_b) \
252   DEF_STUB(a8_veneer_bl) \
253   DEF_STUB(a8_veneer_blx) \
254   DEF_STUB(v4_veneer_bx)
255
256 // Stub types.
257
258 #define DEF_STUB(x) arm_stub_##x,
259 typedef enum
260   {
261     arm_stub_none,
262     DEF_STUBS
263
264     // First reloc stub type.
265     arm_stub_reloc_first = arm_stub_long_branch_any_any,
266     // Last  reloc stub type.
267     arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
268
269     // First Cortex-A8 stub type.
270     arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
271     // Last Cortex-A8 stub type.
272     arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
273     
274     // Last stub type.
275     arm_stub_type_last = arm_stub_v4_veneer_bx
276   } Stub_type;
277 #undef DEF_STUB
278
279 // Stub template class.  Templates are meant to be read-only objects.
280 // A stub template for a stub type contains all read-only attributes
281 // common to all stubs of the same type.
282
283 class Stub_template
284 {
285  public:
286   Stub_template(Stub_type, const Insn_template*, size_t);
287
288   ~Stub_template()
289   { }
290
291   // Return stub type.
292   Stub_type
293   type() const
294   { return this->type_; }
295
296   // Return an array of instruction templates.
297   const Insn_template*
298   insns() const
299   { return this->insns_; }
300
301   // Return size of template in number of instructions.
302   size_t
303   insn_count() const
304   { return this->insn_count_; }
305
306   // Return size of template in bytes.
307   size_t
308   size() const
309   { return this->size_; }
310
311   // Return alignment of the stub template.
312   unsigned
313   alignment() const
314   { return this->alignment_; }
315   
316   // Return whether entry point is in thumb mode.
317   bool
318   entry_in_thumb_mode() const
319   { return this->entry_in_thumb_mode_; }
320
321   // Return number of relocations in this template.
322   size_t
323   reloc_count() const
324   { return this->relocs_.size(); }
325
326   // Return index of the I-th instruction with relocation.
327   size_t
328   reloc_insn_index(size_t i) const
329   {
330     gold_assert(i < this->relocs_.size());
331     return this->relocs_[i].first;
332   }
333
334   // Return the offset of the I-th instruction with relocation from the
335   // beginning of the stub.
336   section_size_type
337   reloc_offset(size_t i) const
338   {
339     gold_assert(i < this->relocs_.size());
340     return this->relocs_[i].second;
341   }
342
343  private:
344   // This contains information about an instruction template with a relocation
345   // and its offset from start of stub.
346   typedef std::pair<size_t, section_size_type> Reloc;
347
348   // A Stub_template may not be copied.  We want to share templates as much
349   // as possible.
350   Stub_template(const Stub_template&);
351   Stub_template& operator=(const Stub_template&);
352   
353   // Stub type.
354   Stub_type type_;
355   // Points to an array of Insn_templates.
356   const Insn_template* insns_;
357   // Number of Insn_templates in insns_[].
358   size_t insn_count_;
359   // Size of templated instructions in bytes.
360   size_t size_;
361   // Alignment of templated instructions.
362   unsigned alignment_;
363   // Flag to indicate if entry is in thumb mode.
364   bool entry_in_thumb_mode_;
365   // A table of reloc instruction indices and offsets.  We can find these by
366   // looking at the instruction templates but we pre-compute and then stash
367   // them here for speed. 
368   std::vector<Reloc> relocs_;
369 };
370
371 //
372 // A class for code stubs.  This is a base class for different type of
373 // stubs used in the ARM target.
374 //
375
376 class Stub
377 {
378  private:
379   static const section_offset_type invalid_offset =
380     static_cast<section_offset_type>(-1);
381
382  public:
383   Stub(const Stub_template* stub_template)
384     : stub_template_(stub_template), offset_(invalid_offset)
385   { }
386
387   virtual
388    ~Stub()
389   { }
390
391   // Return the stub template.
392   const Stub_template*
393   stub_template() const
394   { return this->stub_template_; }
395
396   // Return offset of code stub from beginning of its containing stub table.
397   section_offset_type
398   offset() const
399   {
400     gold_assert(this->offset_ != invalid_offset);
401     return this->offset_;
402   }
403
404   // Set offset of code stub from beginning of its containing stub table.
405   void
406   set_offset(section_offset_type offset)
407   { this->offset_ = offset; }
408   
409   // Return the relocation target address of the i-th relocation in the
410   // stub.  This must be defined in a child class.
411   Arm_address
412   reloc_target(size_t i)
413   { return this->do_reloc_target(i); }
414
415   // Write a stub at output VIEW.  BIG_ENDIAN select how a stub is written.
416   void
417   write(unsigned char* view, section_size_type view_size, bool big_endian)
418   { this->do_write(view, view_size, big_endian); }
419
420   // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
421   // for the i-th instruction.
422   uint16_t
423   thumb16_special(size_t i)
424   { return this->do_thumb16_special(i); }
425
426  protected:
427   // This must be defined in the child class.
428   virtual Arm_address
429   do_reloc_target(size_t) = 0;
430
431   // This may be overridden in the child class.
432   virtual void
433   do_write(unsigned char* view, section_size_type view_size, bool big_endian)
434   {
435     if (big_endian)
436       this->do_fixed_endian_write<true>(view, view_size);
437     else
438       this->do_fixed_endian_write<false>(view, view_size);
439   }
440   
441   // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
442   // instruction template.
443   virtual uint16_t
444   do_thumb16_special(size_t)
445   { gold_unreachable(); }
446
447  private:
448   // A template to implement do_write.
449   template<bool big_endian>
450   void inline
451   do_fixed_endian_write(unsigned char*, section_size_type);
452
453   // Its template.
454   const Stub_template* stub_template_;
455   // Offset within the section of containing this stub.
456   section_offset_type offset_;
457 };
458
459 // Reloc stub class.  These are stubs we use to fix up relocation because
460 // of limited branch ranges.
461
462 class Reloc_stub : public Stub
463 {
464  public:
465   static const unsigned int invalid_index = static_cast<unsigned int>(-1);
466   // We assume we never jump to this address.
467   static const Arm_address invalid_address = static_cast<Arm_address>(-1);
468
469   // Return destination address.
470   Arm_address
471   destination_address() const
472   {
473     gold_assert(this->destination_address_ != this->invalid_address);
474     return this->destination_address_;
475   }
476
477   // Set destination address.
478   void
479   set_destination_address(Arm_address address)
480   {
481     gold_assert(address != this->invalid_address);
482     this->destination_address_ = address;
483   }
484
485   // Reset destination address.
486   void
487   reset_destination_address()
488   { this->destination_address_ = this->invalid_address; }
489
490   // Determine stub type for a branch of a relocation of R_TYPE going
491   // from BRANCH_ADDRESS to BRANCH_TARGET.  If TARGET_IS_THUMB is set,
492   // the branch target is a thumb instruction.  TARGET is used for look
493   // up ARM-specific linker settings.
494   static Stub_type
495   stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
496                       Arm_address branch_target, bool target_is_thumb);
497
498   // Reloc_stub key.  A key is logically a triplet of a stub type, a symbol
499   // and an addend.  Since we treat global and local symbol differently, we
500   // use a Symbol object for a global symbol and a object-index pair for
501   // a local symbol.
502   class Key
503   {
504    public:
505     // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
506     // R_SYM.  Otherwise, this is a local symbol and RELOBJ must non-NULL
507     // and R_SYM must not be invalid_index.
508     Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
509         unsigned int r_sym, int32_t addend)
510       : stub_type_(stub_type), addend_(addend)
511     {
512       if (symbol != NULL)
513         {
514           this->r_sym_ = Reloc_stub::invalid_index;
515           this->u_.symbol = symbol;
516         }
517       else
518         {
519           gold_assert(relobj != NULL && r_sym != invalid_index);
520           this->r_sym_ = r_sym;
521           this->u_.relobj = relobj;
522         }
523     }
524
525     ~Key()
526     { }
527
528     // Accessors: Keys are meant to be read-only object so no modifiers are
529     // provided.
530
531     // Return stub type.
532     Stub_type
533     stub_type() const
534     { return this->stub_type_; }
535
536     // Return the local symbol index or invalid_index.
537     unsigned int
538     r_sym() const
539     { return this->r_sym_; }
540
541     // Return the symbol if there is one.
542     const Symbol*
543     symbol() const
544     { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
545
546     // Return the relobj if there is one.
547     const Relobj*
548     relobj() const
549     { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
550
551     // Whether this equals to another key k.
552     bool
553     eq(const Key& k) const 
554     {
555       return ((this->stub_type_ == k.stub_type_)
556               && (this->r_sym_ == k.r_sym_)
557               && ((this->r_sym_ != Reloc_stub::invalid_index)
558                   ? (this->u_.relobj == k.u_.relobj)
559                   : (this->u_.symbol == k.u_.symbol))
560               && (this->addend_ == k.addend_));
561     }
562
563     // Return a hash value.
564     size_t
565     hash_value() const
566     {
567       return (this->stub_type_
568               ^ this->r_sym_
569               ^ gold::string_hash<char>(
570                     (this->r_sym_ != Reloc_stub::invalid_index)
571                     ? this->u_.relobj->name().c_str()
572                     : this->u_.symbol->name())
573               ^ this->addend_);
574     }
575
576     // Functors for STL associative containers.
577     struct hash
578     {
579       size_t
580       operator()(const Key& k) const
581       { return k.hash_value(); }
582     };
583
584     struct equal_to
585     {
586       bool
587       operator()(const Key& k1, const Key& k2) const
588       { return k1.eq(k2); }
589     };
590
591     // Name of key.  This is mainly for debugging.
592     std::string
593     name() const;
594
595    private:
596     // Stub type.
597     Stub_type stub_type_;
598     // If this is a local symbol, this is the index in the defining object.
599     // Otherwise, it is invalid_index for a global symbol.
600     unsigned int r_sym_;
601     // If r_sym_ is invalid index.  This points to a global symbol.
602     // Otherwise, this points a relobj.  We used the unsized and target
603     // independent Symbol and Relobj classes instead of Sized_symbol<32> and  
604     // Arm_relobj.  This is done to avoid making the stub class a template
605     // as most of the stub machinery is endianness-neutral.  However, it
606     // may require a bit of casting done by users of this class.
607     union
608     {
609       const Symbol* symbol;
610       const Relobj* relobj;
611     } u_;
612     // Addend associated with a reloc.
613     int32_t addend_;
614   };
615
616  protected:
617   // Reloc_stubs are created via a stub factory.  So these are protected.
618   Reloc_stub(const Stub_template* stub_template)
619     : Stub(stub_template), destination_address_(invalid_address)
620   { }
621
622   ~Reloc_stub()
623   { }
624
625   friend class Stub_factory;
626
627   // Return the relocation target address of the i-th relocation in the
628   // stub.
629   Arm_address
630   do_reloc_target(size_t i)
631   {
632     // All reloc stub have only one relocation.
633     gold_assert(i == 0);
634     return this->destination_address_;
635   }
636
637  private:
638   // Address of destination.
639   Arm_address destination_address_;
640 };
641
642 // Cortex-A8 stub class.  We need a Cortex-A8 stub to redirect any 32-bit
643 // THUMB branch that meets the following conditions:
644 // 
645 // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
646 //    branch address is 0xffe.
647 // 2. The branch target address is in the same page as the first word of the
648 //    branch.
649 // 3. The branch follows a 32-bit instruction which is not a branch.
650 //
651 // To do the fix up, we need to store the address of the branch instruction
652 // and its target at least.  We also need to store the original branch
653 // instruction bits for the condition code in a conditional branch.  The
654 // condition code is used in a special instruction template.  We also want
655 // to identify input sections needing Cortex-A8 workaround quickly.  We store
656 // extra information about object and section index of the code section
657 // containing a branch being fixed up.  The information is used to mark
658 // the code section when we finalize the Cortex-A8 stubs.
659 //
660
661 class Cortex_a8_stub : public Stub
662 {
663  public:
664   ~Cortex_a8_stub()
665   { }
666
667   // Return the object of the code section containing the branch being fixed
668   // up.
669   Relobj*
670   relobj() const
671   { return this->relobj_; }
672
673   // Return the section index of the code section containing the branch being
674   // fixed up.
675   unsigned int
676   shndx() const
677   { return this->shndx_; }
678
679   // Return the source address of stub.  This is the address of the original
680   // branch instruction.  LSB is 1 always set to indicate that it is a THUMB
681   // instruction.
682   Arm_address
683   source_address() const
684   { return this->source_address_; }
685
686   // Return the destination address of the stub.  This is the branch taken
687   // address of the original branch instruction.  LSB is 1 if it is a THUMB
688   // instruction address.
689   Arm_address
690   destination_address() const
691   { return this->destination_address_; }
692
693   // Return the instruction being fixed up.
694   uint32_t
695   original_insn() const
696   { return this->original_insn_; }
697
698  protected:
699   // Cortex_a8_stubs are created via a stub factory.  So these are protected.
700   Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
701                  unsigned int shndx, Arm_address source_address,
702                  Arm_address destination_address, uint32_t original_insn)
703     : Stub(stub_template), relobj_(relobj), shndx_(shndx),
704       source_address_(source_address | 1U),
705       destination_address_(destination_address),
706       original_insn_(original_insn)
707   { }
708
709   friend class Stub_factory;
710
711   // Return the relocation target address of the i-th relocation in the
712   // stub.
713   Arm_address
714   do_reloc_target(size_t i)
715   {
716     if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
717       {
718         // The conditional branch veneer has two relocations.
719         gold_assert(i < 2);
720         return i == 0 ? this->source_address_ + 4 : this->destination_address_;
721       }
722     else
723       {
724         // All other Cortex-A8 stubs have only one relocation.
725         gold_assert(i == 0);
726         return this->destination_address_;
727       }
728   }
729
730   // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
731   uint16_t
732   do_thumb16_special(size_t);
733
734  private:
735   // Object of the code section containing the branch being fixed up.
736   Relobj* relobj_;
737   // Section index of the code section containing the branch begin fixed up.
738   unsigned int shndx_;
739   // Source address of original branch.
740   Arm_address source_address_;
741   // Destination address of the original branch.
742   Arm_address destination_address_;
743   // Original branch instruction.  This is needed for copying the condition
744   // code from a condition branch to its stub.
745   uint32_t original_insn_;
746 };
747
748 // ARMv4 BX Rx branch relocation stub class.
749 class Arm_v4bx_stub : public Stub
750 {
751  public:
752   ~Arm_v4bx_stub()
753   { }
754
755   // Return the associated register.
756   uint32_t
757   reg() const
758   { return this->reg_; }
759
760  protected:
761   // Arm V4BX stubs are created via a stub factory.  So these are protected.
762   Arm_v4bx_stub(const Stub_template* stub_template, const uint32_t reg)
763     : Stub(stub_template), reg_(reg)
764   { }
765
766   friend class Stub_factory;
767
768   // Return the relocation target address of the i-th relocation in the
769   // stub.
770   Arm_address
771   do_reloc_target(size_t)
772   { gold_unreachable(); }
773
774   // This may be overridden in the child class.
775   virtual void
776   do_write(unsigned char* view, section_size_type view_size, bool big_endian)
777   {
778     if (big_endian)
779       this->do_fixed_endian_v4bx_write<true>(view, view_size);
780     else
781       this->do_fixed_endian_v4bx_write<false>(view, view_size);
782   }
783
784  private:
785   // A template to implement do_write.
786   template<bool big_endian>
787   void inline
788   do_fixed_endian_v4bx_write(unsigned char* view, section_size_type)
789   {
790     const Insn_template* insns = this->stub_template()->insns();
791     elfcpp::Swap<32, big_endian>::writeval(view,
792                                            (insns[0].data()
793                                            + (this->reg_ << 16)));
794     view += insns[0].size();
795     elfcpp::Swap<32, big_endian>::writeval(view,
796                                            (insns[1].data() + this->reg_));
797     view += insns[1].size();
798     elfcpp::Swap<32, big_endian>::writeval(view,
799                                            (insns[2].data() + this->reg_));
800   }
801
802   // A register index (r0-r14), which is associated with the stub.
803   uint32_t reg_;
804 };
805
806 // Stub factory class.
807
808 class Stub_factory
809 {
810  public:
811   // Return the unique instance of this class.
812   static const Stub_factory&
813   get_instance()
814   {
815     static Stub_factory singleton;
816     return singleton;
817   }
818
819   // Make a relocation stub.
820   Reloc_stub*
821   make_reloc_stub(Stub_type stub_type) const
822   {
823     gold_assert(stub_type >= arm_stub_reloc_first
824                 && stub_type <= arm_stub_reloc_last);
825     return new Reloc_stub(this->stub_templates_[stub_type]);
826   }
827
828   // Make a Cortex-A8 stub.
829   Cortex_a8_stub*
830   make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
831                       Arm_address source, Arm_address destination,
832                       uint32_t original_insn) const
833   {
834     gold_assert(stub_type >= arm_stub_cortex_a8_first
835                 && stub_type <= arm_stub_cortex_a8_last);
836     return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
837                               source, destination, original_insn);
838   }
839
840   // Make an ARM V4BX relocation stub.
841   // This method creates a stub from the arm_stub_v4_veneer_bx template only.
842   Arm_v4bx_stub*
843   make_arm_v4bx_stub(uint32_t reg) const
844   {
845     gold_assert(reg < 0xf);
846     return new Arm_v4bx_stub(this->stub_templates_[arm_stub_v4_veneer_bx],
847                              reg);
848   }
849
850  private:
851   // Constructor and destructor are protected since we only return a single
852   // instance created in Stub_factory::get_instance().
853   
854   Stub_factory();
855
856   // A Stub_factory may not be copied since it is a singleton.
857   Stub_factory(const Stub_factory&);
858   Stub_factory& operator=(Stub_factory&);
859   
860   // Stub templates.  These are initialized in the constructor.
861   const Stub_template* stub_templates_[arm_stub_type_last+1];
862 };
863
864 // A class to hold stubs for the ARM target.
865
866 template<bool big_endian>
867 class Stub_table : public Output_data
868 {
869  public:
870   Stub_table(Arm_input_section<big_endian>* owner)
871     : Output_data(), owner_(owner), reloc_stubs_(), reloc_stubs_size_(0),
872       reloc_stubs_addralign_(1), cortex_a8_stubs_(), arm_v4bx_stubs_(0xf),
873       prev_data_size_(0), prev_addralign_(1)
874   { }
875
876   ~Stub_table()
877   { }
878
879   // Owner of this stub table.
880   Arm_input_section<big_endian>*
881   owner() const
882   { return this->owner_; }
883
884   // Whether this stub table is empty.
885   bool
886   empty() const
887   {
888     return (this->reloc_stubs_.empty()
889             && this->cortex_a8_stubs_.empty()
890             && this->arm_v4bx_stubs_.empty());
891   }
892
893   // Return the current data size.
894   off_t
895   current_data_size() const
896   { return this->current_data_size_for_child(); }
897
898   // Add a STUB with using KEY.  Caller is reponsible for avoid adding
899   // if already a STUB with the same key has been added. 
900   void
901   add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
902   {
903     const Stub_template* stub_template = stub->stub_template();
904     gold_assert(stub_template->type() == key.stub_type());
905     this->reloc_stubs_[key] = stub;
906
907     // Assign stub offset early.  We can do this because we never remove
908     // reloc stubs and they are in the beginning of the stub table.
909     uint64_t align = stub_template->alignment();
910     this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_, align);
911     stub->set_offset(this->reloc_stubs_size_);
912     this->reloc_stubs_size_ += stub_template->size();
913     this->reloc_stubs_addralign_ =
914       std::max(this->reloc_stubs_addralign_, align);
915   }
916
917   // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
918   // Caller is reponsible for avoid adding if already a STUB with the same
919   // address has been added. 
920   void
921   add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
922   {
923     std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
924     this->cortex_a8_stubs_.insert(value);
925   }
926
927   // Add an ARM V4BX relocation stub. A register index will be retrieved
928   // from the stub.
929   void
930   add_arm_v4bx_stub(Arm_v4bx_stub* stub)
931   {
932     gold_assert(stub != NULL && this->arm_v4bx_stubs_[stub->reg()] == NULL);
933     this->arm_v4bx_stubs_[stub->reg()] = stub;
934   }
935
936   // Remove all Cortex-A8 stubs.
937   void
938   remove_all_cortex_a8_stubs();
939
940   // Look up a relocation stub using KEY.  Return NULL if there is none.
941   Reloc_stub*
942   find_reloc_stub(const Reloc_stub::Key& key) const
943   {
944     typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
945     return (p != this->reloc_stubs_.end()) ? p->second : NULL;
946   }
947
948   // Look up an arm v4bx relocation stub using the register index.
949   // Return NULL if there is none.
950   Arm_v4bx_stub*
951   find_arm_v4bx_stub(const uint32_t reg) const
952   {
953     gold_assert(reg < 0xf);
954     return this->arm_v4bx_stubs_[reg];
955   }
956
957   // Relocate stubs in this stub table.
958   void
959   relocate_stubs(const Relocate_info<32, big_endian>*,
960                  Target_arm<big_endian>*, Output_section*,
961                  unsigned char*, Arm_address, section_size_type);
962
963   // Update data size and alignment at the end of a relaxation pass.  Return
964   // true if either data size or alignment is different from that of the
965   // previous relaxation pass.
966   bool
967   update_data_size_and_addralign();
968
969   // Finalize stubs.  Set the offsets of all stubs and mark input sections
970   // needing the Cortex-A8 workaround.
971   void
972   finalize_stubs();
973   
974   // Apply Cortex-A8 workaround to an address range.
975   void
976   apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
977                                               unsigned char*, Arm_address,
978                                               section_size_type);
979
980  protected:
981   // Write out section contents.
982   void
983   do_write(Output_file*);
984  
985   // Return the required alignment.
986   uint64_t
987   do_addralign() const
988   { return this->prev_addralign_; }
989
990   // Reset address and file offset.
991   void
992   do_reset_address_and_file_offset()
993   { this->set_current_data_size_for_child(this->prev_data_size_); }
994
995   // Set final data size.
996   void
997   set_final_data_size()
998   { this->set_data_size(this->current_data_size()); }
999   
1000  private:
1001   // Relocate one stub.
1002   void
1003   relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
1004                 Target_arm<big_endian>*, Output_section*,
1005                 unsigned char*, Arm_address, section_size_type);
1006
1007   // Unordered map of relocation stubs.
1008   typedef
1009     Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
1010                   Reloc_stub::Key::equal_to>
1011     Reloc_stub_map;
1012
1013   // List of Cortex-A8 stubs ordered by addresses of branches being
1014   // fixed up in output.
1015   typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
1016   // List of Arm V4BX relocation stubs ordered by associated registers.
1017   typedef std::vector<Arm_v4bx_stub*> Arm_v4bx_stub_list;
1018
1019   // Owner of this stub table.
1020   Arm_input_section<big_endian>* owner_;
1021   // The relocation stubs.
1022   Reloc_stub_map reloc_stubs_;
1023   // Size of reloc stubs.
1024   off_t reloc_stubs_size_;
1025   // Maximum address alignment of reloc stubs.
1026   uint64_t reloc_stubs_addralign_;
1027   // The cortex_a8_stubs.
1028   Cortex_a8_stub_list cortex_a8_stubs_;
1029   // The Arm V4BX relocation stubs.
1030   Arm_v4bx_stub_list arm_v4bx_stubs_;
1031   // data size of this in the previous pass.
1032   off_t prev_data_size_;
1033   // address alignment of this in the previous pass.
1034   uint64_t prev_addralign_;
1035 };
1036
1037 // Arm_exidx_cantunwind class.  This represents an EXIDX_CANTUNWIND entry
1038 // we add to the end of an EXIDX input section that goes into the output.
1039
1040 class Arm_exidx_cantunwind : public Output_section_data
1041 {
1042  public:
1043   Arm_exidx_cantunwind(Relobj* relobj, unsigned int shndx)
1044     : Output_section_data(8, 4, true), relobj_(relobj), shndx_(shndx)
1045   { }
1046
1047   // Return the object containing the section pointed by this.
1048   Relobj*
1049   relobj() const
1050   { return this->relobj_; }
1051
1052   // Return the section index of the section pointed by this.
1053   unsigned int
1054   shndx() const
1055   { return this->shndx_; }
1056
1057  protected:
1058   void
1059   do_write(Output_file* of)
1060   {
1061     if (parameters->target().is_big_endian())
1062       this->do_fixed_endian_write<true>(of);
1063     else
1064       this->do_fixed_endian_write<false>(of);
1065   }
1066
1067  private:
1068   // Implement do_write for a given endianness.
1069   template<bool big_endian>
1070   void inline
1071   do_fixed_endian_write(Output_file*);
1072   
1073   // The object containing the section pointed by this.
1074   Relobj* relobj_;
1075   // The section index of the section pointed by this.
1076   unsigned int shndx_;
1077 };
1078
1079 // During EXIDX coverage fix-up, we compact an EXIDX section.  The
1080 // Offset map is used to map input section offset within the EXIDX section
1081 // to the output offset from the start of this EXIDX section. 
1082
1083 typedef std::map<section_offset_type, section_offset_type>
1084         Arm_exidx_section_offset_map;
1085
1086 // Arm_exidx_merged_section class.  This represents an EXIDX input section
1087 // with some of its entries merged.
1088
1089 class Arm_exidx_merged_section : public Output_relaxed_input_section
1090 {
1091  public:
1092   // Constructor for Arm_exidx_merged_section.
1093   // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
1094   // SECTION_OFFSET_MAP points to a section offset map describing how
1095   // parts of the input section are mapped to output.  DELETED_BYTES is
1096   // the number of bytes deleted from the EXIDX input section.
1097   Arm_exidx_merged_section(
1098       const Arm_exidx_input_section& exidx_input_section,
1099       const Arm_exidx_section_offset_map& section_offset_map,
1100       uint32_t deleted_bytes);
1101
1102   // Return the original EXIDX input section.
1103   const Arm_exidx_input_section&
1104   exidx_input_section() const
1105   { return this->exidx_input_section_; }
1106
1107   // Return the section offset map.
1108   const Arm_exidx_section_offset_map&
1109   section_offset_map() const
1110   { return this->section_offset_map_; }
1111
1112  protected:
1113   // Write merged section into file OF.
1114   void
1115   do_write(Output_file* of);
1116
1117   bool
1118   do_output_offset(const Relobj*, unsigned int, section_offset_type,
1119                   section_offset_type*) const;
1120
1121  private:
1122   // Original EXIDX input section.
1123   const Arm_exidx_input_section& exidx_input_section_;
1124   // Section offset map.
1125   const Arm_exidx_section_offset_map& section_offset_map_;
1126 };
1127
1128 // A class to wrap an ordinary input section containing executable code.
1129
1130 template<bool big_endian>
1131 class Arm_input_section : public Output_relaxed_input_section
1132 {
1133  public:
1134   Arm_input_section(Relobj* relobj, unsigned int shndx)
1135     : Output_relaxed_input_section(relobj, shndx, 1),
1136       original_addralign_(1), original_size_(0), stub_table_(NULL)
1137   { }
1138
1139   ~Arm_input_section()
1140   { }
1141
1142   // Initialize.
1143   void
1144   init();
1145   
1146   // Whether this is a stub table owner.
1147   bool
1148   is_stub_table_owner() const
1149   { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1150
1151   // Return the stub table.
1152   Stub_table<big_endian>*
1153   stub_table() const
1154   { return this->stub_table_; }
1155
1156   // Set the stub_table.
1157   void
1158   set_stub_table(Stub_table<big_endian>* stub_table)
1159   { this->stub_table_ = stub_table; }
1160
1161   // Downcast a base pointer to an Arm_input_section pointer.  This is
1162   // not type-safe but we only use Arm_input_section not the base class.
1163   static Arm_input_section<big_endian>*
1164   as_arm_input_section(Output_relaxed_input_section* poris)
1165   { return static_cast<Arm_input_section<big_endian>*>(poris); }
1166
1167   // Return the original size of the section.
1168   uint32_t
1169   original_size() const
1170   { return this->original_size_; }
1171
1172  protected:
1173   // Write data to output file.
1174   void
1175   do_write(Output_file*);
1176
1177   // Return required alignment of this.
1178   uint64_t
1179   do_addralign() const
1180   {
1181     if (this->is_stub_table_owner())
1182       return std::max(this->stub_table_->addralign(),
1183                       static_cast<uint64_t>(this->original_addralign_));
1184     else
1185       return this->original_addralign_;
1186   }
1187
1188   // Finalize data size.
1189   void
1190   set_final_data_size();
1191
1192   // Reset address and file offset.
1193   void
1194   do_reset_address_and_file_offset();
1195
1196   // Output offset.
1197   bool
1198   do_output_offset(const Relobj* object, unsigned int shndx,
1199                    section_offset_type offset,
1200                    section_offset_type* poutput) const
1201   {
1202     if ((object == this->relobj())
1203         && (shndx == this->shndx())
1204         && (offset >= 0)
1205         && (offset <=
1206             convert_types<section_offset_type, uint32_t>(this->original_size_)))
1207       {
1208         *poutput = offset;
1209         return true;
1210       }
1211     else
1212       return false;
1213   }
1214
1215  private:
1216   // Copying is not allowed.
1217   Arm_input_section(const Arm_input_section&);
1218   Arm_input_section& operator=(const Arm_input_section&);
1219
1220   // Address alignment of the original input section.
1221   uint32_t original_addralign_;
1222   // Section size of the original input section.
1223   uint32_t original_size_;
1224   // Stub table.
1225   Stub_table<big_endian>* stub_table_;
1226 };
1227
1228 // Arm_exidx_fixup class.  This is used to define a number of methods
1229 // and keep states for fixing up EXIDX coverage.
1230
1231 class Arm_exidx_fixup
1232 {
1233  public:
1234   Arm_exidx_fixup(Output_section* exidx_output_section,
1235                   bool merge_exidx_entries = true)
1236     : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
1237       last_inlined_entry_(0), last_input_section_(NULL),
1238       section_offset_map_(NULL), first_output_text_section_(NULL),
1239       merge_exidx_entries_(merge_exidx_entries)
1240   { }
1241
1242   ~Arm_exidx_fixup()
1243   { delete this->section_offset_map_; }
1244
1245   // Process an EXIDX section for entry merging.  Return  number of bytes to
1246   // be deleted in output.  If parts of the input EXIDX section are merged
1247   // a heap allocated Arm_exidx_section_offset_map is store in the located
1248   // PSECTION_OFFSET_MAP.  The caller owns the map and is reponsible for
1249   // releasing it.
1250   template<bool big_endian>
1251   uint32_t
1252   process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
1253                         Arm_exidx_section_offset_map** psection_offset_map);
1254   
1255   // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
1256   // input section, if there is not one already.
1257   void
1258   add_exidx_cantunwind_as_needed();
1259
1260   // Return the output section for the text section which is linked to the
1261   // first exidx input in output.
1262   Output_section*
1263   first_output_text_section() const
1264   { return this->first_output_text_section_; }
1265
1266  private:
1267   // Copying is not allowed.
1268   Arm_exidx_fixup(const Arm_exidx_fixup&);
1269   Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
1270
1271   // Type of EXIDX unwind entry.
1272   enum Unwind_type
1273   {
1274     // No type.
1275     UT_NONE,
1276     // EXIDX_CANTUNWIND.
1277     UT_EXIDX_CANTUNWIND,
1278     // Inlined entry.
1279     UT_INLINED_ENTRY,
1280     // Normal entry.
1281     UT_NORMAL_ENTRY,
1282   };
1283
1284   // Process an EXIDX entry.  We only care about the second word of the
1285   // entry.  Return true if the entry can be deleted.
1286   bool
1287   process_exidx_entry(uint32_t second_word);
1288
1289   // Update the current section offset map during EXIDX section fix-up.
1290   // If there is no map, create one.  INPUT_OFFSET is the offset of a
1291   // reference point, DELETED_BYTES is the number of deleted by in the
1292   // section so far.  If DELETE_ENTRY is true, the reference point and
1293   // all offsets after the previous reference point are discarded.
1294   void
1295   update_offset_map(section_offset_type input_offset,
1296                     section_size_type deleted_bytes, bool delete_entry);
1297
1298   // EXIDX output section.
1299   Output_section* exidx_output_section_;
1300   // Unwind type of the last EXIDX entry processed.
1301   Unwind_type last_unwind_type_;
1302   // Last seen inlined EXIDX entry.
1303   uint32_t last_inlined_entry_;
1304   // Last processed EXIDX input section.
1305   const Arm_exidx_input_section* last_input_section_;
1306   // Section offset map created in process_exidx_section.
1307   Arm_exidx_section_offset_map* section_offset_map_;
1308   // Output section for the text section which is linked to the first exidx
1309   // input in output.
1310   Output_section* first_output_text_section_;
1311
1312   bool merge_exidx_entries_;
1313 };
1314
1315 // Arm output section class.  This is defined mainly to add a number of
1316 // stub generation methods.
1317
1318 template<bool big_endian>
1319 class Arm_output_section : public Output_section
1320 {
1321  public:
1322   typedef std::vector<std::pair<Relobj*, unsigned int> > Text_section_list;
1323
1324   Arm_output_section(const char* name, elfcpp::Elf_Word type,
1325                      elfcpp::Elf_Xword flags)
1326     : Output_section(name, type, flags)
1327   {
1328     if (type == elfcpp::SHT_ARM_EXIDX)
1329       this->set_always_keeps_input_sections();
1330   }
1331
1332   ~Arm_output_section()
1333   { }
1334   
1335   // Group input sections for stub generation.
1336   void
1337   group_sections(section_size_type, bool, Target_arm<big_endian>*);
1338
1339   // Downcast a base pointer to an Arm_output_section pointer.  This is
1340   // not type-safe but we only use Arm_output_section not the base class.
1341   static Arm_output_section<big_endian>*
1342   as_arm_output_section(Output_section* os)
1343   { return static_cast<Arm_output_section<big_endian>*>(os); }
1344
1345   // Append all input text sections in this into LIST.
1346   void
1347   append_text_sections_to_list(Text_section_list* list);
1348
1349   // Fix EXIDX coverage of this EXIDX output section.  SORTED_TEXT_SECTION
1350   // is a list of text input sections sorted in ascending order of their
1351   // output addresses.
1352   void
1353   fix_exidx_coverage(Layout* layout,
1354                      const Text_section_list& sorted_text_section,
1355                      Symbol_table* symtab,
1356                      bool merge_exidx_entries);
1357
1358   // Link an EXIDX section into its corresponding text section.
1359   void
1360   set_exidx_section_link();
1361
1362  private:
1363   // For convenience.
1364   typedef Output_section::Input_section Input_section;
1365   typedef Output_section::Input_section_list Input_section_list;
1366
1367   // Create a stub group.
1368   void create_stub_group(Input_section_list::const_iterator,
1369                          Input_section_list::const_iterator,
1370                          Input_section_list::const_iterator,
1371                          Target_arm<big_endian>*,
1372                          std::vector<Output_relaxed_input_section*>*);
1373 };
1374
1375 // Arm_exidx_input_section class.  This represents an EXIDX input section.
1376
1377 class Arm_exidx_input_section
1378 {
1379  public:
1380   static const section_offset_type invalid_offset =
1381     static_cast<section_offset_type>(-1);
1382
1383   Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
1384                           unsigned int link, uint32_t size, uint32_t addralign)
1385     : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
1386       addralign_(addralign), has_errors_(false)
1387   { }
1388
1389   ~Arm_exidx_input_section()
1390   { }
1391         
1392   // Accessors:  This is a read-only class.
1393
1394   // Return the object containing this EXIDX input section.
1395   Relobj*
1396   relobj() const
1397   { return this->relobj_; }
1398
1399   // Return the section index of this EXIDX input section.
1400   unsigned int
1401   shndx() const
1402   { return this->shndx_; }
1403
1404   // Return the section index of linked text section in the same object.
1405   unsigned int
1406   link() const
1407   { return this->link_; }
1408
1409   // Return size of the EXIDX input section.
1410   uint32_t
1411   size() const
1412   { return this->size_; }
1413
1414   // Reutnr address alignment of EXIDX input section.
1415   uint32_t
1416   addralign() const
1417   { return this->addralign_; }
1418
1419   // Whether there are any errors in the EXIDX input section.
1420   bool
1421   has_errors() const
1422   { return this->has_errors_; }
1423
1424   // Set has-errors flag.
1425   void
1426   set_has_errors()
1427   { this->has_errors_ = true; }
1428
1429  private:
1430   // Object containing this.
1431   Relobj* relobj_;
1432   // Section index of this.
1433   unsigned int shndx_;
1434   // text section linked to this in the same object.
1435   unsigned int link_;
1436   // Size of this.  For ARM 32-bit is sufficient.
1437   uint32_t size_;
1438   // Address alignment of this.  For ARM 32-bit is sufficient.
1439   uint32_t addralign_;
1440   // Whether this has any errors.
1441   bool has_errors_;
1442 };
1443
1444 // Arm_relobj class.
1445
1446 template<bool big_endian>
1447 class Arm_relobj : public Sized_relobj<32, big_endian>
1448 {
1449  public:
1450   static const Arm_address invalid_address = static_cast<Arm_address>(-1);
1451
1452   Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
1453              const typename elfcpp::Ehdr<32, big_endian>& ehdr)
1454     : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
1455       stub_tables_(), local_symbol_is_thumb_function_(),
1456       attributes_section_data_(NULL), mapping_symbols_info_(),
1457       section_has_cortex_a8_workaround_(NULL), exidx_section_map_(),
1458       output_local_symbol_count_needs_update_(false),
1459       merge_flags_and_attributes_(true)
1460   { }
1461
1462   ~Arm_relobj()
1463   { delete this->attributes_section_data_; }
1464  
1465   // Return the stub table of the SHNDX-th section if there is one.
1466   Stub_table<big_endian>*
1467   stub_table(unsigned int shndx) const
1468   {
1469     gold_assert(shndx < this->stub_tables_.size());
1470     return this->stub_tables_[shndx];
1471   }
1472
1473   // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1474   void
1475   set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
1476   {
1477     gold_assert(shndx < this->stub_tables_.size());
1478     this->stub_tables_[shndx] = stub_table;
1479   }
1480
1481   // Whether a local symbol is a THUMB function.  R_SYM is the symbol table
1482   // index.  This is only valid after do_count_local_symbol is called.
1483   bool
1484   local_symbol_is_thumb_function(unsigned int r_sym) const
1485   {
1486     gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
1487     return this->local_symbol_is_thumb_function_[r_sym];
1488   }
1489   
1490   // Scan all relocation sections for stub generation.
1491   void
1492   scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
1493                           const Layout*);
1494
1495   // Convert regular input section with index SHNDX to a relaxed section.
1496   void
1497   convert_input_section_to_relaxed_section(unsigned shndx)
1498   {
1499     // The stubs have relocations and we need to process them after writing
1500     // out the stubs.  So relocation now must follow section write.
1501     this->set_section_offset(shndx, -1ULL);
1502     this->set_relocs_must_follow_section_writes();
1503   }
1504
1505   // Downcast a base pointer to an Arm_relobj pointer.  This is
1506   // not type-safe but we only use Arm_relobj not the base class.
1507   static Arm_relobj<big_endian>*
1508   as_arm_relobj(Relobj* relobj)
1509   { return static_cast<Arm_relobj<big_endian>*>(relobj); }
1510
1511   // Processor-specific flags in ELF file header.  This is valid only after
1512   // reading symbols.
1513   elfcpp::Elf_Word
1514   processor_specific_flags() const
1515   { return this->processor_specific_flags_; }
1516
1517   // Attribute section data  This is the contents of the .ARM.attribute section
1518   // if there is one.
1519   const Attributes_section_data*
1520   attributes_section_data() const
1521   { return this->attributes_section_data_; }
1522
1523   // Mapping symbol location.
1524   typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
1525
1526   // Functor for STL container.
1527   struct Mapping_symbol_position_less
1528   {
1529     bool
1530     operator()(const Mapping_symbol_position& p1,
1531                const Mapping_symbol_position& p2) const
1532     {
1533       return (p1.first < p2.first
1534               || (p1.first == p2.first && p1.second < p2.second));
1535     }
1536   };
1537   
1538   // We only care about the first character of a mapping symbol, so
1539   // we only store that instead of the whole symbol name.
1540   typedef std::map<Mapping_symbol_position, char,
1541                    Mapping_symbol_position_less> Mapping_symbols_info;
1542
1543   // Whether a section contains any Cortex-A8 workaround.
1544   bool
1545   section_has_cortex_a8_workaround(unsigned int shndx) const
1546   { 
1547     return (this->section_has_cortex_a8_workaround_ != NULL
1548             && (*this->section_has_cortex_a8_workaround_)[shndx]);
1549   }
1550   
1551   // Mark a section that has Cortex-A8 workaround.
1552   void
1553   mark_section_for_cortex_a8_workaround(unsigned int shndx)
1554   {
1555     if (this->section_has_cortex_a8_workaround_ == NULL)
1556       this->section_has_cortex_a8_workaround_ =
1557         new std::vector<bool>(this->shnum(), false);
1558     (*this->section_has_cortex_a8_workaround_)[shndx] = true;
1559   }
1560
1561   // Return the EXIDX section of an text section with index SHNDX or NULL
1562   // if the text section has no associated EXIDX section.
1563   const Arm_exidx_input_section*
1564   exidx_input_section_by_link(unsigned int shndx) const
1565   {
1566     Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1567     return ((p != this->exidx_section_map_.end()
1568              && p->second->link() == shndx)
1569             ? p->second
1570             : NULL);
1571   }
1572
1573   // Return the EXIDX section with index SHNDX or NULL if there is none.
1574   const Arm_exidx_input_section*
1575   exidx_input_section_by_shndx(unsigned shndx) const
1576   {
1577     Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
1578     return ((p != this->exidx_section_map_.end()
1579              && p->second->shndx() == shndx)
1580             ? p->second
1581             : NULL);
1582   }
1583
1584   // Whether output local symbol count needs updating.
1585   bool
1586   output_local_symbol_count_needs_update() const
1587   { return this->output_local_symbol_count_needs_update_; }
1588
1589   // Set output_local_symbol_count_needs_update flag to be true.
1590   void
1591   set_output_local_symbol_count_needs_update()
1592   { this->output_local_symbol_count_needs_update_ = true; }
1593   
1594   // Update output local symbol count at the end of relaxation.
1595   void
1596   update_output_local_symbol_count();
1597
1598   // Whether we want to merge processor-specific flags and attributes.
1599   bool
1600   merge_flags_and_attributes() const
1601   { return this->merge_flags_and_attributes_; }
1602   
1603   // Export list of EXIDX section indices.
1604   void
1605   get_exidx_shndx_list(std::vector<unsigned int>* list) const
1606   {
1607     list->clear();
1608     for (Exidx_section_map::const_iterator p = this->exidx_section_map_.begin();
1609          p != this->exidx_section_map_.end();
1610          ++p)
1611       {
1612         if (p->second->shndx() == p->first)
1613           list->push_back(p->first);
1614       }
1615     // Sort list to make result independent of implementation of map. 
1616     std::sort(list->begin(), list->end());
1617   }
1618
1619  protected:
1620   // Post constructor setup.
1621   void
1622   do_setup()
1623   {
1624     // Call parent's setup method.
1625     Sized_relobj<32, big_endian>::do_setup();
1626
1627     // Initialize look-up tables.
1628     Stub_table_list empty_stub_table_list(this->shnum(), NULL);
1629     this->stub_tables_.swap(empty_stub_table_list);
1630   }
1631
1632   // Count the local symbols.
1633   void
1634   do_count_local_symbols(Stringpool_template<char>*,
1635                          Stringpool_template<char>*);
1636
1637   void
1638   do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
1639                        const unsigned char* pshdrs,
1640                        typename Sized_relobj<32, big_endian>::Views* pivews);
1641
1642   // Read the symbol information.
1643   void
1644   do_read_symbols(Read_symbols_data* sd);
1645
1646   // Process relocs for garbage collection.
1647   void
1648   do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1649
1650  private:
1651
1652   // Whether a section needs to be scanned for relocation stubs.
1653   bool
1654   section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1655                                     const Relobj::Output_sections&,
1656                                     const Symbol_table *, const unsigned char*);
1657
1658   // Whether a section is a scannable text section.
1659   bool
1660   section_is_scannable(const elfcpp::Shdr<32, big_endian>&, unsigned int,
1661                        const Output_section*, const Symbol_table *);
1662
1663   // Whether a section needs to be scanned for the Cortex-A8 erratum.
1664   bool
1665   section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
1666                                         unsigned int, Output_section*,
1667                                         const Symbol_table *);
1668
1669   // Scan a section for the Cortex-A8 erratum.
1670   void
1671   scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
1672                                      unsigned int, Output_section*,
1673                                      Target_arm<big_endian>*);
1674
1675   // Find the linked text section of an EXIDX section by looking at the
1676   // first reloction of the EXIDX section.  PSHDR points to the section
1677   // headers of a relocation section and PSYMS points to the local symbols.
1678   // PSHNDX points to a location storing the text section index if found.
1679   // Return whether we can find the linked section.
1680   bool
1681   find_linked_text_section(const unsigned char* pshdr,
1682                            const unsigned char* psyms, unsigned int* pshndx);
1683
1684   //
1685   // Make a new Arm_exidx_input_section object for EXIDX section with
1686   // index SHNDX and section header SHDR.  TEXT_SHNDX is the section
1687   // index of the linked text section.
1688   void
1689   make_exidx_input_section(unsigned int shndx,
1690                            const elfcpp::Shdr<32, big_endian>& shdr,
1691                            unsigned int text_shndx,
1692                            const elfcpp::Shdr<32, big_endian>& text_shdr);
1693
1694   // Return the output address of either a plain input section or a
1695   // relaxed input section.  SHNDX is the section index.
1696   Arm_address
1697   simple_input_section_output_address(unsigned int, Output_section*);
1698
1699   typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
1700   typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
1701     Exidx_section_map;
1702
1703   // List of stub tables.
1704   Stub_table_list stub_tables_;
1705   // Bit vector to tell if a local symbol is a thumb function or not.
1706   // This is only valid after do_count_local_symbol is called.
1707   std::vector<bool> local_symbol_is_thumb_function_;
1708   // processor-specific flags in ELF file header.
1709   elfcpp::Elf_Word processor_specific_flags_;
1710   // Object attributes if there is an .ARM.attributes section or NULL.
1711   Attributes_section_data* attributes_section_data_;
1712   // Mapping symbols information.
1713   Mapping_symbols_info mapping_symbols_info_;
1714   // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
1715   std::vector<bool>* section_has_cortex_a8_workaround_;
1716   // Map a text section to its associated .ARM.exidx section, if there is one.
1717   Exidx_section_map exidx_section_map_;
1718   // Whether output local symbol count needs updating.
1719   bool output_local_symbol_count_needs_update_;
1720   // Whether we merge processor flags and attributes of this object to
1721   // output.
1722   bool merge_flags_and_attributes_;
1723 };
1724
1725 // Arm_dynobj class.
1726
1727 template<bool big_endian>
1728 class Arm_dynobj : public Sized_dynobj<32, big_endian>
1729 {
1730  public:
1731   Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
1732              const elfcpp::Ehdr<32, big_endian>& ehdr)
1733     : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
1734       processor_specific_flags_(0), attributes_section_data_(NULL)
1735   { }
1736  
1737   ~Arm_dynobj()
1738   { delete this->attributes_section_data_; }
1739
1740   // Downcast a base pointer to an Arm_relobj pointer.  This is
1741   // not type-safe but we only use Arm_relobj not the base class.
1742   static Arm_dynobj<big_endian>*
1743   as_arm_dynobj(Dynobj* dynobj)
1744   { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
1745
1746   // Processor-specific flags in ELF file header.  This is valid only after
1747   // reading symbols.
1748   elfcpp::Elf_Word
1749   processor_specific_flags() const
1750   { return this->processor_specific_flags_; }
1751
1752   // Attributes section data.
1753   const Attributes_section_data*
1754   attributes_section_data() const
1755   { return this->attributes_section_data_; }
1756
1757  protected:
1758   // Read the symbol information.
1759   void
1760   do_read_symbols(Read_symbols_data* sd);
1761
1762  private:
1763   // processor-specific flags in ELF file header.
1764   elfcpp::Elf_Word processor_specific_flags_;
1765   // Object attributes if there is an .ARM.attributes section or NULL.
1766   Attributes_section_data* attributes_section_data_;
1767 };
1768
1769 // Functor to read reloc addends during stub generation.
1770
1771 template<int sh_type, bool big_endian>
1772 struct Stub_addend_reader
1773 {
1774   // Return the addend for a relocation of a particular type.  Depending
1775   // on whether this is a REL or RELA relocation, read the addend from a
1776   // view or from a Reloc object.
1777   elfcpp::Elf_types<32>::Elf_Swxword
1778   operator()(
1779     unsigned int /* r_type */,
1780     const unsigned char* /* view */,
1781     const typename Reloc_types<sh_type,
1782                                32, big_endian>::Reloc& /* reloc */) const;
1783 };
1784
1785 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
1786
1787 template<bool big_endian>
1788 struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
1789 {
1790   elfcpp::Elf_types<32>::Elf_Swxword
1791   operator()(
1792     unsigned int,
1793     const unsigned char*,
1794     const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
1795 };
1796
1797 // Specialized Stub_addend_reader for RELA type relocation sections.
1798 // We currently do not handle RELA type relocation sections but it is trivial
1799 // to implement the addend reader.  This is provided for completeness and to
1800 // make it easier to add support for RELA relocation sections in the future.
1801
1802 template<bool big_endian>
1803 struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
1804 {
1805   elfcpp::Elf_types<32>::Elf_Swxword
1806   operator()(
1807     unsigned int,
1808     const unsigned char*,
1809     const typename Reloc_types<elfcpp::SHT_RELA, 32,
1810                                big_endian>::Reloc& reloc) const
1811   { return reloc.get_r_addend(); }
1812 };
1813
1814 // Cortex_a8_reloc class.  We keep record of relocation that may need
1815 // the Cortex-A8 erratum workaround.
1816
1817 class Cortex_a8_reloc
1818 {
1819  public:
1820   Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
1821                   Arm_address destination)
1822     : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
1823   { }
1824
1825   ~Cortex_a8_reloc()
1826   { }
1827
1828   // Accessors:  This is a read-only class.
1829   
1830   // Return the relocation stub associated with this relocation if there is
1831   // one.
1832   const Reloc_stub*
1833   reloc_stub() const
1834   { return this->reloc_stub_; } 
1835   
1836   // Return the relocation type.
1837   unsigned int
1838   r_type() const
1839   { return this->r_type_; }
1840
1841   // Return the destination address of the relocation.  LSB stores the THUMB
1842   // bit.
1843   Arm_address
1844   destination() const
1845   { return this->destination_; }
1846
1847  private:
1848   // Associated relocation stub if there is one, or NULL.
1849   const Reloc_stub* reloc_stub_;
1850   // Relocation type.
1851   unsigned int r_type_;
1852   // Destination address of this relocation.  LSB is used to distinguish
1853   // ARM/THUMB mode.
1854   Arm_address destination_;
1855 };
1856
1857 // Arm_output_data_got class.  We derive this from Output_data_got to add
1858 // extra methods to handle TLS relocations in a static link.
1859
1860 template<bool big_endian>
1861 class Arm_output_data_got : public Output_data_got<32, big_endian>
1862 {
1863  public:
1864   Arm_output_data_got(Symbol_table* symtab, Layout* layout)
1865     : Output_data_got<32, big_endian>(), symbol_table_(symtab), layout_(layout)
1866   { }
1867
1868   // Add a static entry for the GOT entry at OFFSET.  GSYM is a global
1869   // symbol and R_TYPE is the code of a dynamic relocation that needs to be
1870   // applied in a static link.
1871   void
1872   add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1873   { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
1874
1875   // Add a static reloc for the GOT entry at OFFSET.  RELOBJ is an object
1876   // defining a local symbol with INDEX.  R_TYPE is the code of a dynamic
1877   // relocation that needs to be applied in a static link.
1878   void
1879   add_static_reloc(unsigned int got_offset, unsigned int r_type,
1880                    Sized_relobj<32, big_endian>* relobj, unsigned int index)
1881   {
1882     this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
1883                                                 index));
1884   }
1885
1886   // Add a GOT pair for R_ARM_TLS_GD32.  The creates a pair of GOT entries.
1887   // The first one is initialized to be 1, which is the module index for
1888   // the main executable and the second one 0.  A reloc of the type
1889   // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
1890   // be applied by gold.  GSYM is a global symbol.
1891   void
1892   add_tls_gd32_with_static_reloc(unsigned int got_type, Symbol* gsym);
1893
1894   // Same as the above but for a local symbol in OBJECT with INDEX.
1895   void
1896   add_tls_gd32_with_static_reloc(unsigned int got_type,
1897                                  Sized_relobj<32, big_endian>* object,
1898                                  unsigned int index);
1899
1900  protected:
1901   // Write out the GOT table.
1902   void
1903   do_write(Output_file*);
1904
1905  private:
1906   // This class represent dynamic relocations that need to be applied by
1907   // gold because we are using TLS relocations in a static link.
1908   class Static_reloc
1909   {
1910    public:
1911     Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
1912       : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
1913     { this->u_.global.symbol = gsym; }
1914
1915     Static_reloc(unsigned int got_offset, unsigned int r_type,
1916           Sized_relobj<32, big_endian>* relobj, unsigned int index)
1917       : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
1918     {
1919       this->u_.local.relobj = relobj;
1920       this->u_.local.index = index;
1921     }
1922
1923     // Return the GOT offset.
1924     unsigned int
1925     got_offset() const
1926     { return this->got_offset_; }
1927
1928     // Relocation type.
1929     unsigned int
1930     r_type() const
1931     { return this->r_type_; }
1932
1933     // Whether the symbol is global or not.
1934     bool
1935     symbol_is_global() const
1936     { return this->symbol_is_global_; }
1937
1938     // For a relocation against a global symbol, the global symbol.
1939     Symbol*
1940     symbol() const
1941     {
1942       gold_assert(this->symbol_is_global_);
1943       return this->u_.global.symbol;
1944     }
1945
1946     // For a relocation against a local symbol, the defining object.
1947     Sized_relobj<32, big_endian>*
1948     relobj() const
1949     {
1950       gold_assert(!this->symbol_is_global_);
1951       return this->u_.local.relobj;
1952     }
1953
1954     // For a relocation against a local symbol, the local symbol index.
1955     unsigned int
1956     index() const
1957     {
1958       gold_assert(!this->symbol_is_global_);
1959       return this->u_.local.index;
1960     }
1961
1962    private:
1963     // GOT offset of the entry to which this relocation is applied.
1964     unsigned int got_offset_;
1965     // Type of relocation.
1966     unsigned int r_type_;
1967     // Whether this relocation is against a global symbol.
1968     bool symbol_is_global_;
1969     // A global or local symbol.
1970     union
1971     {
1972       struct
1973       {
1974         // For a global symbol, the symbol itself.
1975         Symbol* symbol;
1976       } global;
1977       struct
1978       {
1979         // For a local symbol, the object defining object.
1980         Sized_relobj<32, big_endian>* relobj;
1981         // For a local symbol, the symbol index.
1982         unsigned int index;
1983       } local;
1984     } u_;
1985   };
1986
1987   // Symbol table of the output object.
1988   Symbol_table* symbol_table_;
1989   // Layout of the output object.
1990   Layout* layout_;
1991   // Static relocs to be applied to the GOT.
1992   std::vector<Static_reloc> static_relocs_;
1993 };
1994
1995 // The ARM target has many relocation types with odd-sizes or incontigious
1996 // bits.  The default handling of relocatable relocation cannot process these
1997 // relocations.  So we have to extend the default code.
1998
1999 template<bool big_endian, int sh_type, typename Classify_reloc>
2000 class Arm_scan_relocatable_relocs :
2001   public Default_scan_relocatable_relocs<sh_type, Classify_reloc>
2002 {
2003  public:
2004   // Return the strategy to use for a local symbol which is a section
2005   // symbol, given the relocation type.
2006   inline Relocatable_relocs::Reloc_strategy
2007   local_section_strategy(unsigned int r_type, Relobj*)
2008   {
2009     if (sh_type == elfcpp::SHT_RELA)
2010       return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2011     else
2012       {
2013         if (r_type == elfcpp::R_ARM_TARGET1
2014             || r_type == elfcpp::R_ARM_TARGET2)
2015           {
2016             const Target_arm<big_endian>* arm_target =
2017               Target_arm<big_endian>::default_target();
2018             r_type = arm_target->get_real_reloc_type(r_type);
2019           }
2020
2021         switch(r_type)
2022           {
2023           // Relocations that write nothing.  These exclude R_ARM_TARGET1
2024           // and R_ARM_TARGET2.
2025           case elfcpp::R_ARM_NONE:
2026           case elfcpp::R_ARM_V4BX:
2027           case elfcpp::R_ARM_TLS_GOTDESC:
2028           case elfcpp::R_ARM_TLS_CALL:
2029           case elfcpp::R_ARM_TLS_DESCSEQ:
2030           case elfcpp::R_ARM_THM_TLS_CALL:
2031           case elfcpp::R_ARM_GOTRELAX:
2032           case elfcpp::R_ARM_GNU_VTENTRY:
2033           case elfcpp::R_ARM_GNU_VTINHERIT:
2034           case elfcpp::R_ARM_THM_TLS_DESCSEQ16:
2035           case elfcpp::R_ARM_THM_TLS_DESCSEQ32:
2036             return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
2037           // These should have been converted to something else above.
2038           case elfcpp::R_ARM_TARGET1:
2039           case elfcpp::R_ARM_TARGET2:
2040             gold_unreachable();
2041           // Relocations that write full 32 bits.
2042           case elfcpp::R_ARM_ABS32:
2043           case elfcpp::R_ARM_REL32:
2044           case elfcpp::R_ARM_SBREL32:
2045           case elfcpp::R_ARM_GOTOFF32:
2046           case elfcpp::R_ARM_BASE_PREL:
2047           case elfcpp::R_ARM_GOT_BREL:
2048           case elfcpp::R_ARM_BASE_ABS:
2049           case elfcpp::R_ARM_ABS32_NOI:
2050           case elfcpp::R_ARM_REL32_NOI:
2051           case elfcpp::R_ARM_PLT32_ABS:
2052           case elfcpp::R_ARM_GOT_ABS:
2053           case elfcpp::R_ARM_GOT_PREL:
2054           case elfcpp::R_ARM_TLS_GD32:
2055           case elfcpp::R_ARM_TLS_LDM32:
2056           case elfcpp::R_ARM_TLS_LDO32:
2057           case elfcpp::R_ARM_TLS_IE32:
2058           case elfcpp::R_ARM_TLS_LE32:
2059             return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4;
2060           default:
2061             // For all other static relocations, return RELOC_SPECIAL.
2062             return Relocatable_relocs::RELOC_SPECIAL;
2063           }
2064       }
2065   }
2066 };
2067
2068 // Utilities for manipulating integers of up to 32-bits
2069
2070 namespace utils
2071 {
2072   // Sign extend an n-bit unsigned integer stored in an uint32_t into
2073   // an int32_t.  NO_BITS must be between 1 to 32.
2074   template<int no_bits>
2075   static inline int32_t
2076   sign_extend(uint32_t bits)
2077   {
2078     gold_assert(no_bits >= 0 && no_bits <= 32);
2079     if (no_bits == 32)
2080       return static_cast<int32_t>(bits);
2081     uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
2082     bits &= mask;
2083     uint32_t top_bit = 1U << (no_bits - 1);
2084     int32_t as_signed = static_cast<int32_t>(bits);
2085     return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
2086   }
2087
2088   // Detects overflow of an NO_BITS integer stored in a uint32_t.
2089   template<int no_bits>
2090   static inline bool
2091   has_overflow(uint32_t bits)
2092   {
2093     gold_assert(no_bits >= 0 && no_bits <= 32);
2094     if (no_bits == 32)
2095       return false;
2096     int32_t max = (1 << (no_bits - 1)) - 1;
2097     int32_t min = -(1 << (no_bits - 1));
2098     int32_t as_signed = static_cast<int32_t>(bits);
2099     return as_signed > max || as_signed < min;
2100   }
2101
2102   // Detects overflow of an NO_BITS integer stored in a uint32_t when it
2103   // fits in the given number of bits as either a signed or unsigned value.
2104   // For example, has_signed_unsigned_overflow<8> would check
2105   // -128 <= bits <= 255
2106   template<int no_bits>
2107   static inline bool
2108   has_signed_unsigned_overflow(uint32_t bits)
2109   {
2110     gold_assert(no_bits >= 2 && no_bits <= 32);
2111     if (no_bits == 32)
2112       return false;
2113     int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
2114     int32_t min = -(1 << (no_bits - 1));
2115     int32_t as_signed = static_cast<int32_t>(bits);
2116     return as_signed > max || as_signed < min;
2117   }
2118
2119   // Select bits from A and B using bits in MASK.  For each n in [0..31],
2120   // the n-th bit in the result is chosen from the n-th bits of A and B.
2121   // A zero selects A and a one selects B.
2122   static inline uint32_t
2123   bit_select(uint32_t a, uint32_t b, uint32_t mask)
2124   { return (a & ~mask) | (b & mask); }
2125 };
2126
2127 template<bool big_endian>
2128 class Target_arm : public Sized_target<32, big_endian>
2129 {
2130  public:
2131   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
2132     Reloc_section;
2133
2134   // When were are relocating a stub, we pass this as the relocation number.
2135   static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
2136
2137   Target_arm()
2138     : Sized_target<32, big_endian>(&arm_info),
2139       got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
2140       copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), 
2141       got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
2142       stub_tables_(), stub_factory_(Stub_factory::get_instance()),
2143       may_use_blx_(false), should_force_pic_veneer_(false),
2144       arm_input_section_map_(), attributes_section_data_(NULL),
2145       fix_cortex_a8_(false), cortex_a8_relocs_info_()
2146   { }
2147
2148   // Virtual function which is set to return true by a target if
2149   // it can use relocation types to determine if a function's
2150   // pointer is taken.
2151   virtual bool
2152   can_check_for_function_pointers() const
2153   { return true; }
2154
2155   // Whether a section called SECTION_NAME may have function pointers to
2156   // sections not eligible for safe ICF folding.
2157   virtual bool
2158   section_may_have_icf_unsafe_pointers(const char* section_name) const
2159   {
2160     return (!is_prefix_of(".ARM.exidx", section_name)
2161             && !is_prefix_of(".ARM.extab", section_name)
2162             && Target::section_may_have_icf_unsafe_pointers(section_name));
2163   }
2164   
2165   // Whether we can use BLX.
2166   bool
2167   may_use_blx() const
2168   { return this->may_use_blx_; }
2169
2170   // Set use-BLX flag.
2171   void
2172   set_may_use_blx(bool value)
2173   { this->may_use_blx_ = value; }
2174   
2175   // Whether we force PCI branch veneers.
2176   bool
2177   should_force_pic_veneer() const
2178   { return this->should_force_pic_veneer_; }
2179
2180   // Set PIC veneer flag.
2181   void
2182   set_should_force_pic_veneer(bool value)
2183   { this->should_force_pic_veneer_ = value; }
2184   
2185   // Whether we use THUMB-2 instructions.
2186   bool
2187   using_thumb2() const
2188   {
2189     Object_attribute* attr =
2190       this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2191     int arch = attr->int_value();
2192     return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
2193   }
2194
2195   // Whether we use THUMB/THUMB-2 instructions only.
2196   bool
2197   using_thumb_only() const
2198   {
2199     Object_attribute* attr =
2200       this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2201
2202     if (attr->int_value() == elfcpp::TAG_CPU_ARCH_V6_M
2203         || attr->int_value() == elfcpp::TAG_CPU_ARCH_V6S_M)
2204       return true;
2205     if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
2206         && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
2207       return false;
2208     attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
2209     return attr->int_value() == 'M';
2210   }
2211
2212   // Whether we have an NOP instruction.  If not, use mov r0, r0 instead.
2213   bool
2214   may_use_arm_nop() const
2215   {
2216     Object_attribute* attr =
2217       this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2218     int arch = attr->int_value();
2219     return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2220             || arch == elfcpp::TAG_CPU_ARCH_V6K
2221             || arch == elfcpp::TAG_CPU_ARCH_V7
2222             || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2223   }
2224
2225   // Whether we have THUMB-2 NOP.W instruction.
2226   bool
2227   may_use_thumb2_nop() const
2228   {
2229     Object_attribute* attr =
2230       this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
2231     int arch = attr->int_value();
2232     return (arch == elfcpp::TAG_CPU_ARCH_V6T2
2233             || arch == elfcpp::TAG_CPU_ARCH_V7
2234             || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
2235   }
2236   
2237   // Process the relocations to determine unreferenced sections for 
2238   // garbage collection.
2239   void
2240   gc_process_relocs(Symbol_table* symtab,
2241                     Layout* layout,
2242                     Sized_relobj<32, big_endian>* object,
2243                     unsigned int data_shndx,
2244                     unsigned int sh_type,
2245                     const unsigned char* prelocs,
2246                     size_t reloc_count,
2247                     Output_section* output_section,
2248                     bool needs_special_offset_handling,
2249                     size_t local_symbol_count,
2250                     const unsigned char* plocal_symbols);
2251
2252   // Scan the relocations to look for symbol adjustments.
2253   void
2254   scan_relocs(Symbol_table* symtab,
2255               Layout* layout,
2256               Sized_relobj<32, big_endian>* object,
2257               unsigned int data_shndx,
2258               unsigned int sh_type,
2259               const unsigned char* prelocs,
2260               size_t reloc_count,
2261               Output_section* output_section,
2262               bool needs_special_offset_handling,
2263               size_t local_symbol_count,
2264               const unsigned char* plocal_symbols);
2265
2266   // Finalize the sections.
2267   void
2268   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2269
2270   // Return the value to use for a dynamic symbol which requires special
2271   // treatment.
2272   uint64_t
2273   do_dynsym_value(const Symbol*) const;
2274
2275   // Relocate a section.
2276   void
2277   relocate_section(const Relocate_info<32, big_endian>*,
2278                    unsigned int sh_type,
2279                    const unsigned char* prelocs,
2280                    size_t reloc_count,
2281                    Output_section* output_section,
2282                    bool needs_special_offset_handling,
2283                    unsigned char* view,
2284                    Arm_address view_address,
2285                    section_size_type view_size,
2286                    const Reloc_symbol_changes*);
2287
2288   // Scan the relocs during a relocatable link.
2289   void
2290   scan_relocatable_relocs(Symbol_table* symtab,
2291                           Layout* layout,
2292                           Sized_relobj<32, big_endian>* object,
2293                           unsigned int data_shndx,
2294                           unsigned int sh_type,
2295                           const unsigned char* prelocs,
2296                           size_t reloc_count,
2297                           Output_section* output_section,
2298                           bool needs_special_offset_handling,
2299                           size_t local_symbol_count,
2300                           const unsigned char* plocal_symbols,
2301                           Relocatable_relocs*);
2302
2303   // Relocate a section during a relocatable link.
2304   void
2305   relocate_for_relocatable(const Relocate_info<32, big_endian>*,
2306                            unsigned int sh_type,
2307                            const unsigned char* prelocs,
2308                            size_t reloc_count,
2309                            Output_section* output_section,
2310                            off_t offset_in_output_section,
2311                            const Relocatable_relocs*,
2312                            unsigned char* view,
2313                            Arm_address view_address,
2314                            section_size_type view_size,
2315                            unsigned char* reloc_view,
2316                            section_size_type reloc_view_size);
2317
2318   // Perform target-specific processing in a relocatable link.  This is
2319   // only used if we use the relocation strategy RELOC_SPECIAL.
2320   void
2321   relocate_special_relocatable(const Relocate_info<32, big_endian>* relinfo,
2322                                unsigned int sh_type,
2323                                const unsigned char* preloc_in,
2324                                size_t relnum,
2325                                Output_section* output_section,
2326                                off_t offset_in_output_section,
2327                                unsigned char* view,
2328                                typename elfcpp::Elf_types<32>::Elf_Addr
2329                                  view_address,
2330                                section_size_type view_size,
2331                                unsigned char* preloc_out);
2332  
2333   // Return whether SYM is defined by the ABI.
2334   bool
2335   do_is_defined_by_abi(Symbol* sym) const
2336   { return strcmp(sym->name(), "__tls_get_addr") == 0; }
2337
2338   // Return whether there is a GOT section.
2339   bool
2340   has_got_section() const
2341   { return this->got_ != NULL; }
2342
2343   // Return the size of the GOT section.
2344   section_size_type
2345   got_size()
2346   {
2347     gold_assert(this->got_ != NULL);
2348     return this->got_->data_size();
2349   }
2350
2351   // Map platform-specific reloc types
2352   static unsigned int
2353   get_real_reloc_type (unsigned int r_type);
2354
2355   //
2356   // Methods to support stub-generations.
2357   //
2358   
2359   // Return the stub factory
2360   const Stub_factory&
2361   stub_factory() const
2362   { return this->stub_factory_; }
2363
2364   // Make a new Arm_input_section object.
2365   Arm_input_section<big_endian>*
2366   new_arm_input_section(Relobj*, unsigned int);
2367
2368   // Find the Arm_input_section object corresponding to the SHNDX-th input
2369   // section of RELOBJ.
2370   Arm_input_section<big_endian>*
2371   find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
2372
2373   // Make a new Stub_table
2374   Stub_table<big_endian>*
2375   new_stub_table(Arm_input_section<big_endian>*);
2376
2377   // Scan a section for stub generation.
2378   void
2379   scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
2380                          const unsigned char*, size_t, Output_section*,
2381                          bool, const unsigned char*, Arm_address,
2382                          section_size_type);
2383
2384   // Relocate a stub. 
2385   void
2386   relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
2387                 Output_section*, unsigned char*, Arm_address,
2388                 section_size_type);
2389  
2390   // Get the default ARM target.
2391   static Target_arm<big_endian>*
2392   default_target()
2393   {
2394     gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
2395                 && parameters->target().is_big_endian() == big_endian);
2396     return static_cast<Target_arm<big_endian>*>(
2397              parameters->sized_target<32, big_endian>());
2398   }
2399
2400   // Whether NAME belongs to a mapping symbol.
2401   static bool
2402   is_mapping_symbol_name(const char* name)
2403   {
2404     return (name
2405             && name[0] == '$'
2406             && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
2407             && (name[2] == '\0' || name[2] == '.'));
2408   }
2409
2410   // Whether we work around the Cortex-A8 erratum.
2411   bool
2412   fix_cortex_a8() const
2413   { return this->fix_cortex_a8_; }
2414
2415   // Whether we merge exidx entries in debuginfo.
2416   bool
2417   merge_exidx_entries() const
2418   { return parameters->options().merge_exidx_entries(); }
2419
2420   // Whether we fix R_ARM_V4BX relocation.
2421   // 0 - do not fix
2422   // 1 - replace with MOV instruction (armv4 target)
2423   // 2 - make interworking veneer (>= armv4t targets only)
2424   General_options::Fix_v4bx
2425   fix_v4bx() const
2426   { return parameters->options().fix_v4bx(); }
2427
2428   // Scan a span of THUMB code section for Cortex-A8 erratum.
2429   void
2430   scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
2431                                   section_size_type, section_size_type,
2432                                   const unsigned char*, Arm_address);
2433
2434   // Apply Cortex-A8 workaround to a branch.
2435   void
2436   apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
2437                              unsigned char*, Arm_address);
2438
2439  protected:
2440   // Make an ELF object.
2441   Object*
2442   do_make_elf_object(const std::string&, Input_file*, off_t,
2443                      const elfcpp::Ehdr<32, big_endian>& ehdr);
2444
2445   Object*
2446   do_make_elf_object(const std::string&, Input_file*, off_t,
2447                      const elfcpp::Ehdr<32, !big_endian>&)
2448   { gold_unreachable(); }
2449
2450   Object*
2451   do_make_elf_object(const std::string&, Input_file*, off_t,
2452                       const elfcpp::Ehdr<64, false>&)
2453   { gold_unreachable(); }
2454
2455   Object*
2456   do_make_elf_object(const std::string&, Input_file*, off_t,
2457                      const elfcpp::Ehdr<64, true>&)
2458   { gold_unreachable(); }
2459
2460   // Make an output section.
2461   Output_section*
2462   do_make_output_section(const char* name, elfcpp::Elf_Word type,
2463                          elfcpp::Elf_Xword flags)
2464   { return new Arm_output_section<big_endian>(name, type, flags); }
2465
2466   void
2467   do_adjust_elf_header(unsigned char* view, int len) const;
2468
2469   // We only need to generate stubs, and hence perform relaxation if we are
2470   // not doing relocatable linking.
2471   bool
2472   do_may_relax() const
2473   { return !parameters->options().relocatable(); }
2474
2475   bool
2476   do_relax(int, const Input_objects*, Symbol_table*, Layout*);
2477
2478   // Determine whether an object attribute tag takes an integer, a
2479   // string or both.
2480   int
2481   do_attribute_arg_type(int tag) const;
2482
2483   // Reorder tags during output.
2484   int
2485   do_attributes_order(int num) const;
2486
2487   // This is called when the target is selected as the default.
2488   void
2489   do_select_as_default_target()
2490   {
2491     // No locking is required since there should only be one default target.
2492     // We cannot have both the big-endian and little-endian ARM targets
2493     // as the default.
2494     gold_assert(arm_reloc_property_table == NULL);
2495     arm_reloc_property_table = new Arm_reloc_property_table();
2496   }
2497
2498  private:
2499   // The class which scans relocations.
2500   class Scan
2501   {
2502    public:
2503     Scan()
2504       : issued_non_pic_error_(false)
2505     { }
2506
2507     inline void
2508     local(Symbol_table* symtab, Layout* layout, Target_arm* target,
2509           Sized_relobj<32, big_endian>* object,
2510           unsigned int data_shndx,
2511           Output_section* output_section,
2512           const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2513           const elfcpp::Sym<32, big_endian>& lsym);
2514
2515     inline void
2516     global(Symbol_table* symtab, Layout* layout, Target_arm* target,
2517            Sized_relobj<32, big_endian>* object,
2518            unsigned int data_shndx,
2519            Output_section* output_section,
2520            const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
2521            Symbol* gsym);
2522
2523     inline bool
2524     local_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2525                                         Sized_relobj<32, big_endian>* ,
2526                                         unsigned int ,
2527                                         Output_section* ,
2528                                         const elfcpp::Rel<32, big_endian>& ,
2529                                         unsigned int ,
2530                                         const elfcpp::Sym<32, big_endian>&);
2531
2532     inline bool
2533     global_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
2534                                          Sized_relobj<32, big_endian>* ,
2535                                          unsigned int ,
2536                                          Output_section* ,
2537                                          const elfcpp::Rel<32, big_endian>& ,
2538                                          unsigned int , Symbol*);
2539
2540    private:
2541     static void
2542     unsupported_reloc_local(Sized_relobj<32, big_endian>*,
2543                             unsigned int r_type);
2544
2545     static void
2546     unsupported_reloc_global(Sized_relobj<32, big_endian>*,
2547                              unsigned int r_type, Symbol*);
2548
2549     void
2550     check_non_pic(Relobj*, unsigned int r_type);
2551
2552     // Almost identical to Symbol::needs_plt_entry except that it also
2553     // handles STT_ARM_TFUNC.
2554     static bool
2555     symbol_needs_plt_entry(const Symbol* sym)
2556     {
2557       // An undefined symbol from an executable does not need a PLT entry.
2558       if (sym->is_undefined() && !parameters->options().shared())
2559         return false;
2560
2561       return (!parameters->doing_static_link()
2562               && (sym->type() == elfcpp::STT_FUNC
2563                   || sym->type() == elfcpp::STT_ARM_TFUNC)
2564               && (sym->is_from_dynobj()
2565                   || sym->is_undefined()
2566                   || sym->is_preemptible()));
2567     }
2568
2569     inline bool
2570     possible_function_pointer_reloc(unsigned int r_type);
2571
2572     // Whether we have issued an error about a non-PIC compilation.
2573     bool issued_non_pic_error_;
2574   };
2575
2576   // The class which implements relocation.
2577   class Relocate
2578   {
2579    public:
2580     Relocate()
2581     { }
2582
2583     ~Relocate()
2584     { }
2585
2586     // Return whether the static relocation needs to be applied.
2587     inline bool
2588     should_apply_static_reloc(const Sized_symbol<32>* gsym,
2589                               int ref_flags,
2590                               bool is_32bit,
2591                               Output_section* output_section);
2592
2593     // Do a relocation.  Return false if the caller should not issue
2594     // any warnings about this relocation.
2595     inline bool
2596     relocate(const Relocate_info<32, big_endian>*, Target_arm*,
2597              Output_section*,  size_t relnum,
2598              const elfcpp::Rel<32, big_endian>&,
2599              unsigned int r_type, const Sized_symbol<32>*,
2600              const Symbol_value<32>*,
2601              unsigned char*, Arm_address,
2602              section_size_type);
2603
2604     // Return whether we want to pass flag NON_PIC_REF for this
2605     // reloc.  This means the relocation type accesses a symbol not via
2606     // GOT or PLT.
2607     static inline bool
2608     reloc_is_non_pic (unsigned int r_type)
2609     {
2610       switch (r_type)
2611         {
2612         // These relocation types reference GOT or PLT entries explicitly.
2613         case elfcpp::R_ARM_GOT_BREL:
2614         case elfcpp::R_ARM_GOT_ABS:
2615         case elfcpp::R_ARM_GOT_PREL:
2616         case elfcpp::R_ARM_GOT_BREL12:
2617         case elfcpp::R_ARM_PLT32_ABS:
2618         case elfcpp::R_ARM_TLS_GD32:
2619         case elfcpp::R_ARM_TLS_LDM32:
2620         case elfcpp::R_ARM_TLS_IE32:
2621         case elfcpp::R_ARM_TLS_IE12GP:
2622
2623         // These relocate types may use PLT entries.
2624         case elfcpp::R_ARM_CALL:
2625         case elfcpp::R_ARM_THM_CALL:
2626         case elfcpp::R_ARM_JUMP24:
2627         case elfcpp::R_ARM_THM_JUMP24:
2628         case elfcpp::R_ARM_THM_JUMP19:
2629         case elfcpp::R_ARM_PLT32:
2630         case elfcpp::R_ARM_THM_XPC22:
2631         case elfcpp::R_ARM_PREL31:
2632         case elfcpp::R_ARM_SBREL31:
2633           return false;
2634
2635         default:
2636           return true;
2637         }
2638     }
2639
2640    private:
2641     // Do a TLS relocation.
2642     inline typename Arm_relocate_functions<big_endian>::Status
2643     relocate_tls(const Relocate_info<32, big_endian>*, Target_arm<big_endian>*,
2644                  size_t, const elfcpp::Rel<32, big_endian>&, unsigned int,
2645                  const Sized_symbol<32>*, const Symbol_value<32>*,
2646                  unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
2647                  section_size_type);
2648
2649   };
2650
2651   // A class which returns the size required for a relocation type,
2652   // used while scanning relocs during a relocatable link.
2653   class Relocatable_size_for_reloc
2654   {
2655    public:
2656     unsigned int
2657     get_size_for_reloc(unsigned int, Relobj*);
2658   };
2659
2660   // Adjust TLS relocation type based on the options and whether this
2661   // is a local symbol.
2662   static tls::Tls_optimization
2663   optimize_tls_reloc(bool is_final, int r_type);
2664
2665   // Get the GOT section, creating it if necessary.
2666   Arm_output_data_got<big_endian>*
2667   got_section(Symbol_table*, Layout*);
2668
2669   // Get the GOT PLT section.
2670   Output_data_space*
2671   got_plt_section() const
2672   {
2673     gold_assert(this->got_plt_ != NULL);
2674     return this->got_plt_;
2675   }
2676
2677   // Create a PLT entry for a global symbol.
2678   void
2679   make_plt_entry(Symbol_table*, Layout*, Symbol*);
2680
2681   // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2682   void
2683   define_tls_base_symbol(Symbol_table*, Layout*);
2684
2685   // Create a GOT entry for the TLS module index.
2686   unsigned int
2687   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2688                       Sized_relobj<32, big_endian>* object);
2689
2690   // Get the PLT section.
2691   const Output_data_plt_arm<big_endian>*
2692   plt_section() const
2693   {
2694     gold_assert(this->plt_ != NULL);
2695     return this->plt_;
2696   }
2697
2698   // Get the dynamic reloc section, creating it if necessary.
2699   Reloc_section*
2700   rel_dyn_section(Layout*);
2701
2702   // Get the section to use for TLS_DESC relocations.
2703   Reloc_section*
2704   rel_tls_desc_section(Layout*) const;
2705
2706   // Return true if the symbol may need a COPY relocation.
2707   // References from an executable object to non-function symbols
2708   // defined in a dynamic object may need a COPY relocation.
2709   bool
2710   may_need_copy_reloc(Symbol* gsym)
2711   {
2712     return (gsym->type() != elfcpp::STT_ARM_TFUNC
2713             && gsym->may_need_copy_reloc());
2714   }
2715
2716   // Add a potential copy relocation.
2717   void
2718   copy_reloc(Symbol_table* symtab, Layout* layout,
2719              Sized_relobj<32, big_endian>* object,
2720              unsigned int shndx, Output_section* output_section,
2721              Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
2722   {
2723     this->copy_relocs_.copy_reloc(symtab, layout,
2724                                   symtab->get_sized_symbol<32>(sym),
2725                                   object, shndx, output_section, reloc,
2726                                   this->rel_dyn_section(layout));
2727   }
2728
2729   // Whether two EABI versions are compatible.
2730   static bool
2731   are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
2732
2733   // Merge processor-specific flags from input object and those in the ELF
2734   // header of the output.
2735   void
2736   merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
2737
2738   // Get the secondary compatible architecture.
2739   static int
2740   get_secondary_compatible_arch(const Attributes_section_data*);
2741
2742   // Set the secondary compatible architecture.
2743   static void
2744   set_secondary_compatible_arch(Attributes_section_data*, int);
2745
2746   static int
2747   tag_cpu_arch_combine(const char*, int, int*, int, int);
2748
2749   // Helper to print AEABI enum tag value.
2750   static std::string
2751   aeabi_enum_name(unsigned int);
2752
2753   // Return string value for TAG_CPU_name.
2754   static std::string
2755   tag_cpu_name_value(unsigned int);
2756
2757   // Merge object attributes from input object and those in the output.
2758   void
2759   merge_object_attributes(const char*, const Attributes_section_data*);
2760
2761   // Helper to get an AEABI object attribute
2762   Object_attribute*
2763   get_aeabi_object_attribute(int tag) const
2764   {
2765     Attributes_section_data* pasd = this->attributes_section_data_;
2766     gold_assert(pasd != NULL);
2767     Object_attribute* attr =
2768       pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
2769     gold_assert(attr != NULL);
2770     return attr;
2771   }
2772
2773   //
2774   // Methods to support stub-generations.
2775   //
2776
2777   // Group input sections for stub generation.
2778   void
2779   group_sections(Layout*, section_size_type, bool);
2780
2781   // Scan a relocation for stub generation.
2782   void
2783   scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
2784                       const Sized_symbol<32>*, unsigned int,
2785                       const Symbol_value<32>*,
2786                       elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
2787
2788   // Scan a relocation section for stub.
2789   template<int sh_type>
2790   void
2791   scan_reloc_section_for_stubs(
2792       const Relocate_info<32, big_endian>* relinfo,
2793       const unsigned char* prelocs,
2794       size_t reloc_count,
2795       Output_section* output_section,
2796       bool needs_special_offset_handling,
2797       const unsigned char* view,
2798       elfcpp::Elf_types<32>::Elf_Addr view_address,
2799       section_size_type);
2800
2801   // Fix .ARM.exidx section coverage.
2802   void
2803   fix_exidx_coverage(Layout*, const Input_objects*,
2804                      Arm_output_section<big_endian>*, Symbol_table*);
2805
2806   // Functors for STL set.
2807   struct output_section_address_less_than
2808   {
2809     bool
2810     operator()(const Output_section* s1, const Output_section* s2) const
2811     { return s1->address() < s2->address(); }
2812   };
2813
2814   // Information about this specific target which we pass to the
2815   // general Target structure.
2816   static const Target::Target_info arm_info;
2817
2818   // The types of GOT entries needed for this platform.
2819   enum Got_type
2820   {
2821     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
2822     GOT_TYPE_TLS_NOFFSET = 1,   // GOT entry for negative TLS offset
2823     GOT_TYPE_TLS_OFFSET = 2,    // GOT entry for positive TLS offset
2824     GOT_TYPE_TLS_PAIR = 3,      // GOT entry for TLS module/offset pair
2825     GOT_TYPE_TLS_DESC = 4       // GOT entry for TLS_DESC pair
2826   };
2827
2828   typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
2829
2830   // Map input section to Arm_input_section.
2831   typedef Unordered_map<Section_id,
2832                         Arm_input_section<big_endian>*,
2833                         Section_id_hash>
2834           Arm_input_section_map;
2835     
2836   // Map output addresses to relocs for Cortex-A8 erratum.
2837   typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
2838           Cortex_a8_relocs_info;
2839
2840   // The GOT section.
2841   Arm_output_data_got<big_endian>* got_;
2842   // The PLT section.
2843   Output_data_plt_arm<big_endian>* plt_;
2844   // The GOT PLT section.
2845   Output_data_space* got_plt_;
2846   // The dynamic reloc section.
2847   Reloc_section* rel_dyn_;
2848   // Relocs saved to avoid a COPY reloc.
2849   Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
2850   // Space for variables copied with a COPY reloc.
2851   Output_data_space* dynbss_;
2852   // Offset of the GOT entry for the TLS module index.
2853   unsigned int got_mod_index_offset_;
2854   // True if the _TLS_MODULE_BASE_ symbol has been defined.
2855   bool tls_base_symbol_defined_;
2856   // Vector of Stub_tables created.
2857   Stub_table_list stub_tables_;
2858   // Stub factory.
2859   const Stub_factory &stub_factory_;
2860   // Whether we can use BLX.
2861   bool may_use_blx_;
2862   // Whether we force PIC branch veneers.
2863   bool should_force_pic_veneer_;
2864   // Map for locating Arm_input_sections.
2865   Arm_input_section_map arm_input_section_map_;
2866   // Attributes section data in output.
2867   Attributes_section_data* attributes_section_data_;
2868   // Whether we want to fix code for Cortex-A8 erratum.
2869   bool fix_cortex_a8_;
2870   // Map addresses to relocs for Cortex-A8 erratum.
2871   Cortex_a8_relocs_info cortex_a8_relocs_info_;
2872 };
2873
2874 template<bool big_endian>
2875 const Target::Target_info Target_arm<big_endian>::arm_info =
2876 {
2877   32,                   // size
2878   big_endian,           // is_big_endian
2879   elfcpp::EM_ARM,       // machine_code
2880   false,                // has_make_symbol
2881   false,                // has_resolve
2882   false,                // has_code_fill
2883   true,                 // is_default_stack_executable
2884   '\0',                 // wrap_char
2885   "/usr/lib/libc.so.1", // dynamic_linker
2886   0x8000,               // default_text_segment_address
2887   0x1000,               // abi_pagesize (overridable by -z max-page-size)
2888   0x1000,               // common_pagesize (overridable by -z common-page-size)
2889   elfcpp::SHN_UNDEF,    // small_common_shndx
2890   elfcpp::SHN_UNDEF,    // large_common_shndx
2891   0,                    // small_common_section_flags
2892   0,                    // large_common_section_flags
2893   ".ARM.attributes",    // attributes_section
2894   "aeabi"               // attributes_vendor
2895 };
2896
2897 // Arm relocate functions class
2898 //
2899
2900 template<bool big_endian>
2901 class Arm_relocate_functions : public Relocate_functions<32, big_endian>
2902 {
2903  public:
2904   typedef enum
2905   {
2906     STATUS_OKAY,        // No error during relocation.
2907     STATUS_OVERFLOW,    // Relocation oveflow.
2908     STATUS_BAD_RELOC    // Relocation cannot be applied.
2909   } Status;
2910
2911  private:
2912   typedef Relocate_functions<32, big_endian> Base;
2913   typedef Arm_relocate_functions<big_endian> This;
2914
2915   // Encoding of imm16 argument for movt and movw ARM instructions
2916   // from ARM ARM:
2917   //     
2918   //     imm16 := imm4 | imm12
2919   //
2920   //  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 
2921   // +-------+---------------+-------+-------+-----------------------+
2922   // |       |               |imm4   |       |imm12                  |
2923   // +-------+---------------+-------+-------+-----------------------+
2924
2925   // Extract the relocation addend from VAL based on the ARM
2926   // instruction encoding described above.
2927   static inline typename elfcpp::Swap<32, big_endian>::Valtype
2928   extract_arm_movw_movt_addend(
2929       typename elfcpp::Swap<32, big_endian>::Valtype val)
2930   {
2931     // According to the Elf ABI for ARM Architecture the immediate
2932     // field is sign-extended to form the addend.
2933     return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
2934   }
2935
2936   // Insert X into VAL based on the ARM instruction encoding described
2937   // above.
2938   static inline typename elfcpp::Swap<32, big_endian>::Valtype
2939   insert_val_arm_movw_movt(
2940       typename elfcpp::Swap<32, big_endian>::Valtype val,
2941       typename elfcpp::Swap<32, big_endian>::Valtype x)
2942   {
2943     val &= 0xfff0f000;
2944     val |= x & 0x0fff;
2945     val |= (x & 0xf000) << 4;
2946     return val;
2947   }
2948
2949   // Encoding of imm16 argument for movt and movw Thumb2 instructions
2950   // from ARM ARM:
2951   //     
2952   //     imm16 := imm4 | i | imm3 | imm8
2953   //
2954   //  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 
2955   // +---------+-+-----------+-------++-+-----+-------+---------------+
2956   // |         |i|           |imm4   || |imm3 |       |imm8           |
2957   // +---------+-+-----------+-------++-+-----+-------+---------------+
2958
2959   // Extract the relocation addend from VAL based on the Thumb2
2960   // instruction encoding described above.
2961   static inline typename elfcpp::Swap<32, big_endian>::Valtype
2962   extract_thumb_movw_movt_addend(
2963       typename elfcpp::Swap<32, big_endian>::Valtype val)
2964   {
2965     // According to the Elf ABI for ARM Architecture the immediate
2966     // field is sign-extended to form the addend.
2967     return utils::sign_extend<16>(((val >> 4) & 0xf000)
2968                                   | ((val >> 15) & 0x0800)
2969                                   | ((val >> 4) & 0x0700)
2970                                   | (val & 0x00ff));
2971   }
2972
2973   // Insert X into VAL based on the Thumb2 instruction encoding
2974   // described above.
2975   static inline typename elfcpp::Swap<32, big_endian>::Valtype
2976   insert_val_thumb_movw_movt(
2977       typename elfcpp::Swap<32, big_endian>::Valtype val,
2978       typename elfcpp::Swap<32, big_endian>::Valtype x)
2979   {
2980     val &= 0xfbf08f00;
2981     val |= (x & 0xf000) << 4;
2982     val |= (x & 0x0800) << 15;
2983     val |= (x & 0x0700) << 4;
2984     val |= (x & 0x00ff);
2985     return val;
2986   }
2987
2988   // Calculate the smallest constant Kn for the specified residual.
2989   // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
2990   static uint32_t
2991   calc_grp_kn(typename elfcpp::Swap<32, big_endian>::Valtype residual)
2992   {
2993     int32_t msb;
2994
2995     if (residual == 0)
2996       return 0;
2997     // Determine the most significant bit in the residual and
2998     // align the resulting value to a 2-bit boundary.
2999     for (msb = 30; (msb >= 0) && !(residual & (3 << msb)); msb -= 2)
3000       ;
3001     // The desired shift is now (msb - 6), or zero, whichever
3002     // is the greater.
3003     return (((msb - 6) < 0) ? 0 : (msb - 6));
3004   }
3005
3006   // Calculate the final residual for the specified group index.
3007   // If the passed group index is less than zero, the method will return
3008   // the value of the specified residual without any change.
3009   // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
3010   static typename elfcpp::Swap<32, big_endian>::Valtype
3011   calc_grp_residual(typename elfcpp::Swap<32, big_endian>::Valtype residual,
3012                     const int group)
3013   {
3014     for (int n = 0; n <= group; n++)
3015       {
3016         // Calculate which part of the value to mask.
3017         uint32_t shift = calc_grp_kn(residual);
3018         // Calculate the residual for the next time around.
3019         residual &= ~(residual & (0xff << shift));
3020       }
3021
3022     return residual;
3023   }
3024
3025   // Calculate the value of Gn for the specified group index.
3026   // We return it in the form of an encoded constant-and-rotation.
3027   // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
3028   static typename elfcpp::Swap<32, big_endian>::Valtype
3029   calc_grp_gn(typename elfcpp::Swap<32, big_endian>::Valtype residual,
3030               const int group)
3031   {
3032     typename elfcpp::Swap<32, big_endian>::Valtype gn = 0;
3033     uint32_t shift = 0;
3034
3035     for (int n = 0; n <= group; n++)
3036       {
3037         // Calculate which part of the value to mask.
3038         shift = calc_grp_kn(residual);
3039         // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
3040         gn = residual & (0xff << shift);
3041         // Calculate the residual for the next time around.
3042         residual &= ~gn;
3043       }
3044     // Return Gn in the form of an encoded constant-and-rotation.
3045     return ((gn >> shift) | ((gn <= 0xff ? 0 : (32 - shift) / 2) << 8));
3046   }
3047
3048  public:
3049   // Handle ARM long branches.
3050   static typename This::Status
3051   arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
3052                     unsigned char *, const Sized_symbol<32>*,
3053                     const Arm_relobj<big_endian>*, unsigned int,
3054                     const Symbol_value<32>*, Arm_address, Arm_address, bool);
3055
3056   // Handle THUMB long branches.
3057   static typename This::Status
3058   thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
3059                       unsigned char *, const Sized_symbol<32>*,
3060                       const Arm_relobj<big_endian>*, unsigned int,
3061                       const Symbol_value<32>*, Arm_address, Arm_address, bool);
3062
3063
3064   // Return the branch offset of a 32-bit THUMB branch.
3065   static inline int32_t
3066   thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
3067   {
3068     // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
3069     // involving the J1 and J2 bits.
3070     uint32_t s = (upper_insn & (1U << 10)) >> 10;
3071     uint32_t upper = upper_insn & 0x3ffU;
3072     uint32_t lower = lower_insn & 0x7ffU;
3073     uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
3074     uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
3075     uint32_t i1 = j1 ^ s ? 0 : 1;
3076     uint32_t i2 = j2 ^ s ? 0 : 1;
3077
3078     return utils::sign_extend<25>((s << 24) | (i1 << 23) | (i2 << 22)
3079                                   | (upper << 12) | (lower << 1));
3080   }
3081
3082   // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
3083   // UPPER_INSN is the original upper instruction of the branch.  Caller is
3084   // responsible for overflow checking and BLX offset adjustment.
3085   static inline uint16_t
3086   thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
3087   {
3088     uint32_t s = offset < 0 ? 1 : 0;
3089     uint32_t bits = static_cast<uint32_t>(offset);
3090     return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
3091   }
3092
3093   // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
3094   // LOWER_INSN is the original lower instruction of the branch.  Caller is
3095   // responsible for overflow checking and BLX offset adjustment.
3096   static inline uint16_t
3097   thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
3098   {
3099     uint32_t s = offset < 0 ? 1 : 0;
3100     uint32_t bits = static_cast<uint32_t>(offset);
3101     return ((lower_insn & ~0x2fffU)
3102             | ((((bits >> 23) & 1) ^ !s) << 13)
3103             | ((((bits >> 22) & 1) ^ !s) << 11)
3104             | ((bits >> 1) & 0x7ffU));
3105   }
3106
3107   // Return the branch offset of a 32-bit THUMB conditional branch.
3108   static inline int32_t
3109   thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
3110   {
3111     uint32_t s = (upper_insn & 0x0400U) >> 10;
3112     uint32_t j1 = (lower_insn & 0x2000U) >> 13;
3113     uint32_t j2 = (lower_insn & 0x0800U) >> 11;
3114     uint32_t lower = (lower_insn & 0x07ffU);
3115     uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
3116
3117     return utils::sign_extend<21>((upper << 12) | (lower << 1));
3118   }
3119
3120   // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
3121   // instruction.  UPPER_INSN is the original upper instruction of the branch.
3122   // Caller is responsible for overflow checking.
3123   static inline uint16_t
3124   thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
3125   {
3126     uint32_t s = offset < 0 ? 1 : 0;
3127     uint32_t bits = static_cast<uint32_t>(offset);
3128     return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
3129   }
3130
3131   // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
3132   // instruction.  LOWER_INSN is the original lower instruction of the branch.
3133   // Caller is reponsible for overflow checking.
3134   static inline uint16_t
3135   thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
3136   {
3137     uint32_t bits = static_cast<uint32_t>(offset);
3138     uint32_t j2 = (bits & 0x00080000U) >> 19;
3139     uint32_t j1 = (bits & 0x00040000U) >> 18;
3140     uint32_t lo = (bits & 0x00000ffeU) >> 1;
3141
3142     return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
3143   }
3144
3145   // R_ARM_ABS8: S + A
3146   static inline typename This::Status
3147   abs8(unsigned char *view,
3148        const Sized_relobj<32, big_endian>* object,
3149        const Symbol_value<32>* psymval)
3150   {
3151     typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
3152     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3153     Valtype* wv = reinterpret_cast<Valtype*>(view);
3154     Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
3155     Reltype addend = utils::sign_extend<8>(val);
3156     Reltype x = psymval->value(object, addend);
3157     val = utils::bit_select(val, x, 0xffU);
3158     elfcpp::Swap<8, big_endian>::writeval(wv, val);
3159
3160     // R_ARM_ABS8 permits signed or unsigned results.
3161     int signed_x = static_cast<int32_t>(x);
3162     return ((signed_x < -128 || signed_x > 255)
3163             ? This::STATUS_OVERFLOW
3164             : This::STATUS_OKAY);
3165   }
3166
3167   // R_ARM_THM_ABS5: S + A
3168   static inline typename This::Status
3169   thm_abs5(unsigned char *view,
3170        const Sized_relobj<32, big_endian>* object,
3171        const Symbol_value<32>* psymval)
3172   {
3173     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3174     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3175     Valtype* wv = reinterpret_cast<Valtype*>(view);
3176     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3177     Reltype addend = (val & 0x7e0U) >> 6;
3178     Reltype x = psymval->value(object, addend);
3179     val = utils::bit_select(val, x << 6, 0x7e0U);
3180     elfcpp::Swap<16, big_endian>::writeval(wv, val);
3181
3182     // R_ARM_ABS16 permits signed or unsigned results.
3183     int signed_x = static_cast<int32_t>(x);
3184     return ((signed_x < -32768 || signed_x > 65535)
3185             ? This::STATUS_OVERFLOW
3186             : This::STATUS_OKAY);
3187   }
3188
3189   // R_ARM_ABS12: S + A
3190   static inline typename This::Status
3191   abs12(unsigned char *view,
3192         const Sized_relobj<32, big_endian>* object,
3193         const Symbol_value<32>* psymval)
3194   {
3195     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3196     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3197     Valtype* wv = reinterpret_cast<Valtype*>(view);
3198     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3199     Reltype addend = val & 0x0fffU;
3200     Reltype x = psymval->value(object, addend);
3201     val = utils::bit_select(val, x, 0x0fffU);
3202     elfcpp::Swap<32, big_endian>::writeval(wv, val);
3203     return (utils::has_overflow<12>(x)
3204             ? This::STATUS_OVERFLOW
3205             : This::STATUS_OKAY);
3206   }
3207
3208   // R_ARM_ABS16: S + A
3209   static inline typename This::Status
3210   abs16(unsigned char *view,
3211         const Sized_relobj<32, big_endian>* object,
3212         const Symbol_value<32>* psymval)
3213   {
3214     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3215     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3216     Valtype* wv = reinterpret_cast<Valtype*>(view);
3217     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3218     Reltype addend = utils::sign_extend<16>(val);
3219     Reltype x = psymval->value(object, addend);
3220     val = utils::bit_select(val, x, 0xffffU);
3221     elfcpp::Swap<16, big_endian>::writeval(wv, val);
3222     return (utils::has_signed_unsigned_overflow<16>(x)
3223             ? This::STATUS_OVERFLOW
3224             : This::STATUS_OKAY);
3225   }
3226
3227   // R_ARM_ABS32: (S + A) | T
3228   static inline typename This::Status
3229   abs32(unsigned char *view,
3230         const Sized_relobj<32, big_endian>* object,
3231         const Symbol_value<32>* psymval,
3232         Arm_address thumb_bit)
3233   {
3234     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3235     Valtype* wv = reinterpret_cast<Valtype*>(view);
3236     Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
3237     Valtype x = psymval->value(object, addend) | thumb_bit;
3238     elfcpp::Swap<32, big_endian>::writeval(wv, x);
3239     return This::STATUS_OKAY;
3240   }
3241
3242   // R_ARM_REL32: (S + A) | T - P
3243   static inline typename This::Status
3244   rel32(unsigned char *view,
3245         const Sized_relobj<32, big_endian>* object,
3246         const Symbol_value<32>* psymval,
3247         Arm_address address,
3248         Arm_address thumb_bit)
3249   {
3250     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3251     Valtype* wv = reinterpret_cast<Valtype*>(view);
3252     Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
3253     Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3254     elfcpp::Swap<32, big_endian>::writeval(wv, x);
3255     return This::STATUS_OKAY;
3256   }
3257
3258   // R_ARM_THM_JUMP24: (S + A) | T - P
3259   static typename This::Status
3260   thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
3261              const Symbol_value<32>* psymval, Arm_address address,
3262              Arm_address thumb_bit);
3263
3264   // R_ARM_THM_JUMP6: S + A â€“ P
3265   static inline typename This::Status
3266   thm_jump6(unsigned char *view,
3267             const Sized_relobj<32, big_endian>* object,
3268             const Symbol_value<32>* psymval,
3269             Arm_address address)
3270   {
3271     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3272     typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3273     Valtype* wv = reinterpret_cast<Valtype*>(view);
3274     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3275     // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
3276     Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
3277     Reltype x = (psymval->value(object, addend) - address);
3278     val = (val & 0xfd07) | ((x  & 0x0040) << 3) | ((val & 0x003e) << 2);
3279     elfcpp::Swap<16, big_endian>::writeval(wv, val);
3280     // CZB does only forward jumps.
3281     return ((x > 0x007e)
3282             ? This::STATUS_OVERFLOW
3283             : This::STATUS_OKAY);
3284   }
3285
3286   // R_ARM_THM_JUMP8: S + A â€“ P
3287   static inline typename This::Status
3288   thm_jump8(unsigned char *view,
3289             const Sized_relobj<32, big_endian>* object,
3290             const Symbol_value<32>* psymval,
3291             Arm_address address)
3292   {
3293     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3294     typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3295     Valtype* wv = reinterpret_cast<Valtype*>(view);
3296     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3297     Reltype addend = utils::sign_extend<8>((val & 0x00ff) << 1);
3298     Reltype x = (psymval->value(object, addend) - address);
3299     elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xff00) | ((x & 0x01fe) >> 1));
3300     return (utils::has_overflow<8>(x)
3301             ? This::STATUS_OVERFLOW
3302             : This::STATUS_OKAY);
3303   }
3304
3305   // R_ARM_THM_JUMP11: S + A â€“ P
3306   static inline typename This::Status
3307   thm_jump11(unsigned char *view,
3308             const Sized_relobj<32, big_endian>* object,
3309             const Symbol_value<32>* psymval,
3310             Arm_address address)
3311   {
3312     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3313     typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3314     Valtype* wv = reinterpret_cast<Valtype*>(view);
3315     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
3316     Reltype addend = utils::sign_extend<11>((val & 0x07ff) << 1);
3317     Reltype x = (psymval->value(object, addend) - address);
3318     elfcpp::Swap<16, big_endian>::writeval(wv, (val & 0xf800) | ((x & 0x0ffe) >> 1));
3319     return (utils::has_overflow<11>(x)
3320             ? This::STATUS_OVERFLOW
3321             : This::STATUS_OKAY);
3322   }
3323
3324   // R_ARM_BASE_PREL: B(S) + A - P
3325   static inline typename This::Status
3326   base_prel(unsigned char* view,
3327             Arm_address origin,
3328             Arm_address address)
3329   {
3330     Base::rel32(view, origin - address);
3331     return STATUS_OKAY;
3332   }
3333
3334   // R_ARM_BASE_ABS: B(S) + A
3335   static inline typename This::Status
3336   base_abs(unsigned char* view,
3337            Arm_address origin)
3338   {
3339     Base::rel32(view, origin);
3340     return STATUS_OKAY;
3341   }
3342
3343   // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
3344   static inline typename This::Status
3345   got_brel(unsigned char* view,
3346            typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
3347   {
3348     Base::rel32(view, got_offset);
3349     return This::STATUS_OKAY;
3350   }
3351
3352   // R_ARM_GOT_PREL: GOT(S) + A - P
3353   static inline typename This::Status
3354   got_prel(unsigned char *view,
3355            Arm_address got_entry,
3356            Arm_address address)
3357   {
3358     Base::rel32(view, got_entry - address);
3359     return This::STATUS_OKAY;
3360   }
3361
3362   // R_ARM_PREL: (S + A) | T - P
3363   static inline typename This::Status
3364   prel31(unsigned char *view,
3365          const Sized_relobj<32, big_endian>* object,
3366          const Symbol_value<32>* psymval,
3367          Arm_address address,
3368          Arm_address thumb_bit)
3369   {
3370     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3371     Valtype* wv = reinterpret_cast<Valtype*>(view);
3372     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3373     Valtype addend = utils::sign_extend<31>(val);
3374     Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
3375     val = utils::bit_select(val, x, 0x7fffffffU);
3376     elfcpp::Swap<32, big_endian>::writeval(wv, val);
3377     return (utils::has_overflow<31>(x) ?
3378             This::STATUS_OVERFLOW : This::STATUS_OKAY);
3379   }
3380
3381   // R_ARM_MOVW_ABS_NC: (S + A) | T     (relative address base is )
3382   // R_ARM_MOVW_PREL_NC: (S + A) | T - P
3383   // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3384   // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
3385   static inline typename This::Status
3386   movw(unsigned char* view,
3387        const Sized_relobj<32, big_endian>* object,
3388        const Symbol_value<32>* psymval,
3389        Arm_address relative_address_base,
3390        Arm_address thumb_bit,
3391        bool check_overflow)
3392   {
3393     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3394     Valtype* wv = reinterpret_cast<Valtype*>(view);
3395     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3396     Valtype addend = This::extract_arm_movw_movt_addend(val);
3397     Valtype x = ((psymval->value(object, addend) | thumb_bit)
3398                  - relative_address_base);
3399     val = This::insert_val_arm_movw_movt(val, x);
3400     elfcpp::Swap<32, big_endian>::writeval(wv, val);
3401     return ((check_overflow && utils::has_overflow<16>(x))
3402             ? This::STATUS_OVERFLOW
3403             : This::STATUS_OKAY);
3404   }
3405
3406   // R_ARM_MOVT_ABS: S + A      (relative address base is 0)
3407   // R_ARM_MOVT_PREL: S + A - P
3408   // R_ARM_MOVT_BREL: S + A - B(S)
3409   static inline typename This::Status
3410   movt(unsigned char* view,
3411        const Sized_relobj<32, big_endian>* object,
3412        const Symbol_value<32>* psymval,
3413        Arm_address relative_address_base)
3414   {
3415     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3416     Valtype* wv = reinterpret_cast<Valtype*>(view);
3417     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3418     Valtype addend = This::extract_arm_movw_movt_addend(val);
3419     Valtype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3420     val = This::insert_val_arm_movw_movt(val, x);
3421     elfcpp::Swap<32, big_endian>::writeval(wv, val);
3422     // FIXME: IHI0044D says that we should check for overflow.
3423     return This::STATUS_OKAY;
3424   }
3425
3426   // R_ARM_THM_MOVW_ABS_NC: S + A | T           (relative_address_base is 0)
3427   // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
3428   // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
3429   // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
3430   static inline typename This::Status
3431   thm_movw(unsigned char *view,
3432            const Sized_relobj<32, big_endian>* object,
3433            const Symbol_value<32>* psymval,
3434            Arm_address relative_address_base,
3435            Arm_address thumb_bit,
3436            bool check_overflow)
3437   {
3438     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3439     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3440     Valtype* wv = reinterpret_cast<Valtype*>(view);
3441     Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3442                   | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3443     Reltype addend = This::extract_thumb_movw_movt_addend(val);
3444     Reltype x =
3445       (psymval->value(object, addend) | thumb_bit) - relative_address_base;
3446     val = This::insert_val_thumb_movw_movt(val, x);
3447     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3448     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3449     return ((check_overflow && utils::has_overflow<16>(x))
3450             ? This::STATUS_OVERFLOW
3451             : This::STATUS_OKAY);
3452   }
3453
3454   // R_ARM_THM_MOVT_ABS: S + A          (relative address base is 0)
3455   // R_ARM_THM_MOVT_PREL: S + A - P
3456   // R_ARM_THM_MOVT_BREL: S + A - B(S)
3457   static inline typename This::Status
3458   thm_movt(unsigned char* view,
3459            const Sized_relobj<32, big_endian>* object,
3460            const Symbol_value<32>* psymval,
3461            Arm_address relative_address_base)
3462   {
3463     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3464     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3465     Valtype* wv = reinterpret_cast<Valtype*>(view);
3466     Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3467                   | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3468     Reltype addend = This::extract_thumb_movw_movt_addend(val);
3469     Reltype x = (psymval->value(object, addend) - relative_address_base) >> 16;
3470     val = This::insert_val_thumb_movw_movt(val, x);
3471     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
3472     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
3473     return This::STATUS_OKAY;
3474   }
3475
3476   // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
3477   static inline typename This::Status
3478   thm_alu11(unsigned char* view,
3479             const Sized_relobj<32, big_endian>* object,
3480             const Symbol_value<32>* psymval,
3481             Arm_address address,
3482             Arm_address thumb_bit)
3483   {
3484     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3485     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3486     Valtype* wv = reinterpret_cast<Valtype*>(view);
3487     Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3488                    | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3489
3490     //        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
3491     // -----------------------------------------------------------------------
3492     // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd     |imm8
3493     // ADDW   1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd     |imm8
3494     // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd     |imm8
3495     // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd     |imm8
3496     // SUBW   1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd     |imm8
3497     // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd     |imm8
3498
3499     // Determine a sign for the addend.
3500     const int sign = ((insn & 0xf8ef0000) == 0xf0ad0000
3501                       || (insn & 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
3502     // Thumb2 addend encoding:
3503     // imm12 := i | imm3 | imm8
3504     int32_t addend = (insn & 0xff)
3505                      | ((insn & 0x00007000) >> 4)
3506                      | ((insn & 0x04000000) >> 15);
3507     // Apply a sign to the added.
3508     addend *= sign;
3509
3510     int32_t x = (psymval->value(object, addend) | thumb_bit)
3511                 - (address & 0xfffffffc);
3512     Reltype val = abs(x);
3513     // Mask out the value and a distinct part of the ADD/SUB opcode
3514     // (bits 7:5 of opword).
3515     insn = (insn & 0xfb0f8f00)
3516            | (val & 0xff)
3517            | ((val & 0x700) << 4)
3518            | ((val & 0x800) << 15);
3519     // Set the opcode according to whether the value to go in the
3520     // place is negative.
3521     if (x < 0)
3522       insn |= 0x00a00000;
3523
3524     elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3525     elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3526     return ((val > 0xfff) ?
3527             This::STATUS_OVERFLOW : This::STATUS_OKAY);
3528   }
3529
3530   // R_ARM_THM_PC8: S + A - Pa (Thumb)
3531   static inline typename This::Status
3532   thm_pc8(unsigned char* view,
3533           const Sized_relobj<32, big_endian>* object,
3534           const Symbol_value<32>* psymval,
3535           Arm_address address)
3536   {
3537     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3538     typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
3539     Valtype* wv = reinterpret_cast<Valtype*>(view);
3540     Valtype insn = elfcpp::Swap<16, big_endian>::readval(wv);
3541     Reltype addend = ((insn & 0x00ff) << 2);
3542     int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3543     Reltype val = abs(x);
3544     insn = (insn & 0xff00) | ((val & 0x03fc) >> 2);
3545
3546     elfcpp::Swap<16, big_endian>::writeval(wv, insn);
3547     return ((val > 0x03fc)
3548             ? This::STATUS_OVERFLOW
3549             : This::STATUS_OKAY);
3550   }
3551
3552   // R_ARM_THM_PC12: S + A - Pa (Thumb32)
3553   static inline typename This::Status
3554   thm_pc12(unsigned char* view,
3555            const Sized_relobj<32, big_endian>* object,
3556            const Symbol_value<32>* psymval,
3557            Arm_address address)
3558   {
3559     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3560     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
3561     Valtype* wv = reinterpret_cast<Valtype*>(view);
3562     Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
3563                    | elfcpp::Swap<16, big_endian>::readval(wv + 1);
3564     // Determine a sign for the addend (positive if the U bit is 1).
3565     const int sign = (insn & 0x00800000) ? 1 : -1;
3566     int32_t addend = (insn & 0xfff);
3567     // Apply a sign to the added.
3568     addend *= sign;
3569
3570     int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
3571     Reltype val = abs(x);
3572     // Mask out and apply the value and the U bit.
3573     insn = (insn & 0xff7ff000) | (val & 0xfff);
3574     // Set the U bit according to whether the value to go in the
3575     // place is positive.
3576     if (x >= 0)
3577       insn |= 0x00800000;
3578
3579     elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
3580     elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
3581     return ((val > 0xfff) ?
3582             This::STATUS_OVERFLOW : This::STATUS_OKAY);
3583   }
3584
3585   // R_ARM_V4BX
3586   static inline typename This::Status
3587   v4bx(const Relocate_info<32, big_endian>* relinfo,
3588        unsigned char *view,
3589        const Arm_relobj<big_endian>* object,
3590        const Arm_address address,
3591        const bool is_interworking)
3592   {
3593
3594     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3595     Valtype* wv = reinterpret_cast<Valtype*>(view);
3596     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3597
3598     // Ensure that we have a BX instruction.
3599     gold_assert((val & 0x0ffffff0) == 0x012fff10);
3600     const uint32_t reg = (val & 0xf);
3601     if (is_interworking && reg != 0xf)
3602       {
3603         Stub_table<big_endian>* stub_table =
3604             object->stub_table(relinfo->data_shndx);
3605         gold_assert(stub_table != NULL);
3606
3607         Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
3608         gold_assert(stub != NULL);
3609
3610         int32_t veneer_address =
3611             stub_table->address() + stub->offset() - 8 - address;
3612         gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
3613                     && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
3614         // Replace with a branch to veneer (B <addr>)
3615         val = (val & 0xf0000000) | 0x0a000000
3616               | ((veneer_address >> 2) & 0x00ffffff);
3617       }
3618     else
3619       {
3620         // Preserve Rm (lowest four bits) and the condition code
3621         // (highest four bits). Other bits encode MOV PC,Rm.
3622         val = (val & 0xf000000f) | 0x01a0f000;
3623       }
3624     elfcpp::Swap<32, big_endian>::writeval(wv, val);
3625     return This::STATUS_OKAY;
3626   }
3627
3628   // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
3629   // R_ARM_ALU_PC_G0:    ((S + A) | T) - P
3630   // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
3631   // R_ARM_ALU_PC_G1:    ((S + A) | T) - P
3632   // R_ARM_ALU_PC_G2:    ((S + A) | T) - P
3633   // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
3634   // R_ARM_ALU_SB_G0:    ((S + A) | T) - B(S)
3635   // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
3636   // R_ARM_ALU_SB_G1:    ((S + A) | T) - B(S)
3637   // R_ARM_ALU_SB_G2:    ((S + A) | T) - B(S)
3638   static inline typename This::Status
3639   arm_grp_alu(unsigned char* view,
3640         const Sized_relobj<32, big_endian>* object,
3641         const Symbol_value<32>* psymval,
3642         const int group,
3643         Arm_address address,
3644         Arm_address thumb_bit,
3645         bool check_overflow)
3646   {
3647     gold_assert(group >= 0 && group < 3);
3648     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3649     Valtype* wv = reinterpret_cast<Valtype*>(view);
3650     Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3651
3652     // ALU group relocations are allowed only for the ADD/SUB instructions.
3653     // (0x00800000 - ADD, 0x00400000 - SUB)
3654     const Valtype opcode = insn & 0x01e00000;
3655     if (opcode != 0x00800000 && opcode != 0x00400000)
3656       return This::STATUS_BAD_RELOC;
3657
3658     // Determine a sign for the addend.
3659     const int sign = (opcode == 0x00800000) ? 1 : -1;
3660     // shifter = rotate_imm * 2
3661     const uint32_t shifter = (insn & 0xf00) >> 7;
3662     // Initial addend value.
3663     int32_t addend = insn & 0xff;
3664     // Rotate addend right by shifter.
3665     addend = (addend >> shifter) | (addend << (32 - shifter));
3666     // Apply a sign to the added.
3667     addend *= sign;
3668
3669     int32_t x = ((psymval->value(object, addend) | thumb_bit) - address);
3670     Valtype gn = Arm_relocate_functions::calc_grp_gn(abs(x), group);
3671     // Check for overflow if required
3672     if (check_overflow
3673         && (Arm_relocate_functions::calc_grp_residual(abs(x), group) != 0))
3674       return This::STATUS_OVERFLOW;
3675
3676     // Mask out the value and the ADD/SUB part of the opcode; take care
3677     // not to destroy the S bit.
3678     insn &= 0xff1ff000;
3679     // Set the opcode according to whether the value to go in the
3680     // place is negative.
3681     insn |= ((x < 0) ? 0x00400000 : 0x00800000);
3682     // Encode the offset (encoded Gn).
3683     insn |= gn;
3684
3685     elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3686     return This::STATUS_OKAY;
3687   }
3688
3689   // R_ARM_LDR_PC_G0: S + A - P
3690   // R_ARM_LDR_PC_G1: S + A - P
3691   // R_ARM_LDR_PC_G2: S + A - P
3692   // R_ARM_LDR_SB_G0: S + A - B(S)
3693   // R_ARM_LDR_SB_G1: S + A - B(S)
3694   // R_ARM_LDR_SB_G2: S + A - B(S)
3695   static inline typename This::Status
3696   arm_grp_ldr(unsigned char* view,
3697         const Sized_relobj<32, big_endian>* object,
3698         const Symbol_value<32>* psymval,
3699         const int group,
3700         Arm_address address)
3701   {
3702     gold_assert(group >= 0 && group < 3);
3703     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3704     Valtype* wv = reinterpret_cast<Valtype*>(view);
3705     Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3706
3707     const int sign = (insn & 0x00800000) ? 1 : -1;
3708     int32_t addend = (insn & 0xfff) * sign;
3709     int32_t x = (psymval->value(object, addend) - address);
3710     // Calculate the relevant G(n-1) value to obtain this stage residual.
3711     Valtype residual =
3712         Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3713     if (residual >= 0x1000)
3714       return This::STATUS_OVERFLOW;
3715
3716     // Mask out the value and U bit.
3717     insn &= 0xff7ff000;
3718     // Set the U bit for non-negative values.
3719     if (x >= 0)
3720       insn |= 0x00800000;
3721     insn |= residual;
3722
3723     elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3724     return This::STATUS_OKAY;
3725   }
3726
3727   // R_ARM_LDRS_PC_G0: S + A - P
3728   // R_ARM_LDRS_PC_G1: S + A - P
3729   // R_ARM_LDRS_PC_G2: S + A - P
3730   // R_ARM_LDRS_SB_G0: S + A - B(S)
3731   // R_ARM_LDRS_SB_G1: S + A - B(S)
3732   // R_ARM_LDRS_SB_G2: S + A - B(S)
3733   static inline typename This::Status
3734   arm_grp_ldrs(unsigned char* view,
3735         const Sized_relobj<32, big_endian>* object,
3736         const Symbol_value<32>* psymval,
3737         const int group,
3738         Arm_address address)
3739   {
3740     gold_assert(group >= 0 && group < 3);
3741     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3742     Valtype* wv = reinterpret_cast<Valtype*>(view);
3743     Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3744
3745     const int sign = (insn & 0x00800000) ? 1 : -1;
3746     int32_t addend = (((insn & 0xf00) >> 4) + (insn & 0xf)) * sign;
3747     int32_t x = (psymval->value(object, addend) - address);
3748     // Calculate the relevant G(n-1) value to obtain this stage residual.
3749     Valtype residual =
3750         Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3751    if (residual >= 0x100)
3752       return This::STATUS_OVERFLOW;
3753
3754     // Mask out the value and U bit.
3755     insn &= 0xff7ff0f0;
3756     // Set the U bit for non-negative values.
3757     if (x >= 0)
3758       insn |= 0x00800000;
3759     insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
3760
3761     elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3762     return This::STATUS_OKAY;
3763   }
3764
3765   // R_ARM_LDC_PC_G0: S + A - P
3766   // R_ARM_LDC_PC_G1: S + A - P
3767   // R_ARM_LDC_PC_G2: S + A - P
3768   // R_ARM_LDC_SB_G0: S + A - B(S)
3769   // R_ARM_LDC_SB_G1: S + A - B(S)
3770   // R_ARM_LDC_SB_G2: S + A - B(S)
3771   static inline typename This::Status
3772   arm_grp_ldc(unsigned char* view,
3773       const Sized_relobj<32, big_endian>* object,
3774       const Symbol_value<32>* psymval,
3775       const int group,
3776       Arm_address address)
3777   {
3778     gold_assert(group >= 0 && group < 3);
3779     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3780     Valtype* wv = reinterpret_cast<Valtype*>(view);
3781     Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3782
3783     const int sign = (insn & 0x00800000) ? 1 : -1;
3784     int32_t addend = ((insn & 0xff) << 2) * sign;
3785     int32_t x = (psymval->value(object, addend) - address);
3786     // Calculate the relevant G(n-1) value to obtain this stage residual.
3787     Valtype residual =
3788       Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
3789     if ((residual & 0x3) != 0 || residual >= 0x400)
3790       return This::STATUS_OVERFLOW;
3791
3792     // Mask out the value and U bit.
3793     insn &= 0xff7fff00;
3794     // Set the U bit for non-negative values.
3795     if (x >= 0)
3796       insn |= 0x00800000;
3797     insn |= (residual >> 2);
3798
3799     elfcpp::Swap<32, big_endian>::writeval(wv, insn);
3800     return This::STATUS_OKAY;
3801   }
3802 };
3803
3804 // Relocate ARM long branches.  This handles relocation types
3805 // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
3806 // If IS_WEAK_UNDEFINED_WITH_PLT is true.  The target symbol is weakly
3807 // undefined and we do not use PLT in this relocation.  In such a case,
3808 // the branch is converted into an NOP.
3809
3810 template<bool big_endian>
3811 typename Arm_relocate_functions<big_endian>::Status
3812 Arm_relocate_functions<big_endian>::arm_branch_common(
3813     unsigned int r_type,
3814     const Relocate_info<32, big_endian>* relinfo,
3815     unsigned char *view,
3816     const Sized_symbol<32>* gsym,
3817     const Arm_relobj<big_endian>* object,
3818     unsigned int r_sym,
3819     const Symbol_value<32>* psymval,
3820     Arm_address address,
3821     Arm_address thumb_bit,
3822     bool is_weakly_undefined_without_plt)
3823 {
3824   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3825   Valtype* wv = reinterpret_cast<Valtype*>(view);
3826   Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3827      
3828   bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
3829                     && ((val & 0x0f000000UL) == 0x0a000000UL);
3830   bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
3831   bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
3832                           && ((val & 0x0f000000UL) == 0x0b000000UL);
3833   bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
3834   bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
3835
3836   // Check that the instruction is valid.
3837   if (r_type == elfcpp::R_ARM_CALL)
3838     {
3839       if (!insn_is_uncond_bl && !insn_is_blx)
3840         return This::STATUS_BAD_RELOC;
3841     }
3842   else if (r_type == elfcpp::R_ARM_JUMP24)
3843     {
3844       if (!insn_is_b && !insn_is_cond_bl)
3845         return This::STATUS_BAD_RELOC;
3846     }
3847   else if (r_type == elfcpp::R_ARM_PLT32)
3848     {
3849       if (!insn_is_any_branch)
3850         return This::STATUS_BAD_RELOC;
3851     }
3852   else if (r_type == elfcpp::R_ARM_XPC25)
3853     {
3854       // FIXME: AAELF document IH0044C does not say much about it other
3855       // than it being obsolete.
3856       if (!insn_is_any_branch)
3857         return This::STATUS_BAD_RELOC;
3858     }
3859   else
3860     gold_unreachable();
3861
3862   // A branch to an undefined weak symbol is turned into a jump to
3863   // the next instruction unless a PLT entry will be created.
3864   // Do the same for local undefined symbols.
3865   // The jump to the next instruction is optimized as a NOP depending
3866   // on the architecture.
3867   const Target_arm<big_endian>* arm_target =
3868     Target_arm<big_endian>::default_target();
3869   if (is_weakly_undefined_without_plt)
3870     {
3871       gold_assert(!parameters->options().relocatable());
3872       Valtype cond = val & 0xf0000000U;
3873       if (arm_target->may_use_arm_nop())
3874         val = cond | 0x0320f000;
3875       else
3876         val = cond | 0x01a00000;        // Using pre-UAL nop: mov r0, r0.
3877       elfcpp::Swap<32, big_endian>::writeval(wv, val);
3878       return This::STATUS_OKAY;
3879     }
3880  
3881   Valtype addend = utils::sign_extend<26>(val << 2);
3882   Valtype branch_target = psymval->value(object, addend);
3883   int32_t branch_offset = branch_target - address;
3884
3885   // We need a stub if the branch offset is too large or if we need
3886   // to switch mode.
3887   bool may_use_blx = arm_target->may_use_blx();
3888   Reloc_stub* stub = NULL;
3889
3890   if (!parameters->options().relocatable()
3891       && (utils::has_overflow<26>(branch_offset)
3892           || ((thumb_bit != 0)
3893               && !(may_use_blx && r_type == elfcpp::R_ARM_CALL))))
3894     {
3895       Valtype unadjusted_branch_target = psymval->value(object, 0);
3896
3897       Stub_type stub_type =
3898         Reloc_stub::stub_type_for_reloc(r_type, address,
3899                                         unadjusted_branch_target,
3900                                         (thumb_bit != 0));
3901       if (stub_type != arm_stub_none)
3902         {
3903           Stub_table<big_endian>* stub_table =
3904             object->stub_table(relinfo->data_shndx);
3905           gold_assert(stub_table != NULL);
3906
3907           Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
3908           stub = stub_table->find_reloc_stub(stub_key);
3909           gold_assert(stub != NULL);
3910           thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
3911           branch_target = stub_table->address() + stub->offset() + addend;
3912           branch_offset = branch_target - address;
3913           gold_assert(!utils::has_overflow<26>(branch_offset));
3914         }
3915     }
3916
3917   // At this point, if we still need to switch mode, the instruction
3918   // must either be a BLX or a BL that can be converted to a BLX.
3919   if (thumb_bit != 0)
3920     {
3921       // Turn BL to BLX.
3922       gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
3923       val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
3924     }
3925
3926   val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
3927   elfcpp::Swap<32, big_endian>::writeval(wv, val);
3928   return (utils::has_overflow<26>(branch_offset)
3929           ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
3930 }
3931
3932 // Relocate THUMB long branches.  This handles relocation types
3933 // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
3934 // If IS_WEAK_UNDEFINED_WITH_PLT is true.  The target symbol is weakly
3935 // undefined and we do not use PLT in this relocation.  In such a case,
3936 // the branch is converted into an NOP.
3937
3938 template<bool big_endian>
3939 typename Arm_relocate_functions<big_endian>::Status
3940 Arm_relocate_functions<big_endian>::thumb_branch_common(
3941     unsigned int r_type,
3942     const Relocate_info<32, big_endian>* relinfo,
3943     unsigned char *view,
3944     const Sized_symbol<32>* gsym,
3945     const Arm_relobj<big_endian>* object,
3946     unsigned int r_sym,
3947     const Symbol_value<32>* psymval,
3948     Arm_address address,
3949     Arm_address thumb_bit,
3950     bool is_weakly_undefined_without_plt)
3951 {
3952   typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
3953   Valtype* wv = reinterpret_cast<Valtype*>(view);
3954   uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
3955   uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
3956
3957   // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
3958   // into account.
3959   bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
3960   bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
3961      
3962   // Check that the instruction is valid.
3963   if (r_type == elfcpp::R_ARM_THM_CALL)
3964     {
3965       if (!is_bl_insn && !is_blx_insn)
3966         return This::STATUS_BAD_RELOC;
3967     }
3968   else if (r_type == elfcpp::R_ARM_THM_JUMP24)
3969     {
3970       // This cannot be a BLX.
3971       if (!is_bl_insn)
3972         return This::STATUS_BAD_RELOC;
3973     }
3974   else if (r_type == elfcpp::R_ARM_THM_XPC22)
3975     {
3976       // Check for Thumb to Thumb call.
3977       if (!is_blx_insn)
3978         return This::STATUS_BAD_RELOC;
3979       if (thumb_bit != 0)
3980         {
3981           gold_warning(_("%s: Thumb BLX instruction targets "
3982                          "thumb function '%s'."),
3983                          object->name().c_str(),
3984                          (gsym ? gsym->name() : "(local)")); 
3985           // Convert BLX to BL.
3986           lower_insn |= 0x1000U;
3987         }
3988     }
3989   else
3990     gold_unreachable();
3991
3992   // A branch to an undefined weak symbol is turned into a jump to
3993   // the next instruction unless a PLT entry will be created.
3994   // The jump to the next instruction is optimized as a NOP.W for
3995   // Thumb-2 enabled architectures.
3996   const Target_arm<big_endian>* arm_target =
3997     Target_arm<big_endian>::default_target();
3998   if (is_weakly_undefined_without_plt)
3999     {
4000       gold_assert(!parameters->options().relocatable());
4001       if (arm_target->may_use_thumb2_nop())
4002         {
4003           elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
4004           elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
4005         }
4006       else
4007         {
4008           elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
4009           elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
4010         }
4011       return This::STATUS_OKAY;
4012     }
4013  
4014   int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
4015   Arm_address branch_target = psymval->value(object, addend);
4016
4017   // For BLX, bit 1 of target address comes from bit 1 of base address.
4018   bool may_use_blx = arm_target->may_use_blx();
4019   if (thumb_bit == 0 && may_use_blx)
4020     branch_target = utils::bit_select(branch_target, address, 0x2);
4021
4022   int32_t branch_offset = branch_target - address;
4023
4024   // We need a stub if the branch offset is too large or if we need
4025   // to switch mode.
4026   bool thumb2 = arm_target->using_thumb2();
4027   if (!parameters->options().relocatable()
4028       && ((!thumb2 && utils::has_overflow<23>(branch_offset))
4029           || (thumb2 && utils::has_overflow<25>(branch_offset))
4030           || ((thumb_bit == 0)
4031               && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
4032                   || r_type == elfcpp::R_ARM_THM_JUMP24))))
4033     {
4034       Arm_address unadjusted_branch_target = psymval->value(object, 0);
4035
4036       Stub_type stub_type =
4037         Reloc_stub::stub_type_for_reloc(r_type, address,
4038                                         unadjusted_branch_target,
4039                                         (thumb_bit != 0));
4040
4041       if (stub_type != arm_stub_none)
4042         {
4043           Stub_table<big_endian>* stub_table =
4044             object->stub_table(relinfo->data_shndx);
4045           gold_assert(stub_table != NULL);
4046
4047           Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
4048           Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
4049           gold_assert(stub != NULL);
4050           thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
4051           branch_target = stub_table->address() + stub->offset() + addend;
4052           if (thumb_bit == 0 && may_use_blx) 
4053             branch_target = utils::bit_select(branch_target, address, 0x2);
4054           branch_offset = branch_target - address;
4055         }
4056     }
4057
4058   // At this point, if we still need to switch mode, the instruction
4059   // must either be a BLX or a BL that can be converted to a BLX.
4060   if (thumb_bit == 0)
4061     {
4062       gold_assert(may_use_blx
4063                   && (r_type == elfcpp::R_ARM_THM_CALL
4064                       || r_type == elfcpp::R_ARM_THM_XPC22));
4065       // Make sure this is a BLX.
4066       lower_insn &= ~0x1000U;
4067     }
4068   else
4069     {
4070       // Make sure this is a BL.
4071       lower_insn |= 0x1000U;
4072     }
4073
4074   // For a BLX instruction, make sure that the relocation is rounded up
4075   // to a word boundary.  This follows the semantics of the instruction
4076   // which specifies that bit 1 of the target address will come from bit
4077   // 1 of the base address.
4078   if ((lower_insn & 0x5000U) == 0x4000U)
4079     gold_assert((branch_offset & 3) == 0);
4080
4081   // Put BRANCH_OFFSET back into the insn.  Assumes two's complement.
4082   // We use the Thumb-2 encoding, which is safe even if dealing with
4083   // a Thumb-1 instruction by virtue of our overflow check above.  */
4084   upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
4085   lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
4086
4087   elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
4088   elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
4089
4090   gold_assert(!utils::has_overflow<25>(branch_offset));
4091
4092   return ((thumb2
4093            ? utils::has_overflow<25>(branch_offset)
4094            : utils::has_overflow<23>(branch_offset))
4095           ? This::STATUS_OVERFLOW
4096           : This::STATUS_OKAY);
4097 }
4098
4099 // Relocate THUMB-2 long conditional branches.
4100 // If IS_WEAK_UNDEFINED_WITH_PLT is true.  The target symbol is weakly
4101 // undefined and we do not use PLT in this relocation.  In such a case,
4102 // the branch is converted into an NOP.
4103
4104 template<bool big_endian>
4105 typename Arm_relocate_functions<big_endian>::Status
4106 Arm_relocate_functions<big_endian>::thm_jump19(
4107     unsigned char *view,
4108     const Arm_relobj<big_endian>* object,
4109     const Symbol_value<32>* psymval,
4110     Arm_address address,
4111     Arm_address thumb_bit)
4112 {
4113   typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
4114   Valtype* wv = reinterpret_cast<Valtype*>(view);
4115   uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
4116   uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
4117   int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
4118
4119   Arm_address branch_target = psymval->value(object, addend);
4120   int32_t branch_offset = branch_target - address;
4121
4122   // ??? Should handle interworking?  GCC might someday try to
4123   // use this for tail calls.
4124   // FIXME: We do support thumb entry to PLT yet.
4125   if (thumb_bit == 0)
4126     {
4127       gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
4128       return This::STATUS_BAD_RELOC;
4129     }
4130
4131   // Put RELOCATION back into the insn.
4132   upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
4133   lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
4134
4135   // Put the relocated value back in the object file:
4136   elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
4137   elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
4138
4139   return (utils::has_overflow<21>(branch_offset)
4140           ? This::STATUS_OVERFLOW
4141           : This::STATUS_OKAY);
4142 }
4143
4144 // Get the GOT section, creating it if necessary.
4145
4146 template<bool big_endian>
4147 Arm_output_data_got<big_endian>*
4148 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
4149 {
4150   if (this->got_ == NULL)
4151     {
4152       gold_assert(symtab != NULL && layout != NULL);
4153
4154       this->got_ = new Arm_output_data_got<big_endian>(symtab, layout);
4155
4156       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4157                                       (elfcpp::SHF_ALLOC
4158                                        | elfcpp::SHF_WRITE),
4159                                       this->got_, false, false, false, true);
4160       // The old GNU linker creates a .got.plt section.  We just
4161       // create another set of data in the .got section.  Note that we
4162       // always create a PLT if we create a GOT, although the PLT
4163       // might be empty.
4164       this->got_plt_ = new Output_data_space(4, "** GOT PLT");
4165       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
4166                                       (elfcpp::SHF_ALLOC
4167                                        | elfcpp::SHF_WRITE),
4168                                       this->got_plt_, false, false, false,
4169                                       false);
4170
4171       // The first three entries are reserved.
4172       this->got_plt_->set_current_data_size(3 * 4);
4173
4174       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
4175       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
4176                                     Symbol_table::PREDEFINED,
4177                                     this->got_plt_,
4178                                     0, 0, elfcpp::STT_OBJECT,
4179                                     elfcpp::STB_LOCAL,
4180                                     elfcpp::STV_HIDDEN, 0,
4181                                     false, false);
4182     }
4183   return this->got_;
4184 }
4185
4186 // Get the dynamic reloc section, creating it if necessary.
4187
4188 template<bool big_endian>
4189 typename Target_arm<big_endian>::Reloc_section*
4190 Target_arm<big_endian>::rel_dyn_section(Layout* layout)
4191 {
4192   if (this->rel_dyn_ == NULL)
4193     {
4194       gold_assert(layout != NULL);
4195       this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
4196       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
4197                                       elfcpp::SHF_ALLOC, this->rel_dyn_, true,
4198                                       false, false, false);
4199     }
4200   return this->rel_dyn_;
4201 }
4202
4203 // Insn_template methods.
4204
4205 // Return byte size of an instruction template.
4206
4207 size_t
4208 Insn_template::size() const
4209 {
4210   switch (this->type())
4211     {
4212     case THUMB16_TYPE:
4213     case THUMB16_SPECIAL_TYPE:
4214       return 2;
4215     case ARM_TYPE:
4216     case THUMB32_TYPE:
4217     case DATA_TYPE:
4218       return 4;
4219     default:
4220       gold_unreachable();
4221     }
4222 }
4223
4224 // Return alignment of an instruction template.
4225
4226 unsigned
4227 Insn_template::alignment() const
4228 {
4229   switch (this->type())
4230     {
4231     case THUMB16_TYPE:
4232     case THUMB16_SPECIAL_TYPE:
4233     case THUMB32_TYPE:
4234       return 2;
4235     case ARM_TYPE:
4236     case DATA_TYPE:
4237       return 4;
4238     default:
4239       gold_unreachable();
4240     }
4241 }
4242
4243 // Stub_template methods.
4244
4245 Stub_template::Stub_template(
4246     Stub_type type, const Insn_template* insns,
4247      size_t insn_count)
4248   : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
4249     entry_in_thumb_mode_(false), relocs_()
4250 {
4251   off_t offset = 0;
4252
4253   // Compute byte size and alignment of stub template.
4254   for (size_t i = 0; i < insn_count; i++)
4255     {
4256       unsigned insn_alignment = insns[i].alignment();
4257       size_t insn_size = insns[i].size();
4258       gold_assert((offset & (insn_alignment - 1)) == 0);
4259       this->alignment_ = std::max(this->alignment_, insn_alignment);
4260       switch (insns[i].type())
4261         {
4262         case Insn_template::THUMB16_TYPE:
4263         case Insn_template::THUMB16_SPECIAL_TYPE:
4264           if (i == 0)
4265             this->entry_in_thumb_mode_ = true;
4266           break;
4267
4268         case Insn_template::THUMB32_TYPE:
4269           if (insns[i].r_type() != elfcpp::R_ARM_NONE)
4270             this->relocs_.push_back(Reloc(i, offset));
4271           if (i == 0)
4272             this->entry_in_thumb_mode_ = true;
4273           break;
4274
4275         case Insn_template::ARM_TYPE:
4276           // Handle cases where the target is encoded within the
4277           // instruction.
4278           if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
4279             this->relocs_.push_back(Reloc(i, offset));
4280           break;
4281
4282         case Insn_template::DATA_TYPE:
4283           // Entry point cannot be data.
4284           gold_assert(i != 0);
4285           this->relocs_.push_back(Reloc(i, offset));
4286           break;
4287
4288         default:
4289           gold_unreachable();
4290         }
4291       offset += insn_size; 
4292     }
4293   this->size_ = offset;
4294 }
4295
4296 // Stub methods.
4297
4298 // Template to implement do_write for a specific target endianness.
4299
4300 template<bool big_endian>
4301 void inline
4302 Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
4303 {
4304   const Stub_template* stub_template = this->stub_template();
4305   const Insn_template* insns = stub_template->insns();
4306
4307   // FIXME:  We do not handle BE8 encoding yet.
4308   unsigned char* pov = view;
4309   for (size_t i = 0; i < stub_template->insn_count(); i++)
4310     {
4311       switch (insns[i].type())
4312         {
4313         case Insn_template::THUMB16_TYPE:
4314           elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
4315           break;
4316         case Insn_template::THUMB16_SPECIAL_TYPE:
4317           elfcpp::Swap<16, big_endian>::writeval(
4318               pov,
4319               this->thumb16_special(i));
4320           break;
4321         case Insn_template::THUMB32_TYPE:
4322           {
4323             uint32_t hi = (insns[i].data() >> 16) & 0xffff;
4324             uint32_t lo = insns[i].data() & 0xffff;
4325             elfcpp::Swap<16, big_endian>::writeval(pov, hi);
4326             elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
4327           }
4328           break;
4329         case Insn_template::ARM_TYPE:
4330         case Insn_template::DATA_TYPE:
4331           elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
4332           break;
4333         default:
4334           gold_unreachable();
4335         }
4336       pov += insns[i].size();
4337     }
4338   gold_assert(static_cast<section_size_type>(pov - view) == view_size);
4339
4340
4341 // Reloc_stub::Key methods.
4342
4343 // Dump a Key as a string for debugging.
4344
4345 std::string
4346 Reloc_stub::Key::name() const
4347 {
4348   if (this->r_sym_ == invalid_index)
4349     {
4350       // Global symbol key name
4351       // <stub-type>:<symbol name>:<addend>.
4352       const std::string sym_name = this->u_.symbol->name();
4353       // We need to print two hex number and two colons.  So just add 100 bytes
4354       // to the symbol name size.
4355       size_t len = sym_name.size() + 100;
4356       char* buffer = new char[len];
4357       int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
4358                        sym_name.c_str(), this->addend_);
4359       gold_assert(c > 0 && c < static_cast<int>(len));
4360       delete[] buffer;
4361       return std::string(buffer);
4362     }
4363   else
4364     {
4365       // local symbol key name
4366       // <stub-type>:<object>:<r_sym>:<addend>.
4367       const size_t len = 200;
4368       char buffer[len];
4369       int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
4370                        this->u_.relobj, this->r_sym_, this->addend_);
4371       gold_assert(c > 0 && c < static_cast<int>(len));
4372       return std::string(buffer);
4373     }
4374 }
4375
4376 // Reloc_stub methods.
4377
4378 // Determine the type of stub needed, if any, for a relocation of R_TYPE at
4379 // LOCATION to DESTINATION.
4380 // This code is based on the arm_type_of_stub function in
4381 // bfd/elf32-arm.c.  We have changed the interface a liitle to keep the Stub
4382 // class simple.
4383
4384 Stub_type
4385 Reloc_stub::stub_type_for_reloc(
4386    unsigned int r_type,
4387    Arm_address location,
4388    Arm_address destination,
4389    bool target_is_thumb)
4390 {
4391   Stub_type stub_type = arm_stub_none;
4392
4393   // This is a bit ugly but we want to avoid using a templated class for
4394   // big and little endianities.
4395   bool may_use_blx;
4396   bool should_force_pic_veneer;
4397   bool thumb2;
4398   bool thumb_only;
4399   if (parameters->target().is_big_endian())
4400     {
4401       const Target_arm<true>* big_endian_target =
4402         Target_arm<true>::default_target();
4403       may_use_blx = big_endian_target->may_use_blx();
4404       should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
4405       thumb2 = big_endian_target->using_thumb2();
4406       thumb_only = big_endian_target->using_thumb_only();
4407     }
4408   else
4409     {
4410       const Target_arm<false>* little_endian_target =
4411         Target_arm<false>::default_target();
4412       may_use_blx = little_endian_target->may_use_blx();
4413       should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
4414       thumb2 = little_endian_target->using_thumb2();
4415       thumb_only = little_endian_target->using_thumb_only();
4416     }
4417
4418   int64_t branch_offset;
4419   if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
4420     {
4421       // For THUMB BLX instruction, bit 1 of target comes from bit 1 of the
4422       // base address (instruction address + 4).
4423       if ((r_type == elfcpp::R_ARM_THM_CALL) && may_use_blx && !target_is_thumb)
4424         destination = utils::bit_select(destination, location, 0x2);
4425       branch_offset = static_cast<int64_t>(destination) - location;
4426         
4427       // Handle cases where:
4428       // - this call goes too far (different Thumb/Thumb2 max
4429       //   distance)
4430       // - it's a Thumb->Arm call and blx is not available, or it's a
4431       //   Thumb->Arm branch (not bl). A stub is needed in this case.
4432       if ((!thumb2
4433             && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
4434                 || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
4435           || (thumb2
4436               && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
4437                   || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
4438           || ((!target_is_thumb)
4439               && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
4440                   || (r_type == elfcpp::R_ARM_THM_JUMP24))))
4441         {
4442           if (target_is_thumb)
4443             {
4444               // Thumb to thumb.
4445               if (!thumb_only)
4446                 {
4447                   stub_type = (parameters->options().shared()
4448                                || should_force_pic_veneer)
4449                     // PIC stubs.
4450                     ? ((may_use_blx
4451                         && (r_type == elfcpp::R_ARM_THM_CALL))
4452                        // V5T and above. Stub starts with ARM code, so
4453                        // we must be able to switch mode before
4454                        // reaching it, which is only possible for 'bl'
4455                        // (ie R_ARM_THM_CALL relocation).
4456                        ? arm_stub_long_branch_any_thumb_pic
4457                        // On V4T, use Thumb code only.
4458                        : arm_stub_long_branch_v4t_thumb_thumb_pic)
4459
4460                     // non-PIC stubs.
4461                     : ((may_use_blx
4462                         && (r_type == elfcpp::R_ARM_THM_CALL))
4463                        ? arm_stub_long_branch_any_any // V5T and above.
4464                        : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
4465                 }
4466               else
4467                 {
4468                   stub_type = (parameters->options().shared()
4469                                || should_force_pic_veneer)
4470                     ? arm_stub_long_branch_thumb_only_pic       // PIC stub.
4471                     : arm_stub_long_branch_thumb_only;  // non-PIC stub.
4472                 }
4473             }
4474           else
4475             {
4476               // Thumb to arm.
4477              
4478               // FIXME: We should check that the input section is from an
4479               // object that has interwork enabled.
4480
4481               stub_type = (parameters->options().shared()
4482                            || should_force_pic_veneer)
4483                 // PIC stubs.
4484                 ? ((may_use_blx
4485                     && (r_type == elfcpp::R_ARM_THM_CALL))
4486                    ? arm_stub_long_branch_any_arm_pic   // V5T and above.
4487                    : arm_stub_long_branch_v4t_thumb_arm_pic)    // V4T.
4488
4489                 // non-PIC stubs.
4490                 : ((may_use_blx
4491                     && (r_type == elfcpp::R_ARM_THM_CALL))
4492                    ? arm_stub_long_branch_any_any       // V5T and above.
4493                    : arm_stub_long_branch_v4t_thumb_arm);       // V4T.
4494
4495               // Handle v4t short branches.
4496               if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
4497                   && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
4498                   && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
4499                 stub_type = arm_stub_short_branch_v4t_thumb_arm;
4500             }
4501         }
4502     }
4503   else if (r_type == elfcpp::R_ARM_CALL
4504            || r_type == elfcpp::R_ARM_JUMP24
4505            || r_type == elfcpp::R_ARM_PLT32)
4506     {
4507       branch_offset = static_cast<int64_t>(destination) - location;
4508       if (target_is_thumb)
4509         {
4510           // Arm to thumb.
4511
4512           // FIXME: We should check that the input section is from an
4513           // object that has interwork enabled.
4514
4515           // We have an extra 2-bytes reach because of
4516           // the mode change (bit 24 (H) of BLX encoding).
4517           if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
4518               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
4519               || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
4520               || (r_type == elfcpp::R_ARM_JUMP24)
4521               || (r_type == elfcpp::R_ARM_PLT32))
4522             {
4523               stub_type = (parameters->options().shared()
4524                            || should_force_pic_veneer)
4525                 // PIC stubs.
4526                 ? (may_use_blx
4527                    ? arm_stub_long_branch_any_thumb_pic// V5T and above.
4528                    : arm_stub_long_branch_v4t_arm_thumb_pic)    // V4T stub.
4529
4530                 // non-PIC stubs.
4531                 : (may_use_blx
4532                    ? arm_stub_long_branch_any_any       // V5T and above.
4533                    : arm_stub_long_branch_v4t_arm_thumb);       // V4T.
4534             }
4535         }
4536       else
4537         {
4538           // Arm to arm.
4539           if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
4540               || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
4541             {
4542               stub_type = (parameters->options().shared()
4543                            || should_force_pic_veneer)
4544                 ? arm_stub_long_branch_any_arm_pic      // PIC stubs.
4545                 : arm_stub_long_branch_any_any;         /// non-PIC.
4546             }
4547         }
4548     }
4549
4550   return stub_type;
4551 }
4552
4553 // Cortex_a8_stub methods.
4554
4555 // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
4556 // I is the position of the instruction template in the stub template.
4557
4558 uint16_t
4559 Cortex_a8_stub::do_thumb16_special(size_t i)
4560 {
4561   // The only use of this is to copy condition code from a conditional
4562   // branch being worked around to the corresponding conditional branch in
4563   // to the stub.
4564   gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
4565               && i == 0);
4566   uint16_t data = this->stub_template()->insns()[i].data();
4567   gold_assert((data & 0xff00U) == 0xd000U);
4568   data |= ((this->original_insn_ >> 22) & 0xf) << 8;
4569   return data;
4570 }
4571
4572 // Stub_factory methods.
4573
4574 Stub_factory::Stub_factory()
4575 {
4576   // The instruction template sequences are declared as static
4577   // objects and initialized first time the constructor runs.
4578  
4579   // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
4580   // to reach the stub if necessary.
4581   static const Insn_template elf32_arm_stub_long_branch_any_any[] =
4582     {
4583       Insn_template::arm_insn(0xe51ff004),      // ldr   pc, [pc, #-4]
4584       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4585                                                 // dcd   R_ARM_ABS32(X)
4586     };
4587   
4588   // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
4589   // available.
4590   static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
4591     {
4592       Insn_template::arm_insn(0xe59fc000),      // ldr   ip, [pc, #0]
4593       Insn_template::arm_insn(0xe12fff1c),      // bx    ip
4594       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4595                                                 // dcd   R_ARM_ABS32(X)
4596     };
4597   
4598   // Thumb -> Thumb long branch stub. Used on M-profile architectures.
4599   static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
4600     {
4601       Insn_template::thumb16_insn(0xb401),      // push {r0}
4602       Insn_template::thumb16_insn(0x4802),      // ldr  r0, [pc, #8]
4603       Insn_template::thumb16_insn(0x4684),      // mov  ip, r0
4604       Insn_template::thumb16_insn(0xbc01),      // pop  {r0}
4605       Insn_template::thumb16_insn(0x4760),      // bx   ip
4606       Insn_template::thumb16_insn(0xbf00),      // nop
4607       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4608                                                 // dcd  R_ARM_ABS32(X)
4609     };
4610   
4611   // V4T Thumb -> Thumb long branch stub. Using the stack is not
4612   // allowed.
4613   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
4614     {
4615       Insn_template::thumb16_insn(0x4778),      // bx   pc
4616       Insn_template::thumb16_insn(0x46c0),      // nop
4617       Insn_template::arm_insn(0xe59fc000),      // ldr  ip, [pc, #0]
4618       Insn_template::arm_insn(0xe12fff1c),      // bx   ip
4619       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4620                                                 // dcd  R_ARM_ABS32(X)
4621     };
4622   
4623   // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
4624   // available.
4625   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
4626     {
4627       Insn_template::thumb16_insn(0x4778),      // bx   pc
4628       Insn_template::thumb16_insn(0x46c0),      // nop
4629       Insn_template::arm_insn(0xe51ff004),      // ldr   pc, [pc, #-4]
4630       Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
4631                                                 // dcd   R_ARM_ABS32(X)
4632     };
4633   
4634   // V4T Thumb -> ARM short branch stub. Shorter variant of the above
4635   // one, when the destination is close enough.
4636   static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
4637     {
4638       Insn_template::thumb16_insn(0x4778),              // bx   pc
4639       Insn_template::thumb16_insn(0x46c0),              // nop
4640       Insn_template::arm_rel_insn(0xea000000, -8),      // b    (X-8)
4641     };
4642   
4643   // ARM/Thumb -> ARM long branch stub, PIC.  On V5T and above, use
4644   // blx to reach the stub if necessary.
4645   static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
4646     {
4647       Insn_template::arm_insn(0xe59fc000),      // ldr   r12, [pc]
4648       Insn_template::arm_insn(0xe08ff00c),      // add   pc, pc, ip
4649       Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4650                                                 // dcd   R_ARM_REL32(X-4)
4651     };
4652   
4653   // ARM/Thumb -> Thumb long branch stub, PIC.  On V5T and above, use
4654   // blx to reach the stub if necessary.  We can not add into pc;
4655   // it is not guaranteed to mode switch (different in ARMv6 and
4656   // ARMv7).
4657   static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
4658     {
4659       Insn_template::arm_insn(0xe59fc004),      // ldr   r12, [pc, #4]
4660       Insn_template::arm_insn(0xe08fc00c),      // add   ip, pc, ip
4661       Insn_template::arm_insn(0xe12fff1c),      // bx    ip
4662       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4663                                                 // dcd   R_ARM_REL32(X)
4664     };
4665   
4666   // V4T ARM -> ARM long branch stub, PIC.
4667   static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
4668     {
4669       Insn_template::arm_insn(0xe59fc004),      // ldr   ip, [pc, #4]
4670       Insn_template::arm_insn(0xe08fc00c),      // add   ip, pc, ip
4671       Insn_template::arm_insn(0xe12fff1c),      // bx    ip
4672       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4673                                                 // dcd   R_ARM_REL32(X)
4674     };
4675   
4676   // V4T Thumb -> ARM long branch stub, PIC.
4677   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
4678     {
4679       Insn_template::thumb16_insn(0x4778),      // bx   pc
4680       Insn_template::thumb16_insn(0x46c0),      // nop
4681       Insn_template::arm_insn(0xe59fc000),      // ldr  ip, [pc, #0]
4682       Insn_template::arm_insn(0xe08cf00f),      // add  pc, ip, pc
4683       Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
4684                                                 // dcd  R_ARM_REL32(X)
4685     };
4686   
4687   // Thumb -> Thumb long branch stub, PIC. Used on M-profile
4688   // architectures.
4689   static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
4690     {
4691       Insn_template::thumb16_insn(0xb401),      // push {r0}
4692       Insn_template::thumb16_insn(0x4802),      // ldr  r0, [pc, #8]
4693       Insn_template::thumb16_insn(0x46fc),      // mov  ip, pc
4694       Insn_template::thumb16_insn(0x4484),      // add  ip, r0
4695       Insn_template::thumb16_insn(0xbc01),      // pop  {r0}
4696       Insn_template::thumb16_insn(0x4760),      // bx   ip
4697       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
4698                                                 // dcd  R_ARM_REL32(X)
4699     };
4700   
4701   // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
4702   // allowed.
4703   static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
4704     {
4705       Insn_template::thumb16_insn(0x4778),      // bx   pc
4706       Insn_template::thumb16_insn(0x46c0),      // nop
4707       Insn_template::arm_insn(0xe59fc004),      // ldr  ip, [pc, #4]
4708       Insn_template::arm_insn(0xe08fc00c),      // add   ip, pc, ip
4709       Insn_template::arm_insn(0xe12fff1c),      // bx   ip
4710       Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
4711                                                 // dcd  R_ARM_REL32(X)
4712     };
4713   
4714   // Cortex-A8 erratum-workaround stubs.
4715   
4716   // Stub used for conditional branches (which may be beyond +/-1MB away,
4717   // so we can't use a conditional branch to reach this stub).
4718   
4719   // original code:
4720   //
4721   //    b<cond> X
4722   // after:
4723   //
4724   static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
4725     {
4726       Insn_template::thumb16_bcond_insn(0xd001),        //      b<cond>.n true
4727       Insn_template::thumb32_b_insn(0xf000b800, -4),    //      b.w after
4728       Insn_template::thumb32_b_insn(0xf000b800, -4)     // true:
4729                                                         //      b.w X
4730     };
4731   
4732   // Stub used for b.w and bl.w instructions.
4733   
4734   static const Insn_template elf32_arm_stub_a8_veneer_b[] =
4735     {
4736       Insn_template::thumb32_b_insn(0xf000b800, -4)     // b.w dest
4737     };
4738   
4739   static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
4740     {
4741       Insn_template::thumb32_b_insn(0xf000b800, -4)     // b.w dest
4742     };
4743   
4744   // Stub used for Thumb-2 blx.w instructions.  We modified the original blx.w
4745   // instruction (which switches to ARM mode) to point to this stub.  Jump to
4746   // the real destination using an ARM-mode branch.
4747   static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
4748     {
4749       Insn_template::arm_rel_insn(0xea000000, -8)       // b dest
4750     };
4751
4752   // Stub used to provide an interworking for R_ARM_V4BX relocation
4753   // (bx r[n] instruction).
4754   static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
4755     {
4756       Insn_template::arm_insn(0xe3100001),              // tst   r<n>, #1
4757       Insn_template::arm_insn(0x01a0f000),              // moveq pc, r<n>
4758       Insn_template::arm_insn(0xe12fff10)               // bx    r<n>
4759     };
4760
4761   // Fill in the stub template look-up table.  Stub templates are constructed
4762   // per instance of Stub_factory for fast look-up without locking
4763   // in a thread-enabled environment.
4764
4765   this->stub_templates_[arm_stub_none] =
4766     new Stub_template(arm_stub_none, NULL, 0);
4767
4768 #define DEF_STUB(x)     \
4769   do \
4770     { \
4771       size_t array_size \
4772         = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
4773       Stub_type type = arm_stub_##x; \
4774       this->stub_templates_[type] = \
4775         new Stub_template(type, elf32_arm_stub_##x, array_size); \
4776     } \
4777   while (0);
4778
4779   DEF_STUBS
4780 #undef DEF_STUB
4781 }
4782
4783 // Stub_table methods.
4784
4785 // Removel all Cortex-A8 stub.
4786
4787 template<bool big_endian>
4788 void
4789 Stub_table<big_endian>::remove_all_cortex_a8_stubs()
4790 {
4791   for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4792        p != this->cortex_a8_stubs_.end();
4793        ++p)
4794     delete p->second;
4795   this->cortex_a8_stubs_.clear();
4796 }
4797
4798 // Relocate one stub.  This is a helper for Stub_table::relocate_stubs().
4799
4800 template<bool big_endian>
4801 void
4802 Stub_table<big_endian>::relocate_stub(
4803     Stub* stub,
4804     const Relocate_info<32, big_endian>* relinfo,
4805     Target_arm<big_endian>* arm_target,
4806     Output_section* output_section,
4807     unsigned char* view,
4808     Arm_address address,
4809     section_size_type view_size)
4810 {
4811   const Stub_template* stub_template = stub->stub_template();
4812   if (stub_template->reloc_count() != 0)
4813     {
4814       // Adjust view to cover the stub only.
4815       section_size_type offset = stub->offset();
4816       section_size_type stub_size = stub_template->size();
4817       gold_assert(offset + stub_size <= view_size);
4818
4819       arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
4820                                 address + offset, stub_size);
4821     }
4822 }
4823
4824 // Relocate all stubs in this stub table.
4825
4826 template<bool big_endian>
4827 void
4828 Stub_table<big_endian>::relocate_stubs(
4829     const Relocate_info<32, big_endian>* relinfo,
4830     Target_arm<big_endian>* arm_target,
4831     Output_section* output_section,
4832     unsigned char* view,
4833     Arm_address address,
4834     section_size_type view_size)
4835 {
4836   // If we are passed a view bigger than the stub table's.  we need to
4837   // adjust the view.
4838   gold_assert(address == this->address()
4839               && (view_size
4840                   == static_cast<section_size_type>(this->data_size())));
4841
4842   // Relocate all relocation stubs.
4843   for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4844       p != this->reloc_stubs_.end();
4845       ++p)
4846     this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4847                         address, view_size);
4848
4849   // Relocate all Cortex-A8 stubs.
4850   for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
4851        p != this->cortex_a8_stubs_.end();
4852        ++p)
4853     this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
4854                         address, view_size);
4855
4856   // Relocate all ARM V4BX stubs.
4857   for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
4858        p != this->arm_v4bx_stubs_.end();
4859        ++p)
4860     {
4861       if (*p != NULL)
4862         this->relocate_stub(*p, relinfo, arm_target, output_section, view,
4863                             address, view_size);
4864     }
4865 }
4866
4867 // Write out the stubs to file.
4868
4869 template<bool big_endian>
4870 void
4871 Stub_table<big_endian>::do_write(Output_file* of)
4872 {
4873   off_t offset = this->offset();
4874   const section_size_type oview_size =
4875     convert_to_section_size_type(this->data_size());
4876   unsigned char* const oview = of->get_output_view(offset, oview_size);
4877
4878   // Write relocation stubs.
4879   for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
4880       p != this->reloc_stubs_.end();
4881       ++p)
4882     {
4883       Reloc_stub* stub = p->second;
4884       Arm_address address = this->address() + stub->offset();
4885       gold_assert(address
4886                   == align_address(address,
4887                                    stub->stub_template()->alignment()));
4888       stub->write(oview + stub->offset(), stub->stub_template()->size(),
4889                   big_endian);
4890     }
4891
4892   // Write Cortex-A8 stubs.
4893   for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4894        p != this->cortex_a8_stubs_.end();
4895        ++p)
4896     {
4897       Cortex_a8_stub* stub = p->second;
4898       Arm_address address = this->address() + stub->offset();
4899       gold_assert(address
4900                   == align_address(address,
4901                                    stub->stub_template()->alignment()));
4902       stub->write(oview + stub->offset(), stub->stub_template()->size(),
4903                   big_endian);
4904     }
4905
4906   // Write ARM V4BX relocation stubs.
4907   for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4908        p != this->arm_v4bx_stubs_.end();
4909        ++p)
4910     {
4911       if (*p == NULL)
4912         continue;
4913
4914       Arm_address address = this->address() + (*p)->offset();
4915       gold_assert(address
4916                   == align_address(address,
4917                                    (*p)->stub_template()->alignment()));
4918       (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
4919                   big_endian);
4920     }
4921
4922   of->write_output_view(this->offset(), oview_size, oview);
4923 }
4924
4925 // Update the data size and address alignment of the stub table at the end
4926 // of a relaxation pass.   Return true if either the data size or the
4927 // alignment changed in this relaxation pass.
4928
4929 template<bool big_endian>
4930 bool
4931 Stub_table<big_endian>::update_data_size_and_addralign()
4932 {
4933   // Go over all stubs in table to compute data size and address alignment.
4934   off_t size = this->reloc_stubs_size_;
4935   unsigned addralign = this->reloc_stubs_addralign_;
4936
4937   for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4938        p != this->cortex_a8_stubs_.end();
4939        ++p)
4940     {
4941       const Stub_template* stub_template = p->second->stub_template();
4942       addralign = std::max(addralign, stub_template->alignment());
4943       size = (align_address(size, stub_template->alignment())
4944               + stub_template->size());
4945     }
4946
4947   for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
4948        p != this->arm_v4bx_stubs_.end();
4949        ++p)
4950     {
4951       if (*p == NULL)
4952         continue;
4953
4954       const Stub_template* stub_template = (*p)->stub_template();
4955       addralign = std::max(addralign, stub_template->alignment());
4956       size = (align_address(size, stub_template->alignment())
4957               + stub_template->size());
4958     }
4959
4960   // Check if either data size or alignment changed in this pass.
4961   // Update prev_data_size_ and prev_addralign_.  These will be used
4962   // as the current data size and address alignment for the next pass.
4963   bool changed = size != this->prev_data_size_;
4964   this->prev_data_size_ = size; 
4965
4966   if (addralign != this->prev_addralign_)
4967     changed = true;
4968   this->prev_addralign_ = addralign;
4969
4970   return changed;
4971 }
4972
4973 // Finalize the stubs.  This sets the offsets of the stubs within the stub
4974 // table.  It also marks all input sections needing Cortex-A8 workaround.
4975
4976 template<bool big_endian>
4977 void
4978 Stub_table<big_endian>::finalize_stubs()
4979 {
4980   off_t off = this->reloc_stubs_size_;
4981   for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
4982        p != this->cortex_a8_stubs_.end();
4983        ++p)
4984     {
4985       Cortex_a8_stub* stub = p->second;
4986       const Stub_template* stub_template = stub->stub_template();
4987       uint64_t stub_addralign = stub_template->alignment();
4988       off = align_address(off, stub_addralign);
4989       stub->set_offset(off);
4990       off += stub_template->size();
4991
4992       // Mark input section so that we can determine later if a code section
4993       // needs the Cortex-A8 workaround quickly.
4994       Arm_relobj<big_endian>* arm_relobj =
4995         Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
4996       arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
4997     }
4998
4999   for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
5000       p != this->arm_v4bx_stubs_.end();
5001       ++p)
5002     {
5003       if (*p == NULL)
5004         continue;
5005
5006       const Stub_template* stub_template = (*p)->stub_template();
5007       uint64_t stub_addralign = stub_template->alignment();
5008       off = align_address(off, stub_addralign);
5009       (*p)->set_offset(off);
5010       off += stub_template->size();
5011     }
5012
5013   gold_assert(off <= this->prev_data_size_);
5014 }
5015
5016 // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
5017 // and VIEW_ADDRESS + VIEW_SIZE - 1.  VIEW points to the mapped address
5018 // of the address range seen by the linker.
5019
5020 template<bool big_endian>
5021 void
5022 Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
5023     Target_arm<big_endian>* arm_target,
5024     unsigned char* view,
5025     Arm_address view_address,
5026     section_size_type view_size)
5027 {
5028   // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
5029   for (Cortex_a8_stub_list::const_iterator p =
5030          this->cortex_a8_stubs_.lower_bound(view_address);
5031        ((p != this->cortex_a8_stubs_.end())
5032         && (p->first < (view_address + view_size)));
5033        ++p)
5034     {
5035       // We do not store the THUMB bit in the LSB of either the branch address
5036       // or the stub offset.  There is no need to strip the LSB.
5037       Arm_address branch_address = p->first;
5038       const Cortex_a8_stub* stub = p->second;
5039       Arm_address stub_address = this->address() + stub->offset();
5040
5041       // Offset of the branch instruction relative to this view.
5042       section_size_type offset =
5043         convert_to_section_size_type(branch_address - view_address);
5044       gold_assert((offset + 4) <= view_size);
5045
5046       arm_target->apply_cortex_a8_workaround(stub, stub_address,
5047                                              view + offset, branch_address);
5048     }
5049 }
5050
5051 // Arm_input_section methods.
5052
5053 // Initialize an Arm_input_section.
5054
5055 template<bool big_endian>
5056 void
5057 Arm_input_section<big_endian>::init()
5058 {
5059   Relobj* relobj = this->relobj();
5060   unsigned int shndx = this->shndx();
5061
5062   // Cache these to speed up size and alignment queries.  It is too slow
5063   // to call section_addraglin and section_size every time.
5064   this->original_addralign_ =
5065     convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
5066   this->original_size_ =
5067     convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
5068
5069   // We want to make this look like the original input section after
5070   // output sections are finalized.
5071   Output_section* os = relobj->output_section(shndx);
5072   off_t offset = relobj->output_section_offset(shndx);
5073   gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
5074   this->set_address(os->address() + offset);
5075   this->set_file_offset(os->offset() + offset);
5076
5077   this->set_current_data_size(this->original_size_);
5078   this->finalize_data_size();
5079 }
5080
5081 template<bool big_endian>
5082 void
5083 Arm_input_section<big_endian>::do_write(Output_file* of)
5084 {
5085   // We have to write out the original section content.
5086   section_size_type section_size;
5087   const unsigned char* section_contents =
5088     this->relobj()->section_contents(this->shndx(), &section_size, false); 
5089   of->write(this->offset(), section_contents, section_size); 
5090
5091   // If this owns a stub table and it is not empty, write it.
5092   if (this->is_stub_table_owner() && !this->stub_table_->empty())
5093     this->stub_table_->write(of);
5094 }
5095
5096 // Finalize data size.
5097
5098 template<bool big_endian>
5099 void
5100 Arm_input_section<big_endian>::set_final_data_size()
5101 {
5102   off_t off = convert_types<off_t, uint64_t>(this->original_size_);
5103
5104   if (this->is_stub_table_owner())
5105     {
5106       this->stub_table_->finalize_data_size();
5107       off = align_address(off, this->stub_table_->addralign());
5108       off += this->stub_table_->data_size();
5109     }
5110   this->set_data_size(off);
5111 }
5112
5113 // Reset address and file offset.
5114
5115 template<bool big_endian>
5116 void
5117 Arm_input_section<big_endian>::do_reset_address_and_file_offset()
5118 {
5119   // Size of the original input section contents.
5120   off_t off = convert_types<off_t, uint64_t>(this->original_size_);
5121
5122   // If this is a stub table owner, account for the stub table size.
5123   if (this->is_stub_table_owner())
5124     {
5125       Stub_table<big_endian>* stub_table = this->stub_table_;
5126
5127       // Reset the stub table's address and file offset.  The
5128       // current data size for child will be updated after that.
5129       stub_table_->reset_address_and_file_offset();
5130       off = align_address(off, stub_table_->addralign());
5131       off += stub_table->current_data_size();
5132     }
5133
5134   this->set_current_data_size(off);
5135 }
5136
5137 // Arm_exidx_cantunwind methods.
5138
5139 // Write this to Output file OF for a fixed endianness.
5140
5141 template<bool big_endian>
5142 void
5143 Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
5144 {
5145   off_t offset = this->offset();
5146   const section_size_type oview_size = 8;
5147   unsigned char* const oview = of->get_output_view(offset, oview_size);
5148   
5149   typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5150   Valtype* wv = reinterpret_cast<Valtype*>(oview);
5151
5152   Output_section* os = this->relobj_->output_section(this->shndx_);
5153   gold_assert(os != NULL);
5154
5155   Arm_relobj<big_endian>* arm_relobj =
5156     Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
5157   Arm_address output_offset =
5158     arm_relobj->get_output_section_offset(this->shndx_);
5159   Arm_address section_start;
5160   if (output_offset != Arm_relobj<big_endian>::invalid_address)
5161     section_start = os->address() + output_offset;
5162   else
5163     {
5164       // Currently this only happens for a relaxed section.
5165       const Output_relaxed_input_section* poris =
5166         os->find_relaxed_input_section(this->relobj_, this->shndx_);
5167       gold_assert(poris != NULL);
5168       section_start = poris->address();
5169     }
5170
5171   // We always append this to the end of an EXIDX section.
5172   Arm_address output_address =
5173     section_start + this->relobj_->section_size(this->shndx_);
5174
5175   // Write out the entry.  The first word either points to the beginning
5176   // or after the end of a text section.  The second word is the special
5177   // EXIDX_CANTUNWIND value.
5178   uint32_t prel31_offset = output_address - this->address();
5179   if (utils::has_overflow<31>(offset))
5180     gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
5181   elfcpp::Swap<32, big_endian>::writeval(wv, prel31_offset & 0x7fffffffU);
5182   elfcpp::Swap<32, big_endian>::writeval(wv + 1, elfcpp::EXIDX_CANTUNWIND);
5183
5184   of->write_output_view(this->offset(), oview_size, oview);
5185 }
5186
5187 // Arm_exidx_merged_section methods.
5188
5189 // Constructor for Arm_exidx_merged_section.
5190 // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
5191 // SECTION_OFFSET_MAP points to a section offset map describing how
5192 // parts of the input section are mapped to output.  DELETED_BYTES is
5193 // the number of bytes deleted from the EXIDX input section.
5194
5195 Arm_exidx_merged_section::Arm_exidx_merged_section(
5196     const Arm_exidx_input_section& exidx_input_section,
5197     const Arm_exidx_section_offset_map& section_offset_map,
5198     uint32_t deleted_bytes)
5199   : Output_relaxed_input_section(exidx_input_section.relobj(),
5200                                  exidx_input_section.shndx(),
5201                                  exidx_input_section.addralign()),
5202     exidx_input_section_(exidx_input_section),
5203     section_offset_map_(section_offset_map)
5204 {
5205   // Fix size here so that we do not need to implement set_final_data_size.
5206   this->set_data_size(exidx_input_section.size() - deleted_bytes);
5207   this->fix_data_size();
5208 }
5209
5210 // Given an input OBJECT, an input section index SHNDX within that
5211 // object, and an OFFSET relative to the start of that input
5212 // section, return whether or not the corresponding offset within
5213 // the output section is known.  If this function returns true, it
5214 // sets *POUTPUT to the output offset.  The value -1 indicates that
5215 // this input offset is being discarded.
5216
5217 bool
5218 Arm_exidx_merged_section::do_output_offset(
5219     const Relobj* relobj,
5220     unsigned int shndx,
5221     section_offset_type offset,
5222     section_offset_type* poutput) const
5223 {
5224   // We only handle offsets for the original EXIDX input section.
5225   if (relobj != this->exidx_input_section_.relobj()
5226       || shndx != this->exidx_input_section_.shndx())
5227     return false;
5228
5229   section_offset_type section_size =
5230     convert_types<section_offset_type>(this->exidx_input_section_.size());
5231   if (offset < 0 || offset >= section_size)
5232     // Input offset is out of valid range.
5233     *poutput = -1;
5234   else
5235     {
5236       // We need to look up the section offset map to determine the output
5237       // offset.  Find the reference point in map that is first offset
5238       // bigger than or equal to this offset.
5239       Arm_exidx_section_offset_map::const_iterator p =
5240         this->section_offset_map_.lower_bound(offset);
5241
5242       // The section offset maps are build such that this should not happen if
5243       // input offset is in the valid range.
5244       gold_assert(p != this->section_offset_map_.end());
5245
5246       // We need to check if this is dropped.
5247      section_offset_type ref = p->first;
5248      section_offset_type mapped_ref = p->second;
5249
5250       if (mapped_ref != Arm_exidx_input_section::invalid_offset)
5251         // Offset is present in output.
5252         *poutput = mapped_ref + (offset - ref);
5253       else
5254         // Offset is discarded owing to EXIDX entry merging.
5255         *poutput = -1;
5256     }
5257   
5258   return true;
5259 }
5260
5261 // Write this to output file OF.
5262
5263 void
5264 Arm_exidx_merged_section::do_write(Output_file* of)
5265 {
5266   // If we retain or discard the whole EXIDX input section,  we would
5267   // not be here.
5268   gold_assert(this->data_size() != this->exidx_input_section_.size()
5269               && this->data_size() != 0);
5270
5271   off_t offset = this->offset();
5272   const section_size_type oview_size = this->data_size();
5273   unsigned char* const oview = of->get_output_view(offset, oview_size);
5274   
5275   Output_section* os = this->relobj()->output_section(this->shndx());
5276   gold_assert(os != NULL);
5277
5278   // Get contents of EXIDX input section.
5279   section_size_type section_size;
5280   const unsigned char* section_contents =
5281     this->relobj()->section_contents(this->shndx(), &section_size, false); 
5282   gold_assert(section_size == this->exidx_input_section_.size());
5283
5284   // Go over spans of input offsets and write only those that are not
5285   // discarded.
5286   section_offset_type in_start = 0;
5287   section_offset_type out_start = 0;
5288   for(Arm_exidx_section_offset_map::const_iterator p =
5289         this->section_offset_map_.begin();
5290       p != this->section_offset_map_.end();
5291       ++p)
5292     {
5293       section_offset_type in_end = p->first;
5294       gold_assert(in_end >= in_start);
5295       section_offset_type out_end = p->second;
5296       size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
5297       if (out_end != -1)
5298         {
5299           size_t out_chunk_size =
5300             convert_types<size_t>(out_end - out_start + 1);
5301           gold_assert(out_chunk_size == in_chunk_size);
5302           memcpy(oview + out_start, section_contents + in_start,
5303                  out_chunk_size);
5304           out_start += out_chunk_size;
5305         }
5306       in_start += in_chunk_size;
5307     }
5308
5309   gold_assert(convert_to_section_size_type(out_start) == oview_size);
5310   of->write_output_view(this->offset(), oview_size, oview);
5311 }
5312
5313 // Arm_exidx_fixup methods.
5314
5315 // Append an EXIDX_CANTUNWIND in the current output section if the last entry
5316 // is not an EXIDX_CANTUNWIND entry already.  The new EXIDX_CANTUNWIND entry
5317 // points to the end of the last seen EXIDX section.
5318
5319 void
5320 Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
5321 {
5322   if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
5323       && this->last_input_section_ != NULL)
5324     {
5325       Relobj* relobj = this->last_input_section_->relobj();
5326       unsigned int text_shndx = this->last_input_section_->link();
5327       Arm_exidx_cantunwind* cantunwind =
5328         new Arm_exidx_cantunwind(relobj, text_shndx);
5329       this->exidx_output_section_->add_output_section_data(cantunwind);
5330       this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5331     }
5332 }
5333
5334 // Process an EXIDX section entry in input.  Return whether this entry
5335 // can be deleted in the output.  SECOND_WORD in the second word of the
5336 // EXIDX entry.
5337
5338 bool
5339 Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
5340 {
5341   bool delete_entry;
5342   if (second_word == elfcpp::EXIDX_CANTUNWIND)
5343     {
5344       // Merge if previous entry is also an EXIDX_CANTUNWIND.
5345       delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
5346       this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
5347     }
5348   else if ((second_word & 0x80000000) != 0)
5349     {
5350       // Inlined unwinding data.  Merge if equal to previous.
5351       delete_entry = (merge_exidx_entries_
5352                       && this->last_unwind_type_ == UT_INLINED_ENTRY
5353                       && this->last_inlined_entry_ == second_word);
5354       this->last_unwind_type_ = UT_INLINED_ENTRY;
5355       this->last_inlined_entry_ = second_word;
5356     }
5357   else
5358     {
5359       // Normal table entry.  In theory we could merge these too,
5360       // but duplicate entries are likely to be much less common.
5361       delete_entry = false;
5362       this->last_unwind_type_ = UT_NORMAL_ENTRY;
5363     }
5364   return delete_entry;
5365 }
5366
5367 // Update the current section offset map during EXIDX section fix-up.
5368 // If there is no map, create one.  INPUT_OFFSET is the offset of a
5369 // reference point, DELETED_BYTES is the number of deleted by in the
5370 // section so far.  If DELETE_ENTRY is true, the reference point and
5371 // all offsets after the previous reference point are discarded.
5372
5373 void
5374 Arm_exidx_fixup::update_offset_map(
5375     section_offset_type input_offset,
5376     section_size_type deleted_bytes,
5377     bool delete_entry)
5378 {
5379   if (this->section_offset_map_ == NULL)
5380     this->section_offset_map_ = new Arm_exidx_section_offset_map();
5381   section_offset_type output_offset;
5382   if (delete_entry)
5383     output_offset = Arm_exidx_input_section::invalid_offset;
5384   else
5385     output_offset = input_offset - deleted_bytes;
5386   (*this->section_offset_map_)[input_offset] = output_offset;
5387 }
5388
5389 // Process EXIDX_INPUT_SECTION for EXIDX entry merging.  Return the number of
5390 // bytes deleted.  If some entries are merged, also store a pointer to a newly
5391 // created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP.  The
5392 // caller owns the map and is responsible for releasing it after use.
5393
5394 template<bool big_endian>
5395 uint32_t
5396 Arm_exidx_fixup::process_exidx_section(
5397     const Arm_exidx_input_section* exidx_input_section,
5398     Arm_exidx_section_offset_map** psection_offset_map)
5399 {
5400   Relobj* relobj = exidx_input_section->relobj();
5401   unsigned shndx = exidx_input_section->shndx();
5402   section_size_type section_size;
5403   const unsigned char* section_contents =
5404     relobj->section_contents(shndx, &section_size, false);
5405
5406   if ((section_size % 8) != 0)
5407     {
5408       // Something is wrong with this section.  Better not touch it.
5409       gold_error(_("uneven .ARM.exidx section size in %s section %u"),
5410                  relobj->name().c_str(), shndx);
5411       this->last_input_section_ = exidx_input_section;
5412       this->last_unwind_type_ = UT_NONE;
5413       return 0;
5414     }
5415   
5416   uint32_t deleted_bytes = 0;
5417   bool prev_delete_entry = false;
5418   gold_assert(this->section_offset_map_ == NULL);
5419
5420   for (section_size_type i = 0; i < section_size; i += 8)
5421     {
5422       typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5423       const Valtype* wv =
5424           reinterpret_cast<const Valtype*>(section_contents + i + 4);
5425       uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
5426
5427       bool delete_entry = this->process_exidx_entry(second_word);
5428
5429       // Entry deletion causes changes in output offsets.  We use a std::map
5430       // to record these.  And entry (x, y) means input offset x
5431       // is mapped to output offset y.  If y is invalid_offset, then x is
5432       // dropped in the output.  Because of the way std::map::lower_bound
5433       // works, we record the last offset in a region w.r.t to keeping or
5434       // dropping.  If there is no entry (x0, y0) for an input offset x0,
5435       // the output offset y0 of it is determined by the output offset y1 of
5436       // the smallest input offset x1 > x0 that there is an (x1, y1) entry
5437       // in the map.  If y1 is not -1, then y0 = y1 + x0 - x1.  Othewise, y1
5438       // y0 is also -1.
5439       if (delete_entry != prev_delete_entry && i != 0)
5440         this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
5441
5442       // Update total deleted bytes for this entry.
5443       if (delete_entry)
5444         deleted_bytes += 8;
5445
5446       prev_delete_entry = delete_entry;
5447     }
5448   
5449   // If section offset map is not NULL, make an entry for the end of
5450   // section.
5451   if (this->section_offset_map_ != NULL)
5452     update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
5453
5454   *psection_offset_map = this->section_offset_map_;
5455   this->section_offset_map_ = NULL;
5456   this->last_input_section_ = exidx_input_section;
5457   
5458   // Set the first output text section so that we can link the EXIDX output
5459   // section to it.  Ignore any EXIDX input section that is completely merged.
5460   if (this->first_output_text_section_ == NULL
5461       && deleted_bytes != section_size)
5462     {
5463       unsigned int link = exidx_input_section->link();
5464       Output_section* os = relobj->output_section(link);
5465       gold_assert(os != NULL);
5466       this->first_output_text_section_ = os;
5467     }
5468
5469   return deleted_bytes;
5470 }
5471
5472 // Arm_output_section methods.
5473
5474 // Create a stub group for input sections from BEGIN to END.  OWNER
5475 // points to the input section to be the owner a new stub table.
5476
5477 template<bool big_endian>
5478 void
5479 Arm_output_section<big_endian>::create_stub_group(
5480   Input_section_list::const_iterator begin,
5481   Input_section_list::const_iterator end,
5482   Input_section_list::const_iterator owner,
5483   Target_arm<big_endian>* target,
5484   std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
5485 {
5486   // We use a different kind of relaxed section in an EXIDX section.
5487   // The static casting from Output_relaxed_input_section to
5488   // Arm_input_section is invalid in an EXIDX section.  We are okay
5489   // because we should not be calling this for an EXIDX section. 
5490   gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX);
5491
5492   // Currently we convert ordinary input sections into relaxed sections only
5493   // at this point but we may want to support creating relaxed input section
5494   // very early.  So we check here to see if owner is already a relaxed
5495   // section.
5496   
5497   Arm_input_section<big_endian>* arm_input_section;
5498   if (owner->is_relaxed_input_section())
5499     {
5500       arm_input_section =
5501         Arm_input_section<big_endian>::as_arm_input_section(
5502           owner->relaxed_input_section());
5503     }
5504   else
5505     {
5506       gold_assert(owner->is_input_section());
5507       // Create a new relaxed input section.
5508       arm_input_section =
5509         target->new_arm_input_section(owner->relobj(), owner->shndx());
5510       new_relaxed_sections->push_back(arm_input_section);
5511     }
5512
5513   // Create a stub table.
5514   Stub_table<big_endian>* stub_table =
5515     target->new_stub_table(arm_input_section);
5516
5517   arm_input_section->set_stub_table(stub_table);
5518   
5519   Input_section_list::const_iterator p = begin;
5520   Input_section_list::const_iterator prev_p;
5521
5522   // Look for input sections or relaxed input sections in [begin ... end].
5523   do
5524     {
5525       if (p->is_input_section() || p->is_relaxed_input_section())
5526         {
5527           // The stub table information for input sections live
5528           // in their objects.
5529           Arm_relobj<big_endian>* arm_relobj =
5530             Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5531           arm_relobj->set_stub_table(p->shndx(), stub_table);
5532         }
5533       prev_p = p++;
5534     }
5535   while (prev_p != end);
5536 }
5537
5538 // Group input sections for stub generation.  GROUP_SIZE is roughly the limit
5539 // of stub groups.  We grow a stub group by adding input section until the
5540 // size is just below GROUP_SIZE.  The last input section will be converted
5541 // into a stub table.  If STUB_ALWAYS_AFTER_BRANCH is false, we also add
5542 // input section after the stub table, effectively double the group size.
5543 // 
5544 // This is similar to the group_sections() function in elf32-arm.c but is
5545 // implemented differently.
5546
5547 template<bool big_endian>
5548 void
5549 Arm_output_section<big_endian>::group_sections(
5550     section_size_type group_size,
5551     bool stubs_always_after_branch,
5552     Target_arm<big_endian>* target)
5553 {
5554   // We only care about sections containing code.
5555   if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
5556     return;
5557
5558   // States for grouping.
5559   typedef enum
5560   {
5561     // No group is being built.
5562     NO_GROUP,
5563     // A group is being built but the stub table is not found yet.
5564     // We keep group a stub group until the size is just under GROUP_SIZE.
5565     // The last input section in the group will be used as the stub table.
5566     FINDING_STUB_SECTION,
5567     // A group is being built and we have already found a stub table.
5568     // We enter this state to grow a stub group by adding input section
5569     // after the stub table.  This effectively doubles the group size.
5570     HAS_STUB_SECTION
5571   } State;
5572
5573   // Any newly created relaxed sections are stored here.
5574   std::vector<Output_relaxed_input_section*> new_relaxed_sections;
5575
5576   State state = NO_GROUP;
5577   section_size_type off = 0;
5578   section_size_type group_begin_offset = 0;
5579   section_size_type group_end_offset = 0;
5580   section_size_type stub_table_end_offset = 0;
5581   Input_section_list::const_iterator group_begin =
5582     this->input_sections().end();
5583   Input_section_list::const_iterator stub_table =
5584     this->input_sections().end();
5585   Input_section_list::const_iterator group_end = this->input_sections().end();
5586   for (Input_section_list::const_iterator p = this->input_sections().begin();
5587        p != this->input_sections().end();
5588        ++p)
5589     {
5590       section_size_type section_begin_offset =
5591         align_address(off, p->addralign());
5592       section_size_type section_end_offset =
5593         section_begin_offset + p->data_size(); 
5594       
5595       // Check to see if we should group the previously seens sections.
5596       switch (state)
5597         {
5598         case NO_GROUP:
5599           break;
5600
5601         case FINDING_STUB_SECTION:
5602           // Adding this section makes the group larger than GROUP_SIZE.
5603           if (section_end_offset - group_begin_offset >= group_size)
5604             {
5605               if (stubs_always_after_branch)
5606                 {       
5607                   gold_assert(group_end != this->input_sections().end());
5608                   this->create_stub_group(group_begin, group_end, group_end,
5609                                           target, &new_relaxed_sections);
5610                   state = NO_GROUP;
5611                 }
5612               else
5613                 {
5614                   // But wait, there's more!  Input sections up to
5615                   // stub_group_size bytes after the stub table can be
5616                   // handled by it too.
5617                   state = HAS_STUB_SECTION;
5618                   stub_table = group_end;
5619                   stub_table_end_offset = group_end_offset;
5620                 }
5621             }
5622             break;
5623
5624         case HAS_STUB_SECTION:
5625           // Adding this section makes the post stub-section group larger
5626           // than GROUP_SIZE.
5627           if (section_end_offset - stub_table_end_offset >= group_size)
5628            {
5629              gold_assert(group_end != this->input_sections().end());
5630              this->create_stub_group(group_begin, group_end, stub_table,
5631                                      target, &new_relaxed_sections);
5632              state = NO_GROUP;
5633            }
5634            break;
5635
5636           default:
5637             gold_unreachable();
5638         }       
5639
5640       // If we see an input section and currently there is no group, start
5641       // a new one.  Skip any empty sections.
5642       if ((p->is_input_section() || p->is_relaxed_input_section())
5643           && (p->relobj()->section_size(p->shndx()) != 0))
5644         {
5645           if (state == NO_GROUP)
5646             {
5647               state = FINDING_STUB_SECTION;
5648               group_begin = p;
5649               group_begin_offset = section_begin_offset;
5650             }
5651
5652           // Keep track of the last input section seen.
5653           group_end = p;
5654           group_end_offset = section_end_offset;
5655         }
5656
5657       off = section_end_offset;
5658     }
5659
5660   // Create a stub group for any ungrouped sections.
5661   if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
5662     {
5663       gold_assert(group_end != this->input_sections().end());
5664       this->create_stub_group(group_begin, group_end,
5665                               (state == FINDING_STUB_SECTION
5666                                ? group_end
5667                                : stub_table),
5668                                target, &new_relaxed_sections);
5669     }
5670
5671   // Convert input section into relaxed input section in a batch.
5672   if (!new_relaxed_sections.empty())
5673     this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
5674
5675   // Update the section offsets
5676   for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
5677     {
5678       Arm_relobj<big_endian>* arm_relobj =
5679         Arm_relobj<big_endian>::as_arm_relobj(
5680           new_relaxed_sections[i]->relobj());
5681       unsigned int shndx = new_relaxed_sections[i]->shndx();
5682       // Tell Arm_relobj that this input section is converted.
5683       arm_relobj->convert_input_section_to_relaxed_section(shndx);
5684     }
5685 }
5686
5687 // Append non empty text sections in this to LIST in ascending
5688 // order of their position in this.
5689
5690 template<bool big_endian>
5691 void
5692 Arm_output_section<big_endian>::append_text_sections_to_list(
5693     Text_section_list* list)
5694 {
5695   gold_assert((this->flags() & elfcpp::SHF_ALLOC) != 0);
5696
5697   for (Input_section_list::const_iterator p = this->input_sections().begin();
5698        p != this->input_sections().end();
5699        ++p)
5700     {
5701       // We only care about plain or relaxed input sections.  We also
5702       // ignore any merged sections.
5703       if ((p->is_input_section() || p->is_relaxed_input_section())
5704           && p->data_size() != 0)
5705         list->push_back(Text_section_list::value_type(p->relobj(),
5706                                                       p->shndx()));
5707     }
5708 }
5709
5710 template<bool big_endian>
5711 void
5712 Arm_output_section<big_endian>::fix_exidx_coverage(
5713     Layout* layout,
5714     const Text_section_list& sorted_text_sections,
5715     Symbol_table* symtab,
5716     bool merge_exidx_entries)
5717 {
5718   // We should only do this for the EXIDX output section.
5719   gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
5720
5721   // We don't want the relaxation loop to undo these changes, so we discard
5722   // the current saved states and take another one after the fix-up.
5723   this->discard_states();
5724
5725   // Remove all input sections.
5726   uint64_t address = this->address();
5727   typedef std::list<Output_section::Input_section> Input_section_list;
5728   Input_section_list input_sections;
5729   this->reset_address_and_file_offset();
5730   this->get_input_sections(address, std::string(""), &input_sections);
5731
5732   if (!this->input_sections().empty())
5733     gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
5734   
5735   // Go through all the known input sections and record them.
5736   typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5737   typedef Unordered_map<Section_id, const Output_section::Input_section*,
5738                         Section_id_hash> Text_to_exidx_map;
5739   Text_to_exidx_map text_to_exidx_map;
5740   for (Input_section_list::const_iterator p = input_sections.begin();
5741        p != input_sections.end();
5742        ++p)
5743     {
5744       // This should never happen.  At this point, we should only see
5745       // plain EXIDX input sections.
5746       gold_assert(!p->is_relaxed_input_section());
5747       text_to_exidx_map[Section_id(p->relobj(), p->shndx())] = &(*p);
5748     }
5749
5750   Arm_exidx_fixup exidx_fixup(this, merge_exidx_entries);
5751
5752   // Go over the sorted text sections.
5753   typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
5754   Section_id_set processed_input_sections;
5755   for (Text_section_list::const_iterator p = sorted_text_sections.begin();
5756        p != sorted_text_sections.end();
5757        ++p)
5758     {
5759       Relobj* relobj = p->first;
5760       unsigned int shndx = p->second;
5761
5762       Arm_relobj<big_endian>* arm_relobj =
5763          Arm_relobj<big_endian>::as_arm_relobj(relobj);
5764       const Arm_exidx_input_section* exidx_input_section =
5765          arm_relobj->exidx_input_section_by_link(shndx);
5766
5767       // If this text section has no EXIDX section or if the EXIDX section
5768       // has errors, force an EXIDX_CANTUNWIND entry pointing to the end
5769       // of the last seen EXIDX section.
5770       if (exidx_input_section == NULL || exidx_input_section->has_errors())
5771         {
5772           exidx_fixup.add_exidx_cantunwind_as_needed();
5773           continue;
5774         }
5775
5776       Relobj* exidx_relobj = exidx_input_section->relobj();
5777       unsigned int exidx_shndx = exidx_input_section->shndx();
5778       Section_id sid(exidx_relobj, exidx_shndx);
5779       Text_to_exidx_map::const_iterator iter = text_to_exidx_map.find(sid);
5780       if (iter == text_to_exidx_map.end())
5781         {
5782           // This is odd.  We have not seen this EXIDX input section before.
5783           // We cannot do fix-up.  If we saw a SECTIONS clause in a script,
5784           // issue a warning instead.  We assume the user knows what he
5785           // or she is doing.  Otherwise, this is an error.
5786           if (layout->script_options()->saw_sections_clause())
5787             gold_warning(_("unwinding may not work because EXIDX input section"
5788                            " %u of %s is not in EXIDX output section"),
5789                          exidx_shndx, exidx_relobj->name().c_str());
5790           else
5791             gold_error(_("unwinding may not work because EXIDX input section"
5792                          " %u of %s is not in EXIDX output section"),
5793                        exidx_shndx, exidx_relobj->name().c_str());
5794
5795           exidx_fixup.add_exidx_cantunwind_as_needed();
5796           continue;
5797         }
5798
5799       // Fix up coverage and append input section to output data list.
5800       Arm_exidx_section_offset_map* section_offset_map = NULL;
5801       uint32_t deleted_bytes =
5802         exidx_fixup.process_exidx_section<big_endian>(exidx_input_section,
5803                                                       &section_offset_map);
5804
5805       if (deleted_bytes == exidx_input_section->size())
5806         {
5807           // The whole EXIDX section got merged.  Remove it from output.
5808           gold_assert(section_offset_map == NULL);
5809           exidx_relobj->set_output_section(exidx_shndx, NULL);
5810
5811           // All local symbols defined in this input section will be dropped.
5812           // We need to adjust output local symbol count.
5813           arm_relobj->set_output_local_symbol_count_needs_update();
5814         }
5815       else if (deleted_bytes > 0)
5816         {
5817           // Some entries are merged.  We need to convert this EXIDX input
5818           // section into a relaxed section.
5819           gold_assert(section_offset_map != NULL);
5820           Arm_exidx_merged_section* merged_section =
5821             new Arm_exidx_merged_section(*exidx_input_section,
5822                                          *section_offset_map, deleted_bytes);
5823           this->add_relaxed_input_section(merged_section);
5824           arm_relobj->convert_input_section_to_relaxed_section(exidx_shndx);
5825
5826           // All local symbols defined in discarded portions of this input
5827           // section will be dropped.  We need to adjust output local symbol
5828           // count.
5829           arm_relobj->set_output_local_symbol_count_needs_update();
5830         }
5831       else
5832         {
5833           // Just add back the EXIDX input section.
5834           gold_assert(section_offset_map == NULL);
5835           const Output_section::Input_section* pis = iter->second;
5836           gold_assert(pis->is_input_section());
5837           this->add_script_input_section(*pis);
5838         }
5839
5840       processed_input_sections.insert(Section_id(exidx_relobj, exidx_shndx)); 
5841     }
5842
5843   // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
5844   exidx_fixup.add_exidx_cantunwind_as_needed();
5845
5846   // Remove any known EXIDX input sections that are not processed.
5847   for (Input_section_list::const_iterator p = input_sections.begin();
5848        p != input_sections.end();
5849        ++p)
5850     {
5851       if (processed_input_sections.find(Section_id(p->relobj(), p->shndx()))
5852           == processed_input_sections.end())
5853         {
5854           // We discard a known EXIDX section because its linked
5855           // text section has been folded by ICF.  We also discard an
5856           // EXIDX section with error, the output does not matter in this
5857           // case.  We do this to avoid triggering asserts.
5858           Arm_relobj<big_endian>* arm_relobj =
5859             Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5860           const Arm_exidx_input_section* exidx_input_section =
5861             arm_relobj->exidx_input_section_by_shndx(p->shndx());
5862           gold_assert(exidx_input_section != NULL);
5863           if (!exidx_input_section->has_errors())
5864             {
5865               unsigned int text_shndx = exidx_input_section->link();
5866               gold_assert(symtab->is_section_folded(p->relobj(), text_shndx));
5867             }
5868
5869           // Remove this from link.  We also need to recount the
5870           // local symbols.
5871           p->relobj()->set_output_section(p->shndx(), NULL);
5872           arm_relobj->set_output_local_symbol_count_needs_update();
5873         }
5874     }
5875     
5876   // Link exidx output section to the first seen output section and
5877   // set correct entry size.
5878   this->set_link_section(exidx_fixup.first_output_text_section());
5879   this->set_entsize(8);
5880
5881   // Make changes permanent.
5882   this->save_states();
5883   this->set_section_offsets_need_adjustment();
5884 }
5885
5886 // Link EXIDX output sections to text output sections.
5887
5888 template<bool big_endian>
5889 void
5890 Arm_output_section<big_endian>::set_exidx_section_link()
5891 {
5892   gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
5893   if (!this->input_sections().empty())
5894     {
5895       Input_section_list::const_iterator p = this->input_sections().begin();
5896       Arm_relobj<big_endian>* arm_relobj =
5897         Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
5898       unsigned exidx_shndx = p->shndx();
5899       const Arm_exidx_input_section* exidx_input_section =
5900         arm_relobj->exidx_input_section_by_shndx(exidx_shndx);
5901       gold_assert(exidx_input_section != NULL);
5902       unsigned int text_shndx = exidx_input_section->link();
5903       Output_section* os = arm_relobj->output_section(text_shndx);
5904       this->set_link_section(os);
5905     }
5906 }
5907
5908 // Arm_relobj methods.
5909
5910 // Determine if an input section is scannable for stub processing.  SHDR is
5911 // the header of the section and SHNDX is the section index.  OS is the output
5912 // section for the input section and SYMTAB is the global symbol table used to
5913 // look up ICF information.
5914
5915 template<bool big_endian>
5916 bool
5917 Arm_relobj<big_endian>::section_is_scannable(
5918     const elfcpp::Shdr<32, big_endian>& shdr,
5919     unsigned int shndx,
5920     const Output_section* os,
5921     const Symbol_table *symtab)
5922 {
5923   // Skip any empty sections, unallocated sections or sections whose
5924   // type are not SHT_PROGBITS.
5925   if (shdr.get_sh_size() == 0
5926       || (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
5927       || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
5928     return false;
5929
5930   // Skip any discarded or ICF'ed sections.
5931   if (os == NULL || symtab->is_section_folded(this, shndx))
5932     return false;
5933
5934   // If this requires special offset handling, check to see if it is
5935   // a relaxed section.  If this is not, then it is a merged section that
5936   // we cannot handle.
5937   if (this->is_output_section_offset_invalid(shndx))
5938     {
5939       const Output_relaxed_input_section* poris =
5940         os->find_relaxed_input_section(this, shndx);
5941       if (poris == NULL)
5942         return false;
5943     }
5944
5945   return true;
5946 }
5947
5948 // Determine if we want to scan the SHNDX-th section for relocation stubs.
5949 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
5950
5951 template<bool big_endian>
5952 bool
5953 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
5954     const elfcpp::Shdr<32, big_endian>& shdr,
5955     const Relobj::Output_sections& out_sections,
5956     const Symbol_table *symtab,
5957     const unsigned char* pshdrs)
5958 {
5959   unsigned int sh_type = shdr.get_sh_type();
5960   if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
5961     return false;
5962
5963   // Ignore empty section.
5964   off_t sh_size = shdr.get_sh_size();
5965   if (sh_size == 0)
5966     return false;
5967
5968   // Ignore reloc section with unexpected symbol table.  The
5969   // error will be reported in the final link.
5970   if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
5971     return false;
5972
5973   unsigned int reloc_size;
5974   if (sh_type == elfcpp::SHT_REL)
5975     reloc_size = elfcpp::Elf_sizes<32>::rel_size;
5976   else
5977     reloc_size = elfcpp::Elf_sizes<32>::rela_size;
5978
5979   // Ignore reloc section with unexpected entsize or uneven size.
5980   // The error will be reported in the final link.
5981   if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
5982     return false;
5983
5984   // Ignore reloc section with bad info.  This error will be
5985   // reported in the final link.
5986   unsigned int index = this->adjust_shndx(shdr.get_sh_info());
5987   if (index >= this->shnum())
5988     return false;
5989
5990   const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
5991   const elfcpp::Shdr<32, big_endian> text_shdr(pshdrs + index * shdr_size);
5992   return this->section_is_scannable(text_shdr, index,
5993                                    out_sections[index], symtab);
5994 }
5995
5996 // Return the output address of either a plain input section or a relaxed
5997 // input section.  SHNDX is the section index.  We define and use this
5998 // instead of calling Output_section::output_address because that is slow
5999 // for large output.
6000
6001 template<bool big_endian>
6002 Arm_address
6003 Arm_relobj<big_endian>::simple_input_section_output_address(
6004     unsigned int shndx,
6005     Output_section* os)
6006 {
6007   if (this->is_output_section_offset_invalid(shndx))
6008     {
6009       const Output_relaxed_input_section* poris =
6010         os->find_relaxed_input_section(this, shndx);
6011       // We do not handle merged sections here.
6012       gold_assert(poris != NULL);
6013       return poris->address();
6014     }
6015   else
6016     return os->address() + this->get_output_section_offset(shndx);
6017 }
6018
6019 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
6020 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
6021
6022 template<bool big_endian>
6023 bool
6024 Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
6025     const elfcpp::Shdr<32, big_endian>& shdr,
6026     unsigned int shndx,
6027     Output_section* os,
6028     const Symbol_table* symtab)
6029 {
6030   if (!this->section_is_scannable(shdr, shndx, os, symtab))
6031     return false;
6032
6033   // If the section does not cross any 4K-boundaries, it does not need to
6034   // be scanned.
6035   Arm_address address = this->simple_input_section_output_address(shndx, os);
6036   if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
6037     return false;
6038
6039   return true;
6040 }
6041
6042 // Scan a section for Cortex-A8 workaround.
6043
6044 template<bool big_endian>
6045 void
6046 Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
6047     const elfcpp::Shdr<32, big_endian>& shdr,
6048     unsigned int shndx,
6049     Output_section* os,
6050     Target_arm<big_endian>* arm_target)
6051 {
6052   // Look for the first mapping symbol in this section.  It should be
6053   // at (shndx, 0).
6054   Mapping_symbol_position section_start(shndx, 0);
6055   typename Mapping_symbols_info::const_iterator p =
6056     this->mapping_symbols_info_.lower_bound(section_start);
6057
6058   // There are no mapping symbols for this section.  Treat it as a data-only
6059   // section.  Issue a warning if section is marked as containing
6060   // instructions.
6061   if (p == this->mapping_symbols_info_.end() || p->first.first != shndx)
6062     {
6063       if ((this->section_flags(shndx) & elfcpp::SHF_EXECINSTR) != 0)
6064         gold_warning(_("cannot scan executable section %u of %s for Cortex-A8 "
6065                        "erratum because it has no mapping symbols."),
6066                      shndx, this->name().c_str());
6067       return;
6068     }
6069
6070   Arm_address output_address =
6071     this->simple_input_section_output_address(shndx, os);
6072
6073   // Get the section contents.
6074   section_size_type input_view_size = 0;
6075   const unsigned char* input_view =
6076     this->section_contents(shndx, &input_view_size, false);
6077
6078   // We need to go through the mapping symbols to determine what to
6079   // scan.  There are two reasons.  First, we should look at THUMB code and
6080   // THUMB code only.  Second, we only want to look at the 4K-page boundary
6081   // to speed up the scanning.
6082   
6083   while (p != this->mapping_symbols_info_.end()
6084         && p->first.first == shndx)
6085     {
6086       typename Mapping_symbols_info::const_iterator next =
6087         this->mapping_symbols_info_.upper_bound(p->first);
6088
6089       // Only scan part of a section with THUMB code.
6090       if (p->second == 't')
6091         {
6092           // Determine the end of this range.
6093           section_size_type span_start =
6094             convert_to_section_size_type(p->first.second);
6095           section_size_type span_end;
6096           if (next != this->mapping_symbols_info_.end()
6097               && next->first.first == shndx)
6098             span_end = convert_to_section_size_type(next->first.second);
6099           else
6100             span_end = convert_to_section_size_type(shdr.get_sh_size());
6101           
6102           if (((span_start + output_address) & ~0xfffUL)
6103               != ((span_end + output_address - 1) & ~0xfffUL))
6104             {
6105               arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
6106                                                           span_start, span_end,
6107                                                           input_view,
6108                                                           output_address);
6109             }
6110         }
6111
6112       p = next; 
6113     }
6114 }
6115
6116 // Scan relocations for stub generation.
6117
6118 template<bool big_endian>
6119 void
6120 Arm_relobj<big_endian>::scan_sections_for_stubs(
6121     Target_arm<big_endian>* arm_target,
6122     const Symbol_table* symtab,
6123     const Layout* layout)
6124 {
6125   unsigned int shnum = this->shnum();
6126   const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6127
6128   // Read the section headers.
6129   const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
6130                                                shnum * shdr_size,
6131                                                true, true);
6132
6133   // To speed up processing, we set up hash tables for fast lookup of
6134   // input offsets to output addresses.
6135   this->initialize_input_to_output_maps();
6136
6137   const Relobj::Output_sections& out_sections(this->output_sections());
6138
6139   Relocate_info<32, big_endian> relinfo;
6140   relinfo.symtab = symtab;
6141   relinfo.layout = layout;
6142   relinfo.object = this;
6143
6144   // Do relocation stubs scanning.
6145   const unsigned char* p = pshdrs + shdr_size;
6146   for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
6147     {
6148       const elfcpp::Shdr<32, big_endian> shdr(p);
6149       if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
6150                                                   pshdrs))
6151         {
6152           unsigned int index = this->adjust_shndx(shdr.get_sh_info());
6153           Arm_address output_offset = this->get_output_section_offset(index);
6154           Arm_address output_address;
6155           if (output_offset != invalid_address)
6156             output_address = out_sections[index]->address() + output_offset;
6157           else
6158             {
6159               // Currently this only happens for a relaxed section.
6160               const Output_relaxed_input_section* poris =
6161               out_sections[index]->find_relaxed_input_section(this, index);
6162               gold_assert(poris != NULL);
6163               output_address = poris->address();
6164             }
6165
6166           // Get the relocations.
6167           const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
6168                                                         shdr.get_sh_size(),
6169                                                         true, false);
6170
6171           // Get the section contents.  This does work for the case in which
6172           // we modify the contents of an input section.  We need to pass the
6173           // output view under such circumstances.
6174           section_size_type input_view_size = 0;
6175           const unsigned char* input_view =
6176             this->section_contents(index, &input_view_size, false);
6177
6178           relinfo.reloc_shndx = i;
6179           relinfo.data_shndx = index;
6180           unsigned int sh_type = shdr.get_sh_type();
6181           unsigned int reloc_size;
6182           if (sh_type == elfcpp::SHT_REL)
6183             reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6184           else
6185             reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6186
6187           Output_section* os = out_sections[index];
6188           arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
6189                                              shdr.get_sh_size() / reloc_size,
6190                                              os,
6191                                              output_offset == invalid_address,
6192                                              input_view, output_address,
6193                                              input_view_size);
6194         }
6195     }
6196
6197   // Do Cortex-A8 erratum stubs scanning.  This has to be done for a section
6198   // after its relocation section, if there is one, is processed for
6199   // relocation stubs.  Merging this loop with the one above would have been
6200   // complicated since we would have had to make sure that relocation stub
6201   // scanning is done first.
6202   if (arm_target->fix_cortex_a8())
6203     {
6204       const unsigned char* p = pshdrs + shdr_size;
6205       for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
6206         {
6207           const elfcpp::Shdr<32, big_endian> shdr(p);
6208           if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
6209                                                           out_sections[i],
6210                                                           symtab))
6211             this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
6212                                                      arm_target);
6213         }
6214     }
6215
6216   // After we've done the relocations, we release the hash tables,
6217   // since we no longer need them.
6218   this->free_input_to_output_maps();
6219 }
6220
6221 // Count the local symbols.  The ARM backend needs to know if a symbol
6222 // is a THUMB function or not.  For global symbols, it is easy because
6223 // the Symbol object keeps the ELF symbol type.  For local symbol it is
6224 // harder because we cannot access this information.   So we override the
6225 // do_count_local_symbol in parent and scan local symbols to mark
6226 // THUMB functions.  This is not the most efficient way but I do not want to
6227 // slow down other ports by calling a per symbol targer hook inside
6228 // Sized_relobj<size, big_endian>::do_count_local_symbols. 
6229
6230 template<bool big_endian>
6231 void
6232 Arm_relobj<big_endian>::do_count_local_symbols(
6233     Stringpool_template<char>* pool,
6234     Stringpool_template<char>* dynpool)
6235 {
6236   // We need to fix-up the values of any local symbols whose type are
6237   // STT_ARM_TFUNC.
6238   
6239   // Ask parent to count the local symbols.
6240   Sized_relobj<32, big_endian>::do_count_local_symbols(pool, dynpool);
6241   const unsigned int loccount = this->local_symbol_count();
6242   if (loccount == 0)
6243     return;
6244
6245   // Intialize the thumb function bit-vector.
6246   std::vector<bool> empty_vector(loccount, false);
6247   this->local_symbol_is_thumb_function_.swap(empty_vector);
6248
6249   // Read the symbol table section header.
6250   const unsigned int symtab_shndx = this->symtab_shndx();
6251   elfcpp::Shdr<32, big_endian>
6252       symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6253   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6254
6255   // Read the local symbols.
6256   const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6257   gold_assert(loccount == symtabshdr.get_sh_info());
6258   off_t locsize = loccount * sym_size;
6259   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6260                                               locsize, true, true);
6261
6262   // For mapping symbol processing, we need to read the symbol names.
6263   unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
6264   if (strtab_shndx >= this->shnum())
6265     {
6266       this->error(_("invalid symbol table name index: %u"), strtab_shndx);
6267       return;
6268     }
6269
6270   elfcpp::Shdr<32, big_endian>
6271     strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
6272   if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
6273     {
6274       this->error(_("symbol table name section has wrong type: %u"),
6275                   static_cast<unsigned int>(strtabshdr.get_sh_type()));
6276       return;
6277     }
6278   const char* pnames =
6279     reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
6280                                                  strtabshdr.get_sh_size(),
6281                                                  false, false));
6282
6283   // Loop over the local symbols and mark any local symbols pointing
6284   // to THUMB functions.
6285
6286   // Skip the first dummy symbol.
6287   psyms += sym_size;
6288   typename Sized_relobj<32, big_endian>::Local_values* plocal_values =
6289     this->local_values();
6290   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6291     {
6292       elfcpp::Sym<32, big_endian> sym(psyms);
6293       elfcpp::STT st_type = sym.get_st_type();
6294       Symbol_value<32>& lv((*plocal_values)[i]);
6295       Arm_address input_value = lv.input_value();
6296
6297       // Check to see if this is a mapping symbol.
6298       const char* sym_name = pnames + sym.get_st_name();
6299       if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
6300         {
6301           bool is_ordinary;
6302           unsigned int input_shndx =
6303             this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
6304           gold_assert(is_ordinary);
6305
6306           // Strip of LSB in case this is a THUMB symbol.
6307           Mapping_symbol_position msp(input_shndx, input_value & ~1U);
6308           this->mapping_symbols_info_[msp] = sym_name[1];
6309         }
6310
6311       if (st_type == elfcpp::STT_ARM_TFUNC
6312           || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
6313         {
6314           // This is a THUMB function.  Mark this and canonicalize the
6315           // symbol value by setting LSB.
6316           this->local_symbol_is_thumb_function_[i] = true;
6317           if ((input_value & 1) == 0)
6318             lv.set_input_value(input_value | 1);
6319         }
6320     }
6321 }
6322
6323 // Relocate sections.
6324 template<bool big_endian>
6325 void
6326 Arm_relobj<big_endian>::do_relocate_sections(
6327     const Symbol_table* symtab,
6328     const Layout* layout,
6329     const unsigned char* pshdrs,
6330     typename Sized_relobj<32, big_endian>::Views* pviews)
6331 {
6332   // Call parent to relocate sections.
6333   Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
6334                                                      pviews); 
6335
6336   // We do not generate stubs if doing a relocatable link.
6337   if (parameters->options().relocatable())
6338     return;
6339
6340   // Relocate stub tables.
6341   unsigned int shnum = this->shnum();
6342
6343   Target_arm<big_endian>* arm_target =
6344     Target_arm<big_endian>::default_target();
6345
6346   Relocate_info<32, big_endian> relinfo;
6347   relinfo.symtab = symtab;
6348   relinfo.layout = layout;
6349   relinfo.object = this;
6350
6351   for (unsigned int i = 1; i < shnum; ++i)
6352     {
6353       Arm_input_section<big_endian>* arm_input_section =
6354         arm_target->find_arm_input_section(this, i);
6355
6356       if (arm_input_section != NULL
6357           && arm_input_section->is_stub_table_owner()
6358           && !arm_input_section->stub_table()->empty())
6359         {
6360           // We cannot discard a section if it owns a stub table.
6361           Output_section* os = this->output_section(i);
6362           gold_assert(os != NULL);
6363
6364           relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
6365           relinfo.reloc_shdr = NULL;
6366           relinfo.data_shndx = i;
6367           relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
6368
6369           gold_assert((*pviews)[i].view != NULL);
6370
6371           // We are passed the output section view.  Adjust it to cover the
6372           // stub table only.
6373           Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
6374           gold_assert((stub_table->address() >= (*pviews)[i].address)
6375                       && ((stub_table->address() + stub_table->data_size())
6376                           <= (*pviews)[i].address + (*pviews)[i].view_size));
6377
6378           off_t offset = stub_table->address() - (*pviews)[i].address;
6379           unsigned char* view = (*pviews)[i].view + offset;
6380           Arm_address address = stub_table->address();
6381           section_size_type view_size = stub_table->data_size();
6382  
6383           stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
6384                                      view_size);
6385         }
6386
6387       // Apply Cortex A8 workaround if applicable.
6388       if (this->section_has_cortex_a8_workaround(i))
6389         {
6390           unsigned char* view = (*pviews)[i].view;
6391           Arm_address view_address = (*pviews)[i].address;
6392           section_size_type view_size = (*pviews)[i].view_size;
6393           Stub_table<big_endian>* stub_table = this->stub_tables_[i];
6394
6395           // Adjust view to cover section.
6396           Output_section* os = this->output_section(i);
6397           gold_assert(os != NULL);
6398           Arm_address section_address =
6399             this->simple_input_section_output_address(i, os);
6400           uint64_t section_size = this->section_size(i);
6401
6402           gold_assert(section_address >= view_address
6403                       && ((section_address + section_size)
6404                           <= (view_address + view_size)));
6405
6406           unsigned char* section_view = view + (section_address - view_address);
6407
6408           // Apply the Cortex-A8 workaround to the output address range
6409           // corresponding to this input section.
6410           stub_table->apply_cortex_a8_workaround_to_address_range(
6411               arm_target,
6412               section_view,
6413               section_address,
6414               section_size);
6415         }
6416     }
6417 }
6418
6419 // Find the linked text section of an EXIDX section by looking the the first
6420 // relocation.  4.4.1 of the EHABI specifications says that an EXIDX section
6421 // must be linked to to its associated code section via the sh_link field of
6422 // its section header.  However, some tools are broken and the link is not
6423 // always set.  LD just drops such an EXIDX section silently, causing the
6424 // associated code not unwindabled.   Here we try a little bit harder to
6425 // discover the linked code section.
6426 //
6427 // PSHDR points to the section header of a relocation section of an EXIDX
6428 // section.  If we can find a linked text section, return true and
6429 // store the text section index in the location PSHNDX.  Otherwise
6430 // return false.
6431
6432 template<bool big_endian>
6433 bool
6434 Arm_relobj<big_endian>::find_linked_text_section(
6435     const unsigned char* pshdr,
6436     const unsigned char* psyms,
6437     unsigned int* pshndx)
6438 {
6439   elfcpp::Shdr<32, big_endian> shdr(pshdr);
6440   
6441   // If there is no relocation, we cannot find the linked text section.
6442   size_t reloc_size;
6443   if (shdr.get_sh_type() == elfcpp::SHT_REL)
6444       reloc_size = elfcpp::Elf_sizes<32>::rel_size;
6445   else
6446       reloc_size = elfcpp::Elf_sizes<32>::rela_size;
6447   size_t reloc_count = shdr.get_sh_size() / reloc_size;
6448  
6449   // Get the relocations.
6450   const unsigned char* prelocs =
6451       this->get_view(shdr.get_sh_offset(), shdr.get_sh_size(), true, false); 
6452
6453   // Find the REL31 relocation for the first word of the first EXIDX entry.
6454   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6455     {
6456       Arm_address r_offset;
6457       typename elfcpp::Elf_types<32>::Elf_WXword r_info;
6458       if (shdr.get_sh_type() == elfcpp::SHT_REL)
6459         {
6460           typename elfcpp::Rel<32, big_endian> reloc(prelocs);
6461           r_info = reloc.get_r_info();
6462           r_offset = reloc.get_r_offset();
6463         }
6464       else
6465         {
6466           typename elfcpp::Rela<32, big_endian> reloc(prelocs);
6467           r_info = reloc.get_r_info();
6468           r_offset = reloc.get_r_offset();
6469         }
6470
6471       unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
6472       if (r_type != elfcpp::R_ARM_PREL31 && r_type != elfcpp::R_ARM_SBREL31)
6473         continue;
6474
6475       unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
6476       if (r_sym == 0
6477           || r_sym >= this->local_symbol_count()
6478           || r_offset != 0)
6479         continue;
6480
6481       // This is the relocation for the first word of the first EXIDX entry.
6482       // We expect to see a local section symbol.
6483       const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6484       elfcpp::Sym<32, big_endian> sym(psyms + r_sym * sym_size);
6485       if (sym.get_st_type() == elfcpp::STT_SECTION)
6486         {
6487           bool is_ordinary;
6488           *pshndx =
6489             this->adjust_sym_shndx(r_sym, sym.get_st_shndx(), &is_ordinary);
6490           gold_assert(is_ordinary);
6491           return true;
6492         }
6493       else
6494         return false;
6495     }
6496
6497   return false;
6498 }
6499
6500 // Make an EXIDX input section object for an EXIDX section whose index is
6501 // SHNDX.  SHDR is the section header of the EXIDX section and TEXT_SHNDX
6502 // is the section index of the linked text section.
6503
6504 template<bool big_endian>
6505 void
6506 Arm_relobj<big_endian>::make_exidx_input_section(
6507     unsigned int shndx,
6508     const elfcpp::Shdr<32, big_endian>& shdr,
6509     unsigned int text_shndx,
6510     const elfcpp::Shdr<32, big_endian>& text_shdr)
6511 {
6512   // Create an Arm_exidx_input_section object for this EXIDX section.
6513   Arm_exidx_input_section* exidx_input_section =
6514     new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
6515                                 shdr.get_sh_addralign());
6516
6517   gold_assert(this->exidx_section_map_[shndx] == NULL);
6518   this->exidx_section_map_[shndx] = exidx_input_section;
6519
6520   if (text_shndx == elfcpp::SHN_UNDEF || text_shndx >= this->shnum())
6521     {
6522       gold_error(_("EXIDX section %s(%u) links to invalid section %u in %s"),
6523                  this->section_name(shndx).c_str(), shndx, text_shndx,
6524                  this->name().c_str());
6525       exidx_input_section->set_has_errors();
6526     } 
6527   else if (this->exidx_section_map_[text_shndx] != NULL)
6528     {
6529       unsigned other_exidx_shndx =
6530         this->exidx_section_map_[text_shndx]->shndx();
6531       gold_error(_("EXIDX sections %s(%u) and %s(%u) both link to text section"
6532                    "%s(%u) in %s"),
6533                  this->section_name(shndx).c_str(), shndx,
6534                  this->section_name(other_exidx_shndx).c_str(),
6535                  other_exidx_shndx, this->section_name(text_shndx).c_str(),
6536                  text_shndx, this->name().c_str());
6537       exidx_input_section->set_has_errors();
6538     }
6539   else
6540      this->exidx_section_map_[text_shndx] = exidx_input_section;
6541
6542   // Check section flags of text section.
6543   if ((text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
6544     {
6545       gold_error(_("EXIDX section %s(%u) links to non-allocated section %s(%u) "
6546                    " in %s"),
6547                  this->section_name(shndx).c_str(), shndx,
6548                  this->section_name(text_shndx).c_str(), text_shndx,
6549                  this->name().c_str());
6550       exidx_input_section->set_has_errors();
6551     }
6552   else if ((text_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) == 0)
6553     // I would like to make this an error but currenlty ld just ignores
6554     // this.
6555     gold_warning(_("EXIDX section %s(%u) links to non-executable section "
6556                    "%s(%u) in %s"),
6557                  this->section_name(shndx).c_str(), shndx,
6558                  this->section_name(text_shndx).c_str(), text_shndx,
6559                  this->name().c_str());
6560 }
6561
6562 // Read the symbol information.
6563
6564 template<bool big_endian>
6565 void
6566 Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6567 {
6568   // Call parent class to read symbol information.
6569   Sized_relobj<32, big_endian>::do_read_symbols(sd);
6570
6571   // If this input file is a binary file, it has no processor
6572   // specific flags and attributes section.
6573   Input_file::Format format = this->input_file()->format();
6574   if (format != Input_file::FORMAT_ELF)
6575     {
6576       gold_assert(format == Input_file::FORMAT_BINARY);
6577       this->merge_flags_and_attributes_ = false;
6578       return;
6579     }
6580
6581   // Read processor-specific flags in ELF file header.
6582   const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6583                                               elfcpp::Elf_sizes<32>::ehdr_size,
6584                                               true, false);
6585   elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6586   this->processor_specific_flags_ = ehdr.get_e_flags();
6587
6588   // Go over the section headers and look for .ARM.attributes and .ARM.exidx
6589   // sections.
6590   std::vector<unsigned int> deferred_exidx_sections;
6591   const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6592   const unsigned char* pshdrs = sd->section_headers->data();
6593   const unsigned char *ps = pshdrs + shdr_size;
6594   bool must_merge_flags_and_attributes = false;
6595   for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6596     {
6597       elfcpp::Shdr<32, big_endian> shdr(ps);
6598
6599       // Sometimes an object has no contents except the section name string
6600       // table and an empty symbol table with the undefined symbol.  We
6601       // don't want to merge processor-specific flags from such an object.
6602       if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
6603         {
6604           // Symbol table is not empty.
6605           const elfcpp::Elf_types<32>::Elf_WXword sym_size =
6606              elfcpp::Elf_sizes<32>::sym_size;
6607           if (shdr.get_sh_size() > sym_size)
6608             must_merge_flags_and_attributes = true;
6609         }
6610       else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
6611         // If this is neither an empty symbol table nor a string table,
6612         // be conservative.
6613         must_merge_flags_and_attributes = true;
6614
6615       if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6616         {
6617           gold_assert(this->attributes_section_data_ == NULL);
6618           section_offset_type section_offset = shdr.get_sh_offset();
6619           section_size_type section_size =
6620             convert_to_section_size_type(shdr.get_sh_size());
6621           File_view* view = this->get_lasting_view(section_offset,
6622                                                    section_size, true, false);
6623           this->attributes_section_data_ =
6624             new Attributes_section_data(view->data(), section_size);
6625         }
6626       else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6627         {
6628           unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6629           if (text_shndx == elfcpp::SHN_UNDEF)
6630             deferred_exidx_sections.push_back(i);
6631           else
6632             {
6633               elfcpp::Shdr<32, big_endian> text_shdr(pshdrs
6634                                                      + text_shndx * shdr_size);
6635               this->make_exidx_input_section(i, shdr, text_shndx, text_shdr);
6636             }
6637         }
6638     }
6639
6640   // This is rare.
6641   if (!must_merge_flags_and_attributes)
6642     {
6643       gold_assert(deferred_exidx_sections.empty());
6644       this->merge_flags_and_attributes_ = false;
6645       return;
6646     }
6647
6648   // Some tools are broken and they do not set the link of EXIDX sections. 
6649   // We look at the first relocation to figure out the linked sections.
6650   if (!deferred_exidx_sections.empty())
6651     {
6652       // We need to go over the section headers again to find the mapping
6653       // from sections being relocated to their relocation sections.  This is
6654       // a bit inefficient as we could do that in the loop above.  However,
6655       // we do not expect any deferred EXIDX sections normally.  So we do not
6656       // want to slow down the most common path.
6657       typedef Unordered_map<unsigned int, unsigned int> Reloc_map;
6658       Reloc_map reloc_map;
6659       ps = pshdrs + shdr_size;
6660       for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
6661         {
6662           elfcpp::Shdr<32, big_endian> shdr(ps);
6663           elfcpp::Elf_Word sh_type = shdr.get_sh_type();
6664           if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
6665             {
6666               unsigned int info_shndx = this->adjust_shndx(shdr.get_sh_info());
6667               if (info_shndx >= this->shnum())
6668                 gold_error(_("relocation section %u has invalid info %u"),
6669                            i, info_shndx);
6670               Reloc_map::value_type value(info_shndx, i);
6671               std::pair<Reloc_map::iterator, bool> result =
6672                 reloc_map.insert(value);
6673               if (!result.second)
6674                 gold_error(_("section %u has multiple relocation sections "
6675                              "%u and %u"),
6676                            info_shndx, i, reloc_map[info_shndx]);
6677             }
6678         }
6679
6680       // Read the symbol table section header.
6681       const unsigned int symtab_shndx = this->symtab_shndx();
6682       elfcpp::Shdr<32, big_endian>
6683           symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6684       gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6685
6686       // Read the local symbols.
6687       const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
6688       const unsigned int loccount = this->local_symbol_count();
6689       gold_assert(loccount == symtabshdr.get_sh_info());
6690       off_t locsize = loccount * sym_size;
6691       const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6692                                                   locsize, true, true);
6693
6694       // Process the deferred EXIDX sections. 
6695       for(unsigned int i = 0; i < deferred_exidx_sections.size(); ++i)
6696         {
6697           unsigned int shndx = deferred_exidx_sections[i];
6698           elfcpp::Shdr<32, big_endian> shdr(pshdrs + shndx * shdr_size);
6699           unsigned int text_shndx = elfcpp::SHN_UNDEF;
6700           Reloc_map::const_iterator it = reloc_map.find(shndx);
6701           if (it != reloc_map.end())
6702             find_linked_text_section(pshdrs + it->second * shdr_size,
6703                                      psyms, &text_shndx);
6704           elfcpp::Shdr<32, big_endian> text_shdr(pshdrs
6705                                                  + text_shndx * shdr_size);
6706           this->make_exidx_input_section(shndx, shdr, text_shndx, text_shdr);
6707         }
6708     }
6709 }
6710
6711 // Process relocations for garbage collection.  The ARM target uses .ARM.exidx
6712 // sections for unwinding.  These sections are referenced implicitly by 
6713 // text sections linked in the section headers.  If we ignore these implict
6714 // references, the .ARM.exidx sections and any .ARM.extab sections they use
6715 // will be garbage-collected incorrectly.  Hence we override the same function
6716 // in the base class to handle these implicit references.
6717
6718 template<bool big_endian>
6719 void
6720 Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
6721                                              Layout* layout,
6722                                              Read_relocs_data* rd)
6723 {
6724   // First, call base class method to process relocations in this object.
6725   Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
6726
6727   // If --gc-sections is not specified, there is nothing more to do.
6728   // This happens when --icf is used but --gc-sections is not.
6729   if (!parameters->options().gc_sections())
6730     return;
6731   
6732   unsigned int shnum = this->shnum();
6733   const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6734   const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
6735                                                shnum * shdr_size,
6736                                                true, true);
6737
6738   // Scan section headers for sections of type SHT_ARM_EXIDX.  Add references
6739   // to these from the linked text sections.
6740   const unsigned char* ps = pshdrs + shdr_size;
6741   for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
6742     {
6743       elfcpp::Shdr<32, big_endian> shdr(ps);
6744       if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
6745         {
6746           // Found an .ARM.exidx section, add it to the set of reachable
6747           // sections from its linked text section.
6748           unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
6749           symtab->gc()->add_reference(this, text_shndx, this, i);
6750         }
6751     }
6752 }
6753
6754 // Update output local symbol count.  Owing to EXIDX entry merging, some local
6755 // symbols  will be removed in output.  Adjust output local symbol count
6756 // accordingly.  We can only changed the static output local symbol count.  It
6757 // is too late to change the dynamic symbols.
6758
6759 template<bool big_endian>
6760 void
6761 Arm_relobj<big_endian>::update_output_local_symbol_count()
6762 {
6763   // Caller should check that this needs updating.  We want caller checking
6764   // because output_local_symbol_count_needs_update() is most likely inlined.
6765   gold_assert(this->output_local_symbol_count_needs_update_);
6766
6767   gold_assert(this->symtab_shndx() != -1U);
6768   if (this->symtab_shndx() == 0)
6769     {
6770       // This object has no symbols.  Weird but legal.
6771       return;
6772     }
6773
6774   // Read the symbol table section header.
6775   const unsigned int symtab_shndx = this->symtab_shndx();
6776   elfcpp::Shdr<32, big_endian>
6777     symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
6778   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
6779
6780   // Read the local symbols.
6781   const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
6782   const unsigned int loccount = this->local_symbol_count();
6783   gold_assert(loccount == symtabshdr.get_sh_info());
6784   off_t locsize = loccount * sym_size;
6785   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
6786                                               locsize, true, true);
6787
6788   // Loop over the local symbols.
6789
6790   typedef typename Sized_relobj<32, big_endian>::Output_sections
6791      Output_sections;
6792   const Output_sections& out_sections(this->output_sections());
6793   unsigned int shnum = this->shnum();
6794   unsigned int count = 0;
6795   // Skip the first, dummy, symbol.
6796   psyms += sym_size;
6797   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
6798     {
6799       elfcpp::Sym<32, big_endian> sym(psyms);
6800
6801       Symbol_value<32>& lv((*this->local_values())[i]);
6802
6803       // This local symbol was already discarded by do_count_local_symbols.
6804       if (lv.is_output_symtab_index_set() && !lv.has_output_symtab_entry())
6805         continue;
6806
6807       bool is_ordinary;
6808       unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
6809                                                   &is_ordinary);
6810
6811       if (shndx < shnum)
6812         {
6813           Output_section* os = out_sections[shndx];
6814
6815           // This local symbol no longer has an output section.  Discard it.
6816           if (os == NULL)
6817             {
6818               lv.set_no_output_symtab_entry();
6819               continue;
6820             }
6821
6822           // Currently we only discard parts of EXIDX input sections.
6823           // We explicitly check for a merged EXIDX input section to avoid
6824           // calling Output_section_data::output_offset unless necessary.
6825           if ((this->get_output_section_offset(shndx) == invalid_address)
6826               && (this->exidx_input_section_by_shndx(shndx) != NULL))
6827             {
6828               section_offset_type output_offset =
6829                 os->output_offset(this, shndx, lv.input_value());
6830               if (output_offset == -1)
6831                 {
6832                   // This symbol is defined in a part of an EXIDX input section
6833                   // that is discarded due to entry merging.
6834                   lv.set_no_output_symtab_entry();
6835                   continue;
6836                 }       
6837             }
6838         }
6839
6840       ++count;
6841     }
6842
6843   this->set_output_local_symbol_count(count);
6844   this->output_local_symbol_count_needs_update_ = false;
6845 }
6846
6847 // Arm_dynobj methods.
6848
6849 // Read the symbol information.
6850
6851 template<bool big_endian>
6852 void
6853 Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
6854 {
6855   // Call parent class to read symbol information.
6856   Sized_dynobj<32, big_endian>::do_read_symbols(sd);
6857
6858   // Read processor-specific flags in ELF file header.
6859   const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
6860                                               elfcpp::Elf_sizes<32>::ehdr_size,
6861                                               true, false);
6862   elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
6863   this->processor_specific_flags_ = ehdr.get_e_flags();
6864
6865   // Read the attributes section if there is one.
6866   // We read from the end because gas seems to put it near the end of
6867   // the section headers.
6868   const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
6869   const unsigned char *ps =
6870     sd->section_headers->data() + shdr_size * (this->shnum() - 1);
6871   for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
6872     {
6873       elfcpp::Shdr<32, big_endian> shdr(ps);
6874       if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
6875         {
6876           section_offset_type section_offset = shdr.get_sh_offset();
6877           section_size_type section_size =
6878             convert_to_section_size_type(shdr.get_sh_size());
6879           File_view* view = this->get_lasting_view(section_offset,
6880                                                    section_size, true, false);
6881           this->attributes_section_data_ =
6882             new Attributes_section_data(view->data(), section_size);
6883           break;
6884         }
6885     }
6886 }
6887
6888 // Stub_addend_reader methods.
6889
6890 // Read the addend of a REL relocation of type R_TYPE at VIEW.
6891
6892 template<bool big_endian>
6893 elfcpp::Elf_types<32>::Elf_Swxword
6894 Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
6895     unsigned int r_type,
6896     const unsigned char* view,
6897     const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
6898 {
6899   typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
6900   
6901   switch (r_type)
6902     {
6903     case elfcpp::R_ARM_CALL:
6904     case elfcpp::R_ARM_JUMP24:
6905     case elfcpp::R_ARM_PLT32:
6906       {
6907         typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6908         const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6909         Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
6910         return utils::sign_extend<26>(val << 2);
6911       }
6912
6913     case elfcpp::R_ARM_THM_CALL:
6914     case elfcpp::R_ARM_THM_JUMP24:
6915     case elfcpp::R_ARM_THM_XPC22:
6916       {
6917         typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6918         const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6919         Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6920         Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
6921         return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
6922       }
6923
6924     case elfcpp::R_ARM_THM_JUMP19:
6925       {
6926         typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
6927         const Valtype* wv = reinterpret_cast<const Valtype*>(view);
6928         Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
6929         Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
6930         return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
6931       }
6932
6933     default:
6934       gold_unreachable();
6935     }
6936 }
6937
6938 // Arm_output_data_got methods.
6939
6940 // Add a GOT pair for R_ARM_TLS_GD32.  The creates a pair of GOT entries.
6941 // The first one is initialized to be 1, which is the module index for
6942 // the main executable and the second one 0.  A reloc of the type
6943 // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
6944 // be applied by gold.  GSYM is a global symbol.
6945 //
6946 template<bool big_endian>
6947 void
6948 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
6949     unsigned int got_type,
6950     Symbol* gsym)
6951 {
6952   if (gsym->has_got_offset(got_type))
6953     return;
6954
6955   // We are doing a static link.  Just mark it as belong to module 1,
6956   // the executable.
6957   unsigned int got_offset = this->add_constant(1);
6958   gsym->set_got_offset(got_type, got_offset); 
6959   got_offset = this->add_constant(0);
6960   this->static_relocs_.push_back(Static_reloc(got_offset,
6961                                               elfcpp::R_ARM_TLS_DTPOFF32,
6962                                               gsym));
6963 }
6964
6965 // Same as the above but for a local symbol.
6966
6967 template<bool big_endian>
6968 void
6969 Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
6970   unsigned int got_type,
6971   Sized_relobj<32, big_endian>* object,
6972   unsigned int index)
6973 {
6974   if (object->local_has_got_offset(index, got_type))
6975     return;
6976
6977   // We are doing a static link.  Just mark it as belong to module 1,
6978   // the executable.
6979   unsigned int got_offset = this->add_constant(1);
6980   object->set_local_got_offset(index, got_type, got_offset);
6981   got_offset = this->add_constant(0);
6982   this->static_relocs_.push_back(Static_reloc(got_offset, 
6983                                               elfcpp::R_ARM_TLS_DTPOFF32, 
6984                                               object, index));
6985 }
6986
6987 template<bool big_endian>
6988 void
6989 Arm_output_data_got<big_endian>::do_write(Output_file* of)
6990 {
6991   // Call parent to write out GOT.
6992   Output_data_got<32, big_endian>::do_write(of);
6993
6994   // We are done if there is no fix up.
6995   if (this->static_relocs_.empty())
6996     return;
6997
6998   gold_assert(parameters->doing_static_link());
6999
7000   const off_t offset = this->offset();
7001   const section_size_type oview_size =
7002     convert_to_section_size_type(this->data_size());
7003   unsigned char* const oview = of->get_output_view(offset, oview_size);
7004
7005   Output_segment* tls_segment = this->layout_->tls_segment();
7006   gold_assert(tls_segment != NULL);
7007   
7008   // The thread pointer $tp points to the TCB, which is followed by the
7009   // TLS.  So we need to adjust $tp relative addressing by this amount.
7010   Arm_address aligned_tcb_size =
7011     align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
7012
7013   for (size_t i = 0; i < this->static_relocs_.size(); ++i)
7014     {
7015       Static_reloc& reloc(this->static_relocs_[i]);
7016       
7017       Arm_address value;
7018       if (!reloc.symbol_is_global())
7019         {
7020           Sized_relobj<32, big_endian>* object = reloc.relobj();
7021           const Symbol_value<32>* psymval =
7022             reloc.relobj()->local_symbol(reloc.index());
7023
7024           // We are doing static linking.  Issue an error and skip this
7025           // relocation if the symbol is undefined or in a discarded_section.
7026           bool is_ordinary;
7027           unsigned int shndx = psymval->input_shndx(&is_ordinary);
7028           if ((shndx == elfcpp::SHN_UNDEF)
7029               || (is_ordinary
7030                   && shndx != elfcpp::SHN_UNDEF
7031                   && !object->is_section_included(shndx)
7032                   && !this->symbol_table_->is_section_folded(object, shndx)))
7033             {
7034               gold_error(_("undefined or discarded local symbol %u from "
7035                            " object %s in GOT"),
7036                          reloc.index(), reloc.relobj()->name().c_str());
7037               continue;
7038             }
7039           
7040           value = psymval->value(object, 0);
7041         }
7042       else
7043         {
7044           const Symbol* gsym = reloc.symbol();
7045           gold_assert(gsym != NULL);
7046           if (gsym->is_forwarder())
7047             gsym = this->symbol_table_->resolve_forwards(gsym);
7048
7049           // We are doing static linking.  Issue an error and skip this
7050           // relocation if the symbol is undefined or in a discarded_section
7051           // unless it is a weakly_undefined symbol.
7052           if ((gsym->is_defined_in_discarded_section()
7053                || gsym->is_undefined())
7054               && !gsym->is_weak_undefined())
7055             {
7056               gold_error(_("undefined or discarded symbol %s in GOT"),
7057                          gsym->name());
7058               continue;
7059             }
7060
7061           if (!gsym->is_weak_undefined())
7062             {
7063               const Sized_symbol<32>* sym =
7064                 static_cast<const Sized_symbol<32>*>(gsym);
7065               value = sym->value();
7066             }
7067           else
7068               value = 0;
7069         }
7070
7071       unsigned got_offset = reloc.got_offset();
7072       gold_assert(got_offset < oview_size);
7073
7074       typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
7075       Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
7076       Valtype x;
7077       switch (reloc.r_type())
7078         {
7079         case elfcpp::R_ARM_TLS_DTPOFF32:
7080           x = value;
7081           break;
7082         case elfcpp::R_ARM_TLS_TPOFF32:
7083           x = value + aligned_tcb_size;
7084           break;
7085         default:
7086           gold_unreachable();
7087         }
7088       elfcpp::Swap<32, big_endian>::writeval(wv, x);
7089     }
7090
7091   of->write_output_view(offset, oview_size, oview);
7092 }
7093
7094 // A class to handle the PLT data.
7095
7096 template<bool big_endian>
7097 class Output_data_plt_arm : public Output_section_data
7098 {
7099  public:
7100   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
7101     Reloc_section;
7102
7103   Output_data_plt_arm(Layout*, Output_data_space*);
7104
7105   // Add an entry to the PLT.
7106   void
7107   add_entry(Symbol* gsym);
7108
7109   // Return the .rel.plt section data.
7110   const Reloc_section*
7111   rel_plt() const
7112   { return this->rel_; }
7113
7114  protected:
7115   void
7116   do_adjust_output_section(Output_section* os);
7117
7118   // Write to a map file.
7119   void
7120   do_print_to_mapfile(Mapfile* mapfile) const
7121   { mapfile->print_output_data(this, _("** PLT")); }
7122
7123  private:
7124   // Template for the first PLT entry.
7125   static const uint32_t first_plt_entry[5];
7126
7127   // Template for subsequent PLT entries. 
7128   static const uint32_t plt_entry[3];
7129
7130   // Set the final size.
7131   void
7132   set_final_data_size()
7133   {
7134     this->set_data_size(sizeof(first_plt_entry)
7135                         + this->count_ * sizeof(plt_entry));
7136   }
7137
7138   // Write out the PLT data.
7139   void
7140   do_write(Output_file*);
7141
7142   // The reloc section.
7143   Reloc_section* rel_;
7144   // The .got.plt section.
7145   Output_data_space* got_plt_;
7146   // The number of PLT entries.
7147   unsigned int count_;
7148 };
7149
7150 // Create the PLT section.  The ordinary .got section is an argument,
7151 // since we need to refer to the start.  We also create our own .got
7152 // section just for PLT entries.
7153
7154 template<bool big_endian>
7155 Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
7156                                                      Output_data_space* got_plt)
7157   : Output_section_data(4), got_plt_(got_plt), count_(0)
7158 {
7159   this->rel_ = new Reloc_section(false);
7160   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
7161                                   elfcpp::SHF_ALLOC, this->rel_, true, false,
7162                                   false, false);
7163 }
7164
7165 template<bool big_endian>
7166 void
7167 Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
7168 {
7169   os->set_entsize(0);
7170 }
7171
7172 // Add an entry to the PLT.
7173
7174 template<bool big_endian>
7175 void
7176 Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
7177 {
7178   gold_assert(!gsym->has_plt_offset());
7179
7180   // Note that when setting the PLT offset we skip the initial
7181   // reserved PLT entry.
7182   gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
7183                        + sizeof(first_plt_entry));
7184
7185   ++this->count_;
7186
7187   section_offset_type got_offset = this->got_plt_->current_data_size();
7188
7189   // Every PLT entry needs a GOT entry which points back to the PLT
7190   // entry (this will be changed by the dynamic linker, normally
7191   // lazily when the function is called).
7192   this->got_plt_->set_current_data_size(got_offset + 4);
7193
7194   // Every PLT entry needs a reloc.
7195   gsym->set_needs_dynsym_entry();
7196   this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
7197                          got_offset);
7198
7199   // Note that we don't need to save the symbol.  The contents of the
7200   // PLT are independent of which symbols are used.  The symbols only
7201   // appear in the relocations.
7202 }
7203
7204 // ARM PLTs.
7205 // FIXME:  This is not very flexible.  Right now this has only been tested
7206 // on armv5te.  If we are to support additional architecture features like
7207 // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
7208
7209 // The first entry in the PLT.
7210 template<bool big_endian>
7211 const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
7212 {
7213   0xe52de004,   // str   lr, [sp, #-4]!
7214   0xe59fe004,   // ldr   lr, [pc, #4]
7215   0xe08fe00e,   // add   lr, pc, lr 
7216   0xe5bef008,   // ldr   pc, [lr, #8]!
7217   0x00000000,   // &GOT[0] - .
7218 };
7219
7220 // Subsequent entries in the PLT.
7221
7222 template<bool big_endian>
7223 const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
7224 {
7225   0xe28fc600,   // add   ip, pc, #0xNN00000
7226   0xe28cca00,   // add   ip, ip, #0xNN000
7227   0xe5bcf000,   // ldr   pc, [ip, #0xNNN]!
7228 };
7229
7230 // Write out the PLT.  This uses the hand-coded instructions above,
7231 // and adjusts them as needed.  This is all specified by the arm ELF
7232 // Processor Supplement.
7233
7234 template<bool big_endian>
7235 void
7236 Output_data_plt_arm<big_endian>::do_write(Output_file* of)
7237 {
7238   const off_t offset = this->offset();
7239   const section_size_type oview_size =
7240     convert_to_section_size_type(this->data_size());
7241   unsigned char* const oview = of->get_output_view(offset, oview_size);
7242
7243   const off_t got_file_offset = this->got_plt_->offset();
7244   const section_size_type got_size =
7245     convert_to_section_size_type(this->got_plt_->data_size());
7246   unsigned char* const got_view = of->get_output_view(got_file_offset,
7247                                                       got_size);
7248   unsigned char* pov = oview;
7249
7250   Arm_address plt_address = this->address();
7251   Arm_address got_address = this->got_plt_->address();
7252
7253   // Write first PLT entry.  All but the last word are constants.
7254   const size_t num_first_plt_words = (sizeof(first_plt_entry)
7255                                       / sizeof(plt_entry[0]));
7256   for (size_t i = 0; i < num_first_plt_words - 1; i++)
7257     elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
7258   // Last word in first PLT entry is &GOT[0] - .
7259   elfcpp::Swap<32, big_endian>::writeval(pov + 16,
7260                                          got_address - (plt_address + 16));
7261   pov += sizeof(first_plt_entry);
7262
7263   unsigned char* got_pov = got_view;
7264
7265   memset(got_pov, 0, 12);
7266   got_pov += 12;
7267
7268   const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
7269   unsigned int plt_offset = sizeof(first_plt_entry);
7270   unsigned int plt_rel_offset = 0;
7271   unsigned int got_offset = 12;
7272   const unsigned int count = this->count_;
7273   for (unsigned int i = 0;
7274        i < count;
7275        ++i,
7276          pov += sizeof(plt_entry),
7277          got_pov += 4,
7278          plt_offset += sizeof(plt_entry),
7279          plt_rel_offset += rel_size,
7280          got_offset += 4)
7281     {
7282       // Set and adjust the PLT entry itself.
7283       int32_t offset = ((got_address + got_offset)
7284                          - (plt_address + plt_offset + 8));
7285
7286       gold_assert(offset >= 0 && offset < 0x0fffffff);
7287       uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
7288       elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
7289       uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
7290       elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
7291       uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
7292       elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
7293
7294       // Set the entry in the GOT.
7295       elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
7296     }
7297
7298   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
7299   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
7300
7301   of->write_output_view(offset, oview_size, oview);
7302   of->write_output_view(got_file_offset, got_size, got_view);
7303 }
7304
7305 // Create a PLT entry for a global symbol.
7306
7307 template<bool big_endian>
7308 void
7309 Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
7310                                        Symbol* gsym)
7311 {
7312   if (gsym->has_plt_offset())
7313     return;
7314
7315   if (this->plt_ == NULL)
7316     {
7317       // Create the GOT sections first.
7318       this->got_section(symtab, layout);
7319
7320       this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
7321       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
7322                                       (elfcpp::SHF_ALLOC
7323                                        | elfcpp::SHF_EXECINSTR),
7324                                       this->plt_, false, false, false, false);
7325     }
7326   this->plt_->add_entry(gsym);
7327 }
7328
7329 // Get the section to use for TLS_DESC relocations.
7330
7331 template<bool big_endian>
7332 typename Target_arm<big_endian>::Reloc_section*
7333 Target_arm<big_endian>::rel_tls_desc_section(Layout* layout) const
7334 {
7335   return this->plt_section()->rel_tls_desc(layout);
7336 }
7337
7338 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
7339
7340 template<bool big_endian>
7341 void
7342 Target_arm<big_endian>::define_tls_base_symbol(
7343     Symbol_table* symtab,
7344     Layout* layout)
7345 {
7346   if (this->tls_base_symbol_defined_)
7347     return;
7348
7349   Output_segment* tls_segment = layout->tls_segment();
7350   if (tls_segment != NULL)
7351     {
7352       bool is_exec = parameters->options().output_is_executable();
7353       symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
7354                                        Symbol_table::PREDEFINED,
7355                                        tls_segment, 0, 0,
7356                                        elfcpp::STT_TLS,
7357                                        elfcpp::STB_LOCAL,
7358                                        elfcpp::STV_HIDDEN, 0,
7359                                        (is_exec
7360                                         ? Symbol::SEGMENT_END
7361                                         : Symbol::SEGMENT_START),
7362                                        true);
7363     }
7364   this->tls_base_symbol_defined_ = true;
7365 }
7366
7367 // Create a GOT entry for the TLS module index.
7368
7369 template<bool big_endian>
7370 unsigned int
7371 Target_arm<big_endian>::got_mod_index_entry(
7372     Symbol_table* symtab,
7373     Layout* layout,
7374     Sized_relobj<32, big_endian>* object)
7375 {
7376   if (this->got_mod_index_offset_ == -1U)
7377     {
7378       gold_assert(symtab != NULL && layout != NULL && object != NULL);
7379       Arm_output_data_got<big_endian>* got = this->got_section(symtab, layout);
7380       unsigned int got_offset;
7381       if (!parameters->doing_static_link())
7382         {
7383           got_offset = got->add_constant(0);
7384           Reloc_section* rel_dyn = this->rel_dyn_section(layout);
7385           rel_dyn->add_local(object, 0, elfcpp::R_ARM_TLS_DTPMOD32, got,
7386                              got_offset);
7387         }
7388       else
7389         {
7390           // We are doing a static link.  Just mark it as belong to module 1,
7391           // the executable.
7392           got_offset = got->add_constant(1);
7393         }
7394
7395       got->add_constant(0);
7396       this->got_mod_index_offset_ = got_offset;
7397     }
7398   return this->got_mod_index_offset_;
7399 }
7400
7401 // Optimize the TLS relocation type based on what we know about the
7402 // symbol.  IS_FINAL is true if the final address of this symbol is
7403 // known at link time.
7404
7405 template<bool big_endian>
7406 tls::Tls_optimization
7407 Target_arm<big_endian>::optimize_tls_reloc(bool, int)
7408 {
7409   // FIXME: Currently we do not do any TLS optimization.
7410   return tls::TLSOPT_NONE;
7411 }
7412
7413 // Report an unsupported relocation against a local symbol.
7414
7415 template<bool big_endian>
7416 void
7417 Target_arm<big_endian>::Scan::unsupported_reloc_local(
7418     Sized_relobj<32, big_endian>* object,
7419     unsigned int r_type)
7420 {
7421   gold_error(_("%s: unsupported reloc %u against local symbol"),
7422              object->name().c_str(), r_type);
7423 }
7424
7425 // We are about to emit a dynamic relocation of type R_TYPE.  If the
7426 // dynamic linker does not support it, issue an error.  The GNU linker
7427 // only issues a non-PIC error for an allocated read-only section.
7428 // Here we know the section is allocated, but we don't know that it is
7429 // read-only.  But we check for all the relocation types which the
7430 // glibc dynamic linker supports, so it seems appropriate to issue an
7431 // error even if the section is not read-only.
7432
7433 template<bool big_endian>
7434 void
7435 Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
7436                                             unsigned int r_type)
7437 {
7438   switch (r_type)
7439     {
7440     // These are the relocation types supported by glibc for ARM.
7441     case elfcpp::R_ARM_RELATIVE:
7442     case elfcpp::R_ARM_COPY:
7443     case elfcpp::R_ARM_GLOB_DAT:
7444     case elfcpp::R_ARM_JUMP_SLOT:
7445     case elfcpp::R_ARM_ABS32:
7446     case elfcpp::R_ARM_ABS32_NOI:
7447     case elfcpp::R_ARM_PC24:
7448     // FIXME: The following 3 types are not supported by Android's dynamic
7449     // linker.
7450     case elfcpp::R_ARM_TLS_DTPMOD32:
7451     case elfcpp::R_ARM_TLS_DTPOFF32:
7452     case elfcpp::R_ARM_TLS_TPOFF32:
7453       return;
7454
7455     default:
7456       {
7457         // This prevents us from issuing more than one error per reloc
7458         // section.  But we can still wind up issuing more than one
7459         // error per object file.
7460         if (this->issued_non_pic_error_)
7461           return;
7462         const Arm_reloc_property* reloc_property =
7463           arm_reloc_property_table->get_reloc_property(r_type);
7464         gold_assert(reloc_property != NULL);
7465         object->error(_("requires unsupported dynamic reloc %s; "
7466                       "recompile with -fPIC"),
7467                       reloc_property->name().c_str());
7468         this->issued_non_pic_error_ = true;
7469         return;
7470       }
7471
7472     case elfcpp::R_ARM_NONE:
7473       gold_unreachable();
7474     }
7475 }
7476
7477 // Scan a relocation for a local symbol.
7478 // FIXME: This only handles a subset of relocation types used by Android
7479 // on ARM v5te devices.
7480
7481 template<bool big_endian>
7482 inline void
7483 Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
7484                                     Layout* layout,
7485                                     Target_arm* target,
7486                                     Sized_relobj<32, big_endian>* object,
7487                                     unsigned int data_shndx,
7488                                     Output_section* output_section,
7489                                     const elfcpp::Rel<32, big_endian>& reloc,
7490                                     unsigned int r_type,
7491                                     const elfcpp::Sym<32, big_endian>& lsym)
7492 {
7493   r_type = get_real_reloc_type(r_type);
7494   switch (r_type)
7495     {
7496     case elfcpp::R_ARM_NONE:
7497     case elfcpp::R_ARM_V4BX:
7498     case elfcpp::R_ARM_GNU_VTENTRY:
7499     case elfcpp::R_ARM_GNU_VTINHERIT:
7500       break;
7501
7502     case elfcpp::R_ARM_ABS32:
7503     case elfcpp::R_ARM_ABS32_NOI:
7504       // If building a shared library (or a position-independent
7505       // executable), we need to create a dynamic relocation for
7506       // this location. The relocation applied at link time will
7507       // apply the link-time value, so we flag the location with
7508       // an R_ARM_RELATIVE relocation so the dynamic loader can
7509       // relocate it easily.
7510       if (parameters->options().output_is_position_independent())
7511         {
7512           Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7513           unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7514           // If we are to add more other reloc types than R_ARM_ABS32,
7515           // we need to add check_non_pic(object, r_type) here.
7516           rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
7517                                       output_section, data_shndx,
7518                                       reloc.get_r_offset());
7519         }
7520       break;
7521
7522     case elfcpp::R_ARM_ABS16:
7523     case elfcpp::R_ARM_ABS12:
7524     case elfcpp::R_ARM_THM_ABS5:
7525     case elfcpp::R_ARM_ABS8:
7526     case elfcpp::R_ARM_BASE_ABS:
7527     case elfcpp::R_ARM_MOVW_ABS_NC:
7528     case elfcpp::R_ARM_MOVT_ABS:
7529     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7530     case elfcpp::R_ARM_THM_MOVT_ABS:
7531       // If building a shared library (or a position-independent
7532       // executable), we need to create a dynamic relocation for
7533       // this location. Because the addend needs to remain in the
7534       // data section, we need to be careful not to apply this
7535       // relocation statically.
7536       if (parameters->options().output_is_position_independent())
7537         {
7538           check_non_pic(object, r_type);
7539           Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7540           unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7541           if (lsym.get_st_type() != elfcpp::STT_SECTION)
7542             rel_dyn->add_local(object, r_sym, r_type, output_section,
7543                                data_shndx, reloc.get_r_offset());
7544           else
7545             {
7546               gold_assert(lsym.get_st_value() == 0);
7547               unsigned int shndx = lsym.get_st_shndx();
7548               bool is_ordinary;
7549               shndx = object->adjust_sym_shndx(r_sym, shndx,
7550                                                &is_ordinary);
7551               if (!is_ordinary)
7552                 object->error(_("section symbol %u has bad shndx %u"),
7553                               r_sym, shndx);
7554               else
7555                 rel_dyn->add_local_section(object, shndx,
7556                                            r_type, output_section,
7557                                            data_shndx, reloc.get_r_offset());
7558             }
7559         }
7560       break;
7561
7562     case elfcpp::R_ARM_PC24:
7563     case elfcpp::R_ARM_REL32:
7564     case elfcpp::R_ARM_LDR_PC_G0:
7565     case elfcpp::R_ARM_SBREL32:
7566     case elfcpp::R_ARM_THM_CALL:
7567     case elfcpp::R_ARM_THM_PC8:
7568     case elfcpp::R_ARM_BASE_PREL:
7569     case elfcpp::R_ARM_PLT32:
7570     case elfcpp::R_ARM_CALL:
7571     case elfcpp::R_ARM_JUMP24:
7572     case elfcpp::R_ARM_THM_JUMP24:
7573     case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7574     case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7575     case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7576     case elfcpp::R_ARM_SBREL31:
7577     case elfcpp::R_ARM_PREL31:
7578     case elfcpp::R_ARM_MOVW_PREL_NC:
7579     case elfcpp::R_ARM_MOVT_PREL:
7580     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7581     case elfcpp::R_ARM_THM_MOVT_PREL:
7582     case elfcpp::R_ARM_THM_JUMP19:
7583     case elfcpp::R_ARM_THM_JUMP6:
7584     case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7585     case elfcpp::R_ARM_THM_PC12:
7586     case elfcpp::R_ARM_REL32_NOI:
7587     case elfcpp::R_ARM_ALU_PC_G0_NC:
7588     case elfcpp::R_ARM_ALU_PC_G0:
7589     case elfcpp::R_ARM_ALU_PC_G1_NC:
7590     case elfcpp::R_ARM_ALU_PC_G1:
7591     case elfcpp::R_ARM_ALU_PC_G2:
7592     case elfcpp::R_ARM_LDR_PC_G1:
7593     case elfcpp::R_ARM_LDR_PC_G2:
7594     case elfcpp::R_ARM_LDRS_PC_G0:
7595     case elfcpp::R_ARM_LDRS_PC_G1:
7596     case elfcpp::R_ARM_LDRS_PC_G2:
7597     case elfcpp::R_ARM_LDC_PC_G0:
7598     case elfcpp::R_ARM_LDC_PC_G1:
7599     case elfcpp::R_ARM_LDC_PC_G2:
7600     case elfcpp::R_ARM_ALU_SB_G0_NC:
7601     case elfcpp::R_ARM_ALU_SB_G0:
7602     case elfcpp::R_ARM_ALU_SB_G1_NC:
7603     case elfcpp::R_ARM_ALU_SB_G1:
7604     case elfcpp::R_ARM_ALU_SB_G2:
7605     case elfcpp::R_ARM_LDR_SB_G0:
7606     case elfcpp::R_ARM_LDR_SB_G1:
7607     case elfcpp::R_ARM_LDR_SB_G2:
7608     case elfcpp::R_ARM_LDRS_SB_G0:
7609     case elfcpp::R_ARM_LDRS_SB_G1:
7610     case elfcpp::R_ARM_LDRS_SB_G2:
7611     case elfcpp::R_ARM_LDC_SB_G0:
7612     case elfcpp::R_ARM_LDC_SB_G1:
7613     case elfcpp::R_ARM_LDC_SB_G2:
7614     case elfcpp::R_ARM_MOVW_BREL_NC:
7615     case elfcpp::R_ARM_MOVT_BREL:
7616     case elfcpp::R_ARM_MOVW_BREL:
7617     case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7618     case elfcpp::R_ARM_THM_MOVT_BREL:
7619     case elfcpp::R_ARM_THM_MOVW_BREL:
7620     case elfcpp::R_ARM_THM_JUMP11:
7621     case elfcpp::R_ARM_THM_JUMP8:
7622       // We don't need to do anything for a relative addressing relocation
7623       // against a local symbol if it does not reference the GOT.
7624       break;
7625
7626     case elfcpp::R_ARM_GOTOFF32:
7627     case elfcpp::R_ARM_GOTOFF12:
7628       // We need a GOT section:
7629       target->got_section(symtab, layout);
7630       break;
7631
7632     case elfcpp::R_ARM_GOT_BREL:
7633     case elfcpp::R_ARM_GOT_PREL:
7634       {
7635         // The symbol requires a GOT entry.
7636         Arm_output_data_got<big_endian>* got =
7637           target->got_section(symtab, layout);
7638         unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7639         if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
7640           {
7641             // If we are generating a shared object, we need to add a
7642             // dynamic RELATIVE relocation for this symbol's GOT entry.
7643             if (parameters->options().output_is_position_independent())
7644               {
7645                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7646                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7647                 rel_dyn->add_local_relative(
7648                     object, r_sym, elfcpp::R_ARM_RELATIVE, got,
7649                     object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
7650               }
7651           }
7652       }
7653       break;
7654
7655     case elfcpp::R_ARM_TARGET1:
7656     case elfcpp::R_ARM_TARGET2:
7657       // This should have been mapped to another type already.
7658       // Fall through.
7659     case elfcpp::R_ARM_COPY:
7660     case elfcpp::R_ARM_GLOB_DAT:
7661     case elfcpp::R_ARM_JUMP_SLOT:
7662     case elfcpp::R_ARM_RELATIVE:
7663       // These are relocations which should only be seen by the
7664       // dynamic linker, and should never be seen here.
7665       gold_error(_("%s: unexpected reloc %u in object file"),
7666                  object->name().c_str(), r_type);
7667       break;
7668
7669
7670       // These are initial TLS relocs, which are expected when
7671       // linking.
7672     case elfcpp::R_ARM_TLS_GD32:        // Global-dynamic
7673     case elfcpp::R_ARM_TLS_LDM32:       // Local-dynamic
7674     case elfcpp::R_ARM_TLS_LDO32:       // Alternate local-dynamic
7675     case elfcpp::R_ARM_TLS_IE32:        // Initial-exec
7676     case elfcpp::R_ARM_TLS_LE32:        // Local-exec
7677       {
7678         bool output_is_shared = parameters->options().shared();
7679         const tls::Tls_optimization optimized_type
7680             = Target_arm<big_endian>::optimize_tls_reloc(!output_is_shared,
7681                                                          r_type);
7682         switch (r_type)
7683           {
7684           case elfcpp::R_ARM_TLS_GD32:          // Global-dynamic
7685             if (optimized_type == tls::TLSOPT_NONE)
7686               {
7687                 // Create a pair of GOT entries for the module index and
7688                 // dtv-relative offset.
7689                 Arm_output_data_got<big_endian>* got
7690                     = target->got_section(symtab, layout);
7691                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7692                 unsigned int shndx = lsym.get_st_shndx();
7693                 bool is_ordinary;
7694                 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7695                 if (!is_ordinary)
7696                   {
7697                     object->error(_("local symbol %u has bad shndx %u"),
7698                                   r_sym, shndx);
7699                     break;
7700                   }
7701
7702                 if (!parameters->doing_static_link())
7703                   got->add_local_pair_with_rel(object, r_sym, shndx,
7704                                                GOT_TYPE_TLS_PAIR,
7705                                                target->rel_dyn_section(layout),
7706                                                elfcpp::R_ARM_TLS_DTPMOD32, 0);
7707                 else
7708                   got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR,
7709                                                       object, r_sym);
7710               }
7711             else
7712               // FIXME: TLS optimization not supported yet.
7713               gold_unreachable();
7714             break;
7715
7716           case elfcpp::R_ARM_TLS_LDM32:         // Local-dynamic
7717             if (optimized_type == tls::TLSOPT_NONE)
7718               {
7719                 // Create a GOT entry for the module index.
7720                 target->got_mod_index_entry(symtab, layout, object);
7721               }
7722             else
7723               // FIXME: TLS optimization not supported yet.
7724               gold_unreachable();
7725             break;
7726
7727           case elfcpp::R_ARM_TLS_LDO32:         // Alternate local-dynamic
7728             break;
7729
7730           case elfcpp::R_ARM_TLS_IE32:          // Initial-exec
7731             layout->set_has_static_tls();
7732             if (optimized_type == tls::TLSOPT_NONE)
7733               {
7734                 // Create a GOT entry for the tp-relative offset.
7735                 Arm_output_data_got<big_endian>* got
7736                   = target->got_section(symtab, layout);
7737                 unsigned int r_sym =
7738                    elfcpp::elf_r_sym<32>(reloc.get_r_info());
7739                 if (!parameters->doing_static_link())
7740                     got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
7741                                             target->rel_dyn_section(layout),
7742                                             elfcpp::R_ARM_TLS_TPOFF32);
7743                 else if (!object->local_has_got_offset(r_sym,
7744                                                        GOT_TYPE_TLS_OFFSET))
7745                   {
7746                     got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
7747                     unsigned int got_offset =
7748                       object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
7749                     got->add_static_reloc(got_offset,
7750                                           elfcpp::R_ARM_TLS_TPOFF32, object,
7751                                           r_sym);
7752                   }
7753               }
7754             else
7755               // FIXME: TLS optimization not supported yet.
7756               gold_unreachable();
7757             break;
7758
7759           case elfcpp::R_ARM_TLS_LE32:          // Local-exec
7760             layout->set_has_static_tls();
7761             if (output_is_shared)
7762               {
7763                 // We need to create a dynamic relocation.
7764                 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
7765                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7766                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7767                 rel_dyn->add_local(object, r_sym, elfcpp::R_ARM_TLS_TPOFF32,
7768                                    output_section, data_shndx,
7769                                    reloc.get_r_offset());
7770               }
7771             break;
7772
7773           default:
7774             gold_unreachable();
7775           }
7776       }
7777       break;
7778
7779     default:
7780       unsupported_reloc_local(object, r_type);
7781       break;
7782     }
7783 }
7784
7785 // Report an unsupported relocation against a global symbol.
7786
7787 template<bool big_endian>
7788 void
7789 Target_arm<big_endian>::Scan::unsupported_reloc_global(
7790     Sized_relobj<32, big_endian>* object,
7791     unsigned int r_type,
7792     Symbol* gsym)
7793 {
7794   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7795              object->name().c_str(), r_type, gsym->demangled_name().c_str());
7796 }
7797
7798 template<bool big_endian>
7799 inline bool
7800 Target_arm<big_endian>::Scan::possible_function_pointer_reloc(
7801     unsigned int r_type)
7802 {
7803   switch (r_type)
7804     {
7805     case elfcpp::R_ARM_PC24:
7806     case elfcpp::R_ARM_THM_CALL:
7807     case elfcpp::R_ARM_PLT32:
7808     case elfcpp::R_ARM_CALL:
7809     case elfcpp::R_ARM_JUMP24:
7810     case elfcpp::R_ARM_THM_JUMP24:
7811     case elfcpp::R_ARM_SBREL31:
7812     case elfcpp::R_ARM_PREL31:
7813     case elfcpp::R_ARM_THM_JUMP19:
7814     case elfcpp::R_ARM_THM_JUMP6:
7815     case elfcpp::R_ARM_THM_JUMP11:
7816     case elfcpp::R_ARM_THM_JUMP8:
7817       // All the relocations above are branches except SBREL31 and PREL31.
7818       return false;
7819
7820     default:
7821       // Be conservative and assume this is a function pointer.
7822       return true;
7823     }
7824 }
7825
7826 template<bool big_endian>
7827 inline bool
7828 Target_arm<big_endian>::Scan::local_reloc_may_be_function_pointer(
7829   Symbol_table*,
7830   Layout*,
7831   Target_arm<big_endian>* target,
7832   Sized_relobj<32, big_endian>*,
7833   unsigned int,
7834   Output_section*,
7835   const elfcpp::Rel<32, big_endian>&,
7836   unsigned int r_type,
7837   const elfcpp::Sym<32, big_endian>&)
7838 {
7839   r_type = target->get_real_reloc_type(r_type);
7840   return possible_function_pointer_reloc(r_type);
7841 }
7842
7843 template<bool big_endian>
7844 inline bool
7845 Target_arm<big_endian>::Scan::global_reloc_may_be_function_pointer(
7846   Symbol_table*,
7847   Layout*,
7848   Target_arm<big_endian>* target,
7849   Sized_relobj<32, big_endian>*,
7850   unsigned int,
7851   Output_section*,
7852   const elfcpp::Rel<32, big_endian>&,
7853   unsigned int r_type,
7854   Symbol* gsym)
7855 {
7856   // GOT is not a function.
7857   if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7858     return false;
7859
7860   r_type = target->get_real_reloc_type(r_type);
7861   return possible_function_pointer_reloc(r_type);
7862 }
7863
7864 // Scan a relocation for a global symbol.
7865
7866 template<bool big_endian>
7867 inline void
7868 Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
7869                                      Layout* layout,
7870                                      Target_arm* target,
7871                                      Sized_relobj<32, big_endian>* object,
7872                                      unsigned int data_shndx,
7873                                      Output_section* output_section,
7874                                      const elfcpp::Rel<32, big_endian>& reloc,
7875                                      unsigned int r_type,
7876                                      Symbol* gsym)
7877 {
7878   // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
7879   // section.  We check here to avoid creating a dynamic reloc against
7880   // _GLOBAL_OFFSET_TABLE_.
7881   if (!target->has_got_section()
7882       && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7883     target->got_section(symtab, layout);
7884
7885   r_type = get_real_reloc_type(r_type);
7886   switch (r_type)
7887     {
7888     case elfcpp::R_ARM_NONE:
7889     case elfcpp::R_ARM_V4BX:
7890     case elfcpp::R_ARM_GNU_VTENTRY:
7891     case elfcpp::R_ARM_GNU_VTINHERIT:
7892       break;
7893
7894     case elfcpp::R_ARM_ABS32:
7895     case elfcpp::R_ARM_ABS16:
7896     case elfcpp::R_ARM_ABS12:
7897     case elfcpp::R_ARM_THM_ABS5:
7898     case elfcpp::R_ARM_ABS8:
7899     case elfcpp::R_ARM_BASE_ABS:
7900     case elfcpp::R_ARM_MOVW_ABS_NC:
7901     case elfcpp::R_ARM_MOVT_ABS:
7902     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
7903     case elfcpp::R_ARM_THM_MOVT_ABS:
7904     case elfcpp::R_ARM_ABS32_NOI:
7905       // Absolute addressing relocations.
7906       {
7907         // Make a PLT entry if necessary.
7908         if (this->symbol_needs_plt_entry(gsym))
7909           {
7910             target->make_plt_entry(symtab, layout, gsym);
7911             // Since this is not a PC-relative relocation, we may be
7912             // taking the address of a function. In that case we need to
7913             // set the entry in the dynamic symbol table to the address of
7914             // the PLT entry.
7915             if (gsym->is_from_dynobj() && !parameters->options().shared())
7916               gsym->set_needs_dynsym_value();
7917           }
7918         // Make a dynamic relocation if necessary.
7919         if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
7920           {
7921             if (gsym->may_need_copy_reloc())
7922               {
7923                 target->copy_reloc(symtab, layout, object,
7924                                    data_shndx, output_section, gsym, reloc);
7925               }
7926             else if ((r_type == elfcpp::R_ARM_ABS32
7927                       || r_type == elfcpp::R_ARM_ABS32_NOI)
7928                      && gsym->can_use_relative_reloc(false))
7929               {
7930                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7931                 rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
7932                                              output_section, object,
7933                                              data_shndx, reloc.get_r_offset());
7934               }
7935             else
7936               {
7937                 check_non_pic(object, r_type);
7938                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7939                 rel_dyn->add_global(gsym, r_type, output_section, object,
7940                                     data_shndx, reloc.get_r_offset());
7941               }
7942           }
7943       }
7944       break;
7945
7946     case elfcpp::R_ARM_GOTOFF32:
7947     case elfcpp::R_ARM_GOTOFF12:
7948       // We need a GOT section.
7949       target->got_section(symtab, layout);
7950       break;
7951       
7952     case elfcpp::R_ARM_REL32:
7953     case elfcpp::R_ARM_LDR_PC_G0:
7954     case elfcpp::R_ARM_SBREL32:
7955     case elfcpp::R_ARM_THM_PC8:
7956     case elfcpp::R_ARM_BASE_PREL:
7957     case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
7958     case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
7959     case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
7960     case elfcpp::R_ARM_MOVW_PREL_NC:
7961     case elfcpp::R_ARM_MOVT_PREL:
7962     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
7963     case elfcpp::R_ARM_THM_MOVT_PREL:
7964     case elfcpp::R_ARM_THM_ALU_PREL_11_0:
7965     case elfcpp::R_ARM_THM_PC12:
7966     case elfcpp::R_ARM_REL32_NOI:
7967     case elfcpp::R_ARM_ALU_PC_G0_NC:
7968     case elfcpp::R_ARM_ALU_PC_G0:
7969     case elfcpp::R_ARM_ALU_PC_G1_NC:
7970     case elfcpp::R_ARM_ALU_PC_G1:
7971     case elfcpp::R_ARM_ALU_PC_G2:
7972     case elfcpp::R_ARM_LDR_PC_G1:
7973     case elfcpp::R_ARM_LDR_PC_G2:
7974     case elfcpp::R_ARM_LDRS_PC_G0:
7975     case elfcpp::R_ARM_LDRS_PC_G1:
7976     case elfcpp::R_ARM_LDRS_PC_G2:
7977     case elfcpp::R_ARM_LDC_PC_G0:
7978     case elfcpp::R_ARM_LDC_PC_G1:
7979     case elfcpp::R_ARM_LDC_PC_G2:
7980     case elfcpp::R_ARM_ALU_SB_G0_NC:
7981     case elfcpp::R_ARM_ALU_SB_G0:
7982     case elfcpp::R_ARM_ALU_SB_G1_NC:
7983     case elfcpp::R_ARM_ALU_SB_G1:
7984     case elfcpp::R_ARM_ALU_SB_G2:
7985     case elfcpp::R_ARM_LDR_SB_G0:
7986     case elfcpp::R_ARM_LDR_SB_G1:
7987     case elfcpp::R_ARM_LDR_SB_G2:
7988     case elfcpp::R_ARM_LDRS_SB_G0:
7989     case elfcpp::R_ARM_LDRS_SB_G1:
7990     case elfcpp::R_ARM_LDRS_SB_G2:
7991     case elfcpp::R_ARM_LDC_SB_G0:
7992     case elfcpp::R_ARM_LDC_SB_G1:
7993     case elfcpp::R_ARM_LDC_SB_G2:
7994     case elfcpp::R_ARM_MOVW_BREL_NC:
7995     case elfcpp::R_ARM_MOVT_BREL:
7996     case elfcpp::R_ARM_MOVW_BREL:
7997     case elfcpp::R_ARM_THM_MOVW_BREL_NC:
7998     case elfcpp::R_ARM_THM_MOVT_BREL:
7999     case elfcpp::R_ARM_THM_MOVW_BREL:
8000       // Relative addressing relocations.
8001       {
8002         // Make a dynamic relocation if necessary.
8003         int flags = Symbol::NON_PIC_REF;
8004         if (gsym->needs_dynamic_reloc(flags))
8005           {
8006             if (target->may_need_copy_reloc(gsym))
8007               {
8008                 target->copy_reloc(symtab, layout, object,
8009                                    data_shndx, output_section, gsym, reloc);
8010               }
8011             else
8012               {
8013                 check_non_pic(object, r_type);
8014                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8015                 rel_dyn->add_global(gsym, r_type, output_section, object,
8016                                     data_shndx, reloc.get_r_offset());
8017               }
8018           }
8019       }
8020       break;
8021
8022     case elfcpp::R_ARM_PC24:
8023     case elfcpp::R_ARM_THM_CALL:
8024     case elfcpp::R_ARM_PLT32:
8025     case elfcpp::R_ARM_CALL:
8026     case elfcpp::R_ARM_JUMP24:
8027     case elfcpp::R_ARM_THM_JUMP24:
8028     case elfcpp::R_ARM_SBREL31:
8029     case elfcpp::R_ARM_PREL31:
8030     case elfcpp::R_ARM_THM_JUMP19:
8031     case elfcpp::R_ARM_THM_JUMP6:
8032     case elfcpp::R_ARM_THM_JUMP11:
8033     case elfcpp::R_ARM_THM_JUMP8:
8034       // All the relocation above are branches except for the PREL31 ones.
8035       // A PREL31 relocation can point to a personality function in a shared
8036       // library.  In that case we want to use a PLT because we want to
8037       // call the personality routine and the dyanmic linkers we care about
8038       // do not support dynamic PREL31 relocations. An REL31 relocation may
8039       // point to a function whose unwinding behaviour is being described but
8040       // we will not mistakenly generate a PLT for that because we should use
8041       // a local section symbol.
8042
8043       // If the symbol is fully resolved, this is just a relative
8044       // local reloc.  Otherwise we need a PLT entry.
8045       if (gsym->final_value_is_known())
8046         break;
8047       // If building a shared library, we can also skip the PLT entry
8048       // if the symbol is defined in the output file and is protected
8049       // or hidden.
8050       if (gsym->is_defined()
8051           && !gsym->is_from_dynobj()
8052           && !gsym->is_preemptible())
8053         break;
8054       target->make_plt_entry(symtab, layout, gsym);
8055       break;
8056
8057     case elfcpp::R_ARM_GOT_BREL:
8058     case elfcpp::R_ARM_GOT_ABS:
8059     case elfcpp::R_ARM_GOT_PREL:
8060       {
8061         // The symbol requires a GOT entry.
8062         Arm_output_data_got<big_endian>* got =
8063           target->got_section(symtab, layout);
8064         if (gsym->final_value_is_known())
8065           got->add_global(gsym, GOT_TYPE_STANDARD);
8066         else
8067           {
8068             // If this symbol is not fully resolved, we need to add a
8069             // GOT entry with a dynamic relocation.
8070             Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8071             if (gsym->is_from_dynobj()
8072                 || gsym->is_undefined()
8073                 || gsym->is_preemptible())
8074               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
8075                                        rel_dyn, elfcpp::R_ARM_GLOB_DAT);
8076             else
8077               {
8078                 if (got->add_global(gsym, GOT_TYPE_STANDARD))
8079                   rel_dyn->add_global_relative(
8080                       gsym, elfcpp::R_ARM_RELATIVE, got,
8081                       gsym->got_offset(GOT_TYPE_STANDARD));
8082               }
8083           }
8084       }
8085       break;
8086
8087     case elfcpp::R_ARM_TARGET1:
8088     case elfcpp::R_ARM_TARGET2:
8089       // These should have been mapped to other types already.
8090       // Fall through.
8091     case elfcpp::R_ARM_COPY:
8092     case elfcpp::R_ARM_GLOB_DAT:
8093     case elfcpp::R_ARM_JUMP_SLOT:
8094     case elfcpp::R_ARM_RELATIVE:
8095       // These are relocations which should only be seen by the
8096       // dynamic linker, and should never be seen here.
8097       gold_error(_("%s: unexpected reloc %u in object file"),
8098                  object->name().c_str(), r_type);
8099       break;
8100
8101       // These are initial tls relocs, which are expected when
8102       // linking.
8103     case elfcpp::R_ARM_TLS_GD32:        // Global-dynamic
8104     case elfcpp::R_ARM_TLS_LDM32:       // Local-dynamic
8105     case elfcpp::R_ARM_TLS_LDO32:       // Alternate local-dynamic
8106     case elfcpp::R_ARM_TLS_IE32:        // Initial-exec
8107     case elfcpp::R_ARM_TLS_LE32:        // Local-exec
8108       {
8109         const bool is_final = gsym->final_value_is_known();
8110         const tls::Tls_optimization optimized_type
8111             = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
8112         switch (r_type)
8113           {
8114           case elfcpp::R_ARM_TLS_GD32:          // Global-dynamic
8115             if (optimized_type == tls::TLSOPT_NONE)
8116               {
8117                 // Create a pair of GOT entries for the module index and
8118                 // dtv-relative offset.
8119                 Arm_output_data_got<big_endian>* got
8120                     = target->got_section(symtab, layout);
8121                 if (!parameters->doing_static_link())
8122                   got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
8123                                                 target->rel_dyn_section(layout),
8124                                                 elfcpp::R_ARM_TLS_DTPMOD32,
8125                                                 elfcpp::R_ARM_TLS_DTPOFF32);
8126                 else
8127                   got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym);
8128               }
8129             else
8130               // FIXME: TLS optimization not supported yet.
8131               gold_unreachable();
8132             break;
8133
8134           case elfcpp::R_ARM_TLS_LDM32:         // Local-dynamic
8135             if (optimized_type == tls::TLSOPT_NONE)
8136               {
8137                 // Create a GOT entry for the module index.
8138                 target->got_mod_index_entry(symtab, layout, object);
8139               }
8140             else
8141               // FIXME: TLS optimization not supported yet.
8142               gold_unreachable();
8143             break;
8144
8145           case elfcpp::R_ARM_TLS_LDO32:         // Alternate local-dynamic
8146             break;
8147
8148           case elfcpp::R_ARM_TLS_IE32:          // Initial-exec
8149             layout->set_has_static_tls();
8150             if (optimized_type == tls::TLSOPT_NONE)
8151               {
8152                 // Create a GOT entry for the tp-relative offset.
8153                 Arm_output_data_got<big_endian>* got
8154                   = target->got_section(symtab, layout);
8155                 if (!parameters->doing_static_link())
8156                   got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
8157                                            target->rel_dyn_section(layout),
8158                                            elfcpp::R_ARM_TLS_TPOFF32);
8159                 else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
8160                   {
8161                     got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
8162                     unsigned int got_offset =
8163                        gsym->got_offset(GOT_TYPE_TLS_OFFSET);
8164                     got->add_static_reloc(got_offset,
8165                                           elfcpp::R_ARM_TLS_TPOFF32, gsym);
8166                   }
8167               }
8168             else
8169               // FIXME: TLS optimization not supported yet.
8170               gold_unreachable();
8171             break;
8172
8173           case elfcpp::R_ARM_TLS_LE32:  // Local-exec
8174             layout->set_has_static_tls();
8175             if (parameters->options().shared())
8176               {
8177                 // We need to create a dynamic relocation.
8178                 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8179                 rel_dyn->add_global(gsym, elfcpp::R_ARM_TLS_TPOFF32,
8180                                     output_section, object,
8181                                     data_shndx, reloc.get_r_offset());
8182               }
8183             break;
8184
8185           default:
8186             gold_unreachable();
8187           }
8188       }
8189       break;
8190
8191     default:
8192       unsupported_reloc_global(object, r_type, gsym);
8193       break;
8194     }
8195 }
8196
8197 // Process relocations for gc.
8198
8199 template<bool big_endian>
8200 void
8201 Target_arm<big_endian>::gc_process_relocs(Symbol_table* symtab,
8202                                           Layout* layout,
8203                                           Sized_relobj<32, big_endian>* object,
8204                                           unsigned int data_shndx,
8205                                           unsigned int,
8206                                           const unsigned char* prelocs,
8207                                           size_t reloc_count,
8208                                           Output_section* output_section,
8209                                           bool needs_special_offset_handling,
8210                                           size_t local_symbol_count,
8211                                           const unsigned char* plocal_symbols)
8212 {
8213   typedef Target_arm<big_endian> Arm;
8214   typedef typename Target_arm<big_endian>::Scan Scan;
8215
8216   gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan,
8217                           Target_arm::Relocatable_size_for_reloc>(
8218     symtab,
8219     layout,
8220     this,
8221     object,
8222     data_shndx,
8223     prelocs,
8224     reloc_count,
8225     output_section,
8226     needs_special_offset_handling,
8227     local_symbol_count,
8228     plocal_symbols);
8229 }
8230
8231 // Scan relocations for a section.
8232
8233 template<bool big_endian>
8234 void
8235 Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
8236                                     Layout* layout,
8237                                     Sized_relobj<32, big_endian>* object,
8238                                     unsigned int data_shndx,
8239                                     unsigned int sh_type,
8240                                     const unsigned char* prelocs,
8241                                     size_t reloc_count,
8242                                     Output_section* output_section,
8243                                     bool needs_special_offset_handling,
8244                                     size_t local_symbol_count,
8245                                     const unsigned char* plocal_symbols)
8246 {
8247   typedef typename Target_arm<big_endian>::Scan Scan;
8248   if (sh_type == elfcpp::SHT_RELA)
8249     {
8250       gold_error(_("%s: unsupported RELA reloc section"),
8251                  object->name().c_str());
8252       return;
8253     }
8254
8255   gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
8256     symtab,
8257     layout,
8258     this,
8259     object,
8260     data_shndx,
8261     prelocs,
8262     reloc_count,
8263     output_section,
8264     needs_special_offset_handling,
8265     local_symbol_count,
8266     plocal_symbols);
8267 }
8268
8269 // Finalize the sections.
8270
8271 template<bool big_endian>
8272 void
8273 Target_arm<big_endian>::do_finalize_sections(
8274     Layout* layout,
8275     const Input_objects* input_objects,
8276     Symbol_table* symtab)
8277 {
8278   bool merged_any_attributes = false;
8279   // Merge processor-specific flags.
8280   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
8281        p != input_objects->relobj_end();
8282        ++p)
8283     {
8284       Arm_relobj<big_endian>* arm_relobj =
8285         Arm_relobj<big_endian>::as_arm_relobj(*p);
8286       if (arm_relobj->merge_flags_and_attributes())
8287         {
8288           this->merge_processor_specific_flags(
8289               arm_relobj->name(),
8290               arm_relobj->processor_specific_flags());
8291           this->merge_object_attributes(arm_relobj->name().c_str(),
8292                                         arm_relobj->attributes_section_data());
8293           merged_any_attributes = true;
8294         }
8295     } 
8296
8297   for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
8298        p != input_objects->dynobj_end();
8299        ++p)
8300     {
8301       Arm_dynobj<big_endian>* arm_dynobj =
8302         Arm_dynobj<big_endian>::as_arm_dynobj(*p);
8303       this->merge_processor_specific_flags(
8304           arm_dynobj->name(),
8305           arm_dynobj->processor_specific_flags());
8306       this->merge_object_attributes(arm_dynobj->name().c_str(),
8307                                     arm_dynobj->attributes_section_data());
8308       merged_any_attributes = true;
8309     }
8310
8311   // Create an empty uninitialized attribute section if we still don't have it
8312   // at this moment.  This happens if there is no attributes sections in all
8313   // inputs.
8314   if (this->attributes_section_data_ == NULL)
8315     this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
8316
8317   // Check BLX use.
8318   const Object_attribute* cpu_arch_attr =
8319     this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
8320   if (cpu_arch_attr->int_value() > elfcpp::TAG_CPU_ARCH_V4)
8321     this->set_may_use_blx(true);
8322  
8323   // Check if we need to use Cortex-A8 workaround.
8324   if (parameters->options().user_set_fix_cortex_a8())
8325     this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
8326   else
8327     {
8328       // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
8329       // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
8330       // profile.  
8331       const Object_attribute* cpu_arch_profile_attr =
8332         this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
8333       this->fix_cortex_a8_ =
8334         (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
8335          && (cpu_arch_profile_attr->int_value() == 'A'
8336              || cpu_arch_profile_attr->int_value() == 0));
8337     }
8338   
8339   // Check if we can use V4BX interworking.
8340   // The V4BX interworking stub contains BX instruction,
8341   // which is not specified for some profiles.
8342   if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
8343       && !this->may_use_blx())
8344     gold_error(_("unable to provide V4BX reloc interworking fix up; "
8345                  "the target profile does not support BX instruction"));
8346
8347   // Fill in some more dynamic tags.
8348   const Reloc_section* rel_plt = (this->plt_ == NULL
8349                                   ? NULL
8350                                   : this->plt_->rel_plt());
8351   layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
8352                                   this->rel_dyn_, true, false);
8353
8354   // Emit any relocs we saved in an attempt to avoid generating COPY
8355   // relocs.
8356   if (this->copy_relocs_.any_saved_relocs())
8357     this->copy_relocs_.emit(this->rel_dyn_section(layout));
8358
8359   // Handle the .ARM.exidx section.
8360   Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
8361   if (exidx_section != NULL
8362       && exidx_section->type() == elfcpp::SHT_ARM_EXIDX
8363       && !parameters->options().relocatable())
8364     {
8365       // Create __exidx_start and __exdix_end symbols.
8366       symtab->define_in_output_data("__exidx_start", NULL,
8367                                     Symbol_table::PREDEFINED,
8368                                     exidx_section, 0, 0, elfcpp::STT_OBJECT,
8369                                     elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8370                                     false, true);
8371       symtab->define_in_output_data("__exidx_end", NULL,
8372                                     Symbol_table::PREDEFINED,
8373                                     exidx_section, 0, 0, elfcpp::STT_OBJECT,
8374                                     elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
8375                                     true, true);
8376
8377       // For the ARM target, we need to add a PT_ARM_EXIDX segment for
8378       // the .ARM.exidx section.
8379       if (!layout->script_options()->saw_phdrs_clause())
8380         {
8381           gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
8382                       == NULL);
8383           Output_segment*  exidx_segment =
8384             layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
8385           exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
8386                                             false);
8387         }
8388     }
8389
8390   // Create an .ARM.attributes section if we have merged any attributes
8391   // from inputs.
8392   if (merged_any_attributes)
8393     {
8394       Output_attributes_section_data* attributes_section =
8395       new Output_attributes_section_data(*this->attributes_section_data_);
8396       layout->add_output_section_data(".ARM.attributes",
8397                                       elfcpp::SHT_ARM_ATTRIBUTES, 0,
8398                                       attributes_section, false, false, false,
8399                                       false);
8400     }
8401
8402   // Fix up links in section EXIDX headers.
8403   for (Layout::Section_list::const_iterator p = layout->section_list().begin();
8404        p != layout->section_list().end();
8405        ++p)
8406     if ((*p)->type() == elfcpp::SHT_ARM_EXIDX)
8407       {
8408         Arm_output_section<big_endian>* os =
8409           Arm_output_section<big_endian>::as_arm_output_section(*p);
8410         os->set_exidx_section_link();
8411       }
8412 }
8413
8414 // Return whether a direct absolute static relocation needs to be applied.
8415 // In cases where Scan::local() or Scan::global() has created
8416 // a dynamic relocation other than R_ARM_RELATIVE, the addend
8417 // of the relocation is carried in the data, and we must not
8418 // apply the static relocation.
8419
8420 template<bool big_endian>
8421 inline bool
8422 Target_arm<big_endian>::Relocate::should_apply_static_reloc(
8423     const Sized_symbol<32>* gsym,
8424     int ref_flags,
8425     bool is_32bit,
8426     Output_section* output_section)
8427 {
8428   // If the output section is not allocated, then we didn't call
8429   // scan_relocs, we didn't create a dynamic reloc, and we must apply
8430   // the reloc here.
8431   if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
8432       return true;
8433
8434   // For local symbols, we will have created a non-RELATIVE dynamic
8435   // relocation only if (a) the output is position independent,
8436   // (b) the relocation is absolute (not pc- or segment-relative), and
8437   // (c) the relocation is not 32 bits wide.
8438   if (gsym == NULL)
8439     return !(parameters->options().output_is_position_independent()
8440              && (ref_flags & Symbol::ABSOLUTE_REF)
8441              && !is_32bit);
8442
8443   // For global symbols, we use the same helper routines used in the
8444   // scan pass.  If we did not create a dynamic relocation, or if we
8445   // created a RELATIVE dynamic relocation, we should apply the static
8446   // relocation.
8447   bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
8448   bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
8449                  && gsym->can_use_relative_reloc(ref_flags
8450                                                  & Symbol::FUNCTION_CALL);
8451   return !has_dyn || is_rel;
8452 }
8453
8454 // Perform a relocation.
8455
8456 template<bool big_endian>
8457 inline bool
8458 Target_arm<big_endian>::Relocate::relocate(
8459     const Relocate_info<32, big_endian>* relinfo,
8460     Target_arm* target,
8461     Output_section *output_section,
8462     size_t relnum,
8463     const elfcpp::Rel<32, big_endian>& rel,
8464     unsigned int r_type,
8465     const Sized_symbol<32>* gsym,
8466     const Symbol_value<32>* psymval,
8467     unsigned char* view,
8468     Arm_address address,
8469     section_size_type view_size)
8470 {
8471   typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
8472
8473   r_type = get_real_reloc_type(r_type);
8474   const Arm_reloc_property* reloc_property =
8475     arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
8476   if (reloc_property == NULL)
8477     {
8478       std::string reloc_name =
8479         arm_reloc_property_table->reloc_name_in_error_message(r_type);
8480       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8481                              _("cannot relocate %s in object file"),
8482                              reloc_name.c_str());
8483       return true;
8484     }
8485
8486   const Arm_relobj<big_endian>* object =
8487     Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
8488
8489   // If the final branch target of a relocation is THUMB instruction, this
8490   // is 1.  Otherwise it is 0.
8491   Arm_address thumb_bit = 0;
8492   Symbol_value<32> symval;
8493   bool is_weakly_undefined_without_plt = false;
8494   if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
8495     {
8496       if (gsym != NULL)
8497         {
8498           // This is a global symbol.  Determine if we use PLT and if the
8499           // final target is THUMB.
8500           if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
8501             {
8502               // This uses a PLT, change the symbol value.
8503               symval.set_output_value(target->plt_section()->address()
8504                                       + gsym->plt_offset());
8505               psymval = &symval;
8506             }
8507           else if (gsym->is_weak_undefined())
8508             {
8509               // This is a weakly undefined symbol and we do not use PLT
8510               // for this relocation.  A branch targeting this symbol will
8511               // be converted into an NOP.
8512               is_weakly_undefined_without_plt = true;
8513             }
8514           else if (gsym->is_undefined() && reloc_property->uses_symbol())
8515             {
8516               // This relocation uses the symbol value but the symbol is
8517               // undefined.  Exit early and have the caller reporting an
8518               // error.
8519               return true;
8520             }
8521           else
8522             {
8523               // Set thumb bit if symbol:
8524               // -Has type STT_ARM_TFUNC or
8525               // -Has type STT_FUNC, is defined and with LSB in value set.
8526               thumb_bit =
8527                 (((gsym->type() == elfcpp::STT_ARM_TFUNC)
8528                  || (gsym->type() == elfcpp::STT_FUNC
8529                      && !gsym->is_undefined()
8530                      && ((psymval->value(object, 0) & 1) != 0)))
8531                 ? 1
8532                 : 0);
8533             }
8534         }
8535       else
8536         {
8537           // This is a local symbol.  Determine if the final target is THUMB.
8538           // We saved this information when all the local symbols were read.
8539           elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
8540           unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
8541           thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
8542         }
8543     }
8544   else
8545     {
8546       // This is a fake relocation synthesized for a stub.  It does not have
8547       // a real symbol.  We just look at the LSB of the symbol value to
8548       // determine if the target is THUMB or not.
8549       thumb_bit = ((psymval->value(object, 0) & 1) != 0);
8550     }
8551
8552   // Strip LSB if this points to a THUMB target.
8553   if (thumb_bit != 0
8554       && reloc_property->uses_thumb_bit() 
8555       && ((psymval->value(object, 0) & 1) != 0))
8556     {
8557       Arm_address stripped_value =
8558         psymval->value(object, 0) & ~static_cast<Arm_address>(1);
8559       symval.set_output_value(stripped_value);
8560       psymval = &symval;
8561     } 
8562
8563   // Get the GOT offset if needed.
8564   // The GOT pointer points to the end of the GOT section.
8565   // We need to subtract the size of the GOT section to get
8566   // the actual offset to use in the relocation.
8567   bool have_got_offset = false;
8568   unsigned int got_offset = 0;
8569   switch (r_type)
8570     {
8571     case elfcpp::R_ARM_GOT_BREL:
8572     case elfcpp::R_ARM_GOT_PREL:
8573       if (gsym != NULL)
8574         {
8575           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8576           got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
8577                         - target->got_size());
8578         }
8579       else
8580         {
8581           unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8582           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
8583           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
8584                         - target->got_size());
8585         }
8586       have_got_offset = true;
8587       break;
8588
8589     default:
8590       break;
8591     }
8592
8593   // To look up relocation stubs, we need to pass the symbol table index of
8594   // a local symbol.
8595   unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
8596
8597   // Get the addressing origin of the output segment defining the
8598   // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
8599   Arm_address sym_origin = 0;
8600   if (reloc_property->uses_symbol_base())
8601     {
8602       if (r_type == elfcpp::R_ARM_BASE_ABS && gsym == NULL)
8603         // R_ARM_BASE_ABS with the NULL symbol will give the
8604         // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
8605         // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
8606         sym_origin = target->got_plt_section()->address();
8607       else if (gsym == NULL)
8608         sym_origin = 0;
8609       else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
8610         sym_origin = gsym->output_segment()->vaddr();
8611       else if (gsym->source() == Symbol::IN_OUTPUT_DATA)
8612         sym_origin = gsym->output_data()->address();
8613
8614       // TODO: Assumes the segment base to be zero for the global symbols
8615       // till the proper support for the segment-base-relative addressing
8616       // will be implemented.  This is consistent with GNU ld.
8617     }
8618
8619   // For relative addressing relocation, find out the relative address base.
8620   Arm_address relative_address_base = 0;
8621   switch(reloc_property->relative_address_base())
8622     {
8623     case Arm_reloc_property::RAB_NONE:
8624     // Relocations with relative address bases RAB_TLS and RAB_tp are
8625     // handled by relocate_tls.  So we do not need to do anything here.
8626     case Arm_reloc_property::RAB_TLS:
8627     case Arm_reloc_property::RAB_tp:
8628       break;
8629     case Arm_reloc_property::RAB_B_S:
8630       relative_address_base = sym_origin;
8631       break;
8632     case Arm_reloc_property::RAB_GOT_ORG:
8633       relative_address_base = target->got_plt_section()->address();
8634       break;
8635     case Arm_reloc_property::RAB_P:
8636       relative_address_base = address;
8637       break;
8638     case Arm_reloc_property::RAB_Pa:
8639       relative_address_base = address & 0xfffffffcU;
8640       break;
8641     default:
8642       gold_unreachable(); 
8643     }
8644     
8645   typename Arm_relocate_functions::Status reloc_status =
8646         Arm_relocate_functions::STATUS_OKAY;
8647   bool check_overflow = reloc_property->checks_overflow();
8648   switch (r_type)
8649     {
8650     case elfcpp::R_ARM_NONE:
8651       break;
8652
8653     case elfcpp::R_ARM_ABS8:
8654       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8655                                     output_section))
8656         reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
8657       break;
8658
8659     case elfcpp::R_ARM_ABS12:
8660       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8661                                     output_section))
8662         reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
8663       break;
8664
8665     case elfcpp::R_ARM_ABS16:
8666       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8667                                     output_section))
8668         reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
8669       break;
8670
8671     case elfcpp::R_ARM_ABS32:
8672       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
8673                                     output_section))
8674         reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8675                                                      thumb_bit);
8676       break;
8677
8678     case elfcpp::R_ARM_ABS32_NOI:
8679       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
8680                                     output_section))
8681         // No thumb bit for this relocation: (S + A)
8682         reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
8683                                                      0);
8684       break;
8685
8686     case elfcpp::R_ARM_MOVW_ABS_NC:
8687       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8688                                     output_section))
8689         reloc_status = Arm_relocate_functions::movw(view, object, psymval,
8690                                                     0, thumb_bit,
8691                                                     check_overflow);
8692       break;
8693
8694     case elfcpp::R_ARM_MOVT_ABS:
8695       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8696                                     output_section))
8697         reloc_status = Arm_relocate_functions::movt(view, object, psymval, 0);
8698       break;
8699
8700     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
8701       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8702                                     output_section))
8703         reloc_status = Arm_relocate_functions::thm_movw(view, object, psymval,
8704                                                         0, thumb_bit, false);
8705       break;
8706
8707     case elfcpp::R_ARM_THM_MOVT_ABS:
8708       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8709                                     output_section))
8710         reloc_status = Arm_relocate_functions::thm_movt(view, object,
8711                                                         psymval, 0);
8712       break;
8713
8714     case elfcpp::R_ARM_MOVW_PREL_NC:
8715     case elfcpp::R_ARM_MOVW_BREL_NC:
8716     case elfcpp::R_ARM_MOVW_BREL:
8717       reloc_status =
8718         Arm_relocate_functions::movw(view, object, psymval,
8719                                      relative_address_base, thumb_bit,
8720                                      check_overflow);
8721       break;
8722
8723     case elfcpp::R_ARM_MOVT_PREL:
8724     case elfcpp::R_ARM_MOVT_BREL:
8725       reloc_status =
8726         Arm_relocate_functions::movt(view, object, psymval,
8727                                      relative_address_base);
8728       break;
8729
8730     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
8731     case elfcpp::R_ARM_THM_MOVW_BREL_NC:
8732     case elfcpp::R_ARM_THM_MOVW_BREL:
8733       reloc_status =
8734         Arm_relocate_functions::thm_movw(view, object, psymval,
8735                                          relative_address_base,
8736                                          thumb_bit, check_overflow);
8737       break;
8738
8739     case elfcpp::R_ARM_THM_MOVT_PREL:
8740     case elfcpp::R_ARM_THM_MOVT_BREL:
8741       reloc_status =
8742         Arm_relocate_functions::thm_movt(view, object, psymval,
8743                                          relative_address_base);
8744       break;
8745         
8746     case elfcpp::R_ARM_REL32:
8747       reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
8748                                                    address, thumb_bit);
8749       break;
8750
8751     case elfcpp::R_ARM_THM_ABS5:
8752       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8753                                     output_section))
8754         reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
8755       break;
8756
8757     // Thumb long branches.
8758     case elfcpp::R_ARM_THM_CALL:
8759     case elfcpp::R_ARM_THM_XPC22:
8760     case elfcpp::R_ARM_THM_JUMP24:
8761       reloc_status =
8762         Arm_relocate_functions::thumb_branch_common(
8763             r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8764             thumb_bit, is_weakly_undefined_without_plt);
8765       break;
8766
8767     case elfcpp::R_ARM_GOTOFF32:
8768       {
8769         Arm_address got_origin;
8770         got_origin = target->got_plt_section()->address();
8771         reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
8772                                                      got_origin, thumb_bit);
8773       }
8774       break;
8775
8776     case elfcpp::R_ARM_BASE_PREL:
8777       gold_assert(gsym != NULL);
8778       reloc_status =
8779           Arm_relocate_functions::base_prel(view, sym_origin, address);
8780       break;
8781
8782     case elfcpp::R_ARM_BASE_ABS:
8783       {
8784         if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
8785                                       output_section))
8786           break;
8787
8788         reloc_status = Arm_relocate_functions::base_abs(view, sym_origin);
8789       }
8790       break;
8791
8792     case elfcpp::R_ARM_GOT_BREL:
8793       gold_assert(have_got_offset);
8794       reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
8795       break;
8796
8797     case elfcpp::R_ARM_GOT_PREL:
8798       gold_assert(have_got_offset);
8799       // Get the address origin for GOT PLT, which is allocated right
8800       // after the GOT section, to calculate an absolute address of
8801       // the symbol GOT entry (got_origin + got_offset).
8802       Arm_address got_origin;
8803       got_origin = target->got_plt_section()->address();
8804       reloc_status = Arm_relocate_functions::got_prel(view,
8805                                                       got_origin + got_offset,
8806                                                       address);
8807       break;
8808
8809     case elfcpp::R_ARM_PLT32:
8810     case elfcpp::R_ARM_CALL:
8811     case elfcpp::R_ARM_JUMP24:
8812     case elfcpp::R_ARM_XPC25:
8813       gold_assert(gsym == NULL
8814                   || gsym->has_plt_offset()
8815                   || gsym->final_value_is_known()
8816                   || (gsym->is_defined()
8817                       && !gsym->is_from_dynobj()
8818                       && !gsym->is_preemptible()));
8819       reloc_status =
8820         Arm_relocate_functions::arm_branch_common(
8821             r_type, relinfo, view, gsym, object, r_sym, psymval, address,
8822             thumb_bit, is_weakly_undefined_without_plt);
8823       break;
8824
8825     case elfcpp::R_ARM_THM_JUMP19:
8826       reloc_status =
8827         Arm_relocate_functions::thm_jump19(view, object, psymval, address,
8828                                            thumb_bit);
8829       break;
8830
8831     case elfcpp::R_ARM_THM_JUMP6:
8832       reloc_status =
8833         Arm_relocate_functions::thm_jump6(view, object, psymval, address);
8834       break;
8835
8836     case elfcpp::R_ARM_THM_JUMP8:
8837       reloc_status =
8838         Arm_relocate_functions::thm_jump8(view, object, psymval, address);
8839       break;
8840
8841     case elfcpp::R_ARM_THM_JUMP11:
8842       reloc_status =
8843         Arm_relocate_functions::thm_jump11(view, object, psymval, address);
8844       break;
8845
8846     case elfcpp::R_ARM_PREL31:
8847       reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
8848                                                     address, thumb_bit);
8849       break;
8850
8851     case elfcpp::R_ARM_V4BX:
8852       if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
8853         {
8854           const bool is_v4bx_interworking =
8855               (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
8856           reloc_status =
8857             Arm_relocate_functions::v4bx(relinfo, view, object, address,
8858                                          is_v4bx_interworking);
8859         }
8860       break;
8861
8862     case elfcpp::R_ARM_THM_PC8:
8863       reloc_status =
8864         Arm_relocate_functions::thm_pc8(view, object, psymval, address);
8865       break;
8866
8867     case elfcpp::R_ARM_THM_PC12:
8868       reloc_status =
8869         Arm_relocate_functions::thm_pc12(view, object, psymval, address);
8870       break;
8871
8872     case elfcpp::R_ARM_THM_ALU_PREL_11_0:
8873       reloc_status =
8874         Arm_relocate_functions::thm_alu11(view, object, psymval, address,
8875                                           thumb_bit);
8876       break;
8877
8878     case elfcpp::R_ARM_ALU_PC_G0_NC:
8879     case elfcpp::R_ARM_ALU_PC_G0:
8880     case elfcpp::R_ARM_ALU_PC_G1_NC:
8881     case elfcpp::R_ARM_ALU_PC_G1:
8882     case elfcpp::R_ARM_ALU_PC_G2:
8883     case elfcpp::R_ARM_ALU_SB_G0_NC:
8884     case elfcpp::R_ARM_ALU_SB_G0:
8885     case elfcpp::R_ARM_ALU_SB_G1_NC:
8886     case elfcpp::R_ARM_ALU_SB_G1:
8887     case elfcpp::R_ARM_ALU_SB_G2:
8888       reloc_status =
8889         Arm_relocate_functions::arm_grp_alu(view, object, psymval,
8890                                             reloc_property->group_index(),
8891                                             relative_address_base,
8892                                             thumb_bit, check_overflow);
8893       break;
8894
8895     case elfcpp::R_ARM_LDR_PC_G0:
8896     case elfcpp::R_ARM_LDR_PC_G1:
8897     case elfcpp::R_ARM_LDR_PC_G2:
8898     case elfcpp::R_ARM_LDR_SB_G0:
8899     case elfcpp::R_ARM_LDR_SB_G1:
8900     case elfcpp::R_ARM_LDR_SB_G2:
8901       reloc_status =
8902           Arm_relocate_functions::arm_grp_ldr(view, object, psymval,
8903                                               reloc_property->group_index(),
8904                                               relative_address_base);
8905       break;
8906
8907     case elfcpp::R_ARM_LDRS_PC_G0:
8908     case elfcpp::R_ARM_LDRS_PC_G1:
8909     case elfcpp::R_ARM_LDRS_PC_G2:
8910     case elfcpp::R_ARM_LDRS_SB_G0:
8911     case elfcpp::R_ARM_LDRS_SB_G1:
8912     case elfcpp::R_ARM_LDRS_SB_G2:
8913       reloc_status =
8914           Arm_relocate_functions::arm_grp_ldrs(view, object, psymval,
8915                                                reloc_property->group_index(),
8916                                                relative_address_base);
8917       break;
8918
8919     case elfcpp::R_ARM_LDC_PC_G0:
8920     case elfcpp::R_ARM_LDC_PC_G1:
8921     case elfcpp::R_ARM_LDC_PC_G2:
8922     case elfcpp::R_ARM_LDC_SB_G0:
8923     case elfcpp::R_ARM_LDC_SB_G1:
8924     case elfcpp::R_ARM_LDC_SB_G2:
8925       reloc_status =
8926           Arm_relocate_functions::arm_grp_ldc(view, object, psymval,
8927                                               reloc_property->group_index(),
8928                                               relative_address_base);
8929       break;
8930
8931       // These are initial tls relocs, which are expected when
8932       // linking.
8933     case elfcpp::R_ARM_TLS_GD32:        // Global-dynamic
8934     case elfcpp::R_ARM_TLS_LDM32:       // Local-dynamic
8935     case elfcpp::R_ARM_TLS_LDO32:       // Alternate local-dynamic
8936     case elfcpp::R_ARM_TLS_IE32:        // Initial-exec
8937     case elfcpp::R_ARM_TLS_LE32:        // Local-exec
8938       reloc_status =
8939         this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
8940                            view, address, view_size);
8941       break;
8942
8943     default:
8944       gold_unreachable();
8945     }
8946
8947   // Report any errors.
8948   switch (reloc_status)
8949     {
8950     case Arm_relocate_functions::STATUS_OKAY:
8951       break;
8952     case Arm_relocate_functions::STATUS_OVERFLOW:
8953       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
8954                              _("relocation overflow in %s"),
8955                              reloc_property->name().c_str());
8956       break;
8957     case Arm_relocate_functions::STATUS_BAD_RELOC:
8958       gold_error_at_location(
8959         relinfo,
8960         relnum,
8961         rel.get_r_offset(),
8962         _("unexpected opcode while processing relocation %s"),
8963         reloc_property->name().c_str());
8964       break;
8965     default:
8966       gold_unreachable();
8967     }
8968
8969   return true;
8970 }
8971
8972 // Perform a TLS relocation.
8973
8974 template<bool big_endian>
8975 inline typename Arm_relocate_functions<big_endian>::Status
8976 Target_arm<big_endian>::Relocate::relocate_tls(
8977     const Relocate_info<32, big_endian>* relinfo,
8978     Target_arm<big_endian>* target,
8979     size_t relnum,
8980     const elfcpp::Rel<32, big_endian>& rel,
8981     unsigned int r_type,
8982     const Sized_symbol<32>* gsym,
8983     const Symbol_value<32>* psymval,
8984     unsigned char* view,
8985     elfcpp::Elf_types<32>::Elf_Addr address,
8986     section_size_type /*view_size*/ )
8987 {
8988   typedef Arm_relocate_functions<big_endian> ArmRelocFuncs;
8989   typedef Relocate_functions<32, big_endian> RelocFuncs;
8990   Output_segment* tls_segment = relinfo->layout->tls_segment();
8991
8992   const Sized_relobj<32, big_endian>* object = relinfo->object;
8993
8994   elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
8995
8996   const bool is_final = (gsym == NULL
8997                          ? !parameters->options().shared()
8998                          : gsym->final_value_is_known());
8999   const tls::Tls_optimization optimized_type
9000       = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
9001   switch (r_type)
9002     {
9003     case elfcpp::R_ARM_TLS_GD32:        // Global-dynamic
9004         {
9005           unsigned int got_type = GOT_TYPE_TLS_PAIR;
9006           unsigned int got_offset;
9007           if (gsym != NULL)
9008             {
9009               gold_assert(gsym->has_got_offset(got_type));
9010               got_offset = gsym->got_offset(got_type) - target->got_size();
9011             }
9012           else
9013             {
9014               unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9015               gold_assert(object->local_has_got_offset(r_sym, got_type));
9016               got_offset = (object->local_got_offset(r_sym, got_type)
9017                             - target->got_size());
9018             }
9019           if (optimized_type == tls::TLSOPT_NONE)
9020             {
9021               Arm_address got_entry =
9022                 target->got_plt_section()->address() + got_offset;
9023               
9024               // Relocate the field with the PC relative offset of the pair of
9025               // GOT entries.
9026               RelocFuncs::pcrel32(view, got_entry, address);
9027               return ArmRelocFuncs::STATUS_OKAY;
9028             }
9029         }
9030       break;
9031
9032     case elfcpp::R_ARM_TLS_LDM32:       // Local-dynamic
9033       if (optimized_type == tls::TLSOPT_NONE)
9034         {
9035           // Relocate the field with the offset of the GOT entry for
9036           // the module index.
9037           unsigned int got_offset;
9038           got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
9039                         - target->got_size());
9040           Arm_address got_entry =
9041             target->got_plt_section()->address() + got_offset;
9042
9043           // Relocate the field with the PC relative offset of the pair of
9044           // GOT entries.
9045           RelocFuncs::pcrel32(view, got_entry, address);
9046           return ArmRelocFuncs::STATUS_OKAY;
9047         }
9048       break;
9049
9050     case elfcpp::R_ARM_TLS_LDO32:       // Alternate local-dynamic
9051       RelocFuncs::rel32(view, value);
9052       return ArmRelocFuncs::STATUS_OKAY;
9053
9054     case elfcpp::R_ARM_TLS_IE32:        // Initial-exec
9055       if (optimized_type == tls::TLSOPT_NONE)
9056         {
9057           // Relocate the field with the offset of the GOT entry for
9058           // the tp-relative offset of the symbol.
9059           unsigned int got_type = GOT_TYPE_TLS_OFFSET;
9060           unsigned int got_offset;
9061           if (gsym != NULL)
9062             {
9063               gold_assert(gsym->has_got_offset(got_type));
9064               got_offset = gsym->got_offset(got_type);
9065             }
9066           else
9067             {
9068               unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9069               gold_assert(object->local_has_got_offset(r_sym, got_type));
9070               got_offset = object->local_got_offset(r_sym, got_type);
9071             }
9072
9073           // All GOT offsets are relative to the end of the GOT.
9074           got_offset -= target->got_size();
9075
9076           Arm_address got_entry =
9077             target->got_plt_section()->address() + got_offset;
9078
9079           // Relocate the field with the PC relative offset of the GOT entry.
9080           RelocFuncs::pcrel32(view, got_entry, address);
9081           return ArmRelocFuncs::STATUS_OKAY;
9082         }
9083       break;
9084
9085     case elfcpp::R_ARM_TLS_LE32:        // Local-exec
9086       // If we're creating a shared library, a dynamic relocation will
9087       // have been created for this location, so do not apply it now.
9088       if (!parameters->options().shared())
9089         {
9090           gold_assert(tls_segment != NULL);
9091
9092           // $tp points to the TCB, which is followed by the TLS, so we
9093           // need to add TCB size to the offset.
9094           Arm_address aligned_tcb_size =
9095             align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
9096           RelocFuncs::rel32(view, value + aligned_tcb_size);
9097
9098         }
9099       return ArmRelocFuncs::STATUS_OKAY;
9100     
9101     default:
9102       gold_unreachable();
9103     }
9104
9105   gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9106                          _("unsupported reloc %u"),
9107                          r_type);
9108   return ArmRelocFuncs::STATUS_BAD_RELOC;
9109 }
9110
9111 // Relocate section data.
9112
9113 template<bool big_endian>
9114 void
9115 Target_arm<big_endian>::relocate_section(
9116     const Relocate_info<32, big_endian>* relinfo,
9117     unsigned int sh_type,
9118     const unsigned char* prelocs,
9119     size_t reloc_count,
9120     Output_section* output_section,
9121     bool needs_special_offset_handling,
9122     unsigned char* view,
9123     Arm_address address,
9124     section_size_type view_size,
9125     const Reloc_symbol_changes* reloc_symbol_changes)
9126 {
9127   typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
9128   gold_assert(sh_type == elfcpp::SHT_REL);
9129
9130   // See if we are relocating a relaxed input section.  If so, the view
9131   // covers the whole output section and we need to adjust accordingly.
9132   if (needs_special_offset_handling)
9133     {
9134       const Output_relaxed_input_section* poris =
9135         output_section->find_relaxed_input_section(relinfo->object,
9136                                                    relinfo->data_shndx);
9137       if (poris != NULL)
9138         {
9139           Arm_address section_address = poris->address();
9140           section_size_type section_size = poris->data_size();
9141
9142           gold_assert((section_address >= address)
9143                       && ((section_address + section_size)
9144                           <= (address + view_size)));
9145
9146           off_t offset = section_address - address;
9147           view += offset;
9148           address += offset;
9149           view_size = section_size;
9150         }
9151     }
9152
9153   gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
9154                          Arm_relocate>(
9155     relinfo,
9156     this,
9157     prelocs,
9158     reloc_count,
9159     output_section,
9160     needs_special_offset_handling,
9161     view,
9162     address,
9163     view_size,
9164     reloc_symbol_changes);
9165 }
9166
9167 // Return the size of a relocation while scanning during a relocatable
9168 // link.
9169
9170 template<bool big_endian>
9171 unsigned int
9172 Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
9173     unsigned int r_type,
9174     Relobj* object)
9175 {
9176   r_type = get_real_reloc_type(r_type);
9177   const Arm_reloc_property* arp =
9178       arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
9179   if (arp != NULL)
9180     return arp->size();
9181   else
9182     {
9183       std::string reloc_name =
9184         arm_reloc_property_table->reloc_name_in_error_message(r_type);
9185       gold_error(_("%s: unexpected %s in object file"),
9186                  object->name().c_str(), reloc_name.c_str());
9187       return 0;
9188     }
9189 }
9190
9191 // Scan the relocs during a relocatable link.
9192
9193 template<bool big_endian>
9194 void
9195 Target_arm<big_endian>::scan_relocatable_relocs(
9196     Symbol_table* symtab,
9197     Layout* layout,
9198     Sized_relobj<32, big_endian>* object,
9199     unsigned int data_shndx,
9200     unsigned int sh_type,
9201     const unsigned char* prelocs,
9202     size_t reloc_count,
9203     Output_section* output_section,
9204     bool needs_special_offset_handling,
9205     size_t local_symbol_count,
9206     const unsigned char* plocal_symbols,
9207     Relocatable_relocs* rr)
9208 {
9209   gold_assert(sh_type == elfcpp::SHT_REL);
9210
9211   typedef Arm_scan_relocatable_relocs<big_endian, elfcpp::SHT_REL,
9212     Relocatable_size_for_reloc> Scan_relocatable_relocs;
9213
9214   gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
9215       Scan_relocatable_relocs>(
9216     symtab,
9217     layout,
9218     object,
9219     data_shndx,
9220     prelocs,
9221     reloc_count,
9222     output_section,
9223     needs_special_offset_handling,
9224     local_symbol_count,
9225     plocal_symbols,
9226     rr);
9227 }
9228
9229 // Relocate a section during a relocatable link.
9230
9231 template<bool big_endian>
9232 void
9233 Target_arm<big_endian>::relocate_for_relocatable(
9234     const Relocate_info<32, big_endian>* relinfo,
9235     unsigned int sh_type,
9236     const unsigned char* prelocs,
9237     size_t reloc_count,
9238     Output_section* output_section,
9239     off_t offset_in_output_section,
9240     const Relocatable_relocs* rr,
9241     unsigned char* view,
9242     Arm_address view_address,
9243     section_size_type view_size,
9244     unsigned char* reloc_view,
9245     section_size_type reloc_view_size)
9246 {
9247   gold_assert(sh_type == elfcpp::SHT_REL);
9248
9249   gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
9250     relinfo,
9251     prelocs,
9252     reloc_count,
9253     output_section,
9254     offset_in_output_section,
9255     rr,
9256     view,
9257     view_address,
9258     view_size,
9259     reloc_view,
9260     reloc_view_size);
9261 }
9262
9263 // Perform target-specific processing in a relocatable link.  This is
9264 // only used if we use the relocation strategy RELOC_SPECIAL.
9265
9266 template<bool big_endian>
9267 void
9268 Target_arm<big_endian>::relocate_special_relocatable(
9269     const Relocate_info<32, big_endian>* relinfo,
9270     unsigned int sh_type,
9271     const unsigned char* preloc_in,
9272     size_t relnum,
9273     Output_section* output_section,
9274     off_t offset_in_output_section,
9275     unsigned char* view,
9276     elfcpp::Elf_types<32>::Elf_Addr view_address,
9277     section_size_type,
9278     unsigned char* preloc_out)
9279 {
9280   // We can only handle REL type relocation sections.
9281   gold_assert(sh_type == elfcpp::SHT_REL);
9282
9283   typedef typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc Reltype;
9284   typedef typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc_write
9285     Reltype_write;
9286   const Arm_address invalid_address = static_cast<Arm_address>(0) - 1;
9287
9288   const Arm_relobj<big_endian>* object =
9289     Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
9290   const unsigned int local_count = object->local_symbol_count();
9291
9292   Reltype reloc(preloc_in);
9293   Reltype_write reloc_write(preloc_out);
9294
9295   elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
9296   const unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
9297   const unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
9298
9299   const Arm_reloc_property* arp =
9300     arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
9301   gold_assert(arp != NULL);
9302
9303   // Get the new symbol index.
9304   // We only use RELOC_SPECIAL strategy in local relocations.
9305   gold_assert(r_sym < local_count);
9306
9307   // We are adjusting a section symbol.  We need to find
9308   // the symbol table index of the section symbol for
9309   // the output section corresponding to input section
9310   // in which this symbol is defined.
9311   bool is_ordinary;
9312   unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
9313   gold_assert(is_ordinary);
9314   Output_section* os = object->output_section(shndx);
9315   gold_assert(os != NULL);
9316   gold_assert(os->needs_symtab_index());
9317   unsigned int new_symndx = os->symtab_index();
9318
9319   // Get the new offset--the location in the output section where
9320   // this relocation should be applied.
9321
9322   Arm_address offset = reloc.get_r_offset();
9323   Arm_address new_offset;
9324   if (offset_in_output_section != invalid_address)
9325     new_offset = offset + offset_in_output_section;
9326   else
9327     {
9328       section_offset_type sot_offset =
9329           convert_types<section_offset_type, Arm_address>(offset);
9330       section_offset_type new_sot_offset =
9331           output_section->output_offset(object, relinfo->data_shndx,
9332                                         sot_offset);
9333       gold_assert(new_sot_offset != -1);
9334       new_offset = new_sot_offset;
9335     }
9336
9337   // In an object file, r_offset is an offset within the section.
9338   // In an executable or dynamic object, generated by
9339   // --emit-relocs, r_offset is an absolute address.
9340   if (!parameters->options().relocatable())
9341     {
9342       new_offset += view_address;
9343       if (offset_in_output_section != invalid_address)
9344         new_offset -= offset_in_output_section;
9345     }
9346
9347   reloc_write.put_r_offset(new_offset);
9348   reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
9349
9350   // Handle the reloc addend.
9351   // The relocation uses a section symbol in the input file.
9352   // We are adjusting it to use a section symbol in the output
9353   // file.  The input section symbol refers to some address in
9354   // the input section.  We need the relocation in the output
9355   // file to refer to that same address.  This adjustment to
9356   // the addend is the same calculation we use for a simple
9357   // absolute relocation for the input section symbol.
9358
9359   const Symbol_value<32>* psymval = object->local_symbol(r_sym);
9360
9361   // Handle THUMB bit.
9362   Symbol_value<32> symval;
9363   Arm_address thumb_bit =
9364      object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
9365   if (thumb_bit != 0
9366       && arp->uses_thumb_bit() 
9367       && ((psymval->value(object, 0) & 1) != 0))
9368     {
9369       Arm_address stripped_value =
9370         psymval->value(object, 0) & ~static_cast<Arm_address>(1);
9371       symval.set_output_value(stripped_value);
9372       psymval = &symval;
9373     } 
9374
9375   unsigned char* paddend = view + offset;
9376   typename Arm_relocate_functions<big_endian>::Status reloc_status =
9377         Arm_relocate_functions<big_endian>::STATUS_OKAY;
9378   switch (r_type)
9379     {
9380     case elfcpp::R_ARM_ABS8:
9381       reloc_status = Arm_relocate_functions<big_endian>::abs8(paddend, object,
9382                                                               psymval);
9383       break;
9384
9385     case elfcpp::R_ARM_ABS12:
9386       reloc_status = Arm_relocate_functions<big_endian>::abs12(paddend, object,
9387                                                                psymval);
9388       break;
9389
9390     case elfcpp::R_ARM_ABS16:
9391       reloc_status = Arm_relocate_functions<big_endian>::abs16(paddend, object,
9392                                                                psymval);
9393       break;
9394
9395     case elfcpp::R_ARM_THM_ABS5:
9396       reloc_status = Arm_relocate_functions<big_endian>::thm_abs5(paddend,
9397                                                                   object,
9398                                                                   psymval);
9399       break;
9400
9401     case elfcpp::R_ARM_MOVW_ABS_NC:
9402     case elfcpp::R_ARM_MOVW_PREL_NC:
9403     case elfcpp::R_ARM_MOVW_BREL_NC:
9404     case elfcpp::R_ARM_MOVW_BREL:
9405       reloc_status = Arm_relocate_functions<big_endian>::movw(
9406           paddend, object, psymval, 0, thumb_bit, arp->checks_overflow());
9407       break;
9408
9409     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
9410     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
9411     case elfcpp::R_ARM_THM_MOVW_BREL_NC:
9412     case elfcpp::R_ARM_THM_MOVW_BREL:
9413       reloc_status = Arm_relocate_functions<big_endian>::thm_movw(
9414           paddend, object, psymval, 0, thumb_bit, arp->checks_overflow());
9415       break;
9416
9417     case elfcpp::R_ARM_THM_CALL:
9418     case elfcpp::R_ARM_THM_XPC22:
9419     case elfcpp::R_ARM_THM_JUMP24:
9420       reloc_status =
9421         Arm_relocate_functions<big_endian>::thumb_branch_common(
9422             r_type, relinfo, paddend, NULL, object, 0, psymval, 0, thumb_bit,
9423             false);
9424       break;
9425
9426     case elfcpp::R_ARM_PLT32:
9427     case elfcpp::R_ARM_CALL:
9428     case elfcpp::R_ARM_JUMP24:
9429     case elfcpp::R_ARM_XPC25:
9430       reloc_status =
9431         Arm_relocate_functions<big_endian>::arm_branch_common(
9432             r_type, relinfo, paddend, NULL, object, 0, psymval, 0, thumb_bit,
9433             false);
9434       break;
9435
9436     case elfcpp::R_ARM_THM_JUMP19:
9437       reloc_status =
9438         Arm_relocate_functions<big_endian>::thm_jump19(paddend, object,
9439                                                        psymval, 0, thumb_bit);
9440       break;
9441
9442     case elfcpp::R_ARM_THM_JUMP6:
9443       reloc_status =
9444         Arm_relocate_functions<big_endian>::thm_jump6(paddend, object, psymval,
9445                                                       0);
9446       break;
9447
9448     case elfcpp::R_ARM_THM_JUMP8:
9449       reloc_status =
9450         Arm_relocate_functions<big_endian>::thm_jump8(paddend, object, psymval,
9451                                                       0);
9452       break;
9453
9454     case elfcpp::R_ARM_THM_JUMP11:
9455       reloc_status =
9456         Arm_relocate_functions<big_endian>::thm_jump11(paddend, object, psymval,
9457                                                        0);
9458       break;
9459
9460     case elfcpp::R_ARM_PREL31:
9461       reloc_status =
9462         Arm_relocate_functions<big_endian>::prel31(paddend, object, psymval, 0,
9463                                                    thumb_bit);
9464       break;
9465
9466     case elfcpp::R_ARM_THM_PC8:
9467       reloc_status =
9468         Arm_relocate_functions<big_endian>::thm_pc8(paddend, object, psymval,
9469                                                     0);
9470       break;
9471
9472     case elfcpp::R_ARM_THM_PC12:
9473       reloc_status =
9474         Arm_relocate_functions<big_endian>::thm_pc12(paddend, object, psymval,
9475                                                      0);
9476       break;
9477
9478     case elfcpp::R_ARM_THM_ALU_PREL_11_0:
9479       reloc_status =
9480         Arm_relocate_functions<big_endian>::thm_alu11(paddend, object, psymval,
9481                                                       0, thumb_bit);
9482       break;
9483
9484     // These relocation truncate relocation results so we cannot handle them
9485     // in a relocatable link.
9486     case elfcpp::R_ARM_MOVT_ABS:
9487     case elfcpp::R_ARM_THM_MOVT_ABS:
9488     case elfcpp::R_ARM_MOVT_PREL:
9489     case elfcpp::R_ARM_MOVT_BREL:
9490     case elfcpp::R_ARM_THM_MOVT_PREL:
9491     case elfcpp::R_ARM_THM_MOVT_BREL:
9492     case elfcpp::R_ARM_ALU_PC_G0_NC:
9493     case elfcpp::R_ARM_ALU_PC_G0:
9494     case elfcpp::R_ARM_ALU_PC_G1_NC:
9495     case elfcpp::R_ARM_ALU_PC_G1:
9496     case elfcpp::R_ARM_ALU_PC_G2:
9497     case elfcpp::R_ARM_ALU_SB_G0_NC:
9498     case elfcpp::R_ARM_ALU_SB_G0:
9499     case elfcpp::R_ARM_ALU_SB_G1_NC:
9500     case elfcpp::R_ARM_ALU_SB_G1:
9501     case elfcpp::R_ARM_ALU_SB_G2:
9502     case elfcpp::R_ARM_LDR_PC_G0:
9503     case elfcpp::R_ARM_LDR_PC_G1:
9504     case elfcpp::R_ARM_LDR_PC_G2:
9505     case elfcpp::R_ARM_LDR_SB_G0:
9506     case elfcpp::R_ARM_LDR_SB_G1:
9507     case elfcpp::R_ARM_LDR_SB_G2:
9508     case elfcpp::R_ARM_LDRS_PC_G0:
9509     case elfcpp::R_ARM_LDRS_PC_G1:
9510     case elfcpp::R_ARM_LDRS_PC_G2:
9511     case elfcpp::R_ARM_LDRS_SB_G0:
9512     case elfcpp::R_ARM_LDRS_SB_G1:
9513     case elfcpp::R_ARM_LDRS_SB_G2:
9514     case elfcpp::R_ARM_LDC_PC_G0:
9515     case elfcpp::R_ARM_LDC_PC_G1:
9516     case elfcpp::R_ARM_LDC_PC_G2:
9517     case elfcpp::R_ARM_LDC_SB_G0:
9518     case elfcpp::R_ARM_LDC_SB_G1:
9519     case elfcpp::R_ARM_LDC_SB_G2:
9520       gold_error(_("cannot handle %s in a relocatable link"),
9521                  arp->name().c_str());
9522       break;
9523
9524     default:
9525       gold_unreachable();
9526     }
9527
9528   // Report any errors.
9529   switch (reloc_status)
9530     {
9531     case Arm_relocate_functions<big_endian>::STATUS_OKAY:
9532       break;
9533     case Arm_relocate_functions<big_endian>::STATUS_OVERFLOW:
9534       gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
9535                              _("relocation overflow in %s"),
9536                              arp->name().c_str());
9537       break;
9538     case Arm_relocate_functions<big_endian>::STATUS_BAD_RELOC:
9539       gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
9540         _("unexpected opcode while processing relocation %s"),
9541         arp->name().c_str());
9542       break;
9543     default:
9544       gold_unreachable();
9545     }
9546 }
9547
9548 // Return the value to use for a dynamic symbol which requires special
9549 // treatment.  This is how we support equality comparisons of function
9550 // pointers across shared library boundaries, as described in the
9551 // processor specific ABI supplement.
9552
9553 template<bool big_endian>
9554 uint64_t
9555 Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
9556 {
9557   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
9558   return this->plt_section()->address() + gsym->plt_offset();
9559 }
9560
9561 // Map platform-specific relocs to real relocs
9562 //
9563 template<bool big_endian>
9564 unsigned int
9565 Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
9566 {
9567   switch (r_type)
9568     {
9569     case elfcpp::R_ARM_TARGET1:
9570       // This is either R_ARM_ABS32 or R_ARM_REL32;
9571       return elfcpp::R_ARM_ABS32;
9572
9573     case elfcpp::R_ARM_TARGET2:
9574       // This can be any reloc type but ususally is R_ARM_GOT_PREL
9575       return elfcpp::R_ARM_GOT_PREL;
9576
9577     default:
9578       return r_type;
9579     }
9580 }
9581
9582 // Whether if two EABI versions V1 and V2 are compatible.
9583
9584 template<bool big_endian>
9585 bool
9586 Target_arm<big_endian>::are_eabi_versions_compatible(
9587     elfcpp::Elf_Word v1,
9588     elfcpp::Elf_Word v2)
9589 {
9590   // v4 and v5 are the same spec before and after it was released,
9591   // so allow mixing them.
9592   if ((v1 == elfcpp::EF_ARM_EABI_UNKNOWN || v2 == elfcpp::EF_ARM_EABI_UNKNOWN)
9593       || (v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
9594       || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
9595     return true;
9596
9597   return v1 == v2;
9598 }
9599
9600 // Combine FLAGS from an input object called NAME and the processor-specific
9601 // flags in the ELF header of the output.  Much of this is adapted from the
9602 // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
9603 // in bfd/elf32-arm.c.
9604
9605 template<bool big_endian>
9606 void
9607 Target_arm<big_endian>::merge_processor_specific_flags(
9608     const std::string& name,
9609     elfcpp::Elf_Word flags)
9610 {
9611   if (this->are_processor_specific_flags_set())
9612     {
9613       elfcpp::Elf_Word out_flags = this->processor_specific_flags();
9614
9615       // Nothing to merge if flags equal to those in output.
9616       if (flags == out_flags)
9617         return;
9618
9619       // Complain about various flag mismatches.
9620       elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
9621       elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
9622       if (!this->are_eabi_versions_compatible(version1, version2)
9623           && parameters->options().warn_mismatch())
9624         gold_error(_("Source object %s has EABI version %d but output has "
9625                      "EABI version %d."),
9626                    name.c_str(),
9627                    (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
9628                    (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
9629     }
9630   else
9631     {
9632       // If the input is the default architecture and had the default
9633       // flags then do not bother setting the flags for the output
9634       // architecture, instead allow future merges to do this.  If no
9635       // future merges ever set these flags then they will retain their
9636       // uninitialised values, which surprise surprise, correspond
9637       // to the default values.
9638       if (flags == 0)
9639         return;
9640
9641       // This is the first time, just copy the flags.
9642       // We only copy the EABI version for now.
9643       this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
9644     }
9645 }
9646
9647 // Adjust ELF file header.
9648 template<bool big_endian>
9649 void
9650 Target_arm<big_endian>::do_adjust_elf_header(
9651     unsigned char* view,
9652     int len) const
9653 {
9654   gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
9655
9656   elfcpp::Ehdr<32, big_endian> ehdr(view);
9657   unsigned char e_ident[elfcpp::EI_NIDENT];
9658   memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
9659
9660   if (elfcpp::arm_eabi_version(this->processor_specific_flags())
9661       == elfcpp::EF_ARM_EABI_UNKNOWN)
9662     e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
9663   else
9664     e_ident[elfcpp::EI_OSABI] = 0;
9665   e_ident[elfcpp::EI_ABIVERSION] = 0;
9666
9667   // FIXME: Do EF_ARM_BE8 adjustment.
9668
9669   elfcpp::Ehdr_write<32, big_endian> oehdr(view);
9670   oehdr.put_e_ident(e_ident);
9671 }
9672
9673 // do_make_elf_object to override the same function in the base class.
9674 // We need to use a target-specific sub-class of Sized_relobj<32, big_endian>
9675 // to store ARM specific information.  Hence we need to have our own
9676 // ELF object creation.
9677
9678 template<bool big_endian>
9679 Object*
9680 Target_arm<big_endian>::do_make_elf_object(
9681     const std::string& name,
9682     Input_file* input_file,
9683     off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
9684 {
9685   int et = ehdr.get_e_type();
9686   if (et == elfcpp::ET_REL)
9687     {
9688       Arm_relobj<big_endian>* obj =
9689         new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
9690       obj->setup();
9691       return obj;
9692     }
9693   else if (et == elfcpp::ET_DYN)
9694     {
9695       Sized_dynobj<32, big_endian>* obj =
9696         new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
9697       obj->setup();
9698       return obj;
9699     }
9700   else
9701     {
9702       gold_error(_("%s: unsupported ELF file type %d"),
9703                  name.c_str(), et);
9704       return NULL;
9705     }
9706 }
9707
9708 // Read the architecture from the Tag_also_compatible_with attribute, if any.
9709 // Returns -1 if no architecture could be read.
9710 // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
9711
9712 template<bool big_endian>
9713 int
9714 Target_arm<big_endian>::get_secondary_compatible_arch(
9715     const Attributes_section_data* pasd)
9716 {
9717   const Object_attribute *known_attributes =
9718     pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
9719
9720   // Note: the tag and its argument below are uleb128 values, though
9721   // currently-defined values fit in one byte for each.
9722   const std::string& sv =
9723     known_attributes[elfcpp::Tag_also_compatible_with].string_value();
9724   if (sv.size() == 2
9725       && sv.data()[0] == elfcpp::Tag_CPU_arch
9726       && (sv.data()[1] & 128) != 128)
9727    return sv.data()[1];
9728
9729   // This tag is "safely ignorable", so don't complain if it looks funny.
9730   return -1;
9731 }
9732
9733 // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
9734 // The tag is removed if ARCH is -1.
9735 // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
9736
9737 template<bool big_endian>
9738 void
9739 Target_arm<big_endian>::set_secondary_compatible_arch(
9740     Attributes_section_data* pasd,
9741     int arch)
9742 {
9743   Object_attribute *known_attributes =
9744     pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
9745
9746   if (arch == -1)
9747     {
9748       known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
9749       return;
9750     }
9751
9752   // Note: the tag and its argument below are uleb128 values, though
9753   // currently-defined values fit in one byte for each.
9754   char sv[3];
9755   sv[0] = elfcpp::Tag_CPU_arch;
9756   gold_assert(arch != 0);
9757   sv[1] = arch;
9758   sv[2] = '\0';
9759
9760   known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
9761 }
9762
9763 // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
9764 // into account.
9765 // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
9766
9767 template<bool big_endian>
9768 int
9769 Target_arm<big_endian>::tag_cpu_arch_combine(
9770     const char* name,
9771     int oldtag,
9772     int* secondary_compat_out,
9773     int newtag,
9774     int secondary_compat)
9775 {
9776 #define T(X) elfcpp::TAG_CPU_ARCH_##X
9777   static const int v6t2[] =
9778     {
9779       T(V6T2),   // PRE_V4.
9780       T(V6T2),   // V4.
9781       T(V6T2),   // V4T.
9782       T(V6T2),   // V5T.
9783       T(V6T2),   // V5TE.
9784       T(V6T2),   // V5TEJ.
9785       T(V6T2),   // V6.
9786       T(V7),     // V6KZ.
9787       T(V6T2)    // V6T2.
9788     };
9789   static const int v6k[] =
9790     {
9791       T(V6K),    // PRE_V4.
9792       T(V6K),    // V4.
9793       T(V6K),    // V4T.
9794       T(V6K),    // V5T.
9795       T(V6K),    // V5TE.
9796       T(V6K),    // V5TEJ.
9797       T(V6K),    // V6.
9798       T(V6KZ),   // V6KZ.
9799       T(V7),     // V6T2.
9800       T(V6K)     // V6K.
9801     };
9802   static const int v7[] =
9803     {
9804       T(V7),     // PRE_V4.
9805       T(V7),     // V4.
9806       T(V7),     // V4T.
9807       T(V7),     // V5T.
9808       T(V7),     // V5TE.
9809       T(V7),     // V5TEJ.
9810       T(V7),     // V6.
9811       T(V7),     // V6KZ.
9812       T(V7),     // V6T2.
9813       T(V7),     // V6K.
9814       T(V7)      // V7.
9815     };
9816   static const int v6_m[] =
9817     {
9818       -1,        // PRE_V4.
9819       -1,        // V4.
9820       T(V6K),    // V4T.
9821       T(V6K),    // V5T.
9822       T(V6K),    // V5TE.
9823       T(V6K),    // V5TEJ.
9824       T(V6K),    // V6.
9825       T(V6KZ),   // V6KZ.
9826       T(V7),     // V6T2.
9827       T(V6K),    // V6K.
9828       T(V7),     // V7.
9829       T(V6_M)    // V6_M.
9830     };
9831   static const int v6s_m[] =
9832     {
9833       -1,        // PRE_V4.
9834       -1,        // V4.
9835       T(V6K),    // V4T.
9836       T(V6K),    // V5T.
9837       T(V6K),    // V5TE.
9838       T(V6K),    // V5TEJ.
9839       T(V6K),    // V6.
9840       T(V6KZ),   // V6KZ.
9841       T(V7),     // V6T2.
9842       T(V6K),    // V6K.
9843       T(V7),     // V7.
9844       T(V6S_M),  // V6_M.
9845       T(V6S_M)   // V6S_M.
9846     };
9847   static const int v7e_m[] =
9848     {
9849       -1,       // PRE_V4.
9850       -1,       // V4.
9851       T(V7E_M), // V4T.
9852       T(V7E_M), // V5T.
9853       T(V7E_M), // V5TE.
9854       T(V7E_M), // V5TEJ.
9855       T(V7E_M), // V6.
9856       T(V7E_M), // V6KZ.
9857       T(V7E_M), // V6T2.
9858       T(V7E_M), // V6K.
9859       T(V7E_M), // V7.
9860       T(V7E_M), // V6_M.
9861       T(V7E_M), // V6S_M.
9862       T(V7E_M)  // V7E_M.
9863     };
9864   static const int v4t_plus_v6_m[] =
9865     {
9866       -1,               // PRE_V4.
9867       -1,               // V4.
9868       T(V4T),           // V4T.
9869       T(V5T),           // V5T.
9870       T(V5TE),          // V5TE.
9871       T(V5TEJ),         // V5TEJ.
9872       T(V6),            // V6.
9873       T(V6KZ),          // V6KZ.
9874       T(V6T2),          // V6T2.
9875       T(V6K),           // V6K.
9876       T(V7),            // V7.
9877       T(V6_M),          // V6_M.
9878       T(V6S_M),         // V6S_M.
9879       T(V7E_M),         // V7E_M.
9880       T(V4T_PLUS_V6_M)  // V4T plus V6_M.
9881     };
9882   static const int *comb[] =
9883     {
9884       v6t2,
9885       v6k,
9886       v7,
9887       v6_m,
9888       v6s_m,
9889       v7e_m,
9890       // Pseudo-architecture.
9891       v4t_plus_v6_m
9892     };
9893
9894   // Check we've not got a higher architecture than we know about.
9895
9896   if (oldtag >= elfcpp::MAX_TAG_CPU_ARCH || newtag >= elfcpp::MAX_TAG_CPU_ARCH)
9897     {
9898       gold_error(_("%s: unknown CPU architecture"), name);
9899       return -1;
9900     }
9901
9902   // Override old tag if we have a Tag_also_compatible_with on the output.
9903
9904   if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
9905       || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
9906     oldtag = T(V4T_PLUS_V6_M);
9907
9908   // And override the new tag if we have a Tag_also_compatible_with on the
9909   // input.
9910
9911   if ((newtag == T(V6_M) && secondary_compat == T(V4T))
9912       || (newtag == T(V4T) && secondary_compat == T(V6_M)))
9913     newtag = T(V4T_PLUS_V6_M);
9914
9915   // Architectures before V6KZ add features monotonically.
9916   int tagh = std::max(oldtag, newtag);
9917   if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
9918     return tagh;
9919
9920   int tagl = std::min(oldtag, newtag);
9921   int result = comb[tagh - T(V6T2)][tagl];
9922
9923   // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
9924   // as the canonical version.
9925   if (result == T(V4T_PLUS_V6_M))
9926     {
9927       result = T(V4T);
9928       *secondary_compat_out = T(V6_M);
9929     }
9930   else
9931     *secondary_compat_out = -1;
9932
9933   if (result == -1)
9934     {
9935       gold_error(_("%s: conflicting CPU architectures %d/%d"),
9936                  name, oldtag, newtag);
9937       return -1;
9938     }
9939
9940   return result;
9941 #undef T
9942 }
9943
9944 // Helper to print AEABI enum tag value.
9945
9946 template<bool big_endian>
9947 std::string
9948 Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
9949 {
9950   static const char *aeabi_enum_names[] =
9951     { "", "variable-size", "32-bit", "" };
9952   const size_t aeabi_enum_names_size =
9953     sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
9954
9955   if (value < aeabi_enum_names_size)
9956     return std::string(aeabi_enum_names[value]);
9957   else
9958     {
9959       char buffer[100];
9960       sprintf(buffer, "<unknown value %u>", value);
9961       return std::string(buffer);
9962     }
9963 }
9964
9965 // Return the string value to store in TAG_CPU_name.
9966
9967 template<bool big_endian>
9968 std::string
9969 Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
9970 {
9971   static const char *name_table[] = {
9972     // These aren't real CPU names, but we can't guess
9973     // that from the architecture version alone.
9974    "Pre v4",
9975    "ARM v4",
9976    "ARM v4T",
9977    "ARM v5T",
9978    "ARM v5TE",
9979    "ARM v5TEJ",
9980    "ARM v6",
9981    "ARM v6KZ",
9982    "ARM v6T2",
9983    "ARM v6K",
9984    "ARM v7",
9985    "ARM v6-M",
9986    "ARM v6S-M",
9987    "ARM v7E-M"
9988  };
9989  const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
9990
9991   if (value < name_table_size)
9992     return std::string(name_table[value]);
9993   else
9994     {
9995       char buffer[100];
9996       sprintf(buffer, "<unknown CPU value %u>", value);
9997       return std::string(buffer);
9998     } 
9999 }
10000
10001 // Merge object attributes from input file called NAME with those of the
10002 // output.  The input object attributes are in the object pointed by PASD.
10003
10004 template<bool big_endian>
10005 void
10006 Target_arm<big_endian>::merge_object_attributes(
10007     const char* name,
10008     const Attributes_section_data* pasd)
10009 {
10010   // Return if there is no attributes section data.
10011   if (pasd == NULL)
10012     return;
10013
10014   // If output has no object attributes, just copy.
10015   const int vendor = Object_attribute::OBJ_ATTR_PROC;
10016   if (this->attributes_section_data_ == NULL)
10017     {
10018       this->attributes_section_data_ = new Attributes_section_data(*pasd);
10019       Object_attribute* out_attr =
10020         this->attributes_section_data_->known_attributes(vendor);
10021
10022       // We do not output objects with Tag_MPextension_use_legacy - we move
10023       //  the attribute's value to Tag_MPextension_use.  */
10024       if (out_attr[elfcpp::Tag_MPextension_use_legacy].int_value() != 0)
10025         {
10026           if (out_attr[elfcpp::Tag_MPextension_use].int_value() != 0
10027               && out_attr[elfcpp::Tag_MPextension_use_legacy].int_value()
10028                 != out_attr[elfcpp::Tag_MPextension_use].int_value())
10029             {
10030               gold_error(_("%s has both the current and legacy "
10031                            "Tag_MPextension_use attributes"),
10032                          name);
10033             }
10034
10035           out_attr[elfcpp::Tag_MPextension_use] =
10036             out_attr[elfcpp::Tag_MPextension_use_legacy];
10037           out_attr[elfcpp::Tag_MPextension_use_legacy].set_type(0);
10038           out_attr[elfcpp::Tag_MPextension_use_legacy].set_int_value(0);
10039         }
10040
10041       return;
10042     }
10043
10044   const Object_attribute* in_attr = pasd->known_attributes(vendor);
10045   Object_attribute* out_attr =
10046     this->attributes_section_data_->known_attributes(vendor);
10047
10048   // This needs to happen before Tag_ABI_FP_number_model is merged.  */
10049   if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
10050       != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
10051     {
10052       // Ignore mismatches if the object doesn't use floating point.  */
10053       if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
10054         out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
10055             in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
10056       else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0
10057                && parameters->options().warn_mismatch())
10058         gold_error(_("%s uses VFP register arguments, output does not"),
10059                    name);
10060     }
10061
10062   for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
10063     {
10064       // Merge this attribute with existing attributes.
10065       switch (i)
10066         {
10067         case elfcpp::Tag_CPU_raw_name:
10068         case elfcpp::Tag_CPU_name:
10069           // These are merged after Tag_CPU_arch.
10070           break;
10071
10072         case elfcpp::Tag_ABI_optimization_goals:
10073         case elfcpp::Tag_ABI_FP_optimization_goals:
10074           // Use the first value seen.
10075           break;
10076
10077         case elfcpp::Tag_CPU_arch:
10078           {
10079             unsigned int saved_out_attr = out_attr->int_value();
10080             // Merge Tag_CPU_arch and Tag_also_compatible_with.
10081             int secondary_compat =
10082               this->get_secondary_compatible_arch(pasd);
10083             int secondary_compat_out =
10084               this->get_secondary_compatible_arch(
10085                   this->attributes_section_data_);
10086             out_attr[i].set_int_value(
10087                 tag_cpu_arch_combine(name, out_attr[i].int_value(),
10088                                      &secondary_compat_out,
10089                                      in_attr[i].int_value(),
10090                                      secondary_compat));
10091             this->set_secondary_compatible_arch(this->attributes_section_data_,
10092                                                 secondary_compat_out);
10093
10094             // Merge Tag_CPU_name and Tag_CPU_raw_name.
10095             if (out_attr[i].int_value() == saved_out_attr)
10096               ; // Leave the names alone.
10097             else if (out_attr[i].int_value() == in_attr[i].int_value())
10098               {
10099                 // The output architecture has been changed to match the
10100                 // input architecture.  Use the input names.
10101                 out_attr[elfcpp::Tag_CPU_name].set_string_value(
10102                     in_attr[elfcpp::Tag_CPU_name].string_value());
10103                 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
10104                     in_attr[elfcpp::Tag_CPU_raw_name].string_value());
10105               }
10106             else
10107               {
10108                 out_attr[elfcpp::Tag_CPU_name].set_string_value("");
10109                 out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
10110               }
10111
10112             // If we still don't have a value for Tag_CPU_name,
10113             // make one up now.  Tag_CPU_raw_name remains blank.
10114             if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
10115               {
10116                 const std::string cpu_name =
10117                   this->tag_cpu_name_value(out_attr[i].int_value());
10118                 // FIXME:  If we see an unknown CPU, this will be set
10119                 // to "<unknown CPU n>", where n is the attribute value.
10120                 // This is different from BFD, which leaves the name alone.
10121                 out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
10122               }
10123           }
10124           break;
10125
10126         case elfcpp::Tag_ARM_ISA_use:
10127         case elfcpp::Tag_THUMB_ISA_use:
10128         case elfcpp::Tag_WMMX_arch:
10129         case elfcpp::Tag_Advanced_SIMD_arch:
10130           // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
10131         case elfcpp::Tag_ABI_FP_rounding:
10132         case elfcpp::Tag_ABI_FP_exceptions:
10133         case elfcpp::Tag_ABI_FP_user_exceptions:
10134         case elfcpp::Tag_ABI_FP_number_model:
10135         case elfcpp::Tag_VFP_HP_extension:
10136         case elfcpp::Tag_CPU_unaligned_access:
10137         case elfcpp::Tag_T2EE_use:
10138         case elfcpp::Tag_Virtualization_use:
10139         case elfcpp::Tag_MPextension_use:
10140           // Use the largest value specified.
10141           if (in_attr[i].int_value() > out_attr[i].int_value())
10142             out_attr[i].set_int_value(in_attr[i].int_value());
10143           break;
10144
10145         case elfcpp::Tag_ABI_align8_preserved:
10146         case elfcpp::Tag_ABI_PCS_RO_data:
10147           // Use the smallest value specified.
10148           if (in_attr[i].int_value() < out_attr[i].int_value())
10149             out_attr[i].set_int_value(in_attr[i].int_value());
10150           break;
10151
10152         case elfcpp::Tag_ABI_align8_needed:
10153           if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
10154               && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
10155                   || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
10156                       == 0)))
10157             {
10158               // This error message should be enabled once all non-conformant
10159               // binaries in the toolchain have had the attributes set
10160               // properly.
10161               // gold_error(_("output 8-byte data alignment conflicts with %s"),
10162               //            name);
10163             }
10164           // Fall through.
10165         case elfcpp::Tag_ABI_FP_denormal:
10166         case elfcpp::Tag_ABI_PCS_GOT_use:
10167           {
10168             // These tags have 0 = don't care, 1 = strong requirement,
10169             // 2 = weak requirement.
10170             static const int order_021[3] = {0, 2, 1};
10171
10172             // Use the "greatest" from the sequence 0, 2, 1, or the largest
10173             // value if greater than 2 (for future-proofing).
10174             if ((in_attr[i].int_value() > 2
10175                  && in_attr[i].int_value() > out_attr[i].int_value())
10176                 || (in_attr[i].int_value() <= 2
10177                     && out_attr[i].int_value() <= 2
10178                     && (order_021[in_attr[i].int_value()]
10179                         > order_021[out_attr[i].int_value()])))
10180               out_attr[i].set_int_value(in_attr[i].int_value());
10181           }
10182           break;
10183
10184         case elfcpp::Tag_CPU_arch_profile:
10185           if (out_attr[i].int_value() != in_attr[i].int_value())
10186             {
10187               // 0 will merge with anything.
10188               // 'A' and 'S' merge to 'A'.
10189               // 'R' and 'S' merge to 'R'.
10190               // 'M' and 'A|R|S' is an error.
10191               if (out_attr[i].int_value() == 0
10192                   || (out_attr[i].int_value() == 'S'
10193                       && (in_attr[i].int_value() == 'A'
10194                           || in_attr[i].int_value() == 'R')))
10195                 out_attr[i].set_int_value(in_attr[i].int_value());
10196               else if (in_attr[i].int_value() == 0
10197                        || (in_attr[i].int_value() == 'S'
10198                            && (out_attr[i].int_value() == 'A'
10199                                || out_attr[i].int_value() == 'R')))
10200                 ; // Do nothing.
10201               else if (parameters->options().warn_mismatch())
10202                 {
10203                   gold_error
10204                     (_("conflicting architecture profiles %c/%c"),
10205                      in_attr[i].int_value() ? in_attr[i].int_value() : '0',
10206                      out_attr[i].int_value() ? out_attr[i].int_value() : '0');
10207                 }
10208             }
10209           break;
10210         case elfcpp::Tag_VFP_arch:
10211             {
10212               static const struct
10213               {
10214                   int ver;
10215                   int regs;
10216               } vfp_versions[7] =
10217                 {
10218                   {0, 0},
10219                   {1, 16},
10220                   {2, 16},
10221                   {3, 32},
10222                   {3, 16},
10223                   {4, 32},
10224                   {4, 16}
10225                 };
10226
10227               // Values greater than 6 aren't defined, so just pick the
10228               // biggest.
10229               if (in_attr[i].int_value() > 6
10230                   && in_attr[i].int_value() > out_attr[i].int_value())
10231                 {
10232                   *out_attr = *in_attr;
10233                   break;
10234                 }
10235               // The output uses the superset of input features
10236               // (ISA version) and registers.
10237               int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
10238                                  vfp_versions[out_attr[i].int_value()].ver);
10239               int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
10240                                   vfp_versions[out_attr[i].int_value()].regs);
10241               // This assumes all possible supersets are also a valid
10242               // options.
10243               int newval;
10244               for (newval = 6; newval > 0; newval--)
10245                 {
10246                   if (regs == vfp_versions[newval].regs
10247                       && ver == vfp_versions[newval].ver)
10248                     break;
10249                 }
10250               out_attr[i].set_int_value(newval);
10251             }
10252           break;
10253         case elfcpp::Tag_PCS_config:
10254           if (out_attr[i].int_value() == 0)
10255             out_attr[i].set_int_value(in_attr[i].int_value());
10256           else if (in_attr[i].int_value() != 0
10257                    && out_attr[i].int_value() != 0
10258                    && parameters->options().warn_mismatch())
10259             {
10260               // It's sometimes ok to mix different configs, so this is only
10261               // a warning.
10262               gold_warning(_("%s: conflicting platform configuration"), name);
10263             }
10264           break;
10265         case elfcpp::Tag_ABI_PCS_R9_use:
10266           if (in_attr[i].int_value() != out_attr[i].int_value()
10267               && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
10268               && in_attr[i].int_value() != elfcpp::AEABI_R9_unused
10269               && parameters->options().warn_mismatch())
10270             {
10271               gold_error(_("%s: conflicting use of R9"), name);
10272             }
10273           if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
10274             out_attr[i].set_int_value(in_attr[i].int_value());
10275           break;
10276         case elfcpp::Tag_ABI_PCS_RW_data:
10277           if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
10278               && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
10279                   != elfcpp::AEABI_R9_SB)
10280               && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
10281                   != elfcpp::AEABI_R9_unused)
10282               && parameters->options().warn_mismatch())
10283             {
10284               gold_error(_("%s: SB relative addressing conflicts with use "
10285                            "of R9"),
10286                            name);
10287             }
10288           // Use the smallest value specified.
10289           if (in_attr[i].int_value() < out_attr[i].int_value())
10290             out_attr[i].set_int_value(in_attr[i].int_value());
10291           break;
10292         case elfcpp::Tag_ABI_PCS_wchar_t:
10293           // FIXME: Make it possible to turn off this warning.
10294           if (out_attr[i].int_value()
10295               && in_attr[i].int_value()
10296               && out_attr[i].int_value() != in_attr[i].int_value()
10297               && parameters->options().warn_mismatch())
10298             {
10299               gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
10300                              "use %u-byte wchar_t; use of wchar_t values "
10301                              "across objects may fail"),
10302                            name, in_attr[i].int_value(),
10303                            out_attr[i].int_value());
10304             }
10305           else if (in_attr[i].int_value() && !out_attr[i].int_value())
10306             out_attr[i].set_int_value(in_attr[i].int_value());
10307           break;
10308         case elfcpp::Tag_ABI_enum_size:
10309           if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
10310             {
10311               if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
10312                   || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
10313                 {
10314                   // The existing object is compatible with anything.
10315                   // Use whatever requirements the new object has.
10316                   out_attr[i].set_int_value(in_attr[i].int_value());
10317                 }
10318               // FIXME: Make it possible to turn off this warning.
10319               else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
10320                        && out_attr[i].int_value() != in_attr[i].int_value()
10321                        && parameters->options().warn_mismatch())
10322                 {
10323                   unsigned int in_value = in_attr[i].int_value();
10324                   unsigned int out_value = out_attr[i].int_value();
10325                   gold_warning(_("%s uses %s enums yet the output is to use "
10326                                  "%s enums; use of enum values across objects "
10327                                  "may fail"),
10328                                name,
10329                                this->aeabi_enum_name(in_value).c_str(),
10330                                this->aeabi_enum_name(out_value).c_str());
10331                 }
10332             }
10333           break;
10334         case elfcpp::Tag_ABI_VFP_args:
10335           // Aready done.
10336           break;
10337         case elfcpp::Tag_ABI_WMMX_args:
10338           if (in_attr[i].int_value() != out_attr[i].int_value()
10339               && parameters->options().warn_mismatch())
10340             {
10341               gold_error(_("%s uses iWMMXt register arguments, output does "
10342                            "not"),
10343                          name);
10344             }
10345           break;
10346         case Object_attribute::Tag_compatibility:
10347           // Merged in target-independent code.
10348           break;
10349         case elfcpp::Tag_ABI_HardFP_use:
10350           // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
10351           if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
10352               || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
10353             out_attr[i].set_int_value(3);
10354           else if (in_attr[i].int_value() > out_attr[i].int_value())
10355             out_attr[i].set_int_value(in_attr[i].int_value());
10356           break;
10357         case elfcpp::Tag_ABI_FP_16bit_format:
10358           if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
10359             {
10360               if (in_attr[i].int_value() != out_attr[i].int_value()
10361                   && parameters->options().warn_mismatch())
10362                 gold_error(_("fp16 format mismatch between %s and output"),
10363                            name);
10364             }
10365           if (in_attr[i].int_value() != 0)
10366             out_attr[i].set_int_value(in_attr[i].int_value());
10367           break;
10368
10369         case elfcpp::Tag_DIV_use:
10370           // This tag is set to zero if we can use UDIV and SDIV in Thumb
10371           // mode on a v7-M or v7-R CPU; to one if we can not use UDIV or
10372           // SDIV at all; and to two if we can use UDIV or SDIV on a v7-A
10373           // CPU.  We will merge as follows: If the input attribute's value
10374           // is one then the output attribute's value remains unchanged.  If
10375           // the input attribute's value is zero or two then if the output
10376           // attribute's value is one the output value is set to the input
10377           // value, otherwise the output value must be the same as the
10378           // inputs.  */ 
10379           if (in_attr[i].int_value() != 1 && out_attr[i].int_value() != 1) 
10380             { 
10381               if (in_attr[i].int_value() != out_attr[i].int_value())
10382                 {
10383                   gold_error(_("DIV usage mismatch between %s and output"),
10384                              name);
10385                 }
10386             } 
10387
10388           if (in_attr[i].int_value() != 1)
10389             out_attr[i].set_int_value(in_attr[i].int_value()); 
10390           
10391           break;
10392
10393         case elfcpp::Tag_MPextension_use_legacy:
10394           // We don't output objects with Tag_MPextension_use_legacy - we
10395           // move the value to Tag_MPextension_use.
10396           if (in_attr[i].int_value() != 0
10397               && in_attr[elfcpp::Tag_MPextension_use].int_value() != 0)
10398             {
10399               if (in_attr[elfcpp::Tag_MPextension_use].int_value()
10400                   != in_attr[i].int_value())
10401                 {
10402                   gold_error(_("%s has has both the current and legacy "
10403                                "Tag_MPextension_use attributes"), 
10404                              name);
10405                 }
10406             }
10407
10408           if (in_attr[i].int_value()
10409               > out_attr[elfcpp::Tag_MPextension_use].int_value())
10410             out_attr[elfcpp::Tag_MPextension_use] = in_attr[i];
10411
10412           break;
10413
10414         case elfcpp::Tag_nodefaults:
10415           // This tag is set if it exists, but the value is unused (and is
10416           // typically zero).  We don't actually need to do anything here -
10417           // the merge happens automatically when the type flags are merged
10418           // below.
10419           break;
10420         case elfcpp::Tag_also_compatible_with:
10421           // Already done in Tag_CPU_arch.
10422           break;
10423         case elfcpp::Tag_conformance:
10424           // Keep the attribute if it matches.  Throw it away otherwise.
10425           // No attribute means no claim to conform.
10426           if (in_attr[i].string_value() != out_attr[i].string_value())
10427             out_attr[i].set_string_value("");
10428           break;
10429
10430         default:
10431           {
10432             const char* err_object = NULL;
10433
10434             // The "known_obj_attributes" table does contain some undefined
10435             // attributes.  Ensure that there are unused.
10436             if (out_attr[i].int_value() != 0
10437                 || out_attr[i].string_value() != "")
10438               err_object = "output";
10439             else if (in_attr[i].int_value() != 0
10440                      || in_attr[i].string_value() != "")
10441               err_object = name;
10442
10443             if (err_object != NULL
10444                 && parameters->options().warn_mismatch())
10445               {
10446                 // Attribute numbers >=64 (mod 128) can be safely ignored.
10447                 if ((i & 127) < 64)
10448                   gold_error(_("%s: unknown mandatory EABI object attribute "
10449                                "%d"),
10450                              err_object, i);
10451                 else
10452                   gold_warning(_("%s: unknown EABI object attribute %d"),
10453                                err_object, i);
10454               }
10455
10456             // Only pass on attributes that match in both inputs.
10457             if (!in_attr[i].matches(out_attr[i]))
10458               {
10459                 out_attr[i].set_int_value(0);
10460                 out_attr[i].set_string_value("");
10461               }
10462           }
10463         }
10464
10465       // If out_attr was copied from in_attr then it won't have a type yet.
10466       if (in_attr[i].type() && !out_attr[i].type())
10467         out_attr[i].set_type(in_attr[i].type());
10468     }
10469
10470   // Merge Tag_compatibility attributes and any common GNU ones.
10471   this->attributes_section_data_->merge(name, pasd);
10472
10473   // Check for any attributes not known on ARM.
10474   typedef Vendor_object_attributes::Other_attributes Other_attributes;
10475   const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
10476   Other_attributes::const_iterator in_iter = in_other_attributes->begin();
10477   Other_attributes* out_other_attributes =
10478     this->attributes_section_data_->other_attributes(vendor);
10479   Other_attributes::iterator out_iter = out_other_attributes->begin();
10480
10481   while (in_iter != in_other_attributes->end()
10482          || out_iter != out_other_attributes->end())
10483     {
10484       const char* err_object = NULL;
10485       int err_tag = 0;
10486
10487       // The tags for each list are in numerical order.
10488       // If the tags are equal, then merge.
10489       if (out_iter != out_other_attributes->end()
10490           && (in_iter == in_other_attributes->end()
10491               || in_iter->first > out_iter->first))
10492         {
10493           // This attribute only exists in output.  We can't merge, and we
10494           // don't know what the tag means, so delete it.
10495           err_object = "output";
10496           err_tag = out_iter->first;
10497           int saved_tag = out_iter->first;
10498           delete out_iter->second;
10499           out_other_attributes->erase(out_iter); 
10500           out_iter = out_other_attributes->upper_bound(saved_tag);
10501         }
10502       else if (in_iter != in_other_attributes->end()
10503                && (out_iter != out_other_attributes->end()
10504                    || in_iter->first < out_iter->first))
10505         {
10506           // This attribute only exists in input. We can't merge, and we
10507           // don't know what the tag means, so ignore it.
10508           err_object = name;
10509           err_tag = in_iter->first;
10510           ++in_iter;
10511         }
10512       else // The tags are equal.
10513         {
10514           // As present, all attributes in the list are unknown, and
10515           // therefore can't be merged meaningfully.
10516           err_object = "output";
10517           err_tag = out_iter->first;
10518
10519           //  Only pass on attributes that match in both inputs.
10520           if (!in_iter->second->matches(*(out_iter->second)))
10521             {
10522               // No match.  Delete the attribute.
10523               int saved_tag = out_iter->first;
10524               delete out_iter->second;
10525               out_other_attributes->erase(out_iter);
10526               out_iter = out_other_attributes->upper_bound(saved_tag);
10527             }
10528           else
10529             {
10530               // Matched.  Keep the attribute and move to the next.
10531               ++out_iter;
10532               ++in_iter;
10533             }
10534         }
10535
10536       if (err_object && parameters->options().warn_mismatch())
10537         {
10538           // Attribute numbers >=64 (mod 128) can be safely ignored.  */
10539           if ((err_tag & 127) < 64)
10540             {
10541               gold_error(_("%s: unknown mandatory EABI object attribute %d"),
10542                          err_object, err_tag);
10543             }
10544           else
10545             {
10546               gold_warning(_("%s: unknown EABI object attribute %d"),
10547                            err_object, err_tag);
10548             }
10549         }
10550     }
10551 }
10552
10553 // Stub-generation methods for Target_arm.
10554
10555 // Make a new Arm_input_section object.
10556
10557 template<bool big_endian>
10558 Arm_input_section<big_endian>*
10559 Target_arm<big_endian>::new_arm_input_section(
10560     Relobj* relobj,
10561     unsigned int shndx)
10562 {
10563   Section_id sid(relobj, shndx);
10564
10565   Arm_input_section<big_endian>* arm_input_section =
10566     new Arm_input_section<big_endian>(relobj, shndx);
10567   arm_input_section->init();
10568
10569   // Register new Arm_input_section in map for look-up.
10570   std::pair<typename Arm_input_section_map::iterator, bool> ins =
10571     this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
10572
10573   // Make sure that it we have not created another Arm_input_section
10574   // for this input section already.
10575   gold_assert(ins.second);
10576
10577   return arm_input_section; 
10578 }
10579
10580 // Find the Arm_input_section object corresponding to the SHNDX-th input
10581 // section of RELOBJ.
10582
10583 template<bool big_endian>
10584 Arm_input_section<big_endian>*
10585 Target_arm<big_endian>::find_arm_input_section(
10586     Relobj* relobj,
10587     unsigned int shndx) const
10588 {
10589   Section_id sid(relobj, shndx);
10590   typename Arm_input_section_map::const_iterator p =
10591     this->arm_input_section_map_.find(sid);
10592   return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
10593 }
10594
10595 // Make a new stub table.
10596
10597 template<bool big_endian>
10598 Stub_table<big_endian>*
10599 Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
10600 {
10601   Stub_table<big_endian>* stub_table =
10602     new Stub_table<big_endian>(owner);
10603   this->stub_tables_.push_back(stub_table);
10604
10605   stub_table->set_address(owner->address() + owner->data_size());
10606   stub_table->set_file_offset(owner->offset() + owner->data_size());
10607   stub_table->finalize_data_size();
10608
10609   return stub_table;
10610 }
10611
10612 // Scan a relocation for stub generation.
10613
10614 template<bool big_endian>
10615 void
10616 Target_arm<big_endian>::scan_reloc_for_stub(
10617     const Relocate_info<32, big_endian>* relinfo,
10618     unsigned int r_type,
10619     const Sized_symbol<32>* gsym,
10620     unsigned int r_sym,
10621     const Symbol_value<32>* psymval,
10622     elfcpp::Elf_types<32>::Elf_Swxword addend,
10623     Arm_address address)
10624 {
10625   typedef typename Target_arm<big_endian>::Relocate Relocate;
10626
10627   const Arm_relobj<big_endian>* arm_relobj =
10628     Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
10629
10630   bool target_is_thumb;
10631   Symbol_value<32> symval;
10632   if (gsym != NULL)
10633     {
10634       // This is a global symbol.  Determine if we use PLT and if the
10635       // final target is THUMB.
10636       if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
10637         {
10638           // This uses a PLT, change the symbol value.
10639           symval.set_output_value(this->plt_section()->address()
10640                                   + gsym->plt_offset());
10641           psymval = &symval;
10642           target_is_thumb = false;
10643         }
10644       else if (gsym->is_undefined())
10645         // There is no need to generate a stub symbol is undefined.
10646         return;
10647       else
10648         {
10649           target_is_thumb =
10650             ((gsym->type() == elfcpp::STT_ARM_TFUNC)
10651              || (gsym->type() == elfcpp::STT_FUNC
10652                  && !gsym->is_undefined()
10653                  && ((psymval->value(arm_relobj, 0) & 1) != 0)));
10654         }
10655     }
10656   else
10657     {
10658       // This is a local symbol.  Determine if the final target is THUMB.
10659       target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
10660     }
10661
10662   // Strip LSB if this points to a THUMB target.
10663   const Arm_reloc_property* reloc_property =
10664     arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
10665   gold_assert(reloc_property != NULL);
10666   if (target_is_thumb
10667       && reloc_property->uses_thumb_bit()
10668       && ((psymval->value(arm_relobj, 0) & 1) != 0))
10669     {
10670       Arm_address stripped_value =
10671         psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
10672       symval.set_output_value(stripped_value);
10673       psymval = &symval;
10674     } 
10675
10676   // Get the symbol value.
10677   Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
10678
10679   // Owing to pipelining, the PC relative branches below actually skip
10680   // two instructions when the branch offset is 0.
10681   Arm_address destination;
10682   switch (r_type)
10683     {
10684     case elfcpp::R_ARM_CALL:
10685     case elfcpp::R_ARM_JUMP24:
10686     case elfcpp::R_ARM_PLT32:
10687       // ARM branches.
10688       destination = value + addend + 8;
10689       break;
10690     case elfcpp::R_ARM_THM_CALL:
10691     case elfcpp::R_ARM_THM_XPC22:
10692     case elfcpp::R_ARM_THM_JUMP24:
10693     case elfcpp::R_ARM_THM_JUMP19:
10694       // THUMB branches.
10695       destination = value + addend + 4;
10696       break;
10697     default:
10698       gold_unreachable();
10699     }
10700
10701   Reloc_stub* stub = NULL;
10702   Stub_type stub_type =
10703     Reloc_stub::stub_type_for_reloc(r_type, address, destination,
10704                                     target_is_thumb);
10705   if (stub_type != arm_stub_none)
10706     {
10707       // Try looking up an existing stub from a stub table.
10708       Stub_table<big_endian>* stub_table = 
10709         arm_relobj->stub_table(relinfo->data_shndx);
10710       gold_assert(stub_table != NULL);
10711    
10712       // Locate stub by destination.
10713       Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
10714
10715       // Create a stub if there is not one already
10716       stub = stub_table->find_reloc_stub(stub_key);
10717       if (stub == NULL)
10718         {
10719           // create a new stub and add it to stub table.
10720           stub = this->stub_factory().make_reloc_stub(stub_type);
10721           stub_table->add_reloc_stub(stub, stub_key);
10722         }
10723
10724       // Record the destination address.
10725       stub->set_destination_address(destination
10726                                     | (target_is_thumb ? 1 : 0));
10727     }
10728
10729   // For Cortex-A8, we need to record a relocation at 4K page boundary.
10730   if (this->fix_cortex_a8_
10731       && (r_type == elfcpp::R_ARM_THM_JUMP24
10732           || r_type == elfcpp::R_ARM_THM_JUMP19
10733           || r_type == elfcpp::R_ARM_THM_CALL
10734           || r_type == elfcpp::R_ARM_THM_XPC22)
10735       && (address & 0xfffU) == 0xffeU)
10736     {
10737       // Found a candidate.  Note we haven't checked the destination is
10738       // within 4K here: if we do so (and don't create a record) we can't
10739       // tell that a branch should have been relocated when scanning later.
10740       this->cortex_a8_relocs_info_[address] =
10741         new Cortex_a8_reloc(stub, r_type,
10742                             destination | (target_is_thumb ? 1 : 0));
10743     }
10744 }
10745
10746 // This function scans a relocation sections for stub generation.
10747 // The template parameter Relocate must be a class type which provides
10748 // a single function, relocate(), which implements the machine
10749 // specific part of a relocation.
10750
10751 // BIG_ENDIAN is the endianness of the data.  SH_TYPE is the section type:
10752 // SHT_REL or SHT_RELA.
10753
10754 // PRELOCS points to the relocation data.  RELOC_COUNT is the number
10755 // of relocs.  OUTPUT_SECTION is the output section.
10756 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
10757 // mapped to output offsets.
10758
10759 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
10760 // VIEW_SIZE is the size.  These refer to the input section, unless
10761 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
10762 // the output section.
10763
10764 template<bool big_endian>
10765 template<int sh_type>
10766 void inline
10767 Target_arm<big_endian>::scan_reloc_section_for_stubs(
10768     const Relocate_info<32, big_endian>* relinfo,
10769     const unsigned char* prelocs,
10770     size_t reloc_count,
10771     Output_section* output_section,
10772     bool needs_special_offset_handling,
10773     const unsigned char* view,
10774     elfcpp::Elf_types<32>::Elf_Addr view_address,
10775     section_size_type)
10776 {
10777   typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
10778   const int reloc_size =
10779     Reloc_types<sh_type, 32, big_endian>::reloc_size;
10780
10781   Arm_relobj<big_endian>* arm_object =
10782     Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
10783   unsigned int local_count = arm_object->local_symbol_count();
10784
10785   Comdat_behavior comdat_behavior = CB_UNDETERMINED;
10786
10787   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
10788     {
10789       Reltype reloc(prelocs);
10790
10791       typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
10792       unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
10793       unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
10794
10795       r_type = this->get_real_reloc_type(r_type);
10796
10797       // Only a few relocation types need stubs.
10798       if ((r_type != elfcpp::R_ARM_CALL)
10799          && (r_type != elfcpp::R_ARM_JUMP24)
10800          && (r_type != elfcpp::R_ARM_PLT32)
10801          && (r_type != elfcpp::R_ARM_THM_CALL)
10802          && (r_type != elfcpp::R_ARM_THM_XPC22)
10803          && (r_type != elfcpp::R_ARM_THM_JUMP24)
10804          && (r_type != elfcpp::R_ARM_THM_JUMP19)
10805          && (r_type != elfcpp::R_ARM_V4BX))
10806         continue;
10807
10808       section_offset_type offset =
10809         convert_to_section_size_type(reloc.get_r_offset());
10810
10811       if (needs_special_offset_handling)
10812         {
10813           offset = output_section->output_offset(relinfo->object,
10814                                                  relinfo->data_shndx,
10815                                                  offset);
10816           if (offset == -1)
10817             continue;
10818         }
10819
10820       // Create a v4bx stub if --fix-v4bx-interworking is used.
10821       if (r_type == elfcpp::R_ARM_V4BX)
10822         {
10823           if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING)
10824             {
10825               // Get the BX instruction.
10826               typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10827               const Valtype* wv =
10828                 reinterpret_cast<const Valtype*>(view + offset);
10829               elfcpp::Elf_types<32>::Elf_Swxword insn =
10830                 elfcpp::Swap<32, big_endian>::readval(wv);
10831               const uint32_t reg = (insn & 0xf);
10832
10833               if (reg < 0xf)
10834                 {
10835                   // Try looking up an existing stub from a stub table.
10836                   Stub_table<big_endian>* stub_table =
10837                     arm_object->stub_table(relinfo->data_shndx);
10838                   gold_assert(stub_table != NULL);
10839
10840                   if (stub_table->find_arm_v4bx_stub(reg) == NULL)
10841                     {
10842                       // create a new stub and add it to stub table.
10843                       Arm_v4bx_stub* stub =
10844                         this->stub_factory().make_arm_v4bx_stub(reg);
10845                       gold_assert(stub != NULL);
10846                       stub_table->add_arm_v4bx_stub(stub);
10847                     }
10848                 }
10849             }
10850           continue;
10851         }
10852
10853       // Get the addend.
10854       Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
10855       elfcpp::Elf_types<32>::Elf_Swxword addend =
10856         stub_addend_reader(r_type, view + offset, reloc);
10857
10858       const Sized_symbol<32>* sym;
10859
10860       Symbol_value<32> symval;
10861       const Symbol_value<32> *psymval;
10862       if (r_sym < local_count)
10863         {
10864           sym = NULL;
10865           psymval = arm_object->local_symbol(r_sym);
10866
10867           // If the local symbol belongs to a section we are discarding,
10868           // and that section is a debug section, try to find the
10869           // corresponding kept section and map this symbol to its
10870           // counterpart in the kept section.  The symbol must not 
10871           // correspond to a section we are folding.
10872           bool is_ordinary;
10873           unsigned int shndx = psymval->input_shndx(&is_ordinary);
10874           if (is_ordinary
10875               && shndx != elfcpp::SHN_UNDEF
10876               && !arm_object->is_section_included(shndx) 
10877               && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
10878             {
10879               if (comdat_behavior == CB_UNDETERMINED)
10880                 {
10881                   std::string name =
10882                     arm_object->section_name(relinfo->data_shndx);
10883                   comdat_behavior = get_comdat_behavior(name.c_str());
10884                 }
10885               if (comdat_behavior == CB_PRETEND)
10886                 {
10887                   bool found;
10888                   typename elfcpp::Elf_types<32>::Elf_Addr value =
10889                     arm_object->map_to_kept_section(shndx, &found);
10890                   if (found)
10891                     symval.set_output_value(value + psymval->input_value());
10892                   else
10893                     symval.set_output_value(0);
10894                 }
10895               else
10896                 {
10897                   symval.set_output_value(0);
10898                 }
10899               symval.set_no_output_symtab_entry();
10900               psymval = &symval;
10901             }
10902         }
10903       else
10904         {
10905           const Symbol* gsym = arm_object->global_symbol(r_sym);
10906           gold_assert(gsym != NULL);
10907           if (gsym->is_forwarder())
10908             gsym = relinfo->symtab->resolve_forwards(gsym);
10909
10910           sym = static_cast<const Sized_symbol<32>*>(gsym);
10911           if (sym->has_symtab_index())
10912             symval.set_output_symtab_index(sym->symtab_index());
10913           else
10914             symval.set_no_output_symtab_entry();
10915
10916           // We need to compute the would-be final value of this global
10917           // symbol.
10918           const Symbol_table* symtab = relinfo->symtab;
10919           const Sized_symbol<32>* sized_symbol =
10920             symtab->get_sized_symbol<32>(gsym);
10921           Symbol_table::Compute_final_value_status status;
10922           Arm_address value =
10923             symtab->compute_final_value<32>(sized_symbol, &status);
10924
10925           // Skip this if the symbol has not output section.
10926           if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
10927             continue;
10928
10929           symval.set_output_value(value);
10930           psymval = &symval;
10931         }
10932
10933       // If symbol is a section symbol, we don't know the actual type of
10934       // destination.  Give up.
10935       if (psymval->is_section_symbol())
10936         continue;
10937
10938       this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
10939                                 addend, view_address + offset);
10940     }
10941 }
10942
10943 // Scan an input section for stub generation.
10944
10945 template<bool big_endian>
10946 void
10947 Target_arm<big_endian>::scan_section_for_stubs(
10948     const Relocate_info<32, big_endian>* relinfo,
10949     unsigned int sh_type,
10950     const unsigned char* prelocs,
10951     size_t reloc_count,
10952     Output_section* output_section,
10953     bool needs_special_offset_handling,
10954     const unsigned char* view,
10955     Arm_address view_address,
10956     section_size_type view_size)
10957 {
10958   if (sh_type == elfcpp::SHT_REL)
10959     this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
10960         relinfo,
10961         prelocs,
10962         reloc_count,
10963         output_section,
10964         needs_special_offset_handling,
10965         view,
10966         view_address,
10967         view_size);
10968   else if (sh_type == elfcpp::SHT_RELA)
10969     // We do not support RELA type relocations yet.  This is provided for
10970     // completeness.
10971     this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
10972         relinfo,
10973         prelocs,
10974         reloc_count,
10975         output_section,
10976         needs_special_offset_handling,
10977         view,
10978         view_address,
10979         view_size);
10980   else
10981     gold_unreachable();
10982 }
10983
10984 // Group input sections for stub generation.
10985 //
10986 // We goup input sections in an output sections so that the total size,
10987 // including any padding space due to alignment is smaller than GROUP_SIZE
10988 // unless the only input section in group is bigger than GROUP_SIZE already.
10989 // Then an ARM stub table is created to follow the last input section
10990 // in group.  For each group an ARM stub table is created an is placed
10991 // after the last group.  If STUB_ALWATS_AFTER_BRANCH is false, we further
10992 // extend the group after the stub table.
10993
10994 template<bool big_endian>
10995 void
10996 Target_arm<big_endian>::group_sections(
10997     Layout* layout,
10998     section_size_type group_size,
10999     bool stubs_always_after_branch)
11000 {
11001   // Group input sections and insert stub table
11002   Layout::Section_list section_list;
11003   layout->get_allocated_sections(&section_list);
11004   for (Layout::Section_list::const_iterator p = section_list.begin();
11005        p != section_list.end();
11006        ++p)
11007     {
11008       Arm_output_section<big_endian>* output_section =
11009         Arm_output_section<big_endian>::as_arm_output_section(*p);
11010       output_section->group_sections(group_size, stubs_always_after_branch,
11011                                      this);
11012     }
11013 }
11014
11015 // Relaxation hook.  This is where we do stub generation.
11016
11017 template<bool big_endian>
11018 bool
11019 Target_arm<big_endian>::do_relax(
11020     int pass,
11021     const Input_objects* input_objects,
11022     Symbol_table* symtab,
11023     Layout* layout)
11024 {
11025   // No need to generate stubs if this is a relocatable link.
11026   gold_assert(!parameters->options().relocatable());
11027
11028   // If this is the first pass, we need to group input sections into
11029   // stub groups.
11030   bool done_exidx_fixup = false;
11031   typedef typename Stub_table_list::iterator Stub_table_iterator;
11032   if (pass == 1)
11033     {
11034       // Determine the stub group size.  The group size is the absolute
11035       // value of the parameter --stub-group-size.  If --stub-group-size
11036       // is passed a negative value, we restict stubs to be always after
11037       // the stubbed branches.
11038       int32_t stub_group_size_param =
11039         parameters->options().stub_group_size();
11040       bool stubs_always_after_branch = stub_group_size_param < 0;
11041       section_size_type stub_group_size = abs(stub_group_size_param);
11042
11043       if (stub_group_size == 1)
11044         {
11045           // Default value.
11046           // Thumb branch range is +-4MB has to be used as the default
11047           // maximum size (a given section can contain both ARM and Thumb
11048           // code, so the worst case has to be taken into account).  If we are
11049           // fixing cortex-a8 errata, the branch range has to be even smaller,
11050           // since wide conditional branch has a range of +-1MB only.
11051           //
11052           // This value is 48K less than that, which allows for 4096
11053           // 12-byte stubs.  If we exceed that, then we will fail to link.
11054           // The user will have to relink with an explicit group size
11055           // option.
11056             stub_group_size = 4145152;
11057         }
11058
11059       // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
11060       // page as the first half of a 32-bit branch straddling two 4K pages.
11061       // This is a crude way of enforcing that.  In addition, long conditional
11062       // branches of THUMB-2 have a range of +-1M.  If we are fixing cortex-A8
11063       // erratum, limit the group size to  (1M - 12k) to avoid unreachable
11064       // cortex-A8 stubs from long conditional branches.
11065       if (this->fix_cortex_a8_)
11066         {
11067           stubs_always_after_branch = true;
11068           const section_size_type cortex_a8_group_size = 1024 * (1024 - 12);
11069           stub_group_size = std::max(stub_group_size, cortex_a8_group_size);
11070         }
11071
11072       group_sections(layout, stub_group_size, stubs_always_after_branch);
11073      
11074       // Also fix .ARM.exidx section coverage.
11075       Arm_output_section<big_endian>* exidx_output_section = NULL;
11076       for (Layout::Section_list::const_iterator p =
11077              layout->section_list().begin();
11078            p != layout->section_list().end();
11079            ++p)
11080         if ((*p)->type() == elfcpp::SHT_ARM_EXIDX)
11081           {
11082             if (exidx_output_section == NULL)
11083               exidx_output_section =
11084                 Arm_output_section<big_endian>::as_arm_output_section(*p);
11085             else
11086               // We cannot handle this now.
11087               gold_error(_("multiple SHT_ARM_EXIDX sections %s and %s in a "
11088                            "non-relocatable link"),
11089                           exidx_output_section->name(),
11090                           (*p)->name());
11091           }
11092
11093       if (exidx_output_section != NULL)
11094         {
11095           this->fix_exidx_coverage(layout, input_objects, exidx_output_section,
11096                                    symtab);
11097           done_exidx_fixup = true;
11098         }
11099     }
11100   else
11101     {
11102       // If this is not the first pass, addresses and file offsets have
11103       // been reset at this point, set them here.
11104       for (Stub_table_iterator sp = this->stub_tables_.begin();
11105            sp != this->stub_tables_.end();
11106            ++sp)
11107         {
11108           Arm_input_section<big_endian>* owner = (*sp)->owner();
11109           off_t off = align_address(owner->original_size(),
11110                                     (*sp)->addralign());
11111           (*sp)->set_address_and_file_offset(owner->address() + off,
11112                                              owner->offset() + off);
11113         }
11114     }
11115
11116   // The Cortex-A8 stubs are sensitive to layout of code sections.  At the
11117   // beginning of each relaxation pass, just blow away all the stubs.
11118   // Alternatively, we could selectively remove only the stubs and reloc
11119   // information for code sections that have moved since the last pass.
11120   // That would require more book-keeping.
11121   if (this->fix_cortex_a8_)
11122     {
11123       // Clear all Cortex-A8 reloc information.
11124       for (typename Cortex_a8_relocs_info::const_iterator p =
11125              this->cortex_a8_relocs_info_.begin();
11126            p != this->cortex_a8_relocs_info_.end();
11127            ++p)
11128         delete p->second;
11129       this->cortex_a8_relocs_info_.clear();
11130
11131       // Remove all Cortex-A8 stubs.
11132       for (Stub_table_iterator sp = this->stub_tables_.begin();
11133            sp != this->stub_tables_.end();
11134            ++sp)
11135         (*sp)->remove_all_cortex_a8_stubs();
11136     }
11137   
11138   // Scan relocs for relocation stubs
11139   for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
11140        op != input_objects->relobj_end();
11141        ++op)
11142     {
11143       Arm_relobj<big_endian>* arm_relobj =
11144         Arm_relobj<big_endian>::as_arm_relobj(*op);
11145       arm_relobj->scan_sections_for_stubs(this, symtab, layout);
11146     }
11147
11148   // Check all stub tables to see if any of them have their data sizes
11149   // or addresses alignments changed.  These are the only things that
11150   // matter.
11151   bool any_stub_table_changed = false;
11152   Unordered_set<const Output_section*> sections_needing_adjustment;
11153   for (Stub_table_iterator sp = this->stub_tables_.begin();
11154        (sp != this->stub_tables_.end()) && !any_stub_table_changed;
11155        ++sp)
11156     {
11157       if ((*sp)->update_data_size_and_addralign())
11158         {
11159           // Update data size of stub table owner.
11160           Arm_input_section<big_endian>* owner = (*sp)->owner();
11161           uint64_t address = owner->address();
11162           off_t offset = owner->offset();
11163           owner->reset_address_and_file_offset();
11164           owner->set_address_and_file_offset(address, offset);
11165
11166           sections_needing_adjustment.insert(owner->output_section());
11167           any_stub_table_changed = true;
11168         }
11169     }
11170
11171   // Output_section_data::output_section() returns a const pointer but we
11172   // need to update output sections, so we record all output sections needing
11173   // update above and scan the sections here to find out what sections need
11174   // to be updated.
11175   for(Layout::Section_list::const_iterator p = layout->section_list().begin();
11176       p != layout->section_list().end();
11177       ++p)
11178     {
11179       if (sections_needing_adjustment.find(*p)
11180           != sections_needing_adjustment.end())
11181         (*p)->set_section_offsets_need_adjustment();
11182     }
11183
11184   // Stop relaxation if no EXIDX fix-up and no stub table change.
11185   bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
11186
11187   // Finalize the stubs in the last relaxation pass.
11188   if (!continue_relaxation)
11189     {
11190       for (Stub_table_iterator sp = this->stub_tables_.begin();
11191            (sp != this->stub_tables_.end()) && !any_stub_table_changed;
11192             ++sp)
11193         (*sp)->finalize_stubs();
11194
11195       // Update output local symbol counts of objects if necessary.
11196       for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
11197            op != input_objects->relobj_end();
11198            ++op)
11199         {
11200           Arm_relobj<big_endian>* arm_relobj =
11201             Arm_relobj<big_endian>::as_arm_relobj(*op);
11202
11203           // Update output local symbol counts.  We need to discard local
11204           // symbols defined in parts of input sections that are discarded by
11205           // relaxation.
11206           if (arm_relobj->output_local_symbol_count_needs_update())
11207             arm_relobj->update_output_local_symbol_count();
11208         }
11209     }
11210
11211   return continue_relaxation;
11212 }
11213
11214 // Relocate a stub.
11215
11216 template<bool big_endian>
11217 void
11218 Target_arm<big_endian>::relocate_stub(
11219     Stub* stub,
11220     const Relocate_info<32, big_endian>* relinfo,
11221     Output_section* output_section,
11222     unsigned char* view,
11223     Arm_address address,
11224     section_size_type view_size)
11225 {
11226   Relocate relocate;
11227   const Stub_template* stub_template = stub->stub_template();
11228   for (size_t i = 0; i < stub_template->reloc_count(); i++)
11229     {
11230       size_t reloc_insn_index = stub_template->reloc_insn_index(i);
11231       const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
11232
11233       unsigned int r_type = insn->r_type();
11234       section_size_type reloc_offset = stub_template->reloc_offset(i);
11235       section_size_type reloc_size = insn->size();
11236       gold_assert(reloc_offset + reloc_size <= view_size);
11237
11238       // This is the address of the stub destination.
11239       Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
11240       Symbol_value<32> symval;
11241       symval.set_output_value(target);
11242
11243       // Synthesize a fake reloc just in case.  We don't have a symbol so
11244       // we use 0.
11245       unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
11246       memset(reloc_buffer, 0, sizeof(reloc_buffer));
11247       elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
11248       reloc_write.put_r_offset(reloc_offset);
11249       reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
11250       elfcpp::Rel<32, big_endian> rel(reloc_buffer);
11251
11252       relocate.relocate(relinfo, this, output_section,
11253                         this->fake_relnum_for_stubs, rel, r_type,
11254                         NULL, &symval, view + reloc_offset,
11255                         address + reloc_offset, reloc_size);
11256     }
11257 }
11258
11259 // Determine whether an object attribute tag takes an integer, a
11260 // string or both.
11261
11262 template<bool big_endian>
11263 int
11264 Target_arm<big_endian>::do_attribute_arg_type(int tag) const
11265 {
11266   if (tag == Object_attribute::Tag_compatibility)
11267     return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
11268             | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
11269   else if (tag == elfcpp::Tag_nodefaults)
11270     return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
11271             | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
11272   else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
11273     return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
11274   else if (tag < 32)
11275     return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
11276   else
11277     return ((tag & 1) != 0
11278             ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
11279             : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
11280 }
11281
11282 // Reorder attributes.
11283 //
11284 // The ABI defines that Tag_conformance should be emitted first, and that
11285 // Tag_nodefaults should be second (if either is defined).  This sets those
11286 // two positions, and bumps up the position of all the remaining tags to
11287 // compensate.
11288
11289 template<bool big_endian>
11290 int
11291 Target_arm<big_endian>::do_attributes_order(int num) const
11292 {
11293   // Reorder the known object attributes in output.  We want to move
11294   // Tag_conformance to position 4 and Tag_conformance to position 5
11295   // and shift eveything between 4 .. Tag_conformance - 1 to make room.
11296   if (num == 4)
11297     return elfcpp::Tag_conformance;
11298   if (num == 5)
11299     return elfcpp::Tag_nodefaults;
11300   if ((num - 2) < elfcpp::Tag_nodefaults)
11301     return num - 2;
11302   if ((num - 1) < elfcpp::Tag_conformance)
11303     return num - 1;
11304   return num;
11305 }
11306
11307 // Scan a span of THUMB code for Cortex-A8 erratum.
11308
11309 template<bool big_endian>
11310 void
11311 Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
11312     Arm_relobj<big_endian>* arm_relobj,
11313     unsigned int shndx,
11314     section_size_type span_start,
11315     section_size_type span_end,
11316     const unsigned char* view,
11317     Arm_address address)
11318 {
11319   // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
11320   //
11321   // The opcode is BLX.W, BL.W, B.W, Bcc.W
11322   // The branch target is in the same 4KB region as the
11323   // first half of the branch.
11324   // The instruction before the branch is a 32-bit
11325   // length non-branch instruction.
11326   section_size_type i = span_start;
11327   bool last_was_32bit = false;
11328   bool last_was_branch = false;
11329   while (i < span_end)
11330     {
11331       typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
11332       const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
11333       uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
11334       bool is_blx = false, is_b = false;
11335       bool is_bl = false, is_bcc = false;
11336
11337       bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
11338       if (insn_32bit)
11339         {
11340           // Load the rest of the insn (in manual-friendly order).
11341           insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
11342
11343           // Encoding T4: B<c>.W.
11344           is_b = (insn & 0xf800d000U) == 0xf0009000U;
11345           // Encoding T1: BL<c>.W.
11346           is_bl = (insn & 0xf800d000U) == 0xf000d000U;
11347           // Encoding T2: BLX<c>.W.
11348           is_blx = (insn & 0xf800d000U) == 0xf000c000U;
11349           // Encoding T3: B<c>.W (not permitted in IT block).
11350           is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
11351                     && (insn & 0x07f00000U) != 0x03800000U);
11352         }
11353
11354       bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
11355                            
11356       // If this instruction is a 32-bit THUMB branch that crosses a 4K
11357       // page boundary and it follows 32-bit non-branch instruction,
11358       // we need to work around.
11359       if (is_32bit_branch
11360           && ((address + i) & 0xfffU) == 0xffeU
11361           && last_was_32bit
11362           && !last_was_branch)
11363         {
11364           // Check to see if there is a relocation stub for this branch.
11365           bool force_target_arm = false;
11366           bool force_target_thumb = false;
11367           const Cortex_a8_reloc* cortex_a8_reloc = NULL;
11368           Cortex_a8_relocs_info::const_iterator p =
11369             this->cortex_a8_relocs_info_.find(address + i);
11370
11371           if (p != this->cortex_a8_relocs_info_.end())
11372             {
11373               cortex_a8_reloc = p->second;
11374               bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
11375
11376               if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
11377                   && !target_is_thumb)
11378                 force_target_arm = true;
11379               else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
11380                        && target_is_thumb)
11381                 force_target_thumb = true;
11382             }
11383
11384           off_t offset;
11385           Stub_type stub_type = arm_stub_none;
11386
11387           // Check if we have an offending branch instruction.
11388           uint16_t upper_insn = (insn >> 16) & 0xffffU;
11389           uint16_t lower_insn = insn & 0xffffU;
11390           typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
11391
11392           if (cortex_a8_reloc != NULL
11393               && cortex_a8_reloc->reloc_stub() != NULL)
11394             // We've already made a stub for this instruction, e.g.
11395             // it's a long branch or a Thumb->ARM stub.  Assume that
11396             // stub will suffice to work around the A8 erratum (see
11397             // setting of always_after_branch above).
11398             ;
11399           else if (is_bcc)
11400             {
11401               offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
11402                                                               lower_insn);
11403               stub_type = arm_stub_a8_veneer_b_cond;
11404             }
11405           else if (is_b || is_bl || is_blx)
11406             {
11407               offset = RelocFuncs::thumb32_branch_offset(upper_insn,
11408                                                          lower_insn);
11409               if (is_blx)
11410                 offset &= ~3;
11411
11412               stub_type = (is_blx
11413                            ? arm_stub_a8_veneer_blx
11414                            : (is_bl
11415                               ? arm_stub_a8_veneer_bl
11416                               : arm_stub_a8_veneer_b));
11417             }
11418
11419           if (stub_type != arm_stub_none)
11420             {
11421               Arm_address pc_for_insn = address + i + 4;
11422
11423               // The original instruction is a BL, but the target is
11424               // an ARM instruction.  If we were not making a stub,
11425               // the BL would have been converted to a BLX.  Use the
11426               // BLX stub instead in that case.
11427               if (this->may_use_blx() && force_target_arm
11428                   && stub_type == arm_stub_a8_veneer_bl)
11429                 {
11430                   stub_type = arm_stub_a8_veneer_blx;
11431                   is_blx = true;
11432                   is_bl = false;
11433                 }
11434               // Conversely, if the original instruction was
11435               // BLX but the target is Thumb mode, use the BL stub.
11436               else if (force_target_thumb
11437                        && stub_type == arm_stub_a8_veneer_blx)
11438                 {
11439                   stub_type = arm_stub_a8_veneer_bl;
11440                   is_blx = false;
11441                   is_bl = true;
11442                 }
11443
11444               if (is_blx)
11445                 pc_for_insn &= ~3;
11446
11447               // If we found a relocation, use the proper destination,
11448               // not the offset in the (unrelocated) instruction.
11449               // Note this is always done if we switched the stub type above.
11450               if (cortex_a8_reloc != NULL)
11451                 offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
11452
11453               Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
11454
11455               // Add a new stub if destination address in in the same page.
11456               if (((address + i) & ~0xfffU) == (target & ~0xfffU))
11457                 {
11458                   Cortex_a8_stub* stub =
11459                     this->stub_factory_.make_cortex_a8_stub(stub_type,
11460                                                             arm_relobj, shndx,
11461                                                             address + i,
11462                                                             target, insn);
11463                   Stub_table<big_endian>* stub_table =
11464                     arm_relobj->stub_table(shndx);
11465                   gold_assert(stub_table != NULL);
11466                   stub_table->add_cortex_a8_stub(address + i, stub);
11467                 }
11468             }
11469         }
11470
11471       i += insn_32bit ? 4 : 2;
11472       last_was_32bit = insn_32bit;
11473       last_was_branch = is_32bit_branch;
11474     }
11475 }
11476
11477 // Apply the Cortex-A8 workaround.
11478
11479 template<bool big_endian>
11480 void
11481 Target_arm<big_endian>::apply_cortex_a8_workaround(
11482     const Cortex_a8_stub* stub,
11483     Arm_address stub_address,
11484     unsigned char* insn_view,
11485     Arm_address insn_address)
11486 {
11487   typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
11488   Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
11489   Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
11490   Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
11491   off_t branch_offset = stub_address - (insn_address + 4);
11492
11493   typedef struct Arm_relocate_functions<big_endian> RelocFuncs;
11494   switch (stub->stub_template()->type())
11495     {
11496     case arm_stub_a8_veneer_b_cond:
11497       // For a conditional branch, we re-write it to be a uncondition
11498       // branch to the stub.  We use the THUMB-2 encoding here.
11499       upper_insn = 0xf000U;
11500       lower_insn = 0xb800U;
11501       // Fall through
11502     case arm_stub_a8_veneer_b:
11503     case arm_stub_a8_veneer_bl:
11504     case arm_stub_a8_veneer_blx:
11505       if ((lower_insn & 0x5000U) == 0x4000U)
11506         // For a BLX instruction, make sure that the relocation is
11507         // rounded up to a word boundary.  This follows the semantics of
11508         // the instruction which specifies that bit 1 of the target
11509         // address will come from bit 1 of the base address.
11510         branch_offset = (branch_offset + 2) & ~3;
11511
11512       // Put BRANCH_OFFSET back into the insn.
11513       gold_assert(!utils::has_overflow<25>(branch_offset));
11514       upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
11515       lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
11516       break;
11517
11518     default:
11519       gold_unreachable();
11520     }
11521
11522   // Put the relocated value back in the object file:
11523   elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
11524   elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
11525 }
11526
11527 template<bool big_endian>
11528 class Target_selector_arm : public Target_selector
11529 {
11530  public:
11531   Target_selector_arm()
11532     : Target_selector(elfcpp::EM_ARM, 32, big_endian,
11533                       (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
11534   { }
11535
11536   Target*
11537   do_instantiate_target()
11538   { return new Target_arm<big_endian>(); }
11539 };
11540
11541 // Fix .ARM.exidx section coverage.
11542
11543 template<bool big_endian>
11544 void
11545 Target_arm<big_endian>::fix_exidx_coverage(
11546     Layout* layout,
11547     const Input_objects* input_objects,
11548     Arm_output_section<big_endian>* exidx_section,
11549     Symbol_table* symtab)
11550 {
11551   // We need to look at all the input sections in output in ascending
11552   // order of of output address.  We do that by building a sorted list
11553   // of output sections by addresses.  Then we looks at the output sections
11554   // in order.  The input sections in an output section are already sorted
11555   // by addresses within the output section.
11556
11557   typedef std::set<Output_section*, output_section_address_less_than>
11558       Sorted_output_section_list;
11559   Sorted_output_section_list sorted_output_sections;
11560
11561   // Find out all the output sections of input sections pointed by
11562   // EXIDX input sections.
11563   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
11564        p != input_objects->relobj_end();
11565        ++p)
11566     {
11567       Arm_relobj<big_endian>* arm_relobj =
11568         Arm_relobj<big_endian>::as_arm_relobj(*p);
11569       std::vector<unsigned int> shndx_list;
11570       arm_relobj->get_exidx_shndx_list(&shndx_list);
11571       for (size_t i = 0; i < shndx_list.size(); ++i)
11572         {
11573           const Arm_exidx_input_section* exidx_input_section =
11574             arm_relobj->exidx_input_section_by_shndx(shndx_list[i]);
11575           gold_assert(exidx_input_section != NULL);
11576           if (!exidx_input_section->has_errors())
11577             {
11578               unsigned int text_shndx = exidx_input_section->link();
11579               Output_section *os = arm_relobj->output_section(text_shndx);
11580               if (os != NULL && (os->flags() & elfcpp::SHF_ALLOC) != 0)
11581                 sorted_output_sections.insert(os);
11582             }
11583         }
11584     }
11585
11586   // Go over the output sections in ascending order of output addresses.
11587   typedef typename Arm_output_section<big_endian>::Text_section_list
11588       Text_section_list;
11589   Text_section_list sorted_text_sections;
11590   for(typename Sorted_output_section_list::iterator p =
11591         sorted_output_sections.begin();
11592       p != sorted_output_sections.end();
11593       ++p)
11594     {
11595       Arm_output_section<big_endian>* arm_output_section =
11596         Arm_output_section<big_endian>::as_arm_output_section(*p);
11597       arm_output_section->append_text_sections_to_list(&sorted_text_sections);
11598     } 
11599
11600   exidx_section->fix_exidx_coverage(layout, sorted_text_sections, symtab,
11601                                     merge_exidx_entries());
11602 }
11603
11604 Target_selector_arm<false> target_selector_arm;
11605 Target_selector_arm<true> target_selector_armbe;
11606
11607 } // End anonymous namespace.