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;
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".
933 template<int size, bool big_endian>
934 const int Erratum_stub<size, big_endian>::STUB_ADDR_ALIGN = 4;
936 // Comparator used in set definition.
937 template<int size, bool big_endian>
938 struct Erratum_stub_less
941 operator()(const Erratum_stub<size, big_endian>* s1,
942 const Erratum_stub<size, big_endian>* s2) const
943 { return *s1 < *s2; }
946 // Erratum_stub implementation for writing stub to output file.
948 template<int size, bool big_endian>
950 Erratum_stub<size, big_endian>::do_write(unsigned char* view, section_size_type)
952 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
953 const Insntype* insns = this->insns();
954 uint32_t num_insns = this->insn_num();
955 Insntype* ip = reinterpret_cast<Insntype*>(view);
956 // For current implemnted erratum 843419, (and 835769 which is to be
957 // implemented soon), the first insn in the stub is always a copy of the
958 // problematic insn (in 843419, the mem access insn), followed by a jump-back.
959 elfcpp::Swap<32, big_endian>::writeval(ip, this->erratum_insn());
960 for (uint32_t i = 1; i < num_insns; ++i)
961 elfcpp::Swap<32, big_endian>::writeval(ip + i, insns[i]);
967 template<int size, bool big_endian>
968 class Reloc_stub : public Stub_base<size, big_endian>
971 typedef Reloc_stub<size, big_endian> This;
972 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
974 // Branch range. This is used to calculate the section group size, as well as
975 // determine whether a stub is needed.
976 static const int MAX_BRANCH_OFFSET = ((1 << 25) - 1) << 2;
977 static const int MIN_BRANCH_OFFSET = -((1 << 25) << 2);
979 // Constant used to determine if an offset fits in the adrp instruction
981 static const int MAX_ADRP_IMM = (1 << 20) - 1;
982 static const int MIN_ADRP_IMM = -(1 << 20);
984 static const int BYTES_PER_INSN = 4;
985 static const int STUB_ADDR_ALIGN;
987 // Determine whether the offset fits in the jump/branch instruction.
989 aarch64_valid_branch_offset_p(int64_t offset)
990 { return offset >= MIN_BRANCH_OFFSET && offset <= MAX_BRANCH_OFFSET; }
992 // Determine whether the offset fits in the adrp immediate field.
994 aarch64_valid_for_adrp_p(AArch64_address location, AArch64_address dest)
996 typedef AArch64_relocate_functions<size, big_endian> Reloc;
997 int64_t adrp_imm = (Reloc::Page(dest) - Reloc::Page(location)) >> 12;
998 return adrp_imm >= MIN_ADRP_IMM && adrp_imm <= MAX_ADRP_IMM;
1001 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
1004 stub_type_for_reloc(unsigned int r_type, AArch64_address address,
1005 AArch64_address target);
1007 Reloc_stub(int type)
1008 : Stub_base<size, big_endian>(type)
1014 // The key class used to index the stub instance in the stub table's stub map.
1018 Key(int type, const Symbol* symbol, const Relobj* relobj,
1019 unsigned int r_sym, int32_t addend)
1020 : type_(type), addend_(addend)
1024 this->r_sym_ = Reloc_stub::invalid_index;
1025 this->u_.symbol = symbol;
1029 gold_assert(relobj != NULL && r_sym != invalid_index);
1030 this->r_sym_ = r_sym;
1031 this->u_.relobj = relobj;
1038 // Return stub type.
1041 { return this->type_; }
1043 // Return the local symbol index or invalid_index.
1046 { return this->r_sym_; }
1048 // Return the symbol if there is one.
1051 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
1053 // Return the relobj if there is one.
1056 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
1058 // Whether this equals to another key k.
1060 eq(const Key& k) const
1062 return ((this->type_ == k.type_)
1063 && (this->r_sym_ == k.r_sym_)
1064 && ((this->r_sym_ != Reloc_stub::invalid_index)
1065 ? (this->u_.relobj == k.u_.relobj)
1066 : (this->u_.symbol == k.u_.symbol))
1067 && (this->addend_ == k.addend_));
1070 // Return a hash value.
1074 size_t name_hash_value = gold::string_hash<char>(
1075 (this->r_sym_ != Reloc_stub::invalid_index)
1076 ? this->u_.relobj->name().c_str()
1077 : this->u_.symbol->name());
1078 // We only have 4 stub types.
1079 size_t stub_type_hash_value = 0x03 & this->type_;
1080 return (name_hash_value
1081 ^ stub_type_hash_value
1082 ^ ((this->r_sym_ & 0x3fff) << 2)
1083 ^ ((this->addend_ & 0xffff) << 16));
1086 // Functors for STL associative containers.
1090 operator()(const Key& k) const
1091 { return k.hash_value(); }
1097 operator()(const Key& k1, const Key& k2) const
1098 { return k1.eq(k2); }
1104 // If this is a local symbol, this is the index in the defining object.
1105 // Otherwise, it is invalid_index for a global symbol.
1106 unsigned int r_sym_;
1107 // If r_sym_ is an invalid index, this points to a global symbol.
1108 // Otherwise, it points to a relobj. We used the unsized and target
1109 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
1110 // Arm_relobj, in order to avoid making the stub class a template
1111 // as most of the stub machinery is endianness-neutral. However, it
1112 // may require a bit of casting done by users of this class.
1115 const Symbol* symbol;
1116 const Relobj* relobj;
1118 // Addend associated with a reloc.
1120 }; // End of inner class Reloc_stub::Key
1123 // This may be overridden in the child class.
1125 do_write(unsigned char*, section_size_type);
1128 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
1129 }; // End of Reloc_stub
1131 template<int size, bool big_endian>
1132 const int Reloc_stub<size, big_endian>::STUB_ADDR_ALIGN = 4;
1134 // Write data to output file.
1136 template<int size, bool big_endian>
1138 Reloc_stub<size, big_endian>::
1139 do_write(unsigned char* view, section_size_type)
1141 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
1142 const uint32_t* insns = this->insns();
1143 uint32_t num_insns = this->insn_num();
1144 Insntype* ip = reinterpret_cast<Insntype*>(view);
1145 for (uint32_t i = 0; i < num_insns; ++i)
1146 elfcpp::Swap<32, big_endian>::writeval(ip + i, insns[i]);
1150 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
1153 template<int size, bool big_endian>
1155 Reloc_stub<size, big_endian>::stub_type_for_reloc(
1156 unsigned int r_type, AArch64_address location, AArch64_address dest)
1158 int64_t branch_offset = 0;
1161 case elfcpp::R_AARCH64_CALL26:
1162 case elfcpp::R_AARCH64_JUMP26:
1163 branch_offset = dest - location;
1169 if (aarch64_valid_branch_offset_p(branch_offset))
1172 if (aarch64_valid_for_adrp_p(location, dest))
1173 return ST_ADRP_BRANCH;
1175 if (parameters->options().output_is_position_independent()
1176 && parameters->options().output_is_executable())
1177 return ST_LONG_BRANCH_PCREL;
1179 return ST_LONG_BRANCH_ABS;
1182 // A class to hold stubs for the ARM target.
1184 template<int size, bool big_endian>
1185 class Stub_table : public Output_data
1188 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1189 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
1190 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
1191 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1192 typedef Reloc_stub<size, big_endian> The_reloc_stub;
1193 typedef typename The_reloc_stub::Key The_reloc_stub_key;
1194 typedef Erratum_stub<size, big_endian> The_erratum_stub;
1195 typedef Erratum_stub_less<size, big_endian> The_erratum_stub_less;
1196 typedef typename The_reloc_stub_key::hash The_reloc_stub_key_hash;
1197 typedef typename The_reloc_stub_key::equal_to The_reloc_stub_key_equal_to;
1198 typedef Stub_table<size, big_endian> The_stub_table;
1199 typedef Unordered_map<The_reloc_stub_key, The_reloc_stub*,
1200 The_reloc_stub_key_hash, The_reloc_stub_key_equal_to>
1202 typedef typename Reloc_stub_map::const_iterator Reloc_stub_map_const_iter;
1203 typedef Relocate_info<size, big_endian> The_relocate_info;
1205 typedef std::set<The_erratum_stub*, The_erratum_stub_less> Erratum_stub_set;
1206 typedef typename Erratum_stub_set::iterator Erratum_stub_set_iter;
1208 Stub_table(The_aarch64_input_section* owner)
1209 : Output_data(), owner_(owner), reloc_stubs_size_(0),
1210 erratum_stubs_size_(0), prev_data_size_(0)
1216 The_aarch64_input_section*
1220 // Whether this stub table is empty.
1223 { return reloc_stubs_.empty() && erratum_stubs_.empty(); }
1225 // Return the current data size.
1227 current_data_size() const
1228 { return this->current_data_size_for_child(); }
1230 // Add a STUB using KEY. The caller is responsible for avoiding addition
1231 // if a STUB with the same key has already been added.
1233 add_reloc_stub(The_reloc_stub* stub, const The_reloc_stub_key& key);
1235 // Add an erratum stub into the erratum stub set. The set is ordered by
1236 // (relobj, shndx, sh_offset).
1238 add_erratum_stub(The_erratum_stub* stub);
1240 // Find if such erratum exists for any given (obj, shndx, sh_offset).
1242 find_erratum_stub(The_aarch64_relobj* a64relobj,
1243 unsigned int shndx, unsigned int sh_offset);
1245 // Find all the erratums for a given input section. The return value is a pair
1246 // of iterators [begin, end).
1247 std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
1248 find_erratum_stubs_for_input_section(The_aarch64_relobj* a64relobj,
1249 unsigned int shndx);
1251 // Compute the erratum stub address.
1253 erratum_stub_address(The_erratum_stub* stub) const
1255 AArch64_address r = align_address(this->address() + this->reloc_stubs_size_,
1256 The_erratum_stub::STUB_ADDR_ALIGN);
1257 r += stub->offset();
1261 // Finalize stubs. No-op here, just for completeness.
1266 // Look up a relocation stub using KEY. Return NULL if there is none.
1268 find_reloc_stub(The_reloc_stub_key& key)
1270 Reloc_stub_map_const_iter p = this->reloc_stubs_.find(key);
1271 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
1274 // Relocate stubs in this stub table.
1276 relocate_stubs(const The_relocate_info*,
1277 The_target_aarch64*,
1283 // Update data size at the end of a relaxation pass. Return true if data size
1284 // is different from that of the previous relaxation pass.
1286 update_data_size_changed_p()
1288 // No addralign changed here.
1289 off_t s = align_address(this->reloc_stubs_size_,
1290 The_erratum_stub::STUB_ADDR_ALIGN)
1291 + this->erratum_stubs_size_;
1292 bool changed = (s != this->prev_data_size_);
1293 this->prev_data_size_ = s;
1298 // Write out section contents.
1300 do_write(Output_file*);
1302 // Return the required alignment.
1304 do_addralign() const
1306 return std::max(The_reloc_stub::STUB_ADDR_ALIGN,
1307 The_erratum_stub::STUB_ADDR_ALIGN);
1310 // Reset address and file offset.
1312 do_reset_address_and_file_offset()
1313 { this->set_current_data_size_for_child(this->prev_data_size_); }
1315 // Set final data size.
1317 set_final_data_size()
1318 { this->set_data_size(this->current_data_size()); }
1321 // Relocate one stub.
1323 relocate_stub(The_reloc_stub*,
1324 const The_relocate_info*,
1325 The_target_aarch64*,
1332 // Owner of this stub table.
1333 The_aarch64_input_section* owner_;
1334 // The relocation stubs.
1335 Reloc_stub_map reloc_stubs_;
1336 // The erratum stubs.
1337 Erratum_stub_set erratum_stubs_;
1338 // Size of reloc stubs.
1339 off_t reloc_stubs_size_;
1340 // Size of erratum stubs.
1341 off_t erratum_stubs_size_;
1342 // data size of this in the previous pass.
1343 off_t prev_data_size_;
1344 }; // End of Stub_table
1347 // Add an erratum stub into the erratum stub set. The set is ordered by
1348 // (relobj, shndx, sh_offset).
1350 template<int size, bool big_endian>
1352 Stub_table<size, big_endian>::add_erratum_stub(The_erratum_stub* stub)
1354 std::pair<Erratum_stub_set_iter, bool> ret =
1355 this->erratum_stubs_.insert(stub);
1356 gold_assert(ret.second);
1357 this->erratum_stubs_size_ = align_address(
1358 this->erratum_stubs_size_, The_erratum_stub::STUB_ADDR_ALIGN);
1359 stub->set_offset(this->erratum_stubs_size_);
1360 this->erratum_stubs_size_ += stub->stub_size();
1364 // Find if such erratum exists for givein (obj, shndx, sh_offset).
1366 template<int size, bool big_endian>
1367 Erratum_stub<size, big_endian>*
1368 Stub_table<size, big_endian>::find_erratum_stub(
1369 The_aarch64_relobj* a64relobj, unsigned int shndx, unsigned int sh_offset)
1371 // A dummy object used as key to search in the set.
1372 The_erratum_stub key(a64relobj, ST_NONE,
1374 Erratum_stub_set_iter i = this->erratum_stubs_.find(&key);
1375 if (i != this->erratum_stubs_.end())
1377 The_erratum_stub* stub(*i);
1378 gold_assert(stub->erratum_insn() != 0);
1385 // Find all the errata for a given input section. The return value is a pair of
1386 // iterators [begin, end).
1388 template<int size, bool big_endian>
1389 std::pair<typename Stub_table<size, big_endian>::Erratum_stub_set_iter,
1390 typename Stub_table<size, big_endian>::Erratum_stub_set_iter>
1391 Stub_table<size, big_endian>::find_erratum_stubs_for_input_section(
1392 The_aarch64_relobj* a64relobj, unsigned int shndx)
1394 typedef std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter> Result_pair;
1395 Erratum_stub_set_iter start, end;
1396 The_erratum_stub low_key(a64relobj, ST_NONE, shndx, 0);
1397 start = this->erratum_stubs_.lower_bound(&low_key);
1398 if (start == this->erratum_stubs_.end())
1399 return Result_pair(this->erratum_stubs_.end(),
1400 this->erratum_stubs_.end());
1402 while (end != this->erratum_stubs_.end() &&
1403 (*end)->relobj() == a64relobj && (*end)->shndx() == shndx)
1405 return Result_pair(start, end);
1409 // Add a STUB using KEY. The caller is responsible for avoiding addition
1410 // if a STUB with the same key has already been added.
1412 template<int size, bool big_endian>
1414 Stub_table<size, big_endian>::add_reloc_stub(
1415 The_reloc_stub* stub, const The_reloc_stub_key& key)
1417 gold_assert(stub->type() == key.type());
1418 this->reloc_stubs_[key] = stub;
1420 // Assign stub offset early. We can do this because we never remove
1421 // reloc stubs and they are in the beginning of the stub table.
1422 this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_,
1423 The_reloc_stub::STUB_ADDR_ALIGN);
1424 stub->set_offset(this->reloc_stubs_size_);
1425 this->reloc_stubs_size_ += stub->stub_size();
1429 // Relocate all stubs in this stub table.
1431 template<int size, bool big_endian>
1433 Stub_table<size, big_endian>::
1434 relocate_stubs(const The_relocate_info* relinfo,
1435 The_target_aarch64* target_aarch64,
1436 Output_section* output_section,
1437 unsigned char* view,
1438 AArch64_address address,
1439 section_size_type view_size)
1441 // "view_size" is the total size of the stub_table.
1442 gold_assert(address == this->address() &&
1443 view_size == static_cast<section_size_type>(this->data_size()));
1444 for(Reloc_stub_map_const_iter p = this->reloc_stubs_.begin();
1445 p != this->reloc_stubs_.end(); ++p)
1446 relocate_stub(p->second, relinfo, target_aarch64, output_section,
1447 view, address, view_size);
1449 // Just for convenience.
1450 const int BPI = AArch64_insn_utilities<big_endian>::BYTES_PER_INSN;
1452 // Now 'relocate' erratum stubs.
1453 for(Erratum_stub_set_iter i = this->erratum_stubs_.begin();
1454 i != this->erratum_stubs_.end(); ++i)
1456 AArch64_address stub_address = this->erratum_stub_address(*i);
1457 // The address of "b" in the stub that is to be "relocated".
1458 AArch64_address stub_b_insn_address;
1459 // Branch offset that is to be filled in "b" insn.
1461 switch ((*i)->type())
1464 // For the erratum, the 2nd insn is a b-insn to be patched
1466 stub_b_insn_address = stub_address + 1 * BPI;
1467 b_offset = (*i)->destination_address() - stub_b_insn_address;
1468 AArch64_relocate_functions<size, big_endian>::construct_b(
1469 view + (stub_b_insn_address - this->address()),
1470 ((unsigned int)(b_offset)) & 0xfffffff);
1480 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
1482 template<int size, bool big_endian>
1484 Stub_table<size, big_endian>::
1485 relocate_stub(The_reloc_stub* stub,
1486 const The_relocate_info* relinfo,
1487 The_target_aarch64* target_aarch64,
1488 Output_section* output_section,
1489 unsigned char* view,
1490 AArch64_address address,
1491 section_size_type view_size)
1493 // "offset" is the offset from the beginning of the stub_table.
1494 section_size_type offset = stub->offset();
1495 section_size_type stub_size = stub->stub_size();
1496 // "view_size" is the total size of the stub_table.
1497 gold_assert(offset + stub_size <= view_size);
1499 target_aarch64->relocate_stub(stub, relinfo, output_section,
1500 view + offset, address + offset, view_size);
1504 // Write out the stubs to file.
1506 template<int size, bool big_endian>
1508 Stub_table<size, big_endian>::do_write(Output_file* of)
1510 off_t offset = this->offset();
1511 const section_size_type oview_size =
1512 convert_to_section_size_type(this->data_size());
1513 unsigned char* const oview = of->get_output_view(offset, oview_size);
1515 // Write relocation stubs.
1516 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
1517 p != this->reloc_stubs_.end(); ++p)
1519 The_reloc_stub* stub = p->second;
1520 AArch64_address address = this->address() + stub->offset();
1521 gold_assert(address ==
1522 align_address(address, The_reloc_stub::STUB_ADDR_ALIGN));
1523 stub->write(oview + stub->offset(), stub->stub_size());
1526 // Write erratum stubs.
1527 unsigned int erratum_stub_start_offset =
1528 align_address(this->reloc_stubs_size_, The_erratum_stub::STUB_ADDR_ALIGN);
1529 for (typename Erratum_stub_set::iterator p = this->erratum_stubs_.begin();
1530 p != this->erratum_stubs_.end(); ++p)
1532 The_erratum_stub* stub(*p);
1533 stub->write(oview + erratum_stub_start_offset + stub->offset(),
1537 of->write_output_view(this->offset(), oview_size, oview);
1541 // AArch64_relobj class.
1543 template<int size, bool big_endian>
1544 class AArch64_relobj : public Sized_relobj_file<size, big_endian>
1547 typedef AArch64_relobj<size, big_endian> This;
1548 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1549 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1550 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
1551 typedef Stub_table<size, big_endian> The_stub_table;
1552 typedef Erratum_stub<size, big_endian> The_erratum_stub;
1553 typedef typename The_stub_table::Erratum_stub_set_iter Erratum_stub_set_iter;
1554 typedef std::vector<The_stub_table*> Stub_table_list;
1555 static const AArch64_address invalid_address =
1556 static_cast<AArch64_address>(-1);
1558 AArch64_relobj(const std::string& name, Input_file* input_file, off_t offset,
1559 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1560 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1567 // Return the stub table of the SHNDX-th section if there is one.
1569 stub_table(unsigned int shndx) const
1571 gold_assert(shndx < this->stub_tables_.size());
1572 return this->stub_tables_[shndx];
1575 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1577 set_stub_table(unsigned int shndx, The_stub_table* stub_table)
1579 gold_assert(shndx < this->stub_tables_.size());
1580 this->stub_tables_[shndx] = stub_table;
1583 // Entrance to erratum_843419 scanning.
1585 scan_erratum_843419(unsigned int shndx,
1586 const elfcpp::Shdr<size, big_endian>&,
1587 Output_section*, const Symbol_table*,
1588 The_target_aarch64*);
1590 // Scan all relocation sections for stub generation.
1592 scan_sections_for_stubs(The_target_aarch64*, const Symbol_table*,
1595 // Whether a section is a scannable text section.
1597 text_section_is_scannable(const elfcpp::Shdr<size, big_endian>&, unsigned int,
1598 const Output_section*, const Symbol_table*);
1600 // Convert regular input section with index SHNDX to a relaxed section.
1602 convert_input_section_to_relaxed_section(unsigned /* shndx */)
1604 // The stubs have relocations and we need to process them after writing
1605 // out the stubs. So relocation now must follow section write.
1606 this->set_relocs_must_follow_section_writes();
1609 // Structure for mapping symbol position.
1610 struct Mapping_symbol_position
1612 Mapping_symbol_position(unsigned int shndx, AArch64_address offset):
1613 shndx_(shndx), offset_(offset)
1616 // "<" comparator used in ordered_map container.
1618 operator<(const Mapping_symbol_position& p) const
1620 return (this->shndx_ < p.shndx_
1621 || (this->shndx_ == p.shndx_ && this->offset_ < p.offset_));
1625 unsigned int shndx_;
1628 AArch64_address offset_;
1631 typedef std::map<Mapping_symbol_position, char> Mapping_symbol_info;
1634 // Post constructor setup.
1638 // Call parent's setup method.
1639 Sized_relobj_file<size, big_endian>::do_setup();
1641 // Initialize look-up tables.
1642 this->stub_tables_.resize(this->shnum());
1646 do_relocate_sections(
1647 const Symbol_table* symtab, const Layout* layout,
1648 const unsigned char* pshdrs, Output_file* of,
1649 typename Sized_relobj_file<size, big_endian>::Views* pviews);
1651 // Count local symbols and (optionally) record mapping info.
1653 do_count_local_symbols(Stringpool_template<char>*,
1654 Stringpool_template<char>*);
1657 // Fix all errata in the object.
1659 fix_errata(typename Sized_relobj_file<size, big_endian>::Views* pviews);
1661 // Whether a section needs to be scanned for relocation stubs.
1663 section_needs_reloc_stub_scanning(const elfcpp::Shdr<size, big_endian>&,
1664 const Relobj::Output_sections&,
1665 const Symbol_table*, const unsigned char*);
1667 // List of stub tables.
1668 Stub_table_list stub_tables_;
1670 // Mapping symbol information sorted by (section index, section_offset).
1671 Mapping_symbol_info mapping_symbol_info_;
1672 }; // End of AArch64_relobj
1675 // Override to record mapping symbol information.
1676 template<int size, bool big_endian>
1678 AArch64_relobj<size, big_endian>::do_count_local_symbols(
1679 Stringpool_template<char>* pool, Stringpool_template<char>* dynpool)
1681 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
1683 // Only erratum-fixing work needs mapping symbols, so skip this time consuming
1684 // processing if not fixing erratum.
1685 if (!parameters->options().fix_cortex_a53_843419())
1688 const unsigned int loccount = this->local_symbol_count();
1692 // Read the symbol table section header.
1693 const unsigned int symtab_shndx = this->symtab_shndx();
1694 elfcpp::Shdr<size, big_endian>
1695 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
1696 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1698 // Read the local symbols.
1699 const int sym_size =elfcpp::Elf_sizes<size>::sym_size;
1700 gold_assert(loccount == symtabshdr.get_sh_info());
1701 off_t locsize = loccount * sym_size;
1702 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1703 locsize, true, true);
1705 // For mapping symbol processing, we need to read the symbol names.
1706 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
1707 if (strtab_shndx >= this->shnum())
1709 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
1713 elfcpp::Shdr<size, big_endian>
1714 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
1715 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
1717 this->error(_("symbol table name section has wrong type: %u"),
1718 static_cast<unsigned int>(strtabshdr.get_sh_type()));
1722 const char* pnames =
1723 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
1724 strtabshdr.get_sh_size(),
1727 // Skip the first dummy symbol.
1729 typename Sized_relobj_file<size, big_endian>::Local_values*
1730 plocal_values = this->local_values();
1731 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1733 elfcpp::Sym<size, big_endian> sym(psyms);
1734 Symbol_value<size>& lv((*plocal_values)[i]);
1735 AArch64_address input_value = lv.input_value();
1737 // Check to see if this is a mapping symbol.
1738 const char* sym_name = pnames + sym.get_st_name();
1739 if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd')
1740 && sym_name[2] == '\0')
1743 unsigned int input_shndx =
1744 this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
1745 gold_assert(is_ordinary);
1747 Mapping_symbol_position msp(input_shndx, input_value);
1748 // Insert mapping_symbol_info into map whose ordering is defined by
1749 // (shndx, offset_within_section).
1750 this->mapping_symbol_info_[msp] = sym_name[1];
1756 // Fix all errata in the object.
1758 template<int size, bool big_endian>
1760 AArch64_relobj<size, big_endian>::fix_errata(
1761 typename Sized_relobj_file<size, big_endian>::Views* pviews)
1763 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
1764 unsigned int shnum = this->shnum();
1765 for (unsigned int i = 1; i < shnum; ++i)
1767 The_stub_table* stub_table = this->stub_table(i);
1770 std::pair<Erratum_stub_set_iter, Erratum_stub_set_iter>
1771 ipair(stub_table->find_erratum_stubs_for_input_section(this, i));
1772 Erratum_stub_set_iter p = ipair.first, end = ipair.second;
1775 The_erratum_stub* stub = *p;
1776 typename Sized_relobj_file<size, big_endian>::View_size&
1777 pview((*pviews)[i]);
1779 // Double check data before fix.
1781 reinterpret_cast<Insntype*>(pview.view + stub->sh_offset());
1782 Insntype insn_to_fix = ip[0];
1783 gold_assert(insn_to_fix == stub->erratum_insn());
1784 gold_assert(pview.address + stub->sh_offset()
1785 == stub->erratum_address());
1787 AArch64_address stub_address =
1788 stub_table->erratum_stub_address(stub);
1789 unsigned int b_offset = stub_address - stub->erratum_address();
1790 AArch64_relocate_functions<size, big_endian>::construct_b(
1791 pview.view + stub->sh_offset(), b_offset & 0xfffffff);
1798 // Relocate sections.
1800 template<int size, bool big_endian>
1802 AArch64_relobj<size, big_endian>::do_relocate_sections(
1803 const Symbol_table* symtab, const Layout* layout,
1804 const unsigned char* pshdrs, Output_file* of,
1805 typename Sized_relobj_file<size, big_endian>::Views* pviews)
1807 // Call parent to relocate sections.
1808 Sized_relobj_file<size, big_endian>::do_relocate_sections(symtab, layout,
1809 pshdrs, of, pviews);
1811 // We do not generate stubs if doing a relocatable link.
1812 if (parameters->options().relocatable())
1815 if (parameters->options().fix_cortex_a53_843419())
1816 this->fix_errata(pviews);
1818 Relocate_info<size, big_endian> relinfo;
1819 relinfo.symtab = symtab;
1820 relinfo.layout = layout;
1821 relinfo.object = this;
1823 // Relocate stub tables.
1824 unsigned int shnum = this->shnum();
1825 The_target_aarch64* target = The_target_aarch64::current_target();
1827 for (unsigned int i = 1; i < shnum; ++i)
1829 The_aarch64_input_section* aarch64_input_section =
1830 target->find_aarch64_input_section(this, i);
1831 if (aarch64_input_section != NULL
1832 && aarch64_input_section->is_stub_table_owner()
1833 && !aarch64_input_section->stub_table()->empty())
1835 Output_section* os = this->output_section(i);
1836 gold_assert(os != NULL);
1838 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
1839 relinfo.reloc_shdr = NULL;
1840 relinfo.data_shndx = i;
1841 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<size>::shdr_size;
1843 typename Sized_relobj_file<size, big_endian>::View_size&
1844 view_struct = (*pviews)[i];
1845 gold_assert(view_struct.view != NULL);
1847 The_stub_table* stub_table = aarch64_input_section->stub_table();
1848 off_t offset = stub_table->address() - view_struct.address;
1849 unsigned char* view = view_struct.view + offset;
1850 AArch64_address address = stub_table->address();
1851 section_size_type view_size = stub_table->data_size();
1852 stub_table->relocate_stubs(&relinfo, target, os, view, address,
1859 // Determine if an input section is scannable for stub processing. SHDR is
1860 // the header of the section and SHNDX is the section index. OS is the output
1861 // section for the input section and SYMTAB is the global symbol table used to
1862 // look up ICF information.
1864 template<int size, bool big_endian>
1866 AArch64_relobj<size, big_endian>::text_section_is_scannable(
1867 const elfcpp::Shdr<size, big_endian>& text_shdr,
1868 unsigned int text_shndx,
1869 const Output_section* os,
1870 const Symbol_table* symtab)
1872 // Skip any empty sections, unallocated sections or sections whose
1873 // type are not SHT_PROGBITS.
1874 if (text_shdr.get_sh_size() == 0
1875 || (text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
1876 || text_shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
1879 // Skip any discarded or ICF'ed sections.
1880 if (os == NULL || symtab->is_section_folded(this, text_shndx))
1883 // Skip exception frame.
1884 if (strcmp(os->name(), ".eh_frame") == 0)
1887 gold_assert(!this->is_output_section_offset_invalid(text_shndx) ||
1888 os->find_relaxed_input_section(this, text_shndx) != NULL);
1894 // Determine if we want to scan the SHNDX-th section for relocation stubs.
1895 // This is a helper for AArch64_relobj::scan_sections_for_stubs().
1897 template<int size, bool big_endian>
1899 AArch64_relobj<size, big_endian>::section_needs_reloc_stub_scanning(
1900 const elfcpp::Shdr<size, big_endian>& shdr,
1901 const Relobj::Output_sections& out_sections,
1902 const Symbol_table* symtab,
1903 const unsigned char* pshdrs)
1905 unsigned int sh_type = shdr.get_sh_type();
1906 if (sh_type != elfcpp::SHT_RELA)
1909 // Ignore empty section.
1910 off_t sh_size = shdr.get_sh_size();
1914 // Ignore reloc section with unexpected symbol table. The
1915 // error will be reported in the final link.
1916 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
1919 gold_assert(sh_type == elfcpp::SHT_RELA);
1920 unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
1922 // Ignore reloc section with unexpected entsize or uneven size.
1923 // The error will be reported in the final link.
1924 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
1927 // Ignore reloc section with bad info. This error will be
1928 // reported in the final link.
1929 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_info());
1930 if (text_shndx >= this->shnum())
1933 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1934 const elfcpp::Shdr<size, big_endian> text_shdr(pshdrs +
1935 text_shndx * shdr_size);
1936 return this->text_section_is_scannable(text_shdr, text_shndx,
1937 out_sections[text_shndx], symtab);
1941 // Scan section SHNDX for erratum 843419.
1943 template<int size, bool big_endian>
1945 AArch64_relobj<size, big_endian>::scan_erratum_843419(
1946 unsigned int shndx, const elfcpp::Shdr<size, big_endian>& shdr,
1947 Output_section* os, const Symbol_table* symtab,
1948 The_target_aarch64* target)
1950 if (shdr.get_sh_size() == 0
1951 || (shdr.get_sh_flags() &
1952 (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) == 0
1953 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
1956 if (!os || symtab->is_section_folded(this, shndx)) return;
1958 AArch64_address output_offset = this->get_output_section_offset(shndx);
1959 AArch64_address output_address;
1960 if (output_offset != invalid_address)
1961 output_address = os->address() + output_offset;
1964 const Output_relaxed_input_section* poris =
1965 os->find_relaxed_input_section(this, shndx);
1967 output_address = poris->address();
1970 section_size_type input_view_size = 0;
1971 const unsigned char* input_view =
1972 this->section_contents(shndx, &input_view_size, false);
1974 Mapping_symbol_position section_start(shndx, 0);
1975 // Find the first mapping symbol record within section shndx.
1976 typename Mapping_symbol_info::const_iterator p =
1977 this->mapping_symbol_info_.lower_bound(section_start);
1978 if (p == this->mapping_symbol_info_.end() || p->first.shndx_ != shndx)
1979 gold_warning(_("cannot scan executable section %u of %s for Cortex-A53 "
1980 "erratum because it has no mapping symbols."),
1981 shndx, this->name().c_str());
1982 while (p != this->mapping_symbol_info_.end() &&
1983 p->first.shndx_ == shndx)
1985 typename Mapping_symbol_info::const_iterator prev = p;
1987 if (prev->second == 'x')
1989 section_size_type span_start =
1990 convert_to_section_size_type(prev->first.offset_);
1991 section_size_type span_end;
1992 if (p != this->mapping_symbol_info_.end()
1993 && p->first.shndx_ == shndx)
1994 span_end = convert_to_section_size_type(p->first.offset_);
1996 span_end = convert_to_section_size_type(shdr.get_sh_size());
1997 target->scan_erratum_843419_span(
1998 this, shndx, span_start, span_end,
1999 const_cast<unsigned char*>(input_view), output_address);
2005 // Scan relocations for stub generation.
2007 template<int size, bool big_endian>
2009 AArch64_relobj<size, big_endian>::scan_sections_for_stubs(
2010 The_target_aarch64* target,
2011 const Symbol_table* symtab,
2012 const Layout* layout)
2014 unsigned int shnum = this->shnum();
2015 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2017 // Read the section headers.
2018 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
2022 // To speed up processing, we set up hash tables for fast lookup of
2023 // input offsets to output addresses.
2024 this->initialize_input_to_output_maps();
2026 const Relobj::Output_sections& out_sections(this->output_sections());
2028 Relocate_info<size, big_endian> relinfo;
2029 relinfo.symtab = symtab;
2030 relinfo.layout = layout;
2031 relinfo.object = this;
2033 // Do relocation stubs scanning.
2034 const unsigned char* p = pshdrs + shdr_size;
2035 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
2037 const elfcpp::Shdr<size, big_endian> shdr(p);
2038 if (parameters->options().fix_cortex_a53_843419())
2039 scan_erratum_843419(i, shdr, out_sections[i], symtab, target);
2040 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
2043 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
2044 AArch64_address output_offset =
2045 this->get_output_section_offset(index);
2046 AArch64_address output_address;
2047 if (output_offset != invalid_address)
2049 output_address = out_sections[index]->address() + output_offset;
2053 // Currently this only happens for a relaxed section.
2054 const Output_relaxed_input_section* poris =
2055 out_sections[index]->find_relaxed_input_section(this, index);
2056 gold_assert(poris != NULL);
2057 output_address = poris->address();
2060 // Get the relocations.
2061 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
2065 // Get the section contents.
2066 section_size_type input_view_size = 0;
2067 const unsigned char* input_view =
2068 this->section_contents(index, &input_view_size, false);
2070 relinfo.reloc_shndx = i;
2071 relinfo.data_shndx = index;
2072 unsigned int sh_type = shdr.get_sh_type();
2073 unsigned int reloc_size;
2074 gold_assert (sh_type == elfcpp::SHT_RELA);
2075 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2077 Output_section* os = out_sections[index];
2078 target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
2079 shdr.get_sh_size() / reloc_size,
2081 output_offset == invalid_address,
2082 input_view, output_address,
2089 // A class to wrap an ordinary input section containing executable code.
2091 template<int size, bool big_endian>
2092 class AArch64_input_section : public Output_relaxed_input_section
2095 typedef Stub_table<size, big_endian> The_stub_table;
2097 AArch64_input_section(Relobj* relobj, unsigned int shndx)
2098 : Output_relaxed_input_section(relobj, shndx, 1),
2100 original_contents_(NULL), original_size_(0),
2101 original_addralign_(1)
2104 ~AArch64_input_section()
2105 { delete[] this->original_contents_; }
2111 // Set the stub_table.
2113 set_stub_table(The_stub_table* st)
2114 { this->stub_table_ = st; }
2116 // Whether this is a stub table owner.
2118 is_stub_table_owner() const
2119 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
2121 // Return the original size of the section.
2123 original_size() const
2124 { return this->original_size_; }
2126 // Return the stub table.
2129 { return stub_table_; }
2132 // Write out this input section.
2134 do_write(Output_file*);
2136 // Return required alignment of this.
2138 do_addralign() const
2140 if (this->is_stub_table_owner())
2141 return std::max(this->stub_table_->addralign(),
2142 static_cast<uint64_t>(this->original_addralign_));
2144 return this->original_addralign_;
2147 // Finalize data size.
2149 set_final_data_size();
2151 // Reset address and file offset.
2153 do_reset_address_and_file_offset();
2157 do_output_offset(const Relobj* object, unsigned int shndx,
2158 section_offset_type offset,
2159 section_offset_type* poutput) const
2161 if ((object == this->relobj())
2162 && (shndx == this->shndx())
2165 convert_types<section_offset_type, uint32_t>(this->original_size_)))
2175 // Copying is not allowed.
2176 AArch64_input_section(const AArch64_input_section&);
2177 AArch64_input_section& operator=(const AArch64_input_section&);
2179 // The relocation stubs.
2180 The_stub_table* stub_table_;
2181 // Original section contents. We have to make a copy here since the file
2182 // containing the original section may not be locked when we need to access
2184 unsigned char* original_contents_;
2185 // Section size of the original input section.
2186 uint32_t original_size_;
2187 // Address alignment of the original input section.
2188 uint32_t original_addralign_;
2189 }; // End of AArch64_input_section
2192 // Finalize data size.
2194 template<int size, bool big_endian>
2196 AArch64_input_section<size, big_endian>::set_final_data_size()
2198 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2200 if (this->is_stub_table_owner())
2202 this->stub_table_->finalize_data_size();
2203 off = align_address(off, this->stub_table_->addralign());
2204 off += this->stub_table_->data_size();
2206 this->set_data_size(off);
2210 // Reset address and file offset.
2212 template<int size, bool big_endian>
2214 AArch64_input_section<size, big_endian>::do_reset_address_and_file_offset()
2216 // Size of the original input section contents.
2217 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
2219 // If this is a stub table owner, account for the stub table size.
2220 if (this->is_stub_table_owner())
2222 The_stub_table* stub_table = this->stub_table_;
2224 // Reset the stub table's address and file offset. The
2225 // current data size for child will be updated after that.
2226 stub_table_->reset_address_and_file_offset();
2227 off = align_address(off, stub_table_->addralign());
2228 off += stub_table->current_data_size();
2231 this->set_current_data_size(off);
2235 // Initialize an Arm_input_section.
2237 template<int size, bool big_endian>
2239 AArch64_input_section<size, big_endian>::init()
2241 Relobj* relobj = this->relobj();
2242 unsigned int shndx = this->shndx();
2244 // We have to cache original size, alignment and contents to avoid locking
2245 // the original file.
2246 this->original_addralign_ =
2247 convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
2249 // This is not efficient but we expect only a small number of relaxed
2250 // input sections for stubs.
2251 section_size_type section_size;
2252 const unsigned char* section_contents =
2253 relobj->section_contents(shndx, §ion_size, false);
2254 this->original_size_ =
2255 convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
2257 gold_assert(this->original_contents_ == NULL);
2258 this->original_contents_ = new unsigned char[section_size];
2259 memcpy(this->original_contents_, section_contents, section_size);
2261 // We want to make this look like the original input section after
2262 // output sections are finalized.
2263 Output_section* os = relobj->output_section(shndx);
2264 off_t offset = relobj->output_section_offset(shndx);
2265 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
2266 this->set_address(os->address() + offset);
2267 this->set_file_offset(os->offset() + offset);
2268 this->set_current_data_size(this->original_size_);
2269 this->finalize_data_size();
2273 // Write data to output file.
2275 template<int size, bool big_endian>
2277 AArch64_input_section<size, big_endian>::do_write(Output_file* of)
2279 // We have to write out the original section content.
2280 gold_assert(this->original_contents_ != NULL);
2281 of->write(this->offset(), this->original_contents_,
2282 this->original_size_);
2284 // If this owns a stub table and it is not empty, write it.
2285 if (this->is_stub_table_owner() && !this->stub_table_->empty())
2286 this->stub_table_->write(of);
2290 // Arm output section class. This is defined mainly to add a number of stub
2291 // generation methods.
2293 template<int size, bool big_endian>
2294 class AArch64_output_section : public Output_section
2297 typedef Target_aarch64<size, big_endian> The_target_aarch64;
2298 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2299 typedef Stub_table<size, big_endian> The_stub_table;
2300 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2303 AArch64_output_section(const char* name, elfcpp::Elf_Word type,
2304 elfcpp::Elf_Xword flags)
2305 : Output_section(name, type, flags)
2308 ~AArch64_output_section() {}
2310 // Group input sections for stub generation.
2312 group_sections(section_size_type, bool, Target_aarch64<size, big_endian>*,
2316 typedef Output_section::Input_section Input_section;
2317 typedef Output_section::Input_section_list Input_section_list;
2319 // Create a stub group.
2321 create_stub_group(Input_section_list::const_iterator,
2322 Input_section_list::const_iterator,
2323 Input_section_list::const_iterator,
2324 The_target_aarch64*,
2325 std::vector<Output_relaxed_input_section*>&,
2327 }; // End of AArch64_output_section
2330 // Create a stub group for input sections from FIRST to LAST. OWNER points to
2331 // the input section that will be the owner of the stub table.
2333 template<int size, bool big_endian> void
2334 AArch64_output_section<size, big_endian>::create_stub_group(
2335 Input_section_list::const_iterator first,
2336 Input_section_list::const_iterator last,
2337 Input_section_list::const_iterator owner,
2338 The_target_aarch64* target,
2339 std::vector<Output_relaxed_input_section*>& new_relaxed_sections,
2342 // Currently we convert ordinary input sections into relaxed sections only
2344 The_aarch64_input_section* input_section;
2345 if (owner->is_relaxed_input_section())
2349 gold_assert(owner->is_input_section());
2350 // Create a new relaxed input section. We need to lock the original
2352 Task_lock_obj<Object> tl(task, owner->relobj());
2354 target->new_aarch64_input_section(owner->relobj(), owner->shndx());
2355 new_relaxed_sections.push_back(input_section);
2358 // Create a stub table.
2359 The_stub_table* stub_table =
2360 target->new_stub_table(input_section);
2362 input_section->set_stub_table(stub_table);
2364 Input_section_list::const_iterator p = first;
2365 // Look for input sections or relaxed input sections in [first ... last].
2368 if (p->is_input_section() || p->is_relaxed_input_section())
2370 // The stub table information for input sections live
2371 // in their objects.
2372 The_aarch64_relobj* aarch64_relobj =
2373 static_cast<The_aarch64_relobj*>(p->relobj());
2374 aarch64_relobj->set_stub_table(p->shndx(), stub_table);
2377 while (p++ != last);
2381 // Group input sections for stub generation. GROUP_SIZE is roughly the limit of
2382 // stub groups. We grow a stub group by adding input section until the size is
2383 // just below GROUP_SIZE. The last input section will be converted into a stub
2384 // table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
2385 // after the stub table, effectively doubling the group size.
2387 // This is similar to the group_sections() function in elf32-arm.c but is
2388 // implemented differently.
2390 template<int size, bool big_endian>
2391 void AArch64_output_section<size, big_endian>::group_sections(
2392 section_size_type group_size,
2393 bool stubs_always_after_branch,
2394 Target_aarch64<size, big_endian>* target,
2400 FINDING_STUB_SECTION,
2404 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
2406 State state = NO_GROUP;
2407 section_size_type off = 0;
2408 section_size_type group_begin_offset = 0;
2409 section_size_type group_end_offset = 0;
2410 section_size_type stub_table_end_offset = 0;
2411 Input_section_list::const_iterator group_begin =
2412 this->input_sections().end();
2413 Input_section_list::const_iterator stub_table =
2414 this->input_sections().end();
2415 Input_section_list::const_iterator group_end = this->input_sections().end();
2416 for (Input_section_list::const_iterator p = this->input_sections().begin();
2417 p != this->input_sections().end();
2420 section_size_type section_begin_offset =
2421 align_address(off, p->addralign());
2422 section_size_type section_end_offset =
2423 section_begin_offset + p->data_size();
2425 // Check to see if we should group the previously seen sections.
2431 case FINDING_STUB_SECTION:
2432 // Adding this section makes the group larger than GROUP_SIZE.
2433 if (section_end_offset - group_begin_offset >= group_size)
2435 if (stubs_always_after_branch)
2437 gold_assert(group_end != this->input_sections().end());
2438 this->create_stub_group(group_begin, group_end, group_end,
2439 target, new_relaxed_sections,
2445 // Input sections up to stub_group_size bytes after the stub
2446 // table can be handled by it too.
2447 state = HAS_STUB_SECTION;
2448 stub_table = group_end;
2449 stub_table_end_offset = group_end_offset;
2454 case HAS_STUB_SECTION:
2455 // Adding this section makes the post stub-section group larger
2458 // NOT SUPPORTED YET. For completeness only.
2459 if (section_end_offset - stub_table_end_offset >= group_size)
2461 gold_assert(group_end != this->input_sections().end());
2462 this->create_stub_group(group_begin, group_end, stub_table,
2463 target, new_relaxed_sections, task);
2472 // If we see an input section and currently there is no group, start
2473 // a new one. Skip any empty sections. We look at the data size
2474 // instead of calling p->relobj()->section_size() to avoid locking.
2475 if ((p->is_input_section() || p->is_relaxed_input_section())
2476 && (p->data_size() != 0))
2478 if (state == NO_GROUP)
2480 state = FINDING_STUB_SECTION;
2482 group_begin_offset = section_begin_offset;
2485 // Keep track of the last input section seen.
2487 group_end_offset = section_end_offset;
2490 off = section_end_offset;
2493 // Create a stub group for any ungrouped sections.
2494 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
2496 gold_assert(group_end != this->input_sections().end());
2497 this->create_stub_group(group_begin, group_end,
2498 (state == FINDING_STUB_SECTION
2501 target, new_relaxed_sections, task);
2504 if (!new_relaxed_sections.empty())
2505 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
2507 // Update the section offsets
2508 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
2510 The_aarch64_relobj* relobj = static_cast<The_aarch64_relobj*>(
2511 new_relaxed_sections[i]->relobj());
2512 unsigned int shndx = new_relaxed_sections[i]->shndx();
2513 // Tell AArch64_relobj that this input section is converted.
2514 relobj->convert_input_section_to_relaxed_section(shndx);
2516 } // End of AArch64_output_section::group_sections
2519 AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
2522 // The aarch64 target class.
2524 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
2525 template<int size, bool big_endian>
2526 class Target_aarch64 : public Sized_target<size, big_endian>
2529 typedef Target_aarch64<size, big_endian> This;
2530 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
2532 typedef Relocate_info<size, big_endian> The_relocate_info;
2533 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2534 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2535 typedef Reloc_stub<size, big_endian> The_reloc_stub;
2536 typedef Erratum_stub<size, big_endian> The_erratum_stub;
2537 typedef typename Reloc_stub<size, big_endian>::Key The_reloc_stub_key;
2538 typedef Stub_table<size, big_endian> The_stub_table;
2539 typedef std::vector<The_stub_table*> Stub_table_list;
2540 typedef typename Stub_table_list::iterator Stub_table_iterator;
2541 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2542 typedef AArch64_output_section<size, big_endian> The_aarch64_output_section;
2543 typedef Unordered_map<Section_id,
2544 AArch64_input_section<size, big_endian>*,
2545 Section_id_hash> AArch64_input_section_map;
2546 typedef AArch64_insn_utilities<big_endian> Insn_utilities;
2547 const static int TCB_SIZE = size / 8 * 2;
2549 Target_aarch64(const Target::Target_info* info = &aarch64_info)
2550 : Sized_target<size, big_endian>(info),
2551 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
2552 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
2553 rela_irelative_(NULL), copy_relocs_(elfcpp::R_AARCH64_COPY),
2554 got_mod_index_offset_(-1U),
2555 tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
2556 stub_tables_(), stub_group_size_(0), aarch64_input_section_map_()
2559 // Scan the relocations to determine unreferenced sections for
2560 // garbage collection.
2562 gc_process_relocs(Symbol_table* symtab,
2564 Sized_relobj_file<size, big_endian>* object,
2565 unsigned int data_shndx,
2566 unsigned int sh_type,
2567 const unsigned char* prelocs,
2569 Output_section* output_section,
2570 bool needs_special_offset_handling,
2571 size_t local_symbol_count,
2572 const unsigned char* plocal_symbols);
2574 // Scan the relocations to look for symbol adjustments.
2576 scan_relocs(Symbol_table* symtab,
2578 Sized_relobj_file<size, big_endian>* object,
2579 unsigned int data_shndx,
2580 unsigned int sh_type,
2581 const unsigned char* prelocs,
2583 Output_section* output_section,
2584 bool needs_special_offset_handling,
2585 size_t local_symbol_count,
2586 const unsigned char* plocal_symbols);
2588 // Finalize the sections.
2590 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2592 // Return the value to use for a dynamic which requires special
2595 do_dynsym_value(const Symbol*) const;
2597 // Relocate a section.
2599 relocate_section(const Relocate_info<size, big_endian>*,
2600 unsigned int sh_type,
2601 const unsigned char* prelocs,
2603 Output_section* output_section,
2604 bool needs_special_offset_handling,
2605 unsigned char* view,
2606 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2607 section_size_type view_size,
2608 const Reloc_symbol_changes*);
2610 // Scan the relocs during a relocatable link.
2612 scan_relocatable_relocs(Symbol_table* symtab,
2614 Sized_relobj_file<size, big_endian>* object,
2615 unsigned int data_shndx,
2616 unsigned int sh_type,
2617 const unsigned char* prelocs,
2619 Output_section* output_section,
2620 bool needs_special_offset_handling,
2621 size_t local_symbol_count,
2622 const unsigned char* plocal_symbols,
2623 Relocatable_relocs*);
2625 // Relocate a section during a relocatable link.
2628 const Relocate_info<size, big_endian>*,
2629 unsigned int sh_type,
2630 const unsigned char* prelocs,
2632 Output_section* output_section,
2633 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
2634 const Relocatable_relocs*,
2635 unsigned char* view,
2636 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2637 section_size_type view_size,
2638 unsigned char* reloc_view,
2639 section_size_type reloc_view_size);
2641 // Return the symbol index to use for a target specific relocation.
2642 // The only target specific relocation is R_AARCH64_TLSDESC for a
2643 // local symbol, which is an absolute reloc.
2645 do_reloc_symbol_index(void*, unsigned int r_type) const
2647 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
2651 // Return the addend to use for a target specific relocation.
2653 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
2655 // Return the PLT section.
2657 do_plt_address_for_global(const Symbol* gsym) const
2658 { return this->plt_section()->address_for_global(gsym); }
2661 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
2662 { return this->plt_section()->address_for_local(relobj, symndx); }
2664 // This function should be defined in targets that can use relocation
2665 // types to determine (implemented in local_reloc_may_be_function_pointer
2666 // and global_reloc_may_be_function_pointer)
2667 // if a function's pointer is taken. ICF uses this in safe mode to only
2668 // fold those functions whose pointer is defintely not taken.
2670 do_can_check_for_function_pointers() const
2673 // Return the number of entries in the PLT.
2675 plt_entry_count() const;
2677 //Return the offset of the first non-reserved PLT entry.
2679 first_plt_entry_offset() const;
2681 // Return the size of each PLT entry.
2683 plt_entry_size() const;
2685 // Create a stub table.
2687 new_stub_table(The_aarch64_input_section*);
2689 // Create an aarch64 input section.
2690 The_aarch64_input_section*
2691 new_aarch64_input_section(Relobj*, unsigned int);
2693 // Find an aarch64 input section instance for a given OBJ and SHNDX.
2694 The_aarch64_input_section*
2695 find_aarch64_input_section(Relobj*, unsigned int) const;
2697 // Return the thread control block size.
2699 tcb_size() const { return This::TCB_SIZE; }
2701 // Scan a section for stub generation.
2703 scan_section_for_stubs(const Relocate_info<size, big_endian>*, unsigned int,
2704 const unsigned char*, size_t, Output_section*,
2705 bool, const unsigned char*,
2709 // Scan a relocation section for stub.
2710 template<int sh_type>
2712 scan_reloc_section_for_stubs(
2713 const The_relocate_info* relinfo,
2714 const unsigned char* prelocs,
2716 Output_section* output_section,
2717 bool needs_special_offset_handling,
2718 const unsigned char* view,
2719 Address view_address,
2722 // Relocate a single stub.
2724 relocate_stub(The_reloc_stub*, const Relocate_info<size, big_endian>*,
2725 Output_section*, unsigned char*, Address,
2728 // Get the default AArch64 target.
2732 gold_assert(parameters->target().machine_code() == elfcpp::EM_AARCH64
2733 && parameters->target().get_size() == size
2734 && parameters->target().is_big_endian() == big_endian);
2735 return static_cast<This*>(parameters->sized_target<size, big_endian>());
2739 // Scan erratum for a part of a section.
2741 scan_erratum_843419_span(
2742 AArch64_relobj<size, big_endian>*,
2744 const section_size_type,
2745 const section_size_type,
2751 do_select_as_default_target()
2753 gold_assert(aarch64_reloc_property_table == NULL);
2754 aarch64_reloc_property_table = new AArch64_reloc_property_table();
2757 // Add a new reloc argument, returning the index in the vector.
2759 add_tlsdesc_info(Sized_relobj_file<size, big_endian>* object,
2762 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
2763 return this->tlsdesc_reloc_info_.size() - 1;
2766 virtual Output_data_plt_aarch64<size, big_endian>*
2767 do_make_data_plt(Layout* layout,
2768 Output_data_got_aarch64<size, big_endian>* got,
2769 Output_data_space* got_plt,
2770 Output_data_space* got_irelative)
2772 return new Output_data_plt_aarch64_standard<size, big_endian>(
2773 layout, got, got_plt, got_irelative);
2777 // do_make_elf_object to override the same function in the base class.
2779 do_make_elf_object(const std::string&, Input_file*, off_t,
2780 const elfcpp::Ehdr<size, big_endian>&);
2782 Output_data_plt_aarch64<size, big_endian>*
2783 make_data_plt(Layout* layout,
2784 Output_data_got_aarch64<size, big_endian>* got,
2785 Output_data_space* got_plt,
2786 Output_data_space* got_irelative)
2788 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
2791 // We only need to generate stubs, and hence perform relaxation if we are
2792 // not doing relocatable linking.
2794 do_may_relax() const
2795 { return !parameters->options().relocatable(); }
2797 // Relaxation hook. This is where we do stub generation.
2799 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
2802 group_sections(Layout* layout,
2803 section_size_type group_size,
2804 bool stubs_always_after_branch,
2808 scan_reloc_for_stub(const The_relocate_info*, unsigned int,
2809 const Sized_symbol<size>*, unsigned int,
2810 const Symbol_value<size>*,
2811 typename elfcpp::Elf_types<size>::Elf_Swxword,
2814 // Make an output section.
2816 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2817 elfcpp::Elf_Xword flags)
2818 { return new The_aarch64_output_section(name, type, flags); }
2821 // The class which scans relocations.
2826 : issued_non_pic_error_(false)
2830 local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
2831 Sized_relobj_file<size, big_endian>* object,
2832 unsigned int data_shndx,
2833 Output_section* output_section,
2834 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
2835 const elfcpp::Sym<size, big_endian>& lsym,
2839 global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
2840 Sized_relobj_file<size, big_endian>* object,
2841 unsigned int data_shndx,
2842 Output_section* output_section,
2843 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
2847 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
2848 Target_aarch64<size, big_endian>* ,
2849 Sized_relobj_file<size, big_endian>* ,
2852 const elfcpp::Rela<size, big_endian>& ,
2853 unsigned int r_type,
2854 const elfcpp::Sym<size, big_endian>&);
2857 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
2858 Target_aarch64<size, big_endian>* ,
2859 Sized_relobj_file<size, big_endian>* ,
2862 const elfcpp::Rela<size, big_endian>& ,
2863 unsigned int r_type,
2868 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
2869 unsigned int r_type);
2872 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
2873 unsigned int r_type, Symbol*);
2876 possible_function_pointer_reloc(unsigned int r_type);
2879 check_non_pic(Relobj*, unsigned int r_type);
2882 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
2883 unsigned int r_type);
2885 // Whether we have issued an error about a non-PIC compilation.
2886 bool issued_non_pic_error_;
2889 // The class which implements relocation.
2894 : skip_call_tls_get_addr_(false)
2900 // Do a relocation. Return false if the caller should not issue
2901 // any warnings about this relocation.
2903 relocate(const Relocate_info<size, big_endian>*, Target_aarch64*,
2905 size_t relnum, const elfcpp::Rela<size, big_endian>&,
2906 unsigned int r_type, const Sized_symbol<size>*,
2907 const Symbol_value<size>*,
2908 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
2912 inline typename AArch64_relocate_functions<size, big_endian>::Status
2913 relocate_tls(const Relocate_info<size, big_endian>*,
2914 Target_aarch64<size, big_endian>*,
2916 const elfcpp::Rela<size, big_endian>&,
2917 unsigned int r_type, const Sized_symbol<size>*,
2918 const Symbol_value<size>*,
2920 typename elfcpp::Elf_types<size>::Elf_Addr);
2922 inline typename AArch64_relocate_functions<size, big_endian>::Status
2924 const Relocate_info<size, big_endian>*,
2925 Target_aarch64<size, big_endian>*,
2926 const elfcpp::Rela<size, big_endian>&,
2929 const Symbol_value<size>*);
2931 inline typename AArch64_relocate_functions<size, big_endian>::Status
2933 const Relocate_info<size, big_endian>*,
2934 Target_aarch64<size, big_endian>*,
2935 const elfcpp::Rela<size, big_endian>&,
2938 const Symbol_value<size>*);
2940 inline typename AArch64_relocate_functions<size, big_endian>::Status
2942 const Relocate_info<size, big_endian>*,
2943 Target_aarch64<size, big_endian>*,
2944 const elfcpp::Rela<size, big_endian>&,
2947 const Symbol_value<size>*);
2949 inline typename AArch64_relocate_functions<size, big_endian>::Status
2951 const Relocate_info<size, big_endian>*,
2952 Target_aarch64<size, big_endian>*,
2953 const elfcpp::Rela<size, big_endian>&,
2956 const Symbol_value<size>*);
2958 inline typename AArch64_relocate_functions<size, big_endian>::Status
2960 const Relocate_info<size, big_endian>*,
2961 Target_aarch64<size, big_endian>*,
2962 const elfcpp::Rela<size, big_endian>&,
2965 const Symbol_value<size>*,
2966 typename elfcpp::Elf_types<size>::Elf_Addr,
2967 typename elfcpp::Elf_types<size>::Elf_Addr);
2969 bool skip_call_tls_get_addr_;
2971 }; // End of class Relocate
2973 // A class which returns the size required for a relocation type,
2974 // used while scanning relocs during a relocatable link.
2975 class Relocatable_size_for_reloc
2979 get_size_for_reloc(unsigned int, Relobj*);
2982 // Adjust TLS relocation type based on the options and whether this
2983 // is a local symbol.
2984 static tls::Tls_optimization
2985 optimize_tls_reloc(bool is_final, int r_type);
2987 // Get the GOT section, creating it if necessary.
2988 Output_data_got_aarch64<size, big_endian>*
2989 got_section(Symbol_table*, Layout*);
2991 // Get the GOT PLT section.
2993 got_plt_section() const
2995 gold_assert(this->got_plt_ != NULL);
2996 return this->got_plt_;
2999 // Get the GOT section for TLSDESC entries.
3000 Output_data_got<size, big_endian>*
3001 got_tlsdesc_section() const
3003 gold_assert(this->got_tlsdesc_ != NULL);
3004 return this->got_tlsdesc_;
3007 // Create the PLT section.
3009 make_plt_section(Symbol_table* symtab, Layout* layout);
3011 // Create a PLT entry for a global symbol.
3013 make_plt_entry(Symbol_table*, Layout*, Symbol*);
3015 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
3017 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
3018 Sized_relobj_file<size, big_endian>* relobj,
3019 unsigned int local_sym_index);
3021 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
3023 define_tls_base_symbol(Symbol_table*, Layout*);
3025 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
3027 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
3029 // Create a GOT entry for the TLS module index.
3031 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
3032 Sized_relobj_file<size, big_endian>* object);
3034 // Get the PLT section.
3035 Output_data_plt_aarch64<size, big_endian>*
3038 gold_assert(this->plt_ != NULL);
3042 // Return whether this is a 3-insn erratum sequence.
3043 bool is_erratum_843419_sequence(
3044 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
3045 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
3046 typename elfcpp::Swap<32,big_endian>::Valtype insn3);
3048 // Get the dynamic reloc section, creating it if necessary.
3050 rela_dyn_section(Layout*);
3052 // Get the section to use for TLSDESC relocations.
3054 rela_tlsdesc_section(Layout*) const;
3056 // Get the section to use for IRELATIVE relocations.
3058 rela_irelative_section(Layout*);
3060 // Add a potential copy relocation.
3062 copy_reloc(Symbol_table* symtab, Layout* layout,
3063 Sized_relobj_file<size, big_endian>* object,
3064 unsigned int shndx, Output_section* output_section,
3065 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
3067 this->copy_relocs_.copy_reloc(symtab, layout,
3068 symtab->get_sized_symbol<size>(sym),
3069 object, shndx, output_section,
3070 reloc, this->rela_dyn_section(layout));
3073 // Information about this specific target which we pass to the
3074 // general Target structure.
3075 static const Target::Target_info aarch64_info;
3077 // The types of GOT entries needed for this platform.
3078 // These values are exposed to the ABI in an incremental link.
3079 // Do not renumber existing values without changing the version
3080 // number of the .gnu_incremental_inputs section.
3083 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
3084 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
3085 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
3086 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
3089 // This type is used as the argument to the target specific
3090 // relocation routines. The only target specific reloc is
3091 // R_AARCh64_TLSDESC against a local symbol.
3094 Tlsdesc_info(Sized_relobj_file<size, big_endian>* a_object,
3095 unsigned int a_r_sym)
3096 : object(a_object), r_sym(a_r_sym)
3099 // The object in which the local symbol is defined.
3100 Sized_relobj_file<size, big_endian>* object;
3101 // The local symbol index in the object.
3106 Output_data_got_aarch64<size, big_endian>* got_;
3108 Output_data_plt_aarch64<size, big_endian>* plt_;
3109 // The GOT PLT section.
3110 Output_data_space* got_plt_;
3111 // The GOT section for IRELATIVE relocations.
3112 Output_data_space* got_irelative_;
3113 // The GOT section for TLSDESC relocations.
3114 Output_data_got<size, big_endian>* got_tlsdesc_;
3115 // The _GLOBAL_OFFSET_TABLE_ symbol.
3116 Symbol* global_offset_table_;
3117 // The dynamic reloc section.
3118 Reloc_section* rela_dyn_;
3119 // The section to use for IRELATIVE relocs.
3120 Reloc_section* rela_irelative_;
3121 // Relocs saved to avoid a COPY reloc.
3122 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
3123 // Offset of the GOT entry for the TLS module index.
3124 unsigned int got_mod_index_offset_;
3125 // We handle R_AARCH64_TLSDESC against a local symbol as a target
3126 // specific relocation. Here we store the object and local symbol
3127 // index for the relocation.
3128 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
3129 // True if the _TLS_MODULE_BASE_ symbol has been defined.
3130 bool tls_base_symbol_defined_;
3131 // List of stub_tables
3132 Stub_table_list stub_tables_;
3133 // Actual stub group size
3134 section_size_type stub_group_size_;
3135 AArch64_input_section_map aarch64_input_section_map_;
3136 }; // End of Target_aarch64
3140 const Target::Target_info Target_aarch64<64, false>::aarch64_info =
3143 false, // is_big_endian
3144 elfcpp::EM_AARCH64, // machine_code
3145 false, // has_make_symbol
3146 false, // has_resolve
3147 false, // has_code_fill
3148 true, // is_default_stack_executable
3149 true, // can_icf_inline_merge_sections
3151 "/lib/ld.so.1", // program interpreter
3152 0x400000, // default_text_segment_address
3153 0x1000, // abi_pagesize (overridable by -z max-page-size)
3154 0x1000, // common_pagesize (overridable by -z common-page-size)
3155 false, // isolate_execinstr
3157 elfcpp::SHN_UNDEF, // small_common_shndx
3158 elfcpp::SHN_UNDEF, // large_common_shndx
3159 0, // small_common_section_flags
3160 0, // large_common_section_flags
3161 NULL, // attributes_section
3162 NULL, // attributes_vendor
3163 "_start" // entry_symbol_name
3167 const Target::Target_info Target_aarch64<32, false>::aarch64_info =
3170 false, // is_big_endian
3171 elfcpp::EM_AARCH64, // machine_code
3172 false, // has_make_symbol
3173 false, // has_resolve
3174 false, // has_code_fill
3175 true, // is_default_stack_executable
3176 false, // can_icf_inline_merge_sections
3178 "/lib/ld.so.1", // program interpreter
3179 0x400000, // default_text_segment_address
3180 0x1000, // abi_pagesize (overridable by -z max-page-size)
3181 0x1000, // common_pagesize (overridable by -z common-page-size)
3182 false, // isolate_execinstr
3184 elfcpp::SHN_UNDEF, // small_common_shndx
3185 elfcpp::SHN_UNDEF, // large_common_shndx
3186 0, // small_common_section_flags
3187 0, // large_common_section_flags
3188 NULL, // attributes_section
3189 NULL, // attributes_vendor
3190 "_start" // entry_symbol_name
3194 const Target::Target_info Target_aarch64<64, true>::aarch64_info =
3197 true, // is_big_endian
3198 elfcpp::EM_AARCH64, // machine_code
3199 false, // has_make_symbol
3200 false, // has_resolve
3201 false, // has_code_fill
3202 true, // is_default_stack_executable
3203 true, // can_icf_inline_merge_sections
3205 "/lib/ld.so.1", // program interpreter
3206 0x400000, // default_text_segment_address
3207 0x1000, // abi_pagesize (overridable by -z max-page-size)
3208 0x1000, // common_pagesize (overridable by -z common-page-size)
3209 false, // isolate_execinstr
3211 elfcpp::SHN_UNDEF, // small_common_shndx
3212 elfcpp::SHN_UNDEF, // large_common_shndx
3213 0, // small_common_section_flags
3214 0, // large_common_section_flags
3215 NULL, // attributes_section
3216 NULL, // attributes_vendor
3217 "_start" // entry_symbol_name
3221 const Target::Target_info Target_aarch64<32, true>::aarch64_info =
3224 true, // is_big_endian
3225 elfcpp::EM_AARCH64, // machine_code
3226 false, // has_make_symbol
3227 false, // has_resolve
3228 false, // has_code_fill
3229 true, // is_default_stack_executable
3230 false, // can_icf_inline_merge_sections
3232 "/lib/ld.so.1", // program interpreter
3233 0x400000, // default_text_segment_address
3234 0x1000, // abi_pagesize (overridable by -z max-page-size)
3235 0x1000, // common_pagesize (overridable by -z common-page-size)
3236 false, // isolate_execinstr
3238 elfcpp::SHN_UNDEF, // small_common_shndx
3239 elfcpp::SHN_UNDEF, // large_common_shndx
3240 0, // small_common_section_flags
3241 0, // large_common_section_flags
3242 NULL, // attributes_section
3243 NULL, // attributes_vendor
3244 "_start" // entry_symbol_name
3247 // Get the GOT section, creating it if necessary.
3249 template<int size, bool big_endian>
3250 Output_data_got_aarch64<size, big_endian>*
3251 Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
3254 if (this->got_ == NULL)
3256 gold_assert(symtab != NULL && layout != NULL);
3258 // When using -z now, we can treat .got.plt as a relro section.
3259 // Without -z now, it is modified after program startup by lazy
3261 bool is_got_plt_relro = parameters->options().now();
3262 Output_section_order got_order = (is_got_plt_relro
3264 : ORDER_RELRO_LAST);
3265 Output_section_order got_plt_order = (is_got_plt_relro
3267 : ORDER_NON_RELRO_FIRST);
3269 // Layout of .got and .got.plt sections.
3270 // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
3272 // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
3273 // .gotplt[1] reserved for ld.so (resolver)
3274 // .gotplt[2] reserved
3276 // Generate .got section.
3277 this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
3279 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3280 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
3281 this->got_, got_order, true);
3282 // The first word of GOT is reserved for the address of .dynamic.
3283 // We put 0 here now. The value will be replaced later in
3284 // Output_data_got_aarch64::do_write.
3285 this->got_->add_constant(0);
3287 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3288 // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
3289 // even if there is a .got.plt section.
3290 this->global_offset_table_ =
3291 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3292 Symbol_table::PREDEFINED,
3294 0, 0, elfcpp::STT_OBJECT,
3296 elfcpp::STV_HIDDEN, 0,
3299 // Generate .got.plt section.
3300 this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
3301 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3303 | elfcpp::SHF_WRITE),
3304 this->got_plt_, got_plt_order,
3307 // The first three entries are reserved.
3308 this->got_plt_->set_current_data_size(
3309 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
3311 // If there are any IRELATIVE relocations, they get GOT entries
3312 // in .got.plt after the jump slot entries.
3313 this->got_irelative_ = new Output_data_space(size / 8,
3314 "** GOT IRELATIVE PLT");
3315 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3317 | elfcpp::SHF_WRITE),
3318 this->got_irelative_,
3322 // If there are any TLSDESC relocations, they get GOT entries in
3323 // .got.plt after the jump slot and IRELATIVE entries.
3324 this->got_tlsdesc_ = new Output_data_got<size, big_endian>();
3325 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
3327 | elfcpp::SHF_WRITE),
3332 if (!is_got_plt_relro)
3334 // Those bytes can go into the relro segment.
3335 layout->increase_relro(
3336 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
3343 // Get the dynamic reloc section, creating it if necessary.
3345 template<int size, bool big_endian>
3346 typename Target_aarch64<size, big_endian>::Reloc_section*
3347 Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
3349 if (this->rela_dyn_ == NULL)
3351 gold_assert(layout != NULL);
3352 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3353 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3354 elfcpp::SHF_ALLOC, this->rela_dyn_,
3355 ORDER_DYNAMIC_RELOCS, false);
3357 return this->rela_dyn_;
3360 // Get the section to use for IRELATIVE relocs, creating it if
3361 // necessary. These go in .rela.dyn, but only after all other dynamic
3362 // relocations. They need to follow the other dynamic relocations so
3363 // that they can refer to global variables initialized by those
3366 template<int size, bool big_endian>
3367 typename Target_aarch64<size, big_endian>::Reloc_section*
3368 Target_aarch64<size, big_endian>::rela_irelative_section(Layout* layout)
3370 if (this->rela_irelative_ == NULL)
3372 // Make sure we have already created the dynamic reloc section.
3373 this->rela_dyn_section(layout);
3374 this->rela_irelative_ = new Reloc_section(false);
3375 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3376 elfcpp::SHF_ALLOC, this->rela_irelative_,
3377 ORDER_DYNAMIC_RELOCS, false);
3378 gold_assert(this->rela_dyn_->output_section()
3379 == this->rela_irelative_->output_section());
3381 return this->rela_irelative_;
3385 // do_make_elf_object to override the same function in the base class. We need
3386 // to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
3387 // store backend specific information. Hence we need to have our own ELF object
3390 template<int size, bool big_endian>
3392 Target_aarch64<size, big_endian>::do_make_elf_object(
3393 const std::string& name,
3394 Input_file* input_file,
3395 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
3397 int et = ehdr.get_e_type();
3398 // ET_EXEC files are valid input for --just-symbols/-R,
3399 // and we treat them as relocatable objects.
3400 if (et == elfcpp::ET_EXEC && input_file->just_symbols())
3401 return Sized_target<size, big_endian>::do_make_elf_object(
3402 name, input_file, offset, ehdr);
3403 else if (et == elfcpp::ET_REL)
3405 AArch64_relobj<size, big_endian>* obj =
3406 new AArch64_relobj<size, big_endian>(name, input_file, offset, ehdr);
3410 else if (et == elfcpp::ET_DYN)
3412 // Keep base implementation.
3413 Sized_dynobj<size, big_endian>* obj =
3414 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
3420 gold_error(_("%s: unsupported ELF file type %d"),
3427 // Scan a relocation for stub generation.
3429 template<int size, bool big_endian>
3431 Target_aarch64<size, big_endian>::scan_reloc_for_stub(
3432 const Relocate_info<size, big_endian>* relinfo,
3433 unsigned int r_type,
3434 const Sized_symbol<size>* gsym,
3436 const Symbol_value<size>* psymval,
3437 typename elfcpp::Elf_types<size>::Elf_Swxword addend,
3440 const AArch64_relobj<size, big_endian>* aarch64_relobj =
3441 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3443 Symbol_value<size> symval;
3446 const AArch64_reloc_property* arp = aarch64_reloc_property_table->
3447 get_reloc_property(r_type);
3448 if (gsym->use_plt_offset(arp->reference_flags()))
3450 // This uses a PLT, change the symbol value.
3451 symval.set_output_value(this->plt_section()->address()
3452 + gsym->plt_offset());
3455 else if (gsym->is_undefined())
3456 // There is no need to generate a stub symbol is undefined.
3460 // Get the symbol value.
3461 typename Symbol_value<size>::Value value = psymval->value(aarch64_relobj, 0);
3463 // Owing to pipelining, the PC relative branches below actually skip
3464 // two instructions when the branch offset is 0.
3465 Address destination = static_cast<Address>(-1);
3468 case elfcpp::R_AARCH64_CALL26:
3469 case elfcpp::R_AARCH64_JUMP26:
3470 destination = value + addend;
3476 int stub_type = The_reloc_stub::
3477 stub_type_for_reloc(r_type, address, destination);
3478 if (stub_type == ST_NONE)
3481 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
3482 gold_assert(stub_table != NULL);
3484 The_reloc_stub_key key(stub_type, gsym, aarch64_relobj, r_sym, addend);
3485 The_reloc_stub* stub = stub_table->find_reloc_stub(key);
3488 stub = new The_reloc_stub(stub_type);
3489 stub_table->add_reloc_stub(stub, key);
3491 stub->set_destination_address(destination);
3492 } // End of Target_aarch64::scan_reloc_for_stub
3495 // This function scans a relocation section for stub generation.
3496 // The template parameter Relocate must be a class type which provides
3497 // a single function, relocate(), which implements the machine
3498 // specific part of a relocation.
3500 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
3501 // SHT_REL or SHT_RELA.
3503 // PRELOCS points to the relocation data. RELOC_COUNT is the number
3504 // of relocs. OUTPUT_SECTION is the output section.
3505 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
3506 // mapped to output offsets.
3508 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
3509 // VIEW_SIZE is the size. These refer to the input section, unless
3510 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
3511 // the output section.
3513 template<int size, bool big_endian>
3514 template<int sh_type>
3516 Target_aarch64<size, big_endian>::scan_reloc_section_for_stubs(
3517 const Relocate_info<size, big_endian>* relinfo,
3518 const unsigned char* prelocs,
3520 Output_section* /*output_section*/,
3521 bool /*needs_special_offset_handling*/,
3522 const unsigned char* /*view*/,
3523 Address view_address,
3526 typedef typename Reloc_types<sh_type,size,big_endian>::Reloc Reltype;
3528 const int reloc_size =
3529 Reloc_types<sh_type,size,big_endian>::reloc_size;
3530 AArch64_relobj<size, big_endian>* object =
3531 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3532 unsigned int local_count = object->local_symbol_count();
3534 gold::Default_comdat_behavior default_comdat_behavior;
3535 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
3537 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
3539 Reltype reloc(prelocs);
3540 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
3541 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3542 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
3543 if (r_type != elfcpp::R_AARCH64_CALL26
3544 && r_type != elfcpp::R_AARCH64_JUMP26)
3547 section_offset_type offset =
3548 convert_to_section_size_type(reloc.get_r_offset());
3551 typename elfcpp::Elf_types<size>::Elf_Swxword addend =
3552 reloc.get_r_addend();
3554 const Sized_symbol<size>* sym;
3555 Symbol_value<size> symval;
3556 const Symbol_value<size> *psymval;
3557 bool is_defined_in_discarded_section;
3559 if (r_sym < local_count)
3562 psymval = object->local_symbol(r_sym);
3564 // If the local symbol belongs to a section we are discarding,
3565 // and that section is a debug section, try to find the
3566 // corresponding kept section and map this symbol to its
3567 // counterpart in the kept section. The symbol must not
3568 // correspond to a section we are folding.
3570 shndx = psymval->input_shndx(&is_ordinary);
3571 is_defined_in_discarded_section =
3573 && shndx != elfcpp::SHN_UNDEF
3574 && !object->is_section_included(shndx)
3575 && !relinfo->symtab->is_section_folded(object, shndx));
3577 // We need to compute the would-be final value of this local
3579 if (!is_defined_in_discarded_section)
3581 typedef Sized_relobj_file<size, big_endian> ObjType;
3582 typename ObjType::Compute_final_local_value_status status =
3583 object->compute_final_local_value(r_sym, psymval, &symval,
3585 if (status == ObjType::CFLV_OK)
3587 // Currently we cannot handle a branch to a target in
3588 // a merged section. If this is the case, issue an error
3589 // and also free the merge symbol value.
3590 if (!symval.has_output_value())
3592 const std::string& section_name =
3593 object->section_name(shndx);
3594 object->error(_("cannot handle branch to local %u "
3595 "in a merged section %s"),
3596 r_sym, section_name.c_str());
3602 // We cannot determine the final value.
3610 gsym = object->global_symbol(r_sym);
3611 gold_assert(gsym != NULL);
3612 if (gsym->is_forwarder())
3613 gsym = relinfo->symtab->resolve_forwards(gsym);
3615 sym = static_cast<const Sized_symbol<size>*>(gsym);
3616 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
3617 symval.set_output_symtab_index(sym->symtab_index());
3619 symval.set_no_output_symtab_entry();
3621 // We need to compute the would-be final value of this global
3623 const Symbol_table* symtab = relinfo->symtab;
3624 const Sized_symbol<size>* sized_symbol =
3625 symtab->get_sized_symbol<size>(gsym);
3626 Symbol_table::Compute_final_value_status status;
3627 typename elfcpp::Elf_types<size>::Elf_Addr value =
3628 symtab->compute_final_value<size>(sized_symbol, &status);
3630 // Skip this if the symbol has not output section.
3631 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
3633 symval.set_output_value(value);
3635 if (gsym->type() == elfcpp::STT_TLS)
3636 symval.set_is_tls_symbol();
3637 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3638 symval.set_is_ifunc_symbol();
3641 is_defined_in_discarded_section =
3642 (gsym->is_defined_in_discarded_section()
3643 && gsym->is_undefined());
3647 Symbol_value<size> symval2;
3648 if (is_defined_in_discarded_section)
3650 if (comdat_behavior == CB_UNDETERMINED)
3652 std::string name = object->section_name(relinfo->data_shndx);
3653 comdat_behavior = default_comdat_behavior.get(name.c_str());
3655 if (comdat_behavior == CB_PRETEND)
3658 typename elfcpp::Elf_types<size>::Elf_Addr value =
3659 object->map_to_kept_section(shndx, &found);
3661 symval2.set_output_value(value + psymval->input_value());
3663 symval2.set_output_value(0);
3667 if (comdat_behavior == CB_WARNING)
3668 gold_warning_at_location(relinfo, i, offset,
3669 _("relocation refers to discarded "
3671 symval2.set_output_value(0);
3673 symval2.set_no_output_symtab_entry();
3677 // If symbol is a section symbol, we don't know the actual type of
3678 // destination. Give up.
3679 if (psymval->is_section_symbol())
3682 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
3683 addend, view_address + offset);
3684 } // End of iterating relocs in a section
3685 } // End of Target_aarch64::scan_reloc_section_for_stubs
3688 // Scan an input section for stub generation.
3690 template<int size, bool big_endian>
3692 Target_aarch64<size, big_endian>::scan_section_for_stubs(
3693 const Relocate_info<size, big_endian>* relinfo,
3694 unsigned int sh_type,
3695 const unsigned char* prelocs,
3697 Output_section* output_section,
3698 bool needs_special_offset_handling,
3699 const unsigned char* view,
3700 Address view_address,
3701 section_size_type view_size)
3703 gold_assert(sh_type == elfcpp::SHT_RELA);
3704 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
3709 needs_special_offset_handling,
3716 // Relocate a single stub.
3718 template<int size, bool big_endian>
3719 void Target_aarch64<size, big_endian>::
3720 relocate_stub(The_reloc_stub* stub,
3721 const The_relocate_info*,
3723 unsigned char* view,
3727 typedef AArch64_relocate_functions<size, big_endian> The_reloc_functions;
3728 typedef typename The_reloc_functions::Status The_reloc_functions_status;
3729 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
3731 Insntype* ip = reinterpret_cast<Insntype*>(view);
3732 int insn_number = stub->insn_num();
3733 const uint32_t* insns = stub->insns();
3734 // Check the insns are really those stub insns.
3735 for (int i = 0; i < insn_number; ++i)
3737 Insntype insn = elfcpp::Swap<32,big_endian>::readval(ip + i);
3738 gold_assert(((uint32_t)insn == insns[i]));
3741 Address dest = stub->destination_address();
3743 switch(stub->type())
3745 case ST_ADRP_BRANCH:
3747 // 1st reloc is ADR_PREL_PG_HI21
3748 The_reloc_functions_status status =
3749 The_reloc_functions::adrp(view, dest, address);
3750 // An error should never arise in the above step. If so, please
3751 // check 'aarch64_valid_for_adrp_p'.
3752 gold_assert(status == The_reloc_functions::STATUS_OKAY);
3754 // 2nd reloc is ADD_ABS_LO12_NC
3755 const AArch64_reloc_property* arp =
3756 aarch64_reloc_property_table->get_reloc_property(
3757 elfcpp::R_AARCH64_ADD_ABS_LO12_NC);
3758 gold_assert(arp != NULL);
3759 status = The_reloc_functions::template
3760 rela_general<32>(view + 4, dest, 0, arp);
3761 // An error should never arise, it is an "_NC" relocation.
3762 gold_assert(status == The_reloc_functions::STATUS_OKAY);
3766 case ST_LONG_BRANCH_ABS:
3767 // 1st reloc is R_AARCH64_PREL64, at offset 8
3768 elfcpp::Swap<64,big_endian>::writeval(view + 8, dest);
3771 case ST_LONG_BRANCH_PCREL:
3773 // "PC" calculation is the 2nd insn in the stub.
3774 uint64_t offset = dest - (address + 4);
3775 // Offset is placed at offset 4 and 5.
3776 elfcpp::Swap<64,big_endian>::writeval(view + 16, offset);
3786 // A class to handle the PLT data.
3787 // This is an abstract base class that handles most of the linker details
3788 // but does not know the actual contents of PLT entries. The derived
3789 // classes below fill in those details.
3791 template<int size, bool big_endian>
3792 class Output_data_plt_aarch64 : public Output_section_data
3795 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
3797 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3799 Output_data_plt_aarch64(Layout* layout,
3801 Output_data_got_aarch64<size, big_endian>* got,
3802 Output_data_space* got_plt,
3803 Output_data_space* got_irelative)
3804 : Output_section_data(addralign), tlsdesc_rel_(NULL), irelative_rel_(NULL),
3805 got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
3806 count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
3807 { this->init(layout); }
3809 // Initialize the PLT section.
3811 init(Layout* layout);
3813 // Add an entry to the PLT.
3815 add_entry(Symbol_table*, Layout*, Symbol* gsym);
3817 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
3819 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
3820 Sized_relobj_file<size, big_endian>* relobj,
3821 unsigned int local_sym_index);
3823 // Add the relocation for a PLT entry.
3825 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
3826 unsigned int got_offset);
3828 // Add the reserved TLSDESC_PLT entry to the PLT.
3830 reserve_tlsdesc_entry(unsigned int got_offset)
3831 { this->tlsdesc_got_offset_ = got_offset; }
3833 // Return true if a TLSDESC_PLT entry has been reserved.
3835 has_tlsdesc_entry() const
3836 { return this->tlsdesc_got_offset_ != -1U; }
3838 // Return the GOT offset for the reserved TLSDESC_PLT entry.
3840 get_tlsdesc_got_offset() const
3841 { return this->tlsdesc_got_offset_; }
3843 // Return the PLT offset of the reserved TLSDESC_PLT entry.
3845 get_tlsdesc_plt_offset() const
3847 return (this->first_plt_entry_offset() +
3848 (this->count_ + this->irelative_count_)
3849 * this->get_plt_entry_size());
3852 // Return the .rela.plt section data.
3855 { return this->rel_; }
3857 // Return where the TLSDESC relocations should go.
3859 rela_tlsdesc(Layout*);
3861 // Return where the IRELATIVE relocations should go in the PLT
3864 rela_irelative(Symbol_table*, Layout*);
3866 // Return whether we created a section for IRELATIVE relocations.
3868 has_irelative_section() const
3869 { return this->irelative_rel_ != NULL; }
3871 // Return the number of PLT entries.
3874 { return this->count_ + this->irelative_count_; }
3876 // Return the offset of the first non-reserved PLT entry.
3878 first_plt_entry_offset() const
3879 { return this->do_first_plt_entry_offset(); }
3881 // Return the size of a PLT entry.
3883 get_plt_entry_size() const
3884 { return this->do_get_plt_entry_size(); }
3886 // Return the reserved tlsdesc entry size.
3888 get_plt_tlsdesc_entry_size() const
3889 { return this->do_get_plt_tlsdesc_entry_size(); }
3891 // Return the PLT address to use for a global symbol.
3893 address_for_global(const Symbol*);
3895 // Return the PLT address to use for a local symbol.
3897 address_for_local(const Relobj*, unsigned int symndx);
3900 // Fill in the first PLT entry.
3902 fill_first_plt_entry(unsigned char* pov,
3903 Address got_address,
3904 Address plt_address)
3905 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
3907 // Fill in a normal PLT entry.
3909 fill_plt_entry(unsigned char* pov,
3910 Address got_address,
3911 Address plt_address,
3912 unsigned int got_offset,
3913 unsigned int plt_offset)
3915 this->do_fill_plt_entry(pov, got_address, plt_address,
3916 got_offset, plt_offset);
3919 // Fill in the reserved TLSDESC PLT entry.
3921 fill_tlsdesc_entry(unsigned char* pov,
3922 Address gotplt_address,
3923 Address plt_address,
3925 unsigned int tlsdesc_got_offset,
3926 unsigned int plt_offset)
3928 this->do_fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
3929 tlsdesc_got_offset, plt_offset);
3932 virtual unsigned int
3933 do_first_plt_entry_offset() const = 0;
3935 virtual unsigned int
3936 do_get_plt_entry_size() const = 0;
3938 virtual unsigned int
3939 do_get_plt_tlsdesc_entry_size() const = 0;
3942 do_fill_first_plt_entry(unsigned char* pov,
3944 Address plt_addr) = 0;
3947 do_fill_plt_entry(unsigned char* pov,
3948 Address got_address,
3949 Address plt_address,
3950 unsigned int got_offset,
3951 unsigned int plt_offset) = 0;
3954 do_fill_tlsdesc_entry(unsigned char* pov,
3955 Address gotplt_address,
3956 Address plt_address,
3958 unsigned int tlsdesc_got_offset,
3959 unsigned int plt_offset) = 0;
3962 do_adjust_output_section(Output_section* os);
3964 // Write to a map file.
3966 do_print_to_mapfile(Mapfile* mapfile) const
3967 { mapfile->print_output_data(this, _("** PLT")); }
3970 // Set the final size.
3972 set_final_data_size();
3974 // Write out the PLT data.
3976 do_write(Output_file*);
3978 // The reloc section.
3979 Reloc_section* rel_;
3981 // The TLSDESC relocs, if necessary. These must follow the regular
3983 Reloc_section* tlsdesc_rel_;
3985 // The IRELATIVE relocs, if necessary. These must follow the
3986 // regular PLT relocations.
3987 Reloc_section* irelative_rel_;
3989 // The .got section.
3990 Output_data_got_aarch64<size, big_endian>* got_;
3992 // The .got.plt section.
3993 Output_data_space* got_plt_;
3995 // The part of the .got.plt section used for IRELATIVE relocs.
3996 Output_data_space* got_irelative_;
3998 // The number of PLT entries.
3999 unsigned int count_;
4001 // Number of PLT entries with R_AARCH64_IRELATIVE relocs. These
4002 // follow the regular PLT entries.
4003 unsigned int irelative_count_;
4005 // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
4006 // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
4007 // indicates an offset is not allocated.
4008 unsigned int tlsdesc_got_offset_;
4011 // Initialize the PLT section.
4013 template<int size, bool big_endian>
4015 Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
4017 this->rel_ = new Reloc_section(false);
4018 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4019 elfcpp::SHF_ALLOC, this->rel_,
4020 ORDER_DYNAMIC_PLT_RELOCS, false);
4023 template<int size, bool big_endian>
4025 Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
4028 os->set_entsize(this->get_plt_entry_size());
4031 // Add an entry to the PLT.
4033 template<int size, bool big_endian>
4035 Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol_table* symtab,
4036 Layout* layout, Symbol* gsym)
4038 gold_assert(!gsym->has_plt_offset());
4040 unsigned int* pcount;
4041 unsigned int plt_reserved;
4042 Output_section_data_build* got;
4044 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4045 && gsym->can_use_relative_reloc(false))
4047 pcount = &this->irelative_count_;
4049 got = this->got_irelative_;
4053 pcount = &this->count_;
4054 plt_reserved = this->first_plt_entry_offset();
4055 got = this->got_plt_;
4058 gsym->set_plt_offset((*pcount) * this->get_plt_entry_size()
4063 section_offset_type got_offset = got->current_data_size();
4065 // Every PLT entry needs a GOT entry which points back to the PLT
4066 // entry (this will be changed by the dynamic linker, normally
4067 // lazily when the function is called).
4068 got->set_current_data_size(got_offset + size / 8);
4070 // Every PLT entry needs a reloc.
4071 this->add_relocation(symtab, layout, gsym, got_offset);
4073 // Note that we don't need to save the symbol. The contents of the
4074 // PLT are independent of which symbols are used. The symbols only
4075 // appear in the relocations.
4078 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
4081 template<int size, bool big_endian>
4083 Output_data_plt_aarch64<size, big_endian>::add_local_ifunc_entry(
4084 Symbol_table* symtab,
4086 Sized_relobj_file<size, big_endian>* relobj,
4087 unsigned int local_sym_index)
4089 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
4090 ++this->irelative_count_;
4092 section_offset_type got_offset = this->got_irelative_->current_data_size();
4094 // Every PLT entry needs a GOT entry which points back to the PLT
4096 this->got_irelative_->set_current_data_size(got_offset + size / 8);
4098 // Every PLT entry needs a reloc.
4099 Reloc_section* rela = this->rela_irelative(symtab, layout);
4100 rela->add_symbolless_local_addend(relobj, local_sym_index,
4101 elfcpp::R_AARCH64_IRELATIVE,
4102 this->got_irelative_, got_offset, 0);
4107 // Add the relocation for a PLT entry.
4109 template<int size, bool big_endian>
4111 Output_data_plt_aarch64<size, big_endian>::add_relocation(
4112 Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
4114 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4115 && gsym->can_use_relative_reloc(false))
4117 Reloc_section* rela = this->rela_irelative(symtab, layout);
4118 rela->add_symbolless_global_addend(gsym, elfcpp::R_AARCH64_IRELATIVE,
4119 this->got_irelative_, got_offset, 0);
4123 gsym->set_needs_dynsym_entry();
4124 this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT, this->got_plt_,
4129 // Return where the TLSDESC relocations should go, creating it if
4130 // necessary. These follow the JUMP_SLOT relocations.
4132 template<int size, bool big_endian>
4133 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
4134 Output_data_plt_aarch64<size, big_endian>::rela_tlsdesc(Layout* layout)
4136 if (this->tlsdesc_rel_ == NULL)
4138 this->tlsdesc_rel_ = new Reloc_section(false);
4139 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4140 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
4141 ORDER_DYNAMIC_PLT_RELOCS, false);
4142 gold_assert(this->tlsdesc_rel_->output_section()
4143 == this->rel_->output_section());
4145 return this->tlsdesc_rel_;
4148 // Return where the IRELATIVE relocations should go in the PLT. These
4149 // follow the JUMP_SLOT and the TLSDESC relocations.
4151 template<int size, bool big_endian>
4152 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
4153 Output_data_plt_aarch64<size, big_endian>::rela_irelative(Symbol_table* symtab,
4156 if (this->irelative_rel_ == NULL)
4158 // Make sure we have a place for the TLSDESC relocations, in
4159 // case we see any later on.
4160 this->rela_tlsdesc(layout);
4161 this->irelative_rel_ = new Reloc_section(false);
4162 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4163 elfcpp::SHF_ALLOC, this->irelative_rel_,
4164 ORDER_DYNAMIC_PLT_RELOCS, false);
4165 gold_assert(this->irelative_rel_->output_section()
4166 == this->rel_->output_section());
4168 if (parameters->doing_static_link())
4170 // A statically linked executable will only have a .rela.plt
4171 // section to hold R_AARCH64_IRELATIVE relocs for
4172 // STT_GNU_IFUNC symbols. The library will use these
4173 // symbols to locate the IRELATIVE relocs at program startup
4175 symtab->define_in_output_data("__rela_iplt_start", NULL,
4176 Symbol_table::PREDEFINED,
4177 this->irelative_rel_, 0, 0,
4178 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4179 elfcpp::STV_HIDDEN, 0, false, true);
4180 symtab->define_in_output_data("__rela_iplt_end", NULL,
4181 Symbol_table::PREDEFINED,
4182 this->irelative_rel_, 0, 0,
4183 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4184 elfcpp::STV_HIDDEN, 0, true, true);
4187 return this->irelative_rel_;
4190 // Return the PLT address to use for a global symbol.
4192 template<int size, bool big_endian>
4194 Output_data_plt_aarch64<size, big_endian>::address_for_global(
4197 uint64_t offset = 0;
4198 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4199 && gsym->can_use_relative_reloc(false))
4200 offset = (this->first_plt_entry_offset() +
4201 this->count_ * this->get_plt_entry_size());
4202 return this->address() + offset + gsym->plt_offset();
4205 // Return the PLT address to use for a local symbol. These are always
4206 // IRELATIVE relocs.
4208 template<int size, bool big_endian>
4210 Output_data_plt_aarch64<size, big_endian>::address_for_local(
4211 const Relobj* object,
4214 return (this->address()
4215 + this->first_plt_entry_offset()
4216 + this->count_ * this->get_plt_entry_size()
4217 + object->local_plt_offset(r_sym));
4220 // Set the final size.
4222 template<int size, bool big_endian>
4224 Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
4226 unsigned int count = this->count_ + this->irelative_count_;
4227 unsigned int extra_size = 0;
4228 if (this->has_tlsdesc_entry())
4229 extra_size += this->get_plt_tlsdesc_entry_size();
4230 this->set_data_size(this->first_plt_entry_offset()
4231 + count * this->get_plt_entry_size()
4235 template<int size, bool big_endian>
4236 class Output_data_plt_aarch64_standard :
4237 public Output_data_plt_aarch64<size, big_endian>
4240 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4241 Output_data_plt_aarch64_standard(
4243 Output_data_got_aarch64<size, big_endian>* got,
4244 Output_data_space* got_plt,
4245 Output_data_space* got_irelative)
4246 : Output_data_plt_aarch64<size, big_endian>(layout,
4253 // Return the offset of the first non-reserved PLT entry.
4254 virtual unsigned int
4255 do_first_plt_entry_offset() const
4256 { return this->first_plt_entry_size; }
4258 // Return the size of a PLT entry
4259 virtual unsigned int
4260 do_get_plt_entry_size() const
4261 { return this->plt_entry_size; }
4263 // Return the size of a tlsdesc entry
4264 virtual unsigned int
4265 do_get_plt_tlsdesc_entry_size() const
4266 { return this->plt_tlsdesc_entry_size; }
4269 do_fill_first_plt_entry(unsigned char* pov,
4270 Address got_address,
4271 Address plt_address);
4274 do_fill_plt_entry(unsigned char* pov,
4275 Address got_address,
4276 Address plt_address,
4277 unsigned int got_offset,
4278 unsigned int plt_offset);
4281 do_fill_tlsdesc_entry(unsigned char* pov,
4282 Address gotplt_address,
4283 Address plt_address,
4285 unsigned int tlsdesc_got_offset,
4286 unsigned int plt_offset);
4289 // The size of the first plt entry size.
4290 static const int first_plt_entry_size = 32;
4291 // The size of the plt entry size.
4292 static const int plt_entry_size = 16;
4293 // The size of the plt tlsdesc entry size.
4294 static const int plt_tlsdesc_entry_size = 32;
4295 // Template for the first PLT entry.
4296 static const uint32_t first_plt_entry[first_plt_entry_size / 4];
4297 // Template for subsequent PLT entries.
4298 static const uint32_t plt_entry[plt_entry_size / 4];
4299 // The reserved TLSDESC entry in the PLT for an executable.
4300 static const uint32_t tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4];
4303 // The first entry in the PLT for an executable.
4307 Output_data_plt_aarch64_standard<32, false>::
4308 first_plt_entry[first_plt_entry_size / 4] =
4310 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4311 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4312 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4313 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4314 0xd61f0220, /* br x17 */
4315 0xd503201f, /* nop */
4316 0xd503201f, /* nop */
4317 0xd503201f, /* nop */
4323 Output_data_plt_aarch64_standard<32, true>::
4324 first_plt_entry[first_plt_entry_size / 4] =
4326 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4327 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4328 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4329 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4330 0xd61f0220, /* br x17 */
4331 0xd503201f, /* nop */
4332 0xd503201f, /* nop */
4333 0xd503201f, /* nop */
4339 Output_data_plt_aarch64_standard<64, false>::
4340 first_plt_entry[first_plt_entry_size / 4] =
4342 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4343 0x90000010, /* adrp x16, PLT_GOT+16 */
4344 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4345 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4346 0xd61f0220, /* br x17 */
4347 0xd503201f, /* nop */
4348 0xd503201f, /* nop */
4349 0xd503201f, /* nop */
4355 Output_data_plt_aarch64_standard<64, true>::
4356 first_plt_entry[first_plt_entry_size / 4] =
4358 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4359 0x90000010, /* adrp x16, PLT_GOT+16 */
4360 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4361 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4362 0xd61f0220, /* br x17 */
4363 0xd503201f, /* nop */
4364 0xd503201f, /* nop */
4365 0xd503201f, /* nop */
4371 Output_data_plt_aarch64_standard<32, false>::
4372 plt_entry[plt_entry_size / 4] =
4374 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4375 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4376 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4377 0xd61f0220, /* br x17. */
4383 Output_data_plt_aarch64_standard<32, true>::
4384 plt_entry[plt_entry_size / 4] =
4386 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4387 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4388 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4389 0xd61f0220, /* br x17. */
4395 Output_data_plt_aarch64_standard<64, false>::
4396 plt_entry[plt_entry_size / 4] =
4398 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4399 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4400 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4401 0xd61f0220, /* br x17. */
4407 Output_data_plt_aarch64_standard<64, true>::
4408 plt_entry[plt_entry_size / 4] =
4410 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4411 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4412 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4413 0xd61f0220, /* br x17. */
4417 template<int size, bool big_endian>
4419 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
4421 Address got_address,
4422 Address plt_address)
4424 // PLT0 of the small PLT looks like this in ELF64 -
4425 // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
4426 // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
4427 // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
4429 // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
4430 // GOTPLT entry for this.
4432 // PLT0 will be slightly different in ELF32 due to different got entry
4434 memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
4435 Address gotplt_2nd_ent = got_address + (size / 8) * 2;
4437 // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
4438 // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
4439 // FIXME: This only works for 64bit
4440 AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
4441 gotplt_2nd_ent, plt_address + 4);
4443 // Fill in R_AARCH64_LDST8_LO12
4444 elfcpp::Swap<32, big_endian>::writeval(
4446 ((this->first_plt_entry[2] & 0xffc003ff)
4447 | ((gotplt_2nd_ent & 0xff8) << 7)));
4449 // Fill in R_AARCH64_ADD_ABS_LO12
4450 elfcpp::Swap<32, big_endian>::writeval(
4452 ((this->first_plt_entry[3] & 0xffc003ff)
4453 | ((gotplt_2nd_ent & 0xfff) << 10)));
4457 // Subsequent entries in the PLT for an executable.
4458 // FIXME: This only works for 64bit
4460 template<int size, bool big_endian>
4462 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
4464 Address got_address,
4465 Address plt_address,
4466 unsigned int got_offset,
4467 unsigned int plt_offset)
4469 memcpy(pov, this->plt_entry, this->plt_entry_size);
4471 Address gotplt_entry_address = got_address + got_offset;
4472 Address plt_entry_address = plt_address + plt_offset;
4474 // Fill in R_AARCH64_PCREL_ADR_HI21
4475 AArch64_relocate_functions<size, big_endian>::adrp(
4477 gotplt_entry_address,
4480 // Fill in R_AARCH64_LDST64_ABS_LO12
4481 elfcpp::Swap<32, big_endian>::writeval(
4483 ((this->plt_entry[1] & 0xffc003ff)
4484 | ((gotplt_entry_address & 0xff8) << 7)));
4486 // Fill in R_AARCH64_ADD_ABS_LO12
4487 elfcpp::Swap<32, big_endian>::writeval(
4489 ((this->plt_entry[2] & 0xffc003ff)
4490 | ((gotplt_entry_address & 0xfff) <<10)));
4497 Output_data_plt_aarch64_standard<32, false>::
4498 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4500 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4501 0x90000002, /* adrp x2, 0 */
4502 0x90000003, /* adrp x3, 0 */
4503 0xb9400042, /* ldr w2, [w2, #0] */
4504 0x11000063, /* add w3, w3, 0 */
4505 0xd61f0040, /* br x2 */
4506 0xd503201f, /* nop */
4507 0xd503201f, /* nop */
4512 Output_data_plt_aarch64_standard<32, true>::
4513 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4515 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4516 0x90000002, /* adrp x2, 0 */
4517 0x90000003, /* adrp x3, 0 */
4518 0xb9400042, /* ldr w2, [w2, #0] */
4519 0x11000063, /* add w3, w3, 0 */
4520 0xd61f0040, /* br x2 */
4521 0xd503201f, /* nop */
4522 0xd503201f, /* nop */
4527 Output_data_plt_aarch64_standard<64, false>::
4528 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4530 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4531 0x90000002, /* adrp x2, 0 */
4532 0x90000003, /* adrp x3, 0 */
4533 0xf9400042, /* ldr x2, [x2, #0] */
4534 0x91000063, /* add x3, x3, 0 */
4535 0xd61f0040, /* br x2 */
4536 0xd503201f, /* nop */
4537 0xd503201f, /* nop */
4542 Output_data_plt_aarch64_standard<64, true>::
4543 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4545 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4546 0x90000002, /* adrp x2, 0 */
4547 0x90000003, /* adrp x3, 0 */
4548 0xf9400042, /* ldr x2, [x2, #0] */
4549 0x91000063, /* add x3, x3, 0 */
4550 0xd61f0040, /* br x2 */
4551 0xd503201f, /* nop */
4552 0xd503201f, /* nop */
4555 template<int size, bool big_endian>
4557 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_tlsdesc_entry(
4559 Address gotplt_address,
4560 Address plt_address,
4562 unsigned int tlsdesc_got_offset,
4563 unsigned int plt_offset)
4565 memcpy(pov, tlsdesc_plt_entry, plt_tlsdesc_entry_size);
4567 // move DT_TLSDESC_GOT address into x2
4568 // move .got.plt address into x3
4569 Address tlsdesc_got_entry = got_base + tlsdesc_got_offset;
4570 Address plt_entry_address = plt_address + plt_offset;
4572 // R_AARCH64_ADR_PREL_PG_HI21
4573 AArch64_relocate_functions<size, big_endian>::adrp(
4576 plt_entry_address + 4);
4578 // R_AARCH64_ADR_PREL_PG_HI21
4579 AArch64_relocate_functions<size, big_endian>::adrp(
4582 plt_entry_address + 8);
4584 // R_AARCH64_LDST64_ABS_LO12
4585 elfcpp::Swap<32, big_endian>::writeval(
4587 ((this->tlsdesc_plt_entry[3] & 0xffc003ff)
4588 | ((tlsdesc_got_entry & 0xff8) << 7)));
4590 // R_AARCH64_ADD_ABS_LO12
4591 elfcpp::Swap<32, big_endian>::writeval(
4593 ((this->tlsdesc_plt_entry[4] & 0xffc003ff)
4594 | ((gotplt_address & 0xfff) << 10)));
4597 // Write out the PLT. This uses the hand-coded instructions above,
4598 // and adjusts them as needed. This is specified by the AMD64 ABI.
4600 template<int size, bool big_endian>
4602 Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
4604 const off_t offset = this->offset();
4605 const section_size_type oview_size =
4606 convert_to_section_size_type(this->data_size());
4607 unsigned char* const oview = of->get_output_view(offset, oview_size);
4609 const off_t got_file_offset = this->got_plt_->offset();
4610 gold_assert(got_file_offset + this->got_plt_->data_size()
4611 == this->got_irelative_->offset());
4613 const section_size_type got_size =
4614 convert_to_section_size_type(this->got_plt_->data_size()
4615 + this->got_irelative_->data_size());
4616 unsigned char* const got_view = of->get_output_view(got_file_offset,
4619 unsigned char* pov = oview;
4621 // The base address of the .plt section.
4622 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
4623 // The base address of the PLT portion of the .got section.
4624 typename elfcpp::Elf_types<size>::Elf_Addr gotplt_address
4625 = this->got_plt_->address();
4627 this->fill_first_plt_entry(pov, gotplt_address, plt_address);
4628 pov += this->first_plt_entry_offset();
4630 // The first three entries in .got.plt are reserved.
4631 unsigned char* got_pov = got_view;
4632 memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
4633 got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
4635 unsigned int plt_offset = this->first_plt_entry_offset();
4636 unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
4637 const unsigned int count = this->count_ + this->irelative_count_;
4638 for (unsigned int plt_index = 0;
4641 pov += this->get_plt_entry_size(),
4642 got_pov += size / 8,
4643 plt_offset += this->get_plt_entry_size(),
4644 got_offset += size / 8)
4646 // Set and adjust the PLT entry itself.
4647 this->fill_plt_entry(pov, gotplt_address, plt_address,
4648 got_offset, plt_offset);
4650 // Set the entry in the GOT, which points to plt0.
4651 elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
4654 if (this->has_tlsdesc_entry())
4656 // Set and adjust the reserved TLSDESC PLT entry.
4657 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
4658 // The base address of the .base section.
4659 typename elfcpp::Elf_types<size>::Elf_Addr got_base =
4660 this->got_->address();
4661 this->fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
4662 tlsdesc_got_offset, plt_offset);
4663 pov += this->get_plt_tlsdesc_entry_size();
4666 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4667 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4669 of->write_output_view(offset, oview_size, oview);
4670 of->write_output_view(got_file_offset, got_size, got_view);
4673 // Telling how to update the immediate field of an instruction.
4674 struct AArch64_howto
4676 // The immediate field mask.
4677 elfcpp::Elf_Xword dst_mask;
4679 // The offset to apply relocation immediate
4682 // The second part offset, if the immediate field has two parts.
4683 // -1 if the immediate field has only one part.
4687 static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
4689 {0, -1, -1}, // DATA
4690 {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
4691 {0xffffe0, 5, -1}, // LD [23:5]-imm19
4692 {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
4693 {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
4694 {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
4695 {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
4696 {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
4697 {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
4698 {0x3ffffff, 0, -1}, // B [25:0]-imm26
4699 {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
4702 // AArch64 relocate function class
4704 template<int size, bool big_endian>
4705 class AArch64_relocate_functions
4710 STATUS_OKAY, // No error during relocation.
4711 STATUS_OVERFLOW, // Relocation overflow.
4712 STATUS_BAD_RELOC, // Relocation cannot be applied.
4715 typedef AArch64_relocate_functions<size, big_endian> This;
4716 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4717 typedef Relocate_info<size, big_endian> The_relocate_info;
4718 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
4719 typedef Reloc_stub<size, big_endian> The_reloc_stub;
4720 typedef Stub_table<size, big_endian> The_stub_table;
4721 typedef elfcpp::Rela<size, big_endian> The_rela;
4722 typedef typename elfcpp::Swap<size, big_endian>::Valtype AArch64_valtype;
4724 // Return the page address of the address.
4725 // Page(address) = address & ~0xFFF
4727 static inline AArch64_valtype
4728 Page(Address address)
4730 return (address & (~static_cast<Address>(0xFFF)));
4734 // Update instruction (pointed by view) with selected bits (immed).
4735 // val = (val & ~dst_mask) | (immed << doffset)
4737 template<int valsize>
4739 update_view(unsigned char* view,
4740 AArch64_valtype immed,
4741 elfcpp::Elf_Xword doffset,
4742 elfcpp::Elf_Xword dst_mask)
4744 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4745 Valtype* wv = reinterpret_cast<Valtype*>(view);
4746 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
4748 // Clear immediate fields.
4750 elfcpp::Swap<valsize, big_endian>::writeval(wv,
4751 static_cast<Valtype>(val | (immed << doffset)));
4754 // Update two parts of an instruction (pointed by view) with selected
4755 // bits (immed1 and immed2).
4756 // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
4758 template<int valsize>
4760 update_view_two_parts(
4761 unsigned char* view,
4762 AArch64_valtype immed1,
4763 AArch64_valtype immed2,
4764 elfcpp::Elf_Xword doffset1,
4765 elfcpp::Elf_Xword doffset2,
4766 elfcpp::Elf_Xword dst_mask)
4768 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4769 Valtype* wv = reinterpret_cast<Valtype*>(view);
4770 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
4772 elfcpp::Swap<valsize, big_endian>::writeval(wv,
4773 static_cast<Valtype>(val | (immed1 << doffset1) |
4774 (immed2 << doffset2)));
4777 // Update adr or adrp instruction with immed.
4778 // In adr and adrp: [30:29] immlo [23:5] immhi
4781 update_adr(unsigned char* view, AArch64_valtype immed)
4783 elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
4784 This::template update_view_two_parts<32>(
4787 (immed & 0x1ffffc) >> 2,
4793 // Update movz/movn instruction with bits immed.
4794 // Set instruction to movz if is_movz is true, otherwise set instruction
4798 update_movnz(unsigned char* view,
4799 AArch64_valtype immed,
4802 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4803 Valtype* wv = reinterpret_cast<Valtype*>(view);
4804 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4806 const elfcpp::Elf_Xword doffset =
4807 aarch64_howto[AArch64_reloc_property::INST_MOVW].doffset;
4808 const elfcpp::Elf_Xword dst_mask =
4809 aarch64_howto[AArch64_reloc_property::INST_MOVW].dst_mask;
4811 // Clear immediate fields and opc code.
4812 val &= ~(dst_mask | (0x3 << 29));
4814 // Set instruction to movz or movn.
4815 // movz: [30:29] is 10 movn: [30:29] is 00
4819 elfcpp::Swap<32, big_endian>::writeval(wv,
4820 static_cast<Valtype>(val | (immed << doffset)));
4823 // Update selected bits in text.
4825 template<int valsize>
4826 static inline typename This::Status
4827 reloc_common(unsigned char* view, Address x,
4828 const AArch64_reloc_property* reloc_property)
4830 // Select bits from X.
4831 Address immed = reloc_property->select_x_value(x);
4834 const AArch64_reloc_property::Reloc_inst inst =
4835 reloc_property->reloc_inst();
4836 // If it is a data relocation or instruction has 2 parts of immediate
4837 // fields, you should not call pcrela_general.
4838 gold_assert(aarch64_howto[inst].doffset2 == -1 &&
4839 aarch64_howto[inst].doffset != -1);
4840 This::template update_view<valsize>(view, immed,
4841 aarch64_howto[inst].doffset,
4842 aarch64_howto[inst].dst_mask);
4844 // Do check overflow or alignment if needed.
4845 return (reloc_property->checkup_x_value(x)
4847 : This::STATUS_OVERFLOW);
4852 // Construct a B insn. Note, although we group it here with other relocation
4853 // operation, there is actually no 'relocation' involved here.
4855 construct_b(unsigned char* view, unsigned int branch_offset)
4857 update_view_two_parts<32>(view, 0x05, (branch_offset >> 2),
4861 // Do a simple rela relocation at unaligned addresses.
4863 template<int valsize>
4864 static inline typename This::Status
4865 rela_ua(unsigned char* view,
4866 const Sized_relobj_file<size, big_endian>* object,
4867 const Symbol_value<size>* psymval,
4868 AArch64_valtype addend,
4869 const AArch64_reloc_property* reloc_property)
4871 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
4873 typename elfcpp::Elf_types<size>::Elf_Addr x =
4874 psymval->value(object, addend);
4875 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
4876 static_cast<Valtype>(x));
4877 return (reloc_property->checkup_x_value(x)
4879 : This::STATUS_OVERFLOW);
4882 // Do a simple pc-relative relocation at unaligned addresses.
4884 template<int valsize>
4885 static inline typename This::Status
4886 pcrela_ua(unsigned char* view,
4887 const Sized_relobj_file<size, big_endian>* object,
4888 const Symbol_value<size>* psymval,
4889 AArch64_valtype addend,
4891 const AArch64_reloc_property* reloc_property)
4893 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
4895 Address x = psymval->value(object, addend) - address;
4896 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
4897 static_cast<Valtype>(x));
4898 return (reloc_property->checkup_x_value(x)
4900 : This::STATUS_OVERFLOW);
4903 // Do a simple rela relocation at aligned addresses.
4905 template<int valsize>
4906 static inline typename This::Status
4908 unsigned char* view,
4909 const Sized_relobj_file<size, big_endian>* object,
4910 const Symbol_value<size>* psymval,
4911 AArch64_valtype addend,
4912 const AArch64_reloc_property* reloc_property)
4914 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4915 Valtype* wv = reinterpret_cast<Valtype*>(view);
4916 Address x = psymval->value(object, addend);
4917 elfcpp::Swap<valsize, big_endian>::writeval(wv,static_cast<Valtype>(x));
4918 return (reloc_property->checkup_x_value(x)
4920 : This::STATUS_OVERFLOW);
4923 // Do relocate. Update selected bits in text.
4924 // new_val = (val & ~dst_mask) | (immed << doffset)
4926 template<int valsize>
4927 static inline typename This::Status
4928 rela_general(unsigned char* view,
4929 const Sized_relobj_file<size, big_endian>* object,
4930 const Symbol_value<size>* psymval,
4931 AArch64_valtype addend,
4932 const AArch64_reloc_property* reloc_property)
4934 // Calculate relocation.
4935 Address x = psymval->value(object, addend);
4936 return This::template reloc_common<valsize>(view, x, reloc_property);
4939 // Do relocate. Update selected bits in text.
4940 // new val = (val & ~dst_mask) | (immed << doffset)
4942 template<int valsize>
4943 static inline typename This::Status
4945 unsigned char* view,
4947 AArch64_valtype addend,
4948 const AArch64_reloc_property* reloc_property)
4950 // Calculate relocation.
4951 Address x = s + addend;
4952 return This::template reloc_common<valsize>(view, x, reloc_property);
4955 // Do address relative relocate. Update selected bits in text.
4956 // new val = (val & ~dst_mask) | (immed << doffset)
4958 template<int valsize>
4959 static inline typename This::Status
4961 unsigned char* view,
4962 const Sized_relobj_file<size, big_endian>* object,
4963 const Symbol_value<size>* psymval,
4964 AArch64_valtype addend,
4966 const AArch64_reloc_property* reloc_property)
4968 // Calculate relocation.
4969 Address x = psymval->value(object, addend) - address;
4970 return This::template reloc_common<valsize>(view, x, reloc_property);
4974 // Calculate (S + A) - address, update adr instruction.
4976 static inline typename This::Status
4977 adr(unsigned char* view,
4978 const Sized_relobj_file<size, big_endian>* object,
4979 const Symbol_value<size>* psymval,
4982 const AArch64_reloc_property* /* reloc_property */)
4984 AArch64_valtype x = psymval->value(object, addend) - address;
4985 // Pick bits [20:0] of X.
4986 AArch64_valtype immed = x & 0x1fffff;
4987 update_adr(view, immed);
4988 // Check -2^20 <= X < 2^20
4989 return (size == 64 && Bits<21>::has_overflow((x))
4990 ? This::STATUS_OVERFLOW
4991 : This::STATUS_OKAY);
4994 // Calculate PG(S+A) - PG(address), update adrp instruction.
4995 // R_AARCH64_ADR_PREL_PG_HI21
4997 static inline typename This::Status
4999 unsigned char* view,
5003 AArch64_valtype x = This::Page(sa) - This::Page(address);
5004 // Pick [32:12] of X.
5005 AArch64_valtype immed = (x >> 12) & 0x1fffff;
5006 update_adr(view, immed);
5007 // Check -2^32 <= X < 2^32
5008 return (size == 64 && Bits<33>::has_overflow((x))
5009 ? This::STATUS_OVERFLOW
5010 : This::STATUS_OKAY);
5013 // Calculate PG(S+A) - PG(address), update adrp instruction.
5014 // R_AARCH64_ADR_PREL_PG_HI21
5016 static inline typename This::Status
5017 adrp(unsigned char* view,
5018 const Sized_relobj_file<size, big_endian>* object,
5019 const Symbol_value<size>* psymval,
5022 const AArch64_reloc_property* reloc_property)
5024 Address sa = psymval->value(object, addend);
5025 AArch64_valtype x = This::Page(sa) - This::Page(address);
5026 // Pick [32:12] of X.
5027 AArch64_valtype immed = (x >> 12) & 0x1fffff;
5028 update_adr(view, immed);
5029 return (reloc_property->checkup_x_value(x)
5031 : This::STATUS_OVERFLOW);
5034 // Update mov[n/z] instruction. Check overflow if needed.
5035 // If X >=0, set the instruction to movz and its immediate value to the
5037 // If X < 0, set the instruction to movn and its immediate value to
5038 // NOT (selected bits of).
5040 static inline typename This::Status
5041 movnz(unsigned char* view,
5043 const AArch64_reloc_property* reloc_property)
5045 // Select bits from X.
5048 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedW;
5049 if (static_cast<SignedW>(x) >= 0)
5051 immed = reloc_property->select_x_value(x);
5056 immed = reloc_property->select_x_value(~x);;
5060 // Update movnz instruction.
5061 update_movnz(view, immed, is_movz);
5063 // Do check overflow or alignment if needed.
5064 return (reloc_property->checkup_x_value(x)
5066 : This::STATUS_OVERFLOW);
5070 maybe_apply_stub(unsigned int,
5071 const The_relocate_info*,
5075 const Sized_symbol<size>*,
5076 const Symbol_value<size>*,
5077 const Sized_relobj_file<size, big_endian>*,
5080 }; // End of AArch64_relocate_functions
5083 // For a certain relocation type (usually jump/branch), test to see if the
5084 // destination needs a stub to fulfil. If so, re-route the destination of the
5085 // original instruction to the stub, note, at this time, the stub has already
5088 template<int size, bool big_endian>
5090 AArch64_relocate_functions<size, big_endian>::
5091 maybe_apply_stub(unsigned int r_type,
5092 const The_relocate_info* relinfo,
5093 const The_rela& rela,
5094 unsigned char* view,
5096 const Sized_symbol<size>* gsym,
5097 const Symbol_value<size>* psymval,
5098 const Sized_relobj_file<size, big_endian>* object,
5099 section_size_type current_group_size)
5101 if (parameters->options().relocatable())
5104 typename elfcpp::Elf_types<size>::Elf_Swxword addend = rela.get_r_addend();
5105 Address branch_target = psymval->value(object, 0) + addend;
5107 The_reloc_stub::stub_type_for_reloc(r_type, address, branch_target);
5108 if (stub_type == ST_NONE)
5111 const The_aarch64_relobj* aarch64_relobj =
5112 static_cast<const The_aarch64_relobj*>(object);
5113 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
5114 gold_assert(stub_table != NULL);
5116 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5117 typename The_reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
5118 The_reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
5119 gold_assert(stub != NULL);
5121 Address new_branch_target = stub_table->address() + stub->offset();
5122 typename elfcpp::Swap<size, big_endian>::Valtype branch_offset =
5123 new_branch_target - address;
5124 const AArch64_reloc_property* arp =
5125 aarch64_reloc_property_table->get_reloc_property(r_type);
5126 gold_assert(arp != NULL);
5127 typename This::Status status = This::template
5128 rela_general<32>(view, branch_offset, 0, arp);
5129 if (status != This::STATUS_OKAY)
5130 gold_error(_("Stub is too far away, try a smaller value "
5131 "for '--stub-group-size'. The current value is 0x%lx."),
5132 static_cast<unsigned long>(current_group_size));
5137 // Group input sections for stub generation.
5139 // We group input sections in an output section so that the total size,
5140 // including any padding space due to alignment is smaller than GROUP_SIZE
5141 // unless the only input section in group is bigger than GROUP_SIZE already.
5142 // Then an ARM stub table is created to follow the last input section
5143 // in group. For each group an ARM stub table is created an is placed
5144 // after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
5145 // extend the group after the stub table.
5147 template<int size, bool big_endian>
5149 Target_aarch64<size, big_endian>::group_sections(
5151 section_size_type group_size,
5152 bool stubs_always_after_branch,
5155 // Group input sections and insert stub table
5156 Layout::Section_list section_list;
5157 layout->get_executable_sections(§ion_list);
5158 for (Layout::Section_list::const_iterator p = section_list.begin();
5159 p != section_list.end();
5162 AArch64_output_section<size, big_endian>* output_section =
5163 static_cast<AArch64_output_section<size, big_endian>*>(*p);
5164 output_section->group_sections(group_size, stubs_always_after_branch,
5170 // Find the AArch64_input_section object corresponding to the SHNDX-th input
5171 // section of RELOBJ.
5173 template<int size, bool big_endian>
5174 AArch64_input_section<size, big_endian>*
5175 Target_aarch64<size, big_endian>::find_aarch64_input_section(
5176 Relobj* relobj, unsigned int shndx) const
5178 Section_id sid(relobj, shndx);
5179 typename AArch64_input_section_map::const_iterator p =
5180 this->aarch64_input_section_map_.find(sid);
5181 return (p != this->aarch64_input_section_map_.end()) ? p->second : NULL;
5185 // Make a new AArch64_input_section object.
5187 template<int size, bool big_endian>
5188 AArch64_input_section<size, big_endian>*
5189 Target_aarch64<size, big_endian>::new_aarch64_input_section(
5190 Relobj* relobj, unsigned int shndx)
5192 Section_id sid(relobj, shndx);
5194 AArch64_input_section<size, big_endian>* input_section =
5195 new AArch64_input_section<size, big_endian>(relobj, shndx);
5196 input_section->init();
5198 // Register new AArch64_input_section in map for look-up.
5199 std::pair<typename AArch64_input_section_map::iterator,bool> ins =
5200 this->aarch64_input_section_map_.insert(
5201 std::make_pair(sid, input_section));
5203 // Make sure that it we have not created another AArch64_input_section
5204 // for this input section already.
5205 gold_assert(ins.second);
5207 return input_section;
5211 // Relaxation hook. This is where we do stub generation.
5213 template<int size, bool big_endian>
5215 Target_aarch64<size, big_endian>::do_relax(
5217 const Input_objects* input_objects,
5218 Symbol_table* symtab,
5222 gold_assert(!parameters->options().relocatable());
5225 // We don't handle negative stub_group_size right now.
5226 this->stub_group_size_ = abs(parameters->options().stub_group_size());
5227 if (this->stub_group_size_ == 1)
5229 // Leave room for 4096 4-byte stub entries. If we exceed that, then we
5230 // will fail to link. The user will have to relink with an explicit
5231 // group size option.
5232 this->stub_group_size_ = The_reloc_stub::MAX_BRANCH_OFFSET -
5235 group_sections(layout, this->stub_group_size_, true, task);
5239 // If this is not the first pass, addresses and file offsets have
5240 // been reset at this point, set them here.
5241 for (Stub_table_iterator sp = this->stub_tables_.begin();
5242 sp != this->stub_tables_.end(); ++sp)
5244 The_stub_table* stt = *sp;
5245 The_aarch64_input_section* owner = stt->owner();
5246 off_t off = align_address(owner->original_size(),
5248 stt->set_address_and_file_offset(owner->address() + off,
5249 owner->offset() + off);
5253 // Scan relocs for relocation stubs
5254 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
5255 op != input_objects->relobj_end();
5258 The_aarch64_relobj* aarch64_relobj =
5259 static_cast<The_aarch64_relobj*>(*op);
5260 // Lock the object so we can read from it. This is only called
5261 // single-threaded from Layout::finalize, so it is OK to lock.
5262 Task_lock_obj<Object> tl(task, aarch64_relobj);
5263 aarch64_relobj->scan_sections_for_stubs(this, symtab, layout);
5266 bool any_stub_table_changed = false;
5267 for (Stub_table_iterator siter = this->stub_tables_.begin();
5268 siter != this->stub_tables_.end() && !any_stub_table_changed; ++siter)
5270 The_stub_table* stub_table = *siter;
5271 if (stub_table->update_data_size_changed_p())
5273 The_aarch64_input_section* owner = stub_table->owner();
5274 uint64_t address = owner->address();
5275 off_t offset = owner->offset();
5276 owner->reset_address_and_file_offset();
5277 owner->set_address_and_file_offset(address, offset);
5279 any_stub_table_changed = true;
5283 // Do not continue relaxation.
5284 bool continue_relaxation = any_stub_table_changed;
5285 if (!continue_relaxation)
5286 for (Stub_table_iterator sp = this->stub_tables_.begin();
5287 (sp != this->stub_tables_.end());
5289 (*sp)->finalize_stubs();
5291 return continue_relaxation;
5295 // Make a new Stub_table.
5297 template<int size, bool big_endian>
5298 Stub_table<size, big_endian>*
5299 Target_aarch64<size, big_endian>::new_stub_table(
5300 AArch64_input_section<size, big_endian>* owner)
5302 Stub_table<size, big_endian>* stub_table =
5303 new Stub_table<size, big_endian>(owner);
5304 stub_table->set_address(align_address(
5305 owner->address() + owner->data_size(), 8));
5306 stub_table->set_file_offset(owner->offset() + owner->data_size());
5307 stub_table->finalize_data_size();
5309 this->stub_tables_.push_back(stub_table);
5315 template<int size, bool big_endian>
5317 Target_aarch64<size, big_endian>::do_reloc_addend(
5318 void* arg, unsigned int r_type, uint64_t) const
5320 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
5321 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5322 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5323 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5324 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5325 gold_assert(psymval->is_tls_symbol());
5326 // The value of a TLS symbol is the offset in the TLS segment.
5327 return psymval->value(ti.object, 0);
5330 // Return the number of entries in the PLT.
5332 template<int size, bool big_endian>
5334 Target_aarch64<size, big_endian>::plt_entry_count() const
5336 if (this->plt_ == NULL)
5338 return this->plt_->entry_count();
5341 // Return the offset of the first non-reserved PLT entry.
5343 template<int size, bool big_endian>
5345 Target_aarch64<size, big_endian>::first_plt_entry_offset() const
5347 return this->plt_->first_plt_entry_offset();
5350 // Return the size of each PLT entry.
5352 template<int size, bool big_endian>
5354 Target_aarch64<size, big_endian>::plt_entry_size() const
5356 return this->plt_->get_plt_entry_size();
5359 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
5361 template<int size, bool big_endian>
5363 Target_aarch64<size, big_endian>::define_tls_base_symbol(
5364 Symbol_table* symtab, Layout* layout)
5366 if (this->tls_base_symbol_defined_)
5369 Output_segment* tls_segment = layout->tls_segment();
5370 if (tls_segment != NULL)
5372 // _TLS_MODULE_BASE_ always points to the beginning of tls segment.
5373 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
5374 Symbol_table::PREDEFINED,
5378 elfcpp::STV_HIDDEN, 0,
5379 Symbol::SEGMENT_START,
5382 this->tls_base_symbol_defined_ = true;
5385 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
5387 template<int size, bool big_endian>
5389 Target_aarch64<size, big_endian>::reserve_tlsdesc_entries(
5390 Symbol_table* symtab, Layout* layout)
5392 if (this->plt_ == NULL)
5393 this->make_plt_section(symtab, layout);
5395 if (!this->plt_->has_tlsdesc_entry())
5397 // Allocate the TLSDESC_GOT entry.
5398 Output_data_got_aarch64<size, big_endian>* got =
5399 this->got_section(symtab, layout);
5400 unsigned int got_offset = got->add_constant(0);
5402 // Allocate the TLSDESC_PLT entry.
5403 this->plt_->reserve_tlsdesc_entry(got_offset);
5407 // Create a GOT entry for the TLS module index.
5409 template<int size, bool big_endian>
5411 Target_aarch64<size, big_endian>::got_mod_index_entry(
5412 Symbol_table* symtab, Layout* layout,
5413 Sized_relobj_file<size, big_endian>* object)
5415 if (this->got_mod_index_offset_ == -1U)
5417 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5418 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5419 Output_data_got_aarch64<size, big_endian>* got =
5420 this->got_section(symtab, layout);
5421 unsigned int got_offset = got->add_constant(0);
5422 rela_dyn->add_local(object, 0, elfcpp::R_AARCH64_TLS_DTPMOD64, got,
5424 got->add_constant(0);
5425 this->got_mod_index_offset_ = got_offset;
5427 return this->got_mod_index_offset_;
5430 // Optimize the TLS relocation type based on what we know about the
5431 // symbol. IS_FINAL is true if the final address of this symbol is
5432 // known at link time.
5434 template<int size, bool big_endian>
5435 tls::Tls_optimization
5436 Target_aarch64<size, big_endian>::optimize_tls_reloc(bool is_final,
5439 // If we are generating a shared library, then we can't do anything
5441 if (parameters->options().shared())
5442 return tls::TLSOPT_NONE;
5446 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5447 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5448 case elfcpp::R_AARCH64_TLSDESC_LD_PREL19:
5449 case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21:
5450 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5451 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5452 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5453 case elfcpp::R_AARCH64_TLSDESC_OFF_G1:
5454 case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC:
5455 case elfcpp::R_AARCH64_TLSDESC_LDR:
5456 case elfcpp::R_AARCH64_TLSDESC_ADD:
5457 case elfcpp::R_AARCH64_TLSDESC_CALL:
5458 // These are General-Dynamic which permits fully general TLS
5459 // access. Since we know that we are generating an executable,
5460 // we can convert this to Initial-Exec. If we also know that
5461 // this is a local symbol, we can further switch to Local-Exec.
5463 return tls::TLSOPT_TO_LE;
5464 return tls::TLSOPT_TO_IE;
5466 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5467 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5468 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5469 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5470 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5471 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5472 // These are Local-Dynamic, which refer to local symbols in the
5473 // dynamic TLS block. Since we know that we generating an
5474 // executable, we can switch to Local-Exec.
5475 return tls::TLSOPT_TO_LE;
5477 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
5478 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
5479 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5480 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5481 case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5482 // These are Initial-Exec relocs which get the thread offset
5483 // from the GOT. If we know that we are linking against the
5484 // local symbol, we can switch to Local-Exec, which links the
5485 // thread offset into the instruction.
5487 return tls::TLSOPT_TO_LE;
5488 return tls::TLSOPT_NONE;
5490 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5491 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5492 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5493 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5494 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5495 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5496 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5497 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5498 // When we already have Local-Exec, there is nothing further we
5500 return tls::TLSOPT_NONE;
5507 // Returns true if this relocation type could be that of a function pointer.
5509 template<int size, bool big_endian>
5511 Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
5512 unsigned int r_type)
5516 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
5517 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
5518 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
5519 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5520 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5528 // For safe ICF, scan a relocation for a local symbol to check if it
5529 // corresponds to a function pointer being taken. In that case mark
5530 // the function whose pointer was taken as not foldable.
5532 template<int size, bool big_endian>
5534 Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
5537 Target_aarch64<size, big_endian>* ,
5538 Sized_relobj_file<size, big_endian>* ,
5541 const elfcpp::Rela<size, big_endian>& ,
5542 unsigned int r_type,
5543 const elfcpp::Sym<size, big_endian>&)
5545 // When building a shared library, do not fold any local symbols.
5546 return (parameters->options().shared()
5547 || possible_function_pointer_reloc(r_type));
5550 // For safe ICF, scan a relocation for a global symbol to check if it
5551 // corresponds to a function pointer being taken. In that case mark
5552 // the function whose pointer was taken as not foldable.
5554 template<int size, bool big_endian>
5556 Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
5559 Target_aarch64<size, big_endian>* ,
5560 Sized_relobj_file<size, big_endian>* ,
5563 const elfcpp::Rela<size, big_endian>& ,
5564 unsigned int r_type,
5567 // When building a shared library, do not fold symbols whose visibility
5568 // is hidden, internal or protected.
5569 return ((parameters->options().shared()
5570 && (gsym->visibility() == elfcpp::STV_INTERNAL
5571 || gsym->visibility() == elfcpp::STV_PROTECTED
5572 || gsym->visibility() == elfcpp::STV_HIDDEN))
5573 || possible_function_pointer_reloc(r_type));
5576 // Report an unsupported relocation against a local symbol.
5578 template<int size, bool big_endian>
5580 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
5581 Sized_relobj_file<size, big_endian>* object,
5582 unsigned int r_type)
5584 gold_error(_("%s: unsupported reloc %u against local symbol"),
5585 object->name().c_str(), r_type);
5588 // We are about to emit a dynamic relocation of type R_TYPE. If the
5589 // dynamic linker does not support it, issue an error.
5591 template<int size, bool big_endian>
5593 Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
5594 unsigned int r_type)
5596 gold_assert(r_type != elfcpp::R_AARCH64_NONE);
5600 // These are the relocation types supported by glibc for AARCH64.
5601 case elfcpp::R_AARCH64_NONE:
5602 case elfcpp::R_AARCH64_COPY:
5603 case elfcpp::R_AARCH64_GLOB_DAT:
5604 case elfcpp::R_AARCH64_JUMP_SLOT:
5605 case elfcpp::R_AARCH64_RELATIVE:
5606 case elfcpp::R_AARCH64_TLS_DTPREL64:
5607 case elfcpp::R_AARCH64_TLS_DTPMOD64:
5608 case elfcpp::R_AARCH64_TLS_TPREL64:
5609 case elfcpp::R_AARCH64_TLSDESC:
5610 case elfcpp::R_AARCH64_IRELATIVE:
5611 case elfcpp::R_AARCH64_ABS32:
5612 case elfcpp::R_AARCH64_ABS64:
5619 // This prevents us from issuing more than one error per reloc
5620 // section. But we can still wind up issuing more than one
5621 // error per object file.
5622 if (this->issued_non_pic_error_)
5624 gold_assert(parameters->options().output_is_position_independent());
5625 object->error(_("requires unsupported dynamic reloc; "
5626 "recompile with -fPIC"));
5627 this->issued_non_pic_error_ = true;
5631 // Return whether we need to make a PLT entry for a relocation of the
5632 // given type against a STT_GNU_IFUNC symbol.
5634 template<int size, bool big_endian>
5636 Target_aarch64<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
5637 Sized_relobj_file<size, big_endian>* object,
5638 unsigned int r_type)
5640 const AArch64_reloc_property* arp =
5641 aarch64_reloc_property_table->get_reloc_property(r_type);
5642 gold_assert(arp != NULL);
5644 int flags = arp->reference_flags();
5645 if (flags & Symbol::TLS_REF)
5647 gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
5648 object->name().c_str(), arp->name().c_str());
5654 // Scan a relocation for a local symbol.
5656 template<int size, bool big_endian>
5658 Target_aarch64<size, big_endian>::Scan::local(
5659 Symbol_table* symtab,
5661 Target_aarch64<size, big_endian>* target,
5662 Sized_relobj_file<size, big_endian>* object,
5663 unsigned int data_shndx,
5664 Output_section* output_section,
5665 const elfcpp::Rela<size, big_endian>& rela,
5666 unsigned int r_type,
5667 const elfcpp::Sym<size, big_endian>& lsym,
5673 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
5675 Output_data_got_aarch64<size, big_endian>* got =
5676 target->got_section(symtab, layout);
5677 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5679 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5680 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
5681 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
5682 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
5686 case elfcpp::R_AARCH64_ABS32:
5687 case elfcpp::R_AARCH64_ABS16:
5688 if (parameters->options().output_is_position_independent())
5690 gold_error(_("%s: unsupported reloc %u in pos independent link."),
5691 object->name().c_str(), r_type);
5695 case elfcpp::R_AARCH64_ABS64:
5696 // If building a shared library or pie, we need to mark this as a dynmic
5697 // reloction, so that the dynamic loader can relocate it.
5698 if (parameters->options().output_is_position_independent())
5700 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5701 rela_dyn->add_local_relative(object, r_sym,
5702 elfcpp::R_AARCH64_RELATIVE,
5705 rela.get_r_offset(),
5706 rela.get_r_addend(),
5711 case elfcpp::R_AARCH64_PREL64:
5712 case elfcpp::R_AARCH64_PREL32:
5713 case elfcpp::R_AARCH64_PREL16:
5716 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
5717 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
5718 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
5719 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
5720 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
5721 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
5722 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
5723 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
5724 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
5725 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
5728 // Control flow, pc-relative. We don't need to do anything for a relative
5729 // addressing relocation against a local symbol if it does not reference
5731 case elfcpp::R_AARCH64_TSTBR14:
5732 case elfcpp::R_AARCH64_CONDBR19:
5733 case elfcpp::R_AARCH64_JUMP26:
5734 case elfcpp::R_AARCH64_CALL26:
5737 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5738 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5740 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5741 optimize_tls_reloc(!parameters->options().shared(), r_type);
5742 if (tlsopt == tls::TLSOPT_TO_LE)
5745 layout->set_has_static_tls();
5746 // Create a GOT entry for the tp-relative offset.
5747 if (!parameters->doing_static_link())
5749 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
5750 target->rela_dyn_section(layout),
5751 elfcpp::R_AARCH64_TLS_TPREL64);
5753 else if (!object->local_has_got_offset(r_sym,
5754 GOT_TYPE_TLS_OFFSET))
5756 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
5757 unsigned int got_offset =
5758 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
5759 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5760 gold_assert(addend == 0);
5761 got->add_static_reloc(got_offset, elfcpp::R_AARCH64_TLS_TPREL64,
5767 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5768 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5770 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5771 optimize_tls_reloc(!parameters->options().shared(), r_type);
5772 if (tlsopt == tls::TLSOPT_TO_LE)
5774 layout->set_has_static_tls();
5777 gold_assert(tlsopt == tls::TLSOPT_NONE);
5779 got->add_local_pair_with_rel(object,r_sym, data_shndx,
5781 target->rela_dyn_section(layout),
5782 elfcpp::R_AARCH64_TLS_DTPMOD64);
5786 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5787 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5788 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5789 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5790 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5791 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5792 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5793 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5795 layout->set_has_static_tls();
5796 bool output_is_shared = parameters->options().shared();
5797 if (output_is_shared)
5798 gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
5799 object->name().c_str(), r_type);
5803 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5804 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5806 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5807 optimize_tls_reloc(!parameters->options().shared(), r_type);
5808 if (tlsopt == tls::TLSOPT_NONE)
5810 // Create a GOT entry for the module index.
5811 target->got_mod_index_entry(symtab, layout, object);
5813 else if (tlsopt != tls::TLSOPT_TO_LE)
5814 unsupported_reloc_local(object, r_type);
5818 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5819 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5820 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5821 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5824 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5825 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5826 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5828 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5829 optimize_tls_reloc(!parameters->options().shared(), r_type);
5830 target->define_tls_base_symbol(symtab, layout);
5831 if (tlsopt == tls::TLSOPT_NONE)
5833 // Create reserved PLT and GOT entries for the resolver.
5834 target->reserve_tlsdesc_entries(symtab, layout);
5836 // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
5837 // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
5838 // entry needs to be in an area in .got.plt, not .got. Call
5839 // got_section to make sure the section has been created.
5840 target->got_section(symtab, layout);
5841 Output_data_got<size, big_endian>* got =
5842 target->got_tlsdesc_section();
5843 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5844 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
5846 unsigned int got_offset = got->add_constant(0);
5847 got->add_constant(0);
5848 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
5850 Reloc_section* rt = target->rela_tlsdesc_section(layout);
5851 // We store the arguments we need in a vector, and use
5852 // the index into the vector as the parameter to pass
5853 // to the target specific routines.
5854 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
5855 void* arg = reinterpret_cast<void*>(intarg);
5856 rt->add_target_specific(elfcpp::R_AARCH64_TLSDESC, arg,
5857 got, got_offset, 0);
5860 else if (tlsopt != tls::TLSOPT_TO_LE)
5861 unsupported_reloc_local(object, r_type);
5865 case elfcpp::R_AARCH64_TLSDESC_CALL:
5869 unsupported_reloc_local(object, r_type);
5874 // Report an unsupported relocation against a global symbol.
5876 template<int size, bool big_endian>
5878 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
5879 Sized_relobj_file<size, big_endian>* object,
5880 unsigned int r_type,
5883 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5884 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5887 template<int size, bool big_endian>
5889 Target_aarch64<size, big_endian>::Scan::global(
5890 Symbol_table* symtab,
5892 Target_aarch64<size, big_endian>* target,
5893 Sized_relobj_file<size, big_endian> * object,
5894 unsigned int data_shndx,
5895 Output_section* output_section,
5896 const elfcpp::Rela<size, big_endian>& rela,
5897 unsigned int r_type,
5900 // A STT_GNU_IFUNC symbol may require a PLT entry.
5901 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5902 && this->reloc_needs_plt_for_ifunc(object, r_type))
5903 target->make_plt_entry(symtab, layout, gsym);
5905 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
5907 const AArch64_reloc_property* arp =
5908 aarch64_reloc_property_table->get_reloc_property(r_type);
5909 gold_assert(arp != NULL);
5913 case elfcpp::R_AARCH64_ABS16:
5914 case elfcpp::R_AARCH64_ABS32:
5915 case elfcpp::R_AARCH64_ABS64:
5917 // Make a PLT entry if necessary.
5918 if (gsym->needs_plt_entry())
5920 target->make_plt_entry(symtab, layout, gsym);
5921 // Since this is not a PC-relative relocation, we may be
5922 // taking the address of a function. In that case we need to
5923 // set the entry in the dynamic symbol table to the address of
5925 if (gsym->is_from_dynobj() && !parameters->options().shared())
5926 gsym->set_needs_dynsym_value();
5928 // Make a dynamic relocation if necessary.
5929 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
5931 if (!parameters->options().output_is_position_independent()
5932 && gsym->may_need_copy_reloc())
5934 target->copy_reloc(symtab, layout, object,
5935 data_shndx, output_section, gsym, rela);
5937 else if (r_type == elfcpp::R_AARCH64_ABS64
5938 && gsym->type() == elfcpp::STT_GNU_IFUNC
5939 && gsym->can_use_relative_reloc(false)
5940 && !gsym->is_from_dynobj()
5941 && !gsym->is_undefined()
5942 && !gsym->is_preemptible())
5944 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
5945 // symbol. This makes a function address in a PIE executable
5946 // match the address in a shared library that it links against.
5947 Reloc_section* rela_dyn =
5948 target->rela_irelative_section(layout);
5949 unsigned int r_type = elfcpp::R_AARCH64_IRELATIVE;
5950 rela_dyn->add_symbolless_global_addend(gsym, r_type,
5951 output_section, object,
5953 rela.get_r_offset(),
5954 rela.get_r_addend());
5956 else if (r_type == elfcpp::R_AARCH64_ABS64
5957 && gsym->can_use_relative_reloc(false))
5959 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5960 rela_dyn->add_global_relative(gsym,
5961 elfcpp::R_AARCH64_RELATIVE,
5965 rela.get_r_offset(),
5966 rela.get_r_addend(),
5971 check_non_pic(object, r_type);
5972 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*
5973 rela_dyn = target->rela_dyn_section(layout);
5974 rela_dyn->add_global(
5975 gsym, r_type, output_section, object,
5976 data_shndx, rela.get_r_offset(),rela.get_r_addend());
5982 case elfcpp::R_AARCH64_PREL16:
5983 case elfcpp::R_AARCH64_PREL32:
5984 case elfcpp::R_AARCH64_PREL64:
5985 // This is used to fill the GOT absolute address.
5986 if (gsym->needs_plt_entry())
5988 target->make_plt_entry(symtab, layout, gsym);
5992 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
5993 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
5994 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
5995 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
5996 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
5997 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
5998 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
5999 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
6000 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
6001 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
6003 if (gsym->needs_plt_entry())
6004 target->make_plt_entry(symtab, layout, gsym);
6005 // Make a dynamic relocation if necessary.
6006 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
6008 if (parameters->options().output_is_executable()
6009 && gsym->may_need_copy_reloc())
6011 target->copy_reloc(symtab, layout, object,
6012 data_shndx, output_section, gsym, rela);
6018 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6019 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6021 // This pair of relocations is used to access a specific GOT entry.
6022 // Note a GOT entry is an *address* to a symbol.
6023 // The symbol requires a GOT entry
6024 Output_data_got_aarch64<size, big_endian>* got =
6025 target->got_section(symtab, layout);
6026 if (gsym->final_value_is_known())
6028 // For a STT_GNU_IFUNC symbol we want the PLT address.
6029 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
6030 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6032 got->add_global(gsym, GOT_TYPE_STANDARD);
6036 // If this symbol is not fully resolved, we need to add a dynamic
6037 // relocation for it.
6038 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6040 // Use a GLOB_DAT rather than a RELATIVE reloc if:
6042 // 1) The symbol may be defined in some other module.
6043 // 2) We are building a shared library and this is a protected
6044 // symbol; using GLOB_DAT means that the dynamic linker can use
6045 // the address of the PLT in the main executable when appropriate
6046 // so that function address comparisons work.
6047 // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
6048 // again so that function address comparisons work.
6049 if (gsym->is_from_dynobj()
6050 || gsym->is_undefined()
6051 || gsym->is_preemptible()
6052 || (gsym->visibility() == elfcpp::STV_PROTECTED
6053 && parameters->options().shared())
6054 || (gsym->type() == elfcpp::STT_GNU_IFUNC
6055 && parameters->options().output_is_position_independent()))
6056 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
6057 rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
6060 // For a STT_GNU_IFUNC symbol we want to write the PLT
6061 // offset into the GOT, so that function pointer
6062 // comparisons work correctly.
6064 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
6065 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
6068 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6069 // Tell the dynamic linker to use the PLT address
6070 // when resolving relocations.
6071 if (gsym->is_from_dynobj()
6072 && !parameters->options().shared())
6073 gsym->set_needs_dynsym_value();
6077 rela_dyn->add_global_relative(
6078 gsym, elfcpp::R_AARCH64_RELATIVE,
6080 gsym->got_offset(GOT_TYPE_STANDARD),
6089 case elfcpp::R_AARCH64_TSTBR14:
6090 case elfcpp::R_AARCH64_CONDBR19:
6091 case elfcpp::R_AARCH64_JUMP26:
6092 case elfcpp::R_AARCH64_CALL26:
6094 if (gsym->final_value_is_known())
6097 if (gsym->is_defined() &&
6098 !gsym->is_from_dynobj() &&
6099 !gsym->is_preemptible())
6102 // Make plt entry for function call.
6103 target->make_plt_entry(symtab, layout, gsym);
6107 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6108 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // General dynamic
6110 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6111 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6112 if (tlsopt == tls::TLSOPT_TO_LE)
6114 layout->set_has_static_tls();
6117 gold_assert(tlsopt == tls::TLSOPT_NONE);
6120 Output_data_got_aarch64<size, big_endian>* got =
6121 target->got_section(symtab, layout);
6122 // Create 2 consecutive entries for module index and offset.
6123 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
6124 target->rela_dyn_section(layout),
6125 elfcpp::R_AARCH64_TLS_DTPMOD64,
6126 elfcpp::R_AARCH64_TLS_DTPREL64);
6130 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6131 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local dynamic
6133 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6134 optimize_tls_reloc(!parameters->options().shared(), r_type);
6135 if (tlsopt == tls::TLSOPT_NONE)
6137 // Create a GOT entry for the module index.
6138 target->got_mod_index_entry(symtab, layout, object);
6140 else if (tlsopt != tls::TLSOPT_TO_LE)
6141 unsupported_reloc_local(object, r_type);
6145 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6146 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6147 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6148 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local dynamic
6151 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6152 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial executable
6154 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6155 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6156 if (tlsopt == tls::TLSOPT_TO_LE)
6159 layout->set_has_static_tls();
6160 // Create a GOT entry for the tp-relative offset.
6161 Output_data_got_aarch64<size, big_endian>* got
6162 = target->got_section(symtab, layout);
6163 if (!parameters->doing_static_link())
6165 got->add_global_with_rel(
6166 gsym, GOT_TYPE_TLS_OFFSET,
6167 target->rela_dyn_section(layout),
6168 elfcpp::R_AARCH64_TLS_TPREL64);
6170 if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
6172 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
6173 unsigned int got_offset =
6174 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
6175 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6176 gold_assert(addend == 0);
6177 got->add_static_reloc(got_offset,
6178 elfcpp::R_AARCH64_TLS_TPREL64, gsym);
6183 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6184 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6185 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6186 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6187 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6188 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6189 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6190 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: // Local executable
6191 layout->set_has_static_tls();
6192 if (parameters->options().shared())
6193 gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
6194 object->name().c_str(), r_type);
6197 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6198 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6199 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12: // TLS descriptor
6201 target->define_tls_base_symbol(symtab, layout);
6202 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6203 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
6204 if (tlsopt == tls::TLSOPT_NONE)
6206 // Create reserved PLT and GOT entries for the resolver.
6207 target->reserve_tlsdesc_entries(symtab, layout);
6209 // Create a double GOT entry with an R_AARCH64_TLSDESC
6210 // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
6211 // entry needs to be in an area in .got.plt, not .got. Call
6212 // got_section to make sure the section has been created.
6213 target->got_section(symtab, layout);
6214 Output_data_got<size, big_endian>* got =
6215 target->got_tlsdesc_section();
6216 Reloc_section* rt = target->rela_tlsdesc_section(layout);
6217 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
6218 elfcpp::R_AARCH64_TLSDESC, 0);
6220 else if (tlsopt == tls::TLSOPT_TO_IE)
6222 // Create a GOT entry for the tp-relative offset.
6223 Output_data_got<size, big_endian>* got
6224 = target->got_section(symtab, layout);
6225 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
6226 target->rela_dyn_section(layout),
6227 elfcpp::R_AARCH64_TLS_TPREL64);
6229 else if (tlsopt != tls::TLSOPT_TO_LE)
6230 unsupported_reloc_global(object, r_type, gsym);
6234 case elfcpp::R_AARCH64_TLSDESC_CALL:
6238 gold_error(_("%s: unsupported reloc type in global scan"),
6239 aarch64_reloc_property_table->
6240 reloc_name_in_error_message(r_type).c_str());
6243 } // End of Scan::global
6246 // Create the PLT section.
6247 template<int size, bool big_endian>
6249 Target_aarch64<size, big_endian>::make_plt_section(
6250 Symbol_table* symtab, Layout* layout)
6252 if (this->plt_ == NULL)
6254 // Create the GOT section first.
6255 this->got_section(symtab, layout);
6257 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
6258 this->got_irelative_);
6260 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
6262 | elfcpp::SHF_EXECINSTR),
6263 this->plt_, ORDER_PLT, false);
6265 // Make the sh_info field of .rela.plt point to .plt.
6266 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
6267 rela_plt_os->set_info_section(this->plt_->output_section());
6271 // Return the section for TLSDESC relocations.
6273 template<int size, bool big_endian>
6274 typename Target_aarch64<size, big_endian>::Reloc_section*
6275 Target_aarch64<size, big_endian>::rela_tlsdesc_section(Layout* layout) const
6277 return this->plt_section()->rela_tlsdesc(layout);
6280 // Create a PLT entry for a global symbol.
6282 template<int size, bool big_endian>
6284 Target_aarch64<size, big_endian>::make_plt_entry(
6285 Symbol_table* symtab,
6289 if (gsym->has_plt_offset())
6292 if (this->plt_ == NULL)
6293 this->make_plt_section(symtab, layout);
6295 this->plt_->add_entry(symtab, layout, gsym);
6298 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6300 template<int size, bool big_endian>
6302 Target_aarch64<size, big_endian>::make_local_ifunc_plt_entry(
6303 Symbol_table* symtab, Layout* layout,
6304 Sized_relobj_file<size, big_endian>* relobj,
6305 unsigned int local_sym_index)
6307 if (relobj->local_has_plt_offset(local_sym_index))
6309 if (this->plt_ == NULL)
6310 this->make_plt_section(symtab, layout);
6311 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
6314 relobj->set_local_plt_offset(local_sym_index, plt_offset);
6317 template<int size, bool big_endian>
6319 Target_aarch64<size, big_endian>::gc_process_relocs(
6320 Symbol_table* symtab,
6322 Sized_relobj_file<size, big_endian>* object,
6323 unsigned int data_shndx,
6324 unsigned int sh_type,
6325 const unsigned char* prelocs,
6327 Output_section* output_section,
6328 bool needs_special_offset_handling,
6329 size_t local_symbol_count,
6330 const unsigned char* plocal_symbols)
6332 if (sh_type == elfcpp::SHT_REL)
6337 gold::gc_process_relocs<
6339 Target_aarch64<size, big_endian>,
6341 typename Target_aarch64<size, big_endian>::Scan,
6342 typename Target_aarch64<size, big_endian>::Relocatable_size_for_reloc>(
6351 needs_special_offset_handling,
6356 // Scan relocations for a section.
6358 template<int size, bool big_endian>
6360 Target_aarch64<size, big_endian>::scan_relocs(
6361 Symbol_table* symtab,
6363 Sized_relobj_file<size, big_endian>* object,
6364 unsigned int data_shndx,
6365 unsigned int sh_type,
6366 const unsigned char* prelocs,
6368 Output_section* output_section,
6369 bool needs_special_offset_handling,
6370 size_t local_symbol_count,
6371 const unsigned char* plocal_symbols)
6373 if (sh_type == elfcpp::SHT_REL)
6375 gold_error(_("%s: unsupported REL reloc section"),
6376 object->name().c_str());
6379 gold::scan_relocs<size, big_endian, Target_aarch64, elfcpp::SHT_RELA, Scan>(
6388 needs_special_offset_handling,
6393 // Return the value to use for a dynamic which requires special
6394 // treatment. This is how we support equality comparisons of function
6395 // pointers across shared library boundaries, as described in the
6396 // processor specific ABI supplement.
6398 template<int size, bool big_endian>
6400 Target_aarch64<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
6402 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
6403 return this->plt_address_for_global(gsym);
6407 // Finalize the sections.
6409 template<int size, bool big_endian>
6411 Target_aarch64<size, big_endian>::do_finalize_sections(
6413 const Input_objects*,
6414 Symbol_table* symtab)
6416 const Reloc_section* rel_plt = (this->plt_ == NULL
6418 : this->plt_->rela_plt());
6419 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
6420 this->rela_dyn_, true, false);
6422 // Emit any relocs we saved in an attempt to avoid generating COPY
6424 if (this->copy_relocs_.any_saved_relocs())
6425 this->copy_relocs_.emit(this->rela_dyn_section(layout));
6427 // Fill in some more dynamic tags.
6428 Output_data_dynamic* const odyn = layout->dynamic_data();
6431 if (this->plt_ != NULL
6432 && this->plt_->output_section() != NULL
6433 && this->plt_ ->has_tlsdesc_entry())
6435 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
6436 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
6437 this->got_->finalize_data_size();
6438 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
6439 this->plt_, plt_offset);
6440 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
6441 this->got_, got_offset);
6445 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
6446 // the .got.plt section.
6447 Symbol* sym = this->global_offset_table_;
6450 uint64_t data_size = this->got_plt_->current_data_size();
6451 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
6453 // If the .got section is more than 0x8000 bytes, we add
6454 // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
6455 // bit relocations have a greater chance of working.
6456 if (data_size >= 0x8000)
6457 symtab->get_sized_symbol<size>(sym)->set_value(
6458 symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
6461 if (parameters->doing_static_link()
6462 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
6464 // If linking statically, make sure that the __rela_iplt symbols
6465 // were defined if necessary, even if we didn't create a PLT.
6466 static const Define_symbol_in_segment syms[] =
6469 "__rela_iplt_start", // name
6470 elfcpp::PT_LOAD, // segment_type
6471 elfcpp::PF_W, // segment_flags_set
6472 elfcpp::PF(0), // segment_flags_clear
6475 elfcpp::STT_NOTYPE, // type
6476 elfcpp::STB_GLOBAL, // binding
6477 elfcpp::STV_HIDDEN, // visibility
6479 Symbol::SEGMENT_START, // offset_from_base
6483 "__rela_iplt_end", // name
6484 elfcpp::PT_LOAD, // segment_type
6485 elfcpp::PF_W, // segment_flags_set
6486 elfcpp::PF(0), // segment_flags_clear
6489 elfcpp::STT_NOTYPE, // type
6490 elfcpp::STB_GLOBAL, // binding
6491 elfcpp::STV_HIDDEN, // visibility
6493 Symbol::SEGMENT_START, // offset_from_base
6498 symtab->define_symbols(layout, 2, syms,
6499 layout->script_options()->saw_sections_clause());
6505 // Perform a relocation.
6507 template<int size, bool big_endian>
6509 Target_aarch64<size, big_endian>::Relocate::relocate(
6510 const Relocate_info<size, big_endian>* relinfo,
6511 Target_aarch64<size, big_endian>* target,
6514 const elfcpp::Rela<size, big_endian>& rela,
6515 unsigned int r_type,
6516 const Sized_symbol<size>* gsym,
6517 const Symbol_value<size>* psymval,
6518 unsigned char* view,
6519 typename elfcpp::Elf_types<size>::Elf_Addr address,
6520 section_size_type /* view_size */)
6525 typedef AArch64_relocate_functions<size, big_endian> Reloc;
6527 const AArch64_reloc_property* reloc_property =
6528 aarch64_reloc_property_table->get_reloc_property(r_type);
6530 if (reloc_property == NULL)
6532 std::string reloc_name =
6533 aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
6534 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6535 _("cannot relocate %s in object file"),
6536 reloc_name.c_str());
6540 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
6542 // Pick the value to use for symbols defined in the PLT.
6543 Symbol_value<size> symval;
6545 && gsym->use_plt_offset(reloc_property->reference_flags()))
6547 symval.set_output_value(target->plt_address_for_global(gsym));
6550 else if (gsym == NULL && psymval->is_ifunc_symbol())
6552 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6553 if (object->local_has_plt_offset(r_sym))
6555 symval.set_output_value(target->plt_address_for_local(object, r_sym));
6560 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6562 // Get the GOT offset if needed.
6563 // For aarch64, the GOT pointer points to the start of the GOT section.
6564 bool have_got_offset = false;
6566 int got_base = (target->got_ != NULL
6567 ? (target->got_->current_data_size() >= 0x8000
6572 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
6573 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
6574 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
6575 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
6576 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
6577 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
6578 case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
6579 case elfcpp::R_AARCH64_GOTREL64:
6580 case elfcpp::R_AARCH64_GOTREL32:
6581 case elfcpp::R_AARCH64_GOT_LD_PREL19:
6582 case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
6583 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6584 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6585 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
6588 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6589 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
6593 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6594 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6595 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
6598 have_got_offset = true;
6605 typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
6606 typename elfcpp::Elf_types<size>::Elf_Addr value;
6609 case elfcpp::R_AARCH64_NONE:
6612 case elfcpp::R_AARCH64_ABS64:
6613 reloc_status = Reloc::template rela_ua<64>(
6614 view, object, psymval, addend, reloc_property);
6617 case elfcpp::R_AARCH64_ABS32:
6618 reloc_status = Reloc::template rela_ua<32>(
6619 view, object, psymval, addend, reloc_property);
6622 case elfcpp::R_AARCH64_ABS16:
6623 reloc_status = Reloc::template rela_ua<16>(
6624 view, object, psymval, addend, reloc_property);
6627 case elfcpp::R_AARCH64_PREL64:
6628 reloc_status = Reloc::template pcrela_ua<64>(
6629 view, object, psymval, addend, address, reloc_property);
6632 case elfcpp::R_AARCH64_PREL32:
6633 reloc_status = Reloc::template pcrela_ua<32>(
6634 view, object, psymval, addend, address, reloc_property);
6637 case elfcpp::R_AARCH64_PREL16:
6638 reloc_status = Reloc::template pcrela_ua<16>(
6639 view, object, psymval, addend, address, reloc_property);
6642 case elfcpp::R_AARCH64_LD_PREL_LO19:
6643 reloc_status = Reloc::template pcrela_general<32>(
6644 view, object, psymval, addend, address, reloc_property);
6647 case elfcpp::R_AARCH64_ADR_PREL_LO21:
6648 reloc_status = Reloc::adr(view, object, psymval, addend,
6649 address, reloc_property);
6652 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
6653 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
6654 reloc_status = Reloc::adrp(view, object, psymval, addend, address,
6658 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
6659 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
6660 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
6661 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
6662 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
6663 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
6664 reloc_status = Reloc::template rela_general<32>(
6665 view, object, psymval, addend, reloc_property);
6668 case elfcpp::R_AARCH64_CALL26:
6669 if (this->skip_call_tls_get_addr_)
6671 // Double check that the TLSGD insn has been optimized away.
6672 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6673 Insntype insn = elfcpp::Swap<32, big_endian>::readval(
6674 reinterpret_cast<Insntype*>(view));
6675 gold_assert((insn & 0xff000000) == 0x91000000);
6677 reloc_status = Reloc::STATUS_OKAY;
6678 this->skip_call_tls_get_addr_ = false;
6679 // Return false to stop further processing this reloc.
6683 case elfcpp::R_AARCH64_JUMP26:
6684 if (Reloc::maybe_apply_stub(r_type, relinfo, rela, view, address,
6685 gsym, psymval, object,
6686 target->stub_group_size_))
6689 case elfcpp::R_AARCH64_TSTBR14:
6690 case elfcpp::R_AARCH64_CONDBR19:
6691 reloc_status = Reloc::template pcrela_general<32>(
6692 view, object, psymval, addend, address, reloc_property);
6695 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6696 gold_assert(have_got_offset);
6697 value = target->got_->address() + got_base + got_offset;
6698 reloc_status = Reloc::adrp(view, value + addend, address);
6701 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6702 gold_assert(have_got_offset);
6703 value = target->got_->address() + got_base + got_offset;
6704 reloc_status = Reloc::template rela_general<32>(
6705 view, value, addend, reloc_property);
6708 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6709 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6710 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6711 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
6712 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6713 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6714 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6715 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
6716 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6717 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6718 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6719 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6720 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6721 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6722 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6723 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6724 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6725 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6726 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6727 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6728 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6729 case elfcpp::R_AARCH64_TLSDESC_CALL:
6730 reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
6731 gsym, psymval, view, address);
6734 // These are dynamic relocations, which are unexpected when linking.
6735 case elfcpp::R_AARCH64_COPY:
6736 case elfcpp::R_AARCH64_GLOB_DAT:
6737 case elfcpp::R_AARCH64_JUMP_SLOT:
6738 case elfcpp::R_AARCH64_RELATIVE:
6739 case elfcpp::R_AARCH64_IRELATIVE:
6740 case elfcpp::R_AARCH64_TLS_DTPREL64:
6741 case elfcpp::R_AARCH64_TLS_DTPMOD64:
6742 case elfcpp::R_AARCH64_TLS_TPREL64:
6743 case elfcpp::R_AARCH64_TLSDESC:
6744 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6745 _("unexpected reloc %u in object file"),
6750 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6751 _("unsupported reloc %s"),
6752 reloc_property->name().c_str());
6756 // Report any errors.
6757 switch (reloc_status)
6759 case Reloc::STATUS_OKAY:
6761 case Reloc::STATUS_OVERFLOW:
6762 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6763 _("relocation overflow in %s"),
6764 reloc_property->name().c_str());
6766 case Reloc::STATUS_BAD_RELOC:
6767 gold_error_at_location(
6770 rela.get_r_offset(),
6771 _("unexpected opcode while processing relocation %s"),
6772 reloc_property->name().c_str());
6782 template<int size, bool big_endian>
6784 typename AArch64_relocate_functions<size, big_endian>::Status
6785 Target_aarch64<size, big_endian>::Relocate::relocate_tls(
6786 const Relocate_info<size, big_endian>* relinfo,
6787 Target_aarch64<size, big_endian>* target,
6789 const elfcpp::Rela<size, big_endian>& rela,
6790 unsigned int r_type, const Sized_symbol<size>* gsym,
6791 const Symbol_value<size>* psymval,
6792 unsigned char* view,
6793 typename elfcpp::Elf_types<size>::Elf_Addr address)
6795 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6796 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6798 Output_segment* tls_segment = relinfo->layout->tls_segment();
6799 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6800 const AArch64_reloc_property* reloc_property =
6801 aarch64_reloc_property_table->get_reloc_property(r_type);
6802 gold_assert(reloc_property != NULL);
6804 const bool is_final = (gsym == NULL
6805 ? !parameters->options().shared()
6806 : gsym->final_value_is_known());
6807 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6808 optimize_tls_reloc(is_final, r_type);
6810 Sized_relobj_file<size, big_endian>* object = relinfo->object;
6811 int tls_got_offset_type;
6814 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6815 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // Global-dynamic
6817 if (tlsopt == tls::TLSOPT_TO_LE)
6819 if (tls_segment == NULL)
6821 gold_assert(parameters->errors()->error_count() > 0
6822 || issue_undefined_symbol_error(gsym));
6823 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6825 return tls_gd_to_le(relinfo, target, rela, r_type, view,
6828 else if (tlsopt == tls::TLSOPT_NONE)
6830 tls_got_offset_type = GOT_TYPE_TLS_PAIR;
6831 // Firstly get the address for the got entry.
6832 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6835 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6836 got_entry_address = target->got_->address() +
6837 gsym->got_offset(tls_got_offset_type);
6841 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6843 object->local_has_got_offset(r_sym, tls_got_offset_type));
6844 got_entry_address = target->got_->address() +
6845 object->local_got_offset(r_sym, tls_got_offset_type);
6848 // Relocate the address into adrp/ld, adrp/add pair.
6851 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6852 return aarch64_reloc_funcs::adrp(
6853 view, got_entry_address + addend, address);
6857 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6858 return aarch64_reloc_funcs::template rela_general<32>(
6859 view, got_entry_address, addend, reloc_property);
6866 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6867 _("unsupported gd_to_ie relaxation on %u"),
6872 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6873 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local-dynamic
6875 if (tlsopt == tls::TLSOPT_TO_LE)
6877 if (tls_segment == NULL)
6879 gold_assert(parameters->errors()->error_count() > 0
6880 || issue_undefined_symbol_error(gsym));
6881 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6883 return this->tls_ld_to_le(relinfo, target, rela, r_type, view,
6887 gold_assert(tlsopt == tls::TLSOPT_NONE);
6888 // Relocate the field with the offset of the GOT entry for
6889 // the module index.
6890 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6891 got_entry_address = (target->got_mod_index_entry(NULL, NULL, NULL) +
6892 target->got_->address());
6896 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6897 return aarch64_reloc_funcs::adrp(
6898 view, got_entry_address + addend, address);
6901 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
6902 return aarch64_reloc_funcs::template rela_general<32>(
6903 view, got_entry_address, addend, reloc_property);
6912 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6913 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6914 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6915 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local-dynamic
6917 AArch64_address value = psymval->value(object, 0);
6918 if (tlsopt == tls::TLSOPT_TO_LE)
6920 if (tls_segment == NULL)
6922 gold_assert(parameters->errors()->error_count() > 0
6923 || issue_undefined_symbol_error(gsym));
6924 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6929 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6930 return aarch64_reloc_funcs::movnz(view, value + addend,
6934 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6935 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6936 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
6937 return aarch64_reloc_funcs::template rela_general<32>(
6938 view, value, addend, reloc_property);
6944 // We should never reach here.
6948 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6949 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial-exec
6951 if (tlsopt == tls::TLSOPT_TO_LE)
6953 if (tls_segment == NULL)
6955 gold_assert(parameters->errors()->error_count() > 0
6956 || issue_undefined_symbol_error(gsym));
6957 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6959 return tls_ie_to_le(relinfo, target, rela, r_type, view,
6962 tls_got_offset_type = GOT_TYPE_TLS_OFFSET;
6964 // Firstly get the address for the got entry.
6965 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6968 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6969 got_entry_address = target->got_->address() +
6970 gsym->got_offset(tls_got_offset_type);
6974 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6976 object->local_has_got_offset(r_sym, tls_got_offset_type));
6977 got_entry_address = target->got_->address() +
6978 object->local_got_offset(r_sym, tls_got_offset_type);
6980 // Relocate the address into adrp/ld, adrp/add pair.
6983 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6984 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
6987 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6988 return aarch64_reloc_funcs::template rela_general<32>(
6989 view, got_entry_address, addend, reloc_property);
6994 // We shall never reach here.
6997 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6998 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6999 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
7000 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7001 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
7002 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
7003 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
7004 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
7006 gold_assert(tls_segment != NULL);
7007 AArch64_address value = psymval->value(object, 0);
7009 if (!parameters->options().shared())
7011 AArch64_address aligned_tcb_size =
7012 align_address(target->tcb_size(),
7013 tls_segment->maximum_alignment());
7014 value += aligned_tcb_size;
7017 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
7018 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
7019 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
7020 return aarch64_reloc_funcs::movnz(view, value + addend,
7023 return aarch64_reloc_funcs::template
7024 rela_general<32>(view,
7031 gold_error(_("%s: unsupported reloc %u "
7032 "in non-static TLSLE mode."),
7033 object->name().c_str(), r_type);
7037 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7038 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7039 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7040 case elfcpp::R_AARCH64_TLSDESC_CALL:
7042 if (tlsopt == tls::TLSOPT_TO_LE)
7044 if (tls_segment == NULL)
7046 gold_assert(parameters->errors()->error_count() > 0
7047 || issue_undefined_symbol_error(gsym));
7048 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7050 return tls_desc_gd_to_le(relinfo, target, rela, r_type,
7055 tls_got_offset_type = (tlsopt == tls::TLSOPT_TO_IE
7056 ? GOT_TYPE_TLS_OFFSET
7057 : GOT_TYPE_TLS_DESC);
7058 unsigned int got_tlsdesc_offset = 0;
7059 if (r_type != elfcpp::R_AARCH64_TLSDESC_CALL
7060 && tlsopt == tls::TLSOPT_NONE)
7062 // We created GOT entries in the .got.tlsdesc portion of the
7063 // .got.plt section, but the offset stored in the symbol is the
7064 // offset within .got.tlsdesc.
7065 got_tlsdesc_offset = (target->got_->data_size()
7066 + target->got_plt_section()->data_size());
7068 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
7071 gold_assert(gsym->has_got_offset(tls_got_offset_type));
7072 got_entry_address = target->got_->address()
7073 + got_tlsdesc_offset
7074 + gsym->got_offset(tls_got_offset_type);
7078 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7080 object->local_has_got_offset(r_sym, tls_got_offset_type));
7081 got_entry_address = target->got_->address() +
7082 got_tlsdesc_offset +
7083 object->local_got_offset(r_sym, tls_got_offset_type);
7085 if (tlsopt == tls::TLSOPT_TO_IE)
7087 if (tls_segment == NULL)
7089 gold_assert(parameters->errors()->error_count() > 0
7090 || issue_undefined_symbol_error(gsym));
7091 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7093 return tls_desc_gd_to_ie(relinfo, target, rela, r_type,
7094 view, psymval, got_entry_address,
7098 // Now do tlsdesc relocation.
7101 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7102 return aarch64_reloc_funcs::adrp(view,
7103 got_entry_address + addend,
7106 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7107 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7108 return aarch64_reloc_funcs::template rela_general<32>(
7109 view, got_entry_address, addend, reloc_property);
7111 case elfcpp::R_AARCH64_TLSDESC_CALL:
7112 return aarch64_reloc_funcs::STATUS_OKAY;
7122 gold_error(_("%s: unsupported TLS reloc %u."),
7123 object->name().c_str(), r_type);
7125 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7126 } // End of relocate_tls.
7129 template<int size, bool big_endian>
7131 typename AArch64_relocate_functions<size, big_endian>::Status
7132 Target_aarch64<size, big_endian>::Relocate::tls_gd_to_le(
7133 const Relocate_info<size, big_endian>* relinfo,
7134 Target_aarch64<size, big_endian>* target,
7135 const elfcpp::Rela<size, big_endian>& rela,
7136 unsigned int r_type,
7137 unsigned char* view,
7138 const Symbol_value<size>* psymval)
7140 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7141 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7142 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7144 Insntype* ip = reinterpret_cast<Insntype*>(view);
7145 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
7146 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
7147 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
7149 if (r_type == elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC)
7151 // This is the 2nd relocs, optimization should already have been
7153 gold_assert((insn1 & 0xfff00000) == 0x91400000);
7154 return aarch64_reloc_funcs::STATUS_OKAY;
7157 // The original sequence is -
7158 // 90000000 adrp x0, 0 <main>
7159 // 91000000 add x0, x0, #0x0
7160 // 94000000 bl 0 <__tls_get_addr>
7161 // optimized to sequence -
7162 // d53bd040 mrs x0, tpidr_el0
7163 // 91400000 add x0, x0, #0x0, lsl #12
7164 // 91000000 add x0, x0, #0x0
7166 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7167 // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
7168 // have to change "bl tls_get_addr", which does not have a corresponding tls
7169 // relocation type. So before proceeding, we need to make sure compiler
7170 // does not change the sequence.
7171 if(!(insn1 == 0x90000000 // adrp x0,0
7172 && insn2 == 0x91000000 // add x0, x0, #0x0
7173 && insn3 == 0x94000000)) // bl 0
7175 // Ideally we should give up gd_to_le relaxation and do gd access.
7176 // However the gd_to_le relaxation decision has been made early
7177 // in the scan stage, where we did not allocate any GOT entry for
7178 // this symbol. Therefore we have to exit and report error now.
7179 gold_error(_("unexpected reloc insn sequence while relaxing "
7180 "tls gd to le for reloc %u."), r_type);
7181 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7185 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
7186 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
7187 insn3 = 0x91000000; // add x0, x0, #0x0
7188 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
7189 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
7190 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
7192 // Calculate tprel value.
7193 Output_segment* tls_segment = relinfo->layout->tls_segment();
7194 gold_assert(tls_segment != NULL);
7195 AArch64_address value = psymval->value(relinfo->object, 0);
7196 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7197 AArch64_address aligned_tcb_size =
7198 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7199 AArch64_address x = value + aligned_tcb_size;
7201 // After new insns are written, apply TLSLE relocs.
7202 const AArch64_reloc_property* rp1 =
7203 aarch64_reloc_property_table->get_reloc_property(
7204 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
7205 const AArch64_reloc_property* rp2 =
7206 aarch64_reloc_property_table->get_reloc_property(
7207 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
7208 gold_assert(rp1 != NULL && rp2 != NULL);
7210 typename aarch64_reloc_funcs::Status s1 =
7211 aarch64_reloc_funcs::template rela_general<32>(view + 4,
7215 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
7218 typename aarch64_reloc_funcs::Status s2 =
7219 aarch64_reloc_funcs::template rela_general<32>(view + 8,
7224 this->skip_call_tls_get_addr_ = true;
7226 } // End of tls_gd_to_le
7229 template<int size, bool big_endian>
7231 typename AArch64_relocate_functions<size, big_endian>::Status
7232 Target_aarch64<size, big_endian>::Relocate::tls_ld_to_le(
7233 const Relocate_info<size, big_endian>* relinfo,
7234 Target_aarch64<size, big_endian>* target,
7235 const elfcpp::Rela<size, big_endian>& rela,
7236 unsigned int r_type,
7237 unsigned char* view,
7238 const Symbol_value<size>* psymval)
7240 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7241 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7242 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7244 Insntype* ip = reinterpret_cast<Insntype*>(view);
7245 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
7246 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
7247 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
7249 if (r_type == elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC)
7251 // This is the 2nd relocs, optimization should already have been
7253 gold_assert((insn1 & 0xfff00000) == 0x91400000);
7254 return aarch64_reloc_funcs::STATUS_OKAY;
7257 // The original sequence is -
7258 // 90000000 adrp x0, 0 <main>
7259 // 91000000 add x0, x0, #0x0
7260 // 94000000 bl 0 <__tls_get_addr>
7261 // optimized to sequence -
7262 // d53bd040 mrs x0, tpidr_el0
7263 // 91400000 add x0, x0, #0x0, lsl #12
7264 // 91000000 add x0, x0, #0x0
7266 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7267 // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
7268 // have to change "bl tls_get_addr", which does not have a corresponding tls
7269 // relocation type. So before proceeding, we need to make sure compiler
7270 // does not change the sequence.
7271 if(!(insn1 == 0x90000000 // adrp x0,0
7272 && insn2 == 0x91000000 // add x0, x0, #0x0
7273 && insn3 == 0x94000000)) // bl 0
7275 // Ideally we should give up gd_to_le relaxation and do gd access.
7276 // However the gd_to_le relaxation decision has been made early
7277 // in the scan stage, where we did not allocate any GOT entry for
7278 // this symbol. Therefore we have to exit and report error now.
7279 gold_error(_("unexpected reloc insn sequence while relaxing "
7280 "tls gd to le for reloc %u."), r_type);
7281 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7285 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
7286 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
7287 insn3 = 0x91000000; // add x0, x0, #0x0
7288 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
7289 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
7290 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
7292 // Calculate tprel value.
7293 Output_segment* tls_segment = relinfo->layout->tls_segment();
7294 gold_assert(tls_segment != NULL);
7295 AArch64_address value = psymval->value(relinfo->object, 0);
7296 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7297 AArch64_address aligned_tcb_size =
7298 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7299 AArch64_address x = value + aligned_tcb_size;
7301 // After new insns are written, apply TLSLE relocs.
7302 const AArch64_reloc_property* rp1 =
7303 aarch64_reloc_property_table->get_reloc_property(
7304 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
7305 const AArch64_reloc_property* rp2 =
7306 aarch64_reloc_property_table->get_reloc_property(
7307 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
7308 gold_assert(rp1 != NULL && rp2 != NULL);
7310 typename aarch64_reloc_funcs::Status s1 =
7311 aarch64_reloc_funcs::template rela_general<32>(view + 4,
7315 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
7318 typename aarch64_reloc_funcs::Status s2 =
7319 aarch64_reloc_funcs::template rela_general<32>(view + 8,
7324 this->skip_call_tls_get_addr_ = true;
7327 } // End of tls_ld_to_le
7329 template<int size, bool big_endian>
7331 typename AArch64_relocate_functions<size, big_endian>::Status
7332 Target_aarch64<size, big_endian>::Relocate::tls_ie_to_le(
7333 const Relocate_info<size, big_endian>* relinfo,
7334 Target_aarch64<size, big_endian>* target,
7335 const elfcpp::Rela<size, big_endian>& rela,
7336 unsigned int r_type,
7337 unsigned char* view,
7338 const Symbol_value<size>* psymval)
7340 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7341 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7342 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7344 AArch64_address value = psymval->value(relinfo->object, 0);
7345 Output_segment* tls_segment = relinfo->layout->tls_segment();
7346 AArch64_address aligned_tcb_address =
7347 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7348 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7349 AArch64_address x = value + addend + aligned_tcb_address;
7350 // "x" is the offset to tp, we can only do this if x is within
7351 // range [0, 2^32-1]
7352 if (!(size == 32 || (size == 64 && (static_cast<uint64_t>(x) >> 32) == 0)))
7354 gold_error(_("TLS variable referred by reloc %u is too far from TP."),
7356 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7359 Insntype* ip = reinterpret_cast<Insntype*>(view);
7360 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
7363 if (r_type == elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21)
7366 regno = (insn & 0x1f);
7367 newinsn = (0xd2a00000 | regno) | (((x >> 16) & 0xffff) << 5);
7369 else if (r_type == elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
7372 regno = (insn & 0x1f);
7373 gold_assert(regno == ((insn >> 5) & 0x1f));
7374 newinsn = (0xf2800000 | regno) | ((x & 0xffff) << 5);
7379 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7380 return aarch64_reloc_funcs::STATUS_OKAY;
7381 } // End of tls_ie_to_le
7384 template<int size, bool big_endian>
7386 typename AArch64_relocate_functions<size, big_endian>::Status
7387 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_le(
7388 const Relocate_info<size, big_endian>* relinfo,
7389 Target_aarch64<size, big_endian>* target,
7390 const elfcpp::Rela<size, big_endian>& rela,
7391 unsigned int r_type,
7392 unsigned char* view,
7393 const Symbol_value<size>* psymval)
7395 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
7396 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7397 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7399 // TLSDESC-GD sequence is like:
7400 // adrp x0, :tlsdesc:v1
7401 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7402 // add x0, x0, :tlsdesc_lo12:v1
7405 // After desc_gd_to_le optimization, the sequence will be like:
7406 // movz x0, #0x0, lsl #16
7411 // Calculate tprel value.
7412 Output_segment* tls_segment = relinfo->layout->tls_segment();
7413 gold_assert(tls_segment != NULL);
7414 Insntype* ip = reinterpret_cast<Insntype*>(view);
7415 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7416 AArch64_address value = psymval->value(relinfo->object, addend);
7417 AArch64_address aligned_tcb_size =
7418 align_address(target->tcb_size(), tls_segment->maximum_alignment());
7419 AArch64_address x = value + aligned_tcb_size;
7420 // x is the offset to tp, we can only do this if x is within range
7421 // [0, 2^32-1]. If x is out of range, fail and exit.
7422 if (size == 64 && (static_cast<uint64_t>(x) >> 32) != 0)
7424 gold_error(_("TLS variable referred by reloc %u is too far from TP. "
7425 "We Can't do gd_to_le relaxation.\n"), r_type);
7426 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
7431 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7432 case elfcpp::R_AARCH64_TLSDESC_CALL:
7434 newinsn = 0xd503201f;
7437 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7439 newinsn = 0xd2a00000 | (((x >> 16) & 0xffff) << 5);
7442 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7444 newinsn = 0xf2800000 | ((x & 0xffff) << 5);
7448 gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
7452 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7453 return aarch64_reloc_funcs::STATUS_OKAY;
7454 } // End of tls_desc_gd_to_le
7457 template<int size, bool big_endian>
7459 typename AArch64_relocate_functions<size, big_endian>::Status
7460 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_ie(
7461 const Relocate_info<size, big_endian>* /* relinfo */,
7462 Target_aarch64<size, big_endian>* /* target */,
7463 const elfcpp::Rela<size, big_endian>& rela,
7464 unsigned int r_type,
7465 unsigned char* view,
7466 const Symbol_value<size>* /* psymval */,
7467 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address,
7468 typename elfcpp::Elf_types<size>::Elf_Addr address)
7470 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7471 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7473 // TLSDESC-GD sequence is like:
7474 // adrp x0, :tlsdesc:v1
7475 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7476 // add x0, x0, :tlsdesc_lo12:v1
7479 // After desc_gd_to_ie optimization, the sequence will be like:
7480 // adrp x0, :tlsie:v1
7481 // ldr x0, [x0, :tlsie_lo12:v1]
7485 Insntype* ip = reinterpret_cast<Insntype*>(view);
7486 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7490 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7491 case elfcpp::R_AARCH64_TLSDESC_CALL:
7493 newinsn = 0xd503201f;
7494 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7497 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7499 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
7504 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7506 // Set ldr target register to be x0.
7507 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
7509 elfcpp::Swap<32, big_endian>::writeval(ip, insn);
7511 const AArch64_reloc_property* reloc_property =
7512 aarch64_reloc_property_table->get_reloc_property(
7513 elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
7514 return aarch64_reloc_funcs::template rela_general<32>(
7515 view, got_entry_address, addend, reloc_property);
7520 gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
7524 return aarch64_reloc_funcs::STATUS_OKAY;
7525 } // End of tls_desc_gd_to_ie
7527 // Relocate section data.
7529 template<int size, bool big_endian>
7531 Target_aarch64<size, big_endian>::relocate_section(
7532 const Relocate_info<size, big_endian>* relinfo,
7533 unsigned int sh_type,
7534 const unsigned char* prelocs,
7536 Output_section* output_section,
7537 bool needs_special_offset_handling,
7538 unsigned char* view,
7539 typename elfcpp::Elf_types<size>::Elf_Addr address,
7540 section_size_type view_size,
7541 const Reloc_symbol_changes* reloc_symbol_changes)
7543 gold_assert(sh_type == elfcpp::SHT_RELA);
7544 typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
7545 gold::relocate_section<size, big_endian, Target_aarch64, elfcpp::SHT_RELA,
7546 AArch64_relocate, gold::Default_comdat_behavior>(
7552 needs_special_offset_handling,
7556 reloc_symbol_changes);
7559 // Return the size of a relocation while scanning during a relocatable
7562 template<int size, bool big_endian>
7564 Target_aarch64<size, big_endian>::Relocatable_size_for_reloc::
7569 // We will never support SHT_REL relocations.
7574 // Scan the relocs during a relocatable link.
7576 template<int size, bool big_endian>
7578 Target_aarch64<size, big_endian>::scan_relocatable_relocs(
7579 Symbol_table* symtab,
7581 Sized_relobj_file<size, big_endian>* object,
7582 unsigned int data_shndx,
7583 unsigned int sh_type,
7584 const unsigned char* prelocs,
7586 Output_section* output_section,
7587 bool needs_special_offset_handling,
7588 size_t local_symbol_count,
7589 const unsigned char* plocal_symbols,
7590 Relocatable_relocs* rr)
7592 gold_assert(sh_type == elfcpp::SHT_RELA);
7594 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
7595 Relocatable_size_for_reloc> Scan_relocatable_relocs;
7597 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7598 Scan_relocatable_relocs>(
7606 needs_special_offset_handling,
7612 // Relocate a section during a relocatable link.
7614 template<int size, bool big_endian>
7616 Target_aarch64<size, big_endian>::relocate_relocs(
7617 const Relocate_info<size, big_endian>* relinfo,
7618 unsigned int sh_type,
7619 const unsigned char* prelocs,
7621 Output_section* output_section,
7622 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7623 const Relocatable_relocs* rr,
7624 unsigned char* view,
7625 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
7626 section_size_type view_size,
7627 unsigned char* reloc_view,
7628 section_size_type reloc_view_size)
7630 gold_assert(sh_type == elfcpp::SHT_RELA);
7632 gold::relocate_relocs<size, big_endian, elfcpp::SHT_RELA>(
7637 offset_in_output_section,
7647 // Return whether this is a 3-insn erratum sequence.
7649 template<int size, bool big_endian>
7651 Target_aarch64<size, big_endian>::is_erratum_843419_sequence(
7652 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
7653 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
7654 typename elfcpp::Swap<32,big_endian>::Valtype insn3)
7659 // The 2nd insn is a single register load or store; or register pair
7661 if (Insn_utilities::aarch64_mem_op_p(insn2, &rt1, &rt2, &pair, &load)
7662 && (!pair || (pair && !load)))
7664 // The 3rd insn is a load or store instruction from the "Load/store
7665 // register (unsigned immediate)" encoding class, using Rn as the
7666 // base address register.
7667 if (Insn_utilities::aarch64_ldst_uimm(insn3)
7668 && (Insn_utilities::aarch64_rn(insn3)
7669 == Insn_utilities::aarch64_rd(insn1)))
7676 // Scan erratum for section SHNDX range
7677 // [output_address + span_start, output_address + span_end).
7679 template<int size, bool big_endian>
7681 Target_aarch64<size, big_endian>::scan_erratum_843419_span(
7682 AArch64_relobj<size, big_endian>* relobj,
7684 const section_size_type span_start,
7685 const section_size_type span_end,
7686 unsigned char* input_view,
7687 Address output_address)
7689 typedef typename Insn_utilities::Insntype Insntype;
7691 // Adjust output_address and view to the start of span.
7692 output_address += span_start;
7693 input_view += span_start;
7695 if ((output_address & 0x03) != 0)
7698 section_size_type offset = 0;
7699 section_size_type span_length = span_end - span_start;
7700 // The first instruction must be ending at 0xFF8 or 0xFFC.
7701 unsigned int page_offset = output_address & 0xFFF;
7702 // Make sure starting position, that is "output_address+offset",
7703 // starts at page position 0xff8 or 0xffc.
7704 if (page_offset < 0xff8)
7705 offset = 0xff8 - page_offset;
7706 while (offset + 3 * Insn_utilities::BYTES_PER_INSN <= span_length)
7708 Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
7709 Insntype insn1 = ip[0];
7710 if (Insn_utilities::is_adrp(insn1))
7712 Insntype insn2 = ip[1];
7713 Insntype insn3 = ip[2];
7714 Insntype erratum_insn;
7715 unsigned insn_offset;
7716 bool do_report = false;
7717 if (is_erratum_843419_sequence(insn1, insn2, insn3))
7720 erratum_insn = insn3;
7721 insn_offset = 2 * Insn_utilities::BYTES_PER_INSN;
7723 else if (offset + 4 * Insn_utilities::BYTES_PER_INSN <= span_length)
7725 // Optionally we can have an insn between ins2 and ins3
7726 Insntype insn_opt = ip[2];
7727 // And insn_opt must not be a branch.
7728 if (!Insn_utilities::aarch64_b(insn_opt)
7729 && !Insn_utilities::aarch64_bl(insn_opt)
7730 && !Insn_utilities::aarch64_blr(insn_opt)
7731 && !Insn_utilities::aarch64_br(insn_opt))
7733 // And insn_opt must not write to dest reg in insn1. However
7734 // we do a conservative scan, which means we may fix/report
7735 // more than necessary, but it doesn't hurt.
7737 Insntype insn4 = ip[3];
7738 if (is_erratum_843419_sequence(insn1, insn2, insn4))
7741 erratum_insn = insn4;
7742 insn_offset = 3 * Insn_utilities::BYTES_PER_INSN;
7748 gold_warning(_("Erratum 843419 found and fixed at \"%s\", "
7749 "section %d, offset 0x%08x."),
7750 relobj->name().c_str(), shndx,
7751 (unsigned int)(span_start + offset));
7752 unsigned int errata_insn_offset =
7753 span_start + offset + insn_offset;
7754 The_stub_table* stub_table = relobj->stub_table(shndx);
7755 gold_assert(stub_table != NULL);
7756 if (stub_table->find_erratum_stub(relobj,
7758 errata_insn_offset) == NULL)
7760 The_erratum_stub* stub = new The_erratum_stub(
7761 relobj, ST_E_843419, shndx,
7762 errata_insn_offset);
7763 Address erratum_address =
7764 output_address + offset + insn_offset;
7765 // Stub destination address is the next insn after the
7767 Address dest_address = erratum_address
7768 + Insn_utilities::BYTES_PER_INSN;
7769 stub->set_erratum_insn(erratum_insn);
7770 stub->set_erratum_address(erratum_address);
7771 stub->set_destination_address(dest_address);
7772 stub_table->add_erratum_stub(stub);
7777 // Advance to next candidate instruction. We only consider instruction
7778 // sequences starting at a page offset of 0xff8 or 0xffc.
7779 page_offset = (output_address + offset) & 0xfff;
7780 if (page_offset == 0xff8)
7782 else // (page_offset == 0xffc), we move to next page's 0xff8.
7785 } // End of "Target_aarch64::scan_erratum_843419_span".
7788 // The selector for aarch64 object files.
7790 template<int size, bool big_endian>
7791 class Target_selector_aarch64 : public Target_selector
7794 Target_selector_aarch64();
7797 do_instantiate_target()
7798 { return new Target_aarch64<size, big_endian>(); }
7802 Target_selector_aarch64<32, true>::Target_selector_aarch64()
7803 : Target_selector(elfcpp::EM_AARCH64, 32, true,
7804 "elf32-bigaarch64", "aarch64_elf32_be_vec")
7808 Target_selector_aarch64<32, false>::Target_selector_aarch64()
7809 : Target_selector(elfcpp::EM_AARCH64, 32, false,
7810 "elf32-littleaarch64", "aarch64_elf32_le_vec")
7814 Target_selector_aarch64<64, true>::Target_selector_aarch64()
7815 : Target_selector(elfcpp::EM_AARCH64, 64, true,
7816 "elf64-bigaarch64", "aarch64_elf64_be_vec")
7820 Target_selector_aarch64<64, false>::Target_selector_aarch64()
7821 : Target_selector(elfcpp::EM_AARCH64, 64, false,
7822 "elf64-littleaarch64", "aarch64_elf64_le_vec")
7825 Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
7826 Target_selector_aarch64<32, false> target_selector_aarch64elf32;
7827 Target_selector_aarch64<64, true> target_selector_aarch64elfb;
7828 Target_selector_aarch64<64, false> target_selector_aarch64elf;
7830 } // End anonymous namespace.