[gdb/build, c++20] Handle deprecated std::allocator::construct
[binutils-gdb.git] / bfd / elfxx-x86.c
blobf224e8f1354dfa76e1a0b3d6bd3944930f9ae597
1 /* x86 specific support for ELF
2 Copyright (C) 2017-2023 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"
23 #include "objalloc.h"
25 /* The name of the dynamic interpreter. This is put in the .interp
26 section. */
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"
32 bool
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. */
45 void
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))
53 return;
55 bed = get_elf_backend_data (info->output_bfd);
56 htab = elf_x86_hash_table (info, bed->target_id);
57 if (htab == NULL)
58 return;
60 base = htab->tls_module_base;
61 if (base == NULL)
62 return;
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. */
71 bfd_vma
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)
76 return 0;
77 return elf_hash_table (info)->tls_sec->vma;
80 /* Allocate space in .plt, .got and associated reloc sections for
81 dynamic relocs. */
83 static bool
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)
95 return true;
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);
102 if (htab == NULL)
103 return false;
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
112 loop at run-time. */
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
120 reloctions. */
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
130 && h->def_regular)
132 /* GOTOFF relocation needs PLT. */
133 if (eh->gotoff_ref)
134 h->plt.refcount = 1;
136 if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h, &h->dyn_relocs,
137 plt_entry_size,
138 (htab->plt.has_plt0
139 * plt_entry_size),
140 htab->got_entry_size,
141 true))
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;
153 return true;
155 else
156 return false;
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. */
168 if (h->dynindx == -1
169 && !h->forced_local
170 && !resolved_to_zero
171 && h->root.type == bfd_link_hash_undefweak)
173 if (! bfd_elf_link_record_dynamic_symbol (info, h))
174 return false;
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;
183 bool use_plt;
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. */
188 if (s->size == 0)
189 s->size = htab->plt.has_plt0 * plt_entry_size;
191 if (use_plt_got)
192 eh->plt_got.offset = got_s->size;
193 else
195 h->plt.offset = s->size;
196 if (second_s)
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
206 function address. */
207 if (h->def_regular)
208 use_plt = false;
209 else if (htab->pcrel_plt)
210 use_plt = ! bfd_link_dll (info);
211 else
212 use_plt = bfd_link_pde (info);
213 if (use_plt)
215 if (use_plt_got)
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;
222 else
224 if (second_s)
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;
231 else
233 h->root.u.def.section = s;
234 h->root.u.def.value = h->plt.offset;
239 /* Make room for this entry. */
240 if (use_plt_got)
241 got_s->size += htab->non_lazy_plt->plt_entry_size;
242 else
244 s->size += plt_entry_size;
245 if (second_s)
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
250 script. */
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
258 section. */
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);
285 else
287 eh->plt_got.offset = (bfd_vma) -1;
288 h->plt.offset = (bfd_vma) -1;
289 h->needs_plt = 0;
292 else
294 eh->plt_got.offset = (bfd_vma) -1;
295 h->plt.offset = (bfd_vma) -1;
296 h->needs_plt = 0;
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)
307 && h->dynindx == -1
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)
312 asection *s;
313 bool dyn;
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. */
318 if (h->dynindx == -1
319 && !h->forced_local
320 && !resolved_to_zero
321 && h->root.type == bfd_link_hash_undefweak)
323 if (! bfd_elf_link_record_dynamic_symbol (info, h))
324 return false;
327 s = htab->elf.sgot;
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
341 slots. */
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;
376 else
377 h->got.offset = (bfd_vma) -1;
379 if (h->dyn_relocs == NULL)
380 return true;
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;
403 p->pc_count = 0;
404 if (p->count == 0)
405 *pp = p->next;
406 else
407 pp = &p->next;
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)
417 *pp = p->next;
418 else
419 pp = &p->next;
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
430 library. */
431 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
432 || resolved_to_zero)
434 if (bed->target_id == I386_ELF_DATA
435 && h->non_got_ref)
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)
443 *pp = p->next;
444 else
446 /* Remove non-R_386_PC32 relocation. */
447 p->count = p->pc_count;
448 pp = &p->next;
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))
456 return false;
458 else
459 h->dyn_relocs = NULL;
461 else if (h->dynindx == -1
462 && !h->forced_local
463 && !bfd_elf_link_record_dynamic_symbol (info, h))
464 return false;
466 else if (bfd_link_executable (info)
467 && (h->needs_copy || eh->needs_copy)
468 && h->def_dynamic
469 && !h->def_regular)
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)
479 *pp = p->next;
480 else
481 pp = &p->next;
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. */
493 if ((!h->non_got_ref
494 || (h->root.type == bfd_link_hash_undefweak
495 && !resolved_to_zero))
496 && ((h->def_dynamic
497 && !h->def_regular)
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. */
504 if (h->dynindx == -1
505 && !h->forced_local
506 && !resolved_to_zero
507 && h->root.type == bfd_link_hash_undefweak
508 && ! bfd_elf_link_record_dynamic_symbol (info, h))
509 return false;
511 /* If that succeeded, we know we'll be keeping all the
512 relocs. */
513 if (h->dynindx != -1)
514 goto keep;
517 h->dyn_relocs = NULL;
519 keep: ;
522 /* Finally, allocate space. */
523 for (p = h->dyn_relocs; p != NULL; p = p->next)
525 asection *sreloc;
527 if (eh->def_protected && bfd_link_executable (info))
529 /* Disallow copy relocation against non-copyable protected
530 symbol. */
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);
540 return false;
544 sreloc = elf_section_data (p->sec)->sreloc;
546 BFD_ASSERT (sreloc != NULL);
547 sreloc->size += p->count * htab->sizeof_reloc;
550 return true;
553 /* Allocate space in .plt, .got and associated reloc sections for
554 local dynamic relocs. */
556 static int
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
563 || !h->def_regular
564 || !h->ref_regular
565 || !h->forced_local
566 || h->root.type != bfd_link_hash_defined)
567 abort ();
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,
577 bool create)
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));
583 void **slot;
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);
590 if (!slot)
591 return NULL;
593 if (*slot)
595 ret = (struct elf_x86_link_hash_entry *) *slot;
596 return &ret->elf;
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));
602 if (ret)
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;
609 *slot = ret;
611 return &ret->elf;
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,
620 const char *string)
622 /* Allocate the structure if it has not already been allocated by a
623 subclass. */
624 if (entry == NULL)
626 entry = (struct bfd_hash_entry *)
627 bfd_hash_allocate (table,
628 sizeof (struct elf_x86_link_hash_entry));
629 if (entry == NULL)
630 return entry;
633 /* Call the allocation method of the superclass. */
634 entry = _bfd_link_hash_newfunc (entry, table, string);
635 if (entry != NULL)
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. */
646 eh->elf.indx = -1;
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. */
654 eh->elf.non_elf = 1;
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;
661 return entry;
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. */
669 hashval_t
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. */
692 static void
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);
705 static bool
706 elf_i386_is_reloc_section (const char *secname)
708 return startswith (secname, ".rel");
711 static bool
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);
727 if (ret == NULL)
728 return NULL;
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),
734 bed->target_id))
736 free (ret);
737 return NULL;
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->elf_append_reloc = elf_append_rela;
749 ret->elf_write_addend_in_got = _bfd_elf64_write_addend;
751 if (ABI_64_P (abfd))
753 ret->sizeof_reloc = sizeof (Elf64_External_Rela);
754 ret->pointer_r_type = R_X86_64_64;
755 ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
756 ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
757 ret->elf_write_addend = _bfd_elf64_write_addend;
759 else
761 if (bed->target_id == X86_64_ELF_DATA)
763 ret->sizeof_reloc = sizeof (Elf32_External_Rela);
764 ret->pointer_r_type = R_X86_64_32;
765 ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER;
766 ret->dynamic_interpreter_size
767 = sizeof ELFX32_DYNAMIC_INTERPRETER;
768 ret->elf_write_addend = _bfd_elf32_write_addend;
770 else
772 ret->is_reloc_section = elf_i386_is_reloc_section;
773 ret->sizeof_reloc = sizeof (Elf32_External_Rel);
774 ret->got_entry_size = 4;
775 ret->pcrel_plt = false;
776 ret->pointer_r_type = R_386_32;
777 ret->relative_r_type = R_386_RELATIVE;
778 ret->relative_r_name = "R_386_RELATIVE";
779 ret->elf_append_reloc = elf_append_rel;
780 ret->elf_write_addend = _bfd_elf32_write_addend;
781 ret->elf_write_addend_in_got = _bfd_elf32_write_addend;
782 ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
783 ret->dynamic_interpreter_size
784 = sizeof ELF32_DYNAMIC_INTERPRETER;
785 ret->tls_get_addr = "___tls_get_addr";
789 ret->loc_hash_table = htab_try_create (1024,
790 _bfd_x86_elf_local_htab_hash,
791 _bfd_x86_elf_local_htab_eq,
792 NULL);
793 ret->loc_hash_memory = objalloc_create ();
794 if (!ret->loc_hash_table || !ret->loc_hash_memory)
796 elf_x86_link_hash_table_free (abfd);
797 return NULL;
799 ret->elf.root.hash_table_free = elf_x86_link_hash_table_free;
801 return &ret->elf.root;
804 /* Sort relocs into address order. */
807 _bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
809 const arelent *a = * (const arelent **) ap;
810 const arelent *b = * (const arelent **) bp;
812 if (a->address > b->address)
813 return 1;
814 else if (a->address < b->address)
815 return -1;
816 else
817 return 0;
820 /* Mark symbol, NAME, as locally defined by linker if it is referenced
821 and not defined in a relocatable object file. */
823 static void
824 elf_x86_linker_defined (struct bfd_link_info *info, const char *name)
826 struct elf_link_hash_entry *h;
828 h = elf_link_hash_lookup (elf_hash_table (info), name,
829 false, false, false);
830 if (h == NULL)
831 return;
833 while (h->root.type == bfd_link_hash_indirect)
834 h = (struct elf_link_hash_entry *) h->root.u.i.link;
836 if (h->root.type == bfd_link_hash_new
837 || h->root.type == bfd_link_hash_undefined
838 || h->root.type == bfd_link_hash_undefweak
839 || h->root.type == bfd_link_hash_common
840 || (!h->def_regular && h->def_dynamic))
842 elf_x86_hash_entry (h)->local_ref = 2;
843 elf_x86_hash_entry (h)->linker_def = 1;
847 /* Hide a linker-defined symbol, NAME, with hidden visibility. */
849 static void
850 elf_x86_hide_linker_defined (struct bfd_link_info *info,
851 const char *name)
853 struct elf_link_hash_entry *h;
855 h = elf_link_hash_lookup (elf_hash_table (info), name,
856 false, false, false);
857 if (h == NULL)
858 return;
860 while (h->root.type == bfd_link_hash_indirect)
861 h = (struct elf_link_hash_entry *) h->root.u.i.link;
863 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
864 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
865 _bfd_elf_link_hash_hide_symbol (info, h, true);
868 bool
869 _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
871 if (!bfd_link_relocatable (info))
873 /* Check for __tls_get_addr reference. */
874 struct elf_x86_link_hash_table *htab;
875 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
876 htab = elf_x86_hash_table (info, bed->target_id);
877 if (htab)
879 struct elf_link_hash_entry *h;
881 h = elf_link_hash_lookup (elf_hash_table (info),
882 htab->tls_get_addr,
883 false, false, false);
884 if (h != NULL)
886 elf_x86_hash_entry (h)->tls_get_addr = 1;
888 /* Check the versioned __tls_get_addr symbol. */
889 while (h->root.type == bfd_link_hash_indirect)
891 h = (struct elf_link_hash_entry *) h->root.u.i.link;
892 elf_x86_hash_entry (h)->tls_get_addr = 1;
896 /* "__ehdr_start" will be defined by linker as a hidden symbol
897 later if it is referenced and not defined. */
898 elf_x86_linker_defined (info, "__ehdr_start");
900 if (bfd_link_executable (info))
902 /* References to __bss_start, _end and _edata should be
903 locally resolved within executables. */
904 elf_x86_linker_defined (info, "__bss_start");
905 elf_x86_linker_defined (info, "_end");
906 elf_x86_linker_defined (info, "_edata");
908 else
910 /* Hide hidden __bss_start, _end and _edata in shared
911 libraries. */
912 elf_x86_hide_linker_defined (info, "__bss_start");
913 elf_x86_hide_linker_defined (info, "_end");
914 elf_x86_hide_linker_defined (info, "_edata");
919 /* Invoke the regular ELF backend linker to do all the work. */
920 return _bfd_elf_link_check_relocs (abfd, info);
923 /* Look through the relocs for a section before allocation to make the
924 dynamic reloc section. */
926 bool
927 _bfd_x86_elf_check_relocs (bfd *abfd,
928 struct bfd_link_info *info,
929 asection *sec,
930 const Elf_Internal_Rela *relocs)
932 struct elf_x86_link_hash_table *htab;
933 Elf_Internal_Shdr *symtab_hdr;
934 struct elf_link_hash_entry **sym_hashes;
935 const Elf_Internal_Rela *rel;
936 const Elf_Internal_Rela *rel_end;
937 asection *sreloc;
938 const struct elf_backend_data *bed;
939 bool is_x86_64;
941 if (bfd_link_relocatable (info))
942 return true;
944 bed = get_elf_backend_data (abfd);
945 htab = elf_x86_hash_table (info, bed->target_id);
946 if (htab == NULL)
948 sec->check_relocs_failed = 1;
949 return false;
952 is_x86_64 = bed->target_id == X86_64_ELF_DATA;
954 symtab_hdr = &elf_symtab_hdr (abfd);
955 sym_hashes = elf_sym_hashes (abfd);
957 rel_end = relocs + sec->reloc_count;
958 for (rel = relocs; rel < rel_end; rel++)
960 unsigned int r_type;
961 unsigned int r_symndx;
962 struct elf_link_hash_entry *h;
964 r_symndx = htab->r_sym (rel->r_info);
965 r_type = ELF32_R_TYPE (rel->r_info);
967 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
969 /* xgettext:c-format */
970 _bfd_error_handler (_("%pB: bad symbol index: %d"),
971 abfd, r_symndx);
972 goto error_return;
975 if (r_symndx < symtab_hdr->sh_info)
976 h = NULL;
977 else
979 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
980 while (h->root.type == bfd_link_hash_indirect
981 || h->root.type == bfd_link_hash_warning)
982 h = (struct elf_link_hash_entry *) h->root.u.i.link;
985 if (X86_NEED_DYNAMIC_RELOC_TYPE_P (is_x86_64, r_type)
986 && NEED_DYNAMIC_RELOCATION_P (is_x86_64, info, true, h, sec,
987 r_type, htab->pointer_r_type))
989 /* We may copy these reloc types into the output file.
990 Create a reloc section in dynobj and make room for
991 this reloc. */
992 sreloc = _bfd_elf_make_dynamic_reloc_section
993 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
994 abfd, sec->use_rela_p);
996 if (sreloc != NULL)
997 return true;
999 error_return:
1000 sec->check_relocs_failed = 1;
1001 return false;
1005 return true;
1008 /* Add an entry to the relative reloc record. */
1010 static bool
1011 elf_x86_relative_reloc_record_add
1012 (struct bfd_link_info *info,
1013 struct elf_x86_relative_reloc_data *relative_reloc,
1014 Elf_Internal_Rela *rel, asection *sec,
1015 asection *sym_sec, struct elf_link_hash_entry *h,
1016 Elf_Internal_Sym *sym, bfd_vma offset, bool *keep_symbuf_p)
1018 bfd_size_type newidx;
1020 if (relative_reloc->data == NULL)
1022 relative_reloc->data = bfd_malloc
1023 (sizeof (struct elf_x86_relative_reloc_record));
1024 relative_reloc->count = 0;
1025 relative_reloc->size = 1;
1028 newidx = relative_reloc->count++;
1030 if (relative_reloc->count > relative_reloc->size)
1032 relative_reloc->size <<= 1;
1033 relative_reloc->data = bfd_realloc
1034 (relative_reloc->data,
1035 (relative_reloc->size
1036 * sizeof (struct elf_x86_relative_reloc_record)));
1039 if (relative_reloc->data == NULL)
1041 info->callbacks->einfo
1042 /* xgettext:c-format */
1043 (_("%F%P: %pB: failed to allocate relative reloc record\n"),
1044 info->output_bfd);
1045 return false;
1048 relative_reloc->data[newidx].rel = *rel;
1049 relative_reloc->data[newidx].sec = sec;
1050 if (h != NULL)
1052 /* Set SYM to NULL to indicate a global symbol. */
1053 relative_reloc->data[newidx].sym = NULL;
1054 relative_reloc->data[newidx].u.h = h;
1056 else
1058 relative_reloc->data[newidx].sym = sym;
1059 relative_reloc->data[newidx].u.sym_sec = sym_sec;
1060 /* We must keep the symbol buffer since SYM will be used later. */
1061 *keep_symbuf_p = true;
1063 relative_reloc->data[newidx].offset = offset;
1064 relative_reloc->data[newidx].address = 0;
1065 return true;
1068 /* After input sections have been mapped to output sections and
1069 addresses of output sections are set initiallly, scan input
1070 relocations with the same logic in relocate_section to determine
1071 if a relative relocation should be generated. Save the relative
1072 relocation candidate information for sizing the DT_RELR section
1073 later after all symbols addresses can be determined. */
1075 bool
1076 _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
1077 asection *input_section,
1078 struct bfd_link_info *info,
1079 bool *again)
1081 Elf_Internal_Shdr *symtab_hdr;
1082 Elf_Internal_Rela *internal_relocs;
1083 Elf_Internal_Rela *irel, *irelend;
1084 Elf_Internal_Sym *isymbuf = NULL;
1085 struct elf_link_hash_entry **sym_hashes;
1086 const struct elf_backend_data *bed;
1087 struct elf_x86_link_hash_table *htab;
1088 bfd_vma *local_got_offsets;
1089 bool is_x86_64;
1090 bool unaligned_section;
1091 bool return_status = false;
1092 bool keep_symbuf = false;
1094 if (bfd_link_relocatable (info))
1095 return true;
1097 /* Assume we're not going to change any sizes, and we'll only need
1098 one pass. */
1099 *again = false;
1101 bed = get_elf_backend_data (abfd);
1102 htab = elf_x86_hash_table (info, bed->target_id);
1103 if (htab == NULL)
1104 return true;
1106 /* Nothing to do if there are no relocations or relative relocations
1107 have been packed. */
1108 if (input_section == htab->elf.srelrdyn
1109 || input_section->relative_reloc_packed
1110 || ((input_section->flags & (SEC_RELOC | SEC_ALLOC))
1111 != (SEC_RELOC | SEC_ALLOC))
1112 || (input_section->flags & SEC_DEBUGGING) != 0
1113 || input_section->reloc_count == 0)
1114 return true;
1116 /* Skip if the section isn't aligned. */
1117 unaligned_section = input_section->alignment_power == 0;
1119 is_x86_64 = bed->target_id == X86_64_ELF_DATA;
1121 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1122 sym_hashes = elf_sym_hashes (abfd);
1123 local_got_offsets = elf_local_got_offsets (abfd);
1125 /* Load the relocations for this section. */
1126 internal_relocs =
1127 _bfd_elf_link_info_read_relocs (abfd, info, input_section, NULL,
1128 (Elf_Internal_Rela *) NULL,
1129 info->keep_memory);
1130 if (internal_relocs == NULL)
1131 return false;
1133 irelend = internal_relocs + input_section->reloc_count;
1134 for (irel = internal_relocs; irel < irelend; irel++)
1136 unsigned int r_type;
1137 unsigned int r_symndx;
1138 Elf_Internal_Sym *isym;
1139 struct elf_link_hash_entry *h;
1140 struct elf_x86_link_hash_entry *eh;
1141 bfd_vma offset;
1142 bool resolved_to_zero;
1143 bool need_copy_reloc_in_pie;
1144 bool pc32_reloc;
1145 asection *sec;
1146 /* Offset must be a multiple of 2. */
1147 bool unaligned_offset = (irel->r_offset & 1) != 0;
1148 /* True if there is a relative relocation against a dynamic
1149 symbol. */
1150 bool dynamic_relative_reloc_p;
1152 /* Get the value of the symbol referred to by the reloc. */
1153 r_symndx = htab->r_sym (irel->r_info);
1155 r_type = ELF32_R_TYPE (irel->r_info);
1156 /* Clear the R_X86_64_converted_reloc_bit bit. */
1157 r_type &= ~R_X86_64_converted_reloc_bit;
1159 sec = NULL;
1160 h = NULL;
1161 dynamic_relative_reloc_p = false;
1163 if (r_symndx < symtab_hdr->sh_info)
1165 /* Read this BFD's local symbols. */
1166 if (isymbuf == NULL)
1168 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1169 if (isymbuf == NULL)
1171 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1172 symtab_hdr->sh_info,
1173 0, NULL, NULL, NULL);
1174 if (isymbuf == NULL)
1175 goto error_return;
1179 isym = isymbuf + r_symndx;
1180 switch (isym->st_shndx)
1182 case SHN_ABS:
1183 sec = bfd_abs_section_ptr;
1184 break;
1185 case SHN_COMMON:
1186 sec = bfd_com_section_ptr;
1187 break;
1188 case SHN_X86_64_LCOMMON:
1189 if (!is_x86_64)
1190 abort ();
1191 sec = &_bfd_elf_large_com_section;
1192 break;
1193 default:
1194 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1195 break;
1198 /* Skip relocation against local STT_GNU_IFUNC symbol. */
1199 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1200 continue;
1202 eh = (struct elf_x86_link_hash_entry *) h;
1203 resolved_to_zero = false;
1205 else
1207 /* Get H and SEC for GENERATE_DYNAMIC_RELOCATION_P below. */
1208 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1209 while (h->root.type == bfd_link_hash_indirect
1210 || h->root.type == bfd_link_hash_warning)
1211 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1213 if (h->root.type == bfd_link_hash_defined
1214 || h->root.type == bfd_link_hash_defweak)
1215 sec = h->root.u.def.section;
1217 /* Skip relocation against STT_GNU_IFUNC symbol. */
1218 if (h->type == STT_GNU_IFUNC)
1219 continue;
1221 eh = (struct elf_x86_link_hash_entry *) h;
1222 resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
1224 /* NB: See how elf_backend_finish_dynamic_symbol is called
1225 from elf_link_output_extsym. */
1226 if ((h->dynindx != -1 || h->forced_local)
1227 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1228 || h->root.type != bfd_link_hash_undefweak)
1229 || !h->forced_local)
1230 && h->got.offset != (bfd_vma) -1
1231 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
1232 && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
1233 && !resolved_to_zero
1234 && SYMBOL_REFERENCES_LOCAL_P (info, h)
1235 && SYMBOL_DEFINED_NON_SHARED_P (h))
1236 dynamic_relative_reloc_p = true;
1238 isym = NULL;
1241 if (X86_GOT_TYPE_P (is_x86_64, r_type))
1243 /* Pack GOT relative relocations. There should be only a
1244 single R_*_RELATIVE relocation in GOT. */
1245 if (eh != NULL)
1247 if (eh->got_relative_reloc_done)
1248 continue;
1250 if (!(dynamic_relative_reloc_p
1251 || (RESOLVED_LOCALLY_P (info, h, htab)
1252 && GENERATE_RELATIVE_RELOC_P (info, h))))
1253 continue;
1255 if (!dynamic_relative_reloc_p)
1256 eh->no_finish_dynamic_symbol = 1;
1257 eh->got_relative_reloc_done = 1;
1258 offset = h->got.offset;
1260 else
1262 if (elf_x86_relative_reloc_done (abfd)[r_symndx])
1263 continue;
1265 if (!X86_LOCAL_GOT_RELATIVE_RELOC_P (is_x86_64, info,
1266 isym))
1267 continue;
1269 elf_x86_relative_reloc_done (abfd)[r_symndx] = 1;
1270 offset = local_got_offsets[r_symndx];
1273 if (!elf_x86_relative_reloc_record_add (info,
1274 &htab->relative_reloc,
1275 irel, htab->elf.sgot,
1276 sec, h, isym, offset,
1277 &keep_symbuf))
1278 goto error_return;
1280 continue;
1283 if (is_x86_64
1284 && irel->r_addend == 0
1285 && !ABI_64_P (info->output_bfd))
1287 /* For x32, if addend is zero, treat R_X86_64_64 like
1288 R_X86_64_32 and R_X86_64_SIZE64 like R_X86_64_SIZE32. */
1289 if (r_type == R_X86_64_64)
1290 r_type = R_X86_64_32;
1291 else if (r_type == R_X86_64_SIZE64)
1292 r_type = R_X86_64_SIZE32;
1295 if (!X86_RELATIVE_RELOC_TYPE_P (is_x86_64, r_type))
1296 continue;
1298 /* Pack non-GOT relative relocations. */
1299 if (is_x86_64)
1301 need_copy_reloc_in_pie =
1302 (bfd_link_pie (info)
1303 && h != NULL
1304 && (h->needs_copy
1305 || eh->needs_copy
1306 || (h->root.type == bfd_link_hash_undefined))
1307 && (X86_PCREL_TYPE_P (true, r_type)
1308 || X86_SIZE_TYPE_P (true, r_type)));
1309 pc32_reloc = false;
1311 else
1313 need_copy_reloc_in_pie = false;
1314 pc32_reloc = r_type == R_386_PC32;
1317 if (GENERATE_DYNAMIC_RELOCATION_P (is_x86_64, info, eh, r_type,
1318 sec, need_copy_reloc_in_pie,
1319 resolved_to_zero, pc32_reloc))
1321 /* When generating a shared object, these relocations
1322 are copied into the output file to be resolved at run
1323 time. */
1324 offset = _bfd_elf_section_offset (info->output_bfd, info,
1325 input_section,
1326 irel->r_offset);
1327 if (offset == (bfd_vma) -1
1328 || offset == (bfd_vma) -2
1329 || COPY_INPUT_RELOC_P (is_x86_64, info, h, r_type))
1330 continue;
1332 /* This symbol is local, or marked to become local. When
1333 relocation overflow check is disabled, we convert
1334 R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
1335 if (is_x86_64
1336 && !(r_type == htab->pointer_r_type
1337 || (r_type == R_X86_64_32
1338 && htab->params->no_reloc_overflow_check)))
1339 continue;
1341 if (!elf_x86_relative_reloc_record_add
1342 (info,
1343 ((unaligned_section || unaligned_offset)
1344 ? &htab->unaligned_relative_reloc
1345 : &htab->relative_reloc),
1346 irel, input_section, sec, h, isym, offset,
1347 &keep_symbuf))
1348 goto error_return;
1352 input_section->relative_reloc_packed = 1;
1354 return_status = true;
1356 error_return:
1357 if ((unsigned char *) isymbuf != symtab_hdr->contents)
1359 /* Cache the symbol buffer if it must be kept. */
1360 if (keep_symbuf)
1361 symtab_hdr->contents = (unsigned char *) isymbuf;
1362 else
1363 free (isymbuf);
1365 if (elf_section_data (input_section)->relocs != internal_relocs)
1366 free (internal_relocs);
1367 return return_status;
1370 /* Add an entry to the 64-bit DT_RELR bitmap. */
1372 static void
1373 elf64_dt_relr_bitmap_add
1374 (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
1375 uint64_t entry)
1377 bfd_size_type newidx;
1379 if (bitmap->u.elf64 == NULL)
1381 bitmap->u.elf64 = bfd_malloc (sizeof (uint64_t));
1382 bitmap->count = 0;
1383 bitmap->size = 1;
1386 newidx = bitmap->count++;
1388 if (bitmap->count > bitmap->size)
1390 bitmap->size <<= 1;
1391 bitmap->u.elf64 = bfd_realloc (bitmap->u.elf64,
1392 (bitmap->size * sizeof (uint64_t)));
1395 if (bitmap->u.elf64 == NULL)
1397 info->callbacks->einfo
1398 /* xgettext:c-format */
1399 (_("%F%P: %pB: failed to allocate 64-bit DT_RELR bitmap\n"),
1400 info->output_bfd);
1403 bitmap->u.elf64[newidx] = entry;
1406 /* Add an entry to the 32-bit DT_RELR bitmap. */
1408 static void
1409 elf32_dt_relr_bitmap_add
1410 (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
1411 uint32_t entry)
1413 bfd_size_type newidx;
1415 if (bitmap->u.elf32 == NULL)
1417 bitmap->u.elf32 = bfd_malloc (sizeof (uint32_t));
1418 bitmap->count = 0;
1419 bitmap->size = 1;
1422 newidx = bitmap->count++;
1424 if (bitmap->count > bitmap->size)
1426 bitmap->size <<= 1;
1427 bitmap->u.elf32 = bfd_realloc (bitmap->u.elf32,
1428 (bitmap->size * sizeof (uint32_t)));
1431 if (bitmap->u.elf32 == NULL)
1433 info->callbacks->einfo
1434 /* xgettext:c-format */
1435 (_("%F%P: %pB: failed to allocate 32-bit DT_RELR bitmap\n"),
1436 info->output_bfd);
1439 bitmap->u.elf32[newidx] = entry;
1442 void
1443 _bfd_elf32_write_addend (bfd *abfd, uint64_t value, void *addr)
1445 bfd_put_32 (abfd, value, addr);
1448 void
1449 _bfd_elf64_write_addend (bfd *abfd, uint64_t value, void *addr)
1451 bfd_put_64 (abfd, value, addr);
1454 /* Size or finish relative relocations to determine the run-time
1455 addresses for DT_RELR bitmap computation later. OUTREL is set
1456 to NULL in the sizing phase and non-NULL in the finising phase
1457 where the regular relative relocations will be written out. */
1459 static void
1460 elf_x86_size_or_finish_relative_reloc
1461 (bool is_x86_64, struct bfd_link_info *info,
1462 struct elf_x86_link_hash_table *htab, bool unaligned,
1463 Elf_Internal_Rela *outrel)
1465 unsigned int align_mask;
1466 bfd_size_type i, count;
1467 asection *sec, *srel;
1468 struct elf_link_hash_entry *h;
1469 bfd_vma offset;
1470 Elf_Internal_Sym *sym;
1471 asection *sym_sec;
1472 asection *sgot = htab->elf.sgot;
1473 asection *srelgot = htab->elf.srelgot;
1474 struct elf_x86_relative_reloc_data *relative_reloc;
1476 if (unaligned)
1478 align_mask = 0;
1479 relative_reloc = &htab->unaligned_relative_reloc;
1481 else
1483 align_mask = 1;
1484 relative_reloc = &htab->relative_reloc;
1487 count = relative_reloc->count;
1488 for (i = 0; i < count; i++)
1490 sec = relative_reloc->data[i].sec;
1491 sym = relative_reloc->data[i].sym;
1493 /* If SYM is NULL, it must be a global symbol. */
1494 if (sym == NULL)
1495 h = relative_reloc->data[i].u.h;
1496 else
1497 h = NULL;
1499 if (is_x86_64)
1501 bfd_vma relocation;
1502 /* This function may be called more than once and REL may be
1503 updated by _bfd_elf_rela_local_sym below. */
1504 Elf_Internal_Rela rel = relative_reloc->data[i].rel;
1506 if (h != NULL)
1508 if (h->root.type == bfd_link_hash_defined
1509 || h->root.type == bfd_link_hash_defweak)
1511 sym_sec = h->root.u.def.section;
1512 relocation = (h->root.u.def.value
1513 + sym_sec->output_section->vma
1514 + sym_sec->output_offset);
1516 else
1518 /* Allow undefined symbol only at the sizing phase.
1519 Otherwise skip undefined symbol here. Undefined
1520 symbol will be reported by relocate_section. */
1521 if (outrel == NULL)
1522 relocation = 0;
1523 else
1524 continue;
1527 else
1529 sym_sec = relative_reloc->data[i].u.sym_sec;
1530 relocation = _bfd_elf_rela_local_sym
1531 (info->output_bfd, sym, &sym_sec, &rel);
1534 if (outrel != NULL)
1536 outrel->r_addend = relocation;
1537 if (sec == sgot)
1539 if (h != NULL && h->needs_plt)
1540 abort ();
1542 else
1543 outrel->r_addend += rel.r_addend;
1545 /* Write the implicit addend if ALIGN_MASK isn't 0. */
1546 if (align_mask)
1548 if (sec == sgot)
1550 if (relative_reloc->data[i].offset >= sec->size)
1551 abort ();
1552 htab->elf_write_addend_in_got
1553 (info->output_bfd, outrel->r_addend,
1554 sec->contents + relative_reloc->data[i].offset);
1556 else
1558 bfd_byte *contents;
1560 if (rel.r_offset >= sec->size)
1561 abort ();
1563 if (elf_section_data (sec)->this_hdr.contents
1564 != NULL)
1565 contents
1566 = elf_section_data (sec)->this_hdr.contents;
1567 else
1569 if (!bfd_malloc_and_get_section (sec->owner,
1570 sec,
1571 &contents))
1572 info->callbacks->einfo
1573 /* xgettext:c-format */
1574 (_("%F%P: %pB: failed to allocate memory for section `%pA'\n"),
1575 info->output_bfd, sec);
1577 /* Cache the section contents for
1578 elf_link_input_bfd. */
1579 elf_section_data (sec)->this_hdr.contents
1580 = contents;
1582 htab->elf_write_addend
1583 (info->output_bfd, outrel->r_addend,
1584 contents + rel.r_offset);
1590 if (sec == sgot)
1591 srel = srelgot;
1592 else
1593 srel = elf_section_data (sec)->sreloc;
1594 offset = (sec->output_section->vma + sec->output_offset
1595 + relative_reloc->data[i].offset);
1596 relative_reloc->data[i].address = offset;
1597 if (outrel != NULL)
1599 outrel->r_offset = offset;
1601 if ((outrel->r_offset & align_mask) != 0)
1602 abort ();
1604 if (htab->params->report_relative_reloc)
1605 _bfd_x86_elf_link_report_relative_reloc
1606 (info, sec, h, sym, htab->relative_r_name, outrel);
1608 /* Generate regular relative relocation if ALIGN_MASK is 0. */
1609 if (align_mask == 0)
1610 htab->elf_append_reloc (info->output_bfd, srel, outrel);
1615 /* Compute the DT_RELR section size. Set NEED_PLAYOUT to true if
1616 the DT_RELR section size has been increased. */
1618 static void
1619 elf_x86_compute_dl_relr_bitmap
1620 (struct bfd_link_info *info, struct elf_x86_link_hash_table *htab,
1621 bool *need_layout)
1623 bfd_vma base;
1624 bfd_size_type i, count, new_count;
1625 struct elf_x86_relative_reloc_data *relative_reloc =
1626 &htab->relative_reloc;
1627 /* Save the old DT_RELR bitmap count. Don't shrink the DT_RELR bitmap
1628 if the new DT_RELR bitmap count is smaller than the old one. Pad
1629 with trailing 1s which won't be decoded to more relocations. */
1630 bfd_size_type dt_relr_bitmap_count = htab->dt_relr_bitmap.count;
1632 /* Clear the DT_RELR bitmap count. */
1633 htab->dt_relr_bitmap.count = 0;
1635 count = relative_reloc->count;
1637 if (ABI_64_P (info->output_bfd))
1639 /* Compute the 64-bit DT_RELR bitmap. */
1640 i = 0;
1641 while (i < count)
1643 if ((relative_reloc->data[i].address % 1) != 0)
1644 abort ();
1646 elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1647 relative_reloc->data[i].address);
1649 base = relative_reloc->data[i].address + 8;
1650 i++;
1652 while (i < count)
1654 uint64_t bitmap = 0;
1655 for (; i < count; i++)
1657 bfd_vma delta = (relative_reloc->data[i].address
1658 - base);
1659 /* Stop if it is too far from base. */
1660 if (delta >= 63 * 8)
1661 break;
1662 /* Stop if it isn't a multiple of 8. */
1663 if ((delta % 8) != 0)
1664 break;
1665 bitmap |= 1ULL << (delta / 8);
1668 if (bitmap == 0)
1669 break;
1671 elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1672 (bitmap << 1) | 1);
1674 base += 63 * 8;
1678 new_count = htab->dt_relr_bitmap.count;
1679 if (dt_relr_bitmap_count > new_count)
1681 /* Don't shrink the DT_RELR section size to avoid section
1682 layout oscillation. Instead, pad the DT_RELR bitmap with
1683 1s which do not decode to more relocations. */
1685 htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
1686 count = dt_relr_bitmap_count - new_count;
1687 for (i = 0; i < count; i++)
1688 htab->dt_relr_bitmap.u.elf64[new_count + i] = 1;
1691 else
1693 /* Compute the 32-bit DT_RELR bitmap. */
1694 i = 0;
1695 while (i < count)
1697 if ((relative_reloc->data[i].address % 1) != 0)
1698 abort ();
1700 elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1701 relative_reloc->data[i].address);
1703 base = relative_reloc->data[i].address + 4;
1704 i++;
1706 while (i < count)
1708 uint32_t bitmap = 0;
1709 for (; i < count; i++)
1711 bfd_vma delta = (relative_reloc->data[i].address
1712 - base);
1713 /* Stop if it is too far from base. */
1714 if (delta >= 31 * 4)
1715 break;
1716 /* Stop if it isn't a multiple of 4. */
1717 if ((delta % 4) != 0)
1718 break;
1719 bitmap |= 1ULL << (delta / 4);
1722 if (bitmap == 0)
1723 break;
1725 elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1726 (bitmap << 1) | 1);
1728 base += 31 * 4;
1732 new_count = htab->dt_relr_bitmap.count;
1733 if (dt_relr_bitmap_count > new_count)
1735 /* Don't shrink the DT_RELR section size to avoid section
1736 layout oscillation. Instead, pad the DT_RELR bitmap with
1737 1s which do not decode to more relocations. */
1739 htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
1740 count = dt_relr_bitmap_count - new_count;
1741 for (i = 0; i < count; i++)
1742 htab->dt_relr_bitmap.u.elf32[new_count + i] = 1;
1746 if (htab->dt_relr_bitmap.count != dt_relr_bitmap_count)
1748 if (need_layout)
1750 /* The .relr.dyn section size is changed. Update the section
1751 size and tell linker to layout sections again. */
1752 htab->elf.srelrdyn->size =
1753 (htab->dt_relr_bitmap.count
1754 * (ABI_64_P (info->output_bfd) ? 8 : 4));
1756 *need_layout = true;
1758 else
1759 info->callbacks->einfo
1760 /* xgettext:c-format */
1761 (_("%F%P: %pB: size of compact relative reloc section is "
1762 "changed: new (%lu) != old (%lu)\n"),
1763 info->output_bfd, htab->dt_relr_bitmap.count,
1764 dt_relr_bitmap_count);
1768 /* Write out the DT_RELR section. */
1770 static void
1771 elf_x86_write_dl_relr_bitmap (struct bfd_link_info *info,
1772 struct elf_x86_link_hash_table *htab)
1774 asection *sec = htab->elf.srelrdyn;
1775 bfd_size_type size = sec->size;
1776 bfd_size_type i;
1777 unsigned char *contents;
1779 contents = (unsigned char *) bfd_alloc (sec->owner, size);
1780 if (contents == NULL)
1781 info->callbacks->einfo
1782 /* xgettext:c-format */
1783 (_("%F%P: %pB: failed to allocate compact relative reloc section\n"),
1784 info->output_bfd);
1786 /* Cache the section contents for elf_link_input_bfd. */
1787 sec->contents = contents;
1789 if (ABI_64_P (info->output_bfd))
1790 for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 8)
1791 bfd_put_64 (info->output_bfd, htab->dt_relr_bitmap.u.elf64[i],
1792 contents);
1793 else
1794 for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 4)
1795 bfd_put_32 (info->output_bfd, htab->dt_relr_bitmap.u.elf32[i],
1796 contents);
1799 /* Sort relative relocations by address. */
1801 static int
1802 elf_x86_relative_reloc_compare (const void *pa, const void *pb)
1804 struct elf_x86_relative_reloc_record *a =
1805 (struct elf_x86_relative_reloc_record *) pa;
1806 struct elf_x86_relative_reloc_record *b =
1807 (struct elf_x86_relative_reloc_record *) pb;
1808 if (a->address < b->address)
1809 return -1;
1810 if (a->address > b->address)
1811 return 1;
1812 return 0;
1815 enum dynobj_sframe_plt_type
1817 SFRAME_PLT = 1,
1818 SFRAME_PLT_SEC = 2
1821 /* Create SFrame stack trace info for the plt entries in the .plt section
1822 of type PLT_SEC_TYPE. */
1824 static bool
1825 _bfd_x86_elf_create_sframe_plt (bfd *output_bfd,
1826 struct bfd_link_info *info,
1827 unsigned int plt_sec_type)
1829 struct elf_x86_link_hash_table *htab;
1830 const struct elf_backend_data *bed;
1832 bool plt0_generated_p;
1833 unsigned int plt0_entry_size;
1834 unsigned char func_info;
1835 uint32_t fre_type;
1836 /* The dynamic plt section for which .sframe stack trace information is being
1837 created. */
1838 asection *dpltsec;
1840 int err = 0;
1842 sframe_encoder_ctx **ectx = NULL;
1843 unsigned plt_entry_size = 0;
1844 unsigned int num_pltn_fres = 0;
1845 unsigned int num_pltn_entries = 0;
1847 bed = get_elf_backend_data (output_bfd);
1848 htab = elf_x86_hash_table (info, bed->target_id);
1849 /* Whether SFrame stack trace info for plt0 is to be generated. */
1850 plt0_generated_p = htab->plt.has_plt0;
1851 plt0_entry_size
1852 = (plt0_generated_p) ? htab->sframe_plt->plt0_entry_size : 0;
1854 switch (plt_sec_type)
1856 case SFRAME_PLT:
1858 ectx = &htab->plt_cfe_ctx;
1859 dpltsec = htab->elf.splt;
1861 plt_entry_size = htab->plt.plt_entry_size;
1862 num_pltn_fres = htab->sframe_plt->pltn_num_fres;
1863 num_pltn_entries
1864 = (htab->elf.splt->size - plt0_entry_size) / plt_entry_size;
1866 break;
1868 case SFRAME_PLT_SEC:
1870 ectx = &htab->plt_second_cfe_ctx;
1871 /* FIXME - this or htab->plt_second_sframe ? */
1872 dpltsec = htab->plt_second_eh_frame;
1874 plt_entry_size = htab->sframe_plt->sec_pltn_entry_size;
1875 num_pltn_fres = htab->sframe_plt->sec_pltn_num_fres;
1876 num_pltn_entries
1877 = htab->plt_second_eh_frame->size / plt_entry_size;
1878 break;
1880 default:
1881 /* No other value is possible. */
1882 return false;
1883 break;
1886 *ectx = sframe_encode (SFRAME_VERSION_2,
1888 SFRAME_ABI_AMD64_ENDIAN_LITTLE,
1889 SFRAME_CFA_FIXED_FP_INVALID,
1890 -8, /* Fixed RA offset. */
1891 &err);
1893 /* FRE type is dependent on the size of the function. */
1894 fre_type = sframe_calc_fre_type (dpltsec->size);
1895 func_info = sframe_fde_create_func_info (fre_type, SFRAME_FDE_TYPE_PCINC);
1897 /* Add SFrame FDE and the associated FREs for plt0 if plt0 has been
1898 generated. */
1899 if (plt0_generated_p)
1901 /* Add SFrame FDE for plt0, the function start address is updated later
1902 at _bfd_elf_merge_section_sframe time. */
1903 sframe_encoder_add_funcdesc_v2 (*ectx,
1904 0, /* func start addr. */
1905 plt0_entry_size,
1906 func_info,
1908 0 /* Num FREs. */);
1909 sframe_frame_row_entry plt0_fre;
1910 unsigned int num_plt0_fres = htab->sframe_plt->plt0_num_fres;
1911 for (unsigned int j = 0; j < num_plt0_fres; j++)
1913 plt0_fre = *(htab->sframe_plt->plt0_fres[j]);
1914 sframe_encoder_add_fre (*ectx, 0, &plt0_fre);
1919 if (num_pltn_entries)
1921 /* pltn entries use an SFrame FDE of type
1922 SFRAME_FDE_TYPE_PCMASK to exploit the repetitive
1923 pattern of the instructions in these entries. Using this SFrame FDE
1924 type helps in keeping the SFrame stack trace info for pltn entries
1925 compact. */
1926 func_info = sframe_fde_create_func_info (fre_type,
1927 SFRAME_FDE_TYPE_PCMASK);
1928 /* Add the SFrame FDE for all PCs starting at the first pltn entry (hence,
1929 function start address = plt0_entry_size. As usual, this will be
1930 updated later at _bfd_elf_merge_section_sframe, by when the
1931 sections are relocated. */
1932 sframe_encoder_add_funcdesc_v2 (*ectx,
1933 plt0_entry_size, /* func start addr. */
1934 dpltsec->size - plt0_entry_size,
1935 func_info,
1937 0 /* Num FREs. */);
1939 sframe_frame_row_entry pltn_fre;
1940 /* Now add the FREs for pltn. Simply adding the two FREs suffices due
1941 to the usage of SFRAME_FDE_TYPE_PCMASK above. */
1942 for (unsigned int j = 0; j < num_pltn_fres; j++)
1944 pltn_fre = *(htab->sframe_plt->pltn_fres[j]);
1945 sframe_encoder_add_fre (*ectx, 1, &pltn_fre);
1949 return true;
1952 /* Put contents of the .sframe section corresponding to the specified
1953 PLT_SEC_TYPE. */
1955 static bool
1956 _bfd_x86_elf_write_sframe_plt (bfd *output_bfd,
1957 struct bfd_link_info *info,
1958 unsigned int plt_sec_type)
1960 struct elf_x86_link_hash_table *htab;
1961 const struct elf_backend_data *bed;
1962 sframe_encoder_ctx *ectx;
1963 size_t sec_size;
1964 asection *sec;
1965 bfd *dynobj;
1967 int err = 0;
1969 bed = get_elf_backend_data (output_bfd);
1970 htab = elf_x86_hash_table (info, bed->target_id);
1971 dynobj = htab->elf.dynobj;
1973 switch (plt_sec_type)
1975 case SFRAME_PLT:
1976 ectx = htab->plt_cfe_ctx;
1977 sec = htab->plt_sframe;
1978 break;
1979 case SFRAME_PLT_SEC:
1980 ectx = htab->plt_second_cfe_ctx;
1981 sec = htab->plt_second_sframe;
1982 break;
1983 default:
1984 /* No other value is possible. */
1985 return false;
1986 break;
1989 BFD_ASSERT (ectx);
1991 void *contents = sframe_encoder_write (ectx, &sec_size, &err);
1993 sec->size = (bfd_size_type) sec_size;
1994 sec->contents = (unsigned char *) bfd_zalloc (dynobj, sec->size);
1995 memcpy (sec->contents, contents, sec_size);
1997 sframe_encoder_free (&ectx);
1999 return true;
2002 bool
2003 _bfd_elf_x86_size_relative_relocs (struct bfd_link_info *info,
2004 bool *need_layout)
2006 struct elf_x86_link_hash_table *htab;
2007 const struct elf_backend_data *bed;
2008 bool is_x86_64;
2009 bfd_size_type i, count, unaligned_count;
2010 asection *sec, *srel;
2012 /* Do nothing for ld -r. */
2013 if (bfd_link_relocatable (info))
2014 return true;
2016 bed = get_elf_backend_data (info->output_bfd);
2017 htab = elf_x86_hash_table (info, bed->target_id);
2018 if (htab == NULL)
2019 return false;
2021 count = htab->relative_reloc.count;
2022 unaligned_count = htab->unaligned_relative_reloc.count;
2023 if (count == 0)
2025 if (htab->generate_relative_reloc_pass == 0
2026 && htab->elf.srelrdyn != NULL)
2028 /* Remove the empty .relr.dyn sections now. */
2029 if (!bfd_is_abs_section (htab->elf.srelrdyn->output_section))
2031 bfd_section_list_remove
2032 (info->output_bfd, htab->elf.srelrdyn->output_section);
2033 info->output_bfd->section_count--;
2035 bfd_section_list_remove (htab->elf.srelrdyn->owner,
2036 htab->elf.srelrdyn);
2037 htab->elf.srelrdyn->owner->section_count--;
2039 if (unaligned_count == 0)
2041 htab->generate_relative_reloc_pass++;
2042 return true;
2046 is_x86_64 = bed->target_id == X86_64_ELF_DATA;
2048 /* Size relative relocations. */
2049 if (htab->generate_relative_reloc_pass)
2051 /* Reset the regular relative relocation count. */
2052 for (i = 0; i < unaligned_count; i++)
2054 sec = htab->unaligned_relative_reloc.data[i].sec;
2055 srel = elf_section_data (sec)->sreloc;
2056 srel->reloc_count = 0;
2059 else
2061 /* Remove the reserved space for compact relative relocations. */
2062 if (count)
2064 asection *sgot = htab->elf.sgot;
2065 asection *srelgot = htab->elf.srelgot;
2067 for (i = 0; i < count; i++)
2069 sec = htab->relative_reloc.data[i].sec;
2070 if (sec == sgot)
2071 srel = srelgot;
2072 else
2073 srel = elf_section_data (sec)->sreloc;
2074 srel->size -= htab->sizeof_reloc;
2079 /* Size unaligned relative relocations. */
2080 if (unaligned_count)
2081 elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2082 true, NULL);
2084 if (count)
2086 elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2087 false, NULL);
2089 /* Sort relative relocations by addresses. We only need to
2090 sort them in the first pass since the relative positions
2091 won't change. */
2092 if (htab->generate_relative_reloc_pass == 0)
2093 qsort (htab->relative_reloc.data, count,
2094 sizeof (struct elf_x86_relative_reloc_record),
2095 elf_x86_relative_reloc_compare);
2097 elf_x86_compute_dl_relr_bitmap (info, htab, need_layout);
2100 htab->generate_relative_reloc_pass++;
2102 return true;
2105 bool
2106 _bfd_elf_x86_finish_relative_relocs (struct bfd_link_info *info)
2108 struct elf_x86_link_hash_table *htab;
2109 const struct elf_backend_data *bed;
2110 Elf_Internal_Rela outrel;
2111 bool is_x86_64;
2112 bfd_size_type count;
2114 /* Do nothing for ld -r. */
2115 if (bfd_link_relocatable (info))
2116 return true;
2118 bed = get_elf_backend_data (info->output_bfd);
2119 htab = elf_x86_hash_table (info, bed->target_id);
2120 if (htab == NULL)
2121 return false;
2123 is_x86_64 = bed->target_id == X86_64_ELF_DATA;
2125 outrel.r_info = htab->r_info (0, htab->relative_r_type);
2127 if (htab->unaligned_relative_reloc.count)
2128 elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2129 true, &outrel);
2131 count = htab->relative_reloc.count;
2132 if (count)
2134 elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2135 false, &outrel);
2137 elf_x86_compute_dl_relr_bitmap (info, htab, NULL);
2139 elf_x86_write_dl_relr_bitmap (info, htab);
2142 return true;
2145 bool
2146 _bfd_elf_x86_valid_reloc_p (asection *input_section,
2147 struct bfd_link_info *info,
2148 struct elf_x86_link_hash_table *htab,
2149 const Elf_Internal_Rela *rel,
2150 struct elf_link_hash_entry *h,
2151 Elf_Internal_Sym *sym,
2152 Elf_Internal_Shdr *symtab_hdr,
2153 bool *no_dynreloc_p)
2155 bool valid_p = true;
2157 *no_dynreloc_p = false;
2159 /* Check If relocation against non-preemptible absolute symbol is
2160 valid in PIC. FIXME: Can't use SYMBOL_REFERENCES_LOCAL_P since
2161 it may call _bfd_elf_link_hide_sym_by_version and result in
2162 ld-elfvers/ vers21 test failure. */
2163 if (bfd_link_pic (info)
2164 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
2166 const struct elf_backend_data *bed;
2167 unsigned int r_type;
2168 Elf_Internal_Rela irel;
2170 /* Skip non-absolute symbol. */
2171 if (h)
2173 if (!ABS_SYMBOL_P (h))
2174 return valid_p;
2176 else if (sym->st_shndx != SHN_ABS)
2177 return valid_p;
2179 bed = get_elf_backend_data (input_section->owner);
2180 r_type = ELF32_R_TYPE (rel->r_info);
2181 irel = *rel;
2183 /* Only allow relocations against absolute symbol, which can be
2184 resolved as absolute value + addend. GOTPCREL and GOT32
2185 relocations are allowed since absolute value + addend is
2186 stored in the GOT slot. */
2187 if (bed->target_id == X86_64_ELF_DATA)
2189 r_type &= ~R_X86_64_converted_reloc_bit;
2190 valid_p = (r_type == R_X86_64_64
2191 || r_type == R_X86_64_32
2192 || r_type == R_X86_64_32S
2193 || r_type == R_X86_64_16
2194 || r_type == R_X86_64_8
2195 || r_type == R_X86_64_GOTPCREL
2196 || r_type == R_X86_64_GOTPCRELX
2197 || r_type == R_X86_64_REX_GOTPCRELX);
2198 if (!valid_p)
2200 unsigned int r_symndx = htab->r_sym (rel->r_info);
2201 irel.r_info = htab->r_info (r_symndx, r_type);
2204 else
2205 valid_p = (r_type == R_386_32
2206 || r_type == R_386_16
2207 || r_type == R_386_8
2208 || r_type == R_386_GOT32
2209 || r_type == R_386_GOT32X);
2211 if (valid_p)
2212 *no_dynreloc_p = true;
2213 else
2215 const char *name;
2216 arelent internal_reloc;
2218 if (!bed->elf_info_to_howto (input_section->owner,
2219 &internal_reloc, &irel)
2220 || internal_reloc.howto == NULL)
2221 abort ();
2223 if (h)
2224 name = h->root.root.string;
2225 else
2226 name = bfd_elf_sym_name (input_section->owner, symtab_hdr,
2227 sym, NULL);
2228 info->callbacks->einfo
2229 /* xgettext:c-format */
2230 (_("%F%P: %pB: relocation %s against absolute symbol "
2231 "`%s' in section `%pA' is disallowed\n"),
2232 input_section->owner, internal_reloc.howto->name, name,
2233 input_section);
2234 bfd_set_error (bfd_error_bad_value);
2238 return valid_p;
2241 /* Set the sizes of the dynamic sections. */
2243 bool
2244 _bfd_x86_elf_size_dynamic_sections (bfd *output_bfd,
2245 struct bfd_link_info *info)
2247 struct elf_x86_link_hash_table *htab;
2248 bfd *dynobj;
2249 asection *s;
2250 bool relocs;
2251 bfd *ibfd;
2252 const struct elf_backend_data *bed
2253 = get_elf_backend_data (output_bfd);
2255 htab = elf_x86_hash_table (info, bed->target_id);
2256 if (htab == NULL)
2257 return false;
2258 dynobj = htab->elf.dynobj;
2259 if (dynobj == NULL)
2260 abort ();
2262 /* Set up .got offsets for local syms, and space for local dynamic
2263 relocs. */
2264 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2266 bfd_signed_vma *local_got;
2267 bfd_signed_vma *end_local_got;
2268 char *local_tls_type;
2269 bfd_vma *local_tlsdesc_gotent;
2270 bfd_size_type locsymcount;
2271 Elf_Internal_Shdr *symtab_hdr;
2272 asection *srel;
2274 if (! is_x86_elf (ibfd, htab))
2275 continue;
2277 for (s = ibfd->sections; s != NULL; s = s->next)
2279 struct elf_dyn_relocs *p;
2281 for (p = ((struct elf_dyn_relocs *)
2282 elf_section_data (s)->local_dynrel);
2283 p != NULL;
2284 p = p->next)
2286 if (!bfd_is_abs_section (p->sec)
2287 && bfd_is_abs_section (p->sec->output_section))
2289 /* Input section has been discarded, either because
2290 it is a copy of a linkonce section or due to
2291 linker script /DISCARD/, so we'll be discarding
2292 the relocs too. */
2294 else if (htab->elf.target_os == is_vxworks
2295 && strcmp (p->sec->output_section->name,
2296 ".tls_vars") == 0)
2298 /* Relocations in vxworks .tls_vars sections are
2299 handled specially by the loader. */
2301 else if (p->count != 0)
2303 srel = elf_section_data (p->sec)->sreloc;
2304 srel->size += p->count * htab->sizeof_reloc;
2305 if ((p->sec->output_section->flags & SEC_READONLY) != 0
2306 && (info->flags & DF_TEXTREL) == 0)
2308 info->flags |= DF_TEXTREL;
2309 if (bfd_link_textrel_check (info))
2310 /* xgettext:c-format */
2311 info->callbacks->einfo
2312 (_("%P: %pB: warning: relocation "
2313 "in read-only section `%pA'\n"),
2314 p->sec->owner, p->sec);
2320 local_got = elf_local_got_refcounts (ibfd);
2321 if (!local_got)
2322 continue;
2324 symtab_hdr = &elf_symtab_hdr (ibfd);
2325 locsymcount = symtab_hdr->sh_info;
2326 end_local_got = local_got + locsymcount;
2327 local_tls_type = elf_x86_local_got_tls_type (ibfd);
2328 local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
2329 s = htab->elf.sgot;
2330 srel = htab->elf.srelgot;
2331 for (; local_got < end_local_got;
2332 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2334 *local_tlsdesc_gotent = (bfd_vma) -1;
2335 if (*local_got > 0)
2337 if (GOT_TLS_GDESC_P (*local_tls_type))
2339 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2340 - elf_x86_compute_jump_table_size (htab);
2341 htab->elf.sgotplt->size += 2 * htab->got_entry_size;
2342 *local_got = (bfd_vma) -2;
2344 if (! GOT_TLS_GDESC_P (*local_tls_type)
2345 || GOT_TLS_GD_P (*local_tls_type))
2347 *local_got = s->size;
2348 s->size += htab->got_entry_size;
2349 if (GOT_TLS_GD_P (*local_tls_type)
2350 || *local_tls_type == GOT_TLS_IE_BOTH)
2351 s->size += htab->got_entry_size;
2353 if ((bfd_link_pic (info) && *local_tls_type != GOT_ABS)
2354 || GOT_TLS_GD_ANY_P (*local_tls_type)
2355 || (*local_tls_type & GOT_TLS_IE))
2357 if (*local_tls_type == GOT_TLS_IE_BOTH)
2358 srel->size += 2 * htab->sizeof_reloc;
2359 else if (GOT_TLS_GD_P (*local_tls_type)
2360 || ! GOT_TLS_GDESC_P (*local_tls_type))
2361 srel->size += htab->sizeof_reloc;
2362 if (GOT_TLS_GDESC_P (*local_tls_type))
2364 htab->elf.srelplt->size += htab->sizeof_reloc;
2365 if (bed->target_id == X86_64_ELF_DATA)
2366 htab->elf.tlsdesc_plt = (bfd_vma) -1;
2370 else
2371 *local_got = (bfd_vma) -1;
2375 if (htab->tls_ld_or_ldm_got.refcount > 0)
2377 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2378 or R_X86_64_TLSLD relocs. */
2379 htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
2380 htab->elf.sgot->size += 2 * htab->got_entry_size;
2381 htab->elf.srelgot->size += htab->sizeof_reloc;
2383 else
2384 htab->tls_ld_or_ldm_got.offset = -1;
2386 /* Allocate global sym .plt and .got entries, and space for global
2387 sym dynamic relocs. */
2388 elf_link_hash_traverse (&htab->elf, elf_x86_allocate_dynrelocs,
2389 info);
2391 /* Allocate .plt and .got entries, and space for local symbols. */
2392 htab_traverse (htab->loc_hash_table, elf_x86_allocate_local_dynreloc,
2393 info);
2395 /* For every jump slot reserved in the sgotplt, reloc_count is
2396 incremented. However, when we reserve space for TLS descriptors,
2397 it's not incremented, so in order to compute the space reserved
2398 for them, it suffices to multiply the reloc count by the jump
2399 slot size.
2401 PR ld/13302: We start next_irelative_index at the end of .rela.plt
2402 so that R_{386,X86_64}_IRELATIVE entries come last. */
2403 if (htab->elf.srelplt)
2405 htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
2406 htab->sgotplt_jump_table_size
2407 = elf_x86_compute_jump_table_size (htab);
2408 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
2410 else if (htab->elf.irelplt)
2411 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
2413 if (htab->elf.tlsdesc_plt)
2415 /* NB: tlsdesc_plt is set only for x86-64. If we're not using
2416 lazy TLS relocations, don't generate the PLT and GOT entries
2417 they require. */
2418 if ((info->flags & DF_BIND_NOW))
2419 htab->elf.tlsdesc_plt = 0;
2420 else
2422 htab->elf.tlsdesc_got = htab->elf.sgot->size;
2423 htab->elf.sgot->size += htab->got_entry_size;
2424 /* Reserve room for the initial entry.
2425 FIXME: we could probably do away with it in this case. */
2426 if (htab->elf.splt->size == 0)
2427 htab->elf.splt->size = htab->plt.plt_entry_size;
2428 htab->elf.tlsdesc_plt = htab->elf.splt->size;
2429 htab->elf.splt->size += htab->plt.plt_entry_size;
2433 if (htab->elf.sgotplt)
2435 /* Don't allocate .got.plt section if there are no GOT nor PLT
2436 entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
2437 if ((htab->elf.hgot == NULL
2438 || !htab->got_referenced)
2439 && (htab->elf.sgotplt->size == bed->got_header_size)
2440 && (htab->elf.splt == NULL
2441 || htab->elf.splt->size == 0)
2442 && (htab->elf.sgot == NULL
2443 || htab->elf.sgot->size == 0)
2444 && (htab->elf.iplt == NULL
2445 || htab->elf.iplt->size == 0)
2446 && (htab->elf.igotplt == NULL
2447 || htab->elf.igotplt->size == 0))
2449 htab->elf.sgotplt->size = 0;
2450 /* Solaris requires to keep _GLOBAL_OFFSET_TABLE_ even if it
2451 isn't used. */
2452 if (htab->elf.hgot != NULL
2453 && htab->elf.target_os != is_solaris)
2455 /* Remove the unused _GLOBAL_OFFSET_TABLE_ from symbol
2456 table. */
2457 htab->elf.hgot->root.type = bfd_link_hash_undefined;
2458 htab->elf.hgot->root.u.undef.abfd
2459 = htab->elf.hgot->root.u.def.section->owner;
2460 htab->elf.hgot->root.linker_def = 0;
2461 htab->elf.hgot->ref_regular = 0;
2462 htab->elf.hgot->def_regular = 0;
2467 if (_bfd_elf_eh_frame_present (info))
2469 if (htab->plt_eh_frame != NULL
2470 && htab->elf.splt != NULL
2471 && htab->elf.splt->size != 0
2472 && !bfd_is_abs_section (htab->elf.splt->output_section))
2473 htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
2475 if (htab->plt_got_eh_frame != NULL
2476 && htab->plt_got != NULL
2477 && htab->plt_got->size != 0
2478 && !bfd_is_abs_section (htab->plt_got->output_section))
2479 htab->plt_got_eh_frame->size
2480 = htab->non_lazy_plt->eh_frame_plt_size;
2482 /* Unwind info for the second PLT and .plt.got sections are
2483 identical. */
2484 if (htab->plt_second_eh_frame != NULL
2485 && htab->plt_second != NULL
2486 && htab->plt_second->size != 0
2487 && !bfd_is_abs_section (htab->plt_second->output_section))
2488 htab->plt_second_eh_frame->size
2489 = htab->non_lazy_plt->eh_frame_plt_size;
2492 /* No need to size the .sframe section explicitly because the write-out
2493 mechanism is different. Simply prep up the FDE/FRE for the
2494 .plt section. */
2495 if (_bfd_elf_sframe_present (info))
2497 if (htab->plt_sframe != NULL
2498 && htab->elf.splt != NULL
2499 && htab->elf.splt->size != 0
2500 && !bfd_is_abs_section (htab->elf.splt->output_section))
2502 _bfd_x86_elf_create_sframe_plt (output_bfd, info, SFRAME_PLT);
2503 /* FIXME - Dirty Hack. Set the size to something non-zero for now,
2504 so that the section does not get stripped out below. The precise
2505 size of this section is known only when the contents are
2506 serialized in _bfd_x86_elf_write_sframe_plt. */
2507 htab->plt_sframe->size = sizeof (sframe_header) + 1;
2510 /* FIXME - generate for .got.plt ? */
2512 /* Unwind info for the second PLT. */
2513 if (htab->plt_second_sframe != NULL
2514 && htab->plt_second != NULL
2515 && htab->plt_second->size != 0
2516 && !bfd_is_abs_section (htab->plt_second->output_section))
2518 _bfd_x86_elf_create_sframe_plt (output_bfd, info,
2519 SFRAME_PLT_SEC);
2520 /* FIXME - Dirty Hack. Set the size to something non-zero for now,
2521 so that the section does not get stripped out below. The precise
2522 size of this section is known only when the contents are
2523 serialized in _bfd_x86_elf_write_sframe_plt. */
2524 htab->plt_second_sframe->size = sizeof (sframe_header) + 1;
2528 /* We now have determined the sizes of the various dynamic sections.
2529 Allocate memory for them. */
2530 relocs = false;
2531 for (s = dynobj->sections; s != NULL; s = s->next)
2533 bool strip_section = true;
2535 if ((s->flags & SEC_LINKER_CREATED) == 0)
2536 continue;
2538 /* The .relr.dyn section for compact relative relocation will
2539 be filled later. */
2540 if (s == htab->elf.srelrdyn)
2541 continue;
2543 if (s == htab->elf.splt
2544 || s == htab->elf.sgot)
2546 /* Strip this section if we don't need it; see the
2547 comment below. */
2548 /* We'd like to strip these sections if they aren't needed, but if
2549 we've exported dynamic symbols from them we must leave them.
2550 It's too late to tell BFD to get rid of the symbols. */
2552 if (htab->elf.hplt != NULL)
2553 strip_section = false;
2555 else if (s == htab->elf.sgotplt
2556 || s == htab->elf.iplt
2557 || s == htab->elf.igotplt
2558 || s == htab->plt_second
2559 || s == htab->plt_got
2560 || s == htab->plt_eh_frame
2561 || s == htab->plt_got_eh_frame
2562 || s == htab->plt_second_eh_frame
2563 || s == htab->plt_sframe
2564 || s == htab->plt_second_sframe
2565 || s == htab->elf.sdynbss
2566 || s == htab->elf.sdynrelro)
2568 /* Strip these too. */
2570 else if (htab->is_reloc_section (bfd_section_name (s)))
2572 if (s->size != 0
2573 && s != htab->elf.srelplt
2574 && s != htab->srelplt2)
2575 relocs = true;
2577 /* We use the reloc_count field as a counter if we need
2578 to copy relocs into the output file. */
2579 if (s != htab->elf.srelplt)
2580 s->reloc_count = 0;
2582 else
2584 /* It's not one of our sections, so don't allocate space. */
2585 continue;
2588 if (s->size == 0)
2590 /* If we don't need this section, strip it from the
2591 output file. This is mostly to handle .rel.bss and
2592 .rel.plt. We must create both sections in
2593 create_dynamic_sections, because they must be created
2594 before the linker maps input sections to output
2595 sections. The linker does that before
2596 adjust_dynamic_symbol is called, and it is that
2597 function which decides whether anything needs to go
2598 into these sections. */
2599 if (strip_section)
2600 s->flags |= SEC_EXCLUDE;
2601 continue;
2604 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2605 continue;
2607 /* Skip allocating contents for .sframe section as it is written
2608 out differently. See below. */
2609 if ((s == htab->plt_sframe) || (s == htab->plt_second_sframe))
2610 continue;
2612 /* NB: Initially, the iplt section has minimal alignment to
2613 avoid moving dot of the following section backwards when
2614 it is empty. Update its section alignment now since it
2615 is non-empty. */
2616 if (s == htab->elf.iplt)
2617 bfd_set_section_alignment (s, htab->plt.iplt_alignment);
2619 /* Allocate memory for the section contents. We use bfd_zalloc
2620 here in case unused entries are not reclaimed before the
2621 section's contents are written out. This should not happen,
2622 but this way if it does, we get a R_386_NONE or R_X86_64_NONE
2623 reloc instead of garbage. */
2624 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
2625 if (s->contents == NULL)
2626 return false;
2629 if (htab->plt_eh_frame != NULL
2630 && htab->plt_eh_frame->contents != NULL)
2632 memcpy (htab->plt_eh_frame->contents,
2633 htab->plt.eh_frame_plt,
2634 htab->plt_eh_frame->size);
2635 bfd_put_32 (dynobj, htab->elf.splt->size,
2636 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
2639 if (htab->plt_got_eh_frame != NULL
2640 && htab->plt_got_eh_frame->contents != NULL)
2642 memcpy (htab->plt_got_eh_frame->contents,
2643 htab->non_lazy_plt->eh_frame_plt,
2644 htab->plt_got_eh_frame->size);
2645 bfd_put_32 (dynobj, htab->plt_got->size,
2646 (htab->plt_got_eh_frame->contents
2647 + PLT_FDE_LEN_OFFSET));
2650 if (htab->plt_second_eh_frame != NULL
2651 && htab->plt_second_eh_frame->contents != NULL)
2653 memcpy (htab->plt_second_eh_frame->contents,
2654 htab->non_lazy_plt->eh_frame_plt,
2655 htab->plt_second_eh_frame->size);
2656 bfd_put_32 (dynobj, htab->plt_second->size,
2657 (htab->plt_second_eh_frame->contents
2658 + PLT_FDE_LEN_OFFSET));
2661 if (_bfd_elf_sframe_present (info))
2663 if (htab->plt_sframe != NULL
2664 && htab->elf.splt != NULL
2665 && htab->elf.splt->size != 0
2666 && htab->plt_sframe->contents == NULL)
2667 _bfd_x86_elf_write_sframe_plt (output_bfd, info, SFRAME_PLT);
2669 if (htab->plt_second_sframe != NULL
2670 && htab->elf.splt != NULL
2671 && htab->elf.splt->size != 0
2672 && htab->plt_second_sframe->contents == NULL)
2673 _bfd_x86_elf_write_sframe_plt (output_bfd, info, SFRAME_PLT_SEC);
2676 return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
2677 relocs);
2680 /* Finish up the x86 dynamic sections. */
2682 struct elf_x86_link_hash_table *
2683 _bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd,
2684 struct bfd_link_info *info)
2686 struct elf_x86_link_hash_table *htab;
2687 const struct elf_backend_data *bed;
2688 bfd *dynobj;
2689 asection *sdyn;
2690 bfd_byte *dyncon, *dynconend;
2691 bfd_size_type sizeof_dyn;
2693 bed = get_elf_backend_data (output_bfd);
2694 htab = elf_x86_hash_table (info, bed->target_id);
2695 if (htab == NULL)
2696 return htab;
2698 dynobj = htab->elf.dynobj;
2699 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2701 /* GOT is always created in setup_gnu_properties. But it may not be
2702 needed. .got.plt section may be needed for static IFUNC. */
2703 if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
2705 bfd_vma dynamic_addr;
2707 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
2709 _bfd_error_handler
2710 (_("discarded output section: `%pA'"), htab->elf.sgotplt);
2711 return NULL;
2714 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
2715 = htab->got_entry_size;
2717 dynamic_addr = (sdyn == NULL
2718 ? (bfd_vma) 0
2719 : sdyn->output_section->vma + sdyn->output_offset);
2721 /* Set the first entry in the global offset table to the address
2722 of the dynamic section. Write GOT[1] and GOT[2], needed for
2723 the dynamic linker. */
2724 if (htab->got_entry_size == 8)
2726 bfd_put_64 (output_bfd, dynamic_addr,
2727 htab->elf.sgotplt->contents);
2728 bfd_put_64 (output_bfd, (bfd_vma) 0,
2729 htab->elf.sgotplt->contents + 8);
2730 bfd_put_64 (output_bfd, (bfd_vma) 0,
2731 htab->elf.sgotplt->contents + 8*2);
2733 else
2735 bfd_put_32 (output_bfd, dynamic_addr,
2736 htab->elf.sgotplt->contents);
2737 bfd_put_32 (output_bfd, 0,
2738 htab->elf.sgotplt->contents + 4);
2739 bfd_put_32 (output_bfd, 0,
2740 htab->elf.sgotplt->contents + 4*2);
2744 if (!htab->elf.dynamic_sections_created)
2745 return htab;
2747 if (sdyn == NULL || htab->elf.sgot == NULL)
2748 abort ();
2750 sizeof_dyn = bed->s->sizeof_dyn;
2751 dyncon = sdyn->contents;
2752 dynconend = sdyn->contents + sdyn->size;
2753 for (; dyncon < dynconend; dyncon += sizeof_dyn)
2755 Elf_Internal_Dyn dyn;
2756 asection *s;
2758 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
2760 switch (dyn.d_tag)
2762 default:
2763 if (htab->elf.target_os == is_vxworks
2764 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
2765 break;
2766 continue;
2768 case DT_PLTGOT:
2769 s = htab->elf.sgotplt;
2770 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2771 break;
2773 case DT_JMPREL:
2774 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
2775 break;
2777 case DT_PLTRELSZ:
2778 s = htab->elf.srelplt->output_section;
2779 dyn.d_un.d_val = s->size;
2780 break;
2782 case DT_TLSDESC_PLT:
2783 s = htab->elf.splt;
2784 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
2785 + htab->elf.tlsdesc_plt;
2786 break;
2788 case DT_TLSDESC_GOT:
2789 s = htab->elf.sgot;
2790 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
2791 + htab->elf.tlsdesc_got;
2792 break;
2795 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
2798 if (htab->plt_got != NULL && htab->plt_got->size > 0)
2799 elf_section_data (htab->plt_got->output_section)
2800 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
2802 if (htab->plt_second != NULL && htab->plt_second->size > 0)
2803 elf_section_data (htab->plt_second->output_section)
2804 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
2806 /* Adjust .eh_frame for .plt section. */
2807 if (htab->plt_eh_frame != NULL
2808 && htab->plt_eh_frame->contents != NULL)
2810 if (htab->elf.splt != NULL
2811 && htab->elf.splt->size != 0
2812 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
2813 && htab->elf.splt->output_section != NULL
2814 && htab->plt_eh_frame->output_section != NULL)
2816 bfd_vma plt_start = htab->elf.splt->output_section->vma;
2817 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
2818 + htab->plt_eh_frame->output_offset
2819 + PLT_FDE_START_OFFSET;
2820 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2821 htab->plt_eh_frame->contents
2822 + PLT_FDE_START_OFFSET);
2825 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
2827 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2828 htab->plt_eh_frame,
2829 htab->plt_eh_frame->contents))
2830 return NULL;
2834 /* Adjust .eh_frame for .plt.got section. */
2835 if (htab->plt_got_eh_frame != NULL
2836 && htab->plt_got_eh_frame->contents != NULL)
2838 if (htab->plt_got != NULL
2839 && htab->plt_got->size != 0
2840 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
2841 && htab->plt_got->output_section != NULL
2842 && htab->plt_got_eh_frame->output_section != NULL)
2844 bfd_vma plt_start = htab->plt_got->output_section->vma;
2845 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
2846 + htab->plt_got_eh_frame->output_offset
2847 + PLT_FDE_START_OFFSET;
2848 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2849 htab->plt_got_eh_frame->contents
2850 + PLT_FDE_START_OFFSET);
2852 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
2854 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2855 htab->plt_got_eh_frame,
2856 htab->plt_got_eh_frame->contents))
2857 return NULL;
2861 /* Adjust .eh_frame for the second PLT section. */
2862 if (htab->plt_second_eh_frame != NULL
2863 && htab->plt_second_eh_frame->contents != NULL)
2865 if (htab->plt_second != NULL
2866 && htab->plt_second->size != 0
2867 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
2868 && htab->plt_second->output_section != NULL
2869 && htab->plt_second_eh_frame->output_section != NULL)
2871 bfd_vma plt_start = htab->plt_second->output_section->vma;
2872 bfd_vma eh_frame_start
2873 = (htab->plt_second_eh_frame->output_section->vma
2874 + htab->plt_second_eh_frame->output_offset
2875 + PLT_FDE_START_OFFSET);
2876 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2877 htab->plt_second_eh_frame->contents
2878 + PLT_FDE_START_OFFSET);
2880 if (htab->plt_second_eh_frame->sec_info_type
2881 == SEC_INFO_TYPE_EH_FRAME)
2883 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2884 htab->plt_second_eh_frame,
2885 htab->plt_second_eh_frame->contents))
2886 return NULL;
2890 /* Make any adjustment if necessary and merge .sframe section to
2891 create the final .sframe section for output_bfd. */
2892 if (htab->plt_sframe != NULL
2893 && htab->plt_sframe->contents != NULL)
2895 if (htab->elf.splt != NULL
2896 && htab->elf.splt->size != 0
2897 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
2898 && htab->elf.splt->output_section != NULL
2899 && htab->plt_sframe->output_section != NULL)
2901 bfd_vma plt_start = htab->elf.splt->output_section->vma;
2902 bfd_vma sframe_start = htab->plt_sframe->output_section->vma
2903 + htab->plt_sframe->output_offset
2904 + PLT_SFRAME_FDE_START_OFFSET;
2905 #if 0 /* FIXME Testing only. Remove before review. */
2906 bfd_vma test_value = (plt_start - sframe_start)
2907 + htab->plt_sframe->output_section->vma
2908 + htab->plt_sframe->output_offset
2909 + PLT_SFRAME_FDE_START_OFFSET;
2910 bfd_put_signed_32 (dynobj, test_value,
2911 #endif
2912 bfd_put_signed_32 (dynobj, plt_start - sframe_start,
2913 htab->plt_sframe->contents
2914 + PLT_SFRAME_FDE_START_OFFSET);
2916 if (htab->plt_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
2918 if (! _bfd_elf_merge_section_sframe (output_bfd, info,
2919 htab->plt_sframe,
2920 htab->plt_sframe->contents))
2921 return NULL;
2925 if (htab->plt_second_sframe != NULL
2926 && htab->plt_second_sframe->contents != NULL)
2928 if (htab->plt_second != NULL
2929 && htab->plt_second->size != 0
2930 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
2931 && htab->plt_second->output_section != NULL
2932 && htab->plt_second_sframe->output_section != NULL)
2934 bfd_vma plt_start = htab->plt_second->output_section->vma;
2935 bfd_vma sframe_start
2936 = (htab->plt_second_sframe->output_section->vma
2937 + htab->plt_second_sframe->output_offset
2938 + PLT_SFRAME_FDE_START_OFFSET);
2939 #if 0 /* FIXME Testing only. Remove before review. */
2940 bfd_vma test_value = (plt_start - sframe_start)
2941 + htab->plt_second_sframe->output_section->vma
2942 + htab->plt_second_sframe->output_offset
2943 + PLT_SFRAME_FDE_START_OFFSET;
2944 bfd_put_signed_32 (dynobj, test_value,
2945 #endif
2946 bfd_put_signed_32 (dynobj, plt_start - sframe_start,
2947 htab->plt_second_sframe->contents
2948 + PLT_SFRAME_FDE_START_OFFSET);
2950 if (htab->plt_second_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
2952 if (! _bfd_elf_merge_section_sframe (output_bfd, info,
2953 htab->plt_second_sframe,
2954 htab->plt_second_sframe->contents))
2955 return NULL;
2958 if (htab->elf.sgot && htab->elf.sgot->size > 0)
2959 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
2960 = htab->got_entry_size;
2962 return htab;
2966 bool
2967 _bfd_x86_elf_always_size_sections (bfd *output_bfd,
2968 struct bfd_link_info *info)
2970 asection *tls_sec = elf_hash_table (info)->tls_sec;
2972 if (tls_sec && !bfd_link_relocatable (info))
2974 struct elf_link_hash_entry *tlsbase;
2976 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2977 "_TLS_MODULE_BASE_",
2978 false, false, false);
2980 if (tlsbase && tlsbase->type == STT_TLS)
2982 struct elf_x86_link_hash_table *htab;
2983 struct bfd_link_hash_entry *bh = NULL;
2984 const struct elf_backend_data *bed
2985 = get_elf_backend_data (output_bfd);
2987 htab = elf_x86_hash_table (info, bed->target_id);
2988 if (htab == NULL)
2989 return false;
2991 if (!(_bfd_generic_link_add_one_symbol
2992 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2993 tls_sec, 0, NULL, false,
2994 bed->collect, &bh)))
2995 return false;
2997 htab->tls_module_base = bh;
2999 tlsbase = (struct elf_link_hash_entry *)bh;
3000 tlsbase->def_regular = 1;
3001 tlsbase->other = STV_HIDDEN;
3002 tlsbase->root.linker_def = 1;
3003 (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
3007 return true;
3010 void
3011 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3012 unsigned int st_other,
3013 bool definition,
3014 bool dynamic ATTRIBUTE_UNUSED)
3016 if (definition)
3018 struct elf_x86_link_hash_entry *eh
3019 = (struct elf_x86_link_hash_entry *) h;
3020 eh->def_protected = ELF_ST_VISIBILITY (st_other) == STV_PROTECTED;
3024 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3026 void
3027 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
3028 struct elf_link_hash_entry *dir,
3029 struct elf_link_hash_entry *ind)
3031 struct elf_x86_link_hash_entry *edir, *eind;
3033 edir = (struct elf_x86_link_hash_entry *) dir;
3034 eind = (struct elf_x86_link_hash_entry *) ind;
3036 if (ind->root.type == bfd_link_hash_indirect
3037 && dir->got.refcount <= 0)
3039 edir->tls_type = eind->tls_type;
3040 eind->tls_type = GOT_UNKNOWN;
3043 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
3044 generate a R_386_COPY reloc. */
3045 edir->gotoff_ref |= eind->gotoff_ref;
3047 edir->zero_undefweak |= eind->zero_undefweak;
3049 if (ELIMINATE_COPY_RELOCS
3050 && ind->root.type != bfd_link_hash_indirect
3051 && dir->dynamic_adjusted)
3053 /* If called to transfer flags for a weakdef during processing
3054 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
3055 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3056 if (dir->versioned != versioned_hidden)
3057 dir->ref_dynamic |= ind->ref_dynamic;
3058 dir->ref_regular |= ind->ref_regular;
3059 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
3060 dir->needs_plt |= ind->needs_plt;
3061 dir->pointer_equality_needed |= ind->pointer_equality_needed;
3063 else
3064 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3067 /* Remove undefined weak symbol from the dynamic symbol table if it
3068 is resolved to 0. */
3070 bool
3071 _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
3072 struct elf_link_hash_entry *h)
3074 if (h->dynindx != -1
3075 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, elf_x86_hash_entry (h)))
3077 h->dynindx = -1;
3078 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3079 h->dynstr_index);
3081 return true;
3084 /* Change the STT_GNU_IFUNC symbol defined in position-dependent
3085 executable into the normal function symbol and set its address
3086 to its PLT entry, which should be resolved by R_*_IRELATIVE at
3087 run-time. */
3089 void
3090 _bfd_x86_elf_link_fixup_ifunc_symbol (struct bfd_link_info *info,
3091 struct elf_x86_link_hash_table *htab,
3092 struct elf_link_hash_entry *h,
3093 Elf_Internal_Sym *sym)
3095 if (bfd_link_pde (info)
3096 && h->def_regular
3097 && h->dynindx != -1
3098 && h->plt.offset != (bfd_vma) -1
3099 && h->type == STT_GNU_IFUNC)
3101 asection *plt_s;
3102 bfd_vma plt_offset;
3103 bfd *output_bfd = info->output_bfd;
3105 if (htab->plt_second)
3107 struct elf_x86_link_hash_entry *eh
3108 = (struct elf_x86_link_hash_entry *) h;
3110 plt_s = htab->plt_second;
3111 plt_offset = eh->plt_second.offset;
3113 else
3115 plt_s = htab->elf.splt;
3116 plt_offset = h->plt.offset;
3119 sym->st_size = 0;
3120 sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
3121 sym->st_shndx
3122 = _bfd_elf_section_from_bfd_section (output_bfd,
3123 plt_s->output_section);
3124 sym->st_value = (plt_s->output_section->vma
3125 + plt_s->output_offset + plt_offset);
3129 /* Report relative relocation. */
3131 void
3132 _bfd_x86_elf_link_report_relative_reloc
3133 (struct bfd_link_info *info, asection *asect,
3134 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym,
3135 const char *reloc_name, const void *reloc)
3137 const char *name;
3138 bfd *abfd;
3139 const Elf_Internal_Rela *rel = (const Elf_Internal_Rela *) reloc;
3141 /* Use the output BFD for linker created sections. */
3142 if ((asect->flags & SEC_LINKER_CREATED) != 0)
3143 abfd = info->output_bfd;
3144 else
3145 abfd = asect->owner;
3147 if (h != NULL && h->root.root.string != NULL)
3148 name = h->root.root.string;
3149 else
3150 name = bfd_elf_sym_name (abfd, &elf_symtab_hdr (abfd), sym, NULL);
3152 if (asect->use_rela_p)
3153 info->callbacks->einfo
3154 (_("%pB: %s (offset: 0x%v, info: 0x%v, addend: 0x%v) against "
3155 "'%s' " "for section '%pA' in %pB\n"),
3156 info->output_bfd, reloc_name, rel->r_offset, rel->r_info,
3157 rel->r_addend, name, asect, abfd);
3158 else
3159 info->callbacks->einfo
3160 (_("%pB: %s (offset: 0x%v, info: 0x%v) against '%s' for section "
3161 "'%pA' in %pB\n"),
3162 info->output_bfd, reloc_name, rel->r_offset, rel->r_info, name,
3163 asect, abfd);
3166 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3168 bool
3169 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
3171 if (h->plt.offset != (bfd_vma) -1
3172 && !h->def_regular
3173 && !h->pointer_equality_needed)
3174 return false;
3176 return _bfd_elf_hash_symbol (h);
3179 /* Adjust a symbol defined by a dynamic object and referenced by a
3180 regular object. The current definition is in some section of the
3181 dynamic object, but we're not including those sections. We have to
3182 change the definition to something the rest of the link can
3183 understand. */
3185 bool
3186 _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3187 struct elf_link_hash_entry *h)
3189 struct elf_x86_link_hash_table *htab;
3190 asection *s, *srel;
3191 struct elf_x86_link_hash_entry *eh;
3192 struct elf_dyn_relocs *p;
3193 const struct elf_backend_data *bed
3194 = get_elf_backend_data (info->output_bfd);
3196 eh = (struct elf_x86_link_hash_entry *) h;
3198 /* Clear GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS if it is turned
3199 on by an input relocatable file and there is a non-GOT/non-PLT
3200 reference from another relocatable file without it.
3201 NB: There can be non-GOT reference in data sections in input with
3202 GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS. */
3203 if (eh->non_got_ref_without_indirect_extern_access
3204 && info->indirect_extern_access == 1
3205 && bfd_link_executable (info))
3207 unsigned int needed_1;
3208 info->indirect_extern_access = 0;
3209 /* Turn off nocopyreloc if implied by indirect_extern_access. */
3210 if (info->nocopyreloc == 2)
3211 info->nocopyreloc = 0;
3212 needed_1 = bfd_h_get_32 (info->output_bfd, info->needed_1_p);
3213 needed_1 &= ~GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
3214 bfd_h_put_32 (info->output_bfd, needed_1, info->needed_1_p);
3217 /* STT_GNU_IFUNC symbol must go through PLT. */
3218 if (h->type == STT_GNU_IFUNC)
3220 /* All local STT_GNU_IFUNC references must be treate as local
3221 calls via local PLT. */
3222 if (h->ref_regular
3223 && SYMBOL_CALLS_LOCAL (info, h))
3225 bfd_size_type pc_count = 0, count = 0;
3226 struct elf_dyn_relocs **pp;
3228 eh = (struct elf_x86_link_hash_entry *) h;
3229 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
3231 pc_count += p->pc_count;
3232 p->count -= p->pc_count;
3233 p->pc_count = 0;
3234 count += p->count;
3235 if (p->count == 0)
3236 *pp = p->next;
3237 else
3238 pp = &p->next;
3241 if (pc_count || count)
3243 h->non_got_ref = 1;
3244 if (pc_count)
3246 /* Increment PLT reference count only for PC-relative
3247 references. */
3248 h->needs_plt = 1;
3249 if (h->plt.refcount <= 0)
3250 h->plt.refcount = 1;
3251 else
3252 h->plt.refcount += 1;
3256 /* GOTOFF relocation needs PLT. */
3257 if (eh->gotoff_ref)
3258 h->plt.refcount = 1;
3261 if (h->plt.refcount <= 0)
3263 h->plt.offset = (bfd_vma) -1;
3264 h->needs_plt = 0;
3266 return true;
3269 /* If this is a function, put it in the procedure linkage table. We
3270 will fill in the contents of the procedure linkage table later,
3271 when we know the address of the .got section. */
3272 if (h->type == STT_FUNC
3273 || h->needs_plt)
3275 if (h->plt.refcount <= 0
3276 || SYMBOL_CALLS_LOCAL (info, h)
3277 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3278 && h->root.type == bfd_link_hash_undefweak))
3280 /* This case can occur if we saw a PLT32 reloc in an input
3281 file, but the symbol was never referred to by a dynamic
3282 object, or if all references were garbage collected. In
3283 such a case, we don't actually need to build a procedure
3284 linkage table, and we can just do a PC32 reloc instead. */
3285 h->plt.offset = (bfd_vma) -1;
3286 h->needs_plt = 0;
3289 return true;
3291 else
3292 /* It's possible that we incorrectly decided a .plt reloc was needed
3293 * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
3294 check_relocs. We can't decide accurately between function and
3295 non-function syms in check-relocs; Objects loaded later in
3296 the link may change h->type. So fix it now. */
3297 h->plt.offset = (bfd_vma) -1;
3299 /* If this is a weak symbol, and there is a real definition, the
3300 processor independent code will have arranged for us to see the
3301 real definition first, and we can just use the same value. */
3302 if (h->is_weakalias)
3304 struct elf_link_hash_entry *def = weakdef (h);
3305 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3306 h->root.u.def.section = def->root.u.def.section;
3307 h->root.u.def.value = def->root.u.def.value;
3308 if (ELIMINATE_COPY_RELOCS
3309 || info->nocopyreloc
3310 || SYMBOL_NO_COPYRELOC (info, eh))
3312 /* NB: needs_copy is always 0 for i386. */
3313 h->non_got_ref = def->non_got_ref;
3314 eh->needs_copy = def->needs_copy;
3316 return true;
3319 /* This is a reference to a symbol defined by a dynamic object which
3320 is not a function. */
3322 /* If we are creating a shared library, we must presume that the
3323 only references to the symbol are via the global offset table.
3324 For such cases we need not do anything here; the relocations will
3325 be handled correctly by relocate_section. */
3326 if (!bfd_link_executable (info))
3327 return true;
3329 /* If there are no references to this symbol that do not use the
3330 GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
3331 reloc. NB: gotoff_ref is always 0 for x86-64. */
3332 if (!h->non_got_ref && !eh->gotoff_ref)
3333 return true;
3335 /* If -z nocopyreloc was given, we won't generate them either. */
3336 if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
3338 h->non_got_ref = 0;
3339 return true;
3342 htab = elf_x86_hash_table (info, bed->target_id);
3343 if (htab == NULL)
3344 return false;
3346 /* If there aren't any dynamic relocs in read-only sections nor
3347 R_386_GOTOFF relocation, then we can keep the dynamic relocs and
3348 avoid the copy reloc. This doesn't work on VxWorks, where we can
3349 not have dynamic relocations (other than copy and jump slot
3350 relocations) in an executable. */
3351 if (ELIMINATE_COPY_RELOCS
3352 && (bed->target_id == X86_64_ELF_DATA
3353 || (!eh->gotoff_ref
3354 && htab->elf.target_os != is_vxworks)))
3356 /* If we don't find any dynamic relocs in read-only sections,
3357 then we'll be keeping the dynamic relocs and avoiding the copy
3358 reloc. */
3359 if (!_bfd_elf_readonly_dynrelocs (h))
3361 h->non_got_ref = 0;
3362 return true;
3366 /* We must allocate the symbol in our .dynbss section, which will
3367 become part of the .bss section of the executable. There will be
3368 an entry for this symbol in the .dynsym section. The dynamic
3369 object will contain position independent code, so all references
3370 from the dynamic object to this symbol will go through the global
3371 offset table. The dynamic linker will use the .dynsym entry to
3372 determine the address it must put in the global offset table, so
3373 both the dynamic object and the regular object will refer to the
3374 same memory location for the variable. */
3376 /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
3377 dynamic linker to copy the initial value out of the dynamic object
3378 and into the runtime process image. */
3379 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
3381 s = htab->elf.sdynrelro;
3382 srel = htab->elf.sreldynrelro;
3384 else
3386 s = htab->elf.sdynbss;
3387 srel = htab->elf.srelbss;
3389 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3391 if (eh->def_protected && bfd_link_executable (info))
3392 for (p = h->dyn_relocs; p != NULL; p = p->next)
3394 /* Disallow copy relocation against non-copyable protected
3395 symbol. */
3396 s = p->sec->output_section;
3397 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3399 info->callbacks->einfo
3400 /* xgettext:c-format */
3401 (_("%F%P: %pB: copy relocation against non-copyable "
3402 "protected symbol `%s' in %pB\n"),
3403 p->sec->owner, h->root.root.string,
3404 h->root.u.def.section->owner);
3405 return false;
3409 srel->size += htab->sizeof_reloc;
3410 h->needs_copy = 1;
3413 return _bfd_elf_adjust_dynamic_copy (info, h, s);
3416 void
3417 _bfd_x86_elf_hide_symbol (struct bfd_link_info *info,
3418 struct elf_link_hash_entry *h,
3419 bool force_local)
3421 if (h->root.type == bfd_link_hash_undefweak
3422 && info->nointerp
3423 && bfd_link_pie (info))
3425 /* When there is no dynamic interpreter in PIE, make the undefined
3426 weak symbol dynamic so that PC relative branch to the undefined
3427 weak symbol will land to address 0. */
3428 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
3429 if (h->plt.refcount > 0
3430 || eh->plt_got.refcount > 0)
3431 return;
3434 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3437 /* Return TRUE if a symbol is referenced locally. It is similar to
3438 SYMBOL_REFERENCES_LOCAL, but it also checks version script. It
3439 works in check_relocs. */
3441 bool
3442 _bfd_x86_elf_link_symbol_references_local (struct bfd_link_info *info,
3443 struct elf_link_hash_entry *h)
3445 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
3446 struct elf_x86_link_hash_table *htab
3447 = (struct elf_x86_link_hash_table *) info->hash;
3449 if (eh->local_ref > 1)
3450 return true;
3452 if (eh->local_ref == 1)
3453 return false;
3455 /* Unversioned symbols defined in regular objects can be forced local
3456 by linker version script. A weak undefined symbol is forced local
3458 1. It has non-default visibility. Or
3459 2. When building executable, there is no dynamic linker. Or
3460 3. or "-z nodynamic-undefined-weak" is used.
3462 if (_bfd_elf_symbol_refs_local_p (h, info, 1)
3463 || (h->root.type == bfd_link_hash_undefweak
3464 && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3465 || (bfd_link_executable (info)
3466 && htab->interp == NULL)
3467 || info->dynamic_undefined_weak == 0))
3468 || ((h->def_regular || ELF_COMMON_DEF_P (h))
3469 && info->version_info != NULL
3470 && _bfd_elf_link_hide_sym_by_version (info, h)))
3472 eh->local_ref = 2;
3473 return true;
3476 eh->local_ref = 1;
3477 return false;
3480 /* Return the section that should be marked against GC for a given
3481 relocation. */
3483 asection *
3484 _bfd_x86_elf_gc_mark_hook (asection *sec,
3485 struct bfd_link_info *info,
3486 Elf_Internal_Rela *rel,
3487 struct elf_link_hash_entry *h,
3488 Elf_Internal_Sym *sym)
3490 /* Compiler should optimize this out. */
3491 if (((unsigned int) R_X86_64_GNU_VTINHERIT
3492 != (unsigned int) R_386_GNU_VTINHERIT)
3493 || ((unsigned int) R_X86_64_GNU_VTENTRY
3494 != (unsigned int) R_386_GNU_VTENTRY))
3495 abort ();
3497 if (h != NULL)
3498 switch (ELF32_R_TYPE (rel->r_info))
3500 case R_X86_64_GNU_VTINHERIT:
3501 case R_X86_64_GNU_VTENTRY:
3502 return NULL;
3505 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
3508 static bfd_vma
3509 elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
3510 bfd_vma off,
3511 bfd_vma offset ATTRIBUTE_UNUSED,
3512 bfd_vma got_addr)
3514 return got_addr + off;
3517 static bfd_vma
3518 elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
3519 bfd_vma off,
3520 bfd_vma offset,
3521 bfd_vma got_addr ATTRIBUTE_UNUSED)
3523 return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
3526 static bool
3527 elf_i386_valid_plt_reloc_p (unsigned int type)
3529 return (type == R_386_JUMP_SLOT
3530 || type == R_386_GLOB_DAT
3531 || type == R_386_IRELATIVE);
3534 static bool
3535 elf_x86_64_valid_plt_reloc_p (unsigned int type)
3537 return (type == R_X86_64_JUMP_SLOT
3538 || type == R_X86_64_GLOB_DAT
3539 || type == R_X86_64_IRELATIVE);
3542 long
3543 _bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
3544 long count,
3545 long relsize,
3546 bfd_vma got_addr,
3547 struct elf_x86_plt plts[],
3548 asymbol **dynsyms,
3549 asymbol **ret)
3551 long size, i, n, len;
3552 int j;
3553 unsigned int plt_got_offset, plt_entry_size;
3554 asymbol *s;
3555 bfd_byte *plt_contents;
3556 long dynrelcount;
3557 arelent **dynrelbuf, *p;
3558 char *names;
3559 const struct elf_backend_data *bed;
3560 bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
3561 bfd_vma);
3562 bool (*valid_plt_reloc_p) (unsigned int);
3564 dynrelbuf = NULL;
3565 if (count == 0)
3566 goto bad_return;
3568 dynrelbuf = (arelent **) bfd_malloc (relsize);
3569 if (dynrelbuf == NULL)
3570 goto bad_return;
3572 dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
3573 dynsyms);
3574 if (dynrelcount <= 0)
3575 goto bad_return;
3577 /* Sort the relocs by address. */
3578 qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
3579 _bfd_x86_elf_compare_relocs);
3581 size = count * sizeof (asymbol);
3583 /* Allocate space for @plt suffixes. */
3584 n = 0;
3585 for (i = 0; i < dynrelcount; i++)
3587 p = dynrelbuf[i];
3588 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3589 if (p->addend != 0)
3590 size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
3593 s = *ret = (asymbol *) bfd_zmalloc (size);
3594 if (s == NULL)
3595 goto bad_return;
3597 bed = get_elf_backend_data (abfd);
3599 if (bed->target_id == X86_64_ELF_DATA)
3601 get_plt_got_vma = elf_x86_64_get_plt_got_vma;
3602 valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
3604 else
3606 get_plt_got_vma = elf_i386_get_plt_got_vma;
3607 valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
3608 if (got_addr)
3610 /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
3611 address. */
3612 asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
3613 if (sec != NULL)
3614 got_addr = sec->vma;
3615 else
3617 sec = bfd_get_section_by_name (abfd, ".got");
3618 if (sec != NULL)
3619 got_addr = sec->vma;
3622 if (got_addr == (bfd_vma) -1)
3623 goto bad_return;
3627 /* Check for each PLT section. */
3628 names = (char *) (s + count);
3629 size = 0;
3630 n = 0;
3631 for (j = 0; plts[j].name != NULL; j++)
3632 if ((plt_contents = plts[j].contents) != NULL)
3634 long k;
3635 bfd_vma offset;
3636 asection *plt;
3637 struct elf_x86_plt *plt_p = &plts[j];
3639 plt_got_offset = plt_p->plt_got_offset;
3640 plt_entry_size = plt_p->plt_entry_size;
3642 plt = plt_p->sec;
3644 if ((plt_p->type & plt_lazy))
3646 /* Skip PLT0 in lazy PLT. */
3647 k = 1;
3648 offset = plt_entry_size;
3650 else
3652 k = 0;
3653 offset = 0;
3656 /* Check each PLT entry against dynamic relocations. */
3657 for (; k < plt_p->count; k++)
3659 int off;
3660 bfd_vma got_vma;
3661 long min, max, mid;
3663 /* Get the GOT offset for i386 or the PC-relative offset
3664 for x86-64, a signed 32-bit integer. */
3665 off = H_GET_32 (abfd, (plt_contents + offset
3666 + plt_got_offset));
3667 got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
3669 /* Binary search. */
3670 p = dynrelbuf[0];
3671 min = 0;
3672 max = dynrelcount;
3673 while ((min + 1) < max)
3675 arelent *r;
3677 mid = (min + max) / 2;
3678 r = dynrelbuf[mid];
3679 if (got_vma > r->address)
3680 min = mid;
3681 else if (got_vma < r->address)
3682 max = mid;
3683 else
3685 p = r;
3686 break;
3690 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
3691 if (got_vma == p->address
3692 && p->howto != NULL
3693 && valid_plt_reloc_p (p->howto->type))
3695 *s = **p->sym_ptr_ptr;
3696 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
3697 set. Since we are defining a symbol, ensure one
3698 of them is set. */
3699 if ((s->flags & BSF_LOCAL) == 0)
3700 s->flags |= BSF_GLOBAL;
3701 s->flags |= BSF_SYNTHETIC;
3702 /* This is no longer a section symbol. */
3703 s->flags &= ~BSF_SECTION_SYM;
3704 s->section = plt;
3705 s->the_bfd = plt->owner;
3706 s->value = offset;
3707 s->udata.p = NULL;
3708 s->name = names;
3709 len = strlen ((*p->sym_ptr_ptr)->name);
3710 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3711 names += len;
3712 if (p->addend != 0)
3714 char buf[30], *a;
3716 memcpy (names, "+0x", sizeof ("+0x") - 1);
3717 names += sizeof ("+0x") - 1;
3718 bfd_sprintf_vma (abfd, buf, p->addend);
3719 for (a = buf; *a == '0'; ++a)
3721 size = strlen (a);
3722 memcpy (names, a, size);
3723 names += size;
3725 memcpy (names, "@plt", sizeof ("@plt"));
3726 names += sizeof ("@plt");
3727 n++;
3728 s++;
3729 /* There should be only one entry in PLT for a given
3730 symbol. Set howto to NULL after processing a PLT
3731 entry to guard against corrupted PLT. */
3732 p->howto = NULL;
3734 offset += plt_entry_size;
3738 /* PLT entries with R_386_TLS_DESC relocations are skipped. */
3739 if (n == 0)
3741 bad_return:
3742 count = -1;
3744 else
3745 count = n;
3747 for (j = 0; plts[j].name != NULL; j++)
3748 free (plts[j].contents);
3750 free (dynrelbuf);
3752 return count;
3755 /* Parse x86 GNU properties. */
3757 enum elf_property_kind
3758 _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
3759 bfd_byte *ptr, unsigned int datasz)
3761 elf_property *prop;
3763 if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3764 || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3765 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
3766 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3767 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
3768 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
3769 || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3770 && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3772 if (datasz != 4)
3774 _bfd_error_handler
3775 (_("error: %pB: <corrupt x86 property (0x%x) size: 0x%x>"),
3776 abfd, type, datasz);
3777 return property_corrupt;
3779 prop = _bfd_elf_get_property (abfd, type, datasz);
3780 prop->u.number |= bfd_h_get_32 (abfd, ptr);
3781 prop->pr_kind = property_number;
3782 return property_number;
3785 return property_ignored;
3788 /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
3789 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
3790 should be merged with ABFD. */
3792 bool
3793 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
3794 bfd *abfd ATTRIBUTE_UNUSED,
3795 bfd *bbfd ATTRIBUTE_UNUSED,
3796 elf_property *aprop,
3797 elf_property *bprop)
3799 unsigned int number, features;
3800 bool updated = false;
3801 const struct elf_backend_data *bed;
3802 struct elf_x86_link_hash_table *htab;
3803 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
3805 if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3806 || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3807 && pr_type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3809 if (aprop == NULL || bprop == NULL)
3811 /* Only one of APROP and BPROP can be NULL. */
3812 if (aprop != NULL)
3814 /* Remove this property since the other input file doesn't
3815 have it. */
3816 aprop->pr_kind = property_remove;
3817 updated = true;
3820 else
3822 number = aprop->u.number;
3823 aprop->u.number = number | bprop->u.number;
3824 updated = number != (unsigned int) aprop->u.number;
3826 return updated;
3828 else if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3829 || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_LO
3830 && pr_type <= GNU_PROPERTY_X86_UINT32_OR_HI))
3832 features = 0;
3833 if (pr_type == GNU_PROPERTY_X86_ISA_1_NEEDED)
3835 bed = get_elf_backend_data (info->output_bfd);
3836 htab = elf_x86_hash_table (info, bed->target_id);
3837 switch (htab->params->isa_level)
3839 case 0:
3840 break;
3841 case 2:
3842 features = GNU_PROPERTY_X86_ISA_1_V2;
3843 break;
3844 case 3:
3845 features = GNU_PROPERTY_X86_ISA_1_V3;
3846 break;
3847 case 4:
3848 features = GNU_PROPERTY_X86_ISA_1_V4;
3849 break;
3850 default:
3851 abort ();
3854 if (aprop != NULL && bprop != NULL)
3856 number = aprop->u.number;
3857 aprop->u.number = number | bprop->u.number | features;
3858 /* Remove the property if all bits are empty. */
3859 if (aprop->u.number == 0)
3861 aprop->pr_kind = property_remove;
3862 updated = true;
3864 else
3865 updated = number != (unsigned int) aprop->u.number;
3867 else
3869 /* Only one of APROP and BPROP can be NULL. */
3870 if (aprop != NULL)
3872 aprop->u.number |= features;
3873 if (aprop->u.number == 0)
3875 /* Remove APROP if all bits are empty. */
3876 aprop->pr_kind = property_remove;
3877 updated = true;
3880 else
3882 /* Return TRUE if APROP is NULL and all bits of BPROP
3883 aren't empty to indicate that BPROP should be added
3884 to ABFD. */
3885 bprop->u.number |= features;
3886 updated = bprop->u.number != 0;
3889 return updated;
3891 else if (pr_type >= GNU_PROPERTY_X86_UINT32_AND_LO
3892 && pr_type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3894 /* Only one of APROP and BPROP can be NULL:
3895 1. APROP & BPROP when both APROP and BPROP aren't NULL.
3896 2. If APROP is NULL, remove x86 feature.
3897 3. Otherwise, do nothing.
3899 bed = get_elf_backend_data (info->output_bfd);
3900 htab = elf_x86_hash_table (info, bed->target_id);
3901 if (!htab)
3902 abort ();
3903 if (aprop != NULL && bprop != NULL)
3905 number = aprop->u.number;
3906 aprop->u.number = number & bprop->u.number;
3907 if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
3909 features = 0;
3910 if (htab->params->ibt)
3911 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
3912 if (htab->params->shstk)
3913 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
3914 if (htab->params->lam_u48)
3915 features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
3916 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
3917 else if (htab->params->lam_u57)
3918 features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
3919 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT,
3920 GNU_PROPERTY_X86_FEATURE_1_SHSTK,
3921 GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
3922 GNU_PROPERTY_X86_FEATURE_1_LAM_U57. */
3923 aprop->u.number |= features;
3925 updated = number != (unsigned int) aprop->u.number;
3926 /* Remove the property if all feature bits are cleared. */
3927 if (aprop->u.number == 0)
3928 aprop->pr_kind = property_remove;
3930 else
3932 /* There should be no AND properties since some input doesn't
3933 have them. Set IBT and SHSTK properties for -z ibt and -z
3934 shstk if needed. */
3935 features = 0;
3936 if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
3938 if (htab->params->ibt)
3939 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
3940 if (htab->params->shstk)
3941 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
3942 if (htab->params->lam_u48)
3943 features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
3944 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
3945 else if (htab->params->lam_u57)
3946 features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
3948 if (features)
3950 if (aprop != NULL)
3952 updated = features != (unsigned int) aprop->u.number;
3953 aprop->u.number = features;
3955 else
3957 updated = true;
3958 bprop->u.number = features;
3961 else if (aprop != NULL)
3963 aprop->pr_kind = property_remove;
3964 updated = true;
3967 return updated;
3969 else
3971 /* Never should happen. */
3972 abort ();
3975 return updated;
3978 /* Set up x86 GNU properties. Return the first relocatable ELF input
3979 with GNU properties if found. Otherwise, return NULL. */
3981 bfd *
3982 _bfd_x86_elf_link_setup_gnu_properties
3983 (struct bfd_link_info *info, struct elf_x86_init_table *init_table)
3985 bool normal_target;
3986 bool lazy_plt;
3987 asection *sec, *pltsec;
3988 bfd *dynobj;
3989 bool use_ibt_plt;
3990 unsigned int plt_alignment, features, isa_level;
3991 struct elf_x86_link_hash_table *htab;
3992 bfd *pbfd;
3993 bfd *ebfd = NULL;
3994 elf_property *prop;
3995 const struct elf_backend_data *bed;
3996 unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
3997 unsigned int got_align;
3999 /* Find a normal input file with GNU property note. */
4000 for (pbfd = info->input_bfds;
4001 pbfd != NULL;
4002 pbfd = pbfd->link.next)
4003 if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
4004 && bfd_count_sections (pbfd) != 0)
4006 ebfd = pbfd;
4008 if (elf_properties (pbfd) != NULL)
4009 break;
4012 bed = get_elf_backend_data (info->output_bfd);
4014 htab = elf_x86_hash_table (info, bed->target_id);
4015 if (htab == NULL)
4016 return pbfd;
4018 features = 0;
4019 if (htab->params->ibt)
4021 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
4022 htab->params->cet_report &= ~prop_report_ibt;
4024 if (htab->params->shstk)
4026 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
4027 htab->params->cet_report &= ~prop_report_shstk;
4029 if (!(htab->params->cet_report & (prop_report_ibt | prop_report_shstk)))
4030 htab->params->cet_report = prop_report_none;
4031 if (htab->params->lam_u48)
4033 features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4034 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4035 htab->params->lam_u48_report = prop_report_none;
4036 htab->params->lam_u57_report = prop_report_none;
4038 else if (htab->params->lam_u57)
4040 features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
4041 htab->params->lam_u57_report = prop_report_none;
4044 switch (htab->params->isa_level)
4046 case 0:
4047 isa_level = 0;
4048 break;
4049 case 1:
4050 isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE;
4051 break;
4052 case 2:
4053 isa_level = GNU_PROPERTY_X86_ISA_1_V2;
4054 break;
4055 case 3:
4056 isa_level = GNU_PROPERTY_X86_ISA_1_V3;
4057 break;
4058 case 4:
4059 isa_level = GNU_PROPERTY_X86_ISA_1_V4;
4060 break;
4061 default:
4062 abort ();
4065 if (ebfd != NULL)
4067 prop = NULL;
4068 if (features)
4070 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT,
4071 GNU_PROPERTY_X86_FEATURE_1_SHSTK,
4072 GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
4073 GNU_PROPERTY_X86_FEATURE_1_LAM_U57. */
4074 prop = _bfd_elf_get_property (ebfd,
4075 GNU_PROPERTY_X86_FEATURE_1_AND,
4077 prop->u.number |= features;
4078 prop->pr_kind = property_number;
4081 if (isa_level)
4083 /* If ISA level is set, add GNU_PROPERTY_X86_ISA_1_NEEDED. */
4084 prop = _bfd_elf_get_property (ebfd,
4085 GNU_PROPERTY_X86_ISA_1_NEEDED,
4087 prop->u.number |= isa_level;
4088 prop->pr_kind = property_number;
4091 /* Create the GNU property note section if needed. */
4092 if (prop != NULL && pbfd == NULL)
4094 sec = bfd_make_section_with_flags (ebfd,
4095 NOTE_GNU_PROPERTY_SECTION_NAME,
4096 (SEC_ALLOC
4097 | SEC_LOAD
4098 | SEC_IN_MEMORY
4099 | SEC_READONLY
4100 | SEC_HAS_CONTENTS
4101 | SEC_DATA));
4102 if (sec == NULL)
4103 info->callbacks->einfo (_("%F%P: failed to create GNU property section\n"));
4105 if (!bfd_set_section_alignment (sec, class_align))
4107 error_alignment:
4108 info->callbacks->einfo (_("%F%pA: failed to align section\n"),
4109 sec);
4112 elf_section_type (sec) = SHT_NOTE;
4116 if (htab->params->cet_report
4117 || htab->params->lam_u48_report
4118 || htab->params->lam_u57_report)
4120 /* Report missing IBT, SHSTK and LAM properties. */
4121 bfd *abfd;
4122 const char *warning_msg = _("%P: %pB: warning: missing %s\n");
4123 const char *error_msg = _("%X%P: %pB: error: missing %s\n");
4124 const char *cet_msg = NULL;
4125 const char *lam_u48_msg = NULL;
4126 const char *lam_u57_msg = NULL;
4127 const char *missing;
4128 elf_property_list *p;
4129 bool missing_ibt, missing_shstk;
4130 bool missing_lam_u48, missing_lam_u57;
4131 bool check_ibt
4132 = (htab->params->cet_report
4133 && (htab->params->cet_report & prop_report_ibt));
4134 bool check_shstk
4135 = (htab->params->cet_report
4136 && (htab->params->cet_report & prop_report_shstk));
4138 if (htab->params->cet_report)
4140 if ((htab->params->cet_report & prop_report_warning))
4141 cet_msg = warning_msg;
4142 else
4143 cet_msg = error_msg;
4145 if (htab->params->lam_u48_report)
4147 if ((htab->params->lam_u48_report & prop_report_warning))
4148 lam_u48_msg = warning_msg;
4149 else
4150 lam_u48_msg = error_msg;
4152 if (htab->params->lam_u57_report)
4154 if ((htab->params->lam_u57_report & prop_report_warning))
4155 lam_u57_msg = warning_msg;
4156 else
4157 lam_u57_msg = error_msg;
4160 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
4161 if (!(abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED))
4162 && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
4164 for (p = elf_properties (abfd); p; p = p->next)
4165 if (p->property.pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
4166 break;
4168 missing_ibt = check_ibt;
4169 missing_shstk = check_shstk;
4170 missing_lam_u48 = !!lam_u48_msg;
4171 missing_lam_u57 = !!lam_u57_msg;
4172 if (p)
4174 missing_ibt &= !(p->property.u.number
4175 & GNU_PROPERTY_X86_FEATURE_1_IBT);
4176 missing_shstk &= !(p->property.u.number
4177 & GNU_PROPERTY_X86_FEATURE_1_SHSTK);
4178 missing_lam_u48 &= !(p->property.u.number
4179 & GNU_PROPERTY_X86_FEATURE_1_LAM_U48);
4180 missing_lam_u57 &= !(p->property.u.number
4181 & GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4183 if (missing_ibt || missing_shstk)
4185 if (missing_ibt && missing_shstk)
4186 missing = _("IBT and SHSTK properties");
4187 else if (missing_ibt)
4188 missing = _("IBT property");
4189 else
4190 missing = _("SHSTK property");
4191 info->callbacks->einfo (cet_msg, abfd, missing);
4193 if (missing_lam_u48)
4195 missing = _("LAM_U48 property");
4196 info->callbacks->einfo (lam_u48_msg, abfd, missing);
4198 if (missing_lam_u57)
4200 missing = _("LAM_U57 property");
4201 info->callbacks->einfo (lam_u57_msg, abfd, missing);
4206 pbfd = _bfd_elf_link_setup_gnu_properties (info);
4208 htab->r_info = init_table->r_info;
4209 htab->r_sym = init_table->r_sym;
4211 if (bfd_link_relocatable (info))
4212 return pbfd;
4214 htab->plt0_pad_byte = init_table->plt0_pad_byte;
4216 use_ibt_plt = htab->params->ibtplt || htab->params->ibt;
4217 if (!use_ibt_plt && pbfd != NULL)
4219 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
4220 elf_property_list *p;
4222 /* The property list is sorted in order of type. */
4223 for (p = elf_properties (pbfd); p; p = p->next)
4225 if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
4227 use_ibt_plt = !!(p->property.u.number
4228 & GNU_PROPERTY_X86_FEATURE_1_IBT);
4229 break;
4231 else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
4232 break;
4236 dynobj = htab->elf.dynobj;
4238 /* Set htab->elf.dynobj here so that there is no need to check and
4239 set it in check_relocs. */
4240 if (dynobj == NULL)
4242 if (pbfd != NULL)
4244 htab->elf.dynobj = pbfd;
4245 dynobj = pbfd;
4247 else
4249 bfd *abfd;
4251 /* Find a normal input file to hold linker created
4252 sections. */
4253 for (abfd = info->input_bfds;
4254 abfd != NULL;
4255 abfd = abfd->link.next)
4256 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
4257 && (abfd->flags
4258 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
4259 && bed->relocs_compatible (abfd->xvec,
4260 info->output_bfd->xvec))
4262 htab->elf.dynobj = abfd;
4263 dynobj = abfd;
4264 break;
4269 /* Return if there are no normal input files. */
4270 if (dynobj == NULL)
4271 return pbfd;
4273 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
4274 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
4275 canonical function address. */
4276 htab->plt.has_plt0 = 1;
4277 normal_target = htab->elf.target_os == is_normal;
4279 if (normal_target)
4281 if (use_ibt_plt)
4283 htab->lazy_plt = init_table->lazy_ibt_plt;
4284 htab->non_lazy_plt = init_table->non_lazy_ibt_plt;
4286 else
4288 htab->lazy_plt = init_table->lazy_plt;
4289 htab->non_lazy_plt = init_table->non_lazy_plt;
4292 else
4294 htab->lazy_plt = init_table->lazy_plt;
4295 htab->non_lazy_plt = NULL;
4298 pltsec = htab->elf.splt;
4300 if (htab->non_lazy_plt != NULL
4301 && (!htab->plt.has_plt0 || pltsec == NULL))
4302 lazy_plt = false;
4303 else
4304 lazy_plt = true;
4306 if (normal_target)
4308 if (use_ibt_plt)
4310 if (lazy_plt)
4311 htab->sframe_plt = init_table->sframe_lazy_ibt_plt;
4312 else
4313 htab->sframe_plt = init_table->sframe_non_lazy_ibt_plt;
4315 else
4317 if (lazy_plt)
4318 htab->sframe_plt = init_table->sframe_lazy_plt;
4319 else
4320 htab->sframe_plt = init_table->sframe_non_lazy_plt;
4323 else
4324 htab->sframe_plt = NULL;
4326 /* If the non-lazy PLT is available, use it for all PLT entries if
4327 there are no PLT0 or no .plt section. */
4328 if (!lazy_plt)
4330 if (bfd_link_pic (info))
4331 htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
4332 else
4333 htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
4334 htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
4335 htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
4336 htab->plt.plt_got_insn_size
4337 = htab->non_lazy_plt->plt_got_insn_size;
4338 htab->plt.eh_frame_plt_size
4339 = htab->non_lazy_plt->eh_frame_plt_size;
4340 htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
4342 else
4344 if (bfd_link_pic (info))
4346 htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
4347 htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
4349 else
4351 htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
4352 htab->plt.plt_entry = htab->lazy_plt->plt_entry;
4354 htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
4355 htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
4356 htab->plt.plt_got_insn_size
4357 = htab->lazy_plt->plt_got_insn_size;
4358 htab->plt.eh_frame_plt_size
4359 = htab->lazy_plt->eh_frame_plt_size;
4360 htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
4363 if (htab->elf.target_os == is_vxworks
4364 && !elf_vxworks_create_dynamic_sections (dynobj, info,
4365 &htab->srelplt2))
4367 info->callbacks->einfo (_("%F%P: failed to create VxWorks dynamic sections\n"));
4368 return pbfd;
4371 /* Since create_dynamic_sections isn't always called, but GOT
4372 relocations need GOT relocations, create them here so that we
4373 don't need to do it in check_relocs. */
4374 if (htab->elf.sgot == NULL
4375 && !_bfd_elf_create_got_section (dynobj, info))
4376 info->callbacks->einfo (_("%F%P: failed to create GOT sections\n"));
4378 got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
4380 /* Align .got and .got.plt sections to their entry size. Do it here
4381 instead of in create_dynamic_sections so that they are always
4382 properly aligned even if create_dynamic_sections isn't called. */
4383 sec = htab->elf.sgot;
4384 if (!bfd_set_section_alignment (sec, got_align))
4385 goto error_alignment;
4387 sec = htab->elf.sgotplt;
4388 if (!bfd_set_section_alignment (sec, got_align))
4389 goto error_alignment;
4391 /* Create the ifunc sections here so that check_relocs can be
4392 simplified. */
4393 if (!_bfd_elf_create_ifunc_sections (dynobj, info))
4394 info->callbacks->einfo (_("%F%P: failed to create ifunc sections\n"));
4396 plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
4398 if (pltsec != NULL)
4400 /* Whe creating executable, set the contents of the .interp
4401 section to the interpreter. */
4402 if (bfd_link_executable (info) && !info->nointerp)
4404 asection *s = bfd_get_linker_section (dynobj, ".interp");
4405 if (s == NULL)
4406 abort ();
4407 s->size = htab->dynamic_interpreter_size;
4408 s->contents = (unsigned char *) htab->dynamic_interpreter;
4409 htab->interp = s;
4412 if (normal_target)
4414 flagword pltflags = (bed->dynamic_sec_flags
4415 | SEC_ALLOC
4416 | SEC_CODE
4417 | SEC_LOAD
4418 | SEC_READONLY);
4419 unsigned int non_lazy_plt_alignment
4420 = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
4422 sec = pltsec;
4423 if (!bfd_set_section_alignment (sec, plt_alignment))
4424 goto error_alignment;
4426 /* Create the GOT procedure linkage table. */
4427 sec = bfd_make_section_anyway_with_flags (dynobj,
4428 ".plt.got",
4429 pltflags);
4430 if (sec == NULL)
4431 info->callbacks->einfo (_("%F%P: failed to create GOT PLT section\n"));
4433 if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
4434 goto error_alignment;
4436 htab->plt_got = sec;
4438 if (lazy_plt)
4440 sec = NULL;
4442 if (use_ibt_plt)
4444 /* Create the second PLT for Intel IBT support. IBT
4445 PLT is needed only for lazy binding. */
4446 sec = bfd_make_section_anyway_with_flags (dynobj,
4447 ".plt.sec",
4448 pltflags);
4449 if (sec == NULL)
4450 info->callbacks->einfo (_("%F%P: failed to create IBT-enabled PLT section\n"));
4452 if (!bfd_set_section_alignment (sec, plt_alignment))
4453 goto error_alignment;
4456 htab->plt_second = sec;
4460 if (!info->no_ld_generated_unwind_info)
4462 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4463 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4464 | SEC_LINKER_CREATED);
4466 sec = bfd_make_section_anyway_with_flags (dynobj,
4467 ".eh_frame",
4468 flags);
4469 if (sec == NULL)
4470 info->callbacks->einfo (_("%F%P: failed to create PLT .eh_frame section\n"));
4472 if (!bfd_set_section_alignment (sec, class_align))
4473 goto error_alignment;
4475 htab->plt_eh_frame = sec;
4477 if (htab->plt_got != NULL)
4479 sec = bfd_make_section_anyway_with_flags (dynobj,
4480 ".eh_frame",
4481 flags);
4482 if (sec == NULL)
4483 info->callbacks->einfo (_("%F%P: failed to create GOT PLT .eh_frame section\n"));
4485 if (!bfd_set_section_alignment (sec, class_align))
4486 goto error_alignment;
4488 htab->plt_got_eh_frame = sec;
4491 if (htab->plt_second != NULL)
4493 sec = bfd_make_section_anyway_with_flags (dynobj,
4494 ".eh_frame",
4495 flags);
4496 if (sec == NULL)
4497 info->callbacks->einfo (_("%F%P: failed to create the second PLT .eh_frame section\n"));
4499 if (!bfd_set_section_alignment (sec, class_align))
4500 goto error_alignment;
4502 htab->plt_second_eh_frame = sec;
4506 /* .sframe sections are emitted for AMD64 ABI only. */
4507 if (ABI_64_P (info->output_bfd) && !info->no_ld_generated_unwind_info)
4509 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4510 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4511 | SEC_LINKER_CREATED);
4513 sec = bfd_make_section_anyway_with_flags (dynobj,
4514 ".sframe",
4515 flags);
4516 if (sec == NULL)
4517 info->callbacks->einfo (_("%F%P: failed to create PLT .sframe section\n"));
4519 // FIXME check this
4520 // if (!bfd_set_section_alignment (sec, class_align))
4521 // goto error_alignment;
4523 htab->plt_sframe = sec;
4525 /* Second PLT is generated for Intel IBT + lazy plt. */
4526 if (htab->plt_second != NULL)
4528 sec = bfd_make_section_anyway_with_flags (dynobj,
4529 ".sframe",
4530 flags);
4531 if (sec == NULL)
4532 info->callbacks->einfo (_("%F%P: failed to create second PLT .sframe section\n"));
4534 htab->plt_second_sframe = sec;
4536 /* FIXME - add later for plt_got. */
4540 /* The .iplt section is used for IFUNC symbols in static
4541 executables. */
4542 sec = htab->elf.iplt;
4543 if (sec != NULL)
4545 /* NB: Delay setting its alignment until we know it is non-empty.
4546 Otherwise an empty iplt section may change vma and lma of the
4547 following sections, which triggers moving dot of the following
4548 section backwards, resulting in a warning and section lma not
4549 being set properly. It later leads to a "File truncated"
4550 error. */
4551 if (!bfd_set_section_alignment (sec, 0))
4552 goto error_alignment;
4554 htab->plt.iplt_alignment = (normal_target
4555 ? plt_alignment
4556 : bed->plt_alignment);
4559 if (bfd_link_executable (info)
4560 && !info->nointerp
4561 && !htab->params->has_dynamic_linker
4562 && htab->params->static_before_all_inputs)
4564 /* Report error for dynamic input objects if -static is passed at
4565 command-line before all input files without --dynamic-linker
4566 unless --no-dynamic-linker is used. */
4567 bfd *abfd;
4569 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
4570 if ((abfd->flags & DYNAMIC))
4571 info->callbacks->einfo
4572 (_("%X%P: attempted static link of dynamic object `%pB'\n"),
4573 abfd);
4576 return pbfd;
4579 /* Fix up x86 GNU properties. */
4581 void
4582 _bfd_x86_elf_link_fixup_gnu_properties
4583 (struct bfd_link_info *info, elf_property_list **listp)
4585 elf_property_list *p;
4587 for (p = *listp; p; p = p->next)
4589 unsigned int type = p->property.pr_type;
4590 if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
4591 || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4592 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
4593 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4594 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
4595 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
4596 || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
4597 && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
4599 if (p->property.u.number == 0
4600 && (type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4601 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
4602 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4603 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
4604 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)))
4606 /* Remove empty property. */
4607 *listp = p->next;
4608 continue;
4611 /* Keep LAM features only for 64-bit output. */
4612 if (type == GNU_PROPERTY_X86_FEATURE_1_AND
4613 && !ABI_64_P (info->output_bfd))
4614 p->property.u.number &= ~(GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4615 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4617 listp = &p->next;
4619 else if (type > GNU_PROPERTY_HIPROC)
4621 /* The property list is sorted in order of type. */
4622 break;
4627 void
4628 _bfd_elf_linker_x86_set_options (struct bfd_link_info * info,
4629 struct elf_linker_x86_params *params)
4631 const struct elf_backend_data *bed
4632 = get_elf_backend_data (info->output_bfd);
4633 struct elf_x86_link_hash_table *htab
4634 = elf_x86_hash_table (info, bed->target_id);
4635 if (htab != NULL)
4636 htab->params = params;