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(
1867 The_erratum_stub*, AArch64_address,
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 const Relobj::Output_sections& out_sections(this->output_sections());
1984 for (unsigned int i = 1; i < shnum; ++i)
1986 The_stub_table* stub_table = this->stub_table(i);
1989 std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
1990 ipair(stub_table->find_erratum_stubs_for_input_section(this, i));
1991 Erratum_stub_set_iter p = ipair.first, end = ipair.second;
1992 typename Sized_relobj_file<size, big_endian>::View_size&
1993 pview((*pviews)[i]);
1994 AArch64_address view_offset = 0;
1995 if (pview.is_input_output_view)
1997 // In this case, write_sections has not added the output offset to
1998 // the view's address, so we must do so. Currently this only happens
1999 // for a relaxed section.
2000 unsigned int index = this->adjust_shndx(i);
2001 const Output_relaxed_input_section* poris =
2002 out_sections[index]->find_relaxed_input_section(this, index);
2003 gold_assert(poris != NULL);
2004 view_offset = poris->address() - pview.address;
2009 The_erratum_stub* stub = *p;
2011 // Double check data before fix.
2012 gold_assert(pview.address + view_offset + stub->sh_offset()
2013 == stub->erratum_address());
2015 // Update previously recorded erratum insn with relocated
2018 reinterpret_cast<Insntype*>(
2019 pview.view + view_offset + stub->sh_offset());
2020 Insntype insn_to_fix = ip[0];
2021 stub->update_erratum_insn(insn_to_fix);
2023 // First try to see if erratum is 843419 and if it can be fixed
2024 // without using branch-to-stub.
2025 if (!try_fix_erratum_843419_optimized(stub, view_offset, pview))
2027 // Replace the erratum insn with a branch-to-stub.
2028 AArch64_address stub_address =
2029 stub_table->erratum_stub_address(stub);
2030 unsigned int b_offset = stub_address - stub->erratum_address();
2031 AArch64_relocate_functions<size, big_endian>::construct_b(
2032 pview.view + view_offset + stub->sh_offset(),
2033 b_offset & 0xfffffff);
2036 // Erratum fix is done (or skipped), continue to relocate erratum
2037 // stub. Note, when erratum fix is skipped (either because we
2038 // proactively change the code sequence or the code sequence is
2039 // changed by relaxation, etc), we can still safely relocate the
2040 // erratum stub, ignoring the fact the erratum could never be
2042 stub_table->relocate_erratum_stub(
2044 pview.view + view_offset + (stub_table->address() - pview.address));
2046 // Next erratum stub.
2053 // This is an optimization for 843419. This erratum requires the sequence begin
2054 // with 'adrp', when final value calculated by adrp fits in adr, we can just
2055 // replace 'adrp' with 'adr', so we save 2 jumps per occurrence. (Note, however,
2056 // in this case, we do not delete the erratum stub (too late to do so), it is
2057 // merely generated without ever being called.)
2059 template<int size, bool big_endian>
2061 AArch64_relobj<size, big_endian>::try_fix_erratum_843419_optimized(
2062 The_erratum_stub* stub, AArch64_address view_offset,
2063 typename Sized_relobj_file<size, big_endian>::View_size& pview)
2065 if (stub->type() != ST_E_843419)
2068 typedef AArch64_insn_utilities<big_endian> Insn_utilities;
2069 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
2070 E843419_stub<size, big_endian>* e843419_stub =
2071 reinterpret_cast<E843419_stub<size, big_endian>*>(stub);
2072 AArch64_address pc =
2073 pview.address + view_offset + e843419_stub->adrp_sh_offset();
2074 unsigned int adrp_offset = e843419_stub->adrp_sh_offset ();
2075 Insntype* adrp_view =
2076 reinterpret_cast<Insntype*>(pview.view + view_offset + adrp_offset);
2077 Insntype adrp_insn = adrp_view[0];
2079 // If the instruction at adrp_sh_offset is "mrs R, tpidr_el0", it may come
2080 // from IE -> LE relaxation etc. This is a side-effect of TLS relaxation that
2081 // ADRP has been turned into MRS, there is no erratum risk anymore.
2082 // Therefore, we return true to avoid doing unnecessary branch-to-stub.
2083 if (Insn_utilities::is_mrs_tpidr_el0(adrp_insn))
2086 // If the instruction at adrp_sh_offset is not ADRP and the instruction before
2087 // it is "mrs R, tpidr_el0", it may come from LD -> LE relaxation etc.
2088 // Like the above case, there is no erratum risk any more, we can safely
2090 if (!Insn_utilities::is_adrp(adrp_insn) && adrp_offset)
2092 Insntype* prev_view =
2093 reinterpret_cast<Insntype*>(
2094 pview.view + view_offset + adrp_offset - 4);
2095 Insntype prev_insn = prev_view[0];
2097 if (Insn_utilities::is_mrs_tpidr_el0(prev_insn))
2101 /* If we reach here, the first instruction must be ADRP. */
2102 gold_assert(Insn_utilities::is_adrp(adrp_insn));
2103 // Get adrp 33-bit signed imm value.
2104 int64_t adrp_imm = Insn_utilities::
2105 aarch64_adrp_decode_imm(adrp_insn);
2106 // adrp - final value transferred to target register is calculated as:
2107 // PC[11:0] = Zeros(12)
2108 // adrp_dest_value = PC + adrp_imm;
2109 int64_t adrp_dest_value = (pc & ~((1 << 12) - 1)) + adrp_imm;
2110 // adr -final value transferred to target register is calucalted as:
2113 // PC + adr_imm = adrp_dest_value
2115 // adr_imm = adrp_dest_value - PC
2116 int64_t adr_imm = adrp_dest_value - pc;
2117 // Check if imm fits in adr (21-bit signed).
2118 if (-(1 << 20) <= adr_imm && adr_imm < (1 << 20))
2120 // Convert 'adrp' into 'adr'.
2121 Insntype adr_insn = adrp_insn & ((1u << 31) - 1);
2122 adr_insn = Insn_utilities::
2123 aarch64_adr_encode_imm(adr_insn, adr_imm);
2124 elfcpp::Swap<32, big_endian>::writeval(adrp_view, adr_insn);
2131 // Relocate sections.
2133 template<int size, bool big_endian>
2135 AArch64_relobj<size, big_endian>::do_relocate_sections(
2136 const Symbol_table* symtab, const Layout* layout,
2137 const unsigned char* pshdrs, Output_file* of,
2138 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2140 // Relocate the section data.
2141 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2142 1, this->shnum() - 1);
2144 // We do not generate stubs if doing a relocatable link.
2145 if (parameters->options().relocatable())
2148 // This part only relocates erratum stubs that belong to input sections of this
2150 if (parameters->options().fix_cortex_a53_843419()
2151 || parameters->options().fix_cortex_a53_835769())
2152 this->fix_errata_and_relocate_erratum_stubs(pviews);
2154 Relocate_info<size, big_endian> relinfo;
2155 relinfo.symtab = symtab;
2156 relinfo.layout = layout;
2157 relinfo.object = this;
2159 // This part relocates all reloc stubs that are contained in stub_tables of
2160 // this object file.
2161 unsigned int shnum = this->shnum();
2162 The_target_aarch64* target = The_target_aarch64::current_target();
2164 for (unsigned int i = 1; i < shnum; ++i)
2166 The_aarch64_input_section* aarch64_input_section =
2167 target->find_aarch64_input_section(this, i);
2168 if (aarch64_input_section != NULL
2169 && aarch64_input_section->is_stub_table_owner()
2170 && !aarch64_input_section->stub_table()->empty())
2172 Output_section* os = this->output_section(i);
2173 gold_assert(os != NULL);
2175 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
2176 relinfo.reloc_shdr = NULL;
2177 relinfo.data_shndx = i;
2178 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<size>::shdr_size;
2180 typename Sized_relobj_file<size, big_endian>::View_size&
2181 view_struct = (*pviews)[i];
2182 gold_assert(view_struct.view != NULL);
2184 The_stub_table* stub_table = aarch64_input_section->stub_table();
2185 off_t offset = stub_table->address() - view_struct.address;
2186 unsigned char* view = view_struct.view + offset;
2187 AArch64_address address = stub_table->address();
2188 section_size_type view_size = stub_table->data_size();
2189 stub_table->relocate_reloc_stubs(&relinfo, target, os, view, address,
2196 // Determine if an input section is scannable for stub processing. SHDR is
2197 // the header of the section and SHNDX is the section index. OS is the output
2198 // section for the input section and SYMTAB is the global symbol table used to
2199 // look up ICF information.
2201 template<int size, bool big_endian>
2203 AArch64_relobj<size, big_endian>::text_section_is_scannable(
2204 const elfcpp::Shdr<size, big_endian>& text_shdr,
2205 unsigned int text_shndx,
2206 const Output_section* os,
2207 const Symbol_table* symtab)
2209 // Skip any empty sections, unallocated sections or sections whose
2210 // type are not SHT_PROGBITS.
2211 if (text_shdr.get_sh_size() == 0
2212 || (text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
2213 || text_shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
2216 // Skip any discarded or ICF'ed sections.
2217 if (os == NULL || symtab->is_section_folded(this, text_shndx))
2220 // Skip exception frame.
2221 if (strcmp(os->name(), ".eh_frame") == 0)
2224 gold_assert(!this->is_output_section_offset_invalid(text_shndx) ||
2225 os->find_relaxed_input_section(this, text_shndx) != NULL);
2231 // Determine if we want to scan the SHNDX-th section for relocation stubs.
2232 // This is a helper for AArch64_relobj::scan_sections_for_stubs().
2234 template<int size, bool big_endian>
2236 AArch64_relobj<size, big_endian>::section_needs_reloc_stub_scanning(
2237 const elfcpp::Shdr<size, big_endian>& shdr,
2238 const Relobj::Output_sections& out_sections,
2239 const Symbol_table* symtab,
2240 const unsigned char* pshdrs)
2242 unsigned int sh_type = shdr.get_sh_type();
2243 if (sh_type != elfcpp::SHT_RELA)
2246 // Ignore empty section.
2247 off_t sh_size = shdr.get_sh_size();
2251 // Ignore reloc section with unexpected symbol table. The
2252 // error will be reported in the final link.
2253 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
2256 gold_assert(sh_type == elfcpp::SHT_RELA);
2257 unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2259 // Ignore reloc section with unexpected entsize or uneven size.
2260 // The error will be reported in the final link.
2261 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
2264 // Ignore reloc section with bad info. This error will be
2265 // reported in the final link.
2266 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_info());
2267 if (text_shndx >= this->shnum())
2270 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2271 const elfcpp::Shdr<size, big_endian> text_shdr(pshdrs +
2272 text_shndx * shdr_size);
2273 return this->text_section_is_scannable(text_shdr, text_shndx,
2274 out_sections[text_shndx], symtab);
2278 // Scan section SHNDX for erratum 843419 and 835769.
2280 template<int size, bool big_endian>
2282 AArch64_relobj<size, big_endian>::scan_errata(
2283 unsigned int shndx, const elfcpp::Shdr<size, big_endian>& shdr,
2284 Output_section* os, const Symbol_table* symtab,
2285 The_target_aarch64* target)
2287 if (shdr.get_sh_size() == 0
2288 || (shdr.get_sh_flags() &
2289 (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) == 0
2290 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
2293 if (!os || symtab->is_section_folded(this, shndx)) return;
2295 AArch64_address output_offset = this->get_output_section_offset(shndx);
2296 AArch64_address output_address;
2297 if (output_offset != invalid_address)
2298 output_address = os->address() + output_offset;
2301 const Output_relaxed_input_section* poris =
2302 os->find_relaxed_input_section(this, shndx);
2304 output_address = poris->address();
2307 section_size_type input_view_size = 0;
2308 const unsigned char* input_view =
2309 this->section_contents(shndx, &input_view_size, false);
2311 Mapping_symbol_position section_start(shndx, 0);
2312 // Find the first mapping symbol record within section shndx.
2313 typename Mapping_symbol_info::const_iterator p =
2314 this->mapping_symbol_info_.lower_bound(section_start);
2315 while (p != this->mapping_symbol_info_.end() &&
2316 p->first.shndx_ == shndx)
2318 typename Mapping_symbol_info::const_iterator prev = p;
2320 if (prev->second == 'x')
2322 section_size_type span_start =
2323 convert_to_section_size_type(prev->first.offset_);
2324 section_size_type span_end;
2325 if (p != this->mapping_symbol_info_.end()
2326 && p->first.shndx_ == shndx)
2327 span_end = convert_to_section_size_type(p->first.offset_);
2329 span_end = convert_to_section_size_type(shdr.get_sh_size());
2331 // Here we do not share the scanning code of both errata. For 843419,
2332 // only the last few insns of each page are examined, which is fast,
2333 // whereas, for 835769, every insn pair needs to be checked.
2335 if (parameters->options().fix_cortex_a53_843419())
2336 target->scan_erratum_843419_span(
2337 this, shndx, span_start, span_end,
2338 const_cast<unsigned char*>(input_view), output_address);
2340 if (parameters->options().fix_cortex_a53_835769())
2341 target->scan_erratum_835769_span(
2342 this, shndx, span_start, span_end,
2343 const_cast<unsigned char*>(input_view), output_address);
2349 // Scan relocations for stub generation.
2351 template<int size, bool big_endian>
2353 AArch64_relobj<size, big_endian>::scan_sections_for_stubs(
2354 The_target_aarch64* target,
2355 const Symbol_table* symtab,
2356 const Layout* layout)
2358 unsigned int shnum = this->shnum();
2359 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2361 // Read the section headers.
2362 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
2366 // To speed up processing, we set up hash tables for fast lookup of
2367 // input offsets to output addresses.
2368 this->initialize_input_to_output_maps();
2370 const Relobj::Output_sections& out_sections(this->output_sections());
2372 Relocate_info<size, big_endian> relinfo;
2373 relinfo.symtab = symtab;
2374 relinfo.layout = layout;
2375 relinfo.object = this;
2377 // Do relocation stubs scanning.
2378 const unsigned char* p = pshdrs + shdr_size;
2379 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
2381 const elfcpp::Shdr<size, big_endian> shdr(p);
2382 if (parameters->options().fix_cortex_a53_843419()
2383 || parameters->options().fix_cortex_a53_835769())
2384 scan_errata(i, shdr, out_sections[i], symtab, target);
2385 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
2388 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
2389 AArch64_address output_offset =
2390 this->get_output_section_offset(index);
2391 AArch64_address output_address;
2392 if (output_offset != invalid_address)
2394 output_address = out_sections[index]->address() + output_offset;
2398 // Currently this only happens for a relaxed section.
2399 const Output_relaxed_input_section* poris =
2400 out_sections[index]->find_relaxed_input_section(this, index);
2401 gold_assert(poris != NULL);
2402 output_address = poris->address();
2405 // Get the relocations.
2406 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
2410 // Get the section contents.
2411 section_size_type input_view_size = 0;
2412 const unsigned char* input_view =
2413 this->section_contents(index, &input_view_size, false);
2415 relinfo.reloc_shndx = i;
2416 relinfo.data_shndx = index;
2417 unsigned int sh_type = shdr.get_sh_type();
2418 unsigned int reloc_size;
2419 gold_assert (sh_type == elfcpp::SHT_RELA);
2420 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2422 Output_section* os = out_sections[index];
2423 target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
2424 shdr.get_sh_size() / reloc_size,
2426 output_offset == invalid_address,
2427 input_view, output_address,
2434 // A class to wrap an ordinary input section containing executable code.
2436 template<int size, bool big_endian>
2437 class AArch64_input_section : public Output_relaxed_input_section
2440 typedef Stub_table<size, big_endian> The_stub_table;
2442 AArch64_input_section(Relobj* relobj, unsigned int shndx)
2443 : Output_relaxed_input_section(relobj, shndx, 1),
2445 original_contents_(NULL), original_size_(0),
2446 original_addralign_(1)
2449 ~AArch64_input_section()
2450 { delete[] this->original_contents_; }
2456 // Set the stub_table.
2458 set_stub_table(The_stub_table* st)
2459 { this->stub_table_ = st; }
2461 // Whether this is a stub table owner.
2463 is_stub_table_owner() const
2464 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
2466 // Return the original size of the section.
2468 original_size() const
2469 { return this->original_size_; }
2471 // Return the stub table.
2474 { return stub_table_; }
2477 // Write out this input section.
2479 do_write(Output_file*);
2481 // Return required alignment of this.
2483 do_addralign() const
2485 if (this->is_stub_table_owner())
2486 return std::max(this->stub_table_->addralign(),
2487 static_cast<uint64_t>(this->original_addralign_));
2489 return this->original_addralign_;
2492 // Finalize data size.
2494 set_final_data_size();
2496 // Reset address and file offset.
2498 do_reset_address_and_file_offset();
2502 do_output_offset(const Relobj* object, unsigned int shndx,
2503 section_offset_type offset,
2504 section_offset_type* poutput) const
2506 if ((object == this->relobj())
2507 && (shndx == this->shndx())
2510 convert_types<section_offset_type, uint32_t>(this->original_size_)))
2520 // Copying is not allowed.
2521 AArch64_input_section(const AArch64_input_section&);
2522 AArch64_input_section& operator=(const AArch64_input_section&);
2524 // The relocation stubs.
2525 The_stub_table* stub_table_;
2526 // Original section contents. We have to make a copy here since the file
2527 // containing the original section may not be locked when we need to access
2529 unsigned char* original_contents_;
2530 // Section size of the original input section.
2531 uint32_t original_size_;
2532 // Address alignment of the original input section.
2533 uint32_t original_addralign_;
2534 }; // End of AArch64_input_section
2537 // Finalize data size.
2539 template<int size, bool big_endian>
2541 AArch64_input_section<size, big_endian>::set_final_data_size()
2543 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2545 if (this->is_stub_table_owner())
2547 this->stub_table_->finalize_data_size();
2548 off = align_address(off, this->stub_table_->addralign());
2549 off += this->stub_table_->data_size();
2551 this->set_data_size(off);
2555 // Reset address and file offset.
2557 template<int size, bool big_endian>
2559 AArch64_input_section<size, big_endian>::do_reset_address_and_file_offset()
2561 // Size of the original input section contents.
2562 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2564 // If this is a stub table owner, account for the stub table size.
2565 if (this->is_stub_table_owner())
2567 The_stub_table* stub_table = this->stub_table_;
2569 // Reset the stub table's address and file offset. The
2570 // current data size for child will be updated after that.
2571 stub_table_->reset_address_and_file_offset();
2572 off = align_address(off, stub_table_->addralign());
2573 off += stub_table->current_data_size();
2576 this->set_current_data_size(off);
2580 // Initialize an Arm_input_section.
2582 template<int size, bool big_endian>
2584 AArch64_input_section<size, big_endian>::init()
2586 Relobj* relobj = this->relobj();
2587 unsigned int shndx = this->shndx();
2589 // We have to cache original size, alignment and contents to avoid locking
2590 // the original file.
2591 this->original_addralign_ =
2592 convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
2594 // This is not efficient but we expect only a small number of relaxed
2595 // input sections for stubs.
2596 section_size_type section_size;
2597 const unsigned char* section_contents =
2598 relobj->section_contents(shndx, §ion_size, false);
2599 this->original_size_ =
2600 convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
2602 gold_assert(this->original_contents_ == NULL);
2603 this->original_contents_ = new unsigned char[section_size];
2604 memcpy(this->original_contents_, section_contents, section_size);
2606 // We want to make this look like the original input section after
2607 // output sections are finalized.
2608 Output_section* os = relobj->output_section(shndx);
2609 off_t offset = relobj->output_section_offset(shndx);
2610 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2611 this->set_address(os->address() + offset);
2612 this->set_file_offset(os->offset() + offset);
2613 this->set_current_data_size(this->original_size_);
2614 this->finalize_data_size();
2618 // Write data to output file.
2620 template<int size, bool big_endian>
2622 AArch64_input_section<size, big_endian>::do_write(Output_file* of)
2624 // We have to write out the original section content.
2625 gold_assert(this->original_contents_ != NULL);
2626 of->write(this->offset(), this->original_contents_,
2627 this->original_size_);
2629 // If this owns a stub table and it is not empty, write it.
2630 if (this->is_stub_table_owner() && !this->stub_table_->empty())
2631 this->stub_table_->write(of);
2635 // Arm output section class. This is defined mainly to add a number of stub
2636 // generation methods.
2638 template<int size, bool big_endian>
2639 class AArch64_output_section : public Output_section
2642 typedef Target_aarch64<size, big_endian> The_target_aarch64;
2643 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2644 typedef Stub_table<size, big_endian> The_stub_table;
2645 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2648 AArch64_output_section(const char* name, elfcpp::Elf_Word type,
2649 elfcpp::Elf_Xword flags)
2650 : Output_section(name, type, flags)
2653 ~AArch64_output_section() {}
2655 // Group input sections for stub generation.
2657 group_sections(section_size_type, bool, Target_aarch64<size, big_endian>*,
2661 typedef Output_section::Input_section Input_section;
2662 typedef Output_section::Input_section_list Input_section_list;
2664 // Create a stub group.
2666 create_stub_group(Input_section_list::const_iterator,
2667 Input_section_list::const_iterator,
2668 Input_section_list::const_iterator,
2669 The_target_aarch64*,
2670 std::vector<Output_relaxed_input_section*>&,
2672 }; // End of AArch64_output_section
2675 // Create a stub group for input sections from FIRST to LAST. OWNER points to
2676 // the input section that will be the owner of the stub table.
2678 template<int size, bool big_endian> void
2679 AArch64_output_section<size, big_endian>::create_stub_group(
2680 Input_section_list::const_iterator first,
2681 Input_section_list::const_iterator last,
2682 Input_section_list::const_iterator owner,
2683 The_target_aarch64* target,
2684 std::vector<Output_relaxed_input_section*>& new_relaxed_sections,
2687 // Currently we convert ordinary input sections into relaxed sections only
2689 The_aarch64_input_section* input_section;
2690 if (owner->is_relaxed_input_section())
2694 gold_assert(owner->is_input_section());
2695 // Create a new relaxed input section. We need to lock the original
2697 Task_lock_obj<Object> tl(task, owner->relobj());
2699 target->new_aarch64_input_section(owner->relobj(), owner->shndx());
2700 new_relaxed_sections.push_back(input_section);
2703 // Create a stub table.
2704 The_stub_table* stub_table =
2705 target->new_stub_table(input_section);
2707 input_section->set_stub_table(stub_table);
2709 Input_section_list::const_iterator p = first;
2710 // Look for input sections or relaxed input sections in [first ... last].
2713 if (p->is_input_section() || p->is_relaxed_input_section())
2715 // The stub table information for input sections live
2716 // in their objects.
2717 The_aarch64_relobj* aarch64_relobj =
2718 static_cast<The_aarch64_relobj*>(p->relobj());
2719 aarch64_relobj->set_stub_table(p->shndx(), stub_table);
2722 while (p++ != last);
2726 // Group input sections for stub generation. GROUP_SIZE is roughly the limit of
2727 // stub groups. We grow a stub group by adding input section until the size is
2728 // just below GROUP_SIZE. The last input section will be converted into a stub
2729 // table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
2730 // after the stub table, effectively doubling the group size.
2732 // This is similar to the group_sections() function in elf32-arm.c but is
2733 // implemented differently.
2735 template<int size, bool big_endian>
2736 void AArch64_output_section<size, big_endian>::group_sections(
2737 section_size_type group_size,
2738 bool stubs_always_after_branch,
2739 Target_aarch64<size, big_endian>* target,
2745 FINDING_STUB_SECTION,
2749 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
2751 State state = NO_GROUP;
2752 section_size_type off = 0;
2753 section_size_type group_begin_offset = 0;
2754 section_size_type group_end_offset = 0;
2755 section_size_type stub_table_end_offset = 0;
2756 Input_section_list::const_iterator group_begin =
2757 this->input_sections().end();
2758 Input_section_list::const_iterator stub_table =
2759 this->input_sections().end();
2760 Input_section_list::const_iterator group_end = this->input_sections().end();
2761 for (Input_section_list::const_iterator p = this->input_sections().begin();
2762 p != this->input_sections().end();
2765 section_size_type section_begin_offset =
2766 align_address(off, p->addralign());
2767 section_size_type section_end_offset =
2768 section_begin_offset + p->data_size();
2770 // Check to see if we should group the previously seen sections.
2776 case FINDING_STUB_SECTION:
2777 // Adding this section makes the group larger than GROUP_SIZE.
2778 if (section_end_offset - group_begin_offset >= group_size)
2780 if (stubs_always_after_branch)
2782 gold_assert(group_end != this->input_sections().end());
2783 this->create_stub_group(group_begin, group_end, group_end,
2784 target, new_relaxed_sections,
2790 // Input sections up to stub_group_size bytes after the stub
2791 // table can be handled by it too.
2792 state = HAS_STUB_SECTION;
2793 stub_table = group_end;
2794 stub_table_end_offset = group_end_offset;
2799 case HAS_STUB_SECTION:
2800 // Adding this section makes the post stub-section group larger
2803 // NOT SUPPORTED YET. For completeness only.
2804 if (section_end_offset - stub_table_end_offset >= group_size)
2806 gold_assert(group_end != this->input_sections().end());
2807 this->create_stub_group(group_begin, group_end, stub_table,
2808 target, new_relaxed_sections, task);
2817 // If we see an input section and currently there is no group, start
2818 // a new one. Skip any empty sections. We look at the data size
2819 // instead of calling p->relobj()->section_size() to avoid locking.
2820 if ((p->is_input_section() || p->is_relaxed_input_section())
2821 && (p->data_size() != 0))
2823 if (state == NO_GROUP)
2825 state = FINDING_STUB_SECTION;
2827 group_begin_offset = section_begin_offset;
2830 // Keep track of the last input section seen.
2832 group_end_offset = section_end_offset;
2835 off = section_end_offset;
2838 // Create a stub group for any ungrouped sections.
2839 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
2841 gold_assert(group_end != this->input_sections().end());
2842 this->create_stub_group(group_begin, group_end,
2843 (state == FINDING_STUB_SECTION
2846 target, new_relaxed_sections, task);
2849 if (!new_relaxed_sections.empty())
2850 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
2852 // Update the section offsets
2853 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
2855 The_aarch64_relobj* relobj = static_cast<The_aarch64_relobj*>(
2856 new_relaxed_sections[i]->relobj());
2857 unsigned int shndx = new_relaxed_sections[i]->shndx();
2858 // Tell AArch64_relobj that this input section is converted.
2859 relobj->convert_input_section_to_relaxed_section(shndx);
2861 } // End of AArch64_output_section::group_sections
2864 AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
2867 // The aarch64 target class.
2869 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
2870 template<int size, bool big_endian>
2871 class Target_aarch64 : public Sized_target<size, big_endian>
2874 typedef Target_aarch64<size, big_endian> This;
2875 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
2877 typedef Relocate_info<size, big_endian> The_relocate_info;
2878 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2879 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2880 typedef Reloc_stub<size, big_endian> The_reloc_stub;
2881 typedef Erratum_stub<size, big_endian> The_erratum_stub;
2882 typedef typename Reloc_stub<size, big_endian>::Key The_reloc_stub_key;
2883 typedef Stub_table<size, big_endian> The_stub_table;
2884 typedef std::vector<The_stub_table*> Stub_table_list;
2885 typedef typename Stub_table_list::iterator Stub_table_iterator;
2886 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2887 typedef AArch64_output_section<size, big_endian> The_aarch64_output_section;
2888 typedef Unordered_map<Section_id,
2889 AArch64_input_section<size, big_endian>*,
2890 Section_id_hash> AArch64_input_section_map;
2891 typedef AArch64_insn_utilities<big_endian> Insn_utilities;
2892 const static int TCB_SIZE = size / 8 * 2;
2894 Target_aarch64(const Target::Target_info* info = &aarch64_info)
2895 : Sized_target<size, big_endian>(info),
2896 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
2897 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
2898 rela_irelative_(NULL), copy_relocs_(elfcpp::R_AARCH64_COPY),
2899 got_mod_index_offset_(-1U),
2900 tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
2901 stub_tables_(), stub_group_size_(0), aarch64_input_section_map_()
2904 // Scan the relocations to determine unreferenced sections for
2905 // garbage collection.
2907 gc_process_relocs(Symbol_table* symtab,
2909 Sized_relobj_file<size, big_endian>* object,
2910 unsigned int data_shndx,
2911 unsigned int sh_type,
2912 const unsigned char* prelocs,
2914 Output_section* output_section,
2915 bool needs_special_offset_handling,
2916 size_t local_symbol_count,
2917 const unsigned char* plocal_symbols);
2919 // Scan the relocations to look for symbol adjustments.
2921 scan_relocs(Symbol_table* symtab,
2923 Sized_relobj_file<size, big_endian>* object,
2924 unsigned int data_shndx,
2925 unsigned int sh_type,
2926 const unsigned char* prelocs,
2928 Output_section* output_section,
2929 bool needs_special_offset_handling,
2930 size_t local_symbol_count,
2931 const unsigned char* plocal_symbols);
2933 // Finalize the sections.
2935 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2937 // Return the value to use for a dynamic which requires special
2940 do_dynsym_value(const Symbol*) const;
2942 // Relocate a section.
2944 relocate_section(const Relocate_info<size, big_endian>*,
2945 unsigned int sh_type,
2946 const unsigned char* prelocs,
2948 Output_section* output_section,
2949 bool needs_special_offset_handling,
2950 unsigned char* view,
2951 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2952 section_size_type view_size,
2953 const Reloc_symbol_changes*);
2955 // Scan the relocs during a relocatable link.
2957 scan_relocatable_relocs(Symbol_table* symtab,
2959 Sized_relobj_file<size, big_endian>* object,
2960 unsigned int data_shndx,
2961 unsigned int sh_type,
2962 const unsigned char* prelocs,
2964 Output_section* output_section,
2965 bool needs_special_offset_handling,
2966 size_t local_symbol_count,
2967 const unsigned char* plocal_symbols,
2968 Relocatable_relocs*);
2970 // Scan the relocs for --emit-relocs.
2972 emit_relocs_scan(Symbol_table* symtab,
2974 Sized_relobj_file<size, big_endian>* object,
2975 unsigned int data_shndx,
2976 unsigned int sh_type,
2977 const unsigned char* prelocs,
2979 Output_section* output_section,
2980 bool needs_special_offset_handling,
2981 size_t local_symbol_count,
2982 const unsigned char* plocal_syms,
2983 Relocatable_relocs* rr);
2985 // Relocate a section during a relocatable link.
2988 const Relocate_info<size, big_endian>*,
2989 unsigned int sh_type,
2990 const unsigned char* prelocs,
2992 Output_section* output_section,
2993 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
2994 unsigned char* view,
2995 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2996 section_size_type view_size,
2997 unsigned char* reloc_view,
2998 section_size_type reloc_view_size);
3000 // Return the symbol index to use for a target specific relocation.
3001 // The only target specific relocation is R_AARCH64_TLSDESC for a
3002 // local symbol, which is an absolute reloc.
3004 do_reloc_symbol_index(void*, unsigned int r_type) const
3006 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
3010 // Return the addend to use for a target specific relocation.
3012 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
3014 // Return the PLT section.
3016 do_plt_address_for_global(const Symbol* gsym) const
3017 { return this->plt_section()->address_for_global(gsym); }
3020 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
3021 { return this->plt_section()->address_for_local(relobj, symndx); }
3023 // This function should be defined in targets that can use relocation
3024 // types to determine (implemented in local_reloc_may_be_function_pointer
3025 // and global_reloc_may_be_function_pointer)
3026 // if a function's pointer is taken. ICF uses this in safe mode to only
3027 // fold those functions whose pointer is defintely not taken.
3029 do_can_check_for_function_pointers() const
3032 // Return the number of entries in the PLT.
3034 plt_entry_count() const;
3036 //Return the offset of the first non-reserved PLT entry.
3038 first_plt_entry_offset() const;
3040 // Return the size of each PLT entry.
3042 plt_entry_size() const;
3044 // Create a stub table.
3046 new_stub_table(The_aarch64_input_section*);
3048 // Create an aarch64 input section.
3049 The_aarch64_input_section*
3050 new_aarch64_input_section(Relobj*, unsigned int);
3052 // Find an aarch64 input section instance for a given OBJ and SHNDX.
3053 The_aarch64_input_section*
3054 find_aarch64_input_section(Relobj*, unsigned int) const;
3056 // Return the thread control block size.
3058 tcb_size() const { return This::TCB_SIZE; }
3060 // Scan a section for stub generation.
3062 scan_section_for_stubs(const Relocate_info<size, big_endian>*, unsigned int,
3063 const unsigned char*, size_t, Output_section*,
3064 bool, const unsigned char*,
3068 // Scan a relocation section for stub.
3069 template<int sh_type>
3071 scan_reloc_section_for_stubs(
3072 const The_relocate_info* relinfo,
3073 const unsigned char* prelocs,
3075 Output_section* output_section,
3076 bool needs_special_offset_handling,
3077 const unsigned char* view,
3078 Address view_address,
3081 // Relocate a single reloc stub.
3083 relocate_reloc_stub(The_reloc_stub*, const Relocate_info<size, big_endian>*,
3084 Output_section*, unsigned char*, Address,
3087 // Get the default AArch64 target.
3091 gold_assert(parameters->target().machine_code() == elfcpp::EM_AARCH64
3092 && parameters->target().get_size() == size
3093 && parameters->target().is_big_endian() == big_endian);
3094 return static_cast<This*>(parameters->sized_target<size, big_endian>());
3098 // Scan erratum 843419 for a part of a section.
3100 scan_erratum_843419_span(
3101 AArch64_relobj<size, big_endian>*,
3103 const section_size_type,
3104 const section_size_type,
3108 // Scan erratum 835769 for a part of a section.
3110 scan_erratum_835769_span(
3111 AArch64_relobj<size, big_endian>*,
3113 const section_size_type,
3114 const section_size_type,
3120 do_select_as_default_target()
3122 gold_assert(aarch64_reloc_property_table == NULL);
3123 aarch64_reloc_property_table = new AArch64_reloc_property_table();
3126 // Add a new reloc argument, returning the index in the vector.
3128 add_tlsdesc_info(Sized_relobj_file<size, big_endian>* object,
3131 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
3132 return this->tlsdesc_reloc_info_.size() - 1;
3135 virtual Output_data_plt_aarch64<size, big_endian>*
3136 do_make_data_plt(Layout* layout,
3137 Output_data_got_aarch64<size, big_endian>* got,
3138 Output_data_space* got_plt,
3139 Output_data_space* got_irelative)
3141 return new Output_data_plt_aarch64_standard<size, big_endian>(
3142 layout, got, got_plt, got_irelative);
3146 // do_make_elf_object to override the same function in the base class.
3148 do_make_elf_object(const std::string&, Input_file*, off_t,
3149 const elfcpp::Ehdr<size, big_endian>&);
3151 Output_data_plt_aarch64<size, big_endian>*
3152 make_data_plt(Layout* layout,
3153 Output_data_got_aarch64<size, big_endian>* got,
3154 Output_data_space* got_plt,
3155 Output_data_space* got_irelative)
3157 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
3160 // We only need to generate stubs, and hence perform relaxation if we are
3161 // not doing relocatable linking.
3163 do_may_relax() const
3164 { return !parameters->options().relocatable(); }
3166 // Relaxation hook. This is where we do stub generation.
3168 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
3171 group_sections(Layout* layout,
3172 section_size_type group_size,
3173 bool stubs_always_after_branch,
3177 scan_reloc_for_stub(const The_relocate_info*, unsigned int,
3178 const Sized_symbol<size>*, unsigned int,
3179 const Symbol_value<size>*,
3180 typename elfcpp::Elf_types<size>::Elf_Swxword,
3183 // Make an output section.
3185 do_make_output_section(const char* name, elfcpp::Elf_Word type,
3186 elfcpp::Elf_Xword flags)
3187 { return new The_aarch64_output_section(name, type, flags); }
3190 // The class which scans relocations.
3195 : issued_non_pic_error_(false)
3199 local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
3200 Sized_relobj_file<size, big_endian>* object,
3201 unsigned int data_shndx,
3202 Output_section* output_section,
3203 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
3204 const elfcpp::Sym<size, big_endian>& lsym,
3208 global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
3209 Sized_relobj_file<size, big_endian>* object,
3210 unsigned int data_shndx,
3211 Output_section* output_section,
3212 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
3216 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
3217 Target_aarch64<size, big_endian>* ,
3218 Sized_relobj_file<size, big_endian>* ,
3221 const elfcpp::Rela<size, big_endian>& ,
3222 unsigned int r_type,
3223 const elfcpp::Sym<size, big_endian>&);
3226 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
3227 Target_aarch64<size, big_endian>* ,
3228 Sized_relobj_file<size, big_endian>* ,
3231 const elfcpp::Rela<size, big_endian>& ,
3232 unsigned int r_type,
3237 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
3238 unsigned int r_type);
3241 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
3242 unsigned int r_type, Symbol*);
3245 possible_function_pointer_reloc(unsigned int r_type);
3248 check_non_pic(Relobj*, unsigned int r_type);
3251 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
3252 unsigned int r_type);
3254 // Whether we have issued an error about a non-PIC compilation.
3255 bool issued_non_pic_error_;
3258 // The class which implements relocation.
3263 : skip_call_tls_get_addr_(false)
3269 // Do a relocation. Return false if the caller should not issue
3270 // any warnings about this relocation.
3272 relocate(const Relocate_info<size, big_endian>*, unsigned int,
3273 Target_aarch64*, Output_section*, size_t, const unsigned char*,
3274 const Sized_symbol<size>*, const Symbol_value<size>*,
3275 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
3279 inline typename AArch64_relocate_functions<size, big_endian>::Status
3280 relocate_tls(const Relocate_info<size, big_endian>*,
3281 Target_aarch64<size, big_endian>*,
3283 const elfcpp::Rela<size, big_endian>&,
3284 unsigned int r_type, const Sized_symbol<size>*,
3285 const Symbol_value<size>*,
3287 typename elfcpp::Elf_types<size>::Elf_Addr);
3289 inline typename AArch64_relocate_functions<size, big_endian>::Status
3291 const Relocate_info<size, big_endian>*,
3292 Target_aarch64<size, big_endian>*,
3293 const elfcpp::Rela<size, big_endian>&,
3296 const Symbol_value<size>*);
3298 inline typename AArch64_relocate_functions<size, big_endian>::Status
3300 const Relocate_info<size, big_endian>*,
3301 Target_aarch64<size, big_endian>*,
3302 const elfcpp::Rela<size, big_endian>&,
3305 const Symbol_value<size>*);
3307 inline typename AArch64_relocate_functions<size, big_endian>::Status
3309 const Relocate_info<size, big_endian>*,
3310 Target_aarch64<size, big_endian>*,
3311 const elfcpp::Rela<size, big_endian>&,
3314 const Symbol_value<size>*);
3316 inline typename AArch64_relocate_functions<size, big_endian>::Status
3318 const Relocate_info<size, big_endian>*,
3319 Target_aarch64<size, big_endian>*,
3320 const elfcpp::Rela<size, big_endian>&,
3323 const Symbol_value<size>*);
3325 inline typename AArch64_relocate_functions<size, big_endian>::Status
3327 const Relocate_info<size, big_endian>*,
3328 Target_aarch64<size, big_endian>*,
3329 const elfcpp::Rela<size, big_endian>&,
3332 const Symbol_value<size>*,
3333 typename elfcpp::Elf_types<size>::Elf_Addr,
3334 typename elfcpp::Elf_types<size>::Elf_Addr);
3336 bool skip_call_tls_get_addr_;
3338 }; // End of class Relocate
3340 // Adjust TLS relocation type based on the options and whether this
3341 // is a local symbol.
3342 static tls::Tls_optimization
3343 optimize_tls_reloc(bool is_final, int r_type);
3345 // Get the GOT section, creating it if necessary.
3346 Output_data_got_aarch64<size, big_endian>*
3347 got_section(Symbol_table*, Layout*);
3349 // Get the GOT PLT section.
3351 got_plt_section() const
3353 gold_assert(this->got_plt_ != NULL);
3354 return this->got_plt_;
3357 // Get the GOT section for TLSDESC entries.
3358 Output_data_got<size, big_endian>*
3359 got_tlsdesc_section() const
3361 gold_assert(this->got_tlsdesc_ != NULL);
3362 return this->got_tlsdesc_;
3365 // Create the PLT section.
3367 make_plt_section(Symbol_table* symtab, Layout* layout);
3369 // Create a PLT entry for a global symbol.
3371 make_plt_entry(Symbol_table*, Layout*, Symbol*);
3373 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
3375 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
3376 Sized_relobj_file<size, big_endian>* relobj,
3377 unsigned int local_sym_index);
3379 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
3381 define_tls_base_symbol(Symbol_table*, Layout*);
3383 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
3385 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
3387 // Create a GOT entry for the TLS module index.
3389 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
3390 Sized_relobj_file<size, big_endian>* object);
3392 // Get the PLT section.
3393 Output_data_plt_aarch64<size, big_endian>*
3396 gold_assert(this->plt_ != NULL);
3400 // Helper method to create erratum stubs for ST_E_843419 and ST_E_835769. For
3401 // ST_E_843419, we need an additional field for adrp offset.
3402 void create_erratum_stub(
3403 AArch64_relobj<size, big_endian>* relobj,
3405 section_size_type erratum_insn_offset,
3406 Address erratum_address,
3407 typename Insn_utilities::Insntype erratum_insn,
3409 unsigned int e843419_adrp_offset=0);
3411 // Return whether this is a 3-insn erratum sequence.
3412 bool is_erratum_843419_sequence(
3413 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
3414 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
3415 typename elfcpp::Swap<32,big_endian>::Valtype insn3);
3417 // Return whether this is a 835769 sequence.
3418 // (Similarly implemented as in elfnn-aarch64.c.)
3419 bool is_erratum_835769_sequence(
3420 typename elfcpp::Swap<32,big_endian>::Valtype,
3421 typename elfcpp::Swap<32,big_endian>::Valtype);
3423 // Get the dynamic reloc section, creating it if necessary.
3425 rela_dyn_section(Layout*);
3427 // Get the section to use for TLSDESC relocations.
3429 rela_tlsdesc_section(Layout*) const;
3431 // Get the section to use for IRELATIVE relocations.
3433 rela_irelative_section(Layout*);
3435 // Add a potential copy relocation.
3437 copy_reloc(Symbol_table* symtab, Layout* layout,
3438 Sized_relobj_file<size, big_endian>* object,
3439 unsigned int shndx, Output_section* output_section,
3440 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
3442 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
3443 this->copy_relocs_.copy_reloc(symtab, layout,
3444 symtab->get_sized_symbol<size>(sym),
3445 object, shndx, output_section,
3446 r_type, reloc.get_r_offset(),
3447 reloc.get_r_addend(),
3448 this->rela_dyn_section(layout));
3451 // Information about this specific target which we pass to the
3452 // general Target structure.
3453 static const Target::Target_info aarch64_info;
3455 // The types of GOT entries needed for this platform.
3456 // These values are exposed to the ABI in an incremental link.
3457 // Do not renumber existing values without changing the version
3458 // number of the .gnu_incremental_inputs section.
3461 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
3462 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
3463 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
3464 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
3467 // This type is used as the argument to the target specific
3468 // relocation routines. The only target specific reloc is
3469 // R_AARCh64_TLSDESC against a local symbol.
3472 Tlsdesc_info(Sized_relobj_file<size, big_endian>* a_object,
3473 unsigned int a_r_sym)
3474 : object(a_object), r_sym(a_r_sym)
3477 // The object in which the local symbol is defined.
3478 Sized_relobj_file<size, big_endian>* object;
3479 // The local symbol index in the object.
3484 Output_data_got_aarch64<size, big_endian>* got_;
3486 Output_data_plt_aarch64<size, big_endian>* plt_;
3487 // The GOT PLT section.
3488 Output_data_space* got_plt_;
3489 // The GOT section for IRELATIVE relocations.
3490 Output_data_space* got_irelative_;
3491 // The GOT section for TLSDESC relocations.
3492 Output_data_got<size, big_endian>* got_tlsdesc_;
3493 // The _GLOBAL_OFFSET_TABLE_ symbol.
3494 Symbol* global_offset_table_;
3495 // The dynamic reloc section.
3496 Reloc_section* rela_dyn_;
3497 // The section to use for IRELATIVE relocs.
3498 Reloc_section* rela_irelative_;
3499 // Relocs saved to avoid a COPY reloc.
3500 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
3501 // Offset of the GOT entry for the TLS module index.
3502 unsigned int got_mod_index_offset_;
3503 // We handle R_AARCH64_TLSDESC against a local symbol as a target
3504 // specific relocation. Here we store the object and local symbol
3505 // index for the relocation.
3506 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
3507 // True if the _TLS_MODULE_BASE_ symbol has been defined.
3508 bool tls_base_symbol_defined_;
3509 // List of stub_tables
3510 Stub_table_list stub_tables_;
3511 // Actual stub group size
3512 section_size_type stub_group_size_;
3513 AArch64_input_section_map aarch64_input_section_map_;
3514 }; // End of Target_aarch64
3518 const Target::Target_info Target_aarch64<64, false>::aarch64_info =
3521 false, // is_big_endian
3522 elfcpp::EM_AARCH64, // machine_code
3523 false, // has_make_symbol
3524 false, // has_resolve
3525 false, // has_code_fill
3526 false, // is_default_stack_executable
3527 true, // can_icf_inline_merge_sections
3529 "/lib/ld.so.1", // program interpreter
3530 0x400000, // default_text_segment_address
3531 0x10000, // abi_pagesize (overridable by -z max-page-size)
3532 0x1000, // common_pagesize (overridable by -z common-page-size)
3533 false, // isolate_execinstr
3535 elfcpp::SHN_UNDEF, // small_common_shndx
3536 elfcpp::SHN_UNDEF, // large_common_shndx
3537 0, // small_common_section_flags
3538 0, // large_common_section_flags
3539 NULL, // attributes_section
3540 NULL, // attributes_vendor
3541 "_start", // entry_symbol_name
3542 32, // hash_entry_size
3546 const Target::Target_info Target_aarch64<32, false>::aarch64_info =
3549 false, // is_big_endian
3550 elfcpp::EM_AARCH64, // machine_code
3551 false, // has_make_symbol
3552 false, // has_resolve
3553 false, // has_code_fill
3554 false, // is_default_stack_executable
3555 false, // can_icf_inline_merge_sections
3557 "/lib/ld.so.1", // program interpreter
3558 0x400000, // default_text_segment_address
3559 0x10000, // abi_pagesize (overridable by -z max-page-size)
3560 0x1000, // common_pagesize (overridable by -z common-page-size)
3561 false, // isolate_execinstr
3563 elfcpp::SHN_UNDEF, // small_common_shndx
3564 elfcpp::SHN_UNDEF, // large_common_shndx
3565 0, // small_common_section_flags
3566 0, // large_common_section_flags
3567 NULL, // attributes_section
3568 NULL, // attributes_vendor
3569 "_start", // entry_symbol_name
3570 32, // hash_entry_size
3574 const Target::Target_info Target_aarch64<64, true>::aarch64_info =
3577 true, // is_big_endian
3578 elfcpp::EM_AARCH64, // machine_code
3579 false, // has_make_symbol
3580 false, // has_resolve
3581 false, // has_code_fill
3582 false, // is_default_stack_executable
3583 true, // can_icf_inline_merge_sections
3585 "/lib/ld.so.1", // program interpreter
3586 0x400000, // default_text_segment_address
3587 0x10000, // abi_pagesize (overridable by -z max-page-size)
3588 0x1000, // common_pagesize (overridable by -z common-page-size)
3589 false, // isolate_execinstr
3591 elfcpp::SHN_UNDEF, // small_common_shndx
3592 elfcpp::SHN_UNDEF, // large_common_shndx
3593 0, // small_common_section_flags
3594 0, // large_common_section_flags
3595 NULL, // attributes_section
3596 NULL, // attributes_vendor
3597 "_start", // entry_symbol_name
3598 32, // hash_entry_size
3602 const Target::Target_info Target_aarch64<32, true>::aarch64_info =
3605 true, // is_big_endian
3606 elfcpp::EM_AARCH64, // machine_code
3607 false, // has_make_symbol
3608 false, // has_resolve
3609 false, // has_code_fill
3610 false, // is_default_stack_executable
3611 false, // can_icf_inline_merge_sections
3613 "/lib/ld.so.1", // program interpreter
3614 0x400000, // default_text_segment_address
3615 0x10000, // abi_pagesize (overridable by -z max-page-size)
3616 0x1000, // common_pagesize (overridable by -z common-page-size)
3617 false, // isolate_execinstr
3619 elfcpp::SHN_UNDEF, // small_common_shndx
3620 elfcpp::SHN_UNDEF, // large_common_shndx
3621 0, // small_common_section_flags
3622 0, // large_common_section_flags
3623 NULL, // attributes_section
3624 NULL, // attributes_vendor
3625 "_start", // entry_symbol_name
3626 32, // hash_entry_size
3629 // Get the GOT section, creating it if necessary.
3631 template<int size, bool big_endian>
3632 Output_data_got_aarch64<size, big_endian>*
3633 Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
3636 if (this->got_ == NULL)
3638 gold_assert(symtab != NULL && layout != NULL);
3640 // When using -z now, we can treat .got.plt as a relro section.
3641 // Without -z now, it is modified after program startup by lazy
3643 bool is_got_plt_relro = parameters->options().now();
3644 Output_section_order got_order = (is_got_plt_relro
3646 : ORDER_RELRO_LAST);
3647 Output_section_order got_plt_order = (is_got_plt_relro
3649 : ORDER_NON_RELRO_FIRST);
3651 // Layout of .got and .got.plt sections.
3652 // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
3654 // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
3655 // .gotplt[1] reserved for ld.so (resolver)
3656 // .gotplt[2] reserved
3658 // Generate .got section.
3659 this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
3661 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3662 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
3663 this->got_, got_order, true);
3664 // The first word of GOT is reserved for the address of .dynamic.
3665 // We put 0 here now. The value will be replaced later in
3666 // Output_data_got_aarch64::do_write.
3667 this->got_->add_constant(0);
3669 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3670 // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
3671 // even if there is a .got.plt section.
3672 this->global_offset_table_ =
3673 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3674 Symbol_table::PREDEFINED,
3676 0, 0, elfcpp::STT_OBJECT,
3678 elfcpp::STV_HIDDEN, 0,
3681 // Generate .got.plt section.
3682 this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
3683 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3685 | elfcpp::SHF_WRITE),
3686 this->got_plt_, got_plt_order,
3689 // The first three entries are reserved.
3690 this->got_plt_->set_current_data_size(
3691 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
3693 // If there are any IRELATIVE relocations, they get GOT entries
3694 // in .got.plt after the jump slot entries.
3695 this->got_irelative_ = new Output_data_space(size / 8,
3696 "** GOT IRELATIVE PLT");
3697 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3699 | elfcpp::SHF_WRITE),
3700 this->got_irelative_,
3704 // If there are any TLSDESC relocations, they get GOT entries in
3705 // .got.plt after the jump slot and IRELATIVE entries.
3706 this->got_tlsdesc_ = new Output_data_got<size, big_endian>();
3707 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3709 | elfcpp::SHF_WRITE),
3714 if (!is_got_plt_relro)
3716 // Those bytes can go into the relro segment.
3717 layout->increase_relro(
3718 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
3725 // Get the dynamic reloc section, creating it if necessary.
3727 template<int size, bool big_endian>
3728 typename Target_aarch64<size, big_endian>::Reloc_section*
3729 Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
3731 if (this->rela_dyn_ == NULL)
3733 gold_assert(layout != NULL);
3734 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3735 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3736 elfcpp::SHF_ALLOC, this->rela_dyn_,
3737 ORDER_DYNAMIC_RELOCS, false);
3739 return this->rela_dyn_;
3742 // Get the section to use for IRELATIVE relocs, creating it if
3743 // necessary. These go in .rela.dyn, but only after all other dynamic
3744 // relocations. They need to follow the other dynamic relocations so
3745 // that they can refer to global variables initialized by those
3748 template<int size, bool big_endian>
3749 typename Target_aarch64<size, big_endian>::Reloc_section*
3750 Target_aarch64<size, big_endian>::rela_irelative_section(Layout* layout)
3752 if (this->rela_irelative_ == NULL)
3754 // Make sure we have already created the dynamic reloc section.
3755 this->rela_dyn_section(layout);
3756 this->rela_irelative_ = new Reloc_section(false);
3757 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3758 elfcpp::SHF_ALLOC, this->rela_irelative_,
3759 ORDER_DYNAMIC_RELOCS, false);
3760 gold_assert(this->rela_dyn_->output_section()
3761 == this->rela_irelative_->output_section());
3763 return this->rela_irelative_;
3767 // do_make_elf_object to override the same function in the base class. We need
3768 // to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
3769 // store backend specific information. Hence we need to have our own ELF object
3772 template<int size, bool big_endian>
3774 Target_aarch64<size, big_endian>::do_make_elf_object(
3775 const std::string& name,
3776 Input_file* input_file,
3777 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
3779 int et = ehdr.get_e_type();
3780 // ET_EXEC files are valid input for --just-symbols/-R,
3781 // and we treat them as relocatable objects.
3782 if (et == elfcpp::ET_EXEC && input_file->just_symbols())
3783 return Sized_target<size, big_endian>::do_make_elf_object(
3784 name, input_file, offset, ehdr);
3785 else if (et == elfcpp::ET_REL)
3787 AArch64_relobj<size, big_endian>* obj =
3788 new AArch64_relobj<size, big_endian>(name, input_file, offset, ehdr);
3792 else if (et == elfcpp::ET_DYN)
3794 // Keep base implementation.
3795 Sized_dynobj<size, big_endian>* obj =
3796 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
3802 gold_error(_("%s: unsupported ELF file type %d"),
3809 // Scan a relocation for stub generation.
3811 template<int size, bool big_endian>
3813 Target_aarch64<size, big_endian>::scan_reloc_for_stub(
3814 const Relocate_info<size, big_endian>* relinfo,
3815 unsigned int r_type,
3816 const Sized_symbol<size>* gsym,
3818 const Symbol_value<size>* psymval,
3819 typename elfcpp::Elf_types<size>::Elf_Swxword addend,
3822 const AArch64_relobj<size, big_endian>* aarch64_relobj =
3823 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3825 Symbol_value<size> symval;
3828 const AArch64_reloc_property* arp = aarch64_reloc_property_table->
3829 get_reloc_property(r_type);
3830 if (gsym->use_plt_offset(arp->reference_flags()))
3832 // This uses a PLT, change the symbol value.
3833 symval.set_output_value(this->plt_address_for_global(gsym));
3836 else if (gsym->is_undefined())
3838 // There is no need to generate a stub symbol if the original symbol
3840 gold_debug(DEBUG_TARGET,
3841 "stub: not creating a stub for undefined symbol %s in file %s",
3842 gsym->name(), aarch64_relobj->name().c_str());
3847 // Get the symbol value.
3848 typename Symbol_value<size>::Value value = psymval->value(aarch64_relobj, 0);
3850 // Owing to pipelining, the PC relative branches below actually skip
3851 // two instructions when the branch offset is 0.
3852 Address destination = static_cast<Address>(-1);
3855 case elfcpp::R_AARCH64_CALL26:
3856 case elfcpp::R_AARCH64_JUMP26:
3857 destination = value + addend;
3863 int stub_type = The_reloc_stub::
3864 stub_type_for_reloc(r_type, address, destination);
3865 if (stub_type == ST_NONE)
3868 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
3869 gold_assert(stub_table != NULL);
3871 The_reloc_stub_key key(stub_type, gsym, aarch64_relobj, r_sym, addend);
3872 The_reloc_stub* stub = stub_table->find_reloc_stub(key);
3875 stub = new The_reloc_stub(stub_type);
3876 stub_table->add_reloc_stub(stub, key);
3878 stub->set_destination_address(destination);
3879 } // End of Target_aarch64::scan_reloc_for_stub
3882 // This function scans a relocation section for stub generation.
3883 // The template parameter Relocate must be a class type which provides
3884 // a single function, relocate(), which implements the machine
3885 // specific part of a relocation.
3887 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
3888 // SHT_REL or SHT_RELA.
3890 // PRELOCS points to the relocation data. RELOC_COUNT is the number
3891 // of relocs. OUTPUT_SECTION is the output section.
3892 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
3893 // mapped to output offsets.
3895 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
3896 // VIEW_SIZE is the size. These refer to the input section, unless
3897 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
3898 // the output section.
3900 template<int size, bool big_endian>
3901 template<int sh_type>
3903 Target_aarch64<size, big_endian>::scan_reloc_section_for_stubs(
3904 const Relocate_info<size, big_endian>* relinfo,
3905 const unsigned char* prelocs,
3907 Output_section* /*output_section*/,
3908 bool /*needs_special_offset_handling*/,
3909 const unsigned char* /*view*/,
3910 Address view_address,
3913 typedef typename Reloc_types<sh_type,size,big_endian>::Reloc Reltype;
3915 const int reloc_size =
3916 Reloc_types<sh_type,size,big_endian>::reloc_size;
3917 AArch64_relobj<size, big_endian>* object =
3918 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3919 unsigned int local_count = object->local_symbol_count();
3921 gold::Default_comdat_behavior default_comdat_behavior;
3922 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
3924 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
3926 Reltype reloc(prelocs);
3927 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
3928 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3929 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
3930 if (r_type != elfcpp::R_AARCH64_CALL26
3931 && r_type != elfcpp::R_AARCH64_JUMP26)
3934 section_offset_type offset =
3935 convert_to_section_size_type(reloc.get_r_offset());
3938 typename elfcpp::Elf_types<size>::Elf_Swxword addend =
3939 reloc.get_r_addend();
3941 const Sized_symbol<size>* sym;
3942 Symbol_value<size> symval;
3943 const Symbol_value<size> *psymval;
3944 bool is_defined_in_discarded_section;
3946 if (r_sym < local_count)
3949 psymval = object->local_symbol(r_sym);
3951 // If the local symbol belongs to a section we are discarding,
3952 // and that section is a debug section, try to find the
3953 // corresponding kept section and map this symbol to its
3954 // counterpart in the kept section. The symbol must not
3955 // correspond to a section we are folding.
3957 shndx = psymval->input_shndx(&is_ordinary);
3958 is_defined_in_discarded_section =
3960 && shndx != elfcpp::SHN_UNDEF
3961 && !object->is_section_included(shndx)
3962 && !relinfo->symtab->is_section_folded(object, shndx));
3964 // We need to compute the would-be final value of this local
3966 if (!is_defined_in_discarded_section)
3968 typedef Sized_relobj_file<size, big_endian> ObjType;
3969 if (psymval->is_section_symbol())
3970 symval.set_is_section_symbol();
3971 typename ObjType::Compute_final_local_value_status status =
3972 object->compute_final_local_value(r_sym, psymval, &symval,
3974 if (status == ObjType::CFLV_OK)
3976 // Currently we cannot handle a branch to a target in
3977 // a merged section. If this is the case, issue an error
3978 // and also free the merge symbol value.
3979 if (!symval.has_output_value())
3981 const std::string& section_name =
3982 object->section_name(shndx);
3983 object->error(_("cannot handle branch to local %u "
3984 "in a merged section %s"),
3985 r_sym, section_name.c_str());
3991 // We cannot determine the final value.
3999 gsym = object->global_symbol(r_sym);
4000 gold_assert(gsym != NULL);
4001 if (gsym->is_forwarder())
4002 gsym = relinfo->symtab->resolve_forwards(gsym);
4004 sym = static_cast<const Sized_symbol<size>*>(gsym);
4005 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
4006 symval.set_output_symtab_index(sym->symtab_index());
4008 symval.set_no_output_symtab_entry();
4010 // We need to compute the would-be final value of this global
4012 const Symbol_table* symtab = relinfo->symtab;
4013 const Sized_symbol<size>* sized_symbol =
4014 symtab->get_sized_symbol<size>(gsym);
4015 Symbol_table::Compute_final_value_status status;
4016 typename elfcpp::Elf_types<size>::Elf_Addr value =
4017 symtab->compute_final_value<size>(sized_symbol, &status);
4019 // Skip this if the symbol has not output section.
4020 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
4022 symval.set_output_value(value);
4024 if (gsym->type() == elfcpp::STT_TLS)
4025 symval.set_is_tls_symbol();
4026 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
4027 symval.set_is_ifunc_symbol();
4030 is_defined_in_discarded_section =
4031 (gsym->is_defined_in_discarded_section()
4032 && gsym->is_undefined());
4036 Symbol_value<size> symval2;
4037 if (is_defined_in_discarded_section)
4039 if (comdat_behavior == CB_UNDETERMINED)
4041 std::string name = object->section_name(relinfo->data_shndx);
4042 comdat_behavior = default_comdat_behavior.get(name.c_str());
4044 if (comdat_behavior == CB_PRETEND)
4047 typename elfcpp::Elf_types<size>::Elf_Addr value =
4048 object->map_to_kept_section(shndx, &found);
4050 symval2.set_output_value(value + psymval->input_value());
4052 symval2.set_output_value(0);
4056 if (comdat_behavior == CB_WARNING)
4057 gold_warning_at_location(relinfo, i, offset,
4058 _("relocation refers to discarded "
4060 symval2.set_output_value(0);
4062 symval2.set_no_output_symtab_entry();
4066 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
4067 addend, view_address + offset);
4068 } // End of iterating relocs in a section
4069 } // End of Target_aarch64::scan_reloc_section_for_stubs
4072 // Scan an input section for stub generation.
4074 template<int size, bool big_endian>
4076 Target_aarch64<size, big_endian>::scan_section_for_stubs(
4077 const Relocate_info<size, big_endian>* relinfo,
4078 unsigned int sh_type,
4079 const unsigned char* prelocs,
4081 Output_section* output_section,
4082 bool needs_special_offset_handling,
4083 const unsigned char* view,
4084 Address view_address,
4085 section_size_type view_size)
4087 gold_assert(sh_type == elfcpp::SHT_RELA);
4088 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
4093 needs_special_offset_handling,
4100 // Relocate a single reloc stub.
4102 template<int size, bool big_endian>
4103 void Target_aarch64<size, big_endian>::
4104 relocate_reloc_stub(The_reloc_stub* stub,
4105 const The_relocate_info*,
4107 unsigned char* view,
4111 typedef AArch64_relocate_functions<size, big_endian> The_reloc_functions;
4112 typedef typename The_reloc_functions::Status The_reloc_functions_status;
4113 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
4115 Insntype* ip = reinterpret_cast<Insntype*>(view);
4116 int insn_number = stub->insn_num();
4117 const uint32_t* insns = stub->insns();
4118 // Check the insns are really those stub insns.
4119 for (int i = 0; i < insn_number; ++i)
4121 Insntype insn = elfcpp::Swap<32,big_endian>::readval(ip + i);
4122 gold_assert(((uint32_t)insn == insns[i]));
4125 Address dest = stub->destination_address();
4127 switch(stub->type())
4129 case ST_ADRP_BRANCH:
4131 // 1st reloc is ADR_PREL_PG_HI21
4132 The_reloc_functions_status status =
4133 The_reloc_functions::adrp(view, dest, address);
4134 // An error should never arise in the above step. If so, please
4135 // check 'aarch64_valid_for_adrp_p'.
4136 gold_assert(status == The_reloc_functions::STATUS_OKAY);
4138 // 2nd reloc is ADD_ABS_LO12_NC
4139 const AArch64_reloc_property* arp =
4140 aarch64_reloc_property_table->get_reloc_property(
4141 elfcpp::R_AARCH64_ADD_ABS_LO12_NC);
4142 gold_assert(arp != NULL);
4143 status = The_reloc_functions::template
4144 rela_general<32>(view + 4, dest, 0, arp);
4145 // An error should never arise, it is an "_NC" relocation.
4146 gold_assert(status == The_reloc_functions::STATUS_OKAY);
4150 case ST_LONG_BRANCH_ABS:
4151 // 1st reloc is R_AARCH64_PREL64, at offset 8
4152 elfcpp::Swap<64,big_endian>::writeval(view + 8, dest);
4155 case ST_LONG_BRANCH_PCREL:
4157 // "PC" calculation is the 2nd insn in the stub.
4158 uint64_t offset = dest - (address + 4);
4159 // Offset is placed at offset 4 and 5.
4160 elfcpp::Swap<64,big_endian>::writeval(view + 16, offset);
4170 // A class to handle the PLT data.
4171 // This is an abstract base class that handles most of the linker details
4172 // but does not know the actual contents of PLT entries. The derived
4173 // classes below fill in those details.
4175 template<int size, bool big_endian>
4176 class Output_data_plt_aarch64 : public Output_section_data
4179 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
4181 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4183 Output_data_plt_aarch64(Layout* layout,
4185 Output_data_got_aarch64<size, big_endian>* got,
4186 Output_data_space* got_plt,
4187 Output_data_space* got_irelative)
4188 : Output_section_data(addralign), tlsdesc_rel_(NULL), irelative_rel_(NULL),
4189 got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
4190 count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
4191 { this->init(layout); }
4193 // Initialize the PLT section.
4195 init(Layout* layout);
4197 // Add an entry to the PLT.
4199 add_entry(Symbol_table*, Layout*, Symbol* gsym);
4201 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
4203 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
4204 Sized_relobj_file<size, big_endian>* relobj,
4205 unsigned int local_sym_index);
4207 // Add the relocation for a PLT entry.
4209 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
4210 unsigned int got_offset);
4212 // Add the reserved TLSDESC_PLT entry to the PLT.
4214 reserve_tlsdesc_entry(unsigned int got_offset)
4215 { this->tlsdesc_got_offset_ = got_offset; }
4217 // Return true if a TLSDESC_PLT entry has been reserved.
4219 has_tlsdesc_entry() const
4220 { return this->tlsdesc_got_offset_ != -1U; }
4222 // Return the GOT offset for the reserved TLSDESC_PLT entry.
4224 get_tlsdesc_got_offset() const
4225 { return this->tlsdesc_got_offset_; }
4227 // Return the PLT offset of the reserved TLSDESC_PLT entry.
4229 get_tlsdesc_plt_offset() const
4231 return (this->first_plt_entry_offset() +
4232 (this->count_ + this->irelative_count_)
4233 * this->get_plt_entry_size());
4236 // Return the .rela.plt section data.
4239 { return this->rel_; }
4241 // Return where the TLSDESC relocations should go.
4243 rela_tlsdesc(Layout*);
4245 // Return where the IRELATIVE relocations should go in the PLT
4248 rela_irelative(Symbol_table*, Layout*);
4250 // Return whether we created a section for IRELATIVE relocations.
4252 has_irelative_section() const
4253 { return this->irelative_rel_ != NULL; }
4255 // Return the number of PLT entries.
4258 { return this->count_ + this->irelative_count_; }
4260 // Return the offset of the first non-reserved PLT entry.
4262 first_plt_entry_offset() const
4263 { return this->do_first_plt_entry_offset(); }
4265 // Return the size of a PLT entry.
4267 get_plt_entry_size() const
4268 { return this->do_get_plt_entry_size(); }
4270 // Return the reserved tlsdesc entry size.
4272 get_plt_tlsdesc_entry_size() const
4273 { return this->do_get_plt_tlsdesc_entry_size(); }
4275 // Return the PLT address to use for a global symbol.
4277 address_for_global(const Symbol*);
4279 // Return the PLT address to use for a local symbol.
4281 address_for_local(const Relobj*, unsigned int symndx);
4284 // Fill in the first PLT entry.
4286 fill_first_plt_entry(unsigned char* pov,
4287 Address got_address,
4288 Address plt_address)
4289 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
4291 // Fill in a normal PLT entry.
4293 fill_plt_entry(unsigned char* pov,
4294 Address got_address,
4295 Address plt_address,
4296 unsigned int got_offset,
4297 unsigned int plt_offset)
4299 this->do_fill_plt_entry(pov, got_address, plt_address,
4300 got_offset, plt_offset);
4303 // Fill in the reserved TLSDESC PLT entry.
4305 fill_tlsdesc_entry(unsigned char* pov,
4306 Address gotplt_address,
4307 Address plt_address,
4309 unsigned int tlsdesc_got_offset,
4310 unsigned int plt_offset)
4312 this->do_fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
4313 tlsdesc_got_offset, plt_offset);
4316 virtual unsigned int
4317 do_first_plt_entry_offset() const = 0;
4319 virtual unsigned int
4320 do_get_plt_entry_size() const = 0;
4322 virtual unsigned int
4323 do_get_plt_tlsdesc_entry_size() const = 0;
4326 do_fill_first_plt_entry(unsigned char* pov,
4328 Address plt_addr) = 0;
4331 do_fill_plt_entry(unsigned char* pov,
4332 Address got_address,
4333 Address plt_address,
4334 unsigned int got_offset,
4335 unsigned int plt_offset) = 0;
4338 do_fill_tlsdesc_entry(unsigned char* pov,
4339 Address gotplt_address,
4340 Address plt_address,
4342 unsigned int tlsdesc_got_offset,
4343 unsigned int plt_offset) = 0;
4346 do_adjust_output_section(Output_section* os);
4348 // Write to a map file.
4350 do_print_to_mapfile(Mapfile* mapfile) const
4351 { mapfile->print_output_data(this, _("** PLT")); }
4354 // Set the final size.
4356 set_final_data_size();
4358 // Write out the PLT data.
4360 do_write(Output_file*);
4362 // The reloc section.
4363 Reloc_section* rel_;
4365 // The TLSDESC relocs, if necessary. These must follow the regular
4367 Reloc_section* tlsdesc_rel_;
4369 // The IRELATIVE relocs, if necessary. These must follow the
4370 // regular PLT relocations.
4371 Reloc_section* irelative_rel_;
4373 // The .got section.
4374 Output_data_got_aarch64<size, big_endian>* got_;
4376 // The .got.plt section.
4377 Output_data_space* got_plt_;
4379 // The part of the .got.plt section used for IRELATIVE relocs.
4380 Output_data_space* got_irelative_;
4382 // The number of PLT entries.
4383 unsigned int count_;
4385 // Number of PLT entries with R_AARCH64_IRELATIVE relocs. These
4386 // follow the regular PLT entries.
4387 unsigned int irelative_count_;
4389 // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
4390 // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
4391 // indicates an offset is not allocated.
4392 unsigned int tlsdesc_got_offset_;
4395 // Initialize the PLT section.
4397 template<int size, bool big_endian>
4399 Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
4401 this->rel_ = new Reloc_section(false);
4402 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4403 elfcpp::SHF_ALLOC, this->rel_,
4404 ORDER_DYNAMIC_PLT_RELOCS, false);
4407 template<int size, bool big_endian>
4409 Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
4412 os->set_entsize(this->get_plt_entry_size());
4415 // Add an entry to the PLT.
4417 template<int size, bool big_endian>
4419 Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol_table* symtab,
4420 Layout* layout, Symbol* gsym)
4422 gold_assert(!gsym->has_plt_offset());
4424 unsigned int* pcount;
4425 unsigned int plt_reserved;
4426 Output_section_data_build* got;
4428 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4429 && gsym->can_use_relative_reloc(false))
4431 pcount = &this->irelative_count_;
4433 got = this->got_irelative_;
4437 pcount = &this->count_;
4438 plt_reserved = this->first_plt_entry_offset();
4439 got = this->got_plt_;
4442 gsym->set_plt_offset((*pcount) * this->get_plt_entry_size()
4447 section_offset_type got_offset = got->current_data_size();
4449 // Every PLT entry needs a GOT entry which points back to the PLT
4450 // entry (this will be changed by the dynamic linker, normally
4451 // lazily when the function is called).
4452 got->set_current_data_size(got_offset + size / 8);
4454 // Every PLT entry needs a reloc.
4455 this->add_relocation(symtab, layout, gsym, got_offset);
4457 // Note that we don't need to save the symbol. The contents of the
4458 // PLT are independent of which symbols are used. The symbols only
4459 // appear in the relocations.
4462 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
4465 template<int size, bool big_endian>
4467 Output_data_plt_aarch64<size, big_endian>::add_local_ifunc_entry(
4468 Symbol_table* symtab,
4470 Sized_relobj_file<size, big_endian>* relobj,
4471 unsigned int local_sym_index)
4473 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
4474 ++this->irelative_count_;
4476 section_offset_type got_offset = this->got_irelative_->current_data_size();
4478 // Every PLT entry needs a GOT entry which points back to the PLT
4480 this->got_irelative_->set_current_data_size(got_offset + size / 8);
4482 // Every PLT entry needs a reloc.
4483 Reloc_section* rela = this->rela_irelative(symtab, layout);
4484 rela->add_symbolless_local_addend(relobj, local_sym_index,
4485 elfcpp::R_AARCH64_IRELATIVE,
4486 this->got_irelative_, got_offset, 0);
4491 // Add the relocation for a PLT entry.
4493 template<int size, bool big_endian>
4495 Output_data_plt_aarch64<size, big_endian>::add_relocation(
4496 Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
4498 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4499 && gsym->can_use_relative_reloc(false))
4501 Reloc_section* rela = this->rela_irelative(symtab, layout);
4502 rela->add_symbolless_global_addend(gsym, elfcpp::R_AARCH64_IRELATIVE,
4503 this->got_irelative_, got_offset, 0);
4507 gsym->set_needs_dynsym_entry();
4508 this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT, this->got_plt_,
4513 // Return where the TLSDESC relocations should go, creating it if
4514 // necessary. These follow the JUMP_SLOT relocations.
4516 template<int size, bool big_endian>
4517 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
4518 Output_data_plt_aarch64<size, big_endian>::rela_tlsdesc(Layout* layout)
4520 if (this->tlsdesc_rel_ == NULL)
4522 this->tlsdesc_rel_ = new Reloc_section(false);
4523 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4524 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
4525 ORDER_DYNAMIC_PLT_RELOCS, false);
4526 gold_assert(this->tlsdesc_rel_->output_section()
4527 == this->rel_->output_section());
4529 return this->tlsdesc_rel_;
4532 // Return where the IRELATIVE relocations should go in the PLT. These
4533 // follow the JUMP_SLOT and the TLSDESC relocations.
4535 template<int size, bool big_endian>
4536 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
4537 Output_data_plt_aarch64<size, big_endian>::rela_irelative(Symbol_table* symtab,
4540 if (this->irelative_rel_ == NULL)
4542 // Make sure we have a place for the TLSDESC relocations, in
4543 // case we see any later on.
4544 this->rela_tlsdesc(layout);
4545 this->irelative_rel_ = new Reloc_section(false);
4546 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4547 elfcpp::SHF_ALLOC, this->irelative_rel_,
4548 ORDER_DYNAMIC_PLT_RELOCS, false);
4549 gold_assert(this->irelative_rel_->output_section()
4550 == this->rel_->output_section());
4552 if (parameters->doing_static_link())
4554 // A statically linked executable will only have a .rela.plt
4555 // section to hold R_AARCH64_IRELATIVE relocs for
4556 // STT_GNU_IFUNC symbols. The library will use these
4557 // symbols to locate the IRELATIVE relocs at program startup
4559 symtab->define_in_output_data("__rela_iplt_start", NULL,
4560 Symbol_table::PREDEFINED,
4561 this->irelative_rel_, 0, 0,
4562 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4563 elfcpp::STV_HIDDEN, 0, false, true);
4564 symtab->define_in_output_data("__rela_iplt_end", NULL,
4565 Symbol_table::PREDEFINED,
4566 this->irelative_rel_, 0, 0,
4567 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4568 elfcpp::STV_HIDDEN, 0, true, true);
4571 return this->irelative_rel_;
4574 // Return the PLT address to use for a global symbol.
4576 template<int size, bool big_endian>
4578 Output_data_plt_aarch64<size, big_endian>::address_for_global(
4581 uint64_t offset = 0;
4582 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4583 && gsym->can_use_relative_reloc(false))
4584 offset = (this->first_plt_entry_offset() +
4585 this->count_ * this->get_plt_entry_size());
4586 return this->address() + offset + gsym->plt_offset();
4589 // Return the PLT address to use for a local symbol. These are always
4590 // IRELATIVE relocs.
4592 template<int size, bool big_endian>
4594 Output_data_plt_aarch64<size, big_endian>::address_for_local(
4595 const Relobj* object,
4598 return (this->address()
4599 + this->first_plt_entry_offset()
4600 + this->count_ * this->get_plt_entry_size()
4601 + object->local_plt_offset(r_sym));
4604 // Set the final size.
4606 template<int size, bool big_endian>
4608 Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
4610 unsigned int count = this->count_ + this->irelative_count_;
4611 unsigned int extra_size = 0;
4612 if (this->has_tlsdesc_entry())
4613 extra_size += this->get_plt_tlsdesc_entry_size();
4614 this->set_data_size(this->first_plt_entry_offset()
4615 + count * this->get_plt_entry_size()
4619 template<int size, bool big_endian>
4620 class Output_data_plt_aarch64_standard :
4621 public Output_data_plt_aarch64<size, big_endian>
4624 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4625 Output_data_plt_aarch64_standard(
4627 Output_data_got_aarch64<size, big_endian>* got,
4628 Output_data_space* got_plt,
4629 Output_data_space* got_irelative)
4630 : Output_data_plt_aarch64<size, big_endian>(layout,
4637 // Return the offset of the first non-reserved PLT entry.
4638 virtual unsigned int
4639 do_first_plt_entry_offset() const
4640 { return this->first_plt_entry_size; }
4642 // Return the size of a PLT entry
4643 virtual unsigned int
4644 do_get_plt_entry_size() const
4645 { return this->plt_entry_size; }
4647 // Return the size of a tlsdesc entry
4648 virtual unsigned int
4649 do_get_plt_tlsdesc_entry_size() const
4650 { return this->plt_tlsdesc_entry_size; }
4653 do_fill_first_plt_entry(unsigned char* pov,
4654 Address got_address,
4655 Address plt_address);
4658 do_fill_plt_entry(unsigned char* pov,
4659 Address got_address,
4660 Address plt_address,
4661 unsigned int got_offset,
4662 unsigned int plt_offset);
4665 do_fill_tlsdesc_entry(unsigned char* pov,
4666 Address gotplt_address,
4667 Address plt_address,
4669 unsigned int tlsdesc_got_offset,
4670 unsigned int plt_offset);
4673 // The size of the first plt entry size.
4674 static const int first_plt_entry_size = 32;
4675 // The size of the plt entry size.
4676 static const int plt_entry_size = 16;
4677 // The size of the plt tlsdesc entry size.
4678 static const int plt_tlsdesc_entry_size = 32;
4679 // Template for the first PLT entry.
4680 static const uint32_t first_plt_entry[first_plt_entry_size / 4];
4681 // Template for subsequent PLT entries.
4682 static const uint32_t plt_entry[plt_entry_size / 4];
4683 // The reserved TLSDESC entry in the PLT for an executable.
4684 static const uint32_t tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4];
4687 // The first entry in the PLT for an executable.
4691 Output_data_plt_aarch64_standard<32, false>::
4692 first_plt_entry[first_plt_entry_size / 4] =
4694 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4695 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4696 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4697 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4698 0xd61f0220, /* br x17 */
4699 0xd503201f, /* nop */
4700 0xd503201f, /* nop */
4701 0xd503201f, /* nop */
4707 Output_data_plt_aarch64_standard<32, true>::
4708 first_plt_entry[first_plt_entry_size / 4] =
4710 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4711 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4712 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4713 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4714 0xd61f0220, /* br x17 */
4715 0xd503201f, /* nop */
4716 0xd503201f, /* nop */
4717 0xd503201f, /* nop */
4723 Output_data_plt_aarch64_standard<64, false>::
4724 first_plt_entry[first_plt_entry_size / 4] =
4726 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4727 0x90000010, /* adrp x16, PLT_GOT+16 */
4728 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4729 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4730 0xd61f0220, /* br x17 */
4731 0xd503201f, /* nop */
4732 0xd503201f, /* nop */
4733 0xd503201f, /* nop */
4739 Output_data_plt_aarch64_standard<64, true>::
4740 first_plt_entry[first_plt_entry_size / 4] =
4742 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4743 0x90000010, /* adrp x16, PLT_GOT+16 */
4744 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4745 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4746 0xd61f0220, /* br x17 */
4747 0xd503201f, /* nop */
4748 0xd503201f, /* nop */
4749 0xd503201f, /* nop */
4755 Output_data_plt_aarch64_standard<32, false>::
4756 plt_entry[plt_entry_size / 4] =
4758 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4759 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4760 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4761 0xd61f0220, /* br x17. */
4767 Output_data_plt_aarch64_standard<32, true>::
4768 plt_entry[plt_entry_size / 4] =
4770 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4771 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4772 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4773 0xd61f0220, /* br x17. */
4779 Output_data_plt_aarch64_standard<64, false>::
4780 plt_entry[plt_entry_size / 4] =
4782 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4783 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4784 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4785 0xd61f0220, /* br x17. */
4791 Output_data_plt_aarch64_standard<64, true>::
4792 plt_entry[plt_entry_size / 4] =
4794 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4795 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4796 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4797 0xd61f0220, /* br x17. */
4801 template<int size, bool big_endian>
4803 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
4805 Address got_address,
4806 Address plt_address)
4808 // PLT0 of the small PLT looks like this in ELF64 -
4809 // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
4810 // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
4811 // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
4813 // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
4814 // GOTPLT entry for this.
4816 // PLT0 will be slightly different in ELF32 due to different got entry
4818 memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
4819 Address gotplt_2nd_ent = got_address + (size / 8) * 2;
4821 // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
4822 // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
4823 // FIXME: This only works for 64bit
4824 AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
4825 gotplt_2nd_ent, plt_address + 4);
4827 // Fill in R_AARCH64_LDST8_LO12
4828 elfcpp::Swap<32, big_endian>::writeval(
4830 ((this->first_plt_entry[2] & 0xffc003ff)
4831 | ((gotplt_2nd_ent & 0xff8) << 7)));
4833 // Fill in R_AARCH64_ADD_ABS_LO12
4834 elfcpp::Swap<32, big_endian>::writeval(
4836 ((this->first_plt_entry[3] & 0xffc003ff)
4837 | ((gotplt_2nd_ent & 0xfff) << 10)));
4841 // Subsequent entries in the PLT for an executable.
4842 // FIXME: This only works for 64bit
4844 template<int size, bool big_endian>
4846 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
4848 Address got_address,
4849 Address plt_address,
4850 unsigned int got_offset,
4851 unsigned int plt_offset)
4853 memcpy(pov, this->plt_entry, this->plt_entry_size);
4855 Address gotplt_entry_address = got_address + got_offset;
4856 Address plt_entry_address = plt_address + plt_offset;
4858 // Fill in R_AARCH64_PCREL_ADR_HI21
4859 AArch64_relocate_functions<size, big_endian>::adrp(
4861 gotplt_entry_address,
4864 // Fill in R_AARCH64_LDST64_ABS_LO12
4865 elfcpp::Swap<32, big_endian>::writeval(
4867 ((this->plt_entry[1] & 0xffc003ff)
4868 | ((gotplt_entry_address & 0xff8) << 7)));
4870 // Fill in R_AARCH64_ADD_ABS_LO12
4871 elfcpp::Swap<32, big_endian>::writeval(
4873 ((this->plt_entry[2] & 0xffc003ff)
4874 | ((gotplt_entry_address & 0xfff) <<10)));
4881 Output_data_plt_aarch64_standard<32, false>::
4882 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4884 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4885 0x90000002, /* adrp x2, 0 */
4886 0x90000003, /* adrp x3, 0 */
4887 0xb9400042, /* ldr w2, [w2, #0] */
4888 0x11000063, /* add w3, w3, 0 */
4889 0xd61f0040, /* br x2 */
4890 0xd503201f, /* nop */
4891 0xd503201f, /* nop */
4896 Output_data_plt_aarch64_standard<32, true>::
4897 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4899 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4900 0x90000002, /* adrp x2, 0 */
4901 0x90000003, /* adrp x3, 0 */
4902 0xb9400042, /* ldr w2, [w2, #0] */
4903 0x11000063, /* add w3, w3, 0 */
4904 0xd61f0040, /* br x2 */
4905 0xd503201f, /* nop */
4906 0xd503201f, /* nop */
4911 Output_data_plt_aarch64_standard<64, false>::
4912 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4914 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4915 0x90000002, /* adrp x2, 0 */
4916 0x90000003, /* adrp x3, 0 */
4917 0xf9400042, /* ldr x2, [x2, #0] */
4918 0x91000063, /* add x3, x3, 0 */
4919 0xd61f0040, /* br x2 */
4920 0xd503201f, /* nop */
4921 0xd503201f, /* nop */
4926 Output_data_plt_aarch64_standard<64, true>::
4927 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4929 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4930 0x90000002, /* adrp x2, 0 */
4931 0x90000003, /* adrp x3, 0 */
4932 0xf9400042, /* ldr x2, [x2, #0] */
4933 0x91000063, /* add x3, x3, 0 */
4934 0xd61f0040, /* br x2 */
4935 0xd503201f, /* nop */
4936 0xd503201f, /* nop */
4939 template<int size, bool big_endian>
4941 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_tlsdesc_entry(
4943 Address gotplt_address,
4944 Address plt_address,
4946 unsigned int tlsdesc_got_offset,
4947 unsigned int plt_offset)
4949 memcpy(pov, tlsdesc_plt_entry, plt_tlsdesc_entry_size);
4951 // move DT_TLSDESC_GOT address into x2
4952 // move .got.plt address into x3
4953 Address tlsdesc_got_entry = got_base + tlsdesc_got_offset;
4954 Address plt_entry_address = plt_address + plt_offset;
4956 // R_AARCH64_ADR_PREL_PG_HI21
4957 AArch64_relocate_functions<size, big_endian>::adrp(
4960 plt_entry_address + 4);
4962 // R_AARCH64_ADR_PREL_PG_HI21
4963 AArch64_relocate_functions<size, big_endian>::adrp(
4966 plt_entry_address + 8);
4968 // R_AARCH64_LDST64_ABS_LO12
4969 elfcpp::Swap<32, big_endian>::writeval(
4971 ((this->tlsdesc_plt_entry[3] & 0xffc003ff)
4972 | ((tlsdesc_got_entry & 0xff8) << 7)));
4974 // R_AARCH64_ADD_ABS_LO12
4975 elfcpp::Swap<32, big_endian>::writeval(
4977 ((this->tlsdesc_plt_entry[4] & 0xffc003ff)
4978 | ((gotplt_address & 0xfff) << 10)));
4981 // Write out the PLT. This uses the hand-coded instructions above,
4982 // and adjusts them as needed. This is specified by the AMD64 ABI.
4984 template<int size, bool big_endian>
4986 Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
4988 const off_t offset = this->offset();
4989 const section_size_type oview_size =
4990 convert_to_section_size_type(this->data_size());
4991 unsigned char* const oview = of->get_output_view(offset, oview_size);
4993 const off_t got_file_offset = this->got_plt_->offset();
4994 gold_assert(got_file_offset + this->got_plt_->data_size()
4995 == this->got_irelative_->offset());
4997 const section_size_type got_size =
4998 convert_to_section_size_type(this->got_plt_->data_size()
4999 + this->got_irelative_->data_size());
5000 unsigned char* const got_view = of->get_output_view(got_file_offset,
5003 unsigned char* pov = oview;
5005 // The base address of the .plt section.
5006 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
5007 // The base address of the PLT portion of the .got section.
5008 typename elfcpp::Elf_types<size>::Elf_Addr gotplt_address
5009 = this->got_plt_->address();
5011 this->fill_first_plt_entry(pov, gotplt_address, plt_address);
5012 pov += this->first_plt_entry_offset();
5014 // The first three entries in .got.plt are reserved.
5015 unsigned char* got_pov = got_view;
5016 memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
5017 got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
5019 unsigned int plt_offset = this->first_plt_entry_offset();
5020 unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
5021 const unsigned int count = this->count_ + this->irelative_count_;
5022 for (unsigned int plt_index = 0;
5025 pov += this->get_plt_entry_size(),
5026 got_pov += size / 8,
5027 plt_offset += this->get_plt_entry_size(),
5028 got_offset += size / 8)
5030 // Set and adjust the PLT entry itself.
5031 this->fill_plt_entry(pov, gotplt_address, plt_address,
5032 got_offset, plt_offset);
5034 // Set the entry in the GOT, which points to plt0.
5035 elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
5038 if (this->has_tlsdesc_entry())
5040 // Set and adjust the reserved TLSDESC PLT entry.
5041 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
5042 // The base address of the .base section.
5043 typename elfcpp::Elf_types<size>::Elf_Addr got_base =
5044 this->got_->address();
5045 this->fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
5046 tlsdesc_got_offset, plt_offset);
5047 pov += this->get_plt_tlsdesc_entry_size();
5050 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
5051 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
5053 of->write_output_view(offset, oview_size, oview);
5054 of->write_output_view(got_file_offset, got_size, got_view);
5057 // Telling how to update the immediate field of an instruction.
5058 struct AArch64_howto
5060 // The immediate field mask.
5061 elfcpp::Elf_Xword dst_mask;
5063 // The offset to apply relocation immediate
5066 // The second part offset, if the immediate field has two parts.
5067 // -1 if the immediate field has only one part.
5071 static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
5073 {0, -1, -1}, // DATA
5074 {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
5075 {0xffffe0, 5, -1}, // LD [23:5]-imm19
5076 {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
5077 {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
5078 {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
5079 {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
5080 {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
5081 {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
5082 {0x3ffffff, 0, -1}, // B [25:0]-imm26
5083 {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
5086 // AArch64 relocate function class
5088 template<int size, bool big_endian>
5089 class AArch64_relocate_functions
5094 STATUS_OKAY, // No error during relocation.
5095 STATUS_OVERFLOW, // Relocation overflow.
5096 STATUS_BAD_RELOC, // Relocation cannot be applied.
5099 typedef AArch64_relocate_functions<size, big_endian> This;
5100 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5101 typedef Relocate_info<size, big_endian> The_relocate_info;
5102 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
5103 typedef Reloc_stub<size, big_endian> The_reloc_stub;
5104 typedef Stub_table<size, big_endian> The_stub_table;
5105 typedef elfcpp::Rela<size, big_endian> The_rela;
5106 typedef typename elfcpp::Swap<size, big_endian>::Valtype AArch64_valtype;
5108 // Return the page address of the address.
5109 // Page(address) = address & ~0xFFF
5111 static inline AArch64_valtype
5112 Page(Address address)
5114 return (address & (~static_cast<Address>(0xFFF)));
5118 // Update instruction (pointed by view) with selected bits (immed).
5119 // val = (val & ~dst_mask) | (immed << doffset)
5121 template<int valsize>
5123 update_view(unsigned char* view,
5124 AArch64_valtype immed,
5125 elfcpp::Elf_Xword doffset,
5126 elfcpp::Elf_Xword dst_mask)
5128 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
5129 Valtype* wv = reinterpret_cast<Valtype*>(view);
5130 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
5132 // Clear immediate fields.
5134 elfcpp::Swap<valsize, big_endian>::writeval(wv,
5135 static_cast<Valtype>(val | (immed << doffset)));
5138 // Update two parts of an instruction (pointed by view) with selected
5139 // bits (immed1 and immed2).
5140 // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
5142 template<int valsize>
5144 update_view_two_parts(
5145 unsigned char* view,
5146 AArch64_valtype immed1,
5147 AArch64_valtype immed2,
5148 elfcpp::Elf_Xword doffset1,
5149 elfcpp::Elf_Xword doffset2,
5150 elfcpp::Elf_Xword dst_mask)
5152 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
5153 Valtype* wv = reinterpret_cast<Valtype*>(view);
5154 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
5156 elfcpp::Swap<valsize, big_endian>::writeval(wv,
5157 static_cast<Valtype>(val | (immed1 << doffset1) |
5158 (immed2 << doffset2)));
5161 // Update adr or adrp instruction with immed.
5162 // In adr and adrp: [30:29] immlo [23:5] immhi
5165 update_adr(unsigned char* view, AArch64_valtype immed)
5167 elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
5168 This::template update_view_two_parts<32>(
5171 (immed & 0x1ffffc) >> 2,
5177 // Update movz/movn instruction with bits immed.
5178 // Set instruction to movz if is_movz is true, otherwise set instruction
5182 update_movnz(unsigned char* view,
5183 AArch64_valtype immed,
5186 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5187 Valtype* wv = reinterpret_cast<Valtype*>(view);
5188 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
5190 const elfcpp::Elf_Xword doffset =
5191 aarch64_howto[AArch64_reloc_property::INST_MOVW].doffset;
5192 const elfcpp::Elf_Xword dst_mask =
5193 aarch64_howto[AArch64_reloc_property::INST_MOVW].dst_mask;
5195 // Clear immediate fields and opc code.
5196 val &= ~(dst_mask | (0x3 << 29));
5198 // Set instruction to movz or movn.
5199 // movz: [30:29] is 10 movn: [30:29] is 00
5203 elfcpp::Swap<32, big_endian>::writeval(wv,
5204 static_cast<Valtype>(val | (immed << doffset)));
5209 // Update selected bits in text.
5211 template<int valsize>
5212 static inline typename This::Status
5213 reloc_common(unsigned char* view, Address x,
5214 const AArch64_reloc_property* reloc_property)
5216 // Select bits from X.
5217 Address immed = reloc_property->select_x_value(x);
5220 const AArch64_reloc_property::Reloc_inst inst =
5221 reloc_property->reloc_inst();
5222 // If it is a data relocation or instruction has 2 parts of immediate
5223 // fields, you should not call pcrela_general.
5224 gold_assert(aarch64_howto[inst].doffset2 == -1 &&
5225 aarch64_howto[inst].doffset != -1);
5226 This::template update_view<valsize>(view, immed,
5227 aarch64_howto[inst].doffset,
5228 aarch64_howto[inst].dst_mask);
5230 // Do check overflow or alignment if needed.
5231 return (reloc_property->checkup_x_value(x)
5233 : This::STATUS_OVERFLOW);
5236 // Construct a B insn. Note, although we group it here with other relocation
5237 // operation, there is actually no 'relocation' involved here.
5239 construct_b(unsigned char* view, unsigned int branch_offset)
5241 update_view_two_parts<32>(view, 0x05, (branch_offset >> 2),
5245 // Do a simple rela relocation at unaligned addresses.
5247 template<int valsize>
5248 static inline typename This::Status
5249 rela_ua(unsigned char* view,
5250 const Sized_relobj_file<size, big_endian>* object,
5251 const Symbol_value<size>* psymval,
5252 AArch64_valtype addend,
5253 const AArch64_reloc_property* reloc_property)
5255 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
5257 typename elfcpp::Elf_types<size>::Elf_Addr x =
5258 psymval->value(object, addend);
5259 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
5260 static_cast<Valtype>(x));
5261 return (reloc_property->checkup_x_value(x)
5263 : This::STATUS_OVERFLOW);
5266 // Do a simple pc-relative relocation at unaligned addresses.
5268 template<int valsize>
5269 static inline typename This::Status
5270 pcrela_ua(unsigned char* view,
5271 const Sized_relobj_file<size, big_endian>* object,
5272 const Symbol_value<size>* psymval,
5273 AArch64_valtype addend,
5275 const AArch64_reloc_property* reloc_property)
5277 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
5279 Address x = psymval->value(object, addend) - address;
5280 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
5281 static_cast<Valtype>(x));
5282 return (reloc_property->checkup_x_value(x)
5284 : This::STATUS_OVERFLOW);
5287 // Do a simple rela relocation at aligned addresses.
5289 template<int valsize>
5290 static inline typename This::Status
5292 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 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
5299 Valtype* wv = reinterpret_cast<Valtype*>(view);
5300 Address x = psymval->value(object, addend);
5301 elfcpp::Swap<valsize, big_endian>::writeval(wv,static_cast<Valtype>(x));
5302 return (reloc_property->checkup_x_value(x)
5304 : This::STATUS_OVERFLOW);
5307 // Do relocate. Update selected bits in text.
5308 // new_val = (val & ~dst_mask) | (immed << doffset)
5310 template<int valsize>
5311 static inline typename This::Status
5312 rela_general(unsigned char* view,
5313 const Sized_relobj_file<size, big_endian>* object,
5314 const Symbol_value<size>* psymval,
5315 AArch64_valtype addend,
5316 const AArch64_reloc_property* reloc_property)
5318 // Calculate relocation.
5319 Address x = psymval->value(object, addend);
5320 return This::template reloc_common<valsize>(view, x, reloc_property);
5323 // Do relocate. Update selected bits in text.
5324 // new val = (val & ~dst_mask) | (immed << doffset)
5326 template<int valsize>
5327 static inline typename This::Status
5329 unsigned char* view,
5331 AArch64_valtype addend,
5332 const AArch64_reloc_property* reloc_property)
5334 // Calculate relocation.
5335 Address x = s + addend;
5336 return This::template reloc_common<valsize>(view, x, reloc_property);
5339 // Do address relative relocate. Update selected bits in text.
5340 // new val = (val & ~dst_mask) | (immed << doffset)
5342 template<int valsize>
5343 static inline typename This::Status
5345 unsigned char* view,
5346 const Sized_relobj_file<size, big_endian>* object,
5347 const Symbol_value<size>* psymval,
5348 AArch64_valtype addend,
5350 const AArch64_reloc_property* reloc_property)
5352 // Calculate relocation.
5353 Address x = psymval->value(object, addend) - address;
5354 return This::template reloc_common<valsize>(view, x, reloc_property);
5358 // Calculate (S + A) - address, update adr instruction.
5360 static inline typename This::Status
5361 adr(unsigned char* view,
5362 const Sized_relobj_file<size, big_endian>* object,
5363 const Symbol_value<size>* psymval,
5366 const AArch64_reloc_property* /* reloc_property */)
5368 AArch64_valtype x = psymval->value(object, addend) - address;
5369 // Pick bits [20:0] of X.
5370 AArch64_valtype immed = x & 0x1fffff;
5371 update_adr(view, immed);
5372 // Check -2^20 <= X < 2^20
5373 return (size == 64 && Bits<21>::has_overflow((x))
5374 ? This::STATUS_OVERFLOW
5375 : This::STATUS_OKAY);
5378 // Calculate PG(S+A) - PG(address), update adrp instruction.
5379 // R_AARCH64_ADR_PREL_PG_HI21
5381 static inline typename This::Status
5383 unsigned char* view,
5387 AArch64_valtype x = This::Page(sa) - This::Page(address);
5388 // Pick [32:12] of X.
5389 AArch64_valtype immed = (x >> 12) & 0x1fffff;
5390 update_adr(view, immed);
5391 // Check -2^32 <= X < 2^32
5392 return (size == 64 && Bits<33>::has_overflow((x))
5393 ? This::STATUS_OVERFLOW
5394 : This::STATUS_OKAY);
5397 // Calculate PG(S+A) - PG(address), update adrp instruction.
5398 // R_AARCH64_ADR_PREL_PG_HI21
5400 static inline typename This::Status
5401 adrp(unsigned char* view,
5402 const Sized_relobj_file<size, big_endian>* object,
5403 const Symbol_value<size>* psymval,
5406 const AArch64_reloc_property* reloc_property)
5408 Address sa = psymval->value(object, addend);
5409 AArch64_valtype x = This::Page(sa) - This::Page(address);
5410 // Pick [32:12] of X.
5411 AArch64_valtype immed = (x >> 12) & 0x1fffff;
5412 update_adr(view, immed);
5413 return (reloc_property->checkup_x_value(x)
5415 : This::STATUS_OVERFLOW);
5418 // Update mov[n/z] instruction. Check overflow if needed.
5419 // If X >=0, set the instruction to movz and its immediate value to the
5421 // If X < 0, set the instruction to movn and its immediate value to
5422 // NOT (selected bits of).
5424 static inline typename This::Status
5425 movnz(unsigned char* view,
5427 const AArch64_reloc_property* reloc_property)
5429 // Select bits from X.
5432 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedW;
5433 if (static_cast<SignedW>(x) >= 0)
5435 immed = reloc_property->select_x_value(x);
5440 immed = reloc_property->select_x_value(~x);;
5444 // Update movnz instruction.
5445 update_movnz(view, immed, is_movz);
5447 // Do check overflow or alignment if needed.
5448 return (reloc_property->checkup_x_value(x)
5450 : This::STATUS_OVERFLOW);
5454 maybe_apply_stub(unsigned int,
5455 const The_relocate_info*,
5459 const Sized_symbol<size>*,
5460 const Symbol_value<size>*,
5461 const Sized_relobj_file<size, big_endian>*,
5464 }; // End of AArch64_relocate_functions
5467 // For a certain relocation type (usually jump/branch), test to see if the
5468 // destination needs a stub to fulfil. If so, re-route the destination of the
5469 // original instruction to the stub, note, at this time, the stub has already
5472 template<int size, bool big_endian>
5474 AArch64_relocate_functions<size, big_endian>::
5475 maybe_apply_stub(unsigned int r_type,
5476 const The_relocate_info* relinfo,
5477 const The_rela& rela,
5478 unsigned char* view,
5480 const Sized_symbol<size>* gsym,
5481 const Symbol_value<size>* psymval,
5482 const Sized_relobj_file<size, big_endian>* object,
5483 section_size_type current_group_size)
5485 if (parameters->options().relocatable())
5488 typename elfcpp::Elf_types<size>::Elf_Swxword addend = rela.get_r_addend();
5489 Address branch_target = psymval->value(object, 0) + addend;
5491 The_reloc_stub::stub_type_for_reloc(r_type, address, branch_target);
5492 if (stub_type == ST_NONE)
5495 const The_aarch64_relobj* aarch64_relobj =
5496 static_cast<const The_aarch64_relobj*>(object);
5497 const AArch64_reloc_property* arp =
5498 aarch64_reloc_property_table->get_reloc_property(r_type);
5499 gold_assert(arp != NULL);
5501 // We don't create stubs for undefined symbols, but do for weak.
5503 && !gsym->use_plt_offset(arp->reference_flags())
5504 && gsym->is_undefined())
5506 gold_debug(DEBUG_TARGET,
5507 "stub: looking for a stub for undefined symbol %s in file %s",
5508 gsym->name(), aarch64_relobj->name().c_str());
5512 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
5513 gold_assert(stub_table != NULL);
5515 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5516 typename The_reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
5517 The_reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
5518 gold_assert(stub != NULL);
5520 Address new_branch_target = stub_table->address() + stub->offset();
5521 typename elfcpp::Swap<size, big_endian>::Valtype branch_offset =
5522 new_branch_target - address;
5523 typename This::Status status = This::template
5524 rela_general<32>(view, branch_offset, 0, arp);
5525 if (status != This::STATUS_OKAY)
5526 gold_error(_("Stub is too far away, try a smaller value "
5527 "for '--stub-group-size'. The current value is 0x%lx."),
5528 static_cast<unsigned long>(current_group_size));
5533 // Group input sections for stub generation.
5535 // We group input sections in an output section so that the total size,
5536 // including any padding space due to alignment is smaller than GROUP_SIZE
5537 // unless the only input section in group is bigger than GROUP_SIZE already.
5538 // Then an ARM stub table is created to follow the last input section
5539 // in group. For each group an ARM stub table is created an is placed
5540 // after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
5541 // extend the group after the stub table.
5543 template<int size, bool big_endian>
5545 Target_aarch64<size, big_endian>::group_sections(
5547 section_size_type group_size,
5548 bool stubs_always_after_branch,
5551 // Group input sections and insert stub table
5552 Layout::Section_list section_list;
5553 layout->get_executable_sections(§ion_list);
5554 for (Layout::Section_list::const_iterator p = section_list.begin();
5555 p != section_list.end();
5558 AArch64_output_section<size, big_endian>* output_section =
5559 static_cast<AArch64_output_section<size, big_endian>*>(*p);
5560 output_section->group_sections(group_size, stubs_always_after_branch,
5566 // Find the AArch64_input_section object corresponding to the SHNDX-th input
5567 // section of RELOBJ.
5569 template<int size, bool big_endian>
5570 AArch64_input_section<size, big_endian>*
5571 Target_aarch64<size, big_endian>::find_aarch64_input_section(
5572 Relobj* relobj, unsigned int shndx) const
5574 Section_id sid(relobj, shndx);
5575 typename AArch64_input_section_map::const_iterator p =
5576 this->aarch64_input_section_map_.find(sid);
5577 return (p != this->aarch64_input_section_map_.end()) ? p->second : NULL;
5581 // Make a new AArch64_input_section object.
5583 template<int size, bool big_endian>
5584 AArch64_input_section<size, big_endian>*
5585 Target_aarch64<size, big_endian>::new_aarch64_input_section(
5586 Relobj* relobj, unsigned int shndx)
5588 Section_id sid(relobj, shndx);
5590 AArch64_input_section<size, big_endian>* input_section =
5591 new AArch64_input_section<size, big_endian>(relobj, shndx);
5592 input_section->init();
5594 // Register new AArch64_input_section in map for look-up.
5595 std::pair<typename AArch64_input_section_map::iterator,bool> ins =
5596 this->aarch64_input_section_map_.insert(
5597 std::make_pair(sid, input_section));
5599 // Make sure that it we have not created another AArch64_input_section
5600 // for this input section already.
5601 gold_assert(ins.second);
5603 return input_section;
5607 // Relaxation hook. This is where we do stub generation.
5609 template<int size, bool big_endian>
5611 Target_aarch64<size, big_endian>::do_relax(
5613 const Input_objects* input_objects,
5614 Symbol_table* symtab,
5618 gold_assert(!parameters->options().relocatable());
5621 // We don't handle negative stub_group_size right now.
5622 this->stub_group_size_ = abs(parameters->options().stub_group_size());
5623 if (this->stub_group_size_ == 1)
5625 // Leave room for 4096 4-byte stub entries. If we exceed that, then we
5626 // will fail to link. The user will have to relink with an explicit
5627 // group size option.
5628 this->stub_group_size_ = The_reloc_stub::MAX_BRANCH_OFFSET -
5631 group_sections(layout, this->stub_group_size_, true, task);
5635 // If this is not the first pass, addresses and file offsets have
5636 // been reset at this point, set them here.
5637 for (Stub_table_iterator sp = this->stub_tables_.begin();
5638 sp != this->stub_tables_.end(); ++sp)
5640 The_stub_table* stt = *sp;
5641 The_aarch64_input_section* owner = stt->owner();
5642 off_t off = align_address(owner->original_size(),
5644 stt->set_address_and_file_offset(owner->address() + off,
5645 owner->offset() + off);
5649 // Scan relocs for relocation stubs
5650 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
5651 op != input_objects->relobj_end();
5654 The_aarch64_relobj* aarch64_relobj =
5655 static_cast<The_aarch64_relobj*>(*op);
5656 // Lock the object so we can read from it. This is only called
5657 // single-threaded from Layout::finalize, so it is OK to lock.
5658 Task_lock_obj<Object> tl(task, aarch64_relobj);
5659 aarch64_relobj->scan_sections_for_stubs(this, symtab, layout);
5662 bool any_stub_table_changed = false;
5663 for (Stub_table_iterator siter = this->stub_tables_.begin();
5664 siter != this->stub_tables_.end() && !any_stub_table_changed; ++siter)
5666 The_stub_table* stub_table = *siter;
5667 if (stub_table->update_data_size_changed_p())
5669 The_aarch64_input_section* owner = stub_table->owner();
5670 uint64_t address = owner->address();
5671 off_t offset = owner->offset();
5672 owner->reset_address_and_file_offset();
5673 owner->set_address_and_file_offset(address, offset);
5675 any_stub_table_changed = true;
5679 // Do not continue relaxation.
5680 bool continue_relaxation = any_stub_table_changed;
5681 if (!continue_relaxation)
5682 for (Stub_table_iterator sp = this->stub_tables_.begin();
5683 (sp != this->stub_tables_.end());
5685 (*sp)->finalize_stubs();
5687 return continue_relaxation;
5691 // Make a new Stub_table.
5693 template<int size, bool big_endian>
5694 Stub_table<size, big_endian>*
5695 Target_aarch64<size, big_endian>::new_stub_table(
5696 AArch64_input_section<size, big_endian>* owner)
5698 Stub_table<size, big_endian>* stub_table =
5699 new Stub_table<size, big_endian>(owner);
5700 stub_table->set_address(align_address(
5701 owner->address() + owner->data_size(), 8));
5702 stub_table->set_file_offset(owner->offset() + owner->data_size());
5703 stub_table->finalize_data_size();
5705 this->stub_tables_.push_back(stub_table);
5711 template<int size, bool big_endian>
5713 Target_aarch64<size, big_endian>::do_reloc_addend(
5714 void* arg, unsigned int r_type, uint64_t) const
5716 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
5717 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5718 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5719 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5720 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5721 gold_assert(psymval->is_tls_symbol());
5722 // The value of a TLS symbol is the offset in the TLS segment.
5723 return psymval->value(ti.object, 0);
5726 // Return the number of entries in the PLT.
5728 template<int size, bool big_endian>
5730 Target_aarch64<size, big_endian>::plt_entry_count() const
5732 if (this->plt_ == NULL)
5734 return this->plt_->entry_count();
5737 // Return the offset of the first non-reserved PLT entry.
5739 template<int size, bool big_endian>
5741 Target_aarch64<size, big_endian>::first_plt_entry_offset() const
5743 return this->plt_->first_plt_entry_offset();
5746 // Return the size of each PLT entry.
5748 template<int size, bool big_endian>
5750 Target_aarch64<size, big_endian>::plt_entry_size() const
5752 return this->plt_->get_plt_entry_size();
5755 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
5757 template<int size, bool big_endian>
5759 Target_aarch64<size, big_endian>::define_tls_base_symbol(
5760 Symbol_table* symtab, Layout* layout)
5762 if (this->tls_base_symbol_defined_)
5765 Output_segment* tls_segment = layout->tls_segment();
5766 if (tls_segment != NULL)
5768 // _TLS_MODULE_BASE_ always points to the beginning of tls segment.
5769 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
5770 Symbol_table::PREDEFINED,
5774 elfcpp::STV_HIDDEN, 0,
5775 Symbol::SEGMENT_START,
5778 this->tls_base_symbol_defined_ = true;
5781 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
5783 template<int size, bool big_endian>
5785 Target_aarch64<size, big_endian>::reserve_tlsdesc_entries(
5786 Symbol_table* symtab, Layout* layout)
5788 if (this->plt_ == NULL)
5789 this->make_plt_section(symtab, layout);
5791 if (!this->plt_->has_tlsdesc_entry())
5793 // Allocate the TLSDESC_GOT entry.
5794 Output_data_got_aarch64<size, big_endian>* got =
5795 this->got_section(symtab, layout);
5796 unsigned int got_offset = got->add_constant(0);
5798 // Allocate the TLSDESC_PLT entry.
5799 this->plt_->reserve_tlsdesc_entry(got_offset);
5803 // Create a GOT entry for the TLS module index.
5805 template<int size, bool big_endian>
5807 Target_aarch64<size, big_endian>::got_mod_index_entry(
5808 Symbol_table* symtab, Layout* layout,
5809 Sized_relobj_file<size, big_endian>* object)
5811 if (this->got_mod_index_offset_ == -1U)
5813 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5814 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5815 Output_data_got_aarch64<size, big_endian>* got =
5816 this->got_section(symtab, layout);
5817 unsigned int got_offset = got->add_constant(0);
5818 rela_dyn->add_local(object, 0, elfcpp::R_AARCH64_TLS_DTPMOD64, got,
5820 got->add_constant(0);
5821 this->got_mod_index_offset_ = got_offset;
5823 return this->got_mod_index_offset_;
5826 // Optimize the TLS relocation type based on what we know about the
5827 // symbol. IS_FINAL is true if the final address of this symbol is
5828 // known at link time.
5830 template<int size, bool big_endian>
5831 tls::Tls_optimization
5832 Target_aarch64<size, big_endian>::optimize_tls_reloc(bool is_final,
5835 // If we are generating a shared library, then we can't do anything
5837 if (parameters->options().shared())
5838 return tls::TLSOPT_NONE;
5842 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5843 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5844 case elfcpp::R_AARCH64_TLSDESC_LD_PREL19:
5845 case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21:
5846 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5847 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5848 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5849 case elfcpp::R_AARCH64_TLSDESC_OFF_G1:
5850 case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC:
5851 case elfcpp::R_AARCH64_TLSDESC_LDR:
5852 case elfcpp::R_AARCH64_TLSDESC_ADD:
5853 case elfcpp::R_AARCH64_TLSDESC_CALL:
5854 // These are General-Dynamic which permits fully general TLS
5855 // access. Since we know that we are generating an executable,
5856 // we can convert this to Initial-Exec. If we also know that
5857 // this is a local symbol, we can further switch to Local-Exec.
5859 return tls::TLSOPT_TO_LE;
5860 return tls::TLSOPT_TO_IE;
5862 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5863 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5864 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5865 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5866 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5867 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5868 // These are Local-Dynamic, which refer to local symbols in the
5869 // dynamic TLS block. Since we know that we generating an
5870 // executable, we can switch to Local-Exec.
5871 return tls::TLSOPT_TO_LE;
5873 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
5874 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
5875 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5876 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5877 case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5878 // These are Initial-Exec relocs which get the thread offset
5879 // from the GOT. If we know that we are linking against the
5880 // local symbol, we can switch to Local-Exec, which links the
5881 // thread offset into the instruction.
5883 return tls::TLSOPT_TO_LE;
5884 return tls::TLSOPT_NONE;
5886 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5887 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5888 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5889 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5890 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5891 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5892 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5893 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5894 // When we already have Local-Exec, there is nothing further we
5896 return tls::TLSOPT_NONE;
5903 // Returns true if this relocation type could be that of a function pointer.
5905 template<int size, bool big_endian>
5907 Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
5908 unsigned int r_type)
5912 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
5913 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
5914 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
5915 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5916 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5924 // For safe ICF, scan a relocation for a local symbol to check if it
5925 // corresponds to a function pointer being taken. In that case mark
5926 // the function whose pointer was taken as not foldable.
5928 template<int size, bool big_endian>
5930 Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
5933 Target_aarch64<size, big_endian>* ,
5934 Sized_relobj_file<size, big_endian>* ,
5937 const elfcpp::Rela<size, big_endian>& ,
5938 unsigned int r_type,
5939 const elfcpp::Sym<size, big_endian>&)
5941 // When building a shared library, do not fold any local symbols.
5942 return (parameters->options().shared()
5943 || possible_function_pointer_reloc(r_type));
5946 // For safe ICF, scan a relocation for a global symbol to check if it
5947 // corresponds to a function pointer being taken. In that case mark
5948 // the function whose pointer was taken as not foldable.
5950 template<int size, bool big_endian>
5952 Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
5955 Target_aarch64<size, big_endian>* ,
5956 Sized_relobj_file<size, big_endian>* ,
5959 const elfcpp::Rela<size, big_endian>& ,
5960 unsigned int r_type,
5963 // When building a shared library, do not fold symbols whose visibility
5964 // is hidden, internal or protected.
5965 return ((parameters->options().shared()
5966 && (gsym->visibility() == elfcpp::STV_INTERNAL
5967 || gsym->visibility() == elfcpp::STV_PROTECTED
5968 || gsym->visibility() == elfcpp::STV_HIDDEN))
5969 || possible_function_pointer_reloc(r_type));
5972 // Report an unsupported relocation against a local symbol.
5974 template<int size, bool big_endian>
5976 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
5977 Sized_relobj_file<size, big_endian>* object,
5978 unsigned int r_type)
5980 gold_error(_("%s: unsupported reloc %u against local symbol"),
5981 object->name().c_str(), r_type);
5984 // We are about to emit a dynamic relocation of type R_TYPE. If the
5985 // dynamic linker does not support it, issue an error.
5987 template<int size, bool big_endian>
5989 Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
5990 unsigned int r_type)
5992 gold_assert(r_type != elfcpp::R_AARCH64_NONE);
5996 // These are the relocation types supported by glibc for AARCH64.
5997 case elfcpp::R_AARCH64_NONE:
5998 case elfcpp::R_AARCH64_COPY:
5999 case elfcpp::R_AARCH64_GLOB_DAT:
6000 case elfcpp::R_AARCH64_JUMP_SLOT:
6001 case elfcpp::R_AARCH64_RELATIVE:
6002 case elfcpp::R_AARCH64_TLS_DTPREL64:
6003 case elfcpp::R_AARCH64_TLS_DTPMOD64:
6004 case elfcpp::R_AARCH64_TLS_TPREL64:
6005 case elfcpp::R_AARCH64_TLSDESC:
6006 case elfcpp::R_AARCH64_IRELATIVE:
6007 case elfcpp::R_AARCH64_ABS32:
6008 case elfcpp::R_AARCH64_ABS64:
6015 // This prevents us from issuing more than one error per reloc
6016 // section. But we can still wind up issuing more than one
6017 // error per object file.
6018 if (this->issued_non_pic_error_)
6020 gold_assert(parameters->options().output_is_position_independent());
6021 object->error(_("requires unsupported dynamic reloc; "
6022 "recompile with -fPIC"));
6023 this->issued_non_pic_error_ = true;
6027 // Return whether we need to make a PLT entry for a relocation of the
6028 // given type against a STT_GNU_IFUNC symbol.
6030 template<int size, bool big_endian>
6032 Target_aarch64<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
6033 Sized_relobj_file<size, big_endian>* object,
6034 unsigned int r_type)
6036 const AArch64_reloc_property* arp =
6037 aarch64_reloc_property_table->get_reloc_property(r_type);
6038 gold_assert(arp != NULL);
6040 int flags = arp->reference_flags();
6041 if (flags & Symbol::TLS_REF)
6043 gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
6044 object->name().c_str(), arp->name().c_str());
6050 // Scan a relocation for a local symbol.
6052 template<int size, bool big_endian>
6054 Target_aarch64<size, big_endian>::Scan::local(
6055 Symbol_table* symtab,
6057 Target_aarch64<size, big_endian>* target,
6058 Sized_relobj_file<size, big_endian>* object,
6059 unsigned int data_shndx,
6060 Output_section* output_section,
6061 const elfcpp::Rela<size, big_endian>& rela,
6062 unsigned int r_type,
6063 const elfcpp::Sym<size, big_endian>& lsym,
6069 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
6071 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6073 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6074 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
6075 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
6076 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
6080 case elfcpp::R_AARCH64_NONE:
6083 case elfcpp::R_AARCH64_ABS32:
6084 case elfcpp::R_AARCH64_ABS16:
6085 if (parameters->options().output_is_position_independent())
6087 gold_error(_("%s: unsupported reloc %u in pos independent link."),
6088 object->name().c_str(), r_type);
6092 case elfcpp::R_AARCH64_ABS64:
6093 // If building a shared library or pie, we need to mark this as a dynmic
6094 // reloction, so that the dynamic loader can relocate it.
6095 if (parameters->options().output_is_position_independent())
6097 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6098 rela_dyn->add_local_relative(object, r_sym,
6099 elfcpp::R_AARCH64_RELATIVE,
6102 rela.get_r_offset(),
6103 rela.get_r_addend(),
6108 case elfcpp::R_AARCH64_PREL64:
6109 case elfcpp::R_AARCH64_PREL32:
6110 case elfcpp::R_AARCH64_PREL16:
6113 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6114 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6115 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
6116 // The above relocations are used to access GOT entries.
6118 Output_data_got_aarch64<size, big_endian>* got =
6119 target->got_section(symtab, layout);
6120 bool is_new = false;
6121 // This symbol requires a GOT entry.
6123 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
6125 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
6126 if (is_new && parameters->options().output_is_position_independent())
6127 target->rela_dyn_section(layout)->
6128 add_local_relative(object,
6130 elfcpp::R_AARCH64_RELATIVE,
6132 object->local_got_offset(r_sym,
6139 case elfcpp::R_AARCH64_MOVW_UABS_G0: // 263
6140 case elfcpp::R_AARCH64_MOVW_UABS_G0_NC: // 264
6141 case elfcpp::R_AARCH64_MOVW_UABS_G1: // 265
6142 case elfcpp::R_AARCH64_MOVW_UABS_G1_NC: // 266
6143 case elfcpp::R_AARCH64_MOVW_UABS_G2: // 267
6144 case elfcpp::R_AARCH64_MOVW_UABS_G2_NC: // 268
6145 case elfcpp::R_AARCH64_MOVW_UABS_G3: // 269
6146 case elfcpp::R_AARCH64_MOVW_SABS_G0: // 270
6147 case elfcpp::R_AARCH64_MOVW_SABS_G1: // 271
6148 case elfcpp::R_AARCH64_MOVW_SABS_G2: // 272
6149 if (parameters->options().output_is_position_independent())
6151 gold_error(_("%s: unsupported reloc %u in pos independent link."),
6152 object->name().c_str(), r_type);
6156 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
6157 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
6158 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
6159 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
6160 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
6161 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
6162 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
6163 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
6164 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
6165 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
6168 // Control flow, pc-relative. We don't need to do anything for a relative
6169 // addressing relocation against a local symbol if it does not reference
6171 case elfcpp::R_AARCH64_TSTBR14:
6172 case elfcpp::R_AARCH64_CONDBR19:
6173 case elfcpp::R_AARCH64_JUMP26:
6174 case elfcpp::R_AARCH64_CALL26:
6177 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6178 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6180 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6181 optimize_tls_reloc(!parameters->options().shared(), r_type);
6182 if (tlsopt == tls::TLSOPT_TO_LE)
6185 layout->set_has_static_tls();
6186 // Create a GOT entry for the tp-relative offset.
6187 if (!parameters->doing_static_link())
6189 Output_data_got_aarch64<size, big_endian>* got =
6190 target->got_section(symtab, layout);
6191 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
6192 target->rela_dyn_section(layout),
6193 elfcpp::R_AARCH64_TLS_TPREL64);
6195 else if (!object->local_has_got_offset(r_sym,
6196 GOT_TYPE_TLS_OFFSET))
6198 Output_data_got_aarch64<size, big_endian>* got =
6199 target->got_section(symtab, layout);
6200 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
6201 unsigned int got_offset =
6202 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
6203 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6204 gold_assert(addend == 0);
6205 got->add_static_reloc(got_offset, elfcpp::R_AARCH64_TLS_TPREL64,
6211 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6212 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6214 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6215 optimize_tls_reloc(!parameters->options().shared(), r_type);
6216 if (tlsopt == tls::TLSOPT_TO_LE)
6218 layout->set_has_static_tls();
6221 gold_assert(tlsopt == tls::TLSOPT_NONE);
6223 Output_data_got_aarch64<size, big_endian>* got =
6224 target->got_section(symtab, layout);
6225 got->add_local_pair_with_rel(object,r_sym, data_shndx,
6227 target->rela_dyn_section(layout),
6228 elfcpp::R_AARCH64_TLS_DTPMOD64);
6232 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6233 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6234 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6235 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6236 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6237 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6238 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6239 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6241 layout->set_has_static_tls();
6242 bool output_is_shared = parameters->options().shared();
6243 if (output_is_shared)
6244 gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
6245 object->name().c_str(), r_type);
6249 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6250 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
6252 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6253 optimize_tls_reloc(!parameters->options().shared(), r_type);
6254 if (tlsopt == tls::TLSOPT_NONE)
6256 // Create a GOT entry for the module index.
6257 target->got_mod_index_entry(symtab, layout, object);
6259 else if (tlsopt != tls::TLSOPT_TO_LE)
6260 unsupported_reloc_local(object, r_type);
6264 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6265 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6266 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6267 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
6270 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6271 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6272 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6274 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6275 optimize_tls_reloc(!parameters->options().shared(), r_type);
6276 target->define_tls_base_symbol(symtab, layout);
6277 if (tlsopt == tls::TLSOPT_NONE)
6279 // Create reserved PLT and GOT entries for the resolver.
6280 target->reserve_tlsdesc_entries(symtab, layout);
6282 // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
6283 // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
6284 // entry needs to be in an area in .got.plt, not .got. Call
6285 // got_section to make sure the section has been created.
6286 target->got_section(symtab, layout);
6287 Output_data_got<size, big_endian>* got =
6288 target->got_tlsdesc_section();
6289 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6290 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
6292 unsigned int got_offset = got->add_constant(0);
6293 got->add_constant(0);
6294 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
6296 Reloc_section* rt = target->rela_tlsdesc_section(layout);
6297 // We store the arguments we need in a vector, and use
6298 // the index into the vector as the parameter to pass
6299 // to the target specific routines.
6300 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
6301 void* arg = reinterpret_cast<void*>(intarg);
6302 rt->add_target_specific(elfcpp::R_AARCH64_TLSDESC, arg,
6303 got, got_offset, 0);
6306 else if (tlsopt != tls::TLSOPT_TO_LE)
6307 unsupported_reloc_local(object, r_type);
6311 case elfcpp::R_AARCH64_TLSDESC_CALL:
6315 unsupported_reloc_local(object, r_type);
6320 // Report an unsupported relocation against a global symbol.
6322 template<int size, bool big_endian>
6324 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
6325 Sized_relobj_file<size, big_endian>* object,
6326 unsigned int r_type,
6329 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6330 object->name().c_str(), r_type, gsym->demangled_name().c_str());
6333 template<int size, bool big_endian>
6335 Target_aarch64<size, big_endian>::Scan::global(
6336 Symbol_table* symtab,
6338 Target_aarch64<size, big_endian>* target,
6339 Sized_relobj_file<size, big_endian> * object,
6340 unsigned int data_shndx,
6341 Output_section* output_section,
6342 const elfcpp::Rela<size, big_endian>& rela,
6343 unsigned int r_type,
6346 // A STT_GNU_IFUNC symbol may require a PLT entry.
6347 if (gsym->type() == elfcpp::STT_GNU_IFUNC
6348 && this->reloc_needs_plt_for_ifunc(object, r_type))
6349 target->make_plt_entry(symtab, layout, gsym);
6351 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
6353 const AArch64_reloc_property* arp =
6354 aarch64_reloc_property_table->get_reloc_property(r_type);
6355 gold_assert(arp != NULL);
6359 case elfcpp::R_AARCH64_NONE:
6362 case elfcpp::R_AARCH64_ABS16:
6363 case elfcpp::R_AARCH64_ABS32:
6364 case elfcpp::R_AARCH64_ABS64:
6366 // Make a PLT entry if necessary.
6367 if (gsym->needs_plt_entry())
6369 target->make_plt_entry(symtab, layout, gsym);
6370 // Since this is not a PC-relative relocation, we may be
6371 // taking the address of a function. In that case we need to
6372 // set the entry in the dynamic symbol table to the address of
6374 if (gsym->is_from_dynobj() && !parameters->options().shared())
6375 gsym->set_needs_dynsym_value();
6377 // Make a dynamic relocation if necessary.
6378 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
6380 if (!parameters->options().output_is_position_independent()
6381 && gsym->may_need_copy_reloc())
6383 target->copy_reloc(symtab, layout, object,
6384 data_shndx, output_section, gsym, rela);
6386 else if (r_type == elfcpp::R_AARCH64_ABS64
6387 && gsym->type() == elfcpp::STT_GNU_IFUNC
6388 && gsym->can_use_relative_reloc(false)
6389 && !gsym->is_from_dynobj()
6390 && !gsym->is_undefined()
6391 && !gsym->is_preemptible())
6393 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
6394 // symbol. This makes a function address in a PIE executable
6395 // match the address in a shared library that it links against.
6396 Reloc_section* rela_dyn =
6397 target->rela_irelative_section(layout);
6398 unsigned int r_type = elfcpp::R_AARCH64_IRELATIVE;
6399 rela_dyn->add_symbolless_global_addend(gsym, r_type,
6400 output_section, object,
6402 rela.get_r_offset(),
6403 rela.get_r_addend());
6405 else if (r_type == elfcpp::R_AARCH64_ABS64
6406 && gsym->can_use_relative_reloc(false))
6408 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6409 rela_dyn->add_global_relative(gsym,
6410 elfcpp::R_AARCH64_RELATIVE,
6414 rela.get_r_offset(),
6415 rela.get_r_addend(),
6420 check_non_pic(object, r_type);
6421 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*
6422 rela_dyn = target->rela_dyn_section(layout);
6423 rela_dyn->add_global(
6424 gsym, r_type, output_section, object,
6425 data_shndx, rela.get_r_offset(),rela.get_r_addend());
6431 case elfcpp::R_AARCH64_PREL16:
6432 case elfcpp::R_AARCH64_PREL32:
6433 case elfcpp::R_AARCH64_PREL64:
6434 // This is used to fill the GOT absolute address.
6435 if (gsym->needs_plt_entry())
6437 target->make_plt_entry(symtab, layout, gsym);
6441 case elfcpp::R_AARCH64_MOVW_UABS_G0: // 263
6442 case elfcpp::R_AARCH64_MOVW_UABS_G0_NC: // 264
6443 case elfcpp::R_AARCH64_MOVW_UABS_G1: // 265
6444 case elfcpp::R_AARCH64_MOVW_UABS_G1_NC: // 266
6445 case elfcpp::R_AARCH64_MOVW_UABS_G2: // 267
6446 case elfcpp::R_AARCH64_MOVW_UABS_G2_NC: // 268
6447 case elfcpp::R_AARCH64_MOVW_UABS_G3: // 269
6448 case elfcpp::R_AARCH64_MOVW_SABS_G0: // 270
6449 case elfcpp::R_AARCH64_MOVW_SABS_G1: // 271
6450 case elfcpp::R_AARCH64_MOVW_SABS_G2: // 272
6451 if (parameters->options().output_is_position_independent())
6453 gold_error(_("%s: unsupported reloc %u in pos independent link."),
6454 object->name().c_str(), r_type);
6458 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
6459 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
6460 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
6461 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
6462 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
6463 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
6464 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
6465 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
6466 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
6467 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
6469 if (gsym->needs_plt_entry())
6470 target->make_plt_entry(symtab, layout, gsym);
6471 // Make a dynamic relocation if necessary.
6472 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
6474 if (parameters->options().output_is_executable()
6475 && gsym->may_need_copy_reloc())
6477 target->copy_reloc(symtab, layout, object,
6478 data_shndx, output_section, gsym, rela);
6484 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6485 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6486 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
6488 // The above relocations are used to access GOT entries.
6489 // Note a GOT entry is an *address* to a symbol.
6490 // The symbol requires a GOT entry
6491 Output_data_got_aarch64<size, big_endian>* got =
6492 target->got_section(symtab, layout);
6493 if (gsym->final_value_is_known())
6495 // For a STT_GNU_IFUNC symbol we want the PLT address.
6496 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
6497 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6499 got->add_global(gsym, GOT_TYPE_STANDARD);
6503 // If this symbol is not fully resolved, we need to add a dynamic
6504 // relocation for it.
6505 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6507 // Use a GLOB_DAT rather than a RELATIVE reloc if:
6509 // 1) The symbol may be defined in some other module.
6510 // 2) We are building a shared library and this is a protected
6511 // symbol; using GLOB_DAT means that the dynamic linker can use
6512 // the address of the PLT in the main executable when appropriate
6513 // so that function address comparisons work.
6514 // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
6515 // again so that function address comparisons work.
6516 if (gsym->is_from_dynobj()
6517 || gsym->is_undefined()
6518 || gsym->is_preemptible()
6519 || (gsym->visibility() == elfcpp::STV_PROTECTED
6520 && parameters->options().shared())
6521 || (gsym->type() == elfcpp::STT_GNU_IFUNC
6522 && parameters->options().output_is_position_independent()))
6523 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
6524 rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
6527 // For a STT_GNU_IFUNC symbol we want to write the PLT
6528 // offset into the GOT, so that function pointer
6529 // comparisons work correctly.
6531 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
6532 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
6535 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6536 // Tell the dynamic linker to use the PLT address
6537 // when resolving relocations.
6538 if (gsym->is_from_dynobj()
6539 && !parameters->options().shared())
6540 gsym->set_needs_dynsym_value();
6544 rela_dyn->add_global_relative(
6545 gsym, elfcpp::R_AARCH64_RELATIVE,
6547 gsym->got_offset(GOT_TYPE_STANDARD),
6556 case elfcpp::R_AARCH64_TSTBR14:
6557 case elfcpp::R_AARCH64_CONDBR19:
6558 case elfcpp::R_AARCH64_JUMP26:
6559 case elfcpp::R_AARCH64_CALL26:
6561 if (gsym->final_value_is_known())
6564 if (gsym->is_defined() &&
6565 !gsym->is_from_dynobj() &&
6566 !gsym->is_preemptible())
6569 // Make plt entry for function call.
6570 target->make_plt_entry(symtab, layout, gsym);
6574 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6575 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // General dynamic
6577 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6578 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6579 if (tlsopt == tls::TLSOPT_TO_LE)
6581 layout->set_has_static_tls();
6584 gold_assert(tlsopt == tls::TLSOPT_NONE);
6587 Output_data_got_aarch64<size, big_endian>* got =
6588 target->got_section(symtab, layout);
6589 // Create 2 consecutive entries for module index and offset.
6590 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
6591 target->rela_dyn_section(layout),
6592 elfcpp::R_AARCH64_TLS_DTPMOD64,
6593 elfcpp::R_AARCH64_TLS_DTPREL64);
6597 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6598 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local dynamic
6600 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6601 optimize_tls_reloc(!parameters->options().shared(), r_type);
6602 if (tlsopt == tls::TLSOPT_NONE)
6604 // Create a GOT entry for the module index.
6605 target->got_mod_index_entry(symtab, layout, object);
6607 else if (tlsopt != tls::TLSOPT_TO_LE)
6608 unsupported_reloc_local(object, r_type);
6612 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6613 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6614 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6615 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local dynamic
6618 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6619 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial executable
6621 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6622 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6623 if (tlsopt == tls::TLSOPT_TO_LE)
6626 layout->set_has_static_tls();
6627 // Create a GOT entry for the tp-relative offset.
6628 Output_data_got_aarch64<size, big_endian>* got
6629 = target->got_section(symtab, layout);
6630 if (!parameters->doing_static_link())
6632 got->add_global_with_rel(
6633 gsym, GOT_TYPE_TLS_OFFSET,
6634 target->rela_dyn_section(layout),
6635 elfcpp::R_AARCH64_TLS_TPREL64);
6637 if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
6639 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
6640 unsigned int got_offset =
6641 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
6642 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6643 gold_assert(addend == 0);
6644 got->add_static_reloc(got_offset,
6645 elfcpp::R_AARCH64_TLS_TPREL64, gsym);
6650 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6651 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6652 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6653 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6654 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6655 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6656 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6657 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: // Local executable
6658 layout->set_has_static_tls();
6659 if (parameters->options().shared())
6660 gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
6661 object->name().c_str(), r_type);
6664 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6665 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6666 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12: // TLS descriptor
6668 target->define_tls_base_symbol(symtab, layout);
6669 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6670 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6671 if (tlsopt == tls::TLSOPT_NONE)
6673 // Create reserved PLT and GOT entries for the resolver.
6674 target->reserve_tlsdesc_entries(symtab, layout);
6676 // Create a double GOT entry with an R_AARCH64_TLSDESC
6677 // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
6678 // entry needs to be in an area in .got.plt, not .got. Call
6679 // got_section to make sure the section has been created.
6680 target->got_section(symtab, layout);
6681 Output_data_got<size, big_endian>* got =
6682 target->got_tlsdesc_section();
6683 Reloc_section* rt = target->rela_tlsdesc_section(layout);
6684 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
6685 elfcpp::R_AARCH64_TLSDESC, 0);
6687 else if (tlsopt == tls::TLSOPT_TO_IE)
6689 // Create a GOT entry for the tp-relative offset.
6690 Output_data_got<size, big_endian>* got
6691 = target->got_section(symtab, layout);
6692 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
6693 target->rela_dyn_section(layout),
6694 elfcpp::R_AARCH64_TLS_TPREL64);
6696 else if (tlsopt != tls::TLSOPT_TO_LE)
6697 unsupported_reloc_global(object, r_type, gsym);
6701 case elfcpp::R_AARCH64_TLSDESC_CALL:
6705 gold_error(_("%s: unsupported reloc type in global scan"),
6706 aarch64_reloc_property_table->
6707 reloc_name_in_error_message(r_type).c_str());
6710 } // End of Scan::global
6713 // Create the PLT section.
6714 template<int size, bool big_endian>
6716 Target_aarch64<size, big_endian>::make_plt_section(
6717 Symbol_table* symtab, Layout* layout)
6719 if (this->plt_ == NULL)
6721 // Create the GOT section first.
6722 this->got_section(symtab, layout);
6724 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
6725 this->got_irelative_);
6727 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
6729 | elfcpp::SHF_EXECINSTR),
6730 this->plt_, ORDER_PLT, false);
6732 // Make the sh_info field of .rela.plt point to .plt.
6733 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
6734 rela_plt_os->set_info_section(this->plt_->output_section());
6738 // Return the section for TLSDESC relocations.
6740 template<int size, bool big_endian>
6741 typename Target_aarch64<size, big_endian>::Reloc_section*
6742 Target_aarch64<size, big_endian>::rela_tlsdesc_section(Layout* layout) const
6744 return this->plt_section()->rela_tlsdesc(layout);
6747 // Create a PLT entry for a global symbol.
6749 template<int size, bool big_endian>
6751 Target_aarch64<size, big_endian>::make_plt_entry(
6752 Symbol_table* symtab,
6756 if (gsym->has_plt_offset())
6759 if (this->plt_ == NULL)
6760 this->make_plt_section(symtab, layout);
6762 this->plt_->add_entry(symtab, layout, gsym);
6765 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6767 template<int size, bool big_endian>
6769 Target_aarch64<size, big_endian>::make_local_ifunc_plt_entry(
6770 Symbol_table* symtab, Layout* layout,
6771 Sized_relobj_file<size, big_endian>* relobj,
6772 unsigned int local_sym_index)
6774 if (relobj->local_has_plt_offset(local_sym_index))
6776 if (this->plt_ == NULL)
6777 this->make_plt_section(symtab, layout);
6778 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
6781 relobj->set_local_plt_offset(local_sym_index, plt_offset);
6784 template<int size, bool big_endian>
6786 Target_aarch64<size, big_endian>::gc_process_relocs(
6787 Symbol_table* symtab,
6789 Sized_relobj_file<size, big_endian>* object,
6790 unsigned int data_shndx,
6791 unsigned int sh_type,
6792 const unsigned char* prelocs,
6794 Output_section* output_section,
6795 bool needs_special_offset_handling,
6796 size_t local_symbol_count,
6797 const unsigned char* plocal_symbols)
6799 typedef Target_aarch64<size, big_endian> Aarch64;
6800 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
6803 if (sh_type == elfcpp::SHT_REL)
6808 gold::gc_process_relocs<size, big_endian, Aarch64, Scan, Classify_reloc>(
6817 needs_special_offset_handling,
6822 // Scan relocations for a section.
6824 template<int size, bool big_endian>
6826 Target_aarch64<size, big_endian>::scan_relocs(
6827 Symbol_table* symtab,
6829 Sized_relobj_file<size, big_endian>* object,
6830 unsigned int data_shndx,
6831 unsigned int sh_type,
6832 const unsigned char* prelocs,
6834 Output_section* output_section,
6835 bool needs_special_offset_handling,
6836 size_t local_symbol_count,
6837 const unsigned char* plocal_symbols)
6839 typedef Target_aarch64<size, big_endian> Aarch64;
6840 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
6843 if (sh_type == elfcpp::SHT_REL)
6845 gold_error(_("%s: unsupported REL reloc section"),
6846 object->name().c_str());
6850 gold::scan_relocs<size, big_endian, Aarch64, Scan, Classify_reloc>(
6859 needs_special_offset_handling,
6864 // Return the value to use for a dynamic which requires special
6865 // treatment. This is how we support equality comparisons of function
6866 // pointers across shared library boundaries, as described in the
6867 // processor specific ABI supplement.
6869 template<int size, bool big_endian>
6871 Target_aarch64<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
6873 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
6874 return this->plt_address_for_global(gsym);
6878 // Finalize the sections.
6880 template<int size, bool big_endian>
6882 Target_aarch64<size, big_endian>::do_finalize_sections(
6884 const Input_objects*,
6885 Symbol_table* symtab)
6887 const Reloc_section* rel_plt = (this->plt_ == NULL
6889 : this->plt_->rela_plt());
6890 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
6891 this->rela_dyn_, true, false);
6893 // Emit any relocs we saved in an attempt to avoid generating COPY
6895 if (this->copy_relocs_.any_saved_relocs())
6896 this->copy_relocs_.emit(this->rela_dyn_section(layout));
6898 // Fill in some more dynamic tags.
6899 Output_data_dynamic* const odyn = layout->dynamic_data();
6902 if (this->plt_ != NULL
6903 && this->plt_->output_section() != NULL
6904 && this->plt_ ->has_tlsdesc_entry())
6906 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
6907 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
6908 this->got_->finalize_data_size();
6909 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
6910 this->plt_, plt_offset);
6911 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
6912 this->got_, got_offset);
6916 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
6917 // the .got.plt section.
6918 Symbol* sym = this->global_offset_table_;
6921 uint64_t data_size = this->got_plt_->current_data_size();
6922 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
6924 // If the .got section is more than 0x8000 bytes, we add
6925 // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
6926 // bit relocations have a greater chance of working.
6927 if (data_size >= 0x8000)
6928 symtab->get_sized_symbol<size>(sym)->set_value(
6929 symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
6932 if (parameters->doing_static_link()
6933 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
6935 // If linking statically, make sure that the __rela_iplt symbols
6936 // were defined if necessary, even if we didn't create a PLT.
6937 static const Define_symbol_in_segment syms[] =
6940 "__rela_iplt_start", // name
6941 elfcpp::PT_LOAD, // segment_type
6942 elfcpp::PF_W, // segment_flags_set
6943 elfcpp::PF(0), // segment_flags_clear
6946 elfcpp::STT_NOTYPE, // type
6947 elfcpp::STB_GLOBAL, // binding
6948 elfcpp::STV_HIDDEN, // visibility
6950 Symbol::SEGMENT_START, // offset_from_base
6954 "__rela_iplt_end", // name
6955 elfcpp::PT_LOAD, // segment_type
6956 elfcpp::PF_W, // segment_flags_set
6957 elfcpp::PF(0), // segment_flags_clear
6960 elfcpp::STT_NOTYPE, // type
6961 elfcpp::STB_GLOBAL, // binding
6962 elfcpp::STV_HIDDEN, // visibility
6964 Symbol::SEGMENT_START, // offset_from_base
6969 symtab->define_symbols(layout, 2, syms,
6970 layout->script_options()->saw_sections_clause());
6976 // Perform a relocation.
6978 template<int size, bool big_endian>
6980 Target_aarch64<size, big_endian>::Relocate::relocate(
6981 const Relocate_info<size, big_endian>* relinfo,
6983 Target_aarch64<size, big_endian>* target,
6986 const unsigned char* preloc,
6987 const Sized_symbol<size>* gsym,
6988 const Symbol_value<size>* psymval,
6989 unsigned char* view,
6990 typename elfcpp::Elf_types<size>::Elf_Addr address,
6991 section_size_type /* view_size */)
6996 typedef AArch64_relocate_functions<size, big_endian> Reloc;
6998 const elfcpp::Rela<size, big_endian> rela(preloc);
6999 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
7000 const AArch64_reloc_property* reloc_property =
7001 aarch64_reloc_property_table->get_reloc_property(r_type);
7003 if (reloc_property == NULL)
7005 std::string reloc_name =
7006 aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
7007 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7008 _("cannot relocate %s in object file"),
7009 reloc_name.c_str());
7013 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
7015 // Pick the value to use for symbols defined in the PLT.
7016 Symbol_value<size> symval;
7018 && gsym->use_plt_offset(reloc_property->reference_flags()))
7020 symval.set_output_value(target->plt_address_for_global(gsym));
7023 else if (gsym == NULL && psymval->is_ifunc_symbol())
7025 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7026 if (object->local_has_plt_offset(r_sym))
7028 symval.set_output_value(target->plt_address_for_local(object, r_sym));
7033 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7035 // Get the GOT offset if needed.
7036 // For aarch64, the GOT pointer points to the start of the GOT section.
7037 bool have_got_offset = false;
7039 int got_base = (target->got_ != NULL
7040 ? (target->got_->current_data_size() >= 0x8000
7045 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
7046 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
7047 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
7048 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
7049 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
7050 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
7051 case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
7052 case elfcpp::R_AARCH64_GOTREL64:
7053 case elfcpp::R_AARCH64_GOTREL32:
7054 case elfcpp::R_AARCH64_GOT_LD_PREL19:
7055 case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
7056 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
7057 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
7058 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
7061 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
7062 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
7066 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7067 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
7068 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
7071 have_got_offset = true;
7078 typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
7079 typename elfcpp::Elf_types<size>::Elf_Addr value;
7082 case elfcpp::R_AARCH64_NONE:
7085 case elfcpp::R_AARCH64_ABS64:
7086 if (!parameters->options().apply_dynamic_relocs()
7087 && parameters->options().output_is_position_independent()
7089 && gsym->needs_dynamic_reloc(reloc_property->reference_flags())
7090 && !gsym->can_use_relative_reloc(false))
7091 // We have generated an absolute dynamic relocation, so do not
7092 // apply the relocation statically. (Works around bugs in older
7093 // Android dynamic linkers.)
7095 reloc_status = Reloc::template rela_ua<64>(
7096 view, object, psymval, addend, reloc_property);
7099 case elfcpp::R_AARCH64_ABS32:
7100 if (!parameters->options().apply_dynamic_relocs()
7101 && parameters->options().output_is_position_independent()
7103 && gsym->needs_dynamic_reloc(reloc_property->reference_flags()))
7104 // We have generated an absolute dynamic relocation, so do not
7105 // apply the relocation statically. (Works around bugs in older
7106 // Android dynamic linkers.)
7108 reloc_status = Reloc::template rela_ua<32>(
7109 view, object, psymval, addend, reloc_property);
7112 case elfcpp::R_AARCH64_ABS16:
7113 if (!parameters->options().apply_dynamic_relocs()
7114 && parameters->options().output_is_position_independent()
7116 && gsym->needs_dynamic_reloc(reloc_property->reference_flags()))
7117 // We have generated an absolute dynamic relocation, so do not
7118 // apply the relocation statically. (Works around bugs in older
7119 // Android dynamic linkers.)
7121 reloc_status = Reloc::template rela_ua<16>(
7122 view, object, psymval, addend, reloc_property);
7125 case elfcpp::R_AARCH64_PREL64:
7126 reloc_status = Reloc::template pcrela_ua<64>(
7127 view, object, psymval, addend, address, reloc_property);
7130 case elfcpp::R_AARCH64_PREL32:
7131 reloc_status = Reloc::template pcrela_ua<32>(
7132 view, object, psymval, addend, address, reloc_property);
7135 case elfcpp::R_AARCH64_PREL16:
7136 reloc_status = Reloc::template pcrela_ua<16>(
7137 view, object, psymval, addend, address, reloc_property);
7140 case elfcpp::R_AARCH64_MOVW_UABS_G0:
7141 case elfcpp::R_AARCH64_MOVW_UABS_G0_NC:
7142 case elfcpp::R_AARCH64_MOVW_UABS_G1:
7143 case elfcpp::R_AARCH64_MOVW_UABS_G1_NC:
7144 case elfcpp::R_AARCH64_MOVW_UABS_G2:
7145 case elfcpp::R_AARCH64_MOVW_UABS_G2_NC:
7146 case elfcpp::R_AARCH64_MOVW_UABS_G3:
7147 reloc_status = Reloc::template rela_general<32>(
7148 view, object, psymval, addend, reloc_property);
7150 case elfcpp::R_AARCH64_MOVW_SABS_G0:
7151 case elfcpp::R_AARCH64_MOVW_SABS_G1:
7152 case elfcpp::R_AARCH64_MOVW_SABS_G2:
7153 reloc_status = Reloc::movnz(view, psymval->value(object, addend),
7157 case elfcpp::R_AARCH64_LD_PREL_LO19:
7158 reloc_status = Reloc::template pcrela_general<32>(
7159 view, object, psymval, addend, address, reloc_property);
7162 case elfcpp::R_AARCH64_ADR_PREL_LO21:
7163 reloc_status = Reloc::adr(view, object, psymval, addend,
7164 address, reloc_property);
7167 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
7168 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
7169 reloc_status = Reloc::adrp(view, object, psymval, addend, address,
7173 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
7174 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
7175 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
7176 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
7177 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
7178 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
7179 reloc_status = Reloc::template rela_general<32>(
7180 view, object, psymval, addend, reloc_property);
7183 case elfcpp::R_AARCH64_CALL26:
7184 if (this->skip_call_tls_get_addr_)
7186 // Double check that the TLSGD insn has been optimized away.
7187 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7188 Insntype insn = elfcpp::Swap<32, big_endian>::readval(
7189 reinterpret_cast<Insntype*>(view));
7190 gold_assert((insn & 0xff000000) == 0x91000000);
7192 reloc_status = Reloc::STATUS_OKAY;
7193 this->skip_call_tls_get_addr_ = false;
7194 // Return false to stop further processing this reloc.
7198 case elfcpp::R_AARCH64_JUMP26:
7199 if (Reloc::maybe_apply_stub(r_type, relinfo, rela, view, address,
7200 gsym, psymval, object,
7201 target->stub_group_size_))
7204 case elfcpp::R_AARCH64_TSTBR14:
7205 case elfcpp::R_AARCH64_CONDBR19:
7206 reloc_status = Reloc::template pcrela_general<32>(
7207 view, object, psymval, addend, address, reloc_property);
7210 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
7211 gold_assert(have_got_offset);
7212 value = target->got_->address() + got_base + got_offset;
7213 reloc_status = Reloc::adrp(view, value + addend, address);
7216 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
7217 gold_assert(have_got_offset);
7218 value = target->got_->address() + got_base + got_offset;
7219 reloc_status = Reloc::template rela_general<32>(
7220 view, value, addend, reloc_property);
7223 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
7225 gold_assert(have_got_offset);
7226 value = target->got_->address() + got_base + got_offset + addend -
7227 Reloc::Page(target->got_->address() + got_base);
7228 if ((value & 7) != 0)
7229 reloc_status = Reloc::STATUS_OVERFLOW;
7231 reloc_status = Reloc::template reloc_common<32>(
7232 view, value, reloc_property);
7236 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
7237 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
7238 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
7239 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
7240 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
7241 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7242 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
7243 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
7244 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7245 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7246 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
7247 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
7248 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7249 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7250 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
7251 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
7252 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
7253 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
7254 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7255 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7256 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7257 case elfcpp::R_AARCH64_TLSDESC_CALL:
7258 reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
7259 gsym, psymval, view, address);
7262 // These are dynamic relocations, which are unexpected when linking.
7263 case elfcpp::R_AARCH64_COPY:
7264 case elfcpp::R_AARCH64_GLOB_DAT:
7265 case elfcpp::R_AARCH64_JUMP_SLOT:
7266 case elfcpp::R_AARCH64_RELATIVE:
7267 case elfcpp::R_AARCH64_IRELATIVE:
7268 case elfcpp::R_AARCH64_TLS_DTPREL64:
7269 case elfcpp::R_AARCH64_TLS_DTPMOD64:
7270 case elfcpp::R_AARCH64_TLS_TPREL64:
7271 case elfcpp::R_AARCH64_TLSDESC:
7272 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7273 _("unexpected reloc %u in object file"),
7278 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7279 _("unsupported reloc %s"),
7280 reloc_property->name().c_str());
7284 // Report any errors.
7285 switch (reloc_status)
7287 case Reloc::STATUS_OKAY:
7289 case Reloc::STATUS_OVERFLOW:
7290 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7291 _("relocation overflow in %s"),
7292 reloc_property->name().c_str());
7294 case Reloc::STATUS_BAD_RELOC:
7295 gold_error_at_location(
7298 rela.get_r_offset(),
7299 _("unexpected opcode while processing relocation %s"),
7300 reloc_property->name().c_str());
7310 template<int size, bool big_endian>
7312 typename AArch64_relocate_functions<size, big_endian>::Status
7313 Target_aarch64<size, big_endian>::Relocate::relocate_tls(
7314 const Relocate_info<size, big_endian>* relinfo,
7315 Target_aarch64<size, big_endian>* target,
7317 const elfcpp::Rela<size, big_endian>& rela,
7318 unsigned int r_type, const Sized_symbol<size>* gsym,
7319 const Symbol_value<size>* psymval,
7320 unsigned char* view,
7321 typename elfcpp::Elf_types<size>::Elf_Addr address)
7323 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7324 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7326 Output_segment* tls_segment = relinfo->layout->tls_segment();
7327 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7328 const AArch64_reloc_property* reloc_property =
7329 aarch64_reloc_property_table->get_reloc_property(r_type);
7330 gold_assert(reloc_property != NULL);
7332 const bool is_final = (gsym == NULL
7333 ? !parameters->options().shared()
7334 : gsym->final_value_is_known());
7335 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
7336 optimize_tls_reloc(is_final, r_type);
7338 Sized_relobj_file<size, big_endian>* object = relinfo->object;
7339 int tls_got_offset_type;
7342 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
7343 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // Global-dynamic
7345 if (tlsopt == tls::TLSOPT_TO_LE)
7347 if (tls_segment == NULL)
7349 gold_assert(parameters->errors()->error_count() > 0
7350 || issue_undefined_symbol_error(gsym));
7351 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7353 return tls_gd_to_le(relinfo, target, rela, r_type, view,
7356 else if (tlsopt == tls::TLSOPT_NONE)
7358 tls_got_offset_type = GOT_TYPE_TLS_PAIR;
7359 // Firstly get the address for the got entry.
7360 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7363 gold_assert(gsym->has_got_offset(tls_got_offset_type));
7364 got_entry_address = target->got_->address() +
7365 gsym->got_offset(tls_got_offset_type);
7369 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7371 object->local_has_got_offset(r_sym, tls_got_offset_type));
7372 got_entry_address = target->got_->address() +
7373 object->local_got_offset(r_sym, tls_got_offset_type);
7376 // Relocate the address into adrp/ld, adrp/add pair.
7379 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
7380 return aarch64_reloc_funcs::adrp(
7381 view, got_entry_address + addend, address);
7385 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
7386 return aarch64_reloc_funcs::template rela_general<32>(
7387 view, got_entry_address, addend, reloc_property);
7394 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7395 _("unsupported gd_to_ie relaxation on %u"),
7400 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
7401 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local-dynamic
7403 if (tlsopt == tls::TLSOPT_TO_LE)
7405 if (tls_segment == NULL)
7407 gold_assert(parameters->errors()->error_count() > 0
7408 || issue_undefined_symbol_error(gsym));
7409 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7411 return this->tls_ld_to_le(relinfo, target, rela, r_type, view,
7415 gold_assert(tlsopt == tls::TLSOPT_NONE);
7416 // Relocate the field with the offset of the GOT entry for
7417 // the module index.
7418 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7419 got_entry_address = (target->got_mod_index_entry(NULL, NULL, NULL) +
7420 target->got_->address());
7424 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
7425 return aarch64_reloc_funcs::adrp(
7426 view, got_entry_address + addend, address);
7429 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
7430 return aarch64_reloc_funcs::template rela_general<32>(
7431 view, got_entry_address, addend, reloc_property);
7440 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
7441 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7442 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
7443 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local-dynamic
7445 AArch64_address value = psymval->value(object, 0);
7446 if (tlsopt == tls::TLSOPT_TO_LE)
7448 if (tls_segment == NULL)
7450 gold_assert(parameters->errors()->error_count() > 0
7451 || issue_undefined_symbol_error(gsym));
7452 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7457 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
7458 return aarch64_reloc_funcs::movnz(view, value + addend,
7462 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
7463 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
7464 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
7465 return aarch64_reloc_funcs::template rela_general<32>(
7466 view, value, addend, reloc_property);
7472 // We should never reach here.
7476 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7477 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial-exec
7479 if (tlsopt == tls::TLSOPT_TO_LE)
7481 if (tls_segment == NULL)
7483 gold_assert(parameters->errors()->error_count() > 0
7484 || issue_undefined_symbol_error(gsym));
7485 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7487 return tls_ie_to_le(relinfo, target, rela, r_type, view,
7490 tls_got_offset_type = GOT_TYPE_TLS_OFFSET;
7492 // Firstly get the address for the got entry.
7493 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7496 gold_assert(gsym->has_got_offset(tls_got_offset_type));
7497 got_entry_address = target->got_->address() +
7498 gsym->got_offset(tls_got_offset_type);
7502 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7504 object->local_has_got_offset(r_sym, tls_got_offset_type));
7505 got_entry_address = target->got_->address() +
7506 object->local_got_offset(r_sym, tls_got_offset_type);
7508 // Relocate the address into adrp/ld, adrp/add pair.
7511 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
7512 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
7515 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
7516 return aarch64_reloc_funcs::template rela_general<32>(
7517 view, got_entry_address, addend, reloc_property);
7522 // We shall never reach here.
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_G1_NC:
7528 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7529 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
7530 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
7531 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
7532 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
7534 gold_assert(tls_segment != NULL);
7535 AArch64_address value = psymval->value(object, 0);
7537 if (!parameters->options().shared())
7539 AArch64_address aligned_tcb_size =
7540 align_address(target->tcb_size(),
7541 tls_segment->maximum_alignment());
7542 value += aligned_tcb_size;
7545 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
7546 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
7547 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7548 return aarch64_reloc_funcs::movnz(view, value + addend,
7551 return aarch64_reloc_funcs::template
7552 rela_general<32>(view,
7559 gold_error(_("%s: unsupported reloc %u "
7560 "in non-static TLSLE mode."),
7561 object->name().c_str(), r_type);
7565 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7566 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7567 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7568 case elfcpp::R_AARCH64_TLSDESC_CALL:
7570 if (tlsopt == tls::TLSOPT_TO_LE)
7572 if (tls_segment == NULL)
7574 gold_assert(parameters->errors()->error_count() > 0
7575 || issue_undefined_symbol_error(gsym));
7576 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7578 return tls_desc_gd_to_le(relinfo, target, rela, r_type,
7583 tls_got_offset_type = (tlsopt == tls::TLSOPT_TO_IE
7584 ? GOT_TYPE_TLS_OFFSET
7585 : GOT_TYPE_TLS_DESC);
7586 int got_tlsdesc_offset = 0;
7587 if (r_type != elfcpp::R_AARCH64_TLSDESC_CALL
7588 && tlsopt == tls::TLSOPT_NONE)
7590 // We created GOT entries in the .got.tlsdesc portion of the
7591 // .got.plt section, but the offset stored in the symbol is the
7592 // offset within .got.tlsdesc.
7593 got_tlsdesc_offset = (target->got_tlsdesc_->address()
7594 - target->got_->address());
7596 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7599 gold_assert(gsym->has_got_offset(tls_got_offset_type));
7600 got_entry_address = target->got_->address()
7601 + got_tlsdesc_offset
7602 + gsym->got_offset(tls_got_offset_type);
7606 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7608 object->local_has_got_offset(r_sym, tls_got_offset_type));
7609 got_entry_address = target->got_->address() +
7610 got_tlsdesc_offset +
7611 object->local_got_offset(r_sym, tls_got_offset_type);
7613 if (tlsopt == tls::TLSOPT_TO_IE)
7615 return tls_desc_gd_to_ie(relinfo, target, rela, r_type,
7616 view, psymval, got_entry_address,
7620 // Now do tlsdesc relocation.
7623 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7624 return aarch64_reloc_funcs::adrp(view,
7625 got_entry_address + addend,
7628 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7629 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7630 return aarch64_reloc_funcs::template rela_general<32>(
7631 view, got_entry_address, addend, reloc_property);
7633 case elfcpp::R_AARCH64_TLSDESC_CALL:
7634 return aarch64_reloc_funcs::STATUS_OKAY;
7644 gold_error(_("%s: unsupported TLS reloc %u."),
7645 object->name().c_str(), r_type);
7647 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7648 } // End of relocate_tls.
7651 template<int size, bool big_endian>
7653 typename AArch64_relocate_functions<size, big_endian>::Status
7654 Target_aarch64<size, big_endian>::Relocate::tls_gd_to_le(
7655 const Relocate_info<size, big_endian>* relinfo,
7656 Target_aarch64<size, big_endian>* target,
7657 const elfcpp::Rela<size, big_endian>& rela,
7658 unsigned int r_type,
7659 unsigned char* view,
7660 const Symbol_value<size>* psymval)
7662 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7663 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7664 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7666 Insntype* ip = reinterpret_cast<Insntype*>(view);
7667 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
7668 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
7669 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
7671 if (r_type == elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC)
7673 // This is the 2nd relocs, optimization should already have been
7675 gold_assert((insn1 & 0xfff00000) == 0x91400000);
7676 return aarch64_reloc_funcs::STATUS_OKAY;
7679 // The original sequence is -
7680 // 90000000 adrp x0, 0 <main>
7681 // 91000000 add x0, x0, #0x0
7682 // 94000000 bl 0 <__tls_get_addr>
7683 // optimized to sequence -
7684 // d53bd040 mrs x0, tpidr_el0
7685 // 91400000 add x0, x0, #0x0, lsl #12
7686 // 91000000 add x0, x0, #0x0
7688 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7689 // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
7690 // have to change "bl tls_get_addr", which does not have a corresponding tls
7691 // relocation type. So before proceeding, we need to make sure compiler
7692 // does not change the sequence.
7693 if(!(insn1 == 0x90000000 // adrp x0,0
7694 && insn2 == 0x91000000 // add x0, x0, #0x0
7695 && insn3 == 0x94000000)) // bl 0
7697 // Ideally we should give up gd_to_le relaxation and do gd access.
7698 // However the gd_to_le relaxation decision has been made early
7699 // in the scan stage, where we did not allocate any GOT entry for
7700 // this symbol. Therefore we have to exit and report error now.
7701 gold_error(_("unexpected reloc insn sequence while relaxing "
7702 "tls gd to le for reloc %u."), r_type);
7703 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7707 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
7708 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
7709 insn3 = 0x91000000; // add x0, x0, #0x0
7710 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
7711 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
7712 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
7714 // Calculate tprel value.
7715 Output_segment* tls_segment = relinfo->layout->tls_segment();
7716 gold_assert(tls_segment != NULL);
7717 AArch64_address value = psymval->value(relinfo->object, 0);
7718 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7719 AArch64_address aligned_tcb_size =
7720 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7721 AArch64_address x = value + aligned_tcb_size;
7723 // After new insns are written, apply TLSLE relocs.
7724 const AArch64_reloc_property* rp1 =
7725 aarch64_reloc_property_table->get_reloc_property(
7726 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
7727 const AArch64_reloc_property* rp2 =
7728 aarch64_reloc_property_table->get_reloc_property(
7729 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
7730 gold_assert(rp1 != NULL && rp2 != NULL);
7732 typename aarch64_reloc_funcs::Status s1 =
7733 aarch64_reloc_funcs::template rela_general<32>(view + 4,
7737 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
7740 typename aarch64_reloc_funcs::Status s2 =
7741 aarch64_reloc_funcs::template rela_general<32>(view + 8,
7746 this->skip_call_tls_get_addr_ = true;
7748 } // End of tls_gd_to_le
7751 template<int size, bool big_endian>
7753 typename AArch64_relocate_functions<size, big_endian>::Status
7754 Target_aarch64<size, big_endian>::Relocate::tls_ld_to_le(
7755 const Relocate_info<size, big_endian>* relinfo,
7756 Target_aarch64<size, big_endian>* target,
7757 const elfcpp::Rela<size, big_endian>& rela,
7758 unsigned int r_type,
7759 unsigned char* view,
7760 const Symbol_value<size>* psymval)
7762 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7763 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7764 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7766 Insntype* ip = reinterpret_cast<Insntype*>(view);
7767 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
7768 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
7769 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
7771 if (r_type == elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC)
7773 // This is the 2nd relocs, optimization should already have been
7775 gold_assert((insn1 & 0xfff00000) == 0x91400000);
7776 return aarch64_reloc_funcs::STATUS_OKAY;
7779 // The original sequence is -
7780 // 90000000 adrp x0, 0 <main>
7781 // 91000000 add x0, x0, #0x0
7782 // 94000000 bl 0 <__tls_get_addr>
7783 // optimized to sequence -
7784 // d53bd040 mrs x0, tpidr_el0
7785 // 91400000 add x0, x0, #0x0, lsl #12
7786 // 91000000 add x0, x0, #0x0
7788 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7789 // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
7790 // have to change "bl tls_get_addr", which does not have a corresponding tls
7791 // relocation type. So before proceeding, we need to make sure compiler
7792 // does not change the sequence.
7793 if(!(insn1 == 0x90000000 // adrp x0,0
7794 && insn2 == 0x91000000 // add x0, x0, #0x0
7795 && insn3 == 0x94000000)) // bl 0
7797 // Ideally we should give up gd_to_le relaxation and do gd access.
7798 // However the gd_to_le relaxation decision has been made early
7799 // in the scan stage, where we did not allocate a GOT entry for
7800 // this symbol. Therefore we have to exit and report an error now.
7801 gold_error(_("unexpected reloc insn sequence while relaxing "
7802 "tls gd to le for reloc %u."), r_type);
7803 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7807 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
7808 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
7809 insn3 = 0x91000000; // add x0, x0, #0x0
7810 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
7811 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
7812 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
7814 // Calculate tprel value.
7815 Output_segment* tls_segment = relinfo->layout->tls_segment();
7816 gold_assert(tls_segment != NULL);
7817 AArch64_address value = psymval->value(relinfo->object, 0);
7818 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7819 AArch64_address aligned_tcb_size =
7820 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7821 AArch64_address x = value + aligned_tcb_size;
7823 // After new insns are written, apply TLSLE relocs.
7824 const AArch64_reloc_property* rp1 =
7825 aarch64_reloc_property_table->get_reloc_property(
7826 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
7827 const AArch64_reloc_property* rp2 =
7828 aarch64_reloc_property_table->get_reloc_property(
7829 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
7830 gold_assert(rp1 != NULL && rp2 != NULL);
7832 typename aarch64_reloc_funcs::Status s1 =
7833 aarch64_reloc_funcs::template rela_general<32>(view + 4,
7837 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
7840 typename aarch64_reloc_funcs::Status s2 =
7841 aarch64_reloc_funcs::template rela_general<32>(view + 8,
7846 this->skip_call_tls_get_addr_ = true;
7849 } // End of tls_ld_to_le
7851 template<int size, bool big_endian>
7853 typename AArch64_relocate_functions<size, big_endian>::Status
7854 Target_aarch64<size, big_endian>::Relocate::tls_ie_to_le(
7855 const Relocate_info<size, big_endian>* relinfo,
7856 Target_aarch64<size, big_endian>* target,
7857 const elfcpp::Rela<size, big_endian>& rela,
7858 unsigned int r_type,
7859 unsigned char* view,
7860 const Symbol_value<size>* psymval)
7862 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7863 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7864 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7866 AArch64_address value = psymval->value(relinfo->object, 0);
7867 Output_segment* tls_segment = relinfo->layout->tls_segment();
7868 AArch64_address aligned_tcb_address =
7869 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7870 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7871 AArch64_address x = value + addend + aligned_tcb_address;
7872 // "x" is the offset to tp, we can only do this if x is within
7873 // range [0, 2^32-1]
7874 if (!(size == 32 || (size == 64 && (static_cast<uint64_t>(x) >> 32) == 0)))
7876 gold_error(_("TLS variable referred by reloc %u is too far from TP."),
7878 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7881 Insntype* ip = reinterpret_cast<Insntype*>(view);
7882 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
7885 if (r_type == elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21)
7888 regno = (insn & 0x1f);
7889 newinsn = (0xd2a00000 | regno) | (((x >> 16) & 0xffff) << 5);
7891 else if (r_type == elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
7894 regno = (insn & 0x1f);
7895 gold_assert(regno == ((insn >> 5) & 0x1f));
7896 newinsn = (0xf2800000 | regno) | ((x & 0xffff) << 5);
7901 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7902 return aarch64_reloc_funcs::STATUS_OKAY;
7903 } // End of tls_ie_to_le
7906 template<int size, bool big_endian>
7908 typename AArch64_relocate_functions<size, big_endian>::Status
7909 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_le(
7910 const Relocate_info<size, big_endian>* relinfo,
7911 Target_aarch64<size, big_endian>* target,
7912 const elfcpp::Rela<size, big_endian>& rela,
7913 unsigned int r_type,
7914 unsigned char* view,
7915 const Symbol_value<size>* psymval)
7917 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7918 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7919 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7921 // TLSDESC-GD sequence is like:
7922 // adrp x0, :tlsdesc:v1
7923 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7924 // add x0, x0, :tlsdesc_lo12:v1
7927 // After desc_gd_to_le optimization, the sequence will be like:
7928 // movz x0, #0x0, lsl #16
7933 // Calculate tprel value.
7934 Output_segment* tls_segment = relinfo->layout->tls_segment();
7935 gold_assert(tls_segment != NULL);
7936 Insntype* ip = reinterpret_cast<Insntype*>(view);
7937 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7938 AArch64_address value = psymval->value(relinfo->object, addend);
7939 AArch64_address aligned_tcb_size =
7940 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7941 AArch64_address x = value + aligned_tcb_size;
7942 // x is the offset to tp, we can only do this if x is within range
7943 // [0, 2^32-1]. If x is out of range, fail and exit.
7944 if (size == 64 && (static_cast<uint64_t>(x) >> 32) != 0)
7946 gold_error(_("TLS variable referred by reloc %u is too far from TP. "
7947 "We Can't do gd_to_le relaxation.\n"), r_type);
7948 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7953 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7954 case elfcpp::R_AARCH64_TLSDESC_CALL:
7956 newinsn = 0xd503201f;
7959 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7961 newinsn = 0xd2a00000 | (((x >> 16) & 0xffff) << 5);
7964 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7966 newinsn = 0xf2800000 | ((x & 0xffff) << 5);
7970 gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
7974 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7975 return aarch64_reloc_funcs::STATUS_OKAY;
7976 } // End of tls_desc_gd_to_le
7979 template<int size, bool big_endian>
7981 typename AArch64_relocate_functions<size, big_endian>::Status
7982 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_ie(
7983 const Relocate_info<size, big_endian>* /* relinfo */,
7984 Target_aarch64<size, big_endian>* /* target */,
7985 const elfcpp::Rela<size, big_endian>& rela,
7986 unsigned int r_type,
7987 unsigned char* view,
7988 const Symbol_value<size>* /* psymval */,
7989 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address,
7990 typename elfcpp::Elf_types<size>::Elf_Addr address)
7992 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7993 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7995 // TLSDESC-GD sequence is like:
7996 // adrp x0, :tlsdesc:v1
7997 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7998 // add x0, x0, :tlsdesc_lo12:v1
8001 // After desc_gd_to_ie optimization, the sequence will be like:
8002 // adrp x0, :tlsie:v1
8003 // ldr x0, [x0, :tlsie_lo12:v1]
8007 Insntype* ip = reinterpret_cast<Insntype*>(view);
8008 const elfcpp::Elf_Xword addend = rela.get_r_addend();
8012 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
8013 case elfcpp::R_AARCH64_TLSDESC_CALL:
8015 newinsn = 0xd503201f;
8016 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
8019 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
8021 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
8026 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
8028 // Set ldr target register to be x0.
8029 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
8031 elfcpp::Swap<32, big_endian>::writeval(ip, insn);
8033 const AArch64_reloc_property* reloc_property =
8034 aarch64_reloc_property_table->get_reloc_property(
8035 elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
8036 return aarch64_reloc_funcs::template rela_general<32>(
8037 view, got_entry_address, addend, reloc_property);
8042 gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
8046 return aarch64_reloc_funcs::STATUS_OKAY;
8047 } // End of tls_desc_gd_to_ie
8049 // Relocate section data.
8051 template<int size, bool big_endian>
8053 Target_aarch64<size, big_endian>::relocate_section(
8054 const Relocate_info<size, big_endian>* relinfo,
8055 unsigned int sh_type,
8056 const unsigned char* prelocs,
8058 Output_section* output_section,
8059 bool needs_special_offset_handling,
8060 unsigned char* view,
8061 typename elfcpp::Elf_types<size>::Elf_Addr address,
8062 section_size_type view_size,
8063 const Reloc_symbol_changes* reloc_symbol_changes)
8065 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
8066 typedef Target_aarch64<size, big_endian> Aarch64;
8067 typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
8068 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8071 gold_assert(sh_type == elfcpp::SHT_RELA);
8073 // See if we are relocating a relaxed input section. If so, the view
8074 // covers the whole output section and we need to adjust accordingly.
8075 if (needs_special_offset_handling)
8077 const Output_relaxed_input_section* poris =
8078 output_section->find_relaxed_input_section(relinfo->object,
8079 relinfo->data_shndx);
8082 Address section_address = poris->address();
8083 section_size_type section_size = poris->data_size();
8085 gold_assert((section_address >= address)
8086 && ((section_address + section_size)
8087 <= (address + view_size)));
8089 off_t offset = section_address - address;
8092 view_size = section_size;
8096 gold::relocate_section<size, big_endian, Aarch64, AArch64_relocate,
8097 gold::Default_comdat_behavior, Classify_reloc>(
8103 needs_special_offset_handling,
8107 reloc_symbol_changes);
8110 // Scan the relocs during a relocatable link.
8112 template<int size, bool big_endian>
8114 Target_aarch64<size, big_endian>::scan_relocatable_relocs(
8115 Symbol_table* symtab,
8117 Sized_relobj_file<size, big_endian>* object,
8118 unsigned int data_shndx,
8119 unsigned int sh_type,
8120 const unsigned char* prelocs,
8122 Output_section* output_section,
8123 bool needs_special_offset_handling,
8124 size_t local_symbol_count,
8125 const unsigned char* plocal_symbols,
8126 Relocatable_relocs* rr)
8128 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8130 typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
8131 Scan_relocatable_relocs;
8133 gold_assert(sh_type == elfcpp::SHT_RELA);
8135 gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
8143 needs_special_offset_handling,
8149 // Scan the relocs for --emit-relocs.
8151 template<int size, bool big_endian>
8153 Target_aarch64<size, big_endian>::emit_relocs_scan(
8154 Symbol_table* symtab,
8156 Sized_relobj_file<size, big_endian>* object,
8157 unsigned int data_shndx,
8158 unsigned int sh_type,
8159 const unsigned char* prelocs,
8161 Output_section* output_section,
8162 bool needs_special_offset_handling,
8163 size_t local_symbol_count,
8164 const unsigned char* plocal_syms,
8165 Relocatable_relocs* rr)
8167 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8169 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
8170 Emit_relocs_strategy;
8172 gold_assert(sh_type == elfcpp::SHT_RELA);
8174 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
8182 needs_special_offset_handling,
8188 // Relocate a section during a relocatable link.
8190 template<int size, bool big_endian>
8192 Target_aarch64<size, big_endian>::relocate_relocs(
8193 const Relocate_info<size, big_endian>* relinfo,
8194 unsigned int sh_type,
8195 const unsigned char* prelocs,
8197 Output_section* output_section,
8198 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
8199 unsigned char* view,
8200 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
8201 section_size_type view_size,
8202 unsigned char* reloc_view,
8203 section_size_type reloc_view_size)
8205 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8208 gold_assert(sh_type == elfcpp::SHT_RELA);
8210 gold::relocate_relocs<size, big_endian, Classify_reloc>(
8215 offset_in_output_section,
8224 // Return whether this is a 3-insn erratum sequence.
8226 template<int size, bool big_endian>
8228 Target_aarch64<size, big_endian>::is_erratum_843419_sequence(
8229 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
8230 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
8231 typename elfcpp::Swap<32,big_endian>::Valtype insn3)
8236 // The 2nd insn is a single register load or store; or register pair
8238 if (Insn_utilities::aarch64_mem_op_p(insn2, &rt1, &rt2, &pair, &load)
8239 && (!pair || (pair && !load)))
8241 // The 3rd insn is a load or store instruction from the "Load/store
8242 // register (unsigned immediate)" encoding class, using Rn as the
8243 // base address register.
8244 if (Insn_utilities::aarch64_ldst_uimm(insn3)
8245 && (Insn_utilities::aarch64_rn(insn3)
8246 == Insn_utilities::aarch64_rd(insn1)))
8253 // Return whether this is a 835769 sequence.
8254 // (Similarly implemented as in elfnn-aarch64.c.)
8256 template<int size, bool big_endian>
8258 Target_aarch64<size, big_endian>::is_erratum_835769_sequence(
8259 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
8260 typename elfcpp::Swap<32,big_endian>::Valtype insn2)
8270 if (Insn_utilities::aarch64_mlxl(insn2)
8271 && Insn_utilities::aarch64_mem_op_p (insn1, &rt, &rt2, &pair, &load))
8273 /* Any SIMD memory op is independent of the subsequent MLA
8274 by definition of the erratum. */
8275 if (Insn_utilities::aarch64_bit(insn1, 26))
8278 /* If not SIMD, check for integer memory ops and MLA relationship. */
8279 rn = Insn_utilities::aarch64_rn(insn2);
8280 ra = Insn_utilities::aarch64_ra(insn2);
8281 rm = Insn_utilities::aarch64_rm(insn2);
8283 /* If this is a load and there's a true(RAW) dependency, we are safe
8284 and this is not an erratum sequence. */
8286 (rt == rn || rt == rm || rt == ra
8287 || (pair && (rt2 == rn || rt2 == rm || rt2 == ra))))
8290 /* We conservatively put out stubs for all other cases (including
8299 // Helper method to create erratum stub for ST_E_843419 and ST_E_835769.
8301 template<int size, bool big_endian>
8303 Target_aarch64<size, big_endian>::create_erratum_stub(
8304 AArch64_relobj<size, big_endian>* relobj,
8306 section_size_type erratum_insn_offset,
8307 Address erratum_address,
8308 typename Insn_utilities::Insntype erratum_insn,
8310 unsigned int e843419_adrp_offset)
8312 gold_assert(erratum_type == ST_E_843419 || erratum_type == ST_E_835769);
8313 The_stub_table* stub_table = relobj->stub_table(shndx);
8314 gold_assert(stub_table != NULL);
8315 if (stub_table->find_erratum_stub(relobj,
8317 erratum_insn_offset) == NULL)
8319 const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
8320 The_erratum_stub* stub;
8321 if (erratum_type == ST_E_835769)
8322 stub = new The_erratum_stub(relobj, erratum_type, shndx,
8323 erratum_insn_offset);
8324 else if (erratum_type == ST_E_843419)
8325 stub = new E843419_stub<size, big_endian>(
8326 relobj, shndx, erratum_insn_offset, e843419_adrp_offset);
8329 stub->set_erratum_insn(erratum_insn);
8330 stub->set_erratum_address(erratum_address);
8331 // For erratum ST_E_843419 and ST_E_835769, the destination address is
8332 // always the next insn after erratum insn.
8333 stub->set_destination_address(erratum_address + BPI);
8334 stub_table->add_erratum_stub(stub);
8339 // Scan erratum for section SHNDX range [output_address + span_start,
8340 // output_address + span_end). Note here we do not share the code with
8341 // scan_erratum_843419_span function, because for 843419 we optimize by only
8342 // scanning the last few insns of a page, whereas for 835769, we need to scan
8345 template<int size, bool big_endian>
8347 Target_aarch64<size, big_endian>::scan_erratum_835769_span(
8348 AArch64_relobj<size, big_endian>* relobj,
8350 const section_size_type span_start,
8351 const section_size_type span_end,
8352 unsigned char* input_view,
8353 Address output_address)
8355 typedef typename Insn_utilities::Insntype Insntype;
8357 const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
8359 // Adjust output_address and view to the start of span.
8360 output_address += span_start;
8361 input_view += span_start;
8363 section_size_type span_length = span_end - span_start;
8364 section_size_type offset = 0;
8365 for (offset = 0; offset + BPI < span_length; offset += BPI)
8367 Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
8368 Insntype insn1 = ip[0];
8369 Insntype insn2 = ip[1];
8370 if (is_erratum_835769_sequence(insn1, insn2))
8372 Insntype erratum_insn = insn2;
8373 // "span_start + offset" is the offset for insn1. So for insn2, it is
8374 // "span_start + offset + BPI".
8375 section_size_type erratum_insn_offset = span_start + offset + BPI;
8376 Address erratum_address = output_address + offset + BPI;
8377 gold_info(_("Erratum 835769 found and fixed at \"%s\", "
8378 "section %d, offset 0x%08x."),
8379 relobj->name().c_str(), shndx,
8380 (unsigned int)(span_start + offset));
8382 this->create_erratum_stub(relobj, shndx,
8383 erratum_insn_offset, erratum_address,
8384 erratum_insn, ST_E_835769);
8385 offset += BPI; // Skip mac insn.
8388 } // End of "Target_aarch64::scan_erratum_835769_span".
8391 // Scan erratum for section SHNDX range
8392 // [output_address + span_start, output_address + span_end).
8394 template<int size, bool big_endian>
8396 Target_aarch64<size, big_endian>::scan_erratum_843419_span(
8397 AArch64_relobj<size, big_endian>* relobj,
8399 const section_size_type span_start,
8400 const section_size_type span_end,
8401 unsigned char* input_view,
8402 Address output_address)
8404 typedef typename Insn_utilities::Insntype Insntype;
8406 // Adjust output_address and view to the start of span.
8407 output_address += span_start;
8408 input_view += span_start;
8410 if ((output_address & 0x03) != 0)
8413 section_size_type offset = 0;
8414 section_size_type span_length = span_end - span_start;
8415 // The first instruction must be ending at 0xFF8 or 0xFFC.
8416 unsigned int page_offset = output_address & 0xFFF;
8417 // Make sure starting position, that is "output_address+offset",
8418 // starts at page position 0xff8 or 0xffc.
8419 if (page_offset < 0xff8)
8420 offset = 0xff8 - page_offset;
8421 while (offset + 3 * Insn_utilities::BYTES_PER_INSN <= span_length)
8423 Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
8424 Insntype insn1 = ip[0];
8425 if (Insn_utilities::is_adrp(insn1))
8427 Insntype insn2 = ip[1];
8428 Insntype insn3 = ip[2];
8429 Insntype erratum_insn;
8430 unsigned insn_offset;
8431 bool do_report = false;
8432 if (is_erratum_843419_sequence(insn1, insn2, insn3))
8435 erratum_insn = insn3;
8436 insn_offset = 2 * Insn_utilities::BYTES_PER_INSN;
8438 else if (offset + 4 * Insn_utilities::BYTES_PER_INSN <= span_length)
8440 // Optionally we can have an insn between ins2 and ins3
8441 Insntype insn_opt = ip[2];
8442 // And insn_opt must not be a branch.
8443 if (!Insn_utilities::aarch64_b(insn_opt)
8444 && !Insn_utilities::aarch64_bl(insn_opt)
8445 && !Insn_utilities::aarch64_blr(insn_opt)
8446 && !Insn_utilities::aarch64_br(insn_opt))
8448 // And insn_opt must not write to dest reg in insn1. However
8449 // we do a conservative scan, which means we may fix/report
8450 // more than necessary, but it doesn't hurt.
8452 Insntype insn4 = ip[3];
8453 if (is_erratum_843419_sequence(insn1, insn2, insn4))
8456 erratum_insn = insn4;
8457 insn_offset = 3 * Insn_utilities::BYTES_PER_INSN;
8463 unsigned int erratum_insn_offset =
8464 span_start + offset + insn_offset;
8465 Address erratum_address =
8466 output_address + offset + insn_offset;
8467 create_erratum_stub(relobj, shndx,
8468 erratum_insn_offset, erratum_address,
8469 erratum_insn, ST_E_843419,
8470 span_start + offset);
8474 // Advance to next candidate instruction. We only consider instruction
8475 // sequences starting at a page offset of 0xff8 or 0xffc.
8476 page_offset = (output_address + offset) & 0xfff;
8477 if (page_offset == 0xff8)
8479 else // (page_offset == 0xffc), we move to next page's 0xff8.
8482 } // End of "Target_aarch64::scan_erratum_843419_span".
8485 // The selector for aarch64 object files.
8487 template<int size, bool big_endian>
8488 class Target_selector_aarch64 : public Target_selector
8491 Target_selector_aarch64();
8494 do_instantiate_target()
8495 { return new Target_aarch64<size, big_endian>(); }
8499 Target_selector_aarch64<32, true>::Target_selector_aarch64()
8500 : Target_selector(elfcpp::EM_AARCH64, 32, true,
8501 "elf32-bigaarch64", "aarch64_elf32_be_vec")
8505 Target_selector_aarch64<32, false>::Target_selector_aarch64()
8506 : Target_selector(elfcpp::EM_AARCH64, 32, false,
8507 "elf32-littleaarch64", "aarch64_elf32_le_vec")
8511 Target_selector_aarch64<64, true>::Target_selector_aarch64()
8512 : Target_selector(elfcpp::EM_AARCH64, 64, true,
8513 "elf64-bigaarch64", "aarch64_elf64_be_vec")
8517 Target_selector_aarch64<64, false>::Target_selector_aarch64()
8518 : Target_selector(elfcpp::EM_AARCH64, 64, false,
8519 "elf64-littleaarch64", "aarch64_elf64_le_vec")
8522 Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
8523 Target_selector_aarch64<32, false> target_selector_aarch64elf32;
8524 Target_selector_aarch64<64, true> target_selector_aarch64elfb;
8525 Target_selector_aarch64<64, false> target_selector_aarch64elf;
8527 } // End anonymous namespace.