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