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