xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/elflink.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /* ELF linking support for BFD.
2    Copyright (C) 1995-2020 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 "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
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 *
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 *
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       bh = &h->root;
122     }
123   else
124     bh = NULL;
125 
126   bed = get_elf_backend_data (abfd);
127   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
128 					 sec, 0, NULL, FALSE, bed->collect,
129 					 &bh))
130     return NULL;
131   h = (struct elf_link_hash_entry *) bh;
132   BFD_ASSERT (h != NULL);
133   h->def_regular = 1;
134   h->non_elf = 0;
135   h->root.linker_def = 1;
136   h->type = STT_OBJECT;
137   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
139 
140   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
141   return h;
142 }
143 
144 bfd_boolean
145 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
146 {
147   flagword flags;
148   asection *s;
149   struct elf_link_hash_entry *h;
150   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
151   struct elf_link_hash_table *htab = elf_hash_table (info);
152 
153   /* This function may be called more than once.  */
154   if (htab->sgot != NULL)
155     return TRUE;
156 
157   flags = bed->dynamic_sec_flags;
158 
159   s = bfd_make_section_anyway_with_flags (abfd,
160 					  (bed->rela_plts_and_copies_p
161 					   ? ".rela.got" : ".rel.got"),
162 					  (bed->dynamic_sec_flags
163 					   | SEC_READONLY));
164   if (s == NULL
165       || !bfd_set_section_alignment (s, bed->s->log_file_align))
166     return FALSE;
167   htab->srelgot = s;
168 
169   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
170   if (s == NULL
171       || !bfd_set_section_alignment (s, bed->s->log_file_align))
172     return FALSE;
173   htab->sgot = s;
174 
175   if (bed->want_got_plt)
176     {
177       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
178       if (s == NULL
179 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
180 	return FALSE;
181       htab->sgotplt = s;
182     }
183 
184   /* The first bit of the global offset table is the header.  */
185   s->size += bed->got_header_size;
186 
187   if (bed->want_got_sym)
188     {
189       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 	 (or .got.plt) section.  We don't do this in the linker script
191 	 because we don't want to define the symbol if we are not creating
192 	 a global offset table.  */
193       h = _bfd_elf_define_linkage_sym (abfd, info, s,
194 				       "_GLOBAL_OFFSET_TABLE_");
195       elf_hash_table (info)->hgot = h;
196       if (h == NULL)
197 	return FALSE;
198     }
199 
200   return TRUE;
201 }
202 
203 /* Create a strtab to hold the dynamic symbol names.  */
204 static bfd_boolean
205 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
206 {
207   struct elf_link_hash_table *hash_table;
208 
209   hash_table = elf_hash_table (info);
210   if (hash_table->dynobj == NULL)
211     {
212       /* We may not set dynobj, an input file holding linker created
213 	 dynamic sections to abfd, which may be a dynamic object with
214 	 its own dynamic sections.  We need to find a normal input file
215 	 to hold linker created sections if possible.  */
216       if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
217 	{
218 	  bfd *ibfd;
219 	  asection *s;
220 	  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
221 	    if ((ibfd->flags
222 		 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
223 		&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
224 		&& elf_object_id (ibfd) == elf_hash_table_id (hash_table)
225 		&& !((s = ibfd->sections) != NULL
226 		     && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
227 	      {
228 		abfd = ibfd;
229 		break;
230 	      }
231 	}
232       hash_table->dynobj = abfd;
233     }
234 
235   if (hash_table->dynstr == NULL)
236     {
237       hash_table->dynstr = _bfd_elf_strtab_init ();
238       if (hash_table->dynstr == NULL)
239 	return FALSE;
240     }
241   return TRUE;
242 }
243 
244 /* Create some sections which will be filled in with dynamic linking
245    information.  ABFD is an input file which requires dynamic sections
246    to be created.  The dynamic sections take up virtual memory space
247    when the final executable is run, so we need to create them before
248    addresses are assigned to the output sections.  We work out the
249    actual contents and size of these sections later.  */
250 
251 bfd_boolean
252 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
253 {
254   flagword flags;
255   asection *s;
256   const struct elf_backend_data *bed;
257   struct elf_link_hash_entry *h;
258 
259   if (! is_elf_hash_table (info->hash))
260     return FALSE;
261 
262   if (elf_hash_table (info)->dynamic_sections_created)
263     return TRUE;
264 
265   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
266     return FALSE;
267 
268   abfd = elf_hash_table (info)->dynobj;
269   bed = get_elf_backend_data (abfd);
270 
271   flags = bed->dynamic_sec_flags;
272 
273   /* A dynamically linked executable has a .interp section, but a
274      shared library does not.  */
275   if (bfd_link_executable (info) && !info->nointerp)
276     {
277       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
278 					      flags | SEC_READONLY);
279       if (s == NULL)
280 	return FALSE;
281     }
282 
283   /* Create sections to hold version informations.  These are removed
284      if they are not needed.  */
285   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
286 					  flags | SEC_READONLY);
287   if (s == NULL
288       || !bfd_set_section_alignment (s, bed->s->log_file_align))
289     return FALSE;
290 
291   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
292 					  flags | SEC_READONLY);
293   if (s == NULL
294       || !bfd_set_section_alignment (s, 1))
295     return FALSE;
296 
297   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
298 					  flags | SEC_READONLY);
299   if (s == NULL
300       || !bfd_set_section_alignment (s, bed->s->log_file_align))
301     return FALSE;
302 
303   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
304 					  flags | SEC_READONLY);
305   if (s == NULL
306       || !bfd_set_section_alignment (s, bed->s->log_file_align))
307     return FALSE;
308   elf_hash_table (info)->dynsym = s;
309 
310   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
311 					  flags | SEC_READONLY);
312   if (s == NULL)
313     return FALSE;
314 
315   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
316   if (s == NULL
317       || !bfd_set_section_alignment (s, bed->s->log_file_align))
318     return FALSE;
319 
320   /* The special symbol _DYNAMIC is always set to the start of the
321      .dynamic section.  We could set _DYNAMIC in a linker script, but we
322      only want to define it if we are, in fact, creating a .dynamic
323      section.  We don't want to define it if there is no .dynamic
324      section, since on some ELF platforms the start up code examines it
325      to decide how to initialize the process.  */
326   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
327   elf_hash_table (info)->hdynamic = h;
328   if (h == NULL)
329     return FALSE;
330 
331   if (info->emit_hash)
332     {
333       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
334 					      flags | SEC_READONLY);
335       if (s == NULL
336 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
337 	return FALSE;
338       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
339     }
340 
341   if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
342     {
343       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
344 					      flags | SEC_READONLY);
345       if (s == NULL
346 	  || !bfd_set_section_alignment (s, bed->s->log_file_align))
347 	return FALSE;
348       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
349 	 4 32-bit words followed by variable count of 64-bit words, then
350 	 variable count of 32-bit words.  */
351       if (bed->s->arch_size == 64)
352 	elf_section_data (s)->this_hdr.sh_entsize = 0;
353       else
354 	elf_section_data (s)->this_hdr.sh_entsize = 4;
355     }
356 
357   /* Let the backend create the rest of the sections.  This lets the
358      backend set the right flags.  The backend will normally create
359      the .got and .plt sections.  */
360   if (bed->elf_backend_create_dynamic_sections == NULL
361       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
362     return FALSE;
363 
364   elf_hash_table (info)->dynamic_sections_created = TRUE;
365 
366   return TRUE;
367 }
368 
369 /* Create dynamic sections when linking against a dynamic object.  */
370 
371 bfd_boolean
372 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
373 {
374   flagword flags, pltflags;
375   struct elf_link_hash_entry *h;
376   asection *s;
377   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
378   struct elf_link_hash_table *htab = elf_hash_table (info);
379 
380   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
381      .rel[a].bss sections.  */
382   flags = bed->dynamic_sec_flags;
383 
384   pltflags = flags;
385   if (bed->plt_not_loaded)
386     /* We do not clear SEC_ALLOC here because we still want the OS to
387        allocate space for the section; it's just that there's nothing
388        to read in from the object file.  */
389     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
390   else
391     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
392   if (bed->plt_readonly)
393     pltflags |= SEC_READONLY;
394 
395   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
396   if (s == NULL
397       || !bfd_set_section_alignment (s, bed->plt_alignment))
398     return FALSE;
399   htab->splt = s;
400 
401   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
402      .plt section.  */
403   if (bed->want_plt_sym)
404     {
405       h = _bfd_elf_define_linkage_sym (abfd, info, s,
406 				       "_PROCEDURE_LINKAGE_TABLE_");
407       elf_hash_table (info)->hplt = h;
408       if (h == NULL)
409 	return FALSE;
410     }
411 
412   s = bfd_make_section_anyway_with_flags (abfd,
413 					  (bed->rela_plts_and_copies_p
414 					   ? ".rela.plt" : ".rel.plt"),
415 					  flags | SEC_READONLY);
416   if (s == NULL
417       || !bfd_set_section_alignment (s, bed->s->log_file_align))
418     return FALSE;
419   htab->srelplt = s;
420 
421   if (! _bfd_elf_create_got_section (abfd, info))
422     return FALSE;
423 
424   if (bed->want_dynbss)
425     {
426       /* The .dynbss section is a place to put symbols which are defined
427 	 by dynamic objects, are referenced by regular objects, and are
428 	 not functions.  We must allocate space for them in the process
429 	 image and use a R_*_COPY reloc to tell the dynamic linker to
430 	 initialize them at run time.  The linker script puts the .dynbss
431 	 section into the .bss section of the final image.  */
432       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
433 					      SEC_ALLOC | SEC_LINKER_CREATED);
434       if (s == NULL)
435 	return FALSE;
436       htab->sdynbss = s;
437 
438       if (bed->want_dynrelro)
439 	{
440 	  /* Similarly, but for symbols that were originally in read-only
441 	     sections.  This section doesn't really need to have contents,
442 	     but make it like other .data.rel.ro sections.  */
443 	  s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
444 						  flags);
445 	  if (s == NULL)
446 	    return FALSE;
447 	  htab->sdynrelro = s;
448 	}
449 
450       /* The .rel[a].bss section holds copy relocs.  This section is not
451 	 normally needed.  We need to create it here, though, so that the
452 	 linker will map it to an output section.  We can't just create it
453 	 only if we need it, because we will not know whether we need it
454 	 until we have seen all the input files, and the first time the
455 	 main linker code calls BFD after examining all the input files
456 	 (size_dynamic_sections) the input sections have already been
457 	 mapped to the output sections.  If the section turns out not to
458 	 be needed, we can discard it later.  We will never need this
459 	 section when generating a shared object, since they do not use
460 	 copy relocs.  */
461       if (bfd_link_executable (info))
462 	{
463 	  s = bfd_make_section_anyway_with_flags (abfd,
464 						  (bed->rela_plts_and_copies_p
465 						   ? ".rela.bss" : ".rel.bss"),
466 						  flags | SEC_READONLY);
467 	  if (s == NULL
468 	      || !bfd_set_section_alignment (s, bed->s->log_file_align))
469 	    return FALSE;
470 	  htab->srelbss = s;
471 
472 	  if (bed->want_dynrelro)
473 	    {
474 	      s = (bfd_make_section_anyway_with_flags
475 		   (abfd, (bed->rela_plts_and_copies_p
476 			   ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
477 		    flags | SEC_READONLY));
478 	      if (s == NULL
479 		  || !bfd_set_section_alignment (s, bed->s->log_file_align))
480 		return FALSE;
481 	      htab->sreldynrelro = s;
482 	    }
483 	}
484     }
485 
486   return TRUE;
487 }
488 
489 /* Record a new dynamic symbol.  We record the dynamic symbols as we
490    read the input files, since we need to have a list of all of them
491    before we can determine the final sizes of the output sections.
492    Note that we may actually call this function even though we are not
493    going to output any dynamic symbols; in some cases we know that a
494    symbol should be in the dynamic symbol table, but only if there is
495    one.  */
496 
497 bfd_boolean
498 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
499 				    struct elf_link_hash_entry *h)
500 {
501   if (h->dynindx == -1)
502     {
503       struct elf_strtab_hash *dynstr;
504       char *p;
505       const char *name;
506       size_t indx;
507 
508       /* XXX: The ABI draft says the linker must turn hidden and
509 	 internal symbols into STB_LOCAL symbols when producing the
510 	 DSO. However, if ld.so honors st_other in the dynamic table,
511 	 this would not be necessary.  */
512       switch (ELF_ST_VISIBILITY (h->other))
513 	{
514 	case STV_INTERNAL:
515 	case STV_HIDDEN:
516 	  if (h->root.type != bfd_link_hash_undefined
517 	      && h->root.type != bfd_link_hash_undefweak)
518 	    {
519 	      h->forced_local = 1;
520 	      if (!elf_hash_table (info)->is_relocatable_executable)
521 		return TRUE;
522 	    }
523 
524 	default:
525 	  break;
526 	}
527 
528       h->dynindx = elf_hash_table (info)->dynsymcount;
529       ++elf_hash_table (info)->dynsymcount;
530 
531       dynstr = elf_hash_table (info)->dynstr;
532       if (dynstr == NULL)
533 	{
534 	  /* Create a strtab to hold the dynamic symbol names.  */
535 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
536 	  if (dynstr == NULL)
537 	    return FALSE;
538 	}
539 
540       /* We don't put any version information in the dynamic string
541 	 table.  */
542       name = h->root.root.string;
543       p = strchr (name, ELF_VER_CHR);
544       if (p != NULL)
545 	/* We know that the p points into writable memory.  In fact,
546 	   there are only a few symbols that have read-only names, being
547 	   those like _GLOBAL_OFFSET_TABLE_ that are created specially
548 	   by the backends.  Most symbols will have names pointing into
549 	   an ELF string table read from a file, or to objalloc memory.  */
550 	*p = 0;
551 
552       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
553 
554       if (p != NULL)
555 	*p = ELF_VER_CHR;
556 
557       if (indx == (size_t) -1)
558 	return FALSE;
559       h->dynstr_index = indx;
560     }
561 
562   return TRUE;
563 }
564 
565 /* Mark a symbol dynamic.  */
566 
567 static void
568 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
569 				  struct elf_link_hash_entry *h,
570 				  Elf_Internal_Sym *sym)
571 {
572   struct bfd_elf_dynamic_list *d = info->dynamic_list;
573 
574   /* It may be called more than once on the same H.  */
575   if(h->dynamic || bfd_link_relocatable (info))
576     return;
577 
578   if ((info->dynamic_data
579        && (h->type == STT_OBJECT
580 	   || h->type == STT_COMMON
581 	   || (sym != NULL
582 	       && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
583 		   || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
584       || (d != NULL
585 	  && h->non_elf
586 	  && (*d->match) (&d->head, NULL, h->root.root.string)))
587     {
588       h->dynamic = 1;
589       /* NB: If a symbol is made dynamic by --dynamic-list, it has
590 	 non-IR reference.  */
591       h->root.non_ir_ref_dynamic = 1;
592     }
593 }
594 
595 /* Record an assignment to a symbol made by a linker script.  We need
596    this in case some dynamic object refers to this symbol.  */
597 
598 bfd_boolean
599 bfd_elf_record_link_assignment (bfd *output_bfd,
600 				struct bfd_link_info *info,
601 				const char *name,
602 				bfd_boolean provide,
603 				bfd_boolean hidden)
604 {
605   struct elf_link_hash_entry *h, *hv;
606   struct elf_link_hash_table *htab;
607   const struct elf_backend_data *bed;
608 
609   if (!is_elf_hash_table (info->hash))
610     return TRUE;
611 
612   htab = elf_hash_table (info);
613   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
614   if (h == NULL)
615     return provide;
616 
617   if (h->root.type == bfd_link_hash_warning)
618     h = (struct elf_link_hash_entry *) h->root.u.i.link;
619 
620   if (h->versioned == unknown)
621     {
622       /* Set versioned if symbol version is unknown.  */
623       char *version = strrchr (name, ELF_VER_CHR);
624       if (version)
625 	{
626 	  if (version > name && version[-1] != ELF_VER_CHR)
627 	    h->versioned = versioned_hidden;
628 	  else
629 	    h->versioned = versioned;
630 	}
631     }
632 
633   /* Symbols defined in a linker script but not referenced anywhere
634      else will have non_elf set.  */
635   if (h->non_elf)
636     {
637       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
638       h->non_elf = 0;
639     }
640 
641   switch (h->root.type)
642     {
643     case bfd_link_hash_defined:
644     case bfd_link_hash_defweak:
645     case bfd_link_hash_common:
646       break;
647     case bfd_link_hash_undefweak:
648     case bfd_link_hash_undefined:
649       /* Since we're defining the symbol, don't let it seem to have not
650 	 been defined.  record_dynamic_symbol and size_dynamic_sections
651 	 may depend on this.  */
652       h->root.type = bfd_link_hash_new;
653       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
654 	bfd_link_repair_undef_list (&htab->root);
655       break;
656     case bfd_link_hash_new:
657       break;
658     case bfd_link_hash_indirect:
659       /* We had a versioned symbol in a dynamic library.  We make the
660 	 the versioned symbol point to this one.  */
661       bed = get_elf_backend_data (output_bfd);
662       hv = h;
663       while (hv->root.type == bfd_link_hash_indirect
664 	     || hv->root.type == bfd_link_hash_warning)
665 	hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
666       /* We don't need to update h->root.u since linker will set them
667 	 later.  */
668       h->root.type = bfd_link_hash_undefined;
669       hv->root.type = bfd_link_hash_indirect;
670       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
671       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
672       break;
673     default:
674       BFD_FAIL ();
675       return FALSE;
676     }
677 
678   /* If this symbol is being provided by the linker script, and it is
679      currently defined by a dynamic object, but not by a regular
680      object, then mark it as undefined so that the generic linker will
681      force the correct value.  */
682   if (provide
683       && h->def_dynamic
684       && !h->def_regular)
685     h->root.type = bfd_link_hash_undefined;
686 
687   /* If this symbol is currently defined by a dynamic object, but not
688      by a regular object, then clear out any version information because
689      the symbol will not be associated with the dynamic object any
690      more.  */
691   if (h->def_dynamic && !h->def_regular)
692     h->verinfo.verdef = NULL;
693 
694   /* Make sure this symbol is not garbage collected.  */
695   h->mark = 1;
696 
697   h->def_regular = 1;
698 
699   if (hidden)
700     {
701       bed = get_elf_backend_data (output_bfd);
702       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
703 	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
704       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
705     }
706 
707   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
708      and executables.  */
709   if (!bfd_link_relocatable (info)
710       && h->dynindx != -1
711       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
712 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
713     h->forced_local = 1;
714 
715   if ((h->def_dynamic
716        || h->ref_dynamic
717        || bfd_link_dll (info)
718        || elf_hash_table (info)->is_relocatable_executable)
719       && !h->forced_local
720       && h->dynindx == -1)
721     {
722       if (! bfd_elf_link_record_dynamic_symbol (info, h))
723 	return FALSE;
724 
725       /* If this is a weak defined symbol, and we know a corresponding
726 	 real symbol from the same dynamic object, make sure the real
727 	 symbol is also made into a dynamic symbol.  */
728       if (h->is_weakalias)
729 	{
730 	  struct elf_link_hash_entry *def = weakdef (h);
731 
732 	  if (def->dynindx == -1
733 	      && !bfd_elf_link_record_dynamic_symbol (info, def))
734 	    return FALSE;
735 	}
736     }
737 
738   return TRUE;
739 }
740 
741 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
742    success, and 2 on a failure caused by attempting to record a symbol
743    in a discarded section, eg. a discarded link-once section symbol.  */
744 
745 int
746 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
747 					  bfd *input_bfd,
748 					  long input_indx)
749 {
750   bfd_size_type amt;
751   struct elf_link_local_dynamic_entry *entry;
752   struct elf_link_hash_table *eht;
753   struct elf_strtab_hash *dynstr;
754   size_t dynstr_index;
755   char *name;
756   Elf_External_Sym_Shndx eshndx;
757   char esym[sizeof (Elf64_External_Sym)];
758 
759   if (! is_elf_hash_table (info->hash))
760     return 0;
761 
762   /* See if the entry exists already.  */
763   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
764     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
765       return 1;
766 
767   amt = sizeof (*entry);
768   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
769   if (entry == NULL)
770     return 0;
771 
772   /* Go find the symbol, so that we can find it's name.  */
773   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
774 			     1, input_indx, &entry->isym, esym, &eshndx))
775     {
776       bfd_release (input_bfd, entry);
777       return 0;
778     }
779 
780   if (entry->isym.st_shndx != SHN_UNDEF
781       && entry->isym.st_shndx < SHN_LORESERVE)
782     {
783       asection *s;
784 
785       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
786       if (s == NULL || bfd_is_abs_section (s->output_section))
787 	{
788 	  /* We can still bfd_release here as nothing has done another
789 	     bfd_alloc.  We can't do this later in this function.  */
790 	  bfd_release (input_bfd, entry);
791 	  return 2;
792 	}
793     }
794 
795   name = (bfd_elf_string_from_elf_section
796 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
797 	   entry->isym.st_name));
798 
799   dynstr = elf_hash_table (info)->dynstr;
800   if (dynstr == NULL)
801     {
802       /* Create a strtab to hold the dynamic symbol names.  */
803       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
804       if (dynstr == NULL)
805 	return 0;
806     }
807 
808   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
809   if (dynstr_index == (size_t) -1)
810     return 0;
811   entry->isym.st_name = dynstr_index;
812 
813   eht = elf_hash_table (info);
814 
815   entry->next = eht->dynlocal;
816   eht->dynlocal = entry;
817   entry->input_bfd = input_bfd;
818   entry->input_indx = input_indx;
819   eht->dynsymcount++;
820 
821   /* Whatever binding the symbol had before, it's now local.  */
822   entry->isym.st_info
823     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
824 
825   /* The dynindx will be set at the end of size_dynamic_sections.  */
826 
827   return 1;
828 }
829 
830 /* Return the dynindex of a local dynamic symbol.  */
831 
832 long
833 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
834 				    bfd *input_bfd,
835 				    long input_indx)
836 {
837   struct elf_link_local_dynamic_entry *e;
838 
839   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
840     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
841       return e->dynindx;
842   return -1;
843 }
844 
845 /* This function is used to renumber the dynamic symbols, if some of
846    them are removed because they are marked as local.  This is called
847    via elf_link_hash_traverse.  */
848 
849 static bfd_boolean
850 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
851 				      void *data)
852 {
853   size_t *count = (size_t *) data;
854 
855   if (h->forced_local)
856     return TRUE;
857 
858   if (h->dynindx != -1)
859     h->dynindx = ++(*count);
860 
861   return TRUE;
862 }
863 
864 
865 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
866    STB_LOCAL binding.  */
867 
868 static bfd_boolean
869 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
870 					    void *data)
871 {
872   size_t *count = (size_t *) data;
873 
874   if (!h->forced_local)
875     return TRUE;
876 
877   if (h->dynindx != -1)
878     h->dynindx = ++(*count);
879 
880   return TRUE;
881 }
882 
883 /* Return true if the dynamic symbol for a given section should be
884    omitted when creating a shared library.  */
885 bfd_boolean
886 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
887 				      struct bfd_link_info *info,
888 				      asection *p)
889 {
890   struct elf_link_hash_table *htab;
891   asection *ip;
892 
893   switch (elf_section_data (p)->this_hdr.sh_type)
894     {
895     case SHT_PROGBITS:
896     case SHT_NOBITS:
897       /* If sh_type is yet undecided, assume it could be
898 	 SHT_PROGBITS/SHT_NOBITS.  */
899     case SHT_NULL:
900       htab = elf_hash_table (info);
901       if (htab->text_index_section != NULL)
902 	return p != htab->text_index_section && p != htab->data_index_section;
903 
904       return (htab->dynobj != NULL
905 	      && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
906 	      && ip->output_section == p);
907 
908       /* There shouldn't be section relative relocations
909 	 against any other section.  */
910     default:
911       return TRUE;
912     }
913 }
914 
915 bfd_boolean
916 _bfd_elf_omit_section_dynsym_all
917     (bfd *output_bfd ATTRIBUTE_UNUSED,
918      struct bfd_link_info *info ATTRIBUTE_UNUSED,
919      asection *p ATTRIBUTE_UNUSED)
920 {
921   return TRUE;
922 }
923 
924 /* Assign dynsym indices.  In a shared library we generate a section
925    symbol for each output section, which come first.  Next come symbols
926    which have been forced to local binding.  Then all of the back-end
927    allocated local dynamic syms, followed by the rest of the global
928    symbols.  If SECTION_SYM_COUNT is NULL, section dynindx is not set.
929    (This prevents the early call before elf_backend_init_index_section
930    and strip_excluded_output_sections setting dynindx for sections
931    that are stripped.)  */
932 
933 static unsigned long
934 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
935 				struct bfd_link_info *info,
936 				unsigned long *section_sym_count)
937 {
938   unsigned long dynsymcount = 0;
939   bfd_boolean do_sec = section_sym_count != NULL;
940 
941   if (bfd_link_pic (info)
942       || elf_hash_table (info)->is_relocatable_executable)
943     {
944       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
945       asection *p;
946       for (p = output_bfd->sections; p ; p = p->next)
947 	if ((p->flags & SEC_EXCLUDE) == 0
948 	    && (p->flags & SEC_ALLOC) != 0
949 	    && elf_hash_table (info)->dynamic_relocs
950 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
951 	  {
952 	    ++dynsymcount;
953 	    if (do_sec)
954 	      elf_section_data (p)->dynindx = dynsymcount;
955 	  }
956 	else if (do_sec)
957 	  elf_section_data (p)->dynindx = 0;
958     }
959   if (do_sec)
960     *section_sym_count = dynsymcount;
961 
962   elf_link_hash_traverse (elf_hash_table (info),
963 			  elf_link_renumber_local_hash_table_dynsyms,
964 			  &dynsymcount);
965 
966   if (elf_hash_table (info)->dynlocal)
967     {
968       struct elf_link_local_dynamic_entry *p;
969       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
970 	p->dynindx = ++dynsymcount;
971     }
972   elf_hash_table (info)->local_dynsymcount = dynsymcount;
973 
974   elf_link_hash_traverse (elf_hash_table (info),
975 			  elf_link_renumber_hash_table_dynsyms,
976 			  &dynsymcount);
977 
978   /* There is an unused NULL entry at the head of the table which we
979      must account for in our count even if the table is empty since it
980      is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
981      .dynamic section.  */
982   dynsymcount++;
983 
984   elf_hash_table (info)->dynsymcount = dynsymcount;
985   return dynsymcount;
986 }
987 
988 /* Merge st_other field.  */
989 
990 static void
991 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
992 		    const Elf_Internal_Sym *isym, asection *sec,
993 		    bfd_boolean definition, bfd_boolean dynamic)
994 {
995   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
996 
997   /* If st_other has a processor-specific meaning, specific
998      code might be needed here.  */
999   if (bed->elf_backend_merge_symbol_attribute)
1000     (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1001 						dynamic);
1002 
1003   if (!dynamic)
1004     {
1005       unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1006       unsigned hvis = ELF_ST_VISIBILITY (h->other);
1007 
1008       /* Keep the most constraining visibility.  Leave the remainder
1009 	 of the st_other field to elf_backend_merge_symbol_attribute.  */
1010       if (symvis - 1 < hvis - 1)
1011 	h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1012     }
1013   else if (definition
1014 	   && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1015 	   && (sec->flags & SEC_READONLY) == 0)
1016     h->protected_def = 1;
1017 }
1018 
1019 /* This function is called when we want to merge a new symbol with an
1020    existing symbol.  It handles the various cases which arise when we
1021    find a definition in a dynamic object, or when there is already a
1022    definition in a dynamic object.  The new symbol is described by
1023    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
1024    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
1025    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
1026    of an old common symbol.  We set OVERRIDE if the old symbol is
1027    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
1028    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
1029    to change.  By OK to change, we mean that we shouldn't warn if the
1030    type or size does change.  */
1031 
1032 static bfd_boolean
1033 _bfd_elf_merge_symbol (bfd *abfd,
1034 		       struct bfd_link_info *info,
1035 		       const char *name,
1036 		       Elf_Internal_Sym *sym,
1037 		       asection **psec,
1038 		       bfd_vma *pvalue,
1039 		       struct elf_link_hash_entry **sym_hash,
1040 		       bfd **poldbfd,
1041 		       bfd_boolean *pold_weak,
1042 		       unsigned int *pold_alignment,
1043 		       bfd_boolean *skip,
1044 		       bfd_boolean *override,
1045 		       bfd_boolean *type_change_ok,
1046 		       bfd_boolean *size_change_ok,
1047 		       bfd_boolean *matched)
1048 {
1049   asection *sec, *oldsec;
1050   struct elf_link_hash_entry *h;
1051   struct elf_link_hash_entry *hi;
1052   struct elf_link_hash_entry *flip;
1053   int bind;
1054   bfd *oldbfd;
1055   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1056   bfd_boolean newweak, oldweak, newfunc, oldfunc;
1057   const struct elf_backend_data *bed;
1058   char *new_version;
1059   bfd_boolean default_sym = *matched;
1060 
1061   *skip = FALSE;
1062   *override = FALSE;
1063 
1064   sec = *psec;
1065   bind = ELF_ST_BIND (sym->st_info);
1066 
1067   if (! bfd_is_und_section (sec))
1068     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1069   else
1070     h = ((struct elf_link_hash_entry *)
1071 	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1072   if (h == NULL)
1073     return FALSE;
1074   *sym_hash = h;
1075 
1076   bed = get_elf_backend_data (abfd);
1077 
1078   /* NEW_VERSION is the symbol version of the new symbol.  */
1079   if (h->versioned != unversioned)
1080     {
1081       /* Symbol version is unknown or versioned.  */
1082       new_version = strrchr (name, ELF_VER_CHR);
1083       if (new_version)
1084 	{
1085 	  if (h->versioned == unknown)
1086 	    {
1087 	      if (new_version > name && new_version[-1] != ELF_VER_CHR)
1088 		h->versioned = versioned_hidden;
1089 	      else
1090 		h->versioned = versioned;
1091 	    }
1092 	  new_version += 1;
1093 	  if (new_version[0] == '\0')
1094 	    new_version = NULL;
1095 	}
1096       else
1097 	h->versioned = unversioned;
1098     }
1099   else
1100     new_version = NULL;
1101 
1102   /* For merging, we only care about real symbols.  But we need to make
1103      sure that indirect symbol dynamic flags are updated.  */
1104   hi = h;
1105   while (h->root.type == bfd_link_hash_indirect
1106 	 || h->root.type == bfd_link_hash_warning)
1107     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1108 
1109   if (!*matched)
1110     {
1111       if (hi == h || h->root.type == bfd_link_hash_new)
1112 	*matched = TRUE;
1113       else
1114 	{
1115 	  /* OLD_HIDDEN is true if the existing symbol is only visible
1116 	     to the symbol with the same symbol version.  NEW_HIDDEN is
1117 	     true if the new symbol is only visible to the symbol with
1118 	     the same symbol version.  */
1119 	  bfd_boolean old_hidden = h->versioned == versioned_hidden;
1120 	  bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1121 	  if (!old_hidden && !new_hidden)
1122 	    /* The new symbol matches the existing symbol if both
1123 	       aren't hidden.  */
1124 	    *matched = TRUE;
1125 	  else
1126 	    {
1127 	      /* OLD_VERSION is the symbol version of the existing
1128 		 symbol. */
1129 	      char *old_version;
1130 
1131 	      if (h->versioned >= versioned)
1132 		old_version = strrchr (h->root.root.string,
1133 				       ELF_VER_CHR) + 1;
1134 	      else
1135 		 old_version = NULL;
1136 
1137 	      /* The new symbol matches the existing symbol if they
1138 		 have the same symbol version.  */
1139 	      *matched = (old_version == new_version
1140 			  || (old_version != NULL
1141 			      && new_version != NULL
1142 			      && strcmp (old_version, new_version) == 0));
1143 	    }
1144 	}
1145     }
1146 
1147   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1148      existing symbol.  */
1149 
1150   oldbfd = NULL;
1151   oldsec = NULL;
1152   switch (h->root.type)
1153     {
1154     default:
1155       break;
1156 
1157     case bfd_link_hash_undefined:
1158     case bfd_link_hash_undefweak:
1159       oldbfd = h->root.u.undef.abfd;
1160       break;
1161 
1162     case bfd_link_hash_defined:
1163     case bfd_link_hash_defweak:
1164       oldbfd = h->root.u.def.section->owner;
1165       oldsec = h->root.u.def.section;
1166       break;
1167 
1168     case bfd_link_hash_common:
1169       oldbfd = h->root.u.c.p->section->owner;
1170       oldsec = h->root.u.c.p->section;
1171       if (pold_alignment)
1172 	*pold_alignment = h->root.u.c.p->alignment_power;
1173       break;
1174     }
1175   if (poldbfd && *poldbfd == NULL)
1176     *poldbfd = oldbfd;
1177 
1178   /* Differentiate strong and weak symbols.  */
1179   newweak = bind == STB_WEAK;
1180   oldweak = (h->root.type == bfd_link_hash_defweak
1181 	     || h->root.type == bfd_link_hash_undefweak);
1182   if (pold_weak)
1183     *pold_weak = oldweak;
1184 
1185   /* We have to check it for every instance since the first few may be
1186      references and not all compilers emit symbol type for undefined
1187      symbols.  */
1188   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1189 
1190   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1191      respectively, is from a dynamic object.  */
1192 
1193   newdyn = (abfd->flags & DYNAMIC) != 0;
1194 
1195   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1196      syms and defined syms in dynamic libraries respectively.
1197      ref_dynamic on the other hand can be set for a symbol defined in
1198      a dynamic library, and def_dynamic may not be set;  When the
1199      definition in a dynamic lib is overridden by a definition in the
1200      executable use of the symbol in the dynamic lib becomes a
1201      reference to the executable symbol.  */
1202   if (newdyn)
1203     {
1204       if (bfd_is_und_section (sec))
1205 	{
1206 	  if (bind != STB_WEAK)
1207 	    {
1208 	      h->ref_dynamic_nonweak = 1;
1209 	      hi->ref_dynamic_nonweak = 1;
1210 	    }
1211 	}
1212       else
1213 	{
1214 	  /* Update the existing symbol only if they match. */
1215 	  if (*matched)
1216 	    h->dynamic_def = 1;
1217 	  hi->dynamic_def = 1;
1218 	}
1219     }
1220 
1221   /* If we just created the symbol, mark it as being an ELF symbol.
1222      Other than that, there is nothing to do--there is no merge issue
1223      with a newly defined symbol--so we just return.  */
1224 
1225   if (h->root.type == bfd_link_hash_new)
1226     {
1227       h->non_elf = 0;
1228       return TRUE;
1229     }
1230 
1231   /* In cases involving weak versioned symbols, we may wind up trying
1232      to merge a symbol with itself.  Catch that here, to avoid the
1233      confusion that results if we try to override a symbol with
1234      itself.  The additional tests catch cases like
1235      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1236      dynamic object, which we do want to handle here.  */
1237   if (abfd == oldbfd
1238       && (newweak || oldweak)
1239       && ((abfd->flags & DYNAMIC) == 0
1240 	  || !h->def_regular))
1241     return TRUE;
1242 
1243   olddyn = FALSE;
1244   if (oldbfd != NULL)
1245     olddyn = (oldbfd->flags & DYNAMIC) != 0;
1246   else if (oldsec != NULL)
1247     {
1248       /* This handles the special SHN_MIPS_{TEXT,DATA} section
1249 	 indices used by MIPS ELF.  */
1250       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1251     }
1252 
1253   /* Handle a case where plugin_notice won't be called and thus won't
1254      set the non_ir_ref flags on the first pass over symbols.  */
1255   if (oldbfd != NULL
1256       && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1257       && newdyn != olddyn)
1258     {
1259       h->root.non_ir_ref_dynamic = TRUE;
1260       hi->root.non_ir_ref_dynamic = TRUE;
1261     }
1262 
1263   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1264      respectively, appear to be a definition rather than reference.  */
1265 
1266   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1267 
1268   olddef = (h->root.type != bfd_link_hash_undefined
1269 	    && h->root.type != bfd_link_hash_undefweak
1270 	    && h->root.type != bfd_link_hash_common);
1271 
1272   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1273      respectively, appear to be a function.  */
1274 
1275   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1276 	     && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1277 
1278   oldfunc = (h->type != STT_NOTYPE
1279 	     && bed->is_function_type (h->type));
1280 
1281   if (!(newfunc && oldfunc)
1282       && ELF_ST_TYPE (sym->st_info) != h->type
1283       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1284       && h->type != STT_NOTYPE
1285       && (newdef || bfd_is_com_section (sec))
1286       && (olddef || h->root.type == bfd_link_hash_common))
1287     {
1288       /* If creating a default indirect symbol ("foo" or "foo@") from
1289 	 a dynamic versioned definition ("foo@@") skip doing so if
1290 	 there is an existing regular definition with a different
1291 	 type.  We don't want, for example, a "time" variable in the
1292 	 executable overriding a "time" function in a shared library.  */
1293       if (newdyn
1294 	  && !olddyn)
1295 	{
1296 	  *skip = TRUE;
1297 	  return TRUE;
1298 	}
1299 
1300       /* When adding a symbol from a regular object file after we have
1301 	 created indirect symbols, undo the indirection and any
1302 	 dynamic state.  */
1303       if (hi != h
1304 	  && !newdyn
1305 	  && olddyn)
1306 	{
1307 	  h = hi;
1308 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 	  h->forced_local = 0;
1310 	  h->ref_dynamic = 0;
1311 	  h->def_dynamic = 0;
1312 	  h->dynamic_def = 0;
1313 	  if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1314 	    {
1315 	      h->root.type = bfd_link_hash_undefined;
1316 	      h->root.u.undef.abfd = abfd;
1317 	    }
1318 	  else
1319 	    {
1320 	      h->root.type = bfd_link_hash_new;
1321 	      h->root.u.undef.abfd = NULL;
1322 	    }
1323 	  return TRUE;
1324 	}
1325     }
1326 
1327   /* Check TLS symbols.  We don't check undefined symbols introduced
1328      by "ld -u" which have no type (and oldbfd NULL), and we don't
1329      check symbols from plugins because they also have no type.  */
1330   if (oldbfd != NULL
1331       && (oldbfd->flags & BFD_PLUGIN) == 0
1332       && (abfd->flags & BFD_PLUGIN) == 0
1333       && ELF_ST_TYPE (sym->st_info) != h->type
1334       && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1335     {
1336       bfd *ntbfd, *tbfd;
1337       bfd_boolean ntdef, tdef;
1338       asection *ntsec, *tsec;
1339 
1340       if (h->type == STT_TLS)
1341 	{
1342 	  ntbfd = abfd;
1343 	  ntsec = sec;
1344 	  ntdef = newdef;
1345 	  tbfd = oldbfd;
1346 	  tsec = oldsec;
1347 	  tdef = olddef;
1348 	}
1349       else
1350 	{
1351 	  ntbfd = oldbfd;
1352 	  ntsec = oldsec;
1353 	  ntdef = olddef;
1354 	  tbfd = abfd;
1355 	  tsec = sec;
1356 	  tdef = newdef;
1357 	}
1358 
1359       if (tdef && ntdef)
1360 	_bfd_error_handler
1361 	  /* xgettext:c-format */
1362 	  (_("%s: TLS definition in %pB section %pA "
1363 	     "mismatches non-TLS definition in %pB section %pA"),
1364 	   h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1365       else if (!tdef && !ntdef)
1366 	_bfd_error_handler
1367 	  /* xgettext:c-format */
1368 	  (_("%s: TLS reference in %pB "
1369 	     "mismatches non-TLS reference in %pB"),
1370 	   h->root.root.string, tbfd, ntbfd);
1371       else if (tdef)
1372 	_bfd_error_handler
1373 	  /* xgettext:c-format */
1374 	  (_("%s: TLS definition in %pB section %pA "
1375 	     "mismatches non-TLS reference in %pB"),
1376 	   h->root.root.string, tbfd, tsec, ntbfd);
1377       else
1378 	_bfd_error_handler
1379 	  /* xgettext:c-format */
1380 	  (_("%s: TLS reference in %pB "
1381 	     "mismatches non-TLS definition in %pB section %pA"),
1382 	   h->root.root.string, tbfd, ntbfd, ntsec);
1383 
1384       bfd_set_error (bfd_error_bad_value);
1385       return FALSE;
1386     }
1387 
1388   /* If the old symbol has non-default visibility, we ignore the new
1389      definition from a dynamic object.  */
1390   if (newdyn
1391       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1392       && !bfd_is_und_section (sec))
1393     {
1394       *skip = TRUE;
1395       /* Make sure this symbol is dynamic.  */
1396       h->ref_dynamic = 1;
1397       hi->ref_dynamic = 1;
1398       /* A protected symbol has external availability. Make sure it is
1399 	 recorded as dynamic.
1400 
1401 	 FIXME: Should we check type and size for protected symbol?  */
1402       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1403 	return bfd_elf_link_record_dynamic_symbol (info, h);
1404       else
1405 	return TRUE;
1406     }
1407   else if (!newdyn
1408 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1409 	   && h->def_dynamic)
1410     {
1411       /* If the new symbol with non-default visibility comes from a
1412 	 relocatable file and the old definition comes from a dynamic
1413 	 object, we remove the old definition.  */
1414       if (hi->root.type == bfd_link_hash_indirect)
1415 	{
1416 	  /* Handle the case where the old dynamic definition is
1417 	     default versioned.  We need to copy the symbol info from
1418 	     the symbol with default version to the normal one if it
1419 	     was referenced before.  */
1420 	  if (h->ref_regular)
1421 	    {
1422 	      hi->root.type = h->root.type;
1423 	      h->root.type = bfd_link_hash_indirect;
1424 	      (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1425 
1426 	      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1427 	      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1428 		{
1429 		  /* If the new symbol is hidden or internal, completely undo
1430 		     any dynamic link state.  */
1431 		  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1432 		  h->forced_local = 0;
1433 		  h->ref_dynamic = 0;
1434 		}
1435 	      else
1436 		h->ref_dynamic = 1;
1437 
1438 	      h->def_dynamic = 0;
1439 	      /* FIXME: Should we check type and size for protected symbol?  */
1440 	      h->size = 0;
1441 	      h->type = 0;
1442 
1443 	      h = hi;
1444 	    }
1445 	  else
1446 	    h = hi;
1447 	}
1448 
1449       /* If the old symbol was undefined before, then it will still be
1450 	 on the undefs list.  If the new symbol is undefined or
1451 	 common, we can't make it bfd_link_hash_new here, because new
1452 	 undefined or common symbols will be added to the undefs list
1453 	 by _bfd_generic_link_add_one_symbol.  Symbols may not be
1454 	 added twice to the undefs list.  Also, if the new symbol is
1455 	 undefweak then we don't want to lose the strong undef.  */
1456       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1457 	{
1458 	  h->root.type = bfd_link_hash_undefined;
1459 	  h->root.u.undef.abfd = abfd;
1460 	}
1461       else
1462 	{
1463 	  h->root.type = bfd_link_hash_new;
1464 	  h->root.u.undef.abfd = NULL;
1465 	}
1466 
1467       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1468 	{
1469 	  /* If the new symbol is hidden or internal, completely undo
1470 	     any dynamic link state.  */
1471 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1472 	  h->forced_local = 0;
1473 	  h->ref_dynamic = 0;
1474 	}
1475       else
1476 	h->ref_dynamic = 1;
1477       h->def_dynamic = 0;
1478       /* FIXME: Should we check type and size for protected symbol?  */
1479       h->size = 0;
1480       h->type = 0;
1481       return TRUE;
1482     }
1483 
1484   /* If a new weak symbol definition comes from a regular file and the
1485      old symbol comes from a dynamic library, we treat the new one as
1486      strong.  Similarly, an old weak symbol definition from a regular
1487      file is treated as strong when the new symbol comes from a dynamic
1488      library.  Further, an old weak symbol from a dynamic library is
1489      treated as strong if the new symbol is from a dynamic library.
1490      This reflects the way glibc's ld.so works.
1491 
1492      Also allow a weak symbol to override a linker script symbol
1493      defined by an early pass over the script.  This is done so the
1494      linker knows the symbol is defined in an object file, for the
1495      DEFINED script function.
1496 
1497      Do this before setting *type_change_ok or *size_change_ok so that
1498      we warn properly when dynamic library symbols are overridden.  */
1499 
1500   if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1501     newweak = FALSE;
1502   if (olddef && newdyn)
1503     oldweak = FALSE;
1504 
1505   /* Allow changes between different types of function symbol.  */
1506   if (newfunc && oldfunc)
1507     *type_change_ok = TRUE;
1508 
1509   /* It's OK to change the type if either the existing symbol or the
1510      new symbol is weak.  A type change is also OK if the old symbol
1511      is undefined and the new symbol is defined.  */
1512 
1513   if (oldweak
1514       || newweak
1515       || (newdef
1516 	  && h->root.type == bfd_link_hash_undefined))
1517     *type_change_ok = TRUE;
1518 
1519   /* It's OK to change the size if either the existing symbol or the
1520      new symbol is weak, or if the old symbol is undefined.  */
1521 
1522   if (*type_change_ok
1523       || h->root.type == bfd_link_hash_undefined)
1524     *size_change_ok = TRUE;
1525 
1526   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1527      symbol, respectively, appears to be a common symbol in a dynamic
1528      object.  If a symbol appears in an uninitialized section, and is
1529      not weak, and is not a function, then it may be a common symbol
1530      which was resolved when the dynamic object was created.  We want
1531      to treat such symbols specially, because they raise special
1532      considerations when setting the symbol size: if the symbol
1533      appears as a common symbol in a regular object, and the size in
1534      the regular object is larger, we must make sure that we use the
1535      larger size.  This problematic case can always be avoided in C,
1536      but it must be handled correctly when using Fortran shared
1537      libraries.
1538 
1539      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1540      likewise for OLDDYNCOMMON and OLDDEF.
1541 
1542      Note that this test is just a heuristic, and that it is quite
1543      possible to have an uninitialized symbol in a shared object which
1544      is really a definition, rather than a common symbol.  This could
1545      lead to some minor confusion when the symbol really is a common
1546      symbol in some regular object.  However, I think it will be
1547      harmless.  */
1548 
1549   if (newdyn
1550       && newdef
1551       && !newweak
1552       && (sec->flags & SEC_ALLOC) != 0
1553       && (sec->flags & SEC_LOAD) == 0
1554       && sym->st_size > 0
1555       && !newfunc)
1556     newdyncommon = TRUE;
1557   else
1558     newdyncommon = FALSE;
1559 
1560   if (olddyn
1561       && olddef
1562       && h->root.type == bfd_link_hash_defined
1563       && h->def_dynamic
1564       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1565       && (h->root.u.def.section->flags & SEC_LOAD) == 0
1566       && h->size > 0
1567       && !oldfunc)
1568     olddyncommon = TRUE;
1569   else
1570     olddyncommon = FALSE;
1571 
1572   /* We now know everything about the old and new symbols.  We ask the
1573      backend to check if we can merge them.  */
1574   if (bed->merge_symbol != NULL)
1575     {
1576       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1577 	return FALSE;
1578       sec = *psec;
1579     }
1580 
1581   /* There are multiple definitions of a normal symbol.  Skip the
1582      default symbol as well as definition from an IR object.  */
1583   if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1584       && !default_sym && h->def_regular
1585       && !(oldbfd != NULL
1586 	   && (oldbfd->flags & BFD_PLUGIN) != 0
1587 	   && (abfd->flags & BFD_PLUGIN) == 0))
1588     {
1589       /* Handle a multiple definition.  */
1590       (*info->callbacks->multiple_definition) (info, &h->root,
1591 					       abfd, sec, *pvalue);
1592       *skip = TRUE;
1593       return TRUE;
1594     }
1595 
1596   /* If both the old and the new symbols look like common symbols in a
1597      dynamic object, set the size of the symbol to the larger of the
1598      two.  */
1599 
1600   if (olddyncommon
1601       && newdyncommon
1602       && sym->st_size != h->size)
1603     {
1604       /* Since we think we have two common symbols, issue a multiple
1605 	 common warning if desired.  Note that we only warn if the
1606 	 size is different.  If the size is the same, we simply let
1607 	 the old symbol override the new one as normally happens with
1608 	 symbols defined in dynamic objects.  */
1609 
1610       (*info->callbacks->multiple_common) (info, &h->root, abfd,
1611 					   bfd_link_hash_common, sym->st_size);
1612       if (sym->st_size > h->size)
1613 	h->size = sym->st_size;
1614 
1615       *size_change_ok = TRUE;
1616     }
1617 
1618   /* If we are looking at a dynamic object, and we have found a
1619      definition, we need to see if the symbol was already defined by
1620      some other object.  If so, we want to use the existing
1621      definition, and we do not want to report a multiple symbol
1622      definition error; we do this by clobbering *PSEC to be
1623      bfd_und_section_ptr.
1624 
1625      We treat a common symbol as a definition if the symbol in the
1626      shared library is a function, since common symbols always
1627      represent variables; this can cause confusion in principle, but
1628      any such confusion would seem to indicate an erroneous program or
1629      shared library.  We also permit a common symbol in a regular
1630      object to override a weak symbol in a shared object.  */
1631 
1632   if (newdyn
1633       && newdef
1634       && (olddef
1635 	  || (h->root.type == bfd_link_hash_common
1636 	      && (newweak || newfunc))))
1637     {
1638       *override = TRUE;
1639       newdef = FALSE;
1640       newdyncommon = FALSE;
1641 
1642       *psec = sec = bfd_und_section_ptr;
1643       *size_change_ok = TRUE;
1644 
1645       /* If we get here when the old symbol is a common symbol, then
1646 	 we are explicitly letting it override a weak symbol or
1647 	 function in a dynamic object, and we don't want to warn about
1648 	 a type change.  If the old symbol is a defined symbol, a type
1649 	 change warning may still be appropriate.  */
1650 
1651       if (h->root.type == bfd_link_hash_common)
1652 	*type_change_ok = TRUE;
1653     }
1654 
1655   /* Handle the special case of an old common symbol merging with a
1656      new symbol which looks like a common symbol in a shared object.
1657      We change *PSEC and *PVALUE to make the new symbol look like a
1658      common symbol, and let _bfd_generic_link_add_one_symbol do the
1659      right thing.  */
1660 
1661   if (newdyncommon
1662       && h->root.type == bfd_link_hash_common)
1663     {
1664       *override = TRUE;
1665       newdef = FALSE;
1666       newdyncommon = FALSE;
1667       *pvalue = sym->st_size;
1668       *psec = sec = bed->common_section (oldsec);
1669       *size_change_ok = TRUE;
1670     }
1671 
1672   /* Skip weak definitions of symbols that are already defined.  */
1673   if (newdef && olddef && newweak)
1674     {
1675       /* Don't skip new non-IR weak syms.  */
1676       if (!(oldbfd != NULL
1677 	    && (oldbfd->flags & BFD_PLUGIN) != 0
1678 	    && (abfd->flags & BFD_PLUGIN) == 0))
1679 	{
1680 	  newdef = FALSE;
1681 	  *skip = TRUE;
1682 	}
1683 
1684       /* Merge st_other.  If the symbol already has a dynamic index,
1685 	 but visibility says it should not be visible, turn it into a
1686 	 local symbol.  */
1687       elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1688       if (h->dynindx != -1)
1689 	switch (ELF_ST_VISIBILITY (h->other))
1690 	  {
1691 	  case STV_INTERNAL:
1692 	  case STV_HIDDEN:
1693 	    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1694 	    break;
1695 	  }
1696     }
1697 
1698   /* If the old symbol is from a dynamic object, and the new symbol is
1699      a definition which is not from a dynamic object, then the new
1700      symbol overrides the old symbol.  Symbols from regular files
1701      always take precedence over symbols from dynamic objects, even if
1702      they are defined after the dynamic object in the link.
1703 
1704      As above, we again permit a common symbol in a regular object to
1705      override a definition in a shared object if the shared object
1706      symbol is a function or is weak.  */
1707 
1708   flip = NULL;
1709   if (!newdyn
1710       && (newdef
1711 	  || (bfd_is_com_section (sec)
1712 	      && (oldweak || oldfunc)))
1713       && olddyn
1714       && olddef
1715       && h->def_dynamic)
1716     {
1717       /* Change the hash table entry to undefined, and let
1718 	 _bfd_generic_link_add_one_symbol do the right thing with the
1719 	 new definition.  */
1720 
1721       h->root.type = bfd_link_hash_undefined;
1722       h->root.u.undef.abfd = h->root.u.def.section->owner;
1723       *size_change_ok = TRUE;
1724 
1725       olddef = FALSE;
1726       olddyncommon = FALSE;
1727 
1728       /* We again permit a type change when a common symbol may be
1729 	 overriding a function.  */
1730 
1731       if (bfd_is_com_section (sec))
1732 	{
1733 	  if (oldfunc)
1734 	    {
1735 	      /* If a common symbol overrides a function, make sure
1736 		 that it isn't defined dynamically nor has type
1737 		 function.  */
1738 	      h->def_dynamic = 0;
1739 	      h->type = STT_NOTYPE;
1740 	    }
1741 	  *type_change_ok = TRUE;
1742 	}
1743 
1744       if (hi->root.type == bfd_link_hash_indirect)
1745 	flip = hi;
1746       else
1747 	/* This union may have been set to be non-NULL when this symbol
1748 	   was seen in a dynamic object.  We must force the union to be
1749 	   NULL, so that it is correct for a regular symbol.  */
1750 	h->verinfo.vertree = NULL;
1751     }
1752 
1753   /* Handle the special case of a new common symbol merging with an
1754      old symbol that looks like it might be a common symbol defined in
1755      a shared object.  Note that we have already handled the case in
1756      which a new common symbol should simply override the definition
1757      in the shared library.  */
1758 
1759   if (! newdyn
1760       && bfd_is_com_section (sec)
1761       && olddyncommon)
1762     {
1763       /* It would be best if we could set the hash table entry to a
1764 	 common symbol, but we don't know what to use for the section
1765 	 or the alignment.  */
1766       (*info->callbacks->multiple_common) (info, &h->root, abfd,
1767 					   bfd_link_hash_common, sym->st_size);
1768 
1769       /* If the presumed common symbol in the dynamic object is
1770 	 larger, pretend that the new symbol has its size.  */
1771 
1772       if (h->size > *pvalue)
1773 	*pvalue = h->size;
1774 
1775       /* We need to remember the alignment required by the symbol
1776 	 in the dynamic object.  */
1777       BFD_ASSERT (pold_alignment);
1778       *pold_alignment = h->root.u.def.section->alignment_power;
1779 
1780       olddef = FALSE;
1781       olddyncommon = FALSE;
1782 
1783       h->root.type = bfd_link_hash_undefined;
1784       h->root.u.undef.abfd = h->root.u.def.section->owner;
1785 
1786       *size_change_ok = TRUE;
1787       *type_change_ok = TRUE;
1788 
1789       if (hi->root.type == bfd_link_hash_indirect)
1790 	flip = hi;
1791       else
1792 	h->verinfo.vertree = NULL;
1793     }
1794 
1795   if (flip != NULL)
1796     {
1797       /* Handle the case where we had a versioned symbol in a dynamic
1798 	 library and now find a definition in a normal object.  In this
1799 	 case, we make the versioned symbol point to the normal one.  */
1800       flip->root.type = h->root.type;
1801       flip->root.u.undef.abfd = h->root.u.undef.abfd;
1802       h->root.type = bfd_link_hash_indirect;
1803       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1804       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1805       if (h->def_dynamic)
1806 	{
1807 	  h->def_dynamic = 0;
1808 	  flip->ref_dynamic = 1;
1809 	}
1810     }
1811 
1812   return TRUE;
1813 }
1814 
1815 /* This function is called to create an indirect symbol from the
1816    default for the symbol with the default version if needed. The
1817    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1818    set DYNSYM if the new indirect symbol is dynamic.  */
1819 
1820 static bfd_boolean
1821 _bfd_elf_add_default_symbol (bfd *abfd,
1822 			     struct bfd_link_info *info,
1823 			     struct elf_link_hash_entry *h,
1824 			     const char *name,
1825 			     Elf_Internal_Sym *sym,
1826 			     asection *sec,
1827 			     bfd_vma value,
1828 			     bfd **poldbfd,
1829 			     bfd_boolean *dynsym)
1830 {
1831   bfd_boolean type_change_ok;
1832   bfd_boolean size_change_ok;
1833   bfd_boolean skip;
1834   char *shortname;
1835   struct elf_link_hash_entry *hi;
1836   struct bfd_link_hash_entry *bh;
1837   const struct elf_backend_data *bed;
1838   bfd_boolean collect;
1839   bfd_boolean dynamic;
1840   bfd_boolean override;
1841   char *p;
1842   size_t len, shortlen;
1843   asection *tmp_sec;
1844   bfd_boolean matched;
1845 
1846   if (h->versioned == unversioned || h->versioned == versioned_hidden)
1847     return TRUE;
1848 
1849   /* If this symbol has a version, and it is the default version, we
1850      create an indirect symbol from the default name to the fully
1851      decorated name.  This will cause external references which do not
1852      specify a version to be bound to this version of the symbol.  */
1853   p = strchr (name, ELF_VER_CHR);
1854   if (h->versioned == unknown)
1855     {
1856       if (p == NULL)
1857 	{
1858 	  h->versioned = unversioned;
1859 	  return TRUE;
1860 	}
1861       else
1862 	{
1863 	  if (p[1] != ELF_VER_CHR)
1864 	    {
1865 	      h->versioned = versioned_hidden;
1866 	      return TRUE;
1867 	    }
1868 	  else
1869 	    h->versioned = versioned;
1870 	}
1871     }
1872   else
1873     {
1874       /* PR ld/19073: We may see an unversioned definition after the
1875 	 default version.  */
1876       if (p == NULL)
1877 	return TRUE;
1878     }
1879 
1880   bed = get_elf_backend_data (abfd);
1881   collect = bed->collect;
1882   dynamic = (abfd->flags & DYNAMIC) != 0;
1883 
1884   shortlen = p - name;
1885   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1886   if (shortname == NULL)
1887     return FALSE;
1888   memcpy (shortname, name, shortlen);
1889   shortname[shortlen] = '\0';
1890 
1891   /* We are going to create a new symbol.  Merge it with any existing
1892      symbol with this name.  For the purposes of the merge, act as
1893      though we were defining the symbol we just defined, although we
1894      actually going to define an indirect symbol.  */
1895   type_change_ok = FALSE;
1896   size_change_ok = FALSE;
1897   matched = TRUE;
1898   tmp_sec = sec;
1899   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1900 			      &hi, poldbfd, NULL, NULL, &skip, &override,
1901 			      &type_change_ok, &size_change_ok, &matched))
1902     return FALSE;
1903 
1904   if (skip)
1905     goto nondefault;
1906 
1907   if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1908     {
1909       /* If the undecorated symbol will have a version added by a
1910 	 script different to H, then don't indirect to/from the
1911 	 undecorated symbol.  This isn't ideal because we may not yet
1912 	 have seen symbol versions, if given by a script on the
1913 	 command line rather than via --version-script.  */
1914       if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1915 	{
1916 	  bfd_boolean hide;
1917 
1918 	  hi->verinfo.vertree
1919 	    = bfd_find_version_for_sym (info->version_info,
1920 					hi->root.root.string, &hide);
1921 	  if (hi->verinfo.vertree != NULL && hide)
1922 	    {
1923 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1924 	      goto nondefault;
1925 	    }
1926 	}
1927       if (hi->verinfo.vertree != NULL
1928 	  && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1929 	goto nondefault;
1930     }
1931 
1932   if (! override)
1933     {
1934       /* Add the default symbol if not performing a relocatable link.  */
1935       if (! bfd_link_relocatable (info))
1936 	{
1937 	  bh = &hi->root;
1938 	  if (bh->type == bfd_link_hash_defined
1939 	      && bh->u.def.section->owner != NULL
1940 	      && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1941 	    {
1942 	      /* Mark the previous definition from IR object as
1943 		 undefined so that the generic linker will override
1944 		 it.  */
1945 	      bh->type = bfd_link_hash_undefined;
1946 	      bh->u.undef.abfd = bh->u.def.section->owner;
1947 	    }
1948 	  if (! (_bfd_generic_link_add_one_symbol
1949 		 (info, abfd, shortname, BSF_INDIRECT,
1950 		  bfd_ind_section_ptr,
1951 		  0, name, FALSE, collect, &bh)))
1952 	    return FALSE;
1953 	  hi = (struct elf_link_hash_entry *) bh;
1954 	}
1955     }
1956   else
1957     {
1958       /* In this case the symbol named SHORTNAME is overriding the
1959 	 indirect symbol we want to add.  We were planning on making
1960 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
1961 	 is the name without a version.  NAME is the fully versioned
1962 	 name, and it is the default version.
1963 
1964 	 Overriding means that we already saw a definition for the
1965 	 symbol SHORTNAME in a regular object, and it is overriding
1966 	 the symbol defined in the dynamic object.
1967 
1968 	 When this happens, we actually want to change NAME, the
1969 	 symbol we just added, to refer to SHORTNAME.  This will cause
1970 	 references to NAME in the shared object to become references
1971 	 to SHORTNAME in the regular object.  This is what we expect
1972 	 when we override a function in a shared object: that the
1973 	 references in the shared object will be mapped to the
1974 	 definition in the regular object.  */
1975 
1976       while (hi->root.type == bfd_link_hash_indirect
1977 	     || hi->root.type == bfd_link_hash_warning)
1978 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1979 
1980       h->root.type = bfd_link_hash_indirect;
1981       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1982       if (h->def_dynamic)
1983 	{
1984 	  h->def_dynamic = 0;
1985 	  hi->ref_dynamic = 1;
1986 	  if (hi->ref_regular
1987 	      || hi->def_regular)
1988 	    {
1989 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1990 		return FALSE;
1991 	    }
1992 	}
1993 
1994       /* Now set HI to H, so that the following code will set the
1995 	 other fields correctly.  */
1996       hi = h;
1997     }
1998 
1999   /* Check if HI is a warning symbol.  */
2000   if (hi->root.type == bfd_link_hash_warning)
2001     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2002 
2003   /* If there is a duplicate definition somewhere, then HI may not
2004      point to an indirect symbol.  We will have reported an error to
2005      the user in that case.  */
2006 
2007   if (hi->root.type == bfd_link_hash_indirect)
2008     {
2009       struct elf_link_hash_entry *ht;
2010 
2011       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2012       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2013 
2014       /* A reference to the SHORTNAME symbol from a dynamic library
2015 	 will be satisfied by the versioned symbol at runtime.  In
2016 	 effect, we have a reference to the versioned symbol.  */
2017       ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2018       hi->dynamic_def |= ht->dynamic_def;
2019 
2020       /* See if the new flags lead us to realize that the symbol must
2021 	 be dynamic.  */
2022       if (! *dynsym)
2023 	{
2024 	  if (! dynamic)
2025 	    {
2026 	      if (! bfd_link_executable (info)
2027 		  || hi->def_dynamic
2028 		  || hi->ref_dynamic)
2029 		*dynsym = TRUE;
2030 	    }
2031 	  else
2032 	    {
2033 	      if (hi->ref_regular)
2034 		*dynsym = TRUE;
2035 	    }
2036 	}
2037     }
2038 
2039   /* We also need to define an indirection from the nondefault version
2040      of the symbol.  */
2041 
2042 nondefault:
2043   len = strlen (name);
2044   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2045   if (shortname == NULL)
2046     return FALSE;
2047   memcpy (shortname, name, shortlen);
2048   memcpy (shortname + shortlen, p + 1, len - shortlen);
2049 
2050   /* Once again, merge with any existing symbol.  */
2051   type_change_ok = FALSE;
2052   size_change_ok = FALSE;
2053   tmp_sec = sec;
2054   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2055 			      &hi, poldbfd, NULL, NULL, &skip, &override,
2056 			      &type_change_ok, &size_change_ok, &matched))
2057     return FALSE;
2058 
2059   if (skip)
2060     return TRUE;
2061 
2062   if (override)
2063     {
2064       /* Here SHORTNAME is a versioned name, so we don't expect to see
2065 	 the type of override we do in the case above unless it is
2066 	 overridden by a versioned definition.  */
2067       if (hi->root.type != bfd_link_hash_defined
2068 	  && hi->root.type != bfd_link_hash_defweak)
2069 	_bfd_error_handler
2070 	  /* xgettext:c-format */
2071 	  (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2072 	   abfd, shortname);
2073     }
2074   else
2075     {
2076       bh = &hi->root;
2077       if (! (_bfd_generic_link_add_one_symbol
2078 	     (info, abfd, shortname, BSF_INDIRECT,
2079 	      bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2080 	return FALSE;
2081       hi = (struct elf_link_hash_entry *) bh;
2082 
2083       /* If there is a duplicate definition somewhere, then HI may not
2084 	 point to an indirect symbol.  We will have reported an error
2085 	 to the user in that case.  */
2086 
2087       if (hi->root.type == bfd_link_hash_indirect)
2088 	{
2089 	  (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2090 	  h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2091 	  hi->dynamic_def |= h->dynamic_def;
2092 
2093 	  /* See if the new flags lead us to realize that the symbol
2094 	     must be dynamic.  */
2095 	  if (! *dynsym)
2096 	    {
2097 	      if (! dynamic)
2098 		{
2099 		  if (! bfd_link_executable (info)
2100 		      || hi->ref_dynamic)
2101 		    *dynsym = TRUE;
2102 		}
2103 	      else
2104 		{
2105 		  if (hi->ref_regular)
2106 		    *dynsym = TRUE;
2107 		}
2108 	    }
2109 	}
2110     }
2111 
2112   return TRUE;
2113 }
2114 
2115 /* This routine is used to export all defined symbols into the dynamic
2116    symbol table.  It is called via elf_link_hash_traverse.  */
2117 
2118 static bfd_boolean
2119 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2120 {
2121   struct elf_info_failed *eif = (struct elf_info_failed *) data;
2122 
2123   /* Ignore indirect symbols.  These are added by the versioning code.  */
2124   if (h->root.type == bfd_link_hash_indirect)
2125     return TRUE;
2126 
2127   /* Ignore this if we won't export it.  */
2128   if (!eif->info->export_dynamic && !h->dynamic)
2129     return TRUE;
2130 
2131   if (h->dynindx == -1
2132       && (h->def_regular || h->ref_regular)
2133       && ! bfd_hide_sym_by_version (eif->info->version_info,
2134 				    h->root.root.string))
2135     {
2136       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2137 	{
2138 	  eif->failed = TRUE;
2139 	  return FALSE;
2140 	}
2141     }
2142 
2143   return TRUE;
2144 }
2145 
2146 /* Look through the symbols which are defined in other shared
2147    libraries and referenced here.  Update the list of version
2148    dependencies.  This will be put into the .gnu.version_r section.
2149    This function is called via elf_link_hash_traverse.  */
2150 
2151 static bfd_boolean
2152 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2153 					 void *data)
2154 {
2155   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2156   Elf_Internal_Verneed *t;
2157   Elf_Internal_Vernaux *a;
2158   bfd_size_type amt;
2159 
2160   /* We only care about symbols defined in shared objects with version
2161      information.  */
2162   if (!h->def_dynamic
2163       || h->def_regular
2164       || h->dynindx == -1
2165       || h->verinfo.verdef == NULL
2166       || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2167 	  & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2168     return TRUE;
2169 
2170   /* See if we already know about this version.  */
2171   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2172        t != NULL;
2173        t = t->vn_nextref)
2174     {
2175       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2176 	continue;
2177 
2178       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2179 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2180 	  return TRUE;
2181 
2182       break;
2183     }
2184 
2185   /* This is a new version.  Add it to tree we are building.  */
2186 
2187   if (t == NULL)
2188     {
2189       amt = sizeof *t;
2190       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2191       if (t == NULL)
2192 	{
2193 	  rinfo->failed = TRUE;
2194 	  return FALSE;
2195 	}
2196 
2197       t->vn_bfd = h->verinfo.verdef->vd_bfd;
2198       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2199       elf_tdata (rinfo->info->output_bfd)->verref = t;
2200     }
2201 
2202   amt = sizeof *a;
2203   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2204   if (a == NULL)
2205     {
2206       rinfo->failed = TRUE;
2207       return FALSE;
2208     }
2209 
2210   /* Note that we are copying a string pointer here, and testing it
2211      above.  If bfd_elf_string_from_elf_section is ever changed to
2212      discard the string data when low in memory, this will have to be
2213      fixed.  */
2214   a->vna_nodename = h->verinfo.verdef->vd_nodename;
2215 
2216   a->vna_flags = h->verinfo.verdef->vd_flags;
2217   a->vna_nextptr = t->vn_auxptr;
2218 
2219   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2220   ++rinfo->vers;
2221 
2222   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2223 
2224   t->vn_auxptr = a;
2225 
2226   return TRUE;
2227 }
2228 
2229 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2230    hidden.  Set *T_P to NULL if there is no match.  */
2231 
2232 static bfd_boolean
2233 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2234 				     struct elf_link_hash_entry *h,
2235 				     const char *version_p,
2236 				     struct bfd_elf_version_tree **t_p,
2237 				     bfd_boolean *hide)
2238 {
2239   struct bfd_elf_version_tree *t;
2240 
2241   /* Look for the version.  If we find it, it is no longer weak.  */
2242   for (t = info->version_info; t != NULL; t = t->next)
2243     {
2244       if (strcmp (t->name, version_p) == 0)
2245 	{
2246 	  size_t len;
2247 	  char *alc;
2248 	  struct bfd_elf_version_expr *d;
2249 
2250 	  len = version_p - h->root.root.string;
2251 	  alc = (char *) bfd_malloc (len);
2252 	  if (alc == NULL)
2253 	    return FALSE;
2254 	  memcpy (alc, h->root.root.string, len - 1);
2255 	  alc[len - 1] = '\0';
2256 	  if (alc[len - 2] == ELF_VER_CHR)
2257 	    alc[len - 2] = '\0';
2258 
2259 	  h->verinfo.vertree = t;
2260 	  t->used = TRUE;
2261 	  d = NULL;
2262 
2263 	  if (t->globals.list != NULL)
2264 	    d = (*t->match) (&t->globals, NULL, alc);
2265 
2266 	  /* See if there is anything to force this symbol to
2267 	     local scope.  */
2268 	  if (d == NULL && t->locals.list != NULL)
2269 	    {
2270 	      d = (*t->match) (&t->locals, NULL, alc);
2271 	      if (d != NULL
2272 		  && h->dynindx != -1
2273 		  && ! info->export_dynamic)
2274 		*hide = TRUE;
2275 	    }
2276 
2277 	  free (alc);
2278 	  break;
2279 	}
2280     }
2281 
2282   *t_p = t;
2283 
2284   return TRUE;
2285 }
2286 
2287 /* Return TRUE if the symbol H is hidden by version script.  */
2288 
2289 bfd_boolean
2290 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2291 				   struct elf_link_hash_entry *h)
2292 {
2293   const char *p;
2294   bfd_boolean hide = FALSE;
2295   const struct elf_backend_data *bed
2296     = get_elf_backend_data (info->output_bfd);
2297 
2298   /* Version script only hides symbols defined in regular objects.  */
2299   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2300     return TRUE;
2301 
2302   p = strchr (h->root.root.string, ELF_VER_CHR);
2303   if (p != NULL && h->verinfo.vertree == NULL)
2304     {
2305       struct bfd_elf_version_tree *t;
2306 
2307       ++p;
2308       if (*p == ELF_VER_CHR)
2309 	++p;
2310 
2311       if (*p != '\0'
2312 	  && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2313 	  && hide)
2314 	{
2315 	  if (hide)
2316 	    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2317 	  return TRUE;
2318 	}
2319     }
2320 
2321   /* If we don't have a version for this symbol, see if we can find
2322      something.  */
2323   if (h->verinfo.vertree == NULL && info->version_info != NULL)
2324     {
2325       h->verinfo.vertree
2326 	= bfd_find_version_for_sym (info->version_info,
2327 				    h->root.root.string, &hide);
2328       if (h->verinfo.vertree != NULL && hide)
2329 	{
2330 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2331 	  return TRUE;
2332 	}
2333     }
2334 
2335   return FALSE;
2336 }
2337 
2338 /* Figure out appropriate versions for all the symbols.  We may not
2339    have the version number script until we have read all of the input
2340    files, so until that point we don't know which symbols should be
2341    local.  This function is called via elf_link_hash_traverse.  */
2342 
2343 static bfd_boolean
2344 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2345 {
2346   struct elf_info_failed *sinfo;
2347   struct bfd_link_info *info;
2348   const struct elf_backend_data *bed;
2349   struct elf_info_failed eif;
2350   char *p;
2351   bfd_boolean hide;
2352 
2353   sinfo = (struct elf_info_failed *) data;
2354   info = sinfo->info;
2355 
2356   /* Fix the symbol flags.  */
2357   eif.failed = FALSE;
2358   eif.info = info;
2359   if (! _bfd_elf_fix_symbol_flags (h, &eif))
2360     {
2361       if (eif.failed)
2362 	sinfo->failed = TRUE;
2363       return FALSE;
2364     }
2365 
2366   bed = get_elf_backend_data (info->output_bfd);
2367 
2368   /* We only need version numbers for symbols defined in regular
2369      objects.  */
2370   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2371     {
2372       /* Hide symbols defined in discarded input sections.  */
2373       if ((h->root.type == bfd_link_hash_defined
2374 	   || h->root.type == bfd_link_hash_defweak)
2375 	  && discarded_section (h->root.u.def.section))
2376 	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
2377       return TRUE;
2378     }
2379 
2380   hide = FALSE;
2381   p = strchr (h->root.root.string, ELF_VER_CHR);
2382   if (p != NULL && h->verinfo.vertree == NULL)
2383     {
2384       struct bfd_elf_version_tree *t;
2385 
2386       ++p;
2387       if (*p == ELF_VER_CHR)
2388 	++p;
2389 
2390       /* If there is no version string, we can just return out.  */
2391       if (*p == '\0')
2392 	return TRUE;
2393 
2394       if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2395 	{
2396 	  sinfo->failed = TRUE;
2397 	  return FALSE;
2398 	}
2399 
2400       if (hide)
2401 	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
2402 
2403       /* If we are building an application, we need to create a
2404 	 version node for this version.  */
2405       if (t == NULL && bfd_link_executable (info))
2406 	{
2407 	  struct bfd_elf_version_tree **pp;
2408 	  int version_index;
2409 
2410 	  /* If we aren't going to export this symbol, we don't need
2411 	     to worry about it.  */
2412 	  if (h->dynindx == -1)
2413 	    return TRUE;
2414 
2415 	  t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2416 							  sizeof *t);
2417 	  if (t == NULL)
2418 	    {
2419 	      sinfo->failed = TRUE;
2420 	      return FALSE;
2421 	    }
2422 
2423 	  t->name = p;
2424 	  t->name_indx = (unsigned int) -1;
2425 	  t->used = TRUE;
2426 
2427 	  version_index = 1;
2428 	  /* Don't count anonymous version tag.  */
2429 	  if (sinfo->info->version_info != NULL
2430 	      && sinfo->info->version_info->vernum == 0)
2431 	    version_index = 0;
2432 	  for (pp = &sinfo->info->version_info;
2433 	       *pp != NULL;
2434 	       pp = &(*pp)->next)
2435 	    ++version_index;
2436 	  t->vernum = version_index;
2437 
2438 	  *pp = t;
2439 
2440 	  h->verinfo.vertree = t;
2441 	}
2442       else if (t == NULL)
2443 	{
2444 	  /* We could not find the version for a symbol when
2445 	     generating a shared archive.  Return an error.  */
2446 	  _bfd_error_handler
2447 	    /* xgettext:c-format */
2448 	    (_("%pB: version node not found for symbol %s"),
2449 	     info->output_bfd, h->root.root.string);
2450 	  bfd_set_error (bfd_error_bad_value);
2451 	  sinfo->failed = TRUE;
2452 	  return FALSE;
2453 	}
2454     }
2455 
2456   /* If we don't have a version for this symbol, see if we can find
2457      something.  */
2458   if (!hide
2459       && h->verinfo.vertree == NULL
2460       && sinfo->info->version_info != NULL)
2461     {
2462       h->verinfo.vertree
2463 	= bfd_find_version_for_sym (sinfo->info->version_info,
2464 				    h->root.root.string, &hide);
2465       if (h->verinfo.vertree != NULL && hide)
2466 	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
2467     }
2468 
2469   return TRUE;
2470 }
2471 
2472 /* Read and swap the relocs from the section indicated by SHDR.  This
2473    may be either a REL or a RELA section.  The relocations are
2474    translated into RELA relocations and stored in INTERNAL_RELOCS,
2475    which should have already been allocated to contain enough space.
2476    The EXTERNAL_RELOCS are a buffer where the external form of the
2477    relocations should be stored.
2478 
2479    Returns FALSE if something goes wrong.  */
2480 
2481 static bfd_boolean
2482 elf_link_read_relocs_from_section (bfd *abfd,
2483 				   asection *sec,
2484 				   Elf_Internal_Shdr *shdr,
2485 				   void *external_relocs,
2486 				   Elf_Internal_Rela *internal_relocs)
2487 {
2488   const struct elf_backend_data *bed;
2489   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2490   const bfd_byte *erela;
2491   const bfd_byte *erelaend;
2492   Elf_Internal_Rela *irela;
2493   Elf_Internal_Shdr *symtab_hdr;
2494   size_t nsyms;
2495 
2496   /* Position ourselves at the start of the section.  */
2497   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2498     return FALSE;
2499 
2500   /* Read the relocations.  */
2501   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2502     return FALSE;
2503 
2504   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2505   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2506 
2507   bed = get_elf_backend_data (abfd);
2508 
2509   /* Convert the external relocations to the internal format.  */
2510   if (shdr->sh_entsize == bed->s->sizeof_rel)
2511     swap_in = bed->s->swap_reloc_in;
2512   else if (shdr->sh_entsize == bed->s->sizeof_rela)
2513     swap_in = bed->s->swap_reloca_in;
2514   else
2515     {
2516       bfd_set_error (bfd_error_wrong_format);
2517       return FALSE;
2518     }
2519 
2520   erela = (const bfd_byte *) external_relocs;
2521   /* Setting erelaend like this and comparing with <= handles case of
2522      a fuzzed object with sh_size not a multiple of sh_entsize.  */
2523   erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2524   irela = internal_relocs;
2525   while (erela <= erelaend)
2526     {
2527       bfd_vma r_symndx;
2528 
2529       (*swap_in) (abfd, erela, irela);
2530       r_symndx = ELF32_R_SYM (irela->r_info);
2531       if (bed->s->arch_size == 64)
2532 	r_symndx >>= 24;
2533       if (nsyms > 0)
2534 	{
2535 	  if ((size_t) r_symndx >= nsyms)
2536 	    {
2537 	      _bfd_error_handler
2538 		/* xgettext:c-format */
2539 		(_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2540 		   " for offset %#" PRIx64 " in section `%pA'"),
2541 		 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2542 		 (uint64_t) irela->r_offset, sec);
2543 	      bfd_set_error (bfd_error_bad_value);
2544 	      return FALSE;
2545 	    }
2546 	}
2547       else if (r_symndx != STN_UNDEF)
2548 	{
2549 	  _bfd_error_handler
2550 	    /* xgettext:c-format */
2551 	    (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2552 	       " for offset %#" PRIx64 " in section `%pA'"
2553 	       " when the object file has no symbol table"),
2554 	     abfd, (uint64_t) r_symndx,
2555 	     (uint64_t) irela->r_offset, sec);
2556 	  bfd_set_error (bfd_error_bad_value);
2557 	  return FALSE;
2558 	}
2559       irela += bed->s->int_rels_per_ext_rel;
2560       erela += shdr->sh_entsize;
2561     }
2562 
2563   return TRUE;
2564 }
2565 
2566 /* Read and swap the relocs for a section O.  They may have been
2567    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2568    not NULL, they are used as buffers to read into.  They are known to
2569    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2570    the return value is allocated using either malloc or bfd_alloc,
2571    according to the KEEP_MEMORY argument.  If O has two relocation
2572    sections (both REL and RELA relocations), then the REL_HDR
2573    relocations will appear first in INTERNAL_RELOCS, followed by the
2574    RELA_HDR relocations.  */
2575 
2576 Elf_Internal_Rela *
2577 _bfd_elf_link_read_relocs (bfd *abfd,
2578 			   asection *o,
2579 			   void *external_relocs,
2580 			   Elf_Internal_Rela *internal_relocs,
2581 			   bfd_boolean keep_memory)
2582 {
2583   void *alloc1 = NULL;
2584   Elf_Internal_Rela *alloc2 = NULL;
2585   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2586   struct bfd_elf_section_data *esdo = elf_section_data (o);
2587   Elf_Internal_Rela *internal_rela_relocs;
2588 
2589   if (esdo->relocs != NULL)
2590     return esdo->relocs;
2591 
2592   if (o->reloc_count == 0)
2593     return NULL;
2594 
2595   if (internal_relocs == NULL)
2596     {
2597       bfd_size_type size;
2598 
2599       size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2600       if (keep_memory)
2601 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2602       else
2603 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2604       if (internal_relocs == NULL)
2605 	goto error_return;
2606     }
2607 
2608   if (external_relocs == NULL)
2609     {
2610       bfd_size_type size = 0;
2611 
2612       if (esdo->rel.hdr)
2613 	size += esdo->rel.hdr->sh_size;
2614       if (esdo->rela.hdr)
2615 	size += esdo->rela.hdr->sh_size;
2616 
2617       alloc1 = bfd_malloc (size);
2618       if (alloc1 == NULL)
2619 	goto error_return;
2620       external_relocs = alloc1;
2621     }
2622 
2623   internal_rela_relocs = internal_relocs;
2624   if (esdo->rel.hdr)
2625     {
2626       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2627 					      external_relocs,
2628 					      internal_relocs))
2629 	goto error_return;
2630       external_relocs = (((bfd_byte *) external_relocs)
2631 			 + esdo->rel.hdr->sh_size);
2632       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2633 			       * bed->s->int_rels_per_ext_rel);
2634     }
2635 
2636   if (esdo->rela.hdr
2637       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2638 					      external_relocs,
2639 					      internal_rela_relocs)))
2640     goto error_return;
2641 
2642   /* Cache the results for next time, if we can.  */
2643   if (keep_memory)
2644     esdo->relocs = internal_relocs;
2645 
2646   if (alloc1 != NULL)
2647     free (alloc1);
2648 
2649   /* Don't free alloc2, since if it was allocated we are passing it
2650      back (under the name of internal_relocs).  */
2651 
2652   return internal_relocs;
2653 
2654  error_return:
2655   if (alloc1 != NULL)
2656     free (alloc1);
2657   if (alloc2 != NULL)
2658     {
2659       if (keep_memory)
2660 	bfd_release (abfd, alloc2);
2661       else
2662 	free (alloc2);
2663     }
2664   return NULL;
2665 }
2666 
2667 /* Compute the size of, and allocate space for, REL_HDR which is the
2668    section header for a section containing relocations for O.  */
2669 
2670 static bfd_boolean
2671 _bfd_elf_link_size_reloc_section (bfd *abfd,
2672 				  struct bfd_elf_section_reloc_data *reldata)
2673 {
2674   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2675 
2676   /* That allows us to calculate the size of the section.  */
2677   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2678 
2679   /* The contents field must last into write_object_contents, so we
2680      allocate it with bfd_alloc rather than malloc.  Also since we
2681      cannot be sure that the contents will actually be filled in,
2682      we zero the allocated space.  */
2683   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2684   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2685     return FALSE;
2686 
2687   if (reldata->hashes == NULL && reldata->count)
2688     {
2689       struct elf_link_hash_entry **p;
2690 
2691       p = ((struct elf_link_hash_entry **)
2692 	   bfd_zmalloc (reldata->count * sizeof (*p)));
2693       if (p == NULL)
2694 	return FALSE;
2695 
2696       reldata->hashes = p;
2697     }
2698 
2699   return TRUE;
2700 }
2701 
2702 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2703    originated from the section given by INPUT_REL_HDR) to the
2704    OUTPUT_BFD.  */
2705 
2706 bfd_boolean
2707 _bfd_elf_link_output_relocs (bfd *output_bfd,
2708 			     asection *input_section,
2709 			     Elf_Internal_Shdr *input_rel_hdr,
2710 			     Elf_Internal_Rela *internal_relocs,
2711 			     struct elf_link_hash_entry **rel_hash
2712 			       ATTRIBUTE_UNUSED)
2713 {
2714   Elf_Internal_Rela *irela;
2715   Elf_Internal_Rela *irelaend;
2716   bfd_byte *erel;
2717   struct bfd_elf_section_reloc_data *output_reldata;
2718   asection *output_section;
2719   const struct elf_backend_data *bed;
2720   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2721   struct bfd_elf_section_data *esdo;
2722 
2723   output_section = input_section->output_section;
2724 
2725   bed = get_elf_backend_data (output_bfd);
2726   esdo = elf_section_data (output_section);
2727   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2728     {
2729       output_reldata = &esdo->rel;
2730       swap_out = bed->s->swap_reloc_out;
2731     }
2732   else if (esdo->rela.hdr
2733 	   && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2734     {
2735       output_reldata = &esdo->rela;
2736       swap_out = bed->s->swap_reloca_out;
2737     }
2738   else
2739     {
2740       _bfd_error_handler
2741 	/* xgettext:c-format */
2742 	(_("%pB: relocation size mismatch in %pB section %pA"),
2743 	 output_bfd, input_section->owner, input_section);
2744       bfd_set_error (bfd_error_wrong_format);
2745       return FALSE;
2746     }
2747 
2748   erel = output_reldata->hdr->contents;
2749   erel += output_reldata->count * input_rel_hdr->sh_entsize;
2750   irela = internal_relocs;
2751   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2752 		      * bed->s->int_rels_per_ext_rel);
2753   while (irela < irelaend)
2754     {
2755       (*swap_out) (output_bfd, irela, erel);
2756       irela += bed->s->int_rels_per_ext_rel;
2757       erel += input_rel_hdr->sh_entsize;
2758     }
2759 
2760   /* Bump the counter, so that we know where to add the next set of
2761      relocations.  */
2762   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2763 
2764   return TRUE;
2765 }
2766 
2767 /* Make weak undefined symbols in PIE dynamic.  */
2768 
2769 bfd_boolean
2770 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2771 				 struct elf_link_hash_entry *h)
2772 {
2773   if (bfd_link_pie (info)
2774       && h->dynindx == -1
2775       && h->root.type == bfd_link_hash_undefweak)
2776     return bfd_elf_link_record_dynamic_symbol (info, h);
2777 
2778   return TRUE;
2779 }
2780 
2781 /* Fix up the flags for a symbol.  This handles various cases which
2782    can only be fixed after all the input files are seen.  This is
2783    currently called by both adjust_dynamic_symbol and
2784    assign_sym_version, which is unnecessary but perhaps more robust in
2785    the face of future changes.  */
2786 
2787 static bfd_boolean
2788 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2789 			   struct elf_info_failed *eif)
2790 {
2791   const struct elf_backend_data *bed;
2792 
2793   /* If this symbol was mentioned in a non-ELF file, try to set
2794      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
2795      permit a non-ELF file to correctly refer to a symbol defined in
2796      an ELF dynamic object.  */
2797   if (h->non_elf)
2798     {
2799       while (h->root.type == bfd_link_hash_indirect)
2800 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
2801 
2802       if (h->root.type != bfd_link_hash_defined
2803 	  && h->root.type != bfd_link_hash_defweak)
2804 	{
2805 	  h->ref_regular = 1;
2806 	  h->ref_regular_nonweak = 1;
2807 	}
2808       else
2809 	{
2810 	  if (h->root.u.def.section->owner != NULL
2811 	      && (bfd_get_flavour (h->root.u.def.section->owner)
2812 		  == bfd_target_elf_flavour))
2813 	    {
2814 	      h->ref_regular = 1;
2815 	      h->ref_regular_nonweak = 1;
2816 	    }
2817 	  else
2818 	    h->def_regular = 1;
2819 	}
2820 
2821       if (h->dynindx == -1
2822 	  && (h->def_dynamic
2823 	      || h->ref_dynamic))
2824 	{
2825 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2826 	    {
2827 	      eif->failed = TRUE;
2828 	      return FALSE;
2829 	    }
2830 	}
2831     }
2832   else
2833     {
2834       /* Unfortunately, NON_ELF is only correct if the symbol
2835 	 was first seen in a non-ELF file.  Fortunately, if the symbol
2836 	 was first seen in an ELF file, we're probably OK unless the
2837 	 symbol was defined in a non-ELF file.  Catch that case here.
2838 	 FIXME: We're still in trouble if the symbol was first seen in
2839 	 a dynamic object, and then later in a non-ELF regular object.  */
2840       if ((h->root.type == bfd_link_hash_defined
2841 	   || h->root.type == bfd_link_hash_defweak)
2842 	  && !h->def_regular
2843 	  && (h->root.u.def.section->owner != NULL
2844 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
2845 		 != bfd_target_elf_flavour)
2846 	      : (bfd_is_abs_section (h->root.u.def.section)
2847 		 && !h->def_dynamic)))
2848 	h->def_regular = 1;
2849     }
2850 
2851   /* Backend specific symbol fixup.  */
2852   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2853   if (bed->elf_backend_fixup_symbol
2854       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2855     return FALSE;
2856 
2857   /* If this is a final link, and the symbol was defined as a common
2858      symbol in a regular object file, and there was no definition in
2859      any dynamic object, then the linker will have allocated space for
2860      the symbol in a common section but the DEF_REGULAR
2861      flag will not have been set.  */
2862   if (h->root.type == bfd_link_hash_defined
2863       && !h->def_regular
2864       && h->ref_regular
2865       && !h->def_dynamic
2866       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2867     h->def_regular = 1;
2868 
2869   /* Symbols defined in discarded sections shouldn't be dynamic.  */
2870   if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2871     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2872 
2873   /* If a weak undefined symbol has non-default visibility, we also
2874      hide it from the dynamic linker.  */
2875   else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2876 	   && h->root.type == bfd_link_hash_undefweak)
2877     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2878 
2879   /* A hidden versioned symbol in executable should be forced local if
2880      it is is locally defined, not referenced by shared library and not
2881      exported.  */
2882   else if (bfd_link_executable (eif->info)
2883 	   && h->versioned == versioned_hidden
2884 	   && !eif->info->export_dynamic
2885 	   && !h->dynamic
2886 	   && !h->ref_dynamic
2887 	   && h->def_regular)
2888     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2889 
2890   /* If -Bsymbolic was used (which means to bind references to global
2891      symbols to the definition within the shared object), and this
2892      symbol was defined in a regular object, then it actually doesn't
2893      need a PLT entry.  Likewise, if the symbol has non-default
2894      visibility.  If the symbol has hidden or internal visibility, we
2895      will force it local.  */
2896   else if (h->needs_plt
2897 	   && bfd_link_pic (eif->info)
2898 	   && is_elf_hash_table (eif->info->hash)
2899 	   && (SYMBOLIC_BIND (eif->info, h)
2900 	       || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2901 	   && h->def_regular)
2902     {
2903       bfd_boolean force_local;
2904 
2905       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2906 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2907       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2908     }
2909 
2910   /* If this is a weak defined symbol in a dynamic object, and we know
2911      the real definition in the dynamic object, copy interesting flags
2912      over to the real definition.  */
2913   if (h->is_weakalias)
2914     {
2915       struct elf_link_hash_entry *def = weakdef (h);
2916       while (def->root.type == bfd_link_hash_indirect)
2917         def = (struct elf_link_hash_entry *) def->root.u.i.link;
2918 
2919       /* If the real definition is defined by a regular object file,
2920 	 don't do anything special.  See the longer description in
2921 	 _bfd_elf_adjust_dynamic_symbol, below.  If the def is not
2922 	 bfd_link_hash_defined as it was when put on the alias list
2923 	 then it must have originally been a versioned symbol (for
2924 	 which a non-versioned indirect symbol is created) and later
2925 	 a definition for the non-versioned symbol is found.  In that
2926 	 case the indirection is flipped with the versioned symbol
2927 	 becoming an indirect pointing at the non-versioned symbol.
2928 	 Thus, not an alias any more.  */
2929       if (def->def_regular
2930 	  || def->root.type != bfd_link_hash_defined)
2931 	{
2932 	  h = def;
2933 	  while ((h = h->u.alias) != def)
2934 	    h->is_weakalias = 0;
2935 	}
2936       else
2937 	{
2938 	  while (h->root.type == bfd_link_hash_indirect)
2939 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2940 	  BFD_ASSERT (h->root.type == bfd_link_hash_defined
2941 		      || h->root.type == bfd_link_hash_defweak);
2942 	  BFD_ASSERT (def->def_dynamic);
2943 	  (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2944 	}
2945     }
2946 
2947   return TRUE;
2948 }
2949 
2950 /* Make the backend pick a good value for a dynamic symbol.  This is
2951    called via elf_link_hash_traverse, and also calls itself
2952    recursively.  */
2953 
2954 static bfd_boolean
2955 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2956 {
2957   struct elf_info_failed *eif = (struct elf_info_failed *) data;
2958   struct elf_link_hash_table *htab;
2959   const struct elf_backend_data *bed;
2960 
2961   if (! is_elf_hash_table (eif->info->hash))
2962     return FALSE;
2963 
2964   /* Ignore indirect symbols.  These are added by the versioning code.  */
2965   if (h->root.type == bfd_link_hash_indirect)
2966     return TRUE;
2967 
2968   /* Fix the symbol flags.  */
2969   if (! _bfd_elf_fix_symbol_flags (h, eif))
2970     return FALSE;
2971 
2972   htab = elf_hash_table (eif->info);
2973   bed = get_elf_backend_data (htab->dynobj);
2974 
2975   if (h->root.type == bfd_link_hash_undefweak)
2976     {
2977       if (eif->info->dynamic_undefined_weak == 0)
2978 	(*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2979       else if (eif->info->dynamic_undefined_weak > 0
2980 	       && h->ref_regular
2981 	       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2982 	       && !bfd_hide_sym_by_version (eif->info->version_info,
2983 					    h->root.root.string))
2984 	{
2985 	  if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2986 	    {
2987 	      eif->failed = TRUE;
2988 	      return FALSE;
2989 	    }
2990 	}
2991     }
2992 
2993   /* If this symbol does not require a PLT entry, and it is not
2994      defined by a dynamic object, or is not referenced by a regular
2995      object, ignore it.  We do have to handle a weak defined symbol,
2996      even if no regular object refers to it, if we decided to add it
2997      to the dynamic symbol table.  FIXME: Do we normally need to worry
2998      about symbols which are defined by one dynamic object and
2999      referenced by another one?  */
3000   if (!h->needs_plt
3001       && h->type != STT_GNU_IFUNC
3002       && (h->def_regular
3003 	  || !h->def_dynamic
3004 	  || (!h->ref_regular
3005 	      && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3006     {
3007       h->plt = elf_hash_table (eif->info)->init_plt_offset;
3008       return TRUE;
3009     }
3010 
3011   /* If we've already adjusted this symbol, don't do it again.  This
3012      can happen via a recursive call.  */
3013   if (h->dynamic_adjusted)
3014     return TRUE;
3015 
3016   /* Don't look at this symbol again.  Note that we must set this
3017      after checking the above conditions, because we may look at a
3018      symbol once, decide not to do anything, and then get called
3019      recursively later after REF_REGULAR is set below.  */
3020   h->dynamic_adjusted = 1;
3021 
3022   /* If this is a weak definition, and we know a real definition, and
3023      the real symbol is not itself defined by a regular object file,
3024      then get a good value for the real definition.  We handle the
3025      real symbol first, for the convenience of the backend routine.
3026 
3027      Note that there is a confusing case here.  If the real definition
3028      is defined by a regular object file, we don't get the real symbol
3029      from the dynamic object, but we do get the weak symbol.  If the
3030      processor backend uses a COPY reloc, then if some routine in the
3031      dynamic object changes the real symbol, we will not see that
3032      change in the corresponding weak symbol.  This is the way other
3033      ELF linkers work as well, and seems to be a result of the shared
3034      library model.
3035 
3036      I will clarify this issue.  Most SVR4 shared libraries define the
3037      variable _timezone and define timezone as a weak synonym.  The
3038      tzset call changes _timezone.  If you write
3039        extern int timezone;
3040        int _timezone = 5;
3041        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3042      you might expect that, since timezone is a synonym for _timezone,
3043      the same number will print both times.  However, if the processor
3044      backend uses a COPY reloc, then actually timezone will be copied
3045      into your process image, and, since you define _timezone
3046      yourself, _timezone will not.  Thus timezone and _timezone will
3047      wind up at different memory locations.  The tzset call will set
3048      _timezone, leaving timezone unchanged.  */
3049 
3050   if (h->is_weakalias)
3051     {
3052       struct elf_link_hash_entry *def = weakdef (h);
3053 
3054       /* If we get to this point, there is an implicit reference to
3055 	 the alias by a regular object file via the weak symbol H.  */
3056       def->ref_regular = 1;
3057 
3058       /* Ensure that the backend adjust_dynamic_symbol function sees
3059 	 the strong alias before H by recursively calling ourselves.  */
3060       if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3061 	return FALSE;
3062     }
3063 
3064   /* If a symbol has no type and no size and does not require a PLT
3065      entry, then we are probably about to do the wrong thing here: we
3066      are probably going to create a COPY reloc for an empty object.
3067      This case can arise when a shared object is built with assembly
3068      code, and the assembly code fails to set the symbol type.  */
3069   if (h->size == 0
3070       && h->type == STT_NOTYPE
3071       && !h->needs_plt)
3072     _bfd_error_handler
3073       (_("warning: type and size of dynamic symbol `%s' are not defined"),
3074        h->root.root.string);
3075 
3076   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3077     {
3078       eif->failed = TRUE;
3079       return FALSE;
3080     }
3081 
3082   return TRUE;
3083 }
3084 
3085 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3086    DYNBSS.  */
3087 
3088 bfd_boolean
3089 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3090 			      struct elf_link_hash_entry *h,
3091 			      asection *dynbss)
3092 {
3093   unsigned int power_of_two;
3094   bfd_vma mask;
3095   asection *sec = h->root.u.def.section;
3096 
3097   /* The section alignment of the definition is the maximum alignment
3098      requirement of symbols defined in the section.  Since we don't
3099      know the symbol alignment requirement, we start with the
3100      maximum alignment and check low bits of the symbol address
3101      for the minimum alignment.  */
3102   power_of_two = bfd_section_alignment (sec);
3103   mask = ((bfd_vma) 1 << power_of_two) - 1;
3104   while ((h->root.u.def.value & mask) != 0)
3105     {
3106        mask >>= 1;
3107        --power_of_two;
3108     }
3109 
3110   if (power_of_two > bfd_section_alignment (dynbss))
3111     {
3112       /* Adjust the section alignment if needed.  */
3113       if (!bfd_set_section_alignment (dynbss, power_of_two))
3114 	return FALSE;
3115     }
3116 
3117   /* We make sure that the symbol will be aligned properly.  */
3118   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3119 
3120   /* Define the symbol as being at this point in DYNBSS.  */
3121   h->root.u.def.section = dynbss;
3122   h->root.u.def.value = dynbss->size;
3123 
3124   /* Increment the size of DYNBSS to make room for the symbol.  */
3125   dynbss->size += h->size;
3126 
3127   /* No error if extern_protected_data is true.  */
3128   if (h->protected_def
3129       && (!info->extern_protected_data
3130 	  || (info->extern_protected_data < 0
3131 	      && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3132     info->callbacks->einfo
3133       (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3134        h->root.root.string);
3135 
3136   return TRUE;
3137 }
3138 
3139 /* Adjust all external symbols pointing into SEC_MERGE sections
3140    to reflect the object merging within the sections.  */
3141 
3142 static bfd_boolean
3143 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3144 {
3145   asection *sec;
3146 
3147   if ((h->root.type == bfd_link_hash_defined
3148        || h->root.type == bfd_link_hash_defweak)
3149       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3150       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3151     {
3152       bfd *output_bfd = (bfd *) data;
3153 
3154       h->root.u.def.value =
3155 	_bfd_merged_section_offset (output_bfd,
3156 				    &h->root.u.def.section,
3157 				    elf_section_data (sec)->sec_info,
3158 				    h->root.u.def.value);
3159     }
3160 
3161   return TRUE;
3162 }
3163 
3164 /* Returns false if the symbol referred to by H should be considered
3165    to resolve local to the current module, and true if it should be
3166    considered to bind dynamically.  */
3167 
3168 bfd_boolean
3169 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3170 			   struct bfd_link_info *info,
3171 			   bfd_boolean not_local_protected)
3172 {
3173   bfd_boolean binding_stays_local_p;
3174   const struct elf_backend_data *bed;
3175   struct elf_link_hash_table *hash_table;
3176 
3177   if (h == NULL)
3178     return FALSE;
3179 
3180   while (h->root.type == bfd_link_hash_indirect
3181 	 || h->root.type == bfd_link_hash_warning)
3182     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3183 
3184   /* If it was forced local, then clearly it's not dynamic.  */
3185   if (h->dynindx == -1)
3186     return FALSE;
3187   if (h->forced_local)
3188     return FALSE;
3189 
3190   /* Identify the cases where name binding rules say that a
3191      visible symbol resolves locally.  */
3192   binding_stays_local_p = (bfd_link_executable (info)
3193 			   || SYMBOLIC_BIND (info, h));
3194 
3195   switch (ELF_ST_VISIBILITY (h->other))
3196     {
3197     case STV_INTERNAL:
3198     case STV_HIDDEN:
3199       return FALSE;
3200 
3201     case STV_PROTECTED:
3202       hash_table = elf_hash_table (info);
3203       if (!is_elf_hash_table (hash_table))
3204 	return FALSE;
3205 
3206       bed = get_elf_backend_data (hash_table->dynobj);
3207 
3208       /* Proper resolution for function pointer equality may require
3209 	 that these symbols perhaps be resolved dynamically, even though
3210 	 we should be resolving them to the current module.  */
3211       if (!not_local_protected || !bed->is_function_type (h->type))
3212 	binding_stays_local_p = TRUE;
3213       break;
3214 
3215     default:
3216       break;
3217     }
3218 
3219   /* If it isn't defined locally, then clearly it's dynamic.  */
3220   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3221     return TRUE;
3222 
3223   /* Otherwise, the symbol is dynamic if binding rules don't tell
3224      us that it remains local.  */
3225   return !binding_stays_local_p;
3226 }
3227 
3228 /* Return true if the symbol referred to by H should be considered
3229    to resolve local to the current module, and false otherwise.  Differs
3230    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3231    undefined symbols.  The two functions are virtually identical except
3232    for the place where dynindx == -1 is tested.  If that test is true,
3233    _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3234    _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3235    defined symbols.
3236    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3237    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3238    treatment of undefined weak symbols.  For those that do not make
3239    undefined weak symbols dynamic, both functions may return false.  */
3240 
3241 bfd_boolean
3242 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3243 			      struct bfd_link_info *info,
3244 			      bfd_boolean local_protected)
3245 {
3246   const struct elf_backend_data *bed;
3247   struct elf_link_hash_table *hash_table;
3248 
3249   /* If it's a local sym, of course we resolve locally.  */
3250   if (h == NULL)
3251     return TRUE;
3252 
3253   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
3254   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3255       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3256     return TRUE;
3257 
3258   /* Forced local symbols resolve locally.  */
3259   if (h->forced_local)
3260     return TRUE;
3261 
3262   /* Common symbols that become definitions don't get the DEF_REGULAR
3263      flag set, so test it first, and don't bail out.  */
3264   if (ELF_COMMON_DEF_P (h))
3265     /* Do nothing.  */;
3266   /* If we don't have a definition in a regular file, then we can't
3267      resolve locally.  The sym is either undefined or dynamic.  */
3268   else if (!h->def_regular)
3269     return FALSE;
3270 
3271   /* Non-dynamic symbols resolve locally.  */
3272   if (h->dynindx == -1)
3273     return TRUE;
3274 
3275   /* At this point, we know the symbol is defined and dynamic.  In an
3276      executable it must resolve locally, likewise when building symbolic
3277      shared libraries.  */
3278   if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3279     return TRUE;
3280 
3281   /* Now deal with defined dynamic symbols in shared libraries.  Ones
3282      with default visibility might not resolve locally.  */
3283   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3284     return FALSE;
3285 
3286   hash_table = elf_hash_table (info);
3287   if (!is_elf_hash_table (hash_table))
3288     return TRUE;
3289 
3290   bed = get_elf_backend_data (hash_table->dynobj);
3291 
3292   /* If extern_protected_data is false, STV_PROTECTED non-function
3293      symbols are local.  */
3294   if ((!info->extern_protected_data
3295        || (info->extern_protected_data < 0
3296 	   && !bed->extern_protected_data))
3297       && !bed->is_function_type (h->type))
3298     return TRUE;
3299 
3300   /* Function pointer equality tests may require that STV_PROTECTED
3301      symbols be treated as dynamic symbols.  If the address of a
3302      function not defined in an executable is set to that function's
3303      plt entry in the executable, then the address of the function in
3304      a shared library must also be the plt entry in the executable.  */
3305   return local_protected;
3306 }
3307 
3308 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3309    aligned.  Returns the first TLS output section.  */
3310 
3311 struct bfd_section *
3312 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3313 {
3314   struct bfd_section *sec, *tls;
3315   unsigned int align = 0;
3316 
3317   for (sec = obfd->sections; sec != NULL; sec = sec->next)
3318     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3319       break;
3320   tls = sec;
3321 
3322   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3323     if (sec->alignment_power > align)
3324       align = sec->alignment_power;
3325 
3326   elf_hash_table (info)->tls_sec = tls;
3327 
3328   /* Ensure the alignment of the first section is the largest alignment,
3329      so that the tls segment starts aligned.  */
3330   if (tls != NULL)
3331     tls->alignment_power = align;
3332 
3333   return tls;
3334 }
3335 
3336 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3337 static bfd_boolean
3338 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3339 				  Elf_Internal_Sym *sym)
3340 {
3341   const struct elf_backend_data *bed;
3342 
3343   /* Local symbols do not count, but target specific ones might.  */
3344   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3345       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3346     return FALSE;
3347 
3348   bed = get_elf_backend_data (abfd);
3349   /* Function symbols do not count.  */
3350   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3351     return FALSE;
3352 
3353   /* If the section is undefined, then so is the symbol.  */
3354   if (sym->st_shndx == SHN_UNDEF)
3355     return FALSE;
3356 
3357   /* If the symbol is defined in the common section, then
3358      it is a common definition and so does not count.  */
3359   if (bed->common_definition (sym))
3360     return FALSE;
3361 
3362   /* If the symbol is in a target specific section then we
3363      must rely upon the backend to tell us what it is.  */
3364   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3365     /* FIXME - this function is not coded yet:
3366 
3367        return _bfd_is_global_symbol_definition (abfd, sym);
3368 
3369        Instead for now assume that the definition is not global,
3370        Even if this is wrong, at least the linker will behave
3371        in the same way that it used to do.  */
3372     return FALSE;
3373 
3374   return TRUE;
3375 }
3376 
3377 /* Search the symbol table of the archive element of the archive ABFD
3378    whose archive map contains a mention of SYMDEF, and determine if
3379    the symbol is defined in this element.  */
3380 static bfd_boolean
3381 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3382 {
3383   Elf_Internal_Shdr * hdr;
3384   size_t symcount;
3385   size_t extsymcount;
3386   size_t extsymoff;
3387   Elf_Internal_Sym *isymbuf;
3388   Elf_Internal_Sym *isym;
3389   Elf_Internal_Sym *isymend;
3390   bfd_boolean result;
3391 
3392   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3393   if (abfd == NULL)
3394     return FALSE;
3395 
3396   if (! bfd_check_format (abfd, bfd_object))
3397     return FALSE;
3398 
3399   /* Select the appropriate symbol table.  If we don't know if the
3400      object file is an IR object, give linker LTO plugin a chance to
3401      get the correct symbol table.  */
3402   if (abfd->plugin_format == bfd_plugin_yes
3403 #if BFD_SUPPORTS_PLUGINS
3404       || (abfd->plugin_format == bfd_plugin_unknown
3405 	  && bfd_link_plugin_object_p (abfd))
3406 #endif
3407       )
3408     {
3409       /* Use the IR symbol table if the object has been claimed by
3410 	 plugin.  */
3411       abfd = abfd->plugin_dummy_bfd;
3412       hdr = &elf_tdata (abfd)->symtab_hdr;
3413     }
3414   else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3415     hdr = &elf_tdata (abfd)->symtab_hdr;
3416   else
3417     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3418 
3419   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3420 
3421   /* The sh_info field of the symtab header tells us where the
3422      external symbols start.  We don't care about the local symbols.  */
3423   if (elf_bad_symtab (abfd))
3424     {
3425       extsymcount = symcount;
3426       extsymoff = 0;
3427     }
3428   else
3429     {
3430       extsymcount = symcount - hdr->sh_info;
3431       extsymoff = hdr->sh_info;
3432     }
3433 
3434   if (extsymcount == 0)
3435     return FALSE;
3436 
3437   /* Read in the symbol table.  */
3438   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3439 				  NULL, NULL, NULL);
3440   if (isymbuf == NULL)
3441     return FALSE;
3442 
3443   /* Scan the symbol table looking for SYMDEF.  */
3444   result = FALSE;
3445   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3446     {
3447       const char *name;
3448 
3449       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3450 					      isym->st_name);
3451       if (name == NULL)
3452 	break;
3453 
3454       if (strcmp (name, symdef->name) == 0)
3455 	{
3456 	  result = is_global_data_symbol_definition (abfd, isym);
3457 	  break;
3458 	}
3459     }
3460 
3461   free (isymbuf);
3462 
3463   return result;
3464 }
3465 
3466 /* Add an entry to the .dynamic table.  */
3467 
3468 bfd_boolean
3469 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3470 			    bfd_vma tag,
3471 			    bfd_vma val)
3472 {
3473   struct elf_link_hash_table *hash_table;
3474   const struct elf_backend_data *bed;
3475   asection *s;
3476   bfd_size_type newsize;
3477   bfd_byte *newcontents;
3478   Elf_Internal_Dyn dyn;
3479 
3480   hash_table = elf_hash_table (info);
3481   if (! is_elf_hash_table (hash_table))
3482     return FALSE;
3483 
3484   if (tag == DT_RELA || tag == DT_REL)
3485     hash_table->dynamic_relocs = TRUE;
3486 
3487   bed = get_elf_backend_data (hash_table->dynobj);
3488   s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3489   BFD_ASSERT (s != NULL);
3490 
3491   newsize = s->size + bed->s->sizeof_dyn;
3492   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3493   if (newcontents == NULL)
3494     return FALSE;
3495 
3496   dyn.d_tag = tag;
3497   dyn.d_un.d_val = val;
3498   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3499 
3500   s->size = newsize;
3501   s->contents = newcontents;
3502 
3503   return TRUE;
3504 }
3505 
3506 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3507    otherwise just check whether one already exists.  Returns -1 on error,
3508    1 if a DT_NEEDED tag already exists, and 0 on success.  */
3509 
3510 static int
3511 elf_add_dt_needed_tag (bfd *abfd,
3512 		       struct bfd_link_info *info,
3513 		       const char *soname,
3514 		       bfd_boolean do_it)
3515 {
3516   struct elf_link_hash_table *hash_table;
3517   size_t strindex;
3518 
3519   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3520     return -1;
3521 
3522   hash_table = elf_hash_table (info);
3523   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3524   if (strindex == (size_t) -1)
3525     return -1;
3526 
3527   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3528     {
3529       asection *sdyn;
3530       const struct elf_backend_data *bed;
3531       bfd_byte *extdyn;
3532 
3533       bed = get_elf_backend_data (hash_table->dynobj);
3534       sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3535       if (sdyn != NULL)
3536 	for (extdyn = sdyn->contents;
3537 	     extdyn < sdyn->contents + sdyn->size;
3538 	     extdyn += bed->s->sizeof_dyn)
3539 	  {
3540 	    Elf_Internal_Dyn dyn;
3541 
3542 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3543 	    if (dyn.d_tag == DT_NEEDED
3544 		&& dyn.d_un.d_val == strindex)
3545 	      {
3546 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3547 		return 1;
3548 	      }
3549 	  }
3550     }
3551 
3552   if (do_it)
3553     {
3554       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3555 	return -1;
3556 
3557       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3558 	return -1;
3559     }
3560   else
3561     /* We were just checking for existence of the tag.  */
3562     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3563 
3564   return 0;
3565 }
3566 
3567 /* Return true if SONAME is on the needed list between NEEDED and STOP
3568    (or the end of list if STOP is NULL), and needed by a library that
3569    will be loaded.  */
3570 
3571 static bfd_boolean
3572 on_needed_list (const char *soname,
3573 		struct bfd_link_needed_list *needed,
3574 		struct bfd_link_needed_list *stop)
3575 {
3576   struct bfd_link_needed_list *look;
3577   for (look = needed; look != stop; look = look->next)
3578     if (strcmp (soname, look->name) == 0
3579 	&& ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3580 	    /* If needed by a library that itself is not directly
3581 	       needed, recursively check whether that library is
3582 	       indirectly needed.  Since we add DT_NEEDED entries to
3583 	       the end of the list, library dependencies appear after
3584 	       the library.  Therefore search prior to the current
3585 	       LOOK, preventing possible infinite recursion.  */
3586 	    || on_needed_list (elf_dt_name (look->by), needed, look)))
3587       return TRUE;
3588 
3589   return FALSE;
3590 }
3591 
3592 /* Sort symbol by value, section, size, and type.  */
3593 static int
3594 elf_sort_symbol (const void *arg1, const void *arg2)
3595 {
3596   const struct elf_link_hash_entry *h1;
3597   const struct elf_link_hash_entry *h2;
3598   bfd_signed_vma vdiff;
3599   int sdiff;
3600   const char *n1;
3601   const char *n2;
3602 
3603   h1 = *(const struct elf_link_hash_entry **) arg1;
3604   h2 = *(const struct elf_link_hash_entry **) arg2;
3605   vdiff = h1->root.u.def.value - h2->root.u.def.value;
3606   if (vdiff != 0)
3607     return vdiff > 0 ? 1 : -1;
3608 
3609   sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3610   if (sdiff != 0)
3611     return sdiff;
3612 
3613   /* Sort so that sized symbols are selected over zero size symbols.  */
3614   vdiff = h1->size - h2->size;
3615   if (vdiff != 0)
3616     return vdiff > 0 ? 1 : -1;
3617 
3618   /* Sort so that STT_OBJECT is selected over STT_NOTYPE.  */
3619   if (h1->type != h2->type)
3620     return h1->type - h2->type;
3621 
3622   /* If symbols are properly sized and typed, and multiple strong
3623      aliases are not defined in a shared library by the user we
3624      shouldn't get here.  Unfortunately linker script symbols like
3625      __bss_start sometimes match a user symbol defined at the start of
3626      .bss without proper size and type.  We'd like to preference the
3627      user symbol over reserved system symbols.  Sort on leading
3628      underscores.  */
3629   n1 = h1->root.root.string;
3630   n2 = h2->root.root.string;
3631   while (*n1 == *n2)
3632     {
3633       if (*n1 == 0)
3634 	break;
3635       ++n1;
3636       ++n2;
3637     }
3638   if (*n1 == '_')
3639     return -1;
3640   if (*n2 == '_')
3641     return 1;
3642 
3643   /* Final sort on name selects user symbols like '_u' over reserved
3644      system symbols like '_Z' and also will avoid qsort instability.  */
3645   return *n1 - *n2;
3646 }
3647 
3648 /* This function is used to adjust offsets into .dynstr for
3649    dynamic symbols.  This is called via elf_link_hash_traverse.  */
3650 
3651 static bfd_boolean
3652 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3653 {
3654   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3655 
3656   if (h->dynindx != -1)
3657     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3658   return TRUE;
3659 }
3660 
3661 /* Assign string offsets in .dynstr, update all structures referencing
3662    them.  */
3663 
3664 static bfd_boolean
3665 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3666 {
3667   struct elf_link_hash_table *hash_table = elf_hash_table (info);
3668   struct elf_link_local_dynamic_entry *entry;
3669   struct elf_strtab_hash *dynstr = hash_table->dynstr;
3670   bfd *dynobj = hash_table->dynobj;
3671   asection *sdyn;
3672   bfd_size_type size;
3673   const struct elf_backend_data *bed;
3674   bfd_byte *extdyn;
3675 
3676   _bfd_elf_strtab_finalize (dynstr);
3677   size = _bfd_elf_strtab_size (dynstr);
3678 
3679   bed = get_elf_backend_data (dynobj);
3680   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3681   BFD_ASSERT (sdyn != NULL);
3682 
3683   /* Update all .dynamic entries referencing .dynstr strings.  */
3684   for (extdyn = sdyn->contents;
3685        extdyn < sdyn->contents + sdyn->size;
3686        extdyn += bed->s->sizeof_dyn)
3687     {
3688       Elf_Internal_Dyn dyn;
3689 
3690       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3691       switch (dyn.d_tag)
3692 	{
3693 	case DT_STRSZ:
3694 	  dyn.d_un.d_val = size;
3695 	  break;
3696 	case DT_NEEDED:
3697 	case DT_SONAME:
3698 	case DT_RPATH:
3699 	case DT_RUNPATH:
3700 	case DT_FILTER:
3701 	case DT_AUXILIARY:
3702 	case DT_AUDIT:
3703 	case DT_DEPAUDIT:
3704 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3705 	  break;
3706 	default:
3707 	  continue;
3708 	}
3709       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3710     }
3711 
3712   /* Now update local dynamic symbols.  */
3713   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3714     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3715 						  entry->isym.st_name);
3716 
3717   /* And the rest of dynamic symbols.  */
3718   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3719 
3720   /* Adjust version definitions.  */
3721   if (elf_tdata (output_bfd)->cverdefs)
3722     {
3723       asection *s;
3724       bfd_byte *p;
3725       size_t i;
3726       Elf_Internal_Verdef def;
3727       Elf_Internal_Verdaux defaux;
3728 
3729       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3730       p = s->contents;
3731       do
3732 	{
3733 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3734 				   &def);
3735 	  p += sizeof (Elf_External_Verdef);
3736 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
3737 	    continue;
3738 	  for (i = 0; i < def.vd_cnt; ++i)
3739 	    {
3740 	      _bfd_elf_swap_verdaux_in (output_bfd,
3741 					(Elf_External_Verdaux *) p, &defaux);
3742 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3743 							defaux.vda_name);
3744 	      _bfd_elf_swap_verdaux_out (output_bfd,
3745 					 &defaux, (Elf_External_Verdaux *) p);
3746 	      p += sizeof (Elf_External_Verdaux);
3747 	    }
3748 	}
3749       while (def.vd_next);
3750     }
3751 
3752   /* Adjust version references.  */
3753   if (elf_tdata (output_bfd)->verref)
3754     {
3755       asection *s;
3756       bfd_byte *p;
3757       size_t i;
3758       Elf_Internal_Verneed need;
3759       Elf_Internal_Vernaux needaux;
3760 
3761       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3762       p = s->contents;
3763       do
3764 	{
3765 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3766 				    &need);
3767 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3768 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
3769 				     (Elf_External_Verneed *) p);
3770 	  p += sizeof (Elf_External_Verneed);
3771 	  for (i = 0; i < need.vn_cnt; ++i)
3772 	    {
3773 	      _bfd_elf_swap_vernaux_in (output_bfd,
3774 					(Elf_External_Vernaux *) p, &needaux);
3775 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3776 							 needaux.vna_name);
3777 	      _bfd_elf_swap_vernaux_out (output_bfd,
3778 					 &needaux,
3779 					 (Elf_External_Vernaux *) p);
3780 	      p += sizeof (Elf_External_Vernaux);
3781 	    }
3782 	}
3783       while (need.vn_next);
3784     }
3785 
3786   return TRUE;
3787 }
3788 
3789 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3790    The default is to only match when the INPUT and OUTPUT are exactly
3791    the same target.  */
3792 
3793 bfd_boolean
3794 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3795 				    const bfd_target *output)
3796 {
3797   return input == output;
3798 }
3799 
3800 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3801    This version is used when different targets for the same architecture
3802    are virtually identical.  */
3803 
3804 bfd_boolean
3805 _bfd_elf_relocs_compatible (const bfd_target *input,
3806 			    const bfd_target *output)
3807 {
3808   const struct elf_backend_data *obed, *ibed;
3809 
3810   if (input == output)
3811     return TRUE;
3812 
3813   ibed = xvec_get_elf_backend_data (input);
3814   obed = xvec_get_elf_backend_data (output);
3815 
3816   if (ibed->arch != obed->arch)
3817     return FALSE;
3818 
3819   /* If both backends are using this function, deem them compatible.  */
3820   return ibed->relocs_compatible == obed->relocs_compatible;
3821 }
3822 
3823 /* Make a special call to the linker "notice" function to tell it that
3824    we are about to handle an as-needed lib, or have finished
3825    processing the lib.  */
3826 
3827 bfd_boolean
3828 _bfd_elf_notice_as_needed (bfd *ibfd,
3829 			   struct bfd_link_info *info,
3830 			   enum notice_asneeded_action act)
3831 {
3832   return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3833 }
3834 
3835 /* Check relocations an ELF object file.  */
3836 
3837 bfd_boolean
3838 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3839 {
3840   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3841   struct elf_link_hash_table *htab = elf_hash_table (info);
3842 
3843   /* If this object is the same format as the output object, and it is
3844      not a shared library, then let the backend look through the
3845      relocs.
3846 
3847      This is required to build global offset table entries and to
3848      arrange for dynamic relocs.  It is not required for the
3849      particular common case of linking non PIC code, even when linking
3850      against shared libraries, but unfortunately there is no way of
3851      knowing whether an object file has been compiled PIC or not.
3852      Looking through the relocs is not particularly time consuming.
3853      The problem is that we must either (1) keep the relocs in memory,
3854      which causes the linker to require additional runtime memory or
3855      (2) read the relocs twice from the input file, which wastes time.
3856      This would be a good case for using mmap.
3857 
3858      I have no idea how to handle linking PIC code into a file of a
3859      different format.  It probably can't be done.  */
3860   if ((abfd->flags & DYNAMIC) == 0
3861       && is_elf_hash_table (htab)
3862       && bed->check_relocs != NULL
3863       && elf_object_id (abfd) == elf_hash_table_id (htab)
3864       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3865     {
3866       asection *o;
3867 
3868       for (o = abfd->sections; o != NULL; o = o->next)
3869 	{
3870 	  Elf_Internal_Rela *internal_relocs;
3871 	  bfd_boolean ok;
3872 
3873 	  /* Don't check relocations in excluded sections.  */
3874 	  if ((o->flags & SEC_RELOC) == 0
3875 	      || (o->flags & SEC_EXCLUDE) != 0
3876 	      || o->reloc_count == 0
3877 	      || ((info->strip == strip_all || info->strip == strip_debugger)
3878 		  && (o->flags & SEC_DEBUGGING) != 0)
3879 	      || bfd_is_abs_section (o->output_section))
3880 	    continue;
3881 
3882 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3883 						       info->keep_memory);
3884 	  if (internal_relocs == NULL)
3885 	    return FALSE;
3886 
3887 	  ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3888 
3889 	  if (elf_section_data (o)->relocs != internal_relocs)
3890 	    free (internal_relocs);
3891 
3892 	  if (! ok)
3893 	    return FALSE;
3894 	}
3895     }
3896 
3897   return TRUE;
3898 }
3899 
3900 /* Add symbols from an ELF object file to the linker hash table.  */
3901 
3902 static bfd_boolean
3903 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3904 {
3905   Elf_Internal_Ehdr *ehdr;
3906   Elf_Internal_Shdr *hdr;
3907   size_t symcount;
3908   size_t extsymcount;
3909   size_t extsymoff;
3910   struct elf_link_hash_entry **sym_hash;
3911   bfd_boolean dynamic;
3912   Elf_External_Versym *extversym = NULL;
3913   Elf_External_Versym *extversym_end = NULL;
3914   Elf_External_Versym *ever;
3915   struct elf_link_hash_entry *weaks;
3916   struct elf_link_hash_entry **nondeflt_vers = NULL;
3917   size_t nondeflt_vers_cnt = 0;
3918   Elf_Internal_Sym *isymbuf = NULL;
3919   Elf_Internal_Sym *isym;
3920   Elf_Internal_Sym *isymend;
3921   const struct elf_backend_data *bed;
3922   bfd_boolean add_needed;
3923   struct elf_link_hash_table *htab;
3924   bfd_size_type amt;
3925   void *alloc_mark = NULL;
3926   struct bfd_hash_entry **old_table = NULL;
3927   unsigned int old_size = 0;
3928   unsigned int old_count = 0;
3929   void *old_tab = NULL;
3930   void *old_ent;
3931   struct bfd_link_hash_entry *old_undefs = NULL;
3932   struct bfd_link_hash_entry *old_undefs_tail = NULL;
3933   void *old_strtab = NULL;
3934   size_t tabsize = 0;
3935   asection *s;
3936   bfd_boolean just_syms;
3937 
3938   htab = elf_hash_table (info);
3939   bed = get_elf_backend_data (abfd);
3940 
3941   if ((abfd->flags & DYNAMIC) == 0)
3942     dynamic = FALSE;
3943   else
3944     {
3945       dynamic = TRUE;
3946 
3947       /* You can't use -r against a dynamic object.  Also, there's no
3948 	 hope of using a dynamic object which does not exactly match
3949 	 the format of the output file.  */
3950       if (bfd_link_relocatable (info)
3951 	  || !is_elf_hash_table (htab)
3952 	  || info->output_bfd->xvec != abfd->xvec)
3953 	{
3954 	  if (bfd_link_relocatable (info))
3955 	    bfd_set_error (bfd_error_invalid_operation);
3956 	  else
3957 	    bfd_set_error (bfd_error_wrong_format);
3958 	  goto error_return;
3959 	}
3960     }
3961 
3962   ehdr = elf_elfheader (abfd);
3963   if (info->warn_alternate_em
3964       && bed->elf_machine_code != ehdr->e_machine
3965       && ((bed->elf_machine_alt1 != 0
3966 	   && ehdr->e_machine == bed->elf_machine_alt1)
3967 	  || (bed->elf_machine_alt2 != 0
3968 	      && ehdr->e_machine == bed->elf_machine_alt2)))
3969     _bfd_error_handler
3970       /* xgettext:c-format */
3971       (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3972        ehdr->e_machine, abfd, bed->elf_machine_code);
3973 
3974   /* As a GNU extension, any input sections which are named
3975      .gnu.warning.SYMBOL are treated as warning symbols for the given
3976      symbol.  This differs from .gnu.warning sections, which generate
3977      warnings when they are included in an output file.  */
3978   /* PR 12761: Also generate this warning when building shared libraries.  */
3979   for (s = abfd->sections; s != NULL; s = s->next)
3980     {
3981       const char *name;
3982 
3983       name = bfd_section_name (s);
3984       if (CONST_STRNEQ (name, ".gnu.warning."))
3985 	{
3986 	  char *msg;
3987 	  bfd_size_type sz;
3988 
3989 	  name += sizeof ".gnu.warning." - 1;
3990 
3991 	  /* If this is a shared object, then look up the symbol
3992 	     in the hash table.  If it is there, and it is already
3993 	     been defined, then we will not be using the entry
3994 	     from this shared object, so we don't need to warn.
3995 	     FIXME: If we see the definition in a regular object
3996 	     later on, we will warn, but we shouldn't.  The only
3997 	     fix is to keep track of what warnings we are supposed
3998 	     to emit, and then handle them all at the end of the
3999 	     link.  */
4000 	  if (dynamic)
4001 	    {
4002 	      struct elf_link_hash_entry *h;
4003 
4004 	      h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4005 
4006 	      /* FIXME: What about bfd_link_hash_common?  */
4007 	      if (h != NULL
4008 		  && (h->root.type == bfd_link_hash_defined
4009 		      || h->root.type == bfd_link_hash_defweak))
4010 		continue;
4011 	    }
4012 
4013 	  sz = s->size;
4014 	  msg = (char *) bfd_alloc (abfd, sz + 1);
4015 	  if (msg == NULL)
4016 	    goto error_return;
4017 
4018 	  if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4019 	    goto error_return;
4020 
4021 	  msg[sz] = '\0';
4022 
4023 	  if (! (_bfd_generic_link_add_one_symbol
4024 		 (info, abfd, name, BSF_WARNING, s, 0, msg,
4025 		  FALSE, bed->collect, NULL)))
4026 	    goto error_return;
4027 
4028 	  if (bfd_link_executable (info))
4029 	    {
4030 	      /* Clobber the section size so that the warning does
4031 		 not get copied into the output file.  */
4032 	      s->size = 0;
4033 
4034 	      /* Also set SEC_EXCLUDE, so that symbols defined in
4035 		 the warning section don't get copied to the output.  */
4036 	      s->flags |= SEC_EXCLUDE;
4037 	    }
4038 	}
4039     }
4040 
4041   just_syms = ((s = abfd->sections) != NULL
4042 	       && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4043 
4044   add_needed = TRUE;
4045   if (! dynamic)
4046     {
4047       /* If we are creating a shared library, create all the dynamic
4048 	 sections immediately.  We need to attach them to something,
4049 	 so we attach them to this BFD, provided it is the right
4050 	 format and is not from ld --just-symbols.  Always create the
4051 	 dynamic sections for -E/--dynamic-list.  FIXME: If there
4052 	 are no input BFD's of the same format as the output, we can't
4053 	 make a shared library.  */
4054       if (!just_syms
4055 	  && (bfd_link_pic (info)
4056 	      || (!bfd_link_relocatable (info)
4057 		  && info->nointerp
4058 		  && (info->export_dynamic || info->dynamic)))
4059 	  && is_elf_hash_table (htab)
4060 	  && info->output_bfd->xvec == abfd->xvec
4061 	  && !htab->dynamic_sections_created)
4062 	{
4063 	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4064 	    goto error_return;
4065 	}
4066     }
4067   else if (!is_elf_hash_table (htab))
4068     goto error_return;
4069   else
4070     {
4071       const char *soname = NULL;
4072       char *audit = NULL;
4073       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4074       const Elf_Internal_Phdr *phdr;
4075       int ret;
4076 
4077       /* ld --just-symbols and dynamic objects don't mix very well.
4078 	 ld shouldn't allow it.  */
4079       if (just_syms)
4080 	abort ();
4081 
4082       /* If this dynamic lib was specified on the command line with
4083 	 --as-needed in effect, then we don't want to add a DT_NEEDED
4084 	 tag unless the lib is actually used.  Similary for libs brought
4085 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
4086 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4087 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
4088 	 all.  */
4089       add_needed = (elf_dyn_lib_class (abfd)
4090 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
4091 		       | DYN_NO_NEEDED)) == 0;
4092 
4093       s = bfd_get_section_by_name (abfd, ".dynamic");
4094       if (s != NULL)
4095 	{
4096 	  bfd_byte *dynbuf;
4097 	  bfd_byte *extdyn;
4098 	  unsigned int elfsec;
4099 	  unsigned long shlink;
4100 
4101 	  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4102 	    {
4103 error_free_dyn:
4104 	      free (dynbuf);
4105 	      goto error_return;
4106 	    }
4107 
4108 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4109 	  if (elfsec == SHN_BAD)
4110 	    goto error_free_dyn;
4111 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4112 
4113 	  for (extdyn = dynbuf;
4114 	       extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4115 	       extdyn += bed->s->sizeof_dyn)
4116 	    {
4117 	      Elf_Internal_Dyn dyn;
4118 
4119 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4120 	      if (dyn.d_tag == DT_SONAME)
4121 		{
4122 		  unsigned int tagv = dyn.d_un.d_val;
4123 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4124 		  if (soname == NULL)
4125 		    goto error_free_dyn;
4126 		}
4127 	      if (dyn.d_tag == DT_NEEDED)
4128 		{
4129 		  struct bfd_link_needed_list *n, **pn;
4130 		  char *fnm, *anm;
4131 		  unsigned int tagv = dyn.d_un.d_val;
4132 
4133 		  amt = sizeof (struct bfd_link_needed_list);
4134 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4135 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4136 		  if (n == NULL || fnm == NULL)
4137 		    goto error_free_dyn;
4138 		  amt = strlen (fnm) + 1;
4139 		  anm = (char *) bfd_alloc (abfd, amt);
4140 		  if (anm == NULL)
4141 		    goto error_free_dyn;
4142 		  memcpy (anm, fnm, amt);
4143 		  n->name = anm;
4144 		  n->by = abfd;
4145 		  n->next = NULL;
4146 		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4147 		    ;
4148 		  *pn = n;
4149 		}
4150 	      if (dyn.d_tag == DT_RUNPATH)
4151 		{
4152 		  struct bfd_link_needed_list *n, **pn;
4153 		  char *fnm, *anm;
4154 		  unsigned int tagv = dyn.d_un.d_val;
4155 
4156 		  amt = sizeof (struct bfd_link_needed_list);
4157 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4158 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4159 		  if (n == NULL || fnm == NULL)
4160 		    goto error_free_dyn;
4161 		  amt = strlen (fnm) + 1;
4162 		  anm = (char *) bfd_alloc (abfd, amt);
4163 		  if (anm == NULL)
4164 		    goto error_free_dyn;
4165 		  memcpy (anm, fnm, amt);
4166 		  n->name = anm;
4167 		  n->by = abfd;
4168 		  n->next = NULL;
4169 		  for (pn = & runpath;
4170 		       *pn != NULL;
4171 		       pn = &(*pn)->next)
4172 		    ;
4173 		  *pn = n;
4174 		}
4175 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
4176 	      if (!runpath && dyn.d_tag == DT_RPATH)
4177 		{
4178 		  struct bfd_link_needed_list *n, **pn;
4179 		  char *fnm, *anm;
4180 		  unsigned int tagv = dyn.d_un.d_val;
4181 
4182 		  amt = sizeof (struct bfd_link_needed_list);
4183 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4184 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4185 		  if (n == NULL || fnm == NULL)
4186 		    goto error_free_dyn;
4187 		  amt = strlen (fnm) + 1;
4188 		  anm = (char *) bfd_alloc (abfd, amt);
4189 		  if (anm == NULL)
4190 		    goto error_free_dyn;
4191 		  memcpy (anm, fnm, amt);
4192 		  n->name = anm;
4193 		  n->by = abfd;
4194 		  n->next = NULL;
4195 		  for (pn = & rpath;
4196 		       *pn != NULL;
4197 		       pn = &(*pn)->next)
4198 		    ;
4199 		  *pn = n;
4200 		}
4201 	      if (dyn.d_tag == DT_AUDIT)
4202 		{
4203 		  unsigned int tagv = dyn.d_un.d_val;
4204 		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4205 		}
4206 	    }
4207 
4208 	  free (dynbuf);
4209 	}
4210 
4211       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
4212 	 frees all more recently bfd_alloc'd blocks as well.  */
4213       if (runpath)
4214 	rpath = runpath;
4215 
4216       if (rpath)
4217 	{
4218 	  struct bfd_link_needed_list **pn;
4219 	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4220 	    ;
4221 	  *pn = rpath;
4222 	}
4223 
4224       /* If we have a PT_GNU_RELRO program header, mark as read-only
4225 	 all sections contained fully therein.  This makes relro
4226 	 shared library sections appear as they will at run-time.  */
4227       phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4228       while (phdr-- > elf_tdata (abfd)->phdr)
4229 	if (phdr->p_type == PT_GNU_RELRO)
4230 	  {
4231 	    for (s = abfd->sections; s != NULL; s = s->next)
4232 	      if ((s->flags & SEC_ALLOC) != 0
4233 		  && s->vma >= phdr->p_vaddr
4234 		  && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4235 		s->flags |= SEC_READONLY;
4236 	    break;
4237 	  }
4238 
4239       /* We do not want to include any of the sections in a dynamic
4240 	 object in the output file.  We hack by simply clobbering the
4241 	 list of sections in the BFD.  This could be handled more
4242 	 cleanly by, say, a new section flag; the existing
4243 	 SEC_NEVER_LOAD flag is not the one we want, because that one
4244 	 still implies that the section takes up space in the output
4245 	 file.  */
4246       bfd_section_list_clear (abfd);
4247 
4248       /* Find the name to use in a DT_NEEDED entry that refers to this
4249 	 object.  If the object has a DT_SONAME entry, we use it.
4250 	 Otherwise, if the generic linker stuck something in
4251 	 elf_dt_name, we use that.  Otherwise, we just use the file
4252 	 name.  */
4253       if (soname == NULL || *soname == '\0')
4254 	{
4255 	  soname = elf_dt_name (abfd);
4256 	  if (soname == NULL || *soname == '\0')
4257 	    soname = bfd_get_filename (abfd);
4258 	}
4259 
4260       /* Save the SONAME because sometimes the linker emulation code
4261 	 will need to know it.  */
4262       elf_dt_name (abfd) = soname;
4263 
4264       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4265       if (ret < 0)
4266 	goto error_return;
4267 
4268       /* If we have already included this dynamic object in the
4269 	 link, just ignore it.  There is no reason to include a
4270 	 particular dynamic object more than once.  */
4271       if (ret > 0)
4272 	return TRUE;
4273 
4274       /* Save the DT_AUDIT entry for the linker emulation code. */
4275       elf_dt_audit (abfd) = audit;
4276     }
4277 
4278   /* If this is a dynamic object, we always link against the .dynsym
4279      symbol table, not the .symtab symbol table.  The dynamic linker
4280      will only see the .dynsym symbol table, so there is no reason to
4281      look at .symtab for a dynamic object.  */
4282 
4283   if (! dynamic || elf_dynsymtab (abfd) == 0)
4284     hdr = &elf_tdata (abfd)->symtab_hdr;
4285   else
4286     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4287 
4288   symcount = hdr->sh_size / bed->s->sizeof_sym;
4289 
4290   /* The sh_info field of the symtab header tells us where the
4291      external symbols start.  We don't care about the local symbols at
4292      this point.  */
4293   if (elf_bad_symtab (abfd))
4294     {
4295       extsymcount = symcount;
4296       extsymoff = 0;
4297     }
4298   else
4299     {
4300       extsymcount = symcount - hdr->sh_info;
4301       extsymoff = hdr->sh_info;
4302     }
4303 
4304   sym_hash = elf_sym_hashes (abfd);
4305   if (extsymcount != 0)
4306     {
4307       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4308 				      NULL, NULL, NULL);
4309       if (isymbuf == NULL)
4310 	goto error_return;
4311 
4312       if (sym_hash == NULL)
4313 	{
4314 	  /* We store a pointer to the hash table entry for each
4315 	     external symbol.  */
4316 	  amt = extsymcount;
4317 	  amt *= sizeof (struct elf_link_hash_entry *);
4318 	  sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4319 	  if (sym_hash == NULL)
4320 	    goto error_free_sym;
4321 	  elf_sym_hashes (abfd) = sym_hash;
4322 	}
4323     }
4324 
4325   if (dynamic)
4326     {
4327       /* Read in any version definitions.  */
4328       if (!_bfd_elf_slurp_version_tables (abfd,
4329 					  info->default_imported_symver))
4330 	goto error_free_sym;
4331 
4332       /* Read in the symbol versions, but don't bother to convert them
4333 	 to internal format.  */
4334       if (elf_dynversym (abfd) != 0)
4335 	{
4336 	  Elf_Internal_Shdr *versymhdr;
4337 
4338 	  versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4339 	  amt = versymhdr->sh_size;
4340 	  extversym = (Elf_External_Versym *) bfd_malloc (amt);
4341 	  if (extversym == NULL)
4342 	    goto error_free_sym;
4343 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4344 	      || bfd_bread (extversym, amt, abfd) != amt)
4345 	    goto error_free_vers;
4346 	  extversym_end = extversym + (amt / sizeof (* extversym));
4347 	}
4348     }
4349 
4350   /* If we are loading an as-needed shared lib, save the symbol table
4351      state before we start adding symbols.  If the lib turns out
4352      to be unneeded, restore the state.  */
4353   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4354     {
4355       unsigned int i;
4356       size_t entsize;
4357 
4358       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4359 	{
4360 	  struct bfd_hash_entry *p;
4361 	  struct elf_link_hash_entry *h;
4362 
4363 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4364 	    {
4365 	      h = (struct elf_link_hash_entry *) p;
4366 	      entsize += htab->root.table.entsize;
4367 	      if (h->root.type == bfd_link_hash_warning)
4368 		entsize += htab->root.table.entsize;
4369 	    }
4370 	}
4371 
4372       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4373       old_tab = bfd_malloc (tabsize + entsize);
4374       if (old_tab == NULL)
4375 	goto error_free_vers;
4376 
4377       /* Remember the current objalloc pointer, so that all mem for
4378 	 symbols added can later be reclaimed.  */
4379       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4380       if (alloc_mark == NULL)
4381 	goto error_free_vers;
4382 
4383       /* Make a special call to the linker "notice" function to
4384 	 tell it that we are about to handle an as-needed lib.  */
4385       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4386 	goto error_free_vers;
4387 
4388       /* Clone the symbol table.  Remember some pointers into the
4389 	 symbol table, and dynamic symbol count.  */
4390       old_ent = (char *) old_tab + tabsize;
4391       memcpy (old_tab, htab->root.table.table, tabsize);
4392       old_undefs = htab->root.undefs;
4393       old_undefs_tail = htab->root.undefs_tail;
4394       old_table = htab->root.table.table;
4395       old_size = htab->root.table.size;
4396       old_count = htab->root.table.count;
4397       old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4398       if (old_strtab == NULL)
4399 	goto error_free_vers;
4400 
4401       for (i = 0; i < htab->root.table.size; i++)
4402 	{
4403 	  struct bfd_hash_entry *p;
4404 	  struct elf_link_hash_entry *h;
4405 
4406 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4407 	    {
4408 	      memcpy (old_ent, p, htab->root.table.entsize);
4409 	      old_ent = (char *) old_ent + htab->root.table.entsize;
4410 	      h = (struct elf_link_hash_entry *) p;
4411 	      if (h->root.type == bfd_link_hash_warning)
4412 		{
4413 		  memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4414 		  old_ent = (char *) old_ent + htab->root.table.entsize;
4415 		}
4416 	    }
4417 	}
4418     }
4419 
4420   weaks = NULL;
4421   if (extversym == NULL)
4422     ever = NULL;
4423   else if (extversym + extsymoff < extversym_end)
4424     ever = extversym + extsymoff;
4425   else
4426     {
4427       /* xgettext:c-format */
4428       _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4429 			  abfd, (long) extsymoff,
4430 			  (long) (extversym_end - extversym) / sizeof (* extversym));
4431       bfd_set_error (bfd_error_bad_value);
4432       goto error_free_vers;
4433     }
4434 
4435   if (!bfd_link_relocatable (info)
4436       && abfd->lto_slim_object)
4437     {
4438       _bfd_error_handler
4439 	(_("%pB: plugin needed to handle lto object"), abfd);
4440     }
4441 
4442   for (isym = isymbuf, isymend = isymbuf + extsymcount;
4443        isym < isymend;
4444        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4445     {
4446       int bind;
4447       bfd_vma value;
4448       asection *sec, *new_sec;
4449       flagword flags;
4450       const char *name;
4451       struct elf_link_hash_entry *h;
4452       struct elf_link_hash_entry *hi;
4453       bfd_boolean definition;
4454       bfd_boolean size_change_ok;
4455       bfd_boolean type_change_ok;
4456       bfd_boolean new_weak;
4457       bfd_boolean old_weak;
4458       bfd_boolean override;
4459       bfd_boolean common;
4460       bfd_boolean discarded;
4461       unsigned int old_alignment;
4462       unsigned int shindex;
4463       bfd *old_bfd;
4464       bfd_boolean matched;
4465 
4466       override = FALSE;
4467 
4468       flags = BSF_NO_FLAGS;
4469       sec = NULL;
4470       value = isym->st_value;
4471       common = bed->common_definition (isym);
4472       if (common && info->inhibit_common_definition)
4473 	{
4474 	  /* Treat common symbol as undefined for --no-define-common.  */
4475 	  isym->st_shndx = SHN_UNDEF;
4476 	  common = FALSE;
4477 	}
4478       discarded = FALSE;
4479 
4480       bind = ELF_ST_BIND (isym->st_info);
4481       switch (bind)
4482 	{
4483 	case STB_LOCAL:
4484 	  /* This should be impossible, since ELF requires that all
4485 	     global symbols follow all local symbols, and that sh_info
4486 	     point to the first global symbol.  Unfortunately, Irix 5
4487 	     screws this up.  */
4488 	  if (elf_bad_symtab (abfd))
4489 	    continue;
4490 
4491 	  /* If we aren't prepared to handle locals within the globals
4492 	     then we'll likely segfault on a NULL symbol hash if the
4493 	     symbol is ever referenced in relocations.  */
4494 	  shindex = elf_elfheader (abfd)->e_shstrndx;
4495 	  name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4496 	  _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4497 				" (>= sh_info of %lu)"),
4498 			      abfd, name, (long) (isym - isymbuf + extsymoff),
4499 			      (long) extsymoff);
4500 
4501 	  /* Dynamic object relocations are not processed by ld, so
4502 	     ld won't run into the problem mentioned above.  */
4503 	  if (dynamic)
4504 	    continue;
4505 	  bfd_set_error (bfd_error_bad_value);
4506 	  goto error_free_vers;
4507 
4508 	case STB_GLOBAL:
4509 	  if (isym->st_shndx != SHN_UNDEF && !common)
4510 	    flags = BSF_GLOBAL;
4511 	  break;
4512 
4513 	case STB_WEAK:
4514 	  flags = BSF_WEAK;
4515 	  break;
4516 
4517 	case STB_GNU_UNIQUE:
4518 	  flags = BSF_GNU_UNIQUE;
4519 	  break;
4520 
4521 	default:
4522 	  /* Leave it up to the processor backend.  */
4523 	  break;
4524 	}
4525 
4526       if (isym->st_shndx == SHN_UNDEF)
4527 	sec = bfd_und_section_ptr;
4528       else if (isym->st_shndx == SHN_ABS)
4529 	sec = bfd_abs_section_ptr;
4530       else if (isym->st_shndx == SHN_COMMON)
4531 	{
4532 	  sec = bfd_com_section_ptr;
4533 	  /* What ELF calls the size we call the value.  What ELF
4534 	     calls the value we call the alignment.  */
4535 	  value = isym->st_size;
4536 	}
4537       else
4538 	{
4539 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4540 	  if (sec == NULL)
4541 	    sec = bfd_abs_section_ptr;
4542 	  else if (discarded_section (sec))
4543 	    {
4544 	      /* Symbols from discarded section are undefined.  We keep
4545 		 its visibility.  */
4546 	      sec = bfd_und_section_ptr;
4547 	      discarded = TRUE;
4548 	      isym->st_shndx = SHN_UNDEF;
4549 	    }
4550 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4551 	    value -= sec->vma;
4552 	}
4553 
4554       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4555 					      isym->st_name);
4556       if (name == NULL)
4557 	goto error_free_vers;
4558 
4559       if (isym->st_shndx == SHN_COMMON
4560 	  && (abfd->flags & BFD_PLUGIN) != 0)
4561 	{
4562 	  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4563 
4564 	  if (xc == NULL)
4565 	    {
4566 	      flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4567 				 | SEC_EXCLUDE);
4568 	      xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4569 	      if (xc == NULL)
4570 		goto error_free_vers;
4571 	    }
4572 	  sec = xc;
4573 	}
4574       else if (isym->st_shndx == SHN_COMMON
4575 	       && ELF_ST_TYPE (isym->st_info) == STT_TLS
4576 	       && !bfd_link_relocatable (info))
4577 	{
4578 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4579 
4580 	  if (tcomm == NULL)
4581 	    {
4582 	      flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4583 				 | SEC_LINKER_CREATED);
4584 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4585 	      if (tcomm == NULL)
4586 		goto error_free_vers;
4587 	    }
4588 	  sec = tcomm;
4589 	}
4590       else if (bed->elf_add_symbol_hook)
4591 	{
4592 	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4593 					     &sec, &value))
4594 	    goto error_free_vers;
4595 
4596 	  /* The hook function sets the name to NULL if this symbol
4597 	     should be skipped for some reason.  */
4598 	  if (name == NULL)
4599 	    continue;
4600 	}
4601 
4602       /* Sanity check that all possibilities were handled.  */
4603       if (sec == NULL)
4604 	abort ();
4605 
4606       /* Silently discard TLS symbols from --just-syms.  There's
4607 	 no way to combine a static TLS block with a new TLS block
4608 	 for this executable.  */
4609       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4610 	  && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4611 	continue;
4612 
4613       if (bfd_is_und_section (sec)
4614 	  || bfd_is_com_section (sec))
4615 	definition = FALSE;
4616       else
4617 	definition = TRUE;
4618 
4619       size_change_ok = FALSE;
4620       type_change_ok = bed->type_change_ok;
4621       old_weak = FALSE;
4622       matched = FALSE;
4623       old_alignment = 0;
4624       old_bfd = NULL;
4625       new_sec = sec;
4626 
4627       if (is_elf_hash_table (htab))
4628 	{
4629 	  Elf_Internal_Versym iver;
4630 	  unsigned int vernum = 0;
4631 	  bfd_boolean skip;
4632 
4633 	  if (ever == NULL)
4634 	    {
4635 	      if (info->default_imported_symver)
4636 		/* Use the default symbol version created earlier.  */
4637 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
4638 	      else
4639 		iver.vs_vers = 0;
4640 	    }
4641 	  else if (ever >= extversym_end)
4642 	    {
4643 	      /* xgettext:c-format */
4644 	      _bfd_error_handler (_("%pB: not enough version information"),
4645 				  abfd);
4646 	      bfd_set_error (bfd_error_bad_value);
4647 	      goto error_free_vers;
4648 	    }
4649 	  else
4650 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
4651 
4652 	  vernum = iver.vs_vers & VERSYM_VERSION;
4653 
4654 	  /* If this is a hidden symbol, or if it is not version
4655 	     1, we append the version name to the symbol name.
4656 	     However, we do not modify a non-hidden absolute symbol
4657 	     if it is not a function, because it might be the version
4658 	     symbol itself.  FIXME: What if it isn't?  */
4659 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4660 	      || (vernum > 1
4661 		  && (!bfd_is_abs_section (sec)
4662 		      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4663 	    {
4664 	      const char *verstr;
4665 	      size_t namelen, verlen, newlen;
4666 	      char *newname, *p;
4667 
4668 	      if (isym->st_shndx != SHN_UNDEF)
4669 		{
4670 		  if (vernum > elf_tdata (abfd)->cverdefs)
4671 		    verstr = NULL;
4672 		  else if (vernum > 1)
4673 		    verstr =
4674 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4675 		  else
4676 		    verstr = "";
4677 
4678 		  if (verstr == NULL)
4679 		    {
4680 		      _bfd_error_handler
4681 			/* xgettext:c-format */
4682 			(_("%pB: %s: invalid version %u (max %d)"),
4683 			 abfd, name, vernum,
4684 			 elf_tdata (abfd)->cverdefs);
4685 		      bfd_set_error (bfd_error_bad_value);
4686 		      goto error_free_vers;
4687 		    }
4688 		}
4689 	      else
4690 		{
4691 		  /* We cannot simply test for the number of
4692 		     entries in the VERNEED section since the
4693 		     numbers for the needed versions do not start
4694 		     at 0.  */
4695 		  Elf_Internal_Verneed *t;
4696 
4697 		  verstr = NULL;
4698 		  for (t = elf_tdata (abfd)->verref;
4699 		       t != NULL;
4700 		       t = t->vn_nextref)
4701 		    {
4702 		      Elf_Internal_Vernaux *a;
4703 
4704 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4705 			{
4706 			  if (a->vna_other == vernum)
4707 			    {
4708 			      verstr = a->vna_nodename;
4709 			      break;
4710 			    }
4711 			}
4712 		      if (a != NULL)
4713 			break;
4714 		    }
4715 		  if (verstr == NULL)
4716 		    {
4717 		      _bfd_error_handler
4718 			/* xgettext:c-format */
4719 			(_("%pB: %s: invalid needed version %d"),
4720 			 abfd, name, vernum);
4721 		      bfd_set_error (bfd_error_bad_value);
4722 		      goto error_free_vers;
4723 		    }
4724 		}
4725 
4726 	      namelen = strlen (name);
4727 	      verlen = strlen (verstr);
4728 	      newlen = namelen + verlen + 2;
4729 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4730 		  && isym->st_shndx != SHN_UNDEF)
4731 		++newlen;
4732 
4733 	      newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4734 	      if (newname == NULL)
4735 		goto error_free_vers;
4736 	      memcpy (newname, name, namelen);
4737 	      p = newname + namelen;
4738 	      *p++ = ELF_VER_CHR;
4739 	      /* If this is a defined non-hidden version symbol,
4740 		 we add another @ to the name.  This indicates the
4741 		 default version of the symbol.  */
4742 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4743 		  && isym->st_shndx != SHN_UNDEF)
4744 		*p++ = ELF_VER_CHR;
4745 	      memcpy (p, verstr, verlen + 1);
4746 
4747 	      name = newname;
4748 	    }
4749 
4750 	  /* If this symbol has default visibility and the user has
4751 	     requested we not re-export it, then mark it as hidden.  */
4752 	  if (!bfd_is_und_section (sec)
4753 	      && !dynamic
4754 	      && abfd->no_export
4755 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4756 	    isym->st_other = (STV_HIDDEN
4757 			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4758 
4759 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4760 				      sym_hash, &old_bfd, &old_weak,
4761 				      &old_alignment, &skip, &override,
4762 				      &type_change_ok, &size_change_ok,
4763 				      &matched))
4764 	    goto error_free_vers;
4765 
4766 	  if (skip)
4767 	    continue;
4768 
4769 	  /* Override a definition only if the new symbol matches the
4770 	     existing one.  */
4771 	  if (override && matched)
4772 	    definition = FALSE;
4773 
4774 	  h = *sym_hash;
4775 	  while (h->root.type == bfd_link_hash_indirect
4776 		 || h->root.type == bfd_link_hash_warning)
4777 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4778 
4779 	  if (elf_tdata (abfd)->verdef != NULL
4780 	      && vernum > 1
4781 	      && definition)
4782 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4783 	}
4784 
4785       if (! (_bfd_generic_link_add_one_symbol
4786 	     (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4787 	      (struct bfd_link_hash_entry **) sym_hash)))
4788 	goto error_free_vers;
4789 
4790       h = *sym_hash;
4791       /* We need to make sure that indirect symbol dynamic flags are
4792 	 updated.  */
4793       hi = h;
4794       while (h->root.type == bfd_link_hash_indirect
4795 	     || h->root.type == bfd_link_hash_warning)
4796 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
4797 
4798       /* Setting the index to -3 tells elf_link_output_extsym that
4799 	 this symbol is defined in a discarded section.  */
4800       if (discarded)
4801 	h->indx = -3;
4802 
4803       *sym_hash = h;
4804 
4805       new_weak = (flags & BSF_WEAK) != 0;
4806       if (dynamic
4807 	  && definition
4808 	  && new_weak
4809 	  && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4810 	  && is_elf_hash_table (htab)
4811 	  && h->u.alias == NULL)
4812 	{
4813 	  /* Keep a list of all weak defined non function symbols from
4814 	     a dynamic object, using the alias field.  Later in this
4815 	     function we will set the alias field to the correct
4816 	     value.  We only put non-function symbols from dynamic
4817 	     objects on this list, because that happens to be the only
4818 	     time we need to know the normal symbol corresponding to a
4819 	     weak symbol, and the information is time consuming to
4820 	     figure out.  If the alias field is not already NULL,
4821 	     then this symbol was already defined by some previous
4822 	     dynamic object, and we will be using that previous
4823 	     definition anyhow.  */
4824 
4825 	  h->u.alias = weaks;
4826 	  weaks = h;
4827 	}
4828 
4829       /* Set the alignment of a common symbol.  */
4830       if ((common || bfd_is_com_section (sec))
4831 	  && h->root.type == bfd_link_hash_common)
4832 	{
4833 	  unsigned int align;
4834 
4835 	  if (common)
4836 	    align = bfd_log2 (isym->st_value);
4837 	  else
4838 	    {
4839 	      /* The new symbol is a common symbol in a shared object.
4840 		 We need to get the alignment from the section.  */
4841 	      align = new_sec->alignment_power;
4842 	    }
4843 	  if (align > old_alignment)
4844 	    h->root.u.c.p->alignment_power = align;
4845 	  else
4846 	    h->root.u.c.p->alignment_power = old_alignment;
4847 	}
4848 
4849       if (is_elf_hash_table (htab))
4850 	{
4851 	  /* Set a flag in the hash table entry indicating the type of
4852 	     reference or definition we just found.  A dynamic symbol
4853 	     is one which is referenced or defined by both a regular
4854 	     object and a shared object.  */
4855 	  bfd_boolean dynsym = FALSE;
4856 
4857 	  /* Plugin symbols aren't normal.  Don't set def_regular or
4858 	     ref_regular for them, or make them dynamic.  */
4859 	  if ((abfd->flags & BFD_PLUGIN) != 0)
4860 	    ;
4861 	  else if (! dynamic)
4862 	    {
4863 	      if (! definition)
4864 		{
4865 		  h->ref_regular = 1;
4866 		  if (bind != STB_WEAK)
4867 		    h->ref_regular_nonweak = 1;
4868 		}
4869 	      else
4870 		{
4871 		  h->def_regular = 1;
4872 		  if (h->def_dynamic)
4873 		    {
4874 		      h->def_dynamic = 0;
4875 		      h->ref_dynamic = 1;
4876 		    }
4877 		}
4878 
4879 	      /* If the indirect symbol has been forced local, don't
4880 		 make the real symbol dynamic.  */
4881 	      if ((h == hi || !hi->forced_local)
4882 		  && (bfd_link_dll (info)
4883 		      || h->def_dynamic
4884 		      || h->ref_dynamic))
4885 		dynsym = TRUE;
4886 	    }
4887 	  else
4888 	    {
4889 	      if (! definition)
4890 		{
4891 		  h->ref_dynamic = 1;
4892 		  hi->ref_dynamic = 1;
4893 		}
4894 	      else
4895 		{
4896 		  h->def_dynamic = 1;
4897 		  hi->def_dynamic = 1;
4898 		}
4899 
4900 	      /* If the indirect symbol has been forced local, don't
4901 		 make the real symbol dynamic.  */
4902 	      if ((h == hi || !hi->forced_local)
4903 		  && (h->def_regular
4904 		      || h->ref_regular
4905 		      || (h->is_weakalias
4906 			  && weakdef (h)->dynindx != -1)))
4907 		dynsym = TRUE;
4908 	    }
4909 
4910 	  /* Check to see if we need to add an indirect symbol for
4911 	     the default name.  */
4912 	  if (definition
4913 	      || (!override && h->root.type == bfd_link_hash_common))
4914 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4915 					      sec, value, &old_bfd, &dynsym))
4916 	      goto error_free_vers;
4917 
4918 	  /* Check the alignment when a common symbol is involved. This
4919 	     can change when a common symbol is overridden by a normal
4920 	     definition or a common symbol is ignored due to the old
4921 	     normal definition. We need to make sure the maximum
4922 	     alignment is maintained.  */
4923 	  if ((old_alignment || common)
4924 	      && h->root.type != bfd_link_hash_common)
4925 	    {
4926 	      unsigned int common_align;
4927 	      unsigned int normal_align;
4928 	      unsigned int symbol_align;
4929 	      bfd *normal_bfd;
4930 	      bfd *common_bfd;
4931 
4932 	      BFD_ASSERT (h->root.type == bfd_link_hash_defined
4933 			  || h->root.type == bfd_link_hash_defweak);
4934 
4935 	      symbol_align = ffs (h->root.u.def.value) - 1;
4936 	      if (h->root.u.def.section->owner != NULL
4937 		  && (h->root.u.def.section->owner->flags
4938 		       & (DYNAMIC | BFD_PLUGIN)) == 0)
4939 		{
4940 		  normal_align = h->root.u.def.section->alignment_power;
4941 		  if (normal_align > symbol_align)
4942 		    normal_align = symbol_align;
4943 		}
4944 	      else
4945 		normal_align = symbol_align;
4946 
4947 	      if (old_alignment)
4948 		{
4949 		  common_align = old_alignment;
4950 		  common_bfd = old_bfd;
4951 		  normal_bfd = abfd;
4952 		}
4953 	      else
4954 		{
4955 		  common_align = bfd_log2 (isym->st_value);
4956 		  common_bfd = abfd;
4957 		  normal_bfd = old_bfd;
4958 		}
4959 
4960 	      if (normal_align < common_align)
4961 		{
4962 		  /* PR binutils/2735 */
4963 		  if (normal_bfd == NULL)
4964 		    _bfd_error_handler
4965 		      /* xgettext:c-format */
4966 		      (_("warning: alignment %u of common symbol `%s' in %pB is"
4967 			 " greater than the alignment (%u) of its section %pA"),
4968 		       1 << common_align, name, common_bfd,
4969 		       1 << normal_align, h->root.u.def.section);
4970 		  else
4971 		    _bfd_error_handler
4972 		      /* xgettext:c-format */
4973 		      (_("warning: alignment %u of symbol `%s' in %pB"
4974 			 " is smaller than %u in %pB"),
4975 		       1 << normal_align, name, normal_bfd,
4976 		       1 << common_align, common_bfd);
4977 		}
4978 	    }
4979 
4980 	  /* Remember the symbol size if it isn't undefined.  */
4981 	  if (isym->st_size != 0
4982 	      && isym->st_shndx != SHN_UNDEF
4983 	      && (definition || h->size == 0))
4984 	    {
4985 	      if (h->size != 0
4986 		  && h->size != isym->st_size
4987 		  && ! size_change_ok)
4988 		_bfd_error_handler
4989 		  /* xgettext:c-format */
4990 		  (_("warning: size of symbol `%s' changed"
4991 		     " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4992 		   name, (uint64_t) h->size, old_bfd,
4993 		   (uint64_t) isym->st_size, abfd);
4994 
4995 	      h->size = isym->st_size;
4996 	    }
4997 
4998 	  /* If this is a common symbol, then we always want H->SIZE
4999 	     to be the size of the common symbol.  The code just above
5000 	     won't fix the size if a common symbol becomes larger.  We
5001 	     don't warn about a size change here, because that is
5002 	     covered by --warn-common.  Allow changes between different
5003 	     function types.  */
5004 	  if (h->root.type == bfd_link_hash_common)
5005 	    h->size = h->root.u.c.size;
5006 
5007 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5008 	      && ((definition && !new_weak)
5009 		  || (old_weak && h->root.type == bfd_link_hash_common)
5010 		  || h->type == STT_NOTYPE))
5011 	    {
5012 	      unsigned int type = ELF_ST_TYPE (isym->st_info);
5013 
5014 	      /* Turn an IFUNC symbol from a DSO into a normal FUNC
5015 		 symbol.  */
5016 	      if (type == STT_GNU_IFUNC
5017 		  && (abfd->flags & DYNAMIC) != 0)
5018 		type = STT_FUNC;
5019 
5020 	      if (h->type != type)
5021 		{
5022 		  if (h->type != STT_NOTYPE && ! type_change_ok)
5023 		    /* xgettext:c-format */
5024 		    _bfd_error_handler
5025 		      (_("warning: type of symbol `%s' changed"
5026 			 " from %d to %d in %pB"),
5027 		       name, h->type, type, abfd);
5028 
5029 		  h->type = type;
5030 		}
5031 	    }
5032 
5033 	  /* Merge st_other field.  */
5034 	  elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5035 
5036 	  /* We don't want to make debug symbol dynamic.  */
5037 	  if (definition
5038 	      && (sec->flags & SEC_DEBUGGING)
5039 	      && !bfd_link_relocatable (info))
5040 	    dynsym = FALSE;
5041 
5042 	  /* Nor should we make plugin symbols dynamic.  */
5043 	  if ((abfd->flags & BFD_PLUGIN) != 0)
5044 	    dynsym = FALSE;
5045 
5046 	  if (definition)
5047 	    {
5048 	      h->target_internal = isym->st_target_internal;
5049 	      h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5050 	    }
5051 
5052 	  if (definition && !dynamic)
5053 	    {
5054 	      char *p = strchr (name, ELF_VER_CHR);
5055 	      if (p != NULL && p[1] != ELF_VER_CHR)
5056 		{
5057 		  /* Queue non-default versions so that .symver x, x@FOO
5058 		     aliases can be checked.  */
5059 		  if (!nondeflt_vers)
5060 		    {
5061 		      amt = ((isymend - isym + 1)
5062 			     * sizeof (struct elf_link_hash_entry *));
5063 		      nondeflt_vers
5064 			= (struct elf_link_hash_entry **) bfd_malloc (amt);
5065 		      if (!nondeflt_vers)
5066 			goto error_free_vers;
5067 		    }
5068 		  nondeflt_vers[nondeflt_vers_cnt++] = h;
5069 		}
5070 	    }
5071 
5072 	  if (dynsym && h->dynindx == -1)
5073 	    {
5074 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
5075 		goto error_free_vers;
5076 	      if (h->is_weakalias
5077 		  && weakdef (h)->dynindx == -1)
5078 		{
5079 		  if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5080 		    goto error_free_vers;
5081 		}
5082 	    }
5083 	  else if (h->dynindx != -1)
5084 	    /* If the symbol already has a dynamic index, but
5085 	       visibility says it should not be visible, turn it into
5086 	       a local symbol.  */
5087 	    switch (ELF_ST_VISIBILITY (h->other))
5088 	      {
5089 	      case STV_INTERNAL:
5090 	      case STV_HIDDEN:
5091 		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
5092 		dynsym = FALSE;
5093 		break;
5094 	      }
5095 
5096 	  /* Don't add DT_NEEDED for references from the dummy bfd nor
5097 	     for unmatched symbol.  */
5098 	  if (!add_needed
5099 	      && matched
5100 	      && definition
5101 	      && ((dynsym
5102 		   && h->ref_regular_nonweak
5103 		   && (old_bfd == NULL
5104 		       || (old_bfd->flags & BFD_PLUGIN) == 0))
5105 		  || (h->ref_dynamic_nonweak
5106 		      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5107 		      && !on_needed_list (elf_dt_name (abfd),
5108 					  htab->needed, NULL))))
5109 	    {
5110 	      int ret;
5111 	      const char *soname = elf_dt_name (abfd);
5112 
5113 	      info->callbacks->minfo ("%!", soname, old_bfd,
5114 				      h->root.root.string);
5115 
5116 	      /* A symbol from a library loaded via DT_NEEDED of some
5117 		 other library is referenced by a regular object.
5118 		 Add a DT_NEEDED entry for it.  Issue an error if
5119 		 --no-add-needed is used and the reference was not
5120 		 a weak one.  */
5121 	      if (old_bfd != NULL
5122 		  && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5123 		{
5124 		  _bfd_error_handler
5125 		    /* xgettext:c-format */
5126 		    (_("%pB: undefined reference to symbol '%s'"),
5127 		     old_bfd, name);
5128 		  bfd_set_error (bfd_error_missing_dso);
5129 		  goto error_free_vers;
5130 		}
5131 
5132 	      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5133 		(elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5134 
5135 	      add_needed = TRUE;
5136 	      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5137 	      if (ret < 0)
5138 		goto error_free_vers;
5139 
5140 	      BFD_ASSERT (ret == 0);
5141 	    }
5142 	}
5143     }
5144 
5145   if (info->lto_plugin_active
5146       && !bfd_link_relocatable (info)
5147       && (abfd->flags & BFD_PLUGIN) == 0
5148       && !just_syms
5149       && extsymcount)
5150     {
5151       int r_sym_shift;
5152 
5153       if (bed->s->arch_size == 32)
5154 	r_sym_shift = 8;
5155       else
5156 	r_sym_shift = 32;
5157 
5158       /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5159 	 referenced in regular objects so that linker plugin will get
5160 	 the correct symbol resolution.  */
5161 
5162       sym_hash = elf_sym_hashes (abfd);
5163       for (s = abfd->sections; s != NULL; s = s->next)
5164 	{
5165 	  Elf_Internal_Rela *internal_relocs;
5166 	  Elf_Internal_Rela *rel, *relend;
5167 
5168 	  /* Don't check relocations in excluded sections.  */
5169 	  if ((s->flags & SEC_RELOC) == 0
5170 	      || s->reloc_count == 0
5171 	      || (s->flags & SEC_EXCLUDE) != 0
5172 	      || ((info->strip == strip_all
5173 		   || info->strip == strip_debugger)
5174 		  && (s->flags & SEC_DEBUGGING) != 0))
5175 	    continue;
5176 
5177 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5178 						       NULL,
5179 						       info->keep_memory);
5180 	  if (internal_relocs == NULL)
5181 	    goto error_free_vers;
5182 
5183 	  rel = internal_relocs;
5184 	  relend = rel + s->reloc_count;
5185 	  for ( ; rel < relend; rel++)
5186 	    {
5187 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
5188 	      struct elf_link_hash_entry *h;
5189 
5190 	      /* Skip local symbols.  */
5191 	      if (r_symndx < extsymoff)
5192 		continue;
5193 
5194 	      h = sym_hash[r_symndx - extsymoff];
5195 	      if (h != NULL)
5196 		h->root.non_ir_ref_regular = 1;
5197 	    }
5198 
5199 	  if (elf_section_data (s)->relocs != internal_relocs)
5200 	    free (internal_relocs);
5201 	}
5202     }
5203 
5204   if (extversym != NULL)
5205     {
5206       free (extversym);
5207       extversym = NULL;
5208     }
5209 
5210   if (isymbuf != NULL)
5211     {
5212       free (isymbuf);
5213       isymbuf = NULL;
5214     }
5215 
5216   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5217     {
5218       unsigned int i;
5219 
5220       /* Restore the symbol table.  */
5221       old_ent = (char *) old_tab + tabsize;
5222       memset (elf_sym_hashes (abfd), 0,
5223 	      extsymcount * sizeof (struct elf_link_hash_entry *));
5224       htab->root.table.table = old_table;
5225       htab->root.table.size = old_size;
5226       htab->root.table.count = old_count;
5227       memcpy (htab->root.table.table, old_tab, tabsize);
5228       htab->root.undefs = old_undefs;
5229       htab->root.undefs_tail = old_undefs_tail;
5230       _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5231       free (old_strtab);
5232       old_strtab = NULL;
5233       for (i = 0; i < htab->root.table.size; i++)
5234 	{
5235 	  struct bfd_hash_entry *p;
5236 	  struct elf_link_hash_entry *h;
5237 	  bfd_size_type size;
5238 	  unsigned int alignment_power;
5239 	  unsigned int non_ir_ref_dynamic;
5240 
5241 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5242 	    {
5243 	      h = (struct elf_link_hash_entry *) p;
5244 	      if (h->root.type == bfd_link_hash_warning)
5245 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
5246 
5247 	      /* Preserve the maximum alignment and size for common
5248 		 symbols even if this dynamic lib isn't on DT_NEEDED
5249 		 since it can still be loaded at run time by another
5250 		 dynamic lib.  */
5251 	      if (h->root.type == bfd_link_hash_common)
5252 		{
5253 		  size = h->root.u.c.size;
5254 		  alignment_power = h->root.u.c.p->alignment_power;
5255 		}
5256 	      else
5257 		{
5258 		  size = 0;
5259 		  alignment_power = 0;
5260 		}
5261 	      /* Preserve non_ir_ref_dynamic so that this symbol
5262 		 will be exported when the dynamic lib becomes needed
5263 		 in the second pass.  */
5264 	      non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5265 	      memcpy (p, old_ent, htab->root.table.entsize);
5266 	      old_ent = (char *) old_ent + htab->root.table.entsize;
5267 	      h = (struct elf_link_hash_entry *) p;
5268 	      if (h->root.type == bfd_link_hash_warning)
5269 		{
5270 		  memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5271 		  old_ent = (char *) old_ent + htab->root.table.entsize;
5272 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
5273 		}
5274 	      if (h->root.type == bfd_link_hash_common)
5275 		{
5276 		  if (size > h->root.u.c.size)
5277 		    h->root.u.c.size = size;
5278 		  if (alignment_power > h->root.u.c.p->alignment_power)
5279 		    h->root.u.c.p->alignment_power = alignment_power;
5280 		}
5281 	      h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5282 	    }
5283 	}
5284 
5285       /* Make a special call to the linker "notice" function to
5286 	 tell it that symbols added for crefs may need to be removed.  */
5287       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5288 	goto error_free_vers;
5289 
5290       free (old_tab);
5291       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5292 			   alloc_mark);
5293       if (nondeflt_vers != NULL)
5294 	free (nondeflt_vers);
5295       return TRUE;
5296     }
5297 
5298   if (old_tab != NULL)
5299     {
5300       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5301 	goto error_free_vers;
5302       free (old_tab);
5303       old_tab = NULL;
5304     }
5305 
5306   /* Now that all the symbols from this input file are created, if
5307      not performing a relocatable link, handle .symver foo, foo@BAR
5308      such that any relocs against foo become foo@BAR.  */
5309   if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5310     {
5311       size_t cnt, symidx;
5312 
5313       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5314 	{
5315 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5316 	  char *shortname, *p;
5317 
5318 	  p = strchr (h->root.root.string, ELF_VER_CHR);
5319 	  if (p == NULL
5320 	      || (h->root.type != bfd_link_hash_defined
5321 		  && h->root.type != bfd_link_hash_defweak))
5322 	    continue;
5323 
5324 	  amt = p - h->root.root.string;
5325 	  shortname = (char *) bfd_malloc (amt + 1);
5326 	  if (!shortname)
5327 	    goto error_free_vers;
5328 	  memcpy (shortname, h->root.root.string, amt);
5329 	  shortname[amt] = '\0';
5330 
5331 	  hi = (struct elf_link_hash_entry *)
5332 	       bfd_link_hash_lookup (&htab->root, shortname,
5333 				     FALSE, FALSE, FALSE);
5334 	  if (hi != NULL
5335 	      && hi->root.type == h->root.type
5336 	      && hi->root.u.def.value == h->root.u.def.value
5337 	      && hi->root.u.def.section == h->root.u.def.section)
5338 	    {
5339 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5340 	      hi->root.type = bfd_link_hash_indirect;
5341 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5342 	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5343 	      sym_hash = elf_sym_hashes (abfd);
5344 	      if (sym_hash)
5345 		for (symidx = 0; symidx < extsymcount; ++symidx)
5346 		  if (sym_hash[symidx] == hi)
5347 		    {
5348 		      sym_hash[symidx] = h;
5349 		      break;
5350 		    }
5351 	    }
5352 	  free (shortname);
5353 	}
5354       free (nondeflt_vers);
5355       nondeflt_vers = NULL;
5356     }
5357 
5358   /* Now set the alias field correctly for all the weak defined
5359      symbols we found.  The only way to do this is to search all the
5360      symbols.  Since we only need the information for non functions in
5361      dynamic objects, that's the only time we actually put anything on
5362      the list WEAKS.  We need this information so that if a regular
5363      object refers to a symbol defined weakly in a dynamic object, the
5364      real symbol in the dynamic object is also put in the dynamic
5365      symbols; we also must arrange for both symbols to point to the
5366      same memory location.  We could handle the general case of symbol
5367      aliasing, but a general symbol alias can only be generated in
5368      assembler code, handling it correctly would be very time
5369      consuming, and other ELF linkers don't handle general aliasing
5370      either.  */
5371   if (weaks != NULL)
5372     {
5373       struct elf_link_hash_entry **hpp;
5374       struct elf_link_hash_entry **hppend;
5375       struct elf_link_hash_entry **sorted_sym_hash;
5376       struct elf_link_hash_entry *h;
5377       size_t sym_count;
5378 
5379       /* Since we have to search the whole symbol list for each weak
5380 	 defined symbol, search time for N weak defined symbols will be
5381 	 O(N^2). Binary search will cut it down to O(NlogN).  */
5382       amt = extsymcount;
5383       amt *= sizeof (*sorted_sym_hash);
5384       sorted_sym_hash = bfd_malloc (amt);
5385       if (sorted_sym_hash == NULL)
5386 	goto error_return;
5387       sym_hash = sorted_sym_hash;
5388       hpp = elf_sym_hashes (abfd);
5389       hppend = hpp + extsymcount;
5390       sym_count = 0;
5391       for (; hpp < hppend; hpp++)
5392 	{
5393 	  h = *hpp;
5394 	  if (h != NULL
5395 	      && h->root.type == bfd_link_hash_defined
5396 	      && !bed->is_function_type (h->type))
5397 	    {
5398 	      *sym_hash = h;
5399 	      sym_hash++;
5400 	      sym_count++;
5401 	    }
5402 	}
5403 
5404       qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5405 	     elf_sort_symbol);
5406 
5407       while (weaks != NULL)
5408 	{
5409 	  struct elf_link_hash_entry *hlook;
5410 	  asection *slook;
5411 	  bfd_vma vlook;
5412 	  size_t i, j, idx = 0;
5413 
5414 	  hlook = weaks;
5415 	  weaks = hlook->u.alias;
5416 	  hlook->u.alias = NULL;
5417 
5418 	  if (hlook->root.type != bfd_link_hash_defined
5419 	      && hlook->root.type != bfd_link_hash_defweak)
5420 	    continue;
5421 
5422 	  slook = hlook->root.u.def.section;
5423 	  vlook = hlook->root.u.def.value;
5424 
5425 	  i = 0;
5426 	  j = sym_count;
5427 	  while (i != j)
5428 	    {
5429 	      bfd_signed_vma vdiff;
5430 	      idx = (i + j) / 2;
5431 	      h = sorted_sym_hash[idx];
5432 	      vdiff = vlook - h->root.u.def.value;
5433 	      if (vdiff < 0)
5434 		j = idx;
5435 	      else if (vdiff > 0)
5436 		i = idx + 1;
5437 	      else
5438 		{
5439 		  int sdiff = slook->id - h->root.u.def.section->id;
5440 		  if (sdiff < 0)
5441 		    j = idx;
5442 		  else if (sdiff > 0)
5443 		    i = idx + 1;
5444 		  else
5445 		    break;
5446 		}
5447 	    }
5448 
5449 	  /* We didn't find a value/section match.  */
5450 	  if (i == j)
5451 	    continue;
5452 
5453 	  /* With multiple aliases, or when the weak symbol is already
5454 	     strongly defined, we have multiple matching symbols and
5455 	     the binary search above may land on any of them.  Step
5456 	     one past the matching symbol(s).  */
5457 	  while (++idx != j)
5458 	    {
5459 	      h = sorted_sym_hash[idx];
5460 	      if (h->root.u.def.section != slook
5461 		  || h->root.u.def.value != vlook)
5462 		break;
5463 	    }
5464 
5465 	  /* Now look back over the aliases.  Since we sorted by size
5466 	     as well as value and section, we'll choose the one with
5467 	     the largest size.  */
5468 	  while (idx-- != i)
5469 	    {
5470 	      h = sorted_sym_hash[idx];
5471 
5472 	      /* Stop if value or section doesn't match.  */
5473 	      if (h->root.u.def.section != slook
5474 		  || h->root.u.def.value != vlook)
5475 		break;
5476 	      else if (h != hlook)
5477 		{
5478 		  struct elf_link_hash_entry *t;
5479 
5480 		  hlook->u.alias = h;
5481 		  hlook->is_weakalias = 1;
5482 		  t = h;
5483 		  if (t->u.alias != NULL)
5484 		    while (t->u.alias != h)
5485 		      t = t->u.alias;
5486 		  t->u.alias = hlook;
5487 
5488 		  /* If the weak definition is in the list of dynamic
5489 		     symbols, make sure the real definition is put
5490 		     there as well.  */
5491 		  if (hlook->dynindx != -1 && h->dynindx == -1)
5492 		    {
5493 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
5494 			{
5495 			err_free_sym_hash:
5496 			  free (sorted_sym_hash);
5497 			  goto error_return;
5498 			}
5499 		    }
5500 
5501 		  /* If the real definition is in the list of dynamic
5502 		     symbols, make sure the weak definition is put
5503 		     there as well.  If we don't do this, then the
5504 		     dynamic loader might not merge the entries for the
5505 		     real definition and the weak definition.  */
5506 		  if (h->dynindx != -1 && hlook->dynindx == -1)
5507 		    {
5508 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5509 			goto err_free_sym_hash;
5510 		    }
5511 		  break;
5512 		}
5513 	    }
5514 	}
5515 
5516       free (sorted_sym_hash);
5517     }
5518 
5519   if (bed->check_directives
5520       && !(*bed->check_directives) (abfd, info))
5521     return FALSE;
5522 
5523   /* If this is a non-traditional link, try to optimize the handling
5524      of the .stab/.stabstr sections.  */
5525   if (! dynamic
5526       && ! info->traditional_format
5527       && is_elf_hash_table (htab)
5528       && (info->strip != strip_all && info->strip != strip_debugger))
5529     {
5530       asection *stabstr;
5531 
5532       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5533       if (stabstr != NULL)
5534 	{
5535 	  bfd_size_type string_offset = 0;
5536 	  asection *stab;
5537 
5538 	  for (stab = abfd->sections; stab; stab = stab->next)
5539 	    if (CONST_STRNEQ (stab->name, ".stab")
5540 		&& (!stab->name[5] ||
5541 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5542 		&& (stab->flags & SEC_MERGE) == 0
5543 		&& !bfd_is_abs_section (stab->output_section))
5544 	      {
5545 		struct bfd_elf_section_data *secdata;
5546 
5547 		secdata = elf_section_data (stab);
5548 		if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5549 					       stabstr, &secdata->sec_info,
5550 					       &string_offset))
5551 		  goto error_return;
5552 		if (secdata->sec_info)
5553 		  stab->sec_info_type = SEC_INFO_TYPE_STABS;
5554 	    }
5555 	}
5556     }
5557 
5558   if (is_elf_hash_table (htab) && add_needed)
5559     {
5560       /* Add this bfd to the loaded list.  */
5561       struct elf_link_loaded_list *n;
5562 
5563       n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5564       if (n == NULL)
5565 	goto error_return;
5566       n->abfd = abfd;
5567       n->next = htab->loaded;
5568       htab->loaded = n;
5569     }
5570 
5571   return TRUE;
5572 
5573  error_free_vers:
5574   if (old_tab != NULL)
5575     free (old_tab);
5576   if (old_strtab != NULL)
5577     free (old_strtab);
5578   if (nondeflt_vers != NULL)
5579     free (nondeflt_vers);
5580   if (extversym != NULL)
5581     free (extversym);
5582  error_free_sym:
5583   if (isymbuf != NULL)
5584     free (isymbuf);
5585  error_return:
5586   return FALSE;
5587 }
5588 
5589 /* Return the linker hash table entry of a symbol that might be
5590    satisfied by an archive symbol.  Return -1 on error.  */
5591 
5592 struct elf_link_hash_entry *
5593 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5594 				struct bfd_link_info *info,
5595 				const char *name)
5596 {
5597   struct elf_link_hash_entry *h;
5598   char *p, *copy;
5599   size_t len, first;
5600 
5601   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5602   if (h != NULL)
5603     return h;
5604 
5605   /* If this is a default version (the name contains @@), look up the
5606      symbol again with only one `@' as well as without the version.
5607      The effect is that references to the symbol with and without the
5608      version will be matched by the default symbol in the archive.  */
5609 
5610   p = strchr (name, ELF_VER_CHR);
5611   if (p == NULL || p[1] != ELF_VER_CHR)
5612     return h;
5613 
5614   /* First check with only one `@'.  */
5615   len = strlen (name);
5616   copy = (char *) bfd_alloc (abfd, len);
5617   if (copy == NULL)
5618     return (struct elf_link_hash_entry *) -1;
5619 
5620   first = p - name + 1;
5621   memcpy (copy, name, first);
5622   memcpy (copy + first, name + first + 1, len - first);
5623 
5624   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5625   if (h == NULL)
5626     {
5627       /* We also need to check references to the symbol without the
5628 	 version.  */
5629       copy[first - 1] = '\0';
5630       h = elf_link_hash_lookup (elf_hash_table (info), copy,
5631 				FALSE, FALSE, TRUE);
5632     }
5633 
5634   bfd_release (abfd, copy);
5635   return h;
5636 }
5637 
5638 /* Add symbols from an ELF archive file to the linker hash table.  We
5639    don't use _bfd_generic_link_add_archive_symbols because we need to
5640    handle versioned symbols.
5641 
5642    Fortunately, ELF archive handling is simpler than that done by
5643    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5644    oddities.  In ELF, if we find a symbol in the archive map, and the
5645    symbol is currently undefined, we know that we must pull in that
5646    object file.
5647 
5648    Unfortunately, we do have to make multiple passes over the symbol
5649    table until nothing further is resolved.  */
5650 
5651 static bfd_boolean
5652 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5653 {
5654   symindex c;
5655   unsigned char *included = NULL;
5656   carsym *symdefs;
5657   bfd_boolean loop;
5658   bfd_size_type amt;
5659   const struct elf_backend_data *bed;
5660   struct elf_link_hash_entry * (*archive_symbol_lookup)
5661     (bfd *, struct bfd_link_info *, const char *);
5662 
5663   if (! bfd_has_map (abfd))
5664     {
5665       /* An empty archive is a special case.  */
5666       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5667 	return TRUE;
5668       bfd_set_error (bfd_error_no_armap);
5669       return FALSE;
5670     }
5671 
5672   /* Keep track of all symbols we know to be already defined, and all
5673      files we know to be already included.  This is to speed up the
5674      second and subsequent passes.  */
5675   c = bfd_ardata (abfd)->symdef_count;
5676   if (c == 0)
5677     return TRUE;
5678   amt = c;
5679   amt *= sizeof (*included);
5680   included = (unsigned char *) bfd_zmalloc (amt);
5681   if (included == NULL)
5682     return FALSE;
5683 
5684   symdefs = bfd_ardata (abfd)->symdefs;
5685   bed = get_elf_backend_data (abfd);
5686   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5687 
5688   do
5689     {
5690       file_ptr last;
5691       symindex i;
5692       carsym *symdef;
5693       carsym *symdefend;
5694 
5695       loop = FALSE;
5696       last = -1;
5697 
5698       symdef = symdefs;
5699       symdefend = symdef + c;
5700       for (i = 0; symdef < symdefend; symdef++, i++)
5701 	{
5702 	  struct elf_link_hash_entry *h;
5703 	  bfd *element;
5704 	  struct bfd_link_hash_entry *undefs_tail;
5705 	  symindex mark;
5706 
5707 	  if (included[i])
5708 	    continue;
5709 	  if (symdef->file_offset == last)
5710 	    {
5711 	      included[i] = TRUE;
5712 	      continue;
5713 	    }
5714 
5715 	  h = archive_symbol_lookup (abfd, info, symdef->name);
5716 	  if (h == (struct elf_link_hash_entry *) -1)
5717 	    goto error_return;
5718 
5719 	  if (h == NULL)
5720 	    continue;
5721 
5722 	  if (h->root.type == bfd_link_hash_common)
5723 	    {
5724 	      /* We currently have a common symbol.  The archive map contains
5725 		 a reference to this symbol, so we may want to include it.  We
5726 		 only want to include it however, if this archive element
5727 		 contains a definition of the symbol, not just another common
5728 		 declaration of it.
5729 
5730 		 Unfortunately some archivers (including GNU ar) will put
5731 		 declarations of common symbols into their archive maps, as
5732 		 well as real definitions, so we cannot just go by the archive
5733 		 map alone.  Instead we must read in the element's symbol
5734 		 table and check that to see what kind of symbol definition
5735 		 this is.  */
5736 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5737 		continue;
5738 	    }
5739 	  else if (h->root.type != bfd_link_hash_undefined)
5740 	    {
5741 	      if (h->root.type != bfd_link_hash_undefweak)
5742 		/* Symbol must be defined.  Don't check it again.  */
5743 		included[i] = TRUE;
5744 	      continue;
5745 	    }
5746 
5747 	  /* We need to include this archive member.  */
5748 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5749 	  if (element == NULL)
5750 	    goto error_return;
5751 
5752 	  if (! bfd_check_format (element, bfd_object))
5753 	    goto error_return;
5754 
5755 	  undefs_tail = info->hash->undefs_tail;
5756 
5757 	  if (!(*info->callbacks
5758 		->add_archive_element) (info, element, symdef->name, &element))
5759 	    continue;
5760 	  if (!bfd_link_add_symbols (element, info))
5761 	    goto error_return;
5762 
5763 	  /* If there are any new undefined symbols, we need to make
5764 	     another pass through the archive in order to see whether
5765 	     they can be defined.  FIXME: This isn't perfect, because
5766 	     common symbols wind up on undefs_tail and because an
5767 	     undefined symbol which is defined later on in this pass
5768 	     does not require another pass.  This isn't a bug, but it
5769 	     does make the code less efficient than it could be.  */
5770 	  if (undefs_tail != info->hash->undefs_tail)
5771 	    loop = TRUE;
5772 
5773 	  /* Look backward to mark all symbols from this object file
5774 	     which we have already seen in this pass.  */
5775 	  mark = i;
5776 	  do
5777 	    {
5778 	      included[mark] = TRUE;
5779 	      if (mark == 0)
5780 		break;
5781 	      --mark;
5782 	    }
5783 	  while (symdefs[mark].file_offset == symdef->file_offset);
5784 
5785 	  /* We mark subsequent symbols from this object file as we go
5786 	     on through the loop.  */
5787 	  last = symdef->file_offset;
5788 	}
5789     }
5790   while (loop);
5791 
5792   free (included);
5793 
5794   return TRUE;
5795 
5796  error_return:
5797   if (included != NULL)
5798     free (included);
5799   return FALSE;
5800 }
5801 
5802 /* Given an ELF BFD, add symbols to the global hash table as
5803    appropriate.  */
5804 
5805 bfd_boolean
5806 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5807 {
5808   switch (bfd_get_format (abfd))
5809     {
5810     case bfd_object:
5811       return elf_link_add_object_symbols (abfd, info);
5812     case bfd_archive:
5813       return elf_link_add_archive_symbols (abfd, info);
5814     default:
5815       bfd_set_error (bfd_error_wrong_format);
5816       return FALSE;
5817     }
5818 }
5819 
5820 struct hash_codes_info
5821 {
5822   unsigned long *hashcodes;
5823   bfd_boolean error;
5824 };
5825 
5826 /* This function will be called though elf_link_hash_traverse to store
5827    all hash value of the exported symbols in an array.  */
5828 
5829 static bfd_boolean
5830 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5831 {
5832   struct hash_codes_info *inf = (struct hash_codes_info *) data;
5833   const char *name;
5834   unsigned long ha;
5835   char *alc = NULL;
5836 
5837   /* Ignore indirect symbols.  These are added by the versioning code.  */
5838   if (h->dynindx == -1)
5839     return TRUE;
5840 
5841   name = h->root.root.string;
5842   if (h->versioned >= versioned)
5843     {
5844       char *p = strchr (name, ELF_VER_CHR);
5845       if (p != NULL)
5846 	{
5847 	  alc = (char *) bfd_malloc (p - name + 1);
5848 	  if (alc == NULL)
5849 	    {
5850 	      inf->error = TRUE;
5851 	      return FALSE;
5852 	    }
5853 	  memcpy (alc, name, p - name);
5854 	  alc[p - name] = '\0';
5855 	  name = alc;
5856 	}
5857     }
5858 
5859   /* Compute the hash value.  */
5860   ha = bfd_elf_hash (name);
5861 
5862   /* Store the found hash value in the array given as the argument.  */
5863   *(inf->hashcodes)++ = ha;
5864 
5865   /* And store it in the struct so that we can put it in the hash table
5866      later.  */
5867   h->u.elf_hash_value = ha;
5868 
5869   if (alc != NULL)
5870     free (alc);
5871 
5872   return TRUE;
5873 }
5874 
5875 struct collect_gnu_hash_codes
5876 {
5877   bfd *output_bfd;
5878   const struct elf_backend_data *bed;
5879   unsigned long int nsyms;
5880   unsigned long int maskbits;
5881   unsigned long int *hashcodes;
5882   unsigned long int *hashval;
5883   unsigned long int *indx;
5884   unsigned long int *counts;
5885   bfd_vma *bitmask;
5886   bfd_byte *contents;
5887   bfd_size_type xlat;
5888   long int min_dynindx;
5889   unsigned long int bucketcount;
5890   unsigned long int symindx;
5891   long int local_indx;
5892   long int shift1, shift2;
5893   unsigned long int mask;
5894   bfd_boolean error;
5895 };
5896 
5897 /* This function will be called though elf_link_hash_traverse to store
5898    all hash value of the exported symbols in an array.  */
5899 
5900 static bfd_boolean
5901 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5902 {
5903   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5904   const char *name;
5905   unsigned long ha;
5906   char *alc = NULL;
5907 
5908   /* Ignore indirect symbols.  These are added by the versioning code.  */
5909   if (h->dynindx == -1)
5910     return TRUE;
5911 
5912   /* Ignore also local symbols and undefined symbols.  */
5913   if (! (*s->bed->elf_hash_symbol) (h))
5914     return TRUE;
5915 
5916   name = h->root.root.string;
5917   if (h->versioned >= versioned)
5918     {
5919       char *p = strchr (name, ELF_VER_CHR);
5920       if (p != NULL)
5921 	{
5922 	  alc = (char *) bfd_malloc (p - name + 1);
5923 	  if (alc == NULL)
5924 	    {
5925 	      s->error = TRUE;
5926 	      return FALSE;
5927 	    }
5928 	  memcpy (alc, name, p - name);
5929 	  alc[p - name] = '\0';
5930 	  name = alc;
5931 	}
5932     }
5933 
5934   /* Compute the hash value.  */
5935   ha = bfd_elf_gnu_hash (name);
5936 
5937   /* Store the found hash value in the array for compute_bucket_count,
5938      and also for .dynsym reordering purposes.  */
5939   s->hashcodes[s->nsyms] = ha;
5940   s->hashval[h->dynindx] = ha;
5941   ++s->nsyms;
5942   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5943     s->min_dynindx = h->dynindx;
5944 
5945   if (alc != NULL)
5946     free (alc);
5947 
5948   return TRUE;
5949 }
5950 
5951 /* This function will be called though elf_link_hash_traverse to do
5952    final dynamic symbol renumbering in case of .gnu.hash.
5953    If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
5954    to the translation table.  */
5955 
5956 static bfd_boolean
5957 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
5958 {
5959   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5960   unsigned long int bucket;
5961   unsigned long int val;
5962 
5963   /* Ignore indirect symbols.  */
5964   if (h->dynindx == -1)
5965     return TRUE;
5966 
5967   /* Ignore also local symbols and undefined symbols.  */
5968   if (! (*s->bed->elf_hash_symbol) (h))
5969     {
5970       if (h->dynindx >= s->min_dynindx)
5971 	{
5972 	  if (s->bed->record_xhash_symbol != NULL)
5973 	    {
5974 	      (*s->bed->record_xhash_symbol) (h, 0);
5975 	      s->local_indx++;
5976 	    }
5977 	  else
5978 	    h->dynindx = s->local_indx++;
5979 	}
5980       return TRUE;
5981     }
5982 
5983   bucket = s->hashval[h->dynindx] % s->bucketcount;
5984   val = (s->hashval[h->dynindx] >> s->shift1)
5985 	& ((s->maskbits >> s->shift1) - 1);
5986   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5987   s->bitmask[val]
5988     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5989   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5990   if (s->counts[bucket] == 1)
5991     /* Last element terminates the chain.  */
5992     val |= 1;
5993   bfd_put_32 (s->output_bfd, val,
5994 	      s->contents + (s->indx[bucket] - s->symindx) * 4);
5995   --s->counts[bucket];
5996   if (s->bed->record_xhash_symbol != NULL)
5997     {
5998       bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
5999 
6000       (*s->bed->record_xhash_symbol) (h, xlat_loc);
6001     }
6002   else
6003     h->dynindx = s->indx[bucket]++;
6004   return TRUE;
6005 }
6006 
6007 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
6008 
6009 bfd_boolean
6010 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6011 {
6012   return !(h->forced_local
6013 	   || h->root.type == bfd_link_hash_undefined
6014 	   || h->root.type == bfd_link_hash_undefweak
6015 	   || ((h->root.type == bfd_link_hash_defined
6016 		|| h->root.type == bfd_link_hash_defweak)
6017 	       && h->root.u.def.section->output_section == NULL));
6018 }
6019 
6020 /* Array used to determine the number of hash table buckets to use
6021    based on the number of symbols there are.  If there are fewer than
6022    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6023    fewer than 37 we use 17 buckets, and so forth.  We never use more
6024    than 32771 buckets.  */
6025 
6026 static const size_t elf_buckets[] =
6027 {
6028   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6029   16411, 32771, 0
6030 };
6031 
6032 /* Compute bucket count for hashing table.  We do not use a static set
6033    of possible tables sizes anymore.  Instead we determine for all
6034    possible reasonable sizes of the table the outcome (i.e., the
6035    number of collisions etc) and choose the best solution.  The
6036    weighting functions are not too simple to allow the table to grow
6037    without bounds.  Instead one of the weighting factors is the size.
6038    Therefore the result is always a good payoff between few collisions
6039    (= short chain lengths) and table size.  */
6040 static size_t
6041 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6042 		      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6043 		      unsigned long int nsyms,
6044 		      int gnu_hash)
6045 {
6046   size_t best_size = 0;
6047   unsigned long int i;
6048 
6049   /* We have a problem here.  The following code to optimize the table
6050      size requires an integer type with more the 32 bits.  If
6051      BFD_HOST_U_64_BIT is set we know about such a type.  */
6052 #ifdef BFD_HOST_U_64_BIT
6053   if (info->optimize)
6054     {
6055       size_t minsize;
6056       size_t maxsize;
6057       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6058       bfd *dynobj = elf_hash_table (info)->dynobj;
6059       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6060       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6061       unsigned long int *counts;
6062       bfd_size_type amt;
6063       unsigned int no_improvement_count = 0;
6064 
6065       /* Possible optimization parameters: if we have NSYMS symbols we say
6066 	 that the hashing table must at least have NSYMS/4 and at most
6067 	 2*NSYMS buckets.  */
6068       minsize = nsyms / 4;
6069       if (minsize == 0)
6070 	minsize = 1;
6071       best_size = maxsize = nsyms * 2;
6072       if (gnu_hash)
6073 	{
6074 	  if (minsize < 2)
6075 	    minsize = 2;
6076 	  if ((best_size & 31) == 0)
6077 	    ++best_size;
6078 	}
6079 
6080       /* Create array where we count the collisions in.  We must use bfd_malloc
6081 	 since the size could be large.  */
6082       amt = maxsize;
6083       amt *= sizeof (unsigned long int);
6084       counts = (unsigned long int *) bfd_malloc (amt);
6085       if (counts == NULL)
6086 	return 0;
6087 
6088       /* Compute the "optimal" size for the hash table.  The criteria is a
6089 	 minimal chain length.  The minor criteria is (of course) the size
6090 	 of the table.  */
6091       for (i = minsize; i < maxsize; ++i)
6092 	{
6093 	  /* Walk through the array of hashcodes and count the collisions.  */
6094 	  BFD_HOST_U_64_BIT max;
6095 	  unsigned long int j;
6096 	  unsigned long int fact;
6097 
6098 	  if (gnu_hash && (i & 31) == 0)
6099 	    continue;
6100 
6101 	  memset (counts, '\0', i * sizeof (unsigned long int));
6102 
6103 	  /* Determine how often each hash bucket is used.  */
6104 	  for (j = 0; j < nsyms; ++j)
6105 	    ++counts[hashcodes[j] % i];
6106 
6107 	  /* For the weight function we need some information about the
6108 	     pagesize on the target.  This is information need not be 100%
6109 	     accurate.  Since this information is not available (so far) we
6110 	     define it here to a reasonable default value.  If it is crucial
6111 	     to have a better value some day simply define this value.  */
6112 # ifndef BFD_TARGET_PAGESIZE
6113 #  define BFD_TARGET_PAGESIZE	(4096)
6114 # endif
6115 
6116 	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6117 	     and the chains.  */
6118 	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6119 
6120 # if 1
6121 	  /* Variant 1: optimize for short chains.  We add the squares
6122 	     of all the chain lengths (which favors many small chain
6123 	     over a few long chains).  */
6124 	  for (j = 0; j < i; ++j)
6125 	    max += counts[j] * counts[j];
6126 
6127 	  /* This adds penalties for the overall size of the table.  */
6128 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6129 	  max *= fact * fact;
6130 # else
6131 	  /* Variant 2: Optimize a lot more for small table.  Here we
6132 	     also add squares of the size but we also add penalties for
6133 	     empty slots (the +1 term).  */
6134 	  for (j = 0; j < i; ++j)
6135 	    max += (1 + counts[j]) * (1 + counts[j]);
6136 
6137 	  /* The overall size of the table is considered, but not as
6138 	     strong as in variant 1, where it is squared.  */
6139 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6140 	  max *= fact;
6141 # endif
6142 
6143 	  /* Compare with current best results.  */
6144 	  if (max < best_chlen)
6145 	    {
6146 	      best_chlen = max;
6147 	      best_size = i;
6148 	      no_improvement_count = 0;
6149 	    }
6150 	  /* PR 11843: Avoid futile long searches for the best bucket size
6151 	     when there are a large number of symbols.  */
6152 	  else if (++no_improvement_count == 100)
6153 	    break;
6154 	}
6155 
6156       free (counts);
6157     }
6158   else
6159 #endif /* defined (BFD_HOST_U_64_BIT) */
6160     {
6161       /* This is the fallback solution if no 64bit type is available or if we
6162 	 are not supposed to spend much time on optimizations.  We select the
6163 	 bucket count using a fixed set of numbers.  */
6164       for (i = 0; elf_buckets[i] != 0; i++)
6165 	{
6166 	  best_size = elf_buckets[i];
6167 	  if (nsyms < elf_buckets[i + 1])
6168 	    break;
6169 	}
6170       if (gnu_hash && best_size < 2)
6171 	best_size = 2;
6172     }
6173 
6174   return best_size;
6175 }
6176 
6177 /* Size any SHT_GROUP section for ld -r.  */
6178 
6179 bfd_boolean
6180 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6181 {
6182   bfd *ibfd;
6183   asection *s;
6184 
6185   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6186     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6187 	&& (s = ibfd->sections) != NULL
6188 	&& s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6189 	&& !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6190       return FALSE;
6191   return TRUE;
6192 }
6193 
6194 /* Set a default stack segment size.  The value in INFO wins.  If it
6195    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6196    undefined it is initialized.  */
6197 
6198 bfd_boolean
6199 bfd_elf_stack_segment_size (bfd *output_bfd,
6200 			    struct bfd_link_info *info,
6201 			    const char *legacy_symbol,
6202 			    bfd_vma default_size)
6203 {
6204   struct elf_link_hash_entry *h = NULL;
6205 
6206   /* Look for legacy symbol.  */
6207   if (legacy_symbol)
6208     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6209 			      FALSE, FALSE, FALSE);
6210   if (h && (h->root.type == bfd_link_hash_defined
6211 	    || h->root.type == bfd_link_hash_defweak)
6212       && h->def_regular
6213       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6214     {
6215       /* The symbol has no type if specified on the command line.  */
6216       h->type = STT_OBJECT;
6217       if (info->stacksize)
6218 	/* xgettext:c-format */
6219 	_bfd_error_handler (_("%pB: stack size specified and %s set"),
6220 			    output_bfd, legacy_symbol);
6221       else if (h->root.u.def.section != bfd_abs_section_ptr)
6222 	/* xgettext:c-format */
6223 	_bfd_error_handler (_("%pB: %s not absolute"),
6224 			    output_bfd, legacy_symbol);
6225       else
6226 	info->stacksize = h->root.u.def.value;
6227     }
6228 
6229   if (!info->stacksize)
6230     /* If the user didn't set a size, or explicitly inhibit the
6231        size, set it now.  */
6232     info->stacksize = default_size;
6233 
6234   /* Provide the legacy symbol, if it is referenced.  */
6235   if (h && (h->root.type == bfd_link_hash_undefined
6236 	    || h->root.type == bfd_link_hash_undefweak))
6237     {
6238       struct bfd_link_hash_entry *bh = NULL;
6239 
6240       if (!(_bfd_generic_link_add_one_symbol
6241 	    (info, output_bfd, legacy_symbol,
6242 	     BSF_GLOBAL, bfd_abs_section_ptr,
6243 	     info->stacksize >= 0 ? info->stacksize : 0,
6244 	     NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6245 	return FALSE;
6246 
6247       h = (struct elf_link_hash_entry *) bh;
6248       h->def_regular = 1;
6249       h->type = STT_OBJECT;
6250     }
6251 
6252   return TRUE;
6253 }
6254 
6255 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
6256 
6257 struct elf_gc_sweep_symbol_info
6258 {
6259   struct bfd_link_info *info;
6260   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6261 		       bfd_boolean);
6262 };
6263 
6264 static bfd_boolean
6265 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6266 {
6267   if (!h->mark
6268       && (((h->root.type == bfd_link_hash_defined
6269 	    || h->root.type == bfd_link_hash_defweak)
6270 	   && !((h->def_regular || ELF_COMMON_DEF_P (h))
6271 		&& h->root.u.def.section->gc_mark))
6272 	  || h->root.type == bfd_link_hash_undefined
6273 	  || h->root.type == bfd_link_hash_undefweak))
6274     {
6275       struct elf_gc_sweep_symbol_info *inf;
6276 
6277       inf = (struct elf_gc_sweep_symbol_info *) data;
6278       (*inf->hide_symbol) (inf->info, h, TRUE);
6279       h->def_regular = 0;
6280       h->ref_regular = 0;
6281       h->ref_regular_nonweak = 0;
6282     }
6283 
6284   return TRUE;
6285 }
6286 
6287 /* Set up the sizes and contents of the ELF dynamic sections.  This is
6288    called by the ELF linker emulation before_allocation routine.  We
6289    must set the sizes of the sections before the linker sets the
6290    addresses of the various sections.  */
6291 
6292 bfd_boolean
6293 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6294 			       const char *soname,
6295 			       const char *rpath,
6296 			       const char *filter_shlib,
6297 			       const char *audit,
6298 			       const char *depaudit,
6299 			       const char * const *auxiliary_filters,
6300 			       struct bfd_link_info *info,
6301 			       asection **sinterpptr)
6302 {
6303   bfd *dynobj;
6304   const struct elf_backend_data *bed;
6305 
6306   *sinterpptr = NULL;
6307 
6308   if (!is_elf_hash_table (info->hash))
6309     return TRUE;
6310 
6311   dynobj = elf_hash_table (info)->dynobj;
6312 
6313   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6314     {
6315       struct bfd_elf_version_tree *verdefs;
6316       struct elf_info_failed asvinfo;
6317       struct bfd_elf_version_tree *t;
6318       struct bfd_elf_version_expr *d;
6319       asection *s;
6320       size_t soname_indx;
6321 
6322       /* If we are supposed to export all symbols into the dynamic symbol
6323 	 table (this is not the normal case), then do so.  */
6324       if (info->export_dynamic
6325 	  || (bfd_link_executable (info) && info->dynamic))
6326 	{
6327 	  struct elf_info_failed eif;
6328 
6329 	  eif.info = info;
6330 	  eif.failed = FALSE;
6331 	  elf_link_hash_traverse (elf_hash_table (info),
6332 				  _bfd_elf_export_symbol,
6333 				  &eif);
6334 	  if (eif.failed)
6335 	    return FALSE;
6336 	}
6337 
6338       if (soname != NULL)
6339 	{
6340 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6341 					     soname, TRUE);
6342 	  if (soname_indx == (size_t) -1
6343 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6344 	    return FALSE;
6345 	}
6346       else
6347 	soname_indx = (size_t) -1;
6348 
6349       /* Make all global versions with definition.  */
6350       for (t = info->version_info; t != NULL; t = t->next)
6351 	for (d = t->globals.list; d != NULL; d = d->next)
6352 	  if (!d->symver && d->literal)
6353 	    {
6354 	      const char *verstr, *name;
6355 	      size_t namelen, verlen, newlen;
6356 	      char *newname, *p, leading_char;
6357 	      struct elf_link_hash_entry *newh;
6358 
6359 	      leading_char = bfd_get_symbol_leading_char (output_bfd);
6360 	      name = d->pattern;
6361 	      namelen = strlen (name) + (leading_char != '\0');
6362 	      verstr = t->name;
6363 	      verlen = strlen (verstr);
6364 	      newlen = namelen + verlen + 3;
6365 
6366 	      newname = (char *) bfd_malloc (newlen);
6367 	      if (newname == NULL)
6368 		return FALSE;
6369 	      newname[0] = leading_char;
6370 	      memcpy (newname + (leading_char != '\0'), name, namelen);
6371 
6372 	      /* Check the hidden versioned definition.  */
6373 	      p = newname + namelen;
6374 	      *p++ = ELF_VER_CHR;
6375 	      memcpy (p, verstr, verlen + 1);
6376 	      newh = elf_link_hash_lookup (elf_hash_table (info),
6377 					   newname, FALSE, FALSE,
6378 					   FALSE);
6379 	      if (newh == NULL
6380 		  || (newh->root.type != bfd_link_hash_defined
6381 		      && newh->root.type != bfd_link_hash_defweak))
6382 		{
6383 		  /* Check the default versioned definition.  */
6384 		  *p++ = ELF_VER_CHR;
6385 		  memcpy (p, verstr, verlen + 1);
6386 		  newh = elf_link_hash_lookup (elf_hash_table (info),
6387 					       newname, FALSE, FALSE,
6388 					       FALSE);
6389 		}
6390 	      free (newname);
6391 
6392 	      /* Mark this version if there is a definition and it is
6393 		 not defined in a shared object.  */
6394 	      if (newh != NULL
6395 		  && !newh->def_dynamic
6396 		  && (newh->root.type == bfd_link_hash_defined
6397 		      || newh->root.type == bfd_link_hash_defweak))
6398 		d->symver = 1;
6399 	    }
6400 
6401       /* Attach all the symbols to their version information.  */
6402       asvinfo.info = info;
6403       asvinfo.failed = FALSE;
6404 
6405       elf_link_hash_traverse (elf_hash_table (info),
6406 			      _bfd_elf_link_assign_sym_version,
6407 			      &asvinfo);
6408       if (asvinfo.failed)
6409 	return FALSE;
6410 
6411       if (!info->allow_undefined_version)
6412 	{
6413 	  /* Check if all global versions have a definition.  */
6414 	  bfd_boolean all_defined = TRUE;
6415 	  for (t = info->version_info; t != NULL; t = t->next)
6416 	    for (d = t->globals.list; d != NULL; d = d->next)
6417 	      if (d->literal && !d->symver && !d->script)
6418 		{
6419 		  _bfd_error_handler
6420 		    (_("%s: undefined version: %s"),
6421 		     d->pattern, t->name);
6422 		  all_defined = FALSE;
6423 		}
6424 
6425 	  if (!all_defined)
6426 	    {
6427 	      bfd_set_error (bfd_error_bad_value);
6428 	      return FALSE;
6429 	    }
6430 	}
6431 
6432       /* Set up the version definition section.  */
6433       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6434       BFD_ASSERT (s != NULL);
6435 
6436       /* We may have created additional version definitions if we are
6437 	 just linking a regular application.  */
6438       verdefs = info->version_info;
6439 
6440       /* Skip anonymous version tag.  */
6441       if (verdefs != NULL && verdefs->vernum == 0)
6442 	verdefs = verdefs->next;
6443 
6444       if (verdefs == NULL && !info->create_default_symver)
6445 	s->flags |= SEC_EXCLUDE;
6446       else
6447 	{
6448 	  unsigned int cdefs;
6449 	  bfd_size_type size;
6450 	  bfd_byte *p;
6451 	  Elf_Internal_Verdef def;
6452 	  Elf_Internal_Verdaux defaux;
6453 	  struct bfd_link_hash_entry *bh;
6454 	  struct elf_link_hash_entry *h;
6455 	  const char *name;
6456 
6457 	  cdefs = 0;
6458 	  size = 0;
6459 
6460 	  /* Make space for the base version.  */
6461 	  size += sizeof (Elf_External_Verdef);
6462 	  size += sizeof (Elf_External_Verdaux);
6463 	  ++cdefs;
6464 
6465 	  /* Make space for the default version.  */
6466 	  if (info->create_default_symver)
6467 	    {
6468 	      size += sizeof (Elf_External_Verdef);
6469 	      ++cdefs;
6470 	    }
6471 
6472 	  for (t = verdefs; t != NULL; t = t->next)
6473 	    {
6474 	      struct bfd_elf_version_deps *n;
6475 
6476 	      /* Don't emit base version twice.  */
6477 	      if (t->vernum == 0)
6478 		continue;
6479 
6480 	      size += sizeof (Elf_External_Verdef);
6481 	      size += sizeof (Elf_External_Verdaux);
6482 	      ++cdefs;
6483 
6484 	      for (n = t->deps; n != NULL; n = n->next)
6485 		size += sizeof (Elf_External_Verdaux);
6486 	    }
6487 
6488 	  s->size = size;
6489 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6490 	  if (s->contents == NULL && s->size != 0)
6491 	    return FALSE;
6492 
6493 	  /* Fill in the version definition section.  */
6494 
6495 	  p = s->contents;
6496 
6497 	  def.vd_version = VER_DEF_CURRENT;
6498 	  def.vd_flags = VER_FLG_BASE;
6499 	  def.vd_ndx = 1;
6500 	  def.vd_cnt = 1;
6501 	  if (info->create_default_symver)
6502 	    {
6503 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6504 	      def.vd_next = sizeof (Elf_External_Verdef);
6505 	    }
6506 	  else
6507 	    {
6508 	      def.vd_aux = sizeof (Elf_External_Verdef);
6509 	      def.vd_next = (sizeof (Elf_External_Verdef)
6510 			     + sizeof (Elf_External_Verdaux));
6511 	    }
6512 
6513 	  if (soname_indx != (size_t) -1)
6514 	    {
6515 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6516 				      soname_indx);
6517 	      def.vd_hash = bfd_elf_hash (soname);
6518 	      defaux.vda_name = soname_indx;
6519 	      name = soname;
6520 	    }
6521 	  else
6522 	    {
6523 	      size_t indx;
6524 
6525 	      name = lbasename (output_bfd->filename);
6526 	      def.vd_hash = bfd_elf_hash (name);
6527 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6528 					  name, FALSE);
6529 	      if (indx == (size_t) -1)
6530 		return FALSE;
6531 	      defaux.vda_name = indx;
6532 	    }
6533 	  defaux.vda_next = 0;
6534 
6535 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
6536 				    (Elf_External_Verdef *) p);
6537 	  p += sizeof (Elf_External_Verdef);
6538 	  if (info->create_default_symver)
6539 	    {
6540 	      /* Add a symbol representing this version.  */
6541 	      bh = NULL;
6542 	      if (! (_bfd_generic_link_add_one_symbol
6543 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6544 		      0, NULL, FALSE,
6545 		      get_elf_backend_data (dynobj)->collect, &bh)))
6546 		return FALSE;
6547 	      h = (struct elf_link_hash_entry *) bh;
6548 	      h->non_elf = 0;
6549 	      h->def_regular = 1;
6550 	      h->type = STT_OBJECT;
6551 	      h->verinfo.vertree = NULL;
6552 
6553 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6554 		return FALSE;
6555 
6556 	      /* Create a duplicate of the base version with the same
6557 		 aux block, but different flags.  */
6558 	      def.vd_flags = 0;
6559 	      def.vd_ndx = 2;
6560 	      def.vd_aux = sizeof (Elf_External_Verdef);
6561 	      if (verdefs)
6562 		def.vd_next = (sizeof (Elf_External_Verdef)
6563 			       + sizeof (Elf_External_Verdaux));
6564 	      else
6565 		def.vd_next = 0;
6566 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
6567 					(Elf_External_Verdef *) p);
6568 	      p += sizeof (Elf_External_Verdef);
6569 	    }
6570 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6571 				     (Elf_External_Verdaux *) p);
6572 	  p += sizeof (Elf_External_Verdaux);
6573 
6574 	  for (t = verdefs; t != NULL; t = t->next)
6575 	    {
6576 	      unsigned int cdeps;
6577 	      struct bfd_elf_version_deps *n;
6578 
6579 	      /* Don't emit the base version twice.  */
6580 	      if (t->vernum == 0)
6581 		continue;
6582 
6583 	      cdeps = 0;
6584 	      for (n = t->deps; n != NULL; n = n->next)
6585 		++cdeps;
6586 
6587 	      /* Add a symbol representing this version.  */
6588 	      bh = NULL;
6589 	      if (! (_bfd_generic_link_add_one_symbol
6590 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6591 		      0, NULL, FALSE,
6592 		      get_elf_backend_data (dynobj)->collect, &bh)))
6593 		return FALSE;
6594 	      h = (struct elf_link_hash_entry *) bh;
6595 	      h->non_elf = 0;
6596 	      h->def_regular = 1;
6597 	      h->type = STT_OBJECT;
6598 	      h->verinfo.vertree = t;
6599 
6600 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6601 		return FALSE;
6602 
6603 	      def.vd_version = VER_DEF_CURRENT;
6604 	      def.vd_flags = 0;
6605 	      if (t->globals.list == NULL
6606 		  && t->locals.list == NULL
6607 		  && ! t->used)
6608 		def.vd_flags |= VER_FLG_WEAK;
6609 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6610 	      def.vd_cnt = cdeps + 1;
6611 	      def.vd_hash = bfd_elf_hash (t->name);
6612 	      def.vd_aux = sizeof (Elf_External_Verdef);
6613 	      def.vd_next = 0;
6614 
6615 	      /* If a basever node is next, it *must* be the last node in
6616 		 the chain, otherwise Verdef construction breaks.  */
6617 	      if (t->next != NULL && t->next->vernum == 0)
6618 		BFD_ASSERT (t->next->next == NULL);
6619 
6620 	      if (t->next != NULL && t->next->vernum != 0)
6621 		def.vd_next = (sizeof (Elf_External_Verdef)
6622 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6623 
6624 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
6625 					(Elf_External_Verdef *) p);
6626 	      p += sizeof (Elf_External_Verdef);
6627 
6628 	      defaux.vda_name = h->dynstr_index;
6629 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6630 				      h->dynstr_index);
6631 	      defaux.vda_next = 0;
6632 	      if (t->deps != NULL)
6633 		defaux.vda_next = sizeof (Elf_External_Verdaux);
6634 	      t->name_indx = defaux.vda_name;
6635 
6636 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6637 					 (Elf_External_Verdaux *) p);
6638 	      p += sizeof (Elf_External_Verdaux);
6639 
6640 	      for (n = t->deps; n != NULL; n = n->next)
6641 		{
6642 		  if (n->version_needed == NULL)
6643 		    {
6644 		      /* This can happen if there was an error in the
6645 			 version script.  */
6646 		      defaux.vda_name = 0;
6647 		    }
6648 		  else
6649 		    {
6650 		      defaux.vda_name = n->version_needed->name_indx;
6651 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6652 					      defaux.vda_name);
6653 		    }
6654 		  if (n->next == NULL)
6655 		    defaux.vda_next = 0;
6656 		  else
6657 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
6658 
6659 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6660 					     (Elf_External_Verdaux *) p);
6661 		  p += sizeof (Elf_External_Verdaux);
6662 		}
6663 	    }
6664 
6665 	  elf_tdata (output_bfd)->cverdefs = cdefs;
6666 	}
6667     }
6668 
6669   bed = get_elf_backend_data (output_bfd);
6670 
6671   if (info->gc_sections && bed->can_gc_sections)
6672     {
6673       struct elf_gc_sweep_symbol_info sweep_info;
6674 
6675       /* Remove the symbols that were in the swept sections from the
6676 	 dynamic symbol table.  */
6677       sweep_info.info = info;
6678       sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6679       elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6680 			      &sweep_info);
6681     }
6682 
6683   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6684     {
6685       asection *s;
6686       struct elf_find_verdep_info sinfo;
6687 
6688       /* Work out the size of the version reference section.  */
6689 
6690       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6691       BFD_ASSERT (s != NULL);
6692 
6693       sinfo.info = info;
6694       sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6695       if (sinfo.vers == 0)
6696 	sinfo.vers = 1;
6697       sinfo.failed = FALSE;
6698 
6699       elf_link_hash_traverse (elf_hash_table (info),
6700 			      _bfd_elf_link_find_version_dependencies,
6701 			      &sinfo);
6702       if (sinfo.failed)
6703 	return FALSE;
6704 
6705       if (elf_tdata (output_bfd)->verref == NULL)
6706 	s->flags |= SEC_EXCLUDE;
6707       else
6708 	{
6709 	  Elf_Internal_Verneed *vn;
6710 	  unsigned int size;
6711 	  unsigned int crefs;
6712 	  bfd_byte *p;
6713 
6714 	  /* Build the version dependency section.  */
6715 	  size = 0;
6716 	  crefs = 0;
6717 	  for (vn = elf_tdata (output_bfd)->verref;
6718 	       vn != NULL;
6719 	       vn = vn->vn_nextref)
6720 	    {
6721 	      Elf_Internal_Vernaux *a;
6722 
6723 	      size += sizeof (Elf_External_Verneed);
6724 	      ++crefs;
6725 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6726 		size += sizeof (Elf_External_Vernaux);
6727 	    }
6728 
6729 	  s->size = size;
6730 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6731 	  if (s->contents == NULL)
6732 	    return FALSE;
6733 
6734 	  p = s->contents;
6735 	  for (vn = elf_tdata (output_bfd)->verref;
6736 	       vn != NULL;
6737 	       vn = vn->vn_nextref)
6738 	    {
6739 	      unsigned int caux;
6740 	      Elf_Internal_Vernaux *a;
6741 	      size_t indx;
6742 
6743 	      caux = 0;
6744 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6745 		++caux;
6746 
6747 	      vn->vn_version = VER_NEED_CURRENT;
6748 	      vn->vn_cnt = caux;
6749 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6750 					  elf_dt_name (vn->vn_bfd) != NULL
6751 					  ? elf_dt_name (vn->vn_bfd)
6752 					  : lbasename (vn->vn_bfd->filename),
6753 					  FALSE);
6754 	      if (indx == (size_t) -1)
6755 		return FALSE;
6756 	      vn->vn_file = indx;
6757 	      vn->vn_aux = sizeof (Elf_External_Verneed);
6758 	      if (vn->vn_nextref == NULL)
6759 		vn->vn_next = 0;
6760 	      else
6761 		vn->vn_next = (sizeof (Elf_External_Verneed)
6762 			       + caux * sizeof (Elf_External_Vernaux));
6763 
6764 	      _bfd_elf_swap_verneed_out (output_bfd, vn,
6765 					 (Elf_External_Verneed *) p);
6766 	      p += sizeof (Elf_External_Verneed);
6767 
6768 	      for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6769 		{
6770 		  a->vna_hash = bfd_elf_hash (a->vna_nodename);
6771 		  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6772 					      a->vna_nodename, FALSE);
6773 		  if (indx == (size_t) -1)
6774 		    return FALSE;
6775 		  a->vna_name = indx;
6776 		  if (a->vna_nextptr == NULL)
6777 		    a->vna_next = 0;
6778 		  else
6779 		    a->vna_next = sizeof (Elf_External_Vernaux);
6780 
6781 		  _bfd_elf_swap_vernaux_out (output_bfd, a,
6782 					     (Elf_External_Vernaux *) p);
6783 		  p += sizeof (Elf_External_Vernaux);
6784 		}
6785 	    }
6786 
6787 	  elf_tdata (output_bfd)->cverrefs = crefs;
6788 	}
6789     }
6790 
6791   /* Any syms created from now on start with -1 in
6792      got.refcount/offset and plt.refcount/offset.  */
6793   elf_hash_table (info)->init_got_refcount
6794     = elf_hash_table (info)->init_got_offset;
6795   elf_hash_table (info)->init_plt_refcount
6796     = elf_hash_table (info)->init_plt_offset;
6797 
6798   if (bfd_link_relocatable (info)
6799       && !_bfd_elf_size_group_sections (info))
6800     return FALSE;
6801 
6802   /* The backend may have to create some sections regardless of whether
6803      we're dynamic or not.  */
6804   if (bed->elf_backend_always_size_sections
6805       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6806     return FALSE;
6807 
6808   /* Determine any GNU_STACK segment requirements, after the backend
6809      has had a chance to set a default segment size.  */
6810   if (info->execstack)
6811     elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6812   else if (info->noexecstack)
6813     elf_stack_flags (output_bfd) = PF_R | PF_W;
6814   else
6815     {
6816       bfd *inputobj;
6817       asection *notesec = NULL;
6818       int exec = 0;
6819 
6820       for (inputobj = info->input_bfds;
6821 	   inputobj;
6822 	   inputobj = inputobj->link.next)
6823 	{
6824 	  asection *s;
6825 
6826 	  if (inputobj->flags
6827 	      & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6828 	    continue;
6829 	  s = inputobj->sections;
6830 	  if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6831 	    continue;
6832 
6833 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6834 	  if (s)
6835 	    {
6836 	      if (s->flags & SEC_CODE)
6837 		exec = PF_X;
6838 	      notesec = s;
6839 	    }
6840 	  else if (bed->default_execstack)
6841 	    exec = PF_X;
6842 	}
6843       if (notesec || info->stacksize > 0)
6844 	elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6845       if (notesec && exec && bfd_link_relocatable (info)
6846 	  && notesec->output_section != bfd_abs_section_ptr)
6847 	notesec->output_section->flags |= SEC_CODE;
6848     }
6849 
6850   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6851     {
6852       struct elf_info_failed eif;
6853       struct elf_link_hash_entry *h;
6854       asection *dynstr;
6855       asection *s;
6856 
6857       *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6858       BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6859 
6860       if (info->symbolic)
6861 	{
6862 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6863 	    return FALSE;
6864 	  info->flags |= DF_SYMBOLIC;
6865 	}
6866 
6867       if (rpath != NULL)
6868 	{
6869 	  size_t indx;
6870 	  bfd_vma tag;
6871 
6872 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6873 				      TRUE);
6874 	  if (indx == (size_t) -1)
6875 	    return FALSE;
6876 
6877 	  tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6878 	  if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6879 	    return FALSE;
6880 	}
6881 
6882       if (filter_shlib != NULL)
6883 	{
6884 	  size_t indx;
6885 
6886 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6887 				      filter_shlib, TRUE);
6888 	  if (indx == (size_t) -1
6889 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6890 	    return FALSE;
6891 	}
6892 
6893       if (auxiliary_filters != NULL)
6894 	{
6895 	  const char * const *p;
6896 
6897 	  for (p = auxiliary_filters; *p != NULL; p++)
6898 	    {
6899 	      size_t indx;
6900 
6901 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6902 					  *p, TRUE);
6903 	      if (indx == (size_t) -1
6904 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6905 		return FALSE;
6906 	    }
6907 	}
6908 
6909       if (audit != NULL)
6910 	{
6911 	  size_t indx;
6912 
6913 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6914 				      TRUE);
6915 	  if (indx == (size_t) -1
6916 	      || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6917 	    return FALSE;
6918 	}
6919 
6920       if (depaudit != NULL)
6921 	{
6922 	  size_t indx;
6923 
6924 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6925 				      TRUE);
6926 	  if (indx == (size_t) -1
6927 	      || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6928 	    return FALSE;
6929 	}
6930 
6931       eif.info = info;
6932       eif.failed = FALSE;
6933 
6934       /* Find all symbols which were defined in a dynamic object and make
6935 	 the backend pick a reasonable value for them.  */
6936       elf_link_hash_traverse (elf_hash_table (info),
6937 			      _bfd_elf_adjust_dynamic_symbol,
6938 			      &eif);
6939       if (eif.failed)
6940 	return FALSE;
6941 
6942       /* Add some entries to the .dynamic section.  We fill in some of the
6943 	 values later, in bfd_elf_final_link, but we must add the entries
6944 	 now so that we know the final size of the .dynamic section.  */
6945 
6946       /* If there are initialization and/or finalization functions to
6947 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
6948       h = (info->init_function
6949 	   ? elf_link_hash_lookup (elf_hash_table (info),
6950 				   info->init_function, FALSE,
6951 				   FALSE, FALSE)
6952 	   : NULL);
6953       if (h != NULL
6954 	  && (h->ref_regular
6955 	      || h->def_regular))
6956 	{
6957 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6958 	    return FALSE;
6959 	}
6960       h = (info->fini_function
6961 	   ? elf_link_hash_lookup (elf_hash_table (info),
6962 				   info->fini_function, FALSE,
6963 				   FALSE, FALSE)
6964 	   : NULL);
6965       if (h != NULL
6966 	  && (h->ref_regular
6967 	      || h->def_regular))
6968 	{
6969 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6970 	    return FALSE;
6971 	}
6972 
6973       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6974       if (s != NULL && s->linker_has_input)
6975 	{
6976 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
6977 	  if (! bfd_link_executable (info))
6978 	    {
6979 	      bfd *sub;
6980 	      asection *o;
6981 
6982 	      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6983 		if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6984 		    && (o = sub->sections) != NULL
6985 		    && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6986 		  for (o = sub->sections; o != NULL; o = o->next)
6987 		    if (elf_section_data (o)->this_hdr.sh_type
6988 			== SHT_PREINIT_ARRAY)
6989 		      {
6990 			_bfd_error_handler
6991 			  (_("%pB: .preinit_array section is not allowed in DSO"),
6992 			   sub);
6993 			break;
6994 		      }
6995 
6996 	      bfd_set_error (bfd_error_nonrepresentable_section);
6997 	      return FALSE;
6998 	    }
6999 
7000 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7001 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7002 	    return FALSE;
7003 	}
7004       s = bfd_get_section_by_name (output_bfd, ".init_array");
7005       if (s != NULL && s->linker_has_input)
7006 	{
7007 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7008 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7009 	    return FALSE;
7010 	}
7011       s = bfd_get_section_by_name (output_bfd, ".fini_array");
7012       if (s != NULL && s->linker_has_input)
7013 	{
7014 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7015 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7016 	    return FALSE;
7017 	}
7018 
7019       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7020       /* If .dynstr is excluded from the link, we don't want any of
7021 	 these tags.  Strictly, we should be checking each section
7022 	 individually;  This quick check covers for the case where
7023 	 someone does a /DISCARD/ : { *(*) }.  */
7024       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7025 	{
7026 	  bfd_size_type strsize;
7027 
7028 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7029 	  if ((info->emit_hash
7030 	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7031 	      || (info->emit_gnu_hash
7032 		  && (bed->record_xhash_symbol == NULL
7033 		      && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7034 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7035 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7036 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7037 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7038 					      bed->s->sizeof_sym))
7039 	    return FALSE;
7040 	}
7041     }
7042 
7043   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7044     return FALSE;
7045 
7046   /* The backend must work out the sizes of all the other dynamic
7047      sections.  */
7048   if (dynobj != NULL
7049       && bed->elf_backend_size_dynamic_sections != NULL
7050       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7051     return FALSE;
7052 
7053   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7054     {
7055       if (elf_tdata (output_bfd)->cverdefs)
7056 	{
7057 	  unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7058 
7059 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7060 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7061 	    return FALSE;
7062 	}
7063 
7064       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7065 	{
7066 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7067 	    return FALSE;
7068 	}
7069       else if (info->flags & DF_BIND_NOW)
7070 	{
7071 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7072 	    return FALSE;
7073 	}
7074 
7075       if (info->flags_1)
7076 	{
7077 	  if (bfd_link_executable (info))
7078 	    info->flags_1 &= ~ (DF_1_INITFIRST
7079 				| DF_1_NODELETE
7080 				| DF_1_NOOPEN);
7081 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7082 	    return FALSE;
7083 	}
7084 
7085       if (elf_tdata (output_bfd)->cverrefs)
7086 	{
7087 	  unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7088 
7089 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7090 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7091 	    return FALSE;
7092 	}
7093 
7094       if ((elf_tdata (output_bfd)->cverrefs == 0
7095 	   && elf_tdata (output_bfd)->cverdefs == 0)
7096 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7097 	{
7098 	  asection *s;
7099 
7100 	  s = bfd_get_linker_section (dynobj, ".gnu.version");
7101 	  s->flags |= SEC_EXCLUDE;
7102 	}
7103     }
7104   return TRUE;
7105 }
7106 
7107 /* Find the first non-excluded output section.  We'll use its
7108    section symbol for some emitted relocs.  */
7109 void
7110 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7111 {
7112   asection *s;
7113   asection *found = NULL;
7114 
7115   for (s = output_bfd->sections; s != NULL; s = s->next)
7116     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7117 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7118       {
7119 	found = s;
7120 	if ((s->flags & SEC_THREAD_LOCAL) == 0)
7121 	  break;
7122       }
7123   elf_hash_table (info)->text_index_section = found;
7124 }
7125 
7126 /* Find two non-excluded output sections, one for code, one for data.
7127    We'll use their section symbols for some emitted relocs.  */
7128 void
7129 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7130 {
7131   asection *s;
7132   asection *found = NULL;
7133 
7134   /* Data first, since setting text_index_section changes
7135      _bfd_elf_omit_section_dynsym_default.  */
7136   for (s = output_bfd->sections; s != NULL; s = s->next)
7137     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7138 	&& !(s->flags & SEC_READONLY)
7139 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7140       {
7141 	found = s;
7142 	if ((s->flags & SEC_THREAD_LOCAL) == 0)
7143 	  break;
7144       }
7145   elf_hash_table (info)->data_index_section = found;
7146 
7147   for (s = output_bfd->sections; s != NULL; s = s->next)
7148     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7149 	&& (s->flags & SEC_READONLY)
7150 	&& !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7151       {
7152 	found = s;
7153 	break;
7154       }
7155   elf_hash_table (info)->text_index_section = found;
7156 }
7157 
7158 #define GNU_HASH_SECTION_NAME(bed)			    \
7159   (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7160 
7161 bfd_boolean
7162 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7163 {
7164   const struct elf_backend_data *bed;
7165   unsigned long section_sym_count;
7166   bfd_size_type dynsymcount = 0;
7167 
7168   if (!is_elf_hash_table (info->hash))
7169     return TRUE;
7170 
7171   bed = get_elf_backend_data (output_bfd);
7172   (*bed->elf_backend_init_index_section) (output_bfd, info);
7173 
7174   /* Assign dynsym indices.  In a shared library we generate a section
7175      symbol for each output section, which come first.  Next come all
7176      of the back-end allocated local dynamic syms, followed by the rest
7177      of the global symbols.
7178 
7179      This is usually not needed for static binaries, however backends
7180      can request to always do it, e.g. the MIPS backend uses dynamic
7181      symbol counts to lay out GOT, which will be produced in the
7182      presence of GOT relocations even in static binaries (holding fixed
7183      data in that case, to satisfy those relocations).  */
7184 
7185   if (elf_hash_table (info)->dynamic_sections_created
7186       || bed->always_renumber_dynsyms)
7187     dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7188 						  &section_sym_count);
7189 
7190   if (elf_hash_table (info)->dynamic_sections_created)
7191     {
7192       bfd *dynobj;
7193       asection *s;
7194       unsigned int dtagcount;
7195 
7196       dynobj = elf_hash_table (info)->dynobj;
7197 
7198       /* Work out the size of the symbol version section.  */
7199       s = bfd_get_linker_section (dynobj, ".gnu.version");
7200       BFD_ASSERT (s != NULL);
7201       if ((s->flags & SEC_EXCLUDE) == 0)
7202 	{
7203 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
7204 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7205 	  if (s->contents == NULL)
7206 	    return FALSE;
7207 
7208 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7209 	    return FALSE;
7210 	}
7211 
7212       /* Set the size of the .dynsym and .hash sections.  We counted
7213 	 the number of dynamic symbols in elf_link_add_object_symbols.
7214 	 We will build the contents of .dynsym and .hash when we build
7215 	 the final symbol table, because until then we do not know the
7216 	 correct value to give the symbols.  We built the .dynstr
7217 	 section as we went along in elf_link_add_object_symbols.  */
7218       s = elf_hash_table (info)->dynsym;
7219       BFD_ASSERT (s != NULL);
7220       s->size = dynsymcount * bed->s->sizeof_sym;
7221 
7222       s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7223       if (s->contents == NULL)
7224 	return FALSE;
7225 
7226       /* The first entry in .dynsym is a dummy symbol.  Clear all the
7227 	 section syms, in case we don't output them all.  */
7228       ++section_sym_count;
7229       memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7230 
7231       elf_hash_table (info)->bucketcount = 0;
7232 
7233       /* Compute the size of the hashing table.  As a side effect this
7234 	 computes the hash values for all the names we export.  */
7235       if (info->emit_hash)
7236 	{
7237 	  unsigned long int *hashcodes;
7238 	  struct hash_codes_info hashinf;
7239 	  bfd_size_type amt;
7240 	  unsigned long int nsyms;
7241 	  size_t bucketcount;
7242 	  size_t hash_entry_size;
7243 
7244 	  /* Compute the hash values for all exported symbols.  At the same
7245 	     time store the values in an array so that we could use them for
7246 	     optimizations.  */
7247 	  amt = dynsymcount * sizeof (unsigned long int);
7248 	  hashcodes = (unsigned long int *) bfd_malloc (amt);
7249 	  if (hashcodes == NULL)
7250 	    return FALSE;
7251 	  hashinf.hashcodes = hashcodes;
7252 	  hashinf.error = FALSE;
7253 
7254 	  /* Put all hash values in HASHCODES.  */
7255 	  elf_link_hash_traverse (elf_hash_table (info),
7256 				  elf_collect_hash_codes, &hashinf);
7257 	  if (hashinf.error)
7258 	    {
7259 	      free (hashcodes);
7260 	      return FALSE;
7261 	    }
7262 
7263 	  nsyms = hashinf.hashcodes - hashcodes;
7264 	  bucketcount
7265 	    = compute_bucket_count (info, hashcodes, nsyms, 0);
7266 	  free (hashcodes);
7267 
7268 	  if (bucketcount == 0 && nsyms > 0)
7269 	    return FALSE;
7270 
7271 	  elf_hash_table (info)->bucketcount = bucketcount;
7272 
7273 	  s = bfd_get_linker_section (dynobj, ".hash");
7274 	  BFD_ASSERT (s != NULL);
7275 	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7276 	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7277 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7278 	  if (s->contents == NULL)
7279 	    return FALSE;
7280 
7281 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7282 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7283 		   s->contents + hash_entry_size);
7284 	}
7285 
7286       if (info->emit_gnu_hash)
7287 	{
7288 	  size_t i, cnt;
7289 	  unsigned char *contents;
7290 	  struct collect_gnu_hash_codes cinfo;
7291 	  bfd_size_type amt;
7292 	  size_t bucketcount;
7293 
7294 	  memset (&cinfo, 0, sizeof (cinfo));
7295 
7296 	  /* Compute the hash values for all exported symbols.  At the same
7297 	     time store the values in an array so that we could use them for
7298 	     optimizations.  */
7299 	  amt = dynsymcount * 2 * sizeof (unsigned long int);
7300 	  cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7301 	  if (cinfo.hashcodes == NULL)
7302 	    return FALSE;
7303 
7304 	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
7305 	  cinfo.min_dynindx = -1;
7306 	  cinfo.output_bfd = output_bfd;
7307 	  cinfo.bed = bed;
7308 
7309 	  /* Put all hash values in HASHCODES.  */
7310 	  elf_link_hash_traverse (elf_hash_table (info),
7311 				  elf_collect_gnu_hash_codes, &cinfo);
7312 	  if (cinfo.error)
7313 	    {
7314 	      free (cinfo.hashcodes);
7315 	      return FALSE;
7316 	    }
7317 
7318 	  bucketcount
7319 	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7320 
7321 	  if (bucketcount == 0)
7322 	    {
7323 	      free (cinfo.hashcodes);
7324 	      return FALSE;
7325 	    }
7326 
7327 	  s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7328 	  BFD_ASSERT (s != NULL);
7329 
7330 	  if (cinfo.nsyms == 0)
7331 	    {
7332 	      /* Empty .gnu.hash or .MIPS.xhash section is special.  */
7333 	      BFD_ASSERT (cinfo.min_dynindx == -1);
7334 	      free (cinfo.hashcodes);
7335 	      s->size = 5 * 4 + bed->s->arch_size / 8;
7336 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7337 	      if (contents == NULL)
7338 		return FALSE;
7339 	      s->contents = contents;
7340 	      /* 1 empty bucket.  */
7341 	      bfd_put_32 (output_bfd, 1, contents);
7342 	      /* SYMIDX above the special symbol 0.  */
7343 	      bfd_put_32 (output_bfd, 1, contents + 4);
7344 	      /* Just one word for bitmask.  */
7345 	      bfd_put_32 (output_bfd, 1, contents + 8);
7346 	      /* Only hash fn bloom filter.  */
7347 	      bfd_put_32 (output_bfd, 0, contents + 12);
7348 	      /* No hashes are valid - empty bitmask.  */
7349 	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7350 	      /* No hashes in the only bucket.  */
7351 	      bfd_put_32 (output_bfd, 0,
7352 			  contents + 16 + bed->s->arch_size / 8);
7353 	    }
7354 	  else
7355 	    {
7356 	      unsigned long int maskwords, maskbitslog2, x;
7357 	      BFD_ASSERT (cinfo.min_dynindx != -1);
7358 
7359 	      x = cinfo.nsyms;
7360 	      maskbitslog2 = 1;
7361 	      while ((x >>= 1) != 0)
7362 		++maskbitslog2;
7363 	      if (maskbitslog2 < 3)
7364 		maskbitslog2 = 5;
7365 	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7366 		maskbitslog2 = maskbitslog2 + 3;
7367 	      else
7368 		maskbitslog2 = maskbitslog2 + 2;
7369 	      if (bed->s->arch_size == 64)
7370 		{
7371 		  if (maskbitslog2 == 5)
7372 		    maskbitslog2 = 6;
7373 		  cinfo.shift1 = 6;
7374 		}
7375 	      else
7376 		cinfo.shift1 = 5;
7377 	      cinfo.mask = (1 << cinfo.shift1) - 1;
7378 	      cinfo.shift2 = maskbitslog2;
7379 	      cinfo.maskbits = 1 << maskbitslog2;
7380 	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7381 	      amt = bucketcount * sizeof (unsigned long int) * 2;
7382 	      amt += maskwords * sizeof (bfd_vma);
7383 	      cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7384 	      if (cinfo.bitmask == NULL)
7385 		{
7386 		  free (cinfo.hashcodes);
7387 		  return FALSE;
7388 		}
7389 
7390 	      cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7391 	      cinfo.indx = cinfo.counts + bucketcount;
7392 	      cinfo.symindx = dynsymcount - cinfo.nsyms;
7393 	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7394 
7395 	      /* Determine how often each hash bucket is used.  */
7396 	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7397 	      for (i = 0; i < cinfo.nsyms; ++i)
7398 		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7399 
7400 	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7401 		if (cinfo.counts[i] != 0)
7402 		  {
7403 		    cinfo.indx[i] = cnt;
7404 		    cnt += cinfo.counts[i];
7405 		  }
7406 	      BFD_ASSERT (cnt == dynsymcount);
7407 	      cinfo.bucketcount = bucketcount;
7408 	      cinfo.local_indx = cinfo.min_dynindx;
7409 
7410 	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7411 	      s->size += cinfo.maskbits / 8;
7412 	      if (bed->record_xhash_symbol != NULL)
7413 		s->size += cinfo.nsyms * 4;
7414 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7415 	      if (contents == NULL)
7416 		{
7417 		  free (cinfo.bitmask);
7418 		  free (cinfo.hashcodes);
7419 		  return FALSE;
7420 		}
7421 
7422 	      s->contents = contents;
7423 	      bfd_put_32 (output_bfd, bucketcount, contents);
7424 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7425 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
7426 	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7427 	      contents += 16 + cinfo.maskbits / 8;
7428 
7429 	      for (i = 0; i < bucketcount; ++i)
7430 		{
7431 		  if (cinfo.counts[i] == 0)
7432 		    bfd_put_32 (output_bfd, 0, contents);
7433 		  else
7434 		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7435 		  contents += 4;
7436 		}
7437 
7438 	      cinfo.contents = contents;
7439 
7440 	      cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7441 	      /* Renumber dynamic symbols, if populating .gnu.hash section.
7442 		 If using .MIPS.xhash, populate the translation table.  */
7443 	      elf_link_hash_traverse (elf_hash_table (info),
7444 				      elf_gnu_hash_process_symidx, &cinfo);
7445 
7446 	      contents = s->contents + 16;
7447 	      for (i = 0; i < maskwords; ++i)
7448 		{
7449 		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7450 			   contents);
7451 		  contents += bed->s->arch_size / 8;
7452 		}
7453 
7454 	      free (cinfo.bitmask);
7455 	      free (cinfo.hashcodes);
7456 	    }
7457 	}
7458 
7459       s = bfd_get_linker_section (dynobj, ".dynstr");
7460       BFD_ASSERT (s != NULL);
7461 
7462       elf_finalize_dynstr (output_bfd, info);
7463 
7464       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7465 
7466       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7467 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7468 	  return FALSE;
7469     }
7470 
7471   return TRUE;
7472 }
7473 
7474 /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
7475 
7476 static void
7477 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7478 			    asection *sec)
7479 {
7480   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7481   sec->sec_info_type = SEC_INFO_TYPE_NONE;
7482 }
7483 
7484 /* Finish SHF_MERGE section merging.  */
7485 
7486 bfd_boolean
7487 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7488 {
7489   bfd *ibfd;
7490   asection *sec;
7491 
7492   if (!is_elf_hash_table (info->hash))
7493     return FALSE;
7494 
7495   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7496     if ((ibfd->flags & DYNAMIC) == 0
7497 	&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7498 	&& (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7499 	    == get_elf_backend_data (obfd)->s->elfclass))
7500       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7501 	if ((sec->flags & SEC_MERGE) != 0
7502 	    && !bfd_is_abs_section (sec->output_section))
7503 	  {
7504 	    struct bfd_elf_section_data *secdata;
7505 
7506 	    secdata = elf_section_data (sec);
7507 	    if (! _bfd_add_merge_section (obfd,
7508 					  &elf_hash_table (info)->merge_info,
7509 					  sec, &secdata->sec_info))
7510 	      return FALSE;
7511 	    else if (secdata->sec_info)
7512 	      sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7513 	  }
7514 
7515   if (elf_hash_table (info)->merge_info != NULL)
7516     _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7517 			 merge_sections_remove_hook);
7518   return TRUE;
7519 }
7520 
7521 /* Create an entry in an ELF linker hash table.  */
7522 
7523 struct bfd_hash_entry *
7524 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7525 			    struct bfd_hash_table *table,
7526 			    const char *string)
7527 {
7528   /* Allocate the structure if it has not already been allocated by a
7529      subclass.  */
7530   if (entry == NULL)
7531     {
7532       entry = (struct bfd_hash_entry *)
7533 	bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7534       if (entry == NULL)
7535 	return entry;
7536     }
7537 
7538   /* Call the allocation method of the superclass.  */
7539   entry = _bfd_link_hash_newfunc (entry, table, string);
7540   if (entry != NULL)
7541     {
7542       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7543       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7544 
7545       /* Set local fields.  */
7546       ret->indx = -1;
7547       ret->dynindx = -1;
7548       ret->got = htab->init_got_refcount;
7549       ret->plt = htab->init_plt_refcount;
7550       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7551 			      - offsetof (struct elf_link_hash_entry, size)));
7552       /* Assume that we have been called by a non-ELF symbol reader.
7553 	 This flag is then reset by the code which reads an ELF input
7554 	 file.  This ensures that a symbol created by a non-ELF symbol
7555 	 reader will have the flag set correctly.  */
7556       ret->non_elf = 1;
7557     }
7558 
7559   return entry;
7560 }
7561 
7562 /* Copy data from an indirect symbol to its direct symbol, hiding the
7563    old indirect symbol.  Also used for copying flags to a weakdef.  */
7564 
7565 void
7566 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7567 				  struct elf_link_hash_entry *dir,
7568 				  struct elf_link_hash_entry *ind)
7569 {
7570   struct elf_link_hash_table *htab;
7571 
7572   /* Copy down any references that we may have already seen to the
7573      symbol which just became indirect.  */
7574 
7575   if (dir->versioned != versioned_hidden)
7576     dir->ref_dynamic |= ind->ref_dynamic;
7577   dir->ref_regular |= ind->ref_regular;
7578   dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7579   dir->non_got_ref |= ind->non_got_ref;
7580   dir->needs_plt |= ind->needs_plt;
7581   dir->pointer_equality_needed |= ind->pointer_equality_needed;
7582 
7583   if (ind->root.type != bfd_link_hash_indirect)
7584     return;
7585 
7586   /* Copy over the global and procedure linkage table refcount entries.
7587      These may have been already set up by a check_relocs routine.  */
7588   htab = elf_hash_table (info);
7589   if (ind->got.refcount > htab->init_got_refcount.refcount)
7590     {
7591       if (dir->got.refcount < 0)
7592 	dir->got.refcount = 0;
7593       dir->got.refcount += ind->got.refcount;
7594       ind->got.refcount = htab->init_got_refcount.refcount;
7595     }
7596 
7597   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7598     {
7599       if (dir->plt.refcount < 0)
7600 	dir->plt.refcount = 0;
7601       dir->plt.refcount += ind->plt.refcount;
7602       ind->plt.refcount = htab->init_plt_refcount.refcount;
7603     }
7604 
7605   if (ind->dynindx != -1)
7606     {
7607       if (dir->dynindx != -1)
7608 	_bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7609       dir->dynindx = ind->dynindx;
7610       dir->dynstr_index = ind->dynstr_index;
7611       ind->dynindx = -1;
7612       ind->dynstr_index = 0;
7613     }
7614 }
7615 
7616 void
7617 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7618 				struct elf_link_hash_entry *h,
7619 				bfd_boolean force_local)
7620 {
7621   /* STT_GNU_IFUNC symbol must go through PLT.  */
7622   if (h->type != STT_GNU_IFUNC)
7623     {
7624       h->plt = elf_hash_table (info)->init_plt_offset;
7625       h->needs_plt = 0;
7626     }
7627   if (force_local)
7628     {
7629       h->forced_local = 1;
7630       if (h->dynindx != -1)
7631 	{
7632 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7633 				  h->dynstr_index);
7634 	  h->dynindx = -1;
7635 	  h->dynstr_index = 0;
7636 	}
7637     }
7638 }
7639 
7640 /* Hide a symbol. */
7641 
7642 void
7643 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7644 			   struct bfd_link_info *info,
7645 			   struct bfd_link_hash_entry *h)
7646 {
7647   if (is_elf_hash_table (info->hash))
7648     {
7649       const struct elf_backend_data *bed
7650 	= get_elf_backend_data (output_bfd);
7651       struct elf_link_hash_entry *eh
7652 	= (struct elf_link_hash_entry *) h;
7653       bed->elf_backend_hide_symbol (info, eh, TRUE);
7654       eh->def_dynamic = 0;
7655       eh->ref_dynamic = 0;
7656       eh->dynamic_def = 0;
7657     }
7658 }
7659 
7660 /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
7661    caller.  */
7662 
7663 bfd_boolean
7664 _bfd_elf_link_hash_table_init
7665   (struct elf_link_hash_table *table,
7666    bfd *abfd,
7667    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7668 				      struct bfd_hash_table *,
7669 				      const char *),
7670    unsigned int entsize,
7671    enum elf_target_id target_id)
7672 {
7673   bfd_boolean ret;
7674   int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7675 
7676   table->init_got_refcount.refcount = can_refcount - 1;
7677   table->init_plt_refcount.refcount = can_refcount - 1;
7678   table->init_got_offset.offset = -(bfd_vma) 1;
7679   table->init_plt_offset.offset = -(bfd_vma) 1;
7680   /* The first dynamic symbol is a dummy.  */
7681   table->dynsymcount = 1;
7682 
7683   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7684 
7685   table->root.type = bfd_link_elf_hash_table;
7686   table->hash_table_id = target_id;
7687 
7688   return ret;
7689 }
7690 
7691 /* Create an ELF linker hash table.  */
7692 
7693 struct bfd_link_hash_table *
7694 _bfd_elf_link_hash_table_create (bfd *abfd)
7695 {
7696   struct elf_link_hash_table *ret;
7697   bfd_size_type amt = sizeof (struct elf_link_hash_table);
7698 
7699   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7700   if (ret == NULL)
7701     return NULL;
7702 
7703   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7704 				       sizeof (struct elf_link_hash_entry),
7705 				       GENERIC_ELF_DATA))
7706     {
7707       free (ret);
7708       return NULL;
7709     }
7710   ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7711 
7712   return &ret->root;
7713 }
7714 
7715 /* Destroy an ELF linker hash table.  */
7716 
7717 void
7718 _bfd_elf_link_hash_table_free (bfd *obfd)
7719 {
7720   struct elf_link_hash_table *htab;
7721 
7722   htab = (struct elf_link_hash_table *) obfd->link.hash;
7723   if (htab->dynstr != NULL)
7724     _bfd_elf_strtab_free (htab->dynstr);
7725   _bfd_merge_sections_free (htab->merge_info);
7726   _bfd_generic_link_hash_table_free (obfd);
7727 }
7728 
7729 /* This is a hook for the ELF emulation code in the generic linker to
7730    tell the backend linker what file name to use for the DT_NEEDED
7731    entry for a dynamic object.  */
7732 
7733 void
7734 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7735 {
7736   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7737       && bfd_get_format (abfd) == bfd_object)
7738     elf_dt_name (abfd) = name;
7739 }
7740 
7741 int
7742 bfd_elf_get_dyn_lib_class (bfd *abfd)
7743 {
7744   int lib_class;
7745   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7746       && bfd_get_format (abfd) == bfd_object)
7747     lib_class = elf_dyn_lib_class (abfd);
7748   else
7749     lib_class = 0;
7750   return lib_class;
7751 }
7752 
7753 void
7754 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7755 {
7756   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7757       && bfd_get_format (abfd) == bfd_object)
7758     elf_dyn_lib_class (abfd) = lib_class;
7759 }
7760 
7761 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
7762    the linker ELF emulation code.  */
7763 
7764 struct bfd_link_needed_list *
7765 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7766 			 struct bfd_link_info *info)
7767 {
7768   if (! is_elf_hash_table (info->hash))
7769     return NULL;
7770   return elf_hash_table (info)->needed;
7771 }
7772 
7773 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
7774    hook for the linker ELF emulation code.  */
7775 
7776 struct bfd_link_needed_list *
7777 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7778 			  struct bfd_link_info *info)
7779 {
7780   if (! is_elf_hash_table (info->hash))
7781     return NULL;
7782   return elf_hash_table (info)->runpath;
7783 }
7784 
7785 /* Get the name actually used for a dynamic object for a link.  This
7786    is the SONAME entry if there is one.  Otherwise, it is the string
7787    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
7788 
7789 const char *
7790 bfd_elf_get_dt_soname (bfd *abfd)
7791 {
7792   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7793       && bfd_get_format (abfd) == bfd_object)
7794     return elf_dt_name (abfd);
7795   return NULL;
7796 }
7797 
7798 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
7799    the ELF linker emulation code.  */
7800 
7801 bfd_boolean
7802 bfd_elf_get_bfd_needed_list (bfd *abfd,
7803 			     struct bfd_link_needed_list **pneeded)
7804 {
7805   asection *s;
7806   bfd_byte *dynbuf = NULL;
7807   unsigned int elfsec;
7808   unsigned long shlink;
7809   bfd_byte *extdyn, *extdynend;
7810   size_t extdynsize;
7811   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7812 
7813   *pneeded = NULL;
7814 
7815   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7816       || bfd_get_format (abfd) != bfd_object)
7817     return TRUE;
7818 
7819   s = bfd_get_section_by_name (abfd, ".dynamic");
7820   if (s == NULL || s->size == 0)
7821     return TRUE;
7822 
7823   if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7824     goto error_return;
7825 
7826   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7827   if (elfsec == SHN_BAD)
7828     goto error_return;
7829 
7830   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7831 
7832   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7833   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7834 
7835   extdyn = dynbuf;
7836   extdynend = extdyn + s->size;
7837   for (; extdyn < extdynend; extdyn += extdynsize)
7838     {
7839       Elf_Internal_Dyn dyn;
7840 
7841       (*swap_dyn_in) (abfd, extdyn, &dyn);
7842 
7843       if (dyn.d_tag == DT_NULL)
7844 	break;
7845 
7846       if (dyn.d_tag == DT_NEEDED)
7847 	{
7848 	  const char *string;
7849 	  struct bfd_link_needed_list *l;
7850 	  unsigned int tagv = dyn.d_un.d_val;
7851 	  bfd_size_type amt;
7852 
7853 	  string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7854 	  if (string == NULL)
7855 	    goto error_return;
7856 
7857 	  amt = sizeof *l;
7858 	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7859 	  if (l == NULL)
7860 	    goto error_return;
7861 
7862 	  l->by = abfd;
7863 	  l->name = string;
7864 	  l->next = *pneeded;
7865 	  *pneeded = l;
7866 	}
7867     }
7868 
7869   free (dynbuf);
7870 
7871   return TRUE;
7872 
7873  error_return:
7874   if (dynbuf != NULL)
7875     free (dynbuf);
7876   return FALSE;
7877 }
7878 
7879 struct elf_symbuf_symbol
7880 {
7881   unsigned long st_name;	/* Symbol name, index in string tbl */
7882   unsigned char st_info;	/* Type and binding attributes */
7883   unsigned char st_other;	/* Visibilty, and target specific */
7884 };
7885 
7886 struct elf_symbuf_head
7887 {
7888   struct elf_symbuf_symbol *ssym;
7889   size_t count;
7890   unsigned int st_shndx;
7891 };
7892 
7893 struct elf_symbol
7894 {
7895   union
7896     {
7897       Elf_Internal_Sym *isym;
7898       struct elf_symbuf_symbol *ssym;
7899       void *p;
7900     } u;
7901   const char *name;
7902 };
7903 
7904 /* Sort references to symbols by ascending section number.  */
7905 
7906 static int
7907 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7908 {
7909   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7910   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7911 
7912   if (s1->st_shndx != s2->st_shndx)
7913     return s1->st_shndx > s2->st_shndx ? 1 : -1;
7914   /* Final sort by the address of the sym in the symbuf ensures
7915      a stable sort.  */
7916   if (s1 != s2)
7917     return s1 > s2 ? 1 : -1;
7918   return 0;
7919 }
7920 
7921 static int
7922 elf_sym_name_compare (const void *arg1, const void *arg2)
7923 {
7924   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7925   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7926   int ret = strcmp (s1->name, s2->name);
7927   if (ret != 0)
7928     return ret;
7929   if (s1->u.p != s2->u.p)
7930     return s1->u.p > s2->u.p ? 1 : -1;
7931   return 0;
7932 }
7933 
7934 static struct elf_symbuf_head *
7935 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7936 {
7937   Elf_Internal_Sym **ind, **indbufend, **indbuf;
7938   struct elf_symbuf_symbol *ssym;
7939   struct elf_symbuf_head *ssymbuf, *ssymhead;
7940   size_t i, shndx_count, total_size;
7941 
7942   indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7943   if (indbuf == NULL)
7944     return NULL;
7945 
7946   for (ind = indbuf, i = 0; i < symcount; i++)
7947     if (isymbuf[i].st_shndx != SHN_UNDEF)
7948       *ind++ = &isymbuf[i];
7949   indbufend = ind;
7950 
7951   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7952 	 elf_sort_elf_symbol);
7953 
7954   shndx_count = 0;
7955   if (indbufend > indbuf)
7956     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7957       if (ind[0]->st_shndx != ind[1]->st_shndx)
7958 	shndx_count++;
7959 
7960   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7961 		+ (indbufend - indbuf) * sizeof (*ssym));
7962   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7963   if (ssymbuf == NULL)
7964     {
7965       free (indbuf);
7966       return NULL;
7967     }
7968 
7969   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7970   ssymbuf->ssym = NULL;
7971   ssymbuf->count = shndx_count;
7972   ssymbuf->st_shndx = 0;
7973   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7974     {
7975       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7976 	{
7977 	  ssymhead++;
7978 	  ssymhead->ssym = ssym;
7979 	  ssymhead->count = 0;
7980 	  ssymhead->st_shndx = (*ind)->st_shndx;
7981 	}
7982       ssym->st_name = (*ind)->st_name;
7983       ssym->st_info = (*ind)->st_info;
7984       ssym->st_other = (*ind)->st_other;
7985       ssymhead->count++;
7986     }
7987   BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7988 	      && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7989 		  == total_size));
7990 
7991   free (indbuf);
7992   return ssymbuf;
7993 }
7994 
7995 /* Check if 2 sections define the same set of local and global
7996    symbols.  */
7997 
7998 static bfd_boolean
7999 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8000 				   struct bfd_link_info *info)
8001 {
8002   bfd *bfd1, *bfd2;
8003   const struct elf_backend_data *bed1, *bed2;
8004   Elf_Internal_Shdr *hdr1, *hdr2;
8005   size_t symcount1, symcount2;
8006   Elf_Internal_Sym *isymbuf1, *isymbuf2;
8007   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8008   Elf_Internal_Sym *isym, *isymend;
8009   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8010   size_t count1, count2, i;
8011   unsigned int shndx1, shndx2;
8012   bfd_boolean result;
8013 
8014   bfd1 = sec1->owner;
8015   bfd2 = sec2->owner;
8016 
8017   /* Both sections have to be in ELF.  */
8018   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8019       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8020     return FALSE;
8021 
8022   if (elf_section_type (sec1) != elf_section_type (sec2))
8023     return FALSE;
8024 
8025   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8026   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8027   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8028     return FALSE;
8029 
8030   bed1 = get_elf_backend_data (bfd1);
8031   bed2 = get_elf_backend_data (bfd2);
8032   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8033   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8034   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8035   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8036 
8037   if (symcount1 == 0 || symcount2 == 0)
8038     return FALSE;
8039 
8040   result = FALSE;
8041   isymbuf1 = NULL;
8042   isymbuf2 = NULL;
8043   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8044   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8045 
8046   if (ssymbuf1 == NULL)
8047     {
8048       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8049 				       NULL, NULL, NULL);
8050       if (isymbuf1 == NULL)
8051 	goto done;
8052 
8053       if (!info->reduce_memory_overheads)
8054 	{
8055 	  ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8056 	  elf_tdata (bfd1)->symbuf = ssymbuf1;
8057 	}
8058     }
8059 
8060   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8061     {
8062       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8063 				       NULL, NULL, NULL);
8064       if (isymbuf2 == NULL)
8065 	goto done;
8066 
8067       if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
8068 	{
8069 	  ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8070 	  elf_tdata (bfd2)->symbuf = ssymbuf2;
8071 	}
8072     }
8073 
8074   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8075     {
8076       /* Optimized faster version.  */
8077       size_t lo, hi, mid;
8078       struct elf_symbol *symp;
8079       struct elf_symbuf_symbol *ssym, *ssymend;
8080 
8081       lo = 0;
8082       hi = ssymbuf1->count;
8083       ssymbuf1++;
8084       count1 = 0;
8085       while (lo < hi)
8086 	{
8087 	  mid = (lo + hi) / 2;
8088 	  if (shndx1 < ssymbuf1[mid].st_shndx)
8089 	    hi = mid;
8090 	  else if (shndx1 > ssymbuf1[mid].st_shndx)
8091 	    lo = mid + 1;
8092 	  else
8093 	    {
8094 	      count1 = ssymbuf1[mid].count;
8095 	      ssymbuf1 += mid;
8096 	      break;
8097 	    }
8098 	}
8099 
8100       lo = 0;
8101       hi = ssymbuf2->count;
8102       ssymbuf2++;
8103       count2 = 0;
8104       while (lo < hi)
8105 	{
8106 	  mid = (lo + hi) / 2;
8107 	  if (shndx2 < ssymbuf2[mid].st_shndx)
8108 	    hi = mid;
8109 	  else if (shndx2 > ssymbuf2[mid].st_shndx)
8110 	    lo = mid + 1;
8111 	  else
8112 	    {
8113 	      count2 = ssymbuf2[mid].count;
8114 	      ssymbuf2 += mid;
8115 	      break;
8116 	    }
8117 	}
8118 
8119       if (count1 == 0 || count2 == 0 || count1 != count2)
8120 	goto done;
8121 
8122       symtable1
8123 	= (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8124       symtable2
8125 	= (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8126       if (symtable1 == NULL || symtable2 == NULL)
8127 	goto done;
8128 
8129       symp = symtable1;
8130       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8131 	   ssym < ssymend; ssym++, symp++)
8132 	{
8133 	  symp->u.ssym = ssym;
8134 	  symp->name = bfd_elf_string_from_elf_section (bfd1,
8135 							hdr1->sh_link,
8136 							ssym->st_name);
8137 	}
8138 
8139       symp = symtable2;
8140       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8141 	   ssym < ssymend; ssym++, symp++)
8142 	{
8143 	  symp->u.ssym = ssym;
8144 	  symp->name = bfd_elf_string_from_elf_section (bfd2,
8145 							hdr2->sh_link,
8146 							ssym->st_name);
8147 	}
8148 
8149       /* Sort symbol by name.  */
8150       qsort (symtable1, count1, sizeof (struct elf_symbol),
8151 	     elf_sym_name_compare);
8152       qsort (symtable2, count1, sizeof (struct elf_symbol),
8153 	     elf_sym_name_compare);
8154 
8155       for (i = 0; i < count1; i++)
8156 	/* Two symbols must have the same binding, type and name.  */
8157 	if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8158 	    || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8159 	    || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8160 	  goto done;
8161 
8162       result = TRUE;
8163       goto done;
8164     }
8165 
8166   symtable1 = (struct elf_symbol *)
8167       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8168   symtable2 = (struct elf_symbol *)
8169       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8170   if (symtable1 == NULL || symtable2 == NULL)
8171     goto done;
8172 
8173   /* Count definitions in the section.  */
8174   count1 = 0;
8175   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8176     if (isym->st_shndx == shndx1)
8177       symtable1[count1++].u.isym = isym;
8178 
8179   count2 = 0;
8180   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8181     if (isym->st_shndx == shndx2)
8182       symtable2[count2++].u.isym = isym;
8183 
8184   if (count1 == 0 || count2 == 0 || count1 != count2)
8185     goto done;
8186 
8187   for (i = 0; i < count1; i++)
8188     symtable1[i].name
8189       = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8190 					 symtable1[i].u.isym->st_name);
8191 
8192   for (i = 0; i < count2; i++)
8193     symtable2[i].name
8194       = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8195 					 symtable2[i].u.isym->st_name);
8196 
8197   /* Sort symbol by name.  */
8198   qsort (symtable1, count1, sizeof (struct elf_symbol),
8199 	 elf_sym_name_compare);
8200   qsort (symtable2, count1, sizeof (struct elf_symbol),
8201 	 elf_sym_name_compare);
8202 
8203   for (i = 0; i < count1; i++)
8204     /* Two symbols must have the same binding, type and name.  */
8205     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8206 	|| symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8207 	|| strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8208       goto done;
8209 
8210   result = TRUE;
8211 
8212 done:
8213   if (symtable1)
8214     free (symtable1);
8215   if (symtable2)
8216     free (symtable2);
8217   if (isymbuf1)
8218     free (isymbuf1);
8219   if (isymbuf2)
8220     free (isymbuf2);
8221 
8222   return result;
8223 }
8224 
8225 /* Return TRUE if 2 section types are compatible.  */
8226 
8227 bfd_boolean
8228 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8229 				 bfd *bbfd, const asection *bsec)
8230 {
8231   if (asec == NULL
8232       || bsec == NULL
8233       || abfd->xvec->flavour != bfd_target_elf_flavour
8234       || bbfd->xvec->flavour != bfd_target_elf_flavour)
8235     return TRUE;
8236 
8237   return elf_section_type (asec) == elf_section_type (bsec);
8238 }
8239 
8240 /* Final phase of ELF linker.  */
8241 
8242 /* A structure we use to avoid passing large numbers of arguments.  */
8243 
8244 struct elf_final_link_info
8245 {
8246   /* General link information.  */
8247   struct bfd_link_info *info;
8248   /* Output BFD.  */
8249   bfd *output_bfd;
8250   /* Symbol string table.  */
8251   struct elf_strtab_hash *symstrtab;
8252   /* .hash section.  */
8253   asection *hash_sec;
8254   /* symbol version section (.gnu.version).  */
8255   asection *symver_sec;
8256   /* Buffer large enough to hold contents of any section.  */
8257   bfd_byte *contents;
8258   /* Buffer large enough to hold external relocs of any section.  */
8259   void *external_relocs;
8260   /* Buffer large enough to hold internal relocs of any section.  */
8261   Elf_Internal_Rela *internal_relocs;
8262   /* Buffer large enough to hold external local symbols of any input
8263      BFD.  */
8264   bfd_byte *external_syms;
8265   /* And a buffer for symbol section indices.  */
8266   Elf_External_Sym_Shndx *locsym_shndx;
8267   /* Buffer large enough to hold internal local symbols of any input
8268      BFD.  */
8269   Elf_Internal_Sym *internal_syms;
8270   /* Array large enough to hold a symbol index for each local symbol
8271      of any input BFD.  */
8272   long *indices;
8273   /* Array large enough to hold a section pointer for each local
8274      symbol of any input BFD.  */
8275   asection **sections;
8276   /* Buffer for SHT_SYMTAB_SHNDX section.  */
8277   Elf_External_Sym_Shndx *symshndxbuf;
8278   /* Number of STT_FILE syms seen.  */
8279   size_t filesym_count;
8280 };
8281 
8282 /* This struct is used to pass information to elf_link_output_extsym.  */
8283 
8284 struct elf_outext_info
8285 {
8286   bfd_boolean failed;
8287   bfd_boolean localsyms;
8288   bfd_boolean file_sym_done;
8289   struct elf_final_link_info *flinfo;
8290 };
8291 
8292 
8293 /* Support for evaluating a complex relocation.
8294 
8295    Complex relocations are generalized, self-describing relocations.  The
8296    implementation of them consists of two parts: complex symbols, and the
8297    relocations themselves.
8298 
8299    The relocations are use a reserved elf-wide relocation type code (R_RELC
8300    external / BFD_RELOC_RELC internal) and an encoding of relocation field
8301    information (start bit, end bit, word width, etc) into the addend.  This
8302    information is extracted from CGEN-generated operand tables within gas.
8303 
8304    Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8305    internal) representing prefix-notation expressions, including but not
8306    limited to those sorts of expressions normally encoded as addends in the
8307    addend field.  The symbol mangling format is:
8308 
8309    <node> := <literal>
8310 	  |  <unary-operator> ':' <node>
8311 	  |  <binary-operator> ':' <node> ':' <node>
8312 	  ;
8313 
8314    <literal> := 's' <digits=N> ':' <N character symbol name>
8315 	     |  'S' <digits=N> ':' <N character section name>
8316 	     |  '#' <hexdigits>
8317 	     ;
8318 
8319    <binary-operator> := as in C
8320    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
8321 
8322 static void
8323 set_symbol_value (bfd *bfd_with_globals,
8324 		  Elf_Internal_Sym *isymbuf,
8325 		  size_t locsymcount,
8326 		  size_t symidx,
8327 		  bfd_vma val)
8328 {
8329   struct elf_link_hash_entry **sym_hashes;
8330   struct elf_link_hash_entry *h;
8331   size_t extsymoff = locsymcount;
8332 
8333   if (symidx < locsymcount)
8334     {
8335       Elf_Internal_Sym *sym;
8336 
8337       sym = isymbuf + symidx;
8338       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8339 	{
8340 	  /* It is a local symbol: move it to the
8341 	     "absolute" section and give it a value.  */
8342 	  sym->st_shndx = SHN_ABS;
8343 	  sym->st_value = val;
8344 	  return;
8345 	}
8346       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8347       extsymoff = 0;
8348     }
8349 
8350   /* It is a global symbol: set its link type
8351      to "defined" and give it a value.  */
8352 
8353   sym_hashes = elf_sym_hashes (bfd_with_globals);
8354   h = sym_hashes [symidx - extsymoff];
8355   while (h->root.type == bfd_link_hash_indirect
8356 	 || h->root.type == bfd_link_hash_warning)
8357     h = (struct elf_link_hash_entry *) h->root.u.i.link;
8358   h->root.type = bfd_link_hash_defined;
8359   h->root.u.def.value = val;
8360   h->root.u.def.section = bfd_abs_section_ptr;
8361 }
8362 
8363 static bfd_boolean
8364 resolve_symbol (const char *name,
8365 		bfd *input_bfd,
8366 		struct elf_final_link_info *flinfo,
8367 		bfd_vma *result,
8368 		Elf_Internal_Sym *isymbuf,
8369 		size_t locsymcount)
8370 {
8371   Elf_Internal_Sym *sym;
8372   struct bfd_link_hash_entry *global_entry;
8373   const char *candidate = NULL;
8374   Elf_Internal_Shdr *symtab_hdr;
8375   size_t i;
8376 
8377   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8378 
8379   for (i = 0; i < locsymcount; ++ i)
8380     {
8381       sym = isymbuf + i;
8382 
8383       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8384 	continue;
8385 
8386       candidate = bfd_elf_string_from_elf_section (input_bfd,
8387 						   symtab_hdr->sh_link,
8388 						   sym->st_name);
8389 #ifdef DEBUG
8390       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8391 	      name, candidate, (unsigned long) sym->st_value);
8392 #endif
8393       if (candidate && strcmp (candidate, name) == 0)
8394 	{
8395 	  asection *sec = flinfo->sections [i];
8396 
8397 	  *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8398 	  *result += sec->output_offset + sec->output_section->vma;
8399 #ifdef DEBUG
8400 	  printf ("Found symbol with value %8.8lx\n",
8401 		  (unsigned long) *result);
8402 #endif
8403 	  return TRUE;
8404 	}
8405     }
8406 
8407   /* Hmm, haven't found it yet. perhaps it is a global.  */
8408   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8409 				       FALSE, FALSE, TRUE);
8410   if (!global_entry)
8411     return FALSE;
8412 
8413   if (global_entry->type == bfd_link_hash_defined
8414       || global_entry->type == bfd_link_hash_defweak)
8415     {
8416       *result = (global_entry->u.def.value
8417 		 + global_entry->u.def.section->output_section->vma
8418 		 + global_entry->u.def.section->output_offset);
8419 #ifdef DEBUG
8420       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8421 	      global_entry->root.string, (unsigned long) *result);
8422 #endif
8423       return TRUE;
8424     }
8425 
8426   return FALSE;
8427 }
8428 
8429 /* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
8430    bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
8431    names like "foo.end" which is the end address of section "foo".  */
8432 
8433 static bfd_boolean
8434 resolve_section (const char *name,
8435 		 asection *sections,
8436 		 bfd_vma *result,
8437 		 bfd * abfd)
8438 {
8439   asection *curr;
8440   unsigned int len;
8441 
8442   for (curr = sections; curr; curr = curr->next)
8443     if (strcmp (curr->name, name) == 0)
8444       {
8445 	*result = curr->vma;
8446 	return TRUE;
8447       }
8448 
8449   /* Hmm. still haven't found it. try pseudo-section names.  */
8450   /* FIXME: This could be coded more efficiently...  */
8451   for (curr = sections; curr; curr = curr->next)
8452     {
8453       len = strlen (curr->name);
8454       if (len > strlen (name))
8455 	continue;
8456 
8457       if (strncmp (curr->name, name, len) == 0)
8458 	{
8459 	  if (strncmp (".end", name + len, 4) == 0)
8460 	    {
8461 	      *result = (curr->vma
8462 			 + curr->size / bfd_octets_per_byte (abfd, curr));
8463 	      return TRUE;
8464 	    }
8465 
8466 	  /* Insert more pseudo-section names here, if you like.  */
8467 	}
8468     }
8469 
8470   return FALSE;
8471 }
8472 
8473 static void
8474 undefined_reference (const char *reftype, const char *name)
8475 {
8476   /* xgettext:c-format */
8477   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8478 		      reftype, name);
8479 }
8480 
8481 static bfd_boolean
8482 eval_symbol (bfd_vma *result,
8483 	     const char **symp,
8484 	     bfd *input_bfd,
8485 	     struct elf_final_link_info *flinfo,
8486 	     bfd_vma dot,
8487 	     Elf_Internal_Sym *isymbuf,
8488 	     size_t locsymcount,
8489 	     int signed_p)
8490 {
8491   size_t len;
8492   size_t symlen;
8493   bfd_vma a;
8494   bfd_vma b;
8495   char symbuf[4096];
8496   const char *sym = *symp;
8497   const char *symend;
8498   bfd_boolean symbol_is_section = FALSE;
8499 
8500   len = strlen (sym);
8501   symend = sym + len;
8502 
8503   if (len < 1 || len > sizeof (symbuf))
8504     {
8505       bfd_set_error (bfd_error_invalid_operation);
8506       return FALSE;
8507     }
8508 
8509   switch (* sym)
8510     {
8511     case '.':
8512       *result = dot;
8513       *symp = sym + 1;
8514       return TRUE;
8515 
8516     case '#':
8517       ++sym;
8518       *result = strtoul (sym, (char **) symp, 16);
8519       return TRUE;
8520 
8521     case 'S':
8522       symbol_is_section = TRUE;
8523       /* Fall through.  */
8524     case 's':
8525       ++sym;
8526       symlen = strtol (sym, (char **) symp, 10);
8527       sym = *symp + 1; /* Skip the trailing ':'.  */
8528 
8529       if (symend < sym || symlen + 1 > sizeof (symbuf))
8530 	{
8531 	  bfd_set_error (bfd_error_invalid_operation);
8532 	  return FALSE;
8533 	}
8534 
8535       memcpy (symbuf, sym, symlen);
8536       symbuf[symlen] = '\0';
8537       *symp = sym + symlen;
8538 
8539       /* Is it always possible, with complex symbols, that gas "mis-guessed"
8540 	 the symbol as a section, or vice-versa. so we're pretty liberal in our
8541 	 interpretation here; section means "try section first", not "must be a
8542 	 section", and likewise with symbol.  */
8543 
8544       if (symbol_is_section)
8545 	{
8546 	  if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8547 	      && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8548 				  isymbuf, locsymcount))
8549 	    {
8550 	      undefined_reference ("section", symbuf);
8551 	      return FALSE;
8552 	    }
8553 	}
8554       else
8555 	{
8556 	  if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8557 			       isymbuf, locsymcount)
8558 	      && !resolve_section (symbuf, flinfo->output_bfd->sections,
8559 				   result, input_bfd))
8560 	    {
8561 	      undefined_reference ("symbol", symbuf);
8562 	      return FALSE;
8563 	    }
8564 	}
8565 
8566       return TRUE;
8567 
8568       /* All that remains are operators.  */
8569 
8570 #define UNARY_OP(op)						\
8571   if (strncmp (sym, #op, strlen (#op)) == 0)			\
8572     {								\
8573       sym += strlen (#op);					\
8574       if (*sym == ':')						\
8575 	++sym;							\
8576       *symp = sym;						\
8577       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
8578 			isymbuf, locsymcount, signed_p))	\
8579 	return FALSE;						\
8580       if (signed_p)						\
8581 	*result = op ((bfd_signed_vma) a);			\
8582       else							\
8583 	*result = op a;						\
8584       return TRUE;						\
8585     }
8586 
8587 #define BINARY_OP(op)						\
8588   if (strncmp (sym, #op, strlen (#op)) == 0)			\
8589     {								\
8590       sym += strlen (#op);					\
8591       if (*sym == ':')						\
8592 	++sym;							\
8593       *symp = sym;						\
8594       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
8595 			isymbuf, locsymcount, signed_p))	\
8596 	return FALSE;						\
8597       ++*symp;							\
8598       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,	\
8599 			isymbuf, locsymcount, signed_p))	\
8600 	return FALSE;						\
8601       if (signed_p)						\
8602 	*result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b);	\
8603       else							\
8604 	*result = a op b;					\
8605       return TRUE;						\
8606     }
8607 
8608     default:
8609       UNARY_OP  (0-);
8610       BINARY_OP (<<);
8611       BINARY_OP (>>);
8612       BINARY_OP (==);
8613       BINARY_OP (!=);
8614       BINARY_OP (<=);
8615       BINARY_OP (>=);
8616       BINARY_OP (&&);
8617       BINARY_OP (||);
8618       UNARY_OP  (~);
8619       UNARY_OP  (!);
8620       BINARY_OP (*);
8621       BINARY_OP (/);
8622       BINARY_OP (%);
8623       BINARY_OP (^);
8624       BINARY_OP (|);
8625       BINARY_OP (&);
8626       BINARY_OP (+);
8627       BINARY_OP (-);
8628       BINARY_OP (<);
8629       BINARY_OP (>);
8630 #undef UNARY_OP
8631 #undef BINARY_OP
8632       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8633       bfd_set_error (bfd_error_invalid_operation);
8634       return FALSE;
8635     }
8636 }
8637 
8638 static void
8639 put_value (bfd_vma size,
8640 	   unsigned long chunksz,
8641 	   bfd *input_bfd,
8642 	   bfd_vma x,
8643 	   bfd_byte *location)
8644 {
8645   location += (size - chunksz);
8646 
8647   for (; size; size -= chunksz, location -= chunksz)
8648     {
8649       switch (chunksz)
8650 	{
8651 	case 1:
8652 	  bfd_put_8 (input_bfd, x, location);
8653 	  x >>= 8;
8654 	  break;
8655 	case 2:
8656 	  bfd_put_16 (input_bfd, x, location);
8657 	  x >>= 16;
8658 	  break;
8659 	case 4:
8660 	  bfd_put_32 (input_bfd, x, location);
8661 	  /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
8662 	  x >>= 16;
8663 	  x >>= 16;
8664 	  break;
8665 #ifdef BFD64
8666 	case 8:
8667 	  bfd_put_64 (input_bfd, x, location);
8668 	  /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
8669 	  x >>= 32;
8670 	  x >>= 32;
8671 	  break;
8672 #endif
8673 	default:
8674 	  abort ();
8675 	  break;
8676 	}
8677     }
8678 }
8679 
8680 static bfd_vma
8681 get_value (bfd_vma size,
8682 	   unsigned long chunksz,
8683 	   bfd *input_bfd,
8684 	   bfd_byte *location)
8685 {
8686   int shift;
8687   bfd_vma x = 0;
8688 
8689   /* Sanity checks.  */
8690   BFD_ASSERT (chunksz <= sizeof (x)
8691 	      && size >= chunksz
8692 	      && chunksz != 0
8693 	      && (size % chunksz) == 0
8694 	      && input_bfd != NULL
8695 	      && location != NULL);
8696 
8697   if (chunksz == sizeof (x))
8698     {
8699       BFD_ASSERT (size == chunksz);
8700 
8701       /* Make sure that we do not perform an undefined shift operation.
8702 	 We know that size == chunksz so there will only be one iteration
8703 	 of the loop below.  */
8704       shift = 0;
8705     }
8706   else
8707     shift = 8 * chunksz;
8708 
8709   for (; size; size -= chunksz, location += chunksz)
8710     {
8711       switch (chunksz)
8712 	{
8713 	case 1:
8714 	  x = (x << shift) | bfd_get_8 (input_bfd, location);
8715 	  break;
8716 	case 2:
8717 	  x = (x << shift) | bfd_get_16 (input_bfd, location);
8718 	  break;
8719 	case 4:
8720 	  x = (x << shift) | bfd_get_32 (input_bfd, location);
8721 	  break;
8722 #ifdef BFD64
8723 	case 8:
8724 	  x = (x << shift) | bfd_get_64 (input_bfd, location);
8725 	  break;
8726 #endif
8727 	default:
8728 	  abort ();
8729 	}
8730     }
8731   return x;
8732 }
8733 
8734 static void
8735 decode_complex_addend (unsigned long *start,   /* in bits */
8736 		       unsigned long *oplen,   /* in bits */
8737 		       unsigned long *len,     /* in bits */
8738 		       unsigned long *wordsz,  /* in bytes */
8739 		       unsigned long *chunksz, /* in bytes */
8740 		       unsigned long *lsb0_p,
8741 		       unsigned long *signed_p,
8742 		       unsigned long *trunc_p,
8743 		       unsigned long encoded)
8744 {
8745   * start     =	 encoded	& 0x3F;
8746   * len	      = (encoded >>  6) & 0x3F;
8747   * oplen     = (encoded >> 12) & 0x3F;
8748   * wordsz    = (encoded >> 18) & 0xF;
8749   * chunksz   = (encoded >> 22) & 0xF;
8750   * lsb0_p    = (encoded >> 27) & 1;
8751   * signed_p  = (encoded >> 28) & 1;
8752   * trunc_p   = (encoded >> 29) & 1;
8753 }
8754 
8755 bfd_reloc_status_type
8756 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8757 				    asection *input_section,
8758 				    bfd_byte *contents,
8759 				    Elf_Internal_Rela *rel,
8760 				    bfd_vma relocation)
8761 {
8762   bfd_vma shift, x, mask;
8763   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8764   bfd_reloc_status_type r;
8765   bfd_size_type octets;
8766 
8767   /*  Perform this reloc, since it is complex.
8768       (this is not to say that it necessarily refers to a complex
8769       symbol; merely that it is a self-describing CGEN based reloc.
8770       i.e. the addend has the complete reloc information (bit start, end,
8771       word size, etc) encoded within it.).  */
8772 
8773   decode_complex_addend (&start, &oplen, &len, &wordsz,
8774 			 &chunksz, &lsb0_p, &signed_p,
8775 			 &trunc_p, rel->r_addend);
8776 
8777   mask = (((1L << (len - 1)) - 1) << 1) | 1;
8778 
8779   if (lsb0_p)
8780     shift = (start + 1) - len;
8781   else
8782     shift = (8 * wordsz) - (start + len);
8783 
8784   octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8785   x = get_value (wordsz, chunksz, input_bfd, contents + octets);
8786 
8787 #ifdef DEBUG
8788   printf ("Doing complex reloc: "
8789 	  "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8790 	  "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8791 	  "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8792 	  lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8793 	  oplen, (unsigned long) x, (unsigned long) mask,
8794 	  (unsigned long) relocation);
8795 #endif
8796 
8797   r = bfd_reloc_ok;
8798   if (! trunc_p)
8799     /* Now do an overflow check.  */
8800     r = bfd_check_overflow ((signed_p
8801 			     ? complain_overflow_signed
8802 			     : complain_overflow_unsigned),
8803 			    len, 0, (8 * wordsz),
8804 			    relocation);
8805 
8806   /* Do the deed.  */
8807   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8808 
8809 #ifdef DEBUG
8810   printf ("           relocation: %8.8lx\n"
8811 	  "         shifted mask: %8.8lx\n"
8812 	  " shifted/masked reloc: %8.8lx\n"
8813 	  "               result: %8.8lx\n",
8814 	  (unsigned long) relocation, (unsigned long) (mask << shift),
8815 	  (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8816 #endif
8817   put_value (wordsz, chunksz, input_bfd, x, contents + octets);
8818   return r;
8819 }
8820 
8821 /* Functions to read r_offset from external (target order) reloc
8822    entry.  Faster than bfd_getl32 et al, because we let the compiler
8823    know the value is aligned.  */
8824 
8825 static bfd_vma
8826 ext32l_r_offset (const void *p)
8827 {
8828   union aligned32
8829   {
8830     uint32_t v;
8831     unsigned char c[4];
8832   };
8833   const union aligned32 *a
8834     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8835 
8836   uint32_t aval = (  (uint32_t) a->c[0]
8837 		   | (uint32_t) a->c[1] << 8
8838 		   | (uint32_t) a->c[2] << 16
8839 		   | (uint32_t) a->c[3] << 24);
8840   return aval;
8841 }
8842 
8843 static bfd_vma
8844 ext32b_r_offset (const void *p)
8845 {
8846   union aligned32
8847   {
8848     uint32_t v;
8849     unsigned char c[4];
8850   };
8851   const union aligned32 *a
8852     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8853 
8854   uint32_t aval = (  (uint32_t) a->c[0] << 24
8855 		   | (uint32_t) a->c[1] << 16
8856 		   | (uint32_t) a->c[2] << 8
8857 		   | (uint32_t) a->c[3]);
8858   return aval;
8859 }
8860 
8861 #ifdef BFD_HOST_64_BIT
8862 static bfd_vma
8863 ext64l_r_offset (const void *p)
8864 {
8865   union aligned64
8866   {
8867     uint64_t v;
8868     unsigned char c[8];
8869   };
8870   const union aligned64 *a
8871     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8872 
8873   uint64_t aval = (  (uint64_t) a->c[0]
8874 		   | (uint64_t) a->c[1] << 8
8875 		   | (uint64_t) a->c[2] << 16
8876 		   | (uint64_t) a->c[3] << 24
8877 		   | (uint64_t) a->c[4] << 32
8878 		   | (uint64_t) a->c[5] << 40
8879 		   | (uint64_t) a->c[6] << 48
8880 		   | (uint64_t) a->c[7] << 56);
8881   return aval;
8882 }
8883 
8884 static bfd_vma
8885 ext64b_r_offset (const void *p)
8886 {
8887   union aligned64
8888   {
8889     uint64_t v;
8890     unsigned char c[8];
8891   };
8892   const union aligned64 *a
8893     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8894 
8895   uint64_t aval = (  (uint64_t) a->c[0] << 56
8896 		   | (uint64_t) a->c[1] << 48
8897 		   | (uint64_t) a->c[2] << 40
8898 		   | (uint64_t) a->c[3] << 32
8899 		   | (uint64_t) a->c[4] << 24
8900 		   | (uint64_t) a->c[5] << 16
8901 		   | (uint64_t) a->c[6] << 8
8902 		   | (uint64_t) a->c[7]);
8903   return aval;
8904 }
8905 #endif
8906 
8907 /* When performing a relocatable link, the input relocations are
8908    preserved.  But, if they reference global symbols, the indices
8909    referenced must be updated.  Update all the relocations found in
8910    RELDATA.  */
8911 
8912 static bfd_boolean
8913 elf_link_adjust_relocs (bfd *abfd,
8914 			asection *sec,
8915 			struct bfd_elf_section_reloc_data *reldata,
8916 			bfd_boolean sort,
8917 			struct bfd_link_info *info)
8918 {
8919   unsigned int i;
8920   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8921   bfd_byte *erela;
8922   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8923   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8924   bfd_vma r_type_mask;
8925   int r_sym_shift;
8926   unsigned int count = reldata->count;
8927   struct elf_link_hash_entry **rel_hash = reldata->hashes;
8928 
8929   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8930     {
8931       swap_in = bed->s->swap_reloc_in;
8932       swap_out = bed->s->swap_reloc_out;
8933     }
8934   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8935     {
8936       swap_in = bed->s->swap_reloca_in;
8937       swap_out = bed->s->swap_reloca_out;
8938     }
8939   else
8940     abort ();
8941 
8942   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8943     abort ();
8944 
8945   if (bed->s->arch_size == 32)
8946     {
8947       r_type_mask = 0xff;
8948       r_sym_shift = 8;
8949     }
8950   else
8951     {
8952       r_type_mask = 0xffffffff;
8953       r_sym_shift = 32;
8954     }
8955 
8956   erela = reldata->hdr->contents;
8957   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8958     {
8959       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8960       unsigned int j;
8961 
8962       if (*rel_hash == NULL)
8963 	continue;
8964 
8965       if ((*rel_hash)->indx == -2
8966 	  && info->gc_sections
8967 	  && ! info->gc_keep_exported)
8968 	{
8969 	  /* PR 21524: Let the user know if a symbol was removed by garbage collection.  */
8970 	  _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8971 			      abfd, sec,
8972 			      (*rel_hash)->root.root.string);
8973 	  _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8974 			      abfd, sec);
8975 	  bfd_set_error (bfd_error_invalid_operation);
8976 	  return FALSE;
8977 	}
8978       BFD_ASSERT ((*rel_hash)->indx >= 0);
8979 
8980       (*swap_in) (abfd, erela, irela);
8981       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8982 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8983 			   | (irela[j].r_info & r_type_mask));
8984       (*swap_out) (abfd, irela, erela);
8985     }
8986 
8987   if (bed->elf_backend_update_relocs)
8988     (*bed->elf_backend_update_relocs) (sec, reldata);
8989 
8990   if (sort && count != 0)
8991     {
8992       bfd_vma (*ext_r_off) (const void *);
8993       bfd_vma r_off;
8994       size_t elt_size;
8995       bfd_byte *base, *end, *p, *loc;
8996       bfd_byte *buf = NULL;
8997 
8998       if (bed->s->arch_size == 32)
8999 	{
9000 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9001 	    ext_r_off = ext32l_r_offset;
9002 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9003 	    ext_r_off = ext32b_r_offset;
9004 	  else
9005 	    abort ();
9006 	}
9007       else
9008 	{
9009 #ifdef BFD_HOST_64_BIT
9010 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9011 	    ext_r_off = ext64l_r_offset;
9012 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9013 	    ext_r_off = ext64b_r_offset;
9014 	  else
9015 #endif
9016 	    abort ();
9017 	}
9018 
9019       /*  Must use a stable sort here.  A modified insertion sort,
9020 	  since the relocs are mostly sorted already.  */
9021       elt_size = reldata->hdr->sh_entsize;
9022       base = reldata->hdr->contents;
9023       end = base + count * elt_size;
9024       if (elt_size > sizeof (Elf64_External_Rela))
9025 	abort ();
9026 
9027       /* Ensure the first element is lowest.  This acts as a sentinel,
9028 	 speeding the main loop below.  */
9029       r_off = (*ext_r_off) (base);
9030       for (p = loc = base; (p += elt_size) < end; )
9031 	{
9032 	  bfd_vma r_off2 = (*ext_r_off) (p);
9033 	  if (r_off > r_off2)
9034 	    {
9035 	      r_off = r_off2;
9036 	      loc = p;
9037 	    }
9038 	}
9039       if (loc != base)
9040 	{
9041 	  /* Don't just swap *base and *loc as that changes the order
9042 	     of the original base[0] and base[1] if they happen to
9043 	     have the same r_offset.  */
9044 	  bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9045 	  memcpy (onebuf, loc, elt_size);
9046 	  memmove (base + elt_size, base, loc - base);
9047 	  memcpy (base, onebuf, elt_size);
9048 	}
9049 
9050       for (p = base + elt_size; (p += elt_size) < end; )
9051 	{
9052 	  /* base to p is sorted, *p is next to insert.  */
9053 	  r_off = (*ext_r_off) (p);
9054 	  /* Search the sorted region for location to insert.  */
9055 	  loc = p - elt_size;
9056 	  while (r_off < (*ext_r_off) (loc))
9057 	    loc -= elt_size;
9058 	  loc += elt_size;
9059 	  if (loc != p)
9060 	    {
9061 	      /* Chances are there is a run of relocs to insert here,
9062 		 from one of more input files.  Files are not always
9063 		 linked in order due to the way elf_link_input_bfd is
9064 		 called.  See pr17666.  */
9065 	      size_t sortlen = p - loc;
9066 	      bfd_vma r_off2 = (*ext_r_off) (loc);
9067 	      size_t runlen = elt_size;
9068 	      size_t buf_size = 96 * 1024;
9069 	      while (p + runlen < end
9070 		     && (sortlen <= buf_size
9071 			 || runlen + elt_size <= buf_size)
9072 		     && r_off2 > (*ext_r_off) (p + runlen))
9073 		runlen += elt_size;
9074 	      if (buf == NULL)
9075 		{
9076 		  buf = bfd_malloc (buf_size);
9077 		  if (buf == NULL)
9078 		    return FALSE;
9079 		}
9080 	      if (runlen < sortlen)
9081 		{
9082 		  memcpy (buf, p, runlen);
9083 		  memmove (loc + runlen, loc, sortlen);
9084 		  memcpy (loc, buf, runlen);
9085 		}
9086 	      else
9087 		{
9088 		  memcpy (buf, loc, sortlen);
9089 		  memmove (loc, p, runlen);
9090 		  memcpy (loc + runlen, buf, sortlen);
9091 		}
9092 	      p += runlen - elt_size;
9093 	    }
9094 	}
9095       /* Hashes are no longer valid.  */
9096       free (reldata->hashes);
9097       reldata->hashes = NULL;
9098       free (buf);
9099     }
9100   return TRUE;
9101 }
9102 
9103 struct elf_link_sort_rela
9104 {
9105   union {
9106     bfd_vma offset;
9107     bfd_vma sym_mask;
9108   } u;
9109   enum elf_reloc_type_class type;
9110   /* We use this as an array of size int_rels_per_ext_rel.  */
9111   Elf_Internal_Rela rela[1];
9112 };
9113 
9114 /* qsort stability here and for cmp2 is only an issue if multiple
9115    dynamic relocations are emitted at the same address.  But targets
9116    that apply a series of dynamic relocations each operating on the
9117    result of the prior relocation can't use -z combreloc as
9118    implemented anyway.  Such schemes tend to be broken by sorting on
9119    symbol index.  That leaves dynamic NONE relocs as the only other
9120    case where ld might emit multiple relocs at the same address, and
9121    those are only emitted due to target bugs.  */
9122 
9123 static int
9124 elf_link_sort_cmp1 (const void *A, const void *B)
9125 {
9126   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9127   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9128   int relativea, relativeb;
9129 
9130   relativea = a->type == reloc_class_relative;
9131   relativeb = b->type == reloc_class_relative;
9132 
9133   if (relativea < relativeb)
9134     return 1;
9135   if (relativea > relativeb)
9136     return -1;
9137   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9138     return -1;
9139   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9140     return 1;
9141   if (a->rela->r_offset < b->rela->r_offset)
9142     return -1;
9143   if (a->rela->r_offset > b->rela->r_offset)
9144     return 1;
9145   return 0;
9146 }
9147 
9148 static int
9149 elf_link_sort_cmp2 (const void *A, const void *B)
9150 {
9151   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9152   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9153 
9154   if (a->type < b->type)
9155     return -1;
9156   if (a->type > b->type)
9157     return 1;
9158   if (a->u.offset < b->u.offset)
9159     return -1;
9160   if (a->u.offset > b->u.offset)
9161     return 1;
9162   if (a->rela->r_offset < b->rela->r_offset)
9163     return -1;
9164   if (a->rela->r_offset > b->rela->r_offset)
9165     return 1;
9166   return 0;
9167 }
9168 
9169 static size_t
9170 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9171 {
9172   asection *dynamic_relocs;
9173   asection *rela_dyn;
9174   asection *rel_dyn;
9175   bfd_size_type count, size;
9176   size_t i, ret, sort_elt, ext_size;
9177   bfd_byte *sort, *s_non_relative, *p;
9178   struct elf_link_sort_rela *sq;
9179   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9180   int i2e = bed->s->int_rels_per_ext_rel;
9181   unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9182   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9183   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9184   struct bfd_link_order *lo;
9185   bfd_vma r_sym_mask;
9186   bfd_boolean use_rela;
9187 
9188   /* Find a dynamic reloc section.  */
9189   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9190   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
9191   if (rela_dyn != NULL && rela_dyn->size > 0
9192       && rel_dyn != NULL && rel_dyn->size > 0)
9193     {
9194       bfd_boolean use_rela_initialised = FALSE;
9195 
9196       /* This is just here to stop gcc from complaining.
9197 	 Its initialization checking code is not perfect.  */
9198       use_rela = TRUE;
9199 
9200       /* Both sections are present.  Examine the sizes
9201 	 of the indirect sections to help us choose.  */
9202       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9203 	if (lo->type == bfd_indirect_link_order)
9204 	  {
9205 	    asection *o = lo->u.indirect.section;
9206 
9207 	    if ((o->size % bed->s->sizeof_rela) == 0)
9208 	      {
9209 		if ((o->size % bed->s->sizeof_rel) == 0)
9210 		  /* Section size is divisible by both rel and rela sizes.
9211 		     It is of no help to us.  */
9212 		  ;
9213 		else
9214 		  {
9215 		    /* Section size is only divisible by rela.  */
9216 		    if (use_rela_initialised && !use_rela)
9217 		      {
9218 			_bfd_error_handler (_("%pB: unable to sort relocs - "
9219 					      "they are in more than one size"),
9220 					    abfd);
9221 			bfd_set_error (bfd_error_invalid_operation);
9222 			return 0;
9223 		      }
9224 		    else
9225 		      {
9226 			use_rela = TRUE;
9227 			use_rela_initialised = TRUE;
9228 		      }
9229 		  }
9230 	      }
9231 	    else if ((o->size % bed->s->sizeof_rel) == 0)
9232 	      {
9233 		/* Section size is only divisible by rel.  */
9234 		if (use_rela_initialised && use_rela)
9235 		  {
9236 		    _bfd_error_handler (_("%pB: unable to sort relocs - "
9237 					  "they are in more than one size"),
9238 					abfd);
9239 		    bfd_set_error (bfd_error_invalid_operation);
9240 		    return 0;
9241 		  }
9242 		else
9243 		  {
9244 		    use_rela = FALSE;
9245 		    use_rela_initialised = TRUE;
9246 		  }
9247 	      }
9248 	    else
9249 	      {
9250 		/* The section size is not divisible by either -
9251 		   something is wrong.  */
9252 		_bfd_error_handler (_("%pB: unable to sort relocs - "
9253 				      "they are of an unknown size"), abfd);
9254 		bfd_set_error (bfd_error_invalid_operation);
9255 		return 0;
9256 	      }
9257 	  }
9258 
9259       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9260 	if (lo->type == bfd_indirect_link_order)
9261 	  {
9262 	    asection *o = lo->u.indirect.section;
9263 
9264 	    if ((o->size % bed->s->sizeof_rela) == 0)
9265 	      {
9266 		if ((o->size % bed->s->sizeof_rel) == 0)
9267 		  /* Section size is divisible by both rel and rela sizes.
9268 		     It is of no help to us.  */
9269 		  ;
9270 		else
9271 		  {
9272 		    /* Section size is only divisible by rela.  */
9273 		    if (use_rela_initialised && !use_rela)
9274 		      {
9275 			_bfd_error_handler (_("%pB: unable to sort relocs - "
9276 					      "they are in more than one size"),
9277 					    abfd);
9278 			bfd_set_error (bfd_error_invalid_operation);
9279 			return 0;
9280 		      }
9281 		    else
9282 		      {
9283 			use_rela = TRUE;
9284 			use_rela_initialised = TRUE;
9285 		      }
9286 		  }
9287 	      }
9288 	    else if ((o->size % bed->s->sizeof_rel) == 0)
9289 	      {
9290 		/* Section size is only divisible by rel.  */
9291 		if (use_rela_initialised && use_rela)
9292 		  {
9293 		    _bfd_error_handler (_("%pB: unable to sort relocs - "
9294 					  "they are in more than one size"),
9295 					abfd);
9296 		    bfd_set_error (bfd_error_invalid_operation);
9297 		    return 0;
9298 		  }
9299 		else
9300 		  {
9301 		    use_rela = FALSE;
9302 		    use_rela_initialised = TRUE;
9303 		  }
9304 	      }
9305 	    else
9306 	      {
9307 		/* The section size is not divisible by either -
9308 		   something is wrong.  */
9309 		_bfd_error_handler (_("%pB: unable to sort relocs - "
9310 				      "they are of an unknown size"), abfd);
9311 		bfd_set_error (bfd_error_invalid_operation);
9312 		return 0;
9313 	      }
9314 	  }
9315 
9316       if (! use_rela_initialised)
9317 	/* Make a guess.  */
9318 	use_rela = TRUE;
9319     }
9320   else if (rela_dyn != NULL && rela_dyn->size > 0)
9321     use_rela = TRUE;
9322   else if (rel_dyn != NULL && rel_dyn->size > 0)
9323     use_rela = FALSE;
9324   else
9325     return 0;
9326 
9327   if (use_rela)
9328     {
9329       dynamic_relocs = rela_dyn;
9330       ext_size = bed->s->sizeof_rela;
9331       swap_in = bed->s->swap_reloca_in;
9332       swap_out = bed->s->swap_reloca_out;
9333     }
9334   else
9335     {
9336       dynamic_relocs = rel_dyn;
9337       ext_size = bed->s->sizeof_rel;
9338       swap_in = bed->s->swap_reloc_in;
9339       swap_out = bed->s->swap_reloc_out;
9340     }
9341 
9342   size = 0;
9343   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9344     if (lo->type == bfd_indirect_link_order)
9345       size += lo->u.indirect.section->size;
9346 
9347   if (size != dynamic_relocs->size)
9348     return 0;
9349 
9350   sort_elt = (sizeof (struct elf_link_sort_rela)
9351 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
9352 
9353   count = dynamic_relocs->size / ext_size;
9354   if (count == 0)
9355     return 0;
9356   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9357 
9358   if (sort == NULL)
9359     {
9360       (*info->callbacks->warning)
9361 	(info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9362       return 0;
9363     }
9364 
9365   if (bed->s->arch_size == 32)
9366     r_sym_mask = ~(bfd_vma) 0xff;
9367   else
9368     r_sym_mask = ~(bfd_vma) 0xffffffff;
9369 
9370   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9371     if (lo->type == bfd_indirect_link_order)
9372       {
9373 	bfd_byte *erel, *erelend;
9374 	asection *o = lo->u.indirect.section;
9375 
9376 	if (o->contents == NULL && o->size != 0)
9377 	  {
9378 	    /* This is a reloc section that is being handled as a normal
9379 	       section.  See bfd_section_from_shdr.  We can't combine
9380 	       relocs in this case.  */
9381 	    free (sort);
9382 	    return 0;
9383 	  }
9384 	erel = o->contents;
9385 	erelend = o->contents + o->size;
9386 	p = sort + o->output_offset * opb / ext_size * sort_elt;
9387 
9388 	while (erel < erelend)
9389 	  {
9390 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9391 
9392 	    (*swap_in) (abfd, erel, s->rela);
9393 	    s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9394 	    s->u.sym_mask = r_sym_mask;
9395 	    p += sort_elt;
9396 	    erel += ext_size;
9397 	  }
9398       }
9399 
9400   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9401 
9402   for (i = 0, p = sort; i < count; i++, p += sort_elt)
9403     {
9404       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9405       if (s->type != reloc_class_relative)
9406 	break;
9407     }
9408   ret = i;
9409   s_non_relative = p;
9410 
9411   sq = (struct elf_link_sort_rela *) s_non_relative;
9412   for (; i < count; i++, p += sort_elt)
9413     {
9414       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9415       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9416 	sq = sp;
9417       sp->u.offset = sq->rela->r_offset;
9418     }
9419 
9420   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9421 
9422   struct elf_link_hash_table *htab = elf_hash_table (info);
9423   if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9424     {
9425       /* We have plt relocs in .rela.dyn.  */
9426       sq = (struct elf_link_sort_rela *) sort;
9427       for (i = 0; i < count; i++)
9428 	if (sq[count - i - 1].type != reloc_class_plt)
9429 	  break;
9430       if (i != 0 && htab->srelplt->size == i * ext_size)
9431 	{
9432 	  struct bfd_link_order **plo;
9433 	  /* Put srelplt link_order last.  This is so the output_offset
9434 	     set in the next loop is correct for DT_JMPREL.  */
9435 	  for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9436 	    if ((*plo)->type == bfd_indirect_link_order
9437 		&& (*plo)->u.indirect.section == htab->srelplt)
9438 	      {
9439 		lo = *plo;
9440 		*plo = lo->next;
9441 	      }
9442 	    else
9443 	      plo = &(*plo)->next;
9444 	  *plo = lo;
9445 	  lo->next = NULL;
9446 	  dynamic_relocs->map_tail.link_order = lo;
9447 	}
9448     }
9449 
9450   p = sort;
9451   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9452     if (lo->type == bfd_indirect_link_order)
9453       {
9454 	bfd_byte *erel, *erelend;
9455 	asection *o = lo->u.indirect.section;
9456 
9457 	erel = o->contents;
9458 	erelend = o->contents + o->size;
9459 	o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9460 	while (erel < erelend)
9461 	  {
9462 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9463 	    (*swap_out) (abfd, s->rela, erel);
9464 	    p += sort_elt;
9465 	    erel += ext_size;
9466 	  }
9467       }
9468 
9469   free (sort);
9470   *psec = dynamic_relocs;
9471   return ret;
9472 }
9473 
9474 /* Add a symbol to the output symbol string table.  */
9475 
9476 static int
9477 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9478 			   const char *name,
9479 			   Elf_Internal_Sym *elfsym,
9480 			   asection *input_sec,
9481 			   struct elf_link_hash_entry *h)
9482 {
9483   int (*output_symbol_hook)
9484     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9485      struct elf_link_hash_entry *);
9486   struct elf_link_hash_table *hash_table;
9487   const struct elf_backend_data *bed;
9488   bfd_size_type strtabsize;
9489 
9490   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9491 
9492   bed = get_elf_backend_data (flinfo->output_bfd);
9493   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9494   if (output_symbol_hook != NULL)
9495     {
9496       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9497       if (ret != 1)
9498 	return ret;
9499     }
9500 
9501   if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9502     elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9503   if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9504     elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9505 
9506   if (name == NULL
9507       || *name == '\0'
9508       || (input_sec->flags & SEC_EXCLUDE))
9509     elfsym->st_name = (unsigned long) -1;
9510   else
9511     {
9512       /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9513 	 to get the final offset for st_name.  */
9514       elfsym->st_name
9515 	= (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9516 					       name, FALSE);
9517       if (elfsym->st_name == (unsigned long) -1)
9518 	return 0;
9519     }
9520 
9521   hash_table = elf_hash_table (flinfo->info);
9522   strtabsize = hash_table->strtabsize;
9523   if (strtabsize <= hash_table->strtabcount)
9524     {
9525       strtabsize += strtabsize;
9526       hash_table->strtabsize = strtabsize;
9527       strtabsize *= sizeof (*hash_table->strtab);
9528       hash_table->strtab
9529 	= (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9530 						 strtabsize);
9531       if (hash_table->strtab == NULL)
9532 	return 0;
9533     }
9534   hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9535   hash_table->strtab[hash_table->strtabcount].dest_index
9536     = hash_table->strtabcount;
9537   hash_table->strtab[hash_table->strtabcount].destshndx_index
9538     = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9539 
9540   flinfo->output_bfd->symcount += 1;
9541   hash_table->strtabcount += 1;
9542 
9543   return 1;
9544 }
9545 
9546 /* Swap symbols out to the symbol table and flush the output symbols to
9547    the file.  */
9548 
9549 static bfd_boolean
9550 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9551 {
9552   struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9553   bfd_size_type amt;
9554   size_t i;
9555   const struct elf_backend_data *bed;
9556   bfd_byte *symbuf;
9557   Elf_Internal_Shdr *hdr;
9558   file_ptr pos;
9559   bfd_boolean ret;
9560 
9561   if (!hash_table->strtabcount)
9562     return TRUE;
9563 
9564   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9565 
9566   bed = get_elf_backend_data (flinfo->output_bfd);
9567 
9568   amt = bed->s->sizeof_sym * hash_table->strtabcount;
9569   symbuf = (bfd_byte *) bfd_malloc (amt);
9570   if (symbuf == NULL)
9571     return FALSE;
9572 
9573   if (flinfo->symshndxbuf)
9574     {
9575       amt = sizeof (Elf_External_Sym_Shndx);
9576       amt *= bfd_get_symcount (flinfo->output_bfd);
9577       flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9578       if (flinfo->symshndxbuf == NULL)
9579 	{
9580 	  free (symbuf);
9581 	  return FALSE;
9582 	}
9583     }
9584 
9585   for (i = 0; i < hash_table->strtabcount; i++)
9586     {
9587       struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9588       if (elfsym->sym.st_name == (unsigned long) -1)
9589 	elfsym->sym.st_name = 0;
9590       else
9591 	elfsym->sym.st_name
9592 	  = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9593 						    elfsym->sym.st_name);
9594       bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9595 			       ((bfd_byte *) symbuf
9596 				+ (elfsym->dest_index
9597 				   * bed->s->sizeof_sym)),
9598 			       (flinfo->symshndxbuf
9599 				+ elfsym->destshndx_index));
9600     }
9601 
9602   /* Allow the linker to examine the strtab and symtab now they are
9603      populated.  */
9604 
9605   if (flinfo->info->callbacks->examine_strtab)
9606     flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9607 					     hash_table->strtabcount,
9608 					     flinfo->symstrtab);
9609 
9610   hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9611   pos = hdr->sh_offset + hdr->sh_size;
9612   amt = hash_table->strtabcount * bed->s->sizeof_sym;
9613   if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9614       && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9615     {
9616       hdr->sh_size += amt;
9617       ret = TRUE;
9618     }
9619   else
9620     ret = FALSE;
9621 
9622   free (symbuf);
9623 
9624   free (hash_table->strtab);
9625   hash_table->strtab = NULL;
9626 
9627   return ret;
9628 }
9629 
9630 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
9631 
9632 static bfd_boolean
9633 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9634 {
9635   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9636       && sym->st_shndx < SHN_LORESERVE)
9637     {
9638       /* The gABI doesn't support dynamic symbols in output sections
9639 	 beyond 64k.  */
9640       _bfd_error_handler
9641 	/* xgettext:c-format */
9642 	(_("%pB: too many sections: %d (>= %d)"),
9643 	 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9644       bfd_set_error (bfd_error_nonrepresentable_section);
9645       return FALSE;
9646     }
9647   return TRUE;
9648 }
9649 
9650 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9651    allowing an unsatisfied unversioned symbol in the DSO to match a
9652    versioned symbol that would normally require an explicit version.
9653    We also handle the case that a DSO references a hidden symbol
9654    which may be satisfied by a versioned symbol in another DSO.  */
9655 
9656 static bfd_boolean
9657 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9658 				 const struct elf_backend_data *bed,
9659 				 struct elf_link_hash_entry *h)
9660 {
9661   bfd *abfd;
9662   struct elf_link_loaded_list *loaded;
9663 
9664   if (!is_elf_hash_table (info->hash))
9665     return FALSE;
9666 
9667   /* Check indirect symbol.  */
9668   while (h->root.type == bfd_link_hash_indirect)
9669     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9670 
9671   switch (h->root.type)
9672     {
9673     default:
9674       abfd = NULL;
9675       break;
9676 
9677     case bfd_link_hash_undefined:
9678     case bfd_link_hash_undefweak:
9679       abfd = h->root.u.undef.abfd;
9680       if (abfd == NULL
9681 	  || (abfd->flags & DYNAMIC) == 0
9682 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9683 	return FALSE;
9684       break;
9685 
9686     case bfd_link_hash_defined:
9687     case bfd_link_hash_defweak:
9688       abfd = h->root.u.def.section->owner;
9689       break;
9690 
9691     case bfd_link_hash_common:
9692       abfd = h->root.u.c.p->section->owner;
9693       break;
9694     }
9695   BFD_ASSERT (abfd != NULL);
9696 
9697   for (loaded = elf_hash_table (info)->loaded;
9698        loaded != NULL;
9699        loaded = loaded->next)
9700     {
9701       bfd *input;
9702       Elf_Internal_Shdr *hdr;
9703       size_t symcount;
9704       size_t extsymcount;
9705       size_t extsymoff;
9706       Elf_Internal_Shdr *versymhdr;
9707       Elf_Internal_Sym *isym;
9708       Elf_Internal_Sym *isymend;
9709       Elf_Internal_Sym *isymbuf;
9710       Elf_External_Versym *ever;
9711       Elf_External_Versym *extversym;
9712 
9713       input = loaded->abfd;
9714 
9715       /* We check each DSO for a possible hidden versioned definition.  */
9716       if (input == abfd
9717 	  || (input->flags & DYNAMIC) == 0
9718 	  || elf_dynversym (input) == 0)
9719 	continue;
9720 
9721       hdr = &elf_tdata (input)->dynsymtab_hdr;
9722 
9723       symcount = hdr->sh_size / bed->s->sizeof_sym;
9724       if (elf_bad_symtab (input))
9725 	{
9726 	  extsymcount = symcount;
9727 	  extsymoff = 0;
9728 	}
9729       else
9730 	{
9731 	  extsymcount = symcount - hdr->sh_info;
9732 	  extsymoff = hdr->sh_info;
9733 	}
9734 
9735       if (extsymcount == 0)
9736 	continue;
9737 
9738       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9739 				      NULL, NULL, NULL);
9740       if (isymbuf == NULL)
9741 	return FALSE;
9742 
9743       /* Read in any version definitions.  */
9744       versymhdr = &elf_tdata (input)->dynversym_hdr;
9745       extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9746       if (extversym == NULL)
9747 	goto error_ret;
9748 
9749       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9750 	  || (bfd_bread (extversym, versymhdr->sh_size, input)
9751 	      != versymhdr->sh_size))
9752 	{
9753 	  free (extversym);
9754 	error_ret:
9755 	  free (isymbuf);
9756 	  return FALSE;
9757 	}
9758 
9759       ever = extversym + extsymoff;
9760       isymend = isymbuf + extsymcount;
9761       for (isym = isymbuf; isym < isymend; isym++, ever++)
9762 	{
9763 	  const char *name;
9764 	  Elf_Internal_Versym iver;
9765 	  unsigned short version_index;
9766 
9767 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9768 	      || isym->st_shndx == SHN_UNDEF)
9769 	    continue;
9770 
9771 	  name = bfd_elf_string_from_elf_section (input,
9772 						  hdr->sh_link,
9773 						  isym->st_name);
9774 	  if (strcmp (name, h->root.root.string) != 0)
9775 	    continue;
9776 
9777 	  _bfd_elf_swap_versym_in (input, ever, &iver);
9778 
9779 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9780 	      && !(h->def_regular
9781 		   && h->forced_local))
9782 	    {
9783 	      /* If we have a non-hidden versioned sym, then it should
9784 		 have provided a definition for the undefined sym unless
9785 		 it is defined in a non-shared object and forced local.
9786 	       */
9787 	      abort ();
9788 	    }
9789 
9790 	  version_index = iver.vs_vers & VERSYM_VERSION;
9791 	  if (version_index == 1 || version_index == 2)
9792 	    {
9793 	      /* This is the base or first version.  We can use it.  */
9794 	      free (extversym);
9795 	      free (isymbuf);
9796 	      return TRUE;
9797 	    }
9798 	}
9799 
9800       free (extversym);
9801       free (isymbuf);
9802     }
9803 
9804   return FALSE;
9805 }
9806 
9807 /* Convert ELF common symbol TYPE.  */
9808 
9809 static int
9810 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9811 {
9812   /* Commom symbol can only appear in relocatable link.  */
9813   if (!bfd_link_relocatable (info))
9814     abort ();
9815   switch (info->elf_stt_common)
9816     {
9817     case unchanged:
9818       break;
9819     case elf_stt_common:
9820       type = STT_COMMON;
9821       break;
9822     case no_elf_stt_common:
9823       type = STT_OBJECT;
9824       break;
9825     }
9826   return type;
9827 }
9828 
9829 /* Add an external symbol to the symbol table.  This is called from
9830    the hash table traversal routine.  When generating a shared object,
9831    we go through the symbol table twice.  The first time we output
9832    anything that might have been forced to local scope in a version
9833    script.  The second time we output the symbols that are still
9834    global symbols.  */
9835 
9836 static bfd_boolean
9837 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9838 {
9839   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9840   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9841   struct elf_final_link_info *flinfo = eoinfo->flinfo;
9842   bfd_boolean strip;
9843   Elf_Internal_Sym sym;
9844   asection *input_sec;
9845   const struct elf_backend_data *bed;
9846   long indx;
9847   int ret;
9848   unsigned int type;
9849 
9850   if (h->root.type == bfd_link_hash_warning)
9851     {
9852       h = (struct elf_link_hash_entry *) h->root.u.i.link;
9853       if (h->root.type == bfd_link_hash_new)
9854 	return TRUE;
9855     }
9856 
9857   /* Decide whether to output this symbol in this pass.  */
9858   if (eoinfo->localsyms)
9859     {
9860       if (!h->forced_local)
9861 	return TRUE;
9862     }
9863   else
9864     {
9865       if (h->forced_local)
9866 	return TRUE;
9867     }
9868 
9869   bed = get_elf_backend_data (flinfo->output_bfd);
9870 
9871   if (h->root.type == bfd_link_hash_undefined)
9872     {
9873       /* If we have an undefined symbol reference here then it must have
9874 	 come from a shared library that is being linked in.  (Undefined
9875 	 references in regular files have already been handled unless
9876 	 they are in unreferenced sections which are removed by garbage
9877 	 collection).  */
9878       bfd_boolean ignore_undef = FALSE;
9879 
9880       /* Some symbols may be special in that the fact that they're
9881 	 undefined can be safely ignored - let backend determine that.  */
9882       if (bed->elf_backend_ignore_undef_symbol)
9883 	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9884 
9885       /* If we are reporting errors for this situation then do so now.  */
9886       if (!ignore_undef
9887 	  && h->ref_dynamic_nonweak
9888 	  && (!h->ref_regular || flinfo->info->gc_sections)
9889 	  && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9890 	  && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9891 	(*flinfo->info->callbacks->undefined_symbol)
9892 	  (flinfo->info, h->root.root.string,
9893 	   h->ref_regular ? NULL : h->root.u.undef.abfd,
9894 	   NULL, 0,
9895 	   flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9896 
9897       /* Strip a global symbol defined in a discarded section.  */
9898       if (h->indx == -3)
9899 	return TRUE;
9900     }
9901 
9902   /* We should also warn if a forced local symbol is referenced from
9903      shared libraries.  */
9904   if (bfd_link_executable (flinfo->info)
9905       && h->forced_local
9906       && h->ref_dynamic
9907       && h->def_regular
9908       && !h->dynamic_def
9909       && h->ref_dynamic_nonweak
9910       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9911     {
9912       bfd *def_bfd;
9913       const char *msg;
9914       struct elf_link_hash_entry *hi = h;
9915 
9916       /* Check indirect symbol.  */
9917       while (hi->root.type == bfd_link_hash_indirect)
9918 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9919 
9920       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9921 	/* xgettext:c-format */
9922 	msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9923       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9924 	/* xgettext:c-format */
9925 	msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9926       else
9927 	/* xgettext:c-format */
9928 	msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9929       def_bfd = flinfo->output_bfd;
9930       if (hi->root.u.def.section != bfd_abs_section_ptr)
9931 	def_bfd = hi->root.u.def.section->owner;
9932       _bfd_error_handler (msg, flinfo->output_bfd,
9933 			  h->root.root.string, def_bfd);
9934       bfd_set_error (bfd_error_bad_value);
9935       eoinfo->failed = TRUE;
9936       return FALSE;
9937     }
9938 
9939   /* We don't want to output symbols that have never been mentioned by
9940      a regular file, or that we have been told to strip.  However, if
9941      h->indx is set to -2, the symbol is used by a reloc and we must
9942      output it.  */
9943   strip = FALSE;
9944   if (h->indx == -2)
9945     ;
9946   else if ((h->def_dynamic
9947 	    || h->ref_dynamic
9948 	    || h->root.type == bfd_link_hash_new)
9949 	   && !h->def_regular
9950 	   && !h->ref_regular)
9951     strip = TRUE;
9952   else if (flinfo->info->strip == strip_all)
9953     strip = TRUE;
9954   else if (flinfo->info->strip == strip_some
9955 	   && bfd_hash_lookup (flinfo->info->keep_hash,
9956 			       h->root.root.string, FALSE, FALSE) == NULL)
9957     strip = TRUE;
9958   else if ((h->root.type == bfd_link_hash_defined
9959 	    || h->root.type == bfd_link_hash_defweak)
9960 	   && ((flinfo->info->strip_discarded
9961 		&& discarded_section (h->root.u.def.section))
9962 	       || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9963 		   && h->root.u.def.section->owner != NULL
9964 		   && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9965     strip = TRUE;
9966   else if ((h->root.type == bfd_link_hash_undefined
9967 	    || h->root.type == bfd_link_hash_undefweak)
9968 	   && h->root.u.undef.abfd != NULL
9969 	   && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9970     strip = TRUE;
9971 
9972   type = h->type;
9973 
9974   /* If we're stripping it, and it's not a dynamic symbol, there's
9975      nothing else to do.   However, if it is a forced local symbol or
9976      an ifunc symbol we need to give the backend finish_dynamic_symbol
9977      function a chance to make it dynamic.  */
9978   if (strip
9979       && h->dynindx == -1
9980       && type != STT_GNU_IFUNC
9981       && !h->forced_local)
9982     return TRUE;
9983 
9984   sym.st_value = 0;
9985   sym.st_size = h->size;
9986   sym.st_other = h->other;
9987   switch (h->root.type)
9988     {
9989     default:
9990     case bfd_link_hash_new:
9991     case bfd_link_hash_warning:
9992       abort ();
9993       return FALSE;
9994 
9995     case bfd_link_hash_undefined:
9996     case bfd_link_hash_undefweak:
9997       input_sec = bfd_und_section_ptr;
9998       sym.st_shndx = SHN_UNDEF;
9999       break;
10000 
10001     case bfd_link_hash_defined:
10002     case bfd_link_hash_defweak:
10003       {
10004 	input_sec = h->root.u.def.section;
10005 	if (input_sec->output_section != NULL)
10006 	  {
10007 	    sym.st_shndx =
10008 	      _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10009 						 input_sec->output_section);
10010 	    if (sym.st_shndx == SHN_BAD)
10011 	      {
10012 		_bfd_error_handler
10013 		  /* xgettext:c-format */
10014 		  (_("%pB: could not find output section %pA for input section %pA"),
10015 		   flinfo->output_bfd, input_sec->output_section, input_sec);
10016 		bfd_set_error (bfd_error_nonrepresentable_section);
10017 		eoinfo->failed = TRUE;
10018 		return FALSE;
10019 	      }
10020 
10021 	    /* ELF symbols in relocatable files are section relative,
10022 	       but in nonrelocatable files they are virtual
10023 	       addresses.  */
10024 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
10025 	    if (!bfd_link_relocatable (flinfo->info))
10026 	      {
10027 		sym.st_value += input_sec->output_section->vma;
10028 		if (h->type == STT_TLS)
10029 		  {
10030 		    asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10031 		    if (tls_sec != NULL)
10032 		      sym.st_value -= tls_sec->vma;
10033 		  }
10034 	      }
10035 	  }
10036 	else
10037 	  {
10038 	    BFD_ASSERT (input_sec->owner == NULL
10039 			|| (input_sec->owner->flags & DYNAMIC) != 0);
10040 	    sym.st_shndx = SHN_UNDEF;
10041 	    input_sec = bfd_und_section_ptr;
10042 	  }
10043       }
10044       break;
10045 
10046     case bfd_link_hash_common:
10047       input_sec = h->root.u.c.p->section;
10048       sym.st_shndx = bed->common_section_index (input_sec);
10049       sym.st_value = 1 << h->root.u.c.p->alignment_power;
10050       break;
10051 
10052     case bfd_link_hash_indirect:
10053       /* These symbols are created by symbol versioning.  They point
10054 	 to the decorated version of the name.  For example, if the
10055 	 symbol foo@@GNU_1.2 is the default, which should be used when
10056 	 foo is used with no version, then we add an indirect symbol
10057 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
10058 	 since the indirected symbol is already in the hash table.  */
10059       return TRUE;
10060     }
10061 
10062   if (type == STT_COMMON || type == STT_OBJECT)
10063     switch (h->root.type)
10064       {
10065       case bfd_link_hash_common:
10066 	type = elf_link_convert_common_type (flinfo->info, type);
10067 	break;
10068       case bfd_link_hash_defined:
10069       case bfd_link_hash_defweak:
10070 	if (bed->common_definition (&sym))
10071 	  type = elf_link_convert_common_type (flinfo->info, type);
10072 	else
10073 	  type = STT_OBJECT;
10074 	break;
10075       case bfd_link_hash_undefined:
10076       case bfd_link_hash_undefweak:
10077 	break;
10078       default:
10079 	abort ();
10080       }
10081 
10082   if (h->forced_local)
10083     {
10084       sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10085       /* Turn off visibility on local symbol.  */
10086       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10087     }
10088   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
10089   else if (h->unique_global && h->def_regular)
10090     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10091   else if (h->root.type == bfd_link_hash_undefweak
10092 	   || h->root.type == bfd_link_hash_defweak)
10093     sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10094   else
10095     sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10096   sym.st_target_internal = h->target_internal;
10097 
10098   /* Give the processor backend a chance to tweak the symbol value,
10099      and also to finish up anything that needs to be done for this
10100      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
10101      forced local syms when non-shared is due to a historical quirk.
10102      STT_GNU_IFUNC symbol must go through PLT.  */
10103   if ((h->type == STT_GNU_IFUNC
10104        && h->def_regular
10105        && !bfd_link_relocatable (flinfo->info))
10106       || ((h->dynindx != -1
10107 	   || h->forced_local)
10108 	  && ((bfd_link_pic (flinfo->info)
10109 	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10110 		   || h->root.type != bfd_link_hash_undefweak))
10111 	      || !h->forced_local)
10112 	  && elf_hash_table (flinfo->info)->dynamic_sections_created))
10113     {
10114       if (! ((*bed->elf_backend_finish_dynamic_symbol)
10115 	     (flinfo->output_bfd, flinfo->info, h, &sym)))
10116 	{
10117 	  eoinfo->failed = TRUE;
10118 	  return FALSE;
10119 	}
10120     }
10121 
10122   /* If we are marking the symbol as undefined, and there are no
10123      non-weak references to this symbol from a regular object, then
10124      mark the symbol as weak undefined; if there are non-weak
10125      references, mark the symbol as strong.  We can't do this earlier,
10126      because it might not be marked as undefined until the
10127      finish_dynamic_symbol routine gets through with it.  */
10128   if (sym.st_shndx == SHN_UNDEF
10129       && h->ref_regular
10130       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10131 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10132     {
10133       int bindtype;
10134       type = ELF_ST_TYPE (sym.st_info);
10135 
10136       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10137       if (type == STT_GNU_IFUNC)
10138 	type = STT_FUNC;
10139 
10140       if (h->ref_regular_nonweak)
10141 	bindtype = STB_GLOBAL;
10142       else
10143 	bindtype = STB_WEAK;
10144       sym.st_info = ELF_ST_INFO (bindtype, type);
10145     }
10146 
10147   /* If this is a symbol defined in a dynamic library, don't use the
10148      symbol size from the dynamic library.  Relinking an executable
10149      against a new library may introduce gratuitous changes in the
10150      executable's symbols if we keep the size.  */
10151   if (sym.st_shndx == SHN_UNDEF
10152       && !h->def_regular
10153       && h->def_dynamic)
10154     sym.st_size = 0;
10155 
10156   /* If a non-weak symbol with non-default visibility is not defined
10157      locally, it is a fatal error.  */
10158   if (!bfd_link_relocatable (flinfo->info)
10159       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10160       && ELF_ST_BIND (sym.st_info) != STB_WEAK
10161       && h->root.type == bfd_link_hash_undefined
10162       && !h->def_regular)
10163     {
10164       const char *msg;
10165 
10166       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10167 	/* xgettext:c-format */
10168 	msg = _("%pB: protected symbol `%s' isn't defined");
10169       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10170 	/* xgettext:c-format */
10171 	msg = _("%pB: internal symbol `%s' isn't defined");
10172       else
10173 	/* xgettext:c-format */
10174 	msg = _("%pB: hidden symbol `%s' isn't defined");
10175       _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10176       bfd_set_error (bfd_error_bad_value);
10177       eoinfo->failed = TRUE;
10178       return FALSE;
10179     }
10180 
10181   /* If this symbol should be put in the .dynsym section, then put it
10182      there now.  We already know the symbol index.  We also fill in
10183      the entry in the .hash section.  */
10184   if (h->dynindx != -1
10185       && elf_hash_table (flinfo->info)->dynamic_sections_created
10186       && elf_hash_table (flinfo->info)->dynsym != NULL
10187       && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10188     {
10189       bfd_byte *esym;
10190 
10191       /* Since there is no version information in the dynamic string,
10192 	 if there is no version info in symbol version section, we will
10193 	 have a run-time problem if not linking executable, referenced
10194 	 by shared library, or not bound locally.  */
10195       if (h->verinfo.verdef == NULL
10196 	  && (!bfd_link_executable (flinfo->info)
10197 	      || h->ref_dynamic
10198 	      || !h->def_regular))
10199 	{
10200 	  char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10201 
10202 	  if (p && p [1] != '\0')
10203 	    {
10204 	      _bfd_error_handler
10205 		/* xgettext:c-format */
10206 		(_("%pB: no symbol version section for versioned symbol `%s'"),
10207 		 flinfo->output_bfd, h->root.root.string);
10208 	      eoinfo->failed = TRUE;
10209 	      return FALSE;
10210 	    }
10211 	}
10212 
10213       sym.st_name = h->dynstr_index;
10214       esym = (elf_hash_table (flinfo->info)->dynsym->contents
10215 	      + h->dynindx * bed->s->sizeof_sym);
10216       if (!check_dynsym (flinfo->output_bfd, &sym))
10217 	{
10218 	  eoinfo->failed = TRUE;
10219 	  return FALSE;
10220 	}
10221       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10222 
10223       if (flinfo->hash_sec != NULL)
10224 	{
10225 	  size_t hash_entry_size;
10226 	  bfd_byte *bucketpos;
10227 	  bfd_vma chain;
10228 	  size_t bucketcount;
10229 	  size_t bucket;
10230 
10231 	  bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10232 	  bucket = h->u.elf_hash_value % bucketcount;
10233 
10234 	  hash_entry_size
10235 	    = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10236 	  bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10237 		       + (bucket + 2) * hash_entry_size);
10238 	  chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10239 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10240 		   bucketpos);
10241 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10242 		   ((bfd_byte *) flinfo->hash_sec->contents
10243 		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10244 	}
10245 
10246       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10247 	{
10248 	  Elf_Internal_Versym iversym;
10249 	  Elf_External_Versym *eversym;
10250 
10251 	  if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10252 	    {
10253 	      if (h->verinfo.verdef == NULL
10254 		  || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10255 		      & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10256 		iversym.vs_vers = 0;
10257 	      else
10258 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10259 	    }
10260 	  else
10261 	    {
10262 	      if (h->verinfo.vertree == NULL)
10263 		iversym.vs_vers = 1;
10264 	      else
10265 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10266 	      if (flinfo->info->create_default_symver)
10267 		iversym.vs_vers++;
10268 	    }
10269 
10270 	  /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10271 	     defined locally.  */
10272 	  if (h->versioned == versioned_hidden && h->def_regular)
10273 	    iversym.vs_vers |= VERSYM_HIDDEN;
10274 
10275 	  eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10276 	  eversym += h->dynindx;
10277 	  _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10278 	}
10279     }
10280 
10281   /* If the symbol is undefined, and we didn't output it to .dynsym,
10282      strip it from .symtab too.  Obviously we can't do this for
10283      relocatable output or when needed for --emit-relocs.  */
10284   else if (input_sec == bfd_und_section_ptr
10285 	   && h->indx != -2
10286 	   /* PR 22319 Do not strip global undefined symbols marked as being needed.  */
10287 	   && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10288 	   && !bfd_link_relocatable (flinfo->info))
10289     return TRUE;
10290 
10291   /* Also strip others that we couldn't earlier due to dynamic symbol
10292      processing.  */
10293   if (strip)
10294     return TRUE;
10295   if ((input_sec->flags & SEC_EXCLUDE) != 0)
10296     return TRUE;
10297 
10298   /* Output a FILE symbol so that following locals are not associated
10299      with the wrong input file.  We need one for forced local symbols
10300      if we've seen more than one FILE symbol or when we have exactly
10301      one FILE symbol but global symbols are present in a file other
10302      than the one with the FILE symbol.  We also need one if linker
10303      defined symbols are present.  In practice these conditions are
10304      always met, so just emit the FILE symbol unconditionally.  */
10305   if (eoinfo->localsyms
10306       && !eoinfo->file_sym_done
10307       && eoinfo->flinfo->filesym_count != 0)
10308     {
10309       Elf_Internal_Sym fsym;
10310 
10311       memset (&fsym, 0, sizeof (fsym));
10312       fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10313       fsym.st_shndx = SHN_ABS;
10314       if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10315 				      bfd_und_section_ptr, NULL))
10316 	return FALSE;
10317 
10318       eoinfo->file_sym_done = TRUE;
10319     }
10320 
10321   indx = bfd_get_symcount (flinfo->output_bfd);
10322   ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10323 				   input_sec, h);
10324   if (ret == 0)
10325     {
10326       eoinfo->failed = TRUE;
10327       return FALSE;
10328     }
10329   else if (ret == 1)
10330     h->indx = indx;
10331   else if (h->indx == -2)
10332     abort();
10333 
10334   return TRUE;
10335 }
10336 
10337 /* Return TRUE if special handling is done for relocs in SEC against
10338    symbols defined in discarded sections.  */
10339 
10340 static bfd_boolean
10341 elf_section_ignore_discarded_relocs (asection *sec)
10342 {
10343   const struct elf_backend_data *bed;
10344 
10345   switch (sec->sec_info_type)
10346     {
10347     case SEC_INFO_TYPE_STABS:
10348     case SEC_INFO_TYPE_EH_FRAME:
10349     case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10350       return TRUE;
10351     default:
10352       break;
10353     }
10354 
10355   bed = get_elf_backend_data (sec->owner);
10356   if (bed->elf_backend_ignore_discarded_relocs != NULL
10357       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10358     return TRUE;
10359 
10360   return FALSE;
10361 }
10362 
10363 /* Return a mask saying how ld should treat relocations in SEC against
10364    symbols defined in discarded sections.  If this function returns
10365    COMPLAIN set, ld will issue a warning message.  If this function
10366    returns PRETEND set, and the discarded section was link-once and the
10367    same size as the kept link-once section, ld will pretend that the
10368    symbol was actually defined in the kept section.  Otherwise ld will
10369    zero the reloc (at least that is the intent, but some cooperation by
10370    the target dependent code is needed, particularly for REL targets).  */
10371 
10372 unsigned int
10373 _bfd_elf_default_action_discarded (asection *sec)
10374 {
10375   if (sec->flags & SEC_DEBUGGING)
10376     return PRETEND;
10377 
10378   if (strcmp (".eh_frame", sec->name) == 0)
10379     return 0;
10380 
10381   if (strcmp (".gcc_except_table", sec->name) == 0)
10382     return 0;
10383 
10384   return COMPLAIN | PRETEND;
10385 }
10386 
10387 /* Find a match between a section and a member of a section group.  */
10388 
10389 static asection *
10390 match_group_member (asection *sec, asection *group,
10391 		    struct bfd_link_info *info)
10392 {
10393   asection *first = elf_next_in_group (group);
10394   asection *s = first;
10395 
10396   while (s != NULL)
10397     {
10398       if (bfd_elf_match_symbols_in_sections (s, sec, info))
10399 	return s;
10400 
10401       s = elf_next_in_group (s);
10402       if (s == first)
10403 	break;
10404     }
10405 
10406   return NULL;
10407 }
10408 
10409 /* Check if the kept section of a discarded section SEC can be used
10410    to replace it.  Return the replacement if it is OK.  Otherwise return
10411    NULL.  */
10412 
10413 asection *
10414 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10415 {
10416   asection *kept;
10417 
10418   kept = sec->kept_section;
10419   if (kept != NULL)
10420     {
10421       if ((kept->flags & SEC_GROUP) != 0)
10422 	kept = match_group_member (sec, kept, info);
10423       if (kept != NULL
10424 	  && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10425 	      != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10426 	kept = NULL;
10427       sec->kept_section = kept;
10428     }
10429   return kept;
10430 }
10431 
10432 /* Link an input file into the linker output file.  This function
10433    handles all the sections and relocations of the input file at once.
10434    This is so that we only have to read the local symbols once, and
10435    don't have to keep them in memory.  */
10436 
10437 static bfd_boolean
10438 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10439 {
10440   int (*relocate_section)
10441     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10442      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10443   bfd *output_bfd;
10444   Elf_Internal_Shdr *symtab_hdr;
10445   size_t locsymcount;
10446   size_t extsymoff;
10447   Elf_Internal_Sym *isymbuf;
10448   Elf_Internal_Sym *isym;
10449   Elf_Internal_Sym *isymend;
10450   long *pindex;
10451   asection **ppsection;
10452   asection *o;
10453   const struct elf_backend_data *bed;
10454   struct elf_link_hash_entry **sym_hashes;
10455   bfd_size_type address_size;
10456   bfd_vma r_type_mask;
10457   int r_sym_shift;
10458   bfd_boolean have_file_sym = FALSE;
10459 
10460   output_bfd = flinfo->output_bfd;
10461   bed = get_elf_backend_data (output_bfd);
10462   relocate_section = bed->elf_backend_relocate_section;
10463 
10464   /* If this is a dynamic object, we don't want to do anything here:
10465      we don't want the local symbols, and we don't want the section
10466      contents.  */
10467   if ((input_bfd->flags & DYNAMIC) != 0)
10468     return TRUE;
10469 
10470   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10471   if (elf_bad_symtab (input_bfd))
10472     {
10473       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10474       extsymoff = 0;
10475     }
10476   else
10477     {
10478       locsymcount = symtab_hdr->sh_info;
10479       extsymoff = symtab_hdr->sh_info;
10480     }
10481 
10482   /* Read the local symbols.  */
10483   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10484   if (isymbuf == NULL && locsymcount != 0)
10485     {
10486       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10487 				      flinfo->internal_syms,
10488 				      flinfo->external_syms,
10489 				      flinfo->locsym_shndx);
10490       if (isymbuf == NULL)
10491 	return FALSE;
10492     }
10493 
10494   /* Find local symbol sections and adjust values of symbols in
10495      SEC_MERGE sections.  Write out those local symbols we know are
10496      going into the output file.  */
10497   isymend = isymbuf + locsymcount;
10498   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10499        isym < isymend;
10500        isym++, pindex++, ppsection++)
10501     {
10502       asection *isec;
10503       const char *name;
10504       Elf_Internal_Sym osym;
10505       long indx;
10506       int ret;
10507 
10508       *pindex = -1;
10509 
10510       if (elf_bad_symtab (input_bfd))
10511 	{
10512 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10513 	    {
10514 	      *ppsection = NULL;
10515 	      continue;
10516 	    }
10517 	}
10518 
10519       if (isym->st_shndx == SHN_UNDEF)
10520 	isec = bfd_und_section_ptr;
10521       else if (isym->st_shndx == SHN_ABS)
10522 	isec = bfd_abs_section_ptr;
10523       else if (isym->st_shndx == SHN_COMMON)
10524 	isec = bfd_com_section_ptr;
10525       else
10526 	{
10527 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10528 	  if (isec == NULL)
10529 	    {
10530 	      /* Don't attempt to output symbols with st_shnx in the
10531 		 reserved range other than SHN_ABS and SHN_COMMON.  */
10532 	      isec = bfd_und_section_ptr;
10533 	    }
10534 	  else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10535 		   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10536 	    isym->st_value =
10537 	      _bfd_merged_section_offset (output_bfd, &isec,
10538 					  elf_section_data (isec)->sec_info,
10539 					  isym->st_value);
10540 	}
10541 
10542       *ppsection = isec;
10543 
10544       /* Don't output the first, undefined, symbol.  In fact, don't
10545 	 output any undefined local symbol.  */
10546       if (isec == bfd_und_section_ptr)
10547 	continue;
10548 
10549       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10550 	{
10551 	  /* We never output section symbols.  Instead, we use the
10552 	     section symbol of the corresponding section in the output
10553 	     file.  */
10554 	  continue;
10555 	}
10556 
10557       /* If we are stripping all symbols, we don't want to output this
10558 	 one.  */
10559       if (flinfo->info->strip == strip_all)
10560 	continue;
10561 
10562       /* If we are discarding all local symbols, we don't want to
10563 	 output this one.  If we are generating a relocatable output
10564 	 file, then some of the local symbols may be required by
10565 	 relocs; we output them below as we discover that they are
10566 	 needed.  */
10567       if (flinfo->info->discard == discard_all)
10568 	continue;
10569 
10570       /* If this symbol is defined in a section which we are
10571 	 discarding, we don't need to keep it.  */
10572       if (isym->st_shndx != SHN_UNDEF
10573 	  && isym->st_shndx < SHN_LORESERVE
10574 	  && bfd_section_removed_from_list (output_bfd,
10575 					    isec->output_section))
10576 	continue;
10577 
10578       /* Get the name of the symbol.  */
10579       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10580 					      isym->st_name);
10581       if (name == NULL)
10582 	return FALSE;
10583 
10584       /* See if we are discarding symbols with this name.  */
10585       if ((flinfo->info->strip == strip_some
10586 	   && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10587 	       == NULL))
10588 	  || (((flinfo->info->discard == discard_sec_merge
10589 		&& (isec->flags & SEC_MERGE)
10590 		&& !bfd_link_relocatable (flinfo->info))
10591 	       || flinfo->info->discard == discard_l)
10592 	      && bfd_is_local_label_name (input_bfd, name)))
10593 	continue;
10594 
10595       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10596 	{
10597 	  if (input_bfd->lto_output)
10598 	    /* -flto puts a temp file name here.  This means builds
10599 	       are not reproducible.  Discard the symbol.  */
10600 	    continue;
10601 	  have_file_sym = TRUE;
10602 	  flinfo->filesym_count += 1;
10603 	}
10604       if (!have_file_sym)
10605 	{
10606 	  /* In the absence of debug info, bfd_find_nearest_line uses
10607 	     FILE symbols to determine the source file for local
10608 	     function symbols.  Provide a FILE symbol here if input
10609 	     files lack such, so that their symbols won't be
10610 	     associated with a previous input file.  It's not the
10611 	     source file, but the best we can do.  */
10612 	  have_file_sym = TRUE;
10613 	  flinfo->filesym_count += 1;
10614 	  memset (&osym, 0, sizeof (osym));
10615 	  osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10616 	  osym.st_shndx = SHN_ABS;
10617 	  if (!elf_link_output_symstrtab (flinfo,
10618 					  (input_bfd->lto_output ? NULL
10619 					   : input_bfd->filename),
10620 					  &osym, bfd_abs_section_ptr,
10621 					  NULL))
10622 	    return FALSE;
10623 	}
10624 
10625       osym = *isym;
10626 
10627       /* Adjust the section index for the output file.  */
10628       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10629 							 isec->output_section);
10630       if (osym.st_shndx == SHN_BAD)
10631 	return FALSE;
10632 
10633       /* ELF symbols in relocatable files are section relative, but
10634 	 in executable files they are virtual addresses.  Note that
10635 	 this code assumes that all ELF sections have an associated
10636 	 BFD section with a reasonable value for output_offset; below
10637 	 we assume that they also have a reasonable value for
10638 	 output_section.  Any special sections must be set up to meet
10639 	 these requirements.  */
10640       osym.st_value += isec->output_offset;
10641       if (!bfd_link_relocatable (flinfo->info))
10642 	{
10643 	  osym.st_value += isec->output_section->vma;
10644 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10645 	    {
10646 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
10647 	      if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10648 		osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10649 	      else
10650 		osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10651 					    STT_NOTYPE);
10652 	    }
10653 	}
10654 
10655       indx = bfd_get_symcount (output_bfd);
10656       ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10657       if (ret == 0)
10658 	return FALSE;
10659       else if (ret == 1)
10660 	*pindex = indx;
10661     }
10662 
10663   if (bed->s->arch_size == 32)
10664     {
10665       r_type_mask = 0xff;
10666       r_sym_shift = 8;
10667       address_size = 4;
10668     }
10669   else
10670     {
10671       r_type_mask = 0xffffffff;
10672       r_sym_shift = 32;
10673       address_size = 8;
10674     }
10675 
10676   /* Relocate the contents of each section.  */
10677   sym_hashes = elf_sym_hashes (input_bfd);
10678   for (o = input_bfd->sections; o != NULL; o = o->next)
10679     {
10680       bfd_byte *contents;
10681 
10682       if (! o->linker_mark)
10683 	{
10684 	  /* This section was omitted from the link.  */
10685 	  continue;
10686 	}
10687 
10688       if (!flinfo->info->resolve_section_groups
10689 	  && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10690 	{
10691 	  /* Deal with the group signature symbol.  */
10692 	  struct bfd_elf_section_data *sec_data = elf_section_data (o);
10693 	  unsigned long symndx = sec_data->this_hdr.sh_info;
10694 	  asection *osec = o->output_section;
10695 
10696 	  BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10697 	  if (symndx >= locsymcount
10698 	      || (elf_bad_symtab (input_bfd)
10699 		  && flinfo->sections[symndx] == NULL))
10700 	    {
10701 	      struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10702 	      while (h->root.type == bfd_link_hash_indirect
10703 		     || h->root.type == bfd_link_hash_warning)
10704 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
10705 	      /* Arrange for symbol to be output.  */
10706 	      h->indx = -2;
10707 	      elf_section_data (osec)->this_hdr.sh_info = -2;
10708 	    }
10709 	  else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10710 	    {
10711 	      /* We'll use the output section target_index.  */
10712 	      asection *sec = flinfo->sections[symndx]->output_section;
10713 	      elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10714 	    }
10715 	  else
10716 	    {
10717 	      if (flinfo->indices[symndx] == -1)
10718 		{
10719 		  /* Otherwise output the local symbol now.  */
10720 		  Elf_Internal_Sym sym = isymbuf[symndx];
10721 		  asection *sec = flinfo->sections[symndx]->output_section;
10722 		  const char *name;
10723 		  long indx;
10724 		  int ret;
10725 
10726 		  name = bfd_elf_string_from_elf_section (input_bfd,
10727 							  symtab_hdr->sh_link,
10728 							  sym.st_name);
10729 		  if (name == NULL)
10730 		    return FALSE;
10731 
10732 		  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10733 								    sec);
10734 		  if (sym.st_shndx == SHN_BAD)
10735 		    return FALSE;
10736 
10737 		  sym.st_value += o->output_offset;
10738 
10739 		  indx = bfd_get_symcount (output_bfd);
10740 		  ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10741 						   NULL);
10742 		  if (ret == 0)
10743 		    return FALSE;
10744 		  else if (ret == 1)
10745 		    flinfo->indices[symndx] = indx;
10746 		  else
10747 		    abort ();
10748 		}
10749 	      elf_section_data (osec)->this_hdr.sh_info
10750 		= flinfo->indices[symndx];
10751 	    }
10752 	}
10753 
10754       if ((o->flags & SEC_HAS_CONTENTS) == 0
10755 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10756 	continue;
10757 
10758       if ((o->flags & SEC_LINKER_CREATED) != 0)
10759 	{
10760 	  /* Section was created by _bfd_elf_link_create_dynamic_sections
10761 	     or somesuch.  */
10762 	  continue;
10763 	}
10764 
10765       /* Get the contents of the section.  They have been cached by a
10766 	 relaxation routine.  Note that o is a section in an input
10767 	 file, so the contents field will not have been set by any of
10768 	 the routines which work on output files.  */
10769       if (elf_section_data (o)->this_hdr.contents != NULL)
10770 	{
10771 	  contents = elf_section_data (o)->this_hdr.contents;
10772 	  if (bed->caches_rawsize
10773 	      && o->rawsize != 0
10774 	      && o->rawsize < o->size)
10775 	    {
10776 	      memcpy (flinfo->contents, contents, o->rawsize);
10777 	      contents = flinfo->contents;
10778 	    }
10779 	}
10780       else
10781 	{
10782 	  contents = flinfo->contents;
10783 	  if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10784 	    return FALSE;
10785 	}
10786 
10787       if ((o->flags & SEC_RELOC) != 0)
10788 	{
10789 	  Elf_Internal_Rela *internal_relocs;
10790 	  Elf_Internal_Rela *rel, *relend;
10791 	  int action_discarded;
10792 	  int ret;
10793 
10794 	  /* Get the swapped relocs.  */
10795 	  internal_relocs
10796 	    = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10797 					 flinfo->internal_relocs, FALSE);
10798 	  if (internal_relocs == NULL
10799 	      && o->reloc_count > 0)
10800 	    return FALSE;
10801 
10802 	  /* We need to reverse-copy input .ctors/.dtors sections if
10803 	     they are placed in .init_array/.finit_array for output.  */
10804 	  if (o->size > address_size
10805 	      && ((strncmp (o->name, ".ctors", 6) == 0
10806 		   && strcmp (o->output_section->name,
10807 			      ".init_array") == 0)
10808 		  || (strncmp (o->name, ".dtors", 6) == 0
10809 		      && strcmp (o->output_section->name,
10810 				 ".fini_array") == 0))
10811 	      && (o->name[6] == 0 || o->name[6] == '.'))
10812 	    {
10813 	      if (o->size * bed->s->int_rels_per_ext_rel
10814 		  != o->reloc_count * address_size)
10815 		{
10816 		  _bfd_error_handler
10817 		    /* xgettext:c-format */
10818 		    (_("error: %pB: size of section %pA is not "
10819 		       "multiple of address size"),
10820 		     input_bfd, o);
10821 		  bfd_set_error (bfd_error_bad_value);
10822 		  return FALSE;
10823 		}
10824 	      o->flags |= SEC_ELF_REVERSE_COPY;
10825 	    }
10826 
10827 	  action_discarded = -1;
10828 	  if (!elf_section_ignore_discarded_relocs (o))
10829 	    action_discarded = (*bed->action_discarded) (o);
10830 
10831 	  /* Run through the relocs evaluating complex reloc symbols and
10832 	     looking for relocs against symbols from discarded sections
10833 	     or section symbols from removed link-once sections.
10834 	     Complain about relocs against discarded sections.  Zero
10835 	     relocs against removed link-once sections.  */
10836 
10837 	  rel = internal_relocs;
10838 	  relend = rel + o->reloc_count;
10839 	  for ( ; rel < relend; rel++)
10840 	    {
10841 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
10842 	      unsigned int s_type;
10843 	      asection **ps, *sec;
10844 	      struct elf_link_hash_entry *h = NULL;
10845 	      const char *sym_name;
10846 
10847 	      if (r_symndx == STN_UNDEF)
10848 		continue;
10849 
10850 	      if (r_symndx >= locsymcount
10851 		  || (elf_bad_symtab (input_bfd)
10852 		      && flinfo->sections[r_symndx] == NULL))
10853 		{
10854 		  h = sym_hashes[r_symndx - extsymoff];
10855 
10856 		  /* Badly formatted input files can contain relocs that
10857 		     reference non-existant symbols.  Check here so that
10858 		     we do not seg fault.  */
10859 		  if (h == NULL)
10860 		    {
10861 		      _bfd_error_handler
10862 			/* xgettext:c-format */
10863 			(_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10864 			   "that references a non-existent global symbol"),
10865 			 input_bfd, (uint64_t) rel->r_info, o);
10866 		      bfd_set_error (bfd_error_bad_value);
10867 		      return FALSE;
10868 		    }
10869 
10870 		  while (h->root.type == bfd_link_hash_indirect
10871 			 || h->root.type == bfd_link_hash_warning)
10872 		    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10873 
10874 		  s_type = h->type;
10875 
10876 		  /* If a plugin symbol is referenced from a non-IR file,
10877 		     mark the symbol as undefined.  Note that the
10878 		     linker may attach linker created dynamic sections
10879 		     to the plugin bfd.  Symbols defined in linker
10880 		     created sections are not plugin symbols.  */
10881 		  if ((h->root.non_ir_ref_regular
10882 		       || h->root.non_ir_ref_dynamic)
10883 		      && (h->root.type == bfd_link_hash_defined
10884 			  || h->root.type == bfd_link_hash_defweak)
10885 		      && (h->root.u.def.section->flags
10886 			  & SEC_LINKER_CREATED) == 0
10887 		      && h->root.u.def.section->owner != NULL
10888 		      && (h->root.u.def.section->owner->flags
10889 			  & BFD_PLUGIN) != 0)
10890 		    {
10891 		      h->root.type = bfd_link_hash_undefined;
10892 		      h->root.u.undef.abfd = h->root.u.def.section->owner;
10893 		    }
10894 
10895 		  ps = NULL;
10896 		  if (h->root.type == bfd_link_hash_defined
10897 		      || h->root.type == bfd_link_hash_defweak)
10898 		    ps = &h->root.u.def.section;
10899 
10900 		  sym_name = h->root.root.string;
10901 		}
10902 	      else
10903 		{
10904 		  Elf_Internal_Sym *sym = isymbuf + r_symndx;
10905 
10906 		  s_type = ELF_ST_TYPE (sym->st_info);
10907 		  ps = &flinfo->sections[r_symndx];
10908 		  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10909 					       sym, *ps);
10910 		}
10911 
10912 	      if ((s_type == STT_RELC || s_type == STT_SRELC)
10913 		  && !bfd_link_relocatable (flinfo->info))
10914 		{
10915 		  bfd_vma val;
10916 		  bfd_vma dot = (rel->r_offset
10917 				 + o->output_offset + o->output_section->vma);
10918 #ifdef DEBUG
10919 		  printf ("Encountered a complex symbol!");
10920 		  printf (" (input_bfd %s, section %s, reloc %ld\n",
10921 			  input_bfd->filename, o->name,
10922 			  (long) (rel - internal_relocs));
10923 		  printf (" symbol: idx  %8.8lx, name %s\n",
10924 			  r_symndx, sym_name);
10925 		  printf (" reloc : info %8.8lx, addr %8.8lx\n",
10926 			  (unsigned long) rel->r_info,
10927 			  (unsigned long) rel->r_offset);
10928 #endif
10929 		  if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10930 				    isymbuf, locsymcount, s_type == STT_SRELC))
10931 		    return FALSE;
10932 
10933 		  /* Symbol evaluated OK.  Update to absolute value.  */
10934 		  set_symbol_value (input_bfd, isymbuf, locsymcount,
10935 				    r_symndx, val);
10936 		  continue;
10937 		}
10938 
10939 	      if (action_discarded != -1 && ps != NULL)
10940 		{
10941 		  /* Complain if the definition comes from a
10942 		     discarded section.  */
10943 		  if ((sec = *ps) != NULL && discarded_section (sec))
10944 		    {
10945 		      BFD_ASSERT (r_symndx != STN_UNDEF);
10946 		      if (action_discarded & COMPLAIN)
10947 			(*flinfo->info->callbacks->einfo)
10948 			  /* xgettext:c-format */
10949 			  (_("%X`%s' referenced in section `%pA' of %pB: "
10950 			     "defined in discarded section `%pA' of %pB\n"),
10951 			   sym_name, o, input_bfd, sec, sec->owner);
10952 
10953 		      /* Try to do the best we can to support buggy old
10954 			 versions of gcc.  Pretend that the symbol is
10955 			 really defined in the kept linkonce section.
10956 			 FIXME: This is quite broken.  Modifying the
10957 			 symbol here means we will be changing all later
10958 			 uses of the symbol, not just in this section.  */
10959 		      if (action_discarded & PRETEND)
10960 			{
10961 			  asection *kept;
10962 
10963 			  kept = _bfd_elf_check_kept_section (sec,
10964 							      flinfo->info);
10965 			  if (kept != NULL)
10966 			    {
10967 			      *ps = kept;
10968 			      continue;
10969 			    }
10970 			}
10971 		    }
10972 		}
10973 	    }
10974 
10975 	  /* Relocate the section by invoking a back end routine.
10976 
10977 	     The back end routine is responsible for adjusting the
10978 	     section contents as necessary, and (if using Rela relocs
10979 	     and generating a relocatable output file) adjusting the
10980 	     reloc addend as necessary.
10981 
10982 	     The back end routine does not have to worry about setting
10983 	     the reloc address or the reloc symbol index.
10984 
10985 	     The back end routine is given a pointer to the swapped in
10986 	     internal symbols, and can access the hash table entries
10987 	     for the external symbols via elf_sym_hashes (input_bfd).
10988 
10989 	     When generating relocatable output, the back end routine
10990 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
10991 	     output symbol is going to be a section symbol
10992 	     corresponding to the output section, which will require
10993 	     the addend to be adjusted.  */
10994 
10995 	  ret = (*relocate_section) (output_bfd, flinfo->info,
10996 				     input_bfd, o, contents,
10997 				     internal_relocs,
10998 				     isymbuf,
10999 				     flinfo->sections);
11000 	  if (!ret)
11001 	    return FALSE;
11002 
11003 	  if (ret == 2
11004 	      || bfd_link_relocatable (flinfo->info)
11005 	      || flinfo->info->emitrelocations)
11006 	    {
11007 	      Elf_Internal_Rela *irela;
11008 	      Elf_Internal_Rela *irelaend, *irelamid;
11009 	      bfd_vma last_offset;
11010 	      struct elf_link_hash_entry **rel_hash;
11011 	      struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11012 	      Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11013 	      unsigned int next_erel;
11014 	      bfd_boolean rela_normal;
11015 	      struct bfd_elf_section_data *esdi, *esdo;
11016 
11017 	      esdi = elf_section_data (o);
11018 	      esdo = elf_section_data (o->output_section);
11019 	      rela_normal = FALSE;
11020 
11021 	      /* Adjust the reloc addresses and symbol indices.  */
11022 
11023 	      irela = internal_relocs;
11024 	      irelaend = irela + o->reloc_count;
11025 	      rel_hash = esdo->rel.hashes + esdo->rel.count;
11026 	      /* We start processing the REL relocs, if any.  When we reach
11027 		 IRELAMID in the loop, we switch to the RELA relocs.  */
11028 	      irelamid = irela;
11029 	      if (esdi->rel.hdr != NULL)
11030 		irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11031 			     * bed->s->int_rels_per_ext_rel);
11032 	      rel_hash_list = rel_hash;
11033 	      rela_hash_list = NULL;
11034 	      last_offset = o->output_offset;
11035 	      if (!bfd_link_relocatable (flinfo->info))
11036 		last_offset += o->output_section->vma;
11037 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11038 		{
11039 		  unsigned long r_symndx;
11040 		  asection *sec;
11041 		  Elf_Internal_Sym sym;
11042 
11043 		  if (next_erel == bed->s->int_rels_per_ext_rel)
11044 		    {
11045 		      rel_hash++;
11046 		      next_erel = 0;
11047 		    }
11048 
11049 		  if (irela == irelamid)
11050 		    {
11051 		      rel_hash = esdo->rela.hashes + esdo->rela.count;
11052 		      rela_hash_list = rel_hash;
11053 		      rela_normal = bed->rela_normal;
11054 		    }
11055 
11056 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
11057 							     flinfo->info, o,
11058 							     irela->r_offset);
11059 		  if (irela->r_offset >= (bfd_vma) -2)
11060 		    {
11061 		      /* This is a reloc for a deleted entry or somesuch.
11062 			 Turn it into an R_*_NONE reloc, at the same
11063 			 offset as the last reloc.  elf_eh_frame.c and
11064 			 bfd_elf_discard_info rely on reloc offsets
11065 			 being ordered.  */
11066 		      irela->r_offset = last_offset;
11067 		      irela->r_info = 0;
11068 		      irela->r_addend = 0;
11069 		      continue;
11070 		    }
11071 
11072 		  irela->r_offset += o->output_offset;
11073 
11074 		  /* Relocs in an executable have to be virtual addresses.  */
11075 		  if (!bfd_link_relocatable (flinfo->info))
11076 		    irela->r_offset += o->output_section->vma;
11077 
11078 		  last_offset = irela->r_offset;
11079 
11080 		  r_symndx = irela->r_info >> r_sym_shift;
11081 		  if (r_symndx == STN_UNDEF)
11082 		    continue;
11083 
11084 		  if (r_symndx >= locsymcount
11085 		      || (elf_bad_symtab (input_bfd)
11086 			  && flinfo->sections[r_symndx] == NULL))
11087 		    {
11088 		      struct elf_link_hash_entry *rh;
11089 		      unsigned long indx;
11090 
11091 		      /* This is a reloc against a global symbol.  We
11092 			 have not yet output all the local symbols, so
11093 			 we do not know the symbol index of any global
11094 			 symbol.  We set the rel_hash entry for this
11095 			 reloc to point to the global hash table entry
11096 			 for this symbol.  The symbol index is then
11097 			 set at the end of bfd_elf_final_link.  */
11098 		      indx = r_symndx - extsymoff;
11099 		      rh = elf_sym_hashes (input_bfd)[indx];
11100 		      while (rh->root.type == bfd_link_hash_indirect
11101 			     || rh->root.type == bfd_link_hash_warning)
11102 			rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11103 
11104 		      /* Setting the index to -2 tells
11105 			 elf_link_output_extsym that this symbol is
11106 			 used by a reloc.  */
11107 		      BFD_ASSERT (rh->indx < 0);
11108 		      rh->indx = -2;
11109 		      *rel_hash = rh;
11110 
11111 		      continue;
11112 		    }
11113 
11114 		  /* This is a reloc against a local symbol.  */
11115 
11116 		  *rel_hash = NULL;
11117 		  sym = isymbuf[r_symndx];
11118 		  sec = flinfo->sections[r_symndx];
11119 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11120 		    {
11121 		      /* I suppose the backend ought to fill in the
11122 			 section of any STT_SECTION symbol against a
11123 			 processor specific section.  */
11124 		      r_symndx = STN_UNDEF;
11125 		      if (bfd_is_abs_section (sec))
11126 			;
11127 		      else if (sec == NULL || sec->owner == NULL)
11128 			{
11129 			  bfd_set_error (bfd_error_bad_value);
11130 			  return FALSE;
11131 			}
11132 		      else
11133 			{
11134 			  asection *osec = sec->output_section;
11135 
11136 			  /* If we have discarded a section, the output
11137 			     section will be the absolute section.  In
11138 			     case of discarded SEC_MERGE sections, use
11139 			     the kept section.  relocate_section should
11140 			     have already handled discarded linkonce
11141 			     sections.  */
11142 			  if (bfd_is_abs_section (osec)
11143 			      && sec->kept_section != NULL
11144 			      && sec->kept_section->output_section != NULL)
11145 			    {
11146 			      osec = sec->kept_section->output_section;
11147 			      irela->r_addend -= osec->vma;
11148 			    }
11149 
11150 			  if (!bfd_is_abs_section (osec))
11151 			    {
11152 			      r_symndx = osec->target_index;
11153 			      if (r_symndx == STN_UNDEF)
11154 				{
11155 				  irela->r_addend += osec->vma;
11156 				  osec = _bfd_nearby_section (output_bfd, osec,
11157 							      osec->vma);
11158 				  irela->r_addend -= osec->vma;
11159 				  r_symndx = osec->target_index;
11160 				}
11161 			    }
11162 			}
11163 
11164 		      /* Adjust the addend according to where the
11165 			 section winds up in the output section.  */
11166 		      if (rela_normal)
11167 			irela->r_addend += sec->output_offset;
11168 		    }
11169 		  else
11170 		    {
11171 		      if (flinfo->indices[r_symndx] == -1)
11172 			{
11173 			  unsigned long shlink;
11174 			  const char *name;
11175 			  asection *osec;
11176 			  long indx;
11177 
11178 			  if (flinfo->info->strip == strip_all)
11179 			    {
11180 			      /* You can't do ld -r -s.  */
11181 			      bfd_set_error (bfd_error_invalid_operation);
11182 			      return FALSE;
11183 			    }
11184 
11185 			  /* This symbol was skipped earlier, but
11186 			     since it is needed by a reloc, we
11187 			     must output it now.  */
11188 			  shlink = symtab_hdr->sh_link;
11189 			  name = (bfd_elf_string_from_elf_section
11190 				  (input_bfd, shlink, sym.st_name));
11191 			  if (name == NULL)
11192 			    return FALSE;
11193 
11194 			  osec = sec->output_section;
11195 			  sym.st_shndx =
11196 			    _bfd_elf_section_from_bfd_section (output_bfd,
11197 							       osec);
11198 			  if (sym.st_shndx == SHN_BAD)
11199 			    return FALSE;
11200 
11201 			  sym.st_value += sec->output_offset;
11202 			  if (!bfd_link_relocatable (flinfo->info))
11203 			    {
11204 			      sym.st_value += osec->vma;
11205 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11206 				{
11207 				  struct elf_link_hash_table *htab
11208 				    = elf_hash_table (flinfo->info);
11209 
11210 				  /* STT_TLS symbols are relative to PT_TLS
11211 				     segment base.  */
11212 				  if (htab->tls_sec != NULL)
11213 				    sym.st_value -= htab->tls_sec->vma;
11214 				  else
11215 				    sym.st_info
11216 				      = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11217 						     STT_NOTYPE);
11218 				}
11219 			    }
11220 
11221 			  indx = bfd_get_symcount (output_bfd);
11222 			  ret = elf_link_output_symstrtab (flinfo, name,
11223 							   &sym, sec,
11224 							   NULL);
11225 			  if (ret == 0)
11226 			    return FALSE;
11227 			  else if (ret == 1)
11228 			    flinfo->indices[r_symndx] = indx;
11229 			  else
11230 			    abort ();
11231 			}
11232 
11233 		      r_symndx = flinfo->indices[r_symndx];
11234 		    }
11235 
11236 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11237 				   | (irela->r_info & r_type_mask));
11238 		}
11239 
11240 	      /* Swap out the relocs.  */
11241 	      input_rel_hdr = esdi->rel.hdr;
11242 	      if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11243 		{
11244 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
11245 						     input_rel_hdr,
11246 						     internal_relocs,
11247 						     rel_hash_list))
11248 		    return FALSE;
11249 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11250 				      * bed->s->int_rels_per_ext_rel);
11251 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11252 		}
11253 
11254 	      input_rela_hdr = esdi->rela.hdr;
11255 	      if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11256 		{
11257 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
11258 						     input_rela_hdr,
11259 						     internal_relocs,
11260 						     rela_hash_list))
11261 		    return FALSE;
11262 		}
11263 	    }
11264 	}
11265 
11266       /* Write out the modified section contents.  */
11267       if (bed->elf_backend_write_section
11268 	  && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11269 						contents))
11270 	{
11271 	  /* Section written out.  */
11272 	}
11273       else switch (o->sec_info_type)
11274 	{
11275 	case SEC_INFO_TYPE_STABS:
11276 	  if (! (_bfd_write_section_stabs
11277 		 (output_bfd,
11278 		  &elf_hash_table (flinfo->info)->stab_info,
11279 		  o, &elf_section_data (o)->sec_info, contents)))
11280 	    return FALSE;
11281 	  break;
11282 	case SEC_INFO_TYPE_MERGE:
11283 	  if (! _bfd_write_merged_section (output_bfd, o,
11284 					   elf_section_data (o)->sec_info))
11285 	    return FALSE;
11286 	  break;
11287 	case SEC_INFO_TYPE_EH_FRAME:
11288 	  {
11289 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11290 						   o, contents))
11291 	      return FALSE;
11292 	  }
11293 	  break;
11294 	case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11295 	  {
11296 	    if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11297 							 flinfo->info,
11298 							 o, contents))
11299 	      return FALSE;
11300 	  }
11301 	  break;
11302 	default:
11303 	  {
11304 	    if (! (o->flags & SEC_EXCLUDE))
11305 	      {
11306 		file_ptr offset = (file_ptr) o->output_offset;
11307 		bfd_size_type todo = o->size;
11308 
11309 		offset *= bfd_octets_per_byte (output_bfd, o);
11310 
11311 		if ((o->flags & SEC_ELF_REVERSE_COPY))
11312 		  {
11313 		    /* Reverse-copy input section to output.  */
11314 		    do
11315 		      {
11316 			todo -= address_size;
11317 			if (! bfd_set_section_contents (output_bfd,
11318 							o->output_section,
11319 							contents + todo,
11320 							offset,
11321 							address_size))
11322 			  return FALSE;
11323 			if (todo == 0)
11324 			  break;
11325 			offset += address_size;
11326 		      }
11327 		    while (1);
11328 		  }
11329 		else if (! bfd_set_section_contents (output_bfd,
11330 						     o->output_section,
11331 						     contents,
11332 						     offset, todo))
11333 		  return FALSE;
11334 	      }
11335 	  }
11336 	  break;
11337 	}
11338     }
11339 
11340   return TRUE;
11341 }
11342 
11343 /* Generate a reloc when linking an ELF file.  This is a reloc
11344    requested by the linker, and does not come from any input file.  This
11345    is used to build constructor and destructor tables when linking
11346    with -Ur.  */
11347 
11348 static bfd_boolean
11349 elf_reloc_link_order (bfd *output_bfd,
11350 		      struct bfd_link_info *info,
11351 		      asection *output_section,
11352 		      struct bfd_link_order *link_order)
11353 {
11354   reloc_howto_type *howto;
11355   long indx;
11356   bfd_vma offset;
11357   bfd_vma addend;
11358   struct bfd_elf_section_reloc_data *reldata;
11359   struct elf_link_hash_entry **rel_hash_ptr;
11360   Elf_Internal_Shdr *rel_hdr;
11361   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11362   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11363   bfd_byte *erel;
11364   unsigned int i;
11365   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11366 
11367   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11368   if (howto == NULL)
11369     {
11370       bfd_set_error (bfd_error_bad_value);
11371       return FALSE;
11372     }
11373 
11374   addend = link_order->u.reloc.p->addend;
11375 
11376   if (esdo->rel.hdr)
11377     reldata = &esdo->rel;
11378   else if (esdo->rela.hdr)
11379     reldata = &esdo->rela;
11380   else
11381     {
11382       reldata = NULL;
11383       BFD_ASSERT (0);
11384     }
11385 
11386   /* Figure out the symbol index.  */
11387   rel_hash_ptr = reldata->hashes + reldata->count;
11388   if (link_order->type == bfd_section_reloc_link_order)
11389     {
11390       indx = link_order->u.reloc.p->u.section->target_index;
11391       BFD_ASSERT (indx != 0);
11392       *rel_hash_ptr = NULL;
11393     }
11394   else
11395     {
11396       struct elf_link_hash_entry *h;
11397 
11398       /* Treat a reloc against a defined symbol as though it were
11399 	 actually against the section.  */
11400       h = ((struct elf_link_hash_entry *)
11401 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
11402 					 link_order->u.reloc.p->u.name,
11403 					 FALSE, FALSE, TRUE));
11404       if (h != NULL
11405 	  && (h->root.type == bfd_link_hash_defined
11406 	      || h->root.type == bfd_link_hash_defweak))
11407 	{
11408 	  asection *section;
11409 
11410 	  section = h->root.u.def.section;
11411 	  indx = section->output_section->target_index;
11412 	  *rel_hash_ptr = NULL;
11413 	  /* It seems that we ought to add the symbol value to the
11414 	     addend here, but in practice it has already been added
11415 	     because it was passed to constructor_callback.  */
11416 	  addend += section->output_section->vma + section->output_offset;
11417 	}
11418       else if (h != NULL)
11419 	{
11420 	  /* Setting the index to -2 tells elf_link_output_extsym that
11421 	     this symbol is used by a reloc.  */
11422 	  h->indx = -2;
11423 	  *rel_hash_ptr = h;
11424 	  indx = 0;
11425 	}
11426       else
11427 	{
11428 	  (*info->callbacks->unattached_reloc)
11429 	    (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11430 	  indx = 0;
11431 	}
11432     }
11433 
11434   /* If this is an inplace reloc, we must write the addend into the
11435      object file.  */
11436   if (howto->partial_inplace && addend != 0)
11437     {
11438       bfd_size_type size;
11439       bfd_reloc_status_type rstat;
11440       bfd_byte *buf;
11441       bfd_boolean ok;
11442       const char *sym_name;
11443       bfd_size_type octets;
11444 
11445       size = (bfd_size_type) bfd_get_reloc_size (howto);
11446       buf = (bfd_byte *) bfd_zmalloc (size);
11447       if (buf == NULL && size != 0)
11448 	return FALSE;
11449       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11450       switch (rstat)
11451 	{
11452 	case bfd_reloc_ok:
11453 	  break;
11454 
11455 	default:
11456 	case bfd_reloc_outofrange:
11457 	  abort ();
11458 
11459 	case bfd_reloc_overflow:
11460 	  if (link_order->type == bfd_section_reloc_link_order)
11461 	    sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11462 	  else
11463 	    sym_name = link_order->u.reloc.p->u.name;
11464 	  (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11465 					      howto->name, addend, NULL, NULL,
11466 					      (bfd_vma) 0);
11467 	  break;
11468 	}
11469 
11470       octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11471 							 output_section);
11472       ok = bfd_set_section_contents (output_bfd, output_section, buf,
11473 				     octets, size);
11474       free (buf);
11475       if (! ok)
11476 	return FALSE;
11477     }
11478 
11479   /* The address of a reloc is relative to the section in a
11480      relocatable file, and is a virtual address in an executable
11481      file.  */
11482   offset = link_order->offset;
11483   if (! bfd_link_relocatable (info))
11484     offset += output_section->vma;
11485 
11486   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11487     {
11488       irel[i].r_offset = offset;
11489       irel[i].r_info = 0;
11490       irel[i].r_addend = 0;
11491     }
11492   if (bed->s->arch_size == 32)
11493     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11494   else
11495 #ifdef BFD64
11496           {
11497             bfd_uint64_t indx64 = indx;
11498             irel[0].r_info = ELF64_R_INFO (indx64, howto->type);
11499           }
11500 #else
11501           BFD_FAIL();
11502 #endif
11503 
11504   rel_hdr = reldata->hdr;
11505   erel = rel_hdr->contents;
11506   if (rel_hdr->sh_type == SHT_REL)
11507     {
11508       erel += reldata->count * bed->s->sizeof_rel;
11509       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11510     }
11511   else
11512     {
11513       irel[0].r_addend = addend;
11514       erel += reldata->count * bed->s->sizeof_rela;
11515       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11516     }
11517 
11518   ++reldata->count;
11519 
11520   return TRUE;
11521 }
11522 
11523 
11524 /* Compare two sections based on the locations of the sections they are
11525    linked to.  Used by elf_fixup_link_order.  */
11526 
11527 static int
11528 compare_link_order (const void *a, const void *b)
11529 {
11530   const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11531   const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11532   asection *asec = elf_linked_to_section (alo->u.indirect.section);
11533   asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11534   bfd_vma apos = asec->output_section->lma + asec->output_offset;
11535   bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11536 
11537   if (apos < bpos)
11538     return -1;
11539   if (apos > bpos)
11540     return 1;
11541 
11542   /* The only way we should get matching LMAs is when the first of two
11543      sections has zero size.  */
11544   if (asec->size < bsec->size)
11545     return -1;
11546   if (asec->size > bsec->size)
11547     return 1;
11548 
11549   /* If they are both zero size then they almost certainly have the same
11550      VMA and thus are not ordered with respect to each other.  Test VMA
11551      anyway, and fall back to id to make the result reproducible across
11552      qsort implementations.  */
11553   apos = asec->output_section->vma + asec->output_offset;
11554   bpos = bsec->output_section->vma + bsec->output_offset;
11555   if (apos < bpos)
11556     return -1;
11557   if (apos > bpos)
11558     return 1;
11559 
11560   return asec->id - bsec->id;
11561 }
11562 
11563 
11564 /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
11565    order as their linked sections.  Returns false if this could not be done
11566    because an output section includes both ordered and unordered
11567    sections.  Ideally we'd do this in the linker proper.  */
11568 
11569 static bfd_boolean
11570 elf_fixup_link_order (bfd *abfd, asection *o)
11571 {
11572   size_t seen_linkorder;
11573   size_t seen_other;
11574   size_t n;
11575   struct bfd_link_order *p;
11576   bfd *sub;
11577   struct bfd_link_order **sections;
11578   asection *s, *other_sec, *linkorder_sec;
11579   bfd_vma offset;
11580 
11581   other_sec = NULL;
11582   linkorder_sec = NULL;
11583   seen_other = 0;
11584   seen_linkorder = 0;
11585   for (p = o->map_head.link_order; p != NULL; p = p->next)
11586     {
11587       if (p->type == bfd_indirect_link_order)
11588 	{
11589 	  s = p->u.indirect.section;
11590 	  sub = s->owner;
11591 	  if ((s->flags & SEC_LINKER_CREATED) == 0
11592 	      && bfd_get_flavour (sub) == bfd_target_elf_flavour
11593 	      && elf_section_data (s) != NULL
11594 	      && elf_linked_to_section (s) != NULL)
11595 	    {
11596 	      seen_linkorder++;
11597 	      linkorder_sec = s;
11598 	    }
11599 	  else
11600 	    {
11601 	      seen_other++;
11602 	      other_sec = s;
11603 	    }
11604 	}
11605       else
11606 	seen_other++;
11607 
11608       if (seen_other && seen_linkorder)
11609 	{
11610 	  if (other_sec && linkorder_sec)
11611 	    _bfd_error_handler
11612 	      /* xgettext:c-format */
11613 	      (_("%pA has both ordered [`%pA' in %pB] "
11614 		 "and unordered [`%pA' in %pB] sections"),
11615 	       o, linkorder_sec, linkorder_sec->owner,
11616 	       other_sec, other_sec->owner);
11617 	  else
11618 	    _bfd_error_handler
11619 	      (_("%pA has both ordered and unordered sections"), o);
11620 	  bfd_set_error (bfd_error_bad_value);
11621 	  return FALSE;
11622 	}
11623     }
11624 
11625   if (!seen_linkorder)
11626     return TRUE;
11627 
11628   sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11629   if (sections == NULL)
11630     return FALSE;
11631 
11632   seen_linkorder = 0;
11633   for (p = o->map_head.link_order; p != NULL; p = p->next)
11634     sections[seen_linkorder++] = p;
11635 
11636   /* Sort the input sections in the order of their linked section.  */
11637   qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11638 
11639   /* Change the offsets of the sections.  */
11640   offset = 0;
11641   for (n = 0; n < seen_linkorder; n++)
11642     {
11643       bfd_vma mask;
11644       s = sections[n]->u.indirect.section;
11645       mask = ~(bfd_vma) 0 << s->alignment_power;
11646       offset = (offset + ~mask) & mask;
11647       s->output_offset = offset / bfd_octets_per_byte (abfd, s);
11648       sections[n]->offset = offset;
11649       offset += sections[n]->size;
11650     }
11651 
11652   free (sections);
11653   return TRUE;
11654 }
11655 
11656 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11657    Returns TRUE upon success, FALSE otherwise.  */
11658 
11659 static bfd_boolean
11660 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11661 {
11662   bfd_boolean ret = FALSE;
11663   bfd *implib_bfd;
11664   const struct elf_backend_data *bed;
11665   flagword flags;
11666   enum bfd_architecture arch;
11667   unsigned int mach;
11668   asymbol **sympp = NULL;
11669   long symsize;
11670   long symcount;
11671   long src_count;
11672   elf_symbol_type *osymbuf;
11673 
11674   implib_bfd = info->out_implib_bfd;
11675   bed = get_elf_backend_data (abfd);
11676 
11677   if (!bfd_set_format (implib_bfd, bfd_object))
11678     return FALSE;
11679 
11680   /* Use flag from executable but make it a relocatable object.  */
11681   flags = bfd_get_file_flags (abfd);
11682   flags &= ~HAS_RELOC;
11683   if (!bfd_set_start_address (implib_bfd, 0)
11684       || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11685     return FALSE;
11686 
11687   /* Copy architecture of output file to import library file.  */
11688   arch = bfd_get_arch (abfd);
11689   mach = bfd_get_mach (abfd);
11690   if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11691       && (abfd->target_defaulted
11692 	  || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11693     return FALSE;
11694 
11695   /* Get symbol table size.  */
11696   symsize = bfd_get_symtab_upper_bound (abfd);
11697   if (symsize < 0)
11698     return FALSE;
11699 
11700   /* Read in the symbol table.  */
11701   sympp = (asymbol **) bfd_malloc (symsize);
11702   if (sympp == NULL)
11703     return FALSE;
11704 
11705   symcount = bfd_canonicalize_symtab (abfd, sympp);
11706   if (symcount < 0)
11707     goto free_sym_buf;
11708 
11709   /* Allow the BFD backend to copy any private header data it
11710      understands from the output BFD to the import library BFD.  */
11711   if (! bfd_copy_private_header_data (abfd, implib_bfd))
11712     goto free_sym_buf;
11713 
11714   /* Filter symbols to appear in the import library.  */
11715   if (bed->elf_backend_filter_implib_symbols)
11716     symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11717 						       symcount);
11718   else
11719     symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11720   if (symcount == 0)
11721     {
11722       bfd_set_error (bfd_error_no_symbols);
11723       _bfd_error_handler (_("%pB: no symbol found for import library"),
11724 			  implib_bfd);
11725       goto free_sym_buf;
11726     }
11727 
11728 
11729   /* Make symbols absolute.  */
11730   osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11731 					    sizeof (*osymbuf));
11732   if (osymbuf == NULL)
11733     goto free_sym_buf;
11734 
11735   for (src_count = 0; src_count < symcount; src_count++)
11736     {
11737       memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11738 	      sizeof (*osymbuf));
11739       osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11740       osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11741       osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11742       osymbuf[src_count].internal_elf_sym.st_value =
11743 	osymbuf[src_count].symbol.value;
11744       sympp[src_count] = &osymbuf[src_count].symbol;
11745     }
11746 
11747   bfd_set_symtab (implib_bfd, sympp, symcount);
11748 
11749   /* Allow the BFD backend to copy any private data it understands
11750      from the output BFD to the import library BFD.  This is done last
11751      to permit the routine to look at the filtered symbol table.  */
11752   if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11753     goto free_sym_buf;
11754 
11755   if (!bfd_close (implib_bfd))
11756     goto free_sym_buf;
11757 
11758   ret = TRUE;
11759 
11760 free_sym_buf:
11761   free (sympp);
11762   return ret;
11763 }
11764 
11765 static void
11766 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11767 {
11768   asection *o;
11769 
11770   if (flinfo->symstrtab != NULL)
11771     _bfd_elf_strtab_free (flinfo->symstrtab);
11772   if (flinfo->contents != NULL)
11773     free (flinfo->contents);
11774   if (flinfo->external_relocs != NULL)
11775     free (flinfo->external_relocs);
11776   if (flinfo->internal_relocs != NULL)
11777     free (flinfo->internal_relocs);
11778   if (flinfo->external_syms != NULL)
11779     free (flinfo->external_syms);
11780   if (flinfo->locsym_shndx != NULL)
11781     free (flinfo->locsym_shndx);
11782   if (flinfo->internal_syms != NULL)
11783     free (flinfo->internal_syms);
11784   if (flinfo->indices != NULL)
11785     free (flinfo->indices);
11786   if (flinfo->sections != NULL)
11787     free (flinfo->sections);
11788   if (flinfo->symshndxbuf != NULL
11789       && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11790     free (flinfo->symshndxbuf);
11791   for (o = obfd->sections; o != NULL; o = o->next)
11792     {
11793       struct bfd_elf_section_data *esdo = elf_section_data (o);
11794       if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11795 	free (esdo->rel.hashes);
11796       if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11797 	free (esdo->rela.hashes);
11798     }
11799 }
11800 
11801 /* Do the final step of an ELF link.  */
11802 
11803 bfd_boolean
11804 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11805 {
11806   bfd_boolean dynamic;
11807   bfd_boolean emit_relocs;
11808   bfd *dynobj;
11809   struct elf_final_link_info flinfo;
11810   asection *o;
11811   struct bfd_link_order *p;
11812   bfd *sub;
11813   bfd_size_type max_contents_size;
11814   bfd_size_type max_external_reloc_size;
11815   bfd_size_type max_internal_reloc_count;
11816   bfd_size_type max_sym_count;
11817   bfd_size_type max_sym_shndx_count;
11818   Elf_Internal_Sym elfsym;
11819   unsigned int i;
11820   Elf_Internal_Shdr *symtab_hdr;
11821   Elf_Internal_Shdr *symtab_shndx_hdr;
11822   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11823   struct elf_outext_info eoinfo;
11824   bfd_boolean merged;
11825   size_t relativecount = 0;
11826   asection *reldyn = 0;
11827   bfd_size_type amt;
11828   asection *attr_section = NULL;
11829   bfd_vma attr_size = 0;
11830   const char *std_attrs_section;
11831   struct elf_link_hash_table *htab = elf_hash_table (info);
11832   bfd_boolean sections_removed;
11833 
11834   if (!is_elf_hash_table (htab))
11835     return FALSE;
11836 
11837   if (bfd_link_pic (info))
11838     abfd->flags |= DYNAMIC;
11839 
11840   dynamic = htab->dynamic_sections_created;
11841   dynobj = htab->dynobj;
11842 
11843   emit_relocs = (bfd_link_relocatable (info)
11844 		 || info->emitrelocations);
11845 
11846   flinfo.info = info;
11847   flinfo.output_bfd = abfd;
11848   flinfo.symstrtab = _bfd_elf_strtab_init ();
11849   if (flinfo.symstrtab == NULL)
11850     return FALSE;
11851 
11852   if (! dynamic)
11853     {
11854       flinfo.hash_sec = NULL;
11855       flinfo.symver_sec = NULL;
11856     }
11857   else
11858     {
11859       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11860       /* Note that dynsym_sec can be NULL (on VMS).  */
11861       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11862       /* Note that it is OK if symver_sec is NULL.  */
11863     }
11864 
11865   flinfo.contents = NULL;
11866   flinfo.external_relocs = NULL;
11867   flinfo.internal_relocs = NULL;
11868   flinfo.external_syms = NULL;
11869   flinfo.locsym_shndx = NULL;
11870   flinfo.internal_syms = NULL;
11871   flinfo.indices = NULL;
11872   flinfo.sections = NULL;
11873   flinfo.symshndxbuf = NULL;
11874   flinfo.filesym_count = 0;
11875 
11876   /* The object attributes have been merged.  Remove the input
11877      sections from the link, and set the contents of the output
11878      section.  */
11879   sections_removed = FALSE;
11880   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11881   for (o = abfd->sections; o != NULL; o = o->next)
11882     {
11883       bfd_boolean remove_section = FALSE;
11884 
11885       if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11886 	  || strcmp (o->name, ".gnu.attributes") == 0)
11887 	{
11888 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
11889 	    {
11890 	      asection *input_section;
11891 
11892 	      if (p->type != bfd_indirect_link_order)
11893 		continue;
11894 	      input_section = p->u.indirect.section;
11895 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
11896 		 elf_link_input_bfd ignores this section.  */
11897 	      input_section->flags &= ~SEC_HAS_CONTENTS;
11898 	    }
11899 
11900 	  attr_size = bfd_elf_obj_attr_size (abfd);
11901 	  bfd_set_section_size (o, attr_size);
11902 	  /* Skip this section later on.  */
11903 	  o->map_head.link_order = NULL;
11904 	  if (attr_size)
11905 	    attr_section = o;
11906 	  else
11907 	    remove_section = TRUE;
11908 	}
11909       else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11910 	{
11911 	  /* Remove empty group section from linker output.  */
11912 	  remove_section = TRUE;
11913 	}
11914       if (remove_section)
11915 	{
11916 	  o->flags |= SEC_EXCLUDE;
11917 	  bfd_section_list_remove (abfd, o);
11918 	  abfd->section_count--;
11919 	  sections_removed = TRUE;
11920 	}
11921     }
11922   if (sections_removed)
11923     _bfd_fix_excluded_sec_syms (abfd, info);
11924 
11925   /* Count up the number of relocations we will output for each output
11926      section, so that we know the sizes of the reloc sections.  We
11927      also figure out some maximum sizes.  */
11928   max_contents_size = 0;
11929   max_external_reloc_size = 0;
11930   max_internal_reloc_count = 0;
11931   max_sym_count = 0;
11932   max_sym_shndx_count = 0;
11933   merged = FALSE;
11934   for (o = abfd->sections; o != NULL; o = o->next)
11935     {
11936       struct bfd_elf_section_data *esdo = elf_section_data (o);
11937       o->reloc_count = 0;
11938 
11939       for (p = o->map_head.link_order; p != NULL; p = p->next)
11940 	{
11941 	  unsigned int reloc_count = 0;
11942 	  unsigned int additional_reloc_count = 0;
11943 	  struct bfd_elf_section_data *esdi = NULL;
11944 
11945 	  if (p->type == bfd_section_reloc_link_order
11946 	      || p->type == bfd_symbol_reloc_link_order)
11947 	    reloc_count = 1;
11948 	  else if (p->type == bfd_indirect_link_order)
11949 	    {
11950 	      asection *sec;
11951 
11952 	      sec = p->u.indirect.section;
11953 
11954 	      /* Mark all sections which are to be included in the
11955 		 link.  This will normally be every section.  We need
11956 		 to do this so that we can identify any sections which
11957 		 the linker has decided to not include.  */
11958 	      sec->linker_mark = TRUE;
11959 
11960 	      if (sec->flags & SEC_MERGE)
11961 		merged = TRUE;
11962 
11963 	      if (sec->rawsize > max_contents_size)
11964 		max_contents_size = sec->rawsize;
11965 	      if (sec->size > max_contents_size)
11966 		max_contents_size = sec->size;
11967 
11968 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11969 		  && (sec->owner->flags & DYNAMIC) == 0)
11970 		{
11971 		  size_t sym_count;
11972 
11973 		  /* We are interested in just local symbols, not all
11974 		     symbols.  */
11975 		  if (elf_bad_symtab (sec->owner))
11976 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11977 				 / bed->s->sizeof_sym);
11978 		  else
11979 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11980 
11981 		  if (sym_count > max_sym_count)
11982 		    max_sym_count = sym_count;
11983 
11984 		  if (sym_count > max_sym_shndx_count
11985 		      && elf_symtab_shndx_list (sec->owner) != NULL)
11986 		    max_sym_shndx_count = sym_count;
11987 
11988 		  if (esdo->this_hdr.sh_type == SHT_REL
11989 		      || esdo->this_hdr.sh_type == SHT_RELA)
11990 		    /* Some backends use reloc_count in relocation sections
11991 		       to count particular types of relocs.  Of course,
11992 		       reloc sections themselves can't have relocations.  */
11993 		    ;
11994 		  else if (emit_relocs)
11995 		    {
11996 		      reloc_count = sec->reloc_count;
11997 		      if (bed->elf_backend_count_additional_relocs)
11998 			{
11999 			  int c;
12000 			  c = (*bed->elf_backend_count_additional_relocs) (sec);
12001 			  additional_reloc_count += c;
12002 			}
12003 		    }
12004 		  else if (bed->elf_backend_count_relocs)
12005 		    reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12006 
12007 		  esdi = elf_section_data (sec);
12008 
12009 		  if ((sec->flags & SEC_RELOC) != 0)
12010 		    {
12011 		      size_t ext_size = 0;
12012 
12013 		      if (esdi->rel.hdr != NULL)
12014 			ext_size = esdi->rel.hdr->sh_size;
12015 		      if (esdi->rela.hdr != NULL)
12016 			ext_size += esdi->rela.hdr->sh_size;
12017 
12018 		      if (ext_size > max_external_reloc_size)
12019 			max_external_reloc_size = ext_size;
12020 		      if (sec->reloc_count > max_internal_reloc_count)
12021 			max_internal_reloc_count = sec->reloc_count;
12022 		    }
12023 		}
12024 	    }
12025 
12026 	  if (reloc_count == 0)
12027 	    continue;
12028 
12029 	  reloc_count += additional_reloc_count;
12030 	  o->reloc_count += reloc_count;
12031 
12032 	  if (p->type == bfd_indirect_link_order && emit_relocs)
12033 	    {
12034 	      if (esdi->rel.hdr)
12035 		{
12036 		  esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12037 		  esdo->rel.count += additional_reloc_count;
12038 		}
12039 	      if (esdi->rela.hdr)
12040 		{
12041 		  esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12042 		  esdo->rela.count += additional_reloc_count;
12043 		}
12044 	    }
12045 	  else
12046 	    {
12047 	      if (o->use_rela_p)
12048 		esdo->rela.count += reloc_count;
12049 	      else
12050 		esdo->rel.count += reloc_count;
12051 	    }
12052 	}
12053 
12054       if (o->reloc_count > 0)
12055 	o->flags |= SEC_RELOC;
12056       else
12057 	{
12058 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
12059 	     set it (this is probably a bug) and if it is set
12060 	     assign_section_numbers will create a reloc section.  */
12061 	  o->flags &=~ SEC_RELOC;
12062 	}
12063 
12064       /* If the SEC_ALLOC flag is not set, force the section VMA to
12065 	 zero.  This is done in elf_fake_sections as well, but forcing
12066 	 the VMA to 0 here will ensure that relocs against these
12067 	 sections are handled correctly.  */
12068       if ((o->flags & SEC_ALLOC) == 0
12069 	  && ! o->user_set_vma)
12070 	o->vma = 0;
12071     }
12072 
12073   if (! bfd_link_relocatable (info) && merged)
12074     elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12075 
12076   /* Figure out the file positions for everything but the symbol table
12077      and the relocs.  We set symcount to force assign_section_numbers
12078      to create a symbol table.  */
12079   abfd->symcount = info->strip != strip_all || emit_relocs;
12080   BFD_ASSERT (! abfd->output_has_begun);
12081   if (! _bfd_elf_compute_section_file_positions (abfd, info))
12082     goto error_return;
12083 
12084   /* Set sizes, and assign file positions for reloc sections.  */
12085   for (o = abfd->sections; o != NULL; o = o->next)
12086     {
12087       struct bfd_elf_section_data *esdo = elf_section_data (o);
12088       if ((o->flags & SEC_RELOC) != 0)
12089 	{
12090 	  if (esdo->rel.hdr
12091 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12092 	    goto error_return;
12093 
12094 	  if (esdo->rela.hdr
12095 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12096 	    goto error_return;
12097 	}
12098 
12099       /* _bfd_elf_compute_section_file_positions makes temporary use
12100 	 of target_index.  Reset it.  */
12101       o->target_index = 0;
12102 
12103       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12104 	 to count upwards while actually outputting the relocations.  */
12105       esdo->rel.count = 0;
12106       esdo->rela.count = 0;
12107 
12108       if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12109 	  && !bfd_section_is_ctf (o))
12110 	{
12111 	  /* Cache the section contents so that they can be compressed
12112 	     later.  Use bfd_malloc since it will be freed by
12113 	     bfd_compress_section_contents.  */
12114 	  unsigned char *contents = esdo->this_hdr.contents;
12115 	  if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12116 	    abort ();
12117 	  contents
12118 	    = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12119 	  if (contents == NULL)
12120 	    goto error_return;
12121 	  esdo->this_hdr.contents = contents;
12122 	}
12123     }
12124 
12125   /* We have now assigned file positions for all the sections except .symtab,
12126      .strtab, and non-loaded reloc and compressed debugging sections.  We start
12127      the .symtab section at the current file position, and write directly to it.
12128      We build the .strtab section in memory.  */
12129   abfd->symcount = 0;
12130   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12131   /* sh_name is set in prep_headers.  */
12132   symtab_hdr->sh_type = SHT_SYMTAB;
12133   /* sh_flags, sh_addr and sh_size all start off zero.  */
12134   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12135   /* sh_link is set in assign_section_numbers.  */
12136   /* sh_info is set below.  */
12137   /* sh_offset is set just below.  */
12138   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12139 
12140   if (max_sym_count < 20)
12141     max_sym_count = 20;
12142   htab->strtabsize = max_sym_count;
12143   amt = max_sym_count * sizeof (struct elf_sym_strtab);
12144   htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12145   if (htab->strtab == NULL)
12146     goto error_return;
12147   /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
12148   flinfo.symshndxbuf
12149     = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12150        ? (Elf_External_Sym_Shndx *) -1 : NULL);
12151 
12152   if (info->strip != strip_all || emit_relocs)
12153     {
12154       file_ptr off = elf_next_file_pos (abfd);
12155 
12156       _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12157 
12158       /* Note that at this point elf_next_file_pos (abfd) is
12159 	 incorrect.  We do not yet know the size of the .symtab section.
12160 	 We correct next_file_pos below, after we do know the size.  */
12161 
12162       /* Start writing out the symbol table.  The first symbol is always a
12163 	 dummy symbol.  */
12164       elfsym.st_value = 0;
12165       elfsym.st_size = 0;
12166       elfsym.st_info = 0;
12167       elfsym.st_other = 0;
12168       elfsym.st_shndx = SHN_UNDEF;
12169       elfsym.st_target_internal = 0;
12170       if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12171 				     bfd_und_section_ptr, NULL) != 1)
12172 	goto error_return;
12173 
12174       /* Output a symbol for each section.  We output these even if we are
12175 	 discarding local symbols, since they are used for relocs.  These
12176 	 symbols have no names.  We store the index of each one in the
12177 	 index field of the section, so that we can find it again when
12178 	 outputting relocs.  */
12179 
12180       elfsym.st_size = 0;
12181       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12182       elfsym.st_other = 0;
12183       elfsym.st_value = 0;
12184       elfsym.st_target_internal = 0;
12185       for (i = 1; i < elf_numsections (abfd); i++)
12186 	{
12187 	  o = bfd_section_from_elf_index (abfd, i);
12188 	  if (o != NULL)
12189 	    {
12190 	      o->target_index = bfd_get_symcount (abfd);
12191 	      elfsym.st_shndx = i;
12192 	      if (!bfd_link_relocatable (info))
12193 		elfsym.st_value = o->vma;
12194 	      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12195 					     NULL) != 1)
12196 		goto error_return;
12197 	    }
12198 	}
12199     }
12200 
12201   /* Allocate some memory to hold information read in from the input
12202      files.  */
12203   if (max_contents_size != 0)
12204     {
12205       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12206       if (flinfo.contents == NULL)
12207 	goto error_return;
12208     }
12209 
12210   if (max_external_reloc_size != 0)
12211     {
12212       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12213       if (flinfo.external_relocs == NULL)
12214 	goto error_return;
12215     }
12216 
12217   if (max_internal_reloc_count != 0)
12218     {
12219       amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12220       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12221       if (flinfo.internal_relocs == NULL)
12222 	goto error_return;
12223     }
12224 
12225   if (max_sym_count != 0)
12226     {
12227       amt = max_sym_count * bed->s->sizeof_sym;
12228       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12229       if (flinfo.external_syms == NULL)
12230 	goto error_return;
12231 
12232       amt = max_sym_count * sizeof (Elf_Internal_Sym);
12233       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12234       if (flinfo.internal_syms == NULL)
12235 	goto error_return;
12236 
12237       amt = max_sym_count * sizeof (long);
12238       flinfo.indices = (long int *) bfd_malloc (amt);
12239       if (flinfo.indices == NULL)
12240 	goto error_return;
12241 
12242       amt = max_sym_count * sizeof (asection *);
12243       flinfo.sections = (asection **) bfd_malloc (amt);
12244       if (flinfo.sections == NULL)
12245 	goto error_return;
12246     }
12247 
12248   if (max_sym_shndx_count != 0)
12249     {
12250       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12251       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12252       if (flinfo.locsym_shndx == NULL)
12253 	goto error_return;
12254     }
12255 
12256   if (htab->tls_sec)
12257     {
12258       bfd_vma base, end = 0;
12259       asection *sec;
12260 
12261       for (sec = htab->tls_sec;
12262 	   sec && (sec->flags & SEC_THREAD_LOCAL);
12263 	   sec = sec->next)
12264 	{
12265 	  bfd_size_type size = sec->size;
12266 
12267 	  if (size == 0
12268 	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
12269 	    {
12270 	      struct bfd_link_order *ord = sec->map_tail.link_order;
12271 
12272 	      if (ord != NULL)
12273 		size = ord->offset + ord->size;
12274 	    }
12275 	  end = sec->vma + size;
12276 	}
12277       base = htab->tls_sec->vma;
12278       /* Only align end of TLS section if static TLS doesn't have special
12279 	 alignment requirements.  */
12280       if (bed->static_tls_alignment == 1)
12281 	end = align_power (end, htab->tls_sec->alignment_power);
12282       htab->tls_size = end - base;
12283     }
12284 
12285   /* Reorder SHF_LINK_ORDER sections.  */
12286   for (o = abfd->sections; o != NULL; o = o->next)
12287     {
12288       if (!elf_fixup_link_order (abfd, o))
12289 	return FALSE;
12290     }
12291 
12292   if (!_bfd_elf_fixup_eh_frame_hdr (info))
12293     return FALSE;
12294 
12295   /* Since ELF permits relocations to be against local symbols, we
12296      must have the local symbols available when we do the relocations.
12297      Since we would rather only read the local symbols once, and we
12298      would rather not keep them in memory, we handle all the
12299      relocations for a single input file at the same time.
12300 
12301      Unfortunately, there is no way to know the total number of local
12302      symbols until we have seen all of them, and the local symbol
12303      indices precede the global symbol indices.  This means that when
12304      we are generating relocatable output, and we see a reloc against
12305      a global symbol, we can not know the symbol index until we have
12306      finished examining all the local symbols to see which ones we are
12307      going to output.  To deal with this, we keep the relocations in
12308      memory, and don't output them until the end of the link.  This is
12309      an unfortunate waste of memory, but I don't see a good way around
12310      it.  Fortunately, it only happens when performing a relocatable
12311      link, which is not the common case.  FIXME: If keep_memory is set
12312      we could write the relocs out and then read them again; I don't
12313      know how bad the memory loss will be.  */
12314 
12315   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12316     sub->output_has_begun = FALSE;
12317   for (o = abfd->sections; o != NULL; o = o->next)
12318     {
12319       for (p = o->map_head.link_order; p != NULL; p = p->next)
12320 	{
12321 	  if (p->type == bfd_indirect_link_order
12322 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12323 		  == bfd_target_elf_flavour)
12324 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12325 	    {
12326 	      if (! sub->output_has_begun)
12327 		{
12328 		  if (! elf_link_input_bfd (&flinfo, sub))
12329 		    goto error_return;
12330 		  sub->output_has_begun = TRUE;
12331 		}
12332 	    }
12333 	  else if (p->type == bfd_section_reloc_link_order
12334 		   || p->type == bfd_symbol_reloc_link_order)
12335 	    {
12336 	      if (! elf_reloc_link_order (abfd, info, o, p))
12337 		goto error_return;
12338 	    }
12339 	  else
12340 	    {
12341 	      if (! _bfd_default_link_order (abfd, info, o, p))
12342 		{
12343 		  if (p->type == bfd_indirect_link_order
12344 		      && (bfd_get_flavour (sub)
12345 			  == bfd_target_elf_flavour)
12346 		      && (elf_elfheader (sub)->e_ident[EI_CLASS]
12347 			  != bed->s->elfclass))
12348 		    {
12349 		      const char *iclass, *oclass;
12350 
12351 		      switch (bed->s->elfclass)
12352 			{
12353 			case ELFCLASS64: oclass = "ELFCLASS64"; break;
12354 			case ELFCLASS32: oclass = "ELFCLASS32"; break;
12355 			case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12356 			default: abort ();
12357 			}
12358 
12359 		      switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12360 			{
12361 			case ELFCLASS64: iclass = "ELFCLASS64"; break;
12362 			case ELFCLASS32: iclass = "ELFCLASS32"; break;
12363 			case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12364 			default: abort ();
12365 			}
12366 
12367 		      bfd_set_error (bfd_error_wrong_format);
12368 		      _bfd_error_handler
12369 			/* xgettext:c-format */
12370 			(_("%pB: file class %s incompatible with %s"),
12371 			 sub, iclass, oclass);
12372 		    }
12373 
12374 		  goto error_return;
12375 		}
12376 	    }
12377 	}
12378     }
12379 
12380   /* Free symbol buffer if needed.  */
12381   if (!info->reduce_memory_overheads)
12382     {
12383       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12384 	if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12385 	    && elf_tdata (sub)->symbuf)
12386 	  {
12387 	    free (elf_tdata (sub)->symbuf);
12388 	    elf_tdata (sub)->symbuf = NULL;
12389 	  }
12390     }
12391 
12392   /* Output any global symbols that got converted to local in a
12393      version script or due to symbol visibility.  We do this in a
12394      separate step since ELF requires all local symbols to appear
12395      prior to any global symbols.  FIXME: We should only do this if
12396      some global symbols were, in fact, converted to become local.
12397      FIXME: Will this work correctly with the Irix 5 linker?  */
12398   eoinfo.failed = FALSE;
12399   eoinfo.flinfo = &flinfo;
12400   eoinfo.localsyms = TRUE;
12401   eoinfo.file_sym_done = FALSE;
12402   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12403   if (eoinfo.failed)
12404     return FALSE;
12405 
12406   /* If backend needs to output some local symbols not present in the hash
12407      table, do it now.  */
12408   if (bed->elf_backend_output_arch_local_syms
12409       && (info->strip != strip_all || emit_relocs))
12410     {
12411       typedef int (*out_sym_func)
12412 	(void *, const char *, Elf_Internal_Sym *, asection *,
12413 	 struct elf_link_hash_entry *);
12414 
12415       if (! ((*bed->elf_backend_output_arch_local_syms)
12416 	     (abfd, info, &flinfo,
12417 	      (out_sym_func) elf_link_output_symstrtab)))
12418 	return FALSE;
12419     }
12420 
12421   /* That wrote out all the local symbols.  Finish up the symbol table
12422      with the global symbols. Even if we want to strip everything we
12423      can, we still need to deal with those global symbols that got
12424      converted to local in a version script.  */
12425 
12426   /* The sh_info field records the index of the first non local symbol.  */
12427   symtab_hdr->sh_info = bfd_get_symcount (abfd);
12428 
12429   if (dynamic
12430       && htab->dynsym != NULL
12431       && htab->dynsym->output_section != bfd_abs_section_ptr)
12432     {
12433       Elf_Internal_Sym sym;
12434       bfd_byte *dynsym = htab->dynsym->contents;
12435 
12436       o = htab->dynsym->output_section;
12437       elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12438 
12439       /* Write out the section symbols for the output sections.  */
12440       if (bfd_link_pic (info)
12441 	  || htab->is_relocatable_executable)
12442 	{
12443 	  asection *s;
12444 
12445 	  sym.st_size = 0;
12446 	  sym.st_name = 0;
12447 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12448 	  sym.st_other = 0;
12449 	  sym.st_target_internal = 0;
12450 
12451 	  for (s = abfd->sections; s != NULL; s = s->next)
12452 	    {
12453 	      int indx;
12454 	      bfd_byte *dest;
12455 	      long dynindx;
12456 
12457 	      dynindx = elf_section_data (s)->dynindx;
12458 	      if (dynindx <= 0)
12459 		continue;
12460 	      indx = elf_section_data (s)->this_idx;
12461 	      BFD_ASSERT (indx > 0);
12462 	      sym.st_shndx = indx;
12463 	      if (! check_dynsym (abfd, &sym))
12464 		return FALSE;
12465 	      sym.st_value = s->vma;
12466 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
12467 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12468 	    }
12469 	}
12470 
12471       /* Write out the local dynsyms.  */
12472       if (htab->dynlocal)
12473 	{
12474 	  struct elf_link_local_dynamic_entry *e;
12475 	  for (e = htab->dynlocal; e ; e = e->next)
12476 	    {
12477 	      asection *s;
12478 	      bfd_byte *dest;
12479 
12480 	      /* Copy the internal symbol and turn off visibility.
12481 		 Note that we saved a word of storage and overwrote
12482 		 the original st_name with the dynstr_index.  */
12483 	      sym = e->isym;
12484 	      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12485 
12486 	      s = bfd_section_from_elf_index (e->input_bfd,
12487 					      e->isym.st_shndx);
12488 	      if (s != NULL)
12489 		{
12490 		  sym.st_shndx =
12491 		    elf_section_data (s->output_section)->this_idx;
12492 		  if (! check_dynsym (abfd, &sym))
12493 		    return FALSE;
12494 		  sym.st_value = (s->output_section->vma
12495 				  + s->output_offset
12496 				  + e->isym.st_value);
12497 		}
12498 
12499 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12500 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12501 	    }
12502 	}
12503     }
12504 
12505   /* We get the global symbols from the hash table.  */
12506   eoinfo.failed = FALSE;
12507   eoinfo.localsyms = FALSE;
12508   eoinfo.flinfo = &flinfo;
12509   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12510   if (eoinfo.failed)
12511     return FALSE;
12512 
12513   /* If backend needs to output some symbols not present in the hash
12514      table, do it now.  */
12515   if (bed->elf_backend_output_arch_syms
12516       && (info->strip != strip_all || emit_relocs))
12517     {
12518       typedef int (*out_sym_func)
12519 	(void *, const char *, Elf_Internal_Sym *, asection *,
12520 	 struct elf_link_hash_entry *);
12521 
12522       if (! ((*bed->elf_backend_output_arch_syms)
12523 	     (abfd, info, &flinfo,
12524 	      (out_sym_func) elf_link_output_symstrtab)))
12525 	return FALSE;
12526     }
12527 
12528   /* Finalize the .strtab section.  */
12529   _bfd_elf_strtab_finalize (flinfo.symstrtab);
12530 
12531   /* Swap out the .strtab section. */
12532   if (!elf_link_swap_symbols_out (&flinfo))
12533     return FALSE;
12534 
12535   /* Now we know the size of the symtab section.  */
12536   if (bfd_get_symcount (abfd) > 0)
12537     {
12538       /* Finish up and write out the symbol string table (.strtab)
12539 	 section.  */
12540       Elf_Internal_Shdr *symstrtab_hdr = NULL;
12541       file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12542 
12543       if (elf_symtab_shndx_list (abfd))
12544 	{
12545 	  symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12546 
12547 	  if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12548 	    {
12549 	      symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12550 	      symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12551 	      symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12552 	      amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12553 	      symtab_shndx_hdr->sh_size = amt;
12554 
12555 	      off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12556 							       off, TRUE);
12557 
12558 	      if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12559 		  || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12560 		return FALSE;
12561 	    }
12562 	}
12563 
12564       symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12565       /* sh_name was set in prep_headers.  */
12566       symstrtab_hdr->sh_type = SHT_STRTAB;
12567       symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12568       symstrtab_hdr->sh_addr = 0;
12569       symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12570       symstrtab_hdr->sh_entsize = 0;
12571       symstrtab_hdr->sh_link = 0;
12572       symstrtab_hdr->sh_info = 0;
12573       /* sh_offset is set just below.  */
12574       symstrtab_hdr->sh_addralign = 1;
12575 
12576       off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12577 						       off, TRUE);
12578       elf_next_file_pos (abfd) = off;
12579 
12580       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12581 	  || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12582 	return FALSE;
12583     }
12584 
12585   if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12586     {
12587       _bfd_error_handler (_("%pB: failed to generate import library"),
12588 			  info->out_implib_bfd);
12589       return FALSE;
12590     }
12591 
12592   /* Adjust the relocs to have the correct symbol indices.  */
12593   for (o = abfd->sections; o != NULL; o = o->next)
12594     {
12595       struct bfd_elf_section_data *esdo = elf_section_data (o);
12596       bfd_boolean sort;
12597 
12598       if ((o->flags & SEC_RELOC) == 0)
12599 	continue;
12600 
12601       sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12602       if (esdo->rel.hdr != NULL
12603 	  && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12604 	return FALSE;
12605       if (esdo->rela.hdr != NULL
12606 	  && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12607 	return FALSE;
12608 
12609       /* Set the reloc_count field to 0 to prevent write_relocs from
12610 	 trying to swap the relocs out itself.  */
12611       o->reloc_count = 0;
12612     }
12613 
12614   if (dynamic && info->combreloc && dynobj != NULL)
12615     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12616 
12617   /* If we are linking against a dynamic object, or generating a
12618      shared library, finish up the dynamic linking information.  */
12619   if (dynamic)
12620     {
12621       bfd_byte *dyncon, *dynconend;
12622 
12623       /* Fix up .dynamic entries.  */
12624       o = bfd_get_linker_section (dynobj, ".dynamic");
12625       BFD_ASSERT (o != NULL);
12626 
12627       dyncon = o->contents;
12628       dynconend = o->contents + o->size;
12629       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12630 	{
12631 	  Elf_Internal_Dyn dyn;
12632 	  const char *name;
12633 	  unsigned int type;
12634 	  bfd_size_type sh_size;
12635 	  bfd_vma sh_addr;
12636 
12637 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12638 
12639 	  switch (dyn.d_tag)
12640 	    {
12641 	    default:
12642 	      continue;
12643 	    case DT_NULL:
12644 	      if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12645 		{
12646 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
12647 		    {
12648 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12649 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12650 		    default: continue;
12651 		    }
12652 		  dyn.d_un.d_val = relativecount;
12653 		  relativecount = 0;
12654 		  break;
12655 		}
12656 	      continue;
12657 
12658 	    case DT_INIT:
12659 	      name = info->init_function;
12660 	      goto get_sym;
12661 	    case DT_FINI:
12662 	      name = info->fini_function;
12663 	    get_sym:
12664 	      {
12665 		struct elf_link_hash_entry *h;
12666 
12667 		h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12668 		if (h != NULL
12669 		    && (h->root.type == bfd_link_hash_defined
12670 			|| h->root.type == bfd_link_hash_defweak))
12671 		  {
12672 		    dyn.d_un.d_ptr = h->root.u.def.value;
12673 		    o = h->root.u.def.section;
12674 		    if (o->output_section != NULL)
12675 		      dyn.d_un.d_ptr += (o->output_section->vma
12676 					 + o->output_offset);
12677 		    else
12678 		      {
12679 			/* The symbol is imported from another shared
12680 			   library and does not apply to this one.  */
12681 			dyn.d_un.d_ptr = 0;
12682 		      }
12683 		    break;
12684 		  }
12685 	      }
12686 	      continue;
12687 
12688 	    case DT_PREINIT_ARRAYSZ:
12689 	      name = ".preinit_array";
12690 	      goto get_out_size;
12691 	    case DT_INIT_ARRAYSZ:
12692 	      name = ".init_array";
12693 	      goto get_out_size;
12694 	    case DT_FINI_ARRAYSZ:
12695 	      name = ".fini_array";
12696 	    get_out_size:
12697 	      o = bfd_get_section_by_name (abfd, name);
12698 	      if (o == NULL)
12699 		{
12700 		  _bfd_error_handler
12701 		    (_("could not find section %s"), name);
12702 		  goto error_return;
12703 		}
12704 	      if (o->size == 0)
12705 		_bfd_error_handler
12706 		  (_("warning: %s section has zero size"), name);
12707 	      dyn.d_un.d_val = o->size;
12708 	      break;
12709 
12710 	    case DT_PREINIT_ARRAY:
12711 	      name = ".preinit_array";
12712 	      goto get_out_vma;
12713 	    case DT_INIT_ARRAY:
12714 	      name = ".init_array";
12715 	      goto get_out_vma;
12716 	    case DT_FINI_ARRAY:
12717 	      name = ".fini_array";
12718 	    get_out_vma:
12719 	      o = bfd_get_section_by_name (abfd, name);
12720 	      goto do_vma;
12721 
12722 	    case DT_HASH:
12723 	      name = ".hash";
12724 	      goto get_vma;
12725 	    case DT_GNU_HASH:
12726 	      name = ".gnu.hash";
12727 	      goto get_vma;
12728 	    case DT_STRTAB:
12729 	      name = ".dynstr";
12730 	      goto get_vma;
12731 	    case DT_SYMTAB:
12732 	      name = ".dynsym";
12733 	      goto get_vma;
12734 	    case DT_VERDEF:
12735 	      name = ".gnu.version_d";
12736 	      goto get_vma;
12737 	    case DT_VERNEED:
12738 	      name = ".gnu.version_r";
12739 	      goto get_vma;
12740 	    case DT_VERSYM:
12741 	      name = ".gnu.version";
12742 	    get_vma:
12743 	      o = bfd_get_linker_section (dynobj, name);
12744 	    do_vma:
12745 	      if (o == NULL || bfd_is_abs_section (o->output_section))
12746 		{
12747 		  _bfd_error_handler
12748 		    (_("could not find section %s"), name);
12749 		  goto error_return;
12750 		}
12751 	      if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12752 		{
12753 		  _bfd_error_handler
12754 		    (_("warning: section '%s' is being made into a note"), name);
12755 		  bfd_set_error (bfd_error_nonrepresentable_section);
12756 		  goto error_return;
12757 		}
12758 	      dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12759 	      break;
12760 
12761 	    case DT_REL:
12762 	    case DT_RELA:
12763 	    case DT_RELSZ:
12764 	    case DT_RELASZ:
12765 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12766 		type = SHT_REL;
12767 	      else
12768 		type = SHT_RELA;
12769 	      sh_size = 0;
12770 	      sh_addr = 0;
12771 	      for (i = 1; i < elf_numsections (abfd); i++)
12772 		{
12773 		  Elf_Internal_Shdr *hdr;
12774 
12775 		  hdr = elf_elfsections (abfd)[i];
12776 		  if (hdr->sh_type == type
12777 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
12778 		    {
12779 		      sh_size += hdr->sh_size;
12780 		      if (sh_addr == 0
12781 			  || sh_addr > hdr->sh_addr)
12782 			sh_addr = hdr->sh_addr;
12783 		    }
12784 		}
12785 
12786 	      if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12787 		{
12788 		  /* Don't count procedure linkage table relocs in the
12789 		     overall reloc count.  */
12790 		  sh_size -= htab->srelplt->size;
12791 		  if (sh_size == 0)
12792 		    /* If the size is zero, make the address zero too.
12793 		       This is to avoid a glibc bug.  If the backend
12794 		       emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12795 		       zero, then we'll put DT_RELA at the end of
12796 		       DT_JMPREL.  glibc will interpret the end of
12797 		       DT_RELA matching the end of DT_JMPREL as the
12798 		       case where DT_RELA includes DT_JMPREL, and for
12799 		       LD_BIND_NOW will decide that processing DT_RELA
12800 		       will process the PLT relocs too.  Net result:
12801 		       No PLT relocs applied.  */
12802 		    sh_addr = 0;
12803 
12804 		  /* If .rela.plt is the first .rela section, exclude
12805 		     it from DT_RELA.  */
12806 		  else if (sh_addr == (htab->srelplt->output_section->vma
12807 				       + htab->srelplt->output_offset))
12808 		    sh_addr += htab->srelplt->size;
12809 		}
12810 
12811 	      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12812 		dyn.d_un.d_val = sh_size;
12813 	      else
12814 		dyn.d_un.d_ptr = sh_addr;
12815 	      break;
12816 	    }
12817 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12818 	}
12819     }
12820 
12821   /* If we have created any dynamic sections, then output them.  */
12822   if (dynobj != NULL)
12823     {
12824       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12825 	goto error_return;
12826 
12827       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
12828       if (((info->warn_shared_textrel && bfd_link_pic (info))
12829 	   || info->error_textrel)
12830 	  && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12831 	{
12832 	  bfd_byte *dyncon, *dynconend;
12833 
12834 	  dyncon = o->contents;
12835 	  dynconend = o->contents + o->size;
12836 	  for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12837 	    {
12838 	      Elf_Internal_Dyn dyn;
12839 
12840 	      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12841 
12842 	      if (dyn.d_tag == DT_TEXTREL)
12843 		{
12844 		  if (info->error_textrel)
12845 		    info->callbacks->einfo
12846 		      (_("%P%X: read-only segment has dynamic relocations\n"));
12847 		  else
12848 		    info->callbacks->einfo
12849 		      (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12850 		  break;
12851 		}
12852 	    }
12853 	}
12854 
12855       for (o = dynobj->sections; o != NULL; o = o->next)
12856 	{
12857 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
12858 	      || o->size == 0
12859 	      || o->output_section == bfd_abs_section_ptr)
12860 	    continue;
12861 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
12862 	    {
12863 	      /* At this point, we are only interested in sections
12864 		 created by _bfd_elf_link_create_dynamic_sections.  */
12865 	      continue;
12866 	    }
12867 	  if (htab->stab_info.stabstr == o)
12868 	    continue;
12869 	  if (htab->eh_info.hdr_sec == o)
12870 	    continue;
12871 	  if (strcmp (o->name, ".dynstr") != 0)
12872 	    {
12873 	      bfd_size_type octets = ((file_ptr) o->output_offset
12874 				      * bfd_octets_per_byte (abfd, o));
12875 	      if (!bfd_set_section_contents (abfd, o->output_section,
12876 					     o->contents, octets, o->size))
12877 		goto error_return;
12878 	    }
12879 	  else
12880 	    {
12881 	      /* The contents of the .dynstr section are actually in a
12882 		 stringtab.  */
12883 	      file_ptr off;
12884 
12885 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12886 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
12887 		  || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12888 		goto error_return;
12889 	    }
12890 	}
12891     }
12892 
12893   if (!info->resolve_section_groups)
12894     {
12895       bfd_boolean failed = FALSE;
12896 
12897       BFD_ASSERT (bfd_link_relocatable (info));
12898       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12899       if (failed)
12900 	goto error_return;
12901     }
12902 
12903   /* If we have optimized stabs strings, output them.  */
12904   if (htab->stab_info.stabstr != NULL)
12905     {
12906       if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12907 	goto error_return;
12908     }
12909 
12910   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12911     goto error_return;
12912 
12913   if (info->callbacks->emit_ctf)
12914       info->callbacks->emit_ctf ();
12915 
12916   elf_final_link_free (abfd, &flinfo);
12917 
12918   if (attr_section)
12919     {
12920       bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12921       if (contents == NULL)
12922 	return FALSE;	/* Bail out and fail.  */
12923       bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12924       bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12925       free (contents);
12926     }
12927 
12928   return TRUE;
12929 
12930  error_return:
12931   elf_final_link_free (abfd, &flinfo);
12932   return FALSE;
12933 }
12934 
12935 /* Initialize COOKIE for input bfd ABFD.  */
12936 
12937 static bfd_boolean
12938 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12939 		   struct bfd_link_info *info, bfd *abfd)
12940 {
12941   Elf_Internal_Shdr *symtab_hdr;
12942   const struct elf_backend_data *bed;
12943 
12944   bed = get_elf_backend_data (abfd);
12945   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12946 
12947   cookie->abfd = abfd;
12948   cookie->sym_hashes = elf_sym_hashes (abfd);
12949   cookie->bad_symtab = elf_bad_symtab (abfd);
12950   if (cookie->bad_symtab)
12951     {
12952       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12953       cookie->extsymoff = 0;
12954     }
12955   else
12956     {
12957       cookie->locsymcount = symtab_hdr->sh_info;
12958       cookie->extsymoff = symtab_hdr->sh_info;
12959     }
12960 
12961   if (bed->s->arch_size == 32)
12962     cookie->r_sym_shift = 8;
12963   else
12964     cookie->r_sym_shift = 32;
12965 
12966   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12967   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12968     {
12969       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12970 					      cookie->locsymcount, 0,
12971 					      NULL, NULL, NULL);
12972       if (cookie->locsyms == NULL)
12973 	{
12974 	  info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12975 	  return FALSE;
12976 	}
12977       if (info->keep_memory)
12978 	symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12979     }
12980   return TRUE;
12981 }
12982 
12983 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
12984 
12985 static void
12986 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12987 {
12988   Elf_Internal_Shdr *symtab_hdr;
12989 
12990   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12991   if (cookie->locsyms != NULL
12992       && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12993     free (cookie->locsyms);
12994 }
12995 
12996 /* Initialize the relocation information in COOKIE for input section SEC
12997    of input bfd ABFD.  */
12998 
12999 static bfd_boolean
13000 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13001 			struct bfd_link_info *info, bfd *abfd,
13002 			asection *sec)
13003 {
13004   if (sec->reloc_count == 0)
13005     {
13006       cookie->rels = NULL;
13007       cookie->relend = NULL;
13008     }
13009   else
13010     {
13011       cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13012 						info->keep_memory);
13013       if (cookie->rels == NULL)
13014 	return FALSE;
13015       cookie->rel = cookie->rels;
13016       cookie->relend = cookie->rels + sec->reloc_count;
13017     }
13018   cookie->rel = cookie->rels;
13019   return TRUE;
13020 }
13021 
13022 /* Free the memory allocated by init_reloc_cookie_rels,
13023    if appropriate.  */
13024 
13025 static void
13026 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13027 			asection *sec)
13028 {
13029   if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
13030     free (cookie->rels);
13031 }
13032 
13033 /* Initialize the whole of COOKIE for input section SEC.  */
13034 
13035 static bfd_boolean
13036 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13037 			       struct bfd_link_info *info,
13038 			       asection *sec)
13039 {
13040   if (!init_reloc_cookie (cookie, info, sec->owner))
13041     goto error1;
13042   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13043     goto error2;
13044   return TRUE;
13045 
13046  error2:
13047   fini_reloc_cookie (cookie, sec->owner);
13048  error1:
13049   return FALSE;
13050 }
13051 
13052 /* Free the memory allocated by init_reloc_cookie_for_section,
13053    if appropriate.  */
13054 
13055 static void
13056 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13057 			       asection *sec)
13058 {
13059   fini_reloc_cookie_rels (cookie, sec);
13060   fini_reloc_cookie (cookie, sec->owner);
13061 }
13062 
13063 /* Garbage collect unused sections.  */
13064 
13065 /* Default gc_mark_hook.  */
13066 
13067 asection *
13068 _bfd_elf_gc_mark_hook (asection *sec,
13069 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
13070 		       Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13071 		       struct elf_link_hash_entry *h,
13072 		       Elf_Internal_Sym *sym)
13073 {
13074   if (h != NULL)
13075     {
13076       switch (h->root.type)
13077 	{
13078 	case bfd_link_hash_defined:
13079 	case bfd_link_hash_defweak:
13080 	  return h->root.u.def.section;
13081 
13082 	case bfd_link_hash_common:
13083 	  return h->root.u.c.p->section;
13084 
13085 	default:
13086 	  break;
13087 	}
13088     }
13089   else
13090     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13091 
13092   return NULL;
13093 }
13094 
13095 /* Return the debug definition section.  */
13096 
13097 static asection *
13098 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13099 			   struct bfd_link_info *info ATTRIBUTE_UNUSED,
13100 			   Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13101 			   struct elf_link_hash_entry *h,
13102 			   Elf_Internal_Sym *sym)
13103 {
13104   if (h != NULL)
13105     {
13106       /* Return the global debug definition section.  */
13107       if ((h->root.type == bfd_link_hash_defined
13108 	   || h->root.type == bfd_link_hash_defweak)
13109 	  && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13110 	return h->root.u.def.section;
13111     }
13112   else
13113     {
13114       /* Return the local debug definition section.  */
13115       asection *isec = bfd_section_from_elf_index (sec->owner,
13116 						   sym->st_shndx);
13117       if ((isec->flags & SEC_DEBUGGING) != 0)
13118 	return isec;
13119     }
13120 
13121   return NULL;
13122 }
13123 
13124 /* COOKIE->rel describes a relocation against section SEC, which is
13125    a section we've decided to keep.  Return the section that contains
13126    the relocation symbol, or NULL if no section contains it.  */
13127 
13128 asection *
13129 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13130 		       elf_gc_mark_hook_fn gc_mark_hook,
13131 		       struct elf_reloc_cookie *cookie,
13132 		       bfd_boolean *start_stop)
13133 {
13134   unsigned long r_symndx;
13135   struct elf_link_hash_entry *h, *hw;
13136 
13137   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13138   if (r_symndx == STN_UNDEF)
13139     return NULL;
13140 
13141   if (r_symndx >= cookie->locsymcount
13142       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13143     {
13144       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13145       if (h == NULL)
13146 	{
13147 	  info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13148 				  sec->owner);
13149 	  return NULL;
13150 	}
13151       while (h->root.type == bfd_link_hash_indirect
13152 	     || h->root.type == bfd_link_hash_warning)
13153 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
13154       h->mark = 1;
13155       /* Keep all aliases of the symbol too.  If an object symbol
13156 	 needs to be copied into .dynbss then all of its aliases
13157 	 should be present as dynamic symbols, not just the one used
13158 	 on the copy relocation.  */
13159       hw = h;
13160       while (hw->is_weakalias)
13161 	{
13162 	  hw = hw->u.alias;
13163 	  hw->mark = 1;
13164 	}
13165 
13166       if (start_stop != NULL)
13167 	{
13168 	  /* To work around a glibc bug, mark XXX input sections
13169 	     when there is a reference to __start_XXX or __stop_XXX
13170 	     symbols.  */
13171 	  if (h->start_stop)
13172 	    {
13173 	      asection *s = h->u2.start_stop_section;
13174 	      *start_stop = !s->gc_mark;
13175 	      return s;
13176 	    }
13177 	}
13178 
13179       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13180     }
13181 
13182   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13183 			  &cookie->locsyms[r_symndx]);
13184 }
13185 
13186 /* COOKIE->rel describes a relocation against section SEC, which is
13187    a section we've decided to keep.  Mark the section that contains
13188    the relocation symbol.  */
13189 
13190 bfd_boolean
13191 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13192 			asection *sec,
13193 			elf_gc_mark_hook_fn gc_mark_hook,
13194 			struct elf_reloc_cookie *cookie)
13195 {
13196   asection *rsec;
13197   bfd_boolean start_stop = FALSE;
13198 
13199   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13200   while (rsec != NULL)
13201     {
13202       if (!rsec->gc_mark)
13203 	{
13204 	  if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13205 	      || (rsec->owner->flags & DYNAMIC) != 0)
13206 	    rsec->gc_mark = 1;
13207 	  else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13208 	    return FALSE;
13209 	}
13210       if (!start_stop)
13211 	break;
13212       rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13213     }
13214   return TRUE;
13215 }
13216 
13217 /* The mark phase of garbage collection.  For a given section, mark
13218    it and any sections in this section's group, and all the sections
13219    which define symbols to which it refers.  */
13220 
13221 bfd_boolean
13222 _bfd_elf_gc_mark (struct bfd_link_info *info,
13223 		  asection *sec,
13224 		  elf_gc_mark_hook_fn gc_mark_hook)
13225 {
13226   bfd_boolean ret;
13227   asection *group_sec, *eh_frame;
13228 
13229   sec->gc_mark = 1;
13230 
13231   /* Mark all the sections in the group.  */
13232   group_sec = elf_section_data (sec)->next_in_group;
13233   if (group_sec && !group_sec->gc_mark)
13234     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13235       return FALSE;
13236 
13237   /* Look through the section relocs.  */
13238   ret = TRUE;
13239   eh_frame = elf_eh_frame_section (sec->owner);
13240   if ((sec->flags & SEC_RELOC) != 0
13241       && sec->reloc_count > 0
13242       && sec != eh_frame)
13243     {
13244       struct elf_reloc_cookie cookie;
13245 
13246       if (!init_reloc_cookie_for_section (&cookie, info, sec))
13247 	ret = FALSE;
13248       else
13249 	{
13250 	  for (; cookie.rel < cookie.relend; cookie.rel++)
13251 	    if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13252 	      {
13253 		ret = FALSE;
13254 		break;
13255 	      }
13256 	  fini_reloc_cookie_for_section (&cookie, sec);
13257 	}
13258     }
13259 
13260   if (ret && eh_frame && elf_fde_list (sec))
13261     {
13262       struct elf_reloc_cookie cookie;
13263 
13264       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13265 	ret = FALSE;
13266       else
13267 	{
13268 	  if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13269 				      gc_mark_hook, &cookie))
13270 	    ret = FALSE;
13271 	  fini_reloc_cookie_for_section (&cookie, eh_frame);
13272 	}
13273     }
13274 
13275   eh_frame = elf_section_eh_frame_entry (sec);
13276   if (ret && eh_frame && !eh_frame->gc_mark)
13277     if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13278       ret = FALSE;
13279 
13280   return ret;
13281 }
13282 
13283 /* Scan and mark sections in a special or debug section group.  */
13284 
13285 static void
13286 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13287 {
13288   /* Point to first section of section group.  */
13289   asection *ssec;
13290   /* Used to iterate the section group.  */
13291   asection *msec;
13292 
13293   bfd_boolean is_special_grp = TRUE;
13294   bfd_boolean is_debug_grp = TRUE;
13295 
13296   /* First scan to see if group contains any section other than debug
13297      and special section.  */
13298   ssec = msec = elf_next_in_group (grp);
13299   do
13300     {
13301       if ((msec->flags & SEC_DEBUGGING) == 0)
13302 	is_debug_grp = FALSE;
13303 
13304       if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13305 	is_special_grp = FALSE;
13306 
13307       msec = elf_next_in_group (msec);
13308     }
13309   while (msec != ssec);
13310 
13311   /* If this is a pure debug section group or pure special section group,
13312      keep all sections in this group.  */
13313   if (is_debug_grp || is_special_grp)
13314     {
13315       do
13316 	{
13317 	  msec->gc_mark = 1;
13318 	  msec = elf_next_in_group (msec);
13319 	}
13320       while (msec != ssec);
13321     }
13322 }
13323 
13324 /* Keep debug and special sections.  */
13325 
13326 bfd_boolean
13327 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13328 				 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13329 {
13330   bfd *ibfd;
13331 
13332   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13333     {
13334       asection *isec;
13335       bfd_boolean some_kept;
13336       bfd_boolean debug_frag_seen;
13337       bfd_boolean has_kept_debug_info;
13338 
13339       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13340 	continue;
13341       isec = ibfd->sections;
13342       if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13343 	continue;
13344 
13345       /* Ensure all linker created sections are kept,
13346 	 see if any other section is already marked,
13347 	 and note if we have any fragmented debug sections.  */
13348       debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13349       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13350 	{
13351 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
13352 	    isec->gc_mark = 1;
13353 	  else if (isec->gc_mark
13354 		   && (isec->flags & SEC_ALLOC) != 0
13355 		   && elf_section_type (isec) != SHT_NOTE)
13356 	    some_kept = TRUE;
13357 
13358 	  if (!debug_frag_seen
13359 	      && (isec->flags & SEC_DEBUGGING)
13360 	      && CONST_STRNEQ (isec->name, ".debug_line."))
13361 	    debug_frag_seen = TRUE;
13362 	}
13363 
13364       /* If no non-note alloc section in this file will be kept, then
13365 	 we can toss out the debug and special sections.  */
13366       if (!some_kept)
13367 	continue;
13368 
13369       /* Keep debug and special sections like .comment when they are
13370 	 not part of a group.  Also keep section groups that contain
13371 	 just debug sections or special sections.  */
13372       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13373 	{
13374 	  if ((isec->flags & SEC_GROUP) != 0)
13375 	    _bfd_elf_gc_mark_debug_special_section_group (isec);
13376 	  else if (((isec->flags & SEC_DEBUGGING) != 0
13377 		    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13378 		   && elf_next_in_group (isec) == NULL)
13379 	    isec->gc_mark = 1;
13380 	  if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13381 	    has_kept_debug_info = TRUE;
13382 	}
13383 
13384       /* Look for CODE sections which are going to be discarded,
13385 	 and find and discard any fragmented debug sections which
13386 	 are associated with that code section.  */
13387       if (debug_frag_seen)
13388 	for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13389 	  if ((isec->flags & SEC_CODE) != 0
13390 	      && isec->gc_mark == 0)
13391 	    {
13392 	      unsigned int ilen;
13393 	      asection *dsec;
13394 
13395 	      ilen = strlen (isec->name);
13396 
13397 	      /* Association is determined by the name of the debug
13398 		 section containing the name of the code section as
13399 		 a suffix.  For example .debug_line.text.foo is a
13400 		 debug section associated with .text.foo.  */
13401 	      for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13402 		{
13403 		  unsigned int dlen;
13404 
13405 		  if (dsec->gc_mark == 0
13406 		      || (dsec->flags & SEC_DEBUGGING) == 0)
13407 		    continue;
13408 
13409 		  dlen = strlen (dsec->name);
13410 
13411 		  if (dlen > ilen
13412 		      && strncmp (dsec->name + (dlen - ilen),
13413 				  isec->name, ilen) == 0)
13414 		    dsec->gc_mark = 0;
13415 		}
13416 	  }
13417 
13418       /* Mark debug sections referenced by kept debug sections.  */
13419       if (has_kept_debug_info)
13420 	for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13421 	  if (isec->gc_mark
13422 	      && (isec->flags & SEC_DEBUGGING) != 0)
13423 	    if (!_bfd_elf_gc_mark (info, isec,
13424 				   elf_gc_mark_debug_section))
13425 	      return FALSE;
13426     }
13427   return TRUE;
13428 }
13429 
13430 static bfd_boolean
13431 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13432 {
13433   bfd *sub;
13434   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13435 
13436   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13437     {
13438       asection *o;
13439 
13440       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13441 	  || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13442 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13443 	continue;
13444       o = sub->sections;
13445       if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13446 	continue;
13447 
13448       for (o = sub->sections; o != NULL; o = o->next)
13449 	{
13450 	  /* When any section in a section group is kept, we keep all
13451 	     sections in the section group.  If the first member of
13452 	     the section group is excluded, we will also exclude the
13453 	     group section.  */
13454 	  if (o->flags & SEC_GROUP)
13455 	    {
13456 	      asection *first = elf_next_in_group (o);
13457 	      o->gc_mark = first->gc_mark;
13458 	    }
13459 
13460 	  if (o->gc_mark)
13461 	    continue;
13462 
13463 	  /* Skip sweeping sections already excluded.  */
13464 	  if (o->flags & SEC_EXCLUDE)
13465 	    continue;
13466 
13467 	  /* Since this is early in the link process, it is simple
13468 	     to remove a section from the output.  */
13469 	  o->flags |= SEC_EXCLUDE;
13470 
13471 	  if (info->print_gc_sections && o->size != 0)
13472 	    /* xgettext:c-format */
13473 	    _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13474 				o, sub);
13475 	}
13476     }
13477 
13478   return TRUE;
13479 }
13480 
13481 /* Propagate collected vtable information.  This is called through
13482    elf_link_hash_traverse.  */
13483 
13484 static bfd_boolean
13485 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13486 {
13487   /* Those that are not vtables.  */
13488   if (h->start_stop
13489       || h->u2.vtable == NULL
13490       || h->u2.vtable->parent == NULL)
13491     return TRUE;
13492 
13493   /* Those vtables that do not have parents, we cannot merge.  */
13494   if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13495     return TRUE;
13496 
13497   /* If we've already been done, exit.  */
13498   if (h->u2.vtable->used && h->u2.vtable->used[-1])
13499     return TRUE;
13500 
13501   /* Make sure the parent's table is up to date.  */
13502   elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13503 
13504   if (h->u2.vtable->used == NULL)
13505     {
13506       /* None of this table's entries were referenced.  Re-use the
13507 	 parent's table.  */
13508       h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13509       h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13510     }
13511   else
13512     {
13513       size_t n;
13514       bfd_boolean *cu, *pu;
13515 
13516       /* Or the parent's entries into ours.  */
13517       cu = h->u2.vtable->used;
13518       cu[-1] = TRUE;
13519       pu = h->u2.vtable->parent->u2.vtable->used;
13520       if (pu != NULL)
13521 	{
13522 	  const struct elf_backend_data *bed;
13523 	  unsigned int log_file_align;
13524 
13525 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
13526 	  log_file_align = bed->s->log_file_align;
13527 	  n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13528 	  while (n--)
13529 	    {
13530 	      if (*pu)
13531 		*cu = TRUE;
13532 	      pu++;
13533 	      cu++;
13534 	    }
13535 	}
13536     }
13537 
13538   return TRUE;
13539 }
13540 
13541 static bfd_boolean
13542 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13543 {
13544   asection *sec;
13545   bfd_vma hstart, hend;
13546   Elf_Internal_Rela *relstart, *relend, *rel;
13547   const struct elf_backend_data *bed;
13548   unsigned int log_file_align;
13549 
13550   /* Take care of both those symbols that do not describe vtables as
13551      well as those that are not loaded.  */
13552   if (h->start_stop
13553       || h->u2.vtable == NULL
13554       || h->u2.vtable->parent == NULL)
13555     return TRUE;
13556 
13557   BFD_ASSERT (h->root.type == bfd_link_hash_defined
13558 	      || h->root.type == bfd_link_hash_defweak);
13559 
13560   sec = h->root.u.def.section;
13561   hstart = h->root.u.def.value;
13562   hend = hstart + h->size;
13563 
13564   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13565   if (!relstart)
13566     return *(bfd_boolean *) okp = FALSE;
13567   bed = get_elf_backend_data (sec->owner);
13568   log_file_align = bed->s->log_file_align;
13569 
13570   relend = relstart + sec->reloc_count;
13571 
13572   for (rel = relstart; rel < relend; ++rel)
13573     if (rel->r_offset >= hstart && rel->r_offset < hend)
13574       {
13575 	/* If the entry is in use, do nothing.  */
13576 	if (h->u2.vtable->used
13577 	    && (rel->r_offset - hstart) < h->u2.vtable->size)
13578 	  {
13579 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13580 	    if (h->u2.vtable->used[entry])
13581 	      continue;
13582 	  }
13583 	/* Otherwise, kill it.  */
13584 	rel->r_offset = rel->r_info = rel->r_addend = 0;
13585       }
13586 
13587   return TRUE;
13588 }
13589 
13590 /* Mark sections containing dynamically referenced symbols.  When
13591    building shared libraries, we must assume that any visible symbol is
13592    referenced.  */
13593 
13594 bfd_boolean
13595 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13596 {
13597   struct bfd_link_info *info = (struct bfd_link_info *) inf;
13598   struct bfd_elf_dynamic_list *d = info->dynamic_list;
13599 
13600   if ((h->root.type == bfd_link_hash_defined
13601        || h->root.type == bfd_link_hash_defweak)
13602       && ((h->ref_dynamic && !h->forced_local)
13603 	  || ((h->def_regular || ELF_COMMON_DEF_P (h))
13604 	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13605 	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13606 	      && (!bfd_link_executable (info)
13607 		  || info->gc_keep_exported
13608 		  || info->export_dynamic
13609 		  || (h->dynamic
13610 		      && d != NULL
13611 		      && (*d->match) (&d->head, NULL, h->root.root.string)))
13612 	      && (h->versioned >= versioned
13613 		  || !bfd_hide_sym_by_version (info->version_info,
13614 					       h->root.root.string)))))
13615     h->root.u.def.section->flags |= SEC_KEEP;
13616 
13617   return TRUE;
13618 }
13619 
13620 /* Keep all sections containing symbols undefined on the command-line,
13621    and the section containing the entry symbol.  */
13622 
13623 void
13624 _bfd_elf_gc_keep (struct bfd_link_info *info)
13625 {
13626   struct bfd_sym_chain *sym;
13627 
13628   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13629     {
13630       struct elf_link_hash_entry *h;
13631 
13632       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13633 				FALSE, FALSE, FALSE);
13634 
13635       if (h != NULL
13636 	  && (h->root.type == bfd_link_hash_defined
13637 	      || h->root.type == bfd_link_hash_defweak)
13638 	  && !bfd_is_abs_section (h->root.u.def.section)
13639 	  && !bfd_is_und_section (h->root.u.def.section))
13640 	h->root.u.def.section->flags |= SEC_KEEP;
13641     }
13642 }
13643 
13644 bfd_boolean
13645 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13646 				struct bfd_link_info *info)
13647 {
13648   bfd *ibfd = info->input_bfds;
13649 
13650   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13651     {
13652       asection *sec;
13653       struct elf_reloc_cookie cookie;
13654 
13655       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13656 	continue;
13657       sec = ibfd->sections;
13658       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13659 	continue;
13660 
13661       if (!init_reloc_cookie (&cookie, info, ibfd))
13662 	return FALSE;
13663 
13664       for (sec = ibfd->sections; sec; sec = sec->next)
13665 	{
13666 	  if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
13667 	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13668 	    {
13669 	      _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13670 	      fini_reloc_cookie_rels (&cookie, sec);
13671 	    }
13672 	}
13673     }
13674   return TRUE;
13675 }
13676 
13677 /* Do mark and sweep of unused sections.  */
13678 
13679 bfd_boolean
13680 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13681 {
13682   bfd_boolean ok = TRUE;
13683   bfd *sub;
13684   elf_gc_mark_hook_fn gc_mark_hook;
13685   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13686   struct elf_link_hash_table *htab;
13687 
13688   if (!bed->can_gc_sections
13689       || !is_elf_hash_table (info->hash))
13690     {
13691       _bfd_error_handler(_("warning: gc-sections option ignored"));
13692       return TRUE;
13693     }
13694 
13695   bed->gc_keep (info);
13696   htab = elf_hash_table (info);
13697 
13698   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
13699      at the .eh_frame section if we can mark the FDEs individually.  */
13700   for (sub = info->input_bfds;
13701        info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13702        sub = sub->link.next)
13703     {
13704       asection *sec;
13705       struct elf_reloc_cookie cookie;
13706 
13707       sec = sub->sections;
13708       if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13709 	continue;
13710       sec = bfd_get_section_by_name (sub, ".eh_frame");
13711       while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13712 	{
13713 	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13714 	  if (elf_section_data (sec)->sec_info
13715 	      && (sec->flags & SEC_LINKER_CREATED) == 0)
13716 	    elf_eh_frame_section (sub) = sec;
13717 	  fini_reloc_cookie_for_section (&cookie, sec);
13718 	  sec = bfd_get_next_section_by_name (NULL, sec);
13719 	}
13720     }
13721 
13722   /* Apply transitive closure to the vtable entry usage info.  */
13723   elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13724   if (!ok)
13725     return FALSE;
13726 
13727   /* Kill the vtable relocations that were not used.  */
13728   elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13729   if (!ok)
13730     return FALSE;
13731 
13732   /* Mark dynamically referenced symbols.  */
13733   if (htab->dynamic_sections_created || info->gc_keep_exported)
13734     elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13735 
13736   /* Grovel through relocs to find out who stays ...  */
13737   gc_mark_hook = bed->gc_mark_hook;
13738   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13739     {
13740       asection *o;
13741 
13742       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13743 	  || elf_object_id (sub) != elf_hash_table_id (htab)
13744 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13745 	continue;
13746 
13747       o = sub->sections;
13748       if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13749 	continue;
13750 
13751       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13752 	 Also treat note sections as a root, if the section is not part
13753 	 of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
13754 	 well as FINI_ARRAY sections for ld -r.  */
13755       for (o = sub->sections; o != NULL; o = o->next)
13756 	if (!o->gc_mark
13757 	    && (o->flags & SEC_EXCLUDE) == 0
13758 	    && ((o->flags & SEC_KEEP) != 0
13759 		|| (bfd_link_relocatable (info)
13760 		    && ((elf_section_data (o)->this_hdr.sh_type
13761 			 == SHT_PREINIT_ARRAY)
13762 			|| (elf_section_data (o)->this_hdr.sh_type
13763 			    == SHT_INIT_ARRAY)
13764 			|| (elf_section_data (o)->this_hdr.sh_type
13765 			    == SHT_FINI_ARRAY)))
13766 		|| (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13767 		    && elf_next_in_group (o) == NULL )))
13768 	  {
13769 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13770 	      return FALSE;
13771 	  }
13772     }
13773 
13774   /* Allow the backend to mark additional target specific sections.  */
13775   bed->gc_mark_extra_sections (info, gc_mark_hook);
13776 
13777   /* ... and mark SEC_EXCLUDE for those that go.  */
13778   return elf_gc_sweep (abfd, info);
13779 }
13780 
13781 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
13782 
13783 bfd_boolean
13784 bfd_elf_gc_record_vtinherit (bfd *abfd,
13785 			     asection *sec,
13786 			     struct elf_link_hash_entry *h,
13787 			     bfd_vma offset)
13788 {
13789   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13790   struct elf_link_hash_entry **search, *child;
13791   size_t extsymcount;
13792   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13793 
13794   /* The sh_info field of the symtab header tells us where the
13795      external symbols start.  We don't care about the local symbols at
13796      this point.  */
13797   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13798   if (!elf_bad_symtab (abfd))
13799     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13800 
13801   sym_hashes = elf_sym_hashes (abfd);
13802   sym_hashes_end = sym_hashes + extsymcount;
13803 
13804   /* Hunt down the child symbol, which is in this section at the same
13805      offset as the relocation.  */
13806   for (search = sym_hashes; search != sym_hashes_end; ++search)
13807     {
13808       if ((child = *search) != NULL
13809 	  && (child->root.type == bfd_link_hash_defined
13810 	      || child->root.type == bfd_link_hash_defweak)
13811 	  && child->root.u.def.section == sec
13812 	  && child->root.u.def.value == offset)
13813 	goto win;
13814     }
13815 
13816   /* xgettext:c-format */
13817   _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13818 		      abfd, sec, (uint64_t) offset);
13819   bfd_set_error (bfd_error_invalid_operation);
13820   return FALSE;
13821 
13822  win:
13823   if (!child->u2.vtable)
13824     {
13825       child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13826 			  bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13827       if (!child->u2.vtable)
13828 	return FALSE;
13829     }
13830   if (!h)
13831     {
13832       /* This *should* only be the absolute section.  It could potentially
13833 	 be that someone has defined a non-global vtable though, which
13834 	 would be bad.  It isn't worth paging in the local symbols to be
13835 	 sure though; that case should simply be handled by the assembler.  */
13836 
13837       child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13838     }
13839   else
13840     child->u2.vtable->parent = h;
13841 
13842   return TRUE;
13843 }
13844 
13845 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
13846 
13847 bfd_boolean
13848 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
13849 			   struct elf_link_hash_entry *h,
13850 			   bfd_vma addend)
13851 {
13852   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13853   unsigned int log_file_align = bed->s->log_file_align;
13854 
13855   if (!h)
13856     {
13857       /* xgettext:c-format */
13858       _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13859 			  abfd, sec);
13860       bfd_set_error (bfd_error_bad_value);
13861       return FALSE;
13862     }
13863 
13864   if (!h->u2.vtable)
13865     {
13866       h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13867 		      bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13868       if (!h->u2.vtable)
13869 	return FALSE;
13870     }
13871 
13872   if (addend >= h->u2.vtable->size)
13873     {
13874       size_t size, bytes, file_align;
13875       bfd_boolean *ptr = h->u2.vtable->used;
13876 
13877       /* While the symbol is undefined, we have to be prepared to handle
13878 	 a zero size.  */
13879       file_align = 1 << log_file_align;
13880       if (h->root.type == bfd_link_hash_undefined)
13881 	size = addend + file_align;
13882       else
13883 	{
13884 	  size = h->size;
13885 	  if (addend >= size)
13886 	    {
13887 	      /* Oops!  We've got a reference past the defined end of
13888 		 the table.  This is probably a bug -- shall we warn?  */
13889 	      size = addend + file_align;
13890 	    }
13891 	}
13892       size = (size + file_align - 1) & -file_align;
13893 
13894       /* Allocate one extra entry for use as a "done" flag for the
13895 	 consolidation pass.  */
13896       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13897 
13898       if (ptr)
13899 	{
13900 	  ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13901 
13902 	  if (ptr != NULL)
13903 	    {
13904 	      size_t oldbytes;
13905 
13906 	      oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13907 			  * sizeof (bfd_boolean));
13908 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13909 	    }
13910 	}
13911       else
13912 	ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13913 
13914       if (ptr == NULL)
13915 	return FALSE;
13916 
13917       /* And arrange for that done flag to be at index -1.  */
13918       h->u2.vtable->used = ptr + 1;
13919       h->u2.vtable->size = size;
13920     }
13921 
13922   h->u2.vtable->used[addend >> log_file_align] = TRUE;
13923 
13924   return TRUE;
13925 }
13926 
13927 /* Map an ELF section header flag to its corresponding string.  */
13928 typedef struct
13929 {
13930   char *flag_name;
13931   flagword flag_value;
13932 } elf_flags_to_name_table;
13933 
13934 static elf_flags_to_name_table elf_flags_to_names [] =
13935 {
13936   { "SHF_WRITE", SHF_WRITE },
13937   { "SHF_ALLOC", SHF_ALLOC },
13938   { "SHF_EXECINSTR", SHF_EXECINSTR },
13939   { "SHF_MERGE", SHF_MERGE },
13940   { "SHF_STRINGS", SHF_STRINGS },
13941   { "SHF_INFO_LINK", SHF_INFO_LINK},
13942   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13943   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13944   { "SHF_GROUP", SHF_GROUP },
13945   { "SHF_TLS", SHF_TLS },
13946   { "SHF_MASKOS", SHF_MASKOS },
13947   { "SHF_EXCLUDE", SHF_EXCLUDE },
13948 };
13949 
13950 /* Returns TRUE if the section is to be included, otherwise FALSE.  */
13951 bfd_boolean
13952 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13953 			      struct flag_info *flaginfo,
13954 			      asection *section)
13955 {
13956   const bfd_vma sh_flags = elf_section_flags (section);
13957 
13958   if (!flaginfo->flags_initialized)
13959     {
13960       bfd *obfd = info->output_bfd;
13961       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13962       struct flag_info_list *tf = flaginfo->flag_list;
13963       int with_hex = 0;
13964       int without_hex = 0;
13965 
13966       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13967 	{
13968 	  unsigned i;
13969 	  flagword (*lookup) (char *);
13970 
13971 	  lookup = bed->elf_backend_lookup_section_flags_hook;
13972 	  if (lookup != NULL)
13973 	    {
13974 	      flagword hexval = (*lookup) ((char *) tf->name);
13975 
13976 	      if (hexval != 0)
13977 		{
13978 		  if (tf->with == with_flags)
13979 		    with_hex |= hexval;
13980 		  else if (tf->with == without_flags)
13981 		    without_hex |= hexval;
13982 		  tf->valid = TRUE;
13983 		  continue;
13984 		}
13985 	    }
13986 	  for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13987 	    {
13988 	      if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13989 		{
13990 		  if (tf->with == with_flags)
13991 		    with_hex |= elf_flags_to_names[i].flag_value;
13992 		  else if (tf->with == without_flags)
13993 		    without_hex |= elf_flags_to_names[i].flag_value;
13994 		  tf->valid = TRUE;
13995 		  break;
13996 		}
13997 	    }
13998 	  if (!tf->valid)
13999 	    {
14000 	      info->callbacks->einfo
14001 		(_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14002 	      return FALSE;
14003 	    }
14004 	}
14005       flaginfo->flags_initialized = TRUE;
14006       flaginfo->only_with_flags |= with_hex;
14007       flaginfo->not_with_flags |= without_hex;
14008     }
14009 
14010   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14011     return FALSE;
14012 
14013   if ((flaginfo->not_with_flags & sh_flags) != 0)
14014     return FALSE;
14015 
14016   return TRUE;
14017 }
14018 
14019 struct alloc_got_off_arg {
14020   bfd_vma gotoff;
14021   struct bfd_link_info *info;
14022 };
14023 
14024 /* We need a special top-level link routine to convert got reference counts
14025    to real got offsets.  */
14026 
14027 static bfd_boolean
14028 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14029 {
14030   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14031   bfd *obfd = gofarg->info->output_bfd;
14032   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14033 
14034   if (h->got.refcount > 0)
14035     {
14036       h->got.offset = gofarg->gotoff;
14037       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14038     }
14039   else
14040     h->got.offset = (bfd_vma) -1;
14041 
14042   return TRUE;
14043 }
14044 
14045 /* And an accompanying bit to work out final got entry offsets once
14046    we're done.  Should be called from final_link.  */
14047 
14048 bfd_boolean
14049 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14050 					struct bfd_link_info *info)
14051 {
14052   bfd *i;
14053   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14054   bfd_vma gotoff;
14055   struct alloc_got_off_arg gofarg;
14056 
14057   BFD_ASSERT (abfd == info->output_bfd);
14058 
14059   if (! is_elf_hash_table (info->hash))
14060     return FALSE;
14061 
14062   /* The GOT offset is relative to the .got section, but the GOT header is
14063      put into the .got.plt section, if the backend uses it.  */
14064   if (bed->want_got_plt)
14065     gotoff = 0;
14066   else
14067     gotoff = bed->got_header_size;
14068 
14069   /* Do the local .got entries first.  */
14070   for (i = info->input_bfds; i; i = i->link.next)
14071     {
14072       bfd_signed_vma *local_got;
14073       size_t j, locsymcount;
14074       Elf_Internal_Shdr *symtab_hdr;
14075 
14076       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14077 	continue;
14078 
14079       local_got = elf_local_got_refcounts (i);
14080       if (!local_got)
14081 	continue;
14082 
14083       symtab_hdr = &elf_tdata (i)->symtab_hdr;
14084       if (elf_bad_symtab (i))
14085 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14086       else
14087 	locsymcount = symtab_hdr->sh_info;
14088 
14089       for (j = 0; j < locsymcount; ++j)
14090 	{
14091 	  if (local_got[j] > 0)
14092 	    {
14093 	      local_got[j] = gotoff;
14094 	      gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14095 	    }
14096 	  else
14097 	    local_got[j] = (bfd_vma) -1;
14098 	}
14099     }
14100 
14101   /* Then the global .got entries.  .plt refcounts are handled by
14102      adjust_dynamic_symbol  */
14103   gofarg.gotoff = gotoff;
14104   gofarg.info = info;
14105   elf_link_hash_traverse (elf_hash_table (info),
14106 			  elf_gc_allocate_got_offsets,
14107 			  &gofarg);
14108   return TRUE;
14109 }
14110 
14111 /* Many folk need no more in the way of final link than this, once
14112    got entry reference counting is enabled.  */
14113 
14114 bfd_boolean
14115 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14116 {
14117   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14118     return FALSE;
14119 
14120   /* Invoke the regular ELF backend linker to do all the work.  */
14121   return bfd_elf_final_link (abfd, info);
14122 }
14123 
14124 bfd_boolean
14125 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14126 {
14127   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14128 
14129   if (rcookie->bad_symtab)
14130     rcookie->rel = rcookie->rels;
14131 
14132   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14133     {
14134       unsigned long r_symndx;
14135 
14136       if (! rcookie->bad_symtab)
14137 	if (rcookie->rel->r_offset > offset)
14138 	  return FALSE;
14139       if (rcookie->rel->r_offset != offset)
14140 	continue;
14141 
14142       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14143       if (r_symndx == STN_UNDEF)
14144 	return TRUE;
14145 
14146       if (r_symndx >= rcookie->locsymcount
14147 	  || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14148 	{
14149 	  struct elf_link_hash_entry *h;
14150 
14151 	  h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14152 
14153 	  while (h->root.type == bfd_link_hash_indirect
14154 		 || h->root.type == bfd_link_hash_warning)
14155 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
14156 
14157 	  if ((h->root.type == bfd_link_hash_defined
14158 	       || h->root.type == bfd_link_hash_defweak)
14159 	      && (h->root.u.def.section->owner != rcookie->abfd
14160 		  || h->root.u.def.section->kept_section != NULL
14161 		  || discarded_section (h->root.u.def.section)))
14162 	    return TRUE;
14163 	}
14164       else
14165 	{
14166 	  /* It's not a relocation against a global symbol,
14167 	     but it could be a relocation against a local
14168 	     symbol for a discarded section.  */
14169 	  asection *isec;
14170 	  Elf_Internal_Sym *isym;
14171 
14172 	  /* Need to: get the symbol; get the section.  */
14173 	  isym = &rcookie->locsyms[r_symndx];
14174 	  isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14175 	  if (isec != NULL
14176 	      && (isec->kept_section != NULL
14177 		  || discarded_section (isec)))
14178 	    return TRUE;
14179 	}
14180       return FALSE;
14181     }
14182   return FALSE;
14183 }
14184 
14185 /* Discard unneeded references to discarded sections.
14186    Returns -1 on error, 1 if any section's size was changed, 0 if
14187    nothing changed.  This function assumes that the relocations are in
14188    sorted order, which is true for all known assemblers.  */
14189 
14190 int
14191 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14192 {
14193   struct elf_reloc_cookie cookie;
14194   asection *o;
14195   bfd *abfd;
14196   int changed = 0;
14197 
14198   if (info->traditional_format
14199       || !is_elf_hash_table (info->hash))
14200     return 0;
14201 
14202   o = bfd_get_section_by_name (output_bfd, ".stab");
14203   if (o != NULL)
14204     {
14205       asection *i;
14206 
14207       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14208 	{
14209 	  if (i->size == 0
14210 	      || i->reloc_count == 0
14211 	      || i->sec_info_type != SEC_INFO_TYPE_STABS)
14212 	    continue;
14213 
14214 	  abfd = i->owner;
14215 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14216 	    continue;
14217 
14218 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
14219 	    return -1;
14220 
14221 	  if (_bfd_discard_section_stabs (abfd, i,
14222 					  elf_section_data (i)->sec_info,
14223 					  bfd_elf_reloc_symbol_deleted_p,
14224 					  &cookie))
14225 	    changed = 1;
14226 
14227 	  fini_reloc_cookie_for_section (&cookie, i);
14228 	}
14229     }
14230 
14231   o = NULL;
14232   if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14233     o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14234   if (o != NULL)
14235     {
14236       asection *i;
14237       int eh_changed = 0;
14238       unsigned int eh_alignment;
14239 
14240       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14241 	{
14242 	  if (i->size == 0)
14243 	    continue;
14244 
14245 	  abfd = i->owner;
14246 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14247 	    continue;
14248 
14249 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
14250 	    return -1;
14251 
14252 	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14253 	  if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14254 						 bfd_elf_reloc_symbol_deleted_p,
14255 						 &cookie))
14256 	    {
14257 	      eh_changed = 1;
14258 	      if (i->size != i->rawsize)
14259 		changed = 1;
14260 	    }
14261 
14262 	  fini_reloc_cookie_for_section (&cookie, i);
14263 	}
14264 
14265       eh_alignment = 1 << o->alignment_power;
14266       /* Skip over zero terminator, and prevent empty sections from
14267 	 adding alignment padding at the end.  */
14268       for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14269 	if (i->size == 0)
14270 	  i->flags |= SEC_EXCLUDE;
14271 	else if (i->size > 4)
14272 	  break;
14273       /* The last non-empty eh_frame section doesn't need padding.  */
14274       if (i != NULL)
14275 	i = i->map_tail.s;
14276       /* Any prior sections must pad the last FDE out to the output
14277 	 section alignment.  Otherwise we might have zero padding
14278 	 between sections, which would be seen as a terminator.  */
14279       for (; i != NULL; i = i->map_tail.s)
14280 	if (i->size == 4)
14281 	  /* All but the last zero terminator should have been removed.  */
14282 	  BFD_FAIL ();
14283 	else
14284 	  {
14285 	    bfd_size_type size
14286 	      = (i->size + eh_alignment - 1) & -eh_alignment;
14287 	    if (i->size != size)
14288 	      {
14289 		i->size = size;
14290 		changed = 1;
14291 		eh_changed = 1;
14292 	      }
14293 	  }
14294       if (eh_changed)
14295 	elf_link_hash_traverse (elf_hash_table (info),
14296 				_bfd_elf_adjust_eh_frame_global_symbol, NULL);
14297     }
14298 
14299   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14300     {
14301       const struct elf_backend_data *bed;
14302       asection *s;
14303 
14304       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14305 	continue;
14306       s = abfd->sections;
14307       if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14308 	continue;
14309 
14310       bed = get_elf_backend_data (abfd);
14311 
14312       if (bed->elf_backend_discard_info != NULL)
14313 	{
14314 	  if (!init_reloc_cookie (&cookie, info, abfd))
14315 	    return -1;
14316 
14317 	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14318 	    changed = 1;
14319 
14320 	  fini_reloc_cookie (&cookie, abfd);
14321 	}
14322     }
14323 
14324   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14325     _bfd_elf_end_eh_frame_parsing (info);
14326 
14327   if (info->eh_frame_hdr_type
14328       && !bfd_link_relocatable (info)
14329       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14330     changed = 1;
14331 
14332   return changed;
14333 }
14334 
14335 bfd_boolean
14336 _bfd_elf_section_already_linked (bfd *abfd,
14337 				 asection *sec,
14338 				 struct bfd_link_info *info)
14339 {
14340   flagword flags;
14341   const char *name, *key;
14342   struct bfd_section_already_linked *l;
14343   struct bfd_section_already_linked_hash_entry *already_linked_list;
14344 
14345   if (sec->output_section == bfd_abs_section_ptr)
14346     return FALSE;
14347 
14348   flags = sec->flags;
14349 
14350   /* Return if it isn't a linkonce section.  A comdat group section
14351      also has SEC_LINK_ONCE set.  */
14352   if ((flags & SEC_LINK_ONCE) == 0)
14353     return FALSE;
14354 
14355   /* Don't put group member sections on our list of already linked
14356      sections.  They are handled as a group via their group section.  */
14357   if (elf_sec_group (sec) != NULL)
14358     return FALSE;
14359 
14360   /* For a SHT_GROUP section, use the group signature as the key.  */
14361   name = sec->name;
14362   if ((flags & SEC_GROUP) != 0
14363       && elf_next_in_group (sec) != NULL
14364       && elf_group_name (elf_next_in_group (sec)) != NULL)
14365     key = elf_group_name (elf_next_in_group (sec));
14366   else
14367     {
14368       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
14369       if (CONST_STRNEQ (name, ".gnu.linkonce.")
14370 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14371 	key++;
14372       else
14373 	/* Must be a user linkonce section that doesn't follow gcc's
14374 	   naming convention.  In this case we won't be matching
14375 	   single member groups.  */
14376 	key = name;
14377     }
14378 
14379   already_linked_list = bfd_section_already_linked_table_lookup (key);
14380 
14381   for (l = already_linked_list->entry; l != NULL; l = l->next)
14382     {
14383       /* We may have 2 different types of sections on the list: group
14384 	 sections with a signature of <key> (<key> is some string),
14385 	 and linkonce sections named .gnu.linkonce.<type>.<key>.
14386 	 Match like sections.  LTO plugin sections are an exception.
14387 	 They are always named .gnu.linkonce.t.<key> and match either
14388 	 type of section.  */
14389       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14390 	   && ((flags & SEC_GROUP) != 0
14391 	       || strcmp (name, l->sec->name) == 0))
14392 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14393 	{
14394 	  /* The section has already been linked.  See if we should
14395 	     issue a warning.  */
14396 	  if (!_bfd_handle_already_linked (sec, l, info))
14397 	    return FALSE;
14398 
14399 	  if (flags & SEC_GROUP)
14400 	    {
14401 	      asection *first = elf_next_in_group (sec);
14402 	      asection *s = first;
14403 
14404 	      while (s != NULL)
14405 		{
14406 		  s->output_section = bfd_abs_section_ptr;
14407 		  /* Record which group discards it.  */
14408 		  s->kept_section = l->sec;
14409 		  s = elf_next_in_group (s);
14410 		  /* These lists are circular.  */
14411 		  if (s == first)
14412 		    break;
14413 		}
14414 	    }
14415 
14416 	  return TRUE;
14417 	}
14418     }
14419 
14420   /* A single member comdat group section may be discarded by a
14421      linkonce section and vice versa.  */
14422   if ((flags & SEC_GROUP) != 0)
14423     {
14424       asection *first = elf_next_in_group (sec);
14425 
14426       if (first != NULL && elf_next_in_group (first) == first)
14427 	/* Check this single member group against linkonce sections.  */
14428 	for (l = already_linked_list->entry; l != NULL; l = l->next)
14429 	  if ((l->sec->flags & SEC_GROUP) == 0
14430 	      && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14431 	    {
14432 	      first->output_section = bfd_abs_section_ptr;
14433 	      first->kept_section = l->sec;
14434 	      sec->output_section = bfd_abs_section_ptr;
14435 	      break;
14436 	    }
14437     }
14438   else
14439     /* Check this linkonce section against single member groups.  */
14440     for (l = already_linked_list->entry; l != NULL; l = l->next)
14441       if (l->sec->flags & SEC_GROUP)
14442 	{
14443 	  asection *first = elf_next_in_group (l->sec);
14444 
14445 	  if (first != NULL
14446 	      && elf_next_in_group (first) == first
14447 	      && bfd_elf_match_symbols_in_sections (first, sec, info))
14448 	    {
14449 	      sec->output_section = bfd_abs_section_ptr;
14450 	      sec->kept_section = first;
14451 	      break;
14452 	    }
14453 	}
14454 
14455   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14456      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14457      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14458      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
14459      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
14460      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14461      `.gnu.linkonce.t.F' section from a different bfd not requiring any
14462      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
14463      The reverse order cannot happen as there is never a bfd with only the
14464      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
14465      matter as here were are looking only for cross-bfd sections.  */
14466 
14467   if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14468     for (l = already_linked_list->entry; l != NULL; l = l->next)
14469       if ((l->sec->flags & SEC_GROUP) == 0
14470 	  && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14471 	{
14472 	  if (abfd != l->sec->owner)
14473 	    sec->output_section = bfd_abs_section_ptr;
14474 	  break;
14475 	}
14476 
14477   /* This is the first section with this name.  Record it.  */
14478   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14479     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14480   return sec->output_section == bfd_abs_section_ptr;
14481 }
14482 
14483 bfd_boolean
14484 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14485 {
14486   return sym->st_shndx == SHN_COMMON;
14487 }
14488 
14489 unsigned int
14490 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14491 {
14492   return SHN_COMMON;
14493 }
14494 
14495 asection *
14496 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14497 {
14498   return bfd_com_section_ptr;
14499 }
14500 
14501 bfd_vma
14502 _bfd_elf_default_got_elt_size (bfd *abfd,
14503 			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
14504 			       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14505 			       bfd *ibfd ATTRIBUTE_UNUSED,
14506 			       unsigned long symndx ATTRIBUTE_UNUSED)
14507 {
14508   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14509   return bed->s->arch_size / 8;
14510 }
14511 
14512 /* Routines to support the creation of dynamic relocs.  */
14513 
14514 /* Returns the name of the dynamic reloc section associated with SEC.  */
14515 
14516 static const char *
14517 get_dynamic_reloc_section_name (bfd *       abfd,
14518 				asection *  sec,
14519 				bfd_boolean is_rela)
14520 {
14521   char *name;
14522   const char *old_name = bfd_section_name (sec);
14523   const char *prefix = is_rela ? ".rela" : ".rel";
14524 
14525   if (old_name == NULL)
14526     return NULL;
14527 
14528   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14529   sprintf (name, "%s%s", prefix, old_name);
14530 
14531   return name;
14532 }
14533 
14534 /* Returns the dynamic reloc section associated with SEC.
14535    If necessary compute the name of the dynamic reloc section based
14536    on SEC's name (looked up in ABFD's string table) and the setting
14537    of IS_RELA.  */
14538 
14539 asection *
14540 _bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
14541 				    asection *  sec,
14542 				    bfd_boolean is_rela)
14543 {
14544   asection * reloc_sec = elf_section_data (sec)->sreloc;
14545 
14546   if (reloc_sec == NULL)
14547     {
14548       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14549 
14550       if (name != NULL)
14551 	{
14552 	  reloc_sec = bfd_get_linker_section (abfd, name);
14553 
14554 	  if (reloc_sec != NULL)
14555 	    elf_section_data (sec)->sreloc = reloc_sec;
14556 	}
14557     }
14558 
14559   return reloc_sec;
14560 }
14561 
14562 /* Returns the dynamic reloc section associated with SEC.  If the
14563    section does not exist it is created and attached to the DYNOBJ
14564    bfd and stored in the SRELOC field of SEC's elf_section_data
14565    structure.
14566 
14567    ALIGNMENT is the alignment for the newly created section and
14568    IS_RELA defines whether the name should be .rela.<SEC's name>
14569    or .rel.<SEC's name>.  The section name is looked up in the
14570    string table associated with ABFD.  */
14571 
14572 asection *
14573 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14574 				     bfd *dynobj,
14575 				     unsigned int alignment,
14576 				     bfd *abfd,
14577 				     bfd_boolean is_rela)
14578 {
14579   asection * reloc_sec = elf_section_data (sec)->sreloc;
14580 
14581   if (reloc_sec == NULL)
14582     {
14583       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14584 
14585       if (name == NULL)
14586 	return NULL;
14587 
14588       reloc_sec = bfd_get_linker_section (dynobj, name);
14589 
14590       if (reloc_sec == NULL)
14591 	{
14592 	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14593 			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14594 	  if ((sec->flags & SEC_ALLOC) != 0)
14595 	    flags |= SEC_ALLOC | SEC_LOAD;
14596 
14597 	  reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14598 	  if (reloc_sec != NULL)
14599 	    {
14600 	      /* _bfd_elf_get_sec_type_attr chooses a section type by
14601 		 name.  Override as it may be wrong, eg. for a user
14602 		 section named "auto" we'll get ".relauto" which is
14603 		 seen to be a .rela section.  */
14604 	      elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14605 	      if (!bfd_set_section_alignment (reloc_sec, alignment))
14606 		reloc_sec = NULL;
14607 	    }
14608 	}
14609 
14610       elf_section_data (sec)->sreloc = reloc_sec;
14611     }
14612 
14613   return reloc_sec;
14614 }
14615 
14616 /* Copy the ELF symbol type and other attributes for a linker script
14617    assignment from HSRC to HDEST.  Generally this should be treated as
14618    if we found a strong non-dynamic definition for HDEST (except that
14619    ld ignores multiple definition errors).  */
14620 void
14621 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14622 				     struct bfd_link_hash_entry *hdest,
14623 				     struct bfd_link_hash_entry *hsrc)
14624 {
14625   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14626   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14627   Elf_Internal_Sym isym;
14628 
14629   ehdest->type = ehsrc->type;
14630   ehdest->target_internal = ehsrc->target_internal;
14631 
14632   isym.st_other = ehsrc->other;
14633   elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14634 }
14635 
14636 /* Append a RELA relocation REL to section S in BFD.  */
14637 
14638 void
14639 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14640 {
14641   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14642   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14643   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14644   bed->s->swap_reloca_out (abfd, rel, loc);
14645 }
14646 
14647 /* Append a REL relocation REL to section S in BFD.  */
14648 
14649 void
14650 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14651 {
14652   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14653   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14654   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14655   bed->s->swap_reloc_out (abfd, rel, loc);
14656 }
14657 
14658 /* Define __start, __stop, .startof. or .sizeof. symbol.  */
14659 
14660 struct bfd_link_hash_entry *
14661 bfd_elf_define_start_stop (struct bfd_link_info *info,
14662 			   const char *symbol, asection *sec)
14663 {
14664   struct elf_link_hash_entry *h;
14665 
14666   h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14667 			    FALSE, FALSE, TRUE);
14668   if (h != NULL
14669       && (h->root.type == bfd_link_hash_undefined
14670 	  || h->root.type == bfd_link_hash_undefweak
14671 	  || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14672     {
14673       bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14674       h->root.type = bfd_link_hash_defined;
14675       h->root.u.def.section = sec;
14676       h->root.u.def.value = 0;
14677       h->def_regular = 1;
14678       h->def_dynamic = 0;
14679       h->start_stop = 1;
14680       h->u2.start_stop_section = sec;
14681       if (symbol[0] == '.')
14682 	{
14683 	  /* .startof. and .sizeof. symbols are local.  */
14684 	  const struct elf_backend_data *bed;
14685 	  bed = get_elf_backend_data (info->output_bfd);
14686 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14687 	}
14688       else
14689 	{
14690 	  if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14691 	    h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14692 	  if (was_dynamic)
14693 	    bfd_elf_link_record_dynamic_symbol (info, h);
14694 	}
14695       return &h->root;
14696     }
14697   return NULL;
14698 }
14699