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"
34 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
35 executables. Rather than setting it to the beginning of the TLS
36 section, we have to set it to the end. This function may be called
37 multiple times, it is idempotent. */
40 _bfd_x86_elf_set_tls_module_base (struct bfd_link_info
*info
)
42 struct elf_x86_link_hash_table
*htab
;
43 struct bfd_link_hash_entry
*base
;
44 const struct elf_backend_data
*bed
;
46 if (!bfd_link_executable (info
))
49 bed
= get_elf_backend_data (info
->output_bfd
);
50 htab
= elf_x86_hash_table (info
, bed
->target_id
);
54 base
= htab
->tls_module_base
;
58 base
->u
.def
.value
= htab
->elf
.tls_size
;
61 /* Return the base VMA address which should be subtracted from real addresses
62 when resolving @dtpoff relocation.
63 This is PT_TLS segment p_vaddr. */
66 _bfd_x86_elf_dtpoff_base (struct bfd_link_info
*info
)
68 /* If tls_sec is NULL, we should have signalled an error already. */
69 if (elf_hash_table (info
)->tls_sec
== NULL
)
71 return elf_hash_table (info
)->tls_sec
->vma
;
74 /* Find any dynamic relocs that apply to read-only sections. */
77 _bfd_x86_elf_readonly_dynrelocs (struct elf_link_hash_entry
*h
,
80 struct elf_x86_link_hash_entry
*eh
;
81 struct elf_dyn_relocs
*p
;
83 /* Skip local IFUNC symbols. */
84 if (h
->forced_local
&& h
->type
== STT_GNU_IFUNC
)
87 eh
= (struct elf_x86_link_hash_entry
*) h
;
88 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
90 asection
*s
= p
->sec
->output_section
;
92 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
94 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
96 info
->flags
|= DF_TEXTREL
;
98 if ((info
->warn_shared_textrel
&& bfd_link_pic (info
))
99 || info
->error_textrel
)
100 /* xgettext:c-format */
101 info
->callbacks
->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
102 p
->sec
->owner
, h
->root
.root
.string
,
105 /* Not an error, just cut short the traversal. */
112 /* Find and/or create a hash entry for local symbol. */
114 struct elf_link_hash_entry
*
115 _bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table
*htab
,
116 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
119 struct elf_x86_link_hash_entry e
, *ret
;
120 asection
*sec
= abfd
->sections
;
121 hashval_t h
= ELF_LOCAL_SYMBOL_HASH (sec
->id
,
122 htab
->r_sym (rel
->r_info
));
125 e
.elf
.indx
= sec
->id
;
126 e
.elf
.dynstr_index
= htab
->r_sym (rel
->r_info
);
127 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &e
, h
,
128 create
? INSERT
: NO_INSERT
);
135 ret
= (struct elf_x86_link_hash_entry
*) *slot
;
139 ret
= (struct elf_x86_link_hash_entry
*)
140 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
141 sizeof (struct elf_x86_link_hash_entry
));
144 memset (ret
, 0, sizeof (*ret
));
145 ret
->elf
.indx
= sec
->id
;
146 ret
->elf
.dynstr_index
= htab
->r_sym (rel
->r_info
);
147 ret
->elf
.dynindx
= -1;
148 ret
->plt_got
.offset
= (bfd_vma
) -1;
154 /* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN
155 SYNC WITH _bfd_elf_link_hash_newfunc. */
157 struct bfd_hash_entry
*
158 _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
159 struct bfd_hash_table
*table
,
162 /* Allocate the structure if it has not already been allocated by a
166 entry
= (struct bfd_hash_entry
*)
167 bfd_hash_allocate (table
,
168 sizeof (struct elf_x86_link_hash_entry
));
173 /* Call the allocation method of the superclass. */
174 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
177 struct elf_x86_link_hash_entry
*eh
178 = (struct elf_x86_link_hash_entry
*) entry
;
179 struct elf_link_hash_table
*htab
180 = (struct elf_link_hash_table
*) table
;
182 memset (&eh
->elf
.size
, 0,
183 (sizeof (struct elf_x86_link_hash_entry
)
184 - offsetof (struct elf_link_hash_entry
, size
)));
185 /* Set local fields. */
187 eh
->elf
.dynindx
= -1;
188 eh
->elf
.got
= htab
->init_got_refcount
;
189 eh
->elf
.plt
= htab
->init_plt_refcount
;
190 /* Assume that we have been called by a non-ELF symbol reader.
191 This flag is then reset by the code which reads an ELF input
192 file. This ensures that a symbol created by a non-ELF symbol
193 reader will have the flag set correctly. */
195 eh
->plt_second
.offset
= (bfd_vma
) -1;
196 eh
->plt_got
.offset
= (bfd_vma
) -1;
197 eh
->tlsdesc_got
= (bfd_vma
) -1;
203 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
204 for local symbol so that we can handle local STT_GNU_IFUNC symbols
205 as global symbol. We reuse indx and dynstr_index for local symbol
206 hash since they aren't used by global symbols in this backend. */
209 _bfd_x86_elf_local_htab_hash (const void *ptr
)
211 struct elf_link_hash_entry
*h
212 = (struct elf_link_hash_entry
*) ptr
;
213 return ELF_LOCAL_SYMBOL_HASH (h
->indx
, h
->dynstr_index
);
216 /* Compare local hash entries. */
219 _bfd_x86_elf_local_htab_eq (const void *ptr1
, const void *ptr2
)
221 struct elf_link_hash_entry
*h1
222 = (struct elf_link_hash_entry
*) ptr1
;
223 struct elf_link_hash_entry
*h2
224 = (struct elf_link_hash_entry
*) ptr2
;
226 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
229 /* Destroy an x86 ELF linker hash table. */
232 elf_x86_link_hash_table_free (bfd
*obfd
)
234 struct elf_x86_link_hash_table
*htab
235 = (struct elf_x86_link_hash_table
*) obfd
->link
.hash
;
237 if (htab
->loc_hash_table
)
238 htab_delete (htab
->loc_hash_table
);
239 if (htab
->loc_hash_memory
)
240 objalloc_free ((struct objalloc
*) htab
->loc_hash_memory
);
241 _bfd_elf_link_hash_table_free (obfd
);
244 /* Create an x86 ELF linker hash table. */
246 struct bfd_link_hash_table
*
247 _bfd_x86_elf_link_hash_table_create (bfd
*abfd
)
249 struct elf_x86_link_hash_table
*ret
;
250 const struct elf_backend_data
*bed
;
251 bfd_size_type amt
= sizeof (struct elf_x86_link_hash_table
);
253 ret
= (struct elf_x86_link_hash_table
*) bfd_zmalloc (amt
);
257 bed
= get_elf_backend_data (abfd
);
258 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
,
259 _bfd_x86_elf_link_hash_newfunc
,
260 sizeof (struct elf_x86_link_hash_entry
),
270 ret
->r_info
= elf64_r_info
;
271 ret
->r_sym
= elf64_r_sym
;
272 ret
->pointer_r_type
= R_X86_64_64
;
273 ret
->dynamic_interpreter
= ELF64_DYNAMIC_INTERPRETER
;
274 ret
->dynamic_interpreter_size
= sizeof ELF64_DYNAMIC_INTERPRETER
;
275 ret
->tls_get_addr
= "__tls_get_addr";
280 ret
->r_info
= elf32_r_info
;
281 ret
->r_sym
= elf32_r_sym
;
282 if (bed
->target_id
== X86_64_ELF_DATA
)
284 ret
->pointer_r_type
= R_X86_64_32
;
285 ret
->dynamic_interpreter
= ELFX32_DYNAMIC_INTERPRETER
;
286 ret
->dynamic_interpreter_size
287 = sizeof ELFX32_DYNAMIC_INTERPRETER
;
288 ret
->tls_get_addr
= "__tls_get_addr";
292 ret
->pointer_r_type
= R_386_32
;
293 ret
->dynamic_interpreter
= ELF32_DYNAMIC_INTERPRETER
;
294 ret
->dynamic_interpreter_size
295 = sizeof ELF32_DYNAMIC_INTERPRETER
;
296 ret
->tls_get_addr
= "___tls_get_addr";
300 ret
->loc_hash_table
= htab_try_create (1024,
301 _bfd_x86_elf_local_htab_hash
,
302 _bfd_x86_elf_local_htab_eq
,
304 ret
->loc_hash_memory
= objalloc_create ();
305 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
307 elf_x86_link_hash_table_free (abfd
);
310 ret
->elf
.root
.hash_table_free
= elf_x86_link_hash_table_free
;
312 return &ret
->elf
.root
;
315 /* Sort relocs into address order. */
318 _bfd_x86_elf_compare_relocs (const void *ap
, const void *bp
)
320 const arelent
*a
= * (const arelent
**) ap
;
321 const arelent
*b
= * (const arelent
**) bp
;
323 if (a
->address
> b
->address
)
325 else if (a
->address
< b
->address
)
332 _bfd_x86_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
334 if (!bfd_link_relocatable (info
))
336 /* Check for __tls_get_addr reference. */
337 struct elf_x86_link_hash_table
*htab
;
338 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
339 htab
= elf_x86_hash_table (info
, bed
->target_id
);
342 struct elf_link_hash_entry
*h
343 = elf_link_hash_lookup (elf_hash_table (info
),
345 FALSE
, FALSE
, FALSE
);
347 ((struct elf_x86_link_hash_entry
*) h
)->tls_get_addr
= 1;
351 /* Invoke the regular ELF backend linker to do all the work. */
352 return _bfd_elf_link_check_relocs (abfd
, info
);
356 _bfd_x86_elf_always_size_sections (bfd
*output_bfd
,
357 struct bfd_link_info
*info
)
359 asection
*tls_sec
= elf_hash_table (info
)->tls_sec
;
363 struct elf_link_hash_entry
*tlsbase
;
365 tlsbase
= elf_link_hash_lookup (elf_hash_table (info
),
367 FALSE
, FALSE
, FALSE
);
369 if (tlsbase
&& tlsbase
->type
== STT_TLS
)
371 struct elf_x86_link_hash_table
*htab
;
372 struct bfd_link_hash_entry
*bh
= NULL
;
373 const struct elf_backend_data
*bed
374 = get_elf_backend_data (output_bfd
);
376 htab
= elf_x86_hash_table (info
, bed
->target_id
);
380 if (!(_bfd_generic_link_add_one_symbol
381 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
382 tls_sec
, 0, NULL
, FALSE
,
386 htab
->tls_module_base
= bh
;
388 tlsbase
= (struct elf_link_hash_entry
*)bh
;
389 tlsbase
->def_regular
= 1;
390 tlsbase
->other
= STV_HIDDEN
;
391 tlsbase
->root
.linker_def
= 1;
392 (*bed
->elf_backend_hide_symbol
) (info
, tlsbase
, TRUE
);
400 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry
*h
,
401 const Elf_Internal_Sym
*isym
,
402 bfd_boolean definition
,
403 bfd_boolean dynamic ATTRIBUTE_UNUSED
)
407 struct elf_x86_link_hash_entry
*eh
408 = (struct elf_x86_link_hash_entry
*) h
;
409 eh
->def_protected
= (ELF_ST_VISIBILITY (isym
->st_other
)
414 /* Copy the extra info we tack onto an elf_link_hash_entry. */
417 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
418 struct elf_link_hash_entry
*dir
,
419 struct elf_link_hash_entry
*ind
)
421 struct elf_x86_link_hash_entry
*edir
, *eind
;
423 edir
= (struct elf_x86_link_hash_entry
*) dir
;
424 eind
= (struct elf_x86_link_hash_entry
*) ind
;
426 if (eind
->dyn_relocs
!= NULL
)
428 if (edir
->dyn_relocs
!= NULL
)
430 struct elf_dyn_relocs
**pp
;
431 struct elf_dyn_relocs
*p
;
433 /* Add reloc counts against the indirect sym to the direct sym
434 list. Merge any entries against the same section. */
435 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
437 struct elf_dyn_relocs
*q
;
439 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
440 if (q
->sec
== p
->sec
)
442 q
->pc_count
+= p
->pc_count
;
443 q
->count
+= p
->count
;
450 *pp
= edir
->dyn_relocs
;
453 edir
->dyn_relocs
= eind
->dyn_relocs
;
454 eind
->dyn_relocs
= NULL
;
457 if (ind
->root
.type
== bfd_link_hash_indirect
458 && dir
->got
.refcount
<= 0)
460 edir
->tls_type
= eind
->tls_type
;
461 eind
->tls_type
= GOT_UNKNOWN
;
464 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
465 generate a R_386_COPY reloc. */
466 edir
->gotoff_ref
|= eind
->gotoff_ref
;
468 edir
->has_got_reloc
|= eind
->has_got_reloc
;
469 edir
->has_non_got_reloc
|= eind
->has_non_got_reloc
;
471 if (ELIMINATE_COPY_RELOCS
472 && ind
->root
.type
!= bfd_link_hash_indirect
473 && dir
->dynamic_adjusted
)
475 /* If called to transfer flags for a weakdef during processing
476 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
477 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
478 if (dir
->versioned
!= versioned_hidden
)
479 dir
->ref_dynamic
|= ind
->ref_dynamic
;
480 dir
->ref_regular
|= ind
->ref_regular
;
481 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
482 dir
->needs_plt
|= ind
->needs_plt
;
483 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
487 if (eind
->func_pointer_refcount
> 0)
489 edir
->func_pointer_refcount
+= eind
->func_pointer_refcount
;
490 eind
->func_pointer_refcount
= 0;
493 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
497 /* Remove undefined weak symbol from the dynamic symbol table if it
501 _bfd_x86_elf_fixup_symbol (struct bfd_link_info
*info
,
502 struct elf_link_hash_entry
*h
)
504 if (h
->dynindx
!= -1)
506 const struct elf_backend_data
*bed
507 = get_elf_backend_data (info
->output_bfd
);
508 if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
,
510 elf_x86_hash_entry (h
)->has_got_reloc
,
511 elf_x86_hash_entry (h
)))
514 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
521 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
524 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry
*h
)
526 if (h
->plt
.offset
!= (bfd_vma
) -1
528 && !h
->pointer_equality_needed
)
531 return _bfd_elf_hash_symbol (h
);
535 elf_i386_get_plt_got_vma (struct elf_x86_plt
*plt_p ATTRIBUTE_UNUSED
,
537 bfd_vma offset ATTRIBUTE_UNUSED
,
540 return got_addr
+ off
;
544 elf_x86_64_get_plt_got_vma (struct elf_x86_plt
*plt_p
,
547 bfd_vma got_addr ATTRIBUTE_UNUSED
)
549 return plt_p
->sec
->vma
+ offset
+ off
+ plt_p
->plt_got_insn_size
;
553 elf_i386_valid_plt_reloc_p (unsigned int type
)
555 return (type
== R_386_JUMP_SLOT
556 || type
== R_386_GLOB_DAT
557 || type
== R_386_IRELATIVE
);
561 elf_x86_64_valid_plt_reloc_p (unsigned int type
)
563 return (type
== R_X86_64_JUMP_SLOT
564 || type
== R_X86_64_GLOB_DAT
565 || type
== R_X86_64_IRELATIVE
);
569 _bfd_x86_elf_get_synthetic_symtab (bfd
*abfd
,
573 struct elf_x86_plt plts
[],
577 long size
, i
, n
, len
;
579 unsigned int plt_got_offset
, plt_entry_size
;
581 bfd_byte
*plt_contents
;
583 arelent
**dynrelbuf
, *p
;
585 const struct elf_backend_data
*bed
;
586 bfd_vma (*get_plt_got_vma
) (struct elf_x86_plt
*, bfd_vma
, bfd_vma
,
588 bfd_boolean (*valid_plt_reloc_p
) (unsigned int);
593 dynrelbuf
= (arelent
**) bfd_malloc (relsize
);
594 if (dynrelbuf
== NULL
)
597 dynrelcount
= bfd_canonicalize_dynamic_reloc (abfd
, dynrelbuf
,
600 /* Sort the relocs by address. */
601 qsort (dynrelbuf
, dynrelcount
, sizeof (arelent
*),
602 _bfd_x86_elf_compare_relocs
);
604 size
= count
* sizeof (asymbol
);
606 /* Allocate space for @plt suffixes. */
608 for (i
= 0; i
< dynrelcount
; i
++)
611 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
613 size
+= sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd
);
616 s
= *ret
= (asymbol
*) bfd_zmalloc (size
);
620 bed
= get_elf_backend_data (abfd
);
622 if (bed
->target_id
== X86_64_ELF_DATA
)
624 get_plt_got_vma
= elf_x86_64_get_plt_got_vma
;
625 valid_plt_reloc_p
= elf_x86_64_valid_plt_reloc_p
;
629 get_plt_got_vma
= elf_i386_get_plt_got_vma
;
630 valid_plt_reloc_p
= elf_i386_valid_plt_reloc_p
;
633 /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
635 asection
*sec
= bfd_get_section_by_name (abfd
, ".got.plt");
640 sec
= bfd_get_section_by_name (abfd
, ".got");
645 if (got_addr
== (bfd_vma
) -1)
650 /* Check for each PLT section. */
651 names
= (char *) (s
+ count
);
654 for (j
= 0; plts
[j
].name
!= NULL
; j
++)
655 if ((plt_contents
= plts
[j
].contents
) != NULL
)
660 struct elf_x86_plt
*plt_p
= &plts
[j
];
662 plt_got_offset
= plt_p
->plt_got_offset
;
663 plt_entry_size
= plt_p
->plt_entry_size
;
667 if ((plt_p
->type
& plt_lazy
))
669 /* Skip PLT0 in lazy PLT. */
671 offset
= plt_entry_size
;
679 /* Check each PLT entry against dynamic relocations. */
680 for (; k
< plt_p
->count
; k
++)
686 /* Get the GOT offset for i386 or the PC-relative offset
687 for x86-64, a signed 32-bit integer. */
688 off
= H_GET_32 (abfd
, (plt_contents
+ offset
690 got_vma
= get_plt_got_vma (plt_p
, off
, offset
, got_addr
);
696 while ((min
+ 1) < max
)
700 mid
= (min
+ max
) / 2;
702 if (got_vma
> r
->address
)
704 else if (got_vma
< r
->address
)
713 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
714 if (got_vma
== p
->address
716 && valid_plt_reloc_p (p
->howto
->type
))
718 *s
= **p
->sym_ptr_ptr
;
719 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
720 set. Since we are defining a symbol, ensure one
722 if ((s
->flags
& BSF_LOCAL
) == 0)
723 s
->flags
|= BSF_GLOBAL
;
724 s
->flags
|= BSF_SYNTHETIC
;
725 /* This is no longer a section symbol. */
726 s
->flags
&= ~BSF_SECTION_SYM
;
728 s
->the_bfd
= plt
->owner
;
732 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
733 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
739 memcpy (names
, "+0x", sizeof ("+0x") - 1);
740 names
+= sizeof ("+0x") - 1;
741 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
742 for (a
= buf
; *a
== '0'; ++a
)
745 memcpy (names
, a
, size
);
748 memcpy (names
, "@plt", sizeof ("@plt"));
749 names
+= sizeof ("@plt");
753 offset
+= plt_entry_size
;
757 /* PLT entries with R_386_TLS_DESC relocations are skipped. */
766 for (j
= 0; plts
[j
].name
!= NULL
; j
++)
767 if (plts
[j
].contents
!= NULL
)
768 free (plts
[j
].contents
);
775 /* Parse x86 GNU properties. */
777 enum elf_property_kind
778 _bfd_x86_elf_parse_gnu_properties (bfd
*abfd
, unsigned int type
,
779 bfd_byte
*ptr
, unsigned int datasz
)
785 case GNU_PROPERTY_X86_ISA_1_USED
:
786 case GNU_PROPERTY_X86_ISA_1_NEEDED
:
787 case GNU_PROPERTY_X86_FEATURE_1_AND
:
791 ((type
== GNU_PROPERTY_X86_ISA_1_USED
792 ? _("error: %B: <corrupt x86 ISA used size: 0x%x>")
793 : (type
== GNU_PROPERTY_X86_ISA_1_NEEDED
794 ? _("error: %B: <corrupt x86 ISA needed size: 0x%x>")
795 : _("error: %B: <corrupt x86 feature size: 0x%x>"))),
797 return property_corrupt
;
799 prop
= _bfd_elf_get_property (abfd
, type
, datasz
);
800 /* Combine properties of the same type. */
801 prop
->u
.number
|= bfd_h_get_32 (abfd
, ptr
);
802 prop
->pr_kind
= property_number
;
806 return property_ignored
;
809 return property_number
;
812 /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
813 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
814 should be merged with ABFD. */
817 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info
*info
,
818 bfd
*abfd ATTRIBUTE_UNUSED
,
822 unsigned int number
, features
;
823 bfd_boolean updated
= FALSE
;
824 unsigned int pr_type
= aprop
!= NULL
? aprop
->pr_type
: bprop
->pr_type
;
828 case GNU_PROPERTY_X86_ISA_1_USED
:
829 case GNU_PROPERTY_X86_ISA_1_NEEDED
:
830 if (aprop
!= NULL
&& bprop
!= NULL
)
832 number
= aprop
->u
.number
;
833 aprop
->u
.number
= number
| bprop
->u
.number
;
834 updated
= number
!= (unsigned int) aprop
->u
.number
;
838 /* Return TRUE if APROP is NULL to indicate that BPROP should
840 updated
= aprop
== NULL
;
844 case GNU_PROPERTY_X86_FEATURE_1_AND
:
845 /* Only one of APROP and BPROP can be NULL:
846 1. APROP & BPROP when both APROP and BPROP aren't NULL.
847 2. If APROP is NULL, remove x86 feature.
848 3. Otherwise, do nothing.
850 if (aprop
!= NULL
&& bprop
!= NULL
)
854 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
856 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
857 number
= aprop
->u
.number
;
858 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
859 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
860 aprop
->u
.number
= (number
& bprop
->u
.number
) | features
;
861 updated
= number
!= (unsigned int) aprop
->u
.number
;
862 /* Remove the property if all feature bits are cleared. */
863 if (aprop
->u
.number
== 0)
864 aprop
->pr_kind
= property_remove
;
870 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
872 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
875 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
876 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
879 number
= aprop
->u
.number
;
880 aprop
->u
.number
= number
| features
;
881 updated
= number
!= (unsigned int) aprop
->u
.number
;
885 bprop
->u
.number
|= features
;
889 else if (aprop
!= NULL
)
891 aprop
->pr_kind
= property_remove
;
898 /* Never should happen. */
905 /* Set up x86 GNU properties. Return the first relocatable ELF input
906 with GNU properties if found. Otherwise, return NULL. */
909 _bfd_x86_elf_link_setup_gnu_properties
910 (struct bfd_link_info
*info
,
911 struct elf_x86_plt_layout_table
*plt_layout
)
913 bfd_boolean normal_target
;
914 bfd_boolean lazy_plt
;
915 asection
*sec
, *pltsec
;
917 bfd_boolean use_ibt_plt
;
918 unsigned int plt_alignment
, features
;
919 struct elf_x86_link_hash_table
*htab
;
923 const struct elf_backend_data
*bed
;
924 unsigned int class_align
= ABI_64_P (info
->output_bfd
) ? 3 : 2;
925 unsigned int got_align
;
929 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
931 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
933 /* Find a normal input file with GNU property note. */
934 for (pbfd
= info
->input_bfds
;
936 pbfd
= pbfd
->link
.next
)
937 if (bfd_get_flavour (pbfd
) == bfd_target_elf_flavour
938 && bfd_count_sections (pbfd
) != 0)
942 if (elf_properties (pbfd
) != NULL
)
946 if (ebfd
!= NULL
&& features
)
948 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and
949 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
950 prop
= _bfd_elf_get_property (ebfd
,
951 GNU_PROPERTY_X86_FEATURE_1_AND
,
953 prop
->u
.number
|= features
;
954 prop
->pr_kind
= property_number
;
956 /* Create the GNU property note section if needed. */
959 sec
= bfd_make_section_with_flags (ebfd
,
960 NOTE_GNU_PROPERTY_SECTION_NAME
,
968 info
->callbacks
->einfo (_("%F: failed to create GNU property section\n"));
970 if (!bfd_set_section_alignment (ebfd
, sec
, class_align
))
973 info
->callbacks
->einfo (_("%F%A: failed to align section\n"),
977 elf_section_type (sec
) = SHT_NOTE
;
981 pbfd
= _bfd_elf_link_setup_gnu_properties (info
);
983 if (bfd_link_relocatable (info
))
986 bed
= get_elf_backend_data (info
->output_bfd
);
988 htab
= elf_x86_hash_table (info
, bed
->target_id
);
992 use_ibt_plt
= info
->ibtplt
|| info
->ibt
;
993 if (!use_ibt_plt
&& pbfd
!= NULL
)
995 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
996 elf_property_list
*p
;
998 /* The property list is sorted in order of type. */
999 for (p
= elf_properties (pbfd
); p
; p
= p
->next
)
1001 if (GNU_PROPERTY_X86_FEATURE_1_AND
== p
->property
.pr_type
)
1003 use_ibt_plt
= !!(p
->property
.u
.number
1004 & GNU_PROPERTY_X86_FEATURE_1_IBT
);
1007 else if (GNU_PROPERTY_X86_FEATURE_1_AND
< p
->property
.pr_type
)
1012 dynobj
= htab
->elf
.dynobj
;
1014 /* Set htab->elf.dynobj here so that there is no need to check and
1015 set it in check_relocs. */
1020 htab
->elf
.dynobj
= pbfd
;
1027 /* Find a normal input file to hold linker created
1029 for (abfd
= info
->input_bfds
;
1031 abfd
= abfd
->link
.next
)
1032 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1034 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0)
1036 htab
->elf
.dynobj
= abfd
;
1043 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
1044 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
1045 canonical function address. */
1046 htab
->plt
.has_plt0
= 1;
1047 normal_target
= plt_layout
->normal_target
;
1053 htab
->lazy_plt
= plt_layout
->lazy_ibt_plt
;
1054 htab
->non_lazy_plt
= plt_layout
->non_lazy_ibt_plt
;
1058 htab
->lazy_plt
= plt_layout
->lazy_plt
;
1059 htab
->non_lazy_plt
= plt_layout
->non_lazy_plt
;
1064 htab
->lazy_plt
= plt_layout
->lazy_plt
;
1065 htab
->non_lazy_plt
= NULL
;
1068 pltsec
= htab
->elf
.splt
;
1070 /* If the non-lazy PLT is available, use it for all PLT entries if
1071 there are no PLT0 or no .plt section. */
1072 if (htab
->non_lazy_plt
!= NULL
1073 && (!htab
->plt
.has_plt0
|| pltsec
== NULL
))
1076 if (bfd_link_pic (info
))
1077 htab
->plt
.plt_entry
= htab
->non_lazy_plt
->pic_plt_entry
;
1079 htab
->plt
.plt_entry
= htab
->non_lazy_plt
->plt_entry
;
1080 htab
->plt
.plt_entry_size
= htab
->non_lazy_plt
->plt_entry_size
;
1081 htab
->plt
.plt_got_offset
= htab
->non_lazy_plt
->plt_got_offset
;
1082 htab
->plt
.plt_got_insn_size
1083 = htab
->non_lazy_plt
->plt_got_insn_size
;
1084 htab
->plt
.eh_frame_plt_size
1085 = htab
->non_lazy_plt
->eh_frame_plt_size
;
1086 htab
->plt
.eh_frame_plt
= htab
->non_lazy_plt
->eh_frame_plt
;
1091 if (bfd_link_pic (info
))
1093 htab
->plt
.plt0_entry
= htab
->lazy_plt
->pic_plt0_entry
;
1094 htab
->plt
.plt_entry
= htab
->lazy_plt
->pic_plt_entry
;
1098 htab
->plt
.plt0_entry
= htab
->lazy_plt
->plt0_entry
;
1099 htab
->plt
.plt_entry
= htab
->lazy_plt
->plt_entry
;
1101 htab
->plt
.plt_entry_size
= htab
->lazy_plt
->plt_entry_size
;
1102 htab
->plt
.plt_got_offset
= htab
->lazy_plt
->plt_got_offset
;
1103 htab
->plt
.plt_got_insn_size
1104 = htab
->lazy_plt
->plt_got_insn_size
;
1105 htab
->plt
.eh_frame_plt_size
1106 = htab
->lazy_plt
->eh_frame_plt_size
;
1107 htab
->plt
.eh_frame_plt
= htab
->lazy_plt
->eh_frame_plt
;
1110 /* Return if there are no normal input files. */
1114 if (plt_layout
->is_vxworks
1115 && !elf_vxworks_create_dynamic_sections (dynobj
, info
,
1118 info
->callbacks
->einfo (_("%F: failed to create VxWorks dynamic sections\n"));
1122 /* Since create_dynamic_sections isn't always called, but GOT
1123 relocations need GOT relocations, create them here so that we
1124 don't need to do it in check_relocs. */
1125 if (htab
->elf
.sgot
== NULL
1126 && !_bfd_elf_create_got_section (dynobj
, info
))
1127 info
->callbacks
->einfo (_("%F: failed to create GOT sections\n"));
1129 got_align
= (bed
->target_id
== X86_64_ELF_DATA
) ? 3 : 2;
1131 /* Align .got and .got.plt sections to their entry size. Do it here
1132 instead of in create_dynamic_sections so that they are always
1133 properly aligned even if create_dynamic_sections isn't called. */
1134 sec
= htab
->elf
.sgot
;
1135 if (!bfd_set_section_alignment (dynobj
, sec
, got_align
))
1136 goto error_alignment
;
1138 sec
= htab
->elf
.sgotplt
;
1139 if (!bfd_set_section_alignment (dynobj
, sec
, got_align
))
1140 goto error_alignment
;
1142 /* Create the ifunc sections here so that check_relocs can be
1144 if (!_bfd_elf_create_ifunc_sections (dynobj
, info
))
1145 info
->callbacks
->einfo (_("%F: failed to create ifunc sections\n"));
1147 plt_alignment
= bfd_log2 (htab
->plt
.plt_entry_size
);
1151 /* Whe creating executable, set the contents of the .interp
1152 section to the interpreter. */
1153 if (bfd_link_executable (info
) && !info
->nointerp
)
1155 asection
*s
= bfd_get_linker_section (dynobj
, ".interp");
1158 s
->size
= htab
->dynamic_interpreter_size
;
1159 s
->contents
= (unsigned char *) htab
->dynamic_interpreter
;
1163 /* Don't change PLT section alignment for NaCl since it uses
1164 64-byte PLT entry and sets PLT section alignment to 32
1165 bytes. Don't create additional PLT sections for NaCl. */
1168 flagword pltflags
= (bed
->dynamic_sec_flags
1173 unsigned int non_lazy_plt_alignment
1174 = bfd_log2 (htab
->non_lazy_plt
->plt_entry_size
);
1177 if (!bfd_set_section_alignment (sec
->owner
, sec
,
1179 goto error_alignment
;
1181 /* Create the GOT procedure linkage table. */
1182 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1186 info
->callbacks
->einfo (_("%F: failed to create GOT PLT section\n"));
1188 if (!bfd_set_section_alignment (dynobj
, sec
,
1189 non_lazy_plt_alignment
))
1190 goto error_alignment
;
1192 htab
->plt_got
= sec
;
1200 /* Create the second PLT for Intel IBT support. IBT
1201 PLT is supported only for non-NaCl target and is
1202 is needed only for lazy binding. */
1203 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1207 info
->callbacks
->einfo (_("%F: failed to create IBT-enabled PLT section\n"));
1209 if (!bfd_set_section_alignment (dynobj
, sec
,
1211 goto error_alignment
;
1213 else if (info
->bndplt
&& ABI_64_P (dynobj
))
1215 /* Create the second PLT for Intel MPX support. MPX
1216 PLT is supported only for non-NaCl target in 64-bit
1217 mode and is needed only for lazy binding. */
1218 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1222 info
->callbacks
->einfo (_("%F: failed to create BND PLT section\n"));
1224 if (!bfd_set_section_alignment (dynobj
, sec
,
1225 non_lazy_plt_alignment
))
1226 goto error_alignment
;
1229 htab
->plt_second
= sec
;
1233 if (!info
->no_ld_generated_unwind_info
)
1235 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1236 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
1237 | SEC_LINKER_CREATED
);
1239 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1243 info
->callbacks
->einfo (_("%F: failed to create PLT .eh_frame section\n"));
1245 if (!bfd_set_section_alignment (dynobj
, sec
, class_align
))
1246 goto error_alignment
;
1248 htab
->plt_eh_frame
= sec
;
1250 if (htab
->plt_got
!= NULL
)
1252 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1256 info
->callbacks
->einfo (_("%F: failed to create GOT PLT .eh_frame section\n"));
1258 if (!bfd_set_section_alignment (dynobj
, sec
, class_align
))
1259 goto error_alignment
;
1261 htab
->plt_got_eh_frame
= sec
;
1264 if (htab
->plt_second
!= NULL
)
1266 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1270 info
->callbacks
->einfo (_("%F: failed to create the second PLT .eh_frame section\n"));
1272 if (!bfd_set_section_alignment (dynobj
, sec
, class_align
))
1273 goto error_alignment
;
1275 htab
->plt_second_eh_frame
= sec
;
1282 /* The .iplt section is used for IFUNC symbols in static
1284 sec
= htab
->elf
.iplt
;
1286 && !bfd_set_section_alignment (sec
->owner
, sec
,
1288 goto error_alignment
;