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