]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elfxx-x86.c
ELF: Consolidate maybe_set_textrel
[thirdparty/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->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->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->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 ret->target_id = bed->target_id;
766 ret->target_os = get_elf_x86_backend_data (abfd)->target_os;
767
768 ret->loc_hash_table = htab_try_create (1024,
769 _bfd_x86_elf_local_htab_hash,
770 _bfd_x86_elf_local_htab_eq,
771 NULL);
772 ret->loc_hash_memory = objalloc_create ();
773 if (!ret->loc_hash_table || !ret->loc_hash_memory)
774 {
775 elf_x86_link_hash_table_free (abfd);
776 return NULL;
777 }
778 ret->elf.root.hash_table_free = elf_x86_link_hash_table_free;
779
780 return &ret->elf.root;
781 }
782
783 /* Sort relocs into address order. */
784
785 int
786 _bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
787 {
788 const arelent *a = * (const arelent **) ap;
789 const arelent *b = * (const arelent **) bp;
790
791 if (a->address > b->address)
792 return 1;
793 else if (a->address < b->address)
794 return -1;
795 else
796 return 0;
797 }
798
799 /* Mark symbol, NAME, as locally defined by linker if it is referenced
800 and not defined in a relocatable object file. */
801
802 static void
803 elf_x86_linker_defined (struct bfd_link_info *info, const char *name)
804 {
805 struct elf_link_hash_entry *h;
806
807 h = elf_link_hash_lookup (elf_hash_table (info), name,
808 FALSE, FALSE, FALSE);
809 if (h == NULL)
810 return;
811
812 while (h->root.type == bfd_link_hash_indirect)
813 h = (struct elf_link_hash_entry *) h->root.u.i.link;
814
815 if (h->root.type == bfd_link_hash_new
816 || h->root.type == bfd_link_hash_undefined
817 || h->root.type == bfd_link_hash_undefweak
818 || h->root.type == bfd_link_hash_common
819 || (!h->def_regular && h->def_dynamic))
820 {
821 elf_x86_hash_entry (h)->local_ref = 2;
822 elf_x86_hash_entry (h)->linker_def = 1;
823 }
824 }
825
826 /* Hide a linker-defined symbol, NAME, with hidden visibility. */
827
828 static void
829 elf_x86_hide_linker_defined (struct bfd_link_info *info,
830 const char *name)
831 {
832 struct elf_link_hash_entry *h;
833
834 h = elf_link_hash_lookup (elf_hash_table (info), name,
835 FALSE, FALSE, FALSE);
836 if (h == NULL)
837 return;
838
839 while (h->root.type == bfd_link_hash_indirect)
840 h = (struct elf_link_hash_entry *) h->root.u.i.link;
841
842 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
843 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
844 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
845 }
846
847 bfd_boolean
848 _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
849 {
850 if (!bfd_link_relocatable (info))
851 {
852 /* Check for __tls_get_addr reference. */
853 struct elf_x86_link_hash_table *htab;
854 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
855 htab = elf_x86_hash_table (info, bed->target_id);
856 if (htab)
857 {
858 struct elf_link_hash_entry *h;
859
860 h = elf_link_hash_lookup (elf_hash_table (info),
861 htab->tls_get_addr,
862 FALSE, FALSE, FALSE);
863 if (h != NULL)
864 {
865 elf_x86_hash_entry (h)->tls_get_addr = 1;
866
867 /* Check the versioned __tls_get_addr symbol. */
868 while (h->root.type == bfd_link_hash_indirect)
869 {
870 h = (struct elf_link_hash_entry *) h->root.u.i.link;
871 elf_x86_hash_entry (h)->tls_get_addr = 1;
872 }
873 }
874
875 /* "__ehdr_start" will be defined by linker as a hidden symbol
876 later if it is referenced and not defined. */
877 elf_x86_linker_defined (info, "__ehdr_start");
878
879 if (bfd_link_executable (info))
880 {
881 /* References to __bss_start, _end and _edata should be
882 locally resolved within executables. */
883 elf_x86_linker_defined (info, "__bss_start");
884 elf_x86_linker_defined (info, "_end");
885 elf_x86_linker_defined (info, "_edata");
886 }
887 else
888 {
889 /* Hide hidden __bss_start, _end and _edata in shared
890 libraries. */
891 elf_x86_hide_linker_defined (info, "__bss_start");
892 elf_x86_hide_linker_defined (info, "_end");
893 elf_x86_hide_linker_defined (info, "_edata");
894 }
895 }
896 }
897
898 /* Invoke the regular ELF backend linker to do all the work. */
899 return _bfd_elf_link_check_relocs (abfd, info);
900 }
901
902 bfd_boolean
903 _bfd_elf_x86_valid_reloc_p (asection *input_section,
904 struct bfd_link_info *info,
905 struct elf_x86_link_hash_table *htab,
906 const Elf_Internal_Rela *rel,
907 struct elf_link_hash_entry *h,
908 Elf_Internal_Sym *sym,
909 Elf_Internal_Shdr *symtab_hdr,
910 bfd_boolean *no_dynreloc_p)
911 {
912 bfd_boolean valid_p = TRUE;
913
914 *no_dynreloc_p = FALSE;
915
916 /* Check If relocation against non-preemptible absolute symbol is
917 valid in PIC. FIXME: Can't use SYMBOL_REFERENCES_LOCAL_P since
918 it may call _bfd_elf_link_hide_sym_by_version and result in
919 ld-elfvers/ vers21 test failure. */
920 if (bfd_link_pic (info)
921 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
922 {
923 const struct elf_backend_data *bed;
924 unsigned int r_type;
925 Elf_Internal_Rela irel;
926
927 /* Skip non-absolute symbol. */
928 if (h)
929 {
930 if (!ABS_SYMBOL_P (h))
931 return valid_p;
932 }
933 else if (sym->st_shndx != SHN_ABS)
934 return valid_p;
935
936 bed = get_elf_backend_data (input_section->owner);
937 r_type = ELF32_R_TYPE (rel->r_info);
938 irel = *rel;
939
940 /* Only allow relocations against absolute symbol, which can be
941 resolved as absolute value + addend. GOTPCREL relocations
942 are allowed since absolute value + addend is stored in the
943 GOT slot. */
944 if (bed->target_id == X86_64_ELF_DATA)
945 {
946 r_type &= ~R_X86_64_converted_reloc_bit;
947 valid_p = (r_type == R_X86_64_64
948 || r_type == R_X86_64_32
949 || r_type == R_X86_64_32S
950 || r_type == R_X86_64_16
951 || r_type == R_X86_64_8
952 || r_type == R_X86_64_GOTPCREL
953 || r_type == R_X86_64_GOTPCRELX
954 || r_type == R_X86_64_REX_GOTPCRELX);
955 if (!valid_p)
956 {
957 unsigned int r_symndx = htab->r_sym (rel->r_info);
958 irel.r_info = htab->r_info (r_symndx, r_type);
959 }
960 }
961 else
962 valid_p = (r_type == R_386_32
963 || r_type == R_386_16
964 || r_type == R_386_8);
965
966 if (valid_p)
967 *no_dynreloc_p = TRUE;
968 else
969 {
970 const char *name;
971 arelent internal_reloc;
972
973 if (!bed->elf_info_to_howto (input_section->owner,
974 &internal_reloc, &irel)
975 || internal_reloc.howto == NULL)
976 abort ();
977
978 if (h)
979 name = h->root.root.string;
980 else
981 name = bfd_elf_sym_name (input_section->owner, symtab_hdr,
982 sym, NULL);
983 info->callbacks->einfo
984 /* xgettext:c-format */
985 (_("%F%P: %pB: relocation %s against absolute symbol "
986 "`%s' in section `%pA' is disallowed\n"),
987 input_section->owner, internal_reloc.howto->name, name,
988 input_section);
989 bfd_set_error (bfd_error_bad_value);
990 }
991 }
992
993 return valid_p;
994 }
995
996 /* Set the sizes of the dynamic sections. */
997
998 bfd_boolean
999 _bfd_x86_elf_size_dynamic_sections (bfd *output_bfd,
1000 struct bfd_link_info *info)
1001 {
1002 struct elf_x86_link_hash_table *htab;
1003 bfd *dynobj;
1004 asection *s;
1005 bfd_boolean relocs;
1006 bfd *ibfd;
1007 const struct elf_backend_data *bed
1008 = get_elf_backend_data (output_bfd);
1009
1010 htab = elf_x86_hash_table (info, bed->target_id);
1011 if (htab == NULL)
1012 return FALSE;
1013 dynobj = htab->elf.dynobj;
1014 if (dynobj == NULL)
1015 abort ();
1016
1017 /* Set up .got offsets for local syms, and space for local dynamic
1018 relocs. */
1019 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1020 {
1021 bfd_signed_vma *local_got;
1022 bfd_signed_vma *end_local_got;
1023 char *local_tls_type;
1024 bfd_vma *local_tlsdesc_gotent;
1025 bfd_size_type locsymcount;
1026 Elf_Internal_Shdr *symtab_hdr;
1027 asection *srel;
1028
1029 if (! is_x86_elf (ibfd, htab))
1030 continue;
1031
1032 for (s = ibfd->sections; s != NULL; s = s->next)
1033 {
1034 struct elf_dyn_relocs *p;
1035
1036 for (p = ((struct elf_dyn_relocs *)
1037 elf_section_data (s)->local_dynrel);
1038 p != NULL;
1039 p = p->next)
1040 {
1041 if (!bfd_is_abs_section (p->sec)
1042 && bfd_is_abs_section (p->sec->output_section))
1043 {
1044 /* Input section has been discarded, either because
1045 it is a copy of a linkonce section or due to
1046 linker script /DISCARD/, so we'll be discarding
1047 the relocs too. */
1048 }
1049 else if (htab->target_os == is_vxworks
1050 && strcmp (p->sec->output_section->name,
1051 ".tls_vars") == 0)
1052 {
1053 /* Relocations in vxworks .tls_vars sections are
1054 handled specially by the loader. */
1055 }
1056 else if (p->count != 0)
1057 {
1058 srel = elf_section_data (p->sec)->sreloc;
1059 srel->size += p->count * htab->sizeof_reloc;
1060 if ((p->sec->output_section->flags & SEC_READONLY) != 0
1061 && (info->flags & DF_TEXTREL) == 0)
1062 {
1063 info->flags |= DF_TEXTREL;
1064 if (bfd_link_textrel_check (info))
1065 /* xgettext:c-format */
1066 info->callbacks->einfo
1067 (_("%P: %pB: warning: relocation "
1068 "in read-only section `%pA'\n"),
1069 p->sec->owner, p->sec);
1070 }
1071 }
1072 }
1073 }
1074
1075 local_got = elf_local_got_refcounts (ibfd);
1076 if (!local_got)
1077 continue;
1078
1079 symtab_hdr = &elf_symtab_hdr (ibfd);
1080 locsymcount = symtab_hdr->sh_info;
1081 end_local_got = local_got + locsymcount;
1082 local_tls_type = elf_x86_local_got_tls_type (ibfd);
1083 local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
1084 s = htab->elf.sgot;
1085 srel = htab->elf.srelgot;
1086 for (; local_got < end_local_got;
1087 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
1088 {
1089 *local_tlsdesc_gotent = (bfd_vma) -1;
1090 if (*local_got > 0)
1091 {
1092 if (GOT_TLS_GDESC_P (*local_tls_type))
1093 {
1094 *local_tlsdesc_gotent = htab->elf.sgotplt->size
1095 - elf_x86_compute_jump_table_size (htab);
1096 htab->elf.sgotplt->size += 2 * htab->got_entry_size;
1097 *local_got = (bfd_vma) -2;
1098 }
1099 if (! GOT_TLS_GDESC_P (*local_tls_type)
1100 || GOT_TLS_GD_P (*local_tls_type))
1101 {
1102 *local_got = s->size;
1103 s->size += htab->got_entry_size;
1104 if (GOT_TLS_GD_P (*local_tls_type)
1105 || *local_tls_type == GOT_TLS_IE_BOTH)
1106 s->size += htab->got_entry_size;
1107 }
1108 if ((bfd_link_pic (info) && *local_tls_type != GOT_ABS)
1109 || GOT_TLS_GD_ANY_P (*local_tls_type)
1110 || (*local_tls_type & GOT_TLS_IE))
1111 {
1112 if (*local_tls_type == GOT_TLS_IE_BOTH)
1113 srel->size += 2 * htab->sizeof_reloc;
1114 else if (GOT_TLS_GD_P (*local_tls_type)
1115 || ! GOT_TLS_GDESC_P (*local_tls_type))
1116 srel->size += htab->sizeof_reloc;
1117 if (GOT_TLS_GDESC_P (*local_tls_type))
1118 {
1119 htab->elf.srelplt->size += htab->sizeof_reloc;
1120 if (bed->target_id == X86_64_ELF_DATA)
1121 htab->tlsdesc_plt = (bfd_vma) -1;
1122 }
1123 }
1124 }
1125 else
1126 *local_got = (bfd_vma) -1;
1127 }
1128 }
1129
1130 if (htab->tls_ld_or_ldm_got.refcount > 0)
1131 {
1132 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
1133 or R_X86_64_TLSLD relocs. */
1134 htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
1135 htab->elf.sgot->size += 2 * htab->got_entry_size;
1136 htab->elf.srelgot->size += htab->sizeof_reloc;
1137 }
1138 else
1139 htab->tls_ld_or_ldm_got.offset = -1;
1140
1141 /* Allocate global sym .plt and .got entries, and space for global
1142 sym dynamic relocs. */
1143 elf_link_hash_traverse (&htab->elf, elf_x86_allocate_dynrelocs,
1144 info);
1145
1146 /* Allocate .plt and .got entries, and space for local symbols. */
1147 htab_traverse (htab->loc_hash_table, elf_x86_allocate_local_dynreloc,
1148 info);
1149
1150 /* For every jump slot reserved in the sgotplt, reloc_count is
1151 incremented. However, when we reserve space for TLS descriptors,
1152 it's not incremented, so in order to compute the space reserved
1153 for them, it suffices to multiply the reloc count by the jump
1154 slot size.
1155
1156 PR ld/13302: We start next_irelative_index at the end of .rela.plt
1157 so that R_{386,X86_64}_IRELATIVE entries come last. */
1158 if (htab->elf.srelplt)
1159 {
1160 htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
1161 htab->sgotplt_jump_table_size
1162 = elf_x86_compute_jump_table_size (htab);
1163 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
1164 }
1165 else if (htab->elf.irelplt)
1166 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
1167
1168 if (htab->tlsdesc_plt)
1169 {
1170 /* NB: tlsdesc_plt is set only for x86-64. If we're not using
1171 lazy TLS relocations, don't generate the PLT and GOT entries
1172 they require. */
1173 if ((info->flags & DF_BIND_NOW))
1174 htab->tlsdesc_plt = 0;
1175 else
1176 {
1177 htab->tlsdesc_got = htab->elf.sgot->size;
1178 htab->elf.sgot->size += htab->got_entry_size;
1179 /* Reserve room for the initial entry.
1180 FIXME: we could probably do away with it in this case. */
1181 if (htab->elf.splt->size == 0)
1182 htab->elf.splt->size = htab->plt.plt_entry_size;
1183 htab->tlsdesc_plt = htab->elf.splt->size;
1184 htab->elf.splt->size += htab->plt.plt_entry_size;
1185 }
1186 }
1187
1188 if (htab->elf.sgotplt)
1189 {
1190 /* Don't allocate .got.plt section if there are no GOT nor PLT
1191 entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
1192 if ((htab->elf.hgot == NULL
1193 || !htab->got_referenced)
1194 && (htab->elf.sgotplt->size == bed->got_header_size)
1195 && (htab->elf.splt == NULL
1196 || htab->elf.splt->size == 0)
1197 && (htab->elf.sgot == NULL
1198 || htab->elf.sgot->size == 0)
1199 && (htab->elf.iplt == NULL
1200 || htab->elf.iplt->size == 0)
1201 && (htab->elf.igotplt == NULL
1202 || htab->elf.igotplt->size == 0))
1203 {
1204 htab->elf.sgotplt->size = 0;
1205 /* Solaris requires to keep _GLOBAL_OFFSET_TABLE_ even if it
1206 isn't used. */
1207 if (htab->elf.hgot != NULL && htab->target_os != is_solaris)
1208 {
1209 /* Remove the unused _GLOBAL_OFFSET_TABLE_ from symbol
1210 table. */
1211 htab->elf.hgot->root.type = bfd_link_hash_undefined;
1212 htab->elf.hgot->root.u.undef.abfd
1213 = htab->elf.hgot->root.u.def.section->owner;
1214 htab->elf.hgot->root.linker_def = 0;
1215 htab->elf.hgot->ref_regular = 0;
1216 htab->elf.hgot->def_regular = 0;
1217 }
1218 }
1219 }
1220
1221 if (_bfd_elf_eh_frame_present (info))
1222 {
1223 if (htab->plt_eh_frame != NULL
1224 && htab->elf.splt != NULL
1225 && htab->elf.splt->size != 0
1226 && !bfd_is_abs_section (htab->elf.splt->output_section))
1227 htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
1228
1229 if (htab->plt_got_eh_frame != NULL
1230 && htab->plt_got != NULL
1231 && htab->plt_got->size != 0
1232 && !bfd_is_abs_section (htab->plt_got->output_section))
1233 htab->plt_got_eh_frame->size
1234 = htab->non_lazy_plt->eh_frame_plt_size;
1235
1236 /* Unwind info for the second PLT and .plt.got sections are
1237 identical. */
1238 if (htab->plt_second_eh_frame != NULL
1239 && htab->plt_second != NULL
1240 && htab->plt_second->size != 0
1241 && !bfd_is_abs_section (htab->plt_second->output_section))
1242 htab->plt_second_eh_frame->size
1243 = htab->non_lazy_plt->eh_frame_plt_size;
1244 }
1245
1246 /* We now have determined the sizes of the various dynamic sections.
1247 Allocate memory for them. */
1248 relocs = FALSE;
1249 for (s = dynobj->sections; s != NULL; s = s->next)
1250 {
1251 bfd_boolean strip_section = TRUE;
1252
1253 if ((s->flags & SEC_LINKER_CREATED) == 0)
1254 continue;
1255
1256 if (s == htab->elf.splt
1257 || s == htab->elf.sgot)
1258 {
1259 /* Strip this section if we don't need it; see the
1260 comment below. */
1261 /* We'd like to strip these sections if they aren't needed, but if
1262 we've exported dynamic symbols from them we must leave them.
1263 It's too late to tell BFD to get rid of the symbols. */
1264
1265 if (htab->elf.hplt != NULL)
1266 strip_section = FALSE;
1267 }
1268 else if (s == htab->elf.sgotplt
1269 || s == htab->elf.iplt
1270 || s == htab->elf.igotplt
1271 || s == htab->plt_second
1272 || s == htab->plt_got
1273 || s == htab->plt_eh_frame
1274 || s == htab->plt_got_eh_frame
1275 || s == htab->plt_second_eh_frame
1276 || s == htab->elf.sdynbss
1277 || s == htab->elf.sdynrelro)
1278 {
1279 /* Strip these too. */
1280 }
1281 else if (htab->is_reloc_section (bfd_section_name (s)))
1282 {
1283 if (s->size != 0
1284 && s != htab->elf.srelplt
1285 && s != htab->srelplt2)
1286 relocs = TRUE;
1287
1288 /* We use the reloc_count field as a counter if we need
1289 to copy relocs into the output file. */
1290 if (s != htab->elf.srelplt)
1291 s->reloc_count = 0;
1292 }
1293 else
1294 {
1295 /* It's not one of our sections, so don't allocate space. */
1296 continue;
1297 }
1298
1299 if (s->size == 0)
1300 {
1301 /* If we don't need this section, strip it from the
1302 output file. This is mostly to handle .rel.bss and
1303 .rel.plt. We must create both sections in
1304 create_dynamic_sections, because they must be created
1305 before the linker maps input sections to output
1306 sections. The linker does that before
1307 adjust_dynamic_symbol is called, and it is that
1308 function which decides whether anything needs to go
1309 into these sections. */
1310 if (strip_section)
1311 s->flags |= SEC_EXCLUDE;
1312 continue;
1313 }
1314
1315 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1316 continue;
1317
1318 /* NB: Initially, the iplt section has minimal alignment to
1319 avoid moving dot of the following section backwards when
1320 it is empty. Update its section alignment now since it
1321 is non-empty. */
1322 if (s == htab->elf.iplt)
1323 bfd_set_section_alignment (s, htab->plt.iplt_alignment);
1324
1325 /* Allocate memory for the section contents. We use bfd_zalloc
1326 here in case unused entries are not reclaimed before the
1327 section's contents are written out. This should not happen,
1328 but this way if it does, we get a R_386_NONE or R_X86_64_NONE
1329 reloc instead of garbage. */
1330 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
1331 if (s->contents == NULL)
1332 return FALSE;
1333 }
1334
1335 if (htab->plt_eh_frame != NULL
1336 && htab->plt_eh_frame->contents != NULL)
1337 {
1338 memcpy (htab->plt_eh_frame->contents,
1339 htab->plt.eh_frame_plt,
1340 htab->plt_eh_frame->size);
1341 bfd_put_32 (dynobj, htab->elf.splt->size,
1342 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
1343 }
1344
1345 if (htab->plt_got_eh_frame != NULL
1346 && htab->plt_got_eh_frame->contents != NULL)
1347 {
1348 memcpy (htab->plt_got_eh_frame->contents,
1349 htab->non_lazy_plt->eh_frame_plt,
1350 htab->plt_got_eh_frame->size);
1351 bfd_put_32 (dynobj, htab->plt_got->size,
1352 (htab->plt_got_eh_frame->contents
1353 + PLT_FDE_LEN_OFFSET));
1354 }
1355
1356 if (htab->plt_second_eh_frame != NULL
1357 && htab->plt_second_eh_frame->contents != NULL)
1358 {
1359 memcpy (htab->plt_second_eh_frame->contents,
1360 htab->non_lazy_plt->eh_frame_plt,
1361 htab->plt_second_eh_frame->size);
1362 bfd_put_32 (dynobj, htab->plt_second->size,
1363 (htab->plt_second_eh_frame->contents
1364 + PLT_FDE_LEN_OFFSET));
1365 }
1366
1367 if (htab->elf.dynamic_sections_created)
1368 {
1369 /* Add some entries to the .dynamic section. We fill in the
1370 values later, in elf_{i386,x86_64}_finish_dynamic_sections,
1371 but we must add the entries now so that we get the correct
1372 size for the .dynamic section. The DT_DEBUG entry is filled
1373 in by the dynamic linker and used by the debugger. */
1374 #define add_dynamic_entry(TAG, VAL) \
1375 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1376
1377 if (bfd_link_executable (info))
1378 {
1379 if (!add_dynamic_entry (DT_DEBUG, 0))
1380 return FALSE;
1381 }
1382
1383 if (htab->elf.splt->size != 0)
1384 {
1385 /* DT_PLTGOT is used by prelink even if there is no PLT
1386 relocation. */
1387 if (!add_dynamic_entry (DT_PLTGOT, 0))
1388 return FALSE;
1389 }
1390
1391 if (htab->elf.srelplt->size != 0)
1392 {
1393 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
1394 || !add_dynamic_entry (DT_PLTREL, htab->dt_reloc)
1395 || !add_dynamic_entry (DT_JMPREL, 0))
1396 return FALSE;
1397 }
1398
1399 if (htab->tlsdesc_plt
1400 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
1401 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
1402 return FALSE;
1403
1404 if (relocs)
1405 {
1406 if (!add_dynamic_entry (htab->dt_reloc, 0)
1407 || !add_dynamic_entry (htab->dt_reloc_sz, 0)
1408 || !add_dynamic_entry (htab->dt_reloc_ent,
1409 htab->sizeof_reloc))
1410 return FALSE;
1411
1412 /* If any dynamic relocs apply to a read-only section,
1413 then we need a DT_TEXTREL entry. */
1414 if ((info->flags & DF_TEXTREL) == 0)
1415 elf_link_hash_traverse (&htab->elf,
1416 _bfd_elf_maybe_set_textrel, info);
1417
1418 if ((info->flags & DF_TEXTREL) != 0)
1419 {
1420 if (htab->readonly_dynrelocs_against_ifunc)
1421 {
1422 info->callbacks->einfo
1423 (_("%P%X: read-only segment has dynamic IFUNC relocations;"
1424 " recompile with %s\n"),
1425 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
1426 bfd_set_error (bfd_error_bad_value);
1427 return FALSE;
1428 }
1429
1430 if (!add_dynamic_entry (DT_TEXTREL, 0))
1431 return FALSE;
1432 }
1433 }
1434 if (htab->target_os == is_vxworks
1435 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
1436 return FALSE;
1437 }
1438 #undef add_dynamic_entry
1439
1440 return TRUE;
1441 }
1442
1443 /* Finish up the x86 dynamic sections. */
1444
1445 struct elf_x86_link_hash_table *
1446 _bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd,
1447 struct bfd_link_info *info)
1448 {
1449 struct elf_x86_link_hash_table *htab;
1450 const struct elf_backend_data *bed;
1451 bfd *dynobj;
1452 asection *sdyn;
1453 bfd_byte *dyncon, *dynconend;
1454 bfd_size_type sizeof_dyn;
1455
1456 bed = get_elf_backend_data (output_bfd);
1457 htab = elf_x86_hash_table (info, bed->target_id);
1458 if (htab == NULL)
1459 return htab;
1460
1461 dynobj = htab->elf.dynobj;
1462 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
1463
1464 /* GOT is always created in setup_gnu_properties. But it may not be
1465 needed. .got.plt section may be needed for static IFUNC. */
1466 if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
1467 {
1468 bfd_vma dynamic_addr;
1469
1470 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
1471 {
1472 _bfd_error_handler
1473 (_("discarded output section: `%pA'"), htab->elf.sgotplt);
1474 return NULL;
1475 }
1476
1477 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
1478 = htab->got_entry_size;
1479
1480 dynamic_addr = (sdyn == NULL
1481 ? (bfd_vma) 0
1482 : sdyn->output_section->vma + sdyn->output_offset);
1483
1484 /* Set the first entry in the global offset table to the address
1485 of the dynamic section. Write GOT[1] and GOT[2], needed for
1486 the dynamic linker. */
1487 if (htab->got_entry_size == 8)
1488 {
1489 bfd_put_64 (output_bfd, dynamic_addr,
1490 htab->elf.sgotplt->contents);
1491 bfd_put_64 (output_bfd, (bfd_vma) 0,
1492 htab->elf.sgotplt->contents + 8);
1493 bfd_put_64 (output_bfd, (bfd_vma) 0,
1494 htab->elf.sgotplt->contents + 8*2);
1495 }
1496 else
1497 {
1498 bfd_put_32 (output_bfd, dynamic_addr,
1499 htab->elf.sgotplt->contents);
1500 bfd_put_32 (output_bfd, 0,
1501 htab->elf.sgotplt->contents + 4);
1502 bfd_put_32 (output_bfd, 0,
1503 htab->elf.sgotplt->contents + 4*2);
1504 }
1505 }
1506
1507 if (!htab->elf.dynamic_sections_created)
1508 return htab;
1509
1510 if (sdyn == NULL || htab->elf.sgot == NULL)
1511 abort ();
1512
1513 sizeof_dyn = bed->s->sizeof_dyn;
1514 dyncon = sdyn->contents;
1515 dynconend = sdyn->contents + sdyn->size;
1516 for (; dyncon < dynconend; dyncon += sizeof_dyn)
1517 {
1518 Elf_Internal_Dyn dyn;
1519 asection *s;
1520
1521 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
1522
1523 switch (dyn.d_tag)
1524 {
1525 default:
1526 if (htab->target_os == is_vxworks
1527 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
1528 break;
1529 continue;
1530
1531 case DT_PLTGOT:
1532 s = htab->elf.sgotplt;
1533 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
1534 break;
1535
1536 case DT_JMPREL:
1537 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
1538 break;
1539
1540 case DT_PLTRELSZ:
1541 s = htab->elf.srelplt->output_section;
1542 dyn.d_un.d_val = s->size;
1543 break;
1544
1545 case DT_TLSDESC_PLT:
1546 s = htab->elf.splt;
1547 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
1548 + htab->tlsdesc_plt;
1549 break;
1550
1551 case DT_TLSDESC_GOT:
1552 s = htab->elf.sgot;
1553 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
1554 + htab->tlsdesc_got;
1555 break;
1556 }
1557
1558 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
1559 }
1560
1561 if (htab->plt_got != NULL && htab->plt_got->size > 0)
1562 elf_section_data (htab->plt_got->output_section)
1563 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
1564
1565 if (htab->plt_second != NULL && htab->plt_second->size > 0)
1566 elf_section_data (htab->plt_second->output_section)
1567 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
1568
1569 /* Adjust .eh_frame for .plt section. */
1570 if (htab->plt_eh_frame != NULL
1571 && htab->plt_eh_frame->contents != NULL)
1572 {
1573 if (htab->elf.splt != NULL
1574 && htab->elf.splt->size != 0
1575 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
1576 && htab->elf.splt->output_section != NULL
1577 && htab->plt_eh_frame->output_section != NULL)
1578 {
1579 bfd_vma plt_start = htab->elf.splt->output_section->vma;
1580 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
1581 + htab->plt_eh_frame->output_offset
1582 + PLT_FDE_START_OFFSET;
1583 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
1584 htab->plt_eh_frame->contents
1585 + PLT_FDE_START_OFFSET);
1586 }
1587
1588 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
1589 {
1590 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
1591 htab->plt_eh_frame,
1592 htab->plt_eh_frame->contents))
1593 return NULL;
1594 }
1595 }
1596
1597 /* Adjust .eh_frame for .plt.got section. */
1598 if (htab->plt_got_eh_frame != NULL
1599 && htab->plt_got_eh_frame->contents != NULL)
1600 {
1601 if (htab->plt_got != NULL
1602 && htab->plt_got->size != 0
1603 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
1604 && htab->plt_got->output_section != NULL
1605 && htab->plt_got_eh_frame->output_section != NULL)
1606 {
1607 bfd_vma plt_start = htab->plt_got->output_section->vma;
1608 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
1609 + htab->plt_got_eh_frame->output_offset
1610 + PLT_FDE_START_OFFSET;
1611 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
1612 htab->plt_got_eh_frame->contents
1613 + PLT_FDE_START_OFFSET);
1614 }
1615 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
1616 {
1617 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
1618 htab->plt_got_eh_frame,
1619 htab->plt_got_eh_frame->contents))
1620 return NULL;
1621 }
1622 }
1623
1624 /* Adjust .eh_frame for the second PLT section. */
1625 if (htab->plt_second_eh_frame != NULL
1626 && htab->plt_second_eh_frame->contents != NULL)
1627 {
1628 if (htab->plt_second != NULL
1629 && htab->plt_second->size != 0
1630 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
1631 && htab->plt_second->output_section != NULL
1632 && htab->plt_second_eh_frame->output_section != NULL)
1633 {
1634 bfd_vma plt_start = htab->plt_second->output_section->vma;
1635 bfd_vma eh_frame_start
1636 = (htab->plt_second_eh_frame->output_section->vma
1637 + htab->plt_second_eh_frame->output_offset
1638 + PLT_FDE_START_OFFSET);
1639 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
1640 htab->plt_second_eh_frame->contents
1641 + PLT_FDE_START_OFFSET);
1642 }
1643 if (htab->plt_second_eh_frame->sec_info_type
1644 == SEC_INFO_TYPE_EH_FRAME)
1645 {
1646 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
1647 htab->plt_second_eh_frame,
1648 htab->plt_second_eh_frame->contents))
1649 return NULL;
1650 }
1651 }
1652
1653 if (htab->elf.sgot && htab->elf.sgot->size > 0)
1654 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
1655 = htab->got_entry_size;
1656
1657 return htab;
1658 }
1659
1660
1661 bfd_boolean
1662 _bfd_x86_elf_always_size_sections (bfd *output_bfd,
1663 struct bfd_link_info *info)
1664 {
1665 asection *tls_sec = elf_hash_table (info)->tls_sec;
1666
1667 if (tls_sec)
1668 {
1669 struct elf_link_hash_entry *tlsbase;
1670
1671 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
1672 "_TLS_MODULE_BASE_",
1673 FALSE, FALSE, FALSE);
1674
1675 if (tlsbase && tlsbase->type == STT_TLS)
1676 {
1677 struct elf_x86_link_hash_table *htab;
1678 struct bfd_link_hash_entry *bh = NULL;
1679 const struct elf_backend_data *bed
1680 = get_elf_backend_data (output_bfd);
1681
1682 htab = elf_x86_hash_table (info, bed->target_id);
1683 if (htab == NULL)
1684 return FALSE;
1685
1686 if (!(_bfd_generic_link_add_one_symbol
1687 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
1688 tls_sec, 0, NULL, FALSE,
1689 bed->collect, &bh)))
1690 return FALSE;
1691
1692 htab->tls_module_base = bh;
1693
1694 tlsbase = (struct elf_link_hash_entry *)bh;
1695 tlsbase->def_regular = 1;
1696 tlsbase->other = STV_HIDDEN;
1697 tlsbase->root.linker_def = 1;
1698 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
1699 }
1700 }
1701
1702 return TRUE;
1703 }
1704
1705 void
1706 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
1707 const Elf_Internal_Sym *isym,
1708 bfd_boolean definition,
1709 bfd_boolean dynamic ATTRIBUTE_UNUSED)
1710 {
1711 if (definition)
1712 {
1713 struct elf_x86_link_hash_entry *eh
1714 = (struct elf_x86_link_hash_entry *) h;
1715 eh->def_protected = (ELF_ST_VISIBILITY (isym->st_other)
1716 == STV_PROTECTED);
1717 }
1718 }
1719
1720 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1721
1722 void
1723 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
1724 struct elf_link_hash_entry *dir,
1725 struct elf_link_hash_entry *ind)
1726 {
1727 struct elf_x86_link_hash_entry *edir, *eind;
1728
1729 edir = (struct elf_x86_link_hash_entry *) dir;
1730 eind = (struct elf_x86_link_hash_entry *) ind;
1731
1732 if (ind->root.type == bfd_link_hash_indirect
1733 && dir->got.refcount <= 0)
1734 {
1735 edir->tls_type = eind->tls_type;
1736 eind->tls_type = GOT_UNKNOWN;
1737 }
1738
1739 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
1740 generate a R_386_COPY reloc. */
1741 edir->gotoff_ref |= eind->gotoff_ref;
1742
1743 edir->zero_undefweak |= eind->zero_undefweak;
1744
1745 if (ELIMINATE_COPY_RELOCS
1746 && ind->root.type != bfd_link_hash_indirect
1747 && dir->dynamic_adjusted)
1748 {
1749 /* If called to transfer flags for a weakdef during processing
1750 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1751 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1752 if (dir->versioned != versioned_hidden)
1753 dir->ref_dynamic |= ind->ref_dynamic;
1754 dir->ref_regular |= ind->ref_regular;
1755 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1756 dir->needs_plt |= ind->needs_plt;
1757 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1758 }
1759 else
1760 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1761 }
1762
1763 /* Remove undefined weak symbol from the dynamic symbol table if it
1764 is resolved to 0. */
1765
1766 bfd_boolean
1767 _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
1768 struct elf_link_hash_entry *h)
1769 {
1770 if (h->dynindx != -1
1771 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, elf_x86_hash_entry (h)))
1772 {
1773 h->dynindx = -1;
1774 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1775 h->dynstr_index);
1776 }
1777 return TRUE;
1778 }
1779
1780 /* Change the STT_GNU_IFUNC symbol defined in position-dependent
1781 executable into the normal function symbol and set its address
1782 to its PLT entry, which should be resolved by R_*_IRELATIVE at
1783 run-time. */
1784
1785 void
1786 _bfd_x86_elf_link_fixup_ifunc_symbol (struct bfd_link_info *info,
1787 struct elf_x86_link_hash_table *htab,
1788 struct elf_link_hash_entry *h,
1789 Elf_Internal_Sym *sym)
1790 {
1791 if (bfd_link_pde (info)
1792 && h->def_regular
1793 && h->dynindx != -1
1794 && h->plt.offset != (bfd_vma) -1
1795 && h->type == STT_GNU_IFUNC
1796 && h->pointer_equality_needed)
1797 {
1798 asection *plt_s;
1799 bfd_vma plt_offset;
1800 bfd *output_bfd = info->output_bfd;
1801
1802 if (htab->plt_second)
1803 {
1804 struct elf_x86_link_hash_entry *eh
1805 = (struct elf_x86_link_hash_entry *) h;
1806
1807 plt_s = htab->plt_second;
1808 plt_offset = eh->plt_second.offset;
1809 }
1810 else
1811 {
1812 plt_s = htab->elf.splt;
1813 plt_offset = h->plt.offset;
1814 }
1815
1816 sym->st_size = 0;
1817 sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
1818 sym->st_shndx
1819 = _bfd_elf_section_from_bfd_section (output_bfd,
1820 plt_s->output_section);
1821 sym->st_value = (plt_s->output_section->vma
1822 + plt_s->output_offset + plt_offset);
1823 }
1824 }
1825
1826 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
1827
1828 bfd_boolean
1829 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
1830 {
1831 if (h->plt.offset != (bfd_vma) -1
1832 && !h->def_regular
1833 && !h->pointer_equality_needed)
1834 return FALSE;
1835
1836 return _bfd_elf_hash_symbol (h);
1837 }
1838
1839 /* Adjust a symbol defined by a dynamic object and referenced by a
1840 regular object. The current definition is in some section of the
1841 dynamic object, but we're not including those sections. We have to
1842 change the definition to something the rest of the link can
1843 understand. */
1844
1845 bfd_boolean
1846 _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1847 struct elf_link_hash_entry *h)
1848 {
1849 struct elf_x86_link_hash_table *htab;
1850 asection *s, *srel;
1851 struct elf_x86_link_hash_entry *eh;
1852 struct elf_dyn_relocs *p;
1853 const struct elf_backend_data *bed
1854 = get_elf_backend_data (info->output_bfd);
1855
1856 /* STT_GNU_IFUNC symbol must go through PLT. */
1857 if (h->type == STT_GNU_IFUNC)
1858 {
1859 /* All local STT_GNU_IFUNC references must be treate as local
1860 calls via local PLT. */
1861 if (h->ref_regular
1862 && SYMBOL_CALLS_LOCAL (info, h))
1863 {
1864 bfd_size_type pc_count = 0, count = 0;
1865 struct elf_dyn_relocs **pp;
1866
1867 eh = (struct elf_x86_link_hash_entry *) h;
1868 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
1869 {
1870 pc_count += p->pc_count;
1871 p->count -= p->pc_count;
1872 p->pc_count = 0;
1873 count += p->count;
1874 if (p->count == 0)
1875 *pp = p->next;
1876 else
1877 pp = &p->next;
1878 }
1879
1880 if (pc_count || count)
1881 {
1882 h->non_got_ref = 1;
1883 if (pc_count)
1884 {
1885 /* Increment PLT reference count only for PC-relative
1886 references. */
1887 h->needs_plt = 1;
1888 if (h->plt.refcount <= 0)
1889 h->plt.refcount = 1;
1890 else
1891 h->plt.refcount += 1;
1892 }
1893 }
1894 }
1895
1896 if (h->plt.refcount <= 0)
1897 {
1898 h->plt.offset = (bfd_vma) -1;
1899 h->needs_plt = 0;
1900 }
1901 return TRUE;
1902 }
1903
1904 /* If this is a function, put it in the procedure linkage table. We
1905 will fill in the contents of the procedure linkage table later,
1906 when we know the address of the .got section. */
1907 if (h->type == STT_FUNC
1908 || h->needs_plt)
1909 {
1910 if (h->plt.refcount <= 0
1911 || SYMBOL_CALLS_LOCAL (info, h)
1912 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1913 && h->root.type == bfd_link_hash_undefweak))
1914 {
1915 /* This case can occur if we saw a PLT32 reloc in an input
1916 file, but the symbol was never referred to by a dynamic
1917 object, or if all references were garbage collected. In
1918 such a case, we don't actually need to build a procedure
1919 linkage table, and we can just do a PC32 reloc instead. */
1920 h->plt.offset = (bfd_vma) -1;
1921 h->needs_plt = 0;
1922 }
1923
1924 return TRUE;
1925 }
1926 else
1927 /* It's possible that we incorrectly decided a .plt reloc was needed
1928 * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
1929 check_relocs. We can't decide accurately between function and
1930 non-function syms in check-relocs; Objects loaded later in
1931 the link may change h->type. So fix it now. */
1932 h->plt.offset = (bfd_vma) -1;
1933
1934 eh = (struct elf_x86_link_hash_entry *) h;
1935
1936 /* If this is a weak symbol, and there is a real definition, the
1937 processor independent code will have arranged for us to see the
1938 real definition first, and we can just use the same value. */
1939 if (h->is_weakalias)
1940 {
1941 struct elf_link_hash_entry *def = weakdef (h);
1942 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1943 h->root.u.def.section = def->root.u.def.section;
1944 h->root.u.def.value = def->root.u.def.value;
1945 if (ELIMINATE_COPY_RELOCS
1946 || info->nocopyreloc
1947 || SYMBOL_NO_COPYRELOC (info, eh))
1948 {
1949 /* NB: needs_copy is always 0 for i386. */
1950 h->non_got_ref = def->non_got_ref;
1951 eh->needs_copy = def->needs_copy;
1952 }
1953 return TRUE;
1954 }
1955
1956 /* This is a reference to a symbol defined by a dynamic object which
1957 is not a function. */
1958
1959 /* If we are creating a shared library, we must presume that the
1960 only references to the symbol are via the global offset table.
1961 For such cases we need not do anything here; the relocations will
1962 be handled correctly by relocate_section. */
1963 if (!bfd_link_executable (info))
1964 return TRUE;
1965
1966 /* If there are no references to this symbol that do not use the
1967 GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
1968 reloc. NB: gotoff_ref is always 0 for x86-64. */
1969 if (!h->non_got_ref && !eh->gotoff_ref)
1970 return TRUE;
1971
1972 /* If -z nocopyreloc was given, we won't generate them either. */
1973 if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
1974 {
1975 h->non_got_ref = 0;
1976 return TRUE;
1977 }
1978
1979 htab = elf_x86_hash_table (info, bed->target_id);
1980 if (htab == NULL)
1981 return FALSE;
1982
1983 /* If there aren't any dynamic relocs in read-only sections nor
1984 R_386_GOTOFF relocation, then we can keep the dynamic relocs and
1985 avoid the copy reloc. This doesn't work on VxWorks, where we can
1986 not have dynamic relocations (other than copy and jump slot
1987 relocations) in an executable. */
1988 if (ELIMINATE_COPY_RELOCS
1989 && (bed->target_id == X86_64_ELF_DATA
1990 || (!eh->gotoff_ref
1991 && htab->target_os != is_vxworks)))
1992 {
1993 /* If we don't find any dynamic relocs in read-only sections,
1994 then we'll be keeping the dynamic relocs and avoiding the copy
1995 reloc. */
1996 if (!_bfd_elf_readonly_dynrelocs (h))
1997 {
1998 h->non_got_ref = 0;
1999 return TRUE;
2000 }
2001 }
2002
2003 /* We must allocate the symbol in our .dynbss section, which will
2004 become part of the .bss section of the executable. There will be
2005 an entry for this symbol in the .dynsym section. The dynamic
2006 object will contain position independent code, so all references
2007 from the dynamic object to this symbol will go through the global
2008 offset table. The dynamic linker will use the .dynsym entry to
2009 determine the address it must put in the global offset table, so
2010 both the dynamic object and the regular object will refer to the
2011 same memory location for the variable. */
2012
2013 /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
2014 dynamic linker to copy the initial value out of the dynamic object
2015 and into the runtime process image. */
2016 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2017 {
2018 s = htab->elf.sdynrelro;
2019 srel = htab->elf.sreldynrelro;
2020 }
2021 else
2022 {
2023 s = htab->elf.sdynbss;
2024 srel = htab->elf.srelbss;
2025 }
2026 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2027 {
2028 srel->size += htab->sizeof_reloc;
2029 h->needs_copy = 1;
2030 }
2031
2032 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2033 }
2034
2035 void
2036 _bfd_x86_elf_hide_symbol (struct bfd_link_info *info,
2037 struct elf_link_hash_entry *h,
2038 bfd_boolean force_local)
2039 {
2040 if (h->root.type == bfd_link_hash_undefweak
2041 && info->nointerp
2042 && bfd_link_pie (info))
2043 {
2044 /* When there is no dynamic interpreter in PIE, make the undefined
2045 weak symbol dynamic so that PC relative branch to the undefined
2046 weak symbol will land to address 0. */
2047 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
2048 if (h->plt.refcount > 0
2049 || eh->plt_got.refcount > 0)
2050 return;
2051 }
2052
2053 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
2054 }
2055
2056 /* Return TRUE if a symbol is referenced locally. It is similar to
2057 SYMBOL_REFERENCES_LOCAL, but it also checks version script. It
2058 works in check_relocs. */
2059
2060 bfd_boolean
2061 _bfd_x86_elf_link_symbol_references_local (struct bfd_link_info *info,
2062 struct elf_link_hash_entry *h)
2063 {
2064 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
2065 struct elf_x86_link_hash_table *htab
2066 = (struct elf_x86_link_hash_table *) info->hash;
2067
2068 if (eh->local_ref > 1)
2069 return TRUE;
2070
2071 if (eh->local_ref == 1)
2072 return FALSE;
2073
2074 /* Unversioned symbols defined in regular objects can be forced local
2075 by linker version script. A weak undefined symbol is forced local
2076 if
2077 1. It has non-default visibility. Or
2078 2. When building executable, there is no dynamic linker. Or
2079 3. or "-z nodynamic-undefined-weak" is used.
2080 */
2081 if (SYMBOL_REFERENCES_LOCAL (info, h)
2082 || (h->root.type == bfd_link_hash_undefweak
2083 && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2084 || (bfd_link_executable (info)
2085 && htab->interp == NULL)
2086 || info->dynamic_undefined_weak == 0))
2087 || ((h->def_regular || ELF_COMMON_DEF_P (h))
2088 && info->version_info != NULL
2089 && _bfd_elf_link_hide_sym_by_version (info, h)))
2090 {
2091 eh->local_ref = 2;
2092 return TRUE;
2093 }
2094
2095 eh->local_ref = 1;
2096 return FALSE;
2097 }
2098
2099 /* Return the section that should be marked against GC for a given
2100 relocation. */
2101
2102 asection *
2103 _bfd_x86_elf_gc_mark_hook (asection *sec,
2104 struct bfd_link_info *info,
2105 Elf_Internal_Rela *rel,
2106 struct elf_link_hash_entry *h,
2107 Elf_Internal_Sym *sym)
2108 {
2109 /* Compiler should optimize this out. */
2110 if (((unsigned int) R_X86_64_GNU_VTINHERIT
2111 != (unsigned int) R_386_GNU_VTINHERIT)
2112 || ((unsigned int) R_X86_64_GNU_VTENTRY
2113 != (unsigned int) R_386_GNU_VTENTRY))
2114 abort ();
2115
2116 if (h != NULL)
2117 switch (ELF32_R_TYPE (rel->r_info))
2118 {
2119 case R_X86_64_GNU_VTINHERIT:
2120 case R_X86_64_GNU_VTENTRY:
2121 return NULL;
2122 }
2123
2124 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2125 }
2126
2127 static bfd_vma
2128 elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
2129 bfd_vma off,
2130 bfd_vma offset ATTRIBUTE_UNUSED,
2131 bfd_vma got_addr)
2132 {
2133 return got_addr + off;
2134 }
2135
2136 static bfd_vma
2137 elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
2138 bfd_vma off,
2139 bfd_vma offset,
2140 bfd_vma got_addr ATTRIBUTE_UNUSED)
2141 {
2142 return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
2143 }
2144
2145 static bfd_boolean
2146 elf_i386_valid_plt_reloc_p (unsigned int type)
2147 {
2148 return (type == R_386_JUMP_SLOT
2149 || type == R_386_GLOB_DAT
2150 || type == R_386_IRELATIVE);
2151 }
2152
2153 static bfd_boolean
2154 elf_x86_64_valid_plt_reloc_p (unsigned int type)
2155 {
2156 return (type == R_X86_64_JUMP_SLOT
2157 || type == R_X86_64_GLOB_DAT
2158 || type == R_X86_64_IRELATIVE);
2159 }
2160
2161 long
2162 _bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
2163 long count,
2164 long relsize,
2165 bfd_vma got_addr,
2166 struct elf_x86_plt plts[],
2167 asymbol **dynsyms,
2168 asymbol **ret)
2169 {
2170 long size, i, n, len;
2171 int j;
2172 unsigned int plt_got_offset, plt_entry_size;
2173 asymbol *s;
2174 bfd_byte *plt_contents;
2175 long dynrelcount;
2176 arelent **dynrelbuf, *p;
2177 char *names;
2178 const struct elf_backend_data *bed;
2179 bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
2180 bfd_vma);
2181 bfd_boolean (*valid_plt_reloc_p) (unsigned int);
2182
2183 dynrelbuf = NULL;
2184 if (count == 0)
2185 goto bad_return;
2186
2187 dynrelbuf = (arelent **) bfd_malloc (relsize);
2188 if (dynrelbuf == NULL)
2189 goto bad_return;
2190
2191 dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
2192 dynsyms);
2193 if (dynrelcount <= 0)
2194 goto bad_return;
2195
2196 /* Sort the relocs by address. */
2197 qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
2198 _bfd_x86_elf_compare_relocs);
2199
2200 size = count * sizeof (asymbol);
2201
2202 /* Allocate space for @plt suffixes. */
2203 n = 0;
2204 for (i = 0; i < dynrelcount; i++)
2205 {
2206 p = dynrelbuf[i];
2207 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2208 if (p->addend != 0)
2209 size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
2210 }
2211
2212 s = *ret = (asymbol *) bfd_zmalloc (size);
2213 if (s == NULL)
2214 goto bad_return;
2215
2216 bed = get_elf_backend_data (abfd);
2217
2218 if (bed->target_id == X86_64_ELF_DATA)
2219 {
2220 get_plt_got_vma = elf_x86_64_get_plt_got_vma;
2221 valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
2222 }
2223 else
2224 {
2225 get_plt_got_vma = elf_i386_get_plt_got_vma;
2226 valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
2227 if (got_addr)
2228 {
2229 /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
2230 address. */
2231 asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
2232 if (sec != NULL)
2233 got_addr = sec->vma;
2234 else
2235 {
2236 sec = bfd_get_section_by_name (abfd, ".got");
2237 if (sec != NULL)
2238 got_addr = sec->vma;
2239 }
2240
2241 if (got_addr == (bfd_vma) -1)
2242 goto bad_return;
2243 }
2244 }
2245
2246 /* Check for each PLT section. */
2247 names = (char *) (s + count);
2248 size = 0;
2249 n = 0;
2250 for (j = 0; plts[j].name != NULL; j++)
2251 if ((plt_contents = plts[j].contents) != NULL)
2252 {
2253 long k;
2254 bfd_vma offset;
2255 asection *plt;
2256 struct elf_x86_plt *plt_p = &plts[j];
2257
2258 plt_got_offset = plt_p->plt_got_offset;
2259 plt_entry_size = plt_p->plt_entry_size;
2260
2261 plt = plt_p->sec;
2262
2263 if ((plt_p->type & plt_lazy))
2264 {
2265 /* Skip PLT0 in lazy PLT. */
2266 k = 1;
2267 offset = plt_entry_size;
2268 }
2269 else
2270 {
2271 k = 0;
2272 offset = 0;
2273 }
2274
2275 /* Check each PLT entry against dynamic relocations. */
2276 for (; k < plt_p->count; k++)
2277 {
2278 int off;
2279 bfd_vma got_vma;
2280 long min, max, mid;
2281
2282 /* Get the GOT offset for i386 or the PC-relative offset
2283 for x86-64, a signed 32-bit integer. */
2284 off = H_GET_32 (abfd, (plt_contents + offset
2285 + plt_got_offset));
2286 got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
2287
2288 /* Binary search. */
2289 p = dynrelbuf[0];
2290 min = 0;
2291 max = dynrelcount;
2292 while ((min + 1) < max)
2293 {
2294 arelent *r;
2295
2296 mid = (min + max) / 2;
2297 r = dynrelbuf[mid];
2298 if (got_vma > r->address)
2299 min = mid;
2300 else if (got_vma < r->address)
2301 max = mid;
2302 else
2303 {
2304 p = r;
2305 break;
2306 }
2307 }
2308
2309 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
2310 if (got_vma == p->address
2311 && p->howto != NULL
2312 && valid_plt_reloc_p (p->howto->type))
2313 {
2314 *s = **p->sym_ptr_ptr;
2315 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
2316 set. Since we are defining a symbol, ensure one
2317 of them is set. */
2318 if ((s->flags & BSF_LOCAL) == 0)
2319 s->flags |= BSF_GLOBAL;
2320 s->flags |= BSF_SYNTHETIC;
2321 /* This is no longer a section symbol. */
2322 s->flags &= ~BSF_SECTION_SYM;
2323 s->section = plt;
2324 s->the_bfd = plt->owner;
2325 s->value = offset;
2326 s->udata.p = NULL;
2327 s->name = names;
2328 len = strlen ((*p->sym_ptr_ptr)->name);
2329 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2330 names += len;
2331 if (p->addend != 0)
2332 {
2333 char buf[30], *a;
2334
2335 memcpy (names, "+0x", sizeof ("+0x") - 1);
2336 names += sizeof ("+0x") - 1;
2337 bfd_sprintf_vma (abfd, buf, p->addend);
2338 for (a = buf; *a == '0'; ++a)
2339 ;
2340 size = strlen (a);
2341 memcpy (names, a, size);
2342 names += size;
2343 }
2344 memcpy (names, "@plt", sizeof ("@plt"));
2345 names += sizeof ("@plt");
2346 n++;
2347 s++;
2348 /* There should be only one entry in PLT for a given
2349 symbol. Set howto to NULL after processing a PLT
2350 entry to guard against corrupted PLT. */
2351 p->howto = NULL;
2352 }
2353 offset += plt_entry_size;
2354 }
2355 }
2356
2357 /* PLT entries with R_386_TLS_DESC relocations are skipped. */
2358 if (n == 0)
2359 {
2360 bad_return:
2361 count = -1;
2362 }
2363 else
2364 count = n;
2365
2366 for (j = 0; plts[j].name != NULL; j++)
2367 free (plts[j].contents);
2368
2369 free (dynrelbuf);
2370
2371 return count;
2372 }
2373
2374 /* Parse x86 GNU properties. */
2375
2376 enum elf_property_kind
2377 _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
2378 bfd_byte *ptr, unsigned int datasz)
2379 {
2380 elf_property *prop;
2381
2382 if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
2383 || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
2384 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
2385 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
2386 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
2387 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
2388 || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
2389 && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
2390 {
2391 if (datasz != 4)
2392 {
2393 _bfd_error_handler
2394 (_("error: %pB: <corrupt x86 property (0x%x) size: 0x%x>"),
2395 abfd, type, datasz);
2396 return property_corrupt;
2397 }
2398 prop = _bfd_elf_get_property (abfd, type, datasz);
2399 prop->u.number |= bfd_h_get_32 (abfd, ptr);
2400 prop->pr_kind = property_number;
2401 return property_number;
2402 }
2403
2404 return property_ignored;
2405 }
2406
2407 /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
2408 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
2409 should be merged with ABFD. */
2410
2411 bfd_boolean
2412 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
2413 bfd *abfd ATTRIBUTE_UNUSED,
2414 bfd *bbfd ATTRIBUTE_UNUSED,
2415 elf_property *aprop,
2416 elf_property *bprop)
2417 {
2418 unsigned int number, features;
2419 bfd_boolean updated = FALSE;
2420 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
2421
2422 if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
2423 || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
2424 && pr_type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
2425 {
2426 if (aprop == NULL || bprop == NULL)
2427 {
2428 /* Only one of APROP and BPROP can be NULL. */
2429 if (aprop != NULL)
2430 {
2431 /* Remove this property since the other input file doesn't
2432 have it. */
2433 aprop->pr_kind = property_remove;
2434 updated = TRUE;
2435 }
2436 }
2437 else
2438 {
2439 number = aprop->u.number;
2440 aprop->u.number = number | bprop->u.number;
2441 updated = number != (unsigned int) aprop->u.number;
2442 }
2443 return updated;
2444 }
2445 else if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
2446 || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_LO
2447 && pr_type <= GNU_PROPERTY_X86_UINT32_OR_HI))
2448 {
2449 if (aprop != NULL && bprop != NULL)
2450 {
2451 number = aprop->u.number;
2452 aprop->u.number = number | bprop->u.number;
2453 /* Remove the property if all bits are empty. */
2454 if (aprop->u.number == 0)
2455 {
2456 aprop->pr_kind = property_remove;
2457 updated = TRUE;
2458 }
2459 else
2460 updated = number != (unsigned int) aprop->u.number;
2461 }
2462 else
2463 {
2464 /* Only one of APROP and BPROP can be NULL. */
2465 if (aprop != NULL)
2466 {
2467 if (aprop->u.number == 0)
2468 {
2469 /* Remove APROP if all bits are empty. */
2470 aprop->pr_kind = property_remove;
2471 updated = TRUE;
2472 }
2473 }
2474 else
2475 {
2476 /* Return TRUE if APROP is NULL and all bits of BPROP
2477 aren't empty to indicate that BPROP should be added
2478 to ABFD. */
2479 updated = bprop->u.number != 0;
2480 }
2481 }
2482 return updated;
2483 }
2484 else if (pr_type >= GNU_PROPERTY_X86_UINT32_AND_LO
2485 && pr_type <= GNU_PROPERTY_X86_UINT32_AND_HI)
2486 {
2487 /* Only one of APROP and BPROP can be NULL:
2488 1. APROP & BPROP when both APROP and BPROP aren't NULL.
2489 2. If APROP is NULL, remove x86 feature.
2490 3. Otherwise, do nothing.
2491 */
2492 const struct elf_backend_data *bed
2493 = get_elf_backend_data (info->output_bfd);
2494 struct elf_x86_link_hash_table *htab
2495 = elf_x86_hash_table (info, bed->target_id);
2496 if (!htab)
2497 abort ();
2498 if (aprop != NULL && bprop != NULL)
2499 {
2500 features = 0;
2501 if (htab->params->ibt)
2502 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
2503 if (htab->params->shstk)
2504 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
2505 number = aprop->u.number;
2506 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
2507 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
2508 aprop->u.number = (number & bprop->u.number) | features;
2509 updated = number != (unsigned int) aprop->u.number;
2510 /* Remove the property if all feature bits are cleared. */
2511 if (aprop->u.number == 0)
2512 aprop->pr_kind = property_remove;
2513 }
2514 else
2515 {
2516 /* There should be no AND properties since some input doesn't
2517 have them. Set IBT and SHSTK properties for -z ibt and -z
2518 shstk if needed. */
2519 features = 0;
2520 if (htab->params->ibt)
2521 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
2522 if (htab->params->shstk)
2523 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
2524 if (features)
2525 {
2526 if (aprop != NULL)
2527 {
2528 updated = features != (unsigned int) aprop->u.number;
2529 aprop->u.number = features;
2530 }
2531 else
2532 {
2533 updated = TRUE;
2534 bprop->u.number = features;
2535 }
2536 }
2537 else if (aprop != NULL)
2538 {
2539 aprop->pr_kind = property_remove;
2540 updated = TRUE;
2541 }
2542 }
2543 return updated;
2544 }
2545 else
2546 {
2547 /* Never should happen. */
2548 abort ();
2549 }
2550
2551 return updated;
2552 }
2553
2554 /* Set up x86 GNU properties. Return the first relocatable ELF input
2555 with GNU properties if found. Otherwise, return NULL. */
2556
2557 bfd *
2558 _bfd_x86_elf_link_setup_gnu_properties
2559 (struct bfd_link_info *info, struct elf_x86_init_table *init_table)
2560 {
2561 bfd_boolean normal_target;
2562 bfd_boolean lazy_plt;
2563 asection *sec, *pltsec;
2564 bfd *dynobj;
2565 bfd_boolean use_ibt_plt;
2566 unsigned int plt_alignment, features;
2567 struct elf_x86_link_hash_table *htab;
2568 bfd *pbfd;
2569 bfd *ebfd = NULL;
2570 elf_property *prop;
2571 const struct elf_backend_data *bed;
2572 unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
2573 unsigned int got_align;
2574
2575 /* Find a normal input file with GNU property note. */
2576 for (pbfd = info->input_bfds;
2577 pbfd != NULL;
2578 pbfd = pbfd->link.next)
2579 if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
2580 && bfd_count_sections (pbfd) != 0)
2581 {
2582 ebfd = pbfd;
2583
2584 if (elf_properties (pbfd) != NULL)
2585 break;
2586 }
2587
2588 bed = get_elf_backend_data (info->output_bfd);
2589
2590 htab = elf_x86_hash_table (info, bed->target_id);
2591 if (htab == NULL)
2592 return pbfd;
2593
2594 features = 0;
2595 if (htab->params->ibt)
2596 {
2597 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
2598 htab->params->cet_report &= ~cet_report_ibt;
2599 }
2600 if (htab->params->shstk)
2601 {
2602 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
2603 htab->params->cet_report &= ~cet_report_shstk;
2604 }
2605 if (!(htab->params->cet_report & (cet_report_ibt | cet_report_shstk)))
2606 htab->params->cet_report = cet_report_none;
2607
2608 if (ebfd != NULL)
2609 {
2610 prop = NULL;
2611 if (features)
2612 {
2613 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and
2614 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
2615 prop = _bfd_elf_get_property (ebfd,
2616 GNU_PROPERTY_X86_FEATURE_1_AND,
2617 4);
2618 prop->u.number |= features;
2619 prop->pr_kind = property_number;
2620 }
2621
2622 /* Create the GNU property note section if needed. */
2623 if (prop != NULL && pbfd == NULL)
2624 {
2625 sec = bfd_make_section_with_flags (ebfd,
2626 NOTE_GNU_PROPERTY_SECTION_NAME,
2627 (SEC_ALLOC
2628 | SEC_LOAD
2629 | SEC_IN_MEMORY
2630 | SEC_READONLY
2631 | SEC_HAS_CONTENTS
2632 | SEC_DATA));
2633 if (sec == NULL)
2634 info->callbacks->einfo (_("%F%P: failed to create GNU property section\n"));
2635
2636 if (!bfd_set_section_alignment (sec, class_align))
2637 {
2638 error_alignment:
2639 info->callbacks->einfo (_("%F%pA: failed to align section\n"),
2640 sec);
2641 }
2642
2643 elf_section_type (sec) = SHT_NOTE;
2644 }
2645 }
2646
2647 if (htab->params->cet_report)
2648 {
2649 /* Report missing IBT and SHSTK properties. */
2650 bfd *abfd;
2651 const char *msg;
2652 elf_property_list *p;
2653 bfd_boolean missing_ibt, missing_shstk;
2654 bfd_boolean check_ibt
2655 = !!(htab->params->cet_report & cet_report_ibt);
2656 bfd_boolean check_shstk
2657 = !!(htab->params->cet_report & cet_report_shstk);
2658
2659 if ((htab->params->cet_report & cet_report_warning))
2660 msg = _("%P: %pB: warning: missing %s\n");
2661 else
2662 msg = _("%X%P: %pB: error: missing %s\n");
2663
2664 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2665 if (!(abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED))
2666 && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2667 {
2668 for (p = elf_properties (abfd); p; p = p->next)
2669 if (p->property.pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
2670 break;
2671
2672 missing_ibt = check_ibt;
2673 missing_shstk = check_shstk;
2674 if (p)
2675 {
2676 missing_ibt &= !(p->property.u.number
2677 & GNU_PROPERTY_X86_FEATURE_1_IBT);
2678 missing_shstk &= !(p->property.u.number
2679 & GNU_PROPERTY_X86_FEATURE_1_SHSTK);
2680 }
2681 if (missing_ibt || missing_shstk)
2682 {
2683 const char *missing;
2684 if (missing_ibt && missing_shstk)
2685 missing = _("IBT and SHSTK properties");
2686 else if (missing_ibt)
2687 missing = _("IBT property");
2688 else
2689 missing = _("SHSTK property");
2690 info->callbacks->einfo (msg, abfd, missing);
2691 }
2692 }
2693 }
2694
2695 pbfd = _bfd_elf_link_setup_gnu_properties (info);
2696
2697 htab->r_info = init_table->r_info;
2698 htab->r_sym = init_table->r_sym;
2699
2700 if (bfd_link_relocatable (info))
2701 return pbfd;
2702
2703 htab->plt0_pad_byte = init_table->plt0_pad_byte;
2704
2705 use_ibt_plt = htab->params->ibtplt || htab->params->ibt;
2706 if (!use_ibt_plt && pbfd != NULL)
2707 {
2708 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
2709 elf_property_list *p;
2710
2711 /* The property list is sorted in order of type. */
2712 for (p = elf_properties (pbfd); p; p = p->next)
2713 {
2714 if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
2715 {
2716 use_ibt_plt = !!(p->property.u.number
2717 & GNU_PROPERTY_X86_FEATURE_1_IBT);
2718 break;
2719 }
2720 else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
2721 break;
2722 }
2723 }
2724
2725 dynobj = htab->elf.dynobj;
2726
2727 /* Set htab->elf.dynobj here so that there is no need to check and
2728 set it in check_relocs. */
2729 if (dynobj == NULL)
2730 {
2731 if (pbfd != NULL)
2732 {
2733 htab->elf.dynobj = pbfd;
2734 dynobj = pbfd;
2735 }
2736 else
2737 {
2738 bfd *abfd;
2739
2740 /* Find a normal input file to hold linker created
2741 sections. */
2742 for (abfd = info->input_bfds;
2743 abfd != NULL;
2744 abfd = abfd->link.next)
2745 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2746 && (abfd->flags
2747 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
2748 && bed->relocs_compatible (abfd->xvec,
2749 info->output_bfd->xvec))
2750 {
2751 htab->elf.dynobj = abfd;
2752 dynobj = abfd;
2753 break;
2754 }
2755 }
2756 }
2757
2758 /* Return if there are no normal input files. */
2759 if (dynobj == NULL)
2760 return pbfd;
2761
2762 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
2763 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
2764 canonical function address. */
2765 htab->plt.has_plt0 = 1;
2766 normal_target = htab->target_os == is_normal;
2767
2768 if (normal_target)
2769 {
2770 if (use_ibt_plt)
2771 {
2772 htab->lazy_plt = init_table->lazy_ibt_plt;
2773 htab->non_lazy_plt = init_table->non_lazy_ibt_plt;
2774 }
2775 else
2776 {
2777 htab->lazy_plt = init_table->lazy_plt;
2778 htab->non_lazy_plt = init_table->non_lazy_plt;
2779 }
2780 }
2781 else
2782 {
2783 htab->lazy_plt = init_table->lazy_plt;
2784 htab->non_lazy_plt = NULL;
2785 }
2786
2787 pltsec = htab->elf.splt;
2788
2789 /* If the non-lazy PLT is available, use it for all PLT entries if
2790 there are no PLT0 or no .plt section. */
2791 if (htab->non_lazy_plt != NULL
2792 && (!htab->plt.has_plt0 || pltsec == NULL))
2793 {
2794 lazy_plt = FALSE;
2795 if (bfd_link_pic (info))
2796 htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
2797 else
2798 htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
2799 htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
2800 htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
2801 htab->plt.plt_got_insn_size
2802 = htab->non_lazy_plt->plt_got_insn_size;
2803 htab->plt.eh_frame_plt_size
2804 = htab->non_lazy_plt->eh_frame_plt_size;
2805 htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
2806 }
2807 else
2808 {
2809 lazy_plt = TRUE;
2810 if (bfd_link_pic (info))
2811 {
2812 htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
2813 htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
2814 }
2815 else
2816 {
2817 htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
2818 htab->plt.plt_entry = htab->lazy_plt->plt_entry;
2819 }
2820 htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
2821 htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
2822 htab->plt.plt_got_insn_size
2823 = htab->lazy_plt->plt_got_insn_size;
2824 htab->plt.eh_frame_plt_size
2825 = htab->lazy_plt->eh_frame_plt_size;
2826 htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
2827 }
2828
2829 if (htab->target_os == is_vxworks
2830 && !elf_vxworks_create_dynamic_sections (dynobj, info,
2831 &htab->srelplt2))
2832 {
2833 info->callbacks->einfo (_("%F%P: failed to create VxWorks dynamic sections\n"));
2834 return pbfd;
2835 }
2836
2837 /* Since create_dynamic_sections isn't always called, but GOT
2838 relocations need GOT relocations, create them here so that we
2839 don't need to do it in check_relocs. */
2840 if (htab->elf.sgot == NULL
2841 && !_bfd_elf_create_got_section (dynobj, info))
2842 info->callbacks->einfo (_("%F%P: failed to create GOT sections\n"));
2843
2844 got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
2845
2846 /* Align .got and .got.plt sections to their entry size. Do it here
2847 instead of in create_dynamic_sections so that they are always
2848 properly aligned even if create_dynamic_sections isn't called. */
2849 sec = htab->elf.sgot;
2850 if (!bfd_set_section_alignment (sec, got_align))
2851 goto error_alignment;
2852
2853 sec = htab->elf.sgotplt;
2854 if (!bfd_set_section_alignment (sec, got_align))
2855 goto error_alignment;
2856
2857 /* Create the ifunc sections here so that check_relocs can be
2858 simplified. */
2859 if (!_bfd_elf_create_ifunc_sections (dynobj, info))
2860 info->callbacks->einfo (_("%F%P: failed to create ifunc sections\n"));
2861
2862 plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
2863
2864 if (pltsec != NULL)
2865 {
2866 /* Whe creating executable, set the contents of the .interp
2867 section to the interpreter. */
2868 if (bfd_link_executable (info) && !info->nointerp)
2869 {
2870 asection *s = bfd_get_linker_section (dynobj, ".interp");
2871 if (s == NULL)
2872 abort ();
2873 s->size = htab->dynamic_interpreter_size;
2874 s->contents = (unsigned char *) htab->dynamic_interpreter;
2875 htab->interp = s;
2876 }
2877
2878 /* Don't change PLT section alignment for NaCl since it uses
2879 64-byte PLT entry and sets PLT section alignment to 32
2880 bytes. Don't create additional PLT sections for NaCl. */
2881 if (normal_target)
2882 {
2883 flagword pltflags = (bed->dynamic_sec_flags
2884 | SEC_ALLOC
2885 | SEC_CODE
2886 | SEC_LOAD
2887 | SEC_READONLY);
2888 unsigned int non_lazy_plt_alignment
2889 = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
2890
2891 sec = pltsec;
2892 if (!bfd_set_section_alignment (sec, plt_alignment))
2893 goto error_alignment;
2894
2895 /* Create the GOT procedure linkage table. */
2896 sec = bfd_make_section_anyway_with_flags (dynobj,
2897 ".plt.got",
2898 pltflags);
2899 if (sec == NULL)
2900 info->callbacks->einfo (_("%F%P: failed to create GOT PLT section\n"));
2901
2902 if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
2903 goto error_alignment;
2904
2905 htab->plt_got = sec;
2906
2907 if (lazy_plt)
2908 {
2909 sec = NULL;
2910
2911 if (use_ibt_plt)
2912 {
2913 /* Create the second PLT for Intel IBT support. IBT
2914 PLT is supported only for non-NaCl target and is
2915 is needed only for lazy binding. */
2916 sec = bfd_make_section_anyway_with_flags (dynobj,
2917 ".plt.sec",
2918 pltflags);
2919 if (sec == NULL)
2920 info->callbacks->einfo (_("%F%P: failed to create IBT-enabled PLT section\n"));
2921
2922 if (!bfd_set_section_alignment (sec, plt_alignment))
2923 goto error_alignment;
2924 }
2925 else if (htab->params->bndplt && ABI_64_P (dynobj))
2926 {
2927 /* Create the second PLT for Intel MPX support. MPX
2928 PLT is supported only for non-NaCl target in 64-bit
2929 mode and is needed only for lazy binding. */
2930 sec = bfd_make_section_anyway_with_flags (dynobj,
2931 ".plt.sec",
2932 pltflags);
2933 if (sec == NULL)
2934 info->callbacks->einfo (_("%F%P: failed to create BND PLT section\n"));
2935
2936 if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
2937 goto error_alignment;
2938 }
2939
2940 htab->plt_second = sec;
2941 }
2942 }
2943
2944 if (!info->no_ld_generated_unwind_info)
2945 {
2946 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2947 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2948 | SEC_LINKER_CREATED);
2949
2950 sec = bfd_make_section_anyway_with_flags (dynobj,
2951 ".eh_frame",
2952 flags);
2953 if (sec == NULL)
2954 info->callbacks->einfo (_("%F%P: failed to create PLT .eh_frame section\n"));
2955
2956 if (!bfd_set_section_alignment (sec, class_align))
2957 goto error_alignment;
2958
2959 htab->plt_eh_frame = sec;
2960
2961 if (htab->plt_got != NULL)
2962 {
2963 sec = bfd_make_section_anyway_with_flags (dynobj,
2964 ".eh_frame",
2965 flags);
2966 if (sec == NULL)
2967 info->callbacks->einfo (_("%F%P: failed to create GOT PLT .eh_frame section\n"));
2968
2969 if (!bfd_set_section_alignment (sec, class_align))
2970 goto error_alignment;
2971
2972 htab->plt_got_eh_frame = sec;
2973 }
2974
2975 if (htab->plt_second != NULL)
2976 {
2977 sec = bfd_make_section_anyway_with_flags (dynobj,
2978 ".eh_frame",
2979 flags);
2980 if (sec == NULL)
2981 info->callbacks->einfo (_("%F%P: failed to create the second PLT .eh_frame section\n"));
2982
2983 if (!bfd_set_section_alignment (sec, class_align))
2984 goto error_alignment;
2985
2986 htab->plt_second_eh_frame = sec;
2987 }
2988 }
2989 }
2990
2991 /* The .iplt section is used for IFUNC symbols in static
2992 executables. */
2993 sec = htab->elf.iplt;
2994 if (sec != NULL)
2995 {
2996 /* NB: Delay setting its alignment until we know it is non-empty.
2997 Otherwise an empty iplt section may change vma and lma of the
2998 following sections, which triggers moving dot of the following
2999 section backwards, resulting in a warning and section lma not
3000 being set properly. It later leads to a "File truncated"
3001 error. */
3002 if (!bfd_set_section_alignment (sec, 0))
3003 goto error_alignment;
3004
3005 htab->plt.iplt_alignment = (normal_target
3006 ? plt_alignment
3007 : bed->plt_alignment);
3008 }
3009
3010 if (bfd_link_executable (info)
3011 && !info->nointerp
3012 && !htab->params->has_dynamic_linker
3013 && htab->params->static_before_all_inputs)
3014 {
3015 /* Report error for dynamic input objects if -static is passed at
3016 command-line before all input files without --dynamic-linker
3017 unless --no-dynamic-linker is used. */
3018 bfd *abfd;
3019
3020 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
3021 if ((abfd->flags & DYNAMIC))
3022 info->callbacks->einfo
3023 (_("%X%P: attempted static link of dynamic object `%pB'\n"),
3024 abfd);
3025 }
3026
3027 return pbfd;
3028 }
3029
3030 /* Fix up x86 GNU properties. */
3031
3032 void
3033 _bfd_x86_elf_link_fixup_gnu_properties
3034 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3035 elf_property_list **listp)
3036 {
3037 elf_property_list *p;
3038
3039 for (p = *listp; p; p = p->next)
3040 {
3041 unsigned int type = p->property.pr_type;
3042 if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3043 || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3044 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
3045 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3046 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
3047 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
3048 || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3049 && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3050 {
3051 if (p->property.u.number == 0
3052 && (type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3053 || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
3054 && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3055 || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
3056 && type <= GNU_PROPERTY_X86_UINT32_OR_HI)))
3057 {
3058 /* Remove empty property. */
3059 *listp = p->next;
3060 continue;
3061 }
3062
3063 listp = &p->next;
3064 }
3065 else if (type > GNU_PROPERTY_HIPROC)
3066 {
3067 /* The property list is sorted in order of type. */
3068 break;
3069 }
3070 }
3071 }
3072
3073 void
3074 _bfd_elf_linker_x86_set_options (struct bfd_link_info * info,
3075 struct elf_linker_x86_params *params)
3076 {
3077 const struct elf_backend_data *bed
3078 = get_elf_backend_data (info->output_bfd);
3079 struct elf_x86_link_hash_table *htab
3080 = elf_x86_hash_table (info, bed->target_id);
3081 if (htab != NULL)
3082 htab->params = params;
3083 }