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
->sizeof_reloc
= sizeof (Elf64_External_Rela
);
281 ret
->pointer_r_type
= R_X86_64_64
;
282 ret
->dynamic_interpreter
= ELF64_DYNAMIC_INTERPRETER
;
283 ret
->dynamic_interpreter_size
= sizeof ELF64_DYNAMIC_INTERPRETER
;
284 ret
->tls_get_addr
= "__tls_get_addr";
289 ret
->r_info
= elf32_r_info
;
290 ret
->r_sym
= elf32_r_sym
;
291 if (bed
->target_id
== X86_64_ELF_DATA
)
293 ret
->sizeof_reloc
= sizeof (Elf32_External_Rela
);
294 ret
->pointer_r_type
= R_X86_64_32
;
295 ret
->dynamic_interpreter
= ELFX32_DYNAMIC_INTERPRETER
;
296 ret
->dynamic_interpreter_size
297 = sizeof ELFX32_DYNAMIC_INTERPRETER
;
298 ret
->tls_get_addr
= "__tls_get_addr";
302 ret
->sizeof_reloc
= sizeof (Elf32_External_Rel
);
303 ret
->pointer_r_type
= R_386_32
;
304 ret
->dynamic_interpreter
= ELF32_DYNAMIC_INTERPRETER
;
305 ret
->dynamic_interpreter_size
306 = sizeof ELF32_DYNAMIC_INTERPRETER
;
307 ret
->tls_get_addr
= "___tls_get_addr";
311 ret
->loc_hash_table
= htab_try_create (1024,
312 _bfd_x86_elf_local_htab_hash
,
313 _bfd_x86_elf_local_htab_eq
,
315 ret
->loc_hash_memory
= objalloc_create ();
316 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
318 elf_x86_link_hash_table_free (abfd
);
321 ret
->elf
.root
.hash_table_free
= elf_x86_link_hash_table_free
;
323 return &ret
->elf
.root
;
326 /* Sort relocs into address order. */
329 _bfd_x86_elf_compare_relocs (const void *ap
, const void *bp
)
331 const arelent
*a
= * (const arelent
**) ap
;
332 const arelent
*b
= * (const arelent
**) bp
;
334 if (a
->address
> b
->address
)
336 else if (a
->address
< b
->address
)
343 _bfd_x86_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
345 if (!bfd_link_relocatable (info
))
347 /* Check for __tls_get_addr reference. */
348 struct elf_x86_link_hash_table
*htab
;
349 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
350 htab
= elf_x86_hash_table (info
, bed
->target_id
);
353 struct elf_link_hash_entry
*h
354 = elf_link_hash_lookup (elf_hash_table (info
),
356 FALSE
, FALSE
, FALSE
);
358 ((struct elf_x86_link_hash_entry
*) h
)->tls_get_addr
= 1;
362 /* Invoke the regular ELF backend linker to do all the work. */
363 return _bfd_elf_link_check_relocs (abfd
, info
);
367 _bfd_x86_elf_always_size_sections (bfd
*output_bfd
,
368 struct bfd_link_info
*info
)
370 asection
*tls_sec
= elf_hash_table (info
)->tls_sec
;
374 struct elf_link_hash_entry
*tlsbase
;
376 tlsbase
= elf_link_hash_lookup (elf_hash_table (info
),
378 FALSE
, FALSE
, FALSE
);
380 if (tlsbase
&& tlsbase
->type
== STT_TLS
)
382 struct elf_x86_link_hash_table
*htab
;
383 struct bfd_link_hash_entry
*bh
= NULL
;
384 const struct elf_backend_data
*bed
385 = get_elf_backend_data (output_bfd
);
387 htab
= elf_x86_hash_table (info
, bed
->target_id
);
391 if (!(_bfd_generic_link_add_one_symbol
392 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
393 tls_sec
, 0, NULL
, FALSE
,
397 htab
->tls_module_base
= bh
;
399 tlsbase
= (struct elf_link_hash_entry
*)bh
;
400 tlsbase
->def_regular
= 1;
401 tlsbase
->other
= STV_HIDDEN
;
402 tlsbase
->root
.linker_def
= 1;
403 (*bed
->elf_backend_hide_symbol
) (info
, tlsbase
, TRUE
);
411 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry
*h
,
412 const Elf_Internal_Sym
*isym
,
413 bfd_boolean definition
,
414 bfd_boolean dynamic ATTRIBUTE_UNUSED
)
418 struct elf_x86_link_hash_entry
*eh
419 = (struct elf_x86_link_hash_entry
*) h
;
420 eh
->def_protected
= (ELF_ST_VISIBILITY (isym
->st_other
)
425 /* Copy the extra info we tack onto an elf_link_hash_entry. */
428 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
429 struct elf_link_hash_entry
*dir
,
430 struct elf_link_hash_entry
*ind
)
432 struct elf_x86_link_hash_entry
*edir
, *eind
;
434 edir
= (struct elf_x86_link_hash_entry
*) dir
;
435 eind
= (struct elf_x86_link_hash_entry
*) ind
;
437 if (eind
->dyn_relocs
!= NULL
)
439 if (edir
->dyn_relocs
!= NULL
)
441 struct elf_dyn_relocs
**pp
;
442 struct elf_dyn_relocs
*p
;
444 /* Add reloc counts against the indirect sym to the direct sym
445 list. Merge any entries against the same section. */
446 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
448 struct elf_dyn_relocs
*q
;
450 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
451 if (q
->sec
== p
->sec
)
453 q
->pc_count
+= p
->pc_count
;
454 q
->count
+= p
->count
;
461 *pp
= edir
->dyn_relocs
;
464 edir
->dyn_relocs
= eind
->dyn_relocs
;
465 eind
->dyn_relocs
= NULL
;
468 if (ind
->root
.type
== bfd_link_hash_indirect
469 && dir
->got
.refcount
<= 0)
471 edir
->tls_type
= eind
->tls_type
;
472 eind
->tls_type
= GOT_UNKNOWN
;
475 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
476 generate a R_386_COPY reloc. */
477 edir
->gotoff_ref
|= eind
->gotoff_ref
;
479 edir
->has_got_reloc
|= eind
->has_got_reloc
;
480 edir
->has_non_got_reloc
|= eind
->has_non_got_reloc
;
482 if (ELIMINATE_COPY_RELOCS
483 && ind
->root
.type
!= bfd_link_hash_indirect
484 && dir
->dynamic_adjusted
)
486 /* If called to transfer flags for a weakdef during processing
487 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
488 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
489 if (dir
->versioned
!= versioned_hidden
)
490 dir
->ref_dynamic
|= ind
->ref_dynamic
;
491 dir
->ref_regular
|= ind
->ref_regular
;
492 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
493 dir
->needs_plt
|= ind
->needs_plt
;
494 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
498 if (eind
->func_pointer_refcount
> 0)
500 edir
->func_pointer_refcount
+= eind
->func_pointer_refcount
;
501 eind
->func_pointer_refcount
= 0;
504 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
508 /* Remove undefined weak symbol from the dynamic symbol table if it
512 _bfd_x86_elf_fixup_symbol (struct bfd_link_info
*info
,
513 struct elf_link_hash_entry
*h
)
515 if (h
->dynindx
!= -1)
517 const struct elf_backend_data
*bed
518 = get_elf_backend_data (info
->output_bfd
);
519 if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
,
521 elf_x86_hash_entry (h
)->has_got_reloc
,
522 elf_x86_hash_entry (h
)))
525 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
532 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
535 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry
*h
)
537 if (h
->plt
.offset
!= (bfd_vma
) -1
539 && !h
->pointer_equality_needed
)
542 return _bfd_elf_hash_symbol (h
);
545 /* Adjust a symbol defined by a dynamic object and referenced by a
546 regular object. The current definition is in some section of the
547 dynamic object, but we're not including those sections. We have to
548 change the definition to something the rest of the link can
552 _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
553 struct elf_link_hash_entry
*h
)
555 struct elf_x86_link_hash_table
*htab
;
557 struct elf_x86_link_hash_entry
*eh
;
558 struct elf_dyn_relocs
*p
;
559 const struct elf_backend_data
*bed
560 = get_elf_backend_data (info
->output_bfd
);
562 /* STT_GNU_IFUNC symbol must go through PLT. */
563 if (h
->type
== STT_GNU_IFUNC
)
565 /* All local STT_GNU_IFUNC references must be treate as local
566 calls via local PLT. */
568 && SYMBOL_CALLS_LOCAL (info
, h
))
570 bfd_size_type pc_count
= 0, count
= 0;
571 struct elf_dyn_relocs
**pp
;
573 eh
= (struct elf_x86_link_hash_entry
*) h
;
574 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
576 pc_count
+= p
->pc_count
;
577 p
->count
-= p
->pc_count
;
586 if (pc_count
|| count
)
591 /* Increment PLT reference count only for PC-relative
594 if (h
->plt
.refcount
<= 0)
597 h
->plt
.refcount
+= 1;
602 if (h
->plt
.refcount
<= 0)
604 h
->plt
.offset
= (bfd_vma
) -1;
610 /* If this is a function, put it in the procedure linkage table. We
611 will fill in the contents of the procedure linkage table later,
612 when we know the address of the .got section. */
613 if (h
->type
== STT_FUNC
616 if (h
->plt
.refcount
<= 0
617 || SYMBOL_CALLS_LOCAL (info
, h
)
618 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
619 && h
->root
.type
== bfd_link_hash_undefweak
))
621 /* This case can occur if we saw a PLT32 reloc in an input
622 file, but the symbol was never referred to by a dynamic
623 object, or if all references were garbage collected. In
624 such a case, we don't actually need to build a procedure
625 linkage table, and we can just do a PC32 reloc instead. */
626 h
->plt
.offset
= (bfd_vma
) -1;
633 /* It's possible that we incorrectly decided a .plt reloc was needed
634 * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
635 check_relocs. We can't decide accurately between function and
636 non-function syms in check-relocs; Objects loaded later in
637 the link may change h->type. So fix it now. */
638 h
->plt
.offset
= (bfd_vma
) -1;
640 eh
= (struct elf_x86_link_hash_entry
*) h
;
642 /* If this is a weak symbol, and there is a real definition, the
643 processor independent code will have arranged for us to see the
644 real definition first, and we can just use the same value. */
645 if (h
->u
.weakdef
!= NULL
)
647 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
648 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
649 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
650 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
651 if (ELIMINATE_COPY_RELOCS
653 || SYMBOL_NO_COPYRELOC (info
, eh
))
655 /* NB: needs_copy is always 0 for i386. */
656 h
->non_got_ref
= h
->u
.weakdef
->non_got_ref
;
657 eh
->needs_copy
= h
->u
.weakdef
->needs_copy
;
662 /* This is a reference to a symbol defined by a dynamic object which
663 is not a function. */
665 /* If we are creating a shared library, we must presume that the
666 only references to the symbol are via the global offset table.
667 For such cases we need not do anything here; the relocations will
668 be handled correctly by relocate_section. */
669 if (!bfd_link_executable (info
))
672 /* If there are no references to this symbol that do not use the
673 GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
674 reloc. NB: gotoff_ref is always 0 for x86-64. */
675 if (!h
->non_got_ref
&& !eh
->gotoff_ref
)
678 /* If -z nocopyreloc was given, we won't generate them either. */
679 if (info
->nocopyreloc
|| SYMBOL_NO_COPYRELOC (info
, eh
))
685 htab
= elf_x86_hash_table (info
, bed
->target_id
);
689 /* If there aren't any dynamic relocs in read-only sections nor
690 R_386_GOTOFF relocation, then we can keep the dynamic relocs and
691 avoid the copy reloc. This doesn't work on VxWorks, where we can
692 not have dynamic relocations (other than copy and jump slot
693 relocations) in an executable. */
694 if (ELIMINATE_COPY_RELOCS
695 && (bed
->target_id
== X86_64_ELF_DATA
697 && !htab
->is_vxworks
)))
699 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
701 s
= p
->sec
->output_section
;
702 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
706 /* If we didn't find any dynamic relocs in read-only sections,
707 then we'll be keeping the dynamic relocs and avoiding the copy
716 /* We must allocate the symbol in our .dynbss section, which will
717 become part of the .bss section of the executable. There will be
718 an entry for this symbol in the .dynsym section. The dynamic
719 object will contain position independent code, so all references
720 from the dynamic object to this symbol will go through the global
721 offset table. The dynamic linker will use the .dynsym entry to
722 determine the address it must put in the global offset table, so
723 both the dynamic object and the regular object will refer to the
724 same memory location for the variable. */
726 /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
727 dynamic linker to copy the initial value out of the dynamic object
728 and into the runtime process image. */
729 if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
731 s
= htab
->elf
.sdynrelro
;
732 srel
= htab
->elf
.sreldynrelro
;
736 s
= htab
->elf
.sdynbss
;
737 srel
= htab
->elf
.srelbss
;
739 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
741 srel
->size
+= htab
->sizeof_reloc
;
745 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
748 /* Return the section that should be marked against GC for a given
752 _bfd_x86_elf_gc_mark_hook (asection
*sec
,
753 struct bfd_link_info
*info
,
754 Elf_Internal_Rela
*rel
,
755 struct elf_link_hash_entry
*h
,
756 Elf_Internal_Sym
*sym
)
758 /* Compiler should optimize this out. */
759 if (((unsigned int) R_X86_64_GNU_VTINHERIT
760 != (unsigned int) R_386_GNU_VTINHERIT
)
761 || ((unsigned int) R_X86_64_GNU_VTENTRY
762 != (unsigned int) R_386_GNU_VTENTRY
))
766 switch (ELF32_R_TYPE (rel
->r_info
))
768 case R_X86_64_GNU_VTINHERIT
:
769 case R_X86_64_GNU_VTENTRY
:
773 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
777 elf_i386_get_plt_got_vma (struct elf_x86_plt
*plt_p ATTRIBUTE_UNUSED
,
779 bfd_vma offset ATTRIBUTE_UNUSED
,
782 return got_addr
+ off
;
786 elf_x86_64_get_plt_got_vma (struct elf_x86_plt
*plt_p
,
789 bfd_vma got_addr ATTRIBUTE_UNUSED
)
791 return plt_p
->sec
->vma
+ offset
+ off
+ plt_p
->plt_got_insn_size
;
795 elf_i386_valid_plt_reloc_p (unsigned int type
)
797 return (type
== R_386_JUMP_SLOT
798 || type
== R_386_GLOB_DAT
799 || type
== R_386_IRELATIVE
);
803 elf_x86_64_valid_plt_reloc_p (unsigned int type
)
805 return (type
== R_X86_64_JUMP_SLOT
806 || type
== R_X86_64_GLOB_DAT
807 || type
== R_X86_64_IRELATIVE
);
811 _bfd_x86_elf_get_synthetic_symtab (bfd
*abfd
,
815 struct elf_x86_plt plts
[],
819 long size
, i
, n
, len
;
821 unsigned int plt_got_offset
, plt_entry_size
;
823 bfd_byte
*plt_contents
;
825 arelent
**dynrelbuf
, *p
;
827 const struct elf_backend_data
*bed
;
828 bfd_vma (*get_plt_got_vma
) (struct elf_x86_plt
*, bfd_vma
, bfd_vma
,
830 bfd_boolean (*valid_plt_reloc_p
) (unsigned int);
835 dynrelbuf
= (arelent
**) bfd_malloc (relsize
);
836 if (dynrelbuf
== NULL
)
839 dynrelcount
= bfd_canonicalize_dynamic_reloc (abfd
, dynrelbuf
,
842 /* Sort the relocs by address. */
843 qsort (dynrelbuf
, dynrelcount
, sizeof (arelent
*),
844 _bfd_x86_elf_compare_relocs
);
846 size
= count
* sizeof (asymbol
);
848 /* Allocate space for @plt suffixes. */
850 for (i
= 0; i
< dynrelcount
; i
++)
853 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
855 size
+= sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd
);
858 s
= *ret
= (asymbol
*) bfd_zmalloc (size
);
862 bed
= get_elf_backend_data (abfd
);
864 if (bed
->target_id
== X86_64_ELF_DATA
)
866 get_plt_got_vma
= elf_x86_64_get_plt_got_vma
;
867 valid_plt_reloc_p
= elf_x86_64_valid_plt_reloc_p
;
871 get_plt_got_vma
= elf_i386_get_plt_got_vma
;
872 valid_plt_reloc_p
= elf_i386_valid_plt_reloc_p
;
875 /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
877 asection
*sec
= bfd_get_section_by_name (abfd
, ".got.plt");
882 sec
= bfd_get_section_by_name (abfd
, ".got");
887 if (got_addr
== (bfd_vma
) -1)
892 /* Check for each PLT section. */
893 names
= (char *) (s
+ count
);
896 for (j
= 0; plts
[j
].name
!= NULL
; j
++)
897 if ((plt_contents
= plts
[j
].contents
) != NULL
)
902 struct elf_x86_plt
*plt_p
= &plts
[j
];
904 plt_got_offset
= plt_p
->plt_got_offset
;
905 plt_entry_size
= plt_p
->plt_entry_size
;
909 if ((plt_p
->type
& plt_lazy
))
911 /* Skip PLT0 in lazy PLT. */
913 offset
= plt_entry_size
;
921 /* Check each PLT entry against dynamic relocations. */
922 for (; k
< plt_p
->count
; k
++)
928 /* Get the GOT offset for i386 or the PC-relative offset
929 for x86-64, a signed 32-bit integer. */
930 off
= H_GET_32 (abfd
, (plt_contents
+ offset
932 got_vma
= get_plt_got_vma (plt_p
, off
, offset
, got_addr
);
938 while ((min
+ 1) < max
)
942 mid
= (min
+ max
) / 2;
944 if (got_vma
> r
->address
)
946 else if (got_vma
< r
->address
)
955 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
956 if (got_vma
== p
->address
958 && valid_plt_reloc_p (p
->howto
->type
))
960 *s
= **p
->sym_ptr_ptr
;
961 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
962 set. Since we are defining a symbol, ensure one
964 if ((s
->flags
& BSF_LOCAL
) == 0)
965 s
->flags
|= BSF_GLOBAL
;
966 s
->flags
|= BSF_SYNTHETIC
;
967 /* This is no longer a section symbol. */
968 s
->flags
&= ~BSF_SECTION_SYM
;
970 s
->the_bfd
= plt
->owner
;
974 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
975 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
981 memcpy (names
, "+0x", sizeof ("+0x") - 1);
982 names
+= sizeof ("+0x") - 1;
983 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
984 for (a
= buf
; *a
== '0'; ++a
)
987 memcpy (names
, a
, size
);
990 memcpy (names
, "@plt", sizeof ("@plt"));
991 names
+= sizeof ("@plt");
995 offset
+= plt_entry_size
;
999 /* PLT entries with R_386_TLS_DESC relocations are skipped. */
1008 for (j
= 0; plts
[j
].name
!= NULL
; j
++)
1009 if (plts
[j
].contents
!= NULL
)
1010 free (plts
[j
].contents
);
1017 /* Parse x86 GNU properties. */
1019 enum elf_property_kind
1020 _bfd_x86_elf_parse_gnu_properties (bfd
*abfd
, unsigned int type
,
1021 bfd_byte
*ptr
, unsigned int datasz
)
1027 case GNU_PROPERTY_X86_ISA_1_USED
:
1028 case GNU_PROPERTY_X86_ISA_1_NEEDED
:
1029 case GNU_PROPERTY_X86_FEATURE_1_AND
:
1033 ((type
== GNU_PROPERTY_X86_ISA_1_USED
1034 ? _("error: %B: <corrupt x86 ISA used size: 0x%x>")
1035 : (type
== GNU_PROPERTY_X86_ISA_1_NEEDED
1036 ? _("error: %B: <corrupt x86 ISA needed size: 0x%x>")
1037 : _("error: %B: <corrupt x86 feature size: 0x%x>"))),
1039 return property_corrupt
;
1041 prop
= _bfd_elf_get_property (abfd
, type
, datasz
);
1042 /* Combine properties of the same type. */
1043 prop
->u
.number
|= bfd_h_get_32 (abfd
, ptr
);
1044 prop
->pr_kind
= property_number
;
1048 return property_ignored
;
1051 return property_number
;
1054 /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
1055 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
1056 should be merged with ABFD. */
1059 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info
*info
,
1060 bfd
*abfd ATTRIBUTE_UNUSED
,
1061 elf_property
*aprop
,
1062 elf_property
*bprop
)
1064 unsigned int number
, features
;
1065 bfd_boolean updated
= FALSE
;
1066 unsigned int pr_type
= aprop
!= NULL
? aprop
->pr_type
: bprop
->pr_type
;
1070 case GNU_PROPERTY_X86_ISA_1_USED
:
1071 case GNU_PROPERTY_X86_ISA_1_NEEDED
:
1072 if (aprop
!= NULL
&& bprop
!= NULL
)
1074 number
= aprop
->u
.number
;
1075 aprop
->u
.number
= number
| bprop
->u
.number
;
1076 updated
= number
!= (unsigned int) aprop
->u
.number
;
1080 /* Return TRUE if APROP is NULL to indicate that BPROP should
1081 be added to ABFD. */
1082 updated
= aprop
== NULL
;
1086 case GNU_PROPERTY_X86_FEATURE_1_AND
:
1087 /* Only one of APROP and BPROP can be NULL:
1088 1. APROP & BPROP when both APROP and BPROP aren't NULL.
1089 2. If APROP is NULL, remove x86 feature.
1090 3. Otherwise, do nothing.
1092 if (aprop
!= NULL
&& bprop
!= NULL
)
1096 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
1098 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
1099 number
= aprop
->u
.number
;
1100 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
1101 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
1102 aprop
->u
.number
= (number
& bprop
->u
.number
) | features
;
1103 updated
= number
!= (unsigned int) aprop
->u
.number
;
1104 /* Remove the property if all feature bits are cleared. */
1105 if (aprop
->u
.number
== 0)
1106 aprop
->pr_kind
= property_remove
;
1112 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
1114 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
1117 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
1118 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
1121 number
= aprop
->u
.number
;
1122 aprop
->u
.number
= number
| features
;
1123 updated
= number
!= (unsigned int) aprop
->u
.number
;
1127 bprop
->u
.number
|= features
;
1131 else if (aprop
!= NULL
)
1133 aprop
->pr_kind
= property_remove
;
1140 /* Never should happen. */
1147 /* Set up x86 GNU properties. Return the first relocatable ELF input
1148 with GNU properties if found. Otherwise, return NULL. */
1151 _bfd_x86_elf_link_setup_gnu_properties
1152 (struct bfd_link_info
*info
,
1153 struct elf_x86_plt_layout_table
*plt_layout
)
1155 bfd_boolean normal_target
;
1156 bfd_boolean lazy_plt
;
1157 asection
*sec
, *pltsec
;
1159 bfd_boolean use_ibt_plt
;
1160 unsigned int plt_alignment
, features
;
1161 struct elf_x86_link_hash_table
*htab
;
1165 const struct elf_backend_data
*bed
;
1166 unsigned int class_align
= ABI_64_P (info
->output_bfd
) ? 3 : 2;
1167 unsigned int got_align
;
1171 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
1173 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
1175 /* Find a normal input file with GNU property note. */
1176 for (pbfd
= info
->input_bfds
;
1178 pbfd
= pbfd
->link
.next
)
1179 if (bfd_get_flavour (pbfd
) == bfd_target_elf_flavour
1180 && bfd_count_sections (pbfd
) != 0)
1184 if (elf_properties (pbfd
) != NULL
)
1188 if (ebfd
!= NULL
&& features
)
1190 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and
1191 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
1192 prop
= _bfd_elf_get_property (ebfd
,
1193 GNU_PROPERTY_X86_FEATURE_1_AND
,
1195 prop
->u
.number
|= features
;
1196 prop
->pr_kind
= property_number
;
1198 /* Create the GNU property note section if needed. */
1201 sec
= bfd_make_section_with_flags (ebfd
,
1202 NOTE_GNU_PROPERTY_SECTION_NAME
,
1210 info
->callbacks
->einfo (_("%F: failed to create GNU property section\n"));
1212 if (!bfd_set_section_alignment (ebfd
, sec
, class_align
))
1215 info
->callbacks
->einfo (_("%F%A: failed to align section\n"),
1219 elf_section_type (sec
) = SHT_NOTE
;
1223 pbfd
= _bfd_elf_link_setup_gnu_properties (info
);
1225 if (bfd_link_relocatable (info
))
1228 bed
= get_elf_backend_data (info
->output_bfd
);
1230 htab
= elf_x86_hash_table (info
, bed
->target_id
);
1234 htab
->is_vxworks
= plt_layout
->is_vxworks
;
1236 use_ibt_plt
= info
->ibtplt
|| info
->ibt
;
1237 if (!use_ibt_plt
&& pbfd
!= NULL
)
1239 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
1240 elf_property_list
*p
;
1242 /* The property list is sorted in order of type. */
1243 for (p
= elf_properties (pbfd
); p
; p
= p
->next
)
1245 if (GNU_PROPERTY_X86_FEATURE_1_AND
== p
->property
.pr_type
)
1247 use_ibt_plt
= !!(p
->property
.u
.number
1248 & GNU_PROPERTY_X86_FEATURE_1_IBT
);
1251 else if (GNU_PROPERTY_X86_FEATURE_1_AND
< p
->property
.pr_type
)
1256 dynobj
= htab
->elf
.dynobj
;
1258 /* Set htab->elf.dynobj here so that there is no need to check and
1259 set it in check_relocs. */
1264 htab
->elf
.dynobj
= pbfd
;
1271 /* Find a normal input file to hold linker created
1273 for (abfd
= info
->input_bfds
;
1275 abfd
= abfd
->link
.next
)
1276 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1278 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0)
1280 htab
->elf
.dynobj
= abfd
;
1287 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
1288 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
1289 canonical function address. */
1290 htab
->plt
.has_plt0
= 1;
1291 normal_target
= plt_layout
->normal_target
;
1297 htab
->lazy_plt
= plt_layout
->lazy_ibt_plt
;
1298 htab
->non_lazy_plt
= plt_layout
->non_lazy_ibt_plt
;
1302 htab
->lazy_plt
= plt_layout
->lazy_plt
;
1303 htab
->non_lazy_plt
= plt_layout
->non_lazy_plt
;
1308 htab
->lazy_plt
= plt_layout
->lazy_plt
;
1309 htab
->non_lazy_plt
= NULL
;
1312 pltsec
= htab
->elf
.splt
;
1314 /* If the non-lazy PLT is available, use it for all PLT entries if
1315 there are no PLT0 or no .plt section. */
1316 if (htab
->non_lazy_plt
!= NULL
1317 && (!htab
->plt
.has_plt0
|| pltsec
== NULL
))
1320 if (bfd_link_pic (info
))
1321 htab
->plt
.plt_entry
= htab
->non_lazy_plt
->pic_plt_entry
;
1323 htab
->plt
.plt_entry
= htab
->non_lazy_plt
->plt_entry
;
1324 htab
->plt
.plt_entry_size
= htab
->non_lazy_plt
->plt_entry_size
;
1325 htab
->plt
.plt_got_offset
= htab
->non_lazy_plt
->plt_got_offset
;
1326 htab
->plt
.plt_got_insn_size
1327 = htab
->non_lazy_plt
->plt_got_insn_size
;
1328 htab
->plt
.eh_frame_plt_size
1329 = htab
->non_lazy_plt
->eh_frame_plt_size
;
1330 htab
->plt
.eh_frame_plt
= htab
->non_lazy_plt
->eh_frame_plt
;
1335 if (bfd_link_pic (info
))
1337 htab
->plt
.plt0_entry
= htab
->lazy_plt
->pic_plt0_entry
;
1338 htab
->plt
.plt_entry
= htab
->lazy_plt
->pic_plt_entry
;
1342 htab
->plt
.plt0_entry
= htab
->lazy_plt
->plt0_entry
;
1343 htab
->plt
.plt_entry
= htab
->lazy_plt
->plt_entry
;
1345 htab
->plt
.plt_entry_size
= htab
->lazy_plt
->plt_entry_size
;
1346 htab
->plt
.plt_got_offset
= htab
->lazy_plt
->plt_got_offset
;
1347 htab
->plt
.plt_got_insn_size
1348 = htab
->lazy_plt
->plt_got_insn_size
;
1349 htab
->plt
.eh_frame_plt_size
1350 = htab
->lazy_plt
->eh_frame_plt_size
;
1351 htab
->plt
.eh_frame_plt
= htab
->lazy_plt
->eh_frame_plt
;
1354 /* Return if there are no normal input files. */
1358 if (htab
->is_vxworks
1359 && !elf_vxworks_create_dynamic_sections (dynobj
, info
,
1362 info
->callbacks
->einfo (_("%F: failed to create VxWorks dynamic sections\n"));
1366 /* Since create_dynamic_sections isn't always called, but GOT
1367 relocations need GOT relocations, create them here so that we
1368 don't need to do it in check_relocs. */
1369 if (htab
->elf
.sgot
== NULL
1370 && !_bfd_elf_create_got_section (dynobj
, info
))
1371 info
->callbacks
->einfo (_("%F: failed to create GOT sections\n"));
1373 got_align
= (bed
->target_id
== X86_64_ELF_DATA
) ? 3 : 2;
1375 /* Align .got and .got.plt sections to their entry size. Do it here
1376 instead of in create_dynamic_sections so that they are always
1377 properly aligned even if create_dynamic_sections isn't called. */
1378 sec
= htab
->elf
.sgot
;
1379 if (!bfd_set_section_alignment (dynobj
, sec
, got_align
))
1380 goto error_alignment
;
1382 sec
= htab
->elf
.sgotplt
;
1383 if (!bfd_set_section_alignment (dynobj
, sec
, got_align
))
1384 goto error_alignment
;
1386 /* Create the ifunc sections here so that check_relocs can be
1388 if (!_bfd_elf_create_ifunc_sections (dynobj
, info
))
1389 info
->callbacks
->einfo (_("%F: failed to create ifunc sections\n"));
1391 plt_alignment
= bfd_log2 (htab
->plt
.plt_entry_size
);
1395 /* Whe creating executable, set the contents of the .interp
1396 section to the interpreter. */
1397 if (bfd_link_executable (info
) && !info
->nointerp
)
1399 asection
*s
= bfd_get_linker_section (dynobj
, ".interp");
1402 s
->size
= htab
->dynamic_interpreter_size
;
1403 s
->contents
= (unsigned char *) htab
->dynamic_interpreter
;
1407 /* Don't change PLT section alignment for NaCl since it uses
1408 64-byte PLT entry and sets PLT section alignment to 32
1409 bytes. Don't create additional PLT sections for NaCl. */
1412 flagword pltflags
= (bed
->dynamic_sec_flags
1417 unsigned int non_lazy_plt_alignment
1418 = bfd_log2 (htab
->non_lazy_plt
->plt_entry_size
);
1421 if (!bfd_set_section_alignment (sec
->owner
, sec
,
1423 goto error_alignment
;
1425 /* Create the GOT procedure linkage table. */
1426 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1430 info
->callbacks
->einfo (_("%F: failed to create GOT PLT section\n"));
1432 if (!bfd_set_section_alignment (dynobj
, sec
,
1433 non_lazy_plt_alignment
))
1434 goto error_alignment
;
1436 htab
->plt_got
= sec
;
1444 /* Create the second PLT for Intel IBT support. IBT
1445 PLT is supported only for non-NaCl target and is
1446 is needed only for lazy binding. */
1447 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1451 info
->callbacks
->einfo (_("%F: failed to create IBT-enabled PLT section\n"));
1453 if (!bfd_set_section_alignment (dynobj
, sec
,
1455 goto error_alignment
;
1457 else if (info
->bndplt
&& ABI_64_P (dynobj
))
1459 /* Create the second PLT for Intel MPX support. MPX
1460 PLT is supported only for non-NaCl target in 64-bit
1461 mode and is needed only for lazy binding. */
1462 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1466 info
->callbacks
->einfo (_("%F: failed to create BND PLT section\n"));
1468 if (!bfd_set_section_alignment (dynobj
, sec
,
1469 non_lazy_plt_alignment
))
1470 goto error_alignment
;
1473 htab
->plt_second
= sec
;
1477 if (!info
->no_ld_generated_unwind_info
)
1479 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1480 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
1481 | SEC_LINKER_CREATED
);
1483 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1487 info
->callbacks
->einfo (_("%F: failed to create PLT .eh_frame section\n"));
1489 if (!bfd_set_section_alignment (dynobj
, sec
, class_align
))
1490 goto error_alignment
;
1492 htab
->plt_eh_frame
= sec
;
1494 if (htab
->plt_got
!= NULL
)
1496 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1500 info
->callbacks
->einfo (_("%F: failed to create GOT PLT .eh_frame section\n"));
1502 if (!bfd_set_section_alignment (dynobj
, sec
, class_align
))
1503 goto error_alignment
;
1505 htab
->plt_got_eh_frame
= sec
;
1508 if (htab
->plt_second
!= NULL
)
1510 sec
= bfd_make_section_anyway_with_flags (dynobj
,
1514 info
->callbacks
->einfo (_("%F: failed to create the second PLT .eh_frame section\n"));
1516 if (!bfd_set_section_alignment (dynobj
, sec
, class_align
))
1517 goto error_alignment
;
1519 htab
->plt_second_eh_frame
= sec
;
1526 /* The .iplt section is used for IFUNC symbols in static
1528 sec
= htab
->elf
.iplt
;
1530 && !bfd_set_section_alignment (sec
->owner
, sec
,
1532 goto error_alignment
;