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