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