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 ABI_64_P(abfd) \
30 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
32 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
33 copying dynamic variables from a shared lib into an app's dynbss
34 section, and instead use a dynamic relocation to point into the
36 #define ELIMINATE_COPY_RELOCS 1
38 #define elf_x86_hash_table(p, id) \
39 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) == (id) \
40 ? ((struct elf_x86_link_hash_table *) ((p)->hash)) : NULL)
42 /* Is a undefined weak symbol which is resolved to 0. Reference to an
43 undefined weak symbol is resolved to 0 when building executable if
45 1. Has non-GOT/non-PLT relocations in text section. Or
46 2. Has no GOT/PLT relocation.
47 Local undefined weak symbol is always resolved to 0.
49 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, ID, GOT_RELOC, EH) \
50 ((EH)->elf.root.type == bfd_link_hash_undefweak \
51 && ((EH)->elf.forced_local \
52 || (bfd_link_executable (INFO) \
53 && (elf_x86_hash_table ((INFO), (ID))->interp == NULL \
55 || (EH)->has_non_got_reloc \
56 || !(INFO)->dynamic_undefined_weak))))
58 /* Should copy relocation be generated for a symbol. Don't generate
59 copy relocation against a protected symbol defined in a shared
60 object with GNU_PROPERTY_NO_COPY_ON_PROTECTED. */
61 #define SYMBOL_NO_COPYRELOC(INFO, EH) \
62 ((EH)->def_protected \
63 && ((EH)->elf.root.type == bfd_link_hash_defined \
64 || (EH)->elf.root.type == bfd_link_hash_defweak) \
65 && elf_has_no_copy_on_protected ((EH)->elf.root.u.def.section->owner) \
66 && ((EH)->elf.root.u.def.section->owner->flags & DYNAMIC) != 0 \
67 && ((EH)->elf.root.u.def.section->flags & SEC_CODE) == 0)
69 /* x86 ELF linker hash entry. */
71 struct elf_x86_link_hash_entry
73 struct elf_link_hash_entry elf
;
75 /* Track dynamic relocs copied for this symbol. */
76 struct elf_dyn_relocs
*dyn_relocs
;
78 unsigned char tls_type
;
80 /* TRUE if symbol has GOT or PLT relocations. */
81 unsigned int has_got_reloc
: 1;
83 /* TRUE if symbol has non-GOT/non-PLT relocations in text sections. */
84 unsigned int has_non_got_reloc
: 1;
86 /* Don't call finish_dynamic_symbol on this symbol. */
87 unsigned int no_finish_dynamic_symbol
: 1;
89 /* TRUE if symbol is __tls_get_addr. */
90 unsigned int tls_get_addr
: 1;
92 /* TRUE if symbol is defined as a protected symbol. */
93 unsigned int def_protected
: 1;
95 /* Symbol is referenced by R_386_GOTOFF relocation. This is only used
97 unsigned int gotoff_ref
: 1;
99 /* TRUE if a weak symbol with a real definition needs a copy reloc.
100 When there is a weak symbol with a real definition, the processor
101 independent code will have arranged for us to see the real
102 definition first. We need to copy the needs_copy bit from the
103 real definition and check it when allowing copy reloc in PIE. This
104 is only used by x86-64. */
105 unsigned int needs_copy
: 1;
107 /* Reference count of C/C++ function pointer relocations in read-write
108 section which can be resolved at run-time. */
109 bfd_signed_vma func_pointer_refcount
;
111 /* Information about the GOT PLT entry. Filled when there are both
112 GOT and PLT relocations against the same function. */
113 union gotplt_union plt_got
;
115 /* Information about the second PLT entry. */
116 union gotplt_union plt_second
;
118 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
119 starting at the end of the jump table. */
123 struct elf_x86_lazy_plt_layout
125 /* The first entry in an absolute lazy procedure linkage table looks
127 const bfd_byte
*plt0_entry
;
128 unsigned int plt0_entry_size
; /* Size of PLT0 entry. */
130 /* Later entries in an absolute lazy procedure linkage table look
132 const bfd_byte
*plt_entry
;
133 unsigned int plt_entry_size
; /* Size of each PLT entry. */
135 /* Offsets into plt0_entry that are to be replaced with GOT[1] and
137 unsigned int plt0_got1_offset
;
138 unsigned int plt0_got2_offset
;
140 /* Offset of the end of the PC-relative instruction containing
141 plt0_got2_offset. This is for x86-64 only. */
142 unsigned int plt0_got2_insn_end
;
144 /* Offsets into plt_entry that are to be replaced with... */
145 unsigned int plt_got_offset
; /* ... address of this symbol in .got. */
146 unsigned int plt_reloc_offset
; /* ... offset into relocation table. */
147 unsigned int plt_plt_offset
; /* ... offset to start of .plt. */
149 /* Length of the PC-relative instruction containing plt_got_offset.
150 This is used for x86-64 only. */
151 unsigned int plt_got_insn_size
;
153 /* Offset of the end of the PC-relative jump to plt0_entry. This is
154 used for x86-64 only. */
155 unsigned int plt_plt_insn_end
;
157 /* Offset into plt_entry where the initial value of the GOT entry
159 unsigned int plt_lazy_offset
;
161 /* The first entry in a PIC lazy procedure linkage table looks like
162 this. This is used for i386 only. */
163 const bfd_byte
*pic_plt0_entry
;
165 /* Subsequent entries in a PIC lazy procedure linkage table look
166 like this. This is used for i386 only. */
167 const bfd_byte
*pic_plt_entry
;
169 /* .eh_frame covering the lazy .plt section. */
170 const bfd_byte
*eh_frame_plt
;
171 unsigned int eh_frame_plt_size
;
174 struct elf_x86_non_lazy_plt_layout
176 /* Entries in an absolute non-lazy procedure linkage table look like
178 const bfd_byte
*plt_entry
;
179 /* Entries in a PIC non-lazy procedure linkage table look like this.
180 This is used for i386 only. */
181 const bfd_byte
*pic_plt_entry
;
183 unsigned int plt_entry_size
; /* Size of each PLT entry. */
185 /* Offsets into plt_entry that are to be replaced with... */
186 unsigned int plt_got_offset
; /* ... address of this symbol in .got. */
188 /* Length of the PC-relative instruction containing plt_got_offset.
189 This is used for x86-64 only. */
190 unsigned int plt_got_insn_size
;
192 /* .eh_frame covering the non-lazy .plt section. */
193 const bfd_byte
*eh_frame_plt
;
194 unsigned int eh_frame_plt_size
;
197 struct elf_x86_plt_layout
199 /* The first entry in a lazy procedure linkage table looks like this.
200 This is only used for i386 where absolute PLT0 and PIC PLT0 are
202 const bfd_byte
*plt0_entry
;
203 /* Entries in a procedure linkage table look like this. */
204 const bfd_byte
*plt_entry
;
205 unsigned int plt_entry_size
; /* Size of each PLT entry. */
208 unsigned int has_plt0
;
210 /* Offsets into plt_entry that are to be replaced with... */
211 unsigned int plt_got_offset
; /* ... address of this symbol in .got. */
213 /* Length of the PC-relative instruction containing plt_got_offset.
214 This is only used for x86-64. */
215 unsigned int plt_got_insn_size
;
217 /* .eh_frame covering the .plt section. */
218 const bfd_byte
*eh_frame_plt
;
219 unsigned int eh_frame_plt_size
;
222 /* The first 3 values in tls_type of x86 ELF linker hash entry. */
223 #define GOT_UNKNOWN 0
227 #define elf_x86_hash_entry(ent) \
228 ((struct elf_x86_link_hash_entry *)(ent))
230 /* x86 ELF linker hash table. */
232 struct elf_x86_link_hash_table
234 struct elf_link_hash_table elf
;
236 /* Short-cuts to get to dynamic linker sections. */
238 asection
*plt_eh_frame
;
239 asection
*plt_second
;
240 asection
*plt_second_eh_frame
;
242 asection
*plt_got_eh_frame
;
244 /* Parameters describing PLT generation, lazy or non-lazy. */
245 struct elf_x86_plt_layout plt
;
247 /* Parameters describing lazy PLT generation. */
248 const struct elf_x86_lazy_plt_layout
*lazy_plt
;
250 /* Parameters describing non-lazy PLT generation. */
251 const struct elf_x86_non_lazy_plt_layout
*non_lazy_plt
;
255 bfd_signed_vma refcount
;
259 /* The amount of space used by the jump slots in the GOT. */
260 bfd_vma sgotplt_jump_table_size
;
262 /* Small local sym cache. */
263 struct sym_cache sym_cache
;
265 /* _TLS_MODULE_BASE_ symbol. */
266 struct bfd_link_hash_entry
*tls_module_base
;
268 /* Used by local STT_GNU_IFUNC symbols. */
269 htab_t loc_hash_table
;
270 void * loc_hash_memory
;
272 /* The offset into splt of the PLT entry for the TLS descriptor
273 resolver. Special values are 0, if not necessary (or not found
274 to be necessary yet), and -1 if needed but not determined
278 /* The offset into sgot of the GOT entry used by the PLT entry
282 /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
283 bfd_vma next_jump_slot_index
;
284 /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
285 bfd_vma next_irelative_index
;
287 /* TRUE if there are dynamic relocs against IFUNC symbols that apply
288 to read-only sections. */
289 bfd_boolean readonly_dynrelocs_against_ifunc
;
291 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.
292 This is used for i386 only. */
295 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. This
296 is used for i386 only. */
297 bfd_vma next_tls_desc_index
;
299 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
300 bfd_vma (*r_sym
) (bfd_vma
);
301 unsigned int pointer_r_type
;
302 int dynamic_interpreter_size
;
303 const char *dynamic_interpreter
;
304 const char *tls_get_addr
;
307 struct elf_x86_obj_tdata
309 struct elf_obj_tdata root
;
311 /* tls_type for each local got entry. */
312 char *local_got_tls_type
;
314 /* GOTPLT entries for TLS descriptors. */
315 bfd_vma
*local_tlsdesc_gotent
;
318 enum elf_x86_plt_type
332 enum elf_x86_plt_type type
;
333 unsigned int plt_got_offset
;
334 unsigned int plt_entry_size
;
335 unsigned int plt_got_insn_size
; /* Only used for x86-64. */
339 #define elf_x86_tdata(abfd) \
340 ((struct elf_x86_obj_tdata *) (abfd)->tdata.any)
342 #define elf_x86_local_got_tls_type(abfd) \
343 (elf_x86_tdata (abfd)->local_got_tls_type)
345 #define elf_x86_local_tlsdesc_gotent(abfd) \
346 (elf_x86_tdata (abfd)->local_tlsdesc_gotent)
348 extern void _bfd_x86_elf_set_tls_module_base
349 (struct bfd_link_info
*);
351 extern bfd_vma _bfd_x86_elf_dtpoff_base
352 (struct bfd_link_info
*);
354 extern bfd_boolean _bfd_x86_elf_readonly_dynrelocs
355 (struct elf_link_hash_entry
*, void *);
357 extern struct elf_link_hash_entry
* _bfd_elf_x86_get_local_sym_hash
358 (struct elf_x86_link_hash_table
*, bfd
*, const Elf_Internal_Rela
*,
361 extern hashval_t _bfd_x86_elf_local_htab_hash
364 extern int _bfd_x86_elf_local_htab_eq
365 (const void *, const void *);
367 extern struct bfd_hash_entry
* _bfd_x86_elf_link_hash_newfunc
368 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
370 extern struct bfd_link_hash_table
* _bfd_x86_elf_link_hash_table_create
373 extern int _bfd_x86_elf_compare_relocs
374 (const void *, const void *);
376 extern bfd_boolean _bfd_x86_elf_link_check_relocs
377 (bfd
*, struct bfd_link_info
*);
379 extern bfd_boolean _bfd_x86_elf_always_size_sections
380 (bfd
*, struct bfd_link_info
*);
382 extern void _bfd_x86_elf_merge_symbol_attribute
383 (struct elf_link_hash_entry
*, const Elf_Internal_Sym
*,
384 bfd_boolean
, bfd_boolean
);
386 extern void _bfd_x86_elf_copy_indirect_symbol
387 (struct bfd_link_info
*, struct elf_link_hash_entry
*,
388 struct elf_link_hash_entry
*);
390 extern bfd_boolean _bfd_x86_elf_fixup_symbol
391 (struct bfd_link_info
*, struct elf_link_hash_entry
*);
393 extern bfd_boolean _bfd_x86_elf_hash_symbol
394 (struct elf_link_hash_entry
*);
396 extern long _bfd_x86_elf_get_synthetic_symtab
397 (bfd
*, long, long, bfd_vma
, struct elf_x86_plt
[], asymbol
**,
400 extern enum elf_property_kind _bfd_x86_elf_parse_gnu_properties
401 (bfd
*, unsigned int, bfd_byte
*, unsigned int);
403 extern bfd_boolean _bfd_x86_elf_merge_gnu_properties
404 (struct bfd_link_info
*, bfd
*, elf_property
*, elf_property
*);
406 #define bfd_elf64_bfd_link_hash_table_create \
407 _bfd_x86_elf_link_hash_table_create
408 #define bfd_elf32_bfd_link_hash_table_create \
409 _bfd_x86_elf_link_hash_table_create
410 #define bfd_elf64_bfd_link_check_relocs \
411 _bfd_x86_elf_link_check_relocs
412 #define bfd_elf32_bfd_link_check_relocs \
413 _bfd_x86_elf_link_check_relocs
415 #define elf_backend_always_size_sections \
416 _bfd_x86_elf_always_size_sections
417 #define elf_backend_merge_symbol_attribute \
418 _bfd_x86_elf_merge_symbol_attribute
419 #define elf_backend_copy_indirect_symbol \
420 _bfd_x86_elf_copy_indirect_symbol
421 #define elf_backend_fixup_symbol \
422 _bfd_x86_elf_fixup_symbol
423 #define elf_backend_hash_symbol \
424 _bfd_x86_elf_hash_symbol
425 #define elf_backend_omit_section_dynsym \
426 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
427 #define elf_backend_parse_gnu_properties \
428 _bfd_x86_elf_parse_gnu_properties
429 #define elf_backend_merge_gnu_properties \
430 _bfd_x86_elf_merge_gnu_properties