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 /* Is a undefined weak symbol which is resolved to 0. Reference to an
49 undefined weak symbol is resolved to 0 when building executable if
51 1. Has non-GOT/non-PLT relocations in text section. Or
52 2. Has no GOT/PLT relocation.
53 Local undefined weak symbol is always resolved to 0.
55 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, ID, GOT_RELOC, EH) \
56 ((EH)->elf.root.type == bfd_link_hash_undefweak \
57 && ((EH)->elf.forced_local \
58 || (bfd_link_executable (INFO) \
59 && (elf_x86_hash_table ((INFO), (ID))->interp == NULL \
61 || (EH)->has_non_got_reloc \
62 || !(INFO)->dynamic_undefined_weak))))
64 /* Should copy relocation be generated for a symbol. Don't generate
65 copy relocation against a protected symbol defined in a shared
66 object with GNU_PROPERTY_NO_COPY_ON_PROTECTED. */
67 #define SYMBOL_NO_COPYRELOC(INFO, EH) \
68 ((EH)->def_protected \
69 && ((EH)->elf.root.type == bfd_link_hash_defined \
70 || (EH)->elf.root.type == bfd_link_hash_defweak) \
71 && elf_has_no_copy_on_protected ((EH)->elf.root.u.def.section->owner) \
72 && ((EH)->elf.root.u.def.section->owner->flags & DYNAMIC) != 0 \
73 && ((EH)->elf.root.u.def.section->flags & SEC_CODE) == 0)
75 /* x86 ELF linker hash entry. */
77 struct elf_x86_link_hash_entry
79 struct elf_link_hash_entry elf;
81 /* Track dynamic relocs copied for this symbol. */
82 struct elf_dyn_relocs *dyn_relocs;
84 unsigned char tls_type;
86 /* TRUE if symbol has GOT or PLT relocations. */
87 unsigned int has_got_reloc : 1;
89 /* TRUE if symbol has non-GOT/non-PLT relocations in text sections. */
90 unsigned int has_non_got_reloc : 1;
92 /* Don't call finish_dynamic_symbol on this symbol. */
93 unsigned int no_finish_dynamic_symbol : 1;
95 /* TRUE if symbol is __tls_get_addr. */
96 unsigned int tls_get_addr : 1;
98 /* TRUE if symbol is defined as a protected symbol. */
99 unsigned int def_protected : 1;
101 /* Symbol is referenced by R_386_GOTOFF relocation. This is only used
103 unsigned int gotoff_ref : 1;
105 /* TRUE if a weak symbol with a real definition needs a copy reloc.
106 When there is a weak symbol with a real definition, the processor
107 independent code will have arranged for us to see the real
108 definition first. We need to copy the needs_copy bit from the
109 real definition and check it when allowing copy reloc in PIE. This
110 is only used by x86-64. */
111 unsigned int needs_copy : 1;
113 /* Reference count of C/C++ function pointer relocations in read-write
114 section which can be resolved at run-time. */
115 bfd_signed_vma func_pointer_refcount;
117 /* Information about the GOT PLT entry. Filled when there are both
118 GOT and PLT relocations against the same function. */
119 union gotplt_union plt_got;
121 /* Information about the second PLT entry. */
122 union gotplt_union plt_second;
124 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
125 starting at the end of the jump table. */
129 struct elf_x86_lazy_plt_layout
131 /* The first entry in an absolute lazy procedure linkage table looks
133 const bfd_byte *plt0_entry;
134 unsigned int plt0_entry_size; /* Size of PLT0 entry. */
136 /* Later entries in an absolute lazy procedure linkage table look
138 const bfd_byte *plt_entry;
139 unsigned int plt_entry_size; /* Size of each PLT entry. */
141 /* Offsets into plt0_entry that are to be replaced with GOT[1] and
143 unsigned int plt0_got1_offset;
144 unsigned int plt0_got2_offset;
146 /* Offset of the end of the PC-relative instruction containing
147 plt0_got2_offset. This is for x86-64 only. */
148 unsigned int plt0_got2_insn_end;
150 /* Offsets into plt_entry that are to be replaced with... */
151 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
152 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
153 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
155 /* Length of the PC-relative instruction containing plt_got_offset.
156 This is used for x86-64 only. */
157 unsigned int plt_got_insn_size;
159 /* Offset of the end of the PC-relative jump to plt0_entry. This is
160 used for x86-64 only. */
161 unsigned int plt_plt_insn_end;
163 /* Offset into plt_entry where the initial value of the GOT entry
165 unsigned int plt_lazy_offset;
167 /* The first entry in a PIC lazy procedure linkage table looks like
169 const bfd_byte *pic_plt0_entry;
171 /* Subsequent entries in a PIC lazy procedure linkage table look
173 const bfd_byte *pic_plt_entry;
175 /* .eh_frame covering the lazy .plt section. */
176 const bfd_byte *eh_frame_plt;
177 unsigned int eh_frame_plt_size;
180 struct elf_x86_non_lazy_plt_layout
182 /* Entries in an absolute non-lazy procedure linkage table look like
184 const bfd_byte *plt_entry;
185 /* Entries in a PIC non-lazy procedure linkage table look like this. */
186 const bfd_byte *pic_plt_entry;
188 unsigned int plt_entry_size; /* Size of each PLT entry. */
190 /* Offsets into plt_entry that are to be replaced with... */
191 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
193 /* Length of the PC-relative instruction containing plt_got_offset.
194 This is used for x86-64 only. */
195 unsigned int plt_got_insn_size;
197 /* .eh_frame covering the non-lazy .plt section. */
198 const bfd_byte *eh_frame_plt;
199 unsigned int eh_frame_plt_size;
202 struct elf_x86_plt_layout
204 /* The first entry in a lazy procedure linkage table looks like this.
205 This is only used for i386 where absolute PLT0 and PIC PLT0 are
207 const bfd_byte *plt0_entry;
208 /* Entries in a procedure linkage table look like this. */
209 const bfd_byte *plt_entry;
210 unsigned int plt_entry_size; /* Size of each PLT entry. */
213 unsigned int has_plt0;
215 /* Offsets into plt_entry that are to be replaced with... */
216 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
218 /* Length of the PC-relative instruction containing plt_got_offset.
219 This is only used for x86-64. */
220 unsigned int plt_got_insn_size;
222 /* .eh_frame covering the .plt section. */
223 const bfd_byte *eh_frame_plt;
224 unsigned int eh_frame_plt_size;
227 /* Values in tls_type of x86 ELF linker hash entry. */
228 #define GOT_UNKNOWN 0
232 #define GOT_TLS_IE_POS 5
233 #define GOT_TLS_IE_NEG 6
234 #define GOT_TLS_IE_BOTH 7
235 #define GOT_TLS_GDESC 8
236 #define GOT_TLS_GD_BOTH_P(type) \
237 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
238 #define GOT_TLS_GD_P(type) \
239 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
240 #define GOT_TLS_GDESC_P(type) \
241 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
242 #define GOT_TLS_GD_ANY_P(type) \
243 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
245 #define elf_x86_hash_entry(ent) \
246 ((struct elf_x86_link_hash_entry *)(ent))
248 /* x86 ELF linker hash table. */
250 struct elf_x86_link_hash_table
252 struct elf_link_hash_table elf;
254 /* Short-cuts to get to dynamic linker sections. */
256 asection *plt_eh_frame;
257 asection *plt_second;
258 asection *plt_second_eh_frame;
260 asection *plt_got_eh_frame;
262 /* Parameters describing PLT generation, lazy or non-lazy. */
263 struct elf_x86_plt_layout plt;
265 /* Parameters describing lazy PLT generation. */
266 const struct elf_x86_lazy_plt_layout *lazy_plt;
268 /* Parameters describing non-lazy PLT generation. */
269 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
273 bfd_signed_vma refcount;
277 /* The amount of space used by the jump slots in the GOT. */
278 bfd_vma sgotplt_jump_table_size;
280 /* Small local sym cache. */
281 struct sym_cache sym_cache;
283 /* _TLS_MODULE_BASE_ symbol. */
284 struct bfd_link_hash_entry *tls_module_base;
286 /* Used by local STT_GNU_IFUNC symbols. */
287 htab_t loc_hash_table;
288 void * loc_hash_memory;
290 /* The offset into sgot of the GOT entry used by the PLT entry
294 /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
295 bfd_vma next_jump_slot_index;
296 /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
297 bfd_vma next_irelative_index;
299 /* TRUE if there are dynamic relocs against IFUNC symbols that apply
300 to read-only sections. */
301 bfd_boolean readonly_dynrelocs_against_ifunc;
303 /* TRUE if this is a VxWorks x86 target. This is only used for
305 bfd_boolean is_vxworks;
307 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.
308 This is used for i386 only. */
311 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. This
312 is only used for i386. */
313 bfd_vma next_tls_desc_index;
315 /* The offset into splt of the PLT entry for the TLS descriptor
316 resolver. Special values are 0, if not necessary (or not found
317 to be necessary yet), and -1 if needed but not determined
318 yet. This is only used for x86-64. */
321 bfd_vma (*r_info) (bfd_vma, bfd_vma);
322 bfd_vma (*r_sym) (bfd_vma);
323 bfd_boolean (*convert_load) (bfd *, asection *,
324 struct bfd_link_info *);
325 bfd_boolean (*is_reloc_section) (const char *);
326 enum elf_target_id target_id;
327 unsigned int sizeof_reloc;
328 unsigned int dt_reloc;
329 unsigned int dt_reloc_sz;
330 unsigned int dt_reloc_ent;
331 unsigned int got_entry_size;
332 unsigned int pointer_r_type;
333 int dynamic_interpreter_size;
334 const char *dynamic_interpreter;
335 const char *tls_get_addr;
338 struct elf_x86_plt_layout_table
340 /* The lazy PLT layout. */
341 const struct elf_x86_lazy_plt_layout *lazy_plt;
343 /* The non-lazy PLT layout. */
344 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
346 /* The lazy PLT layout for IBT. */
347 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
349 /* The non-lazy PLT layout for IBT. */
350 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
352 /* TRUE if this is a normal x86 target. */
353 bfd_boolean normal_target;
355 /* TRUE if this is a VxWorks x86 target. */
356 bfd_boolean is_vxworks;
359 struct elf_x86_obj_tdata
361 struct elf_obj_tdata root;
363 /* tls_type for each local got entry. */
364 char *local_got_tls_type;
366 /* GOTPLT entries for TLS descriptors. */
367 bfd_vma *local_tlsdesc_gotent;
370 enum elf_x86_plt_type
384 enum elf_x86_plt_type type;
385 unsigned int plt_got_offset;
386 unsigned int plt_entry_size;
387 unsigned int plt_got_insn_size; /* Only used for x86-64. */
391 #define elf_x86_tdata(abfd) \
392 ((struct elf_x86_obj_tdata *) (abfd)->tdata.any)
394 #define elf_x86_local_got_tls_type(abfd) \
395 (elf_x86_tdata (abfd)->local_got_tls_type)
397 #define elf_x86_local_tlsdesc_gotent(abfd) \
398 (elf_x86_tdata (abfd)->local_tlsdesc_gotent)
400 #define elf_x86_compute_jump_table_size(htab) \
401 ((htab)->elf.srelplt->reloc_count * (htab)->got_entry_size)
403 #define is_x86_elf(bfd, htab) \
404 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
405 && elf_tdata (bfd) != NULL \
406 && elf_object_id (bfd) == (htab)->target_id)
408 extern bfd_boolean _bfd_i386_elf_convert_load
409 (bfd *, asection *, struct bfd_link_info *);
411 extern bfd_boolean _bfd_x86_64_elf_convert_load
412 (bfd *, asection *, struct bfd_link_info *);
414 extern bfd_boolean _bfd_x86_elf_mkobject
417 extern void _bfd_x86_elf_set_tls_module_base
418 (struct bfd_link_info *);
420 extern bfd_vma _bfd_x86_elf_dtpoff_base
421 (struct bfd_link_info *);
423 extern bfd_boolean _bfd_x86_elf_readonly_dynrelocs
424 (struct elf_link_hash_entry *, void *);
426 extern struct elf_link_hash_entry * _bfd_elf_x86_get_local_sym_hash
427 (struct elf_x86_link_hash_table *, bfd *, const Elf_Internal_Rela *,
430 extern hashval_t _bfd_x86_elf_local_htab_hash
433 extern int _bfd_x86_elf_local_htab_eq
434 (const void *, const void *);
436 extern struct bfd_hash_entry * _bfd_x86_elf_link_hash_newfunc
437 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
439 extern struct bfd_link_hash_table * _bfd_x86_elf_link_hash_table_create
442 extern int _bfd_x86_elf_compare_relocs
443 (const void *, const void *);
445 extern bfd_boolean _bfd_x86_elf_link_check_relocs
446 (bfd *, struct bfd_link_info *);
448 extern bfd_boolean _bfd_x86_elf_size_dynamic_sections
449 (bfd *, struct bfd_link_info *);
451 extern bfd_boolean _bfd_x86_elf_always_size_sections
452 (bfd *, struct bfd_link_info *);
454 extern void _bfd_x86_elf_merge_symbol_attribute
455 (struct elf_link_hash_entry *, const Elf_Internal_Sym *,
456 bfd_boolean, bfd_boolean);
458 extern void _bfd_x86_elf_copy_indirect_symbol
459 (struct bfd_link_info *, struct elf_link_hash_entry *,
460 struct elf_link_hash_entry *);
462 extern bfd_boolean _bfd_x86_elf_fixup_symbol
463 (struct bfd_link_info *, struct elf_link_hash_entry *);
465 extern bfd_boolean _bfd_x86_elf_hash_symbol
466 (struct elf_link_hash_entry *);
468 extern bfd_boolean _bfd_x86_elf_adjust_dynamic_symbol
469 (struct bfd_link_info *, struct elf_link_hash_entry *);
471 extern asection * _bfd_x86_elf_gc_mark_hook
472 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
473 struct elf_link_hash_entry *, Elf_Internal_Sym *);
475 extern long _bfd_x86_elf_get_synthetic_symtab
476 (bfd *, long, long, bfd_vma, struct elf_x86_plt [], asymbol **,
479 extern enum elf_property_kind _bfd_x86_elf_parse_gnu_properties
480 (bfd *, unsigned int, bfd_byte *, unsigned int);
482 extern bfd_boolean _bfd_x86_elf_merge_gnu_properties
483 (struct bfd_link_info *, bfd *, elf_property *, elf_property *);
485 extern bfd * _bfd_x86_elf_link_setup_gnu_properties
486 (struct bfd_link_info *, struct elf_x86_plt_layout_table *);
488 #define bfd_elf64_mkobject \
489 _bfd_x86_elf_mkobject
490 #define bfd_elf32_mkobject \
491 _bfd_x86_elf_mkobject
492 #define bfd_elf64_bfd_link_hash_table_create \
493 _bfd_x86_elf_link_hash_table_create
494 #define bfd_elf32_bfd_link_hash_table_create \
495 _bfd_x86_elf_link_hash_table_create
496 #define bfd_elf64_bfd_link_check_relocs \
497 _bfd_x86_elf_link_check_relocs
498 #define bfd_elf32_bfd_link_check_relocs \
499 _bfd_x86_elf_link_check_relocs
501 #define elf_backend_size_dynamic_sections \
502 _bfd_x86_elf_size_dynamic_sections
503 #define elf_backend_always_size_sections \
504 _bfd_x86_elf_always_size_sections
505 #define elf_backend_merge_symbol_attribute \
506 _bfd_x86_elf_merge_symbol_attribute
507 #define elf_backend_copy_indirect_symbol \
508 _bfd_x86_elf_copy_indirect_symbol
509 #define elf_backend_fixup_symbol \
510 _bfd_x86_elf_fixup_symbol
511 #define elf_backend_hash_symbol \
512 _bfd_x86_elf_hash_symbol
513 #define elf_backend_adjust_dynamic_symbol \
514 _bfd_x86_elf_adjust_dynamic_symbol
515 #define elf_backend_gc_mark_hook \
516 _bfd_x86_elf_gc_mark_hook
517 #define elf_backend_omit_section_dynsym \
518 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
519 #define elf_backend_parse_gnu_properties \
520 _bfd_x86_elf_parse_gnu_properties
521 #define elf_backend_merge_gnu_properties \
522 _bfd_x86_elf_merge_gnu_properties