]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elfxx-x86.h
x86: Add NEED_DYNAMIC_RELOCATION_P
[thirdparty/binutils-gdb.git] / bfd / elfxx-x86.h
1 /* x86 specific support for ELF
2 Copyright (C) 2017 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 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "bfd_stdint.h"
27 #include "hashtab.h"
28
29 #define PLT_CIE_LENGTH 20
30 #define PLT_FDE_LENGTH 36
31 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
32 #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
33
34 #define ABI_64_P(abfd) \
35 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
36
37 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
38 copying dynamic variables from a shared lib into an app's dynbss
39 section, and instead use a dynamic relocation to point into the
40 shared lib. */
41 #define ELIMINATE_COPY_RELOCS 1
42
43 #define elf_x86_hash_table(p, id) \
44 (is_elf_hash_table ((p)->hash) \
45 && elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) == (id) \
46 ? ((struct elf_x86_link_hash_table *) ((p)->hash)) : NULL)
47
48 /* Will references to this symbol always be local in this object? */
49 #define SYMBOL_REFERENCES_LOCAL_P(INFO, H) \
50 _bfd_x86_elf_link_symbol_references_local ((INFO), (H))
51
52 /* Is a undefined weak symbol which is resolved to 0. Reference to an
53 undefined weak symbol is resolved to 0 when building executable if
54 it isn't dynamic and
55 1. Has non-GOT/non-PLT relocations in text section. Or
56 2. Has no GOT/PLT relocation.
57 Local undefined weak symbol is always resolved to 0.
58 */
59 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, EH) \
60 ((EH)->elf.root.type == bfd_link_hash_undefweak \
61 && (SYMBOL_REFERENCES_LOCAL_P ((INFO), &(EH)->elf) \
62 || (bfd_link_executable (INFO) \
63 && (!(EH)->has_got_reloc \
64 || (EH)->has_non_got_reloc))))
65
66 /* Should copy relocation be generated for a symbol. Don't generate
67 copy relocation against a protected symbol defined in a shared
68 object with GNU_PROPERTY_NO_COPY_ON_PROTECTED. */
69 #define SYMBOL_NO_COPYRELOC(INFO, EH) \
70 ((EH)->def_protected \
71 && ((EH)->elf.root.type == bfd_link_hash_defined \
72 || (EH)->elf.root.type == bfd_link_hash_defweak) \
73 && elf_has_no_copy_on_protected ((EH)->elf.root.u.def.section->owner) \
74 && ((EH)->elf.root.u.def.section->owner->flags & DYNAMIC) != 0 \
75 && ((EH)->elf.root.u.def.section->flags & SEC_CODE) == 0)
76
77 /* TRUE if dynamic relocation is needed. If we are creating a shared
78 library, and this is a reloc against a global symbol, or a non PC
79 relative reloc against a local symbol, then we need to copy the reloc
80 into the shared library. However, if we are linking with -Bsymbolic,
81 we do not need to copy a reloc against a global symbol which is
82 defined in an object we are including in the link (i.e., DEF_REGULAR
83 is set). At this point we have not seen all the input files, so it
84 is possible that DEF_REGULAR is not set now but will be set later (it
85 is never cleared). In case of a weak definition, DEF_REGULAR may be
86 cleared later by a strong definition in a shared library. We account
87 for that possibility below by storing information in the relocs_copied
88 field of the hash table entry. A similar situation occurs when
89 creating shared libraries and symbol visibility changes render the
90 symbol local.
91
92 If on the other hand, we are creating an executable, we may need to
93 keep relocations for symbols satisfied by a dynamic library if we
94 manage to avoid copy relocs for the symbol.
95
96 We also need to generate dynamic pointer relocation against
97 STT_GNU_IFUNC symbol in the non-code section. */
98 #define NEED_DYNAMIC_RELOCATION_P(INFO, H, SEC, R_TYPE, POINTER_TYPE) \
99 ((bfd_link_pic (INFO) \
100 && (! X86_PCREL_TYPE_P (R_TYPE) \
101 || ((H) != NULL \
102 && (! (bfd_link_pie (INFO) \
103 || SYMBOLIC_BIND ((INFO), (H))) \
104 || (H)->root.type == bfd_link_hash_defweak \
105 || !(H)->def_regular)))) \
106 || ((H) != NULL \
107 && (H)->type == STT_GNU_IFUNC \
108 && (R_TYPE) == POINTER_TYPE \
109 && ((SEC)->flags & SEC_CODE) == 0) \
110 || (ELIMINATE_COPY_RELOCS \
111 && !bfd_link_pic (INFO) \
112 && (H) != NULL \
113 && ((H)->root.type == bfd_link_hash_defweak \
114 || !(H)->def_regular)))
115
116 /* TRUE if TLS IE->LE transition is OK. */
117 #define TLS_TRANSITION_IE_TO_LE_P(INFO, H, TLS_TYPE) \
118 (bfd_link_executable (INFO) \
119 && (H) != NULL \
120 && (H)->dynindx == -1 \
121 && (TLS_TYPE & GOT_TLS_IE))
122
123 /* x86 ELF linker hash entry. */
124
125 struct elf_x86_link_hash_entry
126 {
127 struct elf_link_hash_entry elf;
128
129 /* Track dynamic relocs copied for this symbol. */
130 struct elf_dyn_relocs *dyn_relocs;
131
132 unsigned char tls_type;
133
134 /* TRUE if symbol has GOT or PLT relocations. */
135 unsigned int has_got_reloc : 1;
136
137 /* TRUE if symbol has non-GOT/non-PLT relocations in text sections. */
138 unsigned int has_non_got_reloc : 1;
139
140 /* Don't call finish_dynamic_symbol on this symbol. */
141 unsigned int no_finish_dynamic_symbol : 1;
142
143 /* TRUE if symbol is __tls_get_addr. */
144 unsigned int tls_get_addr : 1;
145
146 /* TRUE if symbol is defined as a protected symbol. */
147 unsigned int def_protected : 1;
148
149 /* 0: Symbol references are unknown.
150 1: Symbol references aren't local.
151 2: Symbol references are local.
152 */
153 unsigned int local_ref : 2;
154
155 /* TRUE if symbol is defined by linker. */
156 unsigned int linker_def : 1;
157
158 /* TRUE if symbol is referenced by R_386_GOTOFF relocation. This is
159 only used by i386. */
160 unsigned int gotoff_ref : 1;
161
162 /* TRUE if a weak symbol with a real definition needs a copy reloc.
163 When there is a weak symbol with a real definition, the processor
164 independent code will have arranged for us to see the real
165 definition first. We need to copy the needs_copy bit from the
166 real definition and check it when allowing copy reloc in PIE. This
167 is only used by x86-64. */
168 unsigned int needs_copy : 1;
169
170 /* Reference count of C/C++ function pointer relocations in read-write
171 section which can be resolved at run-time. */
172 bfd_signed_vma func_pointer_refcount;
173
174 /* Information about the GOT PLT entry. Filled when there are both
175 GOT and PLT relocations against the same function. */
176 union gotplt_union plt_got;
177
178 /* Information about the second PLT entry. */
179 union gotplt_union plt_second;
180
181 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
182 starting at the end of the jump table. */
183 bfd_vma tlsdesc_got;
184 };
185
186 struct elf_x86_lazy_plt_layout
187 {
188 /* The first entry in an absolute lazy procedure linkage table looks
189 like this. */
190 const bfd_byte *plt0_entry;
191 unsigned int plt0_entry_size; /* Size of PLT0 entry. */
192
193 /* Later entries in an absolute lazy procedure linkage table look
194 like this. */
195 const bfd_byte *plt_entry;
196 unsigned int plt_entry_size; /* Size of each PLT entry. */
197
198 /* Offsets into plt0_entry that are to be replaced with GOT[1] and
199 GOT[2]. */
200 unsigned int plt0_got1_offset;
201 unsigned int plt0_got2_offset;
202
203 /* Offset of the end of the PC-relative instruction containing
204 plt0_got2_offset. This is for x86-64 only. */
205 unsigned int plt0_got2_insn_end;
206
207 /* Offsets into plt_entry that are to be replaced with... */
208 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
209 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
210 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
211
212 /* Length of the PC-relative instruction containing plt_got_offset.
213 This is used for x86-64 only. */
214 unsigned int plt_got_insn_size;
215
216 /* Offset of the end of the PC-relative jump to plt0_entry. This is
217 used for x86-64 only. */
218 unsigned int plt_plt_insn_end;
219
220 /* Offset into plt_entry where the initial value of the GOT entry
221 points. */
222 unsigned int plt_lazy_offset;
223
224 /* The first entry in a PIC lazy procedure linkage table looks like
225 this. */
226 const bfd_byte *pic_plt0_entry;
227
228 /* Subsequent entries in a PIC lazy procedure linkage table look
229 like this. */
230 const bfd_byte *pic_plt_entry;
231
232 /* .eh_frame covering the lazy .plt section. */
233 const bfd_byte *eh_frame_plt;
234 unsigned int eh_frame_plt_size;
235 };
236
237 struct elf_x86_non_lazy_plt_layout
238 {
239 /* Entries in an absolute non-lazy procedure linkage table look like
240 this. */
241 const bfd_byte *plt_entry;
242 /* Entries in a PIC non-lazy procedure linkage table look like this. */
243 const bfd_byte *pic_plt_entry;
244
245 unsigned int plt_entry_size; /* Size of each PLT entry. */
246
247 /* Offsets into plt_entry that are to be replaced with... */
248 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
249
250 /* Length of the PC-relative instruction containing plt_got_offset.
251 This is used for x86-64 only. */
252 unsigned int plt_got_insn_size;
253
254 /* .eh_frame covering the non-lazy .plt section. */
255 const bfd_byte *eh_frame_plt;
256 unsigned int eh_frame_plt_size;
257 };
258
259 struct elf_x86_plt_layout
260 {
261 /* The first entry in a lazy procedure linkage table looks like this.
262 This is only used for i386 where absolute PLT0 and PIC PLT0 are
263 different. */
264 const bfd_byte *plt0_entry;
265 /* Entries in a procedure linkage table look like this. */
266 const bfd_byte *plt_entry;
267 unsigned int plt_entry_size; /* Size of each PLT entry. */
268
269 /* 1 has PLT0. */
270 unsigned int has_plt0;
271
272 /* Offsets into plt_entry that are to be replaced with... */
273 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
274
275 /* Length of the PC-relative instruction containing plt_got_offset.
276 This is only used for x86-64. */
277 unsigned int plt_got_insn_size;
278
279 /* .eh_frame covering the .plt section. */
280 const bfd_byte *eh_frame_plt;
281 unsigned int eh_frame_plt_size;
282 };
283
284 /* Values in tls_type of x86 ELF linker hash entry. */
285 #define GOT_UNKNOWN 0
286 #define GOT_NORMAL 1
287 #define GOT_TLS_GD 2
288 #define GOT_TLS_IE 4
289 #define GOT_TLS_IE_POS 5
290 #define GOT_TLS_IE_NEG 6
291 #define GOT_TLS_IE_BOTH 7
292 #define GOT_TLS_GDESC 8
293 #define GOT_TLS_GD_BOTH_P(type) \
294 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
295 #define GOT_TLS_GD_P(type) \
296 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
297 #define GOT_TLS_GDESC_P(type) \
298 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
299 #define GOT_TLS_GD_ANY_P(type) \
300 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
301
302 #define elf_x86_hash_entry(ent) \
303 ((struct elf_x86_link_hash_entry *)(ent))
304
305 /* x86 ELF linker hash table. */
306
307 struct elf_x86_link_hash_table
308 {
309 struct elf_link_hash_table elf;
310
311 /* Short-cuts to get to dynamic linker sections. */
312 asection *interp;
313 asection *plt_eh_frame;
314 asection *plt_second;
315 asection *plt_second_eh_frame;
316 asection *plt_got;
317 asection *plt_got_eh_frame;
318
319 /* Parameters describing PLT generation, lazy or non-lazy. */
320 struct elf_x86_plt_layout plt;
321
322 /* Parameters describing lazy PLT generation. */
323 const struct elf_x86_lazy_plt_layout *lazy_plt;
324
325 /* Parameters describing non-lazy PLT generation. */
326 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
327
328 union
329 {
330 bfd_signed_vma refcount;
331 bfd_vma offset;
332 } tls_ld_or_ldm_got;
333
334 /* The amount of space used by the jump slots in the GOT. */
335 bfd_vma sgotplt_jump_table_size;
336
337 /* Small local sym cache. */
338 struct sym_cache sym_cache;
339
340 /* _TLS_MODULE_BASE_ symbol. */
341 struct bfd_link_hash_entry *tls_module_base;
342
343 /* Used by local STT_GNU_IFUNC symbols. */
344 htab_t loc_hash_table;
345 void * loc_hash_memory;
346
347 /* The offset into sgot of the GOT entry used by the PLT entry
348 above. */
349 bfd_vma tlsdesc_got;
350
351 /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
352 bfd_vma next_jump_slot_index;
353 /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
354 bfd_vma next_irelative_index;
355
356 /* TRUE if there are dynamic relocs against IFUNC symbols that apply
357 to read-only sections. */
358 bfd_boolean readonly_dynrelocs_against_ifunc;
359
360 /* TRUE if this is a VxWorks x86 target. This is only used for
361 i386. */
362 bfd_boolean is_vxworks;
363
364 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.
365 This is used for i386 only. */
366 asection *srelplt2;
367
368 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. This
369 is only used for i386. */
370 bfd_vma next_tls_desc_index;
371
372 /* The offset into splt of the PLT entry for the TLS descriptor
373 resolver. Special values are 0, if not necessary (or not found
374 to be necessary yet), and -1 if needed but not determined
375 yet. This is only used for x86-64. */
376 bfd_vma tlsdesc_plt;
377
378 bfd_vma (*r_info) (bfd_vma, bfd_vma);
379 bfd_vma (*r_sym) (bfd_vma);
380 bfd_boolean (*is_reloc_section) (const char *);
381 enum elf_target_id target_id;
382 unsigned int sizeof_reloc;
383 unsigned int dt_reloc;
384 unsigned int dt_reloc_sz;
385 unsigned int dt_reloc_ent;
386 unsigned int got_entry_size;
387 unsigned int pointer_r_type;
388 int dynamic_interpreter_size;
389 const char *dynamic_interpreter;
390 const char *tls_get_addr;
391 };
392
393 struct elf_x86_init_table
394 {
395 /* The lazy PLT layout. */
396 const struct elf_x86_lazy_plt_layout *lazy_plt;
397
398 /* The non-lazy PLT layout. */
399 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
400
401 /* The lazy PLT layout for IBT. */
402 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
403
404 /* The non-lazy PLT layout for IBT. */
405 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
406
407 /* TRUE if this is a normal x86 target. */
408 bfd_boolean normal_target;
409
410 /* TRUE if this is a VxWorks x86 target. */
411 bfd_boolean is_vxworks;
412
413 bfd_vma (*r_info) (bfd_vma, bfd_vma);
414 bfd_vma (*r_sym) (bfd_vma);
415 };
416
417 struct elf_x86_obj_tdata
418 {
419 struct elf_obj_tdata root;
420
421 /* tls_type for each local got entry. */
422 char *local_got_tls_type;
423
424 /* GOTPLT entries for TLS descriptors. */
425 bfd_vma *local_tlsdesc_gotent;
426 };
427
428 enum elf_x86_plt_type
429 {
430 plt_non_lazy = 0,
431 plt_lazy = 1 << 0,
432 plt_pic = 1 << 1,
433 plt_second = 1 << 2,
434 plt_unknown = -1
435 };
436
437 struct elf_x86_plt
438 {
439 const char *name;
440 asection *sec;
441 bfd_byte *contents;
442 enum elf_x86_plt_type type;
443 unsigned int plt_got_offset;
444 unsigned int plt_entry_size;
445 unsigned int plt_got_insn_size; /* Only used for x86-64. */
446 long count;
447 };
448
449 #define elf_x86_tdata(abfd) \
450 ((struct elf_x86_obj_tdata *) (abfd)->tdata.any)
451
452 #define elf_x86_local_got_tls_type(abfd) \
453 (elf_x86_tdata (abfd)->local_got_tls_type)
454
455 #define elf_x86_local_tlsdesc_gotent(abfd) \
456 (elf_x86_tdata (abfd)->local_tlsdesc_gotent)
457
458 #define elf_x86_compute_jump_table_size(htab) \
459 ((htab)->elf.srelplt->reloc_count * (htab)->got_entry_size)
460
461 #define is_x86_elf(bfd, htab) \
462 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
463 && elf_tdata (bfd) != NULL \
464 && elf_object_id (bfd) == (htab)->target_id)
465
466 extern bfd_boolean _bfd_x86_elf_mkobject
467 (bfd *);
468
469 extern void _bfd_x86_elf_set_tls_module_base
470 (struct bfd_link_info *);
471
472 extern bfd_vma _bfd_x86_elf_dtpoff_base
473 (struct bfd_link_info *);
474
475 extern bfd_boolean _bfd_x86_elf_readonly_dynrelocs
476 (struct elf_link_hash_entry *, void *);
477
478 extern struct elf_link_hash_entry * _bfd_elf_x86_get_local_sym_hash
479 (struct elf_x86_link_hash_table *, bfd *, const Elf_Internal_Rela *,
480 bfd_boolean);
481
482 extern hashval_t _bfd_x86_elf_local_htab_hash
483 (const void *);
484
485 extern int _bfd_x86_elf_local_htab_eq
486 (const void *, const void *);
487
488 extern struct bfd_hash_entry * _bfd_x86_elf_link_hash_newfunc
489 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
490
491 extern struct bfd_link_hash_table * _bfd_x86_elf_link_hash_table_create
492 (bfd *);
493
494 extern int _bfd_x86_elf_compare_relocs
495 (const void *, const void *);
496
497 extern bfd_boolean _bfd_x86_elf_link_check_relocs
498 (bfd *, struct bfd_link_info *);
499
500 extern bfd_boolean _bfd_x86_elf_size_dynamic_sections
501 (bfd *, struct bfd_link_info *);
502
503 extern bfd_boolean _bfd_x86_elf_always_size_sections
504 (bfd *, struct bfd_link_info *);
505
506 extern void _bfd_x86_elf_merge_symbol_attribute
507 (struct elf_link_hash_entry *, const Elf_Internal_Sym *,
508 bfd_boolean, bfd_boolean);
509
510 extern void _bfd_x86_elf_copy_indirect_symbol
511 (struct bfd_link_info *, struct elf_link_hash_entry *,
512 struct elf_link_hash_entry *);
513
514 extern bfd_boolean _bfd_x86_elf_fixup_symbol
515 (struct bfd_link_info *, struct elf_link_hash_entry *);
516
517 extern bfd_boolean _bfd_x86_elf_hash_symbol
518 (struct elf_link_hash_entry *);
519
520 extern bfd_boolean _bfd_x86_elf_adjust_dynamic_symbol
521 (struct bfd_link_info *, struct elf_link_hash_entry *);
522
523 extern bfd_boolean _bfd_x86_elf_link_symbol_references_local
524 (struct bfd_link_info *, struct elf_link_hash_entry *);
525
526 extern asection * _bfd_x86_elf_gc_mark_hook
527 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
528 struct elf_link_hash_entry *, Elf_Internal_Sym *);
529
530 extern long _bfd_x86_elf_get_synthetic_symtab
531 (bfd *, long, long, bfd_vma, struct elf_x86_plt [], asymbol **,
532 asymbol **);
533
534 extern enum elf_property_kind _bfd_x86_elf_parse_gnu_properties
535 (bfd *, unsigned int, bfd_byte *, unsigned int);
536
537 extern bfd_boolean _bfd_x86_elf_merge_gnu_properties
538 (struct bfd_link_info *, bfd *, elf_property *, elf_property *);
539
540 extern bfd * _bfd_x86_elf_link_setup_gnu_properties
541 (struct bfd_link_info *, struct elf_x86_init_table *);
542
543 #define bfd_elf64_mkobject \
544 _bfd_x86_elf_mkobject
545 #define bfd_elf32_mkobject \
546 _bfd_x86_elf_mkobject
547 #define bfd_elf64_bfd_link_hash_table_create \
548 _bfd_x86_elf_link_hash_table_create
549 #define bfd_elf32_bfd_link_hash_table_create \
550 _bfd_x86_elf_link_hash_table_create
551 #define bfd_elf64_bfd_link_check_relocs \
552 _bfd_x86_elf_link_check_relocs
553 #define bfd_elf32_bfd_link_check_relocs \
554 _bfd_x86_elf_link_check_relocs
555
556 #define elf_backend_size_dynamic_sections \
557 _bfd_x86_elf_size_dynamic_sections
558 #define elf_backend_always_size_sections \
559 _bfd_x86_elf_always_size_sections
560 #define elf_backend_merge_symbol_attribute \
561 _bfd_x86_elf_merge_symbol_attribute
562 #define elf_backend_copy_indirect_symbol \
563 _bfd_x86_elf_copy_indirect_symbol
564 #define elf_backend_fixup_symbol \
565 _bfd_x86_elf_fixup_symbol
566 #define elf_backend_hash_symbol \
567 _bfd_x86_elf_hash_symbol
568 #define elf_backend_adjust_dynamic_symbol \
569 _bfd_x86_elf_adjust_dynamic_symbol
570 #define elf_backend_gc_mark_hook \
571 _bfd_x86_elf_gc_mark_hook
572 #define elf_backend_omit_section_dynsym \
573 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
574 #define elf_backend_parse_gnu_properties \
575 _bfd_x86_elf_parse_gnu_properties
576 #define elf_backend_merge_gnu_properties \
577 _bfd_x86_elf_merge_gnu_properties