xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/elfxx-x86.c (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1 /* x86 specific support for ELF
2    Copyright (C) 2017-2022 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
_bfd_x86_elf_mkobject(bfd * abfd)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
_bfd_x86_elf_set_tls_module_base(struct bfd_link_info * info)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
_bfd_x86_elf_dtpoff_base(struct bfd_link_info * info)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
elf_x86_allocate_dynrelocs(struct elf_link_hash_entry * h,void * inf)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
elf_x86_allocate_local_dynreloc(void ** slot,void * inf)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 *
_bfd_elf_x86_get_local_sym_hash(struct elf_x86_link_hash_table * htab,bfd * abfd,const Elf_Internal_Rela * rel,bool create)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 *
_bfd_x86_elf_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)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
_bfd_x86_elf_local_htab_hash(const void * ptr)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
_bfd_x86_elf_local_htab_eq(const void * ptr1,const void * ptr2)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
elf_x86_link_hash_table_free(bfd * obfd)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
elf_i386_is_reloc_section(const char * secname)706 elf_i386_is_reloc_section (const char *secname)
707 {
708   return startswith (secname, ".rel");
709 }
710 
711 static bool
elf_x86_64_is_reloc_section(const char * secname)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 *
_bfd_x86_elf_link_hash_table_create(bfd * abfd)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
_bfd_x86_elf_compare_relocs(const void * ap,const void * bp)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
elf_x86_linker_defined(struct bfd_link_info * info,const char * name)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
elf_x86_hide_linker_defined(struct bfd_link_info * info,const char * name)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
_bfd_x86_elf_link_check_relocs(bfd * abfd,struct bfd_link_info * info)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
_bfd_x86_elf_check_relocs(bfd * abfd,struct bfd_link_info * info,asection * sec,const Elf_Internal_Rela * relocs)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
elf_x86_relative_reloc_record_add(struct bfd_link_info * info,struct elf_x86_relative_reloc_data * relative_reloc,Elf_Internal_Rela * rel,asection * sec,asection * sym_sec,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym,bfd_vma offset)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)
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     }
1061   relative_reloc->data[newidx].offset = offset;
1062   relative_reloc->data[newidx].address = 0;
1063   return true;
1064 }
1065 
1066 /* After input sections have been mapped to output sections and
1067    addresses of output sections are set initiallly, scan input
1068    relocations with the same logic in relocate_section to determine
1069    if a relative relocation should be generated.  Save the relative
1070    relocation candidate information for sizing the DT_RELR section
1071    later after all symbols addresses can be determined.  */
1072 
1073 bool
_bfd_x86_elf_link_relax_section(bfd * abfd ATTRIBUTE_UNUSED,asection * input_section,struct bfd_link_info * info,bool * again)1074 _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
1075 				 asection *input_section,
1076 				 struct bfd_link_info *info,
1077 				 bool *again)
1078 {
1079   Elf_Internal_Shdr *symtab_hdr;
1080   Elf_Internal_Rela *internal_relocs;
1081   Elf_Internal_Rela *irel, *irelend;
1082   Elf_Internal_Sym *isymbuf = NULL;
1083   struct elf_link_hash_entry **sym_hashes;
1084   const struct elf_backend_data *bed;
1085   struct elf_x86_link_hash_table *htab;
1086   bfd_vma *local_got_offsets;
1087   bool is_x86_64;
1088   bool unaligned_section;
1089 
1090   if (bfd_link_relocatable (info))
1091     return true;
1092 
1093   /* Assume we're not going to change any sizes, and we'll only need
1094      one pass.  */
1095   *again = false;
1096 
1097   bed = get_elf_backend_data (abfd);
1098   htab = elf_x86_hash_table (info, bed->target_id);
1099   if (htab == NULL)
1100     return true;
1101 
1102   /* Nothing to do if there are no relocations or relative relocations
1103      have been packed.  */
1104   if (input_section == htab->elf.srelrdyn
1105       || input_section->relative_reloc_packed
1106       || ((input_section->flags & (SEC_RELOC | SEC_ALLOC))
1107 	  != (SEC_RELOC | SEC_ALLOC))
1108       || (input_section->flags & SEC_DEBUGGING) != 0
1109       || input_section->reloc_count == 0)
1110     return true;
1111 
1112   /* Skip if the section isn't aligned.  */
1113   unaligned_section = input_section->alignment_power == 0;
1114 
1115   is_x86_64 = bed->target_id == X86_64_ELF_DATA;
1116 
1117   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1118   sym_hashes = elf_sym_hashes (abfd);
1119   local_got_offsets = elf_local_got_offsets (abfd);
1120 
1121   /* Load the relocations for this section.  */
1122   internal_relocs =
1123     _bfd_elf_link_read_relocs (abfd, input_section, NULL,
1124 			       (Elf_Internal_Rela *) NULL,
1125 			       info->keep_memory);
1126   if (internal_relocs == NULL)
1127     return false;
1128 
1129   irelend = internal_relocs + input_section->reloc_count;
1130   for (irel = internal_relocs; irel < irelend; irel++)
1131     {
1132       unsigned int r_type;
1133       unsigned int r_symndx;
1134       Elf_Internal_Sym *isym;
1135       struct elf_link_hash_entry *h;
1136       struct elf_x86_link_hash_entry *eh;
1137       bfd_vma offset;
1138       bool resolved_to_zero;
1139       bool need_copy_reloc_in_pie;
1140       bool pc32_reloc;
1141       asection *sec;
1142       /* Offset must be a multiple of 2.  */
1143       bool unaligned_offset = (irel->r_offset & 1) != 0;
1144       /* True if there is a relative relocation against a dynamic
1145 	 symbol.  */
1146       bool dynamic_relative_reloc_p;
1147 
1148       /* Get the value of the symbol referred to by the reloc.  */
1149       r_symndx = htab->r_sym (irel->r_info);
1150 
1151       r_type = ELF32_R_TYPE (irel->r_info);
1152       /* Clear the R_X86_64_converted_reloc_bit bit.  */
1153       r_type &= ~R_X86_64_converted_reloc_bit;
1154 
1155       sec = NULL;
1156       h = NULL;
1157       dynamic_relative_reloc_p = false;
1158 
1159       if (r_symndx < symtab_hdr->sh_info)
1160 	{
1161 	  /* Read this BFD's local symbols.  */
1162 	  if (isymbuf == NULL)
1163 	    {
1164 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1165 	      if (isymbuf == NULL)
1166 		isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1167 						symtab_hdr->sh_info, 0,
1168 						NULL, NULL, NULL);
1169 	      if (isymbuf == NULL)
1170 		goto error_return;
1171 	    }
1172 
1173 	  isym = isymbuf + r_symndx;
1174 	  switch (isym->st_shndx)
1175 	    {
1176 	    case SHN_ABS:
1177 	      sec = bfd_abs_section_ptr;
1178 	      break;
1179 	    case SHN_COMMON:
1180 	      sec = bfd_com_section_ptr;
1181 	      break;
1182 	    case SHN_X86_64_LCOMMON:
1183 	      if (!is_x86_64)
1184 		abort ();
1185 	      sec = &_bfd_elf_large_com_section;
1186 	      break;
1187 	    default:
1188 	      sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1189 	      break;
1190 	    }
1191 
1192 	  /* Skip relocation against local STT_GNU_IFUNC symbol.  */
1193 	  if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1194 	    continue;
1195 
1196 	  eh = (struct elf_x86_link_hash_entry *) h;
1197 	  resolved_to_zero = false;
1198 	}
1199       else
1200 	{
1201 	  /* Get H and SEC for GENERATE_DYNAMIC_RELOCATION_P below.  */
1202 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1203 	  while (h->root.type == bfd_link_hash_indirect
1204 		 || h->root.type == bfd_link_hash_warning)
1205 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1206 
1207 	  if (h->root.type == bfd_link_hash_defined
1208 	      || h->root.type == bfd_link_hash_defweak)
1209 	    sec = h->root.u.def.section;
1210 
1211 	  /* Skip relocation against STT_GNU_IFUNC symbol.  */
1212 	  if (h->type == STT_GNU_IFUNC)
1213 	    continue;
1214 
1215 	  eh = (struct elf_x86_link_hash_entry *) h;
1216 	  resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
1217 
1218 	  /* NB: See how elf_backend_finish_dynamic_symbol is called
1219 	     from elf_link_output_extsym.  */
1220 	  if ((h->dynindx != -1 || h->forced_local)
1221 	      && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1222 		   || h->root.type != bfd_link_hash_undefweak)
1223 		  || !h->forced_local)
1224 	      && h->got.offset != (bfd_vma) -1
1225 	      && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
1226 	      && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
1227 	      && !resolved_to_zero
1228 	      && SYMBOL_REFERENCES_LOCAL_P (info, h)
1229 	      && SYMBOL_DEFINED_NON_SHARED_P (h))
1230 	    dynamic_relative_reloc_p = true;
1231 
1232 	  isym = NULL;
1233 	}
1234 
1235       if (X86_GOT_TYPE_P (is_x86_64, r_type))
1236 	{
1237 	  /* Pack GOT relative relocations.  There should be only a
1238 	     single R_*_RELATIVE relocation in GOT.  */
1239 	  if (eh != NULL)
1240 	    {
1241 	      if (eh->got_relative_reloc_done)
1242 		continue;
1243 
1244 	      if (!(dynamic_relative_reloc_p
1245 		    || (RESOLVED_LOCALLY_P (info, h, htab)
1246 			&& GENERATE_RELATIVE_RELOC_P (info, h))))
1247 		continue;
1248 
1249 	      if (!dynamic_relative_reloc_p)
1250 		eh->no_finish_dynamic_symbol = 1;
1251 	      eh->got_relative_reloc_done = 1;
1252 	      offset = h->got.offset;
1253 	    }
1254 	  else
1255 	    {
1256 	      if (elf_x86_relative_reloc_done (abfd)[r_symndx])
1257 		continue;
1258 
1259 	      if (!X86_LOCAL_GOT_RELATIVE_RELOC_P (is_x86_64, info,
1260 						   isym))
1261 		continue;
1262 
1263 	      elf_x86_relative_reloc_done (abfd)[r_symndx] = 1;
1264 	      offset = local_got_offsets[r_symndx];
1265 	    }
1266 
1267 	  if (!elf_x86_relative_reloc_record_add (info,
1268 						  &htab->relative_reloc,
1269 						  irel, htab->elf.sgot,
1270 						  sec, h, isym, offset))
1271 	    goto error_return;
1272 
1273 	  continue;
1274 	}
1275 
1276       if (is_x86_64
1277 	  && irel->r_addend == 0
1278 	  && !ABI_64_P (info->output_bfd))
1279 	{
1280 	  /* For x32, if addend is zero, treat R_X86_64_64 like
1281 	     R_X86_64_32 and R_X86_64_SIZE64 like R_X86_64_SIZE32.  */
1282 	  if (r_type == R_X86_64_64)
1283 	    r_type = R_X86_64_32;
1284 	  else if (r_type == R_X86_64_SIZE64)
1285 	    r_type = R_X86_64_SIZE32;
1286 	}
1287 
1288       if (!X86_RELATIVE_RELOC_TYPE_P (is_x86_64, r_type))
1289 	continue;
1290 
1291       /* Pack non-GOT relative relocations.  */
1292       if (is_x86_64)
1293 	{
1294 	  need_copy_reloc_in_pie =
1295 	    (bfd_link_pie (info)
1296 	     && h != NULL
1297 	     && (h->needs_copy
1298 		 || eh->needs_copy
1299 		 || (h->root.type == bfd_link_hash_undefined))
1300 	     && (X86_PCREL_TYPE_P (true, r_type)
1301 		 || X86_SIZE_TYPE_P (true, r_type)));
1302 	  pc32_reloc = false;
1303 	}
1304       else
1305 	{
1306 	  need_copy_reloc_in_pie = false;
1307 	  pc32_reloc = r_type == R_386_PC32;
1308 	}
1309 
1310       if (GENERATE_DYNAMIC_RELOCATION_P (is_x86_64, info, eh, r_type,
1311 					 sec, need_copy_reloc_in_pie,
1312 					 resolved_to_zero, pc32_reloc))
1313 	{
1314 	  /* When generating a shared object, these relocations
1315 	     are copied into the output file to be resolved at run
1316 	     time.	*/
1317 	  offset = _bfd_elf_section_offset (info->output_bfd, info,
1318 					    input_section,
1319 					    irel->r_offset);
1320 	  if (offset == (bfd_vma) -1
1321 	      || offset == (bfd_vma) -2
1322 	      || COPY_INPUT_RELOC_P (is_x86_64, info, h, r_type))
1323 	    continue;
1324 
1325 	  /* This symbol is local, or marked to become local.  When
1326 	     relocation overflow check is disabled, we convert
1327 	     R_X86_64_32 to dynamic R_X86_64_RELATIVE.  */
1328 	  if (is_x86_64
1329 	      && !(r_type == htab->pointer_r_type
1330 		   || (r_type == R_X86_64_32
1331 		       && htab->params->no_reloc_overflow_check)))
1332 	    continue;
1333 
1334 	  if (!elf_x86_relative_reloc_record_add
1335 	        (info,
1336 		 ((unaligned_section || unaligned_offset)
1337 		  ? &htab->unaligned_relative_reloc
1338 		  : &htab->relative_reloc),
1339 		 irel, input_section, sec, h, isym, offset))
1340 	    goto error_return;
1341 	}
1342     }
1343 
1344   input_section->relative_reloc_packed = 1;
1345 
1346   return true;
1347 
1348 error_return:
1349   if ((unsigned char *) isymbuf != symtab_hdr->contents)
1350     free (isymbuf);
1351   if (elf_section_data (input_section)->relocs != internal_relocs)
1352     free (internal_relocs);
1353   return false;
1354 }
1355 
1356 /* Add an entry to the 64-bit DT_RELR bitmap.  */
1357 
1358 static void
elf64_dt_relr_bitmap_add(struct bfd_link_info * info,struct elf_dt_relr_bitmap * bitmap,uint64_t entry)1359 elf64_dt_relr_bitmap_add
1360   (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
1361    uint64_t entry)
1362 {
1363   bfd_size_type newidx;
1364 
1365   if (bitmap->u.elf64 == NULL)
1366     {
1367       bitmap->u.elf64 = bfd_malloc (sizeof (uint64_t));
1368       bitmap->count = 0;
1369       bitmap->size = 1;
1370     }
1371 
1372   newidx = bitmap->count++;
1373 
1374   if (bitmap->count > bitmap->size)
1375     {
1376       bitmap->size <<= 1;
1377       bitmap->u.elf64 = bfd_realloc (bitmap->u.elf64,
1378 				     (bitmap->size * sizeof (uint64_t)));
1379     }
1380 
1381   if (bitmap->u.elf64 == NULL)
1382     {
1383       info->callbacks->einfo
1384 	/* xgettext:c-format */
1385 	(_("%F%P: %pB: failed to allocate 64-bit DT_RELR bitmap\n"),
1386 	 info->output_bfd);
1387     }
1388 
1389   bitmap->u.elf64[newidx] = entry;
1390 }
1391 
1392 /* Add an entry to the 32-bit DT_RELR bitmap.  */
1393 
1394 static void
elf32_dt_relr_bitmap_add(struct bfd_link_info * info,struct elf_dt_relr_bitmap * bitmap,uint32_t entry)1395 elf32_dt_relr_bitmap_add
1396   (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
1397    uint32_t entry)
1398 {
1399   bfd_size_type newidx;
1400 
1401   if (bitmap->u.elf32 == NULL)
1402     {
1403       bitmap->u.elf32 = bfd_malloc (sizeof (uint32_t));
1404       bitmap->count = 0;
1405       bitmap->size = 1;
1406     }
1407 
1408   newidx = bitmap->count++;
1409 
1410   if (bitmap->count > bitmap->size)
1411     {
1412       bitmap->size <<= 1;
1413       bitmap->u.elf32 = bfd_realloc (bitmap->u.elf32,
1414 				     (bitmap->size * sizeof (uint32_t)));
1415     }
1416 
1417   if (bitmap->u.elf32 == NULL)
1418     {
1419       info->callbacks->einfo
1420 	/* xgettext:c-format */
1421 	(_("%F%P: %pB: failed to allocate 32-bit DT_RELR bitmap\n"),
1422 	 info->output_bfd);
1423     }
1424 
1425   bitmap->u.elf32[newidx] = entry;
1426 }
1427 
1428 void
_bfd_elf32_write_addend(bfd * abfd,uint64_t value,void * addr)1429 _bfd_elf32_write_addend (bfd *abfd, uint64_t value, void *addr)
1430 {
1431   bfd_put_32 (abfd, value, addr);
1432 }
1433 
1434 void
_bfd_elf64_write_addend(bfd * abfd,uint64_t value,void * addr)1435 _bfd_elf64_write_addend (bfd *abfd, uint64_t value, void *addr)
1436 {
1437   bfd_put_64 (abfd, value, addr);
1438 }
1439 
1440 /* Size or finish relative relocations to determine the run-time
1441    addresses for DT_RELR bitmap computation later.  OUTREL is set
1442    to NULL in the sizing phase and non-NULL in the finising phase
1443    where the regular relative relocations will be written out.  */
1444 
1445 static void
elf_x86_size_or_finish_relative_reloc(bool is_x86_64,struct bfd_link_info * info,struct elf_x86_link_hash_table * htab,bool unaligned,Elf_Internal_Rela * outrel)1446 elf_x86_size_or_finish_relative_reloc
1447   (bool is_x86_64, struct bfd_link_info *info,
1448    struct elf_x86_link_hash_table *htab, bool unaligned,
1449    Elf_Internal_Rela *outrel)
1450 {
1451   unsigned int align_mask;
1452   bfd_size_type i, count;
1453   asection *sec, *srel;
1454   struct elf_link_hash_entry *h;
1455   bfd_vma offset;
1456   Elf_Internal_Sym *sym;
1457   asection *sym_sec;
1458   asection *sgot = htab->elf.sgot;
1459   asection *srelgot = htab->elf.srelgot;
1460   struct elf_x86_relative_reloc_data *relative_reloc;
1461 
1462   if (unaligned)
1463     {
1464       align_mask = 0;
1465       relative_reloc = &htab->unaligned_relative_reloc;
1466     }
1467   else
1468     {
1469       align_mask = 1;
1470       relative_reloc = &htab->relative_reloc;
1471     }
1472 
1473   count = relative_reloc->count;
1474   for (i = 0; i < count; i++)
1475     {
1476       sec = relative_reloc->data[i].sec;
1477       sym = relative_reloc->data[i].sym;
1478 
1479       /* If SYM is NULL, it must be a global symbol.  */
1480       if (sym == NULL)
1481 	h = relative_reloc->data[i].u.h;
1482       else
1483 	h = NULL;
1484 
1485       if (is_x86_64)
1486 	{
1487 	  bfd_vma relocation;
1488 	  /* This function may be called more than once and REL may be
1489 	     updated by _bfd_elf_rela_local_sym below.  */
1490 	  Elf_Internal_Rela rel = relative_reloc->data[i].rel;
1491 
1492 	  if (h != NULL)
1493 	    {
1494 	      if (h->root.type == bfd_link_hash_defined
1495 		  || h->root.type == bfd_link_hash_defweak)
1496 		{
1497 		  sym_sec = h->root.u.def.section;
1498 		  relocation = (h->root.u.def.value
1499 				+ sym_sec->output_section->vma
1500 				+ sym_sec->output_offset);
1501 		}
1502 	      else
1503 		{
1504 		  /* Allow undefined symbol only at the sizing phase.
1505 		     Otherwise skip undefined symbol here.  Undefined
1506 		     symbol will be reported by relocate_section.  */
1507 		  if (outrel == NULL)
1508 		    relocation = 0;
1509 		  else
1510 		    continue;
1511 		}
1512 	    }
1513 	  else
1514 	    {
1515 	      sym_sec = relative_reloc->data[i].u.sym_sec;
1516 	      relocation = _bfd_elf_rela_local_sym
1517 		(info->output_bfd, sym, &sym_sec, &rel);
1518 	    }
1519 
1520 	  if (outrel != NULL)
1521 	    {
1522 	      outrel->r_addend = relocation;
1523 	      if (sec == sgot)
1524 		{
1525 		  if (h != NULL && h->needs_plt)
1526 		    abort ();
1527 		}
1528 	      else
1529 		outrel->r_addend += rel.r_addend;
1530 
1531 	      /* Write the implicit addend if ALIGN_MASK isn't 0.  */
1532 	      if (align_mask)
1533 		{
1534 		  if (sec == sgot)
1535 		    {
1536 		      if (relative_reloc->data[i].offset >= sec->size)
1537 			abort ();
1538 		      htab->elf_write_addend_in_got
1539 			(info->output_bfd, outrel->r_addend,
1540 			 sec->contents + relative_reloc->data[i].offset);
1541 		    }
1542 		  else
1543 		    {
1544 		      if (rel.r_offset >= sec->size)
1545 			abort ();
1546 		      htab->elf_write_addend
1547 			(info->output_bfd, outrel->r_addend,
1548 			 (elf_section_data (sec)->this_hdr.contents
1549 			  + rel.r_offset));
1550 		    }
1551 		}
1552 	    }
1553 	}
1554 
1555       if (sec == sgot)
1556 	srel = srelgot;
1557       else
1558 	srel = elf_section_data (sec)->sreloc;
1559       offset = (sec->output_section->vma + sec->output_offset
1560 		+ relative_reloc->data[i].offset);
1561       relative_reloc->data[i].address = offset;
1562       if (outrel != NULL)
1563 	{
1564 	  outrel->r_offset = offset;
1565 
1566 	  if ((outrel->r_offset & align_mask) != 0)
1567 	    abort ();
1568 
1569 	  if (htab->params->report_relative_reloc)
1570 	    _bfd_x86_elf_link_report_relative_reloc
1571 	      (info, sec, h, sym, htab->relative_r_name, outrel);
1572 
1573 	  /* Generate regular relative relocation if ALIGN_MASK is 0.  */
1574 	  if (align_mask == 0)
1575 	    htab->elf_append_reloc (info->output_bfd, srel, outrel);
1576 	}
1577     }
1578 }
1579 
1580 /* Compute the DT_RELR section size.  Set NEED_PLAYOUT to true if
1581    the DT_RELR section size has been increased.  */
1582 
1583 static void
elf_x86_compute_dl_relr_bitmap(struct bfd_link_info * info,struct elf_x86_link_hash_table * htab,bool * need_layout)1584 elf_x86_compute_dl_relr_bitmap
1585   (struct bfd_link_info *info, struct elf_x86_link_hash_table *htab,
1586    bool *need_layout)
1587 {
1588   bfd_vma base;
1589   bfd_size_type i, count, new_count;
1590   struct elf_x86_relative_reloc_data *relative_reloc =
1591     &htab->relative_reloc;
1592   /* Save the old DT_RELR bitmap count.  Don't shrink the DT_RELR bitmap
1593      if the new DT_RELR bitmap count is smaller than the old one.  Pad
1594      with trailing 1s which won't be decoded to more relocations.  */
1595   bfd_size_type dt_relr_bitmap_count = htab->dt_relr_bitmap.count;
1596 
1597   /* Clear the DT_RELR bitmap count.  */
1598   htab->dt_relr_bitmap.count = 0;
1599 
1600   count = relative_reloc->count;
1601 
1602   if (ABI_64_P (info->output_bfd))
1603     {
1604       /* Compute the 64-bit DT_RELR bitmap.  */
1605       i = 0;
1606       while (i < count)
1607 	{
1608 	  if ((relative_reloc->data[i].address % 1) != 0)
1609 	    abort ();
1610 
1611 	  elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1612 				    relative_reloc->data[i].address);
1613 
1614 	  base = relative_reloc->data[i].address + 8;
1615 	  i++;
1616 
1617 	  while (i < count)
1618 	    {
1619 	      uint64_t bitmap = 0;
1620 	      for (; i < count; i++)
1621 		{
1622 		  bfd_vma delta = (relative_reloc->data[i].address
1623 				   - base);
1624 		  /* Stop if it is too far from base.  */
1625 		  if (delta >= 63 * 8)
1626 		    break;
1627 		  /* Stop if it isn't a multiple of 8.  */
1628 		  if ((delta % 8) != 0)
1629 		    break;
1630 		  bitmap |= 1ULL << (delta / 8);
1631 		}
1632 
1633 	      if (bitmap == 0)
1634 		break;
1635 
1636 	      elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1637 					(bitmap << 1) | 1);
1638 
1639 	      base += 63 * 8;
1640 	    }
1641 	}
1642 
1643       new_count = htab->dt_relr_bitmap.count;
1644       if (dt_relr_bitmap_count > new_count)
1645 	{
1646 	  /* Don't shrink the DT_RELR section size to avoid section
1647 	     layout oscillation.  Instead, pad the DT_RELR bitmap with
1648 	     1s which do not decode to more relocations.  */
1649 
1650 	  htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
1651 	  count = dt_relr_bitmap_count - new_count;
1652 	  for (i = 0; i < count; i++)
1653 	    htab->dt_relr_bitmap.u.elf64[new_count + i] = 1;
1654 	}
1655     }
1656   else
1657     {
1658       /* Compute the 32-bit DT_RELR bitmap.  */
1659       i = 0;
1660       while (i < count)
1661 	{
1662 	  if ((relative_reloc->data[i].address % 1) != 0)
1663 	    abort ();
1664 
1665 	  elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1666 				    relative_reloc->data[i].address);
1667 
1668 	  base = relative_reloc->data[i].address + 4;
1669 	  i++;
1670 
1671 	  while (i < count)
1672 	    {
1673 	      uint32_t bitmap = 0;
1674 	      for (; i < count; i++)
1675 		{
1676 		  bfd_vma delta = (relative_reloc->data[i].address
1677 				   - base);
1678 		  /* Stop if it is too far from base.  */
1679 		  if (delta >= 31 * 4)
1680 		    break;
1681 		  /* Stop if it isn't a multiple of 4.  */
1682 		  if ((delta % 4) != 0)
1683 		    break;
1684 		  bitmap |= 1ULL << (delta / 4);
1685 		}
1686 
1687 	      if (bitmap == 0)
1688 		break;
1689 
1690 	      elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
1691 					(bitmap << 1) | 1);
1692 
1693 	      base += 31 * 4;
1694 	    }
1695 	}
1696 
1697       new_count = htab->dt_relr_bitmap.count;
1698       if (dt_relr_bitmap_count > new_count)
1699 	{
1700 	  /* Don't shrink the DT_RELR section size to avoid section
1701 	     layout oscillation.  Instead, pad the DT_RELR bitmap with
1702 	     1s which do not decode to more relocations.  */
1703 
1704 	  htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
1705 	  count = dt_relr_bitmap_count - new_count;
1706 	  for (i = 0; i < count; i++)
1707 	    htab->dt_relr_bitmap.u.elf32[new_count + i] = 1;
1708 	}
1709     }
1710 
1711   if (htab->dt_relr_bitmap.count != dt_relr_bitmap_count)
1712     {
1713       if (need_layout)
1714 	{
1715 	  /* The .relr.dyn section size is changed.  Update the section
1716 	     size and tell linker to layout sections again.  */
1717 	  htab->elf.srelrdyn->size =
1718 	    (htab->dt_relr_bitmap.count
1719 	     * (ABI_64_P (info->output_bfd) ? 8 : 4));
1720 
1721 	  *need_layout = true;
1722 	}
1723       else
1724 	info->callbacks->einfo
1725 	  /* xgettext:c-format */
1726 	  (_("%F%P: %pB: size of compact relative reloc section is "
1727 	     "changed: new (%lu) != old (%lu)\n"),
1728 	   info->output_bfd, htab->dt_relr_bitmap.count,
1729 	   dt_relr_bitmap_count);
1730     }
1731 }
1732 
1733 /* Write out the DT_RELR section.  */
1734 
1735 static void
elf_x86_write_dl_relr_bitmap(struct bfd_link_info * info,struct elf_x86_link_hash_table * htab)1736 elf_x86_write_dl_relr_bitmap (struct bfd_link_info *info,
1737 			      struct elf_x86_link_hash_table *htab)
1738 {
1739   asection *sec = htab->elf.srelrdyn;
1740   bfd_size_type size = sec->size;
1741   bfd_size_type i;
1742   unsigned char *contents;
1743 
1744   contents = (unsigned char *) bfd_alloc (sec->owner, size);
1745   if (contents == NULL)
1746     info->callbacks->einfo
1747       /* xgettext:c-format */
1748       (_("%F%P: %pB: failed to allocate compact relative reloc section\n"),
1749        info->output_bfd);
1750 
1751   /* Cache the section contents for elf_link_input_bfd.  */
1752   sec->contents = contents;
1753 
1754   if (ABI_64_P (info->output_bfd))
1755     for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 8)
1756       bfd_put_64 (info->output_bfd, htab->dt_relr_bitmap.u.elf64[i],
1757 		  contents);
1758   else
1759     for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 4)
1760       bfd_put_32 (info->output_bfd, htab->dt_relr_bitmap.u.elf32[i],
1761 		  contents);
1762 }
1763 
1764 /* Sort relative relocations by address.  */
1765 
1766 static int
elf_x86_relative_reloc_compare(const void * pa,const void * pb)1767 elf_x86_relative_reloc_compare (const void *pa, const void *pb)
1768 {
1769   struct elf_x86_relative_reloc_record *a =
1770     (struct elf_x86_relative_reloc_record *) pa;
1771   struct elf_x86_relative_reloc_record *b =
1772     (struct elf_x86_relative_reloc_record *) pb;
1773   if (a->address < b->address)
1774     return -1;
1775   if (a->address > b->address)
1776     return 1;
1777   return 0;
1778 }
1779 
1780 bool
_bfd_elf_x86_size_relative_relocs(struct bfd_link_info * info,bool * need_layout)1781 _bfd_elf_x86_size_relative_relocs (struct bfd_link_info *info,
1782 				   bool *need_layout)
1783 {
1784   struct elf_x86_link_hash_table *htab;
1785   const struct elf_backend_data *bed;
1786   bool is_x86_64;
1787   bfd_size_type i, count, unaligned_count;
1788   asection *sec, *srel;
1789 
1790   /* Do nothing for ld -r.  */
1791   if (bfd_link_relocatable (info))
1792     return true;
1793 
1794   bed = get_elf_backend_data (info->output_bfd);
1795   htab = elf_x86_hash_table (info, bed->target_id);
1796   if (htab == NULL)
1797     return false;
1798 
1799   count = htab->relative_reloc.count;
1800   unaligned_count = htab->unaligned_relative_reloc.count;
1801   if (count == 0)
1802     {
1803       if (htab->generate_relative_reloc_pass == 0
1804 	  && htab->elf.srelrdyn != NULL)
1805 	{
1806 	  /* Remove the empty .relr.dyn sections now.  */
1807 	  if (!bfd_is_abs_section (htab->elf.srelrdyn->output_section))
1808 	    {
1809 	      bfd_section_list_remove
1810 		(info->output_bfd, htab->elf.srelrdyn->output_section);
1811 	      info->output_bfd->section_count--;
1812 	    }
1813 	  bfd_section_list_remove (htab->elf.srelrdyn->owner,
1814 				   htab->elf.srelrdyn);
1815 	  htab->elf.srelrdyn->owner->section_count--;
1816 	}
1817       if (unaligned_count == 0)
1818 	{
1819 	  htab->generate_relative_reloc_pass++;
1820 	  return true;
1821 	}
1822     }
1823 
1824   is_x86_64 = bed->target_id == X86_64_ELF_DATA;
1825 
1826   /* Size relative relocations.  */
1827   if (htab->generate_relative_reloc_pass)
1828     {
1829       /* Reset the regular relative relocation count.  */
1830       for (i = 0; i < unaligned_count; i++)
1831 	{
1832 	  sec = htab->unaligned_relative_reloc.data[i].sec;
1833 	  srel = elf_section_data (sec)->sreloc;
1834 	  srel->reloc_count = 0;
1835 	}
1836     }
1837   else
1838     {
1839       /* Remove the reserved space for compact relative relocations.  */
1840       if (count)
1841 	{
1842 	  asection *sgot = htab->elf.sgot;
1843 	  asection *srelgot = htab->elf.srelgot;
1844 
1845 	  for (i = 0; i < count; i++)
1846 	    {
1847 	      sec = htab->relative_reloc.data[i].sec;
1848 	      if (sec == sgot)
1849 		srel = srelgot;
1850 	      else
1851 		srel = elf_section_data (sec)->sreloc;
1852 	      srel->size -= htab->sizeof_reloc;
1853 	    }
1854 	}
1855     }
1856 
1857   /* Size unaligned relative relocations.  */
1858   if (unaligned_count)
1859     elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
1860 					   true, NULL);
1861 
1862   if (count)
1863     {
1864       elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
1865 					     false, NULL);
1866 
1867       /* Sort relative relocations by addresses.  We only need to
1868 	 sort them in the first pass since the relative positions
1869 	 won't change.  */
1870       if (htab->generate_relative_reloc_pass == 0)
1871 	qsort (htab->relative_reloc.data, count,
1872 	       sizeof (struct elf_x86_relative_reloc_record),
1873 	       elf_x86_relative_reloc_compare);
1874 
1875       elf_x86_compute_dl_relr_bitmap (info, htab, need_layout);
1876     }
1877 
1878   htab->generate_relative_reloc_pass++;
1879 
1880   return true;
1881 }
1882 
1883 bool
_bfd_elf_x86_finish_relative_relocs(struct bfd_link_info * info)1884 _bfd_elf_x86_finish_relative_relocs (struct bfd_link_info *info)
1885 {
1886   struct elf_x86_link_hash_table *htab;
1887   const struct elf_backend_data *bed;
1888   Elf_Internal_Rela outrel;
1889   bool is_x86_64;
1890   bfd_size_type count;
1891 
1892   /* Do nothing for ld -r.  */
1893   if (bfd_link_relocatable (info))
1894     return true;
1895 
1896   bed = get_elf_backend_data (info->output_bfd);
1897   htab = elf_x86_hash_table (info, bed->target_id);
1898   if (htab == NULL)
1899     return false;
1900 
1901   is_x86_64 = bed->target_id == X86_64_ELF_DATA;
1902 
1903   outrel.r_info = htab->r_info (0, htab->relative_r_type);
1904 
1905   if (htab->unaligned_relative_reloc.count)
1906     elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
1907 					   true, &outrel);
1908 
1909   count = htab->relative_reloc.count;
1910   if (count)
1911     {
1912       elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
1913 					     false, &outrel);
1914 
1915       elf_x86_compute_dl_relr_bitmap (info, htab, NULL);
1916 
1917       elf_x86_write_dl_relr_bitmap (info, htab);
1918     }
1919 
1920   return true;
1921 }
1922 
1923 bool
_bfd_elf_x86_valid_reloc_p(asection * input_section,struct bfd_link_info * info,struct elf_x86_link_hash_table * htab,const Elf_Internal_Rela * rel,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym,Elf_Internal_Shdr * symtab_hdr,bool * no_dynreloc_p)1924 _bfd_elf_x86_valid_reloc_p (asection *input_section,
1925 			    struct bfd_link_info *info,
1926 			    struct elf_x86_link_hash_table *htab,
1927 			    const Elf_Internal_Rela *rel,
1928 			    struct elf_link_hash_entry *h,
1929 			    Elf_Internal_Sym *sym,
1930 			    Elf_Internal_Shdr *symtab_hdr,
1931 			    bool *no_dynreloc_p)
1932 {
1933   bool valid_p = true;
1934 
1935   *no_dynreloc_p = false;
1936 
1937   /* Check If relocation against non-preemptible absolute symbol is
1938      valid in PIC.  FIXME: Can't use SYMBOL_REFERENCES_LOCAL_P since
1939      it may call _bfd_elf_link_hide_sym_by_version and result in
1940      ld-elfvers/ vers21 test failure.  */
1941   if (bfd_link_pic (info)
1942       && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
1943     {
1944       const struct elf_backend_data *bed;
1945       unsigned int r_type;
1946       Elf_Internal_Rela irel;
1947 
1948       /* Skip non-absolute symbol.  */
1949       if (h)
1950 	{
1951 	  if (!ABS_SYMBOL_P (h))
1952 	    return valid_p;
1953 	}
1954       else if (sym->st_shndx != SHN_ABS)
1955 	return valid_p;
1956 
1957       bed = get_elf_backend_data (input_section->owner);
1958       r_type = ELF32_R_TYPE (rel->r_info);
1959       irel = *rel;
1960 
1961       /* Only allow relocations against absolute symbol, which can be
1962 	 resolved as absolute value + addend.  GOTPCREL and GOT32
1963 	 relocations are allowed since absolute value + addend is
1964 	 stored in the GOT slot.  */
1965       if (bed->target_id == X86_64_ELF_DATA)
1966 	{
1967 	  r_type &= ~R_X86_64_converted_reloc_bit;
1968 	  valid_p = (r_type == R_X86_64_64
1969 		     || r_type == R_X86_64_32
1970 		     || r_type == R_X86_64_32S
1971 		     || r_type == R_X86_64_16
1972 		     || r_type == R_X86_64_8
1973 		     || r_type == R_X86_64_GOTPCREL
1974 		     || r_type == R_X86_64_GOTPCRELX
1975 		     || r_type == R_X86_64_REX_GOTPCRELX);
1976 	  if (!valid_p)
1977 	    {
1978 	      unsigned int r_symndx = htab->r_sym (rel->r_info);
1979 	      irel.r_info = htab->r_info (r_symndx, r_type);
1980 	    }
1981 	}
1982       else
1983 	valid_p = (r_type == R_386_32
1984 		   || r_type == R_386_16
1985 		   || r_type == R_386_8
1986 		   || r_type == R_386_GOT32
1987 		   || r_type == R_386_GOT32X);
1988 
1989       if (valid_p)
1990 	*no_dynreloc_p = true;
1991       else
1992 	{
1993 	  const char *name;
1994 	  arelent internal_reloc;
1995 
1996 	  if (!bed->elf_info_to_howto (input_section->owner,
1997 				       &internal_reloc, &irel)
1998 	      || internal_reloc.howto == NULL)
1999 	    abort ();
2000 
2001 	  if (h)
2002 	    name = h->root.root.string;
2003 	  else
2004 	    name = bfd_elf_sym_name (input_section->owner, symtab_hdr,
2005 				     sym, NULL);
2006 	  info->callbacks->einfo
2007 	    /* xgettext:c-format */
2008 	    (_("%F%P: %pB: relocation %s against absolute symbol "
2009 	       "`%s' in section `%pA' is disallowed\n"),
2010 	     input_section->owner, internal_reloc.howto->name, name,
2011 	     input_section);
2012 	  bfd_set_error (bfd_error_bad_value);
2013 	}
2014     }
2015 
2016   return valid_p;
2017 }
2018 
2019 /* Set the sizes of the dynamic sections.  */
2020 
2021 bool
_bfd_x86_elf_size_dynamic_sections(bfd * output_bfd,struct bfd_link_info * info)2022 _bfd_x86_elf_size_dynamic_sections (bfd *output_bfd,
2023 				    struct bfd_link_info *info)
2024 {
2025   struct elf_x86_link_hash_table *htab;
2026   bfd *dynobj;
2027   asection *s;
2028   bool relocs;
2029   bfd *ibfd;
2030   const struct elf_backend_data *bed
2031     = get_elf_backend_data (output_bfd);
2032 
2033   htab = elf_x86_hash_table (info, bed->target_id);
2034   if (htab == NULL)
2035     return false;
2036   dynobj = htab->elf.dynobj;
2037   if (dynobj == NULL)
2038     abort ();
2039 
2040   /* Set up .got offsets for local syms, and space for local dynamic
2041      relocs.  */
2042   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2043     {
2044       bfd_signed_vma *local_got;
2045       bfd_signed_vma *end_local_got;
2046       char *local_tls_type;
2047       bfd_vma *local_tlsdesc_gotent;
2048       bfd_size_type locsymcount;
2049       Elf_Internal_Shdr *symtab_hdr;
2050       asection *srel;
2051 
2052       if (! is_x86_elf (ibfd, htab))
2053 	continue;
2054 
2055       for (s = ibfd->sections; s != NULL; s = s->next)
2056 	{
2057 	  struct elf_dyn_relocs *p;
2058 
2059 	  for (p = ((struct elf_dyn_relocs *)
2060 		     elf_section_data (s)->local_dynrel);
2061 	       p != NULL;
2062 	       p = p->next)
2063 	    {
2064 	      if (!bfd_is_abs_section (p->sec)
2065 		  && bfd_is_abs_section (p->sec->output_section))
2066 		{
2067 		  /* Input section has been discarded, either because
2068 		     it is a copy of a linkonce section or due to
2069 		     linker script /DISCARD/, so we'll be discarding
2070 		     the relocs too.  */
2071 		}
2072 	      else if (htab->elf.target_os == is_vxworks
2073 		       && strcmp (p->sec->output_section->name,
2074 				  ".tls_vars") == 0)
2075 		{
2076 		  /* Relocations in vxworks .tls_vars sections are
2077 		     handled specially by the loader.  */
2078 		}
2079 	      else if (p->count != 0)
2080 		{
2081 		  srel = elf_section_data (p->sec)->sreloc;
2082 		  srel->size += p->count * htab->sizeof_reloc;
2083 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0
2084 		      && (info->flags & DF_TEXTREL) == 0)
2085 		    {
2086 		      info->flags |= DF_TEXTREL;
2087 		      if (bfd_link_textrel_check (info))
2088 			/* xgettext:c-format */
2089 			info->callbacks->einfo
2090 			  (_("%P: %pB: warning: relocation "
2091 			     "in read-only section `%pA'\n"),
2092 			   p->sec->owner, p->sec);
2093 		    }
2094 		}
2095 	    }
2096 	}
2097 
2098       local_got = elf_local_got_refcounts (ibfd);
2099       if (!local_got)
2100 	continue;
2101 
2102       symtab_hdr = &elf_symtab_hdr (ibfd);
2103       locsymcount = symtab_hdr->sh_info;
2104       end_local_got = local_got + locsymcount;
2105       local_tls_type = elf_x86_local_got_tls_type (ibfd);
2106       local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
2107       s = htab->elf.sgot;
2108       srel = htab->elf.srelgot;
2109       for (; local_got < end_local_got;
2110 	   ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2111 	{
2112 	  *local_tlsdesc_gotent = (bfd_vma) -1;
2113 	  if (*local_got > 0)
2114 	    {
2115 	      if (GOT_TLS_GDESC_P (*local_tls_type))
2116 		{
2117 		  *local_tlsdesc_gotent = htab->elf.sgotplt->size
2118 		    - elf_x86_compute_jump_table_size (htab);
2119 		  htab->elf.sgotplt->size += 2 * htab->got_entry_size;
2120 		  *local_got = (bfd_vma) -2;
2121 		}
2122 	      if (! GOT_TLS_GDESC_P (*local_tls_type)
2123 		  || GOT_TLS_GD_P (*local_tls_type))
2124 		{
2125 		  *local_got = s->size;
2126 		  s->size += htab->got_entry_size;
2127 		  if (GOT_TLS_GD_P (*local_tls_type)
2128 		      || *local_tls_type == GOT_TLS_IE_BOTH)
2129 		    s->size += htab->got_entry_size;
2130 		}
2131 	      if ((bfd_link_pic (info) && *local_tls_type != GOT_ABS)
2132 		  || GOT_TLS_GD_ANY_P (*local_tls_type)
2133 		  || (*local_tls_type & GOT_TLS_IE))
2134 		{
2135 		  if (*local_tls_type == GOT_TLS_IE_BOTH)
2136 		    srel->size += 2 * htab->sizeof_reloc;
2137 		  else if (GOT_TLS_GD_P (*local_tls_type)
2138 			   || ! GOT_TLS_GDESC_P (*local_tls_type))
2139 		    srel->size += htab->sizeof_reloc;
2140 		  if (GOT_TLS_GDESC_P (*local_tls_type))
2141 		    {
2142 		      htab->elf.srelplt->size += htab->sizeof_reloc;
2143 		      if (bed->target_id == X86_64_ELF_DATA)
2144 			htab->elf.tlsdesc_plt = (bfd_vma) -1;
2145 		    }
2146 		}
2147 	    }
2148 	  else
2149 	    *local_got = (bfd_vma) -1;
2150 	}
2151     }
2152 
2153   if (htab->tls_ld_or_ldm_got.refcount > 0)
2154     {
2155       /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2156 	 or R_X86_64_TLSLD relocs.  */
2157       htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
2158       htab->elf.sgot->size += 2 * htab->got_entry_size;
2159       htab->elf.srelgot->size += htab->sizeof_reloc;
2160     }
2161   else
2162     htab->tls_ld_or_ldm_got.offset = -1;
2163 
2164   /* Allocate global sym .plt and .got entries, and space for global
2165      sym dynamic relocs.  */
2166   elf_link_hash_traverse (&htab->elf, elf_x86_allocate_dynrelocs,
2167 			  info);
2168 
2169   /* Allocate .plt and .got entries, and space for local symbols.  */
2170   htab_traverse (htab->loc_hash_table, elf_x86_allocate_local_dynreloc,
2171 		 info);
2172 
2173   /* For every jump slot reserved in the sgotplt, reloc_count is
2174      incremented.  However, when we reserve space for TLS descriptors,
2175      it's not incremented, so in order to compute the space reserved
2176      for them, it suffices to multiply the reloc count by the jump
2177      slot size.
2178 
2179      PR ld/13302: We start next_irelative_index at the end of .rela.plt
2180      so that R_{386,X86_64}_IRELATIVE entries come last.  */
2181   if (htab->elf.srelplt)
2182     {
2183       htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
2184       htab->sgotplt_jump_table_size
2185 	= elf_x86_compute_jump_table_size (htab);
2186       htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
2187     }
2188   else if (htab->elf.irelplt)
2189     htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
2190 
2191   if (htab->elf.tlsdesc_plt)
2192     {
2193       /* NB: tlsdesc_plt is set only for x86-64.  If we're not using
2194 	 lazy TLS relocations, don't generate the PLT and GOT entries
2195 	 they require.  */
2196       if ((info->flags & DF_BIND_NOW))
2197 	htab->elf.tlsdesc_plt = 0;
2198       else
2199 	{
2200 	  htab->elf.tlsdesc_got = htab->elf.sgot->size;
2201 	  htab->elf.sgot->size += htab->got_entry_size;
2202 	  /* Reserve room for the initial entry.
2203 	     FIXME: we could probably do away with it in this case.  */
2204 	  if (htab->elf.splt->size == 0)
2205 	    htab->elf.splt->size = htab->plt.plt_entry_size;
2206 	  htab->elf.tlsdesc_plt = htab->elf.splt->size;
2207 	  htab->elf.splt->size += htab->plt.plt_entry_size;
2208 	}
2209     }
2210 
2211   if (htab->elf.sgotplt)
2212     {
2213       /* Don't allocate .got.plt section if there are no GOT nor PLT
2214 	 entries and there is no reference to _GLOBAL_OFFSET_TABLE_.  */
2215       if ((htab->elf.hgot == NULL
2216 	   || !htab->got_referenced)
2217 	  && (htab->elf.sgotplt->size == bed->got_header_size)
2218 	  && (htab->elf.splt == NULL
2219 	      || htab->elf.splt->size == 0)
2220 	  && (htab->elf.sgot == NULL
2221 	      || htab->elf.sgot->size == 0)
2222 	  && (htab->elf.iplt == NULL
2223 	      || htab->elf.iplt->size == 0)
2224 	  && (htab->elf.igotplt == NULL
2225 	      || htab->elf.igotplt->size == 0))
2226 	{
2227 	  htab->elf.sgotplt->size = 0;
2228 	  /* Solaris requires to keep _GLOBAL_OFFSET_TABLE_ even if it
2229 	     isn't used.  */
2230 	  if (htab->elf.hgot != NULL
2231 	      && htab->elf.target_os != is_solaris)
2232 	    {
2233 	      /* Remove the unused _GLOBAL_OFFSET_TABLE_ from symbol
2234 		 table. */
2235 	      htab->elf.hgot->root.type = bfd_link_hash_undefined;
2236 	      htab->elf.hgot->root.u.undef.abfd
2237 		= htab->elf.hgot->root.u.def.section->owner;
2238 	      htab->elf.hgot->root.linker_def = 0;
2239 	      htab->elf.hgot->ref_regular = 0;
2240 	      htab->elf.hgot->def_regular = 0;
2241 	    }
2242 	}
2243     }
2244 
2245   if (_bfd_elf_eh_frame_present (info))
2246     {
2247       if (htab->plt_eh_frame != NULL
2248 	  && htab->elf.splt != NULL
2249 	  && htab->elf.splt->size != 0
2250 	  && !bfd_is_abs_section (htab->elf.splt->output_section))
2251 	htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
2252 
2253       if (htab->plt_got_eh_frame != NULL
2254 	  && htab->plt_got != NULL
2255 	  && htab->plt_got->size != 0
2256 	  && !bfd_is_abs_section (htab->plt_got->output_section))
2257 	htab->plt_got_eh_frame->size
2258 	  = htab->non_lazy_plt->eh_frame_plt_size;
2259 
2260       /* Unwind info for the second PLT and .plt.got sections are
2261 	 identical.  */
2262       if (htab->plt_second_eh_frame != NULL
2263 	  && htab->plt_second != NULL
2264 	  && htab->plt_second->size != 0
2265 	  && !bfd_is_abs_section (htab->plt_second->output_section))
2266 	htab->plt_second_eh_frame->size
2267 	  = htab->non_lazy_plt->eh_frame_plt_size;
2268     }
2269 
2270   /* We now have determined the sizes of the various dynamic sections.
2271      Allocate memory for them.  */
2272   relocs = false;
2273   for (s = dynobj->sections; s != NULL; s = s->next)
2274     {
2275       bool strip_section = true;
2276 
2277       if ((s->flags & SEC_LINKER_CREATED) == 0)
2278 	continue;
2279 
2280       /* The .relr.dyn section for compact relative relocation will
2281 	 be filled later.  */
2282       if (s == htab->elf.srelrdyn)
2283 	continue;
2284 
2285       if (s == htab->elf.splt
2286 	  || s == htab->elf.sgot)
2287 	{
2288 	  /* Strip this section if we don't need it; see the
2289 	     comment below.  */
2290 	  /* We'd like to strip these sections if they aren't needed, but if
2291 	     we've exported dynamic symbols from them we must leave them.
2292 	     It's too late to tell BFD to get rid of the symbols.  */
2293 
2294 	  if (htab->elf.hplt != NULL)
2295 	    strip_section = false;
2296 	}
2297       else if (s == htab->elf.sgotplt
2298 	       || s == htab->elf.iplt
2299 	       || s == htab->elf.igotplt
2300 	       || s == htab->plt_second
2301 	       || s == htab->plt_got
2302 	       || s == htab->plt_eh_frame
2303 	       || s == htab->plt_got_eh_frame
2304 	       || s == htab->plt_second_eh_frame
2305 	       || s == htab->elf.sdynbss
2306 	       || s == htab->elf.sdynrelro)
2307 	{
2308 	  /* Strip these too.  */
2309 	}
2310       else if (htab->is_reloc_section (bfd_section_name (s)))
2311 	{
2312 	  if (s->size != 0
2313 	      && s != htab->elf.srelplt
2314 	      && s != htab->srelplt2)
2315 	    relocs = true;
2316 
2317 	  /* We use the reloc_count field as a counter if we need
2318 	     to copy relocs into the output file.  */
2319 	  if (s != htab->elf.srelplt)
2320 	    s->reloc_count = 0;
2321 	}
2322       else
2323 	{
2324 	  /* It's not one of our sections, so don't allocate space.  */
2325 	  continue;
2326 	}
2327 
2328       if (s->size == 0)
2329 	{
2330 	  /* If we don't need this section, strip it from the
2331 	     output file.  This is mostly to handle .rel.bss and
2332 	     .rel.plt.  We must create both sections in
2333 	     create_dynamic_sections, because they must be created
2334 	     before the linker maps input sections to output
2335 	     sections.  The linker does that before
2336 	     adjust_dynamic_symbol is called, and it is that
2337 	     function which decides whether anything needs to go
2338 	     into these sections.  */
2339 	  if (strip_section)
2340 	    s->flags |= SEC_EXCLUDE;
2341 	  continue;
2342 	}
2343 
2344       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2345 	continue;
2346 
2347       /* NB: Initially, the iplt section has minimal alignment to
2348 	 avoid moving dot of the following section backwards when
2349 	 it is empty.  Update its section alignment now since it
2350 	 is non-empty.  */
2351       if (s == htab->elf.iplt)
2352 	bfd_set_section_alignment (s, htab->plt.iplt_alignment);
2353 
2354       /* Allocate memory for the section contents.  We use bfd_zalloc
2355 	 here in case unused entries are not reclaimed before the
2356 	 section's contents are written out.  This should not happen,
2357 	 but this way if it does, we get a R_386_NONE or R_X86_64_NONE
2358 	 reloc instead of garbage.  */
2359       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
2360       if (s->contents == NULL)
2361 	return false;
2362     }
2363 
2364   if (htab->plt_eh_frame != NULL
2365       && htab->plt_eh_frame->contents != NULL)
2366     {
2367       memcpy (htab->plt_eh_frame->contents,
2368 	      htab->plt.eh_frame_plt,
2369 	      htab->plt_eh_frame->size);
2370       bfd_put_32 (dynobj, htab->elf.splt->size,
2371 		  htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
2372     }
2373 
2374   if (htab->plt_got_eh_frame != NULL
2375       && htab->plt_got_eh_frame->contents != NULL)
2376     {
2377       memcpy (htab->plt_got_eh_frame->contents,
2378 	      htab->non_lazy_plt->eh_frame_plt,
2379 	      htab->plt_got_eh_frame->size);
2380       bfd_put_32 (dynobj, htab->plt_got->size,
2381 		  (htab->plt_got_eh_frame->contents
2382 		   + PLT_FDE_LEN_OFFSET));
2383     }
2384 
2385   if (htab->plt_second_eh_frame != NULL
2386       && htab->plt_second_eh_frame->contents != NULL)
2387     {
2388       memcpy (htab->plt_second_eh_frame->contents,
2389 	      htab->non_lazy_plt->eh_frame_plt,
2390 	      htab->plt_second_eh_frame->size);
2391       bfd_put_32 (dynobj, htab->plt_second->size,
2392 		  (htab->plt_second_eh_frame->contents
2393 		   + PLT_FDE_LEN_OFFSET));
2394     }
2395 
2396   return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
2397 						  relocs);
2398 }
2399 
2400 /* Finish up the x86 dynamic sections.  */
2401 
2402 struct elf_x86_link_hash_table *
_bfd_x86_elf_finish_dynamic_sections(bfd * output_bfd,struct bfd_link_info * info)2403 _bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd,
2404 				      struct bfd_link_info *info)
2405 {
2406   struct elf_x86_link_hash_table *htab;
2407   const struct elf_backend_data *bed;
2408   bfd *dynobj;
2409   asection *sdyn;
2410   bfd_byte *dyncon, *dynconend;
2411   bfd_size_type sizeof_dyn;
2412 
2413   bed = get_elf_backend_data (output_bfd);
2414   htab = elf_x86_hash_table (info, bed->target_id);
2415   if (htab == NULL)
2416     return htab;
2417 
2418   dynobj = htab->elf.dynobj;
2419   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2420 
2421   /* GOT is always created in setup_gnu_properties.  But it may not be
2422      needed.  .got.plt section may be needed for static IFUNC.  */
2423   if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
2424     {
2425       bfd_vma dynamic_addr;
2426 
2427       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
2428 	{
2429 	  _bfd_error_handler
2430 	    (_("discarded output section: `%pA'"), htab->elf.sgotplt);
2431 	  return NULL;
2432 	}
2433 
2434       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
2435 	= htab->got_entry_size;
2436 
2437       dynamic_addr = (sdyn == NULL
2438 		      ? (bfd_vma) 0
2439 		      : sdyn->output_section->vma + sdyn->output_offset);
2440 
2441       /* Set the first entry in the global offset table to the address
2442 	 of the dynamic section.  Write GOT[1] and GOT[2], needed for
2443 	 the dynamic linker.  */
2444       if (htab->got_entry_size == 8)
2445 	{
2446 	  bfd_put_64 (output_bfd, dynamic_addr,
2447 		      htab->elf.sgotplt->contents);
2448 	  bfd_put_64 (output_bfd, (bfd_vma) 0,
2449 		      htab->elf.sgotplt->contents + 8);
2450 	  bfd_put_64 (output_bfd, (bfd_vma) 0,
2451 		      htab->elf.sgotplt->contents + 8*2);
2452 	}
2453       else
2454 	{
2455 	  bfd_put_32 (output_bfd, dynamic_addr,
2456 		      htab->elf.sgotplt->contents);
2457 	  bfd_put_32 (output_bfd, 0,
2458 		      htab->elf.sgotplt->contents + 4);
2459 	  bfd_put_32 (output_bfd, 0,
2460 		      htab->elf.sgotplt->contents + 4*2);
2461 	}
2462     }
2463 
2464   if (!htab->elf.dynamic_sections_created)
2465     return htab;
2466 
2467   if (sdyn == NULL || htab->elf.sgot == NULL)
2468     abort ();
2469 
2470   sizeof_dyn = bed->s->sizeof_dyn;
2471   dyncon = sdyn->contents;
2472   dynconend = sdyn->contents + sdyn->size;
2473   for (; dyncon < dynconend; dyncon += sizeof_dyn)
2474     {
2475       Elf_Internal_Dyn dyn;
2476       asection *s;
2477 
2478       (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
2479 
2480       switch (dyn.d_tag)
2481 	{
2482 	default:
2483 	  if (htab->elf.target_os == is_vxworks
2484 	      && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
2485 	    break;
2486 	  continue;
2487 
2488 	case DT_PLTGOT:
2489 	  s = htab->elf.sgotplt;
2490 	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2491 	  break;
2492 
2493 	case DT_JMPREL:
2494 	  dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
2495 	  break;
2496 
2497 	case DT_PLTRELSZ:
2498 	  s = htab->elf.srelplt->output_section;
2499 	  dyn.d_un.d_val = s->size;
2500 	  break;
2501 
2502 	case DT_TLSDESC_PLT:
2503 	  s = htab->elf.splt;
2504 	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
2505 	    + htab->elf.tlsdesc_plt;
2506 	  break;
2507 
2508 	case DT_TLSDESC_GOT:
2509 	  s = htab->elf.sgot;
2510 	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
2511 	    + htab->elf.tlsdesc_got;
2512 	  break;
2513 	}
2514 
2515       (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
2516     }
2517 
2518   if (htab->plt_got != NULL && htab->plt_got->size > 0)
2519     elf_section_data (htab->plt_got->output_section)
2520       ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
2521 
2522   if (htab->plt_second != NULL && htab->plt_second->size > 0)
2523     elf_section_data (htab->plt_second->output_section)
2524       ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
2525 
2526   /* Adjust .eh_frame for .plt section.  */
2527   if (htab->plt_eh_frame != NULL
2528       && htab->plt_eh_frame->contents != NULL)
2529     {
2530       if (htab->elf.splt != NULL
2531 	  && htab->elf.splt->size != 0
2532 	  && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
2533 	  && htab->elf.splt->output_section != NULL
2534 	  && htab->plt_eh_frame->output_section != NULL)
2535 	{
2536 	  bfd_vma plt_start = htab->elf.splt->output_section->vma;
2537 	  bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
2538 				   + htab->plt_eh_frame->output_offset
2539 				   + PLT_FDE_START_OFFSET;
2540 	  bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2541 			     htab->plt_eh_frame->contents
2542 			     + PLT_FDE_START_OFFSET);
2543 	}
2544 
2545       if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
2546 	{
2547 	  if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2548 						 htab->plt_eh_frame,
2549 						 htab->plt_eh_frame->contents))
2550 	    return NULL;
2551 	}
2552     }
2553 
2554   /* Adjust .eh_frame for .plt.got section.  */
2555   if (htab->plt_got_eh_frame != NULL
2556       && htab->plt_got_eh_frame->contents != NULL)
2557     {
2558       if (htab->plt_got != NULL
2559 	  && htab->plt_got->size != 0
2560 	  && (htab->plt_got->flags & SEC_EXCLUDE) == 0
2561 	  && htab->plt_got->output_section != NULL
2562 	  && htab->plt_got_eh_frame->output_section != NULL)
2563 	{
2564 	  bfd_vma plt_start = htab->plt_got->output_section->vma;
2565 	  bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
2566 				   + htab->plt_got_eh_frame->output_offset
2567 				   + PLT_FDE_START_OFFSET;
2568 	  bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2569 			     htab->plt_got_eh_frame->contents
2570 			     + PLT_FDE_START_OFFSET);
2571 	}
2572       if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
2573 	{
2574 	  if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2575 						 htab->plt_got_eh_frame,
2576 						 htab->plt_got_eh_frame->contents))
2577 	    return NULL;
2578 	}
2579     }
2580 
2581   /* Adjust .eh_frame for the second PLT section.  */
2582   if (htab->plt_second_eh_frame != NULL
2583       && htab->plt_second_eh_frame->contents != NULL)
2584     {
2585       if (htab->plt_second != NULL
2586 	  && htab->plt_second->size != 0
2587 	  && (htab->plt_second->flags & SEC_EXCLUDE) == 0
2588 	  && htab->plt_second->output_section != NULL
2589 	  && htab->plt_second_eh_frame->output_section != NULL)
2590 	{
2591 	  bfd_vma plt_start = htab->plt_second->output_section->vma;
2592 	  bfd_vma eh_frame_start
2593 	    = (htab->plt_second_eh_frame->output_section->vma
2594 	       + htab->plt_second_eh_frame->output_offset
2595 	       + PLT_FDE_START_OFFSET);
2596 	  bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
2597 			     htab->plt_second_eh_frame->contents
2598 			     + PLT_FDE_START_OFFSET);
2599 	}
2600       if (htab->plt_second_eh_frame->sec_info_type
2601 	  == SEC_INFO_TYPE_EH_FRAME)
2602 	{
2603 	  if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
2604 						 htab->plt_second_eh_frame,
2605 						 htab->plt_second_eh_frame->contents))
2606 	    return NULL;
2607 	}
2608     }
2609 
2610   if (htab->elf.sgot && htab->elf.sgot->size > 0)
2611     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
2612       = htab->got_entry_size;
2613 
2614   return htab;
2615 }
2616 
2617 
2618 bool
_bfd_x86_elf_always_size_sections(bfd * output_bfd,struct bfd_link_info * info)2619 _bfd_x86_elf_always_size_sections (bfd *output_bfd,
2620 				   struct bfd_link_info *info)
2621 {
2622   asection *tls_sec = elf_hash_table (info)->tls_sec;
2623 
2624   if (tls_sec)
2625     {
2626       struct elf_link_hash_entry *tlsbase;
2627 
2628       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2629 				      "_TLS_MODULE_BASE_",
2630 				      false, false, false);
2631 
2632       if (tlsbase && tlsbase->type == STT_TLS)
2633 	{
2634 	  struct elf_x86_link_hash_table *htab;
2635 	  struct bfd_link_hash_entry *bh = NULL;
2636 	  const struct elf_backend_data *bed
2637 	    = get_elf_backend_data (output_bfd);
2638 
2639 	  htab = elf_x86_hash_table (info, bed->target_id);
2640 	  if (htab == NULL)
2641 	    return false;
2642 
2643 	  if (!(_bfd_generic_link_add_one_symbol
2644 		(info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2645 		 tls_sec, 0, NULL, false,
2646 		 bed->collect, &bh)))
2647 	    return false;
2648 
2649 	  htab->tls_module_base = bh;
2650 
2651 	  tlsbase = (struct elf_link_hash_entry *)bh;
2652 	  tlsbase->def_regular = 1;
2653 	  tlsbase->other = STV_HIDDEN;
2654 	  tlsbase->root.linker_def = 1;
2655 	  (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
2656 	}
2657     }
2658 
2659   return true;
2660 }
2661 
2662 void
_bfd_x86_elf_merge_symbol_attribute(struct elf_link_hash_entry * h,unsigned int st_other,bool definition,bool dynamic ATTRIBUTE_UNUSED)2663 _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
2664 				     unsigned int st_other,
2665 				     bool definition,
2666 				     bool dynamic ATTRIBUTE_UNUSED)
2667 {
2668   if (definition)
2669     {
2670       struct elf_x86_link_hash_entry *eh
2671 	= (struct elf_x86_link_hash_entry *) h;
2672       eh->def_protected = ELF_ST_VISIBILITY (st_other) == STV_PROTECTED;
2673     }
2674 }
2675 
2676 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2677 
2678 void
_bfd_x86_elf_copy_indirect_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * dir,struct elf_link_hash_entry * ind)2679 _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
2680 				   struct elf_link_hash_entry *dir,
2681 				   struct elf_link_hash_entry *ind)
2682 {
2683   struct elf_x86_link_hash_entry *edir, *eind;
2684 
2685   edir = (struct elf_x86_link_hash_entry *) dir;
2686   eind = (struct elf_x86_link_hash_entry *) ind;
2687 
2688   if (ind->root.type == bfd_link_hash_indirect
2689       && dir->got.refcount <= 0)
2690     {
2691       edir->tls_type = eind->tls_type;
2692       eind->tls_type = GOT_UNKNOWN;
2693     }
2694 
2695   /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
2696      generate a R_386_COPY reloc.  */
2697   edir->gotoff_ref |= eind->gotoff_ref;
2698 
2699   edir->zero_undefweak |= eind->zero_undefweak;
2700 
2701   if (ELIMINATE_COPY_RELOCS
2702       && ind->root.type != bfd_link_hash_indirect
2703       && dir->dynamic_adjusted)
2704     {
2705       /* If called to transfer flags for a weakdef during processing
2706 	 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
2707 	 We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
2708       if (dir->versioned != versioned_hidden)
2709 	dir->ref_dynamic |= ind->ref_dynamic;
2710       dir->ref_regular |= ind->ref_regular;
2711       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
2712       dir->needs_plt |= ind->needs_plt;
2713       dir->pointer_equality_needed |= ind->pointer_equality_needed;
2714     }
2715   else
2716     _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2717 }
2718 
2719 /* Remove undefined weak symbol from the dynamic symbol table if it
2720    is resolved to 0.   */
2721 
2722 bool
_bfd_x86_elf_fixup_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h)2723 _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
2724 			   struct elf_link_hash_entry *h)
2725 {
2726   if (h->dynindx != -1
2727       && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, elf_x86_hash_entry (h)))
2728     {
2729       h->dynindx = -1;
2730       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
2731 			      h->dynstr_index);
2732     }
2733   return true;
2734 }
2735 
2736 /* Change the STT_GNU_IFUNC symbol defined in position-dependent
2737    executable into the normal function symbol and set its address
2738    to its PLT entry, which should be resolved by R_*_IRELATIVE at
2739    run-time.  */
2740 
2741 void
_bfd_x86_elf_link_fixup_ifunc_symbol(struct bfd_link_info * info,struct elf_x86_link_hash_table * htab,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)2742 _bfd_x86_elf_link_fixup_ifunc_symbol (struct bfd_link_info *info,
2743 				      struct elf_x86_link_hash_table *htab,
2744 				      struct elf_link_hash_entry *h,
2745 				      Elf_Internal_Sym *sym)
2746 {
2747   if (bfd_link_pde (info)
2748       && h->def_regular
2749       && h->dynindx != -1
2750       && h->plt.offset != (bfd_vma) -1
2751       && h->type == STT_GNU_IFUNC)
2752     {
2753       asection *plt_s;
2754       bfd_vma plt_offset;
2755       bfd *output_bfd = info->output_bfd;
2756 
2757       if (htab->plt_second)
2758 	{
2759 	  struct elf_x86_link_hash_entry *eh
2760 	    = (struct elf_x86_link_hash_entry *) h;
2761 
2762 	  plt_s = htab->plt_second;
2763 	  plt_offset = eh->plt_second.offset;
2764 	}
2765       else
2766 	{
2767 	  plt_s = htab->elf.splt;
2768 	  plt_offset = h->plt.offset;
2769 	}
2770 
2771       sym->st_size = 0;
2772       sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
2773       sym->st_shndx
2774 	= _bfd_elf_section_from_bfd_section (output_bfd,
2775 					     plt_s->output_section);
2776       sym->st_value = (plt_s->output_section->vma
2777 		       + plt_s->output_offset + plt_offset);
2778     }
2779 }
2780 
2781 /* Report relative relocation.  */
2782 
2783 void
_bfd_x86_elf_link_report_relative_reloc(struct bfd_link_info * info,asection * asect,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym,const char * reloc_name,const void * reloc)2784 _bfd_x86_elf_link_report_relative_reloc
2785   (struct bfd_link_info *info, asection *asect,
2786    struct elf_link_hash_entry *h, Elf_Internal_Sym *sym,
2787    const char *reloc_name, const void *reloc)
2788 {
2789   const char *name;
2790   bfd *abfd;
2791   const Elf_Internal_Rela *rel = (const Elf_Internal_Rela *) reloc;
2792 
2793   /* Use the output BFD for linker created sections.  */
2794   if ((asect->flags & SEC_LINKER_CREATED) != 0)
2795     abfd = info->output_bfd;
2796   else
2797     abfd = asect->owner;
2798 
2799   if (h != NULL && h->root.root.string != NULL)
2800     name = h->root.root.string;
2801   else
2802     name = bfd_elf_sym_name (abfd, &elf_symtab_hdr (abfd), sym, NULL);
2803 
2804   if (asect->use_rela_p)
2805     info->callbacks->einfo
2806       (_("%pB: %s (offset: 0x%v, info: 0x%v, addend: 0x%v) against "
2807 	 "'%s' " "for section '%pA' in %pB\n"),
2808        info->output_bfd, reloc_name, rel->r_offset, rel->r_info,
2809        rel->r_addend, name, asect, abfd);
2810   else
2811     info->callbacks->einfo
2812       (_("%pB: %s (offset: 0x%v, info: 0x%v) against '%s' for section "
2813 	 "'%pA' in %pB\n"),
2814        info->output_bfd, reloc_name, rel->r_offset, rel->r_info, name,
2815        asect, abfd);
2816 }
2817 
2818 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
2819 
2820 bool
_bfd_x86_elf_hash_symbol(struct elf_link_hash_entry * h)2821 _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
2822 {
2823   if (h->plt.offset != (bfd_vma) -1
2824       && !h->def_regular
2825       && !h->pointer_equality_needed)
2826     return false;
2827 
2828   return _bfd_elf_hash_symbol (h);
2829 }
2830 
2831 /* Adjust a symbol defined by a dynamic object and referenced by a
2832    regular object.  The current definition is in some section of the
2833    dynamic object, but we're not including those sections.  We have to
2834    change the definition to something the rest of the link can
2835    understand.  */
2836 
2837 bool
_bfd_x86_elf_adjust_dynamic_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h)2838 _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2839 				    struct elf_link_hash_entry *h)
2840 {
2841   struct elf_x86_link_hash_table *htab;
2842   asection *s, *srel;
2843   struct elf_x86_link_hash_entry *eh;
2844   struct elf_dyn_relocs *p;
2845   const struct elf_backend_data *bed
2846     = get_elf_backend_data (info->output_bfd);
2847 
2848   eh = (struct elf_x86_link_hash_entry *) h;
2849 
2850   /* Clear GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS if it is turned
2851      on by an input relocatable file and there is a non-GOT/non-PLT
2852      reference from another relocatable file without it.
2853      NB: There can be non-GOT reference in data sections in input with
2854      GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS.  */
2855   if (eh->non_got_ref_without_indirect_extern_access
2856       && info->indirect_extern_access == 1
2857       && bfd_link_executable (info))
2858     {
2859       unsigned int needed_1;
2860       info->indirect_extern_access = 0;
2861       /* Turn off nocopyreloc if implied by indirect_extern_access.  */
2862       if (info->nocopyreloc == 2)
2863 	info->nocopyreloc = 0;
2864       needed_1 = bfd_h_get_32 (info->output_bfd, info->needed_1_p);
2865       needed_1 &= ~GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
2866       bfd_h_put_32 (info->output_bfd, needed_1, info->needed_1_p);
2867     }
2868 
2869   /* STT_GNU_IFUNC symbol must go through PLT. */
2870   if (h->type == STT_GNU_IFUNC)
2871     {
2872       /* All local STT_GNU_IFUNC references must be treate as local
2873 	 calls via local PLT.  */
2874       if (h->ref_regular
2875 	  && SYMBOL_CALLS_LOCAL (info, h))
2876 	{
2877 	  bfd_size_type pc_count = 0, count = 0;
2878 	  struct elf_dyn_relocs **pp;
2879 
2880 	  eh = (struct elf_x86_link_hash_entry *) h;
2881 	  for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
2882 	    {
2883 	      pc_count += p->pc_count;
2884 	      p->count -= p->pc_count;
2885 	      p->pc_count = 0;
2886 	      count += p->count;
2887 	      if (p->count == 0)
2888 		*pp = p->next;
2889 	      else
2890 		pp = &p->next;
2891 	    }
2892 
2893 	  if (pc_count || count)
2894 	    {
2895 	      h->non_got_ref = 1;
2896 	      if (pc_count)
2897 		{
2898 		  /* Increment PLT reference count only for PC-relative
2899 		     references.  */
2900 		  h->needs_plt = 1;
2901 		  if (h->plt.refcount <= 0)
2902 		    h->plt.refcount = 1;
2903 		  else
2904 		    h->plt.refcount += 1;
2905 		}
2906 	    }
2907 
2908 	  /* GOTOFF relocation needs PLT.  */
2909 	  if (eh->gotoff_ref)
2910 	    h->plt.refcount = 1;
2911 	}
2912 
2913       if (h->plt.refcount <= 0)
2914 	{
2915 	  h->plt.offset = (bfd_vma) -1;
2916 	  h->needs_plt = 0;
2917 	}
2918       return true;
2919     }
2920 
2921   /* If this is a function, put it in the procedure linkage table.  We
2922      will fill in the contents of the procedure linkage table later,
2923      when we know the address of the .got section.  */
2924   if (h->type == STT_FUNC
2925       || h->needs_plt)
2926     {
2927       if (h->plt.refcount <= 0
2928 	  || SYMBOL_CALLS_LOCAL (info, h)
2929 	  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2930 	      && h->root.type == bfd_link_hash_undefweak))
2931 	{
2932 	  /* This case can occur if we saw a PLT32 reloc in an input
2933 	     file, but the symbol was never referred to by a dynamic
2934 	     object, or if all references were garbage collected.  In
2935 	     such a case, we don't actually need to build a procedure
2936 	     linkage table, and we can just do a PC32 reloc instead.  */
2937 	  h->plt.offset = (bfd_vma) -1;
2938 	  h->needs_plt = 0;
2939 	}
2940 
2941       return true;
2942     }
2943   else
2944     /* It's possible that we incorrectly decided a .plt reloc was needed
2945      * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
2946        check_relocs.  We can't decide accurately between function and
2947        non-function syms in check-relocs;  Objects loaded later in
2948        the link may change h->type.  So fix it now.  */
2949     h->plt.offset = (bfd_vma) -1;
2950 
2951   /* If this is a weak symbol, and there is a real definition, the
2952      processor independent code will have arranged for us to see the
2953      real definition first, and we can just use the same value.  */
2954   if (h->is_weakalias)
2955     {
2956       struct elf_link_hash_entry *def = weakdef (h);
2957       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2958       h->root.u.def.section = def->root.u.def.section;
2959       h->root.u.def.value = def->root.u.def.value;
2960       if (ELIMINATE_COPY_RELOCS
2961 	  || info->nocopyreloc
2962 	  || SYMBOL_NO_COPYRELOC (info, eh))
2963 	{
2964 	  /* NB: needs_copy is always 0 for i386.  */
2965 	  h->non_got_ref = def->non_got_ref;
2966 	  eh->needs_copy = def->needs_copy;
2967 	}
2968       return true;
2969     }
2970 
2971   /* This is a reference to a symbol defined by a dynamic object which
2972      is not a function.  */
2973 
2974   /* If we are creating a shared library, we must presume that the
2975      only references to the symbol are via the global offset table.
2976      For such cases we need not do anything here; the relocations will
2977      be handled correctly by relocate_section.  */
2978   if (!bfd_link_executable (info))
2979     return true;
2980 
2981   /* If there are no references to this symbol that do not use the
2982      GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
2983      reloc.  NB: gotoff_ref is always 0 for x86-64.  */
2984   if (!h->non_got_ref && !eh->gotoff_ref)
2985     return true;
2986 
2987   /* If -z nocopyreloc was given, we won't generate them either.  */
2988   if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
2989     {
2990       h->non_got_ref = 0;
2991       return true;
2992     }
2993 
2994   htab = elf_x86_hash_table (info, bed->target_id);
2995   if (htab == NULL)
2996     return false;
2997 
2998   /* If there aren't any dynamic relocs in read-only sections nor
2999      R_386_GOTOFF relocation, then we can keep the dynamic relocs and
3000      avoid the copy reloc.  This doesn't work on VxWorks, where we can
3001      not have dynamic relocations (other than copy and jump slot
3002      relocations) in an executable.  */
3003   if (ELIMINATE_COPY_RELOCS
3004       && (bed->target_id == X86_64_ELF_DATA
3005 	  || (!eh->gotoff_ref
3006 	      && htab->elf.target_os != is_vxworks)))
3007     {
3008       /* If we don't find any dynamic relocs in read-only sections,
3009 	 then we'll be keeping the dynamic relocs and avoiding the copy
3010 	 reloc.  */
3011       if (!_bfd_elf_readonly_dynrelocs (h))
3012 	{
3013 	  h->non_got_ref = 0;
3014 	  return true;
3015 	}
3016     }
3017 
3018   /* We must allocate the symbol in our .dynbss section, which will
3019      become part of the .bss section of the executable.  There will be
3020      an entry for this symbol in the .dynsym section.  The dynamic
3021      object will contain position independent code, so all references
3022      from the dynamic object to this symbol will go through the global
3023      offset table.  The dynamic linker will use the .dynsym entry to
3024      determine the address it must put in the global offset table, so
3025      both the dynamic object and the regular object will refer to the
3026      same memory location for the variable.  */
3027 
3028   /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
3029      dynamic linker to copy the initial value out of the dynamic object
3030      and into the runtime process image.  */
3031   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
3032     {
3033       s = htab->elf.sdynrelro;
3034       srel = htab->elf.sreldynrelro;
3035     }
3036   else
3037     {
3038       s = htab->elf.sdynbss;
3039       srel = htab->elf.srelbss;
3040     }
3041   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3042     {
3043       if (eh->def_protected && bfd_link_executable (info))
3044 	for (p = h->dyn_relocs; p != NULL; p = p->next)
3045 	  {
3046 	    /* Disallow copy relocation against non-copyable protected
3047 	       symbol.  */
3048 	    s = p->sec->output_section;
3049 	    if (s != NULL && (s->flags & SEC_READONLY) != 0)
3050 	      {
3051 		info->callbacks->einfo
3052 		  /* xgettext:c-format */
3053 		  (_("%F%P: %pB: copy relocation against non-copyable "
3054 		     "protected symbol `%s' in %pB\n"),
3055 		   p->sec->owner, h->root.root.string,
3056 		   h->root.u.def.section->owner);
3057 		return false;
3058 	      }
3059 	  }
3060 
3061       srel->size += htab->sizeof_reloc;
3062       h->needs_copy = 1;
3063     }
3064 
3065   return _bfd_elf_adjust_dynamic_copy (info, h, s);
3066 }
3067 
3068 void
_bfd_x86_elf_hide_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h,bool force_local)3069 _bfd_x86_elf_hide_symbol (struct bfd_link_info *info,
3070 			  struct elf_link_hash_entry *h,
3071 			  bool force_local)
3072 {
3073   if (h->root.type == bfd_link_hash_undefweak
3074       && info->nointerp
3075       && bfd_link_pie (info))
3076     {
3077       /* When there is no dynamic interpreter in PIE, make the undefined
3078 	 weak symbol dynamic so that PC relative branch to the undefined
3079 	 weak symbol will land to address 0.  */
3080       struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
3081       if (h->plt.refcount > 0
3082 	  || eh->plt_got.refcount > 0)
3083 	return;
3084     }
3085 
3086   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3087 }
3088 
3089 /* Return TRUE if a symbol is referenced locally.  It is similar to
3090    SYMBOL_REFERENCES_LOCAL, but it also checks version script.  It
3091    works in check_relocs.  */
3092 
3093 bool
_bfd_x86_elf_link_symbol_references_local(struct bfd_link_info * info,struct elf_link_hash_entry * h)3094 _bfd_x86_elf_link_symbol_references_local (struct bfd_link_info *info,
3095 					   struct elf_link_hash_entry *h)
3096 {
3097   struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
3098   struct elf_x86_link_hash_table *htab
3099     = (struct elf_x86_link_hash_table *) info->hash;
3100 
3101   if (eh->local_ref > 1)
3102     return true;
3103 
3104   if (eh->local_ref == 1)
3105     return false;
3106 
3107   /* Unversioned symbols defined in regular objects can be forced local
3108      by linker version script.  A weak undefined symbol is forced local
3109      if
3110      1. It has non-default visibility.  Or
3111      2. When building executable, there is no dynamic linker.  Or
3112      3. or "-z nodynamic-undefined-weak" is used.
3113    */
3114   if (_bfd_elf_symbol_refs_local_p (h, info, 1)
3115       || (h->root.type == bfd_link_hash_undefweak
3116 	  && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3117 	      || (bfd_link_executable (info)
3118 		  && htab->interp == NULL)
3119 	      || info->dynamic_undefined_weak == 0))
3120       || ((h->def_regular || ELF_COMMON_DEF_P (h))
3121 	  && info->version_info != NULL
3122 	  && _bfd_elf_link_hide_sym_by_version (info, h)))
3123     {
3124       eh->local_ref = 2;
3125       return true;
3126     }
3127 
3128   eh->local_ref = 1;
3129   return false;
3130 }
3131 
3132 /* Return the section that should be marked against GC for a given
3133    relocation.	*/
3134 
3135 asection *
_bfd_x86_elf_gc_mark_hook(asection * sec,struct bfd_link_info * info,Elf_Internal_Rela * rel,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)3136 _bfd_x86_elf_gc_mark_hook (asection *sec,
3137 			   struct bfd_link_info *info,
3138 			   Elf_Internal_Rela *rel,
3139 			   struct elf_link_hash_entry *h,
3140 			   Elf_Internal_Sym *sym)
3141 {
3142   /* Compiler should optimize this out.  */
3143   if (((unsigned int) R_X86_64_GNU_VTINHERIT
3144        != (unsigned int) R_386_GNU_VTINHERIT)
3145       || ((unsigned int) R_X86_64_GNU_VTENTRY
3146 	  != (unsigned int) R_386_GNU_VTENTRY))
3147     abort ();
3148 
3149   if (h != NULL)
3150     switch (ELF32_R_TYPE (rel->r_info))
3151       {
3152       case R_X86_64_GNU_VTINHERIT:
3153       case R_X86_64_GNU_VTENTRY:
3154 	return NULL;
3155       }
3156 
3157   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
3158 }
3159 
3160 static bfd_vma
elf_i386_get_plt_got_vma(struct elf_x86_plt * plt_p ATTRIBUTE_UNUSED,bfd_vma off,bfd_vma offset ATTRIBUTE_UNUSED,bfd_vma got_addr)3161 elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
3162 			  bfd_vma off,
3163 			  bfd_vma offset ATTRIBUTE_UNUSED,
3164 			  bfd_vma got_addr)
3165 {
3166   return got_addr + off;
3167 }
3168 
3169 static bfd_vma
elf_x86_64_get_plt_got_vma(struct elf_x86_plt * plt_p,bfd_vma off,bfd_vma offset,bfd_vma got_addr ATTRIBUTE_UNUSED)3170 elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
3171 			    bfd_vma off,
3172 			    bfd_vma offset,
3173 			    bfd_vma got_addr ATTRIBUTE_UNUSED)
3174 {
3175   return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
3176 }
3177 
3178 static bool
elf_i386_valid_plt_reloc_p(unsigned int type)3179 elf_i386_valid_plt_reloc_p (unsigned int type)
3180 {
3181   return (type == R_386_JUMP_SLOT
3182 	  || type == R_386_GLOB_DAT
3183 	  || type == R_386_IRELATIVE);
3184 }
3185 
3186 static bool
elf_x86_64_valid_plt_reloc_p(unsigned int type)3187 elf_x86_64_valid_plt_reloc_p (unsigned int type)
3188 {
3189   return (type == R_X86_64_JUMP_SLOT
3190 	  || type == R_X86_64_GLOB_DAT
3191 	  || type == R_X86_64_IRELATIVE);
3192 }
3193 
3194 long
_bfd_x86_elf_get_synthetic_symtab(bfd * abfd,long count,long relsize,bfd_vma got_addr,struct elf_x86_plt plts[],asymbol ** dynsyms,asymbol ** ret)3195 _bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
3196 				   long count,
3197 				   long relsize,
3198 				   bfd_vma got_addr,
3199 				   struct elf_x86_plt plts[],
3200 				   asymbol **dynsyms,
3201 				   asymbol **ret)
3202 {
3203   long size, i, n, len;
3204   int j;
3205   unsigned int plt_got_offset, plt_entry_size;
3206   asymbol *s;
3207   bfd_byte *plt_contents;
3208   long dynrelcount;
3209   arelent **dynrelbuf, *p;
3210   char *names;
3211   const struct elf_backend_data *bed;
3212   bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
3213 			      bfd_vma);
3214   bool (*valid_plt_reloc_p) (unsigned int);
3215 
3216   dynrelbuf = NULL;
3217   if (count == 0)
3218     goto bad_return;
3219 
3220   dynrelbuf = (arelent **) bfd_malloc (relsize);
3221   if (dynrelbuf == NULL)
3222     goto bad_return;
3223 
3224   dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
3225 						dynsyms);
3226   if (dynrelcount <= 0)
3227     goto bad_return;
3228 
3229   /* Sort the relocs by address.  */
3230   qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
3231 	 _bfd_x86_elf_compare_relocs);
3232 
3233   size = count * sizeof (asymbol);
3234 
3235   /* Allocate space for @plt suffixes.  */
3236   n = 0;
3237   for (i = 0; i < dynrelcount; i++)
3238     {
3239       p = dynrelbuf[i];
3240       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3241       if (p->addend != 0)
3242 	size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
3243     }
3244 
3245   s = *ret = (asymbol *) bfd_zmalloc (size);
3246   if (s == NULL)
3247     goto bad_return;
3248 
3249   bed = get_elf_backend_data (abfd);
3250 
3251   if (bed->target_id == X86_64_ELF_DATA)
3252     {
3253       get_plt_got_vma = elf_x86_64_get_plt_got_vma;
3254       valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
3255     }
3256   else
3257     {
3258       get_plt_got_vma = elf_i386_get_plt_got_vma;
3259       valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
3260       if (got_addr)
3261 	{
3262 	  /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
3263 	     address.  */
3264 	  asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
3265 	  if (sec != NULL)
3266 	    got_addr = sec->vma;
3267 	  else
3268 	    {
3269 	      sec = bfd_get_section_by_name (abfd, ".got");
3270 	      if (sec != NULL)
3271 		got_addr = sec->vma;
3272 	    }
3273 
3274 	  if (got_addr == (bfd_vma) -1)
3275 	    goto bad_return;
3276 	}
3277     }
3278 
3279   /* Check for each PLT section.  */
3280   names = (char *) (s + count);
3281   size = 0;
3282   n = 0;
3283   for (j = 0; plts[j].name != NULL; j++)
3284     if ((plt_contents = plts[j].contents) != NULL)
3285       {
3286 	long k;
3287 	bfd_vma offset;
3288 	asection *plt;
3289 	struct elf_x86_plt *plt_p = &plts[j];
3290 
3291 	plt_got_offset = plt_p->plt_got_offset;
3292 	plt_entry_size = plt_p->plt_entry_size;
3293 
3294 	plt = plt_p->sec;
3295 
3296 	if ((plt_p->type & plt_lazy))
3297 	  {
3298 	    /* Skip PLT0 in lazy PLT.  */
3299 	    k = 1;
3300 	    offset = plt_entry_size;
3301 	  }
3302 	else
3303 	  {
3304 	    k = 0;
3305 	    offset = 0;
3306 	  }
3307 
3308 	/* Check each PLT entry against dynamic relocations.  */
3309 	for (; k < plt_p->count; k++)
3310 	  {
3311 	    int off;
3312 	    bfd_vma got_vma;
3313 	    long min, max, mid;
3314 
3315 	    /* Get the GOT offset for i386 or the PC-relative offset
3316 	       for x86-64, a signed 32-bit integer.  */
3317 	    off = H_GET_32 (abfd, (plt_contents + offset
3318 				   + plt_got_offset));
3319 	    got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
3320 
3321 	    /* Binary search.  */
3322 	    p = dynrelbuf[0];
3323 	    min = 0;
3324 	    max = dynrelcount;
3325 	    while ((min + 1) < max)
3326 	      {
3327 		arelent *r;
3328 
3329 		mid = (min + max) / 2;
3330 		r = dynrelbuf[mid];
3331 		if (got_vma > r->address)
3332 		  min = mid;
3333 		else if (got_vma < r->address)
3334 		  max = mid;
3335 		else
3336 		  {
3337 		    p = r;
3338 		    break;
3339 		  }
3340 	      }
3341 
3342 	    /* Skip unknown relocation.  PR 17512: file: bc9d6cf5.  */
3343 	    if (got_vma == p->address
3344 		&& p->howto != NULL
3345 		&& valid_plt_reloc_p (p->howto->type))
3346 	      {
3347 		*s = **p->sym_ptr_ptr;
3348 		/* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
3349 		   set.  Since we are defining a symbol, ensure one
3350 		   of them is set.  */
3351 		if ((s->flags & BSF_LOCAL) == 0)
3352 		  s->flags |= BSF_GLOBAL;
3353 		s->flags |= BSF_SYNTHETIC;
3354 		/* This is no longer a section symbol.  */
3355 		s->flags &= ~BSF_SECTION_SYM;
3356 		s->section = plt;
3357 		s->the_bfd = plt->owner;
3358 		s->value = offset;
3359 		s->udata.p = NULL;
3360 		s->name = names;
3361 		len = strlen ((*p->sym_ptr_ptr)->name);
3362 		memcpy (names, (*p->sym_ptr_ptr)->name, len);
3363 		names += len;
3364 		if (p->addend != 0)
3365 		  {
3366 		    char buf[30], *a;
3367 
3368 		    memcpy (names, "+0x", sizeof ("+0x") - 1);
3369 		    names += sizeof ("+0x") - 1;
3370 		    bfd_sprintf_vma (abfd, buf, p->addend);
3371 		    for (a = buf; *a == '0'; ++a)
3372 		      ;
3373 		    size = strlen (a);
3374 		    memcpy (names, a, size);
3375 		    names += size;
3376 		  }
3377 		memcpy (names, "@plt", sizeof ("@plt"));
3378 		names += sizeof ("@plt");
3379 		n++;
3380 		s++;
3381 		/* There should be only one entry in PLT for a given
3382 		   symbol.  Set howto to NULL after processing a PLT
3383 		   entry to guard against corrupted PLT.  */
3384 		p->howto = NULL;
3385 	      }
3386 	    offset += plt_entry_size;
3387 	  }
3388       }
3389 
3390   /* PLT entries with R_386_TLS_DESC relocations are skipped.  */
3391   if (n == 0)
3392     {
3393     bad_return:
3394       count = -1;
3395     }
3396   else
3397     count = n;
3398 
3399   for (j = 0; plts[j].name != NULL; j++)
3400     free (plts[j].contents);
3401 
3402   free (dynrelbuf);
3403 
3404   return count;
3405 }
3406 
3407 /* Parse x86 GNU properties.  */
3408 
3409 enum elf_property_kind
_bfd_x86_elf_parse_gnu_properties(bfd * abfd,unsigned int type,bfd_byte * ptr,unsigned int datasz)3410 _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
3411 				   bfd_byte *ptr, unsigned int datasz)
3412 {
3413   elf_property *prop;
3414 
3415   if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3416       || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3417       || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
3418 	  && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3419       || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
3420 	  && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
3421       || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3422 	  && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3423     {
3424       if (datasz != 4)
3425 	{
3426 	  _bfd_error_handler
3427 	    (_("error: %pB: <corrupt x86 property (0x%x) size: 0x%x>"),
3428 	     abfd, type, datasz);
3429 	  return property_corrupt;
3430 	}
3431       prop = _bfd_elf_get_property (abfd, type, datasz);
3432       prop->u.number |= bfd_h_get_32 (abfd, ptr);
3433       prop->pr_kind = property_number;
3434       return property_number;
3435     }
3436 
3437   return property_ignored;
3438 }
3439 
3440 /* Merge x86 GNU property BPROP with APROP.  If APROP isn't NULL,
3441    return TRUE if APROP is updated.  Otherwise, return TRUE if BPROP
3442    should be merged with ABFD.  */
3443 
3444 bool
_bfd_x86_elf_merge_gnu_properties(struct bfd_link_info * info,bfd * abfd ATTRIBUTE_UNUSED,bfd * bbfd ATTRIBUTE_UNUSED,elf_property * aprop,elf_property * bprop)3445 _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
3446 				   bfd *abfd ATTRIBUTE_UNUSED,
3447 				   bfd *bbfd ATTRIBUTE_UNUSED,
3448 				   elf_property *aprop,
3449 				   elf_property *bprop)
3450 {
3451   unsigned int number, features;
3452   bool updated = false;
3453   const struct elf_backend_data *bed;
3454   struct elf_x86_link_hash_table *htab;
3455   unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
3456 
3457   if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
3458       || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
3459 	  && pr_type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
3460     {
3461       if (aprop == NULL || bprop == NULL)
3462 	{
3463 	  /* Only one of APROP and BPROP can be NULL.  */
3464 	  if (aprop != NULL)
3465 	    {
3466 	      /* Remove this property since the other input file doesn't
3467 		 have it.  */
3468 	      aprop->pr_kind = property_remove;
3469 	      updated = true;
3470 	    }
3471 	}
3472       else
3473 	{
3474 	  number = aprop->u.number;
3475 	  aprop->u.number = number | bprop->u.number;
3476 	  updated = number != (unsigned int) aprop->u.number;
3477 	}
3478       return updated;
3479     }
3480   else if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
3481 	   || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_LO
3482 	       && pr_type <= GNU_PROPERTY_X86_UINT32_OR_HI))
3483     {
3484       features = 0;
3485       if (pr_type == GNU_PROPERTY_X86_ISA_1_NEEDED)
3486 	{
3487 	  bed = get_elf_backend_data (info->output_bfd);
3488 	  htab = elf_x86_hash_table (info, bed->target_id);
3489 	  switch (htab->params->isa_level)
3490 	    {
3491 	    case 0:
3492 	      break;
3493 	    case 2:
3494 	      features = GNU_PROPERTY_X86_ISA_1_V2;
3495 	      break;
3496 	    case 3:
3497 	      features = GNU_PROPERTY_X86_ISA_1_V3;
3498 	      break;
3499 	    case 4:
3500 	      features = GNU_PROPERTY_X86_ISA_1_V4;
3501 	      break;
3502 	    default:
3503 	      abort ();
3504 	    }
3505 	}
3506       if (aprop != NULL && bprop != NULL)
3507 	{
3508 	  number = aprop->u.number;
3509 	  aprop->u.number = number | bprop->u.number | features;
3510 	  /* Remove the property if all bits are empty.  */
3511 	  if (aprop->u.number == 0)
3512 	    {
3513 	      aprop->pr_kind = property_remove;
3514 	      updated = true;
3515 	    }
3516 	  else
3517 	    updated = number != (unsigned int) aprop->u.number;
3518 	}
3519       else
3520 	{
3521 	  /* Only one of APROP and BPROP can be NULL.  */
3522 	  if (aprop != NULL)
3523 	    {
3524 	      aprop->u.number |= features;
3525 	      if (aprop->u.number == 0)
3526 		{
3527 		  /* Remove APROP if all bits are empty.  */
3528 		  aprop->pr_kind = property_remove;
3529 		  updated = true;
3530 		}
3531 	    }
3532 	  else
3533 	    {
3534 	      /* Return TRUE if APROP is NULL and all bits of BPROP
3535 		 aren't empty to indicate that BPROP should be added
3536 		 to ABFD.  */
3537 	      bprop->u.number |= features;
3538 	      updated = bprop->u.number != 0;
3539 	    }
3540 	}
3541       return updated;
3542     }
3543   else if (pr_type >= GNU_PROPERTY_X86_UINT32_AND_LO
3544 	   && pr_type <= GNU_PROPERTY_X86_UINT32_AND_HI)
3545     {
3546       /* Only one of APROP and BPROP can be NULL:
3547 	 1. APROP & BPROP when both APROP and BPROP aren't NULL.
3548 	 2. If APROP is NULL, remove x86 feature.
3549 	 3. Otherwise, do nothing.
3550        */
3551       bed = get_elf_backend_data (info->output_bfd);
3552       htab = elf_x86_hash_table (info, bed->target_id);
3553       if (!htab)
3554 	abort ();
3555       if (aprop != NULL && bprop != NULL)
3556 	{
3557 	  number = aprop->u.number;
3558 	  aprop->u.number = number & bprop->u.number;
3559 	  if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
3560 	    {
3561 	      features = 0;
3562 	      if (htab->params->ibt)
3563 		features = GNU_PROPERTY_X86_FEATURE_1_IBT;
3564 	      if (htab->params->shstk)
3565 		features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
3566 	      if (htab->params->lam_u48)
3567 		features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
3568 			     | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
3569 	      else if (htab->params->lam_u57)
3570 		features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
3571 	      /* Add GNU_PROPERTY_X86_FEATURE_1_IBT,
3572 		 GNU_PROPERTY_X86_FEATURE_1_SHSTK,
3573 		 GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
3574 		 GNU_PROPERTY_X86_FEATURE_1_LAM_U57.  */
3575 	      aprop->u.number |= features;
3576 	    }
3577 	  updated = number != (unsigned int) aprop->u.number;
3578 	  /* Remove the property if all feature bits are cleared.  */
3579 	  if (aprop->u.number == 0)
3580 	    aprop->pr_kind = property_remove;
3581 	}
3582       else
3583 	{
3584 	  /* There should be no AND properties since some input doesn't
3585 	     have them.  Set IBT and SHSTK properties for -z ibt and -z
3586 	     shstk if needed.  */
3587 	  features = 0;
3588 	  if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
3589 	    {
3590 	      if (htab->params->ibt)
3591 		features = GNU_PROPERTY_X86_FEATURE_1_IBT;
3592 	      if (htab->params->shstk)
3593 		features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
3594 	      if (htab->params->lam_u48)
3595 		features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
3596 			     | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
3597 	      else if (htab->params->lam_u57)
3598 		features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
3599 	    }
3600 	  if (features)
3601 	    {
3602 	      if (aprop != NULL)
3603 		{
3604 		  updated = features != (unsigned int) aprop->u.number;
3605 		  aprop->u.number = features;
3606 		}
3607 	      else
3608 		{
3609 		  updated = true;
3610 		  bprop->u.number = features;
3611 		}
3612 	    }
3613 	  else if (aprop != NULL)
3614 	    {
3615 	      aprop->pr_kind = property_remove;
3616 	      updated = true;
3617 	    }
3618 	}
3619       return updated;
3620     }
3621   else
3622     {
3623       /* Never should happen.  */
3624       abort ();
3625     }
3626 
3627   return updated;
3628 }
3629 
3630 /* Set up x86 GNU properties.  Return the first relocatable ELF input
3631    with GNU properties if found.  Otherwise, return NULL.  */
3632 
3633 bfd *
_bfd_x86_elf_link_setup_gnu_properties(struct bfd_link_info * info,struct elf_x86_init_table * init_table)3634 _bfd_x86_elf_link_setup_gnu_properties
3635   (struct bfd_link_info *info, struct elf_x86_init_table *init_table)
3636 {
3637   bool normal_target;
3638   bool lazy_plt;
3639   asection *sec, *pltsec;
3640   bfd *dynobj;
3641   bool use_ibt_plt;
3642   unsigned int plt_alignment, features, isa_level;
3643   struct elf_x86_link_hash_table *htab;
3644   bfd *pbfd;
3645   bfd *ebfd = NULL;
3646   elf_property *prop;
3647   const struct elf_backend_data *bed;
3648   unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
3649   unsigned int got_align;
3650 
3651   /* Find a normal input file with GNU property note.  */
3652   for (pbfd = info->input_bfds;
3653        pbfd != NULL;
3654        pbfd = pbfd->link.next)
3655     if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
3656 	&& bfd_count_sections (pbfd) != 0)
3657       {
3658 	ebfd = pbfd;
3659 
3660 	if (elf_properties (pbfd) != NULL)
3661 	  break;
3662       }
3663 
3664   bed = get_elf_backend_data (info->output_bfd);
3665 
3666   htab = elf_x86_hash_table (info, bed->target_id);
3667   if (htab == NULL)
3668     return pbfd;
3669 
3670   features = 0;
3671   if (htab->params->ibt)
3672     {
3673       features = GNU_PROPERTY_X86_FEATURE_1_IBT;
3674       htab->params->cet_report &= ~prop_report_ibt;
3675     }
3676   if (htab->params->shstk)
3677     {
3678       features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
3679       htab->params->cet_report &= ~prop_report_shstk;
3680     }
3681   if (!(htab->params->cet_report & (prop_report_ibt | prop_report_shstk)))
3682     htab->params->cet_report = prop_report_none;
3683   if (htab->params->lam_u48)
3684     {
3685       features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
3686 		   | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
3687       htab->params->lam_u48_report = prop_report_none;
3688       htab->params->lam_u57_report = prop_report_none;
3689     }
3690   else if (htab->params->lam_u57)
3691     {
3692       features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
3693       htab->params->lam_u57_report = prop_report_none;
3694     }
3695 
3696   switch (htab->params->isa_level)
3697     {
3698     case 0:
3699       isa_level = 0;
3700       break;
3701     case 1:
3702       isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE;
3703       break;
3704     case 2:
3705       isa_level = GNU_PROPERTY_X86_ISA_1_V2;
3706       break;
3707     case 3:
3708       isa_level = GNU_PROPERTY_X86_ISA_1_V3;
3709       break;
3710     case 4:
3711       isa_level = GNU_PROPERTY_X86_ISA_1_V4;
3712       break;
3713     default:
3714       abort ();
3715     }
3716 
3717   if (ebfd != NULL)
3718     {
3719       prop = NULL;
3720       if (features)
3721 	{
3722 	  /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT,
3723 	     GNU_PROPERTY_X86_FEATURE_1_SHSTK,
3724 	     GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
3725 	     GNU_PROPERTY_X86_FEATURE_1_LAM_U57.  */
3726 	  prop = _bfd_elf_get_property (ebfd,
3727 					GNU_PROPERTY_X86_FEATURE_1_AND,
3728 					4);
3729 	  prop->u.number |= features;
3730 	  prop->pr_kind = property_number;
3731 	}
3732 
3733       if (isa_level)
3734 	{
3735 	  /* If ISA level is set, add GNU_PROPERTY_X86_ISA_1_NEEDED.  */
3736 	  prop = _bfd_elf_get_property (ebfd,
3737 					GNU_PROPERTY_X86_ISA_1_NEEDED,
3738 					4);
3739 	  prop->u.number |= isa_level;
3740 	  prop->pr_kind = property_number;
3741 	}
3742 
3743       /* Create the GNU property note section if needed.  */
3744       if (prop != NULL && pbfd == NULL)
3745 	{
3746 	  sec = bfd_make_section_with_flags (ebfd,
3747 					     NOTE_GNU_PROPERTY_SECTION_NAME,
3748 					     (SEC_ALLOC
3749 					      | SEC_LOAD
3750 					      | SEC_IN_MEMORY
3751 					      | SEC_READONLY
3752 					      | SEC_HAS_CONTENTS
3753 					      | SEC_DATA));
3754 	  if (sec == NULL)
3755 	    info->callbacks->einfo (_("%F%P: failed to create GNU property section\n"));
3756 
3757 	  if (!bfd_set_section_alignment (sec, class_align))
3758 	    {
3759 	    error_alignment:
3760 	      info->callbacks->einfo (_("%F%pA: failed to align section\n"),
3761 				      sec);
3762 	    }
3763 
3764 	  elf_section_type (sec) = SHT_NOTE;
3765 	}
3766     }
3767 
3768   if (htab->params->cet_report
3769       || htab->params->lam_u48_report
3770       || htab->params->lam_u57_report)
3771     {
3772       /* Report missing IBT, SHSTK and LAM properties.  */
3773       bfd *abfd;
3774       const char *warning_msg = _("%P: %pB: warning: missing %s\n");
3775       const char *error_msg = _("%X%P: %pB: error: missing %s\n");
3776       const char *cet_msg = NULL;
3777       const char *lam_u48_msg = NULL;
3778       const char *lam_u57_msg = NULL;
3779       const char *missing;
3780       elf_property_list *p;
3781       bool missing_ibt, missing_shstk;
3782       bool missing_lam_u48, missing_lam_u57;
3783       bool check_ibt
3784 	= (htab->params->cet_report
3785 	   && (htab->params->cet_report & prop_report_ibt));
3786       bool check_shstk
3787 	= (htab->params->cet_report
3788 	   && (htab->params->cet_report & prop_report_shstk));
3789 
3790       if (htab->params->cet_report)
3791 	{
3792 	  if ((htab->params->cet_report & prop_report_warning))
3793 	    cet_msg = warning_msg;
3794 	  else
3795 	    cet_msg = error_msg;
3796 	}
3797       if (htab->params->lam_u48_report)
3798 	{
3799 	  if ((htab->params->lam_u48_report & prop_report_warning))
3800 	    lam_u48_msg = warning_msg;
3801 	  else
3802 	    lam_u48_msg = error_msg;
3803 	}
3804       if (htab->params->lam_u57_report)
3805 	{
3806 	  if ((htab->params->lam_u57_report & prop_report_warning))
3807 	    lam_u57_msg = warning_msg;
3808 	  else
3809 	    lam_u57_msg = error_msg;
3810 	}
3811 
3812       for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
3813 	if (!(abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED))
3814 	    && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
3815 	  {
3816 	    for (p = elf_properties (abfd); p; p = p->next)
3817 	      if (p->property.pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
3818 		break;
3819 
3820 	    missing_ibt = check_ibt;
3821 	    missing_shstk = check_shstk;
3822 	    missing_lam_u48 = !!lam_u48_msg;
3823 	    missing_lam_u57 = !!lam_u57_msg;
3824 	    if (p)
3825 	      {
3826 		missing_ibt &= !(p->property.u.number
3827 				 & GNU_PROPERTY_X86_FEATURE_1_IBT);
3828 		missing_shstk &= !(p->property.u.number
3829 				   & GNU_PROPERTY_X86_FEATURE_1_SHSTK);
3830 		missing_lam_u48 &= !(p->property.u.number
3831 				     & GNU_PROPERTY_X86_FEATURE_1_LAM_U48);
3832 		missing_lam_u57 &= !(p->property.u.number
3833 				     & GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
3834 	      }
3835 	    if (missing_ibt || missing_shstk)
3836 	      {
3837 		if (missing_ibt && missing_shstk)
3838 		  missing = _("IBT and SHSTK properties");
3839 		else if (missing_ibt)
3840 		  missing = _("IBT property");
3841 		else
3842 		  missing = _("SHSTK property");
3843 		info->callbacks->einfo (cet_msg, abfd, missing);
3844 	      }
3845 	    if (missing_lam_u48)
3846 	      {
3847 		missing = _("LAM_U48 property");
3848 		info->callbacks->einfo (lam_u48_msg, abfd, missing);
3849 	      }
3850 	    if (missing_lam_u57)
3851 	      {
3852 		missing = _("LAM_U57 property");
3853 		info->callbacks->einfo (lam_u57_msg, abfd, missing);
3854 	      }
3855 	  }
3856     }
3857 
3858   pbfd = _bfd_elf_link_setup_gnu_properties (info);
3859 
3860   htab->r_info = init_table->r_info;
3861   htab->r_sym = init_table->r_sym;
3862 
3863   if (bfd_link_relocatable (info))
3864     return pbfd;
3865 
3866   htab->plt0_pad_byte = init_table->plt0_pad_byte;
3867 
3868   use_ibt_plt = htab->params->ibtplt || htab->params->ibt;
3869   if (!use_ibt_plt && pbfd != NULL)
3870     {
3871       /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on.  */
3872       elf_property_list *p;
3873 
3874       /* The property list is sorted in order of type.  */
3875       for (p = elf_properties (pbfd); p; p = p->next)
3876 	{
3877 	  if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
3878 	    {
3879 	      use_ibt_plt = !!(p->property.u.number
3880 			       & GNU_PROPERTY_X86_FEATURE_1_IBT);
3881 	      break;
3882 	    }
3883 	  else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
3884 	    break;
3885 	}
3886     }
3887 
3888   dynobj = htab->elf.dynobj;
3889 
3890   /* Set htab->elf.dynobj here so that there is no need to check and
3891      set it in check_relocs.  */
3892   if (dynobj == NULL)
3893     {
3894       if (pbfd != NULL)
3895 	{
3896 	  htab->elf.dynobj = pbfd;
3897 	  dynobj = pbfd;
3898 	}
3899       else
3900 	{
3901 	  bfd *abfd;
3902 
3903 	  /* Find a normal input file to hold linker created
3904 	     sections.  */
3905 	  for (abfd = info->input_bfds;
3906 	       abfd != NULL;
3907 	       abfd = abfd->link.next)
3908 	    if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
3909 		&& (abfd->flags
3910 		    & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
3911 		&& bed->relocs_compatible (abfd->xvec,
3912 					   info->output_bfd->xvec))
3913 	      {
3914 		htab->elf.dynobj = abfd;
3915 		dynobj = abfd;
3916 		break;
3917 	      }
3918 	}
3919     }
3920 
3921   /* Return if there are no normal input files.  */
3922   if (dynobj == NULL)
3923     return pbfd;
3924 
3925   /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
3926      still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
3927      canonical function address.  */
3928   htab->plt.has_plt0 = 1;
3929   normal_target = htab->elf.target_os == is_normal;
3930 
3931   if (normal_target)
3932     {
3933       if (use_ibt_plt)
3934 	{
3935 	  htab->lazy_plt = init_table->lazy_ibt_plt;
3936 	  htab->non_lazy_plt = init_table->non_lazy_ibt_plt;
3937 	}
3938       else
3939 	{
3940 	  htab->lazy_plt = init_table->lazy_plt;
3941 	  htab->non_lazy_plt = init_table->non_lazy_plt;
3942 	}
3943     }
3944   else
3945     {
3946       htab->lazy_plt = init_table->lazy_plt;
3947       htab->non_lazy_plt = NULL;
3948     }
3949 
3950   pltsec = htab->elf.splt;
3951 
3952   /* If the non-lazy PLT is available, use it for all PLT entries if
3953      there are no PLT0 or no .plt section.  */
3954   if (htab->non_lazy_plt != NULL
3955       && (!htab->plt.has_plt0 || pltsec == NULL))
3956     {
3957       lazy_plt = false;
3958       if (bfd_link_pic (info))
3959 	htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
3960       else
3961 	htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
3962       htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
3963       htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
3964       htab->plt.plt_got_insn_size
3965 	= htab->non_lazy_plt->plt_got_insn_size;
3966       htab->plt.eh_frame_plt_size
3967 	= htab->non_lazy_plt->eh_frame_plt_size;
3968       htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
3969     }
3970   else
3971     {
3972       lazy_plt = true;
3973       if (bfd_link_pic (info))
3974 	{
3975 	  htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
3976 	  htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
3977 	}
3978       else
3979 	{
3980 	  htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
3981 	  htab->plt.plt_entry = htab->lazy_plt->plt_entry;
3982 	}
3983       htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
3984       htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
3985       htab->plt.plt_got_insn_size
3986 	= htab->lazy_plt->plt_got_insn_size;
3987       htab->plt.eh_frame_plt_size
3988 	= htab->lazy_plt->eh_frame_plt_size;
3989       htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
3990     }
3991 
3992   if (htab->elf.target_os == is_vxworks
3993       && !elf_vxworks_create_dynamic_sections (dynobj, info,
3994 					       &htab->srelplt2))
3995     {
3996       info->callbacks->einfo (_("%F%P: failed to create VxWorks dynamic sections\n"));
3997       return pbfd;
3998     }
3999 
4000   /* Since create_dynamic_sections isn't always called, but GOT
4001      relocations need GOT relocations, create them here so that we
4002      don't need to do it in check_relocs.  */
4003   if (htab->elf.sgot == NULL
4004       && !_bfd_elf_create_got_section (dynobj, info))
4005     info->callbacks->einfo (_("%F%P: failed to create GOT sections\n"));
4006 
4007   got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
4008 
4009   /* Align .got and .got.plt sections to their entry size.  Do it here
4010      instead of in create_dynamic_sections so that they are always
4011      properly aligned even if create_dynamic_sections isn't called.  */
4012   sec = htab->elf.sgot;
4013   if (!bfd_set_section_alignment (sec, got_align))
4014     goto error_alignment;
4015 
4016   sec = htab->elf.sgotplt;
4017   if (!bfd_set_section_alignment (sec, got_align))
4018     goto error_alignment;
4019 
4020   /* Create the ifunc sections here so that check_relocs can be
4021      simplified.  */
4022   if (!_bfd_elf_create_ifunc_sections (dynobj, info))
4023     info->callbacks->einfo (_("%F%P: failed to create ifunc sections\n"));
4024 
4025   plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
4026 
4027   if (pltsec != NULL)
4028     {
4029       /* Whe creating executable, set the contents of the .interp
4030 	 section to the interpreter.  */
4031       if (bfd_link_executable (info) && !info->nointerp)
4032 	{
4033 	  asection *s = bfd_get_linker_section (dynobj, ".interp");
4034 	  if (s == NULL)
4035 	    abort ();
4036 	  s->size = htab->dynamic_interpreter_size;
4037 	  s->contents = (unsigned char *) htab->dynamic_interpreter;
4038 	  htab->interp = s;
4039 	}
4040 
4041       if (normal_target)
4042 	{
4043 	  flagword pltflags = (bed->dynamic_sec_flags
4044 			       | SEC_ALLOC
4045 			       | SEC_CODE
4046 			       | SEC_LOAD
4047 			       | SEC_READONLY);
4048 	  unsigned int non_lazy_plt_alignment
4049 	    = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
4050 
4051 	  sec = pltsec;
4052 	  if (!bfd_set_section_alignment (sec, plt_alignment))
4053 	    goto error_alignment;
4054 
4055 	  /* Create the GOT procedure linkage table.  */
4056 	  sec = bfd_make_section_anyway_with_flags (dynobj,
4057 						    ".plt.got",
4058 						    pltflags);
4059 	  if (sec == NULL)
4060 	    info->callbacks->einfo (_("%F%P: failed to create GOT PLT section\n"));
4061 
4062 	  if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
4063 	    goto error_alignment;
4064 
4065 	  htab->plt_got = sec;
4066 
4067 	  if (lazy_plt)
4068 	    {
4069 	      sec = NULL;
4070 
4071 	      if (use_ibt_plt)
4072 		{
4073 		  /* Create the second PLT for Intel IBT support.  IBT
4074 		     PLT is needed only for lazy binding.  */
4075 		  sec = bfd_make_section_anyway_with_flags (dynobj,
4076 							    ".plt.sec",
4077 							    pltflags);
4078 		  if (sec == NULL)
4079 		    info->callbacks->einfo (_("%F%P: failed to create IBT-enabled PLT section\n"));
4080 
4081 		  if (!bfd_set_section_alignment (sec, plt_alignment))
4082 		    goto error_alignment;
4083 		}
4084 	      else if (htab->params->bndplt && ABI_64_P (dynobj))
4085 		{
4086 		  /* Create the second PLT for Intel MPX support.  MPX
4087 		     PLT is supported only in 64-bit mode and is needed
4088 		     only for lazy binding.  */
4089 		  sec = bfd_make_section_anyway_with_flags (dynobj,
4090 							    ".plt.sec",
4091 							    pltflags);
4092 		  if (sec == NULL)
4093 		    info->callbacks->einfo (_("%F%P: failed to create BND PLT section\n"));
4094 
4095 		  if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
4096 		    goto error_alignment;
4097 		}
4098 
4099 	      htab->plt_second = sec;
4100 	    }
4101 	}
4102 
4103       if (!info->no_ld_generated_unwind_info)
4104 	{
4105 	  flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4106 			    | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4107 			    | SEC_LINKER_CREATED);
4108 
4109 	  sec = bfd_make_section_anyway_with_flags (dynobj,
4110 						    ".eh_frame",
4111 						    flags);
4112 	  if (sec == NULL)
4113 	    info->callbacks->einfo (_("%F%P: failed to create PLT .eh_frame section\n"));
4114 
4115 	  if (!bfd_set_section_alignment (sec, class_align))
4116 	    goto error_alignment;
4117 
4118 	  htab->plt_eh_frame = sec;
4119 
4120 	  if (htab->plt_got != NULL)
4121 	    {
4122 	      sec = bfd_make_section_anyway_with_flags (dynobj,
4123 							".eh_frame",
4124 							flags);
4125 	      if (sec == NULL)
4126 		info->callbacks->einfo (_("%F%P: failed to create GOT PLT .eh_frame section\n"));
4127 
4128 	      if (!bfd_set_section_alignment (sec, class_align))
4129 		goto error_alignment;
4130 
4131 	      htab->plt_got_eh_frame = sec;
4132 	    }
4133 
4134 	  if (htab->plt_second != NULL)
4135 	    {
4136 	      sec = bfd_make_section_anyway_with_flags (dynobj,
4137 							".eh_frame",
4138 							flags);
4139 	      if (sec == NULL)
4140 		info->callbacks->einfo (_("%F%P: failed to create the second PLT .eh_frame section\n"));
4141 
4142 	      if (!bfd_set_section_alignment (sec, class_align))
4143 		goto error_alignment;
4144 
4145 	      htab->plt_second_eh_frame = sec;
4146 	    }
4147 	}
4148     }
4149 
4150   /* The .iplt section is used for IFUNC symbols in static
4151      executables.  */
4152   sec = htab->elf.iplt;
4153   if (sec != NULL)
4154     {
4155       /* NB: Delay setting its alignment until we know it is non-empty.
4156 	 Otherwise an empty iplt section may change vma and lma of the
4157 	 following sections, which triggers moving dot of the following
4158 	 section backwards, resulting in a warning and section lma not
4159 	 being set properly.  It later leads to a "File truncated"
4160 	 error.  */
4161       if (!bfd_set_section_alignment (sec, 0))
4162 	goto error_alignment;
4163 
4164       htab->plt.iplt_alignment = (normal_target
4165 				  ? plt_alignment
4166 				  : bed->plt_alignment);
4167     }
4168 
4169   if (bfd_link_executable (info)
4170       && !info->nointerp
4171       && !htab->params->has_dynamic_linker
4172       && htab->params->static_before_all_inputs)
4173     {
4174       /* Report error for dynamic input objects if -static is passed at
4175 	 command-line before all input files without --dynamic-linker
4176 	 unless --no-dynamic-linker is used.  */
4177       bfd *abfd;
4178 
4179       for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
4180 	if ((abfd->flags & DYNAMIC))
4181 	  info->callbacks->einfo
4182 	    (_("%X%P: attempted static link of dynamic object `%pB'\n"),
4183 	     abfd);
4184     }
4185 
4186   return pbfd;
4187 }
4188 
4189 /* Fix up x86 GNU properties.  */
4190 
4191 void
_bfd_x86_elf_link_fixup_gnu_properties(struct bfd_link_info * info,elf_property_list ** listp)4192 _bfd_x86_elf_link_fixup_gnu_properties
4193   (struct bfd_link_info *info, elf_property_list **listp)
4194 {
4195   elf_property_list *p;
4196 
4197   for (p = *listp; p; p = p->next)
4198     {
4199       unsigned int type = p->property.pr_type;
4200       if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
4201 	  || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4202 	  || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
4203 	      && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4204 	  || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
4205 	      && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
4206 	  || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
4207 	      && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
4208 	{
4209 	  if (p->property.u.number == 0
4210 	      && (type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
4211 		  || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
4212 		      && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
4213 		  || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
4214 		      && type <= GNU_PROPERTY_X86_UINT32_OR_HI)))
4215 	    {
4216 	      /* Remove empty property.  */
4217 	      *listp = p->next;
4218 	      continue;
4219 	    }
4220 
4221 	  /* Keep LAM features only for 64-bit output.  */
4222 	  if (type == GNU_PROPERTY_X86_FEATURE_1_AND
4223 	      && !ABI_64_P (info->output_bfd))
4224 	    p->property.u.number &= ~(GNU_PROPERTY_X86_FEATURE_1_LAM_U48
4225 				      | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
4226 
4227 	  listp = &p->next;
4228 	}
4229       else if (type > GNU_PROPERTY_HIPROC)
4230 	{
4231 	  /* The property list is sorted in order of type.  */
4232 	  break;
4233 	}
4234     }
4235 }
4236 
4237 void
_bfd_elf_linker_x86_set_options(struct bfd_link_info * info,struct elf_linker_x86_params * params)4238 _bfd_elf_linker_x86_set_options (struct bfd_link_info * info,
4239 				 struct elf_linker_x86_params *params)
4240 {
4241   const struct elf_backend_data *bed
4242     = get_elf_backend_data (info->output_bfd);
4243   struct elf_x86_link_hash_table *htab
4244     = elf_x86_hash_table (info, bed->target_id);
4245   if (htab != NULL)
4246     htab->params = params;
4247 }
4248