xref: /netbsd-src/external/gpl3/binutils/dist/bfd/elfxx-x86.c (revision 8e33eff89e26cf71871ead62f0d5063e1313c33a)
1 /* x86 specific support for ELF
2    Copyright (C) 2017-2024 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 
25 /* The name of the dynamic interpreter.  This is put in the .interp
26    section.  */
27 
28 #define ELF32_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
29 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
30 #define ELFX32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
31 
32 bool
33 _bfd_x86_elf_mkobject (bfd *abfd)
34 {
35   return bfd_elf_allocate_object (abfd,
36 				  sizeof (struct elf_x86_obj_tdata),
37 				  get_elf_backend_data (abfd)->target_id);
38 }
39 
40 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
41    executables.  Rather than setting it to the beginning of the TLS
42    section, we have to set it to the end.    This function may be called
43    multiple times, it is idempotent.  */
44 
45 void
46 _bfd_x86_elf_set_tls_module_base (struct bfd_link_info *info)
47 {
48   struct elf_x86_link_hash_table *htab;
49   struct bfd_link_hash_entry *base;
50   const struct elf_backend_data *bed;
51 
52   if (!bfd_link_executable (info))
53     return;
54 
55   bed = get_elf_backend_data (info->output_bfd);
56   htab = elf_x86_hash_table (info, bed->target_id);
57   if (htab == NULL)
58     return;
59 
60   base = htab->tls_module_base;
61   if (base == NULL)
62     return;
63 
64   base->u.def.value = htab->elf.tls_size;
65 }
66 
67 /* Return the base VMA address which should be subtracted from real addresses
68    when resolving @dtpoff relocation.
69    This is PT_TLS segment p_vaddr.  */
70 
71 bfd_vma
72 _bfd_x86_elf_dtpoff_base (struct bfd_link_info *info)
73 {
74   /* If tls_sec is NULL, we should have signalled an error already.  */
75   if (elf_hash_table (info)->tls_sec == NULL)
76     return 0;
77   return elf_hash_table (info)->tls_sec->vma;
78 }
79 
80 /* Allocate space in .plt, .got and associated reloc sections for
81    dynamic relocs.  */
82 
83 static bool
84 elf_x86_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
85 {
86   struct bfd_link_info *info;
87   struct elf_x86_link_hash_table *htab;
88   struct elf_x86_link_hash_entry *eh;
89   struct elf_dyn_relocs *p;
90   unsigned int plt_entry_size;
91   bool resolved_to_zero;
92   const struct elf_backend_data *bed;
93 
94   if (h->root.type == bfd_link_hash_indirect)
95     return true;
96 
97   eh = (struct elf_x86_link_hash_entry *) h;
98 
99   info = (struct bfd_link_info *) inf;
100   bed = get_elf_backend_data (info->output_bfd);
101   htab = elf_x86_hash_table (info, bed->target_id);
102   if (htab == NULL)
103     return false;
104 
105   plt_entry_size = htab->plt.plt_entry_size;
106 
107   resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
108 
109   /* We can't use the GOT PLT if pointer equality is needed since
110      finish_dynamic_symbol won't clear symbol value and the dynamic
111      linker won't update the GOT slot.  We will get into an infinite
112      loop at run-time.  */
113   if (htab->plt_got != NULL
114       && h->type != STT_GNU_IFUNC
115       && !h->pointer_equality_needed
116       && h->plt.refcount > 0
117       && h->got.refcount > 0)
118     {
119       /* Don't use the regular PLT if there are both GOT and GOTPLT
120 	 reloctions.  */
121       h->plt.offset = (bfd_vma) -1;
122 
123       /* Use the GOT PLT.  */
124       eh->plt_got.refcount = 1;
125     }
126 
127   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
128      here if it is defined and referenced in a non-shared object.  */
129   if (h->type == STT_GNU_IFUNC
130       && h->def_regular)
131     {
132       /* GOTOFF relocation needs PLT.  */
133       if (eh->gotoff_ref)
134 	h->plt.refcount = 1;
135 
136       if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h, &h->dyn_relocs,
137 					      plt_entry_size,
138 					      (htab->plt.has_plt0
139 					       * plt_entry_size),
140 					       htab->got_entry_size,
141 					       true))
142 	{
143 	  asection *s = htab->plt_second;
144 	  if (h->plt.offset != (bfd_vma) -1 && s != NULL)
145 	    {
146 	      /* Use the second PLT section if it is created.  */
147 	      eh->plt_second.offset = s->size;
148 
149 	      /* Make room for this entry in the second PLT section.  */
150 	      s->size += htab->non_lazy_plt->plt_entry_size;
151 	    }
152 
153 	  return true;
154 	}
155       else
156 	return false;
157     }
158   /* Don't create the PLT entry if there are only function pointer
159      relocations which can be resolved at run-time.  */
160   else if (htab->elf.dynamic_sections_created
161 	   && (h->plt.refcount > 0
162 	       || eh->plt_got.refcount > 0))
163     {
164       bool use_plt_got = eh->plt_got.refcount > 0;
165 
166       /* Make sure this symbol is output as a dynamic symbol.
167 	 Undefined weak syms won't yet be marked as dynamic.  */
168       if (h->dynindx == -1
169 	  && !h->forced_local
170 	  && !resolved_to_zero
171 	  && h->root.type == bfd_link_hash_undefweak)
172 	{
173 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
174 	    return false;
175 	}
176 
177       if (bfd_link_pic (info)
178 	  || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
179 	{
180 	  asection *s = htab->elf.splt;
181 	  asection *second_s = htab->plt_second;
182 	  asection *got_s = htab->plt_got;
183 	  bool use_plt;
184 
185 	  /* If this is the first .plt entry, make room for the special
186 	     first entry.  The .plt section is used by prelink to undo
187 	     prelinking for dynamic relocations.  */
188 	  if (s->size == 0)
189 	    s->size = htab->plt.has_plt0 * plt_entry_size;
190 
191 	  if (use_plt_got)
192 	    eh->plt_got.offset = got_s->size;
193 	  else
194 	    {
195 	      h->plt.offset = s->size;
196 	      if (second_s)
197 		eh->plt_second.offset = second_s->size;
198 	    }
199 
200 	  /* If this symbol is not defined in a regular file, and we are
201 	     generating PDE, then set the symbol to this location in the
202 	     .plt.  This is required to make function pointers compare
203 	     as equal between PDE and the shared library.
204 
205 	     NB: If PLT is PC-relative, we can use the .plt in PIE for
206 	     function address. */
207 	  if (h->def_regular)
208 	    use_plt = false;
209 	  else if (htab->pcrel_plt)
210 	    use_plt = ! bfd_link_dll (info);
211 	  else
212 	    use_plt = bfd_link_pde (info);
213 	  if (use_plt)
214 	    {
215 	      if (use_plt_got)
216 		{
217 		  /* We need to make a call to the entry of the GOT PLT
218 		     instead of regular PLT entry.  */
219 		  h->root.u.def.section = got_s;
220 		  h->root.u.def.value = eh->plt_got.offset;
221 		}
222 	      else
223 		{
224 		  if (second_s)
225 		    {
226 		      /* We need to make a call to the entry of the
227 			 second PLT instead of regular PLT entry.  */
228 		      h->root.u.def.section = second_s;
229 		      h->root.u.def.value = eh->plt_second.offset;
230 		    }
231 		  else
232 		    {
233 		      h->root.u.def.section = s;
234 		      h->root.u.def.value = h->plt.offset;
235 		    }
236 		}
237 	    }
238 
239 	  /* Make room for this entry.  */
240 	  if (use_plt_got)
241 	    got_s->size += htab->non_lazy_plt->plt_entry_size;
242 	  else
243 	    {
244 	      s->size += plt_entry_size;
245 	      if (second_s)
246 		second_s->size += htab->non_lazy_plt->plt_entry_size;
247 
248 	      /* We also need to make an entry in the .got.plt section,
249 		 which will be placed in the .got section by the linker
250 		 script.  */
251 	      htab->elf.sgotplt->size += htab->got_entry_size;
252 
253 	      /* There should be no PLT relocation against resolved
254 		 undefined weak symbol in executable.  */
255 	      if (!resolved_to_zero)
256 		{
257 		  /* We also need to make an entry in the .rel.plt
258 		     section.  */
259 		  htab->elf.srelplt->size += htab->sizeof_reloc;
260 		  htab->elf.srelplt->reloc_count++;
261 		}
262 	    }
263 
264 	  if (htab->elf.target_os == is_vxworks && !bfd_link_pic (info))
265 	    {
266 	      /* VxWorks has a second set of relocations for each PLT entry
267 		 in executables.  They go in a separate relocation section,
268 		 which is processed by the kernel loader.  */
269 
270 	      /* There are two relocations for the initial PLT entry: an
271 		 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
272 		 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
273 
274 	      asection *srelplt2 = htab->srelplt2;
275 	      if (h->plt.offset == plt_entry_size)
276 		srelplt2->size += (htab->sizeof_reloc * 2);
277 
278 	      /* There are two extra relocations for each subsequent PLT entry:
279 		 an R_386_32 relocation for the GOT entry, and an R_386_32
280 		 relocation for the PLT entry.  */
281 
282 	      srelplt2->size += (htab->sizeof_reloc * 2);
283 	    }
284 	}
285       else
286 	{
287 	  eh->plt_got.offset = (bfd_vma) -1;
288 	  h->plt.offset = (bfd_vma) -1;
289 	  h->needs_plt = 0;
290 	}
291     }
292   else
293     {
294       eh->plt_got.offset = (bfd_vma) -1;
295       h->plt.offset = (bfd_vma) -1;
296       h->needs_plt = 0;
297     }
298 
299   eh->tlsdesc_got = (bfd_vma) -1;
300 
301   /* For i386, if R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the
302      binary, make it a R_386_TLS_LE_32 requiring no TLS entry.  For
303      x86-64, if R_X86_64_GOTTPOFF symbol is now local to the binary,
304      make it a R_X86_64_TPOFF32 requiring no GOT entry.  */
305   if (h->got.refcount > 0
306       && bfd_link_executable (info)
307       && h->dynindx == -1
308       && (elf_x86_hash_entry (h)->tls_type & GOT_TLS_IE))
309     h->got.offset = (bfd_vma) -1;
310   else if (h->got.refcount > 0)
311     {
312       asection *s;
313       bool dyn;
314       int tls_type = elf_x86_hash_entry (h)->tls_type;
315 
316       /* Make sure this symbol is output as a dynamic symbol.
317 	 Undefined weak syms won't yet be marked as dynamic.  */
318       if (h->dynindx == -1
319 	  && !h->forced_local
320 	  && !resolved_to_zero
321 	  && h->root.type == bfd_link_hash_undefweak)
322 	{
323 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
324 	    return false;
325 	}
326 
327       s = htab->elf.sgot;
328       if (GOT_TLS_GDESC_P (tls_type))
329 	{
330 	  eh->tlsdesc_got = htab->elf.sgotplt->size
331 	    - elf_x86_compute_jump_table_size (htab);
332 	  htab->elf.sgotplt->size += 2 * htab->got_entry_size;
333 	  h->got.offset = (bfd_vma) -2;
334 	}
335       if (! GOT_TLS_GDESC_P (tls_type)
336 	  || GOT_TLS_GD_P (tls_type))
337 	{
338 	  h->got.offset = s->size;
339 	  s->size += htab->got_entry_size;
340 	  /* R_386_TLS_GD and R_X86_64_TLSGD need 2 consecutive GOT
341 	     slots.  */
342 	  if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
343 	    s->size += htab->got_entry_size;
344 	}
345       dyn = htab->elf.dynamic_sections_created;
346       /* R_386_TLS_IE_32 needs one dynamic relocation,
347 	 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
348 	 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
349 	 need two), R_386_TLS_GD and R_X86_64_TLSGD need one if local
350 	 symbol and two if global.  No dynamic relocation against
351 	 resolved undefined weak symbol in executable.  No dynamic
352 	 relocation against non-preemptible absolute symbol.  */
353       if (tls_type == GOT_TLS_IE_BOTH)
354 	htab->elf.srelgot->size += 2 * htab->sizeof_reloc;
355       else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
356 	       || (tls_type & GOT_TLS_IE))
357 	htab->elf.srelgot->size += htab->sizeof_reloc;
358       else if (GOT_TLS_GD_P (tls_type))
359 	htab->elf.srelgot->size += 2 * htab->sizeof_reloc;
360       else if (! GOT_TLS_GDESC_P (tls_type)
361 	       && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
362 		    && !resolved_to_zero)
363 		   || h->root.type != bfd_link_hash_undefweak)
364 	       && ((bfd_link_pic (info)
365 		    && !(h->dynindx == -1
366 			 && ABS_SYMBOL_P (h)))
367 		   || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
368 	htab->elf.srelgot->size += htab->sizeof_reloc;
369       if (GOT_TLS_GDESC_P (tls_type))
370 	{
371 	  htab->elf.srelplt->size += htab->sizeof_reloc;
372 	  if (bed->target_id == X86_64_ELF_DATA)
373 	    htab->elf.tlsdesc_plt = (bfd_vma) -1;
374 	}
375     }
376   else
377     h->got.offset = (bfd_vma) -1;
378 
379   if (h->dyn_relocs == NULL)
380     return true;
381 
382   /* In the shared -Bsymbolic case, discard space allocated for
383      dynamic pc-relative relocs against symbols which turn out to be
384      defined in regular objects.  For the normal shared case, discard
385      space for pc-relative relocs that have become local due to symbol
386      visibility changes.  */
387 
388   if (bfd_link_pic (info))
389     {
390       /* Relocs that use pc_count are those that appear on a call
391 	 insn, or certain REL relocs that can generated via assembly.
392 	 We want calls to protected symbols to resolve directly to the
393 	 function rather than going via the plt.  If people want
394 	 function pointer comparisons to work as expected then they
395 	 should avoid writing weird assembly.  */
396       if (SYMBOL_CALLS_LOCAL (info, h))
397 	{
398 	  struct elf_dyn_relocs **pp;
399 
400 	  for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
401 	    {
402 	      p->count -= p->pc_count;
403 	      p->pc_count = 0;
404 	      if (p->count == 0)
405 		*pp = p->next;
406 	      else
407 		pp = &p->next;
408 	    }
409 	}
410 
411       if (htab->elf.target_os == is_vxworks)
412 	{
413 	  struct elf_dyn_relocs **pp;
414 	  for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
415 	    {
416 	      if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
417 		*pp = p->next;
418 	      else
419 		pp = &p->next;
420 	    }
421 	}
422 
423       /* Also discard relocs on undefined weak syms with non-default
424 	 visibility or in PIE.  */
425       if (h->dyn_relocs != NULL)
426 	{
427 	  if (h->root.type == bfd_link_hash_undefweak)
428 	    {
429 	      /* Undefined weak symbol is never bound locally in shared
430 		 library.  */
431 	      if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
432 		  || resolved_to_zero)
433 		{
434 		  if (bed->target_id == I386_ELF_DATA
435 		      && h->non_got_ref)
436 		    {
437 		      /* Keep dynamic non-GOT/non-PLT relocation so
438 			 that we can branch to 0 without PLT.  */
439 		      struct elf_dyn_relocs **pp;
440 
441 		      for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
442 			if (p->pc_count == 0)
443 			  *pp = p->next;
444 			else
445 			  {
446 			    /* Remove non-R_386_PC32 relocation.  */
447 			    p->count = p->pc_count;
448 			    pp = &p->next;
449 			  }
450 
451 		      /* Make sure undefined weak symbols are output
452 			 as dynamic symbols in PIEs for dynamic non-GOT
453 			 non-PLT reloations.  */
454 		      if (h->dyn_relocs != NULL
455 			  && !bfd_elf_link_record_dynamic_symbol (info, h))
456 			return false;
457 		    }
458 		  else
459 		    h->dyn_relocs = NULL;
460 		}
461 	      else if (h->dynindx == -1
462 		       && !h->forced_local
463 		       && !bfd_elf_link_record_dynamic_symbol (info, h))
464 		return false;
465 	    }
466 	  else if (bfd_link_executable (info)
467 		   && (h->needs_copy || eh->needs_copy)
468 		   && h->def_dynamic
469 		   && !h->def_regular)
470 	    {
471 	      /* NB: needs_copy is set only for x86-64.  For PIE,
472 		 discard space for pc-relative relocs against symbols
473 		 which turn out to need copy relocs.  */
474 	      struct elf_dyn_relocs **pp;
475 
476 	      for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
477 		{
478 		  if (p->pc_count != 0)
479 		    *pp = p->next;
480 		  else
481 		    pp = &p->next;
482 		}
483 	    }
484 	}
485     }
486   else if (ELIMINATE_COPY_RELOCS)
487     {
488       /* For the non-shared case, discard space for relocs against
489 	 symbols which turn out to need copy relocs or are not
490 	 dynamic.  Keep dynamic relocations for run-time function
491 	 pointer initialization.  */
492 
493       if ((!h->non_got_ref
494 	   || (h->root.type == bfd_link_hash_undefweak
495 	       && !resolved_to_zero))
496 	  && ((h->def_dynamic
497 	       && !h->def_regular)
498 	      || (htab->elf.dynamic_sections_created
499 		  && (h->root.type == bfd_link_hash_undefweak
500 		      || h->root.type == bfd_link_hash_undefined))))
501 	{
502 	  /* Make sure this symbol is output as a dynamic symbol.
503 	     Undefined weak syms won't yet be marked as dynamic.  */
504 	  if (h->dynindx == -1
505 	      && !h->forced_local
506 	      && !resolved_to_zero
507 	      && h->root.type == bfd_link_hash_undefweak
508 	      && ! bfd_elf_link_record_dynamic_symbol (info, h))
509 	    return false;
510 
511 	  /* If that succeeded, we know we'll be keeping all the
512 	     relocs.  */
513 	  if (h->dynindx != -1)
514 	    goto keep;
515 	}
516 
517       h->dyn_relocs = NULL;
518 
519     keep: ;
520     }
521 
522   /* Finally, allocate space.  */
523   for (p = h->dyn_relocs; p != NULL; p = p->next)
524     {
525       asection *sreloc;
526 
527       if (eh->def_protected && bfd_link_executable (info))
528 	{
529 	  /* Disallow copy relocation against non-copyable protected
530 	     symbol.  */
531 	  asection *s = p->sec->output_section;
532 	  if (s != NULL && (s->flags & SEC_READONLY) != 0)
533 	    {
534 	      info->callbacks->einfo
535 		/* xgettext:c-format */
536 		(_("%F%P: %pB: copy relocation against non-copyable "
537 		   "protected symbol `%s' in %pB\n"),
538 		 p->sec->owner, h->root.root.string,
539 		 h->root.u.def.section->owner);
540 	      return false;
541 	    }
542 	}
543 
544       sreloc = elf_section_data (p->sec)->sreloc;
545 
546       BFD_ASSERT (sreloc != NULL);
547       sreloc->size += p->count * htab->sizeof_reloc;
548     }
549 
550   return true;
551 }
552 
553 /* Allocate space in .plt, .got and associated reloc sections for
554    local dynamic relocs.  */
555 
556 static int
557 elf_x86_allocate_local_dynreloc (void **slot, void *inf)
558 {
559   struct elf_link_hash_entry *h
560     = (struct elf_link_hash_entry *) *slot;
561 
562   if (h->type != STT_GNU_IFUNC
563       || !h->def_regular
564       || !h->ref_regular
565       || !h->forced_local
566       || h->root.type != bfd_link_hash_defined)
567     abort ();
568 
569   return elf_x86_allocate_dynrelocs (h, inf);
570 }
571 
572 /* Find and/or create a hash entry for local symbol.  */
573 
574 struct elf_link_hash_entry *
575 _bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table *htab,
576 				 bfd *abfd, const Elf_Internal_Rela *rel,
577 				 bool create)
578 {
579   struct elf_x86_link_hash_entry e, *ret;
580   asection *sec = abfd->sections;
581   hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
582 				       htab->r_sym (rel->r_info));
583   void **slot;
584 
585   e.elf.indx = sec->id;
586   e.elf.dynstr_index = htab->r_sym (rel->r_info);
587   slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
588 				   create ? INSERT : NO_INSERT);
589 
590   if (!slot)
591     return NULL;
592 
593   if (*slot)
594     {
595       ret = (struct elf_x86_link_hash_entry *) *slot;
596       return &ret->elf;
597     }
598 
599   ret = (struct elf_x86_link_hash_entry *)
600 	objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
601 			sizeof (struct elf_x86_link_hash_entry));
602   if (ret)
603     {
604       memset (ret, 0, sizeof (*ret));
605       ret->elf.indx = sec->id;
606       ret->elf.dynstr_index = htab->r_sym (rel->r_info);
607       ret->elf.dynindx = -1;
608       ret->plt_got.offset = (bfd_vma) -1;
609       *slot = ret;
610     }
611   return &ret->elf;
612 }
613 
614 /* Create an entry in a x86 ELF linker hash table.  NB: THIS MUST BE IN
615    SYNC WITH _bfd_elf_link_hash_newfunc.  */
616 
617 struct bfd_hash_entry *
618 _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
619 				struct bfd_hash_table *table,
620 				const char *string)
621 {
622   /* Allocate the structure if it has not already been allocated by a
623      subclass.  */
624   if (entry == NULL)
625     {
626       entry = (struct bfd_hash_entry *)
627 	bfd_hash_allocate (table,
628 			   sizeof (struct elf_x86_link_hash_entry));
629       if (entry == NULL)
630 	return entry;
631     }
632 
633   /* Call the allocation method of the superclass.  */
634   entry = _bfd_link_hash_newfunc (entry, table, string);
635   if (entry != NULL)
636     {
637       struct elf_x86_link_hash_entry *eh
638        = (struct elf_x86_link_hash_entry *) entry;
639       struct elf_link_hash_table *htab
640 	= (struct elf_link_hash_table *) table;
641 
642       memset (&eh->elf.size, 0,
643 	      (sizeof (struct elf_x86_link_hash_entry)
644 	       - offsetof (struct elf_link_hash_entry, size)));
645       /* Set local fields.  */
646       eh->elf.indx = -1;
647       eh->elf.dynindx = -1;
648       eh->elf.got = htab->init_got_refcount;
649       eh->elf.plt = htab->init_plt_refcount;
650       /* Assume that we have been called by a non-ELF symbol reader.
651 	 This flag is then reset by the code which reads an ELF input
652 	 file.  This ensures that a symbol created by a non-ELF symbol
653 	 reader will have the flag set correctly.  */
654       eh->elf.non_elf = 1;
655       eh->plt_second.offset = (bfd_vma) -1;
656       eh->plt_got.offset = (bfd_vma) -1;
657       eh->tlsdesc_got = (bfd_vma) -1;
658       eh->zero_undefweak = 1;
659     }
660 
661   return entry;
662 }
663 
664 /* Compute a hash of a local hash entry.  We use elf_link_hash_entry
665   for local symbol so that we can handle local STT_GNU_IFUNC symbols
666   as global symbol.  We reuse indx and dynstr_index for local symbol
667   hash since they aren't used by global symbols in this backend.  */
668 
669 hashval_t
670 _bfd_x86_elf_local_htab_hash (const void *ptr)
671 {
672   struct elf_link_hash_entry *h
673     = (struct elf_link_hash_entry *) ptr;
674   return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
675 }
676 
677 /* Compare local hash entries.  */
678 
679 int
680 _bfd_x86_elf_local_htab_eq (const void *ptr1, const void *ptr2)
681 {
682   struct elf_link_hash_entry *h1
683      = (struct elf_link_hash_entry *) ptr1;
684   struct elf_link_hash_entry *h2
685     = (struct elf_link_hash_entry *) ptr2;
686 
687   return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
688 }
689 
690 /* Destroy an x86 ELF linker hash table.  */
691 
692 static void
693 elf_x86_link_hash_table_free (bfd *obfd)
694 {
695   struct elf_x86_link_hash_table *htab
696     = (struct elf_x86_link_hash_table *) obfd->link.hash;
697 
698   if (htab->loc_hash_table)
699     htab_delete (htab->loc_hash_table);
700   if (htab->loc_hash_memory)
701     objalloc_free ((struct objalloc *) htab->loc_hash_memory);
702   _bfd_elf_link_hash_table_free (obfd);
703 }
704 
705 static bool
706 elf_i386_is_reloc_section (const char *secname)
707 {
708   return startswith (secname, ".rel");
709 }
710 
711 static bool
712 elf_x86_64_is_reloc_section (const char *secname)
713 {
714   return startswith (secname, ".rela");
715 }
716 
717 /* Create an x86 ELF linker hash table.  */
718 
719 struct bfd_link_hash_table *
720 _bfd_x86_elf_link_hash_table_create (bfd *abfd)
721 {
722   struct elf_x86_link_hash_table *ret;
723   const struct elf_backend_data *bed;
724   size_t amt = sizeof (struct elf_x86_link_hash_table);
725 
726   ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt);
727   if (ret == NULL)
728     return NULL;
729 
730   bed = get_elf_backend_data (abfd);
731   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
732 				      _bfd_x86_elf_link_hash_newfunc,
733 				      sizeof (struct elf_x86_link_hash_entry),
734 				      bed->target_id))
735     {
736       free (ret);
737       return NULL;
738     }
739 
740   if (bed->target_id == X86_64_ELF_DATA)
741     {
742       ret->is_reloc_section = elf_x86_64_is_reloc_section;
743       ret->got_entry_size = 8;
744       ret->pcrel_plt = true;
745       ret->tls_get_addr = "__tls_get_addr";
746       ret->relative_r_type = R_X86_64_RELATIVE;
747       ret->relative_r_name = "R_X86_64_RELATIVE";
748       ret->elf_append_reloc = elf_append_rela;
749       ret->elf_write_addend_in_got = _bfd_elf64_write_addend;
750     }
751   if (ABI_64_P (abfd))
752     {
753       ret->sizeof_reloc = sizeof (Elf64_External_Rela);
754       ret->pointer_r_type = R_X86_64_64;
755       ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
756       ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
757       ret->elf_write_addend = _bfd_elf64_write_addend;
758     }
759   else
760     {
761       if (bed->target_id == X86_64_ELF_DATA)
762 	{
763 	  ret->sizeof_reloc = sizeof (Elf32_External_Rela);
764 	  ret->pointer_r_type = R_X86_64_32;
765 	  ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER;
766 	  ret->dynamic_interpreter_size
767 	    = sizeof ELFX32_DYNAMIC_INTERPRETER;
768 	  ret->elf_write_addend = _bfd_elf32_write_addend;
769 	}
770       else
771 	{
772 	  ret->is_reloc_section = elf_i386_is_reloc_section;
773 	  ret->sizeof_reloc = sizeof (Elf32_External_Rel);
774 	  ret->got_entry_size = 4;
775 	  ret->pcrel_plt = false;
776 	  ret->pointer_r_type = R_386_32;
777 	  ret->relative_r_type = R_386_RELATIVE;
778 	  ret->relative_r_name = "R_386_RELATIVE";
779 	  ret->elf_append_reloc = elf_append_rel;
780 	  ret->elf_write_addend = _bfd_elf32_write_addend;
781 	  ret->elf_write_addend_in_got = _bfd_elf32_write_addend;
782 	  ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
783 	  ret->dynamic_interpreter_size
784 	    = sizeof ELF32_DYNAMIC_INTERPRETER;
785 	  ret->tls_get_addr = "___tls_get_addr";
786 	}
787     }
788 
789   ret->loc_hash_table = htab_try_create (1024,
790 					 _bfd_x86_elf_local_htab_hash,
791 					 _bfd_x86_elf_local_htab_eq,
792 					 NULL);
793   ret->loc_hash_memory = objalloc_create ();
794   if (!ret->loc_hash_table || !ret->loc_hash_memory)
795     {
796       elf_x86_link_hash_table_free (abfd);
797       return NULL;
798     }
799   ret->elf.root.hash_table_free = elf_x86_link_hash_table_free;
800 
801   return &ret->elf.root;
802 }
803 
804 /* Sort relocs into address order.  */
805 
806 int
807 _bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
808 {
809   const arelent *a = * (const arelent **) ap;
810   const arelent *b = * (const arelent **) bp;
811 
812   if (a->address > b->address)
813     return 1;
814   else if (a->address < b->address)
815     return -1;
816   else
817     return 0;
818 }
819 
820 /* Mark symbol, NAME, as locally defined by linker if it is referenced
821    and not defined in a relocatable object file.  */
822 
823 static void
824 elf_x86_linker_defined (struct bfd_link_info *info, const char *name)
825 {
826   struct elf_link_hash_entry *h;
827 
828   h = elf_link_hash_lookup (elf_hash_table (info), name,
829 			    false, false, false);
830   if (h == NULL)
831     return;
832 
833   while (h->root.type == bfd_link_hash_indirect)
834     h = (struct elf_link_hash_entry *) h->root.u.i.link;
835 
836   if (h->root.type == bfd_link_hash_new
837       || h->root.type == bfd_link_hash_undefined
838       || h->root.type == bfd_link_hash_undefweak
839       || h->root.type == bfd_link_hash_common
840       || (!h->def_regular && h->def_dynamic))
841     {
842       elf_x86_hash_entry (h)->local_ref = 2;
843       elf_x86_hash_entry (h)->linker_def = 1;
844     }
845 }
846 
847 /* Hide a linker-defined symbol, NAME, with hidden visibility.  */
848 
849 static void
850 elf_x86_hide_linker_defined (struct bfd_link_info *info,
851 			     const char *name)
852 {
853   struct elf_link_hash_entry *h;
854 
855   h = elf_link_hash_lookup (elf_hash_table (info), name,
856 			    false, false, false);
857   if (h == NULL)
858     return;
859 
860   while (h->root.type == bfd_link_hash_indirect)
861     h = (struct elf_link_hash_entry *) h->root.u.i.link;
862 
863   if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
864       || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
865     _bfd_elf_link_hash_hide_symbol (info, h, true);
866 }
867 
868 bool
869 _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
870 {
871   if (!bfd_link_relocatable (info))
872     {
873       /* Check for __tls_get_addr reference.  */
874       struct elf_x86_link_hash_table *htab;
875       const struct elf_backend_data *bed = get_elf_backend_data (abfd);
876       htab = elf_x86_hash_table (info, bed->target_id);
877       if (htab)
878 	{
879 	  struct elf_link_hash_entry *h;
880 
881 	  h = elf_link_hash_lookup (elf_hash_table (info),
882 				    htab->tls_get_addr,
883 				    false, false, false);
884 	  if (h != NULL)
885 	    {
886 	      elf_x86_hash_entry (h)->tls_get_addr = 1;
887 
888 	      /* Check the versioned __tls_get_addr symbol.  */
889 	      while (h->root.type == bfd_link_hash_indirect)
890 		{
891 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
892 		  elf_x86_hash_entry (h)->tls_get_addr = 1;
893 		}
894 	    }
895 
896 	  /* "__ehdr_start" will be defined by linker as a hidden symbol
897 	     later if it is referenced and not defined.  */
898 	  elf_x86_linker_defined (info, "__ehdr_start");
899 
900 	  if (bfd_link_executable (info))
901 	    {
902 	      /* References to __bss_start, _end and _edata should be
903 		 locally resolved within executables.  */
904 	      elf_x86_linker_defined (info, "__bss_start");
905 	      elf_x86_linker_defined (info, "_end");
906 	      elf_x86_linker_defined (info, "_edata");
907 	    }
908 	  else
909 	    {
910 	      /* Hide hidden __bss_start, _end and _edata in shared
911 		 libraries.  */
912 	      elf_x86_hide_linker_defined (info, "__bss_start");
913 	      elf_x86_hide_linker_defined (info, "_end");
914 	      elf_x86_hide_linker_defined (info, "_edata");
915 	    }
916 	}
917     }
918 
919   /* Invoke the regular ELF backend linker to do all the work.  */
920   return _bfd_elf_link_check_relocs (abfd, info);
921 }
922 
923 /* Look through the relocs for a section before allocation to make the
924    dynamic reloc section.  */
925 
926 bool
927 _bfd_x86_elf_check_relocs (bfd *abfd,
928 			   struct bfd_link_info *info,
929 			   asection *sec,
930 			   const Elf_Internal_Rela *relocs)
931 {
932   struct elf_x86_link_hash_table *htab;
933   Elf_Internal_Shdr *symtab_hdr;
934   struct elf_link_hash_entry **sym_hashes;
935   const Elf_Internal_Rela *rel;
936   const Elf_Internal_Rela *rel_end;
937   asection *sreloc;
938   const struct elf_backend_data *bed;
939   bool is_x86_64;
940 
941   if (bfd_link_relocatable (info))
942     return true;
943 
944   bed = get_elf_backend_data (abfd);
945   htab = elf_x86_hash_table (info, bed->target_id);
946   if (htab == NULL)
947     {
948       sec->check_relocs_failed = 1;
949       return false;
950     }
951 
952   is_x86_64 = bed->target_id == X86_64_ELF_DATA;
953 
954   symtab_hdr = &elf_symtab_hdr (abfd);
955   sym_hashes = elf_sym_hashes (abfd);
956 
957   rel_end = relocs + sec->reloc_count;
958   for (rel = relocs; rel < rel_end; rel++)
959     {
960       unsigned int r_type;
961       unsigned int r_symndx;
962       struct elf_link_hash_entry *h;
963 
964       r_symndx = htab->r_sym (rel->r_info);
965       r_type = ELF32_R_TYPE (rel->r_info);
966 
967       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
968 	{
969 	  /* xgettext:c-format */
970 	  _bfd_error_handler (_("%pB: bad symbol index: %d"),
971 			      abfd, r_symndx);
972 	  goto error_return;
973 	}
974 
975       if (r_symndx < symtab_hdr->sh_info)
976 	h = NULL;
977       else
978 	{
979 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
980 	  while (h->root.type == bfd_link_hash_indirect
981 		 || h->root.type == bfd_link_hash_warning)
982 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
983 	}
984 
985       if (X86_NEED_DYNAMIC_RELOC_TYPE_P (is_x86_64, r_type)
986 	  && NEED_DYNAMIC_RELOCATION_P (is_x86_64, info, true, h, sec,
987 					r_type, htab->pointer_r_type))
988 	{
989 	  /* We may copy these reloc types into the output file.
990 	     Create a reloc section in dynobj and make room for
991 	     this reloc.  */
992 	  sreloc = _bfd_elf_make_dynamic_reloc_section
993 	    (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
994 	     abfd, sec->use_rela_p);
995 
996 	  if (sreloc != NULL)
997 	    return true;
998 
999   error_return:
1000 	  sec->check_relocs_failed = 1;
1001 	  return false;
1002 	}
1003     }
1004 
1005   return true;
1006 }
1007 
1008 /* Add an entry to the relative reloc record.  */
1009 
1010 static bool
1011 elf_x86_relative_reloc_record_add
1012   (struct bfd_link_info *info,
1013    struct elf_x86_relative_reloc_data *relative_reloc,
1014    Elf_Internal_Rela *rel, asection *sec,
1015    asection *sym_sec, struct elf_link_hash_entry *h,
1016    Elf_Internal_Sym *sym, bfd_vma offset, bool *keep_symbuf_p)
1017 {
1018   bfd_size_type newidx;
1019 
1020   if (relative_reloc->data == NULL)
1021     {
1022       relative_reloc->data = bfd_malloc
1023 	(sizeof (struct elf_x86_relative_reloc_record));
1024       relative_reloc->count = 0;
1025       relative_reloc->size = 1;
1026     }
1027 
1028   newidx = relative_reloc->count++;
1029 
1030   if (relative_reloc->count > relative_reloc->size)
1031     {
1032       relative_reloc->size <<= 1;
1033       relative_reloc->data = bfd_realloc
1034 	(relative_reloc->data,
1035 	 (relative_reloc->size
1036 	  * sizeof (struct elf_x86_relative_reloc_record)));
1037     }
1038 
1039   if (relative_reloc->data == NULL)
1040     {
1041       info->callbacks->einfo
1042 	/* xgettext:c-format */
1043 	(_("%F%P: %pB: failed to allocate relative reloc record\n"),
1044 	 info->output_bfd);
1045       return false;
1046     }
1047 
1048   relative_reloc->data[newidx].rel = *rel;
1049   relative_reloc->data[newidx].sec = sec;
1050   if (h != NULL)
1051     {
1052       /* Set SYM to NULL to indicate a global symbol.  */
1053       relative_reloc->data[newidx].sym = NULL;
1054       relative_reloc->data[newidx].u.h = h;
1055     }
1056   else
1057     {
1058       relative_reloc->data[newidx].sym = sym;
1059       relative_reloc->data[newidx].u.sym_sec = sym_sec;
1060       /* We must keep the symbol buffer since SYM will be used later.  */
1061       *keep_symbuf_p = true;
1062     }
1063   relative_reloc->data[newidx].offset = offset;
1064   relative_reloc->data[newidx].address = 0;
1065   return true;
1066 }
1067 
1068 /* After input sections have been mapped to output sections and
1069    addresses of output sections are set initiallly, scan input
1070    relocations with the same logic in relocate_section to determine
1071    if a relative relocation should be generated.  Save the relative
1072    relocation candidate information for sizing the DT_RELR section
1073    later after all symbols addresses can be determined.  */
1074 
1075 bool
1076 _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
1077 				 asection *input_section,
1078 				 struct bfd_link_info *info,
1079 				 bool *again)
1080 {
1081   Elf_Internal_Shdr *symtab_hdr;
1082   Elf_Internal_Rela *internal_relocs;
1083   Elf_Internal_Rela *irel, *irelend;
1084   Elf_Internal_Sym *isymbuf = NULL;
1085   struct elf_link_hash_entry **sym_hashes;
1086   const struct elf_backend_data *bed;
1087   struct elf_x86_link_hash_table *htab;
1088   bfd_vma *local_got_offsets;
1089   bool is_x86_64;
1090   bool unaligned_section;
1091   bool return_status = false;
1092   bool keep_symbuf = false;
1093 
1094   if (bfd_link_relocatable (info))
1095     return true;
1096 
1097   /* Assume we're not going to change any sizes, and we'll only need
1098      one pass.  */
1099   *again = false;
1100 
1101   bed = get_elf_backend_data (abfd);
1102   htab = elf_x86_hash_table (info, bed->target_id);
1103   if (htab == NULL)
1104     return true;
1105 
1106   /* Nothing to do if there are no relocations or relative relocations
1107      have been packed.  */
1108   if (input_section == htab->elf.srelrdyn
1109       || input_section->relative_reloc_packed
1110       || ((input_section->flags & (SEC_RELOC | SEC_ALLOC))
1111 	  != (SEC_RELOC | SEC_ALLOC))
1112       || (input_section->flags & SEC_DEBUGGING) != 0
1113       || input_section->reloc_count == 0)
1114     return true;
1115 
1116   /* Skip if the section isn't aligned.  */
1117   unaligned_section = input_section->alignment_power == 0;
1118 
1119   is_x86_64 = bed->target_id == X86_64_ELF_DATA;
1120 
1121   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1122   sym_hashes = elf_sym_hashes (abfd);
1123   local_got_offsets = elf_local_got_offsets (abfd);
1124 
1125   /* Load the relocations for this section.  */
1126   internal_relocs =
1127     _bfd_elf_link_info_read_relocs (abfd, info, input_section, NULL,
1128 				    (Elf_Internal_Rela *) NULL,
1129 				    info->keep_memory);
1130   if (internal_relocs == NULL)
1131     return false;
1132 
1133   irelend = internal_relocs + input_section->reloc_count;
1134   for (irel = internal_relocs; irel < irelend; irel++)
1135     {
1136       unsigned int r_type;
1137       unsigned int r_symndx;
1138       Elf_Internal_Sym *isym;
1139       struct elf_link_hash_entry *h;
1140       struct elf_x86_link_hash_entry *eh;
1141       bfd_vma offset;
1142       bool resolved_to_zero;
1143       bool need_copy_reloc_in_pie;
1144       bool pc32_reloc;
1145       asection *sec;
1146       /* Offset must be a multiple of 2.  */
1147       bool unaligned_offset = (irel->r_offset & 1) != 0;
1148       /* True if there is a relative relocation against a dynamic
1149 	 symbol.  */
1150       bool dynamic_relative_reloc_p;
1151 
1152       /* Get the value of the symbol referred to by the reloc.  */
1153       r_symndx = htab->r_sym (irel->r_info);
1154 
1155       r_type = ELF32_R_TYPE (irel->r_info);
1156       /* Clear the R_X86_64_converted_reloc_bit bit.  */
1157       r_type &= ~R_X86_64_converted_reloc_bit;
1158 
1159       sec = NULL;
1160       h = NULL;
1161       dynamic_relative_reloc_p = false;
1162 
1163       if (r_symndx < symtab_hdr->sh_info)
1164 	{
1165 	  /* Read this BFD's local symbols.  */
1166 	  if (isymbuf == NULL)
1167 	    {
1168 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1169 	      if (isymbuf == NULL)
1170 		{
1171 		  isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1172 						  symtab_hdr->sh_info,
1173 						  0, NULL, NULL, NULL);
1174 		  if (isymbuf == NULL)
1175 		    goto error_return;
1176 		}
1177 	    }
1178 
1179 	  isym = isymbuf + r_symndx;
1180 	  switch (isym->st_shndx)
1181 	    {
1182 	    case SHN_ABS:
1183 	      sec = bfd_abs_section_ptr;
1184 	      break;
1185 	    case SHN_COMMON:
1186 	      sec = bfd_com_section_ptr;
1187 	      break;
1188 	    case SHN_X86_64_LCOMMON:
1189 	      if (!is_x86_64)
1190 		abort ();
1191 	      sec = &_bfd_elf_large_com_section;
1192 	      break;
1193 	    default:
1194 	      sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1195 	      break;
1196 	    }
1197 
1198 	  /* Skip relocation against local STT_GNU_IFUNC symbol.  */
1199 	  if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1200 	    continue;
1201 
1202 	  eh = (struct elf_x86_link_hash_entry *) h;
1203 	  resolved_to_zero = false;
1204 	}
1205       else
1206 	{
1207 	  /* Get H and SEC for GENERATE_DYNAMIC_RELOCATION_P below.  */
1208 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1209 	  while (h->root.type == bfd_link_hash_indirect
1210 		 || h->root.type == bfd_link_hash_warning)
1211 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1212 
1213 	  if (h->root.type == bfd_link_hash_defined
1214 	      || h->root.type == bfd_link_hash_defweak)
1215 	    sec = h->root.u.def.section;
1216 
1217 	  /* Skip relocation against STT_GNU_IFUNC symbol.  */
1218 	  if (h->type == STT_GNU_IFUNC)
1219 	    continue;
1220 
1221 	  eh = (struct elf_x86_link_hash_entry *) h;
1222 	  resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
1223 
1224 	  /* NB: See how elf_backend_finish_dynamic_symbol is called
1225 	     from elf_link_output_extsym.  */
1226 	  if ((h->dynindx != -1 || h->forced_local)
1227 	      && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1228 		   || h->root.type != bfd_link_hash_undefweak)
1229 		  || !h->forced_local)
1230 	      && h->got.offset != (bfd_vma) -1
1231 	      && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
1232 	      && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
1233 	      && !resolved_to_zero
1234 	      && SYMBOL_REFERENCES_LOCAL_P (info, h)
1235 	      && SYMBOL_DEFINED_NON_SHARED_P (h))
1236 	    dynamic_relative_reloc_p = true;
1237 
1238 	  isym = NULL;
1239 	}
1240 
1241       if (X86_GOT_TYPE_P (is_x86_64, r_type))
1242 	{
1243 	  /* Pack GOT relative relocations.  There should be only a
1244 	     single R_*_RELATIVE relocation in GOT.  */
1245 	  if (eh != NULL)
1246 	    {
1247 	      if (eh->got_relative_reloc_done)
1248 		continue;
1249 
1250 	      if (!(dynamic_relative_reloc_p
1251 		    || (RESOLVED_LOCALLY_P (info, h, htab)
1252 			&& GENERATE_RELATIVE_RELOC_P (info, h))))
1253 		continue;
1254 
1255 	      if (!dynamic_relative_reloc_p)
1256 		eh->no_finish_dynamic_symbol = 1;
1257 	      eh->got_relative_reloc_done = 1;
1258 	      offset = h->got.offset;
1259 	    }
1260 	  else
1261 	    {
1262 	      if (elf_x86_relative_reloc_done (abfd)[r_symndx])
1263 		continue;
1264 
1265 	      if (!X86_LOCAL_GOT_RELATIVE_RELOC_P (is_x86_64, info,
1266 						   isym))
1267 		continue;
1268 
1269 	      elf_x86_relative_reloc_done (abfd)[r_symndx] = 1;
1270 	      offset = local_got_offsets[r_symndx];
1271 	    }
1272 
1273 	  if (!elf_x86_relative_reloc_record_add (info,
1274 						  &htab->relative_reloc,
1275 						  irel, htab->elf.sgot,
1276 						  sec, h, isym, offset,
1277 						  &keep_symbuf))
1278 	    goto error_return;
1279 
1280 	  continue;
1281 	}
1282 
1283       if (is_x86_64
1284 	  && irel->r_addend == 0
1285 	  && !ABI_64_P (info->output_bfd))
1286 	{
1287 	  /* For x32, if addend is zero, treat R_X86_64_64 like
1288 	     R_X86_64_32 and R_X86_64_SIZE64 like R_X86_64_SIZE32.  */
1289 	  if (r_type == R_X86_64_64)
1290 	    r_type = R_X86_64_32;
1291 	  else if (r_type == R_X86_64_SIZE64)
1292 	    r_type = R_X86_64_SIZE32;
1293 	}
1294 
1295       if (!X86_RELATIVE_RELOC_TYPE_P (is_x86_64, r_type))
1296 	continue;
1297 
1298       /* Pack non-GOT relative relocations.  */
1299       if (is_x86_64)
1300 	{
1301 	  need_copy_reloc_in_pie =
1302 	    (bfd_link_pie (info)
1303 	     && h != NULL
1304 	     && (h->needs_copy
1305 		 || eh->needs_copy
1306 		 || (h->root.type == bfd_link_hash_undefined))
1307 	     && (X86_PCREL_TYPE_P (true, r_type)
1308 		 || X86_SIZE_TYPE_P (true, r_type)));
1309 	  pc32_reloc = false;
1310 	}
1311       else
1312 	{
1313 	  need_copy_reloc_in_pie = false;
1314 	  pc32_reloc = r_type == R_386_PC32;
1315 	}
1316 
1317       if (GENERATE_DYNAMIC_RELOCATION_P (is_x86_64, info, eh, r_type,
1318 					 sec, need_copy_reloc_in_pie,
1319 					 resolved_to_zero, pc32_reloc))
1320 	{
1321 	  /* When generating a shared object, these relocations
1322 	     are copied into the output file to be resolved at run
1323 	     time.	*/
1324 	  offset = _bfd_elf_section_offset (info->output_bfd, info,
1325 					    input_section,
1326 					    irel->r_offset);
1327 	  if (offset == (bfd_vma) -1
1328 	      || offset == (bfd_vma) -2
1329 	      || COPY_INPUT_RELOC_P (is_x86_64, info, h, r_type))
1330 	    continue;
1331 
1332 	  /* This symbol is local, or marked to become local.  When
1333 	     relocation overflow check is disabled, we convert
1334 	     R_X86_64_32 to dynamic R_X86_64_RELATIVE.  */
1335 	  if (is_x86_64
1336 	      && !(r_type == htab->pointer_r_type
1337 		   || (r_type == R_X86_64_32
1338 		       && htab->params->no_reloc_overflow_check)))
1339 	    continue;
1340 
1341 	  if (!elf_x86_relative_reloc_record_add
1342 	        (info,
1343 		 ((unaligned_section || unaligned_offset)
1344 		  ? &htab->unaligned_relative_reloc
1345 		  : &htab->relative_reloc),
1346 		 irel, input_section, sec, h, isym, offset,
1347 		 &keep_symbuf))
1348 	    goto error_return;
1349 	}
1350     }
1351 
1352   input_section->relative_reloc_packed = 1;
1353 
1354   return_status = true;
1355 
1356 error_return:
1357   if ((unsigned char *) isymbuf != symtab_hdr->contents)
1358     {
1359       /* Cache the symbol buffer if it must be kept.  */
1360       if (keep_symbuf)
1361 	symtab_hdr->contents = (unsigned char *) isymbuf;
1362       else
1363 	free (isymbuf);
1364     }
1365   if (elf_section_data (input_section)->relocs != internal_relocs)
1366     free (internal_relocs);
1367   return return_status;
1368 }
1369 
1370 /* Add an entry to the 64-bit DT_RELR bitmap.  */
1371 
1372 static void
1373 elf64_dt_relr_bitmap_add
1374   (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
1375    uint64_t entry)
1376 {
1377   bfd_size_type newidx;
1378 
1379   if (bitmap->u.elf64 == NULL)
1380     {
1381       bitmap->u.elf64 = bfd_malloc (sizeof (uint64_t));
1382       bitmap->count = 0;
1383       bitmap->size = 1;
1384     }
1385 
1386   newidx = bitmap->count++;
1387 
1388   if (bitmap->count > bitmap->size)
1389     {
1390       bitmap->size <<= 1;
1391       bitmap->u.elf64 = bfd_realloc (bitmap->u.elf64,
1392 				     (bitmap->size * sizeof (uint64_t)));
1393     }
1394 
1395   if (bitmap->u.elf64 == NULL)
1396     {
1397       info->callbacks->einfo
1398 	/* xgettext:c-format */
1399 	(_("%F%P: %pB: failed to allocate 64-bit DT_RELR bitmap\n"),
1400 	 info->output_bfd);
1401     }
1402 
1403   bitmap->u.elf64[newidx] = entry;
1404 }
1405 
1406 /* Add an entry to the 32-bit DT_RELR bitmap.  */
1407 
1408 static void
1409 elf32_dt_relr_bitmap_add
1410   (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
1411    uint32_t entry)
1412 {
1413   bfd_size_type newidx;
1414 
1415   if (bitmap->u.elf32 == NULL)
1416     {
1417       bitmap->u.elf32 = bfd_malloc (sizeof (uint32_t));
1418       bitmap->count = 0;
1419       bitmap->size = 1;
1420     }
1421 
1422   newidx = bitmap->count++;
1423 
1424   if (bitmap->count > bitmap->size)
1425     {
1426       bitmap->size <<= 1;
1427       bitmap->u.elf32 = bfd_realloc (bitmap->u.elf32,
1428 				     (bitmap->size * sizeof (uint32_t)));
1429     }
1430 
1431   if (bitmap->u.elf32 == NULL)
1432     {
1433       info->callbacks->einfo
1434 	/* xgettext:c-format */
1435 	(_("%F%P: %pB: failed to allocate 32-bit DT_RELR bitmap\n"),
1436 	 info->output_bfd);
1437     }
1438 
1439   bitmap->u.elf32[newidx] = entry;
1440 }
1441 
1442 void
1443 _bfd_elf32_write_addend (bfd *abfd, uint64_t value, void *addr)
1444 {
1445   bfd_put_32 (abfd, value, addr);
1446 }
1447 
1448 void
1449 _bfd_elf64_write_addend (bfd *abfd, uint64_t value, void *addr)
1450 {
1451   bfd_put_64 (abfd, value, addr);
1452 }
1453 
1454 /* Size or finish relative relocations to determine the run-time
1455    addresses for DT_RELR bitmap computation later.  OUTREL is set
1456    to NULL in the sizing phase and non-NULL in the finising phase
1457    where the regular relative relocations will be written out.  */
1458 
1459 static void
1460 elf_x86_size_or_finish_relative_reloc
1461   (bool is_x86_64, struct bfd_link_info *info,
1462    struct elf_x86_link_hash_table *htab, bool unaligned,
1463    Elf_Internal_Rela *outrel)
1464 {
1465   unsigned int align_mask;
1466   bfd_size_type i, count;
1467   asection *sec, *srel;
1468   struct elf_link_hash_entry *h;
1469   bfd_vma offset;
1470   Elf_Internal_Sym *sym;
1471   asection *sym_sec;
1472   asection *sgot = htab->elf.sgot;
1473   asection *srelgot = htab->elf.srelgot;
1474   struct elf_x86_relative_reloc_data *relative_reloc;
1475 
1476   if (unaligned)
1477     {
1478       align_mask = 0;
1479       relative_reloc = &htab->unaligned_relative_reloc;
1480     }
1481   else
1482     {
1483       align_mask = 1;
1484       relative_reloc = &htab->relative_reloc;
1485     }
1486 
1487   count = relative_reloc->count;
1488   for (i = 0; i < count; i++)
1489     {
1490       sec = relative_reloc->data[i].sec;
1491       sym = relative_reloc->data[i].sym;
1492 
1493       /* If SYM is NULL, it must be a global symbol.  */
1494       if (sym == NULL)
1495 	h = relative_reloc->data[i].u.h;
1496       else
1497 	h = NULL;
1498 
1499       if (is_x86_64)
1500 	{
1501 	  bfd_vma relocation;
1502 	  /* This function may be called more than once and REL may be
1503 	     updated by _bfd_elf_rela_local_sym below.  */
1504 	  Elf_Internal_Rela rel = relative_reloc->data[i].rel;
1505 
1506 	  if (h != NULL)
1507 	    {
1508 	      if (h->root.type == bfd_link_hash_defined
1509 		  || h->root.type == bfd_link_hash_defweak)
1510 		{
1511 		  sym_sec = h->root.u.def.section;
1512 		  relocation = (h->root.u.def.value
1513 				+ sym_sec->output_section->vma
1514 				+ sym_sec->output_offset);
1515 		}
1516 	      else
1517 		{
1518 		  /* Allow undefined symbol only at the sizing phase.
1519 		     Otherwise skip undefined symbol here.  Undefined
1520 		     symbol will be reported by relocate_section.  */
1521 		  if (outrel == NULL)
1522 		    relocation = 0;
1523 		  else
1524 		    continue;
1525 		}
1526 	    }
1527 	  else
1528 	    {
1529 	      sym_sec = relative_reloc->data[i].u.sym_sec;
1530 	      relocation = _bfd_elf_rela_local_sym
1531 		(info->output_bfd, sym, &sym_sec, &rel);
1532 	    }
1533 
1534 	  if (outrel != NULL)
1535 	    {
1536 	      outrel->r_addend = relocation;
1537 	      if (sec == sgot)
1538 		{
1539 		  if (h != NULL && h->needs_plt)
1540 		    abort ();
1541 		}
1542 	      else
1543 		outrel->r_addend += rel.r_addend;
1544 
1545 	      /* Write the implicit addend if ALIGN_MASK isn't 0.  */
1546 	      if (align_mask)
1547 		{
1548 		  if (sec == sgot)
1549 		    {
1550 		      if (relative_reloc->data[i].offset >= sec->size)
1551 			abort ();
1552 		      htab->elf_write_addend_in_got
1553 			(info->output_bfd, outrel->r_addend,
1554 			 sec->contents + relative_reloc->data[i].offset);
1555 		    }
1556 		  else
1557 		    {
1558 		      bfd_byte *contents;
1559 
1560 		      if (rel.r_offset >= sec->size)
1561 			abort ();
1562 
1563 		      if (elf_section_data (sec)->this_hdr.contents
1564 			  != NULL)
1565 			contents
1566 			  = elf_section_data (sec)->this_hdr.contents;
1567 		      else
1568 			{
1569 			  if (!bfd_malloc_and_get_section (sec->owner,
1570 							   sec,
1571 							   &contents))
1572 			    info->callbacks->einfo
1573 			      /* xgettext:c-format */
1574 			      (_("%F%P: %pB: failed to allocate memory for section `%pA'\n"),
1575 			       info->output_bfd, sec);
1576 
1577 			  /* Cache the section contents for
1578 			     elf_link_input_bfd.  */
1579 			  elf_section_data (sec)->this_hdr.contents
1580 			    = contents;
1581 			}
1582 		      htab->elf_write_addend
1583 			(info->output_bfd, outrel->r_addend,
1584 			 contents + rel.r_offset);
1585 		    }
1586 		}
1587 	    }
1588 	}
1589 
1590       if (sec == sgot)
1591 	srel = srelgot;
1592       else
1593 	srel = elf_section_data (sec)->sreloc;
1594       offset = (sec->output_section->vma + sec->output_offset
1595 		+ relative_reloc->data[i].offset);
1596       relative_reloc->data[i].address = offset;
1597       if (outrel != NULL)
1598 	{
1599 	  outrel->r_offset = offset;
1600 
1601 	  if ((outrel->r_offset & align_mask) != 0)
1602 	    abort ();
1603 
1604 	  if (htab->params->report_relative_reloc)
1605 	    _bfd_x86_elf_link_report_relative_reloc
1606 	      (info, sec, h, sym, htab->relative_r_name, outrel);
1607 
1608 	  /* Generate regular relative relocation if ALIGN_MASK is 0.  */
1609 	  if (align_mask == 0)
1610 	    htab->elf_append_reloc (info->output_bfd, srel, outrel);
1611 	}
1612     }
1613 }
1614 
1615 /* Compute the DT_RELR section size.  Set NEED_PLAYOUT to true if
1616    the DT_RELR section size has been increased.  */
1617 
1618 static void
1619 elf_x86_compute_dl_relr_bitmap
1620   (struct bfd_link_info *info, struct elf_x86_link_hash_table *htab,
1621    bool *need_layout)
1622 {
1623   bfd_vma base;
1624   bfd_size_type i, count, new_count;
1625   struct elf_x86_relative_reloc_data *relative_reloc =
1626     &htab->relative_reloc;
1627   /* Save the old DT_RELR bitmap count.  Don't shrink the DT_RELR bitmap
1628      if the new DT_RELR bitmap count is smaller than the old one.  Pad
1629      with trailing 1s which won't be decoded to more relocations.  */
1630   bfd_size_type dt_relr_bitmap_count = htab->dt_relr_bitmap.count;
1631 
1632   /* Clear the DT_RELR bitmap count.  */
1633   htab->dt_relr_bitmap.count = 0;
1634 
1635   count = relative_reloc->count;
1636 
1637   if (ABI_64_P (info->output_bfd))
1638     {
1639       /* Compute the 64-bit DT_RELR bitmap.  */
1640       i = 0;
1641       while (i < count)
1642 	{
1643 	  if ((relative_reloc->data[i].address % 1) != 0)
1644 	    abort ();
1645 
1646 	  elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1647 				    relative_reloc->data[i].address);
1648 
1649 	  base = relative_reloc->data[i].address + 8;
1650 	  i++;
1651 
1652 	  while (i < count)
1653 	    {
1654 	      uint64_t bitmap = 0;
1655 	      for (; i < count; i++)
1656 		{
1657 		  bfd_vma delta = (relative_reloc->data[i].address
1658 				   - base);
1659 		  /* Stop if it is too far from base.  */
1660 		  if (delta >= 63 * 8)
1661 		    break;
1662 		  /* Stop if it isn't a multiple of 8.  */
1663 		  if ((delta % 8) != 0)
1664 		    break;
1665 		  bitmap |= 1ULL << (delta / 8);
1666 		}
1667 
1668 	      if (bitmap == 0)
1669 		break;
1670 
1671 	      elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1672 					(bitmap << 1) | 1);
1673 
1674 	      base += 63 * 8;
1675 	    }
1676 	}
1677 
1678       new_count = htab->dt_relr_bitmap.count;
1679       if (dt_relr_bitmap_count > new_count)
1680 	{
1681 	  /* Don't shrink the DT_RELR section size to avoid section
1682 	     layout oscillation.  Instead, pad the DT_RELR bitmap with
1683 	     1s which do not decode to more relocations.  */
1684 
1685 	  htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
1686 	  count = dt_relr_bitmap_count - new_count;
1687 	  for (i = 0; i < count; i++)
1688 	    htab->dt_relr_bitmap.u.elf64[new_count + i] = 1;
1689 	}
1690     }
1691   else
1692     {
1693       /* Compute the 32-bit DT_RELR bitmap.  */
1694       i = 0;
1695       while (i < count)
1696 	{
1697 	  if ((relative_reloc->data[i].address % 1) != 0)
1698 	    abort ();
1699 
1700 	  elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1701 				    relative_reloc->data[i].address);
1702 
1703 	  base = relative_reloc->data[i].address + 4;
1704 	  i++;
1705 
1706 	  while (i < count)
1707 	    {
1708 	      uint32_t bitmap = 0;
1709 	      for (; i < count; i++)
1710 		{
1711 		  bfd_vma delta = (relative_reloc->data[i].address
1712 				   - base);
1713 		  /* Stop if it is too far from base.  */
1714 		  if (delta >= 31 * 4)
1715 		    break;
1716 		  /* Stop if it isn't a multiple of 4.  */
1717 		  if ((delta % 4) != 0)
1718 		    break;
1719 		  bitmap |= 1ULL << (delta / 4);
1720 		}
1721 
1722 	      if (bitmap == 0)
1723 		break;
1724 
1725 	      elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1726 					(bitmap << 1) | 1);
1727 
1728 	      base += 31 * 4;
1729 	    }
1730 	}
1731 
1732       new_count = htab->dt_relr_bitmap.count;
1733       if (dt_relr_bitmap_count > new_count)
1734 	{
1735 	  /* Don't shrink the DT_RELR section size to avoid section
1736 	     layout oscillation.  Instead, pad the DT_RELR bitmap with
1737 	     1s which do not decode to more relocations.  */
1738 
1739 	  htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
1740 	  count = dt_relr_bitmap_count - new_count;
1741 	  for (i = 0; i < count; i++)
1742 	    htab->dt_relr_bitmap.u.elf32[new_count + i] = 1;
1743 	}
1744     }
1745 
1746   if (htab->dt_relr_bitmap.count != dt_relr_bitmap_count)
1747     {
1748       if (need_layout)
1749 	{
1750 	  /* The .relr.dyn section size is changed.  Update the section
1751 	     size and tell linker to layout sections again.  */
1752 	  htab->elf.srelrdyn->size =
1753 	    (htab->dt_relr_bitmap.count
1754 	     * (ABI_64_P (info->output_bfd) ? 8 : 4));
1755 
1756 	  *need_layout = true;
1757 	}
1758       else
1759 	info->callbacks->einfo
1760 	  /* xgettext:c-format */
1761 	  (_("%F%P: %pB: size of compact relative reloc section is "
1762 	     "changed: new (%lu) != old (%lu)\n"),
1763 	   info->output_bfd, htab->dt_relr_bitmap.count,
1764 	   dt_relr_bitmap_count);
1765     }
1766 }
1767 
1768 /* Write out the DT_RELR section.  */
1769 
1770 static void
1771 elf_x86_write_dl_relr_bitmap (struct bfd_link_info *info,
1772 			      struct elf_x86_link_hash_table *htab)
1773 {
1774   asection *sec = htab->elf.srelrdyn;
1775   bfd_size_type size = sec->size;
1776   bfd_size_type i;
1777   unsigned char *contents;
1778 
1779   contents = (unsigned char *) bfd_alloc (sec->owner, size);
1780   if (contents == NULL)
1781     info->callbacks->einfo
1782       /* xgettext:c-format */
1783       (_("%F%P: %pB: failed to allocate compact relative reloc section\n"),
1784        info->output_bfd);
1785 
1786   /* Cache the section contents for elf_link_input_bfd.  */
1787   sec->contents = contents;
1788 
1789   if (ABI_64_P (info->output_bfd))
1790     for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 8)
1791       bfd_put_64 (info->output_bfd, htab->dt_relr_bitmap.u.elf64[i],
1792 		  contents);
1793   else
1794     for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 4)
1795       bfd_put_32 (info->output_bfd, htab->dt_relr_bitmap.u.elf32[i],
1796 		  contents);
1797 }
1798 
1799 /* Sort relative relocations by address.  */
1800 
1801 static int
1802 elf_x86_relative_reloc_compare (const void *pa, const void *pb)
1803 {
1804   struct elf_x86_relative_reloc_record *a =
1805     (struct elf_x86_relative_reloc_record *) pa;
1806   struct elf_x86_relative_reloc_record *b =
1807     (struct elf_x86_relative_reloc_record *) pb;
1808   if (a->address < b->address)
1809     return -1;
1810   if (a->address > b->address)
1811     return 1;
1812   return 0;
1813 }
1814 
1815 enum dynobj_sframe_plt_type
1816 {
1817   SFRAME_PLT = 1,
1818   SFRAME_PLT_SEC = 2
1819 };
1820 
1821 /* Create SFrame stack trace info for the plt entries in the .plt section
1822    of type PLT_SEC_TYPE.  */
1823 
1824 static bool
1825 _bfd_x86_elf_create_sframe_plt (bfd *output_bfd,
1826 				struct bfd_link_info *info,
1827 				unsigned int plt_sec_type)
1828 {
1829   struct elf_x86_link_hash_table *htab;
1830   const struct elf_backend_data *bed;
1831 
1832   bool plt0_generated_p;
1833   unsigned int plt0_entry_size;
1834   unsigned char func_info;
1835   uint32_t fre_type;
1836   /* The dynamic plt section for which .sframe stack trace information is being
1837      created.  */
1838   asection *dpltsec;
1839 
1840   int err = 0;
1841 
1842   sframe_encoder_ctx **ectx = NULL;
1843   unsigned plt_entry_size = 0;
1844   unsigned int num_pltn_fres = 0;
1845   unsigned int num_pltn_entries = 0;
1846 
1847   bed = get_elf_backend_data (output_bfd);
1848   htab = elf_x86_hash_table (info, bed->target_id);
1849   /* Whether SFrame stack trace info for plt0 is to be generated.  */
1850   plt0_generated_p = htab->plt.has_plt0;
1851   plt0_entry_size
1852     = (plt0_generated_p) ? htab->sframe_plt->plt0_entry_size : 0;
1853 
1854   switch (plt_sec_type)
1855     {
1856     case SFRAME_PLT:
1857 	{
1858 	  ectx = &htab->plt_cfe_ctx;
1859 	  dpltsec = htab->elf.splt;
1860 
1861 	  plt_entry_size = htab->plt.plt_entry_size;
1862 	  num_pltn_fres = htab->sframe_plt->pltn_num_fres;
1863 	  num_pltn_entries
1864 	    = (htab->elf.splt->size - plt0_entry_size) / plt_entry_size;
1865 
1866 	  break;
1867 	}
1868     case SFRAME_PLT_SEC:
1869 	{
1870 	  ectx = &htab->plt_second_cfe_ctx;
1871 	  /* FIXME - this or htab->plt_second_sframe ?  */
1872 	  dpltsec = htab->plt_second_eh_frame;
1873 
1874 	  plt_entry_size = htab->sframe_plt->sec_pltn_entry_size;
1875 	  num_pltn_fres = htab->sframe_plt->sec_pltn_num_fres;
1876 	  num_pltn_entries
1877 		= htab->plt_second_eh_frame->size / plt_entry_size;
1878 	  break;
1879 	}
1880     default:
1881       /* No other value is possible.  */
1882       return false;
1883       break;
1884     }
1885 
1886   *ectx = sframe_encode (SFRAME_VERSION_2,
1887 			 0,
1888 			 SFRAME_ABI_AMD64_ENDIAN_LITTLE,
1889 			 SFRAME_CFA_FIXED_FP_INVALID,
1890 			 -8, /*  Fixed RA offset.  */
1891 			 &err);
1892 
1893   /* FRE type is dependent on the size of the function.  */
1894   fre_type = sframe_calc_fre_type (dpltsec->size);
1895   func_info = sframe_fde_create_func_info (fre_type, SFRAME_FDE_TYPE_PCINC);
1896 
1897   /* Add SFrame FDE and the associated FREs for plt0 if plt0 has been
1898      generated.  */
1899   if (plt0_generated_p)
1900     {
1901       /* Add SFrame FDE for plt0, the function start address is updated later
1902 	 at _bfd_elf_merge_section_sframe time.  */
1903       sframe_encoder_add_funcdesc_v2 (*ectx,
1904 				      0, /* func start addr.  */
1905 				      plt0_entry_size,
1906 				      func_info,
1907 				      16,
1908 				      0 /* Num FREs.  */);
1909       sframe_frame_row_entry plt0_fre;
1910       unsigned int num_plt0_fres = htab->sframe_plt->plt0_num_fres;
1911       for (unsigned int j = 0; j < num_plt0_fres; j++)
1912 	{
1913 	  plt0_fre = *(htab->sframe_plt->plt0_fres[j]);
1914 	  sframe_encoder_add_fre (*ectx, 0, &plt0_fre);
1915 	}
1916     }
1917 
1918 
1919   if (num_pltn_entries)
1920     {
1921       /* pltn entries use an SFrame FDE of type
1922 	 SFRAME_FDE_TYPE_PCMASK to exploit the repetitive
1923 	 pattern of the instructions in these entries.  Using this SFrame FDE
1924 	 type helps in keeping the SFrame stack trace info for pltn entries
1925 	 compact.  */
1926       func_info	= sframe_fde_create_func_info (fre_type,
1927 					       SFRAME_FDE_TYPE_PCMASK);
1928       /* Add the SFrame FDE for all PCs starting at the first pltn entry (hence,
1929 	 function start address = plt0_entry_size.  As usual, this will be
1930 	 updated later at _bfd_elf_merge_section_sframe, by when the
1931 	 sections are relocated.  */
1932       sframe_encoder_add_funcdesc_v2 (*ectx,
1933 				      plt0_entry_size, /* func start addr.  */
1934 				      dpltsec->size - plt0_entry_size,
1935 				      func_info,
1936 				      16,
1937 				      0 /* Num FREs.  */);
1938 
1939       sframe_frame_row_entry pltn_fre;
1940       /* Now add the FREs for pltn.  Simply adding the two FREs suffices due
1941 	 to the usage of SFRAME_FDE_TYPE_PCMASK above.  */
1942       for (unsigned int j = 0; j < num_pltn_fres; j++)
1943 	{
1944 	  pltn_fre = *(htab->sframe_plt->pltn_fres[j]);
1945 	  sframe_encoder_add_fre (*ectx, 1, &pltn_fre);
1946 	}
1947     }
1948 
1949   return true;
1950 }
1951 
1952 /* Put contents of the .sframe section corresponding to the specified
1953    PLT_SEC_TYPE.  */
1954 
1955 static bool
1956 _bfd_x86_elf_write_sframe_plt (bfd *output_bfd,
1957 			       struct bfd_link_info *info,
1958 			       unsigned int plt_sec_type)
1959 {
1960   struct elf_x86_link_hash_table *htab;
1961   const struct elf_backend_data *bed;
1962   sframe_encoder_ctx *ectx;
1963   size_t sec_size;
1964   asection *sec;
1965   bfd *dynobj;
1966 
1967   int err = 0;
1968 
1969   bed = get_elf_backend_data (output_bfd);
1970   htab = elf_x86_hash_table (info, bed->target_id);
1971   dynobj = htab->elf.dynobj;
1972 
1973   switch (plt_sec_type)
1974     {
1975     case SFRAME_PLT:
1976       ectx = htab->plt_cfe_ctx;
1977       sec = htab->plt_sframe;
1978       break;
1979     case SFRAME_PLT_SEC:
1980       ectx = htab->plt_second_cfe_ctx;
1981       sec = htab->plt_second_sframe;
1982       break;
1983     default:
1984       /* No other value is possible.  */
1985       return false;
1986       break;
1987     }
1988 
1989   BFD_ASSERT (ectx);
1990 
1991   void *contents = sframe_encoder_write (ectx, &sec_size, &err);
1992 
1993   sec->size = (bfd_size_type) sec_size;
1994   sec->contents = (unsigned char *) bfd_zalloc (dynobj, sec->size);
1995   memcpy (sec->contents, contents, sec_size);
1996 
1997   sframe_encoder_free (&ectx);
1998 
1999   return true;
2000 }
2001 
2002 bool
2003 _bfd_elf_x86_size_relative_relocs (struct bfd_link_info *info,
2004 				   bool *need_layout)
2005 {
2006   struct elf_x86_link_hash_table *htab;
2007   const struct elf_backend_data *bed;
2008   bool is_x86_64;
2009   bfd_size_type i, count, unaligned_count;
2010   asection *sec, *srel;
2011 
2012   /* Do nothing for ld -r.  */
2013   if (bfd_link_relocatable (info))
2014     return true;
2015 
2016   bed = get_elf_backend_data (info->output_bfd);
2017   htab = elf_x86_hash_table (info, bed->target_id);
2018   if (htab == NULL)
2019     return false;
2020 
2021   count = htab->relative_reloc.count;
2022   unaligned_count = htab->unaligned_relative_reloc.count;
2023   if (count == 0)
2024     {
2025       if (htab->generate_relative_reloc_pass == 0
2026 	  && htab->elf.srelrdyn != NULL)
2027 	{
2028 	  /* Remove the empty .relr.dyn sections now.  */
2029 	  if (!bfd_is_abs_section (htab->elf.srelrdyn->output_section))
2030 	    {
2031 	      bfd_section_list_remove
2032 		(info->output_bfd, htab->elf.srelrdyn->output_section);
2033 	      info->output_bfd->section_count--;
2034 	    }
2035 	  bfd_section_list_remove (htab->elf.srelrdyn->owner,
2036 				   htab->elf.srelrdyn);
2037 	  htab->elf.srelrdyn->owner->section_count--;
2038 	}
2039       if (unaligned_count == 0)
2040 	{
2041 	  htab->generate_relative_reloc_pass++;
2042 	  return true;
2043 	}
2044     }
2045 
2046   is_x86_64 = bed->target_id == X86_64_ELF_DATA;
2047 
2048   /* Size relative relocations.  */
2049   if (htab->generate_relative_reloc_pass)
2050     {
2051       /* Reset the regular relative relocation count.  */
2052       for (i = 0; i < unaligned_count; i++)
2053 	{
2054 	  sec = htab->unaligned_relative_reloc.data[i].sec;
2055 	  srel = elf_section_data (sec)->sreloc;
2056 	  srel->reloc_count = 0;
2057 	}
2058     }
2059   else
2060     {
2061       /* Remove the reserved space for compact relative relocations.  */
2062       if (count)
2063 	{
2064 	  asection *sgot = htab->elf.sgot;
2065 	  asection *srelgot = htab->elf.srelgot;
2066 
2067 	  for (i = 0; i < count; i++)
2068 	    {
2069 	      sec = htab->relative_reloc.data[i].sec;
2070 	      if (sec == sgot)
2071 		srel = srelgot;
2072 	      else
2073 		srel = elf_section_data (sec)->sreloc;
2074 	      srel->size -= htab->sizeof_reloc;
2075 	    }
2076 	}
2077     }
2078 
2079   /* Size unaligned relative relocations.  */
2080   if (unaligned_count)
2081     elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2082 					   true, NULL);
2083 
2084   if (count)
2085     {
2086       elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2087 					     false, NULL);
2088 
2089       /* Sort relative relocations by addresses.  We only need to
2090 	 sort them in the first pass since the relative positions
2091 	 won't change.  */
2092       if (htab->generate_relative_reloc_pass == 0)
2093 	qsort (htab->relative_reloc.data, count,
2094 	       sizeof (struct elf_x86_relative_reloc_record),
2095 	       elf_x86_relative_reloc_compare);
2096 
2097       elf_x86_compute_dl_relr_bitmap (info, htab, need_layout);
2098     }
2099 
2100   htab->generate_relative_reloc_pass++;
2101 
2102   return true;
2103 }
2104 
2105 bool
2106 _bfd_elf_x86_finish_relative_relocs (struct bfd_link_info *info)
2107 {
2108   struct elf_x86_link_hash_table *htab;
2109   const struct elf_backend_data *bed;
2110   Elf_Internal_Rela outrel;
2111   bool is_x86_64;
2112   bfd_size_type count;
2113 
2114   /* Do nothing for ld -r.  */
2115   if (bfd_link_relocatable (info))
2116     return true;
2117 
2118   bed = get_elf_backend_data (info->output_bfd);
2119   htab = elf_x86_hash_table (info, bed->target_id);
2120   if (htab == NULL)
2121     return false;
2122 
2123   is_x86_64 = bed->target_id == X86_64_ELF_DATA;
2124 
2125   outrel.r_info = htab->r_info (0, htab->relative_r_type);
2126 
2127   if (htab->unaligned_relative_reloc.count)
2128     elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2129 					   true, &outrel);
2130 
2131   count = htab->relative_reloc.count;
2132   if (count)
2133     {
2134       elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
2135 					     false, &outrel);
2136 
2137       elf_x86_compute_dl_relr_bitmap (info, htab, NULL);
2138 
2139       elf_x86_write_dl_relr_bitmap (info, htab);
2140     }
2141 
2142   return true;
2143 }
2144 
2145 bool
2146 _bfd_elf_x86_valid_reloc_p (asection *input_section,
2147 			    struct bfd_link_info *info,
2148 			    struct elf_x86_link_hash_table *htab,
2149 			    const Elf_Internal_Rela *rel,
2150 			    struct elf_link_hash_entry *h,
2151 			    Elf_Internal_Sym *sym,
2152 			    Elf_Internal_Shdr *symtab_hdr,
2153 			    bool *no_dynreloc_p)
2154 {
2155   bool valid_p = true;
2156 
2157   *no_dynreloc_p = false;
2158 
2159   /* Check If relocation against non-preemptible absolute symbol is
2160      valid in PIC.  FIXME: Can't use SYMBOL_REFERENCES_LOCAL_P since
2161      it may call _bfd_elf_link_hide_sym_by_version and result in
2162      ld-elfvers/ vers21 test failure.  */
2163   if (bfd_link_pic (info)
2164       && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
2165     {
2166       const struct elf_backend_data *bed;
2167       unsigned int r_type;
2168       Elf_Internal_Rela irel;
2169 
2170       /* Skip non-absolute symbol.  */
2171       if (h)
2172 	{
2173 	  if (!ABS_SYMBOL_P (h))
2174 	    return valid_p;
2175 	}
2176       else if (sym->st_shndx != SHN_ABS)
2177 	return valid_p;
2178 
2179       bed = get_elf_backend_data (input_section->owner);
2180       r_type = ELF32_R_TYPE (rel->r_info);
2181       irel = *rel;
2182 
2183       /* Only allow relocations against absolute symbol, which can be
2184 	 resolved as absolute value + addend.  GOTPCREL and GOT32
2185 	 relocations are allowed since absolute value + addend is
2186 	 stored in the GOT slot.  */
2187       if (bed->target_id == X86_64_ELF_DATA)
2188 	{
2189 	  r_type &= ~R_X86_64_converted_reloc_bit;
2190 	  valid_p = (r_type == R_X86_64_64
2191 		     || r_type == R_X86_64_32
2192 		     || r_type == R_X86_64_32S
2193 		     || r_type == R_X86_64_16
2194 		     || r_type == R_X86_64_8
2195 		     || r_type == R_X86_64_GOTPCREL
2196 		     || r_type == R_X86_64_GOTPCRELX
2197 		     || r_type == R_X86_64_REX_GOTPCRELX);
2198 	  if (!valid_p)
2199 	    {
2200 	      unsigned int r_symndx = htab->r_sym (rel->r_info);
2201 	      irel.r_info = htab->r_info (r_symndx, r_type);
2202 	    }
2203 	}
2204       else
2205 	valid_p = (r_type == R_386_32
2206 		   || r_type == R_386_16
2207 		   || r_type == R_386_8
2208 		   || r_type == R_386_GOT32
2209 		   || r_type == R_386_GOT32X);
2210 
2211       if (valid_p)
2212 	*no_dynreloc_p = true;
2213       else
2214 	{
2215 	  const char *name;
2216 	  arelent internal_reloc;
2217 
2218 	  if (!bed->elf_info_to_howto (input_section->owner,
2219 				       &internal_reloc, &irel)
2220 	      || internal_reloc.howto == NULL)
2221 	    abort ();
2222 
2223 	  if (h)
2224 	    name = h->root.root.string;
2225 	  else
2226 	    name = bfd_elf_sym_name (input_section->owner, symtab_hdr,
2227 				     sym, NULL);
2228 	  info->callbacks->einfo
2229 	    /* xgettext:c-format */
2230 	    (_("%F%P: %pB: relocation %s against absolute symbol "
2231 	       "`%s' in section `%pA' is disallowed\n"),
2232 	     input_section->owner, internal_reloc.howto->name, name,
2233 	     input_section);
2234 	  bfd_set_error (bfd_error_bad_value);
2235 	}
2236     }
2237 
2238   return valid_p;
2239 }
2240 
2241 /* Set the sizes of the dynamic sections.  */
2242 
2243 bool
2244 _bfd_x86_elf_size_dynamic_sections (bfd *output_bfd,
2245 				    struct bfd_link_info *info)
2246 {
2247   struct elf_x86_link_hash_table *htab;
2248   bfd *dynobj;
2249   asection *s;
2250   bool relocs;
2251   bfd *ibfd;
2252   const struct elf_backend_data *bed
2253     = get_elf_backend_data (output_bfd);
2254 
2255   htab = elf_x86_hash_table (info, bed->target_id);
2256   if (htab == NULL)
2257     return false;
2258   dynobj = htab->elf.dynobj;
2259   if (dynobj == NULL)
2260     abort ();
2261 
2262   /* Set up .got offsets for local syms, and space for local dynamic
2263      relocs.  */
2264   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2265     {
2266       bfd_signed_vma *local_got;
2267       bfd_signed_vma *end_local_got;
2268       char *local_tls_type;
2269       bfd_vma *local_tlsdesc_gotent;
2270       bfd_size_type locsymcount;
2271       Elf_Internal_Shdr *symtab_hdr;
2272       asection *srel;
2273 
2274       if (! is_x86_elf (ibfd, htab))
2275 	continue;
2276 
2277       for (s = ibfd->sections; s != NULL; s = s->next)
2278 	{
2279 	  struct elf_dyn_relocs *p;
2280 
2281 	  for (p = ((struct elf_dyn_relocs *)
2282 		     elf_section_data (s)->local_dynrel);
2283 	       p != NULL;
2284 	       p = p->next)
2285 	    {
2286 	      if (!bfd_is_abs_section (p->sec)
2287 		  && bfd_is_abs_section (p->sec->output_section))
2288 		{
2289 		  /* Input section has been discarded, either because
2290 		     it is a copy of a linkonce section or due to
2291 		     linker script /DISCARD/, so we'll be discarding
2292 		     the relocs too.  */
2293 		}
2294 	      else if (htab->elf.target_os == is_vxworks
2295 		       && strcmp (p->sec->output_section->name,
2296 				  ".tls_vars") == 0)
2297 		{
2298 		  /* Relocations in vxworks .tls_vars sections are
2299 		     handled specially by the loader.  */
2300 		}
2301 	      else if (p->count != 0)
2302 		{
2303 		  srel = elf_section_data (p->sec)->sreloc;
2304 		  srel->size += p->count * htab->sizeof_reloc;
2305 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0
2306 		      && (info->flags & DF_TEXTREL) == 0)
2307 		    {
2308 		      info->flags |= DF_TEXTREL;
2309 		      if (bfd_link_textrel_check (info))
2310 			/* xgettext:c-format */
2311 			info->callbacks->einfo
2312 			  (_("%P: %pB: warning: relocation "
2313 			     "in read-only section `%pA'\n"),
2314 			   p->sec->owner, p->sec);
2315 		    }
2316 		}
2317 	    }
2318 	}
2319 
2320       local_got = elf_local_got_refcounts (ibfd);
2321       if (!local_got)
2322 	continue;
2323 
2324       symtab_hdr = &elf_symtab_hdr (ibfd);
2325       locsymcount = symtab_hdr->sh_info;
2326       end_local_got = local_got + locsymcount;
2327       local_tls_type = elf_x86_local_got_tls_type (ibfd);
2328       local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
2329       s = htab->elf.sgot;
2330       srel = htab->elf.srelgot;
2331       for (; local_got < end_local_got;
2332 	   ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2333 	{
2334 	  *local_tlsdesc_gotent = (bfd_vma) -1;
2335 	  if (*local_got > 0)
2336 	    {
2337 	      if (GOT_TLS_GDESC_P (*local_tls_type))
2338 		{
2339 		  *local_tlsdesc_gotent = htab->elf.sgotplt->size
2340 		    - elf_x86_compute_jump_table_size (htab);
2341 		  htab->elf.sgotplt->size += 2 * htab->got_entry_size;
2342 		  *local_got = (bfd_vma) -2;
2343 		}
2344 	      if (! GOT_TLS_GDESC_P (*local_tls_type)
2345 		  || GOT_TLS_GD_P (*local_tls_type))
2346 		{
2347 		  *local_got = s->size;
2348 		  s->size += htab->got_entry_size;
2349 		  if (GOT_TLS_GD_P (*local_tls_type)
2350 		      || *local_tls_type == GOT_TLS_IE_BOTH)
2351 		    s->size += htab->got_entry_size;
2352 		}
2353 	      if ((bfd_link_pic (info) && *local_tls_type != GOT_ABS)
2354 		  || GOT_TLS_GD_ANY_P (*local_tls_type)
2355 		  || (*local_tls_type & GOT_TLS_IE))
2356 		{
2357 		  if (*local_tls_type == GOT_TLS_IE_BOTH)
2358 		    srel->size += 2 * htab->sizeof_reloc;
2359 		  else if (GOT_TLS_GD_P (*local_tls_type)
2360 			   || ! GOT_TLS_GDESC_P (*local_tls_type))
2361 		    srel->size += htab->sizeof_reloc;
2362 		  if (GOT_TLS_GDESC_P (*local_tls_type))
2363 		    {
2364 		      htab->elf.srelplt->size += htab->sizeof_reloc;
2365 		      if (bed->target_id == X86_64_ELF_DATA)
2366 			htab->elf.tlsdesc_plt = (bfd_vma) -1;
2367 		    }
2368 		}
2369 	    }
2370 	  else
2371 	    *local_got = (bfd_vma) -1;
2372 	}
2373     }
2374 
2375   if (htab->tls_ld_or_ldm_got.refcount > 0)
2376     {
2377       /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2378 	 or R_X86_64_TLSLD relocs.  */
2379       htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
2380       htab->elf.sgot->size += 2 * htab->got_entry_size;
2381       htab->elf.srelgot->size += htab->sizeof_reloc;
2382     }
2383   else
2384     htab->tls_ld_or_ldm_got.offset = -1;
2385 
2386   /* Allocate global sym .plt and .got entries, and space for global
2387      sym dynamic relocs.  */
2388   elf_link_hash_traverse (&htab->elf, elf_x86_allocate_dynrelocs,
2389 			  info);
2390 
2391   /* Allocate .plt and .got entries, and space for local symbols.  */
2392   htab_traverse (htab->loc_hash_table, elf_x86_allocate_local_dynreloc,
2393 		 info);
2394 
2395   /* For every jump slot reserved in the sgotplt, reloc_count is
2396      incremented.  However, when we reserve space for TLS descriptors,
2397      it's not incremented, so in order to compute the space reserved
2398      for them, it suffices to multiply the reloc count by the jump
2399      slot size.
2400 
2401      PR ld/13302: We start next_irelative_index at the end of .rela.plt
2402      so that R_{386,X86_64}_IRELATIVE entries come last.  */
2403   if (htab->elf.srelplt)
2404     {
2405       htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
2406       htab->sgotplt_jump_table_size
2407 	= elf_x86_compute_jump_table_size (htab);
2408       htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
2409     }
2410   else if (htab->elf.irelplt)
2411     htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
2412 
2413   if (htab->elf.tlsdesc_plt)
2414     {
2415       /* NB: tlsdesc_plt is set only for x86-64.  If we're not using
2416 	 lazy TLS relocations, don't generate the PLT and GOT entries
2417 	 they require.  */
2418       if ((info->flags & DF_BIND_NOW))
2419 	htab->elf.tlsdesc_plt = 0;
2420       else
2421 	{
2422 	  htab->elf.tlsdesc_got = htab->elf.sgot->size;
2423 	  htab->elf.sgot->size += htab->got_entry_size;
2424 	  /* Reserve room for the initial entry.
2425 	     FIXME: we could probably do away with it in this case.  */
2426 	  if (htab->elf.splt->size == 0)
2427 	    htab->elf.splt->size = htab->plt.plt_entry_size;
2428 	  htab->elf.tlsdesc_plt = htab->elf.splt->size;
2429 	  htab->elf.splt->size += htab->plt.plt_entry_size;
2430 	}
2431     }
2432 
2433   if (htab->elf.sgotplt)
2434     {
2435       /* Don't allocate .got.plt section if there are no GOT nor PLT
2436 	 entries and there is no reference to _GLOBAL_OFFSET_TABLE_.  */
2437       if ((htab->elf.hgot == NULL
2438 	   || !htab->got_referenced)
2439 	  && (htab->elf.sgotplt->size == bed->got_header_size)
2440 	  && (htab->elf.splt == NULL
2441 	      || htab->elf.splt->size == 0)
2442 	  && (htab->elf.sgot == NULL
2443 	      || htab->elf.sgot->size == 0)
2444 	  && (htab->elf.iplt == NULL
2445 	      || htab->elf.iplt->size == 0)
2446 	  && (htab->elf.igotplt == NULL
2447 	      || htab->elf.igotplt->size == 0))
2448 	{
2449 	  htab->elf.sgotplt->size = 0;
2450 	  /* Solaris requires to keep _GLOBAL_OFFSET_TABLE_ even if it
2451 	     isn't used.  */
2452 	  if (htab->elf.hgot != NULL
2453 	      && htab->elf.target_os != is_solaris)
2454 	    {
2455 	      /* Remove the unused _GLOBAL_OFFSET_TABLE_ from symbol
2456 		 table. */
2457 	      htab->elf.hgot->root.type = bfd_link_hash_undefined;
2458 	      htab->elf.hgot->root.u.undef.abfd
2459 		= htab->elf.hgot->root.u.def.section->owner;
2460 	      htab->elf.hgot->root.linker_def = 0;
2461 	      htab->elf.hgot->ref_regular = 0;
2462 	      htab->elf.hgot->def_regular = 0;
2463 	    }
2464 	}
2465     }
2466 
2467   if (_bfd_elf_eh_frame_present (info))
2468     {
2469       if (htab->plt_eh_frame != NULL
2470 	  && htab->elf.splt != NULL
2471 	  && htab->elf.splt->size != 0
2472 	  && !bfd_is_abs_section (htab->elf.splt->output_section))
2473 	htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
2474 
2475       if (htab->plt_got_eh_frame != NULL
2476 	  && htab->plt_got != NULL
2477 	  && htab->plt_got->size != 0
2478 	  && !bfd_is_abs_section (htab->plt_got->output_section))
2479 	htab->plt_got_eh_frame->size
2480 	  = htab->non_lazy_plt->eh_frame_plt_size;
2481 
2482       /* Unwind info for the second PLT and .plt.got sections are
2483 	 identical.  */
2484       if (htab->plt_second_eh_frame != NULL
2485 	  && htab->plt_second != NULL
2486 	  && htab->plt_second->size != 0
2487 	  && !bfd_is_abs_section (htab->plt_second->output_section))
2488 	htab->plt_second_eh_frame->size
2489 	  = htab->non_lazy_plt->eh_frame_plt_size;
2490     }
2491 
2492   /* No need to size the .sframe section explicitly because the write-out
2493      mechanism is different.  Simply prep up the FDE/FRE for the
2494      .plt section.  */
2495   if (_bfd_elf_sframe_present (info))
2496     {
2497       if (htab->plt_sframe != NULL
2498 	  && htab->elf.splt != NULL
2499 	  && htab->elf.splt->size != 0
2500 	  && !bfd_is_abs_section (htab->elf.splt->output_section))
2501 	{
2502 	  _bfd_x86_elf_create_sframe_plt (output_bfd, info, SFRAME_PLT);
2503 	  /* FIXME - Dirty Hack.  Set the size to something non-zero for now,
2504 	     so that the section does not get stripped out below.  The precise
2505 	     size of this section is known only when the contents are
2506 	     serialized in _bfd_x86_elf_write_sframe_plt.  */
2507 	  htab->plt_sframe->size = sizeof (sframe_header) + 1;
2508 	}
2509 
2510       /* FIXME - generate for .got.plt ?  */
2511 
2512       /* Unwind info for the second PLT.  */
2513       if (htab->plt_second_sframe != NULL
2514 	  && htab->plt_second != NULL
2515 	  && htab->plt_second->size != 0
2516 	  && !bfd_is_abs_section (htab->plt_second->output_section))
2517 	{
2518 	  _bfd_x86_elf_create_sframe_plt (output_bfd, info,
2519 					  SFRAME_PLT_SEC);
2520 	  /* FIXME - Dirty Hack.  Set the size to something non-zero for now,
2521 	     so that the section does not get stripped out below.  The precise
2522 	     size of this section is known only when the contents are
2523 	     serialized in _bfd_x86_elf_write_sframe_plt.  */
2524 	  htab->plt_second_sframe->size = sizeof (sframe_header) + 1;
2525 	}
2526     }
2527 
2528   asection *resolved_plt = NULL;
2529 
2530   if (htab->params->mark_plt && htab->elf.dynamic_sections_created)
2531     {
2532       if (htab->plt_second != NULL)
2533 	resolved_plt = htab->plt_second;
2534       else
2535 	resolved_plt = htab->elf.splt;
2536 
2537       if (resolved_plt != NULL && resolved_plt->size == 0)
2538 	resolved_plt = NULL;
2539     }
2540 
2541   /* We now have determined the sizes of the various dynamic sections.
2542      Allocate memory for them.  */
2543   relocs = false;
2544   for (s = dynobj->sections; s != NULL; s = s->next)
2545     {
2546       bool strip_section = true;
2547 
2548       if ((s->flags & SEC_LINKER_CREATED) == 0)
2549 	continue;
2550 
2551       /* The .relr.dyn section for compact relative relocation will
2552 	 be filled later.  */
2553       if (s == htab->elf.srelrdyn)
2554 	continue;
2555 
2556       if (s == htab->elf.splt
2557 	  || s == htab->elf.sgot)
2558 	{
2559 	  /* Strip this section if we don't need it; see the
2560 	     comment below.  */
2561 	  /* We'd like to strip these sections if they aren't needed, but if
2562 	     we've exported dynamic symbols from them we must leave them.
2563 	     It's too late to tell BFD to get rid of the symbols.  */
2564 
2565 	  if (htab->elf.hplt != NULL)
2566 	    strip_section = false;
2567 	}
2568       else if (s == htab->elf.sgotplt
2569 	       || s == htab->elf.iplt
2570 	       || s == htab->elf.igotplt
2571 	       || s == htab->plt_second
2572 	       || s == htab->plt_got
2573 	       || s == htab->plt_eh_frame
2574 	       || s == htab->plt_got_eh_frame
2575 	       || s == htab->plt_second_eh_frame
2576 	       || s == htab->plt_sframe
2577 	       || s == htab->plt_second_sframe
2578 	       || s == htab->elf.sdynbss
2579 	       || s == htab->elf.sdynrelro)
2580 	{
2581 	  /* Strip these too.  */
2582 	}
2583       else if (htab->is_reloc_section (bfd_section_name (s)))
2584 	{
2585 	  if (s->size != 0
2586 	      && s != htab->elf.srelplt
2587 	      && s != htab->srelplt2)
2588 	    relocs = true;
2589 
2590 	  /* We use the reloc_count field as a counter if we need
2591 	     to copy relocs into the output file.  */
2592 	  if (s != htab->elf.srelplt)
2593 	    s->reloc_count = 0;
2594 	}
2595       else
2596 	{
2597 	  /* It's not one of our sections, so don't allocate space.  */
2598 	  continue;
2599 	}
2600 
2601       if (s->size == 0)
2602 	{
2603 	  /* If we don't need this section, strip it from the
2604 	     output file.  This is mostly to handle .rel.bss and
2605 	     .rel.plt.  We must create both sections in
2606 	     create_dynamic_sections, because they must be created
2607 	     before the linker maps input sections to output
2608 	     sections.  The linker does that before
2609 	     adjust_dynamic_symbol is called, and it is that
2610 	     function which decides whether anything needs to go
2611 	     into these sections.  */
2612 	  if (strip_section)
2613 	    s->flags |= SEC_EXCLUDE;
2614 	  continue;
2615 	}
2616 
2617       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2618 	continue;
2619 
2620       /* Skip allocating contents for .sframe section as it is written
2621 	 out differently.  See below.  */
2622       if ((s == htab->plt_sframe) || (s == htab->plt_second_sframe))
2623 	continue;
2624 
2625       /* NB: Initially, the iplt section has minimal alignment to
2626 	 avoid moving dot of the following section backwards when
2627 	 it is empty.  Update its section alignment now since it
2628 	 is non-empty.  */
2629       if (s == htab->elf.iplt)
2630 	bfd_set_section_alignment (s, htab->plt.iplt_alignment);
2631 
2632       /* Allocate memory for the section contents.  We use bfd_zalloc
2633 	 here in case unused entries are not reclaimed before the
2634 	 section's contents are written out.  This should not happen,
2635 	 but this way if it does, we get a R_386_NONE or R_X86_64_NONE
2636 	 reloc instead of garbage.  */
2637       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
2638       if (s->contents == NULL)
2639 	return false;
2640     }
2641 
2642   if (htab->plt_eh_frame != NULL
2643       && htab->plt_eh_frame->contents != NULL)
2644     {
2645       memcpy (htab->plt_eh_frame->contents,
2646 	      htab->plt.eh_frame_plt,
2647 	      htab->plt_eh_frame->size);
2648       bfd_put_32 (dynobj, htab->elf.splt->size,
2649 		  htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
2650     }
2651 
2652   if (htab->plt_got_eh_frame != NULL
2653       && htab->plt_got_eh_frame->contents != NULL)
2654     {
2655       memcpy (htab->plt_got_eh_frame->contents,
2656 	      htab->non_lazy_plt->eh_frame_plt,
2657 	      htab->plt_got_eh_frame->size);
2658       bfd_put_32 (dynobj, htab->plt_got->size,
2659 		  (htab->plt_got_eh_frame->contents
2660 		   + PLT_FDE_LEN_OFFSET));
2661     }
2662 
2663   if (htab->plt_second_eh_frame != NULL
2664       && htab->plt_second_eh_frame->contents != NULL)
2665     {
2666       memcpy (htab->plt_second_eh_frame->contents,
2667 	      htab->non_lazy_plt->eh_frame_plt,
2668 	      htab->plt_second_eh_frame->size);
2669       bfd_put_32 (dynobj, htab->plt_second->size,
2670 		  (htab->plt_second_eh_frame->contents
2671 		   + PLT_FDE_LEN_OFFSET));
2672     }
2673 
2674   if (_bfd_elf_sframe_present (info))
2675     {
2676       if (htab->plt_sframe != NULL
2677 	  && htab->elf.splt != NULL
2678 	  && htab->elf.splt->size != 0
2679 	  && htab->plt_sframe->contents == NULL)
2680 	_bfd_x86_elf_write_sframe_plt (output_bfd, info, SFRAME_PLT);
2681 
2682       if (htab->plt_second_sframe != NULL
2683 	  && htab->elf.splt != NULL
2684 	  && htab->elf.splt->size != 0
2685 	  && htab->plt_second_sframe->contents == NULL)
2686 	_bfd_x86_elf_write_sframe_plt (output_bfd, info, SFRAME_PLT_SEC);
2687     }
2688 
2689   if (resolved_plt != NULL
2690       && (!_bfd_elf_add_dynamic_entry (info, DT_X86_64_PLT, 0)
2691 	  || !_bfd_elf_add_dynamic_entry (info, DT_X86_64_PLTSZ, 0)
2692 	  || !_bfd_elf_add_dynamic_entry (info, DT_X86_64_PLTENT, 0)))
2693     return false;
2694 
2695   return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
2696 						  relocs);
2697 }
2698 
2699 /* Finish up the x86 dynamic sections.  */
2700 
2701 struct elf_x86_link_hash_table *
2702 _bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd,
2703 				      struct bfd_link_info *info)
2704 {
2705   struct elf_x86_link_hash_table *htab;
2706   const struct elf_backend_data *bed;
2707   bfd *dynobj;
2708   asection *sdyn;
2709   bfd_byte *dyncon, *dynconend;
2710   bfd_size_type sizeof_dyn;
2711 
2712   bed = get_elf_backend_data (output_bfd);
2713   htab = elf_x86_hash_table (info, bed->target_id);
2714   if (htab == NULL)
2715     return htab;
2716 
2717   dynobj = htab->elf.dynobj;
2718   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2719 
2720   /* GOT is always created in setup_gnu_properties.  But it may not be
2721      needed.  .got.plt section may be needed for static IFUNC.  */
2722   if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
2723     {
2724       bfd_vma dynamic_addr;
2725 
2726       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
2727 	{
2728 	  _bfd_error_handler
2729 	    (_("discarded output section: `%pA'"), htab->elf.sgotplt);
2730 	  return NULL;
2731 	}
2732 
2733       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
2734 	= htab->got_entry_size;
2735 
2736       dynamic_addr = (sdyn == NULL
2737 		      ? (bfd_vma) 0
2738 		      : sdyn->output_section->vma + sdyn->output_offset);
2739 
2740       /* Set the first entry in the global offset table to the address
2741 	 of the dynamic section.  Write GOT[1] and GOT[2], needed for
2742 	 the dynamic linker.  */
2743       if (htab->got_entry_size == 8)
2744 	{
2745 	  bfd_put_64 (output_bfd, dynamic_addr,
2746 		      htab->elf.sgotplt->contents);
2747 	  bfd_put_64 (output_bfd, (bfd_vma) 0,
2748 		      htab->elf.sgotplt->contents + 8);
2749 	  bfd_put_64 (output_bfd, (bfd_vma) 0,
2750 		      htab->elf.sgotplt->contents + 8*2);
2751 	}
2752       else
2753 	{
2754 	  bfd_put_32 (output_bfd, dynamic_addr,
2755 		      htab->elf.sgotplt->contents);
2756 	  bfd_put_32 (output_bfd, 0,
2757 		      htab->elf.sgotplt->contents + 4);
2758 	  bfd_put_32 (output_bfd, 0,
2759 		      htab->elf.sgotplt->contents + 4*2);
2760 	}
2761     }
2762 
2763   if (!htab->elf.dynamic_sections_created)
2764     return htab;
2765 
2766   if (sdyn == NULL || htab->elf.sgot == NULL)
2767     abort ();
2768 
2769   asection *resolved_plt;
2770   if (htab->plt_second != NULL)
2771     resolved_plt = htab->plt_second;
2772   else
2773     resolved_plt = htab->elf.splt;
2774 
2775   sizeof_dyn = bed->s->sizeof_dyn;
2776   dyncon = sdyn->contents;
2777   dynconend = sdyn->contents + sdyn->size;
2778   for (; dyncon < dynconend; dyncon += sizeof_dyn)
2779     {
2780       Elf_Internal_Dyn dyn;
2781       asection *s;
2782 
2783       (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
2784 
2785       switch (dyn.d_tag)
2786 	{
2787 	default:
2788 	  if (htab->elf.target_os == is_vxworks
2789 	      && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
2790 	    break;
2791 	  continue;
2792 
2793 	case DT_PLTGOT:
2794 	  s = htab->elf.sgotplt;
2795 	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2796 	  break;
2797 
2798 	case DT_JMPREL:
2799 	  s = htab->elf.srelplt;
2800 	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2801 	  break;
2802 
2803 	case DT_PLTRELSZ:
2804 	  s = htab->elf.srelplt;
2805 	  dyn.d_un.d_val = s->size;
2806 	  break;
2807 
2808 	case DT_TLSDESC_PLT:
2809 	  s = htab->elf.splt;
2810 	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
2811 	    + htab->elf.tlsdesc_plt;
2812 	  break;
2813 
2814 	case DT_TLSDESC_GOT:
2815 	  s = htab->elf.sgot;
2816 	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
2817 	    + htab->elf.tlsdesc_got;
2818 	  break;
2819 
2820 	case DT_X86_64_PLT:
2821 	  s = resolved_plt->output_section;
2822 	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2823 	  break;
2824 
2825 	case DT_X86_64_PLTSZ:
2826 	  dyn.d_un.d_val = resolved_plt->size;
2827 	  break;
2828 
2829 	case DT_X86_64_PLTENT:
2830 	  dyn.d_un.d_ptr = htab->plt.plt_entry_size;
2831 	  break;
2832 	}
2833 
2834       (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
2835     }
2836 
2837   if (htab->plt_got != NULL && htab->plt_got->size > 0)
2838     elf_section_data (htab->plt_got->output_section)
2839       ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
2840 
2841   if (htab->plt_second != NULL && htab->plt_second->size > 0)
2842     elf_section_data (htab->plt_second->output_section)
2843       ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
2844 
2845   /* Adjust .eh_frame for .plt section.  */
2846   if (htab->plt_eh_frame != NULL
2847       && htab->plt_eh_frame->contents != NULL)
2848     {
2849       if (htab->elf.splt != NULL
2850 	  && htab->elf.splt->size != 0
2851 	  && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
2852 	  && htab->elf.splt->output_section != NULL
2853 	  && htab->plt_eh_frame->output_section != NULL)
2854 	{
2855 	  bfd_vma plt_start = htab->elf.splt->output_section->vma;
2856 	  bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
2857 				   + htab->plt_eh_frame->output_offset
2858 				   + PLT_FDE_START_OFFSET;
2859 	  bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2860 			     htab->plt_eh_frame->contents
2861 			     + PLT_FDE_START_OFFSET);
2862 	}
2863 
2864       if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
2865 	{
2866 	  if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2867 						 htab->plt_eh_frame,
2868 						 htab->plt_eh_frame->contents))
2869 	    return NULL;
2870 	}
2871     }
2872 
2873   /* Adjust .eh_frame for .plt.got section.  */
2874   if (htab->plt_got_eh_frame != NULL
2875       && htab->plt_got_eh_frame->contents != NULL)
2876     {
2877       if (htab->plt_got != NULL
2878 	  && htab->plt_got->size != 0
2879 	  && (htab->plt_got->flags & SEC_EXCLUDE) == 0
2880 	  && htab->plt_got->output_section != NULL
2881 	  && htab->plt_got_eh_frame->output_section != NULL)
2882 	{
2883 	  bfd_vma plt_start = htab->plt_got->output_section->vma;
2884 	  bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
2885 				   + htab->plt_got_eh_frame->output_offset
2886 				   + PLT_FDE_START_OFFSET;
2887 	  bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2888 			     htab->plt_got_eh_frame->contents
2889 			     + PLT_FDE_START_OFFSET);
2890 	}
2891       if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
2892 	{
2893 	  if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2894 						 htab->plt_got_eh_frame,
2895 						 htab->plt_got_eh_frame->contents))
2896 	    return NULL;
2897 	}
2898     }
2899 
2900   /* Adjust .eh_frame for the second PLT section.  */
2901   if (htab->plt_second_eh_frame != NULL
2902       && htab->plt_second_eh_frame->contents != NULL)
2903     {
2904       if (htab->plt_second != NULL
2905 	  && htab->plt_second->size != 0
2906 	  && (htab->plt_second->flags & SEC_EXCLUDE) == 0
2907 	  && htab->plt_second->output_section != NULL
2908 	  && htab->plt_second_eh_frame->output_section != NULL)
2909 	{
2910 	  bfd_vma plt_start = htab->plt_second->output_section->vma;
2911 	  bfd_vma eh_frame_start
2912 	    = (htab->plt_second_eh_frame->output_section->vma
2913 	       + htab->plt_second_eh_frame->output_offset
2914 	       + PLT_FDE_START_OFFSET);
2915 	  bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2916 			     htab->plt_second_eh_frame->contents
2917 			     + PLT_FDE_START_OFFSET);
2918 	}
2919       if (htab->plt_second_eh_frame->sec_info_type
2920 	  == SEC_INFO_TYPE_EH_FRAME)
2921 	{
2922 	  if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2923 						 htab->plt_second_eh_frame,
2924 						 htab->plt_second_eh_frame->contents))
2925 	    return NULL;
2926 	}
2927     }
2928 
2929   /* Make any adjustment if necessary and merge .sframe section to
2930      create the final .sframe section for output_bfd.  */
2931   if (htab->plt_sframe != NULL
2932       && htab->plt_sframe->contents != NULL)
2933     {
2934       if (htab->elf.splt != NULL
2935 	  && htab->elf.splt->size != 0
2936 	  && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
2937 	  && htab->elf.splt->output_section != NULL
2938 	  && htab->plt_sframe->output_section != NULL)
2939 	{
2940 	  bfd_vma plt_start = htab->elf.splt->output_section->vma;
2941 	  bfd_vma sframe_start = htab->plt_sframe->output_section->vma
2942 				   + htab->plt_sframe->output_offset
2943 				   + PLT_SFRAME_FDE_START_OFFSET;
2944 #if 0 /* FIXME Testing only. Remove before review.  */
2945 	  bfd_vma test_value = (plt_start - sframe_start)
2946 	    + htab->plt_sframe->output_section->vma
2947 	    + htab->plt_sframe->output_offset
2948 	    + PLT_SFRAME_FDE_START_OFFSET;
2949 	  bfd_put_signed_32 (dynobj, test_value,
2950 #endif
2951 	  bfd_put_signed_32 (dynobj, plt_start - sframe_start,
2952 			     htab->plt_sframe->contents
2953 			     + PLT_SFRAME_FDE_START_OFFSET);
2954 	}
2955       if (htab->plt_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
2956 	{
2957 	  if (! _bfd_elf_merge_section_sframe (output_bfd, info,
2958 					       htab->plt_sframe,
2959 					       htab->plt_sframe->contents))
2960 	    return NULL;
2961 	}
2962     }
2963 
2964   if (htab->plt_second_sframe != NULL
2965       && htab->plt_second_sframe->contents != NULL)
2966     {
2967       if (htab->plt_second != NULL
2968 	  && htab->plt_second->size != 0
2969 	  && (htab->plt_second->flags & SEC_EXCLUDE) == 0
2970 	  && htab->plt_second->output_section != NULL
2971 	  && htab->plt_second_sframe->output_section != NULL)
2972 	{
2973 	  bfd_vma plt_start = htab->plt_second->output_section->vma;
2974 	  bfd_vma sframe_start
2975 	    = (htab->plt_second_sframe->output_section->vma
2976 	       + htab->plt_second_sframe->output_offset
2977 	       + PLT_SFRAME_FDE_START_OFFSET);
2978 #if 0 /* FIXME Testing only. Remove before review.  */
2979 	  bfd_vma test_value = (plt_start - sframe_start)
2980 	    + htab->plt_second_sframe->output_section->vma
2981 	    + htab->plt_second_sframe->output_offset
2982 	    + PLT_SFRAME_FDE_START_OFFSET;
2983 	  bfd_put_signed_32 (dynobj, test_value,
2984 #endif
2985 	  bfd_put_signed_32 (dynobj, plt_start - sframe_start,
2986 			     htab->plt_second_sframe->contents
2987 			     + PLT_SFRAME_FDE_START_OFFSET);
2988 	}
2989       if (htab->plt_second_sframe->sec_info_type == SEC_INFO_TYPE_SFRAME)
2990 	{
2991 	  if (! _bfd_elf_merge_section_sframe (output_bfd, info,
2992 					       htab->plt_second_sframe,
2993 					       htab->plt_second_sframe->contents))
2994 	    return NULL;
2995 	}
2996     }
2997   if (htab->elf.sgot && htab->elf.sgot->size > 0)
2998     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
2999       = htab->got_entry_size;
3000 
3001   return htab;
3002 }
3003 
3004 
3005 bool
3006 _bfd_x86_elf_always_size_sections (bfd *output_bfd,
3007 				   struct bfd_link_info *info)
3008 {
3009   asection *tls_sec = elf_hash_table (info)->tls_sec;
3010 
3011   if (tls_sec && !bfd_link_relocatable (info))
3012     {
3013       struct elf_link_hash_entry *tlsbase;
3014 
3015       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3016 				      "_TLS_MODULE_BASE_",
3017 				      false, false, false);
3018 
3019       if (tlsbase && tlsbase->type == STT_TLS)
3020 	{
3021 	  struct elf_x86_link_hash_table *htab;
3022 	  struct bfd_link_hash_entry *bh = NULL;
3023 	  const struct elf_backend_data *bed
3024 	    = get_elf_backend_data (output_bfd);
3025 
3026 	  htab = elf_x86_hash_table (info, bed->target_id);
3027 	  if (htab == NULL)
3028 	    return false;
3029 
3030 	  if (!(_bfd_generic_link_add_one_symbol
3031 		(info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3032 		 tls_sec, 0, NULL, false,
3033 		 bed->collect, &bh)))
3034 	    return false;
3035 
3036 	  htab->tls_module_base = bh;
3037 
3038 	  tlsbase = (struct elf_link_hash_entry *)bh;
3039 	  tlsbase->def_regular = 1;
3040 	  tlsbase->other = STV_HIDDEN;
3041 	  tlsbase->root.linker_def = 1;
3042 	  (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
3043 	}
3044     }
3045 
3046   return true;
3047 }
3048 
3049 void
3050 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3051 				     unsigned int st_other,
3052 				     bool definition,
3053 				     bool dynamic ATTRIBUTE_UNUSED)
3054 {
3055   if (definition)
3056     {
3057       struct elf_x86_link_hash_entry *eh
3058 	= (struct elf_x86_link_hash_entry *) h;
3059       eh->def_protected = ELF_ST_VISIBILITY (st_other) == STV_PROTECTED;
3060     }
3061 }
3062 
3063 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3064 
3065 void
3066 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
3067 				   struct elf_link_hash_entry *dir,
3068 				   struct elf_link_hash_entry *ind)
3069 {
3070   struct elf_x86_link_hash_entry *edir, *eind;
3071 
3072   edir = (struct elf_x86_link_hash_entry *) dir;
3073   eind = (struct elf_x86_link_hash_entry *) ind;
3074 
3075   if (ind->root.type == bfd_link_hash_indirect
3076       && dir->got.refcount <= 0)
3077     {
3078       edir->tls_type = eind->tls_type;
3079       eind->tls_type = GOT_UNKNOWN;
3080     }
3081 
3082   /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
3083      generate a R_386_COPY reloc.  */
3084   edir->gotoff_ref |= eind->gotoff_ref;
3085 
3086   edir->zero_undefweak |= eind->zero_undefweak;
3087 
3088   if (ELIMINATE_COPY_RELOCS
3089       && ind->root.type != bfd_link_hash_indirect
3090       && dir->dynamic_adjusted)
3091     {
3092       /* If called to transfer flags for a weakdef during processing
3093 	 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
3094 	 We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
3095       if (dir->versioned != versioned_hidden)
3096 	dir->ref_dynamic |= ind->ref_dynamic;
3097       dir->ref_regular |= ind->ref_regular;
3098       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
3099       dir->needs_plt |= ind->needs_plt;
3100       dir->pointer_equality_needed |= ind->pointer_equality_needed;
3101     }
3102   else
3103     _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3104 }
3105 
3106 /* Remove undefined weak symbol from the dynamic symbol table if it
3107    is resolved to 0.   */
3108 
3109 bool
3110 _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
3111 			   struct elf_link_hash_entry *h)
3112 {
3113   if (h->dynindx != -1
3114       && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, elf_x86_hash_entry (h)))
3115     {
3116       h->dynindx = -1;
3117       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3118 			      h->dynstr_index);
3119     }
3120   return true;
3121 }
3122 
3123 /* Change the STT_GNU_IFUNC symbol defined in position-dependent
3124    executable into the normal function symbol and set its address
3125    to its PLT entry, which should be resolved by R_*_IRELATIVE at
3126    run-time.  */
3127 
3128 void
3129 _bfd_x86_elf_link_fixup_ifunc_symbol (struct bfd_link_info *info,
3130 				      struct elf_x86_link_hash_table *htab,
3131 				      struct elf_link_hash_entry *h,
3132 				      Elf_Internal_Sym *sym)
3133 {
3134   if (bfd_link_pde (info)
3135       && h->def_regular
3136       && h->dynindx != -1
3137       && h->plt.offset != (bfd_vma) -1
3138       && h->type == STT_GNU_IFUNC)
3139     {
3140       asection *plt_s;
3141       bfd_vma plt_offset;
3142       bfd *output_bfd = info->output_bfd;
3143 
3144       if (htab->plt_second)
3145 	{
3146 	  struct elf_x86_link_hash_entry *eh
3147 	    = (struct elf_x86_link_hash_entry *) h;
3148 
3149 	  plt_s = htab->plt_second;
3150 	  plt_offset = eh->plt_second.offset;
3151 	}
3152       else
3153 	{
3154 	  plt_s = htab->elf.splt;
3155 	  plt_offset = h->plt.offset;
3156 	}
3157 
3158       sym->st_size = 0;
3159       sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
3160       sym->st_shndx
3161 	= _bfd_elf_section_from_bfd_section (output_bfd,
3162 					     plt_s->output_section);
3163       sym->st_value = (plt_s->output_section->vma
3164 		       + plt_s->output_offset + plt_offset);
3165     }
3166 }
3167 
3168 /* Report relative relocation.  */
3169 
3170 void
3171 _bfd_x86_elf_link_report_relative_reloc
3172   (struct bfd_link_info *info, asection *asect,
3173    struct elf_link_hash_entry *h, Elf_Internal_Sym *sym,
3174    const char *reloc_name, const void *reloc)
3175 {
3176   const char *name;
3177   bfd *abfd;
3178   const Elf_Internal_Rela *rel = (const Elf_Internal_Rela *) reloc;
3179 
3180   /* Use the output BFD for linker created sections.  */
3181   if ((asect->flags & SEC_LINKER_CREATED) != 0)
3182     abfd = info->output_bfd;
3183   else
3184     abfd = asect->owner;
3185 
3186   if (h != NULL && h->root.root.string != NULL)
3187     name = h->root.root.string;
3188   else
3189     name = bfd_elf_sym_name (abfd, &elf_symtab_hdr (abfd), sym, NULL);
3190 
3191   if (asect->use_rela_p)
3192     info->callbacks->einfo
3193       (_("%pB: %s (offset: 0x%v, info: 0x%v, addend: 0x%v) against "
3194 	 "'%s' " "for section '%pA' in %pB\n"),
3195        info->output_bfd, reloc_name, rel->r_offset, rel->r_info,
3196        rel->r_addend, name, asect, abfd);
3197   else
3198     info->callbacks->einfo
3199       (_("%pB: %s (offset: 0x%v, info: 0x%v) against '%s' for section "
3200 	 "'%pA' in %pB\n"),
3201        info->output_bfd, reloc_name, rel->r_offset, rel->r_info, name,
3202        asect, abfd);
3203 }
3204 
3205 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
3206 
3207 bool
3208 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
3209 {
3210   if (h->plt.offset != (bfd_vma) -1
3211       && !h->def_regular
3212       && !h->pointer_equality_needed)
3213     return false;
3214 
3215   return _bfd_elf_hash_symbol (h);
3216 }
3217 
3218 /* Adjust a symbol defined by a dynamic object and referenced by a
3219    regular object.  The current definition is in some section of the
3220    dynamic object, but we're not including those sections.  We have to
3221    change the definition to something the rest of the link can
3222    understand.  */
3223 
3224 bool
3225 _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3226 				    struct elf_link_hash_entry *h)
3227 {
3228   struct elf_x86_link_hash_table *htab;
3229   asection *s, *srel;
3230   struct elf_x86_link_hash_entry *eh;
3231   struct elf_dyn_relocs *p;
3232   const struct elf_backend_data *bed
3233     = get_elf_backend_data (info->output_bfd);
3234 
3235   eh = (struct elf_x86_link_hash_entry *) h;
3236 
3237   /* Clear GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS if it is turned
3238      on by an input relocatable file and there is a non-GOT/non-PLT
3239      reference from another relocatable file without it.
3240      NB: There can be non-GOT reference in data sections in input with
3241      GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS.  */
3242   if (eh->non_got_ref_without_indirect_extern_access
3243       && info->indirect_extern_access == 1
3244       && bfd_link_executable (info))
3245     {
3246       unsigned int needed_1;
3247       info->indirect_extern_access = 0;
3248       /* Turn off nocopyreloc if implied by indirect_extern_access.  */
3249       if (info->nocopyreloc == 2)
3250 	info->nocopyreloc = 0;
3251       needed_1 = bfd_h_get_32 (info->output_bfd, info->needed_1_p);
3252       needed_1 &= ~GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
3253       bfd_h_put_32 (info->output_bfd, needed_1, info->needed_1_p);
3254     }
3255 
3256   /* STT_GNU_IFUNC symbol must go through PLT. */
3257   if (h->type == STT_GNU_IFUNC)
3258     {
3259       /* All local STT_GNU_IFUNC references must be treate as local
3260 	 calls via local PLT.  */
3261       if (h->ref_regular
3262 	  && SYMBOL_CALLS_LOCAL (info, h))
3263 	{
3264 	  bfd_size_type pc_count = 0, count = 0;
3265 	  struct elf_dyn_relocs **pp;
3266 
3267 	  eh = (struct elf_x86_link_hash_entry *) h;
3268 	  for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
3269 	    {
3270 	      pc_count += p->pc_count;
3271 	      p->count -= p->pc_count;
3272 	      p->pc_count = 0;
3273 	      count += p->count;
3274 	      if (p->count == 0)
3275 		*pp = p->next;
3276 	      else
3277 		pp = &p->next;
3278 	    }
3279 
3280 	  if (pc_count || count)
3281 	    {
3282 	      h->non_got_ref = 1;
3283 	      if (pc_count)
3284 		{
3285 		  /* Increment PLT reference count only for PC-relative
3286 		     references.  */
3287 		  h->needs_plt = 1;
3288 		  if (h->plt.refcount <= 0)
3289 		    h->plt.refcount = 1;
3290 		  else
3291 		    h->plt.refcount += 1;
3292 		}
3293 	    }
3294 
3295 	  /* GOTOFF relocation needs PLT.  */
3296 	  if (eh->gotoff_ref)
3297 	    h->plt.refcount = 1;
3298 	}
3299 
3300       if (h->plt.refcount <= 0)
3301 	{
3302 	  h->plt.offset = (bfd_vma) -1;
3303 	  h->needs_plt = 0;
3304 	}
3305       return true;
3306     }
3307 
3308   /* If this is a function, put it in the procedure linkage table.  We
3309      will fill in the contents of the procedure linkage table later,
3310      when we know the address of the .got section.  */
3311   if (h->type == STT_FUNC
3312       || h->needs_plt)
3313     {
3314       if (h->plt.refcount <= 0
3315 	  || SYMBOL_CALLS_LOCAL (info, h)
3316 	  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3317 	      && h->root.type == bfd_link_hash_undefweak))
3318 	{
3319 	  /* This case can occur if we saw a PLT32 reloc in an input
3320 	     file, but the symbol was never referred to by a dynamic
3321 	     object, or if all references were garbage collected.  In
3322 	     such a case, we don't actually need to build a procedure
3323 	     linkage table, and we can just do a PC32 reloc instead.  */
3324 	  h->plt.offset = (bfd_vma) -1;
3325 	  h->needs_plt = 0;
3326 	}
3327 
3328       return true;
3329     }
3330   else
3331     /* It's possible that we incorrectly decided a .plt reloc was needed
3332      * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
3333        check_relocs.  We can't decide accurately between function and
3334        non-function syms in check-relocs;  Objects loaded later in
3335        the link may change h->type.  So fix it now.  */
3336     h->plt.offset = (bfd_vma) -1;
3337 
3338   /* If this is a weak symbol, and there is a real definition, the
3339      processor independent code will have arranged for us to see the
3340      real definition first, and we can just use the same value.  */
3341   if (h->is_weakalias)
3342     {
3343       struct elf_link_hash_entry *def = weakdef (h);
3344       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3345       h->root.u.def.section = def->root.u.def.section;
3346       h->root.u.def.value = def->root.u.def.value;
3347       if (ELIMINATE_COPY_RELOCS
3348 	  || info->nocopyreloc
3349 	  || SYMBOL_NO_COPYRELOC (info, eh))
3350 	{
3351 	  /* NB: needs_copy is always 0 for i386.  */
3352 	  h->non_got_ref = def->non_got_ref;
3353 	  eh->needs_copy = def->needs_copy;
3354 	}
3355       return true;
3356     }
3357 
3358   /* This is a reference to a symbol defined by a dynamic object which
3359      is not a function.  */
3360 
3361   /* If we are creating a shared library, we must presume that the
3362      only references to the symbol are via the global offset table.
3363      For such cases we need not do anything here; the relocations will
3364      be handled correctly by relocate_section.  */
3365   if (!bfd_link_executable (info))
3366     return true;
3367 
3368   /* If there are no references to this symbol that do not use the
3369      GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
3370      reloc.  NB: gotoff_ref is always 0 for x86-64.  */
3371   if (!h->non_got_ref && !eh->gotoff_ref)
3372     return true;
3373 
3374   /* If -z nocopyreloc was given, we won't generate them either.  */
3375   if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
3376     {
3377       h->non_got_ref = 0;
3378       return true;
3379     }
3380 
3381   htab = elf_x86_hash_table (info, bed->target_id);
3382   if (htab == NULL)
3383     return false;
3384 
3385   /* If there aren't any dynamic relocs in read-only sections nor
3386      R_386_GOTOFF relocation, then we can keep the dynamic relocs and
3387      avoid the copy reloc.  This doesn't work on VxWorks, where we can
3388      not have dynamic relocations (other than copy and jump slot
3389      relocations) in an executable.  */
3390   if (ELIMINATE_COPY_RELOCS
3391       && (bed->target_id == X86_64_ELF_DATA
3392 	  || (!eh->gotoff_ref
3393 	      && htab->elf.target_os != is_vxworks)))
3394     {
3395       /* If we don't find any dynamic relocs in read-only sections,
3396 	 then we'll be keeping the dynamic relocs and avoiding the copy
3397 	 reloc.  */
3398       if (!_bfd_elf_readonly_dynrelocs (h))
3399 	{
3400 	  h->non_got_ref = 0;
3401 	  return true;
3402 	}
3403     }
3404 
3405   /* We must allocate the symbol in our .dynbss section, which will
3406      become part of the .bss section of the executable.  There will be
3407      an entry for this symbol in the .dynsym section.  The dynamic
3408      object will contain position independent code, so all references
3409      from the dynamic object to this symbol will go through the global
3410      offset table.  The dynamic linker will use the .dynsym entry to
3411      determine the address it must put in the global offset table, so
3412      both the dynamic object and the regular object will refer to the
3413      same memory location for the variable.  */
3414 
3415   /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
3416      dynamic linker to copy the initial value out of the dynamic object
3417      and into the runtime process image.  */
3418   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
3419     {
3420       s = htab->elf.sdynrelro;
3421       srel = htab->elf.sreldynrelro;
3422     }
3423   else
3424     {
3425       s = htab->elf.sdynbss;
3426       srel = htab->elf.srelbss;
3427     }
3428   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3429     {
3430       if (eh->def_protected && bfd_link_executable (info))
3431 	for (p = h->dyn_relocs; p != NULL; p = p->next)
3432 	  {
3433 	    /* Disallow copy relocation against non-copyable protected
3434 	       symbol.  */
3435 	    s = p->sec->output_section;
3436 	    if (s != NULL && (s->flags & SEC_READONLY) != 0)
3437 	      {
3438 		info->callbacks->einfo
3439 		  /* xgettext:c-format */
3440 		  (_("%F%P: %pB: copy relocation against non-copyable "
3441 		     "protected symbol `%s' in %pB\n"),
3442 		   p->sec->owner, h->root.root.string,
3443 		   h->root.u.def.section->owner);
3444 		return false;
3445 	      }
3446 	  }
3447 
3448       srel->size += htab->sizeof_reloc;
3449       h->needs_copy = 1;
3450     }
3451 
3452   return _bfd_elf_adjust_dynamic_copy (info, h, s);
3453 }
3454 
3455 void
3456 _bfd_x86_elf_hide_symbol (struct bfd_link_info *info,
3457 			  struct elf_link_hash_entry *h,
3458 			  bool force_local)
3459 {
3460   if (h->root.type == bfd_link_hash_undefweak
3461       && info->nointerp
3462       && bfd_link_pie (info))
3463     {
3464       /* When there is no dynamic interpreter in PIE, make the undefined
3465 	 weak symbol dynamic so that PC relative branch to the undefined
3466 	 weak symbol will land to address 0.  */
3467       struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
3468       if (h->plt.refcount > 0
3469 	  || eh->plt_got.refcount > 0)
3470 	return;
3471     }
3472 
3473   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3474 }
3475 
3476 /* Return TRUE if a symbol is referenced locally.  It is similar to
3477    SYMBOL_REFERENCES_LOCAL, but it also checks version script.  It
3478    works in check_relocs.  */
3479 
3480 bool
3481 _bfd_x86_elf_link_symbol_references_local (struct bfd_link_info *info,
3482 					   struct elf_link_hash_entry *h)
3483 {
3484   struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
3485   struct elf_x86_link_hash_table *htab
3486     = (struct elf_x86_link_hash_table *) info->hash;
3487 
3488   if (eh->local_ref > 1)
3489     return true;
3490 
3491   if (eh->local_ref == 1)
3492     return false;
3493 
3494   /* Unversioned symbols defined in regular objects can be forced local
3495      by linker version script.  A weak undefined symbol is forced local
3496      if
3497      1. It has non-default visibility.  Or
3498      2. When building executable, there is no dynamic linker.  Or
3499      3. or "-z nodynamic-undefined-weak" is used.
3500    */
3501   if (_bfd_elf_symbol_refs_local_p (h, info, 1)
3502       || (h->root.type == bfd_link_hash_undefweak
3503 	  && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3504 	      || (bfd_link_executable (info)
3505 		  && htab->interp == NULL)
3506 	      || info->dynamic_undefined_weak == 0))
3507       || ((h->def_regular || ELF_COMMON_DEF_P (h))
3508 	  && info->version_info != NULL
3509 	  && _bfd_elf_link_hide_sym_by_version (info, h)))
3510     {
3511       eh->local_ref = 2;
3512       return true;
3513     }
3514 
3515   eh->local_ref = 1;
3516   return false;
3517 }
3518 
3519 /* Return the section that should be marked against GC for a given
3520    relocation.	*/
3521 
3522 asection *
3523 _bfd_x86_elf_gc_mark_hook (asection *sec,
3524 			   struct bfd_link_info *info,
3525 			   Elf_Internal_Rela *rel,
3526 			   struct elf_link_hash_entry *h,
3527 			   Elf_Internal_Sym *sym)
3528 {
3529   /* Compiler should optimize this out.  */
3530   if (((unsigned int) R_X86_64_GNU_VTINHERIT
3531        != (unsigned int) R_386_GNU_VTINHERIT)
3532       || ((unsigned int) R_X86_64_GNU_VTENTRY
3533 	  != (unsigned int) R_386_GNU_VTENTRY))
3534     abort ();
3535 
3536   if (h != NULL)
3537     switch (ELF32_R_TYPE (rel->r_info))
3538       {
3539       case R_X86_64_GNU_VTINHERIT:
3540       case R_X86_64_GNU_VTENTRY:
3541 	return NULL;
3542       }
3543 
3544   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
3545 }
3546 
3547 static bfd_vma
3548 elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
3549 			  bfd_vma off,
3550 			  bfd_vma offset ATTRIBUTE_UNUSED,
3551 			  bfd_vma got_addr)
3552 {
3553   return got_addr + off;
3554 }
3555 
3556 static bfd_vma
3557 elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
3558 			    bfd_vma off,
3559 			    bfd_vma offset,
3560 			    bfd_vma got_addr ATTRIBUTE_UNUSED)
3561 {
3562   return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
3563 }
3564 
3565 static bool
3566 elf_i386_valid_plt_reloc_p (unsigned int type)
3567 {
3568   return (type == R_386_JUMP_SLOT
3569 	  || type == R_386_GLOB_DAT
3570 	  || type == R_386_IRELATIVE);
3571 }
3572 
3573 static bool
3574 elf_x86_64_valid_plt_reloc_p (unsigned int type)
3575 {
3576   return (type == R_X86_64_JUMP_SLOT
3577 	  || type == R_X86_64_GLOB_DAT
3578 	  || type == R_X86_64_IRELATIVE);
3579 }
3580 
3581 long
3582 _bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
3583 				   long count,
3584 				   long relsize,
3585 				   bfd_vma got_addr,
3586 				   struct elf_x86_plt plts[],
3587 				   asymbol **dynsyms,
3588 				   asymbol **ret)
3589 {
3590   long size, i, n, len;
3591   int j;
3592   unsigned int plt_got_offset, plt_entry_size;
3593   asymbol *s;
3594   bfd_byte *plt_contents;
3595   long dynrelcount;
3596   arelent **dynrelbuf, *p;
3597   char *names;
3598   const struct elf_backend_data *bed;
3599   bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
3600 			      bfd_vma);
3601   bool (*valid_plt_reloc_p) (unsigned int);
3602   unsigned int jump_slot_reloc;
3603 
3604   dynrelbuf = NULL;
3605   if (count == 0)
3606     goto bad_return;
3607 
3608   dynrelbuf = (arelent **) bfd_malloc (relsize);
3609   if (dynrelbuf == NULL)
3610     goto bad_return;
3611 
3612   dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
3613 						dynsyms);
3614   if (dynrelcount <= 0)
3615     goto bad_return;
3616 
3617   /* Sort the relocs by address.  */
3618   qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
3619 	 _bfd_x86_elf_compare_relocs);
3620 
3621   size = count * sizeof (asymbol);
3622 
3623   /* Allocate space for @plt suffixes.  */
3624   n = 0;
3625   for (i = 0; i < dynrelcount; i++)
3626     {
3627       p = dynrelbuf[i];
3628       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3629       if (p->addend != 0)
3630 	size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
3631     }
3632 
3633   s = *ret = (asymbol *) bfd_zmalloc (size);
3634   if (s == NULL)
3635     goto bad_return;
3636 
3637   bed = get_elf_backend_data (abfd);
3638 
3639   if (bed->target_id == X86_64_ELF_DATA)
3640     {
3641       get_plt_got_vma = elf_x86_64_get_plt_got_vma;
3642       valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
3643       jump_slot_reloc = R_X86_64_JUMP_SLOT;
3644     }
3645   else
3646     {
3647       get_plt_got_vma = elf_i386_get_plt_got_vma;
3648       valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
3649       jump_slot_reloc = R_386_JUMP_SLOT;
3650       if (got_addr)
3651 	{
3652 	  /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
3653 	     address.  */
3654 	  asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
3655 	  if (sec != NULL)
3656 	    got_addr = sec->vma;
3657 	  else
3658 	    {
3659 	      sec = bfd_get_section_by_name (abfd, ".got");
3660 	      if (sec != NULL)
3661 		got_addr = sec->vma;
3662 	    }
3663 
3664 	  if (got_addr == (bfd_vma) -1)
3665 	    goto bad_return;
3666 	}
3667     }
3668 
3669   /* Check for each PLT section.  */
3670   names = (char *) (s + count);
3671   size = 0;
3672   n = 0;
3673   for (j = 0; plts[j].name != NULL; j++)
3674     if ((plt_contents = plts[j].contents) != NULL)
3675       {
3676 	long k;
3677 	bfd_vma offset;
3678 	asection *plt;
3679 	struct elf_x86_plt *plt_p = &plts[j];
3680 
3681 	plt_got_offset = plt_p->plt_got_offset;
3682 	plt_entry_size = plt_p->plt_entry_size;
3683 
3684 	plt = plt_p->sec;
3685 
3686 	if ((plt_p->type & plt_lazy))
3687 	  {
3688 	    /* Skip PLT0 in lazy PLT.  */
3689 	    k = 1;
3690 	    offset = plt_entry_size;
3691 	  }
3692 	else
3693 	  {
3694 	    k = 0;
3695 	    offset = 0;
3696 	  }
3697 
3698 	/* Check each PLT entry against dynamic relocations.  */
3699 	for (; k < plt_p->count; k++)
3700 	  {
3701 	    int off;
3702 	    bfd_vma got_vma;
3703 	    long min, max, mid;
3704 
3705 	    /* Get the GOT offset for i386 or the PC-relative offset
3706 	       for x86-64, a signed 32-bit integer.  */
3707 	    off = H_GET_32 (abfd, (plt_contents + offset
3708 				   + plt_got_offset));
3709 	    got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
3710 
3711 	    /* Binary search.  */
3712 	    p = dynrelbuf[0];
3713 	    min = 0;
3714 	    max = dynrelcount;
3715 	    while ((min + 1) < max)
3716 	      {
3717 		arelent *r;
3718 
3719 		mid = (min + max) / 2;
3720 		r = dynrelbuf[mid];
3721 		if (got_vma > r->address)
3722 		  min = mid;
3723 		else if (got_vma < r->address)
3724 		  max = mid;
3725 		else
3726 		  {
3727 		    p = r;
3728 		    break;
3729 		  }
3730 	      }
3731 
3732 	    /* Skip unknown relocation.  PR 17512: file: bc9d6cf5.  */
3733 	    if (got_vma == p->address
3734 		&& p->howto != NULL
3735 		&& valid_plt_reloc_p (p->howto->type))
3736 	      {
3737 		*s = **p->sym_ptr_ptr;
3738 		/* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
3739 		   set.  Since we are defining a symbol, ensure one
3740 		   of them is set.  */
3741 		if ((s->flags & BSF_LOCAL) == 0)
3742 		  s->flags |= BSF_GLOBAL;
3743 		s->flags |= BSF_SYNTHETIC;
3744 		/* This is no longer a section symbol.  */
3745 		s->flags &= ~BSF_SECTION_SYM;
3746 		s->section = plt;
3747 		s->the_bfd = plt->owner;
3748 		s->value = offset;
3749 		s->udata.p = NULL;
3750 		s->name = names;
3751 		len = strlen ((*p->sym_ptr_ptr)->name);
3752 		memcpy (names, (*p->sym_ptr_ptr)->name, len);
3753 		names += len;
3754 		/* There may be JUMP_SLOT and IRELATIVE relocations.
3755 		   JUMP_SLOT r_addend should be ignored.  */
3756 		if (p->addend != 0 && p->howto->type != jump_slot_reloc)
3757 		  {
3758 		    char buf[30], *a;
3759 
3760 		    memcpy (names, "+0x", sizeof ("+0x") - 1);
3761 		    names += sizeof ("+0x") - 1;
3762 		    bfd_sprintf_vma (abfd, buf, p->addend);
3763 		    for (a = buf; *a == '0'; ++a)
3764 		      ;
3765 		    size = strlen (a);
3766 		    memcpy (names, a, size);
3767 		    names += size;
3768 		  }
3769 		memcpy (names, "@plt", sizeof ("@plt"));
3770 		names += sizeof ("@plt");
3771 		n++;
3772 		s++;
3773 		/* There should be only one entry in PLT for a given
3774 		   symbol.  Set howto to NULL after processing a PLT
3775 		   entry to guard against corrupted PLT.  */
3776 		p->howto = NULL;
3777 	      }
3778 	    offset += plt_entry_size;
3779 	  }
3780       }
3781 
3782   /* PLT entries with R_386_TLS_DESC relocations are skipped.  */
3783   if (n == 0)
3784     {
3785     bad_return:
3786       count = -1;
3787     }
3788   else
3789     count = n;
3790 
3791   for (j = 0; plts[j].name != NULL; j++)
3792     free (plts[j].contents);
3793 
3794   free (dynrelbuf);
3795 
3796   return count;
3797 }
3798 
3799 /* Parse x86 GNU properties.  */
3800 
3801 enum elf_property_kind
3802 _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
3803 				   bfd_byte *ptr, unsigned int datasz)
3804 {
3805   elf_property *prop;
3806 
3807   if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3808       || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3809       || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
3810 	  && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3811       || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
3812 	  && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
3813       || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3814 	  && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3815     {
3816       if (datasz != 4)
3817 	{
3818 	  _bfd_error_handler
3819 	    (_("error: %pB: <corrupt x86 property (0x%x) size: 0x%x>"),
3820 	     abfd, type, datasz);
3821 	  return property_corrupt;
3822 	}
3823       prop = _bfd_elf_get_property (abfd, type, datasz);
3824       prop->u.number |= bfd_h_get_32 (abfd, ptr);
3825       prop->pr_kind = property_number;
3826       return property_number;
3827     }
3828 
3829   return property_ignored;
3830 }
3831 
3832 /* Merge x86 GNU property BPROP with APROP.  If APROP isn't NULL,
3833    return TRUE if APROP is updated.  Otherwise, return TRUE if BPROP
3834    should be merged with ABFD.  */
3835 
3836 bool
3837 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
3838 				   bfd *abfd ATTRIBUTE_UNUSED,
3839 				   bfd *bbfd ATTRIBUTE_UNUSED,
3840 				   elf_property *aprop,
3841 				   elf_property *bprop)
3842 {
3843   unsigned int number, features;
3844   bool updated = false;
3845   const struct elf_backend_data *bed;
3846   struct elf_x86_link_hash_table *htab;
3847   unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
3848 
3849   if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3850       || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3851 	  && pr_type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3852     {
3853       if (aprop == NULL || bprop == NULL)
3854 	{
3855 	  /* Only one of APROP and BPROP can be NULL.  */
3856 	  if (aprop != NULL)
3857 	    {
3858 	      /* Remove this property since the other input file doesn't
3859 		 have it.  */
3860 	      aprop->pr_kind = property_remove;
3861 	      updated = true;
3862 	    }
3863 	}
3864       else
3865 	{
3866 	  number = aprop->u.number;
3867 	  aprop->u.number = number | bprop->u.number;
3868 	  updated = number != (unsigned int) aprop->u.number;
3869 	}
3870       return updated;
3871     }
3872   else if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3873 	   || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_LO
3874 	       && pr_type <= GNU_PROPERTY_X86_UINT32_OR_HI))
3875     {
3876       features = 0;
3877       if (pr_type == GNU_PROPERTY_X86_ISA_1_NEEDED)
3878 	{
3879 	  bed = get_elf_backend_data (info->output_bfd);
3880 	  htab = elf_x86_hash_table (info, bed->target_id);
3881 	  switch (htab->params->isa_level)
3882 	    {
3883 	    case 0:
3884 	      break;
3885 	    case 2:
3886 	      features = GNU_PROPERTY_X86_ISA_1_V2;
3887 	      break;
3888 	    case 3:
3889 	      features = GNU_PROPERTY_X86_ISA_1_V3;
3890 	      break;
3891 	    case 4:
3892 	      features = GNU_PROPERTY_X86_ISA_1_V4;
3893 	      break;
3894 	    default:
3895 	      abort ();
3896 	    }
3897 	}
3898       if (aprop != NULL && bprop != NULL)
3899 	{
3900 	  number = aprop->u.number;
3901 	  aprop->u.number = number | bprop->u.number | features;
3902 	  /* Remove the property if all bits are empty.  */
3903 	  if (aprop->u.number == 0)
3904 	    {
3905 	      aprop->pr_kind = property_remove;
3906 	      updated = true;
3907 	    }
3908 	  else
3909 	    updated = number != (unsigned int) aprop->u.number;
3910 	}
3911       else
3912 	{
3913 	  /* Only one of APROP and BPROP can be NULL.  */
3914 	  if (aprop != NULL)
3915 	    {
3916 	      aprop->u.number |= features;
3917 	      if (aprop->u.number == 0)
3918 		{
3919 		  /* Remove APROP if all bits are empty.  */
3920 		  aprop->pr_kind = property_remove;
3921 		  updated = true;
3922 		}
3923 	    }
3924 	  else
3925 	    {
3926 	      /* Return TRUE if APROP is NULL and all bits of BPROP
3927 		 aren't empty to indicate that BPROP should be added
3928 		 to ABFD.  */
3929 	      bprop->u.number |= features;
3930 	      updated = bprop->u.number != 0;
3931 	    }
3932 	}
3933       return updated;
3934     }
3935   else if (pr_type >= GNU_PROPERTY_X86_UINT32_AND_LO
3936 	   && pr_type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3937     {
3938       /* Only one of APROP and BPROP can be NULL:
3939 	 1. APROP & BPROP when both APROP and BPROP aren't NULL.
3940 	 2. If APROP is NULL, remove x86 feature.
3941 	 3. Otherwise, do nothing.
3942        */
3943       bed = get_elf_backend_data (info->output_bfd);
3944       htab = elf_x86_hash_table (info, bed->target_id);
3945       if (!htab)
3946 	abort ();
3947       if (aprop != NULL && bprop != NULL)
3948 	{
3949 	  number = aprop->u.number;
3950 	  aprop->u.number = number & bprop->u.number;
3951 	  if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
3952 	    {
3953 	      features = 0;
3954 	      if (htab->params->ibt)
3955 		features = GNU_PROPERTY_X86_FEATURE_1_IBT;
3956 	      if (htab->params->shstk)
3957 		features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
3958 	      if (htab->params->lam_u48)
3959 		features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
3960 			     | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
3961 	      else if (htab->params->lam_u57)
3962 		features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
3963 	      /* Add GNU_PROPERTY_X86_FEATURE_1_IBT,
3964 		 GNU_PROPERTY_X86_FEATURE_1_SHSTK,
3965 		 GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
3966 		 GNU_PROPERTY_X86_FEATURE_1_LAM_U57.  */
3967 	      aprop->u.number |= features;
3968 	    }
3969 	  updated = number != (unsigned int) aprop->u.number;
3970 	  /* Remove the property if all feature bits are cleared.  */
3971 	  if (aprop->u.number == 0)
3972 	    aprop->pr_kind = property_remove;
3973 	}
3974       else
3975 	{
3976 	  /* There should be no AND properties since some input doesn't
3977 	     have them.  Set IBT and SHSTK properties for -z ibt and -z
3978 	     shstk if needed.  */
3979 	  features = 0;
3980 	  if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
3981 	    {
3982 	      if (htab->params->ibt)
3983 		features = GNU_PROPERTY_X86_FEATURE_1_IBT;
3984 	      if (htab->params->shstk)
3985 		features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
3986 	      if (htab->params->lam_u48)
3987 		features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
3988 			     | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
3989 	      else if (htab->params->lam_u57)
3990 		features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
3991 	    }
3992 	  if (features)
3993 	    {
3994 	      if (aprop != NULL)
3995 		{
3996 		  updated = features != (unsigned int) aprop->u.number;
3997 		  aprop->u.number = features;
3998 		}
3999 	      else
4000 		{
4001 		  updated = true;
4002 		  bprop->u.number = features;
4003 		}
4004 	    }
4005 	  else if (aprop != NULL)
4006 	    {
4007 	      aprop->pr_kind = property_remove;
4008 	      updated = true;
4009 	    }
4010 	}
4011       return updated;
4012     }
4013   else
4014     {
4015       /* Never should happen.  */
4016       abort ();
4017     }
4018 
4019   return updated;
4020 }
4021 
4022 /* Set up x86 GNU properties.  Return the first relocatable ELF input
4023    with GNU properties if found.  Otherwise, return NULL.  */
4024 
4025 bfd *
4026 _bfd_x86_elf_link_setup_gnu_properties
4027   (struct bfd_link_info *info, struct elf_x86_init_table *init_table)
4028 {
4029   bool normal_target;
4030   bool lazy_plt;
4031   asection *sec, *pltsec;
4032   bfd *dynobj;
4033   bool use_ibt_plt;
4034   unsigned int plt_alignment, features, isa_level;
4035   struct elf_x86_link_hash_table *htab;
4036   bfd *pbfd;
4037   bfd *ebfd = NULL;
4038   elf_property *prop;
4039   const struct elf_backend_data *bed;
4040   unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
4041   unsigned int got_align;
4042 
4043   /* Find a normal input file with GNU property note.  */
4044   for (pbfd = info->input_bfds;
4045        pbfd != NULL;
4046        pbfd = pbfd->link.next)
4047     if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
4048 	&& bfd_count_sections (pbfd) != 0)
4049       {
4050 	ebfd = pbfd;
4051 
4052 	if (elf_properties (pbfd) != NULL)
4053 	  break;
4054       }
4055 
4056   bed = get_elf_backend_data (info->output_bfd);
4057 
4058   htab = elf_x86_hash_table (info, bed->target_id);
4059   if (htab == NULL)
4060     return pbfd;
4061 
4062   features = 0;
4063   if (htab->params->ibt)
4064     {
4065       features = GNU_PROPERTY_X86_FEATURE_1_IBT;
4066       htab->params->cet_report &= ~prop_report_ibt;
4067     }
4068   if (htab->params->shstk)
4069     {
4070       features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
4071       htab->params->cet_report &= ~prop_report_shstk;
4072     }
4073   if (!(htab->params->cet_report & (prop_report_ibt | prop_report_shstk)))
4074     htab->params->cet_report = prop_report_none;
4075   if (htab->params->lam_u48)
4076     {
4077       features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4078 		   | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4079       htab->params->lam_u48_report = prop_report_none;
4080       htab->params->lam_u57_report = prop_report_none;
4081     }
4082   else if (htab->params->lam_u57)
4083     {
4084       features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
4085       htab->params->lam_u57_report = prop_report_none;
4086     }
4087 
4088   switch (htab->params->isa_level)
4089     {
4090     case 0:
4091       isa_level = 0;
4092       break;
4093     case 1:
4094       isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE;
4095       break;
4096     case 2:
4097       isa_level = GNU_PROPERTY_X86_ISA_1_V2;
4098       break;
4099     case 3:
4100       isa_level = GNU_PROPERTY_X86_ISA_1_V3;
4101       break;
4102     case 4:
4103       isa_level = GNU_PROPERTY_X86_ISA_1_V4;
4104       break;
4105     default:
4106       abort ();
4107     }
4108 
4109   if (ebfd != NULL)
4110     {
4111       prop = NULL;
4112       if (features)
4113 	{
4114 	  /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT,
4115 	     GNU_PROPERTY_X86_FEATURE_1_SHSTK,
4116 	     GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
4117 	     GNU_PROPERTY_X86_FEATURE_1_LAM_U57.  */
4118 	  prop = _bfd_elf_get_property (ebfd,
4119 					GNU_PROPERTY_X86_FEATURE_1_AND,
4120 					4);
4121 	  prop->u.number |= features;
4122 	  prop->pr_kind = property_number;
4123 	}
4124 
4125       if (isa_level)
4126 	{
4127 	  /* If ISA level is set, add GNU_PROPERTY_X86_ISA_1_NEEDED.  */
4128 	  prop = _bfd_elf_get_property (ebfd,
4129 					GNU_PROPERTY_X86_ISA_1_NEEDED,
4130 					4);
4131 	  prop->u.number |= isa_level;
4132 	  prop->pr_kind = property_number;
4133 	}
4134 
4135       /* Create the GNU property note section if needed.  */
4136       if (prop != NULL && pbfd == NULL)
4137 	{
4138 	  sec = bfd_make_section_with_flags (ebfd,
4139 					     NOTE_GNU_PROPERTY_SECTION_NAME,
4140 					     (SEC_ALLOC
4141 					      | SEC_LOAD
4142 					      | SEC_IN_MEMORY
4143 					      | SEC_READONLY
4144 					      | SEC_HAS_CONTENTS
4145 					      | SEC_DATA));
4146 	  if (sec == NULL)
4147 	    info->callbacks->einfo (_("%F%P: failed to create GNU property section\n"));
4148 
4149 	  if (!bfd_set_section_alignment (sec, class_align))
4150 	    {
4151 	    error_alignment:
4152 	      info->callbacks->einfo (_("%F%pA: failed to align section\n"),
4153 				      sec);
4154 	    }
4155 
4156 	  elf_section_type (sec) = SHT_NOTE;
4157 	}
4158     }
4159 
4160   if (htab->params->cet_report
4161       || htab->params->lam_u48_report
4162       || htab->params->lam_u57_report)
4163     {
4164       /* Report missing IBT, SHSTK and LAM properties.  */
4165       bfd *abfd;
4166       const char *warning_msg = _("%P: %pB: warning: missing %s\n");
4167       const char *error_msg = _("%X%P: %pB: error: missing %s\n");
4168       const char *cet_msg = NULL;
4169       const char *lam_u48_msg = NULL;
4170       const char *lam_u57_msg = NULL;
4171       const char *missing;
4172       elf_property_list *p;
4173       bool missing_ibt, missing_shstk;
4174       bool missing_lam_u48, missing_lam_u57;
4175       bool check_ibt
4176 	= (htab->params->cet_report
4177 	   && (htab->params->cet_report & prop_report_ibt));
4178       bool check_shstk
4179 	= (htab->params->cet_report
4180 	   && (htab->params->cet_report & prop_report_shstk));
4181 
4182       if (htab->params->cet_report)
4183 	{
4184 	  if ((htab->params->cet_report & prop_report_warning))
4185 	    cet_msg = warning_msg;
4186 	  else
4187 	    cet_msg = error_msg;
4188 	}
4189       if (htab->params->lam_u48_report)
4190 	{
4191 	  if ((htab->params->lam_u48_report & prop_report_warning))
4192 	    lam_u48_msg = warning_msg;
4193 	  else
4194 	    lam_u48_msg = error_msg;
4195 	}
4196       if (htab->params->lam_u57_report)
4197 	{
4198 	  if ((htab->params->lam_u57_report & prop_report_warning))
4199 	    lam_u57_msg = warning_msg;
4200 	  else
4201 	    lam_u57_msg = error_msg;
4202 	}
4203 
4204       for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
4205 	if (!(abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED))
4206 	    && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
4207 	  {
4208 	    for (p = elf_properties (abfd); p; p = p->next)
4209 	      if (p->property.pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
4210 		break;
4211 
4212 	    missing_ibt = check_ibt;
4213 	    missing_shstk = check_shstk;
4214 	    missing_lam_u48 = !!lam_u48_msg;
4215 	    missing_lam_u57 = !!lam_u57_msg;
4216 	    if (p)
4217 	      {
4218 		missing_ibt &= !(p->property.u.number
4219 				 & GNU_PROPERTY_X86_FEATURE_1_IBT);
4220 		missing_shstk &= !(p->property.u.number
4221 				   & GNU_PROPERTY_X86_FEATURE_1_SHSTK);
4222 		missing_lam_u48 &= !(p->property.u.number
4223 				     & GNU_PROPERTY_X86_FEATURE_1_LAM_U48);
4224 		missing_lam_u57 &= !(p->property.u.number
4225 				     & GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4226 	      }
4227 	    if (missing_ibt || missing_shstk)
4228 	      {
4229 		if (missing_ibt && missing_shstk)
4230 		  missing = _("IBT and SHSTK properties");
4231 		else if (missing_ibt)
4232 		  missing = _("IBT property");
4233 		else
4234 		  missing = _("SHSTK property");
4235 		info->callbacks->einfo (cet_msg, abfd, missing);
4236 	      }
4237 	    if (missing_lam_u48)
4238 	      {
4239 		missing = _("LAM_U48 property");
4240 		info->callbacks->einfo (lam_u48_msg, abfd, missing);
4241 	      }
4242 	    if (missing_lam_u57)
4243 	      {
4244 		missing = _("LAM_U57 property");
4245 		info->callbacks->einfo (lam_u57_msg, abfd, missing);
4246 	      }
4247 	  }
4248     }
4249 
4250   pbfd = _bfd_elf_link_setup_gnu_properties (info);
4251 
4252   htab->r_info = init_table->r_info;
4253   htab->r_sym = init_table->r_sym;
4254 
4255   if (bfd_link_relocatable (info))
4256     return pbfd;
4257 
4258   htab->plt0_pad_byte = init_table->plt0_pad_byte;
4259 
4260   use_ibt_plt = htab->params->ibtplt || htab->params->ibt;
4261   if (!use_ibt_plt && pbfd != NULL)
4262     {
4263       /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on.  */
4264       elf_property_list *p;
4265 
4266       /* The property list is sorted in order of type.  */
4267       for (p = elf_properties (pbfd); p; p = p->next)
4268 	{
4269 	  if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
4270 	    {
4271 	      use_ibt_plt = !!(p->property.u.number
4272 			       & GNU_PROPERTY_X86_FEATURE_1_IBT);
4273 	      break;
4274 	    }
4275 	  else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
4276 	    break;
4277 	}
4278     }
4279 
4280   dynobj = htab->elf.dynobj;
4281 
4282   /* Set htab->elf.dynobj here so that there is no need to check and
4283      set it in check_relocs.  */
4284   if (dynobj == NULL)
4285     {
4286       if (pbfd != NULL)
4287 	{
4288 	  htab->elf.dynobj = pbfd;
4289 	  dynobj = pbfd;
4290 	}
4291       else
4292 	{
4293 	  bfd *abfd;
4294 
4295 	  /* Find a normal input file to hold linker created
4296 	     sections.  */
4297 	  for (abfd = info->input_bfds;
4298 	       abfd != NULL;
4299 	       abfd = abfd->link.next)
4300 	    if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
4301 		&& (abfd->flags
4302 		    & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
4303 		&& bed->relocs_compatible (abfd->xvec,
4304 					   info->output_bfd->xvec))
4305 	      {
4306 		htab->elf.dynobj = abfd;
4307 		dynobj = abfd;
4308 		break;
4309 	      }
4310 	}
4311     }
4312 
4313   /* Return if there are no normal input files.  */
4314   if (dynobj == NULL)
4315     return pbfd;
4316 
4317   /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
4318      still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
4319      canonical function address.  */
4320   htab->plt.has_plt0 = 1;
4321   htab->plt.plt_indirect_branch_offset = 0;
4322   normal_target = htab->elf.target_os == is_normal;
4323 
4324   if (normal_target)
4325     {
4326       if (use_ibt_plt)
4327 	{
4328 	  htab->lazy_plt = init_table->lazy_ibt_plt;
4329 	  htab->non_lazy_plt = init_table->non_lazy_ibt_plt;
4330 	  htab->plt.plt_indirect_branch_offset = 4;
4331 	}
4332       else
4333 	{
4334 	  htab->lazy_plt = init_table->lazy_plt;
4335 	  htab->non_lazy_plt = init_table->non_lazy_plt;
4336 	}
4337     }
4338   else
4339     {
4340       htab->lazy_plt = init_table->lazy_plt;
4341       htab->non_lazy_plt = NULL;
4342     }
4343 
4344   pltsec = htab->elf.splt;
4345 
4346   if (htab->non_lazy_plt != NULL
4347       && (!htab->plt.has_plt0 || pltsec == NULL))
4348     lazy_plt = false;
4349   else
4350     lazy_plt = true;
4351 
4352   if (normal_target)
4353     {
4354       if (use_ibt_plt)
4355 	{
4356 	  if (lazy_plt)
4357 	    htab->sframe_plt = init_table->sframe_lazy_ibt_plt;
4358 	  else
4359 	    htab->sframe_plt = init_table->sframe_non_lazy_ibt_plt;
4360 	}
4361       else
4362 	{
4363 	  if (lazy_plt)
4364 	    htab->sframe_plt = init_table->sframe_lazy_plt;
4365 	  else
4366 	    htab->sframe_plt = init_table->sframe_non_lazy_plt;
4367 	}
4368     }
4369   else
4370     htab->sframe_plt = NULL;
4371 
4372   /* If the non-lazy PLT is available, use it for all PLT entries if
4373      there are no PLT0 or no .plt section.  */
4374   if (!lazy_plt)
4375     {
4376       if (bfd_link_pic (info))
4377 	htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
4378       else
4379 	htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
4380       htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
4381       htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
4382       htab->plt.plt_got_insn_size
4383 	= htab->non_lazy_plt->plt_got_insn_size;
4384       htab->plt.eh_frame_plt_size
4385 	= htab->non_lazy_plt->eh_frame_plt_size;
4386       htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
4387     }
4388   else
4389     {
4390       if (bfd_link_pic (info))
4391 	{
4392 	  htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
4393 	  htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
4394 	}
4395       else
4396 	{
4397 	  htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
4398 	  htab->plt.plt_entry = htab->lazy_plt->plt_entry;
4399 	}
4400       htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
4401       htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
4402       htab->plt.plt_got_insn_size
4403 	= htab->lazy_plt->plt_got_insn_size;
4404       htab->plt.eh_frame_plt_size
4405 	= htab->lazy_plt->eh_frame_plt_size;
4406       htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
4407     }
4408 
4409   if (htab->elf.target_os == is_vxworks
4410       && !elf_vxworks_create_dynamic_sections (dynobj, info,
4411 					       &htab->srelplt2))
4412     {
4413       info->callbacks->einfo (_("%F%P: failed to create VxWorks dynamic sections\n"));
4414       return pbfd;
4415     }
4416 
4417   /* Since create_dynamic_sections isn't always called, but GOT
4418      relocations need GOT relocations, create them here so that we
4419      don't need to do it in check_relocs.  */
4420   if (htab->elf.sgot == NULL
4421       && !_bfd_elf_create_got_section (dynobj, info))
4422     info->callbacks->einfo (_("%F%P: failed to create GOT sections\n"));
4423 
4424   got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
4425 
4426   /* Align .got and .got.plt sections to their entry size.  Do it here
4427      instead of in create_dynamic_sections so that they are always
4428      properly aligned even if create_dynamic_sections isn't called.  */
4429   sec = htab->elf.sgot;
4430   if (!bfd_set_section_alignment (sec, got_align))
4431     goto error_alignment;
4432 
4433   sec = htab->elf.sgotplt;
4434   if (!bfd_set_section_alignment (sec, got_align))
4435     goto error_alignment;
4436 
4437   /* Create the ifunc sections here so that check_relocs can be
4438      simplified.  */
4439   if (!_bfd_elf_create_ifunc_sections (dynobj, info))
4440     info->callbacks->einfo (_("%F%P: failed to create ifunc sections\n"));
4441 
4442   plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
4443 
4444   if (pltsec != NULL)
4445     {
4446       /* Whe creating executable, set the contents of the .interp
4447 	 section to the interpreter.  */
4448       if (bfd_link_executable (info) && !info->nointerp)
4449 	{
4450 	  asection *s = bfd_get_linker_section (dynobj, ".interp");
4451 	  if (s == NULL)
4452 	    abort ();
4453 	  s->size = htab->dynamic_interpreter_size;
4454 	  s->contents = (unsigned char *) htab->dynamic_interpreter;
4455 	  htab->interp = s;
4456 	}
4457 
4458       if (normal_target)
4459 	{
4460 	  flagword pltflags = (bed->dynamic_sec_flags
4461 			       | SEC_ALLOC
4462 			       | SEC_CODE
4463 			       | SEC_LOAD
4464 			       | SEC_READONLY);
4465 	  unsigned int non_lazy_plt_alignment
4466 	    = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
4467 
4468 	  sec = pltsec;
4469 	  if (!bfd_set_section_alignment (sec, plt_alignment))
4470 	    goto error_alignment;
4471 
4472 	  /* Create the GOT procedure linkage table.  */
4473 	  sec = bfd_make_section_anyway_with_flags (dynobj,
4474 						    ".plt.got",
4475 						    pltflags);
4476 	  if (sec == NULL)
4477 	    info->callbacks->einfo (_("%F%P: failed to create GOT PLT section\n"));
4478 
4479 	  if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
4480 	    goto error_alignment;
4481 
4482 	  htab->plt_got = sec;
4483 
4484 	  if (lazy_plt)
4485 	    {
4486 	      sec = NULL;
4487 
4488 	      if (use_ibt_plt)
4489 		{
4490 		  /* Create the second PLT for Intel IBT support.  IBT
4491 		     PLT is needed only for lazy binding.  */
4492 		  sec = bfd_make_section_anyway_with_flags (dynobj,
4493 							    ".plt.sec",
4494 							    pltflags);
4495 		  if (sec == NULL)
4496 		    info->callbacks->einfo (_("%F%P: failed to create IBT-enabled PLT section\n"));
4497 
4498 		  if (!bfd_set_section_alignment (sec, plt_alignment))
4499 		    goto error_alignment;
4500 		}
4501 
4502 	      htab->plt_second = sec;
4503 	    }
4504 	}
4505 
4506       if (!info->no_ld_generated_unwind_info)
4507 	{
4508 	  flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4509 			    | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4510 			    | SEC_LINKER_CREATED);
4511 
4512 	  sec = bfd_make_section_anyway_with_flags (dynobj,
4513 						    ".eh_frame",
4514 						    flags);
4515 	  if (sec == NULL)
4516 	    info->callbacks->einfo (_("%F%P: failed to create PLT .eh_frame section\n"));
4517 
4518 	  if (!bfd_set_section_alignment (sec, class_align))
4519 	    goto error_alignment;
4520 
4521 	  htab->plt_eh_frame = sec;
4522 
4523 	  if (htab->plt_got != NULL)
4524 	    {
4525 	      sec = bfd_make_section_anyway_with_flags (dynobj,
4526 							".eh_frame",
4527 							flags);
4528 	      if (sec == NULL)
4529 		info->callbacks->einfo (_("%F%P: failed to create GOT PLT .eh_frame section\n"));
4530 
4531 	      if (!bfd_set_section_alignment (sec, class_align))
4532 		goto error_alignment;
4533 
4534 	      htab->plt_got_eh_frame = sec;
4535 	    }
4536 
4537 	  if (htab->plt_second != NULL)
4538 	    {
4539 	      sec = bfd_make_section_anyway_with_flags (dynobj,
4540 							".eh_frame",
4541 							flags);
4542 	      if (sec == NULL)
4543 		info->callbacks->einfo (_("%F%P: failed to create the second PLT .eh_frame section\n"));
4544 
4545 	      if (!bfd_set_section_alignment (sec, class_align))
4546 		goto error_alignment;
4547 
4548 	      htab->plt_second_eh_frame = sec;
4549 	    }
4550 	}
4551 
4552       /* .sframe sections are emitted for AMD64 ABI only.  */
4553       if (ABI_64_P (info->output_bfd) && !info->no_ld_generated_unwind_info)
4554 	{
4555 	  flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4556 			    | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4557 			    | SEC_LINKER_CREATED);
4558 
4559 	  sec = bfd_make_section_anyway_with_flags (dynobj,
4560 						    ".sframe",
4561 						    flags);
4562 	  if (sec == NULL)
4563 	    info->callbacks->einfo (_("%F%P: failed to create PLT .sframe section\n"));
4564 
4565 	  // FIXME check this
4566 	  // if (!bfd_set_section_alignment (sec, class_align))
4567 	  //  goto error_alignment;
4568 
4569 	  htab->plt_sframe = sec;
4570 
4571 	  /* Second PLT is generated for Intel IBT + lazy plt.  */
4572 	  if (htab->plt_second != NULL)
4573 	    {
4574 	      sec = bfd_make_section_anyway_with_flags (dynobj,
4575 							".sframe",
4576 							flags);
4577 	      if (sec == NULL)
4578 		info->callbacks->einfo (_("%F%P: failed to create second PLT .sframe section\n"));
4579 
4580 	      htab->plt_second_sframe = sec;
4581 	    }
4582 	  /* FIXME - add later for plt_got. */
4583 	}
4584     }
4585 
4586   /* The .iplt section is used for IFUNC symbols in static
4587      executables.  */
4588   sec = htab->elf.iplt;
4589   if (sec != NULL)
4590     {
4591       /* NB: Delay setting its alignment until we know it is non-empty.
4592 	 Otherwise an empty iplt section may change vma and lma of the
4593 	 following sections, which triggers moving dot of the following
4594 	 section backwards, resulting in a warning and section lma not
4595 	 being set properly.  It later leads to a "File truncated"
4596 	 error.  */
4597       if (!bfd_set_section_alignment (sec, 0))
4598 	goto error_alignment;
4599 
4600       htab->plt.iplt_alignment = (normal_target
4601 				  ? plt_alignment
4602 				  : bed->plt_alignment);
4603     }
4604 
4605   if (bfd_link_executable (info)
4606       && !info->nointerp
4607       && !htab->params->has_dynamic_linker
4608       && htab->params->static_before_all_inputs)
4609     {
4610       /* Report error for dynamic input objects if -static is passed at
4611 	 command-line before all input files without --dynamic-linker
4612 	 unless --no-dynamic-linker is used.  */
4613       bfd *abfd;
4614 
4615       for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
4616 	if ((abfd->flags & DYNAMIC))
4617 	  info->callbacks->einfo
4618 	    (_("%X%P: attempted static link of dynamic object `%pB'\n"),
4619 	     abfd);
4620     }
4621 
4622   return pbfd;
4623 }
4624 
4625 /* Fix up x86 GNU properties.  */
4626 
4627 void
4628 _bfd_x86_elf_link_fixup_gnu_properties
4629   (struct bfd_link_info *info, elf_property_list **listp)
4630 {
4631   elf_property_list *p;
4632 
4633   for (p = *listp; p; p = p->next)
4634     {
4635       unsigned int type = p->property.pr_type;
4636       if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
4637 	  || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4638 	  || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
4639 	      && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4640 	  || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
4641 	      && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
4642 	  || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
4643 	      && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
4644 	{
4645 	  if (p->property.u.number == 0
4646 	      && (type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4647 		  || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
4648 		      && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4649 		  || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
4650 		      && type <= GNU_PROPERTY_X86_UINT32_OR_HI)))
4651 	    {
4652 	      /* Remove empty property.  */
4653 	      *listp = p->next;
4654 	      continue;
4655 	    }
4656 
4657 	  /* Keep LAM features only for 64-bit output.  */
4658 	  if (type == GNU_PROPERTY_X86_FEATURE_1_AND
4659 	      && !ABI_64_P (info->output_bfd))
4660 	    p->property.u.number &= ~(GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4661 				      | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4662 
4663 	  listp = &p->next;
4664 	}
4665       else if (type > GNU_PROPERTY_HIPROC)
4666 	{
4667 	  /* The property list is sorted in order of type.  */
4668 	  break;
4669 	}
4670     }
4671 }
4672 
4673 void
4674 _bfd_elf_linker_x86_set_options (struct bfd_link_info * info,
4675 				 struct elf_linker_x86_params *params)
4676 {
4677   const struct elf_backend_data *bed
4678     = get_elf_backend_data (info->output_bfd);
4679   struct elf_x86_link_hash_table *htab
4680     = elf_x86_hash_table (info, bed->target_id);
4681   if (htab != NULL)
4682     htab->params = params;
4683 }
4684