1 /* x86 specific support for ELF
2 Copyright (C) 2017-2024 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 /* The name of the dynamic interpreter. This is put in the .interp
28 #define ELF32_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
29 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
30 #define ELFX32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
33 _bfd_x86_elf_mkobject (bfd
*abfd
)
35 return bfd_elf_allocate_object (abfd
,
36 sizeof (struct elf_x86_obj_tdata
),
37 get_elf_backend_data (abfd
)->target_id
);
40 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
41 executables. Rather than setting it to the beginning of the TLS
42 section, we have to set it to the end. This function may be called
43 multiple times, it is idempotent. */
46 _bfd_x86_elf_set_tls_module_base (struct bfd_link_info
*info
)
48 struct elf_x86_link_hash_table
*htab
;
49 struct bfd_link_hash_entry
*base
;
50 const struct elf_backend_data
*bed
;
52 if (!bfd_link_executable (info
))
55 bed
= get_elf_backend_data (info
->output_bfd
);
56 htab
= elf_x86_hash_table (info
, bed
->target_id
);
60 base
= htab
->tls_module_base
;
64 base
->u
.def
.value
= htab
->elf
.tls_size
;
67 /* Return the base VMA address which should be subtracted from real addresses
68 when resolving @dtpoff relocation.
69 This is PT_TLS segment p_vaddr. */
72 _bfd_x86_elf_dtpoff_base (struct bfd_link_info
*info
)
74 /* If tls_sec is NULL, we should have signalled an error already. */
75 if (elf_hash_table (info
)->tls_sec
== NULL
)
77 return elf_hash_table (info
)->tls_sec
->vma
;
80 /* Allocate space in .plt, .got and associated reloc sections for
84 elf_x86_allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
86 struct bfd_link_info
*info
;
87 struct elf_x86_link_hash_table
*htab
;
88 struct elf_x86_link_hash_entry
*eh
;
89 struct elf_dyn_relocs
*p
;
90 unsigned int plt_entry_size
;
91 bool resolved_to_zero
;
92 const struct elf_backend_data
*bed
;
94 if (h
->root
.type
== bfd_link_hash_indirect
)
97 eh
= (struct elf_x86_link_hash_entry
*) h
;
99 info
= (struct bfd_link_info
*) inf
;
100 bed
= get_elf_backend_data (info
->output_bfd
);
101 htab
= elf_x86_hash_table (info
, bed
->target_id
);
105 plt_entry_size
= htab
->plt
.plt_entry_size
;
107 resolved_to_zero
= UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
, eh
);
109 /* We can't use the GOT PLT if pointer equality is needed since
110 finish_dynamic_symbol won't clear symbol value and the dynamic
111 linker won't update the GOT slot. We will get into an infinite
113 if (htab
->plt_got
!= NULL
114 && h
->type
!= STT_GNU_IFUNC
115 && !h
->pointer_equality_needed
116 && h
->plt
.refcount
> 0
117 && h
->got
.refcount
> 0)
119 /* Don't use the regular PLT if there are both GOT and GOTPLT
121 h
->plt
.offset
= (bfd_vma
) -1;
123 /* Use the GOT PLT. */
124 eh
->plt_got
.refcount
= 1;
127 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
128 here if it is defined and referenced in a non-shared object. */
129 if (h
->type
== STT_GNU_IFUNC
132 /* GOTOFF relocation needs PLT. */
136 if (_bfd_elf_allocate_ifunc_dyn_relocs (info
, h
, &h
->dyn_relocs
,
140 htab
->got_entry_size
,
143 asection
*s
= htab
->plt_second
;
144 if (h
->plt
.offset
!= (bfd_vma
) -1 && s
!= NULL
)
146 /* Use the second PLT section if it is created. */
147 eh
->plt_second
.offset
= s
->size
;
149 /* Make room for this entry in the second PLT section. */
150 s
->size
+= htab
->non_lazy_plt
->plt_entry_size
;
158 /* Don't create the PLT entry if there are only function pointer
159 relocations which can be resolved at run-time. */
160 else if (htab
->elf
.dynamic_sections_created
161 && (h
->plt
.refcount
> 0
162 || eh
->plt_got
.refcount
> 0))
164 bool use_plt_got
= eh
->plt_got
.refcount
> 0;
166 /* Make sure this symbol is output as a dynamic symbol.
167 Undefined weak syms won't yet be marked as dynamic. */
171 && h
->root
.type
== bfd_link_hash_undefweak
)
173 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
177 if (bfd_link_pic (info
)
178 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
180 asection
*s
= htab
->elf
.splt
;
181 asection
*second_s
= htab
->plt_second
;
182 asection
*got_s
= htab
->plt_got
;
185 /* If this is the first .plt entry, make room for the special
186 first entry. The .plt section is used by prelink to undo
187 prelinking for dynamic relocations. */
189 s
->size
= htab
->plt
.has_plt0
* plt_entry_size
;
192 eh
->plt_got
.offset
= got_s
->size
;
195 h
->plt
.offset
= s
->size
;
197 eh
->plt_second
.offset
= second_s
->size
;
200 /* If this symbol is not defined in a regular file, and we are
201 generating PDE, then set the symbol to this location in the
202 .plt. This is required to make function pointers compare
203 as equal between PDE and the shared library.
205 NB: If PLT is PC-relative, we can use the .plt in PIE for
209 else if (htab
->pcrel_plt
)
210 use_plt
= ! bfd_link_dll (info
);
212 use_plt
= bfd_link_pde (info
);
217 /* We need to make a call to the entry of the GOT PLT
218 instead of regular PLT entry. */
219 h
->root
.u
.def
.section
= got_s
;
220 h
->root
.u
.def
.value
= eh
->plt_got
.offset
;
226 /* We need to make a call to the entry of the
227 second PLT instead of regular PLT entry. */
228 h
->root
.u
.def
.section
= second_s
;
229 h
->root
.u
.def
.value
= eh
->plt_second
.offset
;
233 h
->root
.u
.def
.section
= s
;
234 h
->root
.u
.def
.value
= h
->plt
.offset
;
239 /* Make room for this entry. */
241 got_s
->size
+= htab
->non_lazy_plt
->plt_entry_size
;
244 s
->size
+= plt_entry_size
;
246 second_s
->size
+= htab
->non_lazy_plt
->plt_entry_size
;
248 /* We also need to make an entry in the .got.plt section,
249 which will be placed in the .got section by the linker
251 htab
->elf
.sgotplt
->size
+= htab
->got_entry_size
;
253 /* There should be no PLT relocation against resolved
254 undefined weak symbol in executable. */
255 if (!resolved_to_zero
)
257 /* We also need to make an entry in the .rel.plt
259 htab
->elf
.srelplt
->size
+= htab
->sizeof_reloc
;
260 htab
->elf
.srelplt
->reloc_count
++;
264 if (htab
->elf
.target_os
== is_vxworks
&& !bfd_link_pic (info
))
266 /* VxWorks has a second set of relocations for each PLT entry
267 in executables. They go in a separate relocation section,
268 which is processed by the kernel loader. */
270 /* There are two relocations for the initial PLT entry: an
271 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
272 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
274 asection
*srelplt2
= htab
->srelplt2
;
275 if (h
->plt
.offset
== plt_entry_size
)
276 srelplt2
->size
+= (htab
->sizeof_reloc
* 2);
278 /* There are two extra relocations for each subsequent PLT entry:
279 an R_386_32 relocation for the GOT entry, and an R_386_32
280 relocation for the PLT entry. */
282 srelplt2
->size
+= (htab
->sizeof_reloc
* 2);
287 eh
->plt_got
.offset
= (bfd_vma
) -1;
288 h
->plt
.offset
= (bfd_vma
) -1;
294 eh
->plt_got
.offset
= (bfd_vma
) -1;
295 h
->plt
.offset
= (bfd_vma
) -1;
299 eh
->tlsdesc_got
= (bfd_vma
) -1;
301 /* For i386, if R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the
302 binary, make it a R_386_TLS_LE_32 requiring no TLS entry. For
303 x86-64, if R_X86_64_GOTTPOFF symbol is now local to the binary,
304 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
305 if (h
->got
.refcount
> 0
306 && bfd_link_executable (info
)
308 && (elf_x86_hash_entry (h
)->tls_type
& GOT_TLS_IE
))
309 h
->got
.offset
= (bfd_vma
) -1;
310 else if (h
->got
.refcount
> 0)
314 int tls_type
= elf_x86_hash_entry (h
)->tls_type
;
316 /* Make sure this symbol is output as a dynamic symbol.
317 Undefined weak syms won't yet be marked as dynamic. */
321 && h
->root
.type
== bfd_link_hash_undefweak
)
323 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
328 if (GOT_TLS_GDESC_P (tls_type
))
330 eh
->tlsdesc_got
= htab
->elf
.sgotplt
->size
331 - elf_x86_compute_jump_table_size (htab
);
332 htab
->elf
.sgotplt
->size
+= 2 * htab
->got_entry_size
;
333 h
->got
.offset
= (bfd_vma
) -2;
335 if (! GOT_TLS_GDESC_P (tls_type
)
336 || GOT_TLS_GD_P (tls_type
))
338 h
->got
.offset
= s
->size
;
339 s
->size
+= htab
->got_entry_size
;
340 /* R_386_TLS_GD and R_X86_64_TLSGD need 2 consecutive GOT
342 if (GOT_TLS_GD_P (tls_type
) || tls_type
== GOT_TLS_IE_BOTH
)
343 s
->size
+= htab
->got_entry_size
;
345 dyn
= htab
->elf
.dynamic_sections_created
;
346 /* R_386_TLS_IE_32 needs one dynamic relocation,
347 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
348 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
349 need two), R_386_TLS_GD and R_X86_64_TLSGD need one if local
350 symbol and two if global. No dynamic relocation against
351 resolved undefined weak symbol in executable. No dynamic
352 relocation against non-preemptible absolute symbol. */
353 if (tls_type
== GOT_TLS_IE_BOTH
)
354 htab
->elf
.srelgot
->size
+= 2 * htab
->sizeof_reloc
;
355 else if ((GOT_TLS_GD_P (tls_type
) && h
->dynindx
== -1)
356 || (tls_type
& GOT_TLS_IE
))
357 htab
->elf
.srelgot
->size
+= htab
->sizeof_reloc
;
358 else if (GOT_TLS_GD_P (tls_type
))
359 htab
->elf
.srelgot
->size
+= 2 * htab
->sizeof_reloc
;
360 else if (! GOT_TLS_GDESC_P (tls_type
)
361 && ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
362 && !resolved_to_zero
)
363 || h
->root
.type
!= bfd_link_hash_undefweak
)
364 && ((bfd_link_pic (info
)
365 && !(h
->dynindx
== -1
366 && ABS_SYMBOL_P (h
)))
367 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
368 htab
->elf
.srelgot
->size
+= htab
->sizeof_reloc
;
369 if (GOT_TLS_GDESC_P (tls_type
))
371 htab
->elf
.srelplt
->size
+= htab
->sizeof_reloc
;
372 if (bed
->target_id
== X86_64_ELF_DATA
)
373 htab
->elf
.tlsdesc_plt
= (bfd_vma
) -1;
377 h
->got
.offset
= (bfd_vma
) -1;
379 if (h
->dyn_relocs
== NULL
)
382 /* In the shared -Bsymbolic case, discard space allocated for
383 dynamic pc-relative relocs against symbols which turn out to be
384 defined in regular objects. For the normal shared case, discard
385 space for pc-relative relocs that have become local due to symbol
386 visibility changes. */
388 if (bfd_link_pic (info
))
390 /* Relocs that use pc_count are those that appear on a call
391 insn, or certain REL relocs that can generated via assembly.
392 We want calls to protected symbols to resolve directly to the
393 function rather than going via the plt. If people want
394 function pointer comparisons to work as expected then they
395 should avoid writing weird assembly. */
396 if (SYMBOL_CALLS_LOCAL (info
, h
))
398 struct elf_dyn_relocs
**pp
;
400 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
402 p
->count
-= p
->pc_count
;
411 if (htab
->elf
.target_os
== is_vxworks
)
413 struct elf_dyn_relocs
**pp
;
414 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
416 if (strcmp (p
->sec
->output_section
->name
, ".tls_vars") == 0)
423 /* Also discard relocs on undefined weak syms with non-default
424 visibility or in PIE. */
425 if (h
->dyn_relocs
!= NULL
)
427 if (h
->root
.type
== bfd_link_hash_undefweak
)
429 /* Undefined weak symbol is never bound locally in shared
431 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
434 if (bed
->target_id
== I386_ELF_DATA
437 /* Keep dynamic non-GOT/non-PLT relocation so
438 that we can branch to 0 without PLT. */
439 struct elf_dyn_relocs
**pp
;
441 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
442 if (p
->pc_count
== 0)
446 /* Remove non-R_386_PC32 relocation. */
447 p
->count
= p
->pc_count
;
451 /* Make sure undefined weak symbols are output
452 as dynamic symbols in PIEs for dynamic non-GOT
453 non-PLT reloations. */
454 if (h
->dyn_relocs
!= NULL
455 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
459 h
->dyn_relocs
= NULL
;
461 else if (h
->dynindx
== -1
463 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
466 else if (bfd_link_executable (info
)
467 && (h
->needs_copy
|| eh
->needs_copy
)
471 /* NB: needs_copy is set only for x86-64. For PIE,
472 discard space for pc-relative relocs against symbols
473 which turn out to need copy relocs. */
474 struct elf_dyn_relocs
**pp
;
476 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
478 if (p
->pc_count
!= 0)
486 else if (ELIMINATE_COPY_RELOCS
)
488 /* For the non-shared case, discard space for relocs against
489 symbols which turn out to need copy relocs or are not
490 dynamic. Keep dynamic relocations for run-time function
491 pointer initialization. */
494 || (h
->root
.type
== bfd_link_hash_undefweak
495 && !resolved_to_zero
))
498 || (htab
->elf
.dynamic_sections_created
499 && (h
->root
.type
== bfd_link_hash_undefweak
500 || h
->root
.type
== bfd_link_hash_undefined
))))
502 /* Make sure this symbol is output as a dynamic symbol.
503 Undefined weak syms won't yet be marked as dynamic. */
507 && h
->root
.type
== bfd_link_hash_undefweak
508 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
511 /* If that succeeded, we know we'll be keeping all the
513 if (h
->dynindx
!= -1)
517 h
->dyn_relocs
= NULL
;
522 /* Finally, allocate space. */
523 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
527 if (eh
->def_protected
&& bfd_link_executable (info
))
529 /* Disallow copy relocation against non-copyable protected
531 asection
*s
= p
->sec
->output_section
;
532 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
534 info
->callbacks
->einfo
535 /* xgettext:c-format */
536 (_("%F%P: %pB: copy relocation against non-copyable "
537 "protected symbol `%s' in %pB\n"),
538 p
->sec
->owner
, h
->root
.root
.string
,
539 h
->root
.u
.def
.section
->owner
);
544 sreloc
= elf_section_data (p
->sec
)->sreloc
;
546 BFD_ASSERT (sreloc
!= NULL
);
547 sreloc
->size
+= p
->count
* htab
->sizeof_reloc
;
553 /* Allocate space in .plt, .got and associated reloc sections for
554 local dynamic relocs. */
557 elf_x86_allocate_local_dynreloc (void **slot
, void *inf
)
559 struct elf_link_hash_entry
*h
560 = (struct elf_link_hash_entry
*) *slot
;
562 if (h
->type
!= STT_GNU_IFUNC
566 || h
->root
.type
!= bfd_link_hash_defined
)
569 return elf_x86_allocate_dynrelocs (h
, inf
);
572 /* Find and/or create a hash entry for local symbol. */
574 struct elf_link_hash_entry
*
575 _bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table
*htab
,
576 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
579 struct elf_x86_link_hash_entry e
, *ret
;
580 asection
*sec
= abfd
->sections
;
581 hashval_t h
= ELF_LOCAL_SYMBOL_HASH (sec
->id
,
582 htab
->r_sym (rel
->r_info
));
585 e
.elf
.indx
= sec
->id
;
586 e
.elf
.dynstr_index
= htab
->r_sym (rel
->r_info
);
587 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &e
, h
,
588 create
? INSERT
: NO_INSERT
);
595 ret
= (struct elf_x86_link_hash_entry
*) *slot
;
599 ret
= (struct elf_x86_link_hash_entry
*)
600 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
601 sizeof (struct elf_x86_link_hash_entry
));
604 memset (ret
, 0, sizeof (*ret
));
605 ret
->elf
.indx
= sec
->id
;
606 ret
->elf
.dynstr_index
= htab
->r_sym (rel
->r_info
);
607 ret
->elf
.dynindx
= -1;
608 ret
->plt_got
.offset
= (bfd_vma
) -1;
614 /* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN
615 SYNC WITH _bfd_elf_link_hash_newfunc. */
617 struct bfd_hash_entry
*
618 _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
619 struct bfd_hash_table
*table
,
622 /* Allocate the structure if it has not already been allocated by a
626 entry
= (struct bfd_hash_entry
*)
627 bfd_hash_allocate (table
,
628 sizeof (struct elf_x86_link_hash_entry
));
633 /* Call the allocation method of the superclass. */
634 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
637 struct elf_x86_link_hash_entry
*eh
638 = (struct elf_x86_link_hash_entry
*) entry
;
639 struct elf_link_hash_table
*htab
640 = (struct elf_link_hash_table
*) table
;
642 memset (&eh
->elf
.size
, 0,
643 (sizeof (struct elf_x86_link_hash_entry
)
644 - offsetof (struct elf_link_hash_entry
, size
)));
645 /* Set local fields. */
647 eh
->elf
.dynindx
= -1;
648 eh
->elf
.got
= htab
->init_got_refcount
;
649 eh
->elf
.plt
= htab
->init_plt_refcount
;
650 /* Assume that we have been called by a non-ELF symbol reader.
651 This flag is then reset by the code which reads an ELF input
652 file. This ensures that a symbol created by a non-ELF symbol
653 reader will have the flag set correctly. */
655 eh
->plt_second
.offset
= (bfd_vma
) -1;
656 eh
->plt_got
.offset
= (bfd_vma
) -1;
657 eh
->tlsdesc_got
= (bfd_vma
) -1;
658 eh
->zero_undefweak
= 1;
664 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
665 for local symbol so that we can handle local STT_GNU_IFUNC symbols
666 as global symbol. We reuse indx and dynstr_index for local symbol
667 hash since they aren't used by global symbols in this backend. */
670 _bfd_x86_elf_local_htab_hash (const void *ptr
)
672 struct elf_link_hash_entry
*h
673 = (struct elf_link_hash_entry
*) ptr
;
674 return ELF_LOCAL_SYMBOL_HASH (h
->indx
, h
->dynstr_index
);
677 /* Compare local hash entries. */
680 _bfd_x86_elf_local_htab_eq (const void *ptr1
, const void *ptr2
)
682 struct elf_link_hash_entry
*h1
683 = (struct elf_link_hash_entry
*) ptr1
;
684 struct elf_link_hash_entry
*h2
685 = (struct elf_link_hash_entry
*) ptr2
;
687 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
690 /* Destroy an x86 ELF linker hash table. */
693 elf_x86_link_hash_table_free (bfd
*obfd
)
695 struct elf_x86_link_hash_table
*htab
696 = (struct elf_x86_link_hash_table
*) obfd
->link
.hash
;
698 if (htab
->loc_hash_table
)
699 htab_delete (htab
->loc_hash_table
);
700 if (htab
->loc_hash_memory
)
701 objalloc_free ((struct objalloc
*) htab
->loc_hash_memory
);
702 _bfd_elf_link_hash_table_free (obfd
);
706 elf_i386_is_reloc_section (const char *secname
)
708 return startswith (secname
, ".rel");
712 elf_x86_64_is_reloc_section (const char *secname
)
714 return startswith (secname
, ".rela");
717 /* Create an x86 ELF linker hash table. */
719 struct bfd_link_hash_table
*
720 _bfd_x86_elf_link_hash_table_create (bfd
*abfd
)
722 struct elf_x86_link_hash_table
*ret
;
723 const struct elf_backend_data
*bed
;
724 size_t amt
= sizeof (struct elf_x86_link_hash_table
);
726 ret
= (struct elf_x86_link_hash_table
*) bfd_zmalloc (amt
);
730 bed
= get_elf_backend_data (abfd
);
731 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
,
732 _bfd_x86_elf_link_hash_newfunc
,
733 sizeof (struct elf_x86_link_hash_entry
),
740 if (bed
->target_id
== X86_64_ELF_DATA
)
742 ret
->is_reloc_section
= elf_x86_64_is_reloc_section
;
743 ret
->got_entry_size
= 8;
744 ret
->pcrel_plt
= true;
745 ret
->tls_get_addr
= "__tls_get_addr";
746 ret
->relative_r_type
= R_X86_64_RELATIVE
;
747 ret
->relative_r_name
= "R_X86_64_RELATIVE";
748 ret
->ax_register
= "RAX";
749 ret
->elf_append_reloc
= elf_append_rela
;
750 ret
->elf_write_addend_in_got
= _bfd_elf64_write_addend
;
754 ret
->sizeof_reloc
= sizeof (Elf64_External_Rela
);
755 ret
->pointer_r_type
= R_X86_64_64
;
756 ret
->dynamic_interpreter
= ELF64_DYNAMIC_INTERPRETER
;
757 ret
->dynamic_interpreter_size
= sizeof ELF64_DYNAMIC_INTERPRETER
;
758 ret
->elf_write_addend
= _bfd_elf64_write_addend
;
762 if (bed
->target_id
== X86_64_ELF_DATA
)
764 ret
->sizeof_reloc
= sizeof (Elf32_External_Rela
);
765 ret
->pointer_r_type
= R_X86_64_32
;
766 ret
->dynamic_interpreter
= ELFX32_DYNAMIC_INTERPRETER
;
767 ret
->dynamic_interpreter_size
768 = sizeof ELFX32_DYNAMIC_INTERPRETER
;
769 ret
->elf_write_addend
= _bfd_elf32_write_addend
;
773 ret
->is_reloc_section
= elf_i386_is_reloc_section
;
774 ret
->sizeof_reloc
= sizeof (Elf32_External_Rel
);
775 ret
->got_entry_size
= 4;
776 ret
->pcrel_plt
= false;
777 ret
->pointer_r_type
= R_386_32
;
778 ret
->relative_r_type
= R_386_RELATIVE
;
779 ret
->relative_r_name
= "R_386_RELATIVE";
780 ret
->ax_register
= "EAX";
781 ret
->elf_append_reloc
= elf_append_rel
;
782 ret
->elf_write_addend
= _bfd_elf32_write_addend
;
783 ret
->elf_write_addend_in_got
= _bfd_elf32_write_addend
;
784 ret
->dynamic_interpreter
= ELF32_DYNAMIC_INTERPRETER
;
785 ret
->dynamic_interpreter_size
786 = sizeof ELF32_DYNAMIC_INTERPRETER
;
787 ret
->tls_get_addr
= "___tls_get_addr";
791 ret
->loc_hash_table
= htab_try_create (1024,
792 _bfd_x86_elf_local_htab_hash
,
793 _bfd_x86_elf_local_htab_eq
,
795 ret
->loc_hash_memory
= objalloc_create ();
796 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
798 elf_x86_link_hash_table_free (abfd
);
801 ret
->elf
.root
.hash_table_free
= elf_x86_link_hash_table_free
;
803 return &ret
->elf
.root
;
806 /* Sort relocs into address order. */
809 _bfd_x86_elf_compare_relocs (const void *ap
, const void *bp
)
811 const arelent
*a
= * (const arelent
**) ap
;
812 const arelent
*b
= * (const arelent
**) bp
;
814 if (a
->address
> b
->address
)
816 else if (a
->address
< b
->address
)
822 /* Mark symbol, NAME, as locally defined by linker if it is referenced
823 and not defined in a relocatable object file. */
826 elf_x86_linker_defined (struct bfd_link_info
*info
, const char *name
)
828 struct elf_link_hash_entry
*h
;
830 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
831 false, false, false);
835 while (h
->root
.type
== bfd_link_hash_indirect
)
836 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
838 if (h
->root
.type
== bfd_link_hash_new
839 || h
->root
.type
== bfd_link_hash_undefined
840 || h
->root
.type
== bfd_link_hash_undefweak
841 || h
->root
.type
== bfd_link_hash_common
842 || (!h
->def_regular
&& h
->def_dynamic
))
844 elf_x86_hash_entry (h
)->local_ref
= 2;
845 elf_x86_hash_entry (h
)->linker_def
= 1;
849 /* Hide a linker-defined symbol, NAME, with hidden visibility. */
852 elf_x86_hide_linker_defined (struct bfd_link_info
*info
,
855 struct elf_link_hash_entry
*h
;
857 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
858 false, false, false);
862 while (h
->root
.type
== bfd_link_hash_indirect
)
863 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
865 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
866 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
867 _bfd_elf_link_hash_hide_symbol (info
, h
, true);
871 _bfd_x86_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
873 if (!bfd_link_relocatable (info
))
875 /* Check for __tls_get_addr reference. */
876 struct elf_x86_link_hash_table
*htab
;
877 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
878 htab
= elf_x86_hash_table (info
, bed
->target_id
);
881 struct elf_link_hash_entry
*h
;
883 h
= elf_link_hash_lookup (elf_hash_table (info
),
885 false, false, false);
888 elf_x86_hash_entry (h
)->tls_get_addr
= 1;
890 /* Check the versioned __tls_get_addr symbol. */
891 while (h
->root
.type
== bfd_link_hash_indirect
)
893 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
894 elf_x86_hash_entry (h
)->tls_get_addr
= 1;
898 /* "__ehdr_start" will be defined by linker as a hidden symbol
899 later if it is referenced and not defined. */
900 elf_x86_linker_defined (info
, "__ehdr_start");
902 if (bfd_link_executable (info
))
904 /* References to __bss_start, _end and _edata should be
905 locally resolved within executables. */
906 elf_x86_linker_defined (info
, "__bss_start");
907 elf_x86_linker_defined (info
, "_end");
908 elf_x86_linker_defined (info
, "_edata");
912 /* Hide hidden __bss_start, _end and _edata in shared
914 elf_x86_hide_linker_defined (info
, "__bss_start");
915 elf_x86_hide_linker_defined (info
, "_end");
916 elf_x86_hide_linker_defined (info
, "_edata");
921 /* Invoke the regular ELF backend linker to do all the work. */
922 return _bfd_elf_link_check_relocs (abfd
, info
);
925 /* Look through the relocs for a section before allocation to make the
926 dynamic reloc section. */
929 _bfd_x86_elf_check_relocs (bfd
*abfd
,
930 struct bfd_link_info
*info
,
932 const Elf_Internal_Rela
*relocs
)
934 struct elf_x86_link_hash_table
*htab
;
935 Elf_Internal_Shdr
*symtab_hdr
;
936 struct elf_link_hash_entry
**sym_hashes
;
937 const Elf_Internal_Rela
*rel
;
938 const Elf_Internal_Rela
*rel_end
;
940 const struct elf_backend_data
*bed
;
943 if (bfd_link_relocatable (info
))
946 bed
= get_elf_backend_data (abfd
);
947 htab
= elf_x86_hash_table (info
, bed
->target_id
);
950 sec
->check_relocs_failed
= 1;
954 is_x86_64
= bed
->target_id
== X86_64_ELF_DATA
;
956 symtab_hdr
= &elf_symtab_hdr (abfd
);
957 sym_hashes
= elf_sym_hashes (abfd
);
959 rel_end
= relocs
+ sec
->reloc_count
;
960 for (rel
= relocs
; rel
< rel_end
; rel
++)
963 unsigned int r_symndx
;
964 struct elf_link_hash_entry
*h
;
966 r_symndx
= htab
->r_sym (rel
->r_info
);
967 r_type
= ELF32_R_TYPE (rel
->r_info
);
969 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
971 /* xgettext:c-format */
972 _bfd_error_handler (_("%pB: bad symbol index: %d"),
977 if (r_symndx
< symtab_hdr
->sh_info
)
981 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
982 while (h
->root
.type
== bfd_link_hash_indirect
983 || h
->root
.type
== bfd_link_hash_warning
)
984 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
987 if (X86_NEED_DYNAMIC_RELOC_TYPE_P (is_x86_64
, r_type
)
988 && NEED_DYNAMIC_RELOCATION_P (is_x86_64
, info
, true, h
, sec
,
989 r_type
, htab
->pointer_r_type
))
991 /* We may copy these reloc types into the output file.
992 Create a reloc section in dynobj and make room for
994 sreloc
= _bfd_elf_make_dynamic_reloc_section
995 (sec
, htab
->elf
.dynobj
, ABI_64_P (abfd
) ? 3 : 2,
996 abfd
, sec
->use_rela_p
);
1002 sec
->check_relocs_failed
= 1;
1010 /* Add an entry to the relative reloc record. */
1013 elf_x86_relative_reloc_record_add
1014 (struct bfd_link_info
*info
,
1015 struct elf_x86_relative_reloc_data
*relative_reloc
,
1016 Elf_Internal_Rela
*rel
, asection
*sec
,
1017 asection
*sym_sec
, struct elf_link_hash_entry
*h
,
1018 Elf_Internal_Sym
*sym
, bfd_vma offset
, bool *keep_symbuf_p
)
1020 bfd_size_type newidx
;
1022 if (relative_reloc
->data
== NULL
)
1024 relative_reloc
->data
= bfd_malloc
1025 (sizeof (struct elf_x86_relative_reloc_record
));
1026 relative_reloc
->count
= 0;
1027 relative_reloc
->size
= 1;
1030 newidx
= relative_reloc
->count
++;
1032 if (relative_reloc
->count
> relative_reloc
->size
)
1034 relative_reloc
->size
<<= 1;
1035 relative_reloc
->data
= bfd_realloc
1036 (relative_reloc
->data
,
1037 (relative_reloc
->size
1038 * sizeof (struct elf_x86_relative_reloc_record
)));
1041 if (relative_reloc
->data
== NULL
)
1043 info
->callbacks
->einfo
1044 /* xgettext:c-format */
1045 (_("%F%P: %pB: failed to allocate relative reloc record\n"),
1050 relative_reloc
->data
[newidx
].rel
= *rel
;
1051 relative_reloc
->data
[newidx
].sec
= sec
;
1054 /* Set SYM to NULL to indicate a global symbol. */
1055 relative_reloc
->data
[newidx
].sym
= NULL
;
1056 relative_reloc
->data
[newidx
].u
.h
= h
;
1060 relative_reloc
->data
[newidx
].sym
= sym
;
1061 relative_reloc
->data
[newidx
].u
.sym_sec
= sym_sec
;
1062 /* We must keep the symbol buffer since SYM will be used later. */
1063 *keep_symbuf_p
= true;
1065 relative_reloc
->data
[newidx
].offset
= offset
;
1066 relative_reloc
->data
[newidx
].address
= 0;
1070 /* After input sections have been mapped to output sections and
1071 addresses of output sections are set initiallly, scan input
1072 relocations with the same logic in relocate_section to determine
1073 if a relative relocation should be generated. Save the relative
1074 relocation candidate information for sizing the DT_RELR section
1075 later after all symbols addresses can be determined. */
1078 _bfd_x86_elf_link_relax_section (bfd
*abfd ATTRIBUTE_UNUSED
,
1079 asection
*input_section
,
1080 struct bfd_link_info
*info
,
1083 Elf_Internal_Shdr
*symtab_hdr
;
1084 Elf_Internal_Rela
*internal_relocs
;
1085 Elf_Internal_Rela
*irel
, *irelend
;
1086 Elf_Internal_Sym
*isymbuf
= NULL
;
1087 struct elf_link_hash_entry
**sym_hashes
;
1088 const struct elf_backend_data
*bed
;
1089 struct elf_x86_link_hash_table
*htab
;
1090 bfd_vma
*local_got_offsets
;
1092 bool unaligned_section
;
1093 bool return_status
= false;
1094 bool keep_symbuf
= false;
1096 if (bfd_link_relocatable (info
))
1099 /* Assume we're not going to change any sizes, and we'll only need
1103 bed
= get_elf_backend_data (abfd
);
1104 htab
= elf_x86_hash_table (info
, bed
->target_id
);
1108 /* Nothing to do if there are no relocations or relative relocations
1109 have been packed. */
1110 if (input_section
== htab
->elf
.srelrdyn
1111 || input_section
->relative_reloc_packed
1112 || ((input_section
->flags
& (SEC_RELOC
| SEC_ALLOC
))
1113 != (SEC_RELOC
| SEC_ALLOC
))
1114 || (input_section
->flags
& SEC_DEBUGGING
) != 0
1115 || input_section
->reloc_count
== 0)
1118 /* Skip if the section isn't aligned. */
1119 unaligned_section
= input_section
->alignment_power
== 0;
1121 is_x86_64
= bed
->target_id
== X86_64_ELF_DATA
;
1123 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1124 sym_hashes
= elf_sym_hashes (abfd
);
1125 local_got_offsets
= elf_local_got_offsets (abfd
);
1127 /* Load the relocations for this section. */
1129 _bfd_elf_link_info_read_relocs (abfd
, info
, input_section
, NULL
,
1130 (Elf_Internal_Rela
*) NULL
,
1132 if (internal_relocs
== NULL
)
1135 irelend
= internal_relocs
+ input_section
->reloc_count
;
1136 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1138 unsigned int r_type
;
1139 unsigned int r_symndx
;
1140 Elf_Internal_Sym
*isym
;
1141 struct elf_link_hash_entry
*h
;
1142 struct elf_x86_link_hash_entry
*eh
;
1144 bool resolved_to_zero
;
1145 bool need_copy_reloc_in_pie
;
1148 /* Offset must be a multiple of 2. */
1149 bool unaligned_offset
= (irel
->r_offset
& 1) != 0;
1150 /* True if there is a relative relocation against a dynamic
1152 bool dynamic_relative_reloc_p
;
1154 /* Get the value of the symbol referred to by the reloc. */
1155 r_symndx
= htab
->r_sym (irel
->r_info
);
1157 r_type
= ELF32_R_TYPE (irel
->r_info
);
1158 /* Clear the R_X86_64_converted_reloc_bit bit. */
1159 r_type
&= ~R_X86_64_converted_reloc_bit
;
1163 dynamic_relative_reloc_p
= false;
1165 if (r_symndx
< symtab_hdr
->sh_info
)
1167 /* Read this BFD's local symbols. */
1168 if (isymbuf
== NULL
)
1170 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1171 if (isymbuf
== NULL
)
1173 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
1174 symtab_hdr
->sh_info
,
1175 0, NULL
, NULL
, NULL
);
1176 if (isymbuf
== NULL
)
1181 isym
= isymbuf
+ r_symndx
;
1182 switch (isym
->st_shndx
)
1185 sec
= bfd_abs_section_ptr
;
1188 sec
= bfd_com_section_ptr
;
1190 case SHN_X86_64_LCOMMON
:
1193 sec
= &_bfd_elf_large_com_section
;
1196 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1200 /* Skip relocation against local STT_GNU_IFUNC symbol. */
1201 if (ELF32_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
1204 eh
= (struct elf_x86_link_hash_entry
*) h
;
1205 resolved_to_zero
= false;
1209 /* Get H and SEC for GENERATE_DYNAMIC_RELOCATION_P below. */
1210 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1211 while (h
->root
.type
== bfd_link_hash_indirect
1212 || h
->root
.type
== bfd_link_hash_warning
)
1213 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1215 if (h
->root
.type
== bfd_link_hash_defined
1216 || h
->root
.type
== bfd_link_hash_defweak
)
1217 sec
= h
->root
.u
.def
.section
;
1219 /* Skip relocation against STT_GNU_IFUNC symbol. */
1220 if (h
->type
== STT_GNU_IFUNC
)
1223 eh
= (struct elf_x86_link_hash_entry
*) h
;
1224 resolved_to_zero
= UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
, eh
);
1226 /* NB: See how elf_backend_finish_dynamic_symbol is called
1227 from elf_link_output_extsym. */
1228 if ((h
->dynindx
!= -1 || h
->forced_local
)
1229 && ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
1230 || h
->root
.type
!= bfd_link_hash_undefweak
)
1231 || !h
->forced_local
)
1232 && h
->got
.offset
!= (bfd_vma
) -1
1233 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h
)->tls_type
)
1234 && elf_x86_hash_entry (h
)->tls_type
!= GOT_TLS_IE
1235 && !resolved_to_zero
1236 && SYMBOL_REFERENCES_LOCAL_P (info
, h
)
1237 && SYMBOL_DEFINED_NON_SHARED_P (h
))
1238 dynamic_relative_reloc_p
= true;
1243 if (X86_GOT_TYPE_P (is_x86_64
, r_type
))
1245 /* Pack GOT relative relocations. There should be only a
1246 single R_*_RELATIVE relocation in GOT. */
1249 if (eh
->got_relative_reloc_done
)
1252 if (!(dynamic_relative_reloc_p
1253 || (RESOLVED_LOCALLY_P (info
, h
, htab
)
1254 && GENERATE_RELATIVE_RELOC_P (info
, h
))))
1257 if (!dynamic_relative_reloc_p
)
1258 eh
->no_finish_dynamic_symbol
= 1;
1259 eh
->got_relative_reloc_done
= 1;
1260 offset
= h
->got
.offset
;
1264 if (elf_x86_relative_reloc_done (abfd
)[r_symndx
])
1267 if (!X86_LOCAL_GOT_RELATIVE_RELOC_P (is_x86_64
, info
,
1271 elf_x86_relative_reloc_done (abfd
)[r_symndx
] = 1;
1272 offset
= local_got_offsets
[r_symndx
];
1275 if (!elf_x86_relative_reloc_record_add (info
,
1276 &htab
->relative_reloc
,
1277 irel
, htab
->elf
.sgot
,
1278 sec
, h
, isym
, offset
,
1286 && irel
->r_addend
== 0
1287 && !ABI_64_P (info
->output_bfd
))
1289 /* For x32, if addend is zero, treat R_X86_64_64 like
1290 R_X86_64_32 and R_X86_64_SIZE64 like R_X86_64_SIZE32. */
1291 if (r_type
== R_X86_64_64
)
1292 r_type
= R_X86_64_32
;
1293 else if (r_type
== R_X86_64_SIZE64
)
1294 r_type
= R_X86_64_SIZE32
;
1297 if (!X86_RELATIVE_RELOC_TYPE_P (is_x86_64
, r_type
))
1300 /* Pack non-GOT relative relocations. */
1303 need_copy_reloc_in_pie
=
1304 (bfd_link_pie (info
)
1308 || (h
->root
.type
== bfd_link_hash_undefined
))
1309 && (X86_PCREL_TYPE_P (true, r_type
)
1310 || X86_SIZE_TYPE_P (true, r_type
)));
1315 need_copy_reloc_in_pie
= false;
1316 pc32_reloc
= r_type
== R_386_PC32
;
1319 if (GENERATE_DYNAMIC_RELOCATION_P (is_x86_64
, info
, eh
, r_type
,
1320 sec
, need_copy_reloc_in_pie
,
1321 resolved_to_zero
, pc32_reloc
))
1323 /* When generating a shared object, these relocations
1324 are copied into the output file to be resolved at run
1326 offset
= _bfd_elf_section_offset (info
->output_bfd
, info
,
1329 if (offset
== (bfd_vma
) -1
1330 || offset
== (bfd_vma
) -2
1331 || COPY_INPUT_RELOC_P (is_x86_64
, info
, h
, r_type
))
1334 /* This symbol is local, or marked to become local. When
1335 relocation overflow check is disabled, we convert
1336 R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
1338 && !(r_type
== htab
->pointer_r_type
1339 || (r_type
== R_X86_64_32
1340 && htab
->params
->no_reloc_overflow_check
)))
1343 if (!elf_x86_relative_reloc_record_add
1345 ((unaligned_section
|| unaligned_offset
)
1346 ? &htab
->unaligned_relative_reloc
1347 : &htab
->relative_reloc
),
1348 irel
, input_section
, sec
, h
, isym
, offset
,
1354 input_section
->relative_reloc_packed
= 1;
1356 return_status
= true;
1359 if ((unsigned char *) isymbuf
!= symtab_hdr
->contents
)
1361 /* Cache the symbol buffer if it must be kept. */
1363 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1367 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
1368 free (internal_relocs
);
1369 return return_status
;
1372 /* Add an entry to the 64-bit DT_RELR bitmap. */
1375 elf64_dt_relr_bitmap_add
1376 (struct bfd_link_info
*info
, struct elf_dt_relr_bitmap
*bitmap
,
1379 bfd_size_type newidx
;
1381 if (bitmap
->u
.elf64
== NULL
)
1383 bitmap
->u
.elf64
= bfd_malloc (sizeof (uint64_t));
1388 newidx
= bitmap
->count
++;
1390 if (bitmap
->count
> bitmap
->size
)
1393 bitmap
->u
.elf64
= bfd_realloc (bitmap
->u
.elf64
,
1394 (bitmap
->size
* sizeof (uint64_t)));
1397 if (bitmap
->u
.elf64
== NULL
)
1399 info
->callbacks
->einfo
1400 /* xgettext:c-format */
1401 (_("%F%P: %pB: failed to allocate 64-bit DT_RELR bitmap\n"),
1405 bitmap
->u
.elf64
[newidx
] = entry
;
1408 /* Add an entry to the 32-bit DT_RELR bitmap. */
1411 elf32_dt_relr_bitmap_add
1412 (struct bfd_link_info
*info
, struct elf_dt_relr_bitmap
*bitmap
,
1415 bfd_size_type newidx
;
1417 if (bitmap
->u
.elf32
== NULL
)
1419 bitmap
->u
.elf32
= bfd_malloc (sizeof (uint32_t));
1424 newidx
= bitmap
->count
++;
1426 if (bitmap
->count
> bitmap
->size
)
1429 bitmap
->u
.elf32
= bfd_realloc (bitmap
->u
.elf32
,
1430 (bitmap
->size
* sizeof (uint32_t)));
1433 if (bitmap
->u
.elf32
== NULL
)
1435 info
->callbacks
->einfo
1436 /* xgettext:c-format */
1437 (_("%F%P: %pB: failed to allocate 32-bit DT_RELR bitmap\n"),
1441 bitmap
->u
.elf32
[newidx
] = entry
;
1445 _bfd_elf32_write_addend (bfd
*abfd
, uint64_t value
, void *addr
)
1447 bfd_put_32 (abfd
, value
, addr
);
1451 _bfd_elf64_write_addend (bfd
*abfd
, uint64_t value
, void *addr
)
1453 bfd_put_64 (abfd
, value
, addr
);
1456 /* Size or finish relative relocations to determine the run-time
1457 addresses for DT_RELR bitmap computation later. OUTREL is set
1458 to NULL in the sizing phase and non-NULL in the finising phase
1459 where the regular relative relocations will be written out. */
1462 elf_x86_size_or_finish_relative_reloc
1463 (bool is_x86_64
, struct bfd_link_info
*info
,
1464 struct elf_x86_link_hash_table
*htab
, bool unaligned
,
1465 Elf_Internal_Rela
*outrel
)
1467 unsigned int align_mask
;
1468 bfd_size_type i
, count
;
1469 asection
*sec
, *srel
;
1470 struct elf_link_hash_entry
*h
;
1472 Elf_Internal_Sym
*sym
;
1474 asection
*sgot
= htab
->elf
.sgot
;
1475 asection
*srelgot
= htab
->elf
.srelgot
;
1476 struct elf_x86_relative_reloc_data
*relative_reloc
;
1481 relative_reloc
= &htab
->unaligned_relative_reloc
;
1486 relative_reloc
= &htab
->relative_reloc
;
1489 count
= relative_reloc
->count
;
1490 for (i
= 0; i
< count
; i
++)
1492 sec
= relative_reloc
->data
[i
].sec
;
1493 sym
= relative_reloc
->data
[i
].sym
;
1495 /* If SYM is NULL, it must be a global symbol. */
1497 h
= relative_reloc
->data
[i
].u
.h
;
1504 /* This function may be called more than once and REL may be
1505 updated by _bfd_elf_rela_local_sym below. */
1506 Elf_Internal_Rela rel
= relative_reloc
->data
[i
].rel
;
1510 if (h
->root
.type
== bfd_link_hash_defined
1511 || h
->root
.type
== bfd_link_hash_defweak
)
1513 sym_sec
= h
->root
.u
.def
.section
;
1514 relocation
= (h
->root
.u
.def
.value
1515 + sym_sec
->output_section
->vma
1516 + sym_sec
->output_offset
);
1520 /* Allow undefined symbol only at the sizing phase.
1521 Otherwise skip undefined symbol here. Undefined
1522 symbol will be reported by relocate_section. */
1531 sym_sec
= relative_reloc
->data
[i
].u
.sym_sec
;
1532 relocation
= _bfd_elf_rela_local_sym
1533 (info
->output_bfd
, sym
, &sym_sec
, &rel
);
1538 outrel
->r_addend
= relocation
;
1541 if (h
!= NULL
&& h
->needs_plt
)
1545 outrel
->r_addend
+= rel
.r_addend
;
1547 /* Write the implicit addend if ALIGN_MASK isn't 0. */
1552 if (relative_reloc
->data
[i
].offset
>= sec
->size
)
1554 htab
->elf_write_addend_in_got
1555 (info
->output_bfd
, outrel
->r_addend
,
1556 sec
->contents
+ relative_reloc
->data
[i
].offset
);
1562 if (rel
.r_offset
>= sec
->size
)
1565 if (elf_section_data (sec
)->this_hdr
.contents
1568 = elf_section_data (sec
)->this_hdr
.contents
;
1571 if (!_bfd_elf_mmap_section_contents (sec
->owner
,
1574 info
->callbacks
->einfo
1575 /* xgettext:c-format */
1576 (_("%F%P: %pB: failed to allocate memory for section `%pA'\n"),
1577 info
->output_bfd
, sec
);
1579 /* Cache the section contents for
1580 elf_link_input_bfd. */
1581 elf_section_data (sec
)->this_hdr
.contents
1584 htab
->elf_write_addend
1585 (info
->output_bfd
, outrel
->r_addend
,
1586 contents
+ rel
.r_offset
);
1595 srel
= elf_section_data (sec
)->sreloc
;
1596 offset
= (sec
->output_section
->vma
+ sec
->output_offset
1597 + relative_reloc
->data
[i
].offset
);
1598 relative_reloc
->data
[i
].address
= offset
;
1601 outrel
->r_offset
= offset
;
1603 if ((outrel
->r_offset
& align_mask
) != 0)
1606 if (htab
->params
->report_relative_reloc
)
1607 _bfd_x86_elf_link_report_relative_reloc
1608 (info
, sec
, h
, sym
, htab
->relative_r_name
, outrel
);
1610 /* Generate regular relative relocation if ALIGN_MASK is 0. */
1611 if (align_mask
== 0)
1612 htab
->elf_append_reloc (info
->output_bfd
, srel
, outrel
);
1617 /* Compute the DT_RELR section size. Set NEED_PLAYOUT to true if
1618 the DT_RELR section size has been increased. */
1621 elf_x86_compute_dl_relr_bitmap
1622 (struct bfd_link_info
*info
, struct elf_x86_link_hash_table
*htab
,
1626 bfd_size_type i
, count
, new_count
;
1627 struct elf_x86_relative_reloc_data
*relative_reloc
=
1628 &htab
->relative_reloc
;
1629 /* Save the old DT_RELR bitmap count. Don't shrink the DT_RELR bitmap
1630 if the new DT_RELR bitmap count is smaller than the old one. Pad
1631 with trailing 1s which won't be decoded to more relocations. */
1632 bfd_size_type dt_relr_bitmap_count
= htab
->dt_relr_bitmap
.count
;
1634 /* Clear the DT_RELR bitmap count. */
1635 htab
->dt_relr_bitmap
.count
= 0;
1637 count
= relative_reloc
->count
;
1639 if (ABI_64_P (info
->output_bfd
))
1641 /* Compute the 64-bit DT_RELR bitmap. */
1645 if ((relative_reloc
->data
[i
].address
% 1) != 0)
1648 elf64_dt_relr_bitmap_add (info
, &htab
->dt_relr_bitmap
,
1649 relative_reloc
->data
[i
].address
);
1651 base
= relative_reloc
->data
[i
].address
+ 8;
1656 uint64_t bitmap
= 0;
1657 for (; i
< count
; i
++)
1659 bfd_vma delta
= (relative_reloc
->data
[i
].address
1661 /* Stop if it is too far from base. */
1662 if (delta
>= 63 * 8)
1664 /* Stop if it isn't a multiple of 8. */
1665 if ((delta
% 8) != 0)
1667 bitmap
|= 1ULL << (delta
/ 8);
1673 elf64_dt_relr_bitmap_add (info
, &htab
->dt_relr_bitmap
,
1680 new_count
= htab
->dt_relr_bitmap
.count
;
1681 if (dt_relr_bitmap_count
> new_count
)
1683 /* Don't shrink the DT_RELR section size to avoid section
1684 layout oscillation. Instead, pad the DT_RELR bitmap with
1685 1s which do not decode to more relocations. */
1687 htab
->dt_relr_bitmap
.count
= dt_relr_bitmap_count
;
1688 count
= dt_relr_bitmap_count
- new_count
;
1689 for (i
= 0; i
< count
; i
++)
1690 htab
->dt_relr_bitmap
.u
.elf64
[new_count
+ i
] = 1;
1695 /* Compute the 32-bit DT_RELR bitmap. */
1699 if ((relative_reloc
->data
[i
].address
% 1) != 0)
1702 elf32_dt_relr_bitmap_add (info
, &htab
->dt_relr_bitmap
,
1703 relative_reloc
->data
[i
].address
);
1705 base
= relative_reloc
->data
[i
].address
+ 4;
1710 uint32_t bitmap
= 0;
1711 for (; i
< count
; i
++)
1713 bfd_vma delta
= (relative_reloc
->data
[i
].address
1715 /* Stop if it is too far from base. */
1716 if (delta
>= 31 * 4)
1718 /* Stop if it isn't a multiple of 4. */
1719 if ((delta
% 4) != 0)
1721 bitmap
|= 1ULL << (delta
/ 4);
1727 elf32_dt_relr_bitmap_add (info
, &htab
->dt_relr_bitmap
,
1734 new_count
= htab
->dt_relr_bitmap
.count
;
1735 if (dt_relr_bitmap_count
> new_count
)
1737 /* Don't shrink the DT_RELR section size to avoid section
1738 layout oscillation. Instead, pad the DT_RELR bitmap with
1739 1s which do not decode to more relocations. */
1741 htab
->dt_relr_bitmap
.count
= dt_relr_bitmap_count
;
1742 count
= dt_relr_bitmap_count
- new_count
;
1743 for (i
= 0; i
< count
; i
++)
1744 htab
->dt_relr_bitmap
.u
.elf32
[new_count
+ i
] = 1;
1748 if (htab
->dt_relr_bitmap
.count
!= dt_relr_bitmap_count
)
1752 /* The .relr.dyn section size is changed. Update the section
1753 size and tell linker to layout sections again. */
1754 htab
->elf
.srelrdyn
->size
=
1755 (htab
->dt_relr_bitmap
.count
1756 * (ABI_64_P (info
->output_bfd
) ? 8 : 4));
1758 *need_layout
= true;
1761 info
->callbacks
->einfo
1762 /* xgettext:c-format */
1763 (_("%F%P: %pB: size of compact relative reloc section is "
1764 "changed: new (%lu) != old (%lu)\n"),
1765 info
->output_bfd
, htab
->dt_relr_bitmap
.count
,
1766 dt_relr_bitmap_count
);
1770 /* Write out the DT_RELR section. */
1773 elf_x86_write_dl_relr_bitmap (struct bfd_link_info
*info
,
1774 struct elf_x86_link_hash_table
*htab
)
1776 asection
*sec
= htab
->elf
.srelrdyn
;
1777 bfd_size_type size
= sec
->size
;
1779 unsigned char *contents
;
1781 contents
= (unsigned char *) bfd_alloc (sec
->owner
, size
);
1782 if (contents
== NULL
)
1783 info
->callbacks
->einfo
1784 /* xgettext:c-format */
1785 (_("%F%P: %pB: failed to allocate compact relative reloc section\n"),
1788 /* Cache the section contents for elf_link_input_bfd. */
1789 sec
->contents
= contents
;
1791 if (ABI_64_P (info
->output_bfd
))
1792 for (i
= 0; i
< htab
->dt_relr_bitmap
.count
; i
++, contents
+= 8)
1793 bfd_put_64 (info
->output_bfd
, htab
->dt_relr_bitmap
.u
.elf64
[i
],
1796 for (i
= 0; i
< htab
->dt_relr_bitmap
.count
; i
++, contents
+= 4)
1797 bfd_put_32 (info
->output_bfd
, htab
->dt_relr_bitmap
.u
.elf32
[i
],
1801 /* Sort relative relocations by address. */
1804 elf_x86_relative_reloc_compare (const void *pa
, const void *pb
)
1806 struct elf_x86_relative_reloc_record
*a
=
1807 (struct elf_x86_relative_reloc_record
*) pa
;
1808 struct elf_x86_relative_reloc_record
*b
=
1809 (struct elf_x86_relative_reloc_record
*) pb
;
1810 if (a
->address
< b
->address
)
1812 if (a
->address
> b
->address
)
1817 enum dynobj_sframe_plt_type
1824 /* Create SFrame stack trace info for the plt entries in the .plt section
1825 of type PLT_SEC_TYPE. */
1828 _bfd_x86_elf_create_sframe_plt (bfd
*output_bfd
,
1829 struct bfd_link_info
*info
,
1830 unsigned int plt_sec_type
)
1832 struct elf_x86_link_hash_table
*htab
;
1833 const struct elf_backend_data
*bed
;
1835 unsigned int plt0_entry_size
;
1836 unsigned char func_info
;
1838 /* The dynamic plt section for which .sframe stack trace information is being
1844 sframe_encoder_ctx
**ectx
= NULL
;
1845 unsigned plt_entry_size
= 0;
1846 unsigned int num_pltn_fres
= 0;
1847 unsigned int num_pltn_entries
= 0;
1848 const sframe_frame_row_entry
* const *pltn_fres
;
1850 bed
= get_elf_backend_data (output_bfd
);
1851 htab
= elf_x86_hash_table (info
, bed
->target_id
);
1852 /* Whether SFrame stack trace info for plt0 is to be generated. */
1853 switch (plt_sec_type
)
1857 ectx
= &htab
->plt_cfe_ctx
;
1858 dpltsec
= htab
->elf
.splt
;
1861 = htab
->plt
.has_plt0
? htab
->sframe_plt
->plt0_entry_size
: 0;
1862 plt_entry_size
= htab
->sframe_plt
->pltn_entry_size
;
1863 pltn_fres
= htab
->sframe_plt
->pltn_fres
;
1864 num_pltn_fres
= htab
->sframe_plt
->pltn_num_fres
;
1866 = (dpltsec
->size
- plt0_entry_size
) / plt_entry_size
;
1870 case SFRAME_PLT_SEC
:
1872 ectx
= &htab
->plt_second_cfe_ctx
;
1873 dpltsec
= htab
->plt_second
;
1875 plt0_entry_size
= 0;
1877 plt_entry_size
= htab
->sframe_plt
->sec_pltn_entry_size
;
1878 pltn_fres
= htab
->sframe_plt
->sec_pltn_fres
;
1879 num_pltn_fres
= htab
->sframe_plt
->sec_pltn_num_fres
;
1880 num_pltn_entries
= dpltsec
->size
/ plt_entry_size
;
1884 case SFRAME_PLT_GOT
:
1886 ectx
= &htab
->plt_got_cfe_ctx
;
1887 dpltsec
= htab
->plt_got
;
1889 plt0_entry_size
= 0;
1891 plt_entry_size
= htab
->sframe_plt
->plt_got_entry_size
;
1892 pltn_fres
= htab
->sframe_plt
->plt_got_fres
;
1893 num_pltn_fres
= htab
->sframe_plt
->plt_got_num_fres
;
1894 num_pltn_entries
= dpltsec
->size
/ plt_entry_size
;
1900 /* No other value is possible. */
1905 *ectx
= sframe_encode (SFRAME_VERSION_2
,
1907 SFRAME_ABI_AMD64_ENDIAN_LITTLE
,
1908 SFRAME_CFA_FIXED_FP_INVALID
,
1909 -8, /* Fixed RA offset. */
1912 /* FRE type is dependent on the size of the function. */
1913 fre_type
= sframe_calc_fre_type (dpltsec
->size
);
1914 func_info
= sframe_fde_create_func_info (fre_type
, SFRAME_FDE_TYPE_PCINC
);
1916 /* Add SFrame FDE and the associated FREs for plt0 if plt0 has been
1918 if (plt0_entry_size
)
1920 /* Add SFrame FDE for plt0, the function start address is updated later
1921 at _bfd_elf_merge_section_sframe time. */
1922 sframe_encoder_add_funcdesc_v2 (*ectx
,
1923 0, /* func start addr. */
1928 sframe_frame_row_entry plt0_fre
;
1929 unsigned int num_plt0_fres
= htab
->sframe_plt
->plt0_num_fres
;
1930 for (unsigned int j
= 0; j
< num_plt0_fres
; j
++)
1932 plt0_fre
= *(htab
->sframe_plt
->plt0_fres
[j
]);
1933 sframe_encoder_add_fre (*ectx
, 0, &plt0_fre
);
1938 if (num_pltn_entries
)
1940 /* pltn entries use an SFrame FDE of type
1941 SFRAME_FDE_TYPE_PCMASK to exploit the repetitive
1942 pattern of the instructions in these entries. Using this SFrame FDE
1943 type helps in keeping the SFrame stack trace info for pltn entries
1945 func_info
= sframe_fde_create_func_info (fre_type
,
1946 SFRAME_FDE_TYPE_PCMASK
);
1947 /* Add the SFrame FDE for all PCs starting at the first pltn entry (hence,
1948 function start address = plt0_entry_size. As usual, this will be
1949 updated later at _bfd_elf_merge_section_sframe, by when the
1950 sections are relocated. */
1951 sframe_encoder_add_funcdesc_v2 (*ectx
,
1952 plt0_entry_size
, /* func start addr. */
1953 dpltsec
->size
- plt0_entry_size
,
1958 sframe_frame_row_entry pltn_fre
;
1959 /* Now add the FREs for pltn. Simply adding the FREs suffices due
1960 to the usage of SFRAME_FDE_TYPE_PCMASK above. */
1961 for (unsigned int j
= 0; j
< num_pltn_fres
; j
++)
1963 unsigned int func_idx
= plt0_entry_size
? 1 : 0;
1964 pltn_fre
= *(pltn_fres
[j
]);
1965 sframe_encoder_add_fre (*ectx
, func_idx
, &pltn_fre
);
1972 /* Put contents of the .sframe section corresponding to the specified
1976 _bfd_x86_elf_write_sframe_plt (bfd
*output_bfd
,
1977 struct bfd_link_info
*info
,
1978 unsigned int plt_sec_type
)
1980 struct elf_x86_link_hash_table
*htab
;
1981 const struct elf_backend_data
*bed
;
1982 sframe_encoder_ctx
*ectx
;
1989 bed
= get_elf_backend_data (output_bfd
);
1990 htab
= elf_x86_hash_table (info
, bed
->target_id
);
1991 dynobj
= htab
->elf
.dynobj
;
1993 switch (plt_sec_type
)
1996 ectx
= htab
->plt_cfe_ctx
;
1997 sec
= htab
->plt_sframe
;
1999 case SFRAME_PLT_SEC
:
2000 ectx
= htab
->plt_second_cfe_ctx
;
2001 sec
= htab
->plt_second_sframe
;
2003 case SFRAME_PLT_GOT
:
2004 ectx
= htab
->plt_got_cfe_ctx
;
2005 sec
= htab
->plt_got_sframe
;
2008 /* No other value is possible. */
2015 void *contents
= sframe_encoder_write (ectx
, &sec_size
, &err
);
2017 sec
->size
= (bfd_size_type
) sec_size
;
2018 sec
->contents
= (unsigned char *) bfd_zalloc (dynobj
, sec
->size
);
2019 memcpy (sec
->contents
, contents
, sec_size
);
2021 sframe_encoder_free (&ectx
);
2027 _bfd_elf_x86_size_relative_relocs (struct bfd_link_info
*info
,
2030 struct elf_x86_link_hash_table
*htab
;
2031 const struct elf_backend_data
*bed
;
2033 bfd_size_type i
, count
, unaligned_count
;
2034 asection
*sec
, *srel
;
2036 /* Do nothing for ld -r. */
2037 if (bfd_link_relocatable (info
))
2040 bed
= get_elf_backend_data (info
->output_bfd
);
2041 htab
= elf_x86_hash_table (info
, bed
->target_id
);
2045 count
= htab
->relative_reloc
.count
;
2046 unaligned_count
= htab
->unaligned_relative_reloc
.count
;
2049 if (htab
->generate_relative_reloc_pass
== 0
2050 && htab
->elf
.srelrdyn
!= NULL
)
2052 /* Remove the empty .relr.dyn sections now. */
2053 if (!bfd_is_abs_section (htab
->elf
.srelrdyn
->output_section
))
2055 bfd_section_list_remove
2056 (info
->output_bfd
, htab
->elf
.srelrdyn
->output_section
);
2057 info
->output_bfd
->section_count
--;
2059 bfd_section_list_remove (htab
->elf
.srelrdyn
->owner
,
2060 htab
->elf
.srelrdyn
);
2061 htab
->elf
.srelrdyn
->owner
->section_count
--;
2063 if (unaligned_count
== 0)
2065 htab
->generate_relative_reloc_pass
++;
2070 is_x86_64
= bed
->target_id
== X86_64_ELF_DATA
;
2072 /* Size relative relocations. */
2073 if (htab
->generate_relative_reloc_pass
)
2075 /* Reset the regular relative relocation count. */
2076 for (i
= 0; i
< unaligned_count
; i
++)
2078 sec
= htab
->unaligned_relative_reloc
.data
[i
].sec
;
2079 srel
= elf_section_data (sec
)->sreloc
;
2080 srel
->reloc_count
= 0;
2085 /* Remove the reserved space for compact relative relocations. */
2088 asection
*sgot
= htab
->elf
.sgot
;
2089 asection
*srelgot
= htab
->elf
.srelgot
;
2091 for (i
= 0; i
< count
; i
++)
2093 sec
= htab
->relative_reloc
.data
[i
].sec
;
2097 srel
= elf_section_data (sec
)->sreloc
;
2098 srel
->size
-= htab
->sizeof_reloc
;
2103 /* Size unaligned relative relocations. */
2104 if (unaligned_count
)
2105 elf_x86_size_or_finish_relative_reloc (is_x86_64
, info
, htab
,
2110 elf_x86_size_or_finish_relative_reloc (is_x86_64
, info
, htab
,
2113 /* Sort relative relocations by addresses. We only need to
2114 sort them in the first pass since the relative positions
2116 if (htab
->generate_relative_reloc_pass
== 0)
2117 qsort (htab
->relative_reloc
.data
, count
,
2118 sizeof (struct elf_x86_relative_reloc_record
),
2119 elf_x86_relative_reloc_compare
);
2121 elf_x86_compute_dl_relr_bitmap (info
, htab
, need_layout
);
2124 htab
->generate_relative_reloc_pass
++;
2130 _bfd_elf_x86_finish_relative_relocs (struct bfd_link_info
*info
)
2132 struct elf_x86_link_hash_table
*htab
;
2133 const struct elf_backend_data
*bed
;
2134 Elf_Internal_Rela outrel
;
2136 bfd_size_type count
;
2138 /* Do nothing for ld -r. */
2139 if (bfd_link_relocatable (info
))
2142 bed
= get_elf_backend_data (info
->output_bfd
);
2143 htab
= elf_x86_hash_table (info
, bed
->target_id
);
2147 is_x86_64
= bed
->target_id
== X86_64_ELF_DATA
;
2149 outrel
.r_info
= htab
->r_info (0, htab
->relative_r_type
);
2151 if (htab
->unaligned_relative_reloc
.count
)
2152 elf_x86_size_or_finish_relative_reloc (is_x86_64
, info
, htab
,
2155 count
= htab
->relative_reloc
.count
;
2158 elf_x86_size_or_finish_relative_reloc (is_x86_64
, info
, htab
,
2161 elf_x86_compute_dl_relr_bitmap (info
, htab
, NULL
);
2163 elf_x86_write_dl_relr_bitmap (info
, htab
);
2170 _bfd_elf_x86_valid_reloc_p (asection
*input_section
,
2171 struct bfd_link_info
*info
,
2172 struct elf_x86_link_hash_table
*htab
,
2173 const Elf_Internal_Rela
*rel
,
2174 struct elf_link_hash_entry
*h
,
2175 Elf_Internal_Sym
*sym
,
2176 Elf_Internal_Shdr
*symtab_hdr
,
2177 bool *no_dynreloc_p
)
2179 bool valid_p
= true;
2181 *no_dynreloc_p
= false;
2183 /* Check If relocation against non-preemptible absolute symbol is
2184 valid in PIC. FIXME: Can't use SYMBOL_REFERENCES_LOCAL_P since
2185 it may call _bfd_elf_link_hide_sym_by_version and result in
2186 ld-elfvers/ vers21 test failure. */
2187 if (bfd_link_pic (info
)
2188 && (h
== NULL
|| SYMBOL_REFERENCES_LOCAL (info
, h
)))
2190 const struct elf_backend_data
*bed
;
2191 unsigned int r_type
;
2192 Elf_Internal_Rela irel
;
2194 /* Skip non-absolute symbol. */
2197 if (!ABS_SYMBOL_P (h
))
2200 else if (sym
->st_shndx
!= SHN_ABS
)
2203 bed
= get_elf_backend_data (input_section
->owner
);
2204 r_type
= ELF32_R_TYPE (rel
->r_info
);
2207 /* Only allow relocations against absolute symbol, which can be
2208 resolved as absolute value + addend. GOTPCREL and GOT32
2209 relocations are allowed since absolute value + addend is
2210 stored in the GOT slot. */
2211 if (bed
->target_id
== X86_64_ELF_DATA
)
2213 r_type
&= ~R_X86_64_converted_reloc_bit
;
2214 valid_p
= (r_type
== R_X86_64_64
2215 || r_type
== R_X86_64_32
2216 || r_type
== R_X86_64_32S
2217 || r_type
== R_X86_64_16
2218 || r_type
== R_X86_64_8
2219 || r_type
== R_X86_64_GOTPCREL
2220 || r_type
== R_X86_64_GOTPCRELX
2221 || r_type
== R_X86_64_REX_GOTPCRELX
);
2224 unsigned int r_symndx
= htab
->r_sym (rel
->r_info
);
2225 irel
.r_info
= htab
->r_info (r_symndx
, r_type
);
2229 valid_p
= (r_type
== R_386_32
2230 || r_type
== R_386_16
2231 || r_type
== R_386_8
2232 || r_type
== R_386_GOT32
2233 || r_type
== R_386_GOT32X
);
2236 *no_dynreloc_p
= true;
2240 arelent internal_reloc
;
2242 if (!bed
->elf_info_to_howto (input_section
->owner
,
2243 &internal_reloc
, &irel
)
2244 || internal_reloc
.howto
== NULL
)
2248 name
= h
->root
.root
.string
;
2250 name
= bfd_elf_sym_name (input_section
->owner
, symtab_hdr
,
2252 info
->callbacks
->einfo
2253 /* xgettext:c-format */
2254 (_("%F%P: %pB: relocation %s against absolute symbol "
2255 "`%s' in section `%pA' is disallowed\n"),
2256 input_section
->owner
, internal_reloc
.howto
->name
, name
,
2258 bfd_set_error (bfd_error_bad_value
);
2265 /* Set the sizes of the dynamic sections. */
2268 _bfd_x86_elf_late_size_sections (bfd
*output_bfd
,
2269 struct bfd_link_info
*info
)
2271 struct elf_x86_link_hash_table
*htab
;
2276 const struct elf_backend_data
*bed
2277 = get_elf_backend_data (output_bfd
);
2279 htab
= elf_x86_hash_table (info
, bed
->target_id
);
2282 dynobj
= htab
->elf
.dynobj
;
2286 /* Set up .got offsets for local syms, and space for local dynamic
2288 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
2290 bfd_signed_vma
*local_got
;
2291 bfd_signed_vma
*end_local_got
;
2292 char *local_tls_type
;
2293 bfd_vma
*local_tlsdesc_gotent
;
2294 bfd_size_type locsymcount
;
2295 Elf_Internal_Shdr
*symtab_hdr
;
2298 if (! is_x86_elf (ibfd
, htab
))
2301 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2303 struct elf_dyn_relocs
*p
;
2305 for (p
= ((struct elf_dyn_relocs
*)
2306 elf_section_data (s
)->local_dynrel
);
2310 if (!bfd_is_abs_section (p
->sec
)
2311 && bfd_is_abs_section (p
->sec
->output_section
))
2313 /* Input section has been discarded, either because
2314 it is a copy of a linkonce section or due to
2315 linker script /DISCARD/, so we'll be discarding
2318 else if (htab
->elf
.target_os
== is_vxworks
2319 && strcmp (p
->sec
->output_section
->name
,
2322 /* Relocations in vxworks .tls_vars sections are
2323 handled specially by the loader. */
2325 else if (p
->count
!= 0)
2327 srel
= elf_section_data (p
->sec
)->sreloc
;
2328 srel
->size
+= p
->count
* htab
->sizeof_reloc
;
2329 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0
2330 && (info
->flags
& DF_TEXTREL
) == 0)
2332 info
->flags
|= DF_TEXTREL
;
2333 if (bfd_link_textrel_check (info
))
2334 /* xgettext:c-format */
2335 info
->callbacks
->einfo
2336 (_("%P: %pB: warning: relocation "
2337 "in read-only section `%pA'\n"),
2338 p
->sec
->owner
, p
->sec
);
2344 local_got
= elf_local_got_refcounts (ibfd
);
2348 symtab_hdr
= &elf_symtab_hdr (ibfd
);
2349 locsymcount
= symtab_hdr
->sh_info
;
2350 end_local_got
= local_got
+ locsymcount
;
2351 local_tls_type
= elf_x86_local_got_tls_type (ibfd
);
2352 local_tlsdesc_gotent
= elf_x86_local_tlsdesc_gotent (ibfd
);
2354 srel
= htab
->elf
.srelgot
;
2355 for (; local_got
< end_local_got
;
2356 ++local_got
, ++local_tls_type
, ++local_tlsdesc_gotent
)
2358 *local_tlsdesc_gotent
= (bfd_vma
) -1;
2361 if (GOT_TLS_GDESC_P (*local_tls_type
))
2363 *local_tlsdesc_gotent
= htab
->elf
.sgotplt
->size
2364 - elf_x86_compute_jump_table_size (htab
);
2365 htab
->elf
.sgotplt
->size
+= 2 * htab
->got_entry_size
;
2366 *local_got
= (bfd_vma
) -2;
2368 if (! GOT_TLS_GDESC_P (*local_tls_type
)
2369 || GOT_TLS_GD_P (*local_tls_type
))
2371 *local_got
= s
->size
;
2372 s
->size
+= htab
->got_entry_size
;
2373 if (GOT_TLS_GD_P (*local_tls_type
)
2374 || *local_tls_type
== GOT_TLS_IE_BOTH
)
2375 s
->size
+= htab
->got_entry_size
;
2377 if ((bfd_link_pic (info
) && *local_tls_type
!= GOT_ABS
)
2378 || GOT_TLS_GD_ANY_P (*local_tls_type
)
2379 || (*local_tls_type
& GOT_TLS_IE
))
2381 if (*local_tls_type
== GOT_TLS_IE_BOTH
)
2382 srel
->size
+= 2 * htab
->sizeof_reloc
;
2383 else if (GOT_TLS_GD_P (*local_tls_type
)
2384 || ! GOT_TLS_GDESC_P (*local_tls_type
))
2385 srel
->size
+= htab
->sizeof_reloc
;
2386 if (GOT_TLS_GDESC_P (*local_tls_type
))
2388 htab
->elf
.srelplt
->size
+= htab
->sizeof_reloc
;
2389 if (bed
->target_id
== X86_64_ELF_DATA
)
2390 htab
->elf
.tlsdesc_plt
= (bfd_vma
) -1;
2395 *local_got
= (bfd_vma
) -1;
2399 if (htab
->tls_ld_or_ldm_got
.refcount
> 0)
2401 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2402 or R_X86_64_TLSLD relocs. */
2403 htab
->tls_ld_or_ldm_got
.offset
= htab
->elf
.sgot
->size
;
2404 htab
->elf
.sgot
->size
+= 2 * htab
->got_entry_size
;
2405 htab
->elf
.srelgot
->size
+= htab
->sizeof_reloc
;
2408 htab
->tls_ld_or_ldm_got
.offset
= -1;
2410 /* Allocate global sym .plt and .got entries, and space for global
2411 sym dynamic relocs. */
2412 elf_link_hash_traverse (&htab
->elf
, elf_x86_allocate_dynrelocs
,
2415 /* Allocate .plt and .got entries, and space for local symbols. */
2416 htab_traverse (htab
->loc_hash_table
, elf_x86_allocate_local_dynreloc
,
2419 /* For every jump slot reserved in the sgotplt, reloc_count is
2420 incremented. However, when we reserve space for TLS descriptors,
2421 it's not incremented, so in order to compute the space reserved
2422 for them, it suffices to multiply the reloc count by the jump
2425 PR ld/13302: We start next_irelative_index at the end of .rela.plt
2426 so that R_{386,X86_64}_IRELATIVE entries come last. */
2427 if (htab
->elf
.srelplt
)
2429 htab
->next_tls_desc_index
= htab
->elf
.srelplt
->reloc_count
;
2430 htab
->sgotplt_jump_table_size
2431 = elf_x86_compute_jump_table_size (htab
);
2432 htab
->next_irelative_index
= htab
->elf
.srelplt
->reloc_count
- 1;
2434 else if (htab
->elf
.irelplt
)
2435 htab
->next_irelative_index
= htab
->elf
.irelplt
->reloc_count
- 1;
2437 if (htab
->elf
.tlsdesc_plt
)
2439 /* NB: tlsdesc_plt is set only for x86-64. If we're not using
2440 lazy TLS relocations, don't generate the PLT and GOT entries
2442 if ((info
->flags
& DF_BIND_NOW
))
2443 htab
->elf
.tlsdesc_plt
= 0;
2446 htab
->elf
.tlsdesc_got
= htab
->elf
.sgot
->size
;
2447 htab
->elf
.sgot
->size
+= htab
->got_entry_size
;
2448 /* Reserve room for the initial entry.
2449 FIXME: we could probably do away with it in this case. */
2450 if (htab
->elf
.splt
->size
== 0)
2451 htab
->elf
.splt
->size
= htab
->plt
.plt_entry_size
;
2452 htab
->elf
.tlsdesc_plt
= htab
->elf
.splt
->size
;
2453 htab
->elf
.splt
->size
+= htab
->plt
.plt_entry_size
;
2457 if (htab
->elf
.sgotplt
)
2459 /* Don't allocate .got.plt section if there are no GOT nor PLT
2460 entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
2461 if ((htab
->elf
.hgot
== NULL
2462 || !htab
->got_referenced
)
2463 && (htab
->elf
.sgotplt
->size
== bed
->got_header_size
)
2464 && (htab
->elf
.splt
== NULL
2465 || htab
->elf
.splt
->size
== 0)
2466 && (htab
->elf
.sgot
== NULL
2467 || htab
->elf
.sgot
->size
== 0)
2468 && (htab
->elf
.iplt
== NULL
2469 || htab
->elf
.iplt
->size
== 0)
2470 && (htab
->elf
.igotplt
== NULL
2471 || htab
->elf
.igotplt
->size
== 0))
2473 htab
->elf
.sgotplt
->size
= 0;
2474 /* Solaris requires to keep _GLOBAL_OFFSET_TABLE_ even if it
2476 if (htab
->elf
.hgot
!= NULL
2477 && htab
->elf
.target_os
!= is_solaris
)
2479 /* Remove the unused _GLOBAL_OFFSET_TABLE_ from symbol
2481 htab
->elf
.hgot
->root
.type
= bfd_link_hash_undefined
;
2482 htab
->elf
.hgot
->root
.u
.undef
.abfd
2483 = htab
->elf
.hgot
->root
.u
.def
.section
->owner
;
2484 htab
->elf
.hgot
->root
.linker_def
= 0;
2485 htab
->elf
.hgot
->ref_regular
= 0;
2486 htab
->elf
.hgot
->def_regular
= 0;
2491 if (_bfd_elf_eh_frame_present (info
))
2493 if (htab
->plt_eh_frame
!= NULL
2494 && htab
->elf
.splt
!= NULL
2495 && htab
->elf
.splt
->size
!= 0
2496 && !bfd_is_abs_section (htab
->elf
.splt
->output_section
))
2497 htab
->plt_eh_frame
->size
= htab
->plt
.eh_frame_plt_size
;
2499 if (htab
->plt_got_eh_frame
!= NULL
2500 && htab
->plt_got
!= NULL
2501 && htab
->plt_got
->size
!= 0
2502 && !bfd_is_abs_section (htab
->plt_got
->output_section
))
2503 htab
->plt_got_eh_frame
->size
2504 = htab
->non_lazy_plt
->eh_frame_plt_size
;
2506 /* Unwind info for the second PLT and .plt.got sections are
2508 if (htab
->plt_second_eh_frame
!= NULL
2509 && htab
->plt_second
!= NULL
2510 && htab
->plt_second
->size
!= 0
2511 && !bfd_is_abs_section (htab
->plt_second
->output_section
))
2512 htab
->plt_second_eh_frame
->size
2513 = htab
->non_lazy_plt
->eh_frame_plt_size
;
2516 /* No need to size the .sframe section explicitly because the write-out
2517 mechanism is different. Simply prep up the FDE/FRE for the
2519 if (_bfd_elf_sframe_present (info
))
2521 if (htab
->plt_sframe
!= NULL
2522 && htab
->elf
.splt
!= NULL
2523 && htab
->elf
.splt
->size
!= 0
2524 && !bfd_is_abs_section (htab
->elf
.splt
->output_section
))
2526 _bfd_x86_elf_create_sframe_plt (output_bfd
, info
, SFRAME_PLT
);
2527 /* FIXME - Dirty Hack. Set the size to something non-zero for now,
2528 so that the section does not get stripped out below. The precise
2529 size of this section is known only when the contents are
2530 serialized in _bfd_x86_elf_write_sframe_plt. */
2531 htab
->plt_sframe
->size
= sizeof (sframe_header
) + 1;
2534 if (htab
->plt_got_sframe
!= NULL
2535 && htab
->plt_got
!= NULL
2536 && htab
->plt_got
->size
!= 0
2537 && !bfd_is_abs_section (htab
->plt_got
->output_section
))
2539 _bfd_x86_elf_create_sframe_plt (output_bfd
, info
, SFRAME_PLT_GOT
);
2540 /* FIXME - Dirty Hack. Set the size to something non-zero for now,
2541 so that the section does not get stripped out below. The precise
2542 size of this section is known only when the contents are
2543 serialized in _bfd_x86_elf_write_sframe_plt. */
2544 htab
->plt_got_sframe
->size
= sizeof (sframe_header
) + 1;
2547 if (htab
->plt_second_sframe
!= NULL
2548 && htab
->plt_second
!= NULL
2549 && htab
->plt_second
->size
!= 0
2550 && !bfd_is_abs_section (htab
->plt_second
->output_section
))
2552 /* SFrame stack trace info for the second PLT. */
2553 _bfd_x86_elf_create_sframe_plt (output_bfd
, info
, SFRAME_PLT_SEC
);
2554 /* FIXME - Dirty Hack. Set the size to something non-zero for now,
2555 so that the section does not get stripped out below. The precise
2556 size of this section is known only when the contents are
2557 serialized in _bfd_x86_elf_write_sframe_plt. */
2558 htab
->plt_second_sframe
->size
= sizeof (sframe_header
) + 1;
2562 asection
*resolved_plt
= NULL
;
2564 if (htab
->params
->mark_plt
&& htab
->elf
.dynamic_sections_created
)
2566 if (htab
->plt_second
!= NULL
)
2567 resolved_plt
= htab
->plt_second
;
2569 resolved_plt
= htab
->elf
.splt
;
2571 if (resolved_plt
!= NULL
&& resolved_plt
->size
== 0)
2572 resolved_plt
= NULL
;
2575 /* We now have determined the sizes of the various dynamic sections.
2576 Allocate memory for them. */
2578 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
2580 bool strip_section
= true;
2582 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
2585 /* The .relr.dyn section for compact relative relocation will
2587 if (s
== htab
->elf
.srelrdyn
)
2590 if (s
== htab
->elf
.splt
2591 || s
== htab
->elf
.sgot
)
2593 /* Strip this section if we don't need it; see the
2595 /* We'd like to strip these sections if they aren't needed, but if
2596 we've exported dynamic symbols from them we must leave them.
2597 It's too late to tell BFD to get rid of the symbols. */
2599 if (htab
->elf
.hplt
!= NULL
)
2600 strip_section
= false;
2602 else if (s
== htab
->elf
.sgotplt
2603 || s
== htab
->elf
.iplt
2604 || s
== htab
->elf
.igotplt
2605 || s
== htab
->plt_second
2606 || s
== htab
->plt_got
2607 || s
== htab
->plt_eh_frame
2608 || s
== htab
->plt_got_eh_frame
2609 || s
== htab
->plt_second_eh_frame
2610 || s
== htab
->plt_sframe
2611 || s
== htab
->plt_second_sframe
2612 || s
== htab
->plt_got_sframe
2613 || s
== htab
->elf
.sdynbss
2614 || s
== htab
->elf
.sdynrelro
)
2616 /* Strip these too. */
2618 else if (htab
->is_reloc_section (bfd_section_name (s
)))
2621 && s
!= htab
->elf
.srelplt
2622 && s
!= htab
->srelplt2
)
2625 /* We use the reloc_count field as a counter if we need
2626 to copy relocs into the output file. */
2627 if (s
!= htab
->elf
.srelplt
)
2632 /* It's not one of our sections, so don't allocate space. */
2638 /* If we don't need this section, strip it from the
2639 output file. This is mostly to handle .rel.bss and
2640 .rel.plt. We must create both sections in
2641 create_dynamic_sections, because they must be created
2642 before the linker maps input sections to output
2643 sections. The linker does that before
2644 adjust_dynamic_symbol is called, and it is that
2645 function which decides whether anything needs to go
2646 into these sections. */
2648 s
->flags
|= SEC_EXCLUDE
;
2652 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
2655 /* Skip allocating contents for .sframe section as it is written
2656 out differently. See below. */
2657 if ((s
== htab
->plt_sframe
) || (s
== htab
->plt_second_sframe
)
2658 || (s
== htab
->plt_got_sframe
))
2661 /* NB: Initially, the iplt section has minimal alignment to
2662 avoid moving dot of the following section backwards when
2663 it is empty. Update its section alignment now since it
2665 if (s
== htab
->elf
.iplt
)
2666 bfd_set_section_alignment (s
, htab
->plt
.iplt_alignment
);
2668 /* Allocate memory for the section contents. We use bfd_zalloc
2669 here in case unused entries are not reclaimed before the
2670 section's contents are written out. This should not happen,
2671 but this way if it does, we get a R_386_NONE or R_X86_64_NONE
2672 reloc instead of garbage. */
2673 s
->contents
= (unsigned char *) bfd_zalloc (dynobj
, s
->size
);
2674 if (s
->contents
== NULL
)
2678 if (htab
->plt_eh_frame
!= NULL
2679 && htab
->plt_eh_frame
->contents
!= NULL
)
2681 memcpy (htab
->plt_eh_frame
->contents
,
2682 htab
->plt
.eh_frame_plt
,
2683 htab
->plt_eh_frame
->size
);
2684 bfd_put_32 (dynobj
, htab
->elf
.splt
->size
,
2685 htab
->plt_eh_frame
->contents
+ PLT_FDE_LEN_OFFSET
);
2688 if (htab
->plt_got_eh_frame
!= NULL
2689 && htab
->plt_got_eh_frame
->contents
!= NULL
)
2691 memcpy (htab
->plt_got_eh_frame
->contents
,
2692 htab
->non_lazy_plt
->eh_frame_plt
,
2693 htab
->plt_got_eh_frame
->size
);
2694 bfd_put_32 (dynobj
, htab
->plt_got
->size
,
2695 (htab
->plt_got_eh_frame
->contents
2696 + PLT_FDE_LEN_OFFSET
));
2699 if (htab
->plt_second_eh_frame
!= NULL
2700 && htab
->plt_second_eh_frame
->contents
!= NULL
)
2702 memcpy (htab
->plt_second_eh_frame
->contents
,
2703 htab
->non_lazy_plt
->eh_frame_plt
,
2704 htab
->plt_second_eh_frame
->size
);
2705 bfd_put_32 (dynobj
, htab
->plt_second
->size
,
2706 (htab
->plt_second_eh_frame
->contents
2707 + PLT_FDE_LEN_OFFSET
));
2710 if (_bfd_elf_sframe_present (info
))
2712 if (htab
->plt_sframe
!= NULL
2713 && htab
->elf
.splt
!= NULL
2714 && htab
->elf
.splt
->size
!= 0
2715 && htab
->plt_sframe
->contents
== NULL
)
2716 _bfd_x86_elf_write_sframe_plt (output_bfd
, info
, SFRAME_PLT
);
2718 if (htab
->plt_second_sframe
!= NULL
2719 && htab
->plt_second
!= NULL
2720 && htab
->plt_second
->size
!= 0
2721 && htab
->plt_second_sframe
->contents
== NULL
)
2722 _bfd_x86_elf_write_sframe_plt (output_bfd
, info
, SFRAME_PLT_SEC
);
2724 if (htab
->plt_got_sframe
!= NULL
2725 && htab
->plt_got
!= NULL
2726 && htab
->plt_got
->size
!= 0
2727 && htab
->plt_got_sframe
->contents
== NULL
)
2728 _bfd_x86_elf_write_sframe_plt (output_bfd
, info
, SFRAME_PLT_GOT
);
2731 if (resolved_plt
!= NULL
2732 && (!_bfd_elf_add_dynamic_entry (info
, DT_X86_64_PLT
, 0)
2733 || !_bfd_elf_add_dynamic_entry (info
, DT_X86_64_PLTSZ
, 0)
2734 || !_bfd_elf_add_dynamic_entry (info
, DT_X86_64_PLTENT
, 0)))
2737 return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd
, info
,
2741 /* Finish up the x86 dynamic sections. */
2743 struct elf_x86_link_hash_table
*
2744 _bfd_x86_elf_finish_dynamic_sections (bfd
*output_bfd
,
2745 struct bfd_link_info
*info
)
2747 struct elf_x86_link_hash_table
*htab
;
2748 const struct elf_backend_data
*bed
;
2751 bfd_byte
*dyncon
, *dynconend
;
2752 bfd_size_type sizeof_dyn
;
2754 bed
= get_elf_backend_data (output_bfd
);
2755 htab
= elf_x86_hash_table (info
, bed
->target_id
);
2759 dynobj
= htab
->elf
.dynobj
;
2760 sdyn
= htab
->elf
.dynamic
;
2762 /* GOT is always created in setup_gnu_properties. But it may not be
2763 needed. .got.plt section may be needed for static IFUNC. */
2764 if (htab
->elf
.sgotplt
&& htab
->elf
.sgotplt
->size
> 0)
2766 bfd_vma dynamic_addr
;
2768 if (bfd_is_abs_section (htab
->elf
.sgotplt
->output_section
))
2771 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
2775 elf_section_data (htab
->elf
.sgotplt
->output_section
)->this_hdr
.sh_entsize
2776 = htab
->got_entry_size
;
2778 dynamic_addr
= (sdyn
== NULL
2780 : sdyn
->output_section
->vma
+ sdyn
->output_offset
);
2782 /* Set the first entry in the global offset table to the address
2783 of the dynamic section. Write GOT[1] and GOT[2], needed for
2784 the dynamic linker. */
2785 if (htab
->got_entry_size
== 8)
2787 bfd_put_64 (output_bfd
, dynamic_addr
,
2788 htab
->elf
.sgotplt
->contents
);
2789 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
2790 htab
->elf
.sgotplt
->contents
+ 8);
2791 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
2792 htab
->elf
.sgotplt
->contents
+ 8*2);
2796 bfd_put_32 (output_bfd
, dynamic_addr
,
2797 htab
->elf
.sgotplt
->contents
);
2798 bfd_put_32 (output_bfd
, 0,
2799 htab
->elf
.sgotplt
->contents
+ 4);
2800 bfd_put_32 (output_bfd
, 0,
2801 htab
->elf
.sgotplt
->contents
+ 4*2);
2805 if (!htab
->elf
.dynamic_sections_created
)
2808 if (sdyn
== NULL
|| htab
->elf
.sgot
== NULL
)
2811 asection
*resolved_plt
;
2812 if (htab
->plt_second
!= NULL
)
2813 resolved_plt
= htab
->plt_second
;
2815 resolved_plt
= htab
->elf
.splt
;
2817 sizeof_dyn
= bed
->s
->sizeof_dyn
;
2818 dyncon
= sdyn
->contents
;
2819 dynconend
= sdyn
->contents
+ sdyn
->size
;
2820 for (; dyncon
< dynconend
; dyncon
+= sizeof_dyn
)
2822 Elf_Internal_Dyn dyn
;
2825 (*bed
->s
->swap_dyn_in
) (dynobj
, dyncon
, &dyn
);
2830 if (htab
->elf
.target_os
== is_vxworks
2831 && elf_vxworks_finish_dynamic_entry (output_bfd
, &dyn
))
2836 s
= htab
->elf
.sgotplt
;
2837 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2841 s
= htab
->elf
.srelplt
;
2842 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2846 s
= htab
->elf
.srelplt
;
2847 dyn
.d_un
.d_val
= s
->size
;
2850 case DT_TLSDESC_PLT
:
2852 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
2853 + htab
->elf
.tlsdesc_plt
;
2856 case DT_TLSDESC_GOT
:
2858 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
2859 + htab
->elf
.tlsdesc_got
;
2863 s
= resolved_plt
->output_section
;
2864 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2867 case DT_X86_64_PLTSZ
:
2868 dyn
.d_un
.d_val
= resolved_plt
->size
;
2871 case DT_X86_64_PLTENT
:
2872 dyn
.d_un
.d_ptr
= htab
->plt
.plt_entry_size
;
2876 (*bed
->s
->swap_dyn_out
) (output_bfd
, &dyn
, dyncon
);
2879 if (htab
->plt_got
!= NULL
&& htab
->plt_got
->size
> 0)
2880 elf_section_data (htab
->plt_got
->output_section
)
2881 ->this_hdr
.sh_entsize
= htab
->non_lazy_plt
->plt_entry_size
;
2883 if (htab
->plt_second
!= NULL
&& htab
->plt_second
->size
> 0)
2884 elf_section_data (htab
->plt_second
->output_section
)
2885 ->this_hdr
.sh_entsize
= htab
->non_lazy_plt
->plt_entry_size
;
2887 /* Adjust .eh_frame for .plt section. */
2888 if (htab
->plt_eh_frame
!= NULL
2889 && htab
->plt_eh_frame
->contents
!= NULL
)
2891 if (htab
->elf
.splt
!= NULL
2892 && htab
->elf
.splt
->size
!= 0
2893 && (htab
->elf
.splt
->flags
& SEC_EXCLUDE
) == 0
2894 && htab
->elf
.splt
->output_section
!= NULL
2895 && htab
->plt_eh_frame
->output_section
!= NULL
)
2897 bfd_vma plt_start
= htab
->elf
.splt
->output_section
->vma
;
2898 bfd_vma eh_frame_start
= htab
->plt_eh_frame
->output_section
->vma
2899 + htab
->plt_eh_frame
->output_offset
2900 + PLT_FDE_START_OFFSET
;
2901 bfd_put_signed_32 (dynobj
, plt_start
- eh_frame_start
,
2902 htab
->plt_eh_frame
->contents
2903 + PLT_FDE_START_OFFSET
);
2906 if (htab
->plt_eh_frame
->sec_info_type
== SEC_INFO_TYPE_EH_FRAME
)
2908 if (! _bfd_elf_write_section_eh_frame (output_bfd
, info
,
2910 htab
->plt_eh_frame
->contents
))
2915 /* Adjust .eh_frame for .plt.got section. */
2916 if (htab
->plt_got_eh_frame
!= NULL
2917 && htab
->plt_got_eh_frame
->contents
!= NULL
)
2919 if (htab
->plt_got
!= NULL
2920 && htab
->plt_got
->size
!= 0
2921 && (htab
->plt_got
->flags
& SEC_EXCLUDE
) == 0
2922 && htab
->plt_got
->output_section
!= NULL
2923 && htab
->plt_got_eh_frame
->output_section
!= NULL
)
2925 bfd_vma plt_start
= htab
->plt_got
->output_section
->vma
;
2926 bfd_vma eh_frame_start
= htab
->plt_got_eh_frame
->output_section
->vma
2927 + htab
->plt_got_eh_frame
->output_offset
2928 + PLT_FDE_START_OFFSET
;
2929 bfd_put_signed_32 (dynobj
, plt_start
- eh_frame_start
,
2930 htab
->plt_got_eh_frame
->contents
2931 + PLT_FDE_START_OFFSET
);
2933 if (htab
->plt_got_eh_frame
->sec_info_type
== SEC_INFO_TYPE_EH_FRAME
)
2935 if (! _bfd_elf_write_section_eh_frame (output_bfd
, info
,
2936 htab
->plt_got_eh_frame
,
2937 htab
->plt_got_eh_frame
->contents
))
2942 /* Adjust .eh_frame for the second PLT section. */
2943 if (htab
->plt_second_eh_frame
!= NULL
2944 && htab
->plt_second_eh_frame
->contents
!= NULL
)
2946 if (htab
->plt_second
!= NULL
2947 && htab
->plt_second
->size
!= 0
2948 && (htab
->plt_second
->flags
& SEC_EXCLUDE
) == 0
2949 && htab
->plt_second
->output_section
!= NULL
2950 && htab
->plt_second_eh_frame
->output_section
!= NULL
)
2952 bfd_vma plt_start
= htab
->plt_second
->output_section
->vma
;
2953 bfd_vma eh_frame_start
2954 = (htab
->plt_second_eh_frame
->output_section
->vma
2955 + htab
->plt_second_eh_frame
->output_offset
2956 + PLT_FDE_START_OFFSET
);
2957 bfd_put_signed_32 (dynobj
, plt_start
- eh_frame_start
,
2958 htab
->plt_second_eh_frame
->contents
2959 + PLT_FDE_START_OFFSET
);
2961 if (htab
->plt_second_eh_frame
->sec_info_type
2962 == SEC_INFO_TYPE_EH_FRAME
)
2964 if (! _bfd_elf_write_section_eh_frame (output_bfd
, info
,
2965 htab
->plt_second_eh_frame
,
2966 htab
->plt_second_eh_frame
->contents
))
2971 /* Make any adjustment if necessary and merge .sframe section to
2972 create the final .sframe section for output_bfd. */
2973 if (htab
->plt_sframe
!= NULL
2974 && htab
->plt_sframe
->contents
!= NULL
)
2976 if (htab
->elf
.splt
!= NULL
2977 && htab
->elf
.splt
->size
!= 0
2978 && (htab
->elf
.splt
->flags
& SEC_EXCLUDE
) == 0
2979 && htab
->elf
.splt
->output_section
!= NULL
2980 && htab
->plt_sframe
->output_section
!= NULL
)
2982 bfd_vma plt_start
= htab
->elf
.splt
->output_section
->vma
;
2983 bfd_vma sframe_start
= htab
->plt_sframe
->output_section
->vma
2984 + htab
->plt_sframe
->output_offset
2985 + PLT_SFRAME_FDE_START_OFFSET
;
2986 #if 0 /* FIXME Testing only. Remove before review. */
2987 bfd_vma test_value
= (plt_start
- sframe_start
)
2988 + htab
->plt_sframe
->output_section
->vma
2989 + htab
->plt_sframe
->output_offset
2990 + PLT_SFRAME_FDE_START_OFFSET
;
2991 bfd_put_signed_32 (dynobj
, test_value
,
2993 bfd_put_signed_32 (dynobj
, plt_start
- sframe_start
,
2994 htab
->plt_sframe
->contents
2995 + PLT_SFRAME_FDE_START_OFFSET
);
2997 if (htab
->plt_sframe
->sec_info_type
== SEC_INFO_TYPE_SFRAME
)
2999 if (! _bfd_elf_merge_section_sframe (output_bfd
, info
,
3001 htab
->plt_sframe
->contents
))
3006 if (htab
->plt_second_sframe
!= NULL
3007 && htab
->plt_second_sframe
->contents
!= NULL
)
3009 if (htab
->plt_second
!= NULL
3010 && htab
->plt_second
->size
!= 0
3011 && (htab
->plt_second
->flags
& SEC_EXCLUDE
) == 0
3012 && htab
->plt_second
->output_section
!= NULL
3013 && htab
->plt_second_sframe
->output_section
!= NULL
)
3015 bfd_vma plt_start
= htab
->plt_second
->output_section
->vma
;
3016 bfd_vma sframe_start
3017 = (htab
->plt_second_sframe
->output_section
->vma
3018 + htab
->plt_second_sframe
->output_offset
3019 + PLT_SFRAME_FDE_START_OFFSET
);
3020 #if 0 /* FIXME Testing only. Remove before review. */
3021 bfd_vma test_value
= (plt_start
- sframe_start
)
3022 + htab
->plt_second_sframe
->output_section
->vma
3023 + htab
->plt_second_sframe
->output_offset
3024 + PLT_SFRAME_FDE_START_OFFSET
;
3025 bfd_put_signed_32 (dynobj
, test_value
,
3027 bfd_put_signed_32 (dynobj
, plt_start
- sframe_start
,
3028 htab
->plt_second_sframe
->contents
3029 + PLT_SFRAME_FDE_START_OFFSET
);
3031 if (htab
->plt_second_sframe
->sec_info_type
== SEC_INFO_TYPE_SFRAME
)
3033 if (! _bfd_elf_merge_section_sframe (output_bfd
, info
,
3034 htab
->plt_second_sframe
,
3035 htab
->plt_second_sframe
->contents
))
3040 if (htab
->plt_got_sframe
!= NULL
3041 && htab
->plt_got_sframe
->contents
!= NULL
)
3043 if (htab
->plt_got
!= NULL
3044 && htab
->plt_got
->size
!= 0
3045 && (htab
->plt_got
->flags
& SEC_EXCLUDE
) == 0
3046 && htab
->plt_got
->output_section
!= NULL
3047 && htab
->plt_got_sframe
->output_section
!= NULL
)
3049 bfd_vma plt_start
= htab
->plt_got
->output_section
->vma
;
3050 bfd_vma sframe_start
3051 = (htab
->plt_got_sframe
->output_section
->vma
3052 + htab
->plt_got_sframe
->output_offset
3053 + PLT_SFRAME_FDE_START_OFFSET
);
3054 bfd_put_signed_32 (dynobj
, plt_start
- sframe_start
,
3055 htab
->plt_got_sframe
->contents
3056 + PLT_SFRAME_FDE_START_OFFSET
);
3058 if (htab
->plt_got_sframe
->sec_info_type
== SEC_INFO_TYPE_SFRAME
)
3060 if (! _bfd_elf_merge_section_sframe (output_bfd
, info
,
3061 htab
->plt_got_sframe
,
3062 htab
->plt_got_sframe
->contents
))
3067 if (htab
->elf
.sgot
&& htab
->elf
.sgot
->size
> 0)
3068 elf_section_data (htab
->elf
.sgot
->output_section
)->this_hdr
.sh_entsize
3069 = htab
->got_entry_size
;
3076 _bfd_x86_elf_early_size_sections (bfd
*output_bfd
,
3077 struct bfd_link_info
*info
)
3079 asection
*tls_sec
= elf_hash_table (info
)->tls_sec
;
3081 if (tls_sec
&& !bfd_link_relocatable (info
))
3083 struct elf_link_hash_entry
*tlsbase
;
3085 tlsbase
= elf_link_hash_lookup (elf_hash_table (info
),
3086 "_TLS_MODULE_BASE_",
3087 false, false, false);
3089 if (tlsbase
&& tlsbase
->type
== STT_TLS
)
3091 struct elf_x86_link_hash_table
*htab
;
3092 struct bfd_link_hash_entry
*bh
= NULL
;
3093 const struct elf_backend_data
*bed
3094 = get_elf_backend_data (output_bfd
);
3096 htab
= elf_x86_hash_table (info
, bed
->target_id
);
3100 if (!(_bfd_generic_link_add_one_symbol
3101 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
3102 tls_sec
, 0, NULL
, false,
3103 bed
->collect
, &bh
)))
3106 htab
->tls_module_base
= bh
;
3108 tlsbase
= (struct elf_link_hash_entry
*)bh
;
3109 tlsbase
->def_regular
= 1;
3110 tlsbase
->other
= STV_HIDDEN
;
3111 tlsbase
->root
.linker_def
= 1;
3112 (*bed
->elf_backend_hide_symbol
) (info
, tlsbase
, true);
3120 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry
*h
,
3121 unsigned int st_other
,
3123 bool dynamic ATTRIBUTE_UNUSED
)
3127 struct elf_x86_link_hash_entry
*eh
3128 = (struct elf_x86_link_hash_entry
*) h
;
3129 eh
->def_protected
= ELF_ST_VISIBILITY (st_other
) == STV_PROTECTED
;
3133 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3136 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
3137 struct elf_link_hash_entry
*dir
,
3138 struct elf_link_hash_entry
*ind
)
3140 struct elf_x86_link_hash_entry
*edir
, *eind
;
3142 edir
= (struct elf_x86_link_hash_entry
*) dir
;
3143 eind
= (struct elf_x86_link_hash_entry
*) ind
;
3145 if (ind
->root
.type
== bfd_link_hash_indirect
3146 && dir
->got
.refcount
<= 0)
3148 edir
->tls_type
= eind
->tls_type
;
3149 eind
->tls_type
= GOT_UNKNOWN
;
3152 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
3153 generate a R_386_COPY reloc. */
3154 edir
->gotoff_ref
|= eind
->gotoff_ref
;
3156 edir
->zero_undefweak
|= eind
->zero_undefweak
;
3158 if (ELIMINATE_COPY_RELOCS
3159 && ind
->root
.type
!= bfd_link_hash_indirect
3160 && dir
->dynamic_adjusted
)
3162 /* If called to transfer flags for a weakdef during processing
3163 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
3164 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3165 if (dir
->versioned
!= versioned_hidden
)
3166 dir
->ref_dynamic
|= ind
->ref_dynamic
;
3167 dir
->ref_regular
|= ind
->ref_regular
;
3168 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
3169 dir
->needs_plt
|= ind
->needs_plt
;
3170 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
3173 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
3176 /* Remove undefined weak symbol from the dynamic symbol table if it
3177 is resolved to 0. */
3180 _bfd_x86_elf_fixup_symbol (struct bfd_link_info
*info
,
3181 struct elf_link_hash_entry
*h
)
3183 if (h
->dynindx
!= -1
3184 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
, elf_x86_hash_entry (h
)))
3187 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
3193 /* Change the STT_GNU_IFUNC symbol defined in position-dependent
3194 executable into the normal function symbol and set its address
3195 to its PLT entry, which should be resolved by R_*_IRELATIVE at
3199 _bfd_x86_elf_link_fixup_ifunc_symbol (struct bfd_link_info
*info
,
3200 struct elf_x86_link_hash_table
*htab
,
3201 struct elf_link_hash_entry
*h
,
3202 Elf_Internal_Sym
*sym
)
3204 if (bfd_link_pde (info
)
3207 && h
->plt
.offset
!= (bfd_vma
) -1
3208 && h
->type
== STT_GNU_IFUNC
)
3212 bfd
*output_bfd
= info
->output_bfd
;
3214 if (htab
->plt_second
)
3216 struct elf_x86_link_hash_entry
*eh
3217 = (struct elf_x86_link_hash_entry
*) h
;
3219 plt_s
= htab
->plt_second
;
3220 plt_offset
= eh
->plt_second
.offset
;
3224 plt_s
= htab
->elf
.splt
;
3225 plt_offset
= h
->plt
.offset
;
3229 sym
->st_info
= ELF_ST_INFO (ELF_ST_BIND (sym
->st_info
), STT_FUNC
);
3231 = _bfd_elf_section_from_bfd_section (output_bfd
,
3232 plt_s
->output_section
);
3233 sym
->st_value
= (plt_s
->output_section
->vma
3234 + plt_s
->output_offset
+ plt_offset
);
3238 /* Report relative relocation. */
3241 _bfd_x86_elf_link_report_relative_reloc
3242 (struct bfd_link_info
*info
, asection
*asect
,
3243 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*sym
,
3244 const char *reloc_name
, const void *reloc
)
3248 const Elf_Internal_Rela
*rel
= (const Elf_Internal_Rela
*) reloc
;
3250 /* Use the output BFD for linker created sections. */
3251 if ((asect
->flags
& SEC_LINKER_CREATED
) != 0)
3252 abfd
= info
->output_bfd
;
3254 abfd
= asect
->owner
;
3256 if (h
!= NULL
&& h
->root
.root
.string
!= NULL
)
3257 name
= h
->root
.root
.string
;
3259 name
= bfd_elf_sym_name (abfd
, &elf_symtab_hdr (abfd
), sym
, NULL
);
3261 if (asect
->use_rela_p
)
3262 info
->callbacks
->einfo
3263 (_("%pB: %s (offset: 0x%v, info: 0x%v, addend: 0x%v) against "
3264 "'%s' " "for section '%pA' in %pB\n"),
3265 info
->output_bfd
, reloc_name
, rel
->r_offset
, rel
->r_info
,
3266 rel
->r_addend
, name
, asect
, abfd
);
3268 info
->callbacks
->einfo
3269 (_("%pB: %s (offset: 0x%v, info: 0x%v) against '%s' for section "
3271 info
->output_bfd
, reloc_name
, rel
->r_offset
, rel
->r_info
, name
,
3275 /* Report TLS transition error. */
3278 _bfd_x86_elf_link_report_tls_transition_error
3279 (struct bfd_link_info
*info
, bfd
*abfd
, asection
*asect
,
3280 Elf_Internal_Shdr
*symtab_hdr
, struct elf_link_hash_entry
*h
,
3281 Elf_Internal_Sym
*sym
, const Elf_Internal_Rela
*rel
,
3282 const char *from_reloc_name
, const char *to_reloc_name
,
3283 enum elf_x86_tls_error_type tls_error
)
3286 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3287 struct elf_x86_link_hash_table
*htab
3288 = elf_x86_hash_table (info
, bed
->target_id
);
3291 name
= h
->root
.root
.string
;
3297 name
= bfd_elf_sym_name (abfd
, symtab_hdr
, sym
, NULL
);
3302 case elf_x86_tls_error_yes
:
3303 info
->callbacks
->einfo
3304 /* xgettext:c-format */
3305 (_("%pB: TLS transition from %s to %s against `%s' at 0x%v in "
3306 "section `%pA' failed\n"),
3307 abfd
, from_reloc_name
, to_reloc_name
, name
, rel
->r_offset
,
3311 case elf_x86_tls_error_add
:
3312 info
->callbacks
->einfo
3313 /* xgettext:c-format */
3314 (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3316 abfd
, asect
, rel
->r_offset
, from_reloc_name
, name
);
3319 case elf_x86_tls_error_add_mov
:
3320 info
->callbacks
->einfo
3321 /* xgettext:c-format */
3322 (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3323 "in ADD or MOV only\n"),
3324 abfd
, asect
, rel
->r_offset
, from_reloc_name
, name
);
3327 case elf_x86_tls_error_add_sub_mov
:
3328 info
->callbacks
->einfo
3329 /* xgettext:c-format */
3330 (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3331 "in ADD, SUB or MOV only\n"),
3332 abfd
, asect
, rel
->r_offset
, from_reloc_name
, name
);
3335 case elf_x86_tls_error_indirect_call
:
3336 info
->callbacks
->einfo
3337 /* xgettext:c-format */
3338 (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3339 "in indirect CALL with %s register only\n"),
3340 abfd
, asect
, rel
->r_offset
, from_reloc_name
, name
,
3344 case elf_x86_tls_error_lea
:
3345 info
->callbacks
->einfo
3346 /* xgettext:c-format */
3347 (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3349 abfd
, asect
, rel
->r_offset
, from_reloc_name
, name
);
3357 bfd_set_error (bfd_error_bad_value
);
3360 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3363 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry
*h
)
3365 if (h
->plt
.offset
!= (bfd_vma
) -1
3367 && !h
->pointer_equality_needed
)
3370 return _bfd_elf_hash_symbol (h
);
3373 /* Adjust a symbol defined by a dynamic object and referenced by a
3374 regular object. The current definition is in some section of the
3375 dynamic object, but we're not including those sections. We have to
3376 change the definition to something the rest of the link can
3380 _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
3381 struct elf_link_hash_entry
*h
)
3383 struct elf_x86_link_hash_table
*htab
;
3385 struct elf_x86_link_hash_entry
*eh
;
3386 struct elf_dyn_relocs
*p
;
3387 const struct elf_backend_data
*bed
3388 = get_elf_backend_data (info
->output_bfd
);
3390 eh
= (struct elf_x86_link_hash_entry
*) h
;
3392 /* Clear GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS if it is turned
3393 on by an input relocatable file and there is a non-GOT/non-PLT
3394 reference from another relocatable file without it.
3395 NB: There can be non-GOT reference in data sections in input with
3396 GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS. */
3397 if (eh
->non_got_ref_without_indirect_extern_access
3398 && info
->indirect_extern_access
== 1
3399 && bfd_link_executable (info
))
3401 unsigned int needed_1
;
3402 info
->indirect_extern_access
= 0;
3403 /* Turn off nocopyreloc if implied by indirect_extern_access. */
3404 if (info
->nocopyreloc
== 2)
3405 info
->nocopyreloc
= 0;
3406 needed_1
= bfd_h_get_32 (info
->output_bfd
, info
->needed_1_p
);
3407 needed_1
&= ~GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS
;
3408 bfd_h_put_32 (info
->output_bfd
, needed_1
, info
->needed_1_p
);
3411 /* STT_GNU_IFUNC symbol must go through PLT. */
3412 if (h
->type
== STT_GNU_IFUNC
)
3414 /* All local STT_GNU_IFUNC references must be treate as local
3415 calls via local PLT. */
3417 && SYMBOL_CALLS_LOCAL (info
, h
))
3419 bfd_size_type pc_count
= 0, count
= 0;
3420 struct elf_dyn_relocs
**pp
;
3422 eh
= (struct elf_x86_link_hash_entry
*) h
;
3423 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
3425 pc_count
+= p
->pc_count
;
3426 p
->count
-= p
->pc_count
;
3435 if (pc_count
|| count
)
3440 /* Increment PLT reference count only for PC-relative
3443 if (h
->plt
.refcount
<= 0)
3444 h
->plt
.refcount
= 1;
3446 h
->plt
.refcount
+= 1;
3450 /* GOTOFF relocation needs PLT. */
3452 h
->plt
.refcount
= 1;
3455 if (h
->plt
.refcount
<= 0)
3457 h
->plt
.offset
= (bfd_vma
) -1;
3463 /* If this is a function, put it in the procedure linkage table. We
3464 will fill in the contents of the procedure linkage table later,
3465 when we know the address of the .got section. */
3466 if (h
->type
== STT_FUNC
3469 if (h
->plt
.refcount
<= 0
3470 || SYMBOL_CALLS_LOCAL (info
, h
)
3471 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3472 && h
->root
.type
== bfd_link_hash_undefweak
))
3474 /* This case can occur if we saw a PLT32 reloc in an input
3475 file, but the symbol was never referred to by a dynamic
3476 object, or if all references were garbage collected. In
3477 such a case, we don't actually need to build a procedure
3478 linkage table, and we can just do a PC32 reloc instead. */
3479 h
->plt
.offset
= (bfd_vma
) -1;
3486 /* It's possible that we incorrectly decided a .plt reloc was needed
3487 * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
3488 check_relocs. We can't decide accurately between function and
3489 non-function syms in check-relocs; Objects loaded later in
3490 the link may change h->type. So fix it now. */
3491 h
->plt
.offset
= (bfd_vma
) -1;
3493 /* If this is a weak symbol, and there is a real definition, the
3494 processor independent code will have arranged for us to see the
3495 real definition first, and we can just use the same value. */
3496 if (h
->is_weakalias
)
3498 struct elf_link_hash_entry
*def
= weakdef (h
);
3499 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
3500 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
3501 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
3502 if (ELIMINATE_COPY_RELOCS
3503 || info
->nocopyreloc
3504 || SYMBOL_NO_COPYRELOC (info
, eh
))
3506 /* NB: needs_copy is always 0 for i386. */
3507 h
->non_got_ref
= def
->non_got_ref
;
3508 eh
->needs_copy
= def
->needs_copy
;
3513 /* This is a reference to a symbol defined by a dynamic object which
3514 is not a function. */
3516 /* If we are creating a shared library, we must presume that the
3517 only references to the symbol are via the global offset table.
3518 For such cases we need not do anything here; the relocations will
3519 be handled correctly by relocate_section. */
3520 if (!bfd_link_executable (info
))
3523 /* If there are no references to this symbol that do not use the
3524 GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
3525 reloc. NB: gotoff_ref is always 0 for x86-64. */
3526 if (!h
->non_got_ref
&& !eh
->gotoff_ref
)
3529 /* If -z nocopyreloc was given, we won't generate them either. */
3530 if (info
->nocopyreloc
|| SYMBOL_NO_COPYRELOC (info
, eh
))
3536 htab
= elf_x86_hash_table (info
, bed
->target_id
);
3540 /* If there aren't any dynamic relocs in read-only sections nor
3541 R_386_GOTOFF relocation, then we can keep the dynamic relocs and
3542 avoid the copy reloc. This doesn't work on VxWorks, where we can
3543 not have dynamic relocations (other than copy and jump slot
3544 relocations) in an executable. */
3545 if (ELIMINATE_COPY_RELOCS
3546 && (bed
->target_id
== X86_64_ELF_DATA
3548 && htab
->elf
.target_os
!= is_vxworks
)))
3550 /* If we don't find any dynamic relocs in read-only sections,
3551 then we'll be keeping the dynamic relocs and avoiding the copy
3553 if (!_bfd_elf_readonly_dynrelocs (h
))
3560 /* We must allocate the symbol in our .dynbss section, which will
3561 become part of the .bss section of the executable. There will be
3562 an entry for this symbol in the .dynsym section. The dynamic
3563 object will contain position independent code, so all references
3564 from the dynamic object to this symbol will go through the global
3565 offset table. The dynamic linker will use the .dynsym entry to
3566 determine the address it must put in the global offset table, so
3567 both the dynamic object and the regular object will refer to the
3568 same memory location for the variable. */
3570 /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
3571 dynamic linker to copy the initial value out of the dynamic object
3572 and into the runtime process image. */
3573 if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
3575 s
= htab
->elf
.sdynrelro
;
3576 srel
= htab
->elf
.sreldynrelro
;
3580 s
= htab
->elf
.sdynbss
;
3581 srel
= htab
->elf
.srelbss
;
3583 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
3585 if (eh
->def_protected
&& bfd_link_executable (info
))
3586 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
3588 /* Disallow copy relocation against non-copyable protected
3590 s
= p
->sec
->output_section
;
3591 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
3593 info
->callbacks
->einfo
3594 /* xgettext:c-format */
3595 (_("%F%P: %pB: copy relocation against non-copyable "
3596 "protected symbol `%s' in %pB\n"),
3597 p
->sec
->owner
, h
->root
.root
.string
,
3598 h
->root
.u
.def
.section
->owner
);
3603 srel
->size
+= htab
->sizeof_reloc
;
3607 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
3611 _bfd_x86_elf_hide_symbol (struct bfd_link_info
*info
,
3612 struct elf_link_hash_entry
*h
,
3615 if (h
->root
.type
== bfd_link_hash_undefweak
3617 && bfd_link_pie (info
))
3619 /* When there is no dynamic interpreter in PIE, make the undefined
3620 weak symbol dynamic so that PC relative branch to the undefined
3621 weak symbol will land to address 0. */
3622 struct elf_x86_link_hash_entry
*eh
= elf_x86_hash_entry (h
);
3623 if (h
->plt
.refcount
> 0
3624 || eh
->plt_got
.refcount
> 0)
3628 _bfd_elf_link_hash_hide_symbol (info
, h
, force_local
);
3631 /* Return TRUE if a symbol is referenced locally. It is similar to
3632 SYMBOL_REFERENCES_LOCAL, but it also checks version script. It
3633 works in check_relocs. */
3636 _bfd_x86_elf_link_symbol_references_local (struct bfd_link_info
*info
,
3637 struct elf_link_hash_entry
*h
)
3639 struct elf_x86_link_hash_entry
*eh
= elf_x86_hash_entry (h
);
3640 struct elf_x86_link_hash_table
*htab
3641 = (struct elf_x86_link_hash_table
*) info
->hash
;
3643 if (eh
->local_ref
> 1)
3646 if (eh
->local_ref
== 1)
3649 /* Unversioned symbols defined in regular objects can be forced local
3650 by linker version script. A weak undefined symbol is forced local
3652 1. It has non-default visibility. Or
3653 2. When building executable, there is no dynamic linker. Or
3654 3. or "-z nodynamic-undefined-weak" is used.
3656 if (_bfd_elf_symbol_refs_local_p (h
, info
, 1)
3657 || (h
->root
.type
== bfd_link_hash_undefweak
3658 && (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3659 || (bfd_link_executable (info
)
3660 && htab
->interp
== NULL
)
3661 || info
->dynamic_undefined_weak
== 0))
3662 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
3663 && info
->version_info
!= NULL
3664 && _bfd_elf_link_hide_sym_by_version (info
, h
)))
3674 /* Return the section that should be marked against GC for a given
3678 _bfd_x86_elf_gc_mark_hook (asection
*sec
,
3679 struct bfd_link_info
*info
,
3680 Elf_Internal_Rela
*rel
,
3681 struct elf_link_hash_entry
*h
,
3682 Elf_Internal_Sym
*sym
)
3684 /* Compiler should optimize this out. */
3685 if (((unsigned int) R_X86_64_GNU_VTINHERIT
3686 != (unsigned int) R_386_GNU_VTINHERIT
)
3687 || ((unsigned int) R_X86_64_GNU_VTENTRY
3688 != (unsigned int) R_386_GNU_VTENTRY
))
3692 switch (ELF32_R_TYPE (rel
->r_info
))
3694 case R_X86_64_GNU_VTINHERIT
:
3695 case R_X86_64_GNU_VTENTRY
:
3699 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
3703 elf_i386_get_plt_got_vma (struct elf_x86_plt
*plt_p ATTRIBUTE_UNUSED
,
3705 bfd_vma offset ATTRIBUTE_UNUSED
,
3708 return got_addr
+ off
;
3712 elf_x86_64_get_plt_got_vma (struct elf_x86_plt
*plt_p
,
3715 bfd_vma got_addr ATTRIBUTE_UNUSED
)
3717 return plt_p
->sec
->vma
+ offset
+ off
+ plt_p
->plt_got_insn_size
;
3721 elf_i386_valid_plt_reloc_p (unsigned int type
)
3723 return (type
== R_386_JUMP_SLOT
3724 || type
== R_386_GLOB_DAT
3725 || type
== R_386_IRELATIVE
);
3729 elf_x86_64_valid_plt_reloc_p (unsigned int type
)
3731 return (type
== R_X86_64_JUMP_SLOT
3732 || type
== R_X86_64_GLOB_DAT
3733 || type
== R_X86_64_IRELATIVE
);
3737 _bfd_x86_elf_get_synthetic_symtab (bfd
*abfd
,
3741 struct elf_x86_plt plts
[],
3745 long size
, i
, n
, len
;
3747 unsigned int plt_got_offset
, plt_entry_size
;
3749 bfd_byte
*plt_contents
;
3751 arelent
**dynrelbuf
, *p
;
3753 const struct elf_backend_data
*bed
;
3754 bfd_vma (*get_plt_got_vma
) (struct elf_x86_plt
*, bfd_vma
, bfd_vma
,
3756 bool (*valid_plt_reloc_p
) (unsigned int);
3757 unsigned int jump_slot_reloc
;
3763 dynrelbuf
= (arelent
**) bfd_malloc (relsize
);
3764 if (dynrelbuf
== NULL
)
3767 dynrelcount
= bfd_canonicalize_dynamic_reloc (abfd
, dynrelbuf
,
3769 if (dynrelcount
<= 0)
3772 /* Sort the relocs by address. */
3773 qsort (dynrelbuf
, dynrelcount
, sizeof (arelent
*),
3774 _bfd_x86_elf_compare_relocs
);
3776 size
= count
* sizeof (asymbol
);
3778 /* Allocate space for @plt suffixes. */
3780 for (i
= 0; i
< dynrelcount
; i
++)
3783 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
3785 size
+= sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd
);
3788 s
= *ret
= (asymbol
*) bfd_zmalloc (size
);
3792 bed
= get_elf_backend_data (abfd
);
3794 if (bed
->target_id
== X86_64_ELF_DATA
)
3796 get_plt_got_vma
= elf_x86_64_get_plt_got_vma
;
3797 valid_plt_reloc_p
= elf_x86_64_valid_plt_reloc_p
;
3798 jump_slot_reloc
= R_X86_64_JUMP_SLOT
;
3802 get_plt_got_vma
= elf_i386_get_plt_got_vma
;
3803 valid_plt_reloc_p
= elf_i386_valid_plt_reloc_p
;
3804 jump_slot_reloc
= R_386_JUMP_SLOT
;
3807 /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
3809 asection
*sec
= bfd_get_section_by_name (abfd
, ".got.plt");
3811 got_addr
= sec
->vma
;
3814 sec
= bfd_get_section_by_name (abfd
, ".got");
3816 got_addr
= sec
->vma
;
3819 if (got_addr
== (bfd_vma
) -1)
3824 /* Check for each PLT section. */
3825 names
= (char *) (s
+ count
);
3828 for (j
= 0; plts
[j
].name
!= NULL
; j
++)
3829 if ((plt_contents
= plts
[j
].contents
) != NULL
)
3834 struct elf_x86_plt
*plt_p
= &plts
[j
];
3836 plt_got_offset
= plt_p
->plt_got_offset
;
3837 plt_entry_size
= plt_p
->plt_entry_size
;
3841 if ((plt_p
->type
& plt_lazy
))
3843 /* Skip PLT0 in lazy PLT. */
3845 offset
= plt_entry_size
;
3853 /* Check each PLT entry against dynamic relocations. */
3854 for (; k
< plt_p
->count
; k
++)
3860 /* Get the GOT offset for i386 or the PC-relative offset
3861 for x86-64, a signed 32-bit integer. */
3862 off
= H_GET_32 (abfd
, (plt_contents
+ offset
3864 got_vma
= get_plt_got_vma (plt_p
, off
, offset
, got_addr
);
3866 /* Binary search. */
3870 while ((min
+ 1) < max
)
3874 mid
= (min
+ max
) / 2;
3876 if (got_vma
> r
->address
)
3878 else if (got_vma
< r
->address
)
3887 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
3888 if (got_vma
== p
->address
3890 && valid_plt_reloc_p (p
->howto
->type
))
3892 *s
= **p
->sym_ptr_ptr
;
3893 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
3894 set. Since we are defining a symbol, ensure one
3896 if ((s
->flags
& BSF_LOCAL
) == 0)
3897 s
->flags
|= BSF_GLOBAL
;
3898 s
->flags
|= BSF_SYNTHETIC
;
3899 /* This is no longer a section symbol. */
3900 s
->flags
&= ~BSF_SECTION_SYM
;
3902 s
->the_bfd
= plt
->owner
;
3906 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
3907 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
3909 /* There may be JUMP_SLOT and IRELATIVE relocations.
3910 JUMP_SLOT r_addend should be ignored. */
3911 if (p
->addend
!= 0 && p
->howto
->type
!= jump_slot_reloc
)
3915 memcpy (names
, "+0x", sizeof ("+0x") - 1);
3916 names
+= sizeof ("+0x") - 1;
3917 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
3918 for (a
= buf
; *a
== '0'; ++a
)
3921 memcpy (names
, a
, size
);
3924 memcpy (names
, "@plt", sizeof ("@plt"));
3925 names
+= sizeof ("@plt");
3928 /* There should be only one entry in PLT for a given
3929 symbol. Set howto to NULL after processing a PLT
3930 entry to guard against corrupted PLT. */
3933 offset
+= plt_entry_size
;
3937 /* PLT entries with R_386_TLS_DESC relocations are skipped. */
3946 for (j
= 0; plts
[j
].name
!= NULL
; j
++)
3947 _bfd_elf_munmap_section_contents (plts
[j
].sec
, plts
[j
].contents
);
3954 /* Parse x86 GNU properties. */
3956 enum elf_property_kind
3957 _bfd_x86_elf_parse_gnu_properties (bfd
*abfd
, unsigned int type
,
3958 bfd_byte
*ptr
, unsigned int datasz
)
3962 if (type
== GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3963 || type
== GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3964 || (type
>= GNU_PROPERTY_X86_UINT32_AND_LO
3965 && type
<= GNU_PROPERTY_X86_UINT32_AND_HI
)
3966 || (type
>= GNU_PROPERTY_X86_UINT32_OR_LO
3967 && type
<= GNU_PROPERTY_X86_UINT32_OR_HI
)
3968 || (type
>= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3969 && type
<= GNU_PROPERTY_X86_UINT32_OR_AND_HI
))
3974 (_("error: %pB: <corrupt x86 property (0x%x) size: 0x%x>"),
3975 abfd
, type
, datasz
);
3976 return property_corrupt
;
3978 prop
= _bfd_elf_get_property (abfd
, type
, datasz
);
3979 prop
->u
.number
|= bfd_h_get_32 (abfd
, ptr
);
3980 prop
->pr_kind
= property_number
;
3981 return property_number
;
3984 return property_ignored
;
3987 /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
3988 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
3989 should be merged with ABFD. */
3992 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info
*info
,
3993 bfd
*abfd ATTRIBUTE_UNUSED
,
3994 bfd
*bbfd ATTRIBUTE_UNUSED
,
3995 elf_property
*aprop
,
3996 elf_property
*bprop
)
3998 unsigned int number
, features
;
3999 bool updated
= false;
4000 const struct elf_backend_data
*bed
;
4001 struct elf_x86_link_hash_table
*htab
;
4002 unsigned int pr_type
= aprop
!= NULL
? aprop
->pr_type
: bprop
->pr_type
;
4004 if (pr_type
== GNU_PROPERTY_X86_COMPAT_ISA_1_USED
4005 || (pr_type
>= GNU_PROPERTY_X86_UINT32_OR_AND_LO
4006 && pr_type
<= GNU_PROPERTY_X86_UINT32_OR_AND_HI
))
4008 if (aprop
== NULL
|| bprop
== NULL
)
4010 /* Only one of APROP and BPROP can be NULL. */
4013 /* Remove this property since the other input file doesn't
4015 aprop
->pr_kind
= property_remove
;
4021 number
= aprop
->u
.number
;
4022 aprop
->u
.number
= number
| bprop
->u
.number
;
4023 updated
= number
!= (unsigned int) aprop
->u
.number
;
4027 else if (pr_type
== GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4028 || (pr_type
>= GNU_PROPERTY_X86_UINT32_OR_LO
4029 && pr_type
<= GNU_PROPERTY_X86_UINT32_OR_HI
))
4032 if (pr_type
== GNU_PROPERTY_X86_ISA_1_NEEDED
)
4034 bed
= get_elf_backend_data (info
->output_bfd
);
4035 htab
= elf_x86_hash_table (info
, bed
->target_id
);
4036 switch (htab
->params
->isa_level
)
4041 features
= GNU_PROPERTY_X86_ISA_1_V2
;
4044 features
= GNU_PROPERTY_X86_ISA_1_V3
;
4047 features
= GNU_PROPERTY_X86_ISA_1_V4
;
4053 if (aprop
!= NULL
&& bprop
!= NULL
)
4055 number
= aprop
->u
.number
;
4056 aprop
->u
.number
= number
| bprop
->u
.number
| features
;
4057 /* Remove the property if all bits are empty. */
4058 if (aprop
->u
.number
== 0)
4060 aprop
->pr_kind
= property_remove
;
4064 updated
= number
!= (unsigned int) aprop
->u
.number
;
4068 /* Only one of APROP and BPROP can be NULL. */
4071 aprop
->u
.number
|= features
;
4072 if (aprop
->u
.number
== 0)
4074 /* Remove APROP if all bits are empty. */
4075 aprop
->pr_kind
= property_remove
;
4081 /* Return TRUE if APROP is NULL and all bits of BPROP
4082 aren't empty to indicate that BPROP should be added
4084 bprop
->u
.number
|= features
;
4085 updated
= bprop
->u
.number
!= 0;
4090 else if (pr_type
>= GNU_PROPERTY_X86_UINT32_AND_LO
4091 && pr_type
<= GNU_PROPERTY_X86_UINT32_AND_HI
)
4093 /* Only one of APROP and BPROP can be NULL:
4094 1. APROP & BPROP when both APROP and BPROP aren't NULL.
4095 2. If APROP is NULL, remove x86 feature.
4096 3. Otherwise, do nothing.
4098 bed
= get_elf_backend_data (info
->output_bfd
);
4099 htab
= elf_x86_hash_table (info
, bed
->target_id
);
4102 if (aprop
!= NULL
&& bprop
!= NULL
)
4104 number
= aprop
->u
.number
;
4105 aprop
->u
.number
= number
& bprop
->u
.number
;
4106 if (pr_type
== GNU_PROPERTY_X86_FEATURE_1_AND
)
4109 if (htab
->params
->ibt
)
4110 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
4111 if (htab
->params
->shstk
)
4112 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
4113 if (htab
->params
->lam_u48
)
4114 features
|= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4115 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57
);
4116 else if (htab
->params
->lam_u57
)
4117 features
|= GNU_PROPERTY_X86_FEATURE_1_LAM_U57
;
4118 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT,
4119 GNU_PROPERTY_X86_FEATURE_1_SHSTK,
4120 GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
4121 GNU_PROPERTY_X86_FEATURE_1_LAM_U57. */
4122 aprop
->u
.number
|= features
;
4124 updated
= number
!= (unsigned int) aprop
->u
.number
;
4125 /* Remove the property if all feature bits are cleared. */
4126 if (aprop
->u
.number
== 0)
4127 aprop
->pr_kind
= property_remove
;
4131 /* There should be no AND properties since some input doesn't
4132 have them. Set IBT and SHSTK properties for -z ibt and -z
4135 if (pr_type
== GNU_PROPERTY_X86_FEATURE_1_AND
)
4137 if (htab
->params
->ibt
)
4138 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
4139 if (htab
->params
->shstk
)
4140 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
4141 if (htab
->params
->lam_u48
)
4142 features
|= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4143 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57
);
4144 else if (htab
->params
->lam_u57
)
4145 features
|= GNU_PROPERTY_X86_FEATURE_1_LAM_U57
;
4151 updated
= features
!= (unsigned int) aprop
->u
.number
;
4152 aprop
->u
.number
= features
;
4157 bprop
->u
.number
= features
;
4160 else if (aprop
!= NULL
)
4162 aprop
->pr_kind
= property_remove
;
4170 /* Never should happen. */
4177 /* Report x86-64 ISA level. */
4180 report_isa_level (struct bfd_link_info
*info
, bfd
*abfd
,
4181 unsigned int bitmask
, bool needed
)
4187 info
->callbacks
->einfo (_("%pB: x86 ISA needed: "), abfd
);
4189 info
->callbacks
->einfo (_("%pB: x86 ISA used: "), abfd
);
4193 unsigned int bit
= bitmask
& (- bitmask
);
4198 case GNU_PROPERTY_X86_ISA_1_BASELINE
:
4199 info
->callbacks
->einfo ("x86-64-baseline");
4201 case GNU_PROPERTY_X86_ISA_1_V2
:
4202 info
->callbacks
->einfo ("x86-64-v2");
4204 case GNU_PROPERTY_X86_ISA_1_V3
:
4205 info
->callbacks
->einfo ("x86-64-v3");
4207 case GNU_PROPERTY_X86_ISA_1_V4
:
4208 info
->callbacks
->einfo ("x86-64-v4");
4211 info
->callbacks
->einfo (_("<unknown: %x>"), bit
);
4215 info
->callbacks
->einfo (", ");
4218 info
->callbacks
->einfo ("\n");
4221 /* Set up x86 GNU properties. Return the first relocatable ELF input
4222 with GNU properties if found. Otherwise, return NULL. */
4225 _bfd_x86_elf_link_setup_gnu_properties
4226 (struct bfd_link_info
*info
, struct elf_x86_init_table
*init_table
)
4230 asection
*sec
, *pltsec
;
4233 unsigned int plt_alignment
, features
, isa_level
;
4234 struct elf_x86_link_hash_table
*htab
;
4238 const struct elf_backend_data
*bed
;
4239 unsigned int class_align
= ABI_64_P (info
->output_bfd
) ? 3 : 2;
4240 unsigned int got_align
;
4242 /* Find a normal input file with GNU property note. */
4243 for (pbfd
= info
->input_bfds
;
4245 pbfd
= pbfd
->link
.next
)
4246 if (bfd_get_flavour (pbfd
) == bfd_target_elf_flavour
4247 && bfd_count_sections (pbfd
) != 0)
4251 if (elf_properties (pbfd
) != NULL
)
4255 bed
= get_elf_backend_data (info
->output_bfd
);
4257 htab
= elf_x86_hash_table (info
, bed
->target_id
);
4262 if (htab
->params
->ibt
)
4264 features
= GNU_PROPERTY_X86_FEATURE_1_IBT
;
4265 htab
->params
->cet_report
&= ~prop_report_ibt
;
4267 if (htab
->params
->shstk
)
4269 features
|= GNU_PROPERTY_X86_FEATURE_1_SHSTK
;
4270 htab
->params
->cet_report
&= ~prop_report_shstk
;
4272 if (!(htab
->params
->cet_report
& (prop_report_ibt
| prop_report_shstk
)))
4273 htab
->params
->cet_report
= prop_report_none
;
4274 if (htab
->params
->lam_u48
)
4276 features
|= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4277 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57
);
4278 htab
->params
->lam_u48_report
= prop_report_none
;
4279 htab
->params
->lam_u57_report
= prop_report_none
;
4281 else if (htab
->params
->lam_u57
)
4283 features
|= GNU_PROPERTY_X86_FEATURE_1_LAM_U57
;
4284 htab
->params
->lam_u57_report
= prop_report_none
;
4287 switch (htab
->params
->isa_level
)
4293 isa_level
= GNU_PROPERTY_X86_ISA_1_BASELINE
;
4296 isa_level
= GNU_PROPERTY_X86_ISA_1_V2
;
4299 isa_level
= GNU_PROPERTY_X86_ISA_1_V3
;
4302 isa_level
= GNU_PROPERTY_X86_ISA_1_V4
;
4313 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT,
4314 GNU_PROPERTY_X86_FEATURE_1_SHSTK,
4315 GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
4316 GNU_PROPERTY_X86_FEATURE_1_LAM_U57. */
4317 prop
= _bfd_elf_get_property (ebfd
,
4318 GNU_PROPERTY_X86_FEATURE_1_AND
,
4320 prop
->u
.number
|= features
;
4321 prop
->pr_kind
= property_number
;
4326 /* If ISA level is set, add GNU_PROPERTY_X86_ISA_1_NEEDED. */
4327 prop
= _bfd_elf_get_property (ebfd
,
4328 GNU_PROPERTY_X86_ISA_1_NEEDED
,
4330 prop
->u
.number
|= isa_level
;
4331 prop
->pr_kind
= property_number
;
4334 /* Create the GNU property note section if needed. */
4335 if (prop
!= NULL
&& pbfd
== NULL
)
4337 sec
= bfd_make_section_with_flags (ebfd
,
4338 NOTE_GNU_PROPERTY_SECTION_NAME
,
4346 info
->callbacks
->einfo (_("%F%P: failed to create GNU property section\n"));
4348 if (!bfd_set_section_alignment (sec
, class_align
))
4351 info
->callbacks
->einfo (_("%F%pA: failed to align section\n"),
4355 elf_section_type (sec
) = SHT_NOTE
;
4359 bool check_feature_1
= (htab
->params
->cet_report
4360 || htab
->params
->lam_u48_report
4361 || htab
->params
->lam_u57_report
);
4362 if (check_feature_1
|| htab
->params
->isa_level_report
)
4364 /* Report missing IBT, SHSTK, ISA level and LAM properties. */
4366 const char *warning_msg
= _("%P: %pB: warning: missing %s\n");
4367 const char *error_msg
= _("%X%P: %pB: error: missing %s\n");
4368 const char *cet_msg
= NULL
;
4369 const char *lam_u48_msg
= NULL
;
4370 const char *lam_u57_msg
= NULL
;
4371 const char *missing
;
4372 elf_property_list
*p
;
4373 bool missing_ibt
, missing_shstk
;
4374 bool missing_lam_u48
, missing_lam_u57
;
4376 = (htab
->params
->cet_report
4377 && (htab
->params
->cet_report
& prop_report_ibt
));
4379 = (htab
->params
->cet_report
4380 && (htab
->params
->cet_report
& prop_report_shstk
));
4381 bool report_needed_level
4382 = (htab
->params
->isa_level_report
& isa_level_report_needed
) != 0;
4383 bool report_used_level
4384 = (htab
->params
->isa_level_report
& isa_level_report_used
) != 0;
4386 if (htab
->params
->cet_report
)
4388 if ((htab
->params
->cet_report
& prop_report_warning
))
4389 cet_msg
= warning_msg
;
4391 cet_msg
= error_msg
;
4393 if (htab
->params
->lam_u48_report
)
4395 if ((htab
->params
->lam_u48_report
& prop_report_warning
))
4396 lam_u48_msg
= warning_msg
;
4398 lam_u48_msg
= error_msg
;
4400 if (htab
->params
->lam_u57_report
)
4402 if ((htab
->params
->lam_u57_report
& prop_report_warning
))
4403 lam_u57_msg
= warning_msg
;
4405 lam_u57_msg
= error_msg
;
4408 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
4409 if (!(abfd
->flags
& (DYNAMIC
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
4410 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
4412 elf_property_list
*p_feature_1
= NULL
;
4413 elf_property_list
*p_isa_1_needed
= NULL
;
4414 elf_property_list
*p_isa_1_used
= NULL
;
4415 bool find_feature_1
= check_feature_1
;
4416 bool find_needed_level
= report_needed_level
;
4417 bool find_used_level
= report_used_level
;
4419 for (p
= elf_properties (abfd
); p
; p
= p
->next
)
4421 switch (p
->property
.pr_type
)
4423 case GNU_PROPERTY_X86_FEATURE_1_AND
:
4427 find_feature_1
= false;
4430 case GNU_PROPERTY_X86_ISA_1_NEEDED
:
4431 if (find_needed_level
)
4434 find_needed_level
= false;
4437 case GNU_PROPERTY_X86_ISA_1_USED
:
4438 if (find_used_level
)
4441 find_used_level
= false;
4449 && !find_needed_level
4450 && !find_used_level
)
4455 missing_ibt
= check_ibt
;
4456 missing_shstk
= check_shstk
;
4457 missing_lam_u48
= !!lam_u48_msg
;
4458 missing_lam_u57
= !!lam_u57_msg
;
4461 missing_ibt
&= !(p_feature_1
->property
.u
.number
4462 & GNU_PROPERTY_X86_FEATURE_1_IBT
);
4463 missing_shstk
&= !(p_feature_1
->property
.u
.number
4464 & GNU_PROPERTY_X86_FEATURE_1_SHSTK
);
4465 missing_lam_u48
&= !(p_feature_1
->property
.u
.number
4466 & GNU_PROPERTY_X86_FEATURE_1_LAM_U48
);
4467 missing_lam_u57
&= !(p_feature_1
->property
.u
.number
4468 & GNU_PROPERTY_X86_FEATURE_1_LAM_U57
);
4470 if (missing_ibt
|| missing_shstk
)
4472 if (missing_ibt
&& missing_shstk
)
4473 missing
= _("IBT and SHSTK properties");
4474 else if (missing_ibt
)
4475 missing
= _("IBT property");
4477 missing
= _("SHSTK property");
4478 info
->callbacks
->einfo (cet_msg
, abfd
, missing
);
4480 if (missing_lam_u48
)
4482 missing
= _("LAM_U48 property");
4483 info
->callbacks
->einfo (lam_u48_msg
, abfd
, missing
);
4485 if (missing_lam_u57
)
4487 missing
= _("LAM_U57 property");
4488 info
->callbacks
->einfo (lam_u57_msg
, abfd
, missing
);
4492 report_isa_level (info
, abfd
,
4493 p_isa_1_needed
->property
.u
.number
,
4496 report_isa_level (info
, abfd
,
4497 p_isa_1_used
->property
.u
.number
,
4502 pbfd
= _bfd_elf_link_setup_gnu_properties (info
);
4504 htab
->r_info
= init_table
->r_info
;
4505 htab
->r_sym
= init_table
->r_sym
;
4507 if (bfd_link_relocatable (info
))
4510 htab
->plt0_pad_byte
= init_table
->plt0_pad_byte
;
4512 use_ibt_plt
= htab
->params
->ibtplt
|| htab
->params
->ibt
;
4513 if (!use_ibt_plt
&& pbfd
!= NULL
)
4515 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
4516 elf_property_list
*p
;
4518 /* The property list is sorted in order of type. */
4519 for (p
= elf_properties (pbfd
); p
; p
= p
->next
)
4521 if (GNU_PROPERTY_X86_FEATURE_1_AND
== p
->property
.pr_type
)
4523 use_ibt_plt
= !!(p
->property
.u
.number
4524 & GNU_PROPERTY_X86_FEATURE_1_IBT
);
4527 else if (GNU_PROPERTY_X86_FEATURE_1_AND
< p
->property
.pr_type
)
4532 dynobj
= htab
->elf
.dynobj
;
4534 /* Set htab->elf.dynobj here so that there is no need to check and
4535 set it in check_relocs. */
4540 htab
->elf
.dynobj
= pbfd
;
4547 /* Find a normal input file to hold linker created
4549 for (abfd
= info
->input_bfds
;
4551 abfd
= abfd
->link
.next
)
4552 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
4554 & (DYNAMIC
| BFD_LINKER_CREATED
| BFD_PLUGIN
)) == 0
4555 && bed
->relocs_compatible (abfd
->xvec
,
4556 info
->output_bfd
->xvec
))
4558 htab
->elf
.dynobj
= abfd
;
4565 /* Return if there are no normal input files. */
4569 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
4570 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
4571 canonical function address. */
4572 htab
->plt
.has_plt0
= 1;
4573 htab
->plt
.plt_indirect_branch_offset
= 0;
4574 normal_target
= htab
->elf
.target_os
== is_normal
;
4580 htab
->lazy_plt
= init_table
->lazy_ibt_plt
;
4581 htab
->non_lazy_plt
= init_table
->non_lazy_ibt_plt
;
4582 htab
->plt
.plt_indirect_branch_offset
= 4;
4586 htab
->lazy_plt
= init_table
->lazy_plt
;
4587 htab
->non_lazy_plt
= init_table
->non_lazy_plt
;
4592 htab
->lazy_plt
= init_table
->lazy_plt
;
4593 htab
->non_lazy_plt
= NULL
;
4596 pltsec
= htab
->elf
.splt
;
4598 if (htab
->non_lazy_plt
!= NULL
4599 && (!htab
->plt
.has_plt0
|| pltsec
== NULL
))
4609 htab
->sframe_plt
= init_table
->sframe_lazy_ibt_plt
;
4611 htab
->sframe_plt
= init_table
->sframe_non_lazy_ibt_plt
;
4616 htab
->sframe_plt
= init_table
->sframe_lazy_plt
;
4618 htab
->sframe_plt
= init_table
->sframe_non_lazy_plt
;
4622 htab
->sframe_plt
= NULL
;
4624 /* If the non-lazy PLT is available, use it for all PLT entries if
4625 there are no PLT0 or no .plt section. */
4628 if (bfd_link_pic (info
))
4629 htab
->plt
.plt_entry
= htab
->non_lazy_plt
->pic_plt_entry
;
4631 htab
->plt
.plt_entry
= htab
->non_lazy_plt
->plt_entry
;
4632 htab
->plt
.plt_entry_size
= htab
->non_lazy_plt
->plt_entry_size
;
4633 htab
->plt
.plt_got_offset
= htab
->non_lazy_plt
->plt_got_offset
;
4634 htab
->plt
.plt_got_insn_size
4635 = htab
->non_lazy_plt
->plt_got_insn_size
;
4636 htab
->plt
.eh_frame_plt_size
4637 = htab
->non_lazy_plt
->eh_frame_plt_size
;
4638 htab
->plt
.eh_frame_plt
= htab
->non_lazy_plt
->eh_frame_plt
;
4642 if (bfd_link_pic (info
))
4644 htab
->plt
.plt0_entry
= htab
->lazy_plt
->pic_plt0_entry
;
4645 htab
->plt
.plt_entry
= htab
->lazy_plt
->pic_plt_entry
;
4649 htab
->plt
.plt0_entry
= htab
->lazy_plt
->plt0_entry
;
4650 htab
->plt
.plt_entry
= htab
->lazy_plt
->plt_entry
;
4652 htab
->plt
.plt_entry_size
= htab
->lazy_plt
->plt_entry_size
;
4653 htab
->plt
.plt_got_offset
= htab
->lazy_plt
->plt_got_offset
;
4654 htab
->plt
.plt_got_insn_size
4655 = htab
->lazy_plt
->plt_got_insn_size
;
4656 htab
->plt
.eh_frame_plt_size
4657 = htab
->lazy_plt
->eh_frame_plt_size
;
4658 htab
->plt
.eh_frame_plt
= htab
->lazy_plt
->eh_frame_plt
;
4661 if (htab
->elf
.target_os
== is_vxworks
4662 && !elf_vxworks_create_dynamic_sections (dynobj
, info
,
4665 info
->callbacks
->einfo (_("%F%P: failed to create VxWorks dynamic sections\n"));
4669 /* Since create_dynamic_sections isn't always called, but GOT
4670 relocations need GOT relocations, create them here so that we
4671 don't need to do it in check_relocs. */
4672 if (htab
->elf
.sgot
== NULL
4673 && !_bfd_elf_create_got_section (dynobj
, info
))
4674 info
->callbacks
->einfo (_("%F%P: failed to create GOT sections\n"));
4676 got_align
= (bed
->target_id
== X86_64_ELF_DATA
) ? 3 : 2;
4678 /* Align .got and .got.plt sections to their entry size. Do it here
4679 instead of in create_dynamic_sections so that they are always
4680 properly aligned even if create_dynamic_sections isn't called. */
4681 sec
= htab
->elf
.sgot
;
4682 if (!bfd_set_section_alignment (sec
, got_align
))
4683 goto error_alignment
;
4685 sec
= htab
->elf
.sgotplt
;
4686 if (!bfd_set_section_alignment (sec
, got_align
))
4687 goto error_alignment
;
4689 /* Create the ifunc sections here so that check_relocs can be
4691 if (!_bfd_elf_create_ifunc_sections (dynobj
, info
))
4692 info
->callbacks
->einfo (_("%F%P: failed to create ifunc sections\n"));
4694 plt_alignment
= bfd_log2 (htab
->plt
.plt_entry_size
);
4698 /* Whe creating executable, set the contents of the .interp
4699 section to the interpreter. */
4700 if (bfd_link_executable (info
) && !info
->nointerp
)
4702 asection
*s
= bfd_get_linker_section (dynobj
, ".interp");
4705 s
->size
= htab
->dynamic_interpreter_size
;
4706 s
->contents
= (unsigned char *) htab
->dynamic_interpreter
;
4712 flagword pltflags
= (bed
->dynamic_sec_flags
4717 unsigned int non_lazy_plt_alignment
4718 = bfd_log2 (htab
->non_lazy_plt
->plt_entry_size
);
4721 if (!bfd_set_section_alignment (sec
, plt_alignment
))
4722 goto error_alignment
;
4724 /* Create the GOT procedure linkage table. */
4725 sec
= bfd_make_section_anyway_with_flags (dynobj
,
4729 info
->callbacks
->einfo (_("%F%P: failed to create GOT PLT section\n"));
4731 if (!bfd_set_section_alignment (sec
, non_lazy_plt_alignment
))
4732 goto error_alignment
;
4734 htab
->plt_got
= sec
;
4742 /* Create the second PLT for Intel IBT support. IBT
4743 PLT is needed only for lazy binding. */
4744 sec
= bfd_make_section_anyway_with_flags (dynobj
,
4748 info
->callbacks
->einfo (_("%F%P: failed to create IBT-enabled PLT section\n"));
4750 if (!bfd_set_section_alignment (sec
, plt_alignment
))
4751 goto error_alignment
;
4754 htab
->plt_second
= sec
;
4758 if (!info
->no_ld_generated_unwind_info
)
4760 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
4761 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
4762 | SEC_LINKER_CREATED
);
4764 sec
= bfd_make_section_anyway_with_flags (dynobj
,
4768 info
->callbacks
->einfo (_("%F%P: failed to create PLT .eh_frame section\n"));
4770 if (!bfd_set_section_alignment (sec
, class_align
))
4771 goto error_alignment
;
4773 htab
->plt_eh_frame
= sec
;
4775 if (htab
->plt_got
!= NULL
)
4777 sec
= bfd_make_section_anyway_with_flags (dynobj
,
4781 info
->callbacks
->einfo (_("%F%P: failed to create GOT PLT .eh_frame section\n"));
4783 if (!bfd_set_section_alignment (sec
, class_align
))
4784 goto error_alignment
;
4786 htab
->plt_got_eh_frame
= sec
;
4789 if (htab
->plt_second
!= NULL
)
4791 sec
= bfd_make_section_anyway_with_flags (dynobj
,
4795 info
->callbacks
->einfo (_("%F%P: failed to create the second PLT .eh_frame section\n"));
4797 if (!bfd_set_section_alignment (sec
, class_align
))
4798 goto error_alignment
;
4800 htab
->plt_second_eh_frame
= sec
;
4804 /* .sframe sections are emitted for AMD64 ABI only. */
4805 if (ABI_64_P (info
->output_bfd
) && !info
->no_ld_generated_unwind_info
)
4807 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
4808 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
4809 | SEC_LINKER_CREATED
);
4811 sec
= bfd_make_section_anyway_with_flags (dynobj
,
4815 info
->callbacks
->einfo (_("%F%P: failed to create PLT .sframe section\n"));
4818 // if (!bfd_set_section_alignment (sec, class_align))
4819 // goto error_alignment;
4821 htab
->plt_sframe
= sec
;
4823 /* Second PLT is generated for Intel IBT + lazy plt. */
4824 if (htab
->plt_second
!= NULL
)
4826 sec
= bfd_make_section_anyway_with_flags (dynobj
,
4830 info
->callbacks
->einfo (_("%F%P: failed to create second PLT .sframe section\n"));
4832 htab
->plt_second_sframe
= sec
;
4836 if (htab
->plt_got
!= NULL
)
4838 sec
= bfd_make_section_anyway_with_flags (dynobj
,
4842 info
->callbacks
->einfo (_("%F%P: failed to create PLT GOT .sframe section\n"));
4844 htab
->plt_got_sframe
= sec
;
4849 /* The .iplt section is used for IFUNC symbols in static
4851 sec
= htab
->elf
.iplt
;
4854 /* NB: Delay setting its alignment until we know it is non-empty.
4855 Otherwise an empty iplt section may change vma and lma of the
4856 following sections, which triggers moving dot of the following
4857 section backwards, resulting in a warning and section lma not
4858 being set properly. It later leads to a "File truncated"
4860 if (!bfd_set_section_alignment (sec
, 0))
4861 goto error_alignment
;
4863 htab
->plt
.iplt_alignment
= (normal_target
4865 : bed
->plt_alignment
);
4868 if (bfd_link_executable (info
)
4870 && !htab
->params
->has_dynamic_linker
4871 && htab
->params
->static_before_all_inputs
)
4873 /* Report error for dynamic input objects if -static is passed at
4874 command-line before all input files without --dynamic-linker
4875 unless --no-dynamic-linker is used. */
4878 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
4879 if ((abfd
->flags
& DYNAMIC
))
4880 info
->callbacks
->einfo
4881 (_("%X%P: attempted static link of dynamic object `%pB'\n"),
4888 /* Fix up x86 GNU properties. */
4891 _bfd_x86_elf_link_fixup_gnu_properties
4892 (struct bfd_link_info
*info
, elf_property_list
**listp
)
4894 elf_property_list
*p
;
4896 for (p
= *listp
; p
; p
= p
->next
)
4898 unsigned int type
= p
->property
.pr_type
;
4899 if (type
== GNU_PROPERTY_X86_COMPAT_ISA_1_USED
4900 || type
== GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4901 || (type
>= GNU_PROPERTY_X86_UINT32_AND_LO
4902 && type
<= GNU_PROPERTY_X86_UINT32_AND_HI
)
4903 || (type
>= GNU_PROPERTY_X86_UINT32_OR_LO
4904 && type
<= GNU_PROPERTY_X86_UINT32_OR_HI
)
4905 || (type
>= GNU_PROPERTY_X86_UINT32_OR_AND_LO
4906 && type
<= GNU_PROPERTY_X86_UINT32_OR_AND_HI
))
4908 if (p
->property
.u
.number
== 0
4909 && (type
== GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4910 || (type
>= GNU_PROPERTY_X86_UINT32_AND_LO
4911 && type
<= GNU_PROPERTY_X86_UINT32_AND_HI
)
4912 || (type
>= GNU_PROPERTY_X86_UINT32_OR_LO
4913 && type
<= GNU_PROPERTY_X86_UINT32_OR_HI
)))
4915 /* Remove empty property. */
4920 /* Keep LAM features only for 64-bit output. */
4921 if (type
== GNU_PROPERTY_X86_FEATURE_1_AND
4922 && !ABI_64_P (info
->output_bfd
))
4923 p
->property
.u
.number
&= ~(GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4924 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57
);
4928 else if (type
> GNU_PROPERTY_HIPROC
)
4930 /* The property list is sorted in order of type. */
4937 _bfd_elf_linker_x86_set_options (struct bfd_link_info
* info
,
4938 struct elf_linker_x86_params
*params
)
4940 const struct elf_backend_data
*bed
4941 = get_elf_backend_data (info
->output_bfd
);
4942 struct elf_x86_link_hash_table
*htab
4943 = elf_x86_hash_table (info
, bed
->target_id
);
4945 htab
->params
= params
;