1 /* x86 specific support for ELF
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 /* Don't generate unused section symbols. */
22 #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS false
30 #include "elf-linker-x86.h"
32 #include "elf/x86-64.h"
33 #include "sframe-api.h"
35 #define X86_64_PCREL_TYPE_P(TYPE) \
36 ((TYPE) == R_X86_64_PC8 \
37 || (TYPE) == R_X86_64_PC16 \
38 || (TYPE) == R_X86_64_PC32 \
39 || (TYPE) == R_X86_64_PC32_BND \
40 || (TYPE) == R_X86_64_PC64)
41 #define I386_PCREL_TYPE_P(TYPE) ((TYPE) == R_386_PC32)
42 #define X86_PCREL_TYPE_P(IS_X86_64, TYPE) \
43 ((IS_X86_64) ? X86_64_PCREL_TYPE_P (TYPE) : I386_PCREL_TYPE_P (TYPE))
45 #define X86_64_SIZE_TYPE_P(TYPE) \
46 ((TYPE) == R_X86_64_SIZE32 || (TYPE) == R_X86_64_SIZE64)
47 #define I386_SIZE_TYPE_P(TYPE) ((TYPE) == R_386_SIZE32)
48 #define X86_SIZE_TYPE_P(IS_X86_64, TYPE) \
49 ((IS_X86_64) ? X86_64_SIZE_TYPE_P(TYPE) : I386_SIZE_TYPE_P (TYPE))
51 #define X86_64_GOT_TYPE_P(TYPE) \
52 ((TYPE) == R_X86_64_GOTPCREL \
53 || (TYPE) == R_X86_64_GOTPCRELX \
54 || (TYPE) == R_X86_64_REX_GOTPCRELX \
55 || (TYPE) == R_X86_64_GOT32 \
56 || (TYPE) == R_X86_64_GOT64 \
57 || (TYPE) == R_X86_64_GOTPCREL64 \
58 || (TYPE) == R_X86_64_GOTPLT64)
59 #define I386_GOT_TYPE_P(TYPE) \
60 ((TYPE) == R_386_GOT32 || (TYPE) == R_386_GOT32X)
61 #define X86_GOT_TYPE_P(IS_X86_64, TYPE) \
62 ((IS_X86_64) ? X86_64_GOT_TYPE_P (TYPE) : I386_GOT_TYPE_P (TYPE))
64 #define X86_64_RELATIVE_RELOC_TYPE_P(TYPE) \
65 (X86_64_PCREL_TYPE_P (TYPE) \
66 || (TYPE) == R_X86_64_8 \
67 || (TYPE) == R_X86_64_16 \
68 || (TYPE) == R_X86_64_32 \
69 || (TYPE) == R_X86_64_32S \
70 || (TYPE) == R_X86_64_64)
71 #define I386_RELATIVE_RELOC_TYPE_P(TYPE) \
72 ((TYPE) == R_386_32 || (TYPE) == R_386_PC32)
73 #define X86_RELATIVE_RELOC_TYPE_P(IS_X86_64, TYPE) \
75 ? X86_64_RELATIVE_RELOC_TYPE_P (TYPE) \
76 : I386_RELATIVE_RELOC_TYPE_P(TYPE))
78 #define X86_64_NEED_DYNAMIC_RELOC_TYPE_P(TYPE) \
79 (X86_64_SIZE_TYPE_P (TYPE) \
80 || X86_64_RELATIVE_RELOC_TYPE_P (TYPE))
81 #define I386_NEED_DYNAMIC_RELOC_TYPE_P(TYPE) \
82 (I386_SIZE_TYPE_P (TYPE) \
83 || I386_RELATIVE_RELOC_TYPE_P (TYPE) \
84 || (TYPE) == R_386_TLS_IE \
85 || (TYPE) == R_386_TLS_LE \
86 || (TYPE) == R_386_TLS_LE_32)
87 #define X86_NEED_DYNAMIC_RELOC_TYPE_P(IS_X86_64, TYPE) \
89 ? X86_64_NEED_DYNAMIC_RELOC_TYPE_P (TYPE) \
90 : I386_NEED_DYNAMIC_RELOC_TYPE_P (TYPE))
92 #define X86_LOCAL_GOT_RELATIVE_RELOC_P(IS_X86_64, INFO, SYM) \
93 (bfd_link_pic (INFO) \
94 && (!(IS_X86_64) || ((SYM) != NULL && (SYM)->st_shndx != SHN_ABS)))
96 #define PLT_CIE_LENGTH 20
97 #define PLT_FDE_LENGTH 36
98 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
99 #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
101 #define I386_PCREL_TYPE_P(TYPE) ((TYPE) == R_386_PC32)
102 #define X86_64_PCREL_TYPE_P(TYPE) \
103 ((TYPE) == R_X86_64_PC8 \
104 || (TYPE) == R_X86_64_PC16 \
105 || (TYPE) == R_X86_64_PC32 \
106 || (TYPE) == R_X86_64_PC32_BND \
107 || (TYPE) == R_X86_64_PC64)
109 /* This must be the same as sframe_get_hdr_size (sfh). For x86-64, this value
110 is the same as sizeof (sframe_header) because there is no SFrame auxilliary
112 #define PLT_SFRAME_FDE_START_OFFSET sizeof (sframe_header)
114 #define ABI_64_P(abfd) \
115 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
117 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
118 copying dynamic variables from a shared lib into an app's dynbss
119 section, and instead use a dynamic relocation to point into the
121 #define ELIMINATE_COPY_RELOCS 1
123 #define elf_x86_hash_table(p, id) \
124 (is_elf_hash_table ((p)->hash) \
125 && elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) == (id) \
126 ? ((struct elf_x86_link_hash_table *) ((p)->hash)) : NULL)
128 /* Will references to this symbol always be local in this object? */
129 #define SYMBOL_REFERENCES_LOCAL_P(INFO, H) \
130 _bfd_x86_elf_link_symbol_references_local ((INFO), (H))
132 /* TRUE if an undefined weak symbol should be resolved to 0. Local
133 undefined weak symbol is always resolved to 0. Reference to an
134 undefined weak symbol is resolved to 0 in executable if undefined
135 weak symbol should be resolved to 0 (zero_undefweak > 0). */
136 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, EH) \
137 ((EH)->elf.root.type == bfd_link_hash_undefweak \
138 && (SYMBOL_REFERENCES_LOCAL_P ((INFO), &(EH)->elf) \
139 || (bfd_link_executable (INFO) \
140 && (EH)->zero_undefweak > 0)))
142 /* Should copy relocation be generated for a symbol. Don't generate
143 copy relocation against a protected symbol defined in a shared
145 #define SYMBOL_NO_COPYRELOC(INFO, EH) \
146 ((EH)->def_protected \
147 && ((EH)->elf.root.type == bfd_link_hash_defined \
148 || (EH)->elf.root.type == bfd_link_hash_defweak) \
149 && ((EH)->elf.root.u.def.section->owner->flags & DYNAMIC) != 0 \
150 && ((EH)->elf.root.u.def.section->flags & SEC_CODE) == 0)
152 /* TRUE if dynamic relocation is needed. If we are creating a shared
153 library, and this is a reloc against a global symbol, or a non PC
154 relative reloc against a local symbol, then we need to copy the reloc
155 into the shared library. However, if we are linking with -Bsymbolic,
156 we do not need to copy a reloc against a global symbol which is
157 defined in an object we are including in the link (i.e., DEF_REGULAR
160 If PCREL_PLT is true, don't generate dynamic relocation in PIE for
161 PC-relative relocation against a dynamic function definition in data
162 section when PLT address can be used.
164 If on the other hand, we are creating an executable, we may need to
165 keep relocations for symbols satisfied by a dynamic library if we
166 manage to avoid copy relocs for the symbol.
168 We also need to generate dynamic pointer relocation against
169 STT_GNU_IFUNC symbol in the non-code section. */
170 #define NEED_DYNAMIC_RELOCATION_P(IS_X86_64, INFO, PCREL_PLT, H, SEC, \
171 R_TYPE, POINTER_TYPE) \
172 ((bfd_link_pic (INFO) \
173 && (! X86_PCREL_TYPE_P (IS_X86_64, R_TYPE) \
175 && (! (bfd_link_pie (INFO) \
176 || SYMBOLIC_BIND ((INFO), (H))) \
177 || (H)->root.type == bfd_link_hash_defweak \
178 || (!(bfd_link_pie (INFO) \
180 && (H)->plt.refcount > 0 \
181 && ((SEC)->flags & SEC_CODE) == 0 \
182 && (H)->type == STT_FUNC \
183 && (H)->def_dynamic) \
184 && !(H)->def_regular))))) \
186 && (H)->type == STT_GNU_IFUNC \
187 && (R_TYPE) == POINTER_TYPE \
188 && ((SEC)->flags & SEC_CODE) == 0) \
189 || (ELIMINATE_COPY_RELOCS \
190 && !bfd_link_pic (INFO) \
192 && ((H)->root.type == bfd_link_hash_defweak \
193 || !(H)->def_regular)))
195 /* TRUE if dynamic relocation should be generated. Don't copy a
196 pc-relative relocation into the output file if the symbol needs
197 copy reloc or the symbol is undefined when building executable.
198 Copy dynamic function pointer relocations. Don't generate dynamic
199 relocations against resolved undefined weak symbols in PIE, except
200 when PC32_RELOC is TRUE. Undefined weak symbol is bound locally
201 when PIC is false. Don't generate dynamic relocations against
202 non-preemptible absolute symbol. NB: rel_from_abs is set on symbols
203 defined by linker scripts from "dot" (also SEGMENT_START or ORIGIN)
204 outside of an output section statement, which will be converted from
205 absolute to section-relative in set_sym_sections called from
206 ldexp_finalize_syms after ldemul_finish. */
207 #define GENERATE_DYNAMIC_RELOCATION_P(IS_X86_64, INFO, EH, R_TYPE, \
208 SEC, NEED_COPY_RELOC_IN_PIE, \
209 RESOLVED_TO_ZERO, PC32_RELOC) \
210 ((bfd_link_pic (INFO) \
211 && !(bfd_is_abs_section (SEC) \
213 || (EH)->elf.root.rel_from_abs == 0) \
215 || SYMBOL_REFERENCES_LOCAL (INFO, &(EH)->elf))) \
216 && !(NEED_COPY_RELOC_IN_PIE) \
218 || ((ELF_ST_VISIBILITY ((EH)->elf.other) == STV_DEFAULT \
219 && (!(RESOLVED_TO_ZERO) || PC32_RELOC)) \
220 || (EH)->elf.root.type != bfd_link_hash_undefweak)) \
221 && ((!X86_PCREL_TYPE_P (IS_X86_64, R_TYPE) \
222 && !X86_SIZE_TYPE_P (IS_X86_64, R_TYPE)) \
223 || ! SYMBOL_CALLS_LOCAL ((INFO), \
224 (struct elf_link_hash_entry *) (EH)))) \
225 || (ELIMINATE_COPY_RELOCS \
226 && !bfd_link_pic (INFO) \
228 && (EH)->elf.dynindx != -1 \
229 && (!(EH)->elf.non_got_ref \
230 || ((EH)->elf.root.type == bfd_link_hash_undefweak \
231 && !(RESOLVED_TO_ZERO))) \
232 && (((EH)->elf.def_dynamic && !(EH)->elf.def_regular) \
233 || (EH)->elf.root.type == bfd_link_hash_undefined)))
235 /* TRUE if this input relocation should be copied to output. H->dynindx
236 may be -1 if this symbol was marked to become local. */
237 #define COPY_INPUT_RELOC_P(IS_X86_64, INFO, H, R_TYPE) \
239 && (H)->dynindx != -1 \
240 && (X86_PCREL_TYPE_P (IS_X86_64, R_TYPE) \
241 || !(bfd_link_executable (INFO) || SYMBOLIC_BIND ((INFO), (H))) \
242 || !(H)->def_regular))
244 /* TRUE if this is actually a static link, or it is a -Bsymbolic link
245 and the symbol is defined locally, or the symbol was forced to be
246 local because of a version file. */
247 #define RESOLVED_LOCALLY_P(INFO, H, HTAB) \
248 (!WILL_CALL_FINISH_DYNAMIC_SYMBOL ((HTAB)->elf.dynamic_sections_created, \
249 bfd_link_pic (INFO), (H)) \
250 || (bfd_link_pic (INFO) \
251 && SYMBOL_REFERENCES_LOCAL_P ((INFO), (H))) \
252 || (ELF_ST_VISIBILITY ((H)->other) \
253 && (H)->root.type == bfd_link_hash_undefweak))
255 /* TRUE if this symbol isn't defined by a shared object. */
256 #define SYMBOL_DEFINED_NON_SHARED_P(H) \
258 || (H)->root.linker_def \
259 || (H)->root.ldscript_def \
260 || ((struct elf_x86_link_hash_entry *) (H))->linker_def \
261 || ELF_COMMON_DEF_P (H))
263 /* Return TRUE if the symbol described by a linker hash entry H is
264 going to be absolute. Similar to bfd_is_abs_symbol, but excluding
265 all linker-script defined symbols. */
266 #define ABS_SYMBOL_P(H) \
267 (bfd_is_abs_symbol (&(H)->root) && !(H)->root.ldscript_def)
269 /* TRUE if relative relocation should be generated. GOT reference to
270 global symbol in PIC will lead to dynamic symbol. It becomes a
271 problem when "time" or "times" is defined as a variable in an
272 executable, clashing with functions of the same name in libc. If a
273 symbol isn't undefined weak symbol, don't make it dynamic in PIC and
274 generate relative relocation. Don't generate relative relocation
275 against non-preemptible absolute symbol. */
276 #define GENERATE_RELATIVE_RELOC_P(INFO, H) \
277 ((H)->dynindx == -1 \
278 && !(H)->forced_local \
279 && (H)->root.type != bfd_link_hash_undefweak \
280 && bfd_link_pic (INFO) \
281 && !ABS_SYMBOL_P (H))
283 /* TRUE if this is a pointer reference to a local IFUNC. */
284 #define POINTER_LOCAL_IFUNC_P(INFO, H) \
285 ((H)->dynindx == -1 \
286 || (H)->forced_local \
287 || bfd_link_executable (INFO))
289 /* TRUE if this is a PLT reference to a local IFUNC. */
290 #define PLT_LOCAL_IFUNC_P(INFO, H) \
291 ((H)->dynindx == -1 \
292 || ((bfd_link_executable (INFO) \
293 || ELF_ST_VISIBILITY ((H)->other) != STV_DEFAULT) \
294 && (H)->def_regular \
295 && (H)->type == STT_GNU_IFUNC))
297 /* TRUE if TLS IE->LE transition is OK. */
298 #define TLS_TRANSITION_IE_TO_LE_P(INFO, H, TLS_TYPE) \
299 (bfd_link_executable (INFO) \
301 && (H)->dynindx == -1 \
302 && (TLS_TYPE & GOT_TLS_IE))
304 /* Verify that the symbol has an entry in the procedure linkage table. */
305 #define VERIFY_PLT_ENTRY(INFO, H, PLT, GOTPLT, RELPLT, LOCAL_UNDEFWEAK) \
308 if (((H)->dynindx == -1 \
309 && !LOCAL_UNDEFWEAK \
310 && !(((H)->forced_local || bfd_link_executable (INFO)) \
311 && (H)->def_regular \
312 && (H)->type == STT_GNU_IFUNC)) \
314 || (GOTPLT) == NULL \
315 || (RELPLT) == NULL) \
320 /* Verify that the symbol supports copy relocation. */
321 #define VERIFY_COPY_RELOC(H, HTAB) \
324 if ((H)->dynindx == -1 \
325 || ((H)->root.type != bfd_link_hash_defined \
326 && (H)->root.type != bfd_link_hash_defweak) \
327 || (HTAB)->elf.srelbss == NULL \
328 || (HTAB)->elf.sreldynrelro == NULL) \
333 /* x86 ELF linker hash entry. */
335 struct elf_x86_link_hash_entry
337 struct elf_link_hash_entry elf
;
339 unsigned char tls_type
;
341 /* Bit 0: Symbol has no GOT nor PLT relocations.
342 Bit 1: Symbol has non-GOT/non-PLT relocations in text sections.
343 zero_undefweak is initialized to 1 and undefined weak symbol
344 should be resolved to 0 if zero_undefweak > 0. */
345 unsigned int zero_undefweak
: 2;
347 /* Don't call finish_dynamic_symbol on this symbol. */
348 unsigned int no_finish_dynamic_symbol
: 1;
350 /* R_*_RELATIVE relocation in GOT for this symbol has been
352 unsigned int got_relative_reloc_done
: 1;
354 /* TRUE if symbol is __tls_get_addr. */
355 unsigned int tls_get_addr
: 1;
357 /* TRUE if symbol is defined as a protected symbol. */
358 unsigned int def_protected
: 1;
360 /* 0: Symbol references are unknown.
361 1: Symbol references aren't local.
362 2: Symbol references are local.
364 unsigned int local_ref
: 2;
366 /* TRUE if symbol is defined by linker. */
367 unsigned int linker_def
: 1;
369 /* TRUE if symbol is referenced by a non-GOT/non-PLT relocation in a
370 relocatable object file without indirect external access marker. */
371 unsigned int non_got_ref_without_indirect_extern_access
: 1;
373 /* TRUE if symbol is referenced by R_386_GOTOFF relocation. This is
374 only used by i386. */
375 unsigned int gotoff_ref
: 1;
377 /* TRUE if a weak symbol with a real definition needs a copy reloc.
378 When there is a weak symbol with a real definition, the processor
379 independent code will have arranged for us to see the real
380 definition first. We need to copy the needs_copy bit from the
381 real definition and check it when allowing copy reloc in PIE. This
382 is only used by x86-64. */
383 unsigned int needs_copy
: 1;
385 /* Information about the GOT PLT entry. Filled when there are both
386 GOT and PLT relocations against the same function. */
387 union gotplt_union plt_got
;
389 /* Information about the second PLT entry. */
390 union gotplt_union plt_second
;
392 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
393 starting at the end of the jump table. */
397 #define SFRAME_PLT0_MAX_NUM_FRES 2
398 #define SFRAME_PLTN_MAX_NUM_FRES 2
400 struct elf_x86_sframe_plt
402 unsigned int plt0_entry_size
;
403 unsigned int plt0_num_fres
;
404 const sframe_frame_row_entry
*plt0_fres
[SFRAME_PLT0_MAX_NUM_FRES
];
406 unsigned int pltn_entry_size
;
407 unsigned int pltn_num_fres
;
408 const sframe_frame_row_entry
*pltn_fres
[SFRAME_PLTN_MAX_NUM_FRES
];
410 unsigned int sec_pltn_entry_size
;
411 unsigned int sec_pltn_num_fres
;
412 const sframe_frame_row_entry
*sec_pltn_fres
[SFRAME_PLTN_MAX_NUM_FRES
];
415 struct elf_x86_lazy_plt_layout
417 /* The first entry in a lazy procedure linkage table looks like this. */
418 const bfd_byte
*plt0_entry
;
419 unsigned int plt0_entry_size
; /* Size of PLT0 entry. */
421 /* Later entries in a lazy procedure linkage table look like this. */
422 const bfd_byte
*plt_entry
;
423 unsigned int plt_entry_size
; /* Size of each PLT entry. */
425 /* The TLSDESC entry in a lazy procedure linkage table looks like
426 this. This is for x86-64 only. */
427 const bfd_byte
*plt_tlsdesc_entry
;
428 unsigned int plt_tlsdesc_entry_size
; /* Size of TLSDESC entry. */
430 /* Offsets into the TLSDESC entry that are to be replaced with
431 GOT+8 and GOT+TDG. These are for x86-64 only. */
432 unsigned int plt_tlsdesc_got1_offset
;
433 unsigned int plt_tlsdesc_got2_offset
;
435 /* Offset of the end of the PC-relative instructions containing
436 plt_tlsdesc_got1_offset and plt_tlsdesc_got2_offset. These
437 are for x86-64 only. */
438 unsigned int plt_tlsdesc_got1_insn_end
;
439 unsigned int plt_tlsdesc_got2_insn_end
;
441 /* Offsets into plt0_entry that are to be replaced with GOT[1] and
443 unsigned int plt0_got1_offset
;
444 unsigned int plt0_got2_offset
;
446 /* Offset of the end of the PC-relative instruction containing
447 plt0_got2_offset. This is for x86-64 only. */
448 unsigned int plt0_got2_insn_end
;
450 /* Offsets into plt_entry that are to be replaced with... */
451 unsigned int plt_got_offset
; /* ... address of this symbol in .got. */
452 unsigned int plt_reloc_offset
; /* ... offset into relocation table. */
453 unsigned int plt_plt_offset
; /* ... offset to start of .plt. */
455 /* Length of the PC-relative instruction containing plt_got_offset.
456 This is used for x86-64 only. */
457 unsigned int plt_got_insn_size
;
459 /* Offset of the end of the PC-relative jump to plt0_entry. This is
460 used for x86-64 only. */
461 unsigned int plt_plt_insn_end
;
463 /* Offset into plt_entry where the initial value of the GOT entry
465 unsigned int plt_lazy_offset
;
467 /* The first entry in a PIC lazy procedure linkage table looks like
469 const bfd_byte
*pic_plt0_entry
;
471 /* Subsequent entries in a PIC lazy procedure linkage table look
473 const bfd_byte
*pic_plt_entry
;
475 /* .eh_frame covering the lazy .plt section. */
476 const bfd_byte
*eh_frame_plt
;
477 unsigned int eh_frame_plt_size
;
480 struct elf_x86_non_lazy_plt_layout
482 /* Entries in a non-lazy procedure linkage table look like this. */
483 const bfd_byte
*plt_entry
;
484 /* Entries in a PIC non-lazy procedure linkage table look like this.
485 This is only used for i386 where absolute PLT and PIC PLT are
487 const bfd_byte
*pic_plt_entry
;
489 unsigned int plt_entry_size
; /* Size of each PLT entry. */
491 /* Offsets into plt_entry that are to be replaced with... */
492 unsigned int plt_got_offset
; /* ... address of this symbol in .got. */
494 /* Length of the PC-relative instruction containing plt_got_offset.
495 This is used for x86-64 only. */
496 unsigned int plt_got_insn_size
;
498 /* .eh_frame covering the non-lazy .plt section. */
499 const bfd_byte
*eh_frame_plt
;
500 unsigned int eh_frame_plt_size
;
503 struct elf_x86_plt_layout
505 /* The first entry in a lazy procedure linkage table looks like this. */
506 const bfd_byte
*plt0_entry
;
507 /* Entries in a procedure linkage table look like this. */
508 const bfd_byte
*plt_entry
;
509 unsigned int plt_entry_size
; /* Size of each PLT entry. */
512 unsigned int has_plt0
;
514 /* Offsets into plt_entry that are to be replaced with... */
515 unsigned int plt_got_offset
; /* ... address of this symbol in .got. */
517 /* Length of the PC-relative instruction containing plt_got_offset.
518 This is only used for x86-64. */
519 unsigned int plt_got_insn_size
;
521 /* Alignment of the .iplt section. */
522 unsigned int iplt_alignment
;
524 /* .eh_frame covering the .plt section. */
525 const bfd_byte
*eh_frame_plt
;
526 unsigned int eh_frame_plt_size
;
529 /* Values in tls_type of x86 ELF linker hash entry. */
530 #define GOT_UNKNOWN 0
534 #define GOT_TLS_IE_POS 5
535 #define GOT_TLS_IE_NEG 6
536 #define GOT_TLS_IE_BOTH 7
537 #define GOT_TLS_GDESC 8
539 #define GOT_TLS_GD_BOTH_P(type) \
540 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
541 #define GOT_TLS_GD_P(type) \
542 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
543 #define GOT_TLS_GDESC_P(type) \
544 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
545 #define GOT_TLS_GD_ANY_P(type) \
546 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
548 #define elf_x86_hash_entry(ent) \
549 ((struct elf_x86_link_hash_entry *)(ent))
551 /* Information of an input relocation used to compute its contribution
552 to the DT_RELR section size. */
554 struct elf_x86_relative_reloc_record
556 /* The original relocation info. */
557 Elf_Internal_Rela rel
;
558 /* The input or the GOT section where the relocation is applied. */
560 /* Local symbol info. NULL for global symbol. */
561 Elf_Internal_Sym
*sym
;
564 /* Section where the local symbol is defined. */
566 /* Global symbol hash. */
567 struct elf_link_hash_entry
*h
;
569 /* The offset into the output section where the relative relocation
570 will be applied at run-time. */
572 /* The run-time address. */
576 struct elf_x86_relative_reloc_data
580 struct elf_x86_relative_reloc_record
*data
;
583 /* DT_RELR bitmap. */
584 struct elf_dt_relr_bitmap
597 /* x86 ELF linker hash table. */
599 struct elf_x86_link_hash_table
601 struct elf_link_hash_table elf
;
603 /* Short-cuts to get to dynamic linker sections. */
605 asection
*plt_eh_frame
;
606 asection
*plt_second
;
607 asection
*plt_second_eh_frame
;
609 asection
*plt_got_eh_frame
;
611 sframe_encoder_ctx
*plt_cfe_ctx
;
612 asection
*plt_sframe
;
613 sframe_encoder_ctx
*plt_second_cfe_ctx
;
614 asection
*plt_second_sframe
;
616 /* Parameters describing PLT generation, lazy or non-lazy. */
617 struct elf_x86_plt_layout plt
;
619 /* Parameters describing lazy PLT generation. */
620 const struct elf_x86_lazy_plt_layout
*lazy_plt
;
622 /* Parameters describing non-lazy PLT generation. */
623 const struct elf_x86_non_lazy_plt_layout
*non_lazy_plt
;
625 /* The .sframe helper object for .plt section.
626 This is used for x86-64 only. */
627 const struct elf_x86_sframe_plt
*sframe_plt
;
631 bfd_signed_vma refcount
;
635 /* The amount of space used by the jump slots in the GOT. */
636 bfd_vma sgotplt_jump_table_size
;
638 /* _TLS_MODULE_BASE_ symbol. */
639 struct bfd_link_hash_entry
*tls_module_base
;
641 /* Used by local STT_GNU_IFUNC symbols. */
642 htab_t loc_hash_table
;
643 void * loc_hash_memory
;
645 /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
646 bfd_vma next_jump_slot_index
;
647 /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
648 bfd_vma next_irelative_index
;
650 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.
651 This is used for i386 only. */
654 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. This
655 is only used for i386. */
656 bfd_vma next_tls_desc_index
;
658 /* DT_RELR bitmap. */
659 struct elf_dt_relr_bitmap dt_relr_bitmap
;
661 /* Relative relocation data. */
662 struct elf_x86_relative_reloc_data relative_reloc
;
664 /* Unaligned relative relocation data. */
665 struct elf_x86_relative_reloc_data unaligned_relative_reloc
;
667 /* Number of relative reloc generation pass. */
668 unsigned int generate_relative_reloc_pass
;
670 /* Value used to fill the unused bytes of the first PLT entry. This
671 is only used for i386. */
672 bfd_byte plt0_pad_byte
;
674 /* TRUE if GOT is referenced. */
675 unsigned int got_referenced
: 1;
677 /* TRUE if PLT is PC-relative. PLT in PDE and PC-relative PLT in PIE
678 can be used as function address.
680 NB: i386 has non-PIC PLT and PIC PLT. Only non-PIC PLT in PDE can
681 be used as function address. PIC PLT in PIE can't be used as
683 unsigned int pcrel_plt
: 1;
685 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
686 bfd_vma (*r_sym
) (bfd_vma
);
687 bool (*is_reloc_section
) (const char *);
688 unsigned int sizeof_reloc
;
689 unsigned int got_entry_size
;
690 unsigned int pointer_r_type
;
691 unsigned int relative_r_type
;
692 int dynamic_interpreter_size
;
693 const char *dynamic_interpreter
;
694 const char *tls_get_addr
;
695 const char *relative_r_name
;
696 void (*elf_append_reloc
) (bfd
*, asection
*, Elf_Internal_Rela
*);
697 void (*elf_write_addend
) (bfd
*, uint64_t, void *);
698 void (*elf_write_addend_in_got
) (bfd
*, uint64_t, void *);
700 /* Options passed from the linker. */
701 struct elf_linker_x86_params
*params
;
704 struct elf_x86_init_table
706 /* The lazy PLT layout. */
707 const struct elf_x86_lazy_plt_layout
*lazy_plt
;
709 /* The non-lazy PLT layout. */
710 const struct elf_x86_non_lazy_plt_layout
*non_lazy_plt
;
712 /* The lazy PLT layout for IBT. */
713 const struct elf_x86_lazy_plt_layout
*lazy_ibt_plt
;
715 /* The non-lazy PLT layout for IBT. */
716 const struct elf_x86_non_lazy_plt_layout
*non_lazy_ibt_plt
;
718 /* The .sframe helper object for lazy .plt section.
719 This is used for x86-64 only. */
720 const struct elf_x86_sframe_plt
*sframe_lazy_plt
;
722 /* The .sframe helper object for non-lazy .plt section.
723 This is used for x86-64 only. */
724 const struct elf_x86_sframe_plt
*sframe_non_lazy_plt
;
726 /* The .sframe helper object for lazy IBT .plt section.
727 This is used for x86-64 only. */
728 const struct elf_x86_sframe_plt
*sframe_lazy_ibt_plt
;
730 /* The .sframe helper object for non-lazy IBT .plt section.
731 This is used for x86-64 only. */
732 const struct elf_x86_sframe_plt
*sframe_non_lazy_ibt_plt
;
734 bfd_byte plt0_pad_byte
;
736 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
737 bfd_vma (*r_sym
) (bfd_vma
);
740 struct elf_x86_obj_tdata
742 struct elf_obj_tdata root
;
744 /* tls_type for each local got entry. */
745 char *local_got_tls_type
;
747 /* GOTPLT entries for TLS descriptors. */
748 bfd_vma
*local_tlsdesc_gotent
;
750 /* R_*_RELATIVE relocation in GOT for this local symbol has been
752 char *relative_reloc_done
;
755 enum elf_x86_plt_type
769 enum elf_x86_plt_type type
;
770 unsigned int plt_got_offset
;
771 unsigned int plt_entry_size
;
772 unsigned int plt_got_insn_size
; /* Only used for x86-64. */
776 /* Set if a relocation is converted from a GOTPCREL relocation. */
777 #define R_X86_64_converted_reloc_bit (1 << 7)
779 #define elf_x86_tdata(abfd) \
780 ((struct elf_x86_obj_tdata *) (abfd)->tdata.any)
782 #define elf_x86_local_got_tls_type(abfd) \
783 (elf_x86_tdata (abfd)->local_got_tls_type)
785 #define elf_x86_local_tlsdesc_gotent(abfd) \
786 (elf_x86_tdata (abfd)->local_tlsdesc_gotent)
788 #define elf_x86_relative_reloc_done(abfd) \
789 (elf_x86_tdata (abfd)->relative_reloc_done)
791 #define elf_x86_compute_jump_table_size(htab) \
792 ((htab)->elf.srelplt->reloc_count * (htab)->got_entry_size)
794 #define is_x86_elf(bfd, htab) \
795 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
796 && elf_tdata (bfd) != NULL \
797 && elf_object_id (bfd) == (htab)->elf.hash_table_id)
799 /* Rename some of the generic section flags to better document how they
801 #define check_relocs_failed sec_flg0
802 #define relative_reloc_packed sec_flg1
804 extern bool _bfd_x86_elf_mkobject
807 extern void _bfd_x86_elf_set_tls_module_base
808 (struct bfd_link_info
*);
810 extern bfd_vma _bfd_x86_elf_dtpoff_base
811 (struct bfd_link_info
*);
813 extern bool _bfd_x86_elf_readonly_dynrelocs
814 (struct elf_link_hash_entry
*, void *);
816 extern struct elf_link_hash_entry
* _bfd_elf_x86_get_local_sym_hash
817 (struct elf_x86_link_hash_table
*, bfd
*, const Elf_Internal_Rela
*,
820 extern hashval_t _bfd_x86_elf_local_htab_hash
823 extern int _bfd_x86_elf_local_htab_eq
824 (const void *, const void *);
826 extern struct bfd_hash_entry
* _bfd_x86_elf_link_hash_newfunc
827 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
829 extern struct bfd_link_hash_table
* _bfd_x86_elf_link_hash_table_create
832 extern int _bfd_x86_elf_compare_relocs
833 (const void *, const void *);
835 extern bool _bfd_x86_elf_link_check_relocs
836 (bfd
*, struct bfd_link_info
*);
838 extern bool _bfd_x86_elf_check_relocs
839 (bfd
*, struct bfd_link_info
*, asection
*,
840 const Elf_Internal_Rela
*);
842 extern bool _bfd_x86_elf_link_relax_section
843 (bfd
*, asection
*, struct bfd_link_info
*, bool *);
845 extern bool _bfd_elf_x86_size_relative_relocs
846 (struct bfd_link_info
*, bool *);
848 extern bool _bfd_elf_x86_finish_relative_relocs
849 (struct bfd_link_info
*);
851 extern void _bfd_elf32_write_addend (bfd
*, uint64_t, void *);
852 extern void _bfd_elf64_write_addend (bfd
*, uint64_t, void *);
854 extern bool _bfd_elf_x86_valid_reloc_p
855 (asection
*, struct bfd_link_info
*, struct elf_x86_link_hash_table
*,
856 const Elf_Internal_Rela
*, struct elf_link_hash_entry
*,
857 Elf_Internal_Sym
*, Elf_Internal_Shdr
*, bool *);
859 extern bool _bfd_x86_elf_size_dynamic_sections
860 (bfd
*, struct bfd_link_info
*);
862 extern struct elf_x86_link_hash_table
*_bfd_x86_elf_finish_dynamic_sections
863 (bfd
*, struct bfd_link_info
*);
865 extern bool _bfd_x86_elf_always_size_sections
866 (bfd
*, struct bfd_link_info
*);
868 extern void _bfd_x86_elf_merge_symbol_attribute
869 (struct elf_link_hash_entry
*, unsigned int, bool, bool);
871 extern void _bfd_x86_elf_copy_indirect_symbol
872 (struct bfd_link_info
*, struct elf_link_hash_entry
*,
873 struct elf_link_hash_entry
*);
875 extern bool _bfd_x86_elf_fixup_symbol
876 (struct bfd_link_info
*, struct elf_link_hash_entry
*);
878 extern bool _bfd_x86_elf_hash_symbol
879 (struct elf_link_hash_entry
*);
881 extern bool _bfd_x86_elf_adjust_dynamic_symbol
882 (struct bfd_link_info
*, struct elf_link_hash_entry
*);
884 extern void _bfd_x86_elf_hide_symbol
885 (struct bfd_link_info
*, struct elf_link_hash_entry
*, bool);
887 extern bool _bfd_x86_elf_link_symbol_references_local
888 (struct bfd_link_info
*, struct elf_link_hash_entry
*);
890 extern asection
* _bfd_x86_elf_gc_mark_hook
891 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
892 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
894 extern long _bfd_x86_elf_get_synthetic_symtab
895 (bfd
*, long, long, bfd_vma
, struct elf_x86_plt
[], asymbol
**,
898 extern enum elf_property_kind _bfd_x86_elf_parse_gnu_properties
899 (bfd
*, unsigned int, bfd_byte
*, unsigned int);
901 extern bool _bfd_x86_elf_merge_gnu_properties
902 (struct bfd_link_info
*, bfd
*, bfd
*, elf_property
*, elf_property
*);
904 extern void _bfd_x86_elf_link_fixup_gnu_properties
905 (struct bfd_link_info
*, elf_property_list
**);
907 extern bfd
* _bfd_x86_elf_link_setup_gnu_properties
908 (struct bfd_link_info
*, struct elf_x86_init_table
*);
910 extern void _bfd_x86_elf_link_fixup_ifunc_symbol
911 (struct bfd_link_info
*, struct elf_x86_link_hash_table
*,
912 struct elf_link_hash_entry
*, Elf_Internal_Sym
*sym
);
914 extern void _bfd_x86_elf_link_report_relative_reloc
915 (struct bfd_link_info
*, asection
*, struct elf_link_hash_entry
*,
916 Elf_Internal_Sym
*, const char *, const void *);
918 #define bfd_elf64_mkobject \
919 _bfd_x86_elf_mkobject
920 #define bfd_elf32_mkobject \
921 _bfd_x86_elf_mkobject
922 #define bfd_elf64_bfd_link_hash_table_create \
923 _bfd_x86_elf_link_hash_table_create
924 #define bfd_elf32_bfd_link_hash_table_create \
925 _bfd_x86_elf_link_hash_table_create
926 #define bfd_elf64_bfd_link_check_relocs \
927 _bfd_x86_elf_link_check_relocs
928 #define bfd_elf32_bfd_link_check_relocs \
929 _bfd_x86_elf_link_check_relocs
930 #define bfd_elf32_bfd_relax_section \
931 _bfd_x86_elf_link_relax_section
932 #define bfd_elf64_bfd_relax_section \
933 _bfd_x86_elf_link_relax_section
935 #define elf_backend_check_relocs \
936 _bfd_x86_elf_check_relocs
937 #define elf_backend_size_dynamic_sections \
938 _bfd_x86_elf_size_dynamic_sections
939 #define elf_backend_merge_symbol_attribute \
940 _bfd_x86_elf_merge_symbol_attribute
941 #define elf_backend_copy_indirect_symbol \
942 _bfd_x86_elf_copy_indirect_symbol
943 #define elf_backend_fixup_symbol \
944 _bfd_x86_elf_fixup_symbol
945 #define elf_backend_hash_symbol \
946 _bfd_x86_elf_hash_symbol
947 #define elf_backend_adjust_dynamic_symbol \
948 _bfd_x86_elf_adjust_dynamic_symbol
949 #define elf_backend_gc_mark_hook \
950 _bfd_x86_elf_gc_mark_hook
951 #define elf_backend_omit_section_dynsym \
952 _bfd_elf_omit_section_dynsym_all
953 #define elf_backend_parse_gnu_properties \
954 _bfd_x86_elf_parse_gnu_properties
955 #define elf_backend_merge_gnu_properties \
956 _bfd_x86_elf_merge_gnu_properties
957 #define elf_backend_fixup_gnu_properties \
958 _bfd_x86_elf_link_fixup_gnu_properties
959 #define elf_backend_size_relative_relocs \
960 _bfd_elf_x86_size_relative_relocs
961 #define elf_backend_finish_relative_relocs \
962 _bfd_elf_x86_finish_relative_relocs
964 #define ELF_P_ALIGN ELF_MINPAGESIZE
966 /* Allocate x86 GOT info for local symbols. */
969 elf_x86_allocate_local_got_info (bfd
*abfd
, bfd_size_type count
)
971 bfd_signed_vma
*local_got_refcounts
= elf_local_got_refcounts (abfd
);
972 if (local_got_refcounts
== NULL
)
974 bfd_size_type size
= count
* (sizeof (bfd_signed_vma
)
977 local_got_refcounts
= (bfd_signed_vma
*) bfd_zalloc (abfd
, size
);
978 if (local_got_refcounts
== NULL
)
980 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
981 elf_x86_local_tlsdesc_gotent (abfd
) =
982 (bfd_vma
*) (local_got_refcounts
+ count
);
983 elf_x86_local_got_tls_type (abfd
) =
984 (char *) (local_got_refcounts
+ 2 * count
);
985 elf_x86_relative_reloc_done (abfd
) =
986 ((char *) (local_got_refcounts
+ 2 * count
)) + count
;