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