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"
22 #include "elf-vxworks.h"
25 #include "elf/x86-64.h"
27 /* The name of the dynamic interpreter. This is put in the .interp
30 #define ELF32_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
31 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
32 #define ELFX32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
35 _bfd_x86_elf_mkobject (bfd
*abfd
)
37 return bfd_elf_allocate_object (abfd
,
38 sizeof (struct elf_x86_obj_tdata
),
39 get_elf_backend_data (abfd
)->target_id
);
42 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
43 executables. Rather than setting it to the beginning of the TLS
44 section, we have to set it to the end. This function may be called
45 multiple times, it is idempotent. */
48 _bfd_x86_elf_set_tls_module_base (struct bfd_link_info
*info
)
50 struct elf_x86_link_hash_table
*htab
;
51 struct bfd_link_hash_entry
*base
;
52 const struct elf_backend_data
*bed
;
54 if (!bfd_link_executable (info
))
57 bed
= get_elf_backend_data (info
->output_bfd
);
58 htab
= elf_x86_hash_table (info
, bed
->target_id
);
62 base
= htab
->tls_module_base
;
66 base
->u
.def
.value
= htab
->elf
.tls_size
;
69 /* Return the base VMA address which should be subtracted from real addresses
70 when resolving @dtpoff relocation.
71 This is PT_TLS segment p_vaddr. */
74 _bfd_x86_elf_dtpoff_base (struct bfd_link_info
*info
)
76 /* If tls_sec is NULL, we should have signalled an error already. */
77 if (elf_hash_table (info
)->tls_sec
== NULL
)
79 return elf_hash_table (info
)->tls_sec
->vma
;
82 /* Find any dynamic relocs that apply to read-only sections. */
85 _bfd_x86_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
,
88 struct elf_x86_link_hash_entry
*eh
;
89 struct elf_dyn_relocs
*p
;
91 /* Skip local IFUNC symbols. */
92 if (h
->forced_local
&& h
->type
== STT_GNU_IFUNC
)
95 eh
= (struct elf_x86_link_hash_entry
*) h
;
96 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
98 asection
*s
= p
->sec
->output_section
;
100 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
102 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
104 info
->flags
|= DF_TEXTREL
;
106 if ((info
->warn_shared_textrel
&& bfd_link_pic (info
))
107 || info
->error_textrel
)
108 /* xgettext:c-format */
109 info
->callbacks
->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
110 p
->sec
->owner
, h
->root
.root
.string
,
113 /* Not an error, just cut short the traversal. */
120 /* Find and/or create a hash entry for local symbol. */
122 struct elf_link_hash_entry
*
123 _bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table
*htab
,
124 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
127 struct elf_x86_link_hash_entry e
, *ret
;
128 asection
*sec
= abfd
->sections
;
129 hashval_t h
= ELF_LOCAL_SYMBOL_HASH (sec
->id
,
130 htab
->r_sym (rel
->r_info
));
133 e
.elf
.indx
= sec
->id
;
134 e
.elf
.dynstr_index
= htab
->r_sym (rel
->r_info
);
135 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &e
, h
,
136 create
? INSERT
: NO_INSERT
);
143 ret
= (struct elf_x86_link_hash_entry
*) *slot
;
147 ret
= (struct elf_x86_link_hash_entry
*)
148 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
149 sizeof (struct elf_x86_link_hash_entry
));
152 memset (ret
, 0, sizeof (*ret
));
153 ret
->elf
.indx
= sec
->id
;
154 ret
->elf
.dynstr_index
= htab
->r_sym (rel
->r_info
);
155 ret
->elf
.dynindx
= -1;
156 ret
->plt_got
.offset
= (bfd_vma
) -1;
162 /* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN
163 SYNC WITH _bfd_elf_link_hash_newfunc. */
165 struct bfd_hash_entry
*
166 _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
167 struct bfd_hash_table
*table
,
170 /* Allocate the structure if it has not already been allocated by a
174 entry
= (struct bfd_hash_entry
*)
175 bfd_hash_allocate (table
,
176 sizeof (struct elf_x86_link_hash_entry
));
181 /* Call the allocation method of the superclass. */
182 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
185 struct elf_x86_link_hash_entry
*eh
186 = (struct elf_x86_link_hash_entry
*) entry
;
187 struct elf_link_hash_table
*htab
188 = (struct elf_link_hash_table
*) table
;
190 memset (&eh
->elf
.size
, 0,
191 (sizeof (struct elf_x86_link_hash_entry
)
192 - offsetof (struct elf_link_hash_entry
, size
)));
193 /* Set local fields. */
195 eh
->elf
.dynindx
= -1;
196 eh
->elf
.got
= htab
->init_got_refcount
;
197 eh
->elf
.plt
= htab
->init_plt_refcount
;
198 /* Assume that we have been called by a non-ELF symbol reader.
199 This flag is then reset by the code which reads an ELF input
200 file. This ensures that a symbol created by a non-ELF symbol
201 reader will have the flag set correctly. */
203 eh
->plt_second
.offset
= (bfd_vma
) -1;
204 eh
->plt_got
.offset
= (bfd_vma
) -1;
205 eh
->tlsdesc_got
= (bfd_vma
) -1;
211 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
212 for local symbol so that we can handle local STT_GNU_IFUNC symbols
213 as global symbol. We reuse indx and dynstr_index for local symbol
214 hash since they aren't used by global symbols in this backend. */
217 _bfd_x86_elf_local_htab_hash (const void *ptr
)
219 struct elf_link_hash_entry
*h
220 = (struct elf_link_hash_entry
*) ptr
;
221 return ELF_LOCAL_SYMBOL_HASH (h
->indx
, h
->dynstr_index
);
224 /* Compare local hash entries. */
227 _bfd_x86_elf_local_htab_eq (const void *ptr1
, const void *ptr2
)
229 struct elf_link_hash_entry
*h1
230 = (struct elf_link_hash_entry
*) ptr1
;
231 struct elf_link_hash_entry
*h2
232 = (struct elf_link_hash_entry
*) ptr2
;
234 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
237 /* Destroy an x86 ELF linker hash table. */
240 elf_x86_link_hash_table_free (bfd
*obfd
)
242 struct elf_x86_link_hash_table
*htab
243 = (struct elf_x86_link_hash_table
*) obfd
->link
.hash
;
245 if (htab
->loc_hash_table
)
246 htab_delete (htab
->loc_hash_table
);
247 if (htab
->loc_hash_memory
)
248 objalloc_free ((struct objalloc
*) htab
->loc_hash_memory
);
249 _bfd_elf_link_hash_table_free (obfd
);
252 /* Create an x86 ELF linker hash table. */
254 struct bfd_link_hash_table
*
255 _bfd_x86_elf_link_hash_table_create (bfd
*abfd
)
257 struct elf_x86_link_hash_table
*ret
;
258 const struct elf_backend_data
*bed
;
259 bfd_size_type amt
= sizeof (struct elf_x86_link_hash_table
);
261 ret
= (struct elf_x86_link_hash_table
*) bfd_zmalloc (amt
);
265 bed
= get_elf_backend_data (abfd
);
266 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
,
267 _bfd_x86_elf_link_hash_newfunc
,
268 sizeof (struct elf_x86_link_hash_entry
),
278 ret
->r_info
= elf64_r_info
;
279 ret
->r_sym
= elf64_r_sym
;
280 ret
->pointer_r_type
= R_X86_64_64
;
281 ret
->dynamic_interpreter
= ELF64_DYNAMIC_INTERPRETER
;
282 ret
->dynamic_interpreter_size
= sizeof ELF64_DYNAMIC_INTERPRETER
;
283 ret
->tls_get_addr
= "__tls_get_addr";
288 ret
->r_info
= elf32_r_info
;
289 ret
->r_sym
= elf32_r_sym
;
290 if (bed
->target_id
== X86_64_ELF_DATA
)
292 ret
->pointer_r_type
= R_X86_64_32
;
293 ret
->dynamic_interpreter
= ELFX32_DYNAMIC_INTERPRETER
;
294 ret
->dynamic_interpreter_size
295 = sizeof ELFX32_DYNAMIC_INTERPRETER
;
296 ret
->tls_get_addr
= "__tls_get_addr";
300 ret
->pointer_r_type
= R_386_32
;
301 ret
->dynamic_interpreter
= ELF32_DYNAMIC_INTERPRETER
;
302 ret
->dynamic_interpreter_size
303 = sizeof ELF32_DYNAMIC_INTERPRETER
;
304 ret
->tls_get_addr
= "___tls_get_addr";
308 ret
->loc_hash_table
= htab_try_create (1024,
309 _bfd_x86_elf_local_htab_hash
,
310 _bfd_x86_elf_local_htab_eq
,
312 ret
->loc_hash_memory
= objalloc_create ();
313 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
315 elf_x86_link_hash_table_free (abfd
);
318 ret
->elf
.root
.hash_table_free
= elf_x86_link_hash_table_free
;
320 return &ret
->elf
.root
;
323 /* Sort relocs into address order. */
326 _bfd_x86_elf_compare_relocs (const void *ap
, const void *bp
)
328 const arelent
*a
= * (const arelent
**) ap
;
329 const arelent
*b
= * (const arelent
**) bp
;
331 if (a
->address
> b
->address
)
333 else if (a
->address
< b
->address
)
340 _bfd_x86_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
342 if (!bfd_link_relocatable (info
))
344 /* Check for __tls_get_addr reference. */
345 struct elf_x86_link_hash_table
*htab
;
346 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
347 htab
= elf_x86_hash_table (info
, bed
->target_id
);
350 struct elf_link_hash_entry
*h
351 = elf_link_hash_lookup (elf_hash_table (info
),
353 FALSE
, FALSE
, FALSE
);
355 ((struct elf_x86_link_hash_entry
*) h
)->tls_get_addr
= 1;
359 /* Invoke the regular ELF backend linker to do all the work. */
360 return _bfd_elf_link_check_relocs (abfd
, info
);
364 _bfd_x86_elf_always_size_sections (bfd
*output_bfd
,
365 struct bfd_link_info
*info
)
367 asection
*tls_sec
= elf_hash_table (info
)->tls_sec
;
371 struct elf_link_hash_entry
*tlsbase
;
373 tlsbase
= elf_link_hash_lookup (elf_hash_table (info
),
375 FALSE
, FALSE
, FALSE
);
377 if (tlsbase
&& tlsbase
->type
== STT_TLS
)
379 struct elf_x86_link_hash_table
*htab
;
380 struct bfd_link_hash_entry
*bh
= NULL
;
381 const struct elf_backend_data
*bed
382 = get_elf_backend_data (output_bfd
);
384 htab
= elf_x86_hash_table (info
, bed
->target_id
);
388 if (!(_bfd_generic_link_add_one_symbol
389 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
390 tls_sec
, 0, NULL
, FALSE
,
394 htab
->tls_module_base
= bh
;
396 tlsbase
= (struct elf_link_hash_entry
*)bh
;
397 tlsbase
->def_regular
= 1;
398 tlsbase
->other
= STV_HIDDEN
;
399 tlsbase
->root
.linker_def
= 1;
400 (*bed
->elf_backend_hide_symbol
) (info
, tlsbase
, TRUE
);
408 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry
*h
,
409 const Elf_Internal_Sym
*isym
,
410 bfd_boolean definition
,
411 bfd_boolean dynamic ATTRIBUTE_UNUSED
)
415 struct elf_x86_link_hash_entry
*eh
416 = (struct elf_x86_link_hash_entry
*) h
;
417 eh
->def_protected
= (ELF_ST_VISIBILITY (isym
->st_other
)
422 /* Copy the extra info we tack onto an elf_link_hash_entry. */
425 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
426 struct elf_link_hash_entry
*dir
,
427 struct elf_link_hash_entry
*ind
)
429 struct elf_x86_link_hash_entry
*edir
, *eind
;
431 edir
= (struct elf_x86_link_hash_entry
*) dir
;
432 eind
= (struct elf_x86_link_hash_entry
*) ind
;
434 if (eind
->dyn_relocs
!= NULL
)
436 if (edir
->dyn_relocs
!= NULL
)
438 struct elf_dyn_relocs
**pp
;
439 struct elf_dyn_relocs
*p
;
441 /* Add reloc counts against the indirect sym to the direct sym
442 list. Merge any entries against the same section. */
443 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
445 struct elf_dyn_relocs
*q
;
447 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
448 if (q
->sec
== p
->sec
)
450 q
->pc_count
+= p
->pc_count
;
451 q
->count
+= p
->count
;
458 *pp
= edir
->dyn_relocs
;
461 edir
->dyn_relocs
= eind
->dyn_relocs
;
462 eind
->dyn_relocs
= NULL
;
465 if (ind
->root
.type
== bfd_link_hash_indirect
466 && dir
->got
.refcount
<= 0)
468 edir
->tls_type
= eind
->tls_type
;
469 eind
->tls_type
= GOT_UNKNOWN
;
472 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
473 generate a R_386_COPY reloc. */
474 edir
->gotoff_ref
|= eind
->gotoff_ref
;
476 edir
->has_got_reloc
|= eind
->has_got_reloc
;
477 edir
->has_non_got_reloc
|= eind
->has_non_got_reloc
;
479 if (ELIMINATE_COPY_RELOCS
480 && ind
->root
.type
!= bfd_link_hash_indirect
481 && dir
->dynamic_adjusted
)
483 /* If called to transfer flags for a weakdef during processing
484 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
485 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
486 if (dir
->versioned
!= versioned_hidden
)
487 dir
->ref_dynamic
|= ind
->ref_dynamic
;
488 dir
->ref_regular
|= ind
->ref_regular
;
489 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
490 dir
->needs_plt
|= ind
->needs_plt
;
491 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
495 if (eind
->func_pointer_refcount
> 0)
497 edir
->func_pointer_refcount
+= eind
->func_pointer_refcount
;
498 eind
->func_pointer_refcount
= 0;
501 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
505 /* Remove undefined weak symbol from the dynamic symbol table if it
509 _bfd_x86_elf_fixup_symbol (struct bfd_link_info
*info
,
510 struct elf_link_hash_entry
*h
)
512 if (h
->dynindx
!= -1)
514 const struct elf_backend_data
*bed
515 = get_elf_backend_data (info
->output_bfd
);
516 if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
,
518 elf_x86_hash_entry (h
)->has_got_reloc
,
519 elf_x86_hash_entry (h
)))
522 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
529 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
532 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry
*h
)
534 if (h
->plt
.offset
!= (bfd_vma
) -1
536 && !h
->pointer_equality_needed
)
539 return _bfd_elf_hash_symbol (h
);
543 elf_i386_get_plt_got_vma (struct elf_x86_plt
*plt_p ATTRIBUTE_UNUSED
,
545 bfd_vma offset ATTRIBUTE_UNUSED
,
548 return got_addr
+ off
;
552 elf_x86_64_get_plt_got_vma (struct elf_x86_plt
*plt_p
,
555 bfd_vma got_addr ATTRIBUTE_UNUSED
)
557 return plt_p
->sec
->vma
+ offset
+ off
+ plt_p
->plt_got_insn_size
;
561 elf_i386_valid_plt_reloc_p (unsigned int type
)
563 return (type
== R_386_JUMP_SLOT
564 || type
== R_386_GLOB_DAT
565 || type
== R_386_IRELATIVE
);
569 elf_x86_64_valid_plt_reloc_p (unsigned int type
)
571 return (type
== R_X86_64_JUMP_SLOT
572 || type
== R_X86_64_GLOB_DAT
573 || type
== R_X86_64_IRELATIVE
);
577 _bfd_x86_elf_get_synthetic_symtab (bfd
*abfd
,
581 struct elf_x86_plt plts
[],
585 long size
, i
, n
, len
;
587 unsigned int plt_got_offset
, plt_entry_size
;
589 bfd_byte
*plt_contents
;
591 arelent
**dynrelbuf
, *p
;
593 const struct elf_backend_data
*bed
;
594 bfd_vma (*get_plt_got_vma
) (struct elf_x86_plt
*, bfd_vma
, bfd_vma
,
596 bfd_boolean (*valid_plt_reloc_p
) (unsigned int);
601 dynrelbuf
= (arelent
**) bfd_malloc (relsize
);
602 if (dynrelbuf
== NULL
)
605 dynrelcount
= bfd_canonicalize_dynamic_reloc (abfd
, dynrelbuf
,
608 /* Sort the relocs by address. */
609 qsort (dynrelbuf
, dynrelcount
, sizeof (arelent
*),
610 _bfd_x86_elf_compare_relocs
);
612 size
= count
* sizeof (asymbol
);
614 /* Allocate space for @plt suffixes. */
616 for (i
= 0; i
< dynrelcount
; i
++)
619 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
621 size
+= sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd
);
624 s
= *ret
= (asymbol
*) bfd_zmalloc (size
);
628 bed
= get_elf_backend_data (abfd
);
630 if (bed
->target_id
== X86_64_ELF_DATA
)
632 get_plt_got_vma
= elf_x86_64_get_plt_got_vma
;
633 valid_plt_reloc_p
= elf_x86_64_valid_plt_reloc_p
;
637 get_plt_got_vma
= elf_i386_get_plt_got_vma
;
638 valid_plt_reloc_p
= elf_i386_valid_plt_reloc_p
;
641 /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
643 asection
*sec
= bfd_get_section_by_name (abfd
, ".got.plt");
648 sec
= bfd_get_section_by_name (abfd
, ".got");
653 if (got_addr
== (bfd_vma
) -1)
658 /* Check for each PLT section. */
659 names
= (char *) (s
+ count
);
662 for (j
= 0; plts
[j
].name
!= NULL
; j
++)
663 if ((plt_contents
= plts
[j
].contents
) != NULL
)
668 struct elf_x86_plt
*plt_p
= &plts
[j
];
670 plt_got_offset
= plt_p
->plt_got_offset
;
671 plt_entry_size
= plt_p
->plt_entry_size
;
675 if ((plt_p
->type
& plt_lazy
))
677 /* Skip PLT0 in lazy PLT. */
679 offset
= plt_entry_size
;
687 /* Check each PLT entry against dynamic relocations. */
688 for (; k
< plt_p
->count
; k
++)
694 /* Get the GOT offset for i386 or the PC-relative offset
695 for x86-64, a signed 32-bit integer. */
696 off
= H_GET_32 (abfd
, (plt_contents
+ offset
698 got_vma
= get_plt_got_vma (plt_p
, off
, offset
, got_addr
);
704 while ((min
+ 1) < max
)
708 mid
= (min
+ max
) / 2;
710 if (got_vma
> r
->address
)
712 else if (got_vma
< r
->address
)
721 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
722 if (got_vma
== p
->address
724 && valid_plt_reloc_p (p
->howto
->type
))
726 *s
= **p
->sym_ptr_ptr
;
727 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
728 set. Since we are defining a symbol, ensure one
730 if ((s
->flags
& BSF_LOCAL
) == 0)
731 s
->flags
|= BSF_GLOBAL
;
732 s
->flags
|= BSF_SYNTHETIC
;
733 /* This is no longer a section symbol. */
734 s
->flags
&= ~BSF_SECTION_SYM
;
736 s
->the_bfd
= plt
->owner
;
740 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
741 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
747 memcpy (names
, "+0x", sizeof ("+0x") - 1);
748 names
+= sizeof ("+0x") - 1;
749 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
750 for (a
= buf
; *a
== '0'; ++a
)
753 memcpy (names
, a
, size
);
756 memcpy (names
, "@plt", sizeof ("@plt"));
757 names
+= sizeof ("@plt");
761 offset
+= plt_entry_size
;
765 /* PLT entries with R_386_TLS_DESC relocations are skipped. */
774 for (j
= 0; plts
[j
].name
!= NULL
; j
++)
775 if (plts
[j
].contents
!= NULL
)
776 free (plts
[j
].contents
);
783 /* Parse x86 GNU properties. */
785 enum elf_property_kind
786 _bfd_x86_elf_parse_gnu_properties (bfd
*abfd
, unsigned int type
,
787 bfd_byte
*ptr
, unsigned int datasz
)
793 case GNU_PROPERTY_X86_ISA_1_USED
:
794 case GNU_PROPERTY_X86_ISA_1_NEEDED
:
795 case GNU_PROPERTY_X86_FEATURE_1_AND
:
799 ((type
== GNU_PROPERTY_X86_ISA_1_USED
800 ? _("error: %B: <corrupt x86 ISA used size: 0x%x>")
801 : (type
== GNU_PROPERTY_X86_ISA_1_NEEDED
802 ? _("error: %B: <corrupt x86 ISA needed size: 0x%x>")
803 : _("error: %B: <corrupt x86 feature size: 0x%x>"))),
805 return property_corrupt
;
807 prop
= _bfd_elf_get_property (abfd
, type
, datasz
);
808 /* Combine properties of the same type. */
809 prop
->u
.number
|= bfd_h_get_32 (abfd
, ptr
);
810 prop
->pr_kind
= property_number
;
814 return property_ignored
;
817 return property_number
;
820 /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
821 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
822 should be merged with ABFD. */
825 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info
*info
,
826 bfd
*abfd ATTRIBUTE_UNUSED
,
830 unsigned int number
, features
;
831 bfd_boolean updated
= FALSE
;
832 unsigned int pr_type
= aprop
!= NULL
? aprop
->pr_type
: bprop
->pr_type
;
836 case GNU_PROPERTY_X86_ISA_1_USED
:
837 case GNU_PROPERTY_X86_ISA_1_NEEDED
:
838 if (aprop
!= NULL
&& bprop
!= NULL
)
840 number
= aprop
->u
.number
;
841 aprop
->u
.number
= number
| bprop
->u
.number
;
842 updated
= number
!= (unsigned int) aprop
->u
.number
;
846 /* Return TRUE if APROP is NULL to indicate that BPROP should
848 updated
= aprop
== NULL
;
852 case GNU_PROPERTY_X86_FEATURE_1_AND
:
853 /* Only one of APROP and BPROP can be NULL:
854 1. APROP & BPROP when both APROP and BPROP aren't NULL.
855 2. If APROP is NULL, remove x86 feature.
856 3. Otherwise, do nothing.
858 if (aprop
!= NULL
&& bprop
!= NULL
)
862 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
864 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
865 number
= aprop
->u
.number
;
866 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
867 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
868 aprop
->u
.number
= (number
& bprop
->u
.number
) | features
;
869 updated
= number
!= (unsigned int) aprop
->u
.number
;
870 /* Remove the property if all feature bits are cleared. */
871 if (aprop
->u
.number
== 0)
872 aprop
->pr_kind
= property_remove
;
878 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
880 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
883 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
884 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
887 number
= aprop
->u
.number
;
888 aprop
->u
.number
= number
| features
;
889 updated
= number
!= (unsigned int) aprop
->u
.number
;
893 bprop
->u
.number
|= features
;
897 else if (aprop
!= NULL
)
899 aprop
->pr_kind
= property_remove
;
906 /* Never should happen. */
913 /* Set up x86 GNU properties. Return the first relocatable ELF input
914 with GNU properties if found. Otherwise, return NULL. */
917 _bfd_x86_elf_link_setup_gnu_properties
918 (struct bfd_link_info
*info
,
919 struct elf_x86_plt_layout_table
*plt_layout
)
921 bfd_boolean normal_target
;
922 bfd_boolean lazy_plt
;
923 asection
*sec
, *pltsec
;
925 bfd_boolean use_ibt_plt
;
926 unsigned int plt_alignment
, features
;
927 struct elf_x86_link_hash_table
*htab
;
931 const struct elf_backend_data
*bed
;
932 unsigned int class_align
= ABI_64_P (info
->output_bfd
) ? 3 : 2;
933 unsigned int got_align
;
937 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
939 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
941 /* Find a normal input file with GNU property note. */
942 for (pbfd
= info
->input_bfds
;
944 pbfd
= pbfd
->link
.next
)
945 if (bfd_get_flavour (pbfd
) == bfd_target_elf_flavour
946 && bfd_count_sections (pbfd
) != 0)
950 if (elf_properties (pbfd
) != NULL
)
954 if (ebfd
!= NULL
&& features
)
956 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and
957 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
958 prop
= _bfd_elf_get_property (ebfd
,
959 GNU_PROPERTY_X86_FEATURE_1_AND
,
961 prop
->u
.number
|= features
;
962 prop
->pr_kind
= property_number
;
964 /* Create the GNU property note section if needed. */
967 sec
= bfd_make_section_with_flags (ebfd
,
968 NOTE_GNU_PROPERTY_SECTION_NAME
,
976 info
->callbacks
->einfo (_("%F: failed to create GNU property section\n"));
978 if (!bfd_set_section_alignment (ebfd
, sec
, class_align
))
981 info
->callbacks
->einfo (_("%F%A: failed to align section\n"),
985 elf_section_type (sec
) = SHT_NOTE
;
989 pbfd
= _bfd_elf_link_setup_gnu_properties (info
);
991 if (bfd_link_relocatable (info
))
994 bed
= get_elf_backend_data (info
->output_bfd
);
996 htab
= elf_x86_hash_table (info
, bed
->target_id
);
1000 use_ibt_plt
= info
->ibtplt
|| info
->ibt
;
1001 if (!use_ibt_plt
&& pbfd
!= NULL
)
1003 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
1004 elf_property_list
*p
;
1006 /* The property list is sorted in order of type. */
1007 for (p
= elf_properties (pbfd
); p
; p
= p
->next
)
1009 if (GNU_PROPERTY_X86_FEATURE_1_AND
== p
->property
.pr_type
)
1011 use_ibt_plt
= !!(p
->property
.u
.number
1012 & GNU_PROPERTY_X86_FEATURE_1_IBT
);
1015 else if (GNU_PROPERTY_X86_FEATURE_1_AND
< p
->property
.pr_type
)
1020 dynobj
= htab
->elf
.dynobj
;
1022 /* Set htab->elf.dynobj here so that there is no need to check and
1023 set it in check_relocs. */
1028 htab
->elf
.dynobj
= pbfd
;
1035 /* Find a normal input file to hold linker created
1037 for (abfd
= info
->input_bfds
;
1039 abfd
= abfd
->link
.next
)
1040 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1042 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0)
1044 htab
->elf
.dynobj
= abfd
;
1051 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
1052 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
1053 canonical function address. */
1054 htab
->plt
.has_plt0
= 1;
1055 normal_target
= plt_layout
->normal_target
;
1061 htab
->lazy_plt
= plt_layout
->lazy_ibt_plt
;
1062 htab
->non_lazy_plt
= plt_layout
->non_lazy_ibt_plt
;
1066 htab
->lazy_plt
= plt_layout
->lazy_plt
;
1067 htab
->non_lazy_plt
= plt_layout
->non_lazy_plt
;
1072 htab
->lazy_plt
= plt_layout
->lazy_plt
;
1073 htab
->non_lazy_plt
= NULL
;
1076 pltsec
= htab
->elf
.splt
;
1078 /* If the non-lazy PLT is available, use it for all PLT entries if
1079 there are no PLT0 or no .plt section. */
1080 if (htab
->non_lazy_plt
!= NULL
1081 && (!htab
->plt
.has_plt0
|| pltsec
== NULL
))
1084 if (bfd_link_pic (info
))
1085 htab
->plt
.plt_entry
= htab
->non_lazy_plt
->pic_plt_entry
;
1087 htab
->plt
.plt_entry
= htab
->non_lazy_plt
->plt_entry
;
1088 htab
->plt
.plt_entry_size
= htab
->non_lazy_plt
->plt_entry_size
;
1089 htab
->plt
.plt_got_offset
= htab
->non_lazy_plt
->plt_got_offset
;
1090 htab
->plt
.plt_got_insn_size
1091 = htab
->non_lazy_plt
->plt_got_insn_size
;
1092 htab
->plt
.eh_frame_plt_size
1093 = htab
->non_lazy_plt
->eh_frame_plt_size
;
1094 htab
->plt
.eh_frame_plt
= htab
->non_lazy_plt
->eh_frame_plt
;
1099 if (bfd_link_pic (info
))
1101 htab
->plt
.plt0_entry
= htab
->lazy_plt
->pic_plt0_entry
;
1102 htab
->plt
.plt_entry
= htab
->lazy_plt
->pic_plt_entry
;
1106 htab
->plt
.plt0_entry
= htab
->lazy_plt
->plt0_entry
;
1107 htab
->plt
.plt_entry
= htab
->lazy_plt
->plt_entry
;
1109 htab
->plt
.plt_entry_size
= htab
->lazy_plt
->plt_entry_size
;
1110 htab
->plt
.plt_got_offset
= htab
->lazy_plt
->plt_got_offset
;
1111 htab
->plt
.plt_got_insn_size
1112 = htab
->lazy_plt
->plt_got_insn_size
;
1113 htab
->plt
.eh_frame_plt_size
1114 = htab
->lazy_plt
->eh_frame_plt_size
;
1115 htab
->plt
.eh_frame_plt
= htab
->lazy_plt
->eh_frame_plt
;
1118 /* Return if there are no normal input files. */
1122 if (plt_layout
->is_vxworks
1123 && !elf_vxworks_create_dynamic_sections (dynobj
, info
,
1126 info
->callbacks
->einfo (_("%F: failed to create VxWorks dynamic sections\n"));
1130 /* Since create_dynamic_sections isn't always called, but GOT
1131 relocations need GOT relocations, create them here so that we
1132 don't need to do it in check_relocs. */
1133 if (htab
->elf
.sgot
== NULL
1134 && !_bfd_elf_create_got_section (dynobj
, info
))
1135 info
->callbacks
->einfo (_("%F: failed to create GOT sections\n"));
1137 got_align
= (bed
->target_id
== X86_64_ELF_DATA
) ? 3 : 2;
1139 /* Align .got and .got.plt sections to their entry size. Do it here
1140 instead of in create_dynamic_sections so that they are always
1141 properly aligned even if create_dynamic_sections isn't called. */
1142 sec
= htab
->elf
.sgot
;
1143 if (!bfd_set_section_alignment (dynobj
, sec
, got_align
))
1144 goto error_alignment
;
1146 sec
= htab
->elf
.sgotplt
;
1147 if (!bfd_set_section_alignment (dynobj
, sec
, got_align
))
1148 goto error_alignment
;
1150 /* Create the ifunc sections here so that check_relocs can be
1152 if (!_bfd_elf_create_ifunc_sections (dynobj
, info
))
1153 info
->callbacks
->einfo (_("%F: failed to create ifunc sections\n"));
1155 plt_alignment
= bfd_log2 (htab
->plt
.plt_entry_size
);
1159 /* Whe creating executable, set the contents of the .interp
1160 section to the interpreter. */
1161 if (bfd_link_executable (info
) && !info
->nointerp
)
1163 asection
*s
= bfd_get_linker_section (dynobj
, ".interp");
1166 s
->size
= htab
->dynamic_interpreter_size
;
1167 s
->contents
= (unsigned char *) htab
->dynamic_interpreter
;
1171 /* Don't change PLT section alignment for NaCl since it uses
1172 64-byte PLT entry and sets PLT section alignment to 32
1173 bytes. Don't create additional PLT sections for NaCl. */
1176 flagword pltflags
= (bed
->dynamic_sec_flags
1181 unsigned int non_lazy_plt_alignment
1182 = bfd_log2 (htab
->non_lazy_plt
->plt_entry_size
);
1185 if (!bfd_set_section_alignment (sec
->owner
, sec
,
1187 goto error_alignment
;
1189 /* Create the GOT procedure linkage table. */
1190 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1194 info
->callbacks
->einfo (_("%F: failed to create GOT PLT section\n"));
1196 if (!bfd_set_section_alignment (dynobj
, sec
,
1197 non_lazy_plt_alignment
))
1198 goto error_alignment
;
1200 htab
->plt_got
= sec
;
1208 /* Create the second PLT for Intel IBT support. IBT
1209 PLT is supported only for non-NaCl target and is
1210 is needed only for lazy binding. */
1211 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1215 info
->callbacks
->einfo (_("%F: failed to create IBT-enabled PLT section\n"));
1217 if (!bfd_set_section_alignment (dynobj
, sec
,
1219 goto error_alignment
;
1221 else if (info
->bndplt
&& ABI_64_P (dynobj
))
1223 /* Create the second PLT for Intel MPX support. MPX
1224 PLT is supported only for non-NaCl target in 64-bit
1225 mode and is needed only for lazy binding. */
1226 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1230 info
->callbacks
->einfo (_("%F: failed to create BND PLT section\n"));
1232 if (!bfd_set_section_alignment (dynobj
, sec
,
1233 non_lazy_plt_alignment
))
1234 goto error_alignment
;
1237 htab
->plt_second
= sec
;
1241 if (!info
->no_ld_generated_unwind_info
)
1243 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1244 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
1245 | SEC_LINKER_CREATED
);
1247 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1251 info
->callbacks
->einfo (_("%F: failed to create PLT .eh_frame section\n"));
1253 if (!bfd_set_section_alignment (dynobj
, sec
, class_align
))
1254 goto error_alignment
;
1256 htab
->plt_eh_frame
= sec
;
1258 if (htab
->plt_got
!= NULL
)
1260 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1264 info
->callbacks
->einfo (_("%F: failed to create GOT PLT .eh_frame section\n"));
1266 if (!bfd_set_section_alignment (dynobj
, sec
, class_align
))
1267 goto error_alignment
;
1269 htab
->plt_got_eh_frame
= sec
;
1272 if (htab
->plt_second
!= NULL
)
1274 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1278 info
->callbacks
->einfo (_("%F: failed to create the second PLT .eh_frame section\n"));
1280 if (!bfd_set_section_alignment (dynobj
, sec
, class_align
))
1281 goto error_alignment
;
1283 htab
->plt_second_eh_frame
= sec
;
1290 /* The .iplt section is used for IFUNC symbols in static
1292 sec
= htab
->elf
.iplt
;
1294 && !bfd_set_section_alignment (sec
->owner
, sec
,
1296 goto error_alignment
;