]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elfxx-x86.h
x86: Properly handle __ehdr_start
[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, 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 \
64 || !(GOT_RELOC) \
65 || (EH)->has_non_got_reloc \
66 || !(INFO)->dynamic_undefined_weak))))
67
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)
78
79 /* x86 ELF linker hash entry. */
80
81 struct elf_x86_link_hash_entry
82 {
83 struct elf_link_hash_entry elf;
84
85 /* Track dynamic relocs copied for this symbol. */
86 struct elf_dyn_relocs *dyn_relocs;
87
88 unsigned char tls_type;
89
90 /* TRUE if symbol has GOT or PLT relocations. */
91 unsigned int has_got_reloc : 1;
92
93 /* TRUE if symbol has non-GOT/non-PLT relocations in text sections. */
94 unsigned int has_non_got_reloc : 1;
95
96 /* Don't call finish_dynamic_symbol on this symbol. */
97 unsigned int no_finish_dynamic_symbol : 1;
98
99 /* TRUE if symbol is __tls_get_addr. */
100 unsigned int tls_get_addr : 1;
101
102 /* TRUE if symbol is defined as a protected symbol. */
103 unsigned int def_protected : 1;
104
105 /* 0: Symbol references are unknown.
106 1: Symbol references aren't local.
107 2: Symbol references are local.
108 */
109 unsigned int local_ref : 2;
110
111 /* TRUE if symbol is defined by linker. */
112 unsigned int linker_def : 1;
113
114 /* Terue if symbol is referenced by R_386_GOTOFF relocation. This is
115 only used by i386. */
116 unsigned int gotoff_ref : 1;
117
118 /* TRUE if a weak symbol with a real definition needs a copy reloc.
119 When there is a weak symbol with a real definition, the processor
120 independent code will have arranged for us to see the real
121 definition first. We need to copy the needs_copy bit from the
122 real definition and check it when allowing copy reloc in PIE. This
123 is only used by x86-64. */
124 unsigned int needs_copy : 1;
125
126 /* Reference count of C/C++ function pointer relocations in read-write
127 section which can be resolved at run-time. */
128 bfd_signed_vma func_pointer_refcount;
129
130 /* Information about the GOT PLT entry. Filled when there are both
131 GOT and PLT relocations against the same function. */
132 union gotplt_union plt_got;
133
134 /* Information about the second PLT entry. */
135 union gotplt_union plt_second;
136
137 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
138 starting at the end of the jump table. */
139 bfd_vma tlsdesc_got;
140 };
141
142 struct elf_x86_lazy_plt_layout
143 {
144 /* The first entry in an absolute lazy procedure linkage table looks
145 like this. */
146 const bfd_byte *plt0_entry;
147 unsigned int plt0_entry_size; /* Size of PLT0 entry. */
148
149 /* Later entries in an absolute lazy procedure linkage table look
150 like this. */
151 const bfd_byte *plt_entry;
152 unsigned int plt_entry_size; /* Size of each PLT entry. */
153
154 /* Offsets into plt0_entry that are to be replaced with GOT[1] and
155 GOT[2]. */
156 unsigned int plt0_got1_offset;
157 unsigned int plt0_got2_offset;
158
159 /* Offset of the end of the PC-relative instruction containing
160 plt0_got2_offset. This is for x86-64 only. */
161 unsigned int plt0_got2_insn_end;
162
163 /* Offsets into plt_entry that are to be replaced with... */
164 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
165 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
166 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
167
168 /* Length of the PC-relative instruction containing plt_got_offset.
169 This is used for x86-64 only. */
170 unsigned int plt_got_insn_size;
171
172 /* Offset of the end of the PC-relative jump to plt0_entry. This is
173 used for x86-64 only. */
174 unsigned int plt_plt_insn_end;
175
176 /* Offset into plt_entry where the initial value of the GOT entry
177 points. */
178 unsigned int plt_lazy_offset;
179
180 /* The first entry in a PIC lazy procedure linkage table looks like
181 this. */
182 const bfd_byte *pic_plt0_entry;
183
184 /* Subsequent entries in a PIC lazy procedure linkage table look
185 like this. */
186 const bfd_byte *pic_plt_entry;
187
188 /* .eh_frame covering the lazy .plt section. */
189 const bfd_byte *eh_frame_plt;
190 unsigned int eh_frame_plt_size;
191 };
192
193 struct elf_x86_non_lazy_plt_layout
194 {
195 /* Entries in an absolute non-lazy procedure linkage table look like
196 this. */
197 const bfd_byte *plt_entry;
198 /* Entries in a PIC non-lazy procedure linkage table look like this. */
199 const bfd_byte *pic_plt_entry;
200
201 unsigned int plt_entry_size; /* Size of each PLT entry. */
202
203 /* Offsets into plt_entry that are to be replaced with... */
204 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
205
206 /* Length of the PC-relative instruction containing plt_got_offset.
207 This is used for x86-64 only. */
208 unsigned int plt_got_insn_size;
209
210 /* .eh_frame covering the non-lazy .plt section. */
211 const bfd_byte *eh_frame_plt;
212 unsigned int eh_frame_plt_size;
213 };
214
215 struct elf_x86_plt_layout
216 {
217 /* The first entry in a lazy procedure linkage table looks like this.
218 This is only used for i386 where absolute PLT0 and PIC PLT0 are
219 different. */
220 const bfd_byte *plt0_entry;
221 /* Entries in a procedure linkage table look like this. */
222 const bfd_byte *plt_entry;
223 unsigned int plt_entry_size; /* Size of each PLT entry. */
224
225 /* 1 has PLT0. */
226 unsigned int has_plt0;
227
228 /* Offsets into plt_entry that are to be replaced with... */
229 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
230
231 /* Length of the PC-relative instruction containing plt_got_offset.
232 This is only used for x86-64. */
233 unsigned int plt_got_insn_size;
234
235 /* .eh_frame covering the .plt section. */
236 const bfd_byte *eh_frame_plt;
237 unsigned int eh_frame_plt_size;
238 };
239
240 /* Values in tls_type of x86 ELF linker hash entry. */
241 #define GOT_UNKNOWN 0
242 #define GOT_NORMAL 1
243 #define GOT_TLS_GD 2
244 #define GOT_TLS_IE 4
245 #define GOT_TLS_IE_POS 5
246 #define GOT_TLS_IE_NEG 6
247 #define GOT_TLS_IE_BOTH 7
248 #define GOT_TLS_GDESC 8
249 #define GOT_TLS_GD_BOTH_P(type) \
250 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
251 #define GOT_TLS_GD_P(type) \
252 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
253 #define GOT_TLS_GDESC_P(type) \
254 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
255 #define GOT_TLS_GD_ANY_P(type) \
256 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
257
258 #define elf_x86_hash_entry(ent) \
259 ((struct elf_x86_link_hash_entry *)(ent))
260
261 /* x86 ELF linker hash table. */
262
263 struct elf_x86_link_hash_table
264 {
265 struct elf_link_hash_table elf;
266
267 /* Short-cuts to get to dynamic linker sections. */
268 asection *interp;
269 asection *plt_eh_frame;
270 asection *plt_second;
271 asection *plt_second_eh_frame;
272 asection *plt_got;
273 asection *plt_got_eh_frame;
274
275 /* Parameters describing PLT generation, lazy or non-lazy. */
276 struct elf_x86_plt_layout plt;
277
278 /* Parameters describing lazy PLT generation. */
279 const struct elf_x86_lazy_plt_layout *lazy_plt;
280
281 /* Parameters describing non-lazy PLT generation. */
282 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
283
284 union
285 {
286 bfd_signed_vma refcount;
287 bfd_vma offset;
288 } tls_ld_or_ldm_got;
289
290 /* The amount of space used by the jump slots in the GOT. */
291 bfd_vma sgotplt_jump_table_size;
292
293 /* Small local sym cache. */
294 struct sym_cache sym_cache;
295
296 /* _TLS_MODULE_BASE_ symbol. */
297 struct bfd_link_hash_entry *tls_module_base;
298
299 /* Used by local STT_GNU_IFUNC symbols. */
300 htab_t loc_hash_table;
301 void * loc_hash_memory;
302
303 /* The offset into sgot of the GOT entry used by the PLT entry
304 above. */
305 bfd_vma tlsdesc_got;
306
307 /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
308 bfd_vma next_jump_slot_index;
309 /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
310 bfd_vma next_irelative_index;
311
312 /* TRUE if there are dynamic relocs against IFUNC symbols that apply
313 to read-only sections. */
314 bfd_boolean readonly_dynrelocs_against_ifunc;
315
316 /* TRUE if this is a VxWorks x86 target. This is only used for
317 i386. */
318 bfd_boolean is_vxworks;
319
320 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.
321 This is used for i386 only. */
322 asection *srelplt2;
323
324 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. This
325 is only used for i386. */
326 bfd_vma next_tls_desc_index;
327
328 /* The offset into splt of the PLT entry for the TLS descriptor
329 resolver. Special values are 0, if not necessary (or not found
330 to be necessary yet), and -1 if needed but not determined
331 yet. This is only used for x86-64. */
332 bfd_vma tlsdesc_plt;
333
334 bfd_vma (*r_info) (bfd_vma, bfd_vma);
335 bfd_vma (*r_sym) (bfd_vma);
336 bfd_boolean (*is_reloc_section) (const char *);
337 enum elf_target_id target_id;
338 unsigned int sizeof_reloc;
339 unsigned int dt_reloc;
340 unsigned int dt_reloc_sz;
341 unsigned int dt_reloc_ent;
342 unsigned int got_entry_size;
343 unsigned int pointer_r_type;
344 int dynamic_interpreter_size;
345 const char *dynamic_interpreter;
346 const char *tls_get_addr;
347 };
348
349 struct elf_x86_init_table
350 {
351 /* The lazy PLT layout. */
352 const struct elf_x86_lazy_plt_layout *lazy_plt;
353
354 /* The non-lazy PLT layout. */
355 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
356
357 /* The lazy PLT layout for IBT. */
358 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
359
360 /* The non-lazy PLT layout for IBT. */
361 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
362
363 /* TRUE if this is a normal x86 target. */
364 bfd_boolean normal_target;
365
366 /* TRUE if this is a VxWorks x86 target. */
367 bfd_boolean is_vxworks;
368
369 bfd_vma (*r_info) (bfd_vma, bfd_vma);
370 bfd_vma (*r_sym) (bfd_vma);
371 };
372
373 struct elf_x86_obj_tdata
374 {
375 struct elf_obj_tdata root;
376
377 /* tls_type for each local got entry. */
378 char *local_got_tls_type;
379
380 /* GOTPLT entries for TLS descriptors. */
381 bfd_vma *local_tlsdesc_gotent;
382 };
383
384 enum elf_x86_plt_type
385 {
386 plt_non_lazy = 0,
387 plt_lazy = 1 << 0,
388 plt_pic = 1 << 1,
389 plt_second = 1 << 2,
390 plt_unknown = -1
391 };
392
393 struct elf_x86_plt
394 {
395 const char *name;
396 asection *sec;
397 bfd_byte *contents;
398 enum elf_x86_plt_type type;
399 unsigned int plt_got_offset;
400 unsigned int plt_entry_size;
401 unsigned int plt_got_insn_size; /* Only used for x86-64. */
402 long count;
403 };
404
405 #define elf_x86_tdata(abfd) \
406 ((struct elf_x86_obj_tdata *) (abfd)->tdata.any)
407
408 #define elf_x86_local_got_tls_type(abfd) \
409 (elf_x86_tdata (abfd)->local_got_tls_type)
410
411 #define elf_x86_local_tlsdesc_gotent(abfd) \
412 (elf_x86_tdata (abfd)->local_tlsdesc_gotent)
413
414 #define elf_x86_compute_jump_table_size(htab) \
415 ((htab)->elf.srelplt->reloc_count * (htab)->got_entry_size)
416
417 #define is_x86_elf(bfd, htab) \
418 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
419 && elf_tdata (bfd) != NULL \
420 && elf_object_id (bfd) == (htab)->target_id)
421
422 extern bfd_boolean _bfd_x86_elf_mkobject
423 (bfd *);
424
425 extern void _bfd_x86_elf_set_tls_module_base
426 (struct bfd_link_info *);
427
428 extern bfd_vma _bfd_x86_elf_dtpoff_base
429 (struct bfd_link_info *);
430
431 extern bfd_boolean _bfd_x86_elf_readonly_dynrelocs
432 (struct elf_link_hash_entry *, void *);
433
434 extern struct elf_link_hash_entry * _bfd_elf_x86_get_local_sym_hash
435 (struct elf_x86_link_hash_table *, bfd *, const Elf_Internal_Rela *,
436 bfd_boolean);
437
438 extern hashval_t _bfd_x86_elf_local_htab_hash
439 (const void *);
440
441 extern int _bfd_x86_elf_local_htab_eq
442 (const void *, const void *);
443
444 extern struct bfd_hash_entry * _bfd_x86_elf_link_hash_newfunc
445 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
446
447 extern struct bfd_link_hash_table * _bfd_x86_elf_link_hash_table_create
448 (bfd *);
449
450 extern int _bfd_x86_elf_compare_relocs
451 (const void *, const void *);
452
453 extern bfd_boolean _bfd_x86_elf_link_check_relocs
454 (bfd *, struct bfd_link_info *);
455
456 extern bfd_boolean _bfd_x86_elf_size_dynamic_sections
457 (bfd *, struct bfd_link_info *);
458
459 extern bfd_boolean _bfd_x86_elf_always_size_sections
460 (bfd *, struct bfd_link_info *);
461
462 extern void _bfd_x86_elf_merge_symbol_attribute
463 (struct elf_link_hash_entry *, const Elf_Internal_Sym *,
464 bfd_boolean, bfd_boolean);
465
466 extern void _bfd_x86_elf_copy_indirect_symbol
467 (struct bfd_link_info *, struct elf_link_hash_entry *,
468 struct elf_link_hash_entry *);
469
470 extern bfd_boolean _bfd_x86_elf_fixup_symbol
471 (struct bfd_link_info *, struct elf_link_hash_entry *);
472
473 extern bfd_boolean _bfd_x86_elf_hash_symbol
474 (struct elf_link_hash_entry *);
475
476 extern bfd_boolean _bfd_x86_elf_adjust_dynamic_symbol
477 (struct bfd_link_info *, struct elf_link_hash_entry *);
478
479 extern bfd_boolean _bfd_x86_elf_link_symbol_references_local
480 (struct bfd_link_info *, struct elf_link_hash_entry *);
481
482 extern asection * _bfd_x86_elf_gc_mark_hook
483 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
484 struct elf_link_hash_entry *, Elf_Internal_Sym *);
485
486 extern long _bfd_x86_elf_get_synthetic_symtab
487 (bfd *, long, long, bfd_vma, struct elf_x86_plt [], asymbol **,
488 asymbol **);
489
490 extern enum elf_property_kind _bfd_x86_elf_parse_gnu_properties
491 (bfd *, unsigned int, bfd_byte *, unsigned int);
492
493 extern bfd_boolean _bfd_x86_elf_merge_gnu_properties
494 (struct bfd_link_info *, bfd *, elf_property *, elf_property *);
495
496 extern bfd * _bfd_x86_elf_link_setup_gnu_properties
497 (struct bfd_link_info *, struct elf_x86_init_table *);
498
499 #define bfd_elf64_mkobject \
500 _bfd_x86_elf_mkobject
501 #define bfd_elf32_mkobject \
502 _bfd_x86_elf_mkobject
503 #define bfd_elf64_bfd_link_hash_table_create \
504 _bfd_x86_elf_link_hash_table_create
505 #define bfd_elf32_bfd_link_hash_table_create \
506 _bfd_x86_elf_link_hash_table_create
507 #define bfd_elf64_bfd_link_check_relocs \
508 _bfd_x86_elf_link_check_relocs
509 #define bfd_elf32_bfd_link_check_relocs \
510 _bfd_x86_elf_link_check_relocs
511
512 #define elf_backend_size_dynamic_sections \
513 _bfd_x86_elf_size_dynamic_sections
514 #define elf_backend_always_size_sections \
515 _bfd_x86_elf_always_size_sections
516 #define elf_backend_merge_symbol_attribute \
517 _bfd_x86_elf_merge_symbol_attribute
518 #define elf_backend_copy_indirect_symbol \
519 _bfd_x86_elf_copy_indirect_symbol
520 #define elf_backend_fixup_symbol \
521 _bfd_x86_elf_fixup_symbol
522 #define elf_backend_hash_symbol \
523 _bfd_x86_elf_hash_symbol
524 #define elf_backend_adjust_dynamic_symbol \
525 _bfd_x86_elf_adjust_dynamic_symbol
526 #define elf_backend_gc_mark_hook \
527 _bfd_x86_elf_gc_mark_hook
528 #define elf_backend_omit_section_dynsym \
529 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
530 #define elf_backend_parse_gnu_properties \
531 _bfd_x86_elf_parse_gnu_properties
532 #define elf_backend_merge_gnu_properties \
533 _bfd_x86_elf_merge_gnu_properties