]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elfxx-x86.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / bfd / elfxx-x86.c
CommitLineData
0afcef53 1/* x86 specific support for ELF
a2c58332 2 Copyright (C) 2017-2022 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
527 sreloc = elf_section_data (p->sec)->sreloc;
528
529 BFD_ASSERT (sreloc != NULL);
530 sreloc->size += p->count * htab->sizeof_reloc;
531 }
532
0a1b45a2 533 return true;
b9ce864c
L
534}
535
b9ce864c
L
536/* Allocate space in .plt, .got and associated reloc sections for
537 local dynamic relocs. */
538
1201fda6 539static int
5e2ac45d 540elf_x86_allocate_local_dynreloc (void **slot, void *inf)
b9ce864c
L
541{
542 struct elf_link_hash_entry *h
543 = (struct elf_link_hash_entry *) *slot;
544
545 if (h->type != STT_GNU_IFUNC
546 || !h->def_regular
547 || !h->ref_regular
548 || !h->forced_local
549 || h->root.type != bfd_link_hash_defined)
550 abort ();
551
5e2ac45d 552 return elf_x86_allocate_dynrelocs (h, inf);
b9ce864c
L
553}
554
0afcef53
L
555/* Find and/or create a hash entry for local symbol. */
556
557struct elf_link_hash_entry *
558_bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table *htab,
559 bfd *abfd, const Elf_Internal_Rela *rel,
0a1b45a2 560 bool create)
0afcef53
L
561{
562 struct elf_x86_link_hash_entry e, *ret;
563 asection *sec = abfd->sections;
564 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
565 htab->r_sym (rel->r_info));
566 void **slot;
567
568 e.elf.indx = sec->id;
569 e.elf.dynstr_index = htab->r_sym (rel->r_info);
570 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
571 create ? INSERT : NO_INSERT);
572
573 if (!slot)
574 return NULL;
575
576 if (*slot)
577 {
578 ret = (struct elf_x86_link_hash_entry *) *slot;
579 return &ret->elf;
580 }
581
582 ret = (struct elf_x86_link_hash_entry *)
583 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
584 sizeof (struct elf_x86_link_hash_entry));
585 if (ret)
586 {
587 memset (ret, 0, sizeof (*ret));
588 ret->elf.indx = sec->id;
589 ret->elf.dynstr_index = htab->r_sym (rel->r_info);
590 ret->elf.dynindx = -1;
591 ret->plt_got.offset = (bfd_vma) -1;
592 *slot = ret;
593 }
594 return &ret->elf;
595}
596
597/* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN
598 SYNC WITH _bfd_elf_link_hash_newfunc. */
599
600struct bfd_hash_entry *
601_bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
602 struct bfd_hash_table *table,
603 const char *string)
604{
605 /* Allocate the structure if it has not already been allocated by a
606 subclass. */
607 if (entry == NULL)
608 {
609 entry = (struct bfd_hash_entry *)
610 bfd_hash_allocate (table,
611 sizeof (struct elf_x86_link_hash_entry));
612 if (entry == NULL)
613 return entry;
614 }
615
616 /* Call the allocation method of the superclass. */
617 entry = _bfd_link_hash_newfunc (entry, table, string);
618 if (entry != NULL)
619 {
620 struct elf_x86_link_hash_entry *eh
621 = (struct elf_x86_link_hash_entry *) entry;
622 struct elf_link_hash_table *htab
623 = (struct elf_link_hash_table *) table;
624
625 memset (&eh->elf.size, 0,
626 (sizeof (struct elf_x86_link_hash_entry)
627 - offsetof (struct elf_link_hash_entry, size)));
628 /* Set local fields. */
629 eh->elf.indx = -1;
630 eh->elf.dynindx = -1;
631 eh->elf.got = htab->init_got_refcount;
632 eh->elf.plt = htab->init_plt_refcount;
633 /* Assume that we have been called by a non-ELF symbol reader.
634 This flag is then reset by the code which reads an ELF input
635 file. This ensures that a symbol created by a non-ELF symbol
636 reader will have the flag set correctly. */
637 eh->elf.non_elf = 1;
638 eh->plt_second.offset = (bfd_vma) -1;
639 eh->plt_got.offset = (bfd_vma) -1;
640 eh->tlsdesc_got = (bfd_vma) -1;
98b273dc 641 eh->zero_undefweak = 1;
0afcef53
L
642 }
643
644 return entry;
645}
646
647/* Compute a hash of a local hash entry. We use elf_link_hash_entry
648 for local symbol so that we can handle local STT_GNU_IFUNC symbols
649 as global symbol. We reuse indx and dynstr_index for local symbol
650 hash since they aren't used by global symbols in this backend. */
651
652hashval_t
653_bfd_x86_elf_local_htab_hash (const void *ptr)
654{
655 struct elf_link_hash_entry *h
656 = (struct elf_link_hash_entry *) ptr;
657 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
658}
659
660/* Compare local hash entries. */
661
662int
663_bfd_x86_elf_local_htab_eq (const void *ptr1, const void *ptr2)
664{
665 struct elf_link_hash_entry *h1
666 = (struct elf_link_hash_entry *) ptr1;
667 struct elf_link_hash_entry *h2
668 = (struct elf_link_hash_entry *) ptr2;
669
670 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
671}
672
673/* Destroy an x86 ELF linker hash table. */
674
765e526c
L
675static void
676elf_x86_link_hash_table_free (bfd *obfd)
0afcef53
L
677{
678 struct elf_x86_link_hash_table *htab
679 = (struct elf_x86_link_hash_table *) obfd->link.hash;
680
681 if (htab->loc_hash_table)
682 htab_delete (htab->loc_hash_table);
683 if (htab->loc_hash_memory)
684 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
685 _bfd_elf_link_hash_table_free (obfd);
686}
687
0a1b45a2 688static bool
5e2ac45d
L
689elf_i386_is_reloc_section (const char *secname)
690{
08dedd66 691 return startswith (secname, ".rel");
5e2ac45d
L
692}
693
0a1b45a2 694static bool
5e2ac45d
L
695elf_x86_64_is_reloc_section (const char *secname)
696{
08dedd66 697 return startswith (secname, ".rela");
5e2ac45d 698}
5e2ac45d 699
765e526c
L
700/* Create an x86 ELF linker hash table. */
701
702struct bfd_link_hash_table *
703_bfd_x86_elf_link_hash_table_create (bfd *abfd)
704{
705 struct elf_x86_link_hash_table *ret;
706 const struct elf_backend_data *bed;
986f0783 707 size_t amt = sizeof (struct elf_x86_link_hash_table);
765e526c
L
708
709 ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt);
710 if (ret == NULL)
711 return NULL;
712
713 bed = get_elf_backend_data (abfd);
714 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
715 _bfd_x86_elf_link_hash_newfunc,
716 sizeof (struct elf_x86_link_hash_entry),
717 bed->target_id))
718 {
719 free (ret);
720 return NULL;
721 }
722
5e2ac45d
L
723 if (bed->target_id == X86_64_ELF_DATA)
724 {
5e2ac45d 725 ret->is_reloc_section = elf_x86_64_is_reloc_section;
5e2ac45d 726 ret->got_entry_size = 8;
0a1b45a2 727 ret->pcrel_plt = true;
f04bdfa7 728 ret->tls_get_addr = "__tls_get_addr";
5e2ac45d 729 }
765e526c
L
730 if (ABI_64_P (abfd))
731 {
503294e7 732 ret->sizeof_reloc = sizeof (Elf64_External_Rela);
765e526c
L
733 ret->pointer_r_type = R_X86_64_64;
734 ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
735 ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
765e526c
L
736 }
737 else
765e526c 738 {
5b86dbf4 739 if (bed->target_id == X86_64_ELF_DATA)
765e526c 740 {
503294e7 741 ret->sizeof_reloc = sizeof (Elf32_External_Rela);
765e526c
L
742 ret->pointer_r_type = R_X86_64_32;
743 ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER;
744 ret->dynamic_interpreter_size
745 = sizeof ELFX32_DYNAMIC_INTERPRETER;
765e526c
L
746 }
747 else
748 {
5e2ac45d 749 ret->is_reloc_section = elf_i386_is_reloc_section;
503294e7 750 ret->sizeof_reloc = sizeof (Elf32_External_Rel);
9ff114ca 751 ret->got_entry_size = 4;
0a1b45a2 752 ret->pcrel_plt = false;
765e526c
L
753 ret->pointer_r_type = R_386_32;
754 ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
755 ret->dynamic_interpreter_size
756 = sizeof ELF32_DYNAMIC_INTERPRETER;
757 ret->tls_get_addr = "___tls_get_addr";
758 }
759 }
760
761 ret->loc_hash_table = htab_try_create (1024,
762 _bfd_x86_elf_local_htab_hash,
763 _bfd_x86_elf_local_htab_eq,
764 NULL);
765 ret->loc_hash_memory = objalloc_create ();
766 if (!ret->loc_hash_table || !ret->loc_hash_memory)
767 {
768 elf_x86_link_hash_table_free (abfd);
769 return NULL;
770 }
771 ret->elf.root.hash_table_free = elf_x86_link_hash_table_free;
772
773 return &ret->elf.root;
774}
775
0afcef53
L
776/* Sort relocs into address order. */
777
778int
779_bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
780{
781 const arelent *a = * (const arelent **) ap;
782 const arelent *b = * (const arelent **) bp;
783
784 if (a->address > b->address)
785 return 1;
786 else if (a->address < b->address)
787 return -1;
788 else
789 return 0;
790}
791
9bc935ef
L
792/* Mark symbol, NAME, as locally defined by linker if it is referenced
793 and not defined in a relocatable object file. */
794
795static void
796elf_x86_linker_defined (struct bfd_link_info *info, const char *name)
797{
798 struct elf_link_hash_entry *h;
799
800 h = elf_link_hash_lookup (elf_hash_table (info), name,
0a1b45a2 801 false, false, false);
9bc935ef
L
802 if (h == NULL)
803 return;
804
805 while (h->root.type == bfd_link_hash_indirect)
806 h = (struct elf_link_hash_entry *) h->root.u.i.link;
807
808 if (h->root.type == bfd_link_hash_new
809 || h->root.type == bfd_link_hash_undefined
810 || h->root.type == bfd_link_hash_undefweak
811 || h->root.type == bfd_link_hash_common
812 || (!h->def_regular && h->def_dynamic))
813 {
814 elf_x86_hash_entry (h)->local_ref = 2;
815 elf_x86_hash_entry (h)->linker_def = 1;
816 }
817}
818
ecf99cc0
L
819/* Hide a linker-defined symbol, NAME, with hidden visibility. */
820
821static void
822elf_x86_hide_linker_defined (struct bfd_link_info *info,
823 const char *name)
824{
825 struct elf_link_hash_entry *h;
826
827 h = elf_link_hash_lookup (elf_hash_table (info), name,
0a1b45a2 828 false, false, false);
ecf99cc0
L
829 if (h == NULL)
830 return;
831
832 while (h->root.type == bfd_link_hash_indirect)
833 h = (struct elf_link_hash_entry *) h->root.u.i.link;
834
835 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
836 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
0a1b45a2 837 _bfd_elf_link_hash_hide_symbol (info, h, true);
ecf99cc0
L
838}
839
0a1b45a2 840bool
0afcef53
L
841_bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
842{
843 if (!bfd_link_relocatable (info))
844 {
845 /* Check for __tls_get_addr reference. */
846 struct elf_x86_link_hash_table *htab;
847 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
848 htab = elf_x86_hash_table (info, bed->target_id);
849 if (htab)
850 {
0a27fed7
L
851 struct elf_link_hash_entry *h;
852
853 h = elf_link_hash_lookup (elf_hash_table (info),
0afcef53 854 htab->tls_get_addr,
0a1b45a2 855 false, false, false);
0afcef53 856 if (h != NULL)
8a1b824a
L
857 {
858 elf_x86_hash_entry (h)->tls_get_addr = 1;
859
860 /* Check the versioned __tls_get_addr symbol. */
861 while (h->root.type == bfd_link_hash_indirect)
862 {
863 h = (struct elf_link_hash_entry *) h->root.u.i.link;
864 elf_x86_hash_entry (h)->tls_get_addr = 1;
865 }
866 }
0a27fed7
L
867
868 /* "__ehdr_start" will be defined by linker as a hidden symbol
869 later if it is referenced and not defined. */
9bc935ef
L
870 elf_x86_linker_defined (info, "__ehdr_start");
871
872 if (bfd_link_executable (info))
0a27fed7 873 {
9bc935ef
L
874 /* References to __bss_start, _end and _edata should be
875 locally resolved within executables. */
876 elf_x86_linker_defined (info, "__bss_start");
877 elf_x86_linker_defined (info, "_end");
878 elf_x86_linker_defined (info, "_edata");
ecf99cc0
L
879 }
880 else
881 {
882 /* Hide hidden __bss_start, _end and _edata in shared
883 libraries. */
884 elf_x86_hide_linker_defined (info, "__bss_start");
885 elf_x86_hide_linker_defined (info, "_end");
886 elf_x86_hide_linker_defined (info, "_edata");
0a27fed7 887 }
0afcef53
L
888 }
889 }
890
891 /* Invoke the regular ELF backend linker to do all the work. */
892 return _bfd_elf_link_check_relocs (abfd, info);
893}
894
0a1b45a2 895bool
382aae06
L
896_bfd_elf_x86_valid_reloc_p (asection *input_section,
897 struct bfd_link_info *info,
898 struct elf_x86_link_hash_table *htab,
899 const Elf_Internal_Rela *rel,
900 struct elf_link_hash_entry *h,
901 Elf_Internal_Sym *sym,
902 Elf_Internal_Shdr *symtab_hdr,
0a1b45a2 903 bool *no_dynreloc_p)
382aae06 904{
0a1b45a2 905 bool valid_p = true;
382aae06 906
0a1b45a2 907 *no_dynreloc_p = false;
382aae06
L
908
909 /* Check If relocation against non-preemptible absolute symbol is
910 valid in PIC. FIXME: Can't use SYMBOL_REFERENCES_LOCAL_P since
911 it may call _bfd_elf_link_hide_sym_by_version and result in
912 ld-elfvers/ vers21 test failure. */
913 if (bfd_link_pic (info)
914 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
915 {
916 const struct elf_backend_data *bed;
917 unsigned int r_type;
918 Elf_Internal_Rela irel;
919
920 /* Skip non-absolute symbol. */
921 if (h)
922 {
923 if (!ABS_SYMBOL_P (h))
924 return valid_p;
925 }
926 else if (sym->st_shndx != SHN_ABS)
927 return valid_p;
928
929 bed = get_elf_backend_data (input_section->owner);
930 r_type = ELF32_R_TYPE (rel->r_info);
931 irel = *rel;
932
933 /* Only allow relocations against absolute symbol, which can be
934 resolved as absolute value + addend. GOTPCREL relocations
935 are allowed since absolute value + addend is stored in the
936 GOT slot. */
937 if (bed->target_id == X86_64_ELF_DATA)
938 {
939 r_type &= ~R_X86_64_converted_reloc_bit;
940 valid_p = (r_type == R_X86_64_64
941 || r_type == R_X86_64_32
942 || r_type == R_X86_64_32S
943 || r_type == R_X86_64_16
944 || r_type == R_X86_64_8
945 || r_type == R_X86_64_GOTPCREL
946 || r_type == R_X86_64_GOTPCRELX
947 || r_type == R_X86_64_REX_GOTPCRELX);
948 if (!valid_p)
949 {
950 unsigned int r_symndx = htab->r_sym (rel->r_info);
951 irel.r_info = htab->r_info (r_symndx, r_type);
952 }
953 }
954 else
955 valid_p = (r_type == R_386_32
956 || r_type == R_386_16
957 || r_type == R_386_8);
958
959 if (valid_p)
0a1b45a2 960 *no_dynreloc_p = true;
382aae06
L
961 else
962 {
963 const char *name;
964 arelent internal_reloc;
965
966 if (!bed->elf_info_to_howto (input_section->owner,
967 &internal_reloc, &irel)
968 || internal_reloc.howto == NULL)
969 abort ();
970
971 if (h)
972 name = h->root.root.string;
973 else
974 name = bfd_elf_sym_name (input_section->owner, symtab_hdr,
975 sym, NULL);
976 info->callbacks->einfo
977 /* xgettext:c-format */
978 (_("%F%P: %pB: relocation %s against absolute symbol "
979 "`%s' in section `%pA' is disallowed\n"),
980 input_section->owner, internal_reloc.howto->name, name,
981 input_section);
982 bfd_set_error (bfd_error_bad_value);
983 }
984 }
985
986 return valid_p;
987}
988
5e2ac45d
L
989/* Set the sizes of the dynamic sections. */
990
0a1b45a2 991bool
5e2ac45d
L
992_bfd_x86_elf_size_dynamic_sections (bfd *output_bfd,
993 struct bfd_link_info *info)
994{
995 struct elf_x86_link_hash_table *htab;
996 bfd *dynobj;
997 asection *s;
0a1b45a2 998 bool relocs;
5e2ac45d
L
999 bfd *ibfd;
1000 const struct elf_backend_data *bed
1001 = get_elf_backend_data (output_bfd);
1002
1003 htab = elf_x86_hash_table (info, bed->target_id);
1004 if (htab == NULL)
0a1b45a2 1005 return false;
5e2ac45d
L
1006 dynobj = htab->elf.dynobj;
1007 if (dynobj == NULL)
1008 abort ();
1009
1010 /* Set up .got offsets for local syms, and space for local dynamic
1011 relocs. */
1012 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1013 {
1014 bfd_signed_vma *local_got;
1015 bfd_signed_vma *end_local_got;
1016 char *local_tls_type;
1017 bfd_vma *local_tlsdesc_gotent;
1018 bfd_size_type locsymcount;
1019 Elf_Internal_Shdr *symtab_hdr;
1020 asection *srel;
1021
1022 if (! is_x86_elf (ibfd, htab))
1023 continue;
1024
1025 for (s = ibfd->sections; s != NULL; s = s->next)
1026 {
1027 struct elf_dyn_relocs *p;
1028
5e2ac45d
L
1029 for (p = ((struct elf_dyn_relocs *)
1030 elf_section_data (s)->local_dynrel);
1031 p != NULL;
1032 p = p->next)
1033 {
1034 if (!bfd_is_abs_section (p->sec)
1035 && bfd_is_abs_section (p->sec->output_section))
1036 {
1037 /* Input section has been discarded, either because
1038 it is a copy of a linkonce section or due to
1039 linker script /DISCARD/, so we'll be discarding
1040 the relocs too. */
1041 }
90c14f0c 1042 else if (htab->elf.target_os == is_vxworks
5e2ac45d
L
1043 && strcmp (p->sec->output_section->name,
1044 ".tls_vars") == 0)
1045 {
1046 /* Relocations in vxworks .tls_vars sections are
1047 handled specially by the loader. */
1048 }
1049 else if (p->count != 0)
1050 {
1051 srel = elf_section_data (p->sec)->sreloc;
1052 srel->size += p->count * htab->sizeof_reloc;
1053 if ((p->sec->output_section->flags & SEC_READONLY) != 0
1054 && (info->flags & DF_TEXTREL) == 0)
1055 {
1056 info->flags |= DF_TEXTREL;
a6dbf402 1057 if (bfd_link_textrel_check (info))
5e2ac45d 1058 /* xgettext:c-format */
826c3f1e 1059 info->callbacks->einfo
871b3ab2
AM
1060 (_("%P: %pB: warning: relocation "
1061 "in read-only section `%pA'\n"),
826c3f1e 1062 p->sec->owner, p->sec);
5e2ac45d
L
1063 }
1064 }
1065 }
1066 }
1067
1068 local_got = elf_local_got_refcounts (ibfd);
1069 if (!local_got)
1070 continue;
1071
1072 symtab_hdr = &elf_symtab_hdr (ibfd);
1073 locsymcount = symtab_hdr->sh_info;
1074 end_local_got = local_got + locsymcount;
1075 local_tls_type = elf_x86_local_got_tls_type (ibfd);
1076 local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
1077 s = htab->elf.sgot;
1078 srel = htab->elf.srelgot;
1079 for (; local_got < end_local_got;
1080 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
1081 {
1082 *local_tlsdesc_gotent = (bfd_vma) -1;
1083 if (*local_got > 0)
1084 {
1085 if (GOT_TLS_GDESC_P (*local_tls_type))
1086 {
1087 *local_tlsdesc_gotent = htab->elf.sgotplt->size
1088 - elf_x86_compute_jump_table_size (htab);
1089 htab->elf.sgotplt->size += 2 * htab->got_entry_size;
1090 *local_got = (bfd_vma) -2;
1091 }
1092 if (! GOT_TLS_GDESC_P (*local_tls_type)
1093 || GOT_TLS_GD_P (*local_tls_type))
1094 {
1095 *local_got = s->size;
1096 s->size += htab->got_entry_size;
1097 if (GOT_TLS_GD_P (*local_tls_type)
1098 || *local_tls_type == GOT_TLS_IE_BOTH)
1099 s->size += htab->got_entry_size;
1100 }
382aae06 1101 if ((bfd_link_pic (info) && *local_tls_type != GOT_ABS)
5e2ac45d
L
1102 || GOT_TLS_GD_ANY_P (*local_tls_type)
1103 || (*local_tls_type & GOT_TLS_IE))
1104 {
1105 if (*local_tls_type == GOT_TLS_IE_BOTH)
1106 srel->size += 2 * htab->sizeof_reloc;
1107 else if (GOT_TLS_GD_P (*local_tls_type)
1108 || ! GOT_TLS_GDESC_P (*local_tls_type))
1109 srel->size += htab->sizeof_reloc;
1110 if (GOT_TLS_GDESC_P (*local_tls_type))
1111 {
1112 htab->elf.srelplt->size += htab->sizeof_reloc;
1113 if (bed->target_id == X86_64_ELF_DATA)
9bcc30e4 1114 htab->elf.tlsdesc_plt = (bfd_vma) -1;
5e2ac45d
L
1115 }
1116 }
1117 }
1118 else
1119 *local_got = (bfd_vma) -1;
1120 }
1121 }
1122
1123 if (htab->tls_ld_or_ldm_got.refcount > 0)
1124 {
1125 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
07d6d2b8 1126 or R_X86_64_TLSLD relocs. */
5e2ac45d
L
1127 htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
1128 htab->elf.sgot->size += 2 * htab->got_entry_size;
1129 htab->elf.srelgot->size += htab->sizeof_reloc;
1130 }
1131 else
1132 htab->tls_ld_or_ldm_got.offset = -1;
1133
1134 /* Allocate global sym .plt and .got entries, and space for global
1135 sym dynamic relocs. */
1136 elf_link_hash_traverse (&htab->elf, elf_x86_allocate_dynrelocs,
1137 info);
1138
1139 /* Allocate .plt and .got entries, and space for local symbols. */
1140 htab_traverse (htab->loc_hash_table, elf_x86_allocate_local_dynreloc,
1141 info);
1142
1143 /* For every jump slot reserved in the sgotplt, reloc_count is
1144 incremented. However, when we reserve space for TLS descriptors,
1145 it's not incremented, so in order to compute the space reserved
1146 for them, it suffices to multiply the reloc count by the jump
1147 slot size.
1148
1149 PR ld/13302: We start next_irelative_index at the end of .rela.plt
1150 so that R_{386,X86_64}_IRELATIVE entries come last. */
1151 if (htab->elf.srelplt)
1152 {
1153 htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
1154 htab->sgotplt_jump_table_size
1155 = elf_x86_compute_jump_table_size (htab);
1156 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
1157 }
1158 else if (htab->elf.irelplt)
1159 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
1160
9bcc30e4 1161 if (htab->elf.tlsdesc_plt)
5e2ac45d
L
1162 {
1163 /* NB: tlsdesc_plt is set only for x86-64. If we're not using
1164 lazy TLS relocations, don't generate the PLT and GOT entries
1165 they require. */
1166 if ((info->flags & DF_BIND_NOW))
9bcc30e4 1167 htab->elf.tlsdesc_plt = 0;
5e2ac45d
L
1168 else
1169 {
9bcc30e4 1170 htab->elf.tlsdesc_got = htab->elf.sgot->size;
5e2ac45d
L
1171 htab->elf.sgot->size += htab->got_entry_size;
1172 /* Reserve room for the initial entry.
1173 FIXME: we could probably do away with it in this case. */
1174 if (htab->elf.splt->size == 0)
1175 htab->elf.splt->size = htab->plt.plt_entry_size;
9bcc30e4 1176 htab->elf.tlsdesc_plt = htab->elf.splt->size;
5e2ac45d
L
1177 htab->elf.splt->size += htab->plt.plt_entry_size;
1178 }
1179 }
1180
1181 if (htab->elf.sgotplt)
1182 {
1183 /* Don't allocate .got.plt section if there are no GOT nor PLT
1184 entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
1185 if ((htab->elf.hgot == NULL
cd048363 1186 || !htab->got_referenced)
5e2ac45d
L
1187 && (htab->elf.sgotplt->size == bed->got_header_size)
1188 && (htab->elf.splt == NULL
1189 || htab->elf.splt->size == 0)
1190 && (htab->elf.sgot == NULL
1191 || htab->elf.sgot->size == 0)
1192 && (htab->elf.iplt == NULL
1193 || htab->elf.iplt->size == 0)
1194 && (htab->elf.igotplt == NULL
1195 || htab->elf.igotplt->size == 0))
cd048363
L
1196 {
1197 htab->elf.sgotplt->size = 0;
3b4c3844
L
1198 /* Solaris requires to keep _GLOBAL_OFFSET_TABLE_ even if it
1199 isn't used. */
90c14f0c
L
1200 if (htab->elf.hgot != NULL
1201 && htab->elf.target_os != is_solaris)
cd048363
L
1202 {
1203 /* Remove the unused _GLOBAL_OFFSET_TABLE_ from symbol
1204 table. */
1205 htab->elf.hgot->root.type = bfd_link_hash_undefined;
1206 htab->elf.hgot->root.u.undef.abfd
1207 = htab->elf.hgot->root.u.def.section->owner;
1208 htab->elf.hgot->root.linker_def = 0;
1209 htab->elf.hgot->ref_regular = 0;
1210 htab->elf.hgot->def_regular = 0;
1211 }
1212 }
5e2ac45d
L
1213 }
1214
1215 if (_bfd_elf_eh_frame_present (info))
1216 {
1217 if (htab->plt_eh_frame != NULL
1218 && htab->elf.splt != NULL
1219 && htab->elf.splt->size != 0
1220 && !bfd_is_abs_section (htab->elf.splt->output_section))
1221 htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
1222
1223 if (htab->plt_got_eh_frame != NULL
1224 && htab->plt_got != NULL
1225 && htab->plt_got->size != 0
1226 && !bfd_is_abs_section (htab->plt_got->output_section))
1227 htab->plt_got_eh_frame->size
1228 = htab->non_lazy_plt->eh_frame_plt_size;
1229
1230 /* Unwind info for the second PLT and .plt.got sections are
1231 identical. */
1232 if (htab->plt_second_eh_frame != NULL
1233 && htab->plt_second != NULL
1234 && htab->plt_second->size != 0
1235 && !bfd_is_abs_section (htab->plt_second->output_section))
1236 htab->plt_second_eh_frame->size
1237 = htab->non_lazy_plt->eh_frame_plt_size;
1238 }
1239
1240 /* We now have determined the sizes of the various dynamic sections.
1241 Allocate memory for them. */
0a1b45a2 1242 relocs = false;
5e2ac45d
L
1243 for (s = dynobj->sections; s != NULL; s = s->next)
1244 {
0a1b45a2 1245 bool strip_section = true;
5e2ac45d
L
1246
1247 if ((s->flags & SEC_LINKER_CREATED) == 0)
1248 continue;
1249
1250 if (s == htab->elf.splt
1251 || s == htab->elf.sgot)
1252 {
1253 /* Strip this section if we don't need it; see the
1254 comment below. */
1255 /* We'd like to strip these sections if they aren't needed, but if
1256 we've exported dynamic symbols from them we must leave them.
1257 It's too late to tell BFD to get rid of the symbols. */
1258
1259 if (htab->elf.hplt != NULL)
0a1b45a2 1260 strip_section = false;
5e2ac45d
L
1261 }
1262 else if (s == htab->elf.sgotplt
1263 || s == htab->elf.iplt
1264 || s == htab->elf.igotplt
1265 || s == htab->plt_second
1266 || s == htab->plt_got
1267 || s == htab->plt_eh_frame
1268 || s == htab->plt_got_eh_frame
1269 || s == htab->plt_second_eh_frame
1270 || s == htab->elf.sdynbss
1271 || s == htab->elf.sdynrelro)
1272 {
1273 /* Strip these too. */
1274 }
fd361982 1275 else if (htab->is_reloc_section (bfd_section_name (s)))
5e2ac45d
L
1276 {
1277 if (s->size != 0
1278 && s != htab->elf.srelplt
1279 && s != htab->srelplt2)
0a1b45a2 1280 relocs = true;
5e2ac45d
L
1281
1282 /* We use the reloc_count field as a counter if we need
1283 to copy relocs into the output file. */
1284 if (s != htab->elf.srelplt)
1285 s->reloc_count = 0;
1286 }
1287 else
1288 {
1289 /* It's not one of our sections, so don't allocate space. */
1290 continue;
1291 }
1292
1293 if (s->size == 0)
1294 {
1295 /* If we don't need this section, strip it from the
1296 output file. This is mostly to handle .rel.bss and
1297 .rel.plt. We must create both sections in
1298 create_dynamic_sections, because they must be created
1299 before the linker maps input sections to output
1300 sections. The linker does that before
1301 adjust_dynamic_symbol is called, and it is that
1302 function which decides whether anything needs to go
1303 into these sections. */
1304 if (strip_section)
1305 s->flags |= SEC_EXCLUDE;
1306 continue;
1307 }
1308
1309 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1310 continue;
1311
b44ee3a8
L
1312 /* NB: Initially, the iplt section has minimal alignment to
1313 avoid moving dot of the following section backwards when
1314 it is empty. Update its section alignment now since it
1315 is non-empty. */
1316 if (s == htab->elf.iplt)
fd361982 1317 bfd_set_section_alignment (s, htab->plt.iplt_alignment);
b44ee3a8 1318
5e2ac45d
L
1319 /* Allocate memory for the section contents. We use bfd_zalloc
1320 here in case unused entries are not reclaimed before the
1321 section's contents are written out. This should not happen,
1322 but this way if it does, we get a R_386_NONE or R_X86_64_NONE
1323 reloc instead of garbage. */
1324 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
1325 if (s->contents == NULL)
0a1b45a2 1326 return false;
5e2ac45d
L
1327 }
1328
1329 if (htab->plt_eh_frame != NULL
1330 && htab->plt_eh_frame->contents != NULL)
1331 {
1332 memcpy (htab->plt_eh_frame->contents,
1333 htab->plt.eh_frame_plt,
1334 htab->plt_eh_frame->size);
1335 bfd_put_32 (dynobj, htab->elf.splt->size,
1336 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
1337 }
1338
1339 if (htab->plt_got_eh_frame != NULL
1340 && htab->plt_got_eh_frame->contents != NULL)
1341 {
1342 memcpy (htab->plt_got_eh_frame->contents,
1343 htab->non_lazy_plt->eh_frame_plt,
1344 htab->plt_got_eh_frame->size);
1345 bfd_put_32 (dynobj, htab->plt_got->size,
1346 (htab->plt_got_eh_frame->contents
1347 + PLT_FDE_LEN_OFFSET));
1348 }
1349
1350 if (htab->plt_second_eh_frame != NULL
1351 && htab->plt_second_eh_frame->contents != NULL)
1352 {
1353 memcpy (htab->plt_second_eh_frame->contents,
1354 htab->non_lazy_plt->eh_frame_plt,
1355 htab->plt_second_eh_frame->size);
1356 bfd_put_32 (dynobj, htab->plt_second->size,
1357 (htab->plt_second_eh_frame->contents
1358 + PLT_FDE_LEN_OFFSET));
1359 }
1360
3084d7a2
L
1361 return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
1362 relocs);
5e2ac45d
L
1363}
1364
9577f60b
L
1365/* Finish up the x86 dynamic sections. */
1366
1367struct elf_x86_link_hash_table *
1368_bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd,
1369 struct bfd_link_info *info)
1370{
1371 struct elf_x86_link_hash_table *htab;
1372 const struct elf_backend_data *bed;
1373 bfd *dynobj;
1374 asection *sdyn;
1375 bfd_byte *dyncon, *dynconend;
1376 bfd_size_type sizeof_dyn;
1377
1378 bed = get_elf_backend_data (output_bfd);
1379 htab = elf_x86_hash_table (info, bed->target_id);
1380 if (htab == NULL)
1381 return htab;
1382
1383 dynobj = htab->elf.dynobj;
1384 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
1385
1386 /* GOT is always created in setup_gnu_properties. But it may not be
1387 needed. .got.plt section may be needed for static IFUNC. */
1388 if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
1389 {
1390 bfd_vma dynamic_addr;
1391
1392 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
1393 {
1394 _bfd_error_handler
871b3ab2 1395 (_("discarded output section: `%pA'"), htab->elf.sgotplt);
9577f60b
L
1396 return NULL;
1397 }
1398
1399 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
1400 = htab->got_entry_size;
1401
1402 dynamic_addr = (sdyn == NULL
1403 ? (bfd_vma) 0
1404 : sdyn->output_section->vma + sdyn->output_offset);
1405
1406 /* Set the first entry in the global offset table to the address
07d6d2b8 1407 of the dynamic section. Write GOT[1] and GOT[2], needed for
9577f60b
L
1408 the dynamic linker. */
1409 if (htab->got_entry_size == 8)
1410 {
1411 bfd_put_64 (output_bfd, dynamic_addr,
1412 htab->elf.sgotplt->contents);
1413 bfd_put_64 (output_bfd, (bfd_vma) 0,
1414 htab->elf.sgotplt->contents + 8);
1415 bfd_put_64 (output_bfd, (bfd_vma) 0,
1416 htab->elf.sgotplt->contents + 8*2);
1417 }
1418 else
1419 {
1420 bfd_put_32 (output_bfd, dynamic_addr,
1421 htab->elf.sgotplt->contents);
1422 bfd_put_32 (output_bfd, 0,
1423 htab->elf.sgotplt->contents + 4);
1424 bfd_put_32 (output_bfd, 0,
1425 htab->elf.sgotplt->contents + 4*2);
1426 }
1427 }
1428
1429 if (!htab->elf.dynamic_sections_created)
1430 return htab;
1431
1432 if (sdyn == NULL || htab->elf.sgot == NULL)
1433 abort ();
1434
1435 sizeof_dyn = bed->s->sizeof_dyn;
1436 dyncon = sdyn->contents;
1437 dynconend = sdyn->contents + sdyn->size;
1438 for (; dyncon < dynconend; dyncon += sizeof_dyn)
1439 {
1440 Elf_Internal_Dyn dyn;
1441 asection *s;
1442
1443 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
1444
1445 switch (dyn.d_tag)
1446 {
1447 default:
90c14f0c 1448 if (htab->elf.target_os == is_vxworks
9577f60b
L
1449 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
1450 break;
1451 continue;
1452
1453 case DT_PLTGOT:
1454 s = htab->elf.sgotplt;
1455 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
1456 break;
1457
1458 case DT_JMPREL:
1459 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
1460 break;
1461
1462 case DT_PLTRELSZ:
1463 s = htab->elf.srelplt->output_section;
1464 dyn.d_un.d_val = s->size;
1465 break;
1466
1467 case DT_TLSDESC_PLT:
1468 s = htab->elf.splt;
1469 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
9bcc30e4 1470 + htab->elf.tlsdesc_plt;
9577f60b
L
1471 break;
1472
1473 case DT_TLSDESC_GOT:
1474 s = htab->elf.sgot;
1475 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
9bcc30e4 1476 + htab->elf.tlsdesc_got;
9577f60b
L
1477 break;
1478 }
1479
1480 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
1481 }
1482
1483 if (htab->plt_got != NULL && htab->plt_got->size > 0)
1484 elf_section_data (htab->plt_got->output_section)
1485 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
1486
1487 if (htab->plt_second != NULL && htab->plt_second->size > 0)
1488 elf_section_data (htab->plt_second->output_section)
1489 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
1490
1491 /* Adjust .eh_frame for .plt section. */
1492 if (htab->plt_eh_frame != NULL
1493 && htab->plt_eh_frame->contents != NULL)
1494 {
1495 if (htab->elf.splt != NULL
1496 && htab->elf.splt->size != 0
1497 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
1498 && htab->elf.splt->output_section != NULL
1499 && htab->plt_eh_frame->output_section != NULL)
1500 {
1501 bfd_vma plt_start = htab->elf.splt->output_section->vma;
1502 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
1503 + htab->plt_eh_frame->output_offset
1504 + PLT_FDE_START_OFFSET;
1505 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
1506 htab->plt_eh_frame->contents
1507 + PLT_FDE_START_OFFSET);
1508 }
1509
1510 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
1511 {
1512 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
1513 htab->plt_eh_frame,
1514 htab->plt_eh_frame->contents))
1515 return NULL;
1516 }
1517 }
1518
1519 /* Adjust .eh_frame for .plt.got section. */
1520 if (htab->plt_got_eh_frame != NULL
1521 && htab->plt_got_eh_frame->contents != NULL)
1522 {
1523 if (htab->plt_got != NULL
1524 && htab->plt_got->size != 0
1525 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
1526 && htab->plt_got->output_section != NULL
1527 && htab->plt_got_eh_frame->output_section != NULL)
1528 {
1529 bfd_vma plt_start = htab->plt_got->output_section->vma;
1530 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
1531 + htab->plt_got_eh_frame->output_offset
1532 + PLT_FDE_START_OFFSET;
1533 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
1534 htab->plt_got_eh_frame->contents
1535 + PLT_FDE_START_OFFSET);
1536 }
1537 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
1538 {
1539 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
1540 htab->plt_got_eh_frame,
1541 htab->plt_got_eh_frame->contents))
1542 return NULL;
1543 }
1544 }
1545
1546 /* Adjust .eh_frame for the second PLT section. */
1547 if (htab->plt_second_eh_frame != NULL
1548 && htab->plt_second_eh_frame->contents != NULL)
1549 {
1550 if (htab->plt_second != NULL
1551 && htab->plt_second->size != 0
1552 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
1553 && htab->plt_second->output_section != NULL
1554 && htab->plt_second_eh_frame->output_section != NULL)
1555 {
1556 bfd_vma plt_start = htab->plt_second->output_section->vma;
1557 bfd_vma eh_frame_start
1558 = (htab->plt_second_eh_frame->output_section->vma
1559 + htab->plt_second_eh_frame->output_offset
1560 + PLT_FDE_START_OFFSET);
1561 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
1562 htab->plt_second_eh_frame->contents
1563 + PLT_FDE_START_OFFSET);
1564 }
1565 if (htab->plt_second_eh_frame->sec_info_type
1566 == SEC_INFO_TYPE_EH_FRAME)
1567 {
1568 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
1569 htab->plt_second_eh_frame,
1570 htab->plt_second_eh_frame->contents))
1571 return NULL;
1572 }
1573 }
1574
1575 if (htab->elf.sgot && htab->elf.sgot->size > 0)
1576 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
1577 = htab->got_entry_size;
1578
1579 return htab;
1580}
1581
1582
0a1b45a2 1583bool
0afcef53
L
1584_bfd_x86_elf_always_size_sections (bfd *output_bfd,
1585 struct bfd_link_info *info)
1586{
1587 asection *tls_sec = elf_hash_table (info)->tls_sec;
1588
1589 if (tls_sec)
1590 {
1591 struct elf_link_hash_entry *tlsbase;
1592
1593 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
1594 "_TLS_MODULE_BASE_",
0a1b45a2 1595 false, false, false);
0afcef53
L
1596
1597 if (tlsbase && tlsbase->type == STT_TLS)
1598 {
1599 struct elf_x86_link_hash_table *htab;
1600 struct bfd_link_hash_entry *bh = NULL;
1601 const struct elf_backend_data *bed
1602 = get_elf_backend_data (output_bfd);
1603
1604 htab = elf_x86_hash_table (info, bed->target_id);
1605 if (htab == NULL)
0a1b45a2 1606 return false;
0afcef53
L
1607
1608 if (!(_bfd_generic_link_add_one_symbol
1609 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
0a1b45a2 1610 tls_sec, 0, NULL, false,
0afcef53 1611 bed->collect, &bh)))
0a1b45a2 1612 return false;
0afcef53
L
1613
1614 htab->tls_module_base = bh;
1615
1616 tlsbase = (struct elf_link_hash_entry *)bh;
1617 tlsbase->def_regular = 1;
1618 tlsbase->other = STV_HIDDEN;
1619 tlsbase->root.linker_def = 1;
0a1b45a2 1620 (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
0afcef53
L
1621 }
1622 }
1623
0a1b45a2 1624 return true;
0afcef53
L
1625}
1626
1627void
1628_bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5160d0f3 1629 unsigned int st_other,
0a1b45a2
AM
1630 bool definition,
1631 bool dynamic ATTRIBUTE_UNUSED)
0afcef53
L
1632{
1633 if (definition)
1634 {
1635 struct elf_x86_link_hash_entry *eh
1636 = (struct elf_x86_link_hash_entry *) h;
5160d0f3 1637 eh->def_protected = ELF_ST_VISIBILITY (st_other) == STV_PROTECTED;
0afcef53
L
1638 }
1639}
1640
1641/* Copy the extra info we tack onto an elf_link_hash_entry. */
1642
1643void
1644_bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
1645 struct elf_link_hash_entry *dir,
1646 struct elf_link_hash_entry *ind)
1647{
1648 struct elf_x86_link_hash_entry *edir, *eind;
1649
1650 edir = (struct elf_x86_link_hash_entry *) dir;
1651 eind = (struct elf_x86_link_hash_entry *) ind;
1652
0afcef53
L
1653 if (ind->root.type == bfd_link_hash_indirect
1654 && dir->got.refcount <= 0)
1655 {
1656 edir->tls_type = eind->tls_type;
1657 eind->tls_type = GOT_UNKNOWN;
1658 }
1659
1660 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
1661 generate a R_386_COPY reloc. */
1662 edir->gotoff_ref |= eind->gotoff_ref;
1663
98b273dc 1664 edir->zero_undefweak |= eind->zero_undefweak;
0afcef53
L
1665
1666 if (ELIMINATE_COPY_RELOCS
1667 && ind->root.type != bfd_link_hash_indirect
1668 && dir->dynamic_adjusted)
1669 {
1670 /* If called to transfer flags for a weakdef during processing
1671 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1672 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1673 if (dir->versioned != versioned_hidden)
1674 dir->ref_dynamic |= ind->ref_dynamic;
1675 dir->ref_regular |= ind->ref_regular;
1676 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1677 dir->needs_plt |= ind->needs_plt;
1678 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1679 }
1680 else
79b0c981 1681 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
0afcef53
L
1682}
1683
1684/* Remove undefined weak symbol from the dynamic symbol table if it
1685 is resolved to 0. */
1686
0a1b45a2 1687bool
0afcef53
L
1688_bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
1689 struct elf_link_hash_entry *h)
1690{
c5bce5c6
L
1691 if (h->dynindx != -1
1692 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, elf_x86_hash_entry (h)))
0afcef53 1693 {
c5bce5c6
L
1694 h->dynindx = -1;
1695 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1696 h->dynstr_index);
0afcef53 1697 }
0a1b45a2 1698 return true;
0afcef53
L
1699}
1700
4ec09950
L
1701/* Change the STT_GNU_IFUNC symbol defined in position-dependent
1702 executable into the normal function symbol and set its address
1703 to its PLT entry, which should be resolved by R_*_IRELATIVE at
1704 run-time. */
1705
1706void
1707_bfd_x86_elf_link_fixup_ifunc_symbol (struct bfd_link_info *info,
1708 struct elf_x86_link_hash_table *htab,
1709 struct elf_link_hash_entry *h,
1710 Elf_Internal_Sym *sym)
1711{
1712 if (bfd_link_pde (info)
1713 && h->def_regular
1714 && h->dynindx != -1
1715 && h->plt.offset != (bfd_vma) -1
75a933f3 1716 && h->type == STT_GNU_IFUNC)
4ec09950
L
1717 {
1718 asection *plt_s;
1719 bfd_vma plt_offset;
1720 bfd *output_bfd = info->output_bfd;
1721
1722 if (htab->plt_second)
1723 {
1724 struct elf_x86_link_hash_entry *eh
1725 = (struct elf_x86_link_hash_entry *) h;
1726
1727 plt_s = htab->plt_second;
1728 plt_offset = eh->plt_second.offset;
1729 }
1730 else
1731 {
1732 plt_s = htab->elf.splt;
1733 plt_offset = h->plt.offset;
1734 }
1735
1736 sym->st_size = 0;
1737 sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
1738 sym->st_shndx
1739 = _bfd_elf_section_from_bfd_section (output_bfd,
1740 plt_s->output_section);
1741 sym->st_value = (plt_s->output_section->vma
1742 + plt_s->output_offset + plt_offset);
1743 }
1744}
1745
68b00778
L
1746/* Report relative relocation. */
1747
1748void
1749_bfd_x86_elf_link_report_relative_reloc
1750 (struct bfd_link_info *info, asection *asect,
1751 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym,
1752 const char *reloc_name, const void *reloc)
1753{
1754 const char *name;
1755 bfd *abfd;
1756 const Elf_Internal_Rela *rel = (const Elf_Internal_Rela *) reloc;
1757 char r_offset[30], r_info[30];
1758
1759 /* Use the output BFD for linker created sections. */
1760 if ((asect->flags & SEC_LINKER_CREATED) != 0)
1761 abfd = info->output_bfd;
1762 else
1763 abfd = asect->owner;
1764
1765 if (h != NULL && h->root.root.string != NULL)
1766 name = h->root.root.string;
1767 else
1768 name = bfd_elf_sym_name (abfd, &elf_symtab_hdr (abfd), sym, NULL);
1769
1770 bfd_sprintf_vma (abfd, r_offset, rel->r_offset);
1771 bfd_sprintf_vma (abfd, r_info, rel->r_info);
1772
1773 if (asect->use_rela_p)
1774 {
1775 char r_addend[30];
1776
1777 bfd_sprintf_vma (abfd, r_addend, rel->r_addend);
1778
1779 info->callbacks->einfo
1780 (_("%pB: %s (offset: 0x%s, info: 0x%s, addend: 0x%s) against "
1781 "'%s' " "for section '%pA' in %pB\n"),
1782 info->output_bfd, reloc_name, r_offset, r_info, r_addend,
1783 name, asect, abfd);
1784 }
1785 else
1786 info->callbacks->einfo
1787 (_("%pB: %s (offset: 0x%s, info: 0x%s) against '%s' for section "
1788 "'%pA' in %pB\n"),
1789 info->output_bfd, reloc_name, r_offset, r_info, name,
1790 asect, abfd);
1791}
1792
0afcef53
L
1793/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
1794
0a1b45a2 1795bool
0afcef53
L
1796_bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
1797{
1798 if (h->plt.offset != (bfd_vma) -1
1799 && !h->def_regular
1800 && !h->pointer_equality_needed)
0a1b45a2 1801 return false;
0afcef53
L
1802
1803 return _bfd_elf_hash_symbol (h);
1804}
1805
eeb2f20a
L
1806/* Adjust a symbol defined by a dynamic object and referenced by a
1807 regular object. The current definition is in some section of the
1808 dynamic object, but we're not including those sections. We have to
1809 change the definition to something the rest of the link can
1810 understand. */
1811
0a1b45a2 1812bool
eeb2f20a
L
1813_bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1814 struct elf_link_hash_entry *h)
1815{
1816 struct elf_x86_link_hash_table *htab;
1817 asection *s, *srel;
1818 struct elf_x86_link_hash_entry *eh;
1819 struct elf_dyn_relocs *p;
1820 const struct elf_backend_data *bed
1821 = get_elf_backend_data (info->output_bfd);
1822
406b4ada
L
1823 eh = (struct elf_x86_link_hash_entry *) h;
1824
6f365fda
L
1825 /* Clear GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS if it is turned
1826 on by an input relocatable file and there is a non-GOT/non-PLT
1827 reference from another relocatable file without it.
1828 NB: There can be non-GOT reference in data sections in input with
1829 GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS. */
1830 if (eh->non_got_ref_without_indirect_extern_access
1831 && info->indirect_extern_access == 1
1832 && bfd_link_executable (info))
1833 {
1834 unsigned int needed_1;
1835 info->indirect_extern_access = 0;
1836 /* Turn off nocopyreloc if implied by indirect_extern_access. */
1837 if (info->nocopyreloc == 2)
1838 info->nocopyreloc = 0;
1839 needed_1 = bfd_h_get_32 (info->output_bfd, info->needed_1_p);
1840 needed_1 &= ~GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
1841 bfd_h_put_32 (info->output_bfd, needed_1, info->needed_1_p);
1842 }
1843
eeb2f20a
L
1844 /* STT_GNU_IFUNC symbol must go through PLT. */
1845 if (h->type == STT_GNU_IFUNC)
1846 {
1847 /* All local STT_GNU_IFUNC references must be treate as local
1848 calls via local PLT. */
1849 if (h->ref_regular
1850 && SYMBOL_CALLS_LOCAL (info, h))
1851 {
1852 bfd_size_type pc_count = 0, count = 0;
1853 struct elf_dyn_relocs **pp;
1854
1855 eh = (struct elf_x86_link_hash_entry *) h;
190eb1dd 1856 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
eeb2f20a
L
1857 {
1858 pc_count += p->pc_count;
1859 p->count -= p->pc_count;
1860 p->pc_count = 0;
1861 count += p->count;
1862 if (p->count == 0)
1863 *pp = p->next;
1864 else
1865 pp = &p->next;
1866 }
1867
1868 if (pc_count || count)
1869 {
1870 h->non_got_ref = 1;
1871 if (pc_count)
1872 {
1873 /* Increment PLT reference count only for PC-relative
1874 references. */
1875 h->needs_plt = 1;
1876 if (h->plt.refcount <= 0)
1877 h->plt.refcount = 1;
1878 else
1879 h->plt.refcount += 1;
1880 }
1881 }
406b4ada
L
1882
1883 /* GOTOFF relocation needs PLT. */
1884 if (eh->gotoff_ref)
1885 h->plt.refcount = 1;
eeb2f20a
L
1886 }
1887
1888 if (h->plt.refcount <= 0)
1889 {
1890 h->plt.offset = (bfd_vma) -1;
1891 h->needs_plt = 0;
1892 }
0a1b45a2 1893 return true;
eeb2f20a
L
1894 }
1895
1896 /* If this is a function, put it in the procedure linkage table. We
1897 will fill in the contents of the procedure linkage table later,
1898 when we know the address of the .got section. */
1899 if (h->type == STT_FUNC
1900 || h->needs_plt)
1901 {
1902 if (h->plt.refcount <= 0
1903 || SYMBOL_CALLS_LOCAL (info, h)
1904 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1905 && h->root.type == bfd_link_hash_undefweak))
1906 {
1907 /* This case can occur if we saw a PLT32 reloc in an input
1908 file, but the symbol was never referred to by a dynamic
1909 object, or if all references were garbage collected. In
1910 such a case, we don't actually need to build a procedure
1911 linkage table, and we can just do a PC32 reloc instead. */
1912 h->plt.offset = (bfd_vma) -1;
1913 h->needs_plt = 0;
1914 }
1915
0a1b45a2 1916 return true;
eeb2f20a
L
1917 }
1918 else
1919 /* It's possible that we incorrectly decided a .plt reloc was needed
1920 * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
1921 check_relocs. We can't decide accurately between function and
1922 non-function syms in check-relocs; Objects loaded later in
1923 the link may change h->type. So fix it now. */
1924 h->plt.offset = (bfd_vma) -1;
1925
eeb2f20a
L
1926 /* If this is a weak symbol, and there is a real definition, the
1927 processor independent code will have arranged for us to see the
1928 real definition first, and we can just use the same value. */
60d67dc8 1929 if (h->is_weakalias)
eeb2f20a 1930 {
60d67dc8
AM
1931 struct elf_link_hash_entry *def = weakdef (h);
1932 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1933 h->root.u.def.section = def->root.u.def.section;
1934 h->root.u.def.value = def->root.u.def.value;
eeb2f20a
L
1935 if (ELIMINATE_COPY_RELOCS
1936 || info->nocopyreloc
1937 || SYMBOL_NO_COPYRELOC (info, eh))
1938 {
1939 /* NB: needs_copy is always 0 for i386. */
60d67dc8
AM
1940 h->non_got_ref = def->non_got_ref;
1941 eh->needs_copy = def->needs_copy;
eeb2f20a 1942 }
0a1b45a2 1943 return true;
eeb2f20a
L
1944 }
1945
1946 /* This is a reference to a symbol defined by a dynamic object which
1947 is not a function. */
1948
1949 /* If we are creating a shared library, we must presume that the
1950 only references to the symbol are via the global offset table.
1951 For such cases we need not do anything here; the relocations will
1952 be handled correctly by relocate_section. */
1953 if (!bfd_link_executable (info))
0a1b45a2 1954 return true;
eeb2f20a
L
1955
1956 /* If there are no references to this symbol that do not use the
1957 GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
1958 reloc. NB: gotoff_ref is always 0 for x86-64. */
1959 if (!h->non_got_ref && !eh->gotoff_ref)
0a1b45a2 1960 return true;
eeb2f20a
L
1961
1962 /* If -z nocopyreloc was given, we won't generate them either. */
1963 if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
1964 {
1965 h->non_got_ref = 0;
0a1b45a2 1966 return true;
eeb2f20a
L
1967 }
1968
1969 htab = elf_x86_hash_table (info, bed->target_id);
1970 if (htab == NULL)
0a1b45a2 1971 return false;
eeb2f20a
L
1972
1973 /* If there aren't any dynamic relocs in read-only sections nor
1974 R_386_GOTOFF relocation, then we can keep the dynamic relocs and
1975 avoid the copy reloc. This doesn't work on VxWorks, where we can
1976 not have dynamic relocations (other than copy and jump slot
1977 relocations) in an executable. */
1978 if (ELIMINATE_COPY_RELOCS
1979 && (bed->target_id == X86_64_ELF_DATA
1980 || (!eh->gotoff_ref
90c14f0c 1981 && htab->elf.target_os != is_vxworks)))
eeb2f20a 1982 {
826c3f1e 1983 /* If we don't find any dynamic relocs in read-only sections,
eeb2f20a
L
1984 then we'll be keeping the dynamic relocs and avoiding the copy
1985 reloc. */
5dbc8b37 1986 if (!_bfd_elf_readonly_dynrelocs (h))
eeb2f20a
L
1987 {
1988 h->non_got_ref = 0;
0a1b45a2 1989 return true;
eeb2f20a
L
1990 }
1991 }
1992
1993 /* We must allocate the symbol in our .dynbss section, which will
1994 become part of the .bss section of the executable. There will be
1995 an entry for this symbol in the .dynsym section. The dynamic
1996 object will contain position independent code, so all references
1997 from the dynamic object to this symbol will go through the global
1998 offset table. The dynamic linker will use the .dynsym entry to
1999 determine the address it must put in the global offset table, so
2000 both the dynamic object and the regular object will refer to the
2001 same memory location for the variable. */
2002
2003 /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
2004 dynamic linker to copy the initial value out of the dynamic object
2005 and into the runtime process image. */
2006 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2007 {
2008 s = htab->elf.sdynrelro;
2009 srel = htab->elf.sreldynrelro;
2010 }
2011 else
2012 {
2013 s = htab->elf.sdynbss;
2014 srel = htab->elf.srelbss;
2015 }
2016 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2017 {
503294e7 2018 srel->size += htab->sizeof_reloc;
eeb2f20a
L
2019 h->needs_copy = 1;
2020 }
2021
2022 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2023}
2024
9f857535
L
2025void
2026_bfd_x86_elf_hide_symbol (struct bfd_link_info *info,
2027 struct elf_link_hash_entry *h,
0a1b45a2 2028 bool force_local)
9f857535
L
2029{
2030 if (h->root.type == bfd_link_hash_undefweak
2031 && info->nointerp
2032 && bfd_link_pie (info))
2033 {
2034 /* When there is no dynamic interpreter in PIE, make the undefined
2035 weak symbol dynamic so that PC relative branch to the undefined
2036 weak symbol will land to address 0. */
2037 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
79b0c981 2038 if (h->plt.refcount > 0
9f857535
L
2039 || eh->plt_got.refcount > 0)
2040 return;
2041 }
2042
2043 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
2044}
2045
6999821f
L
2046/* Return TRUE if a symbol is referenced locally. It is similar to
2047 SYMBOL_REFERENCES_LOCAL, but it also checks version script. It
2048 works in check_relocs. */
2049
0a1b45a2 2050bool
6999821f
L
2051_bfd_x86_elf_link_symbol_references_local (struct bfd_link_info *info,
2052 struct elf_link_hash_entry *h)
2053{
0a27fed7
L
2054 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
2055 struct elf_x86_link_hash_table *htab
2056 = (struct elf_x86_link_hash_table *) info->hash;
6999821f
L
2057
2058 if (eh->local_ref > 1)
0a1b45a2 2059 return true;
6999821f
L
2060
2061 if (eh->local_ref == 1)
0a1b45a2 2062 return false;
6999821f
L
2063
2064 /* Unversioned symbols defined in regular objects can be forced local
0a27fed7
L
2065 by linker version script. A weak undefined symbol is forced local
2066 if
2067 1. It has non-default visibility. Or
8fbf0ba1 2068 2. When building executable, there is no dynamic linker. Or
0a27fed7
L
2069 3. or "-z nodynamic-undefined-weak" is used.
2070 */
6999821f 2071 if (SYMBOL_REFERENCES_LOCAL (info, h)
0a27fed7
L
2072 || (h->root.type == bfd_link_hash_undefweak
2073 && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2074 || (bfd_link_executable (info)
8fbf0ba1 2075 && htab->interp == NULL)
0a27fed7 2076 || info->dynamic_undefined_weak == 0))
6999821f 2077 || ((h->def_regular || ELF_COMMON_DEF_P (h))
6999821f 2078 && info->version_info != NULL
099bb8fb 2079 && _bfd_elf_link_hide_sym_by_version (info, h)))
6999821f
L
2080 {
2081 eh->local_ref = 2;
0a1b45a2 2082 return true;
6999821f
L
2083 }
2084
099bb8fb 2085 eh->local_ref = 1;
0a1b45a2 2086 return false;
6999821f
L
2087}
2088
4f501a24
L
2089/* Return the section that should be marked against GC for a given
2090 relocation. */
2091
2092asection *
2093_bfd_x86_elf_gc_mark_hook (asection *sec,
2094 struct bfd_link_info *info,
2095 Elf_Internal_Rela *rel,
2096 struct elf_link_hash_entry *h,
2097 Elf_Internal_Sym *sym)
2098{
2099 /* Compiler should optimize this out. */
2100 if (((unsigned int) R_X86_64_GNU_VTINHERIT
2101 != (unsigned int) R_386_GNU_VTINHERIT)
2102 || ((unsigned int) R_X86_64_GNU_VTENTRY
2103 != (unsigned int) R_386_GNU_VTENTRY))
2104 abort ();
2105
2106 if (h != NULL)
2107 switch (ELF32_R_TYPE (rel->r_info))
2108 {
2109 case R_X86_64_GNU_VTINHERIT:
2110 case R_X86_64_GNU_VTENTRY:
2111 return NULL;
2112 }
2113
2114 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2115}
2116
f493882d
L
2117static bfd_vma
2118elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
2119 bfd_vma off,
2120 bfd_vma offset ATTRIBUTE_UNUSED,
2121 bfd_vma got_addr)
2122{
2123 return got_addr + off;
2124}
2125
2126static bfd_vma
2127elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
2128 bfd_vma off,
2129 bfd_vma offset,
2130 bfd_vma got_addr ATTRIBUTE_UNUSED)
2131{
2132 return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
2133}
2134
0a1b45a2 2135static bool
f493882d
L
2136elf_i386_valid_plt_reloc_p (unsigned int type)
2137{
2138 return (type == R_386_JUMP_SLOT
2139 || type == R_386_GLOB_DAT
2140 || type == R_386_IRELATIVE);
2141}
2142
0a1b45a2 2143static bool
f493882d
L
2144elf_x86_64_valid_plt_reloc_p (unsigned int type)
2145{
2146 return (type == R_X86_64_JUMP_SLOT
2147 || type == R_X86_64_GLOB_DAT
2148 || type == R_X86_64_IRELATIVE);
2149}
2150
2151long
2152_bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
2153 long count,
2154 long relsize,
2155 bfd_vma got_addr,
2156 struct elf_x86_plt plts[],
2157 asymbol **dynsyms,
2158 asymbol **ret)
2159{
2160 long size, i, n, len;
2161 int j;
2162 unsigned int plt_got_offset, plt_entry_size;
2163 asymbol *s;
2164 bfd_byte *plt_contents;
2165 long dynrelcount;
2166 arelent **dynrelbuf, *p;
2167 char *names;
2168 const struct elf_backend_data *bed;
2169 bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
2170 bfd_vma);
0a1b45a2 2171 bool (*valid_plt_reloc_p) (unsigned int);
f493882d 2172
2600d80c 2173 dynrelbuf = NULL;
f493882d 2174 if (count == 0)
2600d80c 2175 goto bad_return;
f493882d
L
2176
2177 dynrelbuf = (arelent **) bfd_malloc (relsize);
2178 if (dynrelbuf == NULL)
2600d80c 2179 goto bad_return;
f493882d
L
2180
2181 dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
2182 dynsyms);
b69e9267 2183 if (dynrelcount <= 0)
2600d80c 2184 goto bad_return;
f493882d
L
2185
2186 /* Sort the relocs by address. */
2187 qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
2188 _bfd_x86_elf_compare_relocs);
2189
2190 size = count * sizeof (asymbol);
2191
2192 /* Allocate space for @plt suffixes. */
2193 n = 0;
2194 for (i = 0; i < dynrelcount; i++)
2195 {
2196 p = dynrelbuf[i];
2197 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2198 if (p->addend != 0)
2199 size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
2200 }
2201
2202 s = *ret = (asymbol *) bfd_zmalloc (size);
2203 if (s == NULL)
2204 goto bad_return;
2205
2206 bed = get_elf_backend_data (abfd);
2207
2208 if (bed->target_id == X86_64_ELF_DATA)
2209 {
2210 get_plt_got_vma = elf_x86_64_get_plt_got_vma;
2211 valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
2212 }
2213 else
2214 {
2215 get_plt_got_vma = elf_i386_get_plt_got_vma;
2216 valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
2217 if (got_addr)
2218 {
2219 /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
2220 address. */
2221 asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
2222 if (sec != NULL)
2223 got_addr = sec->vma;
2224 else
2225 {
2226 sec = bfd_get_section_by_name (abfd, ".got");
2227 if (sec != NULL)
2228 got_addr = sec->vma;
2229 }
2230
2231 if (got_addr == (bfd_vma) -1)
2232 goto bad_return;
2233 }
2234 }
2235
2236 /* Check for each PLT section. */
2237 names = (char *) (s + count);
2238 size = 0;
2239 n = 0;
2240 for (j = 0; plts[j].name != NULL; j++)
2241 if ((plt_contents = plts[j].contents) != NULL)
2242 {
2243 long k;
2244 bfd_vma offset;
2245 asection *plt;
2246 struct elf_x86_plt *plt_p = &plts[j];
2247
2248 plt_got_offset = plt_p->plt_got_offset;
2249 plt_entry_size = plt_p->plt_entry_size;
2250
2251 plt = plt_p->sec;
2252
2253 if ((plt_p->type & plt_lazy))
2254 {
2255 /* Skip PLT0 in lazy PLT. */
2256 k = 1;
2257 offset = plt_entry_size;
2258 }
2259 else
2260 {
2261 k = 0;
2262 offset = 0;
2263 }
2264
2265 /* Check each PLT entry against dynamic relocations. */
2266 for (; k < plt_p->count; k++)
2267 {
2268 int off;
2269 bfd_vma got_vma;
2270 long min, max, mid;
2271
2272 /* Get the GOT offset for i386 or the PC-relative offset
2273 for x86-64, a signed 32-bit integer. */
2274 off = H_GET_32 (abfd, (plt_contents + offset
2275 + plt_got_offset));
2276 got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
2277
2278 /* Binary search. */
2279 p = dynrelbuf[0];
2280 min = 0;
2281 max = dynrelcount;
2282 while ((min + 1) < max)
2283 {
2284 arelent *r;
2285
2286 mid = (min + max) / 2;
2287 r = dynrelbuf[mid];
2288 if (got_vma > r->address)
2289 min = mid;
2290 else if (got_vma < r->address)
2291 max = mid;
2292 else
2293 {
2294 p = r;
2295 break;
2296 }
2297 }
2298
2299 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
2300 if (got_vma == p->address
2301 && p->howto != NULL
2302 && valid_plt_reloc_p (p->howto->type))
2303 {
2304 *s = **p->sym_ptr_ptr;
2305 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
2306 set. Since we are defining a symbol, ensure one
2307 of them is set. */
2308 if ((s->flags & BSF_LOCAL) == 0)
2309 s->flags |= BSF_GLOBAL;
2310 s->flags |= BSF_SYNTHETIC;
2311 /* This is no longer a section symbol. */
2312 s->flags &= ~BSF_SECTION_SYM;
2313 s->section = plt;
2314 s->the_bfd = plt->owner;
2315 s->value = offset;
2316 s->udata.p = NULL;
2317 s->name = names;
2318 len = strlen ((*p->sym_ptr_ptr)->name);
2319 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2320 names += len;
2321 if (p->addend != 0)
2322 {
2323 char buf[30], *a;
2324
2325 memcpy (names, "+0x", sizeof ("+0x") - 1);
2326 names += sizeof ("+0x") - 1;
2327 bfd_sprintf_vma (abfd, buf, p->addend);
2328 for (a = buf; *a == '0'; ++a)
2329 ;
2330 size = strlen (a);
2331 memcpy (names, a, size);
2332 names += size;
2333 }
2334 memcpy (names, "@plt", sizeof ("@plt"));
2335 names += sizeof ("@plt");
2336 n++;
2337 s++;
61e3bf5f
L
2338 /* There should be only one entry in PLT for a given
2339 symbol. Set howto to NULL after processing a PLT
2340 entry to guard against corrupted PLT. */
2341 p->howto = NULL;
f493882d
L
2342 }
2343 offset += plt_entry_size;
2344 }
2345 }
2346
2347 /* PLT entries with R_386_TLS_DESC relocations are skipped. */
2348 if (n == 0)
2349 {
dc1e8a47 2350 bad_return:
f493882d
L
2351 count = -1;
2352 }
2353 else
2354 count = n;
2355
2356 for (j = 0; plts[j].name != NULL; j++)
c9594989 2357 free (plts[j].contents);
f493882d
L
2358
2359 free (dynrelbuf);
2360
2361 return count;
2362}
2363
0afcef53
L
2364/* Parse x86 GNU properties. */
2365
2366enum elf_property_kind
2367_bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
2368 bfd_byte *ptr, unsigned int datasz)
2369{
2370 elf_property *prop;
2371
a9eafb08
L
2372 if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
2373 || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
2374 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
2375 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
2376 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
2377 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
2378 || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
2379 && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
0afcef53 2380 {
0afcef53
L
2381 if (datasz != 4)
2382 {
2383 _bfd_error_handler
a9eafb08
L
2384 (_("error: %pB: <corrupt x86 property (0x%x) size: 0x%x>"),
2385 abfd, type, datasz);
0afcef53
L
2386 return property_corrupt;
2387 }
2388 prop = _bfd_elf_get_property (abfd, type, datasz);
1273b2f8 2389 prop->u.number |= bfd_h_get_32 (abfd, ptr);
0afcef53 2390 prop->pr_kind = property_number;
a9eafb08 2391 return property_number;
0afcef53
L
2392 }
2393
a9eafb08 2394 return property_ignored;
0afcef53
L
2395}
2396
2397/* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
2398 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
2399 should be merged with ABFD. */
2400
0a1b45a2 2401bool
0afcef53
L
2402_bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
2403 bfd *abfd ATTRIBUTE_UNUSED,
4e539114 2404 bfd *bbfd ATTRIBUTE_UNUSED,
0afcef53
L
2405 elf_property *aprop,
2406 elf_property *bprop)
2407{
2408 unsigned int number, features;
0a1b45a2 2409 bool updated = false;
32930e4e
L
2410 const struct elf_backend_data *bed;
2411 struct elf_x86_link_hash_table *htab;
0afcef53
L
2412 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
2413
a9eafb08
L
2414 if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
2415 || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
2416 && pr_type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
0afcef53 2417 {
f7309df2
L
2418 if (aprop == NULL || bprop == NULL)
2419 {
2420 /* Only one of APROP and BPROP can be NULL. */
2421 if (aprop != NULL)
2422 {
2423 /* Remove this property since the other input file doesn't
2424 have it. */
2425 aprop->pr_kind = property_remove;
0a1b45a2 2426 updated = true;
f7309df2 2427 }
f7309df2 2428 }
fde51dd1
L
2429 else
2430 {
2431 number = aprop->u.number;
2432 aprop->u.number = number | bprop->u.number;
2433 updated = number != (unsigned int) aprop->u.number;
2434 }
2435 return updated;
a9eafb08
L
2436 }
2437 else if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
2438 || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_LO
2439 && pr_type <= GNU_PROPERTY_X86_UINT32_OR_HI))
2440 {
32930e4e
L
2441 features = 0;
2442 if (pr_type == GNU_PROPERTY_X86_ISA_1_NEEDED)
2443 {
2444 bed = get_elf_backend_data (info->output_bfd);
2445 htab = elf_x86_hash_table (info, bed->target_id);
2446 switch (htab->params->isa_level)
2447 {
2448 case 0:
2449 break;
2450 case 2:
2451 features = GNU_PROPERTY_X86_ISA_1_V2;
2452 break;
2453 case 3:
2454 features = GNU_PROPERTY_X86_ISA_1_V3;
2455 break;
2456 case 4:
2457 features = GNU_PROPERTY_X86_ISA_1_V4;
2458 break;
2459 default:
2460 abort ();
2461 }
2462 }
0afcef53
L
2463 if (aprop != NULL && bprop != NULL)
2464 {
2465 number = aprop->u.number;
32930e4e 2466 aprop->u.number = number | bprop->u.number | features;
f7309df2 2467 /* Remove the property if all bits are empty. */
56ad703d
L
2468 if (aprop->u.number == 0)
2469 {
2470 aprop->pr_kind = property_remove;
0a1b45a2 2471 updated = true;
56ad703d
L
2472 }
2473 else
2474 updated = number != (unsigned int) aprop->u.number;
0afcef53
L
2475 }
2476 else
2477 {
56ad703d
L
2478 /* Only one of APROP and BPROP can be NULL. */
2479 if (aprop != NULL)
2480 {
32930e4e 2481 aprop->u.number |= features;
56ad703d
L
2482 if (aprop->u.number == 0)
2483 {
f7309df2 2484 /* Remove APROP if all bits are empty. */
56ad703d 2485 aprop->pr_kind = property_remove;
0a1b45a2 2486 updated = true;
56ad703d
L
2487 }
2488 }
2489 else
2490 {
f7309df2 2491 /* Return TRUE if APROP is NULL and all bits of BPROP
56ad703d
L
2492 aren't empty to indicate that BPROP should be added
2493 to ABFD. */
32930e4e 2494 bprop->u.number |= features;
56ad703d
L
2495 updated = bprop->u.number != 0;
2496 }
0afcef53 2497 }
a9eafb08
L
2498 return updated;
2499 }
2500 else if (pr_type >= GNU_PROPERTY_X86_UINT32_AND_LO
2501 && pr_type <= GNU_PROPERTY_X86_UINT32_AND_HI)
2502 {
0afcef53
L
2503 /* Only one of APROP and BPROP can be NULL:
2504 1. APROP & BPROP when both APROP and BPROP aren't NULL.
2505 2. If APROP is NULL, remove x86 feature.
2506 3. Otherwise, do nothing.
2507 */
32930e4e
L
2508 bed = get_elf_backend_data (info->output_bfd);
2509 htab = elf_x86_hash_table (info, bed->target_id);
5b9c07b2
L
2510 if (!htab)
2511 abort ();
0afcef53
L
2512 if (aprop != NULL && bprop != NULL)
2513 {
0afcef53 2514 number = aprop->u.number;
574df58f
L
2515 aprop->u.number = number & bprop->u.number;
2516 if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
2517 {
2518 features = 0;
2519 if (htab->params->ibt)
2520 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
2521 if (htab->params->shstk)
2522 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
0a94990b
L
2523 if (htab->params->lam_u48)
2524 features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
2525 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
2526 else if (htab->params->lam_u57)
2527 features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
2528 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT,
2529 GNU_PROPERTY_X86_FEATURE_1_SHSTK,
2530 GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
2531 GNU_PROPERTY_X86_FEATURE_1_LAM_U57. */
574df58f
L
2532 aprop->u.number |= features;
2533 }
0afcef53
L
2534 updated = number != (unsigned int) aprop->u.number;
2535 /* Remove the property if all feature bits are cleared. */
2536 if (aprop->u.number == 0)
2537 aprop->pr_kind = property_remove;
2538 }
2539 else
2540 {
44b27f95
L
2541 /* There should be no AND properties since some input doesn't
2542 have them. Set IBT and SHSTK properties for -z ibt and -z
2543 shstk if needed. */
0afcef53 2544 features = 0;
574df58f
L
2545 if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
2546 {
2547 if (htab->params->ibt)
2548 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
2549 if (htab->params->shstk)
2550 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
0a94990b
L
2551 if (htab->params->lam_u48)
2552 features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
2553 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
2554 else if (htab->params->lam_u57)
2555 features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
574df58f 2556 }
0afcef53
L
2557 if (features)
2558 {
0afcef53
L
2559 if (aprop != NULL)
2560 {
44b27f95
L
2561 updated = features != (unsigned int) aprop->u.number;
2562 aprop->u.number = features;
0afcef53
L
2563 }
2564 else
2565 {
0a1b45a2 2566 updated = true;
44b27f95 2567 bprop->u.number = features;
0afcef53
L
2568 }
2569 }
2570 else if (aprop != NULL)
2571 {
2572 aprop->pr_kind = property_remove;
0a1b45a2 2573 updated = true;
0afcef53
L
2574 }
2575 }
a9eafb08
L
2576 return updated;
2577 }
2578 else
2579 {
0afcef53
L
2580 /* Never should happen. */
2581 abort ();
2582 }
2583
2584 return updated;
2585}
a6798bab
L
2586
2587/* Set up x86 GNU properties. Return the first relocatable ELF input
2588 with GNU properties if found. Otherwise, return NULL. */
2589
2590bfd *
2591_bfd_x86_elf_link_setup_gnu_properties
1de031c8 2592 (struct bfd_link_info *info, struct elf_x86_init_table *init_table)
a6798bab 2593{
0a1b45a2
AM
2594 bool normal_target;
2595 bool lazy_plt;
a6798bab
L
2596 asection *sec, *pltsec;
2597 bfd *dynobj;
0a1b45a2 2598 bool use_ibt_plt;
32930e4e 2599 unsigned int plt_alignment, features, isa_level;
a6798bab
L
2600 struct elf_x86_link_hash_table *htab;
2601 bfd *pbfd;
2602 bfd *ebfd = NULL;
2603 elf_property *prop;
2604 const struct elf_backend_data *bed;
2605 unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
2606 unsigned int got_align;
2607
a6798bab
L
2608 /* Find a normal input file with GNU property note. */
2609 for (pbfd = info->input_bfds;
2610 pbfd != NULL;
2611 pbfd = pbfd->link.next)
2612 if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
2613 && bfd_count_sections (pbfd) != 0)
2614 {
2615 ebfd = pbfd;
2616
2617 if (elf_properties (pbfd) != NULL)
2618 break;
2619 }
2620
241e64e3
L
2621 bed = get_elf_backend_data (info->output_bfd);
2622
2623 htab = elf_x86_hash_table (info, bed->target_id);
2624 if (htab == NULL)
2625 return pbfd;
2626
5b9c07b2
L
2627 features = 0;
2628 if (htab->params->ibt)
233a0083
L
2629 {
2630 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
279d901e 2631 htab->params->cet_report &= ~prop_report_ibt;
233a0083 2632 }
5b9c07b2 2633 if (htab->params->shstk)
233a0083
L
2634 {
2635 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
279d901e
L
2636 htab->params->cet_report &= ~prop_report_shstk;
2637 }
2638 if (!(htab->params->cet_report & (prop_report_ibt | prop_report_shstk)))
2639 htab->params->cet_report = prop_report_none;
2640 if (htab->params->lam_u48)
2641 {
ba9e922f
L
2642 features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
2643 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
279d901e 2644 htab->params->lam_u48_report = prop_report_none;
ba9e922f 2645 htab->params->lam_u57_report = prop_report_none;
279d901e 2646 }
ba9e922f 2647 else if (htab->params->lam_u57)
279d901e
L
2648 {
2649 features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
2650 htab->params->lam_u57_report = prop_report_none;
233a0083 2651 }
5b9c07b2 2652
32930e4e
L
2653 switch (htab->params->isa_level)
2654 {
2655 case 0:
2656 isa_level = 0;
2657 break;
b0ab0693
L
2658 case 1:
2659 isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE;
2660 break;
32930e4e
L
2661 case 2:
2662 isa_level = GNU_PROPERTY_X86_ISA_1_V2;
2663 break;
2664 case 3:
2665 isa_level = GNU_PROPERTY_X86_ISA_1_V3;
2666 break;
2667 case 4:
2668 isa_level = GNU_PROPERTY_X86_ISA_1_V4;
2669 break;
2670 default:
2671 abort ();
2672 }
2673
241e64e3 2674 if (ebfd != NULL)
a6798bab 2675 {
241e64e3
L
2676 prop = NULL;
2677 if (features)
2678 {
279d901e
L
2679 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT,
2680 GNU_PROPERTY_X86_FEATURE_1_SHSTK,
2681 GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
2682 GNU_PROPERTY_X86_FEATURE_1_LAM_U57. */
241e64e3
L
2683 prop = _bfd_elf_get_property (ebfd,
2684 GNU_PROPERTY_X86_FEATURE_1_AND,
2685 4);
2686 prop->u.number |= features;
32930e4e
L
2687 prop->pr_kind = property_number;
2688 }
2689
2690 if (isa_level)
2691 {
2692 /* If ISA level is set, add GNU_PROPERTY_X86_ISA_1_NEEDED. */
2693 prop = _bfd_elf_get_property (ebfd,
2694 GNU_PROPERTY_X86_ISA_1_NEEDED,
2695 4);
2696 prop->u.number |= isa_level;
241e64e3
L
2697 prop->pr_kind = property_number;
2698 }
a6798bab
L
2699
2700 /* Create the GNU property note section if needed. */
241e64e3 2701 if (prop != NULL && pbfd == NULL)
a6798bab
L
2702 {
2703 sec = bfd_make_section_with_flags (ebfd,
2704 NOTE_GNU_PROPERTY_SECTION_NAME,
2705 (SEC_ALLOC
2706 | SEC_LOAD
2707 | SEC_IN_MEMORY
2708 | SEC_READONLY
2709 | SEC_HAS_CONTENTS
2710 | SEC_DATA));
2711 if (sec == NULL)
2c244f9b 2712 info->callbacks->einfo (_("%F%P: failed to create GNU property section\n"));
a6798bab 2713
fd361982 2714 if (!bfd_set_section_alignment (sec, class_align))
a6798bab 2715 {
dc1e8a47 2716 error_alignment:
871b3ab2 2717 info->callbacks->einfo (_("%F%pA: failed to align section\n"),
a6798bab
L
2718 sec);
2719 }
2720
2721 elf_section_type (sec) = SHT_NOTE;
2722 }
2723 }
2724
279d901e
L
2725 if (htab->params->cet_report
2726 || htab->params->lam_u48_report
2727 || htab->params->lam_u57_report)
233a0083 2728 {
279d901e 2729 /* Report missing IBT, SHSTK and LAM properties. */
233a0083 2730 bfd *abfd;
279d901e
L
2731 const char *warning_msg = _("%P: %pB: warning: missing %s\n");
2732 const char *error_msg = _("%X%P: %pB: error: missing %s\n");
2733 const char *cet_msg = NULL;
2734 const char *lam_u48_msg = NULL;
2735 const char *lam_u57_msg = NULL;
2736 const char *missing;
233a0083 2737 elf_property_list *p;
0a1b45a2
AM
2738 bool missing_ibt, missing_shstk;
2739 bool missing_lam_u48, missing_lam_u57;
2740 bool check_ibt
279d901e
L
2741 = (htab->params->cet_report
2742 && (htab->params->cet_report & prop_report_ibt));
0a1b45a2 2743 bool check_shstk
279d901e
L
2744 = (htab->params->cet_report
2745 && (htab->params->cet_report & prop_report_shstk));
233a0083 2746
279d901e
L
2747 if (htab->params->cet_report)
2748 {
2749 if ((htab->params->cet_report & prop_report_warning))
2750 cet_msg = warning_msg;
2751 else
2752 cet_msg = error_msg;
2753 }
2754 if (htab->params->lam_u48_report)
2755 {
2756 if ((htab->params->lam_u48_report & prop_report_warning))
2757 lam_u48_msg = warning_msg;
2758 else
2759 lam_u48_msg = error_msg;
2760 }
2761 if (htab->params->lam_u57_report)
2762 {
2763 if ((htab->params->lam_u57_report & prop_report_warning))
2764 lam_u57_msg = warning_msg;
2765 else
2766 lam_u57_msg = error_msg;
2767 }
233a0083
L
2768
2769 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2770 if (!(abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED))
2771 && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2772 {
2773 for (p = elf_properties (abfd); p; p = p->next)
2774 if (p->property.pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
2775 break;
2776
2777 missing_ibt = check_ibt;
2778 missing_shstk = check_shstk;
279d901e
L
2779 missing_lam_u48 = !!lam_u48_msg;
2780 missing_lam_u57 = !!lam_u57_msg;
233a0083
L
2781 if (p)
2782 {
2783 missing_ibt &= !(p->property.u.number
2784 & GNU_PROPERTY_X86_FEATURE_1_IBT);
2785 missing_shstk &= !(p->property.u.number
2786 & GNU_PROPERTY_X86_FEATURE_1_SHSTK);
279d901e
L
2787 missing_lam_u48 &= !(p->property.u.number
2788 & GNU_PROPERTY_X86_FEATURE_1_LAM_U48);
2789 missing_lam_u57 &= !(p->property.u.number
2790 & GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
233a0083
L
2791 }
2792 if (missing_ibt || missing_shstk)
2793 {
233a0083
L
2794 if (missing_ibt && missing_shstk)
2795 missing = _("IBT and SHSTK properties");
2796 else if (missing_ibt)
2797 missing = _("IBT property");
2798 else
2799 missing = _("SHSTK property");
279d901e
L
2800 info->callbacks->einfo (cet_msg, abfd, missing);
2801 }
2802 if (missing_lam_u48)
2803 {
2804 missing = _("LAM_U48 property");
2805 info->callbacks->einfo (lam_u48_msg, abfd, missing);
2806 }
2807 if (missing_lam_u57)
2808 {
2809 missing = _("LAM_U57 property");
2810 info->callbacks->einfo (lam_u57_msg, abfd, missing);
233a0083
L
2811 }
2812 }
2813 }
2814
a6798bab
L
2815 pbfd = _bfd_elf_link_setup_gnu_properties (info);
2816
1de031c8
L
2817 htab->r_info = init_table->r_info;
2818 htab->r_sym = init_table->r_sym;
7a382c1c
L
2819
2820 if (bfd_link_relocatable (info))
2821 return pbfd;
eeb2f20a 2822
851b6fa1
L
2823 htab->plt0_pad_byte = init_table->plt0_pad_byte;
2824
5b9c07b2 2825 use_ibt_plt = htab->params->ibtplt || htab->params->ibt;
a6798bab
L
2826 if (!use_ibt_plt && pbfd != NULL)
2827 {
2828 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
2829 elf_property_list *p;
2830
2831 /* The property list is sorted in order of type. */
2832 for (p = elf_properties (pbfd); p; p = p->next)
2833 {
2834 if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
2835 {
2836 use_ibt_plt = !!(p->property.u.number
2837 & GNU_PROPERTY_X86_FEATURE_1_IBT);
2838 break;
2839 }
2840 else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
2841 break;
2842 }
2843 }
2844
2845 dynobj = htab->elf.dynobj;
2846
2847 /* Set htab->elf.dynobj here so that there is no need to check and
2848 set it in check_relocs. */
2849 if (dynobj == NULL)
2850 {
2851 if (pbfd != NULL)
2852 {
2853 htab->elf.dynobj = pbfd;
2854 dynobj = pbfd;
2855 }
2856 else
2857 {
2858 bfd *abfd;
2859
2860 /* Find a normal input file to hold linker created
2861 sections. */
2862 for (abfd = info->input_bfds;
2863 abfd != NULL;
2864 abfd = abfd->link.next)
2865 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2866 && (abfd->flags
e4e6a73d
L
2867 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
2868 && bed->relocs_compatible (abfd->xvec,
2869 info->output_bfd->xvec))
a6798bab
L
2870 {
2871 htab->elf.dynobj = abfd;
2872 dynobj = abfd;
2873 break;
2874 }
2875 }
2876 }
2877
851b6fa1
L
2878 /* Return if there are no normal input files. */
2879 if (dynobj == NULL)
2880 return pbfd;
2881
a6798bab
L
2882 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
2883 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
2884 canonical function address. */
2885 htab->plt.has_plt0 = 1;
90c14f0c 2886 normal_target = htab->elf.target_os == is_normal;
a6798bab
L
2887
2888 if (normal_target)
2889 {
2890 if (use_ibt_plt)
2891 {
1de031c8
L
2892 htab->lazy_plt = init_table->lazy_ibt_plt;
2893 htab->non_lazy_plt = init_table->non_lazy_ibt_plt;
a6798bab
L
2894 }
2895 else
2896 {
1de031c8
L
2897 htab->lazy_plt = init_table->lazy_plt;
2898 htab->non_lazy_plt = init_table->non_lazy_plt;
a6798bab
L
2899 }
2900 }
2901 else
2902 {
1de031c8 2903 htab->lazy_plt = init_table->lazy_plt;
a6798bab
L
2904 htab->non_lazy_plt = NULL;
2905 }
2906
2907 pltsec = htab->elf.splt;
2908
2909 /* If the non-lazy PLT is available, use it for all PLT entries if
2910 there are no PLT0 or no .plt section. */
2911 if (htab->non_lazy_plt != NULL
2912 && (!htab->plt.has_plt0 || pltsec == NULL))
2913 {
0a1b45a2 2914 lazy_plt = false;
a6798bab
L
2915 if (bfd_link_pic (info))
2916 htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
2917 else
2918 htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
2919 htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
2920 htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
2921 htab->plt.plt_got_insn_size
2922 = htab->non_lazy_plt->plt_got_insn_size;
2923 htab->plt.eh_frame_plt_size
2924 = htab->non_lazy_plt->eh_frame_plt_size;
2925 htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
2926 }
2927 else
2928 {
0a1b45a2 2929 lazy_plt = true;
a6798bab
L
2930 if (bfd_link_pic (info))
2931 {
2932 htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
2933 htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
2934 }
2935 else
2936 {
2937 htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
2938 htab->plt.plt_entry = htab->lazy_plt->plt_entry;
2939 }
2940 htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
2941 htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
2942 htab->plt.plt_got_insn_size
2943 = htab->lazy_plt->plt_got_insn_size;
2944 htab->plt.eh_frame_plt_size
2945 = htab->lazy_plt->eh_frame_plt_size;
2946 htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
2947 }
2948
90c14f0c 2949 if (htab->elf.target_os == is_vxworks
a6798bab
L
2950 && !elf_vxworks_create_dynamic_sections (dynobj, info,
2951 &htab->srelplt2))
2952 {
2c244f9b 2953 info->callbacks->einfo (_("%F%P: failed to create VxWorks dynamic sections\n"));
a6798bab
L
2954 return pbfd;
2955 }
2956
2957 /* Since create_dynamic_sections isn't always called, but GOT
2958 relocations need GOT relocations, create them here so that we
2959 don't need to do it in check_relocs. */
2960 if (htab->elf.sgot == NULL
2961 && !_bfd_elf_create_got_section (dynobj, info))
2c244f9b 2962 info->callbacks->einfo (_("%F%P: failed to create GOT sections\n"));
a6798bab
L
2963
2964 got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
2965
2966 /* Align .got and .got.plt sections to their entry size. Do it here
2967 instead of in create_dynamic_sections so that they are always
2968 properly aligned even if create_dynamic_sections isn't called. */
2969 sec = htab->elf.sgot;
fd361982 2970 if (!bfd_set_section_alignment (sec, got_align))
a6798bab
L
2971 goto error_alignment;
2972
2973 sec = htab->elf.sgotplt;
fd361982 2974 if (!bfd_set_section_alignment (sec, got_align))
a6798bab
L
2975 goto error_alignment;
2976
2977 /* Create the ifunc sections here so that check_relocs can be
2978 simplified. */
2979 if (!_bfd_elf_create_ifunc_sections (dynobj, info))
2c244f9b 2980 info->callbacks->einfo (_("%F%P: failed to create ifunc sections\n"));
a6798bab
L
2981
2982 plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
2983
2984 if (pltsec != NULL)
2985 {
2986 /* Whe creating executable, set the contents of the .interp
2987 section to the interpreter. */
2988 if (bfd_link_executable (info) && !info->nointerp)
2989 {
2990 asection *s = bfd_get_linker_section (dynobj, ".interp");
2991 if (s == NULL)
2992 abort ();
2993 s->size = htab->dynamic_interpreter_size;
2994 s->contents = (unsigned char *) htab->dynamic_interpreter;
2995 htab->interp = s;
2996 }
2997
a6798bab
L
2998 if (normal_target)
2999 {
3000 flagword pltflags = (bed->dynamic_sec_flags
3001 | SEC_ALLOC
3002 | SEC_CODE
3003 | SEC_LOAD
3004 | SEC_READONLY);
3005 unsigned int non_lazy_plt_alignment
3006 = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
3007
3008 sec = pltsec;
fd361982 3009 if (!bfd_set_section_alignment (sec, plt_alignment))
a6798bab
L
3010 goto error_alignment;
3011
3012 /* Create the GOT procedure linkage table. */
3013 sec = bfd_make_section_anyway_with_flags (dynobj,
3014 ".plt.got",
3015 pltflags);
3016 if (sec == NULL)
2c244f9b 3017 info->callbacks->einfo (_("%F%P: failed to create GOT PLT section\n"));
a6798bab 3018
fd361982 3019 if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
a6798bab
L
3020 goto error_alignment;
3021
3022 htab->plt_got = sec;
3023
3024 if (lazy_plt)
3025 {
3026 sec = NULL;
3027
3028 if (use_ibt_plt)
3029 {
3030 /* Create the second PLT for Intel IBT support. IBT
bbd19b19 3031 PLT is needed only for lazy binding. */
a6798bab
L
3032 sec = bfd_make_section_anyway_with_flags (dynobj,
3033 ".plt.sec",
3034 pltflags);
3035 if (sec == NULL)
2c244f9b 3036 info->callbacks->einfo (_("%F%P: failed to create IBT-enabled PLT section\n"));
a6798bab 3037
fd361982 3038 if (!bfd_set_section_alignment (sec, plt_alignment))
a6798bab
L
3039 goto error_alignment;
3040 }
5b9c07b2 3041 else if (htab->params->bndplt && ABI_64_P (dynobj))
a6798bab
L
3042 {
3043 /* Create the second PLT for Intel MPX support. MPX
bbd19b19
L
3044 PLT is supported only in 64-bit mode and is needed
3045 only for lazy binding. */
a6798bab
L
3046 sec = bfd_make_section_anyway_with_flags (dynobj,
3047 ".plt.sec",
3048 pltflags);
3049 if (sec == NULL)
2c244f9b 3050 info->callbacks->einfo (_("%F%P: failed to create BND PLT section\n"));
a6798bab 3051
fd361982 3052 if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
a6798bab
L
3053 goto error_alignment;
3054 }
3055
3056 htab->plt_second = sec;
3057 }
3058 }
3059
3060 if (!info->no_ld_generated_unwind_info)
3061 {
3062 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3063 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3064 | SEC_LINKER_CREATED);
3065
3066 sec = bfd_make_section_anyway_with_flags (dynobj,
3067 ".eh_frame",
3068 flags);
3069 if (sec == NULL)
2c244f9b 3070 info->callbacks->einfo (_("%F%P: failed to create PLT .eh_frame section\n"));
a6798bab 3071
fd361982 3072 if (!bfd_set_section_alignment (sec, class_align))
a6798bab
L
3073 goto error_alignment;
3074
3075 htab->plt_eh_frame = sec;
3076
3077 if (htab->plt_got != NULL)
3078 {
3079 sec = bfd_make_section_anyway_with_flags (dynobj,
3080 ".eh_frame",
3081 flags);
3082 if (sec == NULL)
2c244f9b 3083 info->callbacks->einfo (_("%F%P: failed to create GOT PLT .eh_frame section\n"));
a6798bab 3084
fd361982 3085 if (!bfd_set_section_alignment (sec, class_align))
a6798bab
L
3086 goto error_alignment;
3087
3088 htab->plt_got_eh_frame = sec;
3089 }
3090
3091 if (htab->plt_second != NULL)
3092 {
3093 sec = bfd_make_section_anyway_with_flags (dynobj,
3094 ".eh_frame",
3095 flags);
3096 if (sec == NULL)
2c244f9b 3097 info->callbacks->einfo (_("%F%P: failed to create the second PLT .eh_frame section\n"));
a6798bab 3098
fd361982 3099 if (!bfd_set_section_alignment (sec, class_align))
a6798bab
L
3100 goto error_alignment;
3101
3102 htab->plt_second_eh_frame = sec;
3103 }
3104 }
3105 }
3106
b44ee3a8
L
3107 /* The .iplt section is used for IFUNC symbols in static
3108 executables. */
3109 sec = htab->elf.iplt;
3110 if (sec != NULL)
a6798bab 3111 {
b44ee3a8
L
3112 /* NB: Delay setting its alignment until we know it is non-empty.
3113 Otherwise an empty iplt section may change vma and lma of the
3114 following sections, which triggers moving dot of the following
3115 section backwards, resulting in a warning and section lma not
3116 being set properly. It later leads to a "File truncated"
3117 error. */
fd361982 3118 if (!bfd_set_section_alignment (sec, 0))
a6798bab 3119 goto error_alignment;
b44ee3a8
L
3120
3121 htab->plt.iplt_alignment = (normal_target
3122 ? plt_alignment
3123 : bed->plt_alignment);
a6798bab
L
3124 }
3125
74e10d17
L
3126 if (bfd_link_executable (info)
3127 && !info->nointerp
3128 && !htab->params->has_dynamic_linker
3129 && htab->params->static_before_all_inputs)
3130 {
3131 /* Report error for dynamic input objects if -static is passed at
3132 command-line before all input files without --dynamic-linker
3133 unless --no-dynamic-linker is used. */
3134 bfd *abfd;
3135
3136 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
3137 if ((abfd->flags & DYNAMIC))
3138 info->callbacks->einfo
3139 (_("%X%P: attempted static link of dynamic object `%pB'\n"),
3140 abfd);
3141 }
3142
a6798bab
L
3143 return pbfd;
3144}
bfb1e8c1
L
3145
3146/* Fix up x86 GNU properties. */
3147
3148void
0a59decb 3149_bfd_x86_elf_link_fixup_gnu_properties
279d901e 3150 (struct bfd_link_info *info, elf_property_list **listp)
bfb1e8c1
L
3151{
3152 elf_property_list *p;
3153
3154 for (p = *listp; p; p = p->next)
a9eafb08
L
3155 {
3156 unsigned int type = p->property.pr_type;
3157 if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3158 || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3159 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
3160 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3161 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
3162 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
3163 || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3164 && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3165 {
fde51dd1
L
3166 if (p->property.u.number == 0
3167 && (type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3168 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
3169 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3170 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
3171 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)))
a9eafb08
L
3172 {
3173 /* Remove empty property. */
3174 *listp = p->next;
3175 continue;
3176 }
aa7bca9b 3177
279d901e
L
3178 /* Keep LAM features only for 64-bit output. */
3179 if (type == GNU_PROPERTY_X86_FEATURE_1_AND
3180 && !ABI_64_P (info->output_bfd))
3181 p->property.u.number &= ~(GNU_PROPERTY_X86_FEATURE_1_LAM_U48
3182 | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
3183
a9eafb08
L
3184 listp = &p->next;
3185 }
3186 else if (type > GNU_PROPERTY_HIPROC)
3187 {
3188 /* The property list is sorted in order of type. */
3189 break;
3190 }
3191 }
bfb1e8c1 3192}
5b9c07b2
L
3193
3194void
3195_bfd_elf_linker_x86_set_options (struct bfd_link_info * info,
3196 struct elf_linker_x86_params *params)
3197{
3198 const struct elf_backend_data *bed
3199 = get_elf_backend_data (info->output_bfd);
3200 struct elf_x86_link_hash_table *htab
3201 = elf_x86_hash_table (info, bed->target_id);
3202 if (htab != NULL)
3203 htab->params = params;
3204}