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