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