1 /* ELF linking support for BFD.
2    Copyright (C) 1995-2016 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 "sysdep.h"
22 #include "bfd.h"
23 #include "bfd_stdint.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin.h"
33 #endif
34 
35 /* This struct is used to pass information to routines called via
36    elf_link_hash_traverse which must return failure.  */
37 
38 struct elf_info_failed
39 {
40   struct bfd_link_info *info;
41   bfd_boolean failed;
42 };
43 
44 /* This structure is used to pass information to
45    _bfd_elf_link_find_version_dependencies.  */
46 
47 struct elf_find_verdep_info
48 {
49   /* General link information.  */
50   struct bfd_link_info *info;
51   /* The number of dependencies.  */
52   unsigned int vers;
53   /* Whether we had a failure.  */
54   bfd_boolean failed;
55 };
56 
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58   (struct elf_link_hash_entry *, struct elf_info_failed *);
59 
60 asection *
_bfd_elf_section_for_symbol(struct elf_reloc_cookie * cookie,unsigned long r_symndx,bfd_boolean discard)61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 			     unsigned long r_symndx,
63 			     bfd_boolean discard)
64 {
65   if (r_symndx >= cookie->locsymcount
66       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67     {
68       struct elf_link_hash_entry *h;
69 
70       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71 
72       while (h->root.type == bfd_link_hash_indirect
73 	     || h->root.type == bfd_link_hash_warning)
74 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
75 
76       if ((h->root.type == bfd_link_hash_defined
77 	   || h->root.type == bfd_link_hash_defweak)
78 	   && discarded_section (h->root.u.def.section))
79         return h->root.u.def.section;
80       else
81 	return NULL;
82     }
83   else
84     {
85       /* It's not a relocation against a global symbol,
86 	 but it could be a relocation against a local
87 	 symbol for a discarded section.  */
88       asection *isec;
89       Elf_Internal_Sym *isym;
90 
91       /* Need to: get the symbol; get the section.  */
92       isym = &cookie->locsyms[r_symndx];
93       isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94       if (isec != NULL
95 	  && discard ? discarded_section (isec) : 1)
96 	return isec;
97      }
98   return NULL;
99 }
100 
101 /* Define a symbol in a dynamic linkage section.  */
102 
103 struct elf_link_hash_entry *
_bfd_elf_define_linkage_sym(bfd * abfd,struct bfd_link_info * info,asection * sec,const char * name)104 _bfd_elf_define_linkage_sym (bfd *abfd,
105 			     struct bfd_link_info *info,
106 			     asection *sec,
107 			     const char *name)
108 {
109   struct elf_link_hash_entry *h;
110   struct bfd_link_hash_entry *bh;
111   const struct elf_backend_data *bed;
112 
113   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114   if (h != NULL)
115     {
116       /* Zap symbol defined in an as-needed lib that wasn't linked.
117 	 This is a symptom of a larger problem:  Absolute symbols
118 	 defined in shared libraries can't be overridden, because we
119 	 lose the link to the bfd which is via the symbol section.  */
120       h->root.type = bfd_link_hash_new;
121     }
122 
123   bh = &h->root;
124   bed = get_elf_backend_data (abfd);
125   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
126 					 sec, 0, NULL, FALSE, bed->collect,
127 					 &bh))
128     return NULL;
129   h = (struct elf_link_hash_entry *) bh;
130   h->def_regular = 1;
131   h->non_elf = 0;
132   h->root.linker_def = 1;
133   h->type = STT_OBJECT;
134   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
135     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
136 
137   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
138   return h;
139 }
140 
141 bfd_boolean
_bfd_elf_create_got_section(bfd * abfd,struct bfd_link_info * info)142 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
143 {
144   flagword flags;
145   asection *s;
146   struct elf_link_hash_entry *h;
147   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
148   struct elf_link_hash_table *htab = elf_hash_table (info);
149 
150   /* This function may be called more than once.  */
151   s = bfd_get_linker_section (abfd, ".got");
152   if (s != NULL)
153     return TRUE;
154 
155   flags = bed->dynamic_sec_flags;
156 
157   s = bfd_make_section_anyway_with_flags (abfd,
158 					  (bed->rela_plts_and_copies_p
159 					   ? ".rela.got" : ".rel.got"),
160 					  (bed->dynamic_sec_flags
161 					   | SEC_READONLY));
162   if (s == NULL
163       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164     return FALSE;
165   htab->srelgot = s;
166 
167   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
168   if (s == NULL
169       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170     return FALSE;
171   htab->sgot = s;
172 
173   if (bed->want_got_plt)
174     {
175       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
176       if (s == NULL
177 	  || !bfd_set_section_alignment (abfd, s,
178 					 bed->s->log_file_align))
179 	return FALSE;
180       htab->sgotplt = s;
181     }
182 
183   /* The first bit of the global offset table is the header.  */
184   s->size += bed->got_header_size;
185 
186   if (bed->want_got_sym)
187     {
188       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 	 (or .got.plt) section.  We don't do this in the linker script
190 	 because we don't want to define the symbol if we are not creating
191 	 a global offset table.  */
192       h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 				       "_GLOBAL_OFFSET_TABLE_");
194       elf_hash_table (info)->hgot = h;
195       if (h == NULL)
196 	return FALSE;
197     }
198 
199   return TRUE;
200 }
201 
202 /* Create a strtab to hold the dynamic symbol names.  */
203 static bfd_boolean
_bfd_elf_link_create_dynstrtab(bfd * abfd,struct bfd_link_info * info)204 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
205 {
206   struct elf_link_hash_table *hash_table;
207 
208   hash_table = elf_hash_table (info);
209   if (hash_table->dynobj == NULL)
210     {
211       /* We may not set dynobj, an input file holding linker created
212 	 dynamic sections to abfd, which may be a dynamic object with
213 	 its own dynamic sections.  We need to find a normal input file
214 	 to hold linker created sections if possible.  */
215       if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
216 	{
217 	  bfd *ibfd;
218 	  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
219 	    if ((ibfd->flags
220 		 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
221 	      {
222 		abfd = ibfd;
223 		break;
224 	      }
225 	}
226       hash_table->dynobj = abfd;
227     }
228 
229   if (hash_table->dynstr == NULL)
230     {
231       hash_table->dynstr = _bfd_elf_strtab_init ();
232       if (hash_table->dynstr == NULL)
233 	return FALSE;
234     }
235   return TRUE;
236 }
237 
238 /* Create some sections which will be filled in with dynamic linking
239    information.  ABFD is an input file which requires dynamic sections
240    to be created.  The dynamic sections take up virtual memory space
241    when the final executable is run, so we need to create them before
242    addresses are assigned to the output sections.  We work out the
243    actual contents and size of these sections later.  */
244 
245 bfd_boolean
_bfd_elf_link_create_dynamic_sections(bfd * abfd,struct bfd_link_info * info)246 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
247 {
248   flagword flags;
249   asection *s;
250   const struct elf_backend_data *bed;
251   struct elf_link_hash_entry *h;
252 
253   if (! is_elf_hash_table (info->hash))
254     return FALSE;
255 
256   if (elf_hash_table (info)->dynamic_sections_created)
257     return TRUE;
258 
259   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
260     return FALSE;
261 
262   abfd = elf_hash_table (info)->dynobj;
263   bed = get_elf_backend_data (abfd);
264 
265   flags = bed->dynamic_sec_flags;
266 
267   /* A dynamically linked executable has a .interp section, but a
268      shared library does not.  */
269   if (bfd_link_executable (info) && !info->nointerp)
270     {
271       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 					      flags | SEC_READONLY);
273       if (s == NULL)
274 	return FALSE;
275     }
276 
277   /* Create sections to hold version informations.  These are removed
278      if they are not needed.  */
279   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 					  flags | SEC_READONLY);
281   if (s == NULL
282       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
283     return FALSE;
284 
285   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 					  flags | SEC_READONLY);
287   if (s == NULL
288       || ! bfd_set_section_alignment (abfd, s, 1))
289     return FALSE;
290 
291   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 					  flags | SEC_READONLY);
293   if (s == NULL
294       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
295     return FALSE;
296 
297   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 					  flags | SEC_READONLY);
299   if (s == NULL
300       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
301     return FALSE;
302   elf_hash_table (info)->dynsym = s;
303 
304   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 					  flags | SEC_READONLY);
306   if (s == NULL)
307     return FALSE;
308 
309   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
310   if (s == NULL
311       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312     return FALSE;
313 
314   /* The special symbol _DYNAMIC is always set to the start of the
315      .dynamic section.  We could set _DYNAMIC in a linker script, but we
316      only want to define it if we are, in fact, creating a .dynamic
317      section.  We don't want to define it if there is no .dynamic
318      section, since on some ELF platforms the start up code examines it
319      to decide how to initialize the process.  */
320   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321   elf_hash_table (info)->hdynamic = h;
322   if (h == NULL)
323     return FALSE;
324 
325   if (info->emit_hash)
326     {
327       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 					      flags | SEC_READONLY);
329       if (s == NULL
330 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
331 	return FALSE;
332       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
333     }
334 
335   if (info->emit_gnu_hash)
336     {
337       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 					      flags | SEC_READONLY);
339       if (s == NULL
340 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
341 	return FALSE;
342       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 	 4 32-bit words followed by variable count of 64-bit words, then
344 	 variable count of 32-bit words.  */
345       if (bed->s->arch_size == 64)
346 	elf_section_data (s)->this_hdr.sh_entsize = 0;
347       else
348 	elf_section_data (s)->this_hdr.sh_entsize = 4;
349     }
350 
351   /* Let the backend create the rest of the sections.  This lets the
352      backend set the right flags.  The backend will normally create
353      the .got and .plt sections.  */
354   if (bed->elf_backend_create_dynamic_sections == NULL
355       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
356     return FALSE;
357 
358   elf_hash_table (info)->dynamic_sections_created = TRUE;
359 
360   return TRUE;
361 }
362 
363 /* Create dynamic sections when linking against a dynamic object.  */
364 
365 bfd_boolean
_bfd_elf_create_dynamic_sections(bfd * abfd,struct bfd_link_info * info)366 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
367 {
368   flagword flags, pltflags;
369   struct elf_link_hash_entry *h;
370   asection *s;
371   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
372   struct elf_link_hash_table *htab = elf_hash_table (info);
373 
374   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375      .rel[a].bss sections.  */
376   flags = bed->dynamic_sec_flags;
377 
378   pltflags = flags;
379   if (bed->plt_not_loaded)
380     /* We do not clear SEC_ALLOC here because we still want the OS to
381        allocate space for the section; it's just that there's nothing
382        to read in from the object file.  */
383     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
384   else
385     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
386   if (bed->plt_readonly)
387     pltflags |= SEC_READONLY;
388 
389   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
390   if (s == NULL
391       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
392     return FALSE;
393   htab->splt = s;
394 
395   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
396      .plt section.  */
397   if (bed->want_plt_sym)
398     {
399       h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 				       "_PROCEDURE_LINKAGE_TABLE_");
401       elf_hash_table (info)->hplt = h;
402       if (h == NULL)
403 	return FALSE;
404     }
405 
406   s = bfd_make_section_anyway_with_flags (abfd,
407 					  (bed->rela_plts_and_copies_p
408 					   ? ".rela.plt" : ".rel.plt"),
409 					  flags | SEC_READONLY);
410   if (s == NULL
411       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
412     return FALSE;
413   htab->srelplt = s;
414 
415   if (! _bfd_elf_create_got_section (abfd, info))
416     return FALSE;
417 
418   if (bed->want_dynbss)
419     {
420       /* The .dynbss section is a place to put symbols which are defined
421 	 by dynamic objects, are referenced by regular objects, and are
422 	 not functions.  We must allocate space for them in the process
423 	 image and use a R_*_COPY reloc to tell the dynamic linker to
424 	 initialize them at run time.  The linker script puts the .dynbss
425 	 section into the .bss section of the final image.  */
426       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 					      (SEC_ALLOC | SEC_LINKER_CREATED));
428       if (s == NULL)
429 	return FALSE;
430 
431       /* The .rel[a].bss section holds copy relocs.  This section is not
432 	 normally needed.  We need to create it here, though, so that the
433 	 linker will map it to an output section.  We can't just create it
434 	 only if we need it, because we will not know whether we need it
435 	 until we have seen all the input files, and the first time the
436 	 main linker code calls BFD after examining all the input files
437 	 (size_dynamic_sections) the input sections have already been
438 	 mapped to the output sections.  If the section turns out not to
439 	 be needed, we can discard it later.  We will never need this
440 	 section when generating a shared object, since they do not use
441 	 copy relocs.  */
442       if (! bfd_link_pic (info))
443 	{
444 	  s = bfd_make_section_anyway_with_flags (abfd,
445 						  (bed->rela_plts_and_copies_p
446 						   ? ".rela.bss" : ".rel.bss"),
447 						  flags | SEC_READONLY);
448 	  if (s == NULL
449 	      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
450 	    return FALSE;
451 	}
452     }
453 
454   return TRUE;
455 }
456 
457 /* Record a new dynamic symbol.  We record the dynamic symbols as we
458    read the input files, since we need to have a list of all of them
459    before we can determine the final sizes of the output sections.
460    Note that we may actually call this function even though we are not
461    going to output any dynamic symbols; in some cases we know that a
462    symbol should be in the dynamic symbol table, but only if there is
463    one.  */
464 
465 bfd_boolean
bfd_elf_link_record_dynamic_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h)466 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
467 				    struct elf_link_hash_entry *h)
468 {
469   if (h->dynindx == -1)
470     {
471       struct elf_strtab_hash *dynstr;
472       char *p;
473       const char *name;
474       size_t indx;
475 
476       /* XXX: The ABI draft says the linker must turn hidden and
477 	 internal symbols into STB_LOCAL symbols when producing the
478 	 DSO. However, if ld.so honors st_other in the dynamic table,
479 	 this would not be necessary.  */
480       switch (ELF_ST_VISIBILITY (h->other))
481 	{
482 	case STV_INTERNAL:
483 	case STV_HIDDEN:
484 	  if (h->root.type != bfd_link_hash_undefined
485 	      && h->root.type != bfd_link_hash_undefweak)
486 	    {
487 	      h->forced_local = 1;
488 	      if (!elf_hash_table (info)->is_relocatable_executable)
489 		return TRUE;
490 	    }
491 
492 	default:
493 	  break;
494 	}
495 
496       h->dynindx = elf_hash_table (info)->dynsymcount;
497       ++elf_hash_table (info)->dynsymcount;
498 
499       dynstr = elf_hash_table (info)->dynstr;
500       if (dynstr == NULL)
501 	{
502 	  /* Create a strtab to hold the dynamic symbol names.  */
503 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
504 	  if (dynstr == NULL)
505 	    return FALSE;
506 	}
507 
508       /* We don't put any version information in the dynamic string
509 	 table.  */
510       name = h->root.root.string;
511       p = strchr (name, ELF_VER_CHR);
512       if (p != NULL)
513 	/* We know that the p points into writable memory.  In fact,
514 	   there are only a few symbols that have read-only names, being
515 	   those like _GLOBAL_OFFSET_TABLE_ that are created specially
516 	   by the backends.  Most symbols will have names pointing into
517 	   an ELF string table read from a file, or to objalloc memory.  */
518 	*p = 0;
519 
520       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
521 
522       if (p != NULL)
523 	*p = ELF_VER_CHR;
524 
525       if (indx == (size_t) -1)
526 	return FALSE;
527       h->dynstr_index = indx;
528     }
529 
530   return TRUE;
531 }
532 
533 /* Mark a symbol dynamic.  */
534 
535 static void
bfd_elf_link_mark_dynamic_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)536 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
537 				  struct elf_link_hash_entry *h,
538 				  Elf_Internal_Sym *sym)
539 {
540   struct bfd_elf_dynamic_list *d = info->dynamic_list;
541 
542   /* It may be called more than once on the same H.  */
543   if(h->dynamic || bfd_link_relocatable (info))
544     return;
545 
546   if ((info->dynamic_data
547        && (h->type == STT_OBJECT
548 	   || h->type == STT_COMMON
549 	   || (sym != NULL
550 	       && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
551 		   || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
552       || (d != NULL
553 	  && h->root.type == bfd_link_hash_new
554 	  && (*d->match) (&d->head, NULL, h->root.root.string)))
555     h->dynamic = 1;
556 }
557 
558 /* Record an assignment to a symbol made by a linker script.  We need
559    this in case some dynamic object refers to this symbol.  */
560 
561 bfd_boolean
bfd_elf_record_link_assignment(bfd * output_bfd,struct bfd_link_info * info,const char * name,bfd_boolean provide,bfd_boolean hidden)562 bfd_elf_record_link_assignment (bfd *output_bfd,
563 				struct bfd_link_info *info,
564 				const char *name,
565 				bfd_boolean provide,
566 				bfd_boolean hidden)
567 {
568   struct elf_link_hash_entry *h, *hv;
569   struct elf_link_hash_table *htab;
570   const struct elf_backend_data *bed;
571 
572   if (!is_elf_hash_table (info->hash))
573     return TRUE;
574 
575   htab = elf_hash_table (info);
576   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
577   if (h == NULL)
578     return provide;
579 
580   if (h->versioned == unknown)
581     {
582       /* Set versioned if symbol version is unknown.  */
583       char *version = strrchr (name, ELF_VER_CHR);
584       if (version)
585 	{
586 	  if (version > name && version[-1] != ELF_VER_CHR)
587 	    h->versioned = versioned_hidden;
588 	  else
589 	    h->versioned = versioned;
590 	}
591     }
592 
593   switch (h->root.type)
594     {
595     case bfd_link_hash_defined:
596     case bfd_link_hash_defweak:
597     case bfd_link_hash_common:
598       break;
599     case bfd_link_hash_undefweak:
600     case bfd_link_hash_undefined:
601       /* Since we're defining the symbol, don't let it seem to have not
602 	 been defined.  record_dynamic_symbol and size_dynamic_sections
603 	 may depend on this.  */
604       h->root.type = bfd_link_hash_new;
605       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
606 	bfd_link_repair_undef_list (&htab->root);
607       break;
608     case bfd_link_hash_new:
609       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
610       h->non_elf = 0;
611       break;
612     case bfd_link_hash_indirect:
613       /* We had a versioned symbol in a dynamic library.  We make the
614 	 the versioned symbol point to this one.  */
615       bed = get_elf_backend_data (output_bfd);
616       hv = h;
617       while (hv->root.type == bfd_link_hash_indirect
618 	     || hv->root.type == bfd_link_hash_warning)
619 	hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
620       /* We don't need to update h->root.u since linker will set them
621 	 later.  */
622       h->root.type = bfd_link_hash_undefined;
623       hv->root.type = bfd_link_hash_indirect;
624       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
625       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
626       break;
627     case bfd_link_hash_warning:
628       abort ();
629       break;
630     }
631 
632   /* If this symbol is being provided by the linker script, and it is
633      currently defined by a dynamic object, but not by a regular
634      object, then mark it as undefined so that the generic linker will
635      force the correct value.  */
636   if (provide
637       && h->def_dynamic
638       && !h->def_regular)
639     h->root.type = bfd_link_hash_undefined;
640 
641   /* If this symbol is not being provided by the linker script, and it is
642      currently defined by a dynamic object, but not by a regular object,
643      then clear out any version information because the symbol will not be
644      associated with the dynamic object any more.  */
645   if (!provide
646       && h->def_dynamic
647       && !h->def_regular)
648     h->verinfo.verdef = NULL;
649 
650   h->def_regular = 1;
651 
652   if (hidden)
653     {
654       bed = get_elf_backend_data (output_bfd);
655       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
656 	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
657       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
658     }
659 
660   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
661      and executables.  */
662   if (!bfd_link_relocatable (info)
663       && h->dynindx != -1
664       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
665 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
666     h->forced_local = 1;
667 
668   if ((h->def_dynamic
669        || h->ref_dynamic
670        || bfd_link_dll (info)
671        || elf_hash_table (info)->is_relocatable_executable)
672       && h->dynindx == -1)
673     {
674       if (! bfd_elf_link_record_dynamic_symbol (info, h))
675 	return FALSE;
676 
677       /* If this is a weak defined symbol, and we know a corresponding
678 	 real symbol from the same dynamic object, make sure the real
679 	 symbol is also made into a dynamic symbol.  */
680       if (h->u.weakdef != NULL
681 	  && h->u.weakdef->dynindx == -1)
682 	{
683 	  if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
684 	    return FALSE;
685 	}
686     }
687 
688   return TRUE;
689 }
690 
691 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
692    success, and 2 on a failure caused by attempting to record a symbol
693    in a discarded section, eg. a discarded link-once section symbol.  */
694 
695 int
bfd_elf_link_record_local_dynamic_symbol(struct bfd_link_info * info,bfd * input_bfd,long input_indx)696 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
697 					  bfd *input_bfd,
698 					  long input_indx)
699 {
700   bfd_size_type amt;
701   struct elf_link_local_dynamic_entry *entry;
702   struct elf_link_hash_table *eht;
703   struct elf_strtab_hash *dynstr;
704   size_t dynstr_index;
705   char *name;
706   Elf_External_Sym_Shndx eshndx;
707   char esym[sizeof (Elf64_External_Sym)];
708 
709   if (! is_elf_hash_table (info->hash))
710     return 0;
711 
712   /* See if the entry exists already.  */
713   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
714     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
715       return 1;
716 
717   amt = sizeof (*entry);
718   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
719   if (entry == NULL)
720     return 0;
721 
722   /* Go find the symbol, so that we can find it's name.  */
723   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
724 			     1, input_indx, &entry->isym, esym, &eshndx))
725     {
726       bfd_release (input_bfd, entry);
727       return 0;
728     }
729 
730   if (entry->isym.st_shndx != SHN_UNDEF
731       && entry->isym.st_shndx < SHN_LORESERVE)
732     {
733       asection *s;
734 
735       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
736       if (s == NULL || bfd_is_abs_section (s->output_section))
737 	{
738 	  /* We can still bfd_release here as nothing has done another
739 	     bfd_alloc.  We can't do this later in this function.  */
740 	  bfd_release (input_bfd, entry);
741 	  return 2;
742 	}
743     }
744 
745   name = (bfd_elf_string_from_elf_section
746 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
747 	   entry->isym.st_name));
748 
749   dynstr = elf_hash_table (info)->dynstr;
750   if (dynstr == NULL)
751     {
752       /* Create a strtab to hold the dynamic symbol names.  */
753       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
754       if (dynstr == NULL)
755 	return 0;
756     }
757 
758   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
759   if (dynstr_index == (size_t) -1)
760     return 0;
761   entry->isym.st_name = dynstr_index;
762 
763   eht = elf_hash_table (info);
764 
765   entry->next = eht->dynlocal;
766   eht->dynlocal = entry;
767   entry->input_bfd = input_bfd;
768   entry->input_indx = input_indx;
769   eht->dynsymcount++;
770 
771   /* Whatever binding the symbol had before, it's now local.  */
772   entry->isym.st_info
773     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
774 
775   /* The dynindx will be set at the end of size_dynamic_sections.  */
776 
777   return 1;
778 }
779 
780 /* Return the dynindex of a local dynamic symbol.  */
781 
782 long
_bfd_elf_link_lookup_local_dynindx(struct bfd_link_info * info,bfd * input_bfd,long input_indx)783 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
784 				    bfd *input_bfd,
785 				    long input_indx)
786 {
787   struct elf_link_local_dynamic_entry *e;
788 
789   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
790     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
791       return e->dynindx;
792   return -1;
793 }
794 
795 /* This function is used to renumber the dynamic symbols, if some of
796    them are removed because they are marked as local.  This is called
797    via elf_link_hash_traverse.  */
798 
799 static bfd_boolean
elf_link_renumber_hash_table_dynsyms(struct elf_link_hash_entry * h,void * data)800 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
801 				      void *data)
802 {
803   size_t *count = (size_t *) data;
804 
805   if (h->forced_local)
806     return TRUE;
807 
808   if (h->dynindx != -1)
809     h->dynindx = ++(*count);
810 
811   return TRUE;
812 }
813 
814 
815 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
816    STB_LOCAL binding.  */
817 
818 static bfd_boolean
elf_link_renumber_local_hash_table_dynsyms(struct elf_link_hash_entry * h,void * data)819 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
820 					    void *data)
821 {
822   size_t *count = (size_t *) data;
823 
824   if (!h->forced_local)
825     return TRUE;
826 
827   if (h->dynindx != -1)
828     h->dynindx = ++(*count);
829 
830   return TRUE;
831 }
832 
833 /* Return true if the dynamic symbol for a given section should be
834    omitted when creating a shared library.  */
835 bfd_boolean
_bfd_elf_link_omit_section_dynsym(bfd * output_bfd ATTRIBUTE_UNUSED,struct bfd_link_info * info,asection * p)836 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
837 				   struct bfd_link_info *info,
838 				   asection *p)
839 {
840   struct elf_link_hash_table *htab;
841   asection *ip;
842 
843   switch (elf_section_data (p)->this_hdr.sh_type)
844     {
845     case SHT_PROGBITS:
846     case SHT_NOBITS:
847       /* If sh_type is yet undecided, assume it could be
848 	 SHT_PROGBITS/SHT_NOBITS.  */
849     case SHT_NULL:
850       htab = elf_hash_table (info);
851       if (p == htab->tls_sec)
852 	return FALSE;
853 
854       if (htab->text_index_section != NULL)
855 	return p != htab->text_index_section && p != htab->data_index_section;
856 
857       return (htab->dynobj != NULL
858 	      && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
859 	      && ip->output_section == p);
860 
861       /* There shouldn't be section relative relocations
862 	 against any other section.  */
863     default:
864       return TRUE;
865     }
866 }
867 
868 /* Assign dynsym indices.  In a shared library we generate a section
869    symbol for each output section, which come first.  Next come symbols
870    which have been forced to local binding.  Then all of the back-end
871    allocated local dynamic syms, followed by the rest of the global
872    symbols.  */
873 
874 static unsigned long
_bfd_elf_link_renumber_dynsyms(bfd * output_bfd,struct bfd_link_info * info,unsigned long * section_sym_count)875 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
876 				struct bfd_link_info *info,
877 				unsigned long *section_sym_count)
878 {
879   unsigned long dynsymcount = 0;
880 
881   if (bfd_link_pic (info)
882       || elf_hash_table (info)->is_relocatable_executable)
883     {
884       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
885       asection *p;
886       for (p = output_bfd->sections; p ; p = p->next)
887 	if ((p->flags & SEC_EXCLUDE) == 0
888 	    && (p->flags & SEC_ALLOC) != 0
889 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
890 	  elf_section_data (p)->dynindx = ++dynsymcount;
891 	else
892 	  elf_section_data (p)->dynindx = 0;
893     }
894   *section_sym_count = dynsymcount;
895 
896   elf_link_hash_traverse (elf_hash_table (info),
897 			  elf_link_renumber_local_hash_table_dynsyms,
898 			  &dynsymcount);
899 
900   if (elf_hash_table (info)->dynlocal)
901     {
902       struct elf_link_local_dynamic_entry *p;
903       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
904 	p->dynindx = ++dynsymcount;
905     }
906 
907   elf_link_hash_traverse (elf_hash_table (info),
908 			  elf_link_renumber_hash_table_dynsyms,
909 			  &dynsymcount);
910 
911   /* There is an unused NULL entry at the head of the table which we
912      must account for in our count even if the table is empty since it
913      is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
914      .dynamic section.  */
915   dynsymcount++;
916 
917   elf_hash_table (info)->dynsymcount = dynsymcount;
918   return dynsymcount;
919 }
920 
921 /* Merge st_other field.  */
922 
923 static void
elf_merge_st_other(bfd * abfd,struct elf_link_hash_entry * h,const Elf_Internal_Sym * isym,asection * sec,bfd_boolean definition,bfd_boolean dynamic)924 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
925 		    const Elf_Internal_Sym *isym, asection *sec,
926 		    bfd_boolean definition, bfd_boolean dynamic)
927 {
928   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
929 
930   /* If st_other has a processor-specific meaning, specific
931      code might be needed here.  */
932   if (bed->elf_backend_merge_symbol_attribute)
933     (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
934 						dynamic);
935 
936   if (!dynamic)
937     {
938       unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
939       unsigned hvis = ELF_ST_VISIBILITY (h->other);
940 
941       /* Keep the most constraining visibility.  Leave the remainder
942 	 of the st_other field to elf_backend_merge_symbol_attribute.  */
943       if (symvis - 1 < hvis - 1)
944 	h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
945     }
946   else if (definition
947 	   && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
948 	   && (sec->flags & SEC_READONLY) == 0)
949     h->protected_def = 1;
950 }
951 
952 /* This function is called when we want to merge a new symbol with an
953    existing symbol.  It handles the various cases which arise when we
954    find a definition in a dynamic object, or when there is already a
955    definition in a dynamic object.  The new symbol is described by
956    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
957    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
958    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
959    of an old common symbol.  We set OVERRIDE if the old symbol is
960    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
961    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
962    to change.  By OK to change, we mean that we shouldn't warn if the
963    type or size does change.  */
964 
965 static bfd_boolean
_bfd_elf_merge_symbol(bfd * abfd,struct bfd_link_info * info,const char * name,Elf_Internal_Sym * sym,asection ** psec,bfd_vma * pvalue,struct elf_link_hash_entry ** sym_hash,bfd ** poldbfd,bfd_boolean * pold_weak,unsigned int * pold_alignment,bfd_boolean * skip,bfd_boolean * override,bfd_boolean * type_change_ok,bfd_boolean * size_change_ok,bfd_boolean * matched)966 _bfd_elf_merge_symbol (bfd *abfd,
967 		       struct bfd_link_info *info,
968 		       const char *name,
969 		       Elf_Internal_Sym *sym,
970 		       asection **psec,
971 		       bfd_vma *pvalue,
972 		       struct elf_link_hash_entry **sym_hash,
973 		       bfd **poldbfd,
974 		       bfd_boolean *pold_weak,
975 		       unsigned int *pold_alignment,
976 		       bfd_boolean *skip,
977 		       bfd_boolean *override,
978 		       bfd_boolean *type_change_ok,
979 		       bfd_boolean *size_change_ok,
980 		       bfd_boolean *matched)
981 {
982   asection *sec, *oldsec;
983   struct elf_link_hash_entry *h;
984   struct elf_link_hash_entry *hi;
985   struct elf_link_hash_entry *flip;
986   int bind;
987   bfd *oldbfd;
988   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
989   bfd_boolean newweak, oldweak, newfunc, oldfunc;
990   const struct elf_backend_data *bed;
991   char *new_version;
992 
993   *skip = FALSE;
994   *override = FALSE;
995 
996   sec = *psec;
997   bind = ELF_ST_BIND (sym->st_info);
998 
999   if (! bfd_is_und_section (sec))
1000     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1001   else
1002     h = ((struct elf_link_hash_entry *)
1003 	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1004   if (h == NULL)
1005     return FALSE;
1006   *sym_hash = h;
1007 
1008   bed = get_elf_backend_data (abfd);
1009 
1010   /* NEW_VERSION is the symbol version of the new symbol.  */
1011   if (h->versioned != unversioned)
1012     {
1013       /* Symbol version is unknown or versioned.  */
1014       new_version = strrchr (name, ELF_VER_CHR);
1015       if (new_version)
1016 	{
1017 	  if (h->versioned == unknown)
1018 	    {
1019 	      if (new_version > name && new_version[-1] != ELF_VER_CHR)
1020 		h->versioned = versioned_hidden;
1021 	      else
1022 		h->versioned = versioned;
1023 	    }
1024 	  new_version += 1;
1025 	  if (new_version[0] == '\0')
1026 	    new_version = NULL;
1027 	}
1028       else
1029 	h->versioned = unversioned;
1030     }
1031   else
1032     new_version = NULL;
1033 
1034   /* For merging, we only care about real symbols.  But we need to make
1035      sure that indirect symbol dynamic flags are updated.  */
1036   hi = h;
1037   while (h->root.type == bfd_link_hash_indirect
1038 	 || h->root.type == bfd_link_hash_warning)
1039     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1040 
1041   if (!*matched)
1042     {
1043       if (hi == h || h->root.type == bfd_link_hash_new)
1044 	*matched = TRUE;
1045       else
1046 	{
1047 	  /* OLD_HIDDEN is true if the existing symbol is only visible
1048 	     to the symbol with the same symbol version.  NEW_HIDDEN is
1049 	     true if the new symbol is only visible to the symbol with
1050 	     the same symbol version.  */
1051 	  bfd_boolean old_hidden = h->versioned == versioned_hidden;
1052 	  bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1053 	  if (!old_hidden && !new_hidden)
1054 	    /* The new symbol matches the existing symbol if both
1055 	       aren't hidden.  */
1056 	    *matched = TRUE;
1057 	  else
1058 	    {
1059 	      /* OLD_VERSION is the symbol version of the existing
1060 		 symbol. */
1061 	      char *old_version;
1062 
1063 	      if (h->versioned >= versioned)
1064 		old_version = strrchr (h->root.root.string,
1065 				       ELF_VER_CHR) + 1;
1066 	      else
1067 		 old_version = NULL;
1068 
1069 	      /* The new symbol matches the existing symbol if they
1070 		 have the same symbol version.  */
1071 	      *matched = (old_version == new_version
1072 			  || (old_version != NULL
1073 			      && new_version != NULL
1074 			      && strcmp (old_version, new_version) == 0));
1075 	    }
1076 	}
1077     }
1078 
1079   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1080      existing symbol.  */
1081 
1082   oldbfd = NULL;
1083   oldsec = NULL;
1084   switch (h->root.type)
1085     {
1086     default:
1087       break;
1088 
1089     case bfd_link_hash_undefined:
1090     case bfd_link_hash_undefweak:
1091       oldbfd = h->root.u.undef.abfd;
1092       break;
1093 
1094     case bfd_link_hash_defined:
1095     case bfd_link_hash_defweak:
1096       oldbfd = h->root.u.def.section->owner;
1097       oldsec = h->root.u.def.section;
1098       break;
1099 
1100     case bfd_link_hash_common:
1101       oldbfd = h->root.u.c.p->section->owner;
1102       oldsec = h->root.u.c.p->section;
1103       if (pold_alignment)
1104 	*pold_alignment = h->root.u.c.p->alignment_power;
1105       break;
1106     }
1107   if (poldbfd && *poldbfd == NULL)
1108     *poldbfd = oldbfd;
1109 
1110   /* Differentiate strong and weak symbols.  */
1111   newweak = bind == STB_WEAK;
1112   oldweak = (h->root.type == bfd_link_hash_defweak
1113 	     || h->root.type == bfd_link_hash_undefweak);
1114   if (pold_weak)
1115     *pold_weak = oldweak;
1116 
1117   /* This code is for coping with dynamic objects, and is only useful
1118      if we are doing an ELF link.  */
1119   if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1120     return TRUE;
1121 
1122   /* We have to check it for every instance since the first few may be
1123      references and not all compilers emit symbol type for undefined
1124      symbols.  */
1125   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1126 
1127   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1128      respectively, is from a dynamic object.  */
1129 
1130   newdyn = (abfd->flags & DYNAMIC) != 0;
1131 
1132   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1133      syms and defined syms in dynamic libraries respectively.
1134      ref_dynamic on the other hand can be set for a symbol defined in
1135      a dynamic library, and def_dynamic may not be set;  When the
1136      definition in a dynamic lib is overridden by a definition in the
1137      executable use of the symbol in the dynamic lib becomes a
1138      reference to the executable symbol.  */
1139   if (newdyn)
1140     {
1141       if (bfd_is_und_section (sec))
1142 	{
1143 	  if (bind != STB_WEAK)
1144 	    {
1145 	      h->ref_dynamic_nonweak = 1;
1146 	      hi->ref_dynamic_nonweak = 1;
1147 	    }
1148 	}
1149       else
1150 	{
1151 	  /* Update the existing symbol only if they match. */
1152 	  if (*matched)
1153 	    h->dynamic_def = 1;
1154 	  hi->dynamic_def = 1;
1155 	}
1156     }
1157 
1158   /* If we just created the symbol, mark it as being an ELF symbol.
1159      Other than that, there is nothing to do--there is no merge issue
1160      with a newly defined symbol--so we just return.  */
1161 
1162   if (h->root.type == bfd_link_hash_new)
1163     {
1164       h->non_elf = 0;
1165       return TRUE;
1166     }
1167 
1168   /* In cases involving weak versioned symbols, we may wind up trying
1169      to merge a symbol with itself.  Catch that here, to avoid the
1170      confusion that results if we try to override a symbol with
1171      itself.  The additional tests catch cases like
1172      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1173      dynamic object, which we do want to handle here.  */
1174   if (abfd == oldbfd
1175       && (newweak || oldweak)
1176       && ((abfd->flags & DYNAMIC) == 0
1177 	  || !h->def_regular))
1178     return TRUE;
1179 
1180   olddyn = FALSE;
1181   if (oldbfd != NULL)
1182     olddyn = (oldbfd->flags & DYNAMIC) != 0;
1183   else if (oldsec != NULL)
1184     {
1185       /* This handles the special SHN_MIPS_{TEXT,DATA} section
1186 	 indices used by MIPS ELF.  */
1187       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1188     }
1189 
1190   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1191      respectively, appear to be a definition rather than reference.  */
1192 
1193   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1194 
1195   olddef = (h->root.type != bfd_link_hash_undefined
1196 	    && h->root.type != bfd_link_hash_undefweak
1197 	    && h->root.type != bfd_link_hash_common);
1198 
1199   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1200      respectively, appear to be a function.  */
1201 
1202   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1203 	     && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1204 
1205   oldfunc = (h->type != STT_NOTYPE
1206 	     && bed->is_function_type (h->type));
1207 
1208   /* If creating a default indirect symbol ("foo" or "foo@") from a
1209      dynamic versioned definition ("foo@@") skip doing so if there is
1210      an existing regular definition with a different type.  We don't
1211      want, for example, a "time" variable in the executable overriding
1212      a "time" function in a shared library.  */
1213   if (pold_alignment == NULL
1214       && newdyn
1215       && newdef
1216       && !olddyn
1217       && (olddef || h->root.type == bfd_link_hash_common)
1218       && ELF_ST_TYPE (sym->st_info) != h->type
1219       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1220       && h->type != STT_NOTYPE
1221       && !(newfunc && oldfunc))
1222     {
1223       *skip = TRUE;
1224       return TRUE;
1225     }
1226 
1227   /* Check TLS symbols.  We don't check undefined symbols introduced
1228      by "ld -u" which have no type (and oldbfd NULL), and we don't
1229      check symbols from plugins because they also have no type.  */
1230   if (oldbfd != NULL
1231       && (oldbfd->flags & BFD_PLUGIN) == 0
1232       && (abfd->flags & BFD_PLUGIN) == 0
1233       && ELF_ST_TYPE (sym->st_info) != h->type
1234       && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1235     {
1236       bfd *ntbfd, *tbfd;
1237       bfd_boolean ntdef, tdef;
1238       asection *ntsec, *tsec;
1239 
1240       if (h->type == STT_TLS)
1241 	{
1242 	  ntbfd = abfd;
1243 	  ntsec = sec;
1244 	  ntdef = newdef;
1245 	  tbfd = oldbfd;
1246 	  tsec = oldsec;
1247 	  tdef = olddef;
1248 	}
1249       else
1250 	{
1251 	  ntbfd = oldbfd;
1252 	  ntsec = oldsec;
1253 	  ntdef = olddef;
1254 	  tbfd = abfd;
1255 	  tsec = sec;
1256 	  tdef = newdef;
1257 	}
1258 
1259       if (tdef && ntdef)
1260 	(*_bfd_error_handler)
1261 	  (_("%s: TLS definition in %B section %A "
1262 	     "mismatches non-TLS definition in %B section %A"),
1263 	   tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1264       else if (!tdef && !ntdef)
1265 	(*_bfd_error_handler)
1266 	  (_("%s: TLS reference in %B "
1267 	     "mismatches non-TLS reference in %B"),
1268 	   tbfd, ntbfd, h->root.root.string);
1269       else if (tdef)
1270 	(*_bfd_error_handler)
1271 	  (_("%s: TLS definition in %B section %A "
1272 	     "mismatches non-TLS reference in %B"),
1273 	   tbfd, tsec, ntbfd, h->root.root.string);
1274       else
1275 	(*_bfd_error_handler)
1276 	  (_("%s: TLS reference in %B "
1277 	     "mismatches non-TLS definition in %B section %A"),
1278 	   tbfd, ntbfd, ntsec, h->root.root.string);
1279 
1280       bfd_set_error (bfd_error_bad_value);
1281       return FALSE;
1282     }
1283 
1284   /* If the old symbol has non-default visibility, we ignore the new
1285      definition from a dynamic object.  */
1286   if (newdyn
1287       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1288       && !bfd_is_und_section (sec))
1289     {
1290       *skip = TRUE;
1291       /* Make sure this symbol is dynamic.  */
1292       h->ref_dynamic = 1;
1293       hi->ref_dynamic = 1;
1294       /* A protected symbol has external availability. Make sure it is
1295 	 recorded as dynamic.
1296 
1297 	 FIXME: Should we check type and size for protected symbol?  */
1298       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1299 	return bfd_elf_link_record_dynamic_symbol (info, h);
1300       else
1301 	return TRUE;
1302     }
1303   else if (!newdyn
1304 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1305 	   && h->def_dynamic)
1306     {
1307       /* If the new symbol with non-default visibility comes from a
1308 	 relocatable file and the old definition comes from a dynamic
1309 	 object, we remove the old definition.  */
1310       if (hi->root.type == bfd_link_hash_indirect)
1311 	{
1312 	  /* Handle the case where the old dynamic definition is
1313 	     default versioned.  We need to copy the symbol info from
1314 	     the symbol with default version to the normal one if it
1315 	     was referenced before.  */
1316 	  if (h->ref_regular)
1317 	    {
1318 	      hi->root.type = h->root.type;
1319 	      h->root.type = bfd_link_hash_indirect;
1320 	      (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1321 
1322 	      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1323 	      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1324 		{
1325 		  /* If the new symbol is hidden or internal, completely undo
1326 		     any dynamic link state.  */
1327 		  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1328 		  h->forced_local = 0;
1329 		  h->ref_dynamic = 0;
1330 		}
1331 	      else
1332 		h->ref_dynamic = 1;
1333 
1334 	      h->def_dynamic = 0;
1335 	      /* FIXME: Should we check type and size for protected symbol?  */
1336 	      h->size = 0;
1337 	      h->type = 0;
1338 
1339 	      h = hi;
1340 	    }
1341 	  else
1342 	    h = hi;
1343 	}
1344 
1345       /* If the old symbol was undefined before, then it will still be
1346 	 on the undefs list.  If the new symbol is undefined or
1347 	 common, we can't make it bfd_link_hash_new here, because new
1348 	 undefined or common symbols will be added to the undefs list
1349 	 by _bfd_generic_link_add_one_symbol.  Symbols may not be
1350 	 added twice to the undefs list.  Also, if the new symbol is
1351 	 undefweak then we don't want to lose the strong undef.  */
1352       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1353 	{
1354 	  h->root.type = bfd_link_hash_undefined;
1355 	  h->root.u.undef.abfd = abfd;
1356 	}
1357       else
1358 	{
1359 	  h->root.type = bfd_link_hash_new;
1360 	  h->root.u.undef.abfd = NULL;
1361 	}
1362 
1363       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1364 	{
1365 	  /* If the new symbol is hidden or internal, completely undo
1366 	     any dynamic link state.  */
1367 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1368 	  h->forced_local = 0;
1369 	  h->ref_dynamic = 0;
1370 	}
1371       else
1372 	h->ref_dynamic = 1;
1373       h->def_dynamic = 0;
1374       /* FIXME: Should we check type and size for protected symbol?  */
1375       h->size = 0;
1376       h->type = 0;
1377       return TRUE;
1378     }
1379 
1380   /* If a new weak symbol definition comes from a regular file and the
1381      old symbol comes from a dynamic library, we treat the new one as
1382      strong.  Similarly, an old weak symbol definition from a regular
1383      file is treated as strong when the new symbol comes from a dynamic
1384      library.  Further, an old weak symbol from a dynamic library is
1385      treated as strong if the new symbol is from a dynamic library.
1386      This reflects the way glibc's ld.so works.
1387 
1388      Do this before setting *type_change_ok or *size_change_ok so that
1389      we warn properly when dynamic library symbols are overridden.  */
1390 
1391   if (newdef && !newdyn && olddyn)
1392     newweak = FALSE;
1393   if (olddef && newdyn)
1394     oldweak = FALSE;
1395 
1396   /* Allow changes between different types of function symbol.  */
1397   if (newfunc && oldfunc)
1398     *type_change_ok = TRUE;
1399 
1400   /* It's OK to change the type if either the existing symbol or the
1401      new symbol is weak.  A type change is also OK if the old symbol
1402      is undefined and the new symbol is defined.  */
1403 
1404   if (oldweak
1405       || newweak
1406       || (newdef
1407 	  && h->root.type == bfd_link_hash_undefined))
1408     *type_change_ok = TRUE;
1409 
1410   /* It's OK to change the size if either the existing symbol or the
1411      new symbol is weak, or if the old symbol is undefined.  */
1412 
1413   if (*type_change_ok
1414       || h->root.type == bfd_link_hash_undefined)
1415     *size_change_ok = TRUE;
1416 
1417   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1418      symbol, respectively, appears to be a common symbol in a dynamic
1419      object.  If a symbol appears in an uninitialized section, and is
1420      not weak, and is not a function, then it may be a common symbol
1421      which was resolved when the dynamic object was created.  We want
1422      to treat such symbols specially, because they raise special
1423      considerations when setting the symbol size: if the symbol
1424      appears as a common symbol in a regular object, and the size in
1425      the regular object is larger, we must make sure that we use the
1426      larger size.  This problematic case can always be avoided in C,
1427      but it must be handled correctly when using Fortran shared
1428      libraries.
1429 
1430      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1431      likewise for OLDDYNCOMMON and OLDDEF.
1432 
1433      Note that this test is just a heuristic, and that it is quite
1434      possible to have an uninitialized symbol in a shared object which
1435      is really a definition, rather than a common symbol.  This could
1436      lead to some minor confusion when the symbol really is a common
1437      symbol in some regular object.  However, I think it will be
1438      harmless.  */
1439 
1440   if (newdyn
1441       && newdef
1442       && !newweak
1443       && (sec->flags & SEC_ALLOC) != 0
1444       && (sec->flags & SEC_LOAD) == 0
1445       && sym->st_size > 0
1446       && !newfunc)
1447     newdyncommon = TRUE;
1448   else
1449     newdyncommon = FALSE;
1450 
1451   if (olddyn
1452       && olddef
1453       && h->root.type == bfd_link_hash_defined
1454       && h->def_dynamic
1455       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1456       && (h->root.u.def.section->flags & SEC_LOAD) == 0
1457       && h->size > 0
1458       && !oldfunc)
1459     olddyncommon = TRUE;
1460   else
1461     olddyncommon = FALSE;
1462 
1463   /* We now know everything about the old and new symbols.  We ask the
1464      backend to check if we can merge them.  */
1465   if (bed->merge_symbol != NULL)
1466     {
1467       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1468 	return FALSE;
1469       sec = *psec;
1470     }
1471 
1472   /* If both the old and the new symbols look like common symbols in a
1473      dynamic object, set the size of the symbol to the larger of the
1474      two.  */
1475 
1476   if (olddyncommon
1477       && newdyncommon
1478       && sym->st_size != h->size)
1479     {
1480       /* Since we think we have two common symbols, issue a multiple
1481 	 common warning if desired.  Note that we only warn if the
1482 	 size is different.  If the size is the same, we simply let
1483 	 the old symbol override the new one as normally happens with
1484 	 symbols defined in dynamic objects.  */
1485 
1486       (*info->callbacks->multiple_common) (info, &h->root, abfd,
1487 					   bfd_link_hash_common, sym->st_size);
1488       if (sym->st_size > h->size)
1489 	h->size = sym->st_size;
1490 
1491       *size_change_ok = TRUE;
1492     }
1493 
1494   /* If we are looking at a dynamic object, and we have found a
1495      definition, we need to see if the symbol was already defined by
1496      some other object.  If so, we want to use the existing
1497      definition, and we do not want to report a multiple symbol
1498      definition error; we do this by clobbering *PSEC to be
1499      bfd_und_section_ptr.
1500 
1501      We treat a common symbol as a definition if the symbol in the
1502      shared library is a function, since common symbols always
1503      represent variables; this can cause confusion in principle, but
1504      any such confusion would seem to indicate an erroneous program or
1505      shared library.  We also permit a common symbol in a regular
1506      object to override a weak symbol in a shared object.  A common
1507      symbol in executable also overrides a symbol in a shared object.  */
1508 
1509   if (newdyn
1510       && newdef
1511       && (olddef
1512 	  || (h->root.type == bfd_link_hash_common
1513 	      && (newweak
1514 		  || newfunc
1515 		  || (!olddyn && bfd_link_executable (info))))))
1516     {
1517       *override = TRUE;
1518       newdef = FALSE;
1519       newdyncommon = FALSE;
1520 
1521       *psec = sec = bfd_und_section_ptr;
1522       *size_change_ok = TRUE;
1523 
1524       /* If we get here when the old symbol is a common symbol, then
1525 	 we are explicitly letting it override a weak symbol or
1526 	 function in a dynamic object, and we don't want to warn about
1527 	 a type change.  If the old symbol is a defined symbol, a type
1528 	 change warning may still be appropriate.  */
1529 
1530       if (h->root.type == bfd_link_hash_common)
1531 	*type_change_ok = TRUE;
1532     }
1533 
1534   /* Handle the special case of an old common symbol merging with a
1535      new symbol which looks like a common symbol in a shared object.
1536      We change *PSEC and *PVALUE to make the new symbol look like a
1537      common symbol, and let _bfd_generic_link_add_one_symbol do the
1538      right thing.  */
1539 
1540   if (newdyncommon
1541       && h->root.type == bfd_link_hash_common)
1542     {
1543       *override = TRUE;
1544       newdef = FALSE;
1545       newdyncommon = FALSE;
1546       *pvalue = sym->st_size;
1547       *psec = sec = bed->common_section (oldsec);
1548       *size_change_ok = TRUE;
1549     }
1550 
1551   /* Skip weak definitions of symbols that are already defined.  */
1552   if (newdef && olddef && newweak)
1553     {
1554       /* Don't skip new non-IR weak syms.  */
1555       if (!(oldbfd != NULL
1556 	    && (oldbfd->flags & BFD_PLUGIN) != 0
1557 	    && (abfd->flags & BFD_PLUGIN) == 0))
1558 	{
1559 	  newdef = FALSE;
1560 	  *skip = TRUE;
1561 	}
1562 
1563       /* Merge st_other.  If the symbol already has a dynamic index,
1564 	 but visibility says it should not be visible, turn it into a
1565 	 local symbol.  */
1566       elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1567       if (h->dynindx != -1)
1568 	switch (ELF_ST_VISIBILITY (h->other))
1569 	  {
1570 	  case STV_INTERNAL:
1571 	  case STV_HIDDEN:
1572 	    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1573 	    break;
1574 	  }
1575     }
1576 
1577   /* If the old symbol is from a dynamic object, and the new symbol is
1578      a definition which is not from a dynamic object, then the new
1579      symbol overrides the old symbol.  Symbols from regular files
1580      always take precedence over symbols from dynamic objects, even if
1581      they are defined after the dynamic object in the link.
1582 
1583      As above, we again permit a common symbol in a regular object to
1584      override a definition in a shared object if the shared object
1585      symbol is a function or is weak.  */
1586 
1587   flip = NULL;
1588   if (!newdyn
1589       && (newdef
1590 	  || (bfd_is_com_section (sec)
1591 	      && (oldweak || oldfunc)))
1592       && olddyn
1593       && olddef
1594       && h->def_dynamic)
1595     {
1596       /* Change the hash table entry to undefined, and let
1597 	 _bfd_generic_link_add_one_symbol do the right thing with the
1598 	 new definition.  */
1599 
1600       h->root.type = bfd_link_hash_undefined;
1601       h->root.u.undef.abfd = h->root.u.def.section->owner;
1602       *size_change_ok = TRUE;
1603 
1604       olddef = FALSE;
1605       olddyncommon = FALSE;
1606 
1607       /* We again permit a type change when a common symbol may be
1608 	 overriding a function.  */
1609 
1610       if (bfd_is_com_section (sec))
1611 	{
1612 	  if (oldfunc)
1613 	    {
1614 	      /* If a common symbol overrides a function, make sure
1615 		 that it isn't defined dynamically nor has type
1616 		 function.  */
1617 	      h->def_dynamic = 0;
1618 	      h->type = STT_NOTYPE;
1619 	    }
1620 	  *type_change_ok = TRUE;
1621 	}
1622 
1623       if (hi->root.type == bfd_link_hash_indirect)
1624 	flip = hi;
1625       else
1626 	/* This union may have been set to be non-NULL when this symbol
1627 	   was seen in a dynamic object.  We must force the union to be
1628 	   NULL, so that it is correct for a regular symbol.  */
1629 	h->verinfo.vertree = NULL;
1630     }
1631 
1632   /* Handle the special case of a new common symbol merging with an
1633      old symbol that looks like it might be a common symbol defined in
1634      a shared object.  Note that we have already handled the case in
1635      which a new common symbol should simply override the definition
1636      in the shared library.  */
1637 
1638   if (! newdyn
1639       && bfd_is_com_section (sec)
1640       && olddyncommon)
1641     {
1642       /* It would be best if we could set the hash table entry to a
1643 	 common symbol, but we don't know what to use for the section
1644 	 or the alignment.  */
1645       (*info->callbacks->multiple_common) (info, &h->root, abfd,
1646 					   bfd_link_hash_common, sym->st_size);
1647 
1648       /* If the presumed common symbol in the dynamic object is
1649 	 larger, pretend that the new symbol has its size.  */
1650 
1651       if (h->size > *pvalue)
1652 	*pvalue = h->size;
1653 
1654       /* We need to remember the alignment required by the symbol
1655 	 in the dynamic object.  */
1656       BFD_ASSERT (pold_alignment);
1657       *pold_alignment = h->root.u.def.section->alignment_power;
1658 
1659       olddef = FALSE;
1660       olddyncommon = FALSE;
1661 
1662       h->root.type = bfd_link_hash_undefined;
1663       h->root.u.undef.abfd = h->root.u.def.section->owner;
1664 
1665       *size_change_ok = TRUE;
1666       *type_change_ok = TRUE;
1667 
1668       if (hi->root.type == bfd_link_hash_indirect)
1669 	flip = hi;
1670       else
1671 	h->verinfo.vertree = NULL;
1672     }
1673 
1674   if (flip != NULL)
1675     {
1676       /* Handle the case where we had a versioned symbol in a dynamic
1677 	 library and now find a definition in a normal object.  In this
1678 	 case, we make the versioned symbol point to the normal one.  */
1679       flip->root.type = h->root.type;
1680       flip->root.u.undef.abfd = h->root.u.undef.abfd;
1681       h->root.type = bfd_link_hash_indirect;
1682       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1683       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1684       if (h->def_dynamic)
1685 	{
1686 	  h->def_dynamic = 0;
1687 	  flip->ref_dynamic = 1;
1688 	}
1689     }
1690 
1691   return TRUE;
1692 }
1693 
1694 /* This function is called to create an indirect symbol from the
1695    default for the symbol with the default version if needed. The
1696    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1697    set DYNSYM if the new indirect symbol is dynamic.  */
1698 
1699 static bfd_boolean
_bfd_elf_add_default_symbol(bfd * abfd,struct bfd_link_info * info,struct elf_link_hash_entry * h,const char * name,Elf_Internal_Sym * sym,asection * sec,bfd_vma value,bfd ** poldbfd,bfd_boolean * dynsym)1700 _bfd_elf_add_default_symbol (bfd *abfd,
1701 			     struct bfd_link_info *info,
1702 			     struct elf_link_hash_entry *h,
1703 			     const char *name,
1704 			     Elf_Internal_Sym *sym,
1705 			     asection *sec,
1706 			     bfd_vma value,
1707 			     bfd **poldbfd,
1708 			     bfd_boolean *dynsym)
1709 {
1710   bfd_boolean type_change_ok;
1711   bfd_boolean size_change_ok;
1712   bfd_boolean skip;
1713   char *shortname;
1714   struct elf_link_hash_entry *hi;
1715   struct bfd_link_hash_entry *bh;
1716   const struct elf_backend_data *bed;
1717   bfd_boolean collect;
1718   bfd_boolean dynamic;
1719   bfd_boolean override;
1720   char *p;
1721   size_t len, shortlen;
1722   asection *tmp_sec;
1723   bfd_boolean matched;
1724 
1725   if (h->versioned == unversioned || h->versioned == versioned_hidden)
1726     return TRUE;
1727 
1728   /* If this symbol has a version, and it is the default version, we
1729      create an indirect symbol from the default name to the fully
1730      decorated name.  This will cause external references which do not
1731      specify a version to be bound to this version of the symbol.  */
1732   p = strchr (name, ELF_VER_CHR);
1733   if (h->versioned == unknown)
1734     {
1735       if (p == NULL)
1736 	{
1737 	  h->versioned = unversioned;
1738 	  return TRUE;
1739 	}
1740       else
1741 	{
1742 	  if (p[1] != ELF_VER_CHR)
1743 	    {
1744 	      h->versioned = versioned_hidden;
1745 	      return TRUE;
1746 	    }
1747 	  else
1748 	    h->versioned = versioned;
1749 	}
1750     }
1751   else
1752     {
1753       /* PR ld/19073: We may see an unversioned definition after the
1754 	 default version.  */
1755       if (p == NULL)
1756 	return TRUE;
1757     }
1758 
1759   bed = get_elf_backend_data (abfd);
1760   collect = bed->collect;
1761   dynamic = (abfd->flags & DYNAMIC) != 0;
1762 
1763   shortlen = p - name;
1764   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1765   if (shortname == NULL)
1766     return FALSE;
1767   memcpy (shortname, name, shortlen);
1768   shortname[shortlen] = '\0';
1769 
1770   /* We are going to create a new symbol.  Merge it with any existing
1771      symbol with this name.  For the purposes of the merge, act as
1772      though we were defining the symbol we just defined, although we
1773      actually going to define an indirect symbol.  */
1774   type_change_ok = FALSE;
1775   size_change_ok = FALSE;
1776   matched = TRUE;
1777   tmp_sec = sec;
1778   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1779 			      &hi, poldbfd, NULL, NULL, &skip, &override,
1780 			      &type_change_ok, &size_change_ok, &matched))
1781     return FALSE;
1782 
1783   if (skip)
1784     goto nondefault;
1785 
1786   if (hi->def_regular)
1787     {
1788       /* If the undecorated symbol will have a version added by a
1789 	 script different to H, then don't indirect to/from the
1790 	 undecorated symbol.  This isn't ideal because we may not yet
1791 	 have seen symbol versions, if given by a script on the
1792 	 command line rather than via --version-script.  */
1793       if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1794 	{
1795 	  bfd_boolean hide;
1796 
1797 	  hi->verinfo.vertree
1798 	    = bfd_find_version_for_sym (info->version_info,
1799 					hi->root.root.string, &hide);
1800 	  if (hi->verinfo.vertree != NULL && hide)
1801 	    {
1802 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1803 	      goto nondefault;
1804 	    }
1805 	}
1806       if (hi->verinfo.vertree != NULL
1807 	  && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1808 	goto nondefault;
1809     }
1810 
1811   if (! override)
1812     {
1813       /* Add the default symbol if not performing a relocatable link.  */
1814       if (! bfd_link_relocatable (info))
1815 	{
1816 	  bh = &hi->root;
1817 	  if (! (_bfd_generic_link_add_one_symbol
1818 		 (info, abfd, shortname, BSF_INDIRECT,
1819 		  bfd_ind_section_ptr,
1820 		  0, name, FALSE, collect, &bh)))
1821 	    return FALSE;
1822 	  hi = (struct elf_link_hash_entry *) bh;
1823 	}
1824     }
1825   else
1826     {
1827       /* In this case the symbol named SHORTNAME is overriding the
1828 	 indirect symbol we want to add.  We were planning on making
1829 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
1830 	 is the name without a version.  NAME is the fully versioned
1831 	 name, and it is the default version.
1832 
1833 	 Overriding means that we already saw a definition for the
1834 	 symbol SHORTNAME in a regular object, and it is overriding
1835 	 the symbol defined in the dynamic object.
1836 
1837 	 When this happens, we actually want to change NAME, the
1838 	 symbol we just added, to refer to SHORTNAME.  This will cause
1839 	 references to NAME in the shared object to become references
1840 	 to SHORTNAME in the regular object.  This is what we expect
1841 	 when we override a function in a shared object: that the
1842 	 references in the shared object will be mapped to the
1843 	 definition in the regular object.  */
1844 
1845       while (hi->root.type == bfd_link_hash_indirect
1846 	     || hi->root.type == bfd_link_hash_warning)
1847 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1848 
1849       h->root.type = bfd_link_hash_indirect;
1850       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1851       if (h->def_dynamic)
1852 	{
1853 	  h->def_dynamic = 0;
1854 	  hi->ref_dynamic = 1;
1855 	  if (hi->ref_regular
1856 	      || hi->def_regular)
1857 	    {
1858 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1859 		return FALSE;
1860 	    }
1861 	}
1862 
1863       /* Now set HI to H, so that the following code will set the
1864 	 other fields correctly.  */
1865       hi = h;
1866     }
1867 
1868   /* Check if HI is a warning symbol.  */
1869   if (hi->root.type == bfd_link_hash_warning)
1870     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1871 
1872   /* If there is a duplicate definition somewhere, then HI may not
1873      point to an indirect symbol.  We will have reported an error to
1874      the user in that case.  */
1875 
1876   if (hi->root.type == bfd_link_hash_indirect)
1877     {
1878       struct elf_link_hash_entry *ht;
1879 
1880       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1881       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1882 
1883       /* A reference to the SHORTNAME symbol from a dynamic library
1884 	 will be satisfied by the versioned symbol at runtime.  In
1885 	 effect, we have a reference to the versioned symbol.  */
1886       ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1887       hi->dynamic_def |= ht->dynamic_def;
1888 
1889       /* See if the new flags lead us to realize that the symbol must
1890 	 be dynamic.  */
1891       if (! *dynsym)
1892 	{
1893 	  if (! dynamic)
1894 	    {
1895 	      if (! bfd_link_executable (info)
1896 		  || hi->def_dynamic
1897 		  || hi->ref_dynamic)
1898 		*dynsym = TRUE;
1899 	    }
1900 	  else
1901 	    {
1902 	      if (hi->ref_regular)
1903 		*dynsym = TRUE;
1904 	    }
1905 	}
1906     }
1907 
1908   /* We also need to define an indirection from the nondefault version
1909      of the symbol.  */
1910 
1911 nondefault:
1912   len = strlen (name);
1913   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1914   if (shortname == NULL)
1915     return FALSE;
1916   memcpy (shortname, name, shortlen);
1917   memcpy (shortname + shortlen, p + 1, len - shortlen);
1918 
1919   /* Once again, merge with any existing symbol.  */
1920   type_change_ok = FALSE;
1921   size_change_ok = FALSE;
1922   tmp_sec = sec;
1923   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1924 			      &hi, poldbfd, NULL, NULL, &skip, &override,
1925 			      &type_change_ok, &size_change_ok, &matched))
1926     return FALSE;
1927 
1928   if (skip)
1929     return TRUE;
1930 
1931   if (override)
1932     {
1933       /* Here SHORTNAME is a versioned name, so we don't expect to see
1934 	 the type of override we do in the case above unless it is
1935 	 overridden by a versioned definition.  */
1936       if (hi->root.type != bfd_link_hash_defined
1937 	  && hi->root.type != bfd_link_hash_defweak)
1938 	(*_bfd_error_handler)
1939 	  (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1940 	   abfd, shortname);
1941     }
1942   else
1943     {
1944       bh = &hi->root;
1945       if (! (_bfd_generic_link_add_one_symbol
1946 	     (info, abfd, shortname, BSF_INDIRECT,
1947 	      bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1948 	return FALSE;
1949       hi = (struct elf_link_hash_entry *) bh;
1950 
1951       /* If there is a duplicate definition somewhere, then HI may not
1952 	 point to an indirect symbol.  We will have reported an error
1953 	 to the user in that case.  */
1954 
1955       if (hi->root.type == bfd_link_hash_indirect)
1956 	{
1957 	  (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1958 	  h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1959 	  hi->dynamic_def |= h->dynamic_def;
1960 
1961 	  /* See if the new flags lead us to realize that the symbol
1962 	     must be dynamic.  */
1963 	  if (! *dynsym)
1964 	    {
1965 	      if (! dynamic)
1966 		{
1967 		  if (! bfd_link_executable (info)
1968 		      || hi->ref_dynamic)
1969 		    *dynsym = TRUE;
1970 		}
1971 	      else
1972 		{
1973 		  if (hi->ref_regular)
1974 		    *dynsym = TRUE;
1975 		}
1976 	    }
1977 	}
1978     }
1979 
1980   return TRUE;
1981 }
1982 
1983 /* This routine is used to export all defined symbols into the dynamic
1984    symbol table.  It is called via elf_link_hash_traverse.  */
1985 
1986 static bfd_boolean
_bfd_elf_export_symbol(struct elf_link_hash_entry * h,void * data)1987 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1988 {
1989   struct elf_info_failed *eif = (struct elf_info_failed *) data;
1990 
1991   /* Ignore indirect symbols.  These are added by the versioning code.  */
1992   if (h->root.type == bfd_link_hash_indirect)
1993     return TRUE;
1994 
1995   /* Ignore this if we won't export it.  */
1996   if (!eif->info->export_dynamic && !h->dynamic)
1997     return TRUE;
1998 
1999   if (h->dynindx == -1
2000       && (h->def_regular || h->ref_regular)
2001       && ! bfd_hide_sym_by_version (eif->info->version_info,
2002 				    h->root.root.string))
2003     {
2004       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2005 	{
2006 	  eif->failed = TRUE;
2007 	  return FALSE;
2008 	}
2009     }
2010 
2011   return TRUE;
2012 }
2013 
2014 /* Look through the symbols which are defined in other shared
2015    libraries and referenced here.  Update the list of version
2016    dependencies.  This will be put into the .gnu.version_r section.
2017    This function is called via elf_link_hash_traverse.  */
2018 
2019 static bfd_boolean
_bfd_elf_link_find_version_dependencies(struct elf_link_hash_entry * h,void * data)2020 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2021 					 void *data)
2022 {
2023   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2024   Elf_Internal_Verneed *t;
2025   Elf_Internal_Vernaux *a;
2026   bfd_size_type amt;
2027 
2028   /* We only care about symbols defined in shared objects with version
2029      information.  */
2030   if (!h->def_dynamic
2031       || h->def_regular
2032       || h->dynindx == -1
2033       || h->verinfo.verdef == NULL
2034       || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2035 	  & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2036     return TRUE;
2037 
2038   /* See if we already know about this version.  */
2039   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2040        t != NULL;
2041        t = t->vn_nextref)
2042     {
2043       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2044 	continue;
2045 
2046       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2047 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2048 	  return TRUE;
2049 
2050       break;
2051     }
2052 
2053   /* This is a new version.  Add it to tree we are building.  */
2054 
2055   if (t == NULL)
2056     {
2057       amt = sizeof *t;
2058       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2059       if (t == NULL)
2060 	{
2061 	  rinfo->failed = TRUE;
2062 	  return FALSE;
2063 	}
2064 
2065       t->vn_bfd = h->verinfo.verdef->vd_bfd;
2066       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2067       elf_tdata (rinfo->info->output_bfd)->verref = t;
2068     }
2069 
2070   amt = sizeof *a;
2071   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2072   if (a == NULL)
2073     {
2074       rinfo->failed = TRUE;
2075       return FALSE;
2076     }
2077 
2078   /* Note that we are copying a string pointer here, and testing it
2079      above.  If bfd_elf_string_from_elf_section is ever changed to
2080      discard the string data when low in memory, this will have to be
2081      fixed.  */
2082   a->vna_nodename = h->verinfo.verdef->vd_nodename;
2083 
2084   a->vna_flags = h->verinfo.verdef->vd_flags;
2085   a->vna_nextptr = t->vn_auxptr;
2086 
2087   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2088   ++rinfo->vers;
2089 
2090   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2091 
2092   t->vn_auxptr = a;
2093 
2094   return TRUE;
2095 }
2096 
2097 /* Figure out appropriate versions for all the symbols.  We may not
2098    have the version number script until we have read all of the input
2099    files, so until that point we don't know which symbols should be
2100    local.  This function is called via elf_link_hash_traverse.  */
2101 
2102 static bfd_boolean
_bfd_elf_link_assign_sym_version(struct elf_link_hash_entry * h,void * data)2103 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2104 {
2105   struct elf_info_failed *sinfo;
2106   struct bfd_link_info *info;
2107   const struct elf_backend_data *bed;
2108   struct elf_info_failed eif;
2109   char *p;
2110 
2111   sinfo = (struct elf_info_failed *) data;
2112   info = sinfo->info;
2113 
2114   /* Fix the symbol flags.  */
2115   eif.failed = FALSE;
2116   eif.info = info;
2117   if (! _bfd_elf_fix_symbol_flags (h, &eif))
2118     {
2119       if (eif.failed)
2120 	sinfo->failed = TRUE;
2121       return FALSE;
2122     }
2123 
2124   /* We only need version numbers for symbols defined in regular
2125      objects.  */
2126   if (!h->def_regular)
2127     return TRUE;
2128 
2129   bed = get_elf_backend_data (info->output_bfd);
2130   p = strchr (h->root.root.string, ELF_VER_CHR);
2131   if (p != NULL && h->verinfo.vertree == NULL)
2132     {
2133       struct bfd_elf_version_tree *t;
2134 
2135       ++p;
2136       if (*p == ELF_VER_CHR)
2137 	++p;
2138 
2139       /* If there is no version string, we can just return out.  */
2140       if (*p == '\0')
2141 	return TRUE;
2142 
2143       /* Look for the version.  If we find it, it is no longer weak.  */
2144       for (t = sinfo->info->version_info; t != NULL; t = t->next)
2145 	{
2146 	  if (strcmp (t->name, p) == 0)
2147 	    {
2148 	      size_t len;
2149 	      char *alc;
2150 	      struct bfd_elf_version_expr *d;
2151 
2152 	      len = p - h->root.root.string;
2153 	      alc = (char *) bfd_malloc (len);
2154 	      if (alc == NULL)
2155 		{
2156 		  sinfo->failed = TRUE;
2157 		  return FALSE;
2158 		}
2159 	      memcpy (alc, h->root.root.string, len - 1);
2160 	      alc[len - 1] = '\0';
2161 	      if (alc[len - 2] == ELF_VER_CHR)
2162 		alc[len - 2] = '\0';
2163 
2164 	      h->verinfo.vertree = t;
2165 	      t->used = TRUE;
2166 	      d = NULL;
2167 
2168 	      if (t->globals.list != NULL)
2169 		d = (*t->match) (&t->globals, NULL, alc);
2170 
2171 	      /* See if there is anything to force this symbol to
2172 		 local scope.  */
2173 	      if (d == NULL && t->locals.list != NULL)
2174 		{
2175 		  d = (*t->match) (&t->locals, NULL, alc);
2176 		  if (d != NULL
2177 		      && h->dynindx != -1
2178 		      && ! info->export_dynamic)
2179 		    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2180 		}
2181 
2182 	      free (alc);
2183 	      break;
2184 	    }
2185 	}
2186 
2187       /* If we are building an application, we need to create a
2188 	 version node for this version.  */
2189       if (t == NULL && bfd_link_executable (info))
2190 	{
2191 	  struct bfd_elf_version_tree **pp;
2192 	  int version_index;
2193 
2194 	  /* If we aren't going to export this symbol, we don't need
2195 	     to worry about it.  */
2196 	  if (h->dynindx == -1)
2197 	    return TRUE;
2198 
2199 	  t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2200 							  sizeof *t);
2201 	  if (t == NULL)
2202 	    {
2203 	      sinfo->failed = TRUE;
2204 	      return FALSE;
2205 	    }
2206 
2207 	  t->name = p;
2208 	  t->name_indx = (unsigned int) -1;
2209 	  t->used = TRUE;
2210 
2211 	  version_index = 1;
2212 	  /* Don't count anonymous version tag.  */
2213 	  if (sinfo->info->version_info != NULL
2214 	      && sinfo->info->version_info->vernum == 0)
2215 	    version_index = 0;
2216 	  for (pp = &sinfo->info->version_info;
2217 	       *pp != NULL;
2218 	       pp = &(*pp)->next)
2219 	    ++version_index;
2220 	  t->vernum = version_index;
2221 
2222 	  *pp = t;
2223 
2224 	  h->verinfo.vertree = t;
2225 	}
2226       else if (t == NULL)
2227 	{
2228 	  /* We could not find the version for a symbol when
2229 	     generating a shared archive.  Return an error.  */
2230 	  (*_bfd_error_handler)
2231 	    (_("%B: version node not found for symbol %s"),
2232 	     info->output_bfd, h->root.root.string);
2233 	  bfd_set_error (bfd_error_bad_value);
2234 	  sinfo->failed = TRUE;
2235 	  return FALSE;
2236 	}
2237     }
2238 
2239   /* If we don't have a version for this symbol, see if we can find
2240      something.  */
2241   if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2242     {
2243       bfd_boolean hide;
2244 
2245       h->verinfo.vertree
2246 	= bfd_find_version_for_sym (sinfo->info->version_info,
2247 				    h->root.root.string, &hide);
2248       if (h->verinfo.vertree != NULL && hide)
2249 	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
2250     }
2251 
2252   return TRUE;
2253 }
2254 
2255 /* Read and swap the relocs from the section indicated by SHDR.  This
2256    may be either a REL or a RELA section.  The relocations are
2257    translated into RELA relocations and stored in INTERNAL_RELOCS,
2258    which should have already been allocated to contain enough space.
2259    The EXTERNAL_RELOCS are a buffer where the external form of the
2260    relocations should be stored.
2261 
2262    Returns FALSE if something goes wrong.  */
2263 
2264 static bfd_boolean
elf_link_read_relocs_from_section(bfd * abfd,asection * sec,Elf_Internal_Shdr * shdr,void * external_relocs,Elf_Internal_Rela * internal_relocs)2265 elf_link_read_relocs_from_section (bfd *abfd,
2266 				   asection *sec,
2267 				   Elf_Internal_Shdr *shdr,
2268 				   void *external_relocs,
2269 				   Elf_Internal_Rela *internal_relocs)
2270 {
2271   const struct elf_backend_data *bed;
2272   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2273   const bfd_byte *erela;
2274   const bfd_byte *erelaend;
2275   Elf_Internal_Rela *irela;
2276   Elf_Internal_Shdr *symtab_hdr;
2277   size_t nsyms;
2278 
2279   /* Position ourselves at the start of the section.  */
2280   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2281     return FALSE;
2282 
2283   /* Read the relocations.  */
2284   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2285     return FALSE;
2286 
2287   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2288   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2289 
2290   bed = get_elf_backend_data (abfd);
2291 
2292   /* Convert the external relocations to the internal format.  */
2293   if (shdr->sh_entsize == bed->s->sizeof_rel)
2294     swap_in = bed->s->swap_reloc_in;
2295   else if (shdr->sh_entsize == bed->s->sizeof_rela)
2296     swap_in = bed->s->swap_reloca_in;
2297   else
2298     {
2299       bfd_set_error (bfd_error_wrong_format);
2300       return FALSE;
2301     }
2302 
2303   erela = (const bfd_byte *) external_relocs;
2304   erelaend = erela + shdr->sh_size;
2305   irela = internal_relocs;
2306   while (erela < erelaend)
2307     {
2308       bfd_vma r_symndx;
2309 
2310       (*swap_in) (abfd, erela, irela);
2311       r_symndx = ELF32_R_SYM (irela->r_info);
2312       if (bed->s->arch_size == 64)
2313 	r_symndx >>= 24;
2314       if (nsyms > 0)
2315 	{
2316 	  if ((size_t) r_symndx >= nsyms)
2317 	    {
2318 	      (*_bfd_error_handler)
2319 		(_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2320 		   " for offset 0x%lx in section `%A'"),
2321 		 abfd, sec,
2322 		 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2323 	      bfd_set_error (bfd_error_bad_value);
2324 	      return FALSE;
2325 	    }
2326 	}
2327       else if (r_symndx != STN_UNDEF)
2328 	{
2329 	  (*_bfd_error_handler)
2330 	    (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2331 	       " when the object file has no symbol table"),
2332 	     abfd, sec,
2333 	     (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2334 	  bfd_set_error (bfd_error_bad_value);
2335 	  return FALSE;
2336 	}
2337       irela += bed->s->int_rels_per_ext_rel;
2338       erela += shdr->sh_entsize;
2339     }
2340 
2341   return TRUE;
2342 }
2343 
2344 /* Read and swap the relocs for a section O.  They may have been
2345    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2346    not NULL, they are used as buffers to read into.  They are known to
2347    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2348    the return value is allocated using either malloc or bfd_alloc,
2349    according to the KEEP_MEMORY argument.  If O has two relocation
2350    sections (both REL and RELA relocations), then the REL_HDR
2351    relocations will appear first in INTERNAL_RELOCS, followed by the
2352    RELA_HDR relocations.  */
2353 
2354 Elf_Internal_Rela *
_bfd_elf_link_read_relocs(bfd * abfd,asection * o,void * external_relocs,Elf_Internal_Rela * internal_relocs,bfd_boolean keep_memory)2355 _bfd_elf_link_read_relocs (bfd *abfd,
2356 			   asection *o,
2357 			   void *external_relocs,
2358 			   Elf_Internal_Rela *internal_relocs,
2359 			   bfd_boolean keep_memory)
2360 {
2361   void *alloc1 = NULL;
2362   Elf_Internal_Rela *alloc2 = NULL;
2363   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2364   struct bfd_elf_section_data *esdo = elf_section_data (o);
2365   Elf_Internal_Rela *internal_rela_relocs;
2366 
2367   if (esdo->relocs != NULL)
2368     return esdo->relocs;
2369 
2370   if (o->reloc_count == 0)
2371     return NULL;
2372 
2373   if (internal_relocs == NULL)
2374     {
2375       bfd_size_type size;
2376 
2377       size = o->reloc_count;
2378       size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2379       if (keep_memory)
2380 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2381       else
2382 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2383       if (internal_relocs == NULL)
2384 	goto error_return;
2385     }
2386 
2387   if (external_relocs == NULL)
2388     {
2389       bfd_size_type size = 0;
2390 
2391       if (esdo->rel.hdr)
2392 	size += esdo->rel.hdr->sh_size;
2393       if (esdo->rela.hdr)
2394 	size += esdo->rela.hdr->sh_size;
2395 
2396       alloc1 = bfd_malloc (size);
2397       if (alloc1 == NULL)
2398 	goto error_return;
2399       external_relocs = alloc1;
2400     }
2401 
2402   internal_rela_relocs = internal_relocs;
2403   if (esdo->rel.hdr)
2404     {
2405       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2406 					      external_relocs,
2407 					      internal_relocs))
2408 	goto error_return;
2409       external_relocs = (((bfd_byte *) external_relocs)
2410 			 + esdo->rel.hdr->sh_size);
2411       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2412 			       * bed->s->int_rels_per_ext_rel);
2413     }
2414 
2415   if (esdo->rela.hdr
2416       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2417 					      external_relocs,
2418 					      internal_rela_relocs)))
2419     goto error_return;
2420 
2421   /* Cache the results for next time, if we can.  */
2422   if (keep_memory)
2423     esdo->relocs = internal_relocs;
2424 
2425   if (alloc1 != NULL)
2426     free (alloc1);
2427 
2428   /* Don't free alloc2, since if it was allocated we are passing it
2429      back (under the name of internal_relocs).  */
2430 
2431   return internal_relocs;
2432 
2433  error_return:
2434   if (alloc1 != NULL)
2435     free (alloc1);
2436   if (alloc2 != NULL)
2437     {
2438       if (keep_memory)
2439 	bfd_release (abfd, alloc2);
2440       else
2441 	free (alloc2);
2442     }
2443   return NULL;
2444 }
2445 
2446 /* Compute the size of, and allocate space for, REL_HDR which is the
2447    section header for a section containing relocations for O.  */
2448 
2449 static bfd_boolean
_bfd_elf_link_size_reloc_section(bfd * abfd,struct bfd_elf_section_reloc_data * reldata)2450 _bfd_elf_link_size_reloc_section (bfd *abfd,
2451 				  struct bfd_elf_section_reloc_data *reldata)
2452 {
2453   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2454 
2455   /* That allows us to calculate the size of the section.  */
2456   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2457 
2458   /* The contents field must last into write_object_contents, so we
2459      allocate it with bfd_alloc rather than malloc.  Also since we
2460      cannot be sure that the contents will actually be filled in,
2461      we zero the allocated space.  */
2462   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2463   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2464     return FALSE;
2465 
2466   if (reldata->hashes == NULL && reldata->count)
2467     {
2468       struct elf_link_hash_entry **p;
2469 
2470       p = ((struct elf_link_hash_entry **)
2471 	   bfd_zmalloc (reldata->count * sizeof (*p)));
2472       if (p == NULL)
2473 	return FALSE;
2474 
2475       reldata->hashes = p;
2476     }
2477 
2478   return TRUE;
2479 }
2480 
2481 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2482    originated from the section given by INPUT_REL_HDR) to the
2483    OUTPUT_BFD.  */
2484 
2485 bfd_boolean
_bfd_elf_link_output_relocs(bfd * output_bfd,asection * input_section,Elf_Internal_Shdr * input_rel_hdr,Elf_Internal_Rela * internal_relocs,struct elf_link_hash_entry ** rel_hash ATTRIBUTE_UNUSED)2486 _bfd_elf_link_output_relocs (bfd *output_bfd,
2487 			     asection *input_section,
2488 			     Elf_Internal_Shdr *input_rel_hdr,
2489 			     Elf_Internal_Rela *internal_relocs,
2490 			     struct elf_link_hash_entry **rel_hash
2491 			       ATTRIBUTE_UNUSED)
2492 {
2493   Elf_Internal_Rela *irela;
2494   Elf_Internal_Rela *irelaend;
2495   bfd_byte *erel;
2496   struct bfd_elf_section_reloc_data *output_reldata;
2497   asection *output_section;
2498   const struct elf_backend_data *bed;
2499   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2500   struct bfd_elf_section_data *esdo;
2501 
2502   output_section = input_section->output_section;
2503 
2504   bed = get_elf_backend_data (output_bfd);
2505   esdo = elf_section_data (output_section);
2506   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2507     {
2508       output_reldata = &esdo->rel;
2509       swap_out = bed->s->swap_reloc_out;
2510     }
2511   else if (esdo->rela.hdr
2512 	   && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2513     {
2514       output_reldata = &esdo->rela;
2515       swap_out = bed->s->swap_reloca_out;
2516     }
2517   else
2518     {
2519       (*_bfd_error_handler)
2520 	(_("%B: relocation size mismatch in %B section %A"),
2521 	 output_bfd, input_section->owner, input_section);
2522       bfd_set_error (bfd_error_wrong_format);
2523       return FALSE;
2524     }
2525 
2526   erel = output_reldata->hdr->contents;
2527   erel += output_reldata->count * input_rel_hdr->sh_entsize;
2528   irela = internal_relocs;
2529   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2530 		      * bed->s->int_rels_per_ext_rel);
2531   while (irela < irelaend)
2532     {
2533       (*swap_out) (output_bfd, irela, erel);
2534       irela += bed->s->int_rels_per_ext_rel;
2535       erel += input_rel_hdr->sh_entsize;
2536     }
2537 
2538   /* Bump the counter, so that we know where to add the next set of
2539      relocations.  */
2540   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2541 
2542   return TRUE;
2543 }
2544 
2545 /* Make weak undefined symbols in PIE dynamic.  */
2546 
2547 bfd_boolean
_bfd_elf_link_hash_fixup_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h)2548 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2549 				 struct elf_link_hash_entry *h)
2550 {
2551   if (bfd_link_pie (info)
2552       && h->dynindx == -1
2553       && h->root.type == bfd_link_hash_undefweak)
2554     return bfd_elf_link_record_dynamic_symbol (info, h);
2555 
2556   return TRUE;
2557 }
2558 
2559 /* Fix up the flags for a symbol.  This handles various cases which
2560    can only be fixed after all the input files are seen.  This is
2561    currently called by both adjust_dynamic_symbol and
2562    assign_sym_version, which is unnecessary but perhaps more robust in
2563    the face of future changes.  */
2564 
2565 static bfd_boolean
_bfd_elf_fix_symbol_flags(struct elf_link_hash_entry * h,struct elf_info_failed * eif)2566 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2567 			   struct elf_info_failed *eif)
2568 {
2569   const struct elf_backend_data *bed;
2570 
2571   /* If this symbol was mentioned in a non-ELF file, try to set
2572      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
2573      permit a non-ELF file to correctly refer to a symbol defined in
2574      an ELF dynamic object.  */
2575   if (h->non_elf)
2576     {
2577       while (h->root.type == bfd_link_hash_indirect)
2578 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
2579 
2580       if (h->root.type != bfd_link_hash_defined
2581 	  && h->root.type != bfd_link_hash_defweak)
2582 	{
2583 	  h->ref_regular = 1;
2584 	  h->ref_regular_nonweak = 1;
2585 	}
2586       else
2587 	{
2588 	  if (h->root.u.def.section->owner != NULL
2589 	      && (bfd_get_flavour (h->root.u.def.section->owner)
2590 		  == bfd_target_elf_flavour))
2591 	    {
2592 	      h->ref_regular = 1;
2593 	      h->ref_regular_nonweak = 1;
2594 	    }
2595 	  else
2596 	    h->def_regular = 1;
2597 	}
2598 
2599       if (h->dynindx == -1
2600 	  && (h->def_dynamic
2601 	      || h->ref_dynamic))
2602 	{
2603 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2604 	    {
2605 	      eif->failed = TRUE;
2606 	      return FALSE;
2607 	    }
2608 	}
2609     }
2610   else
2611     {
2612       /* Unfortunately, NON_ELF is only correct if the symbol
2613 	 was first seen in a non-ELF file.  Fortunately, if the symbol
2614 	 was first seen in an ELF file, we're probably OK unless the
2615 	 symbol was defined in a non-ELF file.  Catch that case here.
2616 	 FIXME: We're still in trouble if the symbol was first seen in
2617 	 a dynamic object, and then later in a non-ELF regular object.  */
2618       if ((h->root.type == bfd_link_hash_defined
2619 	   || h->root.type == bfd_link_hash_defweak)
2620 	  && !h->def_regular
2621 	  && (h->root.u.def.section->owner != NULL
2622 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
2623 		 != bfd_target_elf_flavour)
2624 	      : (bfd_is_abs_section (h->root.u.def.section)
2625 		 && !h->def_dynamic)))
2626 	h->def_regular = 1;
2627     }
2628 
2629   /* Backend specific symbol fixup.  */
2630   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2631   if (bed->elf_backend_fixup_symbol
2632       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2633     return FALSE;
2634 
2635   /* If this is a final link, and the symbol was defined as a common
2636      symbol in a regular object file, and there was no definition in
2637      any dynamic object, then the linker will have allocated space for
2638      the symbol in a common section but the DEF_REGULAR
2639      flag will not have been set.  */
2640   if (h->root.type == bfd_link_hash_defined
2641       && !h->def_regular
2642       && h->ref_regular
2643       && !h->def_dynamic
2644       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2645     h->def_regular = 1;
2646 
2647   /* If -Bsymbolic was used (which means to bind references to global
2648      symbols to the definition within the shared object), and this
2649      symbol was defined in a regular object, then it actually doesn't
2650      need a PLT entry.  Likewise, if the symbol has non-default
2651      visibility.  If the symbol has hidden or internal visibility, we
2652      will force it local.  */
2653   if (h->needs_plt
2654       && bfd_link_pic (eif->info)
2655       && is_elf_hash_table (eif->info->hash)
2656       && (SYMBOLIC_BIND (eif->info, h)
2657 	  || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2658       && h->def_regular)
2659     {
2660       bfd_boolean force_local;
2661 
2662       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2663 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2664       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2665     }
2666 
2667   /* If a weak undefined symbol has non-default visibility, we also
2668      hide it from the dynamic linker.  */
2669   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2670       && h->root.type == bfd_link_hash_undefweak)
2671     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2672 
2673   /* If this is a weak defined symbol in a dynamic object, and we know
2674      the real definition in the dynamic object, copy interesting flags
2675      over to the real definition.  */
2676   if (h->u.weakdef != NULL)
2677     {
2678       /* If the real definition is defined by a regular object file,
2679 	 don't do anything special.  See the longer description in
2680 	 _bfd_elf_adjust_dynamic_symbol, below.  */
2681       if (h->u.weakdef->def_regular)
2682 	h->u.weakdef = NULL;
2683       else
2684 	{
2685 	  struct elf_link_hash_entry *weakdef = h->u.weakdef;
2686 
2687 	  while (h->root.type == bfd_link_hash_indirect)
2688 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2689 
2690 	  BFD_ASSERT (h->root.type == bfd_link_hash_defined
2691 		      || h->root.type == bfd_link_hash_defweak);
2692 	  BFD_ASSERT (weakdef->def_dynamic);
2693 	  BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2694 		      || weakdef->root.type == bfd_link_hash_defweak);
2695 	  (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2696 	}
2697     }
2698 
2699   return TRUE;
2700 }
2701 
2702 /* Make the backend pick a good value for a dynamic symbol.  This is
2703    called via elf_link_hash_traverse, and also calls itself
2704    recursively.  */
2705 
2706 static bfd_boolean
_bfd_elf_adjust_dynamic_symbol(struct elf_link_hash_entry * h,void * data)2707 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2708 {
2709   struct elf_info_failed *eif = (struct elf_info_failed *) data;
2710   bfd *dynobj;
2711   const struct elf_backend_data *bed;
2712 
2713   if (! is_elf_hash_table (eif->info->hash))
2714     return FALSE;
2715 
2716   /* Ignore indirect symbols.  These are added by the versioning code.  */
2717   if (h->root.type == bfd_link_hash_indirect)
2718     return TRUE;
2719 
2720   /* Fix the symbol flags.  */
2721   if (! _bfd_elf_fix_symbol_flags (h, eif))
2722     return FALSE;
2723 
2724   /* If this symbol does not require a PLT entry, and it is not
2725      defined by a dynamic object, or is not referenced by a regular
2726      object, ignore it.  We do have to handle a weak defined symbol,
2727      even if no regular object refers to it, if we decided to add it
2728      to the dynamic symbol table.  FIXME: Do we normally need to worry
2729      about symbols which are defined by one dynamic object and
2730      referenced by another one?  */
2731   if (!h->needs_plt
2732       && h->type != STT_GNU_IFUNC
2733       && (h->def_regular
2734 	  || !h->def_dynamic
2735 	  || (!h->ref_regular
2736 	      && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2737     {
2738       h->plt = elf_hash_table (eif->info)->init_plt_offset;
2739       return TRUE;
2740     }
2741 
2742   /* If we've already adjusted this symbol, don't do it again.  This
2743      can happen via a recursive call.  */
2744   if (h->dynamic_adjusted)
2745     return TRUE;
2746 
2747   /* Don't look at this symbol again.  Note that we must set this
2748      after checking the above conditions, because we may look at a
2749      symbol once, decide not to do anything, and then get called
2750      recursively later after REF_REGULAR is set below.  */
2751   h->dynamic_adjusted = 1;
2752 
2753   /* If this is a weak definition, and we know a real definition, and
2754      the real symbol is not itself defined by a regular object file,
2755      then get a good value for the real definition.  We handle the
2756      real symbol first, for the convenience of the backend routine.
2757 
2758      Note that there is a confusing case here.  If the real definition
2759      is defined by a regular object file, we don't get the real symbol
2760      from the dynamic object, but we do get the weak symbol.  If the
2761      processor backend uses a COPY reloc, then if some routine in the
2762      dynamic object changes the real symbol, we will not see that
2763      change in the corresponding weak symbol.  This is the way other
2764      ELF linkers work as well, and seems to be a result of the shared
2765      library model.
2766 
2767      I will clarify this issue.  Most SVR4 shared libraries define the
2768      variable _timezone and define timezone as a weak synonym.  The
2769      tzset call changes _timezone.  If you write
2770        extern int timezone;
2771        int _timezone = 5;
2772        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2773      you might expect that, since timezone is a synonym for _timezone,
2774      the same number will print both times.  However, if the processor
2775      backend uses a COPY reloc, then actually timezone will be copied
2776      into your process image, and, since you define _timezone
2777      yourself, _timezone will not.  Thus timezone and _timezone will
2778      wind up at different memory locations.  The tzset call will set
2779      _timezone, leaving timezone unchanged.  */
2780 
2781   if (h->u.weakdef != NULL)
2782     {
2783       /* If we get to this point, there is an implicit reference to
2784 	 H->U.WEAKDEF by a regular object file via the weak symbol H.  */
2785       h->u.weakdef->ref_regular = 1;
2786 
2787       /* Ensure that the backend adjust_dynamic_symbol function sees
2788 	 H->U.WEAKDEF before H by recursively calling ourselves.  */
2789       if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2790 	return FALSE;
2791     }
2792 
2793   /* If a symbol has no type and no size and does not require a PLT
2794      entry, then we are probably about to do the wrong thing here: we
2795      are probably going to create a COPY reloc for an empty object.
2796      This case can arise when a shared object is built with assembly
2797      code, and the assembly code fails to set the symbol type.  */
2798   if (h->size == 0
2799       && h->type == STT_NOTYPE
2800       && !h->needs_plt)
2801     (*_bfd_error_handler)
2802       (_("warning: type and size of dynamic symbol `%s' are not defined"),
2803        h->root.root.string);
2804 
2805   dynobj = elf_hash_table (eif->info)->dynobj;
2806   bed = get_elf_backend_data (dynobj);
2807 
2808   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2809     {
2810       eif->failed = TRUE;
2811       return FALSE;
2812     }
2813 
2814   return TRUE;
2815 }
2816 
2817 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2818    DYNBSS.  */
2819 
2820 bfd_boolean
_bfd_elf_adjust_dynamic_copy(struct bfd_link_info * info,struct elf_link_hash_entry * h,asection * dynbss)2821 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2822 			      struct elf_link_hash_entry *h,
2823 			      asection *dynbss)
2824 {
2825   unsigned int power_of_two;
2826   bfd_vma mask;
2827   asection *sec = h->root.u.def.section;
2828 
2829   /* The section aligment of definition is the maximum alignment
2830      requirement of symbols defined in the section.  Since we don't
2831      know the symbol alignment requirement, we start with the
2832      maximum alignment and check low bits of the symbol address
2833      for the minimum alignment.  */
2834   power_of_two = bfd_get_section_alignment (sec->owner, sec);
2835   mask = ((bfd_vma) 1 << power_of_two) - 1;
2836   while ((h->root.u.def.value & mask) != 0)
2837     {
2838        mask >>= 1;
2839        --power_of_two;
2840     }
2841 
2842   if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2843 						dynbss))
2844     {
2845       /* Adjust the section alignment if needed.  */
2846       if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2847 				       power_of_two))
2848 	return FALSE;
2849     }
2850 
2851   /* We make sure that the symbol will be aligned properly.  */
2852   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2853 
2854   /* Define the symbol as being at this point in DYNBSS.  */
2855   h->root.u.def.section = dynbss;
2856   h->root.u.def.value = dynbss->size;
2857 
2858   /* Increment the size of DYNBSS to make room for the symbol.  */
2859   dynbss->size += h->size;
2860 
2861   /* No error if extern_protected_data is true.  */
2862   if (h->protected_def
2863       && (!info->extern_protected_data
2864 	  || (info->extern_protected_data < 0
2865 	      && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2866     info->callbacks->einfo
2867       (_("%P: copy reloc against protected `%T' is dangerous\n"),
2868        h->root.root.string);
2869 
2870   return TRUE;
2871 }
2872 
2873 /* Adjust all external symbols pointing into SEC_MERGE sections
2874    to reflect the object merging within the sections.  */
2875 
2876 static bfd_boolean
_bfd_elf_link_sec_merge_syms(struct elf_link_hash_entry * h,void * data)2877 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2878 {
2879   asection *sec;
2880 
2881   if ((h->root.type == bfd_link_hash_defined
2882        || h->root.type == bfd_link_hash_defweak)
2883       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2884       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2885     {
2886       bfd *output_bfd = (bfd *) data;
2887 
2888       h->root.u.def.value =
2889 	_bfd_merged_section_offset (output_bfd,
2890 				    &h->root.u.def.section,
2891 				    elf_section_data (sec)->sec_info,
2892 				    h->root.u.def.value);
2893     }
2894 
2895   return TRUE;
2896 }
2897 
2898 /* Returns false if the symbol referred to by H should be considered
2899    to resolve local to the current module, and true if it should be
2900    considered to bind dynamically.  */
2901 
2902 bfd_boolean
_bfd_elf_dynamic_symbol_p(struct elf_link_hash_entry * h,struct bfd_link_info * info,bfd_boolean not_local_protected)2903 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2904 			   struct bfd_link_info *info,
2905 			   bfd_boolean not_local_protected)
2906 {
2907   bfd_boolean binding_stays_local_p;
2908   const struct elf_backend_data *bed;
2909   struct elf_link_hash_table *hash_table;
2910 
2911   if (h == NULL)
2912     return FALSE;
2913 
2914   while (h->root.type == bfd_link_hash_indirect
2915 	 || h->root.type == bfd_link_hash_warning)
2916     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2917 
2918   /* If it was forced local, then clearly it's not dynamic.  */
2919   if (h->dynindx == -1)
2920     return FALSE;
2921   if (h->forced_local)
2922     return FALSE;
2923 
2924   /* Identify the cases where name binding rules say that a
2925      visible symbol resolves locally.  */
2926   binding_stays_local_p = (bfd_link_executable (info)
2927 			   || SYMBOLIC_BIND (info, h));
2928 
2929   switch (ELF_ST_VISIBILITY (h->other))
2930     {
2931     case STV_INTERNAL:
2932     case STV_HIDDEN:
2933       return FALSE;
2934 
2935     case STV_PROTECTED:
2936       hash_table = elf_hash_table (info);
2937       if (!is_elf_hash_table (hash_table))
2938 	return FALSE;
2939 
2940       bed = get_elf_backend_data (hash_table->dynobj);
2941 
2942       /* Proper resolution for function pointer equality may require
2943 	 that these symbols perhaps be resolved dynamically, even though
2944 	 we should be resolving them to the current module.  */
2945       if (!not_local_protected || !bed->is_function_type (h->type))
2946 	binding_stays_local_p = TRUE;
2947       break;
2948 
2949     default:
2950       break;
2951     }
2952 
2953   /* If it isn't defined locally, then clearly it's dynamic.  */
2954   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2955     return TRUE;
2956 
2957   /* Otherwise, the symbol is dynamic if binding rules don't tell
2958      us that it remains local.  */
2959   return !binding_stays_local_p;
2960 }
2961 
2962 /* Return true if the symbol referred to by H should be considered
2963    to resolve local to the current module, and false otherwise.  Differs
2964    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2965    undefined symbols.  The two functions are virtually identical except
2966    for the place where forced_local and dynindx == -1 are tested.  If
2967    either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2968    the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2969    the symbol is local only for defined symbols.
2970    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2971    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2972    treatment of undefined weak symbols.  For those that do not make
2973    undefined weak symbols dynamic, both functions may return false.  */
2974 
2975 bfd_boolean
_bfd_elf_symbol_refs_local_p(struct elf_link_hash_entry * h,struct bfd_link_info * info,bfd_boolean local_protected)2976 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2977 			      struct bfd_link_info *info,
2978 			      bfd_boolean local_protected)
2979 {
2980   const struct elf_backend_data *bed;
2981   struct elf_link_hash_table *hash_table;
2982 
2983   /* If it's a local sym, of course we resolve locally.  */
2984   if (h == NULL)
2985     return TRUE;
2986 
2987   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
2988   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2989       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2990     return TRUE;
2991 
2992   /* Common symbols that become definitions don't get the DEF_REGULAR
2993      flag set, so test it first, and don't bail out.  */
2994   if (ELF_COMMON_DEF_P (h))
2995     /* Do nothing.  */;
2996   /* If we don't have a definition in a regular file, then we can't
2997      resolve locally.  The sym is either undefined or dynamic.  */
2998   else if (!h->def_regular)
2999     return FALSE;
3000 
3001   /* Forced local symbols resolve locally.  */
3002   if (h->forced_local)
3003     return TRUE;
3004 
3005   /* As do non-dynamic symbols.  */
3006   if (h->dynindx == -1)
3007     return TRUE;
3008 
3009   /* At this point, we know the symbol is defined and dynamic.  In an
3010      executable it must resolve locally, likewise when building symbolic
3011      shared libraries.  */
3012   if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3013     return TRUE;
3014 
3015   /* Now deal with defined dynamic symbols in shared libraries.  Ones
3016      with default visibility might not resolve locally.  */
3017   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3018     return FALSE;
3019 
3020   hash_table = elf_hash_table (info);
3021   if (!is_elf_hash_table (hash_table))
3022     return TRUE;
3023 
3024   bed = get_elf_backend_data (hash_table->dynobj);
3025 
3026   /* If extern_protected_data is false, STV_PROTECTED non-function
3027      symbols are local.  */
3028   if ((!info->extern_protected_data
3029        || (info->extern_protected_data < 0
3030 	   && !bed->extern_protected_data))
3031       && !bed->is_function_type (h->type))
3032     return TRUE;
3033 
3034   /* Function pointer equality tests may require that STV_PROTECTED
3035      symbols be treated as dynamic symbols.  If the address of a
3036      function not defined in an executable is set to that function's
3037      plt entry in the executable, then the address of the function in
3038      a shared library must also be the plt entry in the executable.  */
3039   return local_protected;
3040 }
3041 
3042 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3043    aligned.  Returns the first TLS output section.  */
3044 
3045 struct bfd_section *
_bfd_elf_tls_setup(bfd * obfd,struct bfd_link_info * info)3046 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3047 {
3048   struct bfd_section *sec, *tls;
3049   unsigned int align = 0;
3050 
3051   for (sec = obfd->sections; sec != NULL; sec = sec->next)
3052     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3053       break;
3054   tls = sec;
3055 
3056   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3057     if (sec->alignment_power > align)
3058       align = sec->alignment_power;
3059 
3060   elf_hash_table (info)->tls_sec = tls;
3061 
3062   /* Ensure the alignment of the first section is the largest alignment,
3063      so that the tls segment starts aligned.  */
3064   if (tls != NULL)
3065     tls->alignment_power = align;
3066 
3067   return tls;
3068 }
3069 
3070 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3071 static bfd_boolean
is_global_data_symbol_definition(bfd * abfd ATTRIBUTE_UNUSED,Elf_Internal_Sym * sym)3072 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3073 				  Elf_Internal_Sym *sym)
3074 {
3075   const struct elf_backend_data *bed;
3076 
3077   /* Local symbols do not count, but target specific ones might.  */
3078   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3079       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3080     return FALSE;
3081 
3082   bed = get_elf_backend_data (abfd);
3083   /* Function symbols do not count.  */
3084   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3085     return FALSE;
3086 
3087   /* If the section is undefined, then so is the symbol.  */
3088   if (sym->st_shndx == SHN_UNDEF)
3089     return FALSE;
3090 
3091   /* If the symbol is defined in the common section, then
3092      it is a common definition and so does not count.  */
3093   if (bed->common_definition (sym))
3094     return FALSE;
3095 
3096   /* If the symbol is in a target specific section then we
3097      must rely upon the backend to tell us what it is.  */
3098   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3099     /* FIXME - this function is not coded yet:
3100 
3101        return _bfd_is_global_symbol_definition (abfd, sym);
3102 
3103        Instead for now assume that the definition is not global,
3104        Even if this is wrong, at least the linker will behave
3105        in the same way that it used to do.  */
3106     return FALSE;
3107 
3108   return TRUE;
3109 }
3110 
3111 /* Search the symbol table of the archive element of the archive ABFD
3112    whose archive map contains a mention of SYMDEF, and determine if
3113    the symbol is defined in this element.  */
3114 static bfd_boolean
elf_link_is_defined_archive_symbol(bfd * abfd,carsym * symdef)3115 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3116 {
3117   Elf_Internal_Shdr * hdr;
3118   size_t symcount;
3119   size_t extsymcount;
3120   size_t extsymoff;
3121   Elf_Internal_Sym *isymbuf;
3122   Elf_Internal_Sym *isym;
3123   Elf_Internal_Sym *isymend;
3124   bfd_boolean result;
3125 
3126   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3127   if (abfd == NULL)
3128     return FALSE;
3129 
3130   if (! bfd_check_format (abfd, bfd_object))
3131     return FALSE;
3132 
3133   /* Select the appropriate symbol table.  If we don't know if the
3134      object file is an IR object, give linker LTO plugin a chance to
3135      get the correct symbol table.  */
3136   if (abfd->plugin_format == bfd_plugin_yes
3137 #if BFD_SUPPORTS_PLUGINS
3138       || (abfd->plugin_format == bfd_plugin_unknown
3139 	  && bfd_link_plugin_object_p (abfd))
3140 #endif
3141       )
3142     {
3143       /* Use the IR symbol table if the object has been claimed by
3144 	 plugin.  */
3145       abfd = abfd->plugin_dummy_bfd;
3146       hdr = &elf_tdata (abfd)->symtab_hdr;
3147     }
3148   else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3149     hdr = &elf_tdata (abfd)->symtab_hdr;
3150   else
3151     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3152 
3153   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3154 
3155   /* The sh_info field of the symtab header tells us where the
3156      external symbols start.  We don't care about the local symbols.  */
3157   if (elf_bad_symtab (abfd))
3158     {
3159       extsymcount = symcount;
3160       extsymoff = 0;
3161     }
3162   else
3163     {
3164       extsymcount = symcount - hdr->sh_info;
3165       extsymoff = hdr->sh_info;
3166     }
3167 
3168   if (extsymcount == 0)
3169     return FALSE;
3170 
3171   /* Read in the symbol table.  */
3172   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3173 				  NULL, NULL, NULL);
3174   if (isymbuf == NULL)
3175     return FALSE;
3176 
3177   /* Scan the symbol table looking for SYMDEF.  */
3178   result = FALSE;
3179   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3180     {
3181       const char *name;
3182 
3183       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3184 					      isym->st_name);
3185       if (name == NULL)
3186 	break;
3187 
3188       if (strcmp (name, symdef->name) == 0)
3189 	{
3190 	  result = is_global_data_symbol_definition (abfd, isym);
3191 	  break;
3192 	}
3193     }
3194 
3195   free (isymbuf);
3196 
3197   return result;
3198 }
3199 
3200 /* Add an entry to the .dynamic table.  */
3201 
3202 bfd_boolean
_bfd_elf_add_dynamic_entry(struct bfd_link_info * info,bfd_vma tag,bfd_vma val)3203 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3204 			    bfd_vma tag,
3205 			    bfd_vma val)
3206 {
3207   struct elf_link_hash_table *hash_table;
3208   const struct elf_backend_data *bed;
3209   asection *s;
3210   bfd_size_type newsize;
3211   bfd_byte *newcontents;
3212   Elf_Internal_Dyn dyn;
3213 
3214   hash_table = elf_hash_table (info);
3215   if (! is_elf_hash_table (hash_table))
3216     return FALSE;
3217 
3218   bed = get_elf_backend_data (hash_table->dynobj);
3219   s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3220   BFD_ASSERT (s != NULL);
3221 
3222   newsize = s->size + bed->s->sizeof_dyn;
3223   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3224   if (newcontents == NULL)
3225     return FALSE;
3226 
3227   dyn.d_tag = tag;
3228   dyn.d_un.d_val = val;
3229   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3230 
3231   s->size = newsize;
3232   s->contents = newcontents;
3233 
3234   return TRUE;
3235 }
3236 
3237 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3238    otherwise just check whether one already exists.  Returns -1 on error,
3239    1 if a DT_NEEDED tag already exists, and 0 on success.  */
3240 
3241 static int
elf_add_dt_needed_tag(bfd * abfd,struct bfd_link_info * info,const char * soname,bfd_boolean do_it)3242 elf_add_dt_needed_tag (bfd *abfd,
3243 		       struct bfd_link_info *info,
3244 		       const char *soname,
3245 		       bfd_boolean do_it)
3246 {
3247   struct elf_link_hash_table *hash_table;
3248   size_t strindex;
3249 
3250   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3251     return -1;
3252 
3253   hash_table = elf_hash_table (info);
3254   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3255   if (strindex == (size_t) -1)
3256     return -1;
3257 
3258   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3259     {
3260       asection *sdyn;
3261       const struct elf_backend_data *bed;
3262       bfd_byte *extdyn;
3263 
3264       bed = get_elf_backend_data (hash_table->dynobj);
3265       sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3266       if (sdyn != NULL)
3267 	for (extdyn = sdyn->contents;
3268 	     extdyn < sdyn->contents + sdyn->size;
3269 	     extdyn += bed->s->sizeof_dyn)
3270 	  {
3271 	    Elf_Internal_Dyn dyn;
3272 
3273 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3274 	    if (dyn.d_tag == DT_NEEDED
3275 		&& dyn.d_un.d_val == strindex)
3276 	      {
3277 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3278 		return 1;
3279 	      }
3280 	  }
3281     }
3282 
3283   if (do_it)
3284     {
3285       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3286 	return -1;
3287 
3288       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3289 	return -1;
3290     }
3291   else
3292     /* We were just checking for existence of the tag.  */
3293     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3294 
3295   return 0;
3296 }
3297 
3298 /* Return true if SONAME is on the needed list between NEEDED and STOP
3299    (or the end of list if STOP is NULL), and needed by a library that
3300    will be loaded.  */
3301 
3302 static bfd_boolean
on_needed_list(const char * soname,struct bfd_link_needed_list * needed,struct bfd_link_needed_list * stop)3303 on_needed_list (const char *soname,
3304 		struct bfd_link_needed_list *needed,
3305 		struct bfd_link_needed_list *stop)
3306 {
3307   struct bfd_link_needed_list *look;
3308   for (look = needed; look != stop; look = look->next)
3309     if (strcmp (soname, look->name) == 0
3310 	&& ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3311 	    /* If needed by a library that itself is not directly
3312 	       needed, recursively check whether that library is
3313 	       indirectly needed.  Since we add DT_NEEDED entries to
3314 	       the end of the list, library dependencies appear after
3315 	       the library.  Therefore search prior to the current
3316 	       LOOK, preventing possible infinite recursion.  */
3317 	    || on_needed_list (elf_dt_name (look->by), needed, look)))
3318       return TRUE;
3319 
3320   return FALSE;
3321 }
3322 
3323 /* Sort symbol by value, section, and size.  */
3324 static int
elf_sort_symbol(const void * arg1,const void * arg2)3325 elf_sort_symbol (const void *arg1, const void *arg2)
3326 {
3327   const struct elf_link_hash_entry *h1;
3328   const struct elf_link_hash_entry *h2;
3329   bfd_signed_vma vdiff;
3330 
3331   h1 = *(const struct elf_link_hash_entry **) arg1;
3332   h2 = *(const struct elf_link_hash_entry **) arg2;
3333   vdiff = h1->root.u.def.value - h2->root.u.def.value;
3334   if (vdiff != 0)
3335     return vdiff > 0 ? 1 : -1;
3336   else
3337     {
3338       int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3339       if (sdiff != 0)
3340 	return sdiff > 0 ? 1 : -1;
3341     }
3342   vdiff = h1->size - h2->size;
3343   return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3344 }
3345 
3346 /* This function is used to adjust offsets into .dynstr for
3347    dynamic symbols.  This is called via elf_link_hash_traverse.  */
3348 
3349 static bfd_boolean
elf_adjust_dynstr_offsets(struct elf_link_hash_entry * h,void * data)3350 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3351 {
3352   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3353 
3354   if (h->dynindx != -1)
3355     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3356   return TRUE;
3357 }
3358 
3359 /* Assign string offsets in .dynstr, update all structures referencing
3360    them.  */
3361 
3362 static bfd_boolean
elf_finalize_dynstr(bfd * output_bfd,struct bfd_link_info * info)3363 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3364 {
3365   struct elf_link_hash_table *hash_table = elf_hash_table (info);
3366   struct elf_link_local_dynamic_entry *entry;
3367   struct elf_strtab_hash *dynstr = hash_table->dynstr;
3368   bfd *dynobj = hash_table->dynobj;
3369   asection *sdyn;
3370   bfd_size_type size;
3371   const struct elf_backend_data *bed;
3372   bfd_byte *extdyn;
3373 
3374   _bfd_elf_strtab_finalize (dynstr);
3375   size = _bfd_elf_strtab_size (dynstr);
3376 
3377   bed = get_elf_backend_data (dynobj);
3378   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3379   BFD_ASSERT (sdyn != NULL);
3380 
3381   /* Update all .dynamic entries referencing .dynstr strings.  */
3382   for (extdyn = sdyn->contents;
3383        extdyn < sdyn->contents + sdyn->size;
3384        extdyn += bed->s->sizeof_dyn)
3385     {
3386       Elf_Internal_Dyn dyn;
3387 
3388       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3389       switch (dyn.d_tag)
3390 	{
3391 	case DT_STRSZ:
3392 	  dyn.d_un.d_val = size;
3393 	  break;
3394 	case DT_NEEDED:
3395 	case DT_SONAME:
3396 	case DT_RPATH:
3397 	case DT_RUNPATH:
3398 	case DT_FILTER:
3399 	case DT_AUXILIARY:
3400 	case DT_AUDIT:
3401 	case DT_DEPAUDIT:
3402 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3403 	  break;
3404 	default:
3405 	  continue;
3406 	}
3407       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3408     }
3409 
3410   /* Now update local dynamic symbols.  */
3411   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3412     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3413 						  entry->isym.st_name);
3414 
3415   /* And the rest of dynamic symbols.  */
3416   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3417 
3418   /* Adjust version definitions.  */
3419   if (elf_tdata (output_bfd)->cverdefs)
3420     {
3421       asection *s;
3422       bfd_byte *p;
3423       size_t i;
3424       Elf_Internal_Verdef def;
3425       Elf_Internal_Verdaux defaux;
3426 
3427       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3428       p = s->contents;
3429       do
3430 	{
3431 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3432 				   &def);
3433 	  p += sizeof (Elf_External_Verdef);
3434 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
3435 	    continue;
3436 	  for (i = 0; i < def.vd_cnt; ++i)
3437 	    {
3438 	      _bfd_elf_swap_verdaux_in (output_bfd,
3439 					(Elf_External_Verdaux *) p, &defaux);
3440 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3441 							defaux.vda_name);
3442 	      _bfd_elf_swap_verdaux_out (output_bfd,
3443 					 &defaux, (Elf_External_Verdaux *) p);
3444 	      p += sizeof (Elf_External_Verdaux);
3445 	    }
3446 	}
3447       while (def.vd_next);
3448     }
3449 
3450   /* Adjust version references.  */
3451   if (elf_tdata (output_bfd)->verref)
3452     {
3453       asection *s;
3454       bfd_byte *p;
3455       size_t i;
3456       Elf_Internal_Verneed need;
3457       Elf_Internal_Vernaux needaux;
3458 
3459       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3460       p = s->contents;
3461       do
3462 	{
3463 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3464 				    &need);
3465 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3466 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
3467 				     (Elf_External_Verneed *) p);
3468 	  p += sizeof (Elf_External_Verneed);
3469 	  for (i = 0; i < need.vn_cnt; ++i)
3470 	    {
3471 	      _bfd_elf_swap_vernaux_in (output_bfd,
3472 					(Elf_External_Vernaux *) p, &needaux);
3473 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3474 							 needaux.vna_name);
3475 	      _bfd_elf_swap_vernaux_out (output_bfd,
3476 					 &needaux,
3477 					 (Elf_External_Vernaux *) p);
3478 	      p += sizeof (Elf_External_Vernaux);
3479 	    }
3480 	}
3481       while (need.vn_next);
3482     }
3483 
3484   return TRUE;
3485 }
3486 
3487 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3488    The default is to only match when the INPUT and OUTPUT are exactly
3489    the same target.  */
3490 
3491 bfd_boolean
_bfd_elf_default_relocs_compatible(const bfd_target * input,const bfd_target * output)3492 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3493 				    const bfd_target *output)
3494 {
3495   return input == output;
3496 }
3497 
3498 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3499    This version is used when different targets for the same architecture
3500    are virtually identical.  */
3501 
3502 bfd_boolean
_bfd_elf_relocs_compatible(const bfd_target * input,const bfd_target * output)3503 _bfd_elf_relocs_compatible (const bfd_target *input,
3504 			    const bfd_target *output)
3505 {
3506   const struct elf_backend_data *obed, *ibed;
3507 
3508   if (input == output)
3509     return TRUE;
3510 
3511   ibed = xvec_get_elf_backend_data (input);
3512   obed = xvec_get_elf_backend_data (output);
3513 
3514   if (ibed->arch != obed->arch)
3515     return FALSE;
3516 
3517   /* If both backends are using this function, deem them compatible.  */
3518   return ibed->relocs_compatible == obed->relocs_compatible;
3519 }
3520 
3521 /* Make a special call to the linker "notice" function to tell it that
3522    we are about to handle an as-needed lib, or have finished
3523    processing the lib.  */
3524 
3525 bfd_boolean
_bfd_elf_notice_as_needed(bfd * ibfd,struct bfd_link_info * info,enum notice_asneeded_action act)3526 _bfd_elf_notice_as_needed (bfd *ibfd,
3527 			   struct bfd_link_info *info,
3528 			   enum notice_asneeded_action act)
3529 {
3530   return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3531 }
3532 
3533 /* Check relocations an ELF object file.  */
3534 
3535 bfd_boolean
_bfd_elf_link_check_relocs(bfd * abfd,struct bfd_link_info * info)3536 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3537 {
3538   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3539   struct elf_link_hash_table *htab = elf_hash_table (info);
3540 
3541   /* If this object is the same format as the output object, and it is
3542      not a shared library, then let the backend look through the
3543      relocs.
3544 
3545      This is required to build global offset table entries and to
3546      arrange for dynamic relocs.  It is not required for the
3547      particular common case of linking non PIC code, even when linking
3548      against shared libraries, but unfortunately there is no way of
3549      knowing whether an object file has been compiled PIC or not.
3550      Looking through the relocs is not particularly time consuming.
3551      The problem is that we must either (1) keep the relocs in memory,
3552      which causes the linker to require additional runtime memory or
3553      (2) read the relocs twice from the input file, which wastes time.
3554      This would be a good case for using mmap.
3555 
3556      I have no idea how to handle linking PIC code into a file of a
3557      different format.  It probably can't be done.  */
3558   if ((abfd->flags & DYNAMIC) == 0
3559       && is_elf_hash_table (htab)
3560       && bed->check_relocs != NULL
3561       && elf_object_id (abfd) == elf_hash_table_id (htab)
3562       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3563     {
3564       asection *o;
3565 
3566       for (o = abfd->sections; o != NULL; o = o->next)
3567 	{
3568 	  Elf_Internal_Rela *internal_relocs;
3569 	  bfd_boolean ok;
3570 
3571 	  /* Don't check relocations in excluded sections.  */
3572 	  if ((o->flags & SEC_RELOC) == 0
3573 	      || (o->flags & SEC_EXCLUDE) != 0
3574 	      || o->reloc_count == 0
3575 	      || ((info->strip == strip_all || info->strip == strip_debugger)
3576 		  && (o->flags & SEC_DEBUGGING) != 0)
3577 	      || bfd_is_abs_section (o->output_section))
3578 	    continue;
3579 
3580 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3581 						       info->keep_memory);
3582 	  if (internal_relocs == NULL)
3583 	    return FALSE;
3584 
3585 	  ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3586 
3587 	  if (elf_section_data (o)->relocs != internal_relocs)
3588 	    free (internal_relocs);
3589 
3590 	  if (! ok)
3591 	    return FALSE;
3592 	}
3593     }
3594 
3595   return TRUE;
3596 }
3597 
3598 /* Add symbols from an ELF object file to the linker hash table.  */
3599 
3600 static bfd_boolean
elf_link_add_object_symbols(bfd * abfd,struct bfd_link_info * info)3601 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3602 {
3603   Elf_Internal_Ehdr *ehdr;
3604   Elf_Internal_Shdr *hdr;
3605   size_t symcount;
3606   size_t extsymcount;
3607   size_t extsymoff;
3608   struct elf_link_hash_entry **sym_hash;
3609   bfd_boolean dynamic;
3610   Elf_External_Versym *extversym = NULL;
3611   Elf_External_Versym *ever;
3612   struct elf_link_hash_entry *weaks;
3613   struct elf_link_hash_entry **nondeflt_vers = NULL;
3614   size_t nondeflt_vers_cnt = 0;
3615   Elf_Internal_Sym *isymbuf = NULL;
3616   Elf_Internal_Sym *isym;
3617   Elf_Internal_Sym *isymend;
3618   const struct elf_backend_data *bed;
3619   bfd_boolean add_needed;
3620   struct elf_link_hash_table *htab;
3621   bfd_size_type amt;
3622   void *alloc_mark = NULL;
3623   struct bfd_hash_entry **old_table = NULL;
3624   unsigned int old_size = 0;
3625   unsigned int old_count = 0;
3626   void *old_tab = NULL;
3627   void *old_ent;
3628   struct bfd_link_hash_entry *old_undefs = NULL;
3629   struct bfd_link_hash_entry *old_undefs_tail = NULL;
3630   void *old_strtab = NULL;
3631   size_t tabsize = 0;
3632   asection *s;
3633   bfd_boolean just_syms;
3634 
3635   htab = elf_hash_table (info);
3636   bed = get_elf_backend_data (abfd);
3637 
3638   if ((abfd->flags & DYNAMIC) == 0)
3639     dynamic = FALSE;
3640   else
3641     {
3642       dynamic = TRUE;
3643 
3644       /* You can't use -r against a dynamic object.  Also, there's no
3645 	 hope of using a dynamic object which does not exactly match
3646 	 the format of the output file.  */
3647       if (bfd_link_relocatable (info)
3648 	  || !is_elf_hash_table (htab)
3649 	  || info->output_bfd->xvec != abfd->xvec)
3650 	{
3651 	  if (bfd_link_relocatable (info))
3652 	    bfd_set_error (bfd_error_invalid_operation);
3653 	  else
3654 	    bfd_set_error (bfd_error_wrong_format);
3655 	  goto error_return;
3656 	}
3657     }
3658 
3659   ehdr = elf_elfheader (abfd);
3660   if (info->warn_alternate_em
3661       && bed->elf_machine_code != ehdr->e_machine
3662       && ((bed->elf_machine_alt1 != 0
3663 	   && ehdr->e_machine == bed->elf_machine_alt1)
3664 	  || (bed->elf_machine_alt2 != 0
3665 	      && ehdr->e_machine == bed->elf_machine_alt2)))
3666     info->callbacks->einfo
3667       (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3668        ehdr->e_machine, abfd, bed->elf_machine_code);
3669 
3670   /* As a GNU extension, any input sections which are named
3671      .gnu.warning.SYMBOL are treated as warning symbols for the given
3672      symbol.  This differs from .gnu.warning sections, which generate
3673      warnings when they are included in an output file.  */
3674   /* PR 12761: Also generate this warning when building shared libraries.  */
3675   for (s = abfd->sections; s != NULL; s = s->next)
3676     {
3677       const char *name;
3678 
3679       name = bfd_get_section_name (abfd, s);
3680       if (CONST_STRNEQ (name, ".gnu.warning."))
3681 	{
3682 	  char *msg;
3683 	  bfd_size_type sz;
3684 
3685 	  name += sizeof ".gnu.warning." - 1;
3686 
3687 	  /* If this is a shared object, then look up the symbol
3688 	     in the hash table.  If it is there, and it is already
3689 	     been defined, then we will not be using the entry
3690 	     from this shared object, so we don't need to warn.
3691 	     FIXME: If we see the definition in a regular object
3692 	     later on, we will warn, but we shouldn't.  The only
3693 	     fix is to keep track of what warnings we are supposed
3694 	     to emit, and then handle them all at the end of the
3695 	     link.  */
3696 	  if (dynamic)
3697 	    {
3698 	      struct elf_link_hash_entry *h;
3699 
3700 	      h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3701 
3702 	      /* FIXME: What about bfd_link_hash_common?  */
3703 	      if (h != NULL
3704 		  && (h->root.type == bfd_link_hash_defined
3705 		      || h->root.type == bfd_link_hash_defweak))
3706 		continue;
3707 	    }
3708 
3709 	  sz = s->size;
3710 	  msg = (char *) bfd_alloc (abfd, sz + 1);
3711 	  if (msg == NULL)
3712 	    goto error_return;
3713 
3714 	  if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3715 	    goto error_return;
3716 
3717 	  msg[sz] = '\0';
3718 
3719 	  if (! (_bfd_generic_link_add_one_symbol
3720 		 (info, abfd, name, BSF_WARNING, s, 0, msg,
3721 		  FALSE, bed->collect, NULL)))
3722 	    goto error_return;
3723 
3724 	  if (bfd_link_executable (info))
3725 	    {
3726 	      /* Clobber the section size so that the warning does
3727 		 not get copied into the output file.  */
3728 	      s->size = 0;
3729 
3730 	      /* Also set SEC_EXCLUDE, so that symbols defined in
3731 		 the warning section don't get copied to the output.  */
3732 	      s->flags |= SEC_EXCLUDE;
3733 	    }
3734 	}
3735     }
3736 
3737   just_syms = ((s = abfd->sections) != NULL
3738 	       && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3739 
3740   add_needed = TRUE;
3741   if (! dynamic)
3742     {
3743       /* If we are creating a shared library, create all the dynamic
3744 	 sections immediately.  We need to attach them to something,
3745 	 so we attach them to this BFD, provided it is the right
3746 	 format and is not from ld --just-symbols.  Always create the
3747 	 dynamic sections for -E/--dynamic-list.  FIXME: If there
3748 	 are no input BFD's of the same format as the output, we can't
3749 	 make a shared library.  */
3750       if (!just_syms
3751 	  && (bfd_link_pic (info)
3752 	      || (!bfd_link_relocatable (info)
3753 		  && (info->export_dynamic || info->dynamic)))
3754 	  && is_elf_hash_table (htab)
3755 	  && info->output_bfd->xvec == abfd->xvec
3756 	  && !htab->dynamic_sections_created)
3757 	{
3758 	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3759 	    goto error_return;
3760 	}
3761     }
3762   else if (!is_elf_hash_table (htab))
3763     goto error_return;
3764   else
3765     {
3766       const char *soname = NULL;
3767       char *audit = NULL;
3768       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3769       int ret;
3770 
3771       /* ld --just-symbols and dynamic objects don't mix very well.
3772 	 ld shouldn't allow it.  */
3773       if (just_syms)
3774 	abort ();
3775 
3776       /* If this dynamic lib was specified on the command line with
3777 	 --as-needed in effect, then we don't want to add a DT_NEEDED
3778 	 tag unless the lib is actually used.  Similary for libs brought
3779 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
3780 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3781 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
3782 	 all.  */
3783       add_needed = (elf_dyn_lib_class (abfd)
3784 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
3785 		       | DYN_NO_NEEDED)) == 0;
3786 
3787       s = bfd_get_section_by_name (abfd, ".dynamic");
3788       if (s != NULL)
3789 	{
3790 	  bfd_byte *dynbuf;
3791 	  bfd_byte *extdyn;
3792 	  unsigned int elfsec;
3793 	  unsigned long shlink;
3794 
3795 	  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3796 	    {
3797 error_free_dyn:
3798 	      free (dynbuf);
3799 	      goto error_return;
3800 	    }
3801 
3802 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3803 	  if (elfsec == SHN_BAD)
3804 	    goto error_free_dyn;
3805 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3806 
3807 	  for (extdyn = dynbuf;
3808 	       extdyn < dynbuf + s->size;
3809 	       extdyn += bed->s->sizeof_dyn)
3810 	    {
3811 	      Elf_Internal_Dyn dyn;
3812 
3813 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3814 	      if (dyn.d_tag == DT_SONAME)
3815 		{
3816 		  unsigned int tagv = dyn.d_un.d_val;
3817 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3818 		  if (soname == NULL)
3819 		    goto error_free_dyn;
3820 		}
3821 	      if (dyn.d_tag == DT_NEEDED)
3822 		{
3823 		  struct bfd_link_needed_list *n, **pn;
3824 		  char *fnm, *anm;
3825 		  unsigned int tagv = dyn.d_un.d_val;
3826 
3827 		  amt = sizeof (struct bfd_link_needed_list);
3828 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3829 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3830 		  if (n == NULL || fnm == NULL)
3831 		    goto error_free_dyn;
3832 		  amt = strlen (fnm) + 1;
3833 		  anm = (char *) bfd_alloc (abfd, amt);
3834 		  if (anm == NULL)
3835 		    goto error_free_dyn;
3836 		  memcpy (anm, fnm, amt);
3837 		  n->name = anm;
3838 		  n->by = abfd;
3839 		  n->next = NULL;
3840 		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3841 		    ;
3842 		  *pn = n;
3843 		}
3844 	      if (dyn.d_tag == DT_RUNPATH)
3845 		{
3846 		  struct bfd_link_needed_list *n, **pn;
3847 		  char *fnm, *anm;
3848 		  unsigned int tagv = dyn.d_un.d_val;
3849 
3850 		  amt = sizeof (struct bfd_link_needed_list);
3851 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3852 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3853 		  if (n == NULL || fnm == NULL)
3854 		    goto error_free_dyn;
3855 		  amt = strlen (fnm) + 1;
3856 		  anm = (char *) bfd_alloc (abfd, amt);
3857 		  if (anm == NULL)
3858 		    goto error_free_dyn;
3859 		  memcpy (anm, fnm, amt);
3860 		  n->name = anm;
3861 		  n->by = abfd;
3862 		  n->next = NULL;
3863 		  for (pn = & runpath;
3864 		       *pn != NULL;
3865 		       pn = &(*pn)->next)
3866 		    ;
3867 		  *pn = n;
3868 		}
3869 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
3870 	      if (!runpath && dyn.d_tag == DT_RPATH)
3871 		{
3872 		  struct bfd_link_needed_list *n, **pn;
3873 		  char *fnm, *anm;
3874 		  unsigned int tagv = dyn.d_un.d_val;
3875 
3876 		  amt = sizeof (struct bfd_link_needed_list);
3877 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3878 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3879 		  if (n == NULL || fnm == NULL)
3880 		    goto error_free_dyn;
3881 		  amt = strlen (fnm) + 1;
3882 		  anm = (char *) bfd_alloc (abfd, amt);
3883 		  if (anm == NULL)
3884 		    goto error_free_dyn;
3885 		  memcpy (anm, fnm, amt);
3886 		  n->name = anm;
3887 		  n->by = abfd;
3888 		  n->next = NULL;
3889 		  for (pn = & rpath;
3890 		       *pn != NULL;
3891 		       pn = &(*pn)->next)
3892 		    ;
3893 		  *pn = n;
3894 		}
3895 	      if (dyn.d_tag == DT_AUDIT)
3896 		{
3897 		  unsigned int tagv = dyn.d_un.d_val;
3898 		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3899 		}
3900 	    }
3901 
3902 	  free (dynbuf);
3903 	}
3904 
3905       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
3906 	 frees all more recently bfd_alloc'd blocks as well.  */
3907       if (runpath)
3908 	rpath = runpath;
3909 
3910       if (rpath)
3911 	{
3912 	  struct bfd_link_needed_list **pn;
3913 	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3914 	    ;
3915 	  *pn = rpath;
3916 	}
3917 
3918       /* We do not want to include any of the sections in a dynamic
3919 	 object in the output file.  We hack by simply clobbering the
3920 	 list of sections in the BFD.  This could be handled more
3921 	 cleanly by, say, a new section flag; the existing
3922 	 SEC_NEVER_LOAD flag is not the one we want, because that one
3923 	 still implies that the section takes up space in the output
3924 	 file.  */
3925       bfd_section_list_clear (abfd);
3926 
3927       /* Find the name to use in a DT_NEEDED entry that refers to this
3928 	 object.  If the object has a DT_SONAME entry, we use it.
3929 	 Otherwise, if the generic linker stuck something in
3930 	 elf_dt_name, we use that.  Otherwise, we just use the file
3931 	 name.  */
3932       if (soname == NULL || *soname == '\0')
3933 	{
3934 	  soname = elf_dt_name (abfd);
3935 	  if (soname == NULL || *soname == '\0')
3936 	    soname = bfd_get_filename (abfd);
3937 	}
3938 
3939       /* Save the SONAME because sometimes the linker emulation code
3940 	 will need to know it.  */
3941       elf_dt_name (abfd) = soname;
3942 
3943       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3944       if (ret < 0)
3945 	goto error_return;
3946 
3947       /* If we have already included this dynamic object in the
3948 	 link, just ignore it.  There is no reason to include a
3949 	 particular dynamic object more than once.  */
3950       if (ret > 0)
3951 	return TRUE;
3952 
3953       /* Save the DT_AUDIT entry for the linker emulation code. */
3954       elf_dt_audit (abfd) = audit;
3955     }
3956 
3957   /* If this is a dynamic object, we always link against the .dynsym
3958      symbol table, not the .symtab symbol table.  The dynamic linker
3959      will only see the .dynsym symbol table, so there is no reason to
3960      look at .symtab for a dynamic object.  */
3961 
3962   if (! dynamic || elf_dynsymtab (abfd) == 0)
3963     hdr = &elf_tdata (abfd)->symtab_hdr;
3964   else
3965     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3966 
3967   symcount = hdr->sh_size / bed->s->sizeof_sym;
3968 
3969   /* The sh_info field of the symtab header tells us where the
3970      external symbols start.  We don't care about the local symbols at
3971      this point.  */
3972   if (elf_bad_symtab (abfd))
3973     {
3974       extsymcount = symcount;
3975       extsymoff = 0;
3976     }
3977   else
3978     {
3979       extsymcount = symcount - hdr->sh_info;
3980       extsymoff = hdr->sh_info;
3981     }
3982 
3983   sym_hash = elf_sym_hashes (abfd);
3984   if (extsymcount != 0)
3985     {
3986       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3987 				      NULL, NULL, NULL);
3988       if (isymbuf == NULL)
3989 	goto error_return;
3990 
3991       if (sym_hash == NULL)
3992 	{
3993 	  /* We store a pointer to the hash table entry for each
3994 	     external symbol.  */
3995 	  amt = extsymcount;
3996 	  amt *= sizeof (struct elf_link_hash_entry *);
3997 	  sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3998 	  if (sym_hash == NULL)
3999 	    goto error_free_sym;
4000 	  elf_sym_hashes (abfd) = sym_hash;
4001 	}
4002     }
4003 
4004   if (dynamic)
4005     {
4006       /* Read in any version definitions.  */
4007       if (!_bfd_elf_slurp_version_tables (abfd,
4008 					  info->default_imported_symver))
4009 	goto error_free_sym;
4010 
4011       /* Read in the symbol versions, but don't bother to convert them
4012 	 to internal format.  */
4013       if (elf_dynversym (abfd) != 0)
4014 	{
4015 	  Elf_Internal_Shdr *versymhdr;
4016 
4017 	  versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4018 	  extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4019 	  if (extversym == NULL)
4020 	    goto error_free_sym;
4021 	  amt = versymhdr->sh_size;
4022 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4023 	      || bfd_bread (extversym, amt, abfd) != amt)
4024 	    goto error_free_vers;
4025 	}
4026     }
4027 
4028   /* If we are loading an as-needed shared lib, save the symbol table
4029      state before we start adding symbols.  If the lib turns out
4030      to be unneeded, restore the state.  */
4031   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4032     {
4033       unsigned int i;
4034       size_t entsize;
4035 
4036       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4037 	{
4038 	  struct bfd_hash_entry *p;
4039 	  struct elf_link_hash_entry *h;
4040 
4041 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4042 	    {
4043 	      h = (struct elf_link_hash_entry *) p;
4044 	      entsize += htab->root.table.entsize;
4045 	      if (h->root.type == bfd_link_hash_warning)
4046 		entsize += htab->root.table.entsize;
4047 	    }
4048 	}
4049 
4050       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4051       old_tab = bfd_malloc (tabsize + entsize);
4052       if (old_tab == NULL)
4053 	goto error_free_vers;
4054 
4055       /* Remember the current objalloc pointer, so that all mem for
4056 	 symbols added can later be reclaimed.  */
4057       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4058       if (alloc_mark == NULL)
4059 	goto error_free_vers;
4060 
4061       /* Make a special call to the linker "notice" function to
4062 	 tell it that we are about to handle an as-needed lib.  */
4063       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4064 	goto error_free_vers;
4065 
4066       /* Clone the symbol table.  Remember some pointers into the
4067 	 symbol table, and dynamic symbol count.  */
4068       old_ent = (char *) old_tab + tabsize;
4069       memcpy (old_tab, htab->root.table.table, tabsize);
4070       old_undefs = htab->root.undefs;
4071       old_undefs_tail = htab->root.undefs_tail;
4072       old_table = htab->root.table.table;
4073       old_size = htab->root.table.size;
4074       old_count = htab->root.table.count;
4075       old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4076       if (old_strtab == NULL)
4077 	goto error_free_vers;
4078 
4079       for (i = 0; i < htab->root.table.size; i++)
4080 	{
4081 	  struct bfd_hash_entry *p;
4082 	  struct elf_link_hash_entry *h;
4083 
4084 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4085 	    {
4086 	      memcpy (old_ent, p, htab->root.table.entsize);
4087 	      old_ent = (char *) old_ent + htab->root.table.entsize;
4088 	      h = (struct elf_link_hash_entry *) p;
4089 	      if (h->root.type == bfd_link_hash_warning)
4090 		{
4091 		  memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4092 		  old_ent = (char *) old_ent + htab->root.table.entsize;
4093 		}
4094 	    }
4095 	}
4096     }
4097 
4098   weaks = NULL;
4099   ever = extversym != NULL ? extversym + extsymoff : NULL;
4100   for (isym = isymbuf, isymend = isymbuf + extsymcount;
4101        isym < isymend;
4102        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4103     {
4104       int bind;
4105       bfd_vma value;
4106       asection *sec, *new_sec;
4107       flagword flags;
4108       const char *name;
4109       struct elf_link_hash_entry *h;
4110       struct elf_link_hash_entry *hi;
4111       bfd_boolean definition;
4112       bfd_boolean size_change_ok;
4113       bfd_boolean type_change_ok;
4114       bfd_boolean new_weakdef;
4115       bfd_boolean new_weak;
4116       bfd_boolean old_weak;
4117       bfd_boolean override;
4118       bfd_boolean common;
4119       bfd_boolean discarded;
4120       unsigned int old_alignment;
4121       bfd *old_bfd;
4122       bfd_boolean matched;
4123 
4124       override = FALSE;
4125 
4126       flags = BSF_NO_FLAGS;
4127       sec = NULL;
4128       value = isym->st_value;
4129       common = bed->common_definition (isym);
4130       discarded = FALSE;
4131 
4132       bind = ELF_ST_BIND (isym->st_info);
4133       switch (bind)
4134 	{
4135 	case STB_LOCAL:
4136 	  /* This should be impossible, since ELF requires that all
4137 	     global symbols follow all local symbols, and that sh_info
4138 	     point to the first global symbol.  Unfortunately, Irix 5
4139 	     screws this up.  */
4140 	  continue;
4141 
4142 	case STB_GLOBAL:
4143 	  if (isym->st_shndx != SHN_UNDEF && !common)
4144 	    flags = BSF_GLOBAL;
4145 	  break;
4146 
4147 	case STB_WEAK:
4148 	  flags = BSF_WEAK;
4149 	  break;
4150 
4151 	case STB_GNU_UNIQUE:
4152 	  flags = BSF_GNU_UNIQUE;
4153 	  break;
4154 
4155 	default:
4156 	  /* Leave it up to the processor backend.  */
4157 	  break;
4158 	}
4159 
4160       if (isym->st_shndx == SHN_UNDEF)
4161 	sec = bfd_und_section_ptr;
4162       else if (isym->st_shndx == SHN_ABS)
4163 	sec = bfd_abs_section_ptr;
4164       else if (isym->st_shndx == SHN_COMMON)
4165 	{
4166 	  sec = bfd_com_section_ptr;
4167 	  /* What ELF calls the size we call the value.  What ELF
4168 	     calls the value we call the alignment.  */
4169 	  value = isym->st_size;
4170 	}
4171       else
4172 	{
4173 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4174 	  if (sec == NULL)
4175 	    sec = bfd_abs_section_ptr;
4176 	  else if (discarded_section (sec))
4177 	    {
4178 	      /* Symbols from discarded section are undefined.  We keep
4179 		 its visibility.  */
4180 	      sec = bfd_und_section_ptr;
4181 	      discarded = TRUE;
4182 	      isym->st_shndx = SHN_UNDEF;
4183 	    }
4184 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4185 	    value -= sec->vma;
4186 	}
4187 
4188       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4189 					      isym->st_name);
4190       if (name == NULL)
4191 	goto error_free_vers;
4192 
4193       if (isym->st_shndx == SHN_COMMON
4194 	  && (abfd->flags & BFD_PLUGIN) != 0)
4195 	{
4196 	  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4197 
4198 	  if (xc == NULL)
4199 	    {
4200 	      flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4201 				 | SEC_EXCLUDE);
4202 	      xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4203 	      if (xc == NULL)
4204 		goto error_free_vers;
4205 	    }
4206 	  sec = xc;
4207 	}
4208       else if (isym->st_shndx == SHN_COMMON
4209 	       && ELF_ST_TYPE (isym->st_info) == STT_TLS
4210 	       && !bfd_link_relocatable (info))
4211 	{
4212 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4213 
4214 	  if (tcomm == NULL)
4215 	    {
4216 	      flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4217 				 | SEC_LINKER_CREATED);
4218 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4219 	      if (tcomm == NULL)
4220 		goto error_free_vers;
4221 	    }
4222 	  sec = tcomm;
4223 	}
4224       else if (bed->elf_add_symbol_hook)
4225 	{
4226 	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4227 					     &sec, &value))
4228 	    goto error_free_vers;
4229 
4230 	  /* The hook function sets the name to NULL if this symbol
4231 	     should be skipped for some reason.  */
4232 	  if (name == NULL)
4233 	    continue;
4234 	}
4235 
4236       /* Sanity check that all possibilities were handled.  */
4237       if (sec == NULL)
4238 	{
4239 	  bfd_set_error (bfd_error_bad_value);
4240 	  goto error_free_vers;
4241 	}
4242 
4243       /* Silently discard TLS symbols from --just-syms.  There's
4244 	 no way to combine a static TLS block with a new TLS block
4245 	 for this executable.  */
4246       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4247 	  && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4248 	continue;
4249 
4250       if (bfd_is_und_section (sec)
4251 	  || bfd_is_com_section (sec))
4252 	definition = FALSE;
4253       else
4254 	definition = TRUE;
4255 
4256       size_change_ok = FALSE;
4257       type_change_ok = bed->type_change_ok;
4258       old_weak = FALSE;
4259       matched = FALSE;
4260       old_alignment = 0;
4261       old_bfd = NULL;
4262       new_sec = sec;
4263 
4264       if (is_elf_hash_table (htab))
4265 	{
4266 	  Elf_Internal_Versym iver;
4267 	  unsigned int vernum = 0;
4268 	  bfd_boolean skip;
4269 
4270 	  if (ever == NULL)
4271 	    {
4272 	      if (info->default_imported_symver)
4273 		/* Use the default symbol version created earlier.  */
4274 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
4275 	      else
4276 		iver.vs_vers = 0;
4277 	    }
4278 	  else
4279 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
4280 
4281 	  vernum = iver.vs_vers & VERSYM_VERSION;
4282 
4283 	  /* If this is a hidden symbol, or if it is not version
4284 	     1, we append the version name to the symbol name.
4285 	     However, we do not modify a non-hidden absolute symbol
4286 	     if it is not a function, because it might be the version
4287 	     symbol itself.  FIXME: What if it isn't?  */
4288 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4289 	      || (vernum > 1
4290 		  && (!bfd_is_abs_section (sec)
4291 		      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4292 	    {
4293 	      const char *verstr;
4294 	      size_t namelen, verlen, newlen;
4295 	      char *newname, *p;
4296 
4297 	      if (isym->st_shndx != SHN_UNDEF)
4298 		{
4299 		  if (vernum > elf_tdata (abfd)->cverdefs)
4300 		    verstr = NULL;
4301 		  else if (vernum > 1)
4302 		    verstr =
4303 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4304 		  else
4305 		    verstr = "";
4306 
4307 		  if (verstr == NULL)
4308 		    {
4309 		      (*_bfd_error_handler)
4310 			(_("%B: %s: invalid version %u (max %d)"),
4311 			 abfd, name, vernum,
4312 			 elf_tdata (abfd)->cverdefs);
4313 		      bfd_set_error (bfd_error_bad_value);
4314 		      goto error_free_vers;
4315 		    }
4316 		}
4317 	      else
4318 		{
4319 		  /* We cannot simply test for the number of
4320 		     entries in the VERNEED section since the
4321 		     numbers for the needed versions do not start
4322 		     at 0.  */
4323 		  Elf_Internal_Verneed *t;
4324 
4325 		  verstr = NULL;
4326 		  for (t = elf_tdata (abfd)->verref;
4327 		       t != NULL;
4328 		       t = t->vn_nextref)
4329 		    {
4330 		      Elf_Internal_Vernaux *a;
4331 
4332 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4333 			{
4334 			  if (a->vna_other == vernum)
4335 			    {
4336 			      verstr = a->vna_nodename;
4337 			      break;
4338 			    }
4339 			}
4340 		      if (a != NULL)
4341 			break;
4342 		    }
4343 		  if (verstr == NULL)
4344 		    {
4345 		      (*_bfd_error_handler)
4346 			(_("%B: %s: invalid needed version %d"),
4347 			 abfd, name, vernum);
4348 		      bfd_set_error (bfd_error_bad_value);
4349 		      goto error_free_vers;
4350 		    }
4351 		}
4352 
4353 	      namelen = strlen (name);
4354 	      verlen = strlen (verstr);
4355 	      newlen = namelen + verlen + 2;
4356 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4357 		  && isym->st_shndx != SHN_UNDEF)
4358 		++newlen;
4359 
4360 	      newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4361 	      if (newname == NULL)
4362 		goto error_free_vers;
4363 	      memcpy (newname, name, namelen);
4364 	      p = newname + namelen;
4365 	      *p++ = ELF_VER_CHR;
4366 	      /* If this is a defined non-hidden version symbol,
4367 		 we add another @ to the name.  This indicates the
4368 		 default version of the symbol.  */
4369 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4370 		  && isym->st_shndx != SHN_UNDEF)
4371 		*p++ = ELF_VER_CHR;
4372 	      memcpy (p, verstr, verlen + 1);
4373 
4374 	      name = newname;
4375 	    }
4376 
4377 	  /* If this symbol has default visibility and the user has
4378 	     requested we not re-export it, then mark it as hidden.  */
4379 	  if (!bfd_is_und_section (sec)
4380 	      && !dynamic
4381 	      && abfd->no_export
4382 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4383 	    isym->st_other = (STV_HIDDEN
4384 			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4385 
4386 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4387 				      sym_hash, &old_bfd, &old_weak,
4388 				      &old_alignment, &skip, &override,
4389 				      &type_change_ok, &size_change_ok,
4390 				      &matched))
4391 	    goto error_free_vers;
4392 
4393 	  if (skip)
4394 	    continue;
4395 
4396 	  /* Override a definition only if the new symbol matches the
4397 	     existing one.  */
4398 	  if (override && matched)
4399 	    definition = FALSE;
4400 
4401 	  h = *sym_hash;
4402 	  while (h->root.type == bfd_link_hash_indirect
4403 		 || h->root.type == bfd_link_hash_warning)
4404 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4405 
4406 	  if (elf_tdata (abfd)->verdef != NULL
4407 	      && vernum > 1
4408 	      && definition)
4409 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4410 	}
4411 
4412       if (! (_bfd_generic_link_add_one_symbol
4413 	     (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4414 	      (struct bfd_link_hash_entry **) sym_hash)))
4415 	goto error_free_vers;
4416 
4417       if ((flags & BSF_GNU_UNIQUE)
4418 	  && (abfd->flags & DYNAMIC) == 0
4419 	  && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4420 	elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4421 
4422       h = *sym_hash;
4423       /* We need to make sure that indirect symbol dynamic flags are
4424 	 updated.  */
4425       hi = h;
4426       while (h->root.type == bfd_link_hash_indirect
4427 	     || h->root.type == bfd_link_hash_warning)
4428 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
4429 
4430       /* Setting the index to -3 tells elf_link_output_extsym that
4431 	 this symbol is defined in a discarded section.  */
4432       if (discarded)
4433 	h->indx = -3;
4434 
4435       *sym_hash = h;
4436 
4437       new_weak = (flags & BSF_WEAK) != 0;
4438       new_weakdef = FALSE;
4439       if (dynamic
4440 	  && definition
4441 	  && new_weak
4442 	  && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4443 	  && is_elf_hash_table (htab)
4444 	  && h->u.weakdef == NULL)
4445 	{
4446 	  /* Keep a list of all weak defined non function symbols from
4447 	     a dynamic object, using the weakdef field.  Later in this
4448 	     function we will set the weakdef field to the correct
4449 	     value.  We only put non-function symbols from dynamic
4450 	     objects on this list, because that happens to be the only
4451 	     time we need to know the normal symbol corresponding to a
4452 	     weak symbol, and the information is time consuming to
4453 	     figure out.  If the weakdef field is not already NULL,
4454 	     then this symbol was already defined by some previous
4455 	     dynamic object, and we will be using that previous
4456 	     definition anyhow.  */
4457 
4458 	  h->u.weakdef = weaks;
4459 	  weaks = h;
4460 	  new_weakdef = TRUE;
4461 	}
4462 
4463       /* Set the alignment of a common symbol.  */
4464       if ((common || bfd_is_com_section (sec))
4465 	  && h->root.type == bfd_link_hash_common)
4466 	{
4467 	  unsigned int align;
4468 
4469 	  if (common)
4470 	    align = bfd_log2 (isym->st_value);
4471 	  else
4472 	    {
4473 	      /* The new symbol is a common symbol in a shared object.
4474 		 We need to get the alignment from the section.  */
4475 	      align = new_sec->alignment_power;
4476 	    }
4477 	  if (align > old_alignment)
4478 	    h->root.u.c.p->alignment_power = align;
4479 	  else
4480 	    h->root.u.c.p->alignment_power = old_alignment;
4481 	}
4482 
4483       if (is_elf_hash_table (htab))
4484 	{
4485 	  /* Set a flag in the hash table entry indicating the type of
4486 	     reference or definition we just found.  A dynamic symbol
4487 	     is one which is referenced or defined by both a regular
4488 	     object and a shared object.  */
4489 	  bfd_boolean dynsym = FALSE;
4490 
4491 	  /* Plugin symbols aren't normal.  Don't set def_regular or
4492 	     ref_regular for them, or make them dynamic.  */
4493 	  if ((abfd->flags & BFD_PLUGIN) != 0)
4494 	    ;
4495 	  else if (! dynamic)
4496 	    {
4497 	      if (! definition)
4498 		{
4499 		  h->ref_regular = 1;
4500 		  if (bind != STB_WEAK)
4501 		    h->ref_regular_nonweak = 1;
4502 		}
4503 	      else
4504 		{
4505 		  h->def_regular = 1;
4506 		  if (h->def_dynamic)
4507 		    {
4508 		      h->def_dynamic = 0;
4509 		      h->ref_dynamic = 1;
4510 		    }
4511 		}
4512 
4513 	      /* If the indirect symbol has been forced local, don't
4514 		 make the real symbol dynamic.  */
4515 	      if ((h == hi || !hi->forced_local)
4516 		  && (bfd_link_dll (info)
4517 		      || h->def_dynamic
4518 		      || h->ref_dynamic))
4519 		dynsym = TRUE;
4520 	    }
4521 	  else
4522 	    {
4523 	      if (! definition)
4524 		{
4525 		  h->ref_dynamic = 1;
4526 		  hi->ref_dynamic = 1;
4527 		}
4528 	      else
4529 		{
4530 		  h->def_dynamic = 1;
4531 		  hi->def_dynamic = 1;
4532 		}
4533 
4534 	      /* If the indirect symbol has been forced local, don't
4535 		 make the real symbol dynamic.  */
4536 	      if ((h == hi || !hi->forced_local)
4537 		  && (h->def_regular
4538 		      || h->ref_regular
4539 		      || (h->u.weakdef != NULL
4540 			  && ! new_weakdef
4541 			  && h->u.weakdef->dynindx != -1)))
4542 		dynsym = TRUE;
4543 	    }
4544 
4545 	  /* Check to see if we need to add an indirect symbol for
4546 	     the default name.  */
4547 	  if (definition
4548 	      || (!override && h->root.type == bfd_link_hash_common))
4549 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4550 					      sec, value, &old_bfd, &dynsym))
4551 	      goto error_free_vers;
4552 
4553 	  /* Check the alignment when a common symbol is involved. This
4554 	     can change when a common symbol is overridden by a normal
4555 	     definition or a common symbol is ignored due to the old
4556 	     normal definition. We need to make sure the maximum
4557 	     alignment is maintained.  */
4558 	  if ((old_alignment || common)
4559 	      && h->root.type != bfd_link_hash_common)
4560 	    {
4561 	      unsigned int common_align;
4562 	      unsigned int normal_align;
4563 	      unsigned int symbol_align;
4564 	      bfd *normal_bfd;
4565 	      bfd *common_bfd;
4566 
4567 	      BFD_ASSERT (h->root.type == bfd_link_hash_defined
4568 			  || h->root.type == bfd_link_hash_defweak);
4569 
4570 	      symbol_align = ffs (h->root.u.def.value) - 1;
4571 	      if (h->root.u.def.section->owner != NULL
4572 		  && (h->root.u.def.section->owner->flags
4573 		       & (DYNAMIC | BFD_PLUGIN)) == 0)
4574 		{
4575 		  normal_align = h->root.u.def.section->alignment_power;
4576 		  if (normal_align > symbol_align)
4577 		    normal_align = symbol_align;
4578 		}
4579 	      else
4580 		normal_align = symbol_align;
4581 
4582 	      if (old_alignment)
4583 		{
4584 		  common_align = old_alignment;
4585 		  common_bfd = old_bfd;
4586 		  normal_bfd = abfd;
4587 		}
4588 	      else
4589 		{
4590 		  common_align = bfd_log2 (isym->st_value);
4591 		  common_bfd = abfd;
4592 		  normal_bfd = old_bfd;
4593 		}
4594 
4595 	      if (normal_align < common_align)
4596 		{
4597 		  /* PR binutils/2735 */
4598 		  if (normal_bfd == NULL)
4599 		    (*_bfd_error_handler)
4600 		      (_("Warning: alignment %u of common symbol `%s' in %B is"
4601 			 " greater than the alignment (%u) of its section %A"),
4602 		       common_bfd, h->root.u.def.section,
4603 		       1 << common_align, name, 1 << normal_align);
4604 		  else
4605 		    (*_bfd_error_handler)
4606 		      (_("Warning: alignment %u of symbol `%s' in %B"
4607 			 " is smaller than %u in %B"),
4608 		       normal_bfd, common_bfd,
4609 		       1 << normal_align, name, 1 << common_align);
4610 		}
4611 	    }
4612 
4613 	  /* Remember the symbol size if it isn't undefined.  */
4614 	  if (isym->st_size != 0
4615 	      && isym->st_shndx != SHN_UNDEF
4616 	      && (definition || h->size == 0))
4617 	    {
4618 	      if (h->size != 0
4619 		  && h->size != isym->st_size
4620 		  && ! size_change_ok)
4621 		(*_bfd_error_handler)
4622 		  (_("Warning: size of symbol `%s' changed"
4623 		     " from %lu in %B to %lu in %B"),
4624 		   old_bfd, abfd,
4625 		   name, (unsigned long) h->size,
4626 		   (unsigned long) isym->st_size);
4627 
4628 	      h->size = isym->st_size;
4629 	    }
4630 
4631 	  /* If this is a common symbol, then we always want H->SIZE
4632 	     to be the size of the common symbol.  The code just above
4633 	     won't fix the size if a common symbol becomes larger.  We
4634 	     don't warn about a size change here, because that is
4635 	     covered by --warn-common.  Allow changes between different
4636 	     function types.  */
4637 	  if (h->root.type == bfd_link_hash_common)
4638 	    h->size = h->root.u.c.size;
4639 
4640 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4641 	      && ((definition && !new_weak)
4642 		  || (old_weak && h->root.type == bfd_link_hash_common)
4643 		  || h->type == STT_NOTYPE))
4644 	    {
4645 	      unsigned int type = ELF_ST_TYPE (isym->st_info);
4646 
4647 	      /* Turn an IFUNC symbol from a DSO into a normal FUNC
4648 		 symbol.  */
4649 	      if (type == STT_GNU_IFUNC
4650 		  && (abfd->flags & DYNAMIC) != 0)
4651 		type = STT_FUNC;
4652 
4653 	      if (h->type != type)
4654 		{
4655 		  if (h->type != STT_NOTYPE && ! type_change_ok)
4656 		    (*_bfd_error_handler)
4657 		      (_("Warning: type of symbol `%s' changed"
4658 			 " from %d to %d in %B"),
4659 		       abfd, name, h->type, type);
4660 
4661 		  h->type = type;
4662 		}
4663 	    }
4664 
4665 	  /* Merge st_other field.  */
4666 	  elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4667 
4668 	  /* We don't want to make debug symbol dynamic.  */
4669 	  if (definition
4670 	      && (sec->flags & SEC_DEBUGGING)
4671 	      && !bfd_link_relocatable (info))
4672 	    dynsym = FALSE;
4673 
4674 	  /* Nor should we make plugin symbols dynamic.  */
4675 	  if ((abfd->flags & BFD_PLUGIN) != 0)
4676 	    dynsym = FALSE;
4677 
4678 	  if (definition)
4679 	    {
4680 	      h->target_internal = isym->st_target_internal;
4681 	      h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4682 	    }
4683 
4684 	  if (definition && !dynamic)
4685 	    {
4686 	      char *p = strchr (name, ELF_VER_CHR);
4687 	      if (p != NULL && p[1] != ELF_VER_CHR)
4688 		{
4689 		  /* Queue non-default versions so that .symver x, x@FOO
4690 		     aliases can be checked.  */
4691 		  if (!nondeflt_vers)
4692 		    {
4693 		      amt = ((isymend - isym + 1)
4694 			     * sizeof (struct elf_link_hash_entry *));
4695 		      nondeflt_vers
4696 			= (struct elf_link_hash_entry **) bfd_malloc (amt);
4697 		      if (!nondeflt_vers)
4698 			goto error_free_vers;
4699 		    }
4700 		  nondeflt_vers[nondeflt_vers_cnt++] = h;
4701 		}
4702 	    }
4703 
4704 	  if (dynsym && h->dynindx == -1)
4705 	    {
4706 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4707 		goto error_free_vers;
4708 	      if (h->u.weakdef != NULL
4709 		  && ! new_weakdef
4710 		  && h->u.weakdef->dynindx == -1)
4711 		{
4712 		  if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4713 		    goto error_free_vers;
4714 		}
4715 	    }
4716 	  else if (h->dynindx != -1)
4717 	    /* If the symbol already has a dynamic index, but
4718 	       visibility says it should not be visible, turn it into
4719 	       a local symbol.  */
4720 	    switch (ELF_ST_VISIBILITY (h->other))
4721 	      {
4722 	      case STV_INTERNAL:
4723 	      case STV_HIDDEN:
4724 		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
4725 		dynsym = FALSE;
4726 		break;
4727 	      }
4728 
4729 	  /* Don't add DT_NEEDED for references from the dummy bfd nor
4730 	     for unmatched symbol.  */
4731 	  if (!add_needed
4732 	      && matched
4733 	      && definition
4734 	      && ((dynsym
4735 		   && h->ref_regular_nonweak
4736 		   && (old_bfd == NULL
4737 		       || (old_bfd->flags & BFD_PLUGIN) == 0))
4738 		  || (h->ref_dynamic_nonweak
4739 		      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4740 		      && !on_needed_list (elf_dt_name (abfd),
4741 					  htab->needed, NULL))))
4742 	    {
4743 	      int ret;
4744 	      const char *soname = elf_dt_name (abfd);
4745 
4746 	      info->callbacks->minfo ("%!", soname, old_bfd,
4747 				      h->root.root.string);
4748 
4749 	      /* A symbol from a library loaded via DT_NEEDED of some
4750 		 other library is referenced by a regular object.
4751 		 Add a DT_NEEDED entry for it.  Issue an error if
4752 		 --no-add-needed is used and the reference was not
4753 		 a weak one.  */
4754 	      if (old_bfd != NULL
4755 		  && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4756 		{
4757 		  (*_bfd_error_handler)
4758 		    (_("%B: undefined reference to symbol '%s'"),
4759 		     old_bfd, name);
4760 		  bfd_set_error (bfd_error_missing_dso);
4761 		  goto error_free_vers;
4762 		}
4763 
4764 	      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4765 		(elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4766 
4767 	      add_needed = TRUE;
4768 	      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4769 	      if (ret < 0)
4770 		goto error_free_vers;
4771 
4772 	      BFD_ASSERT (ret == 0);
4773 	    }
4774 	}
4775     }
4776 
4777   if (extversym != NULL)
4778     {
4779       free (extversym);
4780       extversym = NULL;
4781     }
4782 
4783   if (isymbuf != NULL)
4784     {
4785       free (isymbuf);
4786       isymbuf = NULL;
4787     }
4788 
4789   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4790     {
4791       unsigned int i;
4792 
4793       /* Restore the symbol table.  */
4794       old_ent = (char *) old_tab + tabsize;
4795       memset (elf_sym_hashes (abfd), 0,
4796 	      extsymcount * sizeof (struct elf_link_hash_entry *));
4797       htab->root.table.table = old_table;
4798       htab->root.table.size = old_size;
4799       htab->root.table.count = old_count;
4800       memcpy (htab->root.table.table, old_tab, tabsize);
4801       htab->root.undefs = old_undefs;
4802       htab->root.undefs_tail = old_undefs_tail;
4803       _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4804       free (old_strtab);
4805       old_strtab = NULL;
4806       for (i = 0; i < htab->root.table.size; i++)
4807 	{
4808 	  struct bfd_hash_entry *p;
4809 	  struct elf_link_hash_entry *h;
4810 	  bfd_size_type size;
4811 	  unsigned int alignment_power;
4812 
4813 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4814 	    {
4815 	      h = (struct elf_link_hash_entry *) p;
4816 	      if (h->root.type == bfd_link_hash_warning)
4817 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
4818 
4819 	      /* Preserve the maximum alignment and size for common
4820 		 symbols even if this dynamic lib isn't on DT_NEEDED
4821 		 since it can still be loaded at run time by another
4822 		 dynamic lib.  */
4823 	      if (h->root.type == bfd_link_hash_common)
4824 		{
4825 		  size = h->root.u.c.size;
4826 		  alignment_power = h->root.u.c.p->alignment_power;
4827 		}
4828 	      else
4829 		{
4830 		  size = 0;
4831 		  alignment_power = 0;
4832 		}
4833 	      memcpy (p, old_ent, htab->root.table.entsize);
4834 	      old_ent = (char *) old_ent + htab->root.table.entsize;
4835 	      h = (struct elf_link_hash_entry *) p;
4836 	      if (h->root.type == bfd_link_hash_warning)
4837 		{
4838 		  memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4839 		  old_ent = (char *) old_ent + htab->root.table.entsize;
4840 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
4841 		}
4842 	      if (h->root.type == bfd_link_hash_common)
4843 		{
4844 		  if (size > h->root.u.c.size)
4845 		    h->root.u.c.size = size;
4846 		  if (alignment_power > h->root.u.c.p->alignment_power)
4847 		    h->root.u.c.p->alignment_power = alignment_power;
4848 		}
4849 	    }
4850 	}
4851 
4852       /* Make a special call to the linker "notice" function to
4853 	 tell it that symbols added for crefs may need to be removed.  */
4854       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4855 	goto error_free_vers;
4856 
4857       free (old_tab);
4858       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4859 			   alloc_mark);
4860       if (nondeflt_vers != NULL)
4861 	free (nondeflt_vers);
4862       return TRUE;
4863     }
4864 
4865   if (old_tab != NULL)
4866     {
4867       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4868 	goto error_free_vers;
4869       free (old_tab);
4870       old_tab = NULL;
4871     }
4872 
4873   /* Now that all the symbols from this input file are created, if
4874      not performing a relocatable link, handle .symver foo, foo@BAR
4875      such that any relocs against foo become foo@BAR.  */
4876   if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4877     {
4878       size_t cnt, symidx;
4879 
4880       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4881 	{
4882 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4883 	  char *shortname, *p;
4884 
4885 	  p = strchr (h->root.root.string, ELF_VER_CHR);
4886 	  if (p == NULL
4887 	      || (h->root.type != bfd_link_hash_defined
4888 		  && h->root.type != bfd_link_hash_defweak))
4889 	    continue;
4890 
4891 	  amt = p - h->root.root.string;
4892 	  shortname = (char *) bfd_malloc (amt + 1);
4893 	  if (!shortname)
4894 	    goto error_free_vers;
4895 	  memcpy (shortname, h->root.root.string, amt);
4896 	  shortname[amt] = '\0';
4897 
4898 	  hi = (struct elf_link_hash_entry *)
4899 	       bfd_link_hash_lookup (&htab->root, shortname,
4900 				     FALSE, FALSE, FALSE);
4901 	  if (hi != NULL
4902 	      && hi->root.type == h->root.type
4903 	      && hi->root.u.def.value == h->root.u.def.value
4904 	      && hi->root.u.def.section == h->root.u.def.section)
4905 	    {
4906 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4907 	      hi->root.type = bfd_link_hash_indirect;
4908 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4909 	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4910 	      sym_hash = elf_sym_hashes (abfd);
4911 	      if (sym_hash)
4912 		for (symidx = 0; symidx < extsymcount; ++symidx)
4913 		  if (sym_hash[symidx] == hi)
4914 		    {
4915 		      sym_hash[symidx] = h;
4916 		      break;
4917 		    }
4918 	    }
4919 	  free (shortname);
4920 	}
4921       free (nondeflt_vers);
4922       nondeflt_vers = NULL;
4923     }
4924 
4925   /* Now set the weakdefs field correctly for all the weak defined
4926      symbols we found.  The only way to do this is to search all the
4927      symbols.  Since we only need the information for non functions in
4928      dynamic objects, that's the only time we actually put anything on
4929      the list WEAKS.  We need this information so that if a regular
4930      object refers to a symbol defined weakly in a dynamic object, the
4931      real symbol in the dynamic object is also put in the dynamic
4932      symbols; we also must arrange for both symbols to point to the
4933      same memory location.  We could handle the general case of symbol
4934      aliasing, but a general symbol alias can only be generated in
4935      assembler code, handling it correctly would be very time
4936      consuming, and other ELF linkers don't handle general aliasing
4937      either.  */
4938   if (weaks != NULL)
4939     {
4940       struct elf_link_hash_entry **hpp;
4941       struct elf_link_hash_entry **hppend;
4942       struct elf_link_hash_entry **sorted_sym_hash;
4943       struct elf_link_hash_entry *h;
4944       size_t sym_count;
4945 
4946       /* Since we have to search the whole symbol list for each weak
4947 	 defined symbol, search time for N weak defined symbols will be
4948 	 O(N^2). Binary search will cut it down to O(NlogN).  */
4949       amt = extsymcount;
4950       amt *= sizeof (struct elf_link_hash_entry *);
4951       sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4952       if (sorted_sym_hash == NULL)
4953 	goto error_return;
4954       sym_hash = sorted_sym_hash;
4955       hpp = elf_sym_hashes (abfd);
4956       hppend = hpp + extsymcount;
4957       sym_count = 0;
4958       for (; hpp < hppend; hpp++)
4959 	{
4960 	  h = *hpp;
4961 	  if (h != NULL
4962 	      && h->root.type == bfd_link_hash_defined
4963 	      && !bed->is_function_type (h->type))
4964 	    {
4965 	      *sym_hash = h;
4966 	      sym_hash++;
4967 	      sym_count++;
4968 	    }
4969 	}
4970 
4971       qsort (sorted_sym_hash, sym_count,
4972 	     sizeof (struct elf_link_hash_entry *),
4973 	     elf_sort_symbol);
4974 
4975       while (weaks != NULL)
4976 	{
4977 	  struct elf_link_hash_entry *hlook;
4978 	  asection *slook;
4979 	  bfd_vma vlook;
4980 	  size_t i, j, idx = 0;
4981 
4982 	  hlook = weaks;
4983 	  weaks = hlook->u.weakdef;
4984 	  hlook->u.weakdef = NULL;
4985 
4986 	  BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4987 		      || hlook->root.type == bfd_link_hash_defweak
4988 		      || hlook->root.type == bfd_link_hash_common
4989 		      || hlook->root.type == bfd_link_hash_indirect);
4990 	  slook = hlook->root.u.def.section;
4991 	  vlook = hlook->root.u.def.value;
4992 
4993 	  i = 0;
4994 	  j = sym_count;
4995 	  while (i != j)
4996 	    {
4997 	      bfd_signed_vma vdiff;
4998 	      idx = (i + j) / 2;
4999 	      h = sorted_sym_hash[idx];
5000 	      vdiff = vlook - h->root.u.def.value;
5001 	      if (vdiff < 0)
5002 		j = idx;
5003 	      else if (vdiff > 0)
5004 		i = idx + 1;
5005 	      else
5006 		{
5007 		  int sdiff = slook->id - h->root.u.def.section->id;
5008 		  if (sdiff < 0)
5009 		    j = idx;
5010 		  else if (sdiff > 0)
5011 		    i = idx + 1;
5012 		  else
5013 		    break;
5014 		}
5015 	    }
5016 
5017 	  /* We didn't find a value/section match.  */
5018 	  if (i == j)
5019 	    continue;
5020 
5021 	  /* With multiple aliases, or when the weak symbol is already
5022 	     strongly defined, we have multiple matching symbols and
5023 	     the binary search above may land on any of them.  Step
5024 	     one past the matching symbol(s).  */
5025 	  while (++idx != j)
5026 	    {
5027 	      h = sorted_sym_hash[idx];
5028 	      if (h->root.u.def.section != slook
5029 		  || h->root.u.def.value != vlook)
5030 		break;
5031 	    }
5032 
5033 	  /* Now look back over the aliases.  Since we sorted by size
5034 	     as well as value and section, we'll choose the one with
5035 	     the largest size.  */
5036 	  while (idx-- != i)
5037 	    {
5038 	      h = sorted_sym_hash[idx];
5039 
5040 	      /* Stop if value or section doesn't match.  */
5041 	      if (h->root.u.def.section != slook
5042 		  || h->root.u.def.value != vlook)
5043 		break;
5044 	      else if (h != hlook)
5045 		{
5046 		  hlook->u.weakdef = h;
5047 
5048 		  /* If the weak definition is in the list of dynamic
5049 		     symbols, make sure the real definition is put
5050 		     there as well.  */
5051 		  if (hlook->dynindx != -1 && h->dynindx == -1)
5052 		    {
5053 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
5054 			{
5055 			err_free_sym_hash:
5056 			  free (sorted_sym_hash);
5057 			  goto error_return;
5058 			}
5059 		    }
5060 
5061 		  /* If the real definition is in the list of dynamic
5062 		     symbols, make sure the weak definition is put
5063 		     there as well.  If we don't do this, then the
5064 		     dynamic loader might not merge the entries for the
5065 		     real definition and the weak definition.  */
5066 		  if (h->dynindx != -1 && hlook->dynindx == -1)
5067 		    {
5068 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5069 			goto err_free_sym_hash;
5070 		    }
5071 		  break;
5072 		}
5073 	    }
5074 	}
5075 
5076       free (sorted_sym_hash);
5077     }
5078 
5079   if (bed->check_directives
5080       && !(*bed->check_directives) (abfd, info))
5081     return FALSE;
5082 
5083   if (!info->check_relocs_after_open_input
5084       && !_bfd_elf_link_check_relocs (abfd, info))
5085     return FALSE;
5086 
5087   /* If this is a non-traditional link, try to optimize the handling
5088      of the .stab/.stabstr sections.  */
5089   if (! dynamic
5090       && ! info->traditional_format
5091       && is_elf_hash_table (htab)
5092       && (info->strip != strip_all && info->strip != strip_debugger))
5093     {
5094       asection *stabstr;
5095 
5096       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5097       if (stabstr != NULL)
5098 	{
5099 	  bfd_size_type string_offset = 0;
5100 	  asection *stab;
5101 
5102 	  for (stab = abfd->sections; stab; stab = stab->next)
5103 	    if (CONST_STRNEQ (stab->name, ".stab")
5104 		&& (!stab->name[5] ||
5105 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5106 		&& (stab->flags & SEC_MERGE) == 0
5107 		&& !bfd_is_abs_section (stab->output_section))
5108 	      {
5109 		struct bfd_elf_section_data *secdata;
5110 
5111 		secdata = elf_section_data (stab);
5112 		if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5113 					       stabstr, &secdata->sec_info,
5114 					       &string_offset))
5115 		  goto error_return;
5116 		if (secdata->sec_info)
5117 		  stab->sec_info_type = SEC_INFO_TYPE_STABS;
5118 	    }
5119 	}
5120     }
5121 
5122   if (is_elf_hash_table (htab) && add_needed)
5123     {
5124       /* Add this bfd to the loaded list.  */
5125       struct elf_link_loaded_list *n;
5126 
5127       n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5128       if (n == NULL)
5129 	goto error_return;
5130       n->abfd = abfd;
5131       n->next = htab->loaded;
5132       htab->loaded = n;
5133     }
5134 
5135   return TRUE;
5136 
5137  error_free_vers:
5138   if (old_tab != NULL)
5139     free (old_tab);
5140   if (old_strtab != NULL)
5141     free (old_strtab);
5142   if (nondeflt_vers != NULL)
5143     free (nondeflt_vers);
5144   if (extversym != NULL)
5145     free (extversym);
5146  error_free_sym:
5147   if (isymbuf != NULL)
5148     free (isymbuf);
5149  error_return:
5150   return FALSE;
5151 }
5152 
5153 /* Return the linker hash table entry of a symbol that might be
5154    satisfied by an archive symbol.  Return -1 on error.  */
5155 
5156 struct elf_link_hash_entry *
_bfd_elf_archive_symbol_lookup(bfd * abfd,struct bfd_link_info * info,const char * name)5157 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5158 				struct bfd_link_info *info,
5159 				const char *name)
5160 {
5161   struct elf_link_hash_entry *h;
5162   char *p, *copy;
5163   size_t len, first;
5164 
5165   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5166   if (h != NULL)
5167     return h;
5168 
5169   /* If this is a default version (the name contains @@), look up the
5170      symbol again with only one `@' as well as without the version.
5171      The effect is that references to the symbol with and without the
5172      version will be matched by the default symbol in the archive.  */
5173 
5174   p = strchr (name, ELF_VER_CHR);
5175   if (p == NULL || p[1] != ELF_VER_CHR)
5176     return h;
5177 
5178   /* First check with only one `@'.  */
5179   len = strlen (name);
5180   copy = (char *) bfd_alloc (abfd, len);
5181   if (copy == NULL)
5182     return (struct elf_link_hash_entry *) 0 - 1;
5183 
5184   first = p - name + 1;
5185   memcpy (copy, name, first);
5186   memcpy (copy + first, name + first + 1, len - first);
5187 
5188   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5189   if (h == NULL)
5190     {
5191       /* We also need to check references to the symbol without the
5192 	 version.  */
5193       copy[first - 1] = '\0';
5194       h = elf_link_hash_lookup (elf_hash_table (info), copy,
5195 				FALSE, FALSE, TRUE);
5196     }
5197 
5198   bfd_release (abfd, copy);
5199   return h;
5200 }
5201 
5202 /* Add symbols from an ELF archive file to the linker hash table.  We
5203    don't use _bfd_generic_link_add_archive_symbols because we need to
5204    handle versioned symbols.
5205 
5206    Fortunately, ELF archive handling is simpler than that done by
5207    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5208    oddities.  In ELF, if we find a symbol in the archive map, and the
5209    symbol is currently undefined, we know that we must pull in that
5210    object file.
5211 
5212    Unfortunately, we do have to make multiple passes over the symbol
5213    table until nothing further is resolved.  */
5214 
5215 static bfd_boolean
elf_link_add_archive_symbols(bfd * abfd,struct bfd_link_info * info)5216 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5217 {
5218   symindex c;
5219   unsigned char *included = NULL;
5220   carsym *symdefs;
5221   bfd_boolean loop;
5222   bfd_size_type amt;
5223   const struct elf_backend_data *bed;
5224   struct elf_link_hash_entry * (*archive_symbol_lookup)
5225     (bfd *, struct bfd_link_info *, const char *);
5226 
5227   if (! bfd_has_map (abfd))
5228     {
5229       /* An empty archive is a special case.  */
5230       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5231 	return TRUE;
5232       bfd_set_error (bfd_error_no_armap);
5233       return FALSE;
5234     }
5235 
5236   /* Keep track of all symbols we know to be already defined, and all
5237      files we know to be already included.  This is to speed up the
5238      second and subsequent passes.  */
5239   c = bfd_ardata (abfd)->symdef_count;
5240   if (c == 0)
5241     return TRUE;
5242   amt = c;
5243   amt *= sizeof (*included);
5244   included = (unsigned char *) bfd_zmalloc (amt);
5245   if (included == NULL)
5246     return FALSE;
5247 
5248   symdefs = bfd_ardata (abfd)->symdefs;
5249   bed = get_elf_backend_data (abfd);
5250   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5251 
5252   do
5253     {
5254       file_ptr last;
5255       symindex i;
5256       carsym *symdef;
5257       carsym *symdefend;
5258 
5259       loop = FALSE;
5260       last = -1;
5261 
5262       symdef = symdefs;
5263       symdefend = symdef + c;
5264       for (i = 0; symdef < symdefend; symdef++, i++)
5265 	{
5266 	  struct elf_link_hash_entry *h;
5267 	  bfd *element;
5268 	  struct bfd_link_hash_entry *undefs_tail;
5269 	  symindex mark;
5270 
5271 	  if (included[i])
5272 	    continue;
5273 	  if (symdef->file_offset == last)
5274 	    {
5275 	      included[i] = TRUE;
5276 	      continue;
5277 	    }
5278 
5279 	  h = archive_symbol_lookup (abfd, info, symdef->name);
5280 	  if (h == (struct elf_link_hash_entry *) 0 - 1)
5281 	    goto error_return;
5282 
5283 	  if (h == NULL)
5284 	    continue;
5285 
5286 	  if (h->root.type == bfd_link_hash_common)
5287 	    {
5288 	      /* We currently have a common symbol.  The archive map contains
5289 		 a reference to this symbol, so we may want to include it.  We
5290 		 only want to include it however, if this archive element
5291 		 contains a definition of the symbol, not just another common
5292 		 declaration of it.
5293 
5294 		 Unfortunately some archivers (including GNU ar) will put
5295 		 declarations of common symbols into their archive maps, as
5296 		 well as real definitions, so we cannot just go by the archive
5297 		 map alone.  Instead we must read in the element's symbol
5298 		 table and check that to see what kind of symbol definition
5299 		 this is.  */
5300 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5301 		continue;
5302 	    }
5303 	  else if (h->root.type != bfd_link_hash_undefined)
5304 	    {
5305 	      if (h->root.type != bfd_link_hash_undefweak)
5306 		/* Symbol must be defined.  Don't check it again.  */
5307 		included[i] = TRUE;
5308 	      continue;
5309 	    }
5310 
5311 	  /* We need to include this archive member.  */
5312 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5313 	  if (element == NULL)
5314 	    goto error_return;
5315 
5316 	  if (! bfd_check_format (element, bfd_object))
5317 	    goto error_return;
5318 
5319 	  undefs_tail = info->hash->undefs_tail;
5320 
5321 	  if (!(*info->callbacks
5322 		->add_archive_element) (info, element, symdef->name, &element))
5323 	    continue;
5324 	  if (!bfd_link_add_symbols (element, info))
5325 	    goto error_return;
5326 
5327 	  /* If there are any new undefined symbols, we need to make
5328 	     another pass through the archive in order to see whether
5329 	     they can be defined.  FIXME: This isn't perfect, because
5330 	     common symbols wind up on undefs_tail and because an
5331 	     undefined symbol which is defined later on in this pass
5332 	     does not require another pass.  This isn't a bug, but it
5333 	     does make the code less efficient than it could be.  */
5334 	  if (undefs_tail != info->hash->undefs_tail)
5335 	    loop = TRUE;
5336 
5337 	  /* Look backward to mark all symbols from this object file
5338 	     which we have already seen in this pass.  */
5339 	  mark = i;
5340 	  do
5341 	    {
5342 	      included[mark] = TRUE;
5343 	      if (mark == 0)
5344 		break;
5345 	      --mark;
5346 	    }
5347 	  while (symdefs[mark].file_offset == symdef->file_offset);
5348 
5349 	  /* We mark subsequent symbols from this object file as we go
5350 	     on through the loop.  */
5351 	  last = symdef->file_offset;
5352 	}
5353     }
5354   while (loop);
5355 
5356   free (included);
5357 
5358   return TRUE;
5359 
5360  error_return:
5361   if (included != NULL)
5362     free (included);
5363   return FALSE;
5364 }
5365 
5366 /* Given an ELF BFD, add symbols to the global hash table as
5367    appropriate.  */
5368 
5369 bfd_boolean
bfd_elf_link_add_symbols(bfd * abfd,struct bfd_link_info * info)5370 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5371 {
5372   switch (bfd_get_format (abfd))
5373     {
5374     case bfd_object:
5375       return elf_link_add_object_symbols (abfd, info);
5376     case bfd_archive:
5377       return elf_link_add_archive_symbols (abfd, info);
5378     default:
5379       bfd_set_error (bfd_error_wrong_format);
5380       return FALSE;
5381     }
5382 }
5383 
5384 struct hash_codes_info
5385 {
5386   unsigned long *hashcodes;
5387   bfd_boolean error;
5388 };
5389 
5390 /* This function will be called though elf_link_hash_traverse to store
5391    all hash value of the exported symbols in an array.  */
5392 
5393 static bfd_boolean
elf_collect_hash_codes(struct elf_link_hash_entry * h,void * data)5394 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5395 {
5396   struct hash_codes_info *inf = (struct hash_codes_info *) data;
5397   const char *name;
5398   unsigned long ha;
5399   char *alc = NULL;
5400 
5401   /* Ignore indirect symbols.  These are added by the versioning code.  */
5402   if (h->dynindx == -1)
5403     return TRUE;
5404 
5405   name = h->root.root.string;
5406   if (h->versioned >= versioned)
5407     {
5408       char *p = strchr (name, ELF_VER_CHR);
5409       if (p != NULL)
5410 	{
5411 	  alc = (char *) bfd_malloc (p - name + 1);
5412 	  if (alc == NULL)
5413 	    {
5414 	      inf->error = TRUE;
5415 	      return FALSE;
5416 	    }
5417 	  memcpy (alc, name, p - name);
5418 	  alc[p - name] = '\0';
5419 	  name = alc;
5420 	}
5421     }
5422 
5423   /* Compute the hash value.  */
5424   ha = bfd_elf_hash (name);
5425 
5426   /* Store the found hash value in the array given as the argument.  */
5427   *(inf->hashcodes)++ = ha;
5428 
5429   /* And store it in the struct so that we can put it in the hash table
5430      later.  */
5431   h->u.elf_hash_value = ha;
5432 
5433   if (alc != NULL)
5434     free (alc);
5435 
5436   return TRUE;
5437 }
5438 
5439 struct collect_gnu_hash_codes
5440 {
5441   bfd *output_bfd;
5442   const struct elf_backend_data *bed;
5443   unsigned long int nsyms;
5444   unsigned long int maskbits;
5445   unsigned long int *hashcodes;
5446   unsigned long int *hashval;
5447   unsigned long int *indx;
5448   unsigned long int *counts;
5449   bfd_vma *bitmask;
5450   bfd_byte *contents;
5451   long int min_dynindx;
5452   unsigned long int bucketcount;
5453   unsigned long int symindx;
5454   long int local_indx;
5455   long int shift1, shift2;
5456   unsigned long int mask;
5457   bfd_boolean error;
5458 };
5459 
5460 /* This function will be called though elf_link_hash_traverse to store
5461    all hash value of the exported symbols in an array.  */
5462 
5463 static bfd_boolean
elf_collect_gnu_hash_codes(struct elf_link_hash_entry * h,void * data)5464 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5465 {
5466   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5467   const char *name;
5468   unsigned long ha;
5469   char *alc = NULL;
5470 
5471   /* Ignore indirect symbols.  These are added by the versioning code.  */
5472   if (h->dynindx == -1)
5473     return TRUE;
5474 
5475   /* Ignore also local symbols and undefined symbols.  */
5476   if (! (*s->bed->elf_hash_symbol) (h))
5477     return TRUE;
5478 
5479   name = h->root.root.string;
5480   if (h->versioned >= versioned)
5481     {
5482       char *p = strchr (name, ELF_VER_CHR);
5483       if (p != NULL)
5484 	{
5485 	  alc = (char *) bfd_malloc (p - name + 1);
5486 	  if (alc == NULL)
5487 	    {
5488 	      s->error = TRUE;
5489 	      return FALSE;
5490 	    }
5491 	  memcpy (alc, name, p - name);
5492 	  alc[p - name] = '\0';
5493 	  name = alc;
5494 	}
5495     }
5496 
5497   /* Compute the hash value.  */
5498   ha = bfd_elf_gnu_hash (name);
5499 
5500   /* Store the found hash value in the array for compute_bucket_count,
5501      and also for .dynsym reordering purposes.  */
5502   s->hashcodes[s->nsyms] = ha;
5503   s->hashval[h->dynindx] = ha;
5504   ++s->nsyms;
5505   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5506     s->min_dynindx = h->dynindx;
5507 
5508   if (alc != NULL)
5509     free (alc);
5510 
5511   return TRUE;
5512 }
5513 
5514 /* This function will be called though elf_link_hash_traverse to do
5515    final dynaminc symbol renumbering.  */
5516 
5517 static bfd_boolean
elf_renumber_gnu_hash_syms(struct elf_link_hash_entry * h,void * data)5518 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5519 {
5520   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5521   unsigned long int bucket;
5522   unsigned long int val;
5523 
5524   /* Ignore indirect symbols.  */
5525   if (h->dynindx == -1)
5526     return TRUE;
5527 
5528   /* Ignore also local symbols and undefined symbols.  */
5529   if (! (*s->bed->elf_hash_symbol) (h))
5530     {
5531       if (h->dynindx >= s->min_dynindx)
5532 	h->dynindx = s->local_indx++;
5533       return TRUE;
5534     }
5535 
5536   bucket = s->hashval[h->dynindx] % s->bucketcount;
5537   val = (s->hashval[h->dynindx] >> s->shift1)
5538 	& ((s->maskbits >> s->shift1) - 1);
5539   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5540   s->bitmask[val]
5541     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5542   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5543   if (s->counts[bucket] == 1)
5544     /* Last element terminates the chain.  */
5545     val |= 1;
5546   bfd_put_32 (s->output_bfd, val,
5547 	      s->contents + (s->indx[bucket] - s->symindx) * 4);
5548   --s->counts[bucket];
5549   h->dynindx = s->indx[bucket]++;
5550   return TRUE;
5551 }
5552 
5553 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
5554 
5555 bfd_boolean
_bfd_elf_hash_symbol(struct elf_link_hash_entry * h)5556 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5557 {
5558   return !(h->forced_local
5559 	   || h->root.type == bfd_link_hash_undefined
5560 	   || h->root.type == bfd_link_hash_undefweak
5561 	   || ((h->root.type == bfd_link_hash_defined
5562 		|| h->root.type == bfd_link_hash_defweak)
5563 	       && h->root.u.def.section->output_section == NULL));
5564 }
5565 
5566 /* Array used to determine the number of hash table buckets to use
5567    based on the number of symbols there are.  If there are fewer than
5568    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5569    fewer than 37 we use 17 buckets, and so forth.  We never use more
5570    than 32771 buckets.  */
5571 
5572 static const size_t elf_buckets[] =
5573 {
5574   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5575   16411, 32771, 0
5576 };
5577 
5578 /* Compute bucket count for hashing table.  We do not use a static set
5579    of possible tables sizes anymore.  Instead we determine for all
5580    possible reasonable sizes of the table the outcome (i.e., the
5581    number of collisions etc) and choose the best solution.  The
5582    weighting functions are not too simple to allow the table to grow
5583    without bounds.  Instead one of the weighting factors is the size.
5584    Therefore the result is always a good payoff between few collisions
5585    (= short chain lengths) and table size.  */
5586 static size_t
compute_bucket_count(struct bfd_link_info * info ATTRIBUTE_UNUSED,unsigned long int * hashcodes ATTRIBUTE_UNUSED,unsigned long int nsyms,int gnu_hash)5587 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5588 		      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5589 		      unsigned long int nsyms,
5590 		      int gnu_hash)
5591 {
5592   size_t best_size = 0;
5593   unsigned long int i;
5594 
5595   /* We have a problem here.  The following code to optimize the table
5596      size requires an integer type with more the 32 bits.  If
5597      BFD_HOST_U_64_BIT is set we know about such a type.  */
5598 #ifdef BFD_HOST_U_64_BIT
5599   if (info->optimize)
5600     {
5601       size_t minsize;
5602       size_t maxsize;
5603       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5604       bfd *dynobj = elf_hash_table (info)->dynobj;
5605       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5606       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5607       unsigned long int *counts;
5608       bfd_size_type amt;
5609       unsigned int no_improvement_count = 0;
5610 
5611       /* Possible optimization parameters: if we have NSYMS symbols we say
5612 	 that the hashing table must at least have NSYMS/4 and at most
5613 	 2*NSYMS buckets.  */
5614       minsize = nsyms / 4;
5615       if (minsize == 0)
5616 	minsize = 1;
5617       best_size = maxsize = nsyms * 2;
5618       if (gnu_hash)
5619 	{
5620 	  if (minsize < 2)
5621 	    minsize = 2;
5622 	  if ((best_size & 31) == 0)
5623 	    ++best_size;
5624 	}
5625 
5626       /* Create array where we count the collisions in.  We must use bfd_malloc
5627 	 since the size could be large.  */
5628       amt = maxsize;
5629       amt *= sizeof (unsigned long int);
5630       counts = (unsigned long int *) bfd_malloc (amt);
5631       if (counts == NULL)
5632 	return 0;
5633 
5634       /* Compute the "optimal" size for the hash table.  The criteria is a
5635 	 minimal chain length.  The minor criteria is (of course) the size
5636 	 of the table.  */
5637       for (i = minsize; i < maxsize; ++i)
5638 	{
5639 	  /* Walk through the array of hashcodes and count the collisions.  */
5640 	  BFD_HOST_U_64_BIT max;
5641 	  unsigned long int j;
5642 	  unsigned long int fact;
5643 
5644 	  if (gnu_hash && (i & 31) == 0)
5645 	    continue;
5646 
5647 	  memset (counts, '\0', i * sizeof (unsigned long int));
5648 
5649 	  /* Determine how often each hash bucket is used.  */
5650 	  for (j = 0; j < nsyms; ++j)
5651 	    ++counts[hashcodes[j] % i];
5652 
5653 	  /* For the weight function we need some information about the
5654 	     pagesize on the target.  This is information need not be 100%
5655 	     accurate.  Since this information is not available (so far) we
5656 	     define it here to a reasonable default value.  If it is crucial
5657 	     to have a better value some day simply define this value.  */
5658 # ifndef BFD_TARGET_PAGESIZE
5659 #  define BFD_TARGET_PAGESIZE	(4096)
5660 # endif
5661 
5662 	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5663 	     and the chains.  */
5664 	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5665 
5666 # if 1
5667 	  /* Variant 1: optimize for short chains.  We add the squares
5668 	     of all the chain lengths (which favors many small chain
5669 	     over a few long chains).  */
5670 	  for (j = 0; j < i; ++j)
5671 	    max += counts[j] * counts[j];
5672 
5673 	  /* This adds penalties for the overall size of the table.  */
5674 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5675 	  max *= fact * fact;
5676 # else
5677 	  /* Variant 2: Optimize a lot more for small table.  Here we
5678 	     also add squares of the size but we also add penalties for
5679 	     empty slots (the +1 term).  */
5680 	  for (j = 0; j < i; ++j)
5681 	    max += (1 + counts[j]) * (1 + counts[j]);
5682 
5683 	  /* The overall size of the table is considered, but not as
5684 	     strong as in variant 1, where it is squared.  */
5685 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5686 	  max *= fact;
5687 # endif
5688 
5689 	  /* Compare with current best results.  */
5690 	  if (max < best_chlen)
5691 	    {
5692 	      best_chlen = max;
5693 	      best_size = i;
5694 	      no_improvement_count = 0;
5695 	    }
5696 	  /* PR 11843: Avoid futile long searches for the best bucket size
5697 	     when there are a large number of symbols.  */
5698 	  else if (++no_improvement_count == 100)
5699 	    break;
5700 	}
5701 
5702       free (counts);
5703     }
5704   else
5705 #endif /* defined (BFD_HOST_U_64_BIT) */
5706     {
5707       /* This is the fallback solution if no 64bit type is available or if we
5708 	 are not supposed to spend much time on optimizations.  We select the
5709 	 bucket count using a fixed set of numbers.  */
5710       for (i = 0; elf_buckets[i] != 0; i++)
5711 	{
5712 	  best_size = elf_buckets[i];
5713 	  if (nsyms < elf_buckets[i + 1])
5714 	    break;
5715 	}
5716       if (gnu_hash && best_size < 2)
5717 	best_size = 2;
5718     }
5719 
5720   return best_size;
5721 }
5722 
5723 /* Size any SHT_GROUP section for ld -r.  */
5724 
5725 bfd_boolean
_bfd_elf_size_group_sections(struct bfd_link_info * info)5726 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5727 {
5728   bfd *ibfd;
5729 
5730   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5731     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5732 	&& !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5733       return FALSE;
5734   return TRUE;
5735 }
5736 
5737 /* Set a default stack segment size.  The value in INFO wins.  If it
5738    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5739    undefined it is initialized.  */
5740 
5741 bfd_boolean
bfd_elf_stack_segment_size(bfd * output_bfd,struct bfd_link_info * info,const char * legacy_symbol,bfd_vma default_size)5742 bfd_elf_stack_segment_size (bfd *output_bfd,
5743 			    struct bfd_link_info *info,
5744 			    const char *legacy_symbol,
5745 			    bfd_vma default_size)
5746 {
5747   struct elf_link_hash_entry *h = NULL;
5748 
5749   /* Look for legacy symbol.  */
5750   if (legacy_symbol)
5751     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5752 			      FALSE, FALSE, FALSE);
5753   if (h && (h->root.type == bfd_link_hash_defined
5754 	    || h->root.type == bfd_link_hash_defweak)
5755       && h->def_regular
5756       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5757     {
5758       /* The symbol has no type if specified on the command line.  */
5759       h->type = STT_OBJECT;
5760       if (info->stacksize)
5761 	(*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5762 			       output_bfd, legacy_symbol);
5763       else if (h->root.u.def.section != bfd_abs_section_ptr)
5764 	(*_bfd_error_handler) (_("%B: %s not absolute"),
5765 			       output_bfd, legacy_symbol);
5766       else
5767 	info->stacksize = h->root.u.def.value;
5768     }
5769 
5770   if (!info->stacksize)
5771     /* If the user didn't set a size, or explicitly inhibit the
5772        size, set it now.  */
5773     info->stacksize = default_size;
5774 
5775   /* Provide the legacy symbol, if it is referenced.  */
5776   if (h && (h->root.type == bfd_link_hash_undefined
5777 	    || h->root.type == bfd_link_hash_undefweak))
5778     {
5779       struct bfd_link_hash_entry *bh = NULL;
5780 
5781       if (!(_bfd_generic_link_add_one_symbol
5782 	    (info, output_bfd, legacy_symbol,
5783 	     BSF_GLOBAL, bfd_abs_section_ptr,
5784 	     info->stacksize >= 0 ? info->stacksize : 0,
5785 	     NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5786 	return FALSE;
5787 
5788       h = (struct elf_link_hash_entry *) bh;
5789       h->def_regular = 1;
5790       h->type = STT_OBJECT;
5791     }
5792 
5793   return TRUE;
5794 }
5795 
5796 /* Set up the sizes and contents of the ELF dynamic sections.  This is
5797    called by the ELF linker emulation before_allocation routine.  We
5798    must set the sizes of the sections before the linker sets the
5799    addresses of the various sections.  */
5800 
5801 bfd_boolean
bfd_elf_size_dynamic_sections(bfd * output_bfd,const char * soname,const char * rpath,const char * filter_shlib,const char * audit,const char * depaudit,const char * const * auxiliary_filters,struct bfd_link_info * info,asection ** sinterpptr)5802 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5803 			       const char *soname,
5804 			       const char *rpath,
5805 			       const char *filter_shlib,
5806 			       const char *audit,
5807 			       const char *depaudit,
5808 			       const char * const *auxiliary_filters,
5809 			       struct bfd_link_info *info,
5810 			       asection **sinterpptr)
5811 {
5812   size_t soname_indx;
5813   bfd *dynobj;
5814   const struct elf_backend_data *bed;
5815   struct elf_info_failed asvinfo;
5816 
5817   *sinterpptr = NULL;
5818 
5819   soname_indx = (size_t) -1;
5820 
5821   if (!is_elf_hash_table (info->hash))
5822     return TRUE;
5823 
5824   bed = get_elf_backend_data (output_bfd);
5825 
5826   /* Any syms created from now on start with -1 in
5827      got.refcount/offset and plt.refcount/offset.  */
5828   elf_hash_table (info)->init_got_refcount
5829     = elf_hash_table (info)->init_got_offset;
5830   elf_hash_table (info)->init_plt_refcount
5831     = elf_hash_table (info)->init_plt_offset;
5832 
5833   if (bfd_link_relocatable (info)
5834       && !_bfd_elf_size_group_sections (info))
5835     return FALSE;
5836 
5837   /* The backend may have to create some sections regardless of whether
5838      we're dynamic or not.  */
5839   if (bed->elf_backend_always_size_sections
5840       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5841     return FALSE;
5842 
5843   /* Determine any GNU_STACK segment requirements, after the backend
5844      has had a chance to set a default segment size.  */
5845   if (info->execstack)
5846     elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5847   else if (info->noexecstack)
5848     elf_stack_flags (output_bfd) = PF_R | PF_W;
5849   else
5850     {
5851       bfd *inputobj;
5852       asection *notesec = NULL;
5853       int exec = 0;
5854 
5855       for (inputobj = info->input_bfds;
5856 	   inputobj;
5857 	   inputobj = inputobj->link.next)
5858 	{
5859 	  asection *s;
5860 
5861 	  if (inputobj->flags
5862 	      & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5863 	    continue;
5864 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5865 	  if (s)
5866 	    {
5867 	      if (s->flags & SEC_CODE)
5868 		exec = PF_X;
5869 	      notesec = s;
5870 	    }
5871 	  else if (bed->default_execstack)
5872 	    exec = PF_X;
5873 	}
5874       if (notesec || info->stacksize > 0)
5875 	elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5876       if (notesec && exec && bfd_link_relocatable (info)
5877 	  && notesec->output_section != bfd_abs_section_ptr)
5878 	notesec->output_section->flags |= SEC_CODE;
5879     }
5880 
5881   dynobj = elf_hash_table (info)->dynobj;
5882 
5883   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5884     {
5885       struct elf_info_failed eif;
5886       struct elf_link_hash_entry *h;
5887       asection *dynstr;
5888       struct bfd_elf_version_tree *t;
5889       struct bfd_elf_version_expr *d;
5890       asection *s;
5891       bfd_boolean all_defined;
5892 
5893       *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5894       BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5895 
5896       if (soname != NULL)
5897 	{
5898 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5899 					     soname, TRUE);
5900 	  if (soname_indx == (size_t) -1
5901 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5902 	    return FALSE;
5903 	}
5904 
5905       if (info->symbolic)
5906 	{
5907 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5908 	    return FALSE;
5909 	  info->flags |= DF_SYMBOLIC;
5910 	}
5911 
5912       if (rpath != NULL)
5913 	{
5914 	  size_t indx;
5915 	  bfd_vma tag;
5916 
5917 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5918 				      TRUE);
5919 	  if (indx == (size_t) -1)
5920 	    return FALSE;
5921 
5922 	  tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5923 	  if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5924 	    return FALSE;
5925 	}
5926 
5927       if (filter_shlib != NULL)
5928 	{
5929 	  size_t indx;
5930 
5931 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5932 				      filter_shlib, TRUE);
5933 	  if (indx == (size_t) -1
5934 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5935 	    return FALSE;
5936 	}
5937 
5938       if (auxiliary_filters != NULL)
5939 	{
5940 	  const char * const *p;
5941 
5942 	  for (p = auxiliary_filters; *p != NULL; p++)
5943 	    {
5944 	      size_t indx;
5945 
5946 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5947 					  *p, TRUE);
5948 	      if (indx == (size_t) -1
5949 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5950 		return FALSE;
5951 	    }
5952 	}
5953 
5954       if (audit != NULL)
5955 	{
5956 	  size_t indx;
5957 
5958 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5959 				      TRUE);
5960 	  if (indx == (size_t) -1
5961 	      || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5962 	    return FALSE;
5963 	}
5964 
5965       if (depaudit != NULL)
5966 	{
5967 	  size_t indx;
5968 
5969 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5970 				      TRUE);
5971 	  if (indx == (size_t) -1
5972 	      || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5973 	    return FALSE;
5974 	}
5975 
5976       eif.info = info;
5977       eif.failed = FALSE;
5978 
5979       /* If we are supposed to export all symbols into the dynamic symbol
5980 	 table (this is not the normal case), then do so.  */
5981       if (info->export_dynamic
5982 	  || (bfd_link_executable (info) && info->dynamic))
5983 	{
5984 	  elf_link_hash_traverse (elf_hash_table (info),
5985 				  _bfd_elf_export_symbol,
5986 				  &eif);
5987 	  if (eif.failed)
5988 	    return FALSE;
5989 	}
5990 
5991       /* Make all global versions with definition.  */
5992       for (t = info->version_info; t != NULL; t = t->next)
5993 	for (d = t->globals.list; d != NULL; d = d->next)
5994 	  if (!d->symver && d->literal)
5995 	    {
5996 	      const char *verstr, *name;
5997 	      size_t namelen, verlen, newlen;
5998 	      char *newname, *p, leading_char;
5999 	      struct elf_link_hash_entry *newh;
6000 
6001 	      leading_char = bfd_get_symbol_leading_char (output_bfd);
6002 	      name = d->pattern;
6003 	      namelen = strlen (name) + (leading_char != '\0');
6004 	      verstr = t->name;
6005 	      verlen = strlen (verstr);
6006 	      newlen = namelen + verlen + 3;
6007 
6008 	      newname = (char *) bfd_malloc (newlen);
6009 	      if (newname == NULL)
6010 		return FALSE;
6011 	      newname[0] = leading_char;
6012 	      memcpy (newname + (leading_char != '\0'), name, namelen);
6013 
6014 	      /* Check the hidden versioned definition.  */
6015 	      p = newname + namelen;
6016 	      *p++ = ELF_VER_CHR;
6017 	      memcpy (p, verstr, verlen + 1);
6018 	      newh = elf_link_hash_lookup (elf_hash_table (info),
6019 					   newname, FALSE, FALSE,
6020 					   FALSE);
6021 	      if (newh == NULL
6022 		  || (newh->root.type != bfd_link_hash_defined
6023 		      && newh->root.type != bfd_link_hash_defweak))
6024 		{
6025 		  /* Check the default versioned definition.  */
6026 		  *p++ = ELF_VER_CHR;
6027 		  memcpy (p, verstr, verlen + 1);
6028 		  newh = elf_link_hash_lookup (elf_hash_table (info),
6029 					       newname, FALSE, FALSE,
6030 					       FALSE);
6031 		}
6032 	      free (newname);
6033 
6034 	      /* Mark this version if there is a definition and it is
6035 		 not defined in a shared object.  */
6036 	      if (newh != NULL
6037 		  && !newh->def_dynamic
6038 		  && (newh->root.type == bfd_link_hash_defined
6039 		      || newh->root.type == bfd_link_hash_defweak))
6040 		d->symver = 1;
6041 	    }
6042 
6043       /* Attach all the symbols to their version information.  */
6044       asvinfo.info = info;
6045       asvinfo.failed = FALSE;
6046 
6047       elf_link_hash_traverse (elf_hash_table (info),
6048 			      _bfd_elf_link_assign_sym_version,
6049 			      &asvinfo);
6050       if (asvinfo.failed)
6051 	return FALSE;
6052 
6053       if (!info->allow_undefined_version)
6054 	{
6055 	  /* Check if all global versions have a definition.  */
6056 	  all_defined = TRUE;
6057 	  for (t = info->version_info; t != NULL; t = t->next)
6058 	    for (d = t->globals.list; d != NULL; d = d->next)
6059 	      if (d->literal && !d->symver && !d->script)
6060 		{
6061 		  (*_bfd_error_handler)
6062 		    (_("%s: undefined version: %s"),
6063 		     d->pattern, t->name);
6064 		  all_defined = FALSE;
6065 		}
6066 
6067 	  if (!all_defined)
6068 	    {
6069 	      bfd_set_error (bfd_error_bad_value);
6070 	      return FALSE;
6071 	    }
6072 	}
6073 
6074       /* Find all symbols which were defined in a dynamic object and make
6075 	 the backend pick a reasonable value for them.  */
6076       elf_link_hash_traverse (elf_hash_table (info),
6077 			      _bfd_elf_adjust_dynamic_symbol,
6078 			      &eif);
6079       if (eif.failed)
6080 	return FALSE;
6081 
6082       /* Add some entries to the .dynamic section.  We fill in some of the
6083 	 values later, in bfd_elf_final_link, but we must add the entries
6084 	 now so that we know the final size of the .dynamic section.  */
6085 
6086       /* If there are initialization and/or finalization functions to
6087 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
6088       h = (info->init_function
6089 	   ? elf_link_hash_lookup (elf_hash_table (info),
6090 				   info->init_function, FALSE,
6091 				   FALSE, FALSE)
6092 	   : NULL);
6093       if (h != NULL
6094 	  && (h->ref_regular
6095 	      || h->def_regular))
6096 	{
6097 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6098 	    return FALSE;
6099 	}
6100       h = (info->fini_function
6101 	   ? elf_link_hash_lookup (elf_hash_table (info),
6102 				   info->fini_function, FALSE,
6103 				   FALSE, FALSE)
6104 	   : NULL);
6105       if (h != NULL
6106 	  && (h->ref_regular
6107 	      || h->def_regular))
6108 	{
6109 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6110 	    return FALSE;
6111 	}
6112 
6113       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6114       if (s != NULL && s->linker_has_input)
6115 	{
6116 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
6117 	  if (! bfd_link_executable (info))
6118 	    {
6119 	      bfd *sub;
6120 	      asection *o;
6121 
6122 	      for (sub = info->input_bfds; sub != NULL;
6123 		   sub = sub->link.next)
6124 		if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6125 		  for (o = sub->sections; o != NULL; o = o->next)
6126 		    if (elf_section_data (o)->this_hdr.sh_type
6127 			== SHT_PREINIT_ARRAY)
6128 		      {
6129 			(*_bfd_error_handler)
6130 			  (_("%B: .preinit_array section is not allowed in DSO"),
6131 			   sub);
6132 			break;
6133 		      }
6134 
6135 	      bfd_set_error (bfd_error_nonrepresentable_section);
6136 	      return FALSE;
6137 	    }
6138 
6139 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6140 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6141 	    return FALSE;
6142 	}
6143       s = bfd_get_section_by_name (output_bfd, ".init_array");
6144       if (s != NULL && s->linker_has_input)
6145 	{
6146 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6147 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6148 	    return FALSE;
6149 	}
6150       s = bfd_get_section_by_name (output_bfd, ".fini_array");
6151       if (s != NULL && s->linker_has_input)
6152 	{
6153 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6154 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6155 	    return FALSE;
6156 	}
6157 
6158       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6159       /* If .dynstr is excluded from the link, we don't want any of
6160 	 these tags.  Strictly, we should be checking each section
6161 	 individually;  This quick check covers for the case where
6162 	 someone does a /DISCARD/ : { *(*) }.  */
6163       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6164 	{
6165 	  bfd_size_type strsize;
6166 
6167 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6168 	  if ((info->emit_hash
6169 	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6170 	      || (info->emit_gnu_hash
6171 		  && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6172 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6173 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6174 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6175 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6176 					      bed->s->sizeof_sym))
6177 	    return FALSE;
6178 	}
6179     }
6180 
6181   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6182     return FALSE;
6183 
6184   /* The backend must work out the sizes of all the other dynamic
6185      sections.  */
6186   if (dynobj != NULL
6187       && bed->elf_backend_size_dynamic_sections != NULL
6188       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6189     return FALSE;
6190 
6191   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6192     {
6193       unsigned long section_sym_count;
6194       struct bfd_elf_version_tree *verdefs;
6195       asection *s;
6196 
6197       /* Set up the version definition section.  */
6198       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6199       BFD_ASSERT (s != NULL);
6200 
6201       /* We may have created additional version definitions if we are
6202 	 just linking a regular application.  */
6203       verdefs = info->version_info;
6204 
6205       /* Skip anonymous version tag.  */
6206       if (verdefs != NULL && verdefs->vernum == 0)
6207 	verdefs = verdefs->next;
6208 
6209       if (verdefs == NULL && !info->create_default_symver)
6210 	s->flags |= SEC_EXCLUDE;
6211       else
6212 	{
6213 	  unsigned int cdefs;
6214 	  bfd_size_type size;
6215 	  struct bfd_elf_version_tree *t;
6216 	  bfd_byte *p;
6217 	  Elf_Internal_Verdef def;
6218 	  Elf_Internal_Verdaux defaux;
6219 	  struct bfd_link_hash_entry *bh;
6220 	  struct elf_link_hash_entry *h;
6221 	  const char *name;
6222 
6223 	  cdefs = 0;
6224 	  size = 0;
6225 
6226 	  /* Make space for the base version.  */
6227 	  size += sizeof (Elf_External_Verdef);
6228 	  size += sizeof (Elf_External_Verdaux);
6229 	  ++cdefs;
6230 
6231 	  /* Make space for the default version.  */
6232 	  if (info->create_default_symver)
6233 	    {
6234 	      size += sizeof (Elf_External_Verdef);
6235 	      ++cdefs;
6236 	    }
6237 
6238 	  for (t = verdefs; t != NULL; t = t->next)
6239 	    {
6240 	      struct bfd_elf_version_deps *n;
6241 
6242 	      /* Don't emit base version twice.  */
6243 	      if (t->vernum == 0)
6244 		continue;
6245 
6246 	      size += sizeof (Elf_External_Verdef);
6247 	      size += sizeof (Elf_External_Verdaux);
6248 	      ++cdefs;
6249 
6250 	      for (n = t->deps; n != NULL; n = n->next)
6251 		size += sizeof (Elf_External_Verdaux);
6252 	    }
6253 
6254 	  s->size = size;
6255 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6256 	  if (s->contents == NULL && s->size != 0)
6257 	    return FALSE;
6258 
6259 	  /* Fill in the version definition section.  */
6260 
6261 	  p = s->contents;
6262 
6263 	  def.vd_version = VER_DEF_CURRENT;
6264 	  def.vd_flags = VER_FLG_BASE;
6265 	  def.vd_ndx = 1;
6266 	  def.vd_cnt = 1;
6267 	  if (info->create_default_symver)
6268 	    {
6269 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6270 	      def.vd_next = sizeof (Elf_External_Verdef);
6271 	    }
6272 	  else
6273 	    {
6274 	      def.vd_aux = sizeof (Elf_External_Verdef);
6275 	      def.vd_next = (sizeof (Elf_External_Verdef)
6276 			     + sizeof (Elf_External_Verdaux));
6277 	    }
6278 
6279 	  if (soname_indx != (size_t) -1)
6280 	    {
6281 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6282 				      soname_indx);
6283 	      def.vd_hash = bfd_elf_hash (soname);
6284 	      defaux.vda_name = soname_indx;
6285 	      name = soname;
6286 	    }
6287 	  else
6288 	    {
6289 	      size_t indx;
6290 
6291 	      name = lbasename (output_bfd->filename);
6292 	      def.vd_hash = bfd_elf_hash (name);
6293 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6294 					  name, FALSE);
6295 	      if (indx == (size_t) -1)
6296 		return FALSE;
6297 	      defaux.vda_name = indx;
6298 	    }
6299 	  defaux.vda_next = 0;
6300 
6301 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
6302 				    (Elf_External_Verdef *) p);
6303 	  p += sizeof (Elf_External_Verdef);
6304 	  if (info->create_default_symver)
6305 	    {
6306 	      /* Add a symbol representing this version.  */
6307 	      bh = NULL;
6308 	      if (! (_bfd_generic_link_add_one_symbol
6309 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6310 		      0, NULL, FALSE,
6311 		      get_elf_backend_data (dynobj)->collect, &bh)))
6312 		return FALSE;
6313 	      h = (struct elf_link_hash_entry *) bh;
6314 	      h->non_elf = 0;
6315 	      h->def_regular = 1;
6316 	      h->type = STT_OBJECT;
6317 	      h->verinfo.vertree = NULL;
6318 
6319 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6320 		return FALSE;
6321 
6322 	      /* Create a duplicate of the base version with the same
6323 		 aux block, but different flags.  */
6324 	      def.vd_flags = 0;
6325 	      def.vd_ndx = 2;
6326 	      def.vd_aux = sizeof (Elf_External_Verdef);
6327 	      if (verdefs)
6328 		def.vd_next = (sizeof (Elf_External_Verdef)
6329 			       + sizeof (Elf_External_Verdaux));
6330 	      else
6331 		def.vd_next = 0;
6332 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
6333 					(Elf_External_Verdef *) p);
6334 	      p += sizeof (Elf_External_Verdef);
6335 	    }
6336 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6337 				     (Elf_External_Verdaux *) p);
6338 	  p += sizeof (Elf_External_Verdaux);
6339 
6340 	  for (t = verdefs; t != NULL; t = t->next)
6341 	    {
6342 	      unsigned int cdeps;
6343 	      struct bfd_elf_version_deps *n;
6344 
6345 	      /* Don't emit the base version twice.  */
6346 	      if (t->vernum == 0)
6347 		continue;
6348 
6349 	      cdeps = 0;
6350 	      for (n = t->deps; n != NULL; n = n->next)
6351 		++cdeps;
6352 
6353 	      /* Add a symbol representing this version.  */
6354 	      bh = NULL;
6355 	      if (! (_bfd_generic_link_add_one_symbol
6356 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6357 		      0, NULL, FALSE,
6358 		      get_elf_backend_data (dynobj)->collect, &bh)))
6359 		return FALSE;
6360 	      h = (struct elf_link_hash_entry *) bh;
6361 	      h->non_elf = 0;
6362 	      h->def_regular = 1;
6363 	      h->type = STT_OBJECT;
6364 	      h->verinfo.vertree = t;
6365 
6366 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6367 		return FALSE;
6368 
6369 	      def.vd_version = VER_DEF_CURRENT;
6370 	      def.vd_flags = 0;
6371 	      if (t->globals.list == NULL
6372 		  && t->locals.list == NULL
6373 		  && ! t->used)
6374 		def.vd_flags |= VER_FLG_WEAK;
6375 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6376 	      def.vd_cnt = cdeps + 1;
6377 	      def.vd_hash = bfd_elf_hash (t->name);
6378 	      def.vd_aux = sizeof (Elf_External_Verdef);
6379 	      def.vd_next = 0;
6380 
6381 	      /* If a basever node is next, it *must* be the last node in
6382 		 the chain, otherwise Verdef construction breaks.  */
6383 	      if (t->next != NULL && t->next->vernum == 0)
6384 		BFD_ASSERT (t->next->next == NULL);
6385 
6386 	      if (t->next != NULL && t->next->vernum != 0)
6387 		def.vd_next = (sizeof (Elf_External_Verdef)
6388 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6389 
6390 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
6391 					(Elf_External_Verdef *) p);
6392 	      p += sizeof (Elf_External_Verdef);
6393 
6394 	      defaux.vda_name = h->dynstr_index;
6395 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6396 				      h->dynstr_index);
6397 	      defaux.vda_next = 0;
6398 	      if (t->deps != NULL)
6399 		defaux.vda_next = sizeof (Elf_External_Verdaux);
6400 	      t->name_indx = defaux.vda_name;
6401 
6402 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6403 					 (Elf_External_Verdaux *) p);
6404 	      p += sizeof (Elf_External_Verdaux);
6405 
6406 	      for (n = t->deps; n != NULL; n = n->next)
6407 		{
6408 		  if (n->version_needed == NULL)
6409 		    {
6410 		      /* This can happen if there was an error in the
6411 			 version script.  */
6412 		      defaux.vda_name = 0;
6413 		    }
6414 		  else
6415 		    {
6416 		      defaux.vda_name = n->version_needed->name_indx;
6417 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6418 					      defaux.vda_name);
6419 		    }
6420 		  if (n->next == NULL)
6421 		    defaux.vda_next = 0;
6422 		  else
6423 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
6424 
6425 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6426 					     (Elf_External_Verdaux *) p);
6427 		  p += sizeof (Elf_External_Verdaux);
6428 		}
6429 	    }
6430 
6431 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6432 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6433 	    return FALSE;
6434 
6435 	  elf_tdata (output_bfd)->cverdefs = cdefs;
6436 	}
6437 
6438       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6439 	{
6440 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6441 	    return FALSE;
6442 	}
6443       else if (info->flags & DF_BIND_NOW)
6444 	{
6445 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6446 	    return FALSE;
6447 	}
6448 
6449       if (info->flags_1)
6450 	{
6451 	  if (bfd_link_executable (info))
6452 	    info->flags_1 &= ~ (DF_1_INITFIRST
6453 				| DF_1_NODELETE
6454 				| DF_1_NOOPEN);
6455 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6456 	    return FALSE;
6457 	}
6458 
6459       /* Work out the size of the version reference section.  */
6460 
6461       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6462       BFD_ASSERT (s != NULL);
6463       {
6464 	struct elf_find_verdep_info sinfo;
6465 
6466 	sinfo.info = info;
6467 	sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6468 	if (sinfo.vers == 0)
6469 	  sinfo.vers = 1;
6470 	sinfo.failed = FALSE;
6471 
6472 	elf_link_hash_traverse (elf_hash_table (info),
6473 				_bfd_elf_link_find_version_dependencies,
6474 				&sinfo);
6475 	if (sinfo.failed)
6476 	  return FALSE;
6477 
6478 	if (elf_tdata (output_bfd)->verref == NULL)
6479 	  s->flags |= SEC_EXCLUDE;
6480 	else
6481 	  {
6482 	    Elf_Internal_Verneed *t;
6483 	    unsigned int size;
6484 	    unsigned int crefs;
6485 	    bfd_byte *p;
6486 
6487 	    /* Build the version dependency section.  */
6488 	    size = 0;
6489 	    crefs = 0;
6490 	    for (t = elf_tdata (output_bfd)->verref;
6491 		 t != NULL;
6492 		 t = t->vn_nextref)
6493 	      {
6494 		Elf_Internal_Vernaux *a;
6495 
6496 		size += sizeof (Elf_External_Verneed);
6497 		++crefs;
6498 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6499 		  size += sizeof (Elf_External_Vernaux);
6500 	      }
6501 
6502 	    s->size = size;
6503 	    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6504 	    if (s->contents == NULL)
6505 	      return FALSE;
6506 
6507 	    p = s->contents;
6508 	    for (t = elf_tdata (output_bfd)->verref;
6509 		 t != NULL;
6510 		 t = t->vn_nextref)
6511 	      {
6512 		unsigned int caux;
6513 		Elf_Internal_Vernaux *a;
6514 		size_t indx;
6515 
6516 		caux = 0;
6517 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6518 		  ++caux;
6519 
6520 		t->vn_version = VER_NEED_CURRENT;
6521 		t->vn_cnt = caux;
6522 		indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6523 					    elf_dt_name (t->vn_bfd) != NULL
6524 					    ? elf_dt_name (t->vn_bfd)
6525 					    : lbasename (t->vn_bfd->filename),
6526 					    FALSE);
6527 		if (indx == (size_t) -1)
6528 		  return FALSE;
6529 		t->vn_file = indx;
6530 		t->vn_aux = sizeof (Elf_External_Verneed);
6531 		if (t->vn_nextref == NULL)
6532 		  t->vn_next = 0;
6533 		else
6534 		  t->vn_next = (sizeof (Elf_External_Verneed)
6535 				+ caux * sizeof (Elf_External_Vernaux));
6536 
6537 		_bfd_elf_swap_verneed_out (output_bfd, t,
6538 					   (Elf_External_Verneed *) p);
6539 		p += sizeof (Elf_External_Verneed);
6540 
6541 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6542 		  {
6543 		    a->vna_hash = bfd_elf_hash (a->vna_nodename);
6544 		    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6545 						a->vna_nodename, FALSE);
6546 		    if (indx == (size_t) -1)
6547 		      return FALSE;
6548 		    a->vna_name = indx;
6549 		    if (a->vna_nextptr == NULL)
6550 		      a->vna_next = 0;
6551 		    else
6552 		      a->vna_next = sizeof (Elf_External_Vernaux);
6553 
6554 		    _bfd_elf_swap_vernaux_out (output_bfd, a,
6555 					       (Elf_External_Vernaux *) p);
6556 		    p += sizeof (Elf_External_Vernaux);
6557 		  }
6558 	      }
6559 
6560 	    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6561 		|| !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6562 	      return FALSE;
6563 
6564 	    elf_tdata (output_bfd)->cverrefs = crefs;
6565 	  }
6566       }
6567 
6568       if ((elf_tdata (output_bfd)->cverrefs == 0
6569 	   && elf_tdata (output_bfd)->cverdefs == 0)
6570 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6571 					     &section_sym_count) == 0)
6572 	{
6573 	  s = bfd_get_linker_section (dynobj, ".gnu.version");
6574 	  s->flags |= SEC_EXCLUDE;
6575 	}
6576     }
6577   return TRUE;
6578 }
6579 
6580 /* Find the first non-excluded output section.  We'll use its
6581    section symbol for some emitted relocs.  */
6582 void
_bfd_elf_init_1_index_section(bfd * output_bfd,struct bfd_link_info * info)6583 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6584 {
6585   asection *s;
6586 
6587   for (s = output_bfd->sections; s != NULL; s = s->next)
6588     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6589 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6590       {
6591 	elf_hash_table (info)->text_index_section = s;
6592 	break;
6593       }
6594 }
6595 
6596 /* Find two non-excluded output sections, one for code, one for data.
6597    We'll use their section symbols for some emitted relocs.  */
6598 void
_bfd_elf_init_2_index_sections(bfd * output_bfd,struct bfd_link_info * info)6599 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6600 {
6601   asection *s;
6602 
6603   /* Data first, since setting text_index_section changes
6604      _bfd_elf_link_omit_section_dynsym.  */
6605   for (s = output_bfd->sections; s != NULL; s = s->next)
6606     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6607 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6608       {
6609 	elf_hash_table (info)->data_index_section = s;
6610 	break;
6611       }
6612 
6613   for (s = output_bfd->sections; s != NULL; s = s->next)
6614     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6615 	 == (SEC_ALLOC | SEC_READONLY))
6616 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6617       {
6618 	elf_hash_table (info)->text_index_section = s;
6619 	break;
6620       }
6621 
6622   if (elf_hash_table (info)->text_index_section == NULL)
6623     elf_hash_table (info)->text_index_section
6624       = elf_hash_table (info)->data_index_section;
6625 }
6626 
6627 bfd_boolean
bfd_elf_size_dynsym_hash_dynstr(bfd * output_bfd,struct bfd_link_info * info)6628 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6629 {
6630   const struct elf_backend_data *bed;
6631 
6632   if (!is_elf_hash_table (info->hash))
6633     return TRUE;
6634 
6635   bed = get_elf_backend_data (output_bfd);
6636   (*bed->elf_backend_init_index_section) (output_bfd, info);
6637 
6638   if (elf_hash_table (info)->dynamic_sections_created)
6639     {
6640       bfd *dynobj;
6641       asection *s;
6642       bfd_size_type dynsymcount;
6643       unsigned long section_sym_count;
6644       unsigned int dtagcount;
6645 
6646       dynobj = elf_hash_table (info)->dynobj;
6647 
6648       /* Assign dynsym indicies.  In a shared library we generate a
6649 	 section symbol for each output section, which come first.
6650 	 Next come all of the back-end allocated local dynamic syms,
6651 	 followed by the rest of the global symbols.  */
6652 
6653       dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6654 						    &section_sym_count);
6655 
6656       /* Work out the size of the symbol version section.  */
6657       s = bfd_get_linker_section (dynobj, ".gnu.version");
6658       BFD_ASSERT (s != NULL);
6659       if ((s->flags & SEC_EXCLUDE) == 0)
6660 	{
6661 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
6662 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6663 	  if (s->contents == NULL)
6664 	    return FALSE;
6665 
6666 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6667 	    return FALSE;
6668 	}
6669 
6670       /* Set the size of the .dynsym and .hash sections.  We counted
6671 	 the number of dynamic symbols in elf_link_add_object_symbols.
6672 	 We will build the contents of .dynsym and .hash when we build
6673 	 the final symbol table, because until then we do not know the
6674 	 correct value to give the symbols.  We built the .dynstr
6675 	 section as we went along in elf_link_add_object_symbols.  */
6676       s = elf_hash_table (info)->dynsym;
6677       BFD_ASSERT (s != NULL);
6678       s->size = dynsymcount * bed->s->sizeof_sym;
6679 
6680       s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6681       if (s->contents == NULL)
6682 	return FALSE;
6683 
6684       /* The first entry in .dynsym is a dummy symbol.  Clear all the
6685 	 section syms, in case we don't output them all.  */
6686       ++section_sym_count;
6687       memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6688 
6689       elf_hash_table (info)->bucketcount = 0;
6690 
6691       /* Compute the size of the hashing table.  As a side effect this
6692 	 computes the hash values for all the names we export.  */
6693       if (info->emit_hash)
6694 	{
6695 	  unsigned long int *hashcodes;
6696 	  struct hash_codes_info hashinf;
6697 	  bfd_size_type amt;
6698 	  unsigned long int nsyms;
6699 	  size_t bucketcount;
6700 	  size_t hash_entry_size;
6701 
6702 	  /* Compute the hash values for all exported symbols.  At the same
6703 	     time store the values in an array so that we could use them for
6704 	     optimizations.  */
6705 	  amt = dynsymcount * sizeof (unsigned long int);
6706 	  hashcodes = (unsigned long int *) bfd_malloc (amt);
6707 	  if (hashcodes == NULL)
6708 	    return FALSE;
6709 	  hashinf.hashcodes = hashcodes;
6710 	  hashinf.error = FALSE;
6711 
6712 	  /* Put all hash values in HASHCODES.  */
6713 	  elf_link_hash_traverse (elf_hash_table (info),
6714 				  elf_collect_hash_codes, &hashinf);
6715 	  if (hashinf.error)
6716 	    {
6717 	      free (hashcodes);
6718 	      return FALSE;
6719 	    }
6720 
6721 	  nsyms = hashinf.hashcodes - hashcodes;
6722 	  bucketcount
6723 	    = compute_bucket_count (info, hashcodes, nsyms, 0);
6724 	  free (hashcodes);
6725 
6726 	  if (bucketcount == 0)
6727 	    return FALSE;
6728 
6729 	  elf_hash_table (info)->bucketcount = bucketcount;
6730 
6731 	  s = bfd_get_linker_section (dynobj, ".hash");
6732 	  BFD_ASSERT (s != NULL);
6733 	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6734 	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6735 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6736 	  if (s->contents == NULL)
6737 	    return FALSE;
6738 
6739 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6740 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6741 		   s->contents + hash_entry_size);
6742 	}
6743 
6744       if (info->emit_gnu_hash)
6745 	{
6746 	  size_t i, cnt;
6747 	  unsigned char *contents;
6748 	  struct collect_gnu_hash_codes cinfo;
6749 	  bfd_size_type amt;
6750 	  size_t bucketcount;
6751 
6752 	  memset (&cinfo, 0, sizeof (cinfo));
6753 
6754 	  /* Compute the hash values for all exported symbols.  At the same
6755 	     time store the values in an array so that we could use them for
6756 	     optimizations.  */
6757 	  amt = dynsymcount * 2 * sizeof (unsigned long int);
6758 	  cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6759 	  if (cinfo.hashcodes == NULL)
6760 	    return FALSE;
6761 
6762 	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
6763 	  cinfo.min_dynindx = -1;
6764 	  cinfo.output_bfd = output_bfd;
6765 	  cinfo.bed = bed;
6766 
6767 	  /* Put all hash values in HASHCODES.  */
6768 	  elf_link_hash_traverse (elf_hash_table (info),
6769 				  elf_collect_gnu_hash_codes, &cinfo);
6770 	  if (cinfo.error)
6771 	    {
6772 	      free (cinfo.hashcodes);
6773 	      return FALSE;
6774 	    }
6775 
6776 	  bucketcount
6777 	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6778 
6779 	  if (bucketcount == 0)
6780 	    {
6781 	      free (cinfo.hashcodes);
6782 	      return FALSE;
6783 	    }
6784 
6785 	  s = bfd_get_linker_section (dynobj, ".gnu.hash");
6786 	  BFD_ASSERT (s != NULL);
6787 
6788 	  if (cinfo.nsyms == 0)
6789 	    {
6790 	      /* Empty .gnu.hash section is special.  */
6791 	      BFD_ASSERT (cinfo.min_dynindx == -1);
6792 	      free (cinfo.hashcodes);
6793 	      s->size = 5 * 4 + bed->s->arch_size / 8;
6794 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6795 	      if (contents == NULL)
6796 		return FALSE;
6797 	      s->contents = contents;
6798 	      /* 1 empty bucket.  */
6799 	      bfd_put_32 (output_bfd, 1, contents);
6800 	      /* SYMIDX above the special symbol 0.  */
6801 	      bfd_put_32 (output_bfd, 1, contents + 4);
6802 	      /* Just one word for bitmask.  */
6803 	      bfd_put_32 (output_bfd, 1, contents + 8);
6804 	      /* Only hash fn bloom filter.  */
6805 	      bfd_put_32 (output_bfd, 0, contents + 12);
6806 	      /* No hashes are valid - empty bitmask.  */
6807 	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6808 	      /* No hashes in the only bucket.  */
6809 	      bfd_put_32 (output_bfd, 0,
6810 			  contents + 16 + bed->s->arch_size / 8);
6811 	    }
6812 	  else
6813 	    {
6814 	      unsigned long int maskwords, maskbitslog2, x;
6815 	      BFD_ASSERT (cinfo.min_dynindx != -1);
6816 
6817 	      x = cinfo.nsyms;
6818 	      maskbitslog2 = 1;
6819 	      while ((x >>= 1) != 0)
6820 		++maskbitslog2;
6821 	      if (maskbitslog2 < 3)
6822 		maskbitslog2 = 5;
6823 	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6824 		maskbitslog2 = maskbitslog2 + 3;
6825 	      else
6826 		maskbitslog2 = maskbitslog2 + 2;
6827 	      if (bed->s->arch_size == 64)
6828 		{
6829 		  if (maskbitslog2 == 5)
6830 		    maskbitslog2 = 6;
6831 		  cinfo.shift1 = 6;
6832 		}
6833 	      else
6834 		cinfo.shift1 = 5;
6835 	      cinfo.mask = (1 << cinfo.shift1) - 1;
6836 	      cinfo.shift2 = maskbitslog2;
6837 	      cinfo.maskbits = 1 << maskbitslog2;
6838 	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6839 	      amt = bucketcount * sizeof (unsigned long int) * 2;
6840 	      amt += maskwords * sizeof (bfd_vma);
6841 	      cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6842 	      if (cinfo.bitmask == NULL)
6843 		{
6844 		  free (cinfo.hashcodes);
6845 		  return FALSE;
6846 		}
6847 
6848 	      cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6849 	      cinfo.indx = cinfo.counts + bucketcount;
6850 	      cinfo.symindx = dynsymcount - cinfo.nsyms;
6851 	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6852 
6853 	      /* Determine how often each hash bucket is used.  */
6854 	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6855 	      for (i = 0; i < cinfo.nsyms; ++i)
6856 		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6857 
6858 	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6859 		if (cinfo.counts[i] != 0)
6860 		  {
6861 		    cinfo.indx[i] = cnt;
6862 		    cnt += cinfo.counts[i];
6863 		  }
6864 	      BFD_ASSERT (cnt == dynsymcount);
6865 	      cinfo.bucketcount = bucketcount;
6866 	      cinfo.local_indx = cinfo.min_dynindx;
6867 
6868 	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6869 	      s->size += cinfo.maskbits / 8;
6870 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6871 	      if (contents == NULL)
6872 		{
6873 		  free (cinfo.bitmask);
6874 		  free (cinfo.hashcodes);
6875 		  return FALSE;
6876 		}
6877 
6878 	      s->contents = contents;
6879 	      bfd_put_32 (output_bfd, bucketcount, contents);
6880 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6881 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
6882 	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6883 	      contents += 16 + cinfo.maskbits / 8;
6884 
6885 	      for (i = 0; i < bucketcount; ++i)
6886 		{
6887 		  if (cinfo.counts[i] == 0)
6888 		    bfd_put_32 (output_bfd, 0, contents);
6889 		  else
6890 		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6891 		  contents += 4;
6892 		}
6893 
6894 	      cinfo.contents = contents;
6895 
6896 	      /* Renumber dynamic symbols, populate .gnu.hash section.  */
6897 	      elf_link_hash_traverse (elf_hash_table (info),
6898 				      elf_renumber_gnu_hash_syms, &cinfo);
6899 
6900 	      contents = s->contents + 16;
6901 	      for (i = 0; i < maskwords; ++i)
6902 		{
6903 		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6904 			   contents);
6905 		  contents += bed->s->arch_size / 8;
6906 		}
6907 
6908 	      free (cinfo.bitmask);
6909 	      free (cinfo.hashcodes);
6910 	    }
6911 	}
6912 
6913       s = bfd_get_linker_section (dynobj, ".dynstr");
6914       BFD_ASSERT (s != NULL);
6915 
6916       elf_finalize_dynstr (output_bfd, info);
6917 
6918       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6919 
6920       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6921 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6922 	  return FALSE;
6923     }
6924 
6925   return TRUE;
6926 }
6927 
6928 /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
6929 
6930 static void
merge_sections_remove_hook(bfd * abfd ATTRIBUTE_UNUSED,asection * sec)6931 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6932 			    asection *sec)
6933 {
6934   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6935   sec->sec_info_type = SEC_INFO_TYPE_NONE;
6936 }
6937 
6938 /* Finish SHF_MERGE section merging.  */
6939 
6940 bfd_boolean
_bfd_elf_merge_sections(bfd * obfd,struct bfd_link_info * info)6941 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6942 {
6943   bfd *ibfd;
6944   asection *sec;
6945 
6946   if (!is_elf_hash_table (info->hash))
6947     return FALSE;
6948 
6949   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6950     if ((ibfd->flags & DYNAMIC) == 0
6951 	&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6952 	&& (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6953 	    == get_elf_backend_data (obfd)->s->elfclass))
6954       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6955 	if ((sec->flags & SEC_MERGE) != 0
6956 	    && !bfd_is_abs_section (sec->output_section))
6957 	  {
6958 	    struct bfd_elf_section_data *secdata;
6959 
6960 	    secdata = elf_section_data (sec);
6961 	    if (! _bfd_add_merge_section (obfd,
6962 					  &elf_hash_table (info)->merge_info,
6963 					  sec, &secdata->sec_info))
6964 	      return FALSE;
6965 	    else if (secdata->sec_info)
6966 	      sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6967 	  }
6968 
6969   if (elf_hash_table (info)->merge_info != NULL)
6970     _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6971 			 merge_sections_remove_hook);
6972   return TRUE;
6973 }
6974 
6975 /* Create an entry in an ELF linker hash table.  */
6976 
6977 struct bfd_hash_entry *
_bfd_elf_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)6978 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6979 			    struct bfd_hash_table *table,
6980 			    const char *string)
6981 {
6982   /* Allocate the structure if it has not already been allocated by a
6983      subclass.  */
6984   if (entry == NULL)
6985     {
6986       entry = (struct bfd_hash_entry *)
6987 	bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6988       if (entry == NULL)
6989 	return entry;
6990     }
6991 
6992   /* Call the allocation method of the superclass.  */
6993   entry = _bfd_link_hash_newfunc (entry, table, string);
6994   if (entry != NULL)
6995     {
6996       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6997       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6998 
6999       /* Set local fields.  */
7000       ret->indx = -1;
7001       ret->dynindx = -1;
7002       ret->got = htab->init_got_refcount;
7003       ret->plt = htab->init_plt_refcount;
7004       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7005 			      - offsetof (struct elf_link_hash_entry, size)));
7006       /* Assume that we have been called by a non-ELF symbol reader.
7007 	 This flag is then reset by the code which reads an ELF input
7008 	 file.  This ensures that a symbol created by a non-ELF symbol
7009 	 reader will have the flag set correctly.  */
7010       ret->non_elf = 1;
7011     }
7012 
7013   return entry;
7014 }
7015 
7016 /* Copy data from an indirect symbol to its direct symbol, hiding the
7017    old indirect symbol.  Also used for copying flags to a weakdef.  */
7018 
7019 void
_bfd_elf_link_hash_copy_indirect(struct bfd_link_info * info,struct elf_link_hash_entry * dir,struct elf_link_hash_entry * ind)7020 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7021 				  struct elf_link_hash_entry *dir,
7022 				  struct elf_link_hash_entry *ind)
7023 {
7024   struct elf_link_hash_table *htab;
7025 
7026   /* Copy down any references that we may have already seen to the
7027      symbol which just became indirect if DIR isn't a hidden versioned
7028      symbol.  */
7029 
7030   if (dir->versioned != versioned_hidden)
7031     {
7032       dir->ref_dynamic |= ind->ref_dynamic;
7033       dir->ref_regular |= ind->ref_regular;
7034       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7035       dir->non_got_ref |= ind->non_got_ref;
7036       dir->needs_plt |= ind->needs_plt;
7037       dir->pointer_equality_needed |= ind->pointer_equality_needed;
7038     }
7039 
7040   if (ind->root.type != bfd_link_hash_indirect)
7041     return;
7042 
7043   /* Copy over the global and procedure linkage table refcount entries.
7044      These may have been already set up by a check_relocs routine.  */
7045   htab = elf_hash_table (info);
7046   if (ind->got.refcount > htab->init_got_refcount.refcount)
7047     {
7048       if (dir->got.refcount < 0)
7049 	dir->got.refcount = 0;
7050       dir->got.refcount += ind->got.refcount;
7051       ind->got.refcount = htab->init_got_refcount.refcount;
7052     }
7053 
7054   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7055     {
7056       if (dir->plt.refcount < 0)
7057 	dir->plt.refcount = 0;
7058       dir->plt.refcount += ind->plt.refcount;
7059       ind->plt.refcount = htab->init_plt_refcount.refcount;
7060     }
7061 
7062   if (ind->dynindx != -1)
7063     {
7064       if (dir->dynindx != -1)
7065 	_bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7066       dir->dynindx = ind->dynindx;
7067       dir->dynstr_index = ind->dynstr_index;
7068       ind->dynindx = -1;
7069       ind->dynstr_index = 0;
7070     }
7071 }
7072 
7073 void
_bfd_elf_link_hash_hide_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h,bfd_boolean force_local)7074 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7075 				struct elf_link_hash_entry *h,
7076 				bfd_boolean force_local)
7077 {
7078   /* STT_GNU_IFUNC symbol must go through PLT.  */
7079   if (h->type != STT_GNU_IFUNC)
7080     {
7081       h->plt = elf_hash_table (info)->init_plt_offset;
7082       h->needs_plt = 0;
7083     }
7084   if (force_local)
7085     {
7086       h->forced_local = 1;
7087       if (h->dynindx != -1)
7088 	{
7089 	  h->dynindx = -1;
7090 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7091 				  h->dynstr_index);
7092 	}
7093     }
7094 }
7095 
7096 /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
7097    caller.  */
7098 
7099 bfd_boolean
_bfd_elf_link_hash_table_init(struct elf_link_hash_table * table,bfd * abfd,struct bfd_hash_entry * (* newfunc)(struct bfd_hash_entry *,struct bfd_hash_table *,const char *),unsigned int entsize,enum elf_target_id target_id)7100 _bfd_elf_link_hash_table_init
7101   (struct elf_link_hash_table *table,
7102    bfd *abfd,
7103    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7104 				      struct bfd_hash_table *,
7105 				      const char *),
7106    unsigned int entsize,
7107    enum elf_target_id target_id)
7108 {
7109   bfd_boolean ret;
7110   int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7111 
7112   table->init_got_refcount.refcount = can_refcount - 1;
7113   table->init_plt_refcount.refcount = can_refcount - 1;
7114   table->init_got_offset.offset = -(bfd_vma) 1;
7115   table->init_plt_offset.offset = -(bfd_vma) 1;
7116   /* The first dynamic symbol is a dummy.  */
7117   table->dynsymcount = 1;
7118 
7119   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7120 
7121   table->root.type = bfd_link_elf_hash_table;
7122   table->hash_table_id = target_id;
7123 
7124   return ret;
7125 }
7126 
7127 /* Create an ELF linker hash table.  */
7128 
7129 struct bfd_link_hash_table *
_bfd_elf_link_hash_table_create(bfd * abfd)7130 _bfd_elf_link_hash_table_create (bfd *abfd)
7131 {
7132   struct elf_link_hash_table *ret;
7133   bfd_size_type amt = sizeof (struct elf_link_hash_table);
7134 
7135   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7136   if (ret == NULL)
7137     return NULL;
7138 
7139   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7140 				       sizeof (struct elf_link_hash_entry),
7141 				       GENERIC_ELF_DATA))
7142     {
7143       free (ret);
7144       return NULL;
7145     }
7146   ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7147 
7148   return &ret->root;
7149 }
7150 
7151 /* Destroy an ELF linker hash table.  */
7152 
7153 void
_bfd_elf_link_hash_table_free(bfd * obfd)7154 _bfd_elf_link_hash_table_free (bfd *obfd)
7155 {
7156   struct elf_link_hash_table *htab;
7157 
7158   htab = (struct elf_link_hash_table *) obfd->link.hash;
7159   if (htab->dynstr != NULL)
7160     _bfd_elf_strtab_free (htab->dynstr);
7161   _bfd_merge_sections_free (htab->merge_info);
7162   _bfd_generic_link_hash_table_free (obfd);
7163 }
7164 
7165 /* This is a hook for the ELF emulation code in the generic linker to
7166    tell the backend linker what file name to use for the DT_NEEDED
7167    entry for a dynamic object.  */
7168 
7169 void
bfd_elf_set_dt_needed_name(bfd * abfd,const char * name)7170 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7171 {
7172   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7173       && bfd_get_format (abfd) == bfd_object)
7174     elf_dt_name (abfd) = name;
7175 }
7176 
7177 int
bfd_elf_get_dyn_lib_class(bfd * abfd)7178 bfd_elf_get_dyn_lib_class (bfd *abfd)
7179 {
7180   int lib_class;
7181   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7182       && bfd_get_format (abfd) == bfd_object)
7183     lib_class = elf_dyn_lib_class (abfd);
7184   else
7185     lib_class = 0;
7186   return lib_class;
7187 }
7188 
7189 void
bfd_elf_set_dyn_lib_class(bfd * abfd,enum dynamic_lib_link_class lib_class)7190 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7191 {
7192   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7193       && bfd_get_format (abfd) == bfd_object)
7194     elf_dyn_lib_class (abfd) = lib_class;
7195 }
7196 
7197 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
7198    the linker ELF emulation code.  */
7199 
7200 struct bfd_link_needed_list *
bfd_elf_get_needed_list(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)7201 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7202 			 struct bfd_link_info *info)
7203 {
7204   if (! is_elf_hash_table (info->hash))
7205     return NULL;
7206   return elf_hash_table (info)->needed;
7207 }
7208 
7209 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
7210    hook for the linker ELF emulation code.  */
7211 
7212 struct bfd_link_needed_list *
bfd_elf_get_runpath_list(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)7213 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7214 			  struct bfd_link_info *info)
7215 {
7216   if (! is_elf_hash_table (info->hash))
7217     return NULL;
7218   return elf_hash_table (info)->runpath;
7219 }
7220 
7221 /* Get the name actually used for a dynamic object for a link.  This
7222    is the SONAME entry if there is one.  Otherwise, it is the string
7223    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
7224 
7225 const char *
bfd_elf_get_dt_soname(bfd * abfd)7226 bfd_elf_get_dt_soname (bfd *abfd)
7227 {
7228   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7229       && bfd_get_format (abfd) == bfd_object)
7230     return elf_dt_name (abfd);
7231   return NULL;
7232 }
7233 
7234 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
7235    the ELF linker emulation code.  */
7236 
7237 bfd_boolean
bfd_elf_get_bfd_needed_list(bfd * abfd,struct bfd_link_needed_list ** pneeded)7238 bfd_elf_get_bfd_needed_list (bfd *abfd,
7239 			     struct bfd_link_needed_list **pneeded)
7240 {
7241   asection *s;
7242   bfd_byte *dynbuf = NULL;
7243   unsigned int elfsec;
7244   unsigned long shlink;
7245   bfd_byte *extdyn, *extdynend;
7246   size_t extdynsize;
7247   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7248 
7249   *pneeded = NULL;
7250 
7251   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7252       || bfd_get_format (abfd) != bfd_object)
7253     return TRUE;
7254 
7255   s = bfd_get_section_by_name (abfd, ".dynamic");
7256   if (s == NULL || s->size == 0)
7257     return TRUE;
7258 
7259   if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7260     goto error_return;
7261 
7262   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7263   if (elfsec == SHN_BAD)
7264     goto error_return;
7265 
7266   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7267 
7268   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7269   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7270 
7271   extdyn = dynbuf;
7272   extdynend = extdyn + s->size;
7273   for (; extdyn < extdynend; extdyn += extdynsize)
7274     {
7275       Elf_Internal_Dyn dyn;
7276 
7277       (*swap_dyn_in) (abfd, extdyn, &dyn);
7278 
7279       if (dyn.d_tag == DT_NULL)
7280 	break;
7281 
7282       if (dyn.d_tag == DT_NEEDED)
7283 	{
7284 	  const char *string;
7285 	  struct bfd_link_needed_list *l;
7286 	  unsigned int tagv = dyn.d_un.d_val;
7287 	  bfd_size_type amt;
7288 
7289 	  string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7290 	  if (string == NULL)
7291 	    goto error_return;
7292 
7293 	  amt = sizeof *l;
7294 	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7295 	  if (l == NULL)
7296 	    goto error_return;
7297 
7298 	  l->by = abfd;
7299 	  l->name = string;
7300 	  l->next = *pneeded;
7301 	  *pneeded = l;
7302 	}
7303     }
7304 
7305   free (dynbuf);
7306 
7307   return TRUE;
7308 
7309  error_return:
7310   if (dynbuf != NULL)
7311     free (dynbuf);
7312   return FALSE;
7313 }
7314 
7315 struct elf_symbuf_symbol
7316 {
7317   unsigned long st_name;	/* Symbol name, index in string tbl */
7318   unsigned char st_info;	/* Type and binding attributes */
7319   unsigned char st_other;	/* Visibilty, and target specific */
7320 };
7321 
7322 struct elf_symbuf_head
7323 {
7324   struct elf_symbuf_symbol *ssym;
7325   size_t count;
7326   unsigned int st_shndx;
7327 };
7328 
7329 struct elf_symbol
7330 {
7331   union
7332     {
7333       Elf_Internal_Sym *isym;
7334       struct elf_symbuf_symbol *ssym;
7335     } u;
7336   const char *name;
7337 };
7338 
7339 /* Sort references to symbols by ascending section number.  */
7340 
7341 static int
elf_sort_elf_symbol(const void * arg1,const void * arg2)7342 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7343 {
7344   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7345   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7346 
7347   return s1->st_shndx - s2->st_shndx;
7348 }
7349 
7350 static int
elf_sym_name_compare(const void * arg1,const void * arg2)7351 elf_sym_name_compare (const void *arg1, const void *arg2)
7352 {
7353   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7354   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7355   return strcmp (s1->name, s2->name);
7356 }
7357 
7358 static struct elf_symbuf_head *
elf_create_symbuf(size_t symcount,Elf_Internal_Sym * isymbuf)7359 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7360 {
7361   Elf_Internal_Sym **ind, **indbufend, **indbuf;
7362   struct elf_symbuf_symbol *ssym;
7363   struct elf_symbuf_head *ssymbuf, *ssymhead;
7364   size_t i, shndx_count, total_size;
7365 
7366   indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7367   if (indbuf == NULL)
7368     return NULL;
7369 
7370   for (ind = indbuf, i = 0; i < symcount; i++)
7371     if (isymbuf[i].st_shndx != SHN_UNDEF)
7372       *ind++ = &isymbuf[i];
7373   indbufend = ind;
7374 
7375   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7376 	 elf_sort_elf_symbol);
7377 
7378   shndx_count = 0;
7379   if (indbufend > indbuf)
7380     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7381       if (ind[0]->st_shndx != ind[1]->st_shndx)
7382 	shndx_count++;
7383 
7384   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7385 		+ (indbufend - indbuf) * sizeof (*ssym));
7386   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7387   if (ssymbuf == NULL)
7388     {
7389       free (indbuf);
7390       return NULL;
7391     }
7392 
7393   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7394   ssymbuf->ssym = NULL;
7395   ssymbuf->count = shndx_count;
7396   ssymbuf->st_shndx = 0;
7397   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7398     {
7399       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7400 	{
7401 	  ssymhead++;
7402 	  ssymhead->ssym = ssym;
7403 	  ssymhead->count = 0;
7404 	  ssymhead->st_shndx = (*ind)->st_shndx;
7405 	}
7406       ssym->st_name = (*ind)->st_name;
7407       ssym->st_info = (*ind)->st_info;
7408       ssym->st_other = (*ind)->st_other;
7409       ssymhead->count++;
7410     }
7411   BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7412 	      && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7413 		  == total_size));
7414 
7415   free (indbuf);
7416   return ssymbuf;
7417 }
7418 
7419 /* Check if 2 sections define the same set of local and global
7420    symbols.  */
7421 
7422 static bfd_boolean
bfd_elf_match_symbols_in_sections(asection * sec1,asection * sec2,struct bfd_link_info * info)7423 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7424 				   struct bfd_link_info *info)
7425 {
7426   bfd *bfd1, *bfd2;
7427   const struct elf_backend_data *bed1, *bed2;
7428   Elf_Internal_Shdr *hdr1, *hdr2;
7429   size_t symcount1, symcount2;
7430   Elf_Internal_Sym *isymbuf1, *isymbuf2;
7431   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7432   Elf_Internal_Sym *isym, *isymend;
7433   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7434   size_t count1, count2, i;
7435   unsigned int shndx1, shndx2;
7436   bfd_boolean result;
7437 
7438   bfd1 = sec1->owner;
7439   bfd2 = sec2->owner;
7440 
7441   /* Both sections have to be in ELF.  */
7442   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7443       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7444     return FALSE;
7445 
7446   if (elf_section_type (sec1) != elf_section_type (sec2))
7447     return FALSE;
7448 
7449   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7450   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7451   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7452     return FALSE;
7453 
7454   bed1 = get_elf_backend_data (bfd1);
7455   bed2 = get_elf_backend_data (bfd2);
7456   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7457   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7458   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7459   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7460 
7461   if (symcount1 == 0 || symcount2 == 0)
7462     return FALSE;
7463 
7464   result = FALSE;
7465   isymbuf1 = NULL;
7466   isymbuf2 = NULL;
7467   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7468   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7469 
7470   if (ssymbuf1 == NULL)
7471     {
7472       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7473 				       NULL, NULL, NULL);
7474       if (isymbuf1 == NULL)
7475 	goto done;
7476 
7477       if (!info->reduce_memory_overheads)
7478 	elf_tdata (bfd1)->symbuf = ssymbuf1
7479 	  = elf_create_symbuf (symcount1, isymbuf1);
7480     }
7481 
7482   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7483     {
7484       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7485 				       NULL, NULL, NULL);
7486       if (isymbuf2 == NULL)
7487 	goto done;
7488 
7489       if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7490 	elf_tdata (bfd2)->symbuf = ssymbuf2
7491 	  = elf_create_symbuf (symcount2, isymbuf2);
7492     }
7493 
7494   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7495     {
7496       /* Optimized faster version.  */
7497       size_t lo, hi, mid;
7498       struct elf_symbol *symp;
7499       struct elf_symbuf_symbol *ssym, *ssymend;
7500 
7501       lo = 0;
7502       hi = ssymbuf1->count;
7503       ssymbuf1++;
7504       count1 = 0;
7505       while (lo < hi)
7506 	{
7507 	  mid = (lo + hi) / 2;
7508 	  if (shndx1 < ssymbuf1[mid].st_shndx)
7509 	    hi = mid;
7510 	  else if (shndx1 > ssymbuf1[mid].st_shndx)
7511 	    lo = mid + 1;
7512 	  else
7513 	    {
7514 	      count1 = ssymbuf1[mid].count;
7515 	      ssymbuf1 += mid;
7516 	      break;
7517 	    }
7518 	}
7519 
7520       lo = 0;
7521       hi = ssymbuf2->count;
7522       ssymbuf2++;
7523       count2 = 0;
7524       while (lo < hi)
7525 	{
7526 	  mid = (lo + hi) / 2;
7527 	  if (shndx2 < ssymbuf2[mid].st_shndx)
7528 	    hi = mid;
7529 	  else if (shndx2 > ssymbuf2[mid].st_shndx)
7530 	    lo = mid + 1;
7531 	  else
7532 	    {
7533 	      count2 = ssymbuf2[mid].count;
7534 	      ssymbuf2 += mid;
7535 	      break;
7536 	    }
7537 	}
7538 
7539       if (count1 == 0 || count2 == 0 || count1 != count2)
7540 	goto done;
7541 
7542       symtable1
7543 	= (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7544       symtable2
7545 	= (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7546       if (symtable1 == NULL || symtable2 == NULL)
7547 	goto done;
7548 
7549       symp = symtable1;
7550       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7551 	   ssym < ssymend; ssym++, symp++)
7552 	{
7553 	  symp->u.ssym = ssym;
7554 	  symp->name = bfd_elf_string_from_elf_section (bfd1,
7555 							hdr1->sh_link,
7556 							ssym->st_name);
7557 	}
7558 
7559       symp = symtable2;
7560       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7561 	   ssym < ssymend; ssym++, symp++)
7562 	{
7563 	  symp->u.ssym = ssym;
7564 	  symp->name = bfd_elf_string_from_elf_section (bfd2,
7565 							hdr2->sh_link,
7566 							ssym->st_name);
7567 	}
7568 
7569       /* Sort symbol by name.  */
7570       qsort (symtable1, count1, sizeof (struct elf_symbol),
7571 	     elf_sym_name_compare);
7572       qsort (symtable2, count1, sizeof (struct elf_symbol),
7573 	     elf_sym_name_compare);
7574 
7575       for (i = 0; i < count1; i++)
7576 	/* Two symbols must have the same binding, type and name.  */
7577 	if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7578 	    || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7579 	    || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7580 	  goto done;
7581 
7582       result = TRUE;
7583       goto done;
7584     }
7585 
7586   symtable1 = (struct elf_symbol *)
7587       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7588   symtable2 = (struct elf_symbol *)
7589       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7590   if (symtable1 == NULL || symtable2 == NULL)
7591     goto done;
7592 
7593   /* Count definitions in the section.  */
7594   count1 = 0;
7595   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7596     if (isym->st_shndx == shndx1)
7597       symtable1[count1++].u.isym = isym;
7598 
7599   count2 = 0;
7600   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7601     if (isym->st_shndx == shndx2)
7602       symtable2[count2++].u.isym = isym;
7603 
7604   if (count1 == 0 || count2 == 0 || count1 != count2)
7605     goto done;
7606 
7607   for (i = 0; i < count1; i++)
7608     symtable1[i].name
7609       = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7610 					 symtable1[i].u.isym->st_name);
7611 
7612   for (i = 0; i < count2; i++)
7613     symtable2[i].name
7614       = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7615 					 symtable2[i].u.isym->st_name);
7616 
7617   /* Sort symbol by name.  */
7618   qsort (symtable1, count1, sizeof (struct elf_symbol),
7619 	 elf_sym_name_compare);
7620   qsort (symtable2, count1, sizeof (struct elf_symbol),
7621 	 elf_sym_name_compare);
7622 
7623   for (i = 0; i < count1; i++)
7624     /* Two symbols must have the same binding, type and name.  */
7625     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7626 	|| symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7627 	|| strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7628       goto done;
7629 
7630   result = TRUE;
7631 
7632 done:
7633   if (symtable1)
7634     free (symtable1);
7635   if (symtable2)
7636     free (symtable2);
7637   if (isymbuf1)
7638     free (isymbuf1);
7639   if (isymbuf2)
7640     free (isymbuf2);
7641 
7642   return result;
7643 }
7644 
7645 /* Return TRUE if 2 section types are compatible.  */
7646 
7647 bfd_boolean
_bfd_elf_match_sections_by_type(bfd * abfd,const asection * asec,bfd * bbfd,const asection * bsec)7648 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7649 				 bfd *bbfd, const asection *bsec)
7650 {
7651   if (asec == NULL
7652       || bsec == NULL
7653       || abfd->xvec->flavour != bfd_target_elf_flavour
7654       || bbfd->xvec->flavour != bfd_target_elf_flavour)
7655     return TRUE;
7656 
7657   return elf_section_type (asec) == elf_section_type (bsec);
7658 }
7659 
7660 /* Final phase of ELF linker.  */
7661 
7662 /* A structure we use to avoid passing large numbers of arguments.  */
7663 
7664 struct elf_final_link_info
7665 {
7666   /* General link information.  */
7667   struct bfd_link_info *info;
7668   /* Output BFD.  */
7669   bfd *output_bfd;
7670   /* Symbol string table.  */
7671   struct elf_strtab_hash *symstrtab;
7672   /* .hash section.  */
7673   asection *hash_sec;
7674   /* symbol version section (.gnu.version).  */
7675   asection *symver_sec;
7676   /* Buffer large enough to hold contents of any section.  */
7677   bfd_byte *contents;
7678   /* Buffer large enough to hold external relocs of any section.  */
7679   void *external_relocs;
7680   /* Buffer large enough to hold internal relocs of any section.  */
7681   Elf_Internal_Rela *internal_relocs;
7682   /* Buffer large enough to hold external local symbols of any input
7683      BFD.  */
7684   bfd_byte *external_syms;
7685   /* And a buffer for symbol section indices.  */
7686   Elf_External_Sym_Shndx *locsym_shndx;
7687   /* Buffer large enough to hold internal local symbols of any input
7688      BFD.  */
7689   Elf_Internal_Sym *internal_syms;
7690   /* Array large enough to hold a symbol index for each local symbol
7691      of any input BFD.  */
7692   long *indices;
7693   /* Array large enough to hold a section pointer for each local
7694      symbol of any input BFD.  */
7695   asection **sections;
7696   /* Buffer for SHT_SYMTAB_SHNDX section.  */
7697   Elf_External_Sym_Shndx *symshndxbuf;
7698   /* Number of STT_FILE syms seen.  */
7699   size_t filesym_count;
7700 };
7701 
7702 /* This struct is used to pass information to elf_link_output_extsym.  */
7703 
7704 struct elf_outext_info
7705 {
7706   bfd_boolean failed;
7707   bfd_boolean localsyms;
7708   bfd_boolean file_sym_done;
7709   struct elf_final_link_info *flinfo;
7710 };
7711 
7712 
7713 /* Support for evaluating a complex relocation.
7714 
7715    Complex relocations are generalized, self-describing relocations.  The
7716    implementation of them consists of two parts: complex symbols, and the
7717    relocations themselves.
7718 
7719    The relocations are use a reserved elf-wide relocation type code (R_RELC
7720    external / BFD_RELOC_RELC internal) and an encoding of relocation field
7721    information (start bit, end bit, word width, etc) into the addend.  This
7722    information is extracted from CGEN-generated operand tables within gas.
7723 
7724    Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7725    internal) representing prefix-notation expressions, including but not
7726    limited to those sorts of expressions normally encoded as addends in the
7727    addend field.  The symbol mangling format is:
7728 
7729    <node> := <literal>
7730           |  <unary-operator> ':' <node>
7731           |  <binary-operator> ':' <node> ':' <node>
7732 	  ;
7733 
7734    <literal> := 's' <digits=N> ':' <N character symbol name>
7735              |  'S' <digits=N> ':' <N character section name>
7736 	     |  '#' <hexdigits>
7737 	     ;
7738 
7739    <binary-operator> := as in C
7740    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
7741 
7742 static void
set_symbol_value(bfd * bfd_with_globals,Elf_Internal_Sym * isymbuf,size_t locsymcount,size_t symidx,bfd_vma val)7743 set_symbol_value (bfd *bfd_with_globals,
7744 		  Elf_Internal_Sym *isymbuf,
7745 		  size_t locsymcount,
7746 		  size_t symidx,
7747 		  bfd_vma val)
7748 {
7749   struct elf_link_hash_entry **sym_hashes;
7750   struct elf_link_hash_entry *h;
7751   size_t extsymoff = locsymcount;
7752 
7753   if (symidx < locsymcount)
7754     {
7755       Elf_Internal_Sym *sym;
7756 
7757       sym = isymbuf + symidx;
7758       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7759 	{
7760 	  /* It is a local symbol: move it to the
7761 	     "absolute" section and give it a value.  */
7762 	  sym->st_shndx = SHN_ABS;
7763 	  sym->st_value = val;
7764 	  return;
7765 	}
7766       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7767       extsymoff = 0;
7768     }
7769 
7770   /* It is a global symbol: set its link type
7771      to "defined" and give it a value.  */
7772 
7773   sym_hashes = elf_sym_hashes (bfd_with_globals);
7774   h = sym_hashes [symidx - extsymoff];
7775   while (h->root.type == bfd_link_hash_indirect
7776 	 || h->root.type == bfd_link_hash_warning)
7777     h = (struct elf_link_hash_entry *) h->root.u.i.link;
7778   h->root.type = bfd_link_hash_defined;
7779   h->root.u.def.value = val;
7780   h->root.u.def.section = bfd_abs_section_ptr;
7781 }
7782 
7783 static bfd_boolean
resolve_symbol(const char * name,bfd * input_bfd,struct elf_final_link_info * flinfo,bfd_vma * result,Elf_Internal_Sym * isymbuf,size_t locsymcount)7784 resolve_symbol (const char *name,
7785 		bfd *input_bfd,
7786 		struct elf_final_link_info *flinfo,
7787 		bfd_vma *result,
7788 		Elf_Internal_Sym *isymbuf,
7789 		size_t locsymcount)
7790 {
7791   Elf_Internal_Sym *sym;
7792   struct bfd_link_hash_entry *global_entry;
7793   const char *candidate = NULL;
7794   Elf_Internal_Shdr *symtab_hdr;
7795   size_t i;
7796 
7797   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7798 
7799   for (i = 0; i < locsymcount; ++ i)
7800     {
7801       sym = isymbuf + i;
7802 
7803       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7804 	continue;
7805 
7806       candidate = bfd_elf_string_from_elf_section (input_bfd,
7807 						   symtab_hdr->sh_link,
7808 						   sym->st_name);
7809 #ifdef DEBUG
7810       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7811 	      name, candidate, (unsigned long) sym->st_value);
7812 #endif
7813       if (candidate && strcmp (candidate, name) == 0)
7814 	{
7815 	  asection *sec = flinfo->sections [i];
7816 
7817 	  *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7818 	  *result += sec->output_offset + sec->output_section->vma;
7819 #ifdef DEBUG
7820 	  printf ("Found symbol with value %8.8lx\n",
7821 		  (unsigned long) *result);
7822 #endif
7823 	  return TRUE;
7824 	}
7825     }
7826 
7827   /* Hmm, haven't found it yet. perhaps it is a global.  */
7828   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7829 				       FALSE, FALSE, TRUE);
7830   if (!global_entry)
7831     return FALSE;
7832 
7833   if (global_entry->type == bfd_link_hash_defined
7834       || global_entry->type == bfd_link_hash_defweak)
7835     {
7836       *result = (global_entry->u.def.value
7837 		 + global_entry->u.def.section->output_section->vma
7838 		 + global_entry->u.def.section->output_offset);
7839 #ifdef DEBUG
7840       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7841 	      global_entry->root.string, (unsigned long) *result);
7842 #endif
7843       return TRUE;
7844     }
7845 
7846   return FALSE;
7847 }
7848 
7849 /* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
7850    bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
7851    names like "foo.end" which is the end address of section "foo".  */
7852 
7853 static bfd_boolean
resolve_section(const char * name,asection * sections,bfd_vma * result,bfd * abfd)7854 resolve_section (const char *name,
7855 		 asection *sections,
7856 		 bfd_vma *result,
7857 		 bfd * abfd)
7858 {
7859   asection *curr;
7860   unsigned int len;
7861 
7862   for (curr = sections; curr; curr = curr->next)
7863     if (strcmp (curr->name, name) == 0)
7864       {
7865 	*result = curr->vma;
7866 	return TRUE;
7867       }
7868 
7869   /* Hmm. still haven't found it. try pseudo-section names.  */
7870   /* FIXME: This could be coded more efficiently...  */
7871   for (curr = sections; curr; curr = curr->next)
7872     {
7873       len = strlen (curr->name);
7874       if (len > strlen (name))
7875 	continue;
7876 
7877       if (strncmp (curr->name, name, len) == 0)
7878 	{
7879 	  if (strncmp (".end", name + len, 4) == 0)
7880 	    {
7881 	      *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7882 	      return TRUE;
7883 	    }
7884 
7885 	  /* Insert more pseudo-section names here, if you like.  */
7886 	}
7887     }
7888 
7889   return FALSE;
7890 }
7891 
7892 static void
undefined_reference(const char * reftype,const char * name)7893 undefined_reference (const char *reftype, const char *name)
7894 {
7895   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7896 		      reftype, name);
7897 }
7898 
7899 static bfd_boolean
eval_symbol(bfd_vma * result,const char ** symp,bfd * input_bfd,struct elf_final_link_info * flinfo,bfd_vma dot,Elf_Internal_Sym * isymbuf,size_t locsymcount,int signed_p)7900 eval_symbol (bfd_vma *result,
7901 	     const char **symp,
7902 	     bfd *input_bfd,
7903 	     struct elf_final_link_info *flinfo,
7904 	     bfd_vma dot,
7905 	     Elf_Internal_Sym *isymbuf,
7906 	     size_t locsymcount,
7907 	     int signed_p)
7908 {
7909   size_t len;
7910   size_t symlen;
7911   bfd_vma a;
7912   bfd_vma b;
7913   char symbuf[4096];
7914   const char *sym = *symp;
7915   const char *symend;
7916   bfd_boolean symbol_is_section = FALSE;
7917 
7918   len = strlen (sym);
7919   symend = sym + len;
7920 
7921   if (len < 1 || len > sizeof (symbuf))
7922     {
7923       bfd_set_error (bfd_error_invalid_operation);
7924       return FALSE;
7925     }
7926 
7927   switch (* sym)
7928     {
7929     case '.':
7930       *result = dot;
7931       *symp = sym + 1;
7932       return TRUE;
7933 
7934     case '#':
7935       ++sym;
7936       *result = strtoul (sym, (char **) symp, 16);
7937       return TRUE;
7938 
7939     case 'S':
7940       symbol_is_section = TRUE;
7941     case 's':
7942       ++sym;
7943       symlen = strtol (sym, (char **) symp, 10);
7944       sym = *symp + 1; /* Skip the trailing ':'.  */
7945 
7946       if (symend < sym || symlen + 1 > sizeof (symbuf))
7947 	{
7948 	  bfd_set_error (bfd_error_invalid_operation);
7949 	  return FALSE;
7950 	}
7951 
7952       memcpy (symbuf, sym, symlen);
7953       symbuf[symlen] = '\0';
7954       *symp = sym + symlen;
7955 
7956       /* Is it always possible, with complex symbols, that gas "mis-guessed"
7957 	 the symbol as a section, or vice-versa. so we're pretty liberal in our
7958 	 interpretation here; section means "try section first", not "must be a
7959 	 section", and likewise with symbol.  */
7960 
7961       if (symbol_is_section)
7962 	{
7963 	  if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
7964 	      && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7965 				  isymbuf, locsymcount))
7966 	    {
7967 	      undefined_reference ("section", symbuf);
7968 	      return FALSE;
7969 	    }
7970 	}
7971       else
7972 	{
7973 	  if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7974 			       isymbuf, locsymcount)
7975 	      && !resolve_section (symbuf, flinfo->output_bfd->sections,
7976 				   result, input_bfd))
7977 	    {
7978 	      undefined_reference ("symbol", symbuf);
7979 	      return FALSE;
7980 	    }
7981 	}
7982 
7983       return TRUE;
7984 
7985       /* All that remains are operators.  */
7986 
7987 #define UNARY_OP(op)						\
7988   if (strncmp (sym, #op, strlen (#op)) == 0)			\
7989     {								\
7990       sym += strlen (#op);					\
7991       if (*sym == ':')						\
7992 	++sym;							\
7993       *symp = sym;						\
7994       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
7995 			isymbuf, locsymcount, signed_p))	\
7996 	return FALSE;						\
7997       if (signed_p)						\
7998 	*result = op ((bfd_signed_vma) a);			\
7999       else							\
8000 	*result = op a;						\
8001       return TRUE;						\
8002     }
8003 
8004 #define BINARY_OP(op)						\
8005   if (strncmp (sym, #op, strlen (#op)) == 0)			\
8006     {								\
8007       sym += strlen (#op);					\
8008       if (*sym == ':')						\
8009 	++sym;							\
8010       *symp = sym;						\
8011       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
8012 			isymbuf, locsymcount, signed_p))	\
8013 	return FALSE;						\
8014       ++*symp;							\
8015       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,	\
8016 			isymbuf, locsymcount, signed_p))	\
8017 	return FALSE;						\
8018       if (signed_p)						\
8019 	*result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b);	\
8020       else							\
8021 	*result = a op b;					\
8022       return TRUE;						\
8023     }
8024 
8025     default:
8026       UNARY_OP  (0-);
8027       BINARY_OP (<<);
8028       BINARY_OP (>>);
8029       BINARY_OP (==);
8030       BINARY_OP (!=);
8031       BINARY_OP (<=);
8032       BINARY_OP (>=);
8033       BINARY_OP (&&);
8034       BINARY_OP (||);
8035       UNARY_OP  (~);
8036       UNARY_OP  (!);
8037       BINARY_OP (*);
8038       BINARY_OP (/);
8039       BINARY_OP (%);
8040       BINARY_OP (^);
8041       BINARY_OP (|);
8042       BINARY_OP (&);
8043       BINARY_OP (+);
8044       BINARY_OP (-);
8045       BINARY_OP (<);
8046       BINARY_OP (>);
8047 #undef UNARY_OP
8048 #undef BINARY_OP
8049       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8050       bfd_set_error (bfd_error_invalid_operation);
8051       return FALSE;
8052     }
8053 }
8054 
8055 static void
put_value(bfd_vma size,unsigned long chunksz,bfd * input_bfd,bfd_vma x,bfd_byte * location)8056 put_value (bfd_vma size,
8057 	   unsigned long chunksz,
8058 	   bfd *input_bfd,
8059 	   bfd_vma x,
8060 	   bfd_byte *location)
8061 {
8062   location += (size - chunksz);
8063 
8064   for (; size; size -= chunksz, location -= chunksz)
8065     {
8066       switch (chunksz)
8067 	{
8068 	case 1:
8069 	  bfd_put_8 (input_bfd, x, location);
8070 	  x >>= 8;
8071 	  break;
8072 	case 2:
8073 	  bfd_put_16 (input_bfd, x, location);
8074 	  x >>= 16;
8075 	  break;
8076 	case 4:
8077 	  bfd_put_32 (input_bfd, x, location);
8078 	  /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
8079 	  x >>= 16;
8080 	  x >>= 16;
8081 	  break;
8082 #ifdef BFD64
8083 	case 8:
8084 	  bfd_put_64 (input_bfd, x, location);
8085 	  /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
8086 	  x >>= 32;
8087 	  x >>= 32;
8088 	  break;
8089 #endif
8090 	default:
8091 	  abort ();
8092 	  break;
8093 	}
8094     }
8095 }
8096 
8097 static bfd_vma
get_value(bfd_vma size,unsigned long chunksz,bfd * input_bfd,bfd_byte * location)8098 get_value (bfd_vma size,
8099 	   unsigned long chunksz,
8100 	   bfd *input_bfd,
8101 	   bfd_byte *location)
8102 {
8103   int shift;
8104   bfd_vma x = 0;
8105 
8106   /* Sanity checks.  */
8107   BFD_ASSERT (chunksz <= sizeof (x)
8108 	      && size >= chunksz
8109 	      && chunksz != 0
8110 	      && (size % chunksz) == 0
8111 	      && input_bfd != NULL
8112 	      && location != NULL);
8113 
8114   if (chunksz == sizeof (x))
8115     {
8116       BFD_ASSERT (size == chunksz);
8117 
8118       /* Make sure that we do not perform an undefined shift operation.
8119 	 We know that size == chunksz so there will only be one iteration
8120 	 of the loop below.  */
8121       shift = 0;
8122     }
8123   else
8124     shift = 8 * chunksz;
8125 
8126   for (; size; size -= chunksz, location += chunksz)
8127     {
8128       switch (chunksz)
8129 	{
8130 	case 1:
8131 	  x = (x << shift) | bfd_get_8 (input_bfd, location);
8132 	  break;
8133 	case 2:
8134 	  x = (x << shift) | bfd_get_16 (input_bfd, location);
8135 	  break;
8136 	case 4:
8137 	  x = (x << shift) | bfd_get_32 (input_bfd, location);
8138 	  break;
8139 #ifdef BFD64
8140 	case 8:
8141 	  x = (x << shift) | bfd_get_64 (input_bfd, location);
8142 	  break;
8143 #endif
8144 	default:
8145 	  abort ();
8146 	}
8147     }
8148   return x;
8149 }
8150 
8151 static void
decode_complex_addend(unsigned long * start,unsigned long * oplen,unsigned long * len,unsigned long * wordsz,unsigned long * chunksz,unsigned long * lsb0_p,unsigned long * signed_p,unsigned long * trunc_p,unsigned long encoded)8152 decode_complex_addend (unsigned long *start,   /* in bits */
8153 		       unsigned long *oplen,   /* in bits */
8154 		       unsigned long *len,     /* in bits */
8155 		       unsigned long *wordsz,  /* in bytes */
8156 		       unsigned long *chunksz, /* in bytes */
8157 		       unsigned long *lsb0_p,
8158 		       unsigned long *signed_p,
8159 		       unsigned long *trunc_p,
8160 		       unsigned long encoded)
8161 {
8162   * start     =  encoded        & 0x3F;
8163   * len       = (encoded >>  6) & 0x3F;
8164   * oplen     = (encoded >> 12) & 0x3F;
8165   * wordsz    = (encoded >> 18) & 0xF;
8166   * chunksz   = (encoded >> 22) & 0xF;
8167   * lsb0_p    = (encoded >> 27) & 1;
8168   * signed_p  = (encoded >> 28) & 1;
8169   * trunc_p   = (encoded >> 29) & 1;
8170 }
8171 
8172 bfd_reloc_status_type
bfd_elf_perform_complex_relocation(bfd * input_bfd,asection * input_section ATTRIBUTE_UNUSED,bfd_byte * contents,Elf_Internal_Rela * rel,bfd_vma relocation)8173 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8174 				    asection *input_section ATTRIBUTE_UNUSED,
8175 				    bfd_byte *contents,
8176 				    Elf_Internal_Rela *rel,
8177 				    bfd_vma relocation)
8178 {
8179   bfd_vma shift, x, mask;
8180   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8181   bfd_reloc_status_type r;
8182 
8183   /*  Perform this reloc, since it is complex.
8184       (this is not to say that it necessarily refers to a complex
8185       symbol; merely that it is a self-describing CGEN based reloc.
8186       i.e. the addend has the complete reloc information (bit start, end,
8187       word size, etc) encoded within it.).  */
8188 
8189   decode_complex_addend (&start, &oplen, &len, &wordsz,
8190 			 &chunksz, &lsb0_p, &signed_p,
8191 			 &trunc_p, rel->r_addend);
8192 
8193   mask = (((1L << (len - 1)) - 1) << 1) | 1;
8194 
8195   if (lsb0_p)
8196     shift = (start + 1) - len;
8197   else
8198     shift = (8 * wordsz) - (start + len);
8199 
8200   x = get_value (wordsz, chunksz, input_bfd,
8201 		 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8202 
8203 #ifdef DEBUG
8204   printf ("Doing complex reloc: "
8205 	  "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8206 	  "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8207 	  "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8208 	  lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8209 	  oplen, (unsigned long) x, (unsigned long) mask,
8210 	  (unsigned long) relocation);
8211 #endif
8212 
8213   r = bfd_reloc_ok;
8214   if (! trunc_p)
8215     /* Now do an overflow check.  */
8216     r = bfd_check_overflow ((signed_p
8217 			     ? complain_overflow_signed
8218 			     : complain_overflow_unsigned),
8219 			    len, 0, (8 * wordsz),
8220 			    relocation);
8221 
8222   /* Do the deed.  */
8223   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8224 
8225 #ifdef DEBUG
8226   printf ("           relocation: %8.8lx\n"
8227 	  "         shifted mask: %8.8lx\n"
8228 	  " shifted/masked reloc: %8.8lx\n"
8229 	  "               result: %8.8lx\n",
8230 	  (unsigned long) relocation, (unsigned long) (mask << shift),
8231 	  (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8232 #endif
8233   put_value (wordsz, chunksz, input_bfd, x,
8234 	     contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8235   return r;
8236 }
8237 
8238 /* Functions to read r_offset from external (target order) reloc
8239    entry.  Faster than bfd_getl32 et al, because we let the compiler
8240    know the value is aligned.  */
8241 
8242 static bfd_vma
ext32l_r_offset(const void * p)8243 ext32l_r_offset (const void *p)
8244 {
8245   union aligned32
8246   {
8247     uint32_t v;
8248     unsigned char c[4];
8249   };
8250   const union aligned32 *a
8251     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8252 
8253   uint32_t aval = (  (uint32_t) a->c[0]
8254 		   | (uint32_t) a->c[1] << 8
8255 		   | (uint32_t) a->c[2] << 16
8256 		   | (uint32_t) a->c[3] << 24);
8257   return aval;
8258 }
8259 
8260 static bfd_vma
ext32b_r_offset(const void * p)8261 ext32b_r_offset (const void *p)
8262 {
8263   union aligned32
8264   {
8265     uint32_t v;
8266     unsigned char c[4];
8267   };
8268   const union aligned32 *a
8269     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8270 
8271   uint32_t aval = (  (uint32_t) a->c[0] << 24
8272 		   | (uint32_t) a->c[1] << 16
8273 		   | (uint32_t) a->c[2] << 8
8274 		   | (uint32_t) a->c[3]);
8275   return aval;
8276 }
8277 
8278 #ifdef BFD_HOST_64_BIT
8279 static bfd_vma
ext64l_r_offset(const void * p)8280 ext64l_r_offset (const void *p)
8281 {
8282   union aligned64
8283   {
8284     uint64_t v;
8285     unsigned char c[8];
8286   };
8287   const union aligned64 *a
8288     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8289 
8290   uint64_t aval = (  (uint64_t) a->c[0]
8291 		   | (uint64_t) a->c[1] << 8
8292 		   | (uint64_t) a->c[2] << 16
8293 		   | (uint64_t) a->c[3] << 24
8294 		   | (uint64_t) a->c[4] << 32
8295 		   | (uint64_t) a->c[5] << 40
8296 		   | (uint64_t) a->c[6] << 48
8297 		   | (uint64_t) a->c[7] << 56);
8298   return aval;
8299 }
8300 
8301 static bfd_vma
ext64b_r_offset(const void * p)8302 ext64b_r_offset (const void *p)
8303 {
8304   union aligned64
8305   {
8306     uint64_t v;
8307     unsigned char c[8];
8308   };
8309   const union aligned64 *a
8310     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8311 
8312   uint64_t aval = (  (uint64_t) a->c[0] << 56
8313 		   | (uint64_t) a->c[1] << 48
8314 		   | (uint64_t) a->c[2] << 40
8315 		   | (uint64_t) a->c[3] << 32
8316 		   | (uint64_t) a->c[4] << 24
8317 		   | (uint64_t) a->c[5] << 16
8318 		   | (uint64_t) a->c[6] << 8
8319 		   | (uint64_t) a->c[7]);
8320   return aval;
8321 }
8322 #endif
8323 
8324 /* When performing a relocatable link, the input relocations are
8325    preserved.  But, if they reference global symbols, the indices
8326    referenced must be updated.  Update all the relocations found in
8327    RELDATA.  */
8328 
8329 static bfd_boolean
elf_link_adjust_relocs(bfd * abfd,struct bfd_elf_section_reloc_data * reldata,bfd_boolean sort)8330 elf_link_adjust_relocs (bfd *abfd,
8331 			struct bfd_elf_section_reloc_data *reldata,
8332 			bfd_boolean sort)
8333 {
8334   unsigned int i;
8335   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8336   bfd_byte *erela;
8337   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8338   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8339   bfd_vma r_type_mask;
8340   int r_sym_shift;
8341   unsigned int count = reldata->count;
8342   struct elf_link_hash_entry **rel_hash = reldata->hashes;
8343 
8344   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8345     {
8346       swap_in = bed->s->swap_reloc_in;
8347       swap_out = bed->s->swap_reloc_out;
8348     }
8349   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8350     {
8351       swap_in = bed->s->swap_reloca_in;
8352       swap_out = bed->s->swap_reloca_out;
8353     }
8354   else
8355     abort ();
8356 
8357   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8358     abort ();
8359 
8360   if (bed->s->arch_size == 32)
8361     {
8362       r_type_mask = 0xff;
8363       r_sym_shift = 8;
8364     }
8365   else
8366     {
8367       r_type_mask = 0xffffffff;
8368       r_sym_shift = 32;
8369     }
8370 
8371   erela = reldata->hdr->contents;
8372   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8373     {
8374       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8375       unsigned int j;
8376 
8377       if (*rel_hash == NULL)
8378 	continue;
8379 
8380       BFD_ASSERT ((*rel_hash)->indx >= 0);
8381 
8382       (*swap_in) (abfd, erela, irela);
8383       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8384 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8385 			   | (irela[j].r_info & r_type_mask));
8386       (*swap_out) (abfd, irela, erela);
8387     }
8388 
8389   if (sort && count != 0)
8390     {
8391       bfd_vma (*ext_r_off) (const void *);
8392       bfd_vma r_off;
8393       size_t elt_size;
8394       bfd_byte *base, *end, *p, *loc;
8395       bfd_byte *buf = NULL;
8396 
8397       if (bed->s->arch_size == 32)
8398 	{
8399 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8400 	    ext_r_off = ext32l_r_offset;
8401 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8402 	    ext_r_off = ext32b_r_offset;
8403 	  else
8404 	    abort ();
8405 	}
8406       else
8407 	{
8408 #ifdef BFD_HOST_64_BIT
8409 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8410 	    ext_r_off = ext64l_r_offset;
8411 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8412 	    ext_r_off = ext64b_r_offset;
8413 	  else
8414 #endif
8415 	    abort ();
8416 	}
8417 
8418       /*  Must use a stable sort here.  A modified insertion sort,
8419 	  since the relocs are mostly sorted already.  */
8420       elt_size = reldata->hdr->sh_entsize;
8421       base = reldata->hdr->contents;
8422       end = base + count * elt_size;
8423       if (elt_size > sizeof (Elf64_External_Rela))
8424 	abort ();
8425 
8426       /* Ensure the first element is lowest.  This acts as a sentinel,
8427 	 speeding the main loop below.  */
8428       r_off = (*ext_r_off) (base);
8429       for (p = loc = base; (p += elt_size) < end; )
8430 	{
8431 	  bfd_vma r_off2 = (*ext_r_off) (p);
8432 	  if (r_off > r_off2)
8433 	    {
8434 	      r_off = r_off2;
8435 	      loc = p;
8436 	    }
8437 	}
8438       if (loc != base)
8439 	{
8440 	  /* Don't just swap *base and *loc as that changes the order
8441 	     of the original base[0] and base[1] if they happen to
8442 	     have the same r_offset.  */
8443 	  bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8444 	  memcpy (onebuf, loc, elt_size);
8445 	  memmove (base + elt_size, base, loc - base);
8446 	  memcpy (base, onebuf, elt_size);
8447 	}
8448 
8449       for (p = base + elt_size; (p += elt_size) < end; )
8450 	{
8451 	  /* base to p is sorted, *p is next to insert.  */
8452 	  r_off = (*ext_r_off) (p);
8453 	  /* Search the sorted region for location to insert.  */
8454 	  loc = p - elt_size;
8455 	  while (r_off < (*ext_r_off) (loc))
8456 	    loc -= elt_size;
8457 	  loc += elt_size;
8458 	  if (loc != p)
8459 	    {
8460 	      /* Chances are there is a run of relocs to insert here,
8461 		 from one of more input files.  Files are not always
8462 		 linked in order due to the way elf_link_input_bfd is
8463 		 called.  See pr17666.  */
8464 	      size_t sortlen = p - loc;
8465 	      bfd_vma r_off2 = (*ext_r_off) (loc);
8466 	      size_t runlen = elt_size;
8467 	      size_t buf_size = 96 * 1024;
8468 	      while (p + runlen < end
8469 		     && (sortlen <= buf_size
8470 			 || runlen + elt_size <= buf_size)
8471 		     && r_off2 > (*ext_r_off) (p + runlen))
8472 		runlen += elt_size;
8473 	      if (buf == NULL)
8474 		{
8475 		  buf = bfd_malloc (buf_size);
8476 		  if (buf == NULL)
8477 		    return FALSE;
8478 		}
8479 	      if (runlen < sortlen)
8480 		{
8481 		  memcpy (buf, p, runlen);
8482 		  memmove (loc + runlen, loc, sortlen);
8483 		  memcpy (loc, buf, runlen);
8484 		}
8485 	      else
8486 		{
8487 		  memcpy (buf, loc, sortlen);
8488 		  memmove (loc, p, runlen);
8489 		  memcpy (loc + runlen, buf, sortlen);
8490 		}
8491 	      p += runlen - elt_size;
8492 	    }
8493 	}
8494       /* Hashes are no longer valid.  */
8495       free (reldata->hashes);
8496       reldata->hashes = NULL;
8497       free (buf);
8498     }
8499   return TRUE;
8500 }
8501 
8502 struct elf_link_sort_rela
8503 {
8504   union {
8505     bfd_vma offset;
8506     bfd_vma sym_mask;
8507   } u;
8508   enum elf_reloc_type_class type;
8509   /* We use this as an array of size int_rels_per_ext_rel.  */
8510   Elf_Internal_Rela rela[1];
8511 };
8512 
8513 static int
elf_link_sort_cmp1(const void * A,const void * B)8514 elf_link_sort_cmp1 (const void *A, const void *B)
8515 {
8516   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8517   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8518   int relativea, relativeb;
8519 
8520   relativea = a->type == reloc_class_relative;
8521   relativeb = b->type == reloc_class_relative;
8522 
8523   if (relativea < relativeb)
8524     return 1;
8525   if (relativea > relativeb)
8526     return -1;
8527   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8528     return -1;
8529   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8530     return 1;
8531   if (a->rela->r_offset < b->rela->r_offset)
8532     return -1;
8533   if (a->rela->r_offset > b->rela->r_offset)
8534     return 1;
8535   return 0;
8536 }
8537 
8538 static int
elf_link_sort_cmp2(const void * A,const void * B)8539 elf_link_sort_cmp2 (const void *A, const void *B)
8540 {
8541   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8542   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8543 
8544   if (a->type < b->type)
8545     return -1;
8546   if (a->type > b->type)
8547     return 1;
8548   if (a->u.offset < b->u.offset)
8549     return -1;
8550   if (a->u.offset > b->u.offset)
8551     return 1;
8552   if (a->rela->r_offset < b->rela->r_offset)
8553     return -1;
8554   if (a->rela->r_offset > b->rela->r_offset)
8555     return 1;
8556   return 0;
8557 }
8558 
8559 static size_t
elf_link_sort_relocs(bfd * abfd,struct bfd_link_info * info,asection ** psec)8560 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8561 {
8562   asection *dynamic_relocs;
8563   asection *rela_dyn;
8564   asection *rel_dyn;
8565   bfd_size_type count, size;
8566   size_t i, ret, sort_elt, ext_size;
8567   bfd_byte *sort, *s_non_relative, *p;
8568   struct elf_link_sort_rela *sq;
8569   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8570   int i2e = bed->s->int_rels_per_ext_rel;
8571   unsigned int opb = bfd_octets_per_byte (abfd);
8572   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8573   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8574   struct bfd_link_order *lo;
8575   bfd_vma r_sym_mask;
8576   bfd_boolean use_rela;
8577 
8578   /* Find a dynamic reloc section.  */
8579   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8580   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
8581   if (rela_dyn != NULL && rela_dyn->size > 0
8582       && rel_dyn != NULL && rel_dyn->size > 0)
8583     {
8584       bfd_boolean use_rela_initialised = FALSE;
8585 
8586       /* This is just here to stop gcc from complaining.
8587 	 Its initialization checking code is not perfect.  */
8588       use_rela = TRUE;
8589 
8590       /* Both sections are present.  Examine the sizes
8591 	 of the indirect sections to help us choose.  */
8592       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8593 	if (lo->type == bfd_indirect_link_order)
8594 	  {
8595 	    asection *o = lo->u.indirect.section;
8596 
8597 	    if ((o->size % bed->s->sizeof_rela) == 0)
8598 	      {
8599 		if ((o->size % bed->s->sizeof_rel) == 0)
8600 		  /* Section size is divisible by both rel and rela sizes.
8601 		     It is of no help to us.  */
8602 		  ;
8603 		else
8604 		  {
8605 		    /* Section size is only divisible by rela.  */
8606 		    if (use_rela_initialised && (use_rela == FALSE))
8607 		      {
8608 			_bfd_error_handler (_("%B: Unable to sort relocs - "
8609 					      "they are in more than one size"),
8610 					    abfd);
8611 			bfd_set_error (bfd_error_invalid_operation);
8612 			return 0;
8613 		      }
8614 		    else
8615 		      {
8616 			use_rela = TRUE;
8617 			use_rela_initialised = TRUE;
8618 		      }
8619 		  }
8620 	      }
8621 	    else if ((o->size % bed->s->sizeof_rel) == 0)
8622 	      {
8623 		/* Section size is only divisible by rel.  */
8624 		if (use_rela_initialised && (use_rela == TRUE))
8625 		  {
8626 		    _bfd_error_handler (_("%B: Unable to sort relocs - "
8627 					  "they are in more than one size"),
8628 					abfd);
8629 		    bfd_set_error (bfd_error_invalid_operation);
8630 		    return 0;
8631 		  }
8632 		else
8633 		  {
8634 		    use_rela = FALSE;
8635 		    use_rela_initialised = TRUE;
8636 		  }
8637 	      }
8638 	    else
8639 	      {
8640 		/* The section size is not divisible by either -
8641 		   something is wrong.  */
8642 		_bfd_error_handler (_("%B: Unable to sort relocs - "
8643 				      "they are of an unknown size"), abfd);
8644 		bfd_set_error (bfd_error_invalid_operation);
8645 		return 0;
8646 	      }
8647 	  }
8648 
8649       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8650 	if (lo->type == bfd_indirect_link_order)
8651 	  {
8652 	    asection *o = lo->u.indirect.section;
8653 
8654 	    if ((o->size % bed->s->sizeof_rela) == 0)
8655 	      {
8656 		if ((o->size % bed->s->sizeof_rel) == 0)
8657 		  /* Section size is divisible by both rel and rela sizes.
8658 		     It is of no help to us.  */
8659 		  ;
8660 		else
8661 		  {
8662 		    /* Section size is only divisible by rela.  */
8663 		    if (use_rela_initialised && (use_rela == FALSE))
8664 		      {
8665 			_bfd_error_handler (_("%B: Unable to sort relocs - "
8666 					      "they are in more than one size"),
8667 					    abfd);
8668 			bfd_set_error (bfd_error_invalid_operation);
8669 			return 0;
8670 		      }
8671 		    else
8672 		      {
8673 			use_rela = TRUE;
8674 			use_rela_initialised = TRUE;
8675 		      }
8676 		  }
8677 	      }
8678 	    else if ((o->size % bed->s->sizeof_rel) == 0)
8679 	      {
8680 		/* Section size is only divisible by rel.  */
8681 		if (use_rela_initialised && (use_rela == TRUE))
8682 		  {
8683 		    _bfd_error_handler (_("%B: Unable to sort relocs - "
8684 					  "they are in more than one size"),
8685 					abfd);
8686 		    bfd_set_error (bfd_error_invalid_operation);
8687 		    return 0;
8688 		  }
8689 		else
8690 		  {
8691 		    use_rela = FALSE;
8692 		    use_rela_initialised = TRUE;
8693 		  }
8694 	      }
8695 	    else
8696 	      {
8697 		/* The section size is not divisible by either -
8698 		   something is wrong.  */
8699 		_bfd_error_handler (_("%B: Unable to sort relocs - "
8700 				      "they are of an unknown size"), abfd);
8701 		bfd_set_error (bfd_error_invalid_operation);
8702 		return 0;
8703 	      }
8704 	  }
8705 
8706       if (! use_rela_initialised)
8707 	/* Make a guess.  */
8708 	use_rela = TRUE;
8709     }
8710   else if (rela_dyn != NULL && rela_dyn->size > 0)
8711     use_rela = TRUE;
8712   else if (rel_dyn != NULL && rel_dyn->size > 0)
8713     use_rela = FALSE;
8714   else
8715     return 0;
8716 
8717   if (use_rela)
8718     {
8719       dynamic_relocs = rela_dyn;
8720       ext_size = bed->s->sizeof_rela;
8721       swap_in = bed->s->swap_reloca_in;
8722       swap_out = bed->s->swap_reloca_out;
8723     }
8724   else
8725     {
8726       dynamic_relocs = rel_dyn;
8727       ext_size = bed->s->sizeof_rel;
8728       swap_in = bed->s->swap_reloc_in;
8729       swap_out = bed->s->swap_reloc_out;
8730     }
8731 
8732   size = 0;
8733   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8734     if (lo->type == bfd_indirect_link_order)
8735       size += lo->u.indirect.section->size;
8736 
8737   if (size != dynamic_relocs->size)
8738     return 0;
8739 
8740   sort_elt = (sizeof (struct elf_link_sort_rela)
8741 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
8742 
8743   count = dynamic_relocs->size / ext_size;
8744   if (count == 0)
8745     return 0;
8746   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8747 
8748   if (sort == NULL)
8749     {
8750       (*info->callbacks->warning)
8751 	(info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8752       return 0;
8753     }
8754 
8755   if (bed->s->arch_size == 32)
8756     r_sym_mask = ~(bfd_vma) 0xff;
8757   else
8758     r_sym_mask = ~(bfd_vma) 0xffffffff;
8759 
8760   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8761     if (lo->type == bfd_indirect_link_order)
8762       {
8763 	bfd_byte *erel, *erelend;
8764 	asection *o = lo->u.indirect.section;
8765 
8766 	if (o->contents == NULL && o->size != 0)
8767 	  {
8768 	    /* This is a reloc section that is being handled as a normal
8769 	       section.  See bfd_section_from_shdr.  We can't combine
8770 	       relocs in this case.  */
8771 	    free (sort);
8772 	    return 0;
8773 	  }
8774 	erel = o->contents;
8775 	erelend = o->contents + o->size;
8776 	p = sort + o->output_offset * opb / ext_size * sort_elt;
8777 
8778 	while (erel < erelend)
8779 	  {
8780 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8781 
8782 	    (*swap_in) (abfd, erel, s->rela);
8783 	    s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8784 	    s->u.sym_mask = r_sym_mask;
8785 	    p += sort_elt;
8786 	    erel += ext_size;
8787 	  }
8788       }
8789 
8790   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8791 
8792   for (i = 0, p = sort; i < count; i++, p += sort_elt)
8793     {
8794       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8795       if (s->type != reloc_class_relative)
8796 	break;
8797     }
8798   ret = i;
8799   s_non_relative = p;
8800 
8801   sq = (struct elf_link_sort_rela *) s_non_relative;
8802   for (; i < count; i++, p += sort_elt)
8803     {
8804       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8805       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8806 	sq = sp;
8807       sp->u.offset = sq->rela->r_offset;
8808     }
8809 
8810   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8811 
8812   struct elf_link_hash_table *htab = elf_hash_table (info);
8813   if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8814     {
8815       /* We have plt relocs in .rela.dyn.  */
8816       sq = (struct elf_link_sort_rela *) sort;
8817       for (i = 0; i < count; i++)
8818 	if (sq[count - i - 1].type != reloc_class_plt)
8819 	  break;
8820       if (i != 0 && htab->srelplt->size == i * ext_size)
8821 	{
8822 	  struct bfd_link_order **plo;
8823 	  /* Put srelplt link_order last.  This is so the output_offset
8824 	     set in the next loop is correct for DT_JMPREL.  */
8825 	  for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8826 	    if ((*plo)->type == bfd_indirect_link_order
8827 		&& (*plo)->u.indirect.section == htab->srelplt)
8828 	      {
8829 		lo = *plo;
8830 		*plo = lo->next;
8831 	      }
8832 	    else
8833 	      plo = &(*plo)->next;
8834 	  *plo = lo;
8835 	  lo->next = NULL;
8836 	  dynamic_relocs->map_tail.link_order = lo;
8837 	}
8838     }
8839 
8840   p = sort;
8841   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8842     if (lo->type == bfd_indirect_link_order)
8843       {
8844 	bfd_byte *erel, *erelend;
8845 	asection *o = lo->u.indirect.section;
8846 
8847 	erel = o->contents;
8848 	erelend = o->contents + o->size;
8849 	o->output_offset = (p - sort) / sort_elt * ext_size / opb;
8850 	while (erel < erelend)
8851 	  {
8852 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8853 	    (*swap_out) (abfd, s->rela, erel);
8854 	    p += sort_elt;
8855 	    erel += ext_size;
8856 	  }
8857       }
8858 
8859   free (sort);
8860   *psec = dynamic_relocs;
8861   return ret;
8862 }
8863 
8864 /* Add a symbol to the output symbol string table.  */
8865 
8866 static int
elf_link_output_symstrtab(struct elf_final_link_info * flinfo,const char * name,Elf_Internal_Sym * elfsym,asection * input_sec,struct elf_link_hash_entry * h)8867 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8868 			   const char *name,
8869 			   Elf_Internal_Sym *elfsym,
8870 			   asection *input_sec,
8871 			   struct elf_link_hash_entry *h)
8872 {
8873   int (*output_symbol_hook)
8874     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8875      struct elf_link_hash_entry *);
8876   struct elf_link_hash_table *hash_table;
8877   const struct elf_backend_data *bed;
8878   bfd_size_type strtabsize;
8879 
8880   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8881 
8882   bed = get_elf_backend_data (flinfo->output_bfd);
8883   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8884   if (output_symbol_hook != NULL)
8885     {
8886       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8887       if (ret != 1)
8888 	return ret;
8889     }
8890 
8891   if (name == NULL
8892       || *name == '\0'
8893       || (input_sec->flags & SEC_EXCLUDE))
8894     elfsym->st_name = (unsigned long) -1;
8895   else
8896     {
8897       /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8898 	 to get the final offset for st_name.  */
8899       elfsym->st_name
8900 	= (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8901 					       name, FALSE);
8902       if (elfsym->st_name == (unsigned long) -1)
8903 	return 0;
8904     }
8905 
8906   hash_table = elf_hash_table (flinfo->info);
8907   strtabsize = hash_table->strtabsize;
8908   if (strtabsize <= hash_table->strtabcount)
8909     {
8910       strtabsize += strtabsize;
8911       hash_table->strtabsize = strtabsize;
8912       strtabsize *= sizeof (*hash_table->strtab);
8913       hash_table->strtab
8914 	= (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8915 						 strtabsize);
8916       if (hash_table->strtab == NULL)
8917 	return 0;
8918     }
8919   hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8920   hash_table->strtab[hash_table->strtabcount].dest_index
8921     = hash_table->strtabcount;
8922   hash_table->strtab[hash_table->strtabcount].destshndx_index
8923     = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8924 
8925   bfd_get_symcount (flinfo->output_bfd) += 1;
8926   hash_table->strtabcount += 1;
8927 
8928   return 1;
8929 }
8930 
8931 /* Swap symbols out to the symbol table and flush the output symbols to
8932    the file.  */
8933 
8934 static bfd_boolean
elf_link_swap_symbols_out(struct elf_final_link_info * flinfo)8935 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8936 {
8937   struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8938   bfd_size_type amt;
8939   size_t i;
8940   const struct elf_backend_data *bed;
8941   bfd_byte *symbuf;
8942   Elf_Internal_Shdr *hdr;
8943   file_ptr pos;
8944   bfd_boolean ret;
8945 
8946   if (!hash_table->strtabcount)
8947     return TRUE;
8948 
8949   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8950 
8951   bed = get_elf_backend_data (flinfo->output_bfd);
8952 
8953   amt = bed->s->sizeof_sym * hash_table->strtabcount;
8954   symbuf = (bfd_byte *) bfd_malloc (amt);
8955   if (symbuf == NULL)
8956     return FALSE;
8957 
8958   if (flinfo->symshndxbuf)
8959     {
8960       amt = sizeof (Elf_External_Sym_Shndx);
8961       amt *= bfd_get_symcount (flinfo->output_bfd);
8962       flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8963       if (flinfo->symshndxbuf == NULL)
8964 	{
8965 	  free (symbuf);
8966 	  return FALSE;
8967 	}
8968     }
8969 
8970   for (i = 0; i < hash_table->strtabcount; i++)
8971     {
8972       struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8973       if (elfsym->sym.st_name == (unsigned long) -1)
8974 	elfsym->sym.st_name = 0;
8975       else
8976 	elfsym->sym.st_name
8977 	  = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8978 						    elfsym->sym.st_name);
8979       bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8980 			       ((bfd_byte *) symbuf
8981 				+ (elfsym->dest_index
8982 				   * bed->s->sizeof_sym)),
8983 			       (flinfo->symshndxbuf
8984 				+ elfsym->destshndx_index));
8985     }
8986 
8987   hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8988   pos = hdr->sh_offset + hdr->sh_size;
8989   amt = hash_table->strtabcount * bed->s->sizeof_sym;
8990   if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8991       && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8992     {
8993       hdr->sh_size += amt;
8994       ret = TRUE;
8995     }
8996   else
8997     ret = FALSE;
8998 
8999   free (symbuf);
9000 
9001   free (hash_table->strtab);
9002   hash_table->strtab = NULL;
9003 
9004   return ret;
9005 }
9006 
9007 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
9008 
9009 static bfd_boolean
check_dynsym(bfd * abfd,Elf_Internal_Sym * sym)9010 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9011 {
9012   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9013       && sym->st_shndx < SHN_LORESERVE)
9014     {
9015       /* The gABI doesn't support dynamic symbols in output sections
9016 	 beyond 64k.  */
9017       (*_bfd_error_handler)
9018 	(_("%B: Too many sections: %d (>= %d)"),
9019 	 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9020       bfd_set_error (bfd_error_nonrepresentable_section);
9021       return FALSE;
9022     }
9023   return TRUE;
9024 }
9025 
9026 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9027    allowing an unsatisfied unversioned symbol in the DSO to match a
9028    versioned symbol that would normally require an explicit version.
9029    We also handle the case that a DSO references a hidden symbol
9030    which may be satisfied by a versioned symbol in another DSO.  */
9031 
9032 static bfd_boolean
elf_link_check_versioned_symbol(struct bfd_link_info * info,const struct elf_backend_data * bed,struct elf_link_hash_entry * h)9033 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9034 				 const struct elf_backend_data *bed,
9035 				 struct elf_link_hash_entry *h)
9036 {
9037   bfd *abfd;
9038   struct elf_link_loaded_list *loaded;
9039 
9040   if (!is_elf_hash_table (info->hash))
9041     return FALSE;
9042 
9043   /* Check indirect symbol.  */
9044   while (h->root.type == bfd_link_hash_indirect)
9045     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9046 
9047   switch (h->root.type)
9048     {
9049     default:
9050       abfd = NULL;
9051       break;
9052 
9053     case bfd_link_hash_undefined:
9054     case bfd_link_hash_undefweak:
9055       abfd = h->root.u.undef.abfd;
9056       if (abfd == NULL
9057 	  || (abfd->flags & DYNAMIC) == 0
9058 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9059 	return FALSE;
9060       break;
9061 
9062     case bfd_link_hash_defined:
9063     case bfd_link_hash_defweak:
9064       abfd = h->root.u.def.section->owner;
9065       break;
9066 
9067     case bfd_link_hash_common:
9068       abfd = h->root.u.c.p->section->owner;
9069       break;
9070     }
9071   BFD_ASSERT (abfd != NULL);
9072 
9073   for (loaded = elf_hash_table (info)->loaded;
9074        loaded != NULL;
9075        loaded = loaded->next)
9076     {
9077       bfd *input;
9078       Elf_Internal_Shdr *hdr;
9079       size_t symcount;
9080       size_t extsymcount;
9081       size_t extsymoff;
9082       Elf_Internal_Shdr *versymhdr;
9083       Elf_Internal_Sym *isym;
9084       Elf_Internal_Sym *isymend;
9085       Elf_Internal_Sym *isymbuf;
9086       Elf_External_Versym *ever;
9087       Elf_External_Versym *extversym;
9088 
9089       input = loaded->abfd;
9090 
9091       /* We check each DSO for a possible hidden versioned definition.  */
9092       if (input == abfd
9093 	  || (input->flags & DYNAMIC) == 0
9094 	  || elf_dynversym (input) == 0)
9095 	continue;
9096 
9097       hdr = &elf_tdata (input)->dynsymtab_hdr;
9098 
9099       symcount = hdr->sh_size / bed->s->sizeof_sym;
9100       if (elf_bad_symtab (input))
9101 	{
9102 	  extsymcount = symcount;
9103 	  extsymoff = 0;
9104 	}
9105       else
9106 	{
9107 	  extsymcount = symcount - hdr->sh_info;
9108 	  extsymoff = hdr->sh_info;
9109 	}
9110 
9111       if (extsymcount == 0)
9112 	continue;
9113 
9114       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9115 				      NULL, NULL, NULL);
9116       if (isymbuf == NULL)
9117 	return FALSE;
9118 
9119       /* Read in any version definitions.  */
9120       versymhdr = &elf_tdata (input)->dynversym_hdr;
9121       extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9122       if (extversym == NULL)
9123 	goto error_ret;
9124 
9125       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9126 	  || (bfd_bread (extversym, versymhdr->sh_size, input)
9127 	      != versymhdr->sh_size))
9128 	{
9129 	  free (extversym);
9130 	error_ret:
9131 	  free (isymbuf);
9132 	  return FALSE;
9133 	}
9134 
9135       ever = extversym + extsymoff;
9136       isymend = isymbuf + extsymcount;
9137       for (isym = isymbuf; isym < isymend; isym++, ever++)
9138 	{
9139 	  const char *name;
9140 	  Elf_Internal_Versym iver;
9141 	  unsigned short version_index;
9142 
9143 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9144 	      || isym->st_shndx == SHN_UNDEF)
9145 	    continue;
9146 
9147 	  name = bfd_elf_string_from_elf_section (input,
9148 						  hdr->sh_link,
9149 						  isym->st_name);
9150 	  if (strcmp (name, h->root.root.string) != 0)
9151 	    continue;
9152 
9153 	  _bfd_elf_swap_versym_in (input, ever, &iver);
9154 
9155 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9156 	      && !(h->def_regular
9157 		   && h->forced_local))
9158 	    {
9159 	      /* If we have a non-hidden versioned sym, then it should
9160 		 have provided a definition for the undefined sym unless
9161 		 it is defined in a non-shared object and forced local.
9162 	       */
9163 	      abort ();
9164 	    }
9165 
9166 	  version_index = iver.vs_vers & VERSYM_VERSION;
9167 	  if (version_index == 1 || version_index == 2)
9168 	    {
9169 	      /* This is the base or first version.  We can use it.  */
9170 	      free (extversym);
9171 	      free (isymbuf);
9172 	      return TRUE;
9173 	    }
9174 	}
9175 
9176       free (extversym);
9177       free (isymbuf);
9178     }
9179 
9180   return FALSE;
9181 }
9182 
9183 /* Convert ELF common symbol TYPE.  */
9184 
9185 static int
elf_link_convert_common_type(struct bfd_link_info * info,int type)9186 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9187 {
9188   /* Commom symbol can only appear in relocatable link.  */
9189   if (!bfd_link_relocatable (info))
9190     abort ();
9191   switch (info->elf_stt_common)
9192     {
9193     case unchanged:
9194       break;
9195     case elf_stt_common:
9196       type = STT_COMMON;
9197       break;
9198     case no_elf_stt_common:
9199       type = STT_OBJECT;
9200       break;
9201     }
9202   return type;
9203 }
9204 
9205 /* Add an external symbol to the symbol table.  This is called from
9206    the hash table traversal routine.  When generating a shared object,
9207    we go through the symbol table twice.  The first time we output
9208    anything that might have been forced to local scope in a version
9209    script.  The second time we output the symbols that are still
9210    global symbols.  */
9211 
9212 static bfd_boolean
elf_link_output_extsym(struct bfd_hash_entry * bh,void * data)9213 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9214 {
9215   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9216   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9217   struct elf_final_link_info *flinfo = eoinfo->flinfo;
9218   bfd_boolean strip;
9219   Elf_Internal_Sym sym;
9220   asection *input_sec;
9221   const struct elf_backend_data *bed;
9222   long indx;
9223   int ret;
9224   unsigned int type;
9225   /* A symbol is bound locally if it is forced local or it is locally
9226      defined, hidden versioned, not referenced by shared library and
9227      not exported when linking executable.  */
9228   bfd_boolean local_bind = (h->forced_local
9229 			    || (bfd_link_executable (flinfo->info)
9230 				&& !flinfo->info->export_dynamic
9231 				&& !h->dynamic
9232 				&& !h->ref_dynamic
9233 				&& h->def_regular
9234 				&& h->versioned == versioned_hidden));
9235 
9236   if (h->root.type == bfd_link_hash_warning)
9237     {
9238       h = (struct elf_link_hash_entry *) h->root.u.i.link;
9239       if (h->root.type == bfd_link_hash_new)
9240 	return TRUE;
9241     }
9242 
9243   /* Decide whether to output this symbol in this pass.  */
9244   if (eoinfo->localsyms)
9245     {
9246       if (!local_bind)
9247 	return TRUE;
9248     }
9249   else
9250     {
9251       if (local_bind)
9252 	return TRUE;
9253     }
9254 
9255   bed = get_elf_backend_data (flinfo->output_bfd);
9256 
9257   if (h->root.type == bfd_link_hash_undefined)
9258     {
9259       /* If we have an undefined symbol reference here then it must have
9260 	 come from a shared library that is being linked in.  (Undefined
9261 	 references in regular files have already been handled unless
9262 	 they are in unreferenced sections which are removed by garbage
9263 	 collection).  */
9264       bfd_boolean ignore_undef = FALSE;
9265 
9266       /* Some symbols may be special in that the fact that they're
9267 	 undefined can be safely ignored - let backend determine that.  */
9268       if (bed->elf_backend_ignore_undef_symbol)
9269 	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9270 
9271       /* If we are reporting errors for this situation then do so now.  */
9272       if (!ignore_undef
9273 	  && h->ref_dynamic
9274 	  && (!h->ref_regular || flinfo->info->gc_sections)
9275 	  && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9276 	  && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9277 	(*flinfo->info->callbacks->undefined_symbol)
9278 	  (flinfo->info, h->root.root.string,
9279 	   h->ref_regular ? NULL : h->root.u.undef.abfd,
9280 	   NULL, 0,
9281 	   flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9282 
9283       /* Strip a global symbol defined in a discarded section.  */
9284       if (h->indx == -3)
9285 	return TRUE;
9286     }
9287 
9288   /* We should also warn if a forced local symbol is referenced from
9289      shared libraries.  */
9290   if (bfd_link_executable (flinfo->info)
9291       && h->forced_local
9292       && h->ref_dynamic
9293       && h->def_regular
9294       && !h->dynamic_def
9295       && h->ref_dynamic_nonweak
9296       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9297     {
9298       bfd *def_bfd;
9299       const char *msg;
9300       struct elf_link_hash_entry *hi = h;
9301 
9302       /* Check indirect symbol.  */
9303       while (hi->root.type == bfd_link_hash_indirect)
9304 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9305 
9306       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9307 	msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9308       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9309 	msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9310       else
9311 	msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9312       def_bfd = flinfo->output_bfd;
9313       if (hi->root.u.def.section != bfd_abs_section_ptr)
9314 	def_bfd = hi->root.u.def.section->owner;
9315       (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9316 			     h->root.root.string);
9317       bfd_set_error (bfd_error_bad_value);
9318       eoinfo->failed = TRUE;
9319       return FALSE;
9320     }
9321 
9322   /* We don't want to output symbols that have never been mentioned by
9323      a regular file, or that we have been told to strip.  However, if
9324      h->indx is set to -2, the symbol is used by a reloc and we must
9325      output it.  */
9326   strip = FALSE;
9327   if (h->indx == -2)
9328     ;
9329   else if ((h->def_dynamic
9330 	    || h->ref_dynamic
9331 	    || h->root.type == bfd_link_hash_new)
9332 	   && !h->def_regular
9333 	   && !h->ref_regular)
9334     strip = TRUE;
9335   else if (flinfo->info->strip == strip_all)
9336     strip = TRUE;
9337   else if (flinfo->info->strip == strip_some
9338 	   && bfd_hash_lookup (flinfo->info->keep_hash,
9339 			       h->root.root.string, FALSE, FALSE) == NULL)
9340     strip = TRUE;
9341   else if ((h->root.type == bfd_link_hash_defined
9342 	    || h->root.type == bfd_link_hash_defweak)
9343 	   && ((flinfo->info->strip_discarded
9344 		&& discarded_section (h->root.u.def.section))
9345 	       || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9346 		   && h->root.u.def.section->owner != NULL
9347 		   && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9348     strip = TRUE;
9349   else if ((h->root.type == bfd_link_hash_undefined
9350 	    || h->root.type == bfd_link_hash_undefweak)
9351 	   && h->root.u.undef.abfd != NULL
9352 	   && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9353     strip = TRUE;
9354 
9355   type = h->type;
9356 
9357   /* If we're stripping it, and it's not a dynamic symbol, there's
9358      nothing else to do.   However, if it is a forced local symbol or
9359      an ifunc symbol we need to give the backend finish_dynamic_symbol
9360      function a chance to make it dynamic.  */
9361   if (strip
9362       && h->dynindx == -1
9363       && type != STT_GNU_IFUNC
9364       && !h->forced_local)
9365     return TRUE;
9366 
9367   sym.st_value = 0;
9368   sym.st_size = h->size;
9369   sym.st_other = h->other;
9370   switch (h->root.type)
9371     {
9372     default:
9373     case bfd_link_hash_new:
9374     case bfd_link_hash_warning:
9375       abort ();
9376       return FALSE;
9377 
9378     case bfd_link_hash_undefined:
9379     case bfd_link_hash_undefweak:
9380       input_sec = bfd_und_section_ptr;
9381       sym.st_shndx = SHN_UNDEF;
9382       break;
9383 
9384     case bfd_link_hash_defined:
9385     case bfd_link_hash_defweak:
9386       {
9387 	input_sec = h->root.u.def.section;
9388 	if (input_sec->output_section != NULL)
9389 	  {
9390 	    sym.st_shndx =
9391 	      _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9392 						 input_sec->output_section);
9393 	    if (sym.st_shndx == SHN_BAD)
9394 	      {
9395 		(*_bfd_error_handler)
9396 		  (_("%B: could not find output section %A for input section %A"),
9397 		   flinfo->output_bfd, input_sec->output_section, input_sec);
9398 		bfd_set_error (bfd_error_nonrepresentable_section);
9399 		eoinfo->failed = TRUE;
9400 		return FALSE;
9401 	      }
9402 
9403 	    /* ELF symbols in relocatable files are section relative,
9404 	       but in nonrelocatable files they are virtual
9405 	       addresses.  */
9406 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
9407 	    if (!bfd_link_relocatable (flinfo->info))
9408 	      {
9409 		sym.st_value += input_sec->output_section->vma;
9410 		if (h->type == STT_TLS)
9411 		  {
9412 		    asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9413 		    if (tls_sec != NULL)
9414 		      sym.st_value -= tls_sec->vma;
9415 		  }
9416 	      }
9417 	  }
9418 	else
9419 	  {
9420 	    BFD_ASSERT (input_sec->owner == NULL
9421 			|| (input_sec->owner->flags & DYNAMIC) != 0);
9422 	    sym.st_shndx = SHN_UNDEF;
9423 	    input_sec = bfd_und_section_ptr;
9424 	  }
9425       }
9426       break;
9427 
9428     case bfd_link_hash_common:
9429       input_sec = h->root.u.c.p->section;
9430       sym.st_shndx = bed->common_section_index (input_sec);
9431       sym.st_value = 1 << h->root.u.c.p->alignment_power;
9432       break;
9433 
9434     case bfd_link_hash_indirect:
9435       /* These symbols are created by symbol versioning.  They point
9436 	 to the decorated version of the name.  For example, if the
9437 	 symbol foo@@GNU_1.2 is the default, which should be used when
9438 	 foo is used with no version, then we add an indirect symbol
9439 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
9440 	 since the indirected symbol is already in the hash table.  */
9441       return TRUE;
9442     }
9443 
9444   if (type == STT_COMMON || type == STT_OBJECT)
9445     switch (h->root.type)
9446       {
9447       case bfd_link_hash_common:
9448 	type = elf_link_convert_common_type (flinfo->info, type);
9449 	break;
9450       case bfd_link_hash_defined:
9451       case bfd_link_hash_defweak:
9452 	if (bed->common_definition (&sym))
9453 	  type = elf_link_convert_common_type (flinfo->info, type);
9454 	else
9455 	  type = STT_OBJECT;
9456 	break;
9457       case bfd_link_hash_undefined:
9458       case bfd_link_hash_undefweak:
9459 	break;
9460       default:
9461 	abort ();
9462       }
9463 
9464   if (local_bind)
9465     {
9466       sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9467       /* Turn off visibility on local symbol.  */
9468       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9469     }
9470   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
9471   else if (h->unique_global && h->def_regular)
9472     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9473   else if (h->root.type == bfd_link_hash_undefweak
9474 	   || h->root.type == bfd_link_hash_defweak)
9475     sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9476   else
9477     sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9478   sym.st_target_internal = h->target_internal;
9479 
9480   /* Give the processor backend a chance to tweak the symbol value,
9481      and also to finish up anything that needs to be done for this
9482      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
9483      forced local syms when non-shared is due to a historical quirk.
9484      STT_GNU_IFUNC symbol must go through PLT.  */
9485   if ((h->type == STT_GNU_IFUNC
9486        && h->def_regular
9487        && !bfd_link_relocatable (flinfo->info))
9488       || ((h->dynindx != -1
9489 	   || h->forced_local)
9490 	  && ((bfd_link_pic (flinfo->info)
9491 	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9492 		   || h->root.type != bfd_link_hash_undefweak))
9493 	      || !h->forced_local)
9494 	  && elf_hash_table (flinfo->info)->dynamic_sections_created))
9495     {
9496       if (! ((*bed->elf_backend_finish_dynamic_symbol)
9497 	     (flinfo->output_bfd, flinfo->info, h, &sym)))
9498 	{
9499 	  eoinfo->failed = TRUE;
9500 	  return FALSE;
9501 	}
9502     }
9503 
9504   /* If we are marking the symbol as undefined, and there are no
9505      non-weak references to this symbol from a regular object, then
9506      mark the symbol as weak undefined; if there are non-weak
9507      references, mark the symbol as strong.  We can't do this earlier,
9508      because it might not be marked as undefined until the
9509      finish_dynamic_symbol routine gets through with it.  */
9510   if (sym.st_shndx == SHN_UNDEF
9511       && h->ref_regular
9512       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9513 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9514     {
9515       int bindtype;
9516       type = ELF_ST_TYPE (sym.st_info);
9517 
9518       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9519       if (type == STT_GNU_IFUNC)
9520 	type = STT_FUNC;
9521 
9522       if (h->ref_regular_nonweak)
9523 	bindtype = STB_GLOBAL;
9524       else
9525 	bindtype = STB_WEAK;
9526       sym.st_info = ELF_ST_INFO (bindtype, type);
9527     }
9528 
9529   /* If this is a symbol defined in a dynamic library, don't use the
9530      symbol size from the dynamic library.  Relinking an executable
9531      against a new library may introduce gratuitous changes in the
9532      executable's symbols if we keep the size.  */
9533   if (sym.st_shndx == SHN_UNDEF
9534       && !h->def_regular
9535       && h->def_dynamic)
9536     sym.st_size = 0;
9537 
9538   /* If a non-weak symbol with non-default visibility is not defined
9539      locally, it is a fatal error.  */
9540   if (!bfd_link_relocatable (flinfo->info)
9541       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9542       && ELF_ST_BIND (sym.st_info) != STB_WEAK
9543       && h->root.type == bfd_link_hash_undefined
9544       && !h->def_regular)
9545     {
9546       const char *msg;
9547 
9548       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9549 	msg = _("%B: protected symbol `%s' isn't defined");
9550       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9551 	msg = _("%B: internal symbol `%s' isn't defined");
9552       else
9553 	msg = _("%B: hidden symbol `%s' isn't defined");
9554       (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9555       bfd_set_error (bfd_error_bad_value);
9556       eoinfo->failed = TRUE;
9557       return FALSE;
9558     }
9559 
9560   /* If this symbol should be put in the .dynsym section, then put it
9561      there now.  We already know the symbol index.  We also fill in
9562      the entry in the .hash section.  */
9563   if (elf_hash_table (flinfo->info)->dynsym != NULL
9564       && h->dynindx != -1
9565       && elf_hash_table (flinfo->info)->dynamic_sections_created)
9566     {
9567       bfd_byte *esym;
9568 
9569       /* Since there is no version information in the dynamic string,
9570 	 if there is no version info in symbol version section, we will
9571 	 have a run-time problem if not linking executable, referenced
9572 	 by shared library, not locally defined, or not bound locally.
9573       */
9574       if (h->verinfo.verdef == NULL
9575 	  && !local_bind
9576 	  && (!bfd_link_executable (flinfo->info)
9577 	      || h->ref_dynamic
9578 	      || !h->def_regular))
9579 	{
9580 	  char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9581 
9582 	  if (p && p [1] != '\0')
9583 	    {
9584 	      (*_bfd_error_handler)
9585 		(_("%B: No symbol version section for versioned symbol `%s'"),
9586 		 flinfo->output_bfd, h->root.root.string);
9587 	      eoinfo->failed = TRUE;
9588 	      return FALSE;
9589 	    }
9590 	}
9591 
9592       sym.st_name = h->dynstr_index;
9593       esym = (elf_hash_table (flinfo->info)->dynsym->contents
9594 	      + h->dynindx * bed->s->sizeof_sym);
9595       if (!check_dynsym (flinfo->output_bfd, &sym))
9596 	{
9597 	  eoinfo->failed = TRUE;
9598 	  return FALSE;
9599 	}
9600       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9601 
9602       if (flinfo->hash_sec != NULL)
9603 	{
9604 	  size_t hash_entry_size;
9605 	  bfd_byte *bucketpos;
9606 	  bfd_vma chain;
9607 	  size_t bucketcount;
9608 	  size_t bucket;
9609 
9610 	  bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9611 	  bucket = h->u.elf_hash_value % bucketcount;
9612 
9613 	  hash_entry_size
9614 	    = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9615 	  bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9616 		       + (bucket + 2) * hash_entry_size);
9617 	  chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9618 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9619 		   bucketpos);
9620 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9621 		   ((bfd_byte *) flinfo->hash_sec->contents
9622 		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9623 	}
9624 
9625       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9626 	{
9627 	  Elf_Internal_Versym iversym;
9628 	  Elf_External_Versym *eversym;
9629 
9630 	  if (!h->def_regular)
9631 	    {
9632 	      if (h->verinfo.verdef == NULL
9633 		  || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9634 		      & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9635 		iversym.vs_vers = 0;
9636 	      else
9637 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9638 	    }
9639 	  else
9640 	    {
9641 	      if (h->verinfo.vertree == NULL)
9642 		iversym.vs_vers = 1;
9643 	      else
9644 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9645 	      if (flinfo->info->create_default_symver)
9646 		iversym.vs_vers++;
9647 	    }
9648 
9649 	  /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9650 	     defined locally.  */
9651 	  if (h->versioned == versioned_hidden && h->def_regular)
9652 	    iversym.vs_vers |= VERSYM_HIDDEN;
9653 
9654 	  eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9655 	  eversym += h->dynindx;
9656 	  _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9657 	}
9658     }
9659 
9660   /* If the symbol is undefined, and we didn't output it to .dynsym,
9661      strip it from .symtab too.  Obviously we can't do this for
9662      relocatable output or when needed for --emit-relocs.  */
9663   else if (input_sec == bfd_und_section_ptr
9664 	   && h->indx != -2
9665 	   && !bfd_link_relocatable (flinfo->info))
9666     return TRUE;
9667   /* Also strip others that we couldn't earlier due to dynamic symbol
9668      processing.  */
9669   if (strip)
9670     return TRUE;
9671   if ((input_sec->flags & SEC_EXCLUDE) != 0)
9672     return TRUE;
9673 
9674   /* Output a FILE symbol so that following locals are not associated
9675      with the wrong input file.  We need one for forced local symbols
9676      if we've seen more than one FILE symbol or when we have exactly
9677      one FILE symbol but global symbols are present in a file other
9678      than the one with the FILE symbol.  We also need one if linker
9679      defined symbols are present.  In practice these conditions are
9680      always met, so just emit the FILE symbol unconditionally.  */
9681   if (eoinfo->localsyms
9682       && !eoinfo->file_sym_done
9683       && eoinfo->flinfo->filesym_count != 0)
9684     {
9685       Elf_Internal_Sym fsym;
9686 
9687       memset (&fsym, 0, sizeof (fsym));
9688       fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9689       fsym.st_shndx = SHN_ABS;
9690       if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9691 				      bfd_und_section_ptr, NULL))
9692 	return FALSE;
9693 
9694       eoinfo->file_sym_done = TRUE;
9695     }
9696 
9697   indx = bfd_get_symcount (flinfo->output_bfd);
9698   ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9699 				   input_sec, h);
9700   if (ret == 0)
9701     {
9702       eoinfo->failed = TRUE;
9703       return FALSE;
9704     }
9705   else if (ret == 1)
9706     h->indx = indx;
9707   else if (h->indx == -2)
9708     abort();
9709 
9710   return TRUE;
9711 }
9712 
9713 /* Return TRUE if special handling is done for relocs in SEC against
9714    symbols defined in discarded sections.  */
9715 
9716 static bfd_boolean
elf_section_ignore_discarded_relocs(asection * sec)9717 elf_section_ignore_discarded_relocs (asection *sec)
9718 {
9719   const struct elf_backend_data *bed;
9720 
9721   switch (sec->sec_info_type)
9722     {
9723     case SEC_INFO_TYPE_STABS:
9724     case SEC_INFO_TYPE_EH_FRAME:
9725     case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9726       return TRUE;
9727     default:
9728       break;
9729     }
9730 
9731   bed = get_elf_backend_data (sec->owner);
9732   if (bed->elf_backend_ignore_discarded_relocs != NULL
9733       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9734     return TRUE;
9735 
9736   return FALSE;
9737 }
9738 
9739 /* Return a mask saying how ld should treat relocations in SEC against
9740    symbols defined in discarded sections.  If this function returns
9741    COMPLAIN set, ld will issue a warning message.  If this function
9742    returns PRETEND set, and the discarded section was link-once and the
9743    same size as the kept link-once section, ld will pretend that the
9744    symbol was actually defined in the kept section.  Otherwise ld will
9745    zero the reloc (at least that is the intent, but some cooperation by
9746    the target dependent code is needed, particularly for REL targets).  */
9747 
9748 unsigned int
_bfd_elf_default_action_discarded(asection * sec)9749 _bfd_elf_default_action_discarded (asection *sec)
9750 {
9751   if (sec->flags & SEC_DEBUGGING)
9752     return PRETEND;
9753 
9754   if (strcmp (".eh_frame", sec->name) == 0)
9755     return 0;
9756 
9757   if (strcmp (".gcc_except_table", sec->name) == 0)
9758     return 0;
9759 
9760   return COMPLAIN | PRETEND;
9761 }
9762 
9763 /* Find a match between a section and a member of a section group.  */
9764 
9765 static asection *
match_group_member(asection * sec,asection * group,struct bfd_link_info * info)9766 match_group_member (asection *sec, asection *group,
9767 		    struct bfd_link_info *info)
9768 {
9769   asection *first = elf_next_in_group (group);
9770   asection *s = first;
9771 
9772   while (s != NULL)
9773     {
9774       if (bfd_elf_match_symbols_in_sections (s, sec, info))
9775 	return s;
9776 
9777       s = elf_next_in_group (s);
9778       if (s == first)
9779 	break;
9780     }
9781 
9782   return NULL;
9783 }
9784 
9785 /* Check if the kept section of a discarded section SEC can be used
9786    to replace it.  Return the replacement if it is OK.  Otherwise return
9787    NULL.  */
9788 
9789 asection *
_bfd_elf_check_kept_section(asection * sec,struct bfd_link_info * info)9790 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9791 {
9792   asection *kept;
9793 
9794   kept = sec->kept_section;
9795   if (kept != NULL)
9796     {
9797       if ((kept->flags & SEC_GROUP) != 0)
9798 	kept = match_group_member (sec, kept, info);
9799       if (kept != NULL
9800 	  && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9801 	      != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9802 	kept = NULL;
9803       sec->kept_section = kept;
9804     }
9805   return kept;
9806 }
9807 
9808 /* Link an input file into the linker output file.  This function
9809    handles all the sections and relocations of the input file at once.
9810    This is so that we only have to read the local symbols once, and
9811    don't have to keep them in memory.  */
9812 
9813 static bfd_boolean
elf_link_input_bfd(struct elf_final_link_info * flinfo,bfd * input_bfd)9814 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9815 {
9816   int (*relocate_section)
9817     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9818      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9819   bfd *output_bfd;
9820   Elf_Internal_Shdr *symtab_hdr;
9821   size_t locsymcount;
9822   size_t extsymoff;
9823   Elf_Internal_Sym *isymbuf;
9824   Elf_Internal_Sym *isym;
9825   Elf_Internal_Sym *isymend;
9826   long *pindex;
9827   asection **ppsection;
9828   asection *o;
9829   const struct elf_backend_data *bed;
9830   struct elf_link_hash_entry **sym_hashes;
9831   bfd_size_type address_size;
9832   bfd_vma r_type_mask;
9833   int r_sym_shift;
9834   bfd_boolean have_file_sym = FALSE;
9835 
9836   output_bfd = flinfo->output_bfd;
9837   bed = get_elf_backend_data (output_bfd);
9838   relocate_section = bed->elf_backend_relocate_section;
9839 
9840   /* If this is a dynamic object, we don't want to do anything here:
9841      we don't want the local symbols, and we don't want the section
9842      contents.  */
9843   if ((input_bfd->flags & DYNAMIC) != 0)
9844     return TRUE;
9845 
9846   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9847   if (elf_bad_symtab (input_bfd))
9848     {
9849       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9850       extsymoff = 0;
9851     }
9852   else
9853     {
9854       locsymcount = symtab_hdr->sh_info;
9855       extsymoff = symtab_hdr->sh_info;
9856     }
9857 
9858   /* Read the local symbols.  */
9859   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9860   if (isymbuf == NULL && locsymcount != 0)
9861     {
9862       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9863 				      flinfo->internal_syms,
9864 				      flinfo->external_syms,
9865 				      flinfo->locsym_shndx);
9866       if (isymbuf == NULL)
9867 	return FALSE;
9868     }
9869 
9870   /* Find local symbol sections and adjust values of symbols in
9871      SEC_MERGE sections.  Write out those local symbols we know are
9872      going into the output file.  */
9873   isymend = isymbuf + locsymcount;
9874   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9875        isym < isymend;
9876        isym++, pindex++, ppsection++)
9877     {
9878       asection *isec;
9879       const char *name;
9880       Elf_Internal_Sym osym;
9881       long indx;
9882       int ret;
9883 
9884       *pindex = -1;
9885 
9886       if (elf_bad_symtab (input_bfd))
9887 	{
9888 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9889 	    {
9890 	      *ppsection = NULL;
9891 	      continue;
9892 	    }
9893 	}
9894 
9895       if (isym->st_shndx == SHN_UNDEF)
9896 	isec = bfd_und_section_ptr;
9897       else if (isym->st_shndx == SHN_ABS)
9898 	isec = bfd_abs_section_ptr;
9899       else if (isym->st_shndx == SHN_COMMON)
9900 	isec = bfd_com_section_ptr;
9901       else
9902 	{
9903 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9904 	  if (isec == NULL)
9905 	    {
9906 	      /* Don't attempt to output symbols with st_shnx in the
9907 		 reserved range other than SHN_ABS and SHN_COMMON.  */
9908 	      *ppsection = NULL;
9909 	      continue;
9910 	    }
9911 	  else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9912 		   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9913 	    isym->st_value =
9914 	      _bfd_merged_section_offset (output_bfd, &isec,
9915 					  elf_section_data (isec)->sec_info,
9916 					  isym->st_value);
9917 	}
9918 
9919       *ppsection = isec;
9920 
9921       /* Don't output the first, undefined, symbol.  In fact, don't
9922 	 output any undefined local symbol.  */
9923       if (isec == bfd_und_section_ptr)
9924 	continue;
9925 
9926       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9927 	{
9928 	  /* We never output section symbols.  Instead, we use the
9929 	     section symbol of the corresponding section in the output
9930 	     file.  */
9931 	  continue;
9932 	}
9933 
9934       /* If we are stripping all symbols, we don't want to output this
9935 	 one.  */
9936       if (flinfo->info->strip == strip_all)
9937 	continue;
9938 
9939       /* If we are discarding all local symbols, we don't want to
9940 	 output this one.  If we are generating a relocatable output
9941 	 file, then some of the local symbols may be required by
9942 	 relocs; we output them below as we discover that they are
9943 	 needed.  */
9944       if (flinfo->info->discard == discard_all)
9945 	continue;
9946 
9947       /* If this symbol is defined in a section which we are
9948 	 discarding, we don't need to keep it.  */
9949       if (isym->st_shndx != SHN_UNDEF
9950 	  && isym->st_shndx < SHN_LORESERVE
9951 	  && bfd_section_removed_from_list (output_bfd,
9952 					    isec->output_section))
9953 	continue;
9954 
9955       /* Get the name of the symbol.  */
9956       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9957 					      isym->st_name);
9958       if (name == NULL)
9959 	return FALSE;
9960 
9961       /* See if we are discarding symbols with this name.  */
9962       if ((flinfo->info->strip == strip_some
9963 	   && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9964 	       == NULL))
9965 	  || (((flinfo->info->discard == discard_sec_merge
9966 		&& (isec->flags & SEC_MERGE)
9967 		&& !bfd_link_relocatable (flinfo->info))
9968 	       || flinfo->info->discard == discard_l)
9969 	      && bfd_is_local_label_name (input_bfd, name)))
9970 	continue;
9971 
9972       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9973 	{
9974 	  if (input_bfd->lto_output)
9975 	    /* -flto puts a temp file name here.  This means builds
9976 	       are not reproducible.  Discard the symbol.  */
9977 	    continue;
9978 	  have_file_sym = TRUE;
9979 	  flinfo->filesym_count += 1;
9980 	}
9981       if (!have_file_sym)
9982 	{
9983 	  /* In the absence of debug info, bfd_find_nearest_line uses
9984 	     FILE symbols to determine the source file for local
9985 	     function symbols.  Provide a FILE symbol here if input
9986 	     files lack such, so that their symbols won't be
9987 	     associated with a previous input file.  It's not the
9988 	     source file, but the best we can do.  */
9989 	  have_file_sym = TRUE;
9990 	  flinfo->filesym_count += 1;
9991 	  memset (&osym, 0, sizeof (osym));
9992 	  osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9993 	  osym.st_shndx = SHN_ABS;
9994 	  if (!elf_link_output_symstrtab (flinfo,
9995 					  (input_bfd->lto_output ? NULL
9996 					   : input_bfd->filename),
9997 					  &osym, bfd_abs_section_ptr,
9998 					  NULL))
9999 	    return FALSE;
10000 	}
10001 
10002       osym = *isym;
10003 
10004       /* Adjust the section index for the output file.  */
10005       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10006 							 isec->output_section);
10007       if (osym.st_shndx == SHN_BAD)
10008 	return FALSE;
10009 
10010       /* ELF symbols in relocatable files are section relative, but
10011 	 in executable files they are virtual addresses.  Note that
10012 	 this code assumes that all ELF sections have an associated
10013 	 BFD section with a reasonable value for output_offset; below
10014 	 we assume that they also have a reasonable value for
10015 	 output_section.  Any special sections must be set up to meet
10016 	 these requirements.  */
10017       osym.st_value += isec->output_offset;
10018       if (!bfd_link_relocatable (flinfo->info))
10019 	{
10020 	  osym.st_value += isec->output_section->vma;
10021 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10022 	    {
10023 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
10024 	      BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10025 	      osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10026 	    }
10027 	}
10028 
10029       indx = bfd_get_symcount (output_bfd);
10030       ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10031       if (ret == 0)
10032 	return FALSE;
10033       else if (ret == 1)
10034 	*pindex = indx;
10035     }
10036 
10037   if (bed->s->arch_size == 32)
10038     {
10039       r_type_mask = 0xff;
10040       r_sym_shift = 8;
10041       address_size = 4;
10042     }
10043   else
10044     {
10045       r_type_mask = 0xffffffff;
10046       r_sym_shift = 32;
10047       address_size = 8;
10048     }
10049 
10050   /* Relocate the contents of each section.  */
10051   sym_hashes = elf_sym_hashes (input_bfd);
10052   for (o = input_bfd->sections; o != NULL; o = o->next)
10053     {
10054       bfd_byte *contents;
10055 
10056       if (! o->linker_mark)
10057 	{
10058 	  /* This section was omitted from the link.  */
10059 	  continue;
10060 	}
10061 
10062       if (bfd_link_relocatable (flinfo->info)
10063 	  && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10064 	{
10065 	  /* Deal with the group signature symbol.  */
10066 	  struct bfd_elf_section_data *sec_data = elf_section_data (o);
10067 	  unsigned long symndx = sec_data->this_hdr.sh_info;
10068 	  asection *osec = o->output_section;
10069 
10070 	  if (symndx >= locsymcount
10071 	      || (elf_bad_symtab (input_bfd)
10072 		  && flinfo->sections[symndx] == NULL))
10073 	    {
10074 	      struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10075 	      while (h->root.type == bfd_link_hash_indirect
10076 		     || h->root.type == bfd_link_hash_warning)
10077 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
10078 	      /* Arrange for symbol to be output.  */
10079 	      h->indx = -2;
10080 	      elf_section_data (osec)->this_hdr.sh_info = -2;
10081 	    }
10082 	  else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10083 	    {
10084 	      /* We'll use the output section target_index.  */
10085 	      asection *sec = flinfo->sections[symndx]->output_section;
10086 	      elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10087 	    }
10088 	  else
10089 	    {
10090 	      if (flinfo->indices[symndx] == -1)
10091 		{
10092 		  /* Otherwise output the local symbol now.  */
10093 		  Elf_Internal_Sym sym = isymbuf[symndx];
10094 		  asection *sec = flinfo->sections[symndx]->output_section;
10095 		  const char *name;
10096 		  long indx;
10097 		  int ret;
10098 
10099 		  name = bfd_elf_string_from_elf_section (input_bfd,
10100 							  symtab_hdr->sh_link,
10101 							  sym.st_name);
10102 		  if (name == NULL)
10103 		    return FALSE;
10104 
10105 		  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10106 								    sec);
10107 		  if (sym.st_shndx == SHN_BAD)
10108 		    return FALSE;
10109 
10110 		  sym.st_value += o->output_offset;
10111 
10112 		  indx = bfd_get_symcount (output_bfd);
10113 		  ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10114 						   NULL);
10115 		  if (ret == 0)
10116 		    return FALSE;
10117 		  else if (ret == 1)
10118 		    flinfo->indices[symndx] = indx;
10119 		  else
10120 		    abort ();
10121 		}
10122 	      elf_section_data (osec)->this_hdr.sh_info
10123 		= flinfo->indices[symndx];
10124 	    }
10125 	}
10126 
10127       if ((o->flags & SEC_HAS_CONTENTS) == 0
10128 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10129 	continue;
10130 
10131       if ((o->flags & SEC_LINKER_CREATED) != 0)
10132 	{
10133 	  /* Section was created by _bfd_elf_link_create_dynamic_sections
10134 	     or somesuch.  */
10135 	  continue;
10136 	}
10137 
10138       /* Get the contents of the section.  They have been cached by a
10139 	 relaxation routine.  Note that o is a section in an input
10140 	 file, so the contents field will not have been set by any of
10141 	 the routines which work on output files.  */
10142       if (elf_section_data (o)->this_hdr.contents != NULL)
10143 	{
10144 	  contents = elf_section_data (o)->this_hdr.contents;
10145 	  if (bed->caches_rawsize
10146 	      && o->rawsize != 0
10147 	      && o->rawsize < o->size)
10148 	    {
10149 	      memcpy (flinfo->contents, contents, o->rawsize);
10150 	      contents = flinfo->contents;
10151 	    }
10152 	}
10153       else
10154 	{
10155 	  contents = flinfo->contents;
10156 	  if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10157 	    return FALSE;
10158 	}
10159 
10160       if ((o->flags & SEC_RELOC) != 0)
10161 	{
10162 	  Elf_Internal_Rela *internal_relocs;
10163 	  Elf_Internal_Rela *rel, *relend;
10164 	  int action_discarded;
10165 	  int ret;
10166 
10167 	  /* Get the swapped relocs.  */
10168 	  internal_relocs
10169 	    = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10170 					 flinfo->internal_relocs, FALSE);
10171 	  if (internal_relocs == NULL
10172 	      && o->reloc_count > 0)
10173 	    return FALSE;
10174 
10175 	  /* We need to reverse-copy input .ctors/.dtors sections if
10176 	     they are placed in .init_array/.finit_array for output.  */
10177 	  if (o->size > address_size
10178 	      && ((strncmp (o->name, ".ctors", 6) == 0
10179 		   && strcmp (o->output_section->name,
10180 			      ".init_array") == 0)
10181 		  || (strncmp (o->name, ".dtors", 6) == 0
10182 		      && strcmp (o->output_section->name,
10183 				 ".fini_array") == 0))
10184 	      && (o->name[6] == 0 || o->name[6] == '.'))
10185 	    {
10186 	      if (o->size != o->reloc_count * address_size)
10187 		{
10188 		  (*_bfd_error_handler)
10189 		    (_("error: %B: size of section %A is not "
10190 		       "multiple of address size"),
10191 		     input_bfd, o);
10192 		  bfd_set_error (bfd_error_on_input);
10193 		  return FALSE;
10194 		}
10195 	      o->flags |= SEC_ELF_REVERSE_COPY;
10196 	    }
10197 
10198 	  action_discarded = -1;
10199 	  if (!elf_section_ignore_discarded_relocs (o))
10200 	    action_discarded = (*bed->action_discarded) (o);
10201 
10202 	  /* Run through the relocs evaluating complex reloc symbols and
10203 	     looking for relocs against symbols from discarded sections
10204 	     or section symbols from removed link-once sections.
10205 	     Complain about relocs against discarded sections.  Zero
10206 	     relocs against removed link-once sections.  */
10207 
10208 	  rel = internal_relocs;
10209 	  relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10210 	  for ( ; rel < relend; rel++)
10211 	    {
10212 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
10213 	      unsigned int s_type;
10214 	      asection **ps, *sec;
10215 	      struct elf_link_hash_entry *h = NULL;
10216 	      const char *sym_name;
10217 
10218 	      if (r_symndx == STN_UNDEF)
10219 		continue;
10220 
10221 	      if (r_symndx >= locsymcount
10222 		  || (elf_bad_symtab (input_bfd)
10223 		      && flinfo->sections[r_symndx] == NULL))
10224 		{
10225 		  h = sym_hashes[r_symndx - extsymoff];
10226 
10227 		  /* Badly formatted input files can contain relocs that
10228 		     reference non-existant symbols.  Check here so that
10229 		     we do not seg fault.  */
10230 		  if (h == NULL)
10231 		    {
10232 		      char buffer [32];
10233 
10234 		      sprintf_vma (buffer, rel->r_info);
10235 		      (*_bfd_error_handler)
10236 			(_("error: %B contains a reloc (0x%s) for section %A "
10237 			   "that references a non-existent global symbol"),
10238 			 input_bfd, o, buffer);
10239 		      bfd_set_error (bfd_error_bad_value);
10240 		      return FALSE;
10241 		    }
10242 
10243 		  while (h->root.type == bfd_link_hash_indirect
10244 			 || h->root.type == bfd_link_hash_warning)
10245 		    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10246 
10247 		  s_type = h->type;
10248 
10249 		  /* If a plugin symbol is referenced from a non-IR file,
10250 		     mark the symbol as undefined.  Note that the
10251 		     linker may attach linker created dynamic sections
10252 		     to the plugin bfd.  Symbols defined in linker
10253 		     created sections are not plugin symbols.  */
10254 		  if (h->root.non_ir_ref
10255 		      && (h->root.type == bfd_link_hash_defined
10256 			  || h->root.type == bfd_link_hash_defweak)
10257 		      && (h->root.u.def.section->flags
10258 			  & SEC_LINKER_CREATED) == 0
10259 		      && h->root.u.def.section->owner != NULL
10260 		      && (h->root.u.def.section->owner->flags
10261 			  & BFD_PLUGIN) != 0)
10262 		    {
10263 		      h->root.type = bfd_link_hash_undefined;
10264 		      h->root.u.undef.abfd = h->root.u.def.section->owner;
10265 		    }
10266 
10267 		  ps = NULL;
10268 		  if (h->root.type == bfd_link_hash_defined
10269 		      || h->root.type == bfd_link_hash_defweak)
10270 		    ps = &h->root.u.def.section;
10271 
10272 		  sym_name = h->root.root.string;
10273 		}
10274 	      else
10275 		{
10276 		  Elf_Internal_Sym *sym = isymbuf + r_symndx;
10277 
10278 		  s_type = ELF_ST_TYPE (sym->st_info);
10279 		  ps = &flinfo->sections[r_symndx];
10280 		  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10281 					       sym, *ps);
10282 		}
10283 
10284 	      if ((s_type == STT_RELC || s_type == STT_SRELC)
10285 		  && !bfd_link_relocatable (flinfo->info))
10286 		{
10287 		  bfd_vma val;
10288 		  bfd_vma dot = (rel->r_offset
10289 				 + o->output_offset + o->output_section->vma);
10290 #ifdef DEBUG
10291 		  printf ("Encountered a complex symbol!");
10292 		  printf (" (input_bfd %s, section %s, reloc %ld\n",
10293 			  input_bfd->filename, o->name,
10294 			  (long) (rel - internal_relocs));
10295 		  printf (" symbol: idx  %8.8lx, name %s\n",
10296 			  r_symndx, sym_name);
10297 		  printf (" reloc : info %8.8lx, addr %8.8lx\n",
10298 			  (unsigned long) rel->r_info,
10299 			  (unsigned long) rel->r_offset);
10300 #endif
10301 		  if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10302 				    isymbuf, locsymcount, s_type == STT_SRELC))
10303 		    return FALSE;
10304 
10305 		  /* Symbol evaluated OK.  Update to absolute value.  */
10306 		  set_symbol_value (input_bfd, isymbuf, locsymcount,
10307 				    r_symndx, val);
10308 		  continue;
10309 		}
10310 
10311 	      if (action_discarded != -1 && ps != NULL)
10312 		{
10313 		  /* Complain if the definition comes from a
10314 		     discarded section.  */
10315 		  if ((sec = *ps) != NULL && discarded_section (sec))
10316 		    {
10317 		      BFD_ASSERT (r_symndx != STN_UNDEF);
10318 		      if (action_discarded & COMPLAIN)
10319 			(*flinfo->info->callbacks->einfo)
10320 			  (_("%X`%s' referenced in section `%A' of %B: "
10321 			     "defined in discarded section `%A' of %B\n"),
10322 			   sym_name, o, input_bfd, sec, sec->owner);
10323 
10324 		      /* Try to do the best we can to support buggy old
10325 			 versions of gcc.  Pretend that the symbol is
10326 			 really defined in the kept linkonce section.
10327 			 FIXME: This is quite broken.  Modifying the
10328 			 symbol here means we will be changing all later
10329 			 uses of the symbol, not just in this section.  */
10330 		      if (action_discarded & PRETEND)
10331 			{
10332 			  asection *kept;
10333 
10334 			  kept = _bfd_elf_check_kept_section (sec,
10335 							      flinfo->info);
10336 			  if (kept != NULL)
10337 			    {
10338 			      *ps = kept;
10339 			      continue;
10340 			    }
10341 			}
10342 		    }
10343 		}
10344 	    }
10345 
10346 	  /* Relocate the section by invoking a back end routine.
10347 
10348 	     The back end routine is responsible for adjusting the
10349 	     section contents as necessary, and (if using Rela relocs
10350 	     and generating a relocatable output file) adjusting the
10351 	     reloc addend as necessary.
10352 
10353 	     The back end routine does not have to worry about setting
10354 	     the reloc address or the reloc symbol index.
10355 
10356 	     The back end routine is given a pointer to the swapped in
10357 	     internal symbols, and can access the hash table entries
10358 	     for the external symbols via elf_sym_hashes (input_bfd).
10359 
10360 	     When generating relocatable output, the back end routine
10361 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
10362 	     output symbol is going to be a section symbol
10363 	     corresponding to the output section, which will require
10364 	     the addend to be adjusted.  */
10365 
10366 	  ret = (*relocate_section) (output_bfd, flinfo->info,
10367 				     input_bfd, o, contents,
10368 				     internal_relocs,
10369 				     isymbuf,
10370 				     flinfo->sections);
10371 	  if (!ret)
10372 	    return FALSE;
10373 
10374 	  if (ret == 2
10375 	      || bfd_link_relocatable (flinfo->info)
10376 	      || flinfo->info->emitrelocations)
10377 	    {
10378 	      Elf_Internal_Rela *irela;
10379 	      Elf_Internal_Rela *irelaend, *irelamid;
10380 	      bfd_vma last_offset;
10381 	      struct elf_link_hash_entry **rel_hash;
10382 	      struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10383 	      Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10384 	      unsigned int next_erel;
10385 	      bfd_boolean rela_normal;
10386 	      struct bfd_elf_section_data *esdi, *esdo;
10387 
10388 	      esdi = elf_section_data (o);
10389 	      esdo = elf_section_data (o->output_section);
10390 	      rela_normal = FALSE;
10391 
10392 	      /* Adjust the reloc addresses and symbol indices.  */
10393 
10394 	      irela = internal_relocs;
10395 	      irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10396 	      rel_hash = esdo->rel.hashes + esdo->rel.count;
10397 	      /* We start processing the REL relocs, if any.  When we reach
10398 		 IRELAMID in the loop, we switch to the RELA relocs.  */
10399 	      irelamid = irela;
10400 	      if (esdi->rel.hdr != NULL)
10401 		irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10402 			     * bed->s->int_rels_per_ext_rel);
10403 	      rel_hash_list = rel_hash;
10404 	      rela_hash_list = NULL;
10405 	      last_offset = o->output_offset;
10406 	      if (!bfd_link_relocatable (flinfo->info))
10407 		last_offset += o->output_section->vma;
10408 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10409 		{
10410 		  unsigned long r_symndx;
10411 		  asection *sec;
10412 		  Elf_Internal_Sym sym;
10413 
10414 		  if (next_erel == bed->s->int_rels_per_ext_rel)
10415 		    {
10416 		      rel_hash++;
10417 		      next_erel = 0;
10418 		    }
10419 
10420 		  if (irela == irelamid)
10421 		    {
10422 		      rel_hash = esdo->rela.hashes + esdo->rela.count;
10423 		      rela_hash_list = rel_hash;
10424 		      rela_normal = bed->rela_normal;
10425 		    }
10426 
10427 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
10428 							     flinfo->info, o,
10429 							     irela->r_offset);
10430 		  if (irela->r_offset >= (bfd_vma) -2)
10431 		    {
10432 		      /* This is a reloc for a deleted entry or somesuch.
10433 			 Turn it into an R_*_NONE reloc, at the same
10434 			 offset as the last reloc.  elf_eh_frame.c and
10435 			 bfd_elf_discard_info rely on reloc offsets
10436 			 being ordered.  */
10437 		      irela->r_offset = last_offset;
10438 		      irela->r_info = 0;
10439 		      irela->r_addend = 0;
10440 		      continue;
10441 		    }
10442 
10443 		  irela->r_offset += o->output_offset;
10444 
10445 		  /* Relocs in an executable have to be virtual addresses.  */
10446 		  if (!bfd_link_relocatable (flinfo->info))
10447 		    irela->r_offset += o->output_section->vma;
10448 
10449 		  last_offset = irela->r_offset;
10450 
10451 		  r_symndx = irela->r_info >> r_sym_shift;
10452 		  if (r_symndx == STN_UNDEF)
10453 		    continue;
10454 
10455 		  if (r_symndx >= locsymcount
10456 		      || (elf_bad_symtab (input_bfd)
10457 			  && flinfo->sections[r_symndx] == NULL))
10458 		    {
10459 		      struct elf_link_hash_entry *rh;
10460 		      unsigned long indx;
10461 
10462 		      /* This is a reloc against a global symbol.  We
10463 			 have not yet output all the local symbols, so
10464 			 we do not know the symbol index of any global
10465 			 symbol.  We set the rel_hash entry for this
10466 			 reloc to point to the global hash table entry
10467 			 for this symbol.  The symbol index is then
10468 			 set at the end of bfd_elf_final_link.  */
10469 		      indx = r_symndx - extsymoff;
10470 		      rh = elf_sym_hashes (input_bfd)[indx];
10471 		      while (rh->root.type == bfd_link_hash_indirect
10472 			     || rh->root.type == bfd_link_hash_warning)
10473 			rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10474 
10475 		      /* Setting the index to -2 tells
10476 			 elf_link_output_extsym that this symbol is
10477 			 used by a reloc.  */
10478 		      BFD_ASSERT (rh->indx < 0);
10479 		      rh->indx = -2;
10480 
10481 		      *rel_hash = rh;
10482 
10483 		      continue;
10484 		    }
10485 
10486 		  /* This is a reloc against a local symbol.  */
10487 
10488 		  *rel_hash = NULL;
10489 		  sym = isymbuf[r_symndx];
10490 		  sec = flinfo->sections[r_symndx];
10491 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10492 		    {
10493 		      /* I suppose the backend ought to fill in the
10494 			 section of any STT_SECTION symbol against a
10495 			 processor specific section.  */
10496 		      r_symndx = STN_UNDEF;
10497 		      if (bfd_is_abs_section (sec))
10498 			;
10499 		      else if (sec == NULL || sec->owner == NULL)
10500 			{
10501 			  bfd_set_error (bfd_error_bad_value);
10502 			  return FALSE;
10503 			}
10504 		      else
10505 			{
10506 			  asection *osec = sec->output_section;
10507 
10508 			  /* If we have discarded a section, the output
10509 			     section will be the absolute section.  In
10510 			     case of discarded SEC_MERGE sections, use
10511 			     the kept section.  relocate_section should
10512 			     have already handled discarded linkonce
10513 			     sections.  */
10514 			  if (bfd_is_abs_section (osec)
10515 			      && sec->kept_section != NULL
10516 			      && sec->kept_section->output_section != NULL)
10517 			    {
10518 			      osec = sec->kept_section->output_section;
10519 			      irela->r_addend -= osec->vma;
10520 			    }
10521 
10522 			  if (!bfd_is_abs_section (osec))
10523 			    {
10524 			      r_symndx = osec->target_index;
10525 			      if (r_symndx == STN_UNDEF)
10526 				{
10527 				  irela->r_addend += osec->vma;
10528 				  osec = _bfd_nearby_section (output_bfd, osec,
10529 							      osec->vma);
10530 				  irela->r_addend -= osec->vma;
10531 				  r_symndx = osec->target_index;
10532 				}
10533 			    }
10534 			}
10535 
10536 		      /* Adjust the addend according to where the
10537 			 section winds up in the output section.  */
10538 		      if (rela_normal)
10539 			irela->r_addend += sec->output_offset;
10540 		    }
10541 		  else
10542 		    {
10543 		      if (flinfo->indices[r_symndx] == -1)
10544 			{
10545 			  unsigned long shlink;
10546 			  const char *name;
10547 			  asection *osec;
10548 			  long indx;
10549 
10550 			  if (flinfo->info->strip == strip_all)
10551 			    {
10552 			      /* You can't do ld -r -s.  */
10553 			      bfd_set_error (bfd_error_invalid_operation);
10554 			      return FALSE;
10555 			    }
10556 
10557 			  /* This symbol was skipped earlier, but
10558 			     since it is needed by a reloc, we
10559 			     must output it now.  */
10560 			  shlink = symtab_hdr->sh_link;
10561 			  name = (bfd_elf_string_from_elf_section
10562 				  (input_bfd, shlink, sym.st_name));
10563 			  if (name == NULL)
10564 			    return FALSE;
10565 
10566 			  osec = sec->output_section;
10567 			  sym.st_shndx =
10568 			    _bfd_elf_section_from_bfd_section (output_bfd,
10569 							       osec);
10570 			  if (sym.st_shndx == SHN_BAD)
10571 			    return FALSE;
10572 
10573 			  sym.st_value += sec->output_offset;
10574 			  if (!bfd_link_relocatable (flinfo->info))
10575 			    {
10576 			      sym.st_value += osec->vma;
10577 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10578 				{
10579 				  /* STT_TLS symbols are relative to PT_TLS
10580 				     segment base.  */
10581 				  BFD_ASSERT (elf_hash_table (flinfo->info)
10582 					      ->tls_sec != NULL);
10583 				  sym.st_value -= (elf_hash_table (flinfo->info)
10584 						   ->tls_sec->vma);
10585 				}
10586 			    }
10587 
10588 			  indx = bfd_get_symcount (output_bfd);
10589 			  ret = elf_link_output_symstrtab (flinfo, name,
10590 							   &sym, sec,
10591 							   NULL);
10592 			  if (ret == 0)
10593 			    return FALSE;
10594 			  else if (ret == 1)
10595 			    flinfo->indices[r_symndx] = indx;
10596 			  else
10597 			    abort ();
10598 			}
10599 
10600 		      r_symndx = flinfo->indices[r_symndx];
10601 		    }
10602 
10603 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10604 				   | (irela->r_info & r_type_mask));
10605 		}
10606 
10607 	      /* Swap out the relocs.  */
10608 	      input_rel_hdr = esdi->rel.hdr;
10609 	      if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10610 		{
10611 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
10612 						     input_rel_hdr,
10613 						     internal_relocs,
10614 						     rel_hash_list))
10615 		    return FALSE;
10616 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10617 				      * bed->s->int_rels_per_ext_rel);
10618 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10619 		}
10620 
10621 	      input_rela_hdr = esdi->rela.hdr;
10622 	      if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10623 		{
10624 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
10625 						     input_rela_hdr,
10626 						     internal_relocs,
10627 						     rela_hash_list))
10628 		    return FALSE;
10629 		}
10630 	    }
10631 	}
10632 
10633       /* Write out the modified section contents.  */
10634       if (bed->elf_backend_write_section
10635 	  && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10636 						contents))
10637 	{
10638 	  /* Section written out.  */
10639 	}
10640       else switch (o->sec_info_type)
10641 	{
10642 	case SEC_INFO_TYPE_STABS:
10643 	  if (! (_bfd_write_section_stabs
10644 		 (output_bfd,
10645 		  &elf_hash_table (flinfo->info)->stab_info,
10646 		  o, &elf_section_data (o)->sec_info, contents)))
10647 	    return FALSE;
10648 	  break;
10649 	case SEC_INFO_TYPE_MERGE:
10650 	  if (! _bfd_write_merged_section (output_bfd, o,
10651 					   elf_section_data (o)->sec_info))
10652 	    return FALSE;
10653 	  break;
10654 	case SEC_INFO_TYPE_EH_FRAME:
10655 	  {
10656 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10657 						   o, contents))
10658 	      return FALSE;
10659 	  }
10660 	  break;
10661 	case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10662 	  {
10663 	    if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10664 							 flinfo->info,
10665 							 o, contents))
10666 	      return FALSE;
10667 	  }
10668 	  break;
10669 	default:
10670 	  {
10671 	    if (! (o->flags & SEC_EXCLUDE))
10672 	      {
10673 		file_ptr offset = (file_ptr) o->output_offset;
10674 		bfd_size_type todo = o->size;
10675 
10676 		offset *= bfd_octets_per_byte (output_bfd);
10677 
10678 		if ((o->flags & SEC_ELF_REVERSE_COPY))
10679 		  {
10680 		    /* Reverse-copy input section to output.  */
10681 		    do
10682 		      {
10683 			todo -= address_size;
10684 			if (! bfd_set_section_contents (output_bfd,
10685 							o->output_section,
10686 							contents + todo,
10687 							offset,
10688 							address_size))
10689 			  return FALSE;
10690 			if (todo == 0)
10691 			  break;
10692 			offset += address_size;
10693 		      }
10694 		    while (1);
10695 		  }
10696 		else if (! bfd_set_section_contents (output_bfd,
10697 						     o->output_section,
10698 						     contents,
10699 						     offset, todo))
10700 		  return FALSE;
10701 	      }
10702 	  }
10703 	  break;
10704 	}
10705     }
10706 
10707   return TRUE;
10708 }
10709 
10710 /* Generate a reloc when linking an ELF file.  This is a reloc
10711    requested by the linker, and does not come from any input file.  This
10712    is used to build constructor and destructor tables when linking
10713    with -Ur.  */
10714 
10715 static bfd_boolean
elf_reloc_link_order(bfd * output_bfd,struct bfd_link_info * info,asection * output_section,struct bfd_link_order * link_order)10716 elf_reloc_link_order (bfd *output_bfd,
10717 		      struct bfd_link_info *info,
10718 		      asection *output_section,
10719 		      struct bfd_link_order *link_order)
10720 {
10721   reloc_howto_type *howto;
10722   long indx;
10723   bfd_vma offset;
10724   bfd_vma addend;
10725   struct bfd_elf_section_reloc_data *reldata;
10726   struct elf_link_hash_entry **rel_hash_ptr;
10727   Elf_Internal_Shdr *rel_hdr;
10728   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10729   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10730   bfd_byte *erel;
10731   unsigned int i;
10732   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10733 
10734   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10735   if (howto == NULL)
10736     {
10737       bfd_set_error (bfd_error_bad_value);
10738       return FALSE;
10739     }
10740 
10741   addend = link_order->u.reloc.p->addend;
10742 
10743   if (esdo->rel.hdr)
10744     reldata = &esdo->rel;
10745   else if (esdo->rela.hdr)
10746     reldata = &esdo->rela;
10747   else
10748     {
10749       reldata = NULL;
10750       BFD_ASSERT (0);
10751     }
10752 
10753   /* Figure out the symbol index.  */
10754   rel_hash_ptr = reldata->hashes + reldata->count;
10755   if (link_order->type == bfd_section_reloc_link_order)
10756     {
10757       indx = link_order->u.reloc.p->u.section->target_index;
10758       BFD_ASSERT (indx != 0);
10759       *rel_hash_ptr = NULL;
10760     }
10761   else
10762     {
10763       struct elf_link_hash_entry *h;
10764 
10765       /* Treat a reloc against a defined symbol as though it were
10766 	 actually against the section.  */
10767       h = ((struct elf_link_hash_entry *)
10768 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
10769 					 link_order->u.reloc.p->u.name,
10770 					 FALSE, FALSE, TRUE));
10771       if (h != NULL
10772 	  && (h->root.type == bfd_link_hash_defined
10773 	      || h->root.type == bfd_link_hash_defweak))
10774 	{
10775 	  asection *section;
10776 
10777 	  section = h->root.u.def.section;
10778 	  indx = section->output_section->target_index;
10779 	  *rel_hash_ptr = NULL;
10780 	  /* It seems that we ought to add the symbol value to the
10781 	     addend here, but in practice it has already been added
10782 	     because it was passed to constructor_callback.  */
10783 	  addend += section->output_section->vma + section->output_offset;
10784 	}
10785       else if (h != NULL)
10786 	{
10787 	  /* Setting the index to -2 tells elf_link_output_extsym that
10788 	     this symbol is used by a reloc.  */
10789 	  h->indx = -2;
10790 	  *rel_hash_ptr = h;
10791 	  indx = 0;
10792 	}
10793       else
10794 	{
10795 	  (*info->callbacks->unattached_reloc)
10796 	    (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
10797 	  indx = 0;
10798 	}
10799     }
10800 
10801   /* If this is an inplace reloc, we must write the addend into the
10802      object file.  */
10803   if (howto->partial_inplace && addend != 0)
10804     {
10805       bfd_size_type size;
10806       bfd_reloc_status_type rstat;
10807       bfd_byte *buf;
10808       bfd_boolean ok;
10809       const char *sym_name;
10810 
10811       size = (bfd_size_type) bfd_get_reloc_size (howto);
10812       buf = (bfd_byte *) bfd_zmalloc (size);
10813       if (buf == NULL && size != 0)
10814 	return FALSE;
10815       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10816       switch (rstat)
10817 	{
10818 	case bfd_reloc_ok:
10819 	  break;
10820 
10821 	default:
10822 	case bfd_reloc_outofrange:
10823 	  abort ();
10824 
10825 	case bfd_reloc_overflow:
10826 	  if (link_order->type == bfd_section_reloc_link_order)
10827 	    sym_name = bfd_section_name (output_bfd,
10828 					 link_order->u.reloc.p->u.section);
10829 	  else
10830 	    sym_name = link_order->u.reloc.p->u.name;
10831 	  (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10832 					      howto->name, addend, NULL, NULL,
10833 					      (bfd_vma) 0);
10834 	  break;
10835 	}
10836 
10837       ok = bfd_set_section_contents (output_bfd, output_section, buf,
10838 				     link_order->offset
10839 				     * bfd_octets_per_byte (output_bfd),
10840 				     size);
10841       free (buf);
10842       if (! ok)
10843 	return FALSE;
10844     }
10845 
10846   /* The address of a reloc is relative to the section in a
10847      relocatable file, and is a virtual address in an executable
10848      file.  */
10849   offset = link_order->offset;
10850   if (! bfd_link_relocatable (info))
10851     offset += output_section->vma;
10852 
10853   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10854     {
10855       irel[i].r_offset = offset;
10856       irel[i].r_info = 0;
10857       irel[i].r_addend = 0;
10858     }
10859   if (bed->s->arch_size == 32)
10860     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10861   else
10862     irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10863 
10864   rel_hdr = reldata->hdr;
10865   erel = rel_hdr->contents;
10866   if (rel_hdr->sh_type == SHT_REL)
10867     {
10868       erel += reldata->count * bed->s->sizeof_rel;
10869       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10870     }
10871   else
10872     {
10873       irel[0].r_addend = addend;
10874       erel += reldata->count * bed->s->sizeof_rela;
10875       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10876     }
10877 
10878   ++reldata->count;
10879 
10880   return TRUE;
10881 }
10882 
10883 
10884 /* Get the output vma of the section pointed to by the sh_link field.  */
10885 
10886 static bfd_vma
elf_get_linked_section_vma(struct bfd_link_order * p)10887 elf_get_linked_section_vma (struct bfd_link_order *p)
10888 {
10889   Elf_Internal_Shdr **elf_shdrp;
10890   asection *s;
10891   int elfsec;
10892 
10893   s = p->u.indirect.section;
10894   elf_shdrp = elf_elfsections (s->owner);
10895   elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10896   elfsec = elf_shdrp[elfsec]->sh_link;
10897   /* PR 290:
10898      The Intel C compiler generates SHT_IA_64_UNWIND with
10899      SHF_LINK_ORDER.  But it doesn't set the sh_link or
10900      sh_info fields.  Hence we could get the situation
10901      where elfsec is 0.  */
10902   if (elfsec == 0)
10903     {
10904       const struct elf_backend_data *bed
10905 	= get_elf_backend_data (s->owner);
10906       if (bed->link_order_error_handler)
10907 	bed->link_order_error_handler
10908 	  (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10909       return 0;
10910     }
10911   else
10912     {
10913       s = elf_shdrp[elfsec]->bfd_section;
10914       return s->output_section->vma + s->output_offset;
10915     }
10916 }
10917 
10918 
10919 /* Compare two sections based on the locations of the sections they are
10920    linked to.  Used by elf_fixup_link_order.  */
10921 
10922 static int
compare_link_order(const void * a,const void * b)10923 compare_link_order (const void * a, const void * b)
10924 {
10925   bfd_vma apos;
10926   bfd_vma bpos;
10927 
10928   apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10929   bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10930   if (apos < bpos)
10931     return -1;
10932   return apos > bpos;
10933 }
10934 
10935 
10936 /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
10937    order as their linked sections.  Returns false if this could not be done
10938    because an output section includes both ordered and unordered
10939    sections.  Ideally we'd do this in the linker proper.  */
10940 
10941 static bfd_boolean
elf_fixup_link_order(bfd * abfd,asection * o)10942 elf_fixup_link_order (bfd *abfd, asection *o)
10943 {
10944   int seen_linkorder;
10945   int seen_other;
10946   int n;
10947   struct bfd_link_order *p;
10948   bfd *sub;
10949   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10950   unsigned elfsec;
10951   struct bfd_link_order **sections;
10952   asection *s, *other_sec, *linkorder_sec;
10953   bfd_vma offset;
10954 
10955   other_sec = NULL;
10956   linkorder_sec = NULL;
10957   seen_other = 0;
10958   seen_linkorder = 0;
10959   for (p = o->map_head.link_order; p != NULL; p = p->next)
10960     {
10961       if (p->type == bfd_indirect_link_order)
10962 	{
10963 	  s = p->u.indirect.section;
10964 	  sub = s->owner;
10965 	  if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10966 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10967 	      && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10968 	      && elfsec < elf_numsections (sub)
10969 	      && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10970 	      && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10971 	    {
10972 	      seen_linkorder++;
10973 	      linkorder_sec = s;
10974 	    }
10975 	  else
10976 	    {
10977 	      seen_other++;
10978 	      other_sec = s;
10979 	    }
10980 	}
10981       else
10982 	seen_other++;
10983 
10984       if (seen_other && seen_linkorder)
10985 	{
10986 	  if (other_sec && linkorder_sec)
10987 	    (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10988 				   o, linkorder_sec,
10989 				   linkorder_sec->owner, other_sec,
10990 				   other_sec->owner);
10991 	  else
10992 	    (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10993 				   o);
10994 	  bfd_set_error (bfd_error_bad_value);
10995 	  return FALSE;
10996 	}
10997     }
10998 
10999   if (!seen_linkorder)
11000     return TRUE;
11001 
11002   sections = (struct bfd_link_order **)
11003     bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11004   if (sections == NULL)
11005     return FALSE;
11006   seen_linkorder = 0;
11007 
11008   for (p = o->map_head.link_order; p != NULL; p = p->next)
11009     {
11010       sections[seen_linkorder++] = p;
11011     }
11012   /* Sort the input sections in the order of their linked section.  */
11013   qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11014 	 compare_link_order);
11015 
11016   /* Change the offsets of the sections.  */
11017   offset = 0;
11018   for (n = 0; n < seen_linkorder; n++)
11019     {
11020       s = sections[n]->u.indirect.section;
11021       offset &= ~(bfd_vma) 0 << s->alignment_power;
11022       s->output_offset = offset / bfd_octets_per_byte (abfd);
11023       sections[n]->offset = offset;
11024       offset += sections[n]->size;
11025     }
11026 
11027   free (sections);
11028   return TRUE;
11029 }
11030 
11031 static void
elf_final_link_free(bfd * obfd,struct elf_final_link_info * flinfo)11032 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11033 {
11034   asection *o;
11035 
11036   if (flinfo->symstrtab != NULL)
11037     _bfd_elf_strtab_free (flinfo->symstrtab);
11038   if (flinfo->contents != NULL)
11039     free (flinfo->contents);
11040   if (flinfo->external_relocs != NULL)
11041     free (flinfo->external_relocs);
11042   if (flinfo->internal_relocs != NULL)
11043     free (flinfo->internal_relocs);
11044   if (flinfo->external_syms != NULL)
11045     free (flinfo->external_syms);
11046   if (flinfo->locsym_shndx != NULL)
11047     free (flinfo->locsym_shndx);
11048   if (flinfo->internal_syms != NULL)
11049     free (flinfo->internal_syms);
11050   if (flinfo->indices != NULL)
11051     free (flinfo->indices);
11052   if (flinfo->sections != NULL)
11053     free (flinfo->sections);
11054   if (flinfo->symshndxbuf != NULL)
11055     free (flinfo->symshndxbuf);
11056   for (o = obfd->sections; o != NULL; o = o->next)
11057     {
11058       struct bfd_elf_section_data *esdo = elf_section_data (o);
11059       if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11060 	free (esdo->rel.hashes);
11061       if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11062 	free (esdo->rela.hashes);
11063     }
11064 }
11065 
11066 /* Do the final step of an ELF link.  */
11067 
11068 bfd_boolean
bfd_elf_final_link(bfd * abfd,struct bfd_link_info * info)11069 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11070 {
11071   bfd_boolean dynamic;
11072   bfd_boolean emit_relocs;
11073   bfd *dynobj;
11074   struct elf_final_link_info flinfo;
11075   asection *o;
11076   struct bfd_link_order *p;
11077   bfd *sub;
11078   bfd_size_type max_contents_size;
11079   bfd_size_type max_external_reloc_size;
11080   bfd_size_type max_internal_reloc_count;
11081   bfd_size_type max_sym_count;
11082   bfd_size_type max_sym_shndx_count;
11083   Elf_Internal_Sym elfsym;
11084   unsigned int i;
11085   Elf_Internal_Shdr *symtab_hdr;
11086   Elf_Internal_Shdr *symtab_shndx_hdr;
11087   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11088   struct elf_outext_info eoinfo;
11089   bfd_boolean merged;
11090   size_t relativecount = 0;
11091   asection *reldyn = 0;
11092   bfd_size_type amt;
11093   asection *attr_section = NULL;
11094   bfd_vma attr_size = 0;
11095   const char *std_attrs_section;
11096 
11097   if (! is_elf_hash_table (info->hash))
11098     return FALSE;
11099 
11100   if (bfd_link_pic (info))
11101     abfd->flags |= DYNAMIC;
11102 
11103   dynamic = elf_hash_table (info)->dynamic_sections_created;
11104   dynobj = elf_hash_table (info)->dynobj;
11105 
11106   emit_relocs = (bfd_link_relocatable (info)
11107 		 || info->emitrelocations);
11108 
11109   flinfo.info = info;
11110   flinfo.output_bfd = abfd;
11111   flinfo.symstrtab = _bfd_elf_strtab_init ();
11112   if (flinfo.symstrtab == NULL)
11113     return FALSE;
11114 
11115   if (! dynamic)
11116     {
11117       flinfo.hash_sec = NULL;
11118       flinfo.symver_sec = NULL;
11119     }
11120   else
11121     {
11122       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11123       /* Note that dynsym_sec can be NULL (on VMS).  */
11124       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11125       /* Note that it is OK if symver_sec is NULL.  */
11126     }
11127 
11128   flinfo.contents = NULL;
11129   flinfo.external_relocs = NULL;
11130   flinfo.internal_relocs = NULL;
11131   flinfo.external_syms = NULL;
11132   flinfo.locsym_shndx = NULL;
11133   flinfo.internal_syms = NULL;
11134   flinfo.indices = NULL;
11135   flinfo.sections = NULL;
11136   flinfo.symshndxbuf = NULL;
11137   flinfo.filesym_count = 0;
11138 
11139   /* The object attributes have been merged.  Remove the input
11140      sections from the link, and set the contents of the output
11141      secton.  */
11142   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11143   for (o = abfd->sections; o != NULL; o = o->next)
11144     {
11145       if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11146 	  || strcmp (o->name, ".gnu.attributes") == 0)
11147 	{
11148 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
11149 	    {
11150 	      asection *input_section;
11151 
11152 	      if (p->type != bfd_indirect_link_order)
11153 		continue;
11154 	      input_section = p->u.indirect.section;
11155 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
11156 		 elf_link_input_bfd ignores this section.  */
11157 	      input_section->flags &= ~SEC_HAS_CONTENTS;
11158 	    }
11159 
11160 	  attr_size = bfd_elf_obj_attr_size (abfd);
11161 	  if (attr_size)
11162 	    {
11163 	      bfd_set_section_size (abfd, o, attr_size);
11164 	      attr_section = o;
11165 	      /* Skip this section later on.  */
11166 	      o->map_head.link_order = NULL;
11167 	    }
11168 	  else
11169 	    o->flags |= SEC_EXCLUDE;
11170 	}
11171     }
11172 
11173   /* Count up the number of relocations we will output for each output
11174      section, so that we know the sizes of the reloc sections.  We
11175      also figure out some maximum sizes.  */
11176   max_contents_size = 0;
11177   max_external_reloc_size = 0;
11178   max_internal_reloc_count = 0;
11179   max_sym_count = 0;
11180   max_sym_shndx_count = 0;
11181   merged = FALSE;
11182   for (o = abfd->sections; o != NULL; o = o->next)
11183     {
11184       struct bfd_elf_section_data *esdo = elf_section_data (o);
11185       o->reloc_count = 0;
11186 
11187       for (p = o->map_head.link_order; p != NULL; p = p->next)
11188 	{
11189 	  unsigned int reloc_count = 0;
11190 	  unsigned int additional_reloc_count = 0;
11191 	  struct bfd_elf_section_data *esdi = NULL;
11192 
11193 	  if (p->type == bfd_section_reloc_link_order
11194 	      || p->type == bfd_symbol_reloc_link_order)
11195 	    reloc_count = 1;
11196 	  else if (p->type == bfd_indirect_link_order)
11197 	    {
11198 	      asection *sec;
11199 
11200 	      sec = p->u.indirect.section;
11201 	      esdi = elf_section_data (sec);
11202 
11203 	      /* Mark all sections which are to be included in the
11204 		 link.  This will normally be every section.  We need
11205 		 to do this so that we can identify any sections which
11206 		 the linker has decided to not include.  */
11207 	      sec->linker_mark = TRUE;
11208 
11209 	      if (sec->flags & SEC_MERGE)
11210 		merged = TRUE;
11211 
11212 	      if (esdo->this_hdr.sh_type == SHT_REL
11213 		  || esdo->this_hdr.sh_type == SHT_RELA)
11214 		/* Some backends use reloc_count in relocation sections
11215 		   to count particular types of relocs.  Of course,
11216 		   reloc sections themselves can't have relocations.  */
11217 		reloc_count = 0;
11218 	      else if (emit_relocs)
11219 		{
11220 		  reloc_count = sec->reloc_count;
11221 		  if (bed->elf_backend_count_additional_relocs)
11222 		    {
11223 		      int c;
11224 		      c = (*bed->elf_backend_count_additional_relocs) (sec);
11225 		      additional_reloc_count += c;
11226 		    }
11227 		}
11228 	      else if (bed->elf_backend_count_relocs)
11229 		reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11230 
11231 	      if (sec->rawsize > max_contents_size)
11232 		max_contents_size = sec->rawsize;
11233 	      if (sec->size > max_contents_size)
11234 		max_contents_size = sec->size;
11235 
11236 	      /* We are interested in just local symbols, not all
11237 		 symbols.  */
11238 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11239 		  && (sec->owner->flags & DYNAMIC) == 0)
11240 		{
11241 		  size_t sym_count;
11242 
11243 		  if (elf_bad_symtab (sec->owner))
11244 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11245 				 / bed->s->sizeof_sym);
11246 		  else
11247 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11248 
11249 		  if (sym_count > max_sym_count)
11250 		    max_sym_count = sym_count;
11251 
11252 		  if (sym_count > max_sym_shndx_count
11253 		      && elf_symtab_shndx_list (sec->owner) != NULL)
11254 		    max_sym_shndx_count = sym_count;
11255 
11256 		  if ((sec->flags & SEC_RELOC) != 0)
11257 		    {
11258 		      size_t ext_size = 0;
11259 
11260 		      if (esdi->rel.hdr != NULL)
11261 			ext_size = esdi->rel.hdr->sh_size;
11262 		      if (esdi->rela.hdr != NULL)
11263 			ext_size += esdi->rela.hdr->sh_size;
11264 
11265 		      if (ext_size > max_external_reloc_size)
11266 			max_external_reloc_size = ext_size;
11267 		      if (sec->reloc_count > max_internal_reloc_count)
11268 			max_internal_reloc_count = sec->reloc_count;
11269 		    }
11270 		}
11271 	    }
11272 
11273 	  if (reloc_count == 0)
11274 	    continue;
11275 
11276 	  reloc_count += additional_reloc_count;
11277 	  o->reloc_count += reloc_count;
11278 
11279 	  if (p->type == bfd_indirect_link_order && emit_relocs)
11280 	    {
11281 	      if (esdi->rel.hdr)
11282 		{
11283 		  esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11284 		  esdo->rel.count += additional_reloc_count;
11285 		}
11286 	      if (esdi->rela.hdr)
11287 		{
11288 		  esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11289 		  esdo->rela.count += additional_reloc_count;
11290 		}
11291 	    }
11292 	  else
11293 	    {
11294 	      if (o->use_rela_p)
11295 		esdo->rela.count += reloc_count;
11296 	      else
11297 		esdo->rel.count += reloc_count;
11298 	    }
11299 	}
11300 
11301       if (o->reloc_count > 0)
11302 	o->flags |= SEC_RELOC;
11303       else
11304 	{
11305 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
11306 	     set it (this is probably a bug) and if it is set
11307 	     assign_section_numbers will create a reloc section.  */
11308 	  o->flags &=~ SEC_RELOC;
11309 	}
11310 
11311       /* If the SEC_ALLOC flag is not set, force the section VMA to
11312 	 zero.  This is done in elf_fake_sections as well, but forcing
11313 	 the VMA to 0 here will ensure that relocs against these
11314 	 sections are handled correctly.  */
11315       if ((o->flags & SEC_ALLOC) == 0
11316 	  && ! o->user_set_vma)
11317 	o->vma = 0;
11318     }
11319 
11320   if (! bfd_link_relocatable (info) && merged)
11321     elf_link_hash_traverse (elf_hash_table (info),
11322 			    _bfd_elf_link_sec_merge_syms, abfd);
11323 
11324   /* Figure out the file positions for everything but the symbol table
11325      and the relocs.  We set symcount to force assign_section_numbers
11326      to create a symbol table.  */
11327   bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11328   BFD_ASSERT (! abfd->output_has_begun);
11329   if (! _bfd_elf_compute_section_file_positions (abfd, info))
11330     goto error_return;
11331 
11332   /* Set sizes, and assign file positions for reloc sections.  */
11333   for (o = abfd->sections; o != NULL; o = o->next)
11334     {
11335       struct bfd_elf_section_data *esdo = elf_section_data (o);
11336       if ((o->flags & SEC_RELOC) != 0)
11337 	{
11338 	  if (esdo->rel.hdr
11339 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11340 	    goto error_return;
11341 
11342 	  if (esdo->rela.hdr
11343 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11344 	    goto error_return;
11345 	}
11346 
11347       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11348 	 to count upwards while actually outputting the relocations.  */
11349       esdo->rel.count = 0;
11350       esdo->rela.count = 0;
11351 
11352       if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11353 	{
11354 	  /* Cache the section contents so that they can be compressed
11355 	     later.  Use bfd_malloc since it will be freed by
11356 	     bfd_compress_section_contents.  */
11357 	  unsigned char *contents = esdo->this_hdr.contents;
11358 	  if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11359 	    abort ();
11360 	  contents
11361 	    = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11362 	  if (contents == NULL)
11363 	    goto error_return;
11364 	  esdo->this_hdr.contents = contents;
11365 	}
11366     }
11367 
11368   /* We have now assigned file positions for all the sections except
11369      .symtab, .strtab, and non-loaded reloc sections.  We start the
11370      .symtab section at the current file position, and write directly
11371      to it.  We build the .strtab section in memory.  */
11372   bfd_get_symcount (abfd) = 0;
11373   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11374   /* sh_name is set in prep_headers.  */
11375   symtab_hdr->sh_type = SHT_SYMTAB;
11376   /* sh_flags, sh_addr and sh_size all start off zero.  */
11377   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11378   /* sh_link is set in assign_section_numbers.  */
11379   /* sh_info is set below.  */
11380   /* sh_offset is set just below.  */
11381   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11382 
11383   if (max_sym_count < 20)
11384     max_sym_count = 20;
11385   elf_hash_table (info)->strtabsize = max_sym_count;
11386   amt = max_sym_count * sizeof (struct elf_sym_strtab);
11387   elf_hash_table (info)->strtab
11388     = (struct elf_sym_strtab *) bfd_malloc (amt);
11389   if (elf_hash_table (info)->strtab == NULL)
11390     goto error_return;
11391   /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
11392   flinfo.symshndxbuf
11393     = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11394        ? (Elf_External_Sym_Shndx *) -1 : NULL);
11395 
11396   if (info->strip != strip_all || emit_relocs)
11397     {
11398       file_ptr off = elf_next_file_pos (abfd);
11399 
11400       _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11401 
11402       /* Note that at this point elf_next_file_pos (abfd) is
11403 	 incorrect.  We do not yet know the size of the .symtab section.
11404 	 We correct next_file_pos below, after we do know the size.  */
11405 
11406       /* Start writing out the symbol table.  The first symbol is always a
11407 	 dummy symbol.  */
11408       elfsym.st_value = 0;
11409       elfsym.st_size = 0;
11410       elfsym.st_info = 0;
11411       elfsym.st_other = 0;
11412       elfsym.st_shndx = SHN_UNDEF;
11413       elfsym.st_target_internal = 0;
11414       if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11415 				     bfd_und_section_ptr, NULL) != 1)
11416 	goto error_return;
11417 
11418       /* Output a symbol for each section.  We output these even if we are
11419 	 discarding local symbols, since they are used for relocs.  These
11420 	 symbols have no names.  We store the index of each one in the
11421 	 index field of the section, so that we can find it again when
11422 	 outputting relocs.  */
11423 
11424       elfsym.st_size = 0;
11425       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11426       elfsym.st_other = 0;
11427       elfsym.st_value = 0;
11428       elfsym.st_target_internal = 0;
11429       for (i = 1; i < elf_numsections (abfd); i++)
11430 	{
11431 	  o = bfd_section_from_elf_index (abfd, i);
11432 	  if (o != NULL)
11433 	    {
11434 	      o->target_index = bfd_get_symcount (abfd);
11435 	      elfsym.st_shndx = i;
11436 	      if (!bfd_link_relocatable (info))
11437 		elfsym.st_value = o->vma;
11438 	      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11439 					     NULL) != 1)
11440 		goto error_return;
11441 	    }
11442 	}
11443     }
11444 
11445   /* Allocate some memory to hold information read in from the input
11446      files.  */
11447   if (max_contents_size != 0)
11448     {
11449       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11450       if (flinfo.contents == NULL)
11451 	goto error_return;
11452     }
11453 
11454   if (max_external_reloc_size != 0)
11455     {
11456       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11457       if (flinfo.external_relocs == NULL)
11458 	goto error_return;
11459     }
11460 
11461   if (max_internal_reloc_count != 0)
11462     {
11463       amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11464       amt *= sizeof (Elf_Internal_Rela);
11465       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11466       if (flinfo.internal_relocs == NULL)
11467 	goto error_return;
11468     }
11469 
11470   if (max_sym_count != 0)
11471     {
11472       amt = max_sym_count * bed->s->sizeof_sym;
11473       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11474       if (flinfo.external_syms == NULL)
11475 	goto error_return;
11476 
11477       amt = max_sym_count * sizeof (Elf_Internal_Sym);
11478       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11479       if (flinfo.internal_syms == NULL)
11480 	goto error_return;
11481 
11482       amt = max_sym_count * sizeof (long);
11483       flinfo.indices = (long int *) bfd_malloc (amt);
11484       if (flinfo.indices == NULL)
11485 	goto error_return;
11486 
11487       amt = max_sym_count * sizeof (asection *);
11488       flinfo.sections = (asection **) bfd_malloc (amt);
11489       if (flinfo.sections == NULL)
11490 	goto error_return;
11491     }
11492 
11493   if (max_sym_shndx_count != 0)
11494     {
11495       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11496       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11497       if (flinfo.locsym_shndx == NULL)
11498 	goto error_return;
11499     }
11500 
11501   if (elf_hash_table (info)->tls_sec)
11502     {
11503       bfd_vma base, end = 0;
11504       asection *sec;
11505 
11506       for (sec = elf_hash_table (info)->tls_sec;
11507 	   sec && (sec->flags & SEC_THREAD_LOCAL);
11508 	   sec = sec->next)
11509 	{
11510 	  bfd_size_type size = sec->size;
11511 
11512 	  if (size == 0
11513 	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
11514 	    {
11515 	      struct bfd_link_order *ord = sec->map_tail.link_order;
11516 
11517 	      if (ord != NULL)
11518 		size = ord->offset + ord->size;
11519 	    }
11520 	  end = sec->vma + size;
11521 	}
11522       base = elf_hash_table (info)->tls_sec->vma;
11523       /* Only align end of TLS section if static TLS doesn't have special
11524 	 alignment requirements.  */
11525       if (bed->static_tls_alignment == 1)
11526 	end = align_power (end,
11527 			   elf_hash_table (info)->tls_sec->alignment_power);
11528       elf_hash_table (info)->tls_size = end - base;
11529     }
11530 
11531   /* Reorder SHF_LINK_ORDER sections.  */
11532   for (o = abfd->sections; o != NULL; o = o->next)
11533     {
11534       if (!elf_fixup_link_order (abfd, o))
11535 	return FALSE;
11536     }
11537 
11538   if (!_bfd_elf_fixup_eh_frame_hdr (info))
11539     return FALSE;
11540 
11541   /* Since ELF permits relocations to be against local symbols, we
11542      must have the local symbols available when we do the relocations.
11543      Since we would rather only read the local symbols once, and we
11544      would rather not keep them in memory, we handle all the
11545      relocations for a single input file at the same time.
11546 
11547      Unfortunately, there is no way to know the total number of local
11548      symbols until we have seen all of them, and the local symbol
11549      indices precede the global symbol indices.  This means that when
11550      we are generating relocatable output, and we see a reloc against
11551      a global symbol, we can not know the symbol index until we have
11552      finished examining all the local symbols to see which ones we are
11553      going to output.  To deal with this, we keep the relocations in
11554      memory, and don't output them until the end of the link.  This is
11555      an unfortunate waste of memory, but I don't see a good way around
11556      it.  Fortunately, it only happens when performing a relocatable
11557      link, which is not the common case.  FIXME: If keep_memory is set
11558      we could write the relocs out and then read them again; I don't
11559      know how bad the memory loss will be.  */
11560 
11561   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11562     sub->output_has_begun = FALSE;
11563   for (o = abfd->sections; o != NULL; o = o->next)
11564     {
11565       for (p = o->map_head.link_order; p != NULL; p = p->next)
11566 	{
11567 	  if (p->type == bfd_indirect_link_order
11568 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11569 		  == bfd_target_elf_flavour)
11570 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11571 	    {
11572 	      if (! sub->output_has_begun)
11573 		{
11574 		  if (! elf_link_input_bfd (&flinfo, sub))
11575 		    goto error_return;
11576 		  sub->output_has_begun = TRUE;
11577 		}
11578 	    }
11579 	  else if (p->type == bfd_section_reloc_link_order
11580 		   || p->type == bfd_symbol_reloc_link_order)
11581 	    {
11582 	      if (! elf_reloc_link_order (abfd, info, o, p))
11583 		goto error_return;
11584 	    }
11585 	  else
11586 	    {
11587 	      if (! _bfd_default_link_order (abfd, info, o, p))
11588 		{
11589 		  if (p->type == bfd_indirect_link_order
11590 		      && (bfd_get_flavour (sub)
11591 			  == bfd_target_elf_flavour)
11592 		      && (elf_elfheader (sub)->e_ident[EI_CLASS]
11593 			  != bed->s->elfclass))
11594 		    {
11595 		      const char *iclass, *oclass;
11596 
11597 		      switch (bed->s->elfclass)
11598 			{
11599 			case ELFCLASS64: oclass = "ELFCLASS64"; break;
11600 			case ELFCLASS32: oclass = "ELFCLASS32"; break;
11601 			case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11602 			default: abort ();
11603 			}
11604 
11605 		      switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11606 			{
11607 			case ELFCLASS64: iclass = "ELFCLASS64"; break;
11608 			case ELFCLASS32: iclass = "ELFCLASS32"; break;
11609 			case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11610 			default: abort ();
11611 			}
11612 
11613 		      bfd_set_error (bfd_error_wrong_format);
11614 		      (*_bfd_error_handler)
11615 			(_("%B: file class %s incompatible with %s"),
11616 			 sub, iclass, oclass);
11617 		    }
11618 
11619 		  goto error_return;
11620 		}
11621 	    }
11622 	}
11623     }
11624 
11625   /* Free symbol buffer if needed.  */
11626   if (!info->reduce_memory_overheads)
11627     {
11628       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11629 	if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11630 	    && elf_tdata (sub)->symbuf)
11631 	  {
11632 	    free (elf_tdata (sub)->symbuf);
11633 	    elf_tdata (sub)->symbuf = NULL;
11634 	  }
11635     }
11636 
11637   /* Output any global symbols that got converted to local in a
11638      version script or due to symbol visibility.  We do this in a
11639      separate step since ELF requires all local symbols to appear
11640      prior to any global symbols.  FIXME: We should only do this if
11641      some global symbols were, in fact, converted to become local.
11642      FIXME: Will this work correctly with the Irix 5 linker?  */
11643   eoinfo.failed = FALSE;
11644   eoinfo.flinfo = &flinfo;
11645   eoinfo.localsyms = TRUE;
11646   eoinfo.file_sym_done = FALSE;
11647   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11648   if (eoinfo.failed)
11649     return FALSE;
11650 
11651   /* If backend needs to output some local symbols not present in the hash
11652      table, do it now.  */
11653   if (bed->elf_backend_output_arch_local_syms
11654       && (info->strip != strip_all || emit_relocs))
11655     {
11656       typedef int (*out_sym_func)
11657 	(void *, const char *, Elf_Internal_Sym *, asection *,
11658 	 struct elf_link_hash_entry *);
11659 
11660       if (! ((*bed->elf_backend_output_arch_local_syms)
11661 	     (abfd, info, &flinfo,
11662 	      (out_sym_func) elf_link_output_symstrtab)))
11663 	return FALSE;
11664     }
11665 
11666   /* That wrote out all the local symbols.  Finish up the symbol table
11667      with the global symbols. Even if we want to strip everything we
11668      can, we still need to deal with those global symbols that got
11669      converted to local in a version script.  */
11670 
11671   /* The sh_info field records the index of the first non local symbol.  */
11672   symtab_hdr->sh_info = bfd_get_symcount (abfd);
11673 
11674   if (dynamic
11675       && elf_hash_table (info)->dynsym != NULL
11676       && (elf_hash_table (info)->dynsym->output_section
11677 	  != bfd_abs_section_ptr))
11678     {
11679       Elf_Internal_Sym sym;
11680       bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11681       long last_local = 0;
11682 
11683       /* Write out the section symbols for the output sections.  */
11684       if (bfd_link_pic (info)
11685 	  || elf_hash_table (info)->is_relocatable_executable)
11686 	{
11687 	  asection *s;
11688 
11689 	  sym.st_size = 0;
11690 	  sym.st_name = 0;
11691 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11692 	  sym.st_other = 0;
11693 	  sym.st_target_internal = 0;
11694 
11695 	  for (s = abfd->sections; s != NULL; s = s->next)
11696 	    {
11697 	      int indx;
11698 	      bfd_byte *dest;
11699 	      long dynindx;
11700 
11701 	      dynindx = elf_section_data (s)->dynindx;
11702 	      if (dynindx <= 0)
11703 		continue;
11704 	      indx = elf_section_data (s)->this_idx;
11705 	      BFD_ASSERT (indx > 0);
11706 	      sym.st_shndx = indx;
11707 	      if (! check_dynsym (abfd, &sym))
11708 		return FALSE;
11709 	      sym.st_value = s->vma;
11710 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
11711 	      if (last_local < dynindx)
11712 		last_local = dynindx;
11713 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11714 	    }
11715 	}
11716 
11717       /* Write out the local dynsyms.  */
11718       if (elf_hash_table (info)->dynlocal)
11719 	{
11720 	  struct elf_link_local_dynamic_entry *e;
11721 	  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11722 	    {
11723 	      asection *s;
11724 	      bfd_byte *dest;
11725 
11726 	      /* Copy the internal symbol and turn off visibility.
11727 		 Note that we saved a word of storage and overwrote
11728 		 the original st_name with the dynstr_index.  */
11729 	      sym = e->isym;
11730 	      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11731 
11732 	      s = bfd_section_from_elf_index (e->input_bfd,
11733 					      e->isym.st_shndx);
11734 	      if (s != NULL)
11735 		{
11736 		  sym.st_shndx =
11737 		    elf_section_data (s->output_section)->this_idx;
11738 		  if (! check_dynsym (abfd, &sym))
11739 		    return FALSE;
11740 		  sym.st_value = (s->output_section->vma
11741 				  + s->output_offset
11742 				  + e->isym.st_value);
11743 		}
11744 
11745 	      if (last_local < e->dynindx)
11746 		last_local = e->dynindx;
11747 
11748 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11749 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11750 	    }
11751 	}
11752 
11753       elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11754 	last_local + 1;
11755     }
11756 
11757   /* We get the global symbols from the hash table.  */
11758   eoinfo.failed = FALSE;
11759   eoinfo.localsyms = FALSE;
11760   eoinfo.flinfo = &flinfo;
11761   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11762   if (eoinfo.failed)
11763     return FALSE;
11764 
11765   /* If backend needs to output some symbols not present in the hash
11766      table, do it now.  */
11767   if (bed->elf_backend_output_arch_syms
11768       && (info->strip != strip_all || emit_relocs))
11769     {
11770       typedef int (*out_sym_func)
11771 	(void *, const char *, Elf_Internal_Sym *, asection *,
11772 	 struct elf_link_hash_entry *);
11773 
11774       if (! ((*bed->elf_backend_output_arch_syms)
11775 	     (abfd, info, &flinfo,
11776 	      (out_sym_func) elf_link_output_symstrtab)))
11777 	return FALSE;
11778     }
11779 
11780   /* Finalize the .strtab section.  */
11781   _bfd_elf_strtab_finalize (flinfo.symstrtab);
11782 
11783   /* Swap out the .strtab section. */
11784   if (!elf_link_swap_symbols_out (&flinfo))
11785     return FALSE;
11786 
11787   /* Now we know the size of the symtab section.  */
11788   if (bfd_get_symcount (abfd) > 0)
11789     {
11790       /* Finish up and write out the symbol string table (.strtab)
11791 	 section.  */
11792       Elf_Internal_Shdr *symstrtab_hdr;
11793       file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11794 
11795       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11796       if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11797 	{
11798 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11799 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11800 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11801 	  amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11802 	  symtab_shndx_hdr->sh_size = amt;
11803 
11804 	  off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11805 							   off, TRUE);
11806 
11807 	  if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11808 	      || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11809 	    return FALSE;
11810 	}
11811 
11812       symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11813       /* sh_name was set in prep_headers.  */
11814       symstrtab_hdr->sh_type = SHT_STRTAB;
11815       symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
11816       symstrtab_hdr->sh_addr = 0;
11817       symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11818       symstrtab_hdr->sh_entsize = 0;
11819       symstrtab_hdr->sh_link = 0;
11820       symstrtab_hdr->sh_info = 0;
11821       /* sh_offset is set just below.  */
11822       symstrtab_hdr->sh_addralign = 1;
11823 
11824       off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11825 						       off, TRUE);
11826       elf_next_file_pos (abfd) = off;
11827 
11828       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11829 	  || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11830 	return FALSE;
11831     }
11832 
11833   /* Adjust the relocs to have the correct symbol indices.  */
11834   for (o = abfd->sections; o != NULL; o = o->next)
11835     {
11836       struct bfd_elf_section_data *esdo = elf_section_data (o);
11837       bfd_boolean sort;
11838       if ((o->flags & SEC_RELOC) == 0)
11839 	continue;
11840 
11841       sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11842       if (esdo->rel.hdr != NULL
11843 	  && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11844 	return FALSE;
11845       if (esdo->rela.hdr != NULL
11846 	  && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11847 	return FALSE;
11848 
11849       /* Set the reloc_count field to 0 to prevent write_relocs from
11850 	 trying to swap the relocs out itself.  */
11851       o->reloc_count = 0;
11852     }
11853 
11854   if (dynamic && info->combreloc && dynobj != NULL)
11855     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11856 
11857   /* If we are linking against a dynamic object, or generating a
11858      shared library, finish up the dynamic linking information.  */
11859   if (dynamic)
11860     {
11861       bfd_byte *dyncon, *dynconend;
11862 
11863       /* Fix up .dynamic entries.  */
11864       o = bfd_get_linker_section (dynobj, ".dynamic");
11865       BFD_ASSERT (o != NULL);
11866 
11867       dyncon = o->contents;
11868       dynconend = o->contents + o->size;
11869       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11870 	{
11871 	  Elf_Internal_Dyn dyn;
11872 	  const char *name;
11873 	  unsigned int type;
11874 
11875 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11876 
11877 	  switch (dyn.d_tag)
11878 	    {
11879 	    default:
11880 	      continue;
11881 	    case DT_NULL:
11882 	      if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11883 		{
11884 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
11885 		    {
11886 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11887 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11888 		    default: continue;
11889 		    }
11890 		  dyn.d_un.d_val = relativecount;
11891 		  relativecount = 0;
11892 		  break;
11893 		}
11894 	      continue;
11895 
11896 	    case DT_INIT:
11897 	      name = info->init_function;
11898 	      goto get_sym;
11899 	    case DT_FINI:
11900 	      name = info->fini_function;
11901 	    get_sym:
11902 	      {
11903 		struct elf_link_hash_entry *h;
11904 
11905 		h = elf_link_hash_lookup (elf_hash_table (info), name,
11906 					  FALSE, FALSE, TRUE);
11907 		if (h != NULL
11908 		    && (h->root.type == bfd_link_hash_defined
11909 			|| h->root.type == bfd_link_hash_defweak))
11910 		  {
11911 		    dyn.d_un.d_ptr = h->root.u.def.value;
11912 		    o = h->root.u.def.section;
11913 		    if (o->output_section != NULL)
11914 		      dyn.d_un.d_ptr += (o->output_section->vma
11915 					 + o->output_offset);
11916 		    else
11917 		      {
11918 			/* The symbol is imported from another shared
11919 			   library and does not apply to this one.  */
11920 			dyn.d_un.d_ptr = 0;
11921 		      }
11922 		    break;
11923 		  }
11924 	      }
11925 	      continue;
11926 
11927 	    case DT_PREINIT_ARRAYSZ:
11928 	      name = ".preinit_array";
11929 	      goto get_out_size;
11930 	    case DT_INIT_ARRAYSZ:
11931 	      name = ".init_array";
11932 	      goto get_out_size;
11933 	    case DT_FINI_ARRAYSZ:
11934 	      name = ".fini_array";
11935 	    get_out_size:
11936 	      o = bfd_get_section_by_name (abfd, name);
11937 	      if (o == NULL)
11938 		{
11939 		  (*_bfd_error_handler)
11940 		    (_("could not find section %s"), name);
11941 		  goto error_return;
11942 		}
11943 	      if (o->size == 0)
11944 		(*_bfd_error_handler)
11945 		  (_("warning: %s section has zero size"), name);
11946 	      dyn.d_un.d_val = o->size;
11947 	      break;
11948 
11949 	    case DT_PREINIT_ARRAY:
11950 	      name = ".preinit_array";
11951 	      goto get_out_vma;
11952 	    case DT_INIT_ARRAY:
11953 	      name = ".init_array";
11954 	      goto get_out_vma;
11955 	    case DT_FINI_ARRAY:
11956 	      name = ".fini_array";
11957 	    get_out_vma:
11958 	      o = bfd_get_section_by_name (abfd, name);
11959 	      goto do_vma;
11960 
11961 	    case DT_HASH:
11962 	      name = ".hash";
11963 	      goto get_vma;
11964 	    case DT_GNU_HASH:
11965 	      name = ".gnu.hash";
11966 	      goto get_vma;
11967 	    case DT_STRTAB:
11968 	      name = ".dynstr";
11969 	      goto get_vma;
11970 	    case DT_SYMTAB:
11971 	      name = ".dynsym";
11972 	      goto get_vma;
11973 	    case DT_VERDEF:
11974 	      name = ".gnu.version_d";
11975 	      goto get_vma;
11976 	    case DT_VERNEED:
11977 	      name = ".gnu.version_r";
11978 	      goto get_vma;
11979 	    case DT_VERSYM:
11980 	      name = ".gnu.version";
11981 	    get_vma:
11982 	      o = bfd_get_linker_section (dynobj, name);
11983 	    do_vma:
11984 	      if (o == NULL)
11985 		{
11986 		  (*_bfd_error_handler)
11987 		    (_("could not find section %s"), name);
11988 		  goto error_return;
11989 		}
11990 	      if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11991 		{
11992 		  (*_bfd_error_handler)
11993 		    (_("warning: section '%s' is being made into a note"), name);
11994 		  bfd_set_error (bfd_error_nonrepresentable_section);
11995 		  goto error_return;
11996 		}
11997 	      dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
11998 	      break;
11999 
12000 	    case DT_REL:
12001 	    case DT_RELA:
12002 	    case DT_RELSZ:
12003 	    case DT_RELASZ:
12004 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12005 		type = SHT_REL;
12006 	      else
12007 		type = SHT_RELA;
12008 	      dyn.d_un.d_val = 0;
12009 	      dyn.d_un.d_ptr = 0;
12010 	      for (i = 1; i < elf_numsections (abfd); i++)
12011 		{
12012 		  Elf_Internal_Shdr *hdr;
12013 
12014 		  hdr = elf_elfsections (abfd)[i];
12015 		  if (hdr->sh_type == type
12016 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
12017 		    {
12018 		      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12019 			dyn.d_un.d_val += hdr->sh_size;
12020 		      else
12021 			{
12022 			  if (dyn.d_un.d_ptr == 0
12023 			      || hdr->sh_addr < dyn.d_un.d_ptr)
12024 			    dyn.d_un.d_ptr = hdr->sh_addr;
12025 			}
12026 		    }
12027 		}
12028 	      break;
12029 	    }
12030 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12031 	}
12032     }
12033 
12034   /* If we have created any dynamic sections, then output them.  */
12035   if (dynobj != NULL)
12036     {
12037       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12038 	goto error_return;
12039 
12040       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
12041       if (((info->warn_shared_textrel && bfd_link_pic (info))
12042 	   || info->error_textrel)
12043 	  && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12044 	{
12045 	  bfd_byte *dyncon, *dynconend;
12046 
12047 	  dyncon = o->contents;
12048 	  dynconend = o->contents + o->size;
12049 	  for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12050 	    {
12051 	      Elf_Internal_Dyn dyn;
12052 
12053 	      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12054 
12055 	      if (dyn.d_tag == DT_TEXTREL)
12056 		{
12057 		  if (info->error_textrel)
12058 		    info->callbacks->einfo
12059 		      (_("%P%X: read-only segment has dynamic relocations.\n"));
12060 		  else
12061 		    info->callbacks->einfo
12062 		      (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12063 		  break;
12064 		}
12065 	    }
12066 	}
12067 
12068       for (o = dynobj->sections; o != NULL; o = o->next)
12069 	{
12070 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
12071 	      || o->size == 0
12072 	      || o->output_section == bfd_abs_section_ptr)
12073 	    continue;
12074 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
12075 	    {
12076 	      /* At this point, we are only interested in sections
12077 		 created by _bfd_elf_link_create_dynamic_sections.  */
12078 	      continue;
12079 	    }
12080 	  if (elf_hash_table (info)->stab_info.stabstr == o)
12081 	    continue;
12082 	  if (elf_hash_table (info)->eh_info.hdr_sec == o)
12083 	    continue;
12084 	  if (strcmp (o->name, ".dynstr") != 0)
12085 	    {
12086 	      if (! bfd_set_section_contents (abfd, o->output_section,
12087 					      o->contents,
12088 					      (file_ptr) o->output_offset
12089 					      * bfd_octets_per_byte (abfd),
12090 					      o->size))
12091 		goto error_return;
12092 	    }
12093 	  else
12094 	    {
12095 	      /* The contents of the .dynstr section are actually in a
12096 		 stringtab.  */
12097 	      file_ptr off;
12098 
12099 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12100 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
12101 		  || ! _bfd_elf_strtab_emit (abfd,
12102 					     elf_hash_table (info)->dynstr))
12103 		goto error_return;
12104 	    }
12105 	}
12106     }
12107 
12108   if (bfd_link_relocatable (info))
12109     {
12110       bfd_boolean failed = FALSE;
12111 
12112       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12113       if (failed)
12114 	goto error_return;
12115     }
12116 
12117   /* If we have optimized stabs strings, output them.  */
12118   if (elf_hash_table (info)->stab_info.stabstr != NULL)
12119     {
12120       if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12121 	goto error_return;
12122     }
12123 
12124   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12125     goto error_return;
12126 
12127   elf_final_link_free (abfd, &flinfo);
12128 
12129   elf_linker (abfd) = TRUE;
12130 
12131   if (attr_section)
12132     {
12133       bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12134       if (contents == NULL)
12135 	return FALSE;	/* Bail out and fail.  */
12136       bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12137       bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12138       free (contents);
12139     }
12140 
12141   return TRUE;
12142 
12143  error_return:
12144   elf_final_link_free (abfd, &flinfo);
12145   return FALSE;
12146 }
12147 
12148 /* Initialize COOKIE for input bfd ABFD.  */
12149 
12150 static bfd_boolean
init_reloc_cookie(struct elf_reloc_cookie * cookie,struct bfd_link_info * info,bfd * abfd)12151 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12152 		   struct bfd_link_info *info, bfd *abfd)
12153 {
12154   Elf_Internal_Shdr *symtab_hdr;
12155   const struct elf_backend_data *bed;
12156 
12157   bed = get_elf_backend_data (abfd);
12158   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12159 
12160   cookie->abfd = abfd;
12161   cookie->sym_hashes = elf_sym_hashes (abfd);
12162   cookie->bad_symtab = elf_bad_symtab (abfd);
12163   if (cookie->bad_symtab)
12164     {
12165       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12166       cookie->extsymoff = 0;
12167     }
12168   else
12169     {
12170       cookie->locsymcount = symtab_hdr->sh_info;
12171       cookie->extsymoff = symtab_hdr->sh_info;
12172     }
12173 
12174   if (bed->s->arch_size == 32)
12175     cookie->r_sym_shift = 8;
12176   else
12177     cookie->r_sym_shift = 32;
12178 
12179   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12180   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12181     {
12182       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12183 					      cookie->locsymcount, 0,
12184 					      NULL, NULL, NULL);
12185       if (cookie->locsyms == NULL)
12186 	{
12187 	  info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12188 	  return FALSE;
12189 	}
12190       if (info->keep_memory)
12191 	symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12192     }
12193   return TRUE;
12194 }
12195 
12196 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
12197 
12198 static void
fini_reloc_cookie(struct elf_reloc_cookie * cookie,bfd * abfd)12199 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12200 {
12201   Elf_Internal_Shdr *symtab_hdr;
12202 
12203   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12204   if (cookie->locsyms != NULL
12205       && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12206     free (cookie->locsyms);
12207 }
12208 
12209 /* Initialize the relocation information in COOKIE for input section SEC
12210    of input bfd ABFD.  */
12211 
12212 static bfd_boolean
init_reloc_cookie_rels(struct elf_reloc_cookie * cookie,struct bfd_link_info * info,bfd * abfd,asection * sec)12213 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12214 			struct bfd_link_info *info, bfd *abfd,
12215 			asection *sec)
12216 {
12217   const struct elf_backend_data *bed;
12218 
12219   if (sec->reloc_count == 0)
12220     {
12221       cookie->rels = NULL;
12222       cookie->relend = NULL;
12223     }
12224   else
12225     {
12226       bed = get_elf_backend_data (abfd);
12227 
12228       cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12229 						info->keep_memory);
12230       if (cookie->rels == NULL)
12231 	return FALSE;
12232       cookie->rel = cookie->rels;
12233       cookie->relend = (cookie->rels
12234 			+ sec->reloc_count * bed->s->int_rels_per_ext_rel);
12235     }
12236   cookie->rel = cookie->rels;
12237   return TRUE;
12238 }
12239 
12240 /* Free the memory allocated by init_reloc_cookie_rels,
12241    if appropriate.  */
12242 
12243 static void
fini_reloc_cookie_rels(struct elf_reloc_cookie * cookie,asection * sec)12244 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12245 			asection *sec)
12246 {
12247   if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12248     free (cookie->rels);
12249 }
12250 
12251 /* Initialize the whole of COOKIE for input section SEC.  */
12252 
12253 static bfd_boolean
init_reloc_cookie_for_section(struct elf_reloc_cookie * cookie,struct bfd_link_info * info,asection * sec)12254 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12255 			       struct bfd_link_info *info,
12256 			       asection *sec)
12257 {
12258   if (!init_reloc_cookie (cookie, info, sec->owner))
12259     goto error1;
12260   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12261     goto error2;
12262   return TRUE;
12263 
12264  error2:
12265   fini_reloc_cookie (cookie, sec->owner);
12266  error1:
12267   return FALSE;
12268 }
12269 
12270 /* Free the memory allocated by init_reloc_cookie_for_section,
12271    if appropriate.  */
12272 
12273 static void
fini_reloc_cookie_for_section(struct elf_reloc_cookie * cookie,asection * sec)12274 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12275 			       asection *sec)
12276 {
12277   fini_reloc_cookie_rels (cookie, sec);
12278   fini_reloc_cookie (cookie, sec->owner);
12279 }
12280 
12281 /* Garbage collect unused sections.  */
12282 
12283 /* Default gc_mark_hook.  */
12284 
12285 asection *
_bfd_elf_gc_mark_hook(asection * sec,struct bfd_link_info * info ATTRIBUTE_UNUSED,Elf_Internal_Rela * rel ATTRIBUTE_UNUSED,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)12286 _bfd_elf_gc_mark_hook (asection *sec,
12287 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
12288 		       Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12289 		       struct elf_link_hash_entry *h,
12290 		       Elf_Internal_Sym *sym)
12291 {
12292   if (h != NULL)
12293     {
12294       switch (h->root.type)
12295 	{
12296 	case bfd_link_hash_defined:
12297 	case bfd_link_hash_defweak:
12298 	  return h->root.u.def.section;
12299 
12300 	case bfd_link_hash_common:
12301 	  return h->root.u.c.p->section;
12302 
12303 	default:
12304 	  break;
12305 	}
12306     }
12307   else
12308     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12309 
12310   return NULL;
12311 }
12312 
12313 /* For undefined __start_<name> and __stop_<name> symbols, return the
12314    first input section matching <name>.  Return NULL otherwise.  */
12315 
12316 asection *
_bfd_elf_is_start_stop(const struct bfd_link_info * info,struct elf_link_hash_entry * h)12317 _bfd_elf_is_start_stop (const struct bfd_link_info *info,
12318 			struct elf_link_hash_entry *h)
12319 {
12320   asection *s;
12321   const char *sec_name;
12322 
12323   if (h->root.type != bfd_link_hash_undefined
12324       && h->root.type != bfd_link_hash_undefweak)
12325     return NULL;
12326 
12327   s = h->root.u.undef.section;
12328   if (s != NULL)
12329     {
12330       if (s == (asection *) 0 - 1)
12331 	return NULL;
12332       return s;
12333     }
12334 
12335   sec_name = NULL;
12336   if (strncmp (h->root.root.string, "__start_", 8) == 0)
12337     sec_name = h->root.root.string + 8;
12338   else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12339     sec_name = h->root.root.string + 7;
12340 
12341   if (sec_name != NULL && *sec_name != '\0')
12342     {
12343       bfd *i;
12344 
12345       for (i = info->input_bfds; i != NULL; i = i->link.next)
12346 	{
12347 	  s = bfd_get_section_by_name (i, sec_name);
12348 	  if (s != NULL)
12349 	    {
12350 	      h->root.u.undef.section = s;
12351 	      break;
12352 	    }
12353 	}
12354     }
12355 
12356   if (s == NULL)
12357     h->root.u.undef.section = (asection *) 0 - 1;
12358 
12359   return s;
12360 }
12361 
12362 /* COOKIE->rel describes a relocation against section SEC, which is
12363    a section we've decided to keep.  Return the section that contains
12364    the relocation symbol, or NULL if no section contains it.  */
12365 
12366 asection *
_bfd_elf_gc_mark_rsec(struct bfd_link_info * info,asection * sec,elf_gc_mark_hook_fn gc_mark_hook,struct elf_reloc_cookie * cookie,bfd_boolean * start_stop)12367 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12368 		       elf_gc_mark_hook_fn gc_mark_hook,
12369 		       struct elf_reloc_cookie *cookie,
12370 		       bfd_boolean *start_stop)
12371 {
12372   unsigned long r_symndx;
12373   struct elf_link_hash_entry *h;
12374 
12375   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12376   if (r_symndx == STN_UNDEF)
12377     return NULL;
12378 
12379   if (r_symndx >= cookie->locsymcount
12380       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12381     {
12382       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12383       if (h == NULL)
12384 	{
12385 	  info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12386 				  sec->owner);
12387 	  return NULL;
12388 	}
12389       while (h->root.type == bfd_link_hash_indirect
12390 	     || h->root.type == bfd_link_hash_warning)
12391 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
12392       h->mark = 1;
12393       /* If this symbol is weak and there is a non-weak definition, we
12394 	 keep the non-weak definition because many backends put
12395 	 dynamic reloc info on the non-weak definition for code
12396 	 handling copy relocs.  */
12397       if (h->u.weakdef != NULL)
12398 	h->u.weakdef->mark = 1;
12399 
12400       if (start_stop != NULL)
12401 	{
12402 	  /* To work around a glibc bug, mark all XXX input sections
12403 	     when there is an as yet undefined reference to __start_XXX
12404 	     or __stop_XXX symbols.  The linker will later define such
12405 	     symbols for orphan input sections that have a name
12406 	     representable as a C identifier.  */
12407 	  asection *s = _bfd_elf_is_start_stop (info, h);
12408 
12409 	  if (s != NULL)
12410 	    {
12411 	      *start_stop = !s->gc_mark;
12412 	      return s;
12413 	    }
12414 	}
12415 
12416       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12417     }
12418 
12419   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12420 			  &cookie->locsyms[r_symndx]);
12421 }
12422 
12423 /* COOKIE->rel describes a relocation against section SEC, which is
12424    a section we've decided to keep.  Mark the section that contains
12425    the relocation symbol.  */
12426 
12427 bfd_boolean
_bfd_elf_gc_mark_reloc(struct bfd_link_info * info,asection * sec,elf_gc_mark_hook_fn gc_mark_hook,struct elf_reloc_cookie * cookie)12428 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12429 			asection *sec,
12430 			elf_gc_mark_hook_fn gc_mark_hook,
12431 			struct elf_reloc_cookie *cookie)
12432 {
12433   asection *rsec;
12434   bfd_boolean start_stop = FALSE;
12435 
12436   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12437   while (rsec != NULL)
12438     {
12439       if (!rsec->gc_mark)
12440 	{
12441 	  if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12442 	      || (rsec->owner->flags & DYNAMIC) != 0)
12443 	    rsec->gc_mark = 1;
12444 	  else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12445 	    return FALSE;
12446 	}
12447       if (!start_stop)
12448 	break;
12449       rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12450     }
12451   return TRUE;
12452 }
12453 
12454 /* The mark phase of garbage collection.  For a given section, mark
12455    it and any sections in this section's group, and all the sections
12456    which define symbols to which it refers.  */
12457 
12458 bfd_boolean
_bfd_elf_gc_mark(struct bfd_link_info * info,asection * sec,elf_gc_mark_hook_fn gc_mark_hook)12459 _bfd_elf_gc_mark (struct bfd_link_info *info,
12460 		  asection *sec,
12461 		  elf_gc_mark_hook_fn gc_mark_hook)
12462 {
12463   bfd_boolean ret;
12464   asection *group_sec, *eh_frame;
12465 
12466   sec->gc_mark = 1;
12467 
12468   /* Mark all the sections in the group.  */
12469   group_sec = elf_section_data (sec)->next_in_group;
12470   if (group_sec && !group_sec->gc_mark)
12471     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12472       return FALSE;
12473 
12474   /* Look through the section relocs.  */
12475   ret = TRUE;
12476   eh_frame = elf_eh_frame_section (sec->owner);
12477   if ((sec->flags & SEC_RELOC) != 0
12478       && sec->reloc_count > 0
12479       && sec != eh_frame)
12480     {
12481       struct elf_reloc_cookie cookie;
12482 
12483       if (!init_reloc_cookie_for_section (&cookie, info, sec))
12484 	ret = FALSE;
12485       else
12486 	{
12487 	  for (; cookie.rel < cookie.relend; cookie.rel++)
12488 	    if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12489 	      {
12490 		ret = FALSE;
12491 		break;
12492 	      }
12493 	  fini_reloc_cookie_for_section (&cookie, sec);
12494 	}
12495     }
12496 
12497   if (ret && eh_frame && elf_fde_list (sec))
12498     {
12499       struct elf_reloc_cookie cookie;
12500 
12501       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12502 	ret = FALSE;
12503       else
12504 	{
12505 	  if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12506 				      gc_mark_hook, &cookie))
12507 	    ret = FALSE;
12508 	  fini_reloc_cookie_for_section (&cookie, eh_frame);
12509 	}
12510     }
12511 
12512   eh_frame = elf_section_eh_frame_entry (sec);
12513   if (ret && eh_frame && !eh_frame->gc_mark)
12514     if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12515       ret = FALSE;
12516 
12517   return ret;
12518 }
12519 
12520 /* Scan and mark sections in a special or debug section group.  */
12521 
12522 static void
_bfd_elf_gc_mark_debug_special_section_group(asection * grp)12523 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12524 {
12525   /* Point to first section of section group.  */
12526   asection *ssec;
12527   /* Used to iterate the section group.  */
12528   asection *msec;
12529 
12530   bfd_boolean is_special_grp = TRUE;
12531   bfd_boolean is_debug_grp = TRUE;
12532 
12533   /* First scan to see if group contains any section other than debug
12534      and special section.  */
12535   ssec = msec = elf_next_in_group (grp);
12536   do
12537     {
12538       if ((msec->flags & SEC_DEBUGGING) == 0)
12539 	is_debug_grp = FALSE;
12540 
12541       if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12542 	is_special_grp = FALSE;
12543 
12544       msec = elf_next_in_group (msec);
12545     }
12546   while (msec != ssec);
12547 
12548   /* If this is a pure debug section group or pure special section group,
12549      keep all sections in this group.  */
12550   if (is_debug_grp || is_special_grp)
12551     {
12552       do
12553 	{
12554 	  msec->gc_mark = 1;
12555 	  msec = elf_next_in_group (msec);
12556 	}
12557       while (msec != ssec);
12558     }
12559 }
12560 
12561 /* Keep debug and special sections.  */
12562 
12563 bfd_boolean
_bfd_elf_gc_mark_extra_sections(struct bfd_link_info * info,elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)12564 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12565 				 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12566 {
12567   bfd *ibfd;
12568 
12569   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12570     {
12571       asection *isec;
12572       bfd_boolean some_kept;
12573       bfd_boolean debug_frag_seen;
12574 
12575       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12576 	continue;
12577 
12578       /* Ensure all linker created sections are kept,
12579 	 see if any other section is already marked,
12580 	 and note if we have any fragmented debug sections.  */
12581       debug_frag_seen = some_kept = FALSE;
12582       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12583 	{
12584 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
12585 	    isec->gc_mark = 1;
12586 	  else if (isec->gc_mark)
12587 	    some_kept = TRUE;
12588 
12589 	  if (debug_frag_seen == FALSE
12590 	      && (isec->flags & SEC_DEBUGGING)
12591 	      && CONST_STRNEQ (isec->name, ".debug_line."))
12592 	    debug_frag_seen = TRUE;
12593 	}
12594 
12595       /* If no section in this file will be kept, then we can
12596 	 toss out the debug and special sections.  */
12597       if (!some_kept)
12598 	continue;
12599 
12600       /* Keep debug and special sections like .comment when they are
12601 	 not part of a group.  Also keep section groups that contain
12602 	 just debug sections or special sections.  */
12603       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12604 	{
12605 	  if ((isec->flags & SEC_GROUP) != 0)
12606 	    _bfd_elf_gc_mark_debug_special_section_group (isec);
12607 	  else if (((isec->flags & SEC_DEBUGGING) != 0
12608 		    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12609 		   && elf_next_in_group (isec) == NULL)
12610 	    isec->gc_mark = 1;
12611 	}
12612 
12613       if (! debug_frag_seen)
12614 	continue;
12615 
12616       /* Look for CODE sections which are going to be discarded,
12617 	 and find and discard any fragmented debug sections which
12618 	 are associated with that code section.  */
12619       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12620 	if ((isec->flags & SEC_CODE) != 0
12621 	    && isec->gc_mark == 0)
12622 	  {
12623 	    unsigned int ilen;
12624 	    asection *dsec;
12625 
12626 	    ilen = strlen (isec->name);
12627 
12628 	    /* Association is determined by the name of the debug section
12629 	       containing the name of the code section as a suffix.  For
12630 	       example .debug_line.text.foo is a debug section associated
12631 	       with .text.foo.  */
12632 	    for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12633 	      {
12634 		unsigned int dlen;
12635 
12636 		if (dsec->gc_mark == 0
12637 		    || (dsec->flags & SEC_DEBUGGING) == 0)
12638 		  continue;
12639 
12640 		dlen = strlen (dsec->name);
12641 
12642 		if (dlen > ilen
12643 		    && strncmp (dsec->name + (dlen - ilen),
12644 				isec->name, ilen) == 0)
12645 		  {
12646 		    dsec->gc_mark = 0;
12647 		  }
12648 	      }
12649 	  }
12650     }
12651   return TRUE;
12652 }
12653 
12654 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
12655 
12656 struct elf_gc_sweep_symbol_info
12657 {
12658   struct bfd_link_info *info;
12659   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12660 		       bfd_boolean);
12661 };
12662 
12663 static bfd_boolean
elf_gc_sweep_symbol(struct elf_link_hash_entry * h,void * data)12664 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12665 {
12666   if (!h->mark
12667       && (((h->root.type == bfd_link_hash_defined
12668 	    || h->root.type == bfd_link_hash_defweak)
12669 	   && !((h->def_regular || ELF_COMMON_DEF_P (h))
12670 		&& h->root.u.def.section->gc_mark))
12671 	  || h->root.type == bfd_link_hash_undefined
12672 	  || h->root.type == bfd_link_hash_undefweak))
12673     {
12674       struct elf_gc_sweep_symbol_info *inf;
12675 
12676       inf = (struct elf_gc_sweep_symbol_info *) data;
12677       (*inf->hide_symbol) (inf->info, h, TRUE);
12678       h->def_regular = 0;
12679       h->ref_regular = 0;
12680       h->ref_regular_nonweak = 0;
12681     }
12682 
12683   return TRUE;
12684 }
12685 
12686 /* The sweep phase of garbage collection.  Remove all garbage sections.  */
12687 
12688 typedef bfd_boolean (*gc_sweep_hook_fn)
12689   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12690 
12691 static bfd_boolean
elf_gc_sweep(bfd * abfd,struct bfd_link_info * info)12692 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12693 {
12694   bfd *sub;
12695   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12696   gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12697   unsigned long section_sym_count;
12698   struct elf_gc_sweep_symbol_info sweep_info;
12699 
12700   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12701     {
12702       asection *o;
12703 
12704       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12705 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12706 	continue;
12707 
12708       for (o = sub->sections; o != NULL; o = o->next)
12709 	{
12710 	  /* When any section in a section group is kept, we keep all
12711 	     sections in the section group.  If the first member of
12712 	     the section group is excluded, we will also exclude the
12713 	     group section.  */
12714 	  if (o->flags & SEC_GROUP)
12715 	    {
12716 	      asection *first = elf_next_in_group (o);
12717 	      o->gc_mark = first->gc_mark;
12718 	    }
12719 
12720 	  if (o->gc_mark)
12721 	    continue;
12722 
12723 	  /* Skip sweeping sections already excluded.  */
12724 	  if (o->flags & SEC_EXCLUDE)
12725 	    continue;
12726 
12727 	  /* Since this is early in the link process, it is simple
12728 	     to remove a section from the output.  */
12729 	  o->flags |= SEC_EXCLUDE;
12730 
12731 	  if (info->print_gc_sections && o->size != 0)
12732 	    _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12733 
12734 	  /* But we also have to update some of the relocation
12735 	     info we collected before.  */
12736 	  if (gc_sweep_hook
12737 	      && (o->flags & SEC_RELOC) != 0
12738 	      && o->reloc_count != 0
12739 	      && !((info->strip == strip_all || info->strip == strip_debugger)
12740 		   && (o->flags & SEC_DEBUGGING) != 0)
12741 	      && !bfd_is_abs_section (o->output_section))
12742 	    {
12743 	      Elf_Internal_Rela *internal_relocs;
12744 	      bfd_boolean r;
12745 
12746 	      internal_relocs
12747 		= _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12748 					     info->keep_memory);
12749 	      if (internal_relocs == NULL)
12750 		return FALSE;
12751 
12752 	      r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12753 
12754 	      if (elf_section_data (o)->relocs != internal_relocs)
12755 		free (internal_relocs);
12756 
12757 	      if (!r)
12758 		return FALSE;
12759 	    }
12760 	}
12761     }
12762 
12763   /* Remove the symbols that were in the swept sections from the dynamic
12764      symbol table.  GCFIXME: Anyone know how to get them out of the
12765      static symbol table as well?  */
12766   sweep_info.info = info;
12767   sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12768   elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12769 			  &sweep_info);
12770 
12771   _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12772   return TRUE;
12773 }
12774 
12775 /* Propagate collected vtable information.  This is called through
12776    elf_link_hash_traverse.  */
12777 
12778 static bfd_boolean
elf_gc_propagate_vtable_entries_used(struct elf_link_hash_entry * h,void * okp)12779 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12780 {
12781   /* Those that are not vtables.  */
12782   if (h->vtable == NULL || h->vtable->parent == NULL)
12783     return TRUE;
12784 
12785   /* Those vtables that do not have parents, we cannot merge.  */
12786   if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12787     return TRUE;
12788 
12789   /* If we've already been done, exit.  */
12790   if (h->vtable->used && h->vtable->used[-1])
12791     return TRUE;
12792 
12793   /* Make sure the parent's table is up to date.  */
12794   elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12795 
12796   if (h->vtable->used == NULL)
12797     {
12798       /* None of this table's entries were referenced.  Re-use the
12799 	 parent's table.  */
12800       h->vtable->used = h->vtable->parent->vtable->used;
12801       h->vtable->size = h->vtable->parent->vtable->size;
12802     }
12803   else
12804     {
12805       size_t n;
12806       bfd_boolean *cu, *pu;
12807 
12808       /* Or the parent's entries into ours.  */
12809       cu = h->vtable->used;
12810       cu[-1] = TRUE;
12811       pu = h->vtable->parent->vtable->used;
12812       if (pu != NULL)
12813 	{
12814 	  const struct elf_backend_data *bed;
12815 	  unsigned int log_file_align;
12816 
12817 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
12818 	  log_file_align = bed->s->log_file_align;
12819 	  n = h->vtable->parent->vtable->size >> log_file_align;
12820 	  while (n--)
12821 	    {
12822 	      if (*pu)
12823 		*cu = TRUE;
12824 	      pu++;
12825 	      cu++;
12826 	    }
12827 	}
12828     }
12829 
12830   return TRUE;
12831 }
12832 
12833 static bfd_boolean
elf_gc_smash_unused_vtentry_relocs(struct elf_link_hash_entry * h,void * okp)12834 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12835 {
12836   asection *sec;
12837   bfd_vma hstart, hend;
12838   Elf_Internal_Rela *relstart, *relend, *rel;
12839   const struct elf_backend_data *bed;
12840   unsigned int log_file_align;
12841 
12842   /* Take care of both those symbols that do not describe vtables as
12843      well as those that are not loaded.  */
12844   if (h->vtable == NULL || h->vtable->parent == NULL)
12845     return TRUE;
12846 
12847   BFD_ASSERT (h->root.type == bfd_link_hash_defined
12848 	      || h->root.type == bfd_link_hash_defweak);
12849 
12850   sec = h->root.u.def.section;
12851   hstart = h->root.u.def.value;
12852   hend = hstart + h->size;
12853 
12854   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12855   if (!relstart)
12856     return *(bfd_boolean *) okp = FALSE;
12857   bed = get_elf_backend_data (sec->owner);
12858   log_file_align = bed->s->log_file_align;
12859 
12860   relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12861 
12862   for (rel = relstart; rel < relend; ++rel)
12863     if (rel->r_offset >= hstart && rel->r_offset < hend)
12864       {
12865 	/* If the entry is in use, do nothing.  */
12866 	if (h->vtable->used
12867 	    && (rel->r_offset - hstart) < h->vtable->size)
12868 	  {
12869 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12870 	    if (h->vtable->used[entry])
12871 	      continue;
12872 	  }
12873 	/* Otherwise, kill it.  */
12874 	rel->r_offset = rel->r_info = rel->r_addend = 0;
12875       }
12876 
12877   return TRUE;
12878 }
12879 
12880 /* Mark sections containing dynamically referenced symbols.  When
12881    building shared libraries, we must assume that any visible symbol is
12882    referenced.  */
12883 
12884 bfd_boolean
bfd_elf_gc_mark_dynamic_ref_symbol(struct elf_link_hash_entry * h,void * inf)12885 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12886 {
12887   struct bfd_link_info *info = (struct bfd_link_info *) inf;
12888   struct bfd_elf_dynamic_list *d = info->dynamic_list;
12889 
12890   if ((h->root.type == bfd_link_hash_defined
12891        || h->root.type == bfd_link_hash_defweak)
12892       && (h->ref_dynamic
12893 	  || ((h->def_regular || ELF_COMMON_DEF_P (h))
12894 	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12895 	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12896 	      && (!bfd_link_executable (info)
12897 		  || info->export_dynamic
12898 		  || (h->dynamic
12899 		      && d != NULL
12900 		      && (*d->match) (&d->head, NULL, h->root.root.string)))
12901 	      && (h->versioned >= versioned
12902 		  || !bfd_hide_sym_by_version (info->version_info,
12903 					       h->root.root.string)))))
12904     h->root.u.def.section->flags |= SEC_KEEP;
12905 
12906   return TRUE;
12907 }
12908 
12909 /* Keep all sections containing symbols undefined on the command-line,
12910    and the section containing the entry symbol.  */
12911 
12912 void
_bfd_elf_gc_keep(struct bfd_link_info * info)12913 _bfd_elf_gc_keep (struct bfd_link_info *info)
12914 {
12915   struct bfd_sym_chain *sym;
12916 
12917   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12918     {
12919       struct elf_link_hash_entry *h;
12920 
12921       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12922 				FALSE, FALSE, FALSE);
12923 
12924       if (h != NULL
12925 	  && (h->root.type == bfd_link_hash_defined
12926 	      || h->root.type == bfd_link_hash_defweak)
12927 	  && !bfd_is_abs_section (h->root.u.def.section))
12928 	h->root.u.def.section->flags |= SEC_KEEP;
12929     }
12930 }
12931 
12932 bfd_boolean
bfd_elf_parse_eh_frame_entries(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)12933 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12934 				struct bfd_link_info *info)
12935 {
12936   bfd *ibfd = info->input_bfds;
12937 
12938   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12939     {
12940       asection *sec;
12941       struct elf_reloc_cookie cookie;
12942 
12943       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12944 	continue;
12945 
12946       if (!init_reloc_cookie (&cookie, info, ibfd))
12947 	return FALSE;
12948 
12949       for (sec = ibfd->sections; sec; sec = sec->next)
12950 	{
12951 	  if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12952 	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12953 	    {
12954 	      _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12955 	      fini_reloc_cookie_rels (&cookie, sec);
12956 	    }
12957 	}
12958     }
12959   return TRUE;
12960 }
12961 
12962 /* Do mark and sweep of unused sections.  */
12963 
12964 bfd_boolean
bfd_elf_gc_sections(bfd * abfd,struct bfd_link_info * info)12965 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12966 {
12967   bfd_boolean ok = TRUE;
12968   bfd *sub;
12969   elf_gc_mark_hook_fn gc_mark_hook;
12970   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12971   struct elf_link_hash_table *htab;
12972 
12973   if (!bed->can_gc_sections
12974       || !is_elf_hash_table (info->hash))
12975     {
12976       (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12977       return TRUE;
12978     }
12979 
12980   bed->gc_keep (info);
12981   htab = elf_hash_table (info);
12982 
12983   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
12984      at the .eh_frame section if we can mark the FDEs individually.  */
12985   for (sub = info->input_bfds;
12986        info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12987        sub = sub->link.next)
12988     {
12989       asection *sec;
12990       struct elf_reloc_cookie cookie;
12991 
12992       sec = bfd_get_section_by_name (sub, ".eh_frame");
12993       while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12994 	{
12995 	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12996 	  if (elf_section_data (sec)->sec_info
12997 	      && (sec->flags & SEC_LINKER_CREATED) == 0)
12998 	    elf_eh_frame_section (sub) = sec;
12999 	  fini_reloc_cookie_for_section (&cookie, sec);
13000 	  sec = bfd_get_next_section_by_name (NULL, sec);
13001 	}
13002     }
13003 
13004   /* Apply transitive closure to the vtable entry usage info.  */
13005   elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13006   if (!ok)
13007     return FALSE;
13008 
13009   /* Kill the vtable relocations that were not used.  */
13010   elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13011   if (!ok)
13012     return FALSE;
13013 
13014   /* Mark dynamically referenced symbols.  */
13015   if (htab->dynamic_sections_created)
13016     elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13017 
13018   /* Grovel through relocs to find out who stays ...  */
13019   gc_mark_hook = bed->gc_mark_hook;
13020   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13021     {
13022       asection *o;
13023 
13024       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13025 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13026 	continue;
13027 
13028       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13029 	 Also treat note sections as a root, if the section is not part
13030 	 of a group.  */
13031       for (o = sub->sections; o != NULL; o = o->next)
13032 	if (!o->gc_mark
13033 	    && (o->flags & SEC_EXCLUDE) == 0
13034 	    && ((o->flags & SEC_KEEP) != 0
13035 		|| (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13036 		    && elf_next_in_group (o) == NULL )))
13037 	  {
13038 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13039 	      return FALSE;
13040 	  }
13041     }
13042 
13043   /* Allow the backend to mark additional target specific sections.  */
13044   bed->gc_mark_extra_sections (info, gc_mark_hook);
13045 
13046   /* ... and mark SEC_EXCLUDE for those that go.  */
13047   return elf_gc_sweep (abfd, info);
13048 }
13049 
13050 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
13051 
13052 bfd_boolean
bfd_elf_gc_record_vtinherit(bfd * abfd,asection * sec,struct elf_link_hash_entry * h,bfd_vma offset)13053 bfd_elf_gc_record_vtinherit (bfd *abfd,
13054 			     asection *sec,
13055 			     struct elf_link_hash_entry *h,
13056 			     bfd_vma offset)
13057 {
13058   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13059   struct elf_link_hash_entry **search, *child;
13060   size_t extsymcount;
13061   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13062 
13063   /* The sh_info field of the symtab header tells us where the
13064      external symbols start.  We don't care about the local symbols at
13065      this point.  */
13066   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13067   if (!elf_bad_symtab (abfd))
13068     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13069 
13070   sym_hashes = elf_sym_hashes (abfd);
13071   sym_hashes_end = sym_hashes + extsymcount;
13072 
13073   /* Hunt down the child symbol, which is in this section at the same
13074      offset as the relocation.  */
13075   for (search = sym_hashes; search != sym_hashes_end; ++search)
13076     {
13077       if ((child = *search) != NULL
13078 	  && (child->root.type == bfd_link_hash_defined
13079 	      || child->root.type == bfd_link_hash_defweak)
13080 	  && child->root.u.def.section == sec
13081 	  && child->root.u.def.value == offset)
13082 	goto win;
13083     }
13084 
13085   (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
13086 			 abfd, sec, (unsigned long) offset);
13087   bfd_set_error (bfd_error_invalid_operation);
13088   return FALSE;
13089 
13090  win:
13091   if (!child->vtable)
13092     {
13093       child->vtable = ((struct elf_link_virtual_table_entry *)
13094 		       bfd_zalloc (abfd, sizeof (*child->vtable)));
13095       if (!child->vtable)
13096 	return FALSE;
13097     }
13098   if (!h)
13099     {
13100       /* This *should* only be the absolute section.  It could potentially
13101 	 be that someone has defined a non-global vtable though, which
13102 	 would be bad.  It isn't worth paging in the local symbols to be
13103 	 sure though; that case should simply be handled by the assembler.  */
13104 
13105       child->vtable->parent = (struct elf_link_hash_entry *) -1;
13106     }
13107   else
13108     child->vtable->parent = h;
13109 
13110   return TRUE;
13111 }
13112 
13113 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
13114 
13115 bfd_boolean
bfd_elf_gc_record_vtentry(bfd * abfd ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED,struct elf_link_hash_entry * h,bfd_vma addend)13116 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13117 			   asection *sec ATTRIBUTE_UNUSED,
13118 			   struct elf_link_hash_entry *h,
13119 			   bfd_vma addend)
13120 {
13121   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13122   unsigned int log_file_align = bed->s->log_file_align;
13123 
13124   if (!h->vtable)
13125     {
13126       h->vtable = ((struct elf_link_virtual_table_entry *)
13127 		   bfd_zalloc (abfd, sizeof (*h->vtable)));
13128       if (!h->vtable)
13129 	return FALSE;
13130     }
13131 
13132   if (addend >= h->vtable->size)
13133     {
13134       size_t size, bytes, file_align;
13135       bfd_boolean *ptr = h->vtable->used;
13136 
13137       /* While the symbol is undefined, we have to be prepared to handle
13138 	 a zero size.  */
13139       file_align = 1 << log_file_align;
13140       if (h->root.type == bfd_link_hash_undefined)
13141 	size = addend + file_align;
13142       else
13143 	{
13144 	  size = h->size;
13145 	  if (addend >= size)
13146 	    {
13147 	      /* Oops!  We've got a reference past the defined end of
13148 		 the table.  This is probably a bug -- shall we warn?  */
13149 	      size = addend + file_align;
13150 	    }
13151 	}
13152       size = (size + file_align - 1) & -file_align;
13153 
13154       /* Allocate one extra entry for use as a "done" flag for the
13155 	 consolidation pass.  */
13156       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13157 
13158       if (ptr)
13159 	{
13160 	  ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13161 
13162 	  if (ptr != NULL)
13163 	    {
13164 	      size_t oldbytes;
13165 
13166 	      oldbytes = (((h->vtable->size >> log_file_align) + 1)
13167 			  * sizeof (bfd_boolean));
13168 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13169 	    }
13170 	}
13171       else
13172 	ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13173 
13174       if (ptr == NULL)
13175 	return FALSE;
13176 
13177       /* And arrange for that done flag to be at index -1.  */
13178       h->vtable->used = ptr + 1;
13179       h->vtable->size = size;
13180     }
13181 
13182   h->vtable->used[addend >> log_file_align] = TRUE;
13183 
13184   return TRUE;
13185 }
13186 
13187 /* Map an ELF section header flag to its corresponding string.  */
13188 typedef struct
13189 {
13190   char *flag_name;
13191   flagword flag_value;
13192 } elf_flags_to_name_table;
13193 
13194 static elf_flags_to_name_table elf_flags_to_names [] =
13195 {
13196   { "SHF_WRITE", SHF_WRITE },
13197   { "SHF_ALLOC", SHF_ALLOC },
13198   { "SHF_EXECINSTR", SHF_EXECINSTR },
13199   { "SHF_MERGE", SHF_MERGE },
13200   { "SHF_STRINGS", SHF_STRINGS },
13201   { "SHF_INFO_LINK", SHF_INFO_LINK},
13202   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13203   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13204   { "SHF_GROUP", SHF_GROUP },
13205   { "SHF_TLS", SHF_TLS },
13206   { "SHF_MASKOS", SHF_MASKOS },
13207   { "SHF_EXCLUDE", SHF_EXCLUDE },
13208 };
13209 
13210 /* Returns TRUE if the section is to be included, otherwise FALSE.  */
13211 bfd_boolean
bfd_elf_lookup_section_flags(struct bfd_link_info * info,struct flag_info * flaginfo,asection * section)13212 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13213 			      struct flag_info *flaginfo,
13214 			      asection *section)
13215 {
13216   const bfd_vma sh_flags = elf_section_flags (section);
13217 
13218   if (!flaginfo->flags_initialized)
13219     {
13220       bfd *obfd = info->output_bfd;
13221       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13222       struct flag_info_list *tf = flaginfo->flag_list;
13223       int with_hex = 0;
13224       int without_hex = 0;
13225 
13226       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13227 	{
13228 	  unsigned i;
13229 	  flagword (*lookup) (char *);
13230 
13231 	  lookup = bed->elf_backend_lookup_section_flags_hook;
13232 	  if (lookup != NULL)
13233 	    {
13234 	      flagword hexval = (*lookup) ((char *) tf->name);
13235 
13236 	      if (hexval != 0)
13237 		{
13238 		  if (tf->with == with_flags)
13239 		    with_hex |= hexval;
13240 		  else if (tf->with == without_flags)
13241 		    without_hex |= hexval;
13242 		  tf->valid = TRUE;
13243 		  continue;
13244 		}
13245 	    }
13246 	  for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13247 	    {
13248 	      if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13249 		{
13250 		  if (tf->with == with_flags)
13251 		    with_hex |= elf_flags_to_names[i].flag_value;
13252 		  else if (tf->with == without_flags)
13253 		    without_hex |= elf_flags_to_names[i].flag_value;
13254 		  tf->valid = TRUE;
13255 		  break;
13256 		}
13257 	    }
13258 	  if (!tf->valid)
13259 	    {
13260 	      info->callbacks->einfo
13261 		(_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13262 	      return FALSE;
13263 	    }
13264 	}
13265       flaginfo->flags_initialized = TRUE;
13266       flaginfo->only_with_flags |= with_hex;
13267       flaginfo->not_with_flags |= without_hex;
13268     }
13269 
13270   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13271     return FALSE;
13272 
13273   if ((flaginfo->not_with_flags & sh_flags) != 0)
13274     return FALSE;
13275 
13276   return TRUE;
13277 }
13278 
13279 struct alloc_got_off_arg {
13280   bfd_vma gotoff;
13281   struct bfd_link_info *info;
13282 };
13283 
13284 /* We need a special top-level link routine to convert got reference counts
13285    to real got offsets.  */
13286 
13287 static bfd_boolean
elf_gc_allocate_got_offsets(struct elf_link_hash_entry * h,void * arg)13288 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13289 {
13290   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13291   bfd *obfd = gofarg->info->output_bfd;
13292   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13293 
13294   if (h->got.refcount > 0)
13295     {
13296       h->got.offset = gofarg->gotoff;
13297       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13298     }
13299   else
13300     h->got.offset = (bfd_vma) -1;
13301 
13302   return TRUE;
13303 }
13304 
13305 /* And an accompanying bit to work out final got entry offsets once
13306    we're done.  Should be called from final_link.  */
13307 
13308 bfd_boolean
bfd_elf_gc_common_finalize_got_offsets(bfd * abfd,struct bfd_link_info * info)13309 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13310 					struct bfd_link_info *info)
13311 {
13312   bfd *i;
13313   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13314   bfd_vma gotoff;
13315   struct alloc_got_off_arg gofarg;
13316 
13317   BFD_ASSERT (abfd == info->output_bfd);
13318 
13319   if (! is_elf_hash_table (info->hash))
13320     return FALSE;
13321 
13322   /* The GOT offset is relative to the .got section, but the GOT header is
13323      put into the .got.plt section, if the backend uses it.  */
13324   if (bed->want_got_plt)
13325     gotoff = 0;
13326   else
13327     gotoff = bed->got_header_size;
13328 
13329   /* Do the local .got entries first.  */
13330   for (i = info->input_bfds; i; i = i->link.next)
13331     {
13332       bfd_signed_vma *local_got;
13333       size_t j, locsymcount;
13334       Elf_Internal_Shdr *symtab_hdr;
13335 
13336       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13337 	continue;
13338 
13339       local_got = elf_local_got_refcounts (i);
13340       if (!local_got)
13341 	continue;
13342 
13343       symtab_hdr = &elf_tdata (i)->symtab_hdr;
13344       if (elf_bad_symtab (i))
13345 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13346       else
13347 	locsymcount = symtab_hdr->sh_info;
13348 
13349       for (j = 0; j < locsymcount; ++j)
13350 	{
13351 	  if (local_got[j] > 0)
13352 	    {
13353 	      local_got[j] = gotoff;
13354 	      gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13355 	    }
13356 	  else
13357 	    local_got[j] = (bfd_vma) -1;
13358 	}
13359     }
13360 
13361   /* Then the global .got entries.  .plt refcounts are handled by
13362      adjust_dynamic_symbol  */
13363   gofarg.gotoff = gotoff;
13364   gofarg.info = info;
13365   elf_link_hash_traverse (elf_hash_table (info),
13366 			  elf_gc_allocate_got_offsets,
13367 			  &gofarg);
13368   return TRUE;
13369 }
13370 
13371 /* Many folk need no more in the way of final link than this, once
13372    got entry reference counting is enabled.  */
13373 
13374 bfd_boolean
bfd_elf_gc_common_final_link(bfd * abfd,struct bfd_link_info * info)13375 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13376 {
13377   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13378     return FALSE;
13379 
13380   /* Invoke the regular ELF backend linker to do all the work.  */
13381   return bfd_elf_final_link (abfd, info);
13382 }
13383 
13384 bfd_boolean
bfd_elf_reloc_symbol_deleted_p(bfd_vma offset,void * cookie)13385 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13386 {
13387   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13388 
13389   if (rcookie->bad_symtab)
13390     rcookie->rel = rcookie->rels;
13391 
13392   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13393     {
13394       unsigned long r_symndx;
13395 
13396       if (! rcookie->bad_symtab)
13397 	if (rcookie->rel->r_offset > offset)
13398 	  return FALSE;
13399       if (rcookie->rel->r_offset != offset)
13400 	continue;
13401 
13402       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13403       if (r_symndx == STN_UNDEF)
13404 	return TRUE;
13405 
13406       if (r_symndx >= rcookie->locsymcount
13407 	  || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13408 	{
13409 	  struct elf_link_hash_entry *h;
13410 
13411 	  h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13412 
13413 	  while (h->root.type == bfd_link_hash_indirect
13414 		 || h->root.type == bfd_link_hash_warning)
13415 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
13416 
13417 	  if ((h->root.type == bfd_link_hash_defined
13418 	       || h->root.type == bfd_link_hash_defweak)
13419 	      && (h->root.u.def.section->owner != rcookie->abfd
13420 		  || h->root.u.def.section->kept_section != NULL
13421 		  || discarded_section (h->root.u.def.section)))
13422 	    return TRUE;
13423 	}
13424       else
13425 	{
13426 	  /* It's not a relocation against a global symbol,
13427 	     but it could be a relocation against a local
13428 	     symbol for a discarded section.  */
13429 	  asection *isec;
13430 	  Elf_Internal_Sym *isym;
13431 
13432 	  /* Need to: get the symbol; get the section.  */
13433 	  isym = &rcookie->locsyms[r_symndx];
13434 	  isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13435 	  if (isec != NULL
13436 	      && (isec->kept_section != NULL
13437 		  || discarded_section (isec)))
13438 	    return TRUE;
13439 	}
13440       return FALSE;
13441     }
13442   return FALSE;
13443 }
13444 
13445 /* Discard unneeded references to discarded sections.
13446    Returns -1 on error, 1 if any section's size was changed, 0 if
13447    nothing changed.  This function assumes that the relocations are in
13448    sorted order, which is true for all known assemblers.  */
13449 
13450 int
bfd_elf_discard_info(bfd * output_bfd,struct bfd_link_info * info)13451 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13452 {
13453   struct elf_reloc_cookie cookie;
13454   asection *o;
13455   bfd *abfd;
13456   int changed = 0;
13457 
13458   if (info->traditional_format
13459       || !is_elf_hash_table (info->hash))
13460     return 0;
13461 
13462   o = bfd_get_section_by_name (output_bfd, ".stab");
13463   if (o != NULL)
13464     {
13465       asection *i;
13466 
13467       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13468 	{
13469 	  if (i->size == 0
13470 	      || i->reloc_count == 0
13471 	      || i->sec_info_type != SEC_INFO_TYPE_STABS)
13472 	    continue;
13473 
13474 	  abfd = i->owner;
13475 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13476 	    continue;
13477 
13478 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
13479 	    return -1;
13480 
13481 	  if (_bfd_discard_section_stabs (abfd, i,
13482 					  elf_section_data (i)->sec_info,
13483 					  bfd_elf_reloc_symbol_deleted_p,
13484 					  &cookie))
13485 	    changed = 1;
13486 
13487 	  fini_reloc_cookie_for_section (&cookie, i);
13488 	}
13489     }
13490 
13491   o = NULL;
13492   if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13493     o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13494   if (o != NULL)
13495     {
13496       asection *i;
13497 
13498       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13499 	{
13500 	  if (i->size == 0)
13501 	    continue;
13502 
13503 	  abfd = i->owner;
13504 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13505 	    continue;
13506 
13507 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
13508 	    return -1;
13509 
13510 	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13511 	  if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13512 						 bfd_elf_reloc_symbol_deleted_p,
13513 						 &cookie))
13514 	    changed = 1;
13515 
13516 	  fini_reloc_cookie_for_section (&cookie, i);
13517 	}
13518     }
13519 
13520   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13521     {
13522       const struct elf_backend_data *bed;
13523 
13524       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13525 	continue;
13526 
13527       bed = get_elf_backend_data (abfd);
13528 
13529       if (bed->elf_backend_discard_info != NULL)
13530 	{
13531 	  if (!init_reloc_cookie (&cookie, info, abfd))
13532 	    return -1;
13533 
13534 	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13535 	    changed = 1;
13536 
13537 	  fini_reloc_cookie (&cookie, abfd);
13538 	}
13539     }
13540 
13541   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13542     _bfd_elf_end_eh_frame_parsing (info);
13543 
13544   if (info->eh_frame_hdr_type
13545       && !bfd_link_relocatable (info)
13546       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13547     changed = 1;
13548 
13549   return changed;
13550 }
13551 
13552 bfd_boolean
_bfd_elf_section_already_linked(bfd * abfd,asection * sec,struct bfd_link_info * info)13553 _bfd_elf_section_already_linked (bfd *abfd,
13554 				 asection *sec,
13555 				 struct bfd_link_info *info)
13556 {
13557   flagword flags;
13558   const char *name, *key;
13559   struct bfd_section_already_linked *l;
13560   struct bfd_section_already_linked_hash_entry *already_linked_list;
13561 
13562   if (sec->output_section == bfd_abs_section_ptr)
13563     return FALSE;
13564 
13565   flags = sec->flags;
13566 
13567   /* Return if it isn't a linkonce section.  A comdat group section
13568      also has SEC_LINK_ONCE set.  */
13569   if ((flags & SEC_LINK_ONCE) == 0)
13570     return FALSE;
13571 
13572   /* Don't put group member sections on our list of already linked
13573      sections.  They are handled as a group via their group section.  */
13574   if (elf_sec_group (sec) != NULL)
13575     return FALSE;
13576 
13577   /* For a SHT_GROUP section, use the group signature as the key.  */
13578   name = sec->name;
13579   if ((flags & SEC_GROUP) != 0
13580       && elf_next_in_group (sec) != NULL
13581       && elf_group_name (elf_next_in_group (sec)) != NULL)
13582     key = elf_group_name (elf_next_in_group (sec));
13583   else
13584     {
13585       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
13586       if (CONST_STRNEQ (name, ".gnu.linkonce.")
13587 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13588 	key++;
13589       else
13590 	/* Must be a user linkonce section that doesn't follow gcc's
13591 	   naming convention.  In this case we won't be matching
13592 	   single member groups.  */
13593 	key = name;
13594     }
13595 
13596   already_linked_list = bfd_section_already_linked_table_lookup (key);
13597 
13598   for (l = already_linked_list->entry; l != NULL; l = l->next)
13599     {
13600       /* We may have 2 different types of sections on the list: group
13601 	 sections with a signature of <key> (<key> is some string),
13602 	 and linkonce sections named .gnu.linkonce.<type>.<key>.
13603 	 Match like sections.  LTO plugin sections are an exception.
13604 	 They are always named .gnu.linkonce.t.<key> and match either
13605 	 type of section.  */
13606       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13607 	   && ((flags & SEC_GROUP) != 0
13608 	       || strcmp (name, l->sec->name) == 0))
13609 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13610 	{
13611 	  /* The section has already been linked.  See if we should
13612 	     issue a warning.  */
13613 	  if (!_bfd_handle_already_linked (sec, l, info))
13614 	    return FALSE;
13615 
13616 	  if (flags & SEC_GROUP)
13617 	    {
13618 	      asection *first = elf_next_in_group (sec);
13619 	      asection *s = first;
13620 
13621 	      while (s != NULL)
13622 		{
13623 		  s->output_section = bfd_abs_section_ptr;
13624 		  /* Record which group discards it.  */
13625 		  s->kept_section = l->sec;
13626 		  s = elf_next_in_group (s);
13627 		  /* These lists are circular.  */
13628 		  if (s == first)
13629 		    break;
13630 		}
13631 	    }
13632 
13633 	  return TRUE;
13634 	}
13635     }
13636 
13637   /* A single member comdat group section may be discarded by a
13638      linkonce section and vice versa.  */
13639   if ((flags & SEC_GROUP) != 0)
13640     {
13641       asection *first = elf_next_in_group (sec);
13642 
13643       if (first != NULL && elf_next_in_group (first) == first)
13644 	/* Check this single member group against linkonce sections.  */
13645 	for (l = already_linked_list->entry; l != NULL; l = l->next)
13646 	  if ((l->sec->flags & SEC_GROUP) == 0
13647 	      && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13648 	    {
13649 	      first->output_section = bfd_abs_section_ptr;
13650 	      first->kept_section = l->sec;
13651 	      sec->output_section = bfd_abs_section_ptr;
13652 	      break;
13653 	    }
13654     }
13655   else
13656     /* Check this linkonce section against single member groups.  */
13657     for (l = already_linked_list->entry; l != NULL; l = l->next)
13658       if (l->sec->flags & SEC_GROUP)
13659 	{
13660 	  asection *first = elf_next_in_group (l->sec);
13661 
13662 	  if (first != NULL
13663 	      && elf_next_in_group (first) == first
13664 	      && bfd_elf_match_symbols_in_sections (first, sec, info))
13665 	    {
13666 	      sec->output_section = bfd_abs_section_ptr;
13667 	      sec->kept_section = first;
13668 	      break;
13669 	    }
13670 	}
13671 
13672   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13673      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13674      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13675      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
13676      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
13677      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13678      `.gnu.linkonce.t.F' section from a different bfd not requiring any
13679      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
13680      The reverse order cannot happen as there is never a bfd with only the
13681      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
13682      matter as here were are looking only for cross-bfd sections.  */
13683 
13684   if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13685     for (l = already_linked_list->entry; l != NULL; l = l->next)
13686       if ((l->sec->flags & SEC_GROUP) == 0
13687 	  && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13688 	{
13689 	  if (abfd != l->sec->owner)
13690 	    sec->output_section = bfd_abs_section_ptr;
13691 	  break;
13692 	}
13693 
13694   /* This is the first section with this name.  Record it.  */
13695   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13696     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13697   return sec->output_section == bfd_abs_section_ptr;
13698 }
13699 
13700 bfd_boolean
_bfd_elf_common_definition(Elf_Internal_Sym * sym)13701 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13702 {
13703   return sym->st_shndx == SHN_COMMON;
13704 }
13705 
13706 unsigned int
_bfd_elf_common_section_index(asection * sec ATTRIBUTE_UNUSED)13707 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13708 {
13709   return SHN_COMMON;
13710 }
13711 
13712 asection *
_bfd_elf_common_section(asection * sec ATTRIBUTE_UNUSED)13713 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13714 {
13715   return bfd_com_section_ptr;
13716 }
13717 
13718 bfd_vma
_bfd_elf_default_got_elt_size(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED,struct elf_link_hash_entry * h ATTRIBUTE_UNUSED,bfd * ibfd ATTRIBUTE_UNUSED,unsigned long symndx ATTRIBUTE_UNUSED)13719 _bfd_elf_default_got_elt_size (bfd *abfd,
13720 			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
13721 			       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13722 			       bfd *ibfd ATTRIBUTE_UNUSED,
13723 			       unsigned long symndx ATTRIBUTE_UNUSED)
13724 {
13725   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13726   return bed->s->arch_size / 8;
13727 }
13728 
13729 /* Routines to support the creation of dynamic relocs.  */
13730 
13731 /* Returns the name of the dynamic reloc section associated with SEC.  */
13732 
13733 static const char *
get_dynamic_reloc_section_name(bfd * abfd,asection * sec,bfd_boolean is_rela)13734 get_dynamic_reloc_section_name (bfd *       abfd,
13735 				asection *  sec,
13736 				bfd_boolean is_rela)
13737 {
13738   char *name;
13739   const char *old_name = bfd_get_section_name (NULL, sec);
13740   const char *prefix = is_rela ? ".rela" : ".rel";
13741 
13742   if (old_name == NULL)
13743     return NULL;
13744 
13745   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13746   sprintf (name, "%s%s", prefix, old_name);
13747 
13748   return name;
13749 }
13750 
13751 /* Returns the dynamic reloc section associated with SEC.
13752    If necessary compute the name of the dynamic reloc section based
13753    on SEC's name (looked up in ABFD's string table) and the setting
13754    of IS_RELA.  */
13755 
13756 asection *
_bfd_elf_get_dynamic_reloc_section(bfd * abfd,asection * sec,bfd_boolean is_rela)13757 _bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
13758 				    asection *  sec,
13759 				    bfd_boolean is_rela)
13760 {
13761   asection * reloc_sec = elf_section_data (sec)->sreloc;
13762 
13763   if (reloc_sec == NULL)
13764     {
13765       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13766 
13767       if (name != NULL)
13768 	{
13769 	  reloc_sec = bfd_get_linker_section (abfd, name);
13770 
13771 	  if (reloc_sec != NULL)
13772 	    elf_section_data (sec)->sreloc = reloc_sec;
13773 	}
13774     }
13775 
13776   return reloc_sec;
13777 }
13778 
13779 /* Returns the dynamic reloc section associated with SEC.  If the
13780    section does not exist it is created and attached to the DYNOBJ
13781    bfd and stored in the SRELOC field of SEC's elf_section_data
13782    structure.
13783 
13784    ALIGNMENT is the alignment for the newly created section and
13785    IS_RELA defines whether the name should be .rela.<SEC's name>
13786    or .rel.<SEC's name>.  The section name is looked up in the
13787    string table associated with ABFD.  */
13788 
13789 asection *
_bfd_elf_make_dynamic_reloc_section(asection * sec,bfd * dynobj,unsigned int alignment,bfd * abfd,bfd_boolean is_rela)13790 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13791 				     bfd *dynobj,
13792 				     unsigned int alignment,
13793 				     bfd *abfd,
13794 				     bfd_boolean is_rela)
13795 {
13796   asection * reloc_sec = elf_section_data (sec)->sreloc;
13797 
13798   if (reloc_sec == NULL)
13799     {
13800       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13801 
13802       if (name == NULL)
13803 	return NULL;
13804 
13805       reloc_sec = bfd_get_linker_section (dynobj, name);
13806 
13807       if (reloc_sec == NULL)
13808 	{
13809 	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13810 			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13811 	  if ((sec->flags & SEC_ALLOC) != 0)
13812 	    flags |= SEC_ALLOC | SEC_LOAD;
13813 
13814 	  reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13815 	  if (reloc_sec != NULL)
13816 	    {
13817 	      /* _bfd_elf_get_sec_type_attr chooses a section type by
13818 		 name.  Override as it may be wrong, eg. for a user
13819 		 section named "auto" we'll get ".relauto" which is
13820 		 seen to be a .rela section.  */
13821 	      elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13822 	      if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13823 		reloc_sec = NULL;
13824 	    }
13825 	}
13826 
13827       elf_section_data (sec)->sreloc = reloc_sec;
13828     }
13829 
13830   return reloc_sec;
13831 }
13832 
13833 /* Copy the ELF symbol type and other attributes for a linker script
13834    assignment from HSRC to HDEST.  Generally this should be treated as
13835    if we found a strong non-dynamic definition for HDEST (except that
13836    ld ignores multiple definition errors).  */
13837 void
_bfd_elf_copy_link_hash_symbol_type(bfd * abfd,struct bfd_link_hash_entry * hdest,struct bfd_link_hash_entry * hsrc)13838 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13839 				     struct bfd_link_hash_entry *hdest,
13840 				     struct bfd_link_hash_entry *hsrc)
13841 {
13842   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13843   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13844   Elf_Internal_Sym isym;
13845 
13846   ehdest->type = ehsrc->type;
13847   ehdest->target_internal = ehsrc->target_internal;
13848 
13849   isym.st_other = ehsrc->other;
13850   elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13851 }
13852 
13853 /* Append a RELA relocation REL to section S in BFD.  */
13854 
13855 void
elf_append_rela(bfd * abfd,asection * s,Elf_Internal_Rela * rel)13856 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13857 {
13858   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13859   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13860   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13861   bed->s->swap_reloca_out (abfd, rel, loc);
13862 }
13863 
13864 /* Append a REL relocation REL to section S in BFD.  */
13865 
13866 void
elf_append_rel(bfd * abfd,asection * s,Elf_Internal_Rela * rel)13867 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13868 {
13869   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13870   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13871   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13872   bed->s->swap_reloc_out (abfd, rel, loc);
13873 }
13874