1 // aarch64.cc -- aarch64 target support for gold.
3 // Copyright (C) 2014-2015 Free Software Foundation, Inc.
4 // Written by Jing Yu <jingyu@google.com> and Han Shen <shenhan@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
31 #include "parameters.h"
38 #include "copy-relocs.h"
40 #include "target-reloc.h"
41 #include "target-select.h"
47 #include "aarch64-reloc-property.h"
49 // The first three .got.plt entries are reserved.
50 const int32_t AARCH64_GOTPLT_RESERVE_COUNT
= 3;
58 template<int size
, bool big_endian
>
59 class Output_data_plt_aarch64
;
61 template<int size
, bool big_endian
>
62 class Output_data_plt_aarch64_standard
;
64 template<int size
, bool big_endian
>
67 template<int size
, bool big_endian
>
68 class AArch64_relocate_functions
;
70 // Utility class dealing with insns. This is ported from macros in
71 // bfd/elfnn-aarch64.cc, but wrapped inside a class as static members. This
72 // class is used in erratum sequence scanning.
74 template<bool big_endian
>
75 class AArch64_insn_utilities
78 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
80 static const int BYTES_PER_INSN
;
82 // Zero register encoding - 31.
83 static const unsigned int AARCH64_ZR
;
86 aarch64_bit(Insntype insn
, int pos
)
87 { return ((1 << pos
) & insn
) >> pos
; }
90 aarch64_bits(Insntype insn
, int pos
, int l
)
91 { return (insn
>> pos
) & ((1 << l
) - 1); }
93 // Get the encoding field "op31" of 3-source data processing insns. "op31" is
94 // the name defined in armv8 insn manual C3.5.9.
96 aarch64_op31(Insntype insn
)
97 { return aarch64_bits(insn
, 21, 3); }
99 // Get the encoding field "ra" of 3-source data processing insns. "ra" is the
100 // third source register. See armv8 insn manual C3.5.9.
102 aarch64_ra(Insntype insn
)
103 { return aarch64_bits(insn
, 10, 5); }
106 is_adrp(const Insntype insn
)
107 { return (insn
& 0x9F000000) == 0x90000000; }
110 aarch64_rm(const Insntype insn
)
111 { return aarch64_bits(insn
, 16, 5); }
114 aarch64_rn(const Insntype insn
)
115 { return aarch64_bits(insn
, 5, 5); }
118 aarch64_rd(const Insntype insn
)
119 { return aarch64_bits(insn
, 0, 5); }
122 aarch64_rt(const Insntype insn
)
123 { return aarch64_bits(insn
, 0, 5); }
126 aarch64_rt2(const Insntype insn
)
127 { return aarch64_bits(insn
, 10, 5); }
130 aarch64_b(const Insntype insn
)
131 { return (insn
& 0xFC000000) == 0x14000000; }
134 aarch64_bl(const Insntype insn
)
135 { return (insn
& 0xFC000000) == 0x94000000; }
138 aarch64_blr(const Insntype insn
)
139 { return (insn
& 0xFFFFFC1F) == 0xD63F0000; }
142 aarch64_br(const Insntype insn
)
143 { return (insn
& 0xFFFFFC1F) == 0xD61F0000; }
145 // All ld/st ops. See C4-182 of the ARM ARM. The encoding space for
146 // LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops.
148 aarch64_ld(Insntype insn
) { return aarch64_bit(insn
, 22) == 1; }
151 aarch64_ldst(Insntype insn
)
152 { return (insn
& 0x0a000000) == 0x08000000; }
155 aarch64_ldst_ex(Insntype insn
)
156 { return (insn
& 0x3f000000) == 0x08000000; }
159 aarch64_ldst_pcrel(Insntype insn
)
160 { return (insn
& 0x3b000000) == 0x18000000; }
163 aarch64_ldst_nap(Insntype insn
)
164 { return (insn
& 0x3b800000) == 0x28000000; }
167 aarch64_ldstp_pi(Insntype insn
)
168 { return (insn
& 0x3b800000) == 0x28800000; }
171 aarch64_ldstp_o(Insntype insn
)
172 { return (insn
& 0x3b800000) == 0x29000000; }
175 aarch64_ldstp_pre(Insntype insn
)
176 { return (insn
& 0x3b800000) == 0x29800000; }
179 aarch64_ldst_ui(Insntype insn
)
180 { return (insn
& 0x3b200c00) == 0x38000000; }
183 aarch64_ldst_piimm(Insntype insn
)
184 { return (insn
& 0x3b200c00) == 0x38000400; }
187 aarch64_ldst_u(Insntype insn
)
188 { return (insn
& 0x3b200c00) == 0x38000800; }
191 aarch64_ldst_preimm(Insntype insn
)
192 { return (insn
& 0x3b200c00) == 0x38000c00; }
195 aarch64_ldst_ro(Insntype insn
)
196 { return (insn
& 0x3b200c00) == 0x38200800; }
199 aarch64_ldst_uimm(Insntype insn
)
200 { return (insn
& 0x3b000000) == 0x39000000; }
203 aarch64_ldst_simd_m(Insntype insn
)
204 { return (insn
& 0xbfbf0000) == 0x0c000000; }
207 aarch64_ldst_simd_m_pi(Insntype insn
)
208 { return (insn
& 0xbfa00000) == 0x0c800000; }
211 aarch64_ldst_simd_s(Insntype insn
)
212 { return (insn
& 0xbf9f0000) == 0x0d000000; }
215 aarch64_ldst_simd_s_pi(Insntype insn
)
216 { return (insn
& 0xbf800000) == 0x0d800000; }
218 // Classify an INSN if it is indeed a load/store. Return true if INSN is a
219 // LD/ST instruction otherwise return false. For scalar LD/ST instructions
220 // PAIR is FALSE, RT is returned and RT2 is set equal to RT. For LD/ST pair
221 // instructions PAIR is TRUE, RT and RT2 are returned.
223 aarch64_mem_op_p(Insntype insn
, unsigned int *rt
, unsigned int *rt2
,
224 bool *pair
, bool *load
)
232 /* Bail out quickly if INSN doesn't fall into the the load-store
234 if (!aarch64_ldst (insn
))
239 if (aarch64_ldst_ex (insn
))
241 *rt
= aarch64_rt (insn
);
243 if (aarch64_bit (insn
, 21) == 1)
246 *rt2
= aarch64_rt2 (insn
);
248 *load
= aarch64_ld (insn
);
251 else if (aarch64_ldst_nap (insn
)
252 || aarch64_ldstp_pi (insn
)
253 || aarch64_ldstp_o (insn
)
254 || aarch64_ldstp_pre (insn
))
257 *rt
= aarch64_rt (insn
);
258 *rt2
= aarch64_rt2 (insn
);
259 *load
= aarch64_ld (insn
);
262 else if (aarch64_ldst_pcrel (insn
)
263 || aarch64_ldst_ui (insn
)
264 || aarch64_ldst_piimm (insn
)
265 || aarch64_ldst_u (insn
)
266 || aarch64_ldst_preimm (insn
)
267 || aarch64_ldst_ro (insn
)
268 || aarch64_ldst_uimm (insn
))
270 *rt
= aarch64_rt (insn
);
272 if (aarch64_ldst_pcrel (insn
))
274 opc
= aarch64_bits (insn
, 22, 2);
275 v
= aarch64_bit (insn
, 26);
276 opc_v
= opc
| (v
<< 2);
277 *load
= (opc_v
== 1 || opc_v
== 2 || opc_v
== 3
278 || opc_v
== 5 || opc_v
== 7);
281 else if (aarch64_ldst_simd_m (insn
)
282 || aarch64_ldst_simd_m_pi (insn
))
284 *rt
= aarch64_rt (insn
);
285 *load
= aarch64_bit (insn
, 22);
286 opcode
= (insn
>> 12) & 0xf;
313 else if (aarch64_ldst_simd_s (insn
)
314 || aarch64_ldst_simd_s_pi (insn
))
316 *rt
= aarch64_rt (insn
);
317 r
= (insn
>> 21) & 1;
318 *load
= aarch64_bit (insn
, 22);
319 opcode
= (insn
>> 13) & 0x7;
331 *rt2
= *rt
+ (r
== 0 ? 2 : 3);
339 *rt2
= *rt
+ (r
== 0 ? 2 : 3);
348 } // End of "aarch64_mem_op_p".
350 // Return true if INSN is mac insn.
352 aarch64_mac(Insntype insn
)
353 { return (insn
& 0xff000000) == 0x9b000000; }
355 // Return true if INSN is multiply-accumulate.
356 // (This is similar to implementaton in elfnn-aarch64.c.)
358 aarch64_mlxl(Insntype insn
)
360 uint32_t op31
= aarch64_op31(insn
);
361 if (aarch64_mac(insn
)
362 && (op31
== 0 || op31
== 1 || op31
== 5)
363 /* Exclude MUL instructions which are encoded as a multiple-accumulate
365 && aarch64_ra(insn
) != AARCH64_ZR
)
371 }; // End of "AArch64_insn_utilities".
374 // Insn length in byte.
376 template<bool big_endian
>
377 const int AArch64_insn_utilities
<big_endian
>::BYTES_PER_INSN
= 4;
380 // Zero register encoding - 31.
382 template<bool big_endian
>
383 const unsigned int AArch64_insn_utilities
<big_endian
>::AARCH64_ZR
= 0x1f;
386 // Output_data_got_aarch64 class.
388 template<int size
, bool big_endian
>
389 class Output_data_got_aarch64
: public Output_data_got
<size
, big_endian
>
392 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
393 Output_data_got_aarch64(Symbol_table
* symtab
, Layout
* layout
)
394 : Output_data_got
<size
, big_endian
>(),
395 symbol_table_(symtab
), layout_(layout
)
398 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
399 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
400 // applied in a static link.
402 add_static_reloc(unsigned int got_offset
, unsigned int r_type
, Symbol
* gsym
)
403 { this->static_relocs_
.push_back(Static_reloc(got_offset
, r_type
, gsym
)); }
406 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
407 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
408 // relocation that needs to be applied in a static link.
410 add_static_reloc(unsigned int got_offset
, unsigned int r_type
,
411 Sized_relobj_file
<size
, big_endian
>* relobj
,
414 this->static_relocs_
.push_back(Static_reloc(got_offset
, r_type
, relobj
,
420 // Write out the GOT table.
422 do_write(Output_file
* of
) {
423 // The first entry in the GOT is the address of the .dynamic section.
424 gold_assert(this->data_size() >= size
/ 8);
425 Output_section
* dynamic
= this->layout_
->dynamic_section();
426 Valtype dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
427 this->replace_constant(0, dynamic_addr
);
428 Output_data_got
<size
, big_endian
>::do_write(of
);
430 // Handling static relocs
431 if (this->static_relocs_
.empty())
434 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
436 gold_assert(parameters
->doing_static_link());
437 const off_t offset
= this->offset();
438 const section_size_type oview_size
=
439 convert_to_section_size_type(this->data_size());
440 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
442 Output_segment
* tls_segment
= this->layout_
->tls_segment();
443 gold_assert(tls_segment
!= NULL
);
445 AArch64_address aligned_tcb_address
=
446 align_address(Target_aarch64
<size
, big_endian
>::TCB_SIZE
,
447 tls_segment
->maximum_alignment());
449 for (size_t i
= 0; i
< this->static_relocs_
.size(); ++i
)
451 Static_reloc
& reloc(this->static_relocs_
[i
]);
452 AArch64_address value
;
454 if (!reloc
.symbol_is_global())
456 Sized_relobj_file
<size
, big_endian
>* object
= reloc
.relobj();
457 const Symbol_value
<size
>* psymval
=
458 reloc
.relobj()->local_symbol(reloc
.index());
460 // We are doing static linking. Issue an error and skip this
461 // relocation if the symbol is undefined or in a discarded_section.
463 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
464 if ((shndx
== elfcpp::SHN_UNDEF
)
466 && shndx
!= elfcpp::SHN_UNDEF
467 && !object
->is_section_included(shndx
)
468 && !this->symbol_table_
->is_section_folded(object
, shndx
)))
470 gold_error(_("undefined or discarded local symbol %u from "
471 " object %s in GOT"),
472 reloc
.index(), reloc
.relobj()->name().c_str());
475 value
= psymval
->value(object
, 0);
479 const Symbol
* gsym
= reloc
.symbol();
480 gold_assert(gsym
!= NULL
);
481 if (gsym
->is_forwarder())
482 gsym
= this->symbol_table_
->resolve_forwards(gsym
);
484 // We are doing static linking. Issue an error and skip this
485 // relocation if the symbol is undefined or in a discarded_section
486 // unless it is a weakly_undefined symbol.
487 if ((gsym
->is_defined_in_discarded_section()
488 || gsym
->is_undefined())
489 && !gsym
->is_weak_undefined())
491 gold_error(_("undefined or discarded symbol %s in GOT"),
496 if (!gsym
->is_weak_undefined())
498 const Sized_symbol
<size
>* sym
=
499 static_cast<const Sized_symbol
<size
>*>(gsym
);
500 value
= sym
->value();
506 unsigned got_offset
= reloc
.got_offset();
507 gold_assert(got_offset
< oview_size
);
509 typedef typename
elfcpp::Swap
<size
, big_endian
>::Valtype Valtype
;
510 Valtype
* wv
= reinterpret_cast<Valtype
*>(oview
+ got_offset
);
512 switch (reloc
.r_type())
514 case elfcpp::R_AARCH64_TLS_DTPREL64
:
517 case elfcpp::R_AARCH64_TLS_TPREL64
:
518 x
= value
+ aligned_tcb_address
;
523 elfcpp::Swap
<size
, big_endian
>::writeval(wv
, x
);
526 of
->write_output_view(offset
, oview_size
, oview
);
530 // Symbol table of the output object.
531 Symbol_table
* symbol_table_
;
532 // A pointer to the Layout class, so that we can find the .dynamic
533 // section when we write out the GOT section.
536 // This class represent dynamic relocations that need to be applied by
537 // gold because we are using TLS relocations in a static link.
541 Static_reloc(unsigned int got_offset
, unsigned int r_type
, Symbol
* gsym
)
542 : got_offset_(got_offset
), r_type_(r_type
), symbol_is_global_(true)
543 { this->u_
.global
.symbol
= gsym
; }
545 Static_reloc(unsigned int got_offset
, unsigned int r_type
,
546 Sized_relobj_file
<size
, big_endian
>* relobj
, unsigned int index
)
547 : got_offset_(got_offset
), r_type_(r_type
), symbol_is_global_(false)
549 this->u_
.local
.relobj
= relobj
;
550 this->u_
.local
.index
= index
;
553 // Return the GOT offset.
556 { return this->got_offset_
; }
561 { return this->r_type_
; }
563 // Whether the symbol is global or not.
565 symbol_is_global() const
566 { return this->symbol_is_global_
; }
568 // For a relocation against a global symbol, the global symbol.
572 gold_assert(this->symbol_is_global_
);
573 return this->u_
.global
.symbol
;
576 // For a relocation against a local symbol, the defining object.
577 Sized_relobj_file
<size
, big_endian
>*
580 gold_assert(!this->symbol_is_global_
);
581 return this->u_
.local
.relobj
;
584 // For a relocation against a local symbol, the local symbol index.
588 gold_assert(!this->symbol_is_global_
);
589 return this->u_
.local
.index
;
593 // GOT offset of the entry to which this relocation is applied.
594 unsigned int got_offset_
;
595 // Type of relocation.
596 unsigned int r_type_
;
597 // Whether this relocation is against a global symbol.
598 bool symbol_is_global_
;
599 // A global or local symbol.
604 // For a global symbol, the symbol itself.
609 // For a local symbol, the object defining the symbol.
610 Sized_relobj_file
<size
, big_endian
>* relobj
;
611 // For a local symbol, the symbol index.
615 }; // End of inner class Static_reloc
617 std::vector
<Static_reloc
> static_relocs_
;
618 }; // End of Output_data_got_aarch64
621 template<int size
, bool big_endian
>
622 class AArch64_input_section
;
625 template<int size
, bool big_endian
>
626 class AArch64_output_section
;
629 template<int size
, bool big_endian
>
630 class AArch64_relobj
;
633 // Stub type enum constants.
639 // Using adrp/add pair, 4 insns (including alignment) without mem access,
640 // the fastest stub. This has a limited jump distance, which is tested by
641 // aarch64_valid_for_adrp_p.
644 // Using ldr-absolute-address/br-register, 4 insns with 1 mem access,
645 // unlimited in jump distance.
646 ST_LONG_BRANCH_ABS
= 2,
648 // Using ldr/calculate-pcrel/jump, 8 insns (including alignment) with 1
649 // mem access, slowest one. Only used in position independent executables.
650 ST_LONG_BRANCH_PCREL
= 3,
652 // Stub for erratum 843419 handling.
655 // Stub for erratum 835769 handling.
658 // Number of total stub types.
663 // Struct that wraps insns for a particular stub. All stub templates are
664 // created/initialized as constants by Stub_template_repertoire.
666 template<bool big_endian
>
669 const typename AArch64_insn_utilities
<big_endian
>::Insntype
* insns
;
674 // Simple singleton class that creates/initializes/stores all types of stub
677 template<bool big_endian
>
678 class Stub_template_repertoire
681 typedef typename AArch64_insn_utilities
<big_endian
>::Insntype Insntype
;
683 // Single static method to get stub template for a given stub type.
684 static const Stub_template
<big_endian
>*
685 get_stub_template(int type
)
687 static Stub_template_repertoire
<big_endian
> singleton
;
688 return singleton
.stub_templates_
[type
];
692 // Constructor - creates/initializes all stub templates.
693 Stub_template_repertoire();
694 ~Stub_template_repertoire()
697 // Disallowing copy ctor and copy assignment operator.
698 Stub_template_repertoire(Stub_template_repertoire
&);
699 Stub_template_repertoire
& operator=(Stub_template_repertoire
&);
701 // Data that stores all insn templates.
702 const Stub_template
<big_endian
>* stub_templates_
[ST_NUMBER
];
703 }; // End of "class Stub_template_repertoire".
706 // Constructor - creates/initilizes all stub templates.
708 template<bool big_endian
>
709 Stub_template_repertoire
<big_endian
>::Stub_template_repertoire()
711 // Insn array definitions.
712 const static Insntype ST_NONE_INSNS
[] = {};
714 const static Insntype ST_ADRP_BRANCH_INSNS
[] =
716 0x90000010, /* adrp ip0, X */
717 /* ADR_PREL_PG_HI21(X) */
718 0x91000210, /* add ip0, ip0, :lo12:X */
719 /* ADD_ABS_LO12_NC(X) */
720 0xd61f0200, /* br ip0 */
721 0x00000000, /* alignment padding */
724 const static Insntype ST_LONG_BRANCH_ABS_INSNS
[] =
726 0x58000050, /* ldr ip0, 0x8 */
727 0xd61f0200, /* br ip0 */
728 0x00000000, /* address field */
729 0x00000000, /* address fields */
732 const static Insntype ST_LONG_BRANCH_PCREL_INSNS
[] =
734 0x58000090, /* ldr ip0, 0x10 */
735 0x10000011, /* adr ip1, #0 */
736 0x8b110210, /* add ip0, ip0, ip1 */
737 0xd61f0200, /* br ip0 */
738 0x00000000, /* address field */
739 0x00000000, /* address field */
740 0x00000000, /* alignment padding */
741 0x00000000, /* alignment padding */
744 const static Insntype ST_E_843419_INSNS
[] =
746 0x00000000, /* Placeholder for erratum insn. */
747 0x14000000, /* b <label> */
750 // ST_E_835769 has the same stub template as ST_E_843419.
751 const static Insntype
* ST_E_835769_INSNS
= ST_E_843419_INSNS
;
753 #define install_insn_template(T) \
754 const static Stub_template<big_endian> template_##T = { \
755 T##_INSNS, sizeof(T##_INSNS) / sizeof(T##_INSNS[0]) }; \
756 this->stub_templates_[T] = &template_##T
758 install_insn_template(ST_NONE
);
759 install_insn_template(ST_ADRP_BRANCH
);
760 install_insn_template(ST_LONG_BRANCH_ABS
);
761 install_insn_template(ST_LONG_BRANCH_PCREL
);
762 install_insn_template(ST_E_843419
);
763 install_insn_template(ST_E_835769
);
765 #undef install_insn_template
769 // Base class for stubs.
771 template<int size
, bool big_endian
>
775 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
776 typedef typename AArch64_insn_utilities
<big_endian
>::Insntype Insntype
;
778 static const AArch64_address invalid_address
=
779 static_cast<AArch64_address
>(-1);
781 static const section_offset_type invalid_offset
=
782 static_cast<section_offset_type
>(-1);
785 : destination_address_(invalid_address
),
786 offset_(invalid_offset
),
796 { return this->type_
; }
798 // Get stub template that provides stub insn information.
799 const Stub_template
<big_endian
>*
800 stub_template() const
802 return Stub_template_repertoire
<big_endian
>::
803 get_stub_template(this->type());
806 // Get destination address.
808 destination_address() const
810 gold_assert(this->destination_address_
!= this->invalid_address
);
811 return this->destination_address_
;
814 // Set destination address.
816 set_destination_address(AArch64_address address
)
818 gold_assert(address
!= this->invalid_address
);
819 this->destination_address_
= address
;
822 // Reset the destination address.
824 reset_destination_address()
825 { this->destination_address_
= this->invalid_address
; }
827 // Get offset of code stub. For Reloc_stub, it is the offset from the
828 // beginning of its containing stub table; for Erratum_stub, it is the offset
829 // from the end of reloc_stubs.
833 gold_assert(this->offset_
!= this->invalid_offset
);
834 return this->offset_
;
839 set_offset(section_offset_type offset
)
840 { this->offset_
= offset
; }
842 // Return the stub insn.
845 { return this->stub_template()->insns
; }
847 // Return num of stub insns.
850 { return this->stub_template()->insn_num
; }
852 // Get size of the stub.
856 return this->insn_num() *
857 AArch64_insn_utilities
<big_endian
>::BYTES_PER_INSN
;
860 // Write stub to output file.
862 write(unsigned char* view
, section_size_type view_size
)
863 { this->do_write(view
, view_size
); }
866 // Abstract method to be implemented by sub-classes.
868 do_write(unsigned char*, section_size_type
) = 0;
871 // The last insn of a stub is a jump to destination insn. This field records
872 // the destination address.
873 AArch64_address destination_address_
;
874 // The stub offset. Note this has difference interpretations between an
875 // Reloc_stub and an Erratum_stub. For Reloc_stub this is the offset from the
876 // beginning of the containing stub_table, whereas for Erratum_stub, this is
877 // the offset from the end of reloc_stubs.
878 section_offset_type offset_
;
881 }; // End of "Stub_base".
884 // Erratum stub class. An erratum stub differs from a reloc stub in that for
885 // each erratum occurrence, we generate an erratum stub. We never share erratum
886 // stubs, whereas for reloc stubs, different branches insns share a single reloc
887 // stub as long as the branch targets are the same. (More to the point, reloc
888 // stubs can be shared because they're used to reach a specific target, whereas
889 // erratum stubs branch back to the original control flow.)
891 template<int size
, bool big_endian
>
892 class Erratum_stub
: public Stub_base
<size
, big_endian
>
895 typedef AArch64_relobj
<size
, big_endian
> The_aarch64_relobj
;
896 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
897 typedef typename AArch64_insn_utilities
<big_endian
>::Insntype Insntype
;
899 static const int STUB_ADDR_ALIGN
;
901 static const Insntype invalid_insn
= static_cast<Insntype
>(-1);
903 Erratum_stub(The_aarch64_relobj
* relobj
, int type
,
904 unsigned shndx
, unsigned int sh_offset
)
905 : Stub_base
<size
, big_endian
>(type
), relobj_(relobj
),
906 shndx_(shndx
), sh_offset_(sh_offset
),
907 erratum_insn_(invalid_insn
),
908 erratum_address_(this->invalid_address
)
913 // Return the object that contains the erratum.
916 { return this->relobj_
; }
918 // Get section index of the erratum.
921 { return this->shndx_
; }
923 // Get section offset of the erratum.
926 { return this->sh_offset_
; }
928 // Get the erratum insn. This is the insn located at erratum_insn_address.
932 gold_assert(this->erratum_insn_
!= this->invalid_insn
);
933 return this->erratum_insn_
;
936 // Set the insn that the erratum happens to.
938 set_erratum_insn(Insntype insn
)
939 { this->erratum_insn_
= insn
; }
941 // Return the address where an erratum must be done.
943 erratum_address() const
945 gold_assert(this->erratum_address_
!= this->invalid_address
);
946 return this->erratum_address_
;
949 // Set the address where an erratum must be done.
951 set_erratum_address(AArch64_address addr
)
952 { this->erratum_address_
= addr
; }
954 // Comparator used to group Erratum_stubs in a set by (obj, shndx,
955 // sh_offset). We do not include 'type' in the calculation, becuase there is
956 // at most one stub type at (obj, shndx, sh_offset).
958 operator<(const Erratum_stub
<size
, big_endian
>& k
) const
962 // We group stubs by relobj.
963 if (this->relobj_
!= k
.relobj_
)
964 return this->relobj_
< k
.relobj_
;
965 // Then by section index.
966 if (this->shndx_
!= k
.shndx_
)
967 return this->shndx_
< k
.shndx_
;
968 // Lastly by section offset.
969 return this->sh_offset_
< k
.sh_offset_
;
974 do_write(unsigned char*, section_size_type
);
977 // The object that needs to be fixed.
978 The_aarch64_relobj
* relobj_
;
979 // The shndx in the object that needs to be fixed.
980 const unsigned int shndx_
;
981 // The section offset in the obejct that needs to be fixed.
982 const unsigned int sh_offset_
;
983 // The insn to be fixed.
984 Insntype erratum_insn_
;
985 // The address of the above insn.
986 AArch64_address erratum_address_
;
987 }; // End of "Erratum_stub".
989 template<int size
, bool big_endian
>
990 const int Erratum_stub
<size
, big_endian
>::STUB_ADDR_ALIGN
= 4;
992 // Comparator used in set definition.
993 template<int size
, bool big_endian
>
994 struct Erratum_stub_less
997 operator()(const Erratum_stub
<size
, big_endian
>* s1
,
998 const Erratum_stub
<size
, big_endian
>* s2
) const
999 { return *s1
< *s2
; }
1002 // Erratum_stub implementation for writing stub to output file.
1004 template<int size
, bool big_endian
>
1006 Erratum_stub
<size
, big_endian
>::do_write(unsigned char* view
, section_size_type
)
1008 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
1009 const Insntype
* insns
= this->insns();
1010 uint32_t num_insns
= this->insn_num();
1011 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
1012 // For current implemented erratum 843419 and 835769, the first insn in the
1013 // stub is always a copy of the problematic insn (in 843419, the mem access
1014 // insn, in 835769, the mac insn), followed by a jump-back.
1015 elfcpp::Swap
<32, big_endian
>::writeval(ip
, this->erratum_insn());
1016 for (uint32_t i
= 1; i
< num_insns
; ++i
)
1017 elfcpp::Swap
<32, big_endian
>::writeval(ip
+ i
, insns
[i
]);
1021 // Reloc stub class.
1023 template<int size
, bool big_endian
>
1024 class Reloc_stub
: public Stub_base
<size
, big_endian
>
1027 typedef Reloc_stub
<size
, big_endian
> This
;
1028 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
1030 // Branch range. This is used to calculate the section group size, as well as
1031 // determine whether a stub is needed.
1032 static const int MAX_BRANCH_OFFSET
= ((1 << 25) - 1) << 2;
1033 static const int MIN_BRANCH_OFFSET
= -((1 << 25) << 2);
1035 // Constant used to determine if an offset fits in the adrp instruction
1037 static const int MAX_ADRP_IMM
= (1 << 20) - 1;
1038 static const int MIN_ADRP_IMM
= -(1 << 20);
1040 static const int BYTES_PER_INSN
= 4;
1041 static const int STUB_ADDR_ALIGN
;
1043 // Determine whether the offset fits in the jump/branch instruction.
1045 aarch64_valid_branch_offset_p(int64_t offset
)
1046 { return offset
>= MIN_BRANCH_OFFSET
&& offset
<= MAX_BRANCH_OFFSET
; }
1048 // Determine whether the offset fits in the adrp immediate field.
1050 aarch64_valid_for_adrp_p(AArch64_address location
, AArch64_address dest
)
1052 typedef AArch64_relocate_functions
<size
, big_endian
> Reloc
;
1053 int64_t adrp_imm
= (Reloc::Page(dest
) - Reloc::Page(location
)) >> 12;
1054 return adrp_imm
>= MIN_ADRP_IMM
&& adrp_imm
<= MAX_ADRP_IMM
;
1057 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
1060 stub_type_for_reloc(unsigned int r_type
, AArch64_address address
,
1061 AArch64_address target
);
1063 Reloc_stub(int type
)
1064 : Stub_base
<size
, big_endian
>(type
)
1070 // The key class used to index the stub instance in the stub table's stub map.
1074 Key(int type
, const Symbol
* symbol
, const Relobj
* relobj
,
1075 unsigned int r_sym
, int32_t addend
)
1076 : type_(type
), addend_(addend
)
1080 this->r_sym_
= Reloc_stub::invalid_index
;
1081 this->u_
.symbol
= symbol
;
1085 gold_assert(relobj
!= NULL
&& r_sym
!= invalid_index
);
1086 this->r_sym_
= r_sym
;
1087 this->u_
.relobj
= relobj
;
1094 // Return stub type.
1097 { return this->type_
; }
1099 // Return the local symbol index or invalid_index.
1102 { return this->r_sym_
; }
1104 // Return the symbol if there is one.
1107 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
1109 // Return the relobj if there is one.
1112 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
1114 // Whether this equals to another key k.
1116 eq(const Key
& k
) const
1118 return ((this->type_
== k
.type_
)
1119 && (this->r_sym_
== k
.r_sym_
)
1120 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
1121 ? (this->u_
.relobj
== k
.u_
.relobj
)
1122 : (this->u_
.symbol
== k
.u_
.symbol
))
1123 && (this->addend_
== k
.addend_
));
1126 // Return a hash value.
1130 size_t name_hash_value
= gold::string_hash
<char>(
1131 (this->r_sym_
!= Reloc_stub::invalid_index
)
1132 ? this->u_
.relobj
->name().c_str()
1133 : this->u_
.symbol
->name());
1134 // We only have 4 stub types.
1135 size_t stub_type_hash_value
= 0x03 & this->type_
;
1136 return (name_hash_value
1137 ^ stub_type_hash_value
1138 ^ ((this->r_sym_
& 0x3fff) << 2)
1139 ^ ((this->addend_
& 0xffff) << 16));
1142 // Functors for STL associative containers.
1146 operator()(const Key
& k
) const
1147 { return k
.hash_value(); }
1153 operator()(const Key
& k1
, const Key
& k2
) const
1154 { return k1
.eq(k2
); }
1160 // If this is a local symbol, this is the index in the defining object.
1161 // Otherwise, it is invalid_index for a global symbol.
1162 unsigned int r_sym_
;
1163 // If r_sym_ is an invalid index, this points to a global symbol.
1164 // Otherwise, it points to a relobj. We used the unsized and target
1165 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
1166 // Arm_relobj, in order to avoid making the stub class a template
1167 // as most of the stub machinery is endianness-neutral. However, it
1168 // may require a bit of casting done by users of this class.
1171 const Symbol
* symbol
;
1172 const Relobj
* relobj
;
1174 // Addend associated with a reloc.
1176 }; // End of inner class Reloc_stub::Key
1179 // This may be overridden in the child class.
1181 do_write(unsigned char*, section_size_type
);
1184 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
1185 }; // End of Reloc_stub
1187 template<int size
, bool big_endian
>
1188 const int Reloc_stub
<size
, big_endian
>::STUB_ADDR_ALIGN
= 4;
1190 // Write data to output file.
1192 template<int size
, bool big_endian
>
1194 Reloc_stub
<size
, big_endian
>::
1195 do_write(unsigned char* view
, section_size_type
)
1197 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
1198 const uint32_t* insns
= this->insns();
1199 uint32_t num_insns
= this->insn_num();
1200 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
1201 for (uint32_t i
= 0; i
< num_insns
; ++i
)
1202 elfcpp::Swap
<32, big_endian
>::writeval(ip
+ i
, insns
[i
]);
1206 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
1209 template<int size
, bool big_endian
>
1211 Reloc_stub
<size
, big_endian
>::stub_type_for_reloc(
1212 unsigned int r_type
, AArch64_address location
, AArch64_address dest
)
1214 int64_t branch_offset
= 0;
1217 case elfcpp::R_AARCH64_CALL26
:
1218 case elfcpp::R_AARCH64_JUMP26
:
1219 branch_offset
= dest
- location
;
1225 if (aarch64_valid_branch_offset_p(branch_offset
))
1228 if (aarch64_valid_for_adrp_p(location
, dest
))
1229 return ST_ADRP_BRANCH
;
1231 if (parameters
->options().output_is_position_independent()
1232 && parameters
->options().output_is_executable())
1233 return ST_LONG_BRANCH_PCREL
;
1235 return ST_LONG_BRANCH_ABS
;
1238 // A class to hold stubs for the ARM target.
1240 template<int size
, bool big_endian
>
1241 class Stub_table
: public Output_data
1244 typedef Target_aarch64
<size
, big_endian
> The_target_aarch64
;
1245 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
1246 typedef AArch64_relobj
<size
, big_endian
> The_aarch64_relobj
;
1247 typedef AArch64_input_section
<size
, big_endian
> The_aarch64_input_section
;
1248 typedef Reloc_stub
<size
, big_endian
> The_reloc_stub
;
1249 typedef typename
The_reloc_stub::Key The_reloc_stub_key
;
1250 typedef Erratum_stub
<size
, big_endian
> The_erratum_stub
;
1251 typedef Erratum_stub_less
<size
, big_endian
> The_erratum_stub_less
;
1252 typedef typename
The_reloc_stub_key::hash The_reloc_stub_key_hash
;
1253 typedef typename
The_reloc_stub_key::equal_to The_reloc_stub_key_equal_to
;
1254 typedef Stub_table
<size
, big_endian
> The_stub_table
;
1255 typedef Unordered_map
<The_reloc_stub_key
, The_reloc_stub
*,
1256 The_reloc_stub_key_hash
, The_reloc_stub_key_equal_to
>
1258 typedef typename
Reloc_stub_map::const_iterator Reloc_stub_map_const_iter
;
1259 typedef Relocate_info
<size
, big_endian
> The_relocate_info
;
1261 typedef std::set
<The_erratum_stub
*, The_erratum_stub_less
> Erratum_stub_set
;
1262 typedef typename
Erratum_stub_set::iterator Erratum_stub_set_iter
;
1264 Stub_table(The_aarch64_input_section
* owner
)
1265 : Output_data(), owner_(owner
), reloc_stubs_size_(0),
1266 erratum_stubs_size_(0), prev_data_size_(0)
1272 The_aarch64_input_section
*
1276 // Whether this stub table is empty.
1279 { return reloc_stubs_
.empty() && erratum_stubs_
.empty(); }
1281 // Return the current data size.
1283 current_data_size() const
1284 { return this->current_data_size_for_child(); }
1286 // Add a STUB using KEY. The caller is responsible for avoiding addition
1287 // if a STUB with the same key has already been added.
1289 add_reloc_stub(The_reloc_stub
* stub
, const The_reloc_stub_key
& key
);
1291 // Add an erratum stub into the erratum stub set. The set is ordered by
1292 // (relobj, shndx, sh_offset).
1294 add_erratum_stub(The_erratum_stub
* stub
);
1296 // Find if such erratum exists for any given (obj, shndx, sh_offset).
1298 find_erratum_stub(The_aarch64_relobj
* a64relobj
,
1299 unsigned int shndx
, unsigned int sh_offset
);
1301 // Find all the erratums for a given input section. The return value is a pair
1302 // of iterators [begin, end).
1303 std::pair
<Erratum_stub_set_iter
, Erratum_stub_set_iter
>
1304 find_erratum_stubs_for_input_section(The_aarch64_relobj
* a64relobj
,
1305 unsigned int shndx
);
1307 // Compute the erratum stub address.
1309 erratum_stub_address(The_erratum_stub
* stub
) const
1311 AArch64_address r
= align_address(this->address() + this->reloc_stubs_size_
,
1312 The_erratum_stub::STUB_ADDR_ALIGN
);
1313 r
+= stub
->offset();
1317 // Finalize stubs. No-op here, just for completeness.
1322 // Look up a relocation stub using KEY. Return NULL if there is none.
1324 find_reloc_stub(The_reloc_stub_key
& key
)
1326 Reloc_stub_map_const_iter p
= this->reloc_stubs_
.find(key
);
1327 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
1330 // Relocate stubs in this stub table.
1332 relocate_stubs(const The_relocate_info
*,
1333 The_target_aarch64
*,
1339 // Update data size at the end of a relaxation pass. Return true if data size
1340 // is different from that of the previous relaxation pass.
1342 update_data_size_changed_p()
1344 // No addralign changed here.
1345 off_t s
= align_address(this->reloc_stubs_size_
,
1346 The_erratum_stub::STUB_ADDR_ALIGN
)
1347 + this->erratum_stubs_size_
;
1348 bool changed
= (s
!= this->prev_data_size_
);
1349 this->prev_data_size_
= s
;
1354 // Write out section contents.
1356 do_write(Output_file
*);
1358 // Return the required alignment.
1360 do_addralign() const
1362 return std::max(The_reloc_stub::STUB_ADDR_ALIGN
,
1363 The_erratum_stub::STUB_ADDR_ALIGN
);
1366 // Reset address and file offset.
1368 do_reset_address_and_file_offset()
1369 { this->set_current_data_size_for_child(this->prev_data_size_
); }
1371 // Set final data size.
1373 set_final_data_size()
1374 { this->set_data_size(this->current_data_size()); }
1377 // Relocate one stub.
1379 relocate_stub(The_reloc_stub
*,
1380 const The_relocate_info
*,
1381 The_target_aarch64
*,
1388 // Owner of this stub table.
1389 The_aarch64_input_section
* owner_
;
1390 // The relocation stubs.
1391 Reloc_stub_map reloc_stubs_
;
1392 // The erratum stubs.
1393 Erratum_stub_set erratum_stubs_
;
1394 // Size of reloc stubs.
1395 off_t reloc_stubs_size_
;
1396 // Size of erratum stubs.
1397 off_t erratum_stubs_size_
;
1398 // data size of this in the previous pass.
1399 off_t prev_data_size_
;
1400 }; // End of Stub_table
1403 // Add an erratum stub into the erratum stub set. The set is ordered by
1404 // (relobj, shndx, sh_offset).
1406 template<int size
, bool big_endian
>
1408 Stub_table
<size
, big_endian
>::add_erratum_stub(The_erratum_stub
* stub
)
1410 std::pair
<Erratum_stub_set_iter
, bool> ret
=
1411 this->erratum_stubs_
.insert(stub
);
1412 gold_assert(ret
.second
);
1413 this->erratum_stubs_size_
= align_address(
1414 this->erratum_stubs_size_
, The_erratum_stub::STUB_ADDR_ALIGN
);
1415 stub
->set_offset(this->erratum_stubs_size_
);
1416 this->erratum_stubs_size_
+= stub
->stub_size();
1420 // Find if such erratum exists for givein (obj, shndx, sh_offset).
1422 template<int size
, bool big_endian
>
1423 Erratum_stub
<size
, big_endian
>*
1424 Stub_table
<size
, big_endian
>::find_erratum_stub(
1425 The_aarch64_relobj
* a64relobj
, unsigned int shndx
, unsigned int sh_offset
)
1427 // A dummy object used as key to search in the set.
1428 The_erratum_stub
key(a64relobj
, ST_NONE
,
1430 Erratum_stub_set_iter i
= this->erratum_stubs_
.find(&key
);
1431 if (i
!= this->erratum_stubs_
.end())
1433 The_erratum_stub
* stub(*i
);
1434 gold_assert(stub
->erratum_insn() != 0);
1441 // Find all the errata for a given input section. The return value is a pair of
1442 // iterators [begin, end).
1444 template<int size
, bool big_endian
>
1445 std::pair
<typename Stub_table
<size
, big_endian
>::Erratum_stub_set_iter
,
1446 typename Stub_table
<size
, big_endian
>::Erratum_stub_set_iter
>
1447 Stub_table
<size
, big_endian
>::find_erratum_stubs_for_input_section(
1448 The_aarch64_relobj
* a64relobj
, unsigned int shndx
)
1450 typedef std::pair
<Erratum_stub_set_iter
, Erratum_stub_set_iter
> Result_pair
;
1451 Erratum_stub_set_iter start
, end
;
1452 The_erratum_stub
low_key(a64relobj
, ST_NONE
, shndx
, 0);
1453 start
= this->erratum_stubs_
.lower_bound(&low_key
);
1454 if (start
== this->erratum_stubs_
.end())
1455 return Result_pair(this->erratum_stubs_
.end(),
1456 this->erratum_stubs_
.end());
1458 while (end
!= this->erratum_stubs_
.end() &&
1459 (*end
)->relobj() == a64relobj
&& (*end
)->shndx() == shndx
)
1461 return Result_pair(start
, end
);
1465 // Add a STUB using KEY. The caller is responsible for avoiding addition
1466 // if a STUB with the same key has already been added.
1468 template<int size
, bool big_endian
>
1470 Stub_table
<size
, big_endian
>::add_reloc_stub(
1471 The_reloc_stub
* stub
, const The_reloc_stub_key
& key
)
1473 gold_assert(stub
->type() == key
.type());
1474 this->reloc_stubs_
[key
] = stub
;
1476 // Assign stub offset early. We can do this because we never remove
1477 // reloc stubs and they are in the beginning of the stub table.
1478 this->reloc_stubs_size_
= align_address(this->reloc_stubs_size_
,
1479 The_reloc_stub::STUB_ADDR_ALIGN
);
1480 stub
->set_offset(this->reloc_stubs_size_
);
1481 this->reloc_stubs_size_
+= stub
->stub_size();
1485 // Relocate all stubs in this stub table.
1487 template<int size
, bool big_endian
>
1489 Stub_table
<size
, big_endian
>::
1490 relocate_stubs(const The_relocate_info
* relinfo
,
1491 The_target_aarch64
* target_aarch64
,
1492 Output_section
* output_section
,
1493 unsigned char* view
,
1494 AArch64_address address
,
1495 section_size_type view_size
)
1497 // "view_size" is the total size of the stub_table.
1498 gold_assert(address
== this->address() &&
1499 view_size
== static_cast<section_size_type
>(this->data_size()));
1500 for(Reloc_stub_map_const_iter p
= this->reloc_stubs_
.begin();
1501 p
!= this->reloc_stubs_
.end(); ++p
)
1502 relocate_stub(p
->second
, relinfo
, target_aarch64
, output_section
,
1503 view
, address
, view_size
);
1505 // Just for convenience.
1506 const int BPI
= AArch64_insn_utilities
<big_endian
>::BYTES_PER_INSN
;
1508 // Now 'relocate' erratum stubs.
1509 for(Erratum_stub_set_iter i
= this->erratum_stubs_
.begin();
1510 i
!= this->erratum_stubs_
.end(); ++i
)
1512 AArch64_address stub_address
= this->erratum_stub_address(*i
);
1513 // The address of "b" in the stub that is to be "relocated".
1514 AArch64_address stub_b_insn_address
;
1515 // Branch offset that is to be filled in "b" insn.
1517 switch ((*i
)->type())
1521 // For the erratum, the 2nd insn is a b-insn to be patched
1523 stub_b_insn_address
= stub_address
+ 1 * BPI
;
1524 b_offset
= (*i
)->destination_address() - stub_b_insn_address
;
1525 AArch64_relocate_functions
<size
, big_endian
>::construct_b(
1526 view
+ (stub_b_insn_address
- this->address()),
1527 ((unsigned int)(b_offset
)) & 0xfffffff);
1537 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
1539 template<int size
, bool big_endian
>
1541 Stub_table
<size
, big_endian
>::
1542 relocate_stub(The_reloc_stub
* stub
,
1543 const The_relocate_info
* relinfo
,
1544 The_target_aarch64
* target_aarch64
,
1545 Output_section
* output_section
,
1546 unsigned char* view
,
1547 AArch64_address address
,
1548 section_size_type view_size
)
1550 // "offset" is the offset from the beginning of the stub_table.
1551 section_size_type offset
= stub
->offset();
1552 section_size_type stub_size
= stub
->stub_size();
1553 // "view_size" is the total size of the stub_table.
1554 gold_assert(offset
+ stub_size
<= view_size
);
1556 target_aarch64
->relocate_stub(stub
, relinfo
, output_section
,
1557 view
+ offset
, address
+ offset
, view_size
);
1561 // Write out the stubs to file.
1563 template<int size
, bool big_endian
>
1565 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
1567 off_t offset
= this->offset();
1568 const section_size_type oview_size
=
1569 convert_to_section_size_type(this->data_size());
1570 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1572 // Write relocation stubs.
1573 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
1574 p
!= this->reloc_stubs_
.end(); ++p
)
1576 The_reloc_stub
* stub
= p
->second
;
1577 AArch64_address address
= this->address() + stub
->offset();
1578 gold_assert(address
==
1579 align_address(address
, The_reloc_stub::STUB_ADDR_ALIGN
));
1580 stub
->write(oview
+ stub
->offset(), stub
->stub_size());
1583 // Write erratum stubs.
1584 unsigned int erratum_stub_start_offset
=
1585 align_address(this->reloc_stubs_size_
, The_erratum_stub::STUB_ADDR_ALIGN
);
1586 for (typename
Erratum_stub_set::iterator p
= this->erratum_stubs_
.begin();
1587 p
!= this->erratum_stubs_
.end(); ++p
)
1589 The_erratum_stub
* stub(*p
);
1590 stub
->write(oview
+ erratum_stub_start_offset
+ stub
->offset(),
1594 of
->write_output_view(this->offset(), oview_size
, oview
);
1598 // AArch64_relobj class.
1600 template<int size
, bool big_endian
>
1601 class AArch64_relobj
: public Sized_relobj_file
<size
, big_endian
>
1604 typedef AArch64_relobj
<size
, big_endian
> This
;
1605 typedef Target_aarch64
<size
, big_endian
> The_target_aarch64
;
1606 typedef AArch64_input_section
<size
, big_endian
> The_aarch64_input_section
;
1607 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
1608 typedef Stub_table
<size
, big_endian
> The_stub_table
;
1609 typedef Erratum_stub
<size
, big_endian
> The_erratum_stub
;
1610 typedef typename
The_stub_table::Erratum_stub_set_iter Erratum_stub_set_iter
;
1611 typedef std::vector
<The_stub_table
*> Stub_table_list
;
1612 static const AArch64_address invalid_address
=
1613 static_cast<AArch64_address
>(-1);
1615 AArch64_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1616 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
1617 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
1624 // Return the stub table of the SHNDX-th section if there is one.
1626 stub_table(unsigned int shndx
) const
1628 gold_assert(shndx
< this->stub_tables_
.size());
1629 return this->stub_tables_
[shndx
];
1632 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1634 set_stub_table(unsigned int shndx
, The_stub_table
* stub_table
)
1636 gold_assert(shndx
< this->stub_tables_
.size());
1637 this->stub_tables_
[shndx
] = stub_table
;
1640 // Entrance to errata scanning.
1642 scan_errata(unsigned int shndx
,
1643 const elfcpp::Shdr
<size
, big_endian
>&,
1644 Output_section
*, const Symbol_table
*,
1645 The_target_aarch64
*);
1647 // Scan all relocation sections for stub generation.
1649 scan_sections_for_stubs(The_target_aarch64
*, const Symbol_table
*,
1652 // Whether a section is a scannable text section.
1654 text_section_is_scannable(const elfcpp::Shdr
<size
, big_endian
>&, unsigned int,
1655 const Output_section
*, const Symbol_table
*);
1657 // Convert regular input section with index SHNDX to a relaxed section.
1659 convert_input_section_to_relaxed_section(unsigned /* shndx */)
1661 // The stubs have relocations and we need to process them after writing
1662 // out the stubs. So relocation now must follow section write.
1663 this->set_relocs_must_follow_section_writes();
1666 // Structure for mapping symbol position.
1667 struct Mapping_symbol_position
1669 Mapping_symbol_position(unsigned int shndx
, AArch64_address offset
):
1670 shndx_(shndx
), offset_(offset
)
1673 // "<" comparator used in ordered_map container.
1675 operator<(const Mapping_symbol_position
& p
) const
1677 return (this->shndx_
< p
.shndx_
1678 || (this->shndx_
== p
.shndx_
&& this->offset_
< p
.offset_
));
1682 unsigned int shndx_
;
1685 AArch64_address offset_
;
1688 typedef std::map
<Mapping_symbol_position
, char> Mapping_symbol_info
;
1691 // Post constructor setup.
1695 // Call parent's setup method.
1696 Sized_relobj_file
<size
, big_endian
>::do_setup();
1698 // Initialize look-up tables.
1699 this->stub_tables_
.resize(this->shnum());
1703 do_relocate_sections(
1704 const Symbol_table
* symtab
, const Layout
* layout
,
1705 const unsigned char* pshdrs
, Output_file
* of
,
1706 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
);
1708 // Count local symbols and (optionally) record mapping info.
1710 do_count_local_symbols(Stringpool_template
<char>*,
1711 Stringpool_template
<char>*);
1714 // Fix all errata in the object.
1716 fix_errata(typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
);
1718 // Whether a section needs to be scanned for relocation stubs.
1720 section_needs_reloc_stub_scanning(const elfcpp::Shdr
<size
, big_endian
>&,
1721 const Relobj::Output_sections
&,
1722 const Symbol_table
*, const unsigned char*);
1724 // List of stub tables.
1725 Stub_table_list stub_tables_
;
1727 // Mapping symbol information sorted by (section index, section_offset).
1728 Mapping_symbol_info mapping_symbol_info_
;
1729 }; // End of AArch64_relobj
1732 // Override to record mapping symbol information.
1733 template<int size
, bool big_endian
>
1735 AArch64_relobj
<size
, big_endian
>::do_count_local_symbols(
1736 Stringpool_template
<char>* pool
, Stringpool_template
<char>* dynpool
)
1738 Sized_relobj_file
<size
, big_endian
>::do_count_local_symbols(pool
, dynpool
);
1740 // Only erratum-fixing work needs mapping symbols, so skip this time consuming
1741 // processing if not fixing erratum.
1742 if (!parameters
->options().fix_cortex_a53_843419()
1743 && !parameters
->options().fix_cortex_a53_835769())
1746 const unsigned int loccount
= this->local_symbol_count();
1750 // Read the symbol table section header.
1751 const unsigned int symtab_shndx
= this->symtab_shndx();
1752 elfcpp::Shdr
<size
, big_endian
>
1753 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
1754 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
1756 // Read the local symbols.
1757 const int sym_size
=elfcpp::Elf_sizes
<size
>::sym_size
;
1758 gold_assert(loccount
== symtabshdr
.get_sh_info());
1759 off_t locsize
= loccount
* sym_size
;
1760 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
1761 locsize
, true, true);
1763 // For mapping symbol processing, we need to read the symbol names.
1764 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
1765 if (strtab_shndx
>= this->shnum())
1767 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
1771 elfcpp::Shdr
<size
, big_endian
>
1772 strtabshdr(this, this->elf_file()->section_header(strtab_shndx
));
1773 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
1775 this->error(_("symbol table name section has wrong type: %u"),
1776 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
1780 const char* pnames
=
1781 reinterpret_cast<const char*>(this->get_view(strtabshdr
.get_sh_offset(),
1782 strtabshdr
.get_sh_size(),
1785 // Skip the first dummy symbol.
1787 typename Sized_relobj_file
<size
, big_endian
>::Local_values
*
1788 plocal_values
= this->local_values();
1789 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
1791 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
1792 Symbol_value
<size
>& lv((*plocal_values
)[i
]);
1793 AArch64_address input_value
= lv
.input_value();
1795 // Check to see if this is a mapping symbol.
1796 const char* sym_name
= pnames
+ sym
.get_st_name();
1797 if (sym_name
[0] == '$' && (sym_name
[1] == 'x' || sym_name
[1] == 'd')
1798 && sym_name
[2] == '\0')
1801 unsigned int input_shndx
=
1802 this->adjust_sym_shndx(i
, sym
.get_st_shndx(), &is_ordinary
);
1803 gold_assert(is_ordinary
);
1805 Mapping_symbol_position
msp(input_shndx
, input_value
);
1806 // Insert mapping_symbol_info into map whose ordering is defined by
1807 // (shndx, offset_within_section).
1808 this->mapping_symbol_info_
[msp
] = sym_name
[1];
1814 // Fix all errata in the object.
1816 template<int size
, bool big_endian
>
1818 AArch64_relobj
<size
, big_endian
>::fix_errata(
1819 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
)
1821 typedef typename
elfcpp::Swap
<32,big_endian
>::Valtype Insntype
;
1822 unsigned int shnum
= this->shnum();
1823 for (unsigned int i
= 1; i
< shnum
; ++i
)
1825 The_stub_table
* stub_table
= this->stub_table(i
);
1828 std::pair
<Erratum_stub_set_iter
, Erratum_stub_set_iter
>
1829 ipair(stub_table
->find_erratum_stubs_for_input_section(this, i
));
1830 Erratum_stub_set_iter p
= ipair
.first
, end
= ipair
.second
;
1833 The_erratum_stub
* stub
= *p
;
1834 typename Sized_relobj_file
<size
, big_endian
>::View_size
&
1835 pview((*pviews
)[i
]);
1837 // Double check data before fix.
1839 reinterpret_cast<Insntype
*>(pview
.view
+ stub
->sh_offset());
1840 Insntype insn_to_fix
= ip
[0];
1841 gold_assert(insn_to_fix
== stub
->erratum_insn());
1842 gold_assert(pview
.address
+ stub
->sh_offset()
1843 == stub
->erratum_address());
1845 AArch64_address stub_address
=
1846 stub_table
->erratum_stub_address(stub
);
1847 unsigned int b_offset
= stub_address
- stub
->erratum_address();
1848 AArch64_relocate_functions
<size
, big_endian
>::construct_b(
1849 pview
.view
+ stub
->sh_offset(), b_offset
& 0xfffffff);
1856 // Relocate sections.
1858 template<int size
, bool big_endian
>
1860 AArch64_relobj
<size
, big_endian
>::do_relocate_sections(
1861 const Symbol_table
* symtab
, const Layout
* layout
,
1862 const unsigned char* pshdrs
, Output_file
* of
,
1863 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
)
1865 // Call parent to relocate sections.
1866 Sized_relobj_file
<size
, big_endian
>::do_relocate_sections(symtab
, layout
,
1867 pshdrs
, of
, pviews
);
1869 // We do not generate stubs if doing a relocatable link.
1870 if (parameters
->options().relocatable())
1873 if (parameters
->options().fix_cortex_a53_843419()
1874 || parameters
->options().fix_cortex_a53_835769())
1875 this->fix_errata(pviews
);
1877 Relocate_info
<size
, big_endian
> relinfo
;
1878 relinfo
.symtab
= symtab
;
1879 relinfo
.layout
= layout
;
1880 relinfo
.object
= this;
1882 // Relocate stub tables.
1883 unsigned int shnum
= this->shnum();
1884 The_target_aarch64
* target
= The_target_aarch64::current_target();
1886 for (unsigned int i
= 1; i
< shnum
; ++i
)
1888 The_aarch64_input_section
* aarch64_input_section
=
1889 target
->find_aarch64_input_section(this, i
);
1890 if (aarch64_input_section
!= NULL
1891 && aarch64_input_section
->is_stub_table_owner()
1892 && !aarch64_input_section
->stub_table()->empty())
1894 Output_section
* os
= this->output_section(i
);
1895 gold_assert(os
!= NULL
);
1897 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
1898 relinfo
.reloc_shdr
= NULL
;
1899 relinfo
.data_shndx
= i
;
1900 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<size
>::shdr_size
;
1902 typename Sized_relobj_file
<size
, big_endian
>::View_size
&
1903 view_struct
= (*pviews
)[i
];
1904 gold_assert(view_struct
.view
!= NULL
);
1906 The_stub_table
* stub_table
= aarch64_input_section
->stub_table();
1907 off_t offset
= stub_table
->address() - view_struct
.address
;
1908 unsigned char* view
= view_struct
.view
+ offset
;
1909 AArch64_address address
= stub_table
->address();
1910 section_size_type view_size
= stub_table
->data_size();
1911 stub_table
->relocate_stubs(&relinfo
, target
, os
, view
, address
,
1918 // Determine if an input section is scannable for stub processing. SHDR is
1919 // the header of the section and SHNDX is the section index. OS is the output
1920 // section for the input section and SYMTAB is the global symbol table used to
1921 // look up ICF information.
1923 template<int size
, bool big_endian
>
1925 AArch64_relobj
<size
, big_endian
>::text_section_is_scannable(
1926 const elfcpp::Shdr
<size
, big_endian
>& text_shdr
,
1927 unsigned int text_shndx
,
1928 const Output_section
* os
,
1929 const Symbol_table
* symtab
)
1931 // Skip any empty sections, unallocated sections or sections whose
1932 // type are not SHT_PROGBITS.
1933 if (text_shdr
.get_sh_size() == 0
1934 || (text_shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0
1935 || text_shdr
.get_sh_type() != elfcpp::SHT_PROGBITS
)
1938 // Skip any discarded or ICF'ed sections.
1939 if (os
== NULL
|| symtab
->is_section_folded(this, text_shndx
))
1942 // Skip exception frame.
1943 if (strcmp(os
->name(), ".eh_frame") == 0)
1946 gold_assert(!this->is_output_section_offset_invalid(text_shndx
) ||
1947 os
->find_relaxed_input_section(this, text_shndx
) != NULL
);
1953 // Determine if we want to scan the SHNDX-th section for relocation stubs.
1954 // This is a helper for AArch64_relobj::scan_sections_for_stubs().
1956 template<int size
, bool big_endian
>
1958 AArch64_relobj
<size
, big_endian
>::section_needs_reloc_stub_scanning(
1959 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1960 const Relobj::Output_sections
& out_sections
,
1961 const Symbol_table
* symtab
,
1962 const unsigned char* pshdrs
)
1964 unsigned int sh_type
= shdr
.get_sh_type();
1965 if (sh_type
!= elfcpp::SHT_RELA
)
1968 // Ignore empty section.
1969 off_t sh_size
= shdr
.get_sh_size();
1973 // Ignore reloc section with unexpected symbol table. The
1974 // error will be reported in the final link.
1975 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
1978 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1979 unsigned int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
1981 // Ignore reloc section with unexpected entsize or uneven size.
1982 // The error will be reported in the final link.
1983 if (reloc_size
!= shdr
.get_sh_entsize() || sh_size
% reloc_size
!= 0)
1986 // Ignore reloc section with bad info. This error will be
1987 // reported in the final link.
1988 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_info());
1989 if (text_shndx
>= this->shnum())
1992 const unsigned int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1993 const elfcpp::Shdr
<size
, big_endian
> text_shdr(pshdrs
+
1994 text_shndx
* shdr_size
);
1995 return this->text_section_is_scannable(text_shdr
, text_shndx
,
1996 out_sections
[text_shndx
], symtab
);
2000 // Scan section SHNDX for erratum 843419 and 835769.
2002 template<int size
, bool big_endian
>
2004 AArch64_relobj
<size
, big_endian
>::scan_errata(
2005 unsigned int shndx
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2006 Output_section
* os
, const Symbol_table
* symtab
,
2007 The_target_aarch64
* target
)
2009 if (shdr
.get_sh_size() == 0
2010 || (shdr
.get_sh_flags() &
2011 (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
)) == 0
2012 || shdr
.get_sh_type() != elfcpp::SHT_PROGBITS
)
2015 if (!os
|| symtab
->is_section_folded(this, shndx
)) return;
2017 AArch64_address output_offset
= this->get_output_section_offset(shndx
);
2018 AArch64_address output_address
;
2019 if (output_offset
!= invalid_address
)
2020 output_address
= os
->address() + output_offset
;
2023 const Output_relaxed_input_section
* poris
=
2024 os
->find_relaxed_input_section(this, shndx
);
2026 output_address
= poris
->address();
2029 section_size_type input_view_size
= 0;
2030 const unsigned char* input_view
=
2031 this->section_contents(shndx
, &input_view_size
, false);
2033 Mapping_symbol_position
section_start(shndx
, 0);
2034 // Find the first mapping symbol record within section shndx.
2035 typename
Mapping_symbol_info::const_iterator p
=
2036 this->mapping_symbol_info_
.lower_bound(section_start
);
2037 if (p
== this->mapping_symbol_info_
.end() || p
->first
.shndx_
!= shndx
)
2038 gold_warning(_("cannot scan executable section %u of %s for Cortex-A53 "
2039 "erratum because it has no mapping symbols."),
2040 shndx
, this->name().c_str());
2041 while (p
!= this->mapping_symbol_info_
.end() &&
2042 p
->first
.shndx_
== shndx
)
2044 typename
Mapping_symbol_info::const_iterator prev
= p
;
2046 if (prev
->second
== 'x')
2048 section_size_type span_start
=
2049 convert_to_section_size_type(prev
->first
.offset_
);
2050 section_size_type span_end
;
2051 if (p
!= this->mapping_symbol_info_
.end()
2052 && p
->first
.shndx_
== shndx
)
2053 span_end
= convert_to_section_size_type(p
->first
.offset_
);
2055 span_end
= convert_to_section_size_type(shdr
.get_sh_size());
2057 // Here we do not share the scanning code of both errata. For 843419,
2058 // only the last few insns of each page are examined, which is fast,
2059 // whereas, for 835769, every insn pair needs to be checked.
2061 if (parameters
->options().fix_cortex_a53_843419())
2062 target
->scan_erratum_843419_span(
2063 this, shndx
, span_start
, span_end
,
2064 const_cast<unsigned char*>(input_view
), output_address
);
2066 if (parameters
->options().fix_cortex_a53_835769())
2067 target
->scan_erratum_835769_span(
2068 this, shndx
, span_start
, span_end
,
2069 const_cast<unsigned char*>(input_view
), output_address
);
2075 // Scan relocations for stub generation.
2077 template<int size
, bool big_endian
>
2079 AArch64_relobj
<size
, big_endian
>::scan_sections_for_stubs(
2080 The_target_aarch64
* target
,
2081 const Symbol_table
* symtab
,
2082 const Layout
* layout
)
2084 unsigned int shnum
= this->shnum();
2085 const unsigned int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2087 // Read the section headers.
2088 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
2092 // To speed up processing, we set up hash tables for fast lookup of
2093 // input offsets to output addresses.
2094 this->initialize_input_to_output_maps();
2096 const Relobj::Output_sections
& out_sections(this->output_sections());
2098 Relocate_info
<size
, big_endian
> relinfo
;
2099 relinfo
.symtab
= symtab
;
2100 relinfo
.layout
= layout
;
2101 relinfo
.object
= this;
2103 // Do relocation stubs scanning.
2104 const unsigned char* p
= pshdrs
+ shdr_size
;
2105 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
2107 const elfcpp::Shdr
<size
, big_endian
> shdr(p
);
2108 if (parameters
->options().fix_cortex_a53_843419()
2109 || parameters
->options().fix_cortex_a53_835769())
2110 scan_errata(i
, shdr
, out_sections
[i
], symtab
, target
);
2111 if (this->section_needs_reloc_stub_scanning(shdr
, out_sections
, symtab
,
2114 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
2115 AArch64_address output_offset
=
2116 this->get_output_section_offset(index
);
2117 AArch64_address output_address
;
2118 if (output_offset
!= invalid_address
)
2120 output_address
= out_sections
[index
]->address() + output_offset
;
2124 // Currently this only happens for a relaxed section.
2125 const Output_relaxed_input_section
* poris
=
2126 out_sections
[index
]->find_relaxed_input_section(this, index
);
2127 gold_assert(poris
!= NULL
);
2128 output_address
= poris
->address();
2131 // Get the relocations.
2132 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
2136 // Get the section contents.
2137 section_size_type input_view_size
= 0;
2138 const unsigned char* input_view
=
2139 this->section_contents(index
, &input_view_size
, false);
2141 relinfo
.reloc_shndx
= i
;
2142 relinfo
.data_shndx
= index
;
2143 unsigned int sh_type
= shdr
.get_sh_type();
2144 unsigned int reloc_size
;
2145 gold_assert (sh_type
== elfcpp::SHT_RELA
);
2146 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
2148 Output_section
* os
= out_sections
[index
];
2149 target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
2150 shdr
.get_sh_size() / reloc_size
,
2152 output_offset
== invalid_address
,
2153 input_view
, output_address
,
2160 // A class to wrap an ordinary input section containing executable code.
2162 template<int size
, bool big_endian
>
2163 class AArch64_input_section
: public Output_relaxed_input_section
2166 typedef Stub_table
<size
, big_endian
> The_stub_table
;
2168 AArch64_input_section(Relobj
* relobj
, unsigned int shndx
)
2169 : Output_relaxed_input_section(relobj
, shndx
, 1),
2171 original_contents_(NULL
), original_size_(0),
2172 original_addralign_(1)
2175 ~AArch64_input_section()
2176 { delete[] this->original_contents_
; }
2182 // Set the stub_table.
2184 set_stub_table(The_stub_table
* st
)
2185 { this->stub_table_
= st
; }
2187 // Whether this is a stub table owner.
2189 is_stub_table_owner() const
2190 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
2192 // Return the original size of the section.
2194 original_size() const
2195 { return this->original_size_
; }
2197 // Return the stub table.
2200 { return stub_table_
; }
2203 // Write out this input section.
2205 do_write(Output_file
*);
2207 // Return required alignment of this.
2209 do_addralign() const
2211 if (this->is_stub_table_owner())
2212 return std::max(this->stub_table_
->addralign(),
2213 static_cast<uint64_t>(this->original_addralign_
));
2215 return this->original_addralign_
;
2218 // Finalize data size.
2220 set_final_data_size();
2222 // Reset address and file offset.
2224 do_reset_address_and_file_offset();
2228 do_output_offset(const Relobj
* object
, unsigned int shndx
,
2229 section_offset_type offset
,
2230 section_offset_type
* poutput
) const
2232 if ((object
== this->relobj())
2233 && (shndx
== this->shndx())
2236 convert_types
<section_offset_type
, uint32_t>(this->original_size_
)))
2246 // Copying is not allowed.
2247 AArch64_input_section(const AArch64_input_section
&);
2248 AArch64_input_section
& operator=(const AArch64_input_section
&);
2250 // The relocation stubs.
2251 The_stub_table
* stub_table_
;
2252 // Original section contents. We have to make a copy here since the file
2253 // containing the original section may not be locked when we need to access
2255 unsigned char* original_contents_
;
2256 // Section size of the original input section.
2257 uint32_t original_size_
;
2258 // Address alignment of the original input section.
2259 uint32_t original_addralign_
;
2260 }; // End of AArch64_input_section
2263 // Finalize data size.
2265 template<int size
, bool big_endian
>
2267 AArch64_input_section
<size
, big_endian
>::set_final_data_size()
2269 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
2271 if (this->is_stub_table_owner())
2273 this->stub_table_
->finalize_data_size();
2274 off
= align_address(off
, this->stub_table_
->addralign());
2275 off
+= this->stub_table_
->data_size();
2277 this->set_data_size(off
);
2281 // Reset address and file offset.
2283 template<int size
, bool big_endian
>
2285 AArch64_input_section
<size
, big_endian
>::do_reset_address_and_file_offset()
2287 // Size of the original input section contents.
2288 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
2290 // If this is a stub table owner, account for the stub table size.
2291 if (this->is_stub_table_owner())
2293 The_stub_table
* stub_table
= this->stub_table_
;
2295 // Reset the stub table's address and file offset. The
2296 // current data size for child will be updated after that.
2297 stub_table_
->reset_address_and_file_offset();
2298 off
= align_address(off
, stub_table_
->addralign());
2299 off
+= stub_table
->current_data_size();
2302 this->set_current_data_size(off
);
2306 // Initialize an Arm_input_section.
2308 template<int size
, bool big_endian
>
2310 AArch64_input_section
<size
, big_endian
>::init()
2312 Relobj
* relobj
= this->relobj();
2313 unsigned int shndx
= this->shndx();
2315 // We have to cache original size, alignment and contents to avoid locking
2316 // the original file.
2317 this->original_addralign_
=
2318 convert_types
<uint32_t, uint64_t>(relobj
->section_addralign(shndx
));
2320 // This is not efficient but we expect only a small number of relaxed
2321 // input sections for stubs.
2322 section_size_type section_size
;
2323 const unsigned char* section_contents
=
2324 relobj
->section_contents(shndx
, §ion_size
, false);
2325 this->original_size_
=
2326 convert_types
<uint32_t, uint64_t>(relobj
->section_size(shndx
));
2328 gold_assert(this->original_contents_
== NULL
);
2329 this->original_contents_
= new unsigned char[section_size
];
2330 memcpy(this->original_contents_
, section_contents
, section_size
);
2332 // We want to make this look like the original input section after
2333 // output sections are finalized.
2334 Output_section
* os
= relobj
->output_section(shndx
);
2335 off_t offset
= relobj
->output_section_offset(shndx
);
2336 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
2337 this->set_address(os
->address() + offset
);
2338 this->set_file_offset(os
->offset() + offset
);
2339 this->set_current_data_size(this->original_size_
);
2340 this->finalize_data_size();
2344 // Write data to output file.
2346 template<int size
, bool big_endian
>
2348 AArch64_input_section
<size
, big_endian
>::do_write(Output_file
* of
)
2350 // We have to write out the original section content.
2351 gold_assert(this->original_contents_
!= NULL
);
2352 of
->write(this->offset(), this->original_contents_
,
2353 this->original_size_
);
2355 // If this owns a stub table and it is not empty, write it.
2356 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
2357 this->stub_table_
->write(of
);
2361 // Arm output section class. This is defined mainly to add a number of stub
2362 // generation methods.
2364 template<int size
, bool big_endian
>
2365 class AArch64_output_section
: public Output_section
2368 typedef Target_aarch64
<size
, big_endian
> The_target_aarch64
;
2369 typedef AArch64_relobj
<size
, big_endian
> The_aarch64_relobj
;
2370 typedef Stub_table
<size
, big_endian
> The_stub_table
;
2371 typedef AArch64_input_section
<size
, big_endian
> The_aarch64_input_section
;
2374 AArch64_output_section(const char* name
, elfcpp::Elf_Word type
,
2375 elfcpp::Elf_Xword flags
)
2376 : Output_section(name
, type
, flags
)
2379 ~AArch64_output_section() {}
2381 // Group input sections for stub generation.
2383 group_sections(section_size_type
, bool, Target_aarch64
<size
, big_endian
>*,
2387 typedef Output_section::Input_section Input_section
;
2388 typedef Output_section::Input_section_list Input_section_list
;
2390 // Create a stub group.
2392 create_stub_group(Input_section_list::const_iterator
,
2393 Input_section_list::const_iterator
,
2394 Input_section_list::const_iterator
,
2395 The_target_aarch64
*,
2396 std::vector
<Output_relaxed_input_section
*>&,
2398 }; // End of AArch64_output_section
2401 // Create a stub group for input sections from FIRST to LAST. OWNER points to
2402 // the input section that will be the owner of the stub table.
2404 template<int size
, bool big_endian
> void
2405 AArch64_output_section
<size
, big_endian
>::create_stub_group(
2406 Input_section_list::const_iterator first
,
2407 Input_section_list::const_iterator last
,
2408 Input_section_list::const_iterator owner
,
2409 The_target_aarch64
* target
,
2410 std::vector
<Output_relaxed_input_section
*>& new_relaxed_sections
,
2413 // Currently we convert ordinary input sections into relaxed sections only
2415 The_aarch64_input_section
* input_section
;
2416 if (owner
->is_relaxed_input_section())
2420 gold_assert(owner
->is_input_section());
2421 // Create a new relaxed input section. We need to lock the original
2423 Task_lock_obj
<Object
> tl(task
, owner
->relobj());
2425 target
->new_aarch64_input_section(owner
->relobj(), owner
->shndx());
2426 new_relaxed_sections
.push_back(input_section
);
2429 // Create a stub table.
2430 The_stub_table
* stub_table
=
2431 target
->new_stub_table(input_section
);
2433 input_section
->set_stub_table(stub_table
);
2435 Input_section_list::const_iterator p
= first
;
2436 // Look for input sections or relaxed input sections in [first ... last].
2439 if (p
->is_input_section() || p
->is_relaxed_input_section())
2441 // The stub table information for input sections live
2442 // in their objects.
2443 The_aarch64_relobj
* aarch64_relobj
=
2444 static_cast<The_aarch64_relobj
*>(p
->relobj());
2445 aarch64_relobj
->set_stub_table(p
->shndx(), stub_table
);
2448 while (p
++ != last
);
2452 // Group input sections for stub generation. GROUP_SIZE is roughly the limit of
2453 // stub groups. We grow a stub group by adding input section until the size is
2454 // just below GROUP_SIZE. The last input section will be converted into a stub
2455 // table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
2456 // after the stub table, effectively doubling the group size.
2458 // This is similar to the group_sections() function in elf32-arm.c but is
2459 // implemented differently.
2461 template<int size
, bool big_endian
>
2462 void AArch64_output_section
<size
, big_endian
>::group_sections(
2463 section_size_type group_size
,
2464 bool stubs_always_after_branch
,
2465 Target_aarch64
<size
, big_endian
>* target
,
2471 FINDING_STUB_SECTION
,
2475 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
2477 State state
= NO_GROUP
;
2478 section_size_type off
= 0;
2479 section_size_type group_begin_offset
= 0;
2480 section_size_type group_end_offset
= 0;
2481 section_size_type stub_table_end_offset
= 0;
2482 Input_section_list::const_iterator group_begin
=
2483 this->input_sections().end();
2484 Input_section_list::const_iterator stub_table
=
2485 this->input_sections().end();
2486 Input_section_list::const_iterator group_end
= this->input_sections().end();
2487 for (Input_section_list::const_iterator p
= this->input_sections().begin();
2488 p
!= this->input_sections().end();
2491 section_size_type section_begin_offset
=
2492 align_address(off
, p
->addralign());
2493 section_size_type section_end_offset
=
2494 section_begin_offset
+ p
->data_size();
2496 // Check to see if we should group the previously seen sections.
2502 case FINDING_STUB_SECTION
:
2503 // Adding this section makes the group larger than GROUP_SIZE.
2504 if (section_end_offset
- group_begin_offset
>= group_size
)
2506 if (stubs_always_after_branch
)
2508 gold_assert(group_end
!= this->input_sections().end());
2509 this->create_stub_group(group_begin
, group_end
, group_end
,
2510 target
, new_relaxed_sections
,
2516 // Input sections up to stub_group_size bytes after the stub
2517 // table can be handled by it too.
2518 state
= HAS_STUB_SECTION
;
2519 stub_table
= group_end
;
2520 stub_table_end_offset
= group_end_offset
;
2525 case HAS_STUB_SECTION
:
2526 // Adding this section makes the post stub-section group larger
2529 // NOT SUPPORTED YET. For completeness only.
2530 if (section_end_offset
- stub_table_end_offset
>= group_size
)
2532 gold_assert(group_end
!= this->input_sections().end());
2533 this->create_stub_group(group_begin
, group_end
, stub_table
,
2534 target
, new_relaxed_sections
, task
);
2543 // If we see an input section and currently there is no group, start
2544 // a new one. Skip any empty sections. We look at the data size
2545 // instead of calling p->relobj()->section_size() to avoid locking.
2546 if ((p
->is_input_section() || p
->is_relaxed_input_section())
2547 && (p
->data_size() != 0))
2549 if (state
== NO_GROUP
)
2551 state
= FINDING_STUB_SECTION
;
2553 group_begin_offset
= section_begin_offset
;
2556 // Keep track of the last input section seen.
2558 group_end_offset
= section_end_offset
;
2561 off
= section_end_offset
;
2564 // Create a stub group for any ungrouped sections.
2565 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
2567 gold_assert(group_end
!= this->input_sections().end());
2568 this->create_stub_group(group_begin
, group_end
,
2569 (state
== FINDING_STUB_SECTION
2572 target
, new_relaxed_sections
, task
);
2575 if (!new_relaxed_sections
.empty())
2576 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
2578 // Update the section offsets
2579 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
2581 The_aarch64_relobj
* relobj
= static_cast<The_aarch64_relobj
*>(
2582 new_relaxed_sections
[i
]->relobj());
2583 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
2584 // Tell AArch64_relobj that this input section is converted.
2585 relobj
->convert_input_section_to_relaxed_section(shndx
);
2587 } // End of AArch64_output_section::group_sections
2590 AArch64_reloc_property_table
* aarch64_reloc_property_table
= NULL
;
2593 // The aarch64 target class.
2595 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
2596 template<int size
, bool big_endian
>
2597 class Target_aarch64
: public Sized_target
<size
, big_endian
>
2600 typedef Target_aarch64
<size
, big_endian
> This
;
2601 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>
2603 typedef Relocate_info
<size
, big_endian
> The_relocate_info
;
2604 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2605 typedef AArch64_relobj
<size
, big_endian
> The_aarch64_relobj
;
2606 typedef Reloc_stub
<size
, big_endian
> The_reloc_stub
;
2607 typedef Erratum_stub
<size
, big_endian
> The_erratum_stub
;
2608 typedef typename Reloc_stub
<size
, big_endian
>::Key The_reloc_stub_key
;
2609 typedef Stub_table
<size
, big_endian
> The_stub_table
;
2610 typedef std::vector
<The_stub_table
*> Stub_table_list
;
2611 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
2612 typedef AArch64_input_section
<size
, big_endian
> The_aarch64_input_section
;
2613 typedef AArch64_output_section
<size
, big_endian
> The_aarch64_output_section
;
2614 typedef Unordered_map
<Section_id
,
2615 AArch64_input_section
<size
, big_endian
>*,
2616 Section_id_hash
> AArch64_input_section_map
;
2617 typedef AArch64_insn_utilities
<big_endian
> Insn_utilities
;
2618 const static int TCB_SIZE
= size
/ 8 * 2;
2620 Target_aarch64(const Target::Target_info
* info
= &aarch64_info
)
2621 : Sized_target
<size
, big_endian
>(info
),
2622 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_irelative_(NULL
),
2623 got_tlsdesc_(NULL
), global_offset_table_(NULL
), rela_dyn_(NULL
),
2624 rela_irelative_(NULL
), copy_relocs_(elfcpp::R_AARCH64_COPY
),
2625 got_mod_index_offset_(-1U),
2626 tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
2627 stub_tables_(), stub_group_size_(0), aarch64_input_section_map_()
2630 // Scan the relocations to determine unreferenced sections for
2631 // garbage collection.
2633 gc_process_relocs(Symbol_table
* symtab
,
2635 Sized_relobj_file
<size
, big_endian
>* object
,
2636 unsigned int data_shndx
,
2637 unsigned int sh_type
,
2638 const unsigned char* prelocs
,
2640 Output_section
* output_section
,
2641 bool needs_special_offset_handling
,
2642 size_t local_symbol_count
,
2643 const unsigned char* plocal_symbols
);
2645 // Scan the relocations to look for symbol adjustments.
2647 scan_relocs(Symbol_table
* symtab
,
2649 Sized_relobj_file
<size
, big_endian
>* object
,
2650 unsigned int data_shndx
,
2651 unsigned int sh_type
,
2652 const unsigned char* prelocs
,
2654 Output_section
* output_section
,
2655 bool needs_special_offset_handling
,
2656 size_t local_symbol_count
,
2657 const unsigned char* plocal_symbols
);
2659 // Finalize the sections.
2661 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
2663 // Return the value to use for a dynamic which requires special
2666 do_dynsym_value(const Symbol
*) const;
2668 // Relocate a section.
2670 relocate_section(const Relocate_info
<size
, big_endian
>*,
2671 unsigned int sh_type
,
2672 const unsigned char* prelocs
,
2674 Output_section
* output_section
,
2675 bool needs_special_offset_handling
,
2676 unsigned char* view
,
2677 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
2678 section_size_type view_size
,
2679 const Reloc_symbol_changes
*);
2681 // Scan the relocs during a relocatable link.
2683 scan_relocatable_relocs(Symbol_table
* symtab
,
2685 Sized_relobj_file
<size
, big_endian
>* object
,
2686 unsigned int data_shndx
,
2687 unsigned int sh_type
,
2688 const unsigned char* prelocs
,
2690 Output_section
* output_section
,
2691 bool needs_special_offset_handling
,
2692 size_t local_symbol_count
,
2693 const unsigned char* plocal_symbols
,
2694 Relocatable_relocs
*);
2696 // Relocate a section during a relocatable link.
2699 const Relocate_info
<size
, big_endian
>*,
2700 unsigned int sh_type
,
2701 const unsigned char* prelocs
,
2703 Output_section
* output_section
,
2704 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
2705 const Relocatable_relocs
*,
2706 unsigned char* view
,
2707 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
2708 section_size_type view_size
,
2709 unsigned char* reloc_view
,
2710 section_size_type reloc_view_size
);
2712 // Return the symbol index to use for a target specific relocation.
2713 // The only target specific relocation is R_AARCH64_TLSDESC for a
2714 // local symbol, which is an absolute reloc.
2716 do_reloc_symbol_index(void*, unsigned int r_type
) const
2718 gold_assert(r_type
== elfcpp::R_AARCH64_TLSDESC
);
2722 // Return the addend to use for a target specific relocation.
2724 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
2726 // Return the PLT section.
2728 do_plt_address_for_global(const Symbol
* gsym
) const
2729 { return this->plt_section()->address_for_global(gsym
); }
2732 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
2733 { return this->plt_section()->address_for_local(relobj
, symndx
); }
2735 // This function should be defined in targets that can use relocation
2736 // types to determine (implemented in local_reloc_may_be_function_pointer
2737 // and global_reloc_may_be_function_pointer)
2738 // if a function's pointer is taken. ICF uses this in safe mode to only
2739 // fold those functions whose pointer is defintely not taken.
2741 do_can_check_for_function_pointers() const
2744 // Return the number of entries in the PLT.
2746 plt_entry_count() const;
2748 //Return the offset of the first non-reserved PLT entry.
2750 first_plt_entry_offset() const;
2752 // Return the size of each PLT entry.
2754 plt_entry_size() const;
2756 // Create a stub table.
2758 new_stub_table(The_aarch64_input_section
*);
2760 // Create an aarch64 input section.
2761 The_aarch64_input_section
*
2762 new_aarch64_input_section(Relobj
*, unsigned int);
2764 // Find an aarch64 input section instance for a given OBJ and SHNDX.
2765 The_aarch64_input_section
*
2766 find_aarch64_input_section(Relobj
*, unsigned int) const;
2768 // Return the thread control block size.
2770 tcb_size() const { return This::TCB_SIZE
; }
2772 // Scan a section for stub generation.
2774 scan_section_for_stubs(const Relocate_info
<size
, big_endian
>*, unsigned int,
2775 const unsigned char*, size_t, Output_section
*,
2776 bool, const unsigned char*,
2780 // Scan a relocation section for stub.
2781 template<int sh_type
>
2783 scan_reloc_section_for_stubs(
2784 const The_relocate_info
* relinfo
,
2785 const unsigned char* prelocs
,
2787 Output_section
* output_section
,
2788 bool needs_special_offset_handling
,
2789 const unsigned char* view
,
2790 Address view_address
,
2793 // Relocate a single stub.
2795 relocate_stub(The_reloc_stub
*, const Relocate_info
<size
, big_endian
>*,
2796 Output_section
*, unsigned char*, Address
,
2799 // Get the default AArch64 target.
2803 gold_assert(parameters
->target().machine_code() == elfcpp::EM_AARCH64
2804 && parameters
->target().get_size() == size
2805 && parameters
->target().is_big_endian() == big_endian
);
2806 return static_cast<This
*>(parameters
->sized_target
<size
, big_endian
>());
2810 // Scan erratum 843419 for a part of a section.
2812 scan_erratum_843419_span(
2813 AArch64_relobj
<size
, big_endian
>*,
2815 const section_size_type
,
2816 const section_size_type
,
2820 // Scan erratum 835769 for a part of a section.
2822 scan_erratum_835769_span(
2823 AArch64_relobj
<size
, big_endian
>*,
2825 const section_size_type
,
2826 const section_size_type
,
2832 do_select_as_default_target()
2834 gold_assert(aarch64_reloc_property_table
== NULL
);
2835 aarch64_reloc_property_table
= new AArch64_reloc_property_table();
2838 // Add a new reloc argument, returning the index in the vector.
2840 add_tlsdesc_info(Sized_relobj_file
<size
, big_endian
>* object
,
2843 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
2844 return this->tlsdesc_reloc_info_
.size() - 1;
2847 virtual Output_data_plt_aarch64
<size
, big_endian
>*
2848 do_make_data_plt(Layout
* layout
,
2849 Output_data_got_aarch64
<size
, big_endian
>* got
,
2850 Output_data_space
* got_plt
,
2851 Output_data_space
* got_irelative
)
2853 return new Output_data_plt_aarch64_standard
<size
, big_endian
>(
2854 layout
, got
, got_plt
, got_irelative
);
2858 // do_make_elf_object to override the same function in the base class.
2860 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
2861 const elfcpp::Ehdr
<size
, big_endian
>&);
2863 Output_data_plt_aarch64
<size
, big_endian
>*
2864 make_data_plt(Layout
* layout
,
2865 Output_data_got_aarch64
<size
, big_endian
>* got
,
2866 Output_data_space
* got_plt
,
2867 Output_data_space
* got_irelative
)
2869 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
);
2872 // We only need to generate stubs, and hence perform relaxation if we are
2873 // not doing relocatable linking.
2875 do_may_relax() const
2876 { return !parameters
->options().relocatable(); }
2878 // Relaxation hook. This is where we do stub generation.
2880 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
2883 group_sections(Layout
* layout
,
2884 section_size_type group_size
,
2885 bool stubs_always_after_branch
,
2889 scan_reloc_for_stub(const The_relocate_info
*, unsigned int,
2890 const Sized_symbol
<size
>*, unsigned int,
2891 const Symbol_value
<size
>*,
2892 typename
elfcpp::Elf_types
<size
>::Elf_Swxword
,
2895 // Make an output section.
2897 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
2898 elfcpp::Elf_Xword flags
)
2899 { return new The_aarch64_output_section(name
, type
, flags
); }
2902 // The class which scans relocations.
2907 : issued_non_pic_error_(false)
2911 local(Symbol_table
* symtab
, Layout
* layout
, Target_aarch64
* target
,
2912 Sized_relobj_file
<size
, big_endian
>* object
,
2913 unsigned int data_shndx
,
2914 Output_section
* output_section
,
2915 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
2916 const elfcpp::Sym
<size
, big_endian
>& lsym
,
2920 global(Symbol_table
* symtab
, Layout
* layout
, Target_aarch64
* target
,
2921 Sized_relobj_file
<size
, big_endian
>* object
,
2922 unsigned int data_shndx
,
2923 Output_section
* output_section
,
2924 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
2928 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
2929 Target_aarch64
<size
, big_endian
>* ,
2930 Sized_relobj_file
<size
, big_endian
>* ,
2933 const elfcpp::Rela
<size
, big_endian
>& ,
2934 unsigned int r_type
,
2935 const elfcpp::Sym
<size
, big_endian
>&);
2938 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
2939 Target_aarch64
<size
, big_endian
>* ,
2940 Sized_relobj_file
<size
, big_endian
>* ,
2943 const elfcpp::Rela
<size
, big_endian
>& ,
2944 unsigned int r_type
,
2949 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
2950 unsigned int r_type
);
2953 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
2954 unsigned int r_type
, Symbol
*);
2957 possible_function_pointer_reloc(unsigned int r_type
);
2960 check_non_pic(Relobj
*, unsigned int r_type
);
2963 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, big_endian
>*,
2964 unsigned int r_type
);
2966 // Whether we have issued an error about a non-PIC compilation.
2967 bool issued_non_pic_error_
;
2970 // The class which implements relocation.
2975 : skip_call_tls_get_addr_(false)
2981 // Do a relocation. Return false if the caller should not issue
2982 // any warnings about this relocation.
2984 relocate(const Relocate_info
<size
, big_endian
>*, Target_aarch64
*,
2986 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
2987 unsigned int r_type
, const Sized_symbol
<size
>*,
2988 const Symbol_value
<size
>*,
2989 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
2993 inline typename AArch64_relocate_functions
<size
, big_endian
>::Status
2994 relocate_tls(const Relocate_info
<size
, big_endian
>*,
2995 Target_aarch64
<size
, big_endian
>*,
2997 const elfcpp::Rela
<size
, big_endian
>&,
2998 unsigned int r_type
, const Sized_symbol
<size
>*,
2999 const Symbol_value
<size
>*,
3001 typename
elfcpp::Elf_types
<size
>::Elf_Addr
);
3003 inline typename AArch64_relocate_functions
<size
, big_endian
>::Status
3005 const Relocate_info
<size
, big_endian
>*,
3006 Target_aarch64
<size
, big_endian
>*,
3007 const elfcpp::Rela
<size
, big_endian
>&,
3010 const Symbol_value
<size
>*);
3012 inline typename AArch64_relocate_functions
<size
, big_endian
>::Status
3014 const Relocate_info
<size
, big_endian
>*,
3015 Target_aarch64
<size
, big_endian
>*,
3016 const elfcpp::Rela
<size
, big_endian
>&,
3019 const Symbol_value
<size
>*);
3021 inline typename AArch64_relocate_functions
<size
, big_endian
>::Status
3023 const Relocate_info
<size
, big_endian
>*,
3024 Target_aarch64
<size
, big_endian
>*,
3025 const elfcpp::Rela
<size
, big_endian
>&,
3028 const Symbol_value
<size
>*);
3030 inline typename AArch64_relocate_functions
<size
, big_endian
>::Status
3032 const Relocate_info
<size
, big_endian
>*,
3033 Target_aarch64
<size
, big_endian
>*,
3034 const elfcpp::Rela
<size
, big_endian
>&,
3037 const Symbol_value
<size
>*);
3039 inline typename AArch64_relocate_functions
<size
, big_endian
>::Status
3041 const Relocate_info
<size
, big_endian
>*,
3042 Target_aarch64
<size
, big_endian
>*,
3043 const elfcpp::Rela
<size
, big_endian
>&,
3046 const Symbol_value
<size
>*,
3047 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
3048 typename
elfcpp::Elf_types
<size
>::Elf_Addr
);
3050 bool skip_call_tls_get_addr_
;
3052 }; // End of class Relocate
3054 // A class which returns the size required for a relocation type,
3055 // used while scanning relocs during a relocatable link.
3056 class Relocatable_size_for_reloc
3060 get_size_for_reloc(unsigned int, Relobj
*);
3063 // Adjust TLS relocation type based on the options and whether this
3064 // is a local symbol.
3065 static tls::Tls_optimization
3066 optimize_tls_reloc(bool is_final
, int r_type
);
3068 // Get the GOT section, creating it if necessary.
3069 Output_data_got_aarch64
<size
, big_endian
>*
3070 got_section(Symbol_table
*, Layout
*);
3072 // Get the GOT PLT section.
3074 got_plt_section() const
3076 gold_assert(this->got_plt_
!= NULL
);
3077 return this->got_plt_
;
3080 // Get the GOT section for TLSDESC entries.
3081 Output_data_got
<size
, big_endian
>*
3082 got_tlsdesc_section() const
3084 gold_assert(this->got_tlsdesc_
!= NULL
);
3085 return this->got_tlsdesc_
;
3088 // Create the PLT section.
3090 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
3092 // Create a PLT entry for a global symbol.
3094 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
3096 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
3098 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
3099 Sized_relobj_file
<size
, big_endian
>* relobj
,
3100 unsigned int local_sym_index
);
3102 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
3104 define_tls_base_symbol(Symbol_table
*, Layout
*);
3106 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
3108 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
3110 // Create a GOT entry for the TLS module index.
3112 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
3113 Sized_relobj_file
<size
, big_endian
>* object
);
3115 // Get the PLT section.
3116 Output_data_plt_aarch64
<size
, big_endian
>*
3119 gold_assert(this->plt_
!= NULL
);
3123 // Helper method to create erratum stubs for ST_E_843419 and ST_E_835769.
3124 void create_erratum_stub(
3125 AArch64_relobj
<size
, big_endian
>* relobj
,
3127 section_size_type erratum_insn_offset
,
3128 Address erratum_address
,
3129 typename
Insn_utilities::Insntype erratum_insn
,
3132 // Return whether this is a 3-insn erratum sequence.
3133 bool is_erratum_843419_sequence(
3134 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn1
,
3135 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn2
,
3136 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn3
);
3138 // Return whether this is a 835769 sequence.
3139 // (Similarly implemented as in elfnn-aarch64.c.)
3140 bool is_erratum_835769_sequence(
3141 typename
elfcpp::Swap
<32,big_endian
>::Valtype
,
3142 typename
elfcpp::Swap
<32,big_endian
>::Valtype
);
3144 // Get the dynamic reloc section, creating it if necessary.
3146 rela_dyn_section(Layout
*);
3148 // Get the section to use for TLSDESC relocations.
3150 rela_tlsdesc_section(Layout
*) const;
3152 // Get the section to use for IRELATIVE relocations.
3154 rela_irelative_section(Layout
*);
3156 // Add a potential copy relocation.
3158 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
3159 Sized_relobj_file
<size
, big_endian
>* object
,
3160 unsigned int shndx
, Output_section
* output_section
,
3161 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
3163 this->copy_relocs_
.copy_reloc(symtab
, layout
,
3164 symtab
->get_sized_symbol
<size
>(sym
),
3165 object
, shndx
, output_section
,
3166 reloc
, this->rela_dyn_section(layout
));
3169 // Information about this specific target which we pass to the
3170 // general Target structure.
3171 static const Target::Target_info aarch64_info
;
3173 // The types of GOT entries needed for this platform.
3174 // These values are exposed to the ABI in an incremental link.
3175 // Do not renumber existing values without changing the version
3176 // number of the .gnu_incremental_inputs section.
3179 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
3180 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
3181 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
3182 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
3185 // This type is used as the argument to the target specific
3186 // relocation routines. The only target specific reloc is
3187 // R_AARCh64_TLSDESC against a local symbol.
3190 Tlsdesc_info(Sized_relobj_file
<size
, big_endian
>* a_object
,
3191 unsigned int a_r_sym
)
3192 : object(a_object
), r_sym(a_r_sym
)
3195 // The object in which the local symbol is defined.
3196 Sized_relobj_file
<size
, big_endian
>* object
;
3197 // The local symbol index in the object.
3202 Output_data_got_aarch64
<size
, big_endian
>* got_
;
3204 Output_data_plt_aarch64
<size
, big_endian
>* plt_
;
3205 // The GOT PLT section.
3206 Output_data_space
* got_plt_
;
3207 // The GOT section for IRELATIVE relocations.
3208 Output_data_space
* got_irelative_
;
3209 // The GOT section for TLSDESC relocations.
3210 Output_data_got
<size
, big_endian
>* got_tlsdesc_
;
3211 // The _GLOBAL_OFFSET_TABLE_ symbol.
3212 Symbol
* global_offset_table_
;
3213 // The dynamic reloc section.
3214 Reloc_section
* rela_dyn_
;
3215 // The section to use for IRELATIVE relocs.
3216 Reloc_section
* rela_irelative_
;
3217 // Relocs saved to avoid a COPY reloc.
3218 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
3219 // Offset of the GOT entry for the TLS module index.
3220 unsigned int got_mod_index_offset_
;
3221 // We handle R_AARCH64_TLSDESC against a local symbol as a target
3222 // specific relocation. Here we store the object and local symbol
3223 // index for the relocation.
3224 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
3225 // True if the _TLS_MODULE_BASE_ symbol has been defined.
3226 bool tls_base_symbol_defined_
;
3227 // List of stub_tables
3228 Stub_table_list stub_tables_
;
3229 // Actual stub group size
3230 section_size_type stub_group_size_
;
3231 AArch64_input_section_map aarch64_input_section_map_
;
3232 }; // End of Target_aarch64
3236 const Target::Target_info Target_aarch64
<64, false>::aarch64_info
=
3239 false, // is_big_endian
3240 elfcpp::EM_AARCH64
, // machine_code
3241 false, // has_make_symbol
3242 false, // has_resolve
3243 false, // has_code_fill
3244 true, // is_default_stack_executable
3245 true, // can_icf_inline_merge_sections
3247 "/lib/ld.so.1", // program interpreter
3248 0x400000, // default_text_segment_address
3249 0x1000, // abi_pagesize (overridable by -z max-page-size)
3250 0x1000, // common_pagesize (overridable by -z common-page-size)
3251 false, // isolate_execinstr
3253 elfcpp::SHN_UNDEF
, // small_common_shndx
3254 elfcpp::SHN_UNDEF
, // large_common_shndx
3255 0, // small_common_section_flags
3256 0, // large_common_section_flags
3257 NULL
, // attributes_section
3258 NULL
, // attributes_vendor
3259 "_start" // entry_symbol_name
3263 const Target::Target_info Target_aarch64
<32, false>::aarch64_info
=
3266 false, // is_big_endian
3267 elfcpp::EM_AARCH64
, // machine_code
3268 false, // has_make_symbol
3269 false, // has_resolve
3270 false, // has_code_fill
3271 true, // is_default_stack_executable
3272 false, // can_icf_inline_merge_sections
3274 "/lib/ld.so.1", // program interpreter
3275 0x400000, // default_text_segment_address
3276 0x1000, // abi_pagesize (overridable by -z max-page-size)
3277 0x1000, // common_pagesize (overridable by -z common-page-size)
3278 false, // isolate_execinstr
3280 elfcpp::SHN_UNDEF
, // small_common_shndx
3281 elfcpp::SHN_UNDEF
, // large_common_shndx
3282 0, // small_common_section_flags
3283 0, // large_common_section_flags
3284 NULL
, // attributes_section
3285 NULL
, // attributes_vendor
3286 "_start" // entry_symbol_name
3290 const Target::Target_info Target_aarch64
<64, true>::aarch64_info
=
3293 true, // is_big_endian
3294 elfcpp::EM_AARCH64
, // machine_code
3295 false, // has_make_symbol
3296 false, // has_resolve
3297 false, // has_code_fill
3298 true, // is_default_stack_executable
3299 true, // can_icf_inline_merge_sections
3301 "/lib/ld.so.1", // program interpreter
3302 0x400000, // default_text_segment_address
3303 0x1000, // abi_pagesize (overridable by -z max-page-size)
3304 0x1000, // common_pagesize (overridable by -z common-page-size)
3305 false, // isolate_execinstr
3307 elfcpp::SHN_UNDEF
, // small_common_shndx
3308 elfcpp::SHN_UNDEF
, // large_common_shndx
3309 0, // small_common_section_flags
3310 0, // large_common_section_flags
3311 NULL
, // attributes_section
3312 NULL
, // attributes_vendor
3313 "_start" // entry_symbol_name
3317 const Target::Target_info Target_aarch64
<32, true>::aarch64_info
=
3320 true, // is_big_endian
3321 elfcpp::EM_AARCH64
, // machine_code
3322 false, // has_make_symbol
3323 false, // has_resolve
3324 false, // has_code_fill
3325 true, // is_default_stack_executable
3326 false, // can_icf_inline_merge_sections
3328 "/lib/ld.so.1", // program interpreter
3329 0x400000, // default_text_segment_address
3330 0x1000, // abi_pagesize (overridable by -z max-page-size)
3331 0x1000, // common_pagesize (overridable by -z common-page-size)
3332 false, // isolate_execinstr
3334 elfcpp::SHN_UNDEF
, // small_common_shndx
3335 elfcpp::SHN_UNDEF
, // large_common_shndx
3336 0, // small_common_section_flags
3337 0, // large_common_section_flags
3338 NULL
, // attributes_section
3339 NULL
, // attributes_vendor
3340 "_start" // entry_symbol_name
3343 // Get the GOT section, creating it if necessary.
3345 template<int size
, bool big_endian
>
3346 Output_data_got_aarch64
<size
, big_endian
>*
3347 Target_aarch64
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
3350 if (this->got_
== NULL
)
3352 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
3354 // When using -z now, we can treat .got.plt as a relro section.
3355 // Without -z now, it is modified after program startup by lazy
3357 bool is_got_plt_relro
= parameters
->options().now();
3358 Output_section_order got_order
= (is_got_plt_relro
3360 : ORDER_RELRO_LAST
);
3361 Output_section_order got_plt_order
= (is_got_plt_relro
3363 : ORDER_NON_RELRO_FIRST
);
3365 // Layout of .got and .got.plt sections.
3366 // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
3368 // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
3369 // .gotplt[1] reserved for ld.so (resolver)
3370 // .gotplt[2] reserved
3372 // Generate .got section.
3373 this->got_
= new Output_data_got_aarch64
<size
, big_endian
>(symtab
,
3375 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3376 (elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
),
3377 this->got_
, got_order
, true);
3378 // The first word of GOT is reserved for the address of .dynamic.
3379 // We put 0 here now. The value will be replaced later in
3380 // Output_data_got_aarch64::do_write.
3381 this->got_
->add_constant(0);
3383 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3384 // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
3385 // even if there is a .got.plt section.
3386 this->global_offset_table_
=
3387 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
3388 Symbol_table::PREDEFINED
,
3390 0, 0, elfcpp::STT_OBJECT
,
3392 elfcpp::STV_HIDDEN
, 0,
3395 // Generate .got.plt section.
3396 this->got_plt_
= new Output_data_space(size
/ 8, "** GOT PLT");
3397 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
3399 | elfcpp::SHF_WRITE
),
3400 this->got_plt_
, got_plt_order
,
3403 // The first three entries are reserved.
3404 this->got_plt_
->set_current_data_size(
3405 AARCH64_GOTPLT_RESERVE_COUNT
* (size
/ 8));
3407 // If there are any IRELATIVE relocations, they get GOT entries
3408 // in .got.plt after the jump slot entries.
3409 this->got_irelative_
= new Output_data_space(size
/ 8,
3410 "** GOT IRELATIVE PLT");
3411 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
3413 | elfcpp::SHF_WRITE
),
3414 this->got_irelative_
,
3418 // If there are any TLSDESC relocations, they get GOT entries in
3419 // .got.plt after the jump slot and IRELATIVE entries.
3420 this->got_tlsdesc_
= new Output_data_got
<size
, big_endian
>();
3421 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
3423 | elfcpp::SHF_WRITE
),
3428 if (!is_got_plt_relro
)
3430 // Those bytes can go into the relro segment.
3431 layout
->increase_relro(
3432 AARCH64_GOTPLT_RESERVE_COUNT
* (size
/ 8));
3439 // Get the dynamic reloc section, creating it if necessary.
3441 template<int size
, bool big_endian
>
3442 typename Target_aarch64
<size
, big_endian
>::Reloc_section
*
3443 Target_aarch64
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
3445 if (this->rela_dyn_
== NULL
)
3447 gold_assert(layout
!= NULL
);
3448 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
3449 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
3450 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
3451 ORDER_DYNAMIC_RELOCS
, false);
3453 return this->rela_dyn_
;
3456 // Get the section to use for IRELATIVE relocs, creating it if
3457 // necessary. These go in .rela.dyn, but only after all other dynamic
3458 // relocations. They need to follow the other dynamic relocations so
3459 // that they can refer to global variables initialized by those
3462 template<int size
, bool big_endian
>
3463 typename Target_aarch64
<size
, big_endian
>::Reloc_section
*
3464 Target_aarch64
<size
, big_endian
>::rela_irelative_section(Layout
* layout
)
3466 if (this->rela_irelative_
== NULL
)
3468 // Make sure we have already created the dynamic reloc section.
3469 this->rela_dyn_section(layout
);
3470 this->rela_irelative_
= new Reloc_section(false);
3471 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
3472 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
3473 ORDER_DYNAMIC_RELOCS
, false);
3474 gold_assert(this->rela_dyn_
->output_section()
3475 == this->rela_irelative_
->output_section());
3477 return this->rela_irelative_
;
3481 // do_make_elf_object to override the same function in the base class. We need
3482 // to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
3483 // store backend specific information. Hence we need to have our own ELF object
3486 template<int size
, bool big_endian
>
3488 Target_aarch64
<size
, big_endian
>::do_make_elf_object(
3489 const std::string
& name
,
3490 Input_file
* input_file
,
3491 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
3493 int et
= ehdr
.get_e_type();
3494 // ET_EXEC files are valid input for --just-symbols/-R,
3495 // and we treat them as relocatable objects.
3496 if (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols())
3497 return Sized_target
<size
, big_endian
>::do_make_elf_object(
3498 name
, input_file
, offset
, ehdr
);
3499 else if (et
== elfcpp::ET_REL
)
3501 AArch64_relobj
<size
, big_endian
>* obj
=
3502 new AArch64_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
3506 else if (et
== elfcpp::ET_DYN
)
3508 // Keep base implementation.
3509 Sized_dynobj
<size
, big_endian
>* obj
=
3510 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
3516 gold_error(_("%s: unsupported ELF file type %d"),
3523 // Scan a relocation for stub generation.
3525 template<int size
, bool big_endian
>
3527 Target_aarch64
<size
, big_endian
>::scan_reloc_for_stub(
3528 const Relocate_info
<size
, big_endian
>* relinfo
,
3529 unsigned int r_type
,
3530 const Sized_symbol
<size
>* gsym
,
3532 const Symbol_value
<size
>* psymval
,
3533 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
,
3536 const AArch64_relobj
<size
, big_endian
>* aarch64_relobj
=
3537 static_cast<AArch64_relobj
<size
, big_endian
>*>(relinfo
->object
);
3539 Symbol_value
<size
> symval
;
3542 const AArch64_reloc_property
* arp
= aarch64_reloc_property_table
->
3543 get_reloc_property(r_type
);
3544 if (gsym
->use_plt_offset(arp
->reference_flags()))
3546 // This uses a PLT, change the symbol value.
3547 symval
.set_output_value(this->plt_section()->address()
3548 + gsym
->plt_offset());
3551 else if (gsym
->is_undefined())
3552 // There is no need to generate a stub symbol is undefined.
3556 // Get the symbol value.
3557 typename Symbol_value
<size
>::Value value
= psymval
->value(aarch64_relobj
, 0);
3559 // Owing to pipelining, the PC relative branches below actually skip
3560 // two instructions when the branch offset is 0.
3561 Address destination
= static_cast<Address
>(-1);
3564 case elfcpp::R_AARCH64_CALL26
:
3565 case elfcpp::R_AARCH64_JUMP26
:
3566 destination
= value
+ addend
;
3572 int stub_type
= The_reloc_stub::
3573 stub_type_for_reloc(r_type
, address
, destination
);
3574 if (stub_type
== ST_NONE
)
3577 The_stub_table
* stub_table
= aarch64_relobj
->stub_table(relinfo
->data_shndx
);
3578 gold_assert(stub_table
!= NULL
);
3580 The_reloc_stub_key
key(stub_type
, gsym
, aarch64_relobj
, r_sym
, addend
);
3581 The_reloc_stub
* stub
= stub_table
->find_reloc_stub(key
);
3584 stub
= new The_reloc_stub(stub_type
);
3585 stub_table
->add_reloc_stub(stub
, key
);
3587 stub
->set_destination_address(destination
);
3588 } // End of Target_aarch64::scan_reloc_for_stub
3591 // This function scans a relocation section for stub generation.
3592 // The template parameter Relocate must be a class type which provides
3593 // a single function, relocate(), which implements the machine
3594 // specific part of a relocation.
3596 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
3597 // SHT_REL or SHT_RELA.
3599 // PRELOCS points to the relocation data. RELOC_COUNT is the number
3600 // of relocs. OUTPUT_SECTION is the output section.
3601 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
3602 // mapped to output offsets.
3604 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
3605 // VIEW_SIZE is the size. These refer to the input section, unless
3606 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
3607 // the output section.
3609 template<int size
, bool big_endian
>
3610 template<int sh_type
>
3612 Target_aarch64
<size
, big_endian
>::scan_reloc_section_for_stubs(
3613 const Relocate_info
<size
, big_endian
>* relinfo
,
3614 const unsigned char* prelocs
,
3616 Output_section
* /*output_section*/,
3617 bool /*needs_special_offset_handling*/,
3618 const unsigned char* /*view*/,
3619 Address view_address
,
3622 typedef typename Reloc_types
<sh_type
,size
,big_endian
>::Reloc Reltype
;
3624 const int reloc_size
=
3625 Reloc_types
<sh_type
,size
,big_endian
>::reloc_size
;
3626 AArch64_relobj
<size
, big_endian
>* object
=
3627 static_cast<AArch64_relobj
<size
, big_endian
>*>(relinfo
->object
);
3628 unsigned int local_count
= object
->local_symbol_count();
3630 gold::Default_comdat_behavior default_comdat_behavior
;
3631 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
3633 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
3635 Reltype
reloc(prelocs
);
3636 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
3637 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
3638 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
3639 if (r_type
!= elfcpp::R_AARCH64_CALL26
3640 && r_type
!= elfcpp::R_AARCH64_JUMP26
)
3643 section_offset_type offset
=
3644 convert_to_section_size_type(reloc
.get_r_offset());
3647 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
=
3648 reloc
.get_r_addend();
3650 const Sized_symbol
<size
>* sym
;
3651 Symbol_value
<size
> symval
;
3652 const Symbol_value
<size
> *psymval
;
3653 bool is_defined_in_discarded_section
;
3655 if (r_sym
< local_count
)
3658 psymval
= object
->local_symbol(r_sym
);
3660 // If the local symbol belongs to a section we are discarding,
3661 // and that section is a debug section, try to find the
3662 // corresponding kept section and map this symbol to its
3663 // counterpart in the kept section. The symbol must not
3664 // correspond to a section we are folding.
3666 shndx
= psymval
->input_shndx(&is_ordinary
);
3667 is_defined_in_discarded_section
=
3669 && shndx
!= elfcpp::SHN_UNDEF
3670 && !object
->is_section_included(shndx
)
3671 && !relinfo
->symtab
->is_section_folded(object
, shndx
));
3673 // We need to compute the would-be final value of this local
3675 if (!is_defined_in_discarded_section
)
3677 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
3678 typename
ObjType::Compute_final_local_value_status status
=
3679 object
->compute_final_local_value(r_sym
, psymval
, &symval
,
3681 if (status
== ObjType::CFLV_OK
)
3683 // Currently we cannot handle a branch to a target in
3684 // a merged section. If this is the case, issue an error
3685 // and also free the merge symbol value.
3686 if (!symval
.has_output_value())
3688 const std::string
& section_name
=
3689 object
->section_name(shndx
);
3690 object
->error(_("cannot handle branch to local %u "
3691 "in a merged section %s"),
3692 r_sym
, section_name
.c_str());
3698 // We cannot determine the final value.
3706 gsym
= object
->global_symbol(r_sym
);
3707 gold_assert(gsym
!= NULL
);
3708 if (gsym
->is_forwarder())
3709 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
3711 sym
= static_cast<const Sized_symbol
<size
>*>(gsym
);
3712 if (sym
->has_symtab_index() && sym
->symtab_index() != -1U)
3713 symval
.set_output_symtab_index(sym
->symtab_index());
3715 symval
.set_no_output_symtab_entry();
3717 // We need to compute the would-be final value of this global
3719 const Symbol_table
* symtab
= relinfo
->symtab
;
3720 const Sized_symbol
<size
>* sized_symbol
=
3721 symtab
->get_sized_symbol
<size
>(gsym
);
3722 Symbol_table::Compute_final_value_status status
;
3723 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
=
3724 symtab
->compute_final_value
<size
>(sized_symbol
, &status
);
3726 // Skip this if the symbol has not output section.
3727 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
3729 symval
.set_output_value(value
);
3731 if (gsym
->type() == elfcpp::STT_TLS
)
3732 symval
.set_is_tls_symbol();
3733 else if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
3734 symval
.set_is_ifunc_symbol();
3737 is_defined_in_discarded_section
=
3738 (gsym
->is_defined_in_discarded_section()
3739 && gsym
->is_undefined());
3743 Symbol_value
<size
> symval2
;
3744 if (is_defined_in_discarded_section
)
3746 if (comdat_behavior
== CB_UNDETERMINED
)
3748 std::string name
= object
->section_name(relinfo
->data_shndx
);
3749 comdat_behavior
= default_comdat_behavior
.get(name
.c_str());
3751 if (comdat_behavior
== CB_PRETEND
)
3754 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
=
3755 object
->map_to_kept_section(shndx
, &found
);
3757 symval2
.set_output_value(value
+ psymval
->input_value());
3759 symval2
.set_output_value(0);
3763 if (comdat_behavior
== CB_WARNING
)
3764 gold_warning_at_location(relinfo
, i
, offset
,
3765 _("relocation refers to discarded "
3767 symval2
.set_output_value(0);
3769 symval2
.set_no_output_symtab_entry();
3773 // If symbol is a section symbol, we don't know the actual type of
3774 // destination. Give up.
3775 if (psymval
->is_section_symbol())
3778 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
3779 addend
, view_address
+ offset
);
3780 } // End of iterating relocs in a section
3781 } // End of Target_aarch64::scan_reloc_section_for_stubs
3784 // Scan an input section for stub generation.
3786 template<int size
, bool big_endian
>
3788 Target_aarch64
<size
, big_endian
>::scan_section_for_stubs(
3789 const Relocate_info
<size
, big_endian
>* relinfo
,
3790 unsigned int sh_type
,
3791 const unsigned char* prelocs
,
3793 Output_section
* output_section
,
3794 bool needs_special_offset_handling
,
3795 const unsigned char* view
,
3796 Address view_address
,
3797 section_size_type view_size
)
3799 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3800 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
3805 needs_special_offset_handling
,
3812 // Relocate a single stub.
3814 template<int size
, bool big_endian
>
3815 void Target_aarch64
<size
, big_endian
>::
3816 relocate_stub(The_reloc_stub
* stub
,
3817 const The_relocate_info
*,
3819 unsigned char* view
,
3823 typedef AArch64_relocate_functions
<size
, big_endian
> The_reloc_functions
;
3824 typedef typename
The_reloc_functions::Status The_reloc_functions_status
;
3825 typedef typename
elfcpp::Swap
<32,big_endian
>::Valtype Insntype
;
3827 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
3828 int insn_number
= stub
->insn_num();
3829 const uint32_t* insns
= stub
->insns();
3830 // Check the insns are really those stub insns.
3831 for (int i
= 0; i
< insn_number
; ++i
)
3833 Insntype insn
= elfcpp::Swap
<32,big_endian
>::readval(ip
+ i
);
3834 gold_assert(((uint32_t)insn
== insns
[i
]));
3837 Address dest
= stub
->destination_address();
3839 switch(stub
->type())
3841 case ST_ADRP_BRANCH
:
3843 // 1st reloc is ADR_PREL_PG_HI21
3844 The_reloc_functions_status status
=
3845 The_reloc_functions::adrp(view
, dest
, address
);
3846 // An error should never arise in the above step. If so, please
3847 // check 'aarch64_valid_for_adrp_p'.
3848 gold_assert(status
== The_reloc_functions::STATUS_OKAY
);
3850 // 2nd reloc is ADD_ABS_LO12_NC
3851 const AArch64_reloc_property
* arp
=
3852 aarch64_reloc_property_table
->get_reloc_property(
3853 elfcpp::R_AARCH64_ADD_ABS_LO12_NC
);
3854 gold_assert(arp
!= NULL
);
3855 status
= The_reloc_functions::template
3856 rela_general
<32>(view
+ 4, dest
, 0, arp
);
3857 // An error should never arise, it is an "_NC" relocation.
3858 gold_assert(status
== The_reloc_functions::STATUS_OKAY
);
3862 case ST_LONG_BRANCH_ABS
:
3863 // 1st reloc is R_AARCH64_PREL64, at offset 8
3864 elfcpp::Swap
<64,big_endian
>::writeval(view
+ 8, dest
);
3867 case ST_LONG_BRANCH_PCREL
:
3869 // "PC" calculation is the 2nd insn in the stub.
3870 uint64_t offset
= dest
- (address
+ 4);
3871 // Offset is placed at offset 4 and 5.
3872 elfcpp::Swap
<64,big_endian
>::writeval(view
+ 16, offset
);
3882 // A class to handle the PLT data.
3883 // This is an abstract base class that handles most of the linker details
3884 // but does not know the actual contents of PLT entries. The derived
3885 // classes below fill in those details.
3887 template<int size
, bool big_endian
>
3888 class Output_data_plt_aarch64
: public Output_section_data
3891 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>
3893 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3895 Output_data_plt_aarch64(Layout
* layout
,
3897 Output_data_got_aarch64
<size
, big_endian
>* got
,
3898 Output_data_space
* got_plt
,
3899 Output_data_space
* got_irelative
)
3900 : Output_section_data(addralign
), tlsdesc_rel_(NULL
), irelative_rel_(NULL
),
3901 got_(got
), got_plt_(got_plt
), got_irelative_(got_irelative
),
3902 count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
3903 { this->init(layout
); }
3905 // Initialize the PLT section.
3907 init(Layout
* layout
);
3909 // Add an entry to the PLT.
3911 add_entry(Symbol_table
*, Layout
*, Symbol
* gsym
);
3913 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
3915 add_local_ifunc_entry(Symbol_table
* symtab
, Layout
*,
3916 Sized_relobj_file
<size
, big_endian
>* relobj
,
3917 unsigned int local_sym_index
);
3919 // Add the relocation for a PLT entry.
3921 add_relocation(Symbol_table
*, Layout
*, Symbol
* gsym
,
3922 unsigned int got_offset
);
3924 // Add the reserved TLSDESC_PLT entry to the PLT.
3926 reserve_tlsdesc_entry(unsigned int got_offset
)
3927 { this->tlsdesc_got_offset_
= got_offset
; }
3929 // Return true if a TLSDESC_PLT entry has been reserved.
3931 has_tlsdesc_entry() const
3932 { return this->tlsdesc_got_offset_
!= -1U; }
3934 // Return the GOT offset for the reserved TLSDESC_PLT entry.
3936 get_tlsdesc_got_offset() const
3937 { return this->tlsdesc_got_offset_
; }
3939 // Return the PLT offset of the reserved TLSDESC_PLT entry.
3941 get_tlsdesc_plt_offset() const
3943 return (this->first_plt_entry_offset() +
3944 (this->count_
+ this->irelative_count_
)
3945 * this->get_plt_entry_size());
3948 // Return the .rela.plt section data.
3951 { return this->rel_
; }
3953 // Return where the TLSDESC relocations should go.
3955 rela_tlsdesc(Layout
*);
3957 // Return where the IRELATIVE relocations should go in the PLT
3960 rela_irelative(Symbol_table
*, Layout
*);
3962 // Return whether we created a section for IRELATIVE relocations.
3964 has_irelative_section() const
3965 { return this->irelative_rel_
!= NULL
; }
3967 // Return the number of PLT entries.
3970 { return this->count_
+ this->irelative_count_
; }
3972 // Return the offset of the first non-reserved PLT entry.
3974 first_plt_entry_offset() const
3975 { return this->do_first_plt_entry_offset(); }
3977 // Return the size of a PLT entry.
3979 get_plt_entry_size() const
3980 { return this->do_get_plt_entry_size(); }
3982 // Return the reserved tlsdesc entry size.
3984 get_plt_tlsdesc_entry_size() const
3985 { return this->do_get_plt_tlsdesc_entry_size(); }
3987 // Return the PLT address to use for a global symbol.
3989 address_for_global(const Symbol
*);
3991 // Return the PLT address to use for a local symbol.
3993 address_for_local(const Relobj
*, unsigned int symndx
);
3996 // Fill in the first PLT entry.
3998 fill_first_plt_entry(unsigned char* pov
,
3999 Address got_address
,
4000 Address plt_address
)
4001 { this->do_fill_first_plt_entry(pov
, got_address
, plt_address
); }
4003 // Fill in a normal PLT entry.
4005 fill_plt_entry(unsigned char* pov
,
4006 Address got_address
,
4007 Address plt_address
,
4008 unsigned int got_offset
,
4009 unsigned int plt_offset
)
4011 this->do_fill_plt_entry(pov
, got_address
, plt_address
,
4012 got_offset
, plt_offset
);
4015 // Fill in the reserved TLSDESC PLT entry.
4017 fill_tlsdesc_entry(unsigned char* pov
,
4018 Address gotplt_address
,
4019 Address plt_address
,
4021 unsigned int tlsdesc_got_offset
,
4022 unsigned int plt_offset
)
4024 this->do_fill_tlsdesc_entry(pov
, gotplt_address
, plt_address
, got_base
,
4025 tlsdesc_got_offset
, plt_offset
);
4028 virtual unsigned int
4029 do_first_plt_entry_offset() const = 0;
4031 virtual unsigned int
4032 do_get_plt_entry_size() const = 0;
4034 virtual unsigned int
4035 do_get_plt_tlsdesc_entry_size() const = 0;
4038 do_fill_first_plt_entry(unsigned char* pov
,
4040 Address plt_addr
) = 0;
4043 do_fill_plt_entry(unsigned char* pov
,
4044 Address got_address
,
4045 Address plt_address
,
4046 unsigned int got_offset
,
4047 unsigned int plt_offset
) = 0;
4050 do_fill_tlsdesc_entry(unsigned char* pov
,
4051 Address gotplt_address
,
4052 Address plt_address
,
4054 unsigned int tlsdesc_got_offset
,
4055 unsigned int plt_offset
) = 0;
4058 do_adjust_output_section(Output_section
* os
);
4060 // Write to a map file.
4062 do_print_to_mapfile(Mapfile
* mapfile
) const
4063 { mapfile
->print_output_data(this, _("** PLT")); }
4066 // Set the final size.
4068 set_final_data_size();
4070 // Write out the PLT data.
4072 do_write(Output_file
*);
4074 // The reloc section.
4075 Reloc_section
* rel_
;
4077 // The TLSDESC relocs, if necessary. These must follow the regular
4079 Reloc_section
* tlsdesc_rel_
;
4081 // The IRELATIVE relocs, if necessary. These must follow the
4082 // regular PLT relocations.
4083 Reloc_section
* irelative_rel_
;
4085 // The .got section.
4086 Output_data_got_aarch64
<size
, big_endian
>* got_
;
4088 // The .got.plt section.
4089 Output_data_space
* got_plt_
;
4091 // The part of the .got.plt section used for IRELATIVE relocs.
4092 Output_data_space
* got_irelative_
;
4094 // The number of PLT entries.
4095 unsigned int count_
;
4097 // Number of PLT entries with R_AARCH64_IRELATIVE relocs. These
4098 // follow the regular PLT entries.
4099 unsigned int irelative_count_
;
4101 // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
4102 // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
4103 // indicates an offset is not allocated.
4104 unsigned int tlsdesc_got_offset_
;
4107 // Initialize the PLT section.
4109 template<int size
, bool big_endian
>
4111 Output_data_plt_aarch64
<size
, big_endian
>::init(Layout
* layout
)
4113 this->rel_
= new Reloc_section(false);
4114 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
4115 elfcpp::SHF_ALLOC
, this->rel_
,
4116 ORDER_DYNAMIC_PLT_RELOCS
, false);
4119 template<int size
, bool big_endian
>
4121 Output_data_plt_aarch64
<size
, big_endian
>::do_adjust_output_section(
4124 os
->set_entsize(this->get_plt_entry_size());
4127 // Add an entry to the PLT.
4129 template<int size
, bool big_endian
>
4131 Output_data_plt_aarch64
<size
, big_endian
>::add_entry(Symbol_table
* symtab
,
4132 Layout
* layout
, Symbol
* gsym
)
4134 gold_assert(!gsym
->has_plt_offset());
4136 unsigned int* pcount
;
4137 unsigned int plt_reserved
;
4138 Output_section_data_build
* got
;
4140 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4141 && gsym
->can_use_relative_reloc(false))
4143 pcount
= &this->irelative_count_
;
4145 got
= this->got_irelative_
;
4149 pcount
= &this->count_
;
4150 plt_reserved
= this->first_plt_entry_offset();
4151 got
= this->got_plt_
;
4154 gsym
->set_plt_offset((*pcount
) * this->get_plt_entry_size()
4159 section_offset_type got_offset
= got
->current_data_size();
4161 // Every PLT entry needs a GOT entry which points back to the PLT
4162 // entry (this will be changed by the dynamic linker, normally
4163 // lazily when the function is called).
4164 got
->set_current_data_size(got_offset
+ size
/ 8);
4166 // Every PLT entry needs a reloc.
4167 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
4169 // Note that we don't need to save the symbol. The contents of the
4170 // PLT are independent of which symbols are used. The symbols only
4171 // appear in the relocations.
4174 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
4177 template<int size
, bool big_endian
>
4179 Output_data_plt_aarch64
<size
, big_endian
>::add_local_ifunc_entry(
4180 Symbol_table
* symtab
,
4182 Sized_relobj_file
<size
, big_endian
>* relobj
,
4183 unsigned int local_sym_index
)
4185 unsigned int plt_offset
= this->irelative_count_
* this->get_plt_entry_size();
4186 ++this->irelative_count_
;
4188 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
4190 // Every PLT entry needs a GOT entry which points back to the PLT
4192 this->got_irelative_
->set_current_data_size(got_offset
+ size
/ 8);
4194 // Every PLT entry needs a reloc.
4195 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
4196 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
4197 elfcpp::R_AARCH64_IRELATIVE
,
4198 this->got_irelative_
, got_offset
, 0);
4203 // Add the relocation for a PLT entry.
4205 template<int size
, bool big_endian
>
4207 Output_data_plt_aarch64
<size
, big_endian
>::add_relocation(
4208 Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
, unsigned int got_offset
)
4210 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4211 && gsym
->can_use_relative_reloc(false))
4213 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
4214 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_AARCH64_IRELATIVE
,
4215 this->got_irelative_
, got_offset
, 0);
4219 gsym
->set_needs_dynsym_entry();
4220 this->rel_
->add_global(gsym
, elfcpp::R_AARCH64_JUMP_SLOT
, this->got_plt_
,
4225 // Return where the TLSDESC relocations should go, creating it if
4226 // necessary. These follow the JUMP_SLOT relocations.
4228 template<int size
, bool big_endian
>
4229 typename Output_data_plt_aarch64
<size
, big_endian
>::Reloc_section
*
4230 Output_data_plt_aarch64
<size
, big_endian
>::rela_tlsdesc(Layout
* layout
)
4232 if (this->tlsdesc_rel_
== NULL
)
4234 this->tlsdesc_rel_
= new Reloc_section(false);
4235 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
4236 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
4237 ORDER_DYNAMIC_PLT_RELOCS
, false);
4238 gold_assert(this->tlsdesc_rel_
->output_section()
4239 == this->rel_
->output_section());
4241 return this->tlsdesc_rel_
;
4244 // Return where the IRELATIVE relocations should go in the PLT. These
4245 // follow the JUMP_SLOT and the TLSDESC relocations.
4247 template<int size
, bool big_endian
>
4248 typename Output_data_plt_aarch64
<size
, big_endian
>::Reloc_section
*
4249 Output_data_plt_aarch64
<size
, big_endian
>::rela_irelative(Symbol_table
* symtab
,
4252 if (this->irelative_rel_
== NULL
)
4254 // Make sure we have a place for the TLSDESC relocations, in
4255 // case we see any later on.
4256 this->rela_tlsdesc(layout
);
4257 this->irelative_rel_
= new Reloc_section(false);
4258 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
4259 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
4260 ORDER_DYNAMIC_PLT_RELOCS
, false);
4261 gold_assert(this->irelative_rel_
->output_section()
4262 == this->rel_
->output_section());
4264 if (parameters
->doing_static_link())
4266 // A statically linked executable will only have a .rela.plt
4267 // section to hold R_AARCH64_IRELATIVE relocs for
4268 // STT_GNU_IFUNC symbols. The library will use these
4269 // symbols to locate the IRELATIVE relocs at program startup
4271 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
4272 Symbol_table::PREDEFINED
,
4273 this->irelative_rel_
, 0, 0,
4274 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
4275 elfcpp::STV_HIDDEN
, 0, false, true);
4276 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
4277 Symbol_table::PREDEFINED
,
4278 this->irelative_rel_
, 0, 0,
4279 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
4280 elfcpp::STV_HIDDEN
, 0, true, true);
4283 return this->irelative_rel_
;
4286 // Return the PLT address to use for a global symbol.
4288 template<int size
, bool big_endian
>
4290 Output_data_plt_aarch64
<size
, big_endian
>::address_for_global(
4293 uint64_t offset
= 0;
4294 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4295 && gsym
->can_use_relative_reloc(false))
4296 offset
= (this->first_plt_entry_offset() +
4297 this->count_
* this->get_plt_entry_size());
4298 return this->address() + offset
+ gsym
->plt_offset();
4301 // Return the PLT address to use for a local symbol. These are always
4302 // IRELATIVE relocs.
4304 template<int size
, bool big_endian
>
4306 Output_data_plt_aarch64
<size
, big_endian
>::address_for_local(
4307 const Relobj
* object
,
4310 return (this->address()
4311 + this->first_plt_entry_offset()
4312 + this->count_
* this->get_plt_entry_size()
4313 + object
->local_plt_offset(r_sym
));
4316 // Set the final size.
4318 template<int size
, bool big_endian
>
4320 Output_data_plt_aarch64
<size
, big_endian
>::set_final_data_size()
4322 unsigned int count
= this->count_
+ this->irelative_count_
;
4323 unsigned int extra_size
= 0;
4324 if (this->has_tlsdesc_entry())
4325 extra_size
+= this->get_plt_tlsdesc_entry_size();
4326 this->set_data_size(this->first_plt_entry_offset()
4327 + count
* this->get_plt_entry_size()
4331 template<int size
, bool big_endian
>
4332 class Output_data_plt_aarch64_standard
:
4333 public Output_data_plt_aarch64
<size
, big_endian
>
4336 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4337 Output_data_plt_aarch64_standard(
4339 Output_data_got_aarch64
<size
, big_endian
>* got
,
4340 Output_data_space
* got_plt
,
4341 Output_data_space
* got_irelative
)
4342 : Output_data_plt_aarch64
<size
, big_endian
>(layout
,
4349 // Return the offset of the first non-reserved PLT entry.
4350 virtual unsigned int
4351 do_first_plt_entry_offset() const
4352 { return this->first_plt_entry_size
; }
4354 // Return the size of a PLT entry
4355 virtual unsigned int
4356 do_get_plt_entry_size() const
4357 { return this->plt_entry_size
; }
4359 // Return the size of a tlsdesc entry
4360 virtual unsigned int
4361 do_get_plt_tlsdesc_entry_size() const
4362 { return this->plt_tlsdesc_entry_size
; }
4365 do_fill_first_plt_entry(unsigned char* pov
,
4366 Address got_address
,
4367 Address plt_address
);
4370 do_fill_plt_entry(unsigned char* pov
,
4371 Address got_address
,
4372 Address plt_address
,
4373 unsigned int got_offset
,
4374 unsigned int plt_offset
);
4377 do_fill_tlsdesc_entry(unsigned char* pov
,
4378 Address gotplt_address
,
4379 Address plt_address
,
4381 unsigned int tlsdesc_got_offset
,
4382 unsigned int plt_offset
);
4385 // The size of the first plt entry size.
4386 static const int first_plt_entry_size
= 32;
4387 // The size of the plt entry size.
4388 static const int plt_entry_size
= 16;
4389 // The size of the plt tlsdesc entry size.
4390 static const int plt_tlsdesc_entry_size
= 32;
4391 // Template for the first PLT entry.
4392 static const uint32_t first_plt_entry
[first_plt_entry_size
/ 4];
4393 // Template for subsequent PLT entries.
4394 static const uint32_t plt_entry
[plt_entry_size
/ 4];
4395 // The reserved TLSDESC entry in the PLT for an executable.
4396 static const uint32_t tlsdesc_plt_entry
[plt_tlsdesc_entry_size
/ 4];
4399 // The first entry in the PLT for an executable.
4403 Output_data_plt_aarch64_standard
<32, false>::
4404 first_plt_entry
[first_plt_entry_size
/ 4] =
4406 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4407 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4408 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4409 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4410 0xd61f0220, /* br x17 */
4411 0xd503201f, /* nop */
4412 0xd503201f, /* nop */
4413 0xd503201f, /* nop */
4419 Output_data_plt_aarch64_standard
<32, true>::
4420 first_plt_entry
[first_plt_entry_size
/ 4] =
4422 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4423 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4424 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4425 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4426 0xd61f0220, /* br x17 */
4427 0xd503201f, /* nop */
4428 0xd503201f, /* nop */
4429 0xd503201f, /* nop */
4435 Output_data_plt_aarch64_standard
<64, false>::
4436 first_plt_entry
[first_plt_entry_size
/ 4] =
4438 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4439 0x90000010, /* adrp x16, PLT_GOT+16 */
4440 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4441 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4442 0xd61f0220, /* br x17 */
4443 0xd503201f, /* nop */
4444 0xd503201f, /* nop */
4445 0xd503201f, /* nop */
4451 Output_data_plt_aarch64_standard
<64, true>::
4452 first_plt_entry
[first_plt_entry_size
/ 4] =
4454 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4455 0x90000010, /* adrp x16, PLT_GOT+16 */
4456 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4457 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4458 0xd61f0220, /* br x17 */
4459 0xd503201f, /* nop */
4460 0xd503201f, /* nop */
4461 0xd503201f, /* nop */
4467 Output_data_plt_aarch64_standard
<32, false>::
4468 plt_entry
[plt_entry_size
/ 4] =
4470 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4471 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4472 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4473 0xd61f0220, /* br x17. */
4479 Output_data_plt_aarch64_standard
<32, true>::
4480 plt_entry
[plt_entry_size
/ 4] =
4482 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4483 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4484 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4485 0xd61f0220, /* br x17. */
4491 Output_data_plt_aarch64_standard
<64, false>::
4492 plt_entry
[plt_entry_size
/ 4] =
4494 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4495 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4496 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4497 0xd61f0220, /* br x17. */
4503 Output_data_plt_aarch64_standard
<64, true>::
4504 plt_entry
[plt_entry_size
/ 4] =
4506 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4507 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4508 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4509 0xd61f0220, /* br x17. */
4513 template<int size
, bool big_endian
>
4515 Output_data_plt_aarch64_standard
<size
, big_endian
>::do_fill_first_plt_entry(
4517 Address got_address
,
4518 Address plt_address
)
4520 // PLT0 of the small PLT looks like this in ELF64 -
4521 // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
4522 // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
4523 // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
4525 // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
4526 // GOTPLT entry for this.
4528 // PLT0 will be slightly different in ELF32 due to different got entry
4530 memcpy(pov
, this->first_plt_entry
, this->first_plt_entry_size
);
4531 Address gotplt_2nd_ent
= got_address
+ (size
/ 8) * 2;
4533 // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
4534 // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
4535 // FIXME: This only works for 64bit
4536 AArch64_relocate_functions
<size
, big_endian
>::adrp(pov
+ 4,
4537 gotplt_2nd_ent
, plt_address
+ 4);
4539 // Fill in R_AARCH64_LDST8_LO12
4540 elfcpp::Swap
<32, big_endian
>::writeval(
4542 ((this->first_plt_entry
[2] & 0xffc003ff)
4543 | ((gotplt_2nd_ent
& 0xff8) << 7)));
4545 // Fill in R_AARCH64_ADD_ABS_LO12
4546 elfcpp::Swap
<32, big_endian
>::writeval(
4548 ((this->first_plt_entry
[3] & 0xffc003ff)
4549 | ((gotplt_2nd_ent
& 0xfff) << 10)));
4553 // Subsequent entries in the PLT for an executable.
4554 // FIXME: This only works for 64bit
4556 template<int size
, bool big_endian
>
4558 Output_data_plt_aarch64_standard
<size
, big_endian
>::do_fill_plt_entry(
4560 Address got_address
,
4561 Address plt_address
,
4562 unsigned int got_offset
,
4563 unsigned int plt_offset
)
4565 memcpy(pov
, this->plt_entry
, this->plt_entry_size
);
4567 Address gotplt_entry_address
= got_address
+ got_offset
;
4568 Address plt_entry_address
= plt_address
+ plt_offset
;
4570 // Fill in R_AARCH64_PCREL_ADR_HI21
4571 AArch64_relocate_functions
<size
, big_endian
>::adrp(
4573 gotplt_entry_address
,
4576 // Fill in R_AARCH64_LDST64_ABS_LO12
4577 elfcpp::Swap
<32, big_endian
>::writeval(
4579 ((this->plt_entry
[1] & 0xffc003ff)
4580 | ((gotplt_entry_address
& 0xff8) << 7)));
4582 // Fill in R_AARCH64_ADD_ABS_LO12
4583 elfcpp::Swap
<32, big_endian
>::writeval(
4585 ((this->plt_entry
[2] & 0xffc003ff)
4586 | ((gotplt_entry_address
& 0xfff) <<10)));
4593 Output_data_plt_aarch64_standard
<32, false>::
4594 tlsdesc_plt_entry
[plt_tlsdesc_entry_size
/ 4] =
4596 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4597 0x90000002, /* adrp x2, 0 */
4598 0x90000003, /* adrp x3, 0 */
4599 0xb9400042, /* ldr w2, [w2, #0] */
4600 0x11000063, /* add w3, w3, 0 */
4601 0xd61f0040, /* br x2 */
4602 0xd503201f, /* nop */
4603 0xd503201f, /* nop */
4608 Output_data_plt_aarch64_standard
<32, true>::
4609 tlsdesc_plt_entry
[plt_tlsdesc_entry_size
/ 4] =
4611 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4612 0x90000002, /* adrp x2, 0 */
4613 0x90000003, /* adrp x3, 0 */
4614 0xb9400042, /* ldr w2, [w2, #0] */
4615 0x11000063, /* add w3, w3, 0 */
4616 0xd61f0040, /* br x2 */
4617 0xd503201f, /* nop */
4618 0xd503201f, /* nop */
4623 Output_data_plt_aarch64_standard
<64, false>::
4624 tlsdesc_plt_entry
[plt_tlsdesc_entry_size
/ 4] =
4626 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4627 0x90000002, /* adrp x2, 0 */
4628 0x90000003, /* adrp x3, 0 */
4629 0xf9400042, /* ldr x2, [x2, #0] */
4630 0x91000063, /* add x3, x3, 0 */
4631 0xd61f0040, /* br x2 */
4632 0xd503201f, /* nop */
4633 0xd503201f, /* nop */
4638 Output_data_plt_aarch64_standard
<64, true>::
4639 tlsdesc_plt_entry
[plt_tlsdesc_entry_size
/ 4] =
4641 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4642 0x90000002, /* adrp x2, 0 */
4643 0x90000003, /* adrp x3, 0 */
4644 0xf9400042, /* ldr x2, [x2, #0] */
4645 0x91000063, /* add x3, x3, 0 */
4646 0xd61f0040, /* br x2 */
4647 0xd503201f, /* nop */
4648 0xd503201f, /* nop */
4651 template<int size
, bool big_endian
>
4653 Output_data_plt_aarch64_standard
<size
, big_endian
>::do_fill_tlsdesc_entry(
4655 Address gotplt_address
,
4656 Address plt_address
,
4658 unsigned int tlsdesc_got_offset
,
4659 unsigned int plt_offset
)
4661 memcpy(pov
, tlsdesc_plt_entry
, plt_tlsdesc_entry_size
);
4663 // move DT_TLSDESC_GOT address into x2
4664 // move .got.plt address into x3
4665 Address tlsdesc_got_entry
= got_base
+ tlsdesc_got_offset
;
4666 Address plt_entry_address
= plt_address
+ plt_offset
;
4668 // R_AARCH64_ADR_PREL_PG_HI21
4669 AArch64_relocate_functions
<size
, big_endian
>::adrp(
4672 plt_entry_address
+ 4);
4674 // R_AARCH64_ADR_PREL_PG_HI21
4675 AArch64_relocate_functions
<size
, big_endian
>::adrp(
4678 plt_entry_address
+ 8);
4680 // R_AARCH64_LDST64_ABS_LO12
4681 elfcpp::Swap
<32, big_endian
>::writeval(
4683 ((this->tlsdesc_plt_entry
[3] & 0xffc003ff)
4684 | ((tlsdesc_got_entry
& 0xff8) << 7)));
4686 // R_AARCH64_ADD_ABS_LO12
4687 elfcpp::Swap
<32, big_endian
>::writeval(
4689 ((this->tlsdesc_plt_entry
[4] & 0xffc003ff)
4690 | ((gotplt_address
& 0xfff) << 10)));
4693 // Write out the PLT. This uses the hand-coded instructions above,
4694 // and adjusts them as needed. This is specified by the AMD64 ABI.
4696 template<int size
, bool big_endian
>
4698 Output_data_plt_aarch64
<size
, big_endian
>::do_write(Output_file
* of
)
4700 const off_t offset
= this->offset();
4701 const section_size_type oview_size
=
4702 convert_to_section_size_type(this->data_size());
4703 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4705 const off_t got_file_offset
= this->got_plt_
->offset();
4706 gold_assert(got_file_offset
+ this->got_plt_
->data_size()
4707 == this->got_irelative_
->offset());
4709 const section_size_type got_size
=
4710 convert_to_section_size_type(this->got_plt_
->data_size()
4711 + this->got_irelative_
->data_size());
4712 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
4715 unsigned char* pov
= oview
;
4717 // The base address of the .plt section.
4718 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
4719 // The base address of the PLT portion of the .got section.
4720 typename
elfcpp::Elf_types
<size
>::Elf_Addr gotplt_address
4721 = this->got_plt_
->address();
4723 this->fill_first_plt_entry(pov
, gotplt_address
, plt_address
);
4724 pov
+= this->first_plt_entry_offset();
4726 // The first three entries in .got.plt are reserved.
4727 unsigned char* got_pov
= got_view
;
4728 memset(got_pov
, 0, size
/ 8 * AARCH64_GOTPLT_RESERVE_COUNT
);
4729 got_pov
+= (size
/ 8) * AARCH64_GOTPLT_RESERVE_COUNT
;
4731 unsigned int plt_offset
= this->first_plt_entry_offset();
4732 unsigned int got_offset
= (size
/ 8) * AARCH64_GOTPLT_RESERVE_COUNT
;
4733 const unsigned int count
= this->count_
+ this->irelative_count_
;
4734 for (unsigned int plt_index
= 0;
4737 pov
+= this->get_plt_entry_size(),
4738 got_pov
+= size
/ 8,
4739 plt_offset
+= this->get_plt_entry_size(),
4740 got_offset
+= size
/ 8)
4742 // Set and adjust the PLT entry itself.
4743 this->fill_plt_entry(pov
, gotplt_address
, plt_address
,
4744 got_offset
, plt_offset
);
4746 // Set the entry in the GOT, which points to plt0.
4747 elfcpp::Swap
<size
, big_endian
>::writeval(got_pov
, plt_address
);
4750 if (this->has_tlsdesc_entry())
4752 // Set and adjust the reserved TLSDESC PLT entry.
4753 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
4754 // The base address of the .base section.
4755 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
=
4756 this->got_
->address();
4757 this->fill_tlsdesc_entry(pov
, gotplt_address
, plt_address
, got_base
,
4758 tlsdesc_got_offset
, plt_offset
);
4759 pov
+= this->get_plt_tlsdesc_entry_size();
4762 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
4763 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
4765 of
->write_output_view(offset
, oview_size
, oview
);
4766 of
->write_output_view(got_file_offset
, got_size
, got_view
);
4769 // Telling how to update the immediate field of an instruction.
4770 struct AArch64_howto
4772 // The immediate field mask.
4773 elfcpp::Elf_Xword dst_mask
;
4775 // The offset to apply relocation immediate
4778 // The second part offset, if the immediate field has two parts.
4779 // -1 if the immediate field has only one part.
4783 static const AArch64_howto aarch64_howto
[AArch64_reloc_property::INST_NUM
] =
4785 {0, -1, -1}, // DATA
4786 {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
4787 {0xffffe0, 5, -1}, // LD [23:5]-imm19
4788 {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
4789 {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
4790 {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
4791 {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
4792 {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
4793 {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
4794 {0x3ffffff, 0, -1}, // B [25:0]-imm26
4795 {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
4798 // AArch64 relocate function class
4800 template<int size
, bool big_endian
>
4801 class AArch64_relocate_functions
4806 STATUS_OKAY
, // No error during relocation.
4807 STATUS_OVERFLOW
, // Relocation overflow.
4808 STATUS_BAD_RELOC
, // Relocation cannot be applied.
4811 typedef AArch64_relocate_functions
<size
, big_endian
> This
;
4812 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4813 typedef Relocate_info
<size
, big_endian
> The_relocate_info
;
4814 typedef AArch64_relobj
<size
, big_endian
> The_aarch64_relobj
;
4815 typedef Reloc_stub
<size
, big_endian
> The_reloc_stub
;
4816 typedef Stub_table
<size
, big_endian
> The_stub_table
;
4817 typedef elfcpp::Rela
<size
, big_endian
> The_rela
;
4818 typedef typename
elfcpp::Swap
<size
, big_endian
>::Valtype AArch64_valtype
;
4820 // Return the page address of the address.
4821 // Page(address) = address & ~0xFFF
4823 static inline AArch64_valtype
4824 Page(Address address
)
4826 return (address
& (~static_cast<Address
>(0xFFF)));
4830 // Update instruction (pointed by view) with selected bits (immed).
4831 // val = (val & ~dst_mask) | (immed << doffset)
4833 template<int valsize
>
4835 update_view(unsigned char* view
,
4836 AArch64_valtype immed
,
4837 elfcpp::Elf_Xword doffset
,
4838 elfcpp::Elf_Xword dst_mask
)
4840 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
4841 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
4842 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
4844 // Clear immediate fields.
4846 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
,
4847 static_cast<Valtype
>(val
| (immed
<< doffset
)));
4850 // Update two parts of an instruction (pointed by view) with selected
4851 // bits (immed1 and immed2).
4852 // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
4854 template<int valsize
>
4856 update_view_two_parts(
4857 unsigned char* view
,
4858 AArch64_valtype immed1
,
4859 AArch64_valtype immed2
,
4860 elfcpp::Elf_Xword doffset1
,
4861 elfcpp::Elf_Xword doffset2
,
4862 elfcpp::Elf_Xword dst_mask
)
4864 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
4865 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
4866 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
4868 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
,
4869 static_cast<Valtype
>(val
| (immed1
<< doffset1
) |
4870 (immed2
<< doffset2
)));
4873 // Update adr or adrp instruction with immed.
4874 // In adr and adrp: [30:29] immlo [23:5] immhi
4877 update_adr(unsigned char* view
, AArch64_valtype immed
)
4879 elfcpp::Elf_Xword dst_mask
= (0x3 << 29) | (0x7ffff << 5);
4880 This::template update_view_two_parts
<32>(
4883 (immed
& 0x1ffffc) >> 2,
4889 // Update movz/movn instruction with bits immed.
4890 // Set instruction to movz if is_movz is true, otherwise set instruction
4894 update_movnz(unsigned char* view
,
4895 AArch64_valtype immed
,
4898 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
4899 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
4900 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
4902 const elfcpp::Elf_Xword doffset
=
4903 aarch64_howto
[AArch64_reloc_property::INST_MOVW
].doffset
;
4904 const elfcpp::Elf_Xword dst_mask
=
4905 aarch64_howto
[AArch64_reloc_property::INST_MOVW
].dst_mask
;
4907 // Clear immediate fields and opc code.
4908 val
&= ~(dst_mask
| (0x3 << 29));
4910 // Set instruction to movz or movn.
4911 // movz: [30:29] is 10 movn: [30:29] is 00
4915 elfcpp::Swap
<32, big_endian
>::writeval(wv
,
4916 static_cast<Valtype
>(val
| (immed
<< doffset
)));
4919 // Update selected bits in text.
4921 template<int valsize
>
4922 static inline typename
This::Status
4923 reloc_common(unsigned char* view
, Address x
,
4924 const AArch64_reloc_property
* reloc_property
)
4926 // Select bits from X.
4927 Address immed
= reloc_property
->select_x_value(x
);
4930 const AArch64_reloc_property::Reloc_inst inst
=
4931 reloc_property
->reloc_inst();
4932 // If it is a data relocation or instruction has 2 parts of immediate
4933 // fields, you should not call pcrela_general.
4934 gold_assert(aarch64_howto
[inst
].doffset2
== -1 &&
4935 aarch64_howto
[inst
].doffset
!= -1);
4936 This::template update_view
<valsize
>(view
, immed
,
4937 aarch64_howto
[inst
].doffset
,
4938 aarch64_howto
[inst
].dst_mask
);
4940 // Do check overflow or alignment if needed.
4941 return (reloc_property
->checkup_x_value(x
)
4943 : This::STATUS_OVERFLOW
);
4948 // Construct a B insn. Note, although we group it here with other relocation
4949 // operation, there is actually no 'relocation' involved here.
4951 construct_b(unsigned char* view
, unsigned int branch_offset
)
4953 update_view_two_parts
<32>(view
, 0x05, (branch_offset
>> 2),
4957 // Do a simple rela relocation at unaligned addresses.
4959 template<int valsize
>
4960 static inline typename
This::Status
4961 rela_ua(unsigned char* view
,
4962 const Sized_relobj_file
<size
, big_endian
>* object
,
4963 const Symbol_value
<size
>* psymval
,
4964 AArch64_valtype addend
,
4965 const AArch64_reloc_property
* reloc_property
)
4967 typedef typename
elfcpp::Swap_unaligned
<valsize
, big_endian
>::Valtype
4969 typename
elfcpp::Elf_types
<size
>::Elf_Addr x
=
4970 psymval
->value(object
, addend
);
4971 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
,
4972 static_cast<Valtype
>(x
));
4973 return (reloc_property
->checkup_x_value(x
)
4975 : This::STATUS_OVERFLOW
);
4978 // Do a simple pc-relative relocation at unaligned addresses.
4980 template<int valsize
>
4981 static inline typename
This::Status
4982 pcrela_ua(unsigned char* view
,
4983 const Sized_relobj_file
<size
, big_endian
>* object
,
4984 const Symbol_value
<size
>* psymval
,
4985 AArch64_valtype addend
,
4987 const AArch64_reloc_property
* reloc_property
)
4989 typedef typename
elfcpp::Swap_unaligned
<valsize
, big_endian
>::Valtype
4991 Address x
= psymval
->value(object
, addend
) - address
;
4992 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
,
4993 static_cast<Valtype
>(x
));
4994 return (reloc_property
->checkup_x_value(x
)
4996 : This::STATUS_OVERFLOW
);
4999 // Do a simple rela relocation at aligned addresses.
5001 template<int valsize
>
5002 static inline typename
This::Status
5004 unsigned char* view
,
5005 const Sized_relobj_file
<size
, big_endian
>* object
,
5006 const Symbol_value
<size
>* psymval
,
5007 AArch64_valtype addend
,
5008 const AArch64_reloc_property
* reloc_property
)
5010 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
5011 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
5012 Address x
= psymval
->value(object
, addend
);
5013 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
,static_cast<Valtype
>(x
));
5014 return (reloc_property
->checkup_x_value(x
)
5016 : This::STATUS_OVERFLOW
);
5019 // Do relocate. Update selected bits in text.
5020 // new_val = (val & ~dst_mask) | (immed << doffset)
5022 template<int valsize
>
5023 static inline typename
This::Status
5024 rela_general(unsigned char* view
,
5025 const Sized_relobj_file
<size
, big_endian
>* object
,
5026 const Symbol_value
<size
>* psymval
,
5027 AArch64_valtype addend
,
5028 const AArch64_reloc_property
* reloc_property
)
5030 // Calculate relocation.
5031 Address x
= psymval
->value(object
, addend
);
5032 return This::template reloc_common
<valsize
>(view
, x
, reloc_property
);
5035 // Do relocate. Update selected bits in text.
5036 // new val = (val & ~dst_mask) | (immed << doffset)
5038 template<int valsize
>
5039 static inline typename
This::Status
5041 unsigned char* view
,
5043 AArch64_valtype addend
,
5044 const AArch64_reloc_property
* reloc_property
)
5046 // Calculate relocation.
5047 Address x
= s
+ addend
;
5048 return This::template reloc_common
<valsize
>(view
, x
, reloc_property
);
5051 // Do address relative relocate. Update selected bits in text.
5052 // new val = (val & ~dst_mask) | (immed << doffset)
5054 template<int valsize
>
5055 static inline typename
This::Status
5057 unsigned char* view
,
5058 const Sized_relobj_file
<size
, big_endian
>* object
,
5059 const Symbol_value
<size
>* psymval
,
5060 AArch64_valtype addend
,
5062 const AArch64_reloc_property
* reloc_property
)
5064 // Calculate relocation.
5065 Address x
= psymval
->value(object
, addend
) - address
;
5066 return This::template reloc_common
<valsize
>(view
, x
, reloc_property
);
5070 // Calculate (S + A) - address, update adr instruction.
5072 static inline typename
This::Status
5073 adr(unsigned char* view
,
5074 const Sized_relobj_file
<size
, big_endian
>* object
,
5075 const Symbol_value
<size
>* psymval
,
5078 const AArch64_reloc_property
* /* reloc_property */)
5080 AArch64_valtype x
= psymval
->value(object
, addend
) - address
;
5081 // Pick bits [20:0] of X.
5082 AArch64_valtype immed
= x
& 0x1fffff;
5083 update_adr(view
, immed
);
5084 // Check -2^20 <= X < 2^20
5085 return (size
== 64 && Bits
<21>::has_overflow((x
))
5086 ? This::STATUS_OVERFLOW
5087 : This::STATUS_OKAY
);
5090 // Calculate PG(S+A) - PG(address), update adrp instruction.
5091 // R_AARCH64_ADR_PREL_PG_HI21
5093 static inline typename
This::Status
5095 unsigned char* view
,
5099 AArch64_valtype x
= This::Page(sa
) - This::Page(address
);
5100 // Pick [32:12] of X.
5101 AArch64_valtype immed
= (x
>> 12) & 0x1fffff;
5102 update_adr(view
, immed
);
5103 // Check -2^32 <= X < 2^32
5104 return (size
== 64 && Bits
<33>::has_overflow((x
))
5105 ? This::STATUS_OVERFLOW
5106 : This::STATUS_OKAY
);
5109 // Calculate PG(S+A) - PG(address), update adrp instruction.
5110 // R_AARCH64_ADR_PREL_PG_HI21
5112 static inline typename
This::Status
5113 adrp(unsigned char* view
,
5114 const Sized_relobj_file
<size
, big_endian
>* object
,
5115 const Symbol_value
<size
>* psymval
,
5118 const AArch64_reloc_property
* reloc_property
)
5120 Address sa
= psymval
->value(object
, addend
);
5121 AArch64_valtype x
= This::Page(sa
) - This::Page(address
);
5122 // Pick [32:12] of X.
5123 AArch64_valtype immed
= (x
>> 12) & 0x1fffff;
5124 update_adr(view
, immed
);
5125 return (reloc_property
->checkup_x_value(x
)
5127 : This::STATUS_OVERFLOW
);
5130 // Update mov[n/z] instruction. Check overflow if needed.
5131 // If X >=0, set the instruction to movz and its immediate value to the
5133 // If X < 0, set the instruction to movn and its immediate value to
5134 // NOT (selected bits of).
5136 static inline typename
This::Status
5137 movnz(unsigned char* view
,
5139 const AArch64_reloc_property
* reloc_property
)
5141 // Select bits from X.
5144 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword SignedW
;
5145 if (static_cast<SignedW
>(x
) >= 0)
5147 immed
= reloc_property
->select_x_value(x
);
5152 immed
= reloc_property
->select_x_value(~x
);;
5156 // Update movnz instruction.
5157 update_movnz(view
, immed
, is_movz
);
5159 // Do check overflow or alignment if needed.
5160 return (reloc_property
->checkup_x_value(x
)
5162 : This::STATUS_OVERFLOW
);
5166 maybe_apply_stub(unsigned int,
5167 const The_relocate_info
*,
5171 const Sized_symbol
<size
>*,
5172 const Symbol_value
<size
>*,
5173 const Sized_relobj_file
<size
, big_endian
>*,
5176 }; // End of AArch64_relocate_functions
5179 // For a certain relocation type (usually jump/branch), test to see if the
5180 // destination needs a stub to fulfil. If so, re-route the destination of the
5181 // original instruction to the stub, note, at this time, the stub has already
5184 template<int size
, bool big_endian
>
5186 AArch64_relocate_functions
<size
, big_endian
>::
5187 maybe_apply_stub(unsigned int r_type
,
5188 const The_relocate_info
* relinfo
,
5189 const The_rela
& rela
,
5190 unsigned char* view
,
5192 const Sized_symbol
<size
>* gsym
,
5193 const Symbol_value
<size
>* psymval
,
5194 const Sized_relobj_file
<size
, big_endian
>* object
,
5195 section_size_type current_group_size
)
5197 if (parameters
->options().relocatable())
5200 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
= rela
.get_r_addend();
5201 Address branch_target
= psymval
->value(object
, 0) + addend
;
5203 The_reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
);
5204 if (stub_type
== ST_NONE
)
5207 const The_aarch64_relobj
* aarch64_relobj
=
5208 static_cast<const The_aarch64_relobj
*>(object
);
5209 The_stub_table
* stub_table
= aarch64_relobj
->stub_table(relinfo
->data_shndx
);
5210 gold_assert(stub_table
!= NULL
);
5212 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5213 typename
The_reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
5214 The_reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
5215 gold_assert(stub
!= NULL
);
5217 Address new_branch_target
= stub_table
->address() + stub
->offset();
5218 typename
elfcpp::Swap
<size
, big_endian
>::Valtype branch_offset
=
5219 new_branch_target
- address
;
5220 const AArch64_reloc_property
* arp
=
5221 aarch64_reloc_property_table
->get_reloc_property(r_type
);
5222 gold_assert(arp
!= NULL
);
5223 typename
This::Status status
= This::template
5224 rela_general
<32>(view
, branch_offset
, 0, arp
);
5225 if (status
!= This::STATUS_OKAY
)
5226 gold_error(_("Stub is too far away, try a smaller value "
5227 "for '--stub-group-size'. The current value is 0x%lx."),
5228 static_cast<unsigned long>(current_group_size
));
5233 // Group input sections for stub generation.
5235 // We group input sections in an output section so that the total size,
5236 // including any padding space due to alignment is smaller than GROUP_SIZE
5237 // unless the only input section in group is bigger than GROUP_SIZE already.
5238 // Then an ARM stub table is created to follow the last input section
5239 // in group. For each group an ARM stub table is created an is placed
5240 // after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
5241 // extend the group after the stub table.
5243 template<int size
, bool big_endian
>
5245 Target_aarch64
<size
, big_endian
>::group_sections(
5247 section_size_type group_size
,
5248 bool stubs_always_after_branch
,
5251 // Group input sections and insert stub table
5252 Layout::Section_list section_list
;
5253 layout
->get_executable_sections(§ion_list
);
5254 for (Layout::Section_list::const_iterator p
= section_list
.begin();
5255 p
!= section_list
.end();
5258 AArch64_output_section
<size
, big_endian
>* output_section
=
5259 static_cast<AArch64_output_section
<size
, big_endian
>*>(*p
);
5260 output_section
->group_sections(group_size
, stubs_always_after_branch
,
5266 // Find the AArch64_input_section object corresponding to the SHNDX-th input
5267 // section of RELOBJ.
5269 template<int size
, bool big_endian
>
5270 AArch64_input_section
<size
, big_endian
>*
5271 Target_aarch64
<size
, big_endian
>::find_aarch64_input_section(
5272 Relobj
* relobj
, unsigned int shndx
) const
5274 Section_id
sid(relobj
, shndx
);
5275 typename
AArch64_input_section_map::const_iterator p
=
5276 this->aarch64_input_section_map_
.find(sid
);
5277 return (p
!= this->aarch64_input_section_map_
.end()) ? p
->second
: NULL
;
5281 // Make a new AArch64_input_section object.
5283 template<int size
, bool big_endian
>
5284 AArch64_input_section
<size
, big_endian
>*
5285 Target_aarch64
<size
, big_endian
>::new_aarch64_input_section(
5286 Relobj
* relobj
, unsigned int shndx
)
5288 Section_id
sid(relobj
, shndx
);
5290 AArch64_input_section
<size
, big_endian
>* input_section
=
5291 new AArch64_input_section
<size
, big_endian
>(relobj
, shndx
);
5292 input_section
->init();
5294 // Register new AArch64_input_section in map for look-up.
5295 std::pair
<typename
AArch64_input_section_map::iterator
,bool> ins
=
5296 this->aarch64_input_section_map_
.insert(
5297 std::make_pair(sid
, input_section
));
5299 // Make sure that it we have not created another AArch64_input_section
5300 // for this input section already.
5301 gold_assert(ins
.second
);
5303 return input_section
;
5307 // Relaxation hook. This is where we do stub generation.
5309 template<int size
, bool big_endian
>
5311 Target_aarch64
<size
, big_endian
>::do_relax(
5313 const Input_objects
* input_objects
,
5314 Symbol_table
* symtab
,
5318 gold_assert(!parameters
->options().relocatable());
5321 // We don't handle negative stub_group_size right now.
5322 this->stub_group_size_
= abs(parameters
->options().stub_group_size());
5323 if (this->stub_group_size_
== 1)
5325 // Leave room for 4096 4-byte stub entries. If we exceed that, then we
5326 // will fail to link. The user will have to relink with an explicit
5327 // group size option.
5328 this->stub_group_size_
= The_reloc_stub::MAX_BRANCH_OFFSET
-
5331 group_sections(layout
, this->stub_group_size_
, true, task
);
5335 // If this is not the first pass, addresses and file offsets have
5336 // been reset at this point, set them here.
5337 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
5338 sp
!= this->stub_tables_
.end(); ++sp
)
5340 The_stub_table
* stt
= *sp
;
5341 The_aarch64_input_section
* owner
= stt
->owner();
5342 off_t off
= align_address(owner
->original_size(),
5344 stt
->set_address_and_file_offset(owner
->address() + off
,
5345 owner
->offset() + off
);
5349 // Scan relocs for relocation stubs
5350 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
5351 op
!= input_objects
->relobj_end();
5354 The_aarch64_relobj
* aarch64_relobj
=
5355 static_cast<The_aarch64_relobj
*>(*op
);
5356 // Lock the object so we can read from it. This is only called
5357 // single-threaded from Layout::finalize, so it is OK to lock.
5358 Task_lock_obj
<Object
> tl(task
, aarch64_relobj
);
5359 aarch64_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
5362 bool any_stub_table_changed
= false;
5363 for (Stub_table_iterator siter
= this->stub_tables_
.begin();
5364 siter
!= this->stub_tables_
.end() && !any_stub_table_changed
; ++siter
)
5366 The_stub_table
* stub_table
= *siter
;
5367 if (stub_table
->update_data_size_changed_p())
5369 The_aarch64_input_section
* owner
= stub_table
->owner();
5370 uint64_t address
= owner
->address();
5371 off_t offset
= owner
->offset();
5372 owner
->reset_address_and_file_offset();
5373 owner
->set_address_and_file_offset(address
, offset
);
5375 any_stub_table_changed
= true;
5379 // Do not continue relaxation.
5380 bool continue_relaxation
= any_stub_table_changed
;
5381 if (!continue_relaxation
)
5382 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
5383 (sp
!= this->stub_tables_
.end());
5385 (*sp
)->finalize_stubs();
5387 return continue_relaxation
;
5391 // Make a new Stub_table.
5393 template<int size
, bool big_endian
>
5394 Stub_table
<size
, big_endian
>*
5395 Target_aarch64
<size
, big_endian
>::new_stub_table(
5396 AArch64_input_section
<size
, big_endian
>* owner
)
5398 Stub_table
<size
, big_endian
>* stub_table
=
5399 new Stub_table
<size
, big_endian
>(owner
);
5400 stub_table
->set_address(align_address(
5401 owner
->address() + owner
->data_size(), 8));
5402 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
5403 stub_table
->finalize_data_size();
5405 this->stub_tables_
.push_back(stub_table
);
5411 template<int size
, bool big_endian
>
5413 Target_aarch64
<size
, big_endian
>::do_reloc_addend(
5414 void* arg
, unsigned int r_type
, uint64_t) const
5416 gold_assert(r_type
== elfcpp::R_AARCH64_TLSDESC
);
5417 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
5418 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
5419 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
5420 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
5421 gold_assert(psymval
->is_tls_symbol());
5422 // The value of a TLS symbol is the offset in the TLS segment.
5423 return psymval
->value(ti
.object
, 0);
5426 // Return the number of entries in the PLT.
5428 template<int size
, bool big_endian
>
5430 Target_aarch64
<size
, big_endian
>::plt_entry_count() const
5432 if (this->plt_
== NULL
)
5434 return this->plt_
->entry_count();
5437 // Return the offset of the first non-reserved PLT entry.
5439 template<int size
, bool big_endian
>
5441 Target_aarch64
<size
, big_endian
>::first_plt_entry_offset() const
5443 return this->plt_
->first_plt_entry_offset();
5446 // Return the size of each PLT entry.
5448 template<int size
, bool big_endian
>
5450 Target_aarch64
<size
, big_endian
>::plt_entry_size() const
5452 return this->plt_
->get_plt_entry_size();
5455 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
5457 template<int size
, bool big_endian
>
5459 Target_aarch64
<size
, big_endian
>::define_tls_base_symbol(
5460 Symbol_table
* symtab
, Layout
* layout
)
5462 if (this->tls_base_symbol_defined_
)
5465 Output_segment
* tls_segment
= layout
->tls_segment();
5466 if (tls_segment
!= NULL
)
5468 // _TLS_MODULE_BASE_ always points to the beginning of tls segment.
5469 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
5470 Symbol_table::PREDEFINED
,
5474 elfcpp::STV_HIDDEN
, 0,
5475 Symbol::SEGMENT_START
,
5478 this->tls_base_symbol_defined_
= true;
5481 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
5483 template<int size
, bool big_endian
>
5485 Target_aarch64
<size
, big_endian
>::reserve_tlsdesc_entries(
5486 Symbol_table
* symtab
, Layout
* layout
)
5488 if (this->plt_
== NULL
)
5489 this->make_plt_section(symtab
, layout
);
5491 if (!this->plt_
->has_tlsdesc_entry())
5493 // Allocate the TLSDESC_GOT entry.
5494 Output_data_got_aarch64
<size
, big_endian
>* got
=
5495 this->got_section(symtab
, layout
);
5496 unsigned int got_offset
= got
->add_constant(0);
5498 // Allocate the TLSDESC_PLT entry.
5499 this->plt_
->reserve_tlsdesc_entry(got_offset
);
5503 // Create a GOT entry for the TLS module index.
5505 template<int size
, bool big_endian
>
5507 Target_aarch64
<size
, big_endian
>::got_mod_index_entry(
5508 Symbol_table
* symtab
, Layout
* layout
,
5509 Sized_relobj_file
<size
, big_endian
>* object
)
5511 if (this->got_mod_index_offset_
== -1U)
5513 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
5514 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
5515 Output_data_got_aarch64
<size
, big_endian
>* got
=
5516 this->got_section(symtab
, layout
);
5517 unsigned int got_offset
= got
->add_constant(0);
5518 rela_dyn
->add_local(object
, 0, elfcpp::R_AARCH64_TLS_DTPMOD64
, got
,
5520 got
->add_constant(0);
5521 this->got_mod_index_offset_
= got_offset
;
5523 return this->got_mod_index_offset_
;
5526 // Optimize the TLS relocation type based on what we know about the
5527 // symbol. IS_FINAL is true if the final address of this symbol is
5528 // known at link time.
5530 template<int size
, bool big_endian
>
5531 tls::Tls_optimization
5532 Target_aarch64
<size
, big_endian
>::optimize_tls_reloc(bool is_final
,
5535 // If we are generating a shared library, then we can't do anything
5537 if (parameters
->options().shared())
5538 return tls::TLSOPT_NONE
;
5542 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21
:
5543 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
:
5544 case elfcpp::R_AARCH64_TLSDESC_LD_PREL19
:
5545 case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21
:
5546 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
5547 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
5548 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
5549 case elfcpp::R_AARCH64_TLSDESC_OFF_G1
:
5550 case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC
:
5551 case elfcpp::R_AARCH64_TLSDESC_LDR
:
5552 case elfcpp::R_AARCH64_TLSDESC_ADD
:
5553 case elfcpp::R_AARCH64_TLSDESC_CALL
:
5554 // These are General-Dynamic which permits fully general TLS
5555 // access. Since we know that we are generating an executable,
5556 // we can convert this to Initial-Exec. If we also know that
5557 // this is a local symbol, we can further switch to Local-Exec.
5559 return tls::TLSOPT_TO_LE
;
5560 return tls::TLSOPT_TO_IE
;
5562 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21
:
5563 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
:
5564 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1
:
5565 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
5566 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12
:
5567 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
:
5568 // These are Local-Dynamic, which refer to local symbols in the
5569 // dynamic TLS block. Since we know that we generating an
5570 // executable, we can switch to Local-Exec.
5571 return tls::TLSOPT_TO_LE
;
5573 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1
:
5574 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC
:
5575 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
5576 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
5577 case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19
:
5578 // These are Initial-Exec relocs which get the thread offset
5579 // from the GOT. If we know that we are linking against the
5580 // local symbol, we can switch to Local-Exec, which links the
5581 // thread offset into the instruction.
5583 return tls::TLSOPT_TO_LE
;
5584 return tls::TLSOPT_NONE
;
5586 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2
:
5587 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1
:
5588 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
:
5589 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0
:
5590 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
:
5591 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
5592 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
5593 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
5594 // When we already have Local-Exec, there is nothing further we
5596 return tls::TLSOPT_NONE
;
5603 // Returns true if this relocation type could be that of a function pointer.
5605 template<int size
, bool big_endian
>
5607 Target_aarch64
<size
, big_endian
>::Scan::possible_function_pointer_reloc(
5608 unsigned int r_type
)
5612 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21
:
5613 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC
:
5614 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC
:
5615 case elfcpp::R_AARCH64_ADR_GOT_PAGE
:
5616 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC
:
5624 // For safe ICF, scan a relocation for a local symbol to check if it
5625 // corresponds to a function pointer being taken. In that case mark
5626 // the function whose pointer was taken as not foldable.
5628 template<int size
, bool big_endian
>
5630 Target_aarch64
<size
, big_endian
>::Scan::local_reloc_may_be_function_pointer(
5633 Target_aarch64
<size
, big_endian
>* ,
5634 Sized_relobj_file
<size
, big_endian
>* ,
5637 const elfcpp::Rela
<size
, big_endian
>& ,
5638 unsigned int r_type
,
5639 const elfcpp::Sym
<size
, big_endian
>&)
5641 // When building a shared library, do not fold any local symbols.
5642 return (parameters
->options().shared()
5643 || possible_function_pointer_reloc(r_type
));
5646 // For safe ICF, scan a relocation for a global symbol to check if it
5647 // corresponds to a function pointer being taken. In that case mark
5648 // the function whose pointer was taken as not foldable.
5650 template<int size
, bool big_endian
>
5652 Target_aarch64
<size
, big_endian
>::Scan::global_reloc_may_be_function_pointer(
5655 Target_aarch64
<size
, big_endian
>* ,
5656 Sized_relobj_file
<size
, big_endian
>* ,
5659 const elfcpp::Rela
<size
, big_endian
>& ,
5660 unsigned int r_type
,
5663 // When building a shared library, do not fold symbols whose visibility
5664 // is hidden, internal or protected.
5665 return ((parameters
->options().shared()
5666 && (gsym
->visibility() == elfcpp::STV_INTERNAL
5667 || gsym
->visibility() == elfcpp::STV_PROTECTED
5668 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
5669 || possible_function_pointer_reloc(r_type
));
5672 // Report an unsupported relocation against a local symbol.
5674 template<int size
, bool big_endian
>
5676 Target_aarch64
<size
, big_endian
>::Scan::unsupported_reloc_local(
5677 Sized_relobj_file
<size
, big_endian
>* object
,
5678 unsigned int r_type
)
5680 gold_error(_("%s: unsupported reloc %u against local symbol"),
5681 object
->name().c_str(), r_type
);
5684 // We are about to emit a dynamic relocation of type R_TYPE. If the
5685 // dynamic linker does not support it, issue an error.
5687 template<int size
, bool big_endian
>
5689 Target_aarch64
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
5690 unsigned int r_type
)
5692 gold_assert(r_type
!= elfcpp::R_AARCH64_NONE
);
5696 // These are the relocation types supported by glibc for AARCH64.
5697 case elfcpp::R_AARCH64_NONE
:
5698 case elfcpp::R_AARCH64_COPY
:
5699 case elfcpp::R_AARCH64_GLOB_DAT
:
5700 case elfcpp::R_AARCH64_JUMP_SLOT
:
5701 case elfcpp::R_AARCH64_RELATIVE
:
5702 case elfcpp::R_AARCH64_TLS_DTPREL64
:
5703 case elfcpp::R_AARCH64_TLS_DTPMOD64
:
5704 case elfcpp::R_AARCH64_TLS_TPREL64
:
5705 case elfcpp::R_AARCH64_TLSDESC
:
5706 case elfcpp::R_AARCH64_IRELATIVE
:
5707 case elfcpp::R_AARCH64_ABS32
:
5708 case elfcpp::R_AARCH64_ABS64
:
5715 // This prevents us from issuing more than one error per reloc
5716 // section. But we can still wind up issuing more than one
5717 // error per object file.
5718 if (this->issued_non_pic_error_
)
5720 gold_assert(parameters
->options().output_is_position_independent());
5721 object
->error(_("requires unsupported dynamic reloc; "
5722 "recompile with -fPIC"));
5723 this->issued_non_pic_error_
= true;
5727 // Return whether we need to make a PLT entry for a relocation of the
5728 // given type against a STT_GNU_IFUNC symbol.
5730 template<int size
, bool big_endian
>
5732 Target_aarch64
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
5733 Sized_relobj_file
<size
, big_endian
>* object
,
5734 unsigned int r_type
)
5736 const AArch64_reloc_property
* arp
=
5737 aarch64_reloc_property_table
->get_reloc_property(r_type
);
5738 gold_assert(arp
!= NULL
);
5740 int flags
= arp
->reference_flags();
5741 if (flags
& Symbol::TLS_REF
)
5743 gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
5744 object
->name().c_str(), arp
->name().c_str());
5750 // Scan a relocation for a local symbol.
5752 template<int size
, bool big_endian
>
5754 Target_aarch64
<size
, big_endian
>::Scan::local(
5755 Symbol_table
* symtab
,
5757 Target_aarch64
<size
, big_endian
>* target
,
5758 Sized_relobj_file
<size
, big_endian
>* object
,
5759 unsigned int data_shndx
,
5760 Output_section
* output_section
,
5761 const elfcpp::Rela
<size
, big_endian
>& rela
,
5762 unsigned int r_type
,
5763 const elfcpp::Sym
<size
, big_endian
>& lsym
,
5769 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>
5771 Output_data_got_aarch64
<size
, big_endian
>* got
=
5772 target
->got_section(symtab
, layout
);
5773 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5775 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5776 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
5777 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
5778 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
5782 case elfcpp::R_AARCH64_ABS32
:
5783 case elfcpp::R_AARCH64_ABS16
:
5784 if (parameters
->options().output_is_position_independent())
5786 gold_error(_("%s: unsupported reloc %u in pos independent link."),
5787 object
->name().c_str(), r_type
);
5791 case elfcpp::R_AARCH64_ABS64
:
5792 // If building a shared library or pie, we need to mark this as a dynmic
5793 // reloction, so that the dynamic loader can relocate it.
5794 if (parameters
->options().output_is_position_independent())
5796 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5797 rela_dyn
->add_local_relative(object
, r_sym
,
5798 elfcpp::R_AARCH64_RELATIVE
,
5801 rela
.get_r_offset(),
5802 rela
.get_r_addend(),
5807 case elfcpp::R_AARCH64_PREL64
:
5808 case elfcpp::R_AARCH64_PREL32
:
5809 case elfcpp::R_AARCH64_PREL16
:
5812 case elfcpp::R_AARCH64_LD_PREL_LO19
: // 273
5813 case elfcpp::R_AARCH64_ADR_PREL_LO21
: // 274
5814 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21
: // 275
5815 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC
: // 276
5816 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC
: // 277
5817 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC
: // 278
5818 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC
: // 284
5819 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC
: // 285
5820 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC
: // 286
5821 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC
: // 299
5824 // Control flow, pc-relative. We don't need to do anything for a relative
5825 // addressing relocation against a local symbol if it does not reference
5827 case elfcpp::R_AARCH64_TSTBR14
:
5828 case elfcpp::R_AARCH64_CONDBR19
:
5829 case elfcpp::R_AARCH64_JUMP26
:
5830 case elfcpp::R_AARCH64_CALL26
:
5833 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
5834 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
5836 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
5837 optimize_tls_reloc(!parameters
->options().shared(), r_type
);
5838 if (tlsopt
== tls::TLSOPT_TO_LE
)
5841 layout
->set_has_static_tls();
5842 // Create a GOT entry for the tp-relative offset.
5843 if (!parameters
->doing_static_link())
5845 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
5846 target
->rela_dyn_section(layout
),
5847 elfcpp::R_AARCH64_TLS_TPREL64
);
5849 else if (!object
->local_has_got_offset(r_sym
,
5850 GOT_TYPE_TLS_OFFSET
))
5852 got
->add_local(object
, r_sym
, GOT_TYPE_TLS_OFFSET
);
5853 unsigned int got_offset
=
5854 object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
);
5855 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
5856 gold_assert(addend
== 0);
5857 got
->add_static_reloc(got_offset
, elfcpp::R_AARCH64_TLS_TPREL64
,
5863 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21
:
5864 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
:
5866 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
5867 optimize_tls_reloc(!parameters
->options().shared(), r_type
);
5868 if (tlsopt
== tls::TLSOPT_TO_LE
)
5870 layout
->set_has_static_tls();
5873 gold_assert(tlsopt
== tls::TLSOPT_NONE
);
5875 got
->add_local_pair_with_rel(object
,r_sym
, data_shndx
,
5877 target
->rela_dyn_section(layout
),
5878 elfcpp::R_AARCH64_TLS_DTPMOD64
);
5882 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2
:
5883 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1
:
5884 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
:
5885 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0
:
5886 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
:
5887 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
5888 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
5889 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
5891 layout
->set_has_static_tls();
5892 bool output_is_shared
= parameters
->options().shared();
5893 if (output_is_shared
)
5894 gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
5895 object
->name().c_str(), r_type
);
5899 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21
:
5900 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
:
5902 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
5903 optimize_tls_reloc(!parameters
->options().shared(), r_type
);
5904 if (tlsopt
== tls::TLSOPT_NONE
)
5906 // Create a GOT entry for the module index.
5907 target
->got_mod_index_entry(symtab
, layout
, object
);
5909 else if (tlsopt
!= tls::TLSOPT_TO_LE
)
5910 unsupported_reloc_local(object
, r_type
);
5914 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1
:
5915 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
5916 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12
:
5917 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
:
5920 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
5921 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
5922 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
5924 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
5925 optimize_tls_reloc(!parameters
->options().shared(), r_type
);
5926 target
->define_tls_base_symbol(symtab
, layout
);
5927 if (tlsopt
== tls::TLSOPT_NONE
)
5929 // Create reserved PLT and GOT entries for the resolver.
5930 target
->reserve_tlsdesc_entries(symtab
, layout
);
5932 // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
5933 // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
5934 // entry needs to be in an area in .got.plt, not .got. Call
5935 // got_section to make sure the section has been created.
5936 target
->got_section(symtab
, layout
);
5937 Output_data_got
<size
, big_endian
>* got
=
5938 target
->got_tlsdesc_section();
5939 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5940 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
5942 unsigned int got_offset
= got
->add_constant(0);
5943 got
->add_constant(0);
5944 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
5946 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
5947 // We store the arguments we need in a vector, and use
5948 // the index into the vector as the parameter to pass
5949 // to the target specific routines.
5950 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
5951 void* arg
= reinterpret_cast<void*>(intarg
);
5952 rt
->add_target_specific(elfcpp::R_AARCH64_TLSDESC
, arg
,
5953 got
, got_offset
, 0);
5956 else if (tlsopt
!= tls::TLSOPT_TO_LE
)
5957 unsupported_reloc_local(object
, r_type
);
5961 case elfcpp::R_AARCH64_TLSDESC_CALL
:
5965 unsupported_reloc_local(object
, r_type
);
5970 // Report an unsupported relocation against a global symbol.
5972 template<int size
, bool big_endian
>
5974 Target_aarch64
<size
, big_endian
>::Scan::unsupported_reloc_global(
5975 Sized_relobj_file
<size
, big_endian
>* object
,
5976 unsigned int r_type
,
5979 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5980 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
5983 template<int size
, bool big_endian
>
5985 Target_aarch64
<size
, big_endian
>::Scan::global(
5986 Symbol_table
* symtab
,
5988 Target_aarch64
<size
, big_endian
>* target
,
5989 Sized_relobj_file
<size
, big_endian
> * object
,
5990 unsigned int data_shndx
,
5991 Output_section
* output_section
,
5992 const elfcpp::Rela
<size
, big_endian
>& rela
,
5993 unsigned int r_type
,
5996 // A STT_GNU_IFUNC symbol may require a PLT entry.
5997 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
5998 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
5999 target
->make_plt_entry(symtab
, layout
, gsym
);
6001 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>
6003 const AArch64_reloc_property
* arp
=
6004 aarch64_reloc_property_table
->get_reloc_property(r_type
);
6005 gold_assert(arp
!= NULL
);
6009 case elfcpp::R_AARCH64_ABS16
:
6010 case elfcpp::R_AARCH64_ABS32
:
6011 case elfcpp::R_AARCH64_ABS64
:
6013 // Make a PLT entry if necessary.
6014 if (gsym
->needs_plt_entry())
6016 target
->make_plt_entry(symtab
, layout
, gsym
);
6017 // Since this is not a PC-relative relocation, we may be
6018 // taking the address of a function. In that case we need to
6019 // set the entry in the dynamic symbol table to the address of
6021 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
6022 gsym
->set_needs_dynsym_value();
6024 // Make a dynamic relocation if necessary.
6025 if (gsym
->needs_dynamic_reloc(arp
->reference_flags()))
6027 if (!parameters
->options().output_is_position_independent()
6028 && gsym
->may_need_copy_reloc())
6030 target
->copy_reloc(symtab
, layout
, object
,
6031 data_shndx
, output_section
, gsym
, rela
);
6033 else if (r_type
== elfcpp::R_AARCH64_ABS64
6034 && gsym
->type() == elfcpp::STT_GNU_IFUNC
6035 && gsym
->can_use_relative_reloc(false)
6036 && !gsym
->is_from_dynobj()
6037 && !gsym
->is_undefined()
6038 && !gsym
->is_preemptible())
6040 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
6041 // symbol. This makes a function address in a PIE executable
6042 // match the address in a shared library that it links against.
6043 Reloc_section
* rela_dyn
=
6044 target
->rela_irelative_section(layout
);
6045 unsigned int r_type
= elfcpp::R_AARCH64_IRELATIVE
;
6046 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
6047 output_section
, object
,
6049 rela
.get_r_offset(),
6050 rela
.get_r_addend());
6052 else if (r_type
== elfcpp::R_AARCH64_ABS64
6053 && gsym
->can_use_relative_reloc(false))
6055 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6056 rela_dyn
->add_global_relative(gsym
,
6057 elfcpp::R_AARCH64_RELATIVE
,
6061 rela
.get_r_offset(),
6062 rela
.get_r_addend(),
6067 check_non_pic(object
, r_type
);
6068 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*
6069 rela_dyn
= target
->rela_dyn_section(layout
);
6070 rela_dyn
->add_global(
6071 gsym
, r_type
, output_section
, object
,
6072 data_shndx
, rela
.get_r_offset(),rela
.get_r_addend());
6078 case elfcpp::R_AARCH64_PREL16
:
6079 case elfcpp::R_AARCH64_PREL32
:
6080 case elfcpp::R_AARCH64_PREL64
:
6081 // This is used to fill the GOT absolute address.
6082 if (gsym
->needs_plt_entry())
6084 target
->make_plt_entry(symtab
, layout
, gsym
);
6088 case elfcpp::R_AARCH64_LD_PREL_LO19
: // 273
6089 case elfcpp::R_AARCH64_ADR_PREL_LO21
: // 274
6090 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21
: // 275
6091 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC
: // 276
6092 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC
: // 277
6093 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC
: // 278
6094 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC
: // 284
6095 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC
: // 285
6096 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC
: // 286
6097 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC
: // 299
6099 if (gsym
->needs_plt_entry())
6100 target
->make_plt_entry(symtab
, layout
, gsym
);
6101 // Make a dynamic relocation if necessary.
6102 if (gsym
->needs_dynamic_reloc(arp
->reference_flags()))
6104 if (parameters
->options().output_is_executable()
6105 && gsym
->may_need_copy_reloc())
6107 target
->copy_reloc(symtab
, layout
, object
,
6108 data_shndx
, output_section
, gsym
, rela
);
6114 case elfcpp::R_AARCH64_ADR_GOT_PAGE
:
6115 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC
:
6117 // This pair of relocations is used to access a specific GOT entry.
6118 // Note a GOT entry is an *address* to a symbol.
6119 // The symbol requires a GOT entry
6120 Output_data_got_aarch64
<size
, big_endian
>* got
=
6121 target
->got_section(symtab
, layout
);
6122 if (gsym
->final_value_is_known())
6124 // For a STT_GNU_IFUNC symbol we want the PLT address.
6125 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
6126 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
6128 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
6132 // If this symbol is not fully resolved, we need to add a dynamic
6133 // relocation for it.
6134 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6136 // Use a GLOB_DAT rather than a RELATIVE reloc if:
6138 // 1) The symbol may be defined in some other module.
6139 // 2) We are building a shared library and this is a protected
6140 // symbol; using GLOB_DAT means that the dynamic linker can use
6141 // the address of the PLT in the main executable when appropriate
6142 // so that function address comparisons work.
6143 // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
6144 // again so that function address comparisons work.
6145 if (gsym
->is_from_dynobj()
6146 || gsym
->is_undefined()
6147 || gsym
->is_preemptible()
6148 || (gsym
->visibility() == elfcpp::STV_PROTECTED
6149 && parameters
->options().shared())
6150 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
6151 && parameters
->options().output_is_position_independent()))
6152 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
6153 rela_dyn
, elfcpp::R_AARCH64_GLOB_DAT
);
6156 // For a STT_GNU_IFUNC symbol we want to write the PLT
6157 // offset into the GOT, so that function pointer
6158 // comparisons work correctly.
6160 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
6161 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
6164 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
6165 // Tell the dynamic linker to use the PLT address
6166 // when resolving relocations.
6167 if (gsym
->is_from_dynobj()
6168 && !parameters
->options().shared())
6169 gsym
->set_needs_dynsym_value();
6173 rela_dyn
->add_global_relative(
6174 gsym
, elfcpp::R_AARCH64_RELATIVE
,
6176 gsym
->got_offset(GOT_TYPE_STANDARD
),
6185 case elfcpp::R_AARCH64_TSTBR14
:
6186 case elfcpp::R_AARCH64_CONDBR19
:
6187 case elfcpp::R_AARCH64_JUMP26
:
6188 case elfcpp::R_AARCH64_CALL26
:
6190 if (gsym
->final_value_is_known())
6193 if (gsym
->is_defined() &&
6194 !gsym
->is_from_dynobj() &&
6195 !gsym
->is_preemptible())
6198 // Make plt entry for function call.
6199 target
->make_plt_entry(symtab
, layout
, gsym
);
6203 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21
:
6204 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
: // General dynamic
6206 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6207 optimize_tls_reloc(gsym
->final_value_is_known(), r_type
);
6208 if (tlsopt
== tls::TLSOPT_TO_LE
)
6210 layout
->set_has_static_tls();
6213 gold_assert(tlsopt
== tls::TLSOPT_NONE
);
6216 Output_data_got_aarch64
<size
, big_endian
>* got
=
6217 target
->got_section(symtab
, layout
);
6218 // Create 2 consecutive entries for module index and offset.
6219 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
6220 target
->rela_dyn_section(layout
),
6221 elfcpp::R_AARCH64_TLS_DTPMOD64
,
6222 elfcpp::R_AARCH64_TLS_DTPREL64
);
6226 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21
:
6227 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
: // Local dynamic
6229 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6230 optimize_tls_reloc(!parameters
->options().shared(), r_type
);
6231 if (tlsopt
== tls::TLSOPT_NONE
)
6233 // Create a GOT entry for the module index.
6234 target
->got_mod_index_entry(symtab
, layout
, object
);
6236 else if (tlsopt
!= tls::TLSOPT_TO_LE
)
6237 unsupported_reloc_local(object
, r_type
);
6241 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1
:
6242 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
6243 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12
:
6244 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
: // Other local dynamic
6247 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
6248 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
: // Initial executable
6250 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6251 optimize_tls_reloc(gsym
->final_value_is_known(), r_type
);
6252 if (tlsopt
== tls::TLSOPT_TO_LE
)
6255 layout
->set_has_static_tls();
6256 // Create a GOT entry for the tp-relative offset.
6257 Output_data_got_aarch64
<size
, big_endian
>* got
6258 = target
->got_section(symtab
, layout
);
6259 if (!parameters
->doing_static_link())
6261 got
->add_global_with_rel(
6262 gsym
, GOT_TYPE_TLS_OFFSET
,
6263 target
->rela_dyn_section(layout
),
6264 elfcpp::R_AARCH64_TLS_TPREL64
);
6266 if (!gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
))
6268 got
->add_global(gsym
, GOT_TYPE_TLS_OFFSET
);
6269 unsigned int got_offset
=
6270 gsym
->got_offset(GOT_TYPE_TLS_OFFSET
);
6271 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
6272 gold_assert(addend
== 0);
6273 got
->add_static_reloc(got_offset
,
6274 elfcpp::R_AARCH64_TLS_TPREL64
, gsym
);
6279 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2
:
6280 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1
:
6281 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
:
6282 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0
:
6283 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
:
6284 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
6285 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
6286 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
: // Local executable
6287 layout
->set_has_static_tls();
6288 if (parameters
->options().shared())
6289 gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
6290 object
->name().c_str(), r_type
);
6293 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
6294 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
6295 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
: // TLS descriptor
6297 target
->define_tls_base_symbol(symtab
, layout
);
6298 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6299 optimize_tls_reloc(gsym
->final_value_is_known(), r_type
);
6300 if (tlsopt
== tls::TLSOPT_NONE
)
6302 // Create reserved PLT and GOT entries for the resolver.
6303 target
->reserve_tlsdesc_entries(symtab
, layout
);
6305 // Create a double GOT entry with an R_AARCH64_TLSDESC
6306 // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
6307 // entry needs to be in an area in .got.plt, not .got. Call
6308 // got_section to make sure the section has been created.
6309 target
->got_section(symtab
, layout
);
6310 Output_data_got
<size
, big_endian
>* got
=
6311 target
->got_tlsdesc_section();
6312 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
6313 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
6314 elfcpp::R_AARCH64_TLSDESC
, 0);
6316 else if (tlsopt
== tls::TLSOPT_TO_IE
)
6318 // Create a GOT entry for the tp-relative offset.
6319 Output_data_got
<size
, big_endian
>* got
6320 = target
->got_section(symtab
, layout
);
6321 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
6322 target
->rela_dyn_section(layout
),
6323 elfcpp::R_AARCH64_TLS_TPREL64
);
6325 else if (tlsopt
!= tls::TLSOPT_TO_LE
)
6326 unsupported_reloc_global(object
, r_type
, gsym
);
6330 case elfcpp::R_AARCH64_TLSDESC_CALL
:
6334 gold_error(_("%s: unsupported reloc type in global scan"),
6335 aarch64_reloc_property_table
->
6336 reloc_name_in_error_message(r_type
).c_str());
6339 } // End of Scan::global
6342 // Create the PLT section.
6343 template<int size
, bool big_endian
>
6345 Target_aarch64
<size
, big_endian
>::make_plt_section(
6346 Symbol_table
* symtab
, Layout
* layout
)
6348 if (this->plt_
== NULL
)
6350 // Create the GOT section first.
6351 this->got_section(symtab
, layout
);
6353 this->plt_
= this->make_data_plt(layout
, this->got_
, this->got_plt_
,
6354 this->got_irelative_
);
6356 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
6358 | elfcpp::SHF_EXECINSTR
),
6359 this->plt_
, ORDER_PLT
, false);
6361 // Make the sh_info field of .rela.plt point to .plt.
6362 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
6363 rela_plt_os
->set_info_section(this->plt_
->output_section());
6367 // Return the section for TLSDESC relocations.
6369 template<int size
, bool big_endian
>
6370 typename Target_aarch64
<size
, big_endian
>::Reloc_section
*
6371 Target_aarch64
<size
, big_endian
>::rela_tlsdesc_section(Layout
* layout
) const
6373 return this->plt_section()->rela_tlsdesc(layout
);
6376 // Create a PLT entry for a global symbol.
6378 template<int size
, bool big_endian
>
6380 Target_aarch64
<size
, big_endian
>::make_plt_entry(
6381 Symbol_table
* symtab
,
6385 if (gsym
->has_plt_offset())
6388 if (this->plt_
== NULL
)
6389 this->make_plt_section(symtab
, layout
);
6391 this->plt_
->add_entry(symtab
, layout
, gsym
);
6394 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6396 template<int size
, bool big_endian
>
6398 Target_aarch64
<size
, big_endian
>::make_local_ifunc_plt_entry(
6399 Symbol_table
* symtab
, Layout
* layout
,
6400 Sized_relobj_file
<size
, big_endian
>* relobj
,
6401 unsigned int local_sym_index
)
6403 if (relobj
->local_has_plt_offset(local_sym_index
))
6405 if (this->plt_
== NULL
)
6406 this->make_plt_section(symtab
, layout
);
6407 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
6410 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
6413 template<int size
, bool big_endian
>
6415 Target_aarch64
<size
, big_endian
>::gc_process_relocs(
6416 Symbol_table
* symtab
,
6418 Sized_relobj_file
<size
, big_endian
>* object
,
6419 unsigned int data_shndx
,
6420 unsigned int sh_type
,
6421 const unsigned char* prelocs
,
6423 Output_section
* output_section
,
6424 bool needs_special_offset_handling
,
6425 size_t local_symbol_count
,
6426 const unsigned char* plocal_symbols
)
6428 if (sh_type
== elfcpp::SHT_REL
)
6433 gold::gc_process_relocs
<
6435 Target_aarch64
<size
, big_endian
>,
6437 typename Target_aarch64
<size
, big_endian
>::Scan
,
6438 typename Target_aarch64
<size
, big_endian
>::Relocatable_size_for_reloc
>(
6447 needs_special_offset_handling
,
6452 // Scan relocations for a section.
6454 template<int size
, bool big_endian
>
6456 Target_aarch64
<size
, big_endian
>::scan_relocs(
6457 Symbol_table
* symtab
,
6459 Sized_relobj_file
<size
, big_endian
>* object
,
6460 unsigned int data_shndx
,
6461 unsigned int sh_type
,
6462 const unsigned char* prelocs
,
6464 Output_section
* output_section
,
6465 bool needs_special_offset_handling
,
6466 size_t local_symbol_count
,
6467 const unsigned char* plocal_symbols
)
6469 if (sh_type
== elfcpp::SHT_REL
)
6471 gold_error(_("%s: unsupported REL reloc section"),
6472 object
->name().c_str());
6475 gold::scan_relocs
<size
, big_endian
, Target_aarch64
, elfcpp::SHT_RELA
, Scan
>(
6484 needs_special_offset_handling
,
6489 // Return the value to use for a dynamic which requires special
6490 // treatment. This is how we support equality comparisons of function
6491 // pointers across shared library boundaries, as described in the
6492 // processor specific ABI supplement.
6494 template<int size
, bool big_endian
>
6496 Target_aarch64
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
6498 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
6499 return this->plt_address_for_global(gsym
);
6503 // Finalize the sections.
6505 template<int size
, bool big_endian
>
6507 Target_aarch64
<size
, big_endian
>::do_finalize_sections(
6509 const Input_objects
*,
6510 Symbol_table
* symtab
)
6512 const Reloc_section
* rel_plt
= (this->plt_
== NULL
6514 : this->plt_
->rela_plt());
6515 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
6516 this->rela_dyn_
, true, false);
6518 // Emit any relocs we saved in an attempt to avoid generating COPY
6520 if (this->copy_relocs_
.any_saved_relocs())
6521 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
6523 // Fill in some more dynamic tags.
6524 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
6527 if (this->plt_
!= NULL
6528 && this->plt_
->output_section() != NULL
6529 && this->plt_
->has_tlsdesc_entry())
6531 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
6532 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
6533 this->got_
->finalize_data_size();
6534 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
6535 this->plt_
, plt_offset
);
6536 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
6537 this->got_
, got_offset
);
6541 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
6542 // the .got.plt section.
6543 Symbol
* sym
= this->global_offset_table_
;
6546 uint64_t data_size
= this->got_plt_
->current_data_size();
6547 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
6549 // If the .got section is more than 0x8000 bytes, we add
6550 // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
6551 // bit relocations have a greater chance of working.
6552 if (data_size
>= 0x8000)
6553 symtab
->get_sized_symbol
<size
>(sym
)->set_value(
6554 symtab
->get_sized_symbol
<size
>(sym
)->value() + 0x8000);
6557 if (parameters
->doing_static_link()
6558 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
6560 // If linking statically, make sure that the __rela_iplt symbols
6561 // were defined if necessary, even if we didn't create a PLT.
6562 static const Define_symbol_in_segment syms
[] =
6565 "__rela_iplt_start", // name
6566 elfcpp::PT_LOAD
, // segment_type
6567 elfcpp::PF_W
, // segment_flags_set
6568 elfcpp::PF(0), // segment_flags_clear
6571 elfcpp::STT_NOTYPE
, // type
6572 elfcpp::STB_GLOBAL
, // binding
6573 elfcpp::STV_HIDDEN
, // visibility
6575 Symbol::SEGMENT_START
, // offset_from_base
6579 "__rela_iplt_end", // name
6580 elfcpp::PT_LOAD
, // segment_type
6581 elfcpp::PF_W
, // segment_flags_set
6582 elfcpp::PF(0), // segment_flags_clear
6585 elfcpp::STT_NOTYPE
, // type
6586 elfcpp::STB_GLOBAL
, // binding
6587 elfcpp::STV_HIDDEN
, // visibility
6589 Symbol::SEGMENT_START
, // offset_from_base
6594 symtab
->define_symbols(layout
, 2, syms
,
6595 layout
->script_options()->saw_sections_clause());
6601 // Perform a relocation.
6603 template<int size
, bool big_endian
>
6605 Target_aarch64
<size
, big_endian
>::Relocate::relocate(
6606 const Relocate_info
<size
, big_endian
>* relinfo
,
6607 Target_aarch64
<size
, big_endian
>* target
,
6610 const elfcpp::Rela
<size
, big_endian
>& rela
,
6611 unsigned int r_type
,
6612 const Sized_symbol
<size
>* gsym
,
6613 const Symbol_value
<size
>* psymval
,
6614 unsigned char* view
,
6615 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
6616 section_size_type
/* view_size */)
6621 typedef AArch64_relocate_functions
<size
, big_endian
> Reloc
;
6623 const AArch64_reloc_property
* reloc_property
=
6624 aarch64_reloc_property_table
->get_reloc_property(r_type
);
6626 if (reloc_property
== NULL
)
6628 std::string reloc_name
=
6629 aarch64_reloc_property_table
->reloc_name_in_error_message(r_type
);
6630 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6631 _("cannot relocate %s in object file"),
6632 reloc_name
.c_str());
6636 const Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
6638 // Pick the value to use for symbols defined in the PLT.
6639 Symbol_value
<size
> symval
;
6641 && gsym
->use_plt_offset(reloc_property
->reference_flags()))
6643 symval
.set_output_value(target
->plt_address_for_global(gsym
));
6646 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
6648 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6649 if (object
->local_has_plt_offset(r_sym
))
6651 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
6656 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
6658 // Get the GOT offset if needed.
6659 // For aarch64, the GOT pointer points to the start of the GOT section.
6660 bool have_got_offset
= false;
6662 int got_base
= (target
->got_
!= NULL
6663 ? (target
->got_
->current_data_size() >= 0x8000
6668 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0
:
6669 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC
:
6670 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1
:
6671 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC
:
6672 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2
:
6673 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC
:
6674 case elfcpp::R_AARCH64_MOVW_GOTOFF_G3
:
6675 case elfcpp::R_AARCH64_GOTREL64
:
6676 case elfcpp::R_AARCH64_GOTREL32
:
6677 case elfcpp::R_AARCH64_GOT_LD_PREL19
:
6678 case elfcpp::R_AARCH64_LD64_GOTOFF_LO15
:
6679 case elfcpp::R_AARCH64_ADR_GOT_PAGE
:
6680 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC
:
6681 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15
:
6684 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
6685 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - got_base
;
6689 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6690 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
6691 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
6694 have_got_offset
= true;
6701 typename
Reloc::Status reloc_status
= Reloc::STATUS_OKAY
;
6702 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
6705 case elfcpp::R_AARCH64_NONE
:
6708 case elfcpp::R_AARCH64_ABS64
:
6709 reloc_status
= Reloc::template rela_ua
<64>(
6710 view
, object
, psymval
, addend
, reloc_property
);
6713 case elfcpp::R_AARCH64_ABS32
:
6714 reloc_status
= Reloc::template rela_ua
<32>(
6715 view
, object
, psymval
, addend
, reloc_property
);
6718 case elfcpp::R_AARCH64_ABS16
:
6719 reloc_status
= Reloc::template rela_ua
<16>(
6720 view
, object
, psymval
, addend
, reloc_property
);
6723 case elfcpp::R_AARCH64_PREL64
:
6724 reloc_status
= Reloc::template pcrela_ua
<64>(
6725 view
, object
, psymval
, addend
, address
, reloc_property
);
6728 case elfcpp::R_AARCH64_PREL32
:
6729 reloc_status
= Reloc::template pcrela_ua
<32>(
6730 view
, object
, psymval
, addend
, address
, reloc_property
);
6733 case elfcpp::R_AARCH64_PREL16
:
6734 reloc_status
= Reloc::template pcrela_ua
<16>(
6735 view
, object
, psymval
, addend
, address
, reloc_property
);
6738 case elfcpp::R_AARCH64_LD_PREL_LO19
:
6739 reloc_status
= Reloc::template pcrela_general
<32>(
6740 view
, object
, psymval
, addend
, address
, reloc_property
);
6743 case elfcpp::R_AARCH64_ADR_PREL_LO21
:
6744 reloc_status
= Reloc::adr(view
, object
, psymval
, addend
,
6745 address
, reloc_property
);
6748 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC
:
6749 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21
:
6750 reloc_status
= Reloc::adrp(view
, object
, psymval
, addend
, address
,
6754 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC
:
6755 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC
:
6756 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC
:
6757 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC
:
6758 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC
:
6759 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC
:
6760 reloc_status
= Reloc::template rela_general
<32>(
6761 view
, object
, psymval
, addend
, reloc_property
);
6764 case elfcpp::R_AARCH64_CALL26
:
6765 if (this->skip_call_tls_get_addr_
)
6767 // Double check that the TLSGD insn has been optimized away.
6768 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
6769 Insntype insn
= elfcpp::Swap
<32, big_endian
>::readval(
6770 reinterpret_cast<Insntype
*>(view
));
6771 gold_assert((insn
& 0xff000000) == 0x91000000);
6773 reloc_status
= Reloc::STATUS_OKAY
;
6774 this->skip_call_tls_get_addr_
= false;
6775 // Return false to stop further processing this reloc.
6779 case elfcpp::R_AARCH64_JUMP26
:
6780 if (Reloc::maybe_apply_stub(r_type
, relinfo
, rela
, view
, address
,
6781 gsym
, psymval
, object
,
6782 target
->stub_group_size_
))
6785 case elfcpp::R_AARCH64_TSTBR14
:
6786 case elfcpp::R_AARCH64_CONDBR19
:
6787 reloc_status
= Reloc::template pcrela_general
<32>(
6788 view
, object
, psymval
, addend
, address
, reloc_property
);
6791 case elfcpp::R_AARCH64_ADR_GOT_PAGE
:
6792 gold_assert(have_got_offset
);
6793 value
= target
->got_
->address() + got_base
+ got_offset
;
6794 reloc_status
= Reloc::adrp(view
, value
+ addend
, address
);
6797 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC
:
6798 gold_assert(have_got_offset
);
6799 value
= target
->got_
->address() + got_base
+ got_offset
;
6800 reloc_status
= Reloc::template rela_general
<32>(
6801 view
, value
, addend
, reloc_property
);
6804 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21
:
6805 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
:
6806 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21
:
6807 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
:
6808 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1
:
6809 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
6810 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12
:
6811 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
:
6812 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
6813 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
6814 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2
:
6815 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1
:
6816 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
:
6817 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0
:
6818 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
:
6819 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
6820 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
6821 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
6822 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
6823 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
6824 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
6825 case elfcpp::R_AARCH64_TLSDESC_CALL
:
6826 reloc_status
= relocate_tls(relinfo
, target
, relnum
, rela
, r_type
,
6827 gsym
, psymval
, view
, address
);
6830 // These are dynamic relocations, which are unexpected when linking.
6831 case elfcpp::R_AARCH64_COPY
:
6832 case elfcpp::R_AARCH64_GLOB_DAT
:
6833 case elfcpp::R_AARCH64_JUMP_SLOT
:
6834 case elfcpp::R_AARCH64_RELATIVE
:
6835 case elfcpp::R_AARCH64_IRELATIVE
:
6836 case elfcpp::R_AARCH64_TLS_DTPREL64
:
6837 case elfcpp::R_AARCH64_TLS_DTPMOD64
:
6838 case elfcpp::R_AARCH64_TLS_TPREL64
:
6839 case elfcpp::R_AARCH64_TLSDESC
:
6840 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6841 _("unexpected reloc %u in object file"),
6846 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6847 _("unsupported reloc %s"),
6848 reloc_property
->name().c_str());
6852 // Report any errors.
6853 switch (reloc_status
)
6855 case Reloc::STATUS_OKAY
:
6857 case Reloc::STATUS_OVERFLOW
:
6858 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6859 _("relocation overflow in %s"),
6860 reloc_property
->name().c_str());
6862 case Reloc::STATUS_BAD_RELOC
:
6863 gold_error_at_location(
6866 rela
.get_r_offset(),
6867 _("unexpected opcode while processing relocation %s"),
6868 reloc_property
->name().c_str());
6878 template<int size
, bool big_endian
>
6880 typename AArch64_relocate_functions
<size
, big_endian
>::Status
6881 Target_aarch64
<size
, big_endian
>::Relocate::relocate_tls(
6882 const Relocate_info
<size
, big_endian
>* relinfo
,
6883 Target_aarch64
<size
, big_endian
>* target
,
6885 const elfcpp::Rela
<size
, big_endian
>& rela
,
6886 unsigned int r_type
, const Sized_symbol
<size
>* gsym
,
6887 const Symbol_value
<size
>* psymval
,
6888 unsigned char* view
,
6889 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
6891 typedef AArch64_relocate_functions
<size
, big_endian
> aarch64_reloc_funcs
;
6892 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
6894 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
6895 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
6896 const AArch64_reloc_property
* reloc_property
=
6897 aarch64_reloc_property_table
->get_reloc_property(r_type
);
6898 gold_assert(reloc_property
!= NULL
);
6900 const bool is_final
= (gsym
== NULL
6901 ? !parameters
->options().shared()
6902 : gsym
->final_value_is_known());
6903 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6904 optimize_tls_reloc(is_final
, r_type
);
6906 Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
6907 int tls_got_offset_type
;
6910 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21
:
6911 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
: // Global-dynamic
6913 if (tlsopt
== tls::TLSOPT_TO_LE
)
6915 if (tls_segment
== NULL
)
6917 gold_assert(parameters
->errors()->error_count() > 0
6918 || issue_undefined_symbol_error(gsym
));
6919 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
6921 return tls_gd_to_le(relinfo
, target
, rela
, r_type
, view
,
6924 else if (tlsopt
== tls::TLSOPT_NONE
)
6926 tls_got_offset_type
= GOT_TYPE_TLS_PAIR
;
6927 // Firstly get the address for the got entry.
6928 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_entry_address
;
6931 gold_assert(gsym
->has_got_offset(tls_got_offset_type
));
6932 got_entry_address
= target
->got_
->address() +
6933 gsym
->got_offset(tls_got_offset_type
);
6937 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6939 object
->local_has_got_offset(r_sym
, tls_got_offset_type
));
6940 got_entry_address
= target
->got_
->address() +
6941 object
->local_got_offset(r_sym
, tls_got_offset_type
);
6944 // Relocate the address into adrp/ld, adrp/add pair.
6947 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21
:
6948 return aarch64_reloc_funcs::adrp(
6949 view
, got_entry_address
+ addend
, address
);
6953 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
:
6954 return aarch64_reloc_funcs::template rela_general
<32>(
6955 view
, got_entry_address
, addend
, reloc_property
);
6962 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6963 _("unsupported gd_to_ie relaxation on %u"),
6968 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21
:
6969 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
: // Local-dynamic
6971 if (tlsopt
== tls::TLSOPT_TO_LE
)
6973 if (tls_segment
== NULL
)
6975 gold_assert(parameters
->errors()->error_count() > 0
6976 || issue_undefined_symbol_error(gsym
));
6977 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
6979 return this->tls_ld_to_le(relinfo
, target
, rela
, r_type
, view
,
6983 gold_assert(tlsopt
== tls::TLSOPT_NONE
);
6984 // Relocate the field with the offset of the GOT entry for
6985 // the module index.
6986 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_entry_address
;
6987 got_entry_address
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
) +
6988 target
->got_
->address());
6992 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21
:
6993 return aarch64_reloc_funcs::adrp(
6994 view
, got_entry_address
+ addend
, address
);
6997 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
:
6998 return aarch64_reloc_funcs::template rela_general
<32>(
6999 view
, got_entry_address
, addend
, reloc_property
);
7008 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1
:
7009 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
7010 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12
:
7011 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
: // Other local-dynamic
7013 AArch64_address value
= psymval
->value(object
, 0);
7014 if (tlsopt
== tls::TLSOPT_TO_LE
)
7016 if (tls_segment
== NULL
)
7018 gold_assert(parameters
->errors()->error_count() > 0
7019 || issue_undefined_symbol_error(gsym
));
7020 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7025 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1
:
7026 return aarch64_reloc_funcs::movnz(view
, value
+ addend
,
7030 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
7031 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12
:
7032 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
:
7033 return aarch64_reloc_funcs::template rela_general
<32>(
7034 view
, value
, addend
, reloc_property
);
7040 // We should never reach here.
7044 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
7045 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
: // Initial-exec
7047 if (tlsopt
== tls::TLSOPT_TO_LE
)
7049 if (tls_segment
== NULL
)
7051 gold_assert(parameters
->errors()->error_count() > 0
7052 || issue_undefined_symbol_error(gsym
));
7053 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7055 return tls_ie_to_le(relinfo
, target
, rela
, r_type
, view
,
7058 tls_got_offset_type
= GOT_TYPE_TLS_OFFSET
;
7060 // Firstly get the address for the got entry.
7061 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_entry_address
;
7064 gold_assert(gsym
->has_got_offset(tls_got_offset_type
));
7065 got_entry_address
= target
->got_
->address() +
7066 gsym
->got_offset(tls_got_offset_type
);
7070 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7072 object
->local_has_got_offset(r_sym
, tls_got_offset_type
));
7073 got_entry_address
= target
->got_
->address() +
7074 object
->local_got_offset(r_sym
, tls_got_offset_type
);
7076 // Relocate the address into adrp/ld, adrp/add pair.
7079 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
7080 return aarch64_reloc_funcs::adrp(view
, got_entry_address
+ addend
,
7083 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
7084 return aarch64_reloc_funcs::template rela_general
<32>(
7085 view
, got_entry_address
, addend
, reloc_property
);
7090 // We shall never reach here.
7093 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2
:
7094 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1
:
7095 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
:
7096 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0
:
7097 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
:
7098 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
7099 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
7100 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
7102 gold_assert(tls_segment
!= NULL
);
7103 AArch64_address value
= psymval
->value(object
, 0);
7105 if (!parameters
->options().shared())
7107 AArch64_address aligned_tcb_size
=
7108 align_address(target
->tcb_size(),
7109 tls_segment
->maximum_alignment());
7110 value
+= aligned_tcb_size
;
7113 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2
:
7114 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1
:
7115 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0
:
7116 return aarch64_reloc_funcs::movnz(view
, value
+ addend
,
7119 return aarch64_reloc_funcs::template
7120 rela_general
<32>(view
,
7127 gold_error(_("%s: unsupported reloc %u "
7128 "in non-static TLSLE mode."),
7129 object
->name().c_str(), r_type
);
7133 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
7134 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
7135 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
7136 case elfcpp::R_AARCH64_TLSDESC_CALL
:
7138 if (tlsopt
== tls::TLSOPT_TO_LE
)
7140 if (tls_segment
== NULL
)
7142 gold_assert(parameters
->errors()->error_count() > 0
7143 || issue_undefined_symbol_error(gsym
));
7144 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7146 return tls_desc_gd_to_le(relinfo
, target
, rela
, r_type
,
7151 tls_got_offset_type
= (tlsopt
== tls::TLSOPT_TO_IE
7152 ? GOT_TYPE_TLS_OFFSET
7153 : GOT_TYPE_TLS_DESC
);
7154 unsigned int got_tlsdesc_offset
= 0;
7155 if (r_type
!= elfcpp::R_AARCH64_TLSDESC_CALL
7156 && tlsopt
== tls::TLSOPT_NONE
)
7158 // We created GOT entries in the .got.tlsdesc portion of the
7159 // .got.plt section, but the offset stored in the symbol is the
7160 // offset within .got.tlsdesc.
7161 got_tlsdesc_offset
= (target
->got_
->data_size()
7162 + target
->got_plt_section()->data_size());
7164 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_entry_address
;
7167 gold_assert(gsym
->has_got_offset(tls_got_offset_type
));
7168 got_entry_address
= target
->got_
->address()
7169 + got_tlsdesc_offset
7170 + gsym
->got_offset(tls_got_offset_type
);
7174 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7176 object
->local_has_got_offset(r_sym
, tls_got_offset_type
));
7177 got_entry_address
= target
->got_
->address() +
7178 got_tlsdesc_offset
+
7179 object
->local_got_offset(r_sym
, tls_got_offset_type
);
7181 if (tlsopt
== tls::TLSOPT_TO_IE
)
7183 if (tls_segment
== NULL
)
7185 gold_assert(parameters
->errors()->error_count() > 0
7186 || issue_undefined_symbol_error(gsym
));
7187 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7189 return tls_desc_gd_to_ie(relinfo
, target
, rela
, r_type
,
7190 view
, psymval
, got_entry_address
,
7194 // Now do tlsdesc relocation.
7197 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
7198 return aarch64_reloc_funcs::adrp(view
,
7199 got_entry_address
+ addend
,
7202 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
7203 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
7204 return aarch64_reloc_funcs::template rela_general
<32>(
7205 view
, got_entry_address
, addend
, reloc_property
);
7207 case elfcpp::R_AARCH64_TLSDESC_CALL
:
7208 return aarch64_reloc_funcs::STATUS_OKAY
;
7218 gold_error(_("%s: unsupported TLS reloc %u."),
7219 object
->name().c_str(), r_type
);
7221 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7222 } // End of relocate_tls.
7225 template<int size
, bool big_endian
>
7227 typename AArch64_relocate_functions
<size
, big_endian
>::Status
7228 Target_aarch64
<size
, big_endian
>::Relocate::tls_gd_to_le(
7229 const Relocate_info
<size
, big_endian
>* relinfo
,
7230 Target_aarch64
<size
, big_endian
>* target
,
7231 const elfcpp::Rela
<size
, big_endian
>& rela
,
7232 unsigned int r_type
,
7233 unsigned char* view
,
7234 const Symbol_value
<size
>* psymval
)
7236 typedef AArch64_relocate_functions
<size
, big_endian
> aarch64_reloc_funcs
;
7237 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
7238 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
7240 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
7241 Insntype insn1
= elfcpp::Swap
<32, big_endian
>::readval(ip
);
7242 Insntype insn2
= elfcpp::Swap
<32, big_endian
>::readval(ip
+ 1);
7243 Insntype insn3
= elfcpp::Swap
<32, big_endian
>::readval(ip
+ 2);
7245 if (r_type
== elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
)
7247 // This is the 2nd relocs, optimization should already have been
7249 gold_assert((insn1
& 0xfff00000) == 0x91400000);
7250 return aarch64_reloc_funcs::STATUS_OKAY
;
7253 // The original sequence is -
7254 // 90000000 adrp x0, 0 <main>
7255 // 91000000 add x0, x0, #0x0
7256 // 94000000 bl 0 <__tls_get_addr>
7257 // optimized to sequence -
7258 // d53bd040 mrs x0, tpidr_el0
7259 // 91400000 add x0, x0, #0x0, lsl #12
7260 // 91000000 add x0, x0, #0x0
7262 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7263 // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
7264 // have to change "bl tls_get_addr", which does not have a corresponding tls
7265 // relocation type. So before proceeding, we need to make sure compiler
7266 // does not change the sequence.
7267 if(!(insn1
== 0x90000000 // adrp x0,0
7268 && insn2
== 0x91000000 // add x0, x0, #0x0
7269 && insn3
== 0x94000000)) // bl 0
7271 // Ideally we should give up gd_to_le relaxation and do gd access.
7272 // However the gd_to_le relaxation decision has been made early
7273 // in the scan stage, where we did not allocate any GOT entry for
7274 // this symbol. Therefore we have to exit and report error now.
7275 gold_error(_("unexpected reloc insn sequence while relaxing "
7276 "tls gd to le for reloc %u."), r_type
);
7277 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7281 insn1
= 0xd53bd040; // mrs x0, tpidr_el0
7282 insn2
= 0x91400000; // add x0, x0, #0x0, lsl #12
7283 insn3
= 0x91000000; // add x0, x0, #0x0
7284 elfcpp::Swap
<32, big_endian
>::writeval(ip
, insn1
);
7285 elfcpp::Swap
<32, big_endian
>::writeval(ip
+ 1, insn2
);
7286 elfcpp::Swap
<32, big_endian
>::writeval(ip
+ 2, insn3
);
7288 // Calculate tprel value.
7289 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
7290 gold_assert(tls_segment
!= NULL
);
7291 AArch64_address value
= psymval
->value(relinfo
->object
, 0);
7292 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
7293 AArch64_address aligned_tcb_size
=
7294 align_address(target
->tcb_size(), tls_segment
->maximum_alignment());
7295 AArch64_address x
= value
+ aligned_tcb_size
;
7297 // After new insns are written, apply TLSLE relocs.
7298 const AArch64_reloc_property
* rp1
=
7299 aarch64_reloc_property_table
->get_reloc_property(
7300 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
);
7301 const AArch64_reloc_property
* rp2
=
7302 aarch64_reloc_property_table
->get_reloc_property(
7303 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
);
7304 gold_assert(rp1
!= NULL
&& rp2
!= NULL
);
7306 typename
aarch64_reloc_funcs::Status s1
=
7307 aarch64_reloc_funcs::template rela_general
<32>(view
+ 4,
7311 if (s1
!= aarch64_reloc_funcs::STATUS_OKAY
)
7314 typename
aarch64_reloc_funcs::Status s2
=
7315 aarch64_reloc_funcs::template rela_general
<32>(view
+ 8,
7320 this->skip_call_tls_get_addr_
= true;
7322 } // End of tls_gd_to_le
7325 template<int size
, bool big_endian
>
7327 typename AArch64_relocate_functions
<size
, big_endian
>::Status
7328 Target_aarch64
<size
, big_endian
>::Relocate::tls_ld_to_le(
7329 const Relocate_info
<size
, big_endian
>* relinfo
,
7330 Target_aarch64
<size
, big_endian
>* target
,
7331 const elfcpp::Rela
<size
, big_endian
>& rela
,
7332 unsigned int r_type
,
7333 unsigned char* view
,
7334 const Symbol_value
<size
>* psymval
)
7336 typedef AArch64_relocate_functions
<size
, big_endian
> aarch64_reloc_funcs
;
7337 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
7338 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
7340 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
7341 Insntype insn1
= elfcpp::Swap
<32, big_endian
>::readval(ip
);
7342 Insntype insn2
= elfcpp::Swap
<32, big_endian
>::readval(ip
+ 1);
7343 Insntype insn3
= elfcpp::Swap
<32, big_endian
>::readval(ip
+ 2);
7345 if (r_type
== elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
)
7347 // This is the 2nd relocs, optimization should already have been
7349 gold_assert((insn1
& 0xfff00000) == 0x91400000);
7350 return aarch64_reloc_funcs::STATUS_OKAY
;
7353 // The original sequence is -
7354 // 90000000 adrp x0, 0 <main>
7355 // 91000000 add x0, x0, #0x0
7356 // 94000000 bl 0 <__tls_get_addr>
7357 // optimized to sequence -
7358 // d53bd040 mrs x0, tpidr_el0
7359 // 91400000 add x0, x0, #0x0, lsl #12
7360 // 91000000 add x0, x0, #0x0
7362 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7363 // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
7364 // have to change "bl tls_get_addr", which does not have a corresponding tls
7365 // relocation type. So before proceeding, we need to make sure compiler
7366 // does not change the sequence.
7367 if(!(insn1
== 0x90000000 // adrp x0,0
7368 && insn2
== 0x91000000 // add x0, x0, #0x0
7369 && insn3
== 0x94000000)) // bl 0
7371 // Ideally we should give up gd_to_le relaxation and do gd access.
7372 // However the gd_to_le relaxation decision has been made early
7373 // in the scan stage, where we did not allocate any GOT entry for
7374 // this symbol. Therefore we have to exit and report error now.
7375 gold_error(_("unexpected reloc insn sequence while relaxing "
7376 "tls gd to le for reloc %u."), r_type
);
7377 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7381 insn1
= 0xd53bd040; // mrs x0, tpidr_el0
7382 insn2
= 0x91400000; // add x0, x0, #0x0, lsl #12
7383 insn3
= 0x91000000; // add x0, x0, #0x0
7384 elfcpp::Swap
<32, big_endian
>::writeval(ip
, insn1
);
7385 elfcpp::Swap
<32, big_endian
>::writeval(ip
+ 1, insn2
);
7386 elfcpp::Swap
<32, big_endian
>::writeval(ip
+ 2, insn3
);
7388 // Calculate tprel value.
7389 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
7390 gold_assert(tls_segment
!= NULL
);
7391 AArch64_address value
= psymval
->value(relinfo
->object
, 0);
7392 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
7393 AArch64_address aligned_tcb_size
=
7394 align_address(target
->tcb_size(), tls_segment
->maximum_alignment());
7395 AArch64_address x
= value
+ aligned_tcb_size
;
7397 // After new insns are written, apply TLSLE relocs.
7398 const AArch64_reloc_property
* rp1
=
7399 aarch64_reloc_property_table
->get_reloc_property(
7400 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
);
7401 const AArch64_reloc_property
* rp2
=
7402 aarch64_reloc_property_table
->get_reloc_property(
7403 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
);
7404 gold_assert(rp1
!= NULL
&& rp2
!= NULL
);
7406 typename
aarch64_reloc_funcs::Status s1
=
7407 aarch64_reloc_funcs::template rela_general
<32>(view
+ 4,
7411 if (s1
!= aarch64_reloc_funcs::STATUS_OKAY
)
7414 typename
aarch64_reloc_funcs::Status s2
=
7415 aarch64_reloc_funcs::template rela_general
<32>(view
+ 8,
7420 this->skip_call_tls_get_addr_
= true;
7423 } // End of tls_ld_to_le
7425 template<int size
, bool big_endian
>
7427 typename AArch64_relocate_functions
<size
, big_endian
>::Status
7428 Target_aarch64
<size
, big_endian
>::Relocate::tls_ie_to_le(
7429 const Relocate_info
<size
, big_endian
>* relinfo
,
7430 Target_aarch64
<size
, big_endian
>* target
,
7431 const elfcpp::Rela
<size
, big_endian
>& rela
,
7432 unsigned int r_type
,
7433 unsigned char* view
,
7434 const Symbol_value
<size
>* psymval
)
7436 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
7437 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
7438 typedef AArch64_relocate_functions
<size
, big_endian
> aarch64_reloc_funcs
;
7440 AArch64_address value
= psymval
->value(relinfo
->object
, 0);
7441 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
7442 AArch64_address aligned_tcb_address
=
7443 align_address(target
->tcb_size(), tls_segment
->maximum_alignment());
7444 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
7445 AArch64_address x
= value
+ addend
+ aligned_tcb_address
;
7446 // "x" is the offset to tp, we can only do this if x is within
7447 // range [0, 2^32-1]
7448 if (!(size
== 32 || (size
== 64 && (static_cast<uint64_t>(x
) >> 32) == 0)))
7450 gold_error(_("TLS variable referred by reloc %u is too far from TP."),
7452 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7455 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
7456 Insntype insn
= elfcpp::Swap
<32, big_endian
>::readval(ip
);
7459 if (r_type
== elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
)
7462 regno
= (insn
& 0x1f);
7463 newinsn
= (0xd2a00000 | regno
) | (((x
>> 16) & 0xffff) << 5);
7465 else if (r_type
== elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
)
7468 regno
= (insn
& 0x1f);
7469 gold_assert(regno
== ((insn
>> 5) & 0x1f));
7470 newinsn
= (0xf2800000 | regno
) | ((x
& 0xffff) << 5);
7475 elfcpp::Swap
<32, big_endian
>::writeval(ip
, newinsn
);
7476 return aarch64_reloc_funcs::STATUS_OKAY
;
7477 } // End of tls_ie_to_le
7480 template<int size
, bool big_endian
>
7482 typename AArch64_relocate_functions
<size
, big_endian
>::Status
7483 Target_aarch64
<size
, big_endian
>::Relocate::tls_desc_gd_to_le(
7484 const Relocate_info
<size
, big_endian
>* relinfo
,
7485 Target_aarch64
<size
, big_endian
>* target
,
7486 const elfcpp::Rela
<size
, big_endian
>& rela
,
7487 unsigned int r_type
,
7488 unsigned char* view
,
7489 const Symbol_value
<size
>* psymval
)
7491 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
7492 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
7493 typedef AArch64_relocate_functions
<size
, big_endian
> aarch64_reloc_funcs
;
7495 // TLSDESC-GD sequence is like:
7496 // adrp x0, :tlsdesc:v1
7497 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7498 // add x0, x0, :tlsdesc_lo12:v1
7501 // After desc_gd_to_le optimization, the sequence will be like:
7502 // movz x0, #0x0, lsl #16
7507 // Calculate tprel value.
7508 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
7509 gold_assert(tls_segment
!= NULL
);
7510 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
7511 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
7512 AArch64_address value
= psymval
->value(relinfo
->object
, addend
);
7513 AArch64_address aligned_tcb_size
=
7514 align_address(target
->tcb_size(), tls_segment
->maximum_alignment());
7515 AArch64_address x
= value
+ aligned_tcb_size
;
7516 // x is the offset to tp, we can only do this if x is within range
7517 // [0, 2^32-1]. If x is out of range, fail and exit.
7518 if (size
== 64 && (static_cast<uint64_t>(x
) >> 32) != 0)
7520 gold_error(_("TLS variable referred by reloc %u is too far from TP. "
7521 "We Can't do gd_to_le relaxation.\n"), r_type
);
7522 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7527 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
7528 case elfcpp::R_AARCH64_TLSDESC_CALL
:
7530 newinsn
= 0xd503201f;
7533 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
7535 newinsn
= 0xd2a00000 | (((x
>> 16) & 0xffff) << 5);
7538 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
7540 newinsn
= 0xf2800000 | ((x
& 0xffff) << 5);
7544 gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
7548 elfcpp::Swap
<32, big_endian
>::writeval(ip
, newinsn
);
7549 return aarch64_reloc_funcs::STATUS_OKAY
;
7550 } // End of tls_desc_gd_to_le
7553 template<int size
, bool big_endian
>
7555 typename AArch64_relocate_functions
<size
, big_endian
>::Status
7556 Target_aarch64
<size
, big_endian
>::Relocate::tls_desc_gd_to_ie(
7557 const Relocate_info
<size
, big_endian
>* /* relinfo */,
7558 Target_aarch64
<size
, big_endian
>* /* target */,
7559 const elfcpp::Rela
<size
, big_endian
>& rela
,
7560 unsigned int r_type
,
7561 unsigned char* view
,
7562 const Symbol_value
<size
>* /* psymval */,
7563 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_entry_address
,
7564 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
7566 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
7567 typedef AArch64_relocate_functions
<size
, big_endian
> aarch64_reloc_funcs
;
7569 // TLSDESC-GD sequence is like:
7570 // adrp x0, :tlsdesc:v1
7571 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7572 // add x0, x0, :tlsdesc_lo12:v1
7575 // After desc_gd_to_ie optimization, the sequence will be like:
7576 // adrp x0, :tlsie:v1
7577 // ldr x0, [x0, :tlsie_lo12:v1]
7581 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
7582 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
7586 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
7587 case elfcpp::R_AARCH64_TLSDESC_CALL
:
7589 newinsn
= 0xd503201f;
7590 elfcpp::Swap
<32, big_endian
>::writeval(ip
, newinsn
);
7593 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
7595 return aarch64_reloc_funcs::adrp(view
, got_entry_address
+ addend
,
7600 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
7602 // Set ldr target register to be x0.
7603 Insntype insn
= elfcpp::Swap
<32, big_endian
>::readval(ip
);
7605 elfcpp::Swap
<32, big_endian
>::writeval(ip
, insn
);
7607 const AArch64_reloc_property
* reloc_property
=
7608 aarch64_reloc_property_table
->get_reloc_property(
7609 elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
);
7610 return aarch64_reloc_funcs::template rela_general
<32>(
7611 view
, got_entry_address
, addend
, reloc_property
);
7616 gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
7620 return aarch64_reloc_funcs::STATUS_OKAY
;
7621 } // End of tls_desc_gd_to_ie
7623 // Relocate section data.
7625 template<int size
, bool big_endian
>
7627 Target_aarch64
<size
, big_endian
>::relocate_section(
7628 const Relocate_info
<size
, big_endian
>* relinfo
,
7629 unsigned int sh_type
,
7630 const unsigned char* prelocs
,
7632 Output_section
* output_section
,
7633 bool needs_special_offset_handling
,
7634 unsigned char* view
,
7635 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
7636 section_size_type view_size
,
7637 const Reloc_symbol_changes
* reloc_symbol_changes
)
7639 gold_assert(sh_type
== elfcpp::SHT_RELA
);
7640 typedef typename Target_aarch64
<size
, big_endian
>::Relocate AArch64_relocate
;
7641 gold::relocate_section
<size
, big_endian
, Target_aarch64
, elfcpp::SHT_RELA
,
7642 AArch64_relocate
, gold::Default_comdat_behavior
>(
7648 needs_special_offset_handling
,
7652 reloc_symbol_changes
);
7655 // Return the size of a relocation while scanning during a relocatable
7658 template<int size
, bool big_endian
>
7660 Target_aarch64
<size
, big_endian
>::Relocatable_size_for_reloc::
7665 // We will never support SHT_REL relocations.
7670 // Scan the relocs during a relocatable link.
7672 template<int size
, bool big_endian
>
7674 Target_aarch64
<size
, big_endian
>::scan_relocatable_relocs(
7675 Symbol_table
* symtab
,
7677 Sized_relobj_file
<size
, big_endian
>* object
,
7678 unsigned int data_shndx
,
7679 unsigned int sh_type
,
7680 const unsigned char* prelocs
,
7682 Output_section
* output_section
,
7683 bool needs_special_offset_handling
,
7684 size_t local_symbol_count
,
7685 const unsigned char* plocal_symbols
,
7686 Relocatable_relocs
* rr
)
7688 gold_assert(sh_type
== elfcpp::SHT_RELA
);
7690 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
7691 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
7693 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
7694 Scan_relocatable_relocs
>(
7702 needs_special_offset_handling
,
7708 // Relocate a section during a relocatable link.
7710 template<int size
, bool big_endian
>
7712 Target_aarch64
<size
, big_endian
>::relocate_relocs(
7713 const Relocate_info
<size
, big_endian
>* relinfo
,
7714 unsigned int sh_type
,
7715 const unsigned char* prelocs
,
7717 Output_section
* output_section
,
7718 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
7719 const Relocatable_relocs
* rr
,
7720 unsigned char* view
,
7721 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
7722 section_size_type view_size
,
7723 unsigned char* reloc_view
,
7724 section_size_type reloc_view_size
)
7726 gold_assert(sh_type
== elfcpp::SHT_RELA
);
7728 gold::relocate_relocs
<size
, big_endian
, elfcpp::SHT_RELA
>(
7733 offset_in_output_section
,
7743 // Return whether this is a 3-insn erratum sequence.
7745 template<int size
, bool big_endian
>
7747 Target_aarch64
<size
, big_endian
>::is_erratum_843419_sequence(
7748 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn1
,
7749 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn2
,
7750 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn3
)
7755 // The 2nd insn is a single register load or store; or register pair
7757 if (Insn_utilities::aarch64_mem_op_p(insn2
, &rt1
, &rt2
, &pair
, &load
)
7758 && (!pair
|| (pair
&& !load
)))
7760 // The 3rd insn is a load or store instruction from the "Load/store
7761 // register (unsigned immediate)" encoding class, using Rn as the
7762 // base address register.
7763 if (Insn_utilities::aarch64_ldst_uimm(insn3
)
7764 && (Insn_utilities::aarch64_rn(insn3
)
7765 == Insn_utilities::aarch64_rd(insn1
)))
7772 // Return whether this is a 835769 sequence.
7773 // (Similarly implemented as in elfnn-aarch64.c.)
7775 template<int size
, bool big_endian
>
7777 Target_aarch64
<size
, big_endian
>::is_erratum_835769_sequence(
7778 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn1
,
7779 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn2
)
7789 if (Insn_utilities::aarch64_mlxl(insn2
)
7790 && Insn_utilities::aarch64_mem_op_p (insn1
, &rt
, &rt2
, &pair
, &load
))
7792 /* Any SIMD memory op is independent of the subsequent MLA
7793 by definition of the erratum. */
7794 if (Insn_utilities::aarch64_bit(insn1
, 26))
7797 /* If not SIMD, check for integer memory ops and MLA relationship. */
7798 rn
= Insn_utilities::aarch64_rn(insn2
);
7799 ra
= Insn_utilities::aarch64_ra(insn2
);
7800 rm
= Insn_utilities::aarch64_rm(insn2
);
7802 /* If this is a load and there's a true(RAW) dependency, we are safe
7803 and this is not an erratum sequence. */
7805 (rt
== rn
|| rt
== rm
|| rt
== ra
7806 || (pair
&& (rt2
== rn
|| rt2
== rm
|| rt2
== ra
))))
7809 /* We conservatively put out stubs for all other cases (including
7818 // Helper method to create erratum stub for ST_E_843419 and ST_E_835769.
7820 template<int size
, bool big_endian
>
7822 Target_aarch64
<size
, big_endian
>::create_erratum_stub(
7823 AArch64_relobj
<size
, big_endian
>* relobj
,
7825 section_size_type erratum_insn_offset
,
7826 Address erratum_address
,
7827 typename
Insn_utilities::Insntype erratum_insn
,
7830 gold_assert(erratum_type
== ST_E_843419
|| erratum_type
== ST_E_835769
);
7831 The_stub_table
* stub_table
= relobj
->stub_table(shndx
);
7832 gold_assert(stub_table
!= NULL
);
7833 if (stub_table
->find_erratum_stub(relobj
,
7835 erratum_insn_offset
) == NULL
)
7837 const int BPI
= AArch64_insn_utilities
<big_endian
>::BYTES_PER_INSN
;
7838 The_erratum_stub
* stub
= new The_erratum_stub(
7839 relobj
, erratum_type
, shndx
, erratum_insn_offset
);
7840 stub
->set_erratum_insn(erratum_insn
);
7841 stub
->set_erratum_address(erratum_address
);
7842 // For erratum ST_E_843419 and ST_E_835769, the destination address is
7843 // always the next insn after erratum insn.
7844 stub
->set_destination_address(erratum_address
+ BPI
);
7845 stub_table
->add_erratum_stub(stub
);
7850 // Scan erratum for section SHNDX range [output_address + span_start,
7851 // output_address + span_end). Note here we do not share the code with
7852 // scan_erratum_843419_span function, because for 843419 we optimize by only
7853 // scanning the last few insns of a page, whereas for 835769, we need to scan
7856 template<int size
, bool big_endian
>
7858 Target_aarch64
<size
, big_endian
>::scan_erratum_835769_span(
7859 AArch64_relobj
<size
, big_endian
>* relobj
,
7861 const section_size_type span_start
,
7862 const section_size_type span_end
,
7863 unsigned char* input_view
,
7864 Address output_address
)
7866 typedef typename
Insn_utilities::Insntype Insntype
;
7868 const int BPI
= AArch64_insn_utilities
<big_endian
>::BYTES_PER_INSN
;
7870 // Adjust output_address and view to the start of span.
7871 output_address
+= span_start
;
7872 input_view
+= span_start
;
7874 section_size_type span_length
= span_end
- span_start
;
7875 section_size_type offset
= 0;
7876 for (offset
= 0; offset
+ BPI
< span_length
; offset
+= BPI
)
7878 Insntype
* ip
= reinterpret_cast<Insntype
*>(input_view
+ offset
);
7879 Insntype insn1
= ip
[0];
7880 Insntype insn2
= ip
[1];
7881 if (is_erratum_835769_sequence(insn1
, insn2
))
7883 Insntype erratum_insn
= insn2
;
7884 // "span_start + offset" is the offset for insn1. So for insn2, it is
7885 // "span_start + offset + BPI".
7886 section_size_type erratum_insn_offset
= span_start
+ offset
+ BPI
;
7887 Address erratum_address
= output_address
+ offset
+ BPI
;
7888 gold_warning(_("Erratum 835769 found and fixed at \"%s\", "
7889 "section %d, offset 0x%08x."),
7890 relobj
->name().c_str(), shndx
,
7891 (unsigned int)(span_start
+ offset
));
7893 this->create_erratum_stub(relobj
, shndx
,
7894 erratum_insn_offset
, erratum_address
,
7895 erratum_insn
, ST_E_835769
);
7896 offset
+= BPI
; // Skip mac insn.
7899 } // End of "Target_aarch64::scan_erratum_835769_span".
7902 // Scan erratum for section SHNDX range
7903 // [output_address + span_start, output_address + span_end).
7905 template<int size
, bool big_endian
>
7907 Target_aarch64
<size
, big_endian
>::scan_erratum_843419_span(
7908 AArch64_relobj
<size
, big_endian
>* relobj
,
7910 const section_size_type span_start
,
7911 const section_size_type span_end
,
7912 unsigned char* input_view
,
7913 Address output_address
)
7915 typedef typename
Insn_utilities::Insntype Insntype
;
7917 // Adjust output_address and view to the start of span.
7918 output_address
+= span_start
;
7919 input_view
+= span_start
;
7921 if ((output_address
& 0x03) != 0)
7924 section_size_type offset
= 0;
7925 section_size_type span_length
= span_end
- span_start
;
7926 // The first instruction must be ending at 0xFF8 or 0xFFC.
7927 unsigned int page_offset
= output_address
& 0xFFF;
7928 // Make sure starting position, that is "output_address+offset",
7929 // starts at page position 0xff8 or 0xffc.
7930 if (page_offset
< 0xff8)
7931 offset
= 0xff8 - page_offset
;
7932 while (offset
+ 3 * Insn_utilities::BYTES_PER_INSN
<= span_length
)
7934 Insntype
* ip
= reinterpret_cast<Insntype
*>(input_view
+ offset
);
7935 Insntype insn1
= ip
[0];
7936 if (Insn_utilities::is_adrp(insn1
))
7938 Insntype insn2
= ip
[1];
7939 Insntype insn3
= ip
[2];
7940 Insntype erratum_insn
;
7941 unsigned insn_offset
;
7942 bool do_report
= false;
7943 if (is_erratum_843419_sequence(insn1
, insn2
, insn3
))
7946 erratum_insn
= insn3
;
7947 insn_offset
= 2 * Insn_utilities::BYTES_PER_INSN
;
7949 else if (offset
+ 4 * Insn_utilities::BYTES_PER_INSN
<= span_length
)
7951 // Optionally we can have an insn between ins2 and ins3
7952 Insntype insn_opt
= ip
[2];
7953 // And insn_opt must not be a branch.
7954 if (!Insn_utilities::aarch64_b(insn_opt
)
7955 && !Insn_utilities::aarch64_bl(insn_opt
)
7956 && !Insn_utilities::aarch64_blr(insn_opt
)
7957 && !Insn_utilities::aarch64_br(insn_opt
))
7959 // And insn_opt must not write to dest reg in insn1. However
7960 // we do a conservative scan, which means we may fix/report
7961 // more than necessary, but it doesn't hurt.
7963 Insntype insn4
= ip
[3];
7964 if (is_erratum_843419_sequence(insn1
, insn2
, insn4
))
7967 erratum_insn
= insn4
;
7968 insn_offset
= 3 * Insn_utilities::BYTES_PER_INSN
;
7974 gold_warning(_("Erratum 843419 found and fixed at \"%s\", "
7975 "section %d, offset 0x%08x."),
7976 relobj
->name().c_str(), shndx
,
7977 (unsigned int)(span_start
+ offset
));
7978 unsigned int erratum_insn_offset
=
7979 span_start
+ offset
+ insn_offset
;
7980 Address erratum_address
=
7981 output_address
+ offset
+ insn_offset
;
7982 create_erratum_stub(relobj
, shndx
,
7983 erratum_insn_offset
, erratum_address
,
7984 erratum_insn
, ST_E_843419
);
7988 // Advance to next candidate instruction. We only consider instruction
7989 // sequences starting at a page offset of 0xff8 or 0xffc.
7990 page_offset
= (output_address
+ offset
) & 0xfff;
7991 if (page_offset
== 0xff8)
7993 else // (page_offset == 0xffc), we move to next page's 0xff8.
7996 } // End of "Target_aarch64::scan_erratum_843419_span".
7999 // The selector for aarch64 object files.
8001 template<int size
, bool big_endian
>
8002 class Target_selector_aarch64
: public Target_selector
8005 Target_selector_aarch64();
8008 do_instantiate_target()
8009 { return new Target_aarch64
<size
, big_endian
>(); }
8013 Target_selector_aarch64
<32, true>::Target_selector_aarch64()
8014 : Target_selector(elfcpp::EM_AARCH64
, 32, true,
8015 "elf32-bigaarch64", "aarch64_elf32_be_vec")
8019 Target_selector_aarch64
<32, false>::Target_selector_aarch64()
8020 : Target_selector(elfcpp::EM_AARCH64
, 32, false,
8021 "elf32-littleaarch64", "aarch64_elf32_le_vec")
8025 Target_selector_aarch64
<64, true>::Target_selector_aarch64()
8026 : Target_selector(elfcpp::EM_AARCH64
, 64, true,
8027 "elf64-bigaarch64", "aarch64_elf64_be_vec")
8031 Target_selector_aarch64
<64, false>::Target_selector_aarch64()
8032 : Target_selector(elfcpp::EM_AARCH64
, 64, false,
8033 "elf64-littleaarch64", "aarch64_elf64_le_vec")
8036 Target_selector_aarch64
<32, true> target_selector_aarch64elf32b
;
8037 Target_selector_aarch64
<32, false> target_selector_aarch64elf32
;
8038 Target_selector_aarch64
<64, true> target_selector_aarch64elfb
;
8039 Target_selector_aarch64
<64, false> target_selector_aarch64elf
;
8041 } // End anonymous namespace.