Fix for gold linking tlsdesc into an executable with -pie.
[external/binutils.git] / gold / aarch64.cc
1 // aarch64.cc -- aarch64 target support for gold.
2
3 // Copyright (C) 2014 Free Software Foundation, Inc.
4 // Written by Jing Yu <jingyu@google.com> and Han Shen <shenhan@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstring>
26
27 #include "elfcpp.h"
28 #include "dwarf.h"
29 #include "parameters.h"
30 #include "reloc.h"
31 #include "aarch64.h"
32 #include "object.h"
33 #include "symtab.h"
34 #include "layout.h"
35 #include "output.h"
36 #include "copy-relocs.h"
37 #include "target.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
40 #include "tls.h"
41 #include "freebsd.h"
42 #include "nacl.h"
43 #include "gc.h"
44 #include "icf.h"
45 #include "aarch64-reloc-property.h"
46
47 // The first three .got.plt entries are reserved.
48 const int32_t AARCH64_GOTPLT_RESERVE_COUNT = 3;
49
50
51 namespace
52 {
53
54 using namespace gold;
55
56 template<int size, bool big_endian>
57 class Output_data_plt_aarch64;
58
59 template<int size, bool big_endian>
60 class Output_data_plt_aarch64_standard;
61
62 template<int size, bool big_endian>
63 class Target_aarch64;
64
65 template<int size, bool big_endian>
66 class AArch64_relocate_functions;
67
68 // Output_data_got_aarch64 class.
69
70 template<int size, bool big_endian>
71 class Output_data_got_aarch64 : public Output_data_got<size, big_endian>
72 {
73  public:
74   typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
75   Output_data_got_aarch64(Symbol_table* symtab, Layout* layout)
76     : Output_data_got<size, big_endian>(),
77       symbol_table_(symtab), layout_(layout)
78   { }
79
80   // Add a static entry for the GOT entry at OFFSET.  GSYM is a global
81   // symbol and R_TYPE is the code of a dynamic relocation that needs to be
82   // applied in a static link.
83   void
84   add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
85   { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
86
87
88   // Add a static reloc for the GOT entry at OFFSET.  RELOBJ is an object
89   // defining a local symbol with INDEX.  R_TYPE is the code of a dynamic
90   // relocation that needs to be applied in a static link.
91   void
92   add_static_reloc(unsigned int got_offset, unsigned int r_type,
93                    Sized_relobj_file<size, big_endian>* relobj,
94                    unsigned int index)
95   {
96     this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
97                                                 index));
98   }
99
100
101  protected:
102   // Write out the GOT table.
103   void
104   do_write(Output_file* of) {
105     // The first entry in the GOT is the address of the .dynamic section.
106     gold_assert(this->data_size() >= size / 8);
107     Output_section* dynamic = this->layout_->dynamic_section();
108     Valtype dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
109     this->replace_constant(0, dynamic_addr);
110     Output_data_got<size, big_endian>::do_write(of);
111
112     // Handling static relocs
113     if (this->static_relocs_.empty())
114       return;
115
116     typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
117
118     gold_assert(parameters->doing_static_link());
119     const off_t offset = this->offset();
120     const section_size_type oview_size =
121       convert_to_section_size_type(this->data_size());
122     unsigned char* const oview = of->get_output_view(offset, oview_size);
123
124     Output_segment* tls_segment = this->layout_->tls_segment();
125     gold_assert(tls_segment != NULL);
126
127     AArch64_address aligned_tcb_address =
128       align_address(Target_aarch64<size, big_endian>::TCB_SIZE,
129                     tls_segment->maximum_alignment());
130
131     for (size_t i = 0; i < this->static_relocs_.size(); ++i)
132       {
133         Static_reloc& reloc(this->static_relocs_[i]);
134         AArch64_address value;
135
136         if (!reloc.symbol_is_global())
137           {
138             Sized_relobj_file<size, big_endian>* object = reloc.relobj();
139             const Symbol_value<size>* psymval =
140               reloc.relobj()->local_symbol(reloc.index());
141
142             // We are doing static linking.  Issue an error and skip this
143             // relocation if the symbol is undefined or in a discarded_section.
144             bool is_ordinary;
145             unsigned int shndx = psymval->input_shndx(&is_ordinary);
146             if ((shndx == elfcpp::SHN_UNDEF)
147                 || (is_ordinary
148                     && shndx != elfcpp::SHN_UNDEF
149                     && !object->is_section_included(shndx)
150                     && !this->symbol_table_->is_section_folded(object, shndx)))
151               {
152                 gold_error(_("undefined or discarded local symbol %u from "
153                              " object %s in GOT"),
154                            reloc.index(), reloc.relobj()->name().c_str());
155                 continue;
156               }
157             value = psymval->value(object, 0);
158           }
159         else
160           {
161             const Symbol* gsym = reloc.symbol();
162             gold_assert(gsym != NULL);
163             if (gsym->is_forwarder())
164               gsym = this->symbol_table_->resolve_forwards(gsym);
165
166             // We are doing static linking.  Issue an error and skip this
167             // relocation if the symbol is undefined or in a discarded_section
168             // unless it is a weakly_undefined symbol.
169             if ((gsym->is_defined_in_discarded_section()
170                  || gsym->is_undefined())
171                 && !gsym->is_weak_undefined())
172               {
173                 gold_error(_("undefined or discarded symbol %s in GOT"),
174                            gsym->name());
175                 continue;
176               }
177
178             if (!gsym->is_weak_undefined())
179               {
180                 const Sized_symbol<size>* sym =
181                   static_cast<const Sized_symbol<size>*>(gsym);
182                 value = sym->value();
183               }
184             else
185               value = 0;
186           }
187
188         unsigned got_offset = reloc.got_offset();
189         gold_assert(got_offset < oview_size);
190
191         typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
192         Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
193         Valtype x;
194         switch (reloc.r_type())
195           {
196           case elfcpp::R_AARCH64_TLS_DTPREL64:
197             x = value;
198             break;
199           case elfcpp::R_AARCH64_TLS_TPREL64:
200             x = value + aligned_tcb_address;
201             break;
202           default:
203             gold_unreachable();
204           }
205         elfcpp::Swap<size, big_endian>::writeval(wv, x);
206       }
207
208     of->write_output_view(offset, oview_size, oview);
209   }
210
211  private:
212   // Symbol table of the output object.
213   Symbol_table* symbol_table_;
214   // A pointer to the Layout class, so that we can find the .dynamic
215   // section when we write out the GOT section.
216   Layout* layout_;
217
218   // This class represent dynamic relocations that need to be applied by
219   // gold because we are using TLS relocations in a static link.
220   class Static_reloc
221   {
222    public:
223     Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
224       : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
225     { this->u_.global.symbol = gsym; }
226
227     Static_reloc(unsigned int got_offset, unsigned int r_type,
228           Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
229       : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
230     {
231       this->u_.local.relobj = relobj;
232       this->u_.local.index = index;
233     }
234
235     // Return the GOT offset.
236     unsigned int
237     got_offset() const
238     { return this->got_offset_; }
239
240     // Relocation type.
241     unsigned int
242     r_type() const
243     { return this->r_type_; }
244
245     // Whether the symbol is global or not.
246     bool
247     symbol_is_global() const
248     { return this->symbol_is_global_; }
249
250     // For a relocation against a global symbol, the global symbol.
251     Symbol*
252     symbol() const
253     {
254       gold_assert(this->symbol_is_global_);
255       return this->u_.global.symbol;
256     }
257
258     // For a relocation against a local symbol, the defining object.
259     Sized_relobj_file<size, big_endian>*
260     relobj() const
261     {
262       gold_assert(!this->symbol_is_global_);
263       return this->u_.local.relobj;
264     }
265
266     // For a relocation against a local symbol, the local symbol index.
267     unsigned int
268     index() const
269     {
270       gold_assert(!this->symbol_is_global_);
271       return this->u_.local.index;
272     }
273
274    private:
275     // GOT offset of the entry to which this relocation is applied.
276     unsigned int got_offset_;
277     // Type of relocation.
278     unsigned int r_type_;
279     // Whether this relocation is against a global symbol.
280     bool symbol_is_global_;
281     // A global or local symbol.
282     union
283     {
284       struct
285       {
286         // For a global symbol, the symbol itself.
287         Symbol* symbol;
288       } global;
289       struct
290       {
291         // For a local symbol, the object defining the symbol.
292         Sized_relobj_file<size, big_endian>* relobj;
293         // For a local symbol, the symbol index.
294         unsigned int index;
295       } local;
296     } u_;
297   };  // End of inner class Static_reloc
298
299   std::vector<Static_reloc> static_relocs_;
300 };  // End of Output_data_got_aarch64
301
302
303 template<int size, bool big_endian>
304 class AArch64_input_section;
305
306
307 template<int size, bool big_endian>
308 class AArch64_output_section;
309
310
311 // Reloc stub class.
312
313 template<int size, bool big_endian>
314 class Reloc_stub
315 {
316  public:
317   typedef Reloc_stub<size, big_endian> This;
318   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
319
320   // Do not change the value of the enums, they are used to index into
321   // stub_insns array.
322   typedef enum
323   {
324     ST_NONE = 0,
325
326     // Using adrp/add pair, 4 insns (including alignment) without mem access,
327     // the fastest stub. This has a limited jump distance, which is tested by
328     // aarch64_valid_for_adrp_p.
329     ST_ADRP_BRANCH = 1,
330
331     // Using ldr-absolute-address/br-register, 4 insns with 1 mem access,
332     // unlimited in jump distance.
333     ST_LONG_BRANCH_ABS = 2,
334
335     // Using ldr/calculate-pcrel/jump, 8 insns (including alignment) with 1 mem
336     // access, slowest one. Only used in position independent executables.
337     ST_LONG_BRANCH_PCREL = 3,
338
339   } Stub_type;
340
341   // Branch range. This is used to calculate the section group size, as well as
342   // determine whether a stub is needed.
343   static const int MAX_BRANCH_OFFSET = ((1 << 25) - 1) << 2;
344   static const int MIN_BRANCH_OFFSET = -((1 << 25) << 2);
345
346   // Constant used to determine if an offset fits in the adrp instruction
347   // encoding.
348   static const int MAX_ADRP_IMM = (1 << 20) - 1;
349   static const int MIN_ADRP_IMM = -(1 << 20);
350
351   static const int BYTES_PER_INSN = 4;
352   static const int STUB_ADDR_ALIGN = 4;
353
354   // Determine whether the offset fits in the jump/branch instruction.
355   static bool
356   aarch64_valid_branch_offset_p(int64_t offset)
357   { return offset >= MIN_BRANCH_OFFSET && offset <= MAX_BRANCH_OFFSET; }
358
359   // Determine whether the offset fits in the adrp immediate field.
360   static bool
361   aarch64_valid_for_adrp_p(AArch64_address location, AArch64_address dest)
362   {
363     typedef AArch64_relocate_functions<size, big_endian> Reloc;
364     int64_t adrp_imm = (Reloc::Page(dest) - Reloc::Page(location)) >> 12;
365     return adrp_imm >= MIN_ADRP_IMM && adrp_imm <= MAX_ADRP_IMM;
366   }
367
368   // Determine the stub type for a certain relocation or ST_NONE, if no stub is
369   // needed.
370   static Stub_type
371   stub_type_for_reloc(unsigned int r_type, AArch64_address address,
372                       AArch64_address target);
373
374   Reloc_stub(Stub_type stub_type)
375     : stub_type_(stub_type), offset_(invalid_offset),
376       destination_address_(invalid_address)
377   { }
378
379   ~Reloc_stub()
380   { }
381
382   // Return offset of code stub from beginning of its containing stub table.
383   section_offset_type
384   offset() const
385   {
386     gold_assert(this->offset_ != invalid_offset);
387     return this->offset_;
388   }
389
390   // Set offset of code stub from beginning of its containing stub table.
391   void
392   set_offset(section_offset_type offset)
393   { this->offset_ = offset; }
394
395   // Return destination address.
396   AArch64_address
397   destination_address() const
398   {
399     gold_assert(this->destination_address_ != this->invalid_address);
400     return this->destination_address_;
401   }
402
403   // Set destination address.
404   void
405   set_destination_address(AArch64_address address)
406   {
407     gold_assert(address != this->invalid_address);
408     this->destination_address_ = address;
409   }
410
411   // Reset the destination address.
412   void
413   reset_destination_address()
414   { this->destination_address_ = this->invalid_address; }
415
416   // Return the stub type.
417   Stub_type
418   stub_type() const
419   { return stub_type_; }
420
421   // Return the stub size.
422   uint32_t
423   stub_size() const
424   { return this->stub_insn_number() * BYTES_PER_INSN; }
425
426   // Return the instruction number of this stub instance.
427   int
428   stub_insn_number() const
429   { return stub_insns_[this->stub_type_][0]; }
430
431   // Note the first "insn" is the number of total insns in this array.
432   const uint32_t*
433   stub_insns() const
434   { return stub_insns_[this->stub_type_]; }
435
436   // Write stub to output file.
437   void
438   write(unsigned char* view, section_size_type view_size)
439   { this->do_write(view, view_size); }
440
441   // The key class used to index the stub instance in the stub table's stub map.
442   class Key
443   {
444    public:
445     Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
446         unsigned int r_sym, int32_t addend)
447       : stub_type_(stub_type), addend_(addend)
448     {
449       if (symbol != NULL)
450         {
451           this->r_sym_ = Reloc_stub::invalid_index;
452           this->u_.symbol = symbol;
453         }
454       else
455         {
456           gold_assert(relobj != NULL && r_sym != invalid_index);
457           this->r_sym_ = r_sym;
458           this->u_.relobj = relobj;
459         }
460     }
461
462     ~Key()
463     { }
464
465     // Return stub type.
466     Stub_type
467     stub_type() const
468     { return this->stub_type_; }
469
470     // Return the local symbol index or invalid_index.
471     unsigned int
472     r_sym() const
473     { return this->r_sym_; }
474
475     // Return the symbol if there is one.
476     const Symbol*
477     symbol() const
478     { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
479
480     // Return the relobj if there is one.
481     const Relobj*
482     relobj() const
483     { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
484
485     // Whether this equals to another key k.
486     bool
487     eq(const Key& k) const
488     {
489       return ((this->stub_type_ == k.stub_type_)
490               && (this->r_sym_ == k.r_sym_)
491               && ((this->r_sym_ != Reloc_stub::invalid_index)
492                   ? (this->u_.relobj == k.u_.relobj)
493                   : (this->u_.symbol == k.u_.symbol))
494               && (this->addend_ == k.addend_));
495     }
496
497     // Return a hash value.
498     size_t
499     hash_value() const
500     {
501       size_t name_hash_value = gold::string_hash<char>(
502           (this->r_sym_ != Reloc_stub::invalid_index)
503           ? this->u_.relobj->name().c_str()
504           : this->u_.symbol->name());
505       // We only have 4 stub types.
506       size_t stub_type_hash_value = 0x03 & this->stub_type_;
507       return (name_hash_value
508               ^ stub_type_hash_value
509               ^ ((this->r_sym_ & 0x3fff) << 2)
510               ^ ((this->addend_ & 0xffff) << 16));
511     }
512
513     // Functors for STL associative containers.
514     struct hash
515     {
516       size_t
517       operator()(const Key& k) const
518       { return k.hash_value(); }
519     };
520
521     struct equal_to
522     {
523       bool
524       operator()(const Key& k1, const Key& k2) const
525       { return k1.eq(k2); }
526     };
527
528    private:
529     // Stub type.
530     const Stub_type stub_type_;
531     // If this is a local symbol, this is the index in the defining object.
532     // Otherwise, it is invalid_index for a global symbol.
533     unsigned int r_sym_;
534     // If r_sym_ is an invalid index, this points to a global symbol.
535     // Otherwise, it points to a relobj.  We used the unsized and target
536     // independent Symbol and Relobj classes instead of Sized_symbol<32> and
537     // Arm_relobj, in order to avoid making the stub class a template
538     // as most of the stub machinery is endianness-neutral.  However, it
539     // may require a bit of casting done by users of this class.
540     union
541     {
542       const Symbol* symbol;
543       const Relobj* relobj;
544     } u_;
545     // Addend associated with a reloc.
546     int32_t addend_;
547   };  // End of inner class Reloc_stub::Key
548
549  protected:
550   // This may be overridden in the child class.
551   virtual void
552   do_write(unsigned char*, section_size_type);
553
554  private:
555   static const section_offset_type invalid_offset =
556       static_cast<section_offset_type>(-1);
557   static const unsigned int invalid_index = static_cast<unsigned int>(-1);
558   static const AArch64_address invalid_address =
559       static_cast<AArch64_address>(-1);
560
561   static const uint32_t stub_insns_[][10];
562
563   const Stub_type stub_type_;
564   section_offset_type offset_;
565   AArch64_address destination_address_;
566 };  // End of Reloc_stub
567
568
569 // Write data to output file.
570
571 template<int size, bool big_endian>
572 void
573 Reloc_stub<size, big_endian>::
574 do_write(unsigned char* view, section_size_type)
575 {
576   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
577   const uint32_t* insns = this->stub_insns();
578   uint32_t num_insns = this->stub_insn_number();
579   Insntype* ip = reinterpret_cast<Insntype*>(view);
580   for (uint32_t i = 1; i <= num_insns; ++i)
581     elfcpp::Swap<32, big_endian>::writeval(ip + i - 1, insns[i]);
582 }
583
584
585 // Stubs instructions definition.
586
587 template<int size, bool big_endian>
588 const uint32_t
589 Reloc_stub<size, big_endian>::stub_insns_[][10] =
590   {
591     // The first element of each group is the num of the insns.
592
593     // ST_NONE
594     {0, 0},
595
596     // ST_ADRP_BRANCH
597     {
598         4,
599         0x90000010,     /*      adrp    ip0, X             */
600                         /*        ADR_PREL_PG_HI21(X)      */
601         0x91000210,     /*      add     ip0, ip0, :lo12:X  */
602                         /*        ADD_ABS_LO12_NC(X)       */
603         0xd61f0200,     /*      br      ip0                */
604         0x00000000,     /*      alignment padding          */
605     },
606
607     // ST_LONG_BRANCH_ABS
608     {
609         4,
610         0x58000050,     /*      ldr   ip0, 0x8             */
611         0xd61f0200,     /*      br    ip0                  */
612         0x00000000,     /*      address field              */
613         0x00000000,     /*      address fields             */
614     },
615
616     // ST_LONG_BRANCH_PCREL
617     {
618       8,
619         0x58000090,     /*      ldr   ip0, 0x10            */
620         0x10000011,     /*      adr   ip1, #0              */
621         0x8b110210,     /*      add   ip0, ip0, ip1        */
622         0xd61f0200,     /*      br    ip0                  */
623         0x00000000,     /*      address field              */
624         0x00000000,     /*      address field              */
625         0x00000000,     /*      alignment padding          */
626         0x00000000,     /*      alignment padding          */
627     }
628   };
629
630
631 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
632 // needed.
633
634 template<int size, bool big_endian>
635 inline
636 typename Reloc_stub<size, big_endian>::Stub_type
637 Reloc_stub<size, big_endian>::stub_type_for_reloc(
638     unsigned int r_type, AArch64_address location, AArch64_address dest)
639 {
640   int64_t branch_offset = 0;
641   switch(r_type)
642     {
643     case elfcpp::R_AARCH64_CALL26:
644     case elfcpp::R_AARCH64_JUMP26:
645       branch_offset = dest - location;
646       break;
647     default:
648       gold_unreachable();
649     }
650
651   if (aarch64_valid_branch_offset_p(branch_offset))
652     return ST_NONE;
653
654   if (aarch64_valid_for_adrp_p(location, dest))
655     return ST_ADRP_BRANCH;
656
657   if (parameters->options().output_is_position_independent()
658       && parameters->options().output_is_executable())
659     return ST_LONG_BRANCH_PCREL;
660
661   return ST_LONG_BRANCH_ABS;
662 }
663
664 // A class to hold stubs for the ARM target.
665
666 template<int size, bool big_endian>
667 class Stub_table : public Output_data
668 {
669  public:
670   typedef Target_aarch64<size, big_endian> The_target_aarch64;
671   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
672   typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
673   typedef Reloc_stub<size, big_endian> The_reloc_stub;
674   typedef typename The_reloc_stub::Key The_reloc_stub_key;
675   typedef typename The_reloc_stub_key::hash The_reloc_stub_key_hash;
676   typedef typename The_reloc_stub_key::equal_to The_reloc_stub_key_equal_to;
677   typedef Stub_table<size, big_endian> The_stub_table;
678   typedef Unordered_map<The_reloc_stub_key, The_reloc_stub*,
679                         The_reloc_stub_key_hash, The_reloc_stub_key_equal_to>
680                         Reloc_stub_map;
681   typedef typename Reloc_stub_map::const_iterator Reloc_stub_map_const_iter;
682   typedef Relocate_info<size, big_endian> The_relocate_info;
683
684   Stub_table(The_aarch64_input_section* owner)
685     : Output_data(), owner_(owner), reloc_stubs_size_(0), prev_data_size_(0)
686   { }
687
688   ~Stub_table()
689   { }
690
691   The_aarch64_input_section*
692   owner() const
693   { return owner_; }
694
695   // Whether this stub table is empty.
696   bool
697   empty() const
698   { return reloc_stubs_.empty(); }
699
700   // Return the current data size.
701   off_t
702   current_data_size() const
703   { return this->current_data_size_for_child(); }
704
705   // Add a STUB using KEY.  The caller is responsible for avoiding addition
706   // if a STUB with the same key has already been added.
707   void
708   add_reloc_stub(The_reloc_stub* stub, const The_reloc_stub_key& key);
709
710   // Finalize stubs. No-op here, just for completeness.
711   void
712   finalize_stubs()
713   { }
714
715   // Look up a relocation stub using KEY. Return NULL if there is none.
716   The_reloc_stub*
717   find_reloc_stub(The_reloc_stub_key& key)
718   {
719     Reloc_stub_map_const_iter p = this->reloc_stubs_.find(key);
720     return (p != this->reloc_stubs_.end()) ? p->second : NULL;
721   }
722
723   // Relocate stubs in this stub table.
724   void
725   relocate_stubs(const The_relocate_info*,
726                  The_target_aarch64*,
727                  Output_section*,
728                  unsigned char*,
729                  AArch64_address,
730                  section_size_type);
731
732   // Update data size at the end of a relaxation pass.  Return true if data size
733   // is different from that of the previous relaxation pass.
734   bool
735   update_data_size_changed_p()
736   {
737     // No addralign changed here.
738     off_t s = this->reloc_stubs_size_;
739     bool changed = (s != this->prev_data_size_);
740     this->prev_data_size_ = s;
741     return changed;
742   }
743
744  protected:
745   // Write out section contents.
746   void
747   do_write(Output_file*);
748
749   // Return the required alignment.
750   uint64_t
751   do_addralign() const
752   { return The_reloc_stub::STUB_ADDR_ALIGN; }
753
754   // Reset address and file offset.
755   void
756   do_reset_address_and_file_offset()
757   { this->set_current_data_size_for_child(this->prev_data_size_); }
758
759   // Set final data size.
760   void
761   set_final_data_size()
762   { this->set_data_size(this->current_data_size()); }
763
764  private:
765   // Relocate one stub.
766   void
767   relocate_stub(The_reloc_stub*,
768                 const The_relocate_info*,
769                 The_target_aarch64*,
770                 Output_section*,
771                 unsigned char*,
772                 AArch64_address,
773                 section_size_type);
774
775  private:
776   // Owner of this stub table.
777   The_aarch64_input_section* owner_;
778   // The relocation stubs.
779   Reloc_stub_map reloc_stubs_;
780   // Size of reloc stubs.
781   off_t reloc_stubs_size_;
782   // data size of this in the previous pass.
783   off_t prev_data_size_;
784 };  // End of Stub_table
785
786
787 // Add a STUB using KEY.  The caller is responsible for avoiding addition
788 // if a STUB with the same key has already been added.
789
790 template<int size, bool big_endian>
791 void
792 Stub_table<size, big_endian>::add_reloc_stub(
793     The_reloc_stub* stub, const The_reloc_stub_key& key)
794 {
795   gold_assert(stub->stub_type() == key.stub_type());
796   this->reloc_stubs_[key] = stub;
797
798   // Assign stub offset early.  We can do this because we never remove
799   // reloc stubs and they are in the beginning of the stub table.
800   this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_,
801                                           The_reloc_stub::STUB_ADDR_ALIGN);
802   stub->set_offset(this->reloc_stubs_size_);
803   this->reloc_stubs_size_ += stub->stub_size();
804 }
805
806
807 // Relocate all stubs in this stub table.
808
809 template<int size, bool big_endian>
810 void
811 Stub_table<size, big_endian>::
812 relocate_stubs(const The_relocate_info* relinfo,
813                The_target_aarch64* target_aarch64,
814                Output_section* output_section,
815                unsigned char* view,
816                AArch64_address address,
817                section_size_type view_size)
818 {
819   // "view_size" is the total size of the stub_table.
820   gold_assert(address == this->address() &&
821               view_size == static_cast<section_size_type>(this->data_size()));
822   for(Reloc_stub_map_const_iter p = this->reloc_stubs_.begin();
823       p != this->reloc_stubs_.end(); ++p)
824     relocate_stub(p->second, relinfo, target_aarch64, output_section,
825                   view, address, view_size);
826 }
827
828
829 // Relocate one stub.  This is a helper for Stub_table::relocate_stubs().
830
831 template<int size, bool big_endian>
832 void
833 Stub_table<size, big_endian>::
834 relocate_stub(The_reloc_stub* stub,
835               const The_relocate_info* relinfo,
836               The_target_aarch64* target_aarch64,
837               Output_section* output_section,
838               unsigned char* view,
839               AArch64_address address,
840               section_size_type view_size)
841 {
842   // "offset" is the offset from the beginning of the stub_table.
843   section_size_type offset = stub->offset();
844   section_size_type stub_size = stub->stub_size();
845   // "view_size" is the total size of the stub_table.
846   gold_assert(offset + stub_size <= view_size);
847
848   target_aarch64->relocate_stub(stub, relinfo, output_section,
849                                 view + offset, address + offset, view_size);
850 }
851
852
853 // Write out the stubs to file.
854
855 template<int size, bool big_endian>
856 void
857 Stub_table<size, big_endian>::do_write(Output_file* of)
858 {
859   off_t offset = this->offset();
860   const section_size_type oview_size =
861     convert_to_section_size_type(this->data_size());
862   unsigned char* const oview = of->get_output_view(offset, oview_size);
863
864   // Write relocation stubs.
865   for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
866       p != this->reloc_stubs_.end(); ++p)
867     {
868       The_reloc_stub* stub = p->second;
869       AArch64_address address = this->address() + stub->offset();
870       gold_assert(address ==
871                   align_address(address, The_reloc_stub::STUB_ADDR_ALIGN));
872       stub->write(oview + stub->offset(), stub->stub_size());
873     }
874
875   of->write_output_view(this->offset(), oview_size, oview);
876 }
877
878
879 // AArch64_relobj class.
880
881 template<int size, bool big_endian>
882 class AArch64_relobj : public Sized_relobj_file<size, big_endian>
883 {
884  public:
885   typedef AArch64_relobj<size, big_endian> This;
886   typedef Target_aarch64<size, big_endian> The_target_aarch64;
887   typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
888   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
889   typedef Stub_table<size, big_endian> The_stub_table;
890   typedef std::vector<The_stub_table*> Stub_table_list;
891   static const AArch64_address invalid_address =
892       static_cast<AArch64_address>(-1);
893
894   AArch64_relobj(const std::string& name, Input_file* input_file, off_t offset,
895                  const typename elfcpp::Ehdr<size, big_endian>& ehdr)
896     : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
897       stub_tables_()
898   { }
899
900   ~AArch64_relobj()
901   { }
902
903   // Return the stub table of the SHNDX-th section if there is one.
904   The_stub_table*
905   stub_table(unsigned int shndx) const
906   {
907     gold_assert(shndx < this->stub_tables_.size());
908     return this->stub_tables_[shndx];
909   }
910
911   // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
912   void
913   set_stub_table(unsigned int shndx, The_stub_table* stub_table)
914   {
915     gold_assert(shndx < this->stub_tables_.size());
916     this->stub_tables_[shndx] = stub_table;
917   }
918
919  // Scan all relocation sections for stub generation.
920   void
921   scan_sections_for_stubs(The_target_aarch64*, const Symbol_table*,
922                           const Layout*);
923
924   // Whether a section is a scannable text section.
925   bool
926   text_section_is_scannable(const elfcpp::Shdr<size, big_endian>&, unsigned int,
927                             const Output_section*, const Symbol_table*);
928
929   // Convert regular input section with index SHNDX to a relaxed section.
930   void
931   convert_input_section_to_relaxed_section(unsigned /* shndx */)
932   {
933     // The stubs have relocations and we need to process them after writing
934     // out the stubs.  So relocation now must follow section write.
935     this->set_relocs_must_follow_section_writes();
936   }
937
938  protected:
939   // Post constructor setup.
940   void
941   do_setup()
942   {
943     // Call parent's setup method.
944     Sized_relobj_file<size, big_endian>::do_setup();
945
946     // Initialize look-up tables.
947     this->stub_tables_.resize(this->shnum());
948   }
949
950   virtual void
951   do_relocate_sections(
952       const Symbol_table* symtab, const Layout* layout,
953       const unsigned char* pshdrs, Output_file* of,
954       typename Sized_relobj_file<size, big_endian>::Views* pviews);
955
956  private:
957   // Whether a section needs to be scanned for relocation stubs.
958   bool
959   section_needs_reloc_stub_scanning(const elfcpp::Shdr<size, big_endian>&,
960                                     const Relobj::Output_sections&,
961                                     const Symbol_table*, const unsigned char*);
962
963   // List of stub tables.
964   Stub_table_list stub_tables_;
965 };  // End of AArch64_relobj
966
967
968 // Relocate sections.
969
970 template<int size, bool big_endian>
971 void
972 AArch64_relobj<size, big_endian>::do_relocate_sections(
973     const Symbol_table* symtab, const Layout* layout,
974     const unsigned char* pshdrs, Output_file* of,
975     typename Sized_relobj_file<size, big_endian>::Views* pviews)
976 {
977   // Call parent to relocate sections.
978   Sized_relobj_file<size, big_endian>::do_relocate_sections(symtab, layout,
979                                                             pshdrs, of, pviews);
980
981   // We do not generate stubs if doing a relocatable link.
982   if (parameters->options().relocatable())
983     return;
984
985   Relocate_info<size, big_endian> relinfo;
986   relinfo.symtab = symtab;
987   relinfo.layout = layout;
988   relinfo.object = this;
989
990   // Relocate stub tables.
991   unsigned int shnum = this->shnum();
992   The_target_aarch64* target = The_target_aarch64::current_target();
993
994   for (unsigned int i = 1; i < shnum; ++i)
995     {
996       The_aarch64_input_section* aarch64_input_section =
997           target->find_aarch64_input_section(this, i);
998       if (aarch64_input_section != NULL
999           && aarch64_input_section->is_stub_table_owner()
1000           && !aarch64_input_section->stub_table()->empty())
1001         {
1002           Output_section* os = this->output_section(i);
1003           gold_assert(os != NULL);
1004
1005           relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
1006           relinfo.reloc_shdr = NULL;
1007           relinfo.data_shndx = i;
1008           relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<size>::shdr_size;
1009
1010           typename Sized_relobj_file<size, big_endian>::View_size&
1011               view_struct = (*pviews)[i];
1012           gold_assert(view_struct.view != NULL);
1013
1014           The_stub_table* stub_table = aarch64_input_section->stub_table();
1015           off_t offset = stub_table->address() - view_struct.address;
1016           unsigned char* view = view_struct.view + offset;
1017           AArch64_address address = stub_table->address();
1018           section_size_type view_size = stub_table->data_size();
1019           stub_table->relocate_stubs(&relinfo, target, os, view, address,
1020                                      view_size);
1021         }
1022     }
1023 }
1024
1025
1026 // Determine if an input section is scannable for stub processing.  SHDR is
1027 // the header of the section and SHNDX is the section index.  OS is the output
1028 // section for the input section and SYMTAB is the global symbol table used to
1029 // look up ICF information.
1030
1031 template<int size, bool big_endian>
1032 bool
1033 AArch64_relobj<size, big_endian>::text_section_is_scannable(
1034     const elfcpp::Shdr<size, big_endian>& text_shdr,
1035     unsigned int text_shndx,
1036     const Output_section* os,
1037     const Symbol_table* symtab)
1038 {
1039   // Skip any empty sections, unallocated sections or sections whose
1040   // type are not SHT_PROGBITS.
1041   if (text_shdr.get_sh_size() == 0
1042       || (text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
1043       || text_shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
1044     return false;
1045
1046   // Skip any discarded or ICF'ed sections.
1047   if (os == NULL || symtab->is_section_folded(this, text_shndx))
1048     return false;
1049
1050   // Skip exception frame.
1051   if (strcmp(os->name(), ".eh_frame") == 0)
1052     return false ;
1053
1054   gold_assert(!this->is_output_section_offset_invalid(text_shndx) ||
1055               os->find_relaxed_input_section(this, text_shndx) != NULL);
1056
1057   return true;
1058 }
1059
1060
1061 // Determine if we want to scan the SHNDX-th section for relocation stubs.
1062 // This is a helper for AArch64_relobj::scan_sections_for_stubs().
1063
1064 template<int size, bool big_endian>
1065 bool
1066 AArch64_relobj<size, big_endian>::section_needs_reloc_stub_scanning(
1067     const elfcpp::Shdr<size, big_endian>& shdr,
1068     const Relobj::Output_sections& out_sections,
1069     const Symbol_table* symtab,
1070     const unsigned char* pshdrs)
1071 {
1072   unsigned int sh_type = shdr.get_sh_type();
1073   if (sh_type != elfcpp::SHT_RELA)
1074     return false;
1075
1076   // Ignore empty section.
1077   off_t sh_size = shdr.get_sh_size();
1078   if (sh_size == 0)
1079     return false;
1080
1081   // Ignore reloc section with unexpected symbol table.  The
1082   // error will be reported in the final link.
1083   if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
1084     return false;
1085
1086   gold_assert(sh_type == elfcpp::SHT_RELA);
1087   unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
1088
1089   // Ignore reloc section with unexpected entsize or uneven size.
1090   // The error will be reported in the final link.
1091   if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
1092     return false;
1093
1094   // Ignore reloc section with bad info.  This error will be
1095   // reported in the final link.
1096   unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_info());
1097   if (text_shndx >= this->shnum())
1098     return false;
1099
1100   const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1101   const elfcpp::Shdr<size, big_endian> text_shdr(pshdrs +
1102                                                  text_shndx * shdr_size);
1103   return this->text_section_is_scannable(text_shdr, text_shndx,
1104                                          out_sections[text_shndx], symtab);
1105 }
1106
1107
1108 // Scan relocations for stub generation.
1109
1110 template<int size, bool big_endian>
1111 void
1112 AArch64_relobj<size, big_endian>::scan_sections_for_stubs(
1113     The_target_aarch64* target,
1114     const Symbol_table* symtab,
1115     const Layout* layout)
1116 {
1117   unsigned int shnum = this->shnum();
1118   const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1119
1120   // Read the section headers.
1121   const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
1122                                                shnum * shdr_size,
1123                                                true, true);
1124
1125   // To speed up processing, we set up hash tables for fast lookup of
1126   // input offsets to output addresses.
1127   this->initialize_input_to_output_maps();
1128
1129   const Relobj::Output_sections& out_sections(this->output_sections());
1130
1131   Relocate_info<size, big_endian> relinfo;
1132   relinfo.symtab = symtab;
1133   relinfo.layout = layout;
1134   relinfo.object = this;
1135
1136   // Do relocation stubs scanning.
1137   const unsigned char* p = pshdrs + shdr_size;
1138   for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
1139     {
1140       const elfcpp::Shdr<size, big_endian> shdr(p);
1141       if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
1142                                                   pshdrs))
1143         {
1144           unsigned int index = this->adjust_shndx(shdr.get_sh_info());
1145           AArch64_address output_offset =
1146               this->get_output_section_offset(index);
1147           AArch64_address output_address;
1148           if (output_offset != invalid_address)
1149             {
1150               output_address = out_sections[index]->address() + output_offset;
1151             }
1152           else
1153             {
1154               // Currently this only happens for a relaxed section.
1155               const Output_relaxed_input_section* poris =
1156                   out_sections[index]->find_relaxed_input_section(this, index);
1157               gold_assert(poris != NULL);
1158               output_address = poris->address();
1159             }
1160
1161           // Get the relocations.
1162           const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
1163                                                         shdr.get_sh_size(),
1164                                                         true, false);
1165
1166           // Get the section contents.
1167           section_size_type input_view_size = 0;
1168           const unsigned char* input_view =
1169               this->section_contents(index, &input_view_size, false);
1170
1171           relinfo.reloc_shndx = i;
1172           relinfo.data_shndx = index;
1173           unsigned int sh_type = shdr.get_sh_type();
1174           unsigned int reloc_size;
1175           gold_assert (sh_type == elfcpp::SHT_RELA);
1176           reloc_size = elfcpp::Elf_sizes<size>::rela_size;
1177
1178           Output_section* os = out_sections[index];
1179           target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
1180                                          shdr.get_sh_size() / reloc_size,
1181                                          os,
1182                                          output_offset == invalid_address,
1183                                          input_view, output_address,
1184                                          input_view_size);
1185         }
1186     }
1187 }
1188
1189
1190 // A class to wrap an ordinary input section containing executable code.
1191
1192 template<int size, bool big_endian>
1193 class AArch64_input_section : public Output_relaxed_input_section
1194 {
1195  public:
1196   typedef Stub_table<size, big_endian> The_stub_table;
1197
1198   AArch64_input_section(Relobj* relobj, unsigned int shndx)
1199     : Output_relaxed_input_section(relobj, shndx, 1),
1200       stub_table_(NULL),
1201       original_contents_(NULL), original_size_(0),
1202       original_addralign_(1)
1203   { }
1204
1205   ~AArch64_input_section()
1206   { delete[] this->original_contents_; }
1207
1208   // Initialize.
1209   void
1210   init();
1211
1212   // Set the stub_table.
1213   void
1214   set_stub_table(The_stub_table* st)
1215   { this->stub_table_ = st; }
1216
1217   // Whether this is a stub table owner.
1218   bool
1219   is_stub_table_owner() const
1220   { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1221
1222   // Return the original size of the section.
1223   uint32_t
1224   original_size() const
1225   { return this->original_size_; }
1226
1227   // Return the stub table.
1228   The_stub_table*
1229   stub_table()
1230   { return stub_table_; }
1231
1232  protected:
1233   // Write out this input section.
1234   void
1235   do_write(Output_file*);
1236
1237   // Return required alignment of this.
1238   uint64_t
1239   do_addralign() const
1240   {
1241     if (this->is_stub_table_owner())
1242       return std::max(this->stub_table_->addralign(),
1243                       static_cast<uint64_t>(this->original_addralign_));
1244     else
1245       return this->original_addralign_;
1246   }
1247
1248   // Finalize data size.
1249   void
1250   set_final_data_size();
1251
1252   // Reset address and file offset.
1253   void
1254   do_reset_address_and_file_offset();
1255
1256   // Output offset.
1257   bool
1258   do_output_offset(const Relobj* object, unsigned int shndx,
1259                    section_offset_type offset,
1260                    section_offset_type* poutput) const
1261   {
1262     if ((object == this->relobj())
1263         && (shndx == this->shndx())
1264         && (offset >= 0)
1265         && (offset <=
1266             convert_types<section_offset_type, uint32_t>(this->original_size_)))
1267       {
1268         *poutput = offset;
1269         return true;
1270       }
1271     else
1272       return false;
1273   }
1274
1275  private:
1276   // Copying is not allowed.
1277   AArch64_input_section(const AArch64_input_section&);
1278   AArch64_input_section& operator=(const AArch64_input_section&);
1279
1280   // The relocation stubs.
1281   The_stub_table* stub_table_;
1282   // Original section contents.  We have to make a copy here since the file
1283   // containing the original section may not be locked when we need to access
1284   // the contents.
1285   unsigned char* original_contents_;
1286   // Section size of the original input section.
1287   uint32_t original_size_;
1288   // Address alignment of the original input section.
1289   uint32_t original_addralign_;
1290 };  // End of AArch64_input_section
1291
1292
1293 // Finalize data size.
1294
1295 template<int size, bool big_endian>
1296 void
1297 AArch64_input_section<size, big_endian>::set_final_data_size()
1298 {
1299   off_t off = convert_types<off_t, uint64_t>(this->original_size_);
1300
1301   if (this->is_stub_table_owner())
1302     {
1303       this->stub_table_->finalize_data_size();
1304       off = align_address(off, this->stub_table_->addralign());
1305       off += this->stub_table_->data_size();
1306     }
1307   this->set_data_size(off);
1308 }
1309
1310
1311 // Reset address and file offset.
1312
1313 template<int size, bool big_endian>
1314 void
1315 AArch64_input_section<size, big_endian>::do_reset_address_and_file_offset()
1316 {
1317   // Size of the original input section contents.
1318   off_t off = convert_types<off_t, uint64_t>(this->original_size_);
1319
1320   // If this is a stub table owner, account for the stub table size.
1321   if (this->is_stub_table_owner())
1322     {
1323       The_stub_table* stub_table = this->stub_table_;
1324
1325       // Reset the stub table's address and file offset.  The
1326       // current data size for child will be updated after that.
1327       stub_table_->reset_address_and_file_offset();
1328       off = align_address(off, stub_table_->addralign());
1329       off += stub_table->current_data_size();
1330     }
1331
1332   this->set_current_data_size(off);
1333 }
1334
1335
1336 // Initialize an Arm_input_section.
1337
1338 template<int size, bool big_endian>
1339 void
1340 AArch64_input_section<size, big_endian>::init()
1341 {
1342   Relobj* relobj = this->relobj();
1343   unsigned int shndx = this->shndx();
1344
1345   // We have to cache original size, alignment and contents to avoid locking
1346   // the original file.
1347   this->original_addralign_ =
1348       convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
1349
1350   // This is not efficient but we expect only a small number of relaxed
1351   // input sections for stubs.
1352   section_size_type section_size;
1353   const unsigned char* section_contents =
1354       relobj->section_contents(shndx, &section_size, false);
1355   this->original_size_ =
1356       convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
1357
1358   gold_assert(this->original_contents_ == NULL);
1359   this->original_contents_ = new unsigned char[section_size];
1360   memcpy(this->original_contents_, section_contents, section_size);
1361
1362   // We want to make this look like the original input section after
1363   // output sections are finalized.
1364   Output_section* os = relobj->output_section(shndx);
1365   off_t offset = relobj->output_section_offset(shndx);
1366   gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
1367   this->set_address(os->address() + offset);
1368   this->set_file_offset(os->offset() + offset);
1369   this->set_current_data_size(this->original_size_);
1370   this->finalize_data_size();
1371 }
1372
1373
1374 // Write data to output file.
1375
1376 template<int size, bool big_endian>
1377 void
1378 AArch64_input_section<size, big_endian>::do_write(Output_file* of)
1379 {
1380   // We have to write out the original section content.
1381   gold_assert(this->original_contents_ != NULL);
1382   of->write(this->offset(), this->original_contents_,
1383             this->original_size_);
1384
1385   // If this owns a stub table and it is not empty, write it.
1386   if (this->is_stub_table_owner() && !this->stub_table_->empty())
1387     this->stub_table_->write(of);
1388 }
1389
1390
1391 // Arm output section class.  This is defined mainly to add a number of stub
1392 // generation methods.
1393
1394 template<int size, bool big_endian>
1395 class AArch64_output_section : public Output_section
1396 {
1397  public:
1398   typedef Target_aarch64<size, big_endian> The_target_aarch64;
1399   typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
1400   typedef Stub_table<size, big_endian> The_stub_table;
1401   typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1402
1403  public:
1404   AArch64_output_section(const char* name, elfcpp::Elf_Word type,
1405                          elfcpp::Elf_Xword flags)
1406     : Output_section(name, type, flags)
1407   { }
1408
1409   ~AArch64_output_section() {}
1410
1411   // Group input sections for stub generation.
1412   void
1413   group_sections(section_size_type, bool, Target_aarch64<size, big_endian>*,
1414                  const Task*);
1415
1416  private:
1417   typedef Output_section::Input_section Input_section;
1418   typedef Output_section::Input_section_list Input_section_list;
1419
1420   // Create a stub group.
1421   void
1422   create_stub_group(Input_section_list::const_iterator,
1423                     Input_section_list::const_iterator,
1424                     Input_section_list::const_iterator,
1425                     The_target_aarch64*,
1426                     std::vector<Output_relaxed_input_section*>&,
1427                     const Task*);
1428 };  // End of AArch64_output_section
1429
1430
1431 // Create a stub group for input sections from FIRST to LAST. OWNER points to
1432 // the input section that will be the owner of the stub table.
1433
1434 template<int size, bool big_endian> void
1435 AArch64_output_section<size, big_endian>::create_stub_group(
1436     Input_section_list::const_iterator first,
1437     Input_section_list::const_iterator last,
1438     Input_section_list::const_iterator owner,
1439     The_target_aarch64* target,
1440     std::vector<Output_relaxed_input_section*>& new_relaxed_sections,
1441     const Task* task)
1442 {
1443   // Currently we convert ordinary input sections into relaxed sections only
1444   // at this point.
1445   The_aarch64_input_section* input_section;
1446   if (owner->is_relaxed_input_section())
1447     gold_unreachable();
1448   else
1449     {
1450       gold_assert(owner->is_input_section());
1451       // Create a new relaxed input section.  We need to lock the original
1452       // file.
1453       Task_lock_obj<Object> tl(task, owner->relobj());
1454       input_section =
1455           target->new_aarch64_input_section(owner->relobj(), owner->shndx());
1456       new_relaxed_sections.push_back(input_section);
1457     }
1458
1459   // Create a stub table.
1460   The_stub_table* stub_table =
1461       target->new_stub_table(input_section);
1462
1463   input_section->set_stub_table(stub_table);
1464
1465   Input_section_list::const_iterator p = first;
1466   // Look for input sections or relaxed input sections in [first ... last].
1467   do
1468     {
1469       if (p->is_input_section() || p->is_relaxed_input_section())
1470         {
1471           // The stub table information for input sections live
1472           // in their objects.
1473           The_aarch64_relobj* aarch64_relobj =
1474               static_cast<The_aarch64_relobj*>(p->relobj());
1475           aarch64_relobj->set_stub_table(p->shndx(), stub_table);
1476         }
1477     }
1478   while (p++ != last);
1479 }
1480
1481
1482 // Group input sections for stub generation. GROUP_SIZE is roughly the limit of
1483 // stub groups. We grow a stub group by adding input section until the size is
1484 // just below GROUP_SIZE. The last input section will be converted into a stub
1485 // table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
1486 // after the stub table, effectively doubling the group size.
1487 //
1488 // This is similar to the group_sections() function in elf32-arm.c but is
1489 // implemented differently.
1490
1491 template<int size, bool big_endian>
1492 void AArch64_output_section<size, big_endian>::group_sections(
1493     section_size_type group_size,
1494     bool stubs_always_after_branch,
1495     Target_aarch64<size, big_endian>* target,
1496     const Task* task)
1497 {
1498   typedef enum
1499   {
1500     NO_GROUP,
1501     FINDING_STUB_SECTION,
1502     HAS_STUB_SECTION
1503   } State;
1504
1505   std::vector<Output_relaxed_input_section*> new_relaxed_sections;
1506
1507   State state = NO_GROUP;
1508   section_size_type off = 0;
1509   section_size_type group_begin_offset = 0;
1510   section_size_type group_end_offset = 0;
1511   section_size_type stub_table_end_offset = 0;
1512   Input_section_list::const_iterator group_begin =
1513       this->input_sections().end();
1514   Input_section_list::const_iterator stub_table =
1515       this->input_sections().end();
1516   Input_section_list::const_iterator group_end = this->input_sections().end();
1517   for (Input_section_list::const_iterator p = this->input_sections().begin();
1518        p != this->input_sections().end();
1519        ++p)
1520     {
1521       section_size_type section_begin_offset =
1522         align_address(off, p->addralign());
1523       section_size_type section_end_offset =
1524         section_begin_offset + p->data_size();
1525
1526       // Check to see if we should group the previously seen sections.
1527       switch (state)
1528         {
1529         case NO_GROUP:
1530           break;
1531
1532         case FINDING_STUB_SECTION:
1533           // Adding this section makes the group larger than GROUP_SIZE.
1534           if (section_end_offset - group_begin_offset >= group_size)
1535             {
1536               if (stubs_always_after_branch)
1537                 {
1538                   gold_assert(group_end != this->input_sections().end());
1539                   this->create_stub_group(group_begin, group_end, group_end,
1540                                           target, new_relaxed_sections,
1541                                           task);
1542                   state = NO_GROUP;
1543                 }
1544               else
1545                 {
1546                   // Input sections up to stub_group_size bytes after the stub
1547                   // table can be handled by it too.
1548                   state = HAS_STUB_SECTION;
1549                   stub_table = group_end;
1550                   stub_table_end_offset = group_end_offset;
1551                 }
1552             }
1553             break;
1554
1555         case HAS_STUB_SECTION:
1556           // Adding this section makes the post stub-section group larger
1557           // than GROUP_SIZE.
1558           gold_unreachable();
1559           // NOT SUPPORTED YET. For completeness only.
1560           if (section_end_offset - stub_table_end_offset >= group_size)
1561            {
1562              gold_assert(group_end != this->input_sections().end());
1563              this->create_stub_group(group_begin, group_end, stub_table,
1564                                      target, new_relaxed_sections, task);
1565              state = NO_GROUP;
1566            }
1567            break;
1568
1569           default:
1570             gold_unreachable();
1571         }
1572
1573       // If we see an input section and currently there is no group, start
1574       // a new one.  Skip any empty sections.  We look at the data size
1575       // instead of calling p->relobj()->section_size() to avoid locking.
1576       if ((p->is_input_section() || p->is_relaxed_input_section())
1577           && (p->data_size() != 0))
1578         {
1579           if (state == NO_GROUP)
1580             {
1581               state = FINDING_STUB_SECTION;
1582               group_begin = p;
1583               group_begin_offset = section_begin_offset;
1584             }
1585
1586           // Keep track of the last input section seen.
1587           group_end = p;
1588           group_end_offset = section_end_offset;
1589         }
1590
1591       off = section_end_offset;
1592     }
1593
1594   // Create a stub group for any ungrouped sections.
1595   if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
1596     {
1597       gold_assert(group_end != this->input_sections().end());
1598       this->create_stub_group(group_begin, group_end,
1599                               (state == FINDING_STUB_SECTION
1600                                ? group_end
1601                                : stub_table),
1602                               target, new_relaxed_sections, task);
1603     }
1604
1605   if (!new_relaxed_sections.empty())
1606     this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
1607
1608   // Update the section offsets
1609   for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
1610     {
1611       The_aarch64_relobj* relobj = static_cast<The_aarch64_relobj*>(
1612           new_relaxed_sections[i]->relobj());
1613       unsigned int shndx = new_relaxed_sections[i]->shndx();
1614       // Tell AArch64_relobj that this input section is converted.
1615       relobj->convert_input_section_to_relaxed_section(shndx);
1616     }
1617 }  // End of AArch64_output_section::group_sections
1618
1619
1620 AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
1621
1622
1623 // The aarch64 target class.
1624 // See the ABI at
1625 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
1626 template<int size, bool big_endian>
1627 class Target_aarch64 : public Sized_target<size, big_endian>
1628 {
1629  public:
1630   typedef Target_aarch64<size, big_endian> This;
1631   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
1632       Reloc_section;
1633   typedef Relocate_info<size, big_endian> The_relocate_info;
1634   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1635   typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
1636   typedef Reloc_stub<size, big_endian> The_reloc_stub;
1637   typedef typename The_reloc_stub::Stub_type The_reloc_stub_type;
1638   typedef typename Reloc_stub<size, big_endian>::Key The_reloc_stub_key;
1639   typedef Stub_table<size, big_endian> The_stub_table;
1640   typedef std::vector<The_stub_table*> Stub_table_list;
1641   typedef typename Stub_table_list::iterator Stub_table_iterator;
1642   typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1643   typedef AArch64_output_section<size, big_endian> The_aarch64_output_section;
1644   typedef Unordered_map<Section_id,
1645                         AArch64_input_section<size, big_endian>*,
1646                         Section_id_hash> AArch64_input_section_map;
1647   const static int TCB_SIZE = size / 8 * 2;
1648
1649   Target_aarch64(const Target::Target_info* info = &aarch64_info)
1650     : Sized_target<size, big_endian>(info),
1651       got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
1652       got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
1653       rela_irelative_(NULL), copy_relocs_(elfcpp::R_AARCH64_COPY),
1654       got_mod_index_offset_(-1U),
1655       tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
1656       stub_tables_(), aarch64_input_section_map_()
1657   { }
1658
1659   // Scan the relocations to determine unreferenced sections for
1660   // garbage collection.
1661   void
1662   gc_process_relocs(Symbol_table* symtab,
1663                     Layout* layout,
1664                     Sized_relobj_file<size, big_endian>* object,
1665                     unsigned int data_shndx,
1666                     unsigned int sh_type,
1667                     const unsigned char* prelocs,
1668                     size_t reloc_count,
1669                     Output_section* output_section,
1670                     bool needs_special_offset_handling,
1671                     size_t local_symbol_count,
1672                     const unsigned char* plocal_symbols);
1673
1674   // Scan the relocations to look for symbol adjustments.
1675   void
1676   scan_relocs(Symbol_table* symtab,
1677               Layout* layout,
1678               Sized_relobj_file<size, big_endian>* object,
1679               unsigned int data_shndx,
1680               unsigned int sh_type,
1681               const unsigned char* prelocs,
1682               size_t reloc_count,
1683               Output_section* output_section,
1684               bool needs_special_offset_handling,
1685               size_t local_symbol_count,
1686               const unsigned char* plocal_symbols);
1687
1688   // Finalize the sections.
1689   void
1690   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
1691
1692   // Return the value to use for a dynamic which requires special
1693   // treatment.
1694   uint64_t
1695   do_dynsym_value(const Symbol*) const;
1696
1697   // Relocate a section.
1698   void
1699   relocate_section(const Relocate_info<size, big_endian>*,
1700                    unsigned int sh_type,
1701                    const unsigned char* prelocs,
1702                    size_t reloc_count,
1703                    Output_section* output_section,
1704                    bool needs_special_offset_handling,
1705                    unsigned char* view,
1706                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
1707                    section_size_type view_size,
1708                    const Reloc_symbol_changes*);
1709
1710   // Scan the relocs during a relocatable link.
1711   void
1712   scan_relocatable_relocs(Symbol_table* symtab,
1713                           Layout* layout,
1714                           Sized_relobj_file<size, big_endian>* object,
1715                           unsigned int data_shndx,
1716                           unsigned int sh_type,
1717                           const unsigned char* prelocs,
1718                           size_t reloc_count,
1719                           Output_section* output_section,
1720                           bool needs_special_offset_handling,
1721                           size_t local_symbol_count,
1722                           const unsigned char* plocal_symbols,
1723                           Relocatable_relocs*);
1724
1725   // Relocate a section during a relocatable link.
1726   void
1727   relocate_relocs(
1728       const Relocate_info<size, big_endian>*,
1729       unsigned int sh_type,
1730       const unsigned char* prelocs,
1731       size_t reloc_count,
1732       Output_section* output_section,
1733       typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
1734       const Relocatable_relocs*,
1735       unsigned char* view,
1736       typename elfcpp::Elf_types<size>::Elf_Addr view_address,
1737       section_size_type view_size,
1738       unsigned char* reloc_view,
1739       section_size_type reloc_view_size);
1740
1741   // Return the symbol index to use for a target specific relocation.
1742   // The only target specific relocation is R_AARCH64_TLSDESC for a
1743   // local symbol, which is an absolute reloc.
1744   unsigned int
1745   do_reloc_symbol_index(void*, unsigned int r_type) const
1746   {
1747     gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
1748     return 0;
1749   }
1750
1751   // Return the addend to use for a target specific relocation.
1752   typename elfcpp::Elf_types<size>::Elf_Addr
1753   do_reloc_addend(void* arg, unsigned int r_type,
1754                   typename elfcpp::Elf_types<size>::Elf_Addr addend) const;
1755
1756   // Return the PLT section.
1757   uint64_t
1758   do_plt_address_for_global(const Symbol* gsym) const
1759   { return this->plt_section()->address_for_global(gsym); }
1760
1761   uint64_t
1762   do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
1763   { return this->plt_section()->address_for_local(relobj, symndx); }
1764
1765   // This function should be defined in targets that can use relocation
1766   // types to determine (implemented in local_reloc_may_be_function_pointer
1767   // and global_reloc_may_be_function_pointer)
1768   // if a function's pointer is taken.  ICF uses this in safe mode to only
1769   // fold those functions whose pointer is defintely not taken.
1770   bool
1771   do_can_check_for_function_pointers() const
1772   { return true; }
1773
1774   // Return the number of entries in the PLT.
1775   unsigned int
1776   plt_entry_count() const;
1777
1778   //Return the offset of the first non-reserved PLT entry.
1779   unsigned int
1780   first_plt_entry_offset() const;
1781
1782   // Return the size of each PLT entry.
1783   unsigned int
1784   plt_entry_size() const;
1785
1786   // Create a stub table.
1787   The_stub_table*
1788   new_stub_table(The_aarch64_input_section*);
1789
1790   // Create an aarch64 input section.
1791   The_aarch64_input_section*
1792   new_aarch64_input_section(Relobj*, unsigned int);
1793
1794   // Find an aarch64 input section instance for a given OBJ and SHNDX.
1795   The_aarch64_input_section*
1796   find_aarch64_input_section(Relobj*, unsigned int) const;
1797
1798   // Return the thread control block size.
1799   unsigned int
1800   tcb_size() const { return This::TCB_SIZE; }
1801
1802   // Scan a section for stub generation.
1803   void
1804   scan_section_for_stubs(const Relocate_info<size, big_endian>*, unsigned int,
1805                          const unsigned char*, size_t, Output_section*,
1806                          bool, const unsigned char*,
1807                          Address,
1808                          section_size_type);
1809
1810   // Scan a relocation section for stub.
1811   template<int sh_type>
1812   void
1813   scan_reloc_section_for_stubs(
1814       const The_relocate_info* relinfo,
1815       const unsigned char* prelocs,
1816       size_t reloc_count,
1817       Output_section* output_section,
1818       bool needs_special_offset_handling,
1819       const unsigned char* view,
1820       Address view_address,
1821       section_size_type);
1822
1823   // Relocate a single stub.
1824   void
1825   relocate_stub(The_reloc_stub*, const Relocate_info<size, big_endian>*,
1826                 Output_section*, unsigned char*, Address,
1827                 section_size_type);
1828
1829   // Get the default AArch64 target.
1830   static This*
1831   current_target()
1832   {
1833     gold_assert(parameters->target().machine_code() == elfcpp::EM_AARCH64
1834                 && parameters->target().get_size() == size
1835                 && parameters->target().is_big_endian() == big_endian);
1836     return static_cast<This*>(parameters->sized_target<size, big_endian>());
1837   }
1838
1839  protected:
1840   void
1841   do_select_as_default_target()
1842   {
1843     gold_assert(aarch64_reloc_property_table == NULL);
1844     aarch64_reloc_property_table = new AArch64_reloc_property_table();
1845   }
1846
1847   // Add a new reloc argument, returning the index in the vector.
1848   size_t
1849   add_tlsdesc_info(Sized_relobj_file<size, big_endian>* object,
1850                    unsigned int r_sym)
1851   {
1852     this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
1853     return this->tlsdesc_reloc_info_.size() - 1;
1854   }
1855
1856   virtual Output_data_plt_aarch64<size, big_endian>*
1857   do_make_data_plt(Layout* layout,
1858                    Output_data_got_aarch64<size, big_endian>* got,
1859                    Output_data_space* got_plt,
1860                    Output_data_space* got_irelative)
1861   {
1862     return new Output_data_plt_aarch64_standard<size, big_endian>(
1863       layout, got, got_plt, got_irelative);
1864   }
1865
1866
1867   // do_make_elf_object to override the same function in the base class.
1868   Object*
1869   do_make_elf_object(const std::string&, Input_file*, off_t,
1870                      const elfcpp::Ehdr<size, big_endian>&);
1871
1872   Output_data_plt_aarch64<size, big_endian>*
1873   make_data_plt(Layout* layout,
1874                 Output_data_got_aarch64<size, big_endian>* got,
1875                 Output_data_space* got_plt,
1876                 Output_data_space* got_irelative)
1877   {
1878     return this->do_make_data_plt(layout, got, got_plt, got_irelative);
1879   }
1880
1881   // We only need to generate stubs, and hence perform relaxation if we are
1882   // not doing relocatable linking.
1883   virtual bool
1884   do_may_relax() const
1885   { return !parameters->options().relocatable(); }
1886
1887   // Relaxation hook.  This is where we do stub generation.
1888   virtual bool
1889   do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
1890
1891   void
1892   group_sections(Layout* layout,
1893                  section_size_type group_size,
1894                  bool stubs_always_after_branch,
1895                  const Task* task);
1896
1897   void
1898   scan_reloc_for_stub(const The_relocate_info*, unsigned int,
1899                       const Sized_symbol<size>*, unsigned int,
1900                       const Symbol_value<size>*,
1901                       typename elfcpp::Elf_types<size>::Elf_Swxword,
1902                       Address Elf_Addr);
1903
1904   // Make an output section.
1905   Output_section*
1906   do_make_output_section(const char* name, elfcpp::Elf_Word type,
1907                          elfcpp::Elf_Xword flags)
1908   { return new The_aarch64_output_section(name, type, flags); }
1909
1910  private:
1911   // The class which scans relocations.
1912   class Scan
1913   {
1914   public:
1915     Scan()
1916       : issued_non_pic_error_(false)
1917     { }
1918
1919     inline void
1920     local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
1921           Sized_relobj_file<size, big_endian>* object,
1922           unsigned int data_shndx,
1923           Output_section* output_section,
1924           const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1925           const elfcpp::Sym<size, big_endian>& lsym,
1926           bool is_discarded);
1927
1928     inline void
1929     global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
1930            Sized_relobj_file<size, big_endian>* object,
1931            unsigned int data_shndx,
1932            Output_section* output_section,
1933            const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1934            Symbol* gsym);
1935
1936     inline bool
1937     local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1938                                         Target_aarch64<size, big_endian>* ,
1939                                         Sized_relobj_file<size, big_endian>* ,
1940                                         unsigned int ,
1941                                         Output_section* ,
1942                                         const elfcpp::Rela<size, big_endian>& ,
1943                                         unsigned int r_type,
1944                                         const elfcpp::Sym<size, big_endian>&);
1945
1946     inline bool
1947     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1948                                          Target_aarch64<size, big_endian>* ,
1949                                          Sized_relobj_file<size, big_endian>* ,
1950                                          unsigned int ,
1951                                          Output_section* ,
1952                                          const elfcpp::Rela<size, big_endian>& ,
1953                                          unsigned int r_type,
1954                                          Symbol* gsym);
1955
1956   private:
1957     static void
1958     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1959                             unsigned int r_type);
1960
1961     static void
1962     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1963                              unsigned int r_type, Symbol*);
1964
1965     inline bool
1966     possible_function_pointer_reloc(unsigned int r_type);
1967
1968     void
1969     check_non_pic(Relobj*, unsigned int r_type);
1970
1971     bool
1972     reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
1973                               unsigned int r_type);
1974
1975     // Whether we have issued an error about a non-PIC compilation.
1976     bool issued_non_pic_error_;
1977   };
1978
1979   // The class which implements relocation.
1980   class Relocate
1981   {
1982    public:
1983     Relocate()
1984       : skip_call_tls_get_addr_(false)
1985     { }
1986
1987     ~Relocate()
1988     { }
1989
1990     // Do a relocation.  Return false if the caller should not issue
1991     // any warnings about this relocation.
1992     inline bool
1993     relocate(const Relocate_info<size, big_endian>*, Target_aarch64*,
1994              Output_section*,
1995              size_t relnum, const elfcpp::Rela<size, big_endian>&,
1996              unsigned int r_type, const Sized_symbol<size>*,
1997              const Symbol_value<size>*,
1998              unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1999              section_size_type);
2000
2001   private:
2002     inline typename AArch64_relocate_functions<size, big_endian>::Status
2003     relocate_tls(const Relocate_info<size, big_endian>*,
2004                  Target_aarch64<size, big_endian>*,
2005                  size_t,
2006                  const elfcpp::Rela<size, big_endian>&,
2007                  unsigned int r_type, const Sized_symbol<size>*,
2008                  const Symbol_value<size>*,
2009                  unsigned char*,
2010                  typename elfcpp::Elf_types<size>::Elf_Addr);
2011
2012     inline typename AArch64_relocate_functions<size, big_endian>::Status
2013     tls_gd_to_le(
2014                  const Relocate_info<size, big_endian>*,
2015                  Target_aarch64<size, big_endian>*,
2016                  const elfcpp::Rela<size, big_endian>&,
2017                  unsigned int,
2018                  unsigned char*,
2019                  const Symbol_value<size>*);
2020
2021     inline typename AArch64_relocate_functions<size, big_endian>::Status
2022     tls_ld_to_le(
2023                  const Relocate_info<size, big_endian>*,
2024                  Target_aarch64<size, big_endian>*,
2025                  const elfcpp::Rela<size, big_endian>&,
2026                  unsigned int,
2027                  unsigned char*,
2028                  const Symbol_value<size>*);
2029
2030     inline typename AArch64_relocate_functions<size, big_endian>::Status
2031     tls_ie_to_le(
2032                  const Relocate_info<size, big_endian>*,
2033                  Target_aarch64<size, big_endian>*,
2034                  const elfcpp::Rela<size, big_endian>&,
2035                  unsigned int,
2036                  unsigned char*,
2037                  const Symbol_value<size>*);
2038
2039     inline typename AArch64_relocate_functions<size, big_endian>::Status
2040     tls_desc_gd_to_le(
2041                  const Relocate_info<size, big_endian>*,
2042                  Target_aarch64<size, big_endian>*,
2043                  const elfcpp::Rela<size, big_endian>&,
2044                  unsigned int,
2045                  unsigned char*,
2046                  const Symbol_value<size>*);
2047
2048     inline typename AArch64_relocate_functions<size, big_endian>::Status
2049     tls_desc_gd_to_ie(
2050                  const Relocate_info<size, big_endian>*,
2051                  Target_aarch64<size, big_endian>*,
2052                  const elfcpp::Rela<size, big_endian>&,
2053                  unsigned int,
2054                  unsigned char*,
2055                  const Symbol_value<size>*,
2056                  typename elfcpp::Elf_types<size>::Elf_Addr,
2057                  typename elfcpp::Elf_types<size>::Elf_Addr);
2058
2059     bool skip_call_tls_get_addr_;
2060
2061   };  // End of class Relocate
2062
2063   // A class which returns the size required for a relocation type,
2064   // used while scanning relocs during a relocatable link.
2065   class Relocatable_size_for_reloc
2066   {
2067    public:
2068     unsigned int
2069     get_size_for_reloc(unsigned int, Relobj*);
2070   };
2071
2072   // Adjust TLS relocation type based on the options and whether this
2073   // is a local symbol.
2074   static tls::Tls_optimization
2075   optimize_tls_reloc(bool is_final, int r_type);
2076
2077   // Get the GOT section, creating it if necessary.
2078   Output_data_got_aarch64<size, big_endian>*
2079   got_section(Symbol_table*, Layout*);
2080
2081   // Get the GOT PLT section.
2082   Output_data_space*
2083   got_plt_section() const
2084   {
2085     gold_assert(this->got_plt_ != NULL);
2086     return this->got_plt_;
2087   }
2088
2089   // Get the GOT section for TLSDESC entries.
2090   Output_data_got<size, big_endian>*
2091   got_tlsdesc_section() const
2092   {
2093     gold_assert(this->got_tlsdesc_ != NULL);
2094     return this->got_tlsdesc_;
2095   }
2096
2097   // Create the PLT section.
2098   void
2099   make_plt_section(Symbol_table* symtab, Layout* layout);
2100
2101   // Create a PLT entry for a global symbol.
2102   void
2103   make_plt_entry(Symbol_table*, Layout*, Symbol*);
2104
2105   // Create a PLT entry for a local STT_GNU_IFUNC symbol.
2106   void
2107   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
2108                              Sized_relobj_file<size, big_endian>* relobj,
2109                              unsigned int local_sym_index);
2110
2111   // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2112   void
2113   define_tls_base_symbol(Symbol_table*, Layout*);
2114
2115   // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2116   void
2117   reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
2118
2119   // Create a GOT entry for the TLS module index.
2120   unsigned int
2121   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2122                       Sized_relobj_file<size, big_endian>* object);
2123
2124   // Get the PLT section.
2125   Output_data_plt_aarch64<size, big_endian>*
2126   plt_section() const
2127   {
2128     gold_assert(this->plt_ != NULL);
2129     return this->plt_;
2130   }
2131
2132   // Get the dynamic reloc section, creating it if necessary.
2133   Reloc_section*
2134   rela_dyn_section(Layout*);
2135
2136   // Get the section to use for TLSDESC relocations.
2137   Reloc_section*
2138   rela_tlsdesc_section(Layout*) const;
2139
2140   // Get the section to use for IRELATIVE relocations.
2141   Reloc_section*
2142   rela_irelative_section(Layout*);
2143
2144   // Add a potential copy relocation.
2145   void
2146   copy_reloc(Symbol_table* symtab, Layout* layout,
2147              Sized_relobj_file<size, big_endian>* object,
2148              unsigned int shndx, Output_section* output_section,
2149              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
2150   {
2151     this->copy_relocs_.copy_reloc(symtab, layout,
2152                                   symtab->get_sized_symbol<size>(sym),
2153                                   object, shndx, output_section,
2154                                   reloc, this->rela_dyn_section(layout));
2155   }
2156
2157   // Information about this specific target which we pass to the
2158   // general Target structure.
2159   static const Target::Target_info aarch64_info;
2160
2161   // The types of GOT entries needed for this platform.
2162   // These values are exposed to the ABI in an incremental link.
2163   // Do not renumber existing values without changing the version
2164   // number of the .gnu_incremental_inputs section.
2165   enum Got_type
2166   {
2167     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
2168     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
2169     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
2170     GOT_TYPE_TLS_DESC = 3       // GOT entry for TLS_DESC pair
2171   };
2172
2173   // This type is used as the argument to the target specific
2174   // relocation routines.  The only target specific reloc is
2175   // R_AARCh64_TLSDESC against a local symbol.
2176   struct Tlsdesc_info
2177   {
2178     Tlsdesc_info(Sized_relobj_file<size, big_endian>* a_object,
2179                  unsigned int a_r_sym)
2180       : object(a_object), r_sym(a_r_sym)
2181     { }
2182
2183     // The object in which the local symbol is defined.
2184     Sized_relobj_file<size, big_endian>* object;
2185     // The local symbol index in the object.
2186     unsigned int r_sym;
2187   };
2188
2189   // The GOT section.
2190   Output_data_got_aarch64<size, big_endian>* got_;
2191   // The PLT section.
2192   Output_data_plt_aarch64<size, big_endian>* plt_;
2193   // The GOT PLT section.
2194   Output_data_space* got_plt_;
2195   // The GOT section for IRELATIVE relocations.
2196   Output_data_space* got_irelative_;
2197   // The GOT section for TLSDESC relocations.
2198   Output_data_got<size, big_endian>* got_tlsdesc_;
2199   // The _GLOBAL_OFFSET_TABLE_ symbol.
2200   Symbol* global_offset_table_;
2201   // The dynamic reloc section.
2202   Reloc_section* rela_dyn_;
2203   // The section to use for IRELATIVE relocs.
2204   Reloc_section* rela_irelative_;
2205   // Relocs saved to avoid a COPY reloc.
2206   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
2207   // Offset of the GOT entry for the TLS module index.
2208   unsigned int got_mod_index_offset_;
2209   // We handle R_AARCH64_TLSDESC against a local symbol as a target
2210   // specific relocation. Here we store the object and local symbol
2211   // index for the relocation.
2212   std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
2213   // True if the _TLS_MODULE_BASE_ symbol has been defined.
2214   bool tls_base_symbol_defined_;
2215   // List of stub_tables
2216   Stub_table_list stub_tables_;
2217   AArch64_input_section_map aarch64_input_section_map_;
2218 };  // End of Target_aarch64
2219
2220
2221 template<>
2222 const Target::Target_info Target_aarch64<64, false>::aarch64_info =
2223 {
2224   64,                   // size
2225   false,                // is_big_endian
2226   elfcpp::EM_AARCH64,   // machine_code
2227   false,                // has_make_symbol
2228   false,                // has_resolve
2229   false,                // has_code_fill
2230   true,                 // is_default_stack_executable
2231   true,                 // can_icf_inline_merge_sections
2232   '\0',                 // wrap_char
2233   "/lib/ld.so.1",       // program interpreter
2234   0x400000,             // default_text_segment_address
2235   0x1000,               // abi_pagesize (overridable by -z max-page-size)
2236   0x1000,               // common_pagesize (overridable by -z common-page-size)
2237   false,                // isolate_execinstr
2238   0,                    // rosegment_gap
2239   elfcpp::SHN_UNDEF,    // small_common_shndx
2240   elfcpp::SHN_UNDEF,    // large_common_shndx
2241   0,                    // small_common_section_flags
2242   0,                    // large_common_section_flags
2243   NULL,                 // attributes_section
2244   NULL,                 // attributes_vendor
2245   "_start"              // entry_symbol_name
2246 };
2247
2248 template<>
2249 const Target::Target_info Target_aarch64<32, false>::aarch64_info =
2250 {
2251   32,                   // size
2252   false,                // is_big_endian
2253   elfcpp::EM_AARCH64,   // machine_code
2254   false,                // has_make_symbol
2255   false,                // has_resolve
2256   false,                // has_code_fill
2257   true,                 // is_default_stack_executable
2258   false,                // can_icf_inline_merge_sections
2259   '\0',                 // wrap_char
2260   "/lib/ld.so.1",       // program interpreter
2261   0x400000,             // default_text_segment_address
2262   0x1000,               // abi_pagesize (overridable by -z max-page-size)
2263   0x1000,               // common_pagesize (overridable by -z common-page-size)
2264   false,                // isolate_execinstr
2265   0,                    // rosegment_gap
2266   elfcpp::SHN_UNDEF,    // small_common_shndx
2267   elfcpp::SHN_UNDEF,    // large_common_shndx
2268   0,                    // small_common_section_flags
2269   0,                    // large_common_section_flags
2270   NULL,                 // attributes_section
2271   NULL,                 // attributes_vendor
2272   "_start"              // entry_symbol_name
2273 };
2274
2275 template<>
2276 const Target::Target_info Target_aarch64<64, true>::aarch64_info =
2277 {
2278   64,                   // size
2279   true,                 // is_big_endian
2280   elfcpp::EM_AARCH64,   // machine_code
2281   false,                // has_make_symbol
2282   false,                // has_resolve
2283   false,                // has_code_fill
2284   true,                 // is_default_stack_executable
2285   true,                 // can_icf_inline_merge_sections
2286   '\0',                 // wrap_char
2287   "/lib/ld.so.1",       // program interpreter
2288   0x400000,             // default_text_segment_address
2289   0x1000,               // abi_pagesize (overridable by -z max-page-size)
2290   0x1000,               // common_pagesize (overridable by -z common-page-size)
2291   false,                // isolate_execinstr
2292   0,                    // rosegment_gap
2293   elfcpp::SHN_UNDEF,    // small_common_shndx
2294   elfcpp::SHN_UNDEF,    // large_common_shndx
2295   0,                    // small_common_section_flags
2296   0,                    // large_common_section_flags
2297   NULL,                 // attributes_section
2298   NULL,                 // attributes_vendor
2299   "_start"              // entry_symbol_name
2300 };
2301
2302 template<>
2303 const Target::Target_info Target_aarch64<32, true>::aarch64_info =
2304 {
2305   32,                   // size
2306   true,                 // is_big_endian
2307   elfcpp::EM_AARCH64,   // machine_code
2308   false,                // has_make_symbol
2309   false,                // has_resolve
2310   false,                // has_code_fill
2311   true,                 // is_default_stack_executable
2312   false,                // can_icf_inline_merge_sections
2313   '\0',                 // wrap_char
2314   "/lib/ld.so.1",       // program interpreter
2315   0x400000,             // default_text_segment_address
2316   0x1000,               // abi_pagesize (overridable by -z max-page-size)
2317   0x1000,               // common_pagesize (overridable by -z common-page-size)
2318   false,                // isolate_execinstr
2319   0,                    // rosegment_gap
2320   elfcpp::SHN_UNDEF,    // small_common_shndx
2321   elfcpp::SHN_UNDEF,    // large_common_shndx
2322   0,                    // small_common_section_flags
2323   0,                    // large_common_section_flags
2324   NULL,                 // attributes_section
2325   NULL,                 // attributes_vendor
2326   "_start"              // entry_symbol_name
2327 };
2328
2329 // Get the GOT section, creating it if necessary.
2330
2331 template<int size, bool big_endian>
2332 Output_data_got_aarch64<size, big_endian>*
2333 Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
2334                                               Layout* layout)
2335 {
2336   if (this->got_ == NULL)
2337     {
2338       gold_assert(symtab != NULL && layout != NULL);
2339
2340       // When using -z now, we can treat .got.plt as a relro section.
2341       // Without -z now, it is modified after program startup by lazy
2342       // PLT relocations.
2343       bool is_got_plt_relro = parameters->options().now();
2344       Output_section_order got_order = (is_got_plt_relro
2345                                         ? ORDER_RELRO
2346                                         : ORDER_RELRO_LAST);
2347       Output_section_order got_plt_order = (is_got_plt_relro
2348                                             ? ORDER_RELRO
2349                                             : ORDER_NON_RELRO_FIRST);
2350
2351       // Layout of .got and .got.plt sections.
2352       // .got[0] &_DYNAMIC                          <-_GLOBAL_OFFSET_TABLE_
2353       // ...
2354       // .gotplt[0] reserved for ld.so (&linkmap)   <--DT_PLTGOT
2355       // .gotplt[1] reserved for ld.so (resolver)
2356       // .gotplt[2] reserved
2357
2358       // Generate .got section.
2359       this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
2360                                                                  layout);
2361       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2362                                       (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
2363                                       this->got_, got_order, true);
2364       // The first word of GOT is reserved for the address of .dynamic.
2365       // We put 0 here now. The value will be replaced later in
2366       // Output_data_got_aarch64::do_write.
2367       this->got_->add_constant(0);
2368
2369       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2370       // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
2371       // even if there is a .got.plt section.
2372       this->global_offset_table_ =
2373         symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2374                                       Symbol_table::PREDEFINED,
2375                                       this->got_,
2376                                       0, 0, elfcpp::STT_OBJECT,
2377                                       elfcpp::STB_LOCAL,
2378                                       elfcpp::STV_HIDDEN, 0,
2379                                       false, false);
2380
2381       // Generate .got.plt section.
2382       this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
2383       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2384                                       (elfcpp::SHF_ALLOC
2385                                        | elfcpp::SHF_WRITE),
2386                                       this->got_plt_, got_plt_order,
2387                                       is_got_plt_relro);
2388
2389       // The first three entries are reserved.
2390       this->got_plt_->set_current_data_size(
2391         AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
2392
2393       // If there are any IRELATIVE relocations, they get GOT entries
2394       // in .got.plt after the jump slot entries.
2395       this->got_irelative_ = new Output_data_space(size / 8,
2396                                                    "** GOT IRELATIVE PLT");
2397       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2398                                       (elfcpp::SHF_ALLOC
2399                                        | elfcpp::SHF_WRITE),
2400                                       this->got_irelative_,
2401                                       got_plt_order,
2402                                       is_got_plt_relro);
2403
2404       // If there are any TLSDESC relocations, they get GOT entries in
2405       // .got.plt after the jump slot and IRELATIVE entries.
2406       this->got_tlsdesc_ = new Output_data_got<size, big_endian>();
2407       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2408                                       (elfcpp::SHF_ALLOC
2409                                        | elfcpp::SHF_WRITE),
2410                                       this->got_tlsdesc_,
2411                                       got_plt_order,
2412                                       is_got_plt_relro);
2413
2414       if (!is_got_plt_relro)
2415         {
2416           // Those bytes can go into the relro segment.
2417           layout->increase_relro(
2418             AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
2419         }
2420
2421     }
2422   return this->got_;
2423 }
2424
2425 // Get the dynamic reloc section, creating it if necessary.
2426
2427 template<int size, bool big_endian>
2428 typename Target_aarch64<size, big_endian>::Reloc_section*
2429 Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
2430 {
2431   if (this->rela_dyn_ == NULL)
2432     {
2433       gold_assert(layout != NULL);
2434       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2435       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2436                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
2437                                       ORDER_DYNAMIC_RELOCS, false);
2438     }
2439   return this->rela_dyn_;
2440 }
2441
2442 // Get the section to use for IRELATIVE relocs, creating it if
2443 // necessary.  These go in .rela.dyn, but only after all other dynamic
2444 // relocations.  They need to follow the other dynamic relocations so
2445 // that they can refer to global variables initialized by those
2446 // relocs.
2447
2448 template<int size, bool big_endian>
2449 typename Target_aarch64<size, big_endian>::Reloc_section*
2450 Target_aarch64<size, big_endian>::rela_irelative_section(Layout* layout)
2451 {
2452   if (this->rela_irelative_ == NULL)
2453     {
2454       // Make sure we have already created the dynamic reloc section.
2455       this->rela_dyn_section(layout);
2456       this->rela_irelative_ = new Reloc_section(false);
2457       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2458                                       elfcpp::SHF_ALLOC, this->rela_irelative_,
2459                                       ORDER_DYNAMIC_RELOCS, false);
2460       gold_assert(this->rela_dyn_->output_section()
2461                   == this->rela_irelative_->output_section());
2462     }
2463   return this->rela_irelative_;
2464 }
2465
2466
2467 // do_make_elf_object to override the same function in the base class.  We need
2468 // to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
2469 // store backend specific information. Hence we need to have our own ELF object
2470 // creation.
2471
2472 template<int size, bool big_endian>
2473 Object*
2474 Target_aarch64<size, big_endian>::do_make_elf_object(
2475     const std::string& name,
2476     Input_file* input_file,
2477     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2478 {
2479   int et = ehdr.get_e_type();
2480   // ET_EXEC files are valid input for --just-symbols/-R,
2481   // and we treat them as relocatable objects.
2482   if (et == elfcpp::ET_EXEC && input_file->just_symbols())
2483     return Sized_target<size, big_endian>::do_make_elf_object(
2484         name, input_file, offset, ehdr);
2485   else if (et == elfcpp::ET_REL)
2486     {
2487       AArch64_relobj<size, big_endian>* obj =
2488         new AArch64_relobj<size, big_endian>(name, input_file, offset, ehdr);
2489       obj->setup();
2490       return obj;
2491     }
2492   else if (et == elfcpp::ET_DYN)
2493     {
2494       // Keep base implementation.
2495       Sized_dynobj<size, big_endian>* obj =
2496           new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2497       obj->setup();
2498       return obj;
2499     }
2500   else
2501     {
2502       gold_error(_("%s: unsupported ELF file type %d"),
2503                  name.c_str(), et);
2504       return NULL;
2505     }
2506 }
2507
2508
2509 // Scan a relocation for stub generation.
2510
2511 template<int size, bool big_endian>
2512 void
2513 Target_aarch64<size, big_endian>::scan_reloc_for_stub(
2514     const Relocate_info<size, big_endian>* relinfo,
2515     unsigned int r_type,
2516     const Sized_symbol<size>* gsym,
2517     unsigned int r_sym,
2518     const Symbol_value<size>* psymval,
2519     typename elfcpp::Elf_types<size>::Elf_Swxword addend,
2520     Address address)
2521 {
2522   const AArch64_relobj<size, big_endian>* aarch64_relobj =
2523       static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
2524
2525   Symbol_value<size> symval;
2526   if (gsym != NULL)
2527     {
2528       const AArch64_reloc_property* arp = aarch64_reloc_property_table->
2529         get_reloc_property(r_type);
2530       if (gsym->use_plt_offset(arp->reference_flags()))
2531         {
2532           // This uses a PLT, change the symbol value.
2533           symval.set_output_value(this->plt_section()->address()
2534                                   + gsym->plt_offset());
2535           psymval = &symval;
2536         }
2537       else if (gsym->is_undefined())
2538         // There is no need to generate a stub symbol is undefined.
2539         return;
2540     }
2541
2542   // Get the symbol value.
2543   typename Symbol_value<size>::Value value = psymval->value(aarch64_relobj, 0);
2544
2545   // Owing to pipelining, the PC relative branches below actually skip
2546   // two instructions when the branch offset is 0.
2547   Address destination = static_cast<Address>(-1);
2548   switch (r_type)
2549     {
2550     case elfcpp::R_AARCH64_CALL26:
2551     case elfcpp::R_AARCH64_JUMP26:
2552       destination = value + addend;
2553       break;
2554     default:
2555       gold_unreachable();
2556     }
2557
2558   typename The_reloc_stub::Stub_type stub_type = The_reloc_stub::
2559       stub_type_for_reloc(r_type, address, destination);
2560   if (stub_type == The_reloc_stub::ST_NONE)
2561     return ;
2562
2563   The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
2564   gold_assert(stub_table != NULL);
2565
2566   The_reloc_stub_key key(stub_type, gsym, aarch64_relobj, r_sym, addend);
2567   The_reloc_stub* stub = stub_table->find_reloc_stub(key);
2568   if (stub == NULL)
2569     {
2570       stub = new The_reloc_stub(stub_type);
2571       stub_table->add_reloc_stub(stub, key);
2572     }
2573   stub->set_destination_address(destination);
2574 }  // End of Target_aarch64::scan_reloc_for_stub
2575
2576
2577 // This function scans a relocation section for stub generation.
2578 // The template parameter Relocate must be a class type which provides
2579 // a single function, relocate(), which implements the machine
2580 // specific part of a relocation.
2581
2582 // BIG_ENDIAN is the endianness of the data.  SH_TYPE is the section type:
2583 // SHT_REL or SHT_RELA.
2584
2585 // PRELOCS points to the relocation data.  RELOC_COUNT is the number
2586 // of relocs.  OUTPUT_SECTION is the output section.
2587 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
2588 // mapped to output offsets.
2589
2590 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
2591 // VIEW_SIZE is the size.  These refer to the input section, unless
2592 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
2593 // the output section.
2594
2595 template<int size, bool big_endian>
2596 template<int sh_type>
2597 void inline
2598 Target_aarch64<size, big_endian>::scan_reloc_section_for_stubs(
2599     const Relocate_info<size, big_endian>* relinfo,
2600     const unsigned char* prelocs,
2601     size_t reloc_count,
2602     Output_section* /*output_section*/,
2603     bool /*needs_special_offset_handling*/,
2604     const unsigned char* /*view*/,
2605     Address view_address,
2606     section_size_type)
2607 {
2608   typedef typename Reloc_types<sh_type,size,big_endian>::Reloc Reltype;
2609
2610   const int reloc_size =
2611       Reloc_types<sh_type,size,big_endian>::reloc_size;
2612   AArch64_relobj<size, big_endian>* object =
2613       static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
2614   unsigned int local_count = object->local_symbol_count();
2615
2616   gold::Default_comdat_behavior default_comdat_behavior;
2617   Comdat_behavior comdat_behavior = CB_UNDETERMINED;
2618
2619   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2620     {
2621       Reltype reloc(prelocs);
2622       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
2623       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2624       unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2625       if (r_type != elfcpp::R_AARCH64_CALL26
2626           && r_type != elfcpp::R_AARCH64_JUMP26)
2627         continue;
2628
2629       section_offset_type offset =
2630           convert_to_section_size_type(reloc.get_r_offset());
2631
2632       // Get the addend.
2633       typename elfcpp::Elf_types<size>::Elf_Swxword addend =
2634           reloc.get_r_addend();
2635
2636       const Sized_symbol<size>* sym;
2637       Symbol_value<size> symval;
2638       const Symbol_value<size> *psymval;
2639       bool is_defined_in_discarded_section;
2640       unsigned int shndx;
2641       if (r_sym < local_count)
2642         {
2643           sym = NULL;
2644           psymval = object->local_symbol(r_sym);
2645
2646           // If the local symbol belongs to a section we are discarding,
2647           // and that section is a debug section, try to find the
2648           // corresponding kept section and map this symbol to its
2649           // counterpart in the kept section.  The symbol must not
2650           // correspond to a section we are folding.
2651           bool is_ordinary;
2652           shndx = psymval->input_shndx(&is_ordinary);
2653           is_defined_in_discarded_section =
2654             (is_ordinary
2655              && shndx != elfcpp::SHN_UNDEF
2656              && !object->is_section_included(shndx)
2657              && !relinfo->symtab->is_section_folded(object, shndx));
2658
2659           // We need to compute the would-be final value of this local
2660           // symbol.
2661           if (!is_defined_in_discarded_section)
2662             {
2663               typedef Sized_relobj_file<size, big_endian> ObjType;
2664               typename ObjType::Compute_final_local_value_status status =
2665                 object->compute_final_local_value(r_sym, psymval, &symval,
2666                                                   relinfo->symtab);
2667               if (status == ObjType::CFLV_OK)
2668                 {
2669                   // Currently we cannot handle a branch to a target in
2670                   // a merged section.  If this is the case, issue an error
2671                   // and also free the merge symbol value.
2672                   if (!symval.has_output_value())
2673                     {
2674                       const std::string& section_name =
2675                         object->section_name(shndx);
2676                       object->error(_("cannot handle branch to local %u "
2677                                           "in a merged section %s"),
2678                                         r_sym, section_name.c_str());
2679                     }
2680                   psymval = &symval;
2681                 }
2682               else
2683                 {
2684                   // We cannot determine the final value.
2685                   continue;
2686                 }
2687             }
2688         }
2689       else
2690         {
2691           const Symbol* gsym;
2692           gsym = object->global_symbol(r_sym);
2693           gold_assert(gsym != NULL);
2694           if (gsym->is_forwarder())
2695             gsym = relinfo->symtab->resolve_forwards(gsym);
2696
2697           sym = static_cast<const Sized_symbol<size>*>(gsym);
2698           if (sym->has_symtab_index() && sym->symtab_index() != -1U)
2699             symval.set_output_symtab_index(sym->symtab_index());
2700           else
2701             symval.set_no_output_symtab_entry();
2702
2703           // We need to compute the would-be final value of this global
2704           // symbol.
2705           const Symbol_table* symtab = relinfo->symtab;
2706           const Sized_symbol<size>* sized_symbol =
2707               symtab->get_sized_symbol<size>(gsym);
2708           Symbol_table::Compute_final_value_status status;
2709           typename elfcpp::Elf_types<size>::Elf_Addr value =
2710               symtab->compute_final_value<size>(sized_symbol, &status);
2711
2712           // Skip this if the symbol has not output section.
2713           if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
2714             continue;
2715           symval.set_output_value(value);
2716
2717           if (gsym->type() == elfcpp::STT_TLS)
2718             symval.set_is_tls_symbol();
2719           else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2720             symval.set_is_ifunc_symbol();
2721           psymval = &symval;
2722
2723           is_defined_in_discarded_section =
2724               (gsym->is_defined_in_discarded_section()
2725                && gsym->is_undefined());
2726           shndx = 0;
2727         }
2728
2729       Symbol_value<size> symval2;
2730       if (is_defined_in_discarded_section)
2731         {
2732           if (comdat_behavior == CB_UNDETERMINED)
2733             {
2734               std::string name = object->section_name(relinfo->data_shndx);
2735               comdat_behavior = default_comdat_behavior.get(name.c_str());
2736             }
2737           if (comdat_behavior == CB_PRETEND)
2738             {
2739               bool found;
2740               typename elfcpp::Elf_types<size>::Elf_Addr value =
2741                 object->map_to_kept_section(shndx, &found);
2742               if (found)
2743                 symval2.set_output_value(value + psymval->input_value());
2744               else
2745                 symval2.set_output_value(0);
2746             }
2747           else
2748             {
2749               if (comdat_behavior == CB_WARNING)
2750                 gold_warning_at_location(relinfo, i, offset,
2751                                          _("relocation refers to discarded "
2752                                            "section"));
2753               symval2.set_output_value(0);
2754             }
2755           symval2.set_no_output_symtab_entry();
2756           psymval = &symval2;
2757         }
2758
2759       // If symbol is a section symbol, we don't know the actual type of
2760       // destination.  Give up.
2761       if (psymval->is_section_symbol())
2762         continue;
2763
2764       this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
2765                                 addend, view_address + offset);
2766     }  // End of iterating relocs in a section
2767 }  // End of Target_aarch64::scan_reloc_section_for_stubs
2768
2769
2770 // Scan an input section for stub generation.
2771
2772 template<int size, bool big_endian>
2773 void
2774 Target_aarch64<size, big_endian>::scan_section_for_stubs(
2775     const Relocate_info<size, big_endian>* relinfo,
2776     unsigned int sh_type,
2777     const unsigned char* prelocs,
2778     size_t reloc_count,
2779     Output_section* output_section,
2780     bool needs_special_offset_handling,
2781     const unsigned char* view,
2782     Address view_address,
2783     section_size_type view_size)
2784 {
2785   gold_assert(sh_type == elfcpp::SHT_RELA);
2786   this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
2787       relinfo,
2788       prelocs,
2789       reloc_count,
2790       output_section,
2791       needs_special_offset_handling,
2792       view,
2793       view_address,
2794       view_size);
2795 }
2796
2797
2798 // Relocate a single stub.
2799
2800 template<int size, bool big_endian>
2801 void Target_aarch64<size, big_endian>::
2802 relocate_stub(The_reloc_stub* stub,
2803               const The_relocate_info*,
2804               Output_section*,
2805               unsigned char* view,
2806               Address address,
2807               section_size_type)
2808 {
2809   typedef AArch64_relocate_functions<size, big_endian> The_reloc_functions;
2810   typedef typename The_reloc_functions::Status The_reloc_functions_status;
2811   typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
2812
2813   Insntype* ip = reinterpret_cast<Insntype*>(view);
2814   int insn_number = stub->stub_insn_number();
2815   const uint32_t* insns = stub->stub_insns();
2816   // Check the insns are really those stub insns.
2817   for (int i = 0; i < insn_number; ++i)
2818     {
2819       Insntype insn = elfcpp::Swap<32,big_endian>::readval(ip + i);
2820       gold_assert(((uint32_t)insn == insns[i+1]));
2821     }
2822
2823   Address dest = stub->destination_address();
2824
2825   switch(stub->stub_type())
2826     {
2827     case The_reloc_stub::ST_ADRP_BRANCH:
2828       {
2829         // 1st reloc is ADR_PREL_PG_HI21
2830         The_reloc_functions_status status =
2831             The_reloc_functions::adrp(view, dest, address);
2832         // An error should never arise in the above step. If so, please
2833         // check 'aarch64_valid_for_adrp_p'.
2834         gold_assert(status == The_reloc_functions::STATUS_OKAY);
2835
2836         // 2nd reloc is ADD_ABS_LO12_NC
2837         const AArch64_reloc_property* arp =
2838             aarch64_reloc_property_table->get_reloc_property(
2839                 elfcpp::R_AARCH64_ADD_ABS_LO12_NC);
2840         gold_assert(arp != NULL);
2841         status = The_reloc_functions::template
2842             rela_general<32>(view + 4, dest, 0, arp);
2843         // An error should never arise, it is an "_NC" relocation.
2844         gold_assert(status == The_reloc_functions::STATUS_OKAY);
2845       }
2846       break;
2847
2848     case The_reloc_stub::ST_LONG_BRANCH_ABS:
2849       // 1st reloc is R_AARCH64_PREL64, at offset 8
2850       elfcpp::Swap<64,big_endian>::writeval(view + 8, dest);
2851       break;
2852
2853     case The_reloc_stub::ST_LONG_BRANCH_PCREL:
2854       {
2855         // "PC" calculation is the 2nd insn in the stub.
2856         uint64_t offset = dest - (address + 4);
2857         // Offset is placed at offset 4 and 5.
2858         elfcpp::Swap<64,big_endian>::writeval(view + 16, offset);
2859       }
2860       break;
2861
2862     default:
2863       gold_unreachable();
2864     }
2865 }
2866
2867
2868 // A class to handle the PLT data.
2869 // This is an abstract base class that handles most of the linker details
2870 // but does not know the actual contents of PLT entries.  The derived
2871 // classes below fill in those details.
2872
2873 template<int size, bool big_endian>
2874 class Output_data_plt_aarch64 : public Output_section_data
2875 {
2876  public:
2877   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
2878       Reloc_section;
2879   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2880
2881   Output_data_plt_aarch64(Layout* layout,
2882                           uint64_t addralign,
2883                           Output_data_got_aarch64<size, big_endian>* got,
2884                           Output_data_space* got_plt,
2885                           Output_data_space* got_irelative)
2886     : Output_section_data(addralign), tlsdesc_rel_(NULL), irelative_rel_(NULL),
2887       got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
2888       count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
2889   { this->init(layout); }
2890
2891   // Initialize the PLT section.
2892   void
2893   init(Layout* layout);
2894
2895   // Add an entry to the PLT.
2896   void
2897   add_entry(Symbol_table*, Layout*, Symbol* gsym);
2898
2899   // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
2900   unsigned int
2901   add_local_ifunc_entry(Symbol_table* symtab, Layout*,
2902                         Sized_relobj_file<size, big_endian>* relobj,
2903                         unsigned int local_sym_index);
2904
2905   // Add the relocation for a PLT entry.
2906   void
2907   add_relocation(Symbol_table*, Layout*, Symbol* gsym,
2908                  unsigned int got_offset);
2909
2910   // Add the reserved TLSDESC_PLT entry to the PLT.
2911   void
2912   reserve_tlsdesc_entry(unsigned int got_offset)
2913   { this->tlsdesc_got_offset_ = got_offset; }
2914
2915   // Return true if a TLSDESC_PLT entry has been reserved.
2916   bool
2917   has_tlsdesc_entry() const
2918   { return this->tlsdesc_got_offset_ != -1U; }
2919
2920   // Return the GOT offset for the reserved TLSDESC_PLT entry.
2921   unsigned int
2922   get_tlsdesc_got_offset() const
2923   { return this->tlsdesc_got_offset_; }
2924
2925   // Return the PLT offset of the reserved TLSDESC_PLT entry.
2926   unsigned int
2927   get_tlsdesc_plt_offset() const
2928   {
2929     return (this->first_plt_entry_offset() +
2930             (this->count_ + this->irelative_count_)
2931             * this->get_plt_entry_size());
2932   }
2933
2934   // Return the .rela.plt section data.
2935   Reloc_section*
2936   rela_plt()
2937   { return this->rel_; }
2938
2939   // Return where the TLSDESC relocations should go.
2940   Reloc_section*
2941   rela_tlsdesc(Layout*);
2942
2943   // Return where the IRELATIVE relocations should go in the PLT
2944   // relocations.
2945   Reloc_section*
2946   rela_irelative(Symbol_table*, Layout*);
2947
2948   // Return whether we created a section for IRELATIVE relocations.
2949   bool
2950   has_irelative_section() const
2951   { return this->irelative_rel_ != NULL; }
2952
2953   // Return the number of PLT entries.
2954   unsigned int
2955   entry_count() const
2956   { return this->count_ + this->irelative_count_; }
2957
2958   // Return the offset of the first non-reserved PLT entry.
2959   unsigned int
2960   first_plt_entry_offset() const
2961   { return this->do_first_plt_entry_offset(); }
2962
2963   // Return the size of a PLT entry.
2964   unsigned int
2965   get_plt_entry_size() const
2966   { return this->do_get_plt_entry_size(); }
2967
2968   // Return the reserved tlsdesc entry size.
2969   unsigned int
2970   get_plt_tlsdesc_entry_size() const
2971   { return this->do_get_plt_tlsdesc_entry_size(); }
2972
2973   // Return the PLT address to use for a global symbol.
2974   uint64_t
2975   address_for_global(const Symbol*);
2976
2977   // Return the PLT address to use for a local symbol.
2978   uint64_t
2979   address_for_local(const Relobj*, unsigned int symndx);
2980
2981  protected:
2982   // Fill in the first PLT entry.
2983   void
2984   fill_first_plt_entry(unsigned char* pov,
2985                        Address got_address,
2986                        Address plt_address)
2987   { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
2988
2989   // Fill in a normal PLT entry.
2990   void
2991   fill_plt_entry(unsigned char* pov,
2992                  Address got_address,
2993                  Address plt_address,
2994                  unsigned int got_offset,
2995                  unsigned int plt_offset)
2996   {
2997     this->do_fill_plt_entry(pov, got_address, plt_address,
2998                             got_offset, plt_offset);
2999   }
3000
3001   // Fill in the reserved TLSDESC PLT entry.
3002   void
3003   fill_tlsdesc_entry(unsigned char* pov,
3004                      Address gotplt_address,
3005                      Address plt_address,
3006                      Address got_base,
3007                      unsigned int tlsdesc_got_offset,
3008                      unsigned int plt_offset)
3009   {
3010     this->do_fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
3011                                 tlsdesc_got_offset, plt_offset);
3012   }
3013
3014   virtual unsigned int
3015   do_first_plt_entry_offset() const = 0;
3016
3017   virtual unsigned int
3018   do_get_plt_entry_size() const = 0;
3019
3020   virtual unsigned int
3021   do_get_plt_tlsdesc_entry_size() const = 0;
3022
3023   virtual void
3024   do_fill_first_plt_entry(unsigned char* pov,
3025                           Address got_addr,
3026                           Address plt_addr) = 0;
3027
3028   virtual void
3029   do_fill_plt_entry(unsigned char* pov,
3030                     Address got_address,
3031                     Address plt_address,
3032                     unsigned int got_offset,
3033                     unsigned int plt_offset) = 0;
3034
3035   virtual void
3036   do_fill_tlsdesc_entry(unsigned char* pov,
3037                         Address gotplt_address,
3038                         Address plt_address,
3039                         Address got_base,
3040                         unsigned int tlsdesc_got_offset,
3041                         unsigned int plt_offset) = 0;
3042
3043   void
3044   do_adjust_output_section(Output_section* os);
3045
3046   // Write to a map file.
3047   void
3048   do_print_to_mapfile(Mapfile* mapfile) const
3049   { mapfile->print_output_data(this, _("** PLT")); }
3050
3051  private:
3052   // Set the final size.
3053   void
3054   set_final_data_size();
3055
3056   // Write out the PLT data.
3057   void
3058   do_write(Output_file*);
3059
3060   // The reloc section.
3061   Reloc_section* rel_;
3062
3063   // The TLSDESC relocs, if necessary.  These must follow the regular
3064   // PLT relocs.
3065   Reloc_section* tlsdesc_rel_;
3066
3067   // The IRELATIVE relocs, if necessary.  These must follow the
3068   // regular PLT relocations.
3069   Reloc_section* irelative_rel_;
3070
3071   // The .got section.
3072   Output_data_got_aarch64<size, big_endian>* got_;
3073
3074   // The .got.plt section.
3075   Output_data_space* got_plt_;
3076
3077   // The part of the .got.plt section used for IRELATIVE relocs.
3078   Output_data_space* got_irelative_;
3079
3080   // The number of PLT entries.
3081   unsigned int count_;
3082
3083   // Number of PLT entries with R_X86_64_IRELATIVE relocs.  These
3084   // follow the regular PLT entries.
3085   unsigned int irelative_count_;
3086
3087   // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
3088   // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
3089   // indicates an offset is not allocated.
3090   unsigned int tlsdesc_got_offset_;
3091 };
3092
3093 // Initialize the PLT section.
3094
3095 template<int size, bool big_endian>
3096 void
3097 Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
3098 {
3099   this->rel_ = new Reloc_section(false);
3100   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3101                                   elfcpp::SHF_ALLOC, this->rel_,
3102                                   ORDER_DYNAMIC_PLT_RELOCS, false);
3103 }
3104
3105 template<int size, bool big_endian>
3106 void
3107 Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
3108     Output_section* os)
3109 {
3110   os->set_entsize(this->get_plt_entry_size());
3111 }
3112
3113 // Add an entry to the PLT.
3114
3115 template<int size, bool big_endian>
3116 void
3117 Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol_table* symtab,
3118     Layout* layout, Symbol* gsym)
3119 {
3120   gold_assert(!gsym->has_plt_offset());
3121
3122   unsigned int* pcount;
3123   unsigned int plt_reserved;
3124   Output_section_data_build* got;
3125
3126   if (gsym->type() == elfcpp::STT_GNU_IFUNC
3127       && gsym->can_use_relative_reloc(false))
3128     {
3129       pcount = &this->irelative_count_;
3130       plt_reserved = 0;
3131       got = this->got_irelative_;
3132     }
3133   else
3134     {
3135       pcount = &this->count_;
3136       plt_reserved = this->first_plt_entry_offset();
3137       got = this->got_plt_;
3138     }
3139
3140   gsym->set_plt_offset((*pcount) * this->get_plt_entry_size()
3141                        + plt_reserved);
3142
3143   ++*pcount;
3144
3145   section_offset_type got_offset = got->current_data_size();
3146
3147   // Every PLT entry needs a GOT entry which points back to the PLT
3148   // entry (this will be changed by the dynamic linker, normally
3149   // lazily when the function is called).
3150   got->set_current_data_size(got_offset + size / 8);
3151
3152   // Every PLT entry needs a reloc.
3153   this->add_relocation(symtab, layout, gsym, got_offset);
3154
3155   // Note that we don't need to save the symbol. The contents of the
3156   // PLT are independent of which symbols are used. The symbols only
3157   // appear in the relocations.
3158 }
3159
3160 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.  Return
3161 // the PLT offset.
3162
3163 template<int size, bool big_endian>
3164 unsigned int
3165 Output_data_plt_aarch64<size, big_endian>::add_local_ifunc_entry(
3166     Symbol_table* symtab,
3167     Layout* layout,
3168     Sized_relobj_file<size, big_endian>* relobj,
3169     unsigned int local_sym_index)
3170 {
3171   unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
3172   ++this->irelative_count_;
3173
3174   section_offset_type got_offset = this->got_irelative_->current_data_size();
3175
3176   // Every PLT entry needs a GOT entry which points back to the PLT
3177   // entry.
3178   this->got_irelative_->set_current_data_size(got_offset + size / 8);
3179
3180   // Every PLT entry needs a reloc.
3181   Reloc_section* rela = this->rela_irelative(symtab, layout);
3182   rela->add_symbolless_local_addend(relobj, local_sym_index,
3183                                     elfcpp::R_AARCH64_IRELATIVE,
3184                                     this->got_irelative_, got_offset, 0);
3185
3186   return plt_offset;
3187 }
3188
3189 // Add the relocation for a PLT entry.
3190
3191 template<int size, bool big_endian>
3192 void
3193 Output_data_plt_aarch64<size, big_endian>::add_relocation(
3194     Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
3195 {
3196   if (gsym->type() == elfcpp::STT_GNU_IFUNC
3197       && gsym->can_use_relative_reloc(false))
3198     {
3199       Reloc_section* rela = this->rela_irelative(symtab, layout);
3200       rela->add_symbolless_global_addend(gsym, elfcpp::R_AARCH64_IRELATIVE,
3201                                          this->got_irelative_, got_offset, 0);
3202     }
3203   else
3204     {
3205       gsym->set_needs_dynsym_entry();
3206       this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT, this->got_plt_,
3207                              got_offset, 0);
3208     }
3209 }
3210
3211 // Return where the TLSDESC relocations should go, creating it if
3212 // necessary.  These follow the JUMP_SLOT relocations.
3213
3214 template<int size, bool big_endian>
3215 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
3216 Output_data_plt_aarch64<size, big_endian>::rela_tlsdesc(Layout* layout)
3217 {
3218   if (this->tlsdesc_rel_ == NULL)
3219     {
3220       this->tlsdesc_rel_ = new Reloc_section(false);
3221       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3222                                       elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
3223                                       ORDER_DYNAMIC_PLT_RELOCS, false);
3224       gold_assert(this->tlsdesc_rel_->output_section()
3225                   == this->rel_->output_section());
3226     }
3227   return this->tlsdesc_rel_;
3228 }
3229
3230 // Return where the IRELATIVE relocations should go in the PLT.  These
3231 // follow the JUMP_SLOT and the TLSDESC relocations.
3232
3233 template<int size, bool big_endian>
3234 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
3235 Output_data_plt_aarch64<size, big_endian>::rela_irelative(Symbol_table* symtab,
3236                                                           Layout* layout)
3237 {
3238   if (this->irelative_rel_ == NULL)
3239     {
3240       // Make sure we have a place for the TLSDESC relocations, in
3241       // case we see any later on.
3242       this->rela_tlsdesc(layout);
3243       this->irelative_rel_ = new Reloc_section(false);
3244       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3245                                       elfcpp::SHF_ALLOC, this->irelative_rel_,
3246                                       ORDER_DYNAMIC_PLT_RELOCS, false);
3247       gold_assert(this->irelative_rel_->output_section()
3248                   == this->rel_->output_section());
3249
3250       if (parameters->doing_static_link())
3251         {
3252           // A statically linked executable will only have a .rela.plt
3253           // section to hold R_AARCH64_IRELATIVE relocs for
3254           // STT_GNU_IFUNC symbols.  The library will use these
3255           // symbols to locate the IRELATIVE relocs at program startup
3256           // time.
3257           symtab->define_in_output_data("__rela_iplt_start", NULL,
3258                                         Symbol_table::PREDEFINED,
3259                                         this->irelative_rel_, 0, 0,
3260                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
3261                                         elfcpp::STV_HIDDEN, 0, false, true);
3262           symtab->define_in_output_data("__rela_iplt_end", NULL,
3263                                         Symbol_table::PREDEFINED,
3264                                         this->irelative_rel_, 0, 0,
3265                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
3266                                         elfcpp::STV_HIDDEN, 0, true, true);
3267         }
3268     }
3269   return this->irelative_rel_;
3270 }
3271
3272 // Return the PLT address to use for a global symbol.
3273
3274 template<int size, bool big_endian>
3275 uint64_t
3276 Output_data_plt_aarch64<size, big_endian>::address_for_global(
3277   const Symbol* gsym)
3278 {
3279   uint64_t offset = 0;
3280   if (gsym->type() == elfcpp::STT_GNU_IFUNC
3281       && gsym->can_use_relative_reloc(false))
3282     offset = (this->first_plt_entry_offset() +
3283               this->count_ * this->get_plt_entry_size());
3284   return this->address() + offset + gsym->plt_offset();
3285 }
3286
3287 // Return the PLT address to use for a local symbol.  These are always
3288 // IRELATIVE relocs.
3289
3290 template<int size, bool big_endian>
3291 uint64_t
3292 Output_data_plt_aarch64<size, big_endian>::address_for_local(
3293     const Relobj* object,
3294     unsigned int r_sym)
3295 {
3296   return (this->address()
3297           + this->first_plt_entry_offset()
3298           + this->count_ * this->get_plt_entry_size()
3299           + object->local_plt_offset(r_sym));
3300 }
3301
3302 // Set the final size.
3303
3304 template<int size, bool big_endian>
3305 void
3306 Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
3307 {
3308   unsigned int count = this->count_ + this->irelative_count_;
3309   unsigned int extra_size = 0;
3310   if (this->has_tlsdesc_entry())
3311     extra_size += this->get_plt_tlsdesc_entry_size();
3312   this->set_data_size(this->first_plt_entry_offset()
3313                       + count * this->get_plt_entry_size()
3314                       + extra_size);
3315 }
3316
3317 template<int size, bool big_endian>
3318 class Output_data_plt_aarch64_standard :
3319   public Output_data_plt_aarch64<size, big_endian>
3320 {
3321  public:
3322   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3323   Output_data_plt_aarch64_standard(
3324       Layout* layout,
3325       Output_data_got_aarch64<size, big_endian>* got,
3326       Output_data_space* got_plt,
3327       Output_data_space* got_irelative)
3328     : Output_data_plt_aarch64<size, big_endian>(layout,
3329                                                 size == 32 ? 4 : 8,
3330                                                 got, got_plt,
3331                                                 got_irelative)
3332   { }
3333
3334  protected:
3335   // Return the offset of the first non-reserved PLT entry.
3336   virtual unsigned int
3337   do_first_plt_entry_offset() const
3338   { return this->first_plt_entry_size; }
3339
3340   // Return the size of a PLT entry
3341   virtual unsigned int
3342   do_get_plt_entry_size() const
3343   { return this->plt_entry_size; }
3344
3345   // Return the size of a tlsdesc entry
3346   virtual unsigned int
3347   do_get_plt_tlsdesc_entry_size() const
3348   { return this->plt_tlsdesc_entry_size; }
3349
3350   virtual void
3351   do_fill_first_plt_entry(unsigned char* pov,
3352                           Address got_address,
3353                           Address plt_address);
3354
3355   virtual void
3356   do_fill_plt_entry(unsigned char* pov,
3357                     Address got_address,
3358                     Address plt_address,
3359                     unsigned int got_offset,
3360                     unsigned int plt_offset);
3361
3362   virtual void
3363   do_fill_tlsdesc_entry(unsigned char* pov,
3364                         Address gotplt_address,
3365                         Address plt_address,
3366                         Address got_base,
3367                         unsigned int tlsdesc_got_offset,
3368                         unsigned int plt_offset);
3369
3370  private:
3371   // The size of the first plt entry size.
3372   static const int first_plt_entry_size = 32;
3373   // The size of the plt entry size.
3374   static const int plt_entry_size = 16;
3375   // The size of the plt tlsdesc entry size.
3376   static const int plt_tlsdesc_entry_size = 32;
3377   // Template for the first PLT entry.
3378   static const uint32_t first_plt_entry[first_plt_entry_size / 4];
3379   // Template for subsequent PLT entries.
3380   static const uint32_t plt_entry[plt_entry_size / 4];
3381   // The reserved TLSDESC entry in the PLT for an executable.
3382   static const uint32_t tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4];
3383 };
3384
3385 // The first entry in the PLT for an executable.
3386
3387 template<>
3388 const uint32_t
3389 Output_data_plt_aarch64_standard<32, false>::
3390     first_plt_entry[first_plt_entry_size / 4] =
3391 {
3392   0xa9bf7bf0,   /* stp x16, x30, [sp, #-16]!  */
3393   0x90000010,   /* adrp x16, PLT_GOT+0x8  */
3394   0xb9400A11,   /* ldr w17, [x16, #PLT_GOT+0x8]  */
3395   0x11002210,   /* add w16, w16,#PLT_GOT+0x8   */
3396   0xd61f0220,   /* br x17  */
3397   0xd503201f,   /* nop */
3398   0xd503201f,   /* nop */
3399   0xd503201f,   /* nop */
3400 };
3401
3402
3403 template<>
3404 const uint32_t
3405 Output_data_plt_aarch64_standard<32, true>::
3406     first_plt_entry[first_plt_entry_size / 4] =
3407 {
3408   0xa9bf7bf0,   /* stp x16, x30, [sp, #-16]!  */
3409   0x90000010,   /* adrp x16, PLT_GOT+0x8  */
3410   0xb9400A11,   /* ldr w17, [x16, #PLT_GOT+0x8]  */
3411   0x11002210,   /* add w16, w16,#PLT_GOT+0x8   */
3412   0xd61f0220,   /* br x17  */
3413   0xd503201f,   /* nop */
3414   0xd503201f,   /* nop */
3415   0xd503201f,   /* nop */
3416 };
3417
3418
3419 template<>
3420 const uint32_t
3421 Output_data_plt_aarch64_standard<64, false>::
3422     first_plt_entry[first_plt_entry_size / 4] =
3423 {
3424   0xa9bf7bf0,   /* stp x16, x30, [sp, #-16]!  */
3425   0x90000010,   /* adrp x16, PLT_GOT+16  */
3426   0xf9400A11,   /* ldr x17, [x16, #PLT_GOT+0x10]  */
3427   0x91004210,   /* add x16, x16,#PLT_GOT+0x10   */
3428   0xd61f0220,   /* br x17  */
3429   0xd503201f,   /* nop */
3430   0xd503201f,   /* nop */
3431   0xd503201f,   /* nop */
3432 };
3433
3434
3435 template<>
3436 const uint32_t
3437 Output_data_plt_aarch64_standard<64, true>::
3438     first_plt_entry[first_plt_entry_size / 4] =
3439 {
3440   0xa9bf7bf0,   /* stp x16, x30, [sp, #-16]!  */
3441   0x90000010,   /* adrp x16, PLT_GOT+16  */
3442   0xf9400A11,   /* ldr x17, [x16, #PLT_GOT+0x10]  */
3443   0x91004210,   /* add x16, x16,#PLT_GOT+0x10   */
3444   0xd61f0220,   /* br x17  */
3445   0xd503201f,   /* nop */
3446   0xd503201f,   /* nop */
3447   0xd503201f,   /* nop */
3448 };
3449
3450
3451 template<>
3452 const uint32_t
3453 Output_data_plt_aarch64_standard<32, false>::
3454     plt_entry[plt_entry_size / 4] =
3455 {
3456   0x90000010,   /* adrp x16, PLTGOT + n * 4  */
3457   0xb9400211,   /* ldr w17, [w16, PLTGOT + n * 4] */
3458   0x11000210,   /* add w16, w16, :lo12:PLTGOT + n * 4  */
3459   0xd61f0220,   /* br x17.  */
3460 };
3461
3462
3463 template<>
3464 const uint32_t
3465 Output_data_plt_aarch64_standard<32, true>::
3466     plt_entry[plt_entry_size / 4] =
3467 {
3468   0x90000010,   /* adrp x16, PLTGOT + n * 4  */
3469   0xb9400211,   /* ldr w17, [w16, PLTGOT + n * 4] */
3470   0x11000210,   /* add w16, w16, :lo12:PLTGOT + n * 4  */
3471   0xd61f0220,   /* br x17.  */
3472 };
3473
3474
3475 template<>
3476 const uint32_t
3477 Output_data_plt_aarch64_standard<64, false>::
3478     plt_entry[plt_entry_size / 4] =
3479 {
3480   0x90000010,   /* adrp x16, PLTGOT + n * 8  */
3481   0xf9400211,   /* ldr x17, [x16, PLTGOT + n * 8] */
3482   0x91000210,   /* add x16, x16, :lo12:PLTGOT + n * 8  */
3483   0xd61f0220,   /* br x17.  */
3484 };
3485
3486
3487 template<>
3488 const uint32_t
3489 Output_data_plt_aarch64_standard<64, true>::
3490     plt_entry[plt_entry_size / 4] =
3491 {
3492   0x90000010,   /* adrp x16, PLTGOT + n * 8  */
3493   0xf9400211,   /* ldr x17, [x16, PLTGOT + n * 8] */
3494   0x91000210,   /* add x16, x16, :lo12:PLTGOT + n * 8  */
3495   0xd61f0220,   /* br x17.  */
3496 };
3497
3498
3499 template<int size, bool big_endian>
3500 void
3501 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
3502     unsigned char* pov,
3503     Address got_address,
3504     Address plt_address)
3505 {
3506   // PLT0 of the small PLT looks like this in ELF64 -
3507   // stp x16, x30, [sp, #-16]!          Save the reloc and lr on stack.
3508   // adrp x16, PLT_GOT + 16             Get the page base of the GOTPLT
3509   // ldr  x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
3510   //                                    symbol resolver
3511   // add  x16, x16, #:lo12:PLT_GOT+16   Load the lo12 bits of the
3512   //                                    GOTPLT entry for this.
3513   // br   x17
3514   // PLT0 will be slightly different in ELF32 due to different got entry
3515   // size.
3516   memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
3517   Address gotplt_2nd_ent = got_address + (size / 8) * 2;
3518
3519   // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
3520   // ADRP:  (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
3521   // FIXME: This only works for 64bit
3522   AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
3523       gotplt_2nd_ent, plt_address + 4);
3524
3525   // Fill in R_AARCH64_LDST8_LO12
3526   elfcpp::Swap<32, big_endian>::writeval(
3527       pov + 8,
3528       ((this->first_plt_entry[2] & 0xffc003ff)
3529        | ((gotplt_2nd_ent & 0xff8) << 7)));
3530
3531   // Fill in R_AARCH64_ADD_ABS_LO12
3532   elfcpp::Swap<32, big_endian>::writeval(
3533       pov + 12,
3534       ((this->first_plt_entry[3] & 0xffc003ff)
3535        | ((gotplt_2nd_ent & 0xfff) << 10)));
3536 }
3537
3538
3539 // Subsequent entries in the PLT for an executable.
3540 // FIXME: This only works for 64bit
3541
3542 template<int size, bool big_endian>
3543 void
3544 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
3545     unsigned char* pov,
3546     Address got_address,
3547     Address plt_address,
3548     unsigned int got_offset,
3549     unsigned int plt_offset)
3550 {
3551   memcpy(pov, this->plt_entry, this->plt_entry_size);
3552
3553   Address gotplt_entry_address = got_address + got_offset;
3554   Address plt_entry_address = plt_address + plt_offset;
3555
3556   // Fill in R_AARCH64_PCREL_ADR_HI21
3557   AArch64_relocate_functions<size, big_endian>::adrp(
3558       pov,
3559       gotplt_entry_address,
3560       plt_entry_address);
3561
3562   // Fill in R_AARCH64_LDST64_ABS_LO12
3563   elfcpp::Swap<32, big_endian>::writeval(
3564       pov + 4,
3565       ((this->plt_entry[1] & 0xffc003ff)
3566        | ((gotplt_entry_address & 0xff8) << 7)));
3567
3568   // Fill in R_AARCH64_ADD_ABS_LO12
3569   elfcpp::Swap<32, big_endian>::writeval(
3570       pov + 8,
3571       ((this->plt_entry[2] & 0xffc003ff)
3572        | ((gotplt_entry_address & 0xfff) <<10)));
3573
3574 }
3575
3576
3577 template<>
3578 const uint32_t
3579 Output_data_plt_aarch64_standard<32, false>::
3580     tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
3581 {
3582   0xa9bf0fe2,   /* stp x2, x3, [sp, #-16]!  */
3583   0x90000002,   /* adrp x2, 0 */
3584   0x90000003,   /* adrp x3, 0 */
3585   0xb9400042,   /* ldr w2, [w2, #0] */
3586   0x11000063,   /* add w3, w3, 0 */
3587   0xd61f0040,   /* br x2 */
3588   0xd503201f,   /* nop */
3589   0xd503201f,   /* nop */
3590 };
3591
3592 template<>
3593 const uint32_t
3594 Output_data_plt_aarch64_standard<32, true>::
3595     tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
3596 {
3597   0xa9bf0fe2,   /* stp x2, x3, [sp, #-16]!  */
3598   0x90000002,   /* adrp x2, 0 */
3599   0x90000003,   /* adrp x3, 0 */
3600   0xb9400042,   /* ldr w2, [w2, #0] */
3601   0x11000063,   /* add w3, w3, 0 */
3602   0xd61f0040,   /* br x2 */
3603   0xd503201f,   /* nop */
3604   0xd503201f,   /* nop */
3605 };
3606
3607 template<>
3608 const uint32_t
3609 Output_data_plt_aarch64_standard<64, false>::
3610     tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
3611 {
3612   0xa9bf0fe2,   /* stp x2, x3, [sp, #-16]!  */
3613   0x90000002,   /* adrp x2, 0 */
3614   0x90000003,   /* adrp x3, 0 */
3615   0xf9400042,   /* ldr x2, [x2, #0] */
3616   0x91000063,   /* add x3, x3, 0 */
3617   0xd61f0040,   /* br x2 */
3618   0xd503201f,   /* nop */
3619   0xd503201f,   /* nop */
3620 };
3621
3622 template<>
3623 const uint32_t
3624 Output_data_plt_aarch64_standard<64, true>::
3625     tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
3626 {
3627   0xa9bf0fe2,   /* stp x2, x3, [sp, #-16]!  */
3628   0x90000002,   /* adrp x2, 0 */
3629   0x90000003,   /* adrp x3, 0 */
3630   0xf9400042,   /* ldr x2, [x2, #0] */
3631   0x91000063,   /* add x3, x3, 0 */
3632   0xd61f0040,   /* br x2 */
3633   0xd503201f,   /* nop */
3634   0xd503201f,   /* nop */
3635 };
3636
3637 template<int size, bool big_endian>
3638 void
3639 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_tlsdesc_entry(
3640     unsigned char* pov,
3641     Address gotplt_address,
3642     Address plt_address,
3643     Address got_base,
3644     unsigned int tlsdesc_got_offset,
3645     unsigned int plt_offset)
3646 {
3647   memcpy(pov, tlsdesc_plt_entry, plt_tlsdesc_entry_size);
3648
3649   // move DT_TLSDESC_GOT address into x2
3650   // move .got.plt address into x3
3651   Address tlsdesc_got_entry = got_base + tlsdesc_got_offset;
3652   Address plt_entry_address = plt_address + plt_offset;
3653
3654   // R_AARCH64_ADR_PREL_PG_HI21
3655   AArch64_relocate_functions<size, big_endian>::adrp(
3656       pov + 4,
3657       tlsdesc_got_entry,
3658       plt_entry_address + 4);
3659
3660   // R_AARCH64_ADR_PREL_PG_HI21
3661   AArch64_relocate_functions<size, big_endian>::adrp(
3662       pov + 8,
3663       gotplt_address,
3664       plt_entry_address + 8);
3665
3666   // R_AARCH64_LDST64_ABS_LO12
3667   elfcpp::Swap<32, big_endian>::writeval(
3668       pov + 12,
3669       ((this->tlsdesc_plt_entry[3] & 0xffc003ff)
3670        | ((tlsdesc_got_entry & 0xff8) << 7)));
3671
3672   // R_AARCH64_ADD_ABS_LO12
3673   elfcpp::Swap<32, big_endian>::writeval(
3674       pov + 16,
3675       ((this->tlsdesc_plt_entry[4] & 0xffc003ff)
3676        | ((gotplt_address & 0xfff) << 10)));
3677 }
3678
3679 // Write out the PLT.  This uses the hand-coded instructions above,
3680 // and adjusts them as needed.  This is specified by the AMD64 ABI.
3681
3682 template<int size, bool big_endian>
3683 void
3684 Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
3685 {
3686   const off_t offset = this->offset();
3687   const section_size_type oview_size =
3688     convert_to_section_size_type(this->data_size());
3689   unsigned char* const oview = of->get_output_view(offset, oview_size);
3690
3691   const off_t got_file_offset = this->got_plt_->offset();
3692   gold_assert(got_file_offset + this->got_plt_->data_size()
3693               == this->got_irelative_->offset());
3694
3695   const section_size_type got_size =
3696       convert_to_section_size_type(this->got_plt_->data_size()
3697                                    + this->got_irelative_->data_size());
3698   unsigned char* const got_view = of->get_output_view(got_file_offset,
3699                                                       got_size);
3700
3701   unsigned char* pov = oview;
3702
3703   // The base address of the .plt section.
3704   typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
3705   // The base address of the PLT portion of the .got section.
3706   typename elfcpp::Elf_types<size>::Elf_Addr gotplt_address
3707       = this->got_plt_->address();
3708
3709   this->fill_first_plt_entry(pov, gotplt_address, plt_address);
3710   pov += this->first_plt_entry_offset();
3711
3712   // The first three entries in .got.plt are reserved.
3713   unsigned char* got_pov = got_view;
3714   memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
3715   got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
3716
3717   unsigned int plt_offset = this->first_plt_entry_offset();
3718   unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
3719   const unsigned int count = this->count_ + this->irelative_count_;
3720   for (unsigned int plt_index = 0;
3721        plt_index < count;
3722        ++plt_index,
3723          pov += this->get_plt_entry_size(),
3724          got_pov += size / 8,
3725          plt_offset += this->get_plt_entry_size(),
3726          got_offset += size / 8)
3727     {
3728       // Set and adjust the PLT entry itself.
3729       this->fill_plt_entry(pov, gotplt_address, plt_address,
3730                            got_offset, plt_offset);
3731
3732       // Set the entry in the GOT, which points to plt0.
3733       elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
3734     }
3735
3736   if (this->has_tlsdesc_entry())
3737     {
3738       // Set and adjust the reserved TLSDESC PLT entry.
3739       unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
3740       // The base address of the .base section.
3741       typename elfcpp::Elf_types<size>::Elf_Addr got_base =
3742           this->got_->address();
3743       this->fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
3744                                tlsdesc_got_offset, plt_offset);
3745       pov += this->get_plt_tlsdesc_entry_size();
3746     }
3747
3748   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
3749   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
3750
3751   of->write_output_view(offset, oview_size, oview);
3752   of->write_output_view(got_file_offset, got_size, got_view);
3753 }
3754
3755 // Telling how to update the immediate field of an instruction.
3756 struct AArch64_howto
3757 {
3758   // The immediate field mask.
3759   elfcpp::Elf_Xword dst_mask;
3760
3761   // The offset to apply relocation immediate
3762   int doffset;
3763
3764   // The second part offset, if the immediate field has two parts.
3765   // -1 if the immediate field has only one part.
3766   int doffset2;
3767 };
3768
3769 static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
3770 {
3771   {0, -1, -1},          // DATA
3772   {0x1fffe0, 5, -1},    // MOVW  [20:5]-imm16
3773   {0xffffe0, 5, -1},    // LD    [23:5]-imm19
3774   {0x60ffffe0, 29, 5},  // ADR   [30:29]-immlo  [23:5]-immhi
3775   {0x60ffffe0, 29, 5},  // ADRP  [30:29]-immlo  [23:5]-immhi
3776   {0x3ffc00, 10, -1},   // ADD   [21:10]-imm12
3777   {0x3ffc00, 10, -1},   // LDST  [21:10]-imm12
3778   {0x7ffe0, 5, -1},     // TBZNZ [18:5]-imm14
3779   {0xffffe0, 5, -1},    // CONDB [23:5]-imm19
3780   {0x3ffffff, 0, -1},   // B     [25:0]-imm26
3781   {0x3ffffff, 0, -1},   // CALL  [25:0]-imm26
3782 };
3783
3784 // AArch64 relocate function class
3785
3786 template<int size, bool big_endian>
3787 class AArch64_relocate_functions
3788 {
3789  public:
3790   typedef enum
3791   {
3792     STATUS_OKAY,        // No error during relocation.
3793     STATUS_OVERFLOW,    // Relocation overflow.
3794     STATUS_BAD_RELOC,   // Relocation cannot be applied.
3795   } Status;
3796
3797   typedef AArch64_relocate_functions<size, big_endian> This;
3798   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3799   typedef Relocate_info<size, big_endian> The_relocate_info;
3800   typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
3801   typedef Reloc_stub<size, big_endian> The_reloc_stub;
3802   typedef typename The_reloc_stub::Stub_type The_reloc_stub_type;
3803   typedef Stub_table<size, big_endian> The_stub_table;
3804   typedef elfcpp::Rela<size, big_endian> The_rela;
3805   typedef typename elfcpp::Swap<size, big_endian>::Valtype AArch64_valtype;
3806
3807   // Return the page address of the address.
3808   // Page(address) = address & ~0xFFF
3809
3810   static inline AArch64_valtype
3811   Page(Address address)
3812   {
3813     return (address & (~static_cast<Address>(0xFFF)));
3814   }
3815
3816  private:
3817   // Update instruction (pointed by view) with selected bits (immed).
3818   // val = (val & ~dst_mask) | (immed << doffset)
3819
3820   template<int valsize>
3821   static inline void
3822   update_view(unsigned char* view,
3823               AArch64_valtype immed,
3824               elfcpp::Elf_Xword doffset,
3825               elfcpp::Elf_Xword dst_mask)
3826   {
3827     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
3828     Valtype* wv = reinterpret_cast<Valtype*>(view);
3829     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
3830
3831     // Clear immediate fields.
3832     val &= ~dst_mask;
3833     elfcpp::Swap<valsize, big_endian>::writeval(wv,
3834       static_cast<Valtype>(val | (immed << doffset)));
3835   }
3836
3837   // Update two parts of an instruction (pointed by view) with selected
3838   // bits (immed1 and immed2).
3839   // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
3840
3841   template<int valsize>
3842   static inline void
3843   update_view_two_parts(
3844     unsigned char* view,
3845     AArch64_valtype immed1,
3846     AArch64_valtype immed2,
3847     elfcpp::Elf_Xword doffset1,
3848     elfcpp::Elf_Xword doffset2,
3849     elfcpp::Elf_Xword dst_mask)
3850   {
3851     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
3852     Valtype* wv = reinterpret_cast<Valtype*>(view);
3853     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
3854     val &= ~dst_mask;
3855     elfcpp::Swap<valsize, big_endian>::writeval(wv,
3856       static_cast<Valtype>(val | (immed1 << doffset1) |
3857                            (immed2 << doffset2)));
3858   }
3859
3860   // Update adr or adrp instruction with immed.
3861   // In adr and adrp: [30:29] immlo   [23:5] immhi
3862
3863   static inline void
3864   update_adr(unsigned char* view, AArch64_valtype immed)
3865   {
3866     elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
3867     This::template update_view_two_parts<32>(
3868       view,
3869       immed & 0x3,
3870       (immed & 0x1ffffc) >> 2,
3871       29,
3872       5,
3873       dst_mask);
3874   }
3875
3876   // Update movz/movn instruction with bits immed.
3877   // Set instruction to movz if is_movz is true, otherwise set instruction
3878   // to movn.
3879
3880   static inline void
3881   update_movnz(unsigned char* view,
3882                AArch64_valtype immed,
3883                bool is_movz)
3884   {
3885     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3886     Valtype* wv = reinterpret_cast<Valtype*>(view);
3887     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
3888
3889     const elfcpp::Elf_Xword doffset =
3890         aarch64_howto[AArch64_reloc_property::INST_MOVW].doffset;
3891     const elfcpp::Elf_Xword dst_mask =
3892         aarch64_howto[AArch64_reloc_property::INST_MOVW].dst_mask;
3893
3894     // Clear immediate fields and opc code.
3895     val &= ~(dst_mask | (0x3 << 29));
3896
3897     // Set instruction to movz or movn.
3898     // movz: [30:29] is 10   movn: [30:29] is 00
3899     if (is_movz)
3900       val |= (0x2 << 29);
3901
3902     elfcpp::Swap<32, big_endian>::writeval(wv,
3903       static_cast<Valtype>(val | (immed << doffset)));
3904   }
3905
3906   // Update selected bits in text.
3907
3908   template<int valsize>
3909   static inline typename This::Status
3910   reloc_common(unsigned char* view, Address x,
3911                 const AArch64_reloc_property* reloc_property)
3912   {
3913     // Select bits from X.
3914     Address immed = reloc_property->select_x_value(x);
3915
3916     // Update view.
3917     const AArch64_reloc_property::Reloc_inst inst =
3918       reloc_property->reloc_inst();
3919     // If it is a data relocation or instruction has 2 parts of immediate
3920     // fields, you should not call pcrela_general.
3921     gold_assert(aarch64_howto[inst].doffset2 == -1 &&
3922                 aarch64_howto[inst].doffset != -1);
3923     This::template update_view<valsize>(view, immed,
3924                                         aarch64_howto[inst].doffset,
3925                                         aarch64_howto[inst].dst_mask);
3926
3927     // Do check overflow or alignment if needed.
3928     return (reloc_property->checkup_x_value(x)
3929             ? This::STATUS_OKAY
3930             : This::STATUS_OVERFLOW);
3931   }
3932
3933  public:
3934
3935   // Do a simple rela relocation at unaligned addresses.
3936
3937   template<int valsize>
3938   static inline typename This::Status
3939   rela_ua(unsigned char* view,
3940           const Sized_relobj_file<size, big_endian>* object,
3941           const Symbol_value<size>* psymval,
3942           AArch64_valtype addend,
3943           const AArch64_reloc_property* reloc_property)
3944   {
3945     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
3946       Valtype;
3947     typename elfcpp::Elf_types<size>::Elf_Addr x =
3948         psymval->value(object, addend);
3949     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
3950       static_cast<Valtype>(x));
3951     return (reloc_property->checkup_x_value(x)
3952             ? This::STATUS_OKAY
3953             : This::STATUS_OVERFLOW);
3954   }
3955
3956   // Do a simple pc-relative relocation at unaligned addresses.
3957
3958   template<int valsize>
3959   static inline typename This::Status
3960   pcrela_ua(unsigned char* view,
3961             const Sized_relobj_file<size, big_endian>* object,
3962             const Symbol_value<size>* psymval,
3963             AArch64_valtype addend,
3964             Address address,
3965             const AArch64_reloc_property* reloc_property)
3966   {
3967     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
3968       Valtype;
3969     Address x = psymval->value(object, addend) - address;
3970     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
3971       static_cast<Valtype>(x));
3972     return (reloc_property->checkup_x_value(x)
3973             ? This::STATUS_OKAY
3974             : This::STATUS_OVERFLOW);
3975   }
3976
3977   // Do a simple rela relocation at aligned addresses.
3978
3979   template<int valsize>
3980   static inline typename This::Status
3981   rela(
3982     unsigned char* view,
3983     const Sized_relobj_file<size, big_endian>* object,
3984     const Symbol_value<size>* psymval,
3985     AArch64_valtype addend,
3986     const AArch64_reloc_property* reloc_property)
3987   {
3988     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
3989     Valtype* wv = reinterpret_cast<Valtype*>(view);
3990     Address x = psymval->value(object, addend);
3991     elfcpp::Swap<valsize, big_endian>::writeval(wv,static_cast<Valtype>(x));
3992     return (reloc_property->checkup_x_value(x)
3993             ? This::STATUS_OKAY
3994             : This::STATUS_OVERFLOW);
3995   }
3996
3997   // Do relocate. Update selected bits in text.
3998   // new_val = (val & ~dst_mask) | (immed << doffset)
3999
4000   template<int valsize>
4001   static inline typename This::Status
4002   rela_general(unsigned char* view,
4003                const Sized_relobj_file<size, big_endian>* object,
4004                const Symbol_value<size>* psymval,
4005                AArch64_valtype addend,
4006                const AArch64_reloc_property* reloc_property)
4007   {
4008     // Calculate relocation.
4009     Address x = psymval->value(object, addend);
4010     return This::template reloc_common<valsize>(view, x, reloc_property);
4011   }
4012
4013   // Do relocate. Update selected bits in text.
4014   // new val = (val & ~dst_mask) | (immed << doffset)
4015
4016   template<int valsize>
4017   static inline typename This::Status
4018   rela_general(
4019     unsigned char* view,
4020     AArch64_valtype s,
4021     AArch64_valtype addend,
4022     const AArch64_reloc_property* reloc_property)
4023   {
4024     // Calculate relocation.
4025     Address x = s + addend;
4026     return This::template reloc_common<valsize>(view, x, reloc_property);
4027   }
4028
4029   // Do address relative relocate. Update selected bits in text.
4030   // new val = (val & ~dst_mask) | (immed << doffset)
4031
4032   template<int valsize>
4033   static inline typename This::Status
4034   pcrela_general(
4035     unsigned char* view,
4036     const Sized_relobj_file<size, big_endian>* object,
4037     const Symbol_value<size>* psymval,
4038     AArch64_valtype addend,
4039     Address address,
4040     const AArch64_reloc_property* reloc_property)
4041   {
4042     // Calculate relocation.
4043     Address x = psymval->value(object, addend) - address;
4044     return This::template reloc_common<valsize>(view, x, reloc_property);
4045   }
4046
4047
4048   // Calculate (S + A) - address, update adr instruction.
4049
4050   static inline typename This::Status
4051   adr(unsigned char* view,
4052       const Sized_relobj_file<size, big_endian>* object,
4053       const Symbol_value<size>* psymval,
4054       Address addend,
4055       Address address,
4056       const AArch64_reloc_property* /* reloc_property */)
4057   {
4058     AArch64_valtype x = psymval->value(object, addend) - address;
4059     // Pick bits [20:0] of X.
4060     AArch64_valtype immed = x & 0x1fffff;
4061     update_adr(view, immed);
4062     // Check -2^20 <= X < 2^20
4063     return (size == 64 && Bits<21>::has_overflow((x))
4064             ? This::STATUS_OVERFLOW
4065             : This::STATUS_OKAY);
4066   }
4067
4068   // Calculate PG(S+A) - PG(address), update adrp instruction.
4069   // R_AARCH64_ADR_PREL_PG_HI21
4070
4071   static inline typename This::Status
4072   adrp(
4073     unsigned char* view,
4074     Address sa,
4075     Address address)
4076   {
4077     AArch64_valtype x = This::Page(sa) - This::Page(address);
4078     // Pick [32:12] of X.
4079     AArch64_valtype immed = (x >> 12) & 0x1fffff;
4080     update_adr(view, immed);
4081     // Check -2^32 <= X < 2^32
4082     return (size == 64 && Bits<33>::has_overflow((x))
4083             ? This::STATUS_OVERFLOW
4084             : This::STATUS_OKAY);
4085   }
4086
4087   // Calculate PG(S+A) - PG(address), update adrp instruction.
4088   // R_AARCH64_ADR_PREL_PG_HI21
4089
4090   static inline typename This::Status
4091   adrp(unsigned char* view,
4092        const Sized_relobj_file<size, big_endian>* object,
4093        const Symbol_value<size>* psymval,
4094        Address addend,
4095        Address address,
4096        const AArch64_reloc_property* reloc_property)
4097   {
4098     Address sa = psymval->value(object, addend);
4099     AArch64_valtype x = This::Page(sa) - This::Page(address);
4100     // Pick [32:12] of X.
4101     AArch64_valtype immed = (x >> 12) & 0x1fffff;
4102     update_adr(view, immed);
4103     return (reloc_property->checkup_x_value(x)
4104             ? This::STATUS_OKAY
4105             : This::STATUS_OVERFLOW);
4106   }
4107
4108   // Update mov[n/z] instruction. Check overflow if needed.
4109   // If X >=0, set the instruction to movz and its immediate value to the
4110   // selected bits S.
4111   // If X < 0, set the instruction to movn and its immediate value to
4112   // NOT (selected bits of).
4113
4114   static inline typename This::Status
4115   movnz(unsigned char* view,
4116         AArch64_valtype x,
4117         const AArch64_reloc_property* reloc_property)
4118   {
4119     // Select bits from X.
4120     Address immed;
4121     bool is_movz;
4122     typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedW;
4123     if (static_cast<SignedW>(x) >= 0)
4124       {
4125         immed = reloc_property->select_x_value(x);
4126         is_movz = true;
4127       }
4128     else
4129       {
4130         immed = reloc_property->select_x_value(~x);;
4131         is_movz = false;
4132       }
4133
4134     // Update movnz instruction.
4135     update_movnz(view, immed, is_movz);
4136
4137     // Do check overflow or alignment if needed.
4138     return (reloc_property->checkup_x_value(x)
4139             ? This::STATUS_OKAY
4140             : This::STATUS_OVERFLOW);
4141   }
4142
4143   static inline bool
4144   maybe_apply_stub(unsigned int,
4145                    const The_relocate_info*,
4146                    const The_rela&,
4147                    unsigned char*,
4148                    Address,
4149                    const Sized_symbol<size>*,
4150                    const Symbol_value<size>*,
4151                    const Sized_relobj_file<size, big_endian>*);
4152
4153 };  // End of AArch64_relocate_functions
4154
4155
4156 // For a certain relocation type (usually jump/branch), test to see if the
4157 // destination needs a stub to fulfil. If so, re-route the destination of the
4158 // original instruction to the stub, note, at this time, the stub has already
4159 // been generated.
4160
4161 template<int size, bool big_endian>
4162 bool
4163 AArch64_relocate_functions<size, big_endian>::
4164 maybe_apply_stub(unsigned int r_type,
4165                  const The_relocate_info* relinfo,
4166                  const The_rela& rela,
4167                  unsigned char* view,
4168                  Address address,
4169                  const Sized_symbol<size>* gsym,
4170                  const Symbol_value<size>* psymval,
4171                  const Sized_relobj_file<size, big_endian>* object)
4172 {
4173   if (parameters->options().relocatable())
4174     return false;
4175
4176   typename elfcpp::Elf_types<size>::Elf_Swxword addend = rela.get_r_addend();
4177   Address branch_target = psymval->value(object, 0) + addend;
4178   The_reloc_stub_type stub_type = The_reloc_stub::
4179     stub_type_for_reloc(r_type, address, branch_target);
4180   if (stub_type == The_reloc_stub::ST_NONE)
4181     return false;
4182
4183   const The_aarch64_relobj* aarch64_relobj =
4184       static_cast<const The_aarch64_relobj*>(object);
4185   The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
4186   gold_assert(stub_table != NULL);
4187
4188   unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4189   typename The_reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
4190   The_reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
4191   gold_assert(stub != NULL);
4192
4193   Address new_branch_target = stub_table->address() + stub->offset();
4194   typename elfcpp::Swap<size, big_endian>::Valtype branch_offset =
4195       new_branch_target - address;
4196   const AArch64_reloc_property* arp =
4197       aarch64_reloc_property_table->get_reloc_property(r_type);
4198   gold_assert(arp != NULL);
4199   typename This::Status status = This::template
4200       rela_general<32>(view, branch_offset, 0, arp);
4201   if (status != This::STATUS_OKAY)
4202     gold_error(_("Stub is too far away, try a smaller value "
4203                  "for '--stub-group-size'. For example, 0x2000000."));
4204   return true;
4205 }
4206
4207
4208 // Group input sections for stub generation.
4209 //
4210 // We group input sections in an output section so that the total size,
4211 // including any padding space due to alignment is smaller than GROUP_SIZE
4212 // unless the only input section in group is bigger than GROUP_SIZE already.
4213 // Then an ARM stub table is created to follow the last input section
4214 // in group.  For each group an ARM stub table is created an is placed
4215 // after the last group.  If STUB_ALWAYS_AFTER_BRANCH is false, we further
4216 // extend the group after the stub table.
4217
4218 template<int size, bool big_endian>
4219 void
4220 Target_aarch64<size, big_endian>::group_sections(
4221     Layout* layout,
4222     section_size_type group_size,
4223     bool stubs_always_after_branch,
4224     const Task* task)
4225 {
4226   // Group input sections and insert stub table
4227   Layout::Section_list section_list;
4228   layout->get_executable_sections(&section_list);
4229   for (Layout::Section_list::const_iterator p = section_list.begin();
4230        p != section_list.end();
4231        ++p)
4232     {
4233       AArch64_output_section<size, big_endian>* output_section =
4234           static_cast<AArch64_output_section<size, big_endian>*>(*p);
4235       output_section->group_sections(group_size, stubs_always_after_branch,
4236                                      this, task);
4237     }
4238 }
4239
4240
4241 // Find the AArch64_input_section object corresponding to the SHNDX-th input
4242 // section of RELOBJ.
4243
4244 template<int size, bool big_endian>
4245 AArch64_input_section<size, big_endian>*
4246 Target_aarch64<size, big_endian>::find_aarch64_input_section(
4247     Relobj* relobj, unsigned int shndx) const
4248 {
4249   Section_id sid(relobj, shndx);
4250   typename AArch64_input_section_map::const_iterator p =
4251     this->aarch64_input_section_map_.find(sid);
4252   return (p != this->aarch64_input_section_map_.end()) ? p->second : NULL;
4253 }
4254
4255
4256 // Make a new AArch64_input_section object.
4257
4258 template<int size, bool big_endian>
4259 AArch64_input_section<size, big_endian>*
4260 Target_aarch64<size, big_endian>::new_aarch64_input_section(
4261     Relobj* relobj, unsigned int shndx)
4262 {
4263   Section_id sid(relobj, shndx);
4264
4265   AArch64_input_section<size, big_endian>* input_section =
4266       new AArch64_input_section<size, big_endian>(relobj, shndx);
4267   input_section->init();
4268
4269   // Register new AArch64_input_section in map for look-up.
4270   std::pair<typename AArch64_input_section_map::iterator,bool> ins =
4271       this->aarch64_input_section_map_.insert(
4272           std::make_pair(sid, input_section));
4273
4274   // Make sure that it we have not created another AArch64_input_section
4275   // for this input section already.
4276   gold_assert(ins.second);
4277
4278   return input_section;
4279 }
4280
4281
4282 // Relaxation hook.  This is where we do stub generation.
4283
4284 template<int size, bool big_endian>
4285 bool
4286 Target_aarch64<size, big_endian>::do_relax(
4287     int pass,
4288     const Input_objects* input_objects,
4289     Symbol_table* symtab,
4290     Layout* layout ,
4291     const Task* task)
4292 {
4293   gold_assert(!parameters->options().relocatable());
4294   if (pass == 1)
4295     {
4296       section_size_type stub_group_size =
4297           parameters->options().stub_group_size();
4298       if (stub_group_size == 1)
4299         {
4300           // Leave room for 4096 4-byte stub entries. If we exceed that, then we
4301           // will fail to link.  The user will have to relink with an explicit
4302           // group size option.
4303           stub_group_size = The_reloc_stub::MAX_BRANCH_OFFSET - 4096 * 4;
4304         }
4305       group_sections(layout, stub_group_size, true, task);
4306     }
4307   else
4308     {
4309       // If this is not the first pass, addresses and file offsets have
4310       // been reset at this point, set them here.
4311       for (Stub_table_iterator sp = this->stub_tables_.begin();
4312            sp != this->stub_tables_.end(); ++sp)
4313         {
4314           The_stub_table* stt = *sp;
4315           The_aarch64_input_section* owner = stt->owner();
4316           off_t off = align_address(owner->original_size(),
4317                                     stt->addralign());
4318           stt->set_address_and_file_offset(owner->address() + off,
4319                                            owner->offset() + off);
4320         }
4321     }
4322
4323   // Scan relocs for relocation stubs
4324   for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
4325        op != input_objects->relobj_end();
4326        ++op)
4327     {
4328       The_aarch64_relobj* aarch64_relobj =
4329           static_cast<The_aarch64_relobj*>(*op);
4330       // Lock the object so we can read from it.  This is only called
4331       // single-threaded from Layout::finalize, so it is OK to lock.
4332       Task_lock_obj<Object> tl(task, aarch64_relobj);
4333       aarch64_relobj->scan_sections_for_stubs(this, symtab, layout);
4334     }
4335
4336   bool any_stub_table_changed = false;
4337   for (Stub_table_iterator siter = this->stub_tables_.begin();
4338        siter != this->stub_tables_.end() && !any_stub_table_changed; ++siter)
4339     {
4340       The_stub_table* stub_table = *siter;
4341       if (stub_table->update_data_size_changed_p())
4342         {
4343           The_aarch64_input_section* owner = stub_table->owner();
4344           uint64_t address = owner->address();
4345           off_t offset = owner->offset();
4346           owner->reset_address_and_file_offset();
4347           owner->set_address_and_file_offset(address, offset);
4348
4349           any_stub_table_changed = true;
4350         }
4351     }
4352
4353   // Do not continue relaxation.
4354   bool continue_relaxation = any_stub_table_changed;
4355   if (!continue_relaxation)
4356     for (Stub_table_iterator sp = this->stub_tables_.begin();
4357          (sp != this->stub_tables_.end());
4358          ++sp)
4359       (*sp)->finalize_stubs();
4360
4361   return continue_relaxation;
4362 }
4363
4364
4365 // Make a new Stub_table.
4366
4367 template<int size, bool big_endian>
4368 Stub_table<size, big_endian>*
4369 Target_aarch64<size, big_endian>::new_stub_table(
4370     AArch64_input_section<size, big_endian>* owner)
4371 {
4372   Stub_table<size, big_endian>* stub_table =
4373       new Stub_table<size, big_endian>(owner);
4374   stub_table->set_address(align_address(
4375       owner->address() + owner->data_size(), 8));
4376   stub_table->set_file_offset(owner->offset() + owner->data_size());
4377   stub_table->finalize_data_size();
4378
4379   this->stub_tables_.push_back(stub_table);
4380
4381   return stub_table;
4382 }
4383
4384
4385 template<int size, bool big_endian>
4386 typename elfcpp::Elf_types<size>::Elf_Addr
4387 Target_aarch64<size, big_endian>::do_reloc_addend(
4388     void* arg, unsigned int r_type,
4389     typename elfcpp::Elf_types<size>::Elf_Addr) const
4390 {
4391   gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
4392   uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
4393   gold_assert(intarg < this->tlsdesc_reloc_info_.size());
4394   const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
4395   const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
4396   gold_assert(psymval->is_tls_symbol());
4397   // The value of a TLS symbol is the offset in the TLS segment.
4398   return psymval->value(ti.object, 0);
4399 }
4400
4401 // Return the number of entries in the PLT.
4402
4403 template<int size, bool big_endian>
4404 unsigned int
4405 Target_aarch64<size, big_endian>::plt_entry_count() const
4406 {
4407   if (this->plt_ == NULL)
4408     return 0;
4409   return this->plt_->entry_count();
4410 }
4411
4412 // Return the offset of the first non-reserved PLT entry.
4413
4414 template<int size, bool big_endian>
4415 unsigned int
4416 Target_aarch64<size, big_endian>::first_plt_entry_offset() const
4417 {
4418   return this->plt_->first_plt_entry_offset();
4419 }
4420
4421 // Return the size of each PLT entry.
4422
4423 template<int size, bool big_endian>
4424 unsigned int
4425 Target_aarch64<size, big_endian>::plt_entry_size() const
4426 {
4427   return this->plt_->get_plt_entry_size();
4428 }
4429
4430 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
4431
4432 template<int size, bool big_endian>
4433 void
4434 Target_aarch64<size, big_endian>::define_tls_base_symbol(
4435     Symbol_table* symtab, Layout* layout)
4436 {
4437   if (this->tls_base_symbol_defined_)
4438     return;
4439
4440   Output_segment* tls_segment = layout->tls_segment();
4441   if (tls_segment != NULL)
4442     {
4443       bool is_exec = parameters->options().output_is_executable();
4444       symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
4445                                        Symbol_table::PREDEFINED,
4446                                        tls_segment, 0, 0,
4447                                        elfcpp::STT_TLS,
4448                                        elfcpp::STB_LOCAL,
4449                                        elfcpp::STV_HIDDEN, 0,
4450                                        (is_exec
4451                                         ? Symbol::SEGMENT_END
4452                                         : Symbol::SEGMENT_START),
4453                                        true);
4454     }
4455   this->tls_base_symbol_defined_ = true;
4456 }
4457
4458 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
4459
4460 template<int size, bool big_endian>
4461 void
4462 Target_aarch64<size, big_endian>::reserve_tlsdesc_entries(
4463     Symbol_table* symtab, Layout* layout)
4464 {
4465   if (this->plt_ == NULL)
4466     this->make_plt_section(symtab, layout);
4467
4468   if (!this->plt_->has_tlsdesc_entry())
4469     {
4470       // Allocate the TLSDESC_GOT entry.
4471       Output_data_got_aarch64<size, big_endian>* got =
4472           this->got_section(symtab, layout);
4473       unsigned int got_offset = got->add_constant(0);
4474
4475       // Allocate the TLSDESC_PLT entry.
4476       this->plt_->reserve_tlsdesc_entry(got_offset);
4477     }
4478 }
4479
4480 // Create a GOT entry for the TLS module index.
4481
4482 template<int size, bool big_endian>
4483 unsigned int
4484 Target_aarch64<size, big_endian>::got_mod_index_entry(
4485     Symbol_table* symtab, Layout* layout,
4486     Sized_relobj_file<size, big_endian>* object)
4487 {
4488   if (this->got_mod_index_offset_ == -1U)
4489     {
4490       gold_assert(symtab != NULL && layout != NULL && object != NULL);
4491       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
4492       Output_data_got_aarch64<size, big_endian>* got =
4493           this->got_section(symtab, layout);
4494       unsigned int got_offset = got->add_constant(0);
4495       rela_dyn->add_local(object, 0, elfcpp::R_AARCH64_TLS_DTPMOD64, got,
4496                           got_offset, 0);
4497       got->add_constant(0);
4498       this->got_mod_index_offset_ = got_offset;
4499     }
4500   return this->got_mod_index_offset_;
4501 }
4502
4503 // Optimize the TLS relocation type based on what we know about the
4504 // symbol.  IS_FINAL is true if the final address of this symbol is
4505 // known at link time.
4506
4507 template<int size, bool big_endian>
4508 tls::Tls_optimization
4509 Target_aarch64<size, big_endian>::optimize_tls_reloc(bool is_final,
4510                                                      int r_type)
4511 {
4512   // If we are generating a shared library, then we can't do anything
4513   // in the linker
4514   if (parameters->options().shared())
4515     return tls::TLSOPT_NONE;
4516
4517   switch (r_type)
4518     {
4519     case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
4520     case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
4521     case elfcpp::R_AARCH64_TLSDESC_LD_PREL19:
4522     case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21:
4523     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
4524     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
4525     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
4526     case elfcpp::R_AARCH64_TLSDESC_OFF_G1:
4527     case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC:
4528     case elfcpp::R_AARCH64_TLSDESC_LDR:
4529     case elfcpp::R_AARCH64_TLSDESC_ADD:
4530     case elfcpp::R_AARCH64_TLSDESC_CALL:
4531       // These are General-Dynamic which permits fully general TLS
4532       // access.  Since we know that we are generating an executable,
4533       // we can convert this to Initial-Exec.  If we also know that
4534       // this is a local symbol, we can further switch to Local-Exec.
4535       if (is_final)
4536         return tls::TLSOPT_TO_LE;
4537       return tls::TLSOPT_TO_IE;
4538
4539     case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
4540     case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
4541     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
4542     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
4543       // These are Local-Dynamic, which refer to local symbols in the
4544       // dynamic TLS block. Since we know that we generating an
4545       // executable, we can switch to Local-Exec.
4546       return tls::TLSOPT_TO_LE;
4547
4548     case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
4549     case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
4550     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4551     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4552     case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
4553       // These are Initial-Exec relocs which get the thread offset
4554       // from the GOT. If we know that we are linking against the
4555       // local symbol, we can switch to Local-Exec, which links the
4556       // thread offset into the instruction.
4557       if (is_final)
4558         return tls::TLSOPT_TO_LE;
4559       return tls::TLSOPT_NONE;
4560
4561     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
4562     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
4563     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
4564     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
4565     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
4566     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
4567     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
4568     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
4569       // When we already have Local-Exec, there is nothing further we
4570       // can do.
4571       return tls::TLSOPT_NONE;
4572
4573     default:
4574       gold_unreachable();
4575     }
4576 }
4577
4578 // Returns true if this relocation type could be that of a function pointer.
4579
4580 template<int size, bool big_endian>
4581 inline bool
4582 Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
4583   unsigned int r_type)
4584 {
4585   switch (r_type)
4586     {
4587     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
4588     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
4589     case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
4590     case elfcpp::R_AARCH64_ADR_GOT_PAGE:
4591     case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
4592       {
4593         return true;
4594       }
4595     }
4596   return false;
4597 }
4598
4599 // For safe ICF, scan a relocation for a local symbol to check if it
4600 // corresponds to a function pointer being taken.  In that case mark
4601 // the function whose pointer was taken as not foldable.
4602
4603 template<int size, bool big_endian>
4604 inline bool
4605 Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
4606   Symbol_table* ,
4607   Layout* ,
4608   Target_aarch64<size, big_endian>* ,
4609   Sized_relobj_file<size, big_endian>* ,
4610   unsigned int ,
4611   Output_section* ,
4612   const elfcpp::Rela<size, big_endian>& ,
4613   unsigned int r_type,
4614   const elfcpp::Sym<size, big_endian>&)
4615 {
4616   // When building a shared library, do not fold any local symbols.
4617   return (parameters->options().shared()
4618           || possible_function_pointer_reloc(r_type));
4619 }
4620
4621 // For safe ICF, scan a relocation for a global symbol to check if it
4622 // corresponds to a function pointer being taken.  In that case mark
4623 // the function whose pointer was taken as not foldable.
4624
4625 template<int size, bool big_endian>
4626 inline bool
4627 Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
4628   Symbol_table* ,
4629   Layout* ,
4630   Target_aarch64<size, big_endian>* ,
4631   Sized_relobj_file<size, big_endian>* ,
4632   unsigned int ,
4633   Output_section* ,
4634   const elfcpp::Rela<size, big_endian>& ,
4635   unsigned int r_type,
4636   Symbol* gsym)
4637 {
4638   // When building a shared library, do not fold symbols whose visibility
4639   // is hidden, internal or protected.
4640   return ((parameters->options().shared()
4641            && (gsym->visibility() == elfcpp::STV_INTERNAL
4642                || gsym->visibility() == elfcpp::STV_PROTECTED
4643                || gsym->visibility() == elfcpp::STV_HIDDEN))
4644           || possible_function_pointer_reloc(r_type));
4645 }
4646
4647 // Report an unsupported relocation against a local symbol.
4648
4649 template<int size, bool big_endian>
4650 void
4651 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
4652      Sized_relobj_file<size, big_endian>* object,
4653      unsigned int r_type)
4654 {
4655   gold_error(_("%s: unsupported reloc %u against local symbol"),
4656              object->name().c_str(), r_type);
4657 }
4658
4659 // We are about to emit a dynamic relocation of type R_TYPE.  If the
4660 // dynamic linker does not support it, issue an error.
4661
4662 template<int size, bool big_endian>
4663 void
4664 Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
4665                                                       unsigned int r_type)
4666 {
4667   gold_assert(r_type != elfcpp::R_AARCH64_NONE);
4668
4669   switch (r_type)
4670     {
4671     // These are the relocation types supported by glibc for AARCH64.
4672     case elfcpp::R_AARCH64_NONE:
4673     case elfcpp::R_AARCH64_COPY:
4674     case elfcpp::R_AARCH64_GLOB_DAT:
4675     case elfcpp::R_AARCH64_JUMP_SLOT:
4676     case elfcpp::R_AARCH64_RELATIVE:
4677     case elfcpp::R_AARCH64_TLS_DTPREL64:
4678     case elfcpp::R_AARCH64_TLS_DTPMOD64:
4679     case elfcpp::R_AARCH64_TLS_TPREL64:
4680     case elfcpp::R_AARCH64_TLSDESC:
4681     case elfcpp::R_AARCH64_IRELATIVE:
4682     case elfcpp::R_AARCH64_ABS32:
4683     case elfcpp::R_AARCH64_ABS64:
4684       return;
4685
4686     default:
4687       break;
4688     }
4689
4690   // This prevents us from issuing more than one error per reloc
4691   // section. But we can still wind up issuing more than one
4692   // error per object file.
4693   if (this->issued_non_pic_error_)
4694     return;
4695   gold_assert(parameters->options().output_is_position_independent());
4696   object->error(_("requires unsupported dynamic reloc; "
4697                   "recompile with -fPIC"));
4698   this->issued_non_pic_error_ = true;
4699   return;
4700 }
4701
4702 // Return whether we need to make a PLT entry for a relocation of the
4703 // given type against a STT_GNU_IFUNC symbol.
4704
4705 template<int size, bool big_endian>
4706 bool
4707 Target_aarch64<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
4708     Sized_relobj_file<size, big_endian>* object,
4709     unsigned int r_type)
4710 {
4711   const AArch64_reloc_property* arp =
4712       aarch64_reloc_property_table->get_reloc_property(r_type);
4713   gold_assert(arp != NULL);
4714
4715   int flags = arp->reference_flags();
4716   if (flags & Symbol::TLS_REF)
4717     {
4718       gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
4719                  object->name().c_str(), arp->name().c_str());
4720       return false;
4721     }
4722   return flags != 0;
4723 }
4724
4725 // Scan a relocation for a local symbol.
4726
4727 template<int size, bool big_endian>
4728 inline void
4729 Target_aarch64<size, big_endian>::Scan::local(
4730     Symbol_table* symtab,
4731     Layout* layout,
4732     Target_aarch64<size, big_endian>* target,
4733     Sized_relobj_file<size, big_endian>* object,
4734     unsigned int data_shndx,
4735     Output_section* output_section,
4736     const elfcpp::Rela<size, big_endian>& rela,
4737     unsigned int r_type,
4738     const elfcpp::Sym<size, big_endian>& lsym,
4739     bool is_discarded)
4740 {
4741   if (is_discarded)
4742     return;
4743
4744   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
4745       Reloc_section;
4746   Output_data_got_aarch64<size, big_endian>* got =
4747       target->got_section(symtab, layout);
4748   unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4749
4750   // A local STT_GNU_IFUNC symbol may require a PLT entry.
4751   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
4752   if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
4753     target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
4754
4755   switch (r_type)
4756     {
4757     case elfcpp::R_AARCH64_ABS32:
4758     case elfcpp::R_AARCH64_ABS16:
4759       if (parameters->options().output_is_position_independent())
4760         {
4761           gold_error(_("%s: unsupported reloc %u in pos independent link."),
4762                      object->name().c_str(), r_type);
4763         }
4764       break;
4765
4766     case elfcpp::R_AARCH64_ABS64:
4767       // If building a shared library or pie, we need to mark this as a dynmic
4768       // reloction, so that the dynamic loader can relocate it.
4769       if (parameters->options().output_is_position_independent())
4770         {
4771           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4772           rela_dyn->add_local_relative(object, r_sym,
4773                                        elfcpp::R_AARCH64_RELATIVE,
4774                                        output_section,
4775                                        data_shndx,
4776                                        rela.get_r_offset(),
4777                                        rela.get_r_addend(),
4778                                        is_ifunc);
4779         }
4780       break;
4781
4782     case elfcpp::R_AARCH64_PREL64:
4783     case elfcpp::R_AARCH64_PREL32:
4784     case elfcpp::R_AARCH64_PREL16:
4785       break;
4786
4787     case elfcpp::R_AARCH64_LD_PREL_LO19:        // 273
4788     case elfcpp::R_AARCH64_ADR_PREL_LO21:       // 274
4789     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:    // 275
4790     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
4791     case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:     // 277
4792     case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:   // 278
4793     case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:  // 284
4794     case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:  // 285
4795     case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:  // 286
4796     case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
4797        break;
4798
4799     // Control flow, pc-relative. We don't need to do anything for a relative
4800     // addressing relocation against a local symbol if it does not reference
4801     // the GOT.
4802     case elfcpp::R_AARCH64_TSTBR14:
4803     case elfcpp::R_AARCH64_CONDBR19:
4804     case elfcpp::R_AARCH64_JUMP26:
4805     case elfcpp::R_AARCH64_CALL26:
4806       break;
4807
4808     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
4809     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
4810       {
4811         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
4812           optimize_tls_reloc(!parameters->options().shared(), r_type);
4813         if (tlsopt == tls::TLSOPT_TO_LE)
4814           break;
4815
4816         layout->set_has_static_tls();
4817         // Create a GOT entry for the tp-relative offset.
4818         if (!parameters->doing_static_link())
4819           {
4820             got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
4821                                     target->rela_dyn_section(layout),
4822                                     elfcpp::R_AARCH64_TLS_TPREL64);
4823           }
4824         else if (!object->local_has_got_offset(r_sym,
4825                                                GOT_TYPE_TLS_OFFSET))
4826           {
4827             got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
4828             unsigned int got_offset =
4829                 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
4830             const elfcpp::Elf_Xword addend = rela.get_r_addend();
4831             gold_assert(addend == 0);
4832             got->add_static_reloc(got_offset, elfcpp::R_AARCH64_TLS_TPREL64,
4833                                   object, r_sym);
4834           }
4835       }
4836       break;
4837
4838     case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
4839     case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
4840       {
4841         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
4842             optimize_tls_reloc(!parameters->options().shared(), r_type);
4843         if (tlsopt == tls::TLSOPT_TO_LE)
4844           {
4845             layout->set_has_static_tls();
4846             break;
4847           }
4848         gold_assert(tlsopt == tls::TLSOPT_NONE);
4849
4850         got->add_local_pair_with_rel(object,r_sym, data_shndx,
4851                                      GOT_TYPE_TLS_PAIR,
4852                                      target->rela_dyn_section(layout),
4853                                      elfcpp::R_AARCH64_TLS_DTPMOD64);
4854       }
4855       break;
4856
4857     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
4858     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
4859     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
4860       {
4861         layout->set_has_static_tls();
4862         bool output_is_shared = parameters->options().shared();
4863         if (output_is_shared)
4864           gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
4865                      object->name().c_str(), r_type);
4866       }
4867       break;
4868
4869     case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
4870     case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
4871       {
4872         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
4873             optimize_tls_reloc(!parameters->options().shared(), r_type);
4874         if (tlsopt == tls::TLSOPT_NONE)
4875           {
4876             // Create a GOT entry for the module index.
4877             target->got_mod_index_entry(symtab, layout, object);
4878           }
4879         else if (tlsopt != tls::TLSOPT_TO_LE)
4880           unsupported_reloc_local(object, r_type);
4881       }
4882       break;
4883
4884     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
4885     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
4886       break;
4887
4888     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
4889     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
4890     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
4891       {
4892         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
4893             optimize_tls_reloc(!parameters->options().shared(), r_type);
4894         target->define_tls_base_symbol(symtab, layout);
4895         if (tlsopt == tls::TLSOPT_NONE)
4896           {
4897             // Create reserved PLT and GOT entries for the resolver.
4898             target->reserve_tlsdesc_entries(symtab, layout);
4899
4900             // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
4901             // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
4902             // entry needs to be in an area in .got.plt, not .got. Call
4903             // got_section to make sure the section has been created.
4904             target->got_section(symtab, layout);
4905             Output_data_got<size, big_endian>* got =
4906                 target->got_tlsdesc_section();
4907             unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4908             if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
4909               {
4910                 unsigned int got_offset = got->add_constant(0);
4911                 got->add_constant(0);
4912                 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
4913                                              got_offset);
4914                 Reloc_section* rt = target->rela_tlsdesc_section(layout);
4915                 // We store the arguments we need in a vector, and use
4916                 // the index into the vector as the parameter to pass
4917                 // to the target specific routines.
4918                 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
4919                 void* arg = reinterpret_cast<void*>(intarg);
4920                 rt->add_target_specific(elfcpp::R_AARCH64_TLSDESC, arg,
4921                                         got, got_offset, 0);
4922               }
4923           }
4924         else if (tlsopt != tls::TLSOPT_TO_LE)
4925           unsupported_reloc_local(object, r_type);
4926       }
4927       break;
4928
4929     case elfcpp::R_AARCH64_TLSDESC_CALL:
4930       break;
4931
4932     default:
4933       unsupported_reloc_local(object, r_type);
4934     }
4935 }
4936
4937
4938 // Report an unsupported relocation against a global symbol.
4939
4940 template<int size, bool big_endian>
4941 void
4942 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
4943     Sized_relobj_file<size, big_endian>* object,
4944     unsigned int r_type,
4945     Symbol* gsym)
4946 {
4947   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4948              object->name().c_str(), r_type, gsym->demangled_name().c_str());
4949 }
4950
4951 template<int size, bool big_endian>
4952 inline void
4953 Target_aarch64<size, big_endian>::Scan::global(
4954     Symbol_table* symtab,
4955     Layout* layout,
4956     Target_aarch64<size, big_endian>* target,
4957     Sized_relobj_file<size, big_endian> * object,
4958     unsigned int data_shndx,
4959     Output_section* output_section,
4960     const elfcpp::Rela<size, big_endian>& rela,
4961     unsigned int r_type,
4962     Symbol* gsym)
4963 {
4964   // A STT_GNU_IFUNC symbol may require a PLT entry.
4965   if (gsym->type() == elfcpp::STT_GNU_IFUNC
4966       && this->reloc_needs_plt_for_ifunc(object, r_type))
4967     target->make_plt_entry(symtab, layout, gsym);
4968
4969   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
4970     Reloc_section;
4971   const AArch64_reloc_property* arp =
4972       aarch64_reloc_property_table->get_reloc_property(r_type);
4973   gold_assert(arp != NULL);
4974
4975   switch (r_type)
4976     {
4977     case elfcpp::R_AARCH64_ABS16:
4978     case elfcpp::R_AARCH64_ABS32:
4979     case elfcpp::R_AARCH64_ABS64:
4980       {
4981         // Make a PLT entry if necessary.
4982         if (gsym->needs_plt_entry())
4983           {
4984             target->make_plt_entry(symtab, layout, gsym);
4985             // Since this is not a PC-relative relocation, we may be
4986             // taking the address of a function. In that case we need to
4987             // set the entry in the dynamic symbol table to the address of
4988             // the PLT entry.
4989             if (gsym->is_from_dynobj() && !parameters->options().shared())
4990               gsym->set_needs_dynsym_value();
4991           }
4992         // Make a dynamic relocation if necessary.
4993         if (gsym->needs_dynamic_reloc(arp->reference_flags()))
4994           {
4995             if (!parameters->options().output_is_position_independent()
4996                 && gsym->may_need_copy_reloc())
4997               {
4998                 target->copy_reloc(symtab, layout, object,
4999                                    data_shndx, output_section, gsym, rela);
5000               }
5001             else if (r_type == elfcpp::R_AARCH64_ABS64
5002                      && gsym->type() == elfcpp::STT_GNU_IFUNC
5003                      && gsym->can_use_relative_reloc(false)
5004                      && !gsym->is_from_dynobj()
5005                      && !gsym->is_undefined()
5006                      && !gsym->is_preemptible())
5007               {
5008                 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
5009                 // symbol. This makes a function address in a PIE executable
5010                 // match the address in a shared library that it links against.
5011                 Reloc_section* rela_dyn =
5012                     target->rela_irelative_section(layout);
5013                 unsigned int r_type = elfcpp::R_AARCH64_IRELATIVE;
5014                 rela_dyn->add_symbolless_global_addend(gsym, r_type,
5015                                                        output_section, object,
5016                                                        data_shndx,
5017                                                        rela.get_r_offset(),
5018                                                        rela.get_r_addend());
5019               }
5020             else if (r_type == elfcpp::R_AARCH64_ABS64
5021                      && gsym->can_use_relative_reloc(false))
5022               {
5023                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5024                 rela_dyn->add_global_relative(gsym,
5025                                               elfcpp::R_AARCH64_RELATIVE,
5026                                               output_section,
5027                                               object,
5028                                               data_shndx,
5029                                               rela.get_r_offset(),
5030                                               rela.get_r_addend(),
5031                                               false);
5032               }
5033             else
5034               {
5035                 check_non_pic(object, r_type);
5036                 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*
5037                     rela_dyn = target->rela_dyn_section(layout);
5038                 rela_dyn->add_global(
5039                   gsym, r_type, output_section, object,
5040                   data_shndx, rela.get_r_offset(),rela.get_r_addend());
5041               }
5042           }
5043       }
5044       break;
5045
5046     case elfcpp::R_AARCH64_PREL16:
5047     case elfcpp::R_AARCH64_PREL32:
5048     case elfcpp::R_AARCH64_PREL64:
5049       // This is used to fill the GOT absolute address.
5050       if (gsym->needs_plt_entry())
5051         {
5052           target->make_plt_entry(symtab, layout, gsym);
5053         }
5054       break;
5055
5056     case elfcpp::R_AARCH64_LD_PREL_LO19:        // 273
5057     case elfcpp::R_AARCH64_ADR_PREL_LO21:       // 274
5058     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:    // 275
5059     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
5060     case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:     // 277
5061     case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:   // 278
5062     case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:  // 284
5063     case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:  // 285
5064     case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:  // 286
5065     case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
5066       {
5067         if (gsym->needs_plt_entry())
5068           target->make_plt_entry(symtab, layout, gsym);
5069         // Make a dynamic relocation if necessary.
5070         if (gsym->needs_dynamic_reloc(arp->reference_flags()))
5071           {
5072             if (parameters->options().output_is_executable()
5073                 && gsym->may_need_copy_reloc())
5074               {
5075                 target->copy_reloc(symtab, layout, object,
5076                                    data_shndx, output_section, gsym, rela);
5077               }
5078           }
5079         break;
5080       }
5081
5082     case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5083     case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5084       {
5085         // This pair of relocations is used to access a specific GOT entry.
5086         // Note a GOT entry is an *address* to a symbol.
5087         // The symbol requires a GOT entry
5088         Output_data_got_aarch64<size, big_endian>* got =
5089           target->got_section(symtab, layout);
5090         if (gsym->final_value_is_known())
5091           {
5092             // For a STT_GNU_IFUNC symbol we want the PLT address.
5093             if (gsym->type() == elfcpp::STT_GNU_IFUNC)
5094               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5095             else
5096               got->add_global(gsym, GOT_TYPE_STANDARD);
5097           }
5098         else
5099           {
5100             // If this symbol is not fully resolved, we need to add a dynamic
5101             // relocation for it.
5102             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5103
5104             // Use a GLOB_DAT rather than a RELATIVE reloc if:
5105             //
5106             // 1) The symbol may be defined in some other module.
5107             // 2) We are building a shared library and this is a protected
5108             // symbol; using GLOB_DAT means that the dynamic linker can use
5109             // the address of the PLT in the main executable when appropriate
5110             // so that function address comparisons work.
5111             // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
5112             // again so that function address comparisons work.
5113             if (gsym->is_from_dynobj()
5114                 || gsym->is_undefined()
5115                 || gsym->is_preemptible()
5116                 || (gsym->visibility() == elfcpp::STV_PROTECTED
5117                     && parameters->options().shared())
5118                 || (gsym->type() == elfcpp::STT_GNU_IFUNC
5119                     && parameters->options().output_is_position_independent()))
5120               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
5121                                        rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
5122             else
5123               {
5124                 // For a STT_GNU_IFUNC symbol we want to write the PLT
5125                 // offset into the GOT, so that function pointer
5126                 // comparisons work correctly.
5127                 bool is_new;
5128                 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
5129                   is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
5130                 else
5131                   {
5132                     is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5133                     // Tell the dynamic linker to use the PLT address
5134                     // when resolving relocations.
5135                     if (gsym->is_from_dynobj()
5136                         && !parameters->options().shared())
5137                       gsym->set_needs_dynsym_value();
5138                   }
5139                 if (is_new)
5140                   {
5141                     rela_dyn->add_global_relative(
5142                         gsym, elfcpp::R_AARCH64_RELATIVE,
5143                         got,
5144                         gsym->got_offset(GOT_TYPE_STANDARD),
5145                         0,
5146                         false);
5147                   }
5148               }
5149           }
5150         break;
5151       }
5152
5153     case elfcpp::R_AARCH64_TSTBR14:
5154     case elfcpp::R_AARCH64_CONDBR19:
5155     case elfcpp::R_AARCH64_JUMP26:
5156     case elfcpp::R_AARCH64_CALL26:
5157       {
5158         if (gsym->final_value_is_known())
5159           break;
5160
5161         if (gsym->is_defined() &&
5162             !gsym->is_from_dynobj() &&
5163             !gsym->is_preemptible())
5164           break;
5165
5166         // Make plt entry for function call.
5167         target->make_plt_entry(symtab, layout, gsym);
5168         break;
5169       }
5170
5171     case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5172     case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:  // General dynamic
5173       {
5174         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5175             optimize_tls_reloc(gsym->final_value_is_known(), r_type);
5176         if (tlsopt == tls::TLSOPT_TO_LE)
5177           {
5178             layout->set_has_static_tls();
5179             break;
5180           }
5181         gold_assert(tlsopt == tls::TLSOPT_NONE);
5182
5183         // General dynamic.
5184         Output_data_got_aarch64<size, big_endian>* got =
5185             target->got_section(symtab, layout);
5186         // Create 2 consecutive entries for module index and offset.
5187         got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
5188                                       target->rela_dyn_section(layout),
5189                                       elfcpp::R_AARCH64_TLS_DTPMOD64,
5190                                       elfcpp::R_AARCH64_TLS_DTPREL64);
5191       }
5192       break;
5193
5194     case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5195     case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:  // Local dynamic
5196       {
5197         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5198             optimize_tls_reloc(!parameters->options().shared(), r_type);
5199         if (tlsopt == tls::TLSOPT_NONE)
5200           {
5201             // Create a GOT entry for the module index.
5202             target->got_mod_index_entry(symtab, layout, object);
5203           }
5204         else if (tlsopt != tls::TLSOPT_TO_LE)
5205           unsupported_reloc_local(object, r_type);
5206       }
5207       break;
5208
5209     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5210     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:  // Other local dynamic
5211       break;
5212
5213     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5214     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:  // Initial executable
5215       {
5216         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5217           optimize_tls_reloc(gsym->final_value_is_known(), r_type);
5218         if (tlsopt == tls::TLSOPT_TO_LE)
5219           break;
5220
5221         layout->set_has_static_tls();
5222         // Create a GOT entry for the tp-relative offset.
5223         Output_data_got_aarch64<size, big_endian>* got
5224           = target->got_section(symtab, layout);
5225         if (!parameters->doing_static_link())
5226           {
5227             got->add_global_with_rel(
5228               gsym, GOT_TYPE_TLS_OFFSET,
5229               target->rela_dyn_section(layout),
5230               elfcpp::R_AARCH64_TLS_TPREL64);
5231           }
5232         if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
5233           {
5234             got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
5235             unsigned int got_offset =
5236               gsym->got_offset(GOT_TYPE_TLS_OFFSET);
5237             const elfcpp::Elf_Xword addend = rela.get_r_addend();
5238             gold_assert(addend == 0);
5239             got->add_static_reloc(got_offset,
5240                                   elfcpp::R_AARCH64_TLS_TPREL64, gsym);
5241           }
5242       }
5243       break;
5244
5245     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5246     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5247     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:  // Local executable
5248       layout->set_has_static_tls();
5249       if (parameters->options().shared())
5250         gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
5251                    object->name().c_str(), r_type);
5252       break;
5253
5254     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5255     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5256     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:  // TLS descriptor
5257       {
5258         target->define_tls_base_symbol(symtab, layout);
5259         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5260             optimize_tls_reloc(gsym->final_value_is_known(), r_type);
5261         if (tlsopt == tls::TLSOPT_NONE)
5262           {
5263             // Create reserved PLT and GOT entries for the resolver.
5264             target->reserve_tlsdesc_entries(symtab, layout);
5265
5266             // Create a double GOT entry with an R_AARCH64_TLSDESC
5267             // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
5268             // entry needs to be in an area in .got.plt, not .got. Call
5269             // got_section to make sure the section has been created.
5270             target->got_section(symtab, layout);
5271             Output_data_got<size, big_endian>* got =
5272                 target->got_tlsdesc_section();
5273             Reloc_section* rt = target->rela_tlsdesc_section(layout);
5274             got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
5275                                           elfcpp::R_AARCH64_TLSDESC, 0);
5276           }
5277         else if (tlsopt == tls::TLSOPT_TO_IE)
5278           {
5279             // Create a GOT entry for the tp-relative offset.
5280             Output_data_got<size, big_endian>* got
5281                 = target->got_section(symtab, layout);
5282             got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
5283                                      target->rela_dyn_section(layout),
5284                                      elfcpp::R_AARCH64_TLS_TPREL64);
5285           }
5286         else if (tlsopt != tls::TLSOPT_TO_LE)
5287           unsupported_reloc_global(object, r_type, gsym);
5288       }
5289       break;
5290
5291     case elfcpp::R_AARCH64_TLSDESC_CALL:
5292       break;
5293
5294     default:
5295       gold_error(_("%s: unsupported reloc type in global scan"),
5296                  aarch64_reloc_property_table->
5297                  reloc_name_in_error_message(r_type).c_str());
5298     }
5299   return;
5300 }  // End of Scan::global
5301
5302
5303 // Create the PLT section.
5304 template<int size, bool big_endian>
5305 void
5306 Target_aarch64<size, big_endian>::make_plt_section(
5307   Symbol_table* symtab, Layout* layout)
5308 {
5309   if (this->plt_ == NULL)
5310     {
5311       // Create the GOT section first.
5312       this->got_section(symtab, layout);
5313
5314       this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
5315                                        this->got_irelative_);
5316
5317       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
5318                                       (elfcpp::SHF_ALLOC
5319                                        | elfcpp::SHF_EXECINSTR),
5320                                       this->plt_, ORDER_PLT, false);
5321
5322       // Make the sh_info field of .rela.plt point to .plt.
5323       Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
5324       rela_plt_os->set_info_section(this->plt_->output_section());
5325     }
5326 }
5327
5328 // Return the section for TLSDESC relocations.
5329
5330 template<int size, bool big_endian>
5331 typename Target_aarch64<size, big_endian>::Reloc_section*
5332 Target_aarch64<size, big_endian>::rela_tlsdesc_section(Layout* layout) const
5333 {
5334   return this->plt_section()->rela_tlsdesc(layout);
5335 }
5336
5337 // Create a PLT entry for a global symbol.
5338
5339 template<int size, bool big_endian>
5340 void
5341 Target_aarch64<size, big_endian>::make_plt_entry(
5342     Symbol_table* symtab,
5343     Layout* layout,
5344     Symbol* gsym)
5345 {
5346   if (gsym->has_plt_offset())
5347     return;
5348
5349   if (this->plt_ == NULL)
5350     this->make_plt_section(symtab, layout);
5351
5352   this->plt_->add_entry(symtab, layout, gsym);
5353 }
5354
5355 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5356
5357 template<int size, bool big_endian>
5358 void
5359 Target_aarch64<size, big_endian>::make_local_ifunc_plt_entry(
5360     Symbol_table* symtab, Layout* layout,
5361     Sized_relobj_file<size, big_endian>* relobj,
5362     unsigned int local_sym_index)
5363 {
5364   if (relobj->local_has_plt_offset(local_sym_index))
5365     return;
5366   if (this->plt_ == NULL)
5367     this->make_plt_section(symtab, layout);
5368   unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
5369                                                               relobj,
5370                                                               local_sym_index);
5371   relobj->set_local_plt_offset(local_sym_index, plt_offset);
5372 }
5373
5374 template<int size, bool big_endian>
5375 void
5376 Target_aarch64<size, big_endian>::gc_process_relocs(
5377     Symbol_table* symtab,
5378     Layout* layout,
5379     Sized_relobj_file<size, big_endian>* object,
5380     unsigned int data_shndx,
5381     unsigned int sh_type,
5382     const unsigned char* prelocs,
5383     size_t reloc_count,
5384     Output_section* output_section,
5385     bool needs_special_offset_handling,
5386     size_t local_symbol_count,
5387     const unsigned char* plocal_symbols)
5388 {
5389   if (sh_type == elfcpp::SHT_REL)
5390     {
5391       return;
5392     }
5393
5394   gold::gc_process_relocs<
5395     size, big_endian,
5396     Target_aarch64<size, big_endian>,
5397     elfcpp::SHT_RELA,
5398     typename Target_aarch64<size, big_endian>::Scan,
5399     typename Target_aarch64<size, big_endian>::Relocatable_size_for_reloc>(
5400     symtab,
5401     layout,
5402     this,
5403     object,
5404     data_shndx,
5405     prelocs,
5406     reloc_count,
5407     output_section,
5408     needs_special_offset_handling,
5409     local_symbol_count,
5410     plocal_symbols);
5411 }
5412
5413 // Scan relocations for a section.
5414
5415 template<int size, bool big_endian>
5416 void
5417 Target_aarch64<size, big_endian>::scan_relocs(
5418     Symbol_table* symtab,
5419     Layout* layout,
5420     Sized_relobj_file<size, big_endian>* object,
5421     unsigned int data_shndx,
5422     unsigned int sh_type,
5423     const unsigned char* prelocs,
5424     size_t reloc_count,
5425     Output_section* output_section,
5426     bool needs_special_offset_handling,
5427     size_t local_symbol_count,
5428     const unsigned char* plocal_symbols)
5429 {
5430   if (sh_type == elfcpp::SHT_REL)
5431     {
5432       gold_error(_("%s: unsupported REL reloc section"),
5433                  object->name().c_str());
5434       return;
5435     }
5436   gold::scan_relocs<size, big_endian, Target_aarch64, elfcpp::SHT_RELA, Scan>(
5437     symtab,
5438     layout,
5439     this,
5440     object,
5441     data_shndx,
5442     prelocs,
5443     reloc_count,
5444     output_section,
5445     needs_special_offset_handling,
5446     local_symbol_count,
5447     plocal_symbols);
5448 }
5449
5450 // Return the value to use for a dynamic which requires special
5451 // treatment.  This is how we support equality comparisons of function
5452 // pointers across shared library boundaries, as described in the
5453 // processor specific ABI supplement.
5454
5455 template<int size, bool big_endian>
5456 uint64_t
5457 Target_aarch64<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
5458 {
5459   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5460   return this->plt_address_for_global(gsym);
5461 }
5462
5463
5464 // Finalize the sections.
5465
5466 template<int size, bool big_endian>
5467 void
5468 Target_aarch64<size, big_endian>::do_finalize_sections(
5469     Layout* layout,
5470     const Input_objects*,
5471     Symbol_table* symtab)
5472 {
5473   const Reloc_section* rel_plt = (this->plt_ == NULL
5474                                   ? NULL
5475                                   : this->plt_->rela_plt());
5476   layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
5477                                   this->rela_dyn_, true, false);
5478
5479   // Emit any relocs we saved in an attempt to avoid generating COPY
5480   // relocs.
5481   if (this->copy_relocs_.any_saved_relocs())
5482     this->copy_relocs_.emit(this->rela_dyn_section(layout));
5483
5484   // Fill in some more dynamic tags.
5485   Output_data_dynamic* const odyn = layout->dynamic_data();
5486   if (odyn != NULL)
5487     {
5488       if (this->plt_ != NULL
5489           && this->plt_->output_section() != NULL
5490           && this->plt_ ->has_tlsdesc_entry())
5491         {
5492           unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
5493           unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
5494           this->got_->finalize_data_size();
5495           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
5496                                         this->plt_, plt_offset);
5497           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
5498                                         this->got_, got_offset);
5499         }
5500     }
5501
5502   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
5503   // the .got.plt section.
5504   Symbol* sym = this->global_offset_table_;
5505   if (sym != NULL)
5506     {
5507       uint64_t data_size = this->got_plt_->current_data_size();
5508       symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
5509
5510       // If the .got section is more than 0x8000 bytes, we add
5511       // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
5512       // bit relocations have a greater chance of working.
5513       if (data_size >= 0x8000)
5514         symtab->get_sized_symbol<size>(sym)->set_value(
5515           symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
5516     }
5517
5518   if (parameters->doing_static_link()
5519       && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
5520     {
5521       // If linking statically, make sure that the __rela_iplt symbols
5522       // were defined if necessary, even if we didn't create a PLT.
5523       static const Define_symbol_in_segment syms[] =
5524         {
5525           {
5526             "__rela_iplt_start",        // name
5527             elfcpp::PT_LOAD,            // segment_type
5528             elfcpp::PF_W,               // segment_flags_set
5529             elfcpp::PF(0),              // segment_flags_clear
5530             0,                          // value
5531             0,                          // size
5532             elfcpp::STT_NOTYPE,         // type
5533             elfcpp::STB_GLOBAL,         // binding
5534             elfcpp::STV_HIDDEN,         // visibility
5535             0,                          // nonvis
5536             Symbol::SEGMENT_START,      // offset_from_base
5537             true                        // only_if_ref
5538           },
5539           {
5540             "__rela_iplt_end",          // name
5541             elfcpp::PT_LOAD,            // segment_type
5542             elfcpp::PF_W,               // segment_flags_set
5543             elfcpp::PF(0),              // segment_flags_clear
5544             0,                          // value
5545             0,                          // size
5546             elfcpp::STT_NOTYPE,         // type
5547             elfcpp::STB_GLOBAL,         // binding
5548             elfcpp::STV_HIDDEN,         // visibility
5549             0,                          // nonvis
5550             Symbol::SEGMENT_START,      // offset_from_base
5551             true                        // only_if_ref
5552           }
5553         };
5554
5555       symtab->define_symbols(layout, 2, syms,
5556                              layout->script_options()->saw_sections_clause());
5557     }
5558
5559   return;
5560 }
5561
5562 // Perform a relocation.
5563
5564 template<int size, bool big_endian>
5565 inline bool
5566 Target_aarch64<size, big_endian>::Relocate::relocate(
5567     const Relocate_info<size, big_endian>* relinfo,
5568     Target_aarch64<size, big_endian>* target,
5569     Output_section* ,
5570     size_t relnum,
5571     const elfcpp::Rela<size, big_endian>& rela,
5572     unsigned int r_type,
5573     const Sized_symbol<size>* gsym,
5574     const Symbol_value<size>* psymval,
5575     unsigned char* view,
5576     typename elfcpp::Elf_types<size>::Elf_Addr address,
5577     section_size_type /* view_size */)
5578 {
5579   if (view == NULL)
5580     return true;
5581
5582   typedef AArch64_relocate_functions<size, big_endian> Reloc;
5583
5584   const AArch64_reloc_property* reloc_property =
5585       aarch64_reloc_property_table->get_reloc_property(r_type);
5586
5587   if (reloc_property == NULL)
5588     {
5589       std::string reloc_name =
5590           aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
5591       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5592                              _("cannot relocate %s in object file"),
5593                              reloc_name.c_str());
5594       return true;
5595     }
5596
5597   const Sized_relobj_file<size, big_endian>* object = relinfo->object;
5598
5599   // Pick the value to use for symbols defined in the PLT.
5600   Symbol_value<size> symval;
5601   if (gsym != NULL
5602       && gsym->use_plt_offset(reloc_property->reference_flags()))
5603     {
5604       symval.set_output_value(target->plt_address_for_global(gsym));
5605       psymval = &symval;
5606     }
5607   else if (gsym == NULL && psymval->is_ifunc_symbol())
5608     {
5609       unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5610       if (object->local_has_plt_offset(r_sym))
5611         {
5612           symval.set_output_value(target->plt_address_for_local(object, r_sym));
5613           psymval = &symval;
5614         }
5615     }
5616
5617   const elfcpp::Elf_Xword addend = rela.get_r_addend();
5618
5619   // Get the GOT offset if needed.
5620   // For aarch64, the GOT pointer points to the start of the GOT section.
5621   bool have_got_offset = false;
5622   int got_offset = 0;
5623   int got_base = (target->got_ != NULL
5624                   ? (target->got_->current_data_size() >= 0x8000
5625                      ? 0x8000 : 0)
5626                   : 0);
5627   switch (r_type)
5628     {
5629     case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
5630     case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
5631     case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
5632     case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
5633     case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
5634     case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
5635     case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
5636     case elfcpp::R_AARCH64_GOTREL64:
5637     case elfcpp::R_AARCH64_GOTREL32:
5638     case elfcpp::R_AARCH64_GOT_LD_PREL19:
5639     case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
5640     case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5641     case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5642     case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
5643       if (gsym != NULL)
5644         {
5645           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
5646           got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
5647         }
5648       else
5649         {
5650           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5651           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
5652           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
5653                         - got_base);
5654         }
5655       have_got_offset = true;
5656       break;
5657
5658     default:
5659       break;
5660     }
5661
5662   typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
5663   typename elfcpp::Elf_types<size>::Elf_Addr value;
5664   switch (r_type)
5665     {
5666     case elfcpp::R_AARCH64_NONE:
5667       break;
5668
5669     case elfcpp::R_AARCH64_ABS64:
5670       reloc_status = Reloc::template rela_ua<64>(
5671         view, object, psymval, addend, reloc_property);
5672       break;
5673
5674     case elfcpp::R_AARCH64_ABS32:
5675       reloc_status = Reloc::template rela_ua<32>(
5676         view, object, psymval, addend, reloc_property);
5677       break;
5678
5679     case elfcpp::R_AARCH64_ABS16:
5680       reloc_status = Reloc::template rela_ua<16>(
5681         view, object, psymval, addend, reloc_property);
5682       break;
5683
5684     case elfcpp::R_AARCH64_PREL64:
5685       reloc_status = Reloc::template pcrela_ua<64>(
5686         view, object, psymval, addend, address, reloc_property);
5687       break;
5688
5689     case elfcpp::R_AARCH64_PREL32:
5690       reloc_status = Reloc::template pcrela_ua<32>(
5691         view, object, psymval, addend, address, reloc_property);
5692       break;
5693
5694     case elfcpp::R_AARCH64_PREL16:
5695       reloc_status = Reloc::template pcrela_ua<16>(
5696         view, object, psymval, addend, address, reloc_property);
5697       break;
5698
5699     case elfcpp::R_AARCH64_LD_PREL_LO19:
5700       reloc_status = Reloc::template pcrela_general<32>(
5701           view, object, psymval, addend, address, reloc_property);
5702       break;
5703
5704     case elfcpp::R_AARCH64_ADR_PREL_LO21:
5705       reloc_status = Reloc::adr(view, object, psymval, addend,
5706                                 address, reloc_property);
5707       break;
5708
5709     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
5710     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
5711       reloc_status = Reloc::adrp(view, object, psymval, addend, address,
5712                                  reloc_property);
5713       break;
5714
5715     case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
5716     case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
5717     case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
5718     case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
5719     case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
5720     case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
5721       reloc_status = Reloc::template rela_general<32>(
5722         view, object, psymval, addend, reloc_property);
5723       break;
5724
5725     case elfcpp::R_AARCH64_CALL26:
5726       if (this->skip_call_tls_get_addr_)
5727         {
5728           // Double check that the TLSGD insn has been optimized away.
5729           typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
5730           Insntype insn = elfcpp::Swap<32, big_endian>::readval(
5731               reinterpret_cast<Insntype*>(view));
5732           gold_assert((insn & 0xff000000) == 0x91000000);
5733
5734           reloc_status = Reloc::STATUS_OKAY;
5735           this->skip_call_tls_get_addr_ = false;
5736           // Return false to stop further processing this reloc.
5737           return false;
5738         }
5739       // Fallthrough
5740     case elfcpp::R_AARCH64_JUMP26:
5741       if (Reloc::maybe_apply_stub(r_type, relinfo, rela, view, address,
5742                                   gsym, psymval, object))
5743         break;
5744       // Fallthrough
5745     case elfcpp::R_AARCH64_TSTBR14:
5746     case elfcpp::R_AARCH64_CONDBR19:
5747       reloc_status = Reloc::template pcrela_general<32>(
5748         view, object, psymval, addend, address, reloc_property);
5749       break;
5750
5751     case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5752       gold_assert(have_got_offset);
5753       value = target->got_->address() + got_base + got_offset;
5754       reloc_status = Reloc::adrp(view, value + addend, address);
5755       break;
5756
5757     case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5758       gold_assert(have_got_offset);
5759       value = target->got_->address() + got_base + got_offset;
5760       reloc_status = Reloc::template rela_general<32>(
5761         view, value, addend, reloc_property);
5762       break;
5763
5764     case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5765     case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5766     case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5767     case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5768     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5769     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5770     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5771     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5772     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5773     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5774     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5775     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5776     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5777     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5778     case elfcpp::R_AARCH64_TLSDESC_CALL:
5779       reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
5780                                   gsym, psymval, view, address);
5781       break;
5782
5783     // These are dynamic relocations, which are unexpected when linking.
5784     case elfcpp::R_AARCH64_COPY:
5785     case elfcpp::R_AARCH64_GLOB_DAT:
5786     case elfcpp::R_AARCH64_JUMP_SLOT:
5787     case elfcpp::R_AARCH64_RELATIVE:
5788     case elfcpp::R_AARCH64_IRELATIVE:
5789     case elfcpp::R_AARCH64_TLS_DTPREL64:
5790     case elfcpp::R_AARCH64_TLS_DTPMOD64:
5791     case elfcpp::R_AARCH64_TLS_TPREL64:
5792     case elfcpp::R_AARCH64_TLSDESC:
5793       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5794                              _("unexpected reloc %u in object file"),
5795                              r_type);
5796       break;
5797
5798     default:
5799       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5800                              _("unsupported reloc %s"),
5801                              reloc_property->name().c_str());
5802       break;
5803     }
5804
5805   // Report any errors.
5806   switch (reloc_status)
5807     {
5808     case Reloc::STATUS_OKAY:
5809       break;
5810     case Reloc::STATUS_OVERFLOW:
5811       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5812                              _("relocation overflow in %s"),
5813                              reloc_property->name().c_str());
5814       break;
5815     case Reloc::STATUS_BAD_RELOC:
5816       gold_error_at_location(
5817           relinfo,
5818           relnum,
5819           rela.get_r_offset(),
5820           _("unexpected opcode while processing relocation %s"),
5821           reloc_property->name().c_str());
5822       break;
5823     default:
5824       gold_unreachable();
5825     }
5826
5827   return true;
5828 }
5829
5830
5831 template<int size, bool big_endian>
5832 inline
5833 typename AArch64_relocate_functions<size, big_endian>::Status
5834 Target_aarch64<size, big_endian>::Relocate::relocate_tls(
5835     const Relocate_info<size, big_endian>* relinfo,
5836     Target_aarch64<size, big_endian>* target,
5837     size_t relnum,
5838     const elfcpp::Rela<size, big_endian>& rela,
5839     unsigned int r_type, const Sized_symbol<size>* gsym,
5840     const Symbol_value<size>* psymval,
5841     unsigned char* view,
5842     typename elfcpp::Elf_types<size>::Elf_Addr address)
5843 {
5844   typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
5845   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
5846
5847   Output_segment* tls_segment = relinfo->layout->tls_segment();
5848   const elfcpp::Elf_Xword addend = rela.get_r_addend();
5849   const AArch64_reloc_property* reloc_property =
5850       aarch64_reloc_property_table->get_reloc_property(r_type);
5851   gold_assert(reloc_property != NULL);
5852
5853   const bool is_final = (gsym == NULL
5854                          ? !parameters->options().shared()
5855                          : gsym->final_value_is_known());
5856   tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5857       optimize_tls_reloc(is_final, r_type);
5858
5859   Sized_relobj_file<size, big_endian>* object = relinfo->object;
5860   int tls_got_offset_type;
5861   switch (r_type)
5862     {
5863     case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5864     case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:  // Global-dynamic
5865       {
5866         if (tlsopt == tls::TLSOPT_TO_LE)
5867           {
5868             if (tls_segment == NULL)
5869               {
5870                 gold_assert(parameters->errors()->error_count() > 0
5871                             || issue_undefined_symbol_error(gsym));
5872                 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
5873               }
5874             return tls_gd_to_le(relinfo, target, rela, r_type, view,
5875                                 psymval);
5876           }
5877         else if (tlsopt == tls::TLSOPT_NONE)
5878           {
5879             tls_got_offset_type = GOT_TYPE_TLS_PAIR;
5880             // Firstly get the address for the got entry.
5881             typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
5882             if (gsym != NULL)
5883               {
5884                 gold_assert(gsym->has_got_offset(tls_got_offset_type));
5885                 got_entry_address = target->got_->address() +
5886                                     gsym->got_offset(tls_got_offset_type);
5887               }
5888             else
5889               {
5890                 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5891                 gold_assert(
5892                   object->local_has_got_offset(r_sym, tls_got_offset_type));
5893                 got_entry_address = target->got_->address() +
5894                   object->local_got_offset(r_sym, tls_got_offset_type);
5895               }
5896
5897             // Relocate the address into adrp/ld, adrp/add pair.
5898             switch (r_type)
5899               {
5900               case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5901                 return aarch64_reloc_funcs::adrp(
5902                   view, got_entry_address + addend, address);
5903
5904                 break;
5905
5906               case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5907                 return aarch64_reloc_funcs::template rela_general<32>(
5908                   view, got_entry_address, addend, reloc_property);
5909                 break;
5910
5911               default:
5912                 gold_unreachable();
5913               }
5914           }
5915         gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5916                                _("unsupported gd_to_ie relaxation on %u"),
5917                                r_type);
5918       }
5919       break;
5920
5921     case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5922     case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:  // Local-dynamic
5923       {
5924         if (tlsopt == tls::TLSOPT_TO_LE)
5925           {
5926             if (tls_segment == NULL)
5927               {
5928                 gold_assert(parameters->errors()->error_count() > 0
5929                             || issue_undefined_symbol_error(gsym));
5930                 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
5931               }
5932             return this->tls_ld_to_le(relinfo, target, rela, r_type, view,
5933                                       psymval);
5934           }
5935
5936         gold_assert(tlsopt == tls::TLSOPT_NONE);
5937         // Relocate the field with the offset of the GOT entry for
5938         // the module index.
5939         typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
5940         got_entry_address = (target->got_mod_index_entry(NULL, NULL, NULL) +
5941                              target->got_->address());
5942
5943         switch (r_type)
5944           {
5945           case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5946             return aarch64_reloc_funcs::adrp(
5947               view, got_entry_address + addend, address);
5948             break;
5949
5950           case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5951             return aarch64_reloc_funcs::template rela_general<32>(
5952               view, got_entry_address, addend, reloc_property);
5953             break;
5954
5955           default:
5956             gold_unreachable();
5957           }
5958       }
5959       break;
5960
5961     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5962     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:  // Other local-dynamic
5963       {
5964         AArch64_address value = psymval->value(object, 0);
5965         if (tlsopt == tls::TLSOPT_TO_LE)
5966           {
5967             if (tls_segment == NULL)
5968               {
5969                 gold_assert(parameters->errors()->error_count() > 0
5970                             || issue_undefined_symbol_error(gsym));
5971                 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
5972               }
5973           // If building executable, _TLS_MODULE_BASE_ points to segment
5974           // end. Thus we must subtract it from value.
5975           value -= tls_segment->memsz();
5976           }
5977         switch (r_type)
5978           {
5979           case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5980             return aarch64_reloc_funcs::movnz(view, value + addend,
5981                                               reloc_property);
5982             break;
5983
5984           case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5985             return aarch64_reloc_funcs::template rela_general<32>(
5986                 view, value, addend, reloc_property);
5987             break;
5988
5989           default:
5990             gold_unreachable();
5991           }
5992         // We should never reach here.
5993       }
5994       break;
5995
5996     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5997     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:  // Initial-exec
5998       {
5999         if (tlsopt == tls::TLSOPT_TO_LE)
6000           {
6001             if (tls_segment == NULL)
6002               {
6003                 gold_assert(parameters->errors()->error_count() > 0
6004                             || issue_undefined_symbol_error(gsym));
6005                 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6006               }
6007             return tls_ie_to_le(relinfo, target, rela, r_type, view,
6008                                 psymval);
6009           }
6010         tls_got_offset_type = GOT_TYPE_TLS_OFFSET;
6011
6012         // Firstly get the address for the got entry.
6013         typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6014         if (gsym != NULL)
6015           {
6016             gold_assert(gsym->has_got_offset(tls_got_offset_type));
6017             got_entry_address = target->got_->address() +
6018                                 gsym->got_offset(tls_got_offset_type);
6019           }
6020         else
6021           {
6022             unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6023             gold_assert(
6024                 object->local_has_got_offset(r_sym, tls_got_offset_type));
6025             got_entry_address = target->got_->address() +
6026                 object->local_got_offset(r_sym, tls_got_offset_type);
6027           }
6028         // Relocate the address into adrp/ld, adrp/add pair.
6029         switch (r_type)
6030           {
6031           case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6032             return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
6033                                              address);
6034             break;
6035           case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6036             return aarch64_reloc_funcs::template rela_general<32>(
6037               view, got_entry_address, addend, reloc_property);
6038           default:
6039             gold_unreachable();
6040           }
6041       }
6042       // We shall never reach here.
6043       break;
6044
6045     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6046     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6047     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6048       {
6049         gold_assert(tls_segment != NULL);
6050         AArch64_address value = psymval->value(object, 0);
6051
6052         if (!parameters->options().shared())
6053           {
6054             AArch64_address aligned_tcb_size =
6055                 align_address(target->tcb_size(),
6056                               tls_segment->maximum_alignment());
6057             return aarch64_reloc_funcs::template
6058                 rela_general<32>(view,
6059                                  value + aligned_tcb_size,
6060                                  addend,
6061                                  reloc_property);
6062           }
6063         else
6064           gold_error(_("%s: unsupported reloc %u "
6065                        "in non-static TLSLE mode."),
6066                      object->name().c_str(), r_type);
6067       }
6068       break;
6069
6070     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6071     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6072     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6073     case elfcpp::R_AARCH64_TLSDESC_CALL:
6074       {
6075         if (tlsopt == tls::TLSOPT_TO_LE)
6076           {
6077             if (tls_segment == NULL)
6078               {
6079                 gold_assert(parameters->errors()->error_count() > 0
6080                             || issue_undefined_symbol_error(gsym));
6081                 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6082               }
6083             return tls_desc_gd_to_le(relinfo, target, rela, r_type,
6084                                      view, psymval);
6085           }
6086         else
6087           {
6088             tls_got_offset_type = (tlsopt == tls::TLSOPT_TO_IE
6089                                    ? GOT_TYPE_TLS_OFFSET
6090                                    : GOT_TYPE_TLS_DESC);
6091             unsigned int got_tlsdesc_offset = 0;
6092             if (r_type != elfcpp::R_AARCH64_TLSDESC_CALL
6093                 && tlsopt == tls::TLSOPT_NONE)
6094               {
6095                 // We created GOT entries in the .got.tlsdesc portion of the
6096                 // .got.plt section, but the offset stored in the symbol is the
6097                 // offset within .got.tlsdesc.
6098                 got_tlsdesc_offset = (target->got_->data_size()
6099                                       + target->got_plt_section()->data_size());
6100               }
6101             typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6102             if (gsym != NULL)
6103               {
6104                 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6105                 got_entry_address = target->got_->address()
6106                                     + got_tlsdesc_offset
6107                                     + gsym->got_offset(tls_got_offset_type);
6108               }
6109             else
6110               {
6111                 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6112                 gold_assert(
6113                     object->local_has_got_offset(r_sym, tls_got_offset_type));
6114                 got_entry_address = target->got_->address() +
6115                   got_tlsdesc_offset +
6116                   object->local_got_offset(r_sym, tls_got_offset_type);
6117               }
6118             if (tlsopt == tls::TLSOPT_TO_IE)
6119               {
6120                 if (tls_segment == NULL)
6121                   {
6122                     gold_assert(parameters->errors()->error_count() > 0
6123                                 || issue_undefined_symbol_error(gsym));
6124                     return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6125                   }
6126                 return tls_desc_gd_to_ie(relinfo, target, rela, r_type,
6127                                          view, psymval, got_entry_address,
6128                                          address);
6129               }
6130
6131             // Now do tlsdesc relocation.
6132             switch (r_type)
6133               {
6134               case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6135                 return aarch64_reloc_funcs::adrp(view,
6136                                                  got_entry_address + addend,
6137                                                  address);
6138                 break;
6139               case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6140               case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6141                 return aarch64_reloc_funcs::template rela_general<32>(
6142                   view, got_entry_address, addend, reloc_property);
6143                 break;
6144               case elfcpp::R_AARCH64_TLSDESC_CALL:
6145                 return aarch64_reloc_funcs::STATUS_OKAY;
6146                 break;
6147               default:
6148                 gold_unreachable();
6149               }
6150           }
6151         }
6152       break;
6153
6154     default:
6155       gold_error(_("%s: unsupported TLS reloc %u."),
6156                  object->name().c_str(), r_type);
6157     }
6158   return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6159 }  // End of relocate_tls.
6160
6161
6162 template<int size, bool big_endian>
6163 inline
6164 typename AArch64_relocate_functions<size, big_endian>::Status
6165 Target_aarch64<size, big_endian>::Relocate::tls_gd_to_le(
6166              const Relocate_info<size, big_endian>* relinfo,
6167              Target_aarch64<size, big_endian>* target,
6168              const elfcpp::Rela<size, big_endian>& rela,
6169              unsigned int r_type,
6170              unsigned char* view,
6171              const Symbol_value<size>* psymval)
6172 {
6173   typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6174   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6175   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6176
6177   Insntype* ip = reinterpret_cast<Insntype*>(view);
6178   Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
6179   Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
6180   Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
6181
6182   if (r_type == elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC)
6183     {
6184       // This is the 2nd relocs, optimization should already have been
6185       // done.
6186       gold_assert((insn1 & 0xfff00000) == 0x91400000);
6187       return aarch64_reloc_funcs::STATUS_OKAY;
6188     }
6189
6190   // The original sequence is -
6191   //   90000000        adrp    x0, 0 <main>
6192   //   91000000        add     x0, x0, #0x0
6193   //   94000000        bl      0 <__tls_get_addr>
6194   // optimized to sequence -
6195   //   d53bd040        mrs     x0, tpidr_el0
6196   //   91400000        add     x0, x0, #0x0, lsl #12
6197   //   91000000        add     x0, x0, #0x0
6198
6199   // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
6200   // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
6201   // have to change "bl tls_get_addr", which does not have a corresponding tls
6202   // relocation type. So before proceeding, we need to make sure compiler
6203   // does not change the sequence.
6204   if(!(insn1 == 0x90000000      // adrp x0,0
6205        && insn2 == 0x91000000   // add x0, x0, #0x0
6206        && insn3 == 0x94000000)) // bl 0
6207     {
6208       // Ideally we should give up gd_to_le relaxation and do gd access.
6209       // However the gd_to_le relaxation decision has been made early
6210       // in the scan stage, where we did not allocate any GOT entry for
6211       // this symbol. Therefore we have to exit and report error now.
6212       gold_error(_("unexpected reloc insn sequence while relaxing "
6213                    "tls gd to le for reloc %u."), r_type);
6214       return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6215     }
6216
6217   // Write new insns.
6218   insn1 = 0xd53bd040;  // mrs x0, tpidr_el0
6219   insn2 = 0x91400000;  // add x0, x0, #0x0, lsl #12
6220   insn3 = 0x91000000;  // add x0, x0, #0x0
6221   elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
6222   elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
6223   elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
6224
6225   // Calculate tprel value.
6226   Output_segment* tls_segment = relinfo->layout->tls_segment();
6227   gold_assert(tls_segment != NULL);
6228   AArch64_address value = psymval->value(relinfo->object, 0);
6229   const elfcpp::Elf_Xword addend = rela.get_r_addend();
6230   AArch64_address aligned_tcb_size =
6231       align_address(target->tcb_size(), tls_segment->maximum_alignment());
6232   AArch64_address x = value + aligned_tcb_size;
6233
6234   // After new insns are written, apply TLSLE relocs.
6235   const AArch64_reloc_property* rp1 =
6236       aarch64_reloc_property_table->get_reloc_property(
6237           elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
6238   const AArch64_reloc_property* rp2 =
6239       aarch64_reloc_property_table->get_reloc_property(
6240           elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
6241   gold_assert(rp1 != NULL && rp2 != NULL);
6242
6243   typename aarch64_reloc_funcs::Status s1 =
6244       aarch64_reloc_funcs::template rela_general<32>(view + 4,
6245                                                      x,
6246                                                      addend,
6247                                                      rp1);
6248   if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
6249     return s1;
6250
6251   typename aarch64_reloc_funcs::Status s2 =
6252       aarch64_reloc_funcs::template rela_general<32>(view + 8,
6253                                                      x,
6254                                                      addend,
6255                                                      rp2);
6256
6257   this->skip_call_tls_get_addr_ = true;
6258   return s2;
6259 }  // End of tls_gd_to_le
6260
6261
6262 template<int size, bool big_endian>
6263 inline
6264 typename AArch64_relocate_functions<size, big_endian>::Status
6265 Target_aarch64<size, big_endian>::Relocate::tls_ld_to_le(
6266              const Relocate_info<size, big_endian>* relinfo,
6267              Target_aarch64<size, big_endian>* target,
6268              const elfcpp::Rela<size, big_endian>& rela,
6269              unsigned int r_type,
6270              unsigned char* view,
6271              const Symbol_value<size>* psymval)
6272 {
6273   typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6274   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6275   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6276
6277   Insntype* ip = reinterpret_cast<Insntype*>(view);
6278   Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
6279   Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
6280   Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
6281
6282   if (r_type == elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC)
6283     {
6284       // This is the 2nd relocs, optimization should already have been
6285       // done.
6286       gold_assert((insn1 & 0xfff00000) == 0x91400000);
6287       return aarch64_reloc_funcs::STATUS_OKAY;
6288     }
6289
6290   // The original sequence is -
6291   //   90000000        adrp    x0, 0 <main>
6292   //   91000000        add     x0, x0, #0x0
6293   //   94000000        bl      0 <__tls_get_addr>
6294   // optimized to sequence -
6295   //   d53bd040        mrs     x0, tpidr_el0
6296   //   91400000        add     x0, x0, #0x0, lsl #12
6297   //   91000000        add     x0, x0, #0x0
6298
6299   // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
6300   // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
6301   // have to change "bl tls_get_addr", which does not have a corresponding tls
6302   // relocation type. So before proceeding, we need to make sure compiler
6303   // does not change the sequence.
6304   if(!(insn1 == 0x90000000      // adrp x0,0
6305        && insn2 == 0x91000000   // add x0, x0, #0x0
6306        && insn3 == 0x94000000)) // bl 0
6307     {
6308       // Ideally we should give up gd_to_le relaxation and do gd access.
6309       // However the gd_to_le relaxation decision has been made early
6310       // in the scan stage, where we did not allocate any GOT entry for
6311       // this symbol. Therefore we have to exit and report error now.
6312       gold_error(_("unexpected reloc insn sequence while relaxing "
6313                    "tls gd to le for reloc %u."), r_type);
6314       return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6315     }
6316
6317   // Write new insns.
6318   insn1 = 0xd53bd040;  // mrs x0, tpidr_el0
6319   insn2 = 0x91400000;  // add x0, x0, #0x0, lsl #12
6320   insn3 = 0x91000000;  // add x0, x0, #0x0
6321   elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
6322   elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
6323   elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
6324
6325   // Calculate tprel value.
6326   Output_segment* tls_segment = relinfo->layout->tls_segment();
6327   gold_assert(tls_segment != NULL);
6328   AArch64_address value = psymval->value(relinfo->object, 0);
6329   const elfcpp::Elf_Xword addend = rela.get_r_addend();
6330   AArch64_address aligned_tcb_size =
6331       align_address(target->tcb_size(), tls_segment->maximum_alignment());
6332   AArch64_address x = value + aligned_tcb_size;
6333
6334   // After new insns are written, apply TLSLE relocs.
6335   const AArch64_reloc_property* rp1 =
6336       aarch64_reloc_property_table->get_reloc_property(
6337           elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
6338   const AArch64_reloc_property* rp2 =
6339       aarch64_reloc_property_table->get_reloc_property(
6340           elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
6341   gold_assert(rp1 != NULL && rp2 != NULL);
6342
6343   typename aarch64_reloc_funcs::Status s1 =
6344       aarch64_reloc_funcs::template rela_general<32>(view + 4,
6345                                                      x,
6346                                                      addend,
6347                                                      rp1);
6348   if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
6349     return s1;
6350
6351   typename aarch64_reloc_funcs::Status s2 =
6352       aarch64_reloc_funcs::template rela_general<32>(view + 8,
6353                                                      x,
6354                                                      addend,
6355                                                      rp2);
6356
6357   this->skip_call_tls_get_addr_ = true;
6358   return s2;
6359
6360 }  // End of tls_ld_to_le
6361
6362 template<int size, bool big_endian>
6363 inline
6364 typename AArch64_relocate_functions<size, big_endian>::Status
6365 Target_aarch64<size, big_endian>::Relocate::tls_ie_to_le(
6366              const Relocate_info<size, big_endian>* relinfo,
6367              Target_aarch64<size, big_endian>* target,
6368              const elfcpp::Rela<size, big_endian>& rela,
6369              unsigned int r_type,
6370              unsigned char* view,
6371              const Symbol_value<size>* psymval)
6372 {
6373   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6374   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6375   typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6376
6377   AArch64_address value = psymval->value(relinfo->object, 0);
6378   Output_segment* tls_segment = relinfo->layout->tls_segment();
6379   AArch64_address aligned_tcb_address =
6380       align_address(target->tcb_size(), tls_segment->maximum_alignment());
6381   const elfcpp::Elf_Xword addend = rela.get_r_addend();
6382   AArch64_address x = value + addend + aligned_tcb_address;
6383   // "x" is the offset to tp, we can only do this if x is within
6384   // range [0, 2^32-1]
6385   if (!(size == 32 || (size == 64 && (static_cast<uint64_t>(x) >> 32) == 0)))
6386     {
6387       gold_error(_("TLS variable referred by reloc %u is too far from TP."),
6388                  r_type);
6389       return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6390     }
6391
6392   Insntype* ip = reinterpret_cast<Insntype*>(view);
6393   Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
6394   unsigned int regno;
6395   Insntype newinsn;
6396   if (r_type == elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21)
6397     {
6398       // Generate movz.
6399       regno = (insn & 0x1f);
6400       newinsn = (0xd2a00000 | regno) | (((x >> 16) & 0xffff) << 5);
6401     }
6402   else if (r_type == elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
6403     {
6404       // Generate movk.
6405       regno = (insn & 0x1f);
6406       gold_assert(regno == ((insn >> 5) & 0x1f));
6407       newinsn = (0xf2800000 | regno) | ((x & 0xffff) << 5);
6408     }
6409   else
6410     gold_unreachable();
6411
6412   elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
6413   return aarch64_reloc_funcs::STATUS_OKAY;
6414 }  // End of tls_ie_to_le
6415
6416
6417 template<int size, bool big_endian>
6418 inline
6419 typename AArch64_relocate_functions<size, big_endian>::Status
6420 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_le(
6421              const Relocate_info<size, big_endian>* relinfo,
6422              Target_aarch64<size, big_endian>* target,
6423              const elfcpp::Rela<size, big_endian>& rela,
6424              unsigned int r_type,
6425              unsigned char* view,
6426              const Symbol_value<size>* psymval)
6427 {
6428   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6429   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6430   typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6431
6432   // TLSDESC-GD sequence is like:
6433   //   adrp  x0, :tlsdesc:v1
6434   //   ldr   x1, [x0, #:tlsdesc_lo12:v1]
6435   //   add   x0, x0, :tlsdesc_lo12:v1
6436   //   .tlsdesccall    v1
6437   //   blr   x1
6438   // After desc_gd_to_le optimization, the sequence will be like:
6439   //   movz  x0, #0x0, lsl #16
6440   //   movk  x0, #0x10
6441   //   nop
6442   //   nop
6443
6444   // Calculate tprel value.
6445   Output_segment* tls_segment = relinfo->layout->tls_segment();
6446   gold_assert(tls_segment != NULL);
6447   Insntype* ip = reinterpret_cast<Insntype*>(view);
6448   const elfcpp::Elf_Xword addend = rela.get_r_addend();
6449   AArch64_address value = psymval->value(relinfo->object, addend);
6450   AArch64_address aligned_tcb_size =
6451       align_address(target->tcb_size(), tls_segment->maximum_alignment());
6452   AArch64_address x = value + aligned_tcb_size;
6453   // x is the offset to tp, we can only do this if x is within range
6454   // [0, 2^32-1]. If x is out of range, fail and exit.
6455   if (size == 64 && (static_cast<uint64_t>(x) >> 32) != 0)
6456     {
6457       gold_error(_("TLS variable referred by reloc %u is too far from TP. "
6458                    "We Can't do gd_to_le relaxation.\n"), r_type);
6459       return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6460     }
6461   Insntype newinsn;
6462   switch (r_type)
6463     {
6464     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6465     case elfcpp::R_AARCH64_TLSDESC_CALL:
6466       // Change to nop
6467       newinsn = 0xd503201f;
6468       break;
6469
6470     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6471       // Change to movz.
6472       newinsn = 0xd2a00000 | (((x >> 16) & 0xffff) << 5);
6473       break;
6474
6475     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6476       // Change to movk.
6477       newinsn = 0xf2800000 | ((x & 0xffff) << 5);
6478       break;
6479
6480     default:
6481       gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
6482                  r_type);
6483       gold_unreachable();
6484     }
6485   elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
6486   return aarch64_reloc_funcs::STATUS_OKAY;
6487 }  // End of tls_desc_gd_to_le
6488
6489
6490 template<int size, bool big_endian>
6491 inline
6492 typename AArch64_relocate_functions<size, big_endian>::Status
6493 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_ie(
6494              const Relocate_info<size, big_endian>* /* relinfo */,
6495              Target_aarch64<size, big_endian>* /* target */,
6496              const elfcpp::Rela<size, big_endian>& rela,
6497              unsigned int r_type,
6498              unsigned char* view,
6499              const Symbol_value<size>* /* psymval */,
6500              typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address,
6501              typename elfcpp::Elf_types<size>::Elf_Addr address)
6502 {
6503   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6504   typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6505
6506   // TLSDESC-GD sequence is like:
6507   //   adrp  x0, :tlsdesc:v1
6508   //   ldr   x1, [x0, #:tlsdesc_lo12:v1]
6509   //   add   x0, x0, :tlsdesc_lo12:v1
6510   //   .tlsdesccall    v1
6511   //   blr   x1
6512   // After desc_gd_to_ie optimization, the sequence will be like:
6513   //   adrp  x0, :tlsie:v1
6514   //   ldr   x0, [x0, :tlsie_lo12:v1]
6515   //   nop
6516   //   nop
6517
6518   Insntype* ip = reinterpret_cast<Insntype*>(view);
6519   const elfcpp::Elf_Xword addend = rela.get_r_addend();
6520   Insntype newinsn;
6521   switch (r_type)
6522     {
6523     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6524     case elfcpp::R_AARCH64_TLSDESC_CALL:
6525       // Change to nop
6526       newinsn = 0xd503201f;
6527       elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
6528       break;
6529
6530     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6531       {
6532         return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
6533                                          address);
6534       }
6535       break;
6536
6537     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6538       {
6539        // Set ldr target register to be x0.
6540        Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
6541        insn &= 0xffffffe0;
6542        elfcpp::Swap<32, big_endian>::writeval(ip, insn);
6543        // Do relocation.
6544         const AArch64_reloc_property* reloc_property =
6545             aarch64_reloc_property_table->get_reloc_property(
6546               elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
6547         return aarch64_reloc_funcs::template rela_general<32>(
6548                  view, got_entry_address, addend, reloc_property);
6549       }
6550       break;
6551
6552     default:
6553       gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
6554                  r_type);
6555       gold_unreachable();
6556     }
6557   return aarch64_reloc_funcs::STATUS_OKAY;
6558 }  // End of tls_desc_gd_to_ie
6559
6560 // Relocate section data.
6561
6562 template<int size, bool big_endian>
6563 void
6564 Target_aarch64<size, big_endian>::relocate_section(
6565     const Relocate_info<size, big_endian>* relinfo,
6566     unsigned int sh_type,
6567     const unsigned char* prelocs,
6568     size_t reloc_count,
6569     Output_section* output_section,
6570     bool needs_special_offset_handling,
6571     unsigned char* view,
6572     typename elfcpp::Elf_types<size>::Elf_Addr address,
6573     section_size_type view_size,
6574     const Reloc_symbol_changes* reloc_symbol_changes)
6575 {
6576   gold_assert(sh_type == elfcpp::SHT_RELA);
6577   typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
6578   gold::relocate_section<size, big_endian, Target_aarch64, elfcpp::SHT_RELA,
6579                          AArch64_relocate, gold::Default_comdat_behavior>(
6580     relinfo,
6581     this,
6582     prelocs,
6583     reloc_count,
6584     output_section,
6585     needs_special_offset_handling,
6586     view,
6587     address,
6588     view_size,
6589     reloc_symbol_changes);
6590 }
6591
6592 // Return the size of a relocation while scanning during a relocatable
6593 // link.
6594
6595 template<int size, bool big_endian>
6596 unsigned int
6597 Target_aarch64<size, big_endian>::Relocatable_size_for_reloc::
6598 get_size_for_reloc(
6599     unsigned int ,
6600     Relobj* )
6601 {
6602   // We will never support SHT_REL relocations.
6603   gold_unreachable();
6604   return 0;
6605 }
6606
6607 // Scan the relocs during a relocatable link.
6608
6609 template<int size, bool big_endian>
6610 void
6611 Target_aarch64<size, big_endian>::scan_relocatable_relocs(
6612     Symbol_table* symtab,
6613     Layout* layout,
6614     Sized_relobj_file<size, big_endian>* object,
6615     unsigned int data_shndx,
6616     unsigned int sh_type,
6617     const unsigned char* prelocs,
6618     size_t reloc_count,
6619     Output_section* output_section,
6620     bool needs_special_offset_handling,
6621     size_t local_symbol_count,
6622     const unsigned char* plocal_symbols,
6623     Relocatable_relocs* rr)
6624 {
6625   gold_assert(sh_type == elfcpp::SHT_RELA);
6626
6627   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
6628     Relocatable_size_for_reloc> Scan_relocatable_relocs;
6629
6630   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
6631       Scan_relocatable_relocs>(
6632     symtab,
6633     layout,
6634     object,
6635     data_shndx,
6636     prelocs,
6637     reloc_count,
6638     output_section,
6639     needs_special_offset_handling,
6640     local_symbol_count,
6641     plocal_symbols,
6642     rr);
6643 }
6644
6645 // Relocate a section during a relocatable link.
6646
6647 template<int size, bool big_endian>
6648 void
6649 Target_aarch64<size, big_endian>::relocate_relocs(
6650     const Relocate_info<size, big_endian>* relinfo,
6651     unsigned int sh_type,
6652     const unsigned char* prelocs,
6653     size_t reloc_count,
6654     Output_section* output_section,
6655     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
6656     const Relocatable_relocs* rr,
6657     unsigned char* view,
6658     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
6659     section_size_type view_size,
6660     unsigned char* reloc_view,
6661     section_size_type reloc_view_size)
6662 {
6663   gold_assert(sh_type == elfcpp::SHT_RELA);
6664
6665   gold::relocate_relocs<size, big_endian, elfcpp::SHT_RELA>(
6666     relinfo,
6667     prelocs,
6668     reloc_count,
6669     output_section,
6670     offset_in_output_section,
6671     rr,
6672     view,
6673     view_address,
6674     view_size,
6675     reloc_view,
6676     reloc_view_size);
6677 }
6678
6679
6680 // The selector for aarch64 object files.
6681
6682 template<int size, bool big_endian>
6683 class Target_selector_aarch64 : public Target_selector
6684 {
6685  public:
6686   Target_selector_aarch64();
6687
6688   virtual Target*
6689   do_instantiate_target()
6690   { return new Target_aarch64<size, big_endian>(); }
6691 };
6692
6693 template<>
6694 Target_selector_aarch64<32, true>::Target_selector_aarch64()
6695   : Target_selector(elfcpp::EM_AARCH64, 32, true,
6696                     "elf32-bigaarch64", "aarch64_elf32_be_vec")
6697 { }
6698
6699 template<>
6700 Target_selector_aarch64<32, false>::Target_selector_aarch64()
6701   : Target_selector(elfcpp::EM_AARCH64, 32, false,
6702                     "elf32-littleaarch64", "aarch64_elf32_le_vec")
6703 { }
6704
6705 template<>
6706 Target_selector_aarch64<64, true>::Target_selector_aarch64()
6707   : Target_selector(elfcpp::EM_AARCH64, 64, true,
6708                     "elf64-bigaarch64", "aarch64_elf64_be_vec")
6709 { }
6710
6711 template<>
6712 Target_selector_aarch64<64, false>::Target_selector_aarch64()
6713   : Target_selector(elfcpp::EM_AARCH64, 64, false,
6714                     "elf64-littleaarch64", "aarch64_elf64_le_vec")
6715 { }
6716
6717 Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
6718 Target_selector_aarch64<32, false> target_selector_aarch64elf32;
6719 Target_selector_aarch64<64, true> target_selector_aarch64elfb;
6720 Target_selector_aarch64<64, false> target_selector_aarch64elf;
6721
6722 } // End anonymous namespace.