1 // aarch64.cc -- aarch64 target support for gold.
3 // Copyright (C) 2014-2015 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 = 4;
83 aarch64_bit(Insntype insn, int pos)
84 { return ((1 << pos) & insn) >> pos; }
87 aarch64_bits(Insntype insn, int pos, int l)
88 { return (insn >> pos) & ((1 << l) - 1); }
91 is_adrp(const Insntype insn)
92 { return (insn & 0x9F000000) == 0x90000000; }
95 aarch64_rm(const Insntype insn)
96 { return aarch64_bits(insn, 16, 5); }
99 aarch64_rn(const Insntype insn)
100 { return aarch64_bits(insn, 5, 5); }
103 aarch64_rd(const Insntype insn)
104 { return aarch64_bits(insn, 0, 5); }
107 aarch64_rt(const Insntype insn)
108 { return aarch64_bits(insn, 0, 5); }
111 aarch64_rt2(const Insntype insn)
112 { return aarch64_bits(insn, 10, 5); }
115 aarch64_b(const Insntype insn)
116 { return (insn & 0xFC000000) == 0x14000000; }
119 aarch64_bl(const Insntype insn)
120 { return (insn & 0xFC000000) == 0x94000000; }
123 aarch64_blr(const Insntype insn)
124 { return (insn & 0xFFFFFC1F) == 0xD63F0000; }
127 aarch64_br(const Insntype insn)
128 { return (insn & 0xFFFFFC1F) == 0xD61F0000; }
130 // All ld/st ops. See C4-182 of the ARM ARM. The encoding space for
131 // LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops.
133 aarch64_ld(Insntype insn) { return aarch64_bit(insn, 22) == 1; }
136 aarch64_ldst(Insntype insn)
137 { return (insn & 0x0a000000) == 0x08000000; }
140 aarch64_ldst_ex(Insntype insn)
141 { return (insn & 0x3f000000) == 0x08000000; }
144 aarch64_ldst_pcrel(Insntype insn)
145 { return (insn & 0x3b000000) == 0x18000000; }
148 aarch64_ldst_nap(Insntype insn)
149 { return (insn & 0x3b800000) == 0x28000000; }
152 aarch64_ldstp_pi(Insntype insn)
153 { return (insn & 0x3b800000) == 0x28800000; }
156 aarch64_ldstp_o(Insntype insn)
157 { return (insn & 0x3b800000) == 0x29000000; }
160 aarch64_ldstp_pre(Insntype insn)
161 { return (insn & 0x3b800000) == 0x29800000; }
164 aarch64_ldst_ui(Insntype insn)
165 { return (insn & 0x3b200c00) == 0x38000000; }
168 aarch64_ldst_piimm(Insntype insn)
169 { return (insn & 0x3b200c00) == 0x38000400; }
172 aarch64_ldst_u(Insntype insn)
173 { return (insn & 0x3b200c00) == 0x38000800; }
176 aarch64_ldst_preimm(Insntype insn)
177 { return (insn & 0x3b200c00) == 0x38000c00; }
180 aarch64_ldst_ro(Insntype insn)
181 { return (insn & 0x3b200c00) == 0x38200800; }
184 aarch64_ldst_uimm(Insntype insn)
185 { return (insn & 0x3b000000) == 0x39000000; }
188 aarch64_ldst_simd_m(Insntype insn)
189 { return (insn & 0xbfbf0000) == 0x0c000000; }
192 aarch64_ldst_simd_m_pi(Insntype insn)
193 { return (insn & 0xbfa00000) == 0x0c800000; }
196 aarch64_ldst_simd_s(Insntype insn)
197 { return (insn & 0xbf9f0000) == 0x0d000000; }
200 aarch64_ldst_simd_s_pi(Insntype insn)
201 { return (insn & 0xbf800000) == 0x0d800000; }
203 // Classify an INSN if it is indeed a load/store. Return true if INSN is a
204 // LD/ST instruction otherwise return false. For scalar LD/ST instructions
205 // PAIR is FALSE, RT is returned and RT2 is set equal to RT. For LD/ST pair
206 // instructions PAIR is TRUE, RT and RT2 are returned.
208 aarch64_mem_op_p(Insntype insn, unsigned int *rt, unsigned int *rt2,
209 bool *pair, bool *load)
217 /* Bail out quickly if INSN doesn't fall into the the load-store
219 if (!aarch64_ldst (insn))
224 if (aarch64_ldst_ex (insn))
226 *rt = aarch64_rt (insn);
228 if (aarch64_bit (insn, 21) == 1)
231 *rt2 = aarch64_rt2 (insn);
233 *load = aarch64_ld (insn);
236 else if (aarch64_ldst_nap (insn)
237 || aarch64_ldstp_pi (insn)
238 || aarch64_ldstp_o (insn)
239 || aarch64_ldstp_pre (insn))
242 *rt = aarch64_rt (insn);
243 *rt2 = aarch64_rt2 (insn);
244 *load = aarch64_ld (insn);
247 else if (aarch64_ldst_pcrel (insn)
248 || aarch64_ldst_ui (insn)
249 || aarch64_ldst_piimm (insn)
250 || aarch64_ldst_u (insn)
251 || aarch64_ldst_preimm (insn)
252 || aarch64_ldst_ro (insn)
253 || aarch64_ldst_uimm (insn))
255 *rt = aarch64_rt (insn);
257 if (aarch64_ldst_pcrel (insn))
259 opc = aarch64_bits (insn, 22, 2);
260 v = aarch64_bit (insn, 26);
261 opc_v = opc | (v << 2);
262 *load = (opc_v == 1 || opc_v == 2 || opc_v == 3
263 || opc_v == 5 || opc_v == 7);
266 else if (aarch64_ldst_simd_m (insn)
267 || aarch64_ldst_simd_m_pi (insn))
269 *rt = aarch64_rt (insn);
270 *load = aarch64_bit (insn, 22);
271 opcode = (insn >> 12) & 0xf;
298 else if (aarch64_ldst_simd_s (insn)
299 || aarch64_ldst_simd_s_pi (insn))
301 *rt = aarch64_rt (insn);
302 r = (insn >> 21) & 1;
303 *load = aarch64_bit (insn, 22);
304 opcode = (insn >> 13) & 0x7;
316 *rt2 = *rt + (r == 0 ? 2 : 3);
324 *rt2 = *rt + (r == 0 ? 2 : 3);
337 // Output_data_got_aarch64 class.
339 template<int size, bool big_endian>
340 class Output_data_got_aarch64 : public Output_data_got<size, big_endian>
343 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
344 Output_data_got_aarch64(Symbol_table* symtab, Layout* layout)
345 : Output_data_got<size, big_endian>(),
346 symbol_table_(symtab), layout_(layout)
349 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
350 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
351 // applied in a static link.
353 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
354 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
357 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
358 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
359 // relocation that needs to be applied in a static link.
361 add_static_reloc(unsigned int got_offset, unsigned int r_type,
362 Sized_relobj_file<size, big_endian>* relobj,
365 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
371 // Write out the GOT table.
373 do_write(Output_file* of) {
374 // The first entry in the GOT is the address of the .dynamic section.
375 gold_assert(this->data_size() >= size / 8);
376 Output_section* dynamic = this->layout_->dynamic_section();
377 Valtype dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
378 this->replace_constant(0, dynamic_addr);
379 Output_data_got<size, big_endian>::do_write(of);
381 // Handling static relocs
382 if (this->static_relocs_.empty())
385 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
387 gold_assert(parameters->doing_static_link());
388 const off_t offset = this->offset();
389 const section_size_type oview_size =
390 convert_to_section_size_type(this->data_size());
391 unsigned char* const oview = of->get_output_view(offset, oview_size);
393 Output_segment* tls_segment = this->layout_->tls_segment();
394 gold_assert(tls_segment != NULL);
396 AArch64_address aligned_tcb_address =
397 align_address(Target_aarch64<size, big_endian>::TCB_SIZE,
398 tls_segment->maximum_alignment());
400 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
402 Static_reloc& reloc(this->static_relocs_[i]);
403 AArch64_address value;
405 if (!reloc.symbol_is_global())
407 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
408 const Symbol_value<size>* psymval =
409 reloc.relobj()->local_symbol(reloc.index());
411 // We are doing static linking. Issue an error and skip this
412 // relocation if the symbol is undefined or in a discarded_section.
414 unsigned int shndx = psymval->input_shndx(&is_ordinary);
415 if ((shndx == elfcpp::SHN_UNDEF)
417 && shndx != elfcpp::SHN_UNDEF
418 && !object->is_section_included(shndx)
419 && !this->symbol_table_->is_section_folded(object, shndx)))
421 gold_error(_("undefined or discarded local symbol %u from "
422 " object %s in GOT"),
423 reloc.index(), reloc.relobj()->name().c_str());
426 value = psymval->value(object, 0);
430 const Symbol* gsym = reloc.symbol();
431 gold_assert(gsym != NULL);
432 if (gsym->is_forwarder())
433 gsym = this->symbol_table_->resolve_forwards(gsym);
435 // We are doing static linking. Issue an error and skip this
436 // relocation if the symbol is undefined or in a discarded_section
437 // unless it is a weakly_undefined symbol.
438 if ((gsym->is_defined_in_discarded_section()
439 || gsym->is_undefined())
440 && !gsym->is_weak_undefined())
442 gold_error(_("undefined or discarded symbol %s in GOT"),
447 if (!gsym->is_weak_undefined())
449 const Sized_symbol<size>* sym =
450 static_cast<const Sized_symbol<size>*>(gsym);
451 value = sym->value();
457 unsigned got_offset = reloc.got_offset();
458 gold_assert(got_offset < oview_size);
460 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
461 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
463 switch (reloc.r_type())
465 case elfcpp::R_AARCH64_TLS_DTPREL64:
468 case elfcpp::R_AARCH64_TLS_TPREL64:
469 x = value + aligned_tcb_address;
474 elfcpp::Swap<size, big_endian>::writeval(wv, x);
477 of->write_output_view(offset, oview_size, oview);
481 // Symbol table of the output object.
482 Symbol_table* symbol_table_;
483 // A pointer to the Layout class, so that we can find the .dynamic
484 // section when we write out the GOT section.
487 // This class represent dynamic relocations that need to be applied by
488 // gold because we are using TLS relocations in a static link.
492 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
493 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
494 { this->u_.global.symbol = gsym; }
496 Static_reloc(unsigned int got_offset, unsigned int r_type,
497 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
498 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
500 this->u_.local.relobj = relobj;
501 this->u_.local.index = index;
504 // Return the GOT offset.
507 { return this->got_offset_; }
512 { return this->r_type_; }
514 // Whether the symbol is global or not.
516 symbol_is_global() const
517 { return this->symbol_is_global_; }
519 // For a relocation against a global symbol, the global symbol.
523 gold_assert(this->symbol_is_global_);
524 return this->u_.global.symbol;
527 // For a relocation against a local symbol, the defining object.
528 Sized_relobj_file<size, big_endian>*
531 gold_assert(!this->symbol_is_global_);
532 return this->u_.local.relobj;
535 // For a relocation against a local symbol, the local symbol index.
539 gold_assert(!this->symbol_is_global_);
540 return this->u_.local.index;
544 // GOT offset of the entry to which this relocation is applied.
545 unsigned int got_offset_;
546 // Type of relocation.
547 unsigned int r_type_;
548 // Whether this relocation is against a global symbol.
549 bool symbol_is_global_;
550 // A global or local symbol.
555 // For a global symbol, the symbol itself.
560 // For a local symbol, the object defining the symbol.
561 Sized_relobj_file<size, big_endian>* relobj;
562 // For a local symbol, the symbol index.
566 }; // End of inner class Static_reloc
568 std::vector<Static_reloc> static_relocs_;
569 }; // End of Output_data_got_aarch64
572 template<int size, bool big_endian>
573 class AArch64_input_section;
576 template<int size, bool big_endian>
577 class AArch64_output_section;
580 template<int size, bool big_endian>
581 class AArch64_relobj;
584 // Stub type enum constants.
590 // Using adrp/add pair, 4 insns (including alignment) without mem access,
591 // the fastest stub. This has a limited jump distance, which is tested by
592 // aarch64_valid_for_adrp_p.
595 // Using ldr-absolute-address/br-register, 4 insns with 1 mem access,
596 // unlimited in jump distance.
597 ST_LONG_BRANCH_ABS = 2,
599 // Using ldr/calculate-pcrel/jump, 8 insns (including alignment) with 1
600 // mem access, slowest one. Only used in position independent executables.
601 ST_LONG_BRANCH_PCREL = 3,
603 // Stub for erratum 843419 handling.
606 // Number of total stub types.
611 // Struct that wraps insns for a particular stub. All stub templates are
612 // created/initialized as constants by Stub_template_repertoire.
614 template<bool big_endian>
617 const typename AArch64_insn_utilities<big_endian>::Insntype* insns;
622 // Simple singleton class that creates/initializes/stores all types of stub
625 template<bool big_endian>
626 class Stub_template_repertoire
629 typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
631 // Single static method to get stub template for a given stub type.
632 static const Stub_template<big_endian>*
633 get_stub_template(int type)
635 static Stub_template_repertoire<big_endian> singleton;
636 return singleton.stub_templates_[type];
640 // Constructor - creates/initializes all stub templates.
641 Stub_template_repertoire();
642 ~Stub_template_repertoire()
645 // Disallowing copy ctor and copy assignment operator.
646 Stub_template_repertoire(Stub_template_repertoire&);
647 Stub_template_repertoire& operator=(Stub_template_repertoire&);
649 // Data that stores all insn templates.
650 const Stub_template<big_endian>* stub_templates_[ST_NUMBER];
651 }; // End of "class Stub_template_repertoire".
654 // Constructor - creates/initilizes all stub templates.
656 template<bool big_endian>
657 Stub_template_repertoire<big_endian>::Stub_template_repertoire()
659 // Insn array definitions.
660 const static Insntype ST_NONE_INSNS[] = {};
662 const static Insntype ST_ADRP_BRANCH_INSNS[] =
664 0x90000010, /* adrp ip0, X */
665 /* ADR_PREL_PG_HI21(X) */
666 0x91000210, /* add ip0, ip0, :lo12:X */
667 /* ADD_ABS_LO12_NC(X) */
668 0xd61f0200, /* br ip0 */
669 0x00000000, /* alignment padding */
672 const static Insntype ST_LONG_BRANCH_ABS_INSNS[] =
674 0x58000050, /* ldr ip0, 0x8 */
675 0xd61f0200, /* br ip0 */
676 0x00000000, /* address field */
677 0x00000000, /* address fields */
680 const static Insntype ST_LONG_BRANCH_PCREL_INSNS[] =
682 0x58000090, /* ldr ip0, 0x10 */
683 0x10000011, /* adr ip1, #0 */
684 0x8b110210, /* add ip0, ip0, ip1 */
685 0xd61f0200, /* br ip0 */
686 0x00000000, /* address field */
687 0x00000000, /* address field */
688 0x00000000, /* alignment padding */
689 0x00000000, /* alignment padding */
692 const static Insntype ST_E_843419_INSNS[] =
694 0x00000000, /* Placeholder for erratum insn. */
695 0x14000000, /* b <label> */
698 #define install_insn_template(T) \
699 const static Stub_template<big_endian> template_##T = { \
700 T##_INSNS, sizeof(T##_INSNS) / sizeof(T##_INSNS[0]) }; \
701 this->stub_templates_[T] = &template_##T
703 install_insn_template(ST_NONE);
704 install_insn_template(ST_ADRP_BRANCH);
705 install_insn_template(ST_LONG_BRANCH_ABS);
706 install_insn_template(ST_LONG_BRANCH_PCREL);
707 install_insn_template(ST_E_843419);
709 #undef install_insn_template
713 // Base class for stubs.
715 template<int size, bool big_endian>
719 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
720 typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
722 static const AArch64_address invalid_address =
723 static_cast<AArch64_address>(-1);
725 static const section_offset_type invalid_offset =
726 static_cast<section_offset_type>(-1);
729 : destination_address_(invalid_address),
730 offset_(invalid_offset),
740 { return this->type_; }
742 // Get stub template that provides stub insn information.
743 const Stub_template<big_endian>*
744 stub_template() const
746 return Stub_template_repertoire<big_endian>::
747 get_stub_template(this->type());
750 // Get destination address.
752 destination_address() const
754 gold_assert(this->destination_address_ != this->invalid_address);
755 return this->destination_address_;
758 // Set destination address.
760 set_destination_address(AArch64_address address)
762 gold_assert(address != this->invalid_address);
763 this->destination_address_ = address;
766 // Reset the destination address.
768 reset_destination_address()
769 { this->destination_address_ = this->invalid_address; }
771 // Get offset of code stub. For Reloc_stub, it is the offset from the
772 // beginning of its containing stub table; for Erratum_stub, it is the offset
773 // from the end of reloc_stubs.
777 gold_assert(this->offset_ != this->invalid_offset);
778 return this->offset_;
783 set_offset(section_offset_type offset)
784 { this->offset_ = offset; }
786 // Return the stub insn.
789 { return this->stub_template()->insns; }
791 // Return num of stub insns.
794 { return this->stub_template()->insn_num; }
796 // Get size of the stub.
800 return this->insn_num() *
801 AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
804 // Write stub to output file.
806 write(unsigned char* view, section_size_type view_size)
807 { this->do_write(view, view_size); }
810 // Abstract method to be implemented by sub-classes.
812 do_write(unsigned char*, section_size_type) = 0;
815 // The last insn of a stub is a jump to destination insn. This field records
816 // the destination address.
817 AArch64_address destination_address_;
818 // The stub offset. Note this has difference interpretations between an
819 // Reloc_stub and an Erratum_stub. For Reloc_stub this is the offset from the
820 // beginning of the containing stub_table, whereas for Erratum_stub, this is
821 // the offset from the end of reloc_stubs.
822 section_offset_type offset_;
825 }; // End of "Stub_base".
828 // Erratum stub class. An erratum stub differs from a reloc stub in that for
829 // each erratum occurrence, we generate an erratum stub. We never share erratum
830 // stubs, whereas for reloc stubs, different branches insns share a single reloc
831 // stub as long as the branch targets are the same. (More to the point, reloc
832 // stubs can be shared because they're used to reach a specific target, whereas
833 // erratum stubs branch back to the original control flow.)
835 template<int size, bool big_endian>
836 class Erratum_stub : public Stub_base<size, big_endian>
839 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
840 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
841 typedef typename AArch64_insn_utilities<big_endian>::Insntype Insntype;
843 static const int STUB_ADDR_ALIGN = 4;
845 static const Insntype invalid_insn = static_cast<Insntype>(-1);
847 Erratum_stub(The_aarch64_relobj* relobj, int type,
848 unsigned shndx, unsigned int sh_offset)
849 : Stub_base<size, big_endian>(type), relobj_(relobj),
850 shndx_(shndx), sh_offset_(sh_offset),
851 erratum_insn_(invalid_insn),
852 erratum_address_(this->invalid_address)
857 // Return the object that contains the erratum.
860 { return this->relobj_; }
862 // Get section index of the erratum.
865 { return this->shndx_; }
867 // Get section offset of the erratum.
870 { return this->sh_offset_; }
872 // Get the erratum insn. This is the insn located at erratum_insn_address.
876 gold_assert(this->erratum_insn_ != this->invalid_insn);
877 return this->erratum_insn_;
880 // Set the insn that the erratum happens to.
882 set_erratum_insn(Insntype insn)
883 { this->erratum_insn_ = insn; }
885 // Return the address where an erratum must be done.
887 erratum_address() const
889 gold_assert(this->erratum_address_ != this->invalid_address);
890 return this->erratum_address_;
893 // Set the address where an erratum must be done.
895 set_erratum_address(AArch64_address addr)
896 { this->erratum_address_ = addr; }
898 // Comparator used to group Erratum_stubs in a set by (obj, shndx,
899 // sh_offset). We do not include 'type' in the calculation, becuase there is
900 // at most one stub type at (obj, shndx, sh_offset).
902 operator<(const Erratum_stub<size, big_endian>& k) const
906 // We group stubs by relobj.
907 if (this->relobj_ != k.relobj_)
908 return this->relobj_ < k.relobj_;
909 // Then by section index.
910 if (this->shndx_ != k.shndx_)
911 return this->shndx_ < k.shndx_;
912 // Lastly by section offset.
913 return this->sh_offset_ < k.sh_offset_;
918 do_write(unsigned char*, section_size_type);
921 // The object that needs to be fixed.
922 The_aarch64_relobj* relobj_;
923 // The shndx in the object that needs to be fixed.
924 const unsigned int shndx_;
925 // The section offset in the obejct that needs to be fixed.
926 const unsigned int sh_offset_;
927 // The insn to be fixed.
928 Insntype erratum_insn_;
929 // The address of the above insn.
930 AArch64_address erratum_address_;
931 }; // End of "Erratum_stub".
934 // Comparator used in set definition.
935 template<int size, bool big_endian>
936 struct Erratum_stub_less
939 operator()(const Erratum_stub<size, big_endian>* s1,
940 const Erratum_stub<size, big_endian>* s2) const
941 { return *s1 < *s2; }
944 // Erratum_stub implementation for writing stub to output file.
946 template<int size, bool big_endian>
948 Erratum_stub<size, big_endian>::do_write(unsigned char* view, section_size_type)
950 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
951 const Insntype* insns = this->insns();
952 uint32_t num_insns = this->insn_num();
953 Insntype* ip = reinterpret_cast<Insntype*>(view);
954 // For current implemnted erratum 843419, (and 835769 which is to be
955 // implemented soon), the first insn in the stub is always a copy of the
956 // problematic insn (in 843419, the mem access insn), followed by a jump-back.
957 elfcpp::Swap<32, big_endian>::writeval(ip, this->erratum_insn());
958 for (uint32_t i = 1; i < num_insns; ++i)
959 elfcpp::Swap<32, big_endian>::writeval(ip + i, insns[i]);
965 template<int size, bool big_endian>
966 class Reloc_stub : public Stub_base<size, big_endian>
969 typedef Reloc_stub<size, big_endian> This;
970 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
972 // Branch range. This is used to calculate the section group size, as well as
973 // determine whether a stub is needed.
974 static const int MAX_BRANCH_OFFSET = ((1 << 25) - 1) << 2;
975 static const int MIN_BRANCH_OFFSET = -((1 << 25) << 2);
977 // Constant used to determine if an offset fits in the adrp instruction
979 static const int MAX_ADRP_IMM = (1 << 20) - 1;
980 static const int MIN_ADRP_IMM = -(1 << 20);
982 static const int BYTES_PER_INSN = 4;
983 static const int STUB_ADDR_ALIGN = 4;
985 // Determine whether the offset fits in the jump/branch instruction.
987 aarch64_valid_branch_offset_p(int64_t offset)
988 { return offset >= MIN_BRANCH_OFFSET && offset <= MAX_BRANCH_OFFSET; }
990 // Determine whether the offset fits in the adrp immediate field.
992 aarch64_valid_for_adrp_p(AArch64_address location, AArch64_address dest)
994 typedef AArch64_relocate_functions<size, big_endian> Reloc;
995 int64_t adrp_imm = (Reloc::Page(dest) - Reloc::Page(location)) >> 12;
996 return adrp_imm >= MIN_ADRP_IMM && adrp_imm <= MAX_ADRP_IMM;
999 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
1002 stub_type_for_reloc(unsigned int r_type, AArch64_address address,
1003 AArch64_address target);
1005 Reloc_stub(int type)
1006 : Stub_base<size, big_endian>(type)
1012 // The key class used to index the stub instance in the stub table's stub map.
1016 Key(int type, const Symbol* symbol, const Relobj* relobj,
1017 unsigned int r_sym, int32_t addend)
1018 : type_(type), addend_(addend)
1022 this->r_sym_ = Reloc_stub::invalid_index;
1023 this->u_.symbol = symbol;
1027 gold_assert(relobj != NULL && r_sym != invalid_index);
1028 this->r_sym_ = r_sym;
1029 this->u_.relobj = relobj;
1036 // Return stub type.
1039 { return this->type_; }
1041 // Return the local symbol index or invalid_index.
1044 { return this->r_sym_; }
1046 // Return the symbol if there is one.
1049 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
1051 // Return the relobj if there is one.
1054 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
1056 // Whether this equals to another key k.
1058 eq(const Key& k) const
1060 return ((this->type_ == k.type_)
1061 && (this->r_sym_ == k.r_sym_)
1062 && ((this->r_sym_ != Reloc_stub::invalid_index)
1063 ? (this->u_.relobj == k.u_.relobj)
1064 : (this->u_.symbol == k.u_.symbol))
1065 && (this->addend_ == k.addend_));
1068 // Return a hash value.
1072 size_t name_hash_value = gold::string_hash<char>(
1073 (this->r_sym_ != Reloc_stub::invalid_index)
1074 ? this->u_.relobj->name().c_str()
1075 : this->u_.symbol->name());
1076 // We only have 4 stub types.
1077 size_t stub_type_hash_value = 0x03 & this->type_;
1078 return (name_hash_value
1079 ^ stub_type_hash_value
1080 ^ ((this->r_sym_ & 0x3fff) << 2)
1081 ^ ((this->addend_ & 0xffff) << 16));
1084 // Functors for STL associative containers.
1088 operator()(const Key& k) const
1089 { return k.hash_value(); }
1095 operator()(const Key& k1, const Key& k2) const
1096 { return k1.eq(k2); }
1102 // If this is a local symbol, this is the index in the defining object.
1103 // Otherwise, it is invalid_index for a global symbol.
1104 unsigned int r_sym_;
1105 // If r_sym_ is an invalid index, this points to a global symbol.
1106 // Otherwise, it points to a relobj. We used the unsized and target
1107 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
1108 // Arm_relobj, in order to avoid making the stub class a template
1109 // as most of the stub machinery is endianness-neutral. However, it
1110 // may require a bit of casting done by users of this class.
1113 const Symbol* symbol;
1114 const Relobj* relobj;
1116 // Addend associated with a reloc.
1118 }; // End of inner class Reloc_stub::Key
1121 // This may be overridden in the child class.
1123 do_write(unsigned char*, section_size_type);
1126 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
1127 }; // End of Reloc_stub
1130 // Write data to output file.
1132 template<int size, bool big_endian>
1134 Reloc_stub<size, big_endian>::
1135 do_write(unsigned char* view, section_size_type)
1137 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
1138 const uint32_t* insns = this->insns();
1139 uint32_t num_insns = this->insn_num();
1140 Insntype* ip = reinterpret_cast<Insntype*>(view);
1141 for (uint32_t i = 0; i < num_insns; ++i)
1142 elfcpp::Swap<32, big_endian>::writeval(ip + i, insns[i]);
1146 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
1149 template<int size, bool big_endian>
1151 Reloc_stub<size, big_endian>::stub_type_for_reloc(
1152 unsigned int r_type, AArch64_address location, AArch64_address dest)
1154 int64_t branch_offset = 0;
1157 case elfcpp::R_AARCH64_CALL26:
1158 case elfcpp::R_AARCH64_JUMP26:
1159 branch_offset = dest - location;
1165 if (aarch64_valid_branch_offset_p(branch_offset))
1168 if (aarch64_valid_for_adrp_p(location, dest))
1169 return ST_ADRP_BRANCH;
1171 if (parameters->options().output_is_position_independent()
1172 && parameters->options().output_is_executable())
1173 return ST_LONG_BRANCH_PCREL;
1175 return ST_LONG_BRANCH_ABS;
1178 // A class to hold stubs for the ARM target.
1180 template<int size, bool big_endian>
1181 class Stub_table : public Output_data
1184 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1185 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
1186 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
1187 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1188 typedef Reloc_stub<size, big_endian> The_reloc_stub;
1189 typedef typename The_reloc_stub::Key The_reloc_stub_key;
1190 typedef Erratum_stub<size, big_endian> The_erratum_stub;
1191 typedef Erratum_stub_less<size, big_endian> The_erratum_stub_less;
1192 typedef typename The_reloc_stub_key::hash The_reloc_stub_key_hash;
1193 typedef typename The_reloc_stub_key::equal_to The_reloc_stub_key_equal_to;
1194 typedef Stub_table<size, big_endian> The_stub_table;
1195 typedef Unordered_map<The_reloc_stub_key, The_reloc_stub*,
1196 The_reloc_stub_key_hash, The_reloc_stub_key_equal_to>
1198 typedef typename Reloc_stub_map::const_iterator Reloc_stub_map_const_iter;
1199 typedef Relocate_info<size, big_endian> The_relocate_info;
1201 typedef std::set<The_erratum_stub*, The_erratum_stub_less> Erratum_stub_set;
1202 typedef typename Erratum_stub_set::iterator Erratum_stub_set_iter;
1204 Stub_table(The_aarch64_input_section* owner)
1205 : Output_data(), owner_(owner), reloc_stubs_size_(0),
1206 erratum_stubs_size_(0), prev_data_size_(0)
1212 The_aarch64_input_section*
1216 // Whether this stub table is empty.
1219 { return reloc_stubs_.empty() && erratum_stubs_.empty(); }
1221 // Return the current data size.
1223 current_data_size() const
1224 { return this->current_data_size_for_child(); }
1226 // Add a STUB using KEY. The caller is responsible for avoiding addition
1227 // if a STUB with the same key has already been added.
1229 add_reloc_stub(The_reloc_stub* stub, const The_reloc_stub_key& key);
1231 // Add an erratum stub into the erratum stub set. The set is ordered by
1232 // (relobj, shndx, sh_offset).
1234 add_erratum_stub(The_erratum_stub* stub);
1236 // Find if such erratum exists for any given (obj, shndx, sh_offset).
1238 find_erratum_stub(The_aarch64_relobj* a64relobj,
1239 unsigned int shndx, unsigned int sh_offset);
1241 // Find all the erratums for a given input section. The return value is a pair
1242 // of iterators [begin, end).
1243 std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
1244 find_erratum_stubs_for_input_section(The_aarch64_relobj* a64relobj,
1245 unsigned int shndx);
1247 // Compute the erratum stub address.
1249 erratum_stub_address(The_erratum_stub* stub) const
1251 AArch64_address r = align_address(this->address() + this->reloc_stubs_size_,
1252 The_erratum_stub::STUB_ADDR_ALIGN);
1253 r += stub->offset();
1257 // Finalize stubs. No-op here, just for completeness.
1262 // Look up a relocation stub using KEY. Return NULL if there is none.
1264 find_reloc_stub(The_reloc_stub_key& key)
1266 Reloc_stub_map_const_iter p = this->reloc_stubs_.find(key);
1267 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
1270 // Relocate stubs in this stub table.
1272 relocate_stubs(const The_relocate_info*,
1273 The_target_aarch64*,
1279 // Update data size at the end of a relaxation pass. Return true if data size
1280 // is different from that of the previous relaxation pass.
1282 update_data_size_changed_p()
1284 // No addralign changed here.
1285 off_t s = align_address(this->reloc_stubs_size_,
1286 The_erratum_stub::STUB_ADDR_ALIGN)
1287 + this->erratum_stubs_size_;
1288 bool changed = (s != this->prev_data_size_);
1289 this->prev_data_size_ = s;
1294 // Write out section contents.
1296 do_write(Output_file*);
1298 // Return the required alignment.
1300 do_addralign() const
1302 return std::max(The_reloc_stub::STUB_ADDR_ALIGN,
1303 The_erratum_stub::STUB_ADDR_ALIGN);
1306 // Reset address and file offset.
1308 do_reset_address_and_file_offset()
1309 { this->set_current_data_size_for_child(this->prev_data_size_); }
1311 // Set final data size.
1313 set_final_data_size()
1314 { this->set_data_size(this->current_data_size()); }
1317 // Relocate one stub.
1319 relocate_stub(The_reloc_stub*,
1320 const The_relocate_info*,
1321 The_target_aarch64*,
1328 // Owner of this stub table.
1329 The_aarch64_input_section* owner_;
1330 // The relocation stubs.
1331 Reloc_stub_map reloc_stubs_;
1332 // The erratum stubs.
1333 Erratum_stub_set erratum_stubs_;
1334 // Size of reloc stubs.
1335 off_t reloc_stubs_size_;
1336 // Size of erratum stubs.
1337 off_t erratum_stubs_size_;
1338 // data size of this in the previous pass.
1339 off_t prev_data_size_;
1340 }; // End of Stub_table
1343 // Add an erratum stub into the erratum stub set. The set is ordered by
1344 // (relobj, shndx, sh_offset).
1346 template<int size, bool big_endian>
1348 Stub_table<size, big_endian>::add_erratum_stub(The_erratum_stub* stub)
1350 std::pair<Erratum_stub_set_iter, bool> ret =
1351 this->erratum_stubs_.insert(stub);
1352 gold_assert(ret.second);
1353 this->erratum_stubs_size_ = align_address(
1354 this->erratum_stubs_size_, The_erratum_stub::STUB_ADDR_ALIGN);
1355 stub->set_offset(this->erratum_stubs_size_);
1356 this->erratum_stubs_size_ += stub->stub_size();
1360 // Find if such erratum exists for givein (obj, shndx, sh_offset).
1362 template<int size, bool big_endian>
1363 Erratum_stub<size, big_endian>*
1364 Stub_table<size, big_endian>::find_erratum_stub(
1365 The_aarch64_relobj* a64relobj, unsigned int shndx, unsigned int sh_offset)
1367 // A dummy object used as key to search in the set.
1368 The_erratum_stub key(a64relobj, ST_NONE,
1370 Erratum_stub_set_iter i = this->erratum_stubs_.find(&key);
1371 if (i != this->erratum_stubs_.end())
1373 The_erratum_stub* stub(*i);
1374 gold_assert(stub->erratum_insn() != 0);
1381 // Find all the errata for a given input section. The return value is a pair of
1382 // iterators [begin, end).
1384 template<int size, bool big_endian>
1385 std::pair<typename Stub_table<size, big_endian>::Erratum_stub_set_iter,
1386 typename Stub_table<size, big_endian>::Erratum_stub_set_iter>
1387 Stub_table<size, big_endian>::find_erratum_stubs_for_input_section(
1388 The_aarch64_relobj* a64relobj, unsigned int shndx)
1390 typedef std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter> Result_pair;
1391 Erratum_stub_set_iter start, end;
1392 The_erratum_stub low_key(a64relobj, ST_NONE, shndx, 0);
1393 start = this->erratum_stubs_.lower_bound(&low_key);
1394 if (start == this->erratum_stubs_.end())
1395 return Result_pair(this->erratum_stubs_.end(),
1396 this->erratum_stubs_.end());
1398 while (end != this->erratum_stubs_.end() &&
1399 (*end)->relobj() == a64relobj && (*end)->shndx() == shndx)
1401 return Result_pair(start, end);
1405 // Add a STUB using KEY. The caller is responsible for avoiding addition
1406 // if a STUB with the same key has already been added.
1408 template<int size, bool big_endian>
1410 Stub_table<size, big_endian>::add_reloc_stub(
1411 The_reloc_stub* stub, const The_reloc_stub_key& key)
1413 gold_assert(stub->type() == key.type());
1414 this->reloc_stubs_[key] = stub;
1416 // Assign stub offset early. We can do this because we never remove
1417 // reloc stubs and they are in the beginning of the stub table.
1418 this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_,
1419 The_reloc_stub::STUB_ADDR_ALIGN);
1420 stub->set_offset(this->reloc_stubs_size_);
1421 this->reloc_stubs_size_ += stub->stub_size();
1425 // Relocate all stubs in this stub table.
1427 template<int size, bool big_endian>
1429 Stub_table<size, big_endian>::
1430 relocate_stubs(const The_relocate_info* relinfo,
1431 The_target_aarch64* target_aarch64,
1432 Output_section* output_section,
1433 unsigned char* view,
1434 AArch64_address address,
1435 section_size_type view_size)
1437 // "view_size" is the total size of the stub_table.
1438 gold_assert(address == this->address() &&
1439 view_size == static_cast<section_size_type>(this->data_size()));
1440 for(Reloc_stub_map_const_iter p = this->reloc_stubs_.begin();
1441 p != this->reloc_stubs_.end(); ++p)
1442 relocate_stub(p->second, relinfo, target_aarch64, output_section,
1443 view, address, view_size);
1445 // Just for convenience.
1446 const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
1448 // Now 'relocate' erratum stubs.
1449 for(Erratum_stub_set_iter i = this->erratum_stubs_.begin();
1450 i != this->erratum_stubs_.end(); ++i)
1452 AArch64_address stub_address = this->erratum_stub_address(*i);
1453 // The address of "b" in the stub that is to be "relocated".
1454 AArch64_address stub_b_insn_address;
1455 // Branch offset that is to be filled in "b" insn.
1457 switch ((*i)->type())
1460 // For the erratum, the 2nd insn is a b-insn to be patched
1462 stub_b_insn_address = stub_address + 1 * BPI;
1463 b_offset = (*i)->destination_address() - stub_b_insn_address;
1464 AArch64_relocate_functions<size, big_endian>::construct_b(
1465 view + (stub_b_insn_address - this->address()),
1466 ((unsigned int)(b_offset)) & 0xfffffff);
1476 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
1478 template<int size, bool big_endian>
1480 Stub_table<size, big_endian>::
1481 relocate_stub(The_reloc_stub* stub,
1482 const The_relocate_info* relinfo,
1483 The_target_aarch64* target_aarch64,
1484 Output_section* output_section,
1485 unsigned char* view,
1486 AArch64_address address,
1487 section_size_type view_size)
1489 // "offset" is the offset from the beginning of the stub_table.
1490 section_size_type offset = stub->offset();
1491 section_size_type stub_size = stub->stub_size();
1492 // "view_size" is the total size of the stub_table.
1493 gold_assert(offset + stub_size <= view_size);
1495 target_aarch64->relocate_stub(stub, relinfo, output_section,
1496 view + offset, address + offset, view_size);
1500 // Write out the stubs to file.
1502 template<int size, bool big_endian>
1504 Stub_table<size, big_endian>::do_write(Output_file* of)
1506 off_t offset = this->offset();
1507 const section_size_type oview_size =
1508 convert_to_section_size_type(this->data_size());
1509 unsigned char* const oview = of->get_output_view(offset, oview_size);
1511 // Write relocation stubs.
1512 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
1513 p != this->reloc_stubs_.end(); ++p)
1515 The_reloc_stub* stub = p->second;
1516 AArch64_address address = this->address() + stub->offset();
1517 gold_assert(address ==
1518 align_address(address, The_reloc_stub::STUB_ADDR_ALIGN));
1519 stub->write(oview + stub->offset(), stub->stub_size());
1522 // Write erratum stubs.
1523 unsigned int erratum_stub_start_offset =
1524 align_address(this->reloc_stubs_size_, The_erratum_stub::STUB_ADDR_ALIGN);
1525 for (typename Erratum_stub_set::iterator p = this->erratum_stubs_.begin();
1526 p != this->erratum_stubs_.end(); ++p)
1528 The_erratum_stub* stub(*p);
1529 stub->write(oview + erratum_stub_start_offset + stub->offset(),
1533 of->write_output_view(this->offset(), oview_size, oview);
1537 // AArch64_relobj class.
1539 template<int size, bool big_endian>
1540 class AArch64_relobj : public Sized_relobj_file<size, big_endian>
1543 typedef AArch64_relobj<size, big_endian> This;
1544 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1545 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1546 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
1547 typedef Stub_table<size, big_endian> The_stub_table;
1548 typedef Erratum_stub<size, big_endian> The_erratum_stub;
1549 typedef typename The_stub_table::Erratum_stub_set_iter Erratum_stub_set_iter;
1550 typedef std::vector<The_stub_table*> Stub_table_list;
1551 static const AArch64_address invalid_address =
1552 static_cast<AArch64_address>(-1);
1554 AArch64_relobj(const std::string& name, Input_file* input_file, off_t offset,
1555 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1556 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1563 // Return the stub table of the SHNDX-th section if there is one.
1565 stub_table(unsigned int shndx) const
1567 gold_assert(shndx < this->stub_tables_.size());
1568 return this->stub_tables_[shndx];
1571 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1573 set_stub_table(unsigned int shndx, The_stub_table* stub_table)
1575 gold_assert(shndx < this->stub_tables_.size());
1576 this->stub_tables_[shndx] = stub_table;
1579 // Entrance to erratum_843419 scanning.
1581 scan_erratum_843419(unsigned int shndx,
1582 const elfcpp::Shdr<size, big_endian>&,
1583 Output_section*, const Symbol_table*,
1584 The_target_aarch64*);
1586 // Scan all relocation sections for stub generation.
1588 scan_sections_for_stubs(The_target_aarch64*, const Symbol_table*,
1591 // Whether a section is a scannable text section.
1593 text_section_is_scannable(const elfcpp::Shdr<size, big_endian>&, unsigned int,
1594 const Output_section*, const Symbol_table*);
1596 // Convert regular input section with index SHNDX to a relaxed section.
1598 convert_input_section_to_relaxed_section(unsigned /* shndx */)
1600 // The stubs have relocations and we need to process them after writing
1601 // out the stubs. So relocation now must follow section write.
1602 this->set_relocs_must_follow_section_writes();
1605 // Structure for mapping symbol position.
1606 struct Mapping_symbol_position
1608 Mapping_symbol_position(unsigned int shndx, AArch64_address offset):
1609 shndx_(shndx), offset_(offset)
1612 // "<" comparator used in ordered_map container.
1614 operator<(const Mapping_symbol_position& p) const
1616 return (this->shndx_ < p.shndx_
1617 || (this->shndx_ == p.shndx_ && this->offset_ < p.offset_));
1621 unsigned int shndx_;
1624 AArch64_address offset_;
1627 typedef std::map<Mapping_symbol_position, char> Mapping_symbol_info;
1630 // Post constructor setup.
1634 // Call parent's setup method.
1635 Sized_relobj_file<size, big_endian>::do_setup();
1637 // Initialize look-up tables.
1638 this->stub_tables_.resize(this->shnum());
1642 do_relocate_sections(
1643 const Symbol_table* symtab, const Layout* layout,
1644 const unsigned char* pshdrs, Output_file* of,
1645 typename Sized_relobj_file<size, big_endian>::Views* pviews);
1647 // Count local symbols and (optionally) record mapping info.
1649 do_count_local_symbols(Stringpool_template<char>*,
1650 Stringpool_template<char>*);
1653 // Fix all errata in the object.
1655 fix_errata(typename Sized_relobj_file<size, big_endian>::Views* pviews);
1657 // Whether a section needs to be scanned for relocation stubs.
1659 section_needs_reloc_stub_scanning(const elfcpp::Shdr<size, big_endian>&,
1660 const Relobj::Output_sections&,
1661 const Symbol_table*, const unsigned char*);
1663 // List of stub tables.
1664 Stub_table_list stub_tables_;
1666 // Mapping symbol information sorted by (section index, section_offset).
1667 Mapping_symbol_info mapping_symbol_info_;
1668 }; // End of AArch64_relobj
1671 // Override to record mapping symbol information.
1672 template<int size, bool big_endian>
1674 AArch64_relobj<size, big_endian>::do_count_local_symbols(
1675 Stringpool_template<char>* pool, Stringpool_template<char>* dynpool)
1677 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
1679 // Only erratum-fixing work needs mapping symbols, so skip this time consuming
1680 // processing if not fixing erratum.
1681 if (!parameters->options().fix_cortex_a53_843419())
1684 const unsigned int loccount = this->local_symbol_count();
1688 // Read the symbol table section header.
1689 const unsigned int symtab_shndx = this->symtab_shndx();
1690 elfcpp::Shdr<size, big_endian>
1691 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
1692 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1694 // Read the local symbols.
1695 const int sym_size =elfcpp::Elf_sizes<size>::sym_size;
1696 gold_assert(loccount == symtabshdr.get_sh_info());
1697 off_t locsize = loccount * sym_size;
1698 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1699 locsize, true, true);
1701 // For mapping symbol processing, we need to read the symbol names.
1702 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
1703 if (strtab_shndx >= this->shnum())
1705 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
1709 elfcpp::Shdr<size, big_endian>
1710 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
1711 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
1713 this->error(_("symbol table name section has wrong type: %u"),
1714 static_cast<unsigned int>(strtabshdr.get_sh_type()));
1718 const char* pnames =
1719 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
1720 strtabshdr.get_sh_size(),
1723 // Skip the first dummy symbol.
1725 typename Sized_relobj_file<size, big_endian>::Local_values*
1726 plocal_values = this->local_values();
1727 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1729 elfcpp::Sym<size, big_endian> sym(psyms);
1730 Symbol_value<size>& lv((*plocal_values)[i]);
1731 AArch64_address input_value = lv.input_value();
1733 // Check to see if this is a mapping symbol.
1734 const char* sym_name = pnames + sym.get_st_name();
1735 if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd')
1736 && sym_name[2] == '\0')
1739 unsigned int input_shndx =
1740 this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
1741 gold_assert(is_ordinary);
1743 Mapping_symbol_position msp(input_shndx, input_value);
1744 // Insert mapping_symbol_info into map whose ordering is defined by
1745 // (shndx, offset_within_section).
1746 this->mapping_symbol_info_[msp] = sym_name[1];
1752 // Fix all errata in the object.
1754 template<int size, bool big_endian>
1756 AArch64_relobj<size, big_endian>::fix_errata(
1757 typename Sized_relobj_file<size, big_endian>::Views* pviews)
1759 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
1760 unsigned int shnum = this->shnum();
1761 for (unsigned int i = 1; i < shnum; ++i)
1763 The_stub_table* stub_table = this->stub_table(i);
1766 std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
1767 ipair(stub_table->find_erratum_stubs_for_input_section(this, i));
1768 Erratum_stub_set_iter p = ipair.first, end = ipair.second;
1771 The_erratum_stub* stub = *p;
1772 typename Sized_relobj_file<size, big_endian>::View_size&
1773 pview((*pviews)[i]);
1775 // Double check data before fix.
1777 reinterpret_cast<Insntype*>(pview.view + stub->sh_offset());
1778 Insntype insn_to_fix = ip[0];
1779 gold_assert(insn_to_fix == stub->erratum_insn());
1780 gold_assert(pview.address + stub->sh_offset()
1781 == stub->erratum_address());
1783 AArch64_address stub_address =
1784 stub_table->erratum_stub_address(stub);
1785 unsigned int b_offset = stub_address - stub->erratum_address();
1786 AArch64_relocate_functions<size, big_endian>::construct_b(
1787 pview.view + stub->sh_offset(), b_offset & 0xfffffff);
1794 // Relocate sections.
1796 template<int size, bool big_endian>
1798 AArch64_relobj<size, big_endian>::do_relocate_sections(
1799 const Symbol_table* symtab, const Layout* layout,
1800 const unsigned char* pshdrs, Output_file* of,
1801 typename Sized_relobj_file<size, big_endian>::Views* pviews)
1803 // Call parent to relocate sections.
1804 Sized_relobj_file<size, big_endian>::do_relocate_sections(symtab, layout,
1805 pshdrs, of, pviews);
1807 // We do not generate stubs if doing a relocatable link.
1808 if (parameters->options().relocatable())
1811 if (parameters->options().fix_cortex_a53_843419())
1812 this->fix_errata(pviews);
1814 Relocate_info<size, big_endian> relinfo;
1815 relinfo.symtab = symtab;
1816 relinfo.layout = layout;
1817 relinfo.object = this;
1819 // Relocate stub tables.
1820 unsigned int shnum = this->shnum();
1821 The_target_aarch64* target = The_target_aarch64::current_target();
1823 for (unsigned int i = 1; i < shnum; ++i)
1825 The_aarch64_input_section* aarch64_input_section =
1826 target->find_aarch64_input_section(this, i);
1827 if (aarch64_input_section != NULL
1828 && aarch64_input_section->is_stub_table_owner()
1829 && !aarch64_input_section->stub_table()->empty())
1831 Output_section* os = this->output_section(i);
1832 gold_assert(os != NULL);
1834 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
1835 relinfo.reloc_shdr = NULL;
1836 relinfo.data_shndx = i;
1837 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<size>::shdr_size;
1839 typename Sized_relobj_file<size, big_endian>::View_size&
1840 view_struct = (*pviews)[i];
1841 gold_assert(view_struct.view != NULL);
1843 The_stub_table* stub_table = aarch64_input_section->stub_table();
1844 off_t offset = stub_table->address() - view_struct.address;
1845 unsigned char* view = view_struct.view + offset;
1846 AArch64_address address = stub_table->address();
1847 section_size_type view_size = stub_table->data_size();
1848 stub_table->relocate_stubs(&relinfo, target, os, view, address,
1855 // Determine if an input section is scannable for stub processing. SHDR is
1856 // the header of the section and SHNDX is the section index. OS is the output
1857 // section for the input section and SYMTAB is the global symbol table used to
1858 // look up ICF information.
1860 template<int size, bool big_endian>
1862 AArch64_relobj<size, big_endian>::text_section_is_scannable(
1863 const elfcpp::Shdr<size, big_endian>& text_shdr,
1864 unsigned int text_shndx,
1865 const Output_section* os,
1866 const Symbol_table* symtab)
1868 // Skip any empty sections, unallocated sections or sections whose
1869 // type are not SHT_PROGBITS.
1870 if (text_shdr.get_sh_size() == 0
1871 || (text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
1872 || text_shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
1875 // Skip any discarded or ICF'ed sections.
1876 if (os == NULL || symtab->is_section_folded(this, text_shndx))
1879 // Skip exception frame.
1880 if (strcmp(os->name(), ".eh_frame") == 0)
1883 gold_assert(!this->is_output_section_offset_invalid(text_shndx) ||
1884 os->find_relaxed_input_section(this, text_shndx) != NULL);
1890 // Determine if we want to scan the SHNDX-th section for relocation stubs.
1891 // This is a helper for AArch64_relobj::scan_sections_for_stubs().
1893 template<int size, bool big_endian>
1895 AArch64_relobj<size, big_endian>::section_needs_reloc_stub_scanning(
1896 const elfcpp::Shdr<size, big_endian>& shdr,
1897 const Relobj::Output_sections& out_sections,
1898 const Symbol_table* symtab,
1899 const unsigned char* pshdrs)
1901 unsigned int sh_type = shdr.get_sh_type();
1902 if (sh_type != elfcpp::SHT_RELA)
1905 // Ignore empty section.
1906 off_t sh_size = shdr.get_sh_size();
1910 // Ignore reloc section with unexpected symbol table. The
1911 // error will be reported in the final link.
1912 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
1915 gold_assert(sh_type == elfcpp::SHT_RELA);
1916 unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
1918 // Ignore reloc section with unexpected entsize or uneven size.
1919 // The error will be reported in the final link.
1920 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
1923 // Ignore reloc section with bad info. This error will be
1924 // reported in the final link.
1925 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_info());
1926 if (text_shndx >= this->shnum())
1929 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1930 const elfcpp::Shdr<size, big_endian> text_shdr(pshdrs +
1931 text_shndx * shdr_size);
1932 return this->text_section_is_scannable(text_shdr, text_shndx,
1933 out_sections[text_shndx], symtab);
1937 // Scan section SHNDX for erratum 843419.
1939 template<int size, bool big_endian>
1941 AArch64_relobj<size, big_endian>::scan_erratum_843419(
1942 unsigned int shndx, const elfcpp::Shdr<size, big_endian>& shdr,
1943 Output_section* os, const Symbol_table* symtab,
1944 The_target_aarch64* target)
1946 if (shdr.get_sh_size() == 0
1947 || (shdr.get_sh_flags() &
1948 (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) == 0
1949 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
1952 if (!os || symtab->is_section_folded(this, shndx)) return;
1954 AArch64_address output_offset = this->get_output_section_offset(shndx);
1955 AArch64_address output_address;
1956 if (output_offset != invalid_address)
1957 output_address = os->address() + output_offset;
1960 const Output_relaxed_input_section* poris =
1961 os->find_relaxed_input_section(this, shndx);
1963 output_address = poris->address();
1966 section_size_type input_view_size = 0;
1967 const unsigned char* input_view =
1968 this->section_contents(shndx, &input_view_size, false);
1970 Mapping_symbol_position section_start(shndx, 0);
1971 // Find the first mapping symbol record within section shndx.
1972 typename Mapping_symbol_info::const_iterator p =
1973 this->mapping_symbol_info_.lower_bound(section_start);
1974 if (p == this->mapping_symbol_info_.end() || p->first.shndx_ != shndx)
1975 gold_warning(_("cannot scan executable section %u of %s for Cortex-A53 "
1976 "erratum because it has no mapping symbols."),
1977 shndx, this->name().c_str());
1978 while (p != this->mapping_symbol_info_.end() &&
1979 p->first.shndx_ == shndx)
1981 typename Mapping_symbol_info::const_iterator prev = p;
1983 if (prev->second == 'x')
1985 section_size_type span_start =
1986 convert_to_section_size_type(prev->first.offset_);
1987 section_size_type span_end;
1988 if (p != this->mapping_symbol_info_.end()
1989 && p->first.shndx_ == shndx)
1990 span_end = convert_to_section_size_type(p->first.offset_);
1992 span_end = convert_to_section_size_type(shdr.get_sh_size());
1993 target->scan_erratum_843419_span(
1994 this, shndx, span_start, span_end,
1995 const_cast<unsigned char*>(input_view), output_address);
2001 // Scan relocations for stub generation.
2003 template<int size, bool big_endian>
2005 AArch64_relobj<size, big_endian>::scan_sections_for_stubs(
2006 The_target_aarch64* target,
2007 const Symbol_table* symtab,
2008 const Layout* layout)
2010 unsigned int shnum = this->shnum();
2011 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2013 // Read the section headers.
2014 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
2018 // To speed up processing, we set up hash tables for fast lookup of
2019 // input offsets to output addresses.
2020 this->initialize_input_to_output_maps();
2022 const Relobj::Output_sections& out_sections(this->output_sections());
2024 Relocate_info<size, big_endian> relinfo;
2025 relinfo.symtab = symtab;
2026 relinfo.layout = layout;
2027 relinfo.object = this;
2029 // Do relocation stubs scanning.
2030 const unsigned char* p = pshdrs + shdr_size;
2031 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
2033 const elfcpp::Shdr<size, big_endian> shdr(p);
2034 if (parameters->options().fix_cortex_a53_843419())
2035 scan_erratum_843419(i, shdr, out_sections[i], symtab, target);
2036 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
2039 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
2040 AArch64_address output_offset =
2041 this->get_output_section_offset(index);
2042 AArch64_address output_address;
2043 if (output_offset != invalid_address)
2045 output_address = out_sections[index]->address() + output_offset;
2049 // Currently this only happens for a relaxed section.
2050 const Output_relaxed_input_section* poris =
2051 out_sections[index]->find_relaxed_input_section(this, index);
2052 gold_assert(poris != NULL);
2053 output_address = poris->address();
2056 // Get the relocations.
2057 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
2061 // Get the section contents.
2062 section_size_type input_view_size = 0;
2063 const unsigned char* input_view =
2064 this->section_contents(index, &input_view_size, false);
2066 relinfo.reloc_shndx = i;
2067 relinfo.data_shndx = index;
2068 unsigned int sh_type = shdr.get_sh_type();
2069 unsigned int reloc_size;
2070 gold_assert (sh_type == elfcpp::SHT_RELA);
2071 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2073 Output_section* os = out_sections[index];
2074 target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
2075 shdr.get_sh_size() / reloc_size,
2077 output_offset == invalid_address,
2078 input_view, output_address,
2085 // A class to wrap an ordinary input section containing executable code.
2087 template<int size, bool big_endian>
2088 class AArch64_input_section : public Output_relaxed_input_section
2091 typedef Stub_table<size, big_endian> The_stub_table;
2093 AArch64_input_section(Relobj* relobj, unsigned int shndx)
2094 : Output_relaxed_input_section(relobj, shndx, 1),
2096 original_contents_(NULL), original_size_(0),
2097 original_addralign_(1)
2100 ~AArch64_input_section()
2101 { delete[] this->original_contents_; }
2107 // Set the stub_table.
2109 set_stub_table(The_stub_table* st)
2110 { this->stub_table_ = st; }
2112 // Whether this is a stub table owner.
2114 is_stub_table_owner() const
2115 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
2117 // Return the original size of the section.
2119 original_size() const
2120 { return this->original_size_; }
2122 // Return the stub table.
2125 { return stub_table_; }
2128 // Write out this input section.
2130 do_write(Output_file*);
2132 // Return required alignment of this.
2134 do_addralign() const
2136 if (this->is_stub_table_owner())
2137 return std::max(this->stub_table_->addralign(),
2138 static_cast<uint64_t>(this->original_addralign_));
2140 return this->original_addralign_;
2143 // Finalize data size.
2145 set_final_data_size();
2147 // Reset address and file offset.
2149 do_reset_address_and_file_offset();
2153 do_output_offset(const Relobj* object, unsigned int shndx,
2154 section_offset_type offset,
2155 section_offset_type* poutput) const
2157 if ((object == this->relobj())
2158 && (shndx == this->shndx())
2161 convert_types<section_offset_type, uint32_t>(this->original_size_)))
2171 // Copying is not allowed.
2172 AArch64_input_section(const AArch64_input_section&);
2173 AArch64_input_section& operator=(const AArch64_input_section&);
2175 // The relocation stubs.
2176 The_stub_table* stub_table_;
2177 // Original section contents. We have to make a copy here since the file
2178 // containing the original section may not be locked when we need to access
2180 unsigned char* original_contents_;
2181 // Section size of the original input section.
2182 uint32_t original_size_;
2183 // Address alignment of the original input section.
2184 uint32_t original_addralign_;
2185 }; // End of AArch64_input_section
2188 // Finalize data size.
2190 template<int size, bool big_endian>
2192 AArch64_input_section<size, big_endian>::set_final_data_size()
2194 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2196 if (this->is_stub_table_owner())
2198 this->stub_table_->finalize_data_size();
2199 off = align_address(off, this->stub_table_->addralign());
2200 off += this->stub_table_->data_size();
2202 this->set_data_size(off);
2206 // Reset address and file offset.
2208 template<int size, bool big_endian>
2210 AArch64_input_section<size, big_endian>::do_reset_address_and_file_offset()
2212 // Size of the original input section contents.
2213 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2215 // If this is a stub table owner, account for the stub table size.
2216 if (this->is_stub_table_owner())
2218 The_stub_table* stub_table = this->stub_table_;
2220 // Reset the stub table's address and file offset. The
2221 // current data size for child will be updated after that.
2222 stub_table_->reset_address_and_file_offset();
2223 off = align_address(off, stub_table_->addralign());
2224 off += stub_table->current_data_size();
2227 this->set_current_data_size(off);
2231 // Initialize an Arm_input_section.
2233 template<int size, bool big_endian>
2235 AArch64_input_section<size, big_endian>::init()
2237 Relobj* relobj = this->relobj();
2238 unsigned int shndx = this->shndx();
2240 // We have to cache original size, alignment and contents to avoid locking
2241 // the original file.
2242 this->original_addralign_ =
2243 convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
2245 // This is not efficient but we expect only a small number of relaxed
2246 // input sections for stubs.
2247 section_size_type section_size;
2248 const unsigned char* section_contents =
2249 relobj->section_contents(shndx, §ion_size, false);
2250 this->original_size_ =
2251 convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
2253 gold_assert(this->original_contents_ == NULL);
2254 this->original_contents_ = new unsigned char[section_size];
2255 memcpy(this->original_contents_, section_contents, section_size);
2257 // We want to make this look like the original input section after
2258 // output sections are finalized.
2259 Output_section* os = relobj->output_section(shndx);
2260 off_t offset = relobj->output_section_offset(shndx);
2261 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2262 this->set_address(os->address() + offset);
2263 this->set_file_offset(os->offset() + offset);
2264 this->set_current_data_size(this->original_size_);
2265 this->finalize_data_size();
2269 // Write data to output file.
2271 template<int size, bool big_endian>
2273 AArch64_input_section<size, big_endian>::do_write(Output_file* of)
2275 // We have to write out the original section content.
2276 gold_assert(this->original_contents_ != NULL);
2277 of->write(this->offset(), this->original_contents_,
2278 this->original_size_);
2280 // If this owns a stub table and it is not empty, write it.
2281 if (this->is_stub_table_owner() && !this->stub_table_->empty())
2282 this->stub_table_->write(of);
2286 // Arm output section class. This is defined mainly to add a number of stub
2287 // generation methods.
2289 template<int size, bool big_endian>
2290 class AArch64_output_section : public Output_section
2293 typedef Target_aarch64<size, big_endian> The_target_aarch64;
2294 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2295 typedef Stub_table<size, big_endian> The_stub_table;
2296 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2299 AArch64_output_section(const char* name, elfcpp::Elf_Word type,
2300 elfcpp::Elf_Xword flags)
2301 : Output_section(name, type, flags)
2304 ~AArch64_output_section() {}
2306 // Group input sections for stub generation.
2308 group_sections(section_size_type, bool, Target_aarch64<size, big_endian>*,
2312 typedef Output_section::Input_section Input_section;
2313 typedef Output_section::Input_section_list Input_section_list;
2315 // Create a stub group.
2317 create_stub_group(Input_section_list::const_iterator,
2318 Input_section_list::const_iterator,
2319 Input_section_list::const_iterator,
2320 The_target_aarch64*,
2321 std::vector<Output_relaxed_input_section*>&,
2323 }; // End of AArch64_output_section
2326 // Create a stub group for input sections from FIRST to LAST. OWNER points to
2327 // the input section that will be the owner of the stub table.
2329 template<int size, bool big_endian> void
2330 AArch64_output_section<size, big_endian>::create_stub_group(
2331 Input_section_list::const_iterator first,
2332 Input_section_list::const_iterator last,
2333 Input_section_list::const_iterator owner,
2334 The_target_aarch64* target,
2335 std::vector<Output_relaxed_input_section*>& new_relaxed_sections,
2338 // Currently we convert ordinary input sections into relaxed sections only
2340 The_aarch64_input_section* input_section;
2341 if (owner->is_relaxed_input_section())
2345 gold_assert(owner->is_input_section());
2346 // Create a new relaxed input section. We need to lock the original
2348 Task_lock_obj<Object> tl(task, owner->relobj());
2350 target->new_aarch64_input_section(owner->relobj(), owner->shndx());
2351 new_relaxed_sections.push_back(input_section);
2354 // Create a stub table.
2355 The_stub_table* stub_table =
2356 target->new_stub_table(input_section);
2358 input_section->set_stub_table(stub_table);
2360 Input_section_list::const_iterator p = first;
2361 // Look for input sections or relaxed input sections in [first ... last].
2364 if (p->is_input_section() || p->is_relaxed_input_section())
2366 // The stub table information for input sections live
2367 // in their objects.
2368 The_aarch64_relobj* aarch64_relobj =
2369 static_cast<The_aarch64_relobj*>(p->relobj());
2370 aarch64_relobj->set_stub_table(p->shndx(), stub_table);
2373 while (p++ != last);
2377 // Group input sections for stub generation. GROUP_SIZE is roughly the limit of
2378 // stub groups. We grow a stub group by adding input section until the size is
2379 // just below GROUP_SIZE. The last input section will be converted into a stub
2380 // table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
2381 // after the stub table, effectively doubling the group size.
2383 // This is similar to the group_sections() function in elf32-arm.c but is
2384 // implemented differently.
2386 template<int size, bool big_endian>
2387 void AArch64_output_section<size, big_endian>::group_sections(
2388 section_size_type group_size,
2389 bool stubs_always_after_branch,
2390 Target_aarch64<size, big_endian>* target,
2396 FINDING_STUB_SECTION,
2400 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
2402 State state = NO_GROUP;
2403 section_size_type off = 0;
2404 section_size_type group_begin_offset = 0;
2405 section_size_type group_end_offset = 0;
2406 section_size_type stub_table_end_offset = 0;
2407 Input_section_list::const_iterator group_begin =
2408 this->input_sections().end();
2409 Input_section_list::const_iterator stub_table =
2410 this->input_sections().end();
2411 Input_section_list::const_iterator group_end = this->input_sections().end();
2412 for (Input_section_list::const_iterator p = this->input_sections().begin();
2413 p != this->input_sections().end();
2416 section_size_type section_begin_offset =
2417 align_address(off, p->addralign());
2418 section_size_type section_end_offset =
2419 section_begin_offset + p->data_size();
2421 // Check to see if we should group the previously seen sections.
2427 case FINDING_STUB_SECTION:
2428 // Adding this section makes the group larger than GROUP_SIZE.
2429 if (section_end_offset - group_begin_offset >= group_size)
2431 if (stubs_always_after_branch)
2433 gold_assert(group_end != this->input_sections().end());
2434 this->create_stub_group(group_begin, group_end, group_end,
2435 target, new_relaxed_sections,
2441 // Input sections up to stub_group_size bytes after the stub
2442 // table can be handled by it too.
2443 state = HAS_STUB_SECTION;
2444 stub_table = group_end;
2445 stub_table_end_offset = group_end_offset;
2450 case HAS_STUB_SECTION:
2451 // Adding this section makes the post stub-section group larger
2454 // NOT SUPPORTED YET. For completeness only.
2455 if (section_end_offset - stub_table_end_offset >= group_size)
2457 gold_assert(group_end != this->input_sections().end());
2458 this->create_stub_group(group_begin, group_end, stub_table,
2459 target, new_relaxed_sections, task);
2468 // If we see an input section and currently there is no group, start
2469 // a new one. Skip any empty sections. We look at the data size
2470 // instead of calling p->relobj()->section_size() to avoid locking.
2471 if ((p->is_input_section() || p->is_relaxed_input_section())
2472 && (p->data_size() != 0))
2474 if (state == NO_GROUP)
2476 state = FINDING_STUB_SECTION;
2478 group_begin_offset = section_begin_offset;
2481 // Keep track of the last input section seen.
2483 group_end_offset = section_end_offset;
2486 off = section_end_offset;
2489 // Create a stub group for any ungrouped sections.
2490 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
2492 gold_assert(group_end != this->input_sections().end());
2493 this->create_stub_group(group_begin, group_end,
2494 (state == FINDING_STUB_SECTION
2497 target, new_relaxed_sections, task);
2500 if (!new_relaxed_sections.empty())
2501 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
2503 // Update the section offsets
2504 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
2506 The_aarch64_relobj* relobj = static_cast<The_aarch64_relobj*>(
2507 new_relaxed_sections[i]->relobj());
2508 unsigned int shndx = new_relaxed_sections[i]->shndx();
2509 // Tell AArch64_relobj that this input section is converted.
2510 relobj->convert_input_section_to_relaxed_section(shndx);
2512 } // End of AArch64_output_section::group_sections
2515 AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
2518 // The aarch64 target class.
2520 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
2521 template<int size, bool big_endian>
2522 class Target_aarch64 : public Sized_target<size, big_endian>
2525 typedef Target_aarch64<size, big_endian> This;
2526 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
2528 typedef Relocate_info<size, big_endian> The_relocate_info;
2529 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2530 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2531 typedef Reloc_stub<size, big_endian> The_reloc_stub;
2532 typedef Erratum_stub<size, big_endian> The_erratum_stub;
2533 typedef typename Reloc_stub<size, big_endian>::Key The_reloc_stub_key;
2534 typedef Stub_table<size, big_endian> The_stub_table;
2535 typedef std::vector<The_stub_table*> Stub_table_list;
2536 typedef typename Stub_table_list::iterator Stub_table_iterator;
2537 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2538 typedef AArch64_output_section<size, big_endian> The_aarch64_output_section;
2539 typedef Unordered_map<Section_id,
2540 AArch64_input_section<size, big_endian>*,
2541 Section_id_hash> AArch64_input_section_map;
2542 typedef AArch64_insn_utilities<big_endian> Insn_utilities;
2543 const static int TCB_SIZE = size / 8 * 2;
2545 Target_aarch64(const Target::Target_info* info = &aarch64_info)
2546 : Sized_target<size, big_endian>(info),
2547 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
2548 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
2549 rela_irelative_(NULL), copy_relocs_(elfcpp::R_AARCH64_COPY),
2550 got_mod_index_offset_(-1U),
2551 tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
2552 stub_tables_(), stub_group_size_(0), aarch64_input_section_map_()
2555 // Scan the relocations to determine unreferenced sections for
2556 // garbage collection.
2558 gc_process_relocs(Symbol_table* symtab,
2560 Sized_relobj_file<size, big_endian>* object,
2561 unsigned int data_shndx,
2562 unsigned int sh_type,
2563 const unsigned char* prelocs,
2565 Output_section* output_section,
2566 bool needs_special_offset_handling,
2567 size_t local_symbol_count,
2568 const unsigned char* plocal_symbols);
2570 // Scan the relocations to look for symbol adjustments.
2572 scan_relocs(Symbol_table* symtab,
2574 Sized_relobj_file<size, big_endian>* object,
2575 unsigned int data_shndx,
2576 unsigned int sh_type,
2577 const unsigned char* prelocs,
2579 Output_section* output_section,
2580 bool needs_special_offset_handling,
2581 size_t local_symbol_count,
2582 const unsigned char* plocal_symbols);
2584 // Finalize the sections.
2586 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2588 // Return the value to use for a dynamic which requires special
2591 do_dynsym_value(const Symbol*) const;
2593 // Relocate a section.
2595 relocate_section(const Relocate_info<size, big_endian>*,
2596 unsigned int sh_type,
2597 const unsigned char* prelocs,
2599 Output_section* output_section,
2600 bool needs_special_offset_handling,
2601 unsigned char* view,
2602 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2603 section_size_type view_size,
2604 const Reloc_symbol_changes*);
2606 // Scan the relocs during a relocatable link.
2608 scan_relocatable_relocs(Symbol_table* symtab,
2610 Sized_relobj_file<size, big_endian>* object,
2611 unsigned int data_shndx,
2612 unsigned int sh_type,
2613 const unsigned char* prelocs,
2615 Output_section* output_section,
2616 bool needs_special_offset_handling,
2617 size_t local_symbol_count,
2618 const unsigned char* plocal_symbols,
2619 Relocatable_relocs*);
2621 // Relocate a section during a relocatable link.
2624 const Relocate_info<size, big_endian>*,
2625 unsigned int sh_type,
2626 const unsigned char* prelocs,
2628 Output_section* output_section,
2629 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
2630 const Relocatable_relocs*,
2631 unsigned char* view,
2632 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2633 section_size_type view_size,
2634 unsigned char* reloc_view,
2635 section_size_type reloc_view_size);
2637 // Return the symbol index to use for a target specific relocation.
2638 // The only target specific relocation is R_AARCH64_TLSDESC for a
2639 // local symbol, which is an absolute reloc.
2641 do_reloc_symbol_index(void*, unsigned int r_type) const
2643 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
2647 // Return the addend to use for a target specific relocation.
2649 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
2651 // Return the PLT section.
2653 do_plt_address_for_global(const Symbol* gsym) const
2654 { return this->plt_section()->address_for_global(gsym); }
2657 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
2658 { return this->plt_section()->address_for_local(relobj, symndx); }
2660 // This function should be defined in targets that can use relocation
2661 // types to determine (implemented in local_reloc_may_be_function_pointer
2662 // and global_reloc_may_be_function_pointer)
2663 // if a function's pointer is taken. ICF uses this in safe mode to only
2664 // fold those functions whose pointer is defintely not taken.
2666 do_can_check_for_function_pointers() const
2669 // Return the number of entries in the PLT.
2671 plt_entry_count() const;
2673 //Return the offset of the first non-reserved PLT entry.
2675 first_plt_entry_offset() const;
2677 // Return the size of each PLT entry.
2679 plt_entry_size() const;
2681 // Create a stub table.
2683 new_stub_table(The_aarch64_input_section*);
2685 // Create an aarch64 input section.
2686 The_aarch64_input_section*
2687 new_aarch64_input_section(Relobj*, unsigned int);
2689 // Find an aarch64 input section instance for a given OBJ and SHNDX.
2690 The_aarch64_input_section*
2691 find_aarch64_input_section(Relobj*, unsigned int) const;
2693 // Return the thread control block size.
2695 tcb_size() const { return This::TCB_SIZE; }
2697 // Scan a section for stub generation.
2699 scan_section_for_stubs(const Relocate_info<size, big_endian>*, unsigned int,
2700 const unsigned char*, size_t, Output_section*,
2701 bool, const unsigned char*,
2705 // Scan a relocation section for stub.
2706 template<int sh_type>
2708 scan_reloc_section_for_stubs(
2709 const The_relocate_info* relinfo,
2710 const unsigned char* prelocs,
2712 Output_section* output_section,
2713 bool needs_special_offset_handling,
2714 const unsigned char* view,
2715 Address view_address,
2718 // Relocate a single stub.
2720 relocate_stub(The_reloc_stub*, const Relocate_info<size, big_endian>*,
2721 Output_section*, unsigned char*, Address,
2724 // Get the default AArch64 target.
2728 gold_assert(parameters->target().machine_code() == elfcpp::EM_AARCH64
2729 && parameters->target().get_size() == size
2730 && parameters->target().is_big_endian() == big_endian);
2731 return static_cast<This*>(parameters->sized_target<size, big_endian>());
2735 // Scan erratum for a part of a section.
2737 scan_erratum_843419_span(
2738 AArch64_relobj<size, big_endian>*,
2740 const section_size_type,
2741 const section_size_type,
2747 do_select_as_default_target()
2749 gold_assert(aarch64_reloc_property_table == NULL);
2750 aarch64_reloc_property_table = new AArch64_reloc_property_table();
2753 // Add a new reloc argument, returning the index in the vector.
2755 add_tlsdesc_info(Sized_relobj_file<size, big_endian>* object,
2758 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
2759 return this->tlsdesc_reloc_info_.size() - 1;
2762 virtual Output_data_plt_aarch64<size, big_endian>*
2763 do_make_data_plt(Layout* layout,
2764 Output_data_got_aarch64<size, big_endian>* got,
2765 Output_data_space* got_plt,
2766 Output_data_space* got_irelative)
2768 return new Output_data_plt_aarch64_standard<size, big_endian>(
2769 layout, got, got_plt, got_irelative);
2773 // do_make_elf_object to override the same function in the base class.
2775 do_make_elf_object(const std::string&, Input_file*, off_t,
2776 const elfcpp::Ehdr<size, big_endian>&);
2778 Output_data_plt_aarch64<size, big_endian>*
2779 make_data_plt(Layout* layout,
2780 Output_data_got_aarch64<size, big_endian>* got,
2781 Output_data_space* got_plt,
2782 Output_data_space* got_irelative)
2784 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
2787 // We only need to generate stubs, and hence perform relaxation if we are
2788 // not doing relocatable linking.
2790 do_may_relax() const
2791 { return !parameters->options().relocatable(); }
2793 // Relaxation hook. This is where we do stub generation.
2795 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
2798 group_sections(Layout* layout,
2799 section_size_type group_size,
2800 bool stubs_always_after_branch,
2804 scan_reloc_for_stub(const The_relocate_info*, unsigned int,
2805 const Sized_symbol<size>*, unsigned int,
2806 const Symbol_value<size>*,
2807 typename elfcpp::Elf_types<size>::Elf_Swxword,
2810 // Make an output section.
2812 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2813 elfcpp::Elf_Xword flags)
2814 { return new The_aarch64_output_section(name, type, flags); }
2817 // The class which scans relocations.
2822 : issued_non_pic_error_(false)
2826 local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
2827 Sized_relobj_file<size, big_endian>* object,
2828 unsigned int data_shndx,
2829 Output_section* output_section,
2830 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
2831 const elfcpp::Sym<size, big_endian>& lsym,
2835 global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
2836 Sized_relobj_file<size, big_endian>* object,
2837 unsigned int data_shndx,
2838 Output_section* output_section,
2839 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
2843 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
2844 Target_aarch64<size, big_endian>* ,
2845 Sized_relobj_file<size, big_endian>* ,
2848 const elfcpp::Rela<size, big_endian>& ,
2849 unsigned int r_type,
2850 const elfcpp::Sym<size, big_endian>&);
2853 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
2854 Target_aarch64<size, big_endian>* ,
2855 Sized_relobj_file<size, big_endian>* ,
2858 const elfcpp::Rela<size, big_endian>& ,
2859 unsigned int r_type,
2864 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
2865 unsigned int r_type);
2868 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
2869 unsigned int r_type, Symbol*);
2872 possible_function_pointer_reloc(unsigned int r_type);
2875 check_non_pic(Relobj*, unsigned int r_type);
2878 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
2879 unsigned int r_type);
2881 // Whether we have issued an error about a non-PIC compilation.
2882 bool issued_non_pic_error_;
2885 // The class which implements relocation.
2890 : skip_call_tls_get_addr_(false)
2896 // Do a relocation. Return false if the caller should not issue
2897 // any warnings about this relocation.
2899 relocate(const Relocate_info<size, big_endian>*, Target_aarch64*,
2901 size_t relnum, const elfcpp::Rela<size, big_endian>&,
2902 unsigned int r_type, const Sized_symbol<size>*,
2903 const Symbol_value<size>*,
2904 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
2908 inline typename AArch64_relocate_functions<size, big_endian>::Status
2909 relocate_tls(const Relocate_info<size, big_endian>*,
2910 Target_aarch64<size, big_endian>*,
2912 const elfcpp::Rela<size, big_endian>&,
2913 unsigned int r_type, const Sized_symbol<size>*,
2914 const Symbol_value<size>*,
2916 typename elfcpp::Elf_types<size>::Elf_Addr);
2918 inline typename AArch64_relocate_functions<size, big_endian>::Status
2920 const Relocate_info<size, big_endian>*,
2921 Target_aarch64<size, big_endian>*,
2922 const elfcpp::Rela<size, big_endian>&,
2925 const Symbol_value<size>*);
2927 inline typename AArch64_relocate_functions<size, big_endian>::Status
2929 const Relocate_info<size, big_endian>*,
2930 Target_aarch64<size, big_endian>*,
2931 const elfcpp::Rela<size, big_endian>&,
2934 const Symbol_value<size>*);
2936 inline typename AArch64_relocate_functions<size, big_endian>::Status
2938 const Relocate_info<size, big_endian>*,
2939 Target_aarch64<size, big_endian>*,
2940 const elfcpp::Rela<size, big_endian>&,
2943 const Symbol_value<size>*);
2945 inline typename AArch64_relocate_functions<size, big_endian>::Status
2947 const Relocate_info<size, big_endian>*,
2948 Target_aarch64<size, big_endian>*,
2949 const elfcpp::Rela<size, big_endian>&,
2952 const Symbol_value<size>*);
2954 inline typename AArch64_relocate_functions<size, big_endian>::Status
2956 const Relocate_info<size, big_endian>*,
2957 Target_aarch64<size, big_endian>*,
2958 const elfcpp::Rela<size, big_endian>&,
2961 const Symbol_value<size>*,
2962 typename elfcpp::Elf_types<size>::Elf_Addr,
2963 typename elfcpp::Elf_types<size>::Elf_Addr);
2965 bool skip_call_tls_get_addr_;
2967 }; // End of class Relocate
2969 // A class which returns the size required for a relocation type,
2970 // used while scanning relocs during a relocatable link.
2971 class Relocatable_size_for_reloc
2975 get_size_for_reloc(unsigned int, Relobj*);
2978 // Adjust TLS relocation type based on the options and whether this
2979 // is a local symbol.
2980 static tls::Tls_optimization
2981 optimize_tls_reloc(bool is_final, int r_type);
2983 // Get the GOT section, creating it if necessary.
2984 Output_data_got_aarch64<size, big_endian>*
2985 got_section(Symbol_table*, Layout*);
2987 // Get the GOT PLT section.
2989 got_plt_section() const
2991 gold_assert(this->got_plt_ != NULL);
2992 return this->got_plt_;
2995 // Get the GOT section for TLSDESC entries.
2996 Output_data_got<size, big_endian>*
2997 got_tlsdesc_section() const
2999 gold_assert(this->got_tlsdesc_ != NULL);
3000 return this->got_tlsdesc_;
3003 // Create the PLT section.
3005 make_plt_section(Symbol_table* symtab, Layout* layout);
3007 // Create a PLT entry for a global symbol.
3009 make_plt_entry(Symbol_table*, Layout*, Symbol*);
3011 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
3013 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
3014 Sized_relobj_file<size, big_endian>* relobj,
3015 unsigned int local_sym_index);
3017 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
3019 define_tls_base_symbol(Symbol_table*, Layout*);
3021 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
3023 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
3025 // Create a GOT entry for the TLS module index.
3027 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
3028 Sized_relobj_file<size, big_endian>* object);
3030 // Get the PLT section.
3031 Output_data_plt_aarch64<size, big_endian>*
3034 gold_assert(this->plt_ != NULL);
3038 // Return whether this is a 3-insn erratum sequence.
3039 bool is_erratum_843419_sequence(
3040 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
3041 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
3042 typename elfcpp::Swap<32,big_endian>::Valtype insn3);
3044 // Get the dynamic reloc section, creating it if necessary.
3046 rela_dyn_section(Layout*);
3048 // Get the section to use for TLSDESC relocations.
3050 rela_tlsdesc_section(Layout*) const;
3052 // Get the section to use for IRELATIVE relocations.
3054 rela_irelative_section(Layout*);
3056 // Add a potential copy relocation.
3058 copy_reloc(Symbol_table* symtab, Layout* layout,
3059 Sized_relobj_file<size, big_endian>* object,
3060 unsigned int shndx, Output_section* output_section,
3061 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
3063 this->copy_relocs_.copy_reloc(symtab, layout,
3064 symtab->get_sized_symbol<size>(sym),
3065 object, shndx, output_section,
3066 reloc, this->rela_dyn_section(layout));
3069 // Information about this specific target which we pass to the
3070 // general Target structure.
3071 static const Target::Target_info aarch64_info;
3073 // The types of GOT entries needed for this platform.
3074 // These values are exposed to the ABI in an incremental link.
3075 // Do not renumber existing values without changing the version
3076 // number of the .gnu_incremental_inputs section.
3079 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
3080 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
3081 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
3082 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
3085 // This type is used as the argument to the target specific
3086 // relocation routines. The only target specific reloc is
3087 // R_AARCh64_TLSDESC against a local symbol.
3090 Tlsdesc_info(Sized_relobj_file<size, big_endian>* a_object,
3091 unsigned int a_r_sym)
3092 : object(a_object), r_sym(a_r_sym)
3095 // The object in which the local symbol is defined.
3096 Sized_relobj_file<size, big_endian>* object;
3097 // The local symbol index in the object.
3102 Output_data_got_aarch64<size, big_endian>* got_;
3104 Output_data_plt_aarch64<size, big_endian>* plt_;
3105 // The GOT PLT section.
3106 Output_data_space* got_plt_;
3107 // The GOT section for IRELATIVE relocations.
3108 Output_data_space* got_irelative_;
3109 // The GOT section for TLSDESC relocations.
3110 Output_data_got<size, big_endian>* got_tlsdesc_;
3111 // The _GLOBAL_OFFSET_TABLE_ symbol.
3112 Symbol* global_offset_table_;
3113 // The dynamic reloc section.
3114 Reloc_section* rela_dyn_;
3115 // The section to use for IRELATIVE relocs.
3116 Reloc_section* rela_irelative_;
3117 // Relocs saved to avoid a COPY reloc.
3118 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
3119 // Offset of the GOT entry for the TLS module index.
3120 unsigned int got_mod_index_offset_;
3121 // We handle R_AARCH64_TLSDESC against a local symbol as a target
3122 // specific relocation. Here we store the object and local symbol
3123 // index for the relocation.
3124 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
3125 // True if the _TLS_MODULE_BASE_ symbol has been defined.
3126 bool tls_base_symbol_defined_;
3127 // List of stub_tables
3128 Stub_table_list stub_tables_;
3129 // Actual stub group size
3130 section_size_type stub_group_size_;
3131 AArch64_input_section_map aarch64_input_section_map_;
3132 }; // End of Target_aarch64
3136 const Target::Target_info Target_aarch64<64, false>::aarch64_info =
3139 false, // is_big_endian
3140 elfcpp::EM_AARCH64, // machine_code
3141 false, // has_make_symbol
3142 false, // has_resolve
3143 false, // has_code_fill
3144 true, // is_default_stack_executable
3145 true, // can_icf_inline_merge_sections
3147 "/lib/ld.so.1", // program interpreter
3148 0x400000, // default_text_segment_address
3149 0x1000, // abi_pagesize (overridable by -z max-page-size)
3150 0x1000, // common_pagesize (overridable by -z common-page-size)
3151 false, // isolate_execinstr
3153 elfcpp::SHN_UNDEF, // small_common_shndx
3154 elfcpp::SHN_UNDEF, // large_common_shndx
3155 0, // small_common_section_flags
3156 0, // large_common_section_flags
3157 NULL, // attributes_section
3158 NULL, // attributes_vendor
3159 "_start" // entry_symbol_name
3163 const Target::Target_info Target_aarch64<32, false>::aarch64_info =
3166 false, // is_big_endian
3167 elfcpp::EM_AARCH64, // machine_code
3168 false, // has_make_symbol
3169 false, // has_resolve
3170 false, // has_code_fill
3171 true, // is_default_stack_executable
3172 false, // can_icf_inline_merge_sections
3174 "/lib/ld.so.1", // program interpreter
3175 0x400000, // default_text_segment_address
3176 0x1000, // abi_pagesize (overridable by -z max-page-size)
3177 0x1000, // common_pagesize (overridable by -z common-page-size)
3178 false, // isolate_execinstr
3180 elfcpp::SHN_UNDEF, // small_common_shndx
3181 elfcpp::SHN_UNDEF, // large_common_shndx
3182 0, // small_common_section_flags
3183 0, // large_common_section_flags
3184 NULL, // attributes_section
3185 NULL, // attributes_vendor
3186 "_start" // entry_symbol_name
3190 const Target::Target_info Target_aarch64<64, true>::aarch64_info =
3193 true, // is_big_endian
3194 elfcpp::EM_AARCH64, // machine_code
3195 false, // has_make_symbol
3196 false, // has_resolve
3197 false, // has_code_fill
3198 true, // is_default_stack_executable
3199 true, // can_icf_inline_merge_sections
3201 "/lib/ld.so.1", // program interpreter
3202 0x400000, // default_text_segment_address
3203 0x1000, // abi_pagesize (overridable by -z max-page-size)
3204 0x1000, // common_pagesize (overridable by -z common-page-size)
3205 false, // isolate_execinstr
3207 elfcpp::SHN_UNDEF, // small_common_shndx
3208 elfcpp::SHN_UNDEF, // large_common_shndx
3209 0, // small_common_section_flags
3210 0, // large_common_section_flags
3211 NULL, // attributes_section
3212 NULL, // attributes_vendor
3213 "_start" // entry_symbol_name
3217 const Target::Target_info Target_aarch64<32, true>::aarch64_info =
3220 true, // is_big_endian
3221 elfcpp::EM_AARCH64, // machine_code
3222 false, // has_make_symbol
3223 false, // has_resolve
3224 false, // has_code_fill
3225 true, // is_default_stack_executable
3226 false, // can_icf_inline_merge_sections
3228 "/lib/ld.so.1", // program interpreter
3229 0x400000, // default_text_segment_address
3230 0x1000, // abi_pagesize (overridable by -z max-page-size)
3231 0x1000, // common_pagesize (overridable by -z common-page-size)
3232 false, // isolate_execinstr
3234 elfcpp::SHN_UNDEF, // small_common_shndx
3235 elfcpp::SHN_UNDEF, // large_common_shndx
3236 0, // small_common_section_flags
3237 0, // large_common_section_flags
3238 NULL, // attributes_section
3239 NULL, // attributes_vendor
3240 "_start" // entry_symbol_name
3243 // Get the GOT section, creating it if necessary.
3245 template<int size, bool big_endian>
3246 Output_data_got_aarch64<size, big_endian>*
3247 Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
3250 if (this->got_ == NULL)
3252 gold_assert(symtab != NULL && layout != NULL);
3254 // When using -z now, we can treat .got.plt as a relro section.
3255 // Without -z now, it is modified after program startup by lazy
3257 bool is_got_plt_relro = parameters->options().now();
3258 Output_section_order got_order = (is_got_plt_relro
3260 : ORDER_RELRO_LAST);
3261 Output_section_order got_plt_order = (is_got_plt_relro
3263 : ORDER_NON_RELRO_FIRST);
3265 // Layout of .got and .got.plt sections.
3266 // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
3268 // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
3269 // .gotplt[1] reserved for ld.so (resolver)
3270 // .gotplt[2] reserved
3272 // Generate .got section.
3273 this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
3275 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3276 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
3277 this->got_, got_order, true);
3278 // The first word of GOT is reserved for the address of .dynamic.
3279 // We put 0 here now. The value will be replaced later in
3280 // Output_data_got_aarch64::do_write.
3281 this->got_->add_constant(0);
3283 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3284 // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
3285 // even if there is a .got.plt section.
3286 this->global_offset_table_ =
3287 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3288 Symbol_table::PREDEFINED,
3290 0, 0, elfcpp::STT_OBJECT,
3292 elfcpp::STV_HIDDEN, 0,
3295 // Generate .got.plt section.
3296 this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
3297 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3299 | elfcpp::SHF_WRITE),
3300 this->got_plt_, got_plt_order,
3303 // The first three entries are reserved.
3304 this->got_plt_->set_current_data_size(
3305 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
3307 // If there are any IRELATIVE relocations, they get GOT entries
3308 // in .got.plt after the jump slot entries.
3309 this->got_irelative_ = new Output_data_space(size / 8,
3310 "** GOT IRELATIVE PLT");
3311 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3313 | elfcpp::SHF_WRITE),
3314 this->got_irelative_,
3318 // If there are any TLSDESC relocations, they get GOT entries in
3319 // .got.plt after the jump slot and IRELATIVE entries.
3320 this->got_tlsdesc_ = new Output_data_got<size, big_endian>();
3321 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3323 | elfcpp::SHF_WRITE),
3328 if (!is_got_plt_relro)
3330 // Those bytes can go into the relro segment.
3331 layout->increase_relro(
3332 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
3339 // Get the dynamic reloc section, creating it if necessary.
3341 template<int size, bool big_endian>
3342 typename Target_aarch64<size, big_endian>::Reloc_section*
3343 Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
3345 if (this->rela_dyn_ == NULL)
3347 gold_assert(layout != NULL);
3348 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3349 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3350 elfcpp::SHF_ALLOC, this->rela_dyn_,
3351 ORDER_DYNAMIC_RELOCS, false);
3353 return this->rela_dyn_;
3356 // Get the section to use for IRELATIVE relocs, creating it if
3357 // necessary. These go in .rela.dyn, but only after all other dynamic
3358 // relocations. They need to follow the other dynamic relocations so
3359 // that they can refer to global variables initialized by those
3362 template<int size, bool big_endian>
3363 typename Target_aarch64<size, big_endian>::Reloc_section*
3364 Target_aarch64<size, big_endian>::rela_irelative_section(Layout* layout)
3366 if (this->rela_irelative_ == NULL)
3368 // Make sure we have already created the dynamic reloc section.
3369 this->rela_dyn_section(layout);
3370 this->rela_irelative_ = new Reloc_section(false);
3371 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3372 elfcpp::SHF_ALLOC, this->rela_irelative_,
3373 ORDER_DYNAMIC_RELOCS, false);
3374 gold_assert(this->rela_dyn_->output_section()
3375 == this->rela_irelative_->output_section());
3377 return this->rela_irelative_;
3381 // do_make_elf_object to override the same function in the base class. We need
3382 // to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
3383 // store backend specific information. Hence we need to have our own ELF object
3386 template<int size, bool big_endian>
3388 Target_aarch64<size, big_endian>::do_make_elf_object(
3389 const std::string& name,
3390 Input_file* input_file,
3391 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
3393 int et = ehdr.get_e_type();
3394 // ET_EXEC files are valid input for --just-symbols/-R,
3395 // and we treat them as relocatable objects.
3396 if (et == elfcpp::ET_EXEC && input_file->just_symbols())
3397 return Sized_target<size, big_endian>::do_make_elf_object(
3398 name, input_file, offset, ehdr);
3399 else if (et == elfcpp::ET_REL)
3401 AArch64_relobj<size, big_endian>* obj =
3402 new AArch64_relobj<size, big_endian>(name, input_file, offset, ehdr);
3406 else if (et == elfcpp::ET_DYN)
3408 // Keep base implementation.
3409 Sized_dynobj<size, big_endian>* obj =
3410 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
3416 gold_error(_("%s: unsupported ELF file type %d"),
3423 // Scan a relocation for stub generation.
3425 template<int size, bool big_endian>
3427 Target_aarch64<size, big_endian>::scan_reloc_for_stub(
3428 const Relocate_info<size, big_endian>* relinfo,
3429 unsigned int r_type,
3430 const Sized_symbol<size>* gsym,
3432 const Symbol_value<size>* psymval,
3433 typename elfcpp::Elf_types<size>::Elf_Swxword addend,
3436 const AArch64_relobj<size, big_endian>* aarch64_relobj =
3437 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3439 Symbol_value<size> symval;
3442 const AArch64_reloc_property* arp = aarch64_reloc_property_table->
3443 get_reloc_property(r_type);
3444 if (gsym->use_plt_offset(arp->reference_flags()))
3446 // This uses a PLT, change the symbol value.
3447 symval.set_output_value(this->plt_section()->address()
3448 + gsym->plt_offset());
3451 else if (gsym->is_undefined())
3452 // There is no need to generate a stub symbol is undefined.
3456 // Get the symbol value.
3457 typename Symbol_value<size>::Value value = psymval->value(aarch64_relobj, 0);
3459 // Owing to pipelining, the PC relative branches below actually skip
3460 // two instructions when the branch offset is 0.
3461 Address destination = static_cast<Address>(-1);
3464 case elfcpp::R_AARCH64_CALL26:
3465 case elfcpp::R_AARCH64_JUMP26:
3466 destination = value + addend;
3472 int stub_type = The_reloc_stub::
3473 stub_type_for_reloc(r_type, address, destination);
3474 if (stub_type == ST_NONE)
3477 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
3478 gold_assert(stub_table != NULL);
3480 The_reloc_stub_key key(stub_type, gsym, aarch64_relobj, r_sym, addend);
3481 The_reloc_stub* stub = stub_table->find_reloc_stub(key);
3484 stub = new The_reloc_stub(stub_type);
3485 stub_table->add_reloc_stub(stub, key);
3487 stub->set_destination_address(destination);
3488 } // End of Target_aarch64::scan_reloc_for_stub
3491 // This function scans a relocation section for stub generation.
3492 // The template parameter Relocate must be a class type which provides
3493 // a single function, relocate(), which implements the machine
3494 // specific part of a relocation.
3496 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
3497 // SHT_REL or SHT_RELA.
3499 // PRELOCS points to the relocation data. RELOC_COUNT is the number
3500 // of relocs. OUTPUT_SECTION is the output section.
3501 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
3502 // mapped to output offsets.
3504 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
3505 // VIEW_SIZE is the size. These refer to the input section, unless
3506 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
3507 // the output section.
3509 template<int size, bool big_endian>
3510 template<int sh_type>
3512 Target_aarch64<size, big_endian>::scan_reloc_section_for_stubs(
3513 const Relocate_info<size, big_endian>* relinfo,
3514 const unsigned char* prelocs,
3516 Output_section* /*output_section*/,
3517 bool /*needs_special_offset_handling*/,
3518 const unsigned char* /*view*/,
3519 Address view_address,
3522 typedef typename Reloc_types<sh_type,size,big_endian>::Reloc Reltype;
3524 const int reloc_size =
3525 Reloc_types<sh_type,size,big_endian>::reloc_size;
3526 AArch64_relobj<size, big_endian>* object =
3527 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3528 unsigned int local_count = object->local_symbol_count();
3530 gold::Default_comdat_behavior default_comdat_behavior;
3531 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
3533 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
3535 Reltype reloc(prelocs);
3536 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
3537 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3538 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
3539 if (r_type != elfcpp::R_AARCH64_CALL26
3540 && r_type != elfcpp::R_AARCH64_JUMP26)
3543 section_offset_type offset =
3544 convert_to_section_size_type(reloc.get_r_offset());
3547 typename elfcpp::Elf_types<size>::Elf_Swxword addend =
3548 reloc.get_r_addend();
3550 const Sized_symbol<size>* sym;
3551 Symbol_value<size> symval;
3552 const Symbol_value<size> *psymval;
3553 bool is_defined_in_discarded_section;
3555 if (r_sym < local_count)
3558 psymval = object->local_symbol(r_sym);
3560 // If the local symbol belongs to a section we are discarding,
3561 // and that section is a debug section, try to find the
3562 // corresponding kept section and map this symbol to its
3563 // counterpart in the kept section. The symbol must not
3564 // correspond to a section we are folding.
3566 shndx = psymval->input_shndx(&is_ordinary);
3567 is_defined_in_discarded_section =
3569 && shndx != elfcpp::SHN_UNDEF
3570 && !object->is_section_included(shndx)
3571 && !relinfo->symtab->is_section_folded(object, shndx));
3573 // We need to compute the would-be final value of this local
3575 if (!is_defined_in_discarded_section)
3577 typedef Sized_relobj_file<size, big_endian> ObjType;
3578 typename ObjType::Compute_final_local_value_status status =
3579 object->compute_final_local_value(r_sym, psymval, &symval,
3581 if (status == ObjType::CFLV_OK)
3583 // Currently we cannot handle a branch to a target in
3584 // a merged section. If this is the case, issue an error
3585 // and also free the merge symbol value.
3586 if (!symval.has_output_value())
3588 const std::string& section_name =
3589 object->section_name(shndx);
3590 object->error(_("cannot handle branch to local %u "
3591 "in a merged section %s"),
3592 r_sym, section_name.c_str());
3598 // We cannot determine the final value.
3606 gsym = object->global_symbol(r_sym);
3607 gold_assert(gsym != NULL);
3608 if (gsym->is_forwarder())
3609 gsym = relinfo->symtab->resolve_forwards(gsym);
3611 sym = static_cast<const Sized_symbol<size>*>(gsym);
3612 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
3613 symval.set_output_symtab_index(sym->symtab_index());
3615 symval.set_no_output_symtab_entry();
3617 // We need to compute the would-be final value of this global
3619 const Symbol_table* symtab = relinfo->symtab;
3620 const Sized_symbol<size>* sized_symbol =
3621 symtab->get_sized_symbol<size>(gsym);
3622 Symbol_table::Compute_final_value_status status;
3623 typename elfcpp::Elf_types<size>::Elf_Addr value =
3624 symtab->compute_final_value<size>(sized_symbol, &status);
3626 // Skip this if the symbol has not output section.
3627 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
3629 symval.set_output_value(value);
3631 if (gsym->type() == elfcpp::STT_TLS)
3632 symval.set_is_tls_symbol();
3633 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3634 symval.set_is_ifunc_symbol();
3637 is_defined_in_discarded_section =
3638 (gsym->is_defined_in_discarded_section()
3639 && gsym->is_undefined());
3643 Symbol_value<size> symval2;
3644 if (is_defined_in_discarded_section)
3646 if (comdat_behavior == CB_UNDETERMINED)
3648 std::string name = object->section_name(relinfo->data_shndx);
3649 comdat_behavior = default_comdat_behavior.get(name.c_str());
3651 if (comdat_behavior == CB_PRETEND)
3654 typename elfcpp::Elf_types<size>::Elf_Addr value =
3655 object->map_to_kept_section(shndx, &found);
3657 symval2.set_output_value(value + psymval->input_value());
3659 symval2.set_output_value(0);
3663 if (comdat_behavior == CB_WARNING)
3664 gold_warning_at_location(relinfo, i, offset,
3665 _("relocation refers to discarded "
3667 symval2.set_output_value(0);
3669 symval2.set_no_output_symtab_entry();
3673 // If symbol is a section symbol, we don't know the actual type of
3674 // destination. Give up.
3675 if (psymval->is_section_symbol())
3678 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
3679 addend, view_address + offset);
3680 } // End of iterating relocs in a section
3681 } // End of Target_aarch64::scan_reloc_section_for_stubs
3684 // Scan an input section for stub generation.
3686 template<int size, bool big_endian>
3688 Target_aarch64<size, big_endian>::scan_section_for_stubs(
3689 const Relocate_info<size, big_endian>* relinfo,
3690 unsigned int sh_type,
3691 const unsigned char* prelocs,
3693 Output_section* output_section,
3694 bool needs_special_offset_handling,
3695 const unsigned char* view,
3696 Address view_address,
3697 section_size_type view_size)
3699 gold_assert(sh_type == elfcpp::SHT_RELA);
3700 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
3705 needs_special_offset_handling,
3712 // Relocate a single stub.
3714 template<int size, bool big_endian>
3715 void Target_aarch64<size, big_endian>::
3716 relocate_stub(The_reloc_stub* stub,
3717 const The_relocate_info*,
3719 unsigned char* view,
3723 typedef AArch64_relocate_functions<size, big_endian> The_reloc_functions;
3724 typedef typename The_reloc_functions::Status The_reloc_functions_status;
3725 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
3727 Insntype* ip = reinterpret_cast<Insntype*>(view);
3728 int insn_number = stub->insn_num();
3729 const uint32_t* insns = stub->insns();
3730 // Check the insns are really those stub insns.
3731 for (int i = 0; i < insn_number; ++i)
3733 Insntype insn = elfcpp::Swap<32,big_endian>::readval(ip + i);
3734 gold_assert(((uint32_t)insn == insns[i]));
3737 Address dest = stub->destination_address();
3739 switch(stub->type())
3741 case ST_ADRP_BRANCH:
3743 // 1st reloc is ADR_PREL_PG_HI21
3744 The_reloc_functions_status status =
3745 The_reloc_functions::adrp(view, dest, address);
3746 // An error should never arise in the above step. If so, please
3747 // check 'aarch64_valid_for_adrp_p'.
3748 gold_assert(status == The_reloc_functions::STATUS_OKAY);
3750 // 2nd reloc is ADD_ABS_LO12_NC
3751 const AArch64_reloc_property* arp =
3752 aarch64_reloc_property_table->get_reloc_property(
3753 elfcpp::R_AARCH64_ADD_ABS_LO12_NC);
3754 gold_assert(arp != NULL);
3755 status = The_reloc_functions::template
3756 rela_general<32>(view + 4, dest, 0, arp);
3757 // An error should never arise, it is an "_NC" relocation.
3758 gold_assert(status == The_reloc_functions::STATUS_OKAY);
3762 case ST_LONG_BRANCH_ABS:
3763 // 1st reloc is R_AARCH64_PREL64, at offset 8
3764 elfcpp::Swap<64,big_endian>::writeval(view + 8, dest);
3767 case ST_LONG_BRANCH_PCREL:
3769 // "PC" calculation is the 2nd insn in the stub.
3770 uint64_t offset = dest - (address + 4);
3771 // Offset is placed at offset 4 and 5.
3772 elfcpp::Swap<64,big_endian>::writeval(view + 16, offset);
3782 // A class to handle the PLT data.
3783 // This is an abstract base class that handles most of the linker details
3784 // but does not know the actual contents of PLT entries. The derived
3785 // classes below fill in those details.
3787 template<int size, bool big_endian>
3788 class Output_data_plt_aarch64 : public Output_section_data
3791 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
3793 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3795 Output_data_plt_aarch64(Layout* layout,
3797 Output_data_got_aarch64<size, big_endian>* got,
3798 Output_data_space* got_plt,
3799 Output_data_space* got_irelative)
3800 : Output_section_data(addralign), tlsdesc_rel_(NULL), irelative_rel_(NULL),
3801 got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
3802 count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
3803 { this->init(layout); }
3805 // Initialize the PLT section.
3807 init(Layout* layout);
3809 // Add an entry to the PLT.
3811 add_entry(Symbol_table*, Layout*, Symbol* gsym);
3813 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
3815 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
3816 Sized_relobj_file<size, big_endian>* relobj,
3817 unsigned int local_sym_index);
3819 // Add the relocation for a PLT entry.
3821 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
3822 unsigned int got_offset);
3824 // Add the reserved TLSDESC_PLT entry to the PLT.
3826 reserve_tlsdesc_entry(unsigned int got_offset)
3827 { this->tlsdesc_got_offset_ = got_offset; }
3829 // Return true if a TLSDESC_PLT entry has been reserved.
3831 has_tlsdesc_entry() const
3832 { return this->tlsdesc_got_offset_ != -1U; }
3834 // Return the GOT offset for the reserved TLSDESC_PLT entry.
3836 get_tlsdesc_got_offset() const
3837 { return this->tlsdesc_got_offset_; }
3839 // Return the PLT offset of the reserved TLSDESC_PLT entry.
3841 get_tlsdesc_plt_offset() const
3843 return (this->first_plt_entry_offset() +
3844 (this->count_ + this->irelative_count_)
3845 * this->get_plt_entry_size());
3848 // Return the .rela.plt section data.
3851 { return this->rel_; }
3853 // Return where the TLSDESC relocations should go.
3855 rela_tlsdesc(Layout*);
3857 // Return where the IRELATIVE relocations should go in the PLT
3860 rela_irelative(Symbol_table*, Layout*);
3862 // Return whether we created a section for IRELATIVE relocations.
3864 has_irelative_section() const
3865 { return this->irelative_rel_ != NULL; }
3867 // Return the number of PLT entries.
3870 { return this->count_ + this->irelative_count_; }
3872 // Return the offset of the first non-reserved PLT entry.
3874 first_plt_entry_offset() const
3875 { return this->do_first_plt_entry_offset(); }
3877 // Return the size of a PLT entry.
3879 get_plt_entry_size() const
3880 { return this->do_get_plt_entry_size(); }
3882 // Return the reserved tlsdesc entry size.
3884 get_plt_tlsdesc_entry_size() const
3885 { return this->do_get_plt_tlsdesc_entry_size(); }
3887 // Return the PLT address to use for a global symbol.
3889 address_for_global(const Symbol*);
3891 // Return the PLT address to use for a local symbol.
3893 address_for_local(const Relobj*, unsigned int symndx);
3896 // Fill in the first PLT entry.
3898 fill_first_plt_entry(unsigned char* pov,
3899 Address got_address,
3900 Address plt_address)
3901 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
3903 // Fill in a normal PLT entry.
3905 fill_plt_entry(unsigned char* pov,
3906 Address got_address,
3907 Address plt_address,
3908 unsigned int got_offset,
3909 unsigned int plt_offset)
3911 this->do_fill_plt_entry(pov, got_address, plt_address,
3912 got_offset, plt_offset);
3915 // Fill in the reserved TLSDESC PLT entry.
3917 fill_tlsdesc_entry(unsigned char* pov,
3918 Address gotplt_address,
3919 Address plt_address,
3921 unsigned int tlsdesc_got_offset,
3922 unsigned int plt_offset)
3924 this->do_fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
3925 tlsdesc_got_offset, plt_offset);
3928 virtual unsigned int
3929 do_first_plt_entry_offset() const = 0;
3931 virtual unsigned int
3932 do_get_plt_entry_size() const = 0;
3934 virtual unsigned int
3935 do_get_plt_tlsdesc_entry_size() const = 0;
3938 do_fill_first_plt_entry(unsigned char* pov,
3940 Address plt_addr) = 0;
3943 do_fill_plt_entry(unsigned char* pov,
3944 Address got_address,
3945 Address plt_address,
3946 unsigned int got_offset,
3947 unsigned int plt_offset) = 0;
3950 do_fill_tlsdesc_entry(unsigned char* pov,
3951 Address gotplt_address,
3952 Address plt_address,
3954 unsigned int tlsdesc_got_offset,
3955 unsigned int plt_offset) = 0;
3958 do_adjust_output_section(Output_section* os);
3960 // Write to a map file.
3962 do_print_to_mapfile(Mapfile* mapfile) const
3963 { mapfile->print_output_data(this, _("** PLT")); }
3966 // Set the final size.
3968 set_final_data_size();
3970 // Write out the PLT data.
3972 do_write(Output_file*);
3974 // The reloc section.
3975 Reloc_section* rel_;
3977 // The TLSDESC relocs, if necessary. These must follow the regular
3979 Reloc_section* tlsdesc_rel_;
3981 // The IRELATIVE relocs, if necessary. These must follow the
3982 // regular PLT relocations.
3983 Reloc_section* irelative_rel_;
3985 // The .got section.
3986 Output_data_got_aarch64<size, big_endian>* got_;
3988 // The .got.plt section.
3989 Output_data_space* got_plt_;
3991 // The part of the .got.plt section used for IRELATIVE relocs.
3992 Output_data_space* got_irelative_;
3994 // The number of PLT entries.
3995 unsigned int count_;
3997 // Number of PLT entries with R_AARCH64_IRELATIVE relocs. These
3998 // follow the regular PLT entries.
3999 unsigned int irelative_count_;
4001 // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
4002 // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
4003 // indicates an offset is not allocated.
4004 unsigned int tlsdesc_got_offset_;
4007 // Initialize the PLT section.
4009 template<int size, bool big_endian>
4011 Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
4013 this->rel_ = new Reloc_section(false);
4014 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4015 elfcpp::SHF_ALLOC, this->rel_,
4016 ORDER_DYNAMIC_PLT_RELOCS, false);
4019 template<int size, bool big_endian>
4021 Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
4024 os->set_entsize(this->get_plt_entry_size());
4027 // Add an entry to the PLT.
4029 template<int size, bool big_endian>
4031 Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol_table* symtab,
4032 Layout* layout, Symbol* gsym)
4034 gold_assert(!gsym->has_plt_offset());
4036 unsigned int* pcount;
4037 unsigned int plt_reserved;
4038 Output_section_data_build* got;
4040 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4041 && gsym->can_use_relative_reloc(false))
4043 pcount = &this->irelative_count_;
4045 got = this->got_irelative_;
4049 pcount = &this->count_;
4050 plt_reserved = this->first_plt_entry_offset();
4051 got = this->got_plt_;
4054 gsym->set_plt_offset((*pcount) * this->get_plt_entry_size()
4059 section_offset_type got_offset = got->current_data_size();
4061 // Every PLT entry needs a GOT entry which points back to the PLT
4062 // entry (this will be changed by the dynamic linker, normally
4063 // lazily when the function is called).
4064 got->set_current_data_size(got_offset + size / 8);
4066 // Every PLT entry needs a reloc.
4067 this->add_relocation(symtab, layout, gsym, got_offset);
4069 // Note that we don't need to save the symbol. The contents of the
4070 // PLT are independent of which symbols are used. The symbols only
4071 // appear in the relocations.
4074 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
4077 template<int size, bool big_endian>
4079 Output_data_plt_aarch64<size, big_endian>::add_local_ifunc_entry(
4080 Symbol_table* symtab,
4082 Sized_relobj_file<size, big_endian>* relobj,
4083 unsigned int local_sym_index)
4085 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
4086 ++this->irelative_count_;
4088 section_offset_type got_offset = this->got_irelative_->current_data_size();
4090 // Every PLT entry needs a GOT entry which points back to the PLT
4092 this->got_irelative_->set_current_data_size(got_offset + size / 8);
4094 // Every PLT entry needs a reloc.
4095 Reloc_section* rela = this->rela_irelative(symtab, layout);
4096 rela->add_symbolless_local_addend(relobj, local_sym_index,
4097 elfcpp::R_AARCH64_IRELATIVE,
4098 this->got_irelative_, got_offset, 0);
4103 // Add the relocation for a PLT entry.
4105 template<int size, bool big_endian>
4107 Output_data_plt_aarch64<size, big_endian>::add_relocation(
4108 Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
4110 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4111 && gsym->can_use_relative_reloc(false))
4113 Reloc_section* rela = this->rela_irelative(symtab, layout);
4114 rela->add_symbolless_global_addend(gsym, elfcpp::R_AARCH64_IRELATIVE,
4115 this->got_irelative_, got_offset, 0);
4119 gsym->set_needs_dynsym_entry();
4120 this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT, this->got_plt_,
4125 // Return where the TLSDESC relocations should go, creating it if
4126 // necessary. These follow the JUMP_SLOT relocations.
4128 template<int size, bool big_endian>
4129 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
4130 Output_data_plt_aarch64<size, big_endian>::rela_tlsdesc(Layout* layout)
4132 if (this->tlsdesc_rel_ == NULL)
4134 this->tlsdesc_rel_ = new Reloc_section(false);
4135 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4136 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
4137 ORDER_DYNAMIC_PLT_RELOCS, false);
4138 gold_assert(this->tlsdesc_rel_->output_section()
4139 == this->rel_->output_section());
4141 return this->tlsdesc_rel_;
4144 // Return where the IRELATIVE relocations should go in the PLT. These
4145 // follow the JUMP_SLOT and the TLSDESC relocations.
4147 template<int size, bool big_endian>
4148 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
4149 Output_data_plt_aarch64<size, big_endian>::rela_irelative(Symbol_table* symtab,
4152 if (this->irelative_rel_ == NULL)
4154 // Make sure we have a place for the TLSDESC relocations, in
4155 // case we see any later on.
4156 this->rela_tlsdesc(layout);
4157 this->irelative_rel_ = new Reloc_section(false);
4158 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4159 elfcpp::SHF_ALLOC, this->irelative_rel_,
4160 ORDER_DYNAMIC_PLT_RELOCS, false);
4161 gold_assert(this->irelative_rel_->output_section()
4162 == this->rel_->output_section());
4164 if (parameters->doing_static_link())
4166 // A statically linked executable will only have a .rela.plt
4167 // section to hold R_AARCH64_IRELATIVE relocs for
4168 // STT_GNU_IFUNC symbols. The library will use these
4169 // symbols to locate the IRELATIVE relocs at program startup
4171 symtab->define_in_output_data("__rela_iplt_start", NULL,
4172 Symbol_table::PREDEFINED,
4173 this->irelative_rel_, 0, 0,
4174 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4175 elfcpp::STV_HIDDEN, 0, false, true);
4176 symtab->define_in_output_data("__rela_iplt_end", NULL,
4177 Symbol_table::PREDEFINED,
4178 this->irelative_rel_, 0, 0,
4179 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4180 elfcpp::STV_HIDDEN, 0, true, true);
4183 return this->irelative_rel_;
4186 // Return the PLT address to use for a global symbol.
4188 template<int size, bool big_endian>
4190 Output_data_plt_aarch64<size, big_endian>::address_for_global(
4193 uint64_t offset = 0;
4194 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4195 && gsym->can_use_relative_reloc(false))
4196 offset = (this->first_plt_entry_offset() +
4197 this->count_ * this->get_plt_entry_size());
4198 return this->address() + offset + gsym->plt_offset();
4201 // Return the PLT address to use for a local symbol. These are always
4202 // IRELATIVE relocs.
4204 template<int size, bool big_endian>
4206 Output_data_plt_aarch64<size, big_endian>::address_for_local(
4207 const Relobj* object,
4210 return (this->address()
4211 + this->first_plt_entry_offset()
4212 + this->count_ * this->get_plt_entry_size()
4213 + object->local_plt_offset(r_sym));
4216 // Set the final size.
4218 template<int size, bool big_endian>
4220 Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
4222 unsigned int count = this->count_ + this->irelative_count_;
4223 unsigned int extra_size = 0;
4224 if (this->has_tlsdesc_entry())
4225 extra_size += this->get_plt_tlsdesc_entry_size();
4226 this->set_data_size(this->first_plt_entry_offset()
4227 + count * this->get_plt_entry_size()
4231 template<int size, bool big_endian>
4232 class Output_data_plt_aarch64_standard :
4233 public Output_data_plt_aarch64<size, big_endian>
4236 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4237 Output_data_plt_aarch64_standard(
4239 Output_data_got_aarch64<size, big_endian>* got,
4240 Output_data_space* got_plt,
4241 Output_data_space* got_irelative)
4242 : Output_data_plt_aarch64<size, big_endian>(layout,
4249 // Return the offset of the first non-reserved PLT entry.
4250 virtual unsigned int
4251 do_first_plt_entry_offset() const
4252 { return this->first_plt_entry_size; }
4254 // Return the size of a PLT entry
4255 virtual unsigned int
4256 do_get_plt_entry_size() const
4257 { return this->plt_entry_size; }
4259 // Return the size of a tlsdesc entry
4260 virtual unsigned int
4261 do_get_plt_tlsdesc_entry_size() const
4262 { return this->plt_tlsdesc_entry_size; }
4265 do_fill_first_plt_entry(unsigned char* pov,
4266 Address got_address,
4267 Address plt_address);
4270 do_fill_plt_entry(unsigned char* pov,
4271 Address got_address,
4272 Address plt_address,
4273 unsigned int got_offset,
4274 unsigned int plt_offset);
4277 do_fill_tlsdesc_entry(unsigned char* pov,
4278 Address gotplt_address,
4279 Address plt_address,
4281 unsigned int tlsdesc_got_offset,
4282 unsigned int plt_offset);
4285 // The size of the first plt entry size.
4286 static const int first_plt_entry_size = 32;
4287 // The size of the plt entry size.
4288 static const int plt_entry_size = 16;
4289 // The size of the plt tlsdesc entry size.
4290 static const int plt_tlsdesc_entry_size = 32;
4291 // Template for the first PLT entry.
4292 static const uint32_t first_plt_entry[first_plt_entry_size / 4];
4293 // Template for subsequent PLT entries.
4294 static const uint32_t plt_entry[plt_entry_size / 4];
4295 // The reserved TLSDESC entry in the PLT for an executable.
4296 static const uint32_t tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4];
4299 // The first entry in the PLT for an executable.
4303 Output_data_plt_aarch64_standard<32, false>::
4304 first_plt_entry[first_plt_entry_size / 4] =
4306 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4307 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4308 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4309 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4310 0xd61f0220, /* br x17 */
4311 0xd503201f, /* nop */
4312 0xd503201f, /* nop */
4313 0xd503201f, /* nop */
4319 Output_data_plt_aarch64_standard<32, true>::
4320 first_plt_entry[first_plt_entry_size / 4] =
4322 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4323 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4324 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4325 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4326 0xd61f0220, /* br x17 */
4327 0xd503201f, /* nop */
4328 0xd503201f, /* nop */
4329 0xd503201f, /* nop */
4335 Output_data_plt_aarch64_standard<64, false>::
4336 first_plt_entry[first_plt_entry_size / 4] =
4338 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4339 0x90000010, /* adrp x16, PLT_GOT+16 */
4340 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4341 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4342 0xd61f0220, /* br x17 */
4343 0xd503201f, /* nop */
4344 0xd503201f, /* nop */
4345 0xd503201f, /* nop */
4351 Output_data_plt_aarch64_standard<64, true>::
4352 first_plt_entry[first_plt_entry_size / 4] =
4354 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4355 0x90000010, /* adrp x16, PLT_GOT+16 */
4356 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4357 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4358 0xd61f0220, /* br x17 */
4359 0xd503201f, /* nop */
4360 0xd503201f, /* nop */
4361 0xd503201f, /* nop */
4367 Output_data_plt_aarch64_standard<32, false>::
4368 plt_entry[plt_entry_size / 4] =
4370 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4371 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4372 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4373 0xd61f0220, /* br x17. */
4379 Output_data_plt_aarch64_standard<32, true>::
4380 plt_entry[plt_entry_size / 4] =
4382 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4383 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4384 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4385 0xd61f0220, /* br x17. */
4391 Output_data_plt_aarch64_standard<64, false>::
4392 plt_entry[plt_entry_size / 4] =
4394 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4395 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4396 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4397 0xd61f0220, /* br x17. */
4403 Output_data_plt_aarch64_standard<64, true>::
4404 plt_entry[plt_entry_size / 4] =
4406 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4407 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4408 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4409 0xd61f0220, /* br x17. */
4413 template<int size, bool big_endian>
4415 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
4417 Address got_address,
4418 Address plt_address)
4420 // PLT0 of the small PLT looks like this in ELF64 -
4421 // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
4422 // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
4423 // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
4425 // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
4426 // GOTPLT entry for this.
4428 // PLT0 will be slightly different in ELF32 due to different got entry
4430 memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
4431 Address gotplt_2nd_ent = got_address + (size / 8) * 2;
4433 // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
4434 // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
4435 // FIXME: This only works for 64bit
4436 AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
4437 gotplt_2nd_ent, plt_address + 4);
4439 // Fill in R_AARCH64_LDST8_LO12
4440 elfcpp::Swap<32, big_endian>::writeval(
4442 ((this->first_plt_entry[2] & 0xffc003ff)
4443 | ((gotplt_2nd_ent & 0xff8) << 7)));
4445 // Fill in R_AARCH64_ADD_ABS_LO12
4446 elfcpp::Swap<32, big_endian>::writeval(
4448 ((this->first_plt_entry[3] & 0xffc003ff)
4449 | ((gotplt_2nd_ent & 0xfff) << 10)));
4453 // Subsequent entries in the PLT for an executable.
4454 // FIXME: This only works for 64bit
4456 template<int size, bool big_endian>
4458 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
4460 Address got_address,
4461 Address plt_address,
4462 unsigned int got_offset,
4463 unsigned int plt_offset)
4465 memcpy(pov, this->plt_entry, this->plt_entry_size);
4467 Address gotplt_entry_address = got_address + got_offset;
4468 Address plt_entry_address = plt_address + plt_offset;
4470 // Fill in R_AARCH64_PCREL_ADR_HI21
4471 AArch64_relocate_functions<size, big_endian>::adrp(
4473 gotplt_entry_address,
4476 // Fill in R_AARCH64_LDST64_ABS_LO12
4477 elfcpp::Swap<32, big_endian>::writeval(
4479 ((this->plt_entry[1] & 0xffc003ff)
4480 | ((gotplt_entry_address & 0xff8) << 7)));
4482 // Fill in R_AARCH64_ADD_ABS_LO12
4483 elfcpp::Swap<32, big_endian>::writeval(
4485 ((this->plt_entry[2] & 0xffc003ff)
4486 | ((gotplt_entry_address & 0xfff) <<10)));
4493 Output_data_plt_aarch64_standard<32, false>::
4494 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4496 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4497 0x90000002, /* adrp x2, 0 */
4498 0x90000003, /* adrp x3, 0 */
4499 0xb9400042, /* ldr w2, [w2, #0] */
4500 0x11000063, /* add w3, w3, 0 */
4501 0xd61f0040, /* br x2 */
4502 0xd503201f, /* nop */
4503 0xd503201f, /* nop */
4508 Output_data_plt_aarch64_standard<32, true>::
4509 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4511 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4512 0x90000002, /* adrp x2, 0 */
4513 0x90000003, /* adrp x3, 0 */
4514 0xb9400042, /* ldr w2, [w2, #0] */
4515 0x11000063, /* add w3, w3, 0 */
4516 0xd61f0040, /* br x2 */
4517 0xd503201f, /* nop */
4518 0xd503201f, /* nop */
4523 Output_data_plt_aarch64_standard<64, false>::
4524 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4526 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4527 0x90000002, /* adrp x2, 0 */
4528 0x90000003, /* adrp x3, 0 */
4529 0xf9400042, /* ldr x2, [x2, #0] */
4530 0x91000063, /* add x3, x3, 0 */
4531 0xd61f0040, /* br x2 */
4532 0xd503201f, /* nop */
4533 0xd503201f, /* nop */
4538 Output_data_plt_aarch64_standard<64, true>::
4539 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4541 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4542 0x90000002, /* adrp x2, 0 */
4543 0x90000003, /* adrp x3, 0 */
4544 0xf9400042, /* ldr x2, [x2, #0] */
4545 0x91000063, /* add x3, x3, 0 */
4546 0xd61f0040, /* br x2 */
4547 0xd503201f, /* nop */
4548 0xd503201f, /* nop */
4551 template<int size, bool big_endian>
4553 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_tlsdesc_entry(
4555 Address gotplt_address,
4556 Address plt_address,
4558 unsigned int tlsdesc_got_offset,
4559 unsigned int plt_offset)
4561 memcpy(pov, tlsdesc_plt_entry, plt_tlsdesc_entry_size);
4563 // move DT_TLSDESC_GOT address into x2
4564 // move .got.plt address into x3
4565 Address tlsdesc_got_entry = got_base + tlsdesc_got_offset;
4566 Address plt_entry_address = plt_address + plt_offset;
4568 // R_AARCH64_ADR_PREL_PG_HI21
4569 AArch64_relocate_functions<size, big_endian>::adrp(
4572 plt_entry_address + 4);
4574 // R_AARCH64_ADR_PREL_PG_HI21
4575 AArch64_relocate_functions<size, big_endian>::adrp(
4578 plt_entry_address + 8);
4580 // R_AARCH64_LDST64_ABS_LO12
4581 elfcpp::Swap<32, big_endian>::writeval(
4583 ((this->tlsdesc_plt_entry[3] & 0xffc003ff)
4584 | ((tlsdesc_got_entry & 0xff8) << 7)));
4586 // R_AARCH64_ADD_ABS_LO12
4587 elfcpp::Swap<32, big_endian>::writeval(
4589 ((this->tlsdesc_plt_entry[4] & 0xffc003ff)
4590 | ((gotplt_address & 0xfff) << 10)));
4593 // Write out the PLT. This uses the hand-coded instructions above,
4594 // and adjusts them as needed. This is specified by the AMD64 ABI.
4596 template<int size, bool big_endian>
4598 Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
4600 const off_t offset = this->offset();
4601 const section_size_type oview_size =
4602 convert_to_section_size_type(this->data_size());
4603 unsigned char* const oview = of->get_output_view(offset, oview_size);
4605 const off_t got_file_offset = this->got_plt_->offset();
4606 gold_assert(got_file_offset + this->got_plt_->data_size()
4607 == this->got_irelative_->offset());
4609 const section_size_type got_size =
4610 convert_to_section_size_type(this->got_plt_->data_size()
4611 + this->got_irelative_->data_size());
4612 unsigned char* const got_view = of->get_output_view(got_file_offset,
4615 unsigned char* pov = oview;
4617 // The base address of the .plt section.
4618 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
4619 // The base address of the PLT portion of the .got section.
4620 typename elfcpp::Elf_types<size>::Elf_Addr gotplt_address
4621 = this->got_plt_->address();
4623 this->fill_first_plt_entry(pov, gotplt_address, plt_address);
4624 pov += this->first_plt_entry_offset();
4626 // The first three entries in .got.plt are reserved.
4627 unsigned char* got_pov = got_view;
4628 memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
4629 got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
4631 unsigned int plt_offset = this->first_plt_entry_offset();
4632 unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
4633 const unsigned int count = this->count_ + this->irelative_count_;
4634 for (unsigned int plt_index = 0;
4637 pov += this->get_plt_entry_size(),
4638 got_pov += size / 8,
4639 plt_offset += this->get_plt_entry_size(),
4640 got_offset += size / 8)
4642 // Set and adjust the PLT entry itself.
4643 this->fill_plt_entry(pov, gotplt_address, plt_address,
4644 got_offset, plt_offset);
4646 // Set the entry in the GOT, which points to plt0.
4647 elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
4650 if (this->has_tlsdesc_entry())
4652 // Set and adjust the reserved TLSDESC PLT entry.
4653 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
4654 // The base address of the .base section.
4655 typename elfcpp::Elf_types<size>::Elf_Addr got_base =
4656 this->got_->address();
4657 this->fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
4658 tlsdesc_got_offset, plt_offset);
4659 pov += this->get_plt_tlsdesc_entry_size();
4662 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4663 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4665 of->write_output_view(offset, oview_size, oview);
4666 of->write_output_view(got_file_offset, got_size, got_view);
4669 // Telling how to update the immediate field of an instruction.
4670 struct AArch64_howto
4672 // The immediate field mask.
4673 elfcpp::Elf_Xword dst_mask;
4675 // The offset to apply relocation immediate
4678 // The second part offset, if the immediate field has two parts.
4679 // -1 if the immediate field has only one part.
4683 static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
4685 {0, -1, -1}, // DATA
4686 {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
4687 {0xffffe0, 5, -1}, // LD [23:5]-imm19
4688 {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
4689 {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
4690 {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
4691 {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
4692 {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
4693 {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
4694 {0x3ffffff, 0, -1}, // B [25:0]-imm26
4695 {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
4698 // AArch64 relocate function class
4700 template<int size, bool big_endian>
4701 class AArch64_relocate_functions
4706 STATUS_OKAY, // No error during relocation.
4707 STATUS_OVERFLOW, // Relocation overflow.
4708 STATUS_BAD_RELOC, // Relocation cannot be applied.
4711 typedef AArch64_relocate_functions<size, big_endian> This;
4712 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4713 typedef Relocate_info<size, big_endian> The_relocate_info;
4714 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
4715 typedef Reloc_stub<size, big_endian> The_reloc_stub;
4716 typedef Stub_table<size, big_endian> The_stub_table;
4717 typedef elfcpp::Rela<size, big_endian> The_rela;
4718 typedef typename elfcpp::Swap<size, big_endian>::Valtype AArch64_valtype;
4720 // Return the page address of the address.
4721 // Page(address) = address & ~0xFFF
4723 static inline AArch64_valtype
4724 Page(Address address)
4726 return (address & (~static_cast<Address>(0xFFF)));
4730 // Update instruction (pointed by view) with selected bits (immed).
4731 // val = (val & ~dst_mask) | (immed << doffset)
4733 template<int valsize>
4735 update_view(unsigned char* view,
4736 AArch64_valtype immed,
4737 elfcpp::Elf_Xword doffset,
4738 elfcpp::Elf_Xword dst_mask)
4740 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4741 Valtype* wv = reinterpret_cast<Valtype*>(view);
4742 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
4744 // Clear immediate fields.
4746 elfcpp::Swap<valsize, big_endian>::writeval(wv,
4747 static_cast<Valtype>(val | (immed << doffset)));
4750 // Update two parts of an instruction (pointed by view) with selected
4751 // bits (immed1 and immed2).
4752 // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
4754 template<int valsize>
4756 update_view_two_parts(
4757 unsigned char* view,
4758 AArch64_valtype immed1,
4759 AArch64_valtype immed2,
4760 elfcpp::Elf_Xword doffset1,
4761 elfcpp::Elf_Xword doffset2,
4762 elfcpp::Elf_Xword dst_mask)
4764 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4765 Valtype* wv = reinterpret_cast<Valtype*>(view);
4766 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
4768 elfcpp::Swap<valsize, big_endian>::writeval(wv,
4769 static_cast<Valtype>(val | (immed1 << doffset1) |
4770 (immed2 << doffset2)));
4773 // Update adr or adrp instruction with immed.
4774 // In adr and adrp: [30:29] immlo [23:5] immhi
4777 update_adr(unsigned char* view, AArch64_valtype immed)
4779 elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
4780 This::template update_view_two_parts<32>(
4783 (immed & 0x1ffffc) >> 2,
4789 // Update movz/movn instruction with bits immed.
4790 // Set instruction to movz if is_movz is true, otherwise set instruction
4794 update_movnz(unsigned char* view,
4795 AArch64_valtype immed,
4798 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4799 Valtype* wv = reinterpret_cast<Valtype*>(view);
4800 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4802 const elfcpp::Elf_Xword doffset =
4803 aarch64_howto[AArch64_reloc_property::INST_MOVW].doffset;
4804 const elfcpp::Elf_Xword dst_mask =
4805 aarch64_howto[AArch64_reloc_property::INST_MOVW].dst_mask;
4807 // Clear immediate fields and opc code.
4808 val &= ~(dst_mask | (0x3 << 29));
4810 // Set instruction to movz or movn.
4811 // movz: [30:29] is 10 movn: [30:29] is 00
4815 elfcpp::Swap<32, big_endian>::writeval(wv,
4816 static_cast<Valtype>(val | (immed << doffset)));
4819 // Update selected bits in text.
4821 template<int valsize>
4822 static inline typename This::Status
4823 reloc_common(unsigned char* view, Address x,
4824 const AArch64_reloc_property* reloc_property)
4826 // Select bits from X.
4827 Address immed = reloc_property->select_x_value(x);
4830 const AArch64_reloc_property::Reloc_inst inst =
4831 reloc_property->reloc_inst();
4832 // If it is a data relocation or instruction has 2 parts of immediate
4833 // fields, you should not call pcrela_general.
4834 gold_assert(aarch64_howto[inst].doffset2 == -1 &&
4835 aarch64_howto[inst].doffset != -1);
4836 This::template update_view<valsize>(view, immed,
4837 aarch64_howto[inst].doffset,
4838 aarch64_howto[inst].dst_mask);
4840 // Do check overflow or alignment if needed.
4841 return (reloc_property->checkup_x_value(x)
4843 : This::STATUS_OVERFLOW);
4848 // Construct a B insn. Note, although we group it here with other relocation
4849 // operation, there is actually no 'relocation' involved here.
4851 construct_b(unsigned char* view, unsigned int branch_offset)
4853 update_view_two_parts<32>(view, 0x05, (branch_offset >> 2),
4857 // Do a simple rela relocation at unaligned addresses.
4859 template<int valsize>
4860 static inline typename This::Status
4861 rela_ua(unsigned char* view,
4862 const Sized_relobj_file<size, big_endian>* object,
4863 const Symbol_value<size>* psymval,
4864 AArch64_valtype addend,
4865 const AArch64_reloc_property* reloc_property)
4867 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
4869 typename elfcpp::Elf_types<size>::Elf_Addr x =
4870 psymval->value(object, addend);
4871 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
4872 static_cast<Valtype>(x));
4873 return (reloc_property->checkup_x_value(x)
4875 : This::STATUS_OVERFLOW);
4878 // Do a simple pc-relative relocation at unaligned addresses.
4880 template<int valsize>
4881 static inline typename This::Status
4882 pcrela_ua(unsigned char* view,
4883 const Sized_relobj_file<size, big_endian>* object,
4884 const Symbol_value<size>* psymval,
4885 AArch64_valtype addend,
4887 const AArch64_reloc_property* reloc_property)
4889 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
4891 Address x = psymval->value(object, addend) - address;
4892 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
4893 static_cast<Valtype>(x));
4894 return (reloc_property->checkup_x_value(x)
4896 : This::STATUS_OVERFLOW);
4899 // Do a simple rela relocation at aligned addresses.
4901 template<int valsize>
4902 static inline typename This::Status
4904 unsigned char* view,
4905 const Sized_relobj_file<size, big_endian>* object,
4906 const Symbol_value<size>* psymval,
4907 AArch64_valtype addend,
4908 const AArch64_reloc_property* reloc_property)
4910 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4911 Valtype* wv = reinterpret_cast<Valtype*>(view);
4912 Address x = psymval->value(object, addend);
4913 elfcpp::Swap<valsize, big_endian>::writeval(wv,static_cast<Valtype>(x));
4914 return (reloc_property->checkup_x_value(x)
4916 : This::STATUS_OVERFLOW);
4919 // Do relocate. Update selected bits in text.
4920 // new_val = (val & ~dst_mask) | (immed << doffset)
4922 template<int valsize>
4923 static inline typename This::Status
4924 rela_general(unsigned char* view,
4925 const Sized_relobj_file<size, big_endian>* object,
4926 const Symbol_value<size>* psymval,
4927 AArch64_valtype addend,
4928 const AArch64_reloc_property* reloc_property)
4930 // Calculate relocation.
4931 Address x = psymval->value(object, addend);
4932 return This::template reloc_common<valsize>(view, x, reloc_property);
4935 // Do relocate. Update selected bits in text.
4936 // new val = (val & ~dst_mask) | (immed << doffset)
4938 template<int valsize>
4939 static inline typename This::Status
4941 unsigned char* view,
4943 AArch64_valtype addend,
4944 const AArch64_reloc_property* reloc_property)
4946 // Calculate relocation.
4947 Address x = s + addend;
4948 return This::template reloc_common<valsize>(view, x, reloc_property);
4951 // Do address relative relocate. Update selected bits in text.
4952 // new val = (val & ~dst_mask) | (immed << doffset)
4954 template<int valsize>
4955 static inline typename This::Status
4957 unsigned char* view,
4958 const Sized_relobj_file<size, big_endian>* object,
4959 const Symbol_value<size>* psymval,
4960 AArch64_valtype addend,
4962 const AArch64_reloc_property* reloc_property)
4964 // Calculate relocation.
4965 Address x = psymval->value(object, addend) - address;
4966 return This::template reloc_common<valsize>(view, x, reloc_property);
4970 // Calculate (S + A) - address, update adr instruction.
4972 static inline typename This::Status
4973 adr(unsigned char* view,
4974 const Sized_relobj_file<size, big_endian>* object,
4975 const Symbol_value<size>* psymval,
4978 const AArch64_reloc_property* /* reloc_property */)
4980 AArch64_valtype x = psymval->value(object, addend) - address;
4981 // Pick bits [20:0] of X.
4982 AArch64_valtype immed = x & 0x1fffff;
4983 update_adr(view, immed);
4984 // Check -2^20 <= X < 2^20
4985 return (size == 64 && Bits<21>::has_overflow((x))
4986 ? This::STATUS_OVERFLOW
4987 : This::STATUS_OKAY);
4990 // Calculate PG(S+A) - PG(address), update adrp instruction.
4991 // R_AARCH64_ADR_PREL_PG_HI21
4993 static inline typename This::Status
4995 unsigned char* view,
4999 AArch64_valtype x = This::Page(sa) - This::Page(address);
5000 // Pick [32:12] of X.
5001 AArch64_valtype immed = (x >> 12) & 0x1fffff;
5002 update_adr(view, immed);
5003 // Check -2^32 <= X < 2^32
5004 return (size == 64 && Bits<33>::has_overflow((x))
5005 ? This::STATUS_OVERFLOW
5006 : This::STATUS_OKAY);
5009 // Calculate PG(S+A) - PG(address), update adrp instruction.
5010 // R_AARCH64_ADR_PREL_PG_HI21
5012 static inline typename This::Status
5013 adrp(unsigned char* view,
5014 const Sized_relobj_file<size, big_endian>* object,
5015 const Symbol_value<size>* psymval,
5018 const AArch64_reloc_property* reloc_property)
5020 Address sa = psymval->value(object, addend);
5021 AArch64_valtype x = This::Page(sa) - This::Page(address);
5022 // Pick [32:12] of X.
5023 AArch64_valtype immed = (x >> 12) & 0x1fffff;
5024 update_adr(view, immed);
5025 return (reloc_property->checkup_x_value(x)
5027 : This::STATUS_OVERFLOW);
5030 // Update mov[n/z] instruction. Check overflow if needed.
5031 // If X >=0, set the instruction to movz and its immediate value to the
5033 // If X < 0, set the instruction to movn and its immediate value to
5034 // NOT (selected bits of).
5036 static inline typename This::Status
5037 movnz(unsigned char* view,
5039 const AArch64_reloc_property* reloc_property)
5041 // Select bits from X.
5044 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedW;
5045 if (static_cast<SignedW>(x) >= 0)
5047 immed = reloc_property->select_x_value(x);
5052 immed = reloc_property->select_x_value(~x);;
5056 // Update movnz instruction.
5057 update_movnz(view, immed, is_movz);
5059 // Do check overflow or alignment if needed.
5060 return (reloc_property->checkup_x_value(x)
5062 : This::STATUS_OVERFLOW);
5066 maybe_apply_stub(unsigned int,
5067 const The_relocate_info*,
5071 const Sized_symbol<size>*,
5072 const Symbol_value<size>*,
5073 const Sized_relobj_file<size, big_endian>*,
5076 }; // End of AArch64_relocate_functions
5079 // For a certain relocation type (usually jump/branch), test to see if the
5080 // destination needs a stub to fulfil. If so, re-route the destination of the
5081 // original instruction to the stub, note, at this time, the stub has already
5084 template<int size, bool big_endian>
5086 AArch64_relocate_functions<size, big_endian>::
5087 maybe_apply_stub(unsigned int r_type,
5088 const The_relocate_info* relinfo,
5089 const The_rela& rela,
5090 unsigned char* view,
5092 const Sized_symbol<size>* gsym,
5093 const Symbol_value<size>* psymval,
5094 const Sized_relobj_file<size, big_endian>* object,
5095 section_size_type current_group_size)
5097 if (parameters->options().relocatable())
5100 typename elfcpp::Elf_types<size>::Elf_Swxword addend = rela.get_r_addend();
5101 Address branch_target = psymval->value(object, 0) + addend;
5103 The_reloc_stub::stub_type_for_reloc(r_type, address, branch_target);
5104 if (stub_type == ST_NONE)
5107 const The_aarch64_relobj* aarch64_relobj =
5108 static_cast<const The_aarch64_relobj*>(object);
5109 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
5110 gold_assert(stub_table != NULL);
5112 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5113 typename The_reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
5114 The_reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
5115 gold_assert(stub != NULL);
5117 Address new_branch_target = stub_table->address() + stub->offset();
5118 typename elfcpp::Swap<size, big_endian>::Valtype branch_offset =
5119 new_branch_target - address;
5120 const AArch64_reloc_property* arp =
5121 aarch64_reloc_property_table->get_reloc_property(r_type);
5122 gold_assert(arp != NULL);
5123 typename This::Status status = This::template
5124 rela_general<32>(view, branch_offset, 0, arp);
5125 if (status != This::STATUS_OKAY)
5126 gold_error(_("Stub is too far away, try a smaller value "
5127 "for '--stub-group-size'. The current value is 0x%lx."),
5128 static_cast<unsigned long>(current_group_size));
5133 // Group input sections for stub generation.
5135 // We group input sections in an output section so that the total size,
5136 // including any padding space due to alignment is smaller than GROUP_SIZE
5137 // unless the only input section in group is bigger than GROUP_SIZE already.
5138 // Then an ARM stub table is created to follow the last input section
5139 // in group. For each group an ARM stub table is created an is placed
5140 // after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
5141 // extend the group after the stub table.
5143 template<int size, bool big_endian>
5145 Target_aarch64<size, big_endian>::group_sections(
5147 section_size_type group_size,
5148 bool stubs_always_after_branch,
5151 // Group input sections and insert stub table
5152 Layout::Section_list section_list;
5153 layout->get_executable_sections(§ion_list);
5154 for (Layout::Section_list::const_iterator p = section_list.begin();
5155 p != section_list.end();
5158 AArch64_output_section<size, big_endian>* output_section =
5159 static_cast<AArch64_output_section<size, big_endian>*>(*p);
5160 output_section->group_sections(group_size, stubs_always_after_branch,
5166 // Find the AArch64_input_section object corresponding to the SHNDX-th input
5167 // section of RELOBJ.
5169 template<int size, bool big_endian>
5170 AArch64_input_section<size, big_endian>*
5171 Target_aarch64<size, big_endian>::find_aarch64_input_section(
5172 Relobj* relobj, unsigned int shndx) const
5174 Section_id sid(relobj, shndx);
5175 typename AArch64_input_section_map::const_iterator p =
5176 this->aarch64_input_section_map_.find(sid);
5177 return (p != this->aarch64_input_section_map_.end()) ? p->second : NULL;
5181 // Make a new AArch64_input_section object.
5183 template<int size, bool big_endian>
5184 AArch64_input_section<size, big_endian>*
5185 Target_aarch64<size, big_endian>::new_aarch64_input_section(
5186 Relobj* relobj, unsigned int shndx)
5188 Section_id sid(relobj, shndx);
5190 AArch64_input_section<size, big_endian>* input_section =
5191 new AArch64_input_section<size, big_endian>(relobj, shndx);
5192 input_section->init();
5194 // Register new AArch64_input_section in map for look-up.
5195 std::pair<typename AArch64_input_section_map::iterator,bool> ins =
5196 this->aarch64_input_section_map_.insert(
5197 std::make_pair(sid, input_section));
5199 // Make sure that it we have not created another AArch64_input_section
5200 // for this input section already.
5201 gold_assert(ins.second);
5203 return input_section;
5207 // Relaxation hook. This is where we do stub generation.
5209 template<int size, bool big_endian>
5211 Target_aarch64<size, big_endian>::do_relax(
5213 const Input_objects* input_objects,
5214 Symbol_table* symtab,
5218 gold_assert(!parameters->options().relocatable());
5221 // We don't handle negative stub_group_size right now.
5222 this->stub_group_size_ = abs(parameters->options().stub_group_size());
5223 if (this->stub_group_size_ == 1)
5225 // Leave room for 4096 4-byte stub entries. If we exceed that, then we
5226 // will fail to link. The user will have to relink with an explicit
5227 // group size option.
5228 this->stub_group_size_ = The_reloc_stub::MAX_BRANCH_OFFSET -
5231 group_sections(layout, this->stub_group_size_, true, task);
5235 // If this is not the first pass, addresses and file offsets have
5236 // been reset at this point, set them here.
5237 for (Stub_table_iterator sp = this->stub_tables_.begin();
5238 sp != this->stub_tables_.end(); ++sp)
5240 The_stub_table* stt = *sp;
5241 The_aarch64_input_section* owner = stt->owner();
5242 off_t off = align_address(owner->original_size(),
5244 stt->set_address_and_file_offset(owner->address() + off,
5245 owner->offset() + off);
5249 // Scan relocs for relocation stubs
5250 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
5251 op != input_objects->relobj_end();
5254 The_aarch64_relobj* aarch64_relobj =
5255 static_cast<The_aarch64_relobj*>(*op);
5256 // Lock the object so we can read from it. This is only called
5257 // single-threaded from Layout::finalize, so it is OK to lock.
5258 Task_lock_obj<Object> tl(task, aarch64_relobj);
5259 aarch64_relobj->scan_sections_for_stubs(this, symtab, layout);
5262 bool any_stub_table_changed = false;
5263 for (Stub_table_iterator siter = this->stub_tables_.begin();
5264 siter != this->stub_tables_.end() && !any_stub_table_changed; ++siter)
5266 The_stub_table* stub_table = *siter;
5267 if (stub_table->update_data_size_changed_p())
5269 The_aarch64_input_section* owner = stub_table->owner();
5270 uint64_t address = owner->address();
5271 off_t offset = owner->offset();
5272 owner->reset_address_and_file_offset();
5273 owner->set_address_and_file_offset(address, offset);
5275 any_stub_table_changed = true;
5279 // Do not continue relaxation.
5280 bool continue_relaxation = any_stub_table_changed;
5281 if (!continue_relaxation)
5282 for (Stub_table_iterator sp = this->stub_tables_.begin();
5283 (sp != this->stub_tables_.end());
5285 (*sp)->finalize_stubs();
5287 return continue_relaxation;
5291 // Make a new Stub_table.
5293 template<int size, bool big_endian>
5294 Stub_table<size, big_endian>*
5295 Target_aarch64<size, big_endian>::new_stub_table(
5296 AArch64_input_section<size, big_endian>* owner)
5298 Stub_table<size, big_endian>* stub_table =
5299 new Stub_table<size, big_endian>(owner);
5300 stub_table->set_address(align_address(
5301 owner->address() + owner->data_size(), 8));
5302 stub_table->set_file_offset(owner->offset() + owner->data_size());
5303 stub_table->finalize_data_size();
5305 this->stub_tables_.push_back(stub_table);
5311 template<int size, bool big_endian>
5313 Target_aarch64<size, big_endian>::do_reloc_addend(
5314 void* arg, unsigned int r_type, uint64_t) const
5316 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
5317 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5318 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5319 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5320 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5321 gold_assert(psymval->is_tls_symbol());
5322 // The value of a TLS symbol is the offset in the TLS segment.
5323 return psymval->value(ti.object, 0);
5326 // Return the number of entries in the PLT.
5328 template<int size, bool big_endian>
5330 Target_aarch64<size, big_endian>::plt_entry_count() const
5332 if (this->plt_ == NULL)
5334 return this->plt_->entry_count();
5337 // Return the offset of the first non-reserved PLT entry.
5339 template<int size, bool big_endian>
5341 Target_aarch64<size, big_endian>::first_plt_entry_offset() const
5343 return this->plt_->first_plt_entry_offset();
5346 // Return the size of each PLT entry.
5348 template<int size, bool big_endian>
5350 Target_aarch64<size, big_endian>::plt_entry_size() const
5352 return this->plt_->get_plt_entry_size();
5355 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
5357 template<int size, bool big_endian>
5359 Target_aarch64<size, big_endian>::define_tls_base_symbol(
5360 Symbol_table* symtab, Layout* layout)
5362 if (this->tls_base_symbol_defined_)
5365 Output_segment* tls_segment = layout->tls_segment();
5366 if (tls_segment != NULL)
5368 // _TLS_MODULE_BASE_ always points to the beginning of tls segment.
5369 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
5370 Symbol_table::PREDEFINED,
5374 elfcpp::STV_HIDDEN, 0,
5375 Symbol::SEGMENT_START,
5378 this->tls_base_symbol_defined_ = true;
5381 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
5383 template<int size, bool big_endian>
5385 Target_aarch64<size, big_endian>::reserve_tlsdesc_entries(
5386 Symbol_table* symtab, Layout* layout)
5388 if (this->plt_ == NULL)
5389 this->make_plt_section(symtab, layout);
5391 if (!this->plt_->has_tlsdesc_entry())
5393 // Allocate the TLSDESC_GOT entry.
5394 Output_data_got_aarch64<size, big_endian>* got =
5395 this->got_section(symtab, layout);
5396 unsigned int got_offset = got->add_constant(0);
5398 // Allocate the TLSDESC_PLT entry.
5399 this->plt_->reserve_tlsdesc_entry(got_offset);
5403 // Create a GOT entry for the TLS module index.
5405 template<int size, bool big_endian>
5407 Target_aarch64<size, big_endian>::got_mod_index_entry(
5408 Symbol_table* symtab, Layout* layout,
5409 Sized_relobj_file<size, big_endian>* object)
5411 if (this->got_mod_index_offset_ == -1U)
5413 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5414 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5415 Output_data_got_aarch64<size, big_endian>* got =
5416 this->got_section(symtab, layout);
5417 unsigned int got_offset = got->add_constant(0);
5418 rela_dyn->add_local(object, 0, elfcpp::R_AARCH64_TLS_DTPMOD64, got,
5420 got->add_constant(0);
5421 this->got_mod_index_offset_ = got_offset;
5423 return this->got_mod_index_offset_;
5426 // Optimize the TLS relocation type based on what we know about the
5427 // symbol. IS_FINAL is true if the final address of this symbol is
5428 // known at link time.
5430 template<int size, bool big_endian>
5431 tls::Tls_optimization
5432 Target_aarch64<size, big_endian>::optimize_tls_reloc(bool is_final,
5435 // If we are generating a shared library, then we can't do anything
5437 if (parameters->options().shared())
5438 return tls::TLSOPT_NONE;
5442 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5443 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5444 case elfcpp::R_AARCH64_TLSDESC_LD_PREL19:
5445 case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21:
5446 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5447 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5448 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5449 case elfcpp::R_AARCH64_TLSDESC_OFF_G1:
5450 case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC:
5451 case elfcpp::R_AARCH64_TLSDESC_LDR:
5452 case elfcpp::R_AARCH64_TLSDESC_ADD:
5453 case elfcpp::R_AARCH64_TLSDESC_CALL:
5454 // These are General-Dynamic which permits fully general TLS
5455 // access. Since we know that we are generating an executable,
5456 // we can convert this to Initial-Exec. If we also know that
5457 // this is a local symbol, we can further switch to Local-Exec.
5459 return tls::TLSOPT_TO_LE;
5460 return tls::TLSOPT_TO_IE;
5462 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5463 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5464 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5465 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5466 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5467 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5468 // These are Local-Dynamic, which refer to local symbols in the
5469 // dynamic TLS block. Since we know that we generating an
5470 // executable, we can switch to Local-Exec.
5471 return tls::TLSOPT_TO_LE;
5473 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
5474 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
5475 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5476 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5477 case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5478 // These are Initial-Exec relocs which get the thread offset
5479 // from the GOT. If we know that we are linking against the
5480 // local symbol, we can switch to Local-Exec, which links the
5481 // thread offset into the instruction.
5483 return tls::TLSOPT_TO_LE;
5484 return tls::TLSOPT_NONE;
5486 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5487 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5488 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5489 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5490 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5491 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5492 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5493 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5494 // When we already have Local-Exec, there is nothing further we
5496 return tls::TLSOPT_NONE;
5503 // Returns true if this relocation type could be that of a function pointer.
5505 template<int size, bool big_endian>
5507 Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
5508 unsigned int r_type)
5512 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
5513 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
5514 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
5515 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5516 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5524 // For safe ICF, scan a relocation for a local symbol to check if it
5525 // corresponds to a function pointer being taken. In that case mark
5526 // the function whose pointer was taken as not foldable.
5528 template<int size, bool big_endian>
5530 Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
5533 Target_aarch64<size, big_endian>* ,
5534 Sized_relobj_file<size, big_endian>* ,
5537 const elfcpp::Rela<size, big_endian>& ,
5538 unsigned int r_type,
5539 const elfcpp::Sym<size, big_endian>&)
5541 // When building a shared library, do not fold any local symbols.
5542 return (parameters->options().shared()
5543 || possible_function_pointer_reloc(r_type));
5546 // For safe ICF, scan a relocation for a global symbol to check if it
5547 // corresponds to a function pointer being taken. In that case mark
5548 // the function whose pointer was taken as not foldable.
5550 template<int size, bool big_endian>
5552 Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
5555 Target_aarch64<size, big_endian>* ,
5556 Sized_relobj_file<size, big_endian>* ,
5559 const elfcpp::Rela<size, big_endian>& ,
5560 unsigned int r_type,
5563 // When building a shared library, do not fold symbols whose visibility
5564 // is hidden, internal or protected.
5565 return ((parameters->options().shared()
5566 && (gsym->visibility() == elfcpp::STV_INTERNAL
5567 || gsym->visibility() == elfcpp::STV_PROTECTED
5568 || gsym->visibility() == elfcpp::STV_HIDDEN))
5569 || possible_function_pointer_reloc(r_type));
5572 // Report an unsupported relocation against a local symbol.
5574 template<int size, bool big_endian>
5576 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
5577 Sized_relobj_file<size, big_endian>* object,
5578 unsigned int r_type)
5580 gold_error(_("%s: unsupported reloc %u against local symbol"),
5581 object->name().c_str(), r_type);
5584 // We are about to emit a dynamic relocation of type R_TYPE. If the
5585 // dynamic linker does not support it, issue an error.
5587 template<int size, bool big_endian>
5589 Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
5590 unsigned int r_type)
5592 gold_assert(r_type != elfcpp::R_AARCH64_NONE);
5596 // These are the relocation types supported by glibc for AARCH64.
5597 case elfcpp::R_AARCH64_NONE:
5598 case elfcpp::R_AARCH64_COPY:
5599 case elfcpp::R_AARCH64_GLOB_DAT:
5600 case elfcpp::R_AARCH64_JUMP_SLOT:
5601 case elfcpp::R_AARCH64_RELATIVE:
5602 case elfcpp::R_AARCH64_TLS_DTPREL64:
5603 case elfcpp::R_AARCH64_TLS_DTPMOD64:
5604 case elfcpp::R_AARCH64_TLS_TPREL64:
5605 case elfcpp::R_AARCH64_TLSDESC:
5606 case elfcpp::R_AARCH64_IRELATIVE:
5607 case elfcpp::R_AARCH64_ABS32:
5608 case elfcpp::R_AARCH64_ABS64:
5615 // This prevents us from issuing more than one error per reloc
5616 // section. But we can still wind up issuing more than one
5617 // error per object file.
5618 if (this->issued_non_pic_error_)
5620 gold_assert(parameters->options().output_is_position_independent());
5621 object->error(_("requires unsupported dynamic reloc; "
5622 "recompile with -fPIC"));
5623 this->issued_non_pic_error_ = true;
5627 // Return whether we need to make a PLT entry for a relocation of the
5628 // given type against a STT_GNU_IFUNC symbol.
5630 template<int size, bool big_endian>
5632 Target_aarch64<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
5633 Sized_relobj_file<size, big_endian>* object,
5634 unsigned int r_type)
5636 const AArch64_reloc_property* arp =
5637 aarch64_reloc_property_table->get_reloc_property(r_type);
5638 gold_assert(arp != NULL);
5640 int flags = arp->reference_flags();
5641 if (flags & Symbol::TLS_REF)
5643 gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
5644 object->name().c_str(), arp->name().c_str());
5650 // Scan a relocation for a local symbol.
5652 template<int size, bool big_endian>
5654 Target_aarch64<size, big_endian>::Scan::local(
5655 Symbol_table* symtab,
5657 Target_aarch64<size, big_endian>* target,
5658 Sized_relobj_file<size, big_endian>* object,
5659 unsigned int data_shndx,
5660 Output_section* output_section,
5661 const elfcpp::Rela<size, big_endian>& rela,
5662 unsigned int r_type,
5663 const elfcpp::Sym<size, big_endian>& lsym,
5669 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
5671 Output_data_got_aarch64<size, big_endian>* got =
5672 target->got_section(symtab, layout);
5673 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5675 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5676 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
5677 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
5678 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
5682 case elfcpp::R_AARCH64_ABS32:
5683 case elfcpp::R_AARCH64_ABS16:
5684 if (parameters->options().output_is_position_independent())
5686 gold_error(_("%s: unsupported reloc %u in pos independent link."),
5687 object->name().c_str(), r_type);
5691 case elfcpp::R_AARCH64_ABS64:
5692 // If building a shared library or pie, we need to mark this as a dynmic
5693 // reloction, so that the dynamic loader can relocate it.
5694 if (parameters->options().output_is_position_independent())
5696 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5697 rela_dyn->add_local_relative(object, r_sym,
5698 elfcpp::R_AARCH64_RELATIVE,
5701 rela.get_r_offset(),
5702 rela.get_r_addend(),
5707 case elfcpp::R_AARCH64_PREL64:
5708 case elfcpp::R_AARCH64_PREL32:
5709 case elfcpp::R_AARCH64_PREL16:
5712 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
5713 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
5714 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
5715 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
5716 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
5717 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
5718 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
5719 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
5720 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
5721 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
5724 // Control flow, pc-relative. We don't need to do anything for a relative
5725 // addressing relocation against a local symbol if it does not reference
5727 case elfcpp::R_AARCH64_TSTBR14:
5728 case elfcpp::R_AARCH64_CONDBR19:
5729 case elfcpp::R_AARCH64_JUMP26:
5730 case elfcpp::R_AARCH64_CALL26:
5733 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5734 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5736 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5737 optimize_tls_reloc(!parameters->options().shared(), r_type);
5738 if (tlsopt == tls::TLSOPT_TO_LE)
5741 layout->set_has_static_tls();
5742 // Create a GOT entry for the tp-relative offset.
5743 if (!parameters->doing_static_link())
5745 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
5746 target->rela_dyn_section(layout),
5747 elfcpp::R_AARCH64_TLS_TPREL64);
5749 else if (!object->local_has_got_offset(r_sym,
5750 GOT_TYPE_TLS_OFFSET))
5752 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
5753 unsigned int got_offset =
5754 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
5755 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5756 gold_assert(addend == 0);
5757 got->add_static_reloc(got_offset, elfcpp::R_AARCH64_TLS_TPREL64,
5763 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5764 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5766 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5767 optimize_tls_reloc(!parameters->options().shared(), r_type);
5768 if (tlsopt == tls::TLSOPT_TO_LE)
5770 layout->set_has_static_tls();
5773 gold_assert(tlsopt == tls::TLSOPT_NONE);
5775 got->add_local_pair_with_rel(object,r_sym, data_shndx,
5777 target->rela_dyn_section(layout),
5778 elfcpp::R_AARCH64_TLS_DTPMOD64);
5782 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5783 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5784 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5785 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5786 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5787 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5788 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5789 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5791 layout->set_has_static_tls();
5792 bool output_is_shared = parameters->options().shared();
5793 if (output_is_shared)
5794 gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
5795 object->name().c_str(), r_type);
5799 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5800 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5802 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5803 optimize_tls_reloc(!parameters->options().shared(), r_type);
5804 if (tlsopt == tls::TLSOPT_NONE)
5806 // Create a GOT entry for the module index.
5807 target->got_mod_index_entry(symtab, layout, object);
5809 else if (tlsopt != tls::TLSOPT_TO_LE)
5810 unsupported_reloc_local(object, r_type);
5814 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5815 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5816 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5817 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5820 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5821 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5822 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5824 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5825 optimize_tls_reloc(!parameters->options().shared(), r_type);
5826 target->define_tls_base_symbol(symtab, layout);
5827 if (tlsopt == tls::TLSOPT_NONE)
5829 // Create reserved PLT and GOT entries for the resolver.
5830 target->reserve_tlsdesc_entries(symtab, layout);
5832 // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
5833 // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
5834 // entry needs to be in an area in .got.plt, not .got. Call
5835 // got_section to make sure the section has been created.
5836 target->got_section(symtab, layout);
5837 Output_data_got<size, big_endian>* got =
5838 target->got_tlsdesc_section();
5839 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5840 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
5842 unsigned int got_offset = got->add_constant(0);
5843 got->add_constant(0);
5844 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
5846 Reloc_section* rt = target->rela_tlsdesc_section(layout);
5847 // We store the arguments we need in a vector, and use
5848 // the index into the vector as the parameter to pass
5849 // to the target specific routines.
5850 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
5851 void* arg = reinterpret_cast<void*>(intarg);
5852 rt->add_target_specific(elfcpp::R_AARCH64_TLSDESC, arg,
5853 got, got_offset, 0);
5856 else if (tlsopt != tls::TLSOPT_TO_LE)
5857 unsupported_reloc_local(object, r_type);
5861 case elfcpp::R_AARCH64_TLSDESC_CALL:
5865 unsupported_reloc_local(object, r_type);
5870 // Report an unsupported relocation against a global symbol.
5872 template<int size, bool big_endian>
5874 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
5875 Sized_relobj_file<size, big_endian>* object,
5876 unsigned int r_type,
5879 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5880 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5883 template<int size, bool big_endian>
5885 Target_aarch64<size, big_endian>::Scan::global(
5886 Symbol_table* symtab,
5888 Target_aarch64<size, big_endian>* target,
5889 Sized_relobj_file<size, big_endian> * object,
5890 unsigned int data_shndx,
5891 Output_section* output_section,
5892 const elfcpp::Rela<size, big_endian>& rela,
5893 unsigned int r_type,
5896 // A STT_GNU_IFUNC symbol may require a PLT entry.
5897 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5898 && this->reloc_needs_plt_for_ifunc(object, r_type))
5899 target->make_plt_entry(symtab, layout, gsym);
5901 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
5903 const AArch64_reloc_property* arp =
5904 aarch64_reloc_property_table->get_reloc_property(r_type);
5905 gold_assert(arp != NULL);
5909 case elfcpp::R_AARCH64_ABS16:
5910 case elfcpp::R_AARCH64_ABS32:
5911 case elfcpp::R_AARCH64_ABS64:
5913 // Make a PLT entry if necessary.
5914 if (gsym->needs_plt_entry())
5916 target->make_plt_entry(symtab, layout, gsym);
5917 // Since this is not a PC-relative relocation, we may be
5918 // taking the address of a function. In that case we need to
5919 // set the entry in the dynamic symbol table to the address of
5921 if (gsym->is_from_dynobj() && !parameters->options().shared())
5922 gsym->set_needs_dynsym_value();
5924 // Make a dynamic relocation if necessary.
5925 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
5927 if (!parameters->options().output_is_position_independent()
5928 && gsym->may_need_copy_reloc())
5930 target->copy_reloc(symtab, layout, object,
5931 data_shndx, output_section, gsym, rela);
5933 else if (r_type == elfcpp::R_AARCH64_ABS64
5934 && gsym->type() == elfcpp::STT_GNU_IFUNC
5935 && gsym->can_use_relative_reloc(false)
5936 && !gsym->is_from_dynobj()
5937 && !gsym->is_undefined()
5938 && !gsym->is_preemptible())
5940 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
5941 // symbol. This makes a function address in a PIE executable
5942 // match the address in a shared library that it links against.
5943 Reloc_section* rela_dyn =
5944 target->rela_irelative_section(layout);
5945 unsigned int r_type = elfcpp::R_AARCH64_IRELATIVE;
5946 rela_dyn->add_symbolless_global_addend(gsym, r_type,
5947 output_section, object,
5949 rela.get_r_offset(),
5950 rela.get_r_addend());
5952 else if (r_type == elfcpp::R_AARCH64_ABS64
5953 && gsym->can_use_relative_reloc(false))
5955 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5956 rela_dyn->add_global_relative(gsym,
5957 elfcpp::R_AARCH64_RELATIVE,
5961 rela.get_r_offset(),
5962 rela.get_r_addend(),
5967 check_non_pic(object, r_type);
5968 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*
5969 rela_dyn = target->rela_dyn_section(layout);
5970 rela_dyn->add_global(
5971 gsym, r_type, output_section, object,
5972 data_shndx, rela.get_r_offset(),rela.get_r_addend());
5978 case elfcpp::R_AARCH64_PREL16:
5979 case elfcpp::R_AARCH64_PREL32:
5980 case elfcpp::R_AARCH64_PREL64:
5981 // This is used to fill the GOT absolute address.
5982 if (gsym->needs_plt_entry())
5984 target->make_plt_entry(symtab, layout, gsym);
5988 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
5989 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
5990 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
5991 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
5992 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
5993 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
5994 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
5995 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
5996 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
5997 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
5999 if (gsym->needs_plt_entry())
6000 target->make_plt_entry(symtab, layout, gsym);
6001 // Make a dynamic relocation if necessary.
6002 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
6004 if (parameters->options().output_is_executable()
6005 && gsym->may_need_copy_reloc())
6007 target->copy_reloc(symtab, layout, object,
6008 data_shndx, output_section, gsym, rela);
6014 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6015 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6017 // This pair of relocations is used to access a specific GOT entry.
6018 // Note a GOT entry is an *address* to a symbol.
6019 // The symbol requires a GOT entry
6020 Output_data_got_aarch64<size, big_endian>* got =
6021 target->got_section(symtab, layout);
6022 if (gsym->final_value_is_known())
6024 // For a STT_GNU_IFUNC symbol we want the PLT address.
6025 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
6026 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6028 got->add_global(gsym, GOT_TYPE_STANDARD);
6032 // If this symbol is not fully resolved, we need to add a dynamic
6033 // relocation for it.
6034 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6036 // Use a GLOB_DAT rather than a RELATIVE reloc if:
6038 // 1) The symbol may be defined in some other module.
6039 // 2) We are building a shared library and this is a protected
6040 // symbol; using GLOB_DAT means that the dynamic linker can use
6041 // the address of the PLT in the main executable when appropriate
6042 // so that function address comparisons work.
6043 // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
6044 // again so that function address comparisons work.
6045 if (gsym->is_from_dynobj()
6046 || gsym->is_undefined()
6047 || gsym->is_preemptible()
6048 || (gsym->visibility() == elfcpp::STV_PROTECTED
6049 && parameters->options().shared())
6050 || (gsym->type() == elfcpp::STT_GNU_IFUNC
6051 && parameters->options().output_is_position_independent()))
6052 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
6053 rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
6056 // For a STT_GNU_IFUNC symbol we want to write the PLT
6057 // offset into the GOT, so that function pointer
6058 // comparisons work correctly.
6060 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
6061 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
6064 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6065 // Tell the dynamic linker to use the PLT address
6066 // when resolving relocations.
6067 if (gsym->is_from_dynobj()
6068 && !parameters->options().shared())
6069 gsym->set_needs_dynsym_value();
6073 rela_dyn->add_global_relative(
6074 gsym, elfcpp::R_AARCH64_RELATIVE,
6076 gsym->got_offset(GOT_TYPE_STANDARD),
6085 case elfcpp::R_AARCH64_TSTBR14:
6086 case elfcpp::R_AARCH64_CONDBR19:
6087 case elfcpp::R_AARCH64_JUMP26:
6088 case elfcpp::R_AARCH64_CALL26:
6090 if (gsym->final_value_is_known())
6093 if (gsym->is_defined() &&
6094 !gsym->is_from_dynobj() &&
6095 !gsym->is_preemptible())
6098 // Make plt entry for function call.
6099 target->make_plt_entry(symtab, layout, gsym);
6103 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6104 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // General dynamic
6106 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6107 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6108 if (tlsopt == tls::TLSOPT_TO_LE)
6110 layout->set_has_static_tls();
6113 gold_assert(tlsopt == tls::TLSOPT_NONE);
6116 Output_data_got_aarch64<size, big_endian>* got =
6117 target->got_section(symtab, layout);
6118 // Create 2 consecutive entries for module index and offset.
6119 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
6120 target->rela_dyn_section(layout),
6121 elfcpp::R_AARCH64_TLS_DTPMOD64,
6122 elfcpp::R_AARCH64_TLS_DTPREL64);
6126 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6127 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local dynamic
6129 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6130 optimize_tls_reloc(!parameters->options().shared(), r_type);
6131 if (tlsopt == tls::TLSOPT_NONE)
6133 // Create a GOT entry for the module index.
6134 target->got_mod_index_entry(symtab, layout, object);
6136 else if (tlsopt != tls::TLSOPT_TO_LE)
6137 unsupported_reloc_local(object, r_type);
6141 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6142 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6143 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6144 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local dynamic
6147 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6148 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial executable
6150 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6151 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6152 if (tlsopt == tls::TLSOPT_TO_LE)
6155 layout->set_has_static_tls();
6156 // Create a GOT entry for the tp-relative offset.
6157 Output_data_got_aarch64<size, big_endian>* got
6158 = target->got_section(symtab, layout);
6159 if (!parameters->doing_static_link())
6161 got->add_global_with_rel(
6162 gsym, GOT_TYPE_TLS_OFFSET,
6163 target->rela_dyn_section(layout),
6164 elfcpp::R_AARCH64_TLS_TPREL64);
6166 if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
6168 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
6169 unsigned int got_offset =
6170 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
6171 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6172 gold_assert(addend == 0);
6173 got->add_static_reloc(got_offset,
6174 elfcpp::R_AARCH64_TLS_TPREL64, gsym);
6179 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6180 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6181 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6182 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6183 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6184 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6185 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6186 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: // Local executable
6187 layout->set_has_static_tls();
6188 if (parameters->options().shared())
6189 gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
6190 object->name().c_str(), r_type);
6193 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6194 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6195 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12: // TLS descriptor
6197 target->define_tls_base_symbol(symtab, layout);
6198 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6199 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6200 if (tlsopt == tls::TLSOPT_NONE)
6202 // Create reserved PLT and GOT entries for the resolver.
6203 target->reserve_tlsdesc_entries(symtab, layout);
6205 // Create a double GOT entry with an R_AARCH64_TLSDESC
6206 // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
6207 // entry needs to be in an area in .got.plt, not .got. Call
6208 // got_section to make sure the section has been created.
6209 target->got_section(symtab, layout);
6210 Output_data_got<size, big_endian>* got =
6211 target->got_tlsdesc_section();
6212 Reloc_section* rt = target->rela_tlsdesc_section(layout);
6213 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
6214 elfcpp::R_AARCH64_TLSDESC, 0);
6216 else if (tlsopt == tls::TLSOPT_TO_IE)
6218 // Create a GOT entry for the tp-relative offset.
6219 Output_data_got<size, big_endian>* got
6220 = target->got_section(symtab, layout);
6221 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
6222 target->rela_dyn_section(layout),
6223 elfcpp::R_AARCH64_TLS_TPREL64);
6225 else if (tlsopt != tls::TLSOPT_TO_LE)
6226 unsupported_reloc_global(object, r_type, gsym);
6230 case elfcpp::R_AARCH64_TLSDESC_CALL:
6234 gold_error(_("%s: unsupported reloc type in global scan"),
6235 aarch64_reloc_property_table->
6236 reloc_name_in_error_message(r_type).c_str());
6239 } // End of Scan::global
6242 // Create the PLT section.
6243 template<int size, bool big_endian>
6245 Target_aarch64<size, big_endian>::make_plt_section(
6246 Symbol_table* symtab, Layout* layout)
6248 if (this->plt_ == NULL)
6250 // Create the GOT section first.
6251 this->got_section(symtab, layout);
6253 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
6254 this->got_irelative_);
6256 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
6258 | elfcpp::SHF_EXECINSTR),
6259 this->plt_, ORDER_PLT, false);
6261 // Make the sh_info field of .rela.plt point to .plt.
6262 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
6263 rela_plt_os->set_info_section(this->plt_->output_section());
6267 // Return the section for TLSDESC relocations.
6269 template<int size, bool big_endian>
6270 typename Target_aarch64<size, big_endian>::Reloc_section*
6271 Target_aarch64<size, big_endian>::rela_tlsdesc_section(Layout* layout) const
6273 return this->plt_section()->rela_tlsdesc(layout);
6276 // Create a PLT entry for a global symbol.
6278 template<int size, bool big_endian>
6280 Target_aarch64<size, big_endian>::make_plt_entry(
6281 Symbol_table* symtab,
6285 if (gsym->has_plt_offset())
6288 if (this->plt_ == NULL)
6289 this->make_plt_section(symtab, layout);
6291 this->plt_->add_entry(symtab, layout, gsym);
6294 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6296 template<int size, bool big_endian>
6298 Target_aarch64<size, big_endian>::make_local_ifunc_plt_entry(
6299 Symbol_table* symtab, Layout* layout,
6300 Sized_relobj_file<size, big_endian>* relobj,
6301 unsigned int local_sym_index)
6303 if (relobj->local_has_plt_offset(local_sym_index))
6305 if (this->plt_ == NULL)
6306 this->make_plt_section(symtab, layout);
6307 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
6310 relobj->set_local_plt_offset(local_sym_index, plt_offset);
6313 template<int size, bool big_endian>
6315 Target_aarch64<size, big_endian>::gc_process_relocs(
6316 Symbol_table* symtab,
6318 Sized_relobj_file<size, big_endian>* object,
6319 unsigned int data_shndx,
6320 unsigned int sh_type,
6321 const unsigned char* prelocs,
6323 Output_section* output_section,
6324 bool needs_special_offset_handling,
6325 size_t local_symbol_count,
6326 const unsigned char* plocal_symbols)
6328 if (sh_type == elfcpp::SHT_REL)
6333 gold::gc_process_relocs<
6335 Target_aarch64<size, big_endian>,
6337 typename Target_aarch64<size, big_endian>::Scan,
6338 typename Target_aarch64<size, big_endian>::Relocatable_size_for_reloc>(
6347 needs_special_offset_handling,
6352 // Scan relocations for a section.
6354 template<int size, bool big_endian>
6356 Target_aarch64<size, big_endian>::scan_relocs(
6357 Symbol_table* symtab,
6359 Sized_relobj_file<size, big_endian>* object,
6360 unsigned int data_shndx,
6361 unsigned int sh_type,
6362 const unsigned char* prelocs,
6364 Output_section* output_section,
6365 bool needs_special_offset_handling,
6366 size_t local_symbol_count,
6367 const unsigned char* plocal_symbols)
6369 if (sh_type == elfcpp::SHT_REL)
6371 gold_error(_("%s: unsupported REL reloc section"),
6372 object->name().c_str());
6375 gold::scan_relocs<size, big_endian, Target_aarch64, elfcpp::SHT_RELA, Scan>(
6384 needs_special_offset_handling,
6389 // Return the value to use for a dynamic which requires special
6390 // treatment. This is how we support equality comparisons of function
6391 // pointers across shared library boundaries, as described in the
6392 // processor specific ABI supplement.
6394 template<int size, bool big_endian>
6396 Target_aarch64<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
6398 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
6399 return this->plt_address_for_global(gsym);
6403 // Finalize the sections.
6405 template<int size, bool big_endian>
6407 Target_aarch64<size, big_endian>::do_finalize_sections(
6409 const Input_objects*,
6410 Symbol_table* symtab)
6412 const Reloc_section* rel_plt = (this->plt_ == NULL
6414 : this->plt_->rela_plt());
6415 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
6416 this->rela_dyn_, true, false);
6418 // Emit any relocs we saved in an attempt to avoid generating COPY
6420 if (this->copy_relocs_.any_saved_relocs())
6421 this->copy_relocs_.emit(this->rela_dyn_section(layout));
6423 // Fill in some more dynamic tags.
6424 Output_data_dynamic* const odyn = layout->dynamic_data();
6427 if (this->plt_ != NULL
6428 && this->plt_->output_section() != NULL
6429 && this->plt_ ->has_tlsdesc_entry())
6431 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
6432 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
6433 this->got_->finalize_data_size();
6434 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
6435 this->plt_, plt_offset);
6436 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
6437 this->got_, got_offset);
6441 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
6442 // the .got.plt section.
6443 Symbol* sym = this->global_offset_table_;
6446 uint64_t data_size = this->got_plt_->current_data_size();
6447 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
6449 // If the .got section is more than 0x8000 bytes, we add
6450 // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
6451 // bit relocations have a greater chance of working.
6452 if (data_size >= 0x8000)
6453 symtab->get_sized_symbol<size>(sym)->set_value(
6454 symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
6457 if (parameters->doing_static_link()
6458 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
6460 // If linking statically, make sure that the __rela_iplt symbols
6461 // were defined if necessary, even if we didn't create a PLT.
6462 static const Define_symbol_in_segment syms[] =
6465 "__rela_iplt_start", // name
6466 elfcpp::PT_LOAD, // segment_type
6467 elfcpp::PF_W, // segment_flags_set
6468 elfcpp::PF(0), // segment_flags_clear
6471 elfcpp::STT_NOTYPE, // type
6472 elfcpp::STB_GLOBAL, // binding
6473 elfcpp::STV_HIDDEN, // visibility
6475 Symbol::SEGMENT_START, // offset_from_base
6479 "__rela_iplt_end", // name
6480 elfcpp::PT_LOAD, // segment_type
6481 elfcpp::PF_W, // segment_flags_set
6482 elfcpp::PF(0), // segment_flags_clear
6485 elfcpp::STT_NOTYPE, // type
6486 elfcpp::STB_GLOBAL, // binding
6487 elfcpp::STV_HIDDEN, // visibility
6489 Symbol::SEGMENT_START, // offset_from_base
6494 symtab->define_symbols(layout, 2, syms,
6495 layout->script_options()->saw_sections_clause());
6501 // Perform a relocation.
6503 template<int size, bool big_endian>
6505 Target_aarch64<size, big_endian>::Relocate::relocate(
6506 const Relocate_info<size, big_endian>* relinfo,
6507 Target_aarch64<size, big_endian>* target,
6510 const elfcpp::Rela<size, big_endian>& rela,
6511 unsigned int r_type,
6512 const Sized_symbol<size>* gsym,
6513 const Symbol_value<size>* psymval,
6514 unsigned char* view,
6515 typename elfcpp::Elf_types<size>::Elf_Addr address,
6516 section_size_type /* view_size */)
6521 typedef AArch64_relocate_functions<size, big_endian> Reloc;
6523 const AArch64_reloc_property* reloc_property =
6524 aarch64_reloc_property_table->get_reloc_property(r_type);
6526 if (reloc_property == NULL)
6528 std::string reloc_name =
6529 aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
6530 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6531 _("cannot relocate %s in object file"),
6532 reloc_name.c_str());
6536 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
6538 // Pick the value to use for symbols defined in the PLT.
6539 Symbol_value<size> symval;
6541 && gsym->use_plt_offset(reloc_property->reference_flags()))
6543 symval.set_output_value(target->plt_address_for_global(gsym));
6546 else if (gsym == NULL && psymval->is_ifunc_symbol())
6548 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6549 if (object->local_has_plt_offset(r_sym))
6551 symval.set_output_value(target->plt_address_for_local(object, r_sym));
6556 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6558 // Get the GOT offset if needed.
6559 // For aarch64, the GOT pointer points to the start of the GOT section.
6560 bool have_got_offset = false;
6562 int got_base = (target->got_ != NULL
6563 ? (target->got_->current_data_size() >= 0x8000
6568 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
6569 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
6570 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
6571 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
6572 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
6573 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
6574 case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
6575 case elfcpp::R_AARCH64_GOTREL64:
6576 case elfcpp::R_AARCH64_GOTREL32:
6577 case elfcpp::R_AARCH64_GOT_LD_PREL19:
6578 case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
6579 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6580 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6581 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
6584 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6585 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
6589 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6590 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6591 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
6594 have_got_offset = true;
6601 typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
6602 typename elfcpp::Elf_types<size>::Elf_Addr value;
6605 case elfcpp::R_AARCH64_NONE:
6608 case elfcpp::R_AARCH64_ABS64:
6609 reloc_status = Reloc::template rela_ua<64>(
6610 view, object, psymval, addend, reloc_property);
6613 case elfcpp::R_AARCH64_ABS32:
6614 reloc_status = Reloc::template rela_ua<32>(
6615 view, object, psymval, addend, reloc_property);
6618 case elfcpp::R_AARCH64_ABS16:
6619 reloc_status = Reloc::template rela_ua<16>(
6620 view, object, psymval, addend, reloc_property);
6623 case elfcpp::R_AARCH64_PREL64:
6624 reloc_status = Reloc::template pcrela_ua<64>(
6625 view, object, psymval, addend, address, reloc_property);
6628 case elfcpp::R_AARCH64_PREL32:
6629 reloc_status = Reloc::template pcrela_ua<32>(
6630 view, object, psymval, addend, address, reloc_property);
6633 case elfcpp::R_AARCH64_PREL16:
6634 reloc_status = Reloc::template pcrela_ua<16>(
6635 view, object, psymval, addend, address, reloc_property);
6638 case elfcpp::R_AARCH64_LD_PREL_LO19:
6639 reloc_status = Reloc::template pcrela_general<32>(
6640 view, object, psymval, addend, address, reloc_property);
6643 case elfcpp::R_AARCH64_ADR_PREL_LO21:
6644 reloc_status = Reloc::adr(view, object, psymval, addend,
6645 address, reloc_property);
6648 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
6649 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
6650 reloc_status = Reloc::adrp(view, object, psymval, addend, address,
6654 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
6655 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
6656 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
6657 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
6658 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
6659 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
6660 reloc_status = Reloc::template rela_general<32>(
6661 view, object, psymval, addend, reloc_property);
6664 case elfcpp::R_AARCH64_CALL26:
6665 if (this->skip_call_tls_get_addr_)
6667 // Double check that the TLSGD insn has been optimized away.
6668 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6669 Insntype insn = elfcpp::Swap<32, big_endian>::readval(
6670 reinterpret_cast<Insntype*>(view));
6671 gold_assert((insn & 0xff000000) == 0x91000000);
6673 reloc_status = Reloc::STATUS_OKAY;
6674 this->skip_call_tls_get_addr_ = false;
6675 // Return false to stop further processing this reloc.
6679 case elfcpp::R_AARCH64_JUMP26:
6680 if (Reloc::maybe_apply_stub(r_type, relinfo, rela, view, address,
6681 gsym, psymval, object,
6682 target->stub_group_size_))
6685 case elfcpp::R_AARCH64_TSTBR14:
6686 case elfcpp::R_AARCH64_CONDBR19:
6687 reloc_status = Reloc::template pcrela_general<32>(
6688 view, object, psymval, addend, address, reloc_property);
6691 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6692 gold_assert(have_got_offset);
6693 value = target->got_->address() + got_base + got_offset;
6694 reloc_status = Reloc::adrp(view, value + addend, address);
6697 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6698 gold_assert(have_got_offset);
6699 value = target->got_->address() + got_base + got_offset;
6700 reloc_status = Reloc::template rela_general<32>(
6701 view, value, addend, reloc_property);
6704 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6705 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6706 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6707 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
6708 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6709 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6710 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6711 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
6712 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6713 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6714 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6715 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6716 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6717 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6718 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6719 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6720 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6721 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6722 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6723 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6724 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6725 case elfcpp::R_AARCH64_TLSDESC_CALL:
6726 reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
6727 gsym, psymval, view, address);
6730 // These are dynamic relocations, which are unexpected when linking.
6731 case elfcpp::R_AARCH64_COPY:
6732 case elfcpp::R_AARCH64_GLOB_DAT:
6733 case elfcpp::R_AARCH64_JUMP_SLOT:
6734 case elfcpp::R_AARCH64_RELATIVE:
6735 case elfcpp::R_AARCH64_IRELATIVE:
6736 case elfcpp::R_AARCH64_TLS_DTPREL64:
6737 case elfcpp::R_AARCH64_TLS_DTPMOD64:
6738 case elfcpp::R_AARCH64_TLS_TPREL64:
6739 case elfcpp::R_AARCH64_TLSDESC:
6740 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6741 _("unexpected reloc %u in object file"),
6746 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6747 _("unsupported reloc %s"),
6748 reloc_property->name().c_str());
6752 // Report any errors.
6753 switch (reloc_status)
6755 case Reloc::STATUS_OKAY:
6757 case Reloc::STATUS_OVERFLOW:
6758 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6759 _("relocation overflow in %s"),
6760 reloc_property->name().c_str());
6762 case Reloc::STATUS_BAD_RELOC:
6763 gold_error_at_location(
6766 rela.get_r_offset(),
6767 _("unexpected opcode while processing relocation %s"),
6768 reloc_property->name().c_str());
6778 template<int size, bool big_endian>
6780 typename AArch64_relocate_functions<size, big_endian>::Status
6781 Target_aarch64<size, big_endian>::Relocate::relocate_tls(
6782 const Relocate_info<size, big_endian>* relinfo,
6783 Target_aarch64<size, big_endian>* target,
6785 const elfcpp::Rela<size, big_endian>& rela,
6786 unsigned int r_type, const Sized_symbol<size>* gsym,
6787 const Symbol_value<size>* psymval,
6788 unsigned char* view,
6789 typename elfcpp::Elf_types<size>::Elf_Addr address)
6791 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6792 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6794 Output_segment* tls_segment = relinfo->layout->tls_segment();
6795 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6796 const AArch64_reloc_property* reloc_property =
6797 aarch64_reloc_property_table->get_reloc_property(r_type);
6798 gold_assert(reloc_property != NULL);
6800 const bool is_final = (gsym == NULL
6801 ? !parameters->options().shared()
6802 : gsym->final_value_is_known());
6803 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6804 optimize_tls_reloc(is_final, r_type);
6806 Sized_relobj_file<size, big_endian>* object = relinfo->object;
6807 int tls_got_offset_type;
6810 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6811 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // Global-dynamic
6813 if (tlsopt == tls::TLSOPT_TO_LE)
6815 if (tls_segment == NULL)
6817 gold_assert(parameters->errors()->error_count() > 0
6818 || issue_undefined_symbol_error(gsym));
6819 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6821 return tls_gd_to_le(relinfo, target, rela, r_type, view,
6824 else if (tlsopt == tls::TLSOPT_NONE)
6826 tls_got_offset_type = GOT_TYPE_TLS_PAIR;
6827 // Firstly get the address for the got entry.
6828 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6831 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6832 got_entry_address = target->got_->address() +
6833 gsym->got_offset(tls_got_offset_type);
6837 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6839 object->local_has_got_offset(r_sym, tls_got_offset_type));
6840 got_entry_address = target->got_->address() +
6841 object->local_got_offset(r_sym, tls_got_offset_type);
6844 // Relocate the address into adrp/ld, adrp/add pair.
6847 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6848 return aarch64_reloc_funcs::adrp(
6849 view, got_entry_address + addend, address);
6853 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6854 return aarch64_reloc_funcs::template rela_general<32>(
6855 view, got_entry_address, addend, reloc_property);
6862 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6863 _("unsupported gd_to_ie relaxation on %u"),
6868 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6869 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local-dynamic
6871 if (tlsopt == tls::TLSOPT_TO_LE)
6873 if (tls_segment == NULL)
6875 gold_assert(parameters->errors()->error_count() > 0
6876 || issue_undefined_symbol_error(gsym));
6877 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6879 return this->tls_ld_to_le(relinfo, target, rela, r_type, view,
6883 gold_assert(tlsopt == tls::TLSOPT_NONE);
6884 // Relocate the field with the offset of the GOT entry for
6885 // the module index.
6886 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6887 got_entry_address = (target->got_mod_index_entry(NULL, NULL, NULL) +
6888 target->got_->address());
6892 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6893 return aarch64_reloc_funcs::adrp(
6894 view, got_entry_address + addend, address);
6897 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
6898 return aarch64_reloc_funcs::template rela_general<32>(
6899 view, got_entry_address, addend, reloc_property);
6908 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6909 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6910 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6911 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local-dynamic
6913 AArch64_address value = psymval->value(object, 0);
6914 if (tlsopt == tls::TLSOPT_TO_LE)
6916 if (tls_segment == NULL)
6918 gold_assert(parameters->errors()->error_count() > 0
6919 || issue_undefined_symbol_error(gsym));
6920 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6925 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6926 return aarch64_reloc_funcs::movnz(view, value + addend,
6930 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6931 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6932 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
6933 return aarch64_reloc_funcs::template rela_general<32>(
6934 view, value, addend, reloc_property);
6940 // We should never reach here.
6944 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6945 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial-exec
6947 if (tlsopt == tls::TLSOPT_TO_LE)
6949 if (tls_segment == NULL)
6951 gold_assert(parameters->errors()->error_count() > 0
6952 || issue_undefined_symbol_error(gsym));
6953 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6955 return tls_ie_to_le(relinfo, target, rela, r_type, view,
6958 tls_got_offset_type = GOT_TYPE_TLS_OFFSET;
6960 // Firstly get the address for the got entry.
6961 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6964 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6965 got_entry_address = target->got_->address() +
6966 gsym->got_offset(tls_got_offset_type);
6970 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6972 object->local_has_got_offset(r_sym, tls_got_offset_type));
6973 got_entry_address = target->got_->address() +
6974 object->local_got_offset(r_sym, tls_got_offset_type);
6976 // Relocate the address into adrp/ld, adrp/add pair.
6979 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6980 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
6983 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6984 return aarch64_reloc_funcs::template rela_general<32>(
6985 view, got_entry_address, addend, reloc_property);
6990 // We shall never reach here.
6993 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6994 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6995 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6996 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6997 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6998 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6999 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
7000 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
7002 gold_assert(tls_segment != NULL);
7003 AArch64_address value = psymval->value(object, 0);
7005 if (!parameters->options().shared())
7007 AArch64_address aligned_tcb_size =
7008 align_address(target->tcb_size(),
7009 tls_segment->maximum_alignment());
7010 value += aligned_tcb_size;
7013 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
7014 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
7015 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7016 return aarch64_reloc_funcs::movnz(view, value + addend,
7019 return aarch64_reloc_funcs::template
7020 rela_general<32>(view,
7027 gold_error(_("%s: unsupported reloc %u "
7028 "in non-static TLSLE mode."),
7029 object->name().c_str(), r_type);
7033 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7034 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7035 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7036 case elfcpp::R_AARCH64_TLSDESC_CALL:
7038 if (tlsopt == tls::TLSOPT_TO_LE)
7040 if (tls_segment == NULL)
7042 gold_assert(parameters->errors()->error_count() > 0
7043 || issue_undefined_symbol_error(gsym));
7044 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7046 return tls_desc_gd_to_le(relinfo, target, rela, r_type,
7051 tls_got_offset_type = (tlsopt == tls::TLSOPT_TO_IE
7052 ? GOT_TYPE_TLS_OFFSET
7053 : GOT_TYPE_TLS_DESC);
7054 unsigned int got_tlsdesc_offset = 0;
7055 if (r_type != elfcpp::R_AARCH64_TLSDESC_CALL
7056 && tlsopt == tls::TLSOPT_NONE)
7058 // We created GOT entries in the .got.tlsdesc portion of the
7059 // .got.plt section, but the offset stored in the symbol is the
7060 // offset within .got.tlsdesc.
7061 got_tlsdesc_offset = (target->got_->data_size()
7062 + target->got_plt_section()->data_size());
7064 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7067 gold_assert(gsym->has_got_offset(tls_got_offset_type));
7068 got_entry_address = target->got_->address()
7069 + got_tlsdesc_offset
7070 + gsym->got_offset(tls_got_offset_type);
7074 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7076 object->local_has_got_offset(r_sym, tls_got_offset_type));
7077 got_entry_address = target->got_->address() +
7078 got_tlsdesc_offset +
7079 object->local_got_offset(r_sym, tls_got_offset_type);
7081 if (tlsopt == tls::TLSOPT_TO_IE)
7083 if (tls_segment == NULL)
7085 gold_assert(parameters->errors()->error_count() > 0
7086 || issue_undefined_symbol_error(gsym));
7087 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7089 return tls_desc_gd_to_ie(relinfo, target, rela, r_type,
7090 view, psymval, got_entry_address,
7094 // Now do tlsdesc relocation.
7097 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7098 return aarch64_reloc_funcs::adrp(view,
7099 got_entry_address + addend,
7102 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7103 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7104 return aarch64_reloc_funcs::template rela_general<32>(
7105 view, got_entry_address, addend, reloc_property);
7107 case elfcpp::R_AARCH64_TLSDESC_CALL:
7108 return aarch64_reloc_funcs::STATUS_OKAY;
7118 gold_error(_("%s: unsupported TLS reloc %u."),
7119 object->name().c_str(), r_type);
7121 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7122 } // End of relocate_tls.
7125 template<int size, bool big_endian>
7127 typename AArch64_relocate_functions<size, big_endian>::Status
7128 Target_aarch64<size, big_endian>::Relocate::tls_gd_to_le(
7129 const Relocate_info<size, big_endian>* relinfo,
7130 Target_aarch64<size, big_endian>* target,
7131 const elfcpp::Rela<size, big_endian>& rela,
7132 unsigned int r_type,
7133 unsigned char* view,
7134 const Symbol_value<size>* psymval)
7136 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7137 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7138 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7140 Insntype* ip = reinterpret_cast<Insntype*>(view);
7141 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
7142 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
7143 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
7145 if (r_type == elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC)
7147 // This is the 2nd relocs, optimization should already have been
7149 gold_assert((insn1 & 0xfff00000) == 0x91400000);
7150 return aarch64_reloc_funcs::STATUS_OKAY;
7153 // The original sequence is -
7154 // 90000000 adrp x0, 0 <main>
7155 // 91000000 add x0, x0, #0x0
7156 // 94000000 bl 0 <__tls_get_addr>
7157 // optimized to sequence -
7158 // d53bd040 mrs x0, tpidr_el0
7159 // 91400000 add x0, x0, #0x0, lsl #12
7160 // 91000000 add x0, x0, #0x0
7162 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7163 // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
7164 // have to change "bl tls_get_addr", which does not have a corresponding tls
7165 // relocation type. So before proceeding, we need to make sure compiler
7166 // does not change the sequence.
7167 if(!(insn1 == 0x90000000 // adrp x0,0
7168 && insn2 == 0x91000000 // add x0, x0, #0x0
7169 && insn3 == 0x94000000)) // bl 0
7171 // Ideally we should give up gd_to_le relaxation and do gd access.
7172 // However the gd_to_le relaxation decision has been made early
7173 // in the scan stage, where we did not allocate any GOT entry for
7174 // this symbol. Therefore we have to exit and report error now.
7175 gold_error(_("unexpected reloc insn sequence while relaxing "
7176 "tls gd to le for reloc %u."), r_type);
7177 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7181 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
7182 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
7183 insn3 = 0x91000000; // add x0, x0, #0x0
7184 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
7185 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
7186 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
7188 // Calculate tprel value.
7189 Output_segment* tls_segment = relinfo->layout->tls_segment();
7190 gold_assert(tls_segment != NULL);
7191 AArch64_address value = psymval->value(relinfo->object, 0);
7192 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7193 AArch64_address aligned_tcb_size =
7194 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7195 AArch64_address x = value + aligned_tcb_size;
7197 // After new insns are written, apply TLSLE relocs.
7198 const AArch64_reloc_property* rp1 =
7199 aarch64_reloc_property_table->get_reloc_property(
7200 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
7201 const AArch64_reloc_property* rp2 =
7202 aarch64_reloc_property_table->get_reloc_property(
7203 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
7204 gold_assert(rp1 != NULL && rp2 != NULL);
7206 typename aarch64_reloc_funcs::Status s1 =
7207 aarch64_reloc_funcs::template rela_general<32>(view + 4,
7211 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
7214 typename aarch64_reloc_funcs::Status s2 =
7215 aarch64_reloc_funcs::template rela_general<32>(view + 8,
7220 this->skip_call_tls_get_addr_ = true;
7222 } // End of tls_gd_to_le
7225 template<int size, bool big_endian>
7227 typename AArch64_relocate_functions<size, big_endian>::Status
7228 Target_aarch64<size, big_endian>::Relocate::tls_ld_to_le(
7229 const Relocate_info<size, big_endian>* relinfo,
7230 Target_aarch64<size, big_endian>* target,
7231 const elfcpp::Rela<size, big_endian>& rela,
7232 unsigned int r_type,
7233 unsigned char* view,
7234 const Symbol_value<size>* psymval)
7236 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7237 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7238 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7240 Insntype* ip = reinterpret_cast<Insntype*>(view);
7241 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
7242 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
7243 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
7245 if (r_type == elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC)
7247 // This is the 2nd relocs, optimization should already have been
7249 gold_assert((insn1 & 0xfff00000) == 0x91400000);
7250 return aarch64_reloc_funcs::STATUS_OKAY;
7253 // The original sequence is -
7254 // 90000000 adrp x0, 0 <main>
7255 // 91000000 add x0, x0, #0x0
7256 // 94000000 bl 0 <__tls_get_addr>
7257 // optimized to sequence -
7258 // d53bd040 mrs x0, tpidr_el0
7259 // 91400000 add x0, x0, #0x0, lsl #12
7260 // 91000000 add x0, x0, #0x0
7262 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7263 // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
7264 // have to change "bl tls_get_addr", which does not have a corresponding tls
7265 // relocation type. So before proceeding, we need to make sure compiler
7266 // does not change the sequence.
7267 if(!(insn1 == 0x90000000 // adrp x0,0
7268 && insn2 == 0x91000000 // add x0, x0, #0x0
7269 && insn3 == 0x94000000)) // bl 0
7271 // Ideally we should give up gd_to_le relaxation and do gd access.
7272 // However the gd_to_le relaxation decision has been made early
7273 // in the scan stage, where we did not allocate any GOT entry for
7274 // this symbol. Therefore we have to exit and report error now.
7275 gold_error(_("unexpected reloc insn sequence while relaxing "
7276 "tls gd to le for reloc %u."), r_type);
7277 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7281 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
7282 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
7283 insn3 = 0x91000000; // add x0, x0, #0x0
7284 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
7285 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
7286 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
7288 // Calculate tprel value.
7289 Output_segment* tls_segment = relinfo->layout->tls_segment();
7290 gold_assert(tls_segment != NULL);
7291 AArch64_address value = psymval->value(relinfo->object, 0);
7292 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7293 AArch64_address aligned_tcb_size =
7294 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7295 AArch64_address x = value + aligned_tcb_size;
7297 // After new insns are written, apply TLSLE relocs.
7298 const AArch64_reloc_property* rp1 =
7299 aarch64_reloc_property_table->get_reloc_property(
7300 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
7301 const AArch64_reloc_property* rp2 =
7302 aarch64_reloc_property_table->get_reloc_property(
7303 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
7304 gold_assert(rp1 != NULL && rp2 != NULL);
7306 typename aarch64_reloc_funcs::Status s1 =
7307 aarch64_reloc_funcs::template rela_general<32>(view + 4,
7311 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
7314 typename aarch64_reloc_funcs::Status s2 =
7315 aarch64_reloc_funcs::template rela_general<32>(view + 8,
7320 this->skip_call_tls_get_addr_ = true;
7323 } // End of tls_ld_to_le
7325 template<int size, bool big_endian>
7327 typename AArch64_relocate_functions<size, big_endian>::Status
7328 Target_aarch64<size, big_endian>::Relocate::tls_ie_to_le(
7329 const Relocate_info<size, big_endian>* relinfo,
7330 Target_aarch64<size, big_endian>* target,
7331 const elfcpp::Rela<size, big_endian>& rela,
7332 unsigned int r_type,
7333 unsigned char* view,
7334 const Symbol_value<size>* psymval)
7336 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7337 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7338 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7340 AArch64_address value = psymval->value(relinfo->object, 0);
7341 Output_segment* tls_segment = relinfo->layout->tls_segment();
7342 AArch64_address aligned_tcb_address =
7343 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7344 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7345 AArch64_address x = value + addend + aligned_tcb_address;
7346 // "x" is the offset to tp, we can only do this if x is within
7347 // range [0, 2^32-1]
7348 if (!(size == 32 || (size == 64 && (static_cast<uint64_t>(x) >> 32) == 0)))
7350 gold_error(_("TLS variable referred by reloc %u is too far from TP."),
7352 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7355 Insntype* ip = reinterpret_cast<Insntype*>(view);
7356 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
7359 if (r_type == elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21)
7362 regno = (insn & 0x1f);
7363 newinsn = (0xd2a00000 | regno) | (((x >> 16) & 0xffff) << 5);
7365 else if (r_type == elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
7368 regno = (insn & 0x1f);
7369 gold_assert(regno == ((insn >> 5) & 0x1f));
7370 newinsn = (0xf2800000 | regno) | ((x & 0xffff) << 5);
7375 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7376 return aarch64_reloc_funcs::STATUS_OKAY;
7377 } // End of tls_ie_to_le
7380 template<int size, bool big_endian>
7382 typename AArch64_relocate_functions<size, big_endian>::Status
7383 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_le(
7384 const Relocate_info<size, big_endian>* relinfo,
7385 Target_aarch64<size, big_endian>* target,
7386 const elfcpp::Rela<size, big_endian>& rela,
7387 unsigned int r_type,
7388 unsigned char* view,
7389 const Symbol_value<size>* psymval)
7391 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7392 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7393 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7395 // TLSDESC-GD sequence is like:
7396 // adrp x0, :tlsdesc:v1
7397 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7398 // add x0, x0, :tlsdesc_lo12:v1
7401 // After desc_gd_to_le optimization, the sequence will be like:
7402 // movz x0, #0x0, lsl #16
7407 // Calculate tprel value.
7408 Output_segment* tls_segment = relinfo->layout->tls_segment();
7409 gold_assert(tls_segment != NULL);
7410 Insntype* ip = reinterpret_cast<Insntype*>(view);
7411 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7412 AArch64_address value = psymval->value(relinfo->object, addend);
7413 AArch64_address aligned_tcb_size =
7414 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7415 AArch64_address x = value + aligned_tcb_size;
7416 // x is the offset to tp, we can only do this if x is within range
7417 // [0, 2^32-1]. If x is out of range, fail and exit.
7418 if (size == 64 && (static_cast<uint64_t>(x) >> 32) != 0)
7420 gold_error(_("TLS variable referred by reloc %u is too far from TP. "
7421 "We Can't do gd_to_le relaxation.\n"), r_type);
7422 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7427 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7428 case elfcpp::R_AARCH64_TLSDESC_CALL:
7430 newinsn = 0xd503201f;
7433 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7435 newinsn = 0xd2a00000 | (((x >> 16) & 0xffff) << 5);
7438 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7440 newinsn = 0xf2800000 | ((x & 0xffff) << 5);
7444 gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
7448 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7449 return aarch64_reloc_funcs::STATUS_OKAY;
7450 } // End of tls_desc_gd_to_le
7453 template<int size, bool big_endian>
7455 typename AArch64_relocate_functions<size, big_endian>::Status
7456 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_ie(
7457 const Relocate_info<size, big_endian>* /* relinfo */,
7458 Target_aarch64<size, big_endian>* /* target */,
7459 const elfcpp::Rela<size, big_endian>& rela,
7460 unsigned int r_type,
7461 unsigned char* view,
7462 const Symbol_value<size>* /* psymval */,
7463 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address,
7464 typename elfcpp::Elf_types<size>::Elf_Addr address)
7466 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7467 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7469 // TLSDESC-GD sequence is like:
7470 // adrp x0, :tlsdesc:v1
7471 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7472 // add x0, x0, :tlsdesc_lo12:v1
7475 // After desc_gd_to_ie optimization, the sequence will be like:
7476 // adrp x0, :tlsie:v1
7477 // ldr x0, [x0, :tlsie_lo12:v1]
7481 Insntype* ip = reinterpret_cast<Insntype*>(view);
7482 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7486 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7487 case elfcpp::R_AARCH64_TLSDESC_CALL:
7489 newinsn = 0xd503201f;
7490 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7493 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7495 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
7500 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7502 // Set ldr target register to be x0.
7503 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
7505 elfcpp::Swap<32, big_endian>::writeval(ip, insn);
7507 const AArch64_reloc_property* reloc_property =
7508 aarch64_reloc_property_table->get_reloc_property(
7509 elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
7510 return aarch64_reloc_funcs::template rela_general<32>(
7511 view, got_entry_address, addend, reloc_property);
7516 gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
7520 return aarch64_reloc_funcs::STATUS_OKAY;
7521 } // End of tls_desc_gd_to_ie
7523 // Relocate section data.
7525 template<int size, bool big_endian>
7527 Target_aarch64<size, big_endian>::relocate_section(
7528 const Relocate_info<size, big_endian>* relinfo,
7529 unsigned int sh_type,
7530 const unsigned char* prelocs,
7532 Output_section* output_section,
7533 bool needs_special_offset_handling,
7534 unsigned char* view,
7535 typename elfcpp::Elf_types<size>::Elf_Addr address,
7536 section_size_type view_size,
7537 const Reloc_symbol_changes* reloc_symbol_changes)
7539 gold_assert(sh_type == elfcpp::SHT_RELA);
7540 typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
7541 gold::relocate_section<size, big_endian, Target_aarch64, elfcpp::SHT_RELA,
7542 AArch64_relocate, gold::Default_comdat_behavior>(
7548 needs_special_offset_handling,
7552 reloc_symbol_changes);
7555 // Return the size of a relocation while scanning during a relocatable
7558 template<int size, bool big_endian>
7560 Target_aarch64<size, big_endian>::Relocatable_size_for_reloc::
7565 // We will never support SHT_REL relocations.
7570 // Scan the relocs during a relocatable link.
7572 template<int size, bool big_endian>
7574 Target_aarch64<size, big_endian>::scan_relocatable_relocs(
7575 Symbol_table* symtab,
7577 Sized_relobj_file<size, big_endian>* object,
7578 unsigned int data_shndx,
7579 unsigned int sh_type,
7580 const unsigned char* prelocs,
7582 Output_section* output_section,
7583 bool needs_special_offset_handling,
7584 size_t local_symbol_count,
7585 const unsigned char* plocal_symbols,
7586 Relocatable_relocs* rr)
7588 gold_assert(sh_type == elfcpp::SHT_RELA);
7590 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
7591 Relocatable_size_for_reloc> Scan_relocatable_relocs;
7593 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7594 Scan_relocatable_relocs>(
7602 needs_special_offset_handling,
7608 // Relocate a section during a relocatable link.
7610 template<int size, bool big_endian>
7612 Target_aarch64<size, big_endian>::relocate_relocs(
7613 const Relocate_info<size, big_endian>* relinfo,
7614 unsigned int sh_type,
7615 const unsigned char* prelocs,
7617 Output_section* output_section,
7618 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7619 const Relocatable_relocs* rr,
7620 unsigned char* view,
7621 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
7622 section_size_type view_size,
7623 unsigned char* reloc_view,
7624 section_size_type reloc_view_size)
7626 gold_assert(sh_type == elfcpp::SHT_RELA);
7628 gold::relocate_relocs<size, big_endian, elfcpp::SHT_RELA>(
7633 offset_in_output_section,
7643 // Return whether this is a 3-insn erratum sequence.
7645 template<int size, bool big_endian>
7647 Target_aarch64<size, big_endian>::is_erratum_843419_sequence(
7648 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
7649 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
7650 typename elfcpp::Swap<32,big_endian>::Valtype insn3)
7655 // The 2nd insn is a single register load or store; or register pair
7657 if (Insn_utilities::aarch64_mem_op_p(insn2, &rt1, &rt2, &pair, &load)
7658 && (!pair || (pair && !load)))
7660 // The 3rd insn is a load or store instruction from the "Load/store
7661 // register (unsigned immediate)" encoding class, using Rn as the
7662 // base address register.
7663 if (Insn_utilities::aarch64_ldst_uimm(insn3)
7664 && (Insn_utilities::aarch64_rn(insn3)
7665 == Insn_utilities::aarch64_rd(insn1)))
7672 // Scan erratum for section SHNDX range
7673 // [output_address + span_start, output_address + span_end).
7675 template<int size, bool big_endian>
7677 Target_aarch64<size, big_endian>::scan_erratum_843419_span(
7678 AArch64_relobj<size, big_endian>* relobj,
7680 const section_size_type span_start,
7681 const section_size_type span_end,
7682 unsigned char* input_view,
7683 Address output_address)
7685 typedef typename Insn_utilities::Insntype Insntype;
7687 // Adjust output_address and view to the start of span.
7688 output_address += span_start;
7689 input_view += span_start;
7691 if ((output_address & 0x03) != 0)
7694 section_size_type offset = 0;
7695 section_size_type span_length = span_end - span_start;
7696 // The first instruction must be ending at 0xFF8 or 0xFFC.
7697 unsigned int page_offset = output_address & 0xFFF;
7698 // Make sure starting position, that is "output_address+offset",
7699 // starts at page position 0xff8 or 0xffc.
7700 if (page_offset < 0xff8)
7701 offset = 0xff8 - page_offset;
7702 while (offset + 3 * Insn_utilities::BYTES_PER_INSN <= span_length)
7704 Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
7705 Insntype insn1 = ip[0];
7706 if (Insn_utilities::is_adrp(insn1))
7708 Insntype insn2 = ip[1];
7709 Insntype insn3 = ip[2];
7710 Insntype erratum_insn;
7711 unsigned insn_offset;
7712 bool do_report = false;
7713 if (is_erratum_843419_sequence(insn1, insn2, insn3))
7716 erratum_insn = insn3;
7717 insn_offset = 2 * Insn_utilities::BYTES_PER_INSN;
7719 else if (offset + 4 * Insn_utilities::BYTES_PER_INSN <= span_length)
7721 // Optionally we can have an insn between ins2 and ins3
7722 Insntype insn_opt = ip[2];
7723 // And insn_opt must not be a branch.
7724 if (!Insn_utilities::aarch64_b(insn_opt)
7725 && !Insn_utilities::aarch64_bl(insn_opt)
7726 && !Insn_utilities::aarch64_blr(insn_opt)
7727 && !Insn_utilities::aarch64_br(insn_opt))
7729 // And insn_opt must not write to dest reg in insn1. However
7730 // we do a conservative scan, which means we may fix/report
7731 // more than necessary, but it doesn't hurt.
7733 Insntype insn4 = ip[3];
7734 if (is_erratum_843419_sequence(insn1, insn2, insn4))
7737 erratum_insn = insn4;
7738 insn_offset = 3 * Insn_utilities::BYTES_PER_INSN;
7744 gold_warning(_("Erratum 843419 found and fixed at \"%s\", "
7745 "section %d, offset 0x%08x."),
7746 relobj->name().c_str(), shndx,
7747 (unsigned int)(span_start + offset));
7748 unsigned int errata_insn_offset =
7749 span_start + offset + insn_offset;
7750 The_stub_table* stub_table = relobj->stub_table(shndx);
7751 gold_assert(stub_table != NULL);
7752 if (stub_table->find_erratum_stub(relobj,
7754 errata_insn_offset) == NULL)
7756 The_erratum_stub* stub = new The_erratum_stub(
7757 relobj, ST_E_843419, shndx,
7758 errata_insn_offset);
7759 Address erratum_address =
7760 output_address + offset + insn_offset;
7761 // Stub destination address is the next insn after the
7763 Address dest_address = erratum_address
7764 + Insn_utilities::BYTES_PER_INSN;
7765 stub->set_erratum_insn(erratum_insn);
7766 stub->set_erratum_address(erratum_address);
7767 stub->set_destination_address(dest_address);
7768 stub_table->add_erratum_stub(stub);
7773 // Advance to next candidate instruction. We only consider instruction
7774 // sequences starting at a page offset of 0xff8 or 0xffc.
7775 page_offset = (output_address + offset) & 0xfff;
7776 if (page_offset == 0xff8)
7778 else // (page_offset == 0xffc), we move to next page's 0xff8.
7781 } // End of "Target_aarch64::scan_erratum_843419_span".
7784 // The selector for aarch64 object files.
7786 template<int size, bool big_endian>
7787 class Target_selector_aarch64 : public Target_selector
7790 Target_selector_aarch64();
7793 do_instantiate_target()
7794 { return new Target_aarch64<size, big_endian>(); }
7798 Target_selector_aarch64<32, true>::Target_selector_aarch64()
7799 : Target_selector(elfcpp::EM_AARCH64, 32, true,
7800 "elf32-bigaarch64", "aarch64_elf32_be_vec")
7804 Target_selector_aarch64<32, false>::Target_selector_aarch64()
7805 : Target_selector(elfcpp::EM_AARCH64, 32, false,
7806 "elf32-littleaarch64", "aarch64_elf32_le_vec")
7810 Target_selector_aarch64<64, true>::Target_selector_aarch64()
7811 : Target_selector(elfcpp::EM_AARCH64, 64, true,
7812 "elf64-bigaarch64", "aarch64_elf64_be_vec")
7816 Target_selector_aarch64<64, false>::Target_selector_aarch64()
7817 : Target_selector(elfcpp::EM_AARCH64, 64, false,
7818 "elf64-littleaarch64", "aarch64_elf64_le_vec")
7821 Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
7822 Target_selector_aarch64<32, false> target_selector_aarch64elf32;
7823 Target_selector_aarch64<64, true> target_selector_aarch64elfb;
7824 Target_selector_aarch64<64, false> target_selector_aarch64elf;
7826 } // End anonymous namespace.