go32 sanity check
[binutils-gdb.git] / bfd / elfxx-x86.h
1 /* x86 specific support for ELF
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
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.
10
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.
15
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. */
20
21 /* Don't generate unused section symbols. */
22 #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS false
23
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "bfdlink.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "hashtab.h"
30 #include "elf-linker-x86.h"
31 #include "elf/i386.h"
32 #include "elf/x86-64.h"
33 #include "sframe-api.h"
34
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))
44
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))
50
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))
63
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) \
74 ((IS_X86_64) \
75 ? X86_64_RELATIVE_RELOC_TYPE_P (TYPE) \
76 : I386_RELATIVE_RELOC_TYPE_P(TYPE))
77
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) \
88 ((IS_X86_64) \
89 ? X86_64_NEED_DYNAMIC_RELOC_TYPE_P (TYPE) \
90 : I386_NEED_DYNAMIC_RELOC_TYPE_P (TYPE))
91
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)))
95
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
100
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)
108
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
111 header. */
112 #define PLT_SFRAME_FDE_START_OFFSET sizeof (sframe_header)
113
114 #define ABI_64_P(abfd) \
115 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
116
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
120 shared lib. */
121 #define ELIMINATE_COPY_RELOCS 1
122
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)
127
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))
131
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)))
141
142 /* Should copy relocation be generated for a symbol. Don't generate
143 copy relocation against a protected symbol defined in a shared
144 object. */
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)
151
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
158 is set).
159
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.
163
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.
167
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) \
174 || ((H) != NULL \
175 && (! (bfd_link_pie (INFO) \
176 || SYMBOLIC_BIND ((INFO), (H))) \
177 || (H)->root.type == bfd_link_hash_defweak \
178 || (!(bfd_link_pie (INFO) \
179 && (PCREL_PLT) \
180 && (H)->plt.refcount > 0 \
181 && ((SEC)->flags & SEC_CODE) == 0 \
182 && (H)->type == STT_FUNC \
183 && (H)->def_dynamic) \
184 && !(H)->def_regular))))) \
185 || ((H) != NULL \
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) \
191 && (H) != NULL \
192 && ((H)->root.type == bfd_link_hash_defweak \
193 || !(H)->def_regular)))
194
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) \
212 && ((EH) == NULL \
213 || (EH)->elf.root.rel_from_abs == 0) \
214 && ((EH) == NULL \
215 || SYMBOL_REFERENCES_LOCAL (INFO, &(EH)->elf))) \
216 && !(NEED_COPY_RELOC_IN_PIE) \
217 && ((EH) == NULL \
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) \
227 && (EH) != NULL \
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)))
234
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) \
238 ((H) != NULL \
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))
243
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))
254
255 /* TRUE if this symbol isn't defined by a shared object. */
256 #define SYMBOL_DEFINED_NON_SHARED_P(H) \
257 ((H)->def_regular \
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))
262
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)
268
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))
282
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))
288
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))
296
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) \
300 && (H) != NULL \
301 && (H)->dynindx == -1 \
302 && (TLS_TYPE & GOT_TLS_IE))
303
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) \
306 do \
307 { \
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)) \
313 || (PLT) == NULL \
314 || (GOTPLT) == NULL \
315 || (RELPLT) == NULL) \
316 abort (); \
317 } \
318 while (0);
319
320 /* Verify that the symbol supports copy relocation. */
321 #define VERIFY_COPY_RELOC(H, HTAB) \
322 do \
323 { \
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) \
329 abort (); \
330 } \
331 while (0);
332
333 /* x86 ELF linker hash entry. */
334
335 struct elf_x86_link_hash_entry
336 {
337 struct elf_link_hash_entry elf;
338
339 unsigned char tls_type;
340
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;
346
347 /* Don't call finish_dynamic_symbol on this symbol. */
348 unsigned int no_finish_dynamic_symbol : 1;
349
350 /* R_*_RELATIVE relocation in GOT for this symbol has been
351 processed. */
352 unsigned int got_relative_reloc_done : 1;
353
354 /* TRUE if symbol is __tls_get_addr. */
355 unsigned int tls_get_addr : 1;
356
357 /* TRUE if symbol is defined as a protected symbol. */
358 unsigned int def_protected : 1;
359
360 /* 0: Symbol references are unknown.
361 1: Symbol references aren't local.
362 2: Symbol references are local.
363 */
364 unsigned int local_ref : 2;
365
366 /* TRUE if symbol is defined by linker. */
367 unsigned int linker_def : 1;
368
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;
372
373 /* TRUE if symbol is referenced by R_386_GOTOFF relocation. This is
374 only used by i386. */
375 unsigned int gotoff_ref : 1;
376
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;
384
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;
388
389 /* Information about the second PLT entry. */
390 union gotplt_union plt_second;
391
392 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
393 starting at the end of the jump table. */
394 bfd_vma tlsdesc_got;
395 };
396
397 #define SFRAME_PLT0_MAX_NUM_FRES 2
398 #define SFRAME_PLTN_MAX_NUM_FRES 2
399
400 struct elf_x86_sframe_plt
401 {
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];
405
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];
409
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];
413 };
414
415 struct elf_x86_lazy_plt_layout
416 {
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. */
420
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. */
424
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. */
429
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;
434
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;
440
441 /* Offsets into plt0_entry that are to be replaced with GOT[1] and
442 GOT[2]. */
443 unsigned int plt0_got1_offset;
444 unsigned int plt0_got2_offset;
445
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;
449
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. */
454
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;
458
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;
462
463 /* Offset into plt_entry where the initial value of the GOT entry
464 points. */
465 unsigned int plt_lazy_offset;
466
467 /* The first entry in a PIC lazy procedure linkage table looks like
468 this. */
469 const bfd_byte *pic_plt0_entry;
470
471 /* Subsequent entries in a PIC lazy procedure linkage table look
472 like this. */
473 const bfd_byte *pic_plt_entry;
474
475 /* .eh_frame covering the lazy .plt section. */
476 const bfd_byte *eh_frame_plt;
477 unsigned int eh_frame_plt_size;
478 };
479
480 struct elf_x86_non_lazy_plt_layout
481 {
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
486 different. */
487 const bfd_byte *pic_plt_entry;
488
489 unsigned int plt_entry_size; /* Size of each PLT entry. */
490
491 /* Offsets into plt_entry that are to be replaced with... */
492 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
493
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;
497
498 /* .eh_frame covering the non-lazy .plt section. */
499 const bfd_byte *eh_frame_plt;
500 unsigned int eh_frame_plt_size;
501 };
502
503 struct elf_x86_plt_layout
504 {
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. */
510
511 /* 1 has PLT0. */
512 unsigned int has_plt0;
513
514 /* Offsets into plt_entry that are to be replaced with... */
515 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
516
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;
520
521 /* Alignment of the .iplt section. */
522 unsigned int iplt_alignment;
523
524 /* .eh_frame covering the .plt section. */
525 const bfd_byte *eh_frame_plt;
526 unsigned int eh_frame_plt_size;
527 };
528
529 /* Values in tls_type of x86 ELF linker hash entry. */
530 #define GOT_UNKNOWN 0
531 #define GOT_NORMAL 1
532 #define GOT_TLS_GD 2
533 #define GOT_TLS_IE 4
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
538 #define GOT_ABS 9
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))
547
548 #define elf_x86_hash_entry(ent) \
549 ((struct elf_x86_link_hash_entry *)(ent))
550
551 /* Information of an input relocation used to compute its contribution
552 to the DT_RELR section size. */
553
554 struct elf_x86_relative_reloc_record
555 {
556 /* The original relocation info. */
557 Elf_Internal_Rela rel;
558 /* The input or the GOT section where the relocation is applied. */
559 asection *sec;
560 /* Local symbol info. NULL for global symbol. */
561 Elf_Internal_Sym *sym;
562 union
563 {
564 /* Section where the local symbol is defined. */
565 asection *sym_sec;
566 /* Global symbol hash. */
567 struct elf_link_hash_entry *h;
568 } u;
569 /* The offset into the output section where the relative relocation
570 will be applied at run-time. */
571 bfd_vma offset;
572 /* The run-time address. */
573 bfd_vma address;
574 };
575
576 struct elf_x86_relative_reloc_data
577 {
578 bfd_size_type count;
579 bfd_size_type size;
580 struct elf_x86_relative_reloc_record *data;
581 };
582
583 /* DT_RELR bitmap. */
584 struct elf_dt_relr_bitmap
585 {
586 bfd_size_type count;
587 bfd_size_type size;
588 union
589 {
590 /* 32-bit bitmap. */
591 uint32_t *elf32;
592 /* 64-bit bitmap. */
593 uint64_t *elf64;
594 } u;
595 };
596
597 /* x86 ELF linker hash table. */
598
599 struct elf_x86_link_hash_table
600 {
601 struct elf_link_hash_table elf;
602
603 /* Short-cuts to get to dynamic linker sections. */
604 asection *interp;
605 asection *plt_eh_frame;
606 asection *plt_second;
607 asection *plt_second_eh_frame;
608 asection *plt_got;
609 asection *plt_got_eh_frame;
610
611 sframe_encoder_ctx *plt_cfe_ctx;
612 asection *plt_sframe;
613 sframe_encoder_ctx *plt_second_cfe_ctx;
614 asection *plt_second_sframe;
615
616 /* Parameters describing PLT generation, lazy or non-lazy. */
617 struct elf_x86_plt_layout plt;
618
619 /* Parameters describing lazy PLT generation. */
620 const struct elf_x86_lazy_plt_layout *lazy_plt;
621
622 /* Parameters describing non-lazy PLT generation. */
623 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
624
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;
628
629 union
630 {
631 bfd_signed_vma refcount;
632 bfd_vma offset;
633 } tls_ld_or_ldm_got;
634
635 /* The amount of space used by the jump slots in the GOT. */
636 bfd_vma sgotplt_jump_table_size;
637
638 /* _TLS_MODULE_BASE_ symbol. */
639 struct bfd_link_hash_entry *tls_module_base;
640
641 /* Used by local STT_GNU_IFUNC symbols. */
642 htab_t loc_hash_table;
643 void * loc_hash_memory;
644
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;
649
650 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.
651 This is used for i386 only. */
652 asection *srelplt2;
653
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;
657
658 /* DT_RELR bitmap. */
659 struct elf_dt_relr_bitmap dt_relr_bitmap;
660
661 /* Relative relocation data. */
662 struct elf_x86_relative_reloc_data relative_reloc;
663
664 /* Unaligned relative relocation data. */
665 struct elf_x86_relative_reloc_data unaligned_relative_reloc;
666
667 /* Number of relative reloc generation pass. */
668 unsigned int generate_relative_reloc_pass;
669
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;
673
674 /* TRUE if GOT is referenced. */
675 unsigned int got_referenced : 1;
676
677 /* TRUE if PLT is PC-relative. PLT in PDE and PC-relative PLT in PIE
678 can be used as function address.
679
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
682 function address. */
683 unsigned int pcrel_plt : 1;
684
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 *);
699
700 /* Options passed from the linker. */
701 struct elf_linker_x86_params *params;
702 };
703
704 struct elf_x86_init_table
705 {
706 /* The lazy PLT layout. */
707 const struct elf_x86_lazy_plt_layout *lazy_plt;
708
709 /* The non-lazy PLT layout. */
710 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
711
712 /* The lazy PLT layout for IBT. */
713 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
714
715 /* The non-lazy PLT layout for IBT. */
716 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
717
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;
721
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;
725
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;
729
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;
733
734 bfd_byte plt0_pad_byte;
735
736 bfd_vma (*r_info) (bfd_vma, bfd_vma);
737 bfd_vma (*r_sym) (bfd_vma);
738 };
739
740 struct elf_x86_obj_tdata
741 {
742 struct elf_obj_tdata root;
743
744 /* tls_type for each local got entry. */
745 char *local_got_tls_type;
746
747 /* GOTPLT entries for TLS descriptors. */
748 bfd_vma *local_tlsdesc_gotent;
749
750 /* R_*_RELATIVE relocation in GOT for this local symbol has been
751 processed. */
752 char *relative_reloc_done;
753 };
754
755 enum elf_x86_plt_type
756 {
757 plt_non_lazy = 0,
758 plt_lazy = 1 << 0,
759 plt_pic = 1 << 1,
760 plt_second = 1 << 2,
761 plt_unknown = -1
762 };
763
764 struct elf_x86_plt
765 {
766 const char *name;
767 asection *sec;
768 bfd_byte *contents;
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. */
773 long count;
774 };
775
776 /* Set if a relocation is converted from a GOTPCREL relocation. */
777 #define R_X86_64_converted_reloc_bit (1 << 7)
778
779 #define elf_x86_tdata(abfd) \
780 ((struct elf_x86_obj_tdata *) (abfd)->tdata.any)
781
782 #define elf_x86_local_got_tls_type(abfd) \
783 (elf_x86_tdata (abfd)->local_got_tls_type)
784
785 #define elf_x86_local_tlsdesc_gotent(abfd) \
786 (elf_x86_tdata (abfd)->local_tlsdesc_gotent)
787
788 #define elf_x86_relative_reloc_done(abfd) \
789 (elf_x86_tdata (abfd)->relative_reloc_done)
790
791 #define elf_x86_compute_jump_table_size(htab) \
792 ((htab)->elf.srelplt->reloc_count * (htab)->got_entry_size)
793
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)
798
799 /* Rename some of the generic section flags to better document how they
800 are used here. */
801 #define check_relocs_failed sec_flg0
802 #define relative_reloc_packed sec_flg1
803
804 extern bool _bfd_x86_elf_mkobject
805 (bfd *);
806
807 extern void _bfd_x86_elf_set_tls_module_base
808 (struct bfd_link_info *);
809
810 extern bfd_vma _bfd_x86_elf_dtpoff_base
811 (struct bfd_link_info *);
812
813 extern bool _bfd_x86_elf_readonly_dynrelocs
814 (struct elf_link_hash_entry *, void *);
815
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 *,
818 bool);
819
820 extern hashval_t _bfd_x86_elf_local_htab_hash
821 (const void *);
822
823 extern int _bfd_x86_elf_local_htab_eq
824 (const void *, const void *);
825
826 extern struct bfd_hash_entry * _bfd_x86_elf_link_hash_newfunc
827 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
828
829 extern struct bfd_link_hash_table * _bfd_x86_elf_link_hash_table_create
830 (bfd *);
831
832 extern int _bfd_x86_elf_compare_relocs
833 (const void *, const void *);
834
835 extern bool _bfd_x86_elf_link_check_relocs
836 (bfd *, struct bfd_link_info *);
837
838 extern bool _bfd_x86_elf_check_relocs
839 (bfd *, struct bfd_link_info *, asection *,
840 const Elf_Internal_Rela *);
841
842 extern bool _bfd_x86_elf_link_relax_section
843 (bfd *, asection *, struct bfd_link_info *, bool *);
844
845 extern bool _bfd_elf_x86_size_relative_relocs
846 (struct bfd_link_info *, bool *);
847
848 extern bool _bfd_elf_x86_finish_relative_relocs
849 (struct bfd_link_info *);
850
851 extern void _bfd_elf32_write_addend (bfd *, uint64_t, void *);
852 extern void _bfd_elf64_write_addend (bfd *, uint64_t, void *);
853
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 *);
858
859 extern bool _bfd_x86_elf_size_dynamic_sections
860 (bfd *, struct bfd_link_info *);
861
862 extern struct elf_x86_link_hash_table *_bfd_x86_elf_finish_dynamic_sections
863 (bfd *, struct bfd_link_info *);
864
865 extern bool _bfd_x86_elf_always_size_sections
866 (bfd *, struct bfd_link_info *);
867
868 extern void _bfd_x86_elf_merge_symbol_attribute
869 (struct elf_link_hash_entry *, unsigned int, bool, bool);
870
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 *);
874
875 extern bool _bfd_x86_elf_fixup_symbol
876 (struct bfd_link_info *, struct elf_link_hash_entry *);
877
878 extern bool _bfd_x86_elf_hash_symbol
879 (struct elf_link_hash_entry *);
880
881 extern bool _bfd_x86_elf_adjust_dynamic_symbol
882 (struct bfd_link_info *, struct elf_link_hash_entry *);
883
884 extern void _bfd_x86_elf_hide_symbol
885 (struct bfd_link_info *, struct elf_link_hash_entry *, bool);
886
887 extern bool _bfd_x86_elf_link_symbol_references_local
888 (struct bfd_link_info *, struct elf_link_hash_entry *);
889
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 *);
893
894 extern long _bfd_x86_elf_get_synthetic_symtab
895 (bfd *, long, long, bfd_vma, struct elf_x86_plt [], asymbol **,
896 asymbol **);
897
898 extern enum elf_property_kind _bfd_x86_elf_parse_gnu_properties
899 (bfd *, unsigned int, bfd_byte *, unsigned int);
900
901 extern bool _bfd_x86_elf_merge_gnu_properties
902 (struct bfd_link_info *, bfd *, bfd *, elf_property *, elf_property *);
903
904 extern void _bfd_x86_elf_link_fixup_gnu_properties
905 (struct bfd_link_info *, elf_property_list **);
906
907 extern bfd * _bfd_x86_elf_link_setup_gnu_properties
908 (struct bfd_link_info *, struct elf_x86_init_table *);
909
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);
913
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 *);
917
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
934
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
963
964 #define ELF_P_ALIGN ELF_MINPAGESIZE
965
966 /* Allocate x86 GOT info for local symbols. */
967
968 static inline bool
969 elf_x86_allocate_local_got_info (bfd *abfd, bfd_size_type count)
970 {
971 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
972 if (local_got_refcounts == NULL)
973 {
974 bfd_size_type size = count * (sizeof (bfd_signed_vma)
975 + sizeof (bfd_vma)
976 + 2 * sizeof(char));
977 local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
978 if (local_got_refcounts == NULL)
979 return false;
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;
987 }
988 return true;
989 }