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