elfxx-x86.c revision 1.1.1.5 1 /* x86 specific support for ELF
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
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.
10
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.
15
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. */
20
21 #include "elfxx-x86.h"
22 #include "elf-vxworks.h"
23 #include "objalloc.h"
24
25 /* The name of the dynamic interpreter. This is put in the .interp
26 section. */
27
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"
31
32 bool
33 _bfd_x86_elf_mkobject (bfd *abfd)
34 {
35 return bfd_elf_allocate_object (abfd, sizeof (struct elf_x86_obj_tdata));
36 }
37
38 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
39 executables. Rather than setting it to the beginning of the TLS
40 section, we have to set it to the end. This function may be called
41 multiple times, it is idempotent. */
42
43 void
44 _bfd_x86_elf_set_tls_module_base (struct bfd_link_info *info)
45 {
46 struct elf_x86_link_hash_table *htab;
47 struct bfd_link_hash_entry *base;
48 const struct elf_backend_data *bed;
49
50 if (!bfd_link_executable (info))
51 return;
52
53 bed = get_elf_backend_data (info->output_bfd);
54 htab = elf_x86_hash_table (info, bed->target_id);
55 if (htab == NULL)
56 return;
57
58 base = htab->tls_module_base;
59 if (base == NULL)
60 return;
61
62 base->u.def.value = htab->elf.tls_size;
63 }
64
65 /* Return the base VMA address which should be subtracted from real addresses
66 when resolving @dtpoff relocation.
67 This is PT_TLS segment p_vaddr. */
68
69 bfd_vma
70 _bfd_x86_elf_dtpoff_base (struct bfd_link_info *info)
71 {
72 /* If tls_sec is NULL, we should have signalled an error already. */
73 if (elf_hash_table (info)->tls_sec == NULL)
74 return 0;
75 return elf_hash_table (info)->tls_sec->vma;
76 }
77
78 /* Allocate space in .plt, .got and associated reloc sections for
79 dynamic relocs. */
80
81 static bool
82 elf_x86_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
83 {
84 struct bfd_link_info *info;
85 struct elf_x86_link_hash_table *htab;
86 struct elf_x86_link_hash_entry *eh;
87 struct elf_dyn_relocs *p;
88 unsigned int plt_entry_size;
89 bool resolved_to_zero;
90 const struct elf_backend_data *bed;
91
92 if (h->root.type == bfd_link_hash_indirect)
93 return true;
94
95 eh = (struct elf_x86_link_hash_entry *) h;
96
97 info = (struct bfd_link_info *) inf;
98 bed = get_elf_backend_data (info->output_bfd);
99 htab = elf_x86_hash_table (info, bed->target_id);
100 if (htab == NULL)
101 return false;
102
103 plt_entry_size = htab->plt.plt_entry_size;
104
105 resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
106
107 /* We can't use the GOT PLT if pointer equality is needed since
108 finish_dynamic_symbol won't clear symbol value and the dynamic
109 linker won't update the GOT slot. We will get into an infinite
110 loop at run-time. */
111 if (htab->plt_got != NULL
112 && h->type != STT_GNU_IFUNC
113 && !h->pointer_equality_needed
114 && h->plt.refcount > 0
115 && h->got.refcount > 0)
116 {
117 /* Don't use the regular PLT if there are both GOT and GOTPLT
118 reloctions. */
119 h->plt.offset = (bfd_vma) -1;
120
121 /* Use the GOT PLT. */
122 eh->plt_got.refcount = 1;
123 }
124
125 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
126 here if it is defined and referenced in a non-shared object. */
127 if (h->type == STT_GNU_IFUNC
128 && h->def_regular)
129 {
130 /* GOTOFF relocation needs PLT. */
131 if (eh->gotoff_ref)
132 h->plt.refcount = 1;
133
134 if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h, &h->dyn_relocs,
135 plt_entry_size,
136 (htab->plt.has_plt0
137 * plt_entry_size),
138 htab->got_entry_size,
139 true))
140 {
141 asection *s = htab->plt_second;
142 if (h->plt.offset != (bfd_vma) -1 && s != NULL)
143 {
144 /* Use the second PLT section if it is created. */
145 eh->plt_second.offset = s->size;
146
147 /* Make room for this entry in the second PLT section. */
148 s->size += htab->non_lazy_plt->plt_entry_size;
149 }
150
151 return true;
152 }
153 else
154 return false;
155 }
156 /* Don't create the PLT entry if there are only function pointer
157 relocations which can be resolved at run-time. */
158 else if (htab->elf.dynamic_sections_created
159 && (h->plt.refcount > 0
160 || eh->plt_got.refcount > 0))
161 {
162 bool use_plt_got = eh->plt_got.refcount > 0;
163
164 /* Make sure this symbol is output as a dynamic symbol.
165 Undefined weak syms won't yet be marked as dynamic. */
166 if (h->dynindx == -1
167 && !h->forced_local
168 && !resolved_to_zero
169 && h->root.type == bfd_link_hash_undefweak)
170 {
171 if (! bfd_elf_link_record_dynamic_symbol (info, h))
172 return false;
173 }
174
175 if (bfd_link_pic (info)
176 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
177 {
178 asection *s = htab->elf.splt;
179 asection *second_s = htab->plt_second;
180 asection *got_s = htab->plt_got;
181 bool use_plt;
182
183 /* If this is the first .plt entry, make room for the special
184 first entry. The .plt section is used by prelink to undo
185 prelinking for dynamic relocations. */
186 if (s->size == 0)
187 s->size = htab->plt.has_plt0 * plt_entry_size;
188
189 if (use_plt_got)
190 eh->plt_got.offset = got_s->size;
191 else
192 {
193 h->plt.offset = s->size;
194 if (second_s)
195 eh->plt_second.offset = second_s->size;
196 }
197
198 /* If this symbol is not defined in a regular file, and we are
199 generating PDE, then set the symbol to this location in the
200 .plt. This is required to make function pointers compare
201 as equal between PDE and the shared library.
202
203 NB: If PLT is PC-relative, we can use the .plt in PIE for
204 function address. */
205 if (h->def_regular)
206 use_plt = false;
207 else if (htab->pcrel_plt)
208 use_plt = ! bfd_link_dll (info);
209 else
210 use_plt = bfd_link_pde (info);
211 if (use_plt)
212 {
213 if (use_plt_got)
214 {
215 /* We need to make a call to the entry of the GOT PLT
216 instead of regular PLT entry. */
217 h->root.u.def.section = got_s;
218 h->root.u.def.value = eh->plt_got.offset;
219 }
220 else
221 {
222 if (second_s)
223 {
224 /* We need to make a call to the entry of the
225 second PLT instead of regular PLT entry. */
226 h->root.u.def.section = second_s;
227 h->root.u.def.value = eh->plt_second.offset;
228 }
229 else
230 {
231 h->root.u.def.section = s;
232 h->root.u.def.value = h->plt.offset;
233 }
234 }
235 }
236
237 /* Make room for this entry. */
238 if (use_plt_got)
239 got_s->size += htab->non_lazy_plt->plt_entry_size;
240 else
241 {
242 s->size += plt_entry_size;
243 if (second_s)
244 second_s->size += htab->non_lazy_plt->plt_entry_size;
245
246 /* We also need to make an entry in the .got.plt section,
247 which will be placed in the .got section by the linker
248 script. */
249 htab->elf.sgotplt->size += htab->got_entry_size;
250
251 /* There should be no PLT relocation against resolved
252 undefined weak symbol in executable. */
253 if (!resolved_to_zero)
254 {
255 /* We also need to make an entry in the .rel.plt
256 section. */
257 htab->elf.srelplt->size += htab->sizeof_reloc;
258 htab->elf.srelplt->reloc_count++;
259 }
260 }
261
262 if (htab->elf.target_os == is_vxworks && !bfd_link_pic (info))
263 {
264 /* VxWorks has a second set of relocations for each PLT entry
265 in executables. They go in a separate relocation section,
266 which is processed by the kernel loader. */
267
268 /* There are two relocations for the initial PLT entry: an
269 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
270 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
271
272 asection *srelplt2 = htab->srelplt2;
273 if (h->plt.offset == plt_entry_size)
274 srelplt2->size += (htab->sizeof_reloc * 2);
275
276 /* There are two extra relocations for each subsequent PLT entry:
277 an R_386_32 relocation for the GOT entry, and an R_386_32
278 relocation for the PLT entry. */
279
280 srelplt2->size += (htab->sizeof_reloc * 2);
281 }
282 }
283 else
284 {
285 eh->plt_got.offset = (bfd_vma) -1;
286 h->plt.offset = (bfd_vma) -1;
287 h->needs_plt = 0;
288 }
289 }
290 else
291 {
292 eh->plt_got.offset = (bfd_vma) -1;
293 h->plt.offset = (bfd_vma) -1;
294 h->needs_plt = 0;
295 }
296
297 eh->tlsdesc_got = (bfd_vma) -1;
298
299 /* For i386, if R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the
300 binary, make it a R_386_TLS_LE_32 requiring no TLS entry. For
301 x86-64, if R_X86_64_GOTTPOFF symbol is now local to the binary,
302 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
303 if (h->got.refcount > 0
304 && bfd_link_executable (info)
305 && h->dynindx == -1
306 && (elf_x86_hash_entry (h)->tls_type & GOT_TLS_IE))
307 h->got.offset = (bfd_vma) -1;
308 else if (h->got.refcount > 0)
309 {
310 asection *s;
311 bool dyn;
312 int tls_type = elf_x86_hash_entry (h)->tls_type;
313
314 /* Make sure this symbol is output as a dynamic symbol.
315 Undefined weak syms won't yet be marked as dynamic. */
316 if (h->dynindx == -1
317 && !h->forced_local
318 && !resolved_to_zero
319 && h->root.type == bfd_link_hash_undefweak)
320 {
321 if (! bfd_elf_link_record_dynamic_symbol (info, h))
322 return false;
323 }
324
325 s = htab->elf.sgot;
326 if (GOT_TLS_GDESC_P (tls_type))
327 {
328 eh->tlsdesc_got = htab->elf.sgotplt->size
329 - elf_x86_compute_jump_table_size (htab);
330 htab->elf.sgotplt->size += 2 * htab->got_entry_size;
331 h->got.offset = (bfd_vma) -2;
332 }
333 if (! GOT_TLS_GDESC_P (tls_type)
334 || GOT_TLS_GD_P (tls_type))
335 {
336 h->got.offset = s->size;
337 s->size += htab->got_entry_size;
338 /* R_386_TLS_GD and R_X86_64_TLSGD need 2 consecutive GOT
339 slots. */
340 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
341 s->size += htab->got_entry_size;
342 }
343 dyn = htab->elf.dynamic_sections_created;
344 /* R_386_TLS_IE_32 needs one dynamic relocation,
345 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
346 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
347 need two), R_386_TLS_GD and R_X86_64_TLSGD need one if local
348 symbol and two if global. No dynamic relocation against
349 resolved undefined weak symbol in executable. No dynamic
350 relocation against non-preemptible absolute symbol. */
351 if (tls_type == GOT_TLS_IE_BOTH)
352 htab->elf.srelgot->size += 2 * htab->sizeof_reloc;
353 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
354 || (tls_type & GOT_TLS_IE))
355 htab->elf.srelgot->size += htab->sizeof_reloc;
356 else if (GOT_TLS_GD_P (tls_type))
357 htab->elf.srelgot->size += 2 * htab->sizeof_reloc;
358 else if (! GOT_TLS_GDESC_P (tls_type)
359 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
360 && !resolved_to_zero)
361 || h->root.type != bfd_link_hash_undefweak)
362 && ((bfd_link_pic (info)
363 && !(h->dynindx == -1
364 && ABS_SYMBOL_P (h)))
365 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
366 htab->elf.srelgot->size += htab->sizeof_reloc;
367 if (GOT_TLS_GDESC_P (tls_type))
368 {
369 htab->elf.srelplt->size += htab->sizeof_reloc;
370 if (bed->target_id == X86_64_ELF_DATA)
371 htab->elf.tlsdesc_plt = (bfd_vma) -1;
372 }
373 }
374 else
375 h->got.offset = (bfd_vma) -1;
376
377 if (h->dyn_relocs == NULL)
378 return true;
379
380 /* In the shared -Bsymbolic case, discard space allocated for
381 dynamic pc-relative relocs against symbols which turn out to be
382 defined in regular objects. For the normal shared case, discard
383 space for pc-relative relocs that have become local due to symbol
384 visibility changes. */
385
386 if (bfd_link_pic (info))
387 {
388 /* Relocs that use pc_count are those that appear on a call
389 insn, or certain REL relocs that can generated via assembly.
390 We want calls to protected symbols to resolve directly to the
391 function rather than going via the plt. If people want
392 function pointer comparisons to work as expected then they
393 should avoid writing weird assembly. */
394 if (SYMBOL_CALLS_LOCAL (info, h))
395 {
396 struct elf_dyn_relocs **pp;
397
398 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
399 {
400 p->count -= p->pc_count;
401 p->pc_count = 0;
402 if (p->count == 0)
403 *pp = p->next;
404 else
405 pp = &p->next;
406 }
407 }
408
409 if (htab->elf.target_os == is_vxworks)
410 {
411 struct elf_dyn_relocs **pp;
412 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
413 {
414 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
415 *pp = p->next;
416 else
417 pp = &p->next;
418 }
419 }
420
421 /* Also discard relocs on undefined weak syms with non-default
422 visibility or in PIE. */
423 if (h->dyn_relocs != NULL)
424 {
425 if (h->root.type == bfd_link_hash_undefweak)
426 {
427 /* Undefined weak symbol is never bound locally in shared
428 library. */
429 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
430 || resolved_to_zero)
431 {
432 if (bed->target_id == I386_ELF_DATA
433 && h->non_got_ref)
434 {
435 /* Keep dynamic non-GOT/non-PLT relocation so
436 that we can branch to 0 without PLT. */
437 struct elf_dyn_relocs **pp;
438
439 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
440 if (p->pc_count == 0)
441 *pp = p->next;
442 else
443 {
444 /* Remove non-R_386_PC32 relocation. */
445 p->count = p->pc_count;
446 pp = &p->next;
447 }
448
449 /* Make sure undefined weak symbols are output
450 as dynamic symbols in PIEs for dynamic non-GOT
451 non-PLT reloations. */
452 if (h->dyn_relocs != NULL
453 && !bfd_elf_link_record_dynamic_symbol (info, h))
454 return false;
455 }
456 else
457 h->dyn_relocs = NULL;
458 }
459 else if (h->dynindx == -1
460 && !h->forced_local
461 && !bfd_elf_link_record_dynamic_symbol (info, h))
462 return false;
463 }
464 else if (bfd_link_executable (info)
465 && (h->needs_copy || eh->needs_copy)
466 && h->def_dynamic
467 && !h->def_regular)
468 {
469 /* NB: needs_copy is set only for x86-64. For PIE,
470 discard space for pc-relative relocs against symbols
471 which turn out to need copy relocs. */
472 struct elf_dyn_relocs **pp;
473
474 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
475 {
476 if (p->pc_count != 0)
477 *pp = p->next;
478 else
479 pp = &p->next;
480 }
481 }
482 }
483 }
484 else if (ELIMINATE_COPY_RELOCS)
485 {
486 /* For the non-shared case, discard space for relocs against
487 symbols which turn out to need copy relocs or are not
488 dynamic. Keep dynamic relocations for run-time function
489 pointer initialization. */
490
491 if ((!h->non_got_ref
492 || (h->root.type == bfd_link_hash_undefweak
493 && !resolved_to_zero))
494 && ((h->def_dynamic
495 && !h->def_regular)
496 || (htab->elf.dynamic_sections_created
497 && (h->root.type == bfd_link_hash_undefweak
498 || h->root.type == bfd_link_hash_undefined))))
499 {
500 /* Make sure this symbol is output as a dynamic symbol.
501 Undefined weak syms won't yet be marked as dynamic. */
502 if (h->dynindx == -1
503 && !h->forced_local
504 && !resolved_to_zero
505 && h->root.type == bfd_link_hash_undefweak
506 && ! bfd_elf_link_record_dynamic_symbol (info, h))
507 return false;
508
509 /* If that succeeded, we know we'll be keeping all the
510 relocs. */
511 if (h->dynindx != -1)
512 goto keep;
513 }
514
515 h->dyn_relocs = NULL;
516
517 keep: ;
518 }
519
520 /* Finally, allocate space. */
521 for (p = h->dyn_relocs; p != NULL; p = p->next)
522 {
523 asection *sreloc;
524
525 if (eh->def_protected && bfd_link_executable (info))
526 {
527 /* Disallow copy relocation against non-copyable protected
528 symbol. */
529 asection *s = p->sec->output_section;
530 if (s != NULL && (s->flags & SEC_READONLY) != 0)
531 {
532 info->callbacks->einfo
533 /* xgettext:c-format */
534 (_("%F%P: %pB: copy relocation against non-copyable "
535 "protected symbol `%s' in %pB\n"),
536 p->sec->owner, h->root.root.string,
537 h->root.u.def.section->owner);
538 return false;
539 }
540 }
541
542 sreloc = elf_section_data (p->sec)->sreloc;
543
544 BFD_ASSERT (sreloc != NULL);
545 sreloc->size += p->count * htab->sizeof_reloc;
546 }
547
548 return true;
549 }
550
551 /* Allocate space in .plt, .got and associated reloc sections for
552 local dynamic relocs. */
553
554 static int
555 elf_x86_allocate_local_dynreloc (void **slot, void *inf)
556 {
557 struct elf_link_hash_entry *h
558 = (struct elf_link_hash_entry *) *slot;
559
560 if (h->type != STT_GNU_IFUNC
561 || !h->def_regular
562 || !h->ref_regular
563 || !h->forced_local
564 || h->root.type != bfd_link_hash_defined)
565 abort ();
566
567 return elf_x86_allocate_dynrelocs (h, inf);
568 }
569
570 /* Find and/or create a hash entry for local symbol. */
571
572 struct elf_link_hash_entry *
573 _bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table *htab,
574 bfd *abfd, const Elf_Internal_Rela *rel,
575 bool create)
576 {
577 struct elf_x86_link_hash_entry e, *ret;
578 asection *sec = abfd->sections;
579 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
580 htab->r_sym (rel->r_info));
581 void **slot;
582
583 e.elf.indx = sec->id;
584 e.elf.dynstr_index = htab->r_sym (rel->r_info);
585 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
586 create ? INSERT : NO_INSERT);
587
588 if (!slot)
589 return NULL;
590
591 if (*slot)
592 {
593 ret = (struct elf_x86_link_hash_entry *) *slot;
594 return &ret->elf;
595 }
596
597 ret = (struct elf_x86_link_hash_entry *)
598 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
599 sizeof (struct elf_x86_link_hash_entry));
600 if (ret)
601 {
602 memset (ret, 0, sizeof (*ret));
603 ret->elf.indx = sec->id;
604 ret->elf.dynstr_index = htab->r_sym (rel->r_info);
605 ret->elf.dynindx = -1;
606 ret->plt_got.offset = (bfd_vma) -1;
607 *slot = ret;
608 }
609 return &ret->elf;
610 }
611
612 /* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN
613 SYNC WITH _bfd_elf_link_hash_newfunc. */
614
615 struct bfd_hash_entry *
616 _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
617 struct bfd_hash_table *table,
618 const char *string)
619 {
620 /* Allocate the structure if it has not already been allocated by a
621 subclass. */
622 if (entry == NULL)
623 {
624 entry = (struct bfd_hash_entry *)
625 bfd_hash_allocate (table,
626 sizeof (struct elf_x86_link_hash_entry));
627 if (entry == NULL)
628 return entry;
629 }
630
631 /* Call the allocation method of the superclass. */
632 entry = _bfd_link_hash_newfunc (entry, table, string);
633 if (entry != NULL)
634 {
635 struct elf_x86_link_hash_entry *eh
636 = (struct elf_x86_link_hash_entry *) entry;
637 struct elf_link_hash_table *htab
638 = (struct elf_link_hash_table *) table;
639
640 memset (&eh->elf.size, 0,
641 (sizeof (struct elf_x86_link_hash_entry)
642 - offsetof (struct elf_link_hash_entry, size)));
643 /* Set local fields. */
644 eh->elf.indx = -1;
645 eh->elf.dynindx = -1;
646 eh->elf.got = htab->init_got_refcount;
647 eh->elf.plt = htab->init_plt_refcount;
648 /* Assume that we have been called by a non-ELF symbol reader.
649 This flag is then reset by the code which reads an ELF input
650 file. This ensures that a symbol created by a non-ELF symbol
651 reader will have the flag set correctly. */
652 eh->elf.non_elf = 1;
653 eh->plt_second.offset = (bfd_vma) -1;
654 eh->plt_got.offset = (bfd_vma) -1;
655 eh->tlsdesc_got = (bfd_vma) -1;
656 eh->zero_undefweak = 1;
657 }
658
659 return entry;
660 }
661
662 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
663 for local symbol so that we can handle local STT_GNU_IFUNC symbols
664 as global symbol. We reuse indx and dynstr_index for local symbol
665 hash since they aren't used by global symbols in this backend. */
666
667 hashval_t
668 _bfd_x86_elf_local_htab_hash (const void *ptr)
669 {
670 struct elf_link_hash_entry *h
671 = (struct elf_link_hash_entry *) ptr;
672 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
673 }
674
675 /* Compare local hash entries. */
676
677 int
678 _bfd_x86_elf_local_htab_eq (const void *ptr1, const void *ptr2)
679 {
680 struct elf_link_hash_entry *h1
681 = (struct elf_link_hash_entry *) ptr1;
682 struct elf_link_hash_entry *h2
683 = (struct elf_link_hash_entry *) ptr2;
684
685 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
686 }
687
688 /* Destroy an x86 ELF linker hash table. */
689
690 static void
691 elf_x86_link_hash_table_free (bfd *obfd)
692 {
693 struct elf_x86_link_hash_table *htab
694 = (struct elf_x86_link_hash_table *) obfd->link.hash;
695
696 if (htab->loc_hash_table)
697 htab_delete (htab->loc_hash_table);
698 if (htab->loc_hash_memory)
699 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
700 _bfd_elf_link_hash_table_free (obfd);
701 }
702
703 static bool
704 elf_i386_is_reloc_section (const char *secname)
705 {
706 return startswith (secname, ".rel");
707 }
708
709 static bool
710 elf_x86_64_is_reloc_section (const char *secname)
711 {
712 return startswith (secname, ".rela");
713 }
714
715 /* Create an x86 ELF linker hash table. */
716
717 struct bfd_link_hash_table *
718 _bfd_x86_elf_link_hash_table_create (bfd *abfd)
719 {
720 struct elf_x86_link_hash_table *ret;
721 size_t amt = sizeof (struct elf_x86_link_hash_table);
722
723 ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt);
724 if (ret == NULL)
725 return NULL;
726
727 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
728 _bfd_x86_elf_link_hash_newfunc,
729 sizeof (struct elf_x86_link_hash_entry)))
730 {
731 free (ret);
732 return NULL;
733 }
734
735 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
736 if (bed->target_id == X86_64_ELF_DATA)
737 {
738 ret->is_reloc_section = elf_x86_64_is_reloc_section;
739 ret->got_entry_size = 8;
740 ret->pcrel_plt = true;
741 ret->tls_get_addr = "__tls_get_addr";
742 ret->relative_r_type = R_X86_64_RELATIVE;
743 ret->relative_r_name = "R_X86_64_RELATIVE";
744 ret->ax_register = "RAX";
745 ret->elf_append_reloc = elf_append_rela;
746 ret->elf_write_addend_in_got = _bfd_elf64_write_addend;
747 }
748 if (ABI_64_P (abfd))
749 {
750 ret->sizeof_reloc = sizeof (Elf64_External_Rela);
751 ret->pointer_r_type = R_X86_64_64;
752 ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
753 ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
754 ret->elf_write_addend = _bfd_elf64_write_addend;
755 }
756 else
757 {
758 if (bed->target_id == X86_64_ELF_DATA)
759 {
760 ret->sizeof_reloc = sizeof (Elf32_External_Rela);
761 ret->pointer_r_type = R_X86_64_32;
762 ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER;
763 ret->dynamic_interpreter_size
764 = sizeof ELFX32_DYNAMIC_INTERPRETER;
765 ret->elf_write_addend = _bfd_elf32_write_addend;
766 }
767 else
768 {
769 ret->is_reloc_section = elf_i386_is_reloc_section;
770 ret->sizeof_reloc = sizeof (Elf32_External_Rel);
771 ret->got_entry_size = 4;
772 ret->pcrel_plt = false;
773 ret->pointer_r_type = R_386_32;
774 ret->relative_r_type = R_386_RELATIVE;
775 ret->relative_r_name = "R_386_RELATIVE";
776 ret->ax_register = "EAX";
777 ret->elf_append_reloc = elf_append_rel;
778 ret->elf_write_addend = _bfd_elf32_write_addend;
779 ret->elf_write_addend_in_got = _bfd_elf32_write_addend;
780 ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
781 ret->dynamic_interpreter_size
782 = sizeof ELF32_DYNAMIC_INTERPRETER;
783 ret->tls_get_addr = "___tls_get_addr";
784 }
785 }
786
787 ret->loc_hash_table = htab_try_create (1024,
788 _bfd_x86_elf_local_htab_hash,
789 _bfd_x86_elf_local_htab_eq,
790 NULL);
791 ret->loc_hash_memory = objalloc_create ();
792 if (!ret->loc_hash_table || !ret->loc_hash_memory)
793 {
794 elf_x86_link_hash_table_free (abfd);
795 return NULL;
796 }
797 ret->elf.root.hash_table_free = elf_x86_link_hash_table_free;
798
799 return &ret->elf.root;
800 }
801
802 /* Sort relocs into address order. */
803
804 int
805 _bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
806 {
807 const arelent *a = * (const arelent **) ap;
808 const arelent *b = * (const arelent **) bp;
809
810 if (a->address > b->address)
811 return 1;
812 else if (a->address < b->address)
813 return -1;
814 else
815 return 0;
816 }
817
818 /* Mark symbol, NAME, as locally defined by linker if it is referenced
819 and not defined in a relocatable object file. */
820
821 static void
822 elf_x86_linker_defined (struct bfd_link_info *info, const char *name)
823 {
824 struct elf_link_hash_entry *h;
825
826 h = elf_link_hash_lookup (elf_hash_table (info), name,
827 false, false, false);
828 if (h == NULL)
829 return;
830
831 while (h->root.type == bfd_link_hash_indirect)
832 h = (struct elf_link_hash_entry *) h->root.u.i.link;
833
834 if (h->root.type == bfd_link_hash_new
835 || h->root.type == bfd_link_hash_undefined
836 || h->root.type == bfd_link_hash_undefweak
837 || h->root.type == bfd_link_hash_common
838 || (!h->def_regular && h->def_dynamic))
839 {
840 elf_x86_hash_entry (h)->local_ref = 2;
841 elf_x86_hash_entry (h)->linker_def = 1;
842 }
843 }
844
845 /* Hide a linker-defined symbol, NAME, with hidden visibility. */
846
847 static void
848 elf_x86_hide_linker_defined (struct bfd_link_info *info,
849 const char *name)
850 {
851 struct elf_link_hash_entry *h;
852
853 h = elf_link_hash_lookup (elf_hash_table (info), name,
854 false, false, false);
855 if (h == NULL)
856 return;
857
858 while (h->root.type == bfd_link_hash_indirect)
859 h = (struct elf_link_hash_entry *) h->root.u.i.link;
860
861 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
862 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
863 _bfd_elf_link_hash_hide_symbol (info, h, true);
864 }
865
866 bool
867 _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
868 {
869 if (!bfd_link_relocatable (info))
870 {
871 /* Check for __tls_get_addr reference. */
872 struct elf_x86_link_hash_table *htab;
873 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
874 htab = elf_x86_hash_table (info, bed->target_id);
875 if (htab)
876 {
877 struct elf_link_hash_entry *h;
878
879 h = elf_link_hash_lookup (elf_hash_table (info),
880 htab->tls_get_addr,
881 false, false, false);
882 if (h != NULL)
883 {
884 elf_x86_hash_entry (h)->tls_get_addr = 1;
885
886 /* Check the versioned __tls_get_addr symbol. */
887 while (h->root.type == bfd_link_hash_indirect)
888 {
889 h = (struct elf_link_hash_entry *) h->root.u.i.link;
890 elf_x86_hash_entry (h)->tls_get_addr = 1;
891 }
892 }
893
894 /* "__ehdr_start" will be defined by linker as a hidden symbol
895 later if it is referenced and not defined. */
896 elf_x86_linker_defined (info, "__ehdr_start");
897
898 if (bfd_link_executable (info))
899 {
900 /* References to __bss_start, _end and _edata should be
901 locally resolved within executables. */
902 elf_x86_linker_defined (info, "__bss_start");
903 elf_x86_linker_defined (info, "_end");
904 elf_x86_linker_defined (info, "_edata");
905 }
906 else
907 {
908 /* Hide hidden __bss_start, _end and _edata in shared
909 libraries. */
910 elf_x86_hide_linker_defined (info, "__bss_start");
911 elf_x86_hide_linker_defined (info, "_end");
912 elf_x86_hide_linker_defined (info, "_edata");
913 }
914 }
915 }
916
917 /* Invoke the regular ELF backend linker to do all the work. */
918 return _bfd_elf_link_check_relocs (abfd, info);
919 }
920
921 /* Look through the relocs for a section before allocation to make the
922 dynamic reloc section. */
923
924 bool
925 _bfd_x86_elf_check_relocs (bfd *abfd,
926 struct bfd_link_info *info,
927 asection *sec,
928 const Elf_Internal_Rela *relocs)
929 {
930 struct elf_x86_link_hash_table *htab;
931 Elf_Internal_Shdr *symtab_hdr;
932 struct elf_link_hash_entry **sym_hashes;
933 const Elf_Internal_Rela *rel;
934 const Elf_Internal_Rela *rel_end;
935 asection *sreloc;
936 const struct elf_backend_data *bed;
937 bool is_x86_64;
938
939 if (bfd_link_relocatable (info))
940 return true;
941
942 bed = get_elf_backend_data (abfd);
943 htab = elf_x86_hash_table (info, bed->target_id);
944 if (htab == NULL)
945 {
946 sec->check_relocs_failed = 1;
947 return false;
948 }
949
950 is_x86_64 = bed->target_id == X86_64_ELF_DATA;
951
952 symtab_hdr = &elf_symtab_hdr (abfd);
953 sym_hashes = elf_sym_hashes (abfd);
954
955 rel_end = relocs + sec->reloc_count;
956 for (rel = relocs; rel < rel_end; rel++)
957 {
958 unsigned int r_type;
959 unsigned int r_symndx;
960 struct elf_link_hash_entry *h;
961
962 r_symndx = htab->r_sym (rel->r_info);
963 r_type = ELF32_R_TYPE (rel->r_info);
964
965 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
966 {
967 /* xgettext:c-format */
968 _bfd_error_handler (_("%pB: bad symbol index: %d"),
969 abfd, r_symndx);
970 goto error_return;
971 }
972
973 if (r_symndx < symtab_hdr->sh_info)
974 h = NULL;
975 else
976 {
977 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
978 while (h->root.type == bfd_link_hash_indirect
979 || h->root.type == bfd_link_hash_warning)
980 h = (struct elf_link_hash_entry *) h->root.u.i.link;
981 }
982
983 if (X86_NEED_DYNAMIC_RELOC_TYPE_P (is_x86_64, r_type)
984 && NEED_DYNAMIC_RELOCATION_P (is_x86_64, info, true, h, sec,
985 r_type, htab->pointer_r_type))
986 {
987 /* We may copy these reloc types into the output file.
988 Create a reloc section in dynobj and make room for
989 this reloc. */
990 sreloc = _bfd_elf_make_dynamic_reloc_section
991 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
992 abfd, sec->use_rela_p);
993
994 if (sreloc != NULL)
995 return true;
996
997 error_return:
998 sec->check_relocs_failed = 1;
999 return false;
1000 }
1001 }
1002
1003 return true;
1004 }
1005
1006 /* Add an entry to the relative reloc record. */
1007
1008 static bool
1009 elf_x86_relative_reloc_record_add
1010 (struct bfd_link_info *info,
1011 struct elf_x86_relative_reloc_data *relative_reloc,
1012 Elf_Internal_Rela *rel, asection *sec,
1013 asection *sym_sec, struct elf_link_hash_entry *h,
1014 Elf_Internal_Sym *sym, bfd_vma offset, bool *keep_symbuf_p)
1015 {
1016 bfd_size_type newidx;
1017
1018 if (relative_reloc->data == NULL)
1019 {
1020 relative_reloc->data = bfd_malloc
1021 (sizeof (struct elf_x86_relative_reloc_record));
1022 relative_reloc->count = 0;
1023 relative_reloc->size = 1;
1024 }
1025
1026 newidx = relative_reloc->count++;
1027
1028 if (relative_reloc->count > relative_reloc->size)
1029 {
1030 relative_reloc->size <<= 1;
1031 relative_reloc->data = bfd_realloc
1032 (relative_reloc->data,
1033 (relative_reloc->size
1034 * sizeof (struct elf_x86_relative_reloc_record)));
1035 }
1036
1037 if (relative_reloc->data == NULL)
1038 {
1039 info->callbacks->einfo
1040 /* xgettext:c-format */
1041 (_("%F%P: %pB: failed to allocate relative reloc record\n"),
1042 info->output_bfd);
1043 return false;
1044 }
1045
1046 relative_reloc->data[newidx].rel = *rel;
1047 relative_reloc->data[newidx].sec = sec;
1048 if (h != NULL)
1049 {
1050 /* Set SYM to NULL to indicate a global symbol. */
1051 relative_reloc->data[newidx].sym = NULL;
1052 relative_reloc->data[newidx].u.h = h;
1053 }
1054 else
1055 {
1056 relative_reloc->data[newidx].sym = sym;
1057 relative_reloc->data[newidx].u.sym_sec = sym_sec;
1058 /* We must keep the symbol buffer since SYM will be used later. */
1059 *keep_symbuf_p = true;
1060 }
1061 relative_reloc->data[newidx].offset = offset;
1062 relative_reloc->data[newidx].address = 0;
1063 return true;
1064 }
1065
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. */
1072
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)
1078 {
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;
1089 bool return_status = false;
1090 bool keep_symbuf = false;
1091
1092 if (bfd_link_relocatable (info))
1093 return true;
1094
1095 /* Assume we're not going to change any sizes, and we'll only need
1096 one pass. */
1097 *again = false;
1098
1099 bed = get_elf_backend_data (abfd);
1100 htab = elf_x86_hash_table (info, bed->target_id);
1101 if (htab == NULL)
1102 return true;
1103
1104 /* Nothing to do if there are no relocations or relative relocations
1105 have been packed. */
1106 if (input_section == htab->elf.srelrdyn
1107 || input_section->relative_reloc_packed
1108 || ((input_section->flags & (SEC_RELOC | SEC_ALLOC))
1109 != (SEC_RELOC | SEC_ALLOC))
1110 || (input_section->flags & SEC_DEBUGGING) != 0
1111 || input_section->reloc_count == 0)
1112 return true;
1113
1114 /* Skip if the section isn't aligned. */
1115 unaligned_section = input_section->alignment_power == 0;
1116
1117 is_x86_64 = bed->target_id == X86_64_ELF_DATA;
1118
1119 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1120 sym_hashes = elf_sym_hashes (abfd);
1121 local_got_offsets = elf_local_got_offsets (abfd);
1122
1123 /* Load the relocations for this section. */
1124 internal_relocs =
1125 _bfd_elf_link_info_read_relocs (abfd, info, input_section, NULL,
1126 (Elf_Internal_Rela *) NULL,
1127 info->keep_memory);
1128 if (internal_relocs == NULL)
1129 return false;
1130
1131 irelend = internal_relocs + input_section->reloc_count;
1132 for (irel = internal_relocs; irel < irelend; irel++)
1133 {
1134 unsigned int r_type;
1135 unsigned int r_symndx;
1136 Elf_Internal_Sym *isym;
1137 struct elf_link_hash_entry *h;
1138 struct elf_x86_link_hash_entry *eh;
1139 bfd_vma offset;
1140 bool resolved_to_zero;
1141 bool need_copy_reloc_in_pie;
1142 bool pc32_reloc;
1143 asection *sec;
1144 /* Offset must be a multiple of 2. */
1145 bool unaligned_offset = (irel->r_offset & 1) != 0;
1146 /* True if there is a relative relocation against a dynamic
1147 symbol. */
1148 bool dynamic_relative_reloc_p;
1149
1150 /* Get the value of the symbol referred to by the reloc. */
1151 r_symndx = htab->r_sym (irel->r_info);
1152
1153 r_type = ELF32_R_TYPE (irel->r_info);
1154 /* Clear the R_X86_64_converted_reloc_bit bit. */
1155 r_type &= ~R_X86_64_converted_reloc_bit;
1156
1157 sec = NULL;
1158 h = NULL;
1159 dynamic_relative_reloc_p = false;
1160
1161 if (r_symndx < symtab_hdr->sh_info)
1162 {
1163 /* Read this BFD's local symbols. */
1164 if (isymbuf == NULL)
1165 {
1166 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1167 if (isymbuf == NULL)
1168 {
1169 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1170 symtab_hdr->sh_info,
1171 0, NULL, NULL, NULL);
1172 if (isymbuf == NULL)
1173 goto error_return;
1174 }
1175 }
1176
1177 isym = isymbuf + r_symndx;
1178 switch (isym->st_shndx)
1179 {
1180 case SHN_ABS:
1181 sec = bfd_abs_section_ptr;
1182 break;
1183 case SHN_COMMON:
1184 sec = bfd_com_section_ptr;
1185 break;
1186 case SHN_X86_64_LCOMMON:
1187 if (!is_x86_64)
1188 abort ();
1189 sec = &_bfd_elf_large_com_section;
1190 break;
1191 default:
1192 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1193 break;
1194 }
1195
1196 /* Skip relocation against local STT_GNU_IFUNC symbol. */
1197 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1198 continue;
1199
1200 eh = (struct elf_x86_link_hash_entry *) h;
1201 resolved_to_zero = false;
1202 }
1203 else
1204 {
1205 /* Get H and SEC for GENERATE_DYNAMIC_RELOCATION_P below. */
1206 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1207 while (h->root.type == bfd_link_hash_indirect
1208 || h->root.type == bfd_link_hash_warning)
1209 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1210
1211 if (h->root.type == bfd_link_hash_defined
1212 || h->root.type == bfd_link_hash_defweak)
1213 sec = h->root.u.def.section;
1214
1215 /* Skip relocation against STT_GNU_IFUNC symbol. */
1216 if (h->type == STT_GNU_IFUNC)
1217 continue;
1218
1219 eh = (struct elf_x86_link_hash_entry *) h;
1220 resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
1221
1222 /* NB: See how elf_backend_finish_dynamic_symbol is called
1223 from elf_link_output_extsym. */
1224 if ((h->dynindx != -1 || h->forced_local)
1225 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1226 || h->root.type != bfd_link_hash_undefweak)
1227 || !h->forced_local)
1228 && h->got.offset != (bfd_vma) -1
1229 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
1230 && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
1231 && !resolved_to_zero
1232 && SYMBOL_REFERENCES_LOCAL_P (info, h)
1233 && SYMBOL_DEFINED_NON_SHARED_P (h))
1234 dynamic_relative_reloc_p = true;
1235
1236 isym = NULL;
1237 }
1238
1239 if (X86_GOT_TYPE_P (is_x86_64, r_type))
1240 {
1241 /* Pack GOT relative relocations. There should be only a
1242 single R_*_RELATIVE relocation in GOT. */
1243 if (eh != NULL)
1244 {
1245 if (eh->got_relative_reloc_done)
1246 continue;
1247
1248 if (!(dynamic_relative_reloc_p
1249 || (RESOLVED_LOCALLY_P (info, h, htab)
1250 && GENERATE_RELATIVE_RELOC_P (info, h))))
1251 continue;
1252
1253 if (!dynamic_relative_reloc_p)
1254 eh->no_finish_dynamic_symbol = 1;
1255 eh->got_relative_reloc_done = 1;
1256 offset = h->got.offset;
1257 }
1258 else
1259 {
1260 if (elf_x86_relative_reloc_done (abfd)[r_symndx])
1261 continue;
1262
1263 if (!X86_LOCAL_GOT_RELATIVE_RELOC_P (is_x86_64, info,
1264 isym))
1265 continue;
1266
1267 elf_x86_relative_reloc_done (abfd)[r_symndx] = 1;
1268 offset = local_got_offsets[r_symndx];
1269 }
1270
1271 if (!elf_x86_relative_reloc_record_add (info,
1272 &htab->relative_reloc,
1273 irel, htab->elf.sgot,
1274 sec, h, isym, offset,
1275 &keep_symbuf))
1276 goto error_return;
1277
1278 continue;
1279 }
1280
1281 if (is_x86_64
1282 && irel->r_addend == 0
1283 && !ABI_64_P (info->output_bfd))
1284 {
1285 /* For x32, if addend is zero, treat R_X86_64_64 like
1286 R_X86_64_32 and R_X86_64_SIZE64 like R_X86_64_SIZE32. */
1287 if (r_type == R_X86_64_64)
1288 r_type = R_X86_64_32;
1289 else if (r_type == R_X86_64_SIZE64)
1290 r_type = R_X86_64_SIZE32;
1291 }
1292
1293 if (!X86_RELATIVE_RELOC_TYPE_P (is_x86_64, r_type))
1294 continue;
1295
1296 /* Pack non-GOT relative relocations. */
1297 if (is_x86_64)
1298 {
1299 need_copy_reloc_in_pie =
1300 (bfd_link_pie (info)
1301 && h != NULL
1302 && (h->needs_copy
1303 || eh->needs_copy
1304 || (h->root.type == bfd_link_hash_undefined))
1305 && (X86_PCREL_TYPE_P (true, r_type)
1306 || X86_SIZE_TYPE_P (true, r_type)));
1307 pc32_reloc = false;
1308 }
1309 else
1310 {
1311 need_copy_reloc_in_pie = false;
1312 pc32_reloc = r_type == R_386_PC32;
1313 }
1314
1315 if (GENERATE_DYNAMIC_RELOCATION_P (is_x86_64, info, eh, r_type,
1316 sec, need_copy_reloc_in_pie,
1317 resolved_to_zero, pc32_reloc))
1318 {
1319 /* When generating a shared object, these relocations
1320 are copied into the output file to be resolved at run
1321 time. */
1322 offset = _bfd_elf_section_offset (info->output_bfd, info,
1323 input_section,
1324 irel->r_offset);
1325 if (offset == (bfd_vma) -1
1326 || offset == (bfd_vma) -2
1327 || COPY_INPUT_RELOC_P (is_x86_64, info, h, r_type))
1328 continue;
1329
1330 /* This symbol is local, or marked to become local. When
1331 relocation overflow check is disabled, we convert
1332 R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
1333 if (is_x86_64
1334 && !(r_type == htab->pointer_r_type
1335 || (r_type == R_X86_64_32
1336 && htab->params->no_reloc_overflow_check)))
1337 continue;
1338
1339 if (!elf_x86_relative_reloc_record_add
1340 (info,
1341 ((unaligned_section || unaligned_offset)
1342 ? &htab->unaligned_relative_reloc
1343 : &htab->relative_reloc),
1344 irel, input_section, sec, h, isym, offset,
1345 &keep_symbuf))
1346 goto error_return;
1347 }
1348 }
1349
1350 input_section->relative_reloc_packed = 1;
1351
1352 return_status = true;
1353
1354 error_return:
1355 if ((unsigned char *) isymbuf != symtab_hdr->contents)
1356 {
1357 /* Cache the symbol buffer if it must be kept. */
1358 if (keep_symbuf)
1359 symtab_hdr->contents = (unsigned char *) isymbuf;
1360 else
1361 free (isymbuf);
1362 }
1363 if (elf_section_data (input_section)->relocs != internal_relocs)
1364 free (internal_relocs);
1365 return return_status;
1366 }
1367
1368 /* Add an entry to the 64-bit DT_RELR bitmap. */
1369
1370 static void
1371 elf64_dt_relr_bitmap_add
1372 (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
1373 uint64_t entry)
1374 {
1375 bfd_size_type newidx;
1376
1377 if (bitmap->u.elf64 == NULL)
1378 {
1379 bitmap->u.elf64 = bfd_malloc (sizeof (uint64_t));
1380 bitmap->count = 0;
1381 bitmap->size = 1;
1382 }
1383
1384 newidx = bitmap->count++;
1385
1386 if (bitmap->count > bitmap->size)
1387 {
1388 bitmap->size <<= 1;
1389 bitmap->u.elf64 = bfd_realloc (bitmap->u.elf64,
1390 (bitmap->size * sizeof (uint64_t)));
1391 }
1392
1393 if (bitmap->u.elf64 == NULL)
1394 {
1395 info->callbacks->einfo
1396 /* xgettext:c-format */
1397 (_("%F%P: %pB: failed to allocate 64-bit DT_RELR bitmap\n"),
1398 info->output_bfd);
1399 }
1400
1401 bitmap->u.elf64[newidx] = entry;
1402 }
1403
1404 /* Add an entry to the 32-bit DT_RELR bitmap. */
1405
1406 static void
1407 elf32_dt_relr_bitmap_add
1408 (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
1409 uint32_t entry)
1410 {
1411 bfd_size_type newidx;
1412
1413 if (bitmap->u.elf32 == NULL)
1414 {
1415 bitmap->u.elf32 = bfd_malloc (sizeof (uint32_t));
1416 bitmap->count = 0;
1417 bitmap->size = 1;
1418 }
1419
1420 newidx = bitmap->count++;
1421
1422 if (bitmap->count > bitmap->size)
1423 {
1424 bitmap->size <<= 1;
1425 bitmap->u.elf32 = bfd_realloc (bitmap->u.elf32,
1426 (bitmap->size * sizeof (uint32_t)));
1427 }
1428
1429 if (bitmap->u.elf32 == NULL)
1430 {
1431 info->callbacks->einfo
1432 /* xgettext:c-format */
1433 (_("%F%P: %pB: failed to allocate 32-bit DT_RELR bitmap\n"),
1434 info->output_bfd);
1435 }
1436
1437 bitmap->u.elf32[newidx] = entry;
1438 }
1439
1440 void
1441 _bfd_elf32_write_addend (bfd *abfd, uint64_t value, void *addr)
1442 {
1443 bfd_put_32 (abfd, value, addr);
1444 }
1445
1446 void
1447 _bfd_elf64_write_addend (bfd *abfd, uint64_t value, void *addr)
1448 {
1449 bfd_put_64 (abfd, value, addr);
1450 }
1451
1452 /* Size or finish relative relocations to determine the run-time
1453 addresses for DT_RELR bitmap computation later. OUTREL is set
1454 to NULL in the sizing phase and non-NULL in the finising phase
1455 where the regular relative relocations will be written out. */
1456
1457 static void
1458 elf_x86_size_or_finish_relative_reloc
1459 (bool is_x86_64, struct bfd_link_info *info,
1460 struct elf_x86_link_hash_table *htab, bool unaligned,
1461 Elf_Internal_Rela *outrel)
1462 {
1463 unsigned int align_mask;
1464 bfd_size_type i, count;
1465 asection *sec, *srel;
1466 struct elf_link_hash_entry *h;
1467 bfd_vma offset;
1468 Elf_Internal_Sym *sym;
1469 asection *sym_sec;
1470 asection *sgot = htab->elf.sgot;
1471 asection *srelgot = htab->elf.srelgot;
1472 struct elf_x86_relative_reloc_data *relative_reloc;
1473
1474 if (unaligned)
1475 {
1476 align_mask = 0;
1477 relative_reloc = &htab->unaligned_relative_reloc;
1478 }
1479 else
1480 {
1481 align_mask = 1;
1482 relative_reloc = &htab->relative_reloc;
1483 }
1484
1485 count = relative_reloc->count;
1486 for (i = 0; i < count; i++)
1487 {
1488 sec = relative_reloc->data[i].sec;
1489 sym = relative_reloc->data[i].sym;
1490
1491 /* If SYM is NULL, it must be a global symbol. */
1492 if (sym == NULL)
1493 h = relative_reloc->data[i].u.h;
1494 else
1495 h = NULL;
1496
1497 if (is_x86_64)
1498 {
1499 bfd_vma relocation;
1500 /* This function may be called more than once and REL may be
1501 updated by _bfd_elf_rela_local_sym below. */
1502 Elf_Internal_Rela rel = relative_reloc->data[i].rel;
1503
1504 if (h != NULL)
1505 {
1506 if (h->root.type == bfd_link_hash_defined
1507 || h->root.type == bfd_link_hash_defweak)
1508 {
1509 sym_sec = h->root.u.def.section;
1510 relocation = (h->root.u.def.value
1511 + sym_sec->output_section->vma
1512 + sym_sec->output_offset);
1513 }
1514 else
1515 {
1516 /* Allow undefined symbol only at the sizing phase.
1517 Otherwise skip undefined symbol here. Undefined
1518 symbol will be reported by relocate_section. */
1519 if (outrel == NULL)
1520 relocation = 0;
1521 else
1522 continue;
1523 }
1524 }
1525 else
1526 {
1527 sym_sec = relative_reloc->data[i].u.sym_sec;
1528 relocation = _bfd_elf_rela_local_sym
1529 (info->output_bfd, sym, &sym_sec, &rel);
1530 }
1531
1532 if (outrel != NULL)
1533 {
1534 outrel->r_addend = relocation;
1535 if (sec == sgot)
1536 {
1537 if (h != NULL && h->needs_plt)
1538 abort ();
1539 }
1540 else
1541 outrel->r_addend += rel.r_addend;
1542
1543 /* Write the implicit addend if ALIGN_MASK isn't 0. */
1544 if (align_mask)
1545 {
1546 if (sec == sgot)
1547 {
1548 if (relative_reloc->data[i].offset >= sec->size)
1549 abort ();
1550 htab->elf_write_addend_in_got
1551 (info->output_bfd, outrel->r_addend,
1552 sec->contents + relative_reloc->data[i].offset);
1553 }
1554 else
1555 {
1556 bfd_byte *contents;
1557
1558 if (rel.r_offset >= sec->size)
1559 abort ();
1560
1561 if (elf_section_data (sec)->this_hdr.contents
1562 != NULL)
1563 contents
1564 = elf_section_data (sec)->this_hdr.contents;
1565 else
1566 {
1567 if (!_bfd_elf_mmap_section_contents (sec->owner,
1568 sec,
1569 &contents))
1570 info->callbacks->einfo
1571 /* xgettext:c-format */
1572 (_("%F%P: %pB: failed to allocate memory for section `%pA'\n"),
1573 info->output_bfd, sec);
1574
1575 /* Cache the section contents for
1576 elf_link_input_bfd. */
1577 elf_section_data (sec)->this_hdr.contents
1578 = contents;
1579 }
1580 htab->elf_write_addend
1581 (info->output_bfd, outrel->r_addend,
1582 contents + rel.r_offset);
1583 }
1584 }
1585 }
1586 }
1587
1588 if (sec == sgot)
1589 srel = srelgot;
1590 else
1591 srel = elf_section_data (sec)->sreloc;
1592 offset = (sec->output_section->vma + sec->output_offset
1593 + relative_reloc->data[i].offset);
1594 relative_reloc->data[i].address = offset;
1595 if (outrel != NULL)
1596 {
1597 outrel->r_offset = offset;
1598
1599 if ((outrel->r_offset & align_mask) != 0)
1600 abort ();
1601
1602 if (htab->params->report_relative_reloc)
1603 _bfd_x86_elf_link_report_relative_reloc
1604 (info, sec, h, sym, htab->relative_r_name, outrel);
1605
1606 /* Generate regular relative relocation if ALIGN_MASK is 0. */
1607 if (align_mask == 0)
1608 htab->elf_append_reloc (info->output_bfd, srel, outrel);
1609 }
1610 }
1611 }
1612
1613 /* Compute the DT_RELR section size. Set NEED_PLAYOUT to true if
1614 the DT_RELR section size has been increased. */
1615
1616 static void
1617 elf_x86_compute_dl_relr_bitmap
1618 (struct bfd_link_info *info, struct elf_x86_link_hash_table *htab,
1619 bool *need_layout)
1620 {
1621 bfd_vma base;
1622 bfd_size_type i, count, new_count;
1623 struct elf_x86_relative_reloc_data *relative_reloc =
1624 &htab->relative_reloc;
1625 /* Save the old DT_RELR bitmap count. Don't shrink the DT_RELR bitmap
1626 if the new DT_RELR bitmap count is smaller than the old one. Pad
1627 with trailing 1s which won't be decoded to more relocations. */
1628 bfd_size_type dt_relr_bitmap_count = htab->dt_relr_bitmap.count;
1629
1630 /* Clear the DT_RELR bitmap count. */
1631 htab->dt_relr_bitmap.count = 0;
1632
1633 count = relative_reloc->count;
1634
1635 if (ABI_64_P (info->output_bfd))
1636 {
1637 /* Compute the 64-bit DT_RELR bitmap. */
1638 i = 0;
1639 while (i < count)
1640 {
1641 if ((relative_reloc->data[i].address % 1) != 0)
1642 abort ();
1643
1644 elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1645 relative_reloc->data[i].address);
1646
1647 base = relative_reloc->data[i].address + 8;
1648 i++;
1649
1650 while (i < count)
1651 {
1652 uint64_t bitmap = 0;
1653 for (; i < count; i++)
1654 {
1655 bfd_vma delta = (relative_reloc->data[i].address
1656 - base);
1657 /* Stop if it is too far from base. */
1658 if (delta >= 63 * 8)
1659 break;
1660 /* Stop if it isn't a multiple of 8. */
1661 if ((delta % 8) != 0)
1662 break;
1663 bitmap |= 1ULL << (delta / 8);
1664 }
1665
1666 if (bitmap == 0)
1667 break;
1668
1669 elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1670 (bitmap << 1) | 1);
1671
1672 base += 63 * 8;
1673 }
1674 }
1675
1676 new_count = htab->dt_relr_bitmap.count;
1677 if (dt_relr_bitmap_count > new_count)
1678 {
1679 /* Don't shrink the DT_RELR section size to avoid section
1680 layout oscillation. Instead, pad the DT_RELR bitmap with
1681 1s which do not decode to more relocations. */
1682
1683 htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
1684 count = dt_relr_bitmap_count - new_count;
1685 for (i = 0; i < count; i++)
1686 htab->dt_relr_bitmap.u.elf64[new_count + i] = 1;
1687 }
1688 }
1689 else
1690 {
1691 /* Compute the 32-bit DT_RELR bitmap. */
1692 i = 0;
1693 while (i < count)
1694 {
1695 if ((relative_reloc->data[i].address % 1) != 0)
1696 abort ();
1697
1698 elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1699 relative_reloc->data[i].address);
1700
1701 base = relative_reloc->data[i].address + 4;
1702 i++;
1703
1704 while (i < count)
1705 {
1706 uint32_t bitmap = 0;
1707 for (; i < count; i++)
1708 {
1709 bfd_vma delta = (relative_reloc->data[i].address
1710 - base);
1711 /* Stop if it is too far from base. */
1712 if (delta >= 31 * 4)
1713 break;
1714 /* Stop if it isn't a multiple of 4. */
1715 if ((delta % 4) != 0)
1716 break;
1717 bitmap |= 1ULL << (delta / 4);
1718 }
1719
1720 if (bitmap == 0)
1721 break;
1722
1723 elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1724 (bitmap << 1) | 1);
1725
1726 base += 31 * 4;
1727 }
1728 }
1729
1730 new_count = htab->dt_relr_bitmap.count;
1731 if (dt_relr_bitmap_count > new_count)
1732 {
1733 /* Don't shrink the DT_RELR section size to avoid section
1734 layout oscillation. Instead, pad the DT_RELR bitmap with
1735 1s which do not decode to more relocations. */
1736
1737 htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
1738 count = dt_relr_bitmap_count - new_count;
1739 for (i = 0; i < count; i++)
1740 htab->dt_relr_bitmap.u.elf32[new_count + i] = 1;
1741 }
1742 }
1743
1744 if (htab->dt_relr_bitmap.count != dt_relr_bitmap_count)
1745 {
1746 if (need_layout)
1747 {
1748 /* The .relr.dyn section size is changed. Update the section
1749 size and tell linker to layout sections again. */
1750 htab->elf.srelrdyn->size =
1751 (htab->dt_relr_bitmap.count
1752 * (ABI_64_P (info->output_bfd) ? 8 : 4));
1753
1754 *need_layout = true;
1755 }
1756 else
1757 info->callbacks->einfo
1758 /* xgettext:c-format */
1759 (_("%F%P: %pB: size of compact relative reloc section is "
1760 "changed: new (%lu) != old (%lu)\n"),
1761 info->output_bfd, htab->dt_relr_bitmap.count,
1762 dt_relr_bitmap_count);
1763 }
1764 }
1765
1766 /* Write out the DT_RELR section. */
1767
1768 static void
1769 elf_x86_write_dl_relr_bitmap (struct bfd_link_info *info,
1770 struct elf_x86_link_hash_table *htab)
1771 {
1772 asection *sec = htab->elf.srelrdyn;
1773 bfd_size_type size = sec->size;
1774 bfd_size_type i;
1775 unsigned char *contents;
1776
1777 contents = (unsigned char *) bfd_alloc (sec->owner, size);
1778 if (contents == NULL)
1779 info->callbacks->einfo
1780 /* xgettext:c-format */
1781 (_("%F%P: %pB: failed to allocate compact relative reloc section\n"),
1782 info->output_bfd);
1783
1784 /* Cache the section contents for elf_link_input_bfd. */
1785 sec->contents = contents;
1786
1787 if (ABI_64_P (info->output_bfd))
1788 for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 8)
1789 bfd_put_64 (info->output_bfd, htab->dt_relr_bitmap.u.elf64[i],
1790 contents);
1791 else
1792 for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 4)
1793 bfd_put_32 (info->output_bfd, htab->dt_relr_bitmap.u.elf32[i],
1794 contents);
1795 }
1796
1797 /* Sort relative relocations by address. */
1798
1799 static int
1800 elf_x86_relative_reloc_compare (const void *pa, const void *pb)
1801 {
1802 struct elf_x86_relative_reloc_record *a =
1803 (struct elf_x86_relative_reloc_record *) pa;
1804 struct elf_x86_relative_reloc_record *b =
1805 (struct elf_x86_relative_reloc_record *) pb;
1806 if (a->address < b->address)
1807 return -1;
1808 if (a->address > b->address)
1809 return 1;
1810 return 0;
1811 }
1812
1813 enum dynobj_sframe_plt_type
1814 {
1815 SFRAME_PLT = 1,
1816 SFRAME_PLT_SEC = 2,
1817 SFRAME_PLT_GOT = 3,
1818 };
1819
1820 /* Create SFrame stack trace info for the plt entries in the .plt section
1821 of type PLT_SEC_TYPE. */
1822
1823 static bool
1824 _bfd_x86_elf_create_sframe_plt (bfd *output_bfd,
1825 struct bfd_link_info *info,
1826 unsigned int plt_sec_type)
1827 {
1828 struct elf_x86_link_hash_table *htab;
1829 const struct elf_backend_data *bed;
1830
1831 unsigned int plt0_entry_size;
1832 unsigned char func_info;
1833 uint32_t fre_type;
1834 /* The dynamic plt section for which .sframe stack trace information is being
1835 created. */
1836 asection *dpltsec;
1837
1838 int err = 0;
1839
1840 sframe_encoder_ctx **ectx = NULL;
1841 unsigned plt_entry_size = 0;
1842 unsigned int num_pltn_fres = 0;
1843 unsigned int num_pltn_entries = 0;
1844 const sframe_frame_row_entry * const *pltn_fres;
1845
1846 bed = get_elf_backend_data (output_bfd);
1847 htab = elf_x86_hash_table (info, bed->target_id);
1848 /* Whether SFrame stack trace info for plt0 is to be generated. */
1849 switch (plt_sec_type)
1850 {
1851 case SFRAME_PLT:
1852 {
1853 ectx = &htab->plt_cfe_ctx;
1854 dpltsec = htab->elf.splt;
1855
1856 plt0_entry_size
1857 = htab->plt.has_plt0 ? htab->sframe_plt->plt0_entry_size : 0;
1858 plt_entry_size = htab->sframe_plt->pltn_entry_size;
1859 pltn_fres = htab->sframe_plt->pltn_fres;
1860 num_pltn_fres = htab->sframe_plt->pltn_num_fres;
1861 num_pltn_entries
1862 = (dpltsec->size - plt0_entry_size) / plt_entry_size;
1863
1864 break;
1865 }
1866 case SFRAME_PLT_SEC:
1867 {
1868 ectx = &htab->plt_second_cfe_ctx;
1869 dpltsec = htab->plt_second;
1870
1871 plt0_entry_size = 0;
1872
1873 plt_entry_size = htab->sframe_plt->sec_pltn_entry_size;
1874 pltn_fres = htab->sframe_plt->sec_pltn_fres;
1875 num_pltn_fres = htab->sframe_plt->sec_pltn_num_fres;
1876 num_pltn_entries = dpltsec->size / plt_entry_size;
1877
1878 break;
1879 }
1880 case SFRAME_PLT_GOT:
1881 {
1882 ectx = &htab->plt_got_cfe_ctx;
1883 dpltsec = htab->plt_got;
1884
1885 plt0_entry_size = 0;
1886
1887 plt_entry_size = htab->sframe_plt->plt_got_entry_size;
1888 pltn_fres = htab->sframe_plt->plt_got_fres;
1889 num_pltn_fres = htab->sframe_plt->plt_got_num_fres;
1890 num_pltn_entries = dpltsec->size / plt_entry_size;
1891
1892 break;
1893 }
1894
1895 default:
1896 /* No other value is possible. */
1897 return false;
1898 break;
1899 }
1900
1901 *ectx = sframe_encode (SFRAME_VERSION_2,
1902 0,
1903 SFRAME_ABI_AMD64_ENDIAN_LITTLE,
1904 SFRAME_CFA_FIXED_FP_INVALID,
1905 -8, /* Fixed RA offset. */
1906 &err);
1907
1908 /* FRE type is dependent on the size of the function. */
1909 fre_type = sframe_calc_fre_type (dpltsec->size);
1910 func_info = sframe_fde_create_func_info (fre_type, SFRAME_FDE_TYPE_PCINC);
1911
1912 /* Add SFrame FDE and the associated FREs for plt0 if plt0 has been
1913 generated. */
1914 if (plt0_entry_size)
1915 {
1916 /* Add SFrame FDE for plt0, the function start address is updated later
1917 at _bfd_elf_merge_section_sframe time. */
1918 sframe_encoder_add_funcdesc_v2 (*ectx,
1919 0, /* func start addr. */
1920 plt0_entry_size,
1921 func_info,
1922 16,
1923 0 /* Num FREs. */);
1924 sframe_frame_row_entry plt0_fre;
1925 unsigned int num_plt0_fres = htab->sframe_plt->plt0_num_fres;
1926 for (unsigned int j = 0; j < num_plt0_fres; j++)
1927 {
1928 plt0_fre = *(htab->sframe_plt->plt0_fres[j]);
1929 sframe_encoder_add_fre (*ectx, 0, &plt0_fre);
1930 }
1931 }
1932
1933
1934 if (num_pltn_entries)
1935 {
1936 /* pltn entries use an SFrame FDE of type
1937 SFRAME_FDE_TYPE_PCMASK to exploit the repetitive
1938 pattern of the instructions in these entries. Using this SFrame FDE
1939 type helps in keeping the SFrame stack trace info for pltn entries
1940 compact. */
1941 func_info = sframe_fde_create_func_info (fre_type,
1942 SFRAME_FDE_TYPE_PCMASK);
1943 /* Add the SFrame FDE for all PCs starting at the first pltn entry (hence,
1944 function start address = plt0_entry_size. As usual, this will be
1945 updated later at _bfd_elf_merge_section_sframe, by when the
1946 sections are relocated. */
1947 sframe_encoder_add_funcdesc_v2 (*ectx,
1948 plt0_entry_size, /* func start addr. */
1949 dpltsec->size - plt0_entry_size,
1950 func_info,
1951 plt_entry_size,
1952 0 /* Num FREs. */);
1953
1954 sframe_frame_row_entry pltn_fre;
1955 /* Now add the FREs for pltn. Simply adding the FREs suffices due
1956 to the usage of SFRAME_FDE_TYPE_PCMASK above. */
1957 for (unsigned int j = 0; j < num_pltn_fres; j++)
1958 {
1959 unsigned int func_idx = plt0_entry_size ? 1 : 0;
1960 pltn_fre = *(pltn_fres[j]);
1961 sframe_encoder_add_fre (*ectx, func_idx, &pltn_fre);
1962 }
1963 }
1964
1965 return true;
1966 }
1967
1968 /* Put contents of the .sframe section corresponding to the specified
1969 PLT_SEC_TYPE. */
1970
1971 static bool
1972 _bfd_x86_elf_write_sframe_plt (bfd *output_bfd,
1973 struct bfd_link_info *info,
1974 unsigned int plt_sec_type)
1975 {
1976 struct elf_x86_link_hash_table *htab;
1977 const struct elf_backend_data *bed;
1978 sframe_encoder_ctx *ectx;
1979 size_t sec_size;
1980 asection *sec;
1981 bfd *dynobj;
1982
1983 int err = 0;
1984
1985 bed = get_elf_backend_data (output_bfd);
1986 htab = elf_x86_hash_table (info, bed->target_id);
1987 dynobj = htab->elf.dynobj;
1988
1989 switch (plt_sec_type)
1990 {
1991 case SFRAME_PLT:
1992 ectx = htab->plt_cfe_ctx;
1993 sec = htab->plt_sframe;
1994 break;
1995 case SFRAME_PLT_SEC:
1996 ectx = htab->plt_second_cfe_ctx;
1997 sec = htab->plt_second_sframe;
1998 break;
1999 case SFRAME_PLT_GOT:
2000 ectx = htab->plt_got_cfe_ctx;
2001 sec = htab->plt_got_sframe;
2002 break;
2003 default:
2004 /* No other value is possible. */
2005 return false;
2006 break;
2007 }
2008
2009 BFD_ASSERT (ectx);
2010
2011 void *contents = sframe_encoder_write (ectx, &sec_size, &err);
2012
2013 sec->size = (bfd_size_type) sec_size;
2014 sec->contents = (unsigned char *) bfd_zalloc (dynobj, sec->size);
2015 memcpy (sec->contents, contents, sec_size);
2016
2017 sframe_encoder_free (&ectx);
2018
2019 return true;
2020 }
2021
2022 bool
2023 _bfd_elf_x86_size_relative_relocs (struct bfd_link_info *info,
2024 bool *need_layout)
2025 {
2026 struct elf_x86_link_hash_table *htab;
2027 const struct elf_backend_data *bed;
2028 bool is_x86_64;
2029 bfd_size_type i, count, unaligned_count;
2030 asection *sec, *srel;
2031
2032 /* Do nothing for ld -r. */
2033 if (bfd_link_relocatable (info))
2034 return true;
2035
2036 bed = get_elf_backend_data (info->output_bfd);
2037 htab = elf_x86_hash_table (info, bed->target_id);
2038 if (htab == NULL)
2039 return false;
2040
2041 count = htab->relative_reloc.count;
2042 unaligned_count = htab->unaligned_relative_reloc.count;
2043 if (count == 0)
2044 {
2045 if (htab->generate_relative_reloc_pass == 0
2046 && htab->elf.srelrdyn != NULL)
2047 {
2048 /* Remove the empty .relr.dyn sections now. */
2049 if (!bfd_is_abs_section (htab->elf.srelrdyn->output_section))
2050 {
2051 bfd_section_list_remove
2052 (info->output_bfd, htab->elf.srelrdyn->output_section);
2053 info->output_bfd->section_count--;
2054 }
2055 bfd_section_list_remove (htab->elf.srelrdyn->owner,
2056 htab->elf.srelrdyn);
2057 htab->elf.srelrdyn->owner->section_count--;
2058 }
2059 if (unaligned_count == 0)
2060 {
2061 htab->generate_relative_reloc_pass++;
2062 return true;
2063 }
2064 }
2065
2066 is_x86_64 = bed->target_id == X86_64_ELF_DATA;
2067
2068 /* Size relative relocations. */
2069 if (htab->generate_relative_reloc_pass)
2070 {
2071 /* Reset the regular relative relocation count. */
2072 for (i = 0; i < unaligned_count; i++)
2073 {
2074 sec = htab->unaligned_relative_reloc.data[i].sec;
2075 srel = elf_section_data (sec)->sreloc;
2076 srel->reloc_count = 0;
2077 }
2078 }
2079 else
2080 {
2081 /* Remove the reserved space for compact relative relocations. */
2082 if (count)
2083 {
2084 asection *sgot = htab->elf.sgot;
2085 asection *srelgot = htab->elf.srelgot;
2086
2087 for (i = 0; i < count; i++)
2088 {
2089 sec = htab->relative_reloc.data[i].sec;
2090 if (sec == sgot)
2091 srel = srelgot;
2092 else
2093 srel = elf_section_data (sec)->sreloc;
2094 srel->size -= htab->sizeof_reloc;
2095 }
2096 }
2097 }
2098
2099 /* Size unaligned relative relocations. */
2100 if (unaligned_count)
2101 elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2102 true, NULL);
2103
2104 if (count)
2105 {
2106 elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2107 false, NULL);
2108
2109 /* Sort relative relocations by addresses. We only need to
2110 sort them in the first pass since the relative positions
2111 won't change. */
2112 if (htab->generate_relative_reloc_pass == 0)
2113 qsort (htab->relative_reloc.data, count,
2114 sizeof (struct elf_x86_relative_reloc_record),
2115 elf_x86_relative_reloc_compare);
2116
2117 elf_x86_compute_dl_relr_bitmap (info, htab, need_layout);
2118 }
2119
2120 htab->generate_relative_reloc_pass++;
2121
2122 return true;
2123 }
2124
2125 bool
2126 _bfd_elf_x86_finish_relative_relocs (struct bfd_link_info *info)
2127 {
2128 struct elf_x86_link_hash_table *htab;
2129 const struct elf_backend_data *bed;
2130 Elf_Internal_Rela outrel;
2131 bool is_x86_64;
2132 bfd_size_type count;
2133
2134 /* Do nothing for ld -r. */
2135 if (bfd_link_relocatable (info))
2136 return true;
2137
2138 bed = get_elf_backend_data (info->output_bfd);
2139 htab = elf_x86_hash_table (info, bed->target_id);
2140 if (htab == NULL)
2141 return false;
2142
2143 is_x86_64 = bed->target_id == X86_64_ELF_DATA;
2144
2145 outrel.r_info = htab->r_info (0, htab->relative_r_type);
2146
2147 if (htab->unaligned_relative_reloc.count)
2148 elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2149 true, &outrel);
2150
2151 count = htab->relative_reloc.count;
2152 if (count)
2153 {
2154 elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2155 false, &outrel);
2156
2157 elf_x86_compute_dl_relr_bitmap (info, htab, NULL);
2158
2159 elf_x86_write_dl_relr_bitmap (info, htab);
2160 }
2161
2162 return true;
2163 }
2164
2165 bool
2166 _bfd_elf_x86_valid_reloc_p (asection *input_section,
2167 struct bfd_link_info *info,
2168 struct elf_x86_link_hash_table *htab,
2169 const Elf_Internal_Rela *rel,
2170 struct elf_link_hash_entry *h,
2171 Elf_Internal_Sym *sym,
2172 Elf_Internal_Shdr *symtab_hdr,
2173 bool *no_dynreloc_p)
2174 {
2175 bool valid_p = true;
2176
2177 *no_dynreloc_p = false;
2178
2179 /* Check If relocation against non-preemptible absolute symbol is
2180 valid in PIC. FIXME: Can't use SYMBOL_REFERENCES_LOCAL_P since
2181 it may call _bfd_elf_link_hide_sym_by_version and result in
2182 ld-elfvers/ vers21 test failure. */
2183 if (bfd_link_pic (info)
2184 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
2185 {
2186 const struct elf_backend_data *bed;
2187 unsigned int r_type;
2188 Elf_Internal_Rela irel;
2189
2190 /* Skip non-absolute symbol. */
2191 if (h)
2192 {
2193 if (!ABS_SYMBOL_P (h))
2194 return valid_p;
2195 }
2196 else if (sym->st_shndx != SHN_ABS)
2197 return valid_p;
2198
2199 bed = get_elf_backend_data (input_section->owner);
2200 r_type = ELF32_R_TYPE (rel->r_info);
2201 irel = *rel;
2202
2203 /* Only allow relocations against absolute symbol, which can be
2204 resolved as absolute value + addend. GOTPCREL and GOT32
2205 relocations are allowed since absolute value + addend is
2206 stored in the GOT slot. */
2207 if (bed->target_id == X86_64_ELF_DATA)
2208 {
2209 r_type &= ~R_X86_64_converted_reloc_bit;
2210 valid_p = (r_type == R_X86_64_64
2211 || r_type == R_X86_64_32
2212 || r_type == R_X86_64_32S
2213 || r_type == R_X86_64_16
2214 || r_type == R_X86_64_8
2215 || r_type == R_X86_64_GOTPCREL
2216 || r_type == R_X86_64_GOTPCRELX
2217 || r_type == R_X86_64_REX_GOTPCRELX);
2218 if (!valid_p)
2219 {
2220 unsigned int r_symndx = htab->r_sym (rel->r_info);
2221 irel.r_info = htab->r_info (r_symndx, r_type);
2222 }
2223 }
2224 else
2225 valid_p = (r_type == R_386_32
2226 || r_type == R_386_16
2227 || r_type == R_386_8
2228 || r_type == R_386_GOT32
2229 || r_type == R_386_GOT32X);
2230
2231 if (valid_p)
2232 *no_dynreloc_p = true;
2233 else
2234 {
2235 const char *name;
2236 arelent internal_reloc;
2237
2238 if (!bed->elf_info_to_howto (input_section->owner,
2239 &internal_reloc, &irel)
2240 || internal_reloc.howto == NULL)
2241 abort ();
2242
2243 if (h)
2244 name = h->root.root.string;
2245 else
2246 name = bfd_elf_sym_name (input_section->owner, symtab_hdr,
2247 sym, NULL);
2248 info->callbacks->einfo
2249 /* xgettext:c-format */
2250 (_("%F%P: %pB: relocation %s against absolute symbol "
2251 "`%s' in section `%pA' is disallowed\n"),
2252 input_section->owner, internal_reloc.howto->name, name,
2253 input_section);
2254 bfd_set_error (bfd_error_bad_value);
2255 }
2256 }
2257
2258 return valid_p;
2259 }
2260
2261 /* Set the sizes of the dynamic sections. */
2262
2263 bool
2264 _bfd_x86_elf_late_size_sections (bfd *output_bfd,
2265 struct bfd_link_info *info)
2266 {
2267 struct elf_x86_link_hash_table *htab;
2268 bfd *dynobj;
2269 asection *s;
2270 bool relocs;
2271 bfd *ibfd;
2272 const struct elf_backend_data *bed
2273 = get_elf_backend_data (output_bfd);
2274
2275 htab = elf_x86_hash_table (info, bed->target_id);
2276 if (htab == NULL)
2277 return false;
2278 dynobj = htab->elf.dynobj;
2279 if (dynobj == NULL)
2280 return true;
2281
2282 /* Set up .got offsets for local syms, and space for local dynamic
2283 relocs. */
2284 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2285 {
2286 bfd_signed_vma *local_got;
2287 bfd_signed_vma *end_local_got;
2288 char *local_tls_type;
2289 bfd_vma *local_tlsdesc_gotent;
2290 bfd_size_type locsymcount;
2291 Elf_Internal_Shdr *symtab_hdr;
2292 asection *srel;
2293
2294 if (! is_x86_elf (ibfd, htab))
2295 continue;
2296
2297 for (s = ibfd->sections; s != NULL; s = s->next)
2298 {
2299 struct elf_dyn_relocs *p;
2300
2301 for (p = ((struct elf_dyn_relocs *)
2302 elf_section_data (s)->local_dynrel);
2303 p != NULL;
2304 p = p->next)
2305 {
2306 if (!bfd_is_abs_section (p->sec)
2307 && bfd_is_abs_section (p->sec->output_section))
2308 {
2309 /* Input section has been discarded, either because
2310 it is a copy of a linkonce section or due to
2311 linker script /DISCARD/, so we'll be discarding
2312 the relocs too. */
2313 }
2314 else if (htab->elf.target_os == is_vxworks
2315 && strcmp (p->sec->output_section->name,
2316 ".tls_vars") == 0)
2317 {
2318 /* Relocations in vxworks .tls_vars sections are
2319 handled specially by the loader. */
2320 }
2321 else if (p->count != 0)
2322 {
2323 srel = elf_section_data (p->sec)->sreloc;
2324 srel->size += p->count * htab->sizeof_reloc;
2325 if ((p->sec->output_section->flags & SEC_READONLY) != 0
2326 && (info->flags & DF_TEXTREL) == 0)
2327 {
2328 info->flags |= DF_TEXTREL;
2329 if (bfd_link_textrel_check (info))
2330 /* xgettext:c-format */
2331 info->callbacks->einfo
2332 (_("%P: %pB: warning: relocation "
2333 "in read-only section `%pA'\n"),
2334 p->sec->owner, p->sec);
2335 }
2336 }
2337 }
2338 }
2339
2340 local_got = elf_local_got_refcounts (ibfd);
2341 if (!local_got)
2342 continue;
2343
2344 symtab_hdr = &elf_symtab_hdr (ibfd);
2345 locsymcount = symtab_hdr->sh_info;
2346 end_local_got = local_got + locsymcount;
2347 local_tls_type = elf_x86_local_got_tls_type (ibfd);
2348 local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
2349 s = htab->elf.sgot;
2350 srel = htab->elf.srelgot;
2351 for (; local_got < end_local_got;
2352 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2353 {
2354 *local_tlsdesc_gotent = (bfd_vma) -1;
2355 if (*local_got > 0)
2356 {
2357 if (GOT_TLS_GDESC_P (*local_tls_type))
2358 {
2359 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2360 - elf_x86_compute_jump_table_size (htab);
2361 htab->elf.sgotplt->size += 2 * htab->got_entry_size;
2362 *local_got = (bfd_vma) -2;
2363 }
2364 if (! GOT_TLS_GDESC_P (*local_tls_type)
2365 || GOT_TLS_GD_P (*local_tls_type))
2366 {
2367 *local_got = s->size;
2368 s->size += htab->got_entry_size;
2369 if (GOT_TLS_GD_P (*local_tls_type)
2370 || *local_tls_type == GOT_TLS_IE_BOTH)
2371 s->size += htab->got_entry_size;
2372 }
2373 if ((bfd_link_pic (info) && *local_tls_type != GOT_ABS)
2374 || GOT_TLS_GD_ANY_P (*local_tls_type)
2375 || (*local_tls_type & GOT_TLS_IE))
2376 {
2377 if (*local_tls_type == GOT_TLS_IE_BOTH)
2378 srel->size += 2 * htab->sizeof_reloc;
2379 else if (GOT_TLS_GD_P (*local_tls_type)
2380 || ! GOT_TLS_GDESC_P (*local_tls_type))
2381 srel->size += htab->sizeof_reloc;
2382 if (GOT_TLS_GDESC_P (*local_tls_type))
2383 {
2384 htab->elf.srelplt->size += htab->sizeof_reloc;
2385 if (bed->target_id == X86_64_ELF_DATA)
2386 htab->elf.tlsdesc_plt = (bfd_vma) -1;
2387 }
2388 }
2389 }
2390 else
2391 *local_got = (bfd_vma) -1;
2392 }
2393 }
2394
2395 if (htab->tls_ld_or_ldm_got.refcount > 0)
2396 {
2397 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2398 or R_X86_64_TLSLD relocs. */
2399 htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
2400 htab->elf.sgot->size += 2 * htab->got_entry_size;
2401 htab->elf.srelgot->size += htab->sizeof_reloc;
2402 }
2403 else
2404 htab->tls_ld_or_ldm_got.offset = -1;
2405
2406 /* Allocate global sym .plt and .got entries, and space for global
2407 sym dynamic relocs. */
2408 elf_link_hash_traverse (&htab->elf, elf_x86_allocate_dynrelocs,
2409 info);
2410
2411 /* Allocate .plt and .got entries, and space for local symbols. */
2412 htab_traverse (htab->loc_hash_table, elf_x86_allocate_local_dynreloc,
2413 info);
2414
2415 /* For every jump slot reserved in the sgotplt, reloc_count is
2416 incremented. However, when we reserve space for TLS descriptors,
2417 it's not incremented, so in order to compute the space reserved
2418 for them, it suffices to multiply the reloc count by the jump
2419 slot size.
2420
2421 PR ld/13302: We start next_irelative_index at the end of .rela.plt
2422 so that R_{386,X86_64}_IRELATIVE entries come last. */
2423 if (htab->elf.srelplt)
2424 {
2425 htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
2426 htab->sgotplt_jump_table_size
2427 = elf_x86_compute_jump_table_size (htab);
2428 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
2429 }
2430 else if (htab->elf.irelplt)
2431 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
2432
2433 if (htab->elf.tlsdesc_plt)
2434 {
2435 /* NB: tlsdesc_plt is set only for x86-64. If we're not using
2436 lazy TLS relocations, don't generate the PLT and GOT entries
2437 they require. */
2438 if ((info->flags & DF_BIND_NOW))
2439 htab->elf.tlsdesc_plt = 0;
2440 else
2441 {
2442 htab->elf.tlsdesc_got = htab->elf.sgot->size;
2443 htab->elf.sgot->size += htab->got_entry_size;
2444 /* Reserve room for the initial entry.
2445 FIXME: we could probably do away with it in this case. */
2446 if (htab->elf.splt->size == 0)
2447 htab->elf.splt->size = htab->plt.plt_entry_size;
2448 htab->elf.tlsdesc_plt = htab->elf.splt->size;
2449 htab->elf.splt->size += htab->plt.plt_entry_size;
2450 }
2451 }
2452
2453 if (htab->elf.sgotplt)
2454 {
2455 /* Don't allocate .got.plt section if there are no GOT nor PLT
2456 entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
2457 if ((htab->elf.hgot == NULL
2458 || !htab->got_referenced)
2459 && (htab->elf.sgotplt->size == bed->got_header_size)
2460 && (htab->elf.splt == NULL
2461 || htab->elf.splt->size == 0)
2462 && (htab->elf.sgot == NULL
2463 || htab->elf.sgot->size == 0)
2464 && (htab->elf.iplt == NULL
2465 || htab->elf.iplt->size == 0)
2466 && (htab->elf.igotplt == NULL
2467 || htab->elf.igotplt->size == 0))
2468 {
2469 htab->elf.sgotplt->size = 0;
2470 /* Solaris requires to keep _GLOBAL_OFFSET_TABLE_ even if it
2471 isn't used. */
2472 if (htab->elf.hgot != NULL
2473 && htab->elf.target_os != is_solaris)
2474 {
2475 /* Remove the unused _GLOBAL_OFFSET_TABLE_ from symbol
2476 table. */
2477 htab->elf.hgot->root.type = bfd_link_hash_undefined;
2478 htab->elf.hgot->root.u.undef.abfd
2479 = htab->elf.hgot->root.u.def.section->owner;
2480 htab->elf.hgot->root.linker_def = 0;
2481 htab->elf.hgot->ref_regular = 0;
2482 htab->elf.hgot->def_regular = 0;
2483 }
2484 }
2485 }
2486
2487 if (_bfd_elf_eh_frame_present (info))
2488 {
2489 if (htab->plt_eh_frame != NULL
2490 && htab->elf.splt != NULL
2491 && htab->elf.splt->size != 0
2492 && !bfd_is_abs_section (htab->elf.splt->output_section))
2493 htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
2494
2495 if (htab->plt_got_eh_frame != NULL
2496 && htab->plt_got != NULL
2497 && htab->plt_got->size != 0
2498 && !bfd_is_abs_section (htab->plt_got->output_section))
2499 htab->plt_got_eh_frame->size
2500 = htab->non_lazy_plt->eh_frame_plt_size;
2501
2502 /* Unwind info for the second PLT and .plt.got sections are
2503 identical. */
2504 if (htab->plt_second_eh_frame != NULL
2505 && htab->plt_second != NULL
2506 && htab->plt_second->size != 0
2507 && !bfd_is_abs_section (htab->plt_second->output_section))
2508 htab->plt_second_eh_frame->size
2509 = htab->non_lazy_plt->eh_frame_plt_size;
2510 }
2511
2512 /* No need to size the .sframe section explicitly because the write-out
2513 mechanism is different. Simply prep up the FDE/FRE for the
2514 .plt section. */
2515 if (_bfd_elf_sframe_present (info))
2516 {
2517 if (htab->plt_sframe != NULL
2518 && htab->elf.splt != NULL
2519 && htab->elf.splt->size != 0
2520 && !bfd_is_abs_section (htab->elf.splt->output_section))
2521 {
2522 _bfd_x86_elf_create_sframe_plt (output_bfd, info, SFRAME_PLT);
2523 /* FIXME - Dirty Hack. Set the size to something non-zero for now,
2524 so that the section does not get stripped out below. The precise
2525 size of this section is known only when the contents are
2526 serialized in _bfd_x86_elf_write_sframe_plt. */
2527 htab->plt_sframe->size = sizeof (sframe_header) + 1;
2528 }
2529
2530 if (htab->plt_got_sframe != NULL
2531 && htab->plt_got != NULL
2532 && htab->plt_got->size != 0
2533 && !bfd_is_abs_section (htab->plt_got->output_section))
2534 {
2535 _bfd_x86_elf_create_sframe_plt (output_bfd, info, SFRAME_PLT_GOT);
2536 /* FIXME - Dirty Hack. Set the size to something non-zero for now,
2537 so that the section does not get stripped out below. The precise
2538 size of this section is known only when the contents are
2539 serialized in _bfd_x86_elf_write_sframe_plt. */
2540 htab->plt_got_sframe->size = sizeof (sframe_header) + 1;
2541 }
2542
2543 if (htab->plt_second_sframe != NULL
2544 && htab->plt_second != NULL
2545 && htab->plt_second->size != 0
2546 && !bfd_is_abs_section (htab->plt_second->output_section))
2547 {
2548 /* SFrame stack trace info for the second PLT. */
2549 _bfd_x86_elf_create_sframe_plt (output_bfd, info, SFRAME_PLT_SEC);
2550 /* FIXME - Dirty Hack. Set the size to something non-zero for now,
2551 so that the section does not get stripped out below. The precise
2552 size of this section is known only when the contents are
2553 serialized in _bfd_x86_elf_write_sframe_plt. */
2554 htab->plt_second_sframe->size = sizeof (sframe_header) + 1;
2555 }
2556 }
2557
2558 asection *resolved_plt = NULL;
2559
2560 if (htab->params->mark_plt && htab->elf.dynamic_sections_created)
2561 {
2562 if (htab->plt_second != NULL)
2563 resolved_plt = htab->plt_second;
2564 else
2565 resolved_plt = htab->elf.splt;
2566
2567 if (resolved_plt != NULL && resolved_plt->size == 0)
2568 resolved_plt = NULL;
2569 }
2570
2571 /* We now have determined the sizes of the various dynamic sections.
2572 Allocate memory for them. */
2573 relocs = false;
2574 for (s = dynobj->sections; s != NULL; s = s->next)
2575 {
2576 bool strip_section = true;
2577
2578 if ((s->flags & SEC_LINKER_CREATED) == 0)
2579 continue;
2580
2581 /* The .relr.dyn section for compact relative relocation will
2582 be filled later. */
2583 if (s == htab->elf.srelrdyn)
2584 continue;
2585
2586 if (s == htab->elf.splt
2587 || s == htab->elf.sgot)
2588 {
2589 /* Strip this section if we don't need it; see the
2590 comment below. */
2591 /* We'd like to strip these sections if they aren't needed, but if
2592 we've exported dynamic symbols from them we must leave them.
2593 It's too late to tell BFD to get rid of the symbols. */
2594
2595 if (htab->elf.hplt != NULL)
2596 strip_section = false;
2597 }
2598 else if (s == htab->elf.sgotplt
2599 || s == htab->elf.iplt
2600 || s == htab->elf.igotplt
2601 || s == htab->plt_second
2602 || s == htab->plt_got
2603 || s == htab->plt_eh_frame
2604 || s == htab->plt_got_eh_frame
2605 || s == htab->plt_second_eh_frame
2606 || s == htab->plt_sframe
2607 || s == htab->plt_second_sframe
2608 || s == htab->plt_got_sframe
2609 || s == htab->elf.sdynbss
2610 || s == htab->elf.sdynrelro)
2611 {
2612 /* Strip these too. */
2613 }
2614 else if (htab->is_reloc_section (bfd_section_name (s)))
2615 {
2616 if (s->size != 0
2617 && s != htab->elf.srelplt
2618 && s != htab->srelplt2)
2619 relocs = true;
2620
2621 /* We use the reloc_count field as a counter if we need
2622 to copy relocs into the output file. */
2623 if (s != htab->elf.srelplt)
2624 s->reloc_count = 0;
2625 }
2626 else
2627 {
2628 /* It's not one of our sections, so don't allocate space. */
2629 continue;
2630 }
2631
2632 if (s->size == 0)
2633 {
2634 /* If we don't need this section, strip it from the
2635 output file. This is mostly to handle .rel.bss and
2636 .rel.plt. We must create both sections in
2637 create_dynamic_sections, because they must be created
2638 before the linker maps input sections to output
2639 sections. The linker does that before
2640 adjust_dynamic_symbol is called, and it is that
2641 function which decides whether anything needs to go
2642 into these sections. */
2643 if (strip_section)
2644 s->flags |= SEC_EXCLUDE;
2645 continue;
2646 }
2647
2648 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2649 continue;
2650
2651 /* Skip allocating contents for .sframe section as it is written
2652 out differently. See below. */
2653 if ((s == htab->plt_sframe) || (s == htab->plt_second_sframe)
2654 || (s == htab->plt_got_sframe))
2655 continue;
2656
2657 /* NB: Initially, the iplt section has minimal alignment to
2658 avoid moving dot of the following section backwards when
2659 it is empty. Update its section alignment now since it
2660 is non-empty. */
2661 if (s == htab->elf.iplt)
2662 bfd_set_section_alignment (s, htab->plt.iplt_alignment);
2663
2664 /* Allocate memory for the section contents. We use bfd_zalloc
2665 here in case unused entries are not reclaimed before the
2666 section's contents are written out. This should not happen,
2667 but this way if it does, we get a R_386_NONE or R_X86_64_NONE
2668 reloc instead of garbage. */
2669 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
2670 if (s->contents == NULL)
2671 return false;
2672 }
2673
2674 if (htab->plt_eh_frame != NULL
2675 && htab->plt_eh_frame->contents != NULL)
2676 {
2677 memcpy (htab->plt_eh_frame->contents,
2678 htab->plt.eh_frame_plt,
2679 htab->plt_eh_frame->size);
2680 bfd_put_32 (dynobj, htab->elf.splt->size,
2681 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
2682 }
2683
2684 if (htab->plt_got_eh_frame != NULL
2685 && htab->plt_got_eh_frame->contents != NULL)
2686 {
2687 memcpy (htab->plt_got_eh_frame->contents,
2688 htab->non_lazy_plt->eh_frame_plt,
2689 htab->plt_got_eh_frame->size);
2690 bfd_put_32 (dynobj, htab->plt_got->size,
2691 (htab->plt_got_eh_frame->contents
2692 + PLT_FDE_LEN_OFFSET));
2693 }
2694
2695 if (htab->plt_second_eh_frame != NULL
2696 && htab->plt_second_eh_frame->contents != NULL)
2697 {
2698 memcpy (htab->plt_second_eh_frame->contents,
2699 htab->non_lazy_plt->eh_frame_plt,
2700 htab->plt_second_eh_frame->size);
2701 bfd_put_32 (dynobj, htab->plt_second->size,
2702 (htab->plt_second_eh_frame->contents
2703 + PLT_FDE_LEN_OFFSET));
2704 }
2705
2706 if (_bfd_elf_sframe_present (info))
2707 {
2708 if (htab->plt_sframe != NULL
2709 && htab->elf.splt != NULL
2710 && htab->elf.splt->size != 0
2711 && htab->plt_sframe->contents == NULL)
2712 _bfd_x86_elf_write_sframe_plt (output_bfd, info, SFRAME_PLT);
2713
2714 if (htab->plt_second_sframe != NULL
2715 && htab->plt_second != NULL
2716 && htab->plt_second->size != 0
2717 && htab->plt_second_sframe->contents == NULL)
2718 _bfd_x86_elf_write_sframe_plt (output_bfd, info, SFRAME_PLT_SEC);
2719
2720 if (htab->plt_got_sframe != NULL
2721 && htab->plt_got != NULL
2722 && htab->plt_got->size != 0
2723 && htab->plt_got_sframe->contents == NULL)
2724 _bfd_x86_elf_write_sframe_plt (output_bfd, info, SFRAME_PLT_GOT);
2725 }
2726
2727 if (resolved_plt != NULL
2728 && (!_bfd_elf_add_dynamic_entry (info, DT_X86_64_PLT, 0)
2729 || !_bfd_elf_add_dynamic_entry (info, DT_X86_64_PLTSZ, 0)
2730 || !_bfd_elf_add_dynamic_entry (info, DT_X86_64_PLTENT, 0)))
2731 return false;
2732
2733 return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
2734 relocs);
2735 }
2736
2737 /* Finish up the x86 dynamic sections. */
2738
2739 struct elf_x86_link_hash_table *
2740 _bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd,
2741 struct bfd_link_info *info)
2742 {
2743 struct elf_x86_link_hash_table *htab;
2744 const struct elf_backend_data *bed;
2745 bfd *dynobj;
2746 asection *sdyn;
2747 bfd_byte *dyncon, *dynconend;
2748 bfd_size_type sizeof_dyn;
2749
2750 bed = get_elf_backend_data (output_bfd);
2751 htab = elf_x86_hash_table (info, bed->target_id);
2752 if (htab == NULL)
2753 return htab;
2754
2755 dynobj = htab->elf.dynobj;
2756 sdyn = htab->elf.dynamic;
2757
2758 /* GOT is always created in setup_gnu_properties. But it may not be
2759 needed. .got.plt section may be needed for static IFUNC. */
2760 if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
2761 {
2762 bfd_vma dynamic_addr;
2763
2764 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
2765 {
2766 _bfd_error_handler
2767 (_("discarded output section: `%pA'"), htab->elf.sgotplt);
2768 return NULL;
2769 }
2770
2771 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
2772 = htab->got_entry_size;
2773
2774 dynamic_addr = (sdyn == NULL
2775 ? (bfd_vma) 0
2776 : sdyn->output_section->vma + sdyn->output_offset);
2777
2778 /* Set the first entry in the global offset table to the address
2779 of the dynamic section. Write GOT[1] and GOT[2], needed for
2780 the dynamic linker. */
2781 if (htab->got_entry_size == 8)
2782 {
2783 bfd_put_64 (output_bfd, dynamic_addr,
2784 htab->elf.sgotplt->contents);
2785 bfd_put_64 (output_bfd, (bfd_vma) 0,
2786 htab->elf.sgotplt->contents + 8);
2787 bfd_put_64 (output_bfd, (bfd_vma) 0,
2788 htab->elf.sgotplt->contents + 8*2);
2789 }
2790 else
2791 {
2792 bfd_put_32 (output_bfd, dynamic_addr,
2793 htab->elf.sgotplt->contents);
2794 bfd_put_32 (output_bfd, 0,
2795 htab->elf.sgotplt->contents + 4);
2796 bfd_put_32 (output_bfd, 0,
2797 htab->elf.sgotplt->contents + 4*2);
2798 }
2799 }
2800
2801 if (!htab->elf.dynamic_sections_created)
2802 return htab;
2803
2804 if (sdyn == NULL || htab->elf.sgot == NULL)
2805 abort ();
2806
2807 asection *resolved_plt;
2808 if (htab->plt_second != NULL)
2809 resolved_plt = htab->plt_second;
2810 else
2811 resolved_plt = htab->elf.splt;
2812
2813 sizeof_dyn = bed->s->sizeof_dyn;
2814 dyncon = sdyn->contents;
2815 dynconend = sdyn->contents + sdyn->size;
2816 for (; dyncon < dynconend; dyncon += sizeof_dyn)
2817 {
2818 Elf_Internal_Dyn dyn;
2819 asection *s;
2820
2821 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
2822
2823 switch (dyn.d_tag)
2824 {
2825 default:
2826 if (htab->elf.target_os == is_vxworks
2827 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
2828 break;
2829 continue;
2830
2831 case DT_PLTGOT:
2832 s = htab->elf.sgotplt;
2833 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2834 break;
2835
2836 case DT_JMPREL:
2837 s = htab->elf.srelplt;
2838 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2839 break;
2840
2841 case DT_PLTRELSZ:
2842 s = htab->elf.srelplt;
2843 dyn.d_un.d_val = s->size;
2844 break;
2845
2846 case DT_TLSDESC_PLT:
2847 s = htab->elf.splt;
2848 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
2849 + htab->elf.tlsdesc_plt;
2850 break;
2851
2852 case DT_TLSDESC_GOT:
2853 s = htab->elf.sgot;
2854 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
2855 + htab->elf.tlsdesc_got;
2856 break;
2857
2858 case DT_X86_64_PLT:
2859 s = resolved_plt->output_section;
2860 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2861 break;
2862
2863 case DT_X86_64_PLTSZ:
2864 dyn.d_un.d_val = resolved_plt->size;
2865 break;
2866
2867 case DT_X86_64_PLTENT:
2868 dyn.d_un.d_ptr = htab->plt.plt_entry_size;
2869 break;
2870 }
2871
2872 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
2873 }
2874
2875 if (htab->plt_got != NULL && htab->plt_got->size > 0)
2876 elf_section_data (htab->plt_got->output_section)
2877 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
2878
2879 if (htab->plt_second != NULL && htab->plt_second->size > 0)
2880 elf_section_data (htab->plt_second->output_section)
2881 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
2882
2883 /* Adjust .eh_frame for .plt section. */
2884 if (htab->plt_eh_frame != NULL
2885 && htab->plt_eh_frame->contents != NULL)
2886 {
2887 if (htab->elf.splt != NULL
2888 && htab->elf.splt->size != 0
2889 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
2890 && htab->elf.splt->output_section != NULL
2891 && htab->plt_eh_frame->output_section != NULL)
2892 {
2893 bfd_vma plt_start = htab->elf.splt->output_section->vma;
2894 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
2895 + htab->plt_eh_frame->output_offset
2896 + PLT_FDE_START_OFFSET;
2897 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2898 htab->plt_eh_frame->contents
2899 + PLT_FDE_START_OFFSET);
2900 }
2901
2902 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
2903 {
2904 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2905 htab->plt_eh_frame,
2906 htab->plt_eh_frame->contents))
2907 return NULL;
2908 }
2909 }
2910
2911 /* Adjust .eh_frame for .plt.got section. */
2912 if (htab->plt_got_eh_frame != NULL
2913 && htab->plt_got_eh_frame->contents != NULL)
2914 {
2915 if (htab->plt_got != NULL
2916 && htab->plt_got->size != 0
2917 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
2918 && htab->plt_got->output_section != NULL
2919 && htab->plt_got_eh_frame->output_section != NULL)
2920 {
2921 bfd_vma plt_start = htab->plt_got->output_section->vma;
2922 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
2923 + htab->plt_got_eh_frame->output_offset
2924 + PLT_FDE_START_OFFSET;
2925 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2926 htab->plt_got_eh_frame->contents
2927 + PLT_FDE_START_OFFSET);
2928 }
2929 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
2930 {
2931 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2932 htab->plt_got_eh_frame,
2933 htab->plt_got_eh_frame->contents))
2934 return NULL;
2935 }
2936 }
2937
2938 /* Adjust .eh_frame for the second PLT section. */
2939 if (htab->plt_second_eh_frame != NULL
2940 && htab->plt_second_eh_frame->contents != NULL)
2941 {
2942 if (htab->plt_second != NULL
2943 && htab->plt_second->size != 0
2944 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
2945 && htab->plt_second->output_section != NULL
2946 && htab->plt_second_eh_frame->output_section != NULL)
2947 {
2948 bfd_vma plt_start = htab->plt_second->output_section->vma;
2949 bfd_vma eh_frame_start
2950 = (htab->plt_second_eh_frame->output_section->vma
2951 + htab->plt_second_eh_frame->output_offset
2952 + PLT_FDE_START_OFFSET);
2953 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2954 htab->plt_second_eh_frame->contents
2955 + PLT_FDE_START_OFFSET);
2956 }
2957 if (htab->plt_second_eh_frame->sec_info_type
2958 == SEC_INFO_TYPE_EH_FRAME)
2959 {
2960 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2961 htab->plt_second_eh_frame,
2962 htab->plt_second_eh_frame->contents))
2963 return NULL;
2964 }
2965 }
2966
2967 /* Make any adjustment if necessary and merge .sframe section to
2968 create the final .sframe section for output_bfd. */
2969 if (htab->plt_sframe != NULL
2970 && htab->plt_sframe->contents != NULL)
2971 {
2972 if (htab->elf.splt != NULL
2973 && htab->elf.splt->size != 0
2974 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
2975 && htab->elf.splt->output_section != NULL
2976 && htab->plt_sframe->output_section != NULL)
2977 {
2978 bfd_vma plt_start = htab->elf.splt->output_section->vma;
2979 bfd_vma sframe_start = htab->plt_sframe->output_section->vma
2980 + htab->plt_sframe->output_offset
2981 + PLT_SFRAME_FDE_START_OFFSET;
2982 #if 0 /* FIXME Testing only. Remove before review. */
2983 bfd_vma test_value = (plt_start - sframe_start)
2984 + htab->plt_sframe->output_section->vma
2985 + htab->plt_sframe->output_offset
2986 + PLT_SFRAME_FDE_START_OFFSET;
2987 bfd_put_signed_32 (dynobj, test_value,
2988 #endif
2989 bfd_put_signed_32 (dynobj, plt_start - sframe_start,
2990 htab->plt_sframe->contents
2991 + PLT_SFRAME_FDE_START_OFFSET);
2992 }
2993 if (htab->plt_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
2994 {
2995 if (! _bfd_elf_merge_section_sframe (output_bfd, info,
2996 htab->plt_sframe,
2997 htab->plt_sframe->contents))
2998 return NULL;
2999 }
3000 }
3001
3002 if (htab->plt_second_sframe != NULL
3003 && htab->plt_second_sframe->contents != NULL)
3004 {
3005 if (htab->plt_second != NULL
3006 && htab->plt_second->size != 0
3007 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
3008 && htab->plt_second->output_section != NULL
3009 && htab->plt_second_sframe->output_section != NULL)
3010 {
3011 bfd_vma plt_start = htab->plt_second->output_section->vma;
3012 bfd_vma sframe_start
3013 = (htab->plt_second_sframe->output_section->vma
3014 + htab->plt_second_sframe->output_offset
3015 + PLT_SFRAME_FDE_START_OFFSET);
3016 #if 0 /* FIXME Testing only. Remove before review. */
3017 bfd_vma test_value = (plt_start - sframe_start)
3018 + htab->plt_second_sframe->output_section->vma
3019 + htab->plt_second_sframe->output_offset
3020 + PLT_SFRAME_FDE_START_OFFSET;
3021 bfd_put_signed_32 (dynobj, test_value,
3022 #endif
3023 bfd_put_signed_32 (dynobj, plt_start - sframe_start,
3024 htab->plt_second_sframe->contents
3025 + PLT_SFRAME_FDE_START_OFFSET);
3026 }
3027 if (htab->plt_second_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
3028 {
3029 if (! _bfd_elf_merge_section_sframe (output_bfd, info,
3030 htab->plt_second_sframe,
3031 htab->plt_second_sframe->contents))
3032 return NULL;
3033 }
3034 }
3035
3036 if (htab->plt_got_sframe != NULL
3037 && htab->plt_got_sframe->contents != NULL)
3038 {
3039 if (htab->plt_got != NULL
3040 && htab->plt_got->size != 0
3041 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
3042 && htab->plt_got->output_section != NULL
3043 && htab->plt_got_sframe->output_section != NULL)
3044 {
3045 bfd_vma plt_start = htab->plt_got->output_section->vma;
3046 bfd_vma sframe_start
3047 = (htab->plt_got_sframe->output_section->vma
3048 + htab->plt_got_sframe->output_offset
3049 + PLT_SFRAME_FDE_START_OFFSET);
3050 bfd_put_signed_32 (dynobj, plt_start - sframe_start,
3051 htab->plt_got_sframe->contents
3052 + PLT_SFRAME_FDE_START_OFFSET);
3053 }
3054 if (htab->plt_got_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
3055 {
3056 if (! _bfd_elf_merge_section_sframe (output_bfd, info,
3057 htab->plt_got_sframe,
3058 htab->plt_got_sframe->contents))
3059 return NULL;
3060 }
3061 }
3062
3063 if (htab->elf.sgot && htab->elf.sgot->size > 0)
3064 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
3065 = htab->got_entry_size;
3066
3067 return htab;
3068 }
3069
3070
3071 bool
3072 _bfd_x86_elf_early_size_sections (bfd *output_bfd,
3073 struct bfd_link_info *info)
3074 {
3075 asection *tls_sec = elf_hash_table (info)->tls_sec;
3076
3077 if (tls_sec && !bfd_link_relocatable (info))
3078 {
3079 struct elf_link_hash_entry *tlsbase;
3080
3081 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3082 "_TLS_MODULE_BASE_",
3083 false, false, false);
3084
3085 if (tlsbase && tlsbase->type == STT_TLS)
3086 {
3087 struct elf_x86_link_hash_table *htab;
3088 struct bfd_link_hash_entry *bh = NULL;
3089 const struct elf_backend_data *bed
3090 = get_elf_backend_data (output_bfd);
3091
3092 htab = elf_x86_hash_table (info, bed->target_id);
3093 if (htab == NULL)
3094 return false;
3095
3096 if (!(_bfd_generic_link_add_one_symbol
3097 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3098 tls_sec, 0, NULL, false,
3099 bed->collect, &bh)))
3100 return false;
3101
3102 htab->tls_module_base = bh;
3103
3104 tlsbase = (struct elf_link_hash_entry *)bh;
3105 tlsbase->def_regular = 1;
3106 tlsbase->other = STV_HIDDEN;
3107 tlsbase->root.linker_def = 1;
3108 (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
3109 }
3110 }
3111
3112 return true;
3113 }
3114
3115 void
3116 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3117 unsigned int st_other,
3118 bool definition,
3119 bool dynamic ATTRIBUTE_UNUSED)
3120 {
3121 if (definition)
3122 {
3123 struct elf_x86_link_hash_entry *eh
3124 = (struct elf_x86_link_hash_entry *) h;
3125 eh->def_protected = ELF_ST_VISIBILITY (st_other) == STV_PROTECTED;
3126 }
3127 }
3128
3129 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3130
3131 void
3132 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
3133 struct elf_link_hash_entry *dir,
3134 struct elf_link_hash_entry *ind)
3135 {
3136 struct elf_x86_link_hash_entry *edir, *eind;
3137
3138 edir = (struct elf_x86_link_hash_entry *) dir;
3139 eind = (struct elf_x86_link_hash_entry *) ind;
3140
3141 if (ind->root.type == bfd_link_hash_indirect
3142 && dir->got.refcount <= 0)
3143 {
3144 edir->tls_type = eind->tls_type;
3145 eind->tls_type = GOT_UNKNOWN;
3146 }
3147
3148 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
3149 generate a R_386_COPY reloc. */
3150 edir->gotoff_ref |= eind->gotoff_ref;
3151
3152 edir->zero_undefweak |= eind->zero_undefweak;
3153
3154 if (ELIMINATE_COPY_RELOCS
3155 && ind->root.type != bfd_link_hash_indirect
3156 && dir->dynamic_adjusted)
3157 {
3158 /* If called to transfer flags for a weakdef during processing
3159 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
3160 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3161 if (dir->versioned != versioned_hidden)
3162 dir->ref_dynamic |= ind->ref_dynamic;
3163 dir->ref_regular |= ind->ref_regular;
3164 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
3165 dir->needs_plt |= ind->needs_plt;
3166 dir->pointer_equality_needed |= ind->pointer_equality_needed;
3167 }
3168 else
3169 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3170 }
3171
3172 /* Remove undefined weak symbol from the dynamic symbol table if it
3173 is resolved to 0. */
3174
3175 bool
3176 _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
3177 struct elf_link_hash_entry *h)
3178 {
3179 if (h->dynindx != -1
3180 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, elf_x86_hash_entry (h)))
3181 {
3182 h->dynindx = -1;
3183 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3184 h->dynstr_index);
3185 }
3186 return true;
3187 }
3188
3189 /* Change the STT_GNU_IFUNC symbol defined in position-dependent
3190 executable into the normal function symbol and set its address
3191 to its PLT entry, which should be resolved by R_*_IRELATIVE at
3192 run-time. */
3193
3194 void
3195 _bfd_x86_elf_link_fixup_ifunc_symbol (struct bfd_link_info *info,
3196 struct elf_x86_link_hash_table *htab,
3197 struct elf_link_hash_entry *h,
3198 Elf_Internal_Sym *sym)
3199 {
3200 if (bfd_link_pde (info)
3201 && h->def_regular
3202 && h->dynindx != -1
3203 && h->plt.offset != (bfd_vma) -1
3204 && h->type == STT_GNU_IFUNC)
3205 {
3206 asection *plt_s;
3207 bfd_vma plt_offset;
3208 bfd *output_bfd = info->output_bfd;
3209
3210 if (htab->plt_second)
3211 {
3212 struct elf_x86_link_hash_entry *eh
3213 = (struct elf_x86_link_hash_entry *) h;
3214
3215 plt_s = htab->plt_second;
3216 plt_offset = eh->plt_second.offset;
3217 }
3218 else
3219 {
3220 plt_s = htab->elf.splt;
3221 plt_offset = h->plt.offset;
3222 }
3223
3224 sym->st_size = 0;
3225 sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
3226 sym->st_shndx
3227 = _bfd_elf_section_from_bfd_section (output_bfd,
3228 plt_s->output_section);
3229 sym->st_value = (plt_s->output_section->vma
3230 + plt_s->output_offset + plt_offset);
3231 }
3232 }
3233
3234 /* Report relative relocation. */
3235
3236 void
3237 _bfd_x86_elf_link_report_relative_reloc
3238 (struct bfd_link_info *info, asection *asect,
3239 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym,
3240 const char *reloc_name, const void *reloc)
3241 {
3242 const char *name;
3243 bfd *abfd;
3244 const Elf_Internal_Rela *rel = (const Elf_Internal_Rela *) reloc;
3245
3246 /* Use the output BFD for linker created sections. */
3247 if ((asect->flags & SEC_LINKER_CREATED) != 0)
3248 abfd = info->output_bfd;
3249 else
3250 abfd = asect->owner;
3251
3252 if (h != NULL && h->root.root.string != NULL)
3253 name = h->root.root.string;
3254 else
3255 name = bfd_elf_sym_name (abfd, &elf_symtab_hdr (abfd), sym, NULL);
3256
3257 if (asect->use_rela_p)
3258 info->callbacks->einfo
3259 (_("%pB: %s (offset: 0x%v, info: 0x%v, addend: 0x%v) against "
3260 "'%s' " "for section '%pA' in %pB\n"),
3261 info->output_bfd, reloc_name, rel->r_offset, rel->r_info,
3262 rel->r_addend, name, asect, abfd);
3263 else
3264 info->callbacks->einfo
3265 (_("%pB: %s (offset: 0x%v, info: 0x%v) against '%s' for section "
3266 "'%pA' in %pB\n"),
3267 info->output_bfd, reloc_name, rel->r_offset, rel->r_info, name,
3268 asect, abfd);
3269 }
3270
3271 /* Report TLS transition error. */
3272
3273 void
3274 _bfd_x86_elf_link_report_tls_transition_error
3275 (struct bfd_link_info *info, bfd *abfd, asection *asect,
3276 Elf_Internal_Shdr *symtab_hdr, struct elf_link_hash_entry *h,
3277 Elf_Internal_Sym *sym, const Elf_Internal_Rela *rel,
3278 const char *from_reloc_name, const char *to_reloc_name,
3279 enum elf_x86_tls_error_type tls_error)
3280 {
3281 const char *name;
3282 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3283 struct elf_x86_link_hash_table *htab
3284 = elf_x86_hash_table (info, bed->target_id);
3285
3286 if (h)
3287 name = h->root.root.string;
3288 else
3289 {
3290 if (htab == NULL)
3291 name = "*unknown*";
3292 else
3293 name = bfd_elf_sym_name (abfd, symtab_hdr, sym, NULL);
3294 }
3295
3296 switch (tls_error)
3297 {
3298 case elf_x86_tls_error_yes:
3299 info->callbacks->einfo
3300 /* xgettext:c-format */
3301 (_("%pB: TLS transition from %s to %s against `%s' at 0x%v in "
3302 "section `%pA' failed\n"),
3303 abfd, from_reloc_name, to_reloc_name, name, rel->r_offset,
3304 asect);
3305 break;
3306
3307 case elf_x86_tls_error_add:
3308 info->callbacks->einfo
3309 /* xgettext:c-format */
3310 (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3311 "in ADD only\n"),
3312 abfd, asect, rel->r_offset, from_reloc_name, name);
3313 break;
3314
3315 case elf_x86_tls_error_add_mov:
3316 info->callbacks->einfo
3317 /* xgettext:c-format */
3318 (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3319 "in ADD or MOV only\n"),
3320 abfd, asect, rel->r_offset, from_reloc_name, name);
3321 break;
3322
3323 case elf_x86_tls_error_add_sub_mov:
3324 info->callbacks->einfo
3325 /* xgettext:c-format */
3326 (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3327 "in ADD, SUB or MOV only\n"),
3328 abfd, asect, rel->r_offset, from_reloc_name, name);
3329 break;
3330
3331 case elf_x86_tls_error_indirect_call:
3332 info->callbacks->einfo
3333 /* xgettext:c-format */
3334 (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3335 "in indirect CALL with %s register only\n"),
3336 abfd, asect, rel->r_offset, from_reloc_name, name,
3337 htab->ax_register);
3338 break;
3339
3340 case elf_x86_tls_error_lea:
3341 info->callbacks->einfo
3342 /* xgettext:c-format */
3343 (_("%pB(%pA+0x%v): relocation %s against `%s' must be used "
3344 "in LEA only\n"),
3345 abfd, asect, rel->r_offset, from_reloc_name, name);
3346 break;
3347
3348 default:
3349 abort ();
3350 break;
3351 }
3352
3353 bfd_set_error (bfd_error_bad_value);
3354 }
3355
3356 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3357
3358 bool
3359 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
3360 {
3361 if (h->plt.offset != (bfd_vma) -1
3362 && !h->def_regular
3363 && !h->pointer_equality_needed)
3364 return false;
3365
3366 return _bfd_elf_hash_symbol (h);
3367 }
3368
3369 /* Adjust a symbol defined by a dynamic object and referenced by a
3370 regular object. The current definition is in some section of the
3371 dynamic object, but we're not including those sections. We have to
3372 change the definition to something the rest of the link can
3373 understand. */
3374
3375 bool
3376 _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3377 struct elf_link_hash_entry *h)
3378 {
3379 struct elf_x86_link_hash_table *htab;
3380 asection *s, *srel;
3381 struct elf_x86_link_hash_entry *eh;
3382 struct elf_dyn_relocs *p;
3383 const struct elf_backend_data *bed
3384 = get_elf_backend_data (info->output_bfd);
3385
3386 eh = (struct elf_x86_link_hash_entry *) h;
3387
3388 /* Clear GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS if it is turned
3389 on by an input relocatable file and there is a non-GOT/non-PLT
3390 reference from another relocatable file without it.
3391 NB: There can be non-GOT reference in data sections in input with
3392 GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS. */
3393 if (eh->non_got_ref_without_indirect_extern_access
3394 && info->indirect_extern_access == 1
3395 && bfd_link_executable (info))
3396 {
3397 unsigned int needed_1;
3398 info->indirect_extern_access = 0;
3399 /* Turn off nocopyreloc if implied by indirect_extern_access. */
3400 if (info->nocopyreloc == 2)
3401 info->nocopyreloc = 0;
3402 needed_1 = bfd_h_get_32 (info->output_bfd, info->needed_1_p);
3403 needed_1 &= ~GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
3404 bfd_h_put_32 (info->output_bfd, needed_1, info->needed_1_p);
3405 }
3406
3407 /* STT_GNU_IFUNC symbol must go through PLT. */
3408 if (h->type == STT_GNU_IFUNC)
3409 {
3410 /* All local STT_GNU_IFUNC references must be treate as local
3411 calls via local PLT. */
3412 if (h->ref_regular
3413 && SYMBOL_CALLS_LOCAL (info, h))
3414 {
3415 bfd_size_type pc_count = 0, count = 0;
3416 struct elf_dyn_relocs **pp;
3417
3418 eh = (struct elf_x86_link_hash_entry *) h;
3419 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
3420 {
3421 pc_count += p->pc_count;
3422 p->count -= p->pc_count;
3423 p->pc_count = 0;
3424 count += p->count;
3425 if (p->count == 0)
3426 *pp = p->next;
3427 else
3428 pp = &p->next;
3429 }
3430
3431 if (pc_count || count)
3432 {
3433 h->non_got_ref = 1;
3434 if (pc_count)
3435 {
3436 /* Increment PLT reference count only for PC-relative
3437 references. */
3438 h->needs_plt = 1;
3439 if (h->plt.refcount <= 0)
3440 h->plt.refcount = 1;
3441 else
3442 h->plt.refcount += 1;
3443 }
3444 }
3445
3446 /* GOTOFF relocation needs PLT. */
3447 if (eh->gotoff_ref)
3448 h->plt.refcount = 1;
3449 }
3450
3451 if (h->plt.refcount <= 0)
3452 {
3453 h->plt.offset = (bfd_vma) -1;
3454 h->needs_plt = 0;
3455 }
3456 return true;
3457 }
3458
3459 /* If this is a function, put it in the procedure linkage table. We
3460 will fill in the contents of the procedure linkage table later,
3461 when we know the address of the .got section. */
3462 if (h->type == STT_FUNC
3463 || h->needs_plt)
3464 {
3465 if (h->plt.refcount <= 0
3466 || SYMBOL_CALLS_LOCAL (info, h)
3467 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3468 && h->root.type == bfd_link_hash_undefweak))
3469 {
3470 /* This case can occur if we saw a PLT32 reloc in an input
3471 file, but the symbol was never referred to by a dynamic
3472 object, or if all references were garbage collected. In
3473 such a case, we don't actually need to build a procedure
3474 linkage table, and we can just do a PC32 reloc instead. */
3475 h->plt.offset = (bfd_vma) -1;
3476 h->needs_plt = 0;
3477 }
3478
3479 return true;
3480 }
3481 else
3482 /* It's possible that we incorrectly decided a .plt reloc was needed
3483 * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
3484 check_relocs. We can't decide accurately between function and
3485 non-function syms in check-relocs; Objects loaded later in
3486 the link may change h->type. So fix it now. */
3487 h->plt.offset = (bfd_vma) -1;
3488
3489 /* If this is a weak symbol, and there is a real definition, the
3490 processor independent code will have arranged for us to see the
3491 real definition first, and we can just use the same value. */
3492 if (h->is_weakalias)
3493 {
3494 struct elf_link_hash_entry *def = weakdef (h);
3495 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3496 h->root.u.def.section = def->root.u.def.section;
3497 h->root.u.def.value = def->root.u.def.value;
3498 if (ELIMINATE_COPY_RELOCS
3499 || info->nocopyreloc
3500 || SYMBOL_NO_COPYRELOC (info, eh))
3501 {
3502 /* NB: needs_copy is always 0 for i386. */
3503 h->non_got_ref = def->non_got_ref;
3504 eh->needs_copy = def->needs_copy;
3505 }
3506 return true;
3507 }
3508
3509 /* This is a reference to a symbol defined by a dynamic object which
3510 is not a function. */
3511
3512 /* If we are creating a shared library, we must presume that the
3513 only references to the symbol are via the global offset table.
3514 For such cases we need not do anything here; the relocations will
3515 be handled correctly by relocate_section. */
3516 if (!bfd_link_executable (info))
3517 return true;
3518
3519 /* If there are no references to this symbol that do not use the
3520 GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
3521 reloc. NB: gotoff_ref is always 0 for x86-64. */
3522 if (!h->non_got_ref && !eh->gotoff_ref)
3523 return true;
3524
3525 /* If -z nocopyreloc was given, we won't generate them either. */
3526 if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
3527 {
3528 h->non_got_ref = 0;
3529 return true;
3530 }
3531
3532 htab = elf_x86_hash_table (info, bed->target_id);
3533 if (htab == NULL)
3534 return false;
3535
3536 /* If there aren't any dynamic relocs in read-only sections nor
3537 R_386_GOTOFF relocation, then we can keep the dynamic relocs and
3538 avoid the copy reloc. This doesn't work on VxWorks, where we can
3539 not have dynamic relocations (other than copy and jump slot
3540 relocations) in an executable. */
3541 if (ELIMINATE_COPY_RELOCS
3542 && (bed->target_id == X86_64_ELF_DATA
3543 || (!eh->gotoff_ref
3544 && htab->elf.target_os != is_vxworks)))
3545 {
3546 /* If we don't find any dynamic relocs in read-only sections,
3547 then we'll be keeping the dynamic relocs and avoiding the copy
3548 reloc. */
3549 if (!_bfd_elf_readonly_dynrelocs (h))
3550 {
3551 h->non_got_ref = 0;
3552 return true;
3553 }
3554 }
3555
3556 /* We must allocate the symbol in our .dynbss section, which will
3557 become part of the .bss section of the executable. There will be
3558 an entry for this symbol in the .dynsym section. The dynamic
3559 object will contain position independent code, so all references
3560 from the dynamic object to this symbol will go through the global
3561 offset table. The dynamic linker will use the .dynsym entry to
3562 determine the address it must put in the global offset table, so
3563 both the dynamic object and the regular object will refer to the
3564 same memory location for the variable. */
3565
3566 /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
3567 dynamic linker to copy the initial value out of the dynamic object
3568 and into the runtime process image. */
3569 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
3570 {
3571 s = htab->elf.sdynrelro;
3572 srel = htab->elf.sreldynrelro;
3573 }
3574 else
3575 {
3576 s = htab->elf.sdynbss;
3577 srel = htab->elf.srelbss;
3578 }
3579 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3580 {
3581 if (eh->def_protected && bfd_link_executable (info))
3582 for (p = h->dyn_relocs; p != NULL; p = p->next)
3583 {
3584 /* Disallow copy relocation against non-copyable protected
3585 symbol. */
3586 s = p->sec->output_section;
3587 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3588 {
3589 info->callbacks->einfo
3590 /* xgettext:c-format */
3591 (_("%F%P: %pB: copy relocation against non-copyable "
3592 "protected symbol `%s' in %pB\n"),
3593 p->sec->owner, h->root.root.string,
3594 h->root.u.def.section->owner);
3595 return false;
3596 }
3597 }
3598
3599 srel->size += htab->sizeof_reloc;
3600 h->needs_copy = 1;
3601 }
3602
3603 return _bfd_elf_adjust_dynamic_copy (info, h, s);
3604 }
3605
3606 void
3607 _bfd_x86_elf_hide_symbol (struct bfd_link_info *info,
3608 struct elf_link_hash_entry *h,
3609 bool force_local)
3610 {
3611 if (h->root.type == bfd_link_hash_undefweak
3612 && info->nointerp
3613 && bfd_link_pie (info))
3614 {
3615 /* When there is no dynamic interpreter in PIE, make the undefined
3616 weak symbol dynamic so that PC relative branch to the undefined
3617 weak symbol will land to address 0. */
3618 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
3619 if (h->plt.refcount > 0
3620 || eh->plt_got.refcount > 0)
3621 return;
3622 }
3623
3624 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3625 }
3626
3627 /* Return TRUE if a symbol is referenced locally. It is similar to
3628 SYMBOL_REFERENCES_LOCAL, but it also checks version script. It
3629 works in check_relocs. */
3630
3631 bool
3632 _bfd_x86_elf_link_symbol_references_local (struct bfd_link_info *info,
3633 struct elf_link_hash_entry *h)
3634 {
3635 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
3636 struct elf_x86_link_hash_table *htab
3637 = (struct elf_x86_link_hash_table *) info->hash;
3638
3639 if (eh->local_ref > 1)
3640 return true;
3641
3642 if (eh->local_ref == 1)
3643 return false;
3644
3645 /* Unversioned symbols defined in regular objects can be forced local
3646 by linker version script. A weak undefined symbol is forced local
3647 if
3648 1. It has non-default visibility. Or
3649 2. When building executable, there is no dynamic linker. Or
3650 3. or "-z nodynamic-undefined-weak" is used.
3651 */
3652 if (_bfd_elf_symbol_refs_local_p (h, info, 1)
3653 || (h->root.type == bfd_link_hash_undefweak
3654 && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3655 || (bfd_link_executable (info)
3656 && htab->interp == NULL)
3657 || info->dynamic_undefined_weak == 0))
3658 || ((h->def_regular || ELF_COMMON_DEF_P (h))
3659 && info->version_info != NULL
3660 && _bfd_elf_link_hide_sym_by_version (info, h)))
3661 {
3662 eh->local_ref = 2;
3663 return true;
3664 }
3665
3666 eh->local_ref = 1;
3667 return false;
3668 }
3669
3670 /* Return the section that should be marked against GC for a given
3671 relocation. */
3672
3673 asection *
3674 _bfd_x86_elf_gc_mark_hook (asection *sec,
3675 struct bfd_link_info *info,
3676 Elf_Internal_Rela *rel,
3677 struct elf_link_hash_entry *h,
3678 Elf_Internal_Sym *sym)
3679 {
3680 /* Compiler should optimize this out. */
3681 if (((unsigned int) R_X86_64_GNU_VTINHERIT
3682 != (unsigned int) R_386_GNU_VTINHERIT)
3683 || ((unsigned int) R_X86_64_GNU_VTENTRY
3684 != (unsigned int) R_386_GNU_VTENTRY))
3685 abort ();
3686
3687 if (h != NULL)
3688 switch (ELF32_R_TYPE (rel->r_info))
3689 {
3690 case R_X86_64_GNU_VTINHERIT:
3691 case R_X86_64_GNU_VTENTRY:
3692 return NULL;
3693 }
3694
3695 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
3696 }
3697
3698 static bfd_vma
3699 elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
3700 bfd_vma off,
3701 bfd_vma offset ATTRIBUTE_UNUSED,
3702 bfd_vma got_addr)
3703 {
3704 return got_addr + off;
3705 }
3706
3707 static bfd_vma
3708 elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
3709 bfd_vma off,
3710 bfd_vma offset,
3711 bfd_vma got_addr ATTRIBUTE_UNUSED)
3712 {
3713 return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
3714 }
3715
3716 static bool
3717 elf_i386_valid_plt_reloc_p (unsigned int type)
3718 {
3719 return (type == R_386_JUMP_SLOT
3720 || type == R_386_GLOB_DAT
3721 || type == R_386_IRELATIVE);
3722 }
3723
3724 static bool
3725 elf_x86_64_valid_plt_reloc_p (unsigned int type)
3726 {
3727 return (type == R_X86_64_JUMP_SLOT
3728 || type == R_X86_64_GLOB_DAT
3729 || type == R_X86_64_IRELATIVE);
3730 }
3731
3732 long
3733 _bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
3734 long count,
3735 long relsize,
3736 bfd_vma got_addr,
3737 struct elf_x86_plt plts[],
3738 asymbol **dynsyms,
3739 asymbol **ret)
3740 {
3741 long size, i, n, len;
3742 int j;
3743 unsigned int plt_got_offset, plt_entry_size;
3744 asymbol *s;
3745 bfd_byte *plt_contents;
3746 long dynrelcount;
3747 arelent **dynrelbuf, *p;
3748 char *names;
3749 const struct elf_backend_data *bed;
3750 bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
3751 bfd_vma);
3752 bool (*valid_plt_reloc_p) (unsigned int);
3753 unsigned int jump_slot_reloc;
3754
3755 dynrelbuf = NULL;
3756 if (count == 0)
3757 goto bad_return;
3758
3759 dynrelbuf = (arelent **) bfd_malloc (relsize);
3760 if (dynrelbuf == NULL)
3761 goto bad_return;
3762
3763 dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
3764 dynsyms);
3765 if (dynrelcount <= 0)
3766 goto bad_return;
3767
3768 /* Sort the relocs by address. */
3769 qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
3770 _bfd_x86_elf_compare_relocs);
3771
3772 size = count * sizeof (asymbol);
3773
3774 /* Allocate space for @plt suffixes. */
3775 n = 0;
3776 for (i = 0; i < dynrelcount; i++)
3777 {
3778 p = dynrelbuf[i];
3779 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3780 if (p->addend != 0)
3781 size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
3782 }
3783
3784 s = *ret = (asymbol *) bfd_zmalloc (size);
3785 if (s == NULL)
3786 goto bad_return;
3787
3788 bed = get_elf_backend_data (abfd);
3789
3790 if (bed->target_id == X86_64_ELF_DATA)
3791 {
3792 get_plt_got_vma = elf_x86_64_get_plt_got_vma;
3793 valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
3794 jump_slot_reloc = R_X86_64_JUMP_SLOT;
3795 }
3796 else
3797 {
3798 get_plt_got_vma = elf_i386_get_plt_got_vma;
3799 valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
3800 jump_slot_reloc = R_386_JUMP_SLOT;
3801 if (got_addr)
3802 {
3803 /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
3804 address. */
3805 asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
3806 if (sec != NULL)
3807 got_addr = sec->vma;
3808 else
3809 {
3810 sec = bfd_get_section_by_name (abfd, ".got");
3811 if (sec != NULL)
3812 got_addr = sec->vma;
3813 }
3814
3815 if (got_addr == (bfd_vma) -1)
3816 goto bad_return;
3817 }
3818 }
3819
3820 /* Check for each PLT section. */
3821 names = (char *) (s + count);
3822 size = 0;
3823 n = 0;
3824 for (j = 0; plts[j].name != NULL; j++)
3825 if ((plt_contents = plts[j].contents) != NULL)
3826 {
3827 long k;
3828 bfd_vma offset;
3829 asection *plt;
3830 struct elf_x86_plt *plt_p = &plts[j];
3831
3832 plt_got_offset = plt_p->plt_got_offset;
3833 plt_entry_size = plt_p->plt_entry_size;
3834
3835 plt = plt_p->sec;
3836
3837 if ((plt_p->type & plt_lazy))
3838 {
3839 /* Skip PLT0 in lazy PLT. */
3840 k = 1;
3841 offset = plt_entry_size;
3842 }
3843 else
3844 {
3845 k = 0;
3846 offset = 0;
3847 }
3848
3849 /* Check each PLT entry against dynamic relocations. */
3850 for (; k < plt_p->count; k++)
3851 {
3852 int off;
3853 bfd_vma got_vma;
3854 long min, max, mid;
3855
3856 /* Get the GOT offset for i386 or the PC-relative offset
3857 for x86-64, a signed 32-bit integer. */
3858 off = H_GET_32 (abfd, (plt_contents + offset
3859 + plt_got_offset));
3860 got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
3861
3862 /* Binary search. */
3863 p = dynrelbuf[0];
3864 min = 0;
3865 max = dynrelcount;
3866 while ((min + 1) < max)
3867 {
3868 arelent *r;
3869
3870 mid = (min + max) / 2;
3871 r = dynrelbuf[mid];
3872 if (got_vma > r->address)
3873 min = mid;
3874 else if (got_vma < r->address)
3875 max = mid;
3876 else
3877 {
3878 p = r;
3879 break;
3880 }
3881 }
3882
3883 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
3884 if (got_vma == p->address
3885 && p->howto != NULL
3886 && valid_plt_reloc_p (p->howto->type))
3887 {
3888 *s = **p->sym_ptr_ptr;
3889 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
3890 set. Since we are defining a symbol, ensure one
3891 of them is set. */
3892 if ((s->flags & BSF_LOCAL) == 0)
3893 s->flags |= BSF_GLOBAL;
3894 s->flags |= BSF_SYNTHETIC;
3895 /* This is no longer a section symbol. */
3896 s->flags &= ~BSF_SECTION_SYM;
3897 s->section = plt;
3898 s->the_bfd = plt->owner;
3899 s->value = offset;
3900 s->udata.p = NULL;
3901 s->name = names;
3902 len = strlen ((*p->sym_ptr_ptr)->name);
3903 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3904 names += len;
3905 /* There may be JUMP_SLOT and IRELATIVE relocations.
3906 JUMP_SLOT r_addend should be ignored. */
3907 if (p->addend != 0 && p->howto->type != jump_slot_reloc)
3908 {
3909 char buf[30], *a;
3910
3911 memcpy (names, "+0x", sizeof ("+0x") - 1);
3912 names += sizeof ("+0x") - 1;
3913 bfd_sprintf_vma (abfd, buf, p->addend);
3914 for (a = buf; *a == '0'; ++a)
3915 ;
3916 size = strlen (a);
3917 memcpy (names, a, size);
3918 names += size;
3919 }
3920 memcpy (names, "@plt", sizeof ("@plt"));
3921 names += sizeof ("@plt");
3922 n++;
3923 s++;
3924 /* There should be only one entry in PLT for a given
3925 symbol. Set howto to NULL after processing a PLT
3926 entry to guard against corrupted PLT. */
3927 p->howto = NULL;
3928 }
3929 offset += plt_entry_size;
3930 }
3931 }
3932
3933 /* PLT entries with R_386_TLS_DESC relocations are skipped. */
3934 if (n == 0)
3935 {
3936 bad_return:
3937 count = -1;
3938 }
3939 else
3940 count = n;
3941
3942 for (j = 0; plts[j].name != NULL; j++)
3943 _bfd_elf_munmap_section_contents (plts[j].sec, plts[j].contents);
3944
3945 free (dynrelbuf);
3946
3947 return count;
3948 }
3949
3950 /* Parse x86 GNU properties. */
3951
3952 enum elf_property_kind
3953 _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
3954 bfd_byte *ptr, unsigned int datasz)
3955 {
3956 elf_property *prop;
3957
3958 if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3959 || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3960 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
3961 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3962 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
3963 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
3964 || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3965 && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3966 {
3967 if (datasz != 4)
3968 {
3969 _bfd_error_handler
3970 (_("error: %pB: <corrupt x86 property (0x%x) size: 0x%x>"),
3971 abfd, type, datasz);
3972 return property_corrupt;
3973 }
3974 prop = _bfd_elf_get_property (abfd, type, datasz);
3975 prop->u.number |= bfd_h_get_32 (abfd, ptr);
3976 prop->pr_kind = property_number;
3977 return property_number;
3978 }
3979
3980 return property_ignored;
3981 }
3982
3983 /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
3984 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
3985 should be merged with ABFD. */
3986
3987 bool
3988 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
3989 bfd *abfd ATTRIBUTE_UNUSED,
3990 bfd *bbfd ATTRIBUTE_UNUSED,
3991 elf_property *aprop,
3992 elf_property *bprop)
3993 {
3994 unsigned int number, features;
3995 bool updated = false;
3996 const struct elf_backend_data *bed;
3997 struct elf_x86_link_hash_table *htab;
3998 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
3999
4000 if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
4001 || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
4002 && pr_type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
4003 {
4004 if (aprop == NULL || bprop == NULL)
4005 {
4006 /* Only one of APROP and BPROP can be NULL. */
4007 if (aprop != NULL)
4008 {
4009 /* Remove this property since the other input file doesn't
4010 have it. */
4011 aprop->pr_kind = property_remove;
4012 updated = true;
4013 }
4014 }
4015 else
4016 {
4017 number = aprop->u.number;
4018 aprop->u.number = number | bprop->u.number;
4019 updated = number != (unsigned int) aprop->u.number;
4020 }
4021 return updated;
4022 }
4023 else if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4024 || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_LO
4025 && pr_type <= GNU_PROPERTY_X86_UINT32_OR_HI))
4026 {
4027 features = 0;
4028 if (pr_type == GNU_PROPERTY_X86_ISA_1_NEEDED)
4029 {
4030 bed = get_elf_backend_data (info->output_bfd);
4031 htab = elf_x86_hash_table (info, bed->target_id);
4032 switch (htab->params->isa_level)
4033 {
4034 case 0:
4035 break;
4036 case 2:
4037 features = GNU_PROPERTY_X86_ISA_1_V2;
4038 break;
4039 case 3:
4040 features = GNU_PROPERTY_X86_ISA_1_V3;
4041 break;
4042 case 4:
4043 features = GNU_PROPERTY_X86_ISA_1_V4;
4044 break;
4045 default:
4046 abort ();
4047 }
4048 }
4049 if (aprop != NULL && bprop != NULL)
4050 {
4051 number = aprop->u.number;
4052 aprop->u.number = number | bprop->u.number | features;
4053 /* Remove the property if all bits are empty. */
4054 if (aprop->u.number == 0)
4055 {
4056 aprop->pr_kind = property_remove;
4057 updated = true;
4058 }
4059 else
4060 updated = number != (unsigned int) aprop->u.number;
4061 }
4062 else
4063 {
4064 /* Only one of APROP and BPROP can be NULL. */
4065 if (aprop != NULL)
4066 {
4067 aprop->u.number |= features;
4068 if (aprop->u.number == 0)
4069 {
4070 /* Remove APROP if all bits are empty. */
4071 aprop->pr_kind = property_remove;
4072 updated = true;
4073 }
4074 }
4075 else
4076 {
4077 /* Return TRUE if APROP is NULL and all bits of BPROP
4078 aren't empty to indicate that BPROP should be added
4079 to ABFD. */
4080 bprop->u.number |= features;
4081 updated = bprop->u.number != 0;
4082 }
4083 }
4084 return updated;
4085 }
4086 else if (pr_type >= GNU_PROPERTY_X86_UINT32_AND_LO
4087 && pr_type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4088 {
4089 /* Only one of APROP and BPROP can be NULL:
4090 1. APROP & BPROP when both APROP and BPROP aren't NULL.
4091 2. If APROP is NULL, remove x86 feature.
4092 3. Otherwise, do nothing.
4093 */
4094 bed = get_elf_backend_data (info->output_bfd);
4095 htab = elf_x86_hash_table (info, bed->target_id);
4096 if (!htab)
4097 abort ();
4098 if (aprop != NULL && bprop != NULL)
4099 {
4100 number = aprop->u.number;
4101 aprop->u.number = number & bprop->u.number;
4102 if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
4103 {
4104 features = 0;
4105 if (htab->params->ibt)
4106 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
4107 if (htab->params->shstk)
4108 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
4109 if (htab->params->lam_u48)
4110 features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4111 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4112 else if (htab->params->lam_u57)
4113 features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
4114 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT,
4115 GNU_PROPERTY_X86_FEATURE_1_SHSTK,
4116 GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
4117 GNU_PROPERTY_X86_FEATURE_1_LAM_U57. */
4118 aprop->u.number |= features;
4119 }
4120 updated = number != (unsigned int) aprop->u.number;
4121 /* Remove the property if all feature bits are cleared. */
4122 if (aprop->u.number == 0)
4123 aprop->pr_kind = property_remove;
4124 }
4125 else
4126 {
4127 /* There should be no AND properties since some input doesn't
4128 have them. Set IBT and SHSTK properties for -z ibt and -z
4129 shstk if needed. */
4130 features = 0;
4131 if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
4132 {
4133 if (htab->params->ibt)
4134 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
4135 if (htab->params->shstk)
4136 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
4137 if (htab->params->lam_u48)
4138 features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4139 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4140 else if (htab->params->lam_u57)
4141 features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
4142 }
4143 if (features)
4144 {
4145 if (aprop != NULL)
4146 {
4147 updated = features != (unsigned int) aprop->u.number;
4148 aprop->u.number = features;
4149 }
4150 else
4151 {
4152 updated = true;
4153 bprop->u.number = features;
4154 }
4155 }
4156 else if (aprop != NULL)
4157 {
4158 aprop->pr_kind = property_remove;
4159 updated = true;
4160 }
4161 }
4162 return updated;
4163 }
4164 else
4165 {
4166 /* Never should happen. */
4167 abort ();
4168 }
4169
4170 return updated;
4171 }
4172
4173 /* Report x86-64 ISA level. */
4174
4175 static void
4176 report_isa_level (struct bfd_link_info *info, bfd *abfd,
4177 unsigned int bitmask, bool needed)
4178 {
4179 if (!bitmask)
4180 return;
4181
4182 if (needed)
4183 info->callbacks->einfo (_("%pB: x86 ISA needed: "), abfd);
4184 else
4185 info->callbacks->einfo (_("%pB: x86 ISA used: "), abfd);
4186
4187 while (bitmask)
4188 {
4189 unsigned int bit = bitmask & (- bitmask);
4190
4191 bitmask &= ~ bit;
4192 switch (bit)
4193 {
4194 case GNU_PROPERTY_X86_ISA_1_BASELINE:
4195 info->callbacks->einfo ("x86-64-baseline");
4196 break;
4197 case GNU_PROPERTY_X86_ISA_1_V2:
4198 info->callbacks->einfo ("x86-64-v2");
4199 break;
4200 case GNU_PROPERTY_X86_ISA_1_V3:
4201 info->callbacks->einfo ("x86-64-v3");
4202 break;
4203 case GNU_PROPERTY_X86_ISA_1_V4:
4204 info->callbacks->einfo ("x86-64-v4");
4205 break;
4206 default:
4207 info->callbacks->einfo (_("<unknown: %x>"), bit);
4208 break;
4209 }
4210 if (bitmask)
4211 info->callbacks->einfo (", ");
4212 }
4213
4214 info->callbacks->einfo ("\n");
4215 }
4216
4217 /* Set up x86 GNU properties. Return the first relocatable ELF input
4218 with GNU properties if found. Otherwise, return NULL. */
4219
4220 bfd *
4221 _bfd_x86_elf_link_setup_gnu_properties
4222 (struct bfd_link_info *info, struct elf_x86_init_table *init_table)
4223 {
4224 bool normal_target;
4225 bool lazy_plt;
4226 asection *sec, *pltsec;
4227 bfd *dynobj;
4228 bool use_ibt_plt;
4229 unsigned int plt_alignment, features, isa_level;
4230 struct elf_x86_link_hash_table *htab;
4231 bfd *pbfd;
4232 bfd *ebfd = NULL;
4233 elf_property *prop;
4234 const struct elf_backend_data *bed;
4235 unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
4236 unsigned int got_align;
4237
4238 /* Find a normal input file with GNU property note. */
4239 for (pbfd = info->input_bfds;
4240 pbfd != NULL;
4241 pbfd = pbfd->link.next)
4242 if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
4243 && bfd_count_sections (pbfd) != 0)
4244 {
4245 ebfd = pbfd;
4246
4247 if (elf_properties (pbfd) != NULL)
4248 break;
4249 }
4250
4251 bed = get_elf_backend_data (info->output_bfd);
4252
4253 htab = elf_x86_hash_table (info, bed->target_id);
4254 if (htab == NULL)
4255 return pbfd;
4256
4257 features = 0;
4258 if (htab->params->ibt)
4259 {
4260 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
4261 htab->params->cet_report &= ~prop_report_ibt;
4262 }
4263 if (htab->params->shstk)
4264 {
4265 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
4266 htab->params->cet_report &= ~prop_report_shstk;
4267 }
4268 if (!(htab->params->cet_report & (prop_report_ibt | prop_report_shstk)))
4269 htab->params->cet_report = prop_report_none;
4270 if (htab->params->lam_u48)
4271 {
4272 features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4273 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4274 htab->params->lam_u48_report = prop_report_none;
4275 htab->params->lam_u57_report = prop_report_none;
4276 }
4277 else if (htab->params->lam_u57)
4278 {
4279 features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
4280 htab->params->lam_u57_report = prop_report_none;
4281 }
4282
4283 switch (htab->params->isa_level)
4284 {
4285 case 0:
4286 isa_level = 0;
4287 break;
4288 case 1:
4289 isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE;
4290 break;
4291 case 2:
4292 isa_level = GNU_PROPERTY_X86_ISA_1_V2;
4293 break;
4294 case 3:
4295 isa_level = GNU_PROPERTY_X86_ISA_1_V3;
4296 break;
4297 case 4:
4298 isa_level = GNU_PROPERTY_X86_ISA_1_V4;
4299 break;
4300 default:
4301 abort ();
4302 }
4303
4304 if (ebfd != NULL)
4305 {
4306 prop = NULL;
4307 if (features)
4308 {
4309 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT,
4310 GNU_PROPERTY_X86_FEATURE_1_SHSTK,
4311 GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
4312 GNU_PROPERTY_X86_FEATURE_1_LAM_U57. */
4313 prop = _bfd_elf_get_property (ebfd,
4314 GNU_PROPERTY_X86_FEATURE_1_AND,
4315 4);
4316 prop->u.number |= features;
4317 prop->pr_kind = property_number;
4318 }
4319
4320 if (isa_level)
4321 {
4322 /* If ISA level is set, add GNU_PROPERTY_X86_ISA_1_NEEDED. */
4323 prop = _bfd_elf_get_property (ebfd,
4324 GNU_PROPERTY_X86_ISA_1_NEEDED,
4325 4);
4326 prop->u.number |= isa_level;
4327 prop->pr_kind = property_number;
4328 }
4329
4330 /* Create the GNU property note section if needed. */
4331 if (prop != NULL && pbfd == NULL)
4332 {
4333 sec = bfd_make_section_with_flags (ebfd,
4334 NOTE_GNU_PROPERTY_SECTION_NAME,
4335 (SEC_ALLOC
4336 | SEC_LOAD
4337 | SEC_IN_MEMORY
4338 | SEC_READONLY
4339 | SEC_HAS_CONTENTS
4340 | SEC_DATA));
4341 if (sec == NULL)
4342 info->callbacks->einfo (_("%F%P: failed to create GNU property section\n"));
4343
4344 if (!bfd_set_section_alignment (sec, class_align))
4345 {
4346 error_alignment:
4347 info->callbacks->einfo (_("%F%pA: failed to align section\n"),
4348 sec);
4349 }
4350
4351 elf_section_type (sec) = SHT_NOTE;
4352 }
4353 }
4354
4355 bool check_feature_1 = (htab->params->cet_report
4356 || htab->params->lam_u48_report
4357 || htab->params->lam_u57_report);
4358 if (check_feature_1 || htab->params->isa_level_report)
4359 {
4360 /* Report missing IBT, SHSTK, ISA level and LAM properties. */
4361 bfd *abfd;
4362 const char *warning_msg = _("%P: %pB: warning: missing %s\n");
4363 const char *error_msg = _("%X%P: %pB: error: missing %s\n");
4364 const char *cet_msg = NULL;
4365 const char *lam_u48_msg = NULL;
4366 const char *lam_u57_msg = NULL;
4367 const char *missing;
4368 elf_property_list *p;
4369 bool missing_ibt, missing_shstk;
4370 bool missing_lam_u48, missing_lam_u57;
4371 bool check_ibt
4372 = (htab->params->cet_report
4373 && (htab->params->cet_report & prop_report_ibt));
4374 bool check_shstk
4375 = (htab->params->cet_report
4376 && (htab->params->cet_report & prop_report_shstk));
4377 bool report_needed_level
4378 = (htab->params->isa_level_report & isa_level_report_needed) != 0;
4379 bool report_used_level
4380 = (htab->params->isa_level_report & isa_level_report_used) != 0;
4381
4382 if (htab->params->cet_report)
4383 {
4384 if ((htab->params->cet_report & prop_report_warning))
4385 cet_msg = warning_msg;
4386 else
4387 cet_msg = error_msg;
4388 }
4389 if (htab->params->lam_u48_report)
4390 {
4391 if ((htab->params->lam_u48_report & prop_report_warning))
4392 lam_u48_msg = warning_msg;
4393 else
4394 lam_u48_msg = error_msg;
4395 }
4396 if (htab->params->lam_u57_report)
4397 {
4398 if ((htab->params->lam_u57_report & prop_report_warning))
4399 lam_u57_msg = warning_msg;
4400 else
4401 lam_u57_msg = error_msg;
4402 }
4403
4404 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
4405 if (!(abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED))
4406 && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
4407 {
4408 elf_property_list *p_feature_1 = NULL;
4409 elf_property_list *p_isa_1_needed = NULL;
4410 elf_property_list *p_isa_1_used = NULL;
4411 bool find_feature_1 = check_feature_1;
4412 bool find_needed_level = report_needed_level;
4413 bool find_used_level = report_used_level;
4414
4415 for (p = elf_properties (abfd); p; p = p->next)
4416 {
4417 switch (p->property.pr_type)
4418 {
4419 case GNU_PROPERTY_X86_FEATURE_1_AND:
4420 if (find_feature_1)
4421 {
4422 p_feature_1 = p;
4423 find_feature_1 = false;
4424 }
4425 break;
4426 case GNU_PROPERTY_X86_ISA_1_NEEDED:
4427 if (find_needed_level)
4428 {
4429 p_isa_1_needed = p;
4430 find_needed_level = false;
4431 }
4432 break;
4433 case GNU_PROPERTY_X86_ISA_1_USED:
4434 if (find_used_level)
4435 {
4436 p_isa_1_used = p;
4437 find_used_level = false;
4438 }
4439 break;
4440 default:
4441 break;
4442 }
4443
4444 if (!find_feature_1
4445 && !find_needed_level
4446 && !find_used_level)
4447 break;
4448 }
4449
4450
4451 missing_ibt = check_ibt;
4452 missing_shstk = check_shstk;
4453 missing_lam_u48 = !!lam_u48_msg;
4454 missing_lam_u57 = !!lam_u57_msg;
4455 if (p_feature_1)
4456 {
4457 missing_ibt &= !(p_feature_1->property.u.number
4458 & GNU_PROPERTY_X86_FEATURE_1_IBT);
4459 missing_shstk &= !(p_feature_1->property.u.number
4460 & GNU_PROPERTY_X86_FEATURE_1_SHSTK);
4461 missing_lam_u48 &= !(p_feature_1->property.u.number
4462 & GNU_PROPERTY_X86_FEATURE_1_LAM_U48);
4463 missing_lam_u57 &= !(p_feature_1->property.u.number
4464 & GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4465 }
4466 if (missing_ibt || missing_shstk)
4467 {
4468 if (missing_ibt && missing_shstk)
4469 missing = _("IBT and SHSTK properties");
4470 else if (missing_ibt)
4471 missing = _("IBT property");
4472 else
4473 missing = _("SHSTK property");
4474 info->callbacks->einfo (cet_msg, abfd, missing);
4475 }
4476 if (missing_lam_u48)
4477 {
4478 missing = _("LAM_U48 property");
4479 info->callbacks->einfo (lam_u48_msg, abfd, missing);
4480 }
4481 if (missing_lam_u57)
4482 {
4483 missing = _("LAM_U57 property");
4484 info->callbacks->einfo (lam_u57_msg, abfd, missing);
4485 }
4486
4487 if (p_isa_1_needed)
4488 report_isa_level (info, abfd,
4489 p_isa_1_needed->property.u.number,
4490 true);
4491 if (p_isa_1_used)
4492 report_isa_level (info, abfd,
4493 p_isa_1_used->property.u.number,
4494 false);
4495 }
4496 }
4497
4498 pbfd = _bfd_elf_link_setup_gnu_properties (info);
4499
4500 htab->r_info = init_table->r_info;
4501 htab->r_sym = init_table->r_sym;
4502
4503 if (bfd_link_relocatable (info))
4504 return pbfd;
4505
4506 htab->plt0_pad_byte = init_table->plt0_pad_byte;
4507
4508 use_ibt_plt = htab->params->ibtplt || htab->params->ibt;
4509 if (!use_ibt_plt && pbfd != NULL)
4510 {
4511 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
4512 elf_property_list *p;
4513
4514 /* The property list is sorted in order of type. */
4515 for (p = elf_properties (pbfd); p; p = p->next)
4516 {
4517 if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
4518 {
4519 use_ibt_plt = !!(p->property.u.number
4520 & GNU_PROPERTY_X86_FEATURE_1_IBT);
4521 break;
4522 }
4523 else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
4524 break;
4525 }
4526 }
4527
4528 dynobj = htab->elf.dynobj;
4529
4530 /* Set htab->elf.dynobj here so that there is no need to check and
4531 set it in check_relocs. */
4532 if (dynobj == NULL)
4533 {
4534 if (pbfd != NULL)
4535 {
4536 htab->elf.dynobj = pbfd;
4537 dynobj = pbfd;
4538 }
4539 else
4540 {
4541 bfd *abfd;
4542
4543 /* Find a normal input file to hold linker created
4544 sections. */
4545 for (abfd = info->input_bfds;
4546 abfd != NULL;
4547 abfd = abfd->link.next)
4548 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
4549 && (abfd->flags
4550 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
4551 && bed->relocs_compatible (abfd->xvec,
4552 info->output_bfd->xvec))
4553 {
4554 htab->elf.dynobj = abfd;
4555 dynobj = abfd;
4556 break;
4557 }
4558 }
4559 }
4560
4561 /* Return if there are no normal input files. */
4562 if (dynobj == NULL)
4563 return pbfd;
4564
4565 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
4566 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
4567 canonical function address. */
4568 htab->plt.has_plt0 = 1;
4569 htab->plt.plt_indirect_branch_offset = 0;
4570 normal_target = htab->elf.target_os == is_normal;
4571
4572 if (normal_target)
4573 {
4574 if (use_ibt_plt)
4575 {
4576 htab->lazy_plt = init_table->lazy_ibt_plt;
4577 htab->non_lazy_plt = init_table->non_lazy_ibt_plt;
4578 htab->plt.plt_indirect_branch_offset = 4;
4579 }
4580 else
4581 {
4582 htab->lazy_plt = init_table->lazy_plt;
4583 htab->non_lazy_plt = init_table->non_lazy_plt;
4584 }
4585 }
4586 else
4587 {
4588 htab->lazy_plt = init_table->lazy_plt;
4589 htab->non_lazy_plt = NULL;
4590 }
4591
4592 pltsec = htab->elf.splt;
4593
4594 if (htab->non_lazy_plt != NULL
4595 && (!htab->plt.has_plt0 || pltsec == NULL))
4596 lazy_plt = false;
4597 else
4598 lazy_plt = true;
4599
4600 if (normal_target)
4601 {
4602 if (use_ibt_plt)
4603 {
4604 if (lazy_plt)
4605 htab->sframe_plt = init_table->sframe_lazy_ibt_plt;
4606 else
4607 htab->sframe_plt = init_table->sframe_non_lazy_ibt_plt;
4608 }
4609 else
4610 {
4611 if (lazy_plt)
4612 htab->sframe_plt = init_table->sframe_lazy_plt;
4613 else
4614 htab->sframe_plt = init_table->sframe_non_lazy_plt;
4615 }
4616 }
4617 else
4618 htab->sframe_plt = NULL;
4619
4620 /* If the non-lazy PLT is available, use it for all PLT entries if
4621 there are no PLT0 or no .plt section. */
4622 if (!lazy_plt)
4623 {
4624 if (bfd_link_pic (info))
4625 htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
4626 else
4627 htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
4628 htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
4629 htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
4630 htab->plt.plt_got_insn_size
4631 = htab->non_lazy_plt->plt_got_insn_size;
4632 htab->plt.eh_frame_plt_size
4633 = htab->non_lazy_plt->eh_frame_plt_size;
4634 htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
4635 }
4636 else
4637 {
4638 if (bfd_link_pic (info))
4639 {
4640 htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
4641 htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
4642 }
4643 else
4644 {
4645 htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
4646 htab->plt.plt_entry = htab->lazy_plt->plt_entry;
4647 }
4648 htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
4649 htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
4650 htab->plt.plt_got_insn_size
4651 = htab->lazy_plt->plt_got_insn_size;
4652 htab->plt.eh_frame_plt_size
4653 = htab->lazy_plt->eh_frame_plt_size;
4654 htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
4655 }
4656
4657 if (htab->elf.target_os == is_vxworks
4658 && !elf_vxworks_create_dynamic_sections (dynobj, info,
4659 &htab->srelplt2))
4660 {
4661 info->callbacks->einfo (_("%F%P: failed to create VxWorks dynamic sections\n"));
4662 return pbfd;
4663 }
4664
4665 /* Since create_dynamic_sections isn't always called, but GOT
4666 relocations need GOT relocations, create them here so that we
4667 don't need to do it in check_relocs. */
4668 if (htab->elf.sgot == NULL
4669 && !_bfd_elf_create_got_section (dynobj, info))
4670 info->callbacks->einfo (_("%F%P: failed to create GOT sections\n"));
4671
4672 got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
4673
4674 /* Align .got and .got.plt sections to their entry size. Do it here
4675 instead of in create_dynamic_sections so that they are always
4676 properly aligned even if create_dynamic_sections isn't called. */
4677 sec = htab->elf.sgot;
4678 if (!bfd_set_section_alignment (sec, got_align))
4679 goto error_alignment;
4680
4681 sec = htab->elf.sgotplt;
4682 if (!bfd_set_section_alignment (sec, got_align))
4683 goto error_alignment;
4684
4685 /* Create the ifunc sections here so that check_relocs can be
4686 simplified. */
4687 if (!_bfd_elf_create_ifunc_sections (dynobj, info))
4688 info->callbacks->einfo (_("%F%P: failed to create ifunc sections\n"));
4689
4690 plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
4691
4692 if (pltsec != NULL)
4693 {
4694 /* Whe creating executable, set the contents of the .interp
4695 section to the interpreter. */
4696 if (bfd_link_executable (info) && !info->nointerp)
4697 {
4698 asection *s = bfd_get_linker_section (dynobj, ".interp");
4699 if (s == NULL)
4700 abort ();
4701 s->size = htab->dynamic_interpreter_size;
4702 s->contents = (unsigned char *) htab->dynamic_interpreter;
4703 htab->interp = s;
4704 }
4705
4706 if (normal_target)
4707 {
4708 flagword pltflags = (bed->dynamic_sec_flags
4709 | SEC_ALLOC
4710 | SEC_CODE
4711 | SEC_LOAD
4712 | SEC_READONLY);
4713 unsigned int non_lazy_plt_alignment
4714 = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
4715
4716 sec = pltsec;
4717 if (!bfd_set_section_alignment (sec, plt_alignment))
4718 goto error_alignment;
4719
4720 /* Create the GOT procedure linkage table. */
4721 sec = bfd_make_section_anyway_with_flags (dynobj,
4722 ".plt.got",
4723 pltflags);
4724 if (sec == NULL)
4725 info->callbacks->einfo (_("%F%P: failed to create GOT PLT section\n"));
4726
4727 if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
4728 goto error_alignment;
4729
4730 htab->plt_got = sec;
4731
4732 if (lazy_plt)
4733 {
4734 sec = NULL;
4735
4736 if (use_ibt_plt)
4737 {
4738 /* Create the second PLT for Intel IBT support. IBT
4739 PLT is needed only for lazy binding. */
4740 sec = bfd_make_section_anyway_with_flags (dynobj,
4741 ".plt.sec",
4742 pltflags);
4743 if (sec == NULL)
4744 info->callbacks->einfo (_("%F%P: failed to create IBT-enabled PLT section\n"));
4745
4746 if (!bfd_set_section_alignment (sec, plt_alignment))
4747 goto error_alignment;
4748 }
4749
4750 htab->plt_second = sec;
4751 }
4752 }
4753
4754 if (!info->no_ld_generated_unwind_info)
4755 {
4756 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4757 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4758 | SEC_LINKER_CREATED);
4759
4760 sec = bfd_make_section_anyway_with_flags (dynobj,
4761 ".eh_frame",
4762 flags);
4763 if (sec == NULL)
4764 info->callbacks->einfo (_("%F%P: failed to create PLT .eh_frame section\n"));
4765
4766 if (!bfd_set_section_alignment (sec, class_align))
4767 goto error_alignment;
4768
4769 htab->plt_eh_frame = sec;
4770
4771 if (htab->plt_got != NULL)
4772 {
4773 sec = bfd_make_section_anyway_with_flags (dynobj,
4774 ".eh_frame",
4775 flags);
4776 if (sec == NULL)
4777 info->callbacks->einfo (_("%F%P: failed to create GOT PLT .eh_frame section\n"));
4778
4779 if (!bfd_set_section_alignment (sec, class_align))
4780 goto error_alignment;
4781
4782 htab->plt_got_eh_frame = sec;
4783 }
4784
4785 if (htab->plt_second != NULL)
4786 {
4787 sec = bfd_make_section_anyway_with_flags (dynobj,
4788 ".eh_frame",
4789 flags);
4790 if (sec == NULL)
4791 info->callbacks->einfo (_("%F%P: failed to create the second PLT .eh_frame section\n"));
4792
4793 if (!bfd_set_section_alignment (sec, class_align))
4794 goto error_alignment;
4795
4796 htab->plt_second_eh_frame = sec;
4797 }
4798 }
4799
4800 /* .sframe sections are emitted for AMD64 ABI only. */
4801 if (ABI_64_P (info->output_bfd) && !info->no_ld_generated_unwind_info)
4802 {
4803 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4804 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4805 | SEC_LINKER_CREATED);
4806
4807 sec = bfd_make_section_anyway_with_flags (dynobj,
4808 ".sframe",
4809 flags);
4810 if (sec == NULL)
4811 info->callbacks->einfo (_("%F%P: failed to create PLT .sframe section\n"));
4812
4813 // FIXME check this
4814 // if (!bfd_set_section_alignment (sec, class_align))
4815 // goto error_alignment;
4816
4817 htab->plt_sframe = sec;
4818
4819 /* Second PLT is generated for Intel IBT + lazy plt. */
4820 if (htab->plt_second != NULL)
4821 {
4822 sec = bfd_make_section_anyway_with_flags (dynobj,
4823 ".sframe",
4824 flags);
4825 if (sec == NULL)
4826 info->callbacks->einfo (_("%F%P: failed to create second PLT .sframe section\n"));
4827
4828 htab->plt_second_sframe = sec;
4829 }
4830
4831 /* .plt.got. */
4832 if (htab->plt_got != NULL)
4833 {
4834 sec = bfd_make_section_anyway_with_flags (dynobj,
4835 ".sframe",
4836 flags);
4837 if (sec == NULL)
4838 info->callbacks->einfo (_("%F%P: failed to create PLT GOT .sframe section\n"));
4839
4840 htab->plt_got_sframe = sec;
4841 }
4842 }
4843 }
4844
4845 /* The .iplt section is used for IFUNC symbols in static
4846 executables. */
4847 sec = htab->elf.iplt;
4848 if (sec != NULL)
4849 {
4850 /* NB: Delay setting its alignment until we know it is non-empty.
4851 Otherwise an empty iplt section may change vma and lma of the
4852 following sections, which triggers moving dot of the following
4853 section backwards, resulting in a warning and section lma not
4854 being set properly. It later leads to a "File truncated"
4855 error. */
4856 if (!bfd_set_section_alignment (sec, 0))
4857 goto error_alignment;
4858
4859 htab->plt.iplt_alignment = (normal_target
4860 ? plt_alignment
4861 : bed->plt_alignment);
4862 }
4863
4864 if (bfd_link_executable (info)
4865 && !info->nointerp
4866 && !htab->params->has_dynamic_linker
4867 && htab->params->static_before_all_inputs)
4868 {
4869 /* Report error for dynamic input objects if -static is passed at
4870 command-line before all input files without --dynamic-linker
4871 unless --no-dynamic-linker is used. */
4872 bfd *abfd;
4873
4874 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
4875 if ((abfd->flags & DYNAMIC))
4876 info->callbacks->einfo
4877 (_("%X%P: attempted static link of dynamic object `%pB'\n"),
4878 abfd);
4879 }
4880
4881 return pbfd;
4882 }
4883
4884 /* Fix up x86 GNU properties. */
4885
4886 void
4887 _bfd_x86_elf_link_fixup_gnu_properties
4888 (struct bfd_link_info *info, elf_property_list **listp)
4889 {
4890 elf_property_list *p;
4891
4892 for (p = *listp; p; p = p->next)
4893 {
4894 unsigned int type = p->property.pr_type;
4895 if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
4896 || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4897 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
4898 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4899 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
4900 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
4901 || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
4902 && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
4903 {
4904 if (p->property.u.number == 0
4905 && (type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4906 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
4907 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4908 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
4909 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)))
4910 {
4911 /* Remove empty property. */
4912 *listp = p->next;
4913 continue;
4914 }
4915
4916 /* Keep LAM features only for 64-bit output. */
4917 if (type == GNU_PROPERTY_X86_FEATURE_1_AND
4918 && !ABI_64_P (info->output_bfd))
4919 p->property.u.number &= ~(GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4920 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4921
4922 listp = &p->next;
4923 }
4924 else if (type > GNU_PROPERTY_HIPROC)
4925 {
4926 /* The property list is sorted in order of type. */
4927 break;
4928 }
4929 }
4930 }
4931
4932 void
4933 _bfd_elf_linker_x86_set_options (struct bfd_link_info * info,
4934 struct elf_linker_x86_params *params)
4935 {
4936 const struct elf_backend_data *bed
4937 = get_elf_backend_data (info->output_bfd);
4938 struct elf_x86_link_hash_table *htab
4939 = elf_x86_hash_table (info, bed->target_id);
4940 if (htab != NULL)
4941 htab->params = params;
4942 }
4943