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_adr(const Insntype insn
)
107 { return (insn
& 0x9F000000) == 0x10000000; }
110 is_adrp(const Insntype insn
)
111 { return (insn
& 0x9F000000) == 0x90000000; }
114 aarch64_rm(const Insntype insn
)
115 { return aarch64_bits(insn
, 16, 5); }
118 aarch64_rn(const Insntype insn
)
119 { return aarch64_bits(insn
, 5, 5); }
122 aarch64_rd(const Insntype insn
)
123 { return aarch64_bits(insn
, 0, 5); }
126 aarch64_rt(const Insntype insn
)
127 { return aarch64_bits(insn
, 0, 5); }
130 aarch64_rt2(const Insntype insn
)
131 { return aarch64_bits(insn
, 10, 5); }
133 // Encode imm21 into adr. Signed imm21 is in the range of [-1M, 1M).
135 aarch64_adr_encode_imm(Insntype adr
, int imm21
)
137 gold_assert(is_adr(adr
));
138 gold_assert(-(1 << 20) <= imm21
&& imm21
< (1 << 20));
139 const int mask19
= (1 << 19) - 1;
141 adr
&= ~((mask19
<< 5) | (mask2
<< 29));
142 adr
|= ((imm21
& mask2
) << 29) | (((imm21
>> 2) & mask19
) << 5);
146 // Retrieve encoded adrp 33-bit signed imm value. This value is obtained by
147 // 21-bit signed imm encoded in the insn multiplied by 4k (page size) and
148 // 64-bit sign-extended, resulting in [-4G, 4G) with 12-lsb being 0.
150 aarch64_adrp_decode_imm(const Insntype adrp
)
152 const int mask19
= (1 << 19) - 1;
154 gold_assert(is_adrp(adrp
));
155 // 21-bit imm encoded in adrp.
156 uint64_t imm
= ((adrp
>> 29) & mask2
) | (((adrp
>> 5) & mask19
) << 2);
157 // Retrieve msb of 21-bit-signed imm for sign extension.
158 uint64_t msbt
= (imm
>> 20) & 1;
159 // Real value is imm multipled by 4k. Value now has 33-bit information.
160 int64_t value
= imm
<< 12;
161 // Sign extend to 64-bit by repeating msbt 31 (64-33) times and merge it
163 return ((((uint64_t)(1) << 32) - msbt
) << 33) | value
;
167 aarch64_b(const Insntype insn
)
168 { return (insn
& 0xFC000000) == 0x14000000; }
171 aarch64_bl(const Insntype insn
)
172 { return (insn
& 0xFC000000) == 0x94000000; }
175 aarch64_blr(const Insntype insn
)
176 { return (insn
& 0xFFFFFC1F) == 0xD63F0000; }
179 aarch64_br(const Insntype insn
)
180 { return (insn
& 0xFFFFFC1F) == 0xD61F0000; }
182 // All ld/st ops. See C4-182 of the ARM ARM. The encoding space for
183 // LD_PCREL, LDST_RO, LDST_UI and LDST_UIMM cover prefetch ops.
185 aarch64_ld(Insntype insn
) { return aarch64_bit(insn
, 22) == 1; }
188 aarch64_ldst(Insntype insn
)
189 { return (insn
& 0x0a000000) == 0x08000000; }
192 aarch64_ldst_ex(Insntype insn
)
193 { return (insn
& 0x3f000000) == 0x08000000; }
196 aarch64_ldst_pcrel(Insntype insn
)
197 { return (insn
& 0x3b000000) == 0x18000000; }
200 aarch64_ldst_nap(Insntype insn
)
201 { return (insn
& 0x3b800000) == 0x28000000; }
204 aarch64_ldstp_pi(Insntype insn
)
205 { return (insn
& 0x3b800000) == 0x28800000; }
208 aarch64_ldstp_o(Insntype insn
)
209 { return (insn
& 0x3b800000) == 0x29000000; }
212 aarch64_ldstp_pre(Insntype insn
)
213 { return (insn
& 0x3b800000) == 0x29800000; }
216 aarch64_ldst_ui(Insntype insn
)
217 { return (insn
& 0x3b200c00) == 0x38000000; }
220 aarch64_ldst_piimm(Insntype insn
)
221 { return (insn
& 0x3b200c00) == 0x38000400; }
224 aarch64_ldst_u(Insntype insn
)
225 { return (insn
& 0x3b200c00) == 0x38000800; }
228 aarch64_ldst_preimm(Insntype insn
)
229 { return (insn
& 0x3b200c00) == 0x38000c00; }
232 aarch64_ldst_ro(Insntype insn
)
233 { return (insn
& 0x3b200c00) == 0x38200800; }
236 aarch64_ldst_uimm(Insntype insn
)
237 { return (insn
& 0x3b000000) == 0x39000000; }
240 aarch64_ldst_simd_m(Insntype insn
)
241 { return (insn
& 0xbfbf0000) == 0x0c000000; }
244 aarch64_ldst_simd_m_pi(Insntype insn
)
245 { return (insn
& 0xbfa00000) == 0x0c800000; }
248 aarch64_ldst_simd_s(Insntype insn
)
249 { return (insn
& 0xbf9f0000) == 0x0d000000; }
252 aarch64_ldst_simd_s_pi(Insntype insn
)
253 { return (insn
& 0xbf800000) == 0x0d800000; }
255 // Classify an INSN if it is indeed a load/store. Return true if INSN is a
256 // LD/ST instruction otherwise return false. For scalar LD/ST instructions
257 // PAIR is FALSE, RT is returned and RT2 is set equal to RT. For LD/ST pair
258 // instructions PAIR is TRUE, RT and RT2 are returned.
260 aarch64_mem_op_p(Insntype insn
, unsigned int *rt
, unsigned int *rt2
,
261 bool *pair
, bool *load
)
269 /* Bail out quickly if INSN doesn't fall into the the load-store
271 if (!aarch64_ldst (insn
))
276 if (aarch64_ldst_ex (insn
))
278 *rt
= aarch64_rt (insn
);
280 if (aarch64_bit (insn
, 21) == 1)
283 *rt2
= aarch64_rt2 (insn
);
285 *load
= aarch64_ld (insn
);
288 else if (aarch64_ldst_nap (insn
)
289 || aarch64_ldstp_pi (insn
)
290 || aarch64_ldstp_o (insn
)
291 || aarch64_ldstp_pre (insn
))
294 *rt
= aarch64_rt (insn
);
295 *rt2
= aarch64_rt2 (insn
);
296 *load
= aarch64_ld (insn
);
299 else if (aarch64_ldst_pcrel (insn
)
300 || aarch64_ldst_ui (insn
)
301 || aarch64_ldst_piimm (insn
)
302 || aarch64_ldst_u (insn
)
303 || aarch64_ldst_preimm (insn
)
304 || aarch64_ldst_ro (insn
)
305 || aarch64_ldst_uimm (insn
))
307 *rt
= aarch64_rt (insn
);
309 if (aarch64_ldst_pcrel (insn
))
311 opc
= aarch64_bits (insn
, 22, 2);
312 v
= aarch64_bit (insn
, 26);
313 opc_v
= opc
| (v
<< 2);
314 *load
= (opc_v
== 1 || opc_v
== 2 || opc_v
== 3
315 || opc_v
== 5 || opc_v
== 7);
318 else if (aarch64_ldst_simd_m (insn
)
319 || aarch64_ldst_simd_m_pi (insn
))
321 *rt
= aarch64_rt (insn
);
322 *load
= aarch64_bit (insn
, 22);
323 opcode
= (insn
>> 12) & 0xf;
350 else if (aarch64_ldst_simd_s (insn
)
351 || aarch64_ldst_simd_s_pi (insn
))
353 *rt
= aarch64_rt (insn
);
354 r
= (insn
>> 21) & 1;
355 *load
= aarch64_bit (insn
, 22);
356 opcode
= (insn
>> 13) & 0x7;
368 *rt2
= *rt
+ (r
== 0 ? 2 : 3);
376 *rt2
= *rt
+ (r
== 0 ? 2 : 3);
385 } // End of "aarch64_mem_op_p".
387 // Return true if INSN is mac insn.
389 aarch64_mac(Insntype insn
)
390 { return (insn
& 0xff000000) == 0x9b000000; }
392 // Return true if INSN is multiply-accumulate.
393 // (This is similar to implementaton in elfnn-aarch64.c.)
395 aarch64_mlxl(Insntype insn
)
397 uint32_t op31
= aarch64_op31(insn
);
398 if (aarch64_mac(insn
)
399 && (op31
== 0 || op31
== 1 || op31
== 5)
400 /* Exclude MUL instructions which are encoded as a multiple-accumulate
402 && aarch64_ra(insn
) != AARCH64_ZR
)
408 }; // End of "AArch64_insn_utilities".
411 // Insn length in byte.
413 template<bool big_endian
>
414 const int AArch64_insn_utilities
<big_endian
>::BYTES_PER_INSN
= 4;
417 // Zero register encoding - 31.
419 template<bool big_endian
>
420 const unsigned int AArch64_insn_utilities
<big_endian
>::AARCH64_ZR
= 0x1f;
423 // Output_data_got_aarch64 class.
425 template<int size
, bool big_endian
>
426 class Output_data_got_aarch64
: public Output_data_got
<size
, big_endian
>
429 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
430 Output_data_got_aarch64(Symbol_table
* symtab
, Layout
* layout
)
431 : Output_data_got
<size
, big_endian
>(),
432 symbol_table_(symtab
), layout_(layout
)
435 // Add a static entry for the GOT entry at OFFSET. GSYM is a global
436 // symbol and R_TYPE is the code of a dynamic relocation that needs to be
437 // applied in a static link.
439 add_static_reloc(unsigned int got_offset
, unsigned int r_type
, Symbol
* gsym
)
440 { this->static_relocs_
.push_back(Static_reloc(got_offset
, r_type
, gsym
)); }
443 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
444 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
445 // relocation that needs to be applied in a static link.
447 add_static_reloc(unsigned int got_offset
, unsigned int r_type
,
448 Sized_relobj_file
<size
, big_endian
>* relobj
,
451 this->static_relocs_
.push_back(Static_reloc(got_offset
, r_type
, relobj
,
457 // Write out the GOT table.
459 do_write(Output_file
* of
) {
460 // The first entry in the GOT is the address of the .dynamic section.
461 gold_assert(this->data_size() >= size
/ 8);
462 Output_section
* dynamic
= this->layout_
->dynamic_section();
463 Valtype dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
464 this->replace_constant(0, dynamic_addr
);
465 Output_data_got
<size
, big_endian
>::do_write(of
);
467 // Handling static relocs
468 if (this->static_relocs_
.empty())
471 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
473 gold_assert(parameters
->doing_static_link());
474 const off_t offset
= this->offset();
475 const section_size_type oview_size
=
476 convert_to_section_size_type(this->data_size());
477 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
479 Output_segment
* tls_segment
= this->layout_
->tls_segment();
480 gold_assert(tls_segment
!= NULL
);
482 AArch64_address aligned_tcb_address
=
483 align_address(Target_aarch64
<size
, big_endian
>::TCB_SIZE
,
484 tls_segment
->maximum_alignment());
486 for (size_t i
= 0; i
< this->static_relocs_
.size(); ++i
)
488 Static_reloc
& reloc(this->static_relocs_
[i
]);
489 AArch64_address value
;
491 if (!reloc
.symbol_is_global())
493 Sized_relobj_file
<size
, big_endian
>* object
= reloc
.relobj();
494 const Symbol_value
<size
>* psymval
=
495 reloc
.relobj()->local_symbol(reloc
.index());
497 // We are doing static linking. Issue an error and skip this
498 // relocation if the symbol is undefined or in a discarded_section.
500 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
501 if ((shndx
== elfcpp::SHN_UNDEF
)
503 && shndx
!= elfcpp::SHN_UNDEF
504 && !object
->is_section_included(shndx
)
505 && !this->symbol_table_
->is_section_folded(object
, shndx
)))
507 gold_error(_("undefined or discarded local symbol %u from "
508 " object %s in GOT"),
509 reloc
.index(), reloc
.relobj()->name().c_str());
512 value
= psymval
->value(object
, 0);
516 const Symbol
* gsym
= reloc
.symbol();
517 gold_assert(gsym
!= NULL
);
518 if (gsym
->is_forwarder())
519 gsym
= this->symbol_table_
->resolve_forwards(gsym
);
521 // We are doing static linking. Issue an error and skip this
522 // relocation if the symbol is undefined or in a discarded_section
523 // unless it is a weakly_undefined symbol.
524 if ((gsym
->is_defined_in_discarded_section()
525 || gsym
->is_undefined())
526 && !gsym
->is_weak_undefined())
528 gold_error(_("undefined or discarded symbol %s in GOT"),
533 if (!gsym
->is_weak_undefined())
535 const Sized_symbol
<size
>* sym
=
536 static_cast<const Sized_symbol
<size
>*>(gsym
);
537 value
= sym
->value();
543 unsigned got_offset
= reloc
.got_offset();
544 gold_assert(got_offset
< oview_size
);
546 typedef typename
elfcpp::Swap
<size
, big_endian
>::Valtype Valtype
;
547 Valtype
* wv
= reinterpret_cast<Valtype
*>(oview
+ got_offset
);
549 switch (reloc
.r_type())
551 case elfcpp::R_AARCH64_TLS_DTPREL64
:
554 case elfcpp::R_AARCH64_TLS_TPREL64
:
555 x
= value
+ aligned_tcb_address
;
560 elfcpp::Swap
<size
, big_endian
>::writeval(wv
, x
);
563 of
->write_output_view(offset
, oview_size
, oview
);
567 // Symbol table of the output object.
568 Symbol_table
* symbol_table_
;
569 // A pointer to the Layout class, so that we can find the .dynamic
570 // section when we write out the GOT section.
573 // This class represent dynamic relocations that need to be applied by
574 // gold because we are using TLS relocations in a static link.
578 Static_reloc(unsigned int got_offset
, unsigned int r_type
, Symbol
* gsym
)
579 : got_offset_(got_offset
), r_type_(r_type
), symbol_is_global_(true)
580 { this->u_
.global
.symbol
= gsym
; }
582 Static_reloc(unsigned int got_offset
, unsigned int r_type
,
583 Sized_relobj_file
<size
, big_endian
>* relobj
, unsigned int index
)
584 : got_offset_(got_offset
), r_type_(r_type
), symbol_is_global_(false)
586 this->u_
.local
.relobj
= relobj
;
587 this->u_
.local
.index
= index
;
590 // Return the GOT offset.
593 { return this->got_offset_
; }
598 { return this->r_type_
; }
600 // Whether the symbol is global or not.
602 symbol_is_global() const
603 { return this->symbol_is_global_
; }
605 // For a relocation against a global symbol, the global symbol.
609 gold_assert(this->symbol_is_global_
);
610 return this->u_
.global
.symbol
;
613 // For a relocation against a local symbol, the defining object.
614 Sized_relobj_file
<size
, big_endian
>*
617 gold_assert(!this->symbol_is_global_
);
618 return this->u_
.local
.relobj
;
621 // For a relocation against a local symbol, the local symbol index.
625 gold_assert(!this->symbol_is_global_
);
626 return this->u_
.local
.index
;
630 // GOT offset of the entry to which this relocation is applied.
631 unsigned int got_offset_
;
632 // Type of relocation.
633 unsigned int r_type_
;
634 // Whether this relocation is against a global symbol.
635 bool symbol_is_global_
;
636 // A global or local symbol.
641 // For a global symbol, the symbol itself.
646 // For a local symbol, the object defining the symbol.
647 Sized_relobj_file
<size
, big_endian
>* relobj
;
648 // For a local symbol, the symbol index.
652 }; // End of inner class Static_reloc
654 std::vector
<Static_reloc
> static_relocs_
;
655 }; // End of Output_data_got_aarch64
658 template<int size
, bool big_endian
>
659 class AArch64_input_section
;
662 template<int size
, bool big_endian
>
663 class AArch64_output_section
;
666 template<int size
, bool big_endian
>
667 class AArch64_relobj
;
670 // Stub type enum constants.
676 // Using adrp/add pair, 4 insns (including alignment) without mem access,
677 // the fastest stub. This has a limited jump distance, which is tested by
678 // aarch64_valid_for_adrp_p.
681 // Using ldr-absolute-address/br-register, 4 insns with 1 mem access,
682 // unlimited in jump distance.
683 ST_LONG_BRANCH_ABS
= 2,
685 // Using ldr/calculate-pcrel/jump, 8 insns (including alignment) with 1
686 // mem access, slowest one. Only used in position independent executables.
687 ST_LONG_BRANCH_PCREL
= 3,
689 // Stub for erratum 843419 handling.
692 // Stub for erratum 835769 handling.
695 // Number of total stub types.
700 // Struct that wraps insns for a particular stub. All stub templates are
701 // created/initialized as constants by Stub_template_repertoire.
703 template<bool big_endian
>
706 const typename AArch64_insn_utilities
<big_endian
>::Insntype
* insns
;
711 // Simple singleton class that creates/initializes/stores all types of stub
714 template<bool big_endian
>
715 class Stub_template_repertoire
718 typedef typename AArch64_insn_utilities
<big_endian
>::Insntype Insntype
;
720 // Single static method to get stub template for a given stub type.
721 static const Stub_template
<big_endian
>*
722 get_stub_template(int type
)
724 static Stub_template_repertoire
<big_endian
> singleton
;
725 return singleton
.stub_templates_
[type
];
729 // Constructor - creates/initializes all stub templates.
730 Stub_template_repertoire();
731 ~Stub_template_repertoire()
734 // Disallowing copy ctor and copy assignment operator.
735 Stub_template_repertoire(Stub_template_repertoire
&);
736 Stub_template_repertoire
& operator=(Stub_template_repertoire
&);
738 // Data that stores all insn templates.
739 const Stub_template
<big_endian
>* stub_templates_
[ST_NUMBER
];
740 }; // End of "class Stub_template_repertoire".
743 // Constructor - creates/initilizes all stub templates.
745 template<bool big_endian
>
746 Stub_template_repertoire
<big_endian
>::Stub_template_repertoire()
748 // Insn array definitions.
749 const static Insntype ST_NONE_INSNS
[] = {};
751 const static Insntype ST_ADRP_BRANCH_INSNS
[] =
753 0x90000010, /* adrp ip0, X */
754 /* ADR_PREL_PG_HI21(X) */
755 0x91000210, /* add ip0, ip0, :lo12:X */
756 /* ADD_ABS_LO12_NC(X) */
757 0xd61f0200, /* br ip0 */
758 0x00000000, /* alignment padding */
761 const static Insntype ST_LONG_BRANCH_ABS_INSNS
[] =
763 0x58000050, /* ldr ip0, 0x8 */
764 0xd61f0200, /* br ip0 */
765 0x00000000, /* address field */
766 0x00000000, /* address fields */
769 const static Insntype ST_LONG_BRANCH_PCREL_INSNS
[] =
771 0x58000090, /* ldr ip0, 0x10 */
772 0x10000011, /* adr ip1, #0 */
773 0x8b110210, /* add ip0, ip0, ip1 */
774 0xd61f0200, /* br ip0 */
775 0x00000000, /* address field */
776 0x00000000, /* address field */
777 0x00000000, /* alignment padding */
778 0x00000000, /* alignment padding */
781 const static Insntype ST_E_843419_INSNS
[] =
783 0x00000000, /* Placeholder for erratum insn. */
784 0x14000000, /* b <label> */
787 // ST_E_835769 has the same stub template as ST_E_843419.
788 const static Insntype
* ST_E_835769_INSNS
= ST_E_843419_INSNS
;
790 #define install_insn_template(T) \
791 const static Stub_template<big_endian> template_##T = { \
792 T##_INSNS, sizeof(T##_INSNS) / sizeof(T##_INSNS[0]) }; \
793 this->stub_templates_[T] = &template_##T
795 install_insn_template(ST_NONE
);
796 install_insn_template(ST_ADRP_BRANCH
);
797 install_insn_template(ST_LONG_BRANCH_ABS
);
798 install_insn_template(ST_LONG_BRANCH_PCREL
);
799 install_insn_template(ST_E_843419
);
800 install_insn_template(ST_E_835769
);
802 #undef install_insn_template
806 // Base class for stubs.
808 template<int size
, bool big_endian
>
812 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
813 typedef typename AArch64_insn_utilities
<big_endian
>::Insntype Insntype
;
815 static const AArch64_address invalid_address
=
816 static_cast<AArch64_address
>(-1);
818 static const section_offset_type invalid_offset
=
819 static_cast<section_offset_type
>(-1);
822 : destination_address_(invalid_address
),
823 offset_(invalid_offset
),
833 { return this->type_
; }
835 // Get stub template that provides stub insn information.
836 const Stub_template
<big_endian
>*
837 stub_template() const
839 return Stub_template_repertoire
<big_endian
>::
840 get_stub_template(this->type());
843 // Get destination address.
845 destination_address() const
847 gold_assert(this->destination_address_
!= this->invalid_address
);
848 return this->destination_address_
;
851 // Set destination address.
853 set_destination_address(AArch64_address address
)
855 gold_assert(address
!= this->invalid_address
);
856 this->destination_address_
= address
;
859 // Reset the destination address.
861 reset_destination_address()
862 { this->destination_address_
= this->invalid_address
; }
864 // Get offset of code stub. For Reloc_stub, it is the offset from the
865 // beginning of its containing stub table; for Erratum_stub, it is the offset
866 // from the end of reloc_stubs.
870 gold_assert(this->offset_
!= this->invalid_offset
);
871 return this->offset_
;
876 set_offset(section_offset_type offset
)
877 { this->offset_
= offset
; }
879 // Return the stub insn.
882 { return this->stub_template()->insns
; }
884 // Return num of stub insns.
887 { return this->stub_template()->insn_num
; }
889 // Get size of the stub.
893 return this->insn_num() *
894 AArch64_insn_utilities
<big_endian
>::BYTES_PER_INSN
;
897 // Write stub to output file.
899 write(unsigned char* view
, section_size_type view_size
)
900 { this->do_write(view
, view_size
); }
903 // Abstract method to be implemented by sub-classes.
905 do_write(unsigned char*, section_size_type
) = 0;
908 // The last insn of a stub is a jump to destination insn. This field records
909 // the destination address.
910 AArch64_address destination_address_
;
911 // The stub offset. Note this has difference interpretations between an
912 // Reloc_stub and an Erratum_stub. For Reloc_stub this is the offset from the
913 // beginning of the containing stub_table, whereas for Erratum_stub, this is
914 // the offset from the end of reloc_stubs.
915 section_offset_type offset_
;
918 }; // End of "Stub_base".
921 // Erratum stub class. An erratum stub differs from a reloc stub in that for
922 // each erratum occurrence, we generate an erratum stub. We never share erratum
923 // stubs, whereas for reloc stubs, different branches insns share a single reloc
924 // stub as long as the branch targets are the same. (More to the point, reloc
925 // stubs can be shared because they're used to reach a specific target, whereas
926 // erratum stubs branch back to the original control flow.)
928 template<int size
, bool big_endian
>
929 class Erratum_stub
: public Stub_base
<size
, big_endian
>
932 typedef AArch64_relobj
<size
, big_endian
> The_aarch64_relobj
;
933 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
934 typedef AArch64_insn_utilities
<big_endian
> Insn_utilities
;
935 typedef typename AArch64_insn_utilities
<big_endian
>::Insntype Insntype
;
937 static const int STUB_ADDR_ALIGN
;
939 static const Insntype invalid_insn
= static_cast<Insntype
>(-1);
941 Erratum_stub(The_aarch64_relobj
* relobj
, int type
,
942 unsigned shndx
, unsigned int sh_offset
)
943 : Stub_base
<size
, big_endian
>(type
), relobj_(relobj
),
944 shndx_(shndx
), sh_offset_(sh_offset
),
945 erratum_insn_(invalid_insn
),
946 erratum_address_(this->invalid_address
)
951 // Return the object that contains the erratum.
954 { return this->relobj_
; }
956 // Get section index of the erratum.
959 { return this->shndx_
; }
961 // Get section offset of the erratum.
964 { return this->sh_offset_
; }
966 // Get the erratum insn. This is the insn located at erratum_insn_address.
970 gold_assert(this->erratum_insn_
!= this->invalid_insn
);
971 return this->erratum_insn_
;
974 // Set the insn that the erratum happens to.
976 set_erratum_insn(Insntype insn
)
977 { this->erratum_insn_
= insn
; }
979 // For 843419, the erratum insn is ld/st xt, [xn, #uimm], which may be a
980 // relocation spot, in this case, the erratum_insn_ recorded at scanning phase
981 // is no longer the one we want to write out to the stub, update erratum_insn_
982 // with relocated version. Also note that in this case xn must not be "PC", so
983 // it is safe to move the erratum insn from the origin place to the stub. For
984 // 835769, the erratum insn is multiply-accumulate insn, which could not be a
985 // relocation spot (assertion added though).
987 update_erratum_insn(Insntype insn
)
989 gold_assert(this->erratum_insn_
!= this->invalid_insn
);
990 switch (this->type())
993 gold_assert(Insn_utilities::aarch64_ldst_uimm(insn
));
994 gold_assert(Insn_utilities::aarch64_ldst_uimm(this->erratum_insn()));
995 gold_assert(Insn_utilities::aarch64_rd(insn
) ==
996 Insn_utilities::aarch64_rd(this->erratum_insn()));
997 gold_assert(Insn_utilities::aarch64_rn(insn
) ==
998 Insn_utilities::aarch64_rn(this->erratum_insn()));
999 // Update plain ld/st insn with relocated insn.
1000 this->erratum_insn_
= insn
;
1003 gold_assert(insn
== this->erratum_insn());
1011 // Return the address where an erratum must be done.
1013 erratum_address() const
1015 gold_assert(this->erratum_address_
!= this->invalid_address
);
1016 return this->erratum_address_
;
1019 // Set the address where an erratum must be done.
1021 set_erratum_address(AArch64_address addr
)
1022 { this->erratum_address_
= addr
; }
1024 // Comparator used to group Erratum_stubs in a set by (obj, shndx,
1025 // sh_offset). We do not include 'type' in the calculation, becuase there is
1026 // at most one stub type at (obj, shndx, sh_offset).
1028 operator<(const Erratum_stub
<size
, big_endian
>& k
) const
1032 // We group stubs by relobj.
1033 if (this->relobj_
!= k
.relobj_
)
1034 return this->relobj_
< k
.relobj_
;
1035 // Then by section index.
1036 if (this->shndx_
!= k
.shndx_
)
1037 return this->shndx_
< k
.shndx_
;
1038 // Lastly by section offset.
1039 return this->sh_offset_
< k
.sh_offset_
;
1044 do_write(unsigned char*, section_size_type
);
1047 // The object that needs to be fixed.
1048 The_aarch64_relobj
* relobj_
;
1049 // The shndx in the object that needs to be fixed.
1050 const unsigned int shndx_
;
1051 // The section offset in the obejct that needs to be fixed.
1052 const unsigned int sh_offset_
;
1053 // The insn to be fixed.
1054 Insntype erratum_insn_
;
1055 // The address of the above insn.
1056 AArch64_address erratum_address_
;
1057 }; // End of "Erratum_stub".
1060 // Erratum sub class to wrap additional info needed by 843419. In fixing this
1061 // erratum, we may choose to replace 'adrp' with 'adr', in this case, we need
1062 // adrp's code position (two or three insns before erratum insn itself).
1064 template<int size
, bool big_endian
>
1065 class E843419_stub
: public Erratum_stub
<size
, big_endian
>
1068 typedef typename AArch64_insn_utilities
<big_endian
>::Insntype Insntype
;
1070 E843419_stub(AArch64_relobj
<size
, big_endian
>* relobj
,
1071 unsigned int shndx
, unsigned int sh_offset
,
1072 unsigned int adrp_sh_offset
)
1073 : Erratum_stub
<size
, big_endian
>(relobj
, ST_E_843419
, shndx
, sh_offset
),
1074 adrp_sh_offset_(adrp_sh_offset
)
1078 adrp_sh_offset() const
1079 { return this->adrp_sh_offset_
; }
1082 // Section offset of "adrp". (We do not need a "adrp_shndx_" field, because we
1083 // can can obtain it from its parent.)
1084 const unsigned int adrp_sh_offset_
;
1088 template<int size
, bool big_endian
>
1089 const int Erratum_stub
<size
, big_endian
>::STUB_ADDR_ALIGN
= 4;
1091 // Comparator used in set definition.
1092 template<int size
, bool big_endian
>
1093 struct Erratum_stub_less
1096 operator()(const Erratum_stub
<size
, big_endian
>* s1
,
1097 const Erratum_stub
<size
, big_endian
>* s2
) const
1098 { return *s1
< *s2
; }
1101 // Erratum_stub implementation for writing stub to output file.
1103 template<int size
, bool big_endian
>
1105 Erratum_stub
<size
, big_endian
>::do_write(unsigned char* view
, section_size_type
)
1107 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
1108 const Insntype
* insns
= this->insns();
1109 uint32_t num_insns
= this->insn_num();
1110 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
1111 // For current implemented erratum 843419 and 835769, the first insn in the
1112 // stub is always a copy of the problematic insn (in 843419, the mem access
1113 // insn, in 835769, the mac insn), followed by a jump-back.
1114 elfcpp::Swap
<32, big_endian
>::writeval(ip
, this->erratum_insn());
1115 for (uint32_t i
= 1; i
< num_insns
; ++i
)
1116 elfcpp::Swap
<32, big_endian
>::writeval(ip
+ i
, insns
[i
]);
1120 // Reloc stub class.
1122 template<int size
, bool big_endian
>
1123 class Reloc_stub
: public Stub_base
<size
, big_endian
>
1126 typedef Reloc_stub
<size
, big_endian
> This
;
1127 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
1129 // Branch range. This is used to calculate the section group size, as well as
1130 // determine whether a stub is needed.
1131 static const int MAX_BRANCH_OFFSET
= ((1 << 25) - 1) << 2;
1132 static const int MIN_BRANCH_OFFSET
= -((1 << 25) << 2);
1134 // Constant used to determine if an offset fits in the adrp instruction
1136 static const int MAX_ADRP_IMM
= (1 << 20) - 1;
1137 static const int MIN_ADRP_IMM
= -(1 << 20);
1139 static const int BYTES_PER_INSN
= 4;
1140 static const int STUB_ADDR_ALIGN
;
1142 // Determine whether the offset fits in the jump/branch instruction.
1144 aarch64_valid_branch_offset_p(int64_t offset
)
1145 { return offset
>= MIN_BRANCH_OFFSET
&& offset
<= MAX_BRANCH_OFFSET
; }
1147 // Determine whether the offset fits in the adrp immediate field.
1149 aarch64_valid_for_adrp_p(AArch64_address location
, AArch64_address dest
)
1151 typedef AArch64_relocate_functions
<size
, big_endian
> Reloc
;
1152 int64_t adrp_imm
= (Reloc::Page(dest
) - Reloc::Page(location
)) >> 12;
1153 return adrp_imm
>= MIN_ADRP_IMM
&& adrp_imm
<= MAX_ADRP_IMM
;
1156 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
1159 stub_type_for_reloc(unsigned int r_type
, AArch64_address address
,
1160 AArch64_address target
);
1162 Reloc_stub(int type
)
1163 : Stub_base
<size
, big_endian
>(type
)
1169 // The key class used to index the stub instance in the stub table's stub map.
1173 Key(int type
, const Symbol
* symbol
, const Relobj
* relobj
,
1174 unsigned int r_sym
, int32_t addend
)
1175 : type_(type
), addend_(addend
)
1179 this->r_sym_
= Reloc_stub::invalid_index
;
1180 this->u_
.symbol
= symbol
;
1184 gold_assert(relobj
!= NULL
&& r_sym
!= invalid_index
);
1185 this->r_sym_
= r_sym
;
1186 this->u_
.relobj
= relobj
;
1193 // Return stub type.
1196 { return this->type_
; }
1198 // Return the local symbol index or invalid_index.
1201 { return this->r_sym_
; }
1203 // Return the symbol if there is one.
1206 { return this->r_sym_
== invalid_index
? this->u_
.symbol
: NULL
; }
1208 // Return the relobj if there is one.
1211 { return this->r_sym_
!= invalid_index
? this->u_
.relobj
: NULL
; }
1213 // Whether this equals to another key k.
1215 eq(const Key
& k
) const
1217 return ((this->type_
== k
.type_
)
1218 && (this->r_sym_
== k
.r_sym_
)
1219 && ((this->r_sym_
!= Reloc_stub::invalid_index
)
1220 ? (this->u_
.relobj
== k
.u_
.relobj
)
1221 : (this->u_
.symbol
== k
.u_
.symbol
))
1222 && (this->addend_
== k
.addend_
));
1225 // Return a hash value.
1229 size_t name_hash_value
= gold::string_hash
<char>(
1230 (this->r_sym_
!= Reloc_stub::invalid_index
)
1231 ? this->u_
.relobj
->name().c_str()
1232 : this->u_
.symbol
->name());
1233 // We only have 4 stub types.
1234 size_t stub_type_hash_value
= 0x03 & this->type_
;
1235 return (name_hash_value
1236 ^ stub_type_hash_value
1237 ^ ((this->r_sym_
& 0x3fff) << 2)
1238 ^ ((this->addend_
& 0xffff) << 16));
1241 // Functors for STL associative containers.
1245 operator()(const Key
& k
) const
1246 { return k
.hash_value(); }
1252 operator()(const Key
& k1
, const Key
& k2
) const
1253 { return k1
.eq(k2
); }
1259 // If this is a local symbol, this is the index in the defining object.
1260 // Otherwise, it is invalid_index for a global symbol.
1261 unsigned int r_sym_
;
1262 // If r_sym_ is an invalid index, this points to a global symbol.
1263 // Otherwise, it points to a relobj. We used the unsized and target
1264 // independent Symbol and Relobj classes instead of Sized_symbol<32> and
1265 // Arm_relobj, in order to avoid making the stub class a template
1266 // as most of the stub machinery is endianness-neutral. However, it
1267 // may require a bit of casting done by users of this class.
1270 const Symbol
* symbol
;
1271 const Relobj
* relobj
;
1273 // Addend associated with a reloc.
1275 }; // End of inner class Reloc_stub::Key
1278 // This may be overridden in the child class.
1280 do_write(unsigned char*, section_size_type
);
1283 static const unsigned int invalid_index
= static_cast<unsigned int>(-1);
1284 }; // End of Reloc_stub
1286 template<int size
, bool big_endian
>
1287 const int Reloc_stub
<size
, big_endian
>::STUB_ADDR_ALIGN
= 4;
1289 // Write data to output file.
1291 template<int size
, bool big_endian
>
1293 Reloc_stub
<size
, big_endian
>::
1294 do_write(unsigned char* view
, section_size_type
)
1296 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
1297 const uint32_t* insns
= this->insns();
1298 uint32_t num_insns
= this->insn_num();
1299 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
1300 for (uint32_t i
= 0; i
< num_insns
; ++i
)
1301 elfcpp::Swap
<32, big_endian
>::writeval(ip
+ i
, insns
[i
]);
1305 // Determine the stub type for a certain relocation or ST_NONE, if no stub is
1308 template<int size
, bool big_endian
>
1310 Reloc_stub
<size
, big_endian
>::stub_type_for_reloc(
1311 unsigned int r_type
, AArch64_address location
, AArch64_address dest
)
1313 int64_t branch_offset
= 0;
1316 case elfcpp::R_AARCH64_CALL26
:
1317 case elfcpp::R_AARCH64_JUMP26
:
1318 branch_offset
= dest
- location
;
1324 if (aarch64_valid_branch_offset_p(branch_offset
))
1327 if (aarch64_valid_for_adrp_p(location
, dest
))
1328 return ST_ADRP_BRANCH
;
1330 if (parameters
->options().output_is_position_independent()
1331 && parameters
->options().output_is_executable())
1332 return ST_LONG_BRANCH_PCREL
;
1334 return ST_LONG_BRANCH_ABS
;
1337 // A class to hold stubs for the ARM target.
1339 template<int size
, bool big_endian
>
1340 class Stub_table
: public Output_data
1343 typedef Target_aarch64
<size
, big_endian
> The_target_aarch64
;
1344 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
1345 typedef AArch64_relobj
<size
, big_endian
> The_aarch64_relobj
;
1346 typedef AArch64_input_section
<size
, big_endian
> The_aarch64_input_section
;
1347 typedef Reloc_stub
<size
, big_endian
> The_reloc_stub
;
1348 typedef typename
The_reloc_stub::Key The_reloc_stub_key
;
1349 typedef Erratum_stub
<size
, big_endian
> The_erratum_stub
;
1350 typedef Erratum_stub_less
<size
, big_endian
> The_erratum_stub_less
;
1351 typedef typename
The_reloc_stub_key::hash The_reloc_stub_key_hash
;
1352 typedef typename
The_reloc_stub_key::equal_to The_reloc_stub_key_equal_to
;
1353 typedef Stub_table
<size
, big_endian
> The_stub_table
;
1354 typedef Unordered_map
<The_reloc_stub_key
, The_reloc_stub
*,
1355 The_reloc_stub_key_hash
, The_reloc_stub_key_equal_to
>
1357 typedef typename
Reloc_stub_map::const_iterator Reloc_stub_map_const_iter
;
1358 typedef Relocate_info
<size
, big_endian
> The_relocate_info
;
1360 typedef std::set
<The_erratum_stub
*, The_erratum_stub_less
> Erratum_stub_set
;
1361 typedef typename
Erratum_stub_set::iterator Erratum_stub_set_iter
;
1363 Stub_table(The_aarch64_input_section
* owner
)
1364 : Output_data(), owner_(owner
), reloc_stubs_size_(0),
1365 erratum_stubs_size_(0), prev_data_size_(0)
1371 The_aarch64_input_section
*
1375 // Whether this stub table is empty.
1378 { return reloc_stubs_
.empty() && erratum_stubs_
.empty(); }
1380 // Return the current data size.
1382 current_data_size() const
1383 { return this->current_data_size_for_child(); }
1385 // Add a STUB using KEY. The caller is responsible for avoiding addition
1386 // if a STUB with the same key has already been added.
1388 add_reloc_stub(The_reloc_stub
* stub
, const The_reloc_stub_key
& key
);
1390 // Add an erratum stub into the erratum stub set. The set is ordered by
1391 // (relobj, shndx, sh_offset).
1393 add_erratum_stub(The_erratum_stub
* stub
);
1395 // Find if such erratum exists for any given (obj, shndx, sh_offset).
1397 find_erratum_stub(The_aarch64_relobj
* a64relobj
,
1398 unsigned int shndx
, unsigned int sh_offset
);
1400 // Find all the erratums for a given input section. The return value is a pair
1401 // of iterators [begin, end).
1402 std::pair
<Erratum_stub_set_iter
, Erratum_stub_set_iter
>
1403 find_erratum_stubs_for_input_section(The_aarch64_relobj
* a64relobj
,
1404 unsigned int shndx
);
1406 // Compute the erratum stub address.
1408 erratum_stub_address(The_erratum_stub
* stub
) const
1410 AArch64_address r
= align_address(this->address() + this->reloc_stubs_size_
,
1411 The_erratum_stub::STUB_ADDR_ALIGN
);
1412 r
+= stub
->offset();
1416 // Finalize stubs. No-op here, just for completeness.
1421 // Look up a relocation stub using KEY. Return NULL if there is none.
1423 find_reloc_stub(The_reloc_stub_key
& key
)
1425 Reloc_stub_map_const_iter p
= this->reloc_stubs_
.find(key
);
1426 return (p
!= this->reloc_stubs_
.end()) ? p
->second
: NULL
;
1429 // Relocate stubs in this stub table.
1431 relocate_stubs(const The_relocate_info
*,
1432 The_target_aarch64
*,
1438 // Update data size at the end of a relaxation pass. Return true if data size
1439 // is different from that of the previous relaxation pass.
1441 update_data_size_changed_p()
1443 // No addralign changed here.
1444 off_t s
= align_address(this->reloc_stubs_size_
,
1445 The_erratum_stub::STUB_ADDR_ALIGN
)
1446 + this->erratum_stubs_size_
;
1447 bool changed
= (s
!= this->prev_data_size_
);
1448 this->prev_data_size_
= s
;
1453 // Write out section contents.
1455 do_write(Output_file
*);
1457 // Return the required alignment.
1459 do_addralign() const
1461 return std::max(The_reloc_stub::STUB_ADDR_ALIGN
,
1462 The_erratum_stub::STUB_ADDR_ALIGN
);
1465 // Reset address and file offset.
1467 do_reset_address_and_file_offset()
1468 { this->set_current_data_size_for_child(this->prev_data_size_
); }
1470 // Set final data size.
1472 set_final_data_size()
1473 { this->set_data_size(this->current_data_size()); }
1476 // Relocate one stub.
1478 relocate_stub(The_reloc_stub
*,
1479 const The_relocate_info
*,
1480 The_target_aarch64
*,
1487 // Owner of this stub table.
1488 The_aarch64_input_section
* owner_
;
1489 // The relocation stubs.
1490 Reloc_stub_map reloc_stubs_
;
1491 // The erratum stubs.
1492 Erratum_stub_set erratum_stubs_
;
1493 // Size of reloc stubs.
1494 off_t reloc_stubs_size_
;
1495 // Size of erratum stubs.
1496 off_t erratum_stubs_size_
;
1497 // data size of this in the previous pass.
1498 off_t prev_data_size_
;
1499 }; // End of Stub_table
1502 // Add an erratum stub into the erratum stub set. The set is ordered by
1503 // (relobj, shndx, sh_offset).
1505 template<int size
, bool big_endian
>
1507 Stub_table
<size
, big_endian
>::add_erratum_stub(The_erratum_stub
* stub
)
1509 std::pair
<Erratum_stub_set_iter
, bool> ret
=
1510 this->erratum_stubs_
.insert(stub
);
1511 gold_assert(ret
.second
);
1512 this->erratum_stubs_size_
= align_address(
1513 this->erratum_stubs_size_
, The_erratum_stub::STUB_ADDR_ALIGN
);
1514 stub
->set_offset(this->erratum_stubs_size_
);
1515 this->erratum_stubs_size_
+= stub
->stub_size();
1519 // Find if such erratum exists for given (obj, shndx, sh_offset).
1521 template<int size
, bool big_endian
>
1522 Erratum_stub
<size
, big_endian
>*
1523 Stub_table
<size
, big_endian
>::find_erratum_stub(
1524 The_aarch64_relobj
* a64relobj
, unsigned int shndx
, unsigned int sh_offset
)
1526 // A dummy object used as key to search in the set.
1527 The_erratum_stub
key(a64relobj
, ST_NONE
,
1529 Erratum_stub_set_iter i
= this->erratum_stubs_
.find(&key
);
1530 if (i
!= this->erratum_stubs_
.end())
1532 The_erratum_stub
* stub(*i
);
1533 gold_assert(stub
->erratum_insn() != 0);
1540 // Find all the errata for a given input section. The return value is a pair of
1541 // iterators [begin, end).
1543 template<int size
, bool big_endian
>
1544 std::pair
<typename Stub_table
<size
, big_endian
>::Erratum_stub_set_iter
,
1545 typename Stub_table
<size
, big_endian
>::Erratum_stub_set_iter
>
1546 Stub_table
<size
, big_endian
>::find_erratum_stubs_for_input_section(
1547 The_aarch64_relobj
* a64relobj
, unsigned int shndx
)
1549 typedef std::pair
<Erratum_stub_set_iter
, Erratum_stub_set_iter
> Result_pair
;
1550 Erratum_stub_set_iter start
, end
;
1551 The_erratum_stub
low_key(a64relobj
, ST_NONE
, shndx
, 0);
1552 start
= this->erratum_stubs_
.lower_bound(&low_key
);
1553 if (start
== this->erratum_stubs_
.end())
1554 return Result_pair(this->erratum_stubs_
.end(),
1555 this->erratum_stubs_
.end());
1557 while (end
!= this->erratum_stubs_
.end() &&
1558 (*end
)->relobj() == a64relobj
&& (*end
)->shndx() == shndx
)
1560 return Result_pair(start
, end
);
1564 // Add a STUB using KEY. The caller is responsible for avoiding addition
1565 // if a STUB with the same key has already been added.
1567 template<int size
, bool big_endian
>
1569 Stub_table
<size
, big_endian
>::add_reloc_stub(
1570 The_reloc_stub
* stub
, const The_reloc_stub_key
& key
)
1572 gold_assert(stub
->type() == key
.type());
1573 this->reloc_stubs_
[key
] = stub
;
1575 // Assign stub offset early. We can do this because we never remove
1576 // reloc stubs and they are in the beginning of the stub table.
1577 this->reloc_stubs_size_
= align_address(this->reloc_stubs_size_
,
1578 The_reloc_stub::STUB_ADDR_ALIGN
);
1579 stub
->set_offset(this->reloc_stubs_size_
);
1580 this->reloc_stubs_size_
+= stub
->stub_size();
1584 // Relocate all stubs in this stub table.
1586 template<int size
, bool big_endian
>
1588 Stub_table
<size
, big_endian
>::
1589 relocate_stubs(const The_relocate_info
* relinfo
,
1590 The_target_aarch64
* target_aarch64
,
1591 Output_section
* output_section
,
1592 unsigned char* view
,
1593 AArch64_address address
,
1594 section_size_type view_size
)
1596 // "view_size" is the total size of the stub_table.
1597 gold_assert(address
== this->address() &&
1598 view_size
== static_cast<section_size_type
>(this->data_size()));
1599 for(Reloc_stub_map_const_iter p
= this->reloc_stubs_
.begin();
1600 p
!= this->reloc_stubs_
.end(); ++p
)
1601 relocate_stub(p
->second
, relinfo
, target_aarch64
, output_section
,
1602 view
, address
, view_size
);
1604 // Just for convenience.
1605 const int BPI
= AArch64_insn_utilities
<big_endian
>::BYTES_PER_INSN
;
1607 // Now 'relocate' erratum stubs.
1608 for(Erratum_stub_set_iter i
= this->erratum_stubs_
.begin();
1609 i
!= this->erratum_stubs_
.end(); ++i
)
1611 AArch64_address stub_address
= this->erratum_stub_address(*i
);
1612 // The address of "b" in the stub that is to be "relocated".
1613 AArch64_address stub_b_insn_address
;
1614 // Branch offset that is to be filled in "b" insn.
1616 switch ((*i
)->type())
1620 // The 1st insn of the erratum could be a relocation spot,
1621 // in this case we need to fix it with
1622 // "(*i)->erratum_insn()".
1623 elfcpp::Swap
<32, big_endian
>::writeval(
1624 view
+ (stub_address
- this->address()),
1625 (*i
)->erratum_insn());
1626 // For the erratum, the 2nd insn is a b-insn to be patched
1628 stub_b_insn_address
= stub_address
+ 1 * BPI
;
1629 b_offset
= (*i
)->destination_address() - stub_b_insn_address
;
1630 AArch64_relocate_functions
<size
, big_endian
>::construct_b(
1631 view
+ (stub_b_insn_address
- this->address()),
1632 ((unsigned int)(b_offset
)) & 0xfffffff);
1642 // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
1644 template<int size
, bool big_endian
>
1646 Stub_table
<size
, big_endian
>::
1647 relocate_stub(The_reloc_stub
* stub
,
1648 const The_relocate_info
* relinfo
,
1649 The_target_aarch64
* target_aarch64
,
1650 Output_section
* output_section
,
1651 unsigned char* view
,
1652 AArch64_address address
,
1653 section_size_type view_size
)
1655 // "offset" is the offset from the beginning of the stub_table.
1656 section_size_type offset
= stub
->offset();
1657 section_size_type stub_size
= stub
->stub_size();
1658 // "view_size" is the total size of the stub_table.
1659 gold_assert(offset
+ stub_size
<= view_size
);
1661 target_aarch64
->relocate_stub(stub
, relinfo
, output_section
,
1662 view
+ offset
, address
+ offset
, view_size
);
1666 // Write out the stubs to file.
1668 template<int size
, bool big_endian
>
1670 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
1672 off_t offset
= this->offset();
1673 const section_size_type oview_size
=
1674 convert_to_section_size_type(this->data_size());
1675 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1677 // Write relocation stubs.
1678 for (typename
Reloc_stub_map::const_iterator p
= this->reloc_stubs_
.begin();
1679 p
!= this->reloc_stubs_
.end(); ++p
)
1681 The_reloc_stub
* stub
= p
->second
;
1682 AArch64_address address
= this->address() + stub
->offset();
1683 gold_assert(address
==
1684 align_address(address
, The_reloc_stub::STUB_ADDR_ALIGN
));
1685 stub
->write(oview
+ stub
->offset(), stub
->stub_size());
1688 // Write erratum stubs.
1689 unsigned int erratum_stub_start_offset
=
1690 align_address(this->reloc_stubs_size_
, The_erratum_stub::STUB_ADDR_ALIGN
);
1691 for (typename
Erratum_stub_set::iterator p
= this->erratum_stubs_
.begin();
1692 p
!= this->erratum_stubs_
.end(); ++p
)
1694 The_erratum_stub
* stub(*p
);
1695 stub
->write(oview
+ erratum_stub_start_offset
+ stub
->offset(),
1699 of
->write_output_view(this->offset(), oview_size
, oview
);
1703 // AArch64_relobj class.
1705 template<int size
, bool big_endian
>
1706 class AArch64_relobj
: public Sized_relobj_file
<size
, big_endian
>
1709 typedef AArch64_relobj
<size
, big_endian
> This
;
1710 typedef Target_aarch64
<size
, big_endian
> The_target_aarch64
;
1711 typedef AArch64_input_section
<size
, big_endian
> The_aarch64_input_section
;
1712 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
1713 typedef Stub_table
<size
, big_endian
> The_stub_table
;
1714 typedef Erratum_stub
<size
, big_endian
> The_erratum_stub
;
1715 typedef typename
The_stub_table::Erratum_stub_set_iter Erratum_stub_set_iter
;
1716 typedef std::vector
<The_stub_table
*> Stub_table_list
;
1717 static const AArch64_address invalid_address
=
1718 static_cast<AArch64_address
>(-1);
1720 AArch64_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1721 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
1722 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
1729 // Return the stub table of the SHNDX-th section if there is one.
1731 stub_table(unsigned int shndx
) const
1733 gold_assert(shndx
< this->stub_tables_
.size());
1734 return this->stub_tables_
[shndx
];
1737 // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
1739 set_stub_table(unsigned int shndx
, The_stub_table
* stub_table
)
1741 gold_assert(shndx
< this->stub_tables_
.size());
1742 this->stub_tables_
[shndx
] = stub_table
;
1745 // Entrance to errata scanning.
1747 scan_errata(unsigned int shndx
,
1748 const elfcpp::Shdr
<size
, big_endian
>&,
1749 Output_section
*, const Symbol_table
*,
1750 The_target_aarch64
*);
1752 // Scan all relocation sections for stub generation.
1754 scan_sections_for_stubs(The_target_aarch64
*, const Symbol_table
*,
1757 // Whether a section is a scannable text section.
1759 text_section_is_scannable(const elfcpp::Shdr
<size
, big_endian
>&, unsigned int,
1760 const Output_section
*, const Symbol_table
*);
1762 // Convert regular input section with index SHNDX to a relaxed section.
1764 convert_input_section_to_relaxed_section(unsigned /* shndx */)
1766 // The stubs have relocations and we need to process them after writing
1767 // out the stubs. So relocation now must follow section write.
1768 this->set_relocs_must_follow_section_writes();
1771 // Structure for mapping symbol position.
1772 struct Mapping_symbol_position
1774 Mapping_symbol_position(unsigned int shndx
, AArch64_address offset
):
1775 shndx_(shndx
), offset_(offset
)
1778 // "<" comparator used in ordered_map container.
1780 operator<(const Mapping_symbol_position
& p
) const
1782 return (this->shndx_
< p
.shndx_
1783 || (this->shndx_
== p
.shndx_
&& this->offset_
< p
.offset_
));
1787 unsigned int shndx_
;
1790 AArch64_address offset_
;
1793 typedef std::map
<Mapping_symbol_position
, char> Mapping_symbol_info
;
1796 // Post constructor setup.
1800 // Call parent's setup method.
1801 Sized_relobj_file
<size
, big_endian
>::do_setup();
1803 // Initialize look-up tables.
1804 this->stub_tables_
.resize(this->shnum());
1808 do_relocate_sections(
1809 const Symbol_table
* symtab
, const Layout
* layout
,
1810 const unsigned char* pshdrs
, Output_file
* of
,
1811 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
);
1813 // Count local symbols and (optionally) record mapping info.
1815 do_count_local_symbols(Stringpool_template
<char>*,
1816 Stringpool_template
<char>*);
1819 // Fix all errata in the object.
1821 fix_errata(typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
);
1823 // Try to fix erratum 843419 in an optimized way. Return true if patch is
1826 try_fix_erratum_843419_optimized(
1828 typename Sized_relobj_file
<size
, big_endian
>::View_size
&);
1830 // Whether a section needs to be scanned for relocation stubs.
1832 section_needs_reloc_stub_scanning(const elfcpp::Shdr
<size
, big_endian
>&,
1833 const Relobj::Output_sections
&,
1834 const Symbol_table
*, const unsigned char*);
1836 // List of stub tables.
1837 Stub_table_list stub_tables_
;
1839 // Mapping symbol information sorted by (section index, section_offset).
1840 Mapping_symbol_info mapping_symbol_info_
;
1841 }; // End of AArch64_relobj
1844 // Override to record mapping symbol information.
1845 template<int size
, bool big_endian
>
1847 AArch64_relobj
<size
, big_endian
>::do_count_local_symbols(
1848 Stringpool_template
<char>* pool
, Stringpool_template
<char>* dynpool
)
1850 Sized_relobj_file
<size
, big_endian
>::do_count_local_symbols(pool
, dynpool
);
1852 // Only erratum-fixing work needs mapping symbols, so skip this time consuming
1853 // processing if not fixing erratum.
1854 if (!parameters
->options().fix_cortex_a53_843419()
1855 && !parameters
->options().fix_cortex_a53_835769())
1858 const unsigned int loccount
= this->local_symbol_count();
1862 // Read the symbol table section header.
1863 const unsigned int symtab_shndx
= this->symtab_shndx();
1864 elfcpp::Shdr
<size
, big_endian
>
1865 symtabshdr(this, this->elf_file()->section_header(symtab_shndx
));
1866 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
1868 // Read the local symbols.
1869 const int sym_size
=elfcpp::Elf_sizes
<size
>::sym_size
;
1870 gold_assert(loccount
== symtabshdr
.get_sh_info());
1871 off_t locsize
= loccount
* sym_size
;
1872 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
1873 locsize
, true, true);
1875 // For mapping symbol processing, we need to read the symbol names.
1876 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
1877 if (strtab_shndx
>= this->shnum())
1879 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
1883 elfcpp::Shdr
<size
, big_endian
>
1884 strtabshdr(this, this->elf_file()->section_header(strtab_shndx
));
1885 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
1887 this->error(_("symbol table name section has wrong type: %u"),
1888 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
1892 const char* pnames
=
1893 reinterpret_cast<const char*>(this->get_view(strtabshdr
.get_sh_offset(),
1894 strtabshdr
.get_sh_size(),
1897 // Skip the first dummy symbol.
1899 typename Sized_relobj_file
<size
, big_endian
>::Local_values
*
1900 plocal_values
= this->local_values();
1901 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
1903 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
1904 Symbol_value
<size
>& lv((*plocal_values
)[i
]);
1905 AArch64_address input_value
= lv
.input_value();
1907 // Check to see if this is a mapping symbol. AArch64 mapping symbols are
1908 // defined in "ELF for the ARM 64-bit Architecture", Table 4-4, Mapping
1910 // Mapping symbols could be one of the following 4 forms -
1915 const char* sym_name
= pnames
+ sym
.get_st_name();
1916 if (sym_name
[0] == '$' && (sym_name
[1] == 'x' || sym_name
[1] == 'd')
1917 && (sym_name
[2] == '\0' || sym_name
[2] == '.'))
1920 unsigned int input_shndx
=
1921 this->adjust_sym_shndx(i
, sym
.get_st_shndx(), &is_ordinary
);
1922 gold_assert(is_ordinary
);
1924 Mapping_symbol_position
msp(input_shndx
, input_value
);
1925 // Insert mapping_symbol_info into map whose ordering is defined by
1926 // (shndx, offset_within_section).
1927 this->mapping_symbol_info_
[msp
] = sym_name
[1];
1933 // Fix all errata in the object.
1935 template<int size
, bool big_endian
>
1937 AArch64_relobj
<size
, big_endian
>::fix_errata(
1938 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
)
1940 typedef typename
elfcpp::Swap
<32,big_endian
>::Valtype Insntype
;
1941 unsigned int shnum
= this->shnum();
1942 for (unsigned int i
= 1; i
< shnum
; ++i
)
1944 The_stub_table
* stub_table
= this->stub_table(i
);
1947 std::pair
<Erratum_stub_set_iter
, Erratum_stub_set_iter
>
1948 ipair(stub_table
->find_erratum_stubs_for_input_section(this, i
));
1949 Erratum_stub_set_iter p
= ipair
.first
, end
= ipair
.second
;
1952 The_erratum_stub
* stub
= *p
;
1953 typename Sized_relobj_file
<size
, big_endian
>::View_size
&
1954 pview((*pviews
)[i
]);
1956 // Double check data before fix.
1957 gold_assert(pview
.address
+ stub
->sh_offset()
1958 == stub
->erratum_address());
1960 // Update previously recorded erratum insn with relocated
1963 reinterpret_cast<Insntype
*>(pview
.view
+ stub
->sh_offset());
1964 Insntype insn_to_fix
= ip
[0];
1965 stub
->update_erratum_insn(insn_to_fix
);
1967 // First try to see if erratum is 843419 and if it can be fixed
1968 // without using branch-to-stub.
1969 if (!try_fix_erratum_843419_optimized(stub
, pview
))
1971 // Replace the erratum insn with a branch-to-stub.
1972 AArch64_address stub_address
=
1973 stub_table
->erratum_stub_address(stub
);
1974 unsigned int b_offset
= stub_address
- stub
->erratum_address();
1975 AArch64_relocate_functions
<size
, big_endian
>::construct_b(
1976 pview
.view
+ stub
->sh_offset(), b_offset
& 0xfffffff);
1984 // This is an optimization for 843419. This erratum requires the sequence begin
1985 // with 'adrp', when final value calculated by adrp fits in adr, we can just
1986 // replace 'adrp' with 'adr', so we save 2 jumps per occurrence. (Note, however,
1987 // in this case, we do not delete the erratum stub (too late to do so), it is
1988 // merely generated without ever being called.)
1990 template<int size
, bool big_endian
>
1992 AArch64_relobj
<size
, big_endian
>::try_fix_erratum_843419_optimized(
1993 The_erratum_stub
* stub
,
1994 typename Sized_relobj_file
<size
, big_endian
>::View_size
& pview
)
1996 if (stub
->type() != ST_E_843419
)
1999 typedef AArch64_insn_utilities
<big_endian
> Insn_utilities
;
2000 typedef typename
elfcpp::Swap
<32,big_endian
>::Valtype Insntype
;
2001 E843419_stub
<size
, big_endian
>* e843419_stub
=
2002 reinterpret_cast<E843419_stub
<size
, big_endian
>*>(stub
);
2003 AArch64_address pc
= pview
.address
+ e843419_stub
->adrp_sh_offset();
2004 Insntype
* adrp_view
= reinterpret_cast<Insntype
*>(
2005 pview
.view
+ e843419_stub
->adrp_sh_offset());
2006 Insntype adrp_insn
= adrp_view
[0];
2007 gold_assert(Insn_utilities::is_adrp(adrp_insn
));
2008 // Get adrp 33-bit signed imm value.
2009 int64_t adrp_imm
= Insn_utilities::
2010 aarch64_adrp_decode_imm(adrp_insn
);
2011 // adrp - final value transferred to target register is calculated as:
2012 // PC[11:0] = Zeros(12)
2013 // adrp_dest_value = PC + adrp_imm;
2014 int64_t adrp_dest_value
= (pc
& ~((1 << 12) - 1)) + adrp_imm
;
2015 // adr -final value transferred to target register is calucalted as:
2018 // PC + adr_imm = adrp_dest_value
2020 // adr_imm = adrp_dest_value - PC
2021 int64_t adr_imm
= adrp_dest_value
- pc
;
2022 // Check if imm fits in adr (21-bit signed).
2023 if (-(1 << 20) <= adr_imm
&& adr_imm
< (1 << 20))
2025 // Convert 'adrp' into 'adr'.
2026 Insntype adr_insn
= adrp_insn
& ((1u << 31) - 1);
2027 adr_insn
= Insn_utilities::
2028 aarch64_adr_encode_imm(adr_insn
, adr_imm
);
2029 elfcpp::Swap
<32, big_endian
>::writeval(adrp_view
, adr_insn
);
2036 // Relocate sections.
2038 template<int size
, bool big_endian
>
2040 AArch64_relobj
<size
, big_endian
>::do_relocate_sections(
2041 const Symbol_table
* symtab
, const Layout
* layout
,
2042 const unsigned char* pshdrs
, Output_file
* of
,
2043 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
)
2045 // Call parent to relocate sections.
2046 Sized_relobj_file
<size
, big_endian
>::do_relocate_sections(symtab
, layout
,
2047 pshdrs
, of
, pviews
);
2049 // We do not generate stubs if doing a relocatable link.
2050 if (parameters
->options().relocatable())
2053 if (parameters
->options().fix_cortex_a53_843419()
2054 || parameters
->options().fix_cortex_a53_835769())
2055 this->fix_errata(pviews
);
2057 Relocate_info
<size
, big_endian
> relinfo
;
2058 relinfo
.symtab
= symtab
;
2059 relinfo
.layout
= layout
;
2060 relinfo
.object
= this;
2062 // Relocate stub tables.
2063 unsigned int shnum
= this->shnum();
2064 The_target_aarch64
* target
= The_target_aarch64::current_target();
2066 for (unsigned int i
= 1; i
< shnum
; ++i
)
2068 The_aarch64_input_section
* aarch64_input_section
=
2069 target
->find_aarch64_input_section(this, i
);
2070 if (aarch64_input_section
!= NULL
2071 && aarch64_input_section
->is_stub_table_owner()
2072 && !aarch64_input_section
->stub_table()->empty())
2074 Output_section
* os
= this->output_section(i
);
2075 gold_assert(os
!= NULL
);
2077 relinfo
.reloc_shndx
= elfcpp::SHN_UNDEF
;
2078 relinfo
.reloc_shdr
= NULL
;
2079 relinfo
.data_shndx
= i
;
2080 relinfo
.data_shdr
= pshdrs
+ i
* elfcpp::Elf_sizes
<size
>::shdr_size
;
2082 typename Sized_relobj_file
<size
, big_endian
>::View_size
&
2083 view_struct
= (*pviews
)[i
];
2084 gold_assert(view_struct
.view
!= NULL
);
2086 The_stub_table
* stub_table
= aarch64_input_section
->stub_table();
2087 off_t offset
= stub_table
->address() - view_struct
.address
;
2088 unsigned char* view
= view_struct
.view
+ offset
;
2089 AArch64_address address
= stub_table
->address();
2090 section_size_type view_size
= stub_table
->data_size();
2091 stub_table
->relocate_stubs(&relinfo
, target
, os
, view
, address
,
2098 // Determine if an input section is scannable for stub processing. SHDR is
2099 // the header of the section and SHNDX is the section index. OS is the output
2100 // section for the input section and SYMTAB is the global symbol table used to
2101 // look up ICF information.
2103 template<int size
, bool big_endian
>
2105 AArch64_relobj
<size
, big_endian
>::text_section_is_scannable(
2106 const elfcpp::Shdr
<size
, big_endian
>& text_shdr
,
2107 unsigned int text_shndx
,
2108 const Output_section
* os
,
2109 const Symbol_table
* symtab
)
2111 // Skip any empty sections, unallocated sections or sections whose
2112 // type are not SHT_PROGBITS.
2113 if (text_shdr
.get_sh_size() == 0
2114 || (text_shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0
2115 || text_shdr
.get_sh_type() != elfcpp::SHT_PROGBITS
)
2118 // Skip any discarded or ICF'ed sections.
2119 if (os
== NULL
|| symtab
->is_section_folded(this, text_shndx
))
2122 // Skip exception frame.
2123 if (strcmp(os
->name(), ".eh_frame") == 0)
2126 gold_assert(!this->is_output_section_offset_invalid(text_shndx
) ||
2127 os
->find_relaxed_input_section(this, text_shndx
) != NULL
);
2133 // Determine if we want to scan the SHNDX-th section for relocation stubs.
2134 // This is a helper for AArch64_relobj::scan_sections_for_stubs().
2136 template<int size
, bool big_endian
>
2138 AArch64_relobj
<size
, big_endian
>::section_needs_reloc_stub_scanning(
2139 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2140 const Relobj::Output_sections
& out_sections
,
2141 const Symbol_table
* symtab
,
2142 const unsigned char* pshdrs
)
2144 unsigned int sh_type
= shdr
.get_sh_type();
2145 if (sh_type
!= elfcpp::SHT_RELA
)
2148 // Ignore empty section.
2149 off_t sh_size
= shdr
.get_sh_size();
2153 // Ignore reloc section with unexpected symbol table. The
2154 // error will be reported in the final link.
2155 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx())
2158 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2159 unsigned int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
2161 // Ignore reloc section with unexpected entsize or uneven size.
2162 // The error will be reported in the final link.
2163 if (reloc_size
!= shdr
.get_sh_entsize() || sh_size
% reloc_size
!= 0)
2166 // Ignore reloc section with bad info. This error will be
2167 // reported in the final link.
2168 unsigned int text_shndx
= this->adjust_shndx(shdr
.get_sh_info());
2169 if (text_shndx
>= this->shnum())
2172 const unsigned int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2173 const elfcpp::Shdr
<size
, big_endian
> text_shdr(pshdrs
+
2174 text_shndx
* shdr_size
);
2175 return this->text_section_is_scannable(text_shdr
, text_shndx
,
2176 out_sections
[text_shndx
], symtab
);
2180 // Scan section SHNDX for erratum 843419 and 835769.
2182 template<int size
, bool big_endian
>
2184 AArch64_relobj
<size
, big_endian
>::scan_errata(
2185 unsigned int shndx
, const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2186 Output_section
* os
, const Symbol_table
* symtab
,
2187 The_target_aarch64
* target
)
2189 if (shdr
.get_sh_size() == 0
2190 || (shdr
.get_sh_flags() &
2191 (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
)) == 0
2192 || shdr
.get_sh_type() != elfcpp::SHT_PROGBITS
)
2195 if (!os
|| symtab
->is_section_folded(this, shndx
)) return;
2197 AArch64_address output_offset
= this->get_output_section_offset(shndx
);
2198 AArch64_address output_address
;
2199 if (output_offset
!= invalid_address
)
2200 output_address
= os
->address() + output_offset
;
2203 const Output_relaxed_input_section
* poris
=
2204 os
->find_relaxed_input_section(this, shndx
);
2206 output_address
= poris
->address();
2209 section_size_type input_view_size
= 0;
2210 const unsigned char* input_view
=
2211 this->section_contents(shndx
, &input_view_size
, false);
2213 Mapping_symbol_position
section_start(shndx
, 0);
2214 // Find the first mapping symbol record within section shndx.
2215 typename
Mapping_symbol_info::const_iterator p
=
2216 this->mapping_symbol_info_
.lower_bound(section_start
);
2217 while (p
!= this->mapping_symbol_info_
.end() &&
2218 p
->first
.shndx_
== shndx
)
2220 typename
Mapping_symbol_info::const_iterator prev
= p
;
2222 if (prev
->second
== 'x')
2224 section_size_type span_start
=
2225 convert_to_section_size_type(prev
->first
.offset_
);
2226 section_size_type span_end
;
2227 if (p
!= this->mapping_symbol_info_
.end()
2228 && p
->first
.shndx_
== shndx
)
2229 span_end
= convert_to_section_size_type(p
->first
.offset_
);
2231 span_end
= convert_to_section_size_type(shdr
.get_sh_size());
2233 // Here we do not share the scanning code of both errata. For 843419,
2234 // only the last few insns of each page are examined, which is fast,
2235 // whereas, for 835769, every insn pair needs to be checked.
2237 if (parameters
->options().fix_cortex_a53_843419())
2238 target
->scan_erratum_843419_span(
2239 this, shndx
, span_start
, span_end
,
2240 const_cast<unsigned char*>(input_view
), output_address
);
2242 if (parameters
->options().fix_cortex_a53_835769())
2243 target
->scan_erratum_835769_span(
2244 this, shndx
, span_start
, span_end
,
2245 const_cast<unsigned char*>(input_view
), output_address
);
2251 // Scan relocations for stub generation.
2253 template<int size
, bool big_endian
>
2255 AArch64_relobj
<size
, big_endian
>::scan_sections_for_stubs(
2256 The_target_aarch64
* target
,
2257 const Symbol_table
* symtab
,
2258 const Layout
* layout
)
2260 unsigned int shnum
= this->shnum();
2261 const unsigned int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2263 // Read the section headers.
2264 const unsigned char* pshdrs
= this->get_view(this->elf_file()->shoff(),
2268 // To speed up processing, we set up hash tables for fast lookup of
2269 // input offsets to output addresses.
2270 this->initialize_input_to_output_maps();
2272 const Relobj::Output_sections
& out_sections(this->output_sections());
2274 Relocate_info
<size
, big_endian
> relinfo
;
2275 relinfo
.symtab
= symtab
;
2276 relinfo
.layout
= layout
;
2277 relinfo
.object
= this;
2279 // Do relocation stubs scanning.
2280 const unsigned char* p
= pshdrs
+ shdr_size
;
2281 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
2283 const elfcpp::Shdr
<size
, big_endian
> shdr(p
);
2284 if (parameters
->options().fix_cortex_a53_843419()
2285 || parameters
->options().fix_cortex_a53_835769())
2286 scan_errata(i
, shdr
, out_sections
[i
], symtab
, target
);
2287 if (this->section_needs_reloc_stub_scanning(shdr
, out_sections
, symtab
,
2290 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
2291 AArch64_address output_offset
=
2292 this->get_output_section_offset(index
);
2293 AArch64_address output_address
;
2294 if (output_offset
!= invalid_address
)
2296 output_address
= out_sections
[index
]->address() + output_offset
;
2300 // Currently this only happens for a relaxed section.
2301 const Output_relaxed_input_section
* poris
=
2302 out_sections
[index
]->find_relaxed_input_section(this, index
);
2303 gold_assert(poris
!= NULL
);
2304 output_address
= poris
->address();
2307 // Get the relocations.
2308 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
2312 // Get the section contents.
2313 section_size_type input_view_size
= 0;
2314 const unsigned char* input_view
=
2315 this->section_contents(index
, &input_view_size
, false);
2317 relinfo
.reloc_shndx
= i
;
2318 relinfo
.data_shndx
= index
;
2319 unsigned int sh_type
= shdr
.get_sh_type();
2320 unsigned int reloc_size
;
2321 gold_assert (sh_type
== elfcpp::SHT_RELA
);
2322 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
2324 Output_section
* os
= out_sections
[index
];
2325 target
->scan_section_for_stubs(&relinfo
, sh_type
, prelocs
,
2326 shdr
.get_sh_size() / reloc_size
,
2328 output_offset
== invalid_address
,
2329 input_view
, output_address
,
2336 // A class to wrap an ordinary input section containing executable code.
2338 template<int size
, bool big_endian
>
2339 class AArch64_input_section
: public Output_relaxed_input_section
2342 typedef Stub_table
<size
, big_endian
> The_stub_table
;
2344 AArch64_input_section(Relobj
* relobj
, unsigned int shndx
)
2345 : Output_relaxed_input_section(relobj
, shndx
, 1),
2347 original_contents_(NULL
), original_size_(0),
2348 original_addralign_(1)
2351 ~AArch64_input_section()
2352 { delete[] this->original_contents_
; }
2358 // Set the stub_table.
2360 set_stub_table(The_stub_table
* st
)
2361 { this->stub_table_
= st
; }
2363 // Whether this is a stub table owner.
2365 is_stub_table_owner() const
2366 { return this->stub_table_
!= NULL
&& this->stub_table_
->owner() == this; }
2368 // Return the original size of the section.
2370 original_size() const
2371 { return this->original_size_
; }
2373 // Return the stub table.
2376 { return stub_table_
; }
2379 // Write out this input section.
2381 do_write(Output_file
*);
2383 // Return required alignment of this.
2385 do_addralign() const
2387 if (this->is_stub_table_owner())
2388 return std::max(this->stub_table_
->addralign(),
2389 static_cast<uint64_t>(this->original_addralign_
));
2391 return this->original_addralign_
;
2394 // Finalize data size.
2396 set_final_data_size();
2398 // Reset address and file offset.
2400 do_reset_address_and_file_offset();
2404 do_output_offset(const Relobj
* object
, unsigned int shndx
,
2405 section_offset_type offset
,
2406 section_offset_type
* poutput
) const
2408 if ((object
== this->relobj())
2409 && (shndx
== this->shndx())
2412 convert_types
<section_offset_type
, uint32_t>(this->original_size_
)))
2422 // Copying is not allowed.
2423 AArch64_input_section(const AArch64_input_section
&);
2424 AArch64_input_section
& operator=(const AArch64_input_section
&);
2426 // The relocation stubs.
2427 The_stub_table
* stub_table_
;
2428 // Original section contents. We have to make a copy here since the file
2429 // containing the original section may not be locked when we need to access
2431 unsigned char* original_contents_
;
2432 // Section size of the original input section.
2433 uint32_t original_size_
;
2434 // Address alignment of the original input section.
2435 uint32_t original_addralign_
;
2436 }; // End of AArch64_input_section
2439 // Finalize data size.
2441 template<int size
, bool big_endian
>
2443 AArch64_input_section
<size
, big_endian
>::set_final_data_size()
2445 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
2447 if (this->is_stub_table_owner())
2449 this->stub_table_
->finalize_data_size();
2450 off
= align_address(off
, this->stub_table_
->addralign());
2451 off
+= this->stub_table_
->data_size();
2453 this->set_data_size(off
);
2457 // Reset address and file offset.
2459 template<int size
, bool big_endian
>
2461 AArch64_input_section
<size
, big_endian
>::do_reset_address_and_file_offset()
2463 // Size of the original input section contents.
2464 off_t off
= convert_types
<off_t
, uint64_t>(this->original_size_
);
2466 // If this is a stub table owner, account for the stub table size.
2467 if (this->is_stub_table_owner())
2469 The_stub_table
* stub_table
= this->stub_table_
;
2471 // Reset the stub table's address and file offset. The
2472 // current data size for child will be updated after that.
2473 stub_table_
->reset_address_and_file_offset();
2474 off
= align_address(off
, stub_table_
->addralign());
2475 off
+= stub_table
->current_data_size();
2478 this->set_current_data_size(off
);
2482 // Initialize an Arm_input_section.
2484 template<int size
, bool big_endian
>
2486 AArch64_input_section
<size
, big_endian
>::init()
2488 Relobj
* relobj
= this->relobj();
2489 unsigned int shndx
= this->shndx();
2491 // We have to cache original size, alignment and contents to avoid locking
2492 // the original file.
2493 this->original_addralign_
=
2494 convert_types
<uint32_t, uint64_t>(relobj
->section_addralign(shndx
));
2496 // This is not efficient but we expect only a small number of relaxed
2497 // input sections for stubs.
2498 section_size_type section_size
;
2499 const unsigned char* section_contents
=
2500 relobj
->section_contents(shndx
, §ion_size
, false);
2501 this->original_size_
=
2502 convert_types
<uint32_t, uint64_t>(relobj
->section_size(shndx
));
2504 gold_assert(this->original_contents_
== NULL
);
2505 this->original_contents_
= new unsigned char[section_size
];
2506 memcpy(this->original_contents_
, section_contents
, section_size
);
2508 // We want to make this look like the original input section after
2509 // output sections are finalized.
2510 Output_section
* os
= relobj
->output_section(shndx
);
2511 off_t offset
= relobj
->output_section_offset(shndx
);
2512 gold_assert(os
!= NULL
&& !relobj
->is_output_section_offset_invalid(shndx
));
2513 this->set_address(os
->address() + offset
);
2514 this->set_file_offset(os
->offset() + offset
);
2515 this->set_current_data_size(this->original_size_
);
2516 this->finalize_data_size();
2520 // Write data to output file.
2522 template<int size
, bool big_endian
>
2524 AArch64_input_section
<size
, big_endian
>::do_write(Output_file
* of
)
2526 // We have to write out the original section content.
2527 gold_assert(this->original_contents_
!= NULL
);
2528 of
->write(this->offset(), this->original_contents_
,
2529 this->original_size_
);
2531 // If this owns a stub table and it is not empty, write it.
2532 if (this->is_stub_table_owner() && !this->stub_table_
->empty())
2533 this->stub_table_
->write(of
);
2537 // Arm output section class. This is defined mainly to add a number of stub
2538 // generation methods.
2540 template<int size
, bool big_endian
>
2541 class AArch64_output_section
: public Output_section
2544 typedef Target_aarch64
<size
, big_endian
> The_target_aarch64
;
2545 typedef AArch64_relobj
<size
, big_endian
> The_aarch64_relobj
;
2546 typedef Stub_table
<size
, big_endian
> The_stub_table
;
2547 typedef AArch64_input_section
<size
, big_endian
> The_aarch64_input_section
;
2550 AArch64_output_section(const char* name
, elfcpp::Elf_Word type
,
2551 elfcpp::Elf_Xword flags
)
2552 : Output_section(name
, type
, flags
)
2555 ~AArch64_output_section() {}
2557 // Group input sections for stub generation.
2559 group_sections(section_size_type
, bool, Target_aarch64
<size
, big_endian
>*,
2563 typedef Output_section::Input_section Input_section
;
2564 typedef Output_section::Input_section_list Input_section_list
;
2566 // Create a stub group.
2568 create_stub_group(Input_section_list::const_iterator
,
2569 Input_section_list::const_iterator
,
2570 Input_section_list::const_iterator
,
2571 The_target_aarch64
*,
2572 std::vector
<Output_relaxed_input_section
*>&,
2574 }; // End of AArch64_output_section
2577 // Create a stub group for input sections from FIRST to LAST. OWNER points to
2578 // the input section that will be the owner of the stub table.
2580 template<int size
, bool big_endian
> void
2581 AArch64_output_section
<size
, big_endian
>::create_stub_group(
2582 Input_section_list::const_iterator first
,
2583 Input_section_list::const_iterator last
,
2584 Input_section_list::const_iterator owner
,
2585 The_target_aarch64
* target
,
2586 std::vector
<Output_relaxed_input_section
*>& new_relaxed_sections
,
2589 // Currently we convert ordinary input sections into relaxed sections only
2591 The_aarch64_input_section
* input_section
;
2592 if (owner
->is_relaxed_input_section())
2596 gold_assert(owner
->is_input_section());
2597 // Create a new relaxed input section. We need to lock the original
2599 Task_lock_obj
<Object
> tl(task
, owner
->relobj());
2601 target
->new_aarch64_input_section(owner
->relobj(), owner
->shndx());
2602 new_relaxed_sections
.push_back(input_section
);
2605 // Create a stub table.
2606 The_stub_table
* stub_table
=
2607 target
->new_stub_table(input_section
);
2609 input_section
->set_stub_table(stub_table
);
2611 Input_section_list::const_iterator p
= first
;
2612 // Look for input sections or relaxed input sections in [first ... last].
2615 if (p
->is_input_section() || p
->is_relaxed_input_section())
2617 // The stub table information for input sections live
2618 // in their objects.
2619 The_aarch64_relobj
* aarch64_relobj
=
2620 static_cast<The_aarch64_relobj
*>(p
->relobj());
2621 aarch64_relobj
->set_stub_table(p
->shndx(), stub_table
);
2624 while (p
++ != last
);
2628 // Group input sections for stub generation. GROUP_SIZE is roughly the limit of
2629 // stub groups. We grow a stub group by adding input section until the size is
2630 // just below GROUP_SIZE. The last input section will be converted into a stub
2631 // table owner. If STUB_ALWAYS_AFTER_BRANCH is false, we also add input sectiond
2632 // after the stub table, effectively doubling the group size.
2634 // This is similar to the group_sections() function in elf32-arm.c but is
2635 // implemented differently.
2637 template<int size
, bool big_endian
>
2638 void AArch64_output_section
<size
, big_endian
>::group_sections(
2639 section_size_type group_size
,
2640 bool stubs_always_after_branch
,
2641 Target_aarch64
<size
, big_endian
>* target
,
2647 FINDING_STUB_SECTION
,
2651 std::vector
<Output_relaxed_input_section
*> new_relaxed_sections
;
2653 State state
= NO_GROUP
;
2654 section_size_type off
= 0;
2655 section_size_type group_begin_offset
= 0;
2656 section_size_type group_end_offset
= 0;
2657 section_size_type stub_table_end_offset
= 0;
2658 Input_section_list::const_iterator group_begin
=
2659 this->input_sections().end();
2660 Input_section_list::const_iterator stub_table
=
2661 this->input_sections().end();
2662 Input_section_list::const_iterator group_end
= this->input_sections().end();
2663 for (Input_section_list::const_iterator p
= this->input_sections().begin();
2664 p
!= this->input_sections().end();
2667 section_size_type section_begin_offset
=
2668 align_address(off
, p
->addralign());
2669 section_size_type section_end_offset
=
2670 section_begin_offset
+ p
->data_size();
2672 // Check to see if we should group the previously seen sections.
2678 case FINDING_STUB_SECTION
:
2679 // Adding this section makes the group larger than GROUP_SIZE.
2680 if (section_end_offset
- group_begin_offset
>= group_size
)
2682 if (stubs_always_after_branch
)
2684 gold_assert(group_end
!= this->input_sections().end());
2685 this->create_stub_group(group_begin
, group_end
, group_end
,
2686 target
, new_relaxed_sections
,
2692 // Input sections up to stub_group_size bytes after the stub
2693 // table can be handled by it too.
2694 state
= HAS_STUB_SECTION
;
2695 stub_table
= group_end
;
2696 stub_table_end_offset
= group_end_offset
;
2701 case HAS_STUB_SECTION
:
2702 // Adding this section makes the post stub-section group larger
2705 // NOT SUPPORTED YET. For completeness only.
2706 if (section_end_offset
- stub_table_end_offset
>= group_size
)
2708 gold_assert(group_end
!= this->input_sections().end());
2709 this->create_stub_group(group_begin
, group_end
, stub_table
,
2710 target
, new_relaxed_sections
, task
);
2719 // If we see an input section and currently there is no group, start
2720 // a new one. Skip any empty sections. We look at the data size
2721 // instead of calling p->relobj()->section_size() to avoid locking.
2722 if ((p
->is_input_section() || p
->is_relaxed_input_section())
2723 && (p
->data_size() != 0))
2725 if (state
== NO_GROUP
)
2727 state
= FINDING_STUB_SECTION
;
2729 group_begin_offset
= section_begin_offset
;
2732 // Keep track of the last input section seen.
2734 group_end_offset
= section_end_offset
;
2737 off
= section_end_offset
;
2740 // Create a stub group for any ungrouped sections.
2741 if (state
== FINDING_STUB_SECTION
|| state
== HAS_STUB_SECTION
)
2743 gold_assert(group_end
!= this->input_sections().end());
2744 this->create_stub_group(group_begin
, group_end
,
2745 (state
== FINDING_STUB_SECTION
2748 target
, new_relaxed_sections
, task
);
2751 if (!new_relaxed_sections
.empty())
2752 this->convert_input_sections_to_relaxed_sections(new_relaxed_sections
);
2754 // Update the section offsets
2755 for (size_t i
= 0; i
< new_relaxed_sections
.size(); ++i
)
2757 The_aarch64_relobj
* relobj
= static_cast<The_aarch64_relobj
*>(
2758 new_relaxed_sections
[i
]->relobj());
2759 unsigned int shndx
= new_relaxed_sections
[i
]->shndx();
2760 // Tell AArch64_relobj that this input section is converted.
2761 relobj
->convert_input_section_to_relaxed_section(shndx
);
2763 } // End of AArch64_output_section::group_sections
2766 AArch64_reloc_property_table
* aarch64_reloc_property_table
= NULL
;
2769 // The aarch64 target class.
2771 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
2772 template<int size
, bool big_endian
>
2773 class Target_aarch64
: public Sized_target
<size
, big_endian
>
2776 typedef Target_aarch64
<size
, big_endian
> This
;
2777 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>
2779 typedef Relocate_info
<size
, big_endian
> The_relocate_info
;
2780 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2781 typedef AArch64_relobj
<size
, big_endian
> The_aarch64_relobj
;
2782 typedef Reloc_stub
<size
, big_endian
> The_reloc_stub
;
2783 typedef Erratum_stub
<size
, big_endian
> The_erratum_stub
;
2784 typedef typename Reloc_stub
<size
, big_endian
>::Key The_reloc_stub_key
;
2785 typedef Stub_table
<size
, big_endian
> The_stub_table
;
2786 typedef std::vector
<The_stub_table
*> Stub_table_list
;
2787 typedef typename
Stub_table_list::iterator Stub_table_iterator
;
2788 typedef AArch64_input_section
<size
, big_endian
> The_aarch64_input_section
;
2789 typedef AArch64_output_section
<size
, big_endian
> The_aarch64_output_section
;
2790 typedef Unordered_map
<Section_id
,
2791 AArch64_input_section
<size
, big_endian
>*,
2792 Section_id_hash
> AArch64_input_section_map
;
2793 typedef AArch64_insn_utilities
<big_endian
> Insn_utilities
;
2794 const static int TCB_SIZE
= size
/ 8 * 2;
2796 Target_aarch64(const Target::Target_info
* info
= &aarch64_info
)
2797 : Sized_target
<size
, big_endian
>(info
),
2798 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_irelative_(NULL
),
2799 got_tlsdesc_(NULL
), global_offset_table_(NULL
), rela_dyn_(NULL
),
2800 rela_irelative_(NULL
), copy_relocs_(elfcpp::R_AARCH64_COPY
),
2801 got_mod_index_offset_(-1U),
2802 tlsdesc_reloc_info_(), tls_base_symbol_defined_(false),
2803 stub_tables_(), stub_group_size_(0), aarch64_input_section_map_()
2806 // Scan the relocations to determine unreferenced sections for
2807 // garbage collection.
2809 gc_process_relocs(Symbol_table
* symtab
,
2811 Sized_relobj_file
<size
, big_endian
>* object
,
2812 unsigned int data_shndx
,
2813 unsigned int sh_type
,
2814 const unsigned char* prelocs
,
2816 Output_section
* output_section
,
2817 bool needs_special_offset_handling
,
2818 size_t local_symbol_count
,
2819 const unsigned char* plocal_symbols
);
2821 // Scan the relocations to look for symbol adjustments.
2823 scan_relocs(Symbol_table
* symtab
,
2825 Sized_relobj_file
<size
, big_endian
>* object
,
2826 unsigned int data_shndx
,
2827 unsigned int sh_type
,
2828 const unsigned char* prelocs
,
2830 Output_section
* output_section
,
2831 bool needs_special_offset_handling
,
2832 size_t local_symbol_count
,
2833 const unsigned char* plocal_symbols
);
2835 // Finalize the sections.
2837 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
2839 // Return the value to use for a dynamic which requires special
2842 do_dynsym_value(const Symbol
*) const;
2844 // Relocate a section.
2846 relocate_section(const Relocate_info
<size
, big_endian
>*,
2847 unsigned int sh_type
,
2848 const unsigned char* prelocs
,
2850 Output_section
* output_section
,
2851 bool needs_special_offset_handling
,
2852 unsigned char* view
,
2853 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
2854 section_size_type view_size
,
2855 const Reloc_symbol_changes
*);
2857 // Scan the relocs during a relocatable link.
2859 scan_relocatable_relocs(Symbol_table
* symtab
,
2861 Sized_relobj_file
<size
, big_endian
>* object
,
2862 unsigned int data_shndx
,
2863 unsigned int sh_type
,
2864 const unsigned char* prelocs
,
2866 Output_section
* output_section
,
2867 bool needs_special_offset_handling
,
2868 size_t local_symbol_count
,
2869 const unsigned char* plocal_symbols
,
2870 Relocatable_relocs
*);
2872 // Relocate a section during a relocatable link.
2875 const Relocate_info
<size
, big_endian
>*,
2876 unsigned int sh_type
,
2877 const unsigned char* prelocs
,
2879 Output_section
* output_section
,
2880 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
2881 const Relocatable_relocs
*,
2882 unsigned char* view
,
2883 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
2884 section_size_type view_size
,
2885 unsigned char* reloc_view
,
2886 section_size_type reloc_view_size
);
2888 // Return the symbol index to use for a target specific relocation.
2889 // The only target specific relocation is R_AARCH64_TLSDESC for a
2890 // local symbol, which is an absolute reloc.
2892 do_reloc_symbol_index(void*, unsigned int r_type
) const
2894 gold_assert(r_type
== elfcpp::R_AARCH64_TLSDESC
);
2898 // Return the addend to use for a target specific relocation.
2900 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
2902 // Return the PLT section.
2904 do_plt_address_for_global(const Symbol
* gsym
) const
2905 { return this->plt_section()->address_for_global(gsym
); }
2908 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
2909 { return this->plt_section()->address_for_local(relobj
, symndx
); }
2911 // This function should be defined in targets that can use relocation
2912 // types to determine (implemented in local_reloc_may_be_function_pointer
2913 // and global_reloc_may_be_function_pointer)
2914 // if a function's pointer is taken. ICF uses this in safe mode to only
2915 // fold those functions whose pointer is defintely not taken.
2917 do_can_check_for_function_pointers() const
2920 // Return the number of entries in the PLT.
2922 plt_entry_count() const;
2924 //Return the offset of the first non-reserved PLT entry.
2926 first_plt_entry_offset() const;
2928 // Return the size of each PLT entry.
2930 plt_entry_size() const;
2932 // Create a stub table.
2934 new_stub_table(The_aarch64_input_section
*);
2936 // Create an aarch64 input section.
2937 The_aarch64_input_section
*
2938 new_aarch64_input_section(Relobj
*, unsigned int);
2940 // Find an aarch64 input section instance for a given OBJ and SHNDX.
2941 The_aarch64_input_section
*
2942 find_aarch64_input_section(Relobj
*, unsigned int) const;
2944 // Return the thread control block size.
2946 tcb_size() const { return This::TCB_SIZE
; }
2948 // Scan a section for stub generation.
2950 scan_section_for_stubs(const Relocate_info
<size
, big_endian
>*, unsigned int,
2951 const unsigned char*, size_t, Output_section
*,
2952 bool, const unsigned char*,
2956 // Scan a relocation section for stub.
2957 template<int sh_type
>
2959 scan_reloc_section_for_stubs(
2960 const The_relocate_info
* relinfo
,
2961 const unsigned char* prelocs
,
2963 Output_section
* output_section
,
2964 bool needs_special_offset_handling
,
2965 const unsigned char* view
,
2966 Address view_address
,
2969 // Relocate a single stub.
2971 relocate_stub(The_reloc_stub
*, const Relocate_info
<size
, big_endian
>*,
2972 Output_section
*, unsigned char*, Address
,
2975 // Get the default AArch64 target.
2979 gold_assert(parameters
->target().machine_code() == elfcpp::EM_AARCH64
2980 && parameters
->target().get_size() == size
2981 && parameters
->target().is_big_endian() == big_endian
);
2982 return static_cast<This
*>(parameters
->sized_target
<size
, big_endian
>());
2986 // Scan erratum 843419 for a part of a section.
2988 scan_erratum_843419_span(
2989 AArch64_relobj
<size
, big_endian
>*,
2991 const section_size_type
,
2992 const section_size_type
,
2996 // Scan erratum 835769 for a part of a section.
2998 scan_erratum_835769_span(
2999 AArch64_relobj
<size
, big_endian
>*,
3001 const section_size_type
,
3002 const section_size_type
,
3008 do_select_as_default_target()
3010 gold_assert(aarch64_reloc_property_table
== NULL
);
3011 aarch64_reloc_property_table
= new AArch64_reloc_property_table();
3014 // Add a new reloc argument, returning the index in the vector.
3016 add_tlsdesc_info(Sized_relobj_file
<size
, big_endian
>* object
,
3019 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
3020 return this->tlsdesc_reloc_info_
.size() - 1;
3023 virtual Output_data_plt_aarch64
<size
, big_endian
>*
3024 do_make_data_plt(Layout
* layout
,
3025 Output_data_got_aarch64
<size
, big_endian
>* got
,
3026 Output_data_space
* got_plt
,
3027 Output_data_space
* got_irelative
)
3029 return new Output_data_plt_aarch64_standard
<size
, big_endian
>(
3030 layout
, got
, got_plt
, got_irelative
);
3034 // do_make_elf_object to override the same function in the base class.
3036 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
3037 const elfcpp::Ehdr
<size
, big_endian
>&);
3039 Output_data_plt_aarch64
<size
, big_endian
>*
3040 make_data_plt(Layout
* layout
,
3041 Output_data_got_aarch64
<size
, big_endian
>* got
,
3042 Output_data_space
* got_plt
,
3043 Output_data_space
* got_irelative
)
3045 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
);
3048 // We only need to generate stubs, and hence perform relaxation if we are
3049 // not doing relocatable linking.
3051 do_may_relax() const
3052 { return !parameters
->options().relocatable(); }
3054 // Relaxation hook. This is where we do stub generation.
3056 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
3059 group_sections(Layout
* layout
,
3060 section_size_type group_size
,
3061 bool stubs_always_after_branch
,
3065 scan_reloc_for_stub(const The_relocate_info
*, unsigned int,
3066 const Sized_symbol
<size
>*, unsigned int,
3067 const Symbol_value
<size
>*,
3068 typename
elfcpp::Elf_types
<size
>::Elf_Swxword
,
3071 // Make an output section.
3073 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
3074 elfcpp::Elf_Xword flags
)
3075 { return new The_aarch64_output_section(name
, type
, flags
); }
3078 // The class which scans relocations.
3083 : issued_non_pic_error_(false)
3087 local(Symbol_table
* symtab
, Layout
* layout
, Target_aarch64
* target
,
3088 Sized_relobj_file
<size
, big_endian
>* object
,
3089 unsigned int data_shndx
,
3090 Output_section
* output_section
,
3091 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
3092 const elfcpp::Sym
<size
, big_endian
>& lsym
,
3096 global(Symbol_table
* symtab
, Layout
* layout
, Target_aarch64
* target
,
3097 Sized_relobj_file
<size
, big_endian
>* object
,
3098 unsigned int data_shndx
,
3099 Output_section
* output_section
,
3100 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
3104 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
3105 Target_aarch64
<size
, big_endian
>* ,
3106 Sized_relobj_file
<size
, big_endian
>* ,
3109 const elfcpp::Rela
<size
, big_endian
>& ,
3110 unsigned int r_type
,
3111 const elfcpp::Sym
<size
, big_endian
>&);
3114 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
3115 Target_aarch64
<size
, big_endian
>* ,
3116 Sized_relobj_file
<size
, big_endian
>* ,
3119 const elfcpp::Rela
<size
, big_endian
>& ,
3120 unsigned int r_type
,
3125 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
3126 unsigned int r_type
);
3129 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
3130 unsigned int r_type
, Symbol
*);
3133 possible_function_pointer_reloc(unsigned int r_type
);
3136 check_non_pic(Relobj
*, unsigned int r_type
);
3139 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, big_endian
>*,
3140 unsigned int r_type
);
3142 // Whether we have issued an error about a non-PIC compilation.
3143 bool issued_non_pic_error_
;
3146 // The class which implements relocation.
3151 : skip_call_tls_get_addr_(false)
3157 // Do a relocation. Return false if the caller should not issue
3158 // any warnings about this relocation.
3160 relocate(const Relocate_info
<size
, big_endian
>*, Target_aarch64
*,
3162 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
3163 unsigned int r_type
, const Sized_symbol
<size
>*,
3164 const Symbol_value
<size
>*,
3165 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
3169 inline typename AArch64_relocate_functions
<size
, big_endian
>::Status
3170 relocate_tls(const Relocate_info
<size
, big_endian
>*,
3171 Target_aarch64
<size
, big_endian
>*,
3173 const elfcpp::Rela
<size
, big_endian
>&,
3174 unsigned int r_type
, const Sized_symbol
<size
>*,
3175 const Symbol_value
<size
>*,
3177 typename
elfcpp::Elf_types
<size
>::Elf_Addr
);
3179 inline typename AArch64_relocate_functions
<size
, big_endian
>::Status
3181 const Relocate_info
<size
, big_endian
>*,
3182 Target_aarch64
<size
, big_endian
>*,
3183 const elfcpp::Rela
<size
, big_endian
>&,
3186 const Symbol_value
<size
>*);
3188 inline typename AArch64_relocate_functions
<size
, big_endian
>::Status
3190 const Relocate_info
<size
, big_endian
>*,
3191 Target_aarch64
<size
, big_endian
>*,
3192 const elfcpp::Rela
<size
, big_endian
>&,
3195 const Symbol_value
<size
>*);
3197 inline typename AArch64_relocate_functions
<size
, big_endian
>::Status
3199 const Relocate_info
<size
, big_endian
>*,
3200 Target_aarch64
<size
, big_endian
>*,
3201 const elfcpp::Rela
<size
, big_endian
>&,
3204 const Symbol_value
<size
>*);
3206 inline typename AArch64_relocate_functions
<size
, big_endian
>::Status
3208 const Relocate_info
<size
, big_endian
>*,
3209 Target_aarch64
<size
, big_endian
>*,
3210 const elfcpp::Rela
<size
, big_endian
>&,
3213 const Symbol_value
<size
>*);
3215 inline typename AArch64_relocate_functions
<size
, big_endian
>::Status
3217 const Relocate_info
<size
, big_endian
>*,
3218 Target_aarch64
<size
, big_endian
>*,
3219 const elfcpp::Rela
<size
, big_endian
>&,
3222 const Symbol_value
<size
>*,
3223 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
3224 typename
elfcpp::Elf_types
<size
>::Elf_Addr
);
3226 bool skip_call_tls_get_addr_
;
3228 }; // End of class Relocate
3230 // A class which returns the size required for a relocation type,
3231 // used while scanning relocs during a relocatable link.
3232 class Relocatable_size_for_reloc
3236 get_size_for_reloc(unsigned int, Relobj
*);
3239 // Adjust TLS relocation type based on the options and whether this
3240 // is a local symbol.
3241 static tls::Tls_optimization
3242 optimize_tls_reloc(bool is_final
, int r_type
);
3244 // Get the GOT section, creating it if necessary.
3245 Output_data_got_aarch64
<size
, big_endian
>*
3246 got_section(Symbol_table
*, Layout
*);
3248 // Get the GOT PLT section.
3250 got_plt_section() const
3252 gold_assert(this->got_plt_
!= NULL
);
3253 return this->got_plt_
;
3256 // Get the GOT section for TLSDESC entries.
3257 Output_data_got
<size
, big_endian
>*
3258 got_tlsdesc_section() const
3260 gold_assert(this->got_tlsdesc_
!= NULL
);
3261 return this->got_tlsdesc_
;
3264 // Create the PLT section.
3266 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
3268 // Create a PLT entry for a global symbol.
3270 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
3272 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
3274 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
3275 Sized_relobj_file
<size
, big_endian
>* relobj
,
3276 unsigned int local_sym_index
);
3278 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
3280 define_tls_base_symbol(Symbol_table
*, Layout
*);
3282 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
3284 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
3286 // Create a GOT entry for the TLS module index.
3288 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
3289 Sized_relobj_file
<size
, big_endian
>* object
);
3291 // Get the PLT section.
3292 Output_data_plt_aarch64
<size
, big_endian
>*
3295 gold_assert(this->plt_
!= NULL
);
3299 // Helper method to create erratum stubs for ST_E_843419 and ST_E_835769. For
3300 // ST_E_843419, we need an additional field for adrp offset.
3301 void create_erratum_stub(
3302 AArch64_relobj
<size
, big_endian
>* relobj
,
3304 section_size_type erratum_insn_offset
,
3305 Address erratum_address
,
3306 typename
Insn_utilities::Insntype erratum_insn
,
3308 unsigned int e843419_adrp_offset
=0);
3310 // Return whether this is a 3-insn erratum sequence.
3311 bool is_erratum_843419_sequence(
3312 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn1
,
3313 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn2
,
3314 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn3
);
3316 // Return whether this is a 835769 sequence.
3317 // (Similarly implemented as in elfnn-aarch64.c.)
3318 bool is_erratum_835769_sequence(
3319 typename
elfcpp::Swap
<32,big_endian
>::Valtype
,
3320 typename
elfcpp::Swap
<32,big_endian
>::Valtype
);
3322 // Get the dynamic reloc section, creating it if necessary.
3324 rela_dyn_section(Layout
*);
3326 // Get the section to use for TLSDESC relocations.
3328 rela_tlsdesc_section(Layout
*) const;
3330 // Get the section to use for IRELATIVE relocations.
3332 rela_irelative_section(Layout
*);
3334 // Add a potential copy relocation.
3336 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
3337 Sized_relobj_file
<size
, big_endian
>* object
,
3338 unsigned int shndx
, Output_section
* output_section
,
3339 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
3341 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
3342 this->copy_relocs_
.copy_reloc(symtab
, layout
,
3343 symtab
->get_sized_symbol
<size
>(sym
),
3344 object
, shndx
, output_section
,
3345 r_type
, reloc
.get_r_offset(),
3346 reloc
.get_r_addend(),
3347 this->rela_dyn_section(layout
));
3350 // Information about this specific target which we pass to the
3351 // general Target structure.
3352 static const Target::Target_info aarch64_info
;
3354 // The types of GOT entries needed for this platform.
3355 // These values are exposed to the ABI in an incremental link.
3356 // Do not renumber existing values without changing the version
3357 // number of the .gnu_incremental_inputs section.
3360 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
3361 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
3362 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
3363 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
3366 // This type is used as the argument to the target specific
3367 // relocation routines. The only target specific reloc is
3368 // R_AARCh64_TLSDESC against a local symbol.
3371 Tlsdesc_info(Sized_relobj_file
<size
, big_endian
>* a_object
,
3372 unsigned int a_r_sym
)
3373 : object(a_object
), r_sym(a_r_sym
)
3376 // The object in which the local symbol is defined.
3377 Sized_relobj_file
<size
, big_endian
>* object
;
3378 // The local symbol index in the object.
3383 Output_data_got_aarch64
<size
, big_endian
>* got_
;
3385 Output_data_plt_aarch64
<size
, big_endian
>* plt_
;
3386 // The GOT PLT section.
3387 Output_data_space
* got_plt_
;
3388 // The GOT section for IRELATIVE relocations.
3389 Output_data_space
* got_irelative_
;
3390 // The GOT section for TLSDESC relocations.
3391 Output_data_got
<size
, big_endian
>* got_tlsdesc_
;
3392 // The _GLOBAL_OFFSET_TABLE_ symbol.
3393 Symbol
* global_offset_table_
;
3394 // The dynamic reloc section.
3395 Reloc_section
* rela_dyn_
;
3396 // The section to use for IRELATIVE relocs.
3397 Reloc_section
* rela_irelative_
;
3398 // Relocs saved to avoid a COPY reloc.
3399 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
3400 // Offset of the GOT entry for the TLS module index.
3401 unsigned int got_mod_index_offset_
;
3402 // We handle R_AARCH64_TLSDESC against a local symbol as a target
3403 // specific relocation. Here we store the object and local symbol
3404 // index for the relocation.
3405 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
3406 // True if the _TLS_MODULE_BASE_ symbol has been defined.
3407 bool tls_base_symbol_defined_
;
3408 // List of stub_tables
3409 Stub_table_list stub_tables_
;
3410 // Actual stub group size
3411 section_size_type stub_group_size_
;
3412 AArch64_input_section_map aarch64_input_section_map_
;
3413 }; // End of Target_aarch64
3417 const Target::Target_info Target_aarch64
<64, false>::aarch64_info
=
3420 false, // is_big_endian
3421 elfcpp::EM_AARCH64
, // machine_code
3422 false, // has_make_symbol
3423 false, // has_resolve
3424 false, // has_code_fill
3425 true, // is_default_stack_executable
3426 true, // can_icf_inline_merge_sections
3428 "/lib/ld.so.1", // program interpreter
3429 0x400000, // default_text_segment_address
3430 0x10000, // abi_pagesize (overridable by -z max-page-size)
3431 0x1000, // common_pagesize (overridable by -z common-page-size)
3432 false, // isolate_execinstr
3434 elfcpp::SHN_UNDEF
, // small_common_shndx
3435 elfcpp::SHN_UNDEF
, // large_common_shndx
3436 0, // small_common_section_flags
3437 0, // large_common_section_flags
3438 NULL
, // attributes_section
3439 NULL
, // attributes_vendor
3440 "_start", // entry_symbol_name
3441 32, // hash_entry_size
3445 const Target::Target_info Target_aarch64
<32, false>::aarch64_info
=
3448 false, // is_big_endian
3449 elfcpp::EM_AARCH64
, // machine_code
3450 false, // has_make_symbol
3451 false, // has_resolve
3452 false, // has_code_fill
3453 true, // is_default_stack_executable
3454 false, // can_icf_inline_merge_sections
3456 "/lib/ld.so.1", // program interpreter
3457 0x400000, // default_text_segment_address
3458 0x10000, // abi_pagesize (overridable by -z max-page-size)
3459 0x1000, // common_pagesize (overridable by -z common-page-size)
3460 false, // isolate_execinstr
3462 elfcpp::SHN_UNDEF
, // small_common_shndx
3463 elfcpp::SHN_UNDEF
, // large_common_shndx
3464 0, // small_common_section_flags
3465 0, // large_common_section_flags
3466 NULL
, // attributes_section
3467 NULL
, // attributes_vendor
3468 "_start", // entry_symbol_name
3469 32, // hash_entry_size
3473 const Target::Target_info Target_aarch64
<64, true>::aarch64_info
=
3476 true, // is_big_endian
3477 elfcpp::EM_AARCH64
, // machine_code
3478 false, // has_make_symbol
3479 false, // has_resolve
3480 false, // has_code_fill
3481 true, // is_default_stack_executable
3482 true, // can_icf_inline_merge_sections
3484 "/lib/ld.so.1", // program interpreter
3485 0x400000, // default_text_segment_address
3486 0x10000, // abi_pagesize (overridable by -z max-page-size)
3487 0x1000, // common_pagesize (overridable by -z common-page-size)
3488 false, // isolate_execinstr
3490 elfcpp::SHN_UNDEF
, // small_common_shndx
3491 elfcpp::SHN_UNDEF
, // large_common_shndx
3492 0, // small_common_section_flags
3493 0, // large_common_section_flags
3494 NULL
, // attributes_section
3495 NULL
, // attributes_vendor
3496 "_start", // entry_symbol_name
3497 32, // hash_entry_size
3501 const Target::Target_info Target_aarch64
<32, true>::aarch64_info
=
3504 true, // is_big_endian
3505 elfcpp::EM_AARCH64
, // machine_code
3506 false, // has_make_symbol
3507 false, // has_resolve
3508 false, // has_code_fill
3509 true, // is_default_stack_executable
3510 false, // can_icf_inline_merge_sections
3512 "/lib/ld.so.1", // program interpreter
3513 0x400000, // default_text_segment_address
3514 0x10000, // abi_pagesize (overridable by -z max-page-size)
3515 0x1000, // common_pagesize (overridable by -z common-page-size)
3516 false, // isolate_execinstr
3518 elfcpp::SHN_UNDEF
, // small_common_shndx
3519 elfcpp::SHN_UNDEF
, // large_common_shndx
3520 0, // small_common_section_flags
3521 0, // large_common_section_flags
3522 NULL
, // attributes_section
3523 NULL
, // attributes_vendor
3524 "_start", // entry_symbol_name
3525 32, // hash_entry_size
3528 // Get the GOT section, creating it if necessary.
3530 template<int size
, bool big_endian
>
3531 Output_data_got_aarch64
<size
, big_endian
>*
3532 Target_aarch64
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
3535 if (this->got_
== NULL
)
3537 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
3539 // When using -z now, we can treat .got.plt as a relro section.
3540 // Without -z now, it is modified after program startup by lazy
3542 bool is_got_plt_relro
= parameters
->options().now();
3543 Output_section_order got_order
= (is_got_plt_relro
3545 : ORDER_RELRO_LAST
);
3546 Output_section_order got_plt_order
= (is_got_plt_relro
3548 : ORDER_NON_RELRO_FIRST
);
3550 // Layout of .got and .got.plt sections.
3551 // .got[0] &_DYNAMIC <-_GLOBAL_OFFSET_TABLE_
3553 // .gotplt[0] reserved for ld.so (&linkmap) <--DT_PLTGOT
3554 // .gotplt[1] reserved for ld.so (resolver)
3555 // .gotplt[2] reserved
3557 // Generate .got section.
3558 this->got_
= new Output_data_got_aarch64
<size
, big_endian
>(symtab
,
3560 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3561 (elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
),
3562 this->got_
, got_order
, true);
3563 // The first word of GOT is reserved for the address of .dynamic.
3564 // We put 0 here now. The value will be replaced later in
3565 // Output_data_got_aarch64::do_write.
3566 this->got_
->add_constant(0);
3568 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3569 // _GLOBAL_OFFSET_TABLE_ value points to the start of the .got section,
3570 // even if there is a .got.plt section.
3571 this->global_offset_table_
=
3572 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
3573 Symbol_table::PREDEFINED
,
3575 0, 0, elfcpp::STT_OBJECT
,
3577 elfcpp::STV_HIDDEN
, 0,
3580 // Generate .got.plt section.
3581 this->got_plt_
= new Output_data_space(size
/ 8, "** GOT PLT");
3582 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
3584 | elfcpp::SHF_WRITE
),
3585 this->got_plt_
, got_plt_order
,
3588 // The first three entries are reserved.
3589 this->got_plt_
->set_current_data_size(
3590 AARCH64_GOTPLT_RESERVE_COUNT
* (size
/ 8));
3592 // If there are any IRELATIVE relocations, they get GOT entries
3593 // in .got.plt after the jump slot entries.
3594 this->got_irelative_
= new Output_data_space(size
/ 8,
3595 "** GOT IRELATIVE PLT");
3596 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
3598 | elfcpp::SHF_WRITE
),
3599 this->got_irelative_
,
3603 // If there are any TLSDESC relocations, they get GOT entries in
3604 // .got.plt after the jump slot and IRELATIVE entries.
3605 this->got_tlsdesc_
= new Output_data_got
<size
, big_endian
>();
3606 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
3608 | elfcpp::SHF_WRITE
),
3613 if (!is_got_plt_relro
)
3615 // Those bytes can go into the relro segment.
3616 layout
->increase_relro(
3617 AARCH64_GOTPLT_RESERVE_COUNT
* (size
/ 8));
3624 // Get the dynamic reloc section, creating it if necessary.
3626 template<int size
, bool big_endian
>
3627 typename Target_aarch64
<size
, big_endian
>::Reloc_section
*
3628 Target_aarch64
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
3630 if (this->rela_dyn_
== NULL
)
3632 gold_assert(layout
!= NULL
);
3633 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
3634 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
3635 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
3636 ORDER_DYNAMIC_RELOCS
, false);
3638 return this->rela_dyn_
;
3641 // Get the section to use for IRELATIVE relocs, creating it if
3642 // necessary. These go in .rela.dyn, but only after all other dynamic
3643 // relocations. They need to follow the other dynamic relocations so
3644 // that they can refer to global variables initialized by those
3647 template<int size
, bool big_endian
>
3648 typename Target_aarch64
<size
, big_endian
>::Reloc_section
*
3649 Target_aarch64
<size
, big_endian
>::rela_irelative_section(Layout
* layout
)
3651 if (this->rela_irelative_
== NULL
)
3653 // Make sure we have already created the dynamic reloc section.
3654 this->rela_dyn_section(layout
);
3655 this->rela_irelative_
= new Reloc_section(false);
3656 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
3657 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
3658 ORDER_DYNAMIC_RELOCS
, false);
3659 gold_assert(this->rela_dyn_
->output_section()
3660 == this->rela_irelative_
->output_section());
3662 return this->rela_irelative_
;
3666 // do_make_elf_object to override the same function in the base class. We need
3667 // to use a target-specific sub-class of Sized_relobj_file<size, big_endian> to
3668 // store backend specific information. Hence we need to have our own ELF object
3671 template<int size
, bool big_endian
>
3673 Target_aarch64
<size
, big_endian
>::do_make_elf_object(
3674 const std::string
& name
,
3675 Input_file
* input_file
,
3676 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
3678 int et
= ehdr
.get_e_type();
3679 // ET_EXEC files are valid input for --just-symbols/-R,
3680 // and we treat them as relocatable objects.
3681 if (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols())
3682 return Sized_target
<size
, big_endian
>::do_make_elf_object(
3683 name
, input_file
, offset
, ehdr
);
3684 else if (et
== elfcpp::ET_REL
)
3686 AArch64_relobj
<size
, big_endian
>* obj
=
3687 new AArch64_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
3691 else if (et
== elfcpp::ET_DYN
)
3693 // Keep base implementation.
3694 Sized_dynobj
<size
, big_endian
>* obj
=
3695 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
3701 gold_error(_("%s: unsupported ELF file type %d"),
3708 // Scan a relocation for stub generation.
3710 template<int size
, bool big_endian
>
3712 Target_aarch64
<size
, big_endian
>::scan_reloc_for_stub(
3713 const Relocate_info
<size
, big_endian
>* relinfo
,
3714 unsigned int r_type
,
3715 const Sized_symbol
<size
>* gsym
,
3717 const Symbol_value
<size
>* psymval
,
3718 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
,
3721 const AArch64_relobj
<size
, big_endian
>* aarch64_relobj
=
3722 static_cast<AArch64_relobj
<size
, big_endian
>*>(relinfo
->object
);
3724 Symbol_value
<size
> symval
;
3727 const AArch64_reloc_property
* arp
= aarch64_reloc_property_table
->
3728 get_reloc_property(r_type
);
3729 if (gsym
->use_plt_offset(arp
->reference_flags()))
3731 // This uses a PLT, change the symbol value.
3732 symval
.set_output_value(this->plt_section()->address()
3733 + gsym
->plt_offset());
3736 else if (gsym
->is_undefined())
3737 // There is no need to generate a stub symbol is undefined.
3741 // Get the symbol value.
3742 typename Symbol_value
<size
>::Value value
= psymval
->value(aarch64_relobj
, 0);
3744 // Owing to pipelining, the PC relative branches below actually skip
3745 // two instructions when the branch offset is 0.
3746 Address destination
= static_cast<Address
>(-1);
3749 case elfcpp::R_AARCH64_CALL26
:
3750 case elfcpp::R_AARCH64_JUMP26
:
3751 destination
= value
+ addend
;
3757 int stub_type
= The_reloc_stub::
3758 stub_type_for_reloc(r_type
, address
, destination
);
3759 if (stub_type
== ST_NONE
)
3762 The_stub_table
* stub_table
= aarch64_relobj
->stub_table(relinfo
->data_shndx
);
3763 gold_assert(stub_table
!= NULL
);
3765 The_reloc_stub_key
key(stub_type
, gsym
, aarch64_relobj
, r_sym
, addend
);
3766 The_reloc_stub
* stub
= stub_table
->find_reloc_stub(key
);
3769 stub
= new The_reloc_stub(stub_type
);
3770 stub_table
->add_reloc_stub(stub
, key
);
3772 stub
->set_destination_address(destination
);
3773 } // End of Target_aarch64::scan_reloc_for_stub
3776 // This function scans a relocation section for stub generation.
3777 // The template parameter Relocate must be a class type which provides
3778 // a single function, relocate(), which implements the machine
3779 // specific part of a relocation.
3781 // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
3782 // SHT_REL or SHT_RELA.
3784 // PRELOCS points to the relocation data. RELOC_COUNT is the number
3785 // of relocs. OUTPUT_SECTION is the output section.
3786 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
3787 // mapped to output offsets.
3789 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
3790 // VIEW_SIZE is the size. These refer to the input section, unless
3791 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
3792 // the output section.
3794 template<int size
, bool big_endian
>
3795 template<int sh_type
>
3797 Target_aarch64
<size
, big_endian
>::scan_reloc_section_for_stubs(
3798 const Relocate_info
<size
, big_endian
>* relinfo
,
3799 const unsigned char* prelocs
,
3801 Output_section
* /*output_section*/,
3802 bool /*needs_special_offset_handling*/,
3803 const unsigned char* /*view*/,
3804 Address view_address
,
3807 typedef typename Reloc_types
<sh_type
,size
,big_endian
>::Reloc Reltype
;
3809 const int reloc_size
=
3810 Reloc_types
<sh_type
,size
,big_endian
>::reloc_size
;
3811 AArch64_relobj
<size
, big_endian
>* object
=
3812 static_cast<AArch64_relobj
<size
, big_endian
>*>(relinfo
->object
);
3813 unsigned int local_count
= object
->local_symbol_count();
3815 gold::Default_comdat_behavior default_comdat_behavior
;
3816 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
3818 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
3820 Reltype
reloc(prelocs
);
3821 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
3822 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
3823 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
3824 if (r_type
!= elfcpp::R_AARCH64_CALL26
3825 && r_type
!= elfcpp::R_AARCH64_JUMP26
)
3828 section_offset_type offset
=
3829 convert_to_section_size_type(reloc
.get_r_offset());
3832 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
=
3833 reloc
.get_r_addend();
3835 const Sized_symbol
<size
>* sym
;
3836 Symbol_value
<size
> symval
;
3837 const Symbol_value
<size
> *psymval
;
3838 bool is_defined_in_discarded_section
;
3840 if (r_sym
< local_count
)
3843 psymval
= object
->local_symbol(r_sym
);
3845 // If the local symbol belongs to a section we are discarding,
3846 // and that section is a debug section, try to find the
3847 // corresponding kept section and map this symbol to its
3848 // counterpart in the kept section. The symbol must not
3849 // correspond to a section we are folding.
3851 shndx
= psymval
->input_shndx(&is_ordinary
);
3852 is_defined_in_discarded_section
=
3854 && shndx
!= elfcpp::SHN_UNDEF
3855 && !object
->is_section_included(shndx
)
3856 && !relinfo
->symtab
->is_section_folded(object
, shndx
));
3858 // We need to compute the would-be final value of this local
3860 if (!is_defined_in_discarded_section
)
3862 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
3863 typename
ObjType::Compute_final_local_value_status status
=
3864 object
->compute_final_local_value(r_sym
, psymval
, &symval
,
3866 if (status
== ObjType::CFLV_OK
)
3868 // Currently we cannot handle a branch to a target in
3869 // a merged section. If this is the case, issue an error
3870 // and also free the merge symbol value.
3871 if (!symval
.has_output_value())
3873 const std::string
& section_name
=
3874 object
->section_name(shndx
);
3875 object
->error(_("cannot handle branch to local %u "
3876 "in a merged section %s"),
3877 r_sym
, section_name
.c_str());
3883 // We cannot determine the final value.
3891 gsym
= object
->global_symbol(r_sym
);
3892 gold_assert(gsym
!= NULL
);
3893 if (gsym
->is_forwarder())
3894 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
3896 sym
= static_cast<const Sized_symbol
<size
>*>(gsym
);
3897 if (sym
->has_symtab_index() && sym
->symtab_index() != -1U)
3898 symval
.set_output_symtab_index(sym
->symtab_index());
3900 symval
.set_no_output_symtab_entry();
3902 // We need to compute the would-be final value of this global
3904 const Symbol_table
* symtab
= relinfo
->symtab
;
3905 const Sized_symbol
<size
>* sized_symbol
=
3906 symtab
->get_sized_symbol
<size
>(gsym
);
3907 Symbol_table::Compute_final_value_status status
;
3908 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
=
3909 symtab
->compute_final_value
<size
>(sized_symbol
, &status
);
3911 // Skip this if the symbol has not output section.
3912 if (status
== Symbol_table::CFVS_NO_OUTPUT_SECTION
)
3914 symval
.set_output_value(value
);
3916 if (gsym
->type() == elfcpp::STT_TLS
)
3917 symval
.set_is_tls_symbol();
3918 else if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
3919 symval
.set_is_ifunc_symbol();
3922 is_defined_in_discarded_section
=
3923 (gsym
->is_defined_in_discarded_section()
3924 && gsym
->is_undefined());
3928 Symbol_value
<size
> symval2
;
3929 if (is_defined_in_discarded_section
)
3931 if (comdat_behavior
== CB_UNDETERMINED
)
3933 std::string name
= object
->section_name(relinfo
->data_shndx
);
3934 comdat_behavior
= default_comdat_behavior
.get(name
.c_str());
3936 if (comdat_behavior
== CB_PRETEND
)
3939 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
=
3940 object
->map_to_kept_section(shndx
, &found
);
3942 symval2
.set_output_value(value
+ psymval
->input_value());
3944 symval2
.set_output_value(0);
3948 if (comdat_behavior
== CB_WARNING
)
3949 gold_warning_at_location(relinfo
, i
, offset
,
3950 _("relocation refers to discarded "
3952 symval2
.set_output_value(0);
3954 symval2
.set_no_output_symtab_entry();
3958 // If symbol is a section symbol, we don't know the actual type of
3959 // destination. Give up.
3960 if (psymval
->is_section_symbol())
3963 this->scan_reloc_for_stub(relinfo
, r_type
, sym
, r_sym
, psymval
,
3964 addend
, view_address
+ offset
);
3965 } // End of iterating relocs in a section
3966 } // End of Target_aarch64::scan_reloc_section_for_stubs
3969 // Scan an input section for stub generation.
3971 template<int size
, bool big_endian
>
3973 Target_aarch64
<size
, big_endian
>::scan_section_for_stubs(
3974 const Relocate_info
<size
, big_endian
>* relinfo
,
3975 unsigned int sh_type
,
3976 const unsigned char* prelocs
,
3978 Output_section
* output_section
,
3979 bool needs_special_offset_handling
,
3980 const unsigned char* view
,
3981 Address view_address
,
3982 section_size_type view_size
)
3984 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3985 this->scan_reloc_section_for_stubs
<elfcpp::SHT_RELA
>(
3990 needs_special_offset_handling
,
3997 // Relocate a single stub.
3999 template<int size
, bool big_endian
>
4000 void Target_aarch64
<size
, big_endian
>::
4001 relocate_stub(The_reloc_stub
* stub
,
4002 const The_relocate_info
*,
4004 unsigned char* view
,
4008 typedef AArch64_relocate_functions
<size
, big_endian
> The_reloc_functions
;
4009 typedef typename
The_reloc_functions::Status The_reloc_functions_status
;
4010 typedef typename
elfcpp::Swap
<32,big_endian
>::Valtype Insntype
;
4012 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
4013 int insn_number
= stub
->insn_num();
4014 const uint32_t* insns
= stub
->insns();
4015 // Check the insns are really those stub insns.
4016 for (int i
= 0; i
< insn_number
; ++i
)
4018 Insntype insn
= elfcpp::Swap
<32,big_endian
>::readval(ip
+ i
);
4019 gold_assert(((uint32_t)insn
== insns
[i
]));
4022 Address dest
= stub
->destination_address();
4024 switch(stub
->type())
4026 case ST_ADRP_BRANCH
:
4028 // 1st reloc is ADR_PREL_PG_HI21
4029 The_reloc_functions_status status
=
4030 The_reloc_functions::adrp(view
, dest
, address
);
4031 // An error should never arise in the above step. If so, please
4032 // check 'aarch64_valid_for_adrp_p'.
4033 gold_assert(status
== The_reloc_functions::STATUS_OKAY
);
4035 // 2nd reloc is ADD_ABS_LO12_NC
4036 const AArch64_reloc_property
* arp
=
4037 aarch64_reloc_property_table
->get_reloc_property(
4038 elfcpp::R_AARCH64_ADD_ABS_LO12_NC
);
4039 gold_assert(arp
!= NULL
);
4040 status
= The_reloc_functions::template
4041 rela_general
<32>(view
+ 4, dest
, 0, arp
);
4042 // An error should never arise, it is an "_NC" relocation.
4043 gold_assert(status
== The_reloc_functions::STATUS_OKAY
);
4047 case ST_LONG_BRANCH_ABS
:
4048 // 1st reloc is R_AARCH64_PREL64, at offset 8
4049 elfcpp::Swap
<64,big_endian
>::writeval(view
+ 8, dest
);
4052 case ST_LONG_BRANCH_PCREL
:
4054 // "PC" calculation is the 2nd insn in the stub.
4055 uint64_t offset
= dest
- (address
+ 4);
4056 // Offset is placed at offset 4 and 5.
4057 elfcpp::Swap
<64,big_endian
>::writeval(view
+ 16, offset
);
4067 // A class to handle the PLT data.
4068 // This is an abstract base class that handles most of the linker details
4069 // but does not know the actual contents of PLT entries. The derived
4070 // classes below fill in those details.
4072 template<int size
, bool big_endian
>
4073 class Output_data_plt_aarch64
: public Output_section_data
4076 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>
4078 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4080 Output_data_plt_aarch64(Layout
* layout
,
4082 Output_data_got_aarch64
<size
, big_endian
>* got
,
4083 Output_data_space
* got_plt
,
4084 Output_data_space
* got_irelative
)
4085 : Output_section_data(addralign
), tlsdesc_rel_(NULL
), irelative_rel_(NULL
),
4086 got_(got
), got_plt_(got_plt
), got_irelative_(got_irelative
),
4087 count_(0), irelative_count_(0), tlsdesc_got_offset_(-1U)
4088 { this->init(layout
); }
4090 // Initialize the PLT section.
4092 init(Layout
* layout
);
4094 // Add an entry to the PLT.
4096 add_entry(Symbol_table
*, Layout
*, Symbol
* gsym
);
4098 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
4100 add_local_ifunc_entry(Symbol_table
* symtab
, Layout
*,
4101 Sized_relobj_file
<size
, big_endian
>* relobj
,
4102 unsigned int local_sym_index
);
4104 // Add the relocation for a PLT entry.
4106 add_relocation(Symbol_table
*, Layout
*, Symbol
* gsym
,
4107 unsigned int got_offset
);
4109 // Add the reserved TLSDESC_PLT entry to the PLT.
4111 reserve_tlsdesc_entry(unsigned int got_offset
)
4112 { this->tlsdesc_got_offset_
= got_offset
; }
4114 // Return true if a TLSDESC_PLT entry has been reserved.
4116 has_tlsdesc_entry() const
4117 { return this->tlsdesc_got_offset_
!= -1U; }
4119 // Return the GOT offset for the reserved TLSDESC_PLT entry.
4121 get_tlsdesc_got_offset() const
4122 { return this->tlsdesc_got_offset_
; }
4124 // Return the PLT offset of the reserved TLSDESC_PLT entry.
4126 get_tlsdesc_plt_offset() const
4128 return (this->first_plt_entry_offset() +
4129 (this->count_
+ this->irelative_count_
)
4130 * this->get_plt_entry_size());
4133 // Return the .rela.plt section data.
4136 { return this->rel_
; }
4138 // Return where the TLSDESC relocations should go.
4140 rela_tlsdesc(Layout
*);
4142 // Return where the IRELATIVE relocations should go in the PLT
4145 rela_irelative(Symbol_table
*, Layout
*);
4147 // Return whether we created a section for IRELATIVE relocations.
4149 has_irelative_section() const
4150 { return this->irelative_rel_
!= NULL
; }
4152 // Return the number of PLT entries.
4155 { return this->count_
+ this->irelative_count_
; }
4157 // Return the offset of the first non-reserved PLT entry.
4159 first_plt_entry_offset() const
4160 { return this->do_first_plt_entry_offset(); }
4162 // Return the size of a PLT entry.
4164 get_plt_entry_size() const
4165 { return this->do_get_plt_entry_size(); }
4167 // Return the reserved tlsdesc entry size.
4169 get_plt_tlsdesc_entry_size() const
4170 { return this->do_get_plt_tlsdesc_entry_size(); }
4172 // Return the PLT address to use for a global symbol.
4174 address_for_global(const Symbol
*);
4176 // Return the PLT address to use for a local symbol.
4178 address_for_local(const Relobj
*, unsigned int symndx
);
4181 // Fill in the first PLT entry.
4183 fill_first_plt_entry(unsigned char* pov
,
4184 Address got_address
,
4185 Address plt_address
)
4186 { this->do_fill_first_plt_entry(pov
, got_address
, plt_address
); }
4188 // Fill in a normal PLT entry.
4190 fill_plt_entry(unsigned char* pov
,
4191 Address got_address
,
4192 Address plt_address
,
4193 unsigned int got_offset
,
4194 unsigned int plt_offset
)
4196 this->do_fill_plt_entry(pov
, got_address
, plt_address
,
4197 got_offset
, plt_offset
);
4200 // Fill in the reserved TLSDESC PLT entry.
4202 fill_tlsdesc_entry(unsigned char* pov
,
4203 Address gotplt_address
,
4204 Address plt_address
,
4206 unsigned int tlsdesc_got_offset
,
4207 unsigned int plt_offset
)
4209 this->do_fill_tlsdesc_entry(pov
, gotplt_address
, plt_address
, got_base
,
4210 tlsdesc_got_offset
, plt_offset
);
4213 virtual unsigned int
4214 do_first_plt_entry_offset() const = 0;
4216 virtual unsigned int
4217 do_get_plt_entry_size() const = 0;
4219 virtual unsigned int
4220 do_get_plt_tlsdesc_entry_size() const = 0;
4223 do_fill_first_plt_entry(unsigned char* pov
,
4225 Address plt_addr
) = 0;
4228 do_fill_plt_entry(unsigned char* pov
,
4229 Address got_address
,
4230 Address plt_address
,
4231 unsigned int got_offset
,
4232 unsigned int plt_offset
) = 0;
4235 do_fill_tlsdesc_entry(unsigned char* pov
,
4236 Address gotplt_address
,
4237 Address plt_address
,
4239 unsigned int tlsdesc_got_offset
,
4240 unsigned int plt_offset
) = 0;
4243 do_adjust_output_section(Output_section
* os
);
4245 // Write to a map file.
4247 do_print_to_mapfile(Mapfile
* mapfile
) const
4248 { mapfile
->print_output_data(this, _("** PLT")); }
4251 // Set the final size.
4253 set_final_data_size();
4255 // Write out the PLT data.
4257 do_write(Output_file
*);
4259 // The reloc section.
4260 Reloc_section
* rel_
;
4262 // The TLSDESC relocs, if necessary. These must follow the regular
4264 Reloc_section
* tlsdesc_rel_
;
4266 // The IRELATIVE relocs, if necessary. These must follow the
4267 // regular PLT relocations.
4268 Reloc_section
* irelative_rel_
;
4270 // The .got section.
4271 Output_data_got_aarch64
<size
, big_endian
>* got_
;
4273 // The .got.plt section.
4274 Output_data_space
* got_plt_
;
4276 // The part of the .got.plt section used for IRELATIVE relocs.
4277 Output_data_space
* got_irelative_
;
4279 // The number of PLT entries.
4280 unsigned int count_
;
4282 // Number of PLT entries with R_AARCH64_IRELATIVE relocs. These
4283 // follow the regular PLT entries.
4284 unsigned int irelative_count_
;
4286 // GOT offset of the reserved TLSDESC_GOT entry for the lazy trampoline.
4287 // Communicated to the loader via DT_TLSDESC_GOT. The magic value -1
4288 // indicates an offset is not allocated.
4289 unsigned int tlsdesc_got_offset_
;
4292 // Initialize the PLT section.
4294 template<int size
, bool big_endian
>
4296 Output_data_plt_aarch64
<size
, big_endian
>::init(Layout
* layout
)
4298 this->rel_
= new Reloc_section(false);
4299 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
4300 elfcpp::SHF_ALLOC
, this->rel_
,
4301 ORDER_DYNAMIC_PLT_RELOCS
, false);
4304 template<int size
, bool big_endian
>
4306 Output_data_plt_aarch64
<size
, big_endian
>::do_adjust_output_section(
4309 os
->set_entsize(this->get_plt_entry_size());
4312 // Add an entry to the PLT.
4314 template<int size
, bool big_endian
>
4316 Output_data_plt_aarch64
<size
, big_endian
>::add_entry(Symbol_table
* symtab
,
4317 Layout
* layout
, Symbol
* gsym
)
4319 gold_assert(!gsym
->has_plt_offset());
4321 unsigned int* pcount
;
4322 unsigned int plt_reserved
;
4323 Output_section_data_build
* got
;
4325 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4326 && gsym
->can_use_relative_reloc(false))
4328 pcount
= &this->irelative_count_
;
4330 got
= this->got_irelative_
;
4334 pcount
= &this->count_
;
4335 plt_reserved
= this->first_plt_entry_offset();
4336 got
= this->got_plt_
;
4339 gsym
->set_plt_offset((*pcount
) * this->get_plt_entry_size()
4344 section_offset_type got_offset
= got
->current_data_size();
4346 // Every PLT entry needs a GOT entry which points back to the PLT
4347 // entry (this will be changed by the dynamic linker, normally
4348 // lazily when the function is called).
4349 got
->set_current_data_size(got_offset
+ size
/ 8);
4351 // Every PLT entry needs a reloc.
4352 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
4354 // Note that we don't need to save the symbol. The contents of the
4355 // PLT are independent of which symbols are used. The symbols only
4356 // appear in the relocations.
4359 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
4362 template<int size
, bool big_endian
>
4364 Output_data_plt_aarch64
<size
, big_endian
>::add_local_ifunc_entry(
4365 Symbol_table
* symtab
,
4367 Sized_relobj_file
<size
, big_endian
>* relobj
,
4368 unsigned int local_sym_index
)
4370 unsigned int plt_offset
= this->irelative_count_
* this->get_plt_entry_size();
4371 ++this->irelative_count_
;
4373 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
4375 // Every PLT entry needs a GOT entry which points back to the PLT
4377 this->got_irelative_
->set_current_data_size(got_offset
+ size
/ 8);
4379 // Every PLT entry needs a reloc.
4380 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
4381 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
4382 elfcpp::R_AARCH64_IRELATIVE
,
4383 this->got_irelative_
, got_offset
, 0);
4388 // Add the relocation for a PLT entry.
4390 template<int size
, bool big_endian
>
4392 Output_data_plt_aarch64
<size
, big_endian
>::add_relocation(
4393 Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
, unsigned int got_offset
)
4395 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4396 && gsym
->can_use_relative_reloc(false))
4398 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
4399 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_AARCH64_IRELATIVE
,
4400 this->got_irelative_
, got_offset
, 0);
4404 gsym
->set_needs_dynsym_entry();
4405 this->rel_
->add_global(gsym
, elfcpp::R_AARCH64_JUMP_SLOT
, this->got_plt_
,
4410 // Return where the TLSDESC relocations should go, creating it if
4411 // necessary. These follow the JUMP_SLOT relocations.
4413 template<int size
, bool big_endian
>
4414 typename Output_data_plt_aarch64
<size
, big_endian
>::Reloc_section
*
4415 Output_data_plt_aarch64
<size
, big_endian
>::rela_tlsdesc(Layout
* layout
)
4417 if (this->tlsdesc_rel_
== NULL
)
4419 this->tlsdesc_rel_
= new Reloc_section(false);
4420 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
4421 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
4422 ORDER_DYNAMIC_PLT_RELOCS
, false);
4423 gold_assert(this->tlsdesc_rel_
->output_section()
4424 == this->rel_
->output_section());
4426 return this->tlsdesc_rel_
;
4429 // Return where the IRELATIVE relocations should go in the PLT. These
4430 // follow the JUMP_SLOT and the TLSDESC relocations.
4432 template<int size
, bool big_endian
>
4433 typename Output_data_plt_aarch64
<size
, big_endian
>::Reloc_section
*
4434 Output_data_plt_aarch64
<size
, big_endian
>::rela_irelative(Symbol_table
* symtab
,
4437 if (this->irelative_rel_
== NULL
)
4439 // Make sure we have a place for the TLSDESC relocations, in
4440 // case we see any later on.
4441 this->rela_tlsdesc(layout
);
4442 this->irelative_rel_
= new Reloc_section(false);
4443 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
4444 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
4445 ORDER_DYNAMIC_PLT_RELOCS
, false);
4446 gold_assert(this->irelative_rel_
->output_section()
4447 == this->rel_
->output_section());
4449 if (parameters
->doing_static_link())
4451 // A statically linked executable will only have a .rela.plt
4452 // section to hold R_AARCH64_IRELATIVE relocs for
4453 // STT_GNU_IFUNC symbols. The library will use these
4454 // symbols to locate the IRELATIVE relocs at program startup
4456 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
4457 Symbol_table::PREDEFINED
,
4458 this->irelative_rel_
, 0, 0,
4459 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
4460 elfcpp::STV_HIDDEN
, 0, false, true);
4461 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
4462 Symbol_table::PREDEFINED
,
4463 this->irelative_rel_
, 0, 0,
4464 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
4465 elfcpp::STV_HIDDEN
, 0, true, true);
4468 return this->irelative_rel_
;
4471 // Return the PLT address to use for a global symbol.
4473 template<int size
, bool big_endian
>
4475 Output_data_plt_aarch64
<size
, big_endian
>::address_for_global(
4478 uint64_t offset
= 0;
4479 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4480 && gsym
->can_use_relative_reloc(false))
4481 offset
= (this->first_plt_entry_offset() +
4482 this->count_
* this->get_plt_entry_size());
4483 return this->address() + offset
+ gsym
->plt_offset();
4486 // Return the PLT address to use for a local symbol. These are always
4487 // IRELATIVE relocs.
4489 template<int size
, bool big_endian
>
4491 Output_data_plt_aarch64
<size
, big_endian
>::address_for_local(
4492 const Relobj
* object
,
4495 return (this->address()
4496 + this->first_plt_entry_offset()
4497 + this->count_
* this->get_plt_entry_size()
4498 + object
->local_plt_offset(r_sym
));
4501 // Set the final size.
4503 template<int size
, bool big_endian
>
4505 Output_data_plt_aarch64
<size
, big_endian
>::set_final_data_size()
4507 unsigned int count
= this->count_
+ this->irelative_count_
;
4508 unsigned int extra_size
= 0;
4509 if (this->has_tlsdesc_entry())
4510 extra_size
+= this->get_plt_tlsdesc_entry_size();
4511 this->set_data_size(this->first_plt_entry_offset()
4512 + count
* this->get_plt_entry_size()
4516 template<int size
, bool big_endian
>
4517 class Output_data_plt_aarch64_standard
:
4518 public Output_data_plt_aarch64
<size
, big_endian
>
4521 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4522 Output_data_plt_aarch64_standard(
4524 Output_data_got_aarch64
<size
, big_endian
>* got
,
4525 Output_data_space
* got_plt
,
4526 Output_data_space
* got_irelative
)
4527 : Output_data_plt_aarch64
<size
, big_endian
>(layout
,
4534 // Return the offset of the first non-reserved PLT entry.
4535 virtual unsigned int
4536 do_first_plt_entry_offset() const
4537 { return this->first_plt_entry_size
; }
4539 // Return the size of a PLT entry
4540 virtual unsigned int
4541 do_get_plt_entry_size() const
4542 { return this->plt_entry_size
; }
4544 // Return the size of a tlsdesc entry
4545 virtual unsigned int
4546 do_get_plt_tlsdesc_entry_size() const
4547 { return this->plt_tlsdesc_entry_size
; }
4550 do_fill_first_plt_entry(unsigned char* pov
,
4551 Address got_address
,
4552 Address plt_address
);
4555 do_fill_plt_entry(unsigned char* pov
,
4556 Address got_address
,
4557 Address plt_address
,
4558 unsigned int got_offset
,
4559 unsigned int plt_offset
);
4562 do_fill_tlsdesc_entry(unsigned char* pov
,
4563 Address gotplt_address
,
4564 Address plt_address
,
4566 unsigned int tlsdesc_got_offset
,
4567 unsigned int plt_offset
);
4570 // The size of the first plt entry size.
4571 static const int first_plt_entry_size
= 32;
4572 // The size of the plt entry size.
4573 static const int plt_entry_size
= 16;
4574 // The size of the plt tlsdesc entry size.
4575 static const int plt_tlsdesc_entry_size
= 32;
4576 // Template for the first PLT entry.
4577 static const uint32_t first_plt_entry
[first_plt_entry_size
/ 4];
4578 // Template for subsequent PLT entries.
4579 static const uint32_t plt_entry
[plt_entry_size
/ 4];
4580 // The reserved TLSDESC entry in the PLT for an executable.
4581 static const uint32_t tlsdesc_plt_entry
[plt_tlsdesc_entry_size
/ 4];
4584 // The first entry in the PLT for an executable.
4588 Output_data_plt_aarch64_standard
<32, false>::
4589 first_plt_entry
[first_plt_entry_size
/ 4] =
4591 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4592 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4593 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4594 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4595 0xd61f0220, /* br x17 */
4596 0xd503201f, /* nop */
4597 0xd503201f, /* nop */
4598 0xd503201f, /* nop */
4604 Output_data_plt_aarch64_standard
<32, true>::
4605 first_plt_entry
[first_plt_entry_size
/ 4] =
4607 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4608 0x90000010, /* adrp x16, PLT_GOT+0x8 */
4609 0xb9400A11, /* ldr w17, [x16, #PLT_GOT+0x8] */
4610 0x11002210, /* add w16, w16,#PLT_GOT+0x8 */
4611 0xd61f0220, /* br x17 */
4612 0xd503201f, /* nop */
4613 0xd503201f, /* nop */
4614 0xd503201f, /* nop */
4620 Output_data_plt_aarch64_standard
<64, false>::
4621 first_plt_entry
[first_plt_entry_size
/ 4] =
4623 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4624 0x90000010, /* adrp x16, PLT_GOT+16 */
4625 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4626 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4627 0xd61f0220, /* br x17 */
4628 0xd503201f, /* nop */
4629 0xd503201f, /* nop */
4630 0xd503201f, /* nop */
4636 Output_data_plt_aarch64_standard
<64, true>::
4637 first_plt_entry
[first_plt_entry_size
/ 4] =
4639 0xa9bf7bf0, /* stp x16, x30, [sp, #-16]! */
4640 0x90000010, /* adrp x16, PLT_GOT+16 */
4641 0xf9400A11, /* ldr x17, [x16, #PLT_GOT+0x10] */
4642 0x91004210, /* add x16, x16,#PLT_GOT+0x10 */
4643 0xd61f0220, /* br x17 */
4644 0xd503201f, /* nop */
4645 0xd503201f, /* nop */
4646 0xd503201f, /* nop */
4652 Output_data_plt_aarch64_standard
<32, false>::
4653 plt_entry
[plt_entry_size
/ 4] =
4655 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4656 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4657 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4658 0xd61f0220, /* br x17. */
4664 Output_data_plt_aarch64_standard
<32, true>::
4665 plt_entry
[plt_entry_size
/ 4] =
4667 0x90000010, /* adrp x16, PLTGOT + n * 4 */
4668 0xb9400211, /* ldr w17, [w16, PLTGOT + n * 4] */
4669 0x11000210, /* add w16, w16, :lo12:PLTGOT + n * 4 */
4670 0xd61f0220, /* br x17. */
4676 Output_data_plt_aarch64_standard
<64, false>::
4677 plt_entry
[plt_entry_size
/ 4] =
4679 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4680 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4681 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4682 0xd61f0220, /* br x17. */
4688 Output_data_plt_aarch64_standard
<64, true>::
4689 plt_entry
[plt_entry_size
/ 4] =
4691 0x90000010, /* adrp x16, PLTGOT + n * 8 */
4692 0xf9400211, /* ldr x17, [x16, PLTGOT + n * 8] */
4693 0x91000210, /* add x16, x16, :lo12:PLTGOT + n * 8 */
4694 0xd61f0220, /* br x17. */
4698 template<int size
, bool big_endian
>
4700 Output_data_plt_aarch64_standard
<size
, big_endian
>::do_fill_first_plt_entry(
4702 Address got_address
,
4703 Address plt_address
)
4705 // PLT0 of the small PLT looks like this in ELF64 -
4706 // stp x16, x30, [sp, #-16]! Save the reloc and lr on stack.
4707 // adrp x16, PLT_GOT + 16 Get the page base of the GOTPLT
4708 // ldr x17, [x16, #:lo12:PLT_GOT+16] Load the address of the
4710 // add x16, x16, #:lo12:PLT_GOT+16 Load the lo12 bits of the
4711 // GOTPLT entry for this.
4713 // PLT0 will be slightly different in ELF32 due to different got entry
4715 memcpy(pov
, this->first_plt_entry
, this->first_plt_entry_size
);
4716 Address gotplt_2nd_ent
= got_address
+ (size
/ 8) * 2;
4718 // Fill in the top 21 bits for this: ADRP x16, PLT_GOT + 8 * 2.
4719 // ADRP: (PG(S+A)-PG(P)) >> 12) & 0x1fffff.
4720 // FIXME: This only works for 64bit
4721 AArch64_relocate_functions
<size
, big_endian
>::adrp(pov
+ 4,
4722 gotplt_2nd_ent
, plt_address
+ 4);
4724 // Fill in R_AARCH64_LDST8_LO12
4725 elfcpp::Swap
<32, big_endian
>::writeval(
4727 ((this->first_plt_entry
[2] & 0xffc003ff)
4728 | ((gotplt_2nd_ent
& 0xff8) << 7)));
4730 // Fill in R_AARCH64_ADD_ABS_LO12
4731 elfcpp::Swap
<32, big_endian
>::writeval(
4733 ((this->first_plt_entry
[3] & 0xffc003ff)
4734 | ((gotplt_2nd_ent
& 0xfff) << 10)));
4738 // Subsequent entries in the PLT for an executable.
4739 // FIXME: This only works for 64bit
4741 template<int size
, bool big_endian
>
4743 Output_data_plt_aarch64_standard
<size
, big_endian
>::do_fill_plt_entry(
4745 Address got_address
,
4746 Address plt_address
,
4747 unsigned int got_offset
,
4748 unsigned int plt_offset
)
4750 memcpy(pov
, this->plt_entry
, this->plt_entry_size
);
4752 Address gotplt_entry_address
= got_address
+ got_offset
;
4753 Address plt_entry_address
= plt_address
+ plt_offset
;
4755 // Fill in R_AARCH64_PCREL_ADR_HI21
4756 AArch64_relocate_functions
<size
, big_endian
>::adrp(
4758 gotplt_entry_address
,
4761 // Fill in R_AARCH64_LDST64_ABS_LO12
4762 elfcpp::Swap
<32, big_endian
>::writeval(
4764 ((this->plt_entry
[1] & 0xffc003ff)
4765 | ((gotplt_entry_address
& 0xff8) << 7)));
4767 // Fill in R_AARCH64_ADD_ABS_LO12
4768 elfcpp::Swap
<32, big_endian
>::writeval(
4770 ((this->plt_entry
[2] & 0xffc003ff)
4771 | ((gotplt_entry_address
& 0xfff) <<10)));
4778 Output_data_plt_aarch64_standard
<32, false>::
4779 tlsdesc_plt_entry
[plt_tlsdesc_entry_size
/ 4] =
4781 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4782 0x90000002, /* adrp x2, 0 */
4783 0x90000003, /* adrp x3, 0 */
4784 0xb9400042, /* ldr w2, [w2, #0] */
4785 0x11000063, /* add w3, w3, 0 */
4786 0xd61f0040, /* br x2 */
4787 0xd503201f, /* nop */
4788 0xd503201f, /* nop */
4793 Output_data_plt_aarch64_standard
<32, true>::
4794 tlsdesc_plt_entry
[plt_tlsdesc_entry_size
/ 4] =
4796 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4797 0x90000002, /* adrp x2, 0 */
4798 0x90000003, /* adrp x3, 0 */
4799 0xb9400042, /* ldr w2, [w2, #0] */
4800 0x11000063, /* add w3, w3, 0 */
4801 0xd61f0040, /* br x2 */
4802 0xd503201f, /* nop */
4803 0xd503201f, /* nop */
4808 Output_data_plt_aarch64_standard
<64, false>::
4809 tlsdesc_plt_entry
[plt_tlsdesc_entry_size
/ 4] =
4811 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4812 0x90000002, /* adrp x2, 0 */
4813 0x90000003, /* adrp x3, 0 */
4814 0xf9400042, /* ldr x2, [x2, #0] */
4815 0x91000063, /* add x3, x3, 0 */
4816 0xd61f0040, /* br x2 */
4817 0xd503201f, /* nop */
4818 0xd503201f, /* nop */
4823 Output_data_plt_aarch64_standard
<64, true>::
4824 tlsdesc_plt_entry
[plt_tlsdesc_entry_size
/ 4] =
4826 0xa9bf0fe2, /* stp x2, x3, [sp, #-16]! */
4827 0x90000002, /* adrp x2, 0 */
4828 0x90000003, /* adrp x3, 0 */
4829 0xf9400042, /* ldr x2, [x2, #0] */
4830 0x91000063, /* add x3, x3, 0 */
4831 0xd61f0040, /* br x2 */
4832 0xd503201f, /* nop */
4833 0xd503201f, /* nop */
4836 template<int size
, bool big_endian
>
4838 Output_data_plt_aarch64_standard
<size
, big_endian
>::do_fill_tlsdesc_entry(
4840 Address gotplt_address
,
4841 Address plt_address
,
4843 unsigned int tlsdesc_got_offset
,
4844 unsigned int plt_offset
)
4846 memcpy(pov
, tlsdesc_plt_entry
, plt_tlsdesc_entry_size
);
4848 // move DT_TLSDESC_GOT address into x2
4849 // move .got.plt address into x3
4850 Address tlsdesc_got_entry
= got_base
+ tlsdesc_got_offset
;
4851 Address plt_entry_address
= plt_address
+ plt_offset
;
4853 // R_AARCH64_ADR_PREL_PG_HI21
4854 AArch64_relocate_functions
<size
, big_endian
>::adrp(
4857 plt_entry_address
+ 4);
4859 // R_AARCH64_ADR_PREL_PG_HI21
4860 AArch64_relocate_functions
<size
, big_endian
>::adrp(
4863 plt_entry_address
+ 8);
4865 // R_AARCH64_LDST64_ABS_LO12
4866 elfcpp::Swap
<32, big_endian
>::writeval(
4868 ((this->tlsdesc_plt_entry
[3] & 0xffc003ff)
4869 | ((tlsdesc_got_entry
& 0xff8) << 7)));
4871 // R_AARCH64_ADD_ABS_LO12
4872 elfcpp::Swap
<32, big_endian
>::writeval(
4874 ((this->tlsdesc_plt_entry
[4] & 0xffc003ff)
4875 | ((gotplt_address
& 0xfff) << 10)));
4878 // Write out the PLT. This uses the hand-coded instructions above,
4879 // and adjusts them as needed. This is specified by the AMD64 ABI.
4881 template<int size
, bool big_endian
>
4883 Output_data_plt_aarch64
<size
, big_endian
>::do_write(Output_file
* of
)
4885 const off_t offset
= this->offset();
4886 const section_size_type oview_size
=
4887 convert_to_section_size_type(this->data_size());
4888 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4890 const off_t got_file_offset
= this->got_plt_
->offset();
4891 gold_assert(got_file_offset
+ this->got_plt_
->data_size()
4892 == this->got_irelative_
->offset());
4894 const section_size_type got_size
=
4895 convert_to_section_size_type(this->got_plt_
->data_size()
4896 + this->got_irelative_
->data_size());
4897 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
4900 unsigned char* pov
= oview
;
4902 // The base address of the .plt section.
4903 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
4904 // The base address of the PLT portion of the .got section.
4905 typename
elfcpp::Elf_types
<size
>::Elf_Addr gotplt_address
4906 = this->got_plt_
->address();
4908 this->fill_first_plt_entry(pov
, gotplt_address
, plt_address
);
4909 pov
+= this->first_plt_entry_offset();
4911 // The first three entries in .got.plt are reserved.
4912 unsigned char* got_pov
= got_view
;
4913 memset(got_pov
, 0, size
/ 8 * AARCH64_GOTPLT_RESERVE_COUNT
);
4914 got_pov
+= (size
/ 8) * AARCH64_GOTPLT_RESERVE_COUNT
;
4916 unsigned int plt_offset
= this->first_plt_entry_offset();
4917 unsigned int got_offset
= (size
/ 8) * AARCH64_GOTPLT_RESERVE_COUNT
;
4918 const unsigned int count
= this->count_
+ this->irelative_count_
;
4919 for (unsigned int plt_index
= 0;
4922 pov
+= this->get_plt_entry_size(),
4923 got_pov
+= size
/ 8,
4924 plt_offset
+= this->get_plt_entry_size(),
4925 got_offset
+= size
/ 8)
4927 // Set and adjust the PLT entry itself.
4928 this->fill_plt_entry(pov
, gotplt_address
, plt_address
,
4929 got_offset
, plt_offset
);
4931 // Set the entry in the GOT, which points to plt0.
4932 elfcpp::Swap
<size
, big_endian
>::writeval(got_pov
, plt_address
);
4935 if (this->has_tlsdesc_entry())
4937 // Set and adjust the reserved TLSDESC PLT entry.
4938 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
4939 // The base address of the .base section.
4940 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
=
4941 this->got_
->address();
4942 this->fill_tlsdesc_entry(pov
, gotplt_address
, plt_address
, got_base
,
4943 tlsdesc_got_offset
, plt_offset
);
4944 pov
+= this->get_plt_tlsdesc_entry_size();
4947 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
4948 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
4950 of
->write_output_view(offset
, oview_size
, oview
);
4951 of
->write_output_view(got_file_offset
, got_size
, got_view
);
4954 // Telling how to update the immediate field of an instruction.
4955 struct AArch64_howto
4957 // The immediate field mask.
4958 elfcpp::Elf_Xword dst_mask
;
4960 // The offset to apply relocation immediate
4963 // The second part offset, if the immediate field has two parts.
4964 // -1 if the immediate field has only one part.
4968 static const AArch64_howto aarch64_howto
[AArch64_reloc_property::INST_NUM
] =
4970 {0, -1, -1}, // DATA
4971 {0x1fffe0, 5, -1}, // MOVW [20:5]-imm16
4972 {0xffffe0, 5, -1}, // LD [23:5]-imm19
4973 {0x60ffffe0, 29, 5}, // ADR [30:29]-immlo [23:5]-immhi
4974 {0x60ffffe0, 29, 5}, // ADRP [30:29]-immlo [23:5]-immhi
4975 {0x3ffc00, 10, -1}, // ADD [21:10]-imm12
4976 {0x3ffc00, 10, -1}, // LDST [21:10]-imm12
4977 {0x7ffe0, 5, -1}, // TBZNZ [18:5]-imm14
4978 {0xffffe0, 5, -1}, // CONDB [23:5]-imm19
4979 {0x3ffffff, 0, -1}, // B [25:0]-imm26
4980 {0x3ffffff, 0, -1}, // CALL [25:0]-imm26
4983 // AArch64 relocate function class
4985 template<int size
, bool big_endian
>
4986 class AArch64_relocate_functions
4991 STATUS_OKAY
, // No error during relocation.
4992 STATUS_OVERFLOW
, // Relocation overflow.
4993 STATUS_BAD_RELOC
, // Relocation cannot be applied.
4996 typedef AArch64_relocate_functions
<size
, big_endian
> This
;
4997 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4998 typedef Relocate_info
<size
, big_endian
> The_relocate_info
;
4999 typedef AArch64_relobj
<size
, big_endian
> The_aarch64_relobj
;
5000 typedef Reloc_stub
<size
, big_endian
> The_reloc_stub
;
5001 typedef Stub_table
<size
, big_endian
> The_stub_table
;
5002 typedef elfcpp::Rela
<size
, big_endian
> The_rela
;
5003 typedef typename
elfcpp::Swap
<size
, big_endian
>::Valtype AArch64_valtype
;
5005 // Return the page address of the address.
5006 // Page(address) = address & ~0xFFF
5008 static inline AArch64_valtype
5009 Page(Address address
)
5011 return (address
& (~static_cast<Address
>(0xFFF)));
5015 // Update instruction (pointed by view) with selected bits (immed).
5016 // val = (val & ~dst_mask) | (immed << doffset)
5018 template<int valsize
>
5020 update_view(unsigned char* view
,
5021 AArch64_valtype immed
,
5022 elfcpp::Elf_Xword doffset
,
5023 elfcpp::Elf_Xword dst_mask
)
5025 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
5026 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
5027 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
5029 // Clear immediate fields.
5031 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
,
5032 static_cast<Valtype
>(val
| (immed
<< doffset
)));
5035 // Update two parts of an instruction (pointed by view) with selected
5036 // bits (immed1 and immed2).
5037 // val = (val & ~dst_mask) | (immed1 << doffset1) | (immed2 << doffset2)
5039 template<int valsize
>
5041 update_view_two_parts(
5042 unsigned char* view
,
5043 AArch64_valtype immed1
,
5044 AArch64_valtype immed2
,
5045 elfcpp::Elf_Xword doffset1
,
5046 elfcpp::Elf_Xword doffset2
,
5047 elfcpp::Elf_Xword dst_mask
)
5049 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
5050 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
5051 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
5053 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
,
5054 static_cast<Valtype
>(val
| (immed1
<< doffset1
) |
5055 (immed2
<< doffset2
)));
5058 // Update adr or adrp instruction with immed.
5059 // In adr and adrp: [30:29] immlo [23:5] immhi
5062 update_adr(unsigned char* view
, AArch64_valtype immed
)
5064 elfcpp::Elf_Xword dst_mask
= (0x3 << 29) | (0x7ffff << 5);
5065 This::template update_view_two_parts
<32>(
5068 (immed
& 0x1ffffc) >> 2,
5074 // Update movz/movn instruction with bits immed.
5075 // Set instruction to movz if is_movz is true, otherwise set instruction
5079 update_movnz(unsigned char* view
,
5080 AArch64_valtype immed
,
5083 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
5084 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
5085 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5087 const elfcpp::Elf_Xword doffset
=
5088 aarch64_howto
[AArch64_reloc_property::INST_MOVW
].doffset
;
5089 const elfcpp::Elf_Xword dst_mask
=
5090 aarch64_howto
[AArch64_reloc_property::INST_MOVW
].dst_mask
;
5092 // Clear immediate fields and opc code.
5093 val
&= ~(dst_mask
| (0x3 << 29));
5095 // Set instruction to movz or movn.
5096 // movz: [30:29] is 10 movn: [30:29] is 00
5100 elfcpp::Swap
<32, big_endian
>::writeval(wv
,
5101 static_cast<Valtype
>(val
| (immed
<< doffset
)));
5104 // Update selected bits in text.
5106 template<int valsize
>
5107 static inline typename
This::Status
5108 reloc_common(unsigned char* view
, Address x
,
5109 const AArch64_reloc_property
* reloc_property
)
5111 // Select bits from X.
5112 Address immed
= reloc_property
->select_x_value(x
);
5115 const AArch64_reloc_property::Reloc_inst inst
=
5116 reloc_property
->reloc_inst();
5117 // If it is a data relocation or instruction has 2 parts of immediate
5118 // fields, you should not call pcrela_general.
5119 gold_assert(aarch64_howto
[inst
].doffset2
== -1 &&
5120 aarch64_howto
[inst
].doffset
!= -1);
5121 This::template update_view
<valsize
>(view
, immed
,
5122 aarch64_howto
[inst
].doffset
,
5123 aarch64_howto
[inst
].dst_mask
);
5125 // Do check overflow or alignment if needed.
5126 return (reloc_property
->checkup_x_value(x
)
5128 : This::STATUS_OVERFLOW
);
5133 // Construct a B insn. Note, although we group it here with other relocation
5134 // operation, there is actually no 'relocation' involved here.
5136 construct_b(unsigned char* view
, unsigned int branch_offset
)
5138 update_view_two_parts
<32>(view
, 0x05, (branch_offset
>> 2),
5142 // Do a simple rela relocation at unaligned addresses.
5144 template<int valsize
>
5145 static inline typename
This::Status
5146 rela_ua(unsigned char* view
,
5147 const Sized_relobj_file
<size
, big_endian
>* object
,
5148 const Symbol_value
<size
>* psymval
,
5149 AArch64_valtype addend
,
5150 const AArch64_reloc_property
* reloc_property
)
5152 typedef typename
elfcpp::Swap_unaligned
<valsize
, big_endian
>::Valtype
5154 typename
elfcpp::Elf_types
<size
>::Elf_Addr x
=
5155 psymval
->value(object
, addend
);
5156 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
,
5157 static_cast<Valtype
>(x
));
5158 return (reloc_property
->checkup_x_value(x
)
5160 : This::STATUS_OVERFLOW
);
5163 // Do a simple pc-relative relocation at unaligned addresses.
5165 template<int valsize
>
5166 static inline typename
This::Status
5167 pcrela_ua(unsigned char* view
,
5168 const Sized_relobj_file
<size
, big_endian
>* object
,
5169 const Symbol_value
<size
>* psymval
,
5170 AArch64_valtype addend
,
5172 const AArch64_reloc_property
* reloc_property
)
5174 typedef typename
elfcpp::Swap_unaligned
<valsize
, big_endian
>::Valtype
5176 Address x
= psymval
->value(object
, addend
) - address
;
5177 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
,
5178 static_cast<Valtype
>(x
));
5179 return (reloc_property
->checkup_x_value(x
)
5181 : This::STATUS_OVERFLOW
);
5184 // Do a simple rela relocation at aligned addresses.
5186 template<int valsize
>
5187 static inline typename
This::Status
5189 unsigned char* view
,
5190 const Sized_relobj_file
<size
, big_endian
>* object
,
5191 const Symbol_value
<size
>* psymval
,
5192 AArch64_valtype addend
,
5193 const AArch64_reloc_property
* reloc_property
)
5195 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
5196 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
5197 Address x
= psymval
->value(object
, addend
);
5198 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
,static_cast<Valtype
>(x
));
5199 return (reloc_property
->checkup_x_value(x
)
5201 : This::STATUS_OVERFLOW
);
5204 // Do relocate. Update selected bits in text.
5205 // new_val = (val & ~dst_mask) | (immed << doffset)
5207 template<int valsize
>
5208 static inline typename
This::Status
5209 rela_general(unsigned char* view
,
5210 const Sized_relobj_file
<size
, big_endian
>* object
,
5211 const Symbol_value
<size
>* psymval
,
5212 AArch64_valtype addend
,
5213 const AArch64_reloc_property
* reloc_property
)
5215 // Calculate relocation.
5216 Address x
= psymval
->value(object
, addend
);
5217 return This::template reloc_common
<valsize
>(view
, x
, reloc_property
);
5220 // Do relocate. Update selected bits in text.
5221 // new val = (val & ~dst_mask) | (immed << doffset)
5223 template<int valsize
>
5224 static inline typename
This::Status
5226 unsigned char* view
,
5228 AArch64_valtype addend
,
5229 const AArch64_reloc_property
* reloc_property
)
5231 // Calculate relocation.
5232 Address x
= s
+ addend
;
5233 return This::template reloc_common
<valsize
>(view
, x
, reloc_property
);
5236 // Do address relative relocate. Update selected bits in text.
5237 // new val = (val & ~dst_mask) | (immed << doffset)
5239 template<int valsize
>
5240 static inline typename
This::Status
5242 unsigned char* view
,
5243 const Sized_relobj_file
<size
, big_endian
>* object
,
5244 const Symbol_value
<size
>* psymval
,
5245 AArch64_valtype addend
,
5247 const AArch64_reloc_property
* reloc_property
)
5249 // Calculate relocation.
5250 Address x
= psymval
->value(object
, addend
) - address
;
5251 return This::template reloc_common
<valsize
>(view
, x
, reloc_property
);
5255 // Calculate (S + A) - address, update adr instruction.
5257 static inline typename
This::Status
5258 adr(unsigned char* view
,
5259 const Sized_relobj_file
<size
, big_endian
>* object
,
5260 const Symbol_value
<size
>* psymval
,
5263 const AArch64_reloc_property
* /* reloc_property */)
5265 AArch64_valtype x
= psymval
->value(object
, addend
) - address
;
5266 // Pick bits [20:0] of X.
5267 AArch64_valtype immed
= x
& 0x1fffff;
5268 update_adr(view
, immed
);
5269 // Check -2^20 <= X < 2^20
5270 return (size
== 64 && Bits
<21>::has_overflow((x
))
5271 ? This::STATUS_OVERFLOW
5272 : This::STATUS_OKAY
);
5275 // Calculate PG(S+A) - PG(address), update adrp instruction.
5276 // R_AARCH64_ADR_PREL_PG_HI21
5278 static inline typename
This::Status
5280 unsigned char* view
,
5284 AArch64_valtype x
= This::Page(sa
) - This::Page(address
);
5285 // Pick [32:12] of X.
5286 AArch64_valtype immed
= (x
>> 12) & 0x1fffff;
5287 update_adr(view
, immed
);
5288 // Check -2^32 <= X < 2^32
5289 return (size
== 64 && Bits
<33>::has_overflow((x
))
5290 ? This::STATUS_OVERFLOW
5291 : This::STATUS_OKAY
);
5294 // Calculate PG(S+A) - PG(address), update adrp instruction.
5295 // R_AARCH64_ADR_PREL_PG_HI21
5297 static inline typename
This::Status
5298 adrp(unsigned char* view
,
5299 const Sized_relobj_file
<size
, big_endian
>* object
,
5300 const Symbol_value
<size
>* psymval
,
5303 const AArch64_reloc_property
* reloc_property
)
5305 Address sa
= psymval
->value(object
, addend
);
5306 AArch64_valtype x
= This::Page(sa
) - This::Page(address
);
5307 // Pick [32:12] of X.
5308 AArch64_valtype immed
= (x
>> 12) & 0x1fffff;
5309 update_adr(view
, immed
);
5310 return (reloc_property
->checkup_x_value(x
)
5312 : This::STATUS_OVERFLOW
);
5315 // Update mov[n/z] instruction. Check overflow if needed.
5316 // If X >=0, set the instruction to movz and its immediate value to the
5318 // If X < 0, set the instruction to movn and its immediate value to
5319 // NOT (selected bits of).
5321 static inline typename
This::Status
5322 movnz(unsigned char* view
,
5324 const AArch64_reloc_property
* reloc_property
)
5326 // Select bits from X.
5329 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword SignedW
;
5330 if (static_cast<SignedW
>(x
) >= 0)
5332 immed
= reloc_property
->select_x_value(x
);
5337 immed
= reloc_property
->select_x_value(~x
);;
5341 // Update movnz instruction.
5342 update_movnz(view
, immed
, is_movz
);
5344 // Do check overflow or alignment if needed.
5345 return (reloc_property
->checkup_x_value(x
)
5347 : This::STATUS_OVERFLOW
);
5351 maybe_apply_stub(unsigned int,
5352 const The_relocate_info
*,
5356 const Sized_symbol
<size
>*,
5357 const Symbol_value
<size
>*,
5358 const Sized_relobj_file
<size
, big_endian
>*,
5361 }; // End of AArch64_relocate_functions
5364 // For a certain relocation type (usually jump/branch), test to see if the
5365 // destination needs a stub to fulfil. If so, re-route the destination of the
5366 // original instruction to the stub, note, at this time, the stub has already
5369 template<int size
, bool big_endian
>
5371 AArch64_relocate_functions
<size
, big_endian
>::
5372 maybe_apply_stub(unsigned int r_type
,
5373 const The_relocate_info
* relinfo
,
5374 const The_rela
& rela
,
5375 unsigned char* view
,
5377 const Sized_symbol
<size
>* gsym
,
5378 const Symbol_value
<size
>* psymval
,
5379 const Sized_relobj_file
<size
, big_endian
>* object
,
5380 section_size_type current_group_size
)
5382 if (parameters
->options().relocatable())
5385 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
= rela
.get_r_addend();
5386 Address branch_target
= psymval
->value(object
, 0) + addend
;
5388 The_reloc_stub::stub_type_for_reloc(r_type
, address
, branch_target
);
5389 if (stub_type
== ST_NONE
)
5392 const The_aarch64_relobj
* aarch64_relobj
=
5393 static_cast<const The_aarch64_relobj
*>(object
);
5394 The_stub_table
* stub_table
= aarch64_relobj
->stub_table(relinfo
->data_shndx
);
5395 gold_assert(stub_table
!= NULL
);
5397 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5398 typename
The_reloc_stub::Key
stub_key(stub_type
, gsym
, object
, r_sym
, addend
);
5399 The_reloc_stub
* stub
= stub_table
->find_reloc_stub(stub_key
);
5400 gold_assert(stub
!= NULL
);
5402 Address new_branch_target
= stub_table
->address() + stub
->offset();
5403 typename
elfcpp::Swap
<size
, big_endian
>::Valtype branch_offset
=
5404 new_branch_target
- address
;
5405 const AArch64_reloc_property
* arp
=
5406 aarch64_reloc_property_table
->get_reloc_property(r_type
);
5407 gold_assert(arp
!= NULL
);
5408 typename
This::Status status
= This::template
5409 rela_general
<32>(view
, branch_offset
, 0, arp
);
5410 if (status
!= This::STATUS_OKAY
)
5411 gold_error(_("Stub is too far away, try a smaller value "
5412 "for '--stub-group-size'. The current value is 0x%lx."),
5413 static_cast<unsigned long>(current_group_size
));
5418 // Group input sections for stub generation.
5420 // We group input sections in an output section so that the total size,
5421 // including any padding space due to alignment is smaller than GROUP_SIZE
5422 // unless the only input section in group is bigger than GROUP_SIZE already.
5423 // Then an ARM stub table is created to follow the last input section
5424 // in group. For each group an ARM stub table is created an is placed
5425 // after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
5426 // extend the group after the stub table.
5428 template<int size
, bool big_endian
>
5430 Target_aarch64
<size
, big_endian
>::group_sections(
5432 section_size_type group_size
,
5433 bool stubs_always_after_branch
,
5436 // Group input sections and insert stub table
5437 Layout::Section_list section_list
;
5438 layout
->get_executable_sections(§ion_list
);
5439 for (Layout::Section_list::const_iterator p
= section_list
.begin();
5440 p
!= section_list
.end();
5443 AArch64_output_section
<size
, big_endian
>* output_section
=
5444 static_cast<AArch64_output_section
<size
, big_endian
>*>(*p
);
5445 output_section
->group_sections(group_size
, stubs_always_after_branch
,
5451 // Find the AArch64_input_section object corresponding to the SHNDX-th input
5452 // section of RELOBJ.
5454 template<int size
, bool big_endian
>
5455 AArch64_input_section
<size
, big_endian
>*
5456 Target_aarch64
<size
, big_endian
>::find_aarch64_input_section(
5457 Relobj
* relobj
, unsigned int shndx
) const
5459 Section_id
sid(relobj
, shndx
);
5460 typename
AArch64_input_section_map::const_iterator p
=
5461 this->aarch64_input_section_map_
.find(sid
);
5462 return (p
!= this->aarch64_input_section_map_
.end()) ? p
->second
: NULL
;
5466 // Make a new AArch64_input_section object.
5468 template<int size
, bool big_endian
>
5469 AArch64_input_section
<size
, big_endian
>*
5470 Target_aarch64
<size
, big_endian
>::new_aarch64_input_section(
5471 Relobj
* relobj
, unsigned int shndx
)
5473 Section_id
sid(relobj
, shndx
);
5475 AArch64_input_section
<size
, big_endian
>* input_section
=
5476 new AArch64_input_section
<size
, big_endian
>(relobj
, shndx
);
5477 input_section
->init();
5479 // Register new AArch64_input_section in map for look-up.
5480 std::pair
<typename
AArch64_input_section_map::iterator
,bool> ins
=
5481 this->aarch64_input_section_map_
.insert(
5482 std::make_pair(sid
, input_section
));
5484 // Make sure that it we have not created another AArch64_input_section
5485 // for this input section already.
5486 gold_assert(ins
.second
);
5488 return input_section
;
5492 // Relaxation hook. This is where we do stub generation.
5494 template<int size
, bool big_endian
>
5496 Target_aarch64
<size
, big_endian
>::do_relax(
5498 const Input_objects
* input_objects
,
5499 Symbol_table
* symtab
,
5503 gold_assert(!parameters
->options().relocatable());
5506 // We don't handle negative stub_group_size right now.
5507 this->stub_group_size_
= abs(parameters
->options().stub_group_size());
5508 if (this->stub_group_size_
== 1)
5510 // Leave room for 4096 4-byte stub entries. If we exceed that, then we
5511 // will fail to link. The user will have to relink with an explicit
5512 // group size option.
5513 this->stub_group_size_
= The_reloc_stub::MAX_BRANCH_OFFSET
-
5516 group_sections(layout
, this->stub_group_size_
, true, task
);
5520 // If this is not the first pass, addresses and file offsets have
5521 // been reset at this point, set them here.
5522 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
5523 sp
!= this->stub_tables_
.end(); ++sp
)
5525 The_stub_table
* stt
= *sp
;
5526 The_aarch64_input_section
* owner
= stt
->owner();
5527 off_t off
= align_address(owner
->original_size(),
5529 stt
->set_address_and_file_offset(owner
->address() + off
,
5530 owner
->offset() + off
);
5534 // Scan relocs for relocation stubs
5535 for (Input_objects::Relobj_iterator op
= input_objects
->relobj_begin();
5536 op
!= input_objects
->relobj_end();
5539 The_aarch64_relobj
* aarch64_relobj
=
5540 static_cast<The_aarch64_relobj
*>(*op
);
5541 // Lock the object so we can read from it. This is only called
5542 // single-threaded from Layout::finalize, so it is OK to lock.
5543 Task_lock_obj
<Object
> tl(task
, aarch64_relobj
);
5544 aarch64_relobj
->scan_sections_for_stubs(this, symtab
, layout
);
5547 bool any_stub_table_changed
= false;
5548 for (Stub_table_iterator siter
= this->stub_tables_
.begin();
5549 siter
!= this->stub_tables_
.end() && !any_stub_table_changed
; ++siter
)
5551 The_stub_table
* stub_table
= *siter
;
5552 if (stub_table
->update_data_size_changed_p())
5554 The_aarch64_input_section
* owner
= stub_table
->owner();
5555 uint64_t address
= owner
->address();
5556 off_t offset
= owner
->offset();
5557 owner
->reset_address_and_file_offset();
5558 owner
->set_address_and_file_offset(address
, offset
);
5560 any_stub_table_changed
= true;
5564 // Do not continue relaxation.
5565 bool continue_relaxation
= any_stub_table_changed
;
5566 if (!continue_relaxation
)
5567 for (Stub_table_iterator sp
= this->stub_tables_
.begin();
5568 (sp
!= this->stub_tables_
.end());
5570 (*sp
)->finalize_stubs();
5572 return continue_relaxation
;
5576 // Make a new Stub_table.
5578 template<int size
, bool big_endian
>
5579 Stub_table
<size
, big_endian
>*
5580 Target_aarch64
<size
, big_endian
>::new_stub_table(
5581 AArch64_input_section
<size
, big_endian
>* owner
)
5583 Stub_table
<size
, big_endian
>* stub_table
=
5584 new Stub_table
<size
, big_endian
>(owner
);
5585 stub_table
->set_address(align_address(
5586 owner
->address() + owner
->data_size(), 8));
5587 stub_table
->set_file_offset(owner
->offset() + owner
->data_size());
5588 stub_table
->finalize_data_size();
5590 this->stub_tables_
.push_back(stub_table
);
5596 template<int size
, bool big_endian
>
5598 Target_aarch64
<size
, big_endian
>::do_reloc_addend(
5599 void* arg
, unsigned int r_type
, uint64_t) const
5601 gold_assert(r_type
== elfcpp::R_AARCH64_TLSDESC
);
5602 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
5603 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
5604 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
5605 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
5606 gold_assert(psymval
->is_tls_symbol());
5607 // The value of a TLS symbol is the offset in the TLS segment.
5608 return psymval
->value(ti
.object
, 0);
5611 // Return the number of entries in the PLT.
5613 template<int size
, bool big_endian
>
5615 Target_aarch64
<size
, big_endian
>::plt_entry_count() const
5617 if (this->plt_
== NULL
)
5619 return this->plt_
->entry_count();
5622 // Return the offset of the first non-reserved PLT entry.
5624 template<int size
, bool big_endian
>
5626 Target_aarch64
<size
, big_endian
>::first_plt_entry_offset() const
5628 return this->plt_
->first_plt_entry_offset();
5631 // Return the size of each PLT entry.
5633 template<int size
, bool big_endian
>
5635 Target_aarch64
<size
, big_endian
>::plt_entry_size() const
5637 return this->plt_
->get_plt_entry_size();
5640 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
5642 template<int size
, bool big_endian
>
5644 Target_aarch64
<size
, big_endian
>::define_tls_base_symbol(
5645 Symbol_table
* symtab
, Layout
* layout
)
5647 if (this->tls_base_symbol_defined_
)
5650 Output_segment
* tls_segment
= layout
->tls_segment();
5651 if (tls_segment
!= NULL
)
5653 // _TLS_MODULE_BASE_ always points to the beginning of tls segment.
5654 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
5655 Symbol_table::PREDEFINED
,
5659 elfcpp::STV_HIDDEN
, 0,
5660 Symbol::SEGMENT_START
,
5663 this->tls_base_symbol_defined_
= true;
5666 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
5668 template<int size
, bool big_endian
>
5670 Target_aarch64
<size
, big_endian
>::reserve_tlsdesc_entries(
5671 Symbol_table
* symtab
, Layout
* layout
)
5673 if (this->plt_
== NULL
)
5674 this->make_plt_section(symtab
, layout
);
5676 if (!this->plt_
->has_tlsdesc_entry())
5678 // Allocate the TLSDESC_GOT entry.
5679 Output_data_got_aarch64
<size
, big_endian
>* got
=
5680 this->got_section(symtab
, layout
);
5681 unsigned int got_offset
= got
->add_constant(0);
5683 // Allocate the TLSDESC_PLT entry.
5684 this->plt_
->reserve_tlsdesc_entry(got_offset
);
5688 // Create a GOT entry for the TLS module index.
5690 template<int size
, bool big_endian
>
5692 Target_aarch64
<size
, big_endian
>::got_mod_index_entry(
5693 Symbol_table
* symtab
, Layout
* layout
,
5694 Sized_relobj_file
<size
, big_endian
>* object
)
5696 if (this->got_mod_index_offset_
== -1U)
5698 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
5699 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
5700 Output_data_got_aarch64
<size
, big_endian
>* got
=
5701 this->got_section(symtab
, layout
);
5702 unsigned int got_offset
= got
->add_constant(0);
5703 rela_dyn
->add_local(object
, 0, elfcpp::R_AARCH64_TLS_DTPMOD64
, got
,
5705 got
->add_constant(0);
5706 this->got_mod_index_offset_
= got_offset
;
5708 return this->got_mod_index_offset_
;
5711 // Optimize the TLS relocation type based on what we know about the
5712 // symbol. IS_FINAL is true if the final address of this symbol is
5713 // known at link time.
5715 template<int size
, bool big_endian
>
5716 tls::Tls_optimization
5717 Target_aarch64
<size
, big_endian
>::optimize_tls_reloc(bool is_final
,
5720 // If we are generating a shared library, then we can't do anything
5722 if (parameters
->options().shared())
5723 return tls::TLSOPT_NONE
;
5727 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21
:
5728 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
:
5729 case elfcpp::R_AARCH64_TLSDESC_LD_PREL19
:
5730 case elfcpp::R_AARCH64_TLSDESC_ADR_PREL21
:
5731 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
5732 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
5733 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
5734 case elfcpp::R_AARCH64_TLSDESC_OFF_G1
:
5735 case elfcpp::R_AARCH64_TLSDESC_OFF_G0_NC
:
5736 case elfcpp::R_AARCH64_TLSDESC_LDR
:
5737 case elfcpp::R_AARCH64_TLSDESC_ADD
:
5738 case elfcpp::R_AARCH64_TLSDESC_CALL
:
5739 // These are General-Dynamic which permits fully general TLS
5740 // access. Since we know that we are generating an executable,
5741 // we can convert this to Initial-Exec. If we also know that
5742 // this is a local symbol, we can further switch to Local-Exec.
5744 return tls::TLSOPT_TO_LE
;
5745 return tls::TLSOPT_TO_IE
;
5747 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21
:
5748 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
:
5749 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1
:
5750 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
5751 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12
:
5752 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
:
5753 // These are Local-Dynamic, which refer to local symbols in the
5754 // dynamic TLS block. Since we know that we generating an
5755 // executable, we can switch to Local-Exec.
5756 return tls::TLSOPT_TO_LE
;
5758 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G1
:
5759 case elfcpp::R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC
:
5760 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
5761 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
5762 case elfcpp::R_AARCH64_TLSIE_LD_GOTTPREL_PREL19
:
5763 // These are Initial-Exec relocs which get the thread offset
5764 // from the GOT. If we know that we are linking against the
5765 // local symbol, we can switch to Local-Exec, which links the
5766 // thread offset into the instruction.
5768 return tls::TLSOPT_TO_LE
;
5769 return tls::TLSOPT_NONE
;
5771 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2
:
5772 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1
:
5773 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
:
5774 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0
:
5775 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
:
5776 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
5777 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
5778 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
5779 // When we already have Local-Exec, there is nothing further we
5781 return tls::TLSOPT_NONE
;
5788 // Returns true if this relocation type could be that of a function pointer.
5790 template<int size
, bool big_endian
>
5792 Target_aarch64
<size
, big_endian
>::Scan::possible_function_pointer_reloc(
5793 unsigned int r_type
)
5797 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21
:
5798 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC
:
5799 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC
:
5800 case elfcpp::R_AARCH64_ADR_GOT_PAGE
:
5801 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC
:
5809 // For safe ICF, scan a relocation for a local symbol to check if it
5810 // corresponds to a function pointer being taken. In that case mark
5811 // the function whose pointer was taken as not foldable.
5813 template<int size
, bool big_endian
>
5815 Target_aarch64
<size
, big_endian
>::Scan::local_reloc_may_be_function_pointer(
5818 Target_aarch64
<size
, big_endian
>* ,
5819 Sized_relobj_file
<size
, big_endian
>* ,
5822 const elfcpp::Rela
<size
, big_endian
>& ,
5823 unsigned int r_type
,
5824 const elfcpp::Sym
<size
, big_endian
>&)
5826 // When building a shared library, do not fold any local symbols.
5827 return (parameters
->options().shared()
5828 || possible_function_pointer_reloc(r_type
));
5831 // For safe ICF, scan a relocation for a global symbol to check if it
5832 // corresponds to a function pointer being taken. In that case mark
5833 // the function whose pointer was taken as not foldable.
5835 template<int size
, bool big_endian
>
5837 Target_aarch64
<size
, big_endian
>::Scan::global_reloc_may_be_function_pointer(
5840 Target_aarch64
<size
, big_endian
>* ,
5841 Sized_relobj_file
<size
, big_endian
>* ,
5844 const elfcpp::Rela
<size
, big_endian
>& ,
5845 unsigned int r_type
,
5848 // When building a shared library, do not fold symbols whose visibility
5849 // is hidden, internal or protected.
5850 return ((parameters
->options().shared()
5851 && (gsym
->visibility() == elfcpp::STV_INTERNAL
5852 || gsym
->visibility() == elfcpp::STV_PROTECTED
5853 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
5854 || possible_function_pointer_reloc(r_type
));
5857 // Report an unsupported relocation against a local symbol.
5859 template<int size
, bool big_endian
>
5861 Target_aarch64
<size
, big_endian
>::Scan::unsupported_reloc_local(
5862 Sized_relobj_file
<size
, big_endian
>* object
,
5863 unsigned int r_type
)
5865 gold_error(_("%s: unsupported reloc %u against local symbol"),
5866 object
->name().c_str(), r_type
);
5869 // We are about to emit a dynamic relocation of type R_TYPE. If the
5870 // dynamic linker does not support it, issue an error.
5872 template<int size
, bool big_endian
>
5874 Target_aarch64
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
5875 unsigned int r_type
)
5877 gold_assert(r_type
!= elfcpp::R_AARCH64_NONE
);
5881 // These are the relocation types supported by glibc for AARCH64.
5882 case elfcpp::R_AARCH64_NONE
:
5883 case elfcpp::R_AARCH64_COPY
:
5884 case elfcpp::R_AARCH64_GLOB_DAT
:
5885 case elfcpp::R_AARCH64_JUMP_SLOT
:
5886 case elfcpp::R_AARCH64_RELATIVE
:
5887 case elfcpp::R_AARCH64_TLS_DTPREL64
:
5888 case elfcpp::R_AARCH64_TLS_DTPMOD64
:
5889 case elfcpp::R_AARCH64_TLS_TPREL64
:
5890 case elfcpp::R_AARCH64_TLSDESC
:
5891 case elfcpp::R_AARCH64_IRELATIVE
:
5892 case elfcpp::R_AARCH64_ABS32
:
5893 case elfcpp::R_AARCH64_ABS64
:
5900 // This prevents us from issuing more than one error per reloc
5901 // section. But we can still wind up issuing more than one
5902 // error per object file.
5903 if (this->issued_non_pic_error_
)
5905 gold_assert(parameters
->options().output_is_position_independent());
5906 object
->error(_("requires unsupported dynamic reloc; "
5907 "recompile with -fPIC"));
5908 this->issued_non_pic_error_
= true;
5912 // Return whether we need to make a PLT entry for a relocation of the
5913 // given type against a STT_GNU_IFUNC symbol.
5915 template<int size
, bool big_endian
>
5917 Target_aarch64
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
5918 Sized_relobj_file
<size
, big_endian
>* object
,
5919 unsigned int r_type
)
5921 const AArch64_reloc_property
* arp
=
5922 aarch64_reloc_property_table
->get_reloc_property(r_type
);
5923 gold_assert(arp
!= NULL
);
5925 int flags
= arp
->reference_flags();
5926 if (flags
& Symbol::TLS_REF
)
5928 gold_error(_("%s: unsupported TLS reloc %s for IFUNC symbol"),
5929 object
->name().c_str(), arp
->name().c_str());
5935 // Scan a relocation for a local symbol.
5937 template<int size
, bool big_endian
>
5939 Target_aarch64
<size
, big_endian
>::Scan::local(
5940 Symbol_table
* symtab
,
5942 Target_aarch64
<size
, big_endian
>* target
,
5943 Sized_relobj_file
<size
, big_endian
>* object
,
5944 unsigned int data_shndx
,
5945 Output_section
* output_section
,
5946 const elfcpp::Rela
<size
, big_endian
>& rela
,
5947 unsigned int r_type
,
5948 const elfcpp::Sym
<size
, big_endian
>& lsym
,
5954 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>
5956 Output_data_got_aarch64
<size
, big_endian
>* got
=
5957 target
->got_section(symtab
, layout
);
5958 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5960 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5961 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
5962 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
5963 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
5967 case elfcpp::R_AARCH64_ABS32
:
5968 case elfcpp::R_AARCH64_ABS16
:
5969 if (parameters
->options().output_is_position_independent())
5971 gold_error(_("%s: unsupported reloc %u in pos independent link."),
5972 object
->name().c_str(), r_type
);
5976 case elfcpp::R_AARCH64_ABS64
:
5977 // If building a shared library or pie, we need to mark this as a dynmic
5978 // reloction, so that the dynamic loader can relocate it.
5979 if (parameters
->options().output_is_position_independent())
5981 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5982 rela_dyn
->add_local_relative(object
, r_sym
,
5983 elfcpp::R_AARCH64_RELATIVE
,
5986 rela
.get_r_offset(),
5987 rela
.get_r_addend(),
5992 case elfcpp::R_AARCH64_PREL64
:
5993 case elfcpp::R_AARCH64_PREL32
:
5994 case elfcpp::R_AARCH64_PREL16
:
5997 case elfcpp::R_AARCH64_ADR_GOT_PAGE
:
5998 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC
:
5999 // This pair of relocations is used to access a specific GOT entry.
6001 bool is_new
= false;
6002 // This symbol requires a GOT entry.
6004 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
6006 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
6007 if (is_new
&& parameters
->options().output_is_position_independent())
6008 target
->rela_dyn_section(layout
)->
6009 add_local_relative(object
,
6011 elfcpp::R_AARCH64_RELATIVE
,
6013 object
->local_got_offset(r_sym
,
6020 case elfcpp::R_AARCH64_LD_PREL_LO19
: // 273
6021 case elfcpp::R_AARCH64_ADR_PREL_LO21
: // 274
6022 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21
: // 275
6023 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC
: // 276
6024 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC
: // 277
6025 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC
: // 278
6026 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC
: // 284
6027 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC
: // 285
6028 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC
: // 286
6029 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC
: // 299
6032 // Control flow, pc-relative. We don't need to do anything for a relative
6033 // addressing relocation against a local symbol if it does not reference
6035 case elfcpp::R_AARCH64_TSTBR14
:
6036 case elfcpp::R_AARCH64_CONDBR19
:
6037 case elfcpp::R_AARCH64_JUMP26
:
6038 case elfcpp::R_AARCH64_CALL26
:
6041 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
6042 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
6044 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6045 optimize_tls_reloc(!parameters
->options().shared(), r_type
);
6046 if (tlsopt
== tls::TLSOPT_TO_LE
)
6049 layout
->set_has_static_tls();
6050 // Create a GOT entry for the tp-relative offset.
6051 if (!parameters
->doing_static_link())
6053 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
6054 target
->rela_dyn_section(layout
),
6055 elfcpp::R_AARCH64_TLS_TPREL64
);
6057 else if (!object
->local_has_got_offset(r_sym
,
6058 GOT_TYPE_TLS_OFFSET
))
6060 got
->add_local(object
, r_sym
, GOT_TYPE_TLS_OFFSET
);
6061 unsigned int got_offset
=
6062 object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
);
6063 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
6064 gold_assert(addend
== 0);
6065 got
->add_static_reloc(got_offset
, elfcpp::R_AARCH64_TLS_TPREL64
,
6071 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21
:
6072 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
:
6074 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6075 optimize_tls_reloc(!parameters
->options().shared(), r_type
);
6076 if (tlsopt
== tls::TLSOPT_TO_LE
)
6078 layout
->set_has_static_tls();
6081 gold_assert(tlsopt
== tls::TLSOPT_NONE
);
6083 got
->add_local_pair_with_rel(object
,r_sym
, data_shndx
,
6085 target
->rela_dyn_section(layout
),
6086 elfcpp::R_AARCH64_TLS_DTPMOD64
);
6090 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2
:
6091 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1
:
6092 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
:
6093 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0
:
6094 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
:
6095 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
6096 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
6097 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
6099 layout
->set_has_static_tls();
6100 bool output_is_shared
= parameters
->options().shared();
6101 if (output_is_shared
)
6102 gold_error(_("%s: unsupported TLSLE reloc %u in shared code."),
6103 object
->name().c_str(), r_type
);
6107 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21
:
6108 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
:
6110 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6111 optimize_tls_reloc(!parameters
->options().shared(), r_type
);
6112 if (tlsopt
== tls::TLSOPT_NONE
)
6114 // Create a GOT entry for the module index.
6115 target
->got_mod_index_entry(symtab
, layout
, object
);
6117 else if (tlsopt
!= tls::TLSOPT_TO_LE
)
6118 unsupported_reloc_local(object
, r_type
);
6122 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1
:
6123 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
6124 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12
:
6125 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
:
6128 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
6129 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
6130 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
6132 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6133 optimize_tls_reloc(!parameters
->options().shared(), r_type
);
6134 target
->define_tls_base_symbol(symtab
, layout
);
6135 if (tlsopt
== tls::TLSOPT_NONE
)
6137 // Create reserved PLT and GOT entries for the resolver.
6138 target
->reserve_tlsdesc_entries(symtab
, layout
);
6140 // Generate a double GOT entry with an R_AARCH64_TLSDESC reloc.
6141 // The R_AARCH64_TLSDESC reloc is resolved lazily, so the GOT
6142 // entry needs to be in an area in .got.plt, not .got. Call
6143 // got_section to make sure the section has been created.
6144 target
->got_section(symtab
, layout
);
6145 Output_data_got
<size
, big_endian
>* got
=
6146 target
->got_tlsdesc_section();
6147 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6148 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
6150 unsigned int got_offset
= got
->add_constant(0);
6151 got
->add_constant(0);
6152 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
6154 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
6155 // We store the arguments we need in a vector, and use
6156 // the index into the vector as the parameter to pass
6157 // to the target specific routines.
6158 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
6159 void* arg
= reinterpret_cast<void*>(intarg
);
6160 rt
->add_target_specific(elfcpp::R_AARCH64_TLSDESC
, arg
,
6161 got
, got_offset
, 0);
6164 else if (tlsopt
!= tls::TLSOPT_TO_LE
)
6165 unsupported_reloc_local(object
, r_type
);
6169 case elfcpp::R_AARCH64_TLSDESC_CALL
:
6173 unsupported_reloc_local(object
, r_type
);
6178 // Report an unsupported relocation against a global symbol.
6180 template<int size
, bool big_endian
>
6182 Target_aarch64
<size
, big_endian
>::Scan::unsupported_reloc_global(
6183 Sized_relobj_file
<size
, big_endian
>* object
,
6184 unsigned int r_type
,
6187 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6188 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
6191 template<int size
, bool big_endian
>
6193 Target_aarch64
<size
, big_endian
>::Scan::global(
6194 Symbol_table
* symtab
,
6196 Target_aarch64
<size
, big_endian
>* target
,
6197 Sized_relobj_file
<size
, big_endian
> * object
,
6198 unsigned int data_shndx
,
6199 Output_section
* output_section
,
6200 const elfcpp::Rela
<size
, big_endian
>& rela
,
6201 unsigned int r_type
,
6204 // A STT_GNU_IFUNC symbol may require a PLT entry.
6205 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
6206 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
6207 target
->make_plt_entry(symtab
, layout
, gsym
);
6209 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>
6211 const AArch64_reloc_property
* arp
=
6212 aarch64_reloc_property_table
->get_reloc_property(r_type
);
6213 gold_assert(arp
!= NULL
);
6217 case elfcpp::R_AARCH64_ABS16
:
6218 case elfcpp::R_AARCH64_ABS32
:
6219 case elfcpp::R_AARCH64_ABS64
:
6221 // Make a PLT entry if necessary.
6222 if (gsym
->needs_plt_entry())
6224 target
->make_plt_entry(symtab
, layout
, gsym
);
6225 // Since this is not a PC-relative relocation, we may be
6226 // taking the address of a function. In that case we need to
6227 // set the entry in the dynamic symbol table to the address of
6229 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
6230 gsym
->set_needs_dynsym_value();
6232 // Make a dynamic relocation if necessary.
6233 if (gsym
->needs_dynamic_reloc(arp
->reference_flags()))
6235 if (!parameters
->options().output_is_position_independent()
6236 && gsym
->may_need_copy_reloc())
6238 target
->copy_reloc(symtab
, layout
, object
,
6239 data_shndx
, output_section
, gsym
, rela
);
6241 else if (r_type
== elfcpp::R_AARCH64_ABS64
6242 && gsym
->type() == elfcpp::STT_GNU_IFUNC
6243 && gsym
->can_use_relative_reloc(false)
6244 && !gsym
->is_from_dynobj()
6245 && !gsym
->is_undefined()
6246 && !gsym
->is_preemptible())
6248 // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
6249 // symbol. This makes a function address in a PIE executable
6250 // match the address in a shared library that it links against.
6251 Reloc_section
* rela_dyn
=
6252 target
->rela_irelative_section(layout
);
6253 unsigned int r_type
= elfcpp::R_AARCH64_IRELATIVE
;
6254 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
6255 output_section
, object
,
6257 rela
.get_r_offset(),
6258 rela
.get_r_addend());
6260 else if (r_type
== elfcpp::R_AARCH64_ABS64
6261 && gsym
->can_use_relative_reloc(false))
6263 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6264 rela_dyn
->add_global_relative(gsym
,
6265 elfcpp::R_AARCH64_RELATIVE
,
6269 rela
.get_r_offset(),
6270 rela
.get_r_addend(),
6275 check_non_pic(object
, r_type
);
6276 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*
6277 rela_dyn
= target
->rela_dyn_section(layout
);
6278 rela_dyn
->add_global(
6279 gsym
, r_type
, output_section
, object
,
6280 data_shndx
, rela
.get_r_offset(),rela
.get_r_addend());
6286 case elfcpp::R_AARCH64_PREL16
:
6287 case elfcpp::R_AARCH64_PREL32
:
6288 case elfcpp::R_AARCH64_PREL64
:
6289 // This is used to fill the GOT absolute address.
6290 if (gsym
->needs_plt_entry())
6292 target
->make_plt_entry(symtab
, layout
, gsym
);
6296 case elfcpp::R_AARCH64_LD_PREL_LO19
: // 273
6297 case elfcpp::R_AARCH64_ADR_PREL_LO21
: // 274
6298 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21
: // 275
6299 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC
: // 276
6300 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC
: // 277
6301 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC
: // 278
6302 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC
: // 284
6303 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC
: // 285
6304 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC
: // 286
6305 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC
: // 299
6307 if (gsym
->needs_plt_entry())
6308 target
->make_plt_entry(symtab
, layout
, gsym
);
6309 // Make a dynamic relocation if necessary.
6310 if (gsym
->needs_dynamic_reloc(arp
->reference_flags()))
6312 if (parameters
->options().output_is_executable()
6313 && gsym
->may_need_copy_reloc())
6315 target
->copy_reloc(symtab
, layout
, object
,
6316 data_shndx
, output_section
, gsym
, rela
);
6322 case elfcpp::R_AARCH64_ADR_GOT_PAGE
:
6323 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC
:
6325 // This pair of relocations is used to access a specific GOT entry.
6326 // Note a GOT entry is an *address* to a symbol.
6327 // The symbol requires a GOT entry
6328 Output_data_got_aarch64
<size
, big_endian
>* got
=
6329 target
->got_section(symtab
, layout
);
6330 if (gsym
->final_value_is_known())
6332 // For a STT_GNU_IFUNC symbol we want the PLT address.
6333 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
6334 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
6336 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
6340 // If this symbol is not fully resolved, we need to add a dynamic
6341 // relocation for it.
6342 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6344 // Use a GLOB_DAT rather than a RELATIVE reloc if:
6346 // 1) The symbol may be defined in some other module.
6347 // 2) We are building a shared library and this is a protected
6348 // symbol; using GLOB_DAT means that the dynamic linker can use
6349 // the address of the PLT in the main executable when appropriate
6350 // so that function address comparisons work.
6351 // 3) This is a STT_GNU_IFUNC symbol in position dependent code,
6352 // again so that function address comparisons work.
6353 if (gsym
->is_from_dynobj()
6354 || gsym
->is_undefined()
6355 || gsym
->is_preemptible()
6356 || (gsym
->visibility() == elfcpp::STV_PROTECTED
6357 && parameters
->options().shared())
6358 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
6359 && parameters
->options().output_is_position_independent()))
6360 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
6361 rela_dyn
, elfcpp::R_AARCH64_GLOB_DAT
);
6364 // For a STT_GNU_IFUNC symbol we want to write the PLT
6365 // offset into the GOT, so that function pointer
6366 // comparisons work correctly.
6368 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
6369 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
6372 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
6373 // Tell the dynamic linker to use the PLT address
6374 // when resolving relocations.
6375 if (gsym
->is_from_dynobj()
6376 && !parameters
->options().shared())
6377 gsym
->set_needs_dynsym_value();
6381 rela_dyn
->add_global_relative(
6382 gsym
, elfcpp::R_AARCH64_RELATIVE
,
6384 gsym
->got_offset(GOT_TYPE_STANDARD
),
6393 case elfcpp::R_AARCH64_TSTBR14
:
6394 case elfcpp::R_AARCH64_CONDBR19
:
6395 case elfcpp::R_AARCH64_JUMP26
:
6396 case elfcpp::R_AARCH64_CALL26
:
6398 if (gsym
->final_value_is_known())
6401 if (gsym
->is_defined() &&
6402 !gsym
->is_from_dynobj() &&
6403 !gsym
->is_preemptible())
6406 // Make plt entry for function call.
6407 target
->make_plt_entry(symtab
, layout
, gsym
);
6411 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21
:
6412 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
: // General dynamic
6414 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6415 optimize_tls_reloc(gsym
->final_value_is_known(), r_type
);
6416 if (tlsopt
== tls::TLSOPT_TO_LE
)
6418 layout
->set_has_static_tls();
6421 gold_assert(tlsopt
== tls::TLSOPT_NONE
);
6424 Output_data_got_aarch64
<size
, big_endian
>* got
=
6425 target
->got_section(symtab
, layout
);
6426 // Create 2 consecutive entries for module index and offset.
6427 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
6428 target
->rela_dyn_section(layout
),
6429 elfcpp::R_AARCH64_TLS_DTPMOD64
,
6430 elfcpp::R_AARCH64_TLS_DTPREL64
);
6434 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21
:
6435 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
: // Local dynamic
6437 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6438 optimize_tls_reloc(!parameters
->options().shared(), r_type
);
6439 if (tlsopt
== tls::TLSOPT_NONE
)
6441 // Create a GOT entry for the module index.
6442 target
->got_mod_index_entry(symtab
, layout
, object
);
6444 else if (tlsopt
!= tls::TLSOPT_TO_LE
)
6445 unsupported_reloc_local(object
, r_type
);
6449 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1
:
6450 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
6451 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12
:
6452 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
: // Other local dynamic
6455 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
6456 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
: // Initial executable
6458 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6459 optimize_tls_reloc(gsym
->final_value_is_known(), r_type
);
6460 if (tlsopt
== tls::TLSOPT_TO_LE
)
6463 layout
->set_has_static_tls();
6464 // Create a GOT entry for the tp-relative offset.
6465 Output_data_got_aarch64
<size
, big_endian
>* got
6466 = target
->got_section(symtab
, layout
);
6467 if (!parameters
->doing_static_link())
6469 got
->add_global_with_rel(
6470 gsym
, GOT_TYPE_TLS_OFFSET
,
6471 target
->rela_dyn_section(layout
),
6472 elfcpp::R_AARCH64_TLS_TPREL64
);
6474 if (!gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
))
6476 got
->add_global(gsym
, GOT_TYPE_TLS_OFFSET
);
6477 unsigned int got_offset
=
6478 gsym
->got_offset(GOT_TYPE_TLS_OFFSET
);
6479 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
6480 gold_assert(addend
== 0);
6481 got
->add_static_reloc(got_offset
,
6482 elfcpp::R_AARCH64_TLS_TPREL64
, gsym
);
6487 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2
:
6488 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1
:
6489 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
:
6490 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0
:
6491 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
:
6492 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
6493 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
6494 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
: // Local executable
6495 layout
->set_has_static_tls();
6496 if (parameters
->options().shared())
6497 gold_error(_("%s: unsupported TLSLE reloc type %u in shared objects."),
6498 object
->name().c_str(), r_type
);
6501 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
6502 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
6503 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
: // TLS descriptor
6505 target
->define_tls_base_symbol(symtab
, layout
);
6506 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
6507 optimize_tls_reloc(gsym
->final_value_is_known(), r_type
);
6508 if (tlsopt
== tls::TLSOPT_NONE
)
6510 // Create reserved PLT and GOT entries for the resolver.
6511 target
->reserve_tlsdesc_entries(symtab
, layout
);
6513 // Create a double GOT entry with an R_AARCH64_TLSDESC
6514 // relocation. The R_AARCH64_TLSDESC is resolved lazily, so the GOT
6515 // entry needs to be in an area in .got.plt, not .got. Call
6516 // got_section to make sure the section has been created.
6517 target
->got_section(symtab
, layout
);
6518 Output_data_got
<size
, big_endian
>* got
=
6519 target
->got_tlsdesc_section();
6520 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
6521 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
6522 elfcpp::R_AARCH64_TLSDESC
, 0);
6524 else if (tlsopt
== tls::TLSOPT_TO_IE
)
6526 // Create a GOT entry for the tp-relative offset.
6527 Output_data_got
<size
, big_endian
>* got
6528 = target
->got_section(symtab
, layout
);
6529 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
6530 target
->rela_dyn_section(layout
),
6531 elfcpp::R_AARCH64_TLS_TPREL64
);
6533 else if (tlsopt
!= tls::TLSOPT_TO_LE
)
6534 unsupported_reloc_global(object
, r_type
, gsym
);
6538 case elfcpp::R_AARCH64_TLSDESC_CALL
:
6542 gold_error(_("%s: unsupported reloc type in global scan"),
6543 aarch64_reloc_property_table
->
6544 reloc_name_in_error_message(r_type
).c_str());
6547 } // End of Scan::global
6550 // Create the PLT section.
6551 template<int size
, bool big_endian
>
6553 Target_aarch64
<size
, big_endian
>::make_plt_section(
6554 Symbol_table
* symtab
, Layout
* layout
)
6556 if (this->plt_
== NULL
)
6558 // Create the GOT section first.
6559 this->got_section(symtab
, layout
);
6561 this->plt_
= this->make_data_plt(layout
, this->got_
, this->got_plt_
,
6562 this->got_irelative_
);
6564 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
6566 | elfcpp::SHF_EXECINSTR
),
6567 this->plt_
, ORDER_PLT
, false);
6569 // Make the sh_info field of .rela.plt point to .plt.
6570 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
6571 rela_plt_os
->set_info_section(this->plt_
->output_section());
6575 // Return the section for TLSDESC relocations.
6577 template<int size
, bool big_endian
>
6578 typename Target_aarch64
<size
, big_endian
>::Reloc_section
*
6579 Target_aarch64
<size
, big_endian
>::rela_tlsdesc_section(Layout
* layout
) const
6581 return this->plt_section()->rela_tlsdesc(layout
);
6584 // Create a PLT entry for a global symbol.
6586 template<int size
, bool big_endian
>
6588 Target_aarch64
<size
, big_endian
>::make_plt_entry(
6589 Symbol_table
* symtab
,
6593 if (gsym
->has_plt_offset())
6596 if (this->plt_
== NULL
)
6597 this->make_plt_section(symtab
, layout
);
6599 this->plt_
->add_entry(symtab
, layout
, gsym
);
6602 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6604 template<int size
, bool big_endian
>
6606 Target_aarch64
<size
, big_endian
>::make_local_ifunc_plt_entry(
6607 Symbol_table
* symtab
, Layout
* layout
,
6608 Sized_relobj_file
<size
, big_endian
>* relobj
,
6609 unsigned int local_sym_index
)
6611 if (relobj
->local_has_plt_offset(local_sym_index
))
6613 if (this->plt_
== NULL
)
6614 this->make_plt_section(symtab
, layout
);
6615 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
6618 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
6621 template<int size
, bool big_endian
>
6623 Target_aarch64
<size
, big_endian
>::gc_process_relocs(
6624 Symbol_table
* symtab
,
6626 Sized_relobj_file
<size
, big_endian
>* object
,
6627 unsigned int data_shndx
,
6628 unsigned int sh_type
,
6629 const unsigned char* prelocs
,
6631 Output_section
* output_section
,
6632 bool needs_special_offset_handling
,
6633 size_t local_symbol_count
,
6634 const unsigned char* plocal_symbols
)
6636 if (sh_type
== elfcpp::SHT_REL
)
6641 gold::gc_process_relocs
<
6643 Target_aarch64
<size
, big_endian
>,
6645 typename Target_aarch64
<size
, big_endian
>::Scan
,
6646 typename Target_aarch64
<size
, big_endian
>::Relocatable_size_for_reloc
>(
6655 needs_special_offset_handling
,
6660 // Scan relocations for a section.
6662 template<int size
, bool big_endian
>
6664 Target_aarch64
<size
, big_endian
>::scan_relocs(
6665 Symbol_table
* symtab
,
6667 Sized_relobj_file
<size
, big_endian
>* object
,
6668 unsigned int data_shndx
,
6669 unsigned int sh_type
,
6670 const unsigned char* prelocs
,
6672 Output_section
* output_section
,
6673 bool needs_special_offset_handling
,
6674 size_t local_symbol_count
,
6675 const unsigned char* plocal_symbols
)
6677 if (sh_type
== elfcpp::SHT_REL
)
6679 gold_error(_("%s: unsupported REL reloc section"),
6680 object
->name().c_str());
6683 gold::scan_relocs
<size
, big_endian
, Target_aarch64
, elfcpp::SHT_RELA
, Scan
>(
6692 needs_special_offset_handling
,
6697 // Return the value to use for a dynamic which requires special
6698 // treatment. This is how we support equality comparisons of function
6699 // pointers across shared library boundaries, as described in the
6700 // processor specific ABI supplement.
6702 template<int size
, bool big_endian
>
6704 Target_aarch64
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
6706 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
6707 return this->plt_address_for_global(gsym
);
6711 // Finalize the sections.
6713 template<int size
, bool big_endian
>
6715 Target_aarch64
<size
, big_endian
>::do_finalize_sections(
6717 const Input_objects
*,
6718 Symbol_table
* symtab
)
6720 const Reloc_section
* rel_plt
= (this->plt_
== NULL
6722 : this->plt_
->rela_plt());
6723 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
6724 this->rela_dyn_
, true, false);
6726 // Emit any relocs we saved in an attempt to avoid generating COPY
6728 if (this->copy_relocs_
.any_saved_relocs())
6729 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
6731 // Fill in some more dynamic tags.
6732 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
6735 if (this->plt_
!= NULL
6736 && this->plt_
->output_section() != NULL
6737 && this->plt_
->has_tlsdesc_entry())
6739 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
6740 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
6741 this->got_
->finalize_data_size();
6742 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
6743 this->plt_
, plt_offset
);
6744 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
6745 this->got_
, got_offset
);
6749 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
6750 // the .got.plt section.
6751 Symbol
* sym
= this->global_offset_table_
;
6754 uint64_t data_size
= this->got_plt_
->current_data_size();
6755 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
6757 // If the .got section is more than 0x8000 bytes, we add
6758 // 0x8000 to the value of _GLOBAL_OFFSET_TABLE_, so that 16
6759 // bit relocations have a greater chance of working.
6760 if (data_size
>= 0x8000)
6761 symtab
->get_sized_symbol
<size
>(sym
)->set_value(
6762 symtab
->get_sized_symbol
<size
>(sym
)->value() + 0x8000);
6765 if (parameters
->doing_static_link()
6766 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
6768 // If linking statically, make sure that the __rela_iplt symbols
6769 // were defined if necessary, even if we didn't create a PLT.
6770 static const Define_symbol_in_segment syms
[] =
6773 "__rela_iplt_start", // name
6774 elfcpp::PT_LOAD
, // segment_type
6775 elfcpp::PF_W
, // segment_flags_set
6776 elfcpp::PF(0), // segment_flags_clear
6779 elfcpp::STT_NOTYPE
, // type
6780 elfcpp::STB_GLOBAL
, // binding
6781 elfcpp::STV_HIDDEN
, // visibility
6783 Symbol::SEGMENT_START
, // offset_from_base
6787 "__rela_iplt_end", // name
6788 elfcpp::PT_LOAD
, // segment_type
6789 elfcpp::PF_W
, // segment_flags_set
6790 elfcpp::PF(0), // segment_flags_clear
6793 elfcpp::STT_NOTYPE
, // type
6794 elfcpp::STB_GLOBAL
, // binding
6795 elfcpp::STV_HIDDEN
, // visibility
6797 Symbol::SEGMENT_START
, // offset_from_base
6802 symtab
->define_symbols(layout
, 2, syms
,
6803 layout
->script_options()->saw_sections_clause());
6809 // Perform a relocation.
6811 template<int size
, bool big_endian
>
6813 Target_aarch64
<size
, big_endian
>::Relocate::relocate(
6814 const Relocate_info
<size
, big_endian
>* relinfo
,
6815 Target_aarch64
<size
, big_endian
>* target
,
6818 const elfcpp::Rela
<size
, big_endian
>& rela
,
6819 unsigned int r_type
,
6820 const Sized_symbol
<size
>* gsym
,
6821 const Symbol_value
<size
>* psymval
,
6822 unsigned char* view
,
6823 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
6824 section_size_type
/* view_size */)
6829 typedef AArch64_relocate_functions
<size
, big_endian
> Reloc
;
6831 const AArch64_reloc_property
* reloc_property
=
6832 aarch64_reloc_property_table
->get_reloc_property(r_type
);
6834 if (reloc_property
== NULL
)
6836 std::string reloc_name
=
6837 aarch64_reloc_property_table
->reloc_name_in_error_message(r_type
);
6838 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6839 _("cannot relocate %s in object file"),
6840 reloc_name
.c_str());
6844 const Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
6846 // Pick the value to use for symbols defined in the PLT.
6847 Symbol_value
<size
> symval
;
6849 && gsym
->use_plt_offset(reloc_property
->reference_flags()))
6851 symval
.set_output_value(target
->plt_address_for_global(gsym
));
6854 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
6856 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6857 if (object
->local_has_plt_offset(r_sym
))
6859 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
6864 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
6866 // Get the GOT offset if needed.
6867 // For aarch64, the GOT pointer points to the start of the GOT section.
6868 bool have_got_offset
= false;
6870 int got_base
= (target
->got_
!= NULL
6871 ? (target
->got_
->current_data_size() >= 0x8000
6876 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0
:
6877 case elfcpp::R_AARCH64_MOVW_GOTOFF_G0_NC
:
6878 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1
:
6879 case elfcpp::R_AARCH64_MOVW_GOTOFF_G1_NC
:
6880 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2
:
6881 case elfcpp::R_AARCH64_MOVW_GOTOFF_G2_NC
:
6882 case elfcpp::R_AARCH64_MOVW_GOTOFF_G3
:
6883 case elfcpp::R_AARCH64_GOTREL64
:
6884 case elfcpp::R_AARCH64_GOTREL32
:
6885 case elfcpp::R_AARCH64_GOT_LD_PREL19
:
6886 case elfcpp::R_AARCH64_LD64_GOTOFF_LO15
:
6887 case elfcpp::R_AARCH64_ADR_GOT_PAGE
:
6888 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC
:
6889 case elfcpp::R_AARCH64_LD64_GOTPAGE_LO15
:
6892 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
6893 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - got_base
;
6897 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6898 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
6899 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
6902 have_got_offset
= true;
6909 typename
Reloc::Status reloc_status
= Reloc::STATUS_OKAY
;
6910 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
6913 case elfcpp::R_AARCH64_NONE
:
6916 case elfcpp::R_AARCH64_ABS64
:
6917 if (!parameters
->options().apply_dynamic_relocs()
6918 && parameters
->options().output_is_position_independent()
6920 && gsym
->needs_dynamic_reloc(reloc_property
->reference_flags())
6921 && !gsym
->can_use_relative_reloc(false))
6922 // We have generated an absolute dynamic relocation, so do not
6923 // apply the relocation statically. (Works around bugs in older
6924 // Android dynamic linkers.)
6926 reloc_status
= Reloc::template rela_ua
<64>(
6927 view
, object
, psymval
, addend
, reloc_property
);
6930 case elfcpp::R_AARCH64_ABS32
:
6931 if (!parameters
->options().apply_dynamic_relocs()
6932 && parameters
->options().output_is_position_independent()
6934 && gsym
->needs_dynamic_reloc(reloc_property
->reference_flags()))
6935 // We have generated an absolute dynamic relocation, so do not
6936 // apply the relocation statically. (Works around bugs in older
6937 // Android dynamic linkers.)
6939 reloc_status
= Reloc::template rela_ua
<32>(
6940 view
, object
, psymval
, addend
, reloc_property
);
6943 case elfcpp::R_AARCH64_ABS16
:
6944 if (!parameters
->options().apply_dynamic_relocs()
6945 && parameters
->options().output_is_position_independent()
6947 && gsym
->needs_dynamic_reloc(reloc_property
->reference_flags()))
6948 // We have generated an absolute dynamic relocation, so do not
6949 // apply the relocation statically. (Works around bugs in older
6950 // Android dynamic linkers.)
6952 reloc_status
= Reloc::template rela_ua
<16>(
6953 view
, object
, psymval
, addend
, reloc_property
);
6956 case elfcpp::R_AARCH64_PREL64
:
6957 reloc_status
= Reloc::template pcrela_ua
<64>(
6958 view
, object
, psymval
, addend
, address
, reloc_property
);
6961 case elfcpp::R_AARCH64_PREL32
:
6962 reloc_status
= Reloc::template pcrela_ua
<32>(
6963 view
, object
, psymval
, addend
, address
, reloc_property
);
6966 case elfcpp::R_AARCH64_PREL16
:
6967 reloc_status
= Reloc::template pcrela_ua
<16>(
6968 view
, object
, psymval
, addend
, address
, reloc_property
);
6971 case elfcpp::R_AARCH64_LD_PREL_LO19
:
6972 reloc_status
= Reloc::template pcrela_general
<32>(
6973 view
, object
, psymval
, addend
, address
, reloc_property
);
6976 case elfcpp::R_AARCH64_ADR_PREL_LO21
:
6977 reloc_status
= Reloc::adr(view
, object
, psymval
, addend
,
6978 address
, reloc_property
);
6981 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21_NC
:
6982 case elfcpp::R_AARCH64_ADR_PREL_PG_HI21
:
6983 reloc_status
= Reloc::adrp(view
, object
, psymval
, addend
, address
,
6987 case elfcpp::R_AARCH64_LDST8_ABS_LO12_NC
:
6988 case elfcpp::R_AARCH64_LDST16_ABS_LO12_NC
:
6989 case elfcpp::R_AARCH64_LDST32_ABS_LO12_NC
:
6990 case elfcpp::R_AARCH64_LDST64_ABS_LO12_NC
:
6991 case elfcpp::R_AARCH64_LDST128_ABS_LO12_NC
:
6992 case elfcpp::R_AARCH64_ADD_ABS_LO12_NC
:
6993 reloc_status
= Reloc::template rela_general
<32>(
6994 view
, object
, psymval
, addend
, reloc_property
);
6997 case elfcpp::R_AARCH64_CALL26
:
6998 if (this->skip_call_tls_get_addr_
)
7000 // Double check that the TLSGD insn has been optimized away.
7001 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
7002 Insntype insn
= elfcpp::Swap
<32, big_endian
>::readval(
7003 reinterpret_cast<Insntype
*>(view
));
7004 gold_assert((insn
& 0xff000000) == 0x91000000);
7006 reloc_status
= Reloc::STATUS_OKAY
;
7007 this->skip_call_tls_get_addr_
= false;
7008 // Return false to stop further processing this reloc.
7012 case elfcpp::R_AARCH64_JUMP26
:
7013 if (Reloc::maybe_apply_stub(r_type
, relinfo
, rela
, view
, address
,
7014 gsym
, psymval
, object
,
7015 target
->stub_group_size_
))
7018 case elfcpp::R_AARCH64_TSTBR14
:
7019 case elfcpp::R_AARCH64_CONDBR19
:
7020 reloc_status
= Reloc::template pcrela_general
<32>(
7021 view
, object
, psymval
, addend
, address
, reloc_property
);
7024 case elfcpp::R_AARCH64_ADR_GOT_PAGE
:
7025 gold_assert(have_got_offset
);
7026 value
= target
->got_
->address() + got_base
+ got_offset
;
7027 reloc_status
= Reloc::adrp(view
, value
+ addend
, address
);
7030 case elfcpp::R_AARCH64_LD64_GOT_LO12_NC
:
7031 gold_assert(have_got_offset
);
7032 value
= target
->got_
->address() + got_base
+ got_offset
;
7033 reloc_status
= Reloc::template rela_general
<32>(
7034 view
, value
, addend
, reloc_property
);
7037 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21
:
7038 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
:
7039 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21
:
7040 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
:
7041 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1
:
7042 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
7043 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12
:
7044 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
:
7045 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
7046 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
7047 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2
:
7048 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1
:
7049 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
:
7050 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0
:
7051 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
:
7052 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
7053 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
7054 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
7055 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
7056 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
7057 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
7058 case elfcpp::R_AARCH64_TLSDESC_CALL
:
7059 reloc_status
= relocate_tls(relinfo
, target
, relnum
, rela
, r_type
,
7060 gsym
, psymval
, view
, address
);
7063 // These are dynamic relocations, which are unexpected when linking.
7064 case elfcpp::R_AARCH64_COPY
:
7065 case elfcpp::R_AARCH64_GLOB_DAT
:
7066 case elfcpp::R_AARCH64_JUMP_SLOT
:
7067 case elfcpp::R_AARCH64_RELATIVE
:
7068 case elfcpp::R_AARCH64_IRELATIVE
:
7069 case elfcpp::R_AARCH64_TLS_DTPREL64
:
7070 case elfcpp::R_AARCH64_TLS_DTPMOD64
:
7071 case elfcpp::R_AARCH64_TLS_TPREL64
:
7072 case elfcpp::R_AARCH64_TLSDESC
:
7073 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7074 _("unexpected reloc %u in object file"),
7079 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7080 _("unsupported reloc %s"),
7081 reloc_property
->name().c_str());
7085 // Report any errors.
7086 switch (reloc_status
)
7088 case Reloc::STATUS_OKAY
:
7090 case Reloc::STATUS_OVERFLOW
:
7091 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7092 _("relocation overflow in %s"),
7093 reloc_property
->name().c_str());
7095 case Reloc::STATUS_BAD_RELOC
:
7096 gold_error_at_location(
7099 rela
.get_r_offset(),
7100 _("unexpected opcode while processing relocation %s"),
7101 reloc_property
->name().c_str());
7111 template<int size
, bool big_endian
>
7113 typename AArch64_relocate_functions
<size
, big_endian
>::Status
7114 Target_aarch64
<size
, big_endian
>::Relocate::relocate_tls(
7115 const Relocate_info
<size
, big_endian
>* relinfo
,
7116 Target_aarch64
<size
, big_endian
>* target
,
7118 const elfcpp::Rela
<size
, big_endian
>& rela
,
7119 unsigned int r_type
, const Sized_symbol
<size
>* gsym
,
7120 const Symbol_value
<size
>* psymval
,
7121 unsigned char* view
,
7122 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
7124 typedef AArch64_relocate_functions
<size
, big_endian
> aarch64_reloc_funcs
;
7125 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
7127 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
7128 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
7129 const AArch64_reloc_property
* reloc_property
=
7130 aarch64_reloc_property_table
->get_reloc_property(r_type
);
7131 gold_assert(reloc_property
!= NULL
);
7133 const bool is_final
= (gsym
== NULL
7134 ? !parameters
->options().shared()
7135 : gsym
->final_value_is_known());
7136 tls::Tls_optimization tlsopt
= Target_aarch64
<size
, big_endian
>::
7137 optimize_tls_reloc(is_final
, r_type
);
7139 Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
7140 int tls_got_offset_type
;
7143 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21
:
7144 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
: // Global-dynamic
7146 if (tlsopt
== tls::TLSOPT_TO_LE
)
7148 if (tls_segment
== NULL
)
7150 gold_assert(parameters
->errors()->error_count() > 0
7151 || issue_undefined_symbol_error(gsym
));
7152 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7154 return tls_gd_to_le(relinfo
, target
, rela
, r_type
, view
,
7157 else if (tlsopt
== tls::TLSOPT_NONE
)
7159 tls_got_offset_type
= GOT_TYPE_TLS_PAIR
;
7160 // Firstly get the address for the got entry.
7161 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_entry_address
;
7164 gold_assert(gsym
->has_got_offset(tls_got_offset_type
));
7165 got_entry_address
= target
->got_
->address() +
7166 gsym
->got_offset(tls_got_offset_type
);
7170 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7172 object
->local_has_got_offset(r_sym
, tls_got_offset_type
));
7173 got_entry_address
= target
->got_
->address() +
7174 object
->local_got_offset(r_sym
, tls_got_offset_type
);
7177 // Relocate the address into adrp/ld, adrp/add pair.
7180 case elfcpp::R_AARCH64_TLSGD_ADR_PAGE21
:
7181 return aarch64_reloc_funcs::adrp(
7182 view
, got_entry_address
+ addend
, address
);
7186 case elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
:
7187 return aarch64_reloc_funcs::template rela_general
<32>(
7188 view
, got_entry_address
, addend
, reloc_property
);
7195 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7196 _("unsupported gd_to_ie relaxation on %u"),
7201 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21
:
7202 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
: // Local-dynamic
7204 if (tlsopt
== tls::TLSOPT_TO_LE
)
7206 if (tls_segment
== NULL
)
7208 gold_assert(parameters
->errors()->error_count() > 0
7209 || issue_undefined_symbol_error(gsym
));
7210 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7212 return this->tls_ld_to_le(relinfo
, target
, rela
, r_type
, view
,
7216 gold_assert(tlsopt
== tls::TLSOPT_NONE
);
7217 // Relocate the field with the offset of the GOT entry for
7218 // the module index.
7219 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_entry_address
;
7220 got_entry_address
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
) +
7221 target
->got_
->address());
7225 case elfcpp::R_AARCH64_TLSLD_ADR_PAGE21
:
7226 return aarch64_reloc_funcs::adrp(
7227 view
, got_entry_address
+ addend
, address
);
7230 case elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
:
7231 return aarch64_reloc_funcs::template rela_general
<32>(
7232 view
, got_entry_address
, addend
, reloc_property
);
7241 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1
:
7242 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
7243 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12
:
7244 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
: // Other local-dynamic
7246 AArch64_address value
= psymval
->value(object
, 0);
7247 if (tlsopt
== tls::TLSOPT_TO_LE
)
7249 if (tls_segment
== NULL
)
7251 gold_assert(parameters
->errors()->error_count() > 0
7252 || issue_undefined_symbol_error(gsym
));
7253 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7258 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G1
:
7259 return aarch64_reloc_funcs::movnz(view
, value
+ addend
,
7263 case elfcpp::R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC
:
7264 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_HI12
:
7265 case elfcpp::R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC
:
7266 return aarch64_reloc_funcs::template rela_general
<32>(
7267 view
, value
, addend
, reloc_property
);
7273 // We should never reach here.
7277 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
7278 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
: // Initial-exec
7280 if (tlsopt
== tls::TLSOPT_TO_LE
)
7282 if (tls_segment
== NULL
)
7284 gold_assert(parameters
->errors()->error_count() > 0
7285 || issue_undefined_symbol_error(gsym
));
7286 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7288 return tls_ie_to_le(relinfo
, target
, rela
, r_type
, view
,
7291 tls_got_offset_type
= GOT_TYPE_TLS_OFFSET
;
7293 // Firstly get the address for the got entry.
7294 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_entry_address
;
7297 gold_assert(gsym
->has_got_offset(tls_got_offset_type
));
7298 got_entry_address
= target
->got_
->address() +
7299 gsym
->got_offset(tls_got_offset_type
);
7303 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7305 object
->local_has_got_offset(r_sym
, tls_got_offset_type
));
7306 got_entry_address
= target
->got_
->address() +
7307 object
->local_got_offset(r_sym
, tls_got_offset_type
);
7309 // Relocate the address into adrp/ld, adrp/add pair.
7312 case elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
:
7313 return aarch64_reloc_funcs::adrp(view
, got_entry_address
+ addend
,
7316 case elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
:
7317 return aarch64_reloc_funcs::template rela_general
<32>(
7318 view
, got_entry_address
, addend
, reloc_property
);
7323 // We shall never reach here.
7326 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2
:
7327 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1
:
7328 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1_NC
:
7329 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0
:
7330 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0_NC
:
7331 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
:
7332 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
:
7333 case elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
:
7335 gold_assert(tls_segment
!= NULL
);
7336 AArch64_address value
= psymval
->value(object
, 0);
7338 if (!parameters
->options().shared())
7340 AArch64_address aligned_tcb_size
=
7341 align_address(target
->tcb_size(),
7342 tls_segment
->maximum_alignment());
7343 value
+= aligned_tcb_size
;
7346 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G2
:
7347 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G1
:
7348 case elfcpp::R_AARCH64_TLSLE_MOVW_TPREL_G0
:
7349 return aarch64_reloc_funcs::movnz(view
, value
+ addend
,
7352 return aarch64_reloc_funcs::template
7353 rela_general
<32>(view
,
7360 gold_error(_("%s: unsupported reloc %u "
7361 "in non-static TLSLE mode."),
7362 object
->name().c_str(), r_type
);
7366 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
7367 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
7368 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
7369 case elfcpp::R_AARCH64_TLSDESC_CALL
:
7371 if (tlsopt
== tls::TLSOPT_TO_LE
)
7373 if (tls_segment
== NULL
)
7375 gold_assert(parameters
->errors()->error_count() > 0
7376 || issue_undefined_symbol_error(gsym
));
7377 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7379 return tls_desc_gd_to_le(relinfo
, target
, rela
, r_type
,
7384 tls_got_offset_type
= (tlsopt
== tls::TLSOPT_TO_IE
7385 ? GOT_TYPE_TLS_OFFSET
7386 : GOT_TYPE_TLS_DESC
);
7387 unsigned int got_tlsdesc_offset
= 0;
7388 if (r_type
!= elfcpp::R_AARCH64_TLSDESC_CALL
7389 && tlsopt
== tls::TLSOPT_NONE
)
7391 // We created GOT entries in the .got.tlsdesc portion of the
7392 // .got.plt section, but the offset stored in the symbol is the
7393 // offset within .got.tlsdesc.
7394 got_tlsdesc_offset
= (target
->got_
->data_size()
7395 + target
->got_plt_section()->data_size());
7397 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_entry_address
;
7400 gold_assert(gsym
->has_got_offset(tls_got_offset_type
));
7401 got_entry_address
= target
->got_
->address()
7402 + got_tlsdesc_offset
7403 + gsym
->got_offset(tls_got_offset_type
);
7407 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7409 object
->local_has_got_offset(r_sym
, tls_got_offset_type
));
7410 got_entry_address
= target
->got_
->address() +
7411 got_tlsdesc_offset
+
7412 object
->local_got_offset(r_sym
, tls_got_offset_type
);
7414 if (tlsopt
== tls::TLSOPT_TO_IE
)
7416 if (tls_segment
== NULL
)
7418 gold_assert(parameters
->errors()->error_count() > 0
7419 || issue_undefined_symbol_error(gsym
));
7420 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7422 return tls_desc_gd_to_ie(relinfo
, target
, rela
, r_type
,
7423 view
, psymval
, got_entry_address
,
7427 // Now do tlsdesc relocation.
7430 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
7431 return aarch64_reloc_funcs::adrp(view
,
7432 got_entry_address
+ addend
,
7435 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
7436 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
7437 return aarch64_reloc_funcs::template rela_general
<32>(
7438 view
, got_entry_address
, addend
, reloc_property
);
7440 case elfcpp::R_AARCH64_TLSDESC_CALL
:
7441 return aarch64_reloc_funcs::STATUS_OKAY
;
7451 gold_error(_("%s: unsupported TLS reloc %u."),
7452 object
->name().c_str(), r_type
);
7454 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7455 } // End of relocate_tls.
7458 template<int size
, bool big_endian
>
7460 typename AArch64_relocate_functions
<size
, big_endian
>::Status
7461 Target_aarch64
<size
, big_endian
>::Relocate::tls_gd_to_le(
7462 const Relocate_info
<size
, big_endian
>* relinfo
,
7463 Target_aarch64
<size
, big_endian
>* target
,
7464 const elfcpp::Rela
<size
, big_endian
>& rela
,
7465 unsigned int r_type
,
7466 unsigned char* view
,
7467 const Symbol_value
<size
>* psymval
)
7469 typedef AArch64_relocate_functions
<size
, big_endian
> aarch64_reloc_funcs
;
7470 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
7471 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
7473 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
7474 Insntype insn1
= elfcpp::Swap
<32, big_endian
>::readval(ip
);
7475 Insntype insn2
= elfcpp::Swap
<32, big_endian
>::readval(ip
+ 1);
7476 Insntype insn3
= elfcpp::Swap
<32, big_endian
>::readval(ip
+ 2);
7478 if (r_type
== elfcpp::R_AARCH64_TLSGD_ADD_LO12_NC
)
7480 // This is the 2nd relocs, optimization should already have been
7482 gold_assert((insn1
& 0xfff00000) == 0x91400000);
7483 return aarch64_reloc_funcs::STATUS_OKAY
;
7486 // The original sequence is -
7487 // 90000000 adrp x0, 0 <main>
7488 // 91000000 add x0, x0, #0x0
7489 // 94000000 bl 0 <__tls_get_addr>
7490 // optimized to sequence -
7491 // d53bd040 mrs x0, tpidr_el0
7492 // 91400000 add x0, x0, #0x0, lsl #12
7493 // 91000000 add x0, x0, #0x0
7495 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7496 // encounter the first relocation "R_AARCH64_TLSGD_ADR_PAGE21". Because we
7497 // have to change "bl tls_get_addr", which does not have a corresponding tls
7498 // relocation type. So before proceeding, we need to make sure compiler
7499 // does not change the sequence.
7500 if(!(insn1
== 0x90000000 // adrp x0,0
7501 && insn2
== 0x91000000 // add x0, x0, #0x0
7502 && insn3
== 0x94000000)) // bl 0
7504 // Ideally we should give up gd_to_le relaxation and do gd access.
7505 // However the gd_to_le relaxation decision has been made early
7506 // in the scan stage, where we did not allocate any GOT entry for
7507 // this symbol. Therefore we have to exit and report error now.
7508 gold_error(_("unexpected reloc insn sequence while relaxing "
7509 "tls gd to le for reloc %u."), r_type
);
7510 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7514 insn1
= 0xd53bd040; // mrs x0, tpidr_el0
7515 insn2
= 0x91400000; // add x0, x0, #0x0, lsl #12
7516 insn3
= 0x91000000; // add x0, x0, #0x0
7517 elfcpp::Swap
<32, big_endian
>::writeval(ip
, insn1
);
7518 elfcpp::Swap
<32, big_endian
>::writeval(ip
+ 1, insn2
);
7519 elfcpp::Swap
<32, big_endian
>::writeval(ip
+ 2, insn3
);
7521 // Calculate tprel value.
7522 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
7523 gold_assert(tls_segment
!= NULL
);
7524 AArch64_address value
= psymval
->value(relinfo
->object
, 0);
7525 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
7526 AArch64_address aligned_tcb_size
=
7527 align_address(target
->tcb_size(), tls_segment
->maximum_alignment());
7528 AArch64_address x
= value
+ aligned_tcb_size
;
7530 // After new insns are written, apply TLSLE relocs.
7531 const AArch64_reloc_property
* rp1
=
7532 aarch64_reloc_property_table
->get_reloc_property(
7533 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
);
7534 const AArch64_reloc_property
* rp2
=
7535 aarch64_reloc_property_table
->get_reloc_property(
7536 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
);
7537 gold_assert(rp1
!= NULL
&& rp2
!= NULL
);
7539 typename
aarch64_reloc_funcs::Status s1
=
7540 aarch64_reloc_funcs::template rela_general
<32>(view
+ 4,
7544 if (s1
!= aarch64_reloc_funcs::STATUS_OKAY
)
7547 typename
aarch64_reloc_funcs::Status s2
=
7548 aarch64_reloc_funcs::template rela_general
<32>(view
+ 8,
7553 this->skip_call_tls_get_addr_
= true;
7555 } // End of tls_gd_to_le
7558 template<int size
, bool big_endian
>
7560 typename AArch64_relocate_functions
<size
, big_endian
>::Status
7561 Target_aarch64
<size
, big_endian
>::Relocate::tls_ld_to_le(
7562 const Relocate_info
<size
, big_endian
>* relinfo
,
7563 Target_aarch64
<size
, big_endian
>* target
,
7564 const elfcpp::Rela
<size
, big_endian
>& rela
,
7565 unsigned int r_type
,
7566 unsigned char* view
,
7567 const Symbol_value
<size
>* psymval
)
7569 typedef AArch64_relocate_functions
<size
, big_endian
> aarch64_reloc_funcs
;
7570 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
7571 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
7573 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
7574 Insntype insn1
= elfcpp::Swap
<32, big_endian
>::readval(ip
);
7575 Insntype insn2
= elfcpp::Swap
<32, big_endian
>::readval(ip
+ 1);
7576 Insntype insn3
= elfcpp::Swap
<32, big_endian
>::readval(ip
+ 2);
7578 if (r_type
== elfcpp::R_AARCH64_TLSLD_ADD_LO12_NC
)
7580 // This is the 2nd relocs, optimization should already have been
7582 gold_assert((insn1
& 0xfff00000) == 0x91400000);
7583 return aarch64_reloc_funcs::STATUS_OKAY
;
7586 // The original sequence is -
7587 // 90000000 adrp x0, 0 <main>
7588 // 91000000 add x0, x0, #0x0
7589 // 94000000 bl 0 <__tls_get_addr>
7590 // optimized to sequence -
7591 // d53bd040 mrs x0, tpidr_el0
7592 // 91400000 add x0, x0, #0x0, lsl #12
7593 // 91000000 add x0, x0, #0x0
7595 // Unlike tls_ie_to_le, we change the 3 insns in one function call when we
7596 // encounter the first relocation "R_AARCH64_TLSLD_ADR_PAGE21". Because we
7597 // have to change "bl tls_get_addr", which does not have a corresponding tls
7598 // relocation type. So before proceeding, we need to make sure compiler
7599 // does not change the sequence.
7600 if(!(insn1
== 0x90000000 // adrp x0,0
7601 && insn2
== 0x91000000 // add x0, x0, #0x0
7602 && insn3
== 0x94000000)) // bl 0
7604 // Ideally we should give up gd_to_le relaxation and do gd access.
7605 // However the gd_to_le relaxation decision has been made early
7606 // in the scan stage, where we did not allocate any GOT entry for
7607 // this symbol. Therefore we have to exit and report error now.
7608 gold_error(_("unexpected reloc insn sequence while relaxing "
7609 "tls gd to le for reloc %u."), r_type
);
7610 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7614 insn1
= 0xd53bd040; // mrs x0, tpidr_el0
7615 insn2
= 0x91400000; // add x0, x0, #0x0, lsl #12
7616 insn3
= 0x91000000; // add x0, x0, #0x0
7617 elfcpp::Swap
<32, big_endian
>::writeval(ip
, insn1
);
7618 elfcpp::Swap
<32, big_endian
>::writeval(ip
+ 1, insn2
);
7619 elfcpp::Swap
<32, big_endian
>::writeval(ip
+ 2, insn3
);
7621 // Calculate tprel value.
7622 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
7623 gold_assert(tls_segment
!= NULL
);
7624 AArch64_address value
= psymval
->value(relinfo
->object
, 0);
7625 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
7626 AArch64_address aligned_tcb_size
=
7627 align_address(target
->tcb_size(), tls_segment
->maximum_alignment());
7628 AArch64_address x
= value
+ aligned_tcb_size
;
7630 // After new insns are written, apply TLSLE relocs.
7631 const AArch64_reloc_property
* rp1
=
7632 aarch64_reloc_property_table
->get_reloc_property(
7633 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_HI12
);
7634 const AArch64_reloc_property
* rp2
=
7635 aarch64_reloc_property_table
->get_reloc_property(
7636 elfcpp::R_AARCH64_TLSLE_ADD_TPREL_LO12
);
7637 gold_assert(rp1
!= NULL
&& rp2
!= NULL
);
7639 typename
aarch64_reloc_funcs::Status s1
=
7640 aarch64_reloc_funcs::template rela_general
<32>(view
+ 4,
7644 if (s1
!= aarch64_reloc_funcs::STATUS_OKAY
)
7647 typename
aarch64_reloc_funcs::Status s2
=
7648 aarch64_reloc_funcs::template rela_general
<32>(view
+ 8,
7653 this->skip_call_tls_get_addr_
= true;
7656 } // End of tls_ld_to_le
7658 template<int size
, bool big_endian
>
7660 typename AArch64_relocate_functions
<size
, big_endian
>::Status
7661 Target_aarch64
<size
, big_endian
>::Relocate::tls_ie_to_le(
7662 const Relocate_info
<size
, big_endian
>* relinfo
,
7663 Target_aarch64
<size
, big_endian
>* target
,
7664 const elfcpp::Rela
<size
, big_endian
>& rela
,
7665 unsigned int r_type
,
7666 unsigned char* view
,
7667 const Symbol_value
<size
>* psymval
)
7669 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
7670 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
7671 typedef AArch64_relocate_functions
<size
, big_endian
> aarch64_reloc_funcs
;
7673 AArch64_address value
= psymval
->value(relinfo
->object
, 0);
7674 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
7675 AArch64_address aligned_tcb_address
=
7676 align_address(target
->tcb_size(), tls_segment
->maximum_alignment());
7677 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
7678 AArch64_address x
= value
+ addend
+ aligned_tcb_address
;
7679 // "x" is the offset to tp, we can only do this if x is within
7680 // range [0, 2^32-1]
7681 if (!(size
== 32 || (size
== 64 && (static_cast<uint64_t>(x
) >> 32) == 0)))
7683 gold_error(_("TLS variable referred by reloc %u is too far from TP."),
7685 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7688 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
7689 Insntype insn
= elfcpp::Swap
<32, big_endian
>::readval(ip
);
7692 if (r_type
== elfcpp::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
)
7695 regno
= (insn
& 0x1f);
7696 newinsn
= (0xd2a00000 | regno
) | (((x
>> 16) & 0xffff) << 5);
7698 else if (r_type
== elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
)
7701 regno
= (insn
& 0x1f);
7702 gold_assert(regno
== ((insn
>> 5) & 0x1f));
7703 newinsn
= (0xf2800000 | regno
) | ((x
& 0xffff) << 5);
7708 elfcpp::Swap
<32, big_endian
>::writeval(ip
, newinsn
);
7709 return aarch64_reloc_funcs::STATUS_OKAY
;
7710 } // End of tls_ie_to_le
7713 template<int size
, bool big_endian
>
7715 typename AArch64_relocate_functions
<size
, big_endian
>::Status
7716 Target_aarch64
<size
, big_endian
>::Relocate::tls_desc_gd_to_le(
7717 const Relocate_info
<size
, big_endian
>* relinfo
,
7718 Target_aarch64
<size
, big_endian
>* target
,
7719 const elfcpp::Rela
<size
, big_endian
>& rela
,
7720 unsigned int r_type
,
7721 unsigned char* view
,
7722 const Symbol_value
<size
>* psymval
)
7724 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr AArch64_address
;
7725 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
7726 typedef AArch64_relocate_functions
<size
, big_endian
> aarch64_reloc_funcs
;
7728 // TLSDESC-GD sequence is like:
7729 // adrp x0, :tlsdesc:v1
7730 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7731 // add x0, x0, :tlsdesc_lo12:v1
7734 // After desc_gd_to_le optimization, the sequence will be like:
7735 // movz x0, #0x0, lsl #16
7740 // Calculate tprel value.
7741 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
7742 gold_assert(tls_segment
!= NULL
);
7743 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
7744 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
7745 AArch64_address value
= psymval
->value(relinfo
->object
, addend
);
7746 AArch64_address aligned_tcb_size
=
7747 align_address(target
->tcb_size(), tls_segment
->maximum_alignment());
7748 AArch64_address x
= value
+ aligned_tcb_size
;
7749 // x is the offset to tp, we can only do this if x is within range
7750 // [0, 2^32-1]. If x is out of range, fail and exit.
7751 if (size
== 64 && (static_cast<uint64_t>(x
) >> 32) != 0)
7753 gold_error(_("TLS variable referred by reloc %u is too far from TP. "
7754 "We Can't do gd_to_le relaxation.\n"), r_type
);
7755 return aarch64_reloc_funcs::STATUS_BAD_RELOC
;
7760 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
7761 case elfcpp::R_AARCH64_TLSDESC_CALL
:
7763 newinsn
= 0xd503201f;
7766 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
7768 newinsn
= 0xd2a00000 | (((x
>> 16) & 0xffff) << 5);
7771 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
7773 newinsn
= 0xf2800000 | ((x
& 0xffff) << 5);
7777 gold_error(_("unsupported tlsdesc gd_to_le optimization on reloc %u"),
7781 elfcpp::Swap
<32, big_endian
>::writeval(ip
, newinsn
);
7782 return aarch64_reloc_funcs::STATUS_OKAY
;
7783 } // End of tls_desc_gd_to_le
7786 template<int size
, bool big_endian
>
7788 typename AArch64_relocate_functions
<size
, big_endian
>::Status
7789 Target_aarch64
<size
, big_endian
>::Relocate::tls_desc_gd_to_ie(
7790 const Relocate_info
<size
, big_endian
>* /* relinfo */,
7791 Target_aarch64
<size
, big_endian
>* /* target */,
7792 const elfcpp::Rela
<size
, big_endian
>& rela
,
7793 unsigned int r_type
,
7794 unsigned char* view
,
7795 const Symbol_value
<size
>* /* psymval */,
7796 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_entry_address
,
7797 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
7799 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insntype
;
7800 typedef AArch64_relocate_functions
<size
, big_endian
> aarch64_reloc_funcs
;
7802 // TLSDESC-GD sequence is like:
7803 // adrp x0, :tlsdesc:v1
7804 // ldr x1, [x0, #:tlsdesc_lo12:v1]
7805 // add x0, x0, :tlsdesc_lo12:v1
7808 // After desc_gd_to_ie optimization, the sequence will be like:
7809 // adrp x0, :tlsie:v1
7810 // ldr x0, [x0, :tlsie_lo12:v1]
7814 Insntype
* ip
= reinterpret_cast<Insntype
*>(view
);
7815 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
7819 case elfcpp::R_AARCH64_TLSDESC_ADD_LO12
:
7820 case elfcpp::R_AARCH64_TLSDESC_CALL
:
7822 newinsn
= 0xd503201f;
7823 elfcpp::Swap
<32, big_endian
>::writeval(ip
, newinsn
);
7826 case elfcpp::R_AARCH64_TLSDESC_ADR_PAGE21
:
7828 return aarch64_reloc_funcs::adrp(view
, got_entry_address
+ addend
,
7833 case elfcpp::R_AARCH64_TLSDESC_LD64_LO12
:
7835 // Set ldr target register to be x0.
7836 Insntype insn
= elfcpp::Swap
<32, big_endian
>::readval(ip
);
7838 elfcpp::Swap
<32, big_endian
>::writeval(ip
, insn
);
7840 const AArch64_reloc_property
* reloc_property
=
7841 aarch64_reloc_property_table
->get_reloc_property(
7842 elfcpp::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
);
7843 return aarch64_reloc_funcs::template rela_general
<32>(
7844 view
, got_entry_address
, addend
, reloc_property
);
7849 gold_error(_("Don't support tlsdesc gd_to_ie optimization on reloc %u"),
7853 return aarch64_reloc_funcs::STATUS_OKAY
;
7854 } // End of tls_desc_gd_to_ie
7856 // Relocate section data.
7858 template<int size
, bool big_endian
>
7860 Target_aarch64
<size
, big_endian
>::relocate_section(
7861 const Relocate_info
<size
, big_endian
>* relinfo
,
7862 unsigned int sh_type
,
7863 const unsigned char* prelocs
,
7865 Output_section
* output_section
,
7866 bool needs_special_offset_handling
,
7867 unsigned char* view
,
7868 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
7869 section_size_type view_size
,
7870 const Reloc_symbol_changes
* reloc_symbol_changes
)
7872 gold_assert(sh_type
== elfcpp::SHT_RELA
);
7873 typedef typename Target_aarch64
<size
, big_endian
>::Relocate AArch64_relocate
;
7874 gold::relocate_section
<size
, big_endian
, Target_aarch64
, elfcpp::SHT_RELA
,
7875 AArch64_relocate
, gold::Default_comdat_behavior
>(
7881 needs_special_offset_handling
,
7885 reloc_symbol_changes
);
7888 // Return the size of a relocation while scanning during a relocatable
7891 template<int size
, bool big_endian
>
7893 Target_aarch64
<size
, big_endian
>::Relocatable_size_for_reloc::
7898 // We will never support SHT_REL relocations.
7903 // Scan the relocs during a relocatable link.
7905 template<int size
, bool big_endian
>
7907 Target_aarch64
<size
, big_endian
>::scan_relocatable_relocs(
7908 Symbol_table
* symtab
,
7910 Sized_relobj_file
<size
, big_endian
>* object
,
7911 unsigned int data_shndx
,
7912 unsigned int sh_type
,
7913 const unsigned char* prelocs
,
7915 Output_section
* output_section
,
7916 bool needs_special_offset_handling
,
7917 size_t local_symbol_count
,
7918 const unsigned char* plocal_symbols
,
7919 Relocatable_relocs
* rr
)
7921 gold_assert(sh_type
== elfcpp::SHT_RELA
);
7923 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
7924 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
7926 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
7927 Scan_relocatable_relocs
>(
7935 needs_special_offset_handling
,
7941 // Relocate a section during a relocatable link.
7943 template<int size
, bool big_endian
>
7945 Target_aarch64
<size
, big_endian
>::relocate_relocs(
7946 const Relocate_info
<size
, big_endian
>* relinfo
,
7947 unsigned int sh_type
,
7948 const unsigned char* prelocs
,
7950 Output_section
* output_section
,
7951 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
7952 const Relocatable_relocs
* rr
,
7953 unsigned char* view
,
7954 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
7955 section_size_type view_size
,
7956 unsigned char* reloc_view
,
7957 section_size_type reloc_view_size
)
7959 gold_assert(sh_type
== elfcpp::SHT_RELA
);
7961 gold::relocate_relocs
<size
, big_endian
, elfcpp::SHT_RELA
>(
7966 offset_in_output_section
,
7976 // Return whether this is a 3-insn erratum sequence.
7978 template<int size
, bool big_endian
>
7980 Target_aarch64
<size
, big_endian
>::is_erratum_843419_sequence(
7981 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn1
,
7982 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn2
,
7983 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn3
)
7988 // The 2nd insn is a single register load or store; or register pair
7990 if (Insn_utilities::aarch64_mem_op_p(insn2
, &rt1
, &rt2
, &pair
, &load
)
7991 && (!pair
|| (pair
&& !load
)))
7993 // The 3rd insn is a load or store instruction from the "Load/store
7994 // register (unsigned immediate)" encoding class, using Rn as the
7995 // base address register.
7996 if (Insn_utilities::aarch64_ldst_uimm(insn3
)
7997 && (Insn_utilities::aarch64_rn(insn3
)
7998 == Insn_utilities::aarch64_rd(insn1
)))
8005 // Return whether this is a 835769 sequence.
8006 // (Similarly implemented as in elfnn-aarch64.c.)
8008 template<int size
, bool big_endian
>
8010 Target_aarch64
<size
, big_endian
>::is_erratum_835769_sequence(
8011 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn1
,
8012 typename
elfcpp::Swap
<32,big_endian
>::Valtype insn2
)
8022 if (Insn_utilities::aarch64_mlxl(insn2
)
8023 && Insn_utilities::aarch64_mem_op_p (insn1
, &rt
, &rt2
, &pair
, &load
))
8025 /* Any SIMD memory op is independent of the subsequent MLA
8026 by definition of the erratum. */
8027 if (Insn_utilities::aarch64_bit(insn1
, 26))
8030 /* If not SIMD, check for integer memory ops and MLA relationship. */
8031 rn
= Insn_utilities::aarch64_rn(insn2
);
8032 ra
= Insn_utilities::aarch64_ra(insn2
);
8033 rm
= Insn_utilities::aarch64_rm(insn2
);
8035 /* If this is a load and there's a true(RAW) dependency, we are safe
8036 and this is not an erratum sequence. */
8038 (rt
== rn
|| rt
== rm
|| rt
== ra
8039 || (pair
&& (rt2
== rn
|| rt2
== rm
|| rt2
== ra
))))
8042 /* We conservatively put out stubs for all other cases (including
8051 // Helper method to create erratum stub for ST_E_843419 and ST_E_835769.
8053 template<int size
, bool big_endian
>
8055 Target_aarch64
<size
, big_endian
>::create_erratum_stub(
8056 AArch64_relobj
<size
, big_endian
>* relobj
,
8058 section_size_type erratum_insn_offset
,
8059 Address erratum_address
,
8060 typename
Insn_utilities::Insntype erratum_insn
,
8062 unsigned int e843419_adrp_offset
)
8064 gold_assert(erratum_type
== ST_E_843419
|| erratum_type
== ST_E_835769
);
8065 The_stub_table
* stub_table
= relobj
->stub_table(shndx
);
8066 gold_assert(stub_table
!= NULL
);
8067 if (stub_table
->find_erratum_stub(relobj
,
8069 erratum_insn_offset
) == NULL
)
8071 const int BPI
= AArch64_insn_utilities
<big_endian
>::BYTES_PER_INSN
;
8072 The_erratum_stub
* stub
;
8073 if (erratum_type
== ST_E_835769
)
8074 stub
= new The_erratum_stub(relobj
, erratum_type
, shndx
,
8075 erratum_insn_offset
);
8076 else if (erratum_type
== ST_E_843419
)
8077 stub
= new E843419_stub
<size
, big_endian
>(
8078 relobj
, shndx
, erratum_insn_offset
, e843419_adrp_offset
);
8081 stub
->set_erratum_insn(erratum_insn
);
8082 stub
->set_erratum_address(erratum_address
);
8083 // For erratum ST_E_843419 and ST_E_835769, the destination address is
8084 // always the next insn after erratum insn.
8085 stub
->set_destination_address(erratum_address
+ BPI
);
8086 stub_table
->add_erratum_stub(stub
);
8091 // Scan erratum for section SHNDX range [output_address + span_start,
8092 // output_address + span_end). Note here we do not share the code with
8093 // scan_erratum_843419_span function, because for 843419 we optimize by only
8094 // scanning the last few insns of a page, whereas for 835769, we need to scan
8097 template<int size
, bool big_endian
>
8099 Target_aarch64
<size
, big_endian
>::scan_erratum_835769_span(
8100 AArch64_relobj
<size
, big_endian
>* relobj
,
8102 const section_size_type span_start
,
8103 const section_size_type span_end
,
8104 unsigned char* input_view
,
8105 Address output_address
)
8107 typedef typename
Insn_utilities::Insntype Insntype
;
8109 const int BPI
= AArch64_insn_utilities
<big_endian
>::BYTES_PER_INSN
;
8111 // Adjust output_address and view to the start of span.
8112 output_address
+= span_start
;
8113 input_view
+= span_start
;
8115 section_size_type span_length
= span_end
- span_start
;
8116 section_size_type offset
= 0;
8117 for (offset
= 0; offset
+ BPI
< span_length
; offset
+= BPI
)
8119 Insntype
* ip
= reinterpret_cast<Insntype
*>(input_view
+ offset
);
8120 Insntype insn1
= ip
[0];
8121 Insntype insn2
= ip
[1];
8122 if (is_erratum_835769_sequence(insn1
, insn2
))
8124 Insntype erratum_insn
= insn2
;
8125 // "span_start + offset" is the offset for insn1. So for insn2, it is
8126 // "span_start + offset + BPI".
8127 section_size_type erratum_insn_offset
= span_start
+ offset
+ BPI
;
8128 Address erratum_address
= output_address
+ offset
+ BPI
;
8129 gold_info(_("Erratum 835769 found and fixed at \"%s\", "
8130 "section %d, offset 0x%08x."),
8131 relobj
->name().c_str(), shndx
,
8132 (unsigned int)(span_start
+ offset
));
8134 this->create_erratum_stub(relobj
, shndx
,
8135 erratum_insn_offset
, erratum_address
,
8136 erratum_insn
, ST_E_835769
);
8137 offset
+= BPI
; // Skip mac insn.
8140 } // End of "Target_aarch64::scan_erratum_835769_span".
8143 // Scan erratum for section SHNDX range
8144 // [output_address + span_start, output_address + span_end).
8146 template<int size
, bool big_endian
>
8148 Target_aarch64
<size
, big_endian
>::scan_erratum_843419_span(
8149 AArch64_relobj
<size
, big_endian
>* relobj
,
8151 const section_size_type span_start
,
8152 const section_size_type span_end
,
8153 unsigned char* input_view
,
8154 Address output_address
)
8156 typedef typename
Insn_utilities::Insntype Insntype
;
8158 // Adjust output_address and view to the start of span.
8159 output_address
+= span_start
;
8160 input_view
+= span_start
;
8162 if ((output_address
& 0x03) != 0)
8165 section_size_type offset
= 0;
8166 section_size_type span_length
= span_end
- span_start
;
8167 // The first instruction must be ending at 0xFF8 or 0xFFC.
8168 unsigned int page_offset
= output_address
& 0xFFF;
8169 // Make sure starting position, that is "output_address+offset",
8170 // starts at page position 0xff8 or 0xffc.
8171 if (page_offset
< 0xff8)
8172 offset
= 0xff8 - page_offset
;
8173 while (offset
+ 3 * Insn_utilities::BYTES_PER_INSN
<= span_length
)
8175 Insntype
* ip
= reinterpret_cast<Insntype
*>(input_view
+ offset
);
8176 Insntype insn1
= ip
[0];
8177 if (Insn_utilities::is_adrp(insn1
))
8179 Insntype insn2
= ip
[1];
8180 Insntype insn3
= ip
[2];
8181 Insntype erratum_insn
;
8182 unsigned insn_offset
;
8183 bool do_report
= false;
8184 if (is_erratum_843419_sequence(insn1
, insn2
, insn3
))
8187 erratum_insn
= insn3
;
8188 insn_offset
= 2 * Insn_utilities::BYTES_PER_INSN
;
8190 else if (offset
+ 4 * Insn_utilities::BYTES_PER_INSN
<= span_length
)
8192 // Optionally we can have an insn between ins2 and ins3
8193 Insntype insn_opt
= ip
[2];
8194 // And insn_opt must not be a branch.
8195 if (!Insn_utilities::aarch64_b(insn_opt
)
8196 && !Insn_utilities::aarch64_bl(insn_opt
)
8197 && !Insn_utilities::aarch64_blr(insn_opt
)
8198 && !Insn_utilities::aarch64_br(insn_opt
))
8200 // And insn_opt must not write to dest reg in insn1. However
8201 // we do a conservative scan, which means we may fix/report
8202 // more than necessary, but it doesn't hurt.
8204 Insntype insn4
= ip
[3];
8205 if (is_erratum_843419_sequence(insn1
, insn2
, insn4
))
8208 erratum_insn
= insn4
;
8209 insn_offset
= 3 * Insn_utilities::BYTES_PER_INSN
;
8215 gold_info(_("Erratum 843419 found and fixed at \"%s\", "
8216 "section %d, offset 0x%08x."),
8217 relobj
->name().c_str(), shndx
,
8218 (unsigned int)(span_start
+ offset
));
8219 unsigned int erratum_insn_offset
=
8220 span_start
+ offset
+ insn_offset
;
8221 Address erratum_address
=
8222 output_address
+ offset
+ insn_offset
;
8223 create_erratum_stub(relobj
, shndx
,
8224 erratum_insn_offset
, erratum_address
,
8225 erratum_insn
, ST_E_843419
,
8226 span_start
+ offset
);
8230 // Advance to next candidate instruction. We only consider instruction
8231 // sequences starting at a page offset of 0xff8 or 0xffc.
8232 page_offset
= (output_address
+ offset
) & 0xfff;
8233 if (page_offset
== 0xff8)
8235 else // (page_offset == 0xffc), we move to next page's 0xff8.
8238 } // End of "Target_aarch64::scan_erratum_843419_span".
8241 // The selector for aarch64 object files.
8243 template<int size
, bool big_endian
>
8244 class Target_selector_aarch64
: public Target_selector
8247 Target_selector_aarch64();
8250 do_instantiate_target()
8251 { return new Target_aarch64
<size
, big_endian
>(); }
8255 Target_selector_aarch64
<32, true>::Target_selector_aarch64()
8256 : Target_selector(elfcpp::EM_AARCH64
, 32, true,
8257 "elf32-bigaarch64", "aarch64_elf32_be_vec")
8261 Target_selector_aarch64
<32, false>::Target_selector_aarch64()
8262 : Target_selector(elfcpp::EM_AARCH64
, 32, false,
8263 "elf32-littleaarch64", "aarch64_elf32_le_vec")
8267 Target_selector_aarch64
<64, true>::Target_selector_aarch64()
8268 : Target_selector(elfcpp::EM_AARCH64
, 64, true,
8269 "elf64-bigaarch64", "aarch64_elf64_be_vec")
8273 Target_selector_aarch64
<64, false>::Target_selector_aarch64()
8274 : Target_selector(elfcpp::EM_AARCH64
, 64, false,
8275 "elf64-littleaarch64", "aarch64_elf64_le_vec")
8278 Target_selector_aarch64
<32, true> target_selector_aarch64elf32b
;
8279 Target_selector_aarch64
<32, false> target_selector_aarch64elf32
;
8280 Target_selector_aarch64
<64, true> target_selector_aarch64elfb
;
8281 Target_selector_aarch64
<64, false> target_selector_aarch64elf
;
8283 } // End anonymous namespace.