1 /* x86 specific support for ELF
2 Copyright (C) 2017 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. */
26 #include "bfd_stdint.h"
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
34 #define ABI_64_P(abfd) \
35 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
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
41 #define ELIMINATE_COPY_RELOCS 1
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)
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))
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
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.
59 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, ID, GOT_RELOC, EH) \
60 ((EH)->elf.root.type == bfd_link_hash_undefweak \
61 && (SYMBOL_REFERENCES_LOCAL_P ((INFO), &(EH)->elf) \
62 || (bfd_link_executable (INFO) \
63 && (elf_x86_hash_table ((INFO), (ID))->interp == NULL \
65 || (EH)->has_non_got_reloc \
66 || !(INFO)->dynamic_undefined_weak))))
68 /* Should copy relocation be generated for a symbol. Don't generate
69 copy relocation against a protected symbol defined in a shared
70 object with GNU_PROPERTY_NO_COPY_ON_PROTECTED. */
71 #define SYMBOL_NO_COPYRELOC(INFO, EH) \
72 ((EH)->def_protected \
73 && ((EH)->elf.root.type == bfd_link_hash_defined \
74 || (EH)->elf.root.type == bfd_link_hash_defweak) \
75 && elf_has_no_copy_on_protected ((EH)->elf.root.u.def.section->owner) \
76 && ((EH)->elf.root.u.def.section->owner->flags & DYNAMIC) != 0 \
77 && ((EH)->elf.root.u.def.section->flags & SEC_CODE) == 0)
79 /* x86 ELF linker hash entry. */
81 struct elf_x86_link_hash_entry
83 struct elf_link_hash_entry elf;
85 /* Track dynamic relocs copied for this symbol. */
86 struct elf_dyn_relocs *dyn_relocs;
88 unsigned char tls_type;
90 /* TRUE if symbol has GOT or PLT relocations. */
91 unsigned int has_got_reloc : 1;
93 /* TRUE if symbol has non-GOT/non-PLT relocations in text sections. */
94 unsigned int has_non_got_reloc : 1;
96 /* Don't call finish_dynamic_symbol on this symbol. */
97 unsigned int no_finish_dynamic_symbol : 1;
99 /* TRUE if symbol is __tls_get_addr. */
100 unsigned int tls_get_addr : 1;
102 /* TRUE if symbol is defined as a protected symbol. */
103 unsigned int def_protected : 1;
105 /* 0: Symbol references are unknown.
106 1: Symbol references aren't local.
107 2: Symbol references are local.
109 unsigned int local_ref : 2;
111 /* Terue if symbol is referenced by R_386_GOTOFF relocation. This is
112 only used by i386. */
113 unsigned int gotoff_ref : 1;
115 /* TRUE if a weak symbol with a real definition needs a copy reloc.
116 When there is a weak symbol with a real definition, the processor
117 independent code will have arranged for us to see the real
118 definition first. We need to copy the needs_copy bit from the
119 real definition and check it when allowing copy reloc in PIE. This
120 is only used by x86-64. */
121 unsigned int needs_copy : 1;
123 /* Reference count of C/C++ function pointer relocations in read-write
124 section which can be resolved at run-time. */
125 bfd_signed_vma func_pointer_refcount;
127 /* Information about the GOT PLT entry. Filled when there are both
128 GOT and PLT relocations against the same function. */
129 union gotplt_union plt_got;
131 /* Information about the second PLT entry. */
132 union gotplt_union plt_second;
134 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
135 starting at the end of the jump table. */
139 struct elf_x86_lazy_plt_layout
141 /* The first entry in an absolute lazy procedure linkage table looks
143 const bfd_byte *plt0_entry;
144 unsigned int plt0_entry_size; /* Size of PLT0 entry. */
146 /* Later entries in an absolute lazy procedure linkage table look
148 const bfd_byte *plt_entry;
149 unsigned int plt_entry_size; /* Size of each PLT entry. */
151 /* Offsets into plt0_entry that are to be replaced with GOT[1] and
153 unsigned int plt0_got1_offset;
154 unsigned int plt0_got2_offset;
156 /* Offset of the end of the PC-relative instruction containing
157 plt0_got2_offset. This is for x86-64 only. */
158 unsigned int plt0_got2_insn_end;
160 /* Offsets into plt_entry that are to be replaced with... */
161 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
162 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
163 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
165 /* Length of the PC-relative instruction containing plt_got_offset.
166 This is used for x86-64 only. */
167 unsigned int plt_got_insn_size;
169 /* Offset of the end of the PC-relative jump to plt0_entry. This is
170 used for x86-64 only. */
171 unsigned int plt_plt_insn_end;
173 /* Offset into plt_entry where the initial value of the GOT entry
175 unsigned int plt_lazy_offset;
177 /* The first entry in a PIC lazy procedure linkage table looks like
179 const bfd_byte *pic_plt0_entry;
181 /* Subsequent entries in a PIC lazy procedure linkage table look
183 const bfd_byte *pic_plt_entry;
185 /* .eh_frame covering the lazy .plt section. */
186 const bfd_byte *eh_frame_plt;
187 unsigned int eh_frame_plt_size;
190 struct elf_x86_non_lazy_plt_layout
192 /* Entries in an absolute non-lazy procedure linkage table look like
194 const bfd_byte *plt_entry;
195 /* Entries in a PIC non-lazy procedure linkage table look like this. */
196 const bfd_byte *pic_plt_entry;
198 unsigned int plt_entry_size; /* Size of each PLT entry. */
200 /* Offsets into plt_entry that are to be replaced with... */
201 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
203 /* Length of the PC-relative instruction containing plt_got_offset.
204 This is used for x86-64 only. */
205 unsigned int plt_got_insn_size;
207 /* .eh_frame covering the non-lazy .plt section. */
208 const bfd_byte *eh_frame_plt;
209 unsigned int eh_frame_plt_size;
212 struct elf_x86_plt_layout
214 /* The first entry in a lazy procedure linkage table looks like this.
215 This is only used for i386 where absolute PLT0 and PIC PLT0 are
217 const bfd_byte *plt0_entry;
218 /* Entries in a procedure linkage table look like this. */
219 const bfd_byte *plt_entry;
220 unsigned int plt_entry_size; /* Size of each PLT entry. */
223 unsigned int has_plt0;
225 /* Offsets into plt_entry that are to be replaced with... */
226 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
228 /* Length of the PC-relative instruction containing plt_got_offset.
229 This is only used for x86-64. */
230 unsigned int plt_got_insn_size;
232 /* .eh_frame covering the .plt section. */
233 const bfd_byte *eh_frame_plt;
234 unsigned int eh_frame_plt_size;
237 /* Values in tls_type of x86 ELF linker hash entry. */
238 #define GOT_UNKNOWN 0
242 #define GOT_TLS_IE_POS 5
243 #define GOT_TLS_IE_NEG 6
244 #define GOT_TLS_IE_BOTH 7
245 #define GOT_TLS_GDESC 8
246 #define GOT_TLS_GD_BOTH_P(type) \
247 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
248 #define GOT_TLS_GD_P(type) \
249 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
250 #define GOT_TLS_GDESC_P(type) \
251 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
252 #define GOT_TLS_GD_ANY_P(type) \
253 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
255 #define elf_x86_hash_entry(ent) \
256 ((struct elf_x86_link_hash_entry *)(ent))
258 /* x86 ELF linker hash table. */
260 struct elf_x86_link_hash_table
262 struct elf_link_hash_table elf;
264 /* Short-cuts to get to dynamic linker sections. */
266 asection *plt_eh_frame;
267 asection *plt_second;
268 asection *plt_second_eh_frame;
270 asection *plt_got_eh_frame;
272 /* Parameters describing PLT generation, lazy or non-lazy. */
273 struct elf_x86_plt_layout plt;
275 /* Parameters describing lazy PLT generation. */
276 const struct elf_x86_lazy_plt_layout *lazy_plt;
278 /* Parameters describing non-lazy PLT generation. */
279 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
283 bfd_signed_vma refcount;
287 /* The amount of space used by the jump slots in the GOT. */
288 bfd_vma sgotplt_jump_table_size;
290 /* Small local sym cache. */
291 struct sym_cache sym_cache;
293 /* _TLS_MODULE_BASE_ symbol. */
294 struct bfd_link_hash_entry *tls_module_base;
296 /* Used by local STT_GNU_IFUNC symbols. */
297 htab_t loc_hash_table;
298 void * loc_hash_memory;
300 /* The offset into sgot of the GOT entry used by the PLT entry
304 /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
305 bfd_vma next_jump_slot_index;
306 /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
307 bfd_vma next_irelative_index;
309 /* TRUE if there are dynamic relocs against IFUNC symbols that apply
310 to read-only sections. */
311 bfd_boolean readonly_dynrelocs_against_ifunc;
313 /* TRUE if this is a VxWorks x86 target. This is only used for
315 bfd_boolean is_vxworks;
317 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.
318 This is used for i386 only. */
321 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. This
322 is only used for i386. */
323 bfd_vma next_tls_desc_index;
325 /* The offset into splt of the PLT entry for the TLS descriptor
326 resolver. Special values are 0, if not necessary (or not found
327 to be necessary yet), and -1 if needed but not determined
328 yet. This is only used for x86-64. */
331 bfd_vma (*r_info) (bfd_vma, bfd_vma);
332 bfd_vma (*r_sym) (bfd_vma);
333 bfd_boolean (*is_reloc_section) (const char *);
334 enum elf_target_id target_id;
335 unsigned int sizeof_reloc;
336 unsigned int dt_reloc;
337 unsigned int dt_reloc_sz;
338 unsigned int dt_reloc_ent;
339 unsigned int got_entry_size;
340 unsigned int pointer_r_type;
341 int dynamic_interpreter_size;
342 const char *dynamic_interpreter;
343 const char *tls_get_addr;
346 struct elf_x86_init_table
348 /* The lazy PLT layout. */
349 const struct elf_x86_lazy_plt_layout *lazy_plt;
351 /* The non-lazy PLT layout. */
352 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
354 /* The lazy PLT layout for IBT. */
355 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
357 /* The non-lazy PLT layout for IBT. */
358 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
360 /* TRUE if this is a normal x86 target. */
361 bfd_boolean normal_target;
363 /* TRUE if this is a VxWorks x86 target. */
364 bfd_boolean is_vxworks;
366 bfd_vma (*r_info) (bfd_vma, bfd_vma);
367 bfd_vma (*r_sym) (bfd_vma);
370 struct elf_x86_obj_tdata
372 struct elf_obj_tdata root;
374 /* tls_type for each local got entry. */
375 char *local_got_tls_type;
377 /* GOTPLT entries for TLS descriptors. */
378 bfd_vma *local_tlsdesc_gotent;
381 enum elf_x86_plt_type
395 enum elf_x86_plt_type type;
396 unsigned int plt_got_offset;
397 unsigned int plt_entry_size;
398 unsigned int plt_got_insn_size; /* Only used for x86-64. */
402 #define elf_x86_tdata(abfd) \
403 ((struct elf_x86_obj_tdata *) (abfd)->tdata.any)
405 #define elf_x86_local_got_tls_type(abfd) \
406 (elf_x86_tdata (abfd)->local_got_tls_type)
408 #define elf_x86_local_tlsdesc_gotent(abfd) \
409 (elf_x86_tdata (abfd)->local_tlsdesc_gotent)
411 #define elf_x86_compute_jump_table_size(htab) \
412 ((htab)->elf.srelplt->reloc_count * (htab)->got_entry_size)
414 #define is_x86_elf(bfd, htab) \
415 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
416 && elf_tdata (bfd) != NULL \
417 && elf_object_id (bfd) == (htab)->target_id)
419 extern bfd_boolean _bfd_x86_elf_mkobject
422 extern void _bfd_x86_elf_set_tls_module_base
423 (struct bfd_link_info *);
425 extern bfd_vma _bfd_x86_elf_dtpoff_base
426 (struct bfd_link_info *);
428 extern bfd_boolean _bfd_x86_elf_readonly_dynrelocs
429 (struct elf_link_hash_entry *, void *);
431 extern struct elf_link_hash_entry * _bfd_elf_x86_get_local_sym_hash
432 (struct elf_x86_link_hash_table *, bfd *, const Elf_Internal_Rela *,
435 extern hashval_t _bfd_x86_elf_local_htab_hash
438 extern int _bfd_x86_elf_local_htab_eq
439 (const void *, const void *);
441 extern struct bfd_hash_entry * _bfd_x86_elf_link_hash_newfunc
442 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
444 extern struct bfd_link_hash_table * _bfd_x86_elf_link_hash_table_create
447 extern int _bfd_x86_elf_compare_relocs
448 (const void *, const void *);
450 extern bfd_boolean _bfd_x86_elf_link_check_relocs
451 (bfd *, struct bfd_link_info *);
453 extern bfd_boolean _bfd_x86_elf_size_dynamic_sections
454 (bfd *, struct bfd_link_info *);
456 extern bfd_boolean _bfd_x86_elf_always_size_sections
457 (bfd *, struct bfd_link_info *);
459 extern void _bfd_x86_elf_merge_symbol_attribute
460 (struct elf_link_hash_entry *, const Elf_Internal_Sym *,
461 bfd_boolean, bfd_boolean);
463 extern void _bfd_x86_elf_copy_indirect_symbol
464 (struct bfd_link_info *, struct elf_link_hash_entry *,
465 struct elf_link_hash_entry *);
467 extern bfd_boolean _bfd_x86_elf_fixup_symbol
468 (struct bfd_link_info *, struct elf_link_hash_entry *);
470 extern bfd_boolean _bfd_x86_elf_hash_symbol
471 (struct elf_link_hash_entry *);
473 extern bfd_boolean _bfd_x86_elf_adjust_dynamic_symbol
474 (struct bfd_link_info *, struct elf_link_hash_entry *);
476 extern bfd_boolean _bfd_x86_elf_link_symbol_references_local
477 (struct bfd_link_info *, struct elf_link_hash_entry *);
479 extern asection * _bfd_x86_elf_gc_mark_hook
480 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
481 struct elf_link_hash_entry *, Elf_Internal_Sym *);
483 extern long _bfd_x86_elf_get_synthetic_symtab
484 (bfd *, long, long, bfd_vma, struct elf_x86_plt [], asymbol **,
487 extern enum elf_property_kind _bfd_x86_elf_parse_gnu_properties
488 (bfd *, unsigned int, bfd_byte *, unsigned int);
490 extern bfd_boolean _bfd_x86_elf_merge_gnu_properties
491 (struct bfd_link_info *, bfd *, elf_property *, elf_property *);
493 extern bfd * _bfd_x86_elf_link_setup_gnu_properties
494 (struct bfd_link_info *, struct elf_x86_init_table *);
496 #define bfd_elf64_mkobject \
497 _bfd_x86_elf_mkobject
498 #define bfd_elf32_mkobject \
499 _bfd_x86_elf_mkobject
500 #define bfd_elf64_bfd_link_hash_table_create \
501 _bfd_x86_elf_link_hash_table_create
502 #define bfd_elf32_bfd_link_hash_table_create \
503 _bfd_x86_elf_link_hash_table_create
504 #define bfd_elf64_bfd_link_check_relocs \
505 _bfd_x86_elf_link_check_relocs
506 #define bfd_elf32_bfd_link_check_relocs \
507 _bfd_x86_elf_link_check_relocs
509 #define elf_backend_size_dynamic_sections \
510 _bfd_x86_elf_size_dynamic_sections
511 #define elf_backend_always_size_sections \
512 _bfd_x86_elf_always_size_sections
513 #define elf_backend_merge_symbol_attribute \
514 _bfd_x86_elf_merge_symbol_attribute
515 #define elf_backend_copy_indirect_symbol \
516 _bfd_x86_elf_copy_indirect_symbol
517 #define elf_backend_fixup_symbol \
518 _bfd_x86_elf_fixup_symbol
519 #define elf_backend_hash_symbol \
520 _bfd_x86_elf_hash_symbol
521 #define elf_backend_adjust_dynamic_symbol \
522 _bfd_x86_elf_adjust_dynamic_symbol
523 #define elf_backend_gc_mark_hook \
524 _bfd_x86_elf_gc_mark_hook
525 #define elf_backend_omit_section_dynsym \
526 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
527 #define elf_backend_parse_gnu_properties \
528 _bfd_x86_elf_parse_gnu_properties
529 #define elf_backend_merge_gnu_properties \
530 _bfd_x86_elf_merge_gnu_properties