x86: decouple broadcast type and bytes fields
[binutils-gdb.git] / bfd / elfxx-x86.c
blob132fb791ac6dfc8fea481e8180f3e953d6b768e2
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)
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;
1061 relative_reloc->data[newidx].offset = offset;
1062 relative_reloc->data[newidx].address = 0;
1063 return true;
1066 /* After input sections have been mapped to output sections and
1067 addresses of output sections are set initiallly, scan input
1068 relocations with the same logic in relocate_section to determine
1069 if a relative relocation should be generated. Save the relative
1070 relocation candidate information for sizing the DT_RELR section
1071 later after all symbols addresses can be determined. */
1073 bool
1074 _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
1075 asection *input_section,
1076 struct bfd_link_info *info,
1077 bool *again)
1079 Elf_Internal_Shdr *symtab_hdr;
1080 Elf_Internal_Rela *internal_relocs;
1081 Elf_Internal_Rela *irel, *irelend;
1082 Elf_Internal_Sym *isymbuf = NULL;
1083 struct elf_link_hash_entry **sym_hashes;
1084 const struct elf_backend_data *bed;
1085 struct elf_x86_link_hash_table *htab;
1086 bfd_vma *local_got_offsets;
1087 bool is_x86_64;
1088 bool unaligned_section;
1090 if (bfd_link_relocatable (info))
1091 return true;
1093 /* Assume we're not going to change any sizes, and we'll only need
1094 one pass. */
1095 *again = false;
1097 bed = get_elf_backend_data (abfd);
1098 htab = elf_x86_hash_table (info, bed->target_id);
1099 if (htab == NULL)
1100 return true;
1102 /* Nothing to do if there are no relocations or relative relocations
1103 have been packed. */
1104 if (input_section == htab->elf.srelrdyn
1105 || input_section->relative_reloc_packed
1106 || ((input_section->flags & (SEC_RELOC | SEC_ALLOC))
1107 != (SEC_RELOC | SEC_ALLOC))
1108 || (input_section->flags & SEC_DEBUGGING) != 0
1109 || input_section->reloc_count == 0)
1110 return true;
1112 /* Skip if the section isn't aligned. */
1113 unaligned_section = input_section->alignment_power == 0;
1115 is_x86_64 = bed->target_id == X86_64_ELF_DATA;
1117 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1118 sym_hashes = elf_sym_hashes (abfd);
1119 local_got_offsets = elf_local_got_offsets (abfd);
1121 /* Load the relocations for this section. */
1122 internal_relocs =
1123 _bfd_elf_link_read_relocs (abfd, input_section, NULL,
1124 (Elf_Internal_Rela *) NULL,
1125 info->keep_memory);
1126 if (internal_relocs == NULL)
1127 return false;
1129 irelend = internal_relocs + input_section->reloc_count;
1130 for (irel = internal_relocs; irel < irelend; irel++)
1132 unsigned int r_type;
1133 unsigned int r_symndx;
1134 Elf_Internal_Sym *isym;
1135 struct elf_link_hash_entry *h;
1136 struct elf_x86_link_hash_entry *eh;
1137 bfd_vma offset;
1138 bool resolved_to_zero;
1139 bool need_copy_reloc_in_pie;
1140 bool pc32_reloc;
1141 asection *sec;
1142 /* Offset must be a multiple of 2. */
1143 bool unaligned_offset = (irel->r_offset & 1) != 0;
1144 /* True if there is a relative relocation against a dynamic
1145 symbol. */
1146 bool dynamic_relative_reloc_p;
1148 /* Get the value of the symbol referred to by the reloc. */
1149 r_symndx = htab->r_sym (irel->r_info);
1151 r_type = ELF32_R_TYPE (irel->r_info);
1152 /* Clear the R_X86_64_converted_reloc_bit bit. */
1153 r_type &= ~R_X86_64_converted_reloc_bit;
1155 sec = NULL;
1156 h = NULL;
1157 dynamic_relative_reloc_p = false;
1159 if (r_symndx < symtab_hdr->sh_info)
1161 /* Read this BFD's local symbols. */
1162 if (isymbuf == NULL)
1164 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1165 if (isymbuf == NULL)
1166 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1167 symtab_hdr->sh_info, 0,
1168 NULL, NULL, NULL);
1169 if (isymbuf == NULL)
1170 goto error_return;
1173 isym = isymbuf + r_symndx;
1174 switch (isym->st_shndx)
1176 case SHN_ABS:
1177 sec = bfd_abs_section_ptr;
1178 break;
1179 case SHN_COMMON:
1180 sec = bfd_com_section_ptr;
1181 break;
1182 case SHN_X86_64_LCOMMON:
1183 if (!is_x86_64)
1184 abort ();
1185 sec = &_bfd_elf_large_com_section;
1186 break;
1187 default:
1188 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1189 break;
1192 /* Skip relocation against local STT_GNU_IFUNC symbol. */
1193 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1194 continue;
1196 eh = (struct elf_x86_link_hash_entry *) h;
1197 resolved_to_zero = false;
1199 else
1201 /* Get H and SEC for GENERATE_DYNAMIC_RELOCATION_P below. */
1202 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1203 while (h->root.type == bfd_link_hash_indirect
1204 || h->root.type == bfd_link_hash_warning)
1205 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1207 if (h->root.type == bfd_link_hash_defined
1208 || h->root.type == bfd_link_hash_defweak)
1209 sec = h->root.u.def.section;
1211 /* Skip relocation against STT_GNU_IFUNC symbol. */
1212 if (h->type == STT_GNU_IFUNC)
1213 continue;
1215 eh = (struct elf_x86_link_hash_entry *) h;
1216 resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
1218 /* NB: See how elf_backend_finish_dynamic_symbol is called
1219 from elf_link_output_extsym. */
1220 if ((h->dynindx != -1 || h->forced_local)
1221 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1222 || h->root.type != bfd_link_hash_undefweak)
1223 || !h->forced_local)
1224 && h->got.offset != (bfd_vma) -1
1225 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
1226 && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
1227 && !resolved_to_zero
1228 && SYMBOL_REFERENCES_LOCAL_P (info, h)
1229 && SYMBOL_DEFINED_NON_SHARED_P (h))
1230 dynamic_relative_reloc_p = true;
1232 isym = NULL;
1235 if (X86_GOT_TYPE_P (is_x86_64, r_type))
1237 /* Pack GOT relative relocations. There should be only a
1238 single R_*_RELATIVE relocation in GOT. */
1239 if (eh != NULL)
1241 if (eh->got_relative_reloc_done)
1242 continue;
1244 if (!(dynamic_relative_reloc_p
1245 || (RESOLVED_LOCALLY_P (info, h, htab)
1246 && GENERATE_RELATIVE_RELOC_P (info, h))))
1247 continue;
1249 if (!dynamic_relative_reloc_p)
1250 eh->no_finish_dynamic_symbol = 1;
1251 eh->got_relative_reloc_done = 1;
1252 offset = h->got.offset;
1254 else
1256 if (elf_x86_relative_reloc_done (abfd)[r_symndx])
1257 continue;
1259 if (!X86_LOCAL_GOT_RELATIVE_RELOC_P (is_x86_64, info,
1260 isym))
1261 continue;
1263 elf_x86_relative_reloc_done (abfd)[r_symndx] = 1;
1264 offset = local_got_offsets[r_symndx];
1267 if (!elf_x86_relative_reloc_record_add (info,
1268 &htab->relative_reloc,
1269 irel, htab->elf.sgot,
1270 sec, h, isym, offset))
1271 goto error_return;
1273 continue;
1276 if (is_x86_64
1277 && irel->r_addend == 0
1278 && !ABI_64_P (info->output_bfd))
1280 /* For x32, if addend is zero, treat R_X86_64_64 like
1281 R_X86_64_32 and R_X86_64_SIZE64 like R_X86_64_SIZE32. */
1282 if (r_type == R_X86_64_64)
1283 r_type = R_X86_64_32;
1284 else if (r_type == R_X86_64_SIZE64)
1285 r_type = R_X86_64_SIZE32;
1288 if (!X86_RELATIVE_RELOC_TYPE_P (is_x86_64, r_type))
1289 continue;
1291 /* Pack non-GOT relative relocations. */
1292 if (is_x86_64)
1294 need_copy_reloc_in_pie =
1295 (bfd_link_pie (info)
1296 && h != NULL
1297 && (h->needs_copy
1298 || eh->needs_copy
1299 || (h->root.type == bfd_link_hash_undefined))
1300 && (X86_PCREL_TYPE_P (true, r_type)
1301 || X86_SIZE_TYPE_P (true, r_type)));
1302 pc32_reloc = false;
1304 else
1306 need_copy_reloc_in_pie = false;
1307 pc32_reloc = r_type == R_386_PC32;
1310 if (GENERATE_DYNAMIC_RELOCATION_P (is_x86_64, info, eh, r_type,
1311 sec, need_copy_reloc_in_pie,
1312 resolved_to_zero, pc32_reloc))
1314 /* When generating a shared object, these relocations
1315 are copied into the output file to be resolved at run
1316 time. */
1317 offset = _bfd_elf_section_offset (info->output_bfd, info,
1318 input_section,
1319 irel->r_offset);
1320 if (offset == (bfd_vma) -1
1321 || offset == (bfd_vma) -2
1322 || COPY_INPUT_RELOC_P (is_x86_64, info, h, r_type))
1323 continue;
1325 /* This symbol is local, or marked to become local. When
1326 relocation overflow check is disabled, we convert
1327 R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
1328 if (is_x86_64
1329 && !(r_type == htab->pointer_r_type
1330 || (r_type == R_X86_64_32
1331 && htab->params->no_reloc_overflow_check)))
1332 continue;
1334 if (!elf_x86_relative_reloc_record_add
1335 (info,
1336 ((unaligned_section || unaligned_offset)
1337 ? &htab->unaligned_relative_reloc
1338 : &htab->relative_reloc),
1339 irel, input_section, sec, h, isym, offset))
1340 goto error_return;
1344 input_section->relative_reloc_packed = 1;
1346 return true;
1348 error_return:
1349 if ((unsigned char *) isymbuf != symtab_hdr->contents)
1350 free (isymbuf);
1351 if (elf_section_data (input_section)->relocs != internal_relocs)
1352 free (internal_relocs);
1353 return false;
1356 /* Add an entry to the 64-bit DT_RELR bitmap. */
1358 static void
1359 elf64_dt_relr_bitmap_add
1360 (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
1361 uint64_t entry)
1363 bfd_size_type newidx;
1365 if (bitmap->u.elf64 == NULL)
1367 bitmap->u.elf64 = bfd_malloc (sizeof (uint64_t));
1368 bitmap->count = 0;
1369 bitmap->size = 1;
1372 newidx = bitmap->count++;
1374 if (bitmap->count > bitmap->size)
1376 bitmap->size <<= 1;
1377 bitmap->u.elf64 = bfd_realloc (bitmap->u.elf64,
1378 (bitmap->size * sizeof (uint64_t)));
1381 if (bitmap->u.elf64 == NULL)
1383 info->callbacks->einfo
1384 /* xgettext:c-format */
1385 (_("%F%P: %pB: failed to allocate 64-bit DT_RELR bitmap\n"),
1386 info->output_bfd);
1389 bitmap->u.elf64[newidx] = entry;
1392 /* Add an entry to the 32-bit DT_RELR bitmap. */
1394 static void
1395 elf32_dt_relr_bitmap_add
1396 (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
1397 uint32_t entry)
1399 bfd_size_type newidx;
1401 if (bitmap->u.elf32 == NULL)
1403 bitmap->u.elf32 = bfd_malloc (sizeof (uint32_t));
1404 bitmap->count = 0;
1405 bitmap->size = 1;
1408 newidx = bitmap->count++;
1410 if (bitmap->count > bitmap->size)
1412 bitmap->size <<= 1;
1413 bitmap->u.elf32 = bfd_realloc (bitmap->u.elf32,
1414 (bitmap->size * sizeof (uint32_t)));
1417 if (bitmap->u.elf32 == NULL)
1419 info->callbacks->einfo
1420 /* xgettext:c-format */
1421 (_("%F%P: %pB: failed to allocate 32-bit DT_RELR bitmap\n"),
1422 info->output_bfd);
1425 bitmap->u.elf32[newidx] = entry;
1428 void
1429 _bfd_elf32_write_addend (bfd *abfd, uint64_t value, void *addr)
1431 bfd_put_32 (abfd, value, addr);
1434 void
1435 _bfd_elf64_write_addend (bfd *abfd, uint64_t value, void *addr)
1437 bfd_put_64 (abfd, value, addr);
1440 /* Size or finish relative relocations to determine the run-time
1441 addresses for DT_RELR bitmap computation later. OUTREL is set
1442 to NULL in the sizing phase and non-NULL in the finising phase
1443 where the regular relative relocations will be written out. */
1445 static void
1446 elf_x86_size_or_finish_relative_reloc
1447 (bool is_x86_64, struct bfd_link_info *info,
1448 struct elf_x86_link_hash_table *htab, bool unaligned,
1449 Elf_Internal_Rela *outrel)
1451 unsigned int align_mask;
1452 bfd_size_type i, count;
1453 asection *sec, *srel;
1454 struct elf_link_hash_entry *h;
1455 bfd_vma offset;
1456 Elf_Internal_Sym *sym;
1457 asection *sym_sec;
1458 asection *sgot = htab->elf.sgot;
1459 asection *srelgot = htab->elf.srelgot;
1460 struct elf_x86_relative_reloc_data *relative_reloc;
1462 if (unaligned)
1464 align_mask = 0;
1465 relative_reloc = &htab->unaligned_relative_reloc;
1467 else
1469 align_mask = 1;
1470 relative_reloc = &htab->relative_reloc;
1473 count = relative_reloc->count;
1474 for (i = 0; i < count; i++)
1476 sec = relative_reloc->data[i].sec;
1477 sym = relative_reloc->data[i].sym;
1479 /* If SYM is NULL, it must be a global symbol. */
1480 if (sym == NULL)
1481 h = relative_reloc->data[i].u.h;
1482 else
1483 h = NULL;
1485 if (is_x86_64)
1487 bfd_vma relocation;
1488 /* This function may be called more than once and REL may be
1489 updated by _bfd_elf_rela_local_sym below. */
1490 Elf_Internal_Rela rel = relative_reloc->data[i].rel;
1492 if (h != NULL)
1494 if (h->root.type == bfd_link_hash_defined
1495 || h->root.type == bfd_link_hash_defweak)
1497 sym_sec = h->root.u.def.section;
1498 relocation = (h->root.u.def.value
1499 + sym_sec->output_section->vma
1500 + sym_sec->output_offset);
1502 else
1504 /* Allow undefined symbol only at the sizing phase.
1505 Otherwise skip undefined symbol here. Undefined
1506 symbol will be reported by relocate_section. */
1507 if (outrel == NULL)
1508 relocation = 0;
1509 else
1510 continue;
1513 else
1515 sym_sec = relative_reloc->data[i].u.sym_sec;
1516 relocation = _bfd_elf_rela_local_sym
1517 (info->output_bfd, sym, &sym_sec, &rel);
1520 if (outrel != NULL)
1522 outrel->r_addend = relocation;
1523 if (sec == sgot)
1525 if (h != NULL && h->needs_plt)
1526 abort ();
1528 else
1529 outrel->r_addend += rel.r_addend;
1531 /* Write the implicit addend if ALIGN_MASK isn't 0. */
1532 if (align_mask)
1534 if (sec == sgot)
1536 if (relative_reloc->data[i].offset >= sec->size)
1537 abort ();
1538 htab->elf_write_addend_in_got
1539 (info->output_bfd, outrel->r_addend,
1540 sec->contents + relative_reloc->data[i].offset);
1542 else
1544 bfd_byte *contents;
1546 if (rel.r_offset >= sec->size)
1547 abort ();
1549 if (elf_section_data (sec)->this_hdr.contents
1550 != NULL)
1551 contents
1552 = elf_section_data (sec)->this_hdr.contents;
1553 else
1555 if (!bfd_malloc_and_get_section (sec->owner,
1556 sec,
1557 &contents))
1558 info->callbacks->einfo
1559 /* xgettext:c-format */
1560 (_("%F%P: %pB: failed to allocate memory for section `%pA'\n"),
1561 info->output_bfd, sec);
1563 /* Cache the section contents for
1564 elf_link_input_bfd. */
1565 elf_section_data (sec)->this_hdr.contents
1566 = contents;
1568 htab->elf_write_addend
1569 (info->output_bfd, outrel->r_addend,
1570 contents + rel.r_offset);
1576 if (sec == sgot)
1577 srel = srelgot;
1578 else
1579 srel = elf_section_data (sec)->sreloc;
1580 offset = (sec->output_section->vma + sec->output_offset
1581 + relative_reloc->data[i].offset);
1582 relative_reloc->data[i].address = offset;
1583 if (outrel != NULL)
1585 outrel->r_offset = offset;
1587 if ((outrel->r_offset & align_mask) != 0)
1588 abort ();
1590 if (htab->params->report_relative_reloc)
1591 _bfd_x86_elf_link_report_relative_reloc
1592 (info, sec, h, sym, htab->relative_r_name, outrel);
1594 /* Generate regular relative relocation if ALIGN_MASK is 0. */
1595 if (align_mask == 0)
1596 htab->elf_append_reloc (info->output_bfd, srel, outrel);
1601 /* Compute the DT_RELR section size. Set NEED_PLAYOUT to true if
1602 the DT_RELR section size has been increased. */
1604 static void
1605 elf_x86_compute_dl_relr_bitmap
1606 (struct bfd_link_info *info, struct elf_x86_link_hash_table *htab,
1607 bool *need_layout)
1609 bfd_vma base;
1610 bfd_size_type i, count, new_count;
1611 struct elf_x86_relative_reloc_data *relative_reloc =
1612 &htab->relative_reloc;
1613 /* Save the old DT_RELR bitmap count. Don't shrink the DT_RELR bitmap
1614 if the new DT_RELR bitmap count is smaller than the old one. Pad
1615 with trailing 1s which won't be decoded to more relocations. */
1616 bfd_size_type dt_relr_bitmap_count = htab->dt_relr_bitmap.count;
1618 /* Clear the DT_RELR bitmap count. */
1619 htab->dt_relr_bitmap.count = 0;
1621 count = relative_reloc->count;
1623 if (ABI_64_P (info->output_bfd))
1625 /* Compute the 64-bit DT_RELR bitmap. */
1626 i = 0;
1627 while (i < count)
1629 if ((relative_reloc->data[i].address % 1) != 0)
1630 abort ();
1632 elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1633 relative_reloc->data[i].address);
1635 base = relative_reloc->data[i].address + 8;
1636 i++;
1638 while (i < count)
1640 uint64_t bitmap = 0;
1641 for (; i < count; i++)
1643 bfd_vma delta = (relative_reloc->data[i].address
1644 - base);
1645 /* Stop if it is too far from base. */
1646 if (delta >= 63 * 8)
1647 break;
1648 /* Stop if it isn't a multiple of 8. */
1649 if ((delta % 8) != 0)
1650 break;
1651 bitmap |= 1ULL << (delta / 8);
1654 if (bitmap == 0)
1655 break;
1657 elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1658 (bitmap << 1) | 1);
1660 base += 63 * 8;
1664 new_count = htab->dt_relr_bitmap.count;
1665 if (dt_relr_bitmap_count > new_count)
1667 /* Don't shrink the DT_RELR section size to avoid section
1668 layout oscillation. Instead, pad the DT_RELR bitmap with
1669 1s which do not decode to more relocations. */
1671 htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
1672 count = dt_relr_bitmap_count - new_count;
1673 for (i = 0; i < count; i++)
1674 htab->dt_relr_bitmap.u.elf64[new_count + i] = 1;
1677 else
1679 /* Compute the 32-bit DT_RELR bitmap. */
1680 i = 0;
1681 while (i < count)
1683 if ((relative_reloc->data[i].address % 1) != 0)
1684 abort ();
1686 elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1687 relative_reloc->data[i].address);
1689 base = relative_reloc->data[i].address + 4;
1690 i++;
1692 while (i < count)
1694 uint32_t bitmap = 0;
1695 for (; i < count; i++)
1697 bfd_vma delta = (relative_reloc->data[i].address
1698 - base);
1699 /* Stop if it is too far from base. */
1700 if (delta >= 31 * 4)
1701 break;
1702 /* Stop if it isn't a multiple of 4. */
1703 if ((delta % 4) != 0)
1704 break;
1705 bitmap |= 1ULL << (delta / 4);
1708 if (bitmap == 0)
1709 break;
1711 elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1712 (bitmap << 1) | 1);
1714 base += 31 * 4;
1718 new_count = htab->dt_relr_bitmap.count;
1719 if (dt_relr_bitmap_count > new_count)
1721 /* Don't shrink the DT_RELR section size to avoid section
1722 layout oscillation. Instead, pad the DT_RELR bitmap with
1723 1s which do not decode to more relocations. */
1725 htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
1726 count = dt_relr_bitmap_count - new_count;
1727 for (i = 0; i < count; i++)
1728 htab->dt_relr_bitmap.u.elf32[new_count + i] = 1;
1732 if (htab->dt_relr_bitmap.count != dt_relr_bitmap_count)
1734 if (need_layout)
1736 /* The .relr.dyn section size is changed. Update the section
1737 size and tell linker to layout sections again. */
1738 htab->elf.srelrdyn->size =
1739 (htab->dt_relr_bitmap.count
1740 * (ABI_64_P (info->output_bfd) ? 8 : 4));
1742 *need_layout = true;
1744 else
1745 info->callbacks->einfo
1746 /* xgettext:c-format */
1747 (_("%F%P: %pB: size of compact relative reloc section is "
1748 "changed: new (%lu) != old (%lu)\n"),
1749 info->output_bfd, htab->dt_relr_bitmap.count,
1750 dt_relr_bitmap_count);
1754 /* Write out the DT_RELR section. */
1756 static void
1757 elf_x86_write_dl_relr_bitmap (struct bfd_link_info *info,
1758 struct elf_x86_link_hash_table *htab)
1760 asection *sec = htab->elf.srelrdyn;
1761 bfd_size_type size = sec->size;
1762 bfd_size_type i;
1763 unsigned char *contents;
1765 contents = (unsigned char *) bfd_alloc (sec->owner, size);
1766 if (contents == NULL)
1767 info->callbacks->einfo
1768 /* xgettext:c-format */
1769 (_("%F%P: %pB: failed to allocate compact relative reloc section\n"),
1770 info->output_bfd);
1772 /* Cache the section contents for elf_link_input_bfd. */
1773 sec->contents = contents;
1775 if (ABI_64_P (info->output_bfd))
1776 for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 8)
1777 bfd_put_64 (info->output_bfd, htab->dt_relr_bitmap.u.elf64[i],
1778 contents);
1779 else
1780 for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 4)
1781 bfd_put_32 (info->output_bfd, htab->dt_relr_bitmap.u.elf32[i],
1782 contents);
1785 /* Sort relative relocations by address. */
1787 static int
1788 elf_x86_relative_reloc_compare (const void *pa, const void *pb)
1790 struct elf_x86_relative_reloc_record *a =
1791 (struct elf_x86_relative_reloc_record *) pa;
1792 struct elf_x86_relative_reloc_record *b =
1793 (struct elf_x86_relative_reloc_record *) pb;
1794 if (a->address < b->address)
1795 return -1;
1796 if (a->address > b->address)
1797 return 1;
1798 return 0;
1801 enum dynobj_sframe_plt_type
1803 SFRAME_PLT = 1,
1804 SFRAME_PLT_SEC = 2
1807 /* Create SFrame stack trace info for the plt entries in the .plt section
1808 of type PLT_SEC_TYPE. */
1810 static bool
1811 _bfd_x86_elf_create_sframe_plt (bfd *output_bfd,
1812 struct bfd_link_info *info,
1813 unsigned int plt_sec_type)
1815 struct elf_x86_link_hash_table *htab;
1816 const struct elf_backend_data *bed;
1818 bool plt0_generated_p;
1819 unsigned int plt0_entry_size;
1820 unsigned char func_info;
1821 unsigned int fre_type;
1822 /* The dynamic plt section for which .sframe stack trace information is being
1823 created. */
1824 asection *dpltsec;
1826 int err = 0;
1828 sframe_encoder_ctx **ectx = NULL;
1829 unsigned plt_entry_size = 0;
1830 unsigned int num_pltn_fres = 0;
1831 unsigned int num_pltn_entries = 0;
1833 bed = get_elf_backend_data (output_bfd);
1834 htab = elf_x86_hash_table (info, bed->target_id);
1835 /* Whether SFrame stack trace info for plt0 is to be generated. */
1836 plt0_generated_p = htab->plt.has_plt0;
1837 plt0_entry_size
1838 = (plt0_generated_p) ? htab->sframe_plt->plt0_entry_size : 0;
1840 switch (plt_sec_type)
1842 case SFRAME_PLT:
1844 ectx = &htab->plt_cfe_ctx;
1845 dpltsec = htab->elf.splt;
1847 plt_entry_size = htab->plt.plt_entry_size;
1848 num_pltn_fres = htab->sframe_plt->pltn_num_fres;
1849 num_pltn_entries
1850 = (htab->elf.splt->size - plt0_entry_size) / plt_entry_size;
1852 break;
1854 case SFRAME_PLT_SEC:
1856 ectx = &htab->plt_second_cfe_ctx;
1857 /* FIXME - this or htab->plt_second_sframe ? */
1858 dpltsec = htab->plt_second_eh_frame;
1860 plt_entry_size = htab->sframe_plt->sec_pltn_entry_size;
1861 num_pltn_fres = htab->sframe_plt->sec_pltn_num_fres;
1862 num_pltn_entries
1863 = htab->plt_second_eh_frame->size / plt_entry_size;
1864 break;
1866 default:
1867 /* No other value is possible. */
1868 return false;
1869 break;
1872 *ectx = sframe_encode (SFRAME_VERSION_1,
1874 SFRAME_ABI_AMD64_ENDIAN_LITTLE,
1875 SFRAME_CFA_FIXED_FP_INVALID,
1876 -8, /* Fixed RA offset. */
1877 &err);
1879 /* FRE type is dependent on the size of the function. */
1880 fre_type = sframe_calc_fre_type (dpltsec->size);
1881 func_info = sframe_fde_create_func_info (fre_type, SFRAME_FDE_TYPE_PCINC);
1883 /* Add SFrame FDE and the associated FREs for plt0 if plt0 has been
1884 generated. */
1885 if (plt0_generated_p)
1887 /* Add SFrame FDE for plt0, the function start address is updated later
1888 at _bfd_elf_merge_section_sframe time. */
1889 sframe_encoder_add_funcdesc (*ectx,
1890 0, /* func start addr. */
1891 plt0_entry_size,
1892 func_info,
1893 0 /* Num FREs. */);
1894 sframe_frame_row_entry plt0_fre;
1895 unsigned int num_plt0_fres = htab->sframe_plt->plt0_num_fres;
1896 for (unsigned int j = 0; j < num_plt0_fres; j++)
1898 plt0_fre = *(htab->sframe_plt->plt0_fres[j]);
1899 sframe_encoder_add_fre (*ectx, 0, &plt0_fre);
1904 if (num_pltn_entries)
1906 /* pltn entries use an SFrame FDE of type
1907 SFRAME_FDE_TYPE_PCMASK to exploit the repetitive
1908 pattern of the instructions in these entries. Using this SFrame FDE
1909 type helps in keeping the SFrame stack trace info for pltn entries
1910 compact. */
1911 func_info = sframe_fde_create_func_info (fre_type,
1912 SFRAME_FDE_TYPE_PCMASK);
1913 /* Add the SFrame FDE for all PCs starting at the first pltn entry (hence,
1914 function start address = plt0_entry_size. As usual, this will be
1915 updated later at _bfd_elf_merge_section_sframe, by when the
1916 sections are relocated. */
1917 sframe_encoder_add_funcdesc (*ectx,
1918 plt0_entry_size, /* func start addr. */
1919 dpltsec->size - plt0_entry_size,
1920 func_info,
1921 0 /* Num FREs. */);
1923 sframe_frame_row_entry pltn_fre;
1924 /* Now add the FREs for pltn. Simply adding the two FREs suffices due
1925 to the usage of SFRAME_FDE_TYPE_PCMASK above. */
1926 for (unsigned int j = 0; j < num_pltn_fres; j++)
1928 pltn_fre = *(htab->sframe_plt->pltn_fres[j]);
1929 sframe_encoder_add_fre (*ectx, 1, &pltn_fre);
1933 return true;
1936 /* Put contents of the .sframe section corresponding to the specified
1937 PLT_SEC_TYPE. */
1939 static bool
1940 _bfd_x86_elf_write_sframe_plt (bfd *output_bfd,
1941 struct bfd_link_info *info,
1942 unsigned int plt_sec_type)
1944 struct elf_x86_link_hash_table *htab;
1945 const struct elf_backend_data *bed;
1946 sframe_encoder_ctx *ectx;
1947 size_t sec_size;
1948 asection *sec;
1949 bfd *dynobj;
1951 int err = 0;
1953 bed = get_elf_backend_data (output_bfd);
1954 htab = elf_x86_hash_table (info, bed->target_id);
1955 dynobj = htab->elf.dynobj;
1957 switch (plt_sec_type)
1959 case SFRAME_PLT:
1960 ectx = htab->plt_cfe_ctx;
1961 sec = htab->plt_sframe;
1962 break;
1963 case SFRAME_PLT_SEC:
1964 ectx = htab->plt_second_cfe_ctx;
1965 sec = htab->plt_second_sframe;
1966 break;
1967 default:
1968 /* No other value is possible. */
1969 return false;
1970 break;
1973 BFD_ASSERT (ectx);
1975 void *contents = sframe_encoder_write (ectx, &sec_size, &err);
1977 sec->size = (bfd_size_type) sec_size;
1978 sec->contents = (unsigned char *) bfd_zalloc (dynobj, sec->size);
1979 memcpy (sec->contents, contents, sec_size);
1981 sframe_encoder_free (&ectx);
1983 return true;
1986 bool
1987 _bfd_elf_x86_size_relative_relocs (struct bfd_link_info *info,
1988 bool *need_layout)
1990 struct elf_x86_link_hash_table *htab;
1991 const struct elf_backend_data *bed;
1992 bool is_x86_64;
1993 bfd_size_type i, count, unaligned_count;
1994 asection *sec, *srel;
1996 /* Do nothing for ld -r. */
1997 if (bfd_link_relocatable (info))
1998 return true;
2000 bed = get_elf_backend_data (info->output_bfd);
2001 htab = elf_x86_hash_table (info, bed->target_id);
2002 if (htab == NULL)
2003 return false;
2005 count = htab->relative_reloc.count;
2006 unaligned_count = htab->unaligned_relative_reloc.count;
2007 if (count == 0)
2009 if (htab->generate_relative_reloc_pass == 0
2010 && htab->elf.srelrdyn != NULL)
2012 /* Remove the empty .relr.dyn sections now. */
2013 if (!bfd_is_abs_section (htab->elf.srelrdyn->output_section))
2015 bfd_section_list_remove
2016 (info->output_bfd, htab->elf.srelrdyn->output_section);
2017 info->output_bfd->section_count--;
2019 bfd_section_list_remove (htab->elf.srelrdyn->owner,
2020 htab->elf.srelrdyn);
2021 htab->elf.srelrdyn->owner->section_count--;
2023 if (unaligned_count == 0)
2025 htab->generate_relative_reloc_pass++;
2026 return true;
2030 is_x86_64 = bed->target_id == X86_64_ELF_DATA;
2032 /* Size relative relocations. */
2033 if (htab->generate_relative_reloc_pass)
2035 /* Reset the regular relative relocation count. */
2036 for (i = 0; i < unaligned_count; i++)
2038 sec = htab->unaligned_relative_reloc.data[i].sec;
2039 srel = elf_section_data (sec)->sreloc;
2040 srel->reloc_count = 0;
2043 else
2045 /* Remove the reserved space for compact relative relocations. */
2046 if (count)
2048 asection *sgot = htab->elf.sgot;
2049 asection *srelgot = htab->elf.srelgot;
2051 for (i = 0; i < count; i++)
2053 sec = htab->relative_reloc.data[i].sec;
2054 if (sec == sgot)
2055 srel = srelgot;
2056 else
2057 srel = elf_section_data (sec)->sreloc;
2058 srel->size -= htab->sizeof_reloc;
2063 /* Size unaligned relative relocations. */
2064 if (unaligned_count)
2065 elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2066 true, NULL);
2068 if (count)
2070 elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2071 false, NULL);
2073 /* Sort relative relocations by addresses. We only need to
2074 sort them in the first pass since the relative positions
2075 won't change. */
2076 if (htab->generate_relative_reloc_pass == 0)
2077 qsort (htab->relative_reloc.data, count,
2078 sizeof (struct elf_x86_relative_reloc_record),
2079 elf_x86_relative_reloc_compare);
2081 elf_x86_compute_dl_relr_bitmap (info, htab, need_layout);
2084 htab->generate_relative_reloc_pass++;
2086 return true;
2089 bool
2090 _bfd_elf_x86_finish_relative_relocs (struct bfd_link_info *info)
2092 struct elf_x86_link_hash_table *htab;
2093 const struct elf_backend_data *bed;
2094 Elf_Internal_Rela outrel;
2095 bool is_x86_64;
2096 bfd_size_type count;
2098 /* Do nothing for ld -r. */
2099 if (bfd_link_relocatable (info))
2100 return true;
2102 bed = get_elf_backend_data (info->output_bfd);
2103 htab = elf_x86_hash_table (info, bed->target_id);
2104 if (htab == NULL)
2105 return false;
2107 is_x86_64 = bed->target_id == X86_64_ELF_DATA;
2109 outrel.r_info = htab->r_info (0, htab->relative_r_type);
2111 if (htab->unaligned_relative_reloc.count)
2112 elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2113 true, &outrel);
2115 count = htab->relative_reloc.count;
2116 if (count)
2118 elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2119 false, &outrel);
2121 elf_x86_compute_dl_relr_bitmap (info, htab, NULL);
2123 elf_x86_write_dl_relr_bitmap (info, htab);
2126 return true;
2129 bool
2130 _bfd_elf_x86_valid_reloc_p (asection *input_section,
2131 struct bfd_link_info *info,
2132 struct elf_x86_link_hash_table *htab,
2133 const Elf_Internal_Rela *rel,
2134 struct elf_link_hash_entry *h,
2135 Elf_Internal_Sym *sym,
2136 Elf_Internal_Shdr *symtab_hdr,
2137 bool *no_dynreloc_p)
2139 bool valid_p = true;
2141 *no_dynreloc_p = false;
2143 /* Check If relocation against non-preemptible absolute symbol is
2144 valid in PIC. FIXME: Can't use SYMBOL_REFERENCES_LOCAL_P since
2145 it may call _bfd_elf_link_hide_sym_by_version and result in
2146 ld-elfvers/ vers21 test failure. */
2147 if (bfd_link_pic (info)
2148 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
2150 const struct elf_backend_data *bed;
2151 unsigned int r_type;
2152 Elf_Internal_Rela irel;
2154 /* Skip non-absolute symbol. */
2155 if (h)
2157 if (!ABS_SYMBOL_P (h))
2158 return valid_p;
2160 else if (sym->st_shndx != SHN_ABS)
2161 return valid_p;
2163 bed = get_elf_backend_data (input_section->owner);
2164 r_type = ELF32_R_TYPE (rel->r_info);
2165 irel = *rel;
2167 /* Only allow relocations against absolute symbol, which can be
2168 resolved as absolute value + addend. GOTPCREL and GOT32
2169 relocations are allowed since absolute value + addend is
2170 stored in the GOT slot. */
2171 if (bed->target_id == X86_64_ELF_DATA)
2173 r_type &= ~R_X86_64_converted_reloc_bit;
2174 valid_p = (r_type == R_X86_64_64
2175 || r_type == R_X86_64_32
2176 || r_type == R_X86_64_32S
2177 || r_type == R_X86_64_16
2178 || r_type == R_X86_64_8
2179 || r_type == R_X86_64_GOTPCREL
2180 || r_type == R_X86_64_GOTPCRELX
2181 || r_type == R_X86_64_REX_GOTPCRELX);
2182 if (!valid_p)
2184 unsigned int r_symndx = htab->r_sym (rel->r_info);
2185 irel.r_info = htab->r_info (r_symndx, r_type);
2188 else
2189 valid_p = (r_type == R_386_32
2190 || r_type == R_386_16
2191 || r_type == R_386_8
2192 || r_type == R_386_GOT32
2193 || r_type == R_386_GOT32X);
2195 if (valid_p)
2196 *no_dynreloc_p = true;
2197 else
2199 const char *name;
2200 arelent internal_reloc;
2202 if (!bed->elf_info_to_howto (input_section->owner,
2203 &internal_reloc, &irel)
2204 || internal_reloc.howto == NULL)
2205 abort ();
2207 if (h)
2208 name = h->root.root.string;
2209 else
2210 name = bfd_elf_sym_name (input_section->owner, symtab_hdr,
2211 sym, NULL);
2212 info->callbacks->einfo
2213 /* xgettext:c-format */
2214 (_("%F%P: %pB: relocation %s against absolute symbol "
2215 "`%s' in section `%pA' is disallowed\n"),
2216 input_section->owner, internal_reloc.howto->name, name,
2217 input_section);
2218 bfd_set_error (bfd_error_bad_value);
2222 return valid_p;
2225 /* Set the sizes of the dynamic sections. */
2227 bool
2228 _bfd_x86_elf_size_dynamic_sections (bfd *output_bfd,
2229 struct bfd_link_info *info)
2231 struct elf_x86_link_hash_table *htab;
2232 bfd *dynobj;
2233 asection *s;
2234 bool relocs;
2235 bfd *ibfd;
2236 const struct elf_backend_data *bed
2237 = get_elf_backend_data (output_bfd);
2239 htab = elf_x86_hash_table (info, bed->target_id);
2240 if (htab == NULL)
2241 return false;
2242 dynobj = htab->elf.dynobj;
2243 if (dynobj == NULL)
2244 abort ();
2246 /* Set up .got offsets for local syms, and space for local dynamic
2247 relocs. */
2248 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2250 bfd_signed_vma *local_got;
2251 bfd_signed_vma *end_local_got;
2252 char *local_tls_type;
2253 bfd_vma *local_tlsdesc_gotent;
2254 bfd_size_type locsymcount;
2255 Elf_Internal_Shdr *symtab_hdr;
2256 asection *srel;
2258 if (! is_x86_elf (ibfd, htab))
2259 continue;
2261 for (s = ibfd->sections; s != NULL; s = s->next)
2263 struct elf_dyn_relocs *p;
2265 for (p = ((struct elf_dyn_relocs *)
2266 elf_section_data (s)->local_dynrel);
2267 p != NULL;
2268 p = p->next)
2270 if (!bfd_is_abs_section (p->sec)
2271 && bfd_is_abs_section (p->sec->output_section))
2273 /* Input section has been discarded, either because
2274 it is a copy of a linkonce section or due to
2275 linker script /DISCARD/, so we'll be discarding
2276 the relocs too. */
2278 else if (htab->elf.target_os == is_vxworks
2279 && strcmp (p->sec->output_section->name,
2280 ".tls_vars") == 0)
2282 /* Relocations in vxworks .tls_vars sections are
2283 handled specially by the loader. */
2285 else if (p->count != 0)
2287 srel = elf_section_data (p->sec)->sreloc;
2288 srel->size += p->count * htab->sizeof_reloc;
2289 if ((p->sec->output_section->flags & SEC_READONLY) != 0
2290 && (info->flags & DF_TEXTREL) == 0)
2292 info->flags |= DF_TEXTREL;
2293 if (bfd_link_textrel_check (info))
2294 /* xgettext:c-format */
2295 info->callbacks->einfo
2296 (_("%P: %pB: warning: relocation "
2297 "in read-only section `%pA'\n"),
2298 p->sec->owner, p->sec);
2304 local_got = elf_local_got_refcounts (ibfd);
2305 if (!local_got)
2306 continue;
2308 symtab_hdr = &elf_symtab_hdr (ibfd);
2309 locsymcount = symtab_hdr->sh_info;
2310 end_local_got = local_got + locsymcount;
2311 local_tls_type = elf_x86_local_got_tls_type (ibfd);
2312 local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
2313 s = htab->elf.sgot;
2314 srel = htab->elf.srelgot;
2315 for (; local_got < end_local_got;
2316 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2318 *local_tlsdesc_gotent = (bfd_vma) -1;
2319 if (*local_got > 0)
2321 if (GOT_TLS_GDESC_P (*local_tls_type))
2323 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2324 - elf_x86_compute_jump_table_size (htab);
2325 htab->elf.sgotplt->size += 2 * htab->got_entry_size;
2326 *local_got = (bfd_vma) -2;
2328 if (! GOT_TLS_GDESC_P (*local_tls_type)
2329 || GOT_TLS_GD_P (*local_tls_type))
2331 *local_got = s->size;
2332 s->size += htab->got_entry_size;
2333 if (GOT_TLS_GD_P (*local_tls_type)
2334 || *local_tls_type == GOT_TLS_IE_BOTH)
2335 s->size += htab->got_entry_size;
2337 if ((bfd_link_pic (info) && *local_tls_type != GOT_ABS)
2338 || GOT_TLS_GD_ANY_P (*local_tls_type)
2339 || (*local_tls_type & GOT_TLS_IE))
2341 if (*local_tls_type == GOT_TLS_IE_BOTH)
2342 srel->size += 2 * htab->sizeof_reloc;
2343 else if (GOT_TLS_GD_P (*local_tls_type)
2344 || ! GOT_TLS_GDESC_P (*local_tls_type))
2345 srel->size += htab->sizeof_reloc;
2346 if (GOT_TLS_GDESC_P (*local_tls_type))
2348 htab->elf.srelplt->size += htab->sizeof_reloc;
2349 if (bed->target_id == X86_64_ELF_DATA)
2350 htab->elf.tlsdesc_plt = (bfd_vma) -1;
2354 else
2355 *local_got = (bfd_vma) -1;
2359 if (htab->tls_ld_or_ldm_got.refcount > 0)
2361 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2362 or R_X86_64_TLSLD relocs. */
2363 htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
2364 htab->elf.sgot->size += 2 * htab->got_entry_size;
2365 htab->elf.srelgot->size += htab->sizeof_reloc;
2367 else
2368 htab->tls_ld_or_ldm_got.offset = -1;
2370 /* Allocate global sym .plt and .got entries, and space for global
2371 sym dynamic relocs. */
2372 elf_link_hash_traverse (&htab->elf, elf_x86_allocate_dynrelocs,
2373 info);
2375 /* Allocate .plt and .got entries, and space for local symbols. */
2376 htab_traverse (htab->loc_hash_table, elf_x86_allocate_local_dynreloc,
2377 info);
2379 /* For every jump slot reserved in the sgotplt, reloc_count is
2380 incremented. However, when we reserve space for TLS descriptors,
2381 it's not incremented, so in order to compute the space reserved
2382 for them, it suffices to multiply the reloc count by the jump
2383 slot size.
2385 PR ld/13302: We start next_irelative_index at the end of .rela.plt
2386 so that R_{386,X86_64}_IRELATIVE entries come last. */
2387 if (htab->elf.srelplt)
2389 htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
2390 htab->sgotplt_jump_table_size
2391 = elf_x86_compute_jump_table_size (htab);
2392 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
2394 else if (htab->elf.irelplt)
2395 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
2397 if (htab->elf.tlsdesc_plt)
2399 /* NB: tlsdesc_plt is set only for x86-64. If we're not using
2400 lazy TLS relocations, don't generate the PLT and GOT entries
2401 they require. */
2402 if ((info->flags & DF_BIND_NOW))
2403 htab->elf.tlsdesc_plt = 0;
2404 else
2406 htab->elf.tlsdesc_got = htab->elf.sgot->size;
2407 htab->elf.sgot->size += htab->got_entry_size;
2408 /* Reserve room for the initial entry.
2409 FIXME: we could probably do away with it in this case. */
2410 if (htab->elf.splt->size == 0)
2411 htab->elf.splt->size = htab->plt.plt_entry_size;
2412 htab->elf.tlsdesc_plt = htab->elf.splt->size;
2413 htab->elf.splt->size += htab->plt.plt_entry_size;
2417 if (htab->elf.sgotplt)
2419 /* Don't allocate .got.plt section if there are no GOT nor PLT
2420 entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
2421 if ((htab->elf.hgot == NULL
2422 || !htab->got_referenced)
2423 && (htab->elf.sgotplt->size == bed->got_header_size)
2424 && (htab->elf.splt == NULL
2425 || htab->elf.splt->size == 0)
2426 && (htab->elf.sgot == NULL
2427 || htab->elf.sgot->size == 0)
2428 && (htab->elf.iplt == NULL
2429 || htab->elf.iplt->size == 0)
2430 && (htab->elf.igotplt == NULL
2431 || htab->elf.igotplt->size == 0))
2433 htab->elf.sgotplt->size = 0;
2434 /* Solaris requires to keep _GLOBAL_OFFSET_TABLE_ even if it
2435 isn't used. */
2436 if (htab->elf.hgot != NULL
2437 && htab->elf.target_os != is_solaris)
2439 /* Remove the unused _GLOBAL_OFFSET_TABLE_ from symbol
2440 table. */
2441 htab->elf.hgot->root.type = bfd_link_hash_undefined;
2442 htab->elf.hgot->root.u.undef.abfd
2443 = htab->elf.hgot->root.u.def.section->owner;
2444 htab->elf.hgot->root.linker_def = 0;
2445 htab->elf.hgot->ref_regular = 0;
2446 htab->elf.hgot->def_regular = 0;
2451 if (_bfd_elf_eh_frame_present (info))
2453 if (htab->plt_eh_frame != NULL
2454 && htab->elf.splt != NULL
2455 && htab->elf.splt->size != 0
2456 && !bfd_is_abs_section (htab->elf.splt->output_section))
2457 htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
2459 if (htab->plt_got_eh_frame != NULL
2460 && htab->plt_got != NULL
2461 && htab->plt_got->size != 0
2462 && !bfd_is_abs_section (htab->plt_got->output_section))
2463 htab->plt_got_eh_frame->size
2464 = htab->non_lazy_plt->eh_frame_plt_size;
2466 /* Unwind info for the second PLT and .plt.got sections are
2467 identical. */
2468 if (htab->plt_second_eh_frame != NULL
2469 && htab->plt_second != NULL
2470 && htab->plt_second->size != 0
2471 && !bfd_is_abs_section (htab->plt_second->output_section))
2472 htab->plt_second_eh_frame->size
2473 = htab->non_lazy_plt->eh_frame_plt_size;
2476 /* No need to size the .sframe section explicitly because the write-out
2477 mechanism is different. Simply prep up the FDE/FRE for the
2478 .plt section. */
2479 if (_bfd_elf_sframe_present (info))
2481 if (htab->plt_sframe != NULL
2482 && htab->elf.splt != NULL
2483 && htab->elf.splt->size != 0
2484 && !bfd_is_abs_section (htab->elf.splt->output_section))
2486 _bfd_x86_elf_create_sframe_plt (output_bfd, info, SFRAME_PLT);
2487 /* FIXME - Dirty Hack. Set the size to something non-zero for now,
2488 so that the section does not get stripped out below. The precise
2489 size of this section is known only when the contents are
2490 serialized in _bfd_x86_elf_write_sframe_plt. */
2491 htab->plt_sframe->size = sizeof (sframe_header) + 1;
2494 /* FIXME - generate for .got.plt ? */
2496 /* Unwind info for the second PLT. */
2497 if (htab->plt_second_sframe != NULL
2498 && htab->plt_second != NULL
2499 && htab->plt_second->size != 0
2500 && !bfd_is_abs_section (htab->plt_second->output_section))
2502 _bfd_x86_elf_create_sframe_plt (output_bfd, info,
2503 SFRAME_PLT_SEC);
2504 /* FIXME - Dirty Hack. Set the size to something non-zero for now,
2505 so that the section does not get stripped out below. The precise
2506 size of this section is known only when the contents are
2507 serialized in _bfd_x86_elf_write_sframe_plt. */
2508 htab->plt_second_sframe->size = sizeof (sframe_header) + 1;
2512 /* We now have determined the sizes of the various dynamic sections.
2513 Allocate memory for them. */
2514 relocs = false;
2515 for (s = dynobj->sections; s != NULL; s = s->next)
2517 bool strip_section = true;
2519 if ((s->flags & SEC_LINKER_CREATED) == 0)
2520 continue;
2522 /* The .relr.dyn section for compact relative relocation will
2523 be filled later. */
2524 if (s == htab->elf.srelrdyn)
2525 continue;
2527 if (s == htab->elf.splt
2528 || s == htab->elf.sgot)
2530 /* Strip this section if we don't need it; see the
2531 comment below. */
2532 /* We'd like to strip these sections if they aren't needed, but if
2533 we've exported dynamic symbols from them we must leave them.
2534 It's too late to tell BFD to get rid of the symbols. */
2536 if (htab->elf.hplt != NULL)
2537 strip_section = false;
2539 else if (s == htab->elf.sgotplt
2540 || s == htab->elf.iplt
2541 || s == htab->elf.igotplt
2542 || s == htab->plt_second
2543 || s == htab->plt_got
2544 || s == htab->plt_eh_frame
2545 || s == htab->plt_got_eh_frame
2546 || s == htab->plt_second_eh_frame
2547 || s == htab->plt_sframe
2548 || s == htab->plt_second_sframe
2549 || s == htab->elf.sdynbss
2550 || s == htab->elf.sdynrelro)
2552 /* Strip these too. */
2554 else if (htab->is_reloc_section (bfd_section_name (s)))
2556 if (s->size != 0
2557 && s != htab->elf.srelplt
2558 && s != htab->srelplt2)
2559 relocs = true;
2561 /* We use the reloc_count field as a counter if we need
2562 to copy relocs into the output file. */
2563 if (s != htab->elf.srelplt)
2564 s->reloc_count = 0;
2566 else
2568 /* It's not one of our sections, so don't allocate space. */
2569 continue;
2572 if (s->size == 0)
2574 /* If we don't need this section, strip it from the
2575 output file. This is mostly to handle .rel.bss and
2576 .rel.plt. We must create both sections in
2577 create_dynamic_sections, because they must be created
2578 before the linker maps input sections to output
2579 sections. The linker does that before
2580 adjust_dynamic_symbol is called, and it is that
2581 function which decides whether anything needs to go
2582 into these sections. */
2583 if (strip_section)
2584 s->flags |= SEC_EXCLUDE;
2585 continue;
2588 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2589 continue;
2591 /* Skip allocating contents for .sframe section as it is written
2592 out differently. See below. */
2593 if ((s == htab->plt_sframe) || (s == htab->plt_second_sframe))
2594 continue;
2596 /* NB: Initially, the iplt section has minimal alignment to
2597 avoid moving dot of the following section backwards when
2598 it is empty. Update its section alignment now since it
2599 is non-empty. */
2600 if (s == htab->elf.iplt)
2601 bfd_set_section_alignment (s, htab->plt.iplt_alignment);
2603 /* Allocate memory for the section contents. We use bfd_zalloc
2604 here in case unused entries are not reclaimed before the
2605 section's contents are written out. This should not happen,
2606 but this way if it does, we get a R_386_NONE or R_X86_64_NONE
2607 reloc instead of garbage. */
2608 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
2609 if (s->contents == NULL)
2610 return false;
2613 if (htab->plt_eh_frame != NULL
2614 && htab->plt_eh_frame->contents != NULL)
2616 memcpy (htab->plt_eh_frame->contents,
2617 htab->plt.eh_frame_plt,
2618 htab->plt_eh_frame->size);
2619 bfd_put_32 (dynobj, htab->elf.splt->size,
2620 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
2623 if (htab->plt_got_eh_frame != NULL
2624 && htab->plt_got_eh_frame->contents != NULL)
2626 memcpy (htab->plt_got_eh_frame->contents,
2627 htab->non_lazy_plt->eh_frame_plt,
2628 htab->plt_got_eh_frame->size);
2629 bfd_put_32 (dynobj, htab->plt_got->size,
2630 (htab->plt_got_eh_frame->contents
2631 + PLT_FDE_LEN_OFFSET));
2634 if (htab->plt_second_eh_frame != NULL
2635 && htab->plt_second_eh_frame->contents != NULL)
2637 memcpy (htab->plt_second_eh_frame->contents,
2638 htab->non_lazy_plt->eh_frame_plt,
2639 htab->plt_second_eh_frame->size);
2640 bfd_put_32 (dynobj, htab->plt_second->size,
2641 (htab->plt_second_eh_frame->contents
2642 + PLT_FDE_LEN_OFFSET));
2645 if (_bfd_elf_sframe_present (info))
2647 if (htab->plt_sframe != NULL
2648 && htab->elf.splt != NULL
2649 && htab->elf.splt->size != 0
2650 && htab->plt_sframe->contents == NULL)
2651 _bfd_x86_elf_write_sframe_plt (output_bfd, info, SFRAME_PLT);
2653 if (htab->plt_second_sframe != NULL
2654 && htab->elf.splt != NULL
2655 && htab->elf.splt->size != 0
2656 && htab->plt_second_sframe->contents == NULL)
2657 _bfd_x86_elf_write_sframe_plt (output_bfd, info, SFRAME_PLT_SEC);
2660 return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
2661 relocs);
2664 /* Finish up the x86 dynamic sections. */
2666 struct elf_x86_link_hash_table *
2667 _bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd,
2668 struct bfd_link_info *info)
2670 struct elf_x86_link_hash_table *htab;
2671 const struct elf_backend_data *bed;
2672 bfd *dynobj;
2673 asection *sdyn;
2674 bfd_byte *dyncon, *dynconend;
2675 bfd_size_type sizeof_dyn;
2677 bed = get_elf_backend_data (output_bfd);
2678 htab = elf_x86_hash_table (info, bed->target_id);
2679 if (htab == NULL)
2680 return htab;
2682 dynobj = htab->elf.dynobj;
2683 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2685 /* GOT is always created in setup_gnu_properties. But it may not be
2686 needed. .got.plt section may be needed for static IFUNC. */
2687 if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
2689 bfd_vma dynamic_addr;
2691 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
2693 _bfd_error_handler
2694 (_("discarded output section: `%pA'"), htab->elf.sgotplt);
2695 return NULL;
2698 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
2699 = htab->got_entry_size;
2701 dynamic_addr = (sdyn == NULL
2702 ? (bfd_vma) 0
2703 : sdyn->output_section->vma + sdyn->output_offset);
2705 /* Set the first entry in the global offset table to the address
2706 of the dynamic section. Write GOT[1] and GOT[2], needed for
2707 the dynamic linker. */
2708 if (htab->got_entry_size == 8)
2710 bfd_put_64 (output_bfd, dynamic_addr,
2711 htab->elf.sgotplt->contents);
2712 bfd_put_64 (output_bfd, (bfd_vma) 0,
2713 htab->elf.sgotplt->contents + 8);
2714 bfd_put_64 (output_bfd, (bfd_vma) 0,
2715 htab->elf.sgotplt->contents + 8*2);
2717 else
2719 bfd_put_32 (output_bfd, dynamic_addr,
2720 htab->elf.sgotplt->contents);
2721 bfd_put_32 (output_bfd, 0,
2722 htab->elf.sgotplt->contents + 4);
2723 bfd_put_32 (output_bfd, 0,
2724 htab->elf.sgotplt->contents + 4*2);
2728 if (!htab->elf.dynamic_sections_created)
2729 return htab;
2731 if (sdyn == NULL || htab->elf.sgot == NULL)
2732 abort ();
2734 sizeof_dyn = bed->s->sizeof_dyn;
2735 dyncon = sdyn->contents;
2736 dynconend = sdyn->contents + sdyn->size;
2737 for (; dyncon < dynconend; dyncon += sizeof_dyn)
2739 Elf_Internal_Dyn dyn;
2740 asection *s;
2742 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
2744 switch (dyn.d_tag)
2746 default:
2747 if (htab->elf.target_os == is_vxworks
2748 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
2749 break;
2750 continue;
2752 case DT_PLTGOT:
2753 s = htab->elf.sgotplt;
2754 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2755 break;
2757 case DT_JMPREL:
2758 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
2759 break;
2761 case DT_PLTRELSZ:
2762 s = htab->elf.srelplt->output_section;
2763 dyn.d_un.d_val = s->size;
2764 break;
2766 case DT_TLSDESC_PLT:
2767 s = htab->elf.splt;
2768 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
2769 + htab->elf.tlsdesc_plt;
2770 break;
2772 case DT_TLSDESC_GOT:
2773 s = htab->elf.sgot;
2774 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
2775 + htab->elf.tlsdesc_got;
2776 break;
2779 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
2782 if (htab->plt_got != NULL && htab->plt_got->size > 0)
2783 elf_section_data (htab->plt_got->output_section)
2784 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
2786 if (htab->plt_second != NULL && htab->plt_second->size > 0)
2787 elf_section_data (htab->plt_second->output_section)
2788 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
2790 /* Adjust .eh_frame for .plt section. */
2791 if (htab->plt_eh_frame != NULL
2792 && htab->plt_eh_frame->contents != NULL)
2794 if (htab->elf.splt != NULL
2795 && htab->elf.splt->size != 0
2796 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
2797 && htab->elf.splt->output_section != NULL
2798 && htab->plt_eh_frame->output_section != NULL)
2800 bfd_vma plt_start = htab->elf.splt->output_section->vma;
2801 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
2802 + htab->plt_eh_frame->output_offset
2803 + PLT_FDE_START_OFFSET;
2804 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2805 htab->plt_eh_frame->contents
2806 + PLT_FDE_START_OFFSET);
2809 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
2811 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2812 htab->plt_eh_frame,
2813 htab->plt_eh_frame->contents))
2814 return NULL;
2818 /* Adjust .eh_frame for .plt.got section. */
2819 if (htab->plt_got_eh_frame != NULL
2820 && htab->plt_got_eh_frame->contents != NULL)
2822 if (htab->plt_got != NULL
2823 && htab->plt_got->size != 0
2824 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
2825 && htab->plt_got->output_section != NULL
2826 && htab->plt_got_eh_frame->output_section != NULL)
2828 bfd_vma plt_start = htab->plt_got->output_section->vma;
2829 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
2830 + htab->plt_got_eh_frame->output_offset
2831 + PLT_FDE_START_OFFSET;
2832 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2833 htab->plt_got_eh_frame->contents
2834 + PLT_FDE_START_OFFSET);
2836 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
2838 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2839 htab->plt_got_eh_frame,
2840 htab->plt_got_eh_frame->contents))
2841 return NULL;
2845 /* Adjust .eh_frame for the second PLT section. */
2846 if (htab->plt_second_eh_frame != NULL
2847 && htab->plt_second_eh_frame->contents != NULL)
2849 if (htab->plt_second != NULL
2850 && htab->plt_second->size != 0
2851 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
2852 && htab->plt_second->output_section != NULL
2853 && htab->plt_second_eh_frame->output_section != NULL)
2855 bfd_vma plt_start = htab->plt_second->output_section->vma;
2856 bfd_vma eh_frame_start
2857 = (htab->plt_second_eh_frame->output_section->vma
2858 + htab->plt_second_eh_frame->output_offset
2859 + PLT_FDE_START_OFFSET);
2860 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2861 htab->plt_second_eh_frame->contents
2862 + PLT_FDE_START_OFFSET);
2864 if (htab->plt_second_eh_frame->sec_info_type
2865 == SEC_INFO_TYPE_EH_FRAME)
2867 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2868 htab->plt_second_eh_frame,
2869 htab->plt_second_eh_frame->contents))
2870 return NULL;
2874 /* Make any adjustment if necessary and merge .sframe section to
2875 create the final .sframe section for output_bfd. */
2876 if (htab->plt_sframe != NULL
2877 && htab->plt_sframe->contents != NULL)
2879 if (htab->elf.splt != NULL
2880 && htab->elf.splt->size != 0
2881 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
2882 && htab->elf.splt->output_section != NULL
2883 && htab->plt_sframe->output_section != NULL)
2885 bfd_vma plt_start = htab->elf.splt->output_section->vma;
2886 bfd_vma sframe_start = htab->plt_sframe->output_section->vma
2887 + htab->plt_sframe->output_offset
2888 + PLT_SFRAME_FDE_START_OFFSET;
2889 #if 0 /* FIXME Testing only. Remove before review. */
2890 bfd_vma test_value = (plt_start - sframe_start)
2891 + htab->plt_sframe->output_section->vma
2892 + htab->plt_sframe->output_offset
2893 + PLT_SFRAME_FDE_START_OFFSET;
2894 bfd_put_signed_32 (dynobj, test_value,
2895 #endif
2896 bfd_put_signed_32 (dynobj, plt_start - sframe_start,
2897 htab->plt_sframe->contents
2898 + PLT_SFRAME_FDE_START_OFFSET);
2900 if (htab->plt_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
2902 if (! _bfd_elf_merge_section_sframe (output_bfd, info,
2903 htab->plt_sframe,
2904 htab->plt_sframe->contents))
2905 return NULL;
2909 if (htab->plt_second_sframe != NULL
2910 && htab->plt_second_sframe->contents != NULL)
2912 if (htab->plt_second != NULL
2913 && htab->plt_second->size != 0
2914 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
2915 && htab->plt_second->output_section != NULL
2916 && htab->plt_second_sframe->output_section != NULL)
2918 bfd_vma plt_start = htab->plt_second->output_section->vma;
2919 bfd_vma sframe_start
2920 = (htab->plt_second_sframe->output_section->vma
2921 + htab->plt_second_sframe->output_offset
2922 + PLT_SFRAME_FDE_START_OFFSET);
2923 #if 0 /* FIXME Testing only. Remove before review. */
2924 bfd_vma test_value = (plt_start - sframe_start)
2925 + htab->plt_second_sframe->output_section->vma
2926 + htab->plt_second_sframe->output_offset
2927 + PLT_SFRAME_FDE_START_OFFSET;
2928 bfd_put_signed_32 (dynobj, test_value,
2929 #endif
2930 bfd_put_signed_32 (dynobj, plt_start - sframe_start,
2931 htab->plt_second_sframe->contents
2932 + PLT_SFRAME_FDE_START_OFFSET);
2934 if (htab->plt_second_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
2936 if (! _bfd_elf_merge_section_sframe (output_bfd, info,
2937 htab->plt_second_sframe,
2938 htab->plt_second_sframe->contents))
2939 return NULL;
2942 if (htab->elf.sgot && htab->elf.sgot->size > 0)
2943 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
2944 = htab->got_entry_size;
2946 return htab;
2950 bool
2951 _bfd_x86_elf_always_size_sections (bfd *output_bfd,
2952 struct bfd_link_info *info)
2954 asection *tls_sec = elf_hash_table (info)->tls_sec;
2956 if (tls_sec && !bfd_link_relocatable (info))
2958 struct elf_link_hash_entry *tlsbase;
2960 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2961 "_TLS_MODULE_BASE_",
2962 false, false, false);
2964 if (tlsbase && tlsbase->type == STT_TLS)
2966 struct elf_x86_link_hash_table *htab;
2967 struct bfd_link_hash_entry *bh = NULL;
2968 const struct elf_backend_data *bed
2969 = get_elf_backend_data (output_bfd);
2971 htab = elf_x86_hash_table (info, bed->target_id);
2972 if (htab == NULL)
2973 return false;
2975 if (!(_bfd_generic_link_add_one_symbol
2976 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2977 tls_sec, 0, NULL, false,
2978 bed->collect, &bh)))
2979 return false;
2981 htab->tls_module_base = bh;
2983 tlsbase = (struct elf_link_hash_entry *)bh;
2984 tlsbase->def_regular = 1;
2985 tlsbase->other = STV_HIDDEN;
2986 tlsbase->root.linker_def = 1;
2987 (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
2991 return true;
2994 void
2995 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
2996 unsigned int st_other,
2997 bool definition,
2998 bool dynamic ATTRIBUTE_UNUSED)
3000 if (definition)
3002 struct elf_x86_link_hash_entry *eh
3003 = (struct elf_x86_link_hash_entry *) h;
3004 eh->def_protected = ELF_ST_VISIBILITY (st_other) == STV_PROTECTED;
3008 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3010 void
3011 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
3012 struct elf_link_hash_entry *dir,
3013 struct elf_link_hash_entry *ind)
3015 struct elf_x86_link_hash_entry *edir, *eind;
3017 edir = (struct elf_x86_link_hash_entry *) dir;
3018 eind = (struct elf_x86_link_hash_entry *) ind;
3020 if (ind->root.type == bfd_link_hash_indirect
3021 && dir->got.refcount <= 0)
3023 edir->tls_type = eind->tls_type;
3024 eind->tls_type = GOT_UNKNOWN;
3027 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
3028 generate a R_386_COPY reloc. */
3029 edir->gotoff_ref |= eind->gotoff_ref;
3031 edir->zero_undefweak |= eind->zero_undefweak;
3033 if (ELIMINATE_COPY_RELOCS
3034 && ind->root.type != bfd_link_hash_indirect
3035 && dir->dynamic_adjusted)
3037 /* If called to transfer flags for a weakdef during processing
3038 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
3039 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3040 if (dir->versioned != versioned_hidden)
3041 dir->ref_dynamic |= ind->ref_dynamic;
3042 dir->ref_regular |= ind->ref_regular;
3043 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
3044 dir->needs_plt |= ind->needs_plt;
3045 dir->pointer_equality_needed |= ind->pointer_equality_needed;
3047 else
3048 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3051 /* Remove undefined weak symbol from the dynamic symbol table if it
3052 is resolved to 0. */
3054 bool
3055 _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
3056 struct elf_link_hash_entry *h)
3058 if (h->dynindx != -1
3059 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, elf_x86_hash_entry (h)))
3061 h->dynindx = -1;
3062 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3063 h->dynstr_index);
3065 return true;
3068 /* Change the STT_GNU_IFUNC symbol defined in position-dependent
3069 executable into the normal function symbol and set its address
3070 to its PLT entry, which should be resolved by R_*_IRELATIVE at
3071 run-time. */
3073 void
3074 _bfd_x86_elf_link_fixup_ifunc_symbol (struct bfd_link_info *info,
3075 struct elf_x86_link_hash_table *htab,
3076 struct elf_link_hash_entry *h,
3077 Elf_Internal_Sym *sym)
3079 if (bfd_link_pde (info)
3080 && h->def_regular
3081 && h->dynindx != -1
3082 && h->plt.offset != (bfd_vma) -1
3083 && h->type == STT_GNU_IFUNC)
3085 asection *plt_s;
3086 bfd_vma plt_offset;
3087 bfd *output_bfd = info->output_bfd;
3089 if (htab->plt_second)
3091 struct elf_x86_link_hash_entry *eh
3092 = (struct elf_x86_link_hash_entry *) h;
3094 plt_s = htab->plt_second;
3095 plt_offset = eh->plt_second.offset;
3097 else
3099 plt_s = htab->elf.splt;
3100 plt_offset = h->plt.offset;
3103 sym->st_size = 0;
3104 sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
3105 sym->st_shndx
3106 = _bfd_elf_section_from_bfd_section (output_bfd,
3107 plt_s->output_section);
3108 sym->st_value = (plt_s->output_section->vma
3109 + plt_s->output_offset + plt_offset);
3113 /* Report relative relocation. */
3115 void
3116 _bfd_x86_elf_link_report_relative_reloc
3117 (struct bfd_link_info *info, asection *asect,
3118 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym,
3119 const char *reloc_name, const void *reloc)
3121 const char *name;
3122 bfd *abfd;
3123 const Elf_Internal_Rela *rel = (const Elf_Internal_Rela *) reloc;
3125 /* Use the output BFD for linker created sections. */
3126 if ((asect->flags & SEC_LINKER_CREATED) != 0)
3127 abfd = info->output_bfd;
3128 else
3129 abfd = asect->owner;
3131 if (h != NULL && h->root.root.string != NULL)
3132 name = h->root.root.string;
3133 else
3134 name = bfd_elf_sym_name (abfd, &elf_symtab_hdr (abfd), sym, NULL);
3136 if (asect->use_rela_p)
3137 info->callbacks->einfo
3138 (_("%pB: %s (offset: 0x%v, info: 0x%v, addend: 0x%v) against "
3139 "'%s' " "for section '%pA' in %pB\n"),
3140 info->output_bfd, reloc_name, rel->r_offset, rel->r_info,
3141 rel->r_addend, name, asect, abfd);
3142 else
3143 info->callbacks->einfo
3144 (_("%pB: %s (offset: 0x%v, info: 0x%v) against '%s' for section "
3145 "'%pA' in %pB\n"),
3146 info->output_bfd, reloc_name, rel->r_offset, rel->r_info, name,
3147 asect, abfd);
3150 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3152 bool
3153 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
3155 if (h->plt.offset != (bfd_vma) -1
3156 && !h->def_regular
3157 && !h->pointer_equality_needed)
3158 return false;
3160 return _bfd_elf_hash_symbol (h);
3163 /* Adjust a symbol defined by a dynamic object and referenced by a
3164 regular object. The current definition is in some section of the
3165 dynamic object, but we're not including those sections. We have to
3166 change the definition to something the rest of the link can
3167 understand. */
3169 bool
3170 _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3171 struct elf_link_hash_entry *h)
3173 struct elf_x86_link_hash_table *htab;
3174 asection *s, *srel;
3175 struct elf_x86_link_hash_entry *eh;
3176 struct elf_dyn_relocs *p;
3177 const struct elf_backend_data *bed
3178 = get_elf_backend_data (info->output_bfd);
3180 eh = (struct elf_x86_link_hash_entry *) h;
3182 /* Clear GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS if it is turned
3183 on by an input relocatable file and there is a non-GOT/non-PLT
3184 reference from another relocatable file without it.
3185 NB: There can be non-GOT reference in data sections in input with
3186 GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS. */
3187 if (eh->non_got_ref_without_indirect_extern_access
3188 && info->indirect_extern_access == 1
3189 && bfd_link_executable (info))
3191 unsigned int needed_1;
3192 info->indirect_extern_access = 0;
3193 /* Turn off nocopyreloc if implied by indirect_extern_access. */
3194 if (info->nocopyreloc == 2)
3195 info->nocopyreloc = 0;
3196 needed_1 = bfd_h_get_32 (info->output_bfd, info->needed_1_p);
3197 needed_1 &= ~GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
3198 bfd_h_put_32 (info->output_bfd, needed_1, info->needed_1_p);
3201 /* STT_GNU_IFUNC symbol must go through PLT. */
3202 if (h->type == STT_GNU_IFUNC)
3204 /* All local STT_GNU_IFUNC references must be treate as local
3205 calls via local PLT. */
3206 if (h->ref_regular
3207 && SYMBOL_CALLS_LOCAL (info, h))
3209 bfd_size_type pc_count = 0, count = 0;
3210 struct elf_dyn_relocs **pp;
3212 eh = (struct elf_x86_link_hash_entry *) h;
3213 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
3215 pc_count += p->pc_count;
3216 p->count -= p->pc_count;
3217 p->pc_count = 0;
3218 count += p->count;
3219 if (p->count == 0)
3220 *pp = p->next;
3221 else
3222 pp = &p->next;
3225 if (pc_count || count)
3227 h->non_got_ref = 1;
3228 if (pc_count)
3230 /* Increment PLT reference count only for PC-relative
3231 references. */
3232 h->needs_plt = 1;
3233 if (h->plt.refcount <= 0)
3234 h->plt.refcount = 1;
3235 else
3236 h->plt.refcount += 1;
3240 /* GOTOFF relocation needs PLT. */
3241 if (eh->gotoff_ref)
3242 h->plt.refcount = 1;
3245 if (h->plt.refcount <= 0)
3247 h->plt.offset = (bfd_vma) -1;
3248 h->needs_plt = 0;
3250 return true;
3253 /* If this is a function, put it in the procedure linkage table. We
3254 will fill in the contents of the procedure linkage table later,
3255 when we know the address of the .got section. */
3256 if (h->type == STT_FUNC
3257 || h->needs_plt)
3259 if (h->plt.refcount <= 0
3260 || SYMBOL_CALLS_LOCAL (info, h)
3261 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3262 && h->root.type == bfd_link_hash_undefweak))
3264 /* This case can occur if we saw a PLT32 reloc in an input
3265 file, but the symbol was never referred to by a dynamic
3266 object, or if all references were garbage collected. In
3267 such a case, we don't actually need to build a procedure
3268 linkage table, and we can just do a PC32 reloc instead. */
3269 h->plt.offset = (bfd_vma) -1;
3270 h->needs_plt = 0;
3273 return true;
3275 else
3276 /* It's possible that we incorrectly decided a .plt reloc was needed
3277 * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
3278 check_relocs. We can't decide accurately between function and
3279 non-function syms in check-relocs; Objects loaded later in
3280 the link may change h->type. So fix it now. */
3281 h->plt.offset = (bfd_vma) -1;
3283 /* If this is a weak symbol, and there is a real definition, the
3284 processor independent code will have arranged for us to see the
3285 real definition first, and we can just use the same value. */
3286 if (h->is_weakalias)
3288 struct elf_link_hash_entry *def = weakdef (h);
3289 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3290 h->root.u.def.section = def->root.u.def.section;
3291 h->root.u.def.value = def->root.u.def.value;
3292 if (ELIMINATE_COPY_RELOCS
3293 || info->nocopyreloc
3294 || SYMBOL_NO_COPYRELOC (info, eh))
3296 /* NB: needs_copy is always 0 for i386. */
3297 h->non_got_ref = def->non_got_ref;
3298 eh->needs_copy = def->needs_copy;
3300 return true;
3303 /* This is a reference to a symbol defined by a dynamic object which
3304 is not a function. */
3306 /* If we are creating a shared library, we must presume that the
3307 only references to the symbol are via the global offset table.
3308 For such cases we need not do anything here; the relocations will
3309 be handled correctly by relocate_section. */
3310 if (!bfd_link_executable (info))
3311 return true;
3313 /* If there are no references to this symbol that do not use the
3314 GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
3315 reloc. NB: gotoff_ref is always 0 for x86-64. */
3316 if (!h->non_got_ref && !eh->gotoff_ref)
3317 return true;
3319 /* If -z nocopyreloc was given, we won't generate them either. */
3320 if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
3322 h->non_got_ref = 0;
3323 return true;
3326 htab = elf_x86_hash_table (info, bed->target_id);
3327 if (htab == NULL)
3328 return false;
3330 /* If there aren't any dynamic relocs in read-only sections nor
3331 R_386_GOTOFF relocation, then we can keep the dynamic relocs and
3332 avoid the copy reloc. This doesn't work on VxWorks, where we can
3333 not have dynamic relocations (other than copy and jump slot
3334 relocations) in an executable. */
3335 if (ELIMINATE_COPY_RELOCS
3336 && (bed->target_id == X86_64_ELF_DATA
3337 || (!eh->gotoff_ref
3338 && htab->elf.target_os != is_vxworks)))
3340 /* If we don't find any dynamic relocs in read-only sections,
3341 then we'll be keeping the dynamic relocs and avoiding the copy
3342 reloc. */
3343 if (!_bfd_elf_readonly_dynrelocs (h))
3345 h->non_got_ref = 0;
3346 return true;
3350 /* We must allocate the symbol in our .dynbss section, which will
3351 become part of the .bss section of the executable. There will be
3352 an entry for this symbol in the .dynsym section. The dynamic
3353 object will contain position independent code, so all references
3354 from the dynamic object to this symbol will go through the global
3355 offset table. The dynamic linker will use the .dynsym entry to
3356 determine the address it must put in the global offset table, so
3357 both the dynamic object and the regular object will refer to the
3358 same memory location for the variable. */
3360 /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
3361 dynamic linker to copy the initial value out of the dynamic object
3362 and into the runtime process image. */
3363 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
3365 s = htab->elf.sdynrelro;
3366 srel = htab->elf.sreldynrelro;
3368 else
3370 s = htab->elf.sdynbss;
3371 srel = htab->elf.srelbss;
3373 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3375 if (eh->def_protected && bfd_link_executable (info))
3376 for (p = h->dyn_relocs; p != NULL; p = p->next)
3378 /* Disallow copy relocation against non-copyable protected
3379 symbol. */
3380 s = p->sec->output_section;
3381 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3383 info->callbacks->einfo
3384 /* xgettext:c-format */
3385 (_("%F%P: %pB: copy relocation against non-copyable "
3386 "protected symbol `%s' in %pB\n"),
3387 p->sec->owner, h->root.root.string,
3388 h->root.u.def.section->owner);
3389 return false;
3393 srel->size += htab->sizeof_reloc;
3394 h->needs_copy = 1;
3397 return _bfd_elf_adjust_dynamic_copy (info, h, s);
3400 void
3401 _bfd_x86_elf_hide_symbol (struct bfd_link_info *info,
3402 struct elf_link_hash_entry *h,
3403 bool force_local)
3405 if (h->root.type == bfd_link_hash_undefweak
3406 && info->nointerp
3407 && bfd_link_pie (info))
3409 /* When there is no dynamic interpreter in PIE, make the undefined
3410 weak symbol dynamic so that PC relative branch to the undefined
3411 weak symbol will land to address 0. */
3412 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
3413 if (h->plt.refcount > 0
3414 || eh->plt_got.refcount > 0)
3415 return;
3418 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3421 /* Return TRUE if a symbol is referenced locally. It is similar to
3422 SYMBOL_REFERENCES_LOCAL, but it also checks version script. It
3423 works in check_relocs. */
3425 bool
3426 _bfd_x86_elf_link_symbol_references_local (struct bfd_link_info *info,
3427 struct elf_link_hash_entry *h)
3429 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
3430 struct elf_x86_link_hash_table *htab
3431 = (struct elf_x86_link_hash_table *) info->hash;
3433 if (eh->local_ref > 1)
3434 return true;
3436 if (eh->local_ref == 1)
3437 return false;
3439 /* Unversioned symbols defined in regular objects can be forced local
3440 by linker version script. A weak undefined symbol is forced local
3442 1. It has non-default visibility. Or
3443 2. When building executable, there is no dynamic linker. Or
3444 3. or "-z nodynamic-undefined-weak" is used.
3446 if (_bfd_elf_symbol_refs_local_p (h, info, 1)
3447 || (h->root.type == bfd_link_hash_undefweak
3448 && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3449 || (bfd_link_executable (info)
3450 && htab->interp == NULL)
3451 || info->dynamic_undefined_weak == 0))
3452 || ((h->def_regular || ELF_COMMON_DEF_P (h))
3453 && info->version_info != NULL
3454 && _bfd_elf_link_hide_sym_by_version (info, h)))
3456 eh->local_ref = 2;
3457 return true;
3460 eh->local_ref = 1;
3461 return false;
3464 /* Return the section that should be marked against GC for a given
3465 relocation. */
3467 asection *
3468 _bfd_x86_elf_gc_mark_hook (asection *sec,
3469 struct bfd_link_info *info,
3470 Elf_Internal_Rela *rel,
3471 struct elf_link_hash_entry *h,
3472 Elf_Internal_Sym *sym)
3474 /* Compiler should optimize this out. */
3475 if (((unsigned int) R_X86_64_GNU_VTINHERIT
3476 != (unsigned int) R_386_GNU_VTINHERIT)
3477 || ((unsigned int) R_X86_64_GNU_VTENTRY
3478 != (unsigned int) R_386_GNU_VTENTRY))
3479 abort ();
3481 if (h != NULL)
3482 switch (ELF32_R_TYPE (rel->r_info))
3484 case R_X86_64_GNU_VTINHERIT:
3485 case R_X86_64_GNU_VTENTRY:
3486 return NULL;
3489 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
3492 static bfd_vma
3493 elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
3494 bfd_vma off,
3495 bfd_vma offset ATTRIBUTE_UNUSED,
3496 bfd_vma got_addr)
3498 return got_addr + off;
3501 static bfd_vma
3502 elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
3503 bfd_vma off,
3504 bfd_vma offset,
3505 bfd_vma got_addr ATTRIBUTE_UNUSED)
3507 return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
3510 static bool
3511 elf_i386_valid_plt_reloc_p (unsigned int type)
3513 return (type == R_386_JUMP_SLOT
3514 || type == R_386_GLOB_DAT
3515 || type == R_386_IRELATIVE);
3518 static bool
3519 elf_x86_64_valid_plt_reloc_p (unsigned int type)
3521 return (type == R_X86_64_JUMP_SLOT
3522 || type == R_X86_64_GLOB_DAT
3523 || type == R_X86_64_IRELATIVE);
3526 long
3527 _bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
3528 long count,
3529 long relsize,
3530 bfd_vma got_addr,
3531 struct elf_x86_plt plts[],
3532 asymbol **dynsyms,
3533 asymbol **ret)
3535 long size, i, n, len;
3536 int j;
3537 unsigned int plt_got_offset, plt_entry_size;
3538 asymbol *s;
3539 bfd_byte *plt_contents;
3540 long dynrelcount;
3541 arelent **dynrelbuf, *p;
3542 char *names;
3543 const struct elf_backend_data *bed;
3544 bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
3545 bfd_vma);
3546 bool (*valid_plt_reloc_p) (unsigned int);
3548 dynrelbuf = NULL;
3549 if (count == 0)
3550 goto bad_return;
3552 dynrelbuf = (arelent **) bfd_malloc (relsize);
3553 if (dynrelbuf == NULL)
3554 goto bad_return;
3556 dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
3557 dynsyms);
3558 if (dynrelcount <= 0)
3559 goto bad_return;
3561 /* Sort the relocs by address. */
3562 qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
3563 _bfd_x86_elf_compare_relocs);
3565 size = count * sizeof (asymbol);
3567 /* Allocate space for @plt suffixes. */
3568 n = 0;
3569 for (i = 0; i < dynrelcount; i++)
3571 p = dynrelbuf[i];
3572 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3573 if (p->addend != 0)
3574 size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
3577 s = *ret = (asymbol *) bfd_zmalloc (size);
3578 if (s == NULL)
3579 goto bad_return;
3581 bed = get_elf_backend_data (abfd);
3583 if (bed->target_id == X86_64_ELF_DATA)
3585 get_plt_got_vma = elf_x86_64_get_plt_got_vma;
3586 valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
3588 else
3590 get_plt_got_vma = elf_i386_get_plt_got_vma;
3591 valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
3592 if (got_addr)
3594 /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
3595 address. */
3596 asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
3597 if (sec != NULL)
3598 got_addr = sec->vma;
3599 else
3601 sec = bfd_get_section_by_name (abfd, ".got");
3602 if (sec != NULL)
3603 got_addr = sec->vma;
3606 if (got_addr == (bfd_vma) -1)
3607 goto bad_return;
3611 /* Check for each PLT section. */
3612 names = (char *) (s + count);
3613 size = 0;
3614 n = 0;
3615 for (j = 0; plts[j].name != NULL; j++)
3616 if ((plt_contents = plts[j].contents) != NULL)
3618 long k;
3619 bfd_vma offset;
3620 asection *plt;
3621 struct elf_x86_plt *plt_p = &plts[j];
3623 plt_got_offset = plt_p->plt_got_offset;
3624 plt_entry_size = plt_p->plt_entry_size;
3626 plt = plt_p->sec;
3628 if ((plt_p->type & plt_lazy))
3630 /* Skip PLT0 in lazy PLT. */
3631 k = 1;
3632 offset = plt_entry_size;
3634 else
3636 k = 0;
3637 offset = 0;
3640 /* Check each PLT entry against dynamic relocations. */
3641 for (; k < plt_p->count; k++)
3643 int off;
3644 bfd_vma got_vma;
3645 long min, max, mid;
3647 /* Get the GOT offset for i386 or the PC-relative offset
3648 for x86-64, a signed 32-bit integer. */
3649 off = H_GET_32 (abfd, (plt_contents + offset
3650 + plt_got_offset));
3651 got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
3653 /* Binary search. */
3654 p = dynrelbuf[0];
3655 min = 0;
3656 max = dynrelcount;
3657 while ((min + 1) < max)
3659 arelent *r;
3661 mid = (min + max) / 2;
3662 r = dynrelbuf[mid];
3663 if (got_vma > r->address)
3664 min = mid;
3665 else if (got_vma < r->address)
3666 max = mid;
3667 else
3669 p = r;
3670 break;
3674 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
3675 if (got_vma == p->address
3676 && p->howto != NULL
3677 && valid_plt_reloc_p (p->howto->type))
3679 *s = **p->sym_ptr_ptr;
3680 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
3681 set. Since we are defining a symbol, ensure one
3682 of them is set. */
3683 if ((s->flags & BSF_LOCAL) == 0)
3684 s->flags |= BSF_GLOBAL;
3685 s->flags |= BSF_SYNTHETIC;
3686 /* This is no longer a section symbol. */
3687 s->flags &= ~BSF_SECTION_SYM;
3688 s->section = plt;
3689 s->the_bfd = plt->owner;
3690 s->value = offset;
3691 s->udata.p = NULL;
3692 s->name = names;
3693 len = strlen ((*p->sym_ptr_ptr)->name);
3694 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3695 names += len;
3696 if (p->addend != 0)
3698 char buf[30], *a;
3700 memcpy (names, "+0x", sizeof ("+0x") - 1);
3701 names += sizeof ("+0x") - 1;
3702 bfd_sprintf_vma (abfd, buf, p->addend);
3703 for (a = buf; *a == '0'; ++a)
3705 size = strlen (a);
3706 memcpy (names, a, size);
3707 names += size;
3709 memcpy (names, "@plt", sizeof ("@plt"));
3710 names += sizeof ("@plt");
3711 n++;
3712 s++;
3713 /* There should be only one entry in PLT for a given
3714 symbol. Set howto to NULL after processing a PLT
3715 entry to guard against corrupted PLT. */
3716 p->howto = NULL;
3718 offset += plt_entry_size;
3722 /* PLT entries with R_386_TLS_DESC relocations are skipped. */
3723 if (n == 0)
3725 bad_return:
3726 count = -1;
3728 else
3729 count = n;
3731 for (j = 0; plts[j].name != NULL; j++)
3732 free (plts[j].contents);
3734 free (dynrelbuf);
3736 return count;
3739 /* Parse x86 GNU properties. */
3741 enum elf_property_kind
3742 _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
3743 bfd_byte *ptr, unsigned int datasz)
3745 elf_property *prop;
3747 if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3748 || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3749 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
3750 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3751 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
3752 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
3753 || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3754 && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3756 if (datasz != 4)
3758 _bfd_error_handler
3759 (_("error: %pB: <corrupt x86 property (0x%x) size: 0x%x>"),
3760 abfd, type, datasz);
3761 return property_corrupt;
3763 prop = _bfd_elf_get_property (abfd, type, datasz);
3764 prop->u.number |= bfd_h_get_32 (abfd, ptr);
3765 prop->pr_kind = property_number;
3766 return property_number;
3769 return property_ignored;
3772 /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
3773 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
3774 should be merged with ABFD. */
3776 bool
3777 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
3778 bfd *abfd ATTRIBUTE_UNUSED,
3779 bfd *bbfd ATTRIBUTE_UNUSED,
3780 elf_property *aprop,
3781 elf_property *bprop)
3783 unsigned int number, features;
3784 bool updated = false;
3785 const struct elf_backend_data *bed;
3786 struct elf_x86_link_hash_table *htab;
3787 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
3789 if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3790 || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3791 && pr_type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3793 if (aprop == NULL || bprop == NULL)
3795 /* Only one of APROP and BPROP can be NULL. */
3796 if (aprop != NULL)
3798 /* Remove this property since the other input file doesn't
3799 have it. */
3800 aprop->pr_kind = property_remove;
3801 updated = true;
3804 else
3806 number = aprop->u.number;
3807 aprop->u.number = number | bprop->u.number;
3808 updated = number != (unsigned int) aprop->u.number;
3810 return updated;
3812 else if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3813 || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_LO
3814 && pr_type <= GNU_PROPERTY_X86_UINT32_OR_HI))
3816 features = 0;
3817 if (pr_type == GNU_PROPERTY_X86_ISA_1_NEEDED)
3819 bed = get_elf_backend_data (info->output_bfd);
3820 htab = elf_x86_hash_table (info, bed->target_id);
3821 switch (htab->params->isa_level)
3823 case 0:
3824 break;
3825 case 2:
3826 features = GNU_PROPERTY_X86_ISA_1_V2;
3827 break;
3828 case 3:
3829 features = GNU_PROPERTY_X86_ISA_1_V3;
3830 break;
3831 case 4:
3832 features = GNU_PROPERTY_X86_ISA_1_V4;
3833 break;
3834 default:
3835 abort ();
3838 if (aprop != NULL && bprop != NULL)
3840 number = aprop->u.number;
3841 aprop->u.number = number | bprop->u.number | features;
3842 /* Remove the property if all bits are empty. */
3843 if (aprop->u.number == 0)
3845 aprop->pr_kind = property_remove;
3846 updated = true;
3848 else
3849 updated = number != (unsigned int) aprop->u.number;
3851 else
3853 /* Only one of APROP and BPROP can be NULL. */
3854 if (aprop != NULL)
3856 aprop->u.number |= features;
3857 if (aprop->u.number == 0)
3859 /* Remove APROP if all bits are empty. */
3860 aprop->pr_kind = property_remove;
3861 updated = true;
3864 else
3866 /* Return TRUE if APROP is NULL and all bits of BPROP
3867 aren't empty to indicate that BPROP should be added
3868 to ABFD. */
3869 bprop->u.number |= features;
3870 updated = bprop->u.number != 0;
3873 return updated;
3875 else if (pr_type >= GNU_PROPERTY_X86_UINT32_AND_LO
3876 && pr_type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3878 /* Only one of APROP and BPROP can be NULL:
3879 1. APROP & BPROP when both APROP and BPROP aren't NULL.
3880 2. If APROP is NULL, remove x86 feature.
3881 3. Otherwise, do nothing.
3883 bed = get_elf_backend_data (info->output_bfd);
3884 htab = elf_x86_hash_table (info, bed->target_id);
3885 if (!htab)
3886 abort ();
3887 if (aprop != NULL && bprop != NULL)
3889 number = aprop->u.number;
3890 aprop->u.number = number & bprop->u.number;
3891 if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
3893 features = 0;
3894 if (htab->params->ibt)
3895 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
3896 if (htab->params->shstk)
3897 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
3898 if (htab->params->lam_u48)
3899 features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
3900 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
3901 else if (htab->params->lam_u57)
3902 features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
3903 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT,
3904 GNU_PROPERTY_X86_FEATURE_1_SHSTK,
3905 GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
3906 GNU_PROPERTY_X86_FEATURE_1_LAM_U57. */
3907 aprop->u.number |= features;
3909 updated = number != (unsigned int) aprop->u.number;
3910 /* Remove the property if all feature bits are cleared. */
3911 if (aprop->u.number == 0)
3912 aprop->pr_kind = property_remove;
3914 else
3916 /* There should be no AND properties since some input doesn't
3917 have them. Set IBT and SHSTK properties for -z ibt and -z
3918 shstk if needed. */
3919 features = 0;
3920 if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
3922 if (htab->params->ibt)
3923 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
3924 if (htab->params->shstk)
3925 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
3926 if (htab->params->lam_u48)
3927 features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
3928 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
3929 else if (htab->params->lam_u57)
3930 features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
3932 if (features)
3934 if (aprop != NULL)
3936 updated = features != (unsigned int) aprop->u.number;
3937 aprop->u.number = features;
3939 else
3941 updated = true;
3942 bprop->u.number = features;
3945 else if (aprop != NULL)
3947 aprop->pr_kind = property_remove;
3948 updated = true;
3951 return updated;
3953 else
3955 /* Never should happen. */
3956 abort ();
3959 return updated;
3962 /* Set up x86 GNU properties. Return the first relocatable ELF input
3963 with GNU properties if found. Otherwise, return NULL. */
3965 bfd *
3966 _bfd_x86_elf_link_setup_gnu_properties
3967 (struct bfd_link_info *info, struct elf_x86_init_table *init_table)
3969 bool normal_target;
3970 bool lazy_plt;
3971 asection *sec, *pltsec;
3972 bfd *dynobj;
3973 bool use_ibt_plt;
3974 unsigned int plt_alignment, features, isa_level;
3975 struct elf_x86_link_hash_table *htab;
3976 bfd *pbfd;
3977 bfd *ebfd = NULL;
3978 elf_property *prop;
3979 const struct elf_backend_data *bed;
3980 unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
3981 unsigned int got_align;
3983 /* Find a normal input file with GNU property note. */
3984 for (pbfd = info->input_bfds;
3985 pbfd != NULL;
3986 pbfd = pbfd->link.next)
3987 if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
3988 && bfd_count_sections (pbfd) != 0)
3990 ebfd = pbfd;
3992 if (elf_properties (pbfd) != NULL)
3993 break;
3996 bed = get_elf_backend_data (info->output_bfd);
3998 htab = elf_x86_hash_table (info, bed->target_id);
3999 if (htab == NULL)
4000 return pbfd;
4002 features = 0;
4003 if (htab->params->ibt)
4005 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
4006 htab->params->cet_report &= ~prop_report_ibt;
4008 if (htab->params->shstk)
4010 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
4011 htab->params->cet_report &= ~prop_report_shstk;
4013 if (!(htab->params->cet_report & (prop_report_ibt | prop_report_shstk)))
4014 htab->params->cet_report = prop_report_none;
4015 if (htab->params->lam_u48)
4017 features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4018 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4019 htab->params->lam_u48_report = prop_report_none;
4020 htab->params->lam_u57_report = prop_report_none;
4022 else if (htab->params->lam_u57)
4024 features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
4025 htab->params->lam_u57_report = prop_report_none;
4028 switch (htab->params->isa_level)
4030 case 0:
4031 isa_level = 0;
4032 break;
4033 case 1:
4034 isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE;
4035 break;
4036 case 2:
4037 isa_level = GNU_PROPERTY_X86_ISA_1_V2;
4038 break;
4039 case 3:
4040 isa_level = GNU_PROPERTY_X86_ISA_1_V3;
4041 break;
4042 case 4:
4043 isa_level = GNU_PROPERTY_X86_ISA_1_V4;
4044 break;
4045 default:
4046 abort ();
4049 if (ebfd != NULL)
4051 prop = NULL;
4052 if (features)
4054 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT,
4055 GNU_PROPERTY_X86_FEATURE_1_SHSTK,
4056 GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
4057 GNU_PROPERTY_X86_FEATURE_1_LAM_U57. */
4058 prop = _bfd_elf_get_property (ebfd,
4059 GNU_PROPERTY_X86_FEATURE_1_AND,
4061 prop->u.number |= features;
4062 prop->pr_kind = property_number;
4065 if (isa_level)
4067 /* If ISA level is set, add GNU_PROPERTY_X86_ISA_1_NEEDED. */
4068 prop = _bfd_elf_get_property (ebfd,
4069 GNU_PROPERTY_X86_ISA_1_NEEDED,
4071 prop->u.number |= isa_level;
4072 prop->pr_kind = property_number;
4075 /* Create the GNU property note section if needed. */
4076 if (prop != NULL && pbfd == NULL)
4078 sec = bfd_make_section_with_flags (ebfd,
4079 NOTE_GNU_PROPERTY_SECTION_NAME,
4080 (SEC_ALLOC
4081 | SEC_LOAD
4082 | SEC_IN_MEMORY
4083 | SEC_READONLY
4084 | SEC_HAS_CONTENTS
4085 | SEC_DATA));
4086 if (sec == NULL)
4087 info->callbacks->einfo (_("%F%P: failed to create GNU property section\n"));
4089 if (!bfd_set_section_alignment (sec, class_align))
4091 error_alignment:
4092 info->callbacks->einfo (_("%F%pA: failed to align section\n"),
4093 sec);
4096 elf_section_type (sec) = SHT_NOTE;
4100 if (htab->params->cet_report
4101 || htab->params->lam_u48_report
4102 || htab->params->lam_u57_report)
4104 /* Report missing IBT, SHSTK and LAM properties. */
4105 bfd *abfd;
4106 const char *warning_msg = _("%P: %pB: warning: missing %s\n");
4107 const char *error_msg = _("%X%P: %pB: error: missing %s\n");
4108 const char *cet_msg = NULL;
4109 const char *lam_u48_msg = NULL;
4110 const char *lam_u57_msg = NULL;
4111 const char *missing;
4112 elf_property_list *p;
4113 bool missing_ibt, missing_shstk;
4114 bool missing_lam_u48, missing_lam_u57;
4115 bool check_ibt
4116 = (htab->params->cet_report
4117 && (htab->params->cet_report & prop_report_ibt));
4118 bool check_shstk
4119 = (htab->params->cet_report
4120 && (htab->params->cet_report & prop_report_shstk));
4122 if (htab->params->cet_report)
4124 if ((htab->params->cet_report & prop_report_warning))
4125 cet_msg = warning_msg;
4126 else
4127 cet_msg = error_msg;
4129 if (htab->params->lam_u48_report)
4131 if ((htab->params->lam_u48_report & prop_report_warning))
4132 lam_u48_msg = warning_msg;
4133 else
4134 lam_u48_msg = error_msg;
4136 if (htab->params->lam_u57_report)
4138 if ((htab->params->lam_u57_report & prop_report_warning))
4139 lam_u57_msg = warning_msg;
4140 else
4141 lam_u57_msg = error_msg;
4144 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
4145 if (!(abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED))
4146 && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
4148 for (p = elf_properties (abfd); p; p = p->next)
4149 if (p->property.pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
4150 break;
4152 missing_ibt = check_ibt;
4153 missing_shstk = check_shstk;
4154 missing_lam_u48 = !!lam_u48_msg;
4155 missing_lam_u57 = !!lam_u57_msg;
4156 if (p)
4158 missing_ibt &= !(p->property.u.number
4159 & GNU_PROPERTY_X86_FEATURE_1_IBT);
4160 missing_shstk &= !(p->property.u.number
4161 & GNU_PROPERTY_X86_FEATURE_1_SHSTK);
4162 missing_lam_u48 &= !(p->property.u.number
4163 & GNU_PROPERTY_X86_FEATURE_1_LAM_U48);
4164 missing_lam_u57 &= !(p->property.u.number
4165 & GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4167 if (missing_ibt || missing_shstk)
4169 if (missing_ibt && missing_shstk)
4170 missing = _("IBT and SHSTK properties");
4171 else if (missing_ibt)
4172 missing = _("IBT property");
4173 else
4174 missing = _("SHSTK property");
4175 info->callbacks->einfo (cet_msg, abfd, missing);
4177 if (missing_lam_u48)
4179 missing = _("LAM_U48 property");
4180 info->callbacks->einfo (lam_u48_msg, abfd, missing);
4182 if (missing_lam_u57)
4184 missing = _("LAM_U57 property");
4185 info->callbacks->einfo (lam_u57_msg, abfd, missing);
4190 pbfd = _bfd_elf_link_setup_gnu_properties (info);
4192 htab->r_info = init_table->r_info;
4193 htab->r_sym = init_table->r_sym;
4195 if (bfd_link_relocatable (info))
4196 return pbfd;
4198 htab->plt0_pad_byte = init_table->plt0_pad_byte;
4200 use_ibt_plt = htab->params->ibtplt || htab->params->ibt;
4201 if (!use_ibt_plt && pbfd != NULL)
4203 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
4204 elf_property_list *p;
4206 /* The property list is sorted in order of type. */
4207 for (p = elf_properties (pbfd); p; p = p->next)
4209 if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
4211 use_ibt_plt = !!(p->property.u.number
4212 & GNU_PROPERTY_X86_FEATURE_1_IBT);
4213 break;
4215 else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
4216 break;
4220 dynobj = htab->elf.dynobj;
4222 /* Set htab->elf.dynobj here so that there is no need to check and
4223 set it in check_relocs. */
4224 if (dynobj == NULL)
4226 if (pbfd != NULL)
4228 htab->elf.dynobj = pbfd;
4229 dynobj = pbfd;
4231 else
4233 bfd *abfd;
4235 /* Find a normal input file to hold linker created
4236 sections. */
4237 for (abfd = info->input_bfds;
4238 abfd != NULL;
4239 abfd = abfd->link.next)
4240 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
4241 && (abfd->flags
4242 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
4243 && bed->relocs_compatible (abfd->xvec,
4244 info->output_bfd->xvec))
4246 htab->elf.dynobj = abfd;
4247 dynobj = abfd;
4248 break;
4253 /* Return if there are no normal input files. */
4254 if (dynobj == NULL)
4255 return pbfd;
4257 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
4258 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
4259 canonical function address. */
4260 htab->plt.has_plt0 = 1;
4261 normal_target = htab->elf.target_os == is_normal;
4263 if (normal_target)
4265 if (use_ibt_plt)
4267 htab->lazy_plt = init_table->lazy_ibt_plt;
4268 htab->non_lazy_plt = init_table->non_lazy_ibt_plt;
4270 else
4272 htab->lazy_plt = init_table->lazy_plt;
4273 htab->non_lazy_plt = init_table->non_lazy_plt;
4276 else
4278 htab->lazy_plt = init_table->lazy_plt;
4279 htab->non_lazy_plt = NULL;
4282 pltsec = htab->elf.splt;
4284 if (htab->non_lazy_plt != NULL
4285 && (!htab->plt.has_plt0 || pltsec == NULL))
4286 lazy_plt = false;
4287 else
4288 lazy_plt = true;
4290 if (normal_target)
4292 if (use_ibt_plt)
4294 if (lazy_plt)
4295 htab->sframe_plt = init_table->sframe_lazy_ibt_plt;
4296 else
4297 htab->sframe_plt = init_table->sframe_non_lazy_ibt_plt;
4299 else
4301 if (lazy_plt)
4302 htab->sframe_plt = init_table->sframe_lazy_plt;
4303 else
4304 htab->sframe_plt = init_table->sframe_non_lazy_plt;
4307 else
4308 htab->sframe_plt = NULL;
4310 /* If the non-lazy PLT is available, use it for all PLT entries if
4311 there are no PLT0 or no .plt section. */
4312 if (!lazy_plt)
4314 if (bfd_link_pic (info))
4315 htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
4316 else
4317 htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
4318 htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
4319 htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
4320 htab->plt.plt_got_insn_size
4321 = htab->non_lazy_plt->plt_got_insn_size;
4322 htab->plt.eh_frame_plt_size
4323 = htab->non_lazy_plt->eh_frame_plt_size;
4324 htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
4326 else
4328 if (bfd_link_pic (info))
4330 htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
4331 htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
4333 else
4335 htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
4336 htab->plt.plt_entry = htab->lazy_plt->plt_entry;
4338 htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
4339 htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
4340 htab->plt.plt_got_insn_size
4341 = htab->lazy_plt->plt_got_insn_size;
4342 htab->plt.eh_frame_plt_size
4343 = htab->lazy_plt->eh_frame_plt_size;
4344 htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
4347 if (htab->elf.target_os == is_vxworks
4348 && !elf_vxworks_create_dynamic_sections (dynobj, info,
4349 &htab->srelplt2))
4351 info->callbacks->einfo (_("%F%P: failed to create VxWorks dynamic sections\n"));
4352 return pbfd;
4355 /* Since create_dynamic_sections isn't always called, but GOT
4356 relocations need GOT relocations, create them here so that we
4357 don't need to do it in check_relocs. */
4358 if (htab->elf.sgot == NULL
4359 && !_bfd_elf_create_got_section (dynobj, info))
4360 info->callbacks->einfo (_("%F%P: failed to create GOT sections\n"));
4362 got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
4364 /* Align .got and .got.plt sections to their entry size. Do it here
4365 instead of in create_dynamic_sections so that they are always
4366 properly aligned even if create_dynamic_sections isn't called. */
4367 sec = htab->elf.sgot;
4368 if (!bfd_set_section_alignment (sec, got_align))
4369 goto error_alignment;
4371 sec = htab->elf.sgotplt;
4372 if (!bfd_set_section_alignment (sec, got_align))
4373 goto error_alignment;
4375 /* Create the ifunc sections here so that check_relocs can be
4376 simplified. */
4377 if (!_bfd_elf_create_ifunc_sections (dynobj, info))
4378 info->callbacks->einfo (_("%F%P: failed to create ifunc sections\n"));
4380 plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
4382 if (pltsec != NULL)
4384 /* Whe creating executable, set the contents of the .interp
4385 section to the interpreter. */
4386 if (bfd_link_executable (info) && !info->nointerp)
4388 asection *s = bfd_get_linker_section (dynobj, ".interp");
4389 if (s == NULL)
4390 abort ();
4391 s->size = htab->dynamic_interpreter_size;
4392 s->contents = (unsigned char *) htab->dynamic_interpreter;
4393 htab->interp = s;
4396 if (normal_target)
4398 flagword pltflags = (bed->dynamic_sec_flags
4399 | SEC_ALLOC
4400 | SEC_CODE
4401 | SEC_LOAD
4402 | SEC_READONLY);
4403 unsigned int non_lazy_plt_alignment
4404 = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
4406 sec = pltsec;
4407 if (!bfd_set_section_alignment (sec, plt_alignment))
4408 goto error_alignment;
4410 /* Create the GOT procedure linkage table. */
4411 sec = bfd_make_section_anyway_with_flags (dynobj,
4412 ".plt.got",
4413 pltflags);
4414 if (sec == NULL)
4415 info->callbacks->einfo (_("%F%P: failed to create GOT PLT section\n"));
4417 if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
4418 goto error_alignment;
4420 htab->plt_got = sec;
4422 if (lazy_plt)
4424 sec = NULL;
4426 if (use_ibt_plt)
4428 /* Create the second PLT for Intel IBT support. IBT
4429 PLT is needed only for lazy binding. */
4430 sec = bfd_make_section_anyway_with_flags (dynobj,
4431 ".plt.sec",
4432 pltflags);
4433 if (sec == NULL)
4434 info->callbacks->einfo (_("%F%P: failed to create IBT-enabled PLT section\n"));
4436 if (!bfd_set_section_alignment (sec, plt_alignment))
4437 goto error_alignment;
4440 htab->plt_second = sec;
4444 if (!info->no_ld_generated_unwind_info)
4446 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4447 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4448 | SEC_LINKER_CREATED);
4450 sec = bfd_make_section_anyway_with_flags (dynobj,
4451 ".eh_frame",
4452 flags);
4453 if (sec == NULL)
4454 info->callbacks->einfo (_("%F%P: failed to create PLT .eh_frame section\n"));
4456 if (!bfd_set_section_alignment (sec, class_align))
4457 goto error_alignment;
4459 htab->plt_eh_frame = sec;
4461 if (htab->plt_got != NULL)
4463 sec = bfd_make_section_anyway_with_flags (dynobj,
4464 ".eh_frame",
4465 flags);
4466 if (sec == NULL)
4467 info->callbacks->einfo (_("%F%P: failed to create GOT PLT .eh_frame section\n"));
4469 if (!bfd_set_section_alignment (sec, class_align))
4470 goto error_alignment;
4472 htab->plt_got_eh_frame = sec;
4475 if (htab->plt_second != NULL)
4477 sec = bfd_make_section_anyway_with_flags (dynobj,
4478 ".eh_frame",
4479 flags);
4480 if (sec == NULL)
4481 info->callbacks->einfo (_("%F%P: failed to create the second PLT .eh_frame section\n"));
4483 if (!bfd_set_section_alignment (sec, class_align))
4484 goto error_alignment;
4486 htab->plt_second_eh_frame = sec;
4490 /* .sframe sections are emitted for AMD64 ABI only. */
4491 if (ABI_64_P (info->output_bfd) && !info->no_ld_generated_unwind_info)
4493 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4494 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4495 | SEC_LINKER_CREATED);
4497 sec = bfd_make_section_anyway_with_flags (dynobj,
4498 ".sframe",
4499 flags);
4500 if (sec == NULL)
4501 info->callbacks->einfo (_("%F%P: failed to create PLT .sframe section\n"));
4503 // FIXME check this
4504 // if (!bfd_set_section_alignment (sec, class_align))
4505 // goto error_alignment;
4507 htab->plt_sframe = sec;
4509 /* Second PLT is generated for Intel IBT + lazy plt. */
4510 if (htab->plt_second != NULL)
4512 sec = bfd_make_section_anyway_with_flags (dynobj,
4513 ".sframe",
4514 flags);
4515 if (sec == NULL)
4516 info->callbacks->einfo (_("%F%P: failed to create second PLT .sframe section\n"));
4518 htab->plt_second_sframe = sec;
4520 /* FIXME - add later for plt_got. */
4524 /* The .iplt section is used for IFUNC symbols in static
4525 executables. */
4526 sec = htab->elf.iplt;
4527 if (sec != NULL)
4529 /* NB: Delay setting its alignment until we know it is non-empty.
4530 Otherwise an empty iplt section may change vma and lma of the
4531 following sections, which triggers moving dot of the following
4532 section backwards, resulting in a warning and section lma not
4533 being set properly. It later leads to a "File truncated"
4534 error. */
4535 if (!bfd_set_section_alignment (sec, 0))
4536 goto error_alignment;
4538 htab->plt.iplt_alignment = (normal_target
4539 ? plt_alignment
4540 : bed->plt_alignment);
4543 if (bfd_link_executable (info)
4544 && !info->nointerp
4545 && !htab->params->has_dynamic_linker
4546 && htab->params->static_before_all_inputs)
4548 /* Report error for dynamic input objects if -static is passed at
4549 command-line before all input files without --dynamic-linker
4550 unless --no-dynamic-linker is used. */
4551 bfd *abfd;
4553 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
4554 if ((abfd->flags & DYNAMIC))
4555 info->callbacks->einfo
4556 (_("%X%P: attempted static link of dynamic object `%pB'\n"),
4557 abfd);
4560 return pbfd;
4563 /* Fix up x86 GNU properties. */
4565 void
4566 _bfd_x86_elf_link_fixup_gnu_properties
4567 (struct bfd_link_info *info, elf_property_list **listp)
4569 elf_property_list *p;
4571 for (p = *listp; p; p = p->next)
4573 unsigned int type = p->property.pr_type;
4574 if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
4575 || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4576 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
4577 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4578 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
4579 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
4580 || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
4581 && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
4583 if (p->property.u.number == 0
4584 && (type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4585 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
4586 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4587 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
4588 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)))
4590 /* Remove empty property. */
4591 *listp = p->next;
4592 continue;
4595 /* Keep LAM features only for 64-bit output. */
4596 if (type == GNU_PROPERTY_X86_FEATURE_1_AND
4597 && !ABI_64_P (info->output_bfd))
4598 p->property.u.number &= ~(GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4599 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4601 listp = &p->next;
4603 else if (type > GNU_PROPERTY_HIPROC)
4605 /* The property list is sorted in order of type. */
4606 break;
4611 void
4612 _bfd_elf_linker_x86_set_options (struct bfd_link_info * info,
4613 struct elf_linker_x86_params *params)
4615 const struct elf_backend_data *bed
4616 = get_elf_backend_data (info->output_bfd);
4617 struct elf_x86_link_hash_table *htab
4618 = elf_x86_hash_table (info, bed->target_id);
4619 if (htab != NULL)
4620 htab->params = params;