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