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