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