Add elfxx-x86.h and elfxx-x86.c
[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 "objalloc.h"
28 #include "hashtab.h"
29
30 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
31    copying dynamic variables from a shared lib into an app's dynbss
32    section, and instead use a dynamic relocation to point into the
33    shared lib.  */
34 #define ELIMINATE_COPY_RELOCS 1
35
36 #define elf_x86_hash_table(p, id) \
37   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) == (id) \
38     ? ((struct elf_x86_link_hash_table *) ((p)->hash)) : NULL)
39
40 /* Is a undefined weak symbol which is resolved to 0.  Reference to an
41    undefined weak symbol is resolved to 0 when building executable if
42    it isn't dynamic and
43    1. Has non-GOT/non-PLT relocations in text section.  Or
44    2. Has no GOT/PLT relocation.
45    Local undefined weak symbol is always resolved to 0.
46  */
47 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, ID, GOT_RELOC, EH) \
48   ((EH)->elf.root.type == bfd_link_hash_undefweak                \
49    && ((EH)->elf.forced_local                                    \
50        || (bfd_link_executable (INFO)                            \
51            && (elf_x86_hash_table ((INFO), (ID))->interp == NULL \
52                || !(GOT_RELOC)                                   \
53                || (EH)->has_non_got_reloc                        \
54                || !(INFO)->dynamic_undefined_weak))))
55
56 /* Should copy relocation be generated for a symbol.  Don't generate
57    copy relocation against a protected symbol defined in a shared
58    object with GNU_PROPERTY_NO_COPY_ON_PROTECTED.  */
59 #define SYMBOL_NO_COPYRELOC(INFO, EH) \
60   ((EH)->def_protected \
61    && ((EH)->elf.root.type == bfd_link_hash_defined \
62        || (EH)->elf.root.type == bfd_link_hash_defweak) \
63    && elf_has_no_copy_on_protected ((EH)->elf.root.u.def.section->owner) \
64    && ((EH)->elf.root.u.def.section->owner->flags & DYNAMIC) != 0 \
65    && ((EH)->elf.root.u.def.section->flags & SEC_CODE) == 0)
66
67 /* x86 ELF linker hash entry.  */
68
69 struct elf_x86_link_hash_entry
70 {
71   struct elf_link_hash_entry elf;
72
73   /* Track dynamic relocs copied for this symbol.  */
74   struct elf_dyn_relocs *dyn_relocs;
75
76   unsigned char tls_type;
77
78   /* TRUE if symbol has GOT or PLT relocations.  */
79   unsigned int has_got_reloc : 1;
80
81   /* TRUE if symbol has non-GOT/non-PLT relocations in text sections.  */
82   unsigned int has_non_got_reloc : 1;
83
84   /* Don't call finish_dynamic_symbol on this symbol.  */
85   unsigned int no_finish_dynamic_symbol : 1;
86
87   /* TRUE if symbol is __tls_get_addr.  */
88   unsigned int tls_get_addr : 1;
89
90   /* TRUE if symbol is defined as a protected symbol.  */
91   unsigned int def_protected : 1;
92
93   /* Symbol is referenced by R_386_GOTOFF relocation.  This is only used
94      by i386.  */
95   unsigned int gotoff_ref : 1;
96
97   /* TRUE if a weak symbol with a real definition needs a copy reloc.
98      When there is a weak symbol with a real definition, the processor
99      independent code will have arranged for us to see the real
100      definition first.  We need to copy the needs_copy bit from the
101      real definition and check it when allowing copy reloc in PIE.  This
102      is only used by x86-64.  */
103   unsigned int needs_copy : 1;
104
105   /* Reference count of C/C++ function pointer relocations in read-write
106      section which can be resolved at run-time.  */
107   bfd_signed_vma func_pointer_refcount;
108
109   /* Information about the GOT PLT entry. Filled when there are both
110      GOT and PLT relocations against the same function.  */
111   union gotplt_union plt_got;
112
113   /* Information about the second PLT entry.   */
114   union gotplt_union plt_second;
115
116   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
117      starting at the end of the jump table.  */
118   bfd_vma tlsdesc_got;
119 };
120
121 /* The first 3 values in tls_type of x86 ELF linker hash entry.  */
122 #define GOT_UNKNOWN     0
123 #define GOT_NORMAL      1
124 #define GOT_TLS_GD      2
125
126 #define elf_x86_hash_entry(ent) \
127   ((struct elf_x86_link_hash_entry *)(ent))
128
129 /* x86 ELF linker hash table.  */
130
131 struct elf_x86_link_hash_table
132 {
133   struct elf_link_hash_table elf;
134
135   /* Short-cuts to get to dynamic linker sections.  */
136   asection *interp;
137   asection *plt_eh_frame;
138   asection *plt_second;
139   asection *plt_second_eh_frame;
140   asection *plt_got;
141   asection *plt_got_eh_frame;
142
143   union
144   {
145     bfd_signed_vma refcount;
146     bfd_vma offset;
147   } tls_ld_or_ldm_got;
148
149   /* The amount of space used by the jump slots in the GOT.  */
150   bfd_vma sgotplt_jump_table_size;
151
152   /* Small local sym cache.  */
153   struct sym_cache sym_cache;
154
155   /* _TLS_MODULE_BASE_ symbol.  */
156   struct bfd_link_hash_entry *tls_module_base;
157
158   /* Used by local STT_GNU_IFUNC symbols.  */
159   htab_t loc_hash_table;
160   void * loc_hash_memory;
161
162   /* The offset into splt of the PLT entry for the TLS descriptor
163      resolver.  Special values are 0, if not necessary (or not found
164      to be necessary yet), and -1 if needed but not determined
165      yet.  */
166   bfd_vma tlsdesc_plt;
167
168   /* The offset into sgot of the GOT entry used by the PLT entry
169      above.  */
170   bfd_vma tlsdesc_got;
171
172   /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt.  */
173   bfd_vma next_jump_slot_index;
174   /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt.  */
175   bfd_vma next_irelative_index;
176
177   /* TRUE if there are dynamic relocs against IFUNC symbols that apply
178      to read-only sections.  */
179   bfd_boolean readonly_dynrelocs_against_ifunc;
180
181   bfd_vma (*r_info) (bfd_vma, bfd_vma);
182   bfd_vma (*r_sym) (bfd_vma);
183   unsigned int pointer_r_type;
184   int dynamic_interpreter_size;
185   const char *dynamic_interpreter;
186   const char *tls_get_addr;
187 };
188
189 struct elf_x86_obj_tdata
190 {
191   struct elf_obj_tdata root;
192
193   /* tls_type for each local got entry.  */
194   char *local_got_tls_type;
195
196   /* GOTPLT entries for TLS descriptors.  */
197   bfd_vma *local_tlsdesc_gotent;
198 };
199
200 #define elf_x86_tdata(abfd) \
201   ((struct elf_x86_obj_tdata *) (abfd)->tdata.any)
202
203 #define elf_x86_local_got_tls_type(abfd) \
204   (elf_x86_tdata (abfd)->local_got_tls_type)
205
206 #define elf_x86_local_tlsdesc_gotent(abfd) \
207   (elf_x86_tdata (abfd)->local_tlsdesc_gotent)
208
209 extern void _bfd_x86_elf_set_tls_module_base
210   (struct bfd_link_info *);
211
212 extern bfd_vma _bfd_x86_elf_dtpoff_base
213   (struct bfd_link_info *);
214
215 extern bfd_boolean _bfd_x86_elf_readonly_dynrelocs
216   (struct elf_link_hash_entry *, void *);
217
218 extern struct elf_link_hash_entry * _bfd_elf_x86_get_local_sym_hash
219   (struct elf_x86_link_hash_table *, bfd *, const Elf_Internal_Rela *,
220    bfd_boolean);
221
222 extern hashval_t _bfd_x86_elf_local_htab_hash
223   (const void *);
224
225 extern int _bfd_x86_elf_local_htab_eq
226   (const void *, const void *);
227
228 extern struct bfd_hash_entry * _bfd_x86_elf_link_hash_newfunc
229   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
230
231 extern void _bfd_x86_elf_link_hash_table_free
232   (bfd *);
233
234 extern int _bfd_x86_elf_compare_relocs
235   (const void *, const void *);
236
237 extern bfd_boolean _bfd_x86_elf_link_check_relocs
238   (bfd *, struct bfd_link_info *);
239
240 extern bfd_boolean _bfd_x86_elf_always_size_sections
241   (bfd *, struct bfd_link_info *);
242
243 extern void _bfd_x86_elf_merge_symbol_attribute
244   (struct elf_link_hash_entry *, const Elf_Internal_Sym *,
245    bfd_boolean, bfd_boolean);
246
247 extern void _bfd_x86_elf_copy_indirect_symbol
248   (struct bfd_link_info *, struct elf_link_hash_entry *,
249    struct elf_link_hash_entry *);
250
251 extern bfd_boolean _bfd_x86_elf_fixup_symbol
252   (struct bfd_link_info *, struct elf_link_hash_entry *);
253
254 extern bfd_boolean _bfd_x86_elf_hash_symbol
255   (struct elf_link_hash_entry *);
256
257 extern enum elf_property_kind _bfd_x86_elf_parse_gnu_properties
258   (bfd *, unsigned int, bfd_byte *, unsigned int);
259
260 extern bfd_boolean _bfd_x86_elf_merge_gnu_properties
261   (struct bfd_link_info *, bfd *, elf_property *, elf_property *);
262
263 #define bfd_elf64_bfd_link_check_relocs \
264   _bfd_x86_elf_link_check_relocs
265 #define bfd_elf32_bfd_link_check_relocs \
266   _bfd_x86_elf_link_check_relocs
267
268 #define elf_backend_always_size_sections \
269   _bfd_x86_elf_always_size_sections
270 #define elf_backend_merge_symbol_attribute \
271   _bfd_x86_elf_merge_symbol_attribute
272 #define elf_backend_copy_indirect_symbol \
273   _bfd_x86_elf_copy_indirect_symbol
274 #define elf_backend_fixup_symbol \
275   _bfd_x86_elf_fixup_symbol
276 #define elf_backend_hash_symbol \
277   _bfd_x86_elf_hash_symbol
278 #define elf_backend_omit_section_dynsym \
279   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
280 #define elf_backend_parse_gnu_properties \
281   _bfd_x86_elf_parse_gnu_properties
282 #define elf_backend_merge_gnu_properties \
283   _bfd_x86_elf_merge_gnu_properties