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