Add elfxx-x86.h and elfxx-x86.c
[deliverable/binutils-gdb.git] / bfd / elfxx-x86.c
CommitLineData
0afcef53
L
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 "elfxx-x86.h"
22
23/* _TLS_MODULE_BASE_ needs to be treated especially when linking
24 executables. Rather than setting it to the beginning of the TLS
25 section, we have to set it to the end. This function may be called
26 multiple times, it is idempotent. */
27
28void
29_bfd_x86_elf_set_tls_module_base (struct bfd_link_info *info)
30{
31 struct elf_x86_link_hash_table *htab;
32 struct bfd_link_hash_entry *base;
33 const struct elf_backend_data *bed;
34
35 if (!bfd_link_executable (info))
36 return;
37
38 bed = get_elf_backend_data (info->output_bfd);
39 htab = elf_x86_hash_table (info, bed->target_id);
40 if (htab == NULL)
41 return;
42
43 base = htab->tls_module_base;
44 if (base == NULL)
45 return;
46
47 base->u.def.value = htab->elf.tls_size;
48}
49
50/* Return the base VMA address which should be subtracted from real addresses
51 when resolving @dtpoff relocation.
52 This is PT_TLS segment p_vaddr. */
53
54bfd_vma
55_bfd_x86_elf_dtpoff_base (struct bfd_link_info *info)
56{
57 /* If tls_sec is NULL, we should have signalled an error already. */
58 if (elf_hash_table (info)->tls_sec == NULL)
59 return 0;
60 return elf_hash_table (info)->tls_sec->vma;
61}
62
63/* Find any dynamic relocs that apply to read-only sections. */
64
65bfd_boolean
66_bfd_x86_elf_readonly_dynrelocs (struct elf_link_hash_entry *h,
67 void *inf)
68{
69 struct elf_x86_link_hash_entry *eh;
70 struct elf_dyn_relocs *p;
71
72 /* Skip local IFUNC symbols. */
73 if (h->forced_local && h->type == STT_GNU_IFUNC)
74 return TRUE;
75
76 eh = (struct elf_x86_link_hash_entry *) h;
77 for (p = eh->dyn_relocs; p != NULL; p = p->next)
78 {
79 asection *s = p->sec->output_section;
80
81 if (s != NULL && (s->flags & SEC_READONLY) != 0)
82 {
83 struct bfd_link_info *info = (struct bfd_link_info *) inf;
84
85 info->flags |= DF_TEXTREL;
86
87 if ((info->warn_shared_textrel && bfd_link_pic (info))
88 || info->error_textrel)
89 /* xgettext:c-format */
90 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
91 p->sec->owner, h->root.root.string,
92 p->sec);
93
94 /* Not an error, just cut short the traversal. */
95 return FALSE;
96 }
97 }
98 return TRUE;
99}
100
101/* Find and/or create a hash entry for local symbol. */
102
103struct elf_link_hash_entry *
104_bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table *htab,
105 bfd *abfd, const Elf_Internal_Rela *rel,
106 bfd_boolean create)
107{
108 struct elf_x86_link_hash_entry e, *ret;
109 asection *sec = abfd->sections;
110 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
111 htab->r_sym (rel->r_info));
112 void **slot;
113
114 e.elf.indx = sec->id;
115 e.elf.dynstr_index = htab->r_sym (rel->r_info);
116 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
117 create ? INSERT : NO_INSERT);
118
119 if (!slot)
120 return NULL;
121
122 if (*slot)
123 {
124 ret = (struct elf_x86_link_hash_entry *) *slot;
125 return &ret->elf;
126 }
127
128 ret = (struct elf_x86_link_hash_entry *)
129 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
130 sizeof (struct elf_x86_link_hash_entry));
131 if (ret)
132 {
133 memset (ret, 0, sizeof (*ret));
134 ret->elf.indx = sec->id;
135 ret->elf.dynstr_index = htab->r_sym (rel->r_info);
136 ret->elf.dynindx = -1;
137 ret->plt_got.offset = (bfd_vma) -1;
138 *slot = ret;
139 }
140 return &ret->elf;
141}
142
143/* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN
144 SYNC WITH _bfd_elf_link_hash_newfunc. */
145
146struct bfd_hash_entry *
147_bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
148 struct bfd_hash_table *table,
149 const char *string)
150{
151 /* Allocate the structure if it has not already been allocated by a
152 subclass. */
153 if (entry == NULL)
154 {
155 entry = (struct bfd_hash_entry *)
156 bfd_hash_allocate (table,
157 sizeof (struct elf_x86_link_hash_entry));
158 if (entry == NULL)
159 return entry;
160 }
161
162 /* Call the allocation method of the superclass. */
163 entry = _bfd_link_hash_newfunc (entry, table, string);
164 if (entry != NULL)
165 {
166 struct elf_x86_link_hash_entry *eh
167 = (struct elf_x86_link_hash_entry *) entry;
168 struct elf_link_hash_table *htab
169 = (struct elf_link_hash_table *) table;
170
171 memset (&eh->elf.size, 0,
172 (sizeof (struct elf_x86_link_hash_entry)
173 - offsetof (struct elf_link_hash_entry, size)));
174 /* Set local fields. */
175 eh->elf.indx = -1;
176 eh->elf.dynindx = -1;
177 eh->elf.got = htab->init_got_refcount;
178 eh->elf.plt = htab->init_plt_refcount;
179 /* Assume that we have been called by a non-ELF symbol reader.
180 This flag is then reset by the code which reads an ELF input
181 file. This ensures that a symbol created by a non-ELF symbol
182 reader will have the flag set correctly. */
183 eh->elf.non_elf = 1;
184 eh->plt_second.offset = (bfd_vma) -1;
185 eh->plt_got.offset = (bfd_vma) -1;
186 eh->tlsdesc_got = (bfd_vma) -1;
187 }
188
189 return entry;
190}
191
192/* Compute a hash of a local hash entry. We use elf_link_hash_entry
193 for local symbol so that we can handle local STT_GNU_IFUNC symbols
194 as global symbol. We reuse indx and dynstr_index for local symbol
195 hash since they aren't used by global symbols in this backend. */
196
197hashval_t
198_bfd_x86_elf_local_htab_hash (const void *ptr)
199{
200 struct elf_link_hash_entry *h
201 = (struct elf_link_hash_entry *) ptr;
202 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
203}
204
205/* Compare local hash entries. */
206
207int
208_bfd_x86_elf_local_htab_eq (const void *ptr1, const void *ptr2)
209{
210 struct elf_link_hash_entry *h1
211 = (struct elf_link_hash_entry *) ptr1;
212 struct elf_link_hash_entry *h2
213 = (struct elf_link_hash_entry *) ptr2;
214
215 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
216}
217
218/* Destroy an x86 ELF linker hash table. */
219
220void
221_bfd_x86_elf_link_hash_table_free (bfd *obfd)
222{
223 struct elf_x86_link_hash_table *htab
224 = (struct elf_x86_link_hash_table *) obfd->link.hash;
225
226 if (htab->loc_hash_table)
227 htab_delete (htab->loc_hash_table);
228 if (htab->loc_hash_memory)
229 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
230 _bfd_elf_link_hash_table_free (obfd);
231}
232
233/* Sort relocs into address order. */
234
235int
236_bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
237{
238 const arelent *a = * (const arelent **) ap;
239 const arelent *b = * (const arelent **) bp;
240
241 if (a->address > b->address)
242 return 1;
243 else if (a->address < b->address)
244 return -1;
245 else
246 return 0;
247}
248
249bfd_boolean
250_bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
251{
252 if (!bfd_link_relocatable (info))
253 {
254 /* Check for __tls_get_addr reference. */
255 struct elf_x86_link_hash_table *htab;
256 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
257 htab = elf_x86_hash_table (info, bed->target_id);
258 if (htab)
259 {
260 struct elf_link_hash_entry *h
261 = elf_link_hash_lookup (elf_hash_table (info),
262 htab->tls_get_addr,
263 FALSE, FALSE, FALSE);
264 if (h != NULL)
265 ((struct elf_x86_link_hash_entry *) h)->tls_get_addr = 1;
266 }
267 }
268
269 /* Invoke the regular ELF backend linker to do all the work. */
270 return _bfd_elf_link_check_relocs (abfd, info);
271}
272
273bfd_boolean
274_bfd_x86_elf_always_size_sections (bfd *output_bfd,
275 struct bfd_link_info *info)
276{
277 asection *tls_sec = elf_hash_table (info)->tls_sec;
278
279 if (tls_sec)
280 {
281 struct elf_link_hash_entry *tlsbase;
282
283 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
284 "_TLS_MODULE_BASE_",
285 FALSE, FALSE, FALSE);
286
287 if (tlsbase && tlsbase->type == STT_TLS)
288 {
289 struct elf_x86_link_hash_table *htab;
290 struct bfd_link_hash_entry *bh = NULL;
291 const struct elf_backend_data *bed
292 = get_elf_backend_data (output_bfd);
293
294 htab = elf_x86_hash_table (info, bed->target_id);
295 if (htab == NULL)
296 return FALSE;
297
298 if (!(_bfd_generic_link_add_one_symbol
299 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
300 tls_sec, 0, NULL, FALSE,
301 bed->collect, &bh)))
302 return FALSE;
303
304 htab->tls_module_base = bh;
305
306 tlsbase = (struct elf_link_hash_entry *)bh;
307 tlsbase->def_regular = 1;
308 tlsbase->other = STV_HIDDEN;
309 tlsbase->root.linker_def = 1;
310 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
311 }
312 }
313
314 return TRUE;
315}
316
317void
318_bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
319 const Elf_Internal_Sym *isym,
320 bfd_boolean definition,
321 bfd_boolean dynamic ATTRIBUTE_UNUSED)
322{
323 if (definition)
324 {
325 struct elf_x86_link_hash_entry *eh
326 = (struct elf_x86_link_hash_entry *) h;
327 eh->def_protected = (ELF_ST_VISIBILITY (isym->st_other)
328 == STV_PROTECTED);
329 }
330}
331
332/* Copy the extra info we tack onto an elf_link_hash_entry. */
333
334void
335_bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
336 struct elf_link_hash_entry *dir,
337 struct elf_link_hash_entry *ind)
338{
339 struct elf_x86_link_hash_entry *edir, *eind;
340
341 edir = (struct elf_x86_link_hash_entry *) dir;
342 eind = (struct elf_x86_link_hash_entry *) ind;
343
344 if (eind->dyn_relocs != NULL)
345 {
346 if (edir->dyn_relocs != NULL)
347 {
348 struct elf_dyn_relocs **pp;
349 struct elf_dyn_relocs *p;
350
351 /* Add reloc counts against the indirect sym to the direct sym
352 list. Merge any entries against the same section. */
353 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
354 {
355 struct elf_dyn_relocs *q;
356
357 for (q = edir->dyn_relocs; q != NULL; q = q->next)
358 if (q->sec == p->sec)
359 {
360 q->pc_count += p->pc_count;
361 q->count += p->count;
362 *pp = p->next;
363 break;
364 }
365 if (q == NULL)
366 pp = &p->next;
367 }
368 *pp = edir->dyn_relocs;
369 }
370
371 edir->dyn_relocs = eind->dyn_relocs;
372 eind->dyn_relocs = NULL;
373 }
374
375 if (ind->root.type == bfd_link_hash_indirect
376 && dir->got.refcount <= 0)
377 {
378 edir->tls_type = eind->tls_type;
379 eind->tls_type = GOT_UNKNOWN;
380 }
381
382 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
383 generate a R_386_COPY reloc. */
384 edir->gotoff_ref |= eind->gotoff_ref;
385
386 edir->has_got_reloc |= eind->has_got_reloc;
387 edir->has_non_got_reloc |= eind->has_non_got_reloc;
388
389 if (ELIMINATE_COPY_RELOCS
390 && ind->root.type != bfd_link_hash_indirect
391 && dir->dynamic_adjusted)
392 {
393 /* If called to transfer flags for a weakdef during processing
394 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
395 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
396 if (dir->versioned != versioned_hidden)
397 dir->ref_dynamic |= ind->ref_dynamic;
398 dir->ref_regular |= ind->ref_regular;
399 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
400 dir->needs_plt |= ind->needs_plt;
401 dir->pointer_equality_needed |= ind->pointer_equality_needed;
402 }
403 else
404 {
405 if (eind->func_pointer_refcount > 0)
406 {
407 edir->func_pointer_refcount += eind->func_pointer_refcount;
408 eind->func_pointer_refcount = 0;
409 }
410
411 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
412 }
413}
414
415/* Remove undefined weak symbol from the dynamic symbol table if it
416 is resolved to 0. */
417
418bfd_boolean
419_bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
420 struct elf_link_hash_entry *h)
421{
422 if (h->dynindx != -1)
423 {
424 const struct elf_backend_data *bed
425 = get_elf_backend_data (info->output_bfd);
426 if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
427 bed->target_id,
428 elf_x86_hash_entry (h)->has_got_reloc,
429 elf_x86_hash_entry (h)))
430 {
431 h->dynindx = -1;
432 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
433 h->dynstr_index);
434 }
435 }
436 return TRUE;
437}
438
439/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
440
441bfd_boolean
442_bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
443{
444 if (h->plt.offset != (bfd_vma) -1
445 && !h->def_regular
446 && !h->pointer_equality_needed)
447 return FALSE;
448
449 return _bfd_elf_hash_symbol (h);
450}
451
452/* Parse x86 GNU properties. */
453
454enum elf_property_kind
455_bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
456 bfd_byte *ptr, unsigned int datasz)
457{
458 elf_property *prop;
459
460 switch (type)
461 {
462 case GNU_PROPERTY_X86_ISA_1_USED:
463 case GNU_PROPERTY_X86_ISA_1_NEEDED:
464 case GNU_PROPERTY_X86_FEATURE_1_AND:
465 if (datasz != 4)
466 {
467 _bfd_error_handler
468 ((type == GNU_PROPERTY_X86_ISA_1_USED
469 ? _("error: %B: <corrupt x86 ISA used size: 0x%x>")
470 : (type == GNU_PROPERTY_X86_ISA_1_NEEDED
471 ? _("error: %B: <corrupt x86 ISA needed size: 0x%x>")
472 : _("error: %B: <corrupt x86 feature size: 0x%x>"))),
473 abfd, datasz);
474 return property_corrupt;
475 }
476 prop = _bfd_elf_get_property (abfd, type, datasz);
477 /* Combine properties of the same type. */
478 prop->u.number |= bfd_h_get_32 (abfd, ptr);
479 prop->pr_kind = property_number;
480 break;
481
482 default:
483 return property_ignored;
484 }
485
486 return property_number;
487}
488
489/* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
490 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
491 should be merged with ABFD. */
492
493bfd_boolean
494_bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
495 bfd *abfd ATTRIBUTE_UNUSED,
496 elf_property *aprop,
497 elf_property *bprop)
498{
499 unsigned int number, features;
500 bfd_boolean updated = FALSE;
501 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
502
503 switch (pr_type)
504 {
505 case GNU_PROPERTY_X86_ISA_1_USED:
506 case GNU_PROPERTY_X86_ISA_1_NEEDED:
507 if (aprop != NULL && bprop != NULL)
508 {
509 number = aprop->u.number;
510 aprop->u.number = number | bprop->u.number;
511 updated = number != (unsigned int) aprop->u.number;
512 }
513 else
514 {
515 /* Return TRUE if APROP is NULL to indicate that BPROP should
516 be added to ABFD. */
517 updated = aprop == NULL;
518 }
519 break;
520
521 case GNU_PROPERTY_X86_FEATURE_1_AND:
522 /* Only one of APROP and BPROP can be NULL:
523 1. APROP & BPROP when both APROP and BPROP aren't NULL.
524 2. If APROP is NULL, remove x86 feature.
525 3. Otherwise, do nothing.
526 */
527 if (aprop != NULL && bprop != NULL)
528 {
529 features = 0;
530 if (info->ibt)
531 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
532 if (info->shstk)
533 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
534 number = aprop->u.number;
535 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
536 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
537 aprop->u.number = (number & bprop->u.number) | features;
538 updated = number != (unsigned int) aprop->u.number;
539 /* Remove the property if all feature bits are cleared. */
540 if (aprop->u.number == 0)
541 aprop->pr_kind = property_remove;
542 }
543 else
544 {
545 features = 0;
546 if (info->ibt)
547 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
548 if (info->shstk)
549 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
550 if (features)
551 {
552 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
553 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
554 if (aprop != NULL)
555 {
556 number = aprop->u.number;
557 aprop->u.number = number | features;
558 updated = number != (unsigned int) aprop->u.number;
559 }
560 else
561 {
562 bprop->u.number |= features;
563 updated = TRUE;
564 }
565 }
566 else if (aprop != NULL)
567 {
568 aprop->pr_kind = property_remove;
569 updated = TRUE;
570 }
571 }
572 break;
573
574 default:
575 /* Never should happen. */
576 abort ();
577 }
578
579 return updated;
580}
This page took 0.045142 seconds and 4 git commands to generate.