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