1 // aarch64.cc -- aarch64 target support for gold.
3 // Copyright (C) 2014-2017 Free Software Foundation, Inc.
4 // Written by Jing Yu <jingyu@google.com> and Han Shen <shenhan@google.com>.
6 // This file is part of gold.
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.
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.
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.
31 #include "parameters.h"
38 #include "copy-relocs.h"
40 #include "target-reloc.h"
41 #include "target-select.h"
47 #include "aarch64-reloc-property.h"
49 // The first three .got.plt entries are reserved.
50 const int32_t AARCH64_GOTPLT_RESERVE_COUNT = 3;
58 template<int size, bool big_endian>
59 class Output_data_plt_aarch64;
61 template<int size, bool big_endian>
62 class Output_data_plt_aarch64_standard;
64 template<int size, bool big_endian>
67 template<int size, bool big_endian>
68 class AArch64_relocate_functions;
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.
74 template<bool big_endian>
75 class AArch64_insn_utilities
78 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
80 static const int BYTES_PER_INSN;
82 // Zero register encoding - 31.
83 static const unsigned int AARCH64_ZR;
86 aarch64_bit(Insntype insn, int pos)
87 { return ((1 << pos) & insn) >> pos; }
90 aarch64_bits(Insntype insn, int pos, int l)
91 { return (insn >> pos) & ((1 << l) - 1); }
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.
96 aarch64_op31(Insntype insn)
97 { return aarch64_bits(insn, 21, 3); }
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.
102 aarch64_ra(Insntype insn)
103 { return aarch64_bits(insn, 10, 5); }
106 is_adr(const Insntype insn)
107 { return (insn & 0x9F000000) == 0x10000000; }
110 is_adrp(const Insntype insn)
111 { return (insn & 0x9F000000) == 0x90000000; }
114 is_mrs_tpidr_el0(const Insntype insn)
115 { return (insn & 0xFFFFFFE0) == 0xd53bd040; }
118 aarch64_rm(const Insntype insn)
119 { return aarch64_bits(insn, 16, 5); }
122 aarch64_rn(const Insntype insn)
123 { return aarch64_bits(insn, 5, 5); }
126 aarch64_rd(const Insntype insn)
127 { return aarch64_bits(insn, 0, 5); }
130 aarch64_rt(const Insntype insn)
131 { return aarch64_bits(insn, 0, 5); }
134 aarch64_rt2(const Insntype insn)
135 { return aarch64_bits(insn, 10, 5); }
137 // Encode imm21 into adr. Signed imm21 is in the range of [-1M, 1M).
139 aarch64_adr_encode_imm(Insntype adr, int imm21)
141 gold_assert(is_adr(adr));
142 gold_assert(-(1 << 20) <= imm21 && imm21 < (1 << 20));
143 const int mask19 = (1 << 19) - 1;
145 adr &= ~((mask19 << 5) | (mask2 << 29));
146 adr |= ((imm21 & mask2) << 29) | (((imm21 >> 2) & mask19) << 5);
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.
154 aarch64_adrp_decode_imm(const Insntype adrp)
156 const int mask19 = (1 << 19) - 1;
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 multiplied 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
167 return ((((uint64_t)(1) << 32) - msbt) << 33) | value;
171 aarch64_b(const Insntype insn)
172 { return (insn & 0xFC000000) == 0x14000000; }
175 aarch64_bl(const Insntype insn)
176 { return (insn & 0xFC000000) == 0x94000000; }
179 aarch64_blr(const Insntype insn)
180 { return (insn & 0xFFFFFC1F) == 0xD63F0000; }
183 aarch64_br(const Insntype insn)
184 { return (insn & 0xFFFFFC1F) == 0xD61F0000; }
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.
189 aarch64_ld(Insntype insn) { return aarch64_bit(insn, 22) == 1; }
192 aarch64_ldst(Insntype insn)
193 { return (insn & 0x0a000000) == 0x08000000; }
196 aarch64_ldst_ex(Insntype insn)
197 { return (insn & 0x3f000000) == 0x08000000; }
200 aarch64_ldst_pcrel(Insntype insn)
201 { return (insn & 0x3b000000) == 0x18000000; }
204 aarch64_ldst_nap(Insntype insn)
205 { return (insn & 0x3b800000) == 0x28000000; }
208 aarch64_ldstp_pi(Insntype insn)
209 { return (insn & 0x3b800000) == 0x28800000; }
212 aarch64_ldstp_o(Insntype insn)
213 { return (insn & 0x3b800000) == 0x29000000; }
216 aarch64_ldstp_pre(Insntype insn)
217 { return (insn & 0x3b800000) == 0x29800000; }
220 aarch64_ldst_ui(Insntype insn)
221 { return (insn & 0x3b200c00) == 0x38000000; }
224 aarch64_ldst_piimm(Insntype insn)
225 { return (insn & 0x3b200c00) == 0x38000400; }
228 aarch64_ldst_u(Insntype insn)
229 { return (insn & 0x3b200c00) == 0x38000800; }
232 aarch64_ldst_preimm(Insntype insn)
233 { return (insn & 0x3b200c00) == 0x38000c00; }
236 aarch64_ldst_ro(Insntype insn)
237 { return (insn & 0x3b200c00) == 0x38200800; }
240 aarch64_ldst_uimm(Insntype insn)
241 { return (insn & 0x3b000000) == 0x39000000; }
244 aarch64_ldst_simd_m(Insntype insn)
245 { return (insn & 0xbfbf0000) == 0x0c000000; }
248 aarch64_ldst_simd_m_pi(Insntype insn)
249 { return (insn & 0xbfa00000) == 0x0c800000; }
252 aarch64_ldst_simd_s(Insntype insn)
253 { return (insn & 0xbf9f0000) == 0x0d000000; }
256 aarch64_ldst_simd_s_pi(Insntype insn)
257 { return (insn & 0xbf800000) == 0x0d800000; }
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.
264 aarch64_mem_op_p(Insntype insn, unsigned int *rt, unsigned int *rt2,
265 bool *pair, bool *load)
273 /* Bail out quickly if INSN doesn't fall into the load-store
275 if (!aarch64_ldst (insn))
280 if (aarch64_ldst_ex (insn))
282 *rt = aarch64_rt (insn);
284 if (aarch64_bit (insn, 21) == 1)
287 *rt2 = aarch64_rt2 (insn);
289 *load = aarch64_ld (insn);
292 else if (aarch64_ldst_nap (insn)
293 || aarch64_ldstp_pi (insn)
294 || aarch64_ldstp_o (insn)
295 || aarch64_ldstp_pre (insn))
298 *rt = aarch64_rt (insn);
299 *rt2 = aarch64_rt2 (insn);
300 *load = aarch64_ld (insn);
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))
311 *rt = aarch64_rt (insn);
313 if (aarch64_ldst_pcrel (insn))
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);
322 else if (aarch64_ldst_simd_m (insn)
323 || aarch64_ldst_simd_m_pi (insn))
325 *rt = aarch64_rt (insn);
326 *load = aarch64_bit (insn, 22);
327 opcode = (insn >> 12) & 0xf;
354 else if (aarch64_ldst_simd_s (insn)
355 || aarch64_ldst_simd_s_pi (insn))
357 *rt = aarch64_rt (insn);
358 r = (insn >> 21) & 1;
359 *load = aarch64_bit (insn, 22);
360 opcode = (insn >> 13) & 0x7;
372 *rt2 = *rt + (r == 0 ? 2 : 3);
380 *rt2 = *rt + (r == 0 ? 2 : 3);
389 } // End of "aarch64_mem_op_p".
391 // Return true if INSN is mac insn.
393 aarch64_mac(Insntype insn)
394 { return (insn & 0xff000000) == 0x9b000000; }
396 // Return true if INSN is multiply-accumulate.
397 // (This is similar to implementaton in elfnn-aarch64.c.)
399 aarch64_mlxl(Insntype insn)
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
406 && aarch64_ra(insn) != AARCH64_ZR)
412 }; // End of "AArch64_insn_utilities".
415 // Insn length in byte.
417 template<bool big_endian>
418 const int AArch64_insn_utilities<big_endian>::BYTES_PER_INSN = 4;
421 // Zero register encoding - 31.
423 template<bool big_endian>
424 const unsigned int AArch64_insn_utilities<big_endian>::AARCH64_ZR = 0x1f;
427 // Output_data_got_aarch64 class.
429 template<int size, bool big_endian>
430 class Output_data_got_aarch64 : public Output_data_got<size, big_endian>
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)
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.
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)); }
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.
451 add_static_reloc(unsigned int got_offset, unsigned int r_type,
452 Sized_relobj_file<size, big_endian>* relobj,
455 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
461 // Write out the GOT table.
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);
471 // Handling static relocs
472 if (this->static_relocs_.empty())
475 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
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);
483 Output_segment* tls_segment = this->layout_->tls_segment();
484 gold_assert(tls_segment != NULL);
486 AArch64_address aligned_tcb_address =
487 align_address(Target_aarch64<size, big_endian>::TCB_SIZE,
488 tls_segment->maximum_alignment());
490 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
492 Static_reloc& reloc(this->static_relocs_[i]);
493 AArch64_address value;
495 if (!reloc.symbol_is_global())
497 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
498 const Symbol_value<size>* psymval =
499 reloc.relobj()->local_symbol(reloc.index());
501 // We are doing static linking. Issue an error and skip this
502 // relocation if the symbol is undefined or in a discarded_section.
504 unsigned int shndx = psymval->input_shndx(&is_ordinary);
505 if ((shndx == elfcpp::SHN_UNDEF)
507 && shndx != elfcpp::SHN_UNDEF
508 && !object->is_section_included(shndx)
509 && !this->symbol_table_->is_section_folded(object, shndx)))
511 gold_error(_("undefined or discarded local symbol %u from "
512 " object %s in GOT"),
513 reloc.index(), reloc.relobj()->name().c_str());
516 value = psymval->value(object, 0);
520 const Symbol* gsym = reloc.symbol();
521 gold_assert(gsym != NULL);
522 if (gsym->is_forwarder())
523 gsym = this->symbol_table_->resolve_forwards(gsym);
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())
532 gold_error(_("undefined or discarded symbol %s in GOT"),
537 if (!gsym->is_weak_undefined())
539 const Sized_symbol<size>* sym =
540 static_cast<const Sized_symbol<size>*>(gsym);
541 value = sym->value();
547 unsigned got_offset = reloc.got_offset();
548 gold_assert(got_offset < oview_size);
550 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
551 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
553 switch (reloc.r_type())
555 case elfcpp::R_AARCH64_TLS_DTPREL64:
558 case elfcpp::R_AARCH64_TLS_TPREL64:
559 x = value + aligned_tcb_address;
564 elfcpp::Swap<size, big_endian>::writeval(wv, x);
567 of->write_output_view(offset, oview_size, oview);
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.
577 // This class represent dynamic relocations that need to be applied by
578 // gold because we are using TLS relocations in a static link.
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; }
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)
590 this->u_.local.relobj = relobj;
591 this->u_.local.index = index;
594 // Return the GOT offset.
597 { return this->got_offset_; }
602 { return this->r_type_; }
604 // Whether the symbol is global or not.
606 symbol_is_global() const
607 { return this->symbol_is_global_; }
609 // For a relocation against a global symbol, the global symbol.
613 gold_assert(this->symbol_is_global_);
614 return this->u_.global.symbol;
617 // For a relocation against a local symbol, the defining object.
618 Sized_relobj_file<size, big_endian>*
621 gold_assert(!this->symbol_is_global_);
622 return this->u_.local.relobj;
625 // For a relocation against a local symbol, the local symbol index.
629 gold_assert(!this->symbol_is_global_);
630 return this->u_.local.index;
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.
645 // For a global symbol, the symbol itself.
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.
656 }; // End of inner class Static_reloc
658 std::vector<Static_reloc> static_relocs_;
659 }; // End of Output_data_got_aarch64
662 template<int size, bool big_endian>
663 class AArch64_input_section;
666 template<int size, bool big_endian>
667 class AArch64_output_section;
670 template<int size, bool big_endian>
671 class AArch64_relobj;
674 // Stub type enum constants.
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.
685 // Using ldr-absolute-address/br-register, 4 insns with 1 mem access,
686 // unlimited in jump distance.
687 ST_LONG_BRANCH_ABS = 2,
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,
693 // Stub for erratum 843419 handling.
696 // Stub for erratum 835769 handling.
699 // Number of total stub types.
704 // Struct that wraps insns for a particular stub. All stub templates are
705 // created/initialized as constants by Stub_template_repertoire.
707 template<bool big_endian>
710 const typename AArch64_insn_utilities<big_endian>::Insntype* insns;
715 // Simple singleton class that creates/initializes/stores all types of stub
718 template<bool big_endian>
719 class Stub_template_repertoire
722 typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
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)
728 static Stub_template_repertoire<big_endian> singleton;
729 return singleton.stub_templates_[type];
733 // Constructor - creates/initializes all stub templates.
734 Stub_template_repertoire();
735 ~Stub_template_repertoire()
738 // Disallowing copy ctor and copy assignment operator.
739 Stub_template_repertoire(Stub_template_repertoire&);
740 Stub_template_repertoire& operator=(Stub_template_repertoire&);
742 // Data that stores all insn templates.
743 const Stub_template<big_endian>* stub_templates_[ST_NUMBER];
744 }; // End of "class Stub_template_repertoire".
747 // Constructor - creates/initilizes all stub templates.
749 template<bool big_endian>
750 Stub_template_repertoire<big_endian>::Stub_template_repertoire()
752 // Insn array definitions.
753 const static Insntype ST_NONE_INSNS[] = {};
755 const static Insntype ST_ADRP_BRANCH_INSNS[] =
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 */
765 const static Insntype ST_LONG_BRANCH_ABS_INSNS[] =
767 0x58000050, /* ldr ip0, 0x8 */
768 0xd61f0200, /* br ip0 */
769 0x00000000, /* address field */
770 0x00000000, /* address fields */
773 const static Insntype ST_LONG_BRANCH_PCREL_INSNS[] =
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 */
785 const static Insntype ST_E_843419_INSNS[] =
787 0x00000000, /* Placeholder for erratum insn. */
788 0x14000000, /* b <label> */
791 // ST_E_835769 has the same stub template as ST_E_843419
792 // but we reproduce the array here so that the sizeof
793 // expressions in install_insn_template will work.
794 const static Insntype ST_E_835769_INSNS[] =
796 0x00000000, /* Placeholder for erratum insn. */
797 0x14000000, /* b <label> */
800 #define install_insn_template(T) \
801 const static Stub_template<big_endian> template_##T = { \
802 T##_INSNS, sizeof(T##_INSNS) / sizeof(T##_INSNS[0]) }; \
803 this->stub_templates_[T] = &template_##T
805 install_insn_template(ST_NONE);
806 install_insn_template(ST_ADRP_BRANCH);
807 install_insn_template(ST_LONG_BRANCH_ABS);
808 install_insn_template(ST_LONG_BRANCH_PCREL);
809 install_insn_template(ST_E_843419);
810 install_insn_template(ST_E_835769);
812 #undef install_insn_template
816 // Base class for stubs.
818 template<int size, bool big_endian>
822 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
823 typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
825 static const AArch64_address invalid_address =
826 static_cast<AArch64_address>(-1);
828 static const section_offset_type invalid_offset =
829 static_cast<section_offset_type>(-1);
832 : destination_address_(invalid_address),
833 offset_(invalid_offset),
843 { return this->type_; }
845 // Get stub template that provides stub insn information.
846 const Stub_template<big_endian>*
847 stub_template() const
849 return Stub_template_repertoire<big_endian>::
850 get_stub_template(this->type());
853 // Get destination address.
855 destination_address() const
857 gold_assert(this->destination_address_ != this->invalid_address);
858 return this->destination_address_;
861 // Set destination address.
863 set_destination_address(AArch64_address address)
865 gold_assert(address != this->invalid_address);
866 this->destination_address_ = address;
869 // Reset the destination address.
871 reset_destination_address()
872 { this->destination_address_ = this->invalid_address; }
874 // Get offset of code stub. For Reloc_stub, it is the offset from the
875 // beginning of its containing stub table; for Erratum_stub, it is the offset
876 // from the end of reloc_stubs.
880 gold_assert(this->offset_ != this->invalid_offset);
881 return this->offset_;
886 set_offset(section_offset_type offset)
887 { this->offset_ = offset; }
889 // Return the stub insn.
892 { return this->stub_template()->insns; }
894 // Return num of stub insns.
897 { return this->stub_template()->insn_num; }
899 // Get size of the stub.
903 return this->insn_num() *
904 AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
907 // Write stub to output file.
909 write(unsigned char* view, section_size_type view_size)
910 { this->do_write(view, view_size); }
913 // Abstract method to be implemented by sub-classes.
915 do_write(unsigned char*, section_size_type) = 0;
918 // The last insn of a stub is a jump to destination insn. This field records
919 // the destination address.
920 AArch64_address destination_address_;
921 // The stub offset. Note this has difference interpretations between an
922 // Reloc_stub and an Erratum_stub. For Reloc_stub this is the offset from the
923 // beginning of the containing stub_table, whereas for Erratum_stub, this is
924 // the offset from the end of reloc_stubs.
925 section_offset_type offset_;
928 }; // End of "Stub_base".
931 // Erratum stub class. An erratum stub differs from a reloc stub in that for
932 // each erratum occurrence, we generate an erratum stub. We never share erratum
933 // stubs, whereas for reloc stubs, different branch insns share a single reloc
934 // stub as long as the branch targets are the same. (More to the point, reloc
935 // stubs can be shared because they're used to reach a specific target, whereas
936 // erratum stubs branch back to the original control flow.)
938 template<int size, bool big_endian>
939 class Erratum_stub : public Stub_base<size, big_endian>
942 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
943 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
944 typedef AArch64_insn_utilities<big_endian> Insn_utilities;
945 typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
947 static const int STUB_ADDR_ALIGN;
949 static const Insntype invalid_insn = static_cast<Insntype>(-1);
951 Erratum_stub(The_aarch64_relobj* relobj, int type,
952 unsigned shndx, unsigned int sh_offset)
953 : Stub_base<size, big_endian>(type), relobj_(relobj),
954 shndx_(shndx), sh_offset_(sh_offset),
955 erratum_insn_(invalid_insn),
956 erratum_address_(this->invalid_address)
961 // Return the object that contains the erratum.
964 { return this->relobj_; }
966 // Get section index of the erratum.
969 { return this->shndx_; }
971 // Get section offset of the erratum.
974 { return this->sh_offset_; }
976 // Get the erratum insn. This is the insn located at erratum_insn_address.
980 gold_assert(this->erratum_insn_ != this->invalid_insn);
981 return this->erratum_insn_;
984 // Set the insn that the erratum happens to.
986 set_erratum_insn(Insntype insn)
987 { this->erratum_insn_ = insn; }
989 // For 843419, the erratum insn is ld/st xt, [xn, #uimm], which may be a
990 // relocation spot, in this case, the erratum_insn_ recorded at scanning phase
991 // is no longer the one we want to write out to the stub, update erratum_insn_
992 // with relocated version. Also note that in this case xn must not be "PC", so
993 // it is safe to move the erratum insn from the origin place to the stub. For
994 // 835769, the erratum insn is multiply-accumulate insn, which could not be a
995 // relocation spot (assertion added though).
997 update_erratum_insn(Insntype insn)
999 gold_assert(this->erratum_insn_ != this->invalid_insn);
1000 switch (this->type())
1003 gold_assert(Insn_utilities::aarch64_ldst_uimm(insn));
1004 gold_assert(Insn_utilities::aarch64_ldst_uimm(this->erratum_insn()));
1005 gold_assert(Insn_utilities::aarch64_rd(insn) ==
1006 Insn_utilities::aarch64_rd(this->erratum_insn()));
1007 gold_assert(Insn_utilities::aarch64_rn(insn) ==
1008 Insn_utilities::aarch64_rn(this->erratum_insn()));
1009 // Update plain ld/st insn with relocated insn.
1010 this->erratum_insn_ = insn;
1013 gold_assert(insn == this->erratum_insn());
1021 // Return the address where an erratum must be done.
1023 erratum_address() const
1025 gold_assert(this->erratum_address_ != this->invalid_address);
1026 return this->erratum_address_;
1029 // Set the address where an erratum must be done.
1031 set_erratum_address(AArch64_address addr)
1032 { this->erratum_address_ = addr; }
1034 // Comparator used to group Erratum_stubs in a set by (obj, shndx,
1035 // sh_offset). We do not include 'type' in the calculation, because there is
1036 // at most one stub type at (obj, shndx, sh_offset).
1038 operator<(const Erratum_stub<size, big_endian>& k) const
1042 // We group stubs by relobj.
1043 if (this->relobj_ != k.relobj_)
1044 return this->relobj_ < k.relobj_;
1045 // Then by section index.
1046 if (this->shndx_ != k.shndx_)
1047 return this->shndx_ < k.shndx_;
1048 // Lastly by section offset.
1049 return this->sh_offset_ < k.sh_offset_;
1053 invalidate_erratum_stub()
1055 gold_assert(this->relobj_ != NULL);
1056 this->relobj_ = NULL;
1060 is_invalidated_erratum_stub()
1061 { return this->relobj_ == NULL; }
1065 do_write(unsigned char*, section_size_type);
1068 // The object that needs to be fixed.
1069 The_aarch64_relobj* relobj_;
1070 // The shndx in the object that needs to be fixed.
1071 const unsigned int shndx_;
1072 // The section offset in the obejct that needs to be fixed.
1073 const unsigned int sh_offset_;
1074 // The insn to be fixed.
1075 Insntype erratum_insn_;
1076 // The address of the above insn.
1077 AArch64_address erratum_address_;
1078 }; // End of "Erratum_stub".
1081 // Erratum sub class to wrap additional info needed by 843419. In fixing this
1082 // erratum, we may choose to replace 'adrp' with 'adr', in this case, we need
1083 // adrp's code position (two or three insns before erratum insn itself).
1085 template<int size, bool big_endian>
1086 class E843419_stub : public Erratum_stub<size, big_endian>
1089 typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
1091 E843419_stub(AArch64_relobj<size, big_endian>* relobj,
1092 unsigned int shndx, unsigned int sh_offset,
1093 unsigned int adrp_sh_offset)
1094 : Erratum_stub<size, big_endian>(relobj, ST_E_843419, shndx, sh_offset),
1095 adrp_sh_offset_(adrp_sh_offset)
1099 adrp_sh_offset() const
1100 { return this->adrp_sh_offset_; }
1103 // Section offset of "adrp". (We do not need a "adrp_shndx_" field, because we
1104 // can obtain it from its parent.)
1105 const unsigned int adrp_sh_offset_;
1109 template<int size, bool big_endian>
1110 const int Erratum_stub<size, big_endian>::STUB_ADDR_ALIGN = 4;
1112 // Comparator used in set definition.
1113 template<int size, bool big_endian>
1114 struct Erratum_stub_less
1117 operator()(const Erratum_stub<size, big_endian>* s1,
1118 const Erratum_stub<size, big_endian>* s2) const
1119 { return *s1 < *s2; }
1122 // Erratum_stub implementation for writing stub to output file.
1124 template<int size, bool big_endian>
1126 Erratum_stub<size, big_endian>::do_write(unsigned char* view, section_size_type)
1128 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
1129 const Insntype* insns = this->insns();
1130 uint32_t num_insns = this->insn_num();
1131 Insntype* ip = reinterpret_cast<Insntype*>(view);
1132 // For current implemented erratum 843419 and 835769, the first insn in the
1133 // stub is always a copy of the problematic insn (in 843419, the mem access
1134 // insn, in 835769, the mac insn), followed by a jump-back.
1135 elfcpp::Swap<32, big_endian>::writeval(ip, this->erratum_insn());
1136 for (uint32_t i = 1; i < num_insns; ++i)
1137 elfcpp::Swap<32, big_endian>::writeval(ip + i, insns[i]);
1141 // Reloc stub class.
1143 template<int size, bool big_endian>
1144 class Reloc_stub : public Stub_base<size, big_endian>
1147 typedef Reloc_stub<size, big_endian> This;
1148 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
1150 // Branch range. This is used to calculate the section group size, as well as
1151 // determine whether a stub is needed.
1152 static const int MAX_BRANCH_OFFSET = ((1 << 25) - 1) << 2;
1153 static const int MIN_BRANCH_OFFSET = -((1 << 25) << 2);
1155 // Constant used to determine if an offset fits in the adrp instruction
1157 static const int MAX_ADRP_IMM = (1 << 20) - 1;
1158 static const int MIN_ADRP_IMM = -(1 << 20);
1160 static const int BYTES_PER_INSN = 4;
1161 static const int STUB_ADDR_ALIGN;
1163 // Determine whether the offset fits in the jump/branch instruction.
1165 aarch64_valid_branch_offset_p(int64_t offset)
1166 { return offset >= MIN_BRANCH_OFFSET && offset <= MAX_BRANCH_OFFSET; }
1168 // Determine whether the offset fits in the adrp immediate field.
1170 aarch64_valid_for_adrp_p(AArch64_address location, AArch64_address dest)
1172 typedef AArch64_relocate_functions<size, big_endian> Reloc;
1173 int64_t adrp_imm = (Reloc::Page(dest) - Reloc::Page(location)) >> 12;
1174 return adrp_imm >= MIN_ADRP_IMM && adrp_imm <= MAX_ADRP_IMM;
1177 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
1180 stub_type_for_reloc(unsigned int r_type, AArch64_address address,
1181 AArch64_address target);
1183 Reloc_stub(int type)
1184 : Stub_base<size, big_endian>(type)
1190 // The key class used to index the stub instance in the stub table's stub map.
1194 Key(int type, const Symbol* symbol, const Relobj* relobj,
1195 unsigned int r_sym, int32_t addend)
1196 : type_(type), addend_(addend)
1200 this->r_sym_ = Reloc_stub::invalid_index;
1201 this->u_.symbol = symbol;
1205 gold_assert(relobj != NULL && r_sym != invalid_index);
1206 this->r_sym_ = r_sym;
1207 this->u_.relobj = relobj;
1214 // Return stub type.
1217 { return this->type_; }
1219 // Return the local symbol index or invalid_index.
1222 { return this->r_sym_; }
1224 // Return the symbol if there is one.
1227 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
1229 // Return the relobj if there is one.
1232 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
1234 // Whether this equals to another key k.
1236 eq(const Key& k) const
1238 return ((this->type_ == k.type_)
1239 && (this->r_sym_ == k.r_sym_)
1240 && ((this->r_sym_ != Reloc_stub::invalid_index)
1241 ? (this->u_.relobj == k.u_.relobj)
1242 : (this->u_.symbol == k.u_.symbol))
1243 && (this->addend_ == k.addend_));
1246 // Return a hash value.
1250 size_t name_hash_value = gold::string_hash<char>(
1251 (this->r_sym_ != Reloc_stub::invalid_index)
1252 ? this->u_.relobj->name().c_str()
1253 : this->u_.symbol->name());
1254 // We only have 4 stub types.
1255 size_t stub_type_hash_value = 0x03 & this->type_;
1256 return (name_hash_value
1257 ^ stub_type_hash_value
1258 ^ ((this->r_sym_ & 0x3fff) << 2)
1259 ^ ((this->addend_ & 0xffff) << 16));
1262 // Functors for STL associative containers.
1266 operator()(const Key& k) const
1267 { return k.hash_value(); }
1273 operator()(const Key& k1, const Key& k2) const
1274 { return k1.eq(k2); }
1280 // If this is a local symbol, this is the index in the defining object.
1281 // Otherwise, it is invalid_index for a global symbol.
1282 unsigned int r_sym_;
1283 // If r_sym_ is an invalid index, this points to a global symbol.
1284 // Otherwise, it points to a relobj. We used the unsized and target
1285 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
1286 // Arm_relobj, in order to avoid making the stub class a template
1287 // as most of the stub machinery is endianness-neutral. However, it
1288 // may require a bit of casting done by users of this class.
1291 const Symbol* symbol;
1292 const Relobj* relobj;
1294 // Addend associated with a reloc.
1296 }; // End of inner class Reloc_stub::Key
1299 // This may be overridden in the child class.
1301 do_write(unsigned char*, section_size_type);
1304 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
1305 }; // End of Reloc_stub
1307 template<int size, bool big_endian>
1308 const int Reloc_stub<size, big_endian>::STUB_ADDR_ALIGN = 4;
1310 // Write data to output file.
1312 template<int size, bool big_endian>
1314 Reloc_stub<size, big_endian>::
1315 do_write(unsigned char* view, section_size_type)
1317 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
1318 const uint32_t* insns = this->insns();
1319 uint32_t num_insns = this->insn_num();
1320 Insntype* ip = reinterpret_cast<Insntype*>(view);
1321 for (uint32_t i = 0; i < num_insns; ++i)
1322 elfcpp::Swap<32, big_endian>::writeval(ip + i, insns[i]);
1326 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
1329 template<int size, bool big_endian>
1331 Reloc_stub<size, big_endian>::stub_type_for_reloc(
1332 unsigned int r_type, AArch64_address location, AArch64_address dest)
1334 int64_t branch_offset = 0;
1337 case elfcpp::R_AARCH64_CALL26:
1338 case elfcpp::R_AARCH64_JUMP26:
1339 branch_offset = dest - location;
1345 if (aarch64_valid_branch_offset_p(branch_offset))
1348 if (aarch64_valid_for_adrp_p(location, dest))
1349 return ST_ADRP_BRANCH;
1351 // Always use PC-relative addressing in case of -shared or -pie.
1352 if (parameters->options().output_is_position_independent())
1353 return ST_LONG_BRANCH_PCREL;
1355 // This saves 2 insns per stub, compared to ST_LONG_BRANCH_PCREL.
1356 // But is only applicable to non-shared or non-pie.
1357 return ST_LONG_BRANCH_ABS;
1360 // A class to hold stubs for the ARM target. This contains 2 different types of
1361 // stubs - reloc stubs and erratum stubs.
1363 template<int size, bool big_endian>
1364 class Stub_table : public Output_data
1367 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1368 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
1369 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
1370 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1371 typedef Reloc_stub<size, big_endian> The_reloc_stub;
1372 typedef typename The_reloc_stub::Key The_reloc_stub_key;
1373 typedef Erratum_stub<size, big_endian> The_erratum_stub;
1374 typedef Erratum_stub_less<size, big_endian> The_erratum_stub_less;
1375 typedef typename The_reloc_stub_key::hash The_reloc_stub_key_hash;
1376 typedef typename The_reloc_stub_key::equal_to The_reloc_stub_key_equal_to;
1377 typedef Stub_table<size, big_endian> The_stub_table;
1378 typedef Unordered_map<The_reloc_stub_key, The_reloc_stub*,
1379 The_reloc_stub_key_hash, The_reloc_stub_key_equal_to>
1381 typedef typename Reloc_stub_map::const_iterator Reloc_stub_map_const_iter;
1382 typedef Relocate_info<size, big_endian> The_relocate_info;
1384 typedef std::set<The_erratum_stub*, The_erratum_stub_less> Erratum_stub_set;
1385 typedef typename Erratum_stub_set::iterator Erratum_stub_set_iter;
1387 Stub_table(The_aarch64_input_section* owner)
1388 : Output_data(), owner_(owner), reloc_stubs_size_(0),
1389 erratum_stubs_size_(0), prev_data_size_(0)
1395 The_aarch64_input_section*
1399 // Whether this stub table is empty.
1402 { return reloc_stubs_.empty() && erratum_stubs_.empty(); }
1404 // Return the current data size.
1406 current_data_size() const
1407 { return this->current_data_size_for_child(); }
1409 // Add a STUB using KEY. The caller is responsible for avoiding addition
1410 // if a STUB with the same key has already been added.
1412 add_reloc_stub(The_reloc_stub* stub, const The_reloc_stub_key& key);
1414 // Add an erratum stub into the erratum stub set. The set is ordered by
1415 // (relobj, shndx, sh_offset).
1417 add_erratum_stub(The_erratum_stub* stub);
1419 // Find if such erratum exists for any given (obj, shndx, sh_offset).
1421 find_erratum_stub(The_aarch64_relobj* a64relobj,
1422 unsigned int shndx, unsigned int sh_offset);
1424 // Find all the erratums for a given input section. The return value is a pair
1425 // of iterators [begin, end).
1426 std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
1427 find_erratum_stubs_for_input_section(The_aarch64_relobj* a64relobj,
1428 unsigned int shndx);
1430 // Compute the erratum stub address.
1432 erratum_stub_address(The_erratum_stub* stub) const
1434 AArch64_address r = align_address(this->address() + this->reloc_stubs_size_,
1435 The_erratum_stub::STUB_ADDR_ALIGN);
1436 r += stub->offset();
1440 // Finalize stubs. No-op here, just for completeness.
1445 // Look up a relocation stub using KEY. Return NULL if there is none.
1447 find_reloc_stub(The_reloc_stub_key& key)
1449 Reloc_stub_map_const_iter p = this->reloc_stubs_.find(key);
1450 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
1453 // Relocate reloc stubs in this stub table. This does not relocate erratum stubs.
1455 relocate_reloc_stubs(const The_relocate_info*,
1456 The_target_aarch64*,
1462 // Relocate an erratum stub.
1464 relocate_erratum_stub(The_erratum_stub*, unsigned char*);
1466 // Update data size at the end of a relaxation pass. Return true if data size
1467 // is different from that of the previous relaxation pass.
1469 update_data_size_changed_p()
1471 // No addralign changed here.
1472 off_t s = align_address(this->reloc_stubs_size_,
1473 The_erratum_stub::STUB_ADDR_ALIGN)
1474 + this->erratum_stubs_size_;
1475 bool changed = (s != this->prev_data_size_);
1476 this->prev_data_size_ = s;
1481 // Write out section contents.
1483 do_write(Output_file*);
1485 // Return the required alignment.
1487 do_addralign() const
1489 return std::max(The_reloc_stub::STUB_ADDR_ALIGN,
1490 The_erratum_stub::STUB_ADDR_ALIGN);
1493 // Reset address and file offset.
1495 do_reset_address_and_file_offset()
1496 { this->set_current_data_size_for_child(this->prev_data_size_); }
1498 // Set final data size.
1500 set_final_data_size()
1501 { this->set_data_size(this->current_data_size()); }
1504 // Relocate one reloc stub.
1506 relocate_reloc_stub(The_reloc_stub*,
1507 const The_relocate_info*,
1508 The_target_aarch64*,
1515 // Owner of this stub table.
1516 The_aarch64_input_section* owner_;
1517 // The relocation stubs.
1518 Reloc_stub_map reloc_stubs_;
1519 // The erratum stubs.
1520 Erratum_stub_set erratum_stubs_;
1521 // Size of reloc stubs.
1522 off_t reloc_stubs_size_;
1523 // Size of erratum stubs.
1524 off_t erratum_stubs_size_;
1525 // data size of this in the previous pass.
1526 off_t prev_data_size_;
1527 }; // End of Stub_table
1530 // Add an erratum stub into the erratum stub set. The set is ordered by
1531 // (relobj, shndx, sh_offset).
1533 template<int size, bool big_endian>
1535 Stub_table<size, big_endian>::add_erratum_stub(The_erratum_stub* stub)
1537 std::pair<Erratum_stub_set_iter, bool> ret =
1538 this->erratum_stubs_.insert(stub);
1539 gold_assert(ret.second);
1540 this->erratum_stubs_size_ = align_address(
1541 this->erratum_stubs_size_, The_erratum_stub::STUB_ADDR_ALIGN);
1542 stub->set_offset(this->erratum_stubs_size_);
1543 this->erratum_stubs_size_ += stub->stub_size();
1547 // Find if such erratum exists for given (obj, shndx, sh_offset).
1549 template<int size, bool big_endian>
1550 Erratum_stub<size, big_endian>*
1551 Stub_table<size, big_endian>::find_erratum_stub(
1552 The_aarch64_relobj* a64relobj, unsigned int shndx, unsigned int sh_offset)
1554 // A dummy object used as key to search in the set.
1555 The_erratum_stub key(a64relobj, ST_NONE,
1557 Erratum_stub_set_iter i = this->erratum_stubs_.find(&key);
1558 if (i != this->erratum_stubs_.end())
1560 The_erratum_stub* stub(*i);
1561 gold_assert(stub->erratum_insn() != 0);
1568 // Find all the errata for a given input section. The return value is a pair of
1569 // iterators [begin, end).
1571 template<int size, bool big_endian>
1572 std::pair<typename Stub_table<size, big_endian>::Erratum_stub_set_iter,
1573 typename Stub_table<size, big_endian>::Erratum_stub_set_iter>
1574 Stub_table<size, big_endian>::find_erratum_stubs_for_input_section(
1575 The_aarch64_relobj* a64relobj, unsigned int shndx)
1577 typedef std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter> Result_pair;
1578 Erratum_stub_set_iter start, end;
1579 The_erratum_stub low_key(a64relobj, ST_NONE, shndx, 0);
1580 start = this->erratum_stubs_.lower_bound(&low_key);
1581 if (start == this->erratum_stubs_.end())
1582 return Result_pair(this->erratum_stubs_.end(),
1583 this->erratum_stubs_.end());
1585 while (end != this->erratum_stubs_.end() &&
1586 (*end)->relobj() == a64relobj && (*end)->shndx() == shndx)
1588 return Result_pair(start, end);
1592 // Add a STUB using KEY. The caller is responsible for avoiding addition
1593 // if a STUB with the same key has already been added.
1595 template<int size, bool big_endian>
1597 Stub_table<size, big_endian>::add_reloc_stub(
1598 The_reloc_stub* stub, const The_reloc_stub_key& key)
1600 gold_assert(stub->type() == key.type());
1601 this->reloc_stubs_[key] = stub;
1603 // Assign stub offset early. We can do this because we never remove
1604 // reloc stubs and they are in the beginning of the stub table.
1605 this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_,
1606 The_reloc_stub::STUB_ADDR_ALIGN);
1607 stub->set_offset(this->reloc_stubs_size_);
1608 this->reloc_stubs_size_ += stub->stub_size();
1612 // Relocate an erratum stub.
1614 template<int size, bool big_endian>
1616 Stub_table<size, big_endian>::
1617 relocate_erratum_stub(The_erratum_stub* estub,
1618 unsigned char* view)
1620 // Just for convenience.
1621 const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
1623 gold_assert(!estub->is_invalidated_erratum_stub());
1624 AArch64_address stub_address = this->erratum_stub_address(estub);
1625 // The address of "b" in the stub that is to be "relocated".
1626 AArch64_address stub_b_insn_address;
1627 // Branch offset that is to be filled in "b" insn.
1629 switch (estub->type())
1633 // The 1st insn of the erratum could be a relocation spot,
1634 // in this case we need to fix it with
1635 // "(*i)->erratum_insn()".
1636 elfcpp::Swap<32, big_endian>::writeval(
1637 view + (stub_address - this->address()),
1638 estub->erratum_insn());
1639 // For the erratum, the 2nd insn is a b-insn to be patched
1641 stub_b_insn_address = stub_address + 1 * BPI;
1642 b_offset = estub->destination_address() - stub_b_insn_address;
1643 AArch64_relocate_functions<size, big_endian>::construct_b(
1644 view + (stub_b_insn_address - this->address()),
1645 ((unsigned int)(b_offset)) & 0xfffffff);
1651 estub->invalidate_erratum_stub();
1655 // Relocate only reloc stubs in this stub table. This does not relocate erratum
1658 template<int size, bool big_endian>
1660 Stub_table<size, big_endian>::
1661 relocate_reloc_stubs(const The_relocate_info* relinfo,
1662 The_target_aarch64* target_aarch64,
1663 Output_section* output_section,
1664 unsigned char* view,
1665 AArch64_address address,
1666 section_size_type view_size)
1668 // "view_size" is the total size of the stub_table.
1669 gold_assert(address == this->address() &&
1670 view_size == static_cast<section_size_type>(this->data_size()));
1671 for(Reloc_stub_map_const_iter p = this->reloc_stubs_.begin();
1672 p != this->reloc_stubs_.end(); ++p)
1673 relocate_reloc_stub(p->second, relinfo, target_aarch64, output_section,
1674 view, address, view_size);
1678 // Relocate one reloc stub. This is a helper for
1679 // Stub_table::relocate_reloc_stubs().
1681 template<int size, bool big_endian>
1683 Stub_table<size, big_endian>::
1684 relocate_reloc_stub(The_reloc_stub* stub,
1685 const The_relocate_info* relinfo,
1686 The_target_aarch64* target_aarch64,
1687 Output_section* output_section,
1688 unsigned char* view,
1689 AArch64_address address,
1690 section_size_type view_size)
1692 // "offset" is the offset from the beginning of the stub_table.
1693 section_size_type offset = stub->offset();
1694 section_size_type stub_size = stub->stub_size();
1695 // "view_size" is the total size of the stub_table.
1696 gold_assert(offset + stub_size <= view_size);
1698 target_aarch64->relocate_reloc_stub(stub, relinfo, output_section,
1699 view + offset, address + offset, view_size);
1703 // Write out the stubs to file.
1705 template<int size, bool big_endian>
1707 Stub_table<size, big_endian>::do_write(Output_file* of)
1709 off_t offset = this->offset();
1710 const section_size_type oview_size =
1711 convert_to_section_size_type(this->data_size());
1712 unsigned char* const oview = of->get_output_view(offset, oview_size);
1714 // Write relocation stubs.
1715 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
1716 p != this->reloc_stubs_.end(); ++p)
1718 The_reloc_stub* stub = p->second;
1719 AArch64_address address = this->address() + stub->offset();
1720 gold_assert(address ==
1721 align_address(address, The_reloc_stub::STUB_ADDR_ALIGN));
1722 stub->write(oview + stub->offset(), stub->stub_size());
1725 // Write erratum stubs.
1726 unsigned int erratum_stub_start_offset =
1727 align_address(this->reloc_stubs_size_, The_erratum_stub::STUB_ADDR_ALIGN);
1728 for (typename Erratum_stub_set::iterator p = this->erratum_stubs_.begin();
1729 p != this->erratum_stubs_.end(); ++p)
1731 The_erratum_stub* stub(*p);
1732 stub->write(oview + erratum_stub_start_offset + stub->offset(),
1736 of->write_output_view(this->offset(), oview_size, oview);
1740 // AArch64_relobj class.
1742 template<int size, bool big_endian>
1743 class AArch64_relobj : public Sized_relobj_file<size, big_endian>
1746 typedef AArch64_relobj<size, big_endian> This;
1747 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1748 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1749 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
1750 typedef Stub_table<size, big_endian> The_stub_table;
1751 typedef Erratum_stub<size, big_endian> The_erratum_stub;
1752 typedef typename The_stub_table::Erratum_stub_set_iter Erratum_stub_set_iter;
1753 typedef std::vector<The_stub_table*> Stub_table_list;
1754 static const AArch64_address invalid_address =
1755 static_cast<AArch64_address>(-1);
1757 AArch64_relobj(const std::string& name, Input_file* input_file, off_t offset,
1758 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1759 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1766 // Return the stub table of the SHNDX-th section if there is one.
1768 stub_table(unsigned int shndx) const
1770 gold_assert(shndx < this->stub_tables_.size());
1771 return this->stub_tables_[shndx];
1774 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1776 set_stub_table(unsigned int shndx, The_stub_table* stub_table)
1778 gold_assert(shndx < this->stub_tables_.size());
1779 this->stub_tables_[shndx] = stub_table;
1782 // Entrance to errata scanning.
1784 scan_errata(unsigned int shndx,
1785 const elfcpp::Shdr<size, big_endian>&,
1786 Output_section*, const Symbol_table*,
1787 The_target_aarch64*);
1789 // Scan all relocation sections for stub generation.
1791 scan_sections_for_stubs(The_target_aarch64*, const Symbol_table*,
1794 // Whether a section is a scannable text section.
1796 text_section_is_scannable(const elfcpp::Shdr<size, big_endian>&, unsigned int,
1797 const Output_section*, const Symbol_table*);
1799 // Convert regular input section with index SHNDX to a relaxed section.
1801 convert_input_section_to_relaxed_section(unsigned shndx)
1803 // The stubs have relocations and we need to process them after writing
1804 // out the stubs. So relocation now must follow section write.
1805 this->set_section_offset(shndx, -1ULL);
1806 this->set_relocs_must_follow_section_writes();
1809 // Structure for mapping symbol position.
1810 struct Mapping_symbol_position
1812 Mapping_symbol_position(unsigned int shndx, AArch64_address offset):
1813 shndx_(shndx), offset_(offset)
1816 // "<" comparator used in ordered_map container.
1818 operator<(const Mapping_symbol_position& p) const
1820 return (this->shndx_ < p.shndx_
1821 || (this->shndx_ == p.shndx_ && this->offset_ < p.offset_));
1825 unsigned int shndx_;
1828 AArch64_address offset_;
1831 typedef std::map<Mapping_symbol_position, char> Mapping_symbol_info;
1834 // Post constructor setup.
1838 // Call parent's setup method.
1839 Sized_relobj_file<size, big_endian>::do_setup();
1841 // Initialize look-up tables.
1842 this->stub_tables_.resize(this->shnum());
1846 do_relocate_sections(
1847 const Symbol_table* symtab, const Layout* layout,
1848 const unsigned char* pshdrs, Output_file* of,
1849 typename Sized_relobj_file<size, big_endian>::Views* pviews);
1851 // Count local symbols and (optionally) record mapping info.
1853 do_count_local_symbols(Stringpool_template<char>*,
1854 Stringpool_template<char>*);
1857 // Fix all errata in the object, and for each erratum, relocate corresponding
1860 fix_errata_and_relocate_erratum_stubs(
1861 typename Sized_relobj_file<size, big_endian>::Views* pviews);
1863 // Try to fix erratum 843419 in an optimized way. Return true if patch is
1866 try_fix_erratum_843419_optimized(
1868 typename Sized_relobj_file<size, big_endian>::View_size&);
1870 // Whether a section needs to be scanned for relocation stubs.
1872 section_needs_reloc_stub_scanning(const elfcpp::Shdr<size, big_endian>&,
1873 const Relobj::Output_sections&,
1874 const Symbol_table*, const unsigned char*);
1876 // List of stub tables.
1877 Stub_table_list stub_tables_;
1879 // Mapping symbol information sorted by (section index, section_offset).
1880 Mapping_symbol_info mapping_symbol_info_;
1881 }; // End of AArch64_relobj
1884 // Override to record mapping symbol information.
1885 template<int size, bool big_endian>
1887 AArch64_relobj<size, big_endian>::do_count_local_symbols(
1888 Stringpool_template<char>* pool, Stringpool_template<char>* dynpool)
1890 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
1892 // Only erratum-fixing work needs mapping symbols, so skip this time consuming
1893 // processing if not fixing erratum.
1894 if (!parameters->options().fix_cortex_a53_843419()
1895 && !parameters->options().fix_cortex_a53_835769())
1898 const unsigned int loccount = this->local_symbol_count();
1902 // Read the symbol table section header.
1903 const unsigned int symtab_shndx = this->symtab_shndx();
1904 elfcpp::Shdr<size, big_endian>
1905 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
1906 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1908 // Read the local symbols.
1909 const int sym_size =elfcpp::Elf_sizes<size>::sym_size;
1910 gold_assert(loccount == symtabshdr.get_sh_info());
1911 off_t locsize = loccount * sym_size;
1912 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1913 locsize, true, true);
1915 // For mapping symbol processing, we need to read the symbol names.
1916 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
1917 if (strtab_shndx >= this->shnum())
1919 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
1923 elfcpp::Shdr<size, big_endian>
1924 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
1925 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
1927 this->error(_("symbol table name section has wrong type: %u"),
1928 static_cast<unsigned int>(strtabshdr.get_sh_type()));
1932 const char* pnames =
1933 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
1934 strtabshdr.get_sh_size(),
1937 // Skip the first dummy symbol.
1939 typename Sized_relobj_file<size, big_endian>::Local_values*
1940 plocal_values = this->local_values();
1941 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1943 elfcpp::Sym<size, big_endian> sym(psyms);
1944 Symbol_value<size>& lv((*plocal_values)[i]);
1945 AArch64_address input_value = lv.input_value();
1947 // Check to see if this is a mapping symbol. AArch64 mapping symbols are
1948 // defined in "ELF for the ARM 64-bit Architecture", Table 4-4, Mapping
1950 // Mapping symbols could be one of the following 4 forms -
1955 const char* sym_name = pnames + sym.get_st_name();
1956 if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd')
1957 && (sym_name[2] == '\0' || sym_name[2] == '.'))
1960 unsigned int input_shndx =
1961 this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
1962 gold_assert(is_ordinary);
1964 Mapping_symbol_position msp(input_shndx, input_value);
1965 // Insert mapping_symbol_info into map whose ordering is defined by
1966 // (shndx, offset_within_section).
1967 this->mapping_symbol_info_[msp] = sym_name[1];
1973 // Fix all errata in the object and for each erratum, we relocate the
1974 // corresponding erratum stub (by calling Stub_table::relocate_erratum_stub).
1976 template<int size, bool big_endian>
1978 AArch64_relobj<size, big_endian>::fix_errata_and_relocate_erratum_stubs(
1979 typename Sized_relobj_file<size, big_endian>::Views* pviews)
1981 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
1982 unsigned int shnum = this->shnum();
1983 for (unsigned int i = 1; i < shnum; ++i)
1985 The_stub_table* stub_table = this->stub_table(i);
1988 std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
1989 ipair(stub_table->find_erratum_stubs_for_input_section(this, i));
1990 Erratum_stub_set_iter p = ipair.first, end = ipair.second;
1993 The_erratum_stub* stub = *p;
1994 typename Sized_relobj_file<size, big_endian>::View_size&
1995 pview((*pviews)[i]);
1997 // Double check data before fix.
1998 gold_assert(pview.address + stub->sh_offset()
1999 == stub->erratum_address());
2001 // Update previously recorded erratum insn with relocated
2004 reinterpret_cast<Insntype*>(pview.view + stub->sh_offset());
2005 Insntype insn_to_fix = ip[0];
2006 stub->update_erratum_insn(insn_to_fix);
2008 // First try to see if erratum is 843419 and if it can be fixed
2009 // without using branch-to-stub.
2010 if (!try_fix_erratum_843419_optimized(stub, pview))
2012 // Replace the erratum insn with a branch-to-stub.
2013 AArch64_address stub_address =
2014 stub_table->erratum_stub_address(stub);
2015 unsigned int b_offset = stub_address - stub->erratum_address();
2016 AArch64_relocate_functions<size, big_endian>::construct_b(
2017 pview.view + stub->sh_offset(), b_offset & 0xfffffff);
2020 // Erratum fix is done (or skipped), continue to relocate erratum
2021 // stub. Note, when erratum fix is skipped (either because we
2022 // proactively change the code sequence or the code sequence is
2023 // changed by relaxation, etc), we can still safely relocate the
2024 // erratum stub, ignoring the fact the erratum could never be
2026 stub_table->relocate_erratum_stub(
2027 stub, pview.view + (stub_table->address() - pview.address));
2029 // Next erratum stub.
2036 // This is an optimization for 843419. This erratum requires the sequence begin
2037 // with 'adrp', when final value calculated by adrp fits in adr, we can just
2038 // replace 'adrp' with 'adr', so we save 2 jumps per occurrence. (Note, however,
2039 // in this case, we do not delete the erratum stub (too late to do so), it is
2040 // merely generated without ever being called.)
2042 template<int size, bool big_endian>
2044 AArch64_relobj<size, big_endian>::try_fix_erratum_843419_optimized(
2045 The_erratum_stub* stub,
2046 typename Sized_relobj_file<size, big_endian>::View_size& pview)
2048 if (stub->type() != ST_E_843419)
2051 typedef AArch64_insn_utilities<big_endian> Insn_utilities;
2052 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
2053 E843419_stub<size, big_endian>* e843419_stub =
2054 reinterpret_cast<E843419_stub<size, big_endian>*>(stub);
2055 AArch64_address pc = pview.address + e843419_stub->adrp_sh_offset();
2056 unsigned int adrp_offset = e843419_stub->adrp_sh_offset ();
2057 Insntype* adrp_view = reinterpret_cast<Insntype*>(pview.view + adrp_offset);
2058 Insntype adrp_insn = adrp_view[0];
2060 // If the instruction at adrp_sh_offset is "mrs R, tpidr_el0", it may come
2061 // from IE -> LE relaxation etc. This is a side-effect of TLS relaxation that
2062 // ADRP has been turned into MRS, there is no erratum risk anymore.
2063 // Therefore, we return true to avoid doing unnecessary branch-to-stub.
2064 if (Insn_utilities::is_mrs_tpidr_el0(adrp_insn))
2067 // If the instruction at adrp_sh_offset is not ADRP and the instruction before
2068 // it is "mrs R, tpidr_el0", it may come from LD -> LE relaxation etc.
2069 // Like the above case, there is no erratum risk any more, we can safely
2071 if (!Insn_utilities::is_adrp(adrp_insn) && adrp_offset)
2074 = reinterpret_cast<Insntype*>(pview.view + adrp_offset - 4);
2075 Insntype prev_insn = prev_view[0];
2077 if (Insn_utilities::is_mrs_tpidr_el0(prev_insn))
2081 /* If we reach here, the first instruction must be ADRP. */
2082 gold_assert(Insn_utilities::is_adrp(adrp_insn));
2083 // Get adrp 33-bit signed imm value.
2084 int64_t adrp_imm = Insn_utilities::
2085 aarch64_adrp_decode_imm(adrp_insn);
2086 // adrp - final value transferred to target register is calculated as:
2087 // PC[11:0] = Zeros(12)
2088 // adrp_dest_value = PC + adrp_imm;
2089 int64_t adrp_dest_value = (pc & ~((1 << 12) - 1)) + adrp_imm;
2090 // adr -final value transferred to target register is calucalted as:
2093 // PC + adr_imm = adrp_dest_value
2095 // adr_imm = adrp_dest_value - PC
2096 int64_t adr_imm = adrp_dest_value - pc;
2097 // Check if imm fits in adr (21-bit signed).
2098 if (-(1 << 20) <= adr_imm && adr_imm < (1 << 20))
2100 // Convert 'adrp' into 'adr'.
2101 Insntype adr_insn = adrp_insn & ((1u << 31) - 1);
2102 adr_insn = Insn_utilities::
2103 aarch64_adr_encode_imm(adr_insn, adr_imm);
2104 elfcpp::Swap<32, big_endian>::writeval(adrp_view, adr_insn);
2111 // Relocate sections.
2113 template<int size, bool big_endian>
2115 AArch64_relobj<size, big_endian>::do_relocate_sections(
2116 const Symbol_table* symtab, const Layout* layout,
2117 const unsigned char* pshdrs, Output_file* of,
2118 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2120 // Relocate the section data.
2121 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2122 1, this->shnum() - 1);
2124 // We do not generate stubs if doing a relocatable link.
2125 if (parameters->options().relocatable())
2128 // This part only relocates erratum stubs that belong to input sections of this
2130 if (parameters->options().fix_cortex_a53_843419()
2131 || parameters->options().fix_cortex_a53_835769())
2132 this->fix_errata_and_relocate_erratum_stubs(pviews);
2134 Relocate_info<size, big_endian> relinfo;
2135 relinfo.symtab = symtab;
2136 relinfo.layout = layout;
2137 relinfo.object = this;
2139 // This part relocates all reloc stubs that are contained in stub_tables of
2140 // this object file.
2141 unsigned int shnum = this->shnum();
2142 The_target_aarch64* target = The_target_aarch64::current_target();
2144 for (unsigned int i = 1; i < shnum; ++i)
2146 The_aarch64_input_section* aarch64_input_section =
2147 target->find_aarch64_input_section(this, i);
2148 if (aarch64_input_section != NULL
2149 && aarch64_input_section->is_stub_table_owner()
2150 && !aarch64_input_section->stub_table()->empty())
2152 Output_section* os = this->output_section(i);
2153 gold_assert(os != NULL);
2155 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
2156 relinfo.reloc_shdr = NULL;
2157 relinfo.data_shndx = i;
2158 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<size>::shdr_size;
2160 typename Sized_relobj_file<size, big_endian>::View_size&
2161 view_struct = (*pviews)[i];
2162 gold_assert(view_struct.view != NULL);
2164 The_stub_table* stub_table = aarch64_input_section->stub_table();
2165 off_t offset = stub_table->address() - view_struct.address;
2166 unsigned char* view = view_struct.view + offset;
2167 AArch64_address address = stub_table->address();
2168 section_size_type view_size = stub_table->data_size();
2169 stub_table->relocate_reloc_stubs(&relinfo, target, os, view, address,
2176 // Determine if an input section is scannable for stub processing. SHDR is
2177 // the header of the section and SHNDX is the section index. OS is the output
2178 // section for the input section and SYMTAB is the global symbol table used to
2179 // look up ICF information.
2181 template<int size, bool big_endian>
2183 AArch64_relobj<size, big_endian>::text_section_is_scannable(
2184 const elfcpp::Shdr<size, big_endian>& text_shdr,
2185 unsigned int text_shndx,
2186 const Output_section* os,
2187 const Symbol_table* symtab)
2189 // Skip any empty sections, unallocated sections or sections whose
2190 // type are not SHT_PROGBITS.
2191 if (text_shdr.get_sh_size() == 0
2192 || (text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
2193 || text_shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
2196 // Skip any discarded or ICF'ed sections.
2197 if (os == NULL || symtab->is_section_folded(this, text_shndx))
2200 // Skip exception frame.
2201 if (strcmp(os->name(), ".eh_frame") == 0)
2204 gold_assert(!this->is_output_section_offset_invalid(text_shndx) ||
2205 os->find_relaxed_input_section(this, text_shndx) != NULL);
2211 // Determine if we want to scan the SHNDX-th section for relocation stubs.
2212 // This is a helper for AArch64_relobj::scan_sections_for_stubs().
2214 template<int size, bool big_endian>
2216 AArch64_relobj<size, big_endian>::section_needs_reloc_stub_scanning(
2217 const elfcpp::Shdr<size, big_endian>& shdr,
2218 const Relobj::Output_sections& out_sections,
2219 const Symbol_table* symtab,
2220 const unsigned char* pshdrs)
2222 unsigned int sh_type = shdr.get_sh_type();
2223 if (sh_type != elfcpp::SHT_RELA)
2226 // Ignore empty section.
2227 off_t sh_size = shdr.get_sh_size();
2231 // Ignore reloc section with unexpected symbol table. The
2232 // error will be reported in the final link.
2233 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
2236 gold_assert(sh_type == elfcpp::SHT_RELA);
2237 unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2239 // Ignore reloc section with unexpected entsize or uneven size.
2240 // The error will be reported in the final link.
2241 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
2244 // Ignore reloc section with bad info. This error will be
2245 // reported in the final link.
2246 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_info());
2247 if (text_shndx >= this->shnum())
2250 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2251 const elfcpp::Shdr<size, big_endian> text_shdr(pshdrs +
2252 text_shndx * shdr_size);
2253 return this->text_section_is_scannable(text_shdr, text_shndx,
2254 out_sections[text_shndx], symtab);
2258 // Scan section SHNDX for erratum 843419 and 835769.
2260 template<int size, bool big_endian>
2262 AArch64_relobj<size, big_endian>::scan_errata(
2263 unsigned int shndx, const elfcpp::Shdr<size, big_endian>& shdr,
2264 Output_section* os, const Symbol_table* symtab,
2265 The_target_aarch64* target)
2267 if (shdr.get_sh_size() == 0
2268 || (shdr.get_sh_flags() &
2269 (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) == 0
2270 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
2273 if (!os || symtab->is_section_folded(this, shndx)) return;
2275 AArch64_address output_offset = this->get_output_section_offset(shndx);
2276 AArch64_address output_address;
2277 if (output_offset != invalid_address)
2278 output_address = os->address() + output_offset;
2281 const Output_relaxed_input_section* poris =
2282 os->find_relaxed_input_section(this, shndx);
2284 output_address = poris->address();
2287 section_size_type input_view_size = 0;
2288 const unsigned char* input_view =
2289 this->section_contents(shndx, &input_view_size, false);
2291 Mapping_symbol_position section_start(shndx, 0);
2292 // Find the first mapping symbol record within section shndx.
2293 typename Mapping_symbol_info::const_iterator p =
2294 this->mapping_symbol_info_.lower_bound(section_start);
2295 while (p != this->mapping_symbol_info_.end() &&
2296 p->first.shndx_ == shndx)
2298 typename Mapping_symbol_info::const_iterator prev = p;
2300 if (prev->second == 'x')
2302 section_size_type span_start =
2303 convert_to_section_size_type(prev->first.offset_);
2304 section_size_type span_end;
2305 if (p != this->mapping_symbol_info_.end()
2306 && p->first.shndx_ == shndx)
2307 span_end = convert_to_section_size_type(p->first.offset_);
2309 span_end = convert_to_section_size_type(shdr.get_sh_size());
2311 // Here we do not share the scanning code of both errata. For 843419,
2312 // only the last few insns of each page are examined, which is fast,
2313 // whereas, for 835769, every insn pair needs to be checked.
2315 if (parameters->options().fix_cortex_a53_843419())
2316 target->scan_erratum_843419_span(
2317 this, shndx, span_start, span_end,
2318 const_cast<unsigned char*>(input_view), output_address);
2320 if (parameters->options().fix_cortex_a53_835769())
2321 target->scan_erratum_835769_span(
2322 this, shndx, span_start, span_end,
2323 const_cast<unsigned char*>(input_view), output_address);
2329 // Scan relocations for stub generation.
2331 template<int size, bool big_endian>
2333 AArch64_relobj<size, big_endian>::scan_sections_for_stubs(
2334 The_target_aarch64* target,
2335 const Symbol_table* symtab,
2336 const Layout* layout)
2338 unsigned int shnum = this->shnum();
2339 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2341 // Read the section headers.
2342 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
2346 // To speed up processing, we set up hash tables for fast lookup of
2347 // input offsets to output addresses.
2348 this->initialize_input_to_output_maps();
2350 const Relobj::Output_sections& out_sections(this->output_sections());
2352 Relocate_info<size, big_endian> relinfo;
2353 relinfo.symtab = symtab;
2354 relinfo.layout = layout;
2355 relinfo.object = this;
2357 // Do relocation stubs scanning.
2358 const unsigned char* p = pshdrs + shdr_size;
2359 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
2361 const elfcpp::Shdr<size, big_endian> shdr(p);
2362 if (parameters->options().fix_cortex_a53_843419()
2363 || parameters->options().fix_cortex_a53_835769())
2364 scan_errata(i, shdr, out_sections[i], symtab, target);
2365 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
2368 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
2369 AArch64_address output_offset =
2370 this->get_output_section_offset(index);
2371 AArch64_address output_address;
2372 if (output_offset != invalid_address)
2374 output_address = out_sections[index]->address() + output_offset;
2378 // Currently this only happens for a relaxed section.
2379 const Output_relaxed_input_section* poris =
2380 out_sections[index]->find_relaxed_input_section(this, index);
2381 gold_assert(poris != NULL);
2382 output_address = poris->address();
2385 // Get the relocations.
2386 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
2390 // Get the section contents.
2391 section_size_type input_view_size = 0;
2392 const unsigned char* input_view =
2393 this->section_contents(index, &input_view_size, false);
2395 relinfo.reloc_shndx = i;
2396 relinfo.data_shndx = index;
2397 unsigned int sh_type = shdr.get_sh_type();
2398 unsigned int reloc_size;
2399 gold_assert (sh_type == elfcpp::SHT_RELA);
2400 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2402 Output_section* os = out_sections[index];
2403 target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
2404 shdr.get_sh_size() / reloc_size,
2406 output_offset == invalid_address,
2407 input_view, output_address,
2414 // A class to wrap an ordinary input section containing executable code.
2416 template<int size, bool big_endian>
2417 class AArch64_input_section : public Output_relaxed_input_section
2420 typedef Stub_table<size, big_endian> The_stub_table;
2422 AArch64_input_section(Relobj* relobj, unsigned int shndx)
2423 : Output_relaxed_input_section(relobj, shndx, 1),
2425 original_contents_(NULL), original_size_(0),
2426 original_addralign_(1)
2429 ~AArch64_input_section()
2430 { delete[] this->original_contents_; }
2436 // Set the stub_table.
2438 set_stub_table(The_stub_table* st)
2439 { this->stub_table_ = st; }
2441 // Whether this is a stub table owner.
2443 is_stub_table_owner() const
2444 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
2446 // Return the original size of the section.
2448 original_size() const
2449 { return this->original_size_; }
2451 // Return the stub table.
2454 { return stub_table_; }
2457 // Write out this input section.
2459 do_write(Output_file*);
2461 // Return required alignment of this.
2463 do_addralign() const
2465 if (this->is_stub_table_owner())
2466 return std::max(this->stub_table_->addralign(),
2467 static_cast<uint64_t>(this->original_addralign_));
2469 return this->original_addralign_;
2472 // Finalize data size.
2474 set_final_data_size();
2476 // Reset address and file offset.
2478 do_reset_address_and_file_offset();
2482 do_output_offset(const Relobj* object, unsigned int shndx,
2483 section_offset_type offset,
2484 section_offset_type* poutput) const
2486 if ((object == this->relobj())
2487 && (shndx == this->shndx())
2490 convert_types<section_offset_type, uint32_t>(this->original_size_)))
2500 // Copying is not allowed.
2501 AArch64_input_section(const AArch64_input_section&);
2502 AArch64_input_section& operator=(const AArch64_input_section&);
2504 // The relocation stubs.
2505 The_stub_table* stub_table_;
2506 // Original section contents. We have to make a copy here since the file
2507 // containing the original section may not be locked when we need to access
2509 unsigned char* original_contents_;
2510 // Section size of the original input section.
2511 uint32_t original_size_;
2512 // Address alignment of the original input section.
2513 uint32_t original_addralign_;
2514 }; // End of AArch64_input_section
2517 // Finalize data size.
2519 template<int size, bool big_endian>
2521 AArch64_input_section<size, big_endian>::set_final_data_size()
2523 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2525 if (this->is_stub_table_owner())
2527 this->stub_table_->finalize_data_size();
2528 off = align_address(off, this->stub_table_->addralign());
2529 off += this->stub_table_->data_size();
2531 this->set_data_size(off);
2535 // Reset address and file offset.
2537 template<int size, bool big_endian>
2539 AArch64_input_section<size, big_endian>::do_reset_address_and_file_offset()
2541 // Size of the original input section contents.
2542 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2544 // If this is a stub table owner, account for the stub table size.
2545 if (this->is_stub_table_owner())
2547 The_stub_table* stub_table = this->stub_table_;
2549 // Reset the stub table's address and file offset. The
2550 // current data size for child will be updated after that.
2551 stub_table_->reset_address_and_file_offset();
2552 off = align_address(off, stub_table_->addralign());
2553 off += stub_table->current_data_size();
2556 this->set_current_data_size(off);
2560 // Initialize an Arm_input_section.
2562 template<int size, bool big_endian>
2564 AArch64_input_section<size, big_endian>::init()
2566 Relobj* relobj = this->relobj();
2567 unsigned int shndx = this->shndx();
2569 // We have to cache original size, alignment and contents to avoid locking
2570 // the original file.
2571 this->original_addralign_ =
2572 convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
2574 // This is not efficient but we expect only a small number of relaxed
2575 // input sections for stubs.
2576 section_size_type section_size;
2577 const unsigned char* section_contents =
2578 relobj->section_contents(shndx, §ion_size, false);
2579 this->original_size_ =
2580 convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
2582 gold_assert(this->original_contents_ == NULL);
2583 this->original_contents_ = new unsigned char[section_size];
2584 memcpy(this->original_contents_, section_contents, section_size);
2586 // We want to make this look like the original input section after
2587 // output sections are finalized.
2588 Output_section* os = relobj->output_section(shndx);
2589 off_t offset = relobj->output_section_offset(shndx);
2590 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2591 this->set_address(os->address() + offset);
2592 this->set_file_offset(os->offset() + offset);
2593 this->set_current_data_size(this->original_size_);
2594 this->finalize_data_size();
2598 // Write data to output file.
2600 template<int size, bool big_endian>
2602 AArch64_input_section<size, big_endian>::do_write(Output_file* of)
2604 // We have to write out the original section content.
2605 gold_assert(this->original_contents_ != NULL);
2606 of->write(this->offset(), this->original_contents_,
2607 this->original_size_);
2609 // If this owns a stub table and it is not empty, write it.
2610 if (this->is_stub_table_owner() && !this->stub_table_->empty())
2611 this->stub_table_->write(of);
2615 // Arm output section class. This is defined mainly to add a number of stub
2616 // generation methods.
2618 template<int size, bool big_endian>
2619 class AArch64_output_section : public Output_section
2622 typedef Target_aarch64<size, big_endian> The_target_aarch64;
2623 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2624 typedef Stub_table<size, big_endian> The_stub_table;
2625 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2628 AArch64_output_section(const char* name, elfcpp::Elf_Word type,
2629 elfcpp::Elf_Xword flags)
2630 : Output_section(name, type, flags)
2633 ~AArch64_output_section() {}
2635 // Group input sections for stub generation.
2637 group_sections(section_size_type, bool, Target_aarch64<size, big_endian>*,
2641 typedef Output_section::Input_section Input_section;
2642 typedef Output_section::Input_section_list Input_section_list;
2644 // Create a stub group.
2646 create_stub_group(Input_section_list::const_iterator,
2647 Input_section_list::const_iterator,
2648 Input_section_list::const_iterator,
2649 The_target_aarch64*,
2650 std::vector<Output_relaxed_input_section*>&,
2652 }; // End of AArch64_output_section
2655 // Create a stub group for input sections from FIRST to LAST. OWNER points to
2656 // the input section that will be the owner of the stub table.
2658 template<int size, bool big_endian> void
2659 AArch64_output_section<size, big_endian>::create_stub_group(
2660 Input_section_list::const_iterator first,
2661 Input_section_list::const_iterator last,
2662 Input_section_list::const_iterator owner,
2663 The_target_aarch64* target,
2664 std::vector<Output_relaxed_input_section*>& new_relaxed_sections,
2667 // Currently we convert ordinary input sections into relaxed sections only
2669 The_aarch64_input_section* input_section;
2670 if (owner->is_relaxed_input_section())
2674 gold_assert(owner->is_input_section());
2675 // Create a new relaxed input section. We need to lock the original
2677 Task_lock_obj<Object> tl(task, owner->relobj());
2679 target->new_aarch64_input_section(owner->relobj(), owner->shndx());
2680 new_relaxed_sections.push_back(input_section);
2683 // Create a stub table.
2684 The_stub_table* stub_table =
2685 target->new_stub_table(input_section);
2687 input_section->set_stub_table(stub_table);
2689 Input_section_list::const_iterator p = first;
2690 // Look for input sections or relaxed input sections in [first ... last].
2693 if (p->is_input_section() || p->is_relaxed_input_section())
2695 // The stub table information for input sections live
2696 // in their objects.
2697 The_aarch64_relobj* aarch64_relobj =
2698 static_cast<The_aarch64_relobj*>(p->relobj());
2699 aarch64_relobj->set_stub_table(p->shndx(), stub_table);
2702 while (p++ != last);
2706 // Group input sections for stub generation. GROUP_SIZE is roughly the limit of
2707 // stub groups. We grow a stub group by adding input section until the size is
2708 // just below GROUP_SIZE. The last input section will be converted into a stub
2709 // table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
2710 // after the stub table, effectively doubling the group size.
2712 // This is similar to the group_sections() function in elf32-arm.c but is
2713 // implemented differently.
2715 template<int size, bool big_endian>
2716 void AArch64_output_section<size, big_endian>::group_sections(
2717 section_size_type group_size,
2718 bool stubs_always_after_branch,
2719 Target_aarch64<size, big_endian>* target,
2725 FINDING_STUB_SECTION,
2729 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
2731 State state = NO_GROUP;
2732 section_size_type off = 0;
2733 section_size_type group_begin_offset = 0;
2734 section_size_type group_end_offset = 0;
2735 section_size_type stub_table_end_offset = 0;
2736 Input_section_list::const_iterator group_begin =
2737 this->input_sections().end();
2738 Input_section_list::const_iterator stub_table =
2739 this->input_sections().end();
2740 Input_section_list::const_iterator group_end = this->input_sections().end();
2741 for (Input_section_list::const_iterator p = this->input_sections().begin();
2742 p != this->input_sections().end();
2745 section_size_type section_begin_offset =
2746 align_address(off, p->addralign());
2747 section_size_type section_end_offset =
2748 section_begin_offset + p->data_size();
2750 // Check to see if we should group the previously seen sections.
2756 case FINDING_STUB_SECTION:
2757 // Adding this section makes the group larger than GROUP_SIZE.
2758 if (section_end_offset - group_begin_offset >= group_size)
2760 if (stubs_always_after_branch)
2762 gold_assert(group_end != this->input_sections().end());
2763 this->create_stub_group(group_begin, group_end, group_end,
2764 target, new_relaxed_sections,
2770 // Input sections up to stub_group_size bytes after the stub
2771 // table can be handled by it too.
2772 state = HAS_STUB_SECTION;
2773 stub_table = group_end;
2774 stub_table_end_offset = group_end_offset;
2779 case HAS_STUB_SECTION:
2780 // Adding this section makes the post stub-section group larger
2783 // NOT SUPPORTED YET. For completeness only.
2784 if (section_end_offset - stub_table_end_offset >= group_size)
2786 gold_assert(group_end != this->input_sections().end());
2787 this->create_stub_group(group_begin, group_end, stub_table,
2788 target, new_relaxed_sections, task);
2797 // If we see an input section and currently there is no group, start
2798 // a new one. Skip any empty sections. We look at the data size
2799 // instead of calling p->relobj()->section_size() to avoid locking.
2800 if ((p->is_input_section() || p->is_relaxed_input_section())
2801 && (p->data_size() != 0))
2803 if (state == NO_GROUP)
2805 state = FINDING_STUB_SECTION;
2807 group_begin_offset = section_begin_offset;
2810 // Keep track of the last input section seen.
2812 group_end_offset = section_end_offset;
2815 off = section_end_offset;
2818 // Create a stub group for any ungrouped sections.
2819 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
2821 gold_assert(group_end != this->input_sections().end());
2822 this->create_stub_group(group_begin, group_end,
2823 (state == FINDING_STUB_SECTION
2826 target, new_relaxed_sections, task);
2829 if (!new_relaxed_sections.empty())
2830 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
2832 // Update the section offsets
2833 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
2835 The_aarch64_relobj* relobj = static_cast<The_aarch64_relobj*>(
2836 new_relaxed_sections[i]->relobj());
2837 unsigned int shndx = new_relaxed_sections[i]->shndx();
2838 // Tell AArch64_relobj that this input section is converted.
2839 relobj->convert_input_section_to_relaxed_section(shndx);
2841 } // End of AArch64_output_section::group_sections
2844 AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
2847 // The aarch64 target class.
2849 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
2850 template<int size, bool big_endian>
2851 class Target_aarch64 : public Sized_target<size, big_endian>
2854 typedef Target_aarch64<size, big_endian> This;
2855 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
2857 typedef Relocate_info<size, big_endian> The_relocate_info;
2858 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2859 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2860 typedef Reloc_stub<size, big_endian> The_reloc_stub;
2861 typedef Erratum_stub<size, big_endian> The_erratum_stub;
2862 typedef typename Reloc_stub<size, big_endian>::Key The_reloc_stub_key;
2863 typedef Stub_table<size, big_endian> The_stub_table;
2864 typedef std::vector<The_stub_table*> Stub_table_list;
2865 typedef typename Stub_table_list::iterator Stub_table_iterator;
2866 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2867 typedef AArch64_output_section<size, big_endian> The_aarch64_output_section;
2868 typedef Unordered_map<Section_id,
2869 AArch64_input_section<size, big_endian>*,
2870 Section_id_hash> AArch64_input_section_map;
2871 typedef AArch64_insn_utilities<big_endian> Insn_utilities;
2872 const static int TCB_SIZE = size / 8 * 2;
2874 Target_aarch64(const Target::Target_info* info = &aarch64_info)
2875 : Sized_target<size, big_endian>(info),
2876 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
2877 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
2878 rela_irelative_(NULL), copy_relocs_(elfcpp::R_AARCH64_COPY),
2879 got_mod_index_offset_(-1U),
2880 tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
2881 stub_tables_(), stub_group_size_(0), aarch64_input_section_map_()
2884 // Scan the relocations to determine unreferenced sections for
2885 // garbage collection.
2887 gc_process_relocs(Symbol_table* symtab,
2889 Sized_relobj_file<size, big_endian>* object,
2890 unsigned int data_shndx,
2891 unsigned int sh_type,
2892 const unsigned char* prelocs,
2894 Output_section* output_section,
2895 bool needs_special_offset_handling,
2896 size_t local_symbol_count,
2897 const unsigned char* plocal_symbols);
2899 // Scan the relocations to look for symbol adjustments.
2901 scan_relocs(Symbol_table* symtab,
2903 Sized_relobj_file<size, big_endian>* object,
2904 unsigned int data_shndx,
2905 unsigned int sh_type,
2906 const unsigned char* prelocs,
2908 Output_section* output_section,
2909 bool needs_special_offset_handling,
2910 size_t local_symbol_count,
2911 const unsigned char* plocal_symbols);
2913 // Finalize the sections.
2915 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2917 // Return the value to use for a dynamic which requires special
2920 do_dynsym_value(const Symbol*) const;
2922 // Relocate a section.
2924 relocate_section(const Relocate_info<size, big_endian>*,
2925 unsigned int sh_type,
2926 const unsigned char* prelocs,
2928 Output_section* output_section,
2929 bool needs_special_offset_handling,
2930 unsigned char* view,
2931 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2932 section_size_type view_size,
2933 const Reloc_symbol_changes*);
2935 // Scan the relocs during a relocatable link.
2937 scan_relocatable_relocs(Symbol_table* symtab,
2939 Sized_relobj_file<size, big_endian>* object,
2940 unsigned int data_shndx,
2941 unsigned int sh_type,
2942 const unsigned char* prelocs,
2944 Output_section* output_section,
2945 bool needs_special_offset_handling,
2946 size_t local_symbol_count,
2947 const unsigned char* plocal_symbols,
2948 Relocatable_relocs*);
2950 // Scan the relocs for --emit-relocs.
2952 emit_relocs_scan(Symbol_table* symtab,
2954 Sized_relobj_file<size, big_endian>* object,
2955 unsigned int data_shndx,
2956 unsigned int sh_type,
2957 const unsigned char* prelocs,
2959 Output_section* output_section,
2960 bool needs_special_offset_handling,
2961 size_t local_symbol_count,
2962 const unsigned char* plocal_syms,
2963 Relocatable_relocs* rr);
2965 // Relocate a section during a relocatable link.
2968 const Relocate_info<size, big_endian>*,
2969 unsigned int sh_type,
2970 const unsigned char* prelocs,
2972 Output_section* output_section,
2973 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
2974 unsigned char* view,
2975 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2976 section_size_type view_size,
2977 unsigned char* reloc_view,
2978 section_size_type reloc_view_size);
2980 // Return the symbol index to use for a target specific relocation.
2981 // The only target specific relocation is R_AARCH64_TLSDESC for a
2982 // local symbol, which is an absolute reloc.
2984 do_reloc_symbol_index(void*, unsigned int r_type) const
2986 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
2990 // Return the addend to use for a target specific relocation.
2992 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
2994 // Return the PLT section.
2996 do_plt_address_for_global(const Symbol* gsym) const
2997 { return this->plt_section()->address_for_global(gsym); }
3000 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
3001 { return this->plt_section()->address_for_local(relobj, symndx); }
3003 // This function should be defined in targets that can use relocation
3004 // types to determine (implemented in local_reloc_may_be_function_pointer
3005 // and global_reloc_may_be_function_pointer)
3006 // if a function's pointer is taken. ICF uses this in safe mode to only
3007 // fold those functions whose pointer is defintely not taken.
3009 do_can_check_for_function_pointers() const
3012 // Return the number of entries in the PLT.
3014 plt_entry_count() const;
3016 //Return the offset of the first non-reserved PLT entry.
3018 first_plt_entry_offset() const;
3020 // Return the size of each PLT entry.
3022 plt_entry_size() const;
3024 // Create a stub table.
3026 new_stub_table(The_aarch64_input_section*);
3028 // Create an aarch64 input section.
3029 The_aarch64_input_section*
3030 new_aarch64_input_section(Relobj*, unsigned int);
3032 // Find an aarch64 input section instance for a given OBJ and SHNDX.
3033 The_aarch64_input_section*
3034 find_aarch64_input_section(Relobj*, unsigned int) const;
3036 // Return the thread control block size.
3038 tcb_size() const { return This::TCB_SIZE; }
3040 // Scan a section for stub generation.
3042 scan_section_for_stubs(const Relocate_info<size, big_endian>*, unsigned int,
3043 const unsigned char*, size_t, Output_section*,
3044 bool, const unsigned char*,
3048 // Scan a relocation section for stub.
3049 template<int sh_type>
3051 scan_reloc_section_for_stubs(
3052 const The_relocate_info* relinfo,
3053 const unsigned char* prelocs,
3055 Output_section* output_section,
3056 bool needs_special_offset_handling,
3057 const unsigned char* view,
3058 Address view_address,
3061 // Relocate a single reloc stub.
3063 relocate_reloc_stub(The_reloc_stub*, const Relocate_info<size, big_endian>*,
3064 Output_section*, unsigned char*, Address,
3067 // Get the default AArch64 target.
3071 gold_assert(parameters->target().machine_code() == elfcpp::EM_AARCH64
3072 && parameters->target().get_size() == size
3073 && parameters->target().is_big_endian() == big_endian);
3074 return static_cast<This*>(parameters->sized_target<size, big_endian>());
3078 // Scan erratum 843419 for a part of a section.
3080 scan_erratum_843419_span(
3081 AArch64_relobj<size, big_endian>*,
3083 const section_size_type,
3084 const section_size_type,
3088 // Scan erratum 835769 for a part of a section.
3090 scan_erratum_835769_span(
3091 AArch64_relobj<size, big_endian>*,
3093 const section_size_type,
3094 const section_size_type,
3100 do_select_as_default_target()
3102 gold_assert(aarch64_reloc_property_table == NULL);
3103 aarch64_reloc_property_table = new AArch64_reloc_property_table();
3106 // Add a new reloc argument, returning the index in the vector.
3108 add_tlsdesc_info(Sized_relobj_file<size, big_endian>* object,
3111 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
3112 return this->tlsdesc_reloc_info_.size() - 1;
3115 virtual Output_data_plt_aarch64<size, big_endian>*
3116 do_make_data_plt(Layout* layout,
3117 Output_data_got_aarch64<size, big_endian>* got,
3118 Output_data_space* got_plt,
3119 Output_data_space* got_irelative)
3121 return new Output_data_plt_aarch64_standard<size, big_endian>(
3122 layout, got, got_plt, got_irelative);
3126 // do_make_elf_object to override the same function in the base class.
3128 do_make_elf_object(const std::string&, Input_file*, off_t,
3129 const elfcpp::Ehdr<size, big_endian>&);
3131 Output_data_plt_aarch64<size, big_endian>*
3132 make_data_plt(Layout* layout,
3133 Output_data_got_aarch64<size, big_endian>* got,
3134 Output_data_space* got_plt,
3135 Output_data_space* got_irelative)
3137 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
3140 // We only need to generate stubs, and hence perform relaxation if we are
3141 // not doing relocatable linking.
3143 do_may_relax() const
3144 { return !parameters->options().relocatable(); }
3146 // Relaxation hook. This is where we do stub generation.
3148 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
3151 group_sections(Layout* layout,
3152 section_size_type group_size,
3153 bool stubs_always_after_branch,
3157 scan_reloc_for_stub(const The_relocate_info*, unsigned int,
3158 const Sized_symbol<size>*, unsigned int,
3159 const Symbol_value<size>*,
3160 typename elfcpp::Elf_types<size>::Elf_Swxword,
3163 // Make an output section.
3165 do_make_output_section(const char* name, elfcpp::Elf_Word type,
3166 elfcpp::Elf_Xword flags)
3167 { return new The_aarch64_output_section(name, type, flags); }
3170 // The class which scans relocations.
3175 : issued_non_pic_error_(false)
3179 local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
3180 Sized_relobj_file<size, big_endian>* object,
3181 unsigned int data_shndx,
3182 Output_section* output_section,
3183 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
3184 const elfcpp::Sym<size, big_endian>& lsym,
3188 global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
3189 Sized_relobj_file<size, big_endian>* object,
3190 unsigned int data_shndx,
3191 Output_section* output_section,
3192 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
3196 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
3197 Target_aarch64<size, big_endian>* ,
3198 Sized_relobj_file<size, big_endian>* ,
3201 const elfcpp::Rela<size, big_endian>& ,
3202 unsigned int r_type,
3203 const elfcpp::Sym<size, big_endian>&);
3206 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
3207 Target_aarch64<size, big_endian>* ,
3208 Sized_relobj_file<size, big_endian>* ,
3211 const elfcpp::Rela<size, big_endian>& ,
3212 unsigned int r_type,
3217 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3218 unsigned int r_type);
3221 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3222 unsigned int r_type, Symbol*);
3225 possible_function_pointer_reloc(unsigned int r_type);
3228 check_non_pic(Relobj*, unsigned int r_type);
3231 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
3232 unsigned int r_type);
3234 // Whether we have issued an error about a non-PIC compilation.
3235 bool issued_non_pic_error_;
3238 // The class which implements relocation.
3243 : skip_call_tls_get_addr_(false)
3249 // Do a relocation. Return false if the caller should not issue
3250 // any warnings about this relocation.
3252 relocate(const Relocate_info<size, big_endian>*, unsigned int,
3253 Target_aarch64*, Output_section*, size_t, const unsigned char*,
3254 const Sized_symbol<size>*, const Symbol_value<size>*,
3255 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
3259 inline typename AArch64_relocate_functions<size, big_endian>::Status
3260 relocate_tls(const Relocate_info<size, big_endian>*,
3261 Target_aarch64<size, big_endian>*,
3263 const elfcpp::Rela<size, big_endian>&,
3264 unsigned int r_type, const Sized_symbol<size>*,
3265 const Symbol_value<size>*,
3267 typename elfcpp::Elf_types<size>::Elf_Addr);
3269 inline typename AArch64_relocate_functions<size, big_endian>::Status
3271 const Relocate_info<size, big_endian>*,
3272 Target_aarch64<size, big_endian>*,
3273 const elfcpp::Rela<size, big_endian>&,
3276 const Symbol_value<size>*);
3278 inline typename AArch64_relocate_functions<size, big_endian>::Status
3280 const Relocate_info<size, big_endian>*,
3281 Target_aarch64<size, big_endian>*,
3282 const elfcpp::Rela<size, big_endian>&,
3285 const Symbol_value<size>*);
3287 inline typename AArch64_relocate_functions<size, big_endian>::Status
3289 const Relocate_info<size, big_endian>*,
3290 Target_aarch64<size, big_endian>*,
3291 const elfcpp::Rela<size, big_endian>&,
3294 const Symbol_value<size>*);
3296 inline typename AArch64_relocate_functions<size, big_endian>::Status
3298 const Relocate_info<size, big_endian>*,
3299 Target_aarch64<size, big_endian>*,
3300 const elfcpp::Rela<size, big_endian>&,
3303 const Symbol_value<size>*);
3305 inline typename AArch64_relocate_functions<size, big_endian>::Status
3307 const Relocate_info<size, big_endian>*,
3308 Target_aarch64<size, big_endian>*,
3309 const elfcpp::Rela<size, big_endian>&,
3312 const Symbol_value<size>*,
3313 typename elfcpp::Elf_types<size>::Elf_Addr,
3314 typename elfcpp::Elf_types<size>::Elf_Addr);
3316 bool skip_call_tls_get_addr_;
3318 }; // End of class Relocate
3320 // Adjust TLS relocation type based on the options and whether this
3321 // is a local symbol.
3322 static tls::Tls_optimization
3323 optimize_tls_reloc(bool is_final, int r_type);
3325 // Get the GOT section, creating it if necessary.
3326 Output_data_got_aarch64<size, big_endian>*
3327 got_section(Symbol_table*, Layout*);
3329 // Get the GOT PLT section.
3331 got_plt_section() const
3333 gold_assert(this->got_plt_ != NULL);
3334 return this->got_plt_;
3337 // Get the GOT section for TLSDESC entries.
3338 Output_data_got<size, big_endian>*
3339 got_tlsdesc_section() const
3341 gold_assert(this->got_tlsdesc_ != NULL);
3342 return this->got_tlsdesc_;
3345 // Create the PLT section.
3347 make_plt_section(Symbol_table* symtab, Layout* layout);
3349 // Create a PLT entry for a global symbol.
3351 make_plt_entry(Symbol_table*, Layout*, Symbol*);
3353 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
3355 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
3356 Sized_relobj_file<size, big_endian>* relobj,
3357 unsigned int local_sym_index);
3359 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
3361 define_tls_base_symbol(Symbol_table*, Layout*);
3363 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
3365 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
3367 // Create a GOT entry for the TLS module index.
3369 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
3370 Sized_relobj_file<size, big_endian>* object);
3372 // Get the PLT section.
3373 Output_data_plt_aarch64<size, big_endian>*
3376 gold_assert(this->plt_ != NULL);
3380 // Helper method to create erratum stubs for ST_E_843419 and ST_E_835769. For
3381 // ST_E_843419, we need an additional field for adrp offset.
3382 void create_erratum_stub(
3383 AArch64_relobj<size, big_endian>* relobj,
3385 section_size_type erratum_insn_offset,
3386 Address erratum_address,
3387 typename Insn_utilities::Insntype erratum_insn,
3389 unsigned int e843419_adrp_offset=0);
3391 // Return whether this is a 3-insn erratum sequence.
3392 bool is_erratum_843419_sequence(
3393 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
3394 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
3395 typename elfcpp::Swap<32,big_endian>::Valtype insn3);
3397 // Return whether this is a 835769 sequence.
3398 // (Similarly implemented as in elfnn-aarch64.c.)
3399 bool is_erratum_835769_sequence(
3400 typename elfcpp::Swap<32,big_endian>::Valtype,
3401 typename elfcpp::Swap<32,big_endian>::Valtype);
3403 // Get the dynamic reloc section, creating it if necessary.
3405 rela_dyn_section(Layout*);
3407 // Get the section to use for TLSDESC relocations.
3409 rela_tlsdesc_section(Layout*) const;
3411 // Get the section to use for IRELATIVE relocations.
3413 rela_irelative_section(Layout*);
3415 // Add a potential copy relocation.
3417 copy_reloc(Symbol_table* symtab, Layout* layout,
3418 Sized_relobj_file<size, big_endian>* object,
3419 unsigned int shndx, Output_section* output_section,
3420 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
3422 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
3423 this->copy_relocs_.copy_reloc(symtab, layout,
3424 symtab->get_sized_symbol<size>(sym),
3425 object, shndx, output_section,
3426 r_type, reloc.get_r_offset(),
3427 reloc.get_r_addend(),
3428 this->rela_dyn_section(layout));
3431 // Information about this specific target which we pass to the
3432 // general Target structure.
3433 static const Target::Target_info aarch64_info;
3435 // The types of GOT entries needed for this platform.
3436 // These values are exposed to the ABI in an incremental link.
3437 // Do not renumber existing values without changing the version
3438 // number of the .gnu_incremental_inputs section.
3441 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
3442 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
3443 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
3444 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
3447 // This type is used as the argument to the target specific
3448 // relocation routines. The only target specific reloc is
3449 // R_AARCh64_TLSDESC against a local symbol.
3452 Tlsdesc_info(Sized_relobj_file<size, big_endian>* a_object,
3453 unsigned int a_r_sym)
3454 : object(a_object), r_sym(a_r_sym)
3457 // The object in which the local symbol is defined.
3458 Sized_relobj_file<size, big_endian>* object;
3459 // The local symbol index in the object.
3464 Output_data_got_aarch64<size, big_endian>* got_;
3466 Output_data_plt_aarch64<size, big_endian>* plt_;
3467 // The GOT PLT section.
3468 Output_data_space* got_plt_;
3469 // The GOT section for IRELATIVE relocations.
3470 Output_data_space* got_irelative_;
3471 // The GOT section for TLSDESC relocations.
3472 Output_data_got<size, big_endian>* got_tlsdesc_;
3473 // The _GLOBAL_OFFSET_TABLE_ symbol.
3474 Symbol* global_offset_table_;
3475 // The dynamic reloc section.
3476 Reloc_section* rela_dyn_;
3477 // The section to use for IRELATIVE relocs.
3478 Reloc_section* rela_irelative_;
3479 // Relocs saved to avoid a COPY reloc.
3480 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
3481 // Offset of the GOT entry for the TLS module index.
3482 unsigned int got_mod_index_offset_;
3483 // We handle R_AARCH64_TLSDESC against a local symbol as a target
3484 // specific relocation. Here we store the object and local symbol
3485 // index for the relocation.
3486 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
3487 // True if the _TLS_MODULE_BASE_ symbol has been defined.
3488 bool tls_base_symbol_defined_;
3489 // List of stub_tables
3490 Stub_table_list stub_tables_;
3491 // Actual stub group size
3492 section_size_type stub_group_size_;
3493 AArch64_input_section_map aarch64_input_section_map_;
3494 }; // End of Target_aarch64
3498 const Target::Target_info Target_aarch64<64, false>::aarch64_info =
3501 false, // is_big_endian
3502 elfcpp::EM_AARCH64, // machine_code
3503 false, // has_make_symbol
3504 false, // has_resolve
3505 false, // has_code_fill
3506 true, // is_default_stack_executable
3507 true, // can_icf_inline_merge_sections
3509 "/lib/ld.so.1", // program interpreter
3510 0x400000, // default_text_segment_address
3511 0x10000, // abi_pagesize (overridable by -z max-page-size)
3512 0x1000, // common_pagesize (overridable by -z common-page-size)
3513 false, // isolate_execinstr
3515 elfcpp::SHN_UNDEF, // small_common_shndx
3516 elfcpp::SHN_UNDEF, // large_common_shndx
3517 0, // small_common_section_flags
3518 0, // large_common_section_flags
3519 NULL, // attributes_section
3520 NULL, // attributes_vendor
3521 "_start", // entry_symbol_name
3522 32, // hash_entry_size
3526 const Target::Target_info Target_aarch64<32, false>::aarch64_info =
3529 false, // is_big_endian
3530 elfcpp::EM_AARCH64, // machine_code
3531 false, // has_make_symbol
3532 false, // has_resolve
3533 false, // has_code_fill
3534 true, // is_default_stack_executable
3535 false, // can_icf_inline_merge_sections
3537 "/lib/ld.so.1", // program interpreter
3538 0x400000, // default_text_segment_address
3539 0x10000, // abi_pagesize (overridable by -z max-page-size)
3540 0x1000, // common_pagesize (overridable by -z common-page-size)
3541 false, // isolate_execinstr
3543 elfcpp::SHN_UNDEF, // small_common_shndx
3544 elfcpp::SHN_UNDEF, // large_common_shndx
3545 0, // small_common_section_flags
3546 0, // large_common_section_flags
3547 NULL, // attributes_section
3548 NULL, // attributes_vendor
3549 "_start", // entry_symbol_name
3550 32, // hash_entry_size
3554 const Target::Target_info Target_aarch64<64, true>::aarch64_info =
3557 true, // is_big_endian
3558 elfcpp::EM_AARCH64, // machine_code
3559 false, // has_make_symbol
3560 false, // has_resolve
3561 false, // has_code_fill
3562 true, // is_default_stack_executable
3563 true, // can_icf_inline_merge_sections
3565 "/lib/ld.so.1", // program interpreter
3566 0x400000, // default_text_segment_address
3567 0x10000, // abi_pagesize (overridable by -z max-page-size)
3568 0x1000, // common_pagesize (overridable by -z common-page-size)
3569 false, // isolate_execinstr
3571 elfcpp::SHN_UNDEF, // small_common_shndx
3572 elfcpp::SHN_UNDEF, // large_common_shndx
3573 0, // small_common_section_flags
3574 0, // large_common_section_flags
3575 NULL, // attributes_section
3576 NULL, // attributes_vendor
3577 "_start", // entry_symbol_name
3578 32, // hash_entry_size
3582 const Target::Target_info Target_aarch64<32, true>::aarch64_info =
3585 true, // is_big_endian
3586 elfcpp::EM_AARCH64, // machine_code
3587 false, // has_make_symbol
3588 false, // has_resolve
3589 false, // has_code_fill
3590 true, // is_default_stack_executable
3591 false, // can_icf_inline_merge_sections
3593 "/lib/ld.so.1", // program interpreter
3594 0x400000, // default_text_segment_address
3595 0x10000, // abi_pagesize (overridable by -z max-page-size)
3596 0x1000, // common_pagesize (overridable by -z common-page-size)
3597 false, // isolate_execinstr
3599 elfcpp::SHN_UNDEF, // small_common_shndx
3600 elfcpp::SHN_UNDEF, // large_common_shndx
3601 0, // small_common_section_flags
3602 0, // large_common_section_flags
3603 NULL, // attributes_section
3604 NULL, // attributes_vendor
3605 "_start", // entry_symbol_name
3606 32, // hash_entry_size
3609 // Get the GOT section, creating it if necessary.
3611 template<int size, bool big_endian>
3612 Output_data_got_aarch64<size, big_endian>*
3613 Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
3616 if (this->got_ == NULL)
3618 gold_assert(symtab != NULL && layout != NULL);
3620 // When using -z now, we can treat .got.plt as a relro section.
3621 // Without -z now, it is modified after program startup by lazy
3623 bool is_got_plt_relro = parameters->options().now();
3624 Output_section_order got_order = (is_got_plt_relro
3626 : ORDER_RELRO_LAST);
3627 Output_section_order got_plt_order = (is_got_plt_relro
3629 : ORDER_NON_RELRO_FIRST);
3631 // Layout of .got and .got.plt sections.
3632 // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
3634 // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
3635 // .gotplt[1] reserved for ld.so (resolver)
3636 // .gotplt[2] reserved
3638 // Generate .got section.
3639 this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
3641 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3642 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
3643 this->got_, got_order, true);
3644 // The first word of GOT is reserved for the address of .dynamic.
3645 // We put 0 here now. The value will be replaced later in
3646 // Output_data_got_aarch64::do_write.
3647 this->got_->add_constant(0);
3649 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3650 // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
3651 // even if there is a .got.plt section.
3652 this->global_offset_table_ =
3653 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3654 Symbol_table::PREDEFINED,
3656 0, 0, elfcpp::STT_OBJECT,
3658 elfcpp::STV_HIDDEN, 0,
3661 // Generate .got.plt section.
3662 this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
3663 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3665 | elfcpp::SHF_WRITE),
3666 this->got_plt_, got_plt_order,
3669 // The first three entries are reserved.
3670 this->got_plt_->set_current_data_size(
3671 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
3673 // If there are any IRELATIVE relocations, they get GOT entries
3674 // in .got.plt after the jump slot entries.
3675 this->got_irelative_ = new Output_data_space(size / 8,
3676 "** GOT IRELATIVE PLT");
3677 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3679 | elfcpp::SHF_WRITE),
3680 this->got_irelative_,
3684 // If there are any TLSDESC relocations, they get GOT entries in
3685 // .got.plt after the jump slot and IRELATIVE entries.
3686 this->got_tlsdesc_ = new Output_data_got<size, big_endian>();
3687 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3689 | elfcpp::SHF_WRITE),
3694 if (!is_got_plt_relro)
3696 // Those bytes can go into the relro segment.
3697 layout->increase_relro(
3698 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
3705 // Get the dynamic reloc section, creating it if necessary.
3707 template<int size, bool big_endian>
3708 typename Target_aarch64<size, big_endian>::Reloc_section*
3709 Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
3711 if (this->rela_dyn_ == NULL)
3713 gold_assert(layout != NULL);
3714 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3715 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3716 elfcpp::SHF_ALLOC, this->rela_dyn_,
3717 ORDER_DYNAMIC_RELOCS, false);
3719 return this->rela_dyn_;
3722 // Get the section to use for IRELATIVE relocs, creating it if
3723 // necessary. These go in .rela.dyn, but only after all other dynamic
3724 // relocations. They need to follow the other dynamic relocations so
3725 // that they can refer to global variables initialized by those
3728 template<int size, bool big_endian>
3729 typename Target_aarch64<size, big_endian>::Reloc_section*
3730 Target_aarch64<size, big_endian>::rela_irelative_section(Layout* layout)
3732 if (this->rela_irelative_ == NULL)
3734 // Make sure we have already created the dynamic reloc section.
3735 this->rela_dyn_section(layout);
3736 this->rela_irelative_ = new Reloc_section(false);
3737 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3738 elfcpp::SHF_ALLOC, this->rela_irelative_,
3739 ORDER_DYNAMIC_RELOCS, false);
3740 gold_assert(this->rela_dyn_->output_section()
3741 == this->rela_irelative_->output_section());
3743 return this->rela_irelative_;
3747 // do_make_elf_object to override the same function in the base class. We need
3748 // to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
3749 // store backend specific information. Hence we need to have our own ELF object
3752 template<int size, bool big_endian>
3754 Target_aarch64<size, big_endian>::do_make_elf_object(
3755 const std::string& name,
3756 Input_file* input_file,
3757 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
3759 int et = ehdr.get_e_type();
3760 // ET_EXEC files are valid input for --just-symbols/-R,
3761 // and we treat them as relocatable objects.
3762 if (et == elfcpp::ET_EXEC && input_file->just_symbols())
3763 return Sized_target<size, big_endian>::do_make_elf_object(
3764 name, input_file, offset, ehdr);
3765 else if (et == elfcpp::ET_REL)
3767 AArch64_relobj<size, big_endian>* obj =
3768 new AArch64_relobj<size, big_endian>(name, input_file, offset, ehdr);
3772 else if (et == elfcpp::ET_DYN)
3774 // Keep base implementation.
3775 Sized_dynobj<size, big_endian>* obj =
3776 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
3782 gold_error(_("%s: unsupported ELF file type %d"),
3789 // Scan a relocation for stub generation.
3791 template<int size, bool big_endian>
3793 Target_aarch64<size, big_endian>::scan_reloc_for_stub(
3794 const Relocate_info<size, big_endian>* relinfo,
3795 unsigned int r_type,
3796 const Sized_symbol<size>* gsym,
3798 const Symbol_value<size>* psymval,
3799 typename elfcpp::Elf_types<size>::Elf_Swxword addend,
3802 const AArch64_relobj<size, big_endian>* aarch64_relobj =
3803 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3805 Symbol_value<size> symval;
3808 const AArch64_reloc_property* arp = aarch64_reloc_property_table->
3809 get_reloc_property(r_type);
3810 if (gsym->use_plt_offset(arp->reference_flags()))
3812 // This uses a PLT, change the symbol value.
3813 symval.set_output_value(this->plt_address_for_global(gsym));
3816 else if (gsym->is_undefined())
3818 // There is no need to generate a stub symbol if the original symbol
3820 gold_debug(DEBUG_TARGET,
3821 "stub: not creating a stub for undefined symbol %s in file %s",
3822 gsym->name(), aarch64_relobj->name().c_str());
3827 // Get the symbol value.
3828 typename Symbol_value<size>::Value value = psymval->value(aarch64_relobj, 0);
3830 // Owing to pipelining, the PC relative branches below actually skip
3831 // two instructions when the branch offset is 0.
3832 Address destination = static_cast<Address>(-1);
3835 case elfcpp::R_AARCH64_CALL26:
3836 case elfcpp::R_AARCH64_JUMP26:
3837 destination = value + addend;
3843 int stub_type = The_reloc_stub::
3844 stub_type_for_reloc(r_type, address, destination);
3845 if (stub_type == ST_NONE)
3848 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
3849 gold_assert(stub_table != NULL);
3851 The_reloc_stub_key key(stub_type, gsym, aarch64_relobj, r_sym, addend);
3852 The_reloc_stub* stub = stub_table->find_reloc_stub(key);
3855 stub = new The_reloc_stub(stub_type);
3856 stub_table->add_reloc_stub(stub, key);
3858 stub->set_destination_address(destination);
3859 } // End of Target_aarch64::scan_reloc_for_stub
3862 // This function scans a relocation section for stub generation.
3863 // The template parameter Relocate must be a class type which provides
3864 // a single function, relocate(), which implements the machine
3865 // specific part of a relocation.
3867 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
3868 // SHT_REL or SHT_RELA.
3870 // PRELOCS points to the relocation data. RELOC_COUNT is the number
3871 // of relocs. OUTPUT_SECTION is the output section.
3872 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
3873 // mapped to output offsets.
3875 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
3876 // VIEW_SIZE is the size. These refer to the input section, unless
3877 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
3878 // the output section.
3880 template<int size, bool big_endian>
3881 template<int sh_type>
3883 Target_aarch64<size, big_endian>::scan_reloc_section_for_stubs(
3884 const Relocate_info<size, big_endian>* relinfo,
3885 const unsigned char* prelocs,
3887 Output_section* /*output_section*/,
3888 bool /*needs_special_offset_handling*/,
3889 const unsigned char* /*view*/,
3890 Address view_address,
3893 typedef typename Reloc_types<sh_type,size,big_endian>::Reloc Reltype;
3895 const int reloc_size =
3896 Reloc_types<sh_type,size,big_endian>::reloc_size;
3897 AArch64_relobj<size, big_endian>* object =
3898 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3899 unsigned int local_count = object->local_symbol_count();
3901 gold::Default_comdat_behavior default_comdat_behavior;
3902 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
3904 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
3906 Reltype reloc(prelocs);
3907 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
3908 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3909 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
3910 if (r_type != elfcpp::R_AARCH64_CALL26
3911 && r_type != elfcpp::R_AARCH64_JUMP26)
3914 section_offset_type offset =
3915 convert_to_section_size_type(reloc.get_r_offset());
3918 typename elfcpp::Elf_types<size>::Elf_Swxword addend =
3919 reloc.get_r_addend();
3921 const Sized_symbol<size>* sym;
3922 Symbol_value<size> symval;
3923 const Symbol_value<size> *psymval;
3924 bool is_defined_in_discarded_section;
3926 if (r_sym < local_count)
3929 psymval = object->local_symbol(r_sym);
3931 // If the local symbol belongs to a section we are discarding,
3932 // and that section is a debug section, try to find the
3933 // corresponding kept section and map this symbol to its
3934 // counterpart in the kept section. The symbol must not
3935 // correspond to a section we are folding.
3937 shndx = psymval->input_shndx(&is_ordinary);
3938 is_defined_in_discarded_section =
3940 && shndx != elfcpp::SHN_UNDEF
3941 && !object->is_section_included(shndx)
3942 && !relinfo->symtab->is_section_folded(object, shndx));
3944 // We need to compute the would-be final value of this local
3946 if (!is_defined_in_discarded_section)
3948 typedef Sized_relobj_file<size, big_endian> ObjType;
3949 if (psymval->is_section_symbol())
3950 symval.set_is_section_symbol();
3951 typename ObjType::Compute_final_local_value_status status =
3952 object->compute_final_local_value(r_sym, psymval, &symval,
3954 if (status == ObjType::CFLV_OK)
3956 // Currently we cannot handle a branch to a target in
3957 // a merged section. If this is the case, issue an error
3958 // and also free the merge symbol value.
3959 if (!symval.has_output_value())
3961 const std::string& section_name =
3962 object->section_name(shndx);
3963 object->error(_("cannot handle branch to local %u "
3964 "in a merged section %s"),
3965 r_sym, section_name.c_str());
3971 // We cannot determine the final value.
3979 gsym = object->global_symbol(r_sym);
3980 gold_assert(gsym != NULL);
3981 if (gsym->is_forwarder())
3982 gsym = relinfo->symtab->resolve_forwards(gsym);
3984 sym = static_cast<const Sized_symbol<size>*>(gsym);
3985 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
3986 symval.set_output_symtab_index(sym->symtab_index());
3988 symval.set_no_output_symtab_entry();
3990 // We need to compute the would-be final value of this global
3992 const Symbol_table* symtab = relinfo->symtab;
3993 const Sized_symbol<size>* sized_symbol =
3994 symtab->get_sized_symbol<size>(gsym);
3995 Symbol_table::Compute_final_value_status status;
3996 typename elfcpp::Elf_types<size>::Elf_Addr value =
3997 symtab->compute_final_value<size>(sized_symbol, &status);
3999 // Skip this if the symbol has not output section.
4000 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
4002 symval.set_output_value(value);
4004 if (gsym->type() == elfcpp::STT_TLS)
4005 symval.set_is_tls_symbol();
4006 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
4007 symval.set_is_ifunc_symbol();
4010 is_defined_in_discarded_section =
4011 (gsym->is_defined_in_discarded_section()
4012 && gsym->is_undefined());
4016 Symbol_value<size> symval2;
4017 if (is_defined_in_discarded_section)
4019 if (comdat_behavior == CB_UNDETERMINED)
4021 std::string name = object->section_name(relinfo->data_shndx);
4022 comdat_behavior = default_comdat_behavior.get(name.c_str());
4024 if (comdat_behavior == CB_PRETEND)
4027 typename elfcpp::Elf_types<size>::Elf_Addr value =
4028 object->map_to_kept_section(shndx, &found);
4030 symval2.set_output_value(value + psymval->input_value());
4032 symval2.set_output_value(0);
4036 if (comdat_behavior == CB_WARNING)
4037 gold_warning_at_location(relinfo, i, offset,
4038 _("relocation refers to discarded "
4040 symval2.set_output_value(0);
4042 symval2.set_no_output_symtab_entry();
4046 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
4047 addend, view_address + offset);
4048 } // End of iterating relocs in a section
4049 } // End of Target_aarch64::scan_reloc_section_for_stubs
4052 // Scan an input section for stub generation.
4054 template<int size, bool big_endian>
4056 Target_aarch64<size, big_endian>::scan_section_for_stubs(
4057 const Relocate_info<size, big_endian>* relinfo,
4058 unsigned int sh_type,
4059 const unsigned char* prelocs,
4061 Output_section* output_section,
4062 bool needs_special_offset_handling,
4063 const unsigned char* view,
4064 Address view_address,
4065 section_size_type view_size)
4067 gold_assert(sh_type == elfcpp::SHT_RELA);
4068 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
4073 needs_special_offset_handling,
4080 // Relocate a single reloc stub.
4082 template<int size, bool big_endian>
4083 void Target_aarch64<size, big_endian>::
4084 relocate_reloc_stub(The_reloc_stub* stub,
4085 const The_relocate_info*,
4087 unsigned char* view,
4091 typedef AArch64_relocate_functions<size, big_endian> The_reloc_functions;
4092 typedef typename The_reloc_functions::Status The_reloc_functions_status;
4093 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
4095 Insntype* ip = reinterpret_cast<Insntype*>(view);
4096 int insn_number = stub->insn_num();
4097 const uint32_t* insns = stub->insns();
4098 // Check the insns are really those stub insns.
4099 for (int i = 0; i < insn_number; ++i)
4101 Insntype insn = elfcpp::Swap<32,big_endian>::readval(ip + i);
4102 gold_assert(((uint32_t)insn == insns[i]));
4105 Address dest = stub->destination_address();
4107 switch(stub->type())
4109 case ST_ADRP_BRANCH:
4111 // 1st reloc is ADR_PREL_PG_HI21
4112 The_reloc_functions_status status =
4113 The_reloc_functions::adrp(view, dest, address);
4114 // An error should never arise in the above step. If so, please
4115 // check 'aarch64_valid_for_adrp_p'.
4116 gold_assert(status == The_reloc_functions::STATUS_OKAY);
4118 // 2nd reloc is ADD_ABS_LO12_NC
4119 const AArch64_reloc_property* arp =
4120 aarch64_reloc_property_table->get_reloc_property(
4121 elfcpp::R_AARCH64_ADD_ABS_LO12_NC);
4122 gold_assert(arp != NULL);
4123 status = The_reloc_functions::template
4124 rela_general<32>(view + 4, dest, 0, arp);
4125 // An error should never arise, it is an "_NC" relocation.
4126 gold_assert(status == The_reloc_functions::STATUS_OKAY);
4130 case ST_LONG_BRANCH_ABS:
4131 // 1st reloc is R_AARCH64_PREL64, at offset 8
4132 elfcpp::Swap<64,big_endian>::writeval(view + 8, dest);
4135 case ST_LONG_BRANCH_PCREL:
4137 // "PC" calculation is the 2nd insn in the stub.
4138 uint64_t offset = dest - (address + 4);
4139 // Offset is placed at offset 4 and 5.
4140 elfcpp::Swap<64,big_endian>::writeval(view + 16, offset);
4150 // A class to handle the PLT data.
4151 // This is an abstract base class that handles most of the linker details
4152 // but does not know the actual contents of PLT entries. The derived
4153 // classes below fill in those details.
4155 template<int size, bool big_endian>
4156 class Output_data_plt_aarch64 : public Output_section_data
4159 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
4161 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4163 Output_data_plt_aarch64(Layout* layout,
4165 Output_data_got_aarch64<size, big_endian>* got,
4166 Output_data_space* got_plt,
4167 Output_data_space* got_irelative)
4168 : Output_section_data(addralign), tlsdesc_rel_(NULL), irelative_rel_(NULL),
4169 got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
4170 count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
4171 { this->init(layout); }
4173 // Initialize the PLT section.
4175 init(Layout* layout);
4177 // Add an entry to the PLT.
4179 add_entry(Symbol_table*, Layout*, Symbol* gsym);
4181 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
4183 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
4184 Sized_relobj_file<size, big_endian>* relobj,
4185 unsigned int local_sym_index);
4187 // Add the relocation for a PLT entry.
4189 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
4190 unsigned int got_offset);
4192 // Add the reserved TLSDESC_PLT entry to the PLT.
4194 reserve_tlsdesc_entry(unsigned int got_offset)
4195 { this->tlsdesc_got_offset_ = got_offset; }
4197 // Return true if a TLSDESC_PLT entry has been reserved.
4199 has_tlsdesc_entry() const
4200 { return this->tlsdesc_got_offset_ != -1U; }
4202 // Return the GOT offset for the reserved TLSDESC_PLT entry.
4204 get_tlsdesc_got_offset() const
4205 { return this->tlsdesc_got_offset_; }
4207 // Return the PLT offset of the reserved TLSDESC_PLT entry.
4209 get_tlsdesc_plt_offset() const
4211 return (this->first_plt_entry_offset() +
4212 (this->count_ + this->irelative_count_)
4213 * this->get_plt_entry_size());
4216 // Return the .rela.plt section data.
4219 { return this->rel_; }
4221 // Return where the TLSDESC relocations should go.
4223 rela_tlsdesc(Layout*);
4225 // Return where the IRELATIVE relocations should go in the PLT
4228 rela_irelative(Symbol_table*, Layout*);
4230 // Return whether we created a section for IRELATIVE relocations.
4232 has_irelative_section() const
4233 { return this->irelative_rel_ != NULL; }
4235 // Return the number of PLT entries.
4238 { return this->count_ + this->irelative_count_; }
4240 // Return the offset of the first non-reserved PLT entry.
4242 first_plt_entry_offset() const
4243 { return this->do_first_plt_entry_offset(); }
4245 // Return the size of a PLT entry.
4247 get_plt_entry_size() const
4248 { return this->do_get_plt_entry_size(); }
4250 // Return the reserved tlsdesc entry size.
4252 get_plt_tlsdesc_entry_size() const
4253 { return this->do_get_plt_tlsdesc_entry_size(); }
4255 // Return the PLT address to use for a global symbol.
4257 address_for_global(const Symbol*);
4259 // Return the PLT address to use for a local symbol.
4261 address_for_local(const Relobj*, unsigned int symndx);
4264 // Fill in the first PLT entry.
4266 fill_first_plt_entry(unsigned char* pov,
4267 Address got_address,
4268 Address plt_address)
4269 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
4271 // Fill in a normal PLT entry.
4273 fill_plt_entry(unsigned char* pov,
4274 Address got_address,
4275 Address plt_address,
4276 unsigned int got_offset,
4277 unsigned int plt_offset)
4279 this->do_fill_plt_entry(pov, got_address, plt_address,
4280 got_offset, plt_offset);
4283 // Fill in the reserved TLSDESC PLT entry.
4285 fill_tlsdesc_entry(unsigned char* pov,
4286 Address gotplt_address,
4287 Address plt_address,
4289 unsigned int tlsdesc_got_offset,
4290 unsigned int plt_offset)
4292 this->do_fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
4293 tlsdesc_got_offset, plt_offset);
4296 virtual unsigned int
4297 do_first_plt_entry_offset() const = 0;
4299 virtual unsigned int
4300 do_get_plt_entry_size() const = 0;
4302 virtual unsigned int
4303 do_get_plt_tlsdesc_entry_size() const = 0;
4306 do_fill_first_plt_entry(unsigned char* pov,
4308 Address plt_addr) = 0;
4311 do_fill_plt_entry(unsigned char* pov,
4312 Address got_address,
4313 Address plt_address,
4314 unsigned int got_offset,
4315 unsigned int plt_offset) = 0;
4318 do_fill_tlsdesc_entry(unsigned char* pov,
4319 Address gotplt_address,
4320 Address plt_address,
4322 unsigned int tlsdesc_got_offset,
4323 unsigned int plt_offset) = 0;
4326 do_adjust_output_section(Output_section* os);
4328 // Write to a map file.
4330 do_print_to_mapfile(Mapfile* mapfile) const
4331 { mapfile->print_output_data(this, _("** PLT")); }
4334 // Set the final size.
4336 set_final_data_size();
4338 // Write out the PLT data.
4340 do_write(Output_file*);
4342 // The reloc section.
4343 Reloc_section* rel_;
4345 // The TLSDESC relocs, if necessary. These must follow the regular
4347 Reloc_section* tlsdesc_rel_;
4349 // The IRELATIVE relocs, if necessary. These must follow the
4350 // regular PLT relocations.
4351 Reloc_section* irelative_rel_;
4353 // The .got section.
4354 Output_data_got_aarch64<size, big_endian>* got_;
4356 // The .got.plt section.
4357 Output_data_space* got_plt_;
4359 // The part of the .got.plt section used for IRELATIVE relocs.
4360 Output_data_space* got_irelative_;
4362 // The number of PLT entries.
4363 unsigned int count_;
4365 // Number of PLT entries with R_AARCH64_IRELATIVE relocs. These
4366 // follow the regular PLT entries.
4367 unsigned int irelative_count_;
4369 // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
4370 // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
4371 // indicates an offset is not allocated.
4372 unsigned int tlsdesc_got_offset_;
4375 // Initialize the PLT section.
4377 template<int size, bool big_endian>
4379 Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
4381 this->rel_ = new Reloc_section(false);
4382 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4383 elfcpp::SHF_ALLOC, this->rel_,
4384 ORDER_DYNAMIC_PLT_RELOCS, false);
4387 template<int size, bool big_endian>
4389 Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
4392 os->set_entsize(this->get_plt_entry_size());
4395 // Add an entry to the PLT.
4397 template<int size, bool big_endian>
4399 Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol_table* symtab,
4400 Layout* layout, Symbol* gsym)
4402 gold_assert(!gsym->has_plt_offset());
4404 unsigned int* pcount;
4405 unsigned int plt_reserved;
4406 Output_section_data_build* got;
4408 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4409 && gsym->can_use_relative_reloc(false))
4411 pcount = &this->irelative_count_;
4413 got = this->got_irelative_;
4417 pcount = &this->count_;
4418 plt_reserved = this->first_plt_entry_offset();
4419 got = this->got_plt_;
4422 gsym->set_plt_offset((*pcount) * this->get_plt_entry_size()
4427 section_offset_type got_offset = got->current_data_size();
4429 // Every PLT entry needs a GOT entry which points back to the PLT
4430 // entry (this will be changed by the dynamic linker, normally
4431 // lazily when the function is called).
4432 got->set_current_data_size(got_offset + size / 8);
4434 // Every PLT entry needs a reloc.
4435 this->add_relocation(symtab, layout, gsym, got_offset);
4437 // Note that we don't need to save the symbol. The contents of the
4438 // PLT are independent of which symbols are used. The symbols only
4439 // appear in the relocations.
4442 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
4445 template<int size, bool big_endian>
4447 Output_data_plt_aarch64<size, big_endian>::add_local_ifunc_entry(
4448 Symbol_table* symtab,
4450 Sized_relobj_file<size, big_endian>* relobj,
4451 unsigned int local_sym_index)
4453 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
4454 ++this->irelative_count_;
4456 section_offset_type got_offset = this->got_irelative_->current_data_size();
4458 // Every PLT entry needs a GOT entry which points back to the PLT
4460 this->got_irelative_->set_current_data_size(got_offset + size / 8);
4462 // Every PLT entry needs a reloc.
4463 Reloc_section* rela = this->rela_irelative(symtab, layout);
4464 rela->add_symbolless_local_addend(relobj, local_sym_index,
4465 elfcpp::R_AARCH64_IRELATIVE,
4466 this->got_irelative_, got_offset, 0);
4471 // Add the relocation for a PLT entry.
4473 template<int size, bool big_endian>
4475 Output_data_plt_aarch64<size, big_endian>::add_relocation(
4476 Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
4478 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4479 && gsym->can_use_relative_reloc(false))
4481 Reloc_section* rela = this->rela_irelative(symtab, layout);
4482 rela->add_symbolless_global_addend(gsym, elfcpp::R_AARCH64_IRELATIVE,
4483 this->got_irelative_, got_offset, 0);
4487 gsym->set_needs_dynsym_entry();
4488 this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT, this->got_plt_,
4493 // Return where the TLSDESC relocations should go, creating it if
4494 // necessary. These follow the JUMP_SLOT relocations.
4496 template<int size, bool big_endian>
4497 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
4498 Output_data_plt_aarch64<size, big_endian>::rela_tlsdesc(Layout* layout)
4500 if (this->tlsdesc_rel_ == NULL)
4502 this->tlsdesc_rel_ = new Reloc_section(false);
4503 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4504 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
4505 ORDER_DYNAMIC_PLT_RELOCS, false);
4506 gold_assert(this->tlsdesc_rel_->output_section()
4507 == this->rel_->output_section());
4509 return this->tlsdesc_rel_;
4512 // Return where the IRELATIVE relocations should go in the PLT. These
4513 // follow the JUMP_SLOT and the TLSDESC relocations.
4515 template<int size, bool big_endian>
4516 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
4517 Output_data_plt_aarch64<size, big_endian>::rela_irelative(Symbol_table* symtab,
4520 if (this->irelative_rel_ == NULL)
4522 // Make sure we have a place for the TLSDESC relocations, in
4523 // case we see any later on.
4524 this->rela_tlsdesc(layout);
4525 this->irelative_rel_ = new Reloc_section(false);
4526 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4527 elfcpp::SHF_ALLOC, this->irelative_rel_,
4528 ORDER_DYNAMIC_PLT_RELOCS, false);
4529 gold_assert(this->irelative_rel_->output_section()
4530 == this->rel_->output_section());
4532 if (parameters->doing_static_link())
4534 // A statically linked executable will only have a .rela.plt
4535 // section to hold R_AARCH64_IRELATIVE relocs for
4536 // STT_GNU_IFUNC symbols. The library will use these
4537 // symbols to locate the IRELATIVE relocs at program startup
4539 symtab->define_in_output_data("__rela_iplt_start", NULL,
4540 Symbol_table::PREDEFINED,
4541 this->irelative_rel_, 0, 0,
4542 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4543 elfcpp::STV_HIDDEN, 0, false, true);
4544 symtab->define_in_output_data("__rela_iplt_end", NULL,
4545 Symbol_table::PREDEFINED,
4546 this->irelative_rel_, 0, 0,
4547 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4548 elfcpp::STV_HIDDEN, 0, true, true);
4551 return this->irelative_rel_;
4554 // Return the PLT address to use for a global symbol.
4556 template<int size, bool big_endian>
4558 Output_data_plt_aarch64<size, big_endian>::address_for_global(
4561 uint64_t offset = 0;
4562 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4563 && gsym->can_use_relative_reloc(false))
4564 offset = (this->first_plt_entry_offset() +
4565 this->count_ * this->get_plt_entry_size());
4566 return this->address() + offset + gsym->plt_offset();
4569 // Return the PLT address to use for a local symbol. These are always
4570 // IRELATIVE relocs.
4572 template<int size, bool big_endian>
4574 Output_data_plt_aarch64<size, big_endian>::address_for_local(
4575 const Relobj* object,
4578 return (this->address()
4579 + this->first_plt_entry_offset()
4580 + this->count_ * this->get_plt_entry_size()
4581 + object->local_plt_offset(r_sym));
4584 // Set the final size.
4586 template<int size, bool big_endian>
4588 Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
4590 unsigned int count = this->count_ + this->irelative_count_;
4591 unsigned int extra_size = 0;
4592 if (this->has_tlsdesc_entry())
4593 extra_size += this->get_plt_tlsdesc_entry_size();
4594 this->set_data_size(this->first_plt_entry_offset()
4595 + count * this->get_plt_entry_size()
4599 template<int size, bool big_endian>
4600 class Output_data_plt_aarch64_standard :
4601 public Output_data_plt_aarch64<size, big_endian>
4604 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4605 Output_data_plt_aarch64_standard(
4607 Output_data_got_aarch64<size, big_endian>* got,
4608 Output_data_space* got_plt,
4609 Output_data_space* got_irelative)
4610 : Output_data_plt_aarch64<size, big_endian>(layout,
4617 // Return the offset of the first non-reserved PLT entry.
4618 virtual unsigned int
4619 do_first_plt_entry_offset() const
4620 { return this->first_plt_entry_size; }
4622 // Return the size of a PLT entry
4623 virtual unsigned int
4624 do_get_plt_entry_size() const
4625 { return this->plt_entry_size; }
4627 // Return the size of a tlsdesc entry
4628 virtual unsigned int
4629 do_get_plt_tlsdesc_entry_size() const
4630 { return this->plt_tlsdesc_entry_size; }
4633 do_fill_first_plt_entry(unsigned char* pov,
4634 Address got_address,
4635 Address plt_address);
4638 do_fill_plt_entry(unsigned char* pov,
4639 Address got_address,
4640 Address plt_address,
4641 unsigned int got_offset,
4642 unsigned int plt_offset);
4645 do_fill_tlsdesc_entry(unsigned char* pov,
4646 Address gotplt_address,
4647 Address plt_address,
4649 unsigned int tlsdesc_got_offset,
4650 unsigned int plt_offset);
4653 // The size of the first plt entry size.
4654 static const int first_plt_entry_size = 32;
4655 // The size of the plt entry size.
4656 static const int plt_entry_size = 16;
4657 // The size of the plt tlsdesc entry size.
4658 static const int plt_tlsdesc_entry_size = 32;
4659 // Template for the first PLT entry.
4660 static const uint32_t first_plt_entry[first_plt_entry_size / 4];
4661 // Template for subsequent PLT entries.
4662 static const uint32_t plt_entry[plt_entry_size / 4];
4663 // The reserved TLSDESC entry in the PLT for an executable.
4664 static const uint32_t tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4];
4667 // The first entry in the PLT for an executable.
4671 Output_data_plt_aarch64_standard<32, false>::
4672 first_plt_entry[first_plt_entry_size / 4] =
4674 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4675 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4676 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4677 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4678 0xd61f0220, /* br x17 */
4679 0xd503201f, /* nop */
4680 0xd503201f, /* nop */
4681 0xd503201f, /* nop */
4687 Output_data_plt_aarch64_standard<32, true>::
4688 first_plt_entry[first_plt_entry_size / 4] =
4690 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4691 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4692 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4693 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4694 0xd61f0220, /* br x17 */
4695 0xd503201f, /* nop */
4696 0xd503201f, /* nop */
4697 0xd503201f, /* nop */
4703 Output_data_plt_aarch64_standard<64, false>::
4704 first_plt_entry[first_plt_entry_size / 4] =
4706 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4707 0x90000010, /* adrp x16, PLT_GOT+16 */
4708 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4709 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4710 0xd61f0220, /* br x17 */
4711 0xd503201f, /* nop */
4712 0xd503201f, /* nop */
4713 0xd503201f, /* nop */
4719 Output_data_plt_aarch64_standard<64, true>::
4720 first_plt_entry[first_plt_entry_size / 4] =
4722 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4723 0x90000010, /* adrp x16, PLT_GOT+16 */
4724 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4725 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4726 0xd61f0220, /* br x17 */
4727 0xd503201f, /* nop */
4728 0xd503201f, /* nop */
4729 0xd503201f, /* nop */
4735 Output_data_plt_aarch64_standard<32, false>::
4736 plt_entry[plt_entry_size / 4] =
4738 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4739 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4740 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4741 0xd61f0220, /* br x17. */
4747 Output_data_plt_aarch64_standard<32, true>::
4748 plt_entry[plt_entry_size / 4] =
4750 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4751 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4752 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4753 0xd61f0220, /* br x17. */
4759 Output_data_plt_aarch64_standard<64, false>::
4760 plt_entry[plt_entry_size / 4] =
4762 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4763 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4764 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4765 0xd61f0220, /* br x17. */
4771 Output_data_plt_aarch64_standard<64, true>::
4772 plt_entry[plt_entry_size / 4] =
4774 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4775 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4776 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4777 0xd61f0220, /* br x17. */
4781 template<int size, bool big_endian>
4783 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
4785 Address got_address,
4786 Address plt_address)
4788 // PLT0 of the small PLT looks like this in ELF64 -
4789 // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
4790 // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
4791 // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
4793 // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
4794 // GOTPLT entry for this.
4796 // PLT0 will be slightly different in ELF32 due to different got entry
4798 memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
4799 Address gotplt_2nd_ent = got_address + (size / 8) * 2;
4801 // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
4802 // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
4803 // FIXME: This only works for 64bit
4804 AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
4805 gotplt_2nd_ent, plt_address + 4);
4807 // Fill in R_AARCH64_LDST8_LO12
4808 elfcpp::Swap<32, big_endian>::writeval(
4810 ((this->first_plt_entry[2] & 0xffc003ff)
4811 | ((gotplt_2nd_ent & 0xff8) << 7)));
4813 // Fill in R_AARCH64_ADD_ABS_LO12
4814 elfcpp::Swap<32, big_endian>::writeval(
4816 ((this->first_plt_entry[3] & 0xffc003ff)
4817 | ((gotplt_2nd_ent & 0xfff) << 10)));
4821 // Subsequent entries in the PLT for an executable.
4822 // FIXME: This only works for 64bit
4824 template<int size, bool big_endian>
4826 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
4828 Address got_address,
4829 Address plt_address,
4830 unsigned int got_offset,
4831 unsigned int plt_offset)
4833 memcpy(pov, this->plt_entry, this->plt_entry_size);
4835 Address gotplt_entry_address = got_address + got_offset;
4836 Address plt_entry_address = plt_address + plt_offset;
4838 // Fill in R_AARCH64_PCREL_ADR_HI21
4839 AArch64_relocate_functions<size, big_endian>::adrp(
4841 gotplt_entry_address,
4844 // Fill in R_AARCH64_LDST64_ABS_LO12
4845 elfcpp::Swap<32, big_endian>::writeval(
4847 ((this->plt_entry[1] & 0xffc003ff)
4848 | ((gotplt_entry_address & 0xff8) << 7)));
4850 // Fill in R_AARCH64_ADD_ABS_LO12
4851 elfcpp::Swap<32, big_endian>::writeval(
4853 ((this->plt_entry[2] & 0xffc003ff)
4854 | ((gotplt_entry_address & 0xfff) <<10)));
4861 Output_data_plt_aarch64_standard<32, false>::
4862 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4864 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4865 0x90000002, /* adrp x2, 0 */
4866 0x90000003, /* adrp x3, 0 */
4867 0xb9400042, /* ldr w2, [w2, #0] */
4868 0x11000063, /* add w3, w3, 0 */
4869 0xd61f0040, /* br x2 */
4870 0xd503201f, /* nop */
4871 0xd503201f, /* nop */
4876 Output_data_plt_aarch64_standard<32, true>::
4877 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4879 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4880 0x90000002, /* adrp x2, 0 */
4881 0x90000003, /* adrp x3, 0 */
4882 0xb9400042, /* ldr w2, [w2, #0] */
4883 0x11000063, /* add w3, w3, 0 */
4884 0xd61f0040, /* br x2 */
4885 0xd503201f, /* nop */
4886 0xd503201f, /* nop */
4891 Output_data_plt_aarch64_standard<64, false>::
4892 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4894 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4895 0x90000002, /* adrp x2, 0 */
4896 0x90000003, /* adrp x3, 0 */
4897 0xf9400042, /* ldr x2, [x2, #0] */
4898 0x91000063, /* add x3, x3, 0 */
4899 0xd61f0040, /* br x2 */
4900 0xd503201f, /* nop */
4901 0xd503201f, /* nop */
4906 Output_data_plt_aarch64_standard<64, true>::
4907 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4909 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4910 0x90000002, /* adrp x2, 0 */
4911 0x90000003, /* adrp x3, 0 */
4912 0xf9400042, /* ldr x2, [x2, #0] */
4913 0x91000063, /* add x3, x3, 0 */
4914 0xd61f0040, /* br x2 */
4915 0xd503201f, /* nop */
4916 0xd503201f, /* nop */
4919 template<int size, bool big_endian>
4921 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_tlsdesc_entry(
4923 Address gotplt_address,
4924 Address plt_address,
4926 unsigned int tlsdesc_got_offset,
4927 unsigned int plt_offset)
4929 memcpy(pov, tlsdesc_plt_entry, plt_tlsdesc_entry_size);
4931 // move DT_TLSDESC_GOT address into x2
4932 // move .got.plt address into x3
4933 Address tlsdesc_got_entry = got_base + tlsdesc_got_offset;
4934 Address plt_entry_address = plt_address + plt_offset;
4936 // R_AARCH64_ADR_PREL_PG_HI21
4937 AArch64_relocate_functions<size, big_endian>::adrp(
4940 plt_entry_address + 4);
4942 // R_AARCH64_ADR_PREL_PG_HI21
4943 AArch64_relocate_functions<size, big_endian>::adrp(
4946 plt_entry_address + 8);
4948 // R_AARCH64_LDST64_ABS_LO12
4949 elfcpp::Swap<32, big_endian>::writeval(
4951 ((this->tlsdesc_plt_entry[3] & 0xffc003ff)
4952 | ((tlsdesc_got_entry & 0xff8) << 7)));
4954 // R_AARCH64_ADD_ABS_LO12
4955 elfcpp::Swap<32, big_endian>::writeval(
4957 ((this->tlsdesc_plt_entry[4] & 0xffc003ff)
4958 | ((gotplt_address & 0xfff) << 10)));
4961 // Write out the PLT. This uses the hand-coded instructions above,
4962 // and adjusts them as needed. This is specified by the AMD64 ABI.
4964 template<int size, bool big_endian>
4966 Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
4968 const off_t offset = this->offset();
4969 const section_size_type oview_size =
4970 convert_to_section_size_type(this->data_size());
4971 unsigned char* const oview = of->get_output_view(offset, oview_size);
4973 const off_t got_file_offset = this->got_plt_->offset();
4974 gold_assert(got_file_offset + this->got_plt_->data_size()
4975 == this->got_irelative_->offset());
4977 const section_size_type got_size =
4978 convert_to_section_size_type(this->got_plt_->data_size()
4979 + this->got_irelative_->data_size());
4980 unsigned char* const got_view = of->get_output_view(got_file_offset,
4983 unsigned char* pov = oview;
4985 // The base address of the .plt section.
4986 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
4987 // The base address of the PLT portion of the .got section.
4988 typename elfcpp::Elf_types<size>::Elf_Addr gotplt_address
4989 = this->got_plt_->address();
4991 this->fill_first_plt_entry(pov, gotplt_address, plt_address);
4992 pov += this->first_plt_entry_offset();
4994 // The first three entries in .got.plt are reserved.
4995 unsigned char* got_pov = got_view;
4996 memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
4997 got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
4999 unsigned int plt_offset = this->first_plt_entry_offset();
5000 unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
5001 const unsigned int count = this->count_ + this->irelative_count_;
5002 for (unsigned int plt_index = 0;
5005 pov += this->get_plt_entry_size(),
5006 got_pov += size / 8,
5007 plt_offset += this->get_plt_entry_size(),
5008 got_offset += size / 8)
5010 // Set and adjust the PLT entry itself.
5011 this->fill_plt_entry(pov, gotplt_address, plt_address,
5012 got_offset, plt_offset);
5014 // Set the entry in the GOT, which points to plt0.
5015 elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
5018 if (this->has_tlsdesc_entry())
5020 // Set and adjust the reserved TLSDESC PLT entry.
5021 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
5022 // The base address of the .base section.
5023 typename elfcpp::Elf_types<size>::Elf_Addr got_base =
5024 this->got_->address();
5025 this->fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
5026 tlsdesc_got_offset, plt_offset);
5027 pov += this->get_plt_tlsdesc_entry_size();
5030 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
5031 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
5033 of->write_output_view(offset, oview_size, oview);
5034 of->write_output_view(got_file_offset, got_size, got_view);
5037 // Telling how to update the immediate field of an instruction.
5038 struct AArch64_howto
5040 // The immediate field mask.
5041 elfcpp::Elf_Xword dst_mask;
5043 // The offset to apply relocation immediate
5046 // The second part offset, if the immediate field has two parts.
5047 // -1 if the immediate field has only one part.
5051 static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
5053 {0, -1, -1}, // DATA
5054 {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
5055 {0xffffe0, 5, -1}, // LD [23:5]-imm19
5056 {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
5057 {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
5058 {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
5059 {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
5060 {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
5061 {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
5062 {0x3ffffff, 0, -1}, // B [25:0]-imm26
5063 {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
5066 // AArch64 relocate function class
5068 template<int size, bool big_endian>
5069 class AArch64_relocate_functions
5074 STATUS_OKAY, // No error during relocation.
5075 STATUS_OVERFLOW, // Relocation overflow.
5076 STATUS_BAD_RELOC, // Relocation cannot be applied.
5079 typedef AArch64_relocate_functions<size, big_endian> This;
5080 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5081 typedef Relocate_info<size, big_endian> The_relocate_info;
5082 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
5083 typedef Reloc_stub<size, big_endian> The_reloc_stub;
5084 typedef Stub_table<size, big_endian> The_stub_table;
5085 typedef elfcpp::Rela<size, big_endian> The_rela;
5086 typedef typename elfcpp::Swap<size, big_endian>::Valtype AArch64_valtype;
5088 // Return the page address of the address.
5089 // Page(address) = address & ~0xFFF
5091 static inline AArch64_valtype
5092 Page(Address address)
5094 return (address & (~static_cast<Address>(0xFFF)));
5098 // Update instruction (pointed by view) with selected bits (immed).
5099 // val = (val & ~dst_mask) | (immed << doffset)
5101 template<int valsize>
5103 update_view(unsigned char* view,
5104 AArch64_valtype immed,
5105 elfcpp::Elf_Xword doffset,
5106 elfcpp::Elf_Xword dst_mask)
5108 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
5109 Valtype* wv = reinterpret_cast<Valtype*>(view);
5110 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
5112 // Clear immediate fields.
5114 elfcpp::Swap<valsize, big_endian>::writeval(wv,
5115 static_cast<Valtype>(val | (immed << doffset)));
5118 // Update two parts of an instruction (pointed by view) with selected
5119 // bits (immed1 and immed2).
5120 // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
5122 template<int valsize>
5124 update_view_two_parts(
5125 unsigned char* view,
5126 AArch64_valtype immed1,
5127 AArch64_valtype immed2,
5128 elfcpp::Elf_Xword doffset1,
5129 elfcpp::Elf_Xword doffset2,
5130 elfcpp::Elf_Xword dst_mask)
5132 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
5133 Valtype* wv = reinterpret_cast<Valtype*>(view);
5134 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
5136 elfcpp::Swap<valsize, big_endian>::writeval(wv,
5137 static_cast<Valtype>(val | (immed1 << doffset1) |
5138 (immed2 << doffset2)));
5141 // Update adr or adrp instruction with immed.
5142 // In adr and adrp: [30:29] immlo [23:5] immhi
5145 update_adr(unsigned char* view, AArch64_valtype immed)
5147 elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
5148 This::template update_view_two_parts<32>(
5151 (immed & 0x1ffffc) >> 2,
5157 // Update movz/movn instruction with bits immed.
5158 // Set instruction to movz if is_movz is true, otherwise set instruction
5162 update_movnz(unsigned char* view,
5163 AArch64_valtype immed,
5166 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5167 Valtype* wv = reinterpret_cast<Valtype*>(view);
5168 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
5170 const elfcpp::Elf_Xword doffset =
5171 aarch64_howto[AArch64_reloc_property::INST_MOVW].doffset;
5172 const elfcpp::Elf_Xword dst_mask =
5173 aarch64_howto[AArch64_reloc_property::INST_MOVW].dst_mask;
5175 // Clear immediate fields and opc code.
5176 val &= ~(dst_mask | (0x3 << 29));
5178 // Set instruction to movz or movn.
5179 // movz: [30:29] is 10 movn: [30:29] is 00
5183 elfcpp::Swap<32, big_endian>::writeval(wv,
5184 static_cast<Valtype>(val | (immed << doffset)));
5189 // Update selected bits in text.
5191 template<int valsize>
5192 static inline typename This::Status
5193 reloc_common(unsigned char* view, Address x,
5194 const AArch64_reloc_property* reloc_property)
5196 // Select bits from X.
5197 Address immed = reloc_property->select_x_value(x);
5200 const AArch64_reloc_property::Reloc_inst inst =
5201 reloc_property->reloc_inst();
5202 // If it is a data relocation or instruction has 2 parts of immediate
5203 // fields, you should not call pcrela_general.
5204 gold_assert(aarch64_howto[inst].doffset2 == -1 &&
5205 aarch64_howto[inst].doffset != -1);
5206 This::template update_view<valsize>(view, immed,
5207 aarch64_howto[inst].doffset,
5208 aarch64_howto[inst].dst_mask);
5210 // Do check overflow or alignment if needed.
5211 return (reloc_property->checkup_x_value(x)
5213 : This::STATUS_OVERFLOW);
5216 // Construct a B insn. Note, although we group it here with other relocation
5217 // operation, there is actually no 'relocation' involved here.
5219 construct_b(unsigned char* view, unsigned int branch_offset)
5221 update_view_two_parts<32>(view, 0x05, (branch_offset >> 2),
5225 // Do a simple rela relocation at unaligned addresses.
5227 template<int valsize>
5228 static inline typename This::Status
5229 rela_ua(unsigned char* view,
5230 const Sized_relobj_file<size, big_endian>* object,
5231 const Symbol_value<size>* psymval,
5232 AArch64_valtype addend,
5233 const AArch64_reloc_property* reloc_property)
5235 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
5237 typename elfcpp::Elf_types<size>::Elf_Addr x =
5238 psymval->value(object, addend);
5239 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
5240 static_cast<Valtype>(x));
5241 return (reloc_property->checkup_x_value(x)
5243 : This::STATUS_OVERFLOW);
5246 // Do a simple pc-relative relocation at unaligned addresses.
5248 template<int valsize>
5249 static inline typename This::Status
5250 pcrela_ua(unsigned char* view,
5251 const Sized_relobj_file<size, big_endian>* object,
5252 const Symbol_value<size>* psymval,
5253 AArch64_valtype addend,
5255 const AArch64_reloc_property* reloc_property)
5257 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
5259 Address x = psymval->value(object, addend) - address;
5260 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
5261 static_cast<Valtype>(x));
5262 return (reloc_property->checkup_x_value(x)
5264 : This::STATUS_OVERFLOW);
5267 // Do a simple rela relocation at aligned addresses.
5269 template<int valsize>
5270 static inline typename This::Status
5272 unsigned char* view,
5273 const Sized_relobj_file<size, big_endian>* object,
5274 const Symbol_value<size>* psymval,
5275 AArch64_valtype addend,
5276 const AArch64_reloc_property* reloc_property)
5278 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
5279 Valtype* wv = reinterpret_cast<Valtype*>(view);
5280 Address x = psymval->value(object, addend);
5281 elfcpp::Swap<valsize, big_endian>::writeval(wv,static_cast<Valtype>(x));
5282 return (reloc_property->checkup_x_value(x)
5284 : This::STATUS_OVERFLOW);
5287 // Do relocate. Update selected bits in text.
5288 // new_val = (val & ~dst_mask) | (immed << doffset)
5290 template<int valsize>
5291 static inline typename This::Status
5292 rela_general(unsigned char* view,
5293 const Sized_relobj_file<size, big_endian>* object,
5294 const Symbol_value<size>* psymval,
5295 AArch64_valtype addend,
5296 const AArch64_reloc_property* reloc_property)
5298 // Calculate relocation.
5299 Address x = psymval->value(object, addend);
5300 return This::template reloc_common<valsize>(view, x, reloc_property);
5303 // Do relocate. Update selected bits in text.
5304 // new val = (val & ~dst_mask) | (immed << doffset)
5306 template<int valsize>
5307 static inline typename This::Status
5309 unsigned char* view,
5311 AArch64_valtype addend,
5312 const AArch64_reloc_property* reloc_property)
5314 // Calculate relocation.
5315 Address x = s + addend;
5316 return This::template reloc_common<valsize>(view, x, reloc_property);
5319 // Do address relative relocate. Update selected bits in text.
5320 // new val = (val & ~dst_mask) | (immed << doffset)
5322 template<int valsize>
5323 static inline typename This::Status
5325 unsigned char* view,
5326 const Sized_relobj_file<size, big_endian>* object,
5327 const Symbol_value<size>* psymval,
5328 AArch64_valtype addend,
5330 const AArch64_reloc_property* reloc_property)
5332 // Calculate relocation.
5333 Address x = psymval->value(object, addend) - address;
5334 return This::template reloc_common<valsize>(view, x, reloc_property);
5338 // Calculate (S + A) - address, update adr instruction.
5340 static inline typename This::Status
5341 adr(unsigned char* view,
5342 const Sized_relobj_file<size, big_endian>* object,
5343 const Symbol_value<size>* psymval,
5346 const AArch64_reloc_property* /* reloc_property */)
5348 AArch64_valtype x = psymval->value(object, addend) - address;
5349 // Pick bits [20:0] of X.
5350 AArch64_valtype immed = x & 0x1fffff;
5351 update_adr(view, immed);
5352 // Check -2^20 <= X < 2^20
5353 return (size == 64 && Bits<21>::has_overflow((x))
5354 ? This::STATUS_OVERFLOW
5355 : This::STATUS_OKAY);
5358 // Calculate PG(S+A) - PG(address), update adrp instruction.
5359 // R_AARCH64_ADR_PREL_PG_HI21
5361 static inline typename This::Status
5363 unsigned char* view,
5367 AArch64_valtype x = This::Page(sa) - This::Page(address);
5368 // Pick [32:12] of X.
5369 AArch64_valtype immed = (x >> 12) & 0x1fffff;
5370 update_adr(view, immed);
5371 // Check -2^32 <= X < 2^32
5372 return (size == 64 && Bits<33>::has_overflow((x))
5373 ? This::STATUS_OVERFLOW
5374 : This::STATUS_OKAY);
5377 // Calculate PG(S+A) - PG(address), update adrp instruction.
5378 // R_AARCH64_ADR_PREL_PG_HI21
5380 static inline typename This::Status
5381 adrp(unsigned char* view,
5382 const Sized_relobj_file<size, big_endian>* object,
5383 const Symbol_value<size>* psymval,
5386 const AArch64_reloc_property* reloc_property)
5388 Address sa = psymval->value(object, addend);
5389 AArch64_valtype x = This::Page(sa) - This::Page(address);
5390 // Pick [32:12] of X.
5391 AArch64_valtype immed = (x >> 12) & 0x1fffff;
5392 update_adr(view, immed);
5393 return (reloc_property->checkup_x_value(x)
5395 : This::STATUS_OVERFLOW);
5398 // Update mov[n/z] instruction. Check overflow if needed.
5399 // If X >=0, set the instruction to movz and its immediate value to the
5401 // If X < 0, set the instruction to movn and its immediate value to
5402 // NOT (selected bits of).
5404 static inline typename This::Status
5405 movnz(unsigned char* view,
5407 const AArch64_reloc_property* reloc_property)
5409 // Select bits from X.
5412 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedW;
5413 if (static_cast<SignedW>(x) >= 0)
5415 immed = reloc_property->select_x_value(x);
5420 immed = reloc_property->select_x_value(~x);;
5424 // Update movnz instruction.
5425 update_movnz(view, immed, is_movz);
5427 // Do check overflow or alignment if needed.
5428 return (reloc_property->checkup_x_value(x)
5430 : This::STATUS_OVERFLOW);
5434 maybe_apply_stub(unsigned int,
5435 const The_relocate_info*,
5439 const Sized_symbol<size>*,
5440 const Symbol_value<size>*,
5441 const Sized_relobj_file<size, big_endian>*,
5444 }; // End of AArch64_relocate_functions
5447 // For a certain relocation type (usually jump/branch), test to see if the
5448 // destination needs a stub to fulfil. If so, re-route the destination of the
5449 // original instruction to the stub, note, at this time, the stub has already
5452 template<int size, bool big_endian>
5454 AArch64_relocate_functions<size, big_endian>::
5455 maybe_apply_stub(unsigned int r_type,
5456 const The_relocate_info* relinfo,
5457 const The_rela& rela,
5458 unsigned char* view,
5460 const Sized_symbol<size>* gsym,
5461 const Symbol_value<size>* psymval,
5462 const Sized_relobj_file<size, big_endian>* object,
5463 section_size_type current_group_size)
5465 if (parameters->options().relocatable())
5468 typename elfcpp::Elf_types<size>::Elf_Swxword addend = rela.get_r_addend();
5469 Address branch_target = psymval->value(object, 0) + addend;
5471 The_reloc_stub::stub_type_for_reloc(r_type, address, branch_target);
5472 if (stub_type == ST_NONE)
5475 const The_aarch64_relobj* aarch64_relobj =
5476 static_cast<const The_aarch64_relobj*>(object);
5477 const AArch64_reloc_property* arp =
5478 aarch64_reloc_property_table->get_reloc_property(r_type);
5479 gold_assert(arp != NULL);
5481 // We don't create stubs for undefined symbols, but do for weak.
5483 && !gsym->use_plt_offset(arp->reference_flags())
5484 && gsym->is_undefined())
5486 gold_debug(DEBUG_TARGET,
5487 "stub: looking for a stub for undefined symbol %s in file %s",
5488 gsym->name(), aarch64_relobj->name().c_str());
5492 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
5493 gold_assert(stub_table != NULL);
5495 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5496 typename The_reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
5497 The_reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
5498 gold_assert(stub != NULL);
5500 Address new_branch_target = stub_table->address() + stub->offset();
5501 typename elfcpp::Swap<size, big_endian>::Valtype branch_offset =
5502 new_branch_target - address;
5503 typename This::Status status = This::template
5504 rela_general<32>(view, branch_offset, 0, arp);
5505 if (status != This::STATUS_OKAY)
5506 gold_error(_("Stub is too far away, try a smaller value "
5507 "for '--stub-group-size'. The current value is 0x%lx."),
5508 static_cast<unsigned long>(current_group_size));
5513 // Group input sections for stub generation.
5515 // We group input sections in an output section so that the total size,
5516 // including any padding space due to alignment is smaller than GROUP_SIZE
5517 // unless the only input section in group is bigger than GROUP_SIZE already.
5518 // Then an ARM stub table is created to follow the last input section
5519 // in group. For each group an ARM stub table is created an is placed
5520 // after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
5521 // extend the group after the stub table.
5523 template<int size, bool big_endian>
5525 Target_aarch64<size, big_endian>::group_sections(
5527 section_size_type group_size,
5528 bool stubs_always_after_branch,
5531 // Group input sections and insert stub table
5532 Layout::Section_list section_list;
5533 layout->get_executable_sections(§ion_list);
5534 for (Layout::Section_list::const_iterator p = section_list.begin();
5535 p != section_list.end();
5538 AArch64_output_section<size, big_endian>* output_section =
5539 static_cast<AArch64_output_section<size, big_endian>*>(*p);
5540 output_section->group_sections(group_size, stubs_always_after_branch,
5546 // Find the AArch64_input_section object corresponding to the SHNDX-th input
5547 // section of RELOBJ.
5549 template<int size, bool big_endian>
5550 AArch64_input_section<size, big_endian>*
5551 Target_aarch64<size, big_endian>::find_aarch64_input_section(
5552 Relobj* relobj, unsigned int shndx) const
5554 Section_id sid(relobj, shndx);
5555 typename AArch64_input_section_map::const_iterator p =
5556 this->aarch64_input_section_map_.find(sid);
5557 return (p != this->aarch64_input_section_map_.end()) ? p->second : NULL;
5561 // Make a new AArch64_input_section object.
5563 template<int size, bool big_endian>
5564 AArch64_input_section<size, big_endian>*
5565 Target_aarch64<size, big_endian>::new_aarch64_input_section(
5566 Relobj* relobj, unsigned int shndx)
5568 Section_id sid(relobj, shndx);
5570 AArch64_input_section<size, big_endian>* input_section =
5571 new AArch64_input_section<size, big_endian>(relobj, shndx);
5572 input_section->init();
5574 // Register new AArch64_input_section in map for look-up.
5575 std::pair<typename AArch64_input_section_map::iterator,bool> ins =
5576 this->aarch64_input_section_map_.insert(
5577 std::make_pair(sid, input_section));
5579 // Make sure that it we have not created another AArch64_input_section
5580 // for this input section already.
5581 gold_assert(ins.second);
5583 return input_section;
5587 // Relaxation hook. This is where we do stub generation.
5589 template<int size, bool big_endian>
5591 Target_aarch64<size, big_endian>::do_relax(
5593 const Input_objects* input_objects,
5594 Symbol_table* symtab,
5598 gold_assert(!parameters->options().relocatable());
5601 // We don't handle negative stub_group_size right now.
5602 this->stub_group_size_ = abs(parameters->options().stub_group_size());
5603 if (this->stub_group_size_ == 1)
5605 // Leave room for 4096 4-byte stub entries. If we exceed that, then we
5606 // will fail to link. The user will have to relink with an explicit
5607 // group size option.
5608 this->stub_group_size_ = The_reloc_stub::MAX_BRANCH_OFFSET -
5611 group_sections(layout, this->stub_group_size_, true, task);
5615 // If this is not the first pass, addresses and file offsets have
5616 // been reset at this point, set them here.
5617 for (Stub_table_iterator sp = this->stub_tables_.begin();
5618 sp != this->stub_tables_.end(); ++sp)
5620 The_stub_table* stt = *sp;
5621 The_aarch64_input_section* owner = stt->owner();
5622 off_t off = align_address(owner->original_size(),
5624 stt->set_address_and_file_offset(owner->address() + off,
5625 owner->offset() + off);
5629 // Scan relocs for relocation stubs
5630 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
5631 op != input_objects->relobj_end();
5634 The_aarch64_relobj* aarch64_relobj =
5635 static_cast<The_aarch64_relobj*>(*op);
5636 // Lock the object so we can read from it. This is only called
5637 // single-threaded from Layout::finalize, so it is OK to lock.
5638 Task_lock_obj<Object> tl(task, aarch64_relobj);
5639 aarch64_relobj->scan_sections_for_stubs(this, symtab, layout);
5642 bool any_stub_table_changed = false;
5643 for (Stub_table_iterator siter = this->stub_tables_.begin();
5644 siter != this->stub_tables_.end() && !any_stub_table_changed; ++siter)
5646 The_stub_table* stub_table = *siter;
5647 if (stub_table->update_data_size_changed_p())
5649 The_aarch64_input_section* owner = stub_table->owner();
5650 uint64_t address = owner->address();
5651 off_t offset = owner->offset();
5652 owner->reset_address_and_file_offset();
5653 owner->set_address_and_file_offset(address, offset);
5655 any_stub_table_changed = true;
5659 // Do not continue relaxation.
5660 bool continue_relaxation = any_stub_table_changed;
5661 if (!continue_relaxation)
5662 for (Stub_table_iterator sp = this->stub_tables_.begin();
5663 (sp != this->stub_tables_.end());
5665 (*sp)->finalize_stubs();
5667 return continue_relaxation;
5671 // Make a new Stub_table.
5673 template<int size, bool big_endian>
5674 Stub_table<size, big_endian>*
5675 Target_aarch64<size, big_endian>::new_stub_table(
5676 AArch64_input_section<size, big_endian>* owner)
5678 Stub_table<size, big_endian>* stub_table =
5679 new Stub_table<size, big_endian>(owner);
5680 stub_table->set_address(align_address(
5681 owner->address() + owner->data_size(), 8));
5682 stub_table->set_file_offset(owner->offset() + owner->data_size());
5683 stub_table->finalize_data_size();
5685 this->stub_tables_.push_back(stub_table);
5691 template<int size, bool big_endian>
5693 Target_aarch64<size, big_endian>::do_reloc_addend(
5694 void* arg, unsigned int r_type, uint64_t) const
5696 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
5697 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5698 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5699 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5700 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5701 gold_assert(psymval->is_tls_symbol());
5702 // The value of a TLS symbol is the offset in the TLS segment.
5703 return psymval->value(ti.object, 0);
5706 // Return the number of entries in the PLT.
5708 template<int size, bool big_endian>
5710 Target_aarch64<size, big_endian>::plt_entry_count() const
5712 if (this->plt_ == NULL)
5714 return this->plt_->entry_count();
5717 // Return the offset of the first non-reserved PLT entry.
5719 template<int size, bool big_endian>
5721 Target_aarch64<size, big_endian>::first_plt_entry_offset() const
5723 return this->plt_->first_plt_entry_offset();
5726 // Return the size of each PLT entry.
5728 template<int size, bool big_endian>
5730 Target_aarch64<size, big_endian>::plt_entry_size() const
5732 return this->plt_->get_plt_entry_size();
5735 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
5737 template<int size, bool big_endian>
5739 Target_aarch64<size, big_endian>::define_tls_base_symbol(
5740 Symbol_table* symtab, Layout* layout)
5742 if (this->tls_base_symbol_defined_)
5745 Output_segment* tls_segment = layout->tls_segment();
5746 if (tls_segment != NULL)
5748 // _TLS_MODULE_BASE_ always points to the beginning of tls segment.
5749 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
5750 Symbol_table::PREDEFINED,
5754 elfcpp::STV_HIDDEN, 0,
5755 Symbol::SEGMENT_START,
5758 this->tls_base_symbol_defined_ = true;
5761 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
5763 template<int size, bool big_endian>
5765 Target_aarch64<size, big_endian>::reserve_tlsdesc_entries(
5766 Symbol_table* symtab, Layout* layout)
5768 if (this->plt_ == NULL)
5769 this->make_plt_section(symtab, layout);
5771 if (!this->plt_->has_tlsdesc_entry())
5773 // Allocate the TLSDESC_GOT entry.
5774 Output_data_got_aarch64<size, big_endian>* got =
5775 this->got_section(symtab, layout);
5776 unsigned int got_offset = got->add_constant(0);
5778 // Allocate the TLSDESC_PLT entry.
5779 this->plt_->reserve_tlsdesc_entry(got_offset);
5783 // Create a GOT entry for the TLS module index.
5785 template<int size, bool big_endian>
5787 Target_aarch64<size, big_endian>::got_mod_index_entry(
5788 Symbol_table* symtab, Layout* layout,
5789 Sized_relobj_file<size, big_endian>* object)
5791 if (this->got_mod_index_offset_ == -1U)
5793 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5794 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5795 Output_data_got_aarch64<size, big_endian>* got =
5796 this->got_section(symtab, layout);
5797 unsigned int got_offset = got->add_constant(0);
5798 rela_dyn->add_local(object, 0, elfcpp::R_AARCH64_TLS_DTPMOD64, got,
5800 got->add_constant(0);
5801 this->got_mod_index_offset_ = got_offset;
5803 return this->got_mod_index_offset_;
5806 // Optimize the TLS relocation type based on what we know about the
5807 // symbol. IS_FINAL is true if the final address of this symbol is
5808 // known at link time.
5810 template<int size, bool big_endian>
5811 tls::Tls_optimization
5812 Target_aarch64<size, big_endian>::optimize_tls_reloc(bool is_final,
5815 // If we are generating a shared library, then we can't do anything
5817 if (parameters->options().shared())
5818 return tls::TLSOPT_NONE;
5822 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5823 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5824 case elfcpp::R_AARCH64_TLSDESC_LD_PREL19:
5825 case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21:
5826 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5827 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5828 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5829 case elfcpp::R_AARCH64_TLSDESC_OFF_G1:
5830 case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC:
5831 case elfcpp::R_AARCH64_TLSDESC_LDR:
5832 case elfcpp::R_AARCH64_TLSDESC_ADD:
5833 case elfcpp::R_AARCH64_TLSDESC_CALL:
5834 // These are General-Dynamic which permits fully general TLS
5835 // access. Since we know that we are generating an executable,
5836 // we can convert this to Initial-Exec. If we also know that
5837 // this is a local symbol, we can further switch to Local-Exec.
5839 return tls::TLSOPT_TO_LE;
5840 return tls::TLSOPT_TO_IE;
5842 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5843 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5844 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5845 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5846 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5847 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5848 // These are Local-Dynamic, which refer to local symbols in the
5849 // dynamic TLS block. Since we know that we generating an
5850 // executable, we can switch to Local-Exec.
5851 return tls::TLSOPT_TO_LE;
5853 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
5854 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
5855 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5856 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5857 case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5858 // These are Initial-Exec relocs which get the thread offset
5859 // from the GOT. If we know that we are linking against the
5860 // local symbol, we can switch to Local-Exec, which links the
5861 // thread offset into the instruction.
5863 return tls::TLSOPT_TO_LE;
5864 return tls::TLSOPT_NONE;
5866 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5867 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5868 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5869 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5870 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5871 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5872 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5873 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5874 // When we already have Local-Exec, there is nothing further we
5876 return tls::TLSOPT_NONE;
5883 // Returns true if this relocation type could be that of a function pointer.
5885 template<int size, bool big_endian>
5887 Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
5888 unsigned int r_type)
5892 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
5893 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
5894 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
5895 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5896 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5904 // For safe ICF, scan a relocation for a local symbol to check if it
5905 // corresponds to a function pointer being taken. In that case mark
5906 // the function whose pointer was taken as not foldable.
5908 template<int size, bool big_endian>
5910 Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
5913 Target_aarch64<size, big_endian>* ,
5914 Sized_relobj_file<size, big_endian>* ,
5917 const elfcpp::Rela<size, big_endian>& ,
5918 unsigned int r_type,
5919 const elfcpp::Sym<size, big_endian>&)
5921 // When building a shared library, do not fold any local symbols.
5922 return (parameters->options().shared()
5923 || possible_function_pointer_reloc(r_type));
5926 // For safe ICF, scan a relocation for a global symbol to check if it
5927 // corresponds to a function pointer being taken. In that case mark
5928 // the function whose pointer was taken as not foldable.
5930 template<int size, bool big_endian>
5932 Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
5935 Target_aarch64<size, big_endian>* ,
5936 Sized_relobj_file<size, big_endian>* ,
5939 const elfcpp::Rela<size, big_endian>& ,
5940 unsigned int r_type,
5943 // When building a shared library, do not fold symbols whose visibility
5944 // is hidden, internal or protected.
5945 return ((parameters->options().shared()
5946 && (gsym->visibility() == elfcpp::STV_INTERNAL
5947 || gsym->visibility() == elfcpp::STV_PROTECTED
5948 || gsym->visibility() == elfcpp::STV_HIDDEN))
5949 || possible_function_pointer_reloc(r_type));
5952 // Report an unsupported relocation against a local symbol.
5954 template<int size, bool big_endian>
5956 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
5957 Sized_relobj_file<size, big_endian>* object,
5958 unsigned int r_type)
5960 gold_error(_("%s: unsupported reloc %u against local symbol"),
5961 object->name().c_str(), r_type);
5964 // We are about to emit a dynamic relocation of type R_TYPE. If the
5965 // dynamic linker does not support it, issue an error.
5967 template<int size, bool big_endian>
5969 Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
5970 unsigned int r_type)
5972 gold_assert(r_type != elfcpp::R_AARCH64_NONE);
5976 // These are the relocation types supported by glibc for AARCH64.
5977 case elfcpp::R_AARCH64_NONE:
5978 case elfcpp::R_AARCH64_COPY:
5979 case elfcpp::R_AARCH64_GLOB_DAT:
5980 case elfcpp::R_AARCH64_JUMP_SLOT:
5981 case elfcpp::R_AARCH64_RELATIVE:
5982 case elfcpp::R_AARCH64_TLS_DTPREL64:
5983 case elfcpp::R_AARCH64_TLS_DTPMOD64:
5984 case elfcpp::R_AARCH64_TLS_TPREL64:
5985 case elfcpp::R_AARCH64_TLSDESC:
5986 case elfcpp::R_AARCH64_IRELATIVE:
5987 case elfcpp::R_AARCH64_ABS32:
5988 case elfcpp::R_AARCH64_ABS64:
5995 // This prevents us from issuing more than one error per reloc
5996 // section. But we can still wind up issuing more than one
5997 // error per object file.
5998 if (this->issued_non_pic_error_)
6000 gold_assert(parameters->options().output_is_position_independent());
6001 object->error(_("requires unsupported dynamic reloc; "
6002 "recompile with -fPIC"));
6003 this->issued_non_pic_error_ = true;
6007 // Return whether we need to make a PLT entry for a relocation of the
6008 // given type against a STT_GNU_IFUNC symbol.
6010 template<int size, bool big_endian>
6012 Target_aarch64<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
6013 Sized_relobj_file<size, big_endian>* object,
6014 unsigned int r_type)
6016 const AArch64_reloc_property* arp =
6017 aarch64_reloc_property_table->get_reloc_property(r_type);
6018 gold_assert(arp != NULL);
6020 int flags = arp->reference_flags();
6021 if (flags & Symbol::TLS_REF)
6023 gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
6024 object->name().c_str(), arp->name().c_str());
6030 // Scan a relocation for a local symbol.
6032 template<int size, bool big_endian>
6034 Target_aarch64<size, big_endian>::Scan::local(
6035 Symbol_table* symtab,
6037 Target_aarch64<size, big_endian>* target,
6038 Sized_relobj_file<size, big_endian>* object,
6039 unsigned int data_shndx,
6040 Output_section* output_section,
6041 const elfcpp::Rela<size, big_endian>& rela,
6042 unsigned int r_type,
6043 const elfcpp::Sym<size, big_endian>& lsym,
6049 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
6051 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6053 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6054 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
6055 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
6056 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
6060 case elfcpp::R_AARCH64_NONE:
6063 case elfcpp::R_AARCH64_ABS32:
6064 case elfcpp::R_AARCH64_ABS16:
6065 if (parameters->options().output_is_position_independent())
6067 gold_error(_("%s: unsupported reloc %u in pos independent link."),
6068 object->name().c_str(), r_type);
6072 case elfcpp::R_AARCH64_ABS64:
6073 // If building a shared library or pie, we need to mark this as a dynmic
6074 // reloction, so that the dynamic loader can relocate it.
6075 if (parameters->options().output_is_position_independent())
6077 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6078 rela_dyn->add_local_relative(object, r_sym,
6079 elfcpp::R_AARCH64_RELATIVE,
6082 rela.get_r_offset(),
6083 rela.get_r_addend(),
6088 case elfcpp::R_AARCH64_PREL64:
6089 case elfcpp::R_AARCH64_PREL32:
6090 case elfcpp::R_AARCH64_PREL16:
6093 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6094 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6095 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
6096 // The above relocations are used to access GOT entries.
6098 Output_data_got_aarch64<size, big_endian>* got =
6099 target->got_section(symtab, layout);
6100 bool is_new = false;
6101 // This symbol requires a GOT entry.
6103 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
6105 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
6106 if (is_new && parameters->options().output_is_position_independent())
6107 target->rela_dyn_section(layout)->
6108 add_local_relative(object,
6110 elfcpp::R_AARCH64_RELATIVE,
6112 object->local_got_offset(r_sym,
6119 case elfcpp::R_AARCH64_MOVW_UABS_G0: // 263
6120 case elfcpp::R_AARCH64_MOVW_UABS_G0_NC: // 264
6121 case elfcpp::R_AARCH64_MOVW_UABS_G1: // 265
6122 case elfcpp::R_AARCH64_MOVW_UABS_G1_NC: // 266
6123 case elfcpp::R_AARCH64_MOVW_UABS_G2: // 267
6124 case elfcpp::R_AARCH64_MOVW_UABS_G2_NC: // 268
6125 case elfcpp::R_AARCH64_MOVW_UABS_G3: // 269
6126 case elfcpp::R_AARCH64_MOVW_SABS_G0: // 270
6127 case elfcpp::R_AARCH64_MOVW_SABS_G1: // 271
6128 case elfcpp::R_AARCH64_MOVW_SABS_G2: // 272
6129 if (parameters->options().output_is_position_independent())
6131 gold_error(_("%s: unsupported reloc %u in pos independent link."),
6132 object->name().c_str(), r_type);
6136 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
6137 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
6138 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
6139 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
6140 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
6141 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
6142 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
6143 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
6144 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
6145 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
6148 // Control flow, pc-relative. We don't need to do anything for a relative
6149 // addressing relocation against a local symbol if it does not reference
6151 case elfcpp::R_AARCH64_TSTBR14:
6152 case elfcpp::R_AARCH64_CONDBR19:
6153 case elfcpp::R_AARCH64_JUMP26:
6154 case elfcpp::R_AARCH64_CALL26:
6157 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6158 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6160 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6161 optimize_tls_reloc(!parameters->options().shared(), r_type);
6162 if (tlsopt == tls::TLSOPT_TO_LE)
6165 layout->set_has_static_tls();
6166 // Create a GOT entry for the tp-relative offset.
6167 if (!parameters->doing_static_link())
6169 Output_data_got_aarch64<size, big_endian>* got =
6170 target->got_section(symtab, layout);
6171 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
6172 target->rela_dyn_section(layout),
6173 elfcpp::R_AARCH64_TLS_TPREL64);
6175 else if (!object->local_has_got_offset(r_sym,
6176 GOT_TYPE_TLS_OFFSET))
6178 Output_data_got_aarch64<size, big_endian>* got =
6179 target->got_section(symtab, layout);
6180 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
6181 unsigned int got_offset =
6182 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
6183 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6184 gold_assert(addend == 0);
6185 got->add_static_reloc(got_offset, elfcpp::R_AARCH64_TLS_TPREL64,
6191 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6192 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6194 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6195 optimize_tls_reloc(!parameters->options().shared(), r_type);
6196 if (tlsopt == tls::TLSOPT_TO_LE)
6198 layout->set_has_static_tls();
6201 gold_assert(tlsopt == tls::TLSOPT_NONE);
6203 Output_data_got_aarch64<size, big_endian>* got =
6204 target->got_section(symtab, layout);
6205 got->add_local_pair_with_rel(object,r_sym, data_shndx,
6207 target->rela_dyn_section(layout),
6208 elfcpp::R_AARCH64_TLS_DTPMOD64);
6212 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6213 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6214 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6215 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6216 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6217 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6218 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6219 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6221 layout->set_has_static_tls();
6222 bool output_is_shared = parameters->options().shared();
6223 if (output_is_shared)
6224 gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
6225 object->name().c_str(), r_type);
6229 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6230 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
6232 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6233 optimize_tls_reloc(!parameters->options().shared(), r_type);
6234 if (tlsopt == tls::TLSOPT_NONE)
6236 // Create a GOT entry for the module index.
6237 target->got_mod_index_entry(symtab, layout, object);
6239 else if (tlsopt != tls::TLSOPT_TO_LE)
6240 unsupported_reloc_local(object, r_type);
6244 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6245 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6246 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6247 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
6250 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6251 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6252 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6254 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6255 optimize_tls_reloc(!parameters->options().shared(), r_type);
6256 target->define_tls_base_symbol(symtab, layout);
6257 if (tlsopt == tls::TLSOPT_NONE)
6259 // Create reserved PLT and GOT entries for the resolver.
6260 target->reserve_tlsdesc_entries(symtab, layout);
6262 // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
6263 // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
6264 // entry needs to be in an area in .got.plt, not .got. Call
6265 // got_section to make sure the section has been created.
6266 target->got_section(symtab, layout);
6267 Output_data_got<size, big_endian>* got =
6268 target->got_tlsdesc_section();
6269 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6270 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
6272 unsigned int got_offset = got->add_constant(0);
6273 got->add_constant(0);
6274 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
6276 Reloc_section* rt = target->rela_tlsdesc_section(layout);
6277 // We store the arguments we need in a vector, and use
6278 // the index into the vector as the parameter to pass
6279 // to the target specific routines.
6280 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
6281 void* arg = reinterpret_cast<void*>(intarg);
6282 rt->add_target_specific(elfcpp::R_AARCH64_TLSDESC, arg,
6283 got, got_offset, 0);
6286 else if (tlsopt != tls::TLSOPT_TO_LE)
6287 unsupported_reloc_local(object, r_type);
6291 case elfcpp::R_AARCH64_TLSDESC_CALL:
6295 unsupported_reloc_local(object, r_type);
6300 // Report an unsupported relocation against a global symbol.
6302 template<int size, bool big_endian>
6304 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
6305 Sized_relobj_file<size, big_endian>* object,
6306 unsigned int r_type,
6309 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6310 object->name().c_str(), r_type, gsym->demangled_name().c_str());
6313 template<int size, bool big_endian>
6315 Target_aarch64<size, big_endian>::Scan::global(
6316 Symbol_table* symtab,
6318 Target_aarch64<size, big_endian>* target,
6319 Sized_relobj_file<size, big_endian> * object,
6320 unsigned int data_shndx,
6321 Output_section* output_section,
6322 const elfcpp::Rela<size, big_endian>& rela,
6323 unsigned int r_type,
6326 // A STT_GNU_IFUNC symbol may require a PLT entry.
6327 if (gsym->type() == elfcpp::STT_GNU_IFUNC
6328 && this->reloc_needs_plt_for_ifunc(object, r_type))
6329 target->make_plt_entry(symtab, layout, gsym);
6331 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
6333 const AArch64_reloc_property* arp =
6334 aarch64_reloc_property_table->get_reloc_property(r_type);
6335 gold_assert(arp != NULL);
6339 case elfcpp::R_AARCH64_NONE:
6342 case elfcpp::R_AARCH64_ABS16:
6343 case elfcpp::R_AARCH64_ABS32:
6344 case elfcpp::R_AARCH64_ABS64:
6346 // Make a PLT entry if necessary.
6347 if (gsym->needs_plt_entry())
6349 target->make_plt_entry(symtab, layout, gsym);
6350 // Since this is not a PC-relative relocation, we may be
6351 // taking the address of a function. In that case we need to
6352 // set the entry in the dynamic symbol table to the address of
6354 if (gsym->is_from_dynobj() && !parameters->options().shared())
6355 gsym->set_needs_dynsym_value();
6357 // Make a dynamic relocation if necessary.
6358 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
6360 if (!parameters->options().output_is_position_independent()
6361 && gsym->may_need_copy_reloc())
6363 target->copy_reloc(symtab, layout, object,
6364 data_shndx, output_section, gsym, rela);
6366 else if (r_type == elfcpp::R_AARCH64_ABS64
6367 && gsym->type() == elfcpp::STT_GNU_IFUNC
6368 && gsym->can_use_relative_reloc(false)
6369 && !gsym->is_from_dynobj()
6370 && !gsym->is_undefined()
6371 && !gsym->is_preemptible())
6373 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
6374 // symbol. This makes a function address in a PIE executable
6375 // match the address in a shared library that it links against.
6376 Reloc_section* rela_dyn =
6377 target->rela_irelative_section(layout);
6378 unsigned int r_type = elfcpp::R_AARCH64_IRELATIVE;
6379 rela_dyn->add_symbolless_global_addend(gsym, r_type,
6380 output_section, object,
6382 rela.get_r_offset(),
6383 rela.get_r_addend());
6385 else if (r_type == elfcpp::R_AARCH64_ABS64
6386 && gsym->can_use_relative_reloc(false))
6388 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6389 rela_dyn->add_global_relative(gsym,
6390 elfcpp::R_AARCH64_RELATIVE,
6394 rela.get_r_offset(),
6395 rela.get_r_addend(),
6400 check_non_pic(object, r_type);
6401 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*
6402 rela_dyn = target->rela_dyn_section(layout);
6403 rela_dyn->add_global(
6404 gsym, r_type, output_section, object,
6405 data_shndx, rela.get_r_offset(),rela.get_r_addend());
6411 case elfcpp::R_AARCH64_PREL16:
6412 case elfcpp::R_AARCH64_PREL32:
6413 case elfcpp::R_AARCH64_PREL64:
6414 // This is used to fill the GOT absolute address.
6415 if (gsym->needs_plt_entry())
6417 target->make_plt_entry(symtab, layout, gsym);
6421 case elfcpp::R_AARCH64_MOVW_UABS_G0: // 263
6422 case elfcpp::R_AARCH64_MOVW_UABS_G0_NC: // 264
6423 case elfcpp::R_AARCH64_MOVW_UABS_G1: // 265
6424 case elfcpp::R_AARCH64_MOVW_UABS_G1_NC: // 266
6425 case elfcpp::R_AARCH64_MOVW_UABS_G2: // 267
6426 case elfcpp::R_AARCH64_MOVW_UABS_G2_NC: // 268
6427 case elfcpp::R_AARCH64_MOVW_UABS_G3: // 269
6428 case elfcpp::R_AARCH64_MOVW_SABS_G0: // 270
6429 case elfcpp::R_AARCH64_MOVW_SABS_G1: // 271
6430 case elfcpp::R_AARCH64_MOVW_SABS_G2: // 272
6431 if (parameters->options().output_is_position_independent())
6433 gold_error(_("%s: unsupported reloc %u in pos independent link."),
6434 object->name().c_str(), r_type);
6438 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
6439 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
6440 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
6441 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
6442 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
6443 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
6444 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
6445 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
6446 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
6447 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
6449 if (gsym->needs_plt_entry())
6450 target->make_plt_entry(symtab, layout, gsym);
6451 // Make a dynamic relocation if necessary.
6452 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
6454 if (parameters->options().output_is_executable()
6455 && gsym->may_need_copy_reloc())
6457 target->copy_reloc(symtab, layout, object,
6458 data_shndx, output_section, gsym, rela);
6464 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6465 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6466 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
6468 // The above relocations are used to access GOT entries.
6469 // Note a GOT entry is an *address* to a symbol.
6470 // The symbol requires a GOT entry
6471 Output_data_got_aarch64<size, big_endian>* got =
6472 target->got_section(symtab, layout);
6473 if (gsym->final_value_is_known())
6475 // For a STT_GNU_IFUNC symbol we want the PLT address.
6476 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
6477 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6479 got->add_global(gsym, GOT_TYPE_STANDARD);
6483 // If this symbol is not fully resolved, we need to add a dynamic
6484 // relocation for it.
6485 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6487 // Use a GLOB_DAT rather than a RELATIVE reloc if:
6489 // 1) The symbol may be defined in some other module.
6490 // 2) We are building a shared library and this is a protected
6491 // symbol; using GLOB_DAT means that the dynamic linker can use
6492 // the address of the PLT in the main executable when appropriate
6493 // so that function address comparisons work.
6494 // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
6495 // again so that function address comparisons work.
6496 if (gsym->is_from_dynobj()
6497 || gsym->is_undefined()
6498 || gsym->is_preemptible()
6499 || (gsym->visibility() == elfcpp::STV_PROTECTED
6500 && parameters->options().shared())
6501 || (gsym->type() == elfcpp::STT_GNU_IFUNC
6502 && parameters->options().output_is_position_independent()))
6503 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
6504 rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
6507 // For a STT_GNU_IFUNC symbol we want to write the PLT
6508 // offset into the GOT, so that function pointer
6509 // comparisons work correctly.
6511 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
6512 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
6515 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6516 // Tell the dynamic linker to use the PLT address
6517 // when resolving relocations.
6518 if (gsym->is_from_dynobj()
6519 && !parameters->options().shared())
6520 gsym->set_needs_dynsym_value();
6524 rela_dyn->add_global_relative(
6525 gsym, elfcpp::R_AARCH64_RELATIVE,
6527 gsym->got_offset(GOT_TYPE_STANDARD),
6536 case elfcpp::R_AARCH64_TSTBR14:
6537 case elfcpp::R_AARCH64_CONDBR19:
6538 case elfcpp::R_AARCH64_JUMP26:
6539 case elfcpp::R_AARCH64_CALL26:
6541 if (gsym->final_value_is_known())
6544 if (gsym->is_defined() &&
6545 !gsym->is_from_dynobj() &&
6546 !gsym->is_preemptible())
6549 // Make plt entry for function call.
6550 target->make_plt_entry(symtab, layout, gsym);
6554 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6555 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // General dynamic
6557 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6558 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6559 if (tlsopt == tls::TLSOPT_TO_LE)
6561 layout->set_has_static_tls();
6564 gold_assert(tlsopt == tls::TLSOPT_NONE);
6567 Output_data_got_aarch64<size, big_endian>* got =
6568 target->got_section(symtab, layout);
6569 // Create 2 consecutive entries for module index and offset.
6570 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
6571 target->rela_dyn_section(layout),
6572 elfcpp::R_AARCH64_TLS_DTPMOD64,
6573 elfcpp::R_AARCH64_TLS_DTPREL64);
6577 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6578 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local dynamic
6580 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6581 optimize_tls_reloc(!parameters->options().shared(), r_type);
6582 if (tlsopt == tls::TLSOPT_NONE)
6584 // Create a GOT entry for the module index.
6585 target->got_mod_index_entry(symtab, layout, object);
6587 else if (tlsopt != tls::TLSOPT_TO_LE)
6588 unsupported_reloc_local(object, r_type);
6592 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6593 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6594 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6595 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local dynamic
6598 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6599 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial executable
6601 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6602 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6603 if (tlsopt == tls::TLSOPT_TO_LE)
6606 layout->set_has_static_tls();
6607 // Create a GOT entry for the tp-relative offset.
6608 Output_data_got_aarch64<size, big_endian>* got
6609 = target->got_section(symtab, layout);
6610 if (!parameters->doing_static_link())
6612 got->add_global_with_rel(
6613 gsym, GOT_TYPE_TLS_OFFSET,
6614 target->rela_dyn_section(layout),
6615 elfcpp::R_AARCH64_TLS_TPREL64);
6617 if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
6619 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
6620 unsigned int got_offset =
6621 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
6622 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6623 gold_assert(addend == 0);
6624 got->add_static_reloc(got_offset,
6625 elfcpp::R_AARCH64_TLS_TPREL64, gsym);
6630 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6631 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6632 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6633 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6634 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6635 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6636 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6637 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: // Local executable
6638 layout->set_has_static_tls();
6639 if (parameters->options().shared())
6640 gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
6641 object->name().c_str(), r_type);
6644 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6645 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6646 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12: // TLS descriptor
6648 target->define_tls_base_symbol(symtab, layout);
6649 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6650 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6651 if (tlsopt == tls::TLSOPT_NONE)
6653 // Create reserved PLT and GOT entries for the resolver.
6654 target->reserve_tlsdesc_entries(symtab, layout);
6656 // Create a double GOT entry with an R_AARCH64_TLSDESC
6657 // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
6658 // entry needs to be in an area in .got.plt, not .got. Call
6659 // got_section to make sure the section has been created.
6660 target->got_section(symtab, layout);
6661 Output_data_got<size, big_endian>* got =
6662 target->got_tlsdesc_section();
6663 Reloc_section* rt = target->rela_tlsdesc_section(layout);
6664 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
6665 elfcpp::R_AARCH64_TLSDESC, 0);
6667 else if (tlsopt == tls::TLSOPT_TO_IE)
6669 // Create a GOT entry for the tp-relative offset.
6670 Output_data_got<size, big_endian>* got
6671 = target->got_section(symtab, layout);
6672 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
6673 target->rela_dyn_section(layout),
6674 elfcpp::R_AARCH64_TLS_TPREL64);
6676 else if (tlsopt != tls::TLSOPT_TO_LE)
6677 unsupported_reloc_global(object, r_type, gsym);
6681 case elfcpp::R_AARCH64_TLSDESC_CALL:
6685 gold_error(_("%s: unsupported reloc type in global scan"),
6686 aarch64_reloc_property_table->
6687 reloc_name_in_error_message(r_type).c_str());
6690 } // End of Scan::global
6693 // Create the PLT section.
6694 template<int size, bool big_endian>
6696 Target_aarch64<size, big_endian>::make_plt_section(
6697 Symbol_table* symtab, Layout* layout)
6699 if (this->plt_ == NULL)
6701 // Create the GOT section first.
6702 this->got_section(symtab, layout);
6704 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
6705 this->got_irelative_);
6707 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
6709 | elfcpp::SHF_EXECINSTR),
6710 this->plt_, ORDER_PLT, false);
6712 // Make the sh_info field of .rela.plt point to .plt.
6713 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
6714 rela_plt_os->set_info_section(this->plt_->output_section());
6718 // Return the section for TLSDESC relocations.
6720 template<int size, bool big_endian>
6721 typename Target_aarch64<size, big_endian>::Reloc_section*
6722 Target_aarch64<size, big_endian>::rela_tlsdesc_section(Layout* layout) const
6724 return this->plt_section()->rela_tlsdesc(layout);
6727 // Create a PLT entry for a global symbol.
6729 template<int size, bool big_endian>
6731 Target_aarch64<size, big_endian>::make_plt_entry(
6732 Symbol_table* symtab,
6736 if (gsym->has_plt_offset())
6739 if (this->plt_ == NULL)
6740 this->make_plt_section(symtab, layout);
6742 this->plt_->add_entry(symtab, layout, gsym);
6745 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6747 template<int size, bool big_endian>
6749 Target_aarch64<size, big_endian>::make_local_ifunc_plt_entry(
6750 Symbol_table* symtab, Layout* layout,
6751 Sized_relobj_file<size, big_endian>* relobj,
6752 unsigned int local_sym_index)
6754 if (relobj->local_has_plt_offset(local_sym_index))
6756 if (this->plt_ == NULL)
6757 this->make_plt_section(symtab, layout);
6758 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
6761 relobj->set_local_plt_offset(local_sym_index, plt_offset);
6764 template<int size, bool big_endian>
6766 Target_aarch64<size, big_endian>::gc_process_relocs(
6767 Symbol_table* symtab,
6769 Sized_relobj_file<size, big_endian>* object,
6770 unsigned int data_shndx,
6771 unsigned int sh_type,
6772 const unsigned char* prelocs,
6774 Output_section* output_section,
6775 bool needs_special_offset_handling,
6776 size_t local_symbol_count,
6777 const unsigned char* plocal_symbols)
6779 typedef Target_aarch64<size, big_endian> Aarch64;
6780 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
6783 if (sh_type == elfcpp::SHT_REL)
6788 gold::gc_process_relocs<size, big_endian, Aarch64, Scan, Classify_reloc>(
6797 needs_special_offset_handling,
6802 // Scan relocations for a section.
6804 template<int size, bool big_endian>
6806 Target_aarch64<size, big_endian>::scan_relocs(
6807 Symbol_table* symtab,
6809 Sized_relobj_file<size, big_endian>* object,
6810 unsigned int data_shndx,
6811 unsigned int sh_type,
6812 const unsigned char* prelocs,
6814 Output_section* output_section,
6815 bool needs_special_offset_handling,
6816 size_t local_symbol_count,
6817 const unsigned char* plocal_symbols)
6819 typedef Target_aarch64<size, big_endian> Aarch64;
6820 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
6823 if (sh_type == elfcpp::SHT_REL)
6825 gold_error(_("%s: unsupported REL reloc section"),
6826 object->name().c_str());
6830 gold::scan_relocs<size, big_endian, Aarch64, Scan, Classify_reloc>(
6839 needs_special_offset_handling,
6844 // Return the value to use for a dynamic which requires special
6845 // treatment. This is how we support equality comparisons of function
6846 // pointers across shared library boundaries, as described in the
6847 // processor specific ABI supplement.
6849 template<int size, bool big_endian>
6851 Target_aarch64<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
6853 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
6854 return this->plt_address_for_global(gsym);
6858 // Finalize the sections.
6860 template<int size, bool big_endian>
6862 Target_aarch64<size, big_endian>::do_finalize_sections(
6864 const Input_objects*,
6865 Symbol_table* symtab)
6867 const Reloc_section* rel_plt = (this->plt_ == NULL
6869 : this->plt_->rela_plt());
6870 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
6871 this->rela_dyn_, true, false);
6873 // Emit any relocs we saved in an attempt to avoid generating COPY
6875 if (this->copy_relocs_.any_saved_relocs())
6876 this->copy_relocs_.emit(this->rela_dyn_section(layout));
6878 // Fill in some more dynamic tags.
6879 Output_data_dynamic* const odyn = layout->dynamic_data();
6882 if (this->plt_ != NULL
6883 && this->plt_->output_section() != NULL
6884 && this->plt_ ->has_tlsdesc_entry())
6886 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
6887 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
6888 this->got_->finalize_data_size();
6889 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
6890 this->plt_, plt_offset);
6891 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
6892 this->got_, got_offset);
6896 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
6897 // the .got.plt section.
6898 Symbol* sym = this->global_offset_table_;
6901 uint64_t data_size = this->got_plt_->current_data_size();
6902 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
6904 // If the .got section is more than 0x8000 bytes, we add
6905 // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
6906 // bit relocations have a greater chance of working.
6907 if (data_size >= 0x8000)
6908 symtab->get_sized_symbol<size>(sym)->set_value(
6909 symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
6912 if (parameters->doing_static_link()
6913 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
6915 // If linking statically, make sure that the __rela_iplt symbols
6916 // were defined if necessary, even if we didn't create a PLT.
6917 static const Define_symbol_in_segment syms[] =
6920 "__rela_iplt_start", // name
6921 elfcpp::PT_LOAD, // segment_type
6922 elfcpp::PF_W, // segment_flags_set
6923 elfcpp::PF(0), // segment_flags_clear
6926 elfcpp::STT_NOTYPE, // type
6927 elfcpp::STB_GLOBAL, // binding
6928 elfcpp::STV_HIDDEN, // visibility
6930 Symbol::SEGMENT_START, // offset_from_base
6934 "__rela_iplt_end", // name
6935 elfcpp::PT_LOAD, // segment_type
6936 elfcpp::PF_W, // segment_flags_set
6937 elfcpp::PF(0), // segment_flags_clear
6940 elfcpp::STT_NOTYPE, // type
6941 elfcpp::STB_GLOBAL, // binding
6942 elfcpp::STV_HIDDEN, // visibility
6944 Symbol::SEGMENT_START, // offset_from_base
6949 symtab->define_symbols(layout, 2, syms,
6950 layout->script_options()->saw_sections_clause());
6956 // Perform a relocation.
6958 template<int size, bool big_endian>
6960 Target_aarch64<size, big_endian>::Relocate::relocate(
6961 const Relocate_info<size, big_endian>* relinfo,
6963 Target_aarch64<size, big_endian>* target,
6966 const unsigned char* preloc,
6967 const Sized_symbol<size>* gsym,
6968 const Symbol_value<size>* psymval,
6969 unsigned char* view,
6970 typename elfcpp::Elf_types<size>::Elf_Addr address,
6971 section_size_type /* view_size */)
6976 typedef AArch64_relocate_functions<size, big_endian> Reloc;
6978 const elfcpp::Rela<size, big_endian> rela(preloc);
6979 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
6980 const AArch64_reloc_property* reloc_property =
6981 aarch64_reloc_property_table->get_reloc_property(r_type);
6983 if (reloc_property == NULL)
6985 std::string reloc_name =
6986 aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
6987 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6988 _("cannot relocate %s in object file"),
6989 reloc_name.c_str());
6993 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
6995 // Pick the value to use for symbols defined in the PLT.
6996 Symbol_value<size> symval;
6998 && gsym->use_plt_offset(reloc_property->reference_flags()))
7000 symval.set_output_value(target->plt_address_for_global(gsym));
7003 else if (gsym == NULL && psymval->is_ifunc_symbol())
7005 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7006 if (object->local_has_plt_offset(r_sym))
7008 symval.set_output_value(target->plt_address_for_local(object, r_sym));
7013 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7015 // Get the GOT offset if needed.
7016 // For aarch64, the GOT pointer points to the start of the GOT section.
7017 bool have_got_offset = false;
7019 int got_base = (target->got_ != NULL
7020 ? (target->got_->current_data_size() >= 0x8000
7025 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
7026 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
7027 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
7028 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
7029 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
7030 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
7031 case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
7032 case elfcpp::R_AARCH64_GOTREL64:
7033 case elfcpp::R_AARCH64_GOTREL32:
7034 case elfcpp::R_AARCH64_GOT_LD_PREL19:
7035 case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
7036 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
7037 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
7038 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
7041 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
7042 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
7046 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7047 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
7048 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
7051 have_got_offset = true;
7058 typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
7059 typename elfcpp::Elf_types<size>::Elf_Addr value;
7062 case elfcpp::R_AARCH64_NONE:
7065 case elfcpp::R_AARCH64_ABS64:
7066 if (!parameters->options().apply_dynamic_relocs()
7067 && parameters->options().output_is_position_independent()
7069 && gsym->needs_dynamic_reloc(reloc_property->reference_flags())
7070 && !gsym->can_use_relative_reloc(false))
7071 // We have generated an absolute dynamic relocation, so do not
7072 // apply the relocation statically. (Works around bugs in older
7073 // Android dynamic linkers.)
7075 reloc_status = Reloc::template rela_ua<64>(
7076 view, object, psymval, addend, reloc_property);
7079 case elfcpp::R_AARCH64_ABS32:
7080 if (!parameters->options().apply_dynamic_relocs()
7081 && parameters->options().output_is_position_independent()
7083 && gsym->needs_dynamic_reloc(reloc_property->reference_flags()))
7084 // We have generated an absolute dynamic relocation, so do not
7085 // apply the relocation statically. (Works around bugs in older
7086 // Android dynamic linkers.)
7088 reloc_status = Reloc::template rela_ua<32>(
7089 view, object, psymval, addend, reloc_property);
7092 case elfcpp::R_AARCH64_ABS16:
7093 if (!parameters->options().apply_dynamic_relocs()
7094 && parameters->options().output_is_position_independent()
7096 && gsym->needs_dynamic_reloc(reloc_property->reference_flags()))
7097 // We have generated an absolute dynamic relocation, so do not
7098 // apply the relocation statically. (Works around bugs in older
7099 // Android dynamic linkers.)
7101 reloc_status = Reloc::template rela_ua<16>(
7102 view, object, psymval, addend, reloc_property);
7105 case elfcpp::R_AARCH64_PREL64:
7106 reloc_status = Reloc::template pcrela_ua<64>(
7107 view, object, psymval, addend, address, reloc_property);
7110 case elfcpp::R_AARCH64_PREL32:
7111 reloc_status = Reloc::template pcrela_ua<32>(
7112 view, object, psymval, addend, address, reloc_property);
7115 case elfcpp::R_AARCH64_PREL16:
7116 reloc_status = Reloc::template pcrela_ua<16>(
7117 view, object, psymval, addend, address, reloc_property);
7120 case elfcpp::R_AARCH64_MOVW_UABS_G0:
7121 case elfcpp::R_AARCH64_MOVW_UABS_G0_NC:
7122 case elfcpp::R_AARCH64_MOVW_UABS_G1:
7123 case elfcpp::R_AARCH64_MOVW_UABS_G1_NC:
7124 case elfcpp::R_AARCH64_MOVW_UABS_G2:
7125 case elfcpp::R_AARCH64_MOVW_UABS_G2_NC:
7126 case elfcpp::R_AARCH64_MOVW_UABS_G3:
7127 reloc_status = Reloc::template rela_general<32>(
7128 view, object, psymval, addend, reloc_property);
7130 case elfcpp::R_AARCH64_MOVW_SABS_G0:
7131 case elfcpp::R_AARCH64_MOVW_SABS_G1:
7132 case elfcpp::R_AARCH64_MOVW_SABS_G2:
7133 reloc_status = Reloc::movnz(view, psymval->value(object, addend),
7137 case elfcpp::R_AARCH64_LD_PREL_LO19:
7138 reloc_status = Reloc::template pcrela_general<32>(
7139 view, object, psymval, addend, address, reloc_property);
7142 case elfcpp::R_AARCH64_ADR_PREL_LO21:
7143 reloc_status = Reloc::adr(view, object, psymval, addend,
7144 address, reloc_property);
7147 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
7148 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
7149 reloc_status = Reloc::adrp(view, object, psymval, addend, address,
7153 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
7154 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
7155 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
7156 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
7157 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
7158 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
7159 reloc_status = Reloc::template rela_general<32>(
7160 view, object, psymval, addend, reloc_property);
7163 case elfcpp::R_AARCH64_CALL26:
7164 if (this->skip_call_tls_get_addr_)
7166 // Double check that the TLSGD insn has been optimized away.
7167 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7168 Insntype insn = elfcpp::Swap<32, big_endian>::readval(
7169 reinterpret_cast<Insntype*>(view));
7170 gold_assert((insn & 0xff000000) == 0x91000000);
7172 reloc_status = Reloc::STATUS_OKAY;
7173 this->skip_call_tls_get_addr_ = false;
7174 // Return false to stop further processing this reloc.
7178 case elfcpp::R_AARCH64_JUMP26:
7179 if (Reloc::maybe_apply_stub(r_type, relinfo, rela, view, address,
7180 gsym, psymval, object,
7181 target->stub_group_size_))
7184 case elfcpp::R_AARCH64_TSTBR14:
7185 case elfcpp::R_AARCH64_CONDBR19:
7186 reloc_status = Reloc::template pcrela_general<32>(
7187 view, object, psymval, addend, address, reloc_property);
7190 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
7191 gold_assert(have_got_offset);
7192 value = target->got_->address() + got_base + got_offset;
7193 reloc_status = Reloc::adrp(view, value + addend, address);
7196 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
7197 gold_assert(have_got_offset);
7198 value = target->got_->address() + got_base + got_offset;
7199 reloc_status = Reloc::template rela_general<32>(
7200 view, value, addend, reloc_property);
7203 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
7205 gold_assert(have_got_offset);
7206 value = target->got_->address() + got_base + got_offset + addend -
7207 Reloc::Page(target->got_->address() + got_base);
7208 if ((value & 7) != 0)
7209 reloc_status = Reloc::STATUS_OVERFLOW;
7211 reloc_status = Reloc::template reloc_common<32>(
7212 view, value, reloc_property);
7216 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
7217 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
7218 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
7219 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
7220 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
7221 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7222 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
7223 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
7224 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7225 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7226 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
7227 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
7228 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7229 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7230 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
7231 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
7232 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
7233 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
7234 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7235 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7236 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7237 case elfcpp::R_AARCH64_TLSDESC_CALL:
7238 reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
7239 gsym, psymval, view, address);
7242 // These are dynamic relocations, which are unexpected when linking.
7243 case elfcpp::R_AARCH64_COPY:
7244 case elfcpp::R_AARCH64_GLOB_DAT:
7245 case elfcpp::R_AARCH64_JUMP_SLOT:
7246 case elfcpp::R_AARCH64_RELATIVE:
7247 case elfcpp::R_AARCH64_IRELATIVE:
7248 case elfcpp::R_AARCH64_TLS_DTPREL64:
7249 case elfcpp::R_AARCH64_TLS_DTPMOD64:
7250 case elfcpp::R_AARCH64_TLS_TPREL64:
7251 case elfcpp::R_AARCH64_TLSDESC:
7252 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7253 _("unexpected reloc %u in object file"),
7258 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7259 _("unsupported reloc %s"),
7260 reloc_property->name().c_str());
7264 // Report any errors.
7265 switch (reloc_status)
7267 case Reloc::STATUS_OKAY:
7269 case Reloc::STATUS_OVERFLOW:
7270 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7271 _("relocation overflow in %s"),
7272 reloc_property->name().c_str());
7274 case Reloc::STATUS_BAD_RELOC:
7275 gold_error_at_location(
7278 rela.get_r_offset(),
7279 _("unexpected opcode while processing relocation %s"),
7280 reloc_property->name().c_str());
7290 template<int size, bool big_endian>
7292 typename AArch64_relocate_functions<size, big_endian>::Status
7293 Target_aarch64<size, big_endian>::Relocate::relocate_tls(
7294 const Relocate_info<size, big_endian>* relinfo,
7295 Target_aarch64<size, big_endian>* target,
7297 const elfcpp::Rela<size, big_endian>& rela,
7298 unsigned int r_type, const Sized_symbol<size>* gsym,
7299 const Symbol_value<size>* psymval,
7300 unsigned char* view,
7301 typename elfcpp::Elf_types<size>::Elf_Addr address)
7303 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7304 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7306 Output_segment* tls_segment = relinfo->layout->tls_segment();
7307 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7308 const AArch64_reloc_property* reloc_property =
7309 aarch64_reloc_property_table->get_reloc_property(r_type);
7310 gold_assert(reloc_property != NULL);
7312 const bool is_final = (gsym == NULL
7313 ? !parameters->options().shared()
7314 : gsym->final_value_is_known());
7315 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
7316 optimize_tls_reloc(is_final, r_type);
7318 Sized_relobj_file<size, big_endian>* object = relinfo->object;
7319 int tls_got_offset_type;
7322 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
7323 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // Global-dynamic
7325 if (tlsopt == tls::TLSOPT_TO_LE)
7327 if (tls_segment == NULL)
7329 gold_assert(parameters->errors()->error_count() > 0
7330 || issue_undefined_symbol_error(gsym));
7331 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7333 return tls_gd_to_le(relinfo, target, rela, r_type, view,
7336 else if (tlsopt == tls::TLSOPT_NONE)
7338 tls_got_offset_type = GOT_TYPE_TLS_PAIR;
7339 // Firstly get the address for the got entry.
7340 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7343 gold_assert(gsym->has_got_offset(tls_got_offset_type));
7344 got_entry_address = target->got_->address() +
7345 gsym->got_offset(tls_got_offset_type);
7349 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7351 object->local_has_got_offset(r_sym, tls_got_offset_type));
7352 got_entry_address = target->got_->address() +
7353 object->local_got_offset(r_sym, tls_got_offset_type);
7356 // Relocate the address into adrp/ld, adrp/add pair.
7359 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
7360 return aarch64_reloc_funcs::adrp(
7361 view, got_entry_address + addend, address);
7365 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
7366 return aarch64_reloc_funcs::template rela_general<32>(
7367 view, got_entry_address, addend, reloc_property);
7374 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7375 _("unsupported gd_to_ie relaxation on %u"),
7380 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
7381 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local-dynamic
7383 if (tlsopt == tls::TLSOPT_TO_LE)
7385 if (tls_segment == NULL)
7387 gold_assert(parameters->errors()->error_count() > 0
7388 || issue_undefined_symbol_error(gsym));
7389 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7391 return this->tls_ld_to_le(relinfo, target, rela, r_type, view,
7395 gold_assert(tlsopt == tls::TLSOPT_NONE);
7396 // Relocate the field with the offset of the GOT entry for
7397 // the module index.
7398 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7399 got_entry_address = (target->got_mod_index_entry(NULL, NULL, NULL) +
7400 target->got_->address());
7404 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
7405 return aarch64_reloc_funcs::adrp(
7406 view, got_entry_address + addend, address);
7409 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
7410 return aarch64_reloc_funcs::template rela_general<32>(
7411 view, got_entry_address, addend, reloc_property);
7420 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
7421 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7422 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
7423 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local-dynamic
7425 AArch64_address value = psymval->value(object, 0);
7426 if (tlsopt == tls::TLSOPT_TO_LE)
7428 if (tls_segment == NULL)
7430 gold_assert(parameters->errors()->error_count() > 0
7431 || issue_undefined_symbol_error(gsym));
7432 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7437 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
7438 return aarch64_reloc_funcs::movnz(view, value + addend,
7442 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7443 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
7444 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
7445 return aarch64_reloc_funcs::template rela_general<32>(
7446 view, value, addend, reloc_property);
7452 // We should never reach here.
7456 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7457 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial-exec
7459 if (tlsopt == tls::TLSOPT_TO_LE)
7461 if (tls_segment == NULL)
7463 gold_assert(parameters->errors()->error_count() > 0
7464 || issue_undefined_symbol_error(gsym));
7465 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7467 return tls_ie_to_le(relinfo, target, rela, r_type, view,
7470 tls_got_offset_type = GOT_TYPE_TLS_OFFSET;
7472 // Firstly get the address for the got entry.
7473 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7476 gold_assert(gsym->has_got_offset(tls_got_offset_type));
7477 got_entry_address = target->got_->address() +
7478 gsym->got_offset(tls_got_offset_type);
7482 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7484 object->local_has_got_offset(r_sym, tls_got_offset_type));
7485 got_entry_address = target->got_->address() +
7486 object->local_got_offset(r_sym, tls_got_offset_type);
7488 // Relocate the address into adrp/ld, adrp/add pair.
7491 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7492 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
7495 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7496 return aarch64_reloc_funcs::template rela_general<32>(
7497 view, got_entry_address, addend, reloc_property);
7502 // We shall never reach here.
7505 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
7506 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
7507 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7508 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7509 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
7510 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
7511 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
7512 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
7514 gold_assert(tls_segment != NULL);
7515 AArch64_address value = psymval->value(object, 0);
7517 if (!parameters->options().shared())
7519 AArch64_address aligned_tcb_size =
7520 align_address(target->tcb_size(),
7521 tls_segment->maximum_alignment());
7522 value += aligned_tcb_size;
7525 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
7526 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
7527 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7528 return aarch64_reloc_funcs::movnz(view, value + addend,
7531 return aarch64_reloc_funcs::template
7532 rela_general<32>(view,
7539 gold_error(_("%s: unsupported reloc %u "
7540 "in non-static TLSLE mode."),
7541 object->name().c_str(), r_type);
7545 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7546 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7547 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7548 case elfcpp::R_AARCH64_TLSDESC_CALL:
7550 if (tlsopt == tls::TLSOPT_TO_LE)
7552 if (tls_segment == NULL)
7554 gold_assert(parameters->errors()->error_count() > 0
7555 || issue_undefined_symbol_error(gsym));
7556 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7558 return tls_desc_gd_to_le(relinfo, target, rela, r_type,
7563 tls_got_offset_type = (tlsopt == tls::TLSOPT_TO_IE
7564 ? GOT_TYPE_TLS_OFFSET
7565 : GOT_TYPE_TLS_DESC);
7566 unsigned int got_tlsdesc_offset = 0;
7567 if (r_type != elfcpp::R_AARCH64_TLSDESC_CALL
7568 && tlsopt == tls::TLSOPT_NONE)
7570 // We created GOT entries in the .got.tlsdesc portion of the
7571 // .got.plt section, but the offset stored in the symbol is the
7572 // offset within .got.tlsdesc.
7573 got_tlsdesc_offset = (target->got_->data_size()
7574 + target->got_plt_section()->data_size());
7576 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7579 gold_assert(gsym->has_got_offset(tls_got_offset_type));
7580 got_entry_address = target->got_->address()
7581 + got_tlsdesc_offset
7582 + gsym->got_offset(tls_got_offset_type);
7586 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7588 object->local_has_got_offset(r_sym, tls_got_offset_type));
7589 got_entry_address = target->got_->address() +
7590 got_tlsdesc_offset +
7591 object->local_got_offset(r_sym, tls_got_offset_type);
7593 if (tlsopt == tls::TLSOPT_TO_IE)
7595 return tls_desc_gd_to_ie(relinfo, target, rela, r_type,
7596 view, psymval, got_entry_address,
7600 // Now do tlsdesc relocation.
7603 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7604 return aarch64_reloc_funcs::adrp(view,
7605 got_entry_address + addend,
7608 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7609 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7610 return aarch64_reloc_funcs::template rela_general<32>(
7611 view, got_entry_address, addend, reloc_property);
7613 case elfcpp::R_AARCH64_TLSDESC_CALL:
7614 return aarch64_reloc_funcs::STATUS_OKAY;
7624 gold_error(_("%s: unsupported TLS reloc %u."),
7625 object->name().c_str(), r_type);
7627 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7628 } // End of relocate_tls.
7631 template<int size, bool big_endian>
7633 typename AArch64_relocate_functions<size, big_endian>::Status
7634 Target_aarch64<size, big_endian>::Relocate::tls_gd_to_le(
7635 const Relocate_info<size, big_endian>* relinfo,
7636 Target_aarch64<size, big_endian>* target,
7637 const elfcpp::Rela<size, big_endian>& rela,
7638 unsigned int r_type,
7639 unsigned char* view,
7640 const Symbol_value<size>* psymval)
7642 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7643 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7644 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7646 Insntype* ip = reinterpret_cast<Insntype*>(view);
7647 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
7648 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
7649 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
7651 if (r_type == elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC)
7653 // This is the 2nd relocs, optimization should already have been
7655 gold_assert((insn1 & 0xfff00000) == 0x91400000);
7656 return aarch64_reloc_funcs::STATUS_OKAY;
7659 // The original sequence is -
7660 // 90000000 adrp x0, 0 <main>
7661 // 91000000 add x0, x0, #0x0
7662 // 94000000 bl 0 <__tls_get_addr>
7663 // optimized to sequence -
7664 // d53bd040 mrs x0, tpidr_el0
7665 // 91400000 add x0, x0, #0x0, lsl #12
7666 // 91000000 add x0, x0, #0x0
7668 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7669 // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
7670 // have to change "bl tls_get_addr", which does not have a corresponding tls
7671 // relocation type. So before proceeding, we need to make sure compiler
7672 // does not change the sequence.
7673 if(!(insn1 == 0x90000000 // adrp x0,0
7674 && insn2 == 0x91000000 // add x0, x0, #0x0
7675 && insn3 == 0x94000000)) // bl 0
7677 // Ideally we should give up gd_to_le relaxation and do gd access.
7678 // However the gd_to_le relaxation decision has been made early
7679 // in the scan stage, where we did not allocate any GOT entry for
7680 // this symbol. Therefore we have to exit and report error now.
7681 gold_error(_("unexpected reloc insn sequence while relaxing "
7682 "tls gd to le for reloc %u."), r_type);
7683 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7687 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
7688 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
7689 insn3 = 0x91000000; // add x0, x0, #0x0
7690 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
7691 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
7692 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
7694 // Calculate tprel value.
7695 Output_segment* tls_segment = relinfo->layout->tls_segment();
7696 gold_assert(tls_segment != NULL);
7697 AArch64_address value = psymval->value(relinfo->object, 0);
7698 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7699 AArch64_address aligned_tcb_size =
7700 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7701 AArch64_address x = value + aligned_tcb_size;
7703 // After new insns are written, apply TLSLE relocs.
7704 const AArch64_reloc_property* rp1 =
7705 aarch64_reloc_property_table->get_reloc_property(
7706 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
7707 const AArch64_reloc_property* rp2 =
7708 aarch64_reloc_property_table->get_reloc_property(
7709 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
7710 gold_assert(rp1 != NULL && rp2 != NULL);
7712 typename aarch64_reloc_funcs::Status s1 =
7713 aarch64_reloc_funcs::template rela_general<32>(view + 4,
7717 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
7720 typename aarch64_reloc_funcs::Status s2 =
7721 aarch64_reloc_funcs::template rela_general<32>(view + 8,
7726 this->skip_call_tls_get_addr_ = true;
7728 } // End of tls_gd_to_le
7731 template<int size, bool big_endian>
7733 typename AArch64_relocate_functions<size, big_endian>::Status
7734 Target_aarch64<size, big_endian>::Relocate::tls_ld_to_le(
7735 const Relocate_info<size, big_endian>* relinfo,
7736 Target_aarch64<size, big_endian>* target,
7737 const elfcpp::Rela<size, big_endian>& rela,
7738 unsigned int r_type,
7739 unsigned char* view,
7740 const Symbol_value<size>* psymval)
7742 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7743 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7744 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7746 Insntype* ip = reinterpret_cast<Insntype*>(view);
7747 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
7748 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
7749 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
7751 if (r_type == elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC)
7753 // This is the 2nd relocs, optimization should already have been
7755 gold_assert((insn1 & 0xfff00000) == 0x91400000);
7756 return aarch64_reloc_funcs::STATUS_OKAY;
7759 // The original sequence is -
7760 // 90000000 adrp x0, 0 <main>
7761 // 91000000 add x0, x0, #0x0
7762 // 94000000 bl 0 <__tls_get_addr>
7763 // optimized to sequence -
7764 // d53bd040 mrs x0, tpidr_el0
7765 // 91400000 add x0, x0, #0x0, lsl #12
7766 // 91000000 add x0, x0, #0x0
7768 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7769 // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
7770 // have to change "bl tls_get_addr", which does not have a corresponding tls
7771 // relocation type. So before proceeding, we need to make sure compiler
7772 // does not change the sequence.
7773 if(!(insn1 == 0x90000000 // adrp x0,0
7774 && insn2 == 0x91000000 // add x0, x0, #0x0
7775 && insn3 == 0x94000000)) // bl 0
7777 // Ideally we should give up gd_to_le relaxation and do gd access.
7778 // However the gd_to_le relaxation decision has been made early
7779 // in the scan stage, where we did not allocate a GOT entry for
7780 // this symbol. Therefore we have to exit and report an error now.
7781 gold_error(_("unexpected reloc insn sequence while relaxing "
7782 "tls gd to le for reloc %u."), r_type);
7783 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7787 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
7788 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
7789 insn3 = 0x91000000; // add x0, x0, #0x0
7790 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
7791 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
7792 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
7794 // Calculate tprel value.
7795 Output_segment* tls_segment = relinfo->layout->tls_segment();
7796 gold_assert(tls_segment != NULL);
7797 AArch64_address value = psymval->value(relinfo->object, 0);
7798 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7799 AArch64_address aligned_tcb_size =
7800 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7801 AArch64_address x = value + aligned_tcb_size;
7803 // After new insns are written, apply TLSLE relocs.
7804 const AArch64_reloc_property* rp1 =
7805 aarch64_reloc_property_table->get_reloc_property(
7806 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
7807 const AArch64_reloc_property* rp2 =
7808 aarch64_reloc_property_table->get_reloc_property(
7809 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
7810 gold_assert(rp1 != NULL && rp2 != NULL);
7812 typename aarch64_reloc_funcs::Status s1 =
7813 aarch64_reloc_funcs::template rela_general<32>(view + 4,
7817 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
7820 typename aarch64_reloc_funcs::Status s2 =
7821 aarch64_reloc_funcs::template rela_general<32>(view + 8,
7826 this->skip_call_tls_get_addr_ = true;
7829 } // End of tls_ld_to_le
7831 template<int size, bool big_endian>
7833 typename AArch64_relocate_functions<size, big_endian>::Status
7834 Target_aarch64<size, big_endian>::Relocate::tls_ie_to_le(
7835 const Relocate_info<size, big_endian>* relinfo,
7836 Target_aarch64<size, big_endian>* target,
7837 const elfcpp::Rela<size, big_endian>& rela,
7838 unsigned int r_type,
7839 unsigned char* view,
7840 const Symbol_value<size>* psymval)
7842 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7843 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7844 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7846 AArch64_address value = psymval->value(relinfo->object, 0);
7847 Output_segment* tls_segment = relinfo->layout->tls_segment();
7848 AArch64_address aligned_tcb_address =
7849 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7850 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7851 AArch64_address x = value + addend + aligned_tcb_address;
7852 // "x" is the offset to tp, we can only do this if x is within
7853 // range [0, 2^32-1]
7854 if (!(size == 32 || (size == 64 && (static_cast<uint64_t>(x) >> 32) == 0)))
7856 gold_error(_("TLS variable referred by reloc %u is too far from TP."),
7858 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7861 Insntype* ip = reinterpret_cast<Insntype*>(view);
7862 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
7865 if (r_type == elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21)
7868 regno = (insn & 0x1f);
7869 newinsn = (0xd2a00000 | regno) | (((x >> 16) & 0xffff) << 5);
7871 else if (r_type == elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
7874 regno = (insn & 0x1f);
7875 gold_assert(regno == ((insn >> 5) & 0x1f));
7876 newinsn = (0xf2800000 | regno) | ((x & 0xffff) << 5);
7881 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7882 return aarch64_reloc_funcs::STATUS_OKAY;
7883 } // End of tls_ie_to_le
7886 template<int size, bool big_endian>
7888 typename AArch64_relocate_functions<size, big_endian>::Status
7889 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_le(
7890 const Relocate_info<size, big_endian>* relinfo,
7891 Target_aarch64<size, big_endian>* target,
7892 const elfcpp::Rela<size, big_endian>& rela,
7893 unsigned int r_type,
7894 unsigned char* view,
7895 const Symbol_value<size>* psymval)
7897 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7898 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7899 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7901 // TLSDESC-GD sequence is like:
7902 // adrp x0, :tlsdesc:v1
7903 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7904 // add x0, x0, :tlsdesc_lo12:v1
7907 // After desc_gd_to_le optimization, the sequence will be like:
7908 // movz x0, #0x0, lsl #16
7913 // Calculate tprel value.
7914 Output_segment* tls_segment = relinfo->layout->tls_segment();
7915 gold_assert(tls_segment != NULL);
7916 Insntype* ip = reinterpret_cast<Insntype*>(view);
7917 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7918 AArch64_address value = psymval->value(relinfo->object, addend);
7919 AArch64_address aligned_tcb_size =
7920 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7921 AArch64_address x = value + aligned_tcb_size;
7922 // x is the offset to tp, we can only do this if x is within range
7923 // [0, 2^32-1]. If x is out of range, fail and exit.
7924 if (size == 64 && (static_cast<uint64_t>(x) >> 32) != 0)
7926 gold_error(_("TLS variable referred by reloc %u is too far from TP. "
7927 "We Can't do gd_to_le relaxation.\n"), r_type);
7928 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7933 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7934 case elfcpp::R_AARCH64_TLSDESC_CALL:
7936 newinsn = 0xd503201f;
7939 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7941 newinsn = 0xd2a00000 | (((x >> 16) & 0xffff) << 5);
7944 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7946 newinsn = 0xf2800000 | ((x & 0xffff) << 5);
7950 gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
7954 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7955 return aarch64_reloc_funcs::STATUS_OKAY;
7956 } // End of tls_desc_gd_to_le
7959 template<int size, bool big_endian>
7961 typename AArch64_relocate_functions<size, big_endian>::Status
7962 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_ie(
7963 const Relocate_info<size, big_endian>* /* relinfo */,
7964 Target_aarch64<size, big_endian>* /* target */,
7965 const elfcpp::Rela<size, big_endian>& rela,
7966 unsigned int r_type,
7967 unsigned char* view,
7968 const Symbol_value<size>* /* psymval */,
7969 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address,
7970 typename elfcpp::Elf_types<size>::Elf_Addr address)
7972 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7973 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7975 // TLSDESC-GD sequence is like:
7976 // adrp x0, :tlsdesc:v1
7977 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7978 // add x0, x0, :tlsdesc_lo12:v1
7981 // After desc_gd_to_ie optimization, the sequence will be like:
7982 // adrp x0, :tlsie:v1
7983 // ldr x0, [x0, :tlsie_lo12:v1]
7987 Insntype* ip = reinterpret_cast<Insntype*>(view);
7988 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7992 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7993 case elfcpp::R_AARCH64_TLSDESC_CALL:
7995 newinsn = 0xd503201f;
7996 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7999 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
8001 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
8006 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
8008 // Set ldr target register to be x0.
8009 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
8011 elfcpp::Swap<32, big_endian>::writeval(ip, insn);
8013 const AArch64_reloc_property* reloc_property =
8014 aarch64_reloc_property_table->get_reloc_property(
8015 elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
8016 return aarch64_reloc_funcs::template rela_general<32>(
8017 view, got_entry_address, addend, reloc_property);
8022 gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
8026 return aarch64_reloc_funcs::STATUS_OKAY;
8027 } // End of tls_desc_gd_to_ie
8029 // Relocate section data.
8031 template<int size, bool big_endian>
8033 Target_aarch64<size, big_endian>::relocate_section(
8034 const Relocate_info<size, big_endian>* relinfo,
8035 unsigned int sh_type,
8036 const unsigned char* prelocs,
8038 Output_section* output_section,
8039 bool needs_special_offset_handling,
8040 unsigned char* view,
8041 typename elfcpp::Elf_types<size>::Elf_Addr address,
8042 section_size_type view_size,
8043 const Reloc_symbol_changes* reloc_symbol_changes)
8045 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
8046 typedef Target_aarch64<size, big_endian> Aarch64;
8047 typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
8048 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8051 gold_assert(sh_type == elfcpp::SHT_RELA);
8053 // See if we are relocating a relaxed input section. If so, the view
8054 // covers the whole output section and we need to adjust accordingly.
8055 if (needs_special_offset_handling)
8057 const Output_relaxed_input_section* poris =
8058 output_section->find_relaxed_input_section(relinfo->object,
8059 relinfo->data_shndx);
8062 Address section_address = poris->address();
8063 section_size_type section_size = poris->data_size();
8065 gold_assert((section_address >= address)
8066 && ((section_address + section_size)
8067 <= (address + view_size)));
8069 off_t offset = section_address - address;
8072 view_size = section_size;
8076 gold::relocate_section<size, big_endian, Aarch64, AArch64_relocate,
8077 gold::Default_comdat_behavior, Classify_reloc>(
8083 needs_special_offset_handling,
8087 reloc_symbol_changes);
8090 // Scan the relocs during a relocatable link.
8092 template<int size, bool big_endian>
8094 Target_aarch64<size, big_endian>::scan_relocatable_relocs(
8095 Symbol_table* symtab,
8097 Sized_relobj_file<size, big_endian>* object,
8098 unsigned int data_shndx,
8099 unsigned int sh_type,
8100 const unsigned char* prelocs,
8102 Output_section* output_section,
8103 bool needs_special_offset_handling,
8104 size_t local_symbol_count,
8105 const unsigned char* plocal_symbols,
8106 Relocatable_relocs* rr)
8108 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8110 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
8111 Scan_relocatable_relocs;
8113 gold_assert(sh_type == elfcpp::SHT_RELA);
8115 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
8123 needs_special_offset_handling,
8129 // Scan the relocs for --emit-relocs.
8131 template<int size, bool big_endian>
8133 Target_aarch64<size, big_endian>::emit_relocs_scan(
8134 Symbol_table* symtab,
8136 Sized_relobj_file<size, big_endian>* object,
8137 unsigned int data_shndx,
8138 unsigned int sh_type,
8139 const unsigned char* prelocs,
8141 Output_section* output_section,
8142 bool needs_special_offset_handling,
8143 size_t local_symbol_count,
8144 const unsigned char* plocal_syms,
8145 Relocatable_relocs* rr)
8147 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8149 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
8150 Emit_relocs_strategy;
8152 gold_assert(sh_type == elfcpp::SHT_RELA);
8154 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
8162 needs_special_offset_handling,
8168 // Relocate a section during a relocatable link.
8170 template<int size, bool big_endian>
8172 Target_aarch64<size, big_endian>::relocate_relocs(
8173 const Relocate_info<size, big_endian>* relinfo,
8174 unsigned int sh_type,
8175 const unsigned char* prelocs,
8177 Output_section* output_section,
8178 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
8179 unsigned char* view,
8180 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
8181 section_size_type view_size,
8182 unsigned char* reloc_view,
8183 section_size_type reloc_view_size)
8185 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8188 gold_assert(sh_type == elfcpp::SHT_RELA);
8190 gold::relocate_relocs<size, big_endian, Classify_reloc>(
8195 offset_in_output_section,
8204 // Return whether this is a 3-insn erratum sequence.
8206 template<int size, bool big_endian>
8208 Target_aarch64<size, big_endian>::is_erratum_843419_sequence(
8209 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
8210 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
8211 typename elfcpp::Swap<32,big_endian>::Valtype insn3)
8216 // The 2nd insn is a single register load or store; or register pair
8218 if (Insn_utilities::aarch64_mem_op_p(insn2, &rt1, &rt2, &pair, &load)
8219 && (!pair || (pair && !load)))
8221 // The 3rd insn is a load or store instruction from the "Load/store
8222 // register (unsigned immediate)" encoding class, using Rn as the
8223 // base address register.
8224 if (Insn_utilities::aarch64_ldst_uimm(insn3)
8225 && (Insn_utilities::aarch64_rn(insn3)
8226 == Insn_utilities::aarch64_rd(insn1)))
8233 // Return whether this is a 835769 sequence.
8234 // (Similarly implemented as in elfnn-aarch64.c.)
8236 template<int size, bool big_endian>
8238 Target_aarch64<size, big_endian>::is_erratum_835769_sequence(
8239 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
8240 typename elfcpp::Swap<32,big_endian>::Valtype insn2)
8250 if (Insn_utilities::aarch64_mlxl(insn2)
8251 && Insn_utilities::aarch64_mem_op_p (insn1, &rt, &rt2, &pair, &load))
8253 /* Any SIMD memory op is independent of the subsequent MLA
8254 by definition of the erratum. */
8255 if (Insn_utilities::aarch64_bit(insn1, 26))
8258 /* If not SIMD, check for integer memory ops and MLA relationship. */
8259 rn = Insn_utilities::aarch64_rn(insn2);
8260 ra = Insn_utilities::aarch64_ra(insn2);
8261 rm = Insn_utilities::aarch64_rm(insn2);
8263 /* If this is a load and there's a true(RAW) dependency, we are safe
8264 and this is not an erratum sequence. */
8266 (rt == rn || rt == rm || rt == ra
8267 || (pair && (rt2 == rn || rt2 == rm || rt2 == ra))))
8270 /* We conservatively put out stubs for all other cases (including
8279 // Helper method to create erratum stub for ST_E_843419 and ST_E_835769.
8281 template<int size, bool big_endian>
8283 Target_aarch64<size, big_endian>::create_erratum_stub(
8284 AArch64_relobj<size, big_endian>* relobj,
8286 section_size_type erratum_insn_offset,
8287 Address erratum_address,
8288 typename Insn_utilities::Insntype erratum_insn,
8290 unsigned int e843419_adrp_offset)
8292 gold_assert(erratum_type == ST_E_843419 || erratum_type == ST_E_835769);
8293 The_stub_table* stub_table = relobj->stub_table(shndx);
8294 gold_assert(stub_table != NULL);
8295 if (stub_table->find_erratum_stub(relobj,
8297 erratum_insn_offset) == NULL)
8299 const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
8300 The_erratum_stub* stub;
8301 if (erratum_type == ST_E_835769)
8302 stub = new The_erratum_stub(relobj, erratum_type, shndx,
8303 erratum_insn_offset);
8304 else if (erratum_type == ST_E_843419)
8305 stub = new E843419_stub<size, big_endian>(
8306 relobj, shndx, erratum_insn_offset, e843419_adrp_offset);
8309 stub->set_erratum_insn(erratum_insn);
8310 stub->set_erratum_address(erratum_address);
8311 // For erratum ST_E_843419 and ST_E_835769, the destination address is
8312 // always the next insn after erratum insn.
8313 stub->set_destination_address(erratum_address + BPI);
8314 stub_table->add_erratum_stub(stub);
8319 // Scan erratum for section SHNDX range [output_address + span_start,
8320 // output_address + span_end). Note here we do not share the code with
8321 // scan_erratum_843419_span function, because for 843419 we optimize by only
8322 // scanning the last few insns of a page, whereas for 835769, we need to scan
8325 template<int size, bool big_endian>
8327 Target_aarch64<size, big_endian>::scan_erratum_835769_span(
8328 AArch64_relobj<size, big_endian>* relobj,
8330 const section_size_type span_start,
8331 const section_size_type span_end,
8332 unsigned char* input_view,
8333 Address output_address)
8335 typedef typename Insn_utilities::Insntype Insntype;
8337 const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
8339 // Adjust output_address and view to the start of span.
8340 output_address += span_start;
8341 input_view += span_start;
8343 section_size_type span_length = span_end - span_start;
8344 section_size_type offset = 0;
8345 for (offset = 0; offset + BPI < span_length; offset += BPI)
8347 Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
8348 Insntype insn1 = ip[0];
8349 Insntype insn2 = ip[1];
8350 if (is_erratum_835769_sequence(insn1, insn2))
8352 Insntype erratum_insn = insn2;
8353 // "span_start + offset" is the offset for insn1. So for insn2, it is
8354 // "span_start + offset + BPI".
8355 section_size_type erratum_insn_offset = span_start + offset + BPI;
8356 Address erratum_address = output_address + offset + BPI;
8357 gold_info(_("Erratum 835769 found and fixed at \"%s\", "
8358 "section %d, offset 0x%08x."),
8359 relobj->name().c_str(), shndx,
8360 (unsigned int)(span_start + offset));
8362 this->create_erratum_stub(relobj, shndx,
8363 erratum_insn_offset, erratum_address,
8364 erratum_insn, ST_E_835769);
8365 offset += BPI; // Skip mac insn.
8368 } // End of "Target_aarch64::scan_erratum_835769_span".
8371 // Scan erratum for section SHNDX range
8372 // [output_address + span_start, output_address + span_end).
8374 template<int size, bool big_endian>
8376 Target_aarch64<size, big_endian>::scan_erratum_843419_span(
8377 AArch64_relobj<size, big_endian>* relobj,
8379 const section_size_type span_start,
8380 const section_size_type span_end,
8381 unsigned char* input_view,
8382 Address output_address)
8384 typedef typename Insn_utilities::Insntype Insntype;
8386 // Adjust output_address and view to the start of span.
8387 output_address += span_start;
8388 input_view += span_start;
8390 if ((output_address & 0x03) != 0)
8393 section_size_type offset = 0;
8394 section_size_type span_length = span_end - span_start;
8395 // The first instruction must be ending at 0xFF8 or 0xFFC.
8396 unsigned int page_offset = output_address & 0xFFF;
8397 // Make sure starting position, that is "output_address+offset",
8398 // starts at page position 0xff8 or 0xffc.
8399 if (page_offset < 0xff8)
8400 offset = 0xff8 - page_offset;
8401 while (offset + 3 * Insn_utilities::BYTES_PER_INSN <= span_length)
8403 Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
8404 Insntype insn1 = ip[0];
8405 if (Insn_utilities::is_adrp(insn1))
8407 Insntype insn2 = ip[1];
8408 Insntype insn3 = ip[2];
8409 Insntype erratum_insn;
8410 unsigned insn_offset;
8411 bool do_report = false;
8412 if (is_erratum_843419_sequence(insn1, insn2, insn3))
8415 erratum_insn = insn3;
8416 insn_offset = 2 * Insn_utilities::BYTES_PER_INSN;
8418 else if (offset + 4 * Insn_utilities::BYTES_PER_INSN <= span_length)
8420 // Optionally we can have an insn between ins2 and ins3
8421 Insntype insn_opt = ip[2];
8422 // And insn_opt must not be a branch.
8423 if (!Insn_utilities::aarch64_b(insn_opt)
8424 && !Insn_utilities::aarch64_bl(insn_opt)
8425 && !Insn_utilities::aarch64_blr(insn_opt)
8426 && !Insn_utilities::aarch64_br(insn_opt))
8428 // And insn_opt must not write to dest reg in insn1. However
8429 // we do a conservative scan, which means we may fix/report
8430 // more than necessary, but it doesn't hurt.
8432 Insntype insn4 = ip[3];
8433 if (is_erratum_843419_sequence(insn1, insn2, insn4))
8436 erratum_insn = insn4;
8437 insn_offset = 3 * Insn_utilities::BYTES_PER_INSN;
8443 unsigned int erratum_insn_offset =
8444 span_start + offset + insn_offset;
8445 Address erratum_address =
8446 output_address + offset + insn_offset;
8447 create_erratum_stub(relobj, shndx,
8448 erratum_insn_offset, erratum_address,
8449 erratum_insn, ST_E_843419,
8450 span_start + offset);
8454 // Advance to next candidate instruction. We only consider instruction
8455 // sequences starting at a page offset of 0xff8 or 0xffc.
8456 page_offset = (output_address + offset) & 0xfff;
8457 if (page_offset == 0xff8)
8459 else // (page_offset == 0xffc), we move to next page's 0xff8.
8462 } // End of "Target_aarch64::scan_erratum_843419_span".
8465 // The selector for aarch64 object files.
8467 template<int size, bool big_endian>
8468 class Target_selector_aarch64 : public Target_selector
8471 Target_selector_aarch64();
8474 do_instantiate_target()
8475 { return new Target_aarch64<size, big_endian>(); }
8479 Target_selector_aarch64<32, true>::Target_selector_aarch64()
8480 : Target_selector(elfcpp::EM_AARCH64, 32, true,
8481 "elf32-bigaarch64", "aarch64_elf32_be_vec")
8485 Target_selector_aarch64<32, false>::Target_selector_aarch64()
8486 : Target_selector(elfcpp::EM_AARCH64, 32, false,
8487 "elf32-littleaarch64", "aarch64_elf32_le_vec")
8491 Target_selector_aarch64<64, true>::Target_selector_aarch64()
8492 : Target_selector(elfcpp::EM_AARCH64, 64, true,
8493 "elf64-bigaarch64", "aarch64_elf64_be_vec")
8497 Target_selector_aarch64<64, false>::Target_selector_aarch64()
8498 : Target_selector(elfcpp::EM_AARCH64, 64, false,
8499 "elf64-littleaarch64", "aarch64_elf64_le_vec")
8502 Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
8503 Target_selector_aarch64<32, false> target_selector_aarch64elf32;
8504 Target_selector_aarch64<64, true> target_selector_aarch64elfb;
8505 Target_selector_aarch64<64, false> target_selector_aarch64elf;
8507 } // End anonymous namespace.