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