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