x86: Add _bfd_x86_elf_link_setup_gnu_properties
[deliverable/binutils-gdb.git] / bfd / elfxx-x86.c
1 /* x86 specific support for ELF
2 Copyright (C) 2017 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 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
35 executables. Rather than setting it to the beginning of the TLS
36 section, we have to set it to the end. This function may be called
37 multiple times, it is idempotent. */
38
39 void
40 _bfd_x86_elf_set_tls_module_base (struct bfd_link_info *info)
41 {
42 struct elf_x86_link_hash_table *htab;
43 struct bfd_link_hash_entry *base;
44 const struct elf_backend_data *bed;
45
46 if (!bfd_link_executable (info))
47 return;
48
49 bed = get_elf_backend_data (info->output_bfd);
50 htab = elf_x86_hash_table (info, bed->target_id);
51 if (htab == NULL)
52 return;
53
54 base = htab->tls_module_base;
55 if (base == NULL)
56 return;
57
58 base->u.def.value = htab->elf.tls_size;
59 }
60
61 /* Return the base VMA address which should be subtracted from real addresses
62 when resolving @dtpoff relocation.
63 This is PT_TLS segment p_vaddr. */
64
65 bfd_vma
66 _bfd_x86_elf_dtpoff_base (struct bfd_link_info *info)
67 {
68 /* If tls_sec is NULL, we should have signalled an error already. */
69 if (elf_hash_table (info)->tls_sec == NULL)
70 return 0;
71 return elf_hash_table (info)->tls_sec->vma;
72 }
73
74 /* Find any dynamic relocs that apply to read-only sections. */
75
76 bfd_boolean
77 _bfd_x86_elf_readonly_dynrelocs (struct elf_link_hash_entry *h,
78 void *inf)
79 {
80 struct elf_x86_link_hash_entry *eh;
81 struct elf_dyn_relocs *p;
82
83 /* Skip local IFUNC symbols. */
84 if (h->forced_local && h->type == STT_GNU_IFUNC)
85 return TRUE;
86
87 eh = (struct elf_x86_link_hash_entry *) h;
88 for (p = eh->dyn_relocs; p != NULL; p = p->next)
89 {
90 asection *s = p->sec->output_section;
91
92 if (s != NULL && (s->flags & SEC_READONLY) != 0)
93 {
94 struct bfd_link_info *info = (struct bfd_link_info *) inf;
95
96 info->flags |= DF_TEXTREL;
97
98 if ((info->warn_shared_textrel && bfd_link_pic (info))
99 || info->error_textrel)
100 /* xgettext:c-format */
101 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
102 p->sec->owner, h->root.root.string,
103 p->sec);
104
105 /* Not an error, just cut short the traversal. */
106 return FALSE;
107 }
108 }
109 return TRUE;
110 }
111
112 /* Find and/or create a hash entry for local symbol. */
113
114 struct elf_link_hash_entry *
115 _bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table *htab,
116 bfd *abfd, const Elf_Internal_Rela *rel,
117 bfd_boolean create)
118 {
119 struct elf_x86_link_hash_entry e, *ret;
120 asection *sec = abfd->sections;
121 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
122 htab->r_sym (rel->r_info));
123 void **slot;
124
125 e.elf.indx = sec->id;
126 e.elf.dynstr_index = htab->r_sym (rel->r_info);
127 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
128 create ? INSERT : NO_INSERT);
129
130 if (!slot)
131 return NULL;
132
133 if (*slot)
134 {
135 ret = (struct elf_x86_link_hash_entry *) *slot;
136 return &ret->elf;
137 }
138
139 ret = (struct elf_x86_link_hash_entry *)
140 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
141 sizeof (struct elf_x86_link_hash_entry));
142 if (ret)
143 {
144 memset (ret, 0, sizeof (*ret));
145 ret->elf.indx = sec->id;
146 ret->elf.dynstr_index = htab->r_sym (rel->r_info);
147 ret->elf.dynindx = -1;
148 ret->plt_got.offset = (bfd_vma) -1;
149 *slot = ret;
150 }
151 return &ret->elf;
152 }
153
154 /* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN
155 SYNC WITH _bfd_elf_link_hash_newfunc. */
156
157 struct bfd_hash_entry *
158 _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
159 struct bfd_hash_table *table,
160 const char *string)
161 {
162 /* Allocate the structure if it has not already been allocated by a
163 subclass. */
164 if (entry == NULL)
165 {
166 entry = (struct bfd_hash_entry *)
167 bfd_hash_allocate (table,
168 sizeof (struct elf_x86_link_hash_entry));
169 if (entry == NULL)
170 return entry;
171 }
172
173 /* Call the allocation method of the superclass. */
174 entry = _bfd_link_hash_newfunc (entry, table, string);
175 if (entry != NULL)
176 {
177 struct elf_x86_link_hash_entry *eh
178 = (struct elf_x86_link_hash_entry *) entry;
179 struct elf_link_hash_table *htab
180 = (struct elf_link_hash_table *) table;
181
182 memset (&eh->elf.size, 0,
183 (sizeof (struct elf_x86_link_hash_entry)
184 - offsetof (struct elf_link_hash_entry, size)));
185 /* Set local fields. */
186 eh->elf.indx = -1;
187 eh->elf.dynindx = -1;
188 eh->elf.got = htab->init_got_refcount;
189 eh->elf.plt = htab->init_plt_refcount;
190 /* Assume that we have been called by a non-ELF symbol reader.
191 This flag is then reset by the code which reads an ELF input
192 file. This ensures that a symbol created by a non-ELF symbol
193 reader will have the flag set correctly. */
194 eh->elf.non_elf = 1;
195 eh->plt_second.offset = (bfd_vma) -1;
196 eh->plt_got.offset = (bfd_vma) -1;
197 eh->tlsdesc_got = (bfd_vma) -1;
198 }
199
200 return entry;
201 }
202
203 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
204 for local symbol so that we can handle local STT_GNU_IFUNC symbols
205 as global symbol. We reuse indx and dynstr_index for local symbol
206 hash since they aren't used by global symbols in this backend. */
207
208 hashval_t
209 _bfd_x86_elf_local_htab_hash (const void *ptr)
210 {
211 struct elf_link_hash_entry *h
212 = (struct elf_link_hash_entry *) ptr;
213 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
214 }
215
216 /* Compare local hash entries. */
217
218 int
219 _bfd_x86_elf_local_htab_eq (const void *ptr1, const void *ptr2)
220 {
221 struct elf_link_hash_entry *h1
222 = (struct elf_link_hash_entry *) ptr1;
223 struct elf_link_hash_entry *h2
224 = (struct elf_link_hash_entry *) ptr2;
225
226 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
227 }
228
229 /* Destroy an x86 ELF linker hash table. */
230
231 static void
232 elf_x86_link_hash_table_free (bfd *obfd)
233 {
234 struct elf_x86_link_hash_table *htab
235 = (struct elf_x86_link_hash_table *) obfd->link.hash;
236
237 if (htab->loc_hash_table)
238 htab_delete (htab->loc_hash_table);
239 if (htab->loc_hash_memory)
240 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
241 _bfd_elf_link_hash_table_free (obfd);
242 }
243
244 /* Create an x86 ELF linker hash table. */
245
246 struct bfd_link_hash_table *
247 _bfd_x86_elf_link_hash_table_create (bfd *abfd)
248 {
249 struct elf_x86_link_hash_table *ret;
250 const struct elf_backend_data *bed;
251 bfd_size_type amt = sizeof (struct elf_x86_link_hash_table);
252
253 ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt);
254 if (ret == NULL)
255 return NULL;
256
257 bed = get_elf_backend_data (abfd);
258 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
259 _bfd_x86_elf_link_hash_newfunc,
260 sizeof (struct elf_x86_link_hash_entry),
261 bed->target_id))
262 {
263 free (ret);
264 return NULL;
265 }
266
267 #ifdef BFD64
268 if (ABI_64_P (abfd))
269 {
270 ret->r_info = elf64_r_info;
271 ret->r_sym = elf64_r_sym;
272 ret->pointer_r_type = R_X86_64_64;
273 ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
274 ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
275 ret->tls_get_addr = "__tls_get_addr";
276 }
277 else
278 #endif
279 {
280 ret->r_info = elf32_r_info;
281 ret->r_sym = elf32_r_sym;
282 if (bed->target_id == X86_64_ELF_DATA)
283 {
284 ret->pointer_r_type = R_X86_64_32;
285 ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER;
286 ret->dynamic_interpreter_size
287 = sizeof ELFX32_DYNAMIC_INTERPRETER;
288 ret->tls_get_addr = "__tls_get_addr";
289 }
290 else
291 {
292 ret->pointer_r_type = R_386_32;
293 ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
294 ret->dynamic_interpreter_size
295 = sizeof ELF32_DYNAMIC_INTERPRETER;
296 ret->tls_get_addr = "___tls_get_addr";
297 }
298 }
299
300 ret->loc_hash_table = htab_try_create (1024,
301 _bfd_x86_elf_local_htab_hash,
302 _bfd_x86_elf_local_htab_eq,
303 NULL);
304 ret->loc_hash_memory = objalloc_create ();
305 if (!ret->loc_hash_table || !ret->loc_hash_memory)
306 {
307 elf_x86_link_hash_table_free (abfd);
308 return NULL;
309 }
310 ret->elf.root.hash_table_free = elf_x86_link_hash_table_free;
311
312 return &ret->elf.root;
313 }
314
315 /* Sort relocs into address order. */
316
317 int
318 _bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
319 {
320 const arelent *a = * (const arelent **) ap;
321 const arelent *b = * (const arelent **) bp;
322
323 if (a->address > b->address)
324 return 1;
325 else if (a->address < b->address)
326 return -1;
327 else
328 return 0;
329 }
330
331 bfd_boolean
332 _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
333 {
334 if (!bfd_link_relocatable (info))
335 {
336 /* Check for __tls_get_addr reference. */
337 struct elf_x86_link_hash_table *htab;
338 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
339 htab = elf_x86_hash_table (info, bed->target_id);
340 if (htab)
341 {
342 struct elf_link_hash_entry *h
343 = elf_link_hash_lookup (elf_hash_table (info),
344 htab->tls_get_addr,
345 FALSE, FALSE, FALSE);
346 if (h != NULL)
347 ((struct elf_x86_link_hash_entry *) h)->tls_get_addr = 1;
348 }
349 }
350
351 /* Invoke the regular ELF backend linker to do all the work. */
352 return _bfd_elf_link_check_relocs (abfd, info);
353 }
354
355 bfd_boolean
356 _bfd_x86_elf_always_size_sections (bfd *output_bfd,
357 struct bfd_link_info *info)
358 {
359 asection *tls_sec = elf_hash_table (info)->tls_sec;
360
361 if (tls_sec)
362 {
363 struct elf_link_hash_entry *tlsbase;
364
365 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
366 "_TLS_MODULE_BASE_",
367 FALSE, FALSE, FALSE);
368
369 if (tlsbase && tlsbase->type == STT_TLS)
370 {
371 struct elf_x86_link_hash_table *htab;
372 struct bfd_link_hash_entry *bh = NULL;
373 const struct elf_backend_data *bed
374 = get_elf_backend_data (output_bfd);
375
376 htab = elf_x86_hash_table (info, bed->target_id);
377 if (htab == NULL)
378 return FALSE;
379
380 if (!(_bfd_generic_link_add_one_symbol
381 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
382 tls_sec, 0, NULL, FALSE,
383 bed->collect, &bh)))
384 return FALSE;
385
386 htab->tls_module_base = bh;
387
388 tlsbase = (struct elf_link_hash_entry *)bh;
389 tlsbase->def_regular = 1;
390 tlsbase->other = STV_HIDDEN;
391 tlsbase->root.linker_def = 1;
392 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
393 }
394 }
395
396 return TRUE;
397 }
398
399 void
400 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
401 const Elf_Internal_Sym *isym,
402 bfd_boolean definition,
403 bfd_boolean dynamic ATTRIBUTE_UNUSED)
404 {
405 if (definition)
406 {
407 struct elf_x86_link_hash_entry *eh
408 = (struct elf_x86_link_hash_entry *) h;
409 eh->def_protected = (ELF_ST_VISIBILITY (isym->st_other)
410 == STV_PROTECTED);
411 }
412 }
413
414 /* Copy the extra info we tack onto an elf_link_hash_entry. */
415
416 void
417 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
418 struct elf_link_hash_entry *dir,
419 struct elf_link_hash_entry *ind)
420 {
421 struct elf_x86_link_hash_entry *edir, *eind;
422
423 edir = (struct elf_x86_link_hash_entry *) dir;
424 eind = (struct elf_x86_link_hash_entry *) ind;
425
426 if (eind->dyn_relocs != NULL)
427 {
428 if (edir->dyn_relocs != NULL)
429 {
430 struct elf_dyn_relocs **pp;
431 struct elf_dyn_relocs *p;
432
433 /* Add reloc counts against the indirect sym to the direct sym
434 list. Merge any entries against the same section. */
435 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
436 {
437 struct elf_dyn_relocs *q;
438
439 for (q = edir->dyn_relocs; q != NULL; q = q->next)
440 if (q->sec == p->sec)
441 {
442 q->pc_count += p->pc_count;
443 q->count += p->count;
444 *pp = p->next;
445 break;
446 }
447 if (q == NULL)
448 pp = &p->next;
449 }
450 *pp = edir->dyn_relocs;
451 }
452
453 edir->dyn_relocs = eind->dyn_relocs;
454 eind->dyn_relocs = NULL;
455 }
456
457 if (ind->root.type == bfd_link_hash_indirect
458 && dir->got.refcount <= 0)
459 {
460 edir->tls_type = eind->tls_type;
461 eind->tls_type = GOT_UNKNOWN;
462 }
463
464 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
465 generate a R_386_COPY reloc. */
466 edir->gotoff_ref |= eind->gotoff_ref;
467
468 edir->has_got_reloc |= eind->has_got_reloc;
469 edir->has_non_got_reloc |= eind->has_non_got_reloc;
470
471 if (ELIMINATE_COPY_RELOCS
472 && ind->root.type != bfd_link_hash_indirect
473 && dir->dynamic_adjusted)
474 {
475 /* If called to transfer flags for a weakdef during processing
476 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
477 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
478 if (dir->versioned != versioned_hidden)
479 dir->ref_dynamic |= ind->ref_dynamic;
480 dir->ref_regular |= ind->ref_regular;
481 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
482 dir->needs_plt |= ind->needs_plt;
483 dir->pointer_equality_needed |= ind->pointer_equality_needed;
484 }
485 else
486 {
487 if (eind->func_pointer_refcount > 0)
488 {
489 edir->func_pointer_refcount += eind->func_pointer_refcount;
490 eind->func_pointer_refcount = 0;
491 }
492
493 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
494 }
495 }
496
497 /* Remove undefined weak symbol from the dynamic symbol table if it
498 is resolved to 0. */
499
500 bfd_boolean
501 _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
502 struct elf_link_hash_entry *h)
503 {
504 if (h->dynindx != -1)
505 {
506 const struct elf_backend_data *bed
507 = get_elf_backend_data (info->output_bfd);
508 if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
509 bed->target_id,
510 elf_x86_hash_entry (h)->has_got_reloc,
511 elf_x86_hash_entry (h)))
512 {
513 h->dynindx = -1;
514 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
515 h->dynstr_index);
516 }
517 }
518 return TRUE;
519 }
520
521 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
522
523 bfd_boolean
524 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
525 {
526 if (h->plt.offset != (bfd_vma) -1
527 && !h->def_regular
528 && !h->pointer_equality_needed)
529 return FALSE;
530
531 return _bfd_elf_hash_symbol (h);
532 }
533
534 static bfd_vma
535 elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
536 bfd_vma off,
537 bfd_vma offset ATTRIBUTE_UNUSED,
538 bfd_vma got_addr)
539 {
540 return got_addr + off;
541 }
542
543 static bfd_vma
544 elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
545 bfd_vma off,
546 bfd_vma offset,
547 bfd_vma got_addr ATTRIBUTE_UNUSED)
548 {
549 return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
550 }
551
552 static bfd_boolean
553 elf_i386_valid_plt_reloc_p (unsigned int type)
554 {
555 return (type == R_386_JUMP_SLOT
556 || type == R_386_GLOB_DAT
557 || type == R_386_IRELATIVE);
558 }
559
560 static bfd_boolean
561 elf_x86_64_valid_plt_reloc_p (unsigned int type)
562 {
563 return (type == R_X86_64_JUMP_SLOT
564 || type == R_X86_64_GLOB_DAT
565 || type == R_X86_64_IRELATIVE);
566 }
567
568 long
569 _bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
570 long count,
571 long relsize,
572 bfd_vma got_addr,
573 struct elf_x86_plt plts[],
574 asymbol **dynsyms,
575 asymbol **ret)
576 {
577 long size, i, n, len;
578 int j;
579 unsigned int plt_got_offset, plt_entry_size;
580 asymbol *s;
581 bfd_byte *plt_contents;
582 long dynrelcount;
583 arelent **dynrelbuf, *p;
584 char *names;
585 const struct elf_backend_data *bed;
586 bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
587 bfd_vma);
588 bfd_boolean (*valid_plt_reloc_p) (unsigned int);
589
590 if (count == 0)
591 return -1;
592
593 dynrelbuf = (arelent **) bfd_malloc (relsize);
594 if (dynrelbuf == NULL)
595 return -1;
596
597 dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
598 dynsyms);
599
600 /* Sort the relocs by address. */
601 qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
602 _bfd_x86_elf_compare_relocs);
603
604 size = count * sizeof (asymbol);
605
606 /* Allocate space for @plt suffixes. */
607 n = 0;
608 for (i = 0; i < dynrelcount; i++)
609 {
610 p = dynrelbuf[i];
611 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
612 if (p->addend != 0)
613 size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
614 }
615
616 s = *ret = (asymbol *) bfd_zmalloc (size);
617 if (s == NULL)
618 goto bad_return;
619
620 bed = get_elf_backend_data (abfd);
621
622 if (bed->target_id == X86_64_ELF_DATA)
623 {
624 get_plt_got_vma = elf_x86_64_get_plt_got_vma;
625 valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
626 }
627 else
628 {
629 get_plt_got_vma = elf_i386_get_plt_got_vma;
630 valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
631 if (got_addr)
632 {
633 /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
634 address. */
635 asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
636 if (sec != NULL)
637 got_addr = sec->vma;
638 else
639 {
640 sec = bfd_get_section_by_name (abfd, ".got");
641 if (sec != NULL)
642 got_addr = sec->vma;
643 }
644
645 if (got_addr == (bfd_vma) -1)
646 goto bad_return;
647 }
648 }
649
650 /* Check for each PLT section. */
651 names = (char *) (s + count);
652 size = 0;
653 n = 0;
654 for (j = 0; plts[j].name != NULL; j++)
655 if ((plt_contents = plts[j].contents) != NULL)
656 {
657 long k;
658 bfd_vma offset;
659 asection *plt;
660 struct elf_x86_plt *plt_p = &plts[j];
661
662 plt_got_offset = plt_p->plt_got_offset;
663 plt_entry_size = plt_p->plt_entry_size;
664
665 plt = plt_p->sec;
666
667 if ((plt_p->type & plt_lazy))
668 {
669 /* Skip PLT0 in lazy PLT. */
670 k = 1;
671 offset = plt_entry_size;
672 }
673 else
674 {
675 k = 0;
676 offset = 0;
677 }
678
679 /* Check each PLT entry against dynamic relocations. */
680 for (; k < plt_p->count; k++)
681 {
682 int off;
683 bfd_vma got_vma;
684 long min, max, mid;
685
686 /* Get the GOT offset for i386 or the PC-relative offset
687 for x86-64, a signed 32-bit integer. */
688 off = H_GET_32 (abfd, (plt_contents + offset
689 + plt_got_offset));
690 got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
691
692 /* Binary search. */
693 p = dynrelbuf[0];
694 min = 0;
695 max = dynrelcount;
696 while ((min + 1) < max)
697 {
698 arelent *r;
699
700 mid = (min + max) / 2;
701 r = dynrelbuf[mid];
702 if (got_vma > r->address)
703 min = mid;
704 else if (got_vma < r->address)
705 max = mid;
706 else
707 {
708 p = r;
709 break;
710 }
711 }
712
713 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
714 if (got_vma == p->address
715 && p->howto != NULL
716 && valid_plt_reloc_p (p->howto->type))
717 {
718 *s = **p->sym_ptr_ptr;
719 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
720 set. Since we are defining a symbol, ensure one
721 of them is set. */
722 if ((s->flags & BSF_LOCAL) == 0)
723 s->flags |= BSF_GLOBAL;
724 s->flags |= BSF_SYNTHETIC;
725 /* This is no longer a section symbol. */
726 s->flags &= ~BSF_SECTION_SYM;
727 s->section = plt;
728 s->the_bfd = plt->owner;
729 s->value = offset;
730 s->udata.p = NULL;
731 s->name = names;
732 len = strlen ((*p->sym_ptr_ptr)->name);
733 memcpy (names, (*p->sym_ptr_ptr)->name, len);
734 names += len;
735 if (p->addend != 0)
736 {
737 char buf[30], *a;
738
739 memcpy (names, "+0x", sizeof ("+0x") - 1);
740 names += sizeof ("+0x") - 1;
741 bfd_sprintf_vma (abfd, buf, p->addend);
742 for (a = buf; *a == '0'; ++a)
743 ;
744 size = strlen (a);
745 memcpy (names, a, size);
746 names += size;
747 }
748 memcpy (names, "@plt", sizeof ("@plt"));
749 names += sizeof ("@plt");
750 n++;
751 s++;
752 }
753 offset += plt_entry_size;
754 }
755 }
756
757 /* PLT entries with R_386_TLS_DESC relocations are skipped. */
758 if (n == 0)
759 {
760 bad_return:
761 count = -1;
762 }
763 else
764 count = n;
765
766 for (j = 0; plts[j].name != NULL; j++)
767 if (plts[j].contents != NULL)
768 free (plts[j].contents);
769
770 free (dynrelbuf);
771
772 return count;
773 }
774
775 /* Parse x86 GNU properties. */
776
777 enum elf_property_kind
778 _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
779 bfd_byte *ptr, unsigned int datasz)
780 {
781 elf_property *prop;
782
783 switch (type)
784 {
785 case GNU_PROPERTY_X86_ISA_1_USED:
786 case GNU_PROPERTY_X86_ISA_1_NEEDED:
787 case GNU_PROPERTY_X86_FEATURE_1_AND:
788 if (datasz != 4)
789 {
790 _bfd_error_handler
791 ((type == GNU_PROPERTY_X86_ISA_1_USED
792 ? _("error: %B: <corrupt x86 ISA used size: 0x%x>")
793 : (type == GNU_PROPERTY_X86_ISA_1_NEEDED
794 ? _("error: %B: <corrupt x86 ISA needed size: 0x%x>")
795 : _("error: %B: <corrupt x86 feature size: 0x%x>"))),
796 abfd, datasz);
797 return property_corrupt;
798 }
799 prop = _bfd_elf_get_property (abfd, type, datasz);
800 /* Combine properties of the same type. */
801 prop->u.number |= bfd_h_get_32 (abfd, ptr);
802 prop->pr_kind = property_number;
803 break;
804
805 default:
806 return property_ignored;
807 }
808
809 return property_number;
810 }
811
812 /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
813 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
814 should be merged with ABFD. */
815
816 bfd_boolean
817 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
818 bfd *abfd ATTRIBUTE_UNUSED,
819 elf_property *aprop,
820 elf_property *bprop)
821 {
822 unsigned int number, features;
823 bfd_boolean updated = FALSE;
824 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
825
826 switch (pr_type)
827 {
828 case GNU_PROPERTY_X86_ISA_1_USED:
829 case GNU_PROPERTY_X86_ISA_1_NEEDED:
830 if (aprop != NULL && bprop != NULL)
831 {
832 number = aprop->u.number;
833 aprop->u.number = number | bprop->u.number;
834 updated = number != (unsigned int) aprop->u.number;
835 }
836 else
837 {
838 /* Return TRUE if APROP is NULL to indicate that BPROP should
839 be added to ABFD. */
840 updated = aprop == NULL;
841 }
842 break;
843
844 case GNU_PROPERTY_X86_FEATURE_1_AND:
845 /* Only one of APROP and BPROP can be NULL:
846 1. APROP & BPROP when both APROP and BPROP aren't NULL.
847 2. If APROP is NULL, remove x86 feature.
848 3. Otherwise, do nothing.
849 */
850 if (aprop != NULL && bprop != NULL)
851 {
852 features = 0;
853 if (info->ibt)
854 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
855 if (info->shstk)
856 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
857 number = aprop->u.number;
858 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
859 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
860 aprop->u.number = (number & bprop->u.number) | features;
861 updated = number != (unsigned int) aprop->u.number;
862 /* Remove the property if all feature bits are cleared. */
863 if (aprop->u.number == 0)
864 aprop->pr_kind = property_remove;
865 }
866 else
867 {
868 features = 0;
869 if (info->ibt)
870 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
871 if (info->shstk)
872 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
873 if (features)
874 {
875 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
876 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
877 if (aprop != NULL)
878 {
879 number = aprop->u.number;
880 aprop->u.number = number | features;
881 updated = number != (unsigned int) aprop->u.number;
882 }
883 else
884 {
885 bprop->u.number |= features;
886 updated = TRUE;
887 }
888 }
889 else if (aprop != NULL)
890 {
891 aprop->pr_kind = property_remove;
892 updated = TRUE;
893 }
894 }
895 break;
896
897 default:
898 /* Never should happen. */
899 abort ();
900 }
901
902 return updated;
903 }
904
905 /* Set up x86 GNU properties. Return the first relocatable ELF input
906 with GNU properties if found. Otherwise, return NULL. */
907
908 bfd *
909 _bfd_x86_elf_link_setup_gnu_properties
910 (struct bfd_link_info *info,
911 struct elf_x86_plt_layout_table *plt_layout)
912 {
913 bfd_boolean normal_target;
914 bfd_boolean lazy_plt;
915 asection *sec, *pltsec;
916 bfd *dynobj;
917 bfd_boolean use_ibt_plt;
918 unsigned int plt_alignment, features;
919 struct elf_x86_link_hash_table *htab;
920 bfd *pbfd;
921 bfd *ebfd = NULL;
922 elf_property *prop;
923 const struct elf_backend_data *bed;
924 unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
925 unsigned int got_align;
926
927 features = 0;
928 if (info->ibt)
929 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
930 if (info->shstk)
931 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
932
933 /* Find a normal input file with GNU property note. */
934 for (pbfd = info->input_bfds;
935 pbfd != NULL;
936 pbfd = pbfd->link.next)
937 if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
938 && bfd_count_sections (pbfd) != 0)
939 {
940 ebfd = pbfd;
941
942 if (elf_properties (pbfd) != NULL)
943 break;
944 }
945
946 if (ebfd != NULL && features)
947 {
948 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and
949 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
950 prop = _bfd_elf_get_property (ebfd,
951 GNU_PROPERTY_X86_FEATURE_1_AND,
952 4);
953 prop->u.number |= features;
954 prop->pr_kind = property_number;
955
956 /* Create the GNU property note section if needed. */
957 if (pbfd == NULL)
958 {
959 sec = bfd_make_section_with_flags (ebfd,
960 NOTE_GNU_PROPERTY_SECTION_NAME,
961 (SEC_ALLOC
962 | SEC_LOAD
963 | SEC_IN_MEMORY
964 | SEC_READONLY
965 | SEC_HAS_CONTENTS
966 | SEC_DATA));
967 if (sec == NULL)
968 info->callbacks->einfo (_("%F: failed to create GNU property section\n"));
969
970 if (!bfd_set_section_alignment (ebfd, sec, class_align))
971 {
972 error_alignment:
973 info->callbacks->einfo (_("%F%A: failed to align section\n"),
974 sec);
975 }
976
977 elf_section_type (sec) = SHT_NOTE;
978 }
979 }
980
981 pbfd = _bfd_elf_link_setup_gnu_properties (info);
982
983 if (bfd_link_relocatable (info))
984 return pbfd;
985
986 bed = get_elf_backend_data (info->output_bfd);
987
988 htab = elf_x86_hash_table (info, bed->target_id);
989 if (htab == NULL)
990 return pbfd;
991
992 use_ibt_plt = info->ibtplt || info->ibt;
993 if (!use_ibt_plt && pbfd != NULL)
994 {
995 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
996 elf_property_list *p;
997
998 /* The property list is sorted in order of type. */
999 for (p = elf_properties (pbfd); p; p = p->next)
1000 {
1001 if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
1002 {
1003 use_ibt_plt = !!(p->property.u.number
1004 & GNU_PROPERTY_X86_FEATURE_1_IBT);
1005 break;
1006 }
1007 else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
1008 break;
1009 }
1010 }
1011
1012 dynobj = htab->elf.dynobj;
1013
1014 /* Set htab->elf.dynobj here so that there is no need to check and
1015 set it in check_relocs. */
1016 if (dynobj == NULL)
1017 {
1018 if (pbfd != NULL)
1019 {
1020 htab->elf.dynobj = pbfd;
1021 dynobj = pbfd;
1022 }
1023 else
1024 {
1025 bfd *abfd;
1026
1027 /* Find a normal input file to hold linker created
1028 sections. */
1029 for (abfd = info->input_bfds;
1030 abfd != NULL;
1031 abfd = abfd->link.next)
1032 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1033 && (abfd->flags
1034 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
1035 {
1036 htab->elf.dynobj = abfd;
1037 dynobj = abfd;
1038 break;
1039 }
1040 }
1041 }
1042
1043 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
1044 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
1045 canonical function address. */
1046 htab->plt.has_plt0 = 1;
1047 normal_target = plt_layout->normal_target;
1048
1049 if (normal_target)
1050 {
1051 if (use_ibt_plt)
1052 {
1053 htab->lazy_plt = plt_layout->lazy_ibt_plt;
1054 htab->non_lazy_plt = plt_layout->non_lazy_ibt_plt;
1055 }
1056 else
1057 {
1058 htab->lazy_plt = plt_layout->lazy_plt;
1059 htab->non_lazy_plt = plt_layout->non_lazy_plt;
1060 }
1061 }
1062 else
1063 {
1064 htab->lazy_plt = plt_layout->lazy_plt;
1065 htab->non_lazy_plt = NULL;
1066 }
1067
1068 pltsec = htab->elf.splt;
1069
1070 /* If the non-lazy PLT is available, use it for all PLT entries if
1071 there are no PLT0 or no .plt section. */
1072 if (htab->non_lazy_plt != NULL
1073 && (!htab->plt.has_plt0 || pltsec == NULL))
1074 {
1075 lazy_plt = FALSE;
1076 if (bfd_link_pic (info))
1077 htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
1078 else
1079 htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
1080 htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
1081 htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
1082 htab->plt.plt_got_insn_size
1083 = htab->non_lazy_plt->plt_got_insn_size;
1084 htab->plt.eh_frame_plt_size
1085 = htab->non_lazy_plt->eh_frame_plt_size;
1086 htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
1087 }
1088 else
1089 {
1090 lazy_plt = TRUE;
1091 if (bfd_link_pic (info))
1092 {
1093 htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
1094 htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
1095 }
1096 else
1097 {
1098 htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
1099 htab->plt.plt_entry = htab->lazy_plt->plt_entry;
1100 }
1101 htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
1102 htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
1103 htab->plt.plt_got_insn_size
1104 = htab->lazy_plt->plt_got_insn_size;
1105 htab->plt.eh_frame_plt_size
1106 = htab->lazy_plt->eh_frame_plt_size;
1107 htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
1108 }
1109
1110 /* Return if there are no normal input files. */
1111 if (dynobj == NULL)
1112 return pbfd;
1113
1114 if (plt_layout->is_vxworks
1115 && !elf_vxworks_create_dynamic_sections (dynobj, info,
1116 &htab->srelplt2))
1117 {
1118 info->callbacks->einfo (_("%F: failed to create VxWorks dynamic sections\n"));
1119 return pbfd;
1120 }
1121
1122 /* Since create_dynamic_sections isn't always called, but GOT
1123 relocations need GOT relocations, create them here so that we
1124 don't need to do it in check_relocs. */
1125 if (htab->elf.sgot == NULL
1126 && !_bfd_elf_create_got_section (dynobj, info))
1127 info->callbacks->einfo (_("%F: failed to create GOT sections\n"));
1128
1129 got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
1130
1131 /* Align .got and .got.plt sections to their entry size. Do it here
1132 instead of in create_dynamic_sections so that they are always
1133 properly aligned even if create_dynamic_sections isn't called. */
1134 sec = htab->elf.sgot;
1135 if (!bfd_set_section_alignment (dynobj, sec, got_align))
1136 goto error_alignment;
1137
1138 sec = htab->elf.sgotplt;
1139 if (!bfd_set_section_alignment (dynobj, sec, got_align))
1140 goto error_alignment;
1141
1142 /* Create the ifunc sections here so that check_relocs can be
1143 simplified. */
1144 if (!_bfd_elf_create_ifunc_sections (dynobj, info))
1145 info->callbacks->einfo (_("%F: failed to create ifunc sections\n"));
1146
1147 plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
1148
1149 if (pltsec != NULL)
1150 {
1151 /* Whe creating executable, set the contents of the .interp
1152 section to the interpreter. */
1153 if (bfd_link_executable (info) && !info->nointerp)
1154 {
1155 asection *s = bfd_get_linker_section (dynobj, ".interp");
1156 if (s == NULL)
1157 abort ();
1158 s->size = htab->dynamic_interpreter_size;
1159 s->contents = (unsigned char *) htab->dynamic_interpreter;
1160 htab->interp = s;
1161 }
1162
1163 /* Don't change PLT section alignment for NaCl since it uses
1164 64-byte PLT entry and sets PLT section alignment to 32
1165 bytes. Don't create additional PLT sections for NaCl. */
1166 if (normal_target)
1167 {
1168 flagword pltflags = (bed->dynamic_sec_flags
1169 | SEC_ALLOC
1170 | SEC_CODE
1171 | SEC_LOAD
1172 | SEC_READONLY);
1173 unsigned int non_lazy_plt_alignment
1174 = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
1175
1176 sec = pltsec;
1177 if (!bfd_set_section_alignment (sec->owner, sec,
1178 plt_alignment))
1179 goto error_alignment;
1180
1181 /* Create the GOT procedure linkage table. */
1182 sec = bfd_make_section_anyway_with_flags (dynobj,
1183 ".plt.got",
1184 pltflags);
1185 if (sec == NULL)
1186 info->callbacks->einfo (_("%F: failed to create GOT PLT section\n"));
1187
1188 if (!bfd_set_section_alignment (dynobj, sec,
1189 non_lazy_plt_alignment))
1190 goto error_alignment;
1191
1192 htab->plt_got = sec;
1193
1194 if (lazy_plt)
1195 {
1196 sec = NULL;
1197
1198 if (use_ibt_plt)
1199 {
1200 /* Create the second PLT for Intel IBT support. IBT
1201 PLT is supported only for non-NaCl target and is
1202 is needed only for lazy binding. */
1203 sec = bfd_make_section_anyway_with_flags (dynobj,
1204 ".plt.sec",
1205 pltflags);
1206 if (sec == NULL)
1207 info->callbacks->einfo (_("%F: failed to create IBT-enabled PLT section\n"));
1208
1209 if (!bfd_set_section_alignment (dynobj, sec,
1210 plt_alignment))
1211 goto error_alignment;
1212 }
1213 else if (info->bndplt && ABI_64_P (dynobj))
1214 {
1215 /* Create the second PLT for Intel MPX support. MPX
1216 PLT is supported only for non-NaCl target in 64-bit
1217 mode and is needed only for lazy binding. */
1218 sec = bfd_make_section_anyway_with_flags (dynobj,
1219 ".plt.sec",
1220 pltflags);
1221 if (sec == NULL)
1222 info->callbacks->einfo (_("%F: failed to create BND PLT section\n"));
1223
1224 if (!bfd_set_section_alignment (dynobj, sec,
1225 non_lazy_plt_alignment))
1226 goto error_alignment;
1227 }
1228
1229 htab->plt_second = sec;
1230 }
1231 }
1232
1233 if (!info->no_ld_generated_unwind_info)
1234 {
1235 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
1236 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1237 | SEC_LINKER_CREATED);
1238
1239 sec = bfd_make_section_anyway_with_flags (dynobj,
1240 ".eh_frame",
1241 flags);
1242 if (sec == NULL)
1243 info->callbacks->einfo (_("%F: failed to create PLT .eh_frame section\n"));
1244
1245 if (!bfd_set_section_alignment (dynobj, sec, class_align))
1246 goto error_alignment;
1247
1248 htab->plt_eh_frame = sec;
1249
1250 if (htab->plt_got != NULL)
1251 {
1252 sec = bfd_make_section_anyway_with_flags (dynobj,
1253 ".eh_frame",
1254 flags);
1255 if (sec == NULL)
1256 info->callbacks->einfo (_("%F: failed to create GOT PLT .eh_frame section\n"));
1257
1258 if (!bfd_set_section_alignment (dynobj, sec, class_align))
1259 goto error_alignment;
1260
1261 htab->plt_got_eh_frame = sec;
1262 }
1263
1264 if (htab->plt_second != NULL)
1265 {
1266 sec = bfd_make_section_anyway_with_flags (dynobj,
1267 ".eh_frame",
1268 flags);
1269 if (sec == NULL)
1270 info->callbacks->einfo (_("%F: failed to create the second PLT .eh_frame section\n"));
1271
1272 if (!bfd_set_section_alignment (dynobj, sec, class_align))
1273 goto error_alignment;
1274
1275 htab->plt_second_eh_frame = sec;
1276 }
1277 }
1278 }
1279
1280 if (normal_target)
1281 {
1282 /* The .iplt section is used for IFUNC symbols in static
1283 executables. */
1284 sec = htab->elf.iplt;
1285 if (sec != NULL
1286 && !bfd_set_section_alignment (sec->owner, sec,
1287 plt_alignment))
1288 goto error_alignment;
1289 }
1290
1291 return pbfd;
1292 }
This page took 0.056471 seconds and 4 git commands to generate.