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