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