1 /* x86 specific support for ELF
2 Copyright (C) 2017 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
21 #include "elfxx-x86.h"
24 #include "elf/x86-64.h"
26 /* The name of the dynamic interpreter. This is put in the .interp
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"
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. */
39 _bfd_x86_elf_set_tls_module_base (struct bfd_link_info *info)
41 struct elf_x86_link_hash_table *htab;
42 struct bfd_link_hash_entry *base;
43 const struct elf_backend_data *bed;
45 if (!bfd_link_executable (info))
48 bed = get_elf_backend_data (info->output_bfd);
49 htab = elf_x86_hash_table (info, bed->target_id);
53 base = htab->tls_module_base;
57 base->u.def.value = htab->elf.tls_size;
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. */
65 _bfd_x86_elf_dtpoff_base (struct bfd_link_info *info)
67 /* If tls_sec is NULL, we should have signalled an error already. */
68 if (elf_hash_table (info)->tls_sec == NULL)
70 return elf_hash_table (info)->tls_sec->vma;
73 /* Find any dynamic relocs that apply to read-only sections. */
76 _bfd_x86_elf_readonly_dynrelocs (struct elf_link_hash_entry *h,
79 struct elf_x86_link_hash_entry *eh;
80 struct elf_dyn_relocs *p;
82 /* Skip local IFUNC symbols. */
83 if (h->forced_local && h->type == STT_GNU_IFUNC)
86 eh = (struct elf_x86_link_hash_entry *) h;
87 for (p = eh->dyn_relocs; p != NULL; p = p->next)
89 asection *s = p->sec->output_section;
91 if (s != NULL && (s->flags & SEC_READONLY) != 0)
93 struct bfd_link_info *info = (struct bfd_link_info *) inf;
95 info->flags |= DF_TEXTREL;
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,
104 /* Not an error, just cut short the traversal. */
111 /* Find and/or create a hash entry for local symbol. */
113 struct 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,
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));
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);
134 ret = (struct elf_x86_link_hash_entry *) *slot;
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));
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;
153 /* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN
154 SYNC WITH _bfd_elf_link_hash_newfunc. */
156 struct bfd_hash_entry *
157 _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
158 struct bfd_hash_table *table,
161 /* Allocate the structure if it has not already been allocated by a
165 entry = (struct bfd_hash_entry *)
166 bfd_hash_allocate (table,
167 sizeof (struct elf_x86_link_hash_entry));
172 /* Call the allocation method of the superclass. */
173 entry = _bfd_link_hash_newfunc (entry, table, string);
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;
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. */
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. */
194 eh->plt_second.offset = (bfd_vma) -1;
195 eh->plt_got.offset = (bfd_vma) -1;
196 eh->tlsdesc_got = (bfd_vma) -1;
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. */
208 _bfd_x86_elf_local_htab_hash (const void *ptr)
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);
215 /* Compare local hash entries. */
218 _bfd_x86_elf_local_htab_eq (const void *ptr1, const void *ptr2)
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;
225 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
228 /* Destroy an x86 ELF linker hash table. */
231 elf_x86_link_hash_table_free (bfd *obfd)
233 struct elf_x86_link_hash_table *htab
234 = (struct elf_x86_link_hash_table *) obfd->link.hash;
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);
243 /* Create an x86 ELF linker hash table. */
245 struct bfd_link_hash_table *
246 _bfd_x86_elf_link_hash_table_create (bfd *abfd)
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);
252 ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt);
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),
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";
279 ret->r_info = elf32_r_info;
280 ret->r_sym = elf32_r_sym;
281 if (bed->elf_machine_code == EM_X86_64)
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";
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";
299 ret->loc_hash_table = htab_try_create (1024,
300 _bfd_x86_elf_local_htab_hash,
301 _bfd_x86_elf_local_htab_eq,
303 ret->loc_hash_memory = objalloc_create ();
304 if (!ret->loc_hash_table || !ret->loc_hash_memory)
306 elf_x86_link_hash_table_free (abfd);
309 ret->elf.root.hash_table_free = elf_x86_link_hash_table_free;
311 return &ret->elf.root;
314 /* Sort relocs into address order. */
317 _bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
319 const arelent *a = * (const arelent **) ap;
320 const arelent *b = * (const arelent **) bp;
322 if (a->address > b->address)
324 else if (a->address < b->address)
331 _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
333 if (!bfd_link_relocatable (info))
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);
341 struct elf_link_hash_entry *h
342 = elf_link_hash_lookup (elf_hash_table (info),
344 FALSE, FALSE, FALSE);
346 ((struct elf_x86_link_hash_entry *) h)->tls_get_addr = 1;
350 /* Invoke the regular ELF backend linker to do all the work. */
351 return _bfd_elf_link_check_relocs (abfd, info);
355 _bfd_x86_elf_always_size_sections (bfd *output_bfd,
356 struct bfd_link_info *info)
358 asection *tls_sec = elf_hash_table (info)->tls_sec;
362 struct elf_link_hash_entry *tlsbase;
364 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
366 FALSE, FALSE, FALSE);
368 if (tlsbase && tlsbase->type == STT_TLS)
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);
375 htab = elf_x86_hash_table (info, bed->target_id);
379 if (!(_bfd_generic_link_add_one_symbol
380 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
381 tls_sec, 0, NULL, FALSE,
385 htab->tls_module_base = bh;
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);
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)
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)
413 /* Copy the extra info we tack onto an elf_link_hash_entry. */
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)
420 struct elf_x86_link_hash_entry *edir, *eind;
422 edir = (struct elf_x86_link_hash_entry *) dir;
423 eind = (struct elf_x86_link_hash_entry *) ind;
425 if (eind->dyn_relocs != NULL)
427 if (edir->dyn_relocs != NULL)
429 struct elf_dyn_relocs **pp;
430 struct elf_dyn_relocs *p;
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; )
436 struct elf_dyn_relocs *q;
438 for (q = edir->dyn_relocs; q != NULL; q = q->next)
439 if (q->sec == p->sec)
441 q->pc_count += p->pc_count;
442 q->count += p->count;
449 *pp = edir->dyn_relocs;
452 edir->dyn_relocs = eind->dyn_relocs;
453 eind->dyn_relocs = NULL;
456 if (ind->root.type == bfd_link_hash_indirect
457 && dir->got.refcount <= 0)
459 edir->tls_type = eind->tls_type;
460 eind->tls_type = GOT_UNKNOWN;
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;
467 edir->has_got_reloc |= eind->has_got_reloc;
468 edir->has_non_got_reloc |= eind->has_non_got_reloc;
470 if (ELIMINATE_COPY_RELOCS
471 && ind->root.type != bfd_link_hash_indirect
472 && dir->dynamic_adjusted)
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;
486 if (eind->func_pointer_refcount > 0)
488 edir->func_pointer_refcount += eind->func_pointer_refcount;
489 eind->func_pointer_refcount = 0;
492 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
496 /* Remove undefined weak symbol from the dynamic symbol table if it
500 _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
503 if (h->dynindx != -1)
505 const struct elf_backend_data *bed
506 = get_elf_backend_data (info->output_bfd);
507 if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
509 elf_x86_hash_entry (h)->has_got_reloc,
510 elf_x86_hash_entry (h)))
513 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
520 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
523 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
525 if (h->plt.offset != (bfd_vma) -1
527 && !h->pointer_equality_needed)
530 return _bfd_elf_hash_symbol (h);
533 /* Parse x86 GNU properties. */
535 enum elf_property_kind
536 _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
537 bfd_byte *ptr, unsigned int datasz)
543 case GNU_PROPERTY_X86_ISA_1_USED:
544 case GNU_PROPERTY_X86_ISA_1_NEEDED:
545 case GNU_PROPERTY_X86_FEATURE_1_AND:
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>"))),
555 return property_corrupt;
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;
564 return property_ignored;
567 return property_number;
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. */
575 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
576 bfd *abfd ATTRIBUTE_UNUSED,
580 unsigned int number, features;
581 bfd_boolean updated = FALSE;
582 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
586 case GNU_PROPERTY_X86_ISA_1_USED:
587 case GNU_PROPERTY_X86_ISA_1_NEEDED:
588 if (aprop != NULL && bprop != NULL)
590 number = aprop->u.number;
591 aprop->u.number = number | bprop->u.number;
592 updated = number != (unsigned int) aprop->u.number;
596 /* Return TRUE if APROP is NULL to indicate that BPROP should
598 updated = aprop == NULL;
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.
608 if (aprop != NULL && bprop != NULL)
612 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
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;
628 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
630 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
633 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
634 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
637 number = aprop->u.number;
638 aprop->u.number = number | features;
639 updated = number != (unsigned int) aprop->u.number;
643 bprop->u.number |= features;
647 else if (aprop != NULL)
649 aprop->pr_kind = property_remove;
656 /* Never should happen. */