6b7c119393ee96eeb84f151a5bc14f2cb218cfd6
[external/binutils.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->sizeof_reloc = sizeof (Elf64_External_Rela);
281       ret->got_entry_size = 8;
282       ret->pointer_r_type = R_X86_64_64;
283       ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
284       ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
285       ret->tls_get_addr = "__tls_get_addr";
286     }
287   else
288 #endif
289     {
290       ret->r_info = elf32_r_info;
291       ret->r_sym = elf32_r_sym;
292       if (bed->target_id == X86_64_ELF_DATA)
293         {
294           ret->sizeof_reloc = sizeof (Elf32_External_Rela);
295           ret->got_entry_size = 8;
296           ret->pointer_r_type = R_X86_64_32;
297           ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER;
298           ret->dynamic_interpreter_size
299             = sizeof ELFX32_DYNAMIC_INTERPRETER;
300           ret->tls_get_addr = "__tls_get_addr";
301         }
302       else
303         {
304           ret->sizeof_reloc = sizeof (Elf32_External_Rel);
305           ret->got_entry_size = 4;
306           ret->pointer_r_type = R_386_32;
307           ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
308           ret->dynamic_interpreter_size
309             = sizeof ELF32_DYNAMIC_INTERPRETER;
310           ret->tls_get_addr = "___tls_get_addr";
311         }
312     }
313
314   ret->loc_hash_table = htab_try_create (1024,
315                                          _bfd_x86_elf_local_htab_hash,
316                                          _bfd_x86_elf_local_htab_eq,
317                                          NULL);
318   ret->loc_hash_memory = objalloc_create ();
319   if (!ret->loc_hash_table || !ret->loc_hash_memory)
320     {
321       elf_x86_link_hash_table_free (abfd);
322       return NULL;
323     }
324   ret->elf.root.hash_table_free = elf_x86_link_hash_table_free;
325
326   return &ret->elf.root;
327 }
328
329 /* Sort relocs into address order.  */
330
331 int
332 _bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
333 {
334   const arelent *a = * (const arelent **) ap;
335   const arelent *b = * (const arelent **) bp;
336
337   if (a->address > b->address)
338     return 1;
339   else if (a->address < b->address)
340     return -1;
341   else
342     return 0;
343 }
344
345 bfd_boolean
346 _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
347 {
348   if (!bfd_link_relocatable (info))
349     {
350       /* Check for __tls_get_addr reference.  */
351       struct elf_x86_link_hash_table *htab;
352       const struct elf_backend_data *bed = get_elf_backend_data (abfd);
353       htab = elf_x86_hash_table (info, bed->target_id);
354       if (htab)
355         {
356           struct elf_link_hash_entry *h
357             = elf_link_hash_lookup (elf_hash_table (info),
358                                     htab->tls_get_addr,
359                                     FALSE, FALSE, FALSE);
360           if (h != NULL)
361             ((struct elf_x86_link_hash_entry *) h)->tls_get_addr = 1;
362         }
363     }
364
365   /* Invoke the regular ELF backend linker to do all the work.  */
366   return _bfd_elf_link_check_relocs (abfd, info);
367 }
368
369 bfd_boolean
370 _bfd_x86_elf_always_size_sections (bfd *output_bfd,
371                                    struct bfd_link_info *info)
372 {
373   asection *tls_sec = elf_hash_table (info)->tls_sec;
374
375   if (tls_sec)
376     {
377       struct elf_link_hash_entry *tlsbase;
378
379       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
380                                       "_TLS_MODULE_BASE_",
381                                       FALSE, FALSE, FALSE);
382
383       if (tlsbase && tlsbase->type == STT_TLS)
384         {
385           struct elf_x86_link_hash_table *htab;
386           struct bfd_link_hash_entry *bh = NULL;
387           const struct elf_backend_data *bed
388             = get_elf_backend_data (output_bfd);
389
390           htab = elf_x86_hash_table (info, bed->target_id);
391           if (htab == NULL)
392             return FALSE;
393
394           if (!(_bfd_generic_link_add_one_symbol
395                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
396                  tls_sec, 0, NULL, FALSE,
397                  bed->collect, &bh)))
398             return FALSE;
399
400           htab->tls_module_base = bh;
401
402           tlsbase = (struct elf_link_hash_entry *)bh;
403           tlsbase->def_regular = 1;
404           tlsbase->other = STV_HIDDEN;
405           tlsbase->root.linker_def = 1;
406           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
407         }
408     }
409
410   return TRUE;
411 }
412
413 void
414 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
415                                      const Elf_Internal_Sym *isym,
416                                      bfd_boolean definition,
417                                      bfd_boolean dynamic ATTRIBUTE_UNUSED)
418 {
419   if (definition)
420     {
421       struct elf_x86_link_hash_entry *eh
422         = (struct elf_x86_link_hash_entry *) h;
423       eh->def_protected = (ELF_ST_VISIBILITY (isym->st_other)
424                            == STV_PROTECTED);
425     }
426 }
427
428 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
429
430 void
431 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
432                                    struct elf_link_hash_entry *dir,
433                                    struct elf_link_hash_entry *ind)
434 {
435   struct elf_x86_link_hash_entry *edir, *eind;
436
437   edir = (struct elf_x86_link_hash_entry *) dir;
438   eind = (struct elf_x86_link_hash_entry *) ind;
439
440   if (eind->dyn_relocs != NULL)
441     {
442       if (edir->dyn_relocs != NULL)
443         {
444           struct elf_dyn_relocs **pp;
445           struct elf_dyn_relocs *p;
446
447           /* Add reloc counts against the indirect sym to the direct sym
448              list.  Merge any entries against the same section.  */
449           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
450             {
451               struct elf_dyn_relocs *q;
452
453               for (q = edir->dyn_relocs; q != NULL; q = q->next)
454                 if (q->sec == p->sec)
455                   {
456                     q->pc_count += p->pc_count;
457                     q->count += p->count;
458                     *pp = p->next;
459                     break;
460                   }
461               if (q == NULL)
462                 pp = &p->next;
463             }
464           *pp = edir->dyn_relocs;
465         }
466
467       edir->dyn_relocs = eind->dyn_relocs;
468       eind->dyn_relocs = NULL;
469     }
470
471   if (ind->root.type == bfd_link_hash_indirect
472       && dir->got.refcount <= 0)
473     {
474       edir->tls_type = eind->tls_type;
475       eind->tls_type = GOT_UNKNOWN;
476     }
477
478   /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
479      generate a R_386_COPY reloc.  */
480   edir->gotoff_ref |= eind->gotoff_ref;
481
482   edir->has_got_reloc |= eind->has_got_reloc;
483   edir->has_non_got_reloc |= eind->has_non_got_reloc;
484
485   if (ELIMINATE_COPY_RELOCS
486       && ind->root.type != bfd_link_hash_indirect
487       && dir->dynamic_adjusted)
488     {
489       /* If called to transfer flags for a weakdef during processing
490          of elf_adjust_dynamic_symbol, don't copy non_got_ref.
491          We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
492       if (dir->versioned != versioned_hidden)
493         dir->ref_dynamic |= ind->ref_dynamic;
494       dir->ref_regular |= ind->ref_regular;
495       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
496       dir->needs_plt |= ind->needs_plt;
497       dir->pointer_equality_needed |= ind->pointer_equality_needed;
498     }
499   else
500     {
501       if (eind->func_pointer_refcount > 0)
502         {
503           edir->func_pointer_refcount += eind->func_pointer_refcount;
504           eind->func_pointer_refcount = 0;
505         }
506
507       _bfd_elf_link_hash_copy_indirect (info, dir, ind);
508     }
509 }
510
511 /* Remove undefined weak symbol from the dynamic symbol table if it
512    is resolved to 0.   */
513
514 bfd_boolean
515 _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
516                            struct elf_link_hash_entry *h)
517 {
518   if (h->dynindx != -1)
519     {
520       const struct elf_backend_data *bed
521         = get_elf_backend_data (info->output_bfd);
522       if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
523                                            bed->target_id,
524                                            elf_x86_hash_entry (h)->has_got_reloc,
525                                            elf_x86_hash_entry (h)))
526         {
527           h->dynindx = -1;
528           _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
529                                   h->dynstr_index);
530         }
531     }
532   return TRUE;
533 }
534
535 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
536
537 bfd_boolean
538 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
539 {
540   if (h->plt.offset != (bfd_vma) -1
541       && !h->def_regular
542       && !h->pointer_equality_needed)
543     return FALSE;
544
545   return _bfd_elf_hash_symbol (h);
546 }
547
548 /* Adjust a symbol defined by a dynamic object and referenced by a
549    regular object.  The current definition is in some section of the
550    dynamic object, but we're not including those sections.  We have to
551    change the definition to something the rest of the link can
552    understand.  */
553
554 bfd_boolean
555 _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
556                                     struct elf_link_hash_entry *h)
557 {
558   struct elf_x86_link_hash_table *htab;
559   asection *s, *srel;
560   struct elf_x86_link_hash_entry *eh;
561   struct elf_dyn_relocs *p;
562   const struct elf_backend_data *bed
563     = get_elf_backend_data (info->output_bfd);
564
565   /* STT_GNU_IFUNC symbol must go through PLT. */
566   if (h->type == STT_GNU_IFUNC)
567     {
568       /* All local STT_GNU_IFUNC references must be treate as local
569          calls via local PLT.  */
570       if (h->ref_regular
571           && SYMBOL_CALLS_LOCAL (info, h))
572         {
573           bfd_size_type pc_count = 0, count = 0;
574           struct elf_dyn_relocs **pp;
575
576           eh = (struct elf_x86_link_hash_entry *) h;
577           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
578             {
579               pc_count += p->pc_count;
580               p->count -= p->pc_count;
581               p->pc_count = 0;
582               count += p->count;
583               if (p->count == 0)
584                 *pp = p->next;
585               else
586                 pp = &p->next;
587             }
588
589           if (pc_count || count)
590             {
591               h->non_got_ref = 1;
592               if (pc_count)
593                 {
594                   /* Increment PLT reference count only for PC-relative
595                      references.  */
596                   h->needs_plt = 1;
597                   if (h->plt.refcount <= 0)
598                     h->plt.refcount = 1;
599                   else
600                     h->plt.refcount += 1;
601                 }
602             }
603         }
604
605       if (h->plt.refcount <= 0)
606         {
607           h->plt.offset = (bfd_vma) -1;
608           h->needs_plt = 0;
609         }
610       return TRUE;
611     }
612
613   /* If this is a function, put it in the procedure linkage table.  We
614      will fill in the contents of the procedure linkage table later,
615      when we know the address of the .got section.  */
616   if (h->type == STT_FUNC
617       || h->needs_plt)
618     {
619       if (h->plt.refcount <= 0
620           || SYMBOL_CALLS_LOCAL (info, h)
621           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
622               && h->root.type == bfd_link_hash_undefweak))
623         {
624           /* This case can occur if we saw a PLT32 reloc in an input
625              file, but the symbol was never referred to by a dynamic
626              object, or if all references were garbage collected.  In
627              such a case, we don't actually need to build a procedure
628              linkage table, and we can just do a PC32 reloc instead.  */
629           h->plt.offset = (bfd_vma) -1;
630           h->needs_plt = 0;
631         }
632
633       return TRUE;
634     }
635   else
636     /* It's possible that we incorrectly decided a .plt reloc was needed
637      * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
638        check_relocs.  We can't decide accurately between function and
639        non-function syms in check-relocs;  Objects loaded later in
640        the link may change h->type.  So fix it now.  */
641     h->plt.offset = (bfd_vma) -1;
642
643   eh = (struct elf_x86_link_hash_entry *) h;
644
645   /* If this is a weak symbol, and there is a real definition, the
646      processor independent code will have arranged for us to see the
647      real definition first, and we can just use the same value.  */
648   if (h->u.weakdef != NULL)
649     {
650       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
651                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
652       h->root.u.def.section = h->u.weakdef->root.u.def.section;
653       h->root.u.def.value = h->u.weakdef->root.u.def.value;
654       if (ELIMINATE_COPY_RELOCS
655           || info->nocopyreloc
656           || SYMBOL_NO_COPYRELOC (info, eh))
657         {
658           /* NB: needs_copy is always 0 for i386.  */
659           h->non_got_ref = h->u.weakdef->non_got_ref;
660           eh->needs_copy = h->u.weakdef->needs_copy;
661         }
662       return TRUE;
663     }
664
665   /* This is a reference to a symbol defined by a dynamic object which
666      is not a function.  */
667
668   /* If we are creating a shared library, we must presume that the
669      only references to the symbol are via the global offset table.
670      For such cases we need not do anything here; the relocations will
671      be handled correctly by relocate_section.  */
672   if (!bfd_link_executable (info))
673     return TRUE;
674
675   /* If there are no references to this symbol that do not use the
676      GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
677      reloc.  NB: gotoff_ref is always 0 for x86-64.  */
678   if (!h->non_got_ref && !eh->gotoff_ref)
679     return TRUE;
680
681   /* If -z nocopyreloc was given, we won't generate them either.  */
682   if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
683     {
684       h->non_got_ref = 0;
685       return TRUE;
686     }
687
688   htab = elf_x86_hash_table (info, bed->target_id);
689   if (htab == NULL)
690     return FALSE;
691
692   /* If there aren't any dynamic relocs in read-only sections nor
693      R_386_GOTOFF relocation, then we can keep the dynamic relocs and
694      avoid the copy reloc.  This doesn't work on VxWorks, where we can
695      not have dynamic relocations (other than copy and jump slot
696      relocations) in an executable.  */
697   if (ELIMINATE_COPY_RELOCS
698       && (bed->target_id == X86_64_ELF_DATA
699           || (!eh->gotoff_ref
700               && !htab->is_vxworks)))
701     {
702       for (p = eh->dyn_relocs; p != NULL; p = p->next)
703         {
704           s = p->sec->output_section;
705           if (s != NULL && (s->flags & SEC_READONLY) != 0)
706             break;
707         }
708
709       /* If we didn't find any dynamic relocs in read-only sections,
710          then we'll be keeping the dynamic relocs and avoiding the copy
711          reloc.  */
712       if (p == NULL)
713         {
714           h->non_got_ref = 0;
715           return TRUE;
716         }
717     }
718
719   /* We must allocate the symbol in our .dynbss section, which will
720      become part of the .bss section of the executable.  There will be
721      an entry for this symbol in the .dynsym section.  The dynamic
722      object will contain position independent code, so all references
723      from the dynamic object to this symbol will go through the global
724      offset table.  The dynamic linker will use the .dynsym entry to
725      determine the address it must put in the global offset table, so
726      both the dynamic object and the regular object will refer to the
727      same memory location for the variable.  */
728
729   /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
730      dynamic linker to copy the initial value out of the dynamic object
731      and into the runtime process image.  */
732   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
733     {
734       s = htab->elf.sdynrelro;
735       srel = htab->elf.sreldynrelro;
736     }
737   else
738     {
739       s = htab->elf.sdynbss;
740       srel = htab->elf.srelbss;
741     }
742   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
743     {
744       srel->size += htab->sizeof_reloc;
745       h->needs_copy = 1;
746     }
747
748   return _bfd_elf_adjust_dynamic_copy (info, h, s);
749 }
750
751 /* Return the section that should be marked against GC for a given
752    relocation.  */
753
754 asection *
755 _bfd_x86_elf_gc_mark_hook (asection *sec,
756                            struct bfd_link_info *info,
757                            Elf_Internal_Rela *rel,
758                            struct elf_link_hash_entry *h,
759                            Elf_Internal_Sym *sym)
760 {
761   /* Compiler should optimize this out.  */
762   if (((unsigned int) R_X86_64_GNU_VTINHERIT
763        != (unsigned int) R_386_GNU_VTINHERIT)
764       || ((unsigned int) R_X86_64_GNU_VTENTRY
765           != (unsigned int) R_386_GNU_VTENTRY))
766     abort ();
767
768   if (h != NULL)
769     switch (ELF32_R_TYPE (rel->r_info))
770       {
771       case R_X86_64_GNU_VTINHERIT:
772       case R_X86_64_GNU_VTENTRY:
773         return NULL;
774       }
775
776   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
777 }
778
779 static bfd_vma
780 elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
781                           bfd_vma off,
782                           bfd_vma offset ATTRIBUTE_UNUSED,
783                           bfd_vma got_addr)
784 {
785   return got_addr + off;
786 }
787
788 static bfd_vma
789 elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
790                             bfd_vma off,
791                             bfd_vma offset,
792                             bfd_vma got_addr ATTRIBUTE_UNUSED)
793 {
794   return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
795 }
796
797 static bfd_boolean
798 elf_i386_valid_plt_reloc_p (unsigned int type)
799 {
800   return (type == R_386_JUMP_SLOT
801           || type == R_386_GLOB_DAT
802           || type == R_386_IRELATIVE);
803 }
804
805 static bfd_boolean
806 elf_x86_64_valid_plt_reloc_p (unsigned int type)
807 {
808   return (type == R_X86_64_JUMP_SLOT
809           || type == R_X86_64_GLOB_DAT
810           || type == R_X86_64_IRELATIVE);
811 }
812
813 long
814 _bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
815                                    long count,
816                                    long relsize,
817                                    bfd_vma got_addr,
818                                    struct elf_x86_plt plts[],
819                                    asymbol **dynsyms,
820                                    asymbol **ret)
821 {
822   long size, i, n, len;
823   int j;
824   unsigned int plt_got_offset, plt_entry_size;
825   asymbol *s;
826   bfd_byte *plt_contents;
827   long dynrelcount;
828   arelent **dynrelbuf, *p;
829   char *names;
830   const struct elf_backend_data *bed;
831   bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
832                               bfd_vma);
833   bfd_boolean (*valid_plt_reloc_p) (unsigned int);
834
835   if (count == 0)
836     return -1;
837
838   dynrelbuf = (arelent **) bfd_malloc (relsize);
839   if (dynrelbuf == NULL)
840     return -1;
841
842   dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
843                                                 dynsyms);
844
845   /* Sort the relocs by address.  */
846   qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
847          _bfd_x86_elf_compare_relocs);
848
849   size = count * sizeof (asymbol);
850
851   /* Allocate space for @plt suffixes.  */
852   n = 0;
853   for (i = 0; i < dynrelcount; i++)
854     {
855       p = dynrelbuf[i];
856       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
857       if (p->addend != 0)
858         size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
859     }
860
861   s = *ret = (asymbol *) bfd_zmalloc (size);
862   if (s == NULL)
863     goto bad_return;
864
865   bed = get_elf_backend_data (abfd);
866
867   if (bed->target_id == X86_64_ELF_DATA)
868     {
869       get_plt_got_vma = elf_x86_64_get_plt_got_vma;
870       valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
871     }
872   else
873     {
874       get_plt_got_vma = elf_i386_get_plt_got_vma;
875       valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
876       if (got_addr)
877         {
878           /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
879              address.  */
880           asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
881           if (sec != NULL)
882             got_addr = sec->vma;
883           else
884             {
885               sec = bfd_get_section_by_name (abfd, ".got");
886               if (sec != NULL)
887                 got_addr = sec->vma;
888             }
889
890           if (got_addr == (bfd_vma) -1)
891             goto bad_return;
892         }
893     }
894
895   /* Check for each PLT section.  */
896   names = (char *) (s + count);
897   size = 0;
898   n = 0;
899   for (j = 0; plts[j].name != NULL; j++)
900     if ((plt_contents = plts[j].contents) != NULL)
901       {
902         long k;
903         bfd_vma offset;
904         asection *plt;
905         struct elf_x86_plt *plt_p = &plts[j];
906
907         plt_got_offset = plt_p->plt_got_offset;
908         plt_entry_size = plt_p->plt_entry_size;
909
910         plt = plt_p->sec;
911
912         if ((plt_p->type & plt_lazy))
913           {
914             /* Skip PLT0 in lazy PLT.  */
915             k = 1;
916             offset = plt_entry_size;
917           }
918         else
919           {
920             k = 0;
921             offset = 0;
922           }
923
924         /* Check each PLT entry against dynamic relocations.  */
925         for (; k < plt_p->count; k++)
926           {
927             int off;
928             bfd_vma got_vma;
929             long min, max, mid;
930
931             /* Get the GOT offset for i386 or the PC-relative offset
932                for x86-64, a signed 32-bit integer.  */
933             off = H_GET_32 (abfd, (plt_contents + offset
934                                    + plt_got_offset));
935             got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
936
937             /* Binary search.  */
938             p = dynrelbuf[0];
939             min = 0;
940             max = dynrelcount;
941             while ((min + 1) < max)
942               {
943                 arelent *r;
944
945                 mid = (min + max) / 2;
946                 r = dynrelbuf[mid];
947                 if (got_vma > r->address)
948                   min = mid;
949                 else if (got_vma < r->address)
950                   max = mid;
951                 else
952                   {
953                     p = r;
954                     break;
955                   }
956               }
957
958             /* Skip unknown relocation.  PR 17512: file: bc9d6cf5.  */
959             if (got_vma == p->address
960                 && p->howto != NULL
961                 && valid_plt_reloc_p (p->howto->type))
962               {
963                 *s = **p->sym_ptr_ptr;
964                 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
965                    set.  Since we are defining a symbol, ensure one
966                    of them is set.  */
967                 if ((s->flags & BSF_LOCAL) == 0)
968                   s->flags |= BSF_GLOBAL;
969                 s->flags |= BSF_SYNTHETIC;
970                 /* This is no longer a section symbol.  */
971                 s->flags &= ~BSF_SECTION_SYM;
972                 s->section = plt;
973                 s->the_bfd = plt->owner;
974                 s->value = offset;
975                 s->udata.p = NULL;
976                 s->name = names;
977                 len = strlen ((*p->sym_ptr_ptr)->name);
978                 memcpy (names, (*p->sym_ptr_ptr)->name, len);
979                 names += len;
980                 if (p->addend != 0)
981                   {
982                     char buf[30], *a;
983
984                     memcpy (names, "+0x", sizeof ("+0x") - 1);
985                     names += sizeof ("+0x") - 1;
986                     bfd_sprintf_vma (abfd, buf, p->addend);
987                     for (a = buf; *a == '0'; ++a)
988                       ;
989                     size = strlen (a);
990                     memcpy (names, a, size);
991                     names += size;
992                   }
993                 memcpy (names, "@plt", sizeof ("@plt"));
994                 names += sizeof ("@plt");
995                 n++;
996                 s++;
997               }
998             offset += plt_entry_size;
999           }
1000       }
1001
1002   /* PLT entries with R_386_TLS_DESC relocations are skipped.  */
1003   if (n == 0)
1004     {
1005 bad_return:
1006       count = -1;
1007     }
1008   else
1009     count = n;
1010
1011   for (j = 0; plts[j].name != NULL; j++)
1012     if (plts[j].contents != NULL)
1013       free (plts[j].contents);
1014
1015   free (dynrelbuf);
1016
1017   return count;
1018 }
1019
1020 /* Parse x86 GNU properties.  */
1021
1022 enum elf_property_kind
1023 _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
1024                                    bfd_byte *ptr, unsigned int datasz)
1025 {
1026   elf_property *prop;
1027
1028   switch (type)
1029     {
1030     case GNU_PROPERTY_X86_ISA_1_USED:
1031     case GNU_PROPERTY_X86_ISA_1_NEEDED:
1032     case GNU_PROPERTY_X86_FEATURE_1_AND:
1033       if (datasz != 4)
1034         {
1035           _bfd_error_handler
1036             ((type == GNU_PROPERTY_X86_ISA_1_USED
1037               ? _("error: %B: <corrupt x86 ISA used size: 0x%x>")
1038               : (type == GNU_PROPERTY_X86_ISA_1_NEEDED
1039                  ? _("error: %B: <corrupt x86 ISA needed size: 0x%x>")
1040                  : _("error: %B: <corrupt x86 feature size: 0x%x>"))),
1041              abfd, datasz);
1042           return property_corrupt;
1043         }
1044       prop = _bfd_elf_get_property (abfd, type, datasz);
1045       /* Combine properties of the same type.  */
1046       prop->u.number |= bfd_h_get_32 (abfd, ptr);
1047       prop->pr_kind = property_number;
1048       break;
1049
1050     default:
1051       return property_ignored;
1052     }
1053
1054   return property_number;
1055 }
1056
1057 /* Merge x86 GNU property BPROP with APROP.  If APROP isn't NULL,
1058    return TRUE if APROP is updated.  Otherwise, return TRUE if BPROP
1059    should be merged with ABFD.  */
1060
1061 bfd_boolean
1062 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
1063                                    bfd *abfd ATTRIBUTE_UNUSED,
1064                                    elf_property *aprop,
1065                                    elf_property *bprop)
1066 {
1067   unsigned int number, features;
1068   bfd_boolean updated = FALSE;
1069   unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
1070
1071   switch (pr_type)
1072     {
1073     case GNU_PROPERTY_X86_ISA_1_USED:
1074     case GNU_PROPERTY_X86_ISA_1_NEEDED:
1075       if (aprop != NULL && bprop != NULL)
1076         {
1077           number = aprop->u.number;
1078           aprop->u.number = number | bprop->u.number;
1079           updated = number != (unsigned int) aprop->u.number;
1080         }
1081       else
1082         {
1083           /* Return TRUE if APROP is NULL to indicate that BPROP should
1084              be added to ABFD.  */
1085           updated = aprop == NULL;
1086         }
1087       break;
1088
1089     case GNU_PROPERTY_X86_FEATURE_1_AND:
1090       /* Only one of APROP and BPROP can be NULL:
1091          1. APROP & BPROP when both APROP and BPROP aren't NULL.
1092          2. If APROP is NULL, remove x86 feature.
1093          3. Otherwise, do nothing.
1094        */
1095       if (aprop != NULL && bprop != NULL)
1096         {
1097           features = 0;
1098           if (info->ibt)
1099             features = GNU_PROPERTY_X86_FEATURE_1_IBT;
1100           if (info->shstk)
1101             features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
1102           number = aprop->u.number;
1103           /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
1104              GNU_PROPERTY_X86_FEATURE_1_SHSTK.  */
1105           aprop->u.number = (number & bprop->u.number) | features;
1106           updated = number != (unsigned int) aprop->u.number;
1107           /* Remove the property if all feature bits are cleared.  */
1108           if (aprop->u.number == 0)
1109             aprop->pr_kind = property_remove;
1110         }
1111       else
1112         {
1113           features = 0;
1114           if (info->ibt)
1115             features = GNU_PROPERTY_X86_FEATURE_1_IBT;
1116           if (info->shstk)
1117             features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
1118           if (features)
1119             {
1120               /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
1121                  GNU_PROPERTY_X86_FEATURE_1_SHSTK.  */
1122               if (aprop != NULL)
1123                 {
1124                   number = aprop->u.number;
1125                   aprop->u.number = number | features;
1126                   updated = number != (unsigned int) aprop->u.number;
1127                 }
1128               else
1129                 {
1130                   bprop->u.number |= features;
1131                   updated = TRUE;
1132                 }
1133             }
1134           else if (aprop != NULL)
1135             {
1136               aprop->pr_kind = property_remove;
1137               updated = TRUE;
1138             }
1139         }
1140       break;
1141
1142     default:
1143       /* Never should happen.  */
1144       abort ();
1145     }
1146
1147   return updated;
1148 }
1149
1150 /* Set up x86 GNU properties.  Return the first relocatable ELF input
1151    with GNU properties if found.  Otherwise, return NULL.  */
1152
1153 bfd *
1154 _bfd_x86_elf_link_setup_gnu_properties
1155   (struct bfd_link_info *info,
1156    struct elf_x86_plt_layout_table *plt_layout)
1157 {
1158   bfd_boolean normal_target;
1159   bfd_boolean lazy_plt;
1160   asection *sec, *pltsec;
1161   bfd *dynobj;
1162   bfd_boolean use_ibt_plt;
1163   unsigned int plt_alignment, features;
1164   struct elf_x86_link_hash_table *htab;
1165   bfd *pbfd;
1166   bfd *ebfd = NULL;
1167   elf_property *prop;
1168   const struct elf_backend_data *bed;
1169   unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
1170   unsigned int got_align;
1171
1172   features = 0;
1173   if (info->ibt)
1174     features = GNU_PROPERTY_X86_FEATURE_1_IBT;
1175   if (info->shstk)
1176     features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
1177
1178   /* Find a normal input file with GNU property note.  */
1179   for (pbfd = info->input_bfds;
1180        pbfd != NULL;
1181        pbfd = pbfd->link.next)
1182     if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
1183         && bfd_count_sections (pbfd) != 0)
1184       {
1185         ebfd = pbfd;
1186
1187         if (elf_properties (pbfd) != NULL)
1188           break;
1189       }
1190
1191   if (ebfd != NULL && features)
1192     {
1193       /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and
1194          GNU_PROPERTY_X86_FEATURE_1_SHSTK.  */
1195       prop = _bfd_elf_get_property (ebfd,
1196                                     GNU_PROPERTY_X86_FEATURE_1_AND,
1197                                     4);
1198       prop->u.number |= features;
1199       prop->pr_kind = property_number;
1200
1201       /* Create the GNU property note section if needed.  */
1202       if (pbfd == NULL)
1203         {
1204           sec = bfd_make_section_with_flags (ebfd,
1205                                              NOTE_GNU_PROPERTY_SECTION_NAME,
1206                                              (SEC_ALLOC
1207                                               | SEC_LOAD
1208                                               | SEC_IN_MEMORY
1209                                               | SEC_READONLY
1210                                               | SEC_HAS_CONTENTS
1211                                               | SEC_DATA));
1212           if (sec == NULL)
1213             info->callbacks->einfo (_("%F: failed to create GNU property section\n"));
1214
1215           if (!bfd_set_section_alignment (ebfd, sec, class_align))
1216             {
1217 error_alignment:
1218               info->callbacks->einfo (_("%F%A: failed to align section\n"),
1219                                       sec);
1220             }
1221
1222           elf_section_type (sec) = SHT_NOTE;
1223         }
1224     }
1225
1226   pbfd = _bfd_elf_link_setup_gnu_properties (info);
1227
1228   if (bfd_link_relocatable (info))
1229     return pbfd;
1230
1231   bed = get_elf_backend_data (info->output_bfd);
1232
1233   htab = elf_x86_hash_table (info, bed->target_id);
1234   if (htab == NULL)
1235     return pbfd;
1236
1237   htab->is_vxworks = plt_layout->is_vxworks;
1238
1239   use_ibt_plt = info->ibtplt || info->ibt;
1240   if (!use_ibt_plt && pbfd != NULL)
1241     {
1242       /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on.  */
1243       elf_property_list *p;
1244
1245       /* The property list is sorted in order of type.  */
1246       for (p = elf_properties (pbfd); p; p = p->next)
1247         {
1248           if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
1249             {
1250               use_ibt_plt = !!(p->property.u.number
1251                                & GNU_PROPERTY_X86_FEATURE_1_IBT);
1252               break;
1253             }
1254           else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
1255             break;
1256         }
1257     }
1258
1259   dynobj = htab->elf.dynobj;
1260
1261   /* Set htab->elf.dynobj here so that there is no need to check and
1262      set it in check_relocs.  */
1263   if (dynobj == NULL)
1264     {
1265       if (pbfd != NULL)
1266         {
1267           htab->elf.dynobj = pbfd;
1268           dynobj = pbfd;
1269         }
1270       else
1271         {
1272           bfd *abfd;
1273
1274           /* Find a normal input file to hold linker created
1275              sections.  */
1276           for (abfd = info->input_bfds;
1277                abfd != NULL;
1278                abfd = abfd->link.next)
1279             if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1280                 && (abfd->flags
1281                     & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
1282               {
1283                 htab->elf.dynobj = abfd;
1284                 dynobj = abfd;
1285                 break;
1286               }
1287         }
1288     }
1289
1290   /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
1291      still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
1292      canonical function address.  */
1293   htab->plt.has_plt0 = 1;
1294   normal_target = plt_layout->normal_target;
1295
1296   if (normal_target)
1297     {
1298       if (use_ibt_plt)
1299         {
1300           htab->lazy_plt = plt_layout->lazy_ibt_plt;
1301           htab->non_lazy_plt = plt_layout->non_lazy_ibt_plt;
1302         }
1303       else
1304         {
1305           htab->lazy_plt = plt_layout->lazy_plt;
1306           htab->non_lazy_plt = plt_layout->non_lazy_plt;
1307         }
1308     }
1309   else
1310     {
1311       htab->lazy_plt = plt_layout->lazy_plt;
1312       htab->non_lazy_plt = NULL;
1313     }
1314
1315   pltsec = htab->elf.splt;
1316
1317   /* If the non-lazy PLT is available, use it for all PLT entries if
1318      there are no PLT0 or no .plt section.  */
1319   if (htab->non_lazy_plt != NULL
1320       && (!htab->plt.has_plt0 || pltsec == NULL))
1321     {
1322       lazy_plt = FALSE;
1323       if (bfd_link_pic (info))
1324         htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
1325       else
1326         htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
1327       htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
1328       htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
1329       htab->plt.plt_got_insn_size
1330         = htab->non_lazy_plt->plt_got_insn_size;
1331       htab->plt.eh_frame_plt_size
1332         = htab->non_lazy_plt->eh_frame_plt_size;
1333       htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
1334     }
1335   else
1336     {
1337       lazy_plt = TRUE;
1338       if (bfd_link_pic (info))
1339         {
1340           htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
1341           htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
1342         }
1343       else
1344         {
1345           htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
1346           htab->plt.plt_entry = htab->lazy_plt->plt_entry;
1347         }
1348       htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
1349       htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
1350       htab->plt.plt_got_insn_size
1351         = htab->lazy_plt->plt_got_insn_size;
1352       htab->plt.eh_frame_plt_size
1353         = htab->lazy_plt->eh_frame_plt_size;
1354       htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
1355     }
1356
1357   /* Return if there are no normal input files.  */
1358   if (dynobj == NULL)
1359     return pbfd;
1360
1361   if (htab->is_vxworks
1362       && !elf_vxworks_create_dynamic_sections (dynobj, info,
1363                                                &htab->srelplt2))
1364     {
1365       info->callbacks->einfo (_("%F: failed to create VxWorks dynamic sections\n"));
1366       return pbfd;
1367     }
1368
1369   /* Since create_dynamic_sections isn't always called, but GOT
1370      relocations need GOT relocations, create them here so that we
1371      don't need to do it in check_relocs.  */
1372   if (htab->elf.sgot == NULL
1373       && !_bfd_elf_create_got_section (dynobj, info))
1374     info->callbacks->einfo (_("%F: failed to create GOT sections\n"));
1375
1376   got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
1377
1378   /* Align .got and .got.plt sections to their entry size.  Do it here
1379      instead of in create_dynamic_sections so that they are always
1380      properly aligned even if create_dynamic_sections isn't called.  */
1381   sec = htab->elf.sgot;
1382   if (!bfd_set_section_alignment (dynobj, sec, got_align))
1383     goto error_alignment;
1384
1385   sec = htab->elf.sgotplt;
1386   if (!bfd_set_section_alignment (dynobj, sec, got_align))
1387     goto error_alignment;
1388
1389   /* Create the ifunc sections here so that check_relocs can be
1390      simplified.  */
1391   if (!_bfd_elf_create_ifunc_sections (dynobj, info))
1392     info->callbacks->einfo (_("%F: failed to create ifunc sections\n"));
1393
1394   plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
1395
1396   if (pltsec != NULL)
1397     {
1398       /* Whe creating executable, set the contents of the .interp
1399          section to the interpreter.  */
1400       if (bfd_link_executable (info) && !info->nointerp)
1401         {
1402           asection *s = bfd_get_linker_section (dynobj, ".interp");
1403           if (s == NULL)
1404             abort ();
1405           s->size = htab->dynamic_interpreter_size;
1406           s->contents = (unsigned char *) htab->dynamic_interpreter;
1407           htab->interp = s;
1408         }
1409
1410       /* Don't change PLT section alignment for NaCl since it uses
1411          64-byte PLT entry and sets PLT section alignment to 32
1412          bytes.  Don't create additional PLT sections for NaCl.  */
1413       if (normal_target)
1414         {
1415           flagword pltflags = (bed->dynamic_sec_flags
1416                                | SEC_ALLOC
1417                                | SEC_CODE
1418                                | SEC_LOAD
1419                                | SEC_READONLY);
1420           unsigned int non_lazy_plt_alignment
1421             = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
1422
1423           sec = pltsec;
1424           if (!bfd_set_section_alignment (sec->owner, sec,
1425                                           plt_alignment))
1426             goto error_alignment;
1427
1428           /* Create the GOT procedure linkage table.  */
1429           sec = bfd_make_section_anyway_with_flags (dynobj,
1430                                                     ".plt.got",
1431                                                     pltflags);
1432           if (sec == NULL)
1433             info->callbacks->einfo (_("%F: failed to create GOT PLT section\n"));
1434
1435           if (!bfd_set_section_alignment (dynobj, sec,
1436                                           non_lazy_plt_alignment))
1437             goto error_alignment;
1438
1439           htab->plt_got = sec;
1440
1441           if (lazy_plt)
1442             {
1443               sec = NULL;
1444
1445               if (use_ibt_plt)
1446                 {
1447                   /* Create the second PLT for Intel IBT support.  IBT
1448                      PLT is supported only for non-NaCl target and is
1449                      is needed only for lazy binding.  */
1450                   sec = bfd_make_section_anyway_with_flags (dynobj,
1451                                                             ".plt.sec",
1452                                                             pltflags);
1453                   if (sec == NULL)
1454                     info->callbacks->einfo (_("%F: failed to create IBT-enabled PLT section\n"));
1455
1456                   if (!bfd_set_section_alignment (dynobj, sec,
1457                                                   plt_alignment))
1458                     goto error_alignment;
1459                 }
1460               else if (info->bndplt && ABI_64_P (dynobj))
1461                 {
1462                   /* Create the second PLT for Intel MPX support.  MPX
1463                      PLT is supported only for non-NaCl target in 64-bit
1464                      mode and is needed only for lazy binding.  */
1465                   sec = bfd_make_section_anyway_with_flags (dynobj,
1466                                                             ".plt.sec",
1467                                                             pltflags);
1468                   if (sec == NULL)
1469                     info->callbacks->einfo (_("%F: failed to create BND PLT section\n"));
1470
1471                   if (!bfd_set_section_alignment (dynobj, sec,
1472                                                   non_lazy_plt_alignment))
1473                     goto error_alignment;
1474                 }
1475
1476               htab->plt_second = sec;
1477             }
1478         }
1479
1480       if (!info->no_ld_generated_unwind_info)
1481         {
1482           flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
1483                             | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1484                             | SEC_LINKER_CREATED);
1485
1486           sec = bfd_make_section_anyway_with_flags (dynobj,
1487                                                     ".eh_frame",
1488                                                     flags);
1489           if (sec == NULL)
1490             info->callbacks->einfo (_("%F: failed to create PLT .eh_frame section\n"));
1491
1492           if (!bfd_set_section_alignment (dynobj, sec, class_align))
1493             goto error_alignment;
1494
1495           htab->plt_eh_frame = sec;
1496
1497           if (htab->plt_got != NULL)
1498             {
1499               sec = bfd_make_section_anyway_with_flags (dynobj,
1500                                                         ".eh_frame",
1501                                                         flags);
1502               if (sec == NULL)
1503                 info->callbacks->einfo (_("%F: failed to create GOT PLT .eh_frame section\n"));
1504
1505               if (!bfd_set_section_alignment (dynobj, sec, class_align))
1506                 goto error_alignment;
1507
1508               htab->plt_got_eh_frame = sec;
1509             }
1510
1511           if (htab->plt_second != NULL)
1512             {
1513               sec = bfd_make_section_anyway_with_flags (dynobj,
1514                                                         ".eh_frame",
1515                                                         flags);
1516               if (sec == NULL)
1517                 info->callbacks->einfo (_("%F: failed to create the second PLT .eh_frame section\n"));
1518
1519               if (!bfd_set_section_alignment (dynobj, sec, class_align))
1520                 goto error_alignment;
1521
1522               htab->plt_second_eh_frame = sec;
1523             }
1524         }
1525     }
1526
1527   if (normal_target)
1528     {
1529       /* The .iplt section is used for IFUNC symbols in static
1530          executables.  */
1531       sec = htab->elf.iplt;
1532       if (sec != NULL
1533           && !bfd_set_section_alignment (sec->owner, sec,
1534                                          plt_alignment))
1535         goto error_alignment;
1536     }
1537
1538   return pbfd;
1539 }