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