The 3rd patch for aarch64 gold.
[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 namespace
51 {
52
53 using namespace gold;
54
55 template<int size, bool big_endian>
56 class Output_data_plt_aarch64;
57
58 template<int size, bool big_endian>
59 class Output_data_plt_aarch64_standard;
60
61 template<int size, bool big_endian>
62 class Target_aarch64;
63
64 template<int size, bool big_endian>
65 class AArch64_relocate_functions;
66
67 // Output_data_got_aarch64 class.
68
69 template<int size, bool big_endian>
70 class Output_data_got_aarch64 : public Output_data_got<size, big_endian>
71 {
72  public:
73   typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
74   Output_data_got_aarch64(Symbol_table* symtab, Layout* layout)
75     : Output_data_got<size, big_endian>(),
76       symbol_table_(symtab), layout_(layout)
77   { }
78
79   // Add a static entry for the GOT entry at OFFSET.  GSYM is a global
80   // symbol and R_TYPE is the code of a dynamic relocation that needs to be
81   // applied in a static link.
82   void
83   add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
84   { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
85
86
87   // Add a static reloc for the GOT entry at OFFSET.  RELOBJ is an object
88   // defining a local symbol with INDEX.  R_TYPE is the code of a dynamic
89   // relocation that needs to be applied in a static link.
90   void
91   add_static_reloc(unsigned int got_offset, unsigned int r_type,
92                    Sized_relobj_file<size, big_endian>* relobj,
93                    unsigned int index)
94   {
95     this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
96                                                 index));
97   }
98
99
100  protected:
101   // Write out the GOT table.
102   void
103   do_write(Output_file* of) {
104     // The first entry in the GOT is the address of the .dynamic section.
105     gold_assert(this->data_size() >= size / 8);
106     Output_section* dynamic = this->layout_->dynamic_section();
107     Valtype dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
108     this->replace_constant(0, dynamic_addr);
109     Output_data_got<size, big_endian>::do_write(of);
110
111     // Handling static relocs
112     if (this->static_relocs_.empty())
113       return;
114
115     typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
116
117     gold_assert(parameters->doing_static_link());
118     const off_t offset = this->offset();
119     const section_size_type oview_size =
120       convert_to_section_size_type(this->data_size());
121     unsigned char* const oview = of->get_output_view(offset, oview_size);
122
123     Output_segment* tls_segment = this->layout_->tls_segment();
124     gold_assert(tls_segment != NULL);
125
126     AArch64_address aligned_tcb_address =
127       align_address(Target_aarch64<size,big_endian>::TCB_SIZE,
128                     tls_segment->maximum_alignment());
129
130     for (size_t i = 0; i < this->static_relocs_.size(); ++i)
131       {
132         Static_reloc& reloc(this->static_relocs_[i]);
133         AArch64_address value;
134
135         if (!reloc.symbol_is_global())
136           {
137             Sized_relobj_file<size, big_endian>* object = reloc.relobj();
138             const Symbol_value<size>* psymval =
139               reloc.relobj()->local_symbol(reloc.index());
140
141             // We are doing static linking.  Issue an error and skip this
142             // relocation if the symbol is undefined or in a discarded_section.
143             bool is_ordinary;
144             unsigned int shndx = psymval->input_shndx(&is_ordinary);
145             if ((shndx == elfcpp::SHN_UNDEF)
146                 || (is_ordinary
147                     && shndx != elfcpp::SHN_UNDEF
148                     && !object->is_section_included(shndx)
149                     && !this->symbol_table_->is_section_folded(object, shndx)))
150               {
151                 gold_error(_("undefined or discarded local symbol %u from "
152                              " object %s in GOT"),
153                            reloc.index(), reloc.relobj()->name().c_str());
154                 continue;
155               }
156             value = psymval->value(object, 0);
157           }
158         else
159           {
160             const Symbol* gsym = reloc.symbol();
161             gold_assert(gsym != NULL);
162             if (gsym->is_forwarder())
163               gsym = this->symbol_table_->resolve_forwards(gsym);
164
165             // We are doing static linking.  Issue an error and skip this
166             // relocation if the symbol is undefined or in a discarded_section
167             // unless it is a weakly_undefined symbol.
168             if ((gsym->is_defined_in_discarded_section()
169                  || gsym->is_undefined())
170                 && !gsym->is_weak_undefined())
171               {
172                 gold_error(_("undefined or discarded symbol %s in GOT"),
173                            gsym->name());
174                 continue;
175               }
176
177             if (!gsym->is_weak_undefined())
178               {
179                 const Sized_symbol<size>* sym =
180                   static_cast<const Sized_symbol<size>*>(gsym);
181                 value = sym->value();
182               }
183             else
184               value = 0;
185           }
186
187         unsigned got_offset = reloc.got_offset();
188         gold_assert(got_offset < oview_size);
189
190         typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
191         Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
192         Valtype x;
193         switch (reloc.r_type())
194           {
195           case elfcpp::R_AARCH64_TLS_DTPREL64:
196             x = value;
197             break;
198           case elfcpp::R_AARCH64_TLS_TPREL64:
199             x = value + aligned_tcb_address;
200             break;
201           default:
202             gold_unreachable();
203           }
204         elfcpp::Swap<size, big_endian>::writeval(wv, x);
205       }
206
207     of->write_output_view(offset, oview_size, oview);
208   }
209
210  private:
211   // Symbol table of the output object.
212   Symbol_table* symbol_table_;
213   // A pointer to the Layout class, so that we can find the .dynamic
214   // section when we write out the GOT section.
215   Layout* layout_;
216
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 object.
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 };
301
302 AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
303
304 // The aarch64 target class.
305 // See the ABI at
306 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
307 template<int size, bool big_endian>
308 class Target_aarch64 : public Sized_target<size, big_endian>
309 {
310  public:
311   typedef Target_aarch64<size,big_endian> This;
312   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
313       Reloc_section;
314   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
315   const static int TCB_SIZE = size / 8 * 2;
316
317   Target_aarch64(const Target::Target_info* info = &aarch64_info)
318     : Sized_target<size, big_endian>(info),
319       got_(NULL), plt_(NULL), got_plt_(NULL),
320       global_offset_table_(NULL), rela_dyn_(NULL),
321       copy_relocs_(elfcpp::R_AARCH64_COPY)
322   { }
323
324   // Scan the relocations to determine unreferenced sections for
325   // garbage collection.
326   void
327   gc_process_relocs(Symbol_table* symtab,
328                     Layout* layout,
329                     Sized_relobj_file<size, big_endian>* object,
330                     unsigned int data_shndx,
331                     unsigned int sh_type,
332                     const unsigned char* prelocs,
333                     size_t reloc_count,
334                     Output_section* output_section,
335                     bool needs_special_offset_handling,
336                     size_t local_symbol_count,
337                     const unsigned char* plocal_symbols);
338
339   // Scan the relocations to look for symbol adjustments.
340   void
341   scan_relocs(Symbol_table* symtab,
342               Layout* layout,
343               Sized_relobj_file<size, big_endian>* object,
344               unsigned int data_shndx,
345               unsigned int sh_type,
346               const unsigned char* prelocs,
347               size_t reloc_count,
348               Output_section* output_section,
349               bool needs_special_offset_handling,
350               size_t local_symbol_count,
351               const unsigned char* plocal_symbols);
352
353   // Finalize the sections.
354   void
355   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
356
357   // Relocate a section.
358   void
359   relocate_section(const Relocate_info<size, big_endian>*,
360                    unsigned int sh_type,
361                    const unsigned char* prelocs,
362                    size_t reloc_count,
363                    Output_section* output_section,
364                    bool needs_special_offset_handling,
365                    unsigned char* view,
366                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
367                    section_size_type view_size,
368                    const Reloc_symbol_changes*);
369
370   // Scan the relocs during a relocatable link.
371   void
372   scan_relocatable_relocs(Symbol_table* symtab,
373                           Layout* layout,
374                           Sized_relobj_file<size, big_endian>* object,
375                           unsigned int data_shndx,
376                           unsigned int sh_type,
377                           const unsigned char* prelocs,
378                           size_t reloc_count,
379                           Output_section* output_section,
380                           bool needs_special_offset_handling,
381                           size_t local_symbol_count,
382                           const unsigned char* plocal_symbols,
383                           Relocatable_relocs*);
384
385   // Relocate a section during a relocatable link.
386   void
387   relocate_relocs(
388       const Relocate_info<size, big_endian>*,
389       unsigned int sh_type,
390       const unsigned char* prelocs,
391       size_t reloc_count,
392       Output_section* output_section,
393       typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
394       const Relocatable_relocs*,
395       unsigned char* view,
396       typename elfcpp::Elf_types<size>::Elf_Addr view_address,
397       section_size_type view_size,
398       unsigned char* reloc_view,
399       section_size_type reloc_view_size);
400
401   // Return the PLT section.
402   uint64_t
403   do_plt_address_for_global(const Symbol* gsym) const
404   { return this->plt_section()->address_for_global(gsym); }
405
406   uint64_t
407   do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
408   { return this->plt_section()->address_for_local(relobj, symndx); }
409
410   // Return the number of entries in the PLT.
411   unsigned int
412   plt_entry_count() const;
413
414   //Return the offset of the first non-reserved PLT entry.
415   unsigned int
416   first_plt_entry_offset() const;
417
418   // Return the size of each PLT entry.
419   unsigned int
420   plt_entry_size() const;
421
422   unsigned int
423   tcb_size() const { return This::TCB_SIZE; }
424
425  protected:
426   void
427   do_select_as_default_target()
428   {
429     gold_assert(aarch64_reloc_property_table == NULL);
430     aarch64_reloc_property_table = new AArch64_reloc_property_table();
431   }
432
433   virtual Output_data_plt_aarch64<size, big_endian>*
434   do_make_data_plt(Layout* layout, Output_data_space* got_plt)
435   {
436     return new Output_data_plt_aarch64_standard<size, big_endian>(layout,
437                                                                   got_plt);
438   }
439
440   Output_data_plt_aarch64<size, big_endian>*
441   make_data_plt(Layout* layout, Output_data_space* got_plt)
442   {
443     return this->do_make_data_plt(layout, got_plt);
444   }
445
446  private:
447   // The class which scans relocations.
448   class Scan
449   {
450   public:
451     Scan()
452       : issued_non_pic_error_(false)
453     { }
454
455     inline void
456     local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
457           Sized_relobj_file<size, big_endian>* object,
458           unsigned int data_shndx,
459           Output_section* output_section,
460           const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
461           const elfcpp::Sym<size, big_endian>& lsym,
462           bool is_discarded);
463
464     inline void
465     global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
466            Sized_relobj_file<size, big_endian>* object,
467            unsigned int data_shndx,
468            Output_section* output_section,
469            const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
470            Symbol* gsym);
471
472     inline bool
473     local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
474                                         Target_aarch64<size, big_endian>* ,
475                                         Sized_relobj_file<size, big_endian>* ,
476                                         unsigned int ,
477                                         Output_section* ,
478                                         const elfcpp::Rela<size, big_endian>& ,
479                                         unsigned int r_type,
480                                         const elfcpp::Sym<size, big_endian>&);
481
482     inline bool
483     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
484                                          Target_aarch64<size, big_endian>* ,
485                                          Sized_relobj_file<size, big_endian>* ,
486                                          unsigned int ,
487                                          Output_section* ,
488                                          const elfcpp::Rela<size, big_endian>& ,
489                                          unsigned int r_type,
490                                          Symbol* gsym);
491
492   private:
493     static void
494     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
495                             unsigned int r_type);
496
497     static void
498     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
499                              unsigned int r_type, Symbol*);
500
501     inline bool
502     possible_function_pointer_reloc(unsigned int r_type);
503
504     void
505     check_non_pic(Relobj*, unsigned int r_type);
506
507     // Whether we have issued an error about a non-PIC compilation.
508     bool issued_non_pic_error_;
509   };
510
511   // The class which implements relocation.
512   class Relocate
513   {
514    public:
515     Relocate()
516     { }
517
518     ~Relocate()
519     { }
520
521     // Do a relocation.  Return false if the caller should not issue
522     // any warnings about this relocation.
523     inline bool
524     relocate(const Relocate_info<size, big_endian>*, Target_aarch64*,
525              Output_section*,
526              size_t relnum, const elfcpp::Rela<size, big_endian>&,
527              unsigned int r_type, const Sized_symbol<size>*,
528              const Symbol_value<size>*,
529              unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
530              section_size_type);
531
532   private:
533     inline typename AArch64_relocate_functions<size,big_endian>::Status
534     relocate_tls(const Relocate_info<size,big_endian>*,
535                  Target_aarch64<size, big_endian>*,
536                  size_t,
537                  const elfcpp::Rela<size, big_endian>&,
538                  unsigned int r_type, const Sized_symbol<size>*,
539                  const Symbol_value<size>*,
540                  unsigned char*,
541                  typename elfcpp::Elf_types<size>::Elf_Addr);
542
543   };  // End of class Relocate
544
545   // A class which returns the size required for a relocation type,
546   // used while scanning relocs during a relocatable link.
547   class Relocatable_size_for_reloc
548   {
549    public:
550     unsigned int
551     get_size_for_reloc(unsigned int, Relobj*);
552   };
553
554   // Adjust TLS relocation type based on the options and whether this
555   // is a local symbol.
556   static tls::Tls_optimization
557   optimize_tls_reloc(bool is_final, int r_type);
558
559   // Get the GOT section, creating it if necessary.
560   Output_data_got_aarch64<size, big_endian>*
561   got_section(Symbol_table*, Layout*);
562
563   // Get the GOT PLT section.
564   Output_data_space*
565   got_plt_section() const
566   {
567     gold_assert(this->got_plt_ != NULL);
568     return this->got_plt_;
569   }
570
571   // Create the PLT section.
572   void
573   make_plt_section(Symbol_table* symtab, Layout* layout);
574
575   // Create a PLT entry for a global symbol.
576   void
577   make_plt_entry(Symbol_table*, Layout*, Symbol*);
578
579   // Get the PLT section.
580   Output_data_plt_aarch64<size, big_endian>*
581   plt_section() const
582   {
583     gold_assert(this->plt_ != NULL);
584     return this->plt_;
585   }
586
587   // Get the dynamic reloc section, creating it if necessary.
588   Reloc_section*
589   rela_dyn_section(Layout*);
590
591   // Add a potential copy relocation.
592   void
593   copy_reloc(Symbol_table* symtab, Layout* layout,
594              Sized_relobj_file<size, big_endian>* object,
595              unsigned int shndx, Output_section* output_section,
596              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
597   {
598     this->copy_relocs_.copy_reloc(symtab, layout,
599                                   symtab->get_sized_symbol<size>(sym),
600                                   object, shndx, output_section,
601                                   reloc, this->rela_dyn_section(layout));
602   }
603
604   // Information about this specific target which we pass to the
605   // general Target structure.
606   static const Target::Target_info aarch64_info;
607
608   // The types of GOT entries needed for this platform.
609   // These values are exposed to the ABI in an incremental link.
610   // Do not renumber existing values without changing the version
611   // number of the .gnu_incremental_inputs section.
612   enum Got_type
613   {
614     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
615     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
616     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
617     GOT_TYPE_TLS_DESC = 3       // GOT entry for TLS_DESC pair
618   };
619
620   // The GOT section.
621   Output_data_got_aarch64<size, big_endian>* got_;
622   // The PLT section.
623   Output_data_plt_aarch64<size, big_endian>* plt_;
624   // The GOT PLT section.
625   Output_data_space* got_plt_;
626   // The _GLOBAL_OFFSET_TABLE_ symbol.
627   Symbol* global_offset_table_;
628   // The dynamic reloc section.
629   Reloc_section* rela_dyn_;
630   // Relocs saved to avoid a COPY reloc.
631   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
632 };  // End of Target_aarch64
633
634 template<>
635 const Target::Target_info Target_aarch64<64, false>::aarch64_info =
636 {
637   64,                   // size
638   false,                // is_big_endian
639   elfcpp::EM_AARCH64,   // machine_code
640   false,                // has_make_symbol
641   false,                // has_resolve
642   false,                // has_code_fill
643   true,                 // is_default_stack_executable
644   false,                // can_icf_inline_merge_sections
645   '\0',                 // wrap_char
646   "/lib/ld.so.1",       // program interpreter
647   0x400000,             // default_text_segment_address
648   0x1000,               // abi_pagesize (overridable by -z max-page-size)
649   0x1000,               // common_pagesize (overridable by -z common-page-size)
650   false,                // isolate_execinstr
651   0,                    // rosegment_gap
652   elfcpp::SHN_UNDEF,    // small_common_shndx
653   elfcpp::SHN_UNDEF,    // large_common_shndx
654   0,                    // small_common_section_flags
655   0,                    // large_common_section_flags
656   NULL,                 // attributes_section
657   NULL,                 // attributes_vendor
658   "_start"              // entry_symbol_name
659 };
660
661 template<>
662 const Target::Target_info Target_aarch64<32, false>::aarch64_info =
663 {
664   32,                   // size
665   false,                // is_big_endian
666   elfcpp::EM_AARCH64,   // machine_code
667   false,                // has_make_symbol
668   false,                // has_resolve
669   false,                // has_code_fill
670   true,                 // is_default_stack_executable
671   false,                // can_icf_inline_merge_sections
672   '\0',                 // wrap_char
673   "/lib/ld.so.1",       // program interpreter
674   0x400000,             // default_text_segment_address
675   0x1000,               // abi_pagesize (overridable by -z max-page-size)
676   0x1000,               // common_pagesize (overridable by -z common-page-size)
677   false,                // isolate_execinstr
678   0,                    // rosegment_gap
679   elfcpp::SHN_UNDEF,    // small_common_shndx
680   elfcpp::SHN_UNDEF,    // large_common_shndx
681   0,                    // small_common_section_flags
682   0,                    // large_common_section_flags
683   NULL,                 // attributes_section
684   NULL,                 // attributes_vendor
685   "_start"              // entry_symbol_name
686 };
687
688 template<>
689 const Target::Target_info Target_aarch64<64, true>::aarch64_info =
690 {
691   64,                   // size
692   true,                 // is_big_endian
693   elfcpp::EM_AARCH64,   // machine_code
694   false,                // has_make_symbol
695   false,                // has_resolve
696   false,                // has_code_fill
697   true,                 // is_default_stack_executable
698   false,                // can_icf_inline_merge_sections
699   '\0',                 // wrap_char
700   "/lib/ld.so.1",       // program interpreter
701   0x400000,             // default_text_segment_address
702   0x1000,               // abi_pagesize (overridable by -z max-page-size)
703   0x1000,               // common_pagesize (overridable by -z common-page-size)
704   false,                // isolate_execinstr
705   0,                    // rosegment_gap
706   elfcpp::SHN_UNDEF,    // small_common_shndx
707   elfcpp::SHN_UNDEF,    // large_common_shndx
708   0,                    // small_common_section_flags
709   0,                    // large_common_section_flags
710   NULL,                 // attributes_section
711   NULL,                 // attributes_vendor
712   "_start"              // entry_symbol_name
713 };
714
715 template<>
716 const Target::Target_info Target_aarch64<32, true>::aarch64_info =
717 {
718   32,                   // size
719   true,                 // is_big_endian
720   elfcpp::EM_AARCH64,   // machine_code
721   false,                // has_make_symbol
722   false,                // has_resolve
723   false,                // has_code_fill
724   true,                 // is_default_stack_executable
725   false,                // can_icf_inline_merge_sections
726   '\0',                 // wrap_char
727   "/lib/ld.so.1",       // program interpreter
728   0x400000,             // default_text_segment_address
729   0x1000,               // abi_pagesize (overridable by -z max-page-size)
730   0x1000,               // common_pagesize (overridable by -z common-page-size)
731   false,                // isolate_execinstr
732   0,                    // rosegment_gap
733   elfcpp::SHN_UNDEF,    // small_common_shndx
734   elfcpp::SHN_UNDEF,    // large_common_shndx
735   0,                    // small_common_section_flags
736   0,                    // large_common_section_flags
737   NULL,                 // attributes_section
738   NULL,                 // attributes_vendor
739   "_start"              // entry_symbol_name
740 };
741
742 // Get the GOT section, creating it if necessary.
743
744 template<int size, bool big_endian>
745 Output_data_got_aarch64<size, big_endian>*
746 Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
747                                               Layout* layout)
748 {
749   if (this->got_ == NULL)
750     {
751       gold_assert(symtab != NULL && layout != NULL);
752
753       // When using -z now, we can treat .got.plt as a relro section.
754       // Without -z now, it is modified after program startup by lazy
755       // PLT relocations.
756       bool is_got_plt_relro = parameters->options().now();
757       Output_section_order got_order = (is_got_plt_relro
758                                         ? ORDER_RELRO
759                                         : ORDER_RELRO_LAST);
760       Output_section_order got_plt_order = (is_got_plt_relro
761                                             ? ORDER_RELRO
762                                             : ORDER_NON_RELRO_FIRST);
763
764       // Layout of .got and .got.plt sections.
765       // .got[0] &_DYNAMIC                          <-_GLOBAL_OFFSET_TABLE_
766       // ...
767       // .gotplt[0] reserved for ld.so (&linkmap)   <--DT_PLTGOT
768       // .gotplt[1] reserved for ld.so (resolver)
769       // .gotplt[2] reserved
770
771       // Generate .got section.
772       this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
773                                                                  layout);
774       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
775                                       (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
776                                       this->got_, got_order, true);
777       // The first word of GOT is reserved for the address of .dynamic.
778       // We put 0 here now. The value will be replaced later in
779       // Output_data_got_aarch64::do_write.
780       this->got_->add_constant(0);
781
782       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
783       // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
784       // even if there is a .got.plt section.
785       this->global_offset_table_ =
786         symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
787                                       Symbol_table::PREDEFINED,
788                                       this->got_,
789                                       0, 0, elfcpp::STT_OBJECT,
790                                       elfcpp::STB_LOCAL,
791                                       elfcpp::STV_HIDDEN, 0,
792                                       false, false);
793
794       // Generate .got.plt section.
795       this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
796       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
797                                       (elfcpp::SHF_ALLOC
798                                        | elfcpp::SHF_WRITE),
799                                       this->got_plt_, got_plt_order,
800                                       is_got_plt_relro);
801
802       // The first three entries are reserved.
803       this->got_plt_->set_current_data_size(
804         AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
805
806       if (!is_got_plt_relro)
807         {
808           // Those bytes can go into the relro segment.
809           layout->increase_relro(
810             AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
811         }
812
813     }
814   return this->got_;
815 }
816
817 // Get the dynamic reloc section, creating it if necessary.
818
819 template<int size, bool big_endian>
820 typename Target_aarch64<size, big_endian>::Reloc_section*
821 Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
822 {
823   if (this->rela_dyn_ == NULL)
824     {
825       gold_assert(layout != NULL);
826       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
827       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
828                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
829                                       ORDER_DYNAMIC_RELOCS, false);
830     }
831   return this->rela_dyn_;
832 }
833
834 // A class to handle the PLT data.
835 // This is an abstract base class that handles most of the linker details
836 // but does not know the actual contents of PLT entries.  The derived
837 // classes below fill in those details.
838
839 template<int size, bool big_endian>
840 class Output_data_plt_aarch64 : public Output_section_data
841 {
842  public:
843   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
844       Reloc_section;
845   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
846
847   Output_data_plt_aarch64(Layout* layout,
848                           uint64_t addralign,
849                           Output_data_space* got_plt)
850     : Output_section_data(addralign),
851       got_plt_(got_plt),
852       count_(0)
853   { this->init(layout); }
854
855   // Initialize the PLT section.
856   void
857   init(Layout* layout);
858
859   // Add an entry to the PLT.
860   void
861   add_entry(Symbol* gsym);
862
863   // Return the .rela.plt section data.
864   Reloc_section*
865   rela_plt()
866   { return this->rel_; }
867
868   // Return whether we created a section for IRELATIVE relocations.
869   bool
870   has_irelative_section() const
871   { return this->irelative_rel_ != NULL; }
872
873   // Return the number of PLT entries.
874   unsigned int
875   entry_count() const
876   { return this->count_; }
877
878   // Return the offset of the first non-reserved PLT entry.
879   unsigned int
880   first_plt_entry_offset()
881   { return this->do_first_plt_entry_offset(); }
882
883   // Return the size of a PLT entry.
884   unsigned int
885   get_plt_entry_size() const
886   { return this->do_get_plt_entry_size(); }
887
888   // Return the PLT address to use for a global symbol.
889   uint64_t
890   address_for_global(const Symbol*);
891
892   // Return the PLT address to use for a local symbol.
893   uint64_t
894   address_for_local(const Relobj*, unsigned int symndx);
895
896  protected:
897   // Fill in the first PLT entry.
898   void
899   fill_first_plt_entry(unsigned char* pov,
900                        Address got_address,
901                        Address plt_address)
902   { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
903
904   // Fill in a normal PLT entry.
905   void
906   fill_plt_entry(unsigned char* pov,
907                  Address got_address,
908                  Address plt_address,
909                  unsigned int got_offset,
910                  unsigned int plt_offset)
911   {
912     this->do_fill_plt_entry(pov, got_address, plt_address,
913                             got_offset, plt_offset);
914   }
915
916   virtual unsigned int
917   do_first_plt_entry_offset() const = 0;
918
919   virtual unsigned int
920   do_get_plt_entry_size() const = 0;
921
922   virtual void
923   do_fill_first_plt_entry(unsigned char* pov,
924                           Address got_addr,
925                           Address plt_addr) = 0;
926
927   virtual void
928   do_fill_plt_entry(unsigned char* pov,
929                     Address got_address,
930                     Address plt_address,
931                     unsigned int got_offset,
932                     unsigned int plt_offset) = 0;
933
934   void
935   do_adjust_output_section(Output_section* os);
936
937   // Write to a map file.
938   void
939   do_print_to_mapfile(Mapfile* mapfile) const
940   { mapfile->print_output_data(this, _("** PLT")); }
941
942  private:
943   // Set the final size.
944   void
945   set_final_data_size();
946
947   // Write out the PLT data.
948   void
949   do_write(Output_file*);
950
951   // The reloc section.
952   Reloc_section* rel_;
953   // The IRELATIVE relocs, if necessary.  These must follow the
954   // regular PLT relocations.
955   Reloc_section* irelative_rel_;
956   // The .got section.
957   Output_data_got_aarch64<size, big_endian>* got_;
958   // The .got.plt section.
959   Output_data_space* got_plt_;
960   // The number of PLT entries.
961   unsigned int count_;
962 };
963
964 // Initialize the PLT section.
965
966 template<int size, bool big_endian>
967 void
968 Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
969 {
970   this->rel_ = new Reloc_section(false);
971   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
972                                   elfcpp::SHF_ALLOC, this->rel_,
973                                   ORDER_DYNAMIC_PLT_RELOCS, false);
974 }
975
976 template<int size, bool big_endian>
977 void
978 Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
979     Output_section* os)
980 {
981   os->set_entsize(this->get_plt_entry_size());
982 }
983
984 // Add an entry to the PLT.
985
986 template<int size, bool big_endian>
987 void
988 Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol* gsym)
989 {
990   gold_assert(!gsym->has_plt_offset());
991
992   gsym->set_plt_offset((this->count_) * this->get_plt_entry_size()
993                        + this->first_plt_entry_offset());
994
995   ++this->count_;
996
997   section_offset_type got_offset = this->got_plt_->current_data_size();
998
999   // Every PLT entry needs a GOT entry which points back to the PLT
1000   // entry (this will be changed by the dynamic linker, normally
1001   // lazily when the function is called).
1002   this->got_plt_->set_current_data_size(got_offset + size / 8);
1003
1004   // Every PLT entry needs a reloc.
1005   gsym->set_needs_dynsym_entry();
1006   this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT,
1007                          this->got_plt_, got_offset, 0);
1008
1009   // Note that we don't need to save the symbol. The contents of the
1010   // PLT are independent of which symbols are used. The symbols only
1011   // appear in the relocations.
1012 }
1013
1014 // Return the PLT address to use for a global symbol.
1015
1016 template<int size, bool big_endian>
1017 uint64_t
1018 Output_data_plt_aarch64<size, big_endian>::address_for_global(
1019   const Symbol* gsym)
1020 {
1021   uint64_t offset = 0;
1022   if (gsym->type() == elfcpp::STT_GNU_IFUNC
1023       && gsym->can_use_relative_reloc(false))
1024     offset = (this->first_plt_entry_offset() +
1025               this->count_ * this->get_plt_entry_size());
1026   return this->address() + offset + gsym->plt_offset();
1027 }
1028
1029 // Return the PLT address to use for a local symbol.  These are always
1030 // IRELATIVE relocs.
1031
1032 template<int size, bool big_endian>
1033 uint64_t
1034 Output_data_plt_aarch64<size, big_endian>::address_for_local(
1035     const Relobj* object,
1036     unsigned int r_sym)
1037 {
1038   return (this->address()
1039           + this->first_plt_entry_offset()
1040           + this->count_ * this->get_plt_entry_size()
1041           + object->local_plt_offset(r_sym));
1042 }
1043
1044 // Set the final size.
1045
1046 template<int size, bool big_endian>
1047 void
1048 Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
1049 {
1050   this->set_data_size(this->first_plt_entry_offset()
1051                       + this->count_ * this->get_plt_entry_size());
1052 }
1053
1054 template<int size, bool big_endian>
1055 class Output_data_plt_aarch64_standard :
1056   public Output_data_plt_aarch64<size, big_endian>
1057 {
1058  public:
1059   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1060   Output_data_plt_aarch64_standard(Layout* layout, Output_data_space* got_plt)
1061     : Output_data_plt_aarch64<size, big_endian>(layout,
1062                                                 size == 32 ? 4 : 8,
1063                                                 got_plt)
1064   { }
1065
1066  protected:
1067   // Return the offset of the first non-reserved PLT entry.
1068   virtual unsigned int
1069   do_first_plt_entry_offset() const
1070   { return this->first_plt_entry_size; }
1071
1072   // Return the size of a PLT entry
1073   virtual unsigned int
1074   do_get_plt_entry_size() const
1075   { return this->plt_entry_size; }
1076
1077   virtual void
1078   do_fill_first_plt_entry(unsigned char* pov,
1079                           Address got_address,
1080                           Address plt_address);
1081
1082   virtual void
1083   do_fill_plt_entry(unsigned char* pov,
1084                     Address got_address,
1085                     Address plt_address,
1086                     unsigned int got_offset,
1087                     unsigned int plt_offset);
1088
1089  private:
1090   // The size of the first plt entry size.
1091   static const int first_plt_entry_size = 32;
1092   // The size of the plt entry size.
1093   static const int plt_entry_size = 16;
1094   // Template for the first PLT entry.
1095   static const uint32_t first_plt_entry[first_plt_entry_size / 4];
1096   // Template for subsequent PLT entries.
1097   static const uint32_t plt_entry[plt_entry_size / 4];
1098 };
1099
1100 // The first entry in the PLT for an executable.
1101
1102 template<>
1103 const uint32_t
1104 Output_data_plt_aarch64_standard<32, false>::
1105     first_plt_entry[first_plt_entry_size / 4] =
1106 {
1107   0xa9bf7bf0,   /* stp x16, x30, [sp, #-16]!  */
1108   0x90000010,   /* adrp x16, PLT_GOT+0x8  */
1109   0xb9400A11,   /* ldr w17, [x16, #PLT_GOT+0x8]  */
1110   0x11002210,   /* add w16, w16,#PLT_GOT+0x8   */
1111   0xd61f0220,   /* br x17  */
1112   0xd503201f,   /* nop */
1113   0xd503201f,   /* nop */
1114   0xd503201f,   /* nop */
1115 };
1116
1117 template<>
1118 const uint32_t
1119 Output_data_plt_aarch64_standard<32, true>::
1120     first_plt_entry[first_plt_entry_size / 4] =
1121 {
1122   0xa9bf7bf0,   /* stp x16, x30, [sp, #-16]!  */
1123   0x90000010,   /* adrp x16, PLT_GOT+0x8  */
1124   0xb9400A11,   /* ldr w17, [x16, #PLT_GOT+0x8]  */
1125   0x11002210,   /* add w16, w16,#PLT_GOT+0x8   */
1126   0xd61f0220,   /* br x17  */
1127   0xd503201f,   /* nop */
1128   0xd503201f,   /* nop */
1129   0xd503201f,   /* nop */
1130 };
1131
1132 template<>
1133 const uint32_t
1134 Output_data_plt_aarch64_standard<64, false>::
1135     first_plt_entry[first_plt_entry_size / 4] =
1136 {
1137   0xa9bf7bf0,   /* stp x16, x30, [sp, #-16]!  */
1138   0x90000010,   /* adrp x16, PLT_GOT+16  */
1139   0xf9400A11,   /* ldr x17, [x16, #PLT_GOT+0x10]  */
1140   0x91004210,   /* add x16, x16,#PLT_GOT+0x10   */
1141   0xd61f0220,   /* br x17  */
1142   0xd503201f,   /* nop */
1143   0xd503201f,   /* nop */
1144   0xd503201f,   /* nop */
1145 };
1146
1147 template<>
1148 const uint32_t
1149 Output_data_plt_aarch64_standard<64, true>::
1150     first_plt_entry[first_plt_entry_size / 4] =
1151 {
1152   0xa9bf7bf0,   /* stp x16, x30, [sp, #-16]!  */
1153   0x90000010,   /* adrp x16, PLT_GOT+16  */
1154   0xf9400A11,   /* ldr x17, [x16, #PLT_GOT+0x10]  */
1155   0x91004210,   /* add x16, x16,#PLT_GOT+0x10   */
1156   0xd61f0220,   /* br x17  */
1157   0xd503201f,   /* nop */
1158   0xd503201f,   /* nop */
1159   0xd503201f,   /* nop */
1160 };
1161
1162 template<>
1163 const uint32_t
1164 Output_data_plt_aarch64_standard<32, false>::
1165     plt_entry[plt_entry_size / 4] =
1166 {
1167   0x90000010,   /* adrp x16, PLTGOT + n * 4  */
1168   0xb9400211,   /* ldr w17, [w16, PLTGOT + n * 4] */
1169   0x11000210,   /* add w16, w16, :lo12:PLTGOT + n * 4  */
1170   0xd61f0220,   /* br x17.  */
1171 };
1172
1173 template<>
1174 const uint32_t
1175 Output_data_plt_aarch64_standard<32, true>::
1176     plt_entry[plt_entry_size / 4] =
1177 {
1178   0x90000010,   /* adrp x16, PLTGOT + n * 4  */
1179   0xb9400211,   /* ldr w17, [w16, PLTGOT + n * 4] */
1180   0x11000210,   /* add w16, w16, :lo12:PLTGOT + n * 4  */
1181   0xd61f0220,   /* br x17.  */
1182 };
1183
1184 template<>
1185 const uint32_t
1186 Output_data_plt_aarch64_standard<64, false>::
1187     plt_entry[plt_entry_size / 4] =
1188 {
1189   0x90000010,   /* adrp x16, PLTGOT + n * 8  */
1190   0xf9400211,   /* ldr x17, [x16, PLTGOT + n * 8] */
1191   0x91000210,   /* add x16, x16, :lo12:PLTGOT + n * 8  */
1192   0xd61f0220,   /* br x17.  */
1193 };
1194
1195 template<>
1196 const uint32_t
1197 Output_data_plt_aarch64_standard<64, true>::
1198     plt_entry[plt_entry_size / 4] =
1199 {
1200   0x90000010,   /* adrp x16, PLTGOT + n * 8  */
1201   0xf9400211,   /* ldr x17, [x16, PLTGOT + n * 8] */
1202   0x91000210,   /* add x16, x16, :lo12:PLTGOT + n * 8  */
1203   0xd61f0220,   /* br x17.  */
1204 };
1205
1206 template<int size, bool big_endian>
1207 void
1208 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
1209     unsigned char* pov,
1210     Address got_address,
1211     Address plt_address)
1212 {
1213   // PLT0 of the small PLT looks like this in ELF64 -
1214   // stp x16, x30, [sp, #-16]!          Save the reloc and lr on stack.
1215   // adrp x16, PLT_GOT + 16             Get the page base of the GOTPLT
1216   // ldr  x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
1217   //                                    symbol resolver
1218   // add  x16, x16, #:lo12:PLT_GOT+16   Load the lo12 bits of the
1219   //                                    GOTPLT entry for this.
1220   // br   x17
1221   // PLT0 will be slightly different in ELF32 due to different got entry
1222   // size.
1223   memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
1224   Address gotplt_2nd_ent = got_address + (size / 8) * 2;
1225
1226   // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
1227   // ADRP:  (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
1228   // FIXME: This only works for 64bit
1229   AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
1230       gotplt_2nd_ent, plt_address + 4);
1231
1232   // Fill in R_AARCH64_LDST8_LO12
1233   elfcpp::Swap<32, big_endian>::writeval(
1234       pov + 8,
1235       ((this->first_plt_entry[2] & 0xffc003ff)
1236        | ((gotplt_2nd_ent & 0xff8) << 7)));
1237
1238   // Fill in R_AARCH64_ADD_ABS_LO12
1239   elfcpp::Swap<32, big_endian>::writeval(
1240       pov + 12,
1241       ((this->first_plt_entry[3] & 0xffc003ff)
1242        | ((gotplt_2nd_ent & 0xfff) << 10)));
1243 }
1244
1245 // Subsequent entries in the PLT for an executable.
1246 // FIXME: This only works for 64bit
1247
1248 template<int size, bool big_endian>
1249 void
1250 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
1251     unsigned char* pov,
1252     Address got_address,
1253     Address plt_address,
1254     unsigned int got_offset,
1255     unsigned int plt_offset)
1256 {
1257   memcpy(pov, this->plt_entry, this->plt_entry_size);
1258
1259   Address gotplt_entry_address = got_address + got_offset;
1260   Address plt_entry_address = plt_address + plt_offset;
1261
1262   // Fill in R_AARCH64_PCREL_ADR_HI21
1263   AArch64_relocate_functions<size, big_endian>::adrp(
1264       pov,
1265       gotplt_entry_address,
1266       plt_entry_address);
1267
1268   // Fill in R_AARCH64_LDST64_ABS_LO12
1269   elfcpp::Swap<32, big_endian>::writeval(
1270       pov + 4,
1271       ((this->plt_entry[1] & 0xffc003ff)
1272        | ((gotplt_entry_address & 0xff8) << 7)));
1273
1274   // Fill in R_AARCH64_ADD_ABS_LO12
1275   elfcpp::Swap<32, big_endian>::writeval(
1276       pov + 8,
1277       ((this->plt_entry[2] & 0xffc003ff)
1278        | ((gotplt_entry_address & 0xfff) <<10)));
1279
1280 }
1281
1282 // Write out the PLT.  This uses the hand-coded instructions above,
1283 // and adjusts them as needed.  This is specified by the AMD64 ABI.
1284
1285 template<int size, bool big_endian>
1286 void
1287 Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
1288 {
1289   const off_t offset = this->offset();
1290   const section_size_type oview_size =
1291     convert_to_section_size_type(this->data_size());
1292   unsigned char* const oview = of->get_output_view(offset, oview_size);
1293
1294   const off_t got_file_offset = this->got_plt_->offset();
1295   const section_size_type got_size =
1296     convert_to_section_size_type(this->got_plt_->data_size());
1297   unsigned char* const got_view = of->get_output_view(got_file_offset,
1298                                                       got_size);
1299
1300   unsigned char* pov = oview;
1301
1302   // The base address of the .plt section.
1303   typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
1304   // The base address of the PLT portion of the .got section.
1305   typename elfcpp::Elf_types<size>::Elf_Addr got_address
1306     = this->got_plt_->address();
1307
1308   this->fill_first_plt_entry(pov, got_address, plt_address);
1309   pov += this->first_plt_entry_offset();
1310
1311   // The first three entries in .got.plt are reserved.
1312   unsigned char* got_pov = got_view;
1313   memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
1314   got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
1315
1316   unsigned int plt_offset = this->first_plt_entry_offset();
1317   unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
1318   const unsigned int count = this->count_;
1319   for (unsigned int plt_index = 0;
1320        plt_index < count;
1321        ++plt_index,
1322          pov += this->get_plt_entry_size(),
1323          got_pov += size / 8,
1324          plt_offset += this->get_plt_entry_size(),
1325          got_offset += size / 8)
1326     {
1327       // Set and adjust the PLT entry itself.
1328       this->fill_plt_entry(pov, got_address, plt_address,
1329                            got_offset, plt_offset);
1330
1331       // Set the entry in the GOT, which points to plt0.
1332       elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
1333     }
1334
1335   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1336   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1337
1338   of->write_output_view(offset, oview_size, oview);
1339   of->write_output_view(got_file_offset, got_size, got_view);
1340 }
1341
1342 // Telling how to update the immediate field of an instruction.
1343 struct AArch64_howto
1344 {
1345   // The immediate field mask.
1346   elfcpp::Elf_Xword dst_mask;
1347
1348   // The offset to apply relocation immediate
1349   int doffset;
1350
1351   // The second part offset, if the immediate field has two parts.
1352   // -1 if the immediate field has only one part.
1353   int doffset2;
1354 };
1355
1356 static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
1357 {
1358   {0, -1, -1},          // DATA
1359   {0x1fffe0, 5, -1},    // MOVW  [20:5]-imm16
1360   {0xffffe0, 5, -1},    // LD    [23:5]-imm19
1361   {0x60ffffe0, 29, 5},  // ADR   [30:29]-immlo  [23:5]-immhi
1362   {0x60ffffe0, 29, 5},  // ADRP  [30:29]-immlo  [23:5]-immhi
1363   {0x3ffc00, 10, -1},   // ADD   [21:10]-imm12
1364   {0x3ffc00, 10, -1},   // LDST  [21:10]-imm12
1365   {0x7ffe0, 5, -1},     // TBZNZ [18:5]-imm14
1366   {0xffffe0, 5, -1},    // CONDB [23:5]-imm19
1367   {0x3ffffff, 0, -1},   // B     [25:0]-imm26
1368   {0x3ffffff, 0, -1},   // CALL  [25:0]-imm26
1369 };
1370
1371 // AArch64 relocate function class
1372
1373 template<int size, bool big_endian>
1374 class AArch64_relocate_functions
1375 {
1376  public:
1377   typedef enum
1378   {
1379     STATUS_OKAY,        // No error during relocation.
1380     STATUS_OVERFLOW,    // Relocation overflow.
1381     STATUS_BAD_RELOC,   // Relocation cannot be applied.
1382   } Status;
1383
1384  private:
1385   typedef AArch64_relocate_functions<size, big_endian> This;
1386   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1387
1388   // Return the page address of the address.
1389   // Page(address) = address & ~0xFFF
1390
1391   static inline typename elfcpp::Swap<size, big_endian>::Valtype
1392   Page(Address address)
1393   {
1394     return (address & (~static_cast<Address>(0xFFF)));
1395   }
1396
1397   // Update instruction (pointed by view) with selected bits (immed).
1398   // val = (val & ~dst_mask) | (immed << doffset)
1399
1400   template<int valsize>
1401   static inline void
1402   update_view(unsigned char* view,
1403               typename elfcpp::Swap<size, big_endian>::Valtype immed,
1404               elfcpp::Elf_Xword doffset,
1405               elfcpp::Elf_Xword dst_mask)
1406   {
1407     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1408     Valtype* wv = reinterpret_cast<Valtype*>(view);
1409     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1410
1411     // Clear immediate fields.
1412     val &= ~dst_mask;
1413     elfcpp::Swap<valsize, big_endian>::writeval(wv,
1414       static_cast<Valtype>(val | (immed << doffset)));
1415   }
1416
1417   // Update two parts of an instruction (pointed by view) with selected
1418   // bits (immed1 and immed2).
1419   // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
1420
1421   template<int valsize>
1422   static inline void
1423   update_view_two_parts(
1424     unsigned char* view,
1425     typename elfcpp::Swap<size, big_endian>::Valtype immed1,
1426     typename elfcpp::Swap<size, big_endian>::Valtype immed2,
1427     elfcpp::Elf_Xword doffset1,
1428     elfcpp::Elf_Xword doffset2,
1429     elfcpp::Elf_Xword dst_mask)
1430   {
1431     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1432     Valtype* wv = reinterpret_cast<Valtype*>(view);
1433     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1434     val &= ~dst_mask;
1435     elfcpp::Swap<valsize, big_endian>::writeval(wv,
1436       static_cast<Valtype>(val | (immed1 << doffset1) |
1437                            (immed2 << doffset2)));
1438   }
1439
1440   // Update adr or adrp instruction with [32:12] of X.
1441   // In adr and adrp: [30:29] immlo   [23:5] immhi
1442
1443   static inline void
1444   update_adr(unsigned char* view,
1445              typename elfcpp::Swap<size, big_endian>::Valtype x,
1446              const AArch64_reloc_property* /* reloc_property */)
1447   {
1448     elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
1449     typename elfcpp::Swap<32, big_endian>::Valtype immed =
1450       (x >> 12) & 0x1fffff;
1451     This::template update_view_two_parts<32>(
1452       view,
1453       immed & 0x3,
1454       (immed & 0x1ffffc) >> 2,
1455       29,
1456       5,
1457       dst_mask);
1458   }
1459
1460  public:
1461
1462   // Do a simple rela relocation at unaligned addresses.
1463
1464   template<int valsize>
1465   static inline typename This::Status
1466   rela_ua(unsigned char* view,
1467           const Sized_relobj_file<size, big_endian>* object,
1468           const Symbol_value<size>* psymval,
1469           typename elfcpp::Swap<size, big_endian>::Valtype addend,
1470           const AArch64_reloc_property* reloc_property)
1471   {
1472     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1473       Valtype;
1474     typename elfcpp::Elf_types<size>::Elf_Addr x =
1475         psymval->value(object, addend);
1476     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
1477       static_cast<Valtype>(x));
1478     return (reloc_property->checkup_x_value(x)
1479             ? This::STATUS_OKAY
1480             : This::STATUS_OVERFLOW);
1481   }
1482
1483   // Do a simple pc-relative relocation at unaligned addresses.
1484
1485   template<int valsize>
1486   static inline typename This::Status
1487   pcrela_ua(unsigned char* view,
1488             const Sized_relobj_file<size, big_endian>* object,
1489             const Symbol_value<size>* psymval,
1490             typename elfcpp::Swap<size, big_endian>::Valtype addend,
1491             Address address,
1492             const AArch64_reloc_property* reloc_property)
1493   {
1494     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1495       Valtype;
1496     Address x = psymval->value(object, addend) - address;
1497     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
1498       static_cast<Valtype>(x));
1499     return (reloc_property->checkup_x_value(x)
1500             ? This::STATUS_OKAY
1501             : This::STATUS_OVERFLOW);
1502   }
1503
1504   // Do a simple rela relocation at aligned addresses.
1505
1506   template<int valsize>
1507   static inline typename This::Status
1508   rela(
1509     unsigned char* view,
1510     const Sized_relobj_file<size, big_endian>* object,
1511     const Symbol_value<size>* psymval,
1512     typename elfcpp::Swap<size, big_endian>::Valtype addend,
1513     const AArch64_reloc_property* reloc_property)
1514   {
1515     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype
1516       Valtype;
1517     Valtype* wv = reinterpret_cast<Valtype*>(view);
1518     Address x = psymval->value(object, addend);
1519     elfcpp::Swap<valsize, big_endian>::writeval(wv,
1520       static_cast<Valtype>(x));
1521     return (reloc_property->checkup_x_value(x)
1522             ? This::STATUS_OKAY
1523             : This::STATUS_OVERFLOW);
1524   }
1525
1526   // Do relocate. Update selected bits in text.
1527   // new_val = (val & ~dst_mask) | (immed << doffset)
1528
1529   template<int valsize>
1530   static inline typename This::Status
1531   rela_general(unsigned char* view,
1532                const Sized_relobj_file<size, big_endian>* object,
1533                const Symbol_value<size>* psymval,
1534                typename elfcpp::Swap<size, big_endian>::Valtype addend,
1535                const AArch64_reloc_property* reloc_property)
1536   {
1537     // Calculate relocation.
1538     Address x = psymval->value(object, addend);
1539
1540     // Select bits from X.
1541     Address immed = reloc_property->select_x_value(x);
1542
1543     // Update view.
1544     const AArch64_reloc_property::Reloc_inst inst =
1545       reloc_property->reloc_inst();
1546     // If it is a data relocation or instruction has 2 parts of immediate
1547     // fields, you should not call rela_general.
1548     gold_assert(aarch64_howto[inst].doffset2 == -1 &&
1549                 aarch64_howto[inst].doffset != -1);
1550     This::template update_view<valsize>(view, immed,
1551                                         aarch64_howto[inst].doffset,
1552                                         aarch64_howto[inst].dst_mask);
1553
1554     // Do check overflow or alignment if needed.
1555     return (reloc_property->checkup_x_value(x)
1556             ? This::STATUS_OKAY
1557             : This::STATUS_OVERFLOW);
1558   }
1559
1560   // Do relocate. Update selected bits in text.
1561   // new val = (val & ~dst_mask) | (immed << doffset)
1562
1563   template<int valsize>
1564   static inline typename This::Status
1565   rela_general(
1566     unsigned char* view,
1567     typename elfcpp::Swap<size, big_endian>::Valtype s,
1568     typename elfcpp::Swap<size, big_endian>::Valtype addend,
1569     const AArch64_reloc_property* reloc_property)
1570   {
1571     // Calculate relocation.
1572     Address x = s + addend;
1573
1574     // Select bits from X.
1575     Address immed = reloc_property->select_x_value(x);
1576
1577     // Update view.
1578     const AArch64_reloc_property::Reloc_inst inst =
1579       reloc_property->reloc_inst();
1580     // If it is a data relocation or instruction has 2 parts of immediate
1581     // fields, you should not call rela_general.
1582     gold_assert(aarch64_howto[inst].doffset2 == -1 &&
1583                 aarch64_howto[inst].doffset != -1);
1584     This::template update_view<valsize>(view, immed,
1585                                         aarch64_howto[inst].doffset,
1586                                         aarch64_howto[inst].dst_mask);
1587
1588     // Do check overflow or alignment if needed.
1589     return (reloc_property->checkup_x_value(x)
1590             ? This::STATUS_OKAY
1591             : This::STATUS_OVERFLOW);
1592   }
1593
1594   // Do address relative relocate. Update selected bits in text.
1595   // new val = (val & ~dst_mask) | (immed << doffset)
1596
1597   template<int valsize>
1598   static inline typename This::Status
1599   pcrela_general(
1600     unsigned char* view,
1601     const Sized_relobj_file<size, big_endian>* object,
1602     const Symbol_value<size>* psymval,
1603     typename elfcpp::Swap<size, big_endian>::Valtype addend,
1604     Address address,
1605     const AArch64_reloc_property* reloc_property)
1606   {
1607     // Calculate relocation.
1608     Address x = psymval->value(object, addend) - address;
1609
1610     // Select bits from X.
1611     Address immed = reloc_property->select_x_value(x);
1612
1613     // Update view.
1614     const AArch64_reloc_property::Reloc_inst inst =
1615       reloc_property->reloc_inst();
1616     // If it is a data relocation or instruction has 2 parts of immediate
1617     // fields, you should not call pcrela_general.
1618     gold_assert(aarch64_howto[inst].doffset2 == -1 &&
1619                 aarch64_howto[inst].doffset != -1);
1620     This::template update_view<valsize>(view, immed,
1621                                         aarch64_howto[inst].doffset,
1622                                         aarch64_howto[inst].dst_mask);
1623
1624     // Do check overflow or alignment if needed.
1625     return (reloc_property->checkup_x_value(x)
1626             ? This::STATUS_OKAY
1627             : This::STATUS_OVERFLOW);
1628   }
1629
1630   // Calculate PG(S+A) - PG(address), update adrp instruction.
1631   // R_AARCH64_ADR_PREL_PG_HI21
1632
1633   static inline typename This::Status
1634   adrp(
1635     unsigned char* view,
1636     Address sa,
1637     Address address)
1638   {
1639     typename elfcpp::Swap<size, big_endian>::Valtype x =
1640       This::Page(sa) - This::Page(address);
1641     update_adr(view, x, NULL);
1642     return (size == 64 && Bits<32>::has_overflow(x)
1643             ? This::STATUS_OVERFLOW
1644             : This::STATUS_OKAY);
1645   }
1646
1647   // Calculate PG(S+A) - PG(address), update adrp instruction.
1648   // R_AARCH64_ADR_PREL_PG_HI21
1649
1650   static inline typename This::Status
1651   adrp(unsigned char* view,
1652        const Sized_relobj_file<size, big_endian>* object,
1653        const Symbol_value<size>* psymval,
1654        Address addend,
1655        Address address,
1656        const AArch64_reloc_property* reloc_property)
1657   {
1658     Address sa = psymval->value(object, addend);
1659     typename elfcpp::Swap<size, big_endian>::Valtype x =
1660         This::Page(sa) - This::Page(address);
1661     update_adr(view, x, reloc_property);
1662     return (reloc_property->checkup_x_value(x)
1663             ? This::STATUS_OKAY
1664             : This::STATUS_OVERFLOW);
1665   }
1666
1667 };
1668
1669 // Return the number of entries in the PLT.
1670
1671 template<int size, bool big_endian>
1672 unsigned int
1673 Target_aarch64<size, big_endian>::plt_entry_count() const
1674 {
1675   if (this->plt_ == NULL)
1676     return 0;
1677   return this->plt_->entry_count();
1678 }
1679
1680 // Return the offset of the first non-reserved PLT entry.
1681
1682 template<int size, bool big_endian>
1683 unsigned int
1684 Target_aarch64<size, big_endian>::first_plt_entry_offset() const
1685 {
1686   return this->plt_->first_plt_entry_offset();
1687 }
1688
1689 // Return the size of each PLT entry.
1690
1691 template<int size, bool big_endian>
1692 unsigned int
1693 Target_aarch64<size, big_endian>::plt_entry_size() const
1694 {
1695   return this->plt_->get_plt_entry_size();
1696 }
1697
1698 // Optimize the TLS relocation type based on what we know about the
1699 // symbol.  IS_FINAL is true if the final address of this symbol is
1700 // known at link time.
1701
1702 template<int size, bool big_endian>
1703 tls::Tls_optimization
1704 Target_aarch64<size, big_endian>::optimize_tls_reloc(bool /* is_final */,
1705                                                      int /* r_type */)
1706 {
1707   //TODO
1708   return tls::TLSOPT_NONE;
1709 }
1710
1711 // Returns true if this relocation type could be that of a function pointer.
1712
1713 template<int size, bool big_endian>
1714 inline bool
1715 Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
1716   unsigned int r_type)
1717 {
1718   switch (r_type)
1719     {
1720     case elfcpp::R_AARCH64_ABS64:
1721     //TODO
1722       {
1723         return true;
1724       }
1725     }
1726   return false;
1727 }
1728
1729 // For safe ICF, scan a relocation for a local symbol to check if it
1730 // corresponds to a function pointer being taken.  In that case mark
1731 // the function whose pointer was taken as not foldable.
1732
1733 template<int size, bool big_endian>
1734 inline bool
1735 Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
1736   Symbol_table* ,
1737   Layout* ,
1738   Target_aarch64<size, big_endian>* ,
1739   Sized_relobj_file<size, big_endian>* ,
1740   unsigned int ,
1741   Output_section* ,
1742   const elfcpp::Rela<size, big_endian>& ,
1743   unsigned int r_type,
1744   const elfcpp::Sym<size, big_endian>&)
1745 {
1746   // When building a shared library, do not fold any local symbols as it is
1747   // not possible to distinguish pointer taken versus a call by looking at
1748   // the relocation types.
1749   return (parameters->options().shared()
1750           || possible_function_pointer_reloc(r_type));
1751 }
1752
1753 // For safe ICF, scan a relocation for a global symbol to check if it
1754 // corresponds to a function pointer being taken.  In that case mark
1755 // the function whose pointer was taken as not foldable.
1756
1757 template<int size, bool big_endian>
1758 inline bool
1759 Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
1760   Symbol_table* ,
1761   Layout* ,
1762   Target_aarch64<size, big_endian>* ,
1763   Sized_relobj_file<size, big_endian>* ,
1764   unsigned int ,
1765   Output_section* ,
1766   const elfcpp::Rela<size, big_endian>& ,
1767   unsigned int r_type,
1768   Symbol* gsym)
1769 {
1770   // When building a shared library, do not fold symbols whose visibility
1771   // is hidden, internal or protected.
1772   return ((parameters->options().shared()
1773            && (gsym->visibility() == elfcpp::STV_INTERNAL
1774                || gsym->visibility() == elfcpp::STV_PROTECTED
1775                || gsym->visibility() == elfcpp::STV_HIDDEN))
1776           || possible_function_pointer_reloc(r_type));
1777 }
1778
1779 // Report an unsupported relocation against a local symbol.
1780
1781 template<int size, bool big_endian>
1782 void
1783 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
1784      Sized_relobj_file<size, big_endian>* object,
1785      unsigned int r_type)
1786 {
1787   gold_error(_("%s: unsupported reloc %u against local symbol"),
1788              object->name().c_str(), r_type);
1789 }
1790
1791 // We are about to emit a dynamic relocation of type R_TYPE.  If the
1792 // dynamic linker does not support it, issue an error.
1793
1794 template<int size, bool big_endian>
1795 void
1796 Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
1797                                                       unsigned int r_type)
1798 {
1799   gold_assert(r_type != elfcpp::R_AARCH64_NONE);
1800
1801   switch (r_type)
1802     {
1803     // These are the relocation types supported by glibc for AARCH64.
1804     case elfcpp::R_AARCH64_NONE:
1805     case elfcpp::R_AARCH64_COPY:
1806     case elfcpp::R_AARCH64_GLOB_DAT:
1807     case elfcpp::R_AARCH64_JUMP_SLOT:
1808     case elfcpp::R_AARCH64_RELATIVE:
1809     case elfcpp::R_AARCH64_TLS_DTPREL64:
1810     case elfcpp::R_AARCH64_TLS_DTPMOD64:
1811     case elfcpp::R_AARCH64_TLS_TPREL64:
1812     case elfcpp::R_AARCH64_TLSDESC:
1813     case elfcpp::R_AARCH64_IRELATIVE:
1814     case elfcpp::R_AARCH64_ABS32:
1815     case elfcpp::R_AARCH64_ABS64:
1816       return;
1817
1818     default:
1819       break;
1820     }
1821
1822   // This prevents us from issuing more than one error per reloc
1823   // section. But we can still wind up issuing more than one
1824   // error per object file.
1825   if (this->issued_non_pic_error_)
1826     return;
1827   gold_assert(parameters->options().output_is_position_independent());
1828   object->error(_("requires unsupported dynamic reloc; "
1829                   "recompile with -fPIC"));
1830   this->issued_non_pic_error_ = true;
1831   return;
1832 }
1833
1834 // Scan a relocation for a local symbol.
1835
1836 template<int size, bool big_endian>
1837 inline void
1838 Target_aarch64<size, big_endian>::Scan::local(
1839     Symbol_table* symtab,
1840     Layout* layout,
1841     Target_aarch64<size, big_endian>* target,
1842     Sized_relobj_file<size, big_endian>* object,
1843     unsigned int data_shndx,
1844     Output_section* output_section,
1845     const elfcpp::Rela<size, big_endian>& rela,
1846     unsigned int r_type,
1847     const elfcpp::Sym<size, big_endian>& /* lsym */,
1848     bool is_discarded)
1849 {
1850   if (is_discarded)
1851     return;
1852
1853   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
1854     Reloc_section;
1855
1856   switch (r_type)
1857     {
1858     case elfcpp::R_AARCH64_ABS32:
1859     case elfcpp::R_AARCH64_ABS16:
1860       if (parameters->options().output_is_position_independent())
1861         {
1862           gold_error(_("%s: unsupported reloc %u in pos independent link."),
1863                      object->name().c_str(), r_type);
1864         }
1865       break;
1866
1867     case elfcpp::R_AARCH64_ABS64:
1868       // If building a shared library or pie, we need to mark this as a dynmic
1869       // reloction, so that the dynamic loader can relocate it.
1870       if (parameters->options().output_is_position_independent())
1871         {
1872           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1873           unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1874           rela_dyn->add_local_relative(object, r_sym,
1875                                        elfcpp::R_AARCH64_RELATIVE,
1876                                        output_section,
1877                                        data_shndx,
1878                                        rela.get_r_offset(),
1879                                        rela.get_r_addend(),
1880                                        false /* is ifunc */);
1881         }
1882       break;
1883
1884     case elfcpp::R_AARCH64_PREL64:
1885     case elfcpp::R_AARCH64_PREL32:
1886     case elfcpp::R_AARCH64_PREL16:
1887       break;
1888
1889     // Relocations to generate 19, 21 and 33-bit PC-relative address
1890     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
1891     case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
1892     case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
1893     case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
1894     case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
1895     case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
1896     case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
1897       break;
1898
1899     // Control flow, pc-relative. We don't need to do anything for a relative
1900     // addressing relocation against a local symbol if it does not reference
1901     // the GOT.
1902     case elfcpp::R_AARCH64_TSTBR14:
1903     case elfcpp::R_AARCH64_CONDBR19:
1904     case elfcpp::R_AARCH64_JUMP26:
1905     case elfcpp::R_AARCH64_CALL26:
1906       break;
1907
1908     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1909     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1910       {
1911         layout->set_has_static_tls();
1912         // Create a GOT entry for the tp-relative offset.
1913         Output_data_got_aarch64<size, big_endian>* got =
1914             target->got_section(symtab, layout);
1915         unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1916         if (!parameters->doing_static_link())
1917           {
1918             got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
1919                                     target->rela_dyn_section(layout),
1920                                     elfcpp::R_AARCH64_TLS_TPREL64);
1921           }
1922         else if (!object->local_has_got_offset(r_sym,
1923                                                GOT_TYPE_TLS_OFFSET))
1924           {
1925             got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
1926             unsigned int got_offset =
1927                 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
1928             const elfcpp::Elf_Xword addend = rela.get_r_addend();
1929             gold_assert(addend == 0);
1930             got->add_static_reloc(got_offset, elfcpp::R_AARCH64_TLS_TPREL64,
1931                                   object, r_sym);
1932           }
1933       }
1934       break;
1935
1936     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
1937     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
1938     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1939       {
1940         layout->set_has_static_tls();
1941         bool output_is_shared = parameters->options().shared();
1942         if (output_is_shared)
1943           gold_error(_("%s: unsupported TLSLEreloc %u in shard code."),
1944                      object->name().c_str(), r_type);
1945       }
1946       break;
1947
1948     default:
1949       unsupported_reloc_local(object, r_type);
1950     }
1951 }
1952
1953
1954 // Report an unsupported relocation against a global symbol.
1955
1956 template<int size, bool big_endian>
1957 void
1958 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
1959     Sized_relobj_file<size, big_endian>* object,
1960     unsigned int r_type,
1961     Symbol* gsym)
1962 {
1963   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1964              object->name().c_str(), r_type, gsym->demangled_name().c_str());
1965 }
1966
1967 template<int size, bool big_endian>
1968 inline void
1969 Target_aarch64<size, big_endian>::Scan::global(
1970     Symbol_table* symtab,
1971     Layout* layout,
1972     Target_aarch64<size, big_endian>* target,
1973     Sized_relobj_file<size, big_endian> * object,
1974     unsigned int data_shndx,
1975     Output_section* output_section,
1976     const elfcpp::Rela<size, big_endian>& rela,
1977     unsigned int r_type,
1978     Symbol* gsym)
1979 {
1980   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
1981     Reloc_section;
1982   switch (r_type)
1983     {
1984     case elfcpp::R_AARCH64_ABS16:
1985     case elfcpp::R_AARCH64_ABS32:
1986     case elfcpp::R_AARCH64_ABS64:
1987       {
1988         // Make a PLT entry if necessary.
1989         if (gsym->needs_plt_entry())
1990           {
1991             target->make_plt_entry(symtab, layout, gsym);
1992             // Since this is not a PC-relative relocation, we may be
1993             // taking the address of a function. In that case we need to
1994             // set the entry in the dynamic symbol table to the address of
1995             // the PLT entry.
1996             if (gsym->is_from_dynobj() && !parameters->options().shared())
1997               gsym->set_needs_dynsym_value();
1998           }
1999         // Make a dynamic relocation if necessary.
2000         const AArch64_reloc_property* arp =
2001             aarch64_reloc_property_table->get_reloc_property(r_type);
2002         gold_assert(arp != NULL);
2003         if (gsym->needs_dynamic_reloc(arp->reference_flags()))
2004           {
2005             if (!parameters->options().output_is_position_independent()
2006                 && gsym->may_need_copy_reloc())
2007               {
2008                 gold_error(
2009                   _("%s: unsupported reloc %u which may need copyreloc."),
2010                   object->name().c_str(), r_type);
2011               }
2012             else if (r_type == elfcpp::R_AARCH64_ABS64
2013                      && gsym->can_use_relative_reloc(false))
2014               {
2015                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2016                 rela_dyn->add_global_relative(gsym,
2017                                               elfcpp::R_AARCH64_RELATIVE,
2018                                               output_section,
2019                                               object,
2020                                               data_shndx,
2021                                               rela.get_r_offset(),
2022                                               rela.get_r_addend(),
2023                                               false);
2024               }
2025             else
2026               {
2027                 check_non_pic(object, r_type);
2028                 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*
2029                     rela_dyn = target->rela_dyn_section(layout);
2030                 rela_dyn->add_global(
2031                   gsym, r_type, output_section, object,
2032                   data_shndx, rela.get_r_offset(),rela.get_r_addend());
2033               }
2034           }
2035       }
2036       break;
2037
2038     case elfcpp::R_AARCH64_PREL16:
2039     case elfcpp::R_AARCH64_PREL32:
2040     case elfcpp::R_AARCH64_PREL64:
2041       // This is used to fill the GOT absolute address.
2042       if (gsym->needs_plt_entry())
2043         {
2044           target->make_plt_entry(symtab, layout, gsym);
2045         }
2046       break;
2047
2048     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
2049     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
2050     case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
2051     case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
2052     case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
2053     case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
2054     case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
2055     case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
2056       {
2057         // Do nothing here.
2058         break;
2059       }
2060
2061     case elfcpp::R_AARCH64_ADR_GOT_PAGE:
2062     case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
2063       {
2064         // This pair of relocations is used to access a specific GOT entry.
2065         // Note a GOT entry is an *address* to a symbol.
2066         // The symbol requires a GOT entry
2067         Output_data_got_aarch64<size, big_endian>* got =
2068           target->got_section(symtab, layout);
2069         if (gsym->final_value_is_known())
2070           {
2071             got->add_global(gsym, GOT_TYPE_STANDARD);
2072           }
2073         else
2074           {
2075             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2076             if (gsym->is_from_dynobj()
2077                 || gsym->is_undefined()
2078                 || gsym->is_preemptible()
2079                 || (gsym->visibility() == elfcpp::STV_PROTECTED
2080                     && parameters->options().shared()))
2081               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
2082                                        rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
2083             else
2084               {
2085                 // Not implemented yet.
2086                 gold_assert(false);
2087               }
2088           }
2089         break;
2090       }
2091
2092     case elfcpp::R_AARCH64_TSTBR14:
2093     case elfcpp::R_AARCH64_CONDBR19:
2094     case elfcpp::R_AARCH64_JUMP26:
2095     case elfcpp::R_AARCH64_CALL26:
2096       {
2097         if (gsym->final_value_is_known())
2098           break;
2099
2100         if (gsym->is_defined() &&
2101             !gsym->is_from_dynobj() &&
2102             !gsym->is_preemptible())
2103           break;
2104
2105         // Make plt entry for function call.
2106         const AArch64_reloc_property* arp =
2107             aarch64_reloc_property_table->get_reloc_property(r_type);
2108         gold_assert(arp != NULL);
2109         target->make_plt_entry(symtab, layout, gsym);
2110         break;
2111       }
2112
2113     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
2114     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
2115       {
2116         layout->set_has_static_tls();
2117         // Create a GOT entry for the tp-relative offset.
2118         Output_data_got_aarch64<size, big_endian>* got
2119           = target->got_section(symtab, layout);
2120         if (!parameters->doing_static_link())
2121           {
2122             got->add_global_with_rel(
2123               gsym, GOT_TYPE_TLS_OFFSET,
2124               target->rela_dyn_section(layout),
2125               elfcpp::R_AARCH64_TLS_TPREL64);
2126           }
2127         if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
2128           {
2129             got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
2130             unsigned int got_offset =
2131               gsym->got_offset(GOT_TYPE_TLS_OFFSET);
2132             const elfcpp::Elf_Xword addend = rela.get_r_addend();
2133             gold_assert(addend == 0);
2134             got->add_static_reloc(got_offset,
2135                                   elfcpp::R_AARCH64_TLS_TPREL64, gsym);
2136           }
2137       }
2138       break;
2139
2140     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
2141     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
2142     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
2143       layout->set_has_static_tls();
2144       if (parameters->options().shared())
2145         gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
2146                    object->name().c_str(), r_type);
2147       break;
2148
2149     default:
2150       const AArch64_reloc_property* arp =
2151           aarch64_reloc_property_table->get_reloc_property(r_type);
2152       gold_assert(arp != NULL);
2153       gold_error(_("%s: unsupported reloc type in global scan"),
2154                  arp->name().c_str());
2155     }
2156   return;
2157 }  // End of Scan::global
2158
2159 // Create the PLT section.
2160 template<int size, bool big_endian>
2161 void
2162 Target_aarch64<size, big_endian>::make_plt_section(
2163   Symbol_table* symtab, Layout* layout)
2164 {
2165   if (this->plt_ == NULL)
2166     {
2167       // Create the GOT section first.
2168       this->got_section(symtab, layout);
2169
2170       this->plt_ = this->make_data_plt(layout, this->got_plt_);
2171
2172       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
2173                                       (elfcpp::SHF_ALLOC
2174                                        | elfcpp::SHF_EXECINSTR),
2175                                       this->plt_, ORDER_PLT, false);
2176
2177       // Make the sh_info field of .rela.plt point to .plt.
2178       Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
2179       rela_plt_os->set_info_section(this->plt_->output_section());
2180     }
2181 }
2182
2183 // Create a PLT entry for a global symbol.
2184
2185 template<int size, bool big_endian>
2186 void
2187 Target_aarch64<size, big_endian>::make_plt_entry(
2188     Symbol_table* symtab,
2189     Layout* layout,
2190     Symbol* gsym)
2191 {
2192   if (gsym->has_plt_offset())
2193     return;
2194
2195   if (this->plt_ == NULL)
2196     this->make_plt_section(symtab, layout);
2197
2198   this->plt_->add_entry(gsym);
2199 }
2200
2201 template<int size, bool big_endian>
2202 void
2203 Target_aarch64<size, big_endian>::gc_process_relocs(
2204     Symbol_table* symtab,
2205     Layout* layout,
2206     Sized_relobj_file<size, big_endian>* object,
2207     unsigned int data_shndx,
2208     unsigned int sh_type,
2209     const unsigned char* prelocs,
2210     size_t reloc_count,
2211     Output_section* output_section,
2212     bool needs_special_offset_handling,
2213     size_t local_symbol_count,
2214     const unsigned char* plocal_symbols)
2215 {
2216   if (sh_type == elfcpp::SHT_REL)
2217     {
2218       return;
2219     }
2220
2221   gold::gc_process_relocs<
2222     size, big_endian,
2223     Target_aarch64<size, big_endian>,
2224     elfcpp::SHT_RELA,
2225     typename Target_aarch64<size, big_endian>::Scan,
2226     typename Target_aarch64<size, big_endian>::Relocatable_size_for_reloc>(
2227     symtab,
2228     layout,
2229     this,
2230     object,
2231     data_shndx,
2232     prelocs,
2233     reloc_count,
2234     output_section,
2235     needs_special_offset_handling,
2236     local_symbol_count,
2237     plocal_symbols);
2238 }
2239
2240 // Scan relocations for a section.
2241
2242 template<int size, bool big_endian>
2243 void
2244 Target_aarch64<size, big_endian>::scan_relocs(
2245     Symbol_table* symtab,
2246     Layout* layout,
2247     Sized_relobj_file<size, big_endian>* object,
2248     unsigned int data_shndx,
2249     unsigned int sh_type,
2250     const unsigned char* prelocs,
2251     size_t reloc_count,
2252     Output_section* output_section,
2253     bool needs_special_offset_handling,
2254     size_t local_symbol_count,
2255     const unsigned char* plocal_symbols)
2256 {
2257   if (sh_type == elfcpp::SHT_REL)
2258     {
2259       gold_error(_("%s: unsupported REL reloc section"),
2260                  object->name().c_str());
2261       return;
2262     }
2263   gold::scan_relocs<size, big_endian, Target_aarch64, elfcpp::SHT_RELA, Scan>(
2264     symtab,
2265     layout,
2266     this,
2267     object,
2268     data_shndx,
2269     prelocs,
2270     reloc_count,
2271     output_section,
2272     needs_special_offset_handling,
2273     local_symbol_count,
2274     plocal_symbols);
2275 }
2276
2277 // Finalize the sections.
2278
2279 template<int size, bool big_endian>
2280 void
2281 Target_aarch64<size, big_endian>::do_finalize_sections(
2282     Layout* layout,
2283     const Input_objects*,
2284     Symbol_table* symtab)
2285 {
2286   const Reloc_section* rel_plt = (this->plt_ == NULL
2287                                   ? NULL
2288                                   : this->plt_->rela_plt());
2289   layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2290                                   this->rela_dyn_, true, false);
2291
2292   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2293   // the .got.plt section.
2294   Symbol* sym = this->global_offset_table_;
2295   if (sym != NULL)
2296     {
2297       uint64_t data_size = this->got_plt_->current_data_size();
2298       symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
2299
2300       // If the .got section is more than 0x8000 bytes, we add
2301       // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
2302       // bit relocations have a greater chance of working.
2303       if (data_size >= 0x8000)
2304         symtab->get_sized_symbol<size>(sym)->set_value(
2305           symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
2306     }
2307
2308   if (parameters->doing_static_link()
2309       && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
2310     {
2311       // If linking statically, make sure that the __rela_iplt symbols
2312       // were defined if necessary, even if we didn't create a PLT.
2313       static const Define_symbol_in_segment syms[] =
2314         {
2315           {
2316             "__rela_iplt_start",        // name
2317             elfcpp::PT_LOAD,            // segment_type
2318             elfcpp::PF_W,               // segment_flags_set
2319             elfcpp::PF(0),              // segment_flags_clear
2320             0,                          // value
2321             0,                          // size
2322             elfcpp::STT_NOTYPE,         // type
2323             elfcpp::STB_GLOBAL,         // binding
2324             elfcpp::STV_HIDDEN,         // visibility
2325             0,                          // nonvis
2326             Symbol::SEGMENT_START,      // offset_from_base
2327             true                        // only_if_ref
2328           },
2329           {
2330             "__rela_iplt_end",          // name
2331             elfcpp::PT_LOAD,            // segment_type
2332             elfcpp::PF_W,               // segment_flags_set
2333             elfcpp::PF(0),              // segment_flags_clear
2334             0,                          // value
2335             0,                          // size
2336             elfcpp::STT_NOTYPE,         // type
2337             elfcpp::STB_GLOBAL,         // binding
2338             elfcpp::STV_HIDDEN,         // visibility
2339             0,                          // nonvis
2340             Symbol::SEGMENT_START,      // offset_from_base
2341             true                        // only_if_ref
2342           }
2343         };
2344
2345       symtab->define_symbols(layout, 2, syms,
2346                              layout->script_options()->saw_sections_clause());
2347     }
2348
2349   return;
2350 }
2351
2352 // Perform a relocation.
2353
2354 template<int size, bool big_endian>
2355 inline bool
2356 Target_aarch64<size, big_endian>::Relocate::relocate(
2357     const Relocate_info<size, big_endian>* relinfo,
2358     Target_aarch64<size, big_endian>* target,
2359     Output_section* ,
2360     size_t relnum,
2361     const elfcpp::Rela<size, big_endian>& rela,
2362     unsigned int r_type,
2363     const Sized_symbol<size>* gsym,
2364     const Symbol_value<size>* psymval,
2365     unsigned char* view,
2366     typename elfcpp::Elf_types<size>::Elf_Addr address,
2367     section_size_type /* view_size */)
2368 {
2369   if (view == NULL)
2370     return true;
2371
2372   typedef AArch64_relocate_functions<size, big_endian> Reloc;
2373
2374   const AArch64_reloc_property* reloc_property =
2375       aarch64_reloc_property_table->get_reloc_property(r_type);
2376
2377   if (reloc_property == NULL)
2378     {
2379       std::string reloc_name =
2380           aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
2381       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2382                              _("cannot relocate %s in object file"),
2383                              reloc_name.c_str());
2384       return true;
2385     }
2386
2387   const Sized_relobj_file<size, big_endian>* object = relinfo->object;
2388
2389   // Pick the value to use for symbols defined in the PLT.
2390   Symbol_value<size> symval;
2391   if (gsym != NULL
2392       && gsym->use_plt_offset(reloc_property->reference_flags()))
2393     {
2394       symval.set_output_value(target->plt_address_for_global(gsym));
2395       psymval = &symval;
2396     }
2397   else if (gsym == NULL && psymval->is_ifunc_symbol())
2398     {
2399       unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2400       if (object->local_has_plt_offset(r_sym))
2401         {
2402           symval.set_output_value(target->plt_address_for_local(object, r_sym));
2403           psymval = &symval;
2404         }
2405     }
2406
2407   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2408
2409   // Get the GOT offset if needed.
2410   // For aarch64, the GOT pointer points to the start of the GOT section.
2411   bool have_got_offset = false;
2412   int got_offset = 0;
2413   int got_base = (target->got_ != NULL
2414                   ? (target->got_->current_data_size() >= 0x8000
2415                      ? 0x8000 : 0)
2416                   : 0);
2417   switch (r_type)
2418     {
2419     case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
2420     case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
2421     case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
2422     case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
2423     case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
2424     case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
2425     case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
2426     case elfcpp::R_AARCH64_GOTREL64:
2427     case elfcpp::R_AARCH64_GOTREL32:
2428     case elfcpp::R_AARCH64_GOT_LD_PREL19:
2429     case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
2430     case elfcpp::R_AARCH64_ADR_GOT_PAGE:
2431     case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
2432     case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
2433       if (gsym != NULL)
2434         {
2435           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2436           got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
2437         }
2438       else
2439         {
2440           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2441           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2442           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2443                         - got_base);
2444         }
2445       have_got_offset = true;
2446       break;
2447
2448     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
2449     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
2450       if (gsym != NULL)
2451         {
2452           gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2453           got_offset = gsym->got_offset(GOT_TYPE_TLS_OFFSET) - got_base;
2454         }
2455       else
2456         {
2457           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2458           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TLS_OFFSET));
2459           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2460                         - got_base);
2461         }
2462       have_got_offset = true;
2463       break;
2464
2465     default:
2466       break;
2467     }
2468
2469   typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
2470   typename elfcpp::Elf_types<size>::Elf_Addr value;
2471   switch (r_type)
2472     {
2473     case elfcpp::R_AARCH64_NONE:
2474       break;
2475
2476     case elfcpp::R_AARCH64_ABS64:
2477       reloc_status = Reloc::template rela_ua<64>(
2478         view, object, psymval, addend, reloc_property);
2479       break;
2480
2481     case elfcpp::R_AARCH64_ABS32:
2482       reloc_status = Reloc::template rela_ua<32>(
2483         view, object, psymval, addend, reloc_property);
2484       break;
2485
2486     case elfcpp::R_AARCH64_ABS16:
2487       reloc_status = Reloc::template rela_ua<16>(
2488         view, object, psymval, addend, reloc_property);
2489       break;
2490
2491     case elfcpp::R_AARCH64_PREL64:
2492       reloc_status = Reloc::template pcrela_ua<64>(
2493         view, object, psymval, addend, address, reloc_property);
2494
2495     case elfcpp::R_AARCH64_PREL32:
2496       reloc_status = Reloc::template pcrela_ua<32>(
2497         view, object, psymval, addend, address, reloc_property);
2498
2499     case elfcpp::R_AARCH64_PREL16:
2500       reloc_status = Reloc::template pcrela_ua<16>(
2501         view, object, psymval, addend, address, reloc_property);
2502
2503     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
2504     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
2505       reloc_status = Reloc::adrp(view, object, psymval, addend, address,
2506                                  reloc_property);
2507       break;
2508
2509     case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
2510     case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
2511     case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
2512     case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
2513     case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
2514     case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
2515       reloc_status = Reloc::template rela_general<32>(
2516         view, object, psymval, addend, reloc_property);
2517       break;
2518
2519     case elfcpp::R_AARCH64_TSTBR14:
2520     case elfcpp::R_AARCH64_CONDBR19:
2521     case elfcpp::R_AARCH64_CALL26:
2522     case elfcpp::R_AARCH64_JUMP26:
2523       reloc_status = Reloc::template pcrela_general<32>(
2524         view, object, psymval, addend, address, reloc_property);
2525       break;
2526
2527     case elfcpp::R_AARCH64_ADR_GOT_PAGE:
2528       gold_assert(have_got_offset);
2529       value = target->got_->address() + got_base + got_offset;
2530       reloc_status = Reloc::adrp(view, value + addend, address);
2531       break;
2532
2533     case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
2534       gold_assert(have_got_offset);
2535       value = target->got_->address() + got_base + got_offset;
2536       reloc_status = Reloc::template rela_general<32>(
2537         view, value, addend, reloc_property);
2538       break;
2539
2540     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
2541     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
2542       reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
2543                                   gsym, psymval, view, address);
2544       break;
2545
2546     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
2547     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
2548     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
2549       reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
2550                                   gsym, psymval, view, address);
2551       break;
2552
2553     default:
2554       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2555                              _("unsupported reloc aaa %u"),
2556                              r_type);
2557       break;
2558     }
2559
2560   // Report any errors.
2561   switch (reloc_status)
2562     {
2563     case Reloc::STATUS_OKAY:
2564       break;
2565     case Reloc::STATUS_OVERFLOW:
2566       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2567                              _("relocation overflow in %s"),
2568                              reloc_property->name().c_str());
2569       break;
2570     case Reloc::STATUS_BAD_RELOC:
2571       gold_error_at_location(
2572           relinfo,
2573           relnum,
2574           rela.get_r_offset(),
2575           _("unexpected opcode while processing relocation %s"),
2576           reloc_property->name().c_str());
2577       break;
2578     default:
2579       gold_unreachable();
2580     }
2581
2582   return true;
2583 }
2584
2585 template<int size, bool big_endian>
2586 inline
2587 typename AArch64_relocate_functions<size,big_endian>::Status
2588 Target_aarch64<size, big_endian>::Relocate::relocate_tls(
2589     const Relocate_info<size,big_endian> * relinfo,
2590     Target_aarch64<size, big_endian> * target,
2591     size_t /* relnum */,
2592     const elfcpp::Rela<size, big_endian> & rela,
2593     unsigned int r_type, const Sized_symbol<size> * gsym,
2594     const Symbol_value<size> * psymval,
2595     unsigned char * view,
2596     typename elfcpp::Elf_types<size>::Elf_Addr address)
2597 {
2598   typedef AArch64_relocate_functions<size,big_endian> aarch64_reloc_funcs;
2599   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_Addr;
2600
2601   const AArch64_reloc_property * reloc_property =
2602     aarch64_reloc_property_table->get_reloc_property(r_type);
2603   gold_assert(reloc_property != NULL);
2604
2605   Sized_relobj_file<size,big_endian> * object = relinfo->object;
2606   switch (r_type)
2607     {
2608     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
2609     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
2610       {
2611         // Not implemented - possible IE->LE relaxation opportunity:
2612         //   adrp xd, :gottprel:var   =>   movz xd, :tprel_g1:var
2613         typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
2614         if (gsym != NULL)
2615           {
2616             gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2617             got_entry_address = target->got_->address() +
2618               gsym->got_offset(GOT_TYPE_TLS_OFFSET);
2619           }
2620         else
2621           {
2622             unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2623             gold_assert(
2624               object->local_has_got_offset(r_sym, GOT_TYPE_TLS_OFFSET));
2625             got_entry_address = target->got_->address() +
2626               object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
2627           }
2628         if (r_type == elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21)
2629           {
2630             return aarch64_reloc_funcs::adrp(
2631                                              view, got_entry_address, address);
2632           }
2633         else if (r_type == elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
2634           {
2635             return aarch64_reloc_funcs::template rela_general<64>(
2636               view, got_entry_address, 0, reloc_property);
2637           }
2638         gold_assert(false);
2639       }
2640       break;
2641
2642     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
2643     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
2644     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
2645       {
2646         Output_segment * tls_segment = relinfo->layout->tls_segment();
2647         gold_assert(tls_segment != NULL);
2648         AArch64_Addr value = psymval->value(object, 0);
2649         const elfcpp::Elf_Xword addend = rela.get_r_addend();
2650
2651         if (!parameters->options().shared())
2652           {
2653             AArch64_Addr aligned_tcb_size =
2654               align_address(target->tcb_size(),
2655                             tls_segment->maximum_alignment());
2656             return aarch64_reloc_funcs::template
2657                 rela_general<32>(view,
2658                                  value + aligned_tcb_size,
2659                                  addend,
2660                                  reloc_property);
2661           }
2662         else
2663           gold_error(_("%s: unsupported reloc %u "
2664                        "in non-static TLSLE mode."),
2665                      object->name().c_str(), r_type);
2666       }
2667       break;
2668
2669     default:
2670       gold_error(_("%s: unsupported TLS reloc %u."),
2671                  object->name().c_str(), r_type);
2672     }
2673   return aarch64_reloc_funcs::STATUS_BAD_RELOC;
2674 }
2675
2676
2677 // Relocate section data.
2678
2679 template<int size, bool big_endian>
2680 void
2681 Target_aarch64<size, big_endian>::relocate_section(
2682     const Relocate_info<size, big_endian>* relinfo,
2683     unsigned int sh_type,
2684     const unsigned char* prelocs,
2685     size_t reloc_count,
2686     Output_section* output_section,
2687     bool needs_special_offset_handling,
2688     unsigned char* view,
2689     typename elfcpp::Elf_types<size>::Elf_Addr address,
2690     section_size_type view_size,
2691     const Reloc_symbol_changes* reloc_symbol_changes)
2692 {
2693   gold_assert(sh_type == elfcpp::SHT_RELA);
2694   typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
2695   gold::relocate_section<size, big_endian, Target_aarch64, elfcpp::SHT_RELA,
2696                          AArch64_relocate, gold::Default_comdat_behavior>(
2697     relinfo,
2698     this,
2699     prelocs,
2700     reloc_count,
2701     output_section,
2702     needs_special_offset_handling,
2703     view,
2704     address,
2705     view_size,
2706     reloc_symbol_changes);
2707 }
2708
2709 // Return the size of a relocation while scanning during a relocatable
2710 // link.
2711
2712 template<int size, bool big_endian>
2713 unsigned int
2714 Target_aarch64<size, big_endian>::Relocatable_size_for_reloc::
2715 get_size_for_reloc(
2716     unsigned int ,
2717     Relobj* )
2718 {
2719   // We will never support SHT_REL relocations.
2720   gold_unreachable();
2721   return 0;
2722 }
2723
2724 // Scan the relocs during a relocatable link.
2725
2726 template<int size, bool big_endian>
2727 void
2728 Target_aarch64<size, big_endian>::scan_relocatable_relocs(
2729     Symbol_table* symtab,
2730     Layout* layout,
2731     Sized_relobj_file<size, big_endian>* object,
2732     unsigned int data_shndx,
2733     unsigned int sh_type,
2734     const unsigned char* prelocs,
2735     size_t reloc_count,
2736     Output_section* output_section,
2737     bool needs_special_offset_handling,
2738     size_t local_symbol_count,
2739     const unsigned char* plocal_symbols,
2740     Relocatable_relocs* rr)
2741 {
2742   gold_assert(sh_type == elfcpp::SHT_RELA);
2743
2744   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2745     Relocatable_size_for_reloc> Scan_relocatable_relocs;
2746
2747   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
2748       Scan_relocatable_relocs>(
2749     symtab,
2750     layout,
2751     object,
2752     data_shndx,
2753     prelocs,
2754     reloc_count,
2755     output_section,
2756     needs_special_offset_handling,
2757     local_symbol_count,
2758     plocal_symbols,
2759     rr);
2760 }
2761
2762 // Relocate a section during a relocatable link.
2763
2764 template<int size, bool big_endian>
2765 void
2766 Target_aarch64<size, big_endian>::relocate_relocs(
2767     const Relocate_info<size, big_endian>* relinfo,
2768     unsigned int sh_type,
2769     const unsigned char* prelocs,
2770     size_t reloc_count,
2771     Output_section* output_section,
2772     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
2773     const Relocatable_relocs* rr,
2774     unsigned char* view,
2775     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2776     section_size_type view_size,
2777     unsigned char* reloc_view,
2778     section_size_type reloc_view_size)
2779 {
2780   gold_assert(sh_type == elfcpp::SHT_RELA);
2781
2782   gold::relocate_relocs<size, big_endian, elfcpp::SHT_RELA>(
2783     relinfo,
2784     prelocs,
2785     reloc_count,
2786     output_section,
2787     offset_in_output_section,
2788     rr,
2789     view,
2790     view_address,
2791     view_size,
2792     reloc_view,
2793     reloc_view_size);
2794 }
2795
2796 // The selector for aarch64 object files.
2797
2798 template<int size, bool big_endian>
2799 class Target_selector_aarch64 : public Target_selector
2800 {
2801  public:
2802   Target_selector_aarch64();
2803
2804   virtual Target*
2805   do_instantiate_target()
2806   { return new Target_aarch64<size, big_endian>(); }
2807 };
2808
2809 template<>
2810 Target_selector_aarch64<32, true>::Target_selector_aarch64()
2811   : Target_selector(elfcpp::EM_AARCH64, 32, true,
2812                     "elf32-bigaarch64", "aarch64_elf32_be_vec")
2813 { }
2814
2815 template<>
2816 Target_selector_aarch64<32, false>::Target_selector_aarch64()
2817   : Target_selector(elfcpp::EM_AARCH64, 32, false,
2818                     "elf32-littleaarch64", "aarch64_elf32_le_vec")
2819 { }
2820
2821 template<>
2822 Target_selector_aarch64<64, true>::Target_selector_aarch64()
2823   : Target_selector(elfcpp::EM_AARCH64, 64, true,
2824                     "elf64-bigaarch64", "aarch64_elf64_be_vec")
2825 { }
2826
2827 template<>
2828 Target_selector_aarch64<64, false>::Target_selector_aarch64()
2829   : Target_selector(elfcpp::EM_AARCH64, 64, false,
2830                     "elf64-littleaarch64", "aarch64_elf64_le_vec")
2831 { }
2832
2833 Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
2834 Target_selector_aarch64<32, false> target_selector_aarch64elf32;
2835 Target_selector_aarch64<64, true> target_selector_aarch64elfb;
2836 Target_selector_aarch64<64, false> target_selector_aarch64elf;
2837
2838 } // End anonymous namespace.