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