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"
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. */
29 _bfd_x86_elf_set_tls_module_base (struct bfd_link_info
*info
)
31 struct elf_x86_link_hash_table
*htab
;
32 struct bfd_link_hash_entry
*base
;
33 const struct elf_backend_data
*bed
;
35 if (!bfd_link_executable (info
))
38 bed
= get_elf_backend_data (info
->output_bfd
);
39 htab
= elf_x86_hash_table (info
, bed
->target_id
);
43 base
= htab
->tls_module_base
;
47 base
->u
.def
.value
= htab
->elf
.tls_size
;
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. */
55 _bfd_x86_elf_dtpoff_base (struct bfd_link_info
*info
)
57 /* If tls_sec is NULL, we should have signalled an error already. */
58 if (elf_hash_table (info
)->tls_sec
== NULL
)
60 return elf_hash_table (info
)->tls_sec
->vma
;
63 /* Find any dynamic relocs that apply to read-only sections. */
66 _bfd_x86_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
,
69 struct elf_x86_link_hash_entry
*eh
;
70 struct elf_dyn_relocs
*p
;
72 /* Skip local IFUNC symbols. */
73 if (h
->forced_local
&& h
->type
== STT_GNU_IFUNC
)
76 eh
= (struct elf_x86_link_hash_entry
*) h
;
77 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
79 asection
*s
= p
->sec
->output_section
;
81 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
83 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
85 info
->flags
|= DF_TEXTREL
;
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
,
94 /* Not an error, just cut short the traversal. */
101 /* Find and/or create a hash entry for local symbol. */
103 struct 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
,
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
));
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
);
124 ret
= (struct elf_x86_link_hash_entry
*) *slot
;
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
));
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;
143 /* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN
144 SYNC WITH _bfd_elf_link_hash_newfunc. */
146 struct bfd_hash_entry
*
147 _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
148 struct bfd_hash_table
*table
,
151 /* Allocate the structure if it has not already been allocated by a
155 entry
= (struct bfd_hash_entry
*)
156 bfd_hash_allocate (table
,
157 sizeof (struct elf_x86_link_hash_entry
));
162 /* Call the allocation method of the superclass. */
163 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
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
;
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. */
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. */
184 eh
->plt_second
.offset
= (bfd_vma
) -1;
185 eh
->plt_got
.offset
= (bfd_vma
) -1;
186 eh
->tlsdesc_got
= (bfd_vma
) -1;
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. */
198 _bfd_x86_elf_local_htab_hash (const void *ptr
)
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
);
205 /* Compare local hash entries. */
208 _bfd_x86_elf_local_htab_eq (const void *ptr1
, const void *ptr2
)
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
;
215 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
218 /* Destroy an x86 ELF linker hash table. */
221 _bfd_x86_elf_link_hash_table_free (bfd
*obfd
)
223 struct elf_x86_link_hash_table
*htab
224 = (struct elf_x86_link_hash_table
*) obfd
->link
.hash
;
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
);
233 /* Sort relocs into address order. */
236 _bfd_x86_elf_compare_relocs (const void *ap
, const void *bp
)
238 const arelent
*a
= * (const arelent
**) ap
;
239 const arelent
*b
= * (const arelent
**) bp
;
241 if (a
->address
> b
->address
)
243 else if (a
->address
< b
->address
)
250 _bfd_x86_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
252 if (!bfd_link_relocatable (info
))
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
);
260 struct elf_link_hash_entry
*h
261 = elf_link_hash_lookup (elf_hash_table (info
),
263 FALSE
, FALSE
, FALSE
);
265 ((struct elf_x86_link_hash_entry
*) h
)->tls_get_addr
= 1;
269 /* Invoke the regular ELF backend linker to do all the work. */
270 return _bfd_elf_link_check_relocs (abfd
, info
);
274 _bfd_x86_elf_always_size_sections (bfd
*output_bfd
,
275 struct bfd_link_info
*info
)
277 asection
*tls_sec
= elf_hash_table (info
)->tls_sec
;
281 struct elf_link_hash_entry
*tlsbase
;
283 tlsbase
= elf_link_hash_lookup (elf_hash_table (info
),
285 FALSE
, FALSE
, FALSE
);
287 if (tlsbase
&& tlsbase
->type
== STT_TLS
)
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
);
294 htab
= elf_x86_hash_table (info
, bed
->target_id
);
298 if (!(_bfd_generic_link_add_one_symbol
299 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
300 tls_sec
, 0, NULL
, FALSE
,
304 htab
->tls_module_base
= bh
;
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
);
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
)
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
)
332 /* Copy the extra info we tack onto an elf_link_hash_entry. */
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
)
339 struct elf_x86_link_hash_entry
*edir
, *eind
;
341 edir
= (struct elf_x86_link_hash_entry
*) dir
;
342 eind
= (struct elf_x86_link_hash_entry
*) ind
;
344 if (eind
->dyn_relocs
!= NULL
)
346 if (edir
->dyn_relocs
!= NULL
)
348 struct elf_dyn_relocs
**pp
;
349 struct elf_dyn_relocs
*p
;
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
; )
355 struct elf_dyn_relocs
*q
;
357 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
358 if (q
->sec
== p
->sec
)
360 q
->pc_count
+= p
->pc_count
;
361 q
->count
+= p
->count
;
368 *pp
= edir
->dyn_relocs
;
371 edir
->dyn_relocs
= eind
->dyn_relocs
;
372 eind
->dyn_relocs
= NULL
;
375 if (ind
->root
.type
== bfd_link_hash_indirect
376 && dir
->got
.refcount
<= 0)
378 edir
->tls_type
= eind
->tls_type
;
379 eind
->tls_type
= GOT_UNKNOWN
;
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
;
386 edir
->has_got_reloc
|= eind
->has_got_reloc
;
387 edir
->has_non_got_reloc
|= eind
->has_non_got_reloc
;
389 if (ELIMINATE_COPY_RELOCS
390 && ind
->root
.type
!= bfd_link_hash_indirect
391 && dir
->dynamic_adjusted
)
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
;
405 if (eind
->func_pointer_refcount
> 0)
407 edir
->func_pointer_refcount
+= eind
->func_pointer_refcount
;
408 eind
->func_pointer_refcount
= 0;
411 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
415 /* Remove undefined weak symbol from the dynamic symbol table if it
419 _bfd_x86_elf_fixup_symbol (struct bfd_link_info
*info
,
420 struct elf_link_hash_entry
*h
)
422 if (h
->dynindx
!= -1)
424 const struct elf_backend_data
*bed
425 = get_elf_backend_data (info
->output_bfd
);
426 if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
,
428 elf_x86_hash_entry (h
)->has_got_reloc
,
429 elf_x86_hash_entry (h
)))
432 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
439 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
442 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry
*h
)
444 if (h
->plt
.offset
!= (bfd_vma
) -1
446 && !h
->pointer_equality_needed
)
449 return _bfd_elf_hash_symbol (h
);
452 /* Parse x86 GNU properties. */
454 enum elf_property_kind
455 _bfd_x86_elf_parse_gnu_properties (bfd
*abfd
, unsigned int type
,
456 bfd_byte
*ptr
, unsigned int datasz
)
462 case GNU_PROPERTY_X86_ISA_1_USED
:
463 case GNU_PROPERTY_X86_ISA_1_NEEDED
:
464 case GNU_PROPERTY_X86_FEATURE_1_AND
:
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>"))),
474 return property_corrupt
;
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
;
483 return property_ignored
;
486 return property_number
;
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. */
494 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info
*info
,
495 bfd
*abfd ATTRIBUTE_UNUSED
,
499 unsigned int number
, features
;
500 bfd_boolean updated
= FALSE
;
501 unsigned int pr_type
= aprop
!= NULL
? aprop
->pr_type
: bprop
->pr_type
;
505 case GNU_PROPERTY_X86_ISA_1_USED
:
506 case GNU_PROPERTY_X86_ISA_1_NEEDED
:
507 if (aprop
!= NULL
&& bprop
!= NULL
)
509 number
= aprop
->u
.number
;
510 aprop
->u
.number
= number
| bprop
->u
.number
;
511 updated
= number
!= (unsigned int) aprop
->u
.number
;
515 /* Return TRUE if APROP is NULL to indicate that BPROP should
517 updated
= aprop
== NULL
;
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.
527 if (aprop
!= NULL
&& bprop
!= NULL
)
531 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
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
;
547 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
549 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
552 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
553 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
556 number
= aprop
->u
.number
;
557 aprop
->u
.number
= number
| features
;
558 updated
= number
!= (unsigned int) aprop
->u
.number
;
562 bprop
->u
.number
|= features
;
566 else if (aprop
!= NULL
)
568 aprop
->pr_kind
= property_remove
;
575 /* Never should happen. */