Patch for gold internal error while fixing erratum 843419.
[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.
1835       const char* sym_name = pnames + sym.get_st_name();
1836       if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd')
1837           && sym_name[2] == '\0')
1838         {
1839           bool is_ordinary;
1840           unsigned int input_shndx =
1841             this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
1842           gold_assert(is_ordinary);
1843
1844           Mapping_symbol_position msp(input_shndx, input_value);
1845           // Insert mapping_symbol_info into map whose ordering is defined by
1846           // (shndx, offset_within_section).
1847           this->mapping_symbol_info_[msp] = sym_name[1];
1848         }
1849    }
1850 }
1851
1852
1853 // Fix all errata in the object.
1854
1855 template<int size, bool big_endian>
1856 void
1857 AArch64_relobj<size, big_endian>::fix_errata(
1858     typename Sized_relobj_file<size, big_endian>::Views* pviews)
1859 {
1860   typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
1861   unsigned int shnum = this->shnum();
1862   for (unsigned int i = 1; i < shnum; ++i)
1863     {
1864       The_stub_table* stub_table = this->stub_table(i);
1865       if (!stub_table)
1866         continue;
1867       std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
1868         ipair(stub_table->find_erratum_stubs_for_input_section(this, i));
1869       Erratum_stub_set_iter p = ipair.first, end = ipair.second;
1870       while (p != end)
1871         {
1872           The_erratum_stub* stub = *p;
1873           typename Sized_relobj_file<size, big_endian>::View_size&
1874             pview((*pviews)[i]);
1875
1876           // Double check data before fix.
1877           gold_assert(pview.address + stub->sh_offset()
1878                       == stub->erratum_address());
1879
1880           // Update previously recorded erratum insn with relocated
1881           // version.
1882           Insntype* ip =
1883             reinterpret_cast<Insntype*>(pview.view + stub->sh_offset());
1884           Insntype insn_to_fix = ip[0];
1885           stub->update_erratum_insn(insn_to_fix);
1886
1887           // Replace the erratum insn with a branch-to-stub.
1888           AArch64_address stub_address =
1889             stub_table->erratum_stub_address(stub);
1890           unsigned int b_offset = stub_address - stub->erratum_address();
1891           AArch64_relocate_functions<size, big_endian>::construct_b(
1892             pview.view + stub->sh_offset(), b_offset & 0xfffffff);
1893           ++p;
1894         }
1895     }
1896 }
1897
1898
1899 // Relocate sections.
1900
1901 template<int size, bool big_endian>
1902 void
1903 AArch64_relobj<size, big_endian>::do_relocate_sections(
1904     const Symbol_table* symtab, const Layout* layout,
1905     const unsigned char* pshdrs, Output_file* of,
1906     typename Sized_relobj_file<size, big_endian>::Views* pviews)
1907 {
1908   // Call parent to relocate sections.
1909   Sized_relobj_file<size, big_endian>::do_relocate_sections(symtab, layout,
1910                                                             pshdrs, of, pviews);
1911
1912   // We do not generate stubs if doing a relocatable link.
1913   if (parameters->options().relocatable())
1914     return;
1915
1916   if (parameters->options().fix_cortex_a53_843419()
1917       || parameters->options().fix_cortex_a53_835769())
1918     this->fix_errata(pviews);
1919
1920   Relocate_info<size, big_endian> relinfo;
1921   relinfo.symtab = symtab;
1922   relinfo.layout = layout;
1923   relinfo.object = this;
1924
1925   // Relocate stub tables.
1926   unsigned int shnum = this->shnum();
1927   The_target_aarch64* target = The_target_aarch64::current_target();
1928
1929   for (unsigned int i = 1; i < shnum; ++i)
1930     {
1931       The_aarch64_input_section* aarch64_input_section =
1932           target->find_aarch64_input_section(this, i);
1933       if (aarch64_input_section != NULL
1934           && aarch64_input_section->is_stub_table_owner()
1935           && !aarch64_input_section->stub_table()->empty())
1936         {
1937           Output_section* os = this->output_section(i);
1938           gold_assert(os != NULL);
1939
1940           relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
1941           relinfo.reloc_shdr = NULL;
1942           relinfo.data_shndx = i;
1943           relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<size>::shdr_size;
1944
1945           typename Sized_relobj_file<size, big_endian>::View_size&
1946               view_struct = (*pviews)[i];
1947           gold_assert(view_struct.view != NULL);
1948
1949           The_stub_table* stub_table = aarch64_input_section->stub_table();
1950           off_t offset = stub_table->address() - view_struct.address;
1951           unsigned char* view = view_struct.view + offset;
1952           AArch64_address address = stub_table->address();
1953           section_size_type view_size = stub_table->data_size();
1954           stub_table->relocate_stubs(&relinfo, target, os, view, address,
1955                                      view_size);
1956         }
1957     }
1958 }
1959
1960
1961 // Determine if an input section is scannable for stub processing.  SHDR is
1962 // the header of the section and SHNDX is the section index.  OS is the output
1963 // section for the input section and SYMTAB is the global symbol table used to
1964 // look up ICF information.
1965
1966 template<int size, bool big_endian>
1967 bool
1968 AArch64_relobj<size, big_endian>::text_section_is_scannable(
1969     const elfcpp::Shdr<size, big_endian>& text_shdr,
1970     unsigned int text_shndx,
1971     const Output_section* os,
1972     const Symbol_table* symtab)
1973 {
1974   // Skip any empty sections, unallocated sections or sections whose
1975   // type are not SHT_PROGBITS.
1976   if (text_shdr.get_sh_size() == 0
1977       || (text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
1978       || text_shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
1979     return false;
1980
1981   // Skip any discarded or ICF'ed sections.
1982   if (os == NULL || symtab->is_section_folded(this, text_shndx))
1983     return false;
1984
1985   // Skip exception frame.
1986   if (strcmp(os->name(), ".eh_frame") == 0)
1987     return false ;
1988
1989   gold_assert(!this->is_output_section_offset_invalid(text_shndx) ||
1990               os->find_relaxed_input_section(this, text_shndx) != NULL);
1991
1992   return true;
1993 }
1994
1995
1996 // Determine if we want to scan the SHNDX-th section for relocation stubs.
1997 // This is a helper for AArch64_relobj::scan_sections_for_stubs().
1998
1999 template<int size, bool big_endian>
2000 bool
2001 AArch64_relobj<size, big_endian>::section_needs_reloc_stub_scanning(
2002     const elfcpp::Shdr<size, big_endian>& shdr,
2003     const Relobj::Output_sections& out_sections,
2004     const Symbol_table* symtab,
2005     const unsigned char* pshdrs)
2006 {
2007   unsigned int sh_type = shdr.get_sh_type();
2008   if (sh_type != elfcpp::SHT_RELA)
2009     return false;
2010
2011   // Ignore empty section.
2012   off_t sh_size = shdr.get_sh_size();
2013   if (sh_size == 0)
2014     return false;
2015
2016   // Ignore reloc section with unexpected symbol table.  The
2017   // error will be reported in the final link.
2018   if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
2019     return false;
2020
2021   gold_assert(sh_type == elfcpp::SHT_RELA);
2022   unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2023
2024   // Ignore reloc section with unexpected entsize or uneven size.
2025   // The error will be reported in the final link.
2026   if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
2027     return false;
2028
2029   // Ignore reloc section with bad info.  This error will be
2030   // reported in the final link.
2031   unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_info());
2032   if (text_shndx >= this->shnum())
2033     return false;
2034
2035   const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2036   const elfcpp::Shdr<size, big_endian> text_shdr(pshdrs +
2037                                                  text_shndx * shdr_size);
2038   return this->text_section_is_scannable(text_shdr, text_shndx,
2039                                          out_sections[text_shndx], symtab);
2040 }
2041
2042
2043 // Scan section SHNDX for erratum 843419 and 835769.
2044
2045 template<int size, bool big_endian>
2046 void
2047 AArch64_relobj<size, big_endian>::scan_errata(
2048     unsigned int shndx, const elfcpp::Shdr<size, big_endian>& shdr,
2049     Output_section* os, const Symbol_table* symtab,
2050     The_target_aarch64* target)
2051 {
2052   if (shdr.get_sh_size() == 0
2053       || (shdr.get_sh_flags() &
2054           (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) == 0
2055       || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
2056     return;
2057
2058   if (!os || symtab->is_section_folded(this, shndx)) return;
2059
2060   AArch64_address output_offset = this->get_output_section_offset(shndx);
2061   AArch64_address output_address;
2062   if (output_offset != invalid_address)
2063     output_address = os->address() + output_offset;
2064   else
2065     {
2066       const Output_relaxed_input_section* poris =
2067         os->find_relaxed_input_section(this, shndx);
2068       if (!poris) return;
2069       output_address = poris->address();
2070     }
2071
2072   section_size_type input_view_size = 0;
2073   const unsigned char* input_view =
2074     this->section_contents(shndx, &input_view_size, false);
2075
2076   Mapping_symbol_position section_start(shndx, 0);
2077   // Find the first mapping symbol record within section shndx.
2078   typename Mapping_symbol_info::const_iterator p =
2079     this->mapping_symbol_info_.lower_bound(section_start);
2080   if (p == this->mapping_symbol_info_.end() || p->first.shndx_ != shndx)
2081     gold_warning(_("cannot scan executable section %u of %s for Cortex-A53 "
2082                    "erratum because it has no mapping symbols."),
2083                  shndx, this->name().c_str());
2084   while (p != this->mapping_symbol_info_.end() &&
2085          p->first.shndx_ == shndx)
2086     {
2087       typename Mapping_symbol_info::const_iterator prev = p;
2088       ++p;
2089       if (prev->second == 'x')
2090         {
2091           section_size_type span_start =
2092             convert_to_section_size_type(prev->first.offset_);
2093           section_size_type span_end;
2094           if (p != this->mapping_symbol_info_.end()
2095               && p->first.shndx_ == shndx)
2096             span_end = convert_to_section_size_type(p->first.offset_);
2097           else
2098             span_end = convert_to_section_size_type(shdr.get_sh_size());
2099
2100           // Here we do not share the scanning code of both errata. For 843419,
2101           // only the last few insns of each page are examined, which is fast,
2102           // whereas, for 835769, every insn pair needs to be checked.
2103
2104           if (parameters->options().fix_cortex_a53_843419())
2105             target->scan_erratum_843419_span(
2106               this, shndx, span_start, span_end,
2107               const_cast<unsigned char*>(input_view), output_address);
2108
2109           if (parameters->options().fix_cortex_a53_835769())
2110             target->scan_erratum_835769_span(
2111               this, shndx, span_start, span_end,
2112               const_cast<unsigned char*>(input_view), output_address);
2113         }
2114     }
2115 }
2116
2117
2118 // Scan relocations for stub generation.
2119
2120 template<int size, bool big_endian>
2121 void
2122 AArch64_relobj<size, big_endian>::scan_sections_for_stubs(
2123     The_target_aarch64* target,
2124     const Symbol_table* symtab,
2125     const Layout* layout)
2126 {
2127   unsigned int shnum = this->shnum();
2128   const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2129
2130   // Read the section headers.
2131   const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
2132                                                shnum * shdr_size,
2133                                                true, true);
2134
2135   // To speed up processing, we set up hash tables for fast lookup of
2136   // input offsets to output addresses.
2137   this->initialize_input_to_output_maps();
2138
2139   const Relobj::Output_sections& out_sections(this->output_sections());
2140
2141   Relocate_info<size, big_endian> relinfo;
2142   relinfo.symtab = symtab;
2143   relinfo.layout = layout;
2144   relinfo.object = this;
2145
2146   // Do relocation stubs scanning.
2147   const unsigned char* p = pshdrs + shdr_size;
2148   for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
2149     {
2150       const elfcpp::Shdr<size, big_endian> shdr(p);
2151       if (parameters->options().fix_cortex_a53_843419()
2152           || parameters->options().fix_cortex_a53_835769())
2153         scan_errata(i, shdr, out_sections[i], symtab, target);
2154       if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
2155                                                   pshdrs))
2156         {
2157           unsigned int index = this->adjust_shndx(shdr.get_sh_info());
2158           AArch64_address output_offset =
2159               this->get_output_section_offset(index);
2160           AArch64_address output_address;
2161           if (output_offset != invalid_address)
2162             {
2163               output_address = out_sections[index]->address() + output_offset;
2164             }
2165           else
2166             {
2167               // Currently this only happens for a relaxed section.
2168               const Output_relaxed_input_section* poris =
2169                   out_sections[index]->find_relaxed_input_section(this, index);
2170               gold_assert(poris != NULL);
2171               output_address = poris->address();
2172             }
2173
2174           // Get the relocations.
2175           const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
2176                                                         shdr.get_sh_size(),
2177                                                         true, false);
2178
2179           // Get the section contents.
2180           section_size_type input_view_size = 0;
2181           const unsigned char* input_view =
2182               this->section_contents(index, &input_view_size, false);
2183
2184           relinfo.reloc_shndx = i;
2185           relinfo.data_shndx = index;
2186           unsigned int sh_type = shdr.get_sh_type();
2187           unsigned int reloc_size;
2188           gold_assert (sh_type == elfcpp::SHT_RELA);
2189           reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2190
2191           Output_section* os = out_sections[index];
2192           target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
2193                                          shdr.get_sh_size() / reloc_size,
2194                                          os,
2195                                          output_offset == invalid_address,
2196                                          input_view, output_address,
2197                                          input_view_size);
2198         }
2199     }
2200 }
2201
2202
2203 // A class to wrap an ordinary input section containing executable code.
2204
2205 template<int size, bool big_endian>
2206 class AArch64_input_section : public Output_relaxed_input_section
2207 {
2208  public:
2209   typedef Stub_table<size, big_endian> The_stub_table;
2210
2211   AArch64_input_section(Relobj* relobj, unsigned int shndx)
2212     : Output_relaxed_input_section(relobj, shndx, 1),
2213       stub_table_(NULL),
2214       original_contents_(NULL), original_size_(0),
2215       original_addralign_(1)
2216   { }
2217
2218   ~AArch64_input_section()
2219   { delete[] this->original_contents_; }
2220
2221   // Initialize.
2222   void
2223   init();
2224
2225   // Set the stub_table.
2226   void
2227   set_stub_table(The_stub_table* st)
2228   { this->stub_table_ = st; }
2229
2230   // Whether this is a stub table owner.
2231   bool
2232   is_stub_table_owner() const
2233   { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
2234
2235   // Return the original size of the section.
2236   uint32_t
2237   original_size() const
2238   { return this->original_size_; }
2239
2240   // Return the stub table.
2241   The_stub_table*
2242   stub_table()
2243   { return stub_table_; }
2244
2245  protected:
2246   // Write out this input section.
2247   void
2248   do_write(Output_file*);
2249
2250   // Return required alignment of this.
2251   uint64_t
2252   do_addralign() const
2253   {
2254     if (this->is_stub_table_owner())
2255       return std::max(this->stub_table_->addralign(),
2256                       static_cast<uint64_t>(this->original_addralign_));
2257     else
2258       return this->original_addralign_;
2259   }
2260
2261   // Finalize data size.
2262   void
2263   set_final_data_size();
2264
2265   // Reset address and file offset.
2266   void
2267   do_reset_address_and_file_offset();
2268
2269   // Output offset.
2270   bool
2271   do_output_offset(const Relobj* object, unsigned int shndx,
2272                    section_offset_type offset,
2273                    section_offset_type* poutput) const
2274   {
2275     if ((object == this->relobj())
2276         && (shndx == this->shndx())
2277         && (offset >= 0)
2278         && (offset <=
2279             convert_types<section_offset_type, uint32_t>(this->original_size_)))
2280       {
2281         *poutput = offset;
2282         return true;
2283       }
2284     else
2285       return false;
2286   }
2287
2288  private:
2289   // Copying is not allowed.
2290   AArch64_input_section(const AArch64_input_section&);
2291   AArch64_input_section& operator=(const AArch64_input_section&);
2292
2293   // The relocation stubs.
2294   The_stub_table* stub_table_;
2295   // Original section contents.  We have to make a copy here since the file
2296   // containing the original section may not be locked when we need to access
2297   // the contents.
2298   unsigned char* original_contents_;
2299   // Section size of the original input section.
2300   uint32_t original_size_;
2301   // Address alignment of the original input section.
2302   uint32_t original_addralign_;
2303 };  // End of AArch64_input_section
2304
2305
2306 // Finalize data size.
2307
2308 template<int size, bool big_endian>
2309 void
2310 AArch64_input_section<size, big_endian>::set_final_data_size()
2311 {
2312   off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2313
2314   if (this->is_stub_table_owner())
2315     {
2316       this->stub_table_->finalize_data_size();
2317       off = align_address(off, this->stub_table_->addralign());
2318       off += this->stub_table_->data_size();
2319     }
2320   this->set_data_size(off);
2321 }
2322
2323
2324 // Reset address and file offset.
2325
2326 template<int size, bool big_endian>
2327 void
2328 AArch64_input_section<size, big_endian>::do_reset_address_and_file_offset()
2329 {
2330   // Size of the original input section contents.
2331   off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2332
2333   // If this is a stub table owner, account for the stub table size.
2334   if (this->is_stub_table_owner())
2335     {
2336       The_stub_table* stub_table = this->stub_table_;
2337
2338       // Reset the stub table's address and file offset.  The
2339       // current data size for child will be updated after that.
2340       stub_table_->reset_address_and_file_offset();
2341       off = align_address(off, stub_table_->addralign());
2342       off += stub_table->current_data_size();
2343     }
2344
2345   this->set_current_data_size(off);
2346 }
2347
2348
2349 // Initialize an Arm_input_section.
2350
2351 template<int size, bool big_endian>
2352 void
2353 AArch64_input_section<size, big_endian>::init()
2354 {
2355   Relobj* relobj = this->relobj();
2356   unsigned int shndx = this->shndx();
2357
2358   // We have to cache original size, alignment and contents to avoid locking
2359   // the original file.
2360   this->original_addralign_ =
2361       convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
2362
2363   // This is not efficient but we expect only a small number of relaxed
2364   // input sections for stubs.
2365   section_size_type section_size;
2366   const unsigned char* section_contents =
2367       relobj->section_contents(shndx, &section_size, false);
2368   this->original_size_ =
2369       convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
2370
2371   gold_assert(this->original_contents_ == NULL);
2372   this->original_contents_ = new unsigned char[section_size];
2373   memcpy(this->original_contents_, section_contents, section_size);
2374
2375   // We want to make this look like the original input section after
2376   // output sections are finalized.
2377   Output_section* os = relobj->output_section(shndx);
2378   off_t offset = relobj->output_section_offset(shndx);
2379   gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2380   this->set_address(os->address() + offset);
2381   this->set_file_offset(os->offset() + offset);
2382   this->set_current_data_size(this->original_size_);
2383   this->finalize_data_size();
2384 }
2385
2386
2387 // Write data to output file.
2388
2389 template<int size, bool big_endian>
2390 void
2391 AArch64_input_section<size, big_endian>::do_write(Output_file* of)
2392 {
2393   // We have to write out the original section content.
2394   gold_assert(this->original_contents_ != NULL);
2395   of->write(this->offset(), this->original_contents_,
2396             this->original_size_);
2397
2398   // If this owns a stub table and it is not empty, write it.
2399   if (this->is_stub_table_owner() && !this->stub_table_->empty())
2400     this->stub_table_->write(of);
2401 }
2402
2403
2404 // Arm output section class.  This is defined mainly to add a number of stub
2405 // generation methods.
2406
2407 template<int size, bool big_endian>
2408 class AArch64_output_section : public Output_section
2409 {
2410  public:
2411   typedef Target_aarch64<size, big_endian> The_target_aarch64;
2412   typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2413   typedef Stub_table<size, big_endian> The_stub_table;
2414   typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2415
2416  public:
2417   AArch64_output_section(const char* name, elfcpp::Elf_Word type,
2418                          elfcpp::Elf_Xword flags)
2419     : Output_section(name, type, flags)
2420   { }
2421
2422   ~AArch64_output_section() {}
2423
2424   // Group input sections for stub generation.
2425   void
2426   group_sections(section_size_type, bool, Target_aarch64<size, big_endian>*,
2427                  const Task*);
2428
2429  private:
2430   typedef Output_section::Input_section Input_section;
2431   typedef Output_section::Input_section_list Input_section_list;
2432
2433   // Create a stub group.
2434   void
2435   create_stub_group(Input_section_list::const_iterator,
2436                     Input_section_list::const_iterator,
2437                     Input_section_list::const_iterator,
2438                     The_target_aarch64*,
2439                     std::vector<Output_relaxed_input_section*>&,
2440                     const Task*);
2441 };  // End of AArch64_output_section
2442
2443
2444 // Create a stub group for input sections from FIRST to LAST. OWNER points to
2445 // the input section that will be the owner of the stub table.
2446
2447 template<int size, bool big_endian> void
2448 AArch64_output_section<size, big_endian>::create_stub_group(
2449     Input_section_list::const_iterator first,
2450     Input_section_list::const_iterator last,
2451     Input_section_list::const_iterator owner,
2452     The_target_aarch64* target,
2453     std::vector<Output_relaxed_input_section*>& new_relaxed_sections,
2454     const Task* task)
2455 {
2456   // Currently we convert ordinary input sections into relaxed sections only
2457   // at this point.
2458   The_aarch64_input_section* input_section;
2459   if (owner->is_relaxed_input_section())
2460     gold_unreachable();
2461   else
2462     {
2463       gold_assert(owner->is_input_section());
2464       // Create a new relaxed input section.  We need to lock the original
2465       // file.
2466       Task_lock_obj<Object> tl(task, owner->relobj());
2467       input_section =
2468           target->new_aarch64_input_section(owner->relobj(), owner->shndx());
2469       new_relaxed_sections.push_back(input_section);
2470     }
2471
2472   // Create a stub table.
2473   The_stub_table* stub_table =
2474       target->new_stub_table(input_section);
2475
2476   input_section->set_stub_table(stub_table);
2477
2478   Input_section_list::const_iterator p = first;
2479   // Look for input sections or relaxed input sections in [first ... last].
2480   do
2481     {
2482       if (p->is_input_section() || p->is_relaxed_input_section())
2483         {
2484           // The stub table information for input sections live
2485           // in their objects.
2486           The_aarch64_relobj* aarch64_relobj =
2487               static_cast<The_aarch64_relobj*>(p->relobj());
2488           aarch64_relobj->set_stub_table(p->shndx(), stub_table);
2489         }
2490     }
2491   while (p++ != last);
2492 }
2493
2494
2495 // Group input sections for stub generation. GROUP_SIZE is roughly the limit of
2496 // stub groups. We grow a stub group by adding input section until the size is
2497 // just below GROUP_SIZE. The last input section will be converted into a stub
2498 // table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
2499 // after the stub table, effectively doubling the group size.
2500 //
2501 // This is similar to the group_sections() function in elf32-arm.c but is
2502 // implemented differently.
2503
2504 template<int size, bool big_endian>
2505 void AArch64_output_section<size, big_endian>::group_sections(
2506     section_size_type group_size,
2507     bool stubs_always_after_branch,
2508     Target_aarch64<size, big_endian>* target,
2509     const Task* task)
2510 {
2511   typedef enum
2512   {
2513     NO_GROUP,
2514     FINDING_STUB_SECTION,
2515     HAS_STUB_SECTION
2516   } State;
2517
2518   std::vector<Output_relaxed_input_section*> new_relaxed_sections;
2519
2520   State state = NO_GROUP;
2521   section_size_type off = 0;
2522   section_size_type group_begin_offset = 0;
2523   section_size_type group_end_offset = 0;
2524   section_size_type stub_table_end_offset = 0;
2525   Input_section_list::const_iterator group_begin =
2526       this->input_sections().end();
2527   Input_section_list::const_iterator stub_table =
2528       this->input_sections().end();
2529   Input_section_list::const_iterator group_end = this->input_sections().end();
2530   for (Input_section_list::const_iterator p = this->input_sections().begin();
2531        p != this->input_sections().end();
2532        ++p)
2533     {
2534       section_size_type section_begin_offset =
2535         align_address(off, p->addralign());
2536       section_size_type section_end_offset =
2537         section_begin_offset + p->data_size();
2538
2539       // Check to see if we should group the previously seen sections.
2540       switch (state)
2541         {
2542         case NO_GROUP:
2543           break;
2544
2545         case FINDING_STUB_SECTION:
2546           // Adding this section makes the group larger than GROUP_SIZE.
2547           if (section_end_offset - group_begin_offset >= group_size)
2548             {
2549               if (stubs_always_after_branch)
2550                 {
2551                   gold_assert(group_end != this->input_sections().end());
2552                   this->create_stub_group(group_begin, group_end, group_end,
2553                                           target, new_relaxed_sections,
2554                                           task);
2555                   state = NO_GROUP;
2556                 }
2557               else
2558                 {
2559                   // Input sections up to stub_group_size bytes after the stub
2560                   // table can be handled by it too.
2561                   state = HAS_STUB_SECTION;
2562                   stub_table = group_end;
2563                   stub_table_end_offset = group_end_offset;
2564                 }
2565             }
2566             break;
2567
2568         case HAS_STUB_SECTION:
2569           // Adding this section makes the post stub-section group larger
2570           // than GROUP_SIZE.
2571           gold_unreachable();
2572           // NOT SUPPORTED YET. For completeness only.
2573           if (section_end_offset - stub_table_end_offset >= group_size)
2574            {
2575              gold_assert(group_end != this->input_sections().end());
2576              this->create_stub_group(group_begin, group_end, stub_table,
2577                                      target, new_relaxed_sections, task);
2578              state = NO_GROUP;
2579            }
2580            break;
2581
2582           default:
2583             gold_unreachable();
2584         }
2585
2586       // If we see an input section and currently there is no group, start
2587       // a new one.  Skip any empty sections.  We look at the data size
2588       // instead of calling p->relobj()->section_size() to avoid locking.
2589       if ((p->is_input_section() || p->is_relaxed_input_section())
2590           && (p->data_size() != 0))
2591         {
2592           if (state == NO_GROUP)
2593             {
2594               state = FINDING_STUB_SECTION;
2595               group_begin = p;
2596               group_begin_offset = section_begin_offset;
2597             }
2598
2599           // Keep track of the last input section seen.
2600           group_end = p;
2601           group_end_offset = section_end_offset;
2602         }
2603
2604       off = section_end_offset;
2605     }
2606
2607   // Create a stub group for any ungrouped sections.
2608   if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
2609     {
2610       gold_assert(group_end != this->input_sections().end());
2611       this->create_stub_group(group_begin, group_end,
2612                               (state == FINDING_STUB_SECTION
2613                                ? group_end
2614                                : stub_table),
2615                               target, new_relaxed_sections, task);
2616     }
2617
2618   if (!new_relaxed_sections.empty())
2619     this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
2620
2621   // Update the section offsets
2622   for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
2623     {
2624       The_aarch64_relobj* relobj = static_cast<The_aarch64_relobj*>(
2625           new_relaxed_sections[i]->relobj());
2626       unsigned int shndx = new_relaxed_sections[i]->shndx();
2627       // Tell AArch64_relobj that this input section is converted.
2628       relobj->convert_input_section_to_relaxed_section(shndx);
2629     }
2630 }  // End of AArch64_output_section::group_sections
2631
2632
2633 AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
2634
2635
2636 // The aarch64 target class.
2637 // See the ABI at
2638 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
2639 template<int size, bool big_endian>
2640 class Target_aarch64 : public Sized_target<size, big_endian>
2641 {
2642  public:
2643   typedef Target_aarch64<size, big_endian> This;
2644   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
2645       Reloc_section;
2646   typedef Relocate_info<size, big_endian> The_relocate_info;
2647   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2648   typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2649   typedef Reloc_stub<size, big_endian> The_reloc_stub;
2650   typedef Erratum_stub<size, big_endian> The_erratum_stub;
2651   typedef typename Reloc_stub<size, big_endian>::Key The_reloc_stub_key;
2652   typedef Stub_table<size, big_endian> The_stub_table;
2653   typedef std::vector<The_stub_table*> Stub_table_list;
2654   typedef typename Stub_table_list::iterator Stub_table_iterator;
2655   typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2656   typedef AArch64_output_section<size, big_endian> The_aarch64_output_section;
2657   typedef Unordered_map<Section_id,
2658                         AArch64_input_section<size, big_endian>*,
2659                         Section_id_hash> AArch64_input_section_map;
2660   typedef AArch64_insn_utilities<big_endian> Insn_utilities;
2661   const static int TCB_SIZE = size / 8 * 2;
2662
2663   Target_aarch64(const Target::Target_info* info = &aarch64_info)
2664     : Sized_target<size, big_endian>(info),
2665       got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
2666       got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
2667       rela_irelative_(NULL), copy_relocs_(elfcpp::R_AARCH64_COPY),
2668       got_mod_index_offset_(-1U),
2669       tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
2670       stub_tables_(), stub_group_size_(0), aarch64_input_section_map_()
2671   { }
2672
2673   // Scan the relocations to determine unreferenced sections for
2674   // garbage collection.
2675   void
2676   gc_process_relocs(Symbol_table* symtab,
2677                     Layout* layout,
2678                     Sized_relobj_file<size, big_endian>* object,
2679                     unsigned int data_shndx,
2680                     unsigned int sh_type,
2681                     const unsigned char* prelocs,
2682                     size_t reloc_count,
2683                     Output_section* output_section,
2684                     bool needs_special_offset_handling,
2685                     size_t local_symbol_count,
2686                     const unsigned char* plocal_symbols);
2687
2688   // Scan the relocations to look for symbol adjustments.
2689   void
2690   scan_relocs(Symbol_table* symtab,
2691               Layout* layout,
2692               Sized_relobj_file<size, big_endian>* object,
2693               unsigned int data_shndx,
2694               unsigned int sh_type,
2695               const unsigned char* prelocs,
2696               size_t reloc_count,
2697               Output_section* output_section,
2698               bool needs_special_offset_handling,
2699               size_t local_symbol_count,
2700               const unsigned char* plocal_symbols);
2701
2702   // Finalize the sections.
2703   void
2704   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2705
2706   // Return the value to use for a dynamic which requires special
2707   // treatment.
2708   uint64_t
2709   do_dynsym_value(const Symbol*) const;
2710
2711   // Relocate a section.
2712   void
2713   relocate_section(const Relocate_info<size, big_endian>*,
2714                    unsigned int sh_type,
2715                    const unsigned char* prelocs,
2716                    size_t reloc_count,
2717                    Output_section* output_section,
2718                    bool needs_special_offset_handling,
2719                    unsigned char* view,
2720                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2721                    section_size_type view_size,
2722                    const Reloc_symbol_changes*);
2723
2724   // Scan the relocs during a relocatable link.
2725   void
2726   scan_relocatable_relocs(Symbol_table* symtab,
2727                           Layout* layout,
2728                           Sized_relobj_file<size, big_endian>* object,
2729                           unsigned int data_shndx,
2730                           unsigned int sh_type,
2731                           const unsigned char* prelocs,
2732                           size_t reloc_count,
2733                           Output_section* output_section,
2734                           bool needs_special_offset_handling,
2735                           size_t local_symbol_count,
2736                           const unsigned char* plocal_symbols,
2737                           Relocatable_relocs*);
2738
2739   // Relocate a section during a relocatable link.
2740   void
2741   relocate_relocs(
2742       const Relocate_info<size, big_endian>*,
2743       unsigned int sh_type,
2744       const unsigned char* prelocs,
2745       size_t reloc_count,
2746       Output_section* output_section,
2747       typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
2748       const Relocatable_relocs*,
2749       unsigned char* view,
2750       typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2751       section_size_type view_size,
2752       unsigned char* reloc_view,
2753       section_size_type reloc_view_size);
2754
2755   // Return the symbol index to use for a target specific relocation.
2756   // The only target specific relocation is R_AARCH64_TLSDESC for a
2757   // local symbol, which is an absolute reloc.
2758   unsigned int
2759   do_reloc_symbol_index(void*, unsigned int r_type) const
2760   {
2761     gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
2762     return 0;
2763   }
2764
2765   // Return the addend to use for a target specific relocation.
2766   uint64_t
2767   do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
2768
2769   // Return the PLT section.
2770   uint64_t
2771   do_plt_address_for_global(const Symbol* gsym) const
2772   { return this->plt_section()->address_for_global(gsym); }
2773
2774   uint64_t
2775   do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
2776   { return this->plt_section()->address_for_local(relobj, symndx); }
2777
2778   // This function should be defined in targets that can use relocation
2779   // types to determine (implemented in local_reloc_may_be_function_pointer
2780   // and global_reloc_may_be_function_pointer)
2781   // if a function's pointer is taken.  ICF uses this in safe mode to only
2782   // fold those functions whose pointer is defintely not taken.
2783   bool
2784   do_can_check_for_function_pointers() const
2785   { return true; }
2786
2787   // Return the number of entries in the PLT.
2788   unsigned int
2789   plt_entry_count() const;
2790
2791   //Return the offset of the first non-reserved PLT entry.
2792   unsigned int
2793   first_plt_entry_offset() const;
2794
2795   // Return the size of each PLT entry.
2796   unsigned int
2797   plt_entry_size() const;
2798
2799   // Create a stub table.
2800   The_stub_table*
2801   new_stub_table(The_aarch64_input_section*);
2802
2803   // Create an aarch64 input section.
2804   The_aarch64_input_section*
2805   new_aarch64_input_section(Relobj*, unsigned int);
2806
2807   // Find an aarch64 input section instance for a given OBJ and SHNDX.
2808   The_aarch64_input_section*
2809   find_aarch64_input_section(Relobj*, unsigned int) const;
2810
2811   // Return the thread control block size.
2812   unsigned int
2813   tcb_size() const { return This::TCB_SIZE; }
2814
2815   // Scan a section for stub generation.
2816   void
2817   scan_section_for_stubs(const Relocate_info<size, big_endian>*, unsigned int,
2818                          const unsigned char*, size_t, Output_section*,
2819                          bool, const unsigned char*,
2820                          Address,
2821                          section_size_type);
2822
2823   // Scan a relocation section for stub.
2824   template<int sh_type>
2825   void
2826   scan_reloc_section_for_stubs(
2827       const The_relocate_info* relinfo,
2828       const unsigned char* prelocs,
2829       size_t reloc_count,
2830       Output_section* output_section,
2831       bool needs_special_offset_handling,
2832       const unsigned char* view,
2833       Address view_address,
2834       section_size_type);
2835
2836   // Relocate a single stub.
2837   void
2838   relocate_stub(The_reloc_stub*, const Relocate_info<size, big_endian>*,
2839                 Output_section*, unsigned char*, Address,
2840                 section_size_type);
2841
2842   // Get the default AArch64 target.
2843   static This*
2844   current_target()
2845   {
2846     gold_assert(parameters->target().machine_code() == elfcpp::EM_AARCH64
2847                 && parameters->target().get_size() == size
2848                 && parameters->target().is_big_endian() == big_endian);
2849     return static_cast<This*>(parameters->sized_target<size, big_endian>());
2850   }
2851
2852
2853   // Scan erratum 843419 for a part of a section.
2854   void
2855   scan_erratum_843419_span(
2856     AArch64_relobj<size, big_endian>*,
2857     unsigned int,
2858     const section_size_type,
2859     const section_size_type,
2860     unsigned char*,
2861     Address);
2862
2863   // Scan erratum 835769 for a part of a section.
2864   void
2865   scan_erratum_835769_span(
2866     AArch64_relobj<size, big_endian>*,
2867     unsigned int,
2868     const section_size_type,
2869     const section_size_type,
2870     unsigned char*,
2871     Address);
2872
2873  protected:
2874   void
2875   do_select_as_default_target()
2876   {
2877     gold_assert(aarch64_reloc_property_table == NULL);
2878     aarch64_reloc_property_table = new AArch64_reloc_property_table();
2879   }
2880
2881   // Add a new reloc argument, returning the index in the vector.
2882   size_t
2883   add_tlsdesc_info(Sized_relobj_file<size, big_endian>* object,
2884                    unsigned int r_sym)
2885   {
2886     this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
2887     return this->tlsdesc_reloc_info_.size() - 1;
2888   }
2889
2890   virtual Output_data_plt_aarch64<size, big_endian>*
2891   do_make_data_plt(Layout* layout,
2892                    Output_data_got_aarch64<size, big_endian>* got,
2893                    Output_data_space* got_plt,
2894                    Output_data_space* got_irelative)
2895   {
2896     return new Output_data_plt_aarch64_standard<size, big_endian>(
2897       layout, got, got_plt, got_irelative);
2898   }
2899
2900
2901   // do_make_elf_object to override the same function in the base class.
2902   Object*
2903   do_make_elf_object(const std::string&, Input_file*, off_t,
2904                      const elfcpp::Ehdr<size, big_endian>&);
2905
2906   Output_data_plt_aarch64<size, big_endian>*
2907   make_data_plt(Layout* layout,
2908                 Output_data_got_aarch64<size, big_endian>* got,
2909                 Output_data_space* got_plt,
2910                 Output_data_space* got_irelative)
2911   {
2912     return this->do_make_data_plt(layout, got, got_plt, got_irelative);
2913   }
2914
2915   // We only need to generate stubs, and hence perform relaxation if we are
2916   // not doing relocatable linking.
2917   virtual bool
2918   do_may_relax() const
2919   { return !parameters->options().relocatable(); }
2920
2921   // Relaxation hook.  This is where we do stub generation.
2922   virtual bool
2923   do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
2924
2925   void
2926   group_sections(Layout* layout,
2927                  section_size_type group_size,
2928                  bool stubs_always_after_branch,
2929                  const Task* task);
2930
2931   void
2932   scan_reloc_for_stub(const The_relocate_info*, unsigned int,
2933                       const Sized_symbol<size>*, unsigned int,
2934                       const Symbol_value<size>*,
2935                       typename elfcpp::Elf_types<size>::Elf_Swxword,
2936                       Address Elf_Addr);
2937
2938   // Make an output section.
2939   Output_section*
2940   do_make_output_section(const char* name, elfcpp::Elf_Word type,
2941                          elfcpp::Elf_Xword flags)
2942   { return new The_aarch64_output_section(name, type, flags); }
2943
2944  private:
2945   // The class which scans relocations.
2946   class Scan
2947   {
2948   public:
2949     Scan()
2950       : issued_non_pic_error_(false)
2951     { }
2952
2953     inline void
2954     local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
2955           Sized_relobj_file<size, big_endian>* object,
2956           unsigned int data_shndx,
2957           Output_section* output_section,
2958           const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
2959           const elfcpp::Sym<size, big_endian>& lsym,
2960           bool is_discarded);
2961
2962     inline void
2963     global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
2964            Sized_relobj_file<size, big_endian>* object,
2965            unsigned int data_shndx,
2966            Output_section* output_section,
2967            const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
2968            Symbol* gsym);
2969
2970     inline bool
2971     local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
2972                                         Target_aarch64<size, big_endian>* ,
2973                                         Sized_relobj_file<size, big_endian>* ,
2974                                         unsigned int ,
2975                                         Output_section* ,
2976                                         const elfcpp::Rela<size, big_endian>& ,
2977                                         unsigned int r_type,
2978                                         const elfcpp::Sym<size, big_endian>&);
2979
2980     inline bool
2981     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
2982                                          Target_aarch64<size, big_endian>* ,
2983                                          Sized_relobj_file<size, big_endian>* ,
2984                                          unsigned int ,
2985                                          Output_section* ,
2986                                          const elfcpp::Rela<size, big_endian>& ,
2987                                          unsigned int r_type,
2988                                          Symbol* gsym);
2989
2990   private:
2991     static void
2992     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
2993                             unsigned int r_type);
2994
2995     static void
2996     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
2997                              unsigned int r_type, Symbol*);
2998
2999     inline bool
3000     possible_function_pointer_reloc(unsigned int r_type);
3001
3002     void
3003     check_non_pic(Relobj*, unsigned int r_type);
3004
3005     bool
3006     reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
3007                               unsigned int r_type);
3008
3009     // Whether we have issued an error about a non-PIC compilation.
3010     bool issued_non_pic_error_;
3011   };
3012
3013   // The class which implements relocation.
3014   class Relocate
3015   {
3016    public:
3017     Relocate()
3018       : skip_call_tls_get_addr_(false)
3019     { }
3020
3021     ~Relocate()
3022     { }
3023
3024     // Do a relocation.  Return false if the caller should not issue
3025     // any warnings about this relocation.
3026     inline bool
3027     relocate(const Relocate_info<size, big_endian>*, Target_aarch64*,
3028              Output_section*,
3029              size_t relnum, const elfcpp::Rela<size, big_endian>&,
3030              unsigned int r_type, const Sized_symbol<size>*,
3031              const Symbol_value<size>*,
3032              unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
3033              section_size_type);
3034
3035   private:
3036     inline typename AArch64_relocate_functions<size, big_endian>::Status
3037     relocate_tls(const Relocate_info<size, big_endian>*,
3038                  Target_aarch64<size, big_endian>*,
3039                  size_t,
3040                  const elfcpp::Rela<size, big_endian>&,
3041                  unsigned int r_type, const Sized_symbol<size>*,
3042                  const Symbol_value<size>*,
3043                  unsigned char*,
3044                  typename elfcpp::Elf_types<size>::Elf_Addr);
3045
3046     inline typename AArch64_relocate_functions<size, big_endian>::Status
3047     tls_gd_to_le(
3048                  const Relocate_info<size, big_endian>*,
3049                  Target_aarch64<size, big_endian>*,
3050                  const elfcpp::Rela<size, big_endian>&,
3051                  unsigned int,
3052                  unsigned char*,
3053                  const Symbol_value<size>*);
3054
3055     inline typename AArch64_relocate_functions<size, big_endian>::Status
3056     tls_ld_to_le(
3057                  const Relocate_info<size, big_endian>*,
3058                  Target_aarch64<size, big_endian>*,
3059                  const elfcpp::Rela<size, big_endian>&,
3060                  unsigned int,
3061                  unsigned char*,
3062                  const Symbol_value<size>*);
3063
3064     inline typename AArch64_relocate_functions<size, big_endian>::Status
3065     tls_ie_to_le(
3066                  const Relocate_info<size, big_endian>*,
3067                  Target_aarch64<size, big_endian>*,
3068                  const elfcpp::Rela<size, big_endian>&,
3069                  unsigned int,
3070                  unsigned char*,
3071                  const Symbol_value<size>*);
3072
3073     inline typename AArch64_relocate_functions<size, big_endian>::Status
3074     tls_desc_gd_to_le(
3075                  const Relocate_info<size, big_endian>*,
3076                  Target_aarch64<size, big_endian>*,
3077                  const elfcpp::Rela<size, big_endian>&,
3078                  unsigned int,
3079                  unsigned char*,
3080                  const Symbol_value<size>*);
3081
3082     inline typename AArch64_relocate_functions<size, big_endian>::Status
3083     tls_desc_gd_to_ie(
3084                  const Relocate_info<size, big_endian>*,
3085                  Target_aarch64<size, big_endian>*,
3086                  const elfcpp::Rela<size, big_endian>&,
3087                  unsigned int,
3088                  unsigned char*,
3089                  const Symbol_value<size>*,
3090                  typename elfcpp::Elf_types<size>::Elf_Addr,
3091                  typename elfcpp::Elf_types<size>::Elf_Addr);
3092
3093     bool skip_call_tls_get_addr_;
3094
3095   };  // End of class Relocate
3096
3097   // A class which returns the size required for a relocation type,
3098   // used while scanning relocs during a relocatable link.
3099   class Relocatable_size_for_reloc
3100   {
3101    public:
3102     unsigned int
3103     get_size_for_reloc(unsigned int, Relobj*);
3104   };
3105
3106   // Adjust TLS relocation type based on the options and whether this
3107   // is a local symbol.
3108   static tls::Tls_optimization
3109   optimize_tls_reloc(bool is_final, int r_type);
3110
3111   // Get the GOT section, creating it if necessary.
3112   Output_data_got_aarch64<size, big_endian>*
3113   got_section(Symbol_table*, Layout*);
3114
3115   // Get the GOT PLT section.
3116   Output_data_space*
3117   got_plt_section() const
3118   {
3119     gold_assert(this->got_plt_ != NULL);
3120     return this->got_plt_;
3121   }
3122
3123   // Get the GOT section for TLSDESC entries.
3124   Output_data_got<size, big_endian>*
3125   got_tlsdesc_section() const
3126   {
3127     gold_assert(this->got_tlsdesc_ != NULL);
3128     return this->got_tlsdesc_;
3129   }
3130
3131   // Create the PLT section.
3132   void
3133   make_plt_section(Symbol_table* symtab, Layout* layout);
3134
3135   // Create a PLT entry for a global symbol.
3136   void
3137   make_plt_entry(Symbol_table*, Layout*, Symbol*);
3138
3139   // Create a PLT entry for a local STT_GNU_IFUNC symbol.
3140   void
3141   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
3142                              Sized_relobj_file<size, big_endian>* relobj,
3143                              unsigned int local_sym_index);
3144
3145   // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
3146   void
3147   define_tls_base_symbol(Symbol_table*, Layout*);
3148
3149   // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
3150   void
3151   reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
3152
3153   // Create a GOT entry for the TLS module index.
3154   unsigned int
3155   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
3156                       Sized_relobj_file<size, big_endian>* object);
3157
3158   // Get the PLT section.
3159   Output_data_plt_aarch64<size, big_endian>*
3160   plt_section() const
3161   {
3162     gold_assert(this->plt_ != NULL);
3163     return this->plt_;
3164   }
3165
3166   // Helper method to create erratum stubs for ST_E_843419 and ST_E_835769.
3167   void create_erratum_stub(
3168     AArch64_relobj<size, big_endian>* relobj,
3169     unsigned int shndx,
3170     section_size_type erratum_insn_offset,
3171     Address erratum_address,
3172     typename Insn_utilities::Insntype erratum_insn,
3173     int erratum_type);
3174
3175   // Return whether this is a 3-insn erratum sequence.
3176   bool is_erratum_843419_sequence(
3177       typename elfcpp::Swap<32,big_endian>::Valtype insn1,
3178       typename elfcpp::Swap<32,big_endian>::Valtype insn2,
3179       typename elfcpp::Swap<32,big_endian>::Valtype insn3);
3180
3181   // Return whether this is a 835769 sequence.
3182   // (Similarly implemented as in elfnn-aarch64.c.)
3183   bool is_erratum_835769_sequence(
3184       typename elfcpp::Swap<32,big_endian>::Valtype,
3185       typename elfcpp::Swap<32,big_endian>::Valtype);
3186
3187   // Get the dynamic reloc section, creating it if necessary.
3188   Reloc_section*
3189   rela_dyn_section(Layout*);
3190
3191   // Get the section to use for TLSDESC relocations.
3192   Reloc_section*
3193   rela_tlsdesc_section(Layout*) const;
3194
3195   // Get the section to use for IRELATIVE relocations.
3196   Reloc_section*
3197   rela_irelative_section(Layout*);
3198
3199   // Add a potential copy relocation.
3200   void
3201   copy_reloc(Symbol_table* symtab, Layout* layout,
3202              Sized_relobj_file<size, big_endian>* object,
3203              unsigned int shndx, Output_section* output_section,
3204              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
3205   {
3206     this->copy_relocs_.copy_reloc(symtab, layout,
3207                                   symtab->get_sized_symbol<size>(sym),
3208                                   object, shndx, output_section,
3209                                   reloc, this->rela_dyn_section(layout));
3210   }
3211
3212   // Information about this specific target which we pass to the
3213   // general Target structure.
3214   static const Target::Target_info aarch64_info;
3215
3216   // The types of GOT entries needed for this platform.
3217   // These values are exposed to the ABI in an incremental link.
3218   // Do not renumber existing values without changing the version
3219   // number of the .gnu_incremental_inputs section.
3220   enum Got_type
3221   {
3222     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
3223     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
3224     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
3225     GOT_TYPE_TLS_DESC = 3       // GOT entry for TLS_DESC pair
3226   };
3227
3228   // This type is used as the argument to the target specific
3229   // relocation routines.  The only target specific reloc is
3230   // R_AARCh64_TLSDESC against a local symbol.
3231   struct Tlsdesc_info
3232   {
3233     Tlsdesc_info(Sized_relobj_file<size, big_endian>* a_object,
3234                  unsigned int a_r_sym)
3235       : object(a_object), r_sym(a_r_sym)
3236     { }
3237
3238     // The object in which the local symbol is defined.
3239     Sized_relobj_file<size, big_endian>* object;
3240     // The local symbol index in the object.
3241     unsigned int r_sym;
3242   };
3243
3244   // The GOT section.
3245   Output_data_got_aarch64<size, big_endian>* got_;
3246   // The PLT section.
3247   Output_data_plt_aarch64<size, big_endian>* plt_;
3248   // The GOT PLT section.
3249   Output_data_space* got_plt_;
3250   // The GOT section for IRELATIVE relocations.
3251   Output_data_space* got_irelative_;
3252   // The GOT section for TLSDESC relocations.
3253   Output_data_got<size, big_endian>* got_tlsdesc_;
3254   // The _GLOBAL_OFFSET_TABLE_ symbol.
3255   Symbol* global_offset_table_;
3256   // The dynamic reloc section.
3257   Reloc_section* rela_dyn_;
3258   // The section to use for IRELATIVE relocs.
3259   Reloc_section* rela_irelative_;
3260   // Relocs saved to avoid a COPY reloc.
3261   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
3262   // Offset of the GOT entry for the TLS module index.
3263   unsigned int got_mod_index_offset_;
3264   // We handle R_AARCH64_TLSDESC against a local symbol as a target
3265   // specific relocation. Here we store the object and local symbol
3266   // index for the relocation.
3267   std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
3268   // True if the _TLS_MODULE_BASE_ symbol has been defined.
3269   bool tls_base_symbol_defined_;
3270   // List of stub_tables
3271   Stub_table_list stub_tables_;
3272   // Actual stub group size
3273   section_size_type stub_group_size_;
3274   AArch64_input_section_map aarch64_input_section_map_;
3275 };  // End of Target_aarch64
3276
3277
3278 template<>
3279 const Target::Target_info Target_aarch64<64, false>::aarch64_info =
3280 {
3281   64,                   // size
3282   false,                // is_big_endian
3283   elfcpp::EM_AARCH64,   // machine_code
3284   false,                // has_make_symbol
3285   false,                // has_resolve
3286   false,                // has_code_fill
3287   true,                 // is_default_stack_executable
3288   true,                 // can_icf_inline_merge_sections
3289   '\0',                 // wrap_char
3290   "/lib/ld.so.1",       // program interpreter
3291   0x400000,             // default_text_segment_address
3292   0x1000,               // abi_pagesize (overridable by -z max-page-size)
3293   0x1000,               // common_pagesize (overridable by -z common-page-size)
3294   false,                // isolate_execinstr
3295   0,                    // rosegment_gap
3296   elfcpp::SHN_UNDEF,    // small_common_shndx
3297   elfcpp::SHN_UNDEF,    // large_common_shndx
3298   0,                    // small_common_section_flags
3299   0,                    // large_common_section_flags
3300   NULL,                 // attributes_section
3301   NULL,                 // attributes_vendor
3302   "_start"              // entry_symbol_name
3303 };
3304
3305 template<>
3306 const Target::Target_info Target_aarch64<32, false>::aarch64_info =
3307 {
3308   32,                   // size
3309   false,                // is_big_endian
3310   elfcpp::EM_AARCH64,   // machine_code
3311   false,                // has_make_symbol
3312   false,                // has_resolve
3313   false,                // has_code_fill
3314   true,                 // is_default_stack_executable
3315   false,                // can_icf_inline_merge_sections
3316   '\0',                 // wrap_char
3317   "/lib/ld.so.1",       // program interpreter
3318   0x400000,             // default_text_segment_address
3319   0x1000,               // abi_pagesize (overridable by -z max-page-size)
3320   0x1000,               // common_pagesize (overridable by -z common-page-size)
3321   false,                // isolate_execinstr
3322   0,                    // rosegment_gap
3323   elfcpp::SHN_UNDEF,    // small_common_shndx
3324   elfcpp::SHN_UNDEF,    // large_common_shndx
3325   0,                    // small_common_section_flags
3326   0,                    // large_common_section_flags
3327   NULL,                 // attributes_section
3328   NULL,                 // attributes_vendor
3329   "_start"              // entry_symbol_name
3330 };
3331
3332 template<>
3333 const Target::Target_info Target_aarch64<64, true>::aarch64_info =
3334 {
3335   64,                   // size
3336   true,                 // is_big_endian
3337   elfcpp::EM_AARCH64,   // machine_code
3338   false,                // has_make_symbol
3339   false,                // has_resolve
3340   false,                // has_code_fill
3341   true,                 // is_default_stack_executable
3342   true,                 // can_icf_inline_merge_sections
3343   '\0',                 // wrap_char
3344   "/lib/ld.so.1",       // program interpreter
3345   0x400000,             // default_text_segment_address
3346   0x1000,               // abi_pagesize (overridable by -z max-page-size)
3347   0x1000,               // common_pagesize (overridable by -z common-page-size)
3348   false,                // isolate_execinstr
3349   0,                    // rosegment_gap
3350   elfcpp::SHN_UNDEF,    // small_common_shndx
3351   elfcpp::SHN_UNDEF,    // large_common_shndx
3352   0,                    // small_common_section_flags
3353   0,                    // large_common_section_flags
3354   NULL,                 // attributes_section
3355   NULL,                 // attributes_vendor
3356   "_start"              // entry_symbol_name
3357 };
3358
3359 template<>
3360 const Target::Target_info Target_aarch64<32, true>::aarch64_info =
3361 {
3362   32,                   // size
3363   true,                 // is_big_endian
3364   elfcpp::EM_AARCH64,   // machine_code
3365   false,                // has_make_symbol
3366   false,                // has_resolve
3367   false,                // has_code_fill
3368   true,                 // is_default_stack_executable
3369   false,                // can_icf_inline_merge_sections
3370   '\0',                 // wrap_char
3371   "/lib/ld.so.1",       // program interpreter
3372   0x400000,             // default_text_segment_address
3373   0x1000,               // abi_pagesize (overridable by -z max-page-size)
3374   0x1000,               // common_pagesize (overridable by -z common-page-size)
3375   false,                // isolate_execinstr
3376   0,                    // rosegment_gap
3377   elfcpp::SHN_UNDEF,    // small_common_shndx
3378   elfcpp::SHN_UNDEF,    // large_common_shndx
3379   0,                    // small_common_section_flags
3380   0,                    // large_common_section_flags
3381   NULL,                 // attributes_section
3382   NULL,                 // attributes_vendor
3383   "_start"              // entry_symbol_name
3384 };
3385
3386 // Get the GOT section, creating it if necessary.
3387
3388 template<int size, bool big_endian>
3389 Output_data_got_aarch64<size, big_endian>*
3390 Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
3391                                               Layout* layout)
3392 {
3393   if (this->got_ == NULL)
3394     {
3395       gold_assert(symtab != NULL && layout != NULL);
3396
3397       // When using -z now, we can treat .got.plt as a relro section.
3398       // Without -z now, it is modified after program startup by lazy
3399       // PLT relocations.
3400       bool is_got_plt_relro = parameters->options().now();
3401       Output_section_order got_order = (is_got_plt_relro
3402                                         ? ORDER_RELRO
3403                                         : ORDER_RELRO_LAST);
3404       Output_section_order got_plt_order = (is_got_plt_relro
3405                                             ? ORDER_RELRO
3406                                             : ORDER_NON_RELRO_FIRST);
3407
3408       // Layout of .got and .got.plt sections.
3409       // .got[0] &_DYNAMIC                          <-_GLOBAL_OFFSET_TABLE_
3410       // ...
3411       // .gotplt[0] reserved for ld.so (&linkmap)   <--DT_PLTGOT
3412       // .gotplt[1] reserved for ld.so (resolver)
3413       // .gotplt[2] reserved
3414
3415       // Generate .got section.
3416       this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
3417                                                                  layout);
3418       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3419                                       (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
3420                                       this->got_, got_order, true);
3421       // The first word of GOT is reserved for the address of .dynamic.
3422       // We put 0 here now. The value will be replaced later in
3423       // Output_data_got_aarch64::do_write.
3424       this->got_->add_constant(0);
3425
3426       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3427       // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
3428       // even if there is a .got.plt section.
3429       this->global_offset_table_ =
3430         symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3431                                       Symbol_table::PREDEFINED,
3432                                       this->got_,
3433                                       0, 0, elfcpp::STT_OBJECT,
3434                                       elfcpp::STB_LOCAL,
3435                                       elfcpp::STV_HIDDEN, 0,
3436                                       false, false);
3437
3438       // Generate .got.plt section.
3439       this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
3440       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3441                                       (elfcpp::SHF_ALLOC
3442                                        | elfcpp::SHF_WRITE),
3443                                       this->got_plt_, got_plt_order,
3444                                       is_got_plt_relro);
3445
3446       // The first three entries are reserved.
3447       this->got_plt_->set_current_data_size(
3448         AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
3449
3450       // If there are any IRELATIVE relocations, they get GOT entries
3451       // in .got.plt after the jump slot entries.
3452       this->got_irelative_ = new Output_data_space(size / 8,
3453                                                    "** GOT IRELATIVE PLT");
3454       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3455                                       (elfcpp::SHF_ALLOC
3456                                        | elfcpp::SHF_WRITE),
3457                                       this->got_irelative_,
3458                                       got_plt_order,
3459                                       is_got_plt_relro);
3460
3461       // If there are any TLSDESC relocations, they get GOT entries in
3462       // .got.plt after the jump slot and IRELATIVE entries.
3463       this->got_tlsdesc_ = new Output_data_got<size, big_endian>();
3464       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3465                                       (elfcpp::SHF_ALLOC
3466                                        | elfcpp::SHF_WRITE),
3467                                       this->got_tlsdesc_,
3468                                       got_plt_order,
3469                                       is_got_plt_relro);
3470
3471       if (!is_got_plt_relro)
3472         {
3473           // Those bytes can go into the relro segment.
3474           layout->increase_relro(
3475             AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
3476         }
3477
3478     }
3479   return this->got_;
3480 }
3481
3482 // Get the dynamic reloc section, creating it if necessary.
3483
3484 template<int size, bool big_endian>
3485 typename Target_aarch64<size, big_endian>::Reloc_section*
3486 Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
3487 {
3488   if (this->rela_dyn_ == NULL)
3489     {
3490       gold_assert(layout != NULL);
3491       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3492       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3493                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
3494                                       ORDER_DYNAMIC_RELOCS, false);
3495     }
3496   return this->rela_dyn_;
3497 }
3498
3499 // Get the section to use for IRELATIVE relocs, creating it if
3500 // necessary.  These go in .rela.dyn, but only after all other dynamic
3501 // relocations.  They need to follow the other dynamic relocations so
3502 // that they can refer to global variables initialized by those
3503 // relocs.
3504
3505 template<int size, bool big_endian>
3506 typename Target_aarch64<size, big_endian>::Reloc_section*
3507 Target_aarch64<size, big_endian>::rela_irelative_section(Layout* layout)
3508 {
3509   if (this->rela_irelative_ == NULL)
3510     {
3511       // Make sure we have already created the dynamic reloc section.
3512       this->rela_dyn_section(layout);
3513       this->rela_irelative_ = new Reloc_section(false);
3514       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3515                                       elfcpp::SHF_ALLOC, this->rela_irelative_,
3516                                       ORDER_DYNAMIC_RELOCS, false);
3517       gold_assert(this->rela_dyn_->output_section()
3518                   == this->rela_irelative_->output_section());
3519     }
3520   return this->rela_irelative_;
3521 }
3522
3523
3524 // do_make_elf_object to override the same function in the base class.  We need
3525 // to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
3526 // store backend specific information. Hence we need to have our own ELF object
3527 // creation.
3528
3529 template<int size, bool big_endian>
3530 Object*
3531 Target_aarch64<size, big_endian>::do_make_elf_object(
3532     const std::string& name,
3533     Input_file* input_file,
3534     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
3535 {
3536   int et = ehdr.get_e_type();
3537   // ET_EXEC files are valid input for --just-symbols/-R,
3538   // and we treat them as relocatable objects.
3539   if (et == elfcpp::ET_EXEC && input_file->just_symbols())
3540     return Sized_target<size, big_endian>::do_make_elf_object(
3541         name, input_file, offset, ehdr);
3542   else if (et == elfcpp::ET_REL)
3543     {
3544       AArch64_relobj<size, big_endian>* obj =
3545         new AArch64_relobj<size, big_endian>(name, input_file, offset, ehdr);
3546       obj->setup();
3547       return obj;
3548     }
3549   else if (et == elfcpp::ET_DYN)
3550     {
3551       // Keep base implementation.
3552       Sized_dynobj<size, big_endian>* obj =
3553           new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
3554       obj->setup();
3555       return obj;
3556     }
3557   else
3558     {
3559       gold_error(_("%s: unsupported ELF file type %d"),
3560                  name.c_str(), et);
3561       return NULL;
3562     }
3563 }
3564
3565
3566 // Scan a relocation for stub generation.
3567
3568 template<int size, bool big_endian>
3569 void
3570 Target_aarch64<size, big_endian>::scan_reloc_for_stub(
3571     const Relocate_info<size, big_endian>* relinfo,
3572     unsigned int r_type,
3573     const Sized_symbol<size>* gsym,
3574     unsigned int r_sym,
3575     const Symbol_value<size>* psymval,
3576     typename elfcpp::Elf_types<size>::Elf_Swxword addend,
3577     Address address)
3578 {
3579   const AArch64_relobj<size, big_endian>* aarch64_relobj =
3580       static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3581
3582   Symbol_value<size> symval;
3583   if (gsym != NULL)
3584     {
3585       const AArch64_reloc_property* arp = aarch64_reloc_property_table->
3586         get_reloc_property(r_type);
3587       if (gsym->use_plt_offset(arp->reference_flags()))
3588         {
3589           // This uses a PLT, change the symbol value.
3590           symval.set_output_value(this->plt_section()->address()
3591                                   + gsym->plt_offset());
3592           psymval = &symval;
3593         }
3594       else if (gsym->is_undefined())
3595         // There is no need to generate a stub symbol is undefined.
3596         return;
3597     }
3598
3599   // Get the symbol value.
3600   typename Symbol_value<size>::Value value = psymval->value(aarch64_relobj, 0);
3601
3602   // Owing to pipelining, the PC relative branches below actually skip
3603   // two instructions when the branch offset is 0.
3604   Address destination = static_cast<Address>(-1);
3605   switch (r_type)
3606     {
3607     case elfcpp::R_AARCH64_CALL26:
3608     case elfcpp::R_AARCH64_JUMP26:
3609       destination = value + addend;
3610       break;
3611     default:
3612       gold_unreachable();
3613     }
3614
3615   int stub_type = The_reloc_stub::
3616       stub_type_for_reloc(r_type, address, destination);
3617   if (stub_type == ST_NONE)
3618     return;
3619
3620   The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
3621   gold_assert(stub_table != NULL);
3622
3623   The_reloc_stub_key key(stub_type, gsym, aarch64_relobj, r_sym, addend);
3624   The_reloc_stub* stub = stub_table->find_reloc_stub(key);
3625   if (stub == NULL)
3626     {
3627       stub = new The_reloc_stub(stub_type);
3628       stub_table->add_reloc_stub(stub, key);
3629     }
3630   stub->set_destination_address(destination);
3631 }  // End of Target_aarch64::scan_reloc_for_stub
3632
3633
3634 // This function scans a relocation section for stub generation.
3635 // The template parameter Relocate must be a class type which provides
3636 // a single function, relocate(), which implements the machine
3637 // specific part of a relocation.
3638
3639 // BIG_ENDIAN is the endianness of the data.  SH_TYPE is the section type:
3640 // SHT_REL or SHT_RELA.
3641
3642 // PRELOCS points to the relocation data.  RELOC_COUNT is the number
3643 // of relocs.  OUTPUT_SECTION is the output section.
3644 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
3645 // mapped to output offsets.
3646
3647 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
3648 // VIEW_SIZE is the size.  These refer to the input section, unless
3649 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
3650 // the output section.
3651
3652 template<int size, bool big_endian>
3653 template<int sh_type>
3654 void inline
3655 Target_aarch64<size, big_endian>::scan_reloc_section_for_stubs(
3656     const Relocate_info<size, big_endian>* relinfo,
3657     const unsigned char* prelocs,
3658     size_t reloc_count,
3659     Output_section* /*output_section*/,
3660     bool /*needs_special_offset_handling*/,
3661     const unsigned char* /*view*/,
3662     Address view_address,
3663     section_size_type)
3664 {
3665   typedef typename Reloc_types<sh_type,size,big_endian>::Reloc Reltype;
3666
3667   const int reloc_size =
3668       Reloc_types<sh_type,size,big_endian>::reloc_size;
3669   AArch64_relobj<size, big_endian>* object =
3670       static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3671   unsigned int local_count = object->local_symbol_count();
3672
3673   gold::Default_comdat_behavior default_comdat_behavior;
3674   Comdat_behavior comdat_behavior = CB_UNDETERMINED;
3675
3676   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
3677     {
3678       Reltype reloc(prelocs);
3679       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
3680       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3681       unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
3682       if (r_type != elfcpp::R_AARCH64_CALL26
3683           && r_type != elfcpp::R_AARCH64_JUMP26)
3684         continue;
3685
3686       section_offset_type offset =
3687           convert_to_section_size_type(reloc.get_r_offset());
3688
3689       // Get the addend.
3690       typename elfcpp::Elf_types<size>::Elf_Swxword addend =
3691           reloc.get_r_addend();
3692
3693       const Sized_symbol<size>* sym;
3694       Symbol_value<size> symval;
3695       const Symbol_value<size> *psymval;
3696       bool is_defined_in_discarded_section;
3697       unsigned int shndx;
3698       if (r_sym < local_count)
3699         {
3700           sym = NULL;
3701           psymval = object->local_symbol(r_sym);
3702
3703           // If the local symbol belongs to a section we are discarding,
3704           // and that section is a debug section, try to find the
3705           // corresponding kept section and map this symbol to its
3706           // counterpart in the kept section.  The symbol must not
3707           // correspond to a section we are folding.
3708           bool is_ordinary;
3709           shndx = psymval->input_shndx(&is_ordinary);
3710           is_defined_in_discarded_section =
3711             (is_ordinary
3712              && shndx != elfcpp::SHN_UNDEF
3713              && !object->is_section_included(shndx)
3714              && !relinfo->symtab->is_section_folded(object, shndx));
3715
3716           // We need to compute the would-be final value of this local
3717           // symbol.
3718           if (!is_defined_in_discarded_section)
3719             {
3720               typedef Sized_relobj_file<size, big_endian> ObjType;
3721               typename ObjType::Compute_final_local_value_status status =
3722                 object->compute_final_local_value(r_sym, psymval, &symval,
3723                                                   relinfo->symtab);
3724               if (status == ObjType::CFLV_OK)
3725                 {
3726                   // Currently we cannot handle a branch to a target in
3727                   // a merged section.  If this is the case, issue an error
3728                   // and also free the merge symbol value.
3729                   if (!symval.has_output_value())
3730                     {
3731                       const std::string& section_name =
3732                         object->section_name(shndx);
3733                       object->error(_("cannot handle branch to local %u "
3734                                           "in a merged section %s"),
3735                                         r_sym, section_name.c_str());
3736                     }
3737                   psymval = &symval;
3738                 }
3739               else
3740                 {
3741                   // We cannot determine the final value.
3742                   continue;
3743                 }
3744             }
3745         }
3746       else
3747         {
3748           const Symbol* gsym;
3749           gsym = object->global_symbol(r_sym);
3750           gold_assert(gsym != NULL);
3751           if (gsym->is_forwarder())
3752             gsym = relinfo->symtab->resolve_forwards(gsym);
3753
3754           sym = static_cast<const Sized_symbol<size>*>(gsym);
3755           if (sym->has_symtab_index() && sym->symtab_index() != -1U)
3756             symval.set_output_symtab_index(sym->symtab_index());
3757           else
3758             symval.set_no_output_symtab_entry();
3759
3760           // We need to compute the would-be final value of this global
3761           // symbol.
3762           const Symbol_table* symtab = relinfo->symtab;
3763           const Sized_symbol<size>* sized_symbol =
3764               symtab->get_sized_symbol<size>(gsym);
3765           Symbol_table::Compute_final_value_status status;
3766           typename elfcpp::Elf_types<size>::Elf_Addr value =
3767               symtab->compute_final_value<size>(sized_symbol, &status);
3768
3769           // Skip this if the symbol has not output section.
3770           if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
3771             continue;
3772           symval.set_output_value(value);
3773
3774           if (gsym->type() == elfcpp::STT_TLS)
3775             symval.set_is_tls_symbol();
3776           else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3777             symval.set_is_ifunc_symbol();
3778           psymval = &symval;
3779
3780           is_defined_in_discarded_section =
3781               (gsym->is_defined_in_discarded_section()
3782                && gsym->is_undefined());
3783           shndx = 0;
3784         }
3785
3786       Symbol_value<size> symval2;
3787       if (is_defined_in_discarded_section)
3788         {
3789           if (comdat_behavior == CB_UNDETERMINED)
3790             {
3791               std::string name = object->section_name(relinfo->data_shndx);
3792               comdat_behavior = default_comdat_behavior.get(name.c_str());
3793             }
3794           if (comdat_behavior == CB_PRETEND)
3795             {
3796               bool found;
3797               typename elfcpp::Elf_types<size>::Elf_Addr value =
3798                 object->map_to_kept_section(shndx, &found);
3799               if (found)
3800                 symval2.set_output_value(value + psymval->input_value());
3801               else
3802                 symval2.set_output_value(0);
3803             }
3804           else
3805             {
3806               if (comdat_behavior == CB_WARNING)
3807                 gold_warning_at_location(relinfo, i, offset,
3808                                          _("relocation refers to discarded "
3809                                            "section"));
3810               symval2.set_output_value(0);
3811             }
3812           symval2.set_no_output_symtab_entry();
3813           psymval = &symval2;
3814         }
3815
3816       // If symbol is a section symbol, we don't know the actual type of
3817       // destination.  Give up.
3818       if (psymval->is_section_symbol())
3819         continue;
3820
3821       this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
3822                                 addend, view_address + offset);
3823     }  // End of iterating relocs in a section
3824 }  // End of Target_aarch64::scan_reloc_section_for_stubs
3825
3826
3827 // Scan an input section for stub generation.
3828
3829 template<int size, bool big_endian>
3830 void
3831 Target_aarch64<size, big_endian>::scan_section_for_stubs(
3832     const Relocate_info<size, big_endian>* relinfo,
3833     unsigned int sh_type,
3834     const unsigned char* prelocs,
3835     size_t reloc_count,
3836     Output_section* output_section,
3837     bool needs_special_offset_handling,
3838     const unsigned char* view,
3839     Address view_address,
3840     section_size_type view_size)
3841 {
3842   gold_assert(sh_type == elfcpp::SHT_RELA);
3843   this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
3844       relinfo,
3845       prelocs,
3846       reloc_count,
3847       output_section,
3848       needs_special_offset_handling,
3849       view,
3850       view_address,
3851       view_size);
3852 }
3853
3854
3855 // Relocate a single stub.
3856
3857 template<int size, bool big_endian>
3858 void Target_aarch64<size, big_endian>::
3859 relocate_stub(The_reloc_stub* stub,
3860               const The_relocate_info*,
3861               Output_section*,
3862               unsigned char* view,
3863               Address address,
3864               section_size_type)
3865 {
3866   typedef AArch64_relocate_functions<size, big_endian> The_reloc_functions;
3867   typedef typename The_reloc_functions::Status The_reloc_functions_status;
3868   typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
3869
3870   Insntype* ip = reinterpret_cast<Insntype*>(view);
3871   int insn_number = stub->insn_num();
3872   const uint32_t* insns = stub->insns();
3873   // Check the insns are really those stub insns.
3874   for (int i = 0; i < insn_number; ++i)
3875     {
3876       Insntype insn = elfcpp::Swap<32,big_endian>::readval(ip + i);
3877       gold_assert(((uint32_t)insn == insns[i]));
3878     }
3879
3880   Address dest = stub->destination_address();
3881
3882   switch(stub->type())
3883     {
3884     case ST_ADRP_BRANCH:
3885       {
3886         // 1st reloc is ADR_PREL_PG_HI21
3887         The_reloc_functions_status status =
3888             The_reloc_functions::adrp(view, dest, address);
3889         // An error should never arise in the above step. If so, please
3890         // check 'aarch64_valid_for_adrp_p'.
3891         gold_assert(status == The_reloc_functions::STATUS_OKAY);
3892
3893         // 2nd reloc is ADD_ABS_LO12_NC
3894         const AArch64_reloc_property* arp =
3895             aarch64_reloc_property_table->get_reloc_property(
3896                 elfcpp::R_AARCH64_ADD_ABS_LO12_NC);
3897         gold_assert(arp != NULL);
3898         status = The_reloc_functions::template
3899             rela_general<32>(view + 4, dest, 0, arp);
3900         // An error should never arise, it is an "_NC" relocation.
3901         gold_assert(status == The_reloc_functions::STATUS_OKAY);
3902       }
3903       break;
3904
3905     case ST_LONG_BRANCH_ABS:
3906       // 1st reloc is R_AARCH64_PREL64, at offset 8
3907       elfcpp::Swap<64,big_endian>::writeval(view + 8, dest);
3908       break;
3909
3910     case ST_LONG_BRANCH_PCREL:
3911       {
3912         // "PC" calculation is the 2nd insn in the stub.
3913         uint64_t offset = dest - (address + 4);
3914         // Offset is placed at offset 4 and 5.
3915         elfcpp::Swap<64,big_endian>::writeval(view + 16, offset);
3916       }
3917       break;
3918
3919     default:
3920       gold_unreachable();
3921     }
3922 }
3923
3924
3925 // A class to handle the PLT data.
3926 // This is an abstract base class that handles most of the linker details
3927 // but does not know the actual contents of PLT entries.  The derived
3928 // classes below fill in those details.
3929
3930 template<int size, bool big_endian>
3931 class Output_data_plt_aarch64 : public Output_section_data
3932 {
3933  public:
3934   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
3935       Reloc_section;
3936   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3937
3938   Output_data_plt_aarch64(Layout* layout,
3939                           uint64_t addralign,
3940                           Output_data_got_aarch64<size, big_endian>* got,
3941                           Output_data_space* got_plt,
3942                           Output_data_space* got_irelative)
3943     : Output_section_data(addralign), tlsdesc_rel_(NULL), irelative_rel_(NULL),
3944       got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
3945       count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
3946   { this->init(layout); }
3947
3948   // Initialize the PLT section.
3949   void
3950   init(Layout* layout);
3951
3952   // Add an entry to the PLT.
3953   void
3954   add_entry(Symbol_table*, Layout*, Symbol* gsym);
3955
3956   // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
3957   unsigned int
3958   add_local_ifunc_entry(Symbol_table* symtab, Layout*,
3959                         Sized_relobj_file<size, big_endian>* relobj,
3960                         unsigned int local_sym_index);
3961
3962   // Add the relocation for a PLT entry.
3963   void
3964   add_relocation(Symbol_table*, Layout*, Symbol* gsym,
3965                  unsigned int got_offset);
3966
3967   // Add the reserved TLSDESC_PLT entry to the PLT.
3968   void
3969   reserve_tlsdesc_entry(unsigned int got_offset)
3970   { this->tlsdesc_got_offset_ = got_offset; }
3971
3972   // Return true if a TLSDESC_PLT entry has been reserved.
3973   bool
3974   has_tlsdesc_entry() const
3975   { return this->tlsdesc_got_offset_ != -1U; }
3976
3977   // Return the GOT offset for the reserved TLSDESC_PLT entry.
3978   unsigned int
3979   get_tlsdesc_got_offset() const
3980   { return this->tlsdesc_got_offset_; }
3981
3982   // Return the PLT offset of the reserved TLSDESC_PLT entry.
3983   unsigned int
3984   get_tlsdesc_plt_offset() const
3985   {
3986     return (this->first_plt_entry_offset() +
3987             (this->count_ + this->irelative_count_)
3988             * this->get_plt_entry_size());
3989   }
3990
3991   // Return the .rela.plt section data.
3992   Reloc_section*
3993   rela_plt()
3994   { return this->rel_; }
3995
3996   // Return where the TLSDESC relocations should go.
3997   Reloc_section*
3998   rela_tlsdesc(Layout*);
3999
4000   // Return where the IRELATIVE relocations should go in the PLT
4001   // relocations.
4002   Reloc_section*
4003   rela_irelative(Symbol_table*, Layout*);
4004
4005   // Return whether we created a section for IRELATIVE relocations.
4006   bool
4007   has_irelative_section() const
4008   { return this->irelative_rel_ != NULL; }
4009
4010   // Return the number of PLT entries.
4011   unsigned int
4012   entry_count() const
4013   { return this->count_ + this->irelative_count_; }
4014
4015   // Return the offset of the first non-reserved PLT entry.
4016   unsigned int
4017   first_plt_entry_offset() const
4018   { return this->do_first_plt_entry_offset(); }
4019
4020   // Return the size of a PLT entry.
4021   unsigned int
4022   get_plt_entry_size() const
4023   { return this->do_get_plt_entry_size(); }
4024
4025   // Return the reserved tlsdesc entry size.
4026   unsigned int
4027   get_plt_tlsdesc_entry_size() const
4028   { return this->do_get_plt_tlsdesc_entry_size(); }
4029
4030   // Return the PLT address to use for a global symbol.
4031   uint64_t
4032   address_for_global(const Symbol*);
4033
4034   // Return the PLT address to use for a local symbol.
4035   uint64_t
4036   address_for_local(const Relobj*, unsigned int symndx);
4037
4038  protected:
4039   // Fill in the first PLT entry.
4040   void
4041   fill_first_plt_entry(unsigned char* pov,
4042                        Address got_address,
4043                        Address plt_address)
4044   { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
4045
4046   // Fill in a normal PLT entry.
4047   void
4048   fill_plt_entry(unsigned char* pov,
4049                  Address got_address,
4050                  Address plt_address,
4051                  unsigned int got_offset,
4052                  unsigned int plt_offset)
4053   {
4054     this->do_fill_plt_entry(pov, got_address, plt_address,
4055                             got_offset, plt_offset);
4056   }
4057
4058   // Fill in the reserved TLSDESC PLT entry.
4059   void
4060   fill_tlsdesc_entry(unsigned char* pov,
4061                      Address gotplt_address,
4062                      Address plt_address,
4063                      Address got_base,
4064                      unsigned int tlsdesc_got_offset,
4065                      unsigned int plt_offset)
4066   {
4067     this->do_fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
4068                                 tlsdesc_got_offset, plt_offset);
4069   }
4070
4071   virtual unsigned int
4072   do_first_plt_entry_offset() const = 0;
4073
4074   virtual unsigned int
4075   do_get_plt_entry_size() const = 0;
4076
4077   virtual unsigned int
4078   do_get_plt_tlsdesc_entry_size() const = 0;
4079
4080   virtual void
4081   do_fill_first_plt_entry(unsigned char* pov,
4082                           Address got_addr,
4083                           Address plt_addr) = 0;
4084
4085   virtual void
4086   do_fill_plt_entry(unsigned char* pov,
4087                     Address got_address,
4088                     Address plt_address,
4089                     unsigned int got_offset,
4090                     unsigned int plt_offset) = 0;
4091
4092   virtual void
4093   do_fill_tlsdesc_entry(unsigned char* pov,
4094                         Address gotplt_address,
4095                         Address plt_address,
4096                         Address got_base,
4097                         unsigned int tlsdesc_got_offset,
4098                         unsigned int plt_offset) = 0;
4099
4100   void
4101   do_adjust_output_section(Output_section* os);
4102
4103   // Write to a map file.
4104   void
4105   do_print_to_mapfile(Mapfile* mapfile) const
4106   { mapfile->print_output_data(this, _("** PLT")); }
4107
4108  private:
4109   // Set the final size.
4110   void
4111   set_final_data_size();
4112
4113   // Write out the PLT data.
4114   void
4115   do_write(Output_file*);
4116
4117   // The reloc section.
4118   Reloc_section* rel_;
4119
4120   // The TLSDESC relocs, if necessary.  These must follow the regular
4121   // PLT relocs.
4122   Reloc_section* tlsdesc_rel_;
4123
4124   // The IRELATIVE relocs, if necessary.  These must follow the
4125   // regular PLT relocations.
4126   Reloc_section* irelative_rel_;
4127
4128   // The .got section.
4129   Output_data_got_aarch64<size, big_endian>* got_;
4130
4131   // The .got.plt section.
4132   Output_data_space* got_plt_;
4133
4134   // The part of the .got.plt section used for IRELATIVE relocs.
4135   Output_data_space* got_irelative_;
4136
4137   // The number of PLT entries.
4138   unsigned int count_;
4139
4140   // Number of PLT entries with R_AARCH64_IRELATIVE relocs.  These
4141   // follow the regular PLT entries.
4142   unsigned int irelative_count_;
4143
4144   // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
4145   // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
4146   // indicates an offset is not allocated.
4147   unsigned int tlsdesc_got_offset_;
4148 };
4149
4150 // Initialize the PLT section.
4151
4152 template<int size, bool big_endian>
4153 void
4154 Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
4155 {
4156   this->rel_ = new Reloc_section(false);
4157   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4158                                   elfcpp::SHF_ALLOC, this->rel_,
4159                                   ORDER_DYNAMIC_PLT_RELOCS, false);
4160 }
4161
4162 template<int size, bool big_endian>
4163 void
4164 Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
4165     Output_section* os)
4166 {
4167   os->set_entsize(this->get_plt_entry_size());
4168 }
4169
4170 // Add an entry to the PLT.
4171
4172 template<int size, bool big_endian>
4173 void
4174 Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol_table* symtab,
4175     Layout* layout, Symbol* gsym)
4176 {
4177   gold_assert(!gsym->has_plt_offset());
4178
4179   unsigned int* pcount;
4180   unsigned int plt_reserved;
4181   Output_section_data_build* got;
4182
4183   if (gsym->type() == elfcpp::STT_GNU_IFUNC
4184       && gsym->can_use_relative_reloc(false))
4185     {
4186       pcount = &this->irelative_count_;
4187       plt_reserved = 0;
4188       got = this->got_irelative_;
4189     }
4190   else
4191     {
4192       pcount = &this->count_;
4193       plt_reserved = this->first_plt_entry_offset();
4194       got = this->got_plt_;
4195     }
4196
4197   gsym->set_plt_offset((*pcount) * this->get_plt_entry_size()
4198                        + plt_reserved);
4199
4200   ++*pcount;
4201
4202   section_offset_type got_offset = got->current_data_size();
4203
4204   // Every PLT entry needs a GOT entry which points back to the PLT
4205   // entry (this will be changed by the dynamic linker, normally
4206   // lazily when the function is called).
4207   got->set_current_data_size(got_offset + size / 8);
4208
4209   // Every PLT entry needs a reloc.
4210   this->add_relocation(symtab, layout, gsym, got_offset);
4211
4212   // Note that we don't need to save the symbol. The contents of the
4213   // PLT are independent of which symbols are used. The symbols only
4214   // appear in the relocations.
4215 }
4216
4217 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.  Return
4218 // the PLT offset.
4219
4220 template<int size, bool big_endian>
4221 unsigned int
4222 Output_data_plt_aarch64<size, big_endian>::add_local_ifunc_entry(
4223     Symbol_table* symtab,
4224     Layout* layout,
4225     Sized_relobj_file<size, big_endian>* relobj,
4226     unsigned int local_sym_index)
4227 {
4228   unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
4229   ++this->irelative_count_;
4230
4231   section_offset_type got_offset = this->got_irelative_->current_data_size();
4232
4233   // Every PLT entry needs a GOT entry which points back to the PLT
4234   // entry.
4235   this->got_irelative_->set_current_data_size(got_offset + size / 8);
4236
4237   // Every PLT entry needs a reloc.
4238   Reloc_section* rela = this->rela_irelative(symtab, layout);
4239   rela->add_symbolless_local_addend(relobj, local_sym_index,
4240                                     elfcpp::R_AARCH64_IRELATIVE,
4241                                     this->got_irelative_, got_offset, 0);
4242
4243   return plt_offset;
4244 }
4245
4246 // Add the relocation for a PLT entry.
4247
4248 template<int size, bool big_endian>
4249 void
4250 Output_data_plt_aarch64<size, big_endian>::add_relocation(
4251     Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
4252 {
4253   if (gsym->type() == elfcpp::STT_GNU_IFUNC
4254       && gsym->can_use_relative_reloc(false))
4255     {
4256       Reloc_section* rela = this->rela_irelative(symtab, layout);
4257       rela->add_symbolless_global_addend(gsym, elfcpp::R_AARCH64_IRELATIVE,
4258                                          this->got_irelative_, got_offset, 0);
4259     }
4260   else
4261     {
4262       gsym->set_needs_dynsym_entry();
4263       this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT, this->got_plt_,
4264                              got_offset, 0);
4265     }
4266 }
4267
4268 // Return where the TLSDESC relocations should go, creating it if
4269 // necessary.  These follow the JUMP_SLOT relocations.
4270
4271 template<int size, bool big_endian>
4272 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
4273 Output_data_plt_aarch64<size, big_endian>::rela_tlsdesc(Layout* layout)
4274 {
4275   if (this->tlsdesc_rel_ == NULL)
4276     {
4277       this->tlsdesc_rel_ = new Reloc_section(false);
4278       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4279                                       elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
4280                                       ORDER_DYNAMIC_PLT_RELOCS, false);
4281       gold_assert(this->tlsdesc_rel_->output_section()
4282                   == this->rel_->output_section());
4283     }
4284   return this->tlsdesc_rel_;
4285 }
4286
4287 // Return where the IRELATIVE relocations should go in the PLT.  These
4288 // follow the JUMP_SLOT and the TLSDESC relocations.
4289
4290 template<int size, bool big_endian>
4291 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
4292 Output_data_plt_aarch64<size, big_endian>::rela_irelative(Symbol_table* symtab,
4293                                                           Layout* layout)
4294 {
4295   if (this->irelative_rel_ == NULL)
4296     {
4297       // Make sure we have a place for the TLSDESC relocations, in
4298       // case we see any later on.
4299       this->rela_tlsdesc(layout);
4300       this->irelative_rel_ = new Reloc_section(false);
4301       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4302                                       elfcpp::SHF_ALLOC, this->irelative_rel_,
4303                                       ORDER_DYNAMIC_PLT_RELOCS, false);
4304       gold_assert(this->irelative_rel_->output_section()
4305                   == this->rel_->output_section());
4306
4307       if (parameters->doing_static_link())
4308         {
4309           // A statically linked executable will only have a .rela.plt
4310           // section to hold R_AARCH64_IRELATIVE relocs for
4311           // STT_GNU_IFUNC symbols.  The library will use these
4312           // symbols to locate the IRELATIVE relocs at program startup
4313           // time.
4314           symtab->define_in_output_data("__rela_iplt_start", NULL,
4315                                         Symbol_table::PREDEFINED,
4316                                         this->irelative_rel_, 0, 0,
4317                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4318                                         elfcpp::STV_HIDDEN, 0, false, true);
4319           symtab->define_in_output_data("__rela_iplt_end", NULL,
4320                                         Symbol_table::PREDEFINED,
4321                                         this->irelative_rel_, 0, 0,
4322                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4323                                         elfcpp::STV_HIDDEN, 0, true, true);
4324         }
4325     }
4326   return this->irelative_rel_;
4327 }
4328
4329 // Return the PLT address to use for a global symbol.
4330
4331 template<int size, bool big_endian>
4332 uint64_t
4333 Output_data_plt_aarch64<size, big_endian>::address_for_global(
4334   const Symbol* gsym)
4335 {
4336   uint64_t offset = 0;
4337   if (gsym->type() == elfcpp::STT_GNU_IFUNC
4338       && gsym->can_use_relative_reloc(false))
4339     offset = (this->first_plt_entry_offset() +
4340               this->count_ * this->get_plt_entry_size());
4341   return this->address() + offset + gsym->plt_offset();
4342 }
4343
4344 // Return the PLT address to use for a local symbol.  These are always
4345 // IRELATIVE relocs.
4346
4347 template<int size, bool big_endian>
4348 uint64_t
4349 Output_data_plt_aarch64<size, big_endian>::address_for_local(
4350     const Relobj* object,
4351     unsigned int r_sym)
4352 {
4353   return (this->address()
4354           + this->first_plt_entry_offset()
4355           + this->count_ * this->get_plt_entry_size()
4356           + object->local_plt_offset(r_sym));
4357 }
4358
4359 // Set the final size.
4360
4361 template<int size, bool big_endian>
4362 void
4363 Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
4364 {
4365   unsigned int count = this->count_ + this->irelative_count_;
4366   unsigned int extra_size = 0;
4367   if (this->has_tlsdesc_entry())
4368     extra_size += this->get_plt_tlsdesc_entry_size();
4369   this->set_data_size(this->first_plt_entry_offset()
4370                       + count * this->get_plt_entry_size()
4371                       + extra_size);
4372 }
4373
4374 template<int size, bool big_endian>
4375 class Output_data_plt_aarch64_standard :
4376   public Output_data_plt_aarch64<size, big_endian>
4377 {
4378  public:
4379   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4380   Output_data_plt_aarch64_standard(
4381       Layout* layout,
4382       Output_data_got_aarch64<size, big_endian>* got,
4383       Output_data_space* got_plt,
4384       Output_data_space* got_irelative)
4385     : Output_data_plt_aarch64<size, big_endian>(layout,
4386                                                 size == 32 ? 4 : 8,
4387                                                 got, got_plt,
4388                                                 got_irelative)
4389   { }
4390
4391  protected:
4392   // Return the offset of the first non-reserved PLT entry.
4393   virtual unsigned int
4394   do_first_plt_entry_offset() const
4395   { return this->first_plt_entry_size; }
4396
4397   // Return the size of a PLT entry
4398   virtual unsigned int
4399   do_get_plt_entry_size() const
4400   { return this->plt_entry_size; }
4401
4402   // Return the size of a tlsdesc entry
4403   virtual unsigned int
4404   do_get_plt_tlsdesc_entry_size() const
4405   { return this->plt_tlsdesc_entry_size; }
4406
4407   virtual void
4408   do_fill_first_plt_entry(unsigned char* pov,
4409                           Address got_address,
4410                           Address plt_address);
4411
4412   virtual void
4413   do_fill_plt_entry(unsigned char* pov,
4414                     Address got_address,
4415                     Address plt_address,
4416                     unsigned int got_offset,
4417                     unsigned int plt_offset);
4418
4419   virtual void
4420   do_fill_tlsdesc_entry(unsigned char* pov,
4421                         Address gotplt_address,
4422                         Address plt_address,
4423                         Address got_base,
4424                         unsigned int tlsdesc_got_offset,
4425                         unsigned int plt_offset);
4426
4427  private:
4428   // The size of the first plt entry size.
4429   static const int first_plt_entry_size = 32;
4430   // The size of the plt entry size.
4431   static const int plt_entry_size = 16;
4432   // The size of the plt tlsdesc entry size.
4433   static const int plt_tlsdesc_entry_size = 32;
4434   // Template for the first PLT entry.
4435   static const uint32_t first_plt_entry[first_plt_entry_size / 4];
4436   // Template for subsequent PLT entries.
4437   static const uint32_t plt_entry[plt_entry_size / 4];
4438   // The reserved TLSDESC entry in the PLT for an executable.
4439   static const uint32_t tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4];
4440 };
4441
4442 // The first entry in the PLT for an executable.
4443
4444 template<>
4445 const uint32_t
4446 Output_data_plt_aarch64_standard<32, false>::
4447     first_plt_entry[first_plt_entry_size / 4] =
4448 {
4449   0xa9bf7bf0,   /* stp x16, x30, [sp, #-16]!  */
4450   0x90000010,   /* adrp x16, PLT_GOT+0x8  */
4451   0xb9400A11,   /* ldr w17, [x16, #PLT_GOT+0x8]  */
4452   0x11002210,   /* add w16, w16,#PLT_GOT+0x8   */
4453   0xd61f0220,   /* br x17  */
4454   0xd503201f,   /* nop */
4455   0xd503201f,   /* nop */
4456   0xd503201f,   /* nop */
4457 };
4458
4459
4460 template<>
4461 const uint32_t
4462 Output_data_plt_aarch64_standard<32, true>::
4463     first_plt_entry[first_plt_entry_size / 4] =
4464 {
4465   0xa9bf7bf0,   /* stp x16, x30, [sp, #-16]!  */
4466   0x90000010,   /* adrp x16, PLT_GOT+0x8  */
4467   0xb9400A11,   /* ldr w17, [x16, #PLT_GOT+0x8]  */
4468   0x11002210,   /* add w16, w16,#PLT_GOT+0x8   */
4469   0xd61f0220,   /* br x17  */
4470   0xd503201f,   /* nop */
4471   0xd503201f,   /* nop */
4472   0xd503201f,   /* nop */
4473 };
4474
4475
4476 template<>
4477 const uint32_t
4478 Output_data_plt_aarch64_standard<64, false>::
4479     first_plt_entry[first_plt_entry_size / 4] =
4480 {
4481   0xa9bf7bf0,   /* stp x16, x30, [sp, #-16]!  */
4482   0x90000010,   /* adrp x16, PLT_GOT+16  */
4483   0xf9400A11,   /* ldr x17, [x16, #PLT_GOT+0x10]  */
4484   0x91004210,   /* add x16, x16,#PLT_GOT+0x10   */
4485   0xd61f0220,   /* br x17  */
4486   0xd503201f,   /* nop */
4487   0xd503201f,   /* nop */
4488   0xd503201f,   /* nop */
4489 };
4490
4491
4492 template<>
4493 const uint32_t
4494 Output_data_plt_aarch64_standard<64, true>::
4495     first_plt_entry[first_plt_entry_size / 4] =
4496 {
4497   0xa9bf7bf0,   /* stp x16, x30, [sp, #-16]!  */
4498   0x90000010,   /* adrp x16, PLT_GOT+16  */
4499   0xf9400A11,   /* ldr x17, [x16, #PLT_GOT+0x10]  */
4500   0x91004210,   /* add x16, x16,#PLT_GOT+0x10   */
4501   0xd61f0220,   /* br x17  */
4502   0xd503201f,   /* nop */
4503   0xd503201f,   /* nop */
4504   0xd503201f,   /* nop */
4505 };
4506
4507
4508 template<>
4509 const uint32_t
4510 Output_data_plt_aarch64_standard<32, false>::
4511     plt_entry[plt_entry_size / 4] =
4512 {
4513   0x90000010,   /* adrp x16, PLTGOT + n * 4  */
4514   0xb9400211,   /* ldr w17, [w16, PLTGOT + n * 4] */
4515   0x11000210,   /* add w16, w16, :lo12:PLTGOT + n * 4  */
4516   0xd61f0220,   /* br x17.  */
4517 };
4518
4519
4520 template<>
4521 const uint32_t
4522 Output_data_plt_aarch64_standard<32, true>::
4523     plt_entry[plt_entry_size / 4] =
4524 {
4525   0x90000010,   /* adrp x16, PLTGOT + n * 4  */
4526   0xb9400211,   /* ldr w17, [w16, PLTGOT + n * 4] */
4527   0x11000210,   /* add w16, w16, :lo12:PLTGOT + n * 4  */
4528   0xd61f0220,   /* br x17.  */
4529 };
4530
4531
4532 template<>
4533 const uint32_t
4534 Output_data_plt_aarch64_standard<64, false>::
4535     plt_entry[plt_entry_size / 4] =
4536 {
4537   0x90000010,   /* adrp x16, PLTGOT + n * 8  */
4538   0xf9400211,   /* ldr x17, [x16, PLTGOT + n * 8] */
4539   0x91000210,   /* add x16, x16, :lo12:PLTGOT + n * 8  */
4540   0xd61f0220,   /* br x17.  */
4541 };
4542
4543
4544 template<>
4545 const uint32_t
4546 Output_data_plt_aarch64_standard<64, true>::
4547     plt_entry[plt_entry_size / 4] =
4548 {
4549   0x90000010,   /* adrp x16, PLTGOT + n * 8  */
4550   0xf9400211,   /* ldr x17, [x16, PLTGOT + n * 8] */
4551   0x91000210,   /* add x16, x16, :lo12:PLTGOT + n * 8  */
4552   0xd61f0220,   /* br x17.  */
4553 };
4554
4555
4556 template<int size, bool big_endian>
4557 void
4558 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
4559     unsigned char* pov,
4560     Address got_address,
4561     Address plt_address)
4562 {
4563   // PLT0 of the small PLT looks like this in ELF64 -
4564   // stp x16, x30, [sp, #-16]!          Save the reloc and lr on stack.
4565   // adrp x16, PLT_GOT + 16             Get the page base of the GOTPLT
4566   // ldr  x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
4567   //                                    symbol resolver
4568   // add  x16, x16, #:lo12:PLT_GOT+16   Load the lo12 bits of the
4569   //                                    GOTPLT entry for this.
4570   // br   x17
4571   // PLT0 will be slightly different in ELF32 due to different got entry
4572   // size.
4573   memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
4574   Address gotplt_2nd_ent = got_address + (size / 8) * 2;
4575
4576   // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
4577   // ADRP:  (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
4578   // FIXME: This only works for 64bit
4579   AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
4580       gotplt_2nd_ent, plt_address + 4);
4581
4582   // Fill in R_AARCH64_LDST8_LO12
4583   elfcpp::Swap<32, big_endian>::writeval(
4584       pov + 8,
4585       ((this->first_plt_entry[2] & 0xffc003ff)
4586        | ((gotplt_2nd_ent & 0xff8) << 7)));
4587
4588   // Fill in R_AARCH64_ADD_ABS_LO12
4589   elfcpp::Swap<32, big_endian>::writeval(
4590       pov + 12,
4591       ((this->first_plt_entry[3] & 0xffc003ff)
4592        | ((gotplt_2nd_ent & 0xfff) << 10)));
4593 }
4594
4595
4596 // Subsequent entries in the PLT for an executable.
4597 // FIXME: This only works for 64bit
4598
4599 template<int size, bool big_endian>
4600 void
4601 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
4602     unsigned char* pov,
4603     Address got_address,
4604     Address plt_address,
4605     unsigned int got_offset,
4606     unsigned int plt_offset)
4607 {
4608   memcpy(pov, this->plt_entry, this->plt_entry_size);
4609
4610   Address gotplt_entry_address = got_address + got_offset;
4611   Address plt_entry_address = plt_address + plt_offset;
4612
4613   // Fill in R_AARCH64_PCREL_ADR_HI21
4614   AArch64_relocate_functions<size, big_endian>::adrp(
4615       pov,
4616       gotplt_entry_address,
4617       plt_entry_address);
4618
4619   // Fill in R_AARCH64_LDST64_ABS_LO12
4620   elfcpp::Swap<32, big_endian>::writeval(
4621       pov + 4,
4622       ((this->plt_entry[1] & 0xffc003ff)
4623        | ((gotplt_entry_address & 0xff8) << 7)));
4624
4625   // Fill in R_AARCH64_ADD_ABS_LO12
4626   elfcpp::Swap<32, big_endian>::writeval(
4627       pov + 8,
4628       ((this->plt_entry[2] & 0xffc003ff)
4629        | ((gotplt_entry_address & 0xfff) <<10)));
4630
4631 }
4632
4633
4634 template<>
4635 const uint32_t
4636 Output_data_plt_aarch64_standard<32, false>::
4637     tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4638 {
4639   0xa9bf0fe2,   /* stp x2, x3, [sp, #-16]!  */
4640   0x90000002,   /* adrp x2, 0 */
4641   0x90000003,   /* adrp x3, 0 */
4642   0xb9400042,   /* ldr w2, [w2, #0] */
4643   0x11000063,   /* add w3, w3, 0 */
4644   0xd61f0040,   /* br x2 */
4645   0xd503201f,   /* nop */
4646   0xd503201f,   /* nop */
4647 };
4648
4649 template<>
4650 const uint32_t
4651 Output_data_plt_aarch64_standard<32, true>::
4652     tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4653 {
4654   0xa9bf0fe2,   /* stp x2, x3, [sp, #-16]!  */
4655   0x90000002,   /* adrp x2, 0 */
4656   0x90000003,   /* adrp x3, 0 */
4657   0xb9400042,   /* ldr w2, [w2, #0] */
4658   0x11000063,   /* add w3, w3, 0 */
4659   0xd61f0040,   /* br x2 */
4660   0xd503201f,   /* nop */
4661   0xd503201f,   /* nop */
4662 };
4663
4664 template<>
4665 const uint32_t
4666 Output_data_plt_aarch64_standard<64, false>::
4667     tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4668 {
4669   0xa9bf0fe2,   /* stp x2, x3, [sp, #-16]!  */
4670   0x90000002,   /* adrp x2, 0 */
4671   0x90000003,   /* adrp x3, 0 */
4672   0xf9400042,   /* ldr x2, [x2, #0] */
4673   0x91000063,   /* add x3, x3, 0 */
4674   0xd61f0040,   /* br x2 */
4675   0xd503201f,   /* nop */
4676   0xd503201f,   /* nop */
4677 };
4678
4679 template<>
4680 const uint32_t
4681 Output_data_plt_aarch64_standard<64, true>::
4682     tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4683 {
4684   0xa9bf0fe2,   /* stp x2, x3, [sp, #-16]!  */
4685   0x90000002,   /* adrp x2, 0 */
4686   0x90000003,   /* adrp x3, 0 */
4687   0xf9400042,   /* ldr x2, [x2, #0] */
4688   0x91000063,   /* add x3, x3, 0 */
4689   0xd61f0040,   /* br x2 */
4690   0xd503201f,   /* nop */
4691   0xd503201f,   /* nop */
4692 };
4693
4694 template<int size, bool big_endian>
4695 void
4696 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_tlsdesc_entry(
4697     unsigned char* pov,
4698     Address gotplt_address,
4699     Address plt_address,
4700     Address got_base,
4701     unsigned int tlsdesc_got_offset,
4702     unsigned int plt_offset)
4703 {
4704   memcpy(pov, tlsdesc_plt_entry, plt_tlsdesc_entry_size);
4705
4706   // move DT_TLSDESC_GOT address into x2
4707   // move .got.plt address into x3
4708   Address tlsdesc_got_entry = got_base + tlsdesc_got_offset;
4709   Address plt_entry_address = plt_address + plt_offset;
4710
4711   // R_AARCH64_ADR_PREL_PG_HI21
4712   AArch64_relocate_functions<size, big_endian>::adrp(
4713       pov + 4,
4714       tlsdesc_got_entry,
4715       plt_entry_address + 4);
4716
4717   // R_AARCH64_ADR_PREL_PG_HI21
4718   AArch64_relocate_functions<size, big_endian>::adrp(
4719       pov + 8,
4720       gotplt_address,
4721       plt_entry_address + 8);
4722
4723   // R_AARCH64_LDST64_ABS_LO12
4724   elfcpp::Swap<32, big_endian>::writeval(
4725       pov + 12,
4726       ((this->tlsdesc_plt_entry[3] & 0xffc003ff)
4727        | ((tlsdesc_got_entry & 0xff8) << 7)));
4728
4729   // R_AARCH64_ADD_ABS_LO12
4730   elfcpp::Swap<32, big_endian>::writeval(
4731       pov + 16,
4732       ((this->tlsdesc_plt_entry[4] & 0xffc003ff)
4733        | ((gotplt_address & 0xfff) << 10)));
4734 }
4735
4736 // Write out the PLT.  This uses the hand-coded instructions above,
4737 // and adjusts them as needed.  This is specified by the AMD64 ABI.
4738
4739 template<int size, bool big_endian>
4740 void
4741 Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
4742 {
4743   const off_t offset = this->offset();
4744   const section_size_type oview_size =
4745     convert_to_section_size_type(this->data_size());
4746   unsigned char* const oview = of->get_output_view(offset, oview_size);
4747
4748   const off_t got_file_offset = this->got_plt_->offset();
4749   gold_assert(got_file_offset + this->got_plt_->data_size()
4750               == this->got_irelative_->offset());
4751
4752   const section_size_type got_size =
4753       convert_to_section_size_type(this->got_plt_->data_size()
4754                                    + this->got_irelative_->data_size());
4755   unsigned char* const got_view = of->get_output_view(got_file_offset,
4756                                                       got_size);
4757
4758   unsigned char* pov = oview;
4759
4760   // The base address of the .plt section.
4761   typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
4762   // The base address of the PLT portion of the .got section.
4763   typename elfcpp::Elf_types<size>::Elf_Addr gotplt_address
4764       = this->got_plt_->address();
4765
4766   this->fill_first_plt_entry(pov, gotplt_address, plt_address);
4767   pov += this->first_plt_entry_offset();
4768
4769   // The first three entries in .got.plt are reserved.
4770   unsigned char* got_pov = got_view;
4771   memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
4772   got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
4773
4774   unsigned int plt_offset = this->first_plt_entry_offset();
4775   unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
4776   const unsigned int count = this->count_ + this->irelative_count_;
4777   for (unsigned int plt_index = 0;
4778        plt_index < count;
4779        ++plt_index,
4780          pov += this->get_plt_entry_size(),
4781          got_pov += size / 8,
4782          plt_offset += this->get_plt_entry_size(),
4783          got_offset += size / 8)
4784     {
4785       // Set and adjust the PLT entry itself.
4786       this->fill_plt_entry(pov, gotplt_address, plt_address,
4787                            got_offset, plt_offset);
4788
4789       // Set the entry in the GOT, which points to plt0.
4790       elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
4791     }
4792
4793   if (this->has_tlsdesc_entry())
4794     {
4795       // Set and adjust the reserved TLSDESC PLT entry.
4796       unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
4797       // The base address of the .base section.
4798       typename elfcpp::Elf_types<size>::Elf_Addr got_base =
4799           this->got_->address();
4800       this->fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
4801                                tlsdesc_got_offset, plt_offset);
4802       pov += this->get_plt_tlsdesc_entry_size();
4803     }
4804
4805   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4806   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4807
4808   of->write_output_view(offset, oview_size, oview);
4809   of->write_output_view(got_file_offset, got_size, got_view);
4810 }
4811
4812 // Telling how to update the immediate field of an instruction.
4813 struct AArch64_howto
4814 {
4815   // The immediate field mask.
4816   elfcpp::Elf_Xword dst_mask;
4817
4818   // The offset to apply relocation immediate
4819   int doffset;
4820
4821   // The second part offset, if the immediate field has two parts.
4822   // -1 if the immediate field has only one part.
4823   int doffset2;
4824 };
4825
4826 static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
4827 {
4828   {0, -1, -1},          // DATA
4829   {0x1fffe0, 5, -1},    // MOVW  [20:5]-imm16
4830   {0xffffe0, 5, -1},    // LD    [23:5]-imm19
4831   {0x60ffffe0, 29, 5},  // ADR   [30:29]-immlo  [23:5]-immhi
4832   {0x60ffffe0, 29, 5},  // ADRP  [30:29]-immlo  [23:5]-immhi
4833   {0x3ffc00, 10, -1},   // ADD   [21:10]-imm12
4834   {0x3ffc00, 10, -1},   // LDST  [21:10]-imm12
4835   {0x7ffe0, 5, -1},     // TBZNZ [18:5]-imm14
4836   {0xffffe0, 5, -1},    // CONDB [23:5]-imm19
4837   {0x3ffffff, 0, -1},   // B     [25:0]-imm26
4838   {0x3ffffff, 0, -1},   // CALL  [25:0]-imm26
4839 };
4840
4841 // AArch64 relocate function class
4842
4843 template<int size, bool big_endian>
4844 class AArch64_relocate_functions
4845 {
4846  public:
4847   typedef enum
4848   {
4849     STATUS_OKAY,        // No error during relocation.
4850     STATUS_OVERFLOW,    // Relocation overflow.
4851     STATUS_BAD_RELOC,   // Relocation cannot be applied.
4852   } Status;
4853
4854   typedef AArch64_relocate_functions<size, big_endian> This;
4855   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4856   typedef Relocate_info<size, big_endian> The_relocate_info;
4857   typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
4858   typedef Reloc_stub<size, big_endian> The_reloc_stub;
4859   typedef Stub_table<size, big_endian> The_stub_table;
4860   typedef elfcpp::Rela<size, big_endian> The_rela;
4861   typedef typename elfcpp::Swap<size, big_endian>::Valtype AArch64_valtype;
4862
4863   // Return the page address of the address.
4864   // Page(address) = address & ~0xFFF
4865
4866   static inline AArch64_valtype
4867   Page(Address address)
4868   {
4869     return (address & (~static_cast<Address>(0xFFF)));
4870   }
4871
4872  private:
4873   // Update instruction (pointed by view) with selected bits (immed).
4874   // val = (val & ~dst_mask) | (immed << doffset)
4875
4876   template<int valsize>
4877   static inline void
4878   update_view(unsigned char* view,
4879               AArch64_valtype immed,
4880               elfcpp::Elf_Xword doffset,
4881               elfcpp::Elf_Xword dst_mask)
4882   {
4883     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4884     Valtype* wv = reinterpret_cast<Valtype*>(view);
4885     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
4886
4887     // Clear immediate fields.
4888     val &= ~dst_mask;
4889     elfcpp::Swap<valsize, big_endian>::writeval(wv,
4890       static_cast<Valtype>(val | (immed << doffset)));
4891   }
4892
4893   // Update two parts of an instruction (pointed by view) with selected
4894   // bits (immed1 and immed2).
4895   // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
4896
4897   template<int valsize>
4898   static inline void
4899   update_view_two_parts(
4900     unsigned char* view,
4901     AArch64_valtype immed1,
4902     AArch64_valtype immed2,
4903     elfcpp::Elf_Xword doffset1,
4904     elfcpp::Elf_Xword doffset2,
4905     elfcpp::Elf_Xword dst_mask)
4906   {
4907     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4908     Valtype* wv = reinterpret_cast<Valtype*>(view);
4909     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
4910     val &= ~dst_mask;
4911     elfcpp::Swap<valsize, big_endian>::writeval(wv,
4912       static_cast<Valtype>(val | (immed1 << doffset1) |
4913                            (immed2 << doffset2)));
4914   }
4915
4916   // Update adr or adrp instruction with immed.
4917   // In adr and adrp: [30:29] immlo   [23:5] immhi
4918
4919   static inline void
4920   update_adr(unsigned char* view, AArch64_valtype immed)
4921   {
4922     elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
4923     This::template update_view_two_parts<32>(
4924       view,
4925       immed & 0x3,
4926       (immed & 0x1ffffc) >> 2,
4927       29,
4928       5,
4929       dst_mask);
4930   }
4931
4932   // Update movz/movn instruction with bits immed.
4933   // Set instruction to movz if is_movz is true, otherwise set instruction
4934   // to movn.
4935
4936   static inline void
4937   update_movnz(unsigned char* view,
4938                AArch64_valtype immed,
4939                bool is_movz)
4940   {
4941     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4942     Valtype* wv = reinterpret_cast<Valtype*>(view);
4943     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4944
4945     const elfcpp::Elf_Xword doffset =
4946         aarch64_howto[AArch64_reloc_property::INST_MOVW].doffset;
4947     const elfcpp::Elf_Xword dst_mask =
4948         aarch64_howto[AArch64_reloc_property::INST_MOVW].dst_mask;
4949
4950     // Clear immediate fields and opc code.
4951     val &= ~(dst_mask | (0x3 << 29));
4952
4953     // Set instruction to movz or movn.
4954     // movz: [30:29] is 10   movn: [30:29] is 00
4955     if (is_movz)
4956       val |= (0x2 << 29);
4957
4958     elfcpp::Swap<32, big_endian>::writeval(wv,
4959       static_cast<Valtype>(val | (immed << doffset)));
4960   }
4961
4962   // Update selected bits in text.
4963
4964   template<int valsize>
4965   static inline typename This::Status
4966   reloc_common(unsigned char* view, Address x,
4967                 const AArch64_reloc_property* reloc_property)
4968   {
4969     // Select bits from X.
4970     Address immed = reloc_property->select_x_value(x);
4971
4972     // Update view.
4973     const AArch64_reloc_property::Reloc_inst inst =
4974       reloc_property->reloc_inst();
4975     // If it is a data relocation or instruction has 2 parts of immediate
4976     // fields, you should not call pcrela_general.
4977     gold_assert(aarch64_howto[inst].doffset2 == -1 &&
4978                 aarch64_howto[inst].doffset != -1);
4979     This::template update_view<valsize>(view, immed,
4980                                         aarch64_howto[inst].doffset,
4981                                         aarch64_howto[inst].dst_mask);
4982
4983     // Do check overflow or alignment if needed.
4984     return (reloc_property->checkup_x_value(x)
4985             ? This::STATUS_OKAY
4986             : This::STATUS_OVERFLOW);
4987   }
4988
4989  public:
4990
4991   // Construct a B insn. Note, although we group it here with other relocation
4992   // operation, there is actually no 'relocation' involved here.
4993   static inline void
4994   construct_b(unsigned char* view, unsigned int branch_offset)
4995   {
4996     update_view_two_parts<32>(view, 0x05, (branch_offset >> 2),
4997                               26, 0, 0xffffffff);
4998   }
4999
5000   // Do a simple rela relocation at unaligned addresses.
5001
5002   template<int valsize>
5003   static inline typename This::Status
5004   rela_ua(unsigned char* view,
5005           const Sized_relobj_file<size, big_endian>* object,
5006           const Symbol_value<size>* psymval,
5007           AArch64_valtype addend,
5008           const AArch64_reloc_property* reloc_property)
5009   {
5010     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
5011       Valtype;
5012     typename elfcpp::Elf_types<size>::Elf_Addr x =
5013         psymval->value(object, addend);
5014     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
5015       static_cast<Valtype>(x));
5016     return (reloc_property->checkup_x_value(x)
5017             ? This::STATUS_OKAY
5018             : This::STATUS_OVERFLOW);
5019   }
5020
5021   // Do a simple pc-relative relocation at unaligned addresses.
5022
5023   template<int valsize>
5024   static inline typename This::Status
5025   pcrela_ua(unsigned char* view,
5026             const Sized_relobj_file<size, big_endian>* object,
5027             const Symbol_value<size>* psymval,
5028             AArch64_valtype addend,
5029             Address address,
5030             const AArch64_reloc_property* reloc_property)
5031   {
5032     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
5033       Valtype;
5034     Address x = psymval->value(object, addend) - address;
5035     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
5036       static_cast<Valtype>(x));
5037     return (reloc_property->checkup_x_value(x)
5038             ? This::STATUS_OKAY
5039             : This::STATUS_OVERFLOW);
5040   }
5041
5042   // Do a simple rela relocation at aligned addresses.
5043
5044   template<int valsize>
5045   static inline typename This::Status
5046   rela(
5047     unsigned char* view,
5048     const Sized_relobj_file<size, big_endian>* object,
5049     const Symbol_value<size>* psymval,
5050     AArch64_valtype addend,
5051     const AArch64_reloc_property* reloc_property)
5052   {
5053     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
5054     Valtype* wv = reinterpret_cast<Valtype*>(view);
5055     Address x = psymval->value(object, addend);
5056     elfcpp::Swap<valsize, big_endian>::writeval(wv,static_cast<Valtype>(x));
5057     return (reloc_property->checkup_x_value(x)
5058             ? This::STATUS_OKAY
5059             : This::STATUS_OVERFLOW);
5060   }
5061
5062   // Do relocate. Update selected bits in text.
5063   // new_val = (val & ~dst_mask) | (immed << doffset)
5064
5065   template<int valsize>
5066   static inline typename This::Status
5067   rela_general(unsigned char* view,
5068                const Sized_relobj_file<size, big_endian>* object,
5069                const Symbol_value<size>* psymval,
5070                AArch64_valtype addend,
5071                const AArch64_reloc_property* reloc_property)
5072   {
5073     // Calculate relocation.
5074     Address x = psymval->value(object, addend);
5075     return This::template reloc_common<valsize>(view, x, reloc_property);
5076   }
5077
5078   // Do relocate. Update selected bits in text.
5079   // new val = (val & ~dst_mask) | (immed << doffset)
5080
5081   template<int valsize>
5082   static inline typename This::Status
5083   rela_general(
5084     unsigned char* view,
5085     AArch64_valtype s,
5086     AArch64_valtype addend,
5087     const AArch64_reloc_property* reloc_property)
5088   {
5089     // Calculate relocation.
5090     Address x = s + addend;
5091     return This::template reloc_common<valsize>(view, x, reloc_property);
5092   }
5093
5094   // Do address relative relocate. Update selected bits in text.
5095   // new val = (val & ~dst_mask) | (immed << doffset)
5096
5097   template<int valsize>
5098   static inline typename This::Status
5099   pcrela_general(
5100     unsigned char* view,
5101     const Sized_relobj_file<size, big_endian>* object,
5102     const Symbol_value<size>* psymval,
5103     AArch64_valtype addend,
5104     Address address,
5105     const AArch64_reloc_property* reloc_property)
5106   {
5107     // Calculate relocation.
5108     Address x = psymval->value(object, addend) - address;
5109     return This::template reloc_common<valsize>(view, x, reloc_property);
5110   }
5111
5112
5113   // Calculate (S + A) - address, update adr instruction.
5114
5115   static inline typename This::Status
5116   adr(unsigned char* view,
5117       const Sized_relobj_file<size, big_endian>* object,
5118       const Symbol_value<size>* psymval,
5119       Address addend,
5120       Address address,
5121       const AArch64_reloc_property* /* reloc_property */)
5122   {
5123     AArch64_valtype x = psymval->value(object, addend) - address;
5124     // Pick bits [20:0] of X.
5125     AArch64_valtype immed = x & 0x1fffff;
5126     update_adr(view, immed);
5127     // Check -2^20 <= X < 2^20
5128     return (size == 64 && Bits<21>::has_overflow((x))
5129             ? This::STATUS_OVERFLOW
5130             : This::STATUS_OKAY);
5131   }
5132
5133   // Calculate PG(S+A) - PG(address), update adrp instruction.
5134   // R_AARCH64_ADR_PREL_PG_HI21
5135
5136   static inline typename This::Status
5137   adrp(
5138     unsigned char* view,
5139     Address sa,
5140     Address address)
5141   {
5142     AArch64_valtype x = This::Page(sa) - This::Page(address);
5143     // Pick [32:12] of X.
5144     AArch64_valtype immed = (x >> 12) & 0x1fffff;
5145     update_adr(view, immed);
5146     // Check -2^32 <= X < 2^32
5147     return (size == 64 && Bits<33>::has_overflow((x))
5148             ? This::STATUS_OVERFLOW
5149             : This::STATUS_OKAY);
5150   }
5151
5152   // Calculate PG(S+A) - PG(address), update adrp instruction.
5153   // R_AARCH64_ADR_PREL_PG_HI21
5154
5155   static inline typename This::Status
5156   adrp(unsigned char* view,
5157        const Sized_relobj_file<size, big_endian>* object,
5158        const Symbol_value<size>* psymval,
5159        Address addend,
5160        Address address,
5161        const AArch64_reloc_property* reloc_property)
5162   {
5163     Address sa = psymval->value(object, addend);
5164     AArch64_valtype x = This::Page(sa) - This::Page(address);
5165     // Pick [32:12] of X.
5166     AArch64_valtype immed = (x >> 12) & 0x1fffff;
5167     update_adr(view, immed);
5168     return (reloc_property->checkup_x_value(x)
5169             ? This::STATUS_OKAY
5170             : This::STATUS_OVERFLOW);
5171   }
5172
5173   // Update mov[n/z] instruction. Check overflow if needed.
5174   // If X >=0, set the instruction to movz and its immediate value to the
5175   // selected bits S.
5176   // If X < 0, set the instruction to movn and its immediate value to
5177   // NOT (selected bits of).
5178
5179   static inline typename This::Status
5180   movnz(unsigned char* view,
5181         AArch64_valtype x,
5182         const AArch64_reloc_property* reloc_property)
5183   {
5184     // Select bits from X.
5185     Address immed;
5186     bool is_movz;
5187     typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedW;
5188     if (static_cast<SignedW>(x) >= 0)
5189       {
5190         immed = reloc_property->select_x_value(x);
5191         is_movz = true;
5192       }
5193     else
5194       {
5195         immed = reloc_property->select_x_value(~x);;
5196         is_movz = false;
5197       }
5198
5199     // Update movnz instruction.
5200     update_movnz(view, immed, is_movz);
5201
5202     // Do check overflow or alignment if needed.
5203     return (reloc_property->checkup_x_value(x)
5204             ? This::STATUS_OKAY
5205             : This::STATUS_OVERFLOW);
5206   }
5207
5208   static inline bool
5209   maybe_apply_stub(unsigned int,
5210                    const The_relocate_info*,
5211                    const The_rela&,
5212                    unsigned char*,
5213                    Address,
5214                    const Sized_symbol<size>*,
5215                    const Symbol_value<size>*,
5216                    const Sized_relobj_file<size, big_endian>*,
5217                    section_size_type);
5218
5219 };  // End of AArch64_relocate_functions
5220
5221
5222 // For a certain relocation type (usually jump/branch), test to see if the
5223 // destination needs a stub to fulfil. If so, re-route the destination of the
5224 // original instruction to the stub, note, at this time, the stub has already
5225 // been generated.
5226
5227 template<int size, bool big_endian>
5228 bool
5229 AArch64_relocate_functions<size, big_endian>::
5230 maybe_apply_stub(unsigned int r_type,
5231                  const The_relocate_info* relinfo,
5232                  const The_rela& rela,
5233                  unsigned char* view,
5234                  Address address,
5235                  const Sized_symbol<size>* gsym,
5236                  const Symbol_value<size>* psymval,
5237                  const Sized_relobj_file<size, big_endian>* object,
5238                  section_size_type current_group_size)
5239 {
5240   if (parameters->options().relocatable())
5241     return false;
5242
5243   typename elfcpp::Elf_types<size>::Elf_Swxword addend = rela.get_r_addend();
5244   Address branch_target = psymval->value(object, 0) + addend;
5245   int stub_type =
5246     The_reloc_stub::stub_type_for_reloc(r_type, address, branch_target);
5247   if (stub_type == ST_NONE)
5248     return false;
5249
5250   const The_aarch64_relobj* aarch64_relobj =
5251       static_cast<const The_aarch64_relobj*>(object);
5252   The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
5253   gold_assert(stub_table != NULL);
5254
5255   unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5256   typename The_reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
5257   The_reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
5258   gold_assert(stub != NULL);
5259
5260   Address new_branch_target = stub_table->address() + stub->offset();
5261   typename elfcpp::Swap<size, big_endian>::Valtype branch_offset =
5262       new_branch_target - address;
5263   const AArch64_reloc_property* arp =
5264       aarch64_reloc_property_table->get_reloc_property(r_type);
5265   gold_assert(arp != NULL);
5266   typename This::Status status = This::template
5267       rela_general<32>(view, branch_offset, 0, arp);
5268   if (status != This::STATUS_OKAY)
5269     gold_error(_("Stub is too far away, try a smaller value "
5270                  "for '--stub-group-size'. The current value is 0x%lx."),
5271                static_cast<unsigned long>(current_group_size));
5272   return true;
5273 }
5274
5275
5276 // Group input sections for stub generation.
5277 //
5278 // We group input sections in an output section so that the total size,
5279 // including any padding space due to alignment is smaller than GROUP_SIZE
5280 // unless the only input section in group is bigger than GROUP_SIZE already.
5281 // Then an ARM stub table is created to follow the last input section
5282 // in group.  For each group an ARM stub table is created an is placed
5283 // after the last group.  If STUB_ALWAYS_AFTER_BRANCH is false, we further
5284 // extend the group after the stub table.
5285
5286 template<int size, bool big_endian>
5287 void
5288 Target_aarch64<size, big_endian>::group_sections(
5289     Layout* layout,
5290     section_size_type group_size,
5291     bool stubs_always_after_branch,
5292     const Task* task)
5293 {
5294   // Group input sections and insert stub table
5295   Layout::Section_list section_list;
5296   layout->get_executable_sections(&section_list);
5297   for (Layout::Section_list::const_iterator p = section_list.begin();
5298        p != section_list.end();
5299        ++p)
5300     {
5301       AArch64_output_section<size, big_endian>* output_section =
5302           static_cast<AArch64_output_section<size, big_endian>*>(*p);
5303       output_section->group_sections(group_size, stubs_always_after_branch,
5304                                      this, task);
5305     }
5306 }
5307
5308
5309 // Find the AArch64_input_section object corresponding to the SHNDX-th input
5310 // section of RELOBJ.
5311
5312 template<int size, bool big_endian>
5313 AArch64_input_section<size, big_endian>*
5314 Target_aarch64<size, big_endian>::find_aarch64_input_section(
5315     Relobj* relobj, unsigned int shndx) const
5316 {
5317   Section_id sid(relobj, shndx);
5318   typename AArch64_input_section_map::const_iterator p =
5319     this->aarch64_input_section_map_.find(sid);
5320   return (p != this->aarch64_input_section_map_.end()) ? p->second : NULL;
5321 }
5322
5323
5324 // Make a new AArch64_input_section object.
5325
5326 template<int size, bool big_endian>
5327 AArch64_input_section<size, big_endian>*
5328 Target_aarch64<size, big_endian>::new_aarch64_input_section(
5329     Relobj* relobj, unsigned int shndx)
5330 {
5331   Section_id sid(relobj, shndx);
5332
5333   AArch64_input_section<size, big_endian>* input_section =
5334       new AArch64_input_section<size, big_endian>(relobj, shndx);
5335   input_section->init();
5336
5337   // Register new AArch64_input_section in map for look-up.
5338   std::pair<typename AArch64_input_section_map::iterator,bool> ins =
5339       this->aarch64_input_section_map_.insert(
5340           std::make_pair(sid, input_section));
5341
5342   // Make sure that it we have not created another AArch64_input_section
5343   // for this input section already.
5344   gold_assert(ins.second);
5345
5346   return input_section;
5347 }
5348
5349
5350 // Relaxation hook.  This is where we do stub generation.
5351
5352 template<int size, bool big_endian>
5353 bool
5354 Target_aarch64<size, big_endian>::do_relax(
5355     int pass,
5356     const Input_objects* input_objects,
5357     Symbol_table* symtab,
5358     Layout* layout ,
5359     const Task* task)
5360 {
5361   gold_assert(!parameters->options().relocatable());
5362   if (pass == 1)
5363     {
5364       // We don't handle negative stub_group_size right now.
5365       this->stub_group_size_ = abs(parameters->options().stub_group_size());
5366       if (this->stub_group_size_ == 1)
5367         {
5368           // Leave room for 4096 4-byte stub entries. If we exceed that, then we
5369           // will fail to link.  The user will have to relink with an explicit
5370           // group size option.
5371           this->stub_group_size_ = The_reloc_stub::MAX_BRANCH_OFFSET -
5372                                    4096 * 4;
5373         }
5374       group_sections(layout, this->stub_group_size_, true, task);
5375     }
5376   else
5377     {
5378       // If this is not the first pass, addresses and file offsets have
5379       // been reset at this point, set them here.
5380       for (Stub_table_iterator sp = this->stub_tables_.begin();
5381            sp != this->stub_tables_.end(); ++sp)
5382         {
5383           The_stub_table* stt = *sp;
5384           The_aarch64_input_section* owner = stt->owner();
5385           off_t off = align_address(owner->original_size(),
5386                                     stt->addralign());
5387           stt->set_address_and_file_offset(owner->address() + off,
5388                                            owner->offset() + off);
5389         }
5390     }
5391
5392   // Scan relocs for relocation stubs
5393   for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
5394        op != input_objects->relobj_end();
5395        ++op)
5396     {
5397       The_aarch64_relobj* aarch64_relobj =
5398           static_cast<The_aarch64_relobj*>(*op);
5399       // Lock the object so we can read from it.  This is only called
5400       // single-threaded from Layout::finalize, so it is OK to lock.
5401       Task_lock_obj<Object> tl(task, aarch64_relobj);
5402       aarch64_relobj->scan_sections_for_stubs(this, symtab, layout);
5403     }
5404
5405   bool any_stub_table_changed = false;
5406   for (Stub_table_iterator siter = this->stub_tables_.begin();
5407        siter != this->stub_tables_.end() && !any_stub_table_changed; ++siter)
5408     {
5409       The_stub_table* stub_table = *siter;
5410       if (stub_table->update_data_size_changed_p())
5411         {
5412           The_aarch64_input_section* owner = stub_table->owner();
5413           uint64_t address = owner->address();
5414           off_t offset = owner->offset();
5415           owner->reset_address_and_file_offset();
5416           owner->set_address_and_file_offset(address, offset);
5417
5418           any_stub_table_changed = true;
5419         }
5420     }
5421
5422   // Do not continue relaxation.
5423   bool continue_relaxation = any_stub_table_changed;
5424   if (!continue_relaxation)
5425     for (Stub_table_iterator sp = this->stub_tables_.begin();
5426          (sp != this->stub_tables_.end());
5427          ++sp)
5428       (*sp)->finalize_stubs();
5429
5430   return continue_relaxation;
5431 }
5432
5433
5434 // Make a new Stub_table.
5435
5436 template<int size, bool big_endian>
5437 Stub_table<size, big_endian>*
5438 Target_aarch64<size, big_endian>::new_stub_table(
5439     AArch64_input_section<size, big_endian>* owner)
5440 {
5441   Stub_table<size, big_endian>* stub_table =
5442       new Stub_table<size, big_endian>(owner);
5443   stub_table->set_address(align_address(
5444       owner->address() + owner->data_size(), 8));
5445   stub_table->set_file_offset(owner->offset() + owner->data_size());
5446   stub_table->finalize_data_size();
5447
5448   this->stub_tables_.push_back(stub_table);
5449
5450   return stub_table;
5451 }
5452
5453
5454 template<int size, bool big_endian>
5455 uint64_t
5456 Target_aarch64<size, big_endian>::do_reloc_addend(
5457     void* arg, unsigned int r_type, uint64_t) const
5458 {
5459   gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
5460   uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5461   gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5462   const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5463   const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5464   gold_assert(psymval->is_tls_symbol());
5465   // The value of a TLS symbol is the offset in the TLS segment.
5466   return psymval->value(ti.object, 0);
5467 }
5468
5469 // Return the number of entries in the PLT.
5470
5471 template<int size, bool big_endian>
5472 unsigned int
5473 Target_aarch64<size, big_endian>::plt_entry_count() const
5474 {
5475   if (this->plt_ == NULL)
5476     return 0;
5477   return this->plt_->entry_count();
5478 }
5479
5480 // Return the offset of the first non-reserved PLT entry.
5481
5482 template<int size, bool big_endian>
5483 unsigned int
5484 Target_aarch64<size, big_endian>::first_plt_entry_offset() const
5485 {
5486   return this->plt_->first_plt_entry_offset();
5487 }
5488
5489 // Return the size of each PLT entry.
5490
5491 template<int size, bool big_endian>
5492 unsigned int
5493 Target_aarch64<size, big_endian>::plt_entry_size() const
5494 {
5495   return this->plt_->get_plt_entry_size();
5496 }
5497
5498 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
5499
5500 template<int size, bool big_endian>
5501 void
5502 Target_aarch64<size, big_endian>::define_tls_base_symbol(
5503     Symbol_table* symtab, Layout* layout)
5504 {
5505   if (this->tls_base_symbol_defined_)
5506     return;
5507
5508   Output_segment* tls_segment = layout->tls_segment();
5509   if (tls_segment != NULL)
5510     {
5511       // _TLS_MODULE_BASE_ always points to the beginning of tls segment.
5512       symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
5513                                        Symbol_table::PREDEFINED,
5514                                        tls_segment, 0, 0,
5515                                        elfcpp::STT_TLS,
5516                                        elfcpp::STB_LOCAL,
5517                                        elfcpp::STV_HIDDEN, 0,
5518                                        Symbol::SEGMENT_START,
5519                                        true);
5520     }
5521   this->tls_base_symbol_defined_ = true;
5522 }
5523
5524 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
5525
5526 template<int size, bool big_endian>
5527 void
5528 Target_aarch64<size, big_endian>::reserve_tlsdesc_entries(
5529     Symbol_table* symtab, Layout* layout)
5530 {
5531   if (this->plt_ == NULL)
5532     this->make_plt_section(symtab, layout);
5533
5534   if (!this->plt_->has_tlsdesc_entry())
5535     {
5536       // Allocate the TLSDESC_GOT entry.
5537       Output_data_got_aarch64<size, big_endian>* got =
5538           this->got_section(symtab, layout);
5539       unsigned int got_offset = got->add_constant(0);
5540
5541       // Allocate the TLSDESC_PLT entry.
5542       this->plt_->reserve_tlsdesc_entry(got_offset);
5543     }
5544 }
5545
5546 // Create a GOT entry for the TLS module index.
5547
5548 template<int size, bool big_endian>
5549 unsigned int
5550 Target_aarch64<size, big_endian>::got_mod_index_entry(
5551     Symbol_table* symtab, Layout* layout,
5552     Sized_relobj_file<size, big_endian>* object)
5553 {
5554   if (this->got_mod_index_offset_ == -1U)
5555     {
5556       gold_assert(symtab != NULL && layout != NULL && object != NULL);
5557       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5558       Output_data_got_aarch64<size, big_endian>* got =
5559           this->got_section(symtab, layout);
5560       unsigned int got_offset = got->add_constant(0);
5561       rela_dyn->add_local(object, 0, elfcpp::R_AARCH64_TLS_DTPMOD64, got,
5562                           got_offset, 0);
5563       got->add_constant(0);
5564       this->got_mod_index_offset_ = got_offset;
5565     }
5566   return this->got_mod_index_offset_;
5567 }
5568
5569 // Optimize the TLS relocation type based on what we know about the
5570 // symbol.  IS_FINAL is true if the final address of this symbol is
5571 // known at link time.
5572
5573 template<int size, bool big_endian>
5574 tls::Tls_optimization
5575 Target_aarch64<size, big_endian>::optimize_tls_reloc(bool is_final,
5576                                                      int r_type)
5577 {
5578   // If we are generating a shared library, then we can't do anything
5579   // in the linker
5580   if (parameters->options().shared())
5581     return tls::TLSOPT_NONE;
5582
5583   switch (r_type)
5584     {
5585     case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5586     case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5587     case elfcpp::R_AARCH64_TLSDESC_LD_PREL19:
5588     case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21:
5589     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5590     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5591     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5592     case elfcpp::R_AARCH64_TLSDESC_OFF_G1:
5593     case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC:
5594     case elfcpp::R_AARCH64_TLSDESC_LDR:
5595     case elfcpp::R_AARCH64_TLSDESC_ADD:
5596     case elfcpp::R_AARCH64_TLSDESC_CALL:
5597       // These are General-Dynamic which permits fully general TLS
5598       // access.  Since we know that we are generating an executable,
5599       // we can convert this to Initial-Exec.  If we also know that
5600       // this is a local symbol, we can further switch to Local-Exec.
5601       if (is_final)
5602         return tls::TLSOPT_TO_LE;
5603       return tls::TLSOPT_TO_IE;
5604
5605     case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5606     case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5607     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5608     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5609     case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5610     case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5611       // These are Local-Dynamic, which refer to local symbols in the
5612       // dynamic TLS block. Since we know that we generating an
5613       // executable, we can switch to Local-Exec.
5614       return tls::TLSOPT_TO_LE;
5615
5616     case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
5617     case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
5618     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5619     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5620     case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5621       // These are Initial-Exec relocs which get the thread offset
5622       // from the GOT. If we know that we are linking against the
5623       // local symbol, we can switch to Local-Exec, which links the
5624       // thread offset into the instruction.
5625       if (is_final)
5626         return tls::TLSOPT_TO_LE;
5627       return tls::TLSOPT_NONE;
5628
5629     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5630     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5631     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5632     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5633     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5634     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5635     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5636     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5637       // When we already have Local-Exec, there is nothing further we
5638       // can do.
5639       return tls::TLSOPT_NONE;
5640
5641     default:
5642       gold_unreachable();
5643     }
5644 }
5645
5646 // Returns true if this relocation type could be that of a function pointer.
5647
5648 template<int size, bool big_endian>
5649 inline bool
5650 Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
5651   unsigned int r_type)
5652 {
5653   switch (r_type)
5654     {
5655     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
5656     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
5657     case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
5658     case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5659     case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5660       {
5661         return true;
5662       }
5663     }
5664   return false;
5665 }
5666
5667 // For safe ICF, scan a relocation for a local symbol to check if it
5668 // corresponds to a function pointer being taken.  In that case mark
5669 // the function whose pointer was taken as not foldable.
5670
5671 template<int size, bool big_endian>
5672 inline bool
5673 Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
5674   Symbol_table* ,
5675   Layout* ,
5676   Target_aarch64<size, big_endian>* ,
5677   Sized_relobj_file<size, big_endian>* ,
5678   unsigned int ,
5679   Output_section* ,
5680   const elfcpp::Rela<size, big_endian>& ,
5681   unsigned int r_type,
5682   const elfcpp::Sym<size, big_endian>&)
5683 {
5684   // When building a shared library, do not fold any local symbols.
5685   return (parameters->options().shared()
5686           || possible_function_pointer_reloc(r_type));
5687 }
5688
5689 // For safe ICF, scan a relocation for a global symbol to check if it
5690 // corresponds to a function pointer being taken.  In that case mark
5691 // the function whose pointer was taken as not foldable.
5692
5693 template<int size, bool big_endian>
5694 inline bool
5695 Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
5696   Symbol_table* ,
5697   Layout* ,
5698   Target_aarch64<size, big_endian>* ,
5699   Sized_relobj_file<size, big_endian>* ,
5700   unsigned int ,
5701   Output_section* ,
5702   const elfcpp::Rela<size, big_endian>& ,
5703   unsigned int r_type,
5704   Symbol* gsym)
5705 {
5706   // When building a shared library, do not fold symbols whose visibility
5707   // is hidden, internal or protected.
5708   return ((parameters->options().shared()
5709            && (gsym->visibility() == elfcpp::STV_INTERNAL
5710                || gsym->visibility() == elfcpp::STV_PROTECTED
5711                || gsym->visibility() == elfcpp::STV_HIDDEN))
5712           || possible_function_pointer_reloc(r_type));
5713 }
5714
5715 // Report an unsupported relocation against a local symbol.
5716
5717 template<int size, bool big_endian>
5718 void
5719 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
5720      Sized_relobj_file<size, big_endian>* object,
5721      unsigned int r_type)
5722 {
5723   gold_error(_("%s: unsupported reloc %u against local symbol"),
5724              object->name().c_str(), r_type);
5725 }
5726
5727 // We are about to emit a dynamic relocation of type R_TYPE.  If the
5728 // dynamic linker does not support it, issue an error.
5729
5730 template<int size, bool big_endian>
5731 void
5732 Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
5733                                                       unsigned int r_type)
5734 {
5735   gold_assert(r_type != elfcpp::R_AARCH64_NONE);
5736
5737   switch (r_type)
5738     {
5739     // These are the relocation types supported by glibc for AARCH64.
5740     case elfcpp::R_AARCH64_NONE:
5741     case elfcpp::R_AARCH64_COPY:
5742     case elfcpp::R_AARCH64_GLOB_DAT:
5743     case elfcpp::R_AARCH64_JUMP_SLOT:
5744     case elfcpp::R_AARCH64_RELATIVE:
5745     case elfcpp::R_AARCH64_TLS_DTPREL64:
5746     case elfcpp::R_AARCH64_TLS_DTPMOD64:
5747     case elfcpp::R_AARCH64_TLS_TPREL64:
5748     case elfcpp::R_AARCH64_TLSDESC:
5749     case elfcpp::R_AARCH64_IRELATIVE:
5750     case elfcpp::R_AARCH64_ABS32:
5751     case elfcpp::R_AARCH64_ABS64:
5752       return;
5753
5754     default:
5755       break;
5756     }
5757
5758   // This prevents us from issuing more than one error per reloc
5759   // section. But we can still wind up issuing more than one
5760   // error per object file.
5761   if (this->issued_non_pic_error_)
5762     return;
5763   gold_assert(parameters->options().output_is_position_independent());
5764   object->error(_("requires unsupported dynamic reloc; "
5765                   "recompile with -fPIC"));
5766   this->issued_non_pic_error_ = true;
5767   return;
5768 }
5769
5770 // Return whether we need to make a PLT entry for a relocation of the
5771 // given type against a STT_GNU_IFUNC symbol.
5772
5773 template<int size, bool big_endian>
5774 bool
5775 Target_aarch64<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
5776     Sized_relobj_file<size, big_endian>* object,
5777     unsigned int r_type)
5778 {
5779   const AArch64_reloc_property* arp =
5780       aarch64_reloc_property_table->get_reloc_property(r_type);
5781   gold_assert(arp != NULL);
5782
5783   int flags = arp->reference_flags();
5784   if (flags & Symbol::TLS_REF)
5785     {
5786       gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
5787                  object->name().c_str(), arp->name().c_str());
5788       return false;
5789     }
5790   return flags != 0;
5791 }
5792
5793 // Scan a relocation for a local symbol.
5794
5795 template<int size, bool big_endian>
5796 inline void
5797 Target_aarch64<size, big_endian>::Scan::local(
5798     Symbol_table* symtab,
5799     Layout* layout,
5800     Target_aarch64<size, big_endian>* target,
5801     Sized_relobj_file<size, big_endian>* object,
5802     unsigned int data_shndx,
5803     Output_section* output_section,
5804     const elfcpp::Rela<size, big_endian>& rela,
5805     unsigned int r_type,
5806     const elfcpp::Sym<size, big_endian>& lsym,
5807     bool is_discarded)
5808 {
5809   if (is_discarded)
5810     return;
5811
5812   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
5813       Reloc_section;
5814   Output_data_got_aarch64<size, big_endian>* got =
5815       target->got_section(symtab, layout);
5816   unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5817
5818   // A local STT_GNU_IFUNC symbol may require a PLT entry.
5819   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
5820   if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
5821     target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
5822
5823   switch (r_type)
5824     {
5825     case elfcpp::R_AARCH64_ABS32:
5826     case elfcpp::R_AARCH64_ABS16:
5827       if (parameters->options().output_is_position_independent())
5828         {
5829           gold_error(_("%s: unsupported reloc %u in pos independent link."),
5830                      object->name().c_str(), r_type);
5831         }
5832       break;
5833
5834     case elfcpp::R_AARCH64_ABS64:
5835       // If building a shared library or pie, we need to mark this as a dynmic
5836       // reloction, so that the dynamic loader can relocate it.
5837       if (parameters->options().output_is_position_independent())
5838         {
5839           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5840           rela_dyn->add_local_relative(object, r_sym,
5841                                        elfcpp::R_AARCH64_RELATIVE,
5842                                        output_section,
5843                                        data_shndx,
5844                                        rela.get_r_offset(),
5845                                        rela.get_r_addend(),
5846                                        is_ifunc);
5847         }
5848       break;
5849
5850     case elfcpp::R_AARCH64_PREL64:
5851     case elfcpp::R_AARCH64_PREL32:
5852     case elfcpp::R_AARCH64_PREL16:
5853       break;
5854
5855     case elfcpp::R_AARCH64_LD_PREL_LO19:        // 273
5856     case elfcpp::R_AARCH64_ADR_PREL_LO21:       // 274
5857     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:    // 275
5858     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
5859     case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:     // 277
5860     case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:   // 278
5861     case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:  // 284
5862     case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:  // 285
5863     case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:  // 286
5864     case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
5865        break;
5866
5867     // Control flow, pc-relative. We don't need to do anything for a relative
5868     // addressing relocation against a local symbol if it does not reference
5869     // the GOT.
5870     case elfcpp::R_AARCH64_TSTBR14:
5871     case elfcpp::R_AARCH64_CONDBR19:
5872     case elfcpp::R_AARCH64_JUMP26:
5873     case elfcpp::R_AARCH64_CALL26:
5874       break;
5875
5876     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5877     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5878       {
5879         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5880           optimize_tls_reloc(!parameters->options().shared(), r_type);
5881         if (tlsopt == tls::TLSOPT_TO_LE)
5882           break;
5883
5884         layout->set_has_static_tls();
5885         // Create a GOT entry for the tp-relative offset.
5886         if (!parameters->doing_static_link())
5887           {
5888             got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
5889                                     target->rela_dyn_section(layout),
5890                                     elfcpp::R_AARCH64_TLS_TPREL64);
5891           }
5892         else if (!object->local_has_got_offset(r_sym,
5893                                                GOT_TYPE_TLS_OFFSET))
5894           {
5895             got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
5896             unsigned int got_offset =
5897                 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
5898             const elfcpp::Elf_Xword addend = rela.get_r_addend();
5899             gold_assert(addend == 0);
5900             got->add_static_reloc(got_offset, elfcpp::R_AARCH64_TLS_TPREL64,
5901                                   object, r_sym);
5902           }
5903       }
5904       break;
5905
5906     case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5907     case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5908       {
5909         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5910             optimize_tls_reloc(!parameters->options().shared(), r_type);
5911         if (tlsopt == tls::TLSOPT_TO_LE)
5912           {
5913             layout->set_has_static_tls();
5914             break;
5915           }
5916         gold_assert(tlsopt == tls::TLSOPT_NONE);
5917
5918         got->add_local_pair_with_rel(object,r_sym, data_shndx,
5919                                      GOT_TYPE_TLS_PAIR,
5920                                      target->rela_dyn_section(layout),
5921                                      elfcpp::R_AARCH64_TLS_DTPMOD64);
5922       }
5923       break;
5924
5925     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5926     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5927     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5928     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5929     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5930     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5931     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5932     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5933       {
5934         layout->set_has_static_tls();
5935         bool output_is_shared = parameters->options().shared();
5936         if (output_is_shared)
5937           gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
5938                      object->name().c_str(), r_type);
5939       }
5940       break;
5941
5942     case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5943     case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5944       {
5945         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5946             optimize_tls_reloc(!parameters->options().shared(), r_type);
5947         if (tlsopt == tls::TLSOPT_NONE)
5948           {
5949             // Create a GOT entry for the module index.
5950             target->got_mod_index_entry(symtab, layout, object);
5951           }
5952         else if (tlsopt != tls::TLSOPT_TO_LE)
5953           unsupported_reloc_local(object, r_type);
5954       }
5955       break;
5956
5957     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5958     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5959     case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5960     case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5961       break;
5962
5963     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5964     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5965     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5966       {
5967         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5968             optimize_tls_reloc(!parameters->options().shared(), r_type);
5969         target->define_tls_base_symbol(symtab, layout);
5970         if (tlsopt == tls::TLSOPT_NONE)
5971           {
5972             // Create reserved PLT and GOT entries for the resolver.
5973             target->reserve_tlsdesc_entries(symtab, layout);
5974
5975             // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
5976             // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
5977             // entry needs to be in an area in .got.plt, not .got. Call
5978             // got_section to make sure the section has been created.
5979             target->got_section(symtab, layout);
5980             Output_data_got<size, big_endian>* got =
5981                 target->got_tlsdesc_section();
5982             unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5983             if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
5984               {
5985                 unsigned int got_offset = got->add_constant(0);
5986                 got->add_constant(0);
5987                 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
5988                                              got_offset);
5989                 Reloc_section* rt = target->rela_tlsdesc_section(layout);
5990                 // We store the arguments we need in a vector, and use
5991                 // the index into the vector as the parameter to pass
5992                 // to the target specific routines.
5993                 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
5994                 void* arg = reinterpret_cast<void*>(intarg);
5995                 rt->add_target_specific(elfcpp::R_AARCH64_TLSDESC, arg,
5996                                         got, got_offset, 0);
5997               }
5998           }
5999         else if (tlsopt != tls::TLSOPT_TO_LE)
6000           unsupported_reloc_local(object, r_type);
6001       }
6002       break;
6003
6004     case elfcpp::R_AARCH64_TLSDESC_CALL:
6005       break;
6006
6007     default:
6008       unsupported_reloc_local(object, r_type);
6009     }
6010 }
6011
6012
6013 // Report an unsupported relocation against a global symbol.
6014
6015 template<int size, bool big_endian>
6016 void
6017 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
6018     Sized_relobj_file<size, big_endian>* object,
6019     unsigned int r_type,
6020     Symbol* gsym)
6021 {
6022   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6023              object->name().c_str(), r_type, gsym->demangled_name().c_str());
6024 }
6025
6026 template<int size, bool big_endian>
6027 inline void
6028 Target_aarch64<size, big_endian>::Scan::global(
6029     Symbol_table* symtab,
6030     Layout* layout,
6031     Target_aarch64<size, big_endian>* target,
6032     Sized_relobj_file<size, big_endian> * object,
6033     unsigned int data_shndx,
6034     Output_section* output_section,
6035     const elfcpp::Rela<size, big_endian>& rela,
6036     unsigned int r_type,
6037     Symbol* gsym)
6038 {
6039   // A STT_GNU_IFUNC symbol may require a PLT entry.
6040   if (gsym->type() == elfcpp::STT_GNU_IFUNC
6041       && this->reloc_needs_plt_for_ifunc(object, r_type))
6042     target->make_plt_entry(symtab, layout, gsym);
6043
6044   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
6045     Reloc_section;
6046   const AArch64_reloc_property* arp =
6047       aarch64_reloc_property_table->get_reloc_property(r_type);
6048   gold_assert(arp != NULL);
6049
6050   switch (r_type)
6051     {
6052     case elfcpp::R_AARCH64_ABS16:
6053     case elfcpp::R_AARCH64_ABS32:
6054     case elfcpp::R_AARCH64_ABS64:
6055       {
6056         // Make a PLT entry if necessary.
6057         if (gsym->needs_plt_entry())
6058           {
6059             target->make_plt_entry(symtab, layout, gsym);
6060             // Since this is not a PC-relative relocation, we may be
6061             // taking the address of a function. In that case we need to
6062             // set the entry in the dynamic symbol table to the address of
6063             // the PLT entry.
6064             if (gsym->is_from_dynobj() && !parameters->options().shared())
6065               gsym->set_needs_dynsym_value();
6066           }
6067         // Make a dynamic relocation if necessary.
6068         if (gsym->needs_dynamic_reloc(arp->reference_flags()))
6069           {
6070             if (!parameters->options().output_is_position_independent()
6071                 && gsym->may_need_copy_reloc())
6072               {
6073                 target->copy_reloc(symtab, layout, object,
6074                                    data_shndx, output_section, gsym, rela);
6075               }
6076             else if (r_type == elfcpp::R_AARCH64_ABS64
6077                      && gsym->type() == elfcpp::STT_GNU_IFUNC
6078                      && gsym->can_use_relative_reloc(false)
6079                      && !gsym->is_from_dynobj()
6080                      && !gsym->is_undefined()
6081                      && !gsym->is_preemptible())
6082               {
6083                 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
6084                 // symbol. This makes a function address in a PIE executable
6085                 // match the address in a shared library that it links against.
6086                 Reloc_section* rela_dyn =
6087                     target->rela_irelative_section(layout);
6088                 unsigned int r_type = elfcpp::R_AARCH64_IRELATIVE;
6089                 rela_dyn->add_symbolless_global_addend(gsym, r_type,
6090                                                        output_section, object,
6091                                                        data_shndx,
6092                                                        rela.get_r_offset(),
6093                                                        rela.get_r_addend());
6094               }
6095             else if (r_type == elfcpp::R_AARCH64_ABS64
6096                      && gsym->can_use_relative_reloc(false))
6097               {
6098                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6099                 rela_dyn->add_global_relative(gsym,
6100                                               elfcpp::R_AARCH64_RELATIVE,
6101                                               output_section,
6102                                               object,
6103                                               data_shndx,
6104                                               rela.get_r_offset(),
6105                                               rela.get_r_addend(),
6106                                               false);
6107               }
6108             else
6109               {
6110                 check_non_pic(object, r_type);
6111                 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*
6112                     rela_dyn = target->rela_dyn_section(layout);
6113                 rela_dyn->add_global(
6114                   gsym, r_type, output_section, object,
6115                   data_shndx, rela.get_r_offset(),rela.get_r_addend());
6116               }
6117           }
6118       }
6119       break;
6120
6121     case elfcpp::R_AARCH64_PREL16:
6122     case elfcpp::R_AARCH64_PREL32:
6123     case elfcpp::R_AARCH64_PREL64:
6124       // This is used to fill the GOT absolute address.
6125       if (gsym->needs_plt_entry())
6126         {
6127           target->make_plt_entry(symtab, layout, gsym);
6128         }
6129       break;
6130
6131     case elfcpp::R_AARCH64_LD_PREL_LO19:        // 273
6132     case elfcpp::R_AARCH64_ADR_PREL_LO21:       // 274
6133     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:    // 275
6134     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
6135     case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:     // 277
6136     case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:   // 278
6137     case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:  // 284
6138     case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:  // 285
6139     case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:  // 286
6140     case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
6141       {
6142         if (gsym->needs_plt_entry())
6143           target->make_plt_entry(symtab, layout, gsym);
6144         // Make a dynamic relocation if necessary.
6145         if (gsym->needs_dynamic_reloc(arp->reference_flags()))
6146           {
6147             if (parameters->options().output_is_executable()
6148                 && gsym->may_need_copy_reloc())
6149               {
6150                 target->copy_reloc(symtab, layout, object,
6151                                    data_shndx, output_section, gsym, rela);
6152               }
6153           }
6154         break;
6155       }
6156
6157     case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6158     case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6159       {
6160         // This pair of relocations is used to access a specific GOT entry.
6161         // Note a GOT entry is an *address* to a symbol.
6162         // The symbol requires a GOT entry
6163         Output_data_got_aarch64<size, big_endian>* got =
6164           target->got_section(symtab, layout);
6165         if (gsym->final_value_is_known())
6166           {
6167             // For a STT_GNU_IFUNC symbol we want the PLT address.
6168             if (gsym->type() == elfcpp::STT_GNU_IFUNC)
6169               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6170             else
6171               got->add_global(gsym, GOT_TYPE_STANDARD);
6172           }
6173         else
6174           {
6175             // If this symbol is not fully resolved, we need to add a dynamic
6176             // relocation for it.
6177             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6178
6179             // Use a GLOB_DAT rather than a RELATIVE reloc if:
6180             //
6181             // 1) The symbol may be defined in some other module.
6182             // 2) We are building a shared library and this is a protected
6183             // symbol; using GLOB_DAT means that the dynamic linker can use
6184             // the address of the PLT in the main executable when appropriate
6185             // so that function address comparisons work.
6186             // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
6187             // again so that function address comparisons work.
6188             if (gsym->is_from_dynobj()
6189                 || gsym->is_undefined()
6190                 || gsym->is_preemptible()
6191                 || (gsym->visibility() == elfcpp::STV_PROTECTED
6192                     && parameters->options().shared())
6193                 || (gsym->type() == elfcpp::STT_GNU_IFUNC
6194                     && parameters->options().output_is_position_independent()))
6195               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
6196                                        rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
6197             else
6198               {
6199                 // For a STT_GNU_IFUNC symbol we want to write the PLT
6200                 // offset into the GOT, so that function pointer
6201                 // comparisons work correctly.
6202                 bool is_new;
6203                 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
6204                   is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
6205                 else
6206                   {
6207                     is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6208                     // Tell the dynamic linker to use the PLT address
6209                     // when resolving relocations.
6210                     if (gsym->is_from_dynobj()
6211                         && !parameters->options().shared())
6212                       gsym->set_needs_dynsym_value();
6213                   }
6214                 if (is_new)
6215                   {
6216                     rela_dyn->add_global_relative(
6217                         gsym, elfcpp::R_AARCH64_RELATIVE,
6218                         got,
6219                         gsym->got_offset(GOT_TYPE_STANDARD),
6220                         0,
6221                         false);
6222                   }
6223               }
6224           }
6225         break;
6226       }
6227
6228     case elfcpp::R_AARCH64_TSTBR14:
6229     case elfcpp::R_AARCH64_CONDBR19:
6230     case elfcpp::R_AARCH64_JUMP26:
6231     case elfcpp::R_AARCH64_CALL26:
6232       {
6233         if (gsym->final_value_is_known())
6234           break;
6235
6236         if (gsym->is_defined() &&
6237             !gsym->is_from_dynobj() &&
6238             !gsym->is_preemptible())
6239           break;
6240
6241         // Make plt entry for function call.
6242         target->make_plt_entry(symtab, layout, gsym);
6243         break;
6244       }
6245
6246     case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6247     case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:  // General dynamic
6248       {
6249         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6250             optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6251         if (tlsopt == tls::TLSOPT_TO_LE)
6252           {
6253             layout->set_has_static_tls();
6254             break;
6255           }
6256         gold_assert(tlsopt == tls::TLSOPT_NONE);
6257
6258         // General dynamic.
6259         Output_data_got_aarch64<size, big_endian>* got =
6260             target->got_section(symtab, layout);
6261         // Create 2 consecutive entries for module index and offset.
6262         got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
6263                                       target->rela_dyn_section(layout),
6264                                       elfcpp::R_AARCH64_TLS_DTPMOD64,
6265                                       elfcpp::R_AARCH64_TLS_DTPREL64);
6266       }
6267       break;
6268
6269     case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6270     case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:  // Local dynamic
6271       {
6272         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6273             optimize_tls_reloc(!parameters->options().shared(), r_type);
6274         if (tlsopt == tls::TLSOPT_NONE)
6275           {
6276             // Create a GOT entry for the module index.
6277             target->got_mod_index_entry(symtab, layout, object);
6278           }
6279         else if (tlsopt != tls::TLSOPT_TO_LE)
6280           unsupported_reloc_local(object, r_type);
6281       }
6282       break;
6283
6284     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6285     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6286     case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6287     case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:  // Other local dynamic
6288       break;
6289
6290     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6291     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:  // Initial executable
6292       {
6293         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6294           optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6295         if (tlsopt == tls::TLSOPT_TO_LE)
6296           break;
6297
6298         layout->set_has_static_tls();
6299         // Create a GOT entry for the tp-relative offset.
6300         Output_data_got_aarch64<size, big_endian>* got
6301           = target->got_section(symtab, layout);
6302         if (!parameters->doing_static_link())
6303           {
6304             got->add_global_with_rel(
6305               gsym, GOT_TYPE_TLS_OFFSET,
6306               target->rela_dyn_section(layout),
6307               elfcpp::R_AARCH64_TLS_TPREL64);
6308           }
6309         if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
6310           {
6311             got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
6312             unsigned int got_offset =
6313               gsym->got_offset(GOT_TYPE_TLS_OFFSET);
6314             const elfcpp::Elf_Xword addend = rela.get_r_addend();
6315             gold_assert(addend == 0);
6316             got->add_static_reloc(got_offset,
6317                                   elfcpp::R_AARCH64_TLS_TPREL64, gsym);
6318           }
6319       }
6320       break;
6321
6322     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6323     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6324     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6325     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6326     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6327     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6328     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6329     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:  // Local executable
6330       layout->set_has_static_tls();
6331       if (parameters->options().shared())
6332         gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
6333                    object->name().c_str(), r_type);
6334       break;
6335
6336     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6337     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6338     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:  // TLS descriptor
6339       {
6340         target->define_tls_base_symbol(symtab, layout);
6341         tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6342             optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6343         if (tlsopt == tls::TLSOPT_NONE)
6344           {
6345             // Create reserved PLT and GOT entries for the resolver.
6346             target->reserve_tlsdesc_entries(symtab, layout);
6347
6348             // Create a double GOT entry with an R_AARCH64_TLSDESC
6349             // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
6350             // entry needs to be in an area in .got.plt, not .got. Call
6351             // got_section to make sure the section has been created.
6352             target->got_section(symtab, layout);
6353             Output_data_got<size, big_endian>* got =
6354                 target->got_tlsdesc_section();
6355             Reloc_section* rt = target->rela_tlsdesc_section(layout);
6356             got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
6357                                           elfcpp::R_AARCH64_TLSDESC, 0);
6358           }
6359         else if (tlsopt == tls::TLSOPT_TO_IE)
6360           {
6361             // Create a GOT entry for the tp-relative offset.
6362             Output_data_got<size, big_endian>* got
6363                 = target->got_section(symtab, layout);
6364             got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
6365                                      target->rela_dyn_section(layout),
6366                                      elfcpp::R_AARCH64_TLS_TPREL64);
6367           }
6368         else if (tlsopt != tls::TLSOPT_TO_LE)
6369           unsupported_reloc_global(object, r_type, gsym);
6370       }
6371       break;
6372
6373     case elfcpp::R_AARCH64_TLSDESC_CALL:
6374       break;
6375
6376     default:
6377       gold_error(_("%s: unsupported reloc type in global scan"),
6378                  aarch64_reloc_property_table->
6379                  reloc_name_in_error_message(r_type).c_str());
6380     }
6381   return;
6382 }  // End of Scan::global
6383
6384
6385 // Create the PLT section.
6386 template<int size, bool big_endian>
6387 void
6388 Target_aarch64<size, big_endian>::make_plt_section(
6389   Symbol_table* symtab, Layout* layout)
6390 {
6391   if (this->plt_ == NULL)
6392     {
6393       // Create the GOT section first.
6394       this->got_section(symtab, layout);
6395
6396       this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
6397                                        this->got_irelative_);
6398
6399       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
6400                                       (elfcpp::SHF_ALLOC
6401                                        | elfcpp::SHF_EXECINSTR),
6402                                       this->plt_, ORDER_PLT, false);
6403
6404       // Make the sh_info field of .rela.plt point to .plt.
6405       Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
6406       rela_plt_os->set_info_section(this->plt_->output_section());
6407     }
6408 }
6409
6410 // Return the section for TLSDESC relocations.
6411
6412 template<int size, bool big_endian>
6413 typename Target_aarch64<size, big_endian>::Reloc_section*
6414 Target_aarch64<size, big_endian>::rela_tlsdesc_section(Layout* layout) const
6415 {
6416   return this->plt_section()->rela_tlsdesc(layout);
6417 }
6418
6419 // Create a PLT entry for a global symbol.
6420
6421 template<int size, bool big_endian>
6422 void
6423 Target_aarch64<size, big_endian>::make_plt_entry(
6424     Symbol_table* symtab,
6425     Layout* layout,
6426     Symbol* gsym)
6427 {
6428   if (gsym->has_plt_offset())
6429     return;
6430
6431   if (this->plt_ == NULL)
6432     this->make_plt_section(symtab, layout);
6433
6434   this->plt_->add_entry(symtab, layout, gsym);
6435 }
6436
6437 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6438
6439 template<int size, bool big_endian>
6440 void
6441 Target_aarch64<size, big_endian>::make_local_ifunc_plt_entry(
6442     Symbol_table* symtab, Layout* layout,
6443     Sized_relobj_file<size, big_endian>* relobj,
6444     unsigned int local_sym_index)
6445 {
6446   if (relobj->local_has_plt_offset(local_sym_index))
6447     return;
6448   if (this->plt_ == NULL)
6449     this->make_plt_section(symtab, layout);
6450   unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
6451                                                               relobj,
6452                                                               local_sym_index);
6453   relobj->set_local_plt_offset(local_sym_index, plt_offset);
6454 }
6455
6456 template<int size, bool big_endian>
6457 void
6458 Target_aarch64<size, big_endian>::gc_process_relocs(
6459     Symbol_table* symtab,
6460     Layout* layout,
6461     Sized_relobj_file<size, big_endian>* object,
6462     unsigned int data_shndx,
6463     unsigned int sh_type,
6464     const unsigned char* prelocs,
6465     size_t reloc_count,
6466     Output_section* output_section,
6467     bool needs_special_offset_handling,
6468     size_t local_symbol_count,
6469     const unsigned char* plocal_symbols)
6470 {
6471   if (sh_type == elfcpp::SHT_REL)
6472     {
6473       return;
6474     }
6475
6476   gold::gc_process_relocs<
6477     size, big_endian,
6478     Target_aarch64<size, big_endian>,
6479     elfcpp::SHT_RELA,
6480     typename Target_aarch64<size, big_endian>::Scan,
6481     typename Target_aarch64<size, big_endian>::Relocatable_size_for_reloc>(
6482     symtab,
6483     layout,
6484     this,
6485     object,
6486     data_shndx,
6487     prelocs,
6488     reloc_count,
6489     output_section,
6490     needs_special_offset_handling,
6491     local_symbol_count,
6492     plocal_symbols);
6493 }
6494
6495 // Scan relocations for a section.
6496
6497 template<int size, bool big_endian>
6498 void
6499 Target_aarch64<size, big_endian>::scan_relocs(
6500     Symbol_table* symtab,
6501     Layout* layout,
6502     Sized_relobj_file<size, big_endian>* object,
6503     unsigned int data_shndx,
6504     unsigned int sh_type,
6505     const unsigned char* prelocs,
6506     size_t reloc_count,
6507     Output_section* output_section,
6508     bool needs_special_offset_handling,
6509     size_t local_symbol_count,
6510     const unsigned char* plocal_symbols)
6511 {
6512   if (sh_type == elfcpp::SHT_REL)
6513     {
6514       gold_error(_("%s: unsupported REL reloc section"),
6515                  object->name().c_str());
6516       return;
6517     }
6518   gold::scan_relocs<size, big_endian, Target_aarch64, elfcpp::SHT_RELA, Scan>(
6519     symtab,
6520     layout,
6521     this,
6522     object,
6523     data_shndx,
6524     prelocs,
6525     reloc_count,
6526     output_section,
6527     needs_special_offset_handling,
6528     local_symbol_count,
6529     plocal_symbols);
6530 }
6531
6532 // Return the value to use for a dynamic which requires special
6533 // treatment.  This is how we support equality comparisons of function
6534 // pointers across shared library boundaries, as described in the
6535 // processor specific ABI supplement.
6536
6537 template<int size, bool big_endian>
6538 uint64_t
6539 Target_aarch64<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
6540 {
6541   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
6542   return this->plt_address_for_global(gsym);
6543 }
6544
6545
6546 // Finalize the sections.
6547
6548 template<int size, bool big_endian>
6549 void
6550 Target_aarch64<size, big_endian>::do_finalize_sections(
6551     Layout* layout,
6552     const Input_objects*,
6553     Symbol_table* symtab)
6554 {
6555   const Reloc_section* rel_plt = (this->plt_ == NULL
6556                                   ? NULL
6557                                   : this->plt_->rela_plt());
6558   layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
6559                                   this->rela_dyn_, true, false);
6560
6561   // Emit any relocs we saved in an attempt to avoid generating COPY
6562   // relocs.
6563   if (this->copy_relocs_.any_saved_relocs())
6564     this->copy_relocs_.emit(this->rela_dyn_section(layout));
6565
6566   // Fill in some more dynamic tags.
6567   Output_data_dynamic* const odyn = layout->dynamic_data();
6568   if (odyn != NULL)
6569     {
6570       if (this->plt_ != NULL
6571           && this->plt_->output_section() != NULL
6572           && this->plt_ ->has_tlsdesc_entry())
6573         {
6574           unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
6575           unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
6576           this->got_->finalize_data_size();
6577           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
6578                                         this->plt_, plt_offset);
6579           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
6580                                         this->got_, got_offset);
6581         }
6582     }
6583
6584   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
6585   // the .got.plt section.
6586   Symbol* sym = this->global_offset_table_;
6587   if (sym != NULL)
6588     {
6589       uint64_t data_size = this->got_plt_->current_data_size();
6590       symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
6591
6592       // If the .got section is more than 0x8000 bytes, we add
6593       // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
6594       // bit relocations have a greater chance of working.
6595       if (data_size >= 0x8000)
6596         symtab->get_sized_symbol<size>(sym)->set_value(
6597           symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
6598     }
6599
6600   if (parameters->doing_static_link()
6601       && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
6602     {
6603       // If linking statically, make sure that the __rela_iplt symbols
6604       // were defined if necessary, even if we didn't create a PLT.
6605       static const Define_symbol_in_segment syms[] =
6606         {
6607           {
6608             "__rela_iplt_start",        // name
6609             elfcpp::PT_LOAD,            // segment_type
6610             elfcpp::PF_W,               // segment_flags_set
6611             elfcpp::PF(0),              // segment_flags_clear
6612             0,                          // value
6613             0,                          // size
6614             elfcpp::STT_NOTYPE,         // type
6615             elfcpp::STB_GLOBAL,         // binding
6616             elfcpp::STV_HIDDEN,         // visibility
6617             0,                          // nonvis
6618             Symbol::SEGMENT_START,      // offset_from_base
6619             true                        // only_if_ref
6620           },
6621           {
6622             "__rela_iplt_end",          // name
6623             elfcpp::PT_LOAD,            // segment_type
6624             elfcpp::PF_W,               // segment_flags_set
6625             elfcpp::PF(0),              // segment_flags_clear
6626             0,                          // value
6627             0,                          // size
6628             elfcpp::STT_NOTYPE,         // type
6629             elfcpp::STB_GLOBAL,         // binding
6630             elfcpp::STV_HIDDEN,         // visibility
6631             0,                          // nonvis
6632             Symbol::SEGMENT_START,      // offset_from_base
6633             true                        // only_if_ref
6634           }
6635         };
6636
6637       symtab->define_symbols(layout, 2, syms,
6638                              layout->script_options()->saw_sections_clause());
6639     }
6640
6641   return;
6642 }
6643
6644 // Perform a relocation.
6645
6646 template<int size, bool big_endian>
6647 inline bool
6648 Target_aarch64<size, big_endian>::Relocate::relocate(
6649     const Relocate_info<size, big_endian>* relinfo,
6650     Target_aarch64<size, big_endian>* target,
6651     Output_section* ,
6652     size_t relnum,
6653     const elfcpp::Rela<size, big_endian>& rela,
6654     unsigned int r_type,
6655     const Sized_symbol<size>* gsym,
6656     const Symbol_value<size>* psymval,
6657     unsigned char* view,
6658     typename elfcpp::Elf_types<size>::Elf_Addr address,
6659     section_size_type /* view_size */)
6660 {
6661   if (view == NULL)
6662     return true;
6663
6664   typedef AArch64_relocate_functions<size, big_endian> Reloc;
6665
6666   const AArch64_reloc_property* reloc_property =
6667       aarch64_reloc_property_table->get_reloc_property(r_type);
6668
6669   if (reloc_property == NULL)
6670     {
6671       std::string reloc_name =
6672           aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
6673       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6674                              _("cannot relocate %s in object file"),
6675                              reloc_name.c_str());
6676       return true;
6677     }
6678
6679   const Sized_relobj_file<size, big_endian>* object = relinfo->object;
6680
6681   // Pick the value to use for symbols defined in the PLT.
6682   Symbol_value<size> symval;
6683   if (gsym != NULL
6684       && gsym->use_plt_offset(reloc_property->reference_flags()))
6685     {
6686       symval.set_output_value(target->plt_address_for_global(gsym));
6687       psymval = &symval;
6688     }
6689   else if (gsym == NULL && psymval->is_ifunc_symbol())
6690     {
6691       unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6692       if (object->local_has_plt_offset(r_sym))
6693         {
6694           symval.set_output_value(target->plt_address_for_local(object, r_sym));
6695           psymval = &symval;
6696         }
6697     }
6698
6699   const elfcpp::Elf_Xword addend = rela.get_r_addend();
6700
6701   // Get the GOT offset if needed.
6702   // For aarch64, the GOT pointer points to the start of the GOT section.
6703   bool have_got_offset = false;
6704   int got_offset = 0;
6705   int got_base = (target->got_ != NULL
6706                   ? (target->got_->current_data_size() >= 0x8000
6707                      ? 0x8000 : 0)
6708                   : 0);
6709   switch (r_type)
6710     {
6711     case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
6712     case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
6713     case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
6714     case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
6715     case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
6716     case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
6717     case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
6718     case elfcpp::R_AARCH64_GOTREL64:
6719     case elfcpp::R_AARCH64_GOTREL32:
6720     case elfcpp::R_AARCH64_GOT_LD_PREL19:
6721     case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
6722     case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6723     case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6724     case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
6725       if (gsym != NULL)
6726         {
6727           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6728           got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
6729         }
6730       else
6731         {
6732           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6733           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6734           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
6735                         - got_base);
6736         }
6737       have_got_offset = true;
6738       break;
6739
6740     default:
6741       break;
6742     }
6743
6744   typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
6745   typename elfcpp::Elf_types<size>::Elf_Addr value;
6746   switch (r_type)
6747     {
6748     case elfcpp::R_AARCH64_NONE:
6749       break;
6750
6751     case elfcpp::R_AARCH64_ABS64:
6752       reloc_status = Reloc::template rela_ua<64>(
6753         view, object, psymval, addend, reloc_property);
6754       break;
6755
6756     case elfcpp::R_AARCH64_ABS32:
6757       reloc_status = Reloc::template rela_ua<32>(
6758         view, object, psymval, addend, reloc_property);
6759       break;
6760
6761     case elfcpp::R_AARCH64_ABS16:
6762       reloc_status = Reloc::template rela_ua<16>(
6763         view, object, psymval, addend, reloc_property);
6764       break;
6765
6766     case elfcpp::R_AARCH64_PREL64:
6767       reloc_status = Reloc::template pcrela_ua<64>(
6768         view, object, psymval, addend, address, reloc_property);
6769       break;
6770
6771     case elfcpp::R_AARCH64_PREL32:
6772       reloc_status = Reloc::template pcrela_ua<32>(
6773         view, object, psymval, addend, address, reloc_property);
6774       break;
6775
6776     case elfcpp::R_AARCH64_PREL16:
6777       reloc_status = Reloc::template pcrela_ua<16>(
6778         view, object, psymval, addend, address, reloc_property);
6779       break;
6780
6781     case elfcpp::R_AARCH64_LD_PREL_LO19:
6782       reloc_status = Reloc::template pcrela_general<32>(
6783           view, object, psymval, addend, address, reloc_property);
6784       break;
6785
6786     case elfcpp::R_AARCH64_ADR_PREL_LO21:
6787       reloc_status = Reloc::adr(view, object, psymval, addend,
6788                                 address, reloc_property);
6789       break;
6790
6791     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
6792     case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
6793       reloc_status = Reloc::adrp(view, object, psymval, addend, address,
6794                                  reloc_property);
6795       break;
6796
6797     case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
6798     case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
6799     case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
6800     case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
6801     case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
6802     case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
6803       reloc_status = Reloc::template rela_general<32>(
6804         view, object, psymval, addend, reloc_property);
6805       break;
6806
6807     case elfcpp::R_AARCH64_CALL26:
6808       if (this->skip_call_tls_get_addr_)
6809         {
6810           // Double check that the TLSGD insn has been optimized away.
6811           typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6812           Insntype insn = elfcpp::Swap<32, big_endian>::readval(
6813               reinterpret_cast<Insntype*>(view));
6814           gold_assert((insn & 0xff000000) == 0x91000000);
6815
6816           reloc_status = Reloc::STATUS_OKAY;
6817           this->skip_call_tls_get_addr_ = false;
6818           // Return false to stop further processing this reloc.
6819           return false;
6820         }
6821       // Fallthrough
6822     case elfcpp::R_AARCH64_JUMP26:
6823       if (Reloc::maybe_apply_stub(r_type, relinfo, rela, view, address,
6824                                   gsym, psymval, object,
6825                                   target->stub_group_size_))
6826         break;
6827       // Fallthrough
6828     case elfcpp::R_AARCH64_TSTBR14:
6829     case elfcpp::R_AARCH64_CONDBR19:
6830       reloc_status = Reloc::template pcrela_general<32>(
6831         view, object, psymval, addend, address, reloc_property);
6832       break;
6833
6834     case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6835       gold_assert(have_got_offset);
6836       value = target->got_->address() + got_base + got_offset;
6837       reloc_status = Reloc::adrp(view, value + addend, address);
6838       break;
6839
6840     case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6841       gold_assert(have_got_offset);
6842       value = target->got_->address() + got_base + got_offset;
6843       reloc_status = Reloc::template rela_general<32>(
6844         view, value, addend, reloc_property);
6845       break;
6846
6847     case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6848     case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6849     case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6850     case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
6851     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6852     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6853     case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6854     case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
6855     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6856     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6857     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6858     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6859     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6860     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6861     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6862     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6863     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6864     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6865     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6866     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6867     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6868     case elfcpp::R_AARCH64_TLSDESC_CALL:
6869       reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
6870                                   gsym, psymval, view, address);
6871       break;
6872
6873     // These are dynamic relocations, which are unexpected when linking.
6874     case elfcpp::R_AARCH64_COPY:
6875     case elfcpp::R_AARCH64_GLOB_DAT:
6876     case elfcpp::R_AARCH64_JUMP_SLOT:
6877     case elfcpp::R_AARCH64_RELATIVE:
6878     case elfcpp::R_AARCH64_IRELATIVE:
6879     case elfcpp::R_AARCH64_TLS_DTPREL64:
6880     case elfcpp::R_AARCH64_TLS_DTPMOD64:
6881     case elfcpp::R_AARCH64_TLS_TPREL64:
6882     case elfcpp::R_AARCH64_TLSDESC:
6883       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6884                              _("unexpected reloc %u in object file"),
6885                              r_type);
6886       break;
6887
6888     default:
6889       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6890                              _("unsupported reloc %s"),
6891                              reloc_property->name().c_str());
6892       break;
6893     }
6894
6895   // Report any errors.
6896   switch (reloc_status)
6897     {
6898     case Reloc::STATUS_OKAY:
6899       break;
6900     case Reloc::STATUS_OVERFLOW:
6901       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6902                              _("relocation overflow in %s"),
6903                              reloc_property->name().c_str());
6904       break;
6905     case Reloc::STATUS_BAD_RELOC:
6906       gold_error_at_location(
6907           relinfo,
6908           relnum,
6909           rela.get_r_offset(),
6910           _("unexpected opcode while processing relocation %s"),
6911           reloc_property->name().c_str());
6912       break;
6913     default:
6914       gold_unreachable();
6915     }
6916
6917   return true;
6918 }
6919
6920
6921 template<int size, bool big_endian>
6922 inline
6923 typename AArch64_relocate_functions<size, big_endian>::Status
6924 Target_aarch64<size, big_endian>::Relocate::relocate_tls(
6925     const Relocate_info<size, big_endian>* relinfo,
6926     Target_aarch64<size, big_endian>* target,
6927     size_t relnum,
6928     const elfcpp::Rela<size, big_endian>& rela,
6929     unsigned int r_type, const Sized_symbol<size>* gsym,
6930     const Symbol_value<size>* psymval,
6931     unsigned char* view,
6932     typename elfcpp::Elf_types<size>::Elf_Addr address)
6933 {
6934   typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6935   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6936
6937   Output_segment* tls_segment = relinfo->layout->tls_segment();
6938   const elfcpp::Elf_Xword addend = rela.get_r_addend();
6939   const AArch64_reloc_property* reloc_property =
6940       aarch64_reloc_property_table->get_reloc_property(r_type);
6941   gold_assert(reloc_property != NULL);
6942
6943   const bool is_final = (gsym == NULL
6944                          ? !parameters->options().shared()
6945                          : gsym->final_value_is_known());
6946   tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6947       optimize_tls_reloc(is_final, r_type);
6948
6949   Sized_relobj_file<size, big_endian>* object = relinfo->object;
6950   int tls_got_offset_type;
6951   switch (r_type)
6952     {
6953     case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6954     case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:  // Global-dynamic
6955       {
6956         if (tlsopt == tls::TLSOPT_TO_LE)
6957           {
6958             if (tls_segment == NULL)
6959               {
6960                 gold_assert(parameters->errors()->error_count() > 0
6961                             || issue_undefined_symbol_error(gsym));
6962                 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6963               }
6964             return tls_gd_to_le(relinfo, target, rela, r_type, view,
6965                                 psymval);
6966           }
6967         else if (tlsopt == tls::TLSOPT_NONE)
6968           {
6969             tls_got_offset_type = GOT_TYPE_TLS_PAIR;
6970             // Firstly get the address for the got entry.
6971             typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6972             if (gsym != NULL)
6973               {
6974                 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6975                 got_entry_address = target->got_->address() +
6976                                     gsym->got_offset(tls_got_offset_type);
6977               }
6978             else
6979               {
6980                 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6981                 gold_assert(
6982                   object->local_has_got_offset(r_sym, tls_got_offset_type));
6983                 got_entry_address = target->got_->address() +
6984                   object->local_got_offset(r_sym, tls_got_offset_type);
6985               }
6986
6987             // Relocate the address into adrp/ld, adrp/add pair.
6988             switch (r_type)
6989               {
6990               case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6991                 return aarch64_reloc_funcs::adrp(
6992                   view, got_entry_address + addend, address);
6993
6994                 break;
6995
6996               case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6997                 return aarch64_reloc_funcs::template rela_general<32>(
6998                   view, got_entry_address, addend, reloc_property);
6999                 break;
7000
7001               default:
7002                 gold_unreachable();
7003               }
7004           }
7005         gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7006                                _("unsupported gd_to_ie relaxation on %u"),
7007                                r_type);
7008       }
7009       break;
7010
7011     case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
7012     case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:  // Local-dynamic
7013       {
7014         if (tlsopt == tls::TLSOPT_TO_LE)
7015           {
7016             if (tls_segment == NULL)
7017               {
7018                 gold_assert(parameters->errors()->error_count() > 0
7019                             || issue_undefined_symbol_error(gsym));
7020                 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7021               }
7022             return this->tls_ld_to_le(relinfo, target, rela, r_type, view,
7023                                       psymval);
7024           }
7025
7026         gold_assert(tlsopt == tls::TLSOPT_NONE);
7027         // Relocate the field with the offset of the GOT entry for
7028         // the module index.
7029         typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7030         got_entry_address = (target->got_mod_index_entry(NULL, NULL, NULL) +
7031                              target->got_->address());
7032
7033         switch (r_type)
7034           {
7035           case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
7036             return aarch64_reloc_funcs::adrp(
7037               view, got_entry_address + addend, address);
7038             break;
7039
7040           case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
7041             return aarch64_reloc_funcs::template rela_general<32>(
7042               view, got_entry_address, addend, reloc_property);
7043             break;
7044
7045           default:
7046             gold_unreachable();
7047           }
7048       }
7049       break;
7050
7051     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
7052     case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7053     case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
7054     case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:  // Other local-dynamic
7055       {
7056         AArch64_address value = psymval->value(object, 0);
7057         if (tlsopt == tls::TLSOPT_TO_LE)
7058           {
7059             if (tls_segment == NULL)
7060               {
7061                 gold_assert(parameters->errors()->error_count() > 0
7062                             || issue_undefined_symbol_error(gsym));
7063                 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7064               }
7065           }
7066         switch (r_type)
7067           {
7068           case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
7069             return aarch64_reloc_funcs::movnz(view, value + addend,
7070                                               reloc_property);
7071             break;
7072
7073           case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7074           case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
7075           case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
7076             return aarch64_reloc_funcs::template rela_general<32>(
7077                 view, value, addend, reloc_property);
7078             break;
7079
7080           default:
7081             gold_unreachable();
7082           }
7083         // We should never reach here.
7084       }
7085       break;
7086
7087     case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7088     case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:  // Initial-exec
7089       {
7090         if (tlsopt == tls::TLSOPT_TO_LE)
7091           {
7092             if (tls_segment == NULL)
7093               {
7094                 gold_assert(parameters->errors()->error_count() > 0
7095                             || issue_undefined_symbol_error(gsym));
7096                 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7097               }
7098             return tls_ie_to_le(relinfo, target, rela, r_type, view,
7099                                 psymval);
7100           }
7101         tls_got_offset_type = GOT_TYPE_TLS_OFFSET;
7102
7103         // Firstly get the address for the got entry.
7104         typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7105         if (gsym != NULL)
7106           {
7107             gold_assert(gsym->has_got_offset(tls_got_offset_type));
7108             got_entry_address = target->got_->address() +
7109                                 gsym->got_offset(tls_got_offset_type);
7110           }
7111         else
7112           {
7113             unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7114             gold_assert(
7115                 object->local_has_got_offset(r_sym, tls_got_offset_type));
7116             got_entry_address = target->got_->address() +
7117                 object->local_got_offset(r_sym, tls_got_offset_type);
7118           }
7119         // Relocate the address into adrp/ld, adrp/add pair.
7120         switch (r_type)
7121           {
7122           case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7123             return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
7124                                              address);
7125             break;
7126           case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7127             return aarch64_reloc_funcs::template rela_general<32>(
7128               view, got_entry_address, addend, reloc_property);
7129           default:
7130             gold_unreachable();
7131           }
7132       }
7133       // We shall never reach here.
7134       break;
7135
7136     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
7137     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
7138     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7139     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7140     case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
7141     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
7142     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
7143     case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
7144       {
7145         gold_assert(tls_segment != NULL);
7146         AArch64_address value = psymval->value(object, 0);
7147
7148         if (!parameters->options().shared())
7149           {
7150             AArch64_address aligned_tcb_size =
7151                 align_address(target->tcb_size(),
7152                               tls_segment->maximum_alignment());
7153             value += aligned_tcb_size;
7154             switch (r_type)
7155               {
7156               case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
7157               case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
7158               case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7159                 return aarch64_reloc_funcs::movnz(view, value + addend,
7160                                                   reloc_property);
7161               default:
7162                 return aarch64_reloc_funcs::template
7163                   rela_general<32>(view,
7164                                    value,
7165                                    addend,
7166                                    reloc_property);
7167               }
7168           }
7169         else
7170           gold_error(_("%s: unsupported reloc %u "
7171                        "in non-static TLSLE mode."),
7172                      object->name().c_str(), r_type);
7173       }
7174       break;
7175
7176     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7177     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7178     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7179     case elfcpp::R_AARCH64_TLSDESC_CALL:
7180       {
7181         if (tlsopt == tls::TLSOPT_TO_LE)
7182           {
7183             if (tls_segment == NULL)
7184               {
7185                 gold_assert(parameters->errors()->error_count() > 0
7186                             || issue_undefined_symbol_error(gsym));
7187                 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7188               }
7189             return tls_desc_gd_to_le(relinfo, target, rela, r_type,
7190                                      view, psymval);
7191           }
7192         else
7193           {
7194             tls_got_offset_type = (tlsopt == tls::TLSOPT_TO_IE
7195                                    ? GOT_TYPE_TLS_OFFSET
7196                                    : GOT_TYPE_TLS_DESC);
7197             unsigned int got_tlsdesc_offset = 0;
7198             if (r_type != elfcpp::R_AARCH64_TLSDESC_CALL
7199                 && tlsopt == tls::TLSOPT_NONE)
7200               {
7201                 // We created GOT entries in the .got.tlsdesc portion of the
7202                 // .got.plt section, but the offset stored in the symbol is the
7203                 // offset within .got.tlsdesc.
7204                 got_tlsdesc_offset = (target->got_->data_size()
7205                                       + target->got_plt_section()->data_size());
7206               }
7207             typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7208             if (gsym != NULL)
7209               {
7210                 gold_assert(gsym->has_got_offset(tls_got_offset_type));
7211                 got_entry_address = target->got_->address()
7212                                     + got_tlsdesc_offset
7213                                     + gsym->got_offset(tls_got_offset_type);
7214               }
7215             else
7216               {
7217                 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7218                 gold_assert(
7219                     object->local_has_got_offset(r_sym, tls_got_offset_type));
7220                 got_entry_address = target->got_->address() +
7221                   got_tlsdesc_offset +
7222                   object->local_got_offset(r_sym, tls_got_offset_type);
7223               }
7224             if (tlsopt == tls::TLSOPT_TO_IE)
7225               {
7226                 if (tls_segment == NULL)
7227                   {
7228                     gold_assert(parameters->errors()->error_count() > 0
7229                                 || issue_undefined_symbol_error(gsym));
7230                     return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7231                   }
7232                 return tls_desc_gd_to_ie(relinfo, target, rela, r_type,
7233                                          view, psymval, got_entry_address,
7234                                          address);
7235               }
7236
7237             // Now do tlsdesc relocation.
7238             switch (r_type)
7239               {
7240               case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7241                 return aarch64_reloc_funcs::adrp(view,
7242                                                  got_entry_address + addend,
7243                                                  address);
7244                 break;
7245               case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7246               case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7247                 return aarch64_reloc_funcs::template rela_general<32>(
7248                   view, got_entry_address, addend, reloc_property);
7249                 break;
7250               case elfcpp::R_AARCH64_TLSDESC_CALL:
7251                 return aarch64_reloc_funcs::STATUS_OKAY;
7252                 break;
7253               default:
7254                 gold_unreachable();
7255               }
7256           }
7257         }
7258       break;
7259
7260     default:
7261       gold_error(_("%s: unsupported TLS reloc %u."),
7262                  object->name().c_str(), r_type);
7263     }
7264   return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7265 }  // End of relocate_tls.
7266
7267
7268 template<int size, bool big_endian>
7269 inline
7270 typename AArch64_relocate_functions<size, big_endian>::Status
7271 Target_aarch64<size, big_endian>::Relocate::tls_gd_to_le(
7272              const Relocate_info<size, big_endian>* relinfo,
7273              Target_aarch64<size, big_endian>* target,
7274              const elfcpp::Rela<size, big_endian>& rela,
7275              unsigned int r_type,
7276              unsigned char* view,
7277              const Symbol_value<size>* psymval)
7278 {
7279   typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7280   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7281   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7282
7283   Insntype* ip = reinterpret_cast<Insntype*>(view);
7284   Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
7285   Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
7286   Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
7287
7288   if (r_type == elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC)
7289     {
7290       // This is the 2nd relocs, optimization should already have been
7291       // done.
7292       gold_assert((insn1 & 0xfff00000) == 0x91400000);
7293       return aarch64_reloc_funcs::STATUS_OKAY;
7294     }
7295
7296   // The original sequence is -
7297   //   90000000        adrp    x0, 0 <main>
7298   //   91000000        add     x0, x0, #0x0
7299   //   94000000        bl      0 <__tls_get_addr>
7300   // optimized to sequence -
7301   //   d53bd040        mrs     x0, tpidr_el0
7302   //   91400000        add     x0, x0, #0x0, lsl #12
7303   //   91000000        add     x0, x0, #0x0
7304
7305   // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7306   // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
7307   // have to change "bl tls_get_addr", which does not have a corresponding tls
7308   // relocation type. So before proceeding, we need to make sure compiler
7309   // does not change the sequence.
7310   if(!(insn1 == 0x90000000      // adrp x0,0
7311        && insn2 == 0x91000000   // add x0, x0, #0x0
7312        && insn3 == 0x94000000)) // bl 0
7313     {
7314       // Ideally we should give up gd_to_le relaxation and do gd access.
7315       // However the gd_to_le relaxation decision has been made early
7316       // in the scan stage, where we did not allocate any GOT entry for
7317       // this symbol. Therefore we have to exit and report error now.
7318       gold_error(_("unexpected reloc insn sequence while relaxing "
7319                    "tls gd to le for reloc %u."), r_type);
7320       return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7321     }
7322
7323   // Write new insns.
7324   insn1 = 0xd53bd040;  // mrs x0, tpidr_el0
7325   insn2 = 0x91400000;  // add x0, x0, #0x0, lsl #12
7326   insn3 = 0x91000000;  // add x0, x0, #0x0
7327   elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
7328   elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
7329   elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
7330
7331   // Calculate tprel value.
7332   Output_segment* tls_segment = relinfo->layout->tls_segment();
7333   gold_assert(tls_segment != NULL);
7334   AArch64_address value = psymval->value(relinfo->object, 0);
7335   const elfcpp::Elf_Xword addend = rela.get_r_addend();
7336   AArch64_address aligned_tcb_size =
7337       align_address(target->tcb_size(), tls_segment->maximum_alignment());
7338   AArch64_address x = value + aligned_tcb_size;
7339
7340   // After new insns are written, apply TLSLE relocs.
7341   const AArch64_reloc_property* rp1 =
7342       aarch64_reloc_property_table->get_reloc_property(
7343           elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
7344   const AArch64_reloc_property* rp2 =
7345       aarch64_reloc_property_table->get_reloc_property(
7346           elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
7347   gold_assert(rp1 != NULL && rp2 != NULL);
7348
7349   typename aarch64_reloc_funcs::Status s1 =
7350       aarch64_reloc_funcs::template rela_general<32>(view + 4,
7351                                                      x,
7352                                                      addend,
7353                                                      rp1);
7354   if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
7355     return s1;
7356
7357   typename aarch64_reloc_funcs::Status s2 =
7358       aarch64_reloc_funcs::template rela_general<32>(view + 8,
7359                                                      x,
7360                                                      addend,
7361                                                      rp2);
7362
7363   this->skip_call_tls_get_addr_ = true;
7364   return s2;
7365 }  // End of tls_gd_to_le
7366
7367
7368 template<int size, bool big_endian>
7369 inline
7370 typename AArch64_relocate_functions<size, big_endian>::Status
7371 Target_aarch64<size, big_endian>::Relocate::tls_ld_to_le(
7372              const Relocate_info<size, big_endian>* relinfo,
7373              Target_aarch64<size, big_endian>* target,
7374              const elfcpp::Rela<size, big_endian>& rela,
7375              unsigned int r_type,
7376              unsigned char* view,
7377              const Symbol_value<size>* psymval)
7378 {
7379   typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7380   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7381   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7382
7383   Insntype* ip = reinterpret_cast<Insntype*>(view);
7384   Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
7385   Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
7386   Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
7387
7388   if (r_type == elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC)
7389     {
7390       // This is the 2nd relocs, optimization should already have been
7391       // done.
7392       gold_assert((insn1 & 0xfff00000) == 0x91400000);
7393       return aarch64_reloc_funcs::STATUS_OKAY;
7394     }
7395
7396   // The original sequence is -
7397   //   90000000        adrp    x0, 0 <main>
7398   //   91000000        add     x0, x0, #0x0
7399   //   94000000        bl      0 <__tls_get_addr>
7400   // optimized to sequence -
7401   //   d53bd040        mrs     x0, tpidr_el0
7402   //   91400000        add     x0, x0, #0x0, lsl #12
7403   //   91000000        add     x0, x0, #0x0
7404
7405   // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7406   // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
7407   // have to change "bl tls_get_addr", which does not have a corresponding tls
7408   // relocation type. So before proceeding, we need to make sure compiler
7409   // does not change the sequence.
7410   if(!(insn1 == 0x90000000      // adrp x0,0
7411        && insn2 == 0x91000000   // add x0, x0, #0x0
7412        && insn3 == 0x94000000)) // bl 0
7413     {
7414       // Ideally we should give up gd_to_le relaxation and do gd access.
7415       // However the gd_to_le relaxation decision has been made early
7416       // in the scan stage, where we did not allocate any GOT entry for
7417       // this symbol. Therefore we have to exit and report error now.
7418       gold_error(_("unexpected reloc insn sequence while relaxing "
7419                    "tls gd to le for reloc %u."), r_type);
7420       return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7421     }
7422
7423   // Write new insns.
7424   insn1 = 0xd53bd040;  // mrs x0, tpidr_el0
7425   insn2 = 0x91400000;  // add x0, x0, #0x0, lsl #12
7426   insn3 = 0x91000000;  // add x0, x0, #0x0
7427   elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
7428   elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
7429   elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
7430
7431   // Calculate tprel value.
7432   Output_segment* tls_segment = relinfo->layout->tls_segment();
7433   gold_assert(tls_segment != NULL);
7434   AArch64_address value = psymval->value(relinfo->object, 0);
7435   const elfcpp::Elf_Xword addend = rela.get_r_addend();
7436   AArch64_address aligned_tcb_size =
7437       align_address(target->tcb_size(), tls_segment->maximum_alignment());
7438   AArch64_address x = value + aligned_tcb_size;
7439
7440   // After new insns are written, apply TLSLE relocs.
7441   const AArch64_reloc_property* rp1 =
7442       aarch64_reloc_property_table->get_reloc_property(
7443           elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
7444   const AArch64_reloc_property* rp2 =
7445       aarch64_reloc_property_table->get_reloc_property(
7446           elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
7447   gold_assert(rp1 != NULL && rp2 != NULL);
7448
7449   typename aarch64_reloc_funcs::Status s1 =
7450       aarch64_reloc_funcs::template rela_general<32>(view + 4,
7451                                                      x,
7452                                                      addend,
7453                                                      rp1);
7454   if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
7455     return s1;
7456
7457   typename aarch64_reloc_funcs::Status s2 =
7458       aarch64_reloc_funcs::template rela_general<32>(view + 8,
7459                                                      x,
7460                                                      addend,
7461                                                      rp2);
7462
7463   this->skip_call_tls_get_addr_ = true;
7464   return s2;
7465
7466 }  // End of tls_ld_to_le
7467
7468 template<int size, bool big_endian>
7469 inline
7470 typename AArch64_relocate_functions<size, big_endian>::Status
7471 Target_aarch64<size, big_endian>::Relocate::tls_ie_to_le(
7472              const Relocate_info<size, big_endian>* relinfo,
7473              Target_aarch64<size, big_endian>* target,
7474              const elfcpp::Rela<size, big_endian>& rela,
7475              unsigned int r_type,
7476              unsigned char* view,
7477              const Symbol_value<size>* psymval)
7478 {
7479   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7480   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7481   typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7482
7483   AArch64_address value = psymval->value(relinfo->object, 0);
7484   Output_segment* tls_segment = relinfo->layout->tls_segment();
7485   AArch64_address aligned_tcb_address =
7486       align_address(target->tcb_size(), tls_segment->maximum_alignment());
7487   const elfcpp::Elf_Xword addend = rela.get_r_addend();
7488   AArch64_address x = value + addend + aligned_tcb_address;
7489   // "x" is the offset to tp, we can only do this if x is within
7490   // range [0, 2^32-1]
7491   if (!(size == 32 || (size == 64 && (static_cast<uint64_t>(x) >> 32) == 0)))
7492     {
7493       gold_error(_("TLS variable referred by reloc %u is too far from TP."),
7494                  r_type);
7495       return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7496     }
7497
7498   Insntype* ip = reinterpret_cast<Insntype*>(view);
7499   Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
7500   unsigned int regno;
7501   Insntype newinsn;
7502   if (r_type == elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21)
7503     {
7504       // Generate movz.
7505       regno = (insn & 0x1f);
7506       newinsn = (0xd2a00000 | regno) | (((x >> 16) & 0xffff) << 5);
7507     }
7508   else if (r_type == elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
7509     {
7510       // Generate movk.
7511       regno = (insn & 0x1f);
7512       gold_assert(regno == ((insn >> 5) & 0x1f));
7513       newinsn = (0xf2800000 | regno) | ((x & 0xffff) << 5);
7514     }
7515   else
7516     gold_unreachable();
7517
7518   elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7519   return aarch64_reloc_funcs::STATUS_OKAY;
7520 }  // End of tls_ie_to_le
7521
7522
7523 template<int size, bool big_endian>
7524 inline
7525 typename AArch64_relocate_functions<size, big_endian>::Status
7526 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_le(
7527              const Relocate_info<size, big_endian>* relinfo,
7528              Target_aarch64<size, big_endian>* target,
7529              const elfcpp::Rela<size, big_endian>& rela,
7530              unsigned int r_type,
7531              unsigned char* view,
7532              const Symbol_value<size>* psymval)
7533 {
7534   typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7535   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7536   typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7537
7538   // TLSDESC-GD sequence is like:
7539   //   adrp  x0, :tlsdesc:v1
7540   //   ldr   x1, [x0, #:tlsdesc_lo12:v1]
7541   //   add   x0, x0, :tlsdesc_lo12:v1
7542   //   .tlsdesccall    v1
7543   //   blr   x1
7544   // After desc_gd_to_le optimization, the sequence will be like:
7545   //   movz  x0, #0x0, lsl #16
7546   //   movk  x0, #0x10
7547   //   nop
7548   //   nop
7549
7550   // Calculate tprel value.
7551   Output_segment* tls_segment = relinfo->layout->tls_segment();
7552   gold_assert(tls_segment != NULL);
7553   Insntype* ip = reinterpret_cast<Insntype*>(view);
7554   const elfcpp::Elf_Xword addend = rela.get_r_addend();
7555   AArch64_address value = psymval->value(relinfo->object, addend);
7556   AArch64_address aligned_tcb_size =
7557       align_address(target->tcb_size(), tls_segment->maximum_alignment());
7558   AArch64_address x = value + aligned_tcb_size;
7559   // x is the offset to tp, we can only do this if x is within range
7560   // [0, 2^32-1]. If x is out of range, fail and exit.
7561   if (size == 64 && (static_cast<uint64_t>(x) >> 32) != 0)
7562     {
7563       gold_error(_("TLS variable referred by reloc %u is too far from TP. "
7564                    "We Can't do gd_to_le relaxation.\n"), r_type);
7565       return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7566     }
7567   Insntype newinsn;
7568   switch (r_type)
7569     {
7570     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7571     case elfcpp::R_AARCH64_TLSDESC_CALL:
7572       // Change to nop
7573       newinsn = 0xd503201f;
7574       break;
7575
7576     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7577       // Change to movz.
7578       newinsn = 0xd2a00000 | (((x >> 16) & 0xffff) << 5);
7579       break;
7580
7581     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7582       // Change to movk.
7583       newinsn = 0xf2800000 | ((x & 0xffff) << 5);
7584       break;
7585
7586     default:
7587       gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
7588                  r_type);
7589       gold_unreachable();
7590     }
7591   elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7592   return aarch64_reloc_funcs::STATUS_OKAY;
7593 }  // End of tls_desc_gd_to_le
7594
7595
7596 template<int size, bool big_endian>
7597 inline
7598 typename AArch64_relocate_functions<size, big_endian>::Status
7599 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_ie(
7600              const Relocate_info<size, big_endian>* /* relinfo */,
7601              Target_aarch64<size, big_endian>* /* target */,
7602              const elfcpp::Rela<size, big_endian>& rela,
7603              unsigned int r_type,
7604              unsigned char* view,
7605              const Symbol_value<size>* /* psymval */,
7606              typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address,
7607              typename elfcpp::Elf_types<size>::Elf_Addr address)
7608 {
7609   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7610   typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7611
7612   // TLSDESC-GD sequence is like:
7613   //   adrp  x0, :tlsdesc:v1
7614   //   ldr   x1, [x0, #:tlsdesc_lo12:v1]
7615   //   add   x0, x0, :tlsdesc_lo12:v1
7616   //   .tlsdesccall    v1
7617   //   blr   x1
7618   // After desc_gd_to_ie optimization, the sequence will be like:
7619   //   adrp  x0, :tlsie:v1
7620   //   ldr   x0, [x0, :tlsie_lo12:v1]
7621   //   nop
7622   //   nop
7623
7624   Insntype* ip = reinterpret_cast<Insntype*>(view);
7625   const elfcpp::Elf_Xword addend = rela.get_r_addend();
7626   Insntype newinsn;
7627   switch (r_type)
7628     {
7629     case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7630     case elfcpp::R_AARCH64_TLSDESC_CALL:
7631       // Change to nop
7632       newinsn = 0xd503201f;
7633       elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7634       break;
7635
7636     case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7637       {
7638         return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
7639                                          address);
7640       }
7641       break;
7642
7643     case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7644       {
7645        // Set ldr target register to be x0.
7646        Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
7647        insn &= 0xffffffe0;
7648        elfcpp::Swap<32, big_endian>::writeval(ip, insn);
7649        // Do relocation.
7650         const AArch64_reloc_property* reloc_property =
7651             aarch64_reloc_property_table->get_reloc_property(
7652               elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
7653         return aarch64_reloc_funcs::template rela_general<32>(
7654                  view, got_entry_address, addend, reloc_property);
7655       }
7656       break;
7657
7658     default:
7659       gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
7660                  r_type);
7661       gold_unreachable();
7662     }
7663   return aarch64_reloc_funcs::STATUS_OKAY;
7664 }  // End of tls_desc_gd_to_ie
7665
7666 // Relocate section data.
7667
7668 template<int size, bool big_endian>
7669 void
7670 Target_aarch64<size, big_endian>::relocate_section(
7671     const Relocate_info<size, big_endian>* relinfo,
7672     unsigned int sh_type,
7673     const unsigned char* prelocs,
7674     size_t reloc_count,
7675     Output_section* output_section,
7676     bool needs_special_offset_handling,
7677     unsigned char* view,
7678     typename elfcpp::Elf_types<size>::Elf_Addr address,
7679     section_size_type view_size,
7680     const Reloc_symbol_changes* reloc_symbol_changes)
7681 {
7682   gold_assert(sh_type == elfcpp::SHT_RELA);
7683   typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
7684   gold::relocate_section<size, big_endian, Target_aarch64, elfcpp::SHT_RELA,
7685                          AArch64_relocate, gold::Default_comdat_behavior>(
7686     relinfo,
7687     this,
7688     prelocs,
7689     reloc_count,
7690     output_section,
7691     needs_special_offset_handling,
7692     view,
7693     address,
7694     view_size,
7695     reloc_symbol_changes);
7696 }
7697
7698 // Return the size of a relocation while scanning during a relocatable
7699 // link.
7700
7701 template<int size, bool big_endian>
7702 unsigned int
7703 Target_aarch64<size, big_endian>::Relocatable_size_for_reloc::
7704 get_size_for_reloc(
7705     unsigned int ,
7706     Relobj* )
7707 {
7708   // We will never support SHT_REL relocations.
7709   gold_unreachable();
7710   return 0;
7711 }
7712
7713 // Scan the relocs during a relocatable link.
7714
7715 template<int size, bool big_endian>
7716 void
7717 Target_aarch64<size, big_endian>::scan_relocatable_relocs(
7718     Symbol_table* symtab,
7719     Layout* layout,
7720     Sized_relobj_file<size, big_endian>* object,
7721     unsigned int data_shndx,
7722     unsigned int sh_type,
7723     const unsigned char* prelocs,
7724     size_t reloc_count,
7725     Output_section* output_section,
7726     bool needs_special_offset_handling,
7727     size_t local_symbol_count,
7728     const unsigned char* plocal_symbols,
7729     Relocatable_relocs* rr)
7730 {
7731   gold_assert(sh_type == elfcpp::SHT_RELA);
7732
7733   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
7734     Relocatable_size_for_reloc> Scan_relocatable_relocs;
7735
7736   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7737       Scan_relocatable_relocs>(
7738     symtab,
7739     layout,
7740     object,
7741     data_shndx,
7742     prelocs,
7743     reloc_count,
7744     output_section,
7745     needs_special_offset_handling,
7746     local_symbol_count,
7747     plocal_symbols,
7748     rr);
7749 }
7750
7751 // Relocate a section during a relocatable link.
7752
7753 template<int size, bool big_endian>
7754 void
7755 Target_aarch64<size, big_endian>::relocate_relocs(
7756     const Relocate_info<size, big_endian>* relinfo,
7757     unsigned int sh_type,
7758     const unsigned char* prelocs,
7759     size_t reloc_count,
7760     Output_section* output_section,
7761     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7762     const Relocatable_relocs* rr,
7763     unsigned char* view,
7764     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
7765     section_size_type view_size,
7766     unsigned char* reloc_view,
7767     section_size_type reloc_view_size)
7768 {
7769   gold_assert(sh_type == elfcpp::SHT_RELA);
7770
7771   gold::relocate_relocs<size, big_endian, elfcpp::SHT_RELA>(
7772     relinfo,
7773     prelocs,
7774     reloc_count,
7775     output_section,
7776     offset_in_output_section,
7777     rr,
7778     view,
7779     view_address,
7780     view_size,
7781     reloc_view,
7782     reloc_view_size);
7783 }
7784
7785
7786 // Return whether this is a 3-insn erratum sequence.
7787
7788 template<int size, bool big_endian>
7789 bool
7790 Target_aarch64<size, big_endian>::is_erratum_843419_sequence(
7791     typename elfcpp::Swap<32,big_endian>::Valtype insn1,
7792     typename elfcpp::Swap<32,big_endian>::Valtype insn2,
7793     typename elfcpp::Swap<32,big_endian>::Valtype insn3)
7794 {
7795   unsigned rt1, rt2;
7796   bool load, pair;
7797
7798   // The 2nd insn is a single register load or store; or register pair
7799   // store.
7800   if (Insn_utilities::aarch64_mem_op_p(insn2, &rt1, &rt2, &pair, &load)
7801       && (!pair || (pair && !load)))
7802     {
7803       // The 3rd insn is a load or store instruction from the "Load/store
7804       // register (unsigned immediate)" encoding class, using Rn as the
7805       // base address register.
7806       if (Insn_utilities::aarch64_ldst_uimm(insn3)
7807           && (Insn_utilities::aarch64_rn(insn3)
7808               == Insn_utilities::aarch64_rd(insn1)))
7809         return true;
7810     }
7811   return false;
7812 }
7813
7814
7815 // Return whether this is a 835769 sequence.
7816 // (Similarly implemented as in elfnn-aarch64.c.)
7817
7818 template<int size, bool big_endian>
7819 bool
7820 Target_aarch64<size, big_endian>::is_erratum_835769_sequence(
7821     typename elfcpp::Swap<32,big_endian>::Valtype insn1,
7822     typename elfcpp::Swap<32,big_endian>::Valtype insn2)
7823 {
7824   uint32_t rt;
7825   uint32_t rt2;
7826   uint32_t rn;
7827   uint32_t rm;
7828   uint32_t ra;
7829   bool pair;
7830   bool load;
7831
7832   if (Insn_utilities::aarch64_mlxl(insn2)
7833       && Insn_utilities::aarch64_mem_op_p (insn1, &rt, &rt2, &pair, &load))
7834     {
7835       /* Any SIMD memory op is independent of the subsequent MLA
7836          by definition of the erratum.  */
7837       if (Insn_utilities::aarch64_bit(insn1, 26))
7838         return true;
7839
7840       /* If not SIMD, check for integer memory ops and MLA relationship.  */
7841       rn = Insn_utilities::aarch64_rn(insn2);
7842       ra = Insn_utilities::aarch64_ra(insn2);
7843       rm = Insn_utilities::aarch64_rm(insn2);
7844
7845       /* If this is a load and there's a true(RAW) dependency, we are safe
7846          and this is not an erratum sequence.  */
7847       if (load &&
7848           (rt == rn || rt == rm || rt == ra
7849            || (pair && (rt2 == rn || rt2 == rm || rt2 == ra))))
7850         return false;
7851
7852       /* We conservatively put out stubs for all other cases (including
7853          writebacks).  */
7854       return true;
7855     }
7856
7857   return false;
7858 }
7859
7860
7861 // Helper method to create erratum stub for ST_E_843419 and ST_E_835769.
7862
7863 template<int size, bool big_endian>
7864 void
7865 Target_aarch64<size, big_endian>::create_erratum_stub(
7866     AArch64_relobj<size, big_endian>* relobj,
7867     unsigned int shndx,
7868     section_size_type erratum_insn_offset,
7869     Address erratum_address,
7870     typename Insn_utilities::Insntype erratum_insn,
7871     int erratum_type)
7872 {
7873   gold_assert(erratum_type == ST_E_843419 || erratum_type == ST_E_835769);
7874   The_stub_table* stub_table = relobj->stub_table(shndx);
7875   gold_assert(stub_table != NULL);
7876   if (stub_table->find_erratum_stub(relobj,
7877                                     shndx,
7878                                     erratum_insn_offset) == NULL)
7879     {
7880       const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
7881       The_erratum_stub* stub = new The_erratum_stub(
7882         relobj, erratum_type, shndx, erratum_insn_offset);
7883       stub->set_erratum_insn(erratum_insn);
7884       stub->set_erratum_address(erratum_address);
7885       // For erratum ST_E_843419 and ST_E_835769, the destination address is
7886       // always the next insn after erratum insn.
7887       stub->set_destination_address(erratum_address + BPI);
7888       stub_table->add_erratum_stub(stub);
7889     }
7890 }
7891
7892
7893 // Scan erratum for section SHNDX range [output_address + span_start,
7894 // output_address + span_end). Note here we do not share the code with
7895 // scan_erratum_843419_span function, because for 843419 we optimize by only
7896 // scanning the last few insns of a page, whereas for 835769, we need to scan
7897 // every insn.
7898
7899 template<int size, bool big_endian>
7900 void
7901 Target_aarch64<size, big_endian>::scan_erratum_835769_span(
7902     AArch64_relobj<size, big_endian>*  relobj,
7903     unsigned int shndx,
7904     const section_size_type span_start,
7905     const section_size_type span_end,
7906     unsigned char* input_view,
7907     Address output_address)
7908 {
7909   typedef typename Insn_utilities::Insntype Insntype;
7910
7911   const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
7912
7913   // Adjust output_address and view to the start of span.
7914   output_address += span_start;
7915   input_view += span_start;
7916
7917   section_size_type span_length = span_end - span_start;
7918   section_size_type offset = 0;
7919   for (offset = 0; offset + BPI < span_length; offset += BPI)
7920     {
7921       Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
7922       Insntype insn1 = ip[0];
7923       Insntype insn2 = ip[1];
7924       if (is_erratum_835769_sequence(insn1, insn2))
7925         {
7926           Insntype erratum_insn = insn2;
7927           // "span_start + offset" is the offset for insn1. So for insn2, it is
7928           // "span_start + offset + BPI".
7929           section_size_type erratum_insn_offset = span_start + offset + BPI;
7930           Address erratum_address = output_address + offset + BPI;
7931           gold_warning(_("Erratum 835769 found and fixed at \"%s\", "
7932                          "section %d, offset 0x%08x."),
7933                        relobj->name().c_str(), shndx,
7934                        (unsigned int)(span_start + offset));
7935
7936           this->create_erratum_stub(relobj, shndx,
7937                                     erratum_insn_offset, erratum_address,
7938                                     erratum_insn, ST_E_835769);
7939           offset += BPI;  // Skip mac insn.
7940         }
7941     }
7942 }  // End of "Target_aarch64::scan_erratum_835769_span".
7943
7944
7945 // Scan erratum for section SHNDX range
7946 // [output_address + span_start, output_address + span_end).
7947
7948 template<int size, bool big_endian>
7949 void
7950 Target_aarch64<size, big_endian>::scan_erratum_843419_span(
7951     AArch64_relobj<size, big_endian>*  relobj,
7952     unsigned int shndx,
7953     const section_size_type span_start,
7954     const section_size_type span_end,
7955     unsigned char* input_view,
7956     Address output_address)
7957 {
7958   typedef typename Insn_utilities::Insntype Insntype;
7959
7960   // Adjust output_address and view to the start of span.
7961   output_address += span_start;
7962   input_view += span_start;
7963
7964   if ((output_address & 0x03) != 0)
7965     return;
7966
7967   section_size_type offset = 0;
7968   section_size_type span_length = span_end - span_start;
7969   // The first instruction must be ending at 0xFF8 or 0xFFC.
7970   unsigned int page_offset = output_address & 0xFFF;
7971   // Make sure starting position, that is "output_address+offset",
7972   // starts at page position 0xff8 or 0xffc.
7973   if (page_offset < 0xff8)
7974     offset = 0xff8 - page_offset;
7975   while (offset + 3 * Insn_utilities::BYTES_PER_INSN <= span_length)
7976     {
7977       Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
7978       Insntype insn1 = ip[0];
7979       if (Insn_utilities::is_adrp(insn1))
7980         {
7981           Insntype insn2 = ip[1];
7982           Insntype insn3 = ip[2];
7983           Insntype erratum_insn;
7984           unsigned insn_offset;
7985           bool do_report = false;
7986           if (is_erratum_843419_sequence(insn1, insn2, insn3))
7987             {
7988               do_report = true;
7989               erratum_insn = insn3;
7990               insn_offset = 2 * Insn_utilities::BYTES_PER_INSN;
7991             }
7992           else if (offset + 4 * Insn_utilities::BYTES_PER_INSN <= span_length)
7993             {
7994               // Optionally we can have an insn between ins2 and ins3
7995               Insntype insn_opt = ip[2];
7996               // And insn_opt must not be a branch.
7997               if (!Insn_utilities::aarch64_b(insn_opt)
7998                   && !Insn_utilities::aarch64_bl(insn_opt)
7999                   && !Insn_utilities::aarch64_blr(insn_opt)
8000                   && !Insn_utilities::aarch64_br(insn_opt))
8001                 {
8002                   // And insn_opt must not write to dest reg in insn1. However
8003                   // we do a conservative scan, which means we may fix/report
8004                   // more than necessary, but it doesn't hurt.
8005
8006                   Insntype insn4 = ip[3];
8007                   if (is_erratum_843419_sequence(insn1, insn2, insn4))
8008                     {
8009                       do_report = true;
8010                       erratum_insn = insn4;
8011                       insn_offset = 3 * Insn_utilities::BYTES_PER_INSN;
8012                     }
8013                 }
8014             }
8015           if (do_report)
8016             {
8017               gold_warning(_("Erratum 843419 found and fixed at \"%s\", "
8018                              "section %d, offset 0x%08x."),
8019                            relobj->name().c_str(), shndx,
8020                            (unsigned int)(span_start + offset));
8021               unsigned int erratum_insn_offset =
8022                 span_start + offset + insn_offset;
8023               Address erratum_address =
8024                 output_address + offset + insn_offset;
8025               create_erratum_stub(relobj, shndx,
8026                                   erratum_insn_offset, erratum_address,
8027                                   erratum_insn, ST_E_843419);
8028             }
8029         }
8030
8031       // Advance to next candidate instruction. We only consider instruction
8032       // sequences starting at a page offset of 0xff8 or 0xffc.
8033       page_offset = (output_address + offset) & 0xfff;
8034       if (page_offset == 0xff8)
8035         offset += 4;
8036       else  // (page_offset == 0xffc), we move to next page's 0xff8.
8037         offset += 0xffc;
8038     }
8039 }  // End of "Target_aarch64::scan_erratum_843419_span".
8040
8041
8042 // The selector for aarch64 object files.
8043
8044 template<int size, bool big_endian>
8045 class Target_selector_aarch64 : public Target_selector
8046 {
8047  public:
8048   Target_selector_aarch64();
8049
8050   virtual Target*
8051   do_instantiate_target()
8052   { return new Target_aarch64<size, big_endian>(); }
8053 };
8054
8055 template<>
8056 Target_selector_aarch64<32, true>::Target_selector_aarch64()
8057   : Target_selector(elfcpp::EM_AARCH64, 32, true,
8058                     "elf32-bigaarch64", "aarch64_elf32_be_vec")
8059 { }
8060
8061 template<>
8062 Target_selector_aarch64<32, false>::Target_selector_aarch64()
8063   : Target_selector(elfcpp::EM_AARCH64, 32, false,
8064                     "elf32-littleaarch64", "aarch64_elf32_le_vec")
8065 { }
8066
8067 template<>
8068 Target_selector_aarch64<64, true>::Target_selector_aarch64()
8069   : Target_selector(elfcpp::EM_AARCH64, 64, true,
8070                     "elf64-bigaarch64", "aarch64_elf64_be_vec")
8071 { }
8072
8073 template<>
8074 Target_selector_aarch64<64, false>::Target_selector_aarch64()
8075   : Target_selector(elfcpp::EM_AARCH64, 64, false,
8076                     "elf64-littleaarch64", "aarch64_elf64_le_vec")
8077 { }
8078
8079 Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
8080 Target_selector_aarch64<32, false> target_selector_aarch64elf32;
8081 Target_selector_aarch64<64, true> target_selector_aarch64elfb;
8082 Target_selector_aarch64<64, false> target_selector_aarch64elf;
8083
8084 } // End anonymous namespace.