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