x86: Remove func_pointer_refcount
[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 /* Allocate space in .plt, .got and associated reloc sections for
83    dynamic relocs.  */
84
85 static bfd_boolean
86 elf_x86_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
87 {
88   struct bfd_link_info *info;
89   struct elf_x86_link_hash_table *htab;
90   struct elf_x86_link_hash_entry *eh;
91   struct elf_dyn_relocs *p;
92   unsigned int plt_entry_size;
93   bfd_boolean resolved_to_zero;
94   const struct elf_backend_data *bed;
95
96   if (h->root.type == bfd_link_hash_indirect)
97     return TRUE;
98
99   eh = (struct elf_x86_link_hash_entry *) h;
100
101   info = (struct bfd_link_info *) inf;
102   bed = get_elf_backend_data (info->output_bfd);
103   htab = elf_x86_hash_table (info, bed->target_id);
104   if (htab == NULL)
105     return FALSE;
106
107   plt_entry_size = htab->plt.plt_entry_size;
108
109   resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
110
111   /* We can't use the GOT PLT if pointer equality is needed since
112      finish_dynamic_symbol won't clear symbol value and the dynamic
113      linker won't update the GOT slot.  We will get into an infinite
114      loop at run-time.  */
115   if (htab->plt_got != NULL
116       && h->type != STT_GNU_IFUNC
117       && !h->pointer_equality_needed
118       && h->plt.refcount > 0
119       && h->got.refcount > 0)
120     {
121       /* Don't use the regular PLT if there are both GOT and GOTPLT
122          reloctions.  */
123       h->plt.offset = (bfd_vma) -1;
124
125       /* Use the GOT PLT.  */
126       eh->plt_got.refcount = 1;
127     }
128
129   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
130      here if it is defined and referenced in a non-shared object.  */
131   if (h->type == STT_GNU_IFUNC
132       && h->def_regular)
133     {
134       if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs,
135                                               &htab->readonly_dynrelocs_against_ifunc,
136                                               plt_entry_size,
137                                               (htab->plt.has_plt0
138                                                * plt_entry_size),
139                                                htab->got_entry_size,
140                                                TRUE))
141         {
142           asection *s = htab->plt_second;
143           if (h->plt.offset != (bfd_vma) -1 && s != NULL)
144             {
145               /* Use the second PLT section if it is created.  */
146               eh->plt_second.offset = s->size;
147
148               /* Make room for this entry in the second PLT section.  */
149               s->size += htab->non_lazy_plt->plt_entry_size;
150             }
151
152           return TRUE;
153         }
154       else
155         return FALSE;
156     }
157   /* Don't create the PLT entry if there are only function pointer
158      relocations which can be resolved at run-time.  */
159   else if (htab->elf.dynamic_sections_created
160            && (h->plt.refcount > 0
161                || eh->plt_got.refcount > 0))
162     {
163       bfd_boolean use_plt_got = eh->plt_got.refcount > 0;
164
165       /* Make sure this symbol is output as a dynamic symbol.
166          Undefined weak syms won't yet be marked as dynamic.  */
167       if (h->dynindx == -1
168           && !h->forced_local
169           && !resolved_to_zero
170           && h->root.type == bfd_link_hash_undefweak)
171         {
172           if (! bfd_elf_link_record_dynamic_symbol (info, h))
173             return FALSE;
174         }
175
176       if (bfd_link_pic (info)
177           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
178         {
179           asection *s = htab->elf.splt;
180           asection *second_s = htab->plt_second;
181           asection *got_s = htab->plt_got;
182
183           /* If this is the first .plt entry, make room for the special
184              first entry.  The .plt section is used by prelink to undo
185              prelinking for dynamic relocations.  */
186           if (s->size == 0)
187             s->size = htab->plt.has_plt0 * plt_entry_size;
188
189           if (use_plt_got)
190             eh->plt_got.offset = got_s->size;
191           else
192             {
193               h->plt.offset = s->size;
194               if (second_s)
195                 eh->plt_second.offset = second_s->size;
196             }
197
198           /* If this symbol is not defined in a regular file, and we are
199              not generating a shared library, then set the symbol to this
200              location in the .plt.  This is required to make function
201              pointers compare as equal between the normal executable and
202              the shared library.  */
203           if (! bfd_link_pic (info)
204               && !h->def_regular)
205             {
206               if (use_plt_got)
207                 {
208                   /* We need to make a call to the entry of the GOT PLT
209                      instead of regular PLT entry.  */
210                   h->root.u.def.section = got_s;
211                   h->root.u.def.value = eh->plt_got.offset;
212                 }
213               else
214                 {
215                   if (second_s)
216                     {
217                       /* We need to make a call to the entry of the
218                          second PLT instead of regular PLT entry.  */
219                       h->root.u.def.section = second_s;
220                       h->root.u.def.value = eh->plt_second.offset;
221                     }
222                   else
223                     {
224                       h->root.u.def.section = s;
225                       h->root.u.def.value = h->plt.offset;
226                     }
227                 }
228             }
229
230           /* Make room for this entry.  */
231           if (use_plt_got)
232             got_s->size += htab->non_lazy_plt->plt_entry_size;
233           else
234             {
235               s->size += plt_entry_size;
236               if (second_s)
237                 second_s->size += htab->non_lazy_plt->plt_entry_size;
238
239               /* We also need to make an entry in the .got.plt section,
240                  which will be placed in the .got section by the linker
241                  script.  */
242               htab->elf.sgotplt->size += htab->got_entry_size;
243
244               /* There should be no PLT relocation against resolved
245                  undefined weak symbol in executable.  */
246               if (!resolved_to_zero)
247                 {
248                   /* We also need to make an entry in the .rel.plt
249                      section.  */
250                   htab->elf.srelplt->size += htab->sizeof_reloc;
251                   htab->elf.srelplt->reloc_count++;
252                 }
253             }
254
255           if (htab->target_os == is_vxworks && !bfd_link_pic (info))
256             {
257               /* VxWorks has a second set of relocations for each PLT entry
258                  in executables.  They go in a separate relocation section,
259                  which is processed by the kernel loader.  */
260
261               /* There are two relocations for the initial PLT entry: an
262                  R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
263                  R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
264
265               asection *srelplt2 = htab->srelplt2;
266               if (h->plt.offset == plt_entry_size)
267                 srelplt2->size += (htab->sizeof_reloc * 2);
268
269               /* There are two extra relocations for each subsequent PLT entry:
270                  an R_386_32 relocation for the GOT entry, and an R_386_32
271                  relocation for the PLT entry.  */
272
273               srelplt2->size += (htab->sizeof_reloc * 2);
274             }
275         }
276       else
277         {
278           eh->plt_got.offset = (bfd_vma) -1;
279           h->plt.offset = (bfd_vma) -1;
280           h->needs_plt = 0;
281         }
282     }
283   else
284     {
285       eh->plt_got.offset = (bfd_vma) -1;
286       h->plt.offset = (bfd_vma) -1;
287       h->needs_plt = 0;
288     }
289
290   eh->tlsdesc_got = (bfd_vma) -1;
291
292   /* For i386, if R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the
293      binary, make it a R_386_TLS_LE_32 requiring no TLS entry.  For
294      x86-64, if R_X86_64_GOTTPOFF symbol is now local to the binary,
295      make it a R_X86_64_TPOFF32 requiring no GOT entry.  */
296   if (h->got.refcount > 0
297       && bfd_link_executable (info)
298       && h->dynindx == -1
299       && (elf_x86_hash_entry (h)->tls_type & GOT_TLS_IE))
300     h->got.offset = (bfd_vma) -1;
301   else if (h->got.refcount > 0)
302     {
303       asection *s;
304       bfd_boolean dyn;
305       int tls_type = elf_x86_hash_entry (h)->tls_type;
306
307       /* Make sure this symbol is output as a dynamic symbol.
308          Undefined weak syms won't yet be marked as dynamic.  */
309       if (h->dynindx == -1
310           && !h->forced_local
311           && !resolved_to_zero
312           && h->root.type == bfd_link_hash_undefweak)
313         {
314           if (! bfd_elf_link_record_dynamic_symbol (info, h))
315             return FALSE;
316         }
317
318       s = htab->elf.sgot;
319       if (GOT_TLS_GDESC_P (tls_type))
320         {
321           eh->tlsdesc_got = htab->elf.sgotplt->size
322             - elf_x86_compute_jump_table_size (htab);
323           htab->elf.sgotplt->size += 2 * htab->got_entry_size;
324           h->got.offset = (bfd_vma) -2;
325         }
326       if (! GOT_TLS_GDESC_P (tls_type)
327           || GOT_TLS_GD_P (tls_type))
328         {
329           h->got.offset = s->size;
330           s->size += htab->got_entry_size;
331           /* R_386_TLS_GD and R_X86_64_TLSGD need 2 consecutive GOT
332              slots.  */
333           if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
334             s->size += htab->got_entry_size;
335         }
336       dyn = htab->elf.dynamic_sections_created;
337       /* R_386_TLS_IE_32 needs one dynamic relocation,
338          R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
339          (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
340          need two), R_386_TLS_GD and R_X86_64_TLSGD need one if local
341          symbol and two if global.  No dynamic relocation against
342          resolved undefined weak symbol in executable.  */
343       if (tls_type == GOT_TLS_IE_BOTH)
344         htab->elf.srelgot->size += 2 * htab->sizeof_reloc;
345       else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
346                || (tls_type & GOT_TLS_IE))
347         htab->elf.srelgot->size += htab->sizeof_reloc;
348       else if (GOT_TLS_GD_P (tls_type))
349         htab->elf.srelgot->size += 2 * htab->sizeof_reloc;
350       else if (! GOT_TLS_GDESC_P (tls_type)
351                && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
352                     && !resolved_to_zero)
353                    || h->root.type != bfd_link_hash_undefweak)
354                && (bfd_link_pic (info)
355                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
356         htab->elf.srelgot->size += htab->sizeof_reloc;
357       if (GOT_TLS_GDESC_P (tls_type))
358         {
359           htab->elf.srelplt->size += htab->sizeof_reloc;
360           if (bed->target_id == X86_64_ELF_DATA)
361             htab->tlsdesc_plt = (bfd_vma) -1;
362         }
363     }
364   else
365     h->got.offset = (bfd_vma) -1;
366
367   if (eh->dyn_relocs == NULL)
368     return TRUE;
369
370   /* In the shared -Bsymbolic case, discard space allocated for
371      dynamic pc-relative relocs against symbols which turn out to be
372      defined in regular objects.  For the normal shared case, discard
373      space for pc-relative relocs that have become local due to symbol
374      visibility changes.  */
375
376   if (bfd_link_pic (info))
377     {
378       /* Relocs that use pc_count are those that appear on a call
379          insn, or certain REL relocs that can generated via assembly.
380          We want calls to protected symbols to resolve directly to the
381          function rather than going via the plt.  If people want
382          function pointer comparisons to work as expected then they
383          should avoid writing weird assembly.  */
384       if (SYMBOL_CALLS_LOCAL (info, h))
385         {
386           struct elf_dyn_relocs **pp;
387
388           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
389             {
390               p->count -= p->pc_count;
391               p->pc_count = 0;
392               if (p->count == 0)
393                 *pp = p->next;
394               else
395                 pp = &p->next;
396             }
397         }
398
399       if (htab->target_os == is_vxworks)
400         {
401           struct elf_dyn_relocs **pp;
402           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
403             {
404               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
405                 *pp = p->next;
406               else
407                 pp = &p->next;
408             }
409         }
410
411       /* Also discard relocs on undefined weak syms with non-default
412          visibility or in PIE.  */
413       if (eh->dyn_relocs != NULL)
414         {
415           if (h->root.type == bfd_link_hash_undefweak)
416             {
417               /* Undefined weak symbol is never bound locally in shared
418                  library.  */
419               if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
420                   || resolved_to_zero)
421                 {
422                   if (bed->target_id == I386_ELF_DATA
423                       && h->non_got_ref)
424                     {
425                       /* Keep dynamic non-GOT/non-PLT relocation so
426                          that we can branch to 0 without PLT.  */
427                       struct elf_dyn_relocs **pp;
428
429                       for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
430                         if (p->pc_count == 0)
431                           *pp = p->next;
432                         else
433                           {
434                             /* Remove non-R_386_PC32 relocation.  */
435                             p->count = p->pc_count;
436                             pp = &p->next;
437                           }
438
439                       /* Make sure undefined weak symbols are output
440                          as dynamic symbols in PIEs for dynamic non-GOT
441                          non-PLT reloations.  */
442                       if (eh->dyn_relocs != NULL
443                           && !bfd_elf_link_record_dynamic_symbol (info, h))
444                         return FALSE;
445                     }
446                   else
447                     eh->dyn_relocs = NULL;
448                 }
449               else if (h->dynindx == -1
450                        && !h->forced_local
451                        && !bfd_elf_link_record_dynamic_symbol (info, h))
452                 return FALSE;
453             }
454           else if (bfd_link_executable (info)
455                    && (h->needs_copy || eh->needs_copy)
456                    && h->def_dynamic
457                    && !h->def_regular)
458             {
459               /* NB: needs_copy is set only for x86-64.  For PIE,
460                  discard space for pc-relative relocs against symbols
461                  which turn out to need copy relocs.  */
462               struct elf_dyn_relocs **pp;
463
464               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
465                 {
466                   if (p->pc_count != 0)
467                     *pp = p->next;
468                   else
469                     pp = &p->next;
470                 }
471             }
472         }
473     }
474   else if (ELIMINATE_COPY_RELOCS)
475     {
476       /* For the non-shared case, discard space for relocs against
477          symbols which turn out to need copy relocs or are not
478          dynamic.  Keep dynamic relocations for run-time function
479          pointer initialization.  */
480
481       if ((!h->non_got_ref
482            || (h->root.type == bfd_link_hash_undefweak
483                && !resolved_to_zero))
484           && ((h->def_dynamic
485                && !h->def_regular)
486               || (htab->elf.dynamic_sections_created
487                   && (h->root.type == bfd_link_hash_undefweak
488                       || h->root.type == bfd_link_hash_undefined))))
489         {
490           /* Make sure this symbol is output as a dynamic symbol.
491              Undefined weak syms won't yet be marked as dynamic.  */
492           if (h->dynindx == -1
493               && !h->forced_local
494               && !resolved_to_zero
495               && h->root.type == bfd_link_hash_undefweak
496               && ! bfd_elf_link_record_dynamic_symbol (info, h))
497             return FALSE;
498
499           /* If that succeeded, we know we'll be keeping all the
500              relocs.  */
501           if (h->dynindx != -1)
502             goto keep;
503         }
504
505       eh->dyn_relocs = NULL;
506
507     keep: ;
508     }
509
510   /* Finally, allocate space.  */
511   for (p = eh->dyn_relocs; p != NULL; p = p->next)
512     {
513       asection *sreloc;
514
515       sreloc = elf_section_data (p->sec)->sreloc;
516
517       BFD_ASSERT (sreloc != NULL);
518       sreloc->size += p->count * htab->sizeof_reloc;
519     }
520
521   return TRUE;
522 }
523
524 /* Find any dynamic relocs that apply to read-only sections.  */
525
526 bfd_boolean
527 _bfd_x86_elf_readonly_dynrelocs (struct elf_link_hash_entry *h,
528                                  void *inf)
529 {
530   struct elf_x86_link_hash_entry *eh;
531   struct elf_dyn_relocs *p;
532
533   /* Skip local IFUNC symbols. */
534   if (h->forced_local && h->type == STT_GNU_IFUNC)
535     return TRUE;
536
537   eh = (struct elf_x86_link_hash_entry *) h;
538   for (p = eh->dyn_relocs; p != NULL; p = p->next)
539     {
540       asection *s = p->sec->output_section;
541
542       if (s != NULL && (s->flags & SEC_READONLY) != 0)
543         {
544           struct bfd_link_info *info = (struct bfd_link_info *) inf;
545
546           info->flags |= DF_TEXTREL;
547
548           if ((info->warn_shared_textrel && bfd_link_pic (info))
549               || info->error_textrel)
550             /* xgettext:c-format */
551             info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
552                                     p->sec->owner, h->root.root.string,
553                                     p->sec);
554
555           /* Not an error, just cut short the traversal.  */
556           return FALSE;
557         }
558     }
559   return TRUE;
560 }
561
562 /* Allocate space in .plt, .got and associated reloc sections for
563    local dynamic relocs.  */
564
565 static bfd_boolean
566 elf_x86_allocate_local_dynreloc (void **slot, void *inf)
567 {
568   struct elf_link_hash_entry *h
569     = (struct elf_link_hash_entry *) *slot;
570
571   if (h->type != STT_GNU_IFUNC
572       || !h->def_regular
573       || !h->ref_regular
574       || !h->forced_local
575       || h->root.type != bfd_link_hash_defined)
576     abort ();
577
578   return elf_x86_allocate_dynrelocs (h, inf);
579 }
580
581 /* Find and/or create a hash entry for local symbol.  */
582
583 struct elf_link_hash_entry *
584 _bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table *htab,
585                                  bfd *abfd, const Elf_Internal_Rela *rel,
586                                  bfd_boolean create)
587 {
588   struct elf_x86_link_hash_entry e, *ret;
589   asection *sec = abfd->sections;
590   hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
591                                        htab->r_sym (rel->r_info));
592   void **slot;
593
594   e.elf.indx = sec->id;
595   e.elf.dynstr_index = htab->r_sym (rel->r_info);
596   slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
597                                    create ? INSERT : NO_INSERT);
598
599   if (!slot)
600     return NULL;
601
602   if (*slot)
603     {
604       ret = (struct elf_x86_link_hash_entry *) *slot;
605       return &ret->elf;
606     }
607
608   ret = (struct elf_x86_link_hash_entry *)
609         objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
610                         sizeof (struct elf_x86_link_hash_entry));
611   if (ret)
612     {
613       memset (ret, 0, sizeof (*ret));
614       ret->elf.indx = sec->id;
615       ret->elf.dynstr_index = htab->r_sym (rel->r_info);
616       ret->elf.dynindx = -1;
617       ret->plt_got.offset = (bfd_vma) -1;
618       *slot = ret;
619     }
620   return &ret->elf;
621 }
622
623 /* Create an entry in a x86 ELF linker hash table.  NB: THIS MUST BE IN
624    SYNC WITH _bfd_elf_link_hash_newfunc.  */
625
626 struct bfd_hash_entry *
627 _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
628                                 struct bfd_hash_table *table,
629                                 const char *string)
630 {
631   /* Allocate the structure if it has not already been allocated by a
632      subclass.  */
633   if (entry == NULL)
634     {
635       entry = (struct bfd_hash_entry *)
636         bfd_hash_allocate (table,
637                            sizeof (struct elf_x86_link_hash_entry));
638       if (entry == NULL)
639         return entry;
640     }
641
642   /* Call the allocation method of the superclass.  */
643   entry = _bfd_link_hash_newfunc (entry, table, string);
644   if (entry != NULL)
645     {
646       struct elf_x86_link_hash_entry *eh
647        = (struct elf_x86_link_hash_entry *) entry;
648       struct elf_link_hash_table *htab
649         = (struct elf_link_hash_table *) table;
650
651       memset (&eh->elf.size, 0,
652               (sizeof (struct elf_x86_link_hash_entry)
653                - offsetof (struct elf_link_hash_entry, size)));
654       /* Set local fields.  */
655       eh->elf.indx = -1;
656       eh->elf.dynindx = -1;
657       eh->elf.got = htab->init_got_refcount;
658       eh->elf.plt = htab->init_plt_refcount;
659       /* Assume that we have been called by a non-ELF symbol reader.
660          This flag is then reset by the code which reads an ELF input
661          file.  This ensures that a symbol created by a non-ELF symbol
662          reader will have the flag set correctly.  */
663       eh->elf.non_elf = 1;
664       eh->plt_second.offset = (bfd_vma) -1;
665       eh->plt_got.offset = (bfd_vma) -1;
666       eh->tlsdesc_got = (bfd_vma) -1;
667       eh->zero_undefweak = 1;
668     }
669
670   return entry;
671 }
672
673 /* Compute a hash of a local hash entry.  We use elf_link_hash_entry
674   for local symbol so that we can handle local STT_GNU_IFUNC symbols
675   as global symbol.  We reuse indx and dynstr_index for local symbol
676   hash since they aren't used by global symbols in this backend.  */
677
678 hashval_t
679 _bfd_x86_elf_local_htab_hash (const void *ptr)
680 {
681   struct elf_link_hash_entry *h
682     = (struct elf_link_hash_entry *) ptr;
683   return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
684 }
685
686 /* Compare local hash entries.  */
687
688 int
689 _bfd_x86_elf_local_htab_eq (const void *ptr1, const void *ptr2)
690 {
691   struct elf_link_hash_entry *h1
692      = (struct elf_link_hash_entry *) ptr1;
693   struct elf_link_hash_entry *h2
694     = (struct elf_link_hash_entry *) ptr2;
695
696   return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
697 }
698
699 /* Destroy an x86 ELF linker hash table.  */
700
701 static void
702 elf_x86_link_hash_table_free (bfd *obfd)
703 {
704   struct elf_x86_link_hash_table *htab
705     = (struct elf_x86_link_hash_table *) obfd->link.hash;
706
707   if (htab->loc_hash_table)
708     htab_delete (htab->loc_hash_table);
709   if (htab->loc_hash_memory)
710     objalloc_free ((struct objalloc *) htab->loc_hash_memory);
711   _bfd_elf_link_hash_table_free (obfd);
712 }
713
714 static bfd_boolean
715 elf_i386_is_reloc_section (const char *secname)
716 {
717   return CONST_STRNEQ (secname, ".rel");
718 }
719
720 static bfd_boolean
721 elf_x86_64_is_reloc_section (const char *secname)
722 {
723   return CONST_STRNEQ (secname, ".rela");
724 }
725
726 /* Create an x86 ELF linker hash table.  */
727
728 struct bfd_link_hash_table *
729 _bfd_x86_elf_link_hash_table_create (bfd *abfd)
730 {
731   struct elf_x86_link_hash_table *ret;
732   const struct elf_backend_data *bed;
733   bfd_size_type amt = sizeof (struct elf_x86_link_hash_table);
734
735   ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt);
736   if (ret == NULL)
737     return NULL;
738
739   bed = get_elf_backend_data (abfd);
740   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
741                                       _bfd_x86_elf_link_hash_newfunc,
742                                       sizeof (struct elf_x86_link_hash_entry),
743                                       bed->target_id))
744     {
745       free (ret);
746       return NULL;
747     }
748
749   if (bed->target_id == X86_64_ELF_DATA)
750     {
751       ret->is_reloc_section = elf_x86_64_is_reloc_section;
752       ret->dt_reloc = DT_RELA;
753       ret->dt_reloc_sz = DT_RELASZ;
754       ret->dt_reloc_ent = DT_RELAENT;
755       ret->got_entry_size = 8;
756       ret->tls_get_addr = "__tls_get_addr";
757     }
758   if (ABI_64_P (abfd))
759     {
760       ret->sizeof_reloc = sizeof (Elf64_External_Rela);
761       ret->pointer_r_type = R_X86_64_64;
762       ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
763       ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
764     }
765   else
766     {
767       if (bed->target_id == X86_64_ELF_DATA)
768         {
769           ret->sizeof_reloc = sizeof (Elf32_External_Rela);
770           ret->pointer_r_type = R_X86_64_32;
771           ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER;
772           ret->dynamic_interpreter_size
773             = sizeof ELFX32_DYNAMIC_INTERPRETER;
774         }
775       else
776         {
777           ret->is_reloc_section = elf_i386_is_reloc_section;
778           ret->dt_reloc = DT_REL;
779           ret->dt_reloc_sz = DT_RELSZ;
780           ret->dt_reloc_ent = DT_RELENT;
781           ret->sizeof_reloc = sizeof (Elf32_External_Rel);
782           ret->got_entry_size = 4;
783           ret->pointer_r_type = R_386_32;
784           ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
785           ret->dynamic_interpreter_size
786             = sizeof ELF32_DYNAMIC_INTERPRETER;
787           ret->tls_get_addr = "___tls_get_addr";
788         }
789     }
790   ret->target_id = bed->target_id;
791   ret->target_os = get_elf_x86_backend_data (abfd)->target_os;
792
793   ret->loc_hash_table = htab_try_create (1024,
794                                          _bfd_x86_elf_local_htab_hash,
795                                          _bfd_x86_elf_local_htab_eq,
796                                          NULL);
797   ret->loc_hash_memory = objalloc_create ();
798   if (!ret->loc_hash_table || !ret->loc_hash_memory)
799     {
800       elf_x86_link_hash_table_free (abfd);
801       return NULL;
802     }
803   ret->elf.root.hash_table_free = elf_x86_link_hash_table_free;
804
805   return &ret->elf.root;
806 }
807
808 /* Sort relocs into address order.  */
809
810 int
811 _bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
812 {
813   const arelent *a = * (const arelent **) ap;
814   const arelent *b = * (const arelent **) bp;
815
816   if (a->address > b->address)
817     return 1;
818   else if (a->address < b->address)
819     return -1;
820   else
821     return 0;
822 }
823
824 bfd_boolean
825 _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
826 {
827   if (!bfd_link_relocatable (info))
828     {
829       /* Check for __tls_get_addr reference.  */
830       struct elf_x86_link_hash_table *htab;
831       const struct elf_backend_data *bed = get_elf_backend_data (abfd);
832       htab = elf_x86_hash_table (info, bed->target_id);
833       if (htab)
834         {
835           struct elf_link_hash_entry *h;
836
837           h = elf_link_hash_lookup (elf_hash_table (info),
838                                     htab->tls_get_addr,
839                                     FALSE, FALSE, FALSE);
840           if (h != NULL)
841             elf_x86_hash_entry (h)->tls_get_addr = 1;
842
843           /* "__ehdr_start" will be defined by linker as a hidden symbol
844              later if it is referenced and not defined.  */
845           h = elf_link_hash_lookup (elf_hash_table (info),
846                                     "__ehdr_start",
847                                     FALSE, FALSE, FALSE);
848           if (h != NULL
849               && (h->root.type == bfd_link_hash_new
850                   || h->root.type == bfd_link_hash_undefined
851                   || h->root.type == bfd_link_hash_undefweak
852                   || h->root.type == bfd_link_hash_common))
853             {
854               elf_x86_hash_entry (h)->local_ref = 2;
855               elf_x86_hash_entry (h)->linker_def = 1;
856             }
857         }
858     }
859
860   /* Invoke the regular ELF backend linker to do all the work.  */
861   return _bfd_elf_link_check_relocs (abfd, info);
862 }
863
864 /* Set the sizes of the dynamic sections.  */
865
866 bfd_boolean
867 _bfd_x86_elf_size_dynamic_sections (bfd *output_bfd,
868                                     struct bfd_link_info *info)
869 {
870   struct elf_x86_link_hash_table *htab;
871   bfd *dynobj;
872   asection *s;
873   bfd_boolean relocs;
874   bfd *ibfd;
875   const struct elf_backend_data *bed
876     = get_elf_backend_data (output_bfd);
877
878   htab = elf_x86_hash_table (info, bed->target_id);
879   if (htab == NULL)
880     return FALSE;
881   dynobj = htab->elf.dynobj;
882   if (dynobj == NULL)
883     abort ();
884
885   /* Set up .got offsets for local syms, and space for local dynamic
886      relocs.  */
887   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
888     {
889       bfd_signed_vma *local_got;
890       bfd_signed_vma *end_local_got;
891       char *local_tls_type;
892       bfd_vma *local_tlsdesc_gotent;
893       bfd_size_type locsymcount;
894       Elf_Internal_Shdr *symtab_hdr;
895       asection *srel;
896
897       if (! is_x86_elf (ibfd, htab))
898         continue;
899
900       for (s = ibfd->sections; s != NULL; s = s->next)
901         {
902           struct elf_dyn_relocs *p;
903
904           for (p = ((struct elf_dyn_relocs *)
905                      elf_section_data (s)->local_dynrel);
906                p != NULL;
907                p = p->next)
908             {
909               if (!bfd_is_abs_section (p->sec)
910                   && bfd_is_abs_section (p->sec->output_section))
911                 {
912                   /* Input section has been discarded, either because
913                      it is a copy of a linkonce section or due to
914                      linker script /DISCARD/, so we'll be discarding
915                      the relocs too.  */
916                 }
917               else if (htab->target_os == is_vxworks
918                        && strcmp (p->sec->output_section->name,
919                                   ".tls_vars") == 0)
920                 {
921                   /* Relocations in vxworks .tls_vars sections are
922                      handled specially by the loader.  */
923                 }
924               else if (p->count != 0)
925                 {
926                   srel = elf_section_data (p->sec)->sreloc;
927                   srel->size += p->count * htab->sizeof_reloc;
928                   if ((p->sec->output_section->flags & SEC_READONLY) != 0
929                       && (info->flags & DF_TEXTREL) == 0)
930                     {
931                       info->flags |= DF_TEXTREL;
932                       if ((info->warn_shared_textrel && bfd_link_pic (info))
933                           || info->error_textrel)
934                         /* xgettext:c-format */
935                         info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
936                                                 p->sec->owner, p->sec);
937                     }
938                 }
939             }
940         }
941
942       local_got = elf_local_got_refcounts (ibfd);
943       if (!local_got)
944         continue;
945
946       symtab_hdr = &elf_symtab_hdr (ibfd);
947       locsymcount = symtab_hdr->sh_info;
948       end_local_got = local_got + locsymcount;
949       local_tls_type = elf_x86_local_got_tls_type (ibfd);
950       local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
951       s = htab->elf.sgot;
952       srel = htab->elf.srelgot;
953       for (; local_got < end_local_got;
954            ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
955         {
956           *local_tlsdesc_gotent = (bfd_vma) -1;
957           if (*local_got > 0)
958             {
959               if (GOT_TLS_GDESC_P (*local_tls_type))
960                 {
961                   *local_tlsdesc_gotent = htab->elf.sgotplt->size
962                     - elf_x86_compute_jump_table_size (htab);
963                   htab->elf.sgotplt->size += 2 * htab->got_entry_size;
964                   *local_got = (bfd_vma) -2;
965                 }
966               if (! GOT_TLS_GDESC_P (*local_tls_type)
967                   || GOT_TLS_GD_P (*local_tls_type))
968                 {
969                   *local_got = s->size;
970                   s->size += htab->got_entry_size;
971                   if (GOT_TLS_GD_P (*local_tls_type)
972                       || *local_tls_type == GOT_TLS_IE_BOTH)
973                     s->size += htab->got_entry_size;
974                 }
975               if (bfd_link_pic (info)
976                   || GOT_TLS_GD_ANY_P (*local_tls_type)
977                   || (*local_tls_type & GOT_TLS_IE))
978                 {
979                   if (*local_tls_type == GOT_TLS_IE_BOTH)
980                     srel->size += 2 * htab->sizeof_reloc;
981                   else if (GOT_TLS_GD_P (*local_tls_type)
982                            || ! GOT_TLS_GDESC_P (*local_tls_type))
983                     srel->size += htab->sizeof_reloc;
984                   if (GOT_TLS_GDESC_P (*local_tls_type))
985                     {
986                       htab->elf.srelplt->size += htab->sizeof_reloc;
987                       if (bed->target_id == X86_64_ELF_DATA)
988                         htab->tlsdesc_plt = (bfd_vma) -1;
989                     }
990                 }
991             }
992           else
993             *local_got = (bfd_vma) -1;
994         }
995     }
996
997   if (htab->tls_ld_or_ldm_got.refcount > 0)
998     {
999       /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
1000          or R_X86_64_TLSLD relocs.  */
1001       htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
1002       htab->elf.sgot->size += 2 * htab->got_entry_size;
1003       htab->elf.srelgot->size += htab->sizeof_reloc;
1004     }
1005   else
1006     htab->tls_ld_or_ldm_got.offset = -1;
1007
1008   /* Allocate global sym .plt and .got entries, and space for global
1009      sym dynamic relocs.  */
1010   elf_link_hash_traverse (&htab->elf, elf_x86_allocate_dynrelocs,
1011                           info);
1012
1013   /* Allocate .plt and .got entries, and space for local symbols.  */
1014   htab_traverse (htab->loc_hash_table, elf_x86_allocate_local_dynreloc,
1015                  info);
1016
1017   /* For every jump slot reserved in the sgotplt, reloc_count is
1018      incremented.  However, when we reserve space for TLS descriptors,
1019      it's not incremented, so in order to compute the space reserved
1020      for them, it suffices to multiply the reloc count by the jump
1021      slot size.
1022
1023      PR ld/13302: We start next_irelative_index at the end of .rela.plt
1024      so that R_{386,X86_64}_IRELATIVE entries come last.  */
1025   if (htab->elf.srelplt)
1026     {
1027       htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
1028       htab->sgotplt_jump_table_size
1029         = elf_x86_compute_jump_table_size (htab);
1030       htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
1031     }
1032   else if (htab->elf.irelplt)
1033     htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
1034
1035   if (htab->tlsdesc_plt)
1036     {
1037       /* NB: tlsdesc_plt is set only for x86-64.  If we're not using
1038          lazy TLS relocations, don't generate the PLT and GOT entries
1039          they require.  */
1040       if ((info->flags & DF_BIND_NOW))
1041         htab->tlsdesc_plt = 0;
1042       else
1043         {
1044           htab->tlsdesc_got = htab->elf.sgot->size;
1045           htab->elf.sgot->size += htab->got_entry_size;
1046           /* Reserve room for the initial entry.
1047              FIXME: we could probably do away with it in this case.  */
1048           if (htab->elf.splt->size == 0)
1049             htab->elf.splt->size = htab->plt.plt_entry_size;
1050           htab->tlsdesc_plt = htab->elf.splt->size;
1051           htab->elf.splt->size += htab->plt.plt_entry_size;
1052         }
1053     }
1054
1055   if (htab->elf.sgotplt)
1056     {
1057       /* Don't allocate .got.plt section if there are no GOT nor PLT
1058          entries and there is no reference to _GLOBAL_OFFSET_TABLE_.  */
1059       if ((htab->elf.hgot == NULL
1060            || !htab->elf.hgot->ref_regular_nonweak)
1061           && (htab->elf.sgotplt->size == bed->got_header_size)
1062           && (htab->elf.splt == NULL
1063               || htab->elf.splt->size == 0)
1064           && (htab->elf.sgot == NULL
1065               || htab->elf.sgot->size == 0)
1066           && (htab->elf.iplt == NULL
1067               || htab->elf.iplt->size == 0)
1068           && (htab->elf.igotplt == NULL
1069               || htab->elf.igotplt->size == 0))
1070         htab->elf.sgotplt->size = 0;
1071     }
1072
1073   if (_bfd_elf_eh_frame_present (info))
1074     {
1075       if (htab->plt_eh_frame != NULL
1076           && htab->elf.splt != NULL
1077           && htab->elf.splt->size != 0
1078           && !bfd_is_abs_section (htab->elf.splt->output_section))
1079         htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
1080
1081       if (htab->plt_got_eh_frame != NULL
1082           && htab->plt_got != NULL
1083           && htab->plt_got->size != 0
1084           && !bfd_is_abs_section (htab->plt_got->output_section))
1085         htab->plt_got_eh_frame->size
1086           = htab->non_lazy_plt->eh_frame_plt_size;
1087
1088       /* Unwind info for the second PLT and .plt.got sections are
1089          identical.  */
1090       if (htab->plt_second_eh_frame != NULL
1091           && htab->plt_second != NULL
1092           && htab->plt_second->size != 0
1093           && !bfd_is_abs_section (htab->plt_second->output_section))
1094         htab->plt_second_eh_frame->size
1095           = htab->non_lazy_plt->eh_frame_plt_size;
1096     }
1097
1098   /* We now have determined the sizes of the various dynamic sections.
1099      Allocate memory for them.  */
1100   relocs = FALSE;
1101   for (s = dynobj->sections; s != NULL; s = s->next)
1102     {
1103       bfd_boolean strip_section = TRUE;
1104
1105       if ((s->flags & SEC_LINKER_CREATED) == 0)
1106         continue;
1107
1108       if (s == htab->elf.splt
1109           || s == htab->elf.sgot)
1110         {
1111           /* Strip this section if we don't need it; see the
1112              comment below.  */
1113           /* We'd like to strip these sections if they aren't needed, but if
1114              we've exported dynamic symbols from them we must leave them.
1115              It's too late to tell BFD to get rid of the symbols.  */
1116
1117           if (htab->elf.hplt != NULL)
1118             strip_section = FALSE;
1119         }
1120       else if (s == htab->elf.sgotplt
1121                || s == htab->elf.iplt
1122                || s == htab->elf.igotplt
1123                || s == htab->plt_second
1124                || s == htab->plt_got
1125                || s == htab->plt_eh_frame
1126                || s == htab->plt_got_eh_frame
1127                || s == htab->plt_second_eh_frame
1128                || s == htab->elf.sdynbss
1129                || s == htab->elf.sdynrelro)
1130         {
1131           /* Strip these too.  */
1132         }
1133       else if (htab->is_reloc_section (bfd_get_section_name (dynobj, s)))
1134         {
1135           if (s->size != 0
1136               && s != htab->elf.srelplt
1137               && s != htab->srelplt2)
1138             relocs = TRUE;
1139
1140           /* We use the reloc_count field as a counter if we need
1141              to copy relocs into the output file.  */
1142           if (s != htab->elf.srelplt)
1143             s->reloc_count = 0;
1144         }
1145       else
1146         {
1147           /* It's not one of our sections, so don't allocate space.  */
1148           continue;
1149         }
1150
1151       if (s->size == 0)
1152         {
1153           /* If we don't need this section, strip it from the
1154              output file.  This is mostly to handle .rel.bss and
1155              .rel.plt.  We must create both sections in
1156              create_dynamic_sections, because they must be created
1157              before the linker maps input sections to output
1158              sections.  The linker does that before
1159              adjust_dynamic_symbol is called, and it is that
1160              function which decides whether anything needs to go
1161              into these sections.  */
1162           if (strip_section)
1163             s->flags |= SEC_EXCLUDE;
1164           continue;
1165         }
1166
1167       if ((s->flags & SEC_HAS_CONTENTS) == 0)
1168         continue;
1169
1170       /* Allocate memory for the section contents.  We use bfd_zalloc
1171          here in case unused entries are not reclaimed before the
1172          section's contents are written out.  This should not happen,
1173          but this way if it does, we get a R_386_NONE or R_X86_64_NONE
1174          reloc instead of garbage.  */
1175       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
1176       if (s->contents == NULL)
1177         return FALSE;
1178     }
1179
1180   if (htab->plt_eh_frame != NULL
1181       && htab->plt_eh_frame->contents != NULL)
1182     {
1183       memcpy (htab->plt_eh_frame->contents,
1184               htab->plt.eh_frame_plt,
1185               htab->plt_eh_frame->size);
1186       bfd_put_32 (dynobj, htab->elf.splt->size,
1187                   htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
1188     }
1189
1190   if (htab->plt_got_eh_frame != NULL
1191       && htab->plt_got_eh_frame->contents != NULL)
1192     {
1193       memcpy (htab->plt_got_eh_frame->contents,
1194               htab->non_lazy_plt->eh_frame_plt,
1195               htab->plt_got_eh_frame->size);
1196       bfd_put_32 (dynobj, htab->plt_got->size,
1197                   (htab->plt_got_eh_frame->contents
1198                    + PLT_FDE_LEN_OFFSET));
1199     }
1200
1201   if (htab->plt_second_eh_frame != NULL
1202       && htab->plt_second_eh_frame->contents != NULL)
1203     {
1204       memcpy (htab->plt_second_eh_frame->contents,
1205               htab->non_lazy_plt->eh_frame_plt,
1206               htab->plt_second_eh_frame->size);
1207       bfd_put_32 (dynobj, htab->plt_second->size,
1208                   (htab->plt_second_eh_frame->contents
1209                    + PLT_FDE_LEN_OFFSET));
1210     }
1211
1212   if (htab->elf.dynamic_sections_created)
1213     {
1214       /* Add some entries to the .dynamic section.  We fill in the
1215          values later, in elf_{i386,x86_64}_finish_dynamic_sections,
1216          but we must add the entries now so that we get the correct
1217          size for the .dynamic section.  The DT_DEBUG entry is filled
1218          in by the dynamic linker and used by the debugger.  */
1219 #define add_dynamic_entry(TAG, VAL) \
1220   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1221
1222       if (bfd_link_executable (info))
1223         {
1224           if (!add_dynamic_entry (DT_DEBUG, 0))
1225             return FALSE;
1226         }
1227
1228       if (htab->elf.splt->size != 0)
1229         {
1230           /* DT_PLTGOT is used by prelink even if there is no PLT
1231              relocation.  */
1232           if (!add_dynamic_entry (DT_PLTGOT, 0))
1233             return FALSE;
1234         }
1235
1236       if (htab->elf.srelplt->size != 0)
1237         {
1238           if (!add_dynamic_entry (DT_PLTRELSZ, 0)
1239               || !add_dynamic_entry (DT_PLTREL, htab->dt_reloc)
1240               || !add_dynamic_entry (DT_JMPREL, 0))
1241             return FALSE;
1242         }
1243
1244       if (htab->tlsdesc_plt
1245           && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
1246               || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
1247         return FALSE;
1248
1249       if (relocs)
1250         {
1251           if (!add_dynamic_entry (htab->dt_reloc, 0)
1252               || !add_dynamic_entry (htab->dt_reloc_sz, 0)
1253               || !add_dynamic_entry (htab->dt_reloc_ent,
1254                                      htab->sizeof_reloc))
1255             return FALSE;
1256
1257           /* If any dynamic relocs apply to a read-only section,
1258              then we need a DT_TEXTREL entry.  */
1259           if ((info->flags & DF_TEXTREL) == 0)
1260             elf_link_hash_traverse (&htab->elf,
1261                                     _bfd_x86_elf_readonly_dynrelocs,
1262                                     info);
1263
1264           if ((info->flags & DF_TEXTREL) != 0)
1265             {
1266               if (htab->readonly_dynrelocs_against_ifunc)
1267                 {
1268                   info->callbacks->einfo
1269                     (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
1270                   bfd_set_error (bfd_error_bad_value);
1271                   return FALSE;
1272                 }
1273
1274               if (!add_dynamic_entry (DT_TEXTREL, 0))
1275                 return FALSE;
1276             }
1277         }
1278       if (htab->target_os == is_vxworks
1279           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
1280         return FALSE;
1281     }
1282 #undef add_dynamic_entry
1283
1284   return TRUE;
1285 }
1286
1287 /* Finish up the x86 dynamic sections.  */
1288
1289 struct elf_x86_link_hash_table *
1290 _bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd,
1291                                       struct bfd_link_info *info)
1292 {
1293   struct elf_x86_link_hash_table *htab;
1294   const struct elf_backend_data *bed;
1295   bfd *dynobj;
1296   asection *sdyn;
1297   bfd_byte *dyncon, *dynconend;
1298   bfd_size_type sizeof_dyn;
1299
1300   bed = get_elf_backend_data (output_bfd);
1301   htab = elf_x86_hash_table (info, bed->target_id);
1302   if (htab == NULL)
1303     return htab;
1304
1305   dynobj = htab->elf.dynobj;
1306   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
1307
1308   /* GOT is always created in setup_gnu_properties.  But it may not be
1309      needed.  .got.plt section may be needed for static IFUNC.  */
1310   if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
1311     {
1312       bfd_vma dynamic_addr;
1313
1314       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
1315         {
1316           _bfd_error_handler
1317             (_("discarded output section: `%A'"), htab->elf.sgotplt);
1318           return NULL;
1319         }
1320
1321       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
1322         = htab->got_entry_size;
1323
1324       dynamic_addr = (sdyn == NULL
1325                       ? (bfd_vma) 0
1326                       : sdyn->output_section->vma + sdyn->output_offset);
1327
1328       /* Set the first entry in the global offset table to the address
1329          of the dynamic section.  Write GOT[1] and GOT[2], needed for
1330          the dynamic linker.  */
1331       if (htab->got_entry_size == 8)
1332         {
1333           bfd_put_64 (output_bfd, dynamic_addr,
1334                       htab->elf.sgotplt->contents);
1335           bfd_put_64 (output_bfd, (bfd_vma) 0,
1336                       htab->elf.sgotplt->contents + 8);
1337           bfd_put_64 (output_bfd, (bfd_vma) 0,
1338                       htab->elf.sgotplt->contents + 8*2);
1339         }
1340       else
1341         {
1342           bfd_put_32 (output_bfd, dynamic_addr,
1343                       htab->elf.sgotplt->contents);
1344           bfd_put_32 (output_bfd, 0,
1345                       htab->elf.sgotplt->contents + 4);
1346           bfd_put_32 (output_bfd, 0,
1347                       htab->elf.sgotplt->contents + 4*2);
1348         }
1349     }
1350
1351   if (!htab->elf.dynamic_sections_created)
1352     return htab;
1353
1354   if (sdyn == NULL || htab->elf.sgot == NULL)
1355     abort ();
1356
1357   sizeof_dyn = bed->s->sizeof_dyn;
1358   dyncon = sdyn->contents;
1359   dynconend = sdyn->contents + sdyn->size;
1360   for (; dyncon < dynconend; dyncon += sizeof_dyn)
1361     {
1362       Elf_Internal_Dyn dyn;
1363       asection *s;
1364
1365       (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
1366
1367       switch (dyn.d_tag)
1368         {
1369         default:
1370           if (htab->target_os == is_vxworks
1371               && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
1372             break;
1373           continue;
1374
1375         case DT_PLTGOT:
1376           s = htab->elf.sgotplt;
1377           dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
1378           break;
1379
1380         case DT_JMPREL:
1381           dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
1382           break;
1383
1384         case DT_PLTRELSZ:
1385           s = htab->elf.srelplt->output_section;
1386           dyn.d_un.d_val = s->size;
1387           break;
1388
1389         case DT_TLSDESC_PLT:
1390           s = htab->elf.splt;
1391           dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
1392             + htab->tlsdesc_plt;
1393           break;
1394
1395         case DT_TLSDESC_GOT:
1396           s = htab->elf.sgot;
1397           dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
1398             + htab->tlsdesc_got;
1399           break;
1400         }
1401
1402       (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
1403     }
1404
1405   if (htab->plt_got != NULL && htab->plt_got->size > 0)
1406     elf_section_data (htab->plt_got->output_section)
1407       ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
1408
1409   if (htab->plt_second != NULL && htab->plt_second->size > 0)
1410     elf_section_data (htab->plt_second->output_section)
1411       ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
1412
1413   /* Adjust .eh_frame for .plt section.  */
1414   if (htab->plt_eh_frame != NULL
1415       && htab->plt_eh_frame->contents != NULL)
1416     {
1417       if (htab->elf.splt != NULL
1418           && htab->elf.splt->size != 0
1419           && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
1420           && htab->elf.splt->output_section != NULL
1421           && htab->plt_eh_frame->output_section != NULL)
1422         {
1423           bfd_vma plt_start = htab->elf.splt->output_section->vma;
1424           bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
1425                                    + htab->plt_eh_frame->output_offset
1426                                    + PLT_FDE_START_OFFSET;
1427           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
1428                              htab->plt_eh_frame->contents
1429                              + PLT_FDE_START_OFFSET);
1430         }
1431
1432       if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
1433         {
1434           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
1435                                                  htab->plt_eh_frame,
1436                                                  htab->plt_eh_frame->contents))
1437             return NULL;
1438         }
1439     }
1440
1441   /* Adjust .eh_frame for .plt.got section.  */
1442   if (htab->plt_got_eh_frame != NULL
1443       && htab->plt_got_eh_frame->contents != NULL)
1444     {
1445       if (htab->plt_got != NULL
1446           && htab->plt_got->size != 0
1447           && (htab->plt_got->flags & SEC_EXCLUDE) == 0
1448           && htab->plt_got->output_section != NULL
1449           && htab->plt_got_eh_frame->output_section != NULL)
1450         {
1451           bfd_vma plt_start = htab->plt_got->output_section->vma;
1452           bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
1453                                    + htab->plt_got_eh_frame->output_offset
1454                                    + PLT_FDE_START_OFFSET;
1455           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
1456                              htab->plt_got_eh_frame->contents
1457                              + PLT_FDE_START_OFFSET);
1458         }
1459       if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
1460         {
1461           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
1462                                                  htab->plt_got_eh_frame,
1463                                                  htab->plt_got_eh_frame->contents))
1464             return NULL;
1465         }
1466     }
1467
1468   /* Adjust .eh_frame for the second PLT section.  */
1469   if (htab->plt_second_eh_frame != NULL
1470       && htab->plt_second_eh_frame->contents != NULL)
1471     {
1472       if (htab->plt_second != NULL
1473           && htab->plt_second->size != 0
1474           && (htab->plt_second->flags & SEC_EXCLUDE) == 0
1475           && htab->plt_second->output_section != NULL
1476           && htab->plt_second_eh_frame->output_section != NULL)
1477         {
1478           bfd_vma plt_start = htab->plt_second->output_section->vma;
1479           bfd_vma eh_frame_start
1480             = (htab->plt_second_eh_frame->output_section->vma
1481                + htab->plt_second_eh_frame->output_offset
1482                + PLT_FDE_START_OFFSET);
1483           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
1484                              htab->plt_second_eh_frame->contents
1485                              + PLT_FDE_START_OFFSET);
1486         }
1487       if (htab->plt_second_eh_frame->sec_info_type
1488           == SEC_INFO_TYPE_EH_FRAME)
1489         {
1490           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
1491                                                  htab->plt_second_eh_frame,
1492                                                  htab->plt_second_eh_frame->contents))
1493             return NULL;
1494         }
1495     }
1496
1497   if (htab->elf.sgot && htab->elf.sgot->size > 0)
1498     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
1499       = htab->got_entry_size;
1500
1501   return htab;
1502 }
1503
1504
1505 bfd_boolean
1506 _bfd_x86_elf_always_size_sections (bfd *output_bfd,
1507                                    struct bfd_link_info *info)
1508 {
1509   asection *tls_sec = elf_hash_table (info)->tls_sec;
1510
1511   if (tls_sec)
1512     {
1513       struct elf_link_hash_entry *tlsbase;
1514
1515       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
1516                                       "_TLS_MODULE_BASE_",
1517                                       FALSE, FALSE, FALSE);
1518
1519       if (tlsbase && tlsbase->type == STT_TLS)
1520         {
1521           struct elf_x86_link_hash_table *htab;
1522           struct bfd_link_hash_entry *bh = NULL;
1523           const struct elf_backend_data *bed
1524             = get_elf_backend_data (output_bfd);
1525
1526           htab = elf_x86_hash_table (info, bed->target_id);
1527           if (htab == NULL)
1528             return FALSE;
1529
1530           if (!(_bfd_generic_link_add_one_symbol
1531                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
1532                  tls_sec, 0, NULL, FALSE,
1533                  bed->collect, &bh)))
1534             return FALSE;
1535
1536           htab->tls_module_base = bh;
1537
1538           tlsbase = (struct elf_link_hash_entry *)bh;
1539           tlsbase->def_regular = 1;
1540           tlsbase->other = STV_HIDDEN;
1541           tlsbase->root.linker_def = 1;
1542           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
1543         }
1544     }
1545
1546   return TRUE;
1547 }
1548
1549 void
1550 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
1551                                      const Elf_Internal_Sym *isym,
1552                                      bfd_boolean definition,
1553                                      bfd_boolean dynamic ATTRIBUTE_UNUSED)
1554 {
1555   if (definition)
1556     {
1557       struct elf_x86_link_hash_entry *eh
1558         = (struct elf_x86_link_hash_entry *) h;
1559       eh->def_protected = (ELF_ST_VISIBILITY (isym->st_other)
1560                            == STV_PROTECTED);
1561     }
1562 }
1563
1564 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
1565
1566 void
1567 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
1568                                    struct elf_link_hash_entry *dir,
1569                                    struct elf_link_hash_entry *ind)
1570 {
1571   struct elf_x86_link_hash_entry *edir, *eind;
1572
1573   edir = (struct elf_x86_link_hash_entry *) dir;
1574   eind = (struct elf_x86_link_hash_entry *) ind;
1575
1576   if (eind->dyn_relocs != NULL)
1577     {
1578       if (edir->dyn_relocs != NULL)
1579         {
1580           struct elf_dyn_relocs **pp;
1581           struct elf_dyn_relocs *p;
1582
1583           /* Add reloc counts against the indirect sym to the direct sym
1584              list.  Merge any entries against the same section.  */
1585           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1586             {
1587               struct elf_dyn_relocs *q;
1588
1589               for (q = edir->dyn_relocs; q != NULL; q = q->next)
1590                 if (q->sec == p->sec)
1591                   {
1592                     q->pc_count += p->pc_count;
1593                     q->count += p->count;
1594                     *pp = p->next;
1595                     break;
1596                   }
1597               if (q == NULL)
1598                 pp = &p->next;
1599             }
1600           *pp = edir->dyn_relocs;
1601         }
1602
1603       edir->dyn_relocs = eind->dyn_relocs;
1604       eind->dyn_relocs = NULL;
1605     }
1606
1607   if (ind->root.type == bfd_link_hash_indirect
1608       && dir->got.refcount <= 0)
1609     {
1610       edir->tls_type = eind->tls_type;
1611       eind->tls_type = GOT_UNKNOWN;
1612     }
1613
1614   /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
1615      generate a R_386_COPY reloc.  */
1616   edir->gotoff_ref |= eind->gotoff_ref;
1617
1618   edir->zero_undefweak |= eind->zero_undefweak;
1619
1620   if (ELIMINATE_COPY_RELOCS
1621       && ind->root.type != bfd_link_hash_indirect
1622       && dir->dynamic_adjusted)
1623     {
1624       /* If called to transfer flags for a weakdef during processing
1625          of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1626          We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
1627       if (dir->versioned != versioned_hidden)
1628         dir->ref_dynamic |= ind->ref_dynamic;
1629       dir->ref_regular |= ind->ref_regular;
1630       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1631       dir->needs_plt |= ind->needs_plt;
1632       dir->pointer_equality_needed |= ind->pointer_equality_needed;
1633     }
1634   else
1635     _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1636 }
1637
1638 /* Remove undefined weak symbol from the dynamic symbol table if it
1639    is resolved to 0.   */
1640
1641 bfd_boolean
1642 _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
1643                            struct elf_link_hash_entry *h)
1644 {
1645   if (h->dynindx != -1
1646       && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, elf_x86_hash_entry (h)))
1647     {
1648       h->dynindx = -1;
1649       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1650                               h->dynstr_index);
1651     }
1652   return TRUE;
1653 }
1654
1655 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
1656
1657 bfd_boolean
1658 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
1659 {
1660   if (h->plt.offset != (bfd_vma) -1
1661       && !h->def_regular
1662       && !h->pointer_equality_needed)
1663     return FALSE;
1664
1665   return _bfd_elf_hash_symbol (h);
1666 }
1667
1668 /* Adjust a symbol defined by a dynamic object and referenced by a
1669    regular object.  The current definition is in some section of the
1670    dynamic object, but we're not including those sections.  We have to
1671    change the definition to something the rest of the link can
1672    understand.  */
1673
1674 bfd_boolean
1675 _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1676                                     struct elf_link_hash_entry *h)
1677 {
1678   struct elf_x86_link_hash_table *htab;
1679   asection *s, *srel;
1680   struct elf_x86_link_hash_entry *eh;
1681   struct elf_dyn_relocs *p;
1682   const struct elf_backend_data *bed
1683     = get_elf_backend_data (info->output_bfd);
1684
1685   /* STT_GNU_IFUNC symbol must go through PLT. */
1686   if (h->type == STT_GNU_IFUNC)
1687     {
1688       /* All local STT_GNU_IFUNC references must be treate as local
1689          calls via local PLT.  */
1690       if (h->ref_regular
1691           && SYMBOL_CALLS_LOCAL (info, h))
1692         {
1693           bfd_size_type pc_count = 0, count = 0;
1694           struct elf_dyn_relocs **pp;
1695
1696           eh = (struct elf_x86_link_hash_entry *) h;
1697           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1698             {
1699               pc_count += p->pc_count;
1700               p->count -= p->pc_count;
1701               p->pc_count = 0;
1702               count += p->count;
1703               if (p->count == 0)
1704                 *pp = p->next;
1705               else
1706                 pp = &p->next;
1707             }
1708
1709           if (pc_count || count)
1710             {
1711               h->non_got_ref = 1;
1712               if (pc_count)
1713                 {
1714                   /* Increment PLT reference count only for PC-relative
1715                      references.  */
1716                   h->needs_plt = 1;
1717                   if (h->plt.refcount <= 0)
1718                     h->plt.refcount = 1;
1719                   else
1720                     h->plt.refcount += 1;
1721                 }
1722             }
1723         }
1724
1725       if (h->plt.refcount <= 0)
1726         {
1727           h->plt.offset = (bfd_vma) -1;
1728           h->needs_plt = 0;
1729         }
1730       return TRUE;
1731     }
1732
1733   /* If this is a function, put it in the procedure linkage table.  We
1734      will fill in the contents of the procedure linkage table later,
1735      when we know the address of the .got section.  */
1736   if (h->type == STT_FUNC
1737       || h->needs_plt)
1738     {
1739       if (h->plt.refcount <= 0
1740           || SYMBOL_CALLS_LOCAL (info, h)
1741           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1742               && h->root.type == bfd_link_hash_undefweak))
1743         {
1744           /* This case can occur if we saw a PLT32 reloc in an input
1745              file, but the symbol was never referred to by a dynamic
1746              object, or if all references were garbage collected.  In
1747              such a case, we don't actually need to build a procedure
1748              linkage table, and we can just do a PC32 reloc instead.  */
1749           h->plt.offset = (bfd_vma) -1;
1750           h->needs_plt = 0;
1751         }
1752
1753       return TRUE;
1754     }
1755   else
1756     /* It's possible that we incorrectly decided a .plt reloc was needed
1757      * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
1758        check_relocs.  We can't decide accurately between function and
1759        non-function syms in check-relocs;  Objects loaded later in
1760        the link may change h->type.  So fix it now.  */
1761     h->plt.offset = (bfd_vma) -1;
1762
1763   eh = (struct elf_x86_link_hash_entry *) h;
1764
1765   /* If this is a weak symbol, and there is a real definition, the
1766      processor independent code will have arranged for us to see the
1767      real definition first, and we can just use the same value.  */
1768   if (h->u.weakdef != NULL)
1769     {
1770       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1771                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
1772       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1773       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1774       if (ELIMINATE_COPY_RELOCS
1775           || info->nocopyreloc
1776           || SYMBOL_NO_COPYRELOC (info, eh))
1777         {
1778           /* NB: needs_copy is always 0 for i386.  */
1779           h->non_got_ref = h->u.weakdef->non_got_ref;
1780           eh->needs_copy = h->u.weakdef->needs_copy;
1781         }
1782       return TRUE;
1783     }
1784
1785   /* This is a reference to a symbol defined by a dynamic object which
1786      is not a function.  */
1787
1788   /* If we are creating a shared library, we must presume that the
1789      only references to the symbol are via the global offset table.
1790      For such cases we need not do anything here; the relocations will
1791      be handled correctly by relocate_section.  */
1792   if (!bfd_link_executable (info))
1793     return TRUE;
1794
1795   /* If there are no references to this symbol that do not use the
1796      GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
1797      reloc.  NB: gotoff_ref is always 0 for x86-64.  */
1798   if (!h->non_got_ref && !eh->gotoff_ref)
1799     return TRUE;
1800
1801   /* If -z nocopyreloc was given, we won't generate them either.  */
1802   if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
1803     {
1804       h->non_got_ref = 0;
1805       return TRUE;
1806     }
1807
1808   htab = elf_x86_hash_table (info, bed->target_id);
1809   if (htab == NULL)
1810     return FALSE;
1811
1812   /* If there aren't any dynamic relocs in read-only sections nor
1813      R_386_GOTOFF relocation, then we can keep the dynamic relocs and
1814      avoid the copy reloc.  This doesn't work on VxWorks, where we can
1815      not have dynamic relocations (other than copy and jump slot
1816      relocations) in an executable.  */
1817   if (ELIMINATE_COPY_RELOCS
1818       && (bed->target_id == X86_64_ELF_DATA
1819           || (!eh->gotoff_ref
1820               && htab->target_os != is_vxworks)))
1821     {
1822       for (p = eh->dyn_relocs; p != NULL; p = p->next)
1823         {
1824           s = p->sec->output_section;
1825           if (s != NULL && (s->flags & SEC_READONLY) != 0)
1826             break;
1827         }
1828
1829       /* If we didn't find any dynamic relocs in read-only sections,
1830          then we'll be keeping the dynamic relocs and avoiding the copy
1831          reloc.  */
1832       if (p == NULL)
1833         {
1834           h->non_got_ref = 0;
1835           return TRUE;
1836         }
1837     }
1838
1839   /* We must allocate the symbol in our .dynbss section, which will
1840      become part of the .bss section of the executable.  There will be
1841      an entry for this symbol in the .dynsym section.  The dynamic
1842      object will contain position independent code, so all references
1843      from the dynamic object to this symbol will go through the global
1844      offset table.  The dynamic linker will use the .dynsym entry to
1845      determine the address it must put in the global offset table, so
1846      both the dynamic object and the regular object will refer to the
1847      same memory location for the variable.  */
1848
1849   /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
1850      dynamic linker to copy the initial value out of the dynamic object
1851      and into the runtime process image.  */
1852   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
1853     {
1854       s = htab->elf.sdynrelro;
1855       srel = htab->elf.sreldynrelro;
1856     }
1857   else
1858     {
1859       s = htab->elf.sdynbss;
1860       srel = htab->elf.srelbss;
1861     }
1862   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
1863     {
1864       srel->size += htab->sizeof_reloc;
1865       h->needs_copy = 1;
1866     }
1867
1868   return _bfd_elf_adjust_dynamic_copy (info, h, s);
1869 }
1870
1871 void
1872 _bfd_x86_elf_hide_symbol (struct bfd_link_info *info,
1873                           struct elf_link_hash_entry *h,
1874                           bfd_boolean force_local)
1875 {
1876   if (h->root.type == bfd_link_hash_undefweak
1877       && info->nointerp
1878       && bfd_link_pie (info))
1879     {
1880       /* When there is no dynamic interpreter in PIE, make the undefined
1881          weak symbol dynamic so that PC relative branch to the undefined
1882          weak symbol will land to address 0.  */
1883       struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
1884       if (h->plt.refcount > 0
1885           || eh->plt_got.refcount > 0)
1886         return;
1887     }
1888
1889   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
1890 }
1891
1892 /* Return TRUE if a symbol is referenced locally.  It is similar to
1893    SYMBOL_REFERENCES_LOCAL, but it also checks version script.  It
1894    works in check_relocs.  */
1895
1896 bfd_boolean
1897 _bfd_x86_elf_link_symbol_references_local (struct bfd_link_info *info,
1898                                            struct elf_link_hash_entry *h)
1899 {
1900   struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
1901   struct elf_x86_link_hash_table *htab
1902     = (struct elf_x86_link_hash_table *) info->hash;
1903
1904   if (eh->local_ref > 1)
1905     return TRUE;
1906
1907   if (eh->local_ref == 1)
1908     return FALSE;
1909
1910   /* Unversioned symbols defined in regular objects can be forced local
1911      by linker version script.  A weak undefined symbol is forced local
1912      if
1913      1. It has non-default visibility.  Or
1914      2. When building executable, there is no dynamic linker.  Or
1915      3. or "-z nodynamic-undefined-weak" is used.
1916    */
1917   if (SYMBOL_REFERENCES_LOCAL (info, h)
1918       || (h->root.type == bfd_link_hash_undefweak
1919           && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1920               || (bfd_link_executable (info)
1921                   && htab->interp == NULL)
1922               || info->dynamic_undefined_weak == 0))
1923       || ((h->def_regular || ELF_COMMON_DEF_P (h))
1924           && h->versioned == unversioned
1925           && info->version_info != NULL
1926           && bfd_hide_sym_by_version (info->version_info,
1927                                       h->root.root.string)))
1928     {
1929       eh->local_ref = 2;
1930       return TRUE;
1931     }
1932
1933   eh->local_ref = 1;
1934   return FALSE;
1935 }
1936
1937 /* Return the section that should be marked against GC for a given
1938    relocation.  */
1939
1940 asection *
1941 _bfd_x86_elf_gc_mark_hook (asection *sec,
1942                            struct bfd_link_info *info,
1943                            Elf_Internal_Rela *rel,
1944                            struct elf_link_hash_entry *h,
1945                            Elf_Internal_Sym *sym)
1946 {
1947   /* Compiler should optimize this out.  */
1948   if (((unsigned int) R_X86_64_GNU_VTINHERIT
1949        != (unsigned int) R_386_GNU_VTINHERIT)
1950       || ((unsigned int) R_X86_64_GNU_VTENTRY
1951           != (unsigned int) R_386_GNU_VTENTRY))
1952     abort ();
1953
1954   if (h != NULL)
1955     switch (ELF32_R_TYPE (rel->r_info))
1956       {
1957       case R_X86_64_GNU_VTINHERIT:
1958       case R_X86_64_GNU_VTENTRY:
1959         return NULL;
1960       }
1961
1962   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1963 }
1964
1965 static bfd_vma
1966 elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
1967                           bfd_vma off,
1968                           bfd_vma offset ATTRIBUTE_UNUSED,
1969                           bfd_vma got_addr)
1970 {
1971   return got_addr + off;
1972 }
1973
1974 static bfd_vma
1975 elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
1976                             bfd_vma off,
1977                             bfd_vma offset,
1978                             bfd_vma got_addr ATTRIBUTE_UNUSED)
1979 {
1980   return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
1981 }
1982
1983 static bfd_boolean
1984 elf_i386_valid_plt_reloc_p (unsigned int type)
1985 {
1986   return (type == R_386_JUMP_SLOT
1987           || type == R_386_GLOB_DAT
1988           || type == R_386_IRELATIVE);
1989 }
1990
1991 static bfd_boolean
1992 elf_x86_64_valid_plt_reloc_p (unsigned int type)
1993 {
1994   return (type == R_X86_64_JUMP_SLOT
1995           || type == R_X86_64_GLOB_DAT
1996           || type == R_X86_64_IRELATIVE);
1997 }
1998
1999 long
2000 _bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
2001                                    long count,
2002                                    long relsize,
2003                                    bfd_vma got_addr,
2004                                    struct elf_x86_plt plts[],
2005                                    asymbol **dynsyms,
2006                                    asymbol **ret)
2007 {
2008   long size, i, n, len;
2009   int j;
2010   unsigned int plt_got_offset, plt_entry_size;
2011   asymbol *s;
2012   bfd_byte *plt_contents;
2013   long dynrelcount;
2014   arelent **dynrelbuf, *p;
2015   char *names;
2016   const struct elf_backend_data *bed;
2017   bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
2018                               bfd_vma);
2019   bfd_boolean (*valid_plt_reloc_p) (unsigned int);
2020
2021   if (count == 0)
2022     return -1;
2023
2024   dynrelbuf = (arelent **) bfd_malloc (relsize);
2025   if (dynrelbuf == NULL)
2026     return -1;
2027
2028   dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
2029                                                 dynsyms);
2030   if (dynrelcount <= 0)
2031     return -1;
2032
2033   /* Sort the relocs by address.  */
2034   qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
2035          _bfd_x86_elf_compare_relocs);
2036
2037   size = count * sizeof (asymbol);
2038
2039   /* Allocate space for @plt suffixes.  */
2040   n = 0;
2041   for (i = 0; i < dynrelcount; i++)
2042     {
2043       p = dynrelbuf[i];
2044       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2045       if (p->addend != 0)
2046         size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
2047     }
2048
2049   s = *ret = (asymbol *) bfd_zmalloc (size);
2050   if (s == NULL)
2051     goto bad_return;
2052
2053   bed = get_elf_backend_data (abfd);
2054
2055   if (bed->target_id == X86_64_ELF_DATA)
2056     {
2057       get_plt_got_vma = elf_x86_64_get_plt_got_vma;
2058       valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
2059     }
2060   else
2061     {
2062       get_plt_got_vma = elf_i386_get_plt_got_vma;
2063       valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
2064       if (got_addr)
2065         {
2066           /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
2067              address.  */
2068           asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
2069           if (sec != NULL)
2070             got_addr = sec->vma;
2071           else
2072             {
2073               sec = bfd_get_section_by_name (abfd, ".got");
2074               if (sec != NULL)
2075                 got_addr = sec->vma;
2076             }
2077
2078           if (got_addr == (bfd_vma) -1)
2079             goto bad_return;
2080         }
2081     }
2082
2083   /* Check for each PLT section.  */
2084   names = (char *) (s + count);
2085   size = 0;
2086   n = 0;
2087   for (j = 0; plts[j].name != NULL; j++)
2088     if ((plt_contents = plts[j].contents) != NULL)
2089       {
2090         long k;
2091         bfd_vma offset;
2092         asection *plt;
2093         struct elf_x86_plt *plt_p = &plts[j];
2094
2095         plt_got_offset = plt_p->plt_got_offset;
2096         plt_entry_size = plt_p->plt_entry_size;
2097
2098         plt = plt_p->sec;
2099
2100         if ((plt_p->type & plt_lazy))
2101           {
2102             /* Skip PLT0 in lazy PLT.  */
2103             k = 1;
2104             offset = plt_entry_size;
2105           }
2106         else
2107           {
2108             k = 0;
2109             offset = 0;
2110           }
2111
2112         /* Check each PLT entry against dynamic relocations.  */
2113         for (; k < plt_p->count; k++)
2114           {
2115             int off;
2116             bfd_vma got_vma;
2117             long min, max, mid;
2118
2119             /* Get the GOT offset for i386 or the PC-relative offset
2120                for x86-64, a signed 32-bit integer.  */
2121             off = H_GET_32 (abfd, (plt_contents + offset
2122                                    + plt_got_offset));
2123             got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
2124
2125             /* Binary search.  */
2126             p = dynrelbuf[0];
2127             min = 0;
2128             max = dynrelcount;
2129             while ((min + 1) < max)
2130               {
2131                 arelent *r;
2132
2133                 mid = (min + max) / 2;
2134                 r = dynrelbuf[mid];
2135                 if (got_vma > r->address)
2136                   min = mid;
2137                 else if (got_vma < r->address)
2138                   max = mid;
2139                 else
2140                   {
2141                     p = r;
2142                     break;
2143                   }
2144               }
2145
2146             /* Skip unknown relocation.  PR 17512: file: bc9d6cf5.  */
2147             if (got_vma == p->address
2148                 && p->howto != NULL
2149                 && valid_plt_reloc_p (p->howto->type))
2150               {
2151                 *s = **p->sym_ptr_ptr;
2152                 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
2153                    set.  Since we are defining a symbol, ensure one
2154                    of them is set.  */
2155                 if ((s->flags & BSF_LOCAL) == 0)
2156                   s->flags |= BSF_GLOBAL;
2157                 s->flags |= BSF_SYNTHETIC;
2158                 /* This is no longer a section symbol.  */
2159                 s->flags &= ~BSF_SECTION_SYM;
2160                 s->section = plt;
2161                 s->the_bfd = plt->owner;
2162                 s->value = offset;
2163                 s->udata.p = NULL;
2164                 s->name = names;
2165                 len = strlen ((*p->sym_ptr_ptr)->name);
2166                 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2167                 names += len;
2168                 if (p->addend != 0)
2169                   {
2170                     char buf[30], *a;
2171
2172                     memcpy (names, "+0x", sizeof ("+0x") - 1);
2173                     names += sizeof ("+0x") - 1;
2174                     bfd_sprintf_vma (abfd, buf, p->addend);
2175                     for (a = buf; *a == '0'; ++a)
2176                       ;
2177                     size = strlen (a);
2178                     memcpy (names, a, size);
2179                     names += size;
2180                   }
2181                 memcpy (names, "@plt", sizeof ("@plt"));
2182                 names += sizeof ("@plt");
2183                 n++;
2184                 s++;
2185                 /* There should be only one entry in PLT for a given
2186                    symbol.  Set howto to NULL after processing a PLT
2187                    entry to guard against corrupted PLT.  */
2188                 p->howto = NULL;
2189               }
2190             offset += plt_entry_size;
2191           }
2192       }
2193
2194   /* PLT entries with R_386_TLS_DESC relocations are skipped.  */
2195   if (n == 0)
2196     {
2197 bad_return:
2198       count = -1;
2199     }
2200   else
2201     count = n;
2202
2203   for (j = 0; plts[j].name != NULL; j++)
2204     if (plts[j].contents != NULL)
2205       free (plts[j].contents);
2206
2207   free (dynrelbuf);
2208
2209   return count;
2210 }
2211
2212 /* Parse x86 GNU properties.  */
2213
2214 enum elf_property_kind
2215 _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
2216                                    bfd_byte *ptr, unsigned int datasz)
2217 {
2218   elf_property *prop;
2219
2220   switch (type)
2221     {
2222     case GNU_PROPERTY_X86_ISA_1_USED:
2223     case GNU_PROPERTY_X86_ISA_1_NEEDED:
2224     case GNU_PROPERTY_X86_FEATURE_1_AND:
2225       if (datasz != 4)
2226         {
2227           _bfd_error_handler
2228             ((type == GNU_PROPERTY_X86_ISA_1_USED
2229               ? _("error: %B: <corrupt x86 ISA used size: 0x%x>")
2230               : (type == GNU_PROPERTY_X86_ISA_1_NEEDED
2231                  ? _("error: %B: <corrupt x86 ISA needed size: 0x%x>")
2232                  : _("error: %B: <corrupt x86 feature size: 0x%x>"))),
2233              abfd, datasz);
2234           return property_corrupt;
2235         }
2236       prop = _bfd_elf_get_property (abfd, type, datasz);
2237       /* Combine properties of the same type.  */
2238       prop->u.number |= bfd_h_get_32 (abfd, ptr);
2239       prop->pr_kind = property_number;
2240       break;
2241
2242     default:
2243       return property_ignored;
2244     }
2245
2246   return property_number;
2247 }
2248
2249 /* Merge x86 GNU property BPROP with APROP.  If APROP isn't NULL,
2250    return TRUE if APROP is updated.  Otherwise, return TRUE if BPROP
2251    should be merged with ABFD.  */
2252
2253 bfd_boolean
2254 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
2255                                    bfd *abfd ATTRIBUTE_UNUSED,
2256                                    elf_property *aprop,
2257                                    elf_property *bprop)
2258 {
2259   unsigned int number, features;
2260   bfd_boolean updated = FALSE;
2261   unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
2262
2263   switch (pr_type)
2264     {
2265     case GNU_PROPERTY_X86_ISA_1_USED:
2266     case GNU_PROPERTY_X86_ISA_1_NEEDED:
2267       if (aprop != NULL && bprop != NULL)
2268         {
2269           number = aprop->u.number;
2270           aprop->u.number = number | bprop->u.number;
2271           updated = number != (unsigned int) aprop->u.number;
2272         }
2273       else
2274         {
2275           /* Return TRUE if APROP is NULL to indicate that BPROP should
2276              be added to ABFD.  */
2277           updated = aprop == NULL;
2278         }
2279       break;
2280
2281     case GNU_PROPERTY_X86_FEATURE_1_AND:
2282       /* Only one of APROP and BPROP can be NULL:
2283          1. APROP & BPROP when both APROP and BPROP aren't NULL.
2284          2. If APROP is NULL, remove x86 feature.
2285          3. Otherwise, do nothing.
2286        */
2287       if (aprop != NULL && bprop != NULL)
2288         {
2289           features = 0;
2290           if (info->ibt)
2291             features = GNU_PROPERTY_X86_FEATURE_1_IBT;
2292           if (info->shstk)
2293             features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
2294           number = aprop->u.number;
2295           /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
2296              GNU_PROPERTY_X86_FEATURE_1_SHSTK.  */
2297           aprop->u.number = (number & bprop->u.number) | features;
2298           updated = number != (unsigned int) aprop->u.number;
2299           /* Remove the property if all feature bits are cleared.  */
2300           if (aprop->u.number == 0)
2301             aprop->pr_kind = property_remove;
2302         }
2303       else
2304         {
2305           features = 0;
2306           if (info->ibt)
2307             features = GNU_PROPERTY_X86_FEATURE_1_IBT;
2308           if (info->shstk)
2309             features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
2310           if (features)
2311             {
2312               /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
2313                  GNU_PROPERTY_X86_FEATURE_1_SHSTK.  */
2314               if (aprop != NULL)
2315                 {
2316                   number = aprop->u.number;
2317                   aprop->u.number = number | features;
2318                   updated = number != (unsigned int) aprop->u.number;
2319                 }
2320               else
2321                 {
2322                   bprop->u.number |= features;
2323                   updated = TRUE;
2324                 }
2325             }
2326           else if (aprop != NULL)
2327             {
2328               aprop->pr_kind = property_remove;
2329               updated = TRUE;
2330             }
2331         }
2332       break;
2333
2334     default:
2335       /* Never should happen.  */
2336       abort ();
2337     }
2338
2339   return updated;
2340 }
2341
2342 /* Set up x86 GNU properties.  Return the first relocatable ELF input
2343    with GNU properties if found.  Otherwise, return NULL.  */
2344
2345 bfd *
2346 _bfd_x86_elf_link_setup_gnu_properties
2347   (struct bfd_link_info *info, struct elf_x86_init_table *init_table)
2348 {
2349   bfd_boolean normal_target;
2350   bfd_boolean lazy_plt;
2351   asection *sec, *pltsec;
2352   bfd *dynobj;
2353   bfd_boolean use_ibt_plt;
2354   unsigned int plt_alignment, features;
2355   struct elf_x86_link_hash_table *htab;
2356   bfd *pbfd;
2357   bfd *ebfd = NULL;
2358   elf_property *prop;
2359   const struct elf_backend_data *bed;
2360   unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
2361   unsigned int got_align;
2362
2363   features = 0;
2364   if (info->ibt)
2365     features = GNU_PROPERTY_X86_FEATURE_1_IBT;
2366   if (info->shstk)
2367     features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
2368
2369   /* Find a normal input file with GNU property note.  */
2370   for (pbfd = info->input_bfds;
2371        pbfd != NULL;
2372        pbfd = pbfd->link.next)
2373     if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
2374         && bfd_count_sections (pbfd) != 0)
2375       {
2376         ebfd = pbfd;
2377
2378         if (elf_properties (pbfd) != NULL)
2379           break;
2380       }
2381
2382   if (ebfd != NULL && features)
2383     {
2384       /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and
2385          GNU_PROPERTY_X86_FEATURE_1_SHSTK.  */
2386       prop = _bfd_elf_get_property (ebfd,
2387                                     GNU_PROPERTY_X86_FEATURE_1_AND,
2388                                     4);
2389       prop->u.number |= features;
2390       prop->pr_kind = property_number;
2391
2392       /* Create the GNU property note section if needed.  */
2393       if (pbfd == NULL)
2394         {
2395           sec = bfd_make_section_with_flags (ebfd,
2396                                              NOTE_GNU_PROPERTY_SECTION_NAME,
2397                                              (SEC_ALLOC
2398                                               | SEC_LOAD
2399                                               | SEC_IN_MEMORY
2400                                               | SEC_READONLY
2401                                               | SEC_HAS_CONTENTS
2402                                               | SEC_DATA));
2403           if (sec == NULL)
2404             info->callbacks->einfo (_("%F: failed to create GNU property section\n"));
2405
2406           if (!bfd_set_section_alignment (ebfd, sec, class_align))
2407             {
2408 error_alignment:
2409               info->callbacks->einfo (_("%F%A: failed to align section\n"),
2410                                       sec);
2411             }
2412
2413           elf_section_type (sec) = SHT_NOTE;
2414         }
2415     }
2416
2417   pbfd = _bfd_elf_link_setup_gnu_properties (info);
2418
2419   bed = get_elf_backend_data (info->output_bfd);
2420
2421   htab = elf_x86_hash_table (info, bed->target_id);
2422   if (htab == NULL)
2423     return pbfd;
2424
2425   htab->r_info = init_table->r_info;
2426   htab->r_sym = init_table->r_sym;
2427
2428   if (bfd_link_relocatable (info))
2429     return pbfd;
2430
2431   htab->plt0_pad_byte = init_table->plt0_pad_byte;
2432
2433   use_ibt_plt = info->ibtplt || info->ibt;
2434   if (!use_ibt_plt && pbfd != NULL)
2435     {
2436       /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on.  */
2437       elf_property_list *p;
2438
2439       /* The property list is sorted in order of type.  */
2440       for (p = elf_properties (pbfd); p; p = p->next)
2441         {
2442           if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
2443             {
2444               use_ibt_plt = !!(p->property.u.number
2445                                & GNU_PROPERTY_X86_FEATURE_1_IBT);
2446               break;
2447             }
2448           else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
2449             break;
2450         }
2451     }
2452
2453   dynobj = htab->elf.dynobj;
2454
2455   /* Set htab->elf.dynobj here so that there is no need to check and
2456      set it in check_relocs.  */
2457   if (dynobj == NULL)
2458     {
2459       if (pbfd != NULL)
2460         {
2461           htab->elf.dynobj = pbfd;
2462           dynobj = pbfd;
2463         }
2464       else
2465         {
2466           bfd *abfd;
2467
2468           /* Find a normal input file to hold linker created
2469              sections.  */
2470           for (abfd = info->input_bfds;
2471                abfd != NULL;
2472                abfd = abfd->link.next)
2473             if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2474                 && (abfd->flags
2475                     & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
2476               {
2477                 htab->elf.dynobj = abfd;
2478                 dynobj = abfd;
2479                 break;
2480               }
2481         }
2482     }
2483
2484   /* Return if there are no normal input files.  */
2485   if (dynobj == NULL)
2486     return pbfd;
2487
2488   /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
2489      still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
2490      canonical function address.  */
2491   htab->plt.has_plt0 = 1;
2492   normal_target = htab->target_os == is_normal;
2493
2494   if (normal_target)
2495     {
2496       if (use_ibt_plt)
2497         {
2498           htab->lazy_plt = init_table->lazy_ibt_plt;
2499           htab->non_lazy_plt = init_table->non_lazy_ibt_plt;
2500         }
2501       else
2502         {
2503           htab->lazy_plt = init_table->lazy_plt;
2504           htab->non_lazy_plt = init_table->non_lazy_plt;
2505         }
2506     }
2507   else
2508     {
2509       htab->lazy_plt = init_table->lazy_plt;
2510       htab->non_lazy_plt = NULL;
2511     }
2512
2513   pltsec = htab->elf.splt;
2514
2515   /* If the non-lazy PLT is available, use it for all PLT entries if
2516      there are no PLT0 or no .plt section.  */
2517   if (htab->non_lazy_plt != NULL
2518       && (!htab->plt.has_plt0 || pltsec == NULL))
2519     {
2520       lazy_plt = FALSE;
2521       if (bfd_link_pic (info))
2522         htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
2523       else
2524         htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
2525       htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
2526       htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
2527       htab->plt.plt_got_insn_size
2528         = htab->non_lazy_plt->plt_got_insn_size;
2529       htab->plt.eh_frame_plt_size
2530         = htab->non_lazy_plt->eh_frame_plt_size;
2531       htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
2532     }
2533   else
2534     {
2535       lazy_plt = TRUE;
2536       if (bfd_link_pic (info))
2537         {
2538           htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
2539           htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
2540         }
2541       else
2542         {
2543           htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
2544           htab->plt.plt_entry = htab->lazy_plt->plt_entry;
2545         }
2546       htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
2547       htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
2548       htab->plt.plt_got_insn_size
2549         = htab->lazy_plt->plt_got_insn_size;
2550       htab->plt.eh_frame_plt_size
2551         = htab->lazy_plt->eh_frame_plt_size;
2552       htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
2553     }
2554
2555   if (htab->target_os == is_vxworks
2556       && !elf_vxworks_create_dynamic_sections (dynobj, info,
2557                                                &htab->srelplt2))
2558     {
2559       info->callbacks->einfo (_("%F: failed to create VxWorks dynamic sections\n"));
2560       return pbfd;
2561     }
2562
2563   /* Since create_dynamic_sections isn't always called, but GOT
2564      relocations need GOT relocations, create them here so that we
2565      don't need to do it in check_relocs.  */
2566   if (htab->elf.sgot == NULL
2567       && !_bfd_elf_create_got_section (dynobj, info))
2568     info->callbacks->einfo (_("%F: failed to create GOT sections\n"));
2569
2570   got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
2571
2572   /* Align .got and .got.plt sections to their entry size.  Do it here
2573      instead of in create_dynamic_sections so that they are always
2574      properly aligned even if create_dynamic_sections isn't called.  */
2575   sec = htab->elf.sgot;
2576   if (!bfd_set_section_alignment (dynobj, sec, got_align))
2577     goto error_alignment;
2578
2579   sec = htab->elf.sgotplt;
2580   if (!bfd_set_section_alignment (dynobj, sec, got_align))
2581     goto error_alignment;
2582
2583   /* Create the ifunc sections here so that check_relocs can be
2584      simplified.  */
2585   if (!_bfd_elf_create_ifunc_sections (dynobj, info))
2586     info->callbacks->einfo (_("%F: failed to create ifunc sections\n"));
2587
2588   plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
2589
2590   if (pltsec != NULL)
2591     {
2592       /* Whe creating executable, set the contents of the .interp
2593          section to the interpreter.  */
2594       if (bfd_link_executable (info) && !info->nointerp)
2595         {
2596           asection *s = bfd_get_linker_section (dynobj, ".interp");
2597           if (s == NULL)
2598             abort ();
2599           s->size = htab->dynamic_interpreter_size;
2600           s->contents = (unsigned char *) htab->dynamic_interpreter;
2601           htab->interp = s;
2602         }
2603
2604       /* Don't change PLT section alignment for NaCl since it uses
2605          64-byte PLT entry and sets PLT section alignment to 32
2606          bytes.  Don't create additional PLT sections for NaCl.  */
2607       if (normal_target)
2608         {
2609           flagword pltflags = (bed->dynamic_sec_flags
2610                                | SEC_ALLOC
2611                                | SEC_CODE
2612                                | SEC_LOAD
2613                                | SEC_READONLY);
2614           unsigned int non_lazy_plt_alignment
2615             = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
2616
2617           sec = pltsec;
2618           if (!bfd_set_section_alignment (sec->owner, sec,
2619                                           plt_alignment))
2620             goto error_alignment;
2621
2622           /* Create the GOT procedure linkage table.  */
2623           sec = bfd_make_section_anyway_with_flags (dynobj,
2624                                                     ".plt.got",
2625                                                     pltflags);
2626           if (sec == NULL)
2627             info->callbacks->einfo (_("%F: failed to create GOT PLT section\n"));
2628
2629           if (!bfd_set_section_alignment (dynobj, sec,
2630                                           non_lazy_plt_alignment))
2631             goto error_alignment;
2632
2633           htab->plt_got = sec;
2634
2635           if (lazy_plt)
2636             {
2637               sec = NULL;
2638
2639               if (use_ibt_plt)
2640                 {
2641                   /* Create the second PLT for Intel IBT support.  IBT
2642                      PLT is supported only for non-NaCl target and is
2643                      is needed only for lazy binding.  */
2644                   sec = bfd_make_section_anyway_with_flags (dynobj,
2645                                                             ".plt.sec",
2646                                                             pltflags);
2647                   if (sec == NULL)
2648                     info->callbacks->einfo (_("%F: failed to create IBT-enabled PLT section\n"));
2649
2650                   if (!bfd_set_section_alignment (dynobj, sec,
2651                                                   plt_alignment))
2652                     goto error_alignment;
2653                 }
2654               else if (info->bndplt && ABI_64_P (dynobj))
2655                 {
2656                   /* Create the second PLT for Intel MPX support.  MPX
2657                      PLT is supported only for non-NaCl target in 64-bit
2658                      mode and is needed only for lazy binding.  */
2659                   sec = bfd_make_section_anyway_with_flags (dynobj,
2660                                                             ".plt.sec",
2661                                                             pltflags);
2662                   if (sec == NULL)
2663                     info->callbacks->einfo (_("%F: failed to create BND PLT section\n"));
2664
2665                   if (!bfd_set_section_alignment (dynobj, sec,
2666                                                   non_lazy_plt_alignment))
2667                     goto error_alignment;
2668                 }
2669
2670               htab->plt_second = sec;
2671             }
2672         }
2673
2674       if (!info->no_ld_generated_unwind_info)
2675         {
2676           flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2677                             | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2678                             | SEC_LINKER_CREATED);
2679
2680           sec = bfd_make_section_anyway_with_flags (dynobj,
2681                                                     ".eh_frame",
2682                                                     flags);
2683           if (sec == NULL)
2684             info->callbacks->einfo (_("%F: failed to create PLT .eh_frame section\n"));
2685
2686           if (!bfd_set_section_alignment (dynobj, sec, class_align))
2687             goto error_alignment;
2688
2689           htab->plt_eh_frame = sec;
2690
2691           if (htab->plt_got != NULL)
2692             {
2693               sec = bfd_make_section_anyway_with_flags (dynobj,
2694                                                         ".eh_frame",
2695                                                         flags);
2696               if (sec == NULL)
2697                 info->callbacks->einfo (_("%F: failed to create GOT PLT .eh_frame section\n"));
2698
2699               if (!bfd_set_section_alignment (dynobj, sec, class_align))
2700                 goto error_alignment;
2701
2702               htab->plt_got_eh_frame = sec;
2703             }
2704
2705           if (htab->plt_second != NULL)
2706             {
2707               sec = bfd_make_section_anyway_with_flags (dynobj,
2708                                                         ".eh_frame",
2709                                                         flags);
2710               if (sec == NULL)
2711                 info->callbacks->einfo (_("%F: failed to create the second PLT .eh_frame section\n"));
2712
2713               if (!bfd_set_section_alignment (dynobj, sec, class_align))
2714                 goto error_alignment;
2715
2716               htab->plt_second_eh_frame = sec;
2717             }
2718         }
2719     }
2720
2721   if (normal_target)
2722     {
2723       /* The .iplt section is used for IFUNC symbols in static
2724          executables.  */
2725       sec = htab->elf.iplt;
2726       if (sec != NULL
2727           && !bfd_set_section_alignment (sec->owner, sec,
2728                                          plt_alignment))
2729         goto error_alignment;
2730     }
2731
2732   return pbfd;
2733 }