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