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