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.
30 #include "parameters.h"
37 #include "copy-relocs.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
46 #include "aarch64-reloc-property.h"
48 // The first three .got.plt entries are reserved.
49 const int32_t AARCH64_GOTPLT_RESERVE_COUNT = 3;
57 template<int size, bool big_endian>
58 class Output_data_plt_aarch64;
60 template<int size, bool big_endian>
61 class Output_data_plt_aarch64_standard;
63 template<int size, bool big_endian>
66 template<int size, bool big_endian>
67 class AArch64_relocate_functions;
69 // Utility class dealing with insns. This is ported from macros in
70 // bfd/elfnn-aarch64.cc, but wrapped inside a class as static members. This
71 // class is used in erratum sequence scanning.
73 template<bool big_endian>
74 class AArch64_insn_utilities
77 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
79 static const int BYTES_PER_INSN = 4;
82 aarch64_bit(Insntype insn, int pos)
83 { return ((1 << pos) & insn) >> pos; }
86 aarch64_bits(Insntype insn, int pos, int l)
87 { return (insn >> pos) & ((1 << l) - 1); }
90 is_adrp(const Insntype insn)
91 { return (insn & 0x9F000000) == 0x90000000; }
94 aarch64_rm(const Insntype insn)
95 { return aarch64_bits(insn, 16, 5); }
98 aarch64_rn(const Insntype insn)
99 { return aarch64_bits(insn, 5, 5); }
102 aarch64_rd(const Insntype insn)
103 { return aarch64_bits(insn, 0, 5); }
106 aarch64_rt(const Insntype insn)
107 { return aarch64_bits(insn, 0, 5); }
110 aarch64_rt2(const Insntype insn)
111 { return aarch64_bits(insn, 10, 5); }
114 aarch64_b(const Insntype insn)
115 { return (insn & 0xFC000000) == 0x14000000; }
118 aarch64_bl(const Insntype insn)
119 { return (insn & 0xFC000000) == 0x94000000; }
122 aarch64_blr(const Insntype insn)
123 { return (insn & 0xFFFFFC1F) == 0xD63F0000; }
126 aarch64_br(const Insntype insn)
127 { return (insn & 0xFFFFFC1F) == 0xD61F0000; }
129 // All ld/st ops. See C4-182 of the ARM ARM. The encoding space for
130 // LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops.
132 aarch64_ld(Insntype insn) { return aarch64_bit(insn, 22) == 1; }
135 aarch64_ldst(Insntype insn)
136 { return (insn & 0x0a000000) == 0x08000000; }
139 aarch64_ldst_ex(Insntype insn)
140 { return (insn & 0x3f000000) == 0x08000000; }
143 aarch64_ldst_pcrel(Insntype insn)
144 { return (insn & 0x3b000000) == 0x18000000; }
147 aarch64_ldst_nap(Insntype insn)
148 { return (insn & 0x3b800000) == 0x28000000; }
151 aarch64_ldstp_pi(Insntype insn)
152 { return (insn & 0x3b800000) == 0x28800000; }
155 aarch64_ldstp_o(Insntype insn)
156 { return (insn & 0x3b800000) == 0x29000000; }
159 aarch64_ldstp_pre(Insntype insn)
160 { return (insn & 0x3b800000) == 0x29800000; }
163 aarch64_ldst_ui(Insntype insn)
164 { return (insn & 0x3b200c00) == 0x38000000; }
167 aarch64_ldst_piimm(Insntype insn)
168 { return (insn & 0x3b200c00) == 0x38000400; }
171 aarch64_ldst_u(Insntype insn)
172 { return (insn & 0x3b200c00) == 0x38000800; }
175 aarch64_ldst_preimm(Insntype insn)
176 { return (insn & 0x3b200c00) == 0x38000c00; }
179 aarch64_ldst_ro(Insntype insn)
180 { return (insn & 0x3b200c00) == 0x38200800; }
183 aarch64_ldst_uimm(Insntype insn)
184 { return (insn & 0x3b000000) == 0x39000000; }
187 aarch64_ldst_simd_m(Insntype insn)
188 { return (insn & 0xbfbf0000) == 0x0c000000; }
191 aarch64_ldst_simd_m_pi(Insntype insn)
192 { return (insn & 0xbfa00000) == 0x0c800000; }
195 aarch64_ldst_simd_s(Insntype insn)
196 { return (insn & 0xbf9f0000) == 0x0d000000; }
199 aarch64_ldst_simd_s_pi(Insntype insn)
200 { return (insn & 0xbf800000) == 0x0d800000; }
202 // Classify an INSN if it is indeed a load/store. Return true if INSN is a
203 // LD/ST instruction otherwise return false. For scalar LD/ST instructions
204 // PAIR is FALSE, RT is returned and RT2 is set equal to RT. For LD/ST pair
205 // instructions PAIR is TRUE, RT and RT2 are returned.
207 aarch64_mem_op_p(Insntype insn, unsigned int *rt, unsigned int *rt2,
208 bool *pair, bool *load)
216 /* Bail out quickly if INSN doesn't fall into the the load-store
218 if (!aarch64_ldst (insn))
223 if (aarch64_ldst_ex (insn))
225 *rt = aarch64_rt (insn);
227 if (aarch64_bit (insn, 21) == 1)
230 *rt2 = aarch64_rt2 (insn);
232 *load = aarch64_ld (insn);
235 else if (aarch64_ldst_nap (insn)
236 || aarch64_ldstp_pi (insn)
237 || aarch64_ldstp_o (insn)
238 || aarch64_ldstp_pre (insn))
241 *rt = aarch64_rt (insn);
242 *rt2 = aarch64_rt2 (insn);
243 *load = aarch64_ld (insn);
246 else if (aarch64_ldst_pcrel (insn)
247 || aarch64_ldst_ui (insn)
248 || aarch64_ldst_piimm (insn)
249 || aarch64_ldst_u (insn)
250 || aarch64_ldst_preimm (insn)
251 || aarch64_ldst_ro (insn)
252 || aarch64_ldst_uimm (insn))
254 *rt = aarch64_rt (insn);
256 if (aarch64_ldst_pcrel (insn))
258 opc = aarch64_bits (insn, 22, 2);
259 v = aarch64_bit (insn, 26);
260 opc_v = opc | (v << 2);
261 *load = (opc_v == 1 || opc_v == 2 || opc_v == 3
262 || opc_v == 5 || opc_v == 7);
265 else if (aarch64_ldst_simd_m (insn)
266 || aarch64_ldst_simd_m_pi (insn))
268 *rt = aarch64_rt (insn);
269 *load = aarch64_bit (insn, 22);
270 opcode = (insn >> 12) & 0xf;
297 else if (aarch64_ldst_simd_s (insn)
298 || aarch64_ldst_simd_s_pi (insn))
300 *rt = aarch64_rt (insn);
301 r = (insn >> 21) & 1;
302 *load = aarch64_bit (insn, 22);
303 opcode = (insn >> 13) & 0x7;
315 *rt2 = *rt + (r == 0 ? 2 : 3);
323 *rt2 = *rt + (r == 0 ? 2 : 3);
336 // Output_data_got_aarch64 class.
338 template<int size, bool big_endian>
339 class Output_data_got_aarch64 : public Output_data_got<size, big_endian>
342 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
343 Output_data_got_aarch64(Symbol_table* symtab, Layout* layout)
344 : Output_data_got<size, big_endian>(),
345 symbol_table_(symtab), layout_(layout)
348 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
349 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
350 // applied in a static link.
352 add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
353 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
356 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
357 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
358 // relocation that needs to be applied in a static link.
360 add_static_reloc(unsigned int got_offset, unsigned int r_type,
361 Sized_relobj_file<size, big_endian>* relobj,
364 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
370 // Write out the GOT table.
372 do_write(Output_file* of) {
373 // The first entry in the GOT is the address of the .dynamic section.
374 gold_assert(this->data_size() >= size / 8);
375 Output_section* dynamic = this->layout_->dynamic_section();
376 Valtype dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
377 this->replace_constant(0, dynamic_addr);
378 Output_data_got<size, big_endian>::do_write(of);
380 // Handling static relocs
381 if (this->static_relocs_.empty())
384 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
386 gold_assert(parameters->doing_static_link());
387 const off_t offset = this->offset();
388 const section_size_type oview_size =
389 convert_to_section_size_type(this->data_size());
390 unsigned char* const oview = of->get_output_view(offset, oview_size);
392 Output_segment* tls_segment = this->layout_->tls_segment();
393 gold_assert(tls_segment != NULL);
395 AArch64_address aligned_tcb_address =
396 align_address(Target_aarch64<size, big_endian>::TCB_SIZE,
397 tls_segment->maximum_alignment());
399 for (size_t i = 0; i < this->static_relocs_.size(); ++i)
401 Static_reloc& reloc(this->static_relocs_[i]);
402 AArch64_address value;
404 if (!reloc.symbol_is_global())
406 Sized_relobj_file<size, big_endian>* object = reloc.relobj();
407 const Symbol_value<size>* psymval =
408 reloc.relobj()->local_symbol(reloc.index());
410 // We are doing static linking. Issue an error and skip this
411 // relocation if the symbol is undefined or in a discarded_section.
413 unsigned int shndx = psymval->input_shndx(&is_ordinary);
414 if ((shndx == elfcpp::SHN_UNDEF)
416 && shndx != elfcpp::SHN_UNDEF
417 && !object->is_section_included(shndx)
418 && !this->symbol_table_->is_section_folded(object, shndx)))
420 gold_error(_("undefined or discarded local symbol %u from "
421 " object %s in GOT"),
422 reloc.index(), reloc.relobj()->name().c_str());
425 value = psymval->value(object, 0);
429 const Symbol* gsym = reloc.symbol();
430 gold_assert(gsym != NULL);
431 if (gsym->is_forwarder())
432 gsym = this->symbol_table_->resolve_forwards(gsym);
434 // We are doing static linking. Issue an error and skip this
435 // relocation if the symbol is undefined or in a discarded_section
436 // unless it is a weakly_undefined symbol.
437 if ((gsym->is_defined_in_discarded_section()
438 || gsym->is_undefined())
439 && !gsym->is_weak_undefined())
441 gold_error(_("undefined or discarded symbol %s in GOT"),
446 if (!gsym->is_weak_undefined())
448 const Sized_symbol<size>* sym =
449 static_cast<const Sized_symbol<size>*>(gsym);
450 value = sym->value();
456 unsigned got_offset = reloc.got_offset();
457 gold_assert(got_offset < oview_size);
459 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
460 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
462 switch (reloc.r_type())
464 case elfcpp::R_AARCH64_TLS_DTPREL64:
467 case elfcpp::R_AARCH64_TLS_TPREL64:
468 x = value + aligned_tcb_address;
473 elfcpp::Swap<size, big_endian>::writeval(wv, x);
476 of->write_output_view(offset, oview_size, oview);
480 // Symbol table of the output object.
481 Symbol_table* symbol_table_;
482 // A pointer to the Layout class, so that we can find the .dynamic
483 // section when we write out the GOT section.
486 // This class represent dynamic relocations that need to be applied by
487 // gold because we are using TLS relocations in a static link.
491 Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
492 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
493 { this->u_.global.symbol = gsym; }
495 Static_reloc(unsigned int got_offset, unsigned int r_type,
496 Sized_relobj_file<size, big_endian>* relobj, unsigned int index)
497 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
499 this->u_.local.relobj = relobj;
500 this->u_.local.index = index;
503 // Return the GOT offset.
506 { return this->got_offset_; }
511 { return this->r_type_; }
513 // Whether the symbol is global or not.
515 symbol_is_global() const
516 { return this->symbol_is_global_; }
518 // For a relocation against a global symbol, the global symbol.
522 gold_assert(this->symbol_is_global_);
523 return this->u_.global.symbol;
526 // For a relocation against a local symbol, the defining object.
527 Sized_relobj_file<size, big_endian>*
530 gold_assert(!this->symbol_is_global_);
531 return this->u_.local.relobj;
534 // For a relocation against a local symbol, the local symbol index.
538 gold_assert(!this->symbol_is_global_);
539 return this->u_.local.index;
543 // GOT offset of the entry to which this relocation is applied.
544 unsigned int got_offset_;
545 // Type of relocation.
546 unsigned int r_type_;
547 // Whether this relocation is against a global symbol.
548 bool symbol_is_global_;
549 // A global or local symbol.
554 // For a global symbol, the symbol itself.
559 // For a local symbol, the object defining the symbol.
560 Sized_relobj_file<size, big_endian>* relobj;
561 // For a local symbol, the symbol index.
565 }; // End of inner class Static_reloc
567 std::vector<Static_reloc> static_relocs_;
568 }; // End of Output_data_got_aarch64
571 template<int size, bool big_endian>
572 class AArch64_input_section;
575 template<int size, bool big_endian>
576 class AArch64_output_section;
581 template<int size, bool big_endian>
585 typedef Reloc_stub<size, big_endian> This;
586 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
588 // Do not change the value of the enums, they are used to index into
594 // Using adrp/add pair, 4 insns (including alignment) without mem access,
595 // the fastest stub. This has a limited jump distance, which is tested by
596 // aarch64_valid_for_adrp_p.
599 // Using ldr-absolute-address/br-register, 4 insns with 1 mem access,
600 // unlimited in jump distance.
601 ST_LONG_BRANCH_ABS = 2,
603 // Using ldr/calculate-pcrel/jump, 8 insns (including alignment) with 1 mem
604 // access, slowest one. Only used in position independent executables.
605 ST_LONG_BRANCH_PCREL = 3,
609 // Branch range. This is used to calculate the section group size, as well as
610 // determine whether a stub is needed.
611 static const int MAX_BRANCH_OFFSET = ((1 << 25) - 1) << 2;
612 static const int MIN_BRANCH_OFFSET = -((1 << 25) << 2);
614 // Constant used to determine if an offset fits in the adrp instruction
616 static const int MAX_ADRP_IMM = (1 << 20) - 1;
617 static const int MIN_ADRP_IMM = -(1 << 20);
619 static const int BYTES_PER_INSN = 4;
620 static const int STUB_ADDR_ALIGN = 4;
622 // Determine whether the offset fits in the jump/branch instruction.
624 aarch64_valid_branch_offset_p(int64_t offset)
625 { return offset >= MIN_BRANCH_OFFSET && offset <= MAX_BRANCH_OFFSET; }
627 // Determine whether the offset fits in the adrp immediate field.
629 aarch64_valid_for_adrp_p(AArch64_address location, AArch64_address dest)
631 typedef AArch64_relocate_functions<size, big_endian> Reloc;
632 int64_t adrp_imm = (Reloc::Page(dest) - Reloc::Page(location)) >> 12;
633 return adrp_imm >= MIN_ADRP_IMM && adrp_imm <= MAX_ADRP_IMM;
636 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
639 stub_type_for_reloc(unsigned int r_type, AArch64_address address,
640 AArch64_address target);
642 Reloc_stub(Stub_type stub_type)
643 : stub_type_(stub_type), offset_(invalid_offset),
644 destination_address_(invalid_address)
650 // Return offset of code stub from beginning of its containing stub table.
654 gold_assert(this->offset_ != invalid_offset);
655 return this->offset_;
658 // Set offset of code stub from beginning of its containing stub table.
660 set_offset(section_offset_type offset)
661 { this->offset_ = offset; }
663 // Return destination address.
665 destination_address() const
667 gold_assert(this->destination_address_ != this->invalid_address);
668 return this->destination_address_;
671 // Set destination address.
673 set_destination_address(AArch64_address address)
675 gold_assert(address != this->invalid_address);
676 this->destination_address_ = address;
679 // Reset the destination address.
681 reset_destination_address()
682 { this->destination_address_ = this->invalid_address; }
684 // Return the stub type.
687 { return stub_type_; }
689 // Return the stub size.
692 { return this->stub_insn_number() * BYTES_PER_INSN; }
694 // Return the instruction number of this stub instance.
696 stub_insn_number() const
697 { return stub_insns_[this->stub_type_][0]; }
699 // Note the first "insn" is the number of total insns in this array.
702 { return stub_insns_[this->stub_type_]; }
704 // Write stub to output file.
706 write(unsigned char* view, section_size_type view_size)
707 { this->do_write(view, view_size); }
709 // The key class used to index the stub instance in the stub table's stub map.
713 Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
714 unsigned int r_sym, int32_t addend)
715 : stub_type_(stub_type), addend_(addend)
719 this->r_sym_ = Reloc_stub::invalid_index;
720 this->u_.symbol = symbol;
724 gold_assert(relobj != NULL && r_sym != invalid_index);
725 this->r_sym_ = r_sym;
726 this->u_.relobj = relobj;
736 { return this->stub_type_; }
738 // Return the local symbol index or invalid_index.
741 { return this->r_sym_; }
743 // Return the symbol if there is one.
746 { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
748 // Return the relobj if there is one.
751 { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
753 // Whether this equals to another key k.
755 eq(const Key& k) const
757 return ((this->stub_type_ == k.stub_type_)
758 && (this->r_sym_ == k.r_sym_)
759 && ((this->r_sym_ != Reloc_stub::invalid_index)
760 ? (this->u_.relobj == k.u_.relobj)
761 : (this->u_.symbol == k.u_.symbol))
762 && (this->addend_ == k.addend_));
765 // Return a hash value.
769 size_t name_hash_value = gold::string_hash<char>(
770 (this->r_sym_ != Reloc_stub::invalid_index)
771 ? this->u_.relobj->name().c_str()
772 : this->u_.symbol->name());
773 // We only have 4 stub types.
774 size_t stub_type_hash_value = 0x03 & this->stub_type_;
775 return (name_hash_value
776 ^ stub_type_hash_value
777 ^ ((this->r_sym_ & 0x3fff) << 2)
778 ^ ((this->addend_ & 0xffff) << 16));
781 // Functors for STL associative containers.
785 operator()(const Key& k) const
786 { return k.hash_value(); }
792 operator()(const Key& k1, const Key& k2) const
793 { return k1.eq(k2); }
798 const Stub_type stub_type_;
799 // If this is a local symbol, this is the index in the defining object.
800 // Otherwise, it is invalid_index for a global symbol.
802 // If r_sym_ is an invalid index, this points to a global symbol.
803 // Otherwise, it points to a relobj. We used the unsized and target
804 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
805 // Arm_relobj, in order to avoid making the stub class a template
806 // as most of the stub machinery is endianness-neutral. However, it
807 // may require a bit of casting done by users of this class.
810 const Symbol* symbol;
811 const Relobj* relobj;
813 // Addend associated with a reloc.
815 }; // End of inner class Reloc_stub::Key
818 // This may be overridden in the child class.
820 do_write(unsigned char*, section_size_type);
823 static const section_offset_type invalid_offset =
824 static_cast<section_offset_type>(-1);
825 static const unsigned int invalid_index = static_cast<unsigned int>(-1);
826 static const AArch64_address invalid_address =
827 static_cast<AArch64_address>(-1);
829 static const uint32_t stub_insns_[][10];
831 const Stub_type stub_type_;
832 section_offset_type offset_;
833 AArch64_address destination_address_;
834 }; // End of Reloc_stub
837 // Write data to output file.
839 template<int size, bool big_endian>
841 Reloc_stub<size, big_endian>::
842 do_write(unsigned char* view, section_size_type)
844 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
845 const uint32_t* insns = this->stub_insns();
846 uint32_t num_insns = this->stub_insn_number();
847 Insntype* ip = reinterpret_cast<Insntype*>(view);
848 for (uint32_t i = 1; i <= num_insns; ++i)
849 elfcpp::Swap<32, big_endian>::writeval(ip + i - 1, insns[i]);
853 // Stubs instructions definition.
855 template<int size, bool big_endian>
857 Reloc_stub<size, big_endian>::stub_insns_[][10] =
859 // The first element of each group is the num of the insns.
867 0x90000010, /* adrp ip0, X */
868 /* ADR_PREL_PG_HI21(X) */
869 0x91000210, /* add ip0, ip0, :lo12:X */
870 /* ADD_ABS_LO12_NC(X) */
871 0xd61f0200, /* br ip0 */
872 0x00000000, /* alignment padding */
875 // ST_LONG_BRANCH_ABS
878 0x58000050, /* ldr ip0, 0x8 */
879 0xd61f0200, /* br ip0 */
880 0x00000000, /* address field */
881 0x00000000, /* address fields */
884 // ST_LONG_BRANCH_PCREL
887 0x58000090, /* ldr ip0, 0x10 */
888 0x10000011, /* adr ip1, #0 */
889 0x8b110210, /* add ip0, ip0, ip1 */
890 0xd61f0200, /* br ip0 */
891 0x00000000, /* address field */
892 0x00000000, /* address field */
893 0x00000000, /* alignment padding */
894 0x00000000, /* alignment padding */
899 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
902 template<int size, bool big_endian>
904 typename Reloc_stub<size, big_endian>::Stub_type
905 Reloc_stub<size, big_endian>::stub_type_for_reloc(
906 unsigned int r_type, AArch64_address location, AArch64_address dest)
908 int64_t branch_offset = 0;
911 case elfcpp::R_AARCH64_CALL26:
912 case elfcpp::R_AARCH64_JUMP26:
913 branch_offset = dest - location;
919 if (aarch64_valid_branch_offset_p(branch_offset))
922 if (aarch64_valid_for_adrp_p(location, dest))
923 return ST_ADRP_BRANCH;
925 if (parameters->options().output_is_position_independent()
926 && parameters->options().output_is_executable())
927 return ST_LONG_BRANCH_PCREL;
929 return ST_LONG_BRANCH_ABS;
932 // A class to hold stubs for the ARM target.
934 template<int size, bool big_endian>
935 class Stub_table : public Output_data
938 typedef Target_aarch64<size, big_endian> The_target_aarch64;
939 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
940 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
941 typedef Reloc_stub<size, big_endian> The_reloc_stub;
942 typedef typename The_reloc_stub::Key The_reloc_stub_key;
943 typedef typename The_reloc_stub_key::hash The_reloc_stub_key_hash;
944 typedef typename The_reloc_stub_key::equal_to The_reloc_stub_key_equal_to;
945 typedef Stub_table<size, big_endian> The_stub_table;
946 typedef Unordered_map<The_reloc_stub_key, The_reloc_stub*,
947 The_reloc_stub_key_hash, The_reloc_stub_key_equal_to>
949 typedef typename Reloc_stub_map::const_iterator Reloc_stub_map_const_iter;
950 typedef Relocate_info<size, big_endian> The_relocate_info;
952 Stub_table(The_aarch64_input_section* owner)
953 : Output_data(), owner_(owner), reloc_stubs_size_(0), prev_data_size_(0)
959 The_aarch64_input_section*
963 // Whether this stub table is empty.
966 { return reloc_stubs_.empty(); }
968 // Return the current data size.
970 current_data_size() const
971 { return this->current_data_size_for_child(); }
973 // Add a STUB using KEY. The caller is responsible for avoiding addition
974 // if a STUB with the same key has already been added.
976 add_reloc_stub(The_reloc_stub* stub, const The_reloc_stub_key& key);
978 // Finalize stubs. No-op here, just for completeness.
983 // Look up a relocation stub using KEY. Return NULL if there is none.
985 find_reloc_stub(The_reloc_stub_key& key)
987 Reloc_stub_map_const_iter p = this->reloc_stubs_.find(key);
988 return (p != this->reloc_stubs_.end()) ? p->second : NULL;
991 // Relocate stubs in this stub table.
993 relocate_stubs(const The_relocate_info*,
1000 // Update data size at the end of a relaxation pass. Return true if data size
1001 // is different from that of the previous relaxation pass.
1003 update_data_size_changed_p()
1005 // No addralign changed here.
1006 off_t s = this->reloc_stubs_size_;
1007 bool changed = (s != this->prev_data_size_);
1008 this->prev_data_size_ = s;
1013 // Write out section contents.
1015 do_write(Output_file*);
1017 // Return the required alignment.
1019 do_addralign() const
1020 { return The_reloc_stub::STUB_ADDR_ALIGN; }
1022 // Reset address and file offset.
1024 do_reset_address_and_file_offset()
1025 { this->set_current_data_size_for_child(this->prev_data_size_); }
1027 // Set final data size.
1029 set_final_data_size()
1030 { this->set_data_size(this->current_data_size()); }
1033 // Relocate one stub.
1035 relocate_stub(The_reloc_stub*,
1036 const The_relocate_info*,
1037 The_target_aarch64*,
1044 // Owner of this stub table.
1045 The_aarch64_input_section* owner_;
1046 // The relocation stubs.
1047 Reloc_stub_map reloc_stubs_;
1048 // Size of reloc stubs.
1049 off_t reloc_stubs_size_;
1050 // data size of this in the previous pass.
1051 off_t prev_data_size_;
1052 }; // End of Stub_table
1055 // Add a STUB using KEY. The caller is responsible for avoiding addition
1056 // if a STUB with the same key has already been added.
1058 template<int size, bool big_endian>
1060 Stub_table<size, big_endian>::add_reloc_stub(
1061 The_reloc_stub* stub, const The_reloc_stub_key& key)
1063 gold_assert(stub->stub_type() == key.stub_type());
1064 this->reloc_stubs_[key] = stub;
1066 // Assign stub offset early. We can do this because we never remove
1067 // reloc stubs and they are in the beginning of the stub table.
1068 this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_,
1069 The_reloc_stub::STUB_ADDR_ALIGN);
1070 stub->set_offset(this->reloc_stubs_size_);
1071 this->reloc_stubs_size_ += stub->stub_size();
1075 // Relocate all stubs in this stub table.
1077 template<int size, bool big_endian>
1079 Stub_table<size, big_endian>::
1080 relocate_stubs(const The_relocate_info* relinfo,
1081 The_target_aarch64* target_aarch64,
1082 Output_section* output_section,
1083 unsigned char* view,
1084 AArch64_address address,
1085 section_size_type view_size)
1087 // "view_size" is the total size of the stub_table.
1088 gold_assert(address == this->address() &&
1089 view_size == static_cast<section_size_type>(this->data_size()));
1090 for(Reloc_stub_map_const_iter p = this->reloc_stubs_.begin();
1091 p != this->reloc_stubs_.end(); ++p)
1092 relocate_stub(p->second, relinfo, target_aarch64, output_section,
1093 view, address, view_size);
1097 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
1099 template<int size, bool big_endian>
1101 Stub_table<size, big_endian>::
1102 relocate_stub(The_reloc_stub* stub,
1103 const The_relocate_info* relinfo,
1104 The_target_aarch64* target_aarch64,
1105 Output_section* output_section,
1106 unsigned char* view,
1107 AArch64_address address,
1108 section_size_type view_size)
1110 // "offset" is the offset from the beginning of the stub_table.
1111 section_size_type offset = stub->offset();
1112 section_size_type stub_size = stub->stub_size();
1113 // "view_size" is the total size of the stub_table.
1114 gold_assert(offset + stub_size <= view_size);
1116 target_aarch64->relocate_stub(stub, relinfo, output_section,
1117 view + offset, address + offset, view_size);
1121 // Write out the stubs to file.
1123 template<int size, bool big_endian>
1125 Stub_table<size, big_endian>::do_write(Output_file* of)
1127 off_t offset = this->offset();
1128 const section_size_type oview_size =
1129 convert_to_section_size_type(this->data_size());
1130 unsigned char* const oview = of->get_output_view(offset, oview_size);
1132 // Write relocation stubs.
1133 for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
1134 p != this->reloc_stubs_.end(); ++p)
1136 The_reloc_stub* stub = p->second;
1137 AArch64_address address = this->address() + stub->offset();
1138 gold_assert(address ==
1139 align_address(address, The_reloc_stub::STUB_ADDR_ALIGN));
1140 stub->write(oview + stub->offset(), stub->stub_size());
1143 of->write_output_view(this->offset(), oview_size, oview);
1147 // AArch64_relobj class.
1149 template<int size, bool big_endian>
1150 class AArch64_relobj : public Sized_relobj_file<size, big_endian>
1153 typedef AArch64_relobj<size, big_endian> This;
1154 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1155 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1156 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
1157 typedef Stub_table<size, big_endian> The_stub_table;
1158 typedef std::vector<The_stub_table*> Stub_table_list;
1159 static const AArch64_address invalid_address =
1160 static_cast<AArch64_address>(-1);
1162 AArch64_relobj(const std::string& name, Input_file* input_file, off_t offset,
1163 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
1164 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
1171 // Return the stub table of the SHNDX-th section if there is one.
1173 stub_table(unsigned int shndx) const
1175 gold_assert(shndx < this->stub_tables_.size());
1176 return this->stub_tables_[shndx];
1179 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1181 set_stub_table(unsigned int shndx, The_stub_table* stub_table)
1183 gold_assert(shndx < this->stub_tables_.size());
1184 this->stub_tables_[shndx] = stub_table;
1187 // Entrance to erratum_843419 scanning.
1189 scan_erratum_843419(unsigned int shndx,
1190 const elfcpp::Shdr<size, big_endian>&,
1191 Output_section*, const Symbol_table*,
1192 The_target_aarch64*);
1194 // Scan all relocation sections for stub generation.
1196 scan_sections_for_stubs(The_target_aarch64*, const Symbol_table*,
1199 // Whether a section is a scannable text section.
1201 text_section_is_scannable(const elfcpp::Shdr<size, big_endian>&, unsigned int,
1202 const Output_section*, const Symbol_table*);
1204 // Convert regular input section with index SHNDX to a relaxed section.
1206 convert_input_section_to_relaxed_section(unsigned /* shndx */)
1208 // The stubs have relocations and we need to process them after writing
1209 // out the stubs. So relocation now must follow section write.
1210 this->set_relocs_must_follow_section_writes();
1213 // Structure for mapping symbol position.
1214 struct Mapping_symbol_position
1216 Mapping_symbol_position(unsigned int shndx, AArch64_address offset):
1217 shndx_(shndx), offset_(offset)
1220 // "<" comparator used in ordered_map container.
1222 operator<(const Mapping_symbol_position& p) const
1224 return (this->shndx_ < p.shndx_
1225 || (this->shndx_ == p.shndx_ && this->offset_ < p.offset_));
1229 unsigned int shndx_;
1232 AArch64_address offset_;
1235 typedef std::map<Mapping_symbol_position, char> Mapping_symbol_info;
1238 // Post constructor setup.
1242 // Call parent's setup method.
1243 Sized_relobj_file<size, big_endian>::do_setup();
1245 // Initialize look-up tables.
1246 this->stub_tables_.resize(this->shnum());
1250 do_relocate_sections(
1251 const Symbol_table* symtab, const Layout* layout,
1252 const unsigned char* pshdrs, Output_file* of,
1253 typename Sized_relobj_file<size, big_endian>::Views* pviews);
1255 // Count local symbols and (optionally) record mapping info.
1257 do_count_local_symbols(Stringpool_template<char>*,
1258 Stringpool_template<char>*);
1261 // Whether a section needs to be scanned for relocation stubs.
1263 section_needs_reloc_stub_scanning(const elfcpp::Shdr<size, big_endian>&,
1264 const Relobj::Output_sections&,
1265 const Symbol_table*, const unsigned char*);
1267 // List of stub tables.
1268 Stub_table_list stub_tables_;
1270 // Mapping symbol information sorted by (section index, section_offset).
1271 Mapping_symbol_info mapping_symbol_info_;
1272 }; // End of AArch64_relobj
1275 // Override to record mapping symbol information.
1276 template<int size, bool big_endian>
1278 AArch64_relobj<size, big_endian>::do_count_local_symbols(
1279 Stringpool_template<char>* pool, Stringpool_template<char>* dynpool)
1281 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
1283 // Only erratum-fixing work needs mapping symbols, so skip this time consuming
1284 // processing if not fixing erratum.
1285 if (!parameters->options().fix_cortex_a53_843419())
1288 const unsigned int loccount = this->local_symbol_count();
1292 // Read the symbol table section header.
1293 const unsigned int symtab_shndx = this->symtab_shndx();
1294 elfcpp::Shdr<size, big_endian>
1295 symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
1296 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1298 // Read the local symbols.
1299 const int sym_size =elfcpp::Elf_sizes<size>::sym_size;
1300 gold_assert(loccount == symtabshdr.get_sh_info());
1301 off_t locsize = loccount * sym_size;
1302 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1303 locsize, true, true);
1305 // For mapping symbol processing, we need to read the symbol names.
1306 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
1307 if (strtab_shndx >= this->shnum())
1309 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
1313 elfcpp::Shdr<size, big_endian>
1314 strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
1315 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
1317 this->error(_("symbol table name section has wrong type: %u"),
1318 static_cast<unsigned int>(strtabshdr.get_sh_type()));
1322 const char* pnames =
1323 reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
1324 strtabshdr.get_sh_size(),
1327 // Skip the first dummy symbol.
1329 typename Sized_relobj_file<size, big_endian>::Local_values*
1330 plocal_values = this->local_values();
1331 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1333 elfcpp::Sym<size, big_endian> sym(psyms);
1334 Symbol_value<size>& lv((*plocal_values)[i]);
1335 AArch64_address input_value = lv.input_value();
1337 // Check to see if this is a mapping symbol.
1338 const char* sym_name = pnames + sym.get_st_name();
1339 if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd')
1340 && sym_name[2] == '\0')
1343 unsigned int input_shndx =
1344 this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
1345 gold_assert(is_ordinary);
1347 Mapping_symbol_position msp(input_shndx, input_value);
1348 // Insert mapping_symbol_info into map whose ordering is defined by
1349 // (shndx, offset_within_section).
1350 this->mapping_symbol_info_[msp] = sym_name[1];
1356 // Relocate sections.
1358 template<int size, bool big_endian>
1360 AArch64_relobj<size, big_endian>::do_relocate_sections(
1361 const Symbol_table* symtab, const Layout* layout,
1362 const unsigned char* pshdrs, Output_file* of,
1363 typename Sized_relobj_file<size, big_endian>::Views* pviews)
1365 // Call parent to relocate sections.
1366 Sized_relobj_file<size, big_endian>::do_relocate_sections(symtab, layout,
1367 pshdrs, of, pviews);
1369 // We do not generate stubs if doing a relocatable link.
1370 if (parameters->options().relocatable())
1373 Relocate_info<size, big_endian> relinfo;
1374 relinfo.symtab = symtab;
1375 relinfo.layout = layout;
1376 relinfo.object = this;
1378 // Relocate stub tables.
1379 unsigned int shnum = this->shnum();
1380 The_target_aarch64* target = The_target_aarch64::current_target();
1382 for (unsigned int i = 1; i < shnum; ++i)
1384 The_aarch64_input_section* aarch64_input_section =
1385 target->find_aarch64_input_section(this, i);
1386 if (aarch64_input_section != NULL
1387 && aarch64_input_section->is_stub_table_owner()
1388 && !aarch64_input_section->stub_table()->empty())
1390 Output_section* os = this->output_section(i);
1391 gold_assert(os != NULL);
1393 relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
1394 relinfo.reloc_shdr = NULL;
1395 relinfo.data_shndx = i;
1396 relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<size>::shdr_size;
1398 typename Sized_relobj_file<size, big_endian>::View_size&
1399 view_struct = (*pviews)[i];
1400 gold_assert(view_struct.view != NULL);
1402 The_stub_table* stub_table = aarch64_input_section->stub_table();
1403 off_t offset = stub_table->address() - view_struct.address;
1404 unsigned char* view = view_struct.view + offset;
1405 AArch64_address address = stub_table->address();
1406 section_size_type view_size = stub_table->data_size();
1407 stub_table->relocate_stubs(&relinfo, target, os, view, address,
1414 // Determine if an input section is scannable for stub processing. SHDR is
1415 // the header of the section and SHNDX is the section index. OS is the output
1416 // section for the input section and SYMTAB is the global symbol table used to
1417 // look up ICF information.
1419 template<int size, bool big_endian>
1421 AArch64_relobj<size, big_endian>::text_section_is_scannable(
1422 const elfcpp::Shdr<size, big_endian>& text_shdr,
1423 unsigned int text_shndx,
1424 const Output_section* os,
1425 const Symbol_table* symtab)
1427 // Skip any empty sections, unallocated sections or sections whose
1428 // type are not SHT_PROGBITS.
1429 if (text_shdr.get_sh_size() == 0
1430 || (text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
1431 || text_shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
1434 // Skip any discarded or ICF'ed sections.
1435 if (os == NULL || symtab->is_section_folded(this, text_shndx))
1438 // Skip exception frame.
1439 if (strcmp(os->name(), ".eh_frame") == 0)
1442 gold_assert(!this->is_output_section_offset_invalid(text_shndx) ||
1443 os->find_relaxed_input_section(this, text_shndx) != NULL);
1449 // Determine if we want to scan the SHNDX-th section for relocation stubs.
1450 // This is a helper for AArch64_relobj::scan_sections_for_stubs().
1452 template<int size, bool big_endian>
1454 AArch64_relobj<size, big_endian>::section_needs_reloc_stub_scanning(
1455 const elfcpp::Shdr<size, big_endian>& shdr,
1456 const Relobj::Output_sections& out_sections,
1457 const Symbol_table* symtab,
1458 const unsigned char* pshdrs)
1460 unsigned int sh_type = shdr.get_sh_type();
1461 if (sh_type != elfcpp::SHT_RELA)
1464 // Ignore empty section.
1465 off_t sh_size = shdr.get_sh_size();
1469 // Ignore reloc section with unexpected symbol table. The
1470 // error will be reported in the final link.
1471 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
1474 gold_assert(sh_type == elfcpp::SHT_RELA);
1475 unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
1477 // Ignore reloc section with unexpected entsize or uneven size.
1478 // The error will be reported in the final link.
1479 if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
1482 // Ignore reloc section with bad info. This error will be
1483 // reported in the final link.
1484 unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_info());
1485 if (text_shndx >= this->shnum())
1488 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1489 const elfcpp::Shdr<size, big_endian> text_shdr(pshdrs +
1490 text_shndx * shdr_size);
1491 return this->text_section_is_scannable(text_shdr, text_shndx,
1492 out_sections[text_shndx], symtab);
1496 // Scan section SHNDX for erratum 843419.
1498 template<int size, bool big_endian>
1500 AArch64_relobj<size, big_endian>::scan_erratum_843419(
1501 unsigned int shndx, const elfcpp::Shdr<size, big_endian>& shdr,
1502 Output_section* os, const Symbol_table* symtab,
1503 The_target_aarch64* target)
1505 if (shdr.get_sh_size() == 0
1506 || (shdr.get_sh_flags() &
1507 (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) == 0
1508 || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
1511 if (!os || symtab->is_section_folded(this, shndx)) return;
1513 AArch64_address output_offset = this->get_output_section_offset(shndx);
1514 AArch64_address output_address;
1515 if (output_offset != invalid_address)
1516 output_address = os->address() + output_offset;
1519 const Output_relaxed_input_section* poris =
1520 os->find_relaxed_input_section(this, shndx);
1522 output_address = poris->address();
1525 section_size_type input_view_size = 0;
1526 const unsigned char* input_view =
1527 this->section_contents(shndx, &input_view_size, false);
1529 Mapping_symbol_position section_start(shndx, 0);
1530 // Find the first mapping symbol record within section shndx.
1531 typename Mapping_symbol_info::const_iterator p =
1532 this->mapping_symbol_info_.lower_bound(section_start);
1533 if (p == this->mapping_symbol_info_.end() || p->first.shndx_ != shndx)
1534 gold_warning(_("cannot scan executable section %u of %s for Cortex-A53 "
1535 "erratum because it has no mapping symbols."),
1536 shndx, this->name().c_str());
1537 while (p != this->mapping_symbol_info_.end() &&
1538 p->first.shndx_ == shndx)
1540 typename Mapping_symbol_info::const_iterator prev = p;
1542 if (prev->second == 'x')
1544 section_size_type span_start =
1545 convert_to_section_size_type(prev->first.offset_);
1546 section_size_type span_end;
1547 if (p != this->mapping_symbol_info_.end()
1548 && p->first.shndx_ == shndx)
1549 span_end = convert_to_section_size_type(p->first.offset_);
1551 span_end = convert_to_section_size_type(shdr.get_sh_size());
1552 target->scan_erratum_843419_span(
1553 this, shndx, span_start, span_end,
1554 const_cast<unsigned char*>(input_view), output_address);
1560 // Scan relocations for stub generation.
1562 template<int size, bool big_endian>
1564 AArch64_relobj<size, big_endian>::scan_sections_for_stubs(
1565 The_target_aarch64* target,
1566 const Symbol_table* symtab,
1567 const Layout* layout)
1569 unsigned int shnum = this->shnum();
1570 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1572 // Read the section headers.
1573 const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
1577 // To speed up processing, we set up hash tables for fast lookup of
1578 // input offsets to output addresses.
1579 this->initialize_input_to_output_maps();
1581 const Relobj::Output_sections& out_sections(this->output_sections());
1583 Relocate_info<size, big_endian> relinfo;
1584 relinfo.symtab = symtab;
1585 relinfo.layout = layout;
1586 relinfo.object = this;
1588 // Do relocation stubs scanning.
1589 const unsigned char* p = pshdrs + shdr_size;
1590 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
1592 const elfcpp::Shdr<size, big_endian> shdr(p);
1593 if (parameters->options().fix_cortex_a53_843419())
1594 scan_erratum_843419(i, shdr, out_sections[i], symtab, target);
1595 if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
1598 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
1599 AArch64_address output_offset =
1600 this->get_output_section_offset(index);
1601 AArch64_address output_address;
1602 if (output_offset != invalid_address)
1604 output_address = out_sections[index]->address() + output_offset;
1608 // Currently this only happens for a relaxed section.
1609 const Output_relaxed_input_section* poris =
1610 out_sections[index]->find_relaxed_input_section(this, index);
1611 gold_assert(poris != NULL);
1612 output_address = poris->address();
1615 // Get the relocations.
1616 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
1620 // Get the section contents.
1621 section_size_type input_view_size = 0;
1622 const unsigned char* input_view =
1623 this->section_contents(index, &input_view_size, false);
1625 relinfo.reloc_shndx = i;
1626 relinfo.data_shndx = index;
1627 unsigned int sh_type = shdr.get_sh_type();
1628 unsigned int reloc_size;
1629 gold_assert (sh_type == elfcpp::SHT_RELA);
1630 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
1632 Output_section* os = out_sections[index];
1633 target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
1634 shdr.get_sh_size() / reloc_size,
1636 output_offset == invalid_address,
1637 input_view, output_address,
1644 // A class to wrap an ordinary input section containing executable code.
1646 template<int size, bool big_endian>
1647 class AArch64_input_section : public Output_relaxed_input_section
1650 typedef Stub_table<size, big_endian> The_stub_table;
1652 AArch64_input_section(Relobj* relobj, unsigned int shndx)
1653 : Output_relaxed_input_section(relobj, shndx, 1),
1655 original_contents_(NULL), original_size_(0),
1656 original_addralign_(1)
1659 ~AArch64_input_section()
1660 { delete[] this->original_contents_; }
1666 // Set the stub_table.
1668 set_stub_table(The_stub_table* st)
1669 { this->stub_table_ = st; }
1671 // Whether this is a stub table owner.
1673 is_stub_table_owner() const
1674 { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
1676 // Return the original size of the section.
1678 original_size() const
1679 { return this->original_size_; }
1681 // Return the stub table.
1684 { return stub_table_; }
1687 // Write out this input section.
1689 do_write(Output_file*);
1691 // Return required alignment of this.
1693 do_addralign() const
1695 if (this->is_stub_table_owner())
1696 return std::max(this->stub_table_->addralign(),
1697 static_cast<uint64_t>(this->original_addralign_));
1699 return this->original_addralign_;
1702 // Finalize data size.
1704 set_final_data_size();
1706 // Reset address and file offset.
1708 do_reset_address_and_file_offset();
1712 do_output_offset(const Relobj* object, unsigned int shndx,
1713 section_offset_type offset,
1714 section_offset_type* poutput) const
1716 if ((object == this->relobj())
1717 && (shndx == this->shndx())
1720 convert_types<section_offset_type, uint32_t>(this->original_size_)))
1730 // Copying is not allowed.
1731 AArch64_input_section(const AArch64_input_section&);
1732 AArch64_input_section& operator=(const AArch64_input_section&);
1734 // The relocation stubs.
1735 The_stub_table* stub_table_;
1736 // Original section contents. We have to make a copy here since the file
1737 // containing the original section may not be locked when we need to access
1739 unsigned char* original_contents_;
1740 // Section size of the original input section.
1741 uint32_t original_size_;
1742 // Address alignment of the original input section.
1743 uint32_t original_addralign_;
1744 }; // End of AArch64_input_section
1747 // Finalize data size.
1749 template<int size, bool big_endian>
1751 AArch64_input_section<size, big_endian>::set_final_data_size()
1753 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
1755 if (this->is_stub_table_owner())
1757 this->stub_table_->finalize_data_size();
1758 off = align_address(off, this->stub_table_->addralign());
1759 off += this->stub_table_->data_size();
1761 this->set_data_size(off);
1765 // Reset address and file offset.
1767 template<int size, bool big_endian>
1769 AArch64_input_section<size, big_endian>::do_reset_address_and_file_offset()
1771 // Size of the original input section contents.
1772 off_t off = convert_types<off_t, uint64_t>(this->original_size_);
1774 // If this is a stub table owner, account for the stub table size.
1775 if (this->is_stub_table_owner())
1777 The_stub_table* stub_table = this->stub_table_;
1779 // Reset the stub table's address and file offset. The
1780 // current data size for child will be updated after that.
1781 stub_table_->reset_address_and_file_offset();
1782 off = align_address(off, stub_table_->addralign());
1783 off += stub_table->current_data_size();
1786 this->set_current_data_size(off);
1790 // Initialize an Arm_input_section.
1792 template<int size, bool big_endian>
1794 AArch64_input_section<size, big_endian>::init()
1796 Relobj* relobj = this->relobj();
1797 unsigned int shndx = this->shndx();
1799 // We have to cache original size, alignment and contents to avoid locking
1800 // the original file.
1801 this->original_addralign_ =
1802 convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
1804 // This is not efficient but we expect only a small number of relaxed
1805 // input sections for stubs.
1806 section_size_type section_size;
1807 const unsigned char* section_contents =
1808 relobj->section_contents(shndx, §ion_size, false);
1809 this->original_size_ =
1810 convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
1812 gold_assert(this->original_contents_ == NULL);
1813 this->original_contents_ = new unsigned char[section_size];
1814 memcpy(this->original_contents_, section_contents, section_size);
1816 // We want to make this look like the original input section after
1817 // output sections are finalized.
1818 Output_section* os = relobj->output_section(shndx);
1819 off_t offset = relobj->output_section_offset(shndx);
1820 gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
1821 this->set_address(os->address() + offset);
1822 this->set_file_offset(os->offset() + offset);
1823 this->set_current_data_size(this->original_size_);
1824 this->finalize_data_size();
1828 // Write data to output file.
1830 template<int size, bool big_endian>
1832 AArch64_input_section<size, big_endian>::do_write(Output_file* of)
1834 // We have to write out the original section content.
1835 gold_assert(this->original_contents_ != NULL);
1836 of->write(this->offset(), this->original_contents_,
1837 this->original_size_);
1839 // If this owns a stub table and it is not empty, write it.
1840 if (this->is_stub_table_owner() && !this->stub_table_->empty())
1841 this->stub_table_->write(of);
1845 // Arm output section class. This is defined mainly to add a number of stub
1846 // generation methods.
1848 template<int size, bool big_endian>
1849 class AArch64_output_section : public Output_section
1852 typedef Target_aarch64<size, big_endian> The_target_aarch64;
1853 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
1854 typedef Stub_table<size, big_endian> The_stub_table;
1855 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
1858 AArch64_output_section(const char* name, elfcpp::Elf_Word type,
1859 elfcpp::Elf_Xword flags)
1860 : Output_section(name, type, flags)
1863 ~AArch64_output_section() {}
1865 // Group input sections for stub generation.
1867 group_sections(section_size_type, bool, Target_aarch64<size, big_endian>*,
1871 typedef Output_section::Input_section Input_section;
1872 typedef Output_section::Input_section_list Input_section_list;
1874 // Create a stub group.
1876 create_stub_group(Input_section_list::const_iterator,
1877 Input_section_list::const_iterator,
1878 Input_section_list::const_iterator,
1879 The_target_aarch64*,
1880 std::vector<Output_relaxed_input_section*>&,
1882 }; // End of AArch64_output_section
1885 // Create a stub group for input sections from FIRST to LAST. OWNER points to
1886 // the input section that will be the owner of the stub table.
1888 template<int size, bool big_endian> void
1889 AArch64_output_section<size, big_endian>::create_stub_group(
1890 Input_section_list::const_iterator first,
1891 Input_section_list::const_iterator last,
1892 Input_section_list::const_iterator owner,
1893 The_target_aarch64* target,
1894 std::vector<Output_relaxed_input_section*>& new_relaxed_sections,
1897 // Currently we convert ordinary input sections into relaxed sections only
1899 The_aarch64_input_section* input_section;
1900 if (owner->is_relaxed_input_section())
1904 gold_assert(owner->is_input_section());
1905 // Create a new relaxed input section. We need to lock the original
1907 Task_lock_obj<Object> tl(task, owner->relobj());
1909 target->new_aarch64_input_section(owner->relobj(), owner->shndx());
1910 new_relaxed_sections.push_back(input_section);
1913 // Create a stub table.
1914 The_stub_table* stub_table =
1915 target->new_stub_table(input_section);
1917 input_section->set_stub_table(stub_table);
1919 Input_section_list::const_iterator p = first;
1920 // Look for input sections or relaxed input sections in [first ... last].
1923 if (p->is_input_section() || p->is_relaxed_input_section())
1925 // The stub table information for input sections live
1926 // in their objects.
1927 The_aarch64_relobj* aarch64_relobj =
1928 static_cast<The_aarch64_relobj*>(p->relobj());
1929 aarch64_relobj->set_stub_table(p->shndx(), stub_table);
1932 while (p++ != last);
1936 // Group input sections for stub generation. GROUP_SIZE is roughly the limit of
1937 // stub groups. We grow a stub group by adding input section until the size is
1938 // just below GROUP_SIZE. The last input section will be converted into a stub
1939 // table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
1940 // after the stub table, effectively doubling the group size.
1942 // This is similar to the group_sections() function in elf32-arm.c but is
1943 // implemented differently.
1945 template<int size, bool big_endian>
1946 void AArch64_output_section<size, big_endian>::group_sections(
1947 section_size_type group_size,
1948 bool stubs_always_after_branch,
1949 Target_aarch64<size, big_endian>* target,
1955 FINDING_STUB_SECTION,
1959 std::vector<Output_relaxed_input_section*> new_relaxed_sections;
1961 State state = NO_GROUP;
1962 section_size_type off = 0;
1963 section_size_type group_begin_offset = 0;
1964 section_size_type group_end_offset = 0;
1965 section_size_type stub_table_end_offset = 0;
1966 Input_section_list::const_iterator group_begin =
1967 this->input_sections().end();
1968 Input_section_list::const_iterator stub_table =
1969 this->input_sections().end();
1970 Input_section_list::const_iterator group_end = this->input_sections().end();
1971 for (Input_section_list::const_iterator p = this->input_sections().begin();
1972 p != this->input_sections().end();
1975 section_size_type section_begin_offset =
1976 align_address(off, p->addralign());
1977 section_size_type section_end_offset =
1978 section_begin_offset + p->data_size();
1980 // Check to see if we should group the previously seen sections.
1986 case FINDING_STUB_SECTION:
1987 // Adding this section makes the group larger than GROUP_SIZE.
1988 if (section_end_offset - group_begin_offset >= group_size)
1990 if (stubs_always_after_branch)
1992 gold_assert(group_end != this->input_sections().end());
1993 this->create_stub_group(group_begin, group_end, group_end,
1994 target, new_relaxed_sections,
2000 // Input sections up to stub_group_size bytes after the stub
2001 // table can be handled by it too.
2002 state = HAS_STUB_SECTION;
2003 stub_table = group_end;
2004 stub_table_end_offset = group_end_offset;
2009 case HAS_STUB_SECTION:
2010 // Adding this section makes the post stub-section group larger
2013 // NOT SUPPORTED YET. For completeness only.
2014 if (section_end_offset - stub_table_end_offset >= group_size)
2016 gold_assert(group_end != this->input_sections().end());
2017 this->create_stub_group(group_begin, group_end, stub_table,
2018 target, new_relaxed_sections, task);
2027 // If we see an input section and currently there is no group, start
2028 // a new one. Skip any empty sections. We look at the data size
2029 // instead of calling p->relobj()->section_size() to avoid locking.
2030 if ((p->is_input_section() || p->is_relaxed_input_section())
2031 && (p->data_size() != 0))
2033 if (state == NO_GROUP)
2035 state = FINDING_STUB_SECTION;
2037 group_begin_offset = section_begin_offset;
2040 // Keep track of the last input section seen.
2042 group_end_offset = section_end_offset;
2045 off = section_end_offset;
2048 // Create a stub group for any ungrouped sections.
2049 if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
2051 gold_assert(group_end != this->input_sections().end());
2052 this->create_stub_group(group_begin, group_end,
2053 (state == FINDING_STUB_SECTION
2056 target, new_relaxed_sections, task);
2059 if (!new_relaxed_sections.empty())
2060 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
2062 // Update the section offsets
2063 for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
2065 The_aarch64_relobj* relobj = static_cast<The_aarch64_relobj*>(
2066 new_relaxed_sections[i]->relobj());
2067 unsigned int shndx = new_relaxed_sections[i]->shndx();
2068 // Tell AArch64_relobj that this input section is converted.
2069 relobj->convert_input_section_to_relaxed_section(shndx);
2071 } // End of AArch64_output_section::group_sections
2074 AArch64_reloc_property_table* aarch64_reloc_property_table = NULL;
2077 // The aarch64 target class.
2079 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
2080 template<int size, bool big_endian>
2081 class Target_aarch64 : public Sized_target<size, big_endian>
2084 typedef Target_aarch64<size, big_endian> This;
2085 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
2087 typedef Relocate_info<size, big_endian> The_relocate_info;
2088 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2089 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
2090 typedef Reloc_stub<size, big_endian> The_reloc_stub;
2091 typedef typename The_reloc_stub::Stub_type The_reloc_stub_type;
2092 typedef typename Reloc_stub<size, big_endian>::Key The_reloc_stub_key;
2093 typedef Stub_table<size, big_endian> The_stub_table;
2094 typedef std::vector<The_stub_table*> Stub_table_list;
2095 typedef typename Stub_table_list::iterator Stub_table_iterator;
2096 typedef AArch64_input_section<size, big_endian> The_aarch64_input_section;
2097 typedef AArch64_output_section<size, big_endian> The_aarch64_output_section;
2098 typedef Unordered_map<Section_id,
2099 AArch64_input_section<size, big_endian>*,
2100 Section_id_hash> AArch64_input_section_map;
2101 typedef AArch64_insn_utilities<big_endian> Insn_utilities;
2102 const static int TCB_SIZE = size / 8 * 2;
2104 Target_aarch64(const Target::Target_info* info = &aarch64_info)
2105 : Sized_target<size, big_endian>(info),
2106 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
2107 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
2108 rela_irelative_(NULL), copy_relocs_(elfcpp::R_AARCH64_COPY),
2109 got_mod_index_offset_(-1U),
2110 tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
2111 stub_tables_(), stub_group_size_(0), aarch64_input_section_map_()
2114 // Scan the relocations to determine unreferenced sections for
2115 // garbage collection.
2117 gc_process_relocs(Symbol_table* symtab,
2119 Sized_relobj_file<size, big_endian>* object,
2120 unsigned int data_shndx,
2121 unsigned int sh_type,
2122 const unsigned char* prelocs,
2124 Output_section* output_section,
2125 bool needs_special_offset_handling,
2126 size_t local_symbol_count,
2127 const unsigned char* plocal_symbols);
2129 // Scan the relocations to look for symbol adjustments.
2131 scan_relocs(Symbol_table* symtab,
2133 Sized_relobj_file<size, big_endian>* object,
2134 unsigned int data_shndx,
2135 unsigned int sh_type,
2136 const unsigned char* prelocs,
2138 Output_section* output_section,
2139 bool needs_special_offset_handling,
2140 size_t local_symbol_count,
2141 const unsigned char* plocal_symbols);
2143 // Finalize the sections.
2145 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
2147 // Return the value to use for a dynamic which requires special
2150 do_dynsym_value(const Symbol*) const;
2152 // Relocate a section.
2154 relocate_section(const Relocate_info<size, big_endian>*,
2155 unsigned int sh_type,
2156 const unsigned char* prelocs,
2158 Output_section* output_section,
2159 bool needs_special_offset_handling,
2160 unsigned char* view,
2161 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2162 section_size_type view_size,
2163 const Reloc_symbol_changes*);
2165 // Scan the relocs during a relocatable link.
2167 scan_relocatable_relocs(Symbol_table* symtab,
2169 Sized_relobj_file<size, big_endian>* object,
2170 unsigned int data_shndx,
2171 unsigned int sh_type,
2172 const unsigned char* prelocs,
2174 Output_section* output_section,
2175 bool needs_special_offset_handling,
2176 size_t local_symbol_count,
2177 const unsigned char* plocal_symbols,
2178 Relocatable_relocs*);
2180 // Relocate a section during a relocatable link.
2183 const Relocate_info<size, big_endian>*,
2184 unsigned int sh_type,
2185 const unsigned char* prelocs,
2187 Output_section* output_section,
2188 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
2189 const Relocatable_relocs*,
2190 unsigned char* view,
2191 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2192 section_size_type view_size,
2193 unsigned char* reloc_view,
2194 section_size_type reloc_view_size);
2196 // Return the symbol index to use for a target specific relocation.
2197 // The only target specific relocation is R_AARCH64_TLSDESC for a
2198 // local symbol, which is an absolute reloc.
2200 do_reloc_symbol_index(void*, unsigned int r_type) const
2202 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
2206 // Return the addend to use for a target specific relocation.
2208 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
2210 // Return the PLT section.
2212 do_plt_address_for_global(const Symbol* gsym) const
2213 { return this->plt_section()->address_for_global(gsym); }
2216 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
2217 { return this->plt_section()->address_for_local(relobj, symndx); }
2219 // This function should be defined in targets that can use relocation
2220 // types to determine (implemented in local_reloc_may_be_function_pointer
2221 // and global_reloc_may_be_function_pointer)
2222 // if a function's pointer is taken. ICF uses this in safe mode to only
2223 // fold those functions whose pointer is defintely not taken.
2225 do_can_check_for_function_pointers() const
2228 // Return the number of entries in the PLT.
2230 plt_entry_count() const;
2232 //Return the offset of the first non-reserved PLT entry.
2234 first_plt_entry_offset() const;
2236 // Return the size of each PLT entry.
2238 plt_entry_size() const;
2240 // Create a stub table.
2242 new_stub_table(The_aarch64_input_section*);
2244 // Create an aarch64 input section.
2245 The_aarch64_input_section*
2246 new_aarch64_input_section(Relobj*, unsigned int);
2248 // Find an aarch64 input section instance for a given OBJ and SHNDX.
2249 The_aarch64_input_section*
2250 find_aarch64_input_section(Relobj*, unsigned int) const;
2252 // Return the thread control block size.
2254 tcb_size() const { return This::TCB_SIZE; }
2256 // Scan a section for stub generation.
2258 scan_section_for_stubs(const Relocate_info<size, big_endian>*, unsigned int,
2259 const unsigned char*, size_t, Output_section*,
2260 bool, const unsigned char*,
2264 // Scan a relocation section for stub.
2265 template<int sh_type>
2267 scan_reloc_section_for_stubs(
2268 const The_relocate_info* relinfo,
2269 const unsigned char* prelocs,
2271 Output_section* output_section,
2272 bool needs_special_offset_handling,
2273 const unsigned char* view,
2274 Address view_address,
2277 // Relocate a single stub.
2279 relocate_stub(The_reloc_stub*, const Relocate_info<size, big_endian>*,
2280 Output_section*, unsigned char*, Address,
2283 // Get the default AArch64 target.
2287 gold_assert(parameters->target().machine_code() == elfcpp::EM_AARCH64
2288 && parameters->target().get_size() == size
2289 && parameters->target().is_big_endian() == big_endian);
2290 return static_cast<This*>(parameters->sized_target<size, big_endian>());
2294 // Scan erratum for a part of a section.
2296 scan_erratum_843419_span(
2297 AArch64_relobj<size, big_endian>*,
2299 const section_size_type,
2300 const section_size_type,
2306 do_select_as_default_target()
2308 gold_assert(aarch64_reloc_property_table == NULL);
2309 aarch64_reloc_property_table = new AArch64_reloc_property_table();
2312 // Add a new reloc argument, returning the index in the vector.
2314 add_tlsdesc_info(Sized_relobj_file<size, big_endian>* object,
2317 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
2318 return this->tlsdesc_reloc_info_.size() - 1;
2321 virtual Output_data_plt_aarch64<size, big_endian>*
2322 do_make_data_plt(Layout* layout,
2323 Output_data_got_aarch64<size, big_endian>* got,
2324 Output_data_space* got_plt,
2325 Output_data_space* got_irelative)
2327 return new Output_data_plt_aarch64_standard<size, big_endian>(
2328 layout, got, got_plt, got_irelative);
2332 // do_make_elf_object to override the same function in the base class.
2334 do_make_elf_object(const std::string&, Input_file*, off_t,
2335 const elfcpp::Ehdr<size, big_endian>&);
2337 Output_data_plt_aarch64<size, big_endian>*
2338 make_data_plt(Layout* layout,
2339 Output_data_got_aarch64<size, big_endian>* got,
2340 Output_data_space* got_plt,
2341 Output_data_space* got_irelative)
2343 return this->do_make_data_plt(layout, got, got_plt, got_irelative);
2346 // We only need to generate stubs, and hence perform relaxation if we are
2347 // not doing relocatable linking.
2349 do_may_relax() const
2350 { return !parameters->options().relocatable(); }
2352 // Relaxation hook. This is where we do stub generation.
2354 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
2357 group_sections(Layout* layout,
2358 section_size_type group_size,
2359 bool stubs_always_after_branch,
2363 scan_reloc_for_stub(const The_relocate_info*, unsigned int,
2364 const Sized_symbol<size>*, unsigned int,
2365 const Symbol_value<size>*,
2366 typename elfcpp::Elf_types<size>::Elf_Swxword,
2369 // Make an output section.
2371 do_make_output_section(const char* name, elfcpp::Elf_Word type,
2372 elfcpp::Elf_Xword flags)
2373 { return new The_aarch64_output_section(name, type, flags); }
2376 // The class which scans relocations.
2381 : issued_non_pic_error_(false)
2385 local(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
2386 Sized_relobj_file<size, big_endian>* object,
2387 unsigned int data_shndx,
2388 Output_section* output_section,
2389 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
2390 const elfcpp::Sym<size, big_endian>& lsym,
2394 global(Symbol_table* symtab, Layout* layout, Target_aarch64* target,
2395 Sized_relobj_file<size, big_endian>* object,
2396 unsigned int data_shndx,
2397 Output_section* output_section,
2398 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
2402 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
2403 Target_aarch64<size, big_endian>* ,
2404 Sized_relobj_file<size, big_endian>* ,
2407 const elfcpp::Rela<size, big_endian>& ,
2408 unsigned int r_type,
2409 const elfcpp::Sym<size, big_endian>&);
2412 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
2413 Target_aarch64<size, big_endian>* ,
2414 Sized_relobj_file<size, big_endian>* ,
2417 const elfcpp::Rela<size, big_endian>& ,
2418 unsigned int r_type,
2423 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
2424 unsigned int r_type);
2427 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
2428 unsigned int r_type, Symbol*);
2431 possible_function_pointer_reloc(unsigned int r_type);
2434 check_non_pic(Relobj*, unsigned int r_type);
2437 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
2438 unsigned int r_type);
2440 // Whether we have issued an error about a non-PIC compilation.
2441 bool issued_non_pic_error_;
2444 // The class which implements relocation.
2449 : skip_call_tls_get_addr_(false)
2455 // Do a relocation. Return false if the caller should not issue
2456 // any warnings about this relocation.
2458 relocate(const Relocate_info<size, big_endian>*, Target_aarch64*,
2460 size_t relnum, const elfcpp::Rela<size, big_endian>&,
2461 unsigned int r_type, const Sized_symbol<size>*,
2462 const Symbol_value<size>*,
2463 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
2467 inline typename AArch64_relocate_functions<size, big_endian>::Status
2468 relocate_tls(const Relocate_info<size, big_endian>*,
2469 Target_aarch64<size, big_endian>*,
2471 const elfcpp::Rela<size, big_endian>&,
2472 unsigned int r_type, const Sized_symbol<size>*,
2473 const Symbol_value<size>*,
2475 typename elfcpp::Elf_types<size>::Elf_Addr);
2477 inline typename AArch64_relocate_functions<size, big_endian>::Status
2479 const Relocate_info<size, big_endian>*,
2480 Target_aarch64<size, big_endian>*,
2481 const elfcpp::Rela<size, big_endian>&,
2484 const Symbol_value<size>*);
2486 inline typename AArch64_relocate_functions<size, big_endian>::Status
2488 const Relocate_info<size, big_endian>*,
2489 Target_aarch64<size, big_endian>*,
2490 const elfcpp::Rela<size, big_endian>&,
2493 const Symbol_value<size>*);
2495 inline typename AArch64_relocate_functions<size, big_endian>::Status
2497 const Relocate_info<size, big_endian>*,
2498 Target_aarch64<size, big_endian>*,
2499 const elfcpp::Rela<size, big_endian>&,
2502 const Symbol_value<size>*);
2504 inline typename AArch64_relocate_functions<size, big_endian>::Status
2506 const Relocate_info<size, big_endian>*,
2507 Target_aarch64<size, big_endian>*,
2508 const elfcpp::Rela<size, big_endian>&,
2511 const Symbol_value<size>*);
2513 inline typename AArch64_relocate_functions<size, big_endian>::Status
2515 const Relocate_info<size, big_endian>*,
2516 Target_aarch64<size, big_endian>*,
2517 const elfcpp::Rela<size, big_endian>&,
2520 const Symbol_value<size>*,
2521 typename elfcpp::Elf_types<size>::Elf_Addr,
2522 typename elfcpp::Elf_types<size>::Elf_Addr);
2524 bool skip_call_tls_get_addr_;
2526 }; // End of class Relocate
2528 // A class which returns the size required for a relocation type,
2529 // used while scanning relocs during a relocatable link.
2530 class Relocatable_size_for_reloc
2534 get_size_for_reloc(unsigned int, Relobj*);
2537 // Adjust TLS relocation type based on the options and whether this
2538 // is a local symbol.
2539 static tls::Tls_optimization
2540 optimize_tls_reloc(bool is_final, int r_type);
2542 // Get the GOT section, creating it if necessary.
2543 Output_data_got_aarch64<size, big_endian>*
2544 got_section(Symbol_table*, Layout*);
2546 // Get the GOT PLT section.
2548 got_plt_section() const
2550 gold_assert(this->got_plt_ != NULL);
2551 return this->got_plt_;
2554 // Get the GOT section for TLSDESC entries.
2555 Output_data_got<size, big_endian>*
2556 got_tlsdesc_section() const
2558 gold_assert(this->got_tlsdesc_ != NULL);
2559 return this->got_tlsdesc_;
2562 // Create the PLT section.
2564 make_plt_section(Symbol_table* symtab, Layout* layout);
2566 // Create a PLT entry for a global symbol.
2568 make_plt_entry(Symbol_table*, Layout*, Symbol*);
2570 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
2572 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
2573 Sized_relobj_file<size, big_endian>* relobj,
2574 unsigned int local_sym_index);
2576 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2578 define_tls_base_symbol(Symbol_table*, Layout*);
2580 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2582 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
2584 // Create a GOT entry for the TLS module index.
2586 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
2587 Sized_relobj_file<size, big_endian>* object);
2589 // Get the PLT section.
2590 Output_data_plt_aarch64<size, big_endian>*
2593 gold_assert(this->plt_ != NULL);
2597 // Return whether this is a 3-insn erratum sequence.
2598 bool is_erratum_843419_sequence(
2599 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
2600 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
2601 typename elfcpp::Swap<32,big_endian>::Valtype insn3);
2603 // Get the dynamic reloc section, creating it if necessary.
2605 rela_dyn_section(Layout*);
2607 // Get the section to use for TLSDESC relocations.
2609 rela_tlsdesc_section(Layout*) const;
2611 // Get the section to use for IRELATIVE relocations.
2613 rela_irelative_section(Layout*);
2615 // Add a potential copy relocation.
2617 copy_reloc(Symbol_table* symtab, Layout* layout,
2618 Sized_relobj_file<size, big_endian>* object,
2619 unsigned int shndx, Output_section* output_section,
2620 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
2622 this->copy_relocs_.copy_reloc(symtab, layout,
2623 symtab->get_sized_symbol<size>(sym),
2624 object, shndx, output_section,
2625 reloc, this->rela_dyn_section(layout));
2628 // Information about this specific target which we pass to the
2629 // general Target structure.
2630 static const Target::Target_info aarch64_info;
2632 // The types of GOT entries needed for this platform.
2633 // These values are exposed to the ABI in an incremental link.
2634 // Do not renumber existing values without changing the version
2635 // number of the .gnu_incremental_inputs section.
2638 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
2639 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
2640 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
2641 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
2644 // This type is used as the argument to the target specific
2645 // relocation routines. The only target specific reloc is
2646 // R_AARCh64_TLSDESC against a local symbol.
2649 Tlsdesc_info(Sized_relobj_file<size, big_endian>* a_object,
2650 unsigned int a_r_sym)
2651 : object(a_object), r_sym(a_r_sym)
2654 // The object in which the local symbol is defined.
2655 Sized_relobj_file<size, big_endian>* object;
2656 // The local symbol index in the object.
2661 Output_data_got_aarch64<size, big_endian>* got_;
2663 Output_data_plt_aarch64<size, big_endian>* plt_;
2664 // The GOT PLT section.
2665 Output_data_space* got_plt_;
2666 // The GOT section for IRELATIVE relocations.
2667 Output_data_space* got_irelative_;
2668 // The GOT section for TLSDESC relocations.
2669 Output_data_got<size, big_endian>* got_tlsdesc_;
2670 // The _GLOBAL_OFFSET_TABLE_ symbol.
2671 Symbol* global_offset_table_;
2672 // The dynamic reloc section.
2673 Reloc_section* rela_dyn_;
2674 // The section to use for IRELATIVE relocs.
2675 Reloc_section* rela_irelative_;
2676 // Relocs saved to avoid a COPY reloc.
2677 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
2678 // Offset of the GOT entry for the TLS module index.
2679 unsigned int got_mod_index_offset_;
2680 // We handle R_AARCH64_TLSDESC against a local symbol as a target
2681 // specific relocation. Here we store the object and local symbol
2682 // index for the relocation.
2683 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
2684 // True if the _TLS_MODULE_BASE_ symbol has been defined.
2685 bool tls_base_symbol_defined_;
2686 // List of stub_tables
2687 Stub_table_list stub_tables_;
2688 // Actual stub group size
2689 section_size_type stub_group_size_;
2690 AArch64_input_section_map aarch64_input_section_map_;
2691 }; // End of Target_aarch64
2695 const Target::Target_info Target_aarch64<64, false>::aarch64_info =
2698 false, // is_big_endian
2699 elfcpp::EM_AARCH64, // machine_code
2700 false, // has_make_symbol
2701 false, // has_resolve
2702 false, // has_code_fill
2703 true, // is_default_stack_executable
2704 true, // can_icf_inline_merge_sections
2706 "/lib/ld.so.1", // program interpreter
2707 0x400000, // default_text_segment_address
2708 0x1000, // abi_pagesize (overridable by -z max-page-size)
2709 0x1000, // common_pagesize (overridable by -z common-page-size)
2710 false, // isolate_execinstr
2712 elfcpp::SHN_UNDEF, // small_common_shndx
2713 elfcpp::SHN_UNDEF, // large_common_shndx
2714 0, // small_common_section_flags
2715 0, // large_common_section_flags
2716 NULL, // attributes_section
2717 NULL, // attributes_vendor
2718 "_start" // entry_symbol_name
2722 const Target::Target_info Target_aarch64<32, false>::aarch64_info =
2725 false, // is_big_endian
2726 elfcpp::EM_AARCH64, // machine_code
2727 false, // has_make_symbol
2728 false, // has_resolve
2729 false, // has_code_fill
2730 true, // is_default_stack_executable
2731 false, // can_icf_inline_merge_sections
2733 "/lib/ld.so.1", // program interpreter
2734 0x400000, // default_text_segment_address
2735 0x1000, // abi_pagesize (overridable by -z max-page-size)
2736 0x1000, // common_pagesize (overridable by -z common-page-size)
2737 false, // isolate_execinstr
2739 elfcpp::SHN_UNDEF, // small_common_shndx
2740 elfcpp::SHN_UNDEF, // large_common_shndx
2741 0, // small_common_section_flags
2742 0, // large_common_section_flags
2743 NULL, // attributes_section
2744 NULL, // attributes_vendor
2745 "_start" // entry_symbol_name
2749 const Target::Target_info Target_aarch64<64, true>::aarch64_info =
2752 true, // is_big_endian
2753 elfcpp::EM_AARCH64, // machine_code
2754 false, // has_make_symbol
2755 false, // has_resolve
2756 false, // has_code_fill
2757 true, // is_default_stack_executable
2758 true, // can_icf_inline_merge_sections
2760 "/lib/ld.so.1", // program interpreter
2761 0x400000, // default_text_segment_address
2762 0x1000, // abi_pagesize (overridable by -z max-page-size)
2763 0x1000, // common_pagesize (overridable by -z common-page-size)
2764 false, // isolate_execinstr
2766 elfcpp::SHN_UNDEF, // small_common_shndx
2767 elfcpp::SHN_UNDEF, // large_common_shndx
2768 0, // small_common_section_flags
2769 0, // large_common_section_flags
2770 NULL, // attributes_section
2771 NULL, // attributes_vendor
2772 "_start" // entry_symbol_name
2776 const Target::Target_info Target_aarch64<32, true>::aarch64_info =
2779 true, // is_big_endian
2780 elfcpp::EM_AARCH64, // machine_code
2781 false, // has_make_symbol
2782 false, // has_resolve
2783 false, // has_code_fill
2784 true, // is_default_stack_executable
2785 false, // can_icf_inline_merge_sections
2787 "/lib/ld.so.1", // program interpreter
2788 0x400000, // default_text_segment_address
2789 0x1000, // abi_pagesize (overridable by -z max-page-size)
2790 0x1000, // common_pagesize (overridable by -z common-page-size)
2791 false, // isolate_execinstr
2793 elfcpp::SHN_UNDEF, // small_common_shndx
2794 elfcpp::SHN_UNDEF, // large_common_shndx
2795 0, // small_common_section_flags
2796 0, // large_common_section_flags
2797 NULL, // attributes_section
2798 NULL, // attributes_vendor
2799 "_start" // entry_symbol_name
2802 // Get the GOT section, creating it if necessary.
2804 template<int size, bool big_endian>
2805 Output_data_got_aarch64<size, big_endian>*
2806 Target_aarch64<size, big_endian>::got_section(Symbol_table* symtab,
2809 if (this->got_ == NULL)
2811 gold_assert(symtab != NULL && layout != NULL);
2813 // When using -z now, we can treat .got.plt as a relro section.
2814 // Without -z now, it is modified after program startup by lazy
2816 bool is_got_plt_relro = parameters->options().now();
2817 Output_section_order got_order = (is_got_plt_relro
2819 : ORDER_RELRO_LAST);
2820 Output_section_order got_plt_order = (is_got_plt_relro
2822 : ORDER_NON_RELRO_FIRST);
2824 // Layout of .got and .got.plt sections.
2825 // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
2827 // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
2828 // .gotplt[1] reserved for ld.so (resolver)
2829 // .gotplt[2] reserved
2831 // Generate .got section.
2832 this->got_ = new Output_data_got_aarch64<size, big_endian>(symtab,
2834 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2835 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
2836 this->got_, got_order, true);
2837 // The first word of GOT is reserved for the address of .dynamic.
2838 // We put 0 here now. The value will be replaced later in
2839 // Output_data_got_aarch64::do_write.
2840 this->got_->add_constant(0);
2842 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2843 // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
2844 // even if there is a .got.plt section.
2845 this->global_offset_table_ =
2846 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2847 Symbol_table::PREDEFINED,
2849 0, 0, elfcpp::STT_OBJECT,
2851 elfcpp::STV_HIDDEN, 0,
2854 // Generate .got.plt section.
2855 this->got_plt_ = new Output_data_space(size / 8, "** GOT PLT");
2856 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2858 | elfcpp::SHF_WRITE),
2859 this->got_plt_, got_plt_order,
2862 // The first three entries are reserved.
2863 this->got_plt_->set_current_data_size(
2864 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
2866 // If there are any IRELATIVE relocations, they get GOT entries
2867 // in .got.plt after the jump slot entries.
2868 this->got_irelative_ = new Output_data_space(size / 8,
2869 "** GOT IRELATIVE PLT");
2870 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2872 | elfcpp::SHF_WRITE),
2873 this->got_irelative_,
2877 // If there are any TLSDESC relocations, they get GOT entries in
2878 // .got.plt after the jump slot and IRELATIVE entries.
2879 this->got_tlsdesc_ = new Output_data_got<size, big_endian>();
2880 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
2882 | elfcpp::SHF_WRITE),
2887 if (!is_got_plt_relro)
2889 // Those bytes can go into the relro segment.
2890 layout->increase_relro(
2891 AARCH64_GOTPLT_RESERVE_COUNT * (size / 8));
2898 // Get the dynamic reloc section, creating it if necessary.
2900 template<int size, bool big_endian>
2901 typename Target_aarch64<size, big_endian>::Reloc_section*
2902 Target_aarch64<size, big_endian>::rela_dyn_section(Layout* layout)
2904 if (this->rela_dyn_ == NULL)
2906 gold_assert(layout != NULL);
2907 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2908 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2909 elfcpp::SHF_ALLOC, this->rela_dyn_,
2910 ORDER_DYNAMIC_RELOCS, false);
2912 return this->rela_dyn_;
2915 // Get the section to use for IRELATIVE relocs, creating it if
2916 // necessary. These go in .rela.dyn, but only after all other dynamic
2917 // relocations. They need to follow the other dynamic relocations so
2918 // that they can refer to global variables initialized by those
2921 template<int size, bool big_endian>
2922 typename Target_aarch64<size, big_endian>::Reloc_section*
2923 Target_aarch64<size, big_endian>::rela_irelative_section(Layout* layout)
2925 if (this->rela_irelative_ == NULL)
2927 // Make sure we have already created the dynamic reloc section.
2928 this->rela_dyn_section(layout);
2929 this->rela_irelative_ = new Reloc_section(false);
2930 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2931 elfcpp::SHF_ALLOC, this->rela_irelative_,
2932 ORDER_DYNAMIC_RELOCS, false);
2933 gold_assert(this->rela_dyn_->output_section()
2934 == this->rela_irelative_->output_section());
2936 return this->rela_irelative_;
2940 // do_make_elf_object to override the same function in the base class. We need
2941 // to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
2942 // store backend specific information. Hence we need to have our own ELF object
2945 template<int size, bool big_endian>
2947 Target_aarch64<size, big_endian>::do_make_elf_object(
2948 const std::string& name,
2949 Input_file* input_file,
2950 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2952 int et = ehdr.get_e_type();
2953 // ET_EXEC files are valid input for --just-symbols/-R,
2954 // and we treat them as relocatable objects.
2955 if (et == elfcpp::ET_EXEC && input_file->just_symbols())
2956 return Sized_target<size, big_endian>::do_make_elf_object(
2957 name, input_file, offset, ehdr);
2958 else if (et == elfcpp::ET_REL)
2960 AArch64_relobj<size, big_endian>* obj =
2961 new AArch64_relobj<size, big_endian>(name, input_file, offset, ehdr);
2965 else if (et == elfcpp::ET_DYN)
2967 // Keep base implementation.
2968 Sized_dynobj<size, big_endian>* obj =
2969 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2975 gold_error(_("%s: unsupported ELF file type %d"),
2982 // Scan a relocation for stub generation.
2984 template<int size, bool big_endian>
2986 Target_aarch64<size, big_endian>::scan_reloc_for_stub(
2987 const Relocate_info<size, big_endian>* relinfo,
2988 unsigned int r_type,
2989 const Sized_symbol<size>* gsym,
2991 const Symbol_value<size>* psymval,
2992 typename elfcpp::Elf_types<size>::Elf_Swxword addend,
2995 const AArch64_relobj<size, big_endian>* aarch64_relobj =
2996 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
2998 Symbol_value<size> symval;
3001 const AArch64_reloc_property* arp = aarch64_reloc_property_table->
3002 get_reloc_property(r_type);
3003 if (gsym->use_plt_offset(arp->reference_flags()))
3005 // This uses a PLT, change the symbol value.
3006 symval.set_output_value(this->plt_section()->address()
3007 + gsym->plt_offset());
3010 else if (gsym->is_undefined())
3011 // There is no need to generate a stub symbol is undefined.
3015 // Get the symbol value.
3016 typename Symbol_value<size>::Value value = psymval->value(aarch64_relobj, 0);
3018 // Owing to pipelining, the PC relative branches below actually skip
3019 // two instructions when the branch offset is 0.
3020 Address destination = static_cast<Address>(-1);
3023 case elfcpp::R_AARCH64_CALL26:
3024 case elfcpp::R_AARCH64_JUMP26:
3025 destination = value + addend;
3031 typename The_reloc_stub::Stub_type stub_type = The_reloc_stub::
3032 stub_type_for_reloc(r_type, address, destination);
3033 if (stub_type == The_reloc_stub::ST_NONE)
3036 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
3037 gold_assert(stub_table != NULL);
3039 The_reloc_stub_key key(stub_type, gsym, aarch64_relobj, r_sym, addend);
3040 The_reloc_stub* stub = stub_table->find_reloc_stub(key);
3043 stub = new The_reloc_stub(stub_type);
3044 stub_table->add_reloc_stub(stub, key);
3046 stub->set_destination_address(destination);
3047 } // End of Target_aarch64::scan_reloc_for_stub
3050 // This function scans a relocation section for stub generation.
3051 // The template parameter Relocate must be a class type which provides
3052 // a single function, relocate(), which implements the machine
3053 // specific part of a relocation.
3055 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
3056 // SHT_REL or SHT_RELA.
3058 // PRELOCS points to the relocation data. RELOC_COUNT is the number
3059 // of relocs. OUTPUT_SECTION is the output section.
3060 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
3061 // mapped to output offsets.
3063 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
3064 // VIEW_SIZE is the size. These refer to the input section, unless
3065 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
3066 // the output section.
3068 template<int size, bool big_endian>
3069 template<int sh_type>
3071 Target_aarch64<size, big_endian>::scan_reloc_section_for_stubs(
3072 const Relocate_info<size, big_endian>* relinfo,
3073 const unsigned char* prelocs,
3075 Output_section* /*output_section*/,
3076 bool /*needs_special_offset_handling*/,
3077 const unsigned char* /*view*/,
3078 Address view_address,
3081 typedef typename Reloc_types<sh_type,size,big_endian>::Reloc Reltype;
3083 const int reloc_size =
3084 Reloc_types<sh_type,size,big_endian>::reloc_size;
3085 AArch64_relobj<size, big_endian>* object =
3086 static_cast<AArch64_relobj<size, big_endian>*>(relinfo->object);
3087 unsigned int local_count = object->local_symbol_count();
3089 gold::Default_comdat_behavior default_comdat_behavior;
3090 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
3092 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
3094 Reltype reloc(prelocs);
3095 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
3096 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3097 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
3098 if (r_type != elfcpp::R_AARCH64_CALL26
3099 && r_type != elfcpp::R_AARCH64_JUMP26)
3102 section_offset_type offset =
3103 convert_to_section_size_type(reloc.get_r_offset());
3106 typename elfcpp::Elf_types<size>::Elf_Swxword addend =
3107 reloc.get_r_addend();
3109 const Sized_symbol<size>* sym;
3110 Symbol_value<size> symval;
3111 const Symbol_value<size> *psymval;
3112 bool is_defined_in_discarded_section;
3114 if (r_sym < local_count)
3117 psymval = object->local_symbol(r_sym);
3119 // If the local symbol belongs to a section we are discarding,
3120 // and that section is a debug section, try to find the
3121 // corresponding kept section and map this symbol to its
3122 // counterpart in the kept section. The symbol must not
3123 // correspond to a section we are folding.
3125 shndx = psymval->input_shndx(&is_ordinary);
3126 is_defined_in_discarded_section =
3128 && shndx != elfcpp::SHN_UNDEF
3129 && !object->is_section_included(shndx)
3130 && !relinfo->symtab->is_section_folded(object, shndx));
3132 // We need to compute the would-be final value of this local
3134 if (!is_defined_in_discarded_section)
3136 typedef Sized_relobj_file<size, big_endian> ObjType;
3137 typename ObjType::Compute_final_local_value_status status =
3138 object->compute_final_local_value(r_sym, psymval, &symval,
3140 if (status == ObjType::CFLV_OK)
3142 // Currently we cannot handle a branch to a target in
3143 // a merged section. If this is the case, issue an error
3144 // and also free the merge symbol value.
3145 if (!symval.has_output_value())
3147 const std::string& section_name =
3148 object->section_name(shndx);
3149 object->error(_("cannot handle branch to local %u "
3150 "in a merged section %s"),
3151 r_sym, section_name.c_str());
3157 // We cannot determine the final value.
3165 gsym = object->global_symbol(r_sym);
3166 gold_assert(gsym != NULL);
3167 if (gsym->is_forwarder())
3168 gsym = relinfo->symtab->resolve_forwards(gsym);
3170 sym = static_cast<const Sized_symbol<size>*>(gsym);
3171 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
3172 symval.set_output_symtab_index(sym->symtab_index());
3174 symval.set_no_output_symtab_entry();
3176 // We need to compute the would-be final value of this global
3178 const Symbol_table* symtab = relinfo->symtab;
3179 const Sized_symbol<size>* sized_symbol =
3180 symtab->get_sized_symbol<size>(gsym);
3181 Symbol_table::Compute_final_value_status status;
3182 typename elfcpp::Elf_types<size>::Elf_Addr value =
3183 symtab->compute_final_value<size>(sized_symbol, &status);
3185 // Skip this if the symbol has not output section.
3186 if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
3188 symval.set_output_value(value);
3190 if (gsym->type() == elfcpp::STT_TLS)
3191 symval.set_is_tls_symbol();
3192 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3193 symval.set_is_ifunc_symbol();
3196 is_defined_in_discarded_section =
3197 (gsym->is_defined_in_discarded_section()
3198 && gsym->is_undefined());
3202 Symbol_value<size> symval2;
3203 if (is_defined_in_discarded_section)
3205 if (comdat_behavior == CB_UNDETERMINED)
3207 std::string name = object->section_name(relinfo->data_shndx);
3208 comdat_behavior = default_comdat_behavior.get(name.c_str());
3210 if (comdat_behavior == CB_PRETEND)
3213 typename elfcpp::Elf_types<size>::Elf_Addr value =
3214 object->map_to_kept_section(shndx, &found);
3216 symval2.set_output_value(value + psymval->input_value());
3218 symval2.set_output_value(0);
3222 if (comdat_behavior == CB_WARNING)
3223 gold_warning_at_location(relinfo, i, offset,
3224 _("relocation refers to discarded "
3226 symval2.set_output_value(0);
3228 symval2.set_no_output_symtab_entry();
3232 // If symbol is a section symbol, we don't know the actual type of
3233 // destination. Give up.
3234 if (psymval->is_section_symbol())
3237 this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
3238 addend, view_address + offset);
3239 } // End of iterating relocs in a section
3240 } // End of Target_aarch64::scan_reloc_section_for_stubs
3243 // Scan an input section for stub generation.
3245 template<int size, bool big_endian>
3247 Target_aarch64<size, big_endian>::scan_section_for_stubs(
3248 const Relocate_info<size, big_endian>* relinfo,
3249 unsigned int sh_type,
3250 const unsigned char* prelocs,
3252 Output_section* output_section,
3253 bool needs_special_offset_handling,
3254 const unsigned char* view,
3255 Address view_address,
3256 section_size_type view_size)
3258 gold_assert(sh_type == elfcpp::SHT_RELA);
3259 this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
3264 needs_special_offset_handling,
3271 // Relocate a single stub.
3273 template<int size, bool big_endian>
3274 void Target_aarch64<size, big_endian>::
3275 relocate_stub(The_reloc_stub* stub,
3276 const The_relocate_info*,
3278 unsigned char* view,
3282 typedef AArch64_relocate_functions<size, big_endian> The_reloc_functions;
3283 typedef typename The_reloc_functions::Status The_reloc_functions_status;
3284 typedef typename elfcpp::Swap<32,big_endian>::Valtype Insntype;
3286 Insntype* ip = reinterpret_cast<Insntype*>(view);
3287 int insn_number = stub->stub_insn_number();
3288 const uint32_t* insns = stub->stub_insns();
3289 // Check the insns are really those stub insns.
3290 for (int i = 0; i < insn_number; ++i)
3292 Insntype insn = elfcpp::Swap<32,big_endian>::readval(ip + i);
3293 gold_assert(((uint32_t)insn == insns[i+1]));
3296 Address dest = stub->destination_address();
3298 switch(stub->stub_type())
3300 case The_reloc_stub::ST_ADRP_BRANCH:
3302 // 1st reloc is ADR_PREL_PG_HI21
3303 The_reloc_functions_status status =
3304 The_reloc_functions::adrp(view, dest, address);
3305 // An error should never arise in the above step. If so, please
3306 // check 'aarch64_valid_for_adrp_p'.
3307 gold_assert(status == The_reloc_functions::STATUS_OKAY);
3309 // 2nd reloc is ADD_ABS_LO12_NC
3310 const AArch64_reloc_property* arp =
3311 aarch64_reloc_property_table->get_reloc_property(
3312 elfcpp::R_AARCH64_ADD_ABS_LO12_NC);
3313 gold_assert(arp != NULL);
3314 status = The_reloc_functions::template
3315 rela_general<32>(view + 4, dest, 0, arp);
3316 // An error should never arise, it is an "_NC" relocation.
3317 gold_assert(status == The_reloc_functions::STATUS_OKAY);
3321 case The_reloc_stub::ST_LONG_BRANCH_ABS:
3322 // 1st reloc is R_AARCH64_PREL64, at offset 8
3323 elfcpp::Swap<64,big_endian>::writeval(view + 8, dest);
3326 case The_reloc_stub::ST_LONG_BRANCH_PCREL:
3328 // "PC" calculation is the 2nd insn in the stub.
3329 uint64_t offset = dest - (address + 4);
3330 // Offset is placed at offset 4 and 5.
3331 elfcpp::Swap<64,big_endian>::writeval(view + 16, offset);
3341 // A class to handle the PLT data.
3342 // This is an abstract base class that handles most of the linker details
3343 // but does not know the actual contents of PLT entries. The derived
3344 // classes below fill in those details.
3346 template<int size, bool big_endian>
3347 class Output_data_plt_aarch64 : public Output_section_data
3350 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
3352 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3354 Output_data_plt_aarch64(Layout* layout,
3356 Output_data_got_aarch64<size, big_endian>* got,
3357 Output_data_space* got_plt,
3358 Output_data_space* got_irelative)
3359 : Output_section_data(addralign), tlsdesc_rel_(NULL), irelative_rel_(NULL),
3360 got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
3361 count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
3362 { this->init(layout); }
3364 // Initialize the PLT section.
3366 init(Layout* layout);
3368 // Add an entry to the PLT.
3370 add_entry(Symbol_table*, Layout*, Symbol* gsym);
3372 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
3374 add_local_ifunc_entry(Symbol_table* symtab, Layout*,
3375 Sized_relobj_file<size, big_endian>* relobj,
3376 unsigned int local_sym_index);
3378 // Add the relocation for a PLT entry.
3380 add_relocation(Symbol_table*, Layout*, Symbol* gsym,
3381 unsigned int got_offset);
3383 // Add the reserved TLSDESC_PLT entry to the PLT.
3385 reserve_tlsdesc_entry(unsigned int got_offset)
3386 { this->tlsdesc_got_offset_ = got_offset; }
3388 // Return true if a TLSDESC_PLT entry has been reserved.
3390 has_tlsdesc_entry() const
3391 { return this->tlsdesc_got_offset_ != -1U; }
3393 // Return the GOT offset for the reserved TLSDESC_PLT entry.
3395 get_tlsdesc_got_offset() const
3396 { return this->tlsdesc_got_offset_; }
3398 // Return the PLT offset of the reserved TLSDESC_PLT entry.
3400 get_tlsdesc_plt_offset() const
3402 return (this->first_plt_entry_offset() +
3403 (this->count_ + this->irelative_count_)
3404 * this->get_plt_entry_size());
3407 // Return the .rela.plt section data.
3410 { return this->rel_; }
3412 // Return where the TLSDESC relocations should go.
3414 rela_tlsdesc(Layout*);
3416 // Return where the IRELATIVE relocations should go in the PLT
3419 rela_irelative(Symbol_table*, Layout*);
3421 // Return whether we created a section for IRELATIVE relocations.
3423 has_irelative_section() const
3424 { return this->irelative_rel_ != NULL; }
3426 // Return the number of PLT entries.
3429 { return this->count_ + this->irelative_count_; }
3431 // Return the offset of the first non-reserved PLT entry.
3433 first_plt_entry_offset() const
3434 { return this->do_first_plt_entry_offset(); }
3436 // Return the size of a PLT entry.
3438 get_plt_entry_size() const
3439 { return this->do_get_plt_entry_size(); }
3441 // Return the reserved tlsdesc entry size.
3443 get_plt_tlsdesc_entry_size() const
3444 { return this->do_get_plt_tlsdesc_entry_size(); }
3446 // Return the PLT address to use for a global symbol.
3448 address_for_global(const Symbol*);
3450 // Return the PLT address to use for a local symbol.
3452 address_for_local(const Relobj*, unsigned int symndx);
3455 // Fill in the first PLT entry.
3457 fill_first_plt_entry(unsigned char* pov,
3458 Address got_address,
3459 Address plt_address)
3460 { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
3462 // Fill in a normal PLT entry.
3464 fill_plt_entry(unsigned char* pov,
3465 Address got_address,
3466 Address plt_address,
3467 unsigned int got_offset,
3468 unsigned int plt_offset)
3470 this->do_fill_plt_entry(pov, got_address, plt_address,
3471 got_offset, plt_offset);
3474 // Fill in the reserved TLSDESC PLT entry.
3476 fill_tlsdesc_entry(unsigned char* pov,
3477 Address gotplt_address,
3478 Address plt_address,
3480 unsigned int tlsdesc_got_offset,
3481 unsigned int plt_offset)
3483 this->do_fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
3484 tlsdesc_got_offset, plt_offset);
3487 virtual unsigned int
3488 do_first_plt_entry_offset() const = 0;
3490 virtual unsigned int
3491 do_get_plt_entry_size() const = 0;
3493 virtual unsigned int
3494 do_get_plt_tlsdesc_entry_size() const = 0;
3497 do_fill_first_plt_entry(unsigned char* pov,
3499 Address plt_addr) = 0;
3502 do_fill_plt_entry(unsigned char* pov,
3503 Address got_address,
3504 Address plt_address,
3505 unsigned int got_offset,
3506 unsigned int plt_offset) = 0;
3509 do_fill_tlsdesc_entry(unsigned char* pov,
3510 Address gotplt_address,
3511 Address plt_address,
3513 unsigned int tlsdesc_got_offset,
3514 unsigned int plt_offset) = 0;
3517 do_adjust_output_section(Output_section* os);
3519 // Write to a map file.
3521 do_print_to_mapfile(Mapfile* mapfile) const
3522 { mapfile->print_output_data(this, _("** PLT")); }
3525 // Set the final size.
3527 set_final_data_size();
3529 // Write out the PLT data.
3531 do_write(Output_file*);
3533 // The reloc section.
3534 Reloc_section* rel_;
3536 // The TLSDESC relocs, if necessary. These must follow the regular
3538 Reloc_section* tlsdesc_rel_;
3540 // The IRELATIVE relocs, if necessary. These must follow the
3541 // regular PLT relocations.
3542 Reloc_section* irelative_rel_;
3544 // The .got section.
3545 Output_data_got_aarch64<size, big_endian>* got_;
3547 // The .got.plt section.
3548 Output_data_space* got_plt_;
3550 // The part of the .got.plt section used for IRELATIVE relocs.
3551 Output_data_space* got_irelative_;
3553 // The number of PLT entries.
3554 unsigned int count_;
3556 // Number of PLT entries with R_AARCH64_IRELATIVE relocs. These
3557 // follow the regular PLT entries.
3558 unsigned int irelative_count_;
3560 // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
3561 // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
3562 // indicates an offset is not allocated.
3563 unsigned int tlsdesc_got_offset_;
3566 // Initialize the PLT section.
3568 template<int size, bool big_endian>
3570 Output_data_plt_aarch64<size, big_endian>::init(Layout* layout)
3572 this->rel_ = new Reloc_section(false);
3573 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3574 elfcpp::SHF_ALLOC, this->rel_,
3575 ORDER_DYNAMIC_PLT_RELOCS, false);
3578 template<int size, bool big_endian>
3580 Output_data_plt_aarch64<size, big_endian>::do_adjust_output_section(
3583 os->set_entsize(this->get_plt_entry_size());
3586 // Add an entry to the PLT.
3588 template<int size, bool big_endian>
3590 Output_data_plt_aarch64<size, big_endian>::add_entry(Symbol_table* symtab,
3591 Layout* layout, Symbol* gsym)
3593 gold_assert(!gsym->has_plt_offset());
3595 unsigned int* pcount;
3596 unsigned int plt_reserved;
3597 Output_section_data_build* got;
3599 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3600 && gsym->can_use_relative_reloc(false))
3602 pcount = &this->irelative_count_;
3604 got = this->got_irelative_;
3608 pcount = &this->count_;
3609 plt_reserved = this->first_plt_entry_offset();
3610 got = this->got_plt_;
3613 gsym->set_plt_offset((*pcount) * this->get_plt_entry_size()
3618 section_offset_type got_offset = got->current_data_size();
3620 // Every PLT entry needs a GOT entry which points back to the PLT
3621 // entry (this will be changed by the dynamic linker, normally
3622 // lazily when the function is called).
3623 got->set_current_data_size(got_offset + size / 8);
3625 // Every PLT entry needs a reloc.
3626 this->add_relocation(symtab, layout, gsym, got_offset);
3628 // Note that we don't need to save the symbol. The contents of the
3629 // PLT are independent of which symbols are used. The symbols only
3630 // appear in the relocations.
3633 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
3636 template<int size, bool big_endian>
3638 Output_data_plt_aarch64<size, big_endian>::add_local_ifunc_entry(
3639 Symbol_table* symtab,
3641 Sized_relobj_file<size, big_endian>* relobj,
3642 unsigned int local_sym_index)
3644 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
3645 ++this->irelative_count_;
3647 section_offset_type got_offset = this->got_irelative_->current_data_size();
3649 // Every PLT entry needs a GOT entry which points back to the PLT
3651 this->got_irelative_->set_current_data_size(got_offset + size / 8);
3653 // Every PLT entry needs a reloc.
3654 Reloc_section* rela = this->rela_irelative(symtab, layout);
3655 rela->add_symbolless_local_addend(relobj, local_sym_index,
3656 elfcpp::R_AARCH64_IRELATIVE,
3657 this->got_irelative_, got_offset, 0);
3662 // Add the relocation for a PLT entry.
3664 template<int size, bool big_endian>
3666 Output_data_plt_aarch64<size, big_endian>::add_relocation(
3667 Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
3669 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3670 && gsym->can_use_relative_reloc(false))
3672 Reloc_section* rela = this->rela_irelative(symtab, layout);
3673 rela->add_symbolless_global_addend(gsym, elfcpp::R_AARCH64_IRELATIVE,
3674 this->got_irelative_, got_offset, 0);
3678 gsym->set_needs_dynsym_entry();
3679 this->rel_->add_global(gsym, elfcpp::R_AARCH64_JUMP_SLOT, this->got_plt_,
3684 // Return where the TLSDESC relocations should go, creating it if
3685 // necessary. These follow the JUMP_SLOT relocations.
3687 template<int size, bool big_endian>
3688 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
3689 Output_data_plt_aarch64<size, big_endian>::rela_tlsdesc(Layout* layout)
3691 if (this->tlsdesc_rel_ == NULL)
3693 this->tlsdesc_rel_ = new Reloc_section(false);
3694 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3695 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
3696 ORDER_DYNAMIC_PLT_RELOCS, false);
3697 gold_assert(this->tlsdesc_rel_->output_section()
3698 == this->rel_->output_section());
3700 return this->tlsdesc_rel_;
3703 // Return where the IRELATIVE relocations should go in the PLT. These
3704 // follow the JUMP_SLOT and the TLSDESC relocations.
3706 template<int size, bool big_endian>
3707 typename Output_data_plt_aarch64<size, big_endian>::Reloc_section*
3708 Output_data_plt_aarch64<size, big_endian>::rela_irelative(Symbol_table* symtab,
3711 if (this->irelative_rel_ == NULL)
3713 // Make sure we have a place for the TLSDESC relocations, in
3714 // case we see any later on.
3715 this->rela_tlsdesc(layout);
3716 this->irelative_rel_ = new Reloc_section(false);
3717 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3718 elfcpp::SHF_ALLOC, this->irelative_rel_,
3719 ORDER_DYNAMIC_PLT_RELOCS, false);
3720 gold_assert(this->irelative_rel_->output_section()
3721 == this->rel_->output_section());
3723 if (parameters->doing_static_link())
3725 // A statically linked executable will only have a .rela.plt
3726 // section to hold R_AARCH64_IRELATIVE relocs for
3727 // STT_GNU_IFUNC symbols. The library will use these
3728 // symbols to locate the IRELATIVE relocs at program startup
3730 symtab->define_in_output_data("__rela_iplt_start", NULL,
3731 Symbol_table::PREDEFINED,
3732 this->irelative_rel_, 0, 0,
3733 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
3734 elfcpp::STV_HIDDEN, 0, false, true);
3735 symtab->define_in_output_data("__rela_iplt_end", NULL,
3736 Symbol_table::PREDEFINED,
3737 this->irelative_rel_, 0, 0,
3738 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
3739 elfcpp::STV_HIDDEN, 0, true, true);
3742 return this->irelative_rel_;
3745 // Return the PLT address to use for a global symbol.
3747 template<int size, bool big_endian>
3749 Output_data_plt_aarch64<size, big_endian>::address_for_global(
3752 uint64_t offset = 0;
3753 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3754 && gsym->can_use_relative_reloc(false))
3755 offset = (this->first_plt_entry_offset() +
3756 this->count_ * this->get_plt_entry_size());
3757 return this->address() + offset + gsym->plt_offset();
3760 // Return the PLT address to use for a local symbol. These are always
3761 // IRELATIVE relocs.
3763 template<int size, bool big_endian>
3765 Output_data_plt_aarch64<size, big_endian>::address_for_local(
3766 const Relobj* object,
3769 return (this->address()
3770 + this->first_plt_entry_offset()
3771 + this->count_ * this->get_plt_entry_size()
3772 + object->local_plt_offset(r_sym));
3775 // Set the final size.
3777 template<int size, bool big_endian>
3779 Output_data_plt_aarch64<size, big_endian>::set_final_data_size()
3781 unsigned int count = this->count_ + this->irelative_count_;
3782 unsigned int extra_size = 0;
3783 if (this->has_tlsdesc_entry())
3784 extra_size += this->get_plt_tlsdesc_entry_size();
3785 this->set_data_size(this->first_plt_entry_offset()
3786 + count * this->get_plt_entry_size()
3790 template<int size, bool big_endian>
3791 class Output_data_plt_aarch64_standard :
3792 public Output_data_plt_aarch64<size, big_endian>
3795 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3796 Output_data_plt_aarch64_standard(
3798 Output_data_got_aarch64<size, big_endian>* got,
3799 Output_data_space* got_plt,
3800 Output_data_space* got_irelative)
3801 : Output_data_plt_aarch64<size, big_endian>(layout,
3808 // Return the offset of the first non-reserved PLT entry.
3809 virtual unsigned int
3810 do_first_plt_entry_offset() const
3811 { return this->first_plt_entry_size; }
3813 // Return the size of a PLT entry
3814 virtual unsigned int
3815 do_get_plt_entry_size() const
3816 { return this->plt_entry_size; }
3818 // Return the size of a tlsdesc entry
3819 virtual unsigned int
3820 do_get_plt_tlsdesc_entry_size() const
3821 { return this->plt_tlsdesc_entry_size; }
3824 do_fill_first_plt_entry(unsigned char* pov,
3825 Address got_address,
3826 Address plt_address);
3829 do_fill_plt_entry(unsigned char* pov,
3830 Address got_address,
3831 Address plt_address,
3832 unsigned int got_offset,
3833 unsigned int plt_offset);
3836 do_fill_tlsdesc_entry(unsigned char* pov,
3837 Address gotplt_address,
3838 Address plt_address,
3840 unsigned int tlsdesc_got_offset,
3841 unsigned int plt_offset);
3844 // The size of the first plt entry size.
3845 static const int first_plt_entry_size = 32;
3846 // The size of the plt entry size.
3847 static const int plt_entry_size = 16;
3848 // The size of the plt tlsdesc entry size.
3849 static const int plt_tlsdesc_entry_size = 32;
3850 // Template for the first PLT entry.
3851 static const uint32_t first_plt_entry[first_plt_entry_size / 4];
3852 // Template for subsequent PLT entries.
3853 static const uint32_t plt_entry[plt_entry_size / 4];
3854 // The reserved TLSDESC entry in the PLT for an executable.
3855 static const uint32_t tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4];
3858 // The first entry in the PLT for an executable.
3862 Output_data_plt_aarch64_standard<32, false>::
3863 first_plt_entry[first_plt_entry_size / 4] =
3865 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
3866 0x90000010, /* adrp x16, PLT_GOT+0x8 */
3867 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
3868 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
3869 0xd61f0220, /* br x17 */
3870 0xd503201f, /* nop */
3871 0xd503201f, /* nop */
3872 0xd503201f, /* nop */
3878 Output_data_plt_aarch64_standard<32, true>::
3879 first_plt_entry[first_plt_entry_size / 4] =
3881 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
3882 0x90000010, /* adrp x16, PLT_GOT+0x8 */
3883 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
3884 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
3885 0xd61f0220, /* br x17 */
3886 0xd503201f, /* nop */
3887 0xd503201f, /* nop */
3888 0xd503201f, /* nop */
3894 Output_data_plt_aarch64_standard<64, false>::
3895 first_plt_entry[first_plt_entry_size / 4] =
3897 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
3898 0x90000010, /* adrp x16, PLT_GOT+16 */
3899 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
3900 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
3901 0xd61f0220, /* br x17 */
3902 0xd503201f, /* nop */
3903 0xd503201f, /* nop */
3904 0xd503201f, /* nop */
3910 Output_data_plt_aarch64_standard<64, true>::
3911 first_plt_entry[first_plt_entry_size / 4] =
3913 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
3914 0x90000010, /* adrp x16, PLT_GOT+16 */
3915 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
3916 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
3917 0xd61f0220, /* br x17 */
3918 0xd503201f, /* nop */
3919 0xd503201f, /* nop */
3920 0xd503201f, /* nop */
3926 Output_data_plt_aarch64_standard<32, false>::
3927 plt_entry[plt_entry_size / 4] =
3929 0x90000010, /* adrp x16, PLTGOT + n * 4 */
3930 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
3931 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
3932 0xd61f0220, /* br x17. */
3938 Output_data_plt_aarch64_standard<32, true>::
3939 plt_entry[plt_entry_size / 4] =
3941 0x90000010, /* adrp x16, PLTGOT + n * 4 */
3942 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
3943 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
3944 0xd61f0220, /* br x17. */
3950 Output_data_plt_aarch64_standard<64, false>::
3951 plt_entry[plt_entry_size / 4] =
3953 0x90000010, /* adrp x16, PLTGOT + n * 8 */
3954 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
3955 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
3956 0xd61f0220, /* br x17. */
3962 Output_data_plt_aarch64_standard<64, true>::
3963 plt_entry[plt_entry_size / 4] =
3965 0x90000010, /* adrp x16, PLTGOT + n * 8 */
3966 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
3967 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
3968 0xd61f0220, /* br x17. */
3972 template<int size, bool big_endian>
3974 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_first_plt_entry(
3976 Address got_address,
3977 Address plt_address)
3979 // PLT0 of the small PLT looks like this in ELF64 -
3980 // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
3981 // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
3982 // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
3984 // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
3985 // GOTPLT entry for this.
3987 // PLT0 will be slightly different in ELF32 due to different got entry
3989 memcpy(pov, this->first_plt_entry, this->first_plt_entry_size);
3990 Address gotplt_2nd_ent = got_address + (size / 8) * 2;
3992 // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
3993 // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
3994 // FIXME: This only works for 64bit
3995 AArch64_relocate_functions<size, big_endian>::adrp(pov + 4,
3996 gotplt_2nd_ent, plt_address + 4);
3998 // Fill in R_AARCH64_LDST8_LO12
3999 elfcpp::Swap<32, big_endian>::writeval(
4001 ((this->first_plt_entry[2] & 0xffc003ff)
4002 | ((gotplt_2nd_ent & 0xff8) << 7)));
4004 // Fill in R_AARCH64_ADD_ABS_LO12
4005 elfcpp::Swap<32, big_endian>::writeval(
4007 ((this->first_plt_entry[3] & 0xffc003ff)
4008 | ((gotplt_2nd_ent & 0xfff) << 10)));
4012 // Subsequent entries in the PLT for an executable.
4013 // FIXME: This only works for 64bit
4015 template<int size, bool big_endian>
4017 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_plt_entry(
4019 Address got_address,
4020 Address plt_address,
4021 unsigned int got_offset,
4022 unsigned int plt_offset)
4024 memcpy(pov, this->plt_entry, this->plt_entry_size);
4026 Address gotplt_entry_address = got_address + got_offset;
4027 Address plt_entry_address = plt_address + plt_offset;
4029 // Fill in R_AARCH64_PCREL_ADR_HI21
4030 AArch64_relocate_functions<size, big_endian>::adrp(
4032 gotplt_entry_address,
4035 // Fill in R_AARCH64_LDST64_ABS_LO12
4036 elfcpp::Swap<32, big_endian>::writeval(
4038 ((this->plt_entry[1] & 0xffc003ff)
4039 | ((gotplt_entry_address & 0xff8) << 7)));
4041 // Fill in R_AARCH64_ADD_ABS_LO12
4042 elfcpp::Swap<32, big_endian>::writeval(
4044 ((this->plt_entry[2] & 0xffc003ff)
4045 | ((gotplt_entry_address & 0xfff) <<10)));
4052 Output_data_plt_aarch64_standard<32, false>::
4053 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4055 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4056 0x90000002, /* adrp x2, 0 */
4057 0x90000003, /* adrp x3, 0 */
4058 0xb9400042, /* ldr w2, [w2, #0] */
4059 0x11000063, /* add w3, w3, 0 */
4060 0xd61f0040, /* br x2 */
4061 0xd503201f, /* nop */
4062 0xd503201f, /* nop */
4067 Output_data_plt_aarch64_standard<32, true>::
4068 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4070 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4071 0x90000002, /* adrp x2, 0 */
4072 0x90000003, /* adrp x3, 0 */
4073 0xb9400042, /* ldr w2, [w2, #0] */
4074 0x11000063, /* add w3, w3, 0 */
4075 0xd61f0040, /* br x2 */
4076 0xd503201f, /* nop */
4077 0xd503201f, /* nop */
4082 Output_data_plt_aarch64_standard<64, false>::
4083 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4085 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4086 0x90000002, /* adrp x2, 0 */
4087 0x90000003, /* adrp x3, 0 */
4088 0xf9400042, /* ldr x2, [x2, #0] */
4089 0x91000063, /* add x3, x3, 0 */
4090 0xd61f0040, /* br x2 */
4091 0xd503201f, /* nop */
4092 0xd503201f, /* nop */
4097 Output_data_plt_aarch64_standard<64, true>::
4098 tlsdesc_plt_entry[plt_tlsdesc_entry_size / 4] =
4100 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4101 0x90000002, /* adrp x2, 0 */
4102 0x90000003, /* adrp x3, 0 */
4103 0xf9400042, /* ldr x2, [x2, #0] */
4104 0x91000063, /* add x3, x3, 0 */
4105 0xd61f0040, /* br x2 */
4106 0xd503201f, /* nop */
4107 0xd503201f, /* nop */
4110 template<int size, bool big_endian>
4112 Output_data_plt_aarch64_standard<size, big_endian>::do_fill_tlsdesc_entry(
4114 Address gotplt_address,
4115 Address plt_address,
4117 unsigned int tlsdesc_got_offset,
4118 unsigned int plt_offset)
4120 memcpy(pov, tlsdesc_plt_entry, plt_tlsdesc_entry_size);
4122 // move DT_TLSDESC_GOT address into x2
4123 // move .got.plt address into x3
4124 Address tlsdesc_got_entry = got_base + tlsdesc_got_offset;
4125 Address plt_entry_address = plt_address + plt_offset;
4127 // R_AARCH64_ADR_PREL_PG_HI21
4128 AArch64_relocate_functions<size, big_endian>::adrp(
4131 plt_entry_address + 4);
4133 // R_AARCH64_ADR_PREL_PG_HI21
4134 AArch64_relocate_functions<size, big_endian>::adrp(
4137 plt_entry_address + 8);
4139 // R_AARCH64_LDST64_ABS_LO12
4140 elfcpp::Swap<32, big_endian>::writeval(
4142 ((this->tlsdesc_plt_entry[3] & 0xffc003ff)
4143 | ((tlsdesc_got_entry & 0xff8) << 7)));
4145 // R_AARCH64_ADD_ABS_LO12
4146 elfcpp::Swap<32, big_endian>::writeval(
4148 ((this->tlsdesc_plt_entry[4] & 0xffc003ff)
4149 | ((gotplt_address & 0xfff) << 10)));
4152 // Write out the PLT. This uses the hand-coded instructions above,
4153 // and adjusts them as needed. This is specified by the AMD64 ABI.
4155 template<int size, bool big_endian>
4157 Output_data_plt_aarch64<size, big_endian>::do_write(Output_file* of)
4159 const off_t offset = this->offset();
4160 const section_size_type oview_size =
4161 convert_to_section_size_type(this->data_size());
4162 unsigned char* const oview = of->get_output_view(offset, oview_size);
4164 const off_t got_file_offset = this->got_plt_->offset();
4165 gold_assert(got_file_offset + this->got_plt_->data_size()
4166 == this->got_irelative_->offset());
4168 const section_size_type got_size =
4169 convert_to_section_size_type(this->got_plt_->data_size()
4170 + this->got_irelative_->data_size());
4171 unsigned char* const got_view = of->get_output_view(got_file_offset,
4174 unsigned char* pov = oview;
4176 // The base address of the .plt section.
4177 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
4178 // The base address of the PLT portion of the .got section.
4179 typename elfcpp::Elf_types<size>::Elf_Addr gotplt_address
4180 = this->got_plt_->address();
4182 this->fill_first_plt_entry(pov, gotplt_address, plt_address);
4183 pov += this->first_plt_entry_offset();
4185 // The first three entries in .got.plt are reserved.
4186 unsigned char* got_pov = got_view;
4187 memset(got_pov, 0, size / 8 * AARCH64_GOTPLT_RESERVE_COUNT);
4188 got_pov += (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
4190 unsigned int plt_offset = this->first_plt_entry_offset();
4191 unsigned int got_offset = (size / 8) * AARCH64_GOTPLT_RESERVE_COUNT;
4192 const unsigned int count = this->count_ + this->irelative_count_;
4193 for (unsigned int plt_index = 0;
4196 pov += this->get_plt_entry_size(),
4197 got_pov += size / 8,
4198 plt_offset += this->get_plt_entry_size(),
4199 got_offset += size / 8)
4201 // Set and adjust the PLT entry itself.
4202 this->fill_plt_entry(pov, gotplt_address, plt_address,
4203 got_offset, plt_offset);
4205 // Set the entry in the GOT, which points to plt0.
4206 elfcpp::Swap<size, big_endian>::writeval(got_pov, plt_address);
4209 if (this->has_tlsdesc_entry())
4211 // Set and adjust the reserved TLSDESC PLT entry.
4212 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
4213 // The base address of the .base section.
4214 typename elfcpp::Elf_types<size>::Elf_Addr got_base =
4215 this->got_->address();
4216 this->fill_tlsdesc_entry(pov, gotplt_address, plt_address, got_base,
4217 tlsdesc_got_offset, plt_offset);
4218 pov += this->get_plt_tlsdesc_entry_size();
4221 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
4222 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
4224 of->write_output_view(offset, oview_size, oview);
4225 of->write_output_view(got_file_offset, got_size, got_view);
4228 // Telling how to update the immediate field of an instruction.
4229 struct AArch64_howto
4231 // The immediate field mask.
4232 elfcpp::Elf_Xword dst_mask;
4234 // The offset to apply relocation immediate
4237 // The second part offset, if the immediate field has two parts.
4238 // -1 if the immediate field has only one part.
4242 static const AArch64_howto aarch64_howto[AArch64_reloc_property::INST_NUM] =
4244 {0, -1, -1}, // DATA
4245 {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
4246 {0xffffe0, 5, -1}, // LD [23:5]-imm19
4247 {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
4248 {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
4249 {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
4250 {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
4251 {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
4252 {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
4253 {0x3ffffff, 0, -1}, // B [25:0]-imm26
4254 {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
4257 // AArch64 relocate function class
4259 template<int size, bool big_endian>
4260 class AArch64_relocate_functions
4265 STATUS_OKAY, // No error during relocation.
4266 STATUS_OVERFLOW, // Relocation overflow.
4267 STATUS_BAD_RELOC, // Relocation cannot be applied.
4270 typedef AArch64_relocate_functions<size, big_endian> This;
4271 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4272 typedef Relocate_info<size, big_endian> The_relocate_info;
4273 typedef AArch64_relobj<size, big_endian> The_aarch64_relobj;
4274 typedef Reloc_stub<size, big_endian> The_reloc_stub;
4275 typedef typename The_reloc_stub::Stub_type The_reloc_stub_type;
4276 typedef Stub_table<size, big_endian> The_stub_table;
4277 typedef elfcpp::Rela<size, big_endian> The_rela;
4278 typedef typename elfcpp::Swap<size, big_endian>::Valtype AArch64_valtype;
4280 // Return the page address of the address.
4281 // Page(address) = address & ~0xFFF
4283 static inline AArch64_valtype
4284 Page(Address address)
4286 return (address & (~static_cast<Address>(0xFFF)));
4290 // Update instruction (pointed by view) with selected bits (immed).
4291 // val = (val & ~dst_mask) | (immed << doffset)
4293 template<int valsize>
4295 update_view(unsigned char* view,
4296 AArch64_valtype immed,
4297 elfcpp::Elf_Xword doffset,
4298 elfcpp::Elf_Xword dst_mask)
4300 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4301 Valtype* wv = reinterpret_cast<Valtype*>(view);
4302 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
4304 // Clear immediate fields.
4306 elfcpp::Swap<valsize, big_endian>::writeval(wv,
4307 static_cast<Valtype>(val | (immed << doffset)));
4310 // Update two parts of an instruction (pointed by view) with selected
4311 // bits (immed1 and immed2).
4312 // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
4314 template<int valsize>
4316 update_view_two_parts(
4317 unsigned char* view,
4318 AArch64_valtype immed1,
4319 AArch64_valtype immed2,
4320 elfcpp::Elf_Xword doffset1,
4321 elfcpp::Elf_Xword doffset2,
4322 elfcpp::Elf_Xword dst_mask)
4324 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4325 Valtype* wv = reinterpret_cast<Valtype*>(view);
4326 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
4328 elfcpp::Swap<valsize, big_endian>::writeval(wv,
4329 static_cast<Valtype>(val | (immed1 << doffset1) |
4330 (immed2 << doffset2)));
4333 // Update adr or adrp instruction with immed.
4334 // In adr and adrp: [30:29] immlo [23:5] immhi
4337 update_adr(unsigned char* view, AArch64_valtype immed)
4339 elfcpp::Elf_Xword dst_mask = (0x3 << 29) | (0x7ffff << 5);
4340 This::template update_view_two_parts<32>(
4343 (immed & 0x1ffffc) >> 2,
4349 // Update movz/movn instruction with bits immed.
4350 // Set instruction to movz if is_movz is true, otherwise set instruction
4354 update_movnz(unsigned char* view,
4355 AArch64_valtype immed,
4358 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4359 Valtype* wv = reinterpret_cast<Valtype*>(view);
4360 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
4362 const elfcpp::Elf_Xword doffset =
4363 aarch64_howto[AArch64_reloc_property::INST_MOVW].doffset;
4364 const elfcpp::Elf_Xword dst_mask =
4365 aarch64_howto[AArch64_reloc_property::INST_MOVW].dst_mask;
4367 // Clear immediate fields and opc code.
4368 val &= ~(dst_mask | (0x3 << 29));
4370 // Set instruction to movz or movn.
4371 // movz: [30:29] is 10 movn: [30:29] is 00
4375 elfcpp::Swap<32, big_endian>::writeval(wv,
4376 static_cast<Valtype>(val | (immed << doffset)));
4379 // Update selected bits in text.
4381 template<int valsize>
4382 static inline typename This::Status
4383 reloc_common(unsigned char* view, Address x,
4384 const AArch64_reloc_property* reloc_property)
4386 // Select bits from X.
4387 Address immed = reloc_property->select_x_value(x);
4390 const AArch64_reloc_property::Reloc_inst inst =
4391 reloc_property->reloc_inst();
4392 // If it is a data relocation or instruction has 2 parts of immediate
4393 // fields, you should not call pcrela_general.
4394 gold_assert(aarch64_howto[inst].doffset2 == -1 &&
4395 aarch64_howto[inst].doffset != -1);
4396 This::template update_view<valsize>(view, immed,
4397 aarch64_howto[inst].doffset,
4398 aarch64_howto[inst].dst_mask);
4400 // Do check overflow or alignment if needed.
4401 return (reloc_property->checkup_x_value(x)
4403 : This::STATUS_OVERFLOW);
4408 // Do a simple rela relocation at unaligned addresses.
4410 template<int valsize>
4411 static inline typename This::Status
4412 rela_ua(unsigned char* view,
4413 const Sized_relobj_file<size, big_endian>* object,
4414 const Symbol_value<size>* psymval,
4415 AArch64_valtype addend,
4416 const AArch64_reloc_property* reloc_property)
4418 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
4420 typename elfcpp::Elf_types<size>::Elf_Addr x =
4421 psymval->value(object, addend);
4422 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
4423 static_cast<Valtype>(x));
4424 return (reloc_property->checkup_x_value(x)
4426 : This::STATUS_OVERFLOW);
4429 // Do a simple pc-relative relocation at unaligned addresses.
4431 template<int valsize>
4432 static inline typename This::Status
4433 pcrela_ua(unsigned char* view,
4434 const Sized_relobj_file<size, big_endian>* object,
4435 const Symbol_value<size>* psymval,
4436 AArch64_valtype addend,
4438 const AArch64_reloc_property* reloc_property)
4440 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
4442 Address x = psymval->value(object, addend) - address;
4443 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view,
4444 static_cast<Valtype>(x));
4445 return (reloc_property->checkup_x_value(x)
4447 : This::STATUS_OVERFLOW);
4450 // Do a simple rela relocation at aligned addresses.
4452 template<int valsize>
4453 static inline typename This::Status
4455 unsigned char* view,
4456 const Sized_relobj_file<size, big_endian>* object,
4457 const Symbol_value<size>* psymval,
4458 AArch64_valtype addend,
4459 const AArch64_reloc_property* reloc_property)
4461 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
4462 Valtype* wv = reinterpret_cast<Valtype*>(view);
4463 Address x = psymval->value(object, addend);
4464 elfcpp::Swap<valsize, big_endian>::writeval(wv,static_cast<Valtype>(x));
4465 return (reloc_property->checkup_x_value(x)
4467 : This::STATUS_OVERFLOW);
4470 // Do relocate. Update selected bits in text.
4471 // new_val = (val & ~dst_mask) | (immed << doffset)
4473 template<int valsize>
4474 static inline typename This::Status
4475 rela_general(unsigned char* view,
4476 const Sized_relobj_file<size, big_endian>* object,
4477 const Symbol_value<size>* psymval,
4478 AArch64_valtype addend,
4479 const AArch64_reloc_property* reloc_property)
4481 // Calculate relocation.
4482 Address x = psymval->value(object, addend);
4483 return This::template reloc_common<valsize>(view, x, reloc_property);
4486 // Do relocate. Update selected bits in text.
4487 // new val = (val & ~dst_mask) | (immed << doffset)
4489 template<int valsize>
4490 static inline typename This::Status
4492 unsigned char* view,
4494 AArch64_valtype addend,
4495 const AArch64_reloc_property* reloc_property)
4497 // Calculate relocation.
4498 Address x = s + addend;
4499 return This::template reloc_common<valsize>(view, x, reloc_property);
4502 // Do address relative relocate. Update selected bits in text.
4503 // new val = (val & ~dst_mask) | (immed << doffset)
4505 template<int valsize>
4506 static inline typename This::Status
4508 unsigned char* view,
4509 const Sized_relobj_file<size, big_endian>* object,
4510 const Symbol_value<size>* psymval,
4511 AArch64_valtype addend,
4513 const AArch64_reloc_property* reloc_property)
4515 // Calculate relocation.
4516 Address x = psymval->value(object, addend) - address;
4517 return This::template reloc_common<valsize>(view, x, reloc_property);
4521 // Calculate (S + A) - address, update adr instruction.
4523 static inline typename This::Status
4524 adr(unsigned char* view,
4525 const Sized_relobj_file<size, big_endian>* object,
4526 const Symbol_value<size>* psymval,
4529 const AArch64_reloc_property* /* reloc_property */)
4531 AArch64_valtype x = psymval->value(object, addend) - address;
4532 // Pick bits [20:0] of X.
4533 AArch64_valtype immed = x & 0x1fffff;
4534 update_adr(view, immed);
4535 // Check -2^20 <= X < 2^20
4536 return (size == 64 && Bits<21>::has_overflow((x))
4537 ? This::STATUS_OVERFLOW
4538 : This::STATUS_OKAY);
4541 // Calculate PG(S+A) - PG(address), update adrp instruction.
4542 // R_AARCH64_ADR_PREL_PG_HI21
4544 static inline typename This::Status
4546 unsigned char* view,
4550 AArch64_valtype x = This::Page(sa) - This::Page(address);
4551 // Pick [32:12] of X.
4552 AArch64_valtype immed = (x >> 12) & 0x1fffff;
4553 update_adr(view, immed);
4554 // Check -2^32 <= X < 2^32
4555 return (size == 64 && Bits<33>::has_overflow((x))
4556 ? This::STATUS_OVERFLOW
4557 : This::STATUS_OKAY);
4560 // Calculate PG(S+A) - PG(address), update adrp instruction.
4561 // R_AARCH64_ADR_PREL_PG_HI21
4563 static inline typename This::Status
4564 adrp(unsigned char* view,
4565 const Sized_relobj_file<size, big_endian>* object,
4566 const Symbol_value<size>* psymval,
4569 const AArch64_reloc_property* reloc_property)
4571 Address sa = psymval->value(object, addend);
4572 AArch64_valtype x = This::Page(sa) - This::Page(address);
4573 // Pick [32:12] of X.
4574 AArch64_valtype immed = (x >> 12) & 0x1fffff;
4575 update_adr(view, immed);
4576 return (reloc_property->checkup_x_value(x)
4578 : This::STATUS_OVERFLOW);
4581 // Update mov[n/z] instruction. Check overflow if needed.
4582 // If X >=0, set the instruction to movz and its immediate value to the
4584 // If X < 0, set the instruction to movn and its immediate value to
4585 // NOT (selected bits of).
4587 static inline typename This::Status
4588 movnz(unsigned char* view,
4590 const AArch64_reloc_property* reloc_property)
4592 // Select bits from X.
4595 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedW;
4596 if (static_cast<SignedW>(x) >= 0)
4598 immed = reloc_property->select_x_value(x);
4603 immed = reloc_property->select_x_value(~x);;
4607 // Update movnz instruction.
4608 update_movnz(view, immed, is_movz);
4610 // Do check overflow or alignment if needed.
4611 return (reloc_property->checkup_x_value(x)
4613 : This::STATUS_OVERFLOW);
4617 maybe_apply_stub(unsigned int,
4618 const The_relocate_info*,
4622 const Sized_symbol<size>*,
4623 const Symbol_value<size>*,
4624 const Sized_relobj_file<size, big_endian>*,
4627 }; // End of AArch64_relocate_functions
4630 // For a certain relocation type (usually jump/branch), test to see if the
4631 // destination needs a stub to fulfil. If so, re-route the destination of the
4632 // original instruction to the stub, note, at this time, the stub has already
4635 template<int size, bool big_endian>
4637 AArch64_relocate_functions<size, big_endian>::
4638 maybe_apply_stub(unsigned int r_type,
4639 const The_relocate_info* relinfo,
4640 const The_rela& rela,
4641 unsigned char* view,
4643 const Sized_symbol<size>* gsym,
4644 const Symbol_value<size>* psymval,
4645 const Sized_relobj_file<size, big_endian>* object,
4646 section_size_type current_group_size)
4648 if (parameters->options().relocatable())
4651 typename elfcpp::Elf_types<size>::Elf_Swxword addend = rela.get_r_addend();
4652 Address branch_target = psymval->value(object, 0) + addend;
4653 The_reloc_stub_type stub_type = The_reloc_stub::
4654 stub_type_for_reloc(r_type, address, branch_target);
4655 if (stub_type == The_reloc_stub::ST_NONE)
4658 const The_aarch64_relobj* aarch64_relobj =
4659 static_cast<const The_aarch64_relobj*>(object);
4660 The_stub_table* stub_table = aarch64_relobj->stub_table(relinfo->data_shndx);
4661 gold_assert(stub_table != NULL);
4663 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4664 typename The_reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
4665 The_reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
4666 gold_assert(stub != NULL);
4668 Address new_branch_target = stub_table->address() + stub->offset();
4669 typename elfcpp::Swap<size, big_endian>::Valtype branch_offset =
4670 new_branch_target - address;
4671 const AArch64_reloc_property* arp =
4672 aarch64_reloc_property_table->get_reloc_property(r_type);
4673 gold_assert(arp != NULL);
4674 typename This::Status status = This::template
4675 rela_general<32>(view, branch_offset, 0, arp);
4676 if (status != This::STATUS_OKAY)
4677 gold_error(_("Stub is too far away, try a smaller value "
4678 "for '--stub-group-size'. The current value is 0x%lx."),
4679 static_cast<unsigned long>(current_group_size));
4684 // Group input sections for stub generation.
4686 // We group input sections in an output section so that the total size,
4687 // including any padding space due to alignment is smaller than GROUP_SIZE
4688 // unless the only input section in group is bigger than GROUP_SIZE already.
4689 // Then an ARM stub table is created to follow the last input section
4690 // in group. For each group an ARM stub table is created an is placed
4691 // after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
4692 // extend the group after the stub table.
4694 template<int size, bool big_endian>
4696 Target_aarch64<size, big_endian>::group_sections(
4698 section_size_type group_size,
4699 bool stubs_always_after_branch,
4702 // Group input sections and insert stub table
4703 Layout::Section_list section_list;
4704 layout->get_executable_sections(§ion_list);
4705 for (Layout::Section_list::const_iterator p = section_list.begin();
4706 p != section_list.end();
4709 AArch64_output_section<size, big_endian>* output_section =
4710 static_cast<AArch64_output_section<size, big_endian>*>(*p);
4711 output_section->group_sections(group_size, stubs_always_after_branch,
4717 // Find the AArch64_input_section object corresponding to the SHNDX-th input
4718 // section of RELOBJ.
4720 template<int size, bool big_endian>
4721 AArch64_input_section<size, big_endian>*
4722 Target_aarch64<size, big_endian>::find_aarch64_input_section(
4723 Relobj* relobj, unsigned int shndx) const
4725 Section_id sid(relobj, shndx);
4726 typename AArch64_input_section_map::const_iterator p =
4727 this->aarch64_input_section_map_.find(sid);
4728 return (p != this->aarch64_input_section_map_.end()) ? p->second : NULL;
4732 // Make a new AArch64_input_section object.
4734 template<int size, bool big_endian>
4735 AArch64_input_section<size, big_endian>*
4736 Target_aarch64<size, big_endian>::new_aarch64_input_section(
4737 Relobj* relobj, unsigned int shndx)
4739 Section_id sid(relobj, shndx);
4741 AArch64_input_section<size, big_endian>* input_section =
4742 new AArch64_input_section<size, big_endian>(relobj, shndx);
4743 input_section->init();
4745 // Register new AArch64_input_section in map for look-up.
4746 std::pair<typename AArch64_input_section_map::iterator,bool> ins =
4747 this->aarch64_input_section_map_.insert(
4748 std::make_pair(sid, input_section));
4750 // Make sure that it we have not created another AArch64_input_section
4751 // for this input section already.
4752 gold_assert(ins.second);
4754 return input_section;
4758 // Relaxation hook. This is where we do stub generation.
4760 template<int size, bool big_endian>
4762 Target_aarch64<size, big_endian>::do_relax(
4764 const Input_objects* input_objects,
4765 Symbol_table* symtab,
4769 gold_assert(!parameters->options().relocatable());
4772 // We don't handle negative stub_group_size right now.
4773 this->stub_group_size_ = abs(parameters->options().stub_group_size());
4774 if (this->stub_group_size_ == 1)
4776 // Leave room for 4096 4-byte stub entries. If we exceed that, then we
4777 // will fail to link. The user will have to relink with an explicit
4778 // group size option.
4779 this->stub_group_size_ = The_reloc_stub::MAX_BRANCH_OFFSET -
4782 group_sections(layout, this->stub_group_size_, true, task);
4786 // If this is not the first pass, addresses and file offsets have
4787 // been reset at this point, set them here.
4788 for (Stub_table_iterator sp = this->stub_tables_.begin();
4789 sp != this->stub_tables_.end(); ++sp)
4791 The_stub_table* stt = *sp;
4792 The_aarch64_input_section* owner = stt->owner();
4793 off_t off = align_address(owner->original_size(),
4795 stt->set_address_and_file_offset(owner->address() + off,
4796 owner->offset() + off);
4800 // Scan relocs for relocation stubs
4801 for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
4802 op != input_objects->relobj_end();
4805 The_aarch64_relobj* aarch64_relobj =
4806 static_cast<The_aarch64_relobj*>(*op);
4807 // Lock the object so we can read from it. This is only called
4808 // single-threaded from Layout::finalize, so it is OK to lock.
4809 Task_lock_obj<Object> tl(task, aarch64_relobj);
4810 aarch64_relobj->scan_sections_for_stubs(this, symtab, layout);
4813 bool any_stub_table_changed = false;
4814 for (Stub_table_iterator siter = this->stub_tables_.begin();
4815 siter != this->stub_tables_.end() && !any_stub_table_changed; ++siter)
4817 The_stub_table* stub_table = *siter;
4818 if (stub_table->update_data_size_changed_p())
4820 The_aarch64_input_section* owner = stub_table->owner();
4821 uint64_t address = owner->address();
4822 off_t offset = owner->offset();
4823 owner->reset_address_and_file_offset();
4824 owner->set_address_and_file_offset(address, offset);
4826 any_stub_table_changed = true;
4830 // Do not continue relaxation.
4831 bool continue_relaxation = any_stub_table_changed;
4832 if (!continue_relaxation)
4833 for (Stub_table_iterator sp = this->stub_tables_.begin();
4834 (sp != this->stub_tables_.end());
4836 (*sp)->finalize_stubs();
4838 return continue_relaxation;
4842 // Make a new Stub_table.
4844 template<int size, bool big_endian>
4845 Stub_table<size, big_endian>*
4846 Target_aarch64<size, big_endian>::new_stub_table(
4847 AArch64_input_section<size, big_endian>* owner)
4849 Stub_table<size, big_endian>* stub_table =
4850 new Stub_table<size, big_endian>(owner);
4851 stub_table->set_address(align_address(
4852 owner->address() + owner->data_size(), 8));
4853 stub_table->set_file_offset(owner->offset() + owner->data_size());
4854 stub_table->finalize_data_size();
4856 this->stub_tables_.push_back(stub_table);
4862 template<int size, bool big_endian>
4864 Target_aarch64<size, big_endian>::do_reloc_addend(
4865 void* arg, unsigned int r_type, uint64_t) const
4867 gold_assert(r_type == elfcpp::R_AARCH64_TLSDESC);
4868 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
4869 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
4870 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
4871 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
4872 gold_assert(psymval->is_tls_symbol());
4873 // The value of a TLS symbol is the offset in the TLS segment.
4874 return psymval->value(ti.object, 0);
4877 // Return the number of entries in the PLT.
4879 template<int size, bool big_endian>
4881 Target_aarch64<size, big_endian>::plt_entry_count() const
4883 if (this->plt_ == NULL)
4885 return this->plt_->entry_count();
4888 // Return the offset of the first non-reserved PLT entry.
4890 template<int size, bool big_endian>
4892 Target_aarch64<size, big_endian>::first_plt_entry_offset() const
4894 return this->plt_->first_plt_entry_offset();
4897 // Return the size of each PLT entry.
4899 template<int size, bool big_endian>
4901 Target_aarch64<size, big_endian>::plt_entry_size() const
4903 return this->plt_->get_plt_entry_size();
4906 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
4908 template<int size, bool big_endian>
4910 Target_aarch64<size, big_endian>::define_tls_base_symbol(
4911 Symbol_table* symtab, Layout* layout)
4913 if (this->tls_base_symbol_defined_)
4916 Output_segment* tls_segment = layout->tls_segment();
4917 if (tls_segment != NULL)
4919 // _TLS_MODULE_BASE_ always points to the beginning of tls segment.
4920 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
4921 Symbol_table::PREDEFINED,
4925 elfcpp::STV_HIDDEN, 0,
4926 Symbol::SEGMENT_START,
4929 this->tls_base_symbol_defined_ = true;
4932 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
4934 template<int size, bool big_endian>
4936 Target_aarch64<size, big_endian>::reserve_tlsdesc_entries(
4937 Symbol_table* symtab, Layout* layout)
4939 if (this->plt_ == NULL)
4940 this->make_plt_section(symtab, layout);
4942 if (!this->plt_->has_tlsdesc_entry())
4944 // Allocate the TLSDESC_GOT entry.
4945 Output_data_got_aarch64<size, big_endian>* got =
4946 this->got_section(symtab, layout);
4947 unsigned int got_offset = got->add_constant(0);
4949 // Allocate the TLSDESC_PLT entry.
4950 this->plt_->reserve_tlsdesc_entry(got_offset);
4954 // Create a GOT entry for the TLS module index.
4956 template<int size, bool big_endian>
4958 Target_aarch64<size, big_endian>::got_mod_index_entry(
4959 Symbol_table* symtab, Layout* layout,
4960 Sized_relobj_file<size, big_endian>* object)
4962 if (this->got_mod_index_offset_ == -1U)
4964 gold_assert(symtab != NULL && layout != NULL && object != NULL);
4965 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
4966 Output_data_got_aarch64<size, big_endian>* got =
4967 this->got_section(symtab, layout);
4968 unsigned int got_offset = got->add_constant(0);
4969 rela_dyn->add_local(object, 0, elfcpp::R_AARCH64_TLS_DTPMOD64, got,
4971 got->add_constant(0);
4972 this->got_mod_index_offset_ = got_offset;
4974 return this->got_mod_index_offset_;
4977 // Optimize the TLS relocation type based on what we know about the
4978 // symbol. IS_FINAL is true if the final address of this symbol is
4979 // known at link time.
4981 template<int size, bool big_endian>
4982 tls::Tls_optimization
4983 Target_aarch64<size, big_endian>::optimize_tls_reloc(bool is_final,
4986 // If we are generating a shared library, then we can't do anything
4988 if (parameters->options().shared())
4989 return tls::TLSOPT_NONE;
4993 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
4994 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
4995 case elfcpp::R_AARCH64_TLSDESC_LD_PREL19:
4996 case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21:
4997 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
4998 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
4999 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5000 case elfcpp::R_AARCH64_TLSDESC_OFF_G1:
5001 case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC:
5002 case elfcpp::R_AARCH64_TLSDESC_LDR:
5003 case elfcpp::R_AARCH64_TLSDESC_ADD:
5004 case elfcpp::R_AARCH64_TLSDESC_CALL:
5005 // These are General-Dynamic which permits fully general TLS
5006 // access. Since we know that we are generating an executable,
5007 // we can convert this to Initial-Exec. If we also know that
5008 // this is a local symbol, we can further switch to Local-Exec.
5010 return tls::TLSOPT_TO_LE;
5011 return tls::TLSOPT_TO_IE;
5013 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5014 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5015 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5016 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5017 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5018 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5019 // These are Local-Dynamic, which refer to local symbols in the
5020 // dynamic TLS block. Since we know that we generating an
5021 // executable, we can switch to Local-Exec.
5022 return tls::TLSOPT_TO_LE;
5024 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1:
5025 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC:
5026 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5027 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5028 case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19:
5029 // These are Initial-Exec relocs which get the thread offset
5030 // from the GOT. If we know that we are linking against the
5031 // local symbol, we can switch to Local-Exec, which links the
5032 // thread offset into the instruction.
5034 return tls::TLSOPT_TO_LE;
5035 return tls::TLSOPT_NONE;
5037 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5038 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5039 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5040 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5041 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5042 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5043 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5044 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5045 // When we already have Local-Exec, there is nothing further we
5047 return tls::TLSOPT_NONE;
5054 // Returns true if this relocation type could be that of a function pointer.
5056 template<int size, bool big_endian>
5058 Target_aarch64<size, big_endian>::Scan::possible_function_pointer_reloc(
5059 unsigned int r_type)
5063 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
5064 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
5065 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
5066 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5067 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5075 // For safe ICF, scan a relocation for a local symbol to check if it
5076 // corresponds to a function pointer being taken. In that case mark
5077 // the function whose pointer was taken as not foldable.
5079 template<int size, bool big_endian>
5081 Target_aarch64<size, big_endian>::Scan::local_reloc_may_be_function_pointer(
5084 Target_aarch64<size, big_endian>* ,
5085 Sized_relobj_file<size, big_endian>* ,
5088 const elfcpp::Rela<size, big_endian>& ,
5089 unsigned int r_type,
5090 const elfcpp::Sym<size, big_endian>&)
5092 // When building a shared library, do not fold any local symbols.
5093 return (parameters->options().shared()
5094 || possible_function_pointer_reloc(r_type));
5097 // For safe ICF, scan a relocation for a global symbol to check if it
5098 // corresponds to a function pointer being taken. In that case mark
5099 // the function whose pointer was taken as not foldable.
5101 template<int size, bool big_endian>
5103 Target_aarch64<size, big_endian>::Scan::global_reloc_may_be_function_pointer(
5106 Target_aarch64<size, big_endian>* ,
5107 Sized_relobj_file<size, big_endian>* ,
5110 const elfcpp::Rela<size, big_endian>& ,
5111 unsigned int r_type,
5114 // When building a shared library, do not fold symbols whose visibility
5115 // is hidden, internal or protected.
5116 return ((parameters->options().shared()
5117 && (gsym->visibility() == elfcpp::STV_INTERNAL
5118 || gsym->visibility() == elfcpp::STV_PROTECTED
5119 || gsym->visibility() == elfcpp::STV_HIDDEN))
5120 || possible_function_pointer_reloc(r_type));
5123 // Report an unsupported relocation against a local symbol.
5125 template<int size, bool big_endian>
5127 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_local(
5128 Sized_relobj_file<size, big_endian>* object,
5129 unsigned int r_type)
5131 gold_error(_("%s: unsupported reloc %u against local symbol"),
5132 object->name().c_str(), r_type);
5135 // We are about to emit a dynamic relocation of type R_TYPE. If the
5136 // dynamic linker does not support it, issue an error.
5138 template<int size, bool big_endian>
5140 Target_aarch64<size, big_endian>::Scan::check_non_pic(Relobj* object,
5141 unsigned int r_type)
5143 gold_assert(r_type != elfcpp::R_AARCH64_NONE);
5147 // These are the relocation types supported by glibc for AARCH64.
5148 case elfcpp::R_AARCH64_NONE:
5149 case elfcpp::R_AARCH64_COPY:
5150 case elfcpp::R_AARCH64_GLOB_DAT:
5151 case elfcpp::R_AARCH64_JUMP_SLOT:
5152 case elfcpp::R_AARCH64_RELATIVE:
5153 case elfcpp::R_AARCH64_TLS_DTPREL64:
5154 case elfcpp::R_AARCH64_TLS_DTPMOD64:
5155 case elfcpp::R_AARCH64_TLS_TPREL64:
5156 case elfcpp::R_AARCH64_TLSDESC:
5157 case elfcpp::R_AARCH64_IRELATIVE:
5158 case elfcpp::R_AARCH64_ABS32:
5159 case elfcpp::R_AARCH64_ABS64:
5166 // This prevents us from issuing more than one error per reloc
5167 // section. But we can still wind up issuing more than one
5168 // error per object file.
5169 if (this->issued_non_pic_error_)
5171 gold_assert(parameters->options().output_is_position_independent());
5172 object->error(_("requires unsupported dynamic reloc; "
5173 "recompile with -fPIC"));
5174 this->issued_non_pic_error_ = true;
5178 // Return whether we need to make a PLT entry for a relocation of the
5179 // given type against a STT_GNU_IFUNC symbol.
5181 template<int size, bool big_endian>
5183 Target_aarch64<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
5184 Sized_relobj_file<size, big_endian>* object,
5185 unsigned int r_type)
5187 const AArch64_reloc_property* arp =
5188 aarch64_reloc_property_table->get_reloc_property(r_type);
5189 gold_assert(arp != NULL);
5191 int flags = arp->reference_flags();
5192 if (flags & Symbol::TLS_REF)
5194 gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
5195 object->name().c_str(), arp->name().c_str());
5201 // Scan a relocation for a local symbol.
5203 template<int size, bool big_endian>
5205 Target_aarch64<size, big_endian>::Scan::local(
5206 Symbol_table* symtab,
5208 Target_aarch64<size, big_endian>* target,
5209 Sized_relobj_file<size, big_endian>* object,
5210 unsigned int data_shndx,
5211 Output_section* output_section,
5212 const elfcpp::Rela<size, big_endian>& rela,
5213 unsigned int r_type,
5214 const elfcpp::Sym<size, big_endian>& lsym,
5220 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
5222 Output_data_got_aarch64<size, big_endian>* got =
5223 target->got_section(symtab, layout);
5224 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5226 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5227 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
5228 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
5229 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
5233 case elfcpp::R_AARCH64_ABS32:
5234 case elfcpp::R_AARCH64_ABS16:
5235 if (parameters->options().output_is_position_independent())
5237 gold_error(_("%s: unsupported reloc %u in pos independent link."),
5238 object->name().c_str(), r_type);
5242 case elfcpp::R_AARCH64_ABS64:
5243 // If building a shared library or pie, we need to mark this as a dynmic
5244 // reloction, so that the dynamic loader can relocate it.
5245 if (parameters->options().output_is_position_independent())
5247 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5248 rela_dyn->add_local_relative(object, r_sym,
5249 elfcpp::R_AARCH64_RELATIVE,
5252 rela.get_r_offset(),
5253 rela.get_r_addend(),
5258 case elfcpp::R_AARCH64_PREL64:
5259 case elfcpp::R_AARCH64_PREL32:
5260 case elfcpp::R_AARCH64_PREL16:
5263 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
5264 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
5265 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
5266 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
5267 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
5268 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
5269 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
5270 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
5271 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
5272 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
5275 // Control flow, pc-relative. We don't need to do anything for a relative
5276 // addressing relocation against a local symbol if it does not reference
5278 case elfcpp::R_AARCH64_TSTBR14:
5279 case elfcpp::R_AARCH64_CONDBR19:
5280 case elfcpp::R_AARCH64_JUMP26:
5281 case elfcpp::R_AARCH64_CALL26:
5284 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5285 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
5287 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5288 optimize_tls_reloc(!parameters->options().shared(), r_type);
5289 if (tlsopt == tls::TLSOPT_TO_LE)
5292 layout->set_has_static_tls();
5293 // Create a GOT entry for the tp-relative offset.
5294 if (!parameters->doing_static_link())
5296 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
5297 target->rela_dyn_section(layout),
5298 elfcpp::R_AARCH64_TLS_TPREL64);
5300 else if (!object->local_has_got_offset(r_sym,
5301 GOT_TYPE_TLS_OFFSET))
5303 got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
5304 unsigned int got_offset =
5305 object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
5306 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5307 gold_assert(addend == 0);
5308 got->add_static_reloc(got_offset, elfcpp::R_AARCH64_TLS_TPREL64,
5314 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5315 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
5317 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5318 optimize_tls_reloc(!parameters->options().shared(), r_type);
5319 if (tlsopt == tls::TLSOPT_TO_LE)
5321 layout->set_has_static_tls();
5324 gold_assert(tlsopt == tls::TLSOPT_NONE);
5326 got->add_local_pair_with_rel(object,r_sym, data_shndx,
5328 target->rela_dyn_section(layout),
5329 elfcpp::R_AARCH64_TLS_DTPMOD64);
5333 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5334 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5335 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5336 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5337 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5338 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5339 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5340 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
5342 layout->set_has_static_tls();
5343 bool output_is_shared = parameters->options().shared();
5344 if (output_is_shared)
5345 gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
5346 object->name().c_str(), r_type);
5350 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5351 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
5353 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5354 optimize_tls_reloc(!parameters->options().shared(), r_type);
5355 if (tlsopt == tls::TLSOPT_NONE)
5357 // Create a GOT entry for the module index.
5358 target->got_mod_index_entry(symtab, layout, object);
5360 else if (tlsopt != tls::TLSOPT_TO_LE)
5361 unsupported_reloc_local(object, r_type);
5365 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5366 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5367 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5368 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
5371 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5372 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5373 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
5375 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5376 optimize_tls_reloc(!parameters->options().shared(), r_type);
5377 target->define_tls_base_symbol(symtab, layout);
5378 if (tlsopt == tls::TLSOPT_NONE)
5380 // Create reserved PLT and GOT entries for the resolver.
5381 target->reserve_tlsdesc_entries(symtab, layout);
5383 // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
5384 // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
5385 // entry needs to be in an area in .got.plt, not .got. Call
5386 // got_section to make sure the section has been created.
5387 target->got_section(symtab, layout);
5388 Output_data_got<size, big_endian>* got =
5389 target->got_tlsdesc_section();
5390 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5391 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
5393 unsigned int got_offset = got->add_constant(0);
5394 got->add_constant(0);
5395 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
5397 Reloc_section* rt = target->rela_tlsdesc_section(layout);
5398 // We store the arguments we need in a vector, and use
5399 // the index into the vector as the parameter to pass
5400 // to the target specific routines.
5401 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
5402 void* arg = reinterpret_cast<void*>(intarg);
5403 rt->add_target_specific(elfcpp::R_AARCH64_TLSDESC, arg,
5404 got, got_offset, 0);
5407 else if (tlsopt != tls::TLSOPT_TO_LE)
5408 unsupported_reloc_local(object, r_type);
5412 case elfcpp::R_AARCH64_TLSDESC_CALL:
5416 unsupported_reloc_local(object, r_type);
5421 // Report an unsupported relocation against a global symbol.
5423 template<int size, bool big_endian>
5425 Target_aarch64<size, big_endian>::Scan::unsupported_reloc_global(
5426 Sized_relobj_file<size, big_endian>* object,
5427 unsigned int r_type,
5430 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5431 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5434 template<int size, bool big_endian>
5436 Target_aarch64<size, big_endian>::Scan::global(
5437 Symbol_table* symtab,
5439 Target_aarch64<size, big_endian>* target,
5440 Sized_relobj_file<size, big_endian> * object,
5441 unsigned int data_shndx,
5442 Output_section* output_section,
5443 const elfcpp::Rela<size, big_endian>& rela,
5444 unsigned int r_type,
5447 // A STT_GNU_IFUNC symbol may require a PLT entry.
5448 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5449 && this->reloc_needs_plt_for_ifunc(object, r_type))
5450 target->make_plt_entry(symtab, layout, gsym);
5452 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
5454 const AArch64_reloc_property* arp =
5455 aarch64_reloc_property_table->get_reloc_property(r_type);
5456 gold_assert(arp != NULL);
5460 case elfcpp::R_AARCH64_ABS16:
5461 case elfcpp::R_AARCH64_ABS32:
5462 case elfcpp::R_AARCH64_ABS64:
5464 // Make a PLT entry if necessary.
5465 if (gsym->needs_plt_entry())
5467 target->make_plt_entry(symtab, layout, gsym);
5468 // Since this is not a PC-relative relocation, we may be
5469 // taking the address of a function. In that case we need to
5470 // set the entry in the dynamic symbol table to the address of
5472 if (gsym->is_from_dynobj() && !parameters->options().shared())
5473 gsym->set_needs_dynsym_value();
5475 // Make a dynamic relocation if necessary.
5476 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
5478 if (!parameters->options().output_is_position_independent()
5479 && gsym->may_need_copy_reloc())
5481 target->copy_reloc(symtab, layout, object,
5482 data_shndx, output_section, gsym, rela);
5484 else if (r_type == elfcpp::R_AARCH64_ABS64
5485 && gsym->type() == elfcpp::STT_GNU_IFUNC
5486 && gsym->can_use_relative_reloc(false)
5487 && !gsym->is_from_dynobj()
5488 && !gsym->is_undefined()
5489 && !gsym->is_preemptible())
5491 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
5492 // symbol. This makes a function address in a PIE executable
5493 // match the address in a shared library that it links against.
5494 Reloc_section* rela_dyn =
5495 target->rela_irelative_section(layout);
5496 unsigned int r_type = elfcpp::R_AARCH64_IRELATIVE;
5497 rela_dyn->add_symbolless_global_addend(gsym, r_type,
5498 output_section, object,
5500 rela.get_r_offset(),
5501 rela.get_r_addend());
5503 else if (r_type == elfcpp::R_AARCH64_ABS64
5504 && gsym->can_use_relative_reloc(false))
5506 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5507 rela_dyn->add_global_relative(gsym,
5508 elfcpp::R_AARCH64_RELATIVE,
5512 rela.get_r_offset(),
5513 rela.get_r_addend(),
5518 check_non_pic(object, r_type);
5519 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*
5520 rela_dyn = target->rela_dyn_section(layout);
5521 rela_dyn->add_global(
5522 gsym, r_type, output_section, object,
5523 data_shndx, rela.get_r_offset(),rela.get_r_addend());
5529 case elfcpp::R_AARCH64_PREL16:
5530 case elfcpp::R_AARCH64_PREL32:
5531 case elfcpp::R_AARCH64_PREL64:
5532 // This is used to fill the GOT absolute address.
5533 if (gsym->needs_plt_entry())
5535 target->make_plt_entry(symtab, layout, gsym);
5539 case elfcpp::R_AARCH64_LD_PREL_LO19: // 273
5540 case elfcpp::R_AARCH64_ADR_PREL_LO21: // 274
5541 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21: // 275
5542 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC: // 276
5543 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC: // 277
5544 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC: // 278
5545 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC: // 284
5546 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC: // 285
5547 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC: // 286
5548 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC: // 299
5550 if (gsym->needs_plt_entry())
5551 target->make_plt_entry(symtab, layout, gsym);
5552 // Make a dynamic relocation if necessary.
5553 if (gsym->needs_dynamic_reloc(arp->reference_flags()))
5555 if (parameters->options().output_is_executable()
5556 && gsym->may_need_copy_reloc())
5558 target->copy_reloc(symtab, layout, object,
5559 data_shndx, output_section, gsym, rela);
5565 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
5566 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
5568 // This pair of relocations is used to access a specific GOT entry.
5569 // Note a GOT entry is an *address* to a symbol.
5570 // The symbol requires a GOT entry
5571 Output_data_got_aarch64<size, big_endian>* got =
5572 target->got_section(symtab, layout);
5573 if (gsym->final_value_is_known())
5575 // For a STT_GNU_IFUNC symbol we want the PLT address.
5576 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
5577 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5579 got->add_global(gsym, GOT_TYPE_STANDARD);
5583 // If this symbol is not fully resolved, we need to add a dynamic
5584 // relocation for it.
5585 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5587 // Use a GLOB_DAT rather than a RELATIVE reloc if:
5589 // 1) The symbol may be defined in some other module.
5590 // 2) We are building a shared library and this is a protected
5591 // symbol; using GLOB_DAT means that the dynamic linker can use
5592 // the address of the PLT in the main executable when appropriate
5593 // so that function address comparisons work.
5594 // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
5595 // again so that function address comparisons work.
5596 if (gsym->is_from_dynobj()
5597 || gsym->is_undefined()
5598 || gsym->is_preemptible()
5599 || (gsym->visibility() == elfcpp::STV_PROTECTED
5600 && parameters->options().shared())
5601 || (gsym->type() == elfcpp::STT_GNU_IFUNC
5602 && parameters->options().output_is_position_independent()))
5603 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
5604 rela_dyn, elfcpp::R_AARCH64_GLOB_DAT);
5607 // For a STT_GNU_IFUNC symbol we want to write the PLT
5608 // offset into the GOT, so that function pointer
5609 // comparisons work correctly.
5611 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
5612 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
5615 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5616 // Tell the dynamic linker to use the PLT address
5617 // when resolving relocations.
5618 if (gsym->is_from_dynobj()
5619 && !parameters->options().shared())
5620 gsym->set_needs_dynsym_value();
5624 rela_dyn->add_global_relative(
5625 gsym, elfcpp::R_AARCH64_RELATIVE,
5627 gsym->got_offset(GOT_TYPE_STANDARD),
5636 case elfcpp::R_AARCH64_TSTBR14:
5637 case elfcpp::R_AARCH64_CONDBR19:
5638 case elfcpp::R_AARCH64_JUMP26:
5639 case elfcpp::R_AARCH64_CALL26:
5641 if (gsym->final_value_is_known())
5644 if (gsym->is_defined() &&
5645 !gsym->is_from_dynobj() &&
5646 !gsym->is_preemptible())
5649 // Make plt entry for function call.
5650 target->make_plt_entry(symtab, layout, gsym);
5654 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
5655 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // General dynamic
5657 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5658 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
5659 if (tlsopt == tls::TLSOPT_TO_LE)
5661 layout->set_has_static_tls();
5664 gold_assert(tlsopt == tls::TLSOPT_NONE);
5667 Output_data_got_aarch64<size, big_endian>* got =
5668 target->got_section(symtab, layout);
5669 // Create 2 consecutive entries for module index and offset.
5670 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
5671 target->rela_dyn_section(layout),
5672 elfcpp::R_AARCH64_TLS_DTPMOD64,
5673 elfcpp::R_AARCH64_TLS_DTPREL64);
5677 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
5678 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local dynamic
5680 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5681 optimize_tls_reloc(!parameters->options().shared(), r_type);
5682 if (tlsopt == tls::TLSOPT_NONE)
5684 // Create a GOT entry for the module index.
5685 target->got_mod_index_entry(symtab, layout, object);
5687 else if (tlsopt != tls::TLSOPT_TO_LE)
5688 unsupported_reloc_local(object, r_type);
5692 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
5693 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
5694 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
5695 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local dynamic
5698 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
5699 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial executable
5701 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5702 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
5703 if (tlsopt == tls::TLSOPT_TO_LE)
5706 layout->set_has_static_tls();
5707 // Create a GOT entry for the tp-relative offset.
5708 Output_data_got_aarch64<size, big_endian>* got
5709 = target->got_section(symtab, layout);
5710 if (!parameters->doing_static_link())
5712 got->add_global_with_rel(
5713 gsym, GOT_TYPE_TLS_OFFSET,
5714 target->rela_dyn_section(layout),
5715 elfcpp::R_AARCH64_TLS_TPREL64);
5717 if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
5719 got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
5720 unsigned int got_offset =
5721 gsym->got_offset(GOT_TYPE_TLS_OFFSET);
5722 const elfcpp::Elf_Xword addend = rela.get_r_addend();
5723 gold_assert(addend == 0);
5724 got->add_static_reloc(got_offset,
5725 elfcpp::R_AARCH64_TLS_TPREL64, gsym);
5730 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
5731 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
5732 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
5733 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
5734 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
5735 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
5736 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
5737 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: // Local executable
5738 layout->set_has_static_tls();
5739 if (parameters->options().shared())
5740 gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
5741 object->name().c_str(), r_type);
5744 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
5745 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
5746 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12: // TLS descriptor
5748 target->define_tls_base_symbol(symtab, layout);
5749 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
5750 optimize_tls_reloc(gsym->final_value_is_known(), r_type);
5751 if (tlsopt == tls::TLSOPT_NONE)
5753 // Create reserved PLT and GOT entries for the resolver.
5754 target->reserve_tlsdesc_entries(symtab, layout);
5756 // Create a double GOT entry with an R_AARCH64_TLSDESC
5757 // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
5758 // entry needs to be in an area in .got.plt, not .got. Call
5759 // got_section to make sure the section has been created.
5760 target->got_section(symtab, layout);
5761 Output_data_got<size, big_endian>* got =
5762 target->got_tlsdesc_section();
5763 Reloc_section* rt = target->rela_tlsdesc_section(layout);
5764 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
5765 elfcpp::R_AARCH64_TLSDESC, 0);
5767 else if (tlsopt == tls::TLSOPT_TO_IE)
5769 // Create a GOT entry for the tp-relative offset.
5770 Output_data_got<size, big_endian>* got
5771 = target->got_section(symtab, layout);
5772 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
5773 target->rela_dyn_section(layout),
5774 elfcpp::R_AARCH64_TLS_TPREL64);
5776 else if (tlsopt != tls::TLSOPT_TO_LE)
5777 unsupported_reloc_global(object, r_type, gsym);
5781 case elfcpp::R_AARCH64_TLSDESC_CALL:
5785 gold_error(_("%s: unsupported reloc type in global scan"),
5786 aarch64_reloc_property_table->
5787 reloc_name_in_error_message(r_type).c_str());
5790 } // End of Scan::global
5793 // Create the PLT section.
5794 template<int size, bool big_endian>
5796 Target_aarch64<size, big_endian>::make_plt_section(
5797 Symbol_table* symtab, Layout* layout)
5799 if (this->plt_ == NULL)
5801 // Create the GOT section first.
5802 this->got_section(symtab, layout);
5804 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
5805 this->got_irelative_);
5807 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
5809 | elfcpp::SHF_EXECINSTR),
5810 this->plt_, ORDER_PLT, false);
5812 // Make the sh_info field of .rela.plt point to .plt.
5813 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
5814 rela_plt_os->set_info_section(this->plt_->output_section());
5818 // Return the section for TLSDESC relocations.
5820 template<int size, bool big_endian>
5821 typename Target_aarch64<size, big_endian>::Reloc_section*
5822 Target_aarch64<size, big_endian>::rela_tlsdesc_section(Layout* layout) const
5824 return this->plt_section()->rela_tlsdesc(layout);
5827 // Create a PLT entry for a global symbol.
5829 template<int size, bool big_endian>
5831 Target_aarch64<size, big_endian>::make_plt_entry(
5832 Symbol_table* symtab,
5836 if (gsym->has_plt_offset())
5839 if (this->plt_ == NULL)
5840 this->make_plt_section(symtab, layout);
5842 this->plt_->add_entry(symtab, layout, gsym);
5845 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5847 template<int size, bool big_endian>
5849 Target_aarch64<size, big_endian>::make_local_ifunc_plt_entry(
5850 Symbol_table* symtab, Layout* layout,
5851 Sized_relobj_file<size, big_endian>* relobj,
5852 unsigned int local_sym_index)
5854 if (relobj->local_has_plt_offset(local_sym_index))
5856 if (this->plt_ == NULL)
5857 this->make_plt_section(symtab, layout);
5858 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
5861 relobj->set_local_plt_offset(local_sym_index, plt_offset);
5864 template<int size, bool big_endian>
5866 Target_aarch64<size, big_endian>::gc_process_relocs(
5867 Symbol_table* symtab,
5869 Sized_relobj_file<size, big_endian>* object,
5870 unsigned int data_shndx,
5871 unsigned int sh_type,
5872 const unsigned char* prelocs,
5874 Output_section* output_section,
5875 bool needs_special_offset_handling,
5876 size_t local_symbol_count,
5877 const unsigned char* plocal_symbols)
5879 if (sh_type == elfcpp::SHT_REL)
5884 gold::gc_process_relocs<
5886 Target_aarch64<size, big_endian>,
5888 typename Target_aarch64<size, big_endian>::Scan,
5889 typename Target_aarch64<size, big_endian>::Relocatable_size_for_reloc>(
5898 needs_special_offset_handling,
5903 // Scan relocations for a section.
5905 template<int size, bool big_endian>
5907 Target_aarch64<size, big_endian>::scan_relocs(
5908 Symbol_table* symtab,
5910 Sized_relobj_file<size, big_endian>* object,
5911 unsigned int data_shndx,
5912 unsigned int sh_type,
5913 const unsigned char* prelocs,
5915 Output_section* output_section,
5916 bool needs_special_offset_handling,
5917 size_t local_symbol_count,
5918 const unsigned char* plocal_symbols)
5920 if (sh_type == elfcpp::SHT_REL)
5922 gold_error(_("%s: unsupported REL reloc section"),
5923 object->name().c_str());
5926 gold::scan_relocs<size, big_endian, Target_aarch64, elfcpp::SHT_RELA, Scan>(
5935 needs_special_offset_handling,
5940 // Return the value to use for a dynamic which requires special
5941 // treatment. This is how we support equality comparisons of function
5942 // pointers across shared library boundaries, as described in the
5943 // processor specific ABI supplement.
5945 template<int size, bool big_endian>
5947 Target_aarch64<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
5949 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5950 return this->plt_address_for_global(gsym);
5954 // Finalize the sections.
5956 template<int size, bool big_endian>
5958 Target_aarch64<size, big_endian>::do_finalize_sections(
5960 const Input_objects*,
5961 Symbol_table* symtab)
5963 const Reloc_section* rel_plt = (this->plt_ == NULL
5965 : this->plt_->rela_plt());
5966 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
5967 this->rela_dyn_, true, false);
5969 // Emit any relocs we saved in an attempt to avoid generating COPY
5971 if (this->copy_relocs_.any_saved_relocs())
5972 this->copy_relocs_.emit(this->rela_dyn_section(layout));
5974 // Fill in some more dynamic tags.
5975 Output_data_dynamic* const odyn = layout->dynamic_data();
5978 if (this->plt_ != NULL
5979 && this->plt_->output_section() != NULL
5980 && this->plt_ ->has_tlsdesc_entry())
5982 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
5983 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
5984 this->got_->finalize_data_size();
5985 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
5986 this->plt_, plt_offset);
5987 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
5988 this->got_, got_offset);
5992 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
5993 // the .got.plt section.
5994 Symbol* sym = this->global_offset_table_;
5997 uint64_t data_size = this->got_plt_->current_data_size();
5998 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
6000 // If the .got section is more than 0x8000 bytes, we add
6001 // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
6002 // bit relocations have a greater chance of working.
6003 if (data_size >= 0x8000)
6004 symtab->get_sized_symbol<size>(sym)->set_value(
6005 symtab->get_sized_symbol<size>(sym)->value() + 0x8000);
6008 if (parameters->doing_static_link()
6009 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
6011 // If linking statically, make sure that the __rela_iplt symbols
6012 // were defined if necessary, even if we didn't create a PLT.
6013 static const Define_symbol_in_segment syms[] =
6016 "__rela_iplt_start", // name
6017 elfcpp::PT_LOAD, // segment_type
6018 elfcpp::PF_W, // segment_flags_set
6019 elfcpp::PF(0), // segment_flags_clear
6022 elfcpp::STT_NOTYPE, // type
6023 elfcpp::STB_GLOBAL, // binding
6024 elfcpp::STV_HIDDEN, // visibility
6026 Symbol::SEGMENT_START, // offset_from_base
6030 "__rela_iplt_end", // name
6031 elfcpp::PT_LOAD, // segment_type
6032 elfcpp::PF_W, // segment_flags_set
6033 elfcpp::PF(0), // segment_flags_clear
6036 elfcpp::STT_NOTYPE, // type
6037 elfcpp::STB_GLOBAL, // binding
6038 elfcpp::STV_HIDDEN, // visibility
6040 Symbol::SEGMENT_START, // offset_from_base
6045 symtab->define_symbols(layout, 2, syms,
6046 layout->script_options()->saw_sections_clause());
6052 // Perform a relocation.
6054 template<int size, bool big_endian>
6056 Target_aarch64<size, big_endian>::Relocate::relocate(
6057 const Relocate_info<size, big_endian>* relinfo,
6058 Target_aarch64<size, big_endian>* target,
6061 const elfcpp::Rela<size, big_endian>& rela,
6062 unsigned int r_type,
6063 const Sized_symbol<size>* gsym,
6064 const Symbol_value<size>* psymval,
6065 unsigned char* view,
6066 typename elfcpp::Elf_types<size>::Elf_Addr address,
6067 section_size_type /* view_size */)
6072 typedef AArch64_relocate_functions<size, big_endian> Reloc;
6074 const AArch64_reloc_property* reloc_property =
6075 aarch64_reloc_property_table->get_reloc_property(r_type);
6077 if (reloc_property == NULL)
6079 std::string reloc_name =
6080 aarch64_reloc_property_table->reloc_name_in_error_message(r_type);
6081 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6082 _("cannot relocate %s in object file"),
6083 reloc_name.c_str());
6087 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
6089 // Pick the value to use for symbols defined in the PLT.
6090 Symbol_value<size> symval;
6092 && gsym->use_plt_offset(reloc_property->reference_flags()))
6094 symval.set_output_value(target->plt_address_for_global(gsym));
6097 else if (gsym == NULL && psymval->is_ifunc_symbol())
6099 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6100 if (object->local_has_plt_offset(r_sym))
6102 symval.set_output_value(target->plt_address_for_local(object, r_sym));
6107 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6109 // Get the GOT offset if needed.
6110 // For aarch64, the GOT pointer points to the start of the GOT section.
6111 bool have_got_offset = false;
6113 int got_base = (target->got_ != NULL
6114 ? (target->got_->current_data_size() >= 0x8000
6119 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0:
6120 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC:
6121 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1:
6122 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC:
6123 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2:
6124 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC:
6125 case elfcpp::R_AARCH64_MOVW_GOTOFF_G3:
6126 case elfcpp::R_AARCH64_GOTREL64:
6127 case elfcpp::R_AARCH64_GOTREL32:
6128 case elfcpp::R_AARCH64_GOT_LD_PREL19:
6129 case elfcpp::R_AARCH64_LD64_GOTOFF_LO15:
6130 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6131 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6132 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15:
6135 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6136 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - got_base;
6140 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6141 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6142 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
6145 have_got_offset = true;
6152 typename Reloc::Status reloc_status = Reloc::STATUS_OKAY;
6153 typename elfcpp::Elf_types<size>::Elf_Addr value;
6156 case elfcpp::R_AARCH64_NONE:
6159 case elfcpp::R_AARCH64_ABS64:
6160 reloc_status = Reloc::template rela_ua<64>(
6161 view, object, psymval, addend, reloc_property);
6164 case elfcpp::R_AARCH64_ABS32:
6165 reloc_status = Reloc::template rela_ua<32>(
6166 view, object, psymval, addend, reloc_property);
6169 case elfcpp::R_AARCH64_ABS16:
6170 reloc_status = Reloc::template rela_ua<16>(
6171 view, object, psymval, addend, reloc_property);
6174 case elfcpp::R_AARCH64_PREL64:
6175 reloc_status = Reloc::template pcrela_ua<64>(
6176 view, object, psymval, addend, address, reloc_property);
6179 case elfcpp::R_AARCH64_PREL32:
6180 reloc_status = Reloc::template pcrela_ua<32>(
6181 view, object, psymval, addend, address, reloc_property);
6184 case elfcpp::R_AARCH64_PREL16:
6185 reloc_status = Reloc::template pcrela_ua<16>(
6186 view, object, psymval, addend, address, reloc_property);
6189 case elfcpp::R_AARCH64_LD_PREL_LO19:
6190 reloc_status = Reloc::template pcrela_general<32>(
6191 view, object, psymval, addend, address, reloc_property);
6194 case elfcpp::R_AARCH64_ADR_PREL_LO21:
6195 reloc_status = Reloc::adr(view, object, psymval, addend,
6196 address, reloc_property);
6199 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC:
6200 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21:
6201 reloc_status = Reloc::adrp(view, object, psymval, addend, address,
6205 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC:
6206 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC:
6207 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC:
6208 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC:
6209 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC:
6210 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC:
6211 reloc_status = Reloc::template rela_general<32>(
6212 view, object, psymval, addend, reloc_property);
6215 case elfcpp::R_AARCH64_CALL26:
6216 if (this->skip_call_tls_get_addr_)
6218 // Double check that the TLSGD insn has been optimized away.
6219 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6220 Insntype insn = elfcpp::Swap<32, big_endian>::readval(
6221 reinterpret_cast<Insntype*>(view));
6222 gold_assert((insn & 0xff000000) == 0x91000000);
6224 reloc_status = Reloc::STATUS_OKAY;
6225 this->skip_call_tls_get_addr_ = false;
6226 // Return false to stop further processing this reloc.
6230 case elfcpp::R_AARCH64_JUMP26:
6231 if (Reloc::maybe_apply_stub(r_type, relinfo, rela, view, address,
6232 gsym, psymval, object,
6233 target->stub_group_size_))
6236 case elfcpp::R_AARCH64_TSTBR14:
6237 case elfcpp::R_AARCH64_CONDBR19:
6238 reloc_status = Reloc::template pcrela_general<32>(
6239 view, object, psymval, addend, address, reloc_property);
6242 case elfcpp::R_AARCH64_ADR_GOT_PAGE:
6243 gold_assert(have_got_offset);
6244 value = target->got_->address() + got_base + got_offset;
6245 reloc_status = Reloc::adrp(view, value + addend, address);
6248 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC:
6249 gold_assert(have_got_offset);
6250 value = target->got_->address() + got_base + got_offset;
6251 reloc_status = Reloc::template rela_general<32>(
6252 view, value, addend, reloc_property);
6255 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6256 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6257 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6258 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
6259 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6260 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6261 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6262 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
6263 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6264 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6265 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6266 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6267 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6268 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6269 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6270 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6271 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6272 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6273 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6274 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6275 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6276 case elfcpp::R_AARCH64_TLSDESC_CALL:
6277 reloc_status = relocate_tls(relinfo, target, relnum, rela, r_type,
6278 gsym, psymval, view, address);
6281 // These are dynamic relocations, which are unexpected when linking.
6282 case elfcpp::R_AARCH64_COPY:
6283 case elfcpp::R_AARCH64_GLOB_DAT:
6284 case elfcpp::R_AARCH64_JUMP_SLOT:
6285 case elfcpp::R_AARCH64_RELATIVE:
6286 case elfcpp::R_AARCH64_IRELATIVE:
6287 case elfcpp::R_AARCH64_TLS_DTPREL64:
6288 case elfcpp::R_AARCH64_TLS_DTPMOD64:
6289 case elfcpp::R_AARCH64_TLS_TPREL64:
6290 case elfcpp::R_AARCH64_TLSDESC:
6291 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6292 _("unexpected reloc %u in object file"),
6297 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6298 _("unsupported reloc %s"),
6299 reloc_property->name().c_str());
6303 // Report any errors.
6304 switch (reloc_status)
6306 case Reloc::STATUS_OKAY:
6308 case Reloc::STATUS_OVERFLOW:
6309 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6310 _("relocation overflow in %s"),
6311 reloc_property->name().c_str());
6313 case Reloc::STATUS_BAD_RELOC:
6314 gold_error_at_location(
6317 rela.get_r_offset(),
6318 _("unexpected opcode while processing relocation %s"),
6319 reloc_property->name().c_str());
6329 template<int size, bool big_endian>
6331 typename AArch64_relocate_functions<size, big_endian>::Status
6332 Target_aarch64<size, big_endian>::Relocate::relocate_tls(
6333 const Relocate_info<size, big_endian>* relinfo,
6334 Target_aarch64<size, big_endian>* target,
6336 const elfcpp::Rela<size, big_endian>& rela,
6337 unsigned int r_type, const Sized_symbol<size>* gsym,
6338 const Symbol_value<size>* psymval,
6339 unsigned char* view,
6340 typename elfcpp::Elf_types<size>::Elf_Addr address)
6342 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6343 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6345 Output_segment* tls_segment = relinfo->layout->tls_segment();
6346 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6347 const AArch64_reloc_property* reloc_property =
6348 aarch64_reloc_property_table->get_reloc_property(r_type);
6349 gold_assert(reloc_property != NULL);
6351 const bool is_final = (gsym == NULL
6352 ? !parameters->options().shared()
6353 : gsym->final_value_is_known());
6354 tls::Tls_optimization tlsopt = Target_aarch64<size, big_endian>::
6355 optimize_tls_reloc(is_final, r_type);
6357 Sized_relobj_file<size, big_endian>* object = relinfo->object;
6358 int tls_got_offset_type;
6361 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6362 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC: // Global-dynamic
6364 if (tlsopt == tls::TLSOPT_TO_LE)
6366 if (tls_segment == NULL)
6368 gold_assert(parameters->errors()->error_count() > 0
6369 || issue_undefined_symbol_error(gsym));
6370 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6372 return tls_gd_to_le(relinfo, target, rela, r_type, view,
6375 else if (tlsopt == tls::TLSOPT_NONE)
6377 tls_got_offset_type = GOT_TYPE_TLS_PAIR;
6378 // Firstly get the address for the got entry.
6379 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6382 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6383 got_entry_address = target->got_->address() +
6384 gsym->got_offset(tls_got_offset_type);
6388 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6390 object->local_has_got_offset(r_sym, tls_got_offset_type));
6391 got_entry_address = target->got_->address() +
6392 object->local_got_offset(r_sym, tls_got_offset_type);
6395 // Relocate the address into adrp/ld, adrp/add pair.
6398 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21:
6399 return aarch64_reloc_funcs::adrp(
6400 view, got_entry_address + addend, address);
6404 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC:
6405 return aarch64_reloc_funcs::template rela_general<32>(
6406 view, got_entry_address, addend, reloc_property);
6413 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6414 _("unsupported gd_to_ie relaxation on %u"),
6419 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6420 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC: // Local-dynamic
6422 if (tlsopt == tls::TLSOPT_TO_LE)
6424 if (tls_segment == NULL)
6426 gold_assert(parameters->errors()->error_count() > 0
6427 || issue_undefined_symbol_error(gsym));
6428 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6430 return this->tls_ld_to_le(relinfo, target, rela, r_type, view,
6434 gold_assert(tlsopt == tls::TLSOPT_NONE);
6435 // Relocate the field with the offset of the GOT entry for
6436 // the module index.
6437 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6438 got_entry_address = (target->got_mod_index_entry(NULL, NULL, NULL) +
6439 target->got_->address());
6443 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21:
6444 return aarch64_reloc_funcs::adrp(
6445 view, got_entry_address + addend, address);
6448 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC:
6449 return aarch64_reloc_funcs::template rela_general<32>(
6450 view, got_entry_address, addend, reloc_property);
6459 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6460 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6461 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6462 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC: // Other local-dynamic
6464 AArch64_address value = psymval->value(object, 0);
6465 if (tlsopt == tls::TLSOPT_TO_LE)
6467 if (tls_segment == NULL)
6469 gold_assert(parameters->errors()->error_count() > 0
6470 || issue_undefined_symbol_error(gsym));
6471 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6476 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1:
6477 return aarch64_reloc_funcs::movnz(view, value + addend,
6481 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC:
6482 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12:
6483 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC:
6484 return aarch64_reloc_funcs::template rela_general<32>(
6485 view, value, addend, reloc_property);
6491 // We should never reach here.
6495 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6496 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: // Initial-exec
6498 if (tlsopt == tls::TLSOPT_TO_LE)
6500 if (tls_segment == NULL)
6502 gold_assert(parameters->errors()->error_count() > 0
6503 || issue_undefined_symbol_error(gsym));
6504 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6506 return tls_ie_to_le(relinfo, target, rela, r_type, view,
6509 tls_got_offset_type = GOT_TYPE_TLS_OFFSET;
6511 // Firstly get the address for the got entry.
6512 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6515 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6516 got_entry_address = target->got_->address() +
6517 gsym->got_offset(tls_got_offset_type);
6521 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6523 object->local_has_got_offset(r_sym, tls_got_offset_type));
6524 got_entry_address = target->got_->address() +
6525 object->local_got_offset(r_sym, tls_got_offset_type);
6527 // Relocate the address into adrp/ld, adrp/add pair.
6530 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
6531 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
6534 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
6535 return aarch64_reloc_funcs::template rela_general<32>(
6536 view, got_entry_address, addend, reloc_property);
6541 // We shall never reach here.
6544 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6545 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6546 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC:
6547 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6548 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC:
6549 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12:
6550 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12:
6551 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
6553 gold_assert(tls_segment != NULL);
6554 AArch64_address value = psymval->value(object, 0);
6556 if (!parameters->options().shared())
6558 AArch64_address aligned_tcb_size =
6559 align_address(target->tcb_size(),
6560 tls_segment->maximum_alignment());
6561 value += aligned_tcb_size;
6564 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2:
6565 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1:
6566 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0:
6567 return aarch64_reloc_funcs::movnz(view, value + addend,
6570 return aarch64_reloc_funcs::template
6571 rela_general<32>(view,
6578 gold_error(_("%s: unsupported reloc %u "
6579 "in non-static TLSLE mode."),
6580 object->name().c_str(), r_type);
6584 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6585 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6586 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6587 case elfcpp::R_AARCH64_TLSDESC_CALL:
6589 if (tlsopt == tls::TLSOPT_TO_LE)
6591 if (tls_segment == NULL)
6593 gold_assert(parameters->errors()->error_count() > 0
6594 || issue_undefined_symbol_error(gsym));
6595 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6597 return tls_desc_gd_to_le(relinfo, target, rela, r_type,
6602 tls_got_offset_type = (tlsopt == tls::TLSOPT_TO_IE
6603 ? GOT_TYPE_TLS_OFFSET
6604 : GOT_TYPE_TLS_DESC);
6605 unsigned int got_tlsdesc_offset = 0;
6606 if (r_type != elfcpp::R_AARCH64_TLSDESC_CALL
6607 && tlsopt == tls::TLSOPT_NONE)
6609 // We created GOT entries in the .got.tlsdesc portion of the
6610 // .got.plt section, but the offset stored in the symbol is the
6611 // offset within .got.tlsdesc.
6612 got_tlsdesc_offset = (target->got_->data_size()
6613 + target->got_plt_section()->data_size());
6615 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address;
6618 gold_assert(gsym->has_got_offset(tls_got_offset_type));
6619 got_entry_address = target->got_->address()
6620 + got_tlsdesc_offset
6621 + gsym->got_offset(tls_got_offset_type);
6625 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6627 object->local_has_got_offset(r_sym, tls_got_offset_type));
6628 got_entry_address = target->got_->address() +
6629 got_tlsdesc_offset +
6630 object->local_got_offset(r_sym, tls_got_offset_type);
6632 if (tlsopt == tls::TLSOPT_TO_IE)
6634 if (tls_segment == NULL)
6636 gold_assert(parameters->errors()->error_count() > 0
6637 || issue_undefined_symbol_error(gsym));
6638 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6640 return tls_desc_gd_to_ie(relinfo, target, rela, r_type,
6641 view, psymval, got_entry_address,
6645 // Now do tlsdesc relocation.
6648 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6649 return aarch64_reloc_funcs::adrp(view,
6650 got_entry_address + addend,
6653 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6654 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6655 return aarch64_reloc_funcs::template rela_general<32>(
6656 view, got_entry_address, addend, reloc_property);
6658 case elfcpp::R_AARCH64_TLSDESC_CALL:
6659 return aarch64_reloc_funcs::STATUS_OKAY;
6669 gold_error(_("%s: unsupported TLS reloc %u."),
6670 object->name().c_str(), r_type);
6672 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6673 } // End of relocate_tls.
6676 template<int size, bool big_endian>
6678 typename AArch64_relocate_functions<size, big_endian>::Status
6679 Target_aarch64<size, big_endian>::Relocate::tls_gd_to_le(
6680 const Relocate_info<size, big_endian>* relinfo,
6681 Target_aarch64<size, big_endian>* target,
6682 const elfcpp::Rela<size, big_endian>& rela,
6683 unsigned int r_type,
6684 unsigned char* view,
6685 const Symbol_value<size>* psymval)
6687 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6688 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6689 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6691 Insntype* ip = reinterpret_cast<Insntype*>(view);
6692 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
6693 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
6694 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
6696 if (r_type == elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC)
6698 // This is the 2nd relocs, optimization should already have been
6700 gold_assert((insn1 & 0xfff00000) == 0x91400000);
6701 return aarch64_reloc_funcs::STATUS_OKAY;
6704 // The original sequence is -
6705 // 90000000 adrp x0, 0 <main>
6706 // 91000000 add x0, x0, #0x0
6707 // 94000000 bl 0 <__tls_get_addr>
6708 // optimized to sequence -
6709 // d53bd040 mrs x0, tpidr_el0
6710 // 91400000 add x0, x0, #0x0, lsl #12
6711 // 91000000 add x0, x0, #0x0
6713 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
6714 // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
6715 // have to change "bl tls_get_addr", which does not have a corresponding tls
6716 // relocation type. So before proceeding, we need to make sure compiler
6717 // does not change the sequence.
6718 if(!(insn1 == 0x90000000 // adrp x0,0
6719 && insn2 == 0x91000000 // add x0, x0, #0x0
6720 && insn3 == 0x94000000)) // bl 0
6722 // Ideally we should give up gd_to_le relaxation and do gd access.
6723 // However the gd_to_le relaxation decision has been made early
6724 // in the scan stage, where we did not allocate any GOT entry for
6725 // this symbol. Therefore we have to exit and report error now.
6726 gold_error(_("unexpected reloc insn sequence while relaxing "
6727 "tls gd to le for reloc %u."), r_type);
6728 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6732 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
6733 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
6734 insn3 = 0x91000000; // add x0, x0, #0x0
6735 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
6736 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
6737 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
6739 // Calculate tprel value.
6740 Output_segment* tls_segment = relinfo->layout->tls_segment();
6741 gold_assert(tls_segment != NULL);
6742 AArch64_address value = psymval->value(relinfo->object, 0);
6743 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6744 AArch64_address aligned_tcb_size =
6745 align_address(target->tcb_size(), tls_segment->maximum_alignment());
6746 AArch64_address x = value + aligned_tcb_size;
6748 // After new insns are written, apply TLSLE relocs.
6749 const AArch64_reloc_property* rp1 =
6750 aarch64_reloc_property_table->get_reloc_property(
6751 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
6752 const AArch64_reloc_property* rp2 =
6753 aarch64_reloc_property_table->get_reloc_property(
6754 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
6755 gold_assert(rp1 != NULL && rp2 != NULL);
6757 typename aarch64_reloc_funcs::Status s1 =
6758 aarch64_reloc_funcs::template rela_general<32>(view + 4,
6762 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
6765 typename aarch64_reloc_funcs::Status s2 =
6766 aarch64_reloc_funcs::template rela_general<32>(view + 8,
6771 this->skip_call_tls_get_addr_ = true;
6773 } // End of tls_gd_to_le
6776 template<int size, bool big_endian>
6778 typename AArch64_relocate_functions<size, big_endian>::Status
6779 Target_aarch64<size, big_endian>::Relocate::tls_ld_to_le(
6780 const Relocate_info<size, big_endian>* relinfo,
6781 Target_aarch64<size, big_endian>* target,
6782 const elfcpp::Rela<size, big_endian>& rela,
6783 unsigned int r_type,
6784 unsigned char* view,
6785 const Symbol_value<size>* psymval)
6787 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6788 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6789 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6791 Insntype* ip = reinterpret_cast<Insntype*>(view);
6792 Insntype insn1 = elfcpp::Swap<32, big_endian>::readval(ip);
6793 Insntype insn2 = elfcpp::Swap<32, big_endian>::readval(ip + 1);
6794 Insntype insn3 = elfcpp::Swap<32, big_endian>::readval(ip + 2);
6796 if (r_type == elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC)
6798 // This is the 2nd relocs, optimization should already have been
6800 gold_assert((insn1 & 0xfff00000) == 0x91400000);
6801 return aarch64_reloc_funcs::STATUS_OKAY;
6804 // The original sequence is -
6805 // 90000000 adrp x0, 0 <main>
6806 // 91000000 add x0, x0, #0x0
6807 // 94000000 bl 0 <__tls_get_addr>
6808 // optimized to sequence -
6809 // d53bd040 mrs x0, tpidr_el0
6810 // 91400000 add x0, x0, #0x0, lsl #12
6811 // 91000000 add x0, x0, #0x0
6813 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
6814 // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
6815 // have to change "bl tls_get_addr", which does not have a corresponding tls
6816 // relocation type. So before proceeding, we need to make sure compiler
6817 // does not change the sequence.
6818 if(!(insn1 == 0x90000000 // adrp x0,0
6819 && insn2 == 0x91000000 // add x0, x0, #0x0
6820 && insn3 == 0x94000000)) // bl 0
6822 // Ideally we should give up gd_to_le relaxation and do gd access.
6823 // However the gd_to_le relaxation decision has been made early
6824 // in the scan stage, where we did not allocate any GOT entry for
6825 // this symbol. Therefore we have to exit and report error now.
6826 gold_error(_("unexpected reloc insn sequence while relaxing "
6827 "tls gd to le for reloc %u."), r_type);
6828 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6832 insn1 = 0xd53bd040; // mrs x0, tpidr_el0
6833 insn2 = 0x91400000; // add x0, x0, #0x0, lsl #12
6834 insn3 = 0x91000000; // add x0, x0, #0x0
6835 elfcpp::Swap<32, big_endian>::writeval(ip, insn1);
6836 elfcpp::Swap<32, big_endian>::writeval(ip + 1, insn2);
6837 elfcpp::Swap<32, big_endian>::writeval(ip + 2, insn3);
6839 // Calculate tprel value.
6840 Output_segment* tls_segment = relinfo->layout->tls_segment();
6841 gold_assert(tls_segment != NULL);
6842 AArch64_address value = psymval->value(relinfo->object, 0);
6843 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6844 AArch64_address aligned_tcb_size =
6845 align_address(target->tcb_size(), tls_segment->maximum_alignment());
6846 AArch64_address x = value + aligned_tcb_size;
6848 // After new insns are written, apply TLSLE relocs.
6849 const AArch64_reloc_property* rp1 =
6850 aarch64_reloc_property_table->get_reloc_property(
6851 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12);
6852 const AArch64_reloc_property* rp2 =
6853 aarch64_reloc_property_table->get_reloc_property(
6854 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12);
6855 gold_assert(rp1 != NULL && rp2 != NULL);
6857 typename aarch64_reloc_funcs::Status s1 =
6858 aarch64_reloc_funcs::template rela_general<32>(view + 4,
6862 if (s1 != aarch64_reloc_funcs::STATUS_OKAY)
6865 typename aarch64_reloc_funcs::Status s2 =
6866 aarch64_reloc_funcs::template rela_general<32>(view + 8,
6871 this->skip_call_tls_get_addr_ = true;
6874 } // End of tls_ld_to_le
6876 template<int size, bool big_endian>
6878 typename AArch64_relocate_functions<size, big_endian>::Status
6879 Target_aarch64<size, big_endian>::Relocate::tls_ie_to_le(
6880 const Relocate_info<size, big_endian>* relinfo,
6881 Target_aarch64<size, big_endian>* target,
6882 const elfcpp::Rela<size, big_endian>& rela,
6883 unsigned int r_type,
6884 unsigned char* view,
6885 const Symbol_value<size>* psymval)
6887 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6888 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6889 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6891 AArch64_address value = psymval->value(relinfo->object, 0);
6892 Output_segment* tls_segment = relinfo->layout->tls_segment();
6893 AArch64_address aligned_tcb_address =
6894 align_address(target->tcb_size(), tls_segment->maximum_alignment());
6895 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6896 AArch64_address x = value + addend + aligned_tcb_address;
6897 // "x" is the offset to tp, we can only do this if x is within
6898 // range [0, 2^32-1]
6899 if (!(size == 32 || (size == 64 && (static_cast<uint64_t>(x) >> 32) == 0)))
6901 gold_error(_("TLS variable referred by reloc %u is too far from TP."),
6903 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6906 Insntype* ip = reinterpret_cast<Insntype*>(view);
6907 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
6910 if (r_type == elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21)
6913 regno = (insn & 0x1f);
6914 newinsn = (0xd2a00000 | regno) | (((x >> 16) & 0xffff) << 5);
6916 else if (r_type == elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
6919 regno = (insn & 0x1f);
6920 gold_assert(regno == ((insn >> 5) & 0x1f));
6921 newinsn = (0xf2800000 | regno) | ((x & 0xffff) << 5);
6926 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
6927 return aarch64_reloc_funcs::STATUS_OKAY;
6928 } // End of tls_ie_to_le
6931 template<int size, bool big_endian>
6933 typename AArch64_relocate_functions<size, big_endian>::Status
6934 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_le(
6935 const Relocate_info<size, big_endian>* relinfo,
6936 Target_aarch64<size, big_endian>* target,
6937 const elfcpp::Rela<size, big_endian>& rela,
6938 unsigned int r_type,
6939 unsigned char* view,
6940 const Symbol_value<size>* psymval)
6942 typedef typename elfcpp::Elf_types<size>::Elf_Addr AArch64_address;
6943 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
6944 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
6946 // TLSDESC-GD sequence is like:
6947 // adrp x0, :tlsdesc:v1
6948 // ldr x1, [x0, #:tlsdesc_lo12:v1]
6949 // add x0, x0, :tlsdesc_lo12:v1
6952 // After desc_gd_to_le optimization, the sequence will be like:
6953 // movz x0, #0x0, lsl #16
6958 // Calculate tprel value.
6959 Output_segment* tls_segment = relinfo->layout->tls_segment();
6960 gold_assert(tls_segment != NULL);
6961 Insntype* ip = reinterpret_cast<Insntype*>(view);
6962 const elfcpp::Elf_Xword addend = rela.get_r_addend();
6963 AArch64_address value = psymval->value(relinfo->object, addend);
6964 AArch64_address aligned_tcb_size =
6965 align_address(target->tcb_size(), tls_segment->maximum_alignment());
6966 AArch64_address x = value + aligned_tcb_size;
6967 // x is the offset to tp, we can only do this if x is within range
6968 // [0, 2^32-1]. If x is out of range, fail and exit.
6969 if (size == 64 && (static_cast<uint64_t>(x) >> 32) != 0)
6971 gold_error(_("TLS variable referred by reloc %u is too far from TP. "
6972 "We Can't do gd_to_le relaxation.\n"), r_type);
6973 return aarch64_reloc_funcs::STATUS_BAD_RELOC;
6978 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
6979 case elfcpp::R_AARCH64_TLSDESC_CALL:
6981 newinsn = 0xd503201f;
6984 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
6986 newinsn = 0xd2a00000 | (((x >> 16) & 0xffff) << 5);
6989 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
6991 newinsn = 0xf2800000 | ((x & 0xffff) << 5);
6995 gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
6999 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7000 return aarch64_reloc_funcs::STATUS_OKAY;
7001 } // End of tls_desc_gd_to_le
7004 template<int size, bool big_endian>
7006 typename AArch64_relocate_functions<size, big_endian>::Status
7007 Target_aarch64<size, big_endian>::Relocate::tls_desc_gd_to_ie(
7008 const Relocate_info<size, big_endian>* /* relinfo */,
7009 Target_aarch64<size, big_endian>* /* target */,
7010 const elfcpp::Rela<size, big_endian>& rela,
7011 unsigned int r_type,
7012 unsigned char* view,
7013 const Symbol_value<size>* /* psymval */,
7014 typename elfcpp::Elf_types<size>::Elf_Addr got_entry_address,
7015 typename elfcpp::Elf_types<size>::Elf_Addr address)
7017 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insntype;
7018 typedef AArch64_relocate_functions<size, big_endian> aarch64_reloc_funcs;
7020 // TLSDESC-GD sequence is like:
7021 // adrp x0, :tlsdesc:v1
7022 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7023 // add x0, x0, :tlsdesc_lo12:v1
7026 // After desc_gd_to_ie optimization, the sequence will be like:
7027 // adrp x0, :tlsie:v1
7028 // ldr x0, [x0, :tlsie_lo12:v1]
7032 Insntype* ip = reinterpret_cast<Insntype*>(view);
7033 const elfcpp::Elf_Xword addend = rela.get_r_addend();
7037 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12:
7038 case elfcpp::R_AARCH64_TLSDESC_CALL:
7040 newinsn = 0xd503201f;
7041 elfcpp::Swap<32, big_endian>::writeval(ip, newinsn);
7044 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21:
7046 return aarch64_reloc_funcs::adrp(view, got_entry_address + addend,
7051 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12:
7053 // Set ldr target register to be x0.
7054 Insntype insn = elfcpp::Swap<32, big_endian>::readval(ip);
7056 elfcpp::Swap<32, big_endian>::writeval(ip, insn);
7058 const AArch64_reloc_property* reloc_property =
7059 aarch64_reloc_property_table->get_reloc_property(
7060 elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
7061 return aarch64_reloc_funcs::template rela_general<32>(
7062 view, got_entry_address, addend, reloc_property);
7067 gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
7071 return aarch64_reloc_funcs::STATUS_OKAY;
7072 } // End of tls_desc_gd_to_ie
7074 // Relocate section data.
7076 template<int size, bool big_endian>
7078 Target_aarch64<size, big_endian>::relocate_section(
7079 const Relocate_info<size, big_endian>* relinfo,
7080 unsigned int sh_type,
7081 const unsigned char* prelocs,
7083 Output_section* output_section,
7084 bool needs_special_offset_handling,
7085 unsigned char* view,
7086 typename elfcpp::Elf_types<size>::Elf_Addr address,
7087 section_size_type view_size,
7088 const Reloc_symbol_changes* reloc_symbol_changes)
7090 gold_assert(sh_type == elfcpp::SHT_RELA);
7091 typedef typename Target_aarch64<size, big_endian>::Relocate AArch64_relocate;
7092 gold::relocate_section<size, big_endian, Target_aarch64, elfcpp::SHT_RELA,
7093 AArch64_relocate, gold::Default_comdat_behavior>(
7099 needs_special_offset_handling,
7103 reloc_symbol_changes);
7106 // Return the size of a relocation while scanning during a relocatable
7109 template<int size, bool big_endian>
7111 Target_aarch64<size, big_endian>::Relocatable_size_for_reloc::
7116 // We will never support SHT_REL relocations.
7121 // Scan the relocs during a relocatable link.
7123 template<int size, bool big_endian>
7125 Target_aarch64<size, big_endian>::scan_relocatable_relocs(
7126 Symbol_table* symtab,
7128 Sized_relobj_file<size, big_endian>* object,
7129 unsigned int data_shndx,
7130 unsigned int sh_type,
7131 const unsigned char* prelocs,
7133 Output_section* output_section,
7134 bool needs_special_offset_handling,
7135 size_t local_symbol_count,
7136 const unsigned char* plocal_symbols,
7137 Relocatable_relocs* rr)
7139 gold_assert(sh_type == elfcpp::SHT_RELA);
7141 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
7142 Relocatable_size_for_reloc> Scan_relocatable_relocs;
7144 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7145 Scan_relocatable_relocs>(
7153 needs_special_offset_handling,
7159 // Relocate a section during a relocatable link.
7161 template<int size, bool big_endian>
7163 Target_aarch64<size, big_endian>::relocate_relocs(
7164 const Relocate_info<size, big_endian>* relinfo,
7165 unsigned int sh_type,
7166 const unsigned char* prelocs,
7168 Output_section* output_section,
7169 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7170 const Relocatable_relocs* rr,
7171 unsigned char* view,
7172 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
7173 section_size_type view_size,
7174 unsigned char* reloc_view,
7175 section_size_type reloc_view_size)
7177 gold_assert(sh_type == elfcpp::SHT_RELA);
7179 gold::relocate_relocs<size, big_endian, elfcpp::SHT_RELA>(
7184 offset_in_output_section,
7194 // Return whether this is a 3-insn erratum sequence.
7196 template<int size, bool big_endian>
7198 Target_aarch64<size, big_endian>::is_erratum_843419_sequence(
7199 typename elfcpp::Swap<32,big_endian>::Valtype insn1,
7200 typename elfcpp::Swap<32,big_endian>::Valtype insn2,
7201 typename elfcpp::Swap<32,big_endian>::Valtype insn3)
7206 // The 2nd insn is a single register load or store; or register pair
7208 if (Insn_utilities::aarch64_mem_op_p(insn2, &rt1, &rt2, &pair, &load)
7209 && (!pair || (pair && !load)))
7211 // The 3rd insn is a load or store instruction from the "Load/store
7212 // register (unsigned immediate)" encoding class, using Rn as the
7213 // base address register.
7214 if (Insn_utilities::aarch64_ldst_uimm(insn3)
7215 && (Insn_utilities::aarch64_rn(insn3)
7216 == Insn_utilities::aarch64_rd(insn1)))
7223 // Scan erratum for section SHNDX range
7224 // [output_address + span_start, output_address + span_end).
7226 template<int size, bool big_endian>
7228 Target_aarch64<size, big_endian>::scan_erratum_843419_span(
7229 AArch64_relobj<size, big_endian>* relobj,
7231 const section_size_type span_start,
7232 const section_size_type span_end,
7233 unsigned char* input_view,
7234 Address output_address)
7236 typedef typename Insn_utilities::Insntype Insntype;
7238 // Adjust output_address and view to the start of span.
7239 output_address += span_start;
7240 input_view += span_start;
7242 if ((output_address & 0x03) != 0)
7245 section_size_type offset = 0;
7246 section_size_type span_length = span_end - span_start;
7247 // The first instruction must be ending at 0xFF8 or 0xFFC.
7248 unsigned int page_offset = output_address & 0xFFF;
7249 // Make sure starting position, that is "output_address+offset",
7250 // starts at page position 0xff8 or 0xffc.
7251 if (page_offset < 0xff8)
7252 offset = 0xff8 - page_offset;
7253 while (offset + 3 * Insn_utilities::BYTES_PER_INSN <= span_length)
7255 Insntype* ip = reinterpret_cast<Insntype*>(input_view + offset);
7256 Insntype insn1 = ip[0];
7257 if (Insn_utilities::is_adrp(insn1))
7259 Insntype insn2 = ip[1];
7260 Insntype insn3 = ip[2];
7261 bool do_report = false;
7262 if (is_erratum_843419_sequence(insn1, insn2, insn3))
7264 else if (offset + 4 * Insn_utilities::BYTES_PER_INSN <= span_length)
7266 // Optionally we can have an insn between ins2 and ins3
7267 Insntype insn_opt = ip[2];
7268 // And insn_opt must not be a branch.
7269 if (!Insn_utilities::aarch64_b(insn_opt)
7270 && !Insn_utilities::aarch64_bl(insn_opt)
7271 && !Insn_utilities::aarch64_blr(insn_opt)
7272 && !Insn_utilities::aarch64_br(insn_opt))
7274 // And insn_opt must not write to dest reg in insn1. However
7275 // we do a conservative scan, which means we may fix/report
7276 // more than necessary, but it doesn't hurt.
7278 Insntype insn4 = ip[3];
7279 if (is_erratum_843419_sequence(insn1, insn2, insn4))
7285 gold_error(_("Erratum 943419 found at \"%s\", section %d, "
7287 relobj->name().c_str(), shndx,
7288 (unsigned int)(span_start + offset));
7292 // Advance to next candidate instruction. We only consider instruction
7293 // sequences starting at a page offset of 0xff8 or 0xffc.
7294 page_offset = (output_address + offset) & 0xfff;
7295 if (page_offset == 0xff8)
7297 else // (page_offset == 0xffc), we move to next page's 0xff8.
7303 // The selector for aarch64 object files.
7305 template<int size, bool big_endian>
7306 class Target_selector_aarch64 : public Target_selector
7309 Target_selector_aarch64();
7312 do_instantiate_target()
7313 { return new Target_aarch64<size, big_endian>(); }
7317 Target_selector_aarch64<32, true>::Target_selector_aarch64()
7318 : Target_selector(elfcpp::EM_AARCH64, 32, true,
7319 "elf32-bigaarch64", "aarch64_elf32_be_vec")
7323 Target_selector_aarch64<32, false>::Target_selector_aarch64()
7324 : Target_selector(elfcpp::EM_AARCH64, 32, false,
7325 "elf32-littleaarch64", "aarch64_elf32_le_vec")
7329 Target_selector_aarch64<64, true>::Target_selector_aarch64()
7330 : Target_selector(elfcpp::EM_AARCH64, 64, true,
7331 "elf64-bigaarch64", "aarch64_elf64_be_vec")
7335 Target_selector_aarch64<64, false>::Target_selector_aarch64()
7336 : Target_selector(elfcpp::EM_AARCH64, 64, false,
7337 "elf64-littleaarch64", "aarch64_elf64_le_vec")
7340 Target_selector_aarch64<32, true> target_selector_aarch64elf32b;
7341 Target_selector_aarch64<32, false> target_selector_aarch64elf32;
7342 Target_selector_aarch64<64, true> target_selector_aarch64elfb;
7343 Target_selector_aarch64<64, false> target_selector_aarch64elf;
7345 } // End anonymous namespace.