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