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