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_PC64)
40 #define I386_PCREL_TYPE_P(TYPE) ((TYPE) == R_386_PC32)
41 #define X86_PCREL_TYPE_P(IS_X86_64, TYPE) \
42 ((IS_X86_64) ? X86_64_PCREL_TYPE_P (TYPE) : I386_PCREL_TYPE_P (TYPE))
44 #define X86_64_SIZE_TYPE_P(TYPE) \
45 ((TYPE) == R_X86_64_SIZE32 || (TYPE) == R_X86_64_SIZE64)
46 #define I386_SIZE_TYPE_P(TYPE) ((TYPE) == R_386_SIZE32)
47 #define X86_SIZE_TYPE_P(IS_X86_64, TYPE) \
48 ((IS_X86_64) ? X86_64_SIZE_TYPE_P(TYPE) : I386_SIZE_TYPE_P (TYPE))
50 #define X86_64_GOT_TYPE_P(TYPE) \
51 ((TYPE) == R_X86_64_GOTPCREL \
52 || (TYPE) == R_X86_64_GOTPCRELX \
53 || (TYPE) == R_X86_64_REX_GOTPCRELX \
54 || (TYPE) == R_X86_64_GOT32 \
55 || (TYPE) == R_X86_64_GOT64 \
56 || (TYPE) == R_X86_64_GOTPCREL64 \
57 || (TYPE) == R_X86_64_GOTPLT64)
58 #define I386_GOT_TYPE_P(TYPE) \
59 ((TYPE) == R_386_GOT32 || (TYPE) == R_386_GOT32X)
60 #define X86_GOT_TYPE_P(IS_X86_64, TYPE) \
61 ((IS_X86_64) ? X86_64_GOT_TYPE_P (TYPE) : I386_GOT_TYPE_P (TYPE))
63 #define X86_64_RELATIVE_RELOC_TYPE_P(TYPE) \
64 (X86_64_PCREL_TYPE_P (TYPE) \
65 || (TYPE) == R_X86_64_8 \
66 || (TYPE) == R_X86_64_16 \
67 || (TYPE) == R_X86_64_32 \
68 || (TYPE) == R_X86_64_32S \
69 || (TYPE) == R_X86_64_64)
70 #define I386_RELATIVE_RELOC_TYPE_P(TYPE) \
71 ((TYPE) == R_386_32 || (TYPE) == R_386_PC32)
72 #define X86_RELATIVE_RELOC_TYPE_P(IS_X86_64, TYPE) \
74 ? X86_64_RELATIVE_RELOC_TYPE_P (TYPE) \
75 : I386_RELATIVE_RELOC_TYPE_P(TYPE))
77 #define X86_64_NEED_DYNAMIC_RELOC_TYPE_P(TYPE) \
78 (X86_64_SIZE_TYPE_P (TYPE) \
79 || X86_64_RELATIVE_RELOC_TYPE_P (TYPE))
80 #define I386_NEED_DYNAMIC_RELOC_TYPE_P(TYPE) \
81 (I386_SIZE_TYPE_P (TYPE) \
82 || I386_RELATIVE_RELOC_TYPE_P (TYPE) \
83 || (TYPE) == R_386_TLS_IE \
84 || (TYPE) == R_386_TLS_LE \
85 || (TYPE) == R_386_TLS_LE_32)
86 #define X86_NEED_DYNAMIC_RELOC_TYPE_P(IS_X86_64, TYPE) \
88 ? X86_64_NEED_DYNAMIC_RELOC_TYPE_P (TYPE) \
89 : I386_NEED_DYNAMIC_RELOC_TYPE_P (TYPE))
91 #define X86_LOCAL_GOT_RELATIVE_RELOC_P(IS_X86_64, INFO, SYM) \
92 (bfd_link_pic (INFO) \
93 && (!(IS_X86_64) || ((SYM) != NULL && (SYM)->st_shndx != SHN_ABS)))
95 #define PLT_CIE_LENGTH 20
96 #define PLT_FDE_LENGTH 36
97 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
98 #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
100 #define I386_PCREL_TYPE_P(TYPE) ((TYPE) == R_386_PC32)
101 #define X86_64_PCREL_TYPE_P(TYPE) \
102 ((TYPE) == R_X86_64_PC8 \
103 || (TYPE) == R_X86_64_PC16 \
104 || (TYPE) == R_X86_64_PC32 \
105 || (TYPE) == R_X86_64_PC64)
107 /* This must be the same as sframe_get_hdr_size (sfh). For x86-64, this value
108 is the same as sizeof (sframe_header) because there is no SFrame auxilliary
110 #define PLT_SFRAME_FDE_START_OFFSET sizeof (sframe_header)
112 #define ABI_64_P(abfd) \
113 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
115 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
116 copying dynamic variables from a shared lib into an app's dynbss
117 section, and instead use a dynamic relocation to point into the
119 #define ELIMINATE_COPY_RELOCS 1
121 #define elf_x86_hash_table(p, id) \
122 (is_elf_hash_table ((p)->hash) \
123 && elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) == (id) \
124 ? ((struct elf_x86_link_hash_table *) ((p)->hash)) : NULL)
126 /* Will references to this symbol always be local in this object? */
127 #define SYMBOL_REFERENCES_LOCAL_P(INFO, H) \
128 _bfd_x86_elf_link_symbol_references_local ((INFO), (H))
130 /* TRUE if an undefined weak symbol should be resolved to 0. Local
131 undefined weak symbol is always resolved to 0. Reference to an
132 undefined weak symbol is resolved to 0 in executable if undefined
133 weak symbol should be resolved to 0 (zero_undefweak > 0). */
134 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, EH) \
135 ((EH)->elf.root.type == bfd_link_hash_undefweak \
136 && (SYMBOL_REFERENCES_LOCAL_P ((INFO), &(EH)->elf) \
137 || (bfd_link_executable (INFO) \
138 && (EH)->zero_undefweak > 0)))
140 /* Should copy relocation be generated for a symbol. Don't generate
141 copy relocation against a protected symbol defined in a shared
143 #define SYMBOL_NO_COPYRELOC(INFO, EH) \
144 ((EH)->def_protected \
145 && ((EH)->elf.root.type == bfd_link_hash_defined \
146 || (EH)->elf.root.type == bfd_link_hash_defweak) \
147 && ((EH)->elf.root.u.def.section->owner->flags & DYNAMIC) != 0 \
148 && ((EH)->elf.root.u.def.section->flags & SEC_CODE) == 0)
150 /* TRUE if dynamic relocation is needed. If we are creating a shared
151 library, and this is a reloc against a global symbol, or a non PC
152 relative reloc against a local symbol, then we need to copy the reloc
153 into the shared library. However, if we are linking with -Bsymbolic,
154 we do not need to copy a reloc against a global symbol which is
155 defined in an object we are including in the link (i.e., DEF_REGULAR
158 If PCREL_PLT is true, don't generate dynamic relocation in PIE for
159 PC-relative relocation against a dynamic function definition in data
160 section when PLT address can be used.
162 If on the other hand, we are creating an executable, we may need to
163 keep relocations for symbols satisfied by a dynamic library if we
164 manage to avoid copy relocs for the symbol.
166 We also need to generate dynamic pointer relocation against
167 STT_GNU_IFUNC symbol in the non-code section. */
168 #define NEED_DYNAMIC_RELOCATION_P(IS_X86_64, INFO, PCREL_PLT, H, SEC, \
169 R_TYPE, POINTER_TYPE) \
170 ((bfd_link_pic (INFO) \
171 && (! X86_PCREL_TYPE_P (IS_X86_64, R_TYPE) \
173 && (! (bfd_link_pie (INFO) \
174 || SYMBOLIC_BIND ((INFO), (H))) \
175 || (H)->root.type == bfd_link_hash_defweak \
176 || (!(bfd_link_pie (INFO) \
178 && (H)->plt.refcount > 0 \
179 && ((SEC)->flags & SEC_CODE) == 0 \
180 && (H)->type == STT_FUNC \
181 && (H)->def_dynamic) \
182 && !(H)->def_regular))))) \
184 && (H)->type == STT_GNU_IFUNC \
185 && (R_TYPE) == POINTER_TYPE \
186 && ((SEC)->flags & SEC_CODE) == 0) \
187 || (ELIMINATE_COPY_RELOCS \
188 && !bfd_link_pic (INFO) \
190 && ((H)->root.type == bfd_link_hash_defweak \
191 || !(H)->def_regular)))
193 /* TRUE if dynamic relocation should be generated. Don't copy a
194 pc-relative relocation into the output file if the symbol needs
195 copy reloc or the symbol is undefined when building executable.
196 Copy dynamic function pointer relocations. Don't generate dynamic
197 relocations against resolved undefined weak symbols in PIE, except
198 when PC32_RELOC is TRUE. Undefined weak symbol is bound locally
199 when PIC is false. Don't generate dynamic relocations against
200 non-preemptible absolute symbol. NB: rel_from_abs is set on symbols
201 defined by linker scripts from "dot" (also SEGMENT_START or ORIGIN)
202 outside of an output section statement, which will be converted from
203 absolute to section-relative in set_sym_sections called from
204 ldexp_finalize_syms after ldemul_finish. */
205 #define GENERATE_DYNAMIC_RELOCATION_P(IS_X86_64, INFO, EH, R_TYPE, \
206 SEC, NEED_COPY_RELOC_IN_PIE, \
207 RESOLVED_TO_ZERO, PC32_RELOC) \
208 ((bfd_link_pic (INFO) \
209 && !(bfd_is_abs_section (SEC) \
211 || (EH)->elf.root.rel_from_abs == 0) \
213 || SYMBOL_REFERENCES_LOCAL (INFO, &(EH)->elf))) \
214 && !(NEED_COPY_RELOC_IN_PIE) \
216 || ((ELF_ST_VISIBILITY ((EH)->elf.other) == STV_DEFAULT \
217 && (!(RESOLVED_TO_ZERO) || PC32_RELOC)) \
218 || (EH)->elf.root.type != bfd_link_hash_undefweak)) \
219 && ((!X86_PCREL_TYPE_P (IS_X86_64, R_TYPE) \
220 && !X86_SIZE_TYPE_P (IS_X86_64, R_TYPE)) \
221 || ! SYMBOL_CALLS_LOCAL ((INFO), \
222 (struct elf_link_hash_entry *) (EH)))) \
223 || (ELIMINATE_COPY_RELOCS \
224 && !bfd_link_pic (INFO) \
226 && (EH)->elf.dynindx != -1 \
227 && (!(EH)->elf.non_got_ref \
228 || ((EH)->elf.root.type == bfd_link_hash_undefweak \
229 && !(RESOLVED_TO_ZERO))) \
230 && (((EH)->elf.def_dynamic && !(EH)->elf.def_regular) \
231 || (EH)->elf.root.type == bfd_link_hash_undefined)))
233 /* TRUE if this input relocation should be copied to output. H->dynindx
234 may be -1 if this symbol was marked to become local. */
235 #define COPY_INPUT_RELOC_P(IS_X86_64, INFO, H, R_TYPE) \
237 && (H)->dynindx != -1 \
238 && (X86_PCREL_TYPE_P (IS_X86_64, R_TYPE) \
239 || !(bfd_link_executable (INFO) || SYMBOLIC_BIND ((INFO), (H))) \
240 || !(H)->def_regular))
242 /* TRUE if this is actually a static link, or it is a -Bsymbolic link
243 and the symbol is defined locally, or the symbol was forced to be
244 local because of a version file. */
245 #define RESOLVED_LOCALLY_P(INFO, H, HTAB) \
246 (!WILL_CALL_FINISH_DYNAMIC_SYMBOL ((HTAB)->elf.dynamic_sections_created, \
247 bfd_link_pic (INFO), (H)) \
248 || (bfd_link_pic (INFO) \
249 && SYMBOL_REFERENCES_LOCAL_P ((INFO), (H))) \
250 || (ELF_ST_VISIBILITY ((H)->other) \
251 && (H)->root.type == bfd_link_hash_undefweak))
253 /* TRUE if this symbol isn't defined by a shared object. */
254 #define SYMBOL_DEFINED_NON_SHARED_P(H) \
256 || (H)->root.linker_def \
257 || (H)->root.ldscript_def \
258 || ((struct elf_x86_link_hash_entry *) (H))->linker_def \
259 || ELF_COMMON_DEF_P (H))
261 /* Return TRUE if the symbol described by a linker hash entry H is
262 going to be absolute. Similar to bfd_is_abs_symbol, but excluding
263 all linker-script defined symbols. */
264 #define ABS_SYMBOL_P(H) \
265 (bfd_is_abs_symbol (&(H)->root) && !(H)->root.ldscript_def)
267 /* TRUE if relative relocation should be generated. GOT reference to
268 global symbol in PIC will lead to dynamic symbol. It becomes a
269 problem when "time" or "times" is defined as a variable in an
270 executable, clashing with functions of the same name in libc. If a
271 symbol isn't undefined weak symbol, don't make it dynamic in PIC and
272 generate relative relocation. Don't generate relative relocation
273 against non-preemptible absolute symbol. */
274 #define GENERATE_RELATIVE_RELOC_P(INFO, H) \
275 ((H)->dynindx == -1 \
276 && !(H)->forced_local \
277 && (H)->root.type != bfd_link_hash_undefweak \
278 && bfd_link_pic (INFO) \
279 && !ABS_SYMBOL_P (H))
281 /* TRUE if this is a pointer reference to a local IFUNC. */
282 #define POINTER_LOCAL_IFUNC_P(INFO, H) \
283 ((H)->dynindx == -1 \
284 || (H)->forced_local \
285 || bfd_link_executable (INFO))
287 /* TRUE if this is a PLT reference to a local IFUNC. */
288 #define PLT_LOCAL_IFUNC_P(INFO, H) \
289 ((H)->dynindx == -1 \
290 || ((bfd_link_executable (INFO) \
291 || ELF_ST_VISIBILITY ((H)->other) != STV_DEFAULT) \
292 && (H)->def_regular \
293 && (H)->type == STT_GNU_IFUNC))
295 /* TRUE if TLS IE->LE transition is OK. */
296 #define TLS_TRANSITION_IE_TO_LE_P(INFO, H, TLS_TYPE) \
297 (bfd_link_executable (INFO) \
299 && (H)->dynindx == -1 \
300 && (TLS_TYPE & GOT_TLS_IE))
302 /* Verify that the symbol has an entry in the procedure linkage table. */
303 #define VERIFY_PLT_ENTRY(INFO, H, PLT, GOTPLT, RELPLT, LOCAL_UNDEFWEAK) \
306 if (((H)->dynindx == -1 \
307 && !LOCAL_UNDEFWEAK \
308 && !(((H)->forced_local || bfd_link_executable (INFO)) \
309 && (H)->def_regular \
310 && (H)->type == STT_GNU_IFUNC)) \
312 || (GOTPLT) == NULL \
313 || (RELPLT) == NULL) \
318 /* Verify that the symbol supports copy relocation. */
319 #define VERIFY_COPY_RELOC(H, HTAB) \
322 if ((H)->dynindx == -1 \
323 || ((H)->root.type != bfd_link_hash_defined \
324 && (H)->root.type != bfd_link_hash_defweak) \
325 || (HTAB)->elf.srelbss == NULL \
326 || (HTAB)->elf.sreldynrelro == NULL) \
331 /* x86 ELF linker hash entry. */
333 struct elf_x86_link_hash_entry
335 struct elf_link_hash_entry elf
;
337 unsigned char tls_type
;
339 /* Bit 0: Symbol has no GOT nor PLT relocations.
340 Bit 1: Symbol has non-GOT/non-PLT relocations in text sections.
341 zero_undefweak is initialized to 1 and undefined weak symbol
342 should be resolved to 0 if zero_undefweak > 0. */
343 unsigned int zero_undefweak
: 2;
345 /* Don't call finish_dynamic_symbol on this symbol. */
346 unsigned int no_finish_dynamic_symbol
: 1;
348 /* R_*_RELATIVE relocation in GOT for this symbol has been
350 unsigned int got_relative_reloc_done
: 1;
352 /* TRUE if symbol is __tls_get_addr. */
353 unsigned int tls_get_addr
: 1;
355 /* TRUE if symbol is defined as a protected symbol. */
356 unsigned int def_protected
: 1;
358 /* 0: Symbol references are unknown.
359 1: Symbol references aren't local.
360 2: Symbol references are local.
362 unsigned int local_ref
: 2;
364 /* TRUE if symbol is defined by linker. */
365 unsigned int linker_def
: 1;
367 /* TRUE if symbol is referenced by a non-GOT/non-PLT relocation in a
368 relocatable object file without indirect external access marker. */
369 unsigned int non_got_ref_without_indirect_extern_access
: 1;
371 /* TRUE if symbol is referenced by R_386_GOTOFF relocation. This is
372 only used by i386. */
373 unsigned int gotoff_ref
: 1;
375 /* TRUE if a weak symbol with a real definition needs a copy reloc.
376 When there is a weak symbol with a real definition, the processor
377 independent code will have arranged for us to see the real
378 definition first. We need to copy the needs_copy bit from the
379 real definition and check it when allowing copy reloc in PIE. This
380 is only used by x86-64. */
381 unsigned int needs_copy
: 1;
383 /* Information about the GOT PLT entry. Filled when there are both
384 GOT and PLT relocations against the same function. */
385 union gotplt_union plt_got
;
387 /* Information about the second PLT entry. */
388 union gotplt_union plt_second
;
390 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
391 starting at the end of the jump table. */
395 #define SFRAME_PLT0_MAX_NUM_FRES 2
396 #define SFRAME_PLTN_MAX_NUM_FRES 2
398 struct elf_x86_sframe_plt
400 unsigned int plt0_entry_size
;
401 unsigned int plt0_num_fres
;
402 const sframe_frame_row_entry
*plt0_fres
[SFRAME_PLT0_MAX_NUM_FRES
];
404 unsigned int pltn_entry_size
;
405 unsigned int pltn_num_fres
;
406 const sframe_frame_row_entry
*pltn_fres
[SFRAME_PLTN_MAX_NUM_FRES
];
408 unsigned int sec_pltn_entry_size
;
409 unsigned int sec_pltn_num_fres
;
410 const sframe_frame_row_entry
*sec_pltn_fres
[SFRAME_PLTN_MAX_NUM_FRES
];
413 struct elf_x86_lazy_plt_layout
415 /* The first entry in a lazy procedure linkage table looks like this. */
416 const bfd_byte
*plt0_entry
;
417 unsigned int plt0_entry_size
; /* Size of PLT0 entry. */
419 /* Later entries in a lazy procedure linkage table look like this. */
420 const bfd_byte
*plt_entry
;
421 unsigned int plt_entry_size
; /* Size of each PLT entry. */
423 /* The TLSDESC entry in a lazy procedure linkage table looks like
424 this. This is for x86-64 only. */
425 const bfd_byte
*plt_tlsdesc_entry
;
426 unsigned int plt_tlsdesc_entry_size
; /* Size of TLSDESC entry. */
428 /* Offsets into the TLSDESC entry that are to be replaced with
429 GOT+8 and GOT+TDG. These are for x86-64 only. */
430 unsigned int plt_tlsdesc_got1_offset
;
431 unsigned int plt_tlsdesc_got2_offset
;
433 /* Offset of the end of the PC-relative instructions containing
434 plt_tlsdesc_got1_offset and plt_tlsdesc_got2_offset. These
435 are for x86-64 only. */
436 unsigned int plt_tlsdesc_got1_insn_end
;
437 unsigned int plt_tlsdesc_got2_insn_end
;
439 /* Offsets into plt0_entry that are to be replaced with GOT[1] and
441 unsigned int plt0_got1_offset
;
442 unsigned int plt0_got2_offset
;
444 /* Offset of the end of the PC-relative instruction containing
445 plt0_got2_offset. This is for x86-64 only. */
446 unsigned int plt0_got2_insn_end
;
448 /* Offsets into plt_entry that are to be replaced with... */
449 unsigned int plt_got_offset
; /* ... address of this symbol in .got. */
450 unsigned int plt_reloc_offset
; /* ... offset into relocation table. */
451 unsigned int plt_plt_offset
; /* ... offset to start of .plt. */
453 /* Length of the PC-relative instruction containing plt_got_offset.
454 This is used for x86-64 only. */
455 unsigned int plt_got_insn_size
;
457 /* Offset of the end of the PC-relative jump to plt0_entry. This is
458 used for x86-64 only. */
459 unsigned int plt_plt_insn_end
;
461 /* Offset into plt_entry where the initial value of the GOT entry
463 unsigned int plt_lazy_offset
;
465 /* The first entry in a PIC lazy procedure linkage table looks like
467 const bfd_byte
*pic_plt0_entry
;
469 /* Subsequent entries in a PIC lazy procedure linkage table look
471 const bfd_byte
*pic_plt_entry
;
473 /* .eh_frame covering the lazy .plt section. */
474 const bfd_byte
*eh_frame_plt
;
475 unsigned int eh_frame_plt_size
;
478 struct elf_x86_non_lazy_plt_layout
480 /* Entries in a non-lazy procedure linkage table look like this. */
481 const bfd_byte
*plt_entry
;
482 /* Entries in a PIC non-lazy procedure linkage table look like this.
483 This is only used for i386 where absolute PLT and PIC PLT are
485 const bfd_byte
*pic_plt_entry
;
487 unsigned int plt_entry_size
; /* Size of each PLT entry. */
489 /* Offsets into plt_entry that are to be replaced with... */
490 unsigned int plt_got_offset
; /* ... address of this symbol in .got. */
492 /* Length of the PC-relative instruction containing plt_got_offset.
493 This is used for x86-64 only. */
494 unsigned int plt_got_insn_size
;
496 /* .eh_frame covering the non-lazy .plt section. */
497 const bfd_byte
*eh_frame_plt
;
498 unsigned int eh_frame_plt_size
;
501 struct elf_x86_plt_layout
503 /* The first entry in a lazy procedure linkage table looks like this. */
504 const bfd_byte
*plt0_entry
;
505 /* Entries in a procedure linkage table look like this. */
506 const bfd_byte
*plt_entry
;
507 unsigned int plt_entry_size
; /* Size of each PLT entry. */
510 unsigned int has_plt0
;
512 /* Offsets into plt_entry that are to be replaced with... */
513 unsigned int plt_got_offset
; /* ... address of this symbol in .got. */
515 /* Length of the PC-relative instruction containing plt_got_offset.
516 This is only used for x86-64. */
517 unsigned int plt_got_insn_size
;
519 /* Alignment of the .iplt section. */
520 unsigned int iplt_alignment
;
522 /* .eh_frame covering the .plt section. */
523 const bfd_byte
*eh_frame_plt
;
524 unsigned int eh_frame_plt_size
;
527 /* Values in tls_type of x86 ELF linker hash entry. */
528 #define GOT_UNKNOWN 0
532 #define GOT_TLS_IE_POS 5
533 #define GOT_TLS_IE_NEG 6
534 #define GOT_TLS_IE_BOTH 7
535 #define GOT_TLS_GDESC 8
537 #define GOT_TLS_GD_BOTH_P(type) \
538 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
539 #define GOT_TLS_GD_P(type) \
540 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
541 #define GOT_TLS_GDESC_P(type) \
542 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
543 #define GOT_TLS_GD_ANY_P(type) \
544 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
546 #define elf_x86_hash_entry(ent) \
547 ((struct elf_x86_link_hash_entry *)(ent))
549 /* Information of an input relocation used to compute its contribution
550 to the DT_RELR section size. */
552 struct elf_x86_relative_reloc_record
554 /* The original relocation info. */
555 Elf_Internal_Rela rel
;
556 /* The input or the GOT section where the relocation is applied. */
558 /* Local symbol info. NULL for global symbol. */
559 Elf_Internal_Sym
*sym
;
562 /* Section where the local symbol is defined. */
564 /* Global symbol hash. */
565 struct elf_link_hash_entry
*h
;
567 /* The offset into the output section where the relative relocation
568 will be applied at run-time. */
570 /* The run-time address. */
574 struct elf_x86_relative_reloc_data
578 struct elf_x86_relative_reloc_record
*data
;
581 /* DT_RELR bitmap. */
582 struct elf_dt_relr_bitmap
595 /* x86 ELF linker hash table. */
597 struct elf_x86_link_hash_table
599 struct elf_link_hash_table elf
;
601 /* Short-cuts to get to dynamic linker sections. */
603 asection
*plt_eh_frame
;
604 asection
*plt_second
;
605 asection
*plt_second_eh_frame
;
607 asection
*plt_got_eh_frame
;
609 sframe_encoder_ctx
*plt_cfe_ctx
;
610 asection
*plt_sframe
;
611 sframe_encoder_ctx
*plt_second_cfe_ctx
;
612 asection
*plt_second_sframe
;
614 /* Parameters describing PLT generation, lazy or non-lazy. */
615 struct elf_x86_plt_layout plt
;
617 /* Parameters describing lazy PLT generation. */
618 const struct elf_x86_lazy_plt_layout
*lazy_plt
;
620 /* Parameters describing non-lazy PLT generation. */
621 const struct elf_x86_non_lazy_plt_layout
*non_lazy_plt
;
623 /* The .sframe helper object for .plt section.
624 This is used for x86-64 only. */
625 const struct elf_x86_sframe_plt
*sframe_plt
;
629 bfd_signed_vma refcount
;
633 /* The amount of space used by the jump slots in the GOT. */
634 bfd_vma sgotplt_jump_table_size
;
636 /* _TLS_MODULE_BASE_ symbol. */
637 struct bfd_link_hash_entry
*tls_module_base
;
639 /* Used by local STT_GNU_IFUNC symbols. */
640 htab_t loc_hash_table
;
641 void * loc_hash_memory
;
643 /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
644 bfd_vma next_jump_slot_index
;
645 /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
646 bfd_vma next_irelative_index
;
648 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.
649 This is used for i386 only. */
652 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. This
653 is only used for i386. */
654 bfd_vma next_tls_desc_index
;
656 /* DT_RELR bitmap. */
657 struct elf_dt_relr_bitmap dt_relr_bitmap
;
659 /* Relative relocation data. */
660 struct elf_x86_relative_reloc_data relative_reloc
;
662 /* Unaligned relative relocation data. */
663 struct elf_x86_relative_reloc_data unaligned_relative_reloc
;
665 /* Number of relative reloc generation pass. */
666 unsigned int generate_relative_reloc_pass
;
668 /* Value used to fill the unused bytes of the first PLT entry. This
669 is only used for i386. */
670 bfd_byte plt0_pad_byte
;
672 /* TRUE if GOT is referenced. */
673 unsigned int got_referenced
: 1;
675 /* TRUE if PLT is PC-relative. PLT in PDE and PC-relative PLT in PIE
676 can be used as function address.
678 NB: i386 has non-PIC PLT and PIC PLT. Only non-PIC PLT in PDE can
679 be used as function address. PIC PLT in PIE can't be used as
681 unsigned int pcrel_plt
: 1;
683 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
684 bfd_vma (*r_sym
) (bfd_vma
);
685 bool (*is_reloc_section
) (const char *);
686 unsigned int sizeof_reloc
;
687 unsigned int got_entry_size
;
688 unsigned int pointer_r_type
;
689 unsigned int relative_r_type
;
690 int dynamic_interpreter_size
;
691 const char *dynamic_interpreter
;
692 const char *tls_get_addr
;
693 const char *relative_r_name
;
694 void (*elf_append_reloc
) (bfd
*, asection
*, Elf_Internal_Rela
*);
695 void (*elf_write_addend
) (bfd
*, uint64_t, void *);
696 void (*elf_write_addend_in_got
) (bfd
*, uint64_t, void *);
698 /* Options passed from the linker. */
699 struct elf_linker_x86_params
*params
;
702 struct elf_x86_init_table
704 /* The lazy PLT layout. */
705 const struct elf_x86_lazy_plt_layout
*lazy_plt
;
707 /* The non-lazy PLT layout. */
708 const struct elf_x86_non_lazy_plt_layout
*non_lazy_plt
;
710 /* The lazy PLT layout for IBT. */
711 const struct elf_x86_lazy_plt_layout
*lazy_ibt_plt
;
713 /* The non-lazy PLT layout for IBT. */
714 const struct elf_x86_non_lazy_plt_layout
*non_lazy_ibt_plt
;
716 /* The .sframe helper object for lazy .plt section.
717 This is used for x86-64 only. */
718 const struct elf_x86_sframe_plt
*sframe_lazy_plt
;
720 /* The .sframe helper object for non-lazy .plt section.
721 This is used for x86-64 only. */
722 const struct elf_x86_sframe_plt
*sframe_non_lazy_plt
;
724 /* The .sframe helper object for lazy IBT .plt section.
725 This is used for x86-64 only. */
726 const struct elf_x86_sframe_plt
*sframe_lazy_ibt_plt
;
728 /* The .sframe helper object for non-lazy IBT .plt section.
729 This is used for x86-64 only. */
730 const struct elf_x86_sframe_plt
*sframe_non_lazy_ibt_plt
;
732 bfd_byte plt0_pad_byte
;
734 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
735 bfd_vma (*r_sym
) (bfd_vma
);
738 struct elf_x86_obj_tdata
740 struct elf_obj_tdata root
;
742 /* tls_type for each local got entry. */
743 char *local_got_tls_type
;
745 /* GOTPLT entries for TLS descriptors. */
746 bfd_vma
*local_tlsdesc_gotent
;
748 /* R_*_RELATIVE relocation in GOT for this local symbol has been
750 char *relative_reloc_done
;
753 enum elf_x86_plt_type
767 enum elf_x86_plt_type type
;
768 unsigned int plt_got_offset
;
769 unsigned int plt_entry_size
;
770 unsigned int plt_got_insn_size
; /* Only used for x86-64. */
774 /* Set if a relocation is converted from a GOTPCREL relocation. */
775 #define R_X86_64_converted_reloc_bit (1 << 7)
777 #define elf_x86_tdata(abfd) \
778 ((struct elf_x86_obj_tdata *) (abfd)->tdata.any)
780 #define elf_x86_local_got_tls_type(abfd) \
781 (elf_x86_tdata (abfd)->local_got_tls_type)
783 #define elf_x86_local_tlsdesc_gotent(abfd) \
784 (elf_x86_tdata (abfd)->local_tlsdesc_gotent)
786 #define elf_x86_relative_reloc_done(abfd) \
787 (elf_x86_tdata (abfd)->relative_reloc_done)
789 #define elf_x86_compute_jump_table_size(htab) \
790 ((htab)->elf.srelplt->reloc_count * (htab)->got_entry_size)
792 #define is_x86_elf(bfd, htab) \
793 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
794 && elf_tdata (bfd) != NULL \
795 && elf_object_id (bfd) == (htab)->elf.hash_table_id)
797 /* Rename some of the generic section flags to better document how they
799 #define check_relocs_failed sec_flg0
800 #define relative_reloc_packed sec_flg1
802 extern bool _bfd_x86_elf_mkobject
805 extern void _bfd_x86_elf_set_tls_module_base
806 (struct bfd_link_info
*);
808 extern bfd_vma _bfd_x86_elf_dtpoff_base
809 (struct bfd_link_info
*);
811 extern bool _bfd_x86_elf_readonly_dynrelocs
812 (struct elf_link_hash_entry
*, void *);
814 extern struct elf_link_hash_entry
* _bfd_elf_x86_get_local_sym_hash
815 (struct elf_x86_link_hash_table
*, bfd
*, const Elf_Internal_Rela
*,
818 extern hashval_t _bfd_x86_elf_local_htab_hash
821 extern int _bfd_x86_elf_local_htab_eq
822 (const void *, const void *);
824 extern struct bfd_hash_entry
* _bfd_x86_elf_link_hash_newfunc
825 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
827 extern struct bfd_link_hash_table
* _bfd_x86_elf_link_hash_table_create
830 extern int _bfd_x86_elf_compare_relocs
831 (const void *, const void *);
833 extern bool _bfd_x86_elf_link_check_relocs
834 (bfd
*, struct bfd_link_info
*);
836 extern bool _bfd_x86_elf_check_relocs
837 (bfd
*, struct bfd_link_info
*, asection
*,
838 const Elf_Internal_Rela
*);
840 extern bool _bfd_x86_elf_link_relax_section
841 (bfd
*, asection
*, struct bfd_link_info
*, bool *);
843 extern bool _bfd_elf_x86_size_relative_relocs
844 (struct bfd_link_info
*, bool *);
846 extern bool _bfd_elf_x86_finish_relative_relocs
847 (struct bfd_link_info
*);
849 extern void _bfd_elf32_write_addend (bfd
*, uint64_t, void *);
850 extern void _bfd_elf64_write_addend (bfd
*, uint64_t, void *);
852 extern bool _bfd_elf_x86_valid_reloc_p
853 (asection
*, struct bfd_link_info
*, struct elf_x86_link_hash_table
*,
854 const Elf_Internal_Rela
*, struct elf_link_hash_entry
*,
855 Elf_Internal_Sym
*, Elf_Internal_Shdr
*, bool *);
857 extern bool _bfd_x86_elf_size_dynamic_sections
858 (bfd
*, struct bfd_link_info
*);
860 extern struct elf_x86_link_hash_table
*_bfd_x86_elf_finish_dynamic_sections
861 (bfd
*, struct bfd_link_info
*);
863 extern bool _bfd_x86_elf_always_size_sections
864 (bfd
*, struct bfd_link_info
*);
866 extern void _bfd_x86_elf_merge_symbol_attribute
867 (struct elf_link_hash_entry
*, unsigned int, bool, bool);
869 extern void _bfd_x86_elf_copy_indirect_symbol
870 (struct bfd_link_info
*, struct elf_link_hash_entry
*,
871 struct elf_link_hash_entry
*);
873 extern bool _bfd_x86_elf_fixup_symbol
874 (struct bfd_link_info
*, struct elf_link_hash_entry
*);
876 extern bool _bfd_x86_elf_hash_symbol
877 (struct elf_link_hash_entry
*);
879 extern bool _bfd_x86_elf_adjust_dynamic_symbol
880 (struct bfd_link_info
*, struct elf_link_hash_entry
*);
882 extern void _bfd_x86_elf_hide_symbol
883 (struct bfd_link_info
*, struct elf_link_hash_entry
*, bool);
885 extern bool _bfd_x86_elf_link_symbol_references_local
886 (struct bfd_link_info
*, struct elf_link_hash_entry
*);
888 extern asection
* _bfd_x86_elf_gc_mark_hook
889 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
890 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
892 extern long _bfd_x86_elf_get_synthetic_symtab
893 (bfd
*, long, long, bfd_vma
, struct elf_x86_plt
[], asymbol
**,
896 extern enum elf_property_kind _bfd_x86_elf_parse_gnu_properties
897 (bfd
*, unsigned int, bfd_byte
*, unsigned int);
899 extern bool _bfd_x86_elf_merge_gnu_properties
900 (struct bfd_link_info
*, bfd
*, bfd
*, elf_property
*, elf_property
*);
902 extern void _bfd_x86_elf_link_fixup_gnu_properties
903 (struct bfd_link_info
*, elf_property_list
**);
905 extern bfd
* _bfd_x86_elf_link_setup_gnu_properties
906 (struct bfd_link_info
*, struct elf_x86_init_table
*);
908 extern void _bfd_x86_elf_link_fixup_ifunc_symbol
909 (struct bfd_link_info
*, struct elf_x86_link_hash_table
*,
910 struct elf_link_hash_entry
*, Elf_Internal_Sym
*sym
);
912 extern void _bfd_x86_elf_link_report_relative_reloc
913 (struct bfd_link_info
*, asection
*, struct elf_link_hash_entry
*,
914 Elf_Internal_Sym
*, const char *, const void *);
916 #define bfd_elf64_mkobject \
917 _bfd_x86_elf_mkobject
918 #define bfd_elf32_mkobject \
919 _bfd_x86_elf_mkobject
920 #define bfd_elf64_bfd_link_hash_table_create \
921 _bfd_x86_elf_link_hash_table_create
922 #define bfd_elf32_bfd_link_hash_table_create \
923 _bfd_x86_elf_link_hash_table_create
924 #define bfd_elf64_bfd_link_check_relocs \
925 _bfd_x86_elf_link_check_relocs
926 #define bfd_elf32_bfd_link_check_relocs \
927 _bfd_x86_elf_link_check_relocs
928 #define bfd_elf32_bfd_relax_section \
929 _bfd_x86_elf_link_relax_section
930 #define bfd_elf64_bfd_relax_section \
931 _bfd_x86_elf_link_relax_section
933 #define elf_backend_check_relocs \
934 _bfd_x86_elf_check_relocs
935 #define elf_backend_size_dynamic_sections \
936 _bfd_x86_elf_size_dynamic_sections
937 #define elf_backend_merge_symbol_attribute \
938 _bfd_x86_elf_merge_symbol_attribute
939 #define elf_backend_copy_indirect_symbol \
940 _bfd_x86_elf_copy_indirect_symbol
941 #define elf_backend_fixup_symbol \
942 _bfd_x86_elf_fixup_symbol
943 #define elf_backend_hash_symbol \
944 _bfd_x86_elf_hash_symbol
945 #define elf_backend_adjust_dynamic_symbol \
946 _bfd_x86_elf_adjust_dynamic_symbol
947 #define elf_backend_gc_mark_hook \
948 _bfd_x86_elf_gc_mark_hook
949 #define elf_backend_omit_section_dynsym \
950 _bfd_elf_omit_section_dynsym_all
951 #define elf_backend_parse_gnu_properties \
952 _bfd_x86_elf_parse_gnu_properties
953 #define elf_backend_merge_gnu_properties \
954 _bfd_x86_elf_merge_gnu_properties
955 #define elf_backend_fixup_gnu_properties \
956 _bfd_x86_elf_link_fixup_gnu_properties
957 #define elf_backend_size_relative_relocs \
958 _bfd_elf_x86_size_relative_relocs
959 #define elf_backend_finish_relative_relocs \
960 _bfd_elf_x86_finish_relative_relocs
962 #define ELF_P_ALIGN ELF_MINPAGESIZE
964 /* Allocate x86 GOT info for local symbols. */
967 elf_x86_allocate_local_got_info (bfd
*abfd
, bfd_size_type count
)
969 bfd_signed_vma
*local_got_refcounts
= elf_local_got_refcounts (abfd
);
970 if (local_got_refcounts
== NULL
)
972 bfd_size_type size
= count
* (sizeof (bfd_signed_vma
)
975 local_got_refcounts
= (bfd_signed_vma
*) bfd_zalloc (abfd
, size
);
976 if (local_got_refcounts
== NULL
)
978 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
979 elf_x86_local_tlsdesc_gotent (abfd
) =
980 (bfd_vma
*) (local_got_refcounts
+ count
);
981 elf_x86_local_got_tls_type (abfd
) =
982 (char *) (local_got_refcounts
+ 2 * count
);
983 elf_x86_relative_reloc_done (abfd
) =
984 ((char *) (local_got_refcounts
+ 2 * count
)) + count
;