xref: /netbsd-src/external/gpl3/binutils/dist/bfd/elflink.c (revision 92e958de60c71aa0f2452bd7074cbb006fe6546b)
1 /* ELF linking support for BFD.
2    Copyright (C) 1995-2015 Free Software Foundation, Inc.
3 
4    This file is part of BFD, the Binary File Descriptor library.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfd_stdint.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31 
32 /* This struct is used to pass information to routines called via
33    elf_link_hash_traverse which must return failure.  */
34 
35 struct elf_info_failed
36 {
37   struct bfd_link_info *info;
38   bfd_boolean failed;
39 };
40 
41 /* This structure is used to pass information to
42    _bfd_elf_link_find_version_dependencies.  */
43 
44 struct elf_find_verdep_info
45 {
46   /* General link information.  */
47   struct bfd_link_info *info;
48   /* The number of dependencies.  */
49   unsigned int vers;
50   /* Whether we had a failure.  */
51   bfd_boolean failed;
52 };
53 
54 static bfd_boolean _bfd_elf_fix_symbol_flags
55   (struct elf_link_hash_entry *, struct elf_info_failed *);
56 
57 asection *
58 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59 			     unsigned long r_symndx,
60 			     bfd_boolean discard)
61 {
62   if (r_symndx >= cookie->locsymcount
63       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
64     {
65       struct elf_link_hash_entry *h;
66 
67       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
68 
69       while (h->root.type == bfd_link_hash_indirect
70 	     || h->root.type == bfd_link_hash_warning)
71 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
72 
73       if ((h->root.type == bfd_link_hash_defined
74 	   || h->root.type == bfd_link_hash_defweak)
75 	   && discarded_section (h->root.u.def.section))
76         return h->root.u.def.section;
77       else
78 	return NULL;
79     }
80   else
81     {
82       /* It's not a relocation against a global symbol,
83 	 but it could be a relocation against a local
84 	 symbol for a discarded section.  */
85       asection *isec;
86       Elf_Internal_Sym *isym;
87 
88       /* Need to: get the symbol; get the section.  */
89       isym = &cookie->locsyms[r_symndx];
90       isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
91       if (isec != NULL
92 	  && discard ? discarded_section (isec) : 1)
93 	return isec;
94      }
95   return NULL;
96 }
97 
98 /* Define a symbol in a dynamic linkage section.  */
99 
100 struct elf_link_hash_entry *
101 _bfd_elf_define_linkage_sym (bfd *abfd,
102 			     struct bfd_link_info *info,
103 			     asection *sec,
104 			     const char *name)
105 {
106   struct elf_link_hash_entry *h;
107   struct bfd_link_hash_entry *bh;
108   const struct elf_backend_data *bed;
109 
110   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
111   if (h != NULL)
112     {
113       /* Zap symbol defined in an as-needed lib that wasn't linked.
114 	 This is a symptom of a larger problem:  Absolute symbols
115 	 defined in shared libraries can't be overridden, because we
116 	 lose the link to the bfd which is via the symbol section.  */
117       h->root.type = bfd_link_hash_new;
118     }
119 
120   bh = &h->root;
121   bed = get_elf_backend_data (abfd);
122   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
123 					 sec, 0, NULL, FALSE, bed->collect,
124 					 &bh))
125     return NULL;
126   h = (struct elf_link_hash_entry *) bh;
127   h->def_regular = 1;
128   h->non_elf = 0;
129   h->root.linker_def = 1;
130   h->type = STT_OBJECT;
131   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
133 
134   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
135   return h;
136 }
137 
138 bfd_boolean
139 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
140 {
141   flagword flags;
142   asection *s;
143   struct elf_link_hash_entry *h;
144   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
145   struct elf_link_hash_table *htab = elf_hash_table (info);
146 
147   /* This function may be called more than once.  */
148   s = bfd_get_linker_section (abfd, ".got");
149   if (s != NULL)
150     return TRUE;
151 
152   flags = bed->dynamic_sec_flags;
153 
154   s = bfd_make_section_anyway_with_flags (abfd,
155 					  (bed->rela_plts_and_copies_p
156 					   ? ".rela.got" : ".rel.got"),
157 					  (bed->dynamic_sec_flags
158 					   | SEC_READONLY));
159   if (s == NULL
160       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
161     return FALSE;
162   htab->srelgot = s;
163 
164   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
165   if (s == NULL
166       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167     return FALSE;
168   htab->sgot = s;
169 
170   if (bed->want_got_plt)
171     {
172       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
173       if (s == NULL
174 	  || !bfd_set_section_alignment (abfd, s,
175 					 bed->s->log_file_align))
176 	return FALSE;
177       htab->sgotplt = s;
178     }
179 
180   /* The first bit of the global offset table is the header.  */
181   s->size += bed->got_header_size;
182 
183   if (bed->want_got_sym)
184     {
185       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 	 (or .got.plt) section.  We don't do this in the linker script
187 	 because we don't want to define the symbol if we are not creating
188 	 a global offset table.  */
189       h = _bfd_elf_define_linkage_sym (abfd, info, s,
190 				       "_GLOBAL_OFFSET_TABLE_");
191       elf_hash_table (info)->hgot = h;
192       if (h == NULL)
193 	return FALSE;
194     }
195 
196   return TRUE;
197 }
198 
199 /* Create a strtab to hold the dynamic symbol names.  */
200 static bfd_boolean
201 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
202 {
203   struct elf_link_hash_table *hash_table;
204 
205   hash_table = elf_hash_table (info);
206   if (hash_table->dynobj == NULL)
207     hash_table->dynobj = abfd;
208 
209   if (hash_table->dynstr == NULL)
210     {
211       hash_table->dynstr = _bfd_elf_strtab_init ();
212       if (hash_table->dynstr == NULL)
213 	return FALSE;
214     }
215   return TRUE;
216 }
217 
218 /* Create some sections which will be filled in with dynamic linking
219    information.  ABFD is an input file which requires dynamic sections
220    to be created.  The dynamic sections take up virtual memory space
221    when the final executable is run, so we need to create them before
222    addresses are assigned to the output sections.  We work out the
223    actual contents and size of these sections later.  */
224 
225 bfd_boolean
226 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
227 {
228   flagword flags;
229   asection *s;
230   const struct elf_backend_data *bed;
231   struct elf_link_hash_entry *h;
232 
233   if (! is_elf_hash_table (info->hash))
234     return FALSE;
235 
236   if (elf_hash_table (info)->dynamic_sections_created)
237     return TRUE;
238 
239   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
240     return FALSE;
241 
242   abfd = elf_hash_table (info)->dynobj;
243   bed = get_elf_backend_data (abfd);
244 
245   flags = bed->dynamic_sec_flags;
246 
247   /* A dynamically linked executable has a .interp section, but a
248      shared library does not.  */
249   if (bfd_link_executable (info) && !info->nointerp)
250     {
251       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
252 					      flags | SEC_READONLY);
253       if (s == NULL)
254 	return FALSE;
255     }
256 
257   /* Create sections to hold version informations.  These are removed
258      if they are not needed.  */
259   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
260 					  flags | SEC_READONLY);
261   if (s == NULL
262       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
263     return FALSE;
264 
265   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
266 					  flags | SEC_READONLY);
267   if (s == NULL
268       || ! bfd_set_section_alignment (abfd, s, 1))
269     return FALSE;
270 
271   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
272 					  flags | SEC_READONLY);
273   if (s == NULL
274       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
275     return FALSE;
276 
277   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
278 					  flags | SEC_READONLY);
279   if (s == NULL
280       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
281     return FALSE;
282   elf_hash_table (info)->dynsym = s;
283 
284   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
285 					  flags | SEC_READONLY);
286   if (s == NULL)
287     return FALSE;
288 
289   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
290   if (s == NULL
291       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292     return FALSE;
293 
294   /* The special symbol _DYNAMIC is always set to the start of the
295      .dynamic section.  We could set _DYNAMIC in a linker script, but we
296      only want to define it if we are, in fact, creating a .dynamic
297      section.  We don't want to define it if there is no .dynamic
298      section, since on some ELF platforms the start up code examines it
299      to decide how to initialize the process.  */
300   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
301   elf_hash_table (info)->hdynamic = h;
302   if (h == NULL)
303     return FALSE;
304 
305   if (info->emit_hash)
306     {
307       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
308 					      flags | SEC_READONLY);
309       if (s == NULL
310 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
311 	return FALSE;
312       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
313     }
314 
315   if (info->emit_gnu_hash)
316     {
317       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
318 					      flags | SEC_READONLY);
319       if (s == NULL
320 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 	return FALSE;
322       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
323 	 4 32-bit words followed by variable count of 64-bit words, then
324 	 variable count of 32-bit words.  */
325       if (bed->s->arch_size == 64)
326 	elf_section_data (s)->this_hdr.sh_entsize = 0;
327       else
328 	elf_section_data (s)->this_hdr.sh_entsize = 4;
329     }
330 
331   /* Let the backend create the rest of the sections.  This lets the
332      backend set the right flags.  The backend will normally create
333      the .got and .plt sections.  */
334   if (bed->elf_backend_create_dynamic_sections == NULL
335       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
336     return FALSE;
337 
338   elf_hash_table (info)->dynamic_sections_created = TRUE;
339 
340   return TRUE;
341 }
342 
343 /* Create dynamic sections when linking against a dynamic object.  */
344 
345 bfd_boolean
346 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
347 {
348   flagword flags, pltflags;
349   struct elf_link_hash_entry *h;
350   asection *s;
351   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
352   struct elf_link_hash_table *htab = elf_hash_table (info);
353 
354   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
355      .rel[a].bss sections.  */
356   flags = bed->dynamic_sec_flags;
357 
358   pltflags = flags;
359   if (bed->plt_not_loaded)
360     /* We do not clear SEC_ALLOC here because we still want the OS to
361        allocate space for the section; it's just that there's nothing
362        to read in from the object file.  */
363     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
364   else
365     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
366   if (bed->plt_readonly)
367     pltflags |= SEC_READONLY;
368 
369   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
370   if (s == NULL
371       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
372     return FALSE;
373   htab->splt = s;
374 
375   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
376      .plt section.  */
377   if (bed->want_plt_sym)
378     {
379       h = _bfd_elf_define_linkage_sym (abfd, info, s,
380 				       "_PROCEDURE_LINKAGE_TABLE_");
381       elf_hash_table (info)->hplt = h;
382       if (h == NULL)
383 	return FALSE;
384     }
385 
386   s = bfd_make_section_anyway_with_flags (abfd,
387 					  (bed->rela_plts_and_copies_p
388 					   ? ".rela.plt" : ".rel.plt"),
389 					  flags | SEC_READONLY);
390   if (s == NULL
391       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
392     return FALSE;
393   htab->srelplt = s;
394 
395   if (! _bfd_elf_create_got_section (abfd, info))
396     return FALSE;
397 
398   if (bed->want_dynbss)
399     {
400       /* The .dynbss section is a place to put symbols which are defined
401 	 by dynamic objects, are referenced by regular objects, and are
402 	 not functions.  We must allocate space for them in the process
403 	 image and use a R_*_COPY reloc to tell the dynamic linker to
404 	 initialize them at run time.  The linker script puts the .dynbss
405 	 section into the .bss section of the final image.  */
406       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
407 					      (SEC_ALLOC | SEC_LINKER_CREATED));
408       if (s == NULL)
409 	return FALSE;
410 
411       /* The .rel[a].bss section holds copy relocs.  This section is not
412 	 normally needed.  We need to create it here, though, so that the
413 	 linker will map it to an output section.  We can't just create it
414 	 only if we need it, because we will not know whether we need it
415 	 until we have seen all the input files, and the first time the
416 	 main linker code calls BFD after examining all the input files
417 	 (size_dynamic_sections) the input sections have already been
418 	 mapped to the output sections.  If the section turns out not to
419 	 be needed, we can discard it later.  We will never need this
420 	 section when generating a shared object, since they do not use
421 	 copy relocs.  */
422       if (! bfd_link_pic (info))
423 	{
424 	  s = bfd_make_section_anyway_with_flags (abfd,
425 						  (bed->rela_plts_and_copies_p
426 						   ? ".rela.bss" : ".rel.bss"),
427 						  flags | SEC_READONLY);
428 	  if (s == NULL
429 	      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
430 	    return FALSE;
431 	}
432     }
433 
434   return TRUE;
435 }
436 
437 /* Record a new dynamic symbol.  We record the dynamic symbols as we
438    read the input files, since we need to have a list of all of them
439    before we can determine the final sizes of the output sections.
440    Note that we may actually call this function even though we are not
441    going to output any dynamic symbols; in some cases we know that a
442    symbol should be in the dynamic symbol table, but only if there is
443    one.  */
444 
445 bfd_boolean
446 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
447 				    struct elf_link_hash_entry *h)
448 {
449   if (h->dynindx == -1)
450     {
451       struct elf_strtab_hash *dynstr;
452       char *p;
453       const char *name;
454       bfd_size_type indx;
455 
456       /* XXX: The ABI draft says the linker must turn hidden and
457 	 internal symbols into STB_LOCAL symbols when producing the
458 	 DSO. However, if ld.so honors st_other in the dynamic table,
459 	 this would not be necessary.  */
460       switch (ELF_ST_VISIBILITY (h->other))
461 	{
462 	case STV_INTERNAL:
463 	case STV_HIDDEN:
464 	  if (h->root.type != bfd_link_hash_undefined
465 	      && h->root.type != bfd_link_hash_undefweak)
466 	    {
467 	      h->forced_local = 1;
468 	      if (!elf_hash_table (info)->is_relocatable_executable)
469 		return TRUE;
470 	    }
471 
472 	default:
473 	  break;
474 	}
475 
476       h->dynindx = elf_hash_table (info)->dynsymcount;
477       ++elf_hash_table (info)->dynsymcount;
478 
479       dynstr = elf_hash_table (info)->dynstr;
480       if (dynstr == NULL)
481 	{
482 	  /* Create a strtab to hold the dynamic symbol names.  */
483 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
484 	  if (dynstr == NULL)
485 	    return FALSE;
486 	}
487 
488       /* We don't put any version information in the dynamic string
489 	 table.  */
490       name = h->root.root.string;
491       p = strchr (name, ELF_VER_CHR);
492       if (p != NULL)
493 	/* We know that the p points into writable memory.  In fact,
494 	   there are only a few symbols that have read-only names, being
495 	   those like _GLOBAL_OFFSET_TABLE_ that are created specially
496 	   by the backends.  Most symbols will have names pointing into
497 	   an ELF string table read from a file, or to objalloc memory.  */
498 	*p = 0;
499 
500       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
501 
502       if (p != NULL)
503 	*p = ELF_VER_CHR;
504 
505       if (indx == (bfd_size_type) -1)
506 	return FALSE;
507       h->dynstr_index = indx;
508     }
509 
510   return TRUE;
511 }
512 
513 /* Mark a symbol dynamic.  */
514 
515 static void
516 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
517 				  struct elf_link_hash_entry *h,
518 				  Elf_Internal_Sym *sym)
519 {
520   struct bfd_elf_dynamic_list *d = info->dynamic_list;
521 
522   /* It may be called more than once on the same H.  */
523   if(h->dynamic || bfd_link_relocatable (info))
524     return;
525 
526   if ((info->dynamic_data
527        && (h->type == STT_OBJECT
528 	   || (sym != NULL
529 	       && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
530       || (d != NULL
531 	  && h->root.type == bfd_link_hash_new
532 	  && (*d->match) (&d->head, NULL, h->root.root.string)))
533     h->dynamic = 1;
534 }
535 
536 /* Record an assignment to a symbol made by a linker script.  We need
537    this in case some dynamic object refers to this symbol.  */
538 
539 bfd_boolean
540 bfd_elf_record_link_assignment (bfd *output_bfd,
541 				struct bfd_link_info *info,
542 				const char *name,
543 				bfd_boolean provide,
544 				bfd_boolean hidden)
545 {
546   struct elf_link_hash_entry *h, *hv;
547   struct elf_link_hash_table *htab;
548   const struct elf_backend_data *bed;
549 
550   if (!is_elf_hash_table (info->hash))
551     return TRUE;
552 
553   htab = elf_hash_table (info);
554   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
555   if (h == NULL)
556     return provide;
557 
558   if (h->versioned == unknown)
559     {
560       /* Set versioned if symbol version is unknown.  */
561       char *version = strrchr (name, ELF_VER_CHR);
562       if (version)
563 	{
564 	  if (version > name && version[-1] != ELF_VER_CHR)
565 	    h->versioned = versioned_hidden;
566 	  else
567 	    h->versioned = versioned;
568 	}
569     }
570 
571   switch (h->root.type)
572     {
573     case bfd_link_hash_defined:
574     case bfd_link_hash_defweak:
575     case bfd_link_hash_common:
576       break;
577     case bfd_link_hash_undefweak:
578     case bfd_link_hash_undefined:
579       /* Since we're defining the symbol, don't let it seem to have not
580 	 been defined.  record_dynamic_symbol and size_dynamic_sections
581 	 may depend on this.  */
582       h->root.type = bfd_link_hash_new;
583       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
584 	bfd_link_repair_undef_list (&htab->root);
585       break;
586     case bfd_link_hash_new:
587       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
588       h->non_elf = 0;
589       break;
590     case bfd_link_hash_indirect:
591       /* We had a versioned symbol in a dynamic library.  We make the
592 	 the versioned symbol point to this one.  */
593       bed = get_elf_backend_data (output_bfd);
594       hv = h;
595       while (hv->root.type == bfd_link_hash_indirect
596 	     || hv->root.type == bfd_link_hash_warning)
597 	hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
598       /* We don't need to update h->root.u since linker will set them
599 	 later.  */
600       h->root.type = bfd_link_hash_undefined;
601       hv->root.type = bfd_link_hash_indirect;
602       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
603       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
604       break;
605     case bfd_link_hash_warning:
606       abort ();
607       break;
608     }
609 
610   /* If this symbol is being provided by the linker script, and it is
611      currently defined by a dynamic object, but not by a regular
612      object, then mark it as undefined so that the generic linker will
613      force the correct value.  */
614   if (provide
615       && h->def_dynamic
616       && !h->def_regular)
617     h->root.type = bfd_link_hash_undefined;
618 
619   /* If this symbol is not being provided by the linker script, and it is
620      currently defined by a dynamic object, but not by a regular object,
621      then clear out any version information because the symbol will not be
622      associated with the dynamic object any more.  */
623   if (!provide
624       && h->def_dynamic
625       && !h->def_regular)
626     h->verinfo.verdef = NULL;
627 
628   h->def_regular = 1;
629 
630   if (hidden)
631     {
632       bed = get_elf_backend_data (output_bfd);
633       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
634 	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
635       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
636     }
637 
638   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
639      and executables.  */
640   if (!bfd_link_relocatable (info)
641       && h->dynindx != -1
642       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
643 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
644     h->forced_local = 1;
645 
646   if ((h->def_dynamic
647        || h->ref_dynamic
648        || bfd_link_pic (info)
649        || (bfd_link_pde (info)
650 	   && elf_hash_table (info)->is_relocatable_executable))
651       && h->dynindx == -1)
652     {
653       if (! bfd_elf_link_record_dynamic_symbol (info, h))
654 	return FALSE;
655 
656       /* If this is a weak defined symbol, and we know a corresponding
657 	 real symbol from the same dynamic object, make sure the real
658 	 symbol is also made into a dynamic symbol.  */
659       if (h->u.weakdef != NULL
660 	  && h->u.weakdef->dynindx == -1)
661 	{
662 	  if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
663 	    return FALSE;
664 	}
665     }
666 
667   return TRUE;
668 }
669 
670 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
671    success, and 2 on a failure caused by attempting to record a symbol
672    in a discarded section, eg. a discarded link-once section symbol.  */
673 
674 int
675 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
676 					  bfd *input_bfd,
677 					  long input_indx)
678 {
679   bfd_size_type amt;
680   struct elf_link_local_dynamic_entry *entry;
681   struct elf_link_hash_table *eht;
682   struct elf_strtab_hash *dynstr;
683   unsigned long dynstr_index;
684   char *name;
685   Elf_External_Sym_Shndx eshndx;
686   char esym[sizeof (Elf64_External_Sym)];
687 
688   if (! is_elf_hash_table (info->hash))
689     return 0;
690 
691   /* See if the entry exists already.  */
692   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
693     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
694       return 1;
695 
696   amt = sizeof (*entry);
697   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
698   if (entry == NULL)
699     return 0;
700 
701   /* Go find the symbol, so that we can find it's name.  */
702   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
703 			     1, input_indx, &entry->isym, esym, &eshndx))
704     {
705       bfd_release (input_bfd, entry);
706       return 0;
707     }
708 
709   if (entry->isym.st_shndx != SHN_UNDEF
710       && entry->isym.st_shndx < SHN_LORESERVE)
711     {
712       asection *s;
713 
714       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
715       if (s == NULL || bfd_is_abs_section (s->output_section))
716 	{
717 	  /* We can still bfd_release here as nothing has done another
718 	     bfd_alloc.  We can't do this later in this function.  */
719 	  bfd_release (input_bfd, entry);
720 	  return 2;
721 	}
722     }
723 
724   name = (bfd_elf_string_from_elf_section
725 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
726 	   entry->isym.st_name));
727 
728   dynstr = elf_hash_table (info)->dynstr;
729   if (dynstr == NULL)
730     {
731       /* Create a strtab to hold the dynamic symbol names.  */
732       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
733       if (dynstr == NULL)
734 	return 0;
735     }
736 
737   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
738   if (dynstr_index == (unsigned long) -1)
739     return 0;
740   entry->isym.st_name = dynstr_index;
741 
742   eht = elf_hash_table (info);
743 
744   entry->next = eht->dynlocal;
745   eht->dynlocal = entry;
746   entry->input_bfd = input_bfd;
747   entry->input_indx = input_indx;
748   eht->dynsymcount++;
749 
750   /* Whatever binding the symbol had before, it's now local.  */
751   entry->isym.st_info
752     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
753 
754   /* The dynindx will be set at the end of size_dynamic_sections.  */
755 
756   return 1;
757 }
758 
759 /* Return the dynindex of a local dynamic symbol.  */
760 
761 long
762 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
763 				    bfd *input_bfd,
764 				    long input_indx)
765 {
766   struct elf_link_local_dynamic_entry *e;
767 
768   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
769     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
770       return e->dynindx;
771   return -1;
772 }
773 
774 /* This function is used to renumber the dynamic symbols, if some of
775    them are removed because they are marked as local.  This is called
776    via elf_link_hash_traverse.  */
777 
778 static bfd_boolean
779 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
780 				      void *data)
781 {
782   size_t *count = (size_t *) data;
783 
784   if (h->forced_local)
785     return TRUE;
786 
787   if (h->dynindx != -1)
788     h->dynindx = ++(*count);
789 
790   return TRUE;
791 }
792 
793 
794 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
795    STB_LOCAL binding.  */
796 
797 static bfd_boolean
798 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
799 					    void *data)
800 {
801   size_t *count = (size_t *) data;
802 
803   if (!h->forced_local)
804     return TRUE;
805 
806   if (h->dynindx != -1)
807     h->dynindx = ++(*count);
808 
809   return TRUE;
810 }
811 
812 /* Return true if the dynamic symbol for a given section should be
813    omitted when creating a shared library.  */
814 bfd_boolean
815 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
816 				   struct bfd_link_info *info,
817 				   asection *p)
818 {
819   struct elf_link_hash_table *htab;
820   asection *ip;
821 
822   switch (elf_section_data (p)->this_hdr.sh_type)
823     {
824     case SHT_PROGBITS:
825     case SHT_NOBITS:
826       /* If sh_type is yet undecided, assume it could be
827 	 SHT_PROGBITS/SHT_NOBITS.  */
828     case SHT_NULL:
829       htab = elf_hash_table (info);
830       if (p == htab->tls_sec)
831 	return FALSE;
832 
833       if (htab->text_index_section != NULL)
834 	return p != htab->text_index_section && p != htab->data_index_section;
835 
836       return (htab->dynobj != NULL
837 	      && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
838 	      && ip->output_section == p);
839 
840       /* There shouldn't be section relative relocations
841 	 against any other section.  */
842     default:
843       return TRUE;
844     }
845 }
846 
847 /* Assign dynsym indices.  In a shared library we generate a section
848    symbol for each output section, which come first.  Next come symbols
849    which have been forced to local binding.  Then all of the back-end
850    allocated local dynamic syms, followed by the rest of the global
851    symbols.  */
852 
853 static unsigned long
854 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
855 				struct bfd_link_info *info,
856 				unsigned long *section_sym_count)
857 {
858   unsigned long dynsymcount = 0;
859 
860   if (bfd_link_pic (info)
861       || elf_hash_table (info)->is_relocatable_executable)
862     {
863       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
864       asection *p;
865       for (p = output_bfd->sections; p ; p = p->next)
866 	if ((p->flags & SEC_EXCLUDE) == 0
867 	    && (p->flags & SEC_ALLOC) != 0
868 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
869 	  elf_section_data (p)->dynindx = ++dynsymcount;
870 	else
871 	  elf_section_data (p)->dynindx = 0;
872     }
873   *section_sym_count = dynsymcount;
874 
875   elf_link_hash_traverse (elf_hash_table (info),
876 			  elf_link_renumber_local_hash_table_dynsyms,
877 			  &dynsymcount);
878 
879   if (elf_hash_table (info)->dynlocal)
880     {
881       struct elf_link_local_dynamic_entry *p;
882       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
883 	p->dynindx = ++dynsymcount;
884     }
885 
886   elf_link_hash_traverse (elf_hash_table (info),
887 			  elf_link_renumber_hash_table_dynsyms,
888 			  &dynsymcount);
889 
890   /* There is an unused NULL entry at the head of the table which
891      we must account for in our count.  Unless there weren't any
892      symbols, which means we'll have no table at all.  */
893   if (dynsymcount != 0)
894     ++dynsymcount;
895 
896   elf_hash_table (info)->dynsymcount = dynsymcount;
897   return dynsymcount;
898 }
899 
900 /* Merge st_other field.  */
901 
902 static void
903 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
904 		    const Elf_Internal_Sym *isym, asection *sec,
905 		    bfd_boolean definition, bfd_boolean dynamic)
906 {
907   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
908 
909   /* If st_other has a processor-specific meaning, specific
910      code might be needed here.  */
911   if (bed->elf_backend_merge_symbol_attribute)
912     (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
913 						dynamic);
914 
915   if (!dynamic)
916     {
917       unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
918       unsigned hvis = ELF_ST_VISIBILITY (h->other);
919 
920       /* Keep the most constraining visibility.  Leave the remainder
921 	 of the st_other field to elf_backend_merge_symbol_attribute.  */
922       if (symvis - 1 < hvis - 1)
923 	h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
924     }
925   else if (definition
926 	   && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
927 	   && (sec->flags & SEC_READONLY) == 0)
928     h->protected_def = 1;
929 }
930 
931 /* This function is called when we want to merge a new symbol with an
932    existing symbol.  It handles the various cases which arise when we
933    find a definition in a dynamic object, or when there is already a
934    definition in a dynamic object.  The new symbol is described by
935    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
936    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
937    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
938    of an old common symbol.  We set OVERRIDE if the old symbol is
939    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
940    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
941    to change.  By OK to change, we mean that we shouldn't warn if the
942    type or size does change.  */
943 
944 static bfd_boolean
945 _bfd_elf_merge_symbol (bfd *abfd,
946 		       struct bfd_link_info *info,
947 		       const char *name,
948 		       Elf_Internal_Sym *sym,
949 		       asection **psec,
950 		       bfd_vma *pvalue,
951 		       struct elf_link_hash_entry **sym_hash,
952 		       bfd **poldbfd,
953 		       bfd_boolean *pold_weak,
954 		       unsigned int *pold_alignment,
955 		       bfd_boolean *skip,
956 		       bfd_boolean *override,
957 		       bfd_boolean *type_change_ok,
958 		       bfd_boolean *size_change_ok,
959 		       bfd_boolean *matched)
960 {
961   asection *sec, *oldsec;
962   struct elf_link_hash_entry *h;
963   struct elf_link_hash_entry *hi;
964   struct elf_link_hash_entry *flip;
965   int bind;
966   bfd *oldbfd;
967   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
968   bfd_boolean newweak, oldweak, newfunc, oldfunc;
969   const struct elf_backend_data *bed;
970   char *new_version;
971 
972   *skip = FALSE;
973   *override = FALSE;
974 
975   sec = *psec;
976   bind = ELF_ST_BIND (sym->st_info);
977 
978   if (! bfd_is_und_section (sec))
979     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
980   else
981     h = ((struct elf_link_hash_entry *)
982 	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
983   if (h == NULL)
984     return FALSE;
985   *sym_hash = h;
986 
987   bed = get_elf_backend_data (abfd);
988 
989   /* NEW_VERSION is the symbol version of the new symbol.  */
990   if (h->versioned != unversioned)
991     {
992       /* Symbol version is unknown or versioned.  */
993       new_version = strrchr (name, ELF_VER_CHR);
994       if (new_version)
995 	{
996 	  if (h->versioned == unknown)
997 	    {
998 	      if (new_version > name && new_version[-1] != ELF_VER_CHR)
999 		h->versioned = versioned_hidden;
1000 	      else
1001 		h->versioned = versioned;
1002 	    }
1003 	  new_version += 1;
1004 	  if (new_version[0] == '\0')
1005 	    new_version = NULL;
1006 	}
1007       else
1008 	h->versioned = unversioned;
1009     }
1010   else
1011     new_version = NULL;
1012 
1013   /* For merging, we only care about real symbols.  But we need to make
1014      sure that indirect symbol dynamic flags are updated.  */
1015   hi = h;
1016   while (h->root.type == bfd_link_hash_indirect
1017 	 || h->root.type == bfd_link_hash_warning)
1018     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1019 
1020   if (!*matched)
1021     {
1022       if (hi == h || h->root.type == bfd_link_hash_new)
1023 	*matched = TRUE;
1024       else
1025 	{
1026 	  /* OLD_HIDDEN is true if the existing symbol is only visible
1027 	     to the symbol with the same symbol version.  NEW_HIDDEN is
1028 	     true if the new symbol is only visible to the symbol with
1029 	     the same symbol version.  */
1030 	  bfd_boolean old_hidden = h->versioned == versioned_hidden;
1031 	  bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1032 	  if (!old_hidden && !new_hidden)
1033 	    /* The new symbol matches the existing symbol if both
1034 	       aren't hidden.  */
1035 	    *matched = TRUE;
1036 	  else
1037 	    {
1038 	      /* OLD_VERSION is the symbol version of the existing
1039 		 symbol. */
1040 	      char *old_version;
1041 
1042 	      if (h->versioned >= versioned)
1043 		old_version = strrchr (h->root.root.string,
1044 				       ELF_VER_CHR) + 1;
1045 	      else
1046 		 old_version = NULL;
1047 
1048 	      /* The new symbol matches the existing symbol if they
1049 		 have the same symbol version.  */
1050 	      *matched = (old_version == new_version
1051 			  || (old_version != NULL
1052 			      && new_version != NULL
1053 			      && strcmp (old_version, new_version) == 0));
1054 	    }
1055 	}
1056     }
1057 
1058   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1059      existing symbol.  */
1060 
1061   oldbfd = NULL;
1062   oldsec = NULL;
1063   switch (h->root.type)
1064     {
1065     default:
1066       break;
1067 
1068     case bfd_link_hash_undefined:
1069     case bfd_link_hash_undefweak:
1070       oldbfd = h->root.u.undef.abfd;
1071       break;
1072 
1073     case bfd_link_hash_defined:
1074     case bfd_link_hash_defweak:
1075       oldbfd = h->root.u.def.section->owner;
1076       oldsec = h->root.u.def.section;
1077       break;
1078 
1079     case bfd_link_hash_common:
1080       oldbfd = h->root.u.c.p->section->owner;
1081       oldsec = h->root.u.c.p->section;
1082       if (pold_alignment)
1083 	*pold_alignment = h->root.u.c.p->alignment_power;
1084       break;
1085     }
1086   if (poldbfd && *poldbfd == NULL)
1087     *poldbfd = oldbfd;
1088 
1089   /* Differentiate strong and weak symbols.  */
1090   newweak = bind == STB_WEAK;
1091   oldweak = (h->root.type == bfd_link_hash_defweak
1092 	     || h->root.type == bfd_link_hash_undefweak);
1093   if (pold_weak)
1094     *pold_weak = oldweak;
1095 
1096   /* This code is for coping with dynamic objects, and is only useful
1097      if we are doing an ELF link.  */
1098   if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1099     return TRUE;
1100 
1101   /* We have to check it for every instance since the first few may be
1102      references and not all compilers emit symbol type for undefined
1103      symbols.  */
1104   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1105 
1106   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1107      respectively, is from a dynamic object.  */
1108 
1109   newdyn = (abfd->flags & DYNAMIC) != 0;
1110 
1111   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1112      syms and defined syms in dynamic libraries respectively.
1113      ref_dynamic on the other hand can be set for a symbol defined in
1114      a dynamic library, and def_dynamic may not be set;  When the
1115      definition in a dynamic lib is overridden by a definition in the
1116      executable use of the symbol in the dynamic lib becomes a
1117      reference to the executable symbol.  */
1118   if (newdyn)
1119     {
1120       if (bfd_is_und_section (sec))
1121 	{
1122 	  if (bind != STB_WEAK)
1123 	    {
1124 	      h->ref_dynamic_nonweak = 1;
1125 	      hi->ref_dynamic_nonweak = 1;
1126 	    }
1127 	}
1128       else
1129 	{
1130 	  /* Update the existing symbol only if they match. */
1131 	  if (*matched)
1132 	    h->dynamic_def = 1;
1133 	  hi->dynamic_def = 1;
1134 	}
1135     }
1136 
1137   /* If we just created the symbol, mark it as being an ELF symbol.
1138      Other than that, there is nothing to do--there is no merge issue
1139      with a newly defined symbol--so we just return.  */
1140 
1141   if (h->root.type == bfd_link_hash_new)
1142     {
1143       h->non_elf = 0;
1144       return TRUE;
1145     }
1146 
1147   /* In cases involving weak versioned symbols, we may wind up trying
1148      to merge a symbol with itself.  Catch that here, to avoid the
1149      confusion that results if we try to override a symbol with
1150      itself.  The additional tests catch cases like
1151      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1152      dynamic object, which we do want to handle here.  */
1153   if (abfd == oldbfd
1154       && (newweak || oldweak)
1155       && ((abfd->flags & DYNAMIC) == 0
1156 	  || !h->def_regular))
1157     return TRUE;
1158 
1159   olddyn = FALSE;
1160   if (oldbfd != NULL)
1161     olddyn = (oldbfd->flags & DYNAMIC) != 0;
1162   else if (oldsec != NULL)
1163     {
1164       /* This handles the special SHN_MIPS_{TEXT,DATA} section
1165 	 indices used by MIPS ELF.  */
1166       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1167     }
1168 
1169   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1170      respectively, appear to be a definition rather than reference.  */
1171 
1172   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1173 
1174   olddef = (h->root.type != bfd_link_hash_undefined
1175 	    && h->root.type != bfd_link_hash_undefweak
1176 	    && h->root.type != bfd_link_hash_common);
1177 
1178   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1179      respectively, appear to be a function.  */
1180 
1181   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1182 	     && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1183 
1184   oldfunc = (h->type != STT_NOTYPE
1185 	     && bed->is_function_type (h->type));
1186 
1187   /* If creating a default indirect symbol ("foo" or "foo@") from a
1188      dynamic versioned definition ("foo@@") skip doing so if there is
1189      an existing regular definition with a different type.  We don't
1190      want, for example, a "time" variable in the executable overriding
1191      a "time" function in a shared library.  */
1192   if (pold_alignment == NULL
1193       && newdyn
1194       && newdef
1195       && !olddyn
1196       && (olddef || h->root.type == bfd_link_hash_common)
1197       && ELF_ST_TYPE (sym->st_info) != h->type
1198       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1199       && h->type != STT_NOTYPE
1200       && !(newfunc && oldfunc))
1201     {
1202       *skip = TRUE;
1203       return TRUE;
1204     }
1205 
1206   /* Check TLS symbols.  We don't check undefined symbols introduced
1207      by "ld -u" which have no type (and oldbfd NULL), and we don't
1208      check symbols from plugins because they also have no type.  */
1209   if (oldbfd != NULL
1210       && (oldbfd->flags & BFD_PLUGIN) == 0
1211       && (abfd->flags & BFD_PLUGIN) == 0
1212       && ELF_ST_TYPE (sym->st_info) != h->type
1213       && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1214     {
1215       bfd *ntbfd, *tbfd;
1216       bfd_boolean ntdef, tdef;
1217       asection *ntsec, *tsec;
1218 
1219       if (h->type == STT_TLS)
1220 	{
1221 	  ntbfd = abfd;
1222 	  ntsec = sec;
1223 	  ntdef = newdef;
1224 	  tbfd = oldbfd;
1225 	  tsec = oldsec;
1226 	  tdef = olddef;
1227 	}
1228       else
1229 	{
1230 	  ntbfd = oldbfd;
1231 	  ntsec = oldsec;
1232 	  ntdef = olddef;
1233 	  tbfd = abfd;
1234 	  tsec = sec;
1235 	  tdef = newdef;
1236 	}
1237 
1238       if (tdef && ntdef)
1239 	(*_bfd_error_handler)
1240 	  (_("%s: TLS definition in %B section %A "
1241 	     "mismatches non-TLS definition in %B section %A"),
1242 	   tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1243       else if (!tdef && !ntdef)
1244 	(*_bfd_error_handler)
1245 	  (_("%s: TLS reference in %B "
1246 	     "mismatches non-TLS reference in %B"),
1247 	   tbfd, ntbfd, h->root.root.string);
1248       else if (tdef)
1249 	(*_bfd_error_handler)
1250 	  (_("%s: TLS definition in %B section %A "
1251 	     "mismatches non-TLS reference in %B"),
1252 	   tbfd, tsec, ntbfd, h->root.root.string);
1253       else
1254 	(*_bfd_error_handler)
1255 	  (_("%s: TLS reference in %B "
1256 	     "mismatches non-TLS definition in %B section %A"),
1257 	   tbfd, ntbfd, ntsec, h->root.root.string);
1258 
1259       bfd_set_error (bfd_error_bad_value);
1260       return FALSE;
1261     }
1262 
1263   /* If the old symbol has non-default visibility, we ignore the new
1264      definition from a dynamic object.  */
1265   if (newdyn
1266       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1267       && !bfd_is_und_section (sec))
1268     {
1269       *skip = TRUE;
1270       /* Make sure this symbol is dynamic.  */
1271       h->ref_dynamic = 1;
1272       hi->ref_dynamic = 1;
1273       /* A protected symbol has external availability. Make sure it is
1274 	 recorded as dynamic.
1275 
1276 	 FIXME: Should we check type and size for protected symbol?  */
1277       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1278 	return bfd_elf_link_record_dynamic_symbol (info, h);
1279       else
1280 	return TRUE;
1281     }
1282   else if (!newdyn
1283 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1284 	   && h->def_dynamic)
1285     {
1286       /* If the new symbol with non-default visibility comes from a
1287 	 relocatable file and the old definition comes from a dynamic
1288 	 object, we remove the old definition.  */
1289       if (hi->root.type == bfd_link_hash_indirect)
1290 	{
1291 	  /* Handle the case where the old dynamic definition is
1292 	     default versioned.  We need to copy the symbol info from
1293 	     the symbol with default version to the normal one if it
1294 	     was referenced before.  */
1295 	  if (h->ref_regular)
1296 	    {
1297 	      hi->root.type = h->root.type;
1298 	      h->root.type = bfd_link_hash_indirect;
1299 	      (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1300 
1301 	      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1302 	      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1303 		{
1304 		  /* If the new symbol is hidden or internal, completely undo
1305 		     any dynamic link state.  */
1306 		  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1307 		  h->forced_local = 0;
1308 		  h->ref_dynamic = 0;
1309 		}
1310 	      else
1311 		h->ref_dynamic = 1;
1312 
1313 	      h->def_dynamic = 0;
1314 	      /* FIXME: Should we check type and size for protected symbol?  */
1315 	      h->size = 0;
1316 	      h->type = 0;
1317 
1318 	      h = hi;
1319 	    }
1320 	  else
1321 	    h = hi;
1322 	}
1323 
1324       /* If the old symbol was undefined before, then it will still be
1325 	 on the undefs list.  If the new symbol is undefined or
1326 	 common, we can't make it bfd_link_hash_new here, because new
1327 	 undefined or common symbols will be added to the undefs list
1328 	 by _bfd_generic_link_add_one_symbol.  Symbols may not be
1329 	 added twice to the undefs list.  Also, if the new symbol is
1330 	 undefweak then we don't want to lose the strong undef.  */
1331       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1332 	{
1333 	  h->root.type = bfd_link_hash_undefined;
1334 	  h->root.u.undef.abfd = abfd;
1335 	}
1336       else
1337 	{
1338 	  h->root.type = bfd_link_hash_new;
1339 	  h->root.u.undef.abfd = NULL;
1340 	}
1341 
1342       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1343 	{
1344 	  /* If the new symbol is hidden or internal, completely undo
1345 	     any dynamic link state.  */
1346 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1347 	  h->forced_local = 0;
1348 	  h->ref_dynamic = 0;
1349 	}
1350       else
1351 	h->ref_dynamic = 1;
1352       h->def_dynamic = 0;
1353       /* FIXME: Should we check type and size for protected symbol?  */
1354       h->size = 0;
1355       h->type = 0;
1356       return TRUE;
1357     }
1358 
1359   /* If a new weak symbol definition comes from a regular file and the
1360      old symbol comes from a dynamic library, we treat the new one as
1361      strong.  Similarly, an old weak symbol definition from a regular
1362      file is treated as strong when the new symbol comes from a dynamic
1363      library.  Further, an old weak symbol from a dynamic library is
1364      treated as strong if the new symbol is from a dynamic library.
1365      This reflects the way glibc's ld.so works.
1366 
1367      Do this before setting *type_change_ok or *size_change_ok so that
1368      we warn properly when dynamic library symbols are overridden.  */
1369 
1370   if (newdef && !newdyn && olddyn)
1371     newweak = FALSE;
1372   if (olddef && newdyn)
1373     oldweak = FALSE;
1374 
1375   /* Allow changes between different types of function symbol.  */
1376   if (newfunc && oldfunc)
1377     *type_change_ok = TRUE;
1378 
1379   /* It's OK to change the type if either the existing symbol or the
1380      new symbol is weak.  A type change is also OK if the old symbol
1381      is undefined and the new symbol is defined.  */
1382 
1383   if (oldweak
1384       || newweak
1385       || (newdef
1386 	  && h->root.type == bfd_link_hash_undefined))
1387     *type_change_ok = TRUE;
1388 
1389   /* It's OK to change the size if either the existing symbol or the
1390      new symbol is weak, or if the old symbol is undefined.  */
1391 
1392   if (*type_change_ok
1393       || h->root.type == bfd_link_hash_undefined)
1394     *size_change_ok = TRUE;
1395 
1396   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1397      symbol, respectively, appears to be a common symbol in a dynamic
1398      object.  If a symbol appears in an uninitialized section, and is
1399      not weak, and is not a function, then it may be a common symbol
1400      which was resolved when the dynamic object was created.  We want
1401      to treat such symbols specially, because they raise special
1402      considerations when setting the symbol size: if the symbol
1403      appears as a common symbol in a regular object, and the size in
1404      the regular object is larger, we must make sure that we use the
1405      larger size.  This problematic case can always be avoided in C,
1406      but it must be handled correctly when using Fortran shared
1407      libraries.
1408 
1409      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1410      likewise for OLDDYNCOMMON and OLDDEF.
1411 
1412      Note that this test is just a heuristic, and that it is quite
1413      possible to have an uninitialized symbol in a shared object which
1414      is really a definition, rather than a common symbol.  This could
1415      lead to some minor confusion when the symbol really is a common
1416      symbol in some regular object.  However, I think it will be
1417      harmless.  */
1418 
1419   if (newdyn
1420       && newdef
1421       && !newweak
1422       && (sec->flags & SEC_ALLOC) != 0
1423       && (sec->flags & SEC_LOAD) == 0
1424       && sym->st_size > 0
1425       && !newfunc)
1426     newdyncommon = TRUE;
1427   else
1428     newdyncommon = FALSE;
1429 
1430   if (olddyn
1431       && olddef
1432       && h->root.type == bfd_link_hash_defined
1433       && h->def_dynamic
1434       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1435       && (h->root.u.def.section->flags & SEC_LOAD) == 0
1436       && h->size > 0
1437       && !oldfunc)
1438     olddyncommon = TRUE;
1439   else
1440     olddyncommon = FALSE;
1441 
1442   /* We now know everything about the old and new symbols.  We ask the
1443      backend to check if we can merge them.  */
1444   if (bed->merge_symbol != NULL)
1445     {
1446       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1447 	return FALSE;
1448       sec = *psec;
1449     }
1450 
1451   /* If both the old and the new symbols look like common symbols in a
1452      dynamic object, set the size of the symbol to the larger of the
1453      two.  */
1454 
1455   if (olddyncommon
1456       && newdyncommon
1457       && sym->st_size != h->size)
1458     {
1459       /* Since we think we have two common symbols, issue a multiple
1460 	 common warning if desired.  Note that we only warn if the
1461 	 size is different.  If the size is the same, we simply let
1462 	 the old symbol override the new one as normally happens with
1463 	 symbols defined in dynamic objects.  */
1464 
1465       if (! ((*info->callbacks->multiple_common)
1466 	     (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1467 	return FALSE;
1468 
1469       if (sym->st_size > h->size)
1470 	h->size = sym->st_size;
1471 
1472       *size_change_ok = TRUE;
1473     }
1474 
1475   /* If we are looking at a dynamic object, and we have found a
1476      definition, we need to see if the symbol was already defined by
1477      some other object.  If so, we want to use the existing
1478      definition, and we do not want to report a multiple symbol
1479      definition error; we do this by clobbering *PSEC to be
1480      bfd_und_section_ptr.
1481 
1482      We treat a common symbol as a definition if the symbol in the
1483      shared library is a function, since common symbols always
1484      represent variables; this can cause confusion in principle, but
1485      any such confusion would seem to indicate an erroneous program or
1486      shared library.  We also permit a common symbol in a regular
1487      object to override a weak symbol in a shared object.  A common
1488      symbol in executable also overrides a symbol in a shared object.  */
1489 
1490   if (newdyn
1491       && newdef
1492       && (olddef
1493 	  || (h->root.type == bfd_link_hash_common
1494 	      && (newweak
1495 		  || newfunc
1496 		  || (!olddyn && bfd_link_executable (info))))))
1497     {
1498       *override = TRUE;
1499       newdef = FALSE;
1500       newdyncommon = FALSE;
1501 
1502       *psec = sec = bfd_und_section_ptr;
1503       *size_change_ok = TRUE;
1504 
1505       /* If we get here when the old symbol is a common symbol, then
1506 	 we are explicitly letting it override a weak symbol or
1507 	 function in a dynamic object, and we don't want to warn about
1508 	 a type change.  If the old symbol is a defined symbol, a type
1509 	 change warning may still be appropriate.  */
1510 
1511       if (h->root.type == bfd_link_hash_common)
1512 	*type_change_ok = TRUE;
1513     }
1514 
1515   /* Handle the special case of an old common symbol merging with a
1516      new symbol which looks like a common symbol in a shared object.
1517      We change *PSEC and *PVALUE to make the new symbol look like a
1518      common symbol, and let _bfd_generic_link_add_one_symbol do the
1519      right thing.  */
1520 
1521   if (newdyncommon
1522       && h->root.type == bfd_link_hash_common)
1523     {
1524       *override = TRUE;
1525       newdef = FALSE;
1526       newdyncommon = FALSE;
1527       *pvalue = sym->st_size;
1528       *psec = sec = bed->common_section (oldsec);
1529       *size_change_ok = TRUE;
1530     }
1531 
1532   /* Skip weak definitions of symbols that are already defined.  */
1533   if (newdef && olddef && newweak)
1534     {
1535       /* Don't skip new non-IR weak syms.  */
1536       if (!(oldbfd != NULL
1537 	    && (oldbfd->flags & BFD_PLUGIN) != 0
1538 	    && (abfd->flags & BFD_PLUGIN) == 0))
1539 	{
1540 	  newdef = FALSE;
1541 	  *skip = TRUE;
1542 	}
1543 
1544       /* Merge st_other.  If the symbol already has a dynamic index,
1545 	 but visibility says it should not be visible, turn it into a
1546 	 local symbol.  */
1547       elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1548       if (h->dynindx != -1)
1549 	switch (ELF_ST_VISIBILITY (h->other))
1550 	  {
1551 	  case STV_INTERNAL:
1552 	  case STV_HIDDEN:
1553 	    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1554 	    break;
1555 	  }
1556     }
1557 
1558   /* If the old symbol is from a dynamic object, and the new symbol is
1559      a definition which is not from a dynamic object, then the new
1560      symbol overrides the old symbol.  Symbols from regular files
1561      always take precedence over symbols from dynamic objects, even if
1562      they are defined after the dynamic object in the link.
1563 
1564      As above, we again permit a common symbol in a regular object to
1565      override a definition in a shared object if the shared object
1566      symbol is a function or is weak.  */
1567 
1568   flip = NULL;
1569   if (!newdyn
1570       && (newdef
1571 	  || (bfd_is_com_section (sec)
1572 	      && (oldweak || oldfunc)))
1573       && olddyn
1574       && olddef
1575       && h->def_dynamic)
1576     {
1577       /* Change the hash table entry to undefined, and let
1578 	 _bfd_generic_link_add_one_symbol do the right thing with the
1579 	 new definition.  */
1580 
1581       h->root.type = bfd_link_hash_undefined;
1582       h->root.u.undef.abfd = h->root.u.def.section->owner;
1583       *size_change_ok = TRUE;
1584 
1585       olddef = FALSE;
1586       olddyncommon = FALSE;
1587 
1588       /* We again permit a type change when a common symbol may be
1589 	 overriding a function.  */
1590 
1591       if (bfd_is_com_section (sec))
1592 	{
1593 	  if (oldfunc)
1594 	    {
1595 	      /* If a common symbol overrides a function, make sure
1596 		 that it isn't defined dynamically nor has type
1597 		 function.  */
1598 	      h->def_dynamic = 0;
1599 	      h->type = STT_NOTYPE;
1600 	    }
1601 	  *type_change_ok = TRUE;
1602 	}
1603 
1604       if (hi->root.type == bfd_link_hash_indirect)
1605 	flip = hi;
1606       else
1607 	/* This union may have been set to be non-NULL when this symbol
1608 	   was seen in a dynamic object.  We must force the union to be
1609 	   NULL, so that it is correct for a regular symbol.  */
1610 	h->verinfo.vertree = NULL;
1611     }
1612 
1613   /* Handle the special case of a new common symbol merging with an
1614      old symbol that looks like it might be a common symbol defined in
1615      a shared object.  Note that we have already handled the case in
1616      which a new common symbol should simply override the definition
1617      in the shared library.  */
1618 
1619   if (! newdyn
1620       && bfd_is_com_section (sec)
1621       && olddyncommon)
1622     {
1623       /* It would be best if we could set the hash table entry to a
1624 	 common symbol, but we don't know what to use for the section
1625 	 or the alignment.  */
1626       if (! ((*info->callbacks->multiple_common)
1627 	     (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1628 	return FALSE;
1629 
1630       /* If the presumed common symbol in the dynamic object is
1631 	 larger, pretend that the new symbol has its size.  */
1632 
1633       if (h->size > *pvalue)
1634 	*pvalue = h->size;
1635 
1636       /* We need to remember the alignment required by the symbol
1637 	 in the dynamic object.  */
1638       BFD_ASSERT (pold_alignment);
1639       *pold_alignment = h->root.u.def.section->alignment_power;
1640 
1641       olddef = FALSE;
1642       olddyncommon = FALSE;
1643 
1644       h->root.type = bfd_link_hash_undefined;
1645       h->root.u.undef.abfd = h->root.u.def.section->owner;
1646 
1647       *size_change_ok = TRUE;
1648       *type_change_ok = TRUE;
1649 
1650       if (hi->root.type == bfd_link_hash_indirect)
1651 	flip = hi;
1652       else
1653 	h->verinfo.vertree = NULL;
1654     }
1655 
1656   if (flip != NULL)
1657     {
1658       /* Handle the case where we had a versioned symbol in a dynamic
1659 	 library and now find a definition in a normal object.  In this
1660 	 case, we make the versioned symbol point to the normal one.  */
1661       flip->root.type = h->root.type;
1662       flip->root.u.undef.abfd = h->root.u.undef.abfd;
1663       h->root.type = bfd_link_hash_indirect;
1664       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1665       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1666       if (h->def_dynamic)
1667 	{
1668 	  h->def_dynamic = 0;
1669 	  flip->ref_dynamic = 1;
1670 	}
1671     }
1672 
1673   return TRUE;
1674 }
1675 
1676 /* This function is called to create an indirect symbol from the
1677    default for the symbol with the default version if needed. The
1678    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1679    set DYNSYM if the new indirect symbol is dynamic.  */
1680 
1681 static bfd_boolean
1682 _bfd_elf_add_default_symbol (bfd *abfd,
1683 			     struct bfd_link_info *info,
1684 			     struct elf_link_hash_entry *h,
1685 			     const char *name,
1686 			     Elf_Internal_Sym *sym,
1687 			     asection *sec,
1688 			     bfd_vma value,
1689 			     bfd **poldbfd,
1690 			     bfd_boolean *dynsym)
1691 {
1692   bfd_boolean type_change_ok;
1693   bfd_boolean size_change_ok;
1694   bfd_boolean skip;
1695   char *shortname;
1696   struct elf_link_hash_entry *hi;
1697   struct bfd_link_hash_entry *bh;
1698   const struct elf_backend_data *bed;
1699   bfd_boolean collect;
1700   bfd_boolean dynamic;
1701   bfd_boolean override;
1702   char *p;
1703   size_t len, shortlen;
1704   asection *tmp_sec;
1705   bfd_boolean matched;
1706 
1707   if (h->versioned == unversioned || h->versioned == versioned_hidden)
1708     return TRUE;
1709 
1710   /* If this symbol has a version, and it is the default version, we
1711      create an indirect symbol from the default name to the fully
1712      decorated name.  This will cause external references which do not
1713      specify a version to be bound to this version of the symbol.  */
1714   p = strchr (name, ELF_VER_CHR);
1715   if (h->versioned == unknown)
1716     {
1717       if (p == NULL)
1718 	{
1719 	  h->versioned = unversioned;
1720 	  return TRUE;
1721 	}
1722       else
1723 	{
1724 	  if (p[1] != ELF_VER_CHR)
1725 	    {
1726 	      h->versioned = versioned_hidden;
1727 	      return TRUE;
1728 	    }
1729 	  else
1730 	    h->versioned = versioned;
1731 	}
1732     }
1733   else
1734     {
1735       /* PR ld/19073: We may see an unversioned definition after the
1736 	 default version.  */
1737       if (p == NULL)
1738 	return TRUE;
1739     }
1740 
1741   bed = get_elf_backend_data (abfd);
1742   collect = bed->collect;
1743   dynamic = (abfd->flags & DYNAMIC) != 0;
1744 
1745   shortlen = p - name;
1746   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1747   if (shortname == NULL)
1748     return FALSE;
1749   memcpy (shortname, name, shortlen);
1750   shortname[shortlen] = '\0';
1751 
1752   /* We are going to create a new symbol.  Merge it with any existing
1753      symbol with this name.  For the purposes of the merge, act as
1754      though we were defining the symbol we just defined, although we
1755      actually going to define an indirect symbol.  */
1756   type_change_ok = FALSE;
1757   size_change_ok = FALSE;
1758   matched = TRUE;
1759   tmp_sec = sec;
1760   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1761 			      &hi, poldbfd, NULL, NULL, &skip, &override,
1762 			      &type_change_ok, &size_change_ok, &matched))
1763     return FALSE;
1764 
1765   if (skip)
1766     goto nondefault;
1767 
1768   if (hi->def_regular)
1769     {
1770       /* If the undecorated symbol will have a version added by a
1771 	 script different to H, then don't indirect to/from the
1772 	 undecorated symbol.  This isn't ideal because we may not yet
1773 	 have seen symbol versions, if given by a script on the
1774 	 command line rather than via --version-script.  */
1775       if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1776 	{
1777 	  bfd_boolean hide;
1778 
1779 	  hi->verinfo.vertree
1780 	    = bfd_find_version_for_sym (info->version_info,
1781 					hi->root.root.string, &hide);
1782 	  if (hi->verinfo.vertree != NULL && hide)
1783 	    {
1784 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1785 	      goto nondefault;
1786 	    }
1787 	}
1788       if (hi->verinfo.vertree != NULL
1789 	  && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1790 	goto nondefault;
1791     }
1792 
1793   if (! override)
1794     {
1795       /* Add the default symbol if not performing a relocatable link.  */
1796       if (! bfd_link_relocatable (info))
1797 	{
1798 	  bh = &hi->root;
1799 	  if (! (_bfd_generic_link_add_one_symbol
1800 		 (info, abfd, shortname, BSF_INDIRECT,
1801 		  bfd_ind_section_ptr,
1802 		  0, name, FALSE, collect, &bh)))
1803 	    return FALSE;
1804 	  hi = (struct elf_link_hash_entry *) bh;
1805 	}
1806     }
1807   else
1808     {
1809       /* In this case the symbol named SHORTNAME is overriding the
1810 	 indirect symbol we want to add.  We were planning on making
1811 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
1812 	 is the name without a version.  NAME is the fully versioned
1813 	 name, and it is the default version.
1814 
1815 	 Overriding means that we already saw a definition for the
1816 	 symbol SHORTNAME in a regular object, and it is overriding
1817 	 the symbol defined in the dynamic object.
1818 
1819 	 When this happens, we actually want to change NAME, the
1820 	 symbol we just added, to refer to SHORTNAME.  This will cause
1821 	 references to NAME in the shared object to become references
1822 	 to SHORTNAME in the regular object.  This is what we expect
1823 	 when we override a function in a shared object: that the
1824 	 references in the shared object will be mapped to the
1825 	 definition in the regular object.  */
1826 
1827       while (hi->root.type == bfd_link_hash_indirect
1828 	     || hi->root.type == bfd_link_hash_warning)
1829 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1830 
1831       h->root.type = bfd_link_hash_indirect;
1832       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1833       if (h->def_dynamic)
1834 	{
1835 	  h->def_dynamic = 0;
1836 	  hi->ref_dynamic = 1;
1837 	  if (hi->ref_regular
1838 	      || hi->def_regular)
1839 	    {
1840 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1841 		return FALSE;
1842 	    }
1843 	}
1844 
1845       /* Now set HI to H, so that the following code will set the
1846 	 other fields correctly.  */
1847       hi = h;
1848     }
1849 
1850   /* Check if HI is a warning symbol.  */
1851   if (hi->root.type == bfd_link_hash_warning)
1852     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1853 
1854   /* If there is a duplicate definition somewhere, then HI may not
1855      point to an indirect symbol.  We will have reported an error to
1856      the user in that case.  */
1857 
1858   if (hi->root.type == bfd_link_hash_indirect)
1859     {
1860       struct elf_link_hash_entry *ht;
1861 
1862       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1863       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1864 
1865       /* A reference to the SHORTNAME symbol from a dynamic library
1866 	 will be satisfied by the versioned symbol at runtime.  In
1867 	 effect, we have a reference to the versioned symbol.  */
1868       ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1869       hi->dynamic_def |= ht->dynamic_def;
1870 
1871       /* See if the new flags lead us to realize that the symbol must
1872 	 be dynamic.  */
1873       if (! *dynsym)
1874 	{
1875 	  if (! dynamic)
1876 	    {
1877 	      if (! bfd_link_executable (info)
1878 		  || hi->def_dynamic
1879 		  || hi->ref_dynamic)
1880 		*dynsym = TRUE;
1881 	    }
1882 	  else
1883 	    {
1884 	      if (hi->ref_regular)
1885 		*dynsym = TRUE;
1886 	    }
1887 	}
1888     }
1889 
1890   /* We also need to define an indirection from the nondefault version
1891      of the symbol.  */
1892 
1893 nondefault:
1894   len = strlen (name);
1895   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1896   if (shortname == NULL)
1897     return FALSE;
1898   memcpy (shortname, name, shortlen);
1899   memcpy (shortname + shortlen, p + 1, len - shortlen);
1900 
1901   /* Once again, merge with any existing symbol.  */
1902   type_change_ok = FALSE;
1903   size_change_ok = FALSE;
1904   tmp_sec = sec;
1905   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1906 			      &hi, poldbfd, NULL, NULL, &skip, &override,
1907 			      &type_change_ok, &size_change_ok, &matched))
1908     return FALSE;
1909 
1910   if (skip)
1911     return TRUE;
1912 
1913   if (override)
1914     {
1915       /* Here SHORTNAME is a versioned name, so we don't expect to see
1916 	 the type of override we do in the case above unless it is
1917 	 overridden by a versioned definition.  */
1918       if (hi->root.type != bfd_link_hash_defined
1919 	  && hi->root.type != bfd_link_hash_defweak)
1920 	(*_bfd_error_handler)
1921 	  (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1922 	   abfd, shortname);
1923     }
1924   else
1925     {
1926       bh = &hi->root;
1927       if (! (_bfd_generic_link_add_one_symbol
1928 	     (info, abfd, shortname, BSF_INDIRECT,
1929 	      bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1930 	return FALSE;
1931       hi = (struct elf_link_hash_entry *) bh;
1932 
1933       /* If there is a duplicate definition somewhere, then HI may not
1934 	 point to an indirect symbol.  We will have reported an error
1935 	 to the user in that case.  */
1936 
1937       if (hi->root.type == bfd_link_hash_indirect)
1938 	{
1939 	  (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1940 	  h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1941 	  hi->dynamic_def |= h->dynamic_def;
1942 
1943 	  /* See if the new flags lead us to realize that the symbol
1944 	     must be dynamic.  */
1945 	  if (! *dynsym)
1946 	    {
1947 	      if (! dynamic)
1948 		{
1949 		  if (! bfd_link_executable (info)
1950 		      || hi->ref_dynamic)
1951 		    *dynsym = TRUE;
1952 		}
1953 	      else
1954 		{
1955 		  if (hi->ref_regular)
1956 		    *dynsym = TRUE;
1957 		}
1958 	    }
1959 	}
1960     }
1961 
1962   return TRUE;
1963 }
1964 
1965 /* This routine is used to export all defined symbols into the dynamic
1966    symbol table.  It is called via elf_link_hash_traverse.  */
1967 
1968 static bfd_boolean
1969 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1970 {
1971   struct elf_info_failed *eif = (struct elf_info_failed *) data;
1972 
1973   /* Ignore indirect symbols.  These are added by the versioning code.  */
1974   if (h->root.type == bfd_link_hash_indirect)
1975     return TRUE;
1976 
1977   /* Ignore this if we won't export it.  */
1978   if (!eif->info->export_dynamic && !h->dynamic)
1979     return TRUE;
1980 
1981   if (h->dynindx == -1
1982       && (h->def_regular || h->ref_regular)
1983       && ! bfd_hide_sym_by_version (eif->info->version_info,
1984 				    h->root.root.string))
1985     {
1986       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1987 	{
1988 	  eif->failed = TRUE;
1989 	  return FALSE;
1990 	}
1991     }
1992 
1993   return TRUE;
1994 }
1995 
1996 /* Look through the symbols which are defined in other shared
1997    libraries and referenced here.  Update the list of version
1998    dependencies.  This will be put into the .gnu.version_r section.
1999    This function is called via elf_link_hash_traverse.  */
2000 
2001 static bfd_boolean
2002 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2003 					 void *data)
2004 {
2005   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2006   Elf_Internal_Verneed *t;
2007   Elf_Internal_Vernaux *a;
2008   bfd_size_type amt;
2009 
2010   /* We only care about symbols defined in shared objects with version
2011      information.  */
2012   if (!h->def_dynamic
2013       || h->def_regular
2014       || h->dynindx == -1
2015       || h->verinfo.verdef == NULL
2016       || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2017 	  & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2018     return TRUE;
2019 
2020   /* See if we already know about this version.  */
2021   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2022        t != NULL;
2023        t = t->vn_nextref)
2024     {
2025       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2026 	continue;
2027 
2028       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2029 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2030 	  return TRUE;
2031 
2032       break;
2033     }
2034 
2035   /* This is a new version.  Add it to tree we are building.  */
2036 
2037   if (t == NULL)
2038     {
2039       amt = sizeof *t;
2040       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2041       if (t == NULL)
2042 	{
2043 	  rinfo->failed = TRUE;
2044 	  return FALSE;
2045 	}
2046 
2047       t->vn_bfd = h->verinfo.verdef->vd_bfd;
2048       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2049       elf_tdata (rinfo->info->output_bfd)->verref = t;
2050     }
2051 
2052   amt = sizeof *a;
2053   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2054   if (a == NULL)
2055     {
2056       rinfo->failed = TRUE;
2057       return FALSE;
2058     }
2059 
2060   /* Note that we are copying a string pointer here, and testing it
2061      above.  If bfd_elf_string_from_elf_section is ever changed to
2062      discard the string data when low in memory, this will have to be
2063      fixed.  */
2064   a->vna_nodename = h->verinfo.verdef->vd_nodename;
2065 
2066   a->vna_flags = h->verinfo.verdef->vd_flags;
2067   a->vna_nextptr = t->vn_auxptr;
2068 
2069   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2070   ++rinfo->vers;
2071 
2072   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2073 
2074   t->vn_auxptr = a;
2075 
2076   return TRUE;
2077 }
2078 
2079 /* Figure out appropriate versions for all the symbols.  We may not
2080    have the version number script until we have read all of the input
2081    files, so until that point we don't know which symbols should be
2082    local.  This function is called via elf_link_hash_traverse.  */
2083 
2084 static bfd_boolean
2085 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2086 {
2087   struct elf_info_failed *sinfo;
2088   struct bfd_link_info *info;
2089   const struct elf_backend_data *bed;
2090   struct elf_info_failed eif;
2091   char *p;
2092   bfd_size_type amt;
2093 
2094   sinfo = (struct elf_info_failed *) data;
2095   info = sinfo->info;
2096 
2097   /* Fix the symbol flags.  */
2098   eif.failed = FALSE;
2099   eif.info = info;
2100   if (! _bfd_elf_fix_symbol_flags (h, &eif))
2101     {
2102       if (eif.failed)
2103 	sinfo->failed = TRUE;
2104       return FALSE;
2105     }
2106 
2107   /* We only need version numbers for symbols defined in regular
2108      objects.  */
2109   if (!h->def_regular)
2110     return TRUE;
2111 
2112   bed = get_elf_backend_data (info->output_bfd);
2113   p = strchr (h->root.root.string, ELF_VER_CHR);
2114   if (p != NULL && h->verinfo.vertree == NULL)
2115     {
2116       struct bfd_elf_version_tree *t;
2117 
2118       ++p;
2119       if (*p == ELF_VER_CHR)
2120 	++p;
2121 
2122       /* If there is no version string, we can just return out.  */
2123       if (*p == '\0')
2124 	return TRUE;
2125 
2126       /* Look for the version.  If we find it, it is no longer weak.  */
2127       for (t = sinfo->info->version_info; t != NULL; t = t->next)
2128 	{
2129 	  if (strcmp (t->name, p) == 0)
2130 	    {
2131 	      size_t len;
2132 	      char *alc;
2133 	      struct bfd_elf_version_expr *d;
2134 
2135 	      len = p - h->root.root.string;
2136 	      alc = (char *) bfd_malloc (len);
2137 	      if (alc == NULL)
2138 		{
2139 		  sinfo->failed = TRUE;
2140 		  return FALSE;
2141 		}
2142 	      memcpy (alc, h->root.root.string, len - 1);
2143 	      alc[len - 1] = '\0';
2144 	      if (alc[len - 2] == ELF_VER_CHR)
2145 		alc[len - 2] = '\0';
2146 
2147 	      h->verinfo.vertree = t;
2148 	      t->used = TRUE;
2149 	      d = NULL;
2150 
2151 	      if (t->globals.list != NULL)
2152 		d = (*t->match) (&t->globals, NULL, alc);
2153 
2154 	      /* See if there is anything to force this symbol to
2155 		 local scope.  */
2156 	      if (d == NULL && t->locals.list != NULL)
2157 		{
2158 		  d = (*t->match) (&t->locals, NULL, alc);
2159 		  if (d != NULL
2160 		      && h->dynindx != -1
2161 		      && ! info->export_dynamic)
2162 		    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2163 		}
2164 
2165 	      free (alc);
2166 	      break;
2167 	    }
2168 	}
2169 
2170       /* If we are building an application, we need to create a
2171 	 version node for this version.  */
2172       if (t == NULL && bfd_link_executable (info))
2173 	{
2174 	  struct bfd_elf_version_tree **pp;
2175 	  int version_index;
2176 
2177 	  /* If we aren't going to export this symbol, we don't need
2178 	     to worry about it.  */
2179 	  if (h->dynindx == -1)
2180 	    return TRUE;
2181 
2182 	  amt = sizeof *t;
2183 	  t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2184 	  if (t == NULL)
2185 	    {
2186 	      sinfo->failed = TRUE;
2187 	      return FALSE;
2188 	    }
2189 
2190 	  t->name = p;
2191 	  t->name_indx = (unsigned int) -1;
2192 	  t->used = TRUE;
2193 
2194 	  version_index = 1;
2195 	  /* Don't count anonymous version tag.  */
2196 	  if (sinfo->info->version_info != NULL
2197 	      && sinfo->info->version_info->vernum == 0)
2198 	    version_index = 0;
2199 	  for (pp = &sinfo->info->version_info;
2200 	       *pp != NULL;
2201 	       pp = &(*pp)->next)
2202 	    ++version_index;
2203 	  t->vernum = version_index;
2204 
2205 	  *pp = t;
2206 
2207 	  h->verinfo.vertree = t;
2208 	}
2209       else if (t == NULL)
2210 	{
2211 	  /* We could not find the version for a symbol when
2212 	     generating a shared archive.  Return an error.  */
2213 	  (*_bfd_error_handler)
2214 	    (_("%B: version node not found for symbol %s"),
2215 	     info->output_bfd, h->root.root.string);
2216 	  bfd_set_error (bfd_error_bad_value);
2217 	  sinfo->failed = TRUE;
2218 	  return FALSE;
2219 	}
2220     }
2221 
2222   /* If we don't have a version for this symbol, see if we can find
2223      something.  */
2224   if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2225     {
2226       bfd_boolean hide;
2227 
2228       h->verinfo.vertree
2229 	= bfd_find_version_for_sym (sinfo->info->version_info,
2230 				    h->root.root.string, &hide);
2231       if (h->verinfo.vertree != NULL && hide)
2232 	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
2233     }
2234 
2235   return TRUE;
2236 }
2237 
2238 /* Read and swap the relocs from the section indicated by SHDR.  This
2239    may be either a REL or a RELA section.  The relocations are
2240    translated into RELA relocations and stored in INTERNAL_RELOCS,
2241    which should have already been allocated to contain enough space.
2242    The EXTERNAL_RELOCS are a buffer where the external form of the
2243    relocations should be stored.
2244 
2245    Returns FALSE if something goes wrong.  */
2246 
2247 static bfd_boolean
2248 elf_link_read_relocs_from_section (bfd *abfd,
2249 				   asection *sec,
2250 				   Elf_Internal_Shdr *shdr,
2251 				   void *external_relocs,
2252 				   Elf_Internal_Rela *internal_relocs)
2253 {
2254   const struct elf_backend_data *bed;
2255   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2256   const bfd_byte *erela;
2257   const bfd_byte *erelaend;
2258   Elf_Internal_Rela *irela;
2259   Elf_Internal_Shdr *symtab_hdr;
2260   size_t nsyms;
2261 
2262   /* Position ourselves at the start of the section.  */
2263   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2264     return FALSE;
2265 
2266   /* Read the relocations.  */
2267   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2268     return FALSE;
2269 
2270   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2271   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2272 
2273   bed = get_elf_backend_data (abfd);
2274 
2275   /* Convert the external relocations to the internal format.  */
2276   if (shdr->sh_entsize == bed->s->sizeof_rel)
2277     swap_in = bed->s->swap_reloc_in;
2278   else if (shdr->sh_entsize == bed->s->sizeof_rela)
2279     swap_in = bed->s->swap_reloca_in;
2280   else
2281     {
2282       bfd_set_error (bfd_error_wrong_format);
2283       return FALSE;
2284     }
2285 
2286   erela = (const bfd_byte *) external_relocs;
2287   erelaend = erela + shdr->sh_size;
2288   irela = internal_relocs;
2289   while (erela < erelaend)
2290     {
2291       bfd_vma r_symndx;
2292 
2293       (*swap_in) (abfd, erela, irela);
2294       r_symndx = ELF32_R_SYM (irela->r_info);
2295       if (bed->s->arch_size == 64)
2296 	r_symndx >>= 24;
2297       if (nsyms > 0)
2298 	{
2299 	  if ((size_t) r_symndx >= nsyms)
2300 	    {
2301 	      (*_bfd_error_handler)
2302 		(_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2303 		   " for offset 0x%lx in section `%A'"),
2304 		 abfd, sec,
2305 		 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2306 	      bfd_set_error (bfd_error_bad_value);
2307 	      return FALSE;
2308 	    }
2309 	}
2310       else if (r_symndx != STN_UNDEF)
2311 	{
2312 	  (*_bfd_error_handler)
2313 	    (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2314 	       " when the object file has no symbol table"),
2315 	     abfd, sec,
2316 	     (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2317 	  bfd_set_error (bfd_error_bad_value);
2318 	  return FALSE;
2319 	}
2320       irela += bed->s->int_rels_per_ext_rel;
2321       erela += shdr->sh_entsize;
2322     }
2323 
2324   return TRUE;
2325 }
2326 
2327 /* Read and swap the relocs for a section O.  They may have been
2328    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2329    not NULL, they are used as buffers to read into.  They are known to
2330    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2331    the return value is allocated using either malloc or bfd_alloc,
2332    according to the KEEP_MEMORY argument.  If O has two relocation
2333    sections (both REL and RELA relocations), then the REL_HDR
2334    relocations will appear first in INTERNAL_RELOCS, followed by the
2335    RELA_HDR relocations.  */
2336 
2337 Elf_Internal_Rela *
2338 _bfd_elf_link_read_relocs (bfd *abfd,
2339 			   asection *o,
2340 			   void *external_relocs,
2341 			   Elf_Internal_Rela *internal_relocs,
2342 			   bfd_boolean keep_memory)
2343 {
2344   void *alloc1 = NULL;
2345   Elf_Internal_Rela *alloc2 = NULL;
2346   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2347   struct bfd_elf_section_data *esdo = elf_section_data (o);
2348   Elf_Internal_Rela *internal_rela_relocs;
2349 
2350   if (esdo->relocs != NULL)
2351     return esdo->relocs;
2352 
2353   if (o->reloc_count == 0)
2354     return NULL;
2355 
2356   if (internal_relocs == NULL)
2357     {
2358       bfd_size_type size;
2359 
2360       size = o->reloc_count;
2361       size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2362       if (keep_memory)
2363 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2364       else
2365 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2366       if (internal_relocs == NULL)
2367 	goto error_return;
2368     }
2369 
2370   if (external_relocs == NULL)
2371     {
2372       bfd_size_type size = 0;
2373 
2374       if (esdo->rel.hdr)
2375 	size += esdo->rel.hdr->sh_size;
2376       if (esdo->rela.hdr)
2377 	size += esdo->rela.hdr->sh_size;
2378 
2379       alloc1 = bfd_malloc (size);
2380       if (alloc1 == NULL)
2381 	goto error_return;
2382       external_relocs = alloc1;
2383     }
2384 
2385   internal_rela_relocs = internal_relocs;
2386   if (esdo->rel.hdr)
2387     {
2388       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2389 					      external_relocs,
2390 					      internal_relocs))
2391 	goto error_return;
2392       external_relocs = (((bfd_byte *) external_relocs)
2393 			 + esdo->rel.hdr->sh_size);
2394       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2395 			       * bed->s->int_rels_per_ext_rel);
2396     }
2397 
2398   if (esdo->rela.hdr
2399       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2400 					      external_relocs,
2401 					      internal_rela_relocs)))
2402     goto error_return;
2403 
2404   /* Cache the results for next time, if we can.  */
2405   if (keep_memory)
2406     esdo->relocs = internal_relocs;
2407 
2408   if (alloc1 != NULL)
2409     free (alloc1);
2410 
2411   /* Don't free alloc2, since if it was allocated we are passing it
2412      back (under the name of internal_relocs).  */
2413 
2414   return internal_relocs;
2415 
2416  error_return:
2417   if (alloc1 != NULL)
2418     free (alloc1);
2419   if (alloc2 != NULL)
2420     {
2421       if (keep_memory)
2422 	bfd_release (abfd, alloc2);
2423       else
2424 	free (alloc2);
2425     }
2426   return NULL;
2427 }
2428 
2429 /* Compute the size of, and allocate space for, REL_HDR which is the
2430    section header for a section containing relocations for O.  */
2431 
2432 static bfd_boolean
2433 _bfd_elf_link_size_reloc_section (bfd *abfd,
2434 				  struct bfd_elf_section_reloc_data *reldata)
2435 {
2436   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2437 
2438   /* That allows us to calculate the size of the section.  */
2439   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2440 
2441   /* The contents field must last into write_object_contents, so we
2442      allocate it with bfd_alloc rather than malloc.  Also since we
2443      cannot be sure that the contents will actually be filled in,
2444      we zero the allocated space.  */
2445   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2446   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2447     return FALSE;
2448 
2449   if (reldata->hashes == NULL && reldata->count)
2450     {
2451       struct elf_link_hash_entry **p;
2452 
2453       p = ((struct elf_link_hash_entry **)
2454 	   bfd_zmalloc (reldata->count * sizeof (*p)));
2455       if (p == NULL)
2456 	return FALSE;
2457 
2458       reldata->hashes = p;
2459     }
2460 
2461   return TRUE;
2462 }
2463 
2464 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2465    originated from the section given by INPUT_REL_HDR) to the
2466    OUTPUT_BFD.  */
2467 
2468 bfd_boolean
2469 _bfd_elf_link_output_relocs (bfd *output_bfd,
2470 			     asection *input_section,
2471 			     Elf_Internal_Shdr *input_rel_hdr,
2472 			     Elf_Internal_Rela *internal_relocs,
2473 			     struct elf_link_hash_entry **rel_hash
2474 			       ATTRIBUTE_UNUSED)
2475 {
2476   Elf_Internal_Rela *irela;
2477   Elf_Internal_Rela *irelaend;
2478   bfd_byte *erel;
2479   struct bfd_elf_section_reloc_data *output_reldata;
2480   asection *output_section;
2481   const struct elf_backend_data *bed;
2482   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2483   struct bfd_elf_section_data *esdo;
2484 
2485   output_section = input_section->output_section;
2486 
2487   bed = get_elf_backend_data (output_bfd);
2488   esdo = elf_section_data (output_section);
2489   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2490     {
2491       output_reldata = &esdo->rel;
2492       swap_out = bed->s->swap_reloc_out;
2493     }
2494   else if (esdo->rela.hdr
2495 	   && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2496     {
2497       output_reldata = &esdo->rela;
2498       swap_out = bed->s->swap_reloca_out;
2499     }
2500   else
2501     {
2502       (*_bfd_error_handler)
2503 	(_("%B: relocation size mismatch in %B section %A"),
2504 	 output_bfd, input_section->owner, input_section);
2505       bfd_set_error (bfd_error_wrong_format);
2506       return FALSE;
2507     }
2508 
2509   erel = output_reldata->hdr->contents;
2510   erel += output_reldata->count * input_rel_hdr->sh_entsize;
2511   irela = internal_relocs;
2512   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2513 		      * bed->s->int_rels_per_ext_rel);
2514   while (irela < irelaend)
2515     {
2516       (*swap_out) (output_bfd, irela, erel);
2517       irela += bed->s->int_rels_per_ext_rel;
2518       erel += input_rel_hdr->sh_entsize;
2519     }
2520 
2521   /* Bump the counter, so that we know where to add the next set of
2522      relocations.  */
2523   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2524 
2525   return TRUE;
2526 }
2527 
2528 /* Make weak undefined symbols in PIE dynamic.  */
2529 
2530 bfd_boolean
2531 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2532 				 struct elf_link_hash_entry *h)
2533 {
2534   if (bfd_link_pie (info)
2535       && h->dynindx == -1
2536       && h->root.type == bfd_link_hash_undefweak)
2537     return bfd_elf_link_record_dynamic_symbol (info, h);
2538 
2539   return TRUE;
2540 }
2541 
2542 /* Fix up the flags for a symbol.  This handles various cases which
2543    can only be fixed after all the input files are seen.  This is
2544    currently called by both adjust_dynamic_symbol and
2545    assign_sym_version, which is unnecessary but perhaps more robust in
2546    the face of future changes.  */
2547 
2548 static bfd_boolean
2549 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2550 			   struct elf_info_failed *eif)
2551 {
2552   const struct elf_backend_data *bed;
2553 
2554   /* If this symbol was mentioned in a non-ELF file, try to set
2555      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
2556      permit a non-ELF file to correctly refer to a symbol defined in
2557      an ELF dynamic object.  */
2558   if (h->non_elf)
2559     {
2560       while (h->root.type == bfd_link_hash_indirect)
2561 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
2562 
2563       if (h->root.type != bfd_link_hash_defined
2564 	  && h->root.type != bfd_link_hash_defweak)
2565 	{
2566 	  h->ref_regular = 1;
2567 	  h->ref_regular_nonweak = 1;
2568 	}
2569       else
2570 	{
2571 	  if (h->root.u.def.section->owner != NULL
2572 	      && (bfd_get_flavour (h->root.u.def.section->owner)
2573 		  == bfd_target_elf_flavour))
2574 	    {
2575 	      h->ref_regular = 1;
2576 	      h->ref_regular_nonweak = 1;
2577 	    }
2578 	  else
2579 	    h->def_regular = 1;
2580 	}
2581 
2582       if (h->dynindx == -1
2583 	  && (h->def_dynamic
2584 	      || h->ref_dynamic))
2585 	{
2586 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2587 	    {
2588 	      eif->failed = TRUE;
2589 	      return FALSE;
2590 	    }
2591 	}
2592     }
2593   else
2594     {
2595       /* Unfortunately, NON_ELF is only correct if the symbol
2596 	 was first seen in a non-ELF file.  Fortunately, if the symbol
2597 	 was first seen in an ELF file, we're probably OK unless the
2598 	 symbol was defined in a non-ELF file.  Catch that case here.
2599 	 FIXME: We're still in trouble if the symbol was first seen in
2600 	 a dynamic object, and then later in a non-ELF regular object.  */
2601       if ((h->root.type == bfd_link_hash_defined
2602 	   || h->root.type == bfd_link_hash_defweak)
2603 	  && !h->def_regular
2604 	  && (h->root.u.def.section->owner != NULL
2605 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
2606 		 != bfd_target_elf_flavour)
2607 	      : (bfd_is_abs_section (h->root.u.def.section)
2608 		 && !h->def_dynamic)))
2609 	h->def_regular = 1;
2610     }
2611 
2612   /* Backend specific symbol fixup.  */
2613   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2614   if (bed->elf_backend_fixup_symbol
2615       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2616     return FALSE;
2617 
2618   /* If this is a final link, and the symbol was defined as a common
2619      symbol in a regular object file, and there was no definition in
2620      any dynamic object, then the linker will have allocated space for
2621      the symbol in a common section but the DEF_REGULAR
2622      flag will not have been set.  */
2623   if (h->root.type == bfd_link_hash_defined
2624       && !h->def_regular
2625       && h->ref_regular
2626       && !h->def_dynamic
2627       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2628     h->def_regular = 1;
2629 
2630   /* If -Bsymbolic was used (which means to bind references to global
2631      symbols to the definition within the shared object), and this
2632      symbol was defined in a regular object, then it actually doesn't
2633      need a PLT entry.  Likewise, if the symbol has non-default
2634      visibility.  If the symbol has hidden or internal visibility, we
2635      will force it local.  */
2636   if (h->needs_plt
2637       && bfd_link_pic (eif->info)
2638       && is_elf_hash_table (eif->info->hash)
2639       && (SYMBOLIC_BIND (eif->info, h)
2640 	  || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2641       && h->def_regular)
2642     {
2643       bfd_boolean force_local;
2644 
2645       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2646 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2647       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2648     }
2649 
2650   /* If a weak undefined symbol has non-default visibility, we also
2651      hide it from the dynamic linker.  */
2652   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2653       && h->root.type == bfd_link_hash_undefweak)
2654     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2655 
2656   /* If this is a weak defined symbol in a dynamic object, and we know
2657      the real definition in the dynamic object, copy interesting flags
2658      over to the real definition.  */
2659   if (h->u.weakdef != NULL)
2660     {
2661       /* If the real definition is defined by a regular object file,
2662 	 don't do anything special.  See the longer description in
2663 	 _bfd_elf_adjust_dynamic_symbol, below.  */
2664       if (h->u.weakdef->def_regular)
2665 	h->u.weakdef = NULL;
2666       else
2667 	{
2668 	  struct elf_link_hash_entry *weakdef = h->u.weakdef;
2669 
2670 	  while (h->root.type == bfd_link_hash_indirect)
2671 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2672 
2673 	  BFD_ASSERT (h->root.type == bfd_link_hash_defined
2674 		      || h->root.type == bfd_link_hash_defweak);
2675 	  BFD_ASSERT (weakdef->def_dynamic);
2676 	  BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2677 		      || weakdef->root.type == bfd_link_hash_defweak);
2678 	  (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2679 	}
2680     }
2681 
2682   return TRUE;
2683 }
2684 
2685 /* Make the backend pick a good value for a dynamic symbol.  This is
2686    called via elf_link_hash_traverse, and also calls itself
2687    recursively.  */
2688 
2689 static bfd_boolean
2690 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2691 {
2692   struct elf_info_failed *eif = (struct elf_info_failed *) data;
2693   bfd *dynobj;
2694   const struct elf_backend_data *bed;
2695 
2696   if (! is_elf_hash_table (eif->info->hash))
2697     return FALSE;
2698 
2699   /* Ignore indirect symbols.  These are added by the versioning code.  */
2700   if (h->root.type == bfd_link_hash_indirect)
2701     return TRUE;
2702 
2703   /* Fix the symbol flags.  */
2704   if (! _bfd_elf_fix_symbol_flags (h, eif))
2705     return FALSE;
2706 
2707   /* If this symbol does not require a PLT entry, and it is not
2708      defined by a dynamic object, or is not referenced by a regular
2709      object, ignore it.  We do have to handle a weak defined symbol,
2710      even if no regular object refers to it, if we decided to add it
2711      to the dynamic symbol table.  FIXME: Do we normally need to worry
2712      about symbols which are defined by one dynamic object and
2713      referenced by another one?  */
2714   if (!h->needs_plt
2715       && h->type != STT_GNU_IFUNC
2716       && (h->def_regular
2717 	  || !h->def_dynamic
2718 	  || (!h->ref_regular
2719 	      && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2720     {
2721       h->plt = elf_hash_table (eif->info)->init_plt_offset;
2722       return TRUE;
2723     }
2724 
2725   /* If we've already adjusted this symbol, don't do it again.  This
2726      can happen via a recursive call.  */
2727   if (h->dynamic_adjusted)
2728     return TRUE;
2729 
2730   /* Don't look at this symbol again.  Note that we must set this
2731      after checking the above conditions, because we may look at a
2732      symbol once, decide not to do anything, and then get called
2733      recursively later after REF_REGULAR is set below.  */
2734   h->dynamic_adjusted = 1;
2735 
2736   /* If this is a weak definition, and we know a real definition, and
2737      the real symbol is not itself defined by a regular object file,
2738      then get a good value for the real definition.  We handle the
2739      real symbol first, for the convenience of the backend routine.
2740 
2741      Note that there is a confusing case here.  If the real definition
2742      is defined by a regular object file, we don't get the real symbol
2743      from the dynamic object, but we do get the weak symbol.  If the
2744      processor backend uses a COPY reloc, then if some routine in the
2745      dynamic object changes the real symbol, we will not see that
2746      change in the corresponding weak symbol.  This is the way other
2747      ELF linkers work as well, and seems to be a result of the shared
2748      library model.
2749 
2750      I will clarify this issue.  Most SVR4 shared libraries define the
2751      variable _timezone and define timezone as a weak synonym.  The
2752      tzset call changes _timezone.  If you write
2753        extern int timezone;
2754        int _timezone = 5;
2755        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2756      you might expect that, since timezone is a synonym for _timezone,
2757      the same number will print both times.  However, if the processor
2758      backend uses a COPY reloc, then actually timezone will be copied
2759      into your process image, and, since you define _timezone
2760      yourself, _timezone will not.  Thus timezone and _timezone will
2761      wind up at different memory locations.  The tzset call will set
2762      _timezone, leaving timezone unchanged.  */
2763 
2764   if (h->u.weakdef != NULL)
2765     {
2766       /* If we get to this point, there is an implicit reference to
2767 	 H->U.WEAKDEF by a regular object file via the weak symbol H.  */
2768       h->u.weakdef->ref_regular = 1;
2769 
2770       /* Ensure that the backend adjust_dynamic_symbol function sees
2771 	 H->U.WEAKDEF before H by recursively calling ourselves.  */
2772       if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2773 	return FALSE;
2774     }
2775 
2776   /* If a symbol has no type and no size and does not require a PLT
2777      entry, then we are probably about to do the wrong thing here: we
2778      are probably going to create a COPY reloc for an empty object.
2779      This case can arise when a shared object is built with assembly
2780      code, and the assembly code fails to set the symbol type.  */
2781   if (h->size == 0
2782       && h->type == STT_NOTYPE
2783       && !h->needs_plt)
2784     (*_bfd_error_handler)
2785       (_("warning: type and size of dynamic symbol `%s' are not defined"),
2786        h->root.root.string);
2787 
2788   dynobj = elf_hash_table (eif->info)->dynobj;
2789   bed = get_elf_backend_data (dynobj);
2790 
2791   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2792     {
2793       eif->failed = TRUE;
2794       return FALSE;
2795     }
2796 
2797   return TRUE;
2798 }
2799 
2800 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2801    DYNBSS.  */
2802 
2803 bfd_boolean
2804 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2805 			      struct elf_link_hash_entry *h,
2806 			      asection *dynbss)
2807 {
2808   unsigned int power_of_two;
2809   bfd_vma mask;
2810   asection *sec = h->root.u.def.section;
2811 
2812   /* The section aligment of definition is the maximum alignment
2813      requirement of symbols defined in the section.  Since we don't
2814      know the symbol alignment requirement, we start with the
2815      maximum alignment and check low bits of the symbol address
2816      for the minimum alignment.  */
2817   power_of_two = bfd_get_section_alignment (sec->owner, sec);
2818   mask = ((bfd_vma) 1 << power_of_two) - 1;
2819   while ((h->root.u.def.value & mask) != 0)
2820     {
2821        mask >>= 1;
2822        --power_of_two;
2823     }
2824 
2825   if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2826 						dynbss))
2827     {
2828       /* Adjust the section alignment if needed.  */
2829       if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2830 				       power_of_two))
2831 	return FALSE;
2832     }
2833 
2834   /* We make sure that the symbol will be aligned properly.  */
2835   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2836 
2837   /* Define the symbol as being at this point in DYNBSS.  */
2838   h->root.u.def.section = dynbss;
2839   h->root.u.def.value = dynbss->size;
2840 
2841   /* Increment the size of DYNBSS to make room for the symbol.  */
2842   dynbss->size += h->size;
2843 
2844   /* No error if extern_protected_data is true.  */
2845   if (h->protected_def
2846       && (!info->extern_protected_data
2847 	  || (info->extern_protected_data < 0
2848 	      && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2849     info->callbacks->einfo
2850       (_("%P: copy reloc against protected `%T' is dangerous\n"),
2851        h->root.root.string);
2852 
2853   return TRUE;
2854 }
2855 
2856 /* Adjust all external symbols pointing into SEC_MERGE sections
2857    to reflect the object merging within the sections.  */
2858 
2859 static bfd_boolean
2860 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2861 {
2862   asection *sec;
2863 
2864   if ((h->root.type == bfd_link_hash_defined
2865        || h->root.type == bfd_link_hash_defweak)
2866       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2867       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2868     {
2869       bfd *output_bfd = (bfd *) data;
2870 
2871       h->root.u.def.value =
2872 	_bfd_merged_section_offset (output_bfd,
2873 				    &h->root.u.def.section,
2874 				    elf_section_data (sec)->sec_info,
2875 				    h->root.u.def.value);
2876     }
2877 
2878   return TRUE;
2879 }
2880 
2881 /* Returns false if the symbol referred to by H should be considered
2882    to resolve local to the current module, and true if it should be
2883    considered to bind dynamically.  */
2884 
2885 bfd_boolean
2886 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2887 			   struct bfd_link_info *info,
2888 			   bfd_boolean not_local_protected)
2889 {
2890   bfd_boolean binding_stays_local_p;
2891   const struct elf_backend_data *bed;
2892   struct elf_link_hash_table *hash_table;
2893 
2894   if (h == NULL)
2895     return FALSE;
2896 
2897   while (h->root.type == bfd_link_hash_indirect
2898 	 || h->root.type == bfd_link_hash_warning)
2899     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2900 
2901   /* If it was forced local, then clearly it's not dynamic.  */
2902   if (h->dynindx == -1)
2903     return FALSE;
2904   if (h->forced_local)
2905     return FALSE;
2906 
2907   /* Identify the cases where name binding rules say that a
2908      visible symbol resolves locally.  */
2909   binding_stays_local_p = (bfd_link_executable (info)
2910 			   || SYMBOLIC_BIND (info, h));
2911 
2912   switch (ELF_ST_VISIBILITY (h->other))
2913     {
2914     case STV_INTERNAL:
2915     case STV_HIDDEN:
2916       return FALSE;
2917 
2918     case STV_PROTECTED:
2919       hash_table = elf_hash_table (info);
2920       if (!is_elf_hash_table (hash_table))
2921 	return FALSE;
2922 
2923       bed = get_elf_backend_data (hash_table->dynobj);
2924 
2925       /* Proper resolution for function pointer equality may require
2926 	 that these symbols perhaps be resolved dynamically, even though
2927 	 we should be resolving them to the current module.  */
2928       if (!not_local_protected || !bed->is_function_type (h->type))
2929 	binding_stays_local_p = TRUE;
2930       break;
2931 
2932     default:
2933       break;
2934     }
2935 
2936   /* If it isn't defined locally, then clearly it's dynamic.  */
2937   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2938     return TRUE;
2939 
2940   /* Otherwise, the symbol is dynamic if binding rules don't tell
2941      us that it remains local.  */
2942   return !binding_stays_local_p;
2943 }
2944 
2945 /* Return true if the symbol referred to by H should be considered
2946    to resolve local to the current module, and false otherwise.  Differs
2947    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2948    undefined symbols.  The two functions are virtually identical except
2949    for the place where forced_local and dynindx == -1 are tested.  If
2950    either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2951    the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2952    the symbol is local only for defined symbols.
2953    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2954    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2955    treatment of undefined weak symbols.  For those that do not make
2956    undefined weak symbols dynamic, both functions may return false.  */
2957 
2958 bfd_boolean
2959 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2960 			      struct bfd_link_info *info,
2961 			      bfd_boolean local_protected)
2962 {
2963   const struct elf_backend_data *bed;
2964   struct elf_link_hash_table *hash_table;
2965 
2966   /* If it's a local sym, of course we resolve locally.  */
2967   if (h == NULL)
2968     return TRUE;
2969 
2970   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
2971   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2972       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2973     return TRUE;
2974 
2975   /* Common symbols that become definitions don't get the DEF_REGULAR
2976      flag set, so test it first, and don't bail out.  */
2977   if (ELF_COMMON_DEF_P (h))
2978     /* Do nothing.  */;
2979   /* If we don't have a definition in a regular file, then we can't
2980      resolve locally.  The sym is either undefined or dynamic.  */
2981   else if (!h->def_regular)
2982     return FALSE;
2983 
2984   /* Forced local symbols resolve locally.  */
2985   if (h->forced_local)
2986     return TRUE;
2987 
2988   /* As do non-dynamic symbols.  */
2989   if (h->dynindx == -1)
2990     return TRUE;
2991 
2992   /* At this point, we know the symbol is defined and dynamic.  In an
2993      executable it must resolve locally, likewise when building symbolic
2994      shared libraries.  */
2995   if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
2996     return TRUE;
2997 
2998   /* Now deal with defined dynamic symbols in shared libraries.  Ones
2999      with default visibility might not resolve locally.  */
3000   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3001     return FALSE;
3002 
3003   hash_table = elf_hash_table (info);
3004   if (!is_elf_hash_table (hash_table))
3005     return TRUE;
3006 
3007   bed = get_elf_backend_data (hash_table->dynobj);
3008 
3009   /* If extern_protected_data is false, STV_PROTECTED non-function
3010      symbols are local.  */
3011   if ((!info->extern_protected_data
3012        || (info->extern_protected_data < 0
3013 	   && !bed->extern_protected_data))
3014       && !bed->is_function_type (h->type))
3015     return TRUE;
3016 
3017   /* Function pointer equality tests may require that STV_PROTECTED
3018      symbols be treated as dynamic symbols.  If the address of a
3019      function not defined in an executable is set to that function's
3020      plt entry in the executable, then the address of the function in
3021      a shared library must also be the plt entry in the executable.  */
3022   return local_protected;
3023 }
3024 
3025 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3026    aligned.  Returns the first TLS output section.  */
3027 
3028 struct bfd_section *
3029 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3030 {
3031   struct bfd_section *sec, *tls;
3032   unsigned int align = 0;
3033 
3034   for (sec = obfd->sections; sec != NULL; sec = sec->next)
3035     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3036       break;
3037   tls = sec;
3038 
3039   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3040     if (sec->alignment_power > align)
3041       align = sec->alignment_power;
3042 
3043   elf_hash_table (info)->tls_sec = tls;
3044 
3045   /* Ensure the alignment of the first section is the largest alignment,
3046      so that the tls segment starts aligned.  */
3047   if (tls != NULL)
3048     tls->alignment_power = align;
3049 
3050   return tls;
3051 }
3052 
3053 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3054 static bfd_boolean
3055 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3056 				  Elf_Internal_Sym *sym)
3057 {
3058   const struct elf_backend_data *bed;
3059 
3060   /* Local symbols do not count, but target specific ones might.  */
3061   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3062       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3063     return FALSE;
3064 
3065   bed = get_elf_backend_data (abfd);
3066   /* Function symbols do not count.  */
3067   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3068     return FALSE;
3069 
3070   /* If the section is undefined, then so is the symbol.  */
3071   if (sym->st_shndx == SHN_UNDEF)
3072     return FALSE;
3073 
3074   /* If the symbol is defined in the common section, then
3075      it is a common definition and so does not count.  */
3076   if (bed->common_definition (sym))
3077     return FALSE;
3078 
3079   /* If the symbol is in a target specific section then we
3080      must rely upon the backend to tell us what it is.  */
3081   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3082     /* FIXME - this function is not coded yet:
3083 
3084        return _bfd_is_global_symbol_definition (abfd, sym);
3085 
3086        Instead for now assume that the definition is not global,
3087        Even if this is wrong, at least the linker will behave
3088        in the same way that it used to do.  */
3089     return FALSE;
3090 
3091   return TRUE;
3092 }
3093 
3094 /* Search the symbol table of the archive element of the archive ABFD
3095    whose archive map contains a mention of SYMDEF, and determine if
3096    the symbol is defined in this element.  */
3097 static bfd_boolean
3098 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3099 {
3100   Elf_Internal_Shdr * hdr;
3101   bfd_size_type symcount;
3102   bfd_size_type extsymcount;
3103   bfd_size_type extsymoff;
3104   Elf_Internal_Sym *isymbuf;
3105   Elf_Internal_Sym *isym;
3106   Elf_Internal_Sym *isymend;
3107   bfd_boolean result;
3108 
3109   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3110   if (abfd == NULL)
3111     return FALSE;
3112 
3113   /* Return FALSE if the object has been claimed by plugin.  */
3114   if (abfd->plugin_format == bfd_plugin_yes)
3115     return FALSE;
3116 
3117   if (! bfd_check_format (abfd, bfd_object))
3118     return FALSE;
3119 
3120   /* Select the appropriate symbol table.  */
3121   if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3122     hdr = &elf_tdata (abfd)->symtab_hdr;
3123   else
3124     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3125 
3126   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3127 
3128   /* The sh_info field of the symtab header tells us where the
3129      external symbols start.  We don't care about the local symbols.  */
3130   if (elf_bad_symtab (abfd))
3131     {
3132       extsymcount = symcount;
3133       extsymoff = 0;
3134     }
3135   else
3136     {
3137       extsymcount = symcount - hdr->sh_info;
3138       extsymoff = hdr->sh_info;
3139     }
3140 
3141   if (extsymcount == 0)
3142     return FALSE;
3143 
3144   /* Read in the symbol table.  */
3145   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3146 				  NULL, NULL, NULL);
3147   if (isymbuf == NULL)
3148     return FALSE;
3149 
3150   /* Scan the symbol table looking for SYMDEF.  */
3151   result = FALSE;
3152   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3153     {
3154       const char *name;
3155 
3156       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3157 					      isym->st_name);
3158       if (name == NULL)
3159 	break;
3160 
3161       if (strcmp (name, symdef->name) == 0)
3162 	{
3163 	  result = is_global_data_symbol_definition (abfd, isym);
3164 	  break;
3165 	}
3166     }
3167 
3168   free (isymbuf);
3169 
3170   return result;
3171 }
3172 
3173 /* Add an entry to the .dynamic table.  */
3174 
3175 bfd_boolean
3176 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3177 			    bfd_vma tag,
3178 			    bfd_vma val)
3179 {
3180   struct elf_link_hash_table *hash_table;
3181   const struct elf_backend_data *bed;
3182   asection *s;
3183   bfd_size_type newsize;
3184   bfd_byte *newcontents;
3185   Elf_Internal_Dyn dyn;
3186 
3187   hash_table = elf_hash_table (info);
3188   if (! is_elf_hash_table (hash_table))
3189     return FALSE;
3190 
3191   bed = get_elf_backend_data (hash_table->dynobj);
3192   s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3193   BFD_ASSERT (s != NULL);
3194 
3195   newsize = s->size + bed->s->sizeof_dyn;
3196   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3197   if (newcontents == NULL)
3198     return FALSE;
3199 
3200   dyn.d_tag = tag;
3201   dyn.d_un.d_val = val;
3202   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3203 
3204   s->size = newsize;
3205   s->contents = newcontents;
3206 
3207   return TRUE;
3208 }
3209 
3210 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3211    otherwise just check whether one already exists.  Returns -1 on error,
3212    1 if a DT_NEEDED tag already exists, and 0 on success.  */
3213 
3214 static int
3215 elf_add_dt_needed_tag (bfd *abfd,
3216 		       struct bfd_link_info *info,
3217 		       const char *soname,
3218 		       bfd_boolean do_it)
3219 {
3220   struct elf_link_hash_table *hash_table;
3221   bfd_size_type strindex;
3222 
3223   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3224     return -1;
3225 
3226   hash_table = elf_hash_table (info);
3227   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3228   if (strindex == (bfd_size_type) -1)
3229     return -1;
3230 
3231   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3232     {
3233       asection *sdyn;
3234       const struct elf_backend_data *bed;
3235       bfd_byte *extdyn;
3236 
3237       bed = get_elf_backend_data (hash_table->dynobj);
3238       sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3239       if (sdyn != NULL)
3240 	for (extdyn = sdyn->contents;
3241 	     extdyn < sdyn->contents + sdyn->size;
3242 	     extdyn += bed->s->sizeof_dyn)
3243 	  {
3244 	    Elf_Internal_Dyn dyn;
3245 
3246 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3247 	    if (dyn.d_tag == DT_NEEDED
3248 		&& dyn.d_un.d_val == strindex)
3249 	      {
3250 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3251 		return 1;
3252 	      }
3253 	  }
3254     }
3255 
3256   if (do_it)
3257     {
3258       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3259 	return -1;
3260 
3261       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3262 	return -1;
3263     }
3264   else
3265     /* We were just checking for existence of the tag.  */
3266     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3267 
3268   return 0;
3269 }
3270 
3271 static bfd_boolean
3272 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3273 {
3274   for (; needed != NULL; needed = needed->next)
3275     if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3276 	&& strcmp (soname, needed->name) == 0)
3277       return TRUE;
3278 
3279   return FALSE;
3280 }
3281 
3282 /* Sort symbol by value, section, and size.  */
3283 static int
3284 elf_sort_symbol (const void *arg1, const void *arg2)
3285 {
3286   const struct elf_link_hash_entry *h1;
3287   const struct elf_link_hash_entry *h2;
3288   bfd_signed_vma vdiff;
3289 
3290   h1 = *(const struct elf_link_hash_entry **) arg1;
3291   h2 = *(const struct elf_link_hash_entry **) arg2;
3292   vdiff = h1->root.u.def.value - h2->root.u.def.value;
3293   if (vdiff != 0)
3294     return vdiff > 0 ? 1 : -1;
3295   else
3296     {
3297       int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3298       if (sdiff != 0)
3299 	return sdiff > 0 ? 1 : -1;
3300     }
3301   vdiff = h1->size - h2->size;
3302   return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3303 }
3304 
3305 /* This function is used to adjust offsets into .dynstr for
3306    dynamic symbols.  This is called via elf_link_hash_traverse.  */
3307 
3308 static bfd_boolean
3309 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3310 {
3311   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3312 
3313   if (h->dynindx != -1)
3314     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3315   return TRUE;
3316 }
3317 
3318 /* Assign string offsets in .dynstr, update all structures referencing
3319    them.  */
3320 
3321 static bfd_boolean
3322 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3323 {
3324   struct elf_link_hash_table *hash_table = elf_hash_table (info);
3325   struct elf_link_local_dynamic_entry *entry;
3326   struct elf_strtab_hash *dynstr = hash_table->dynstr;
3327   bfd *dynobj = hash_table->dynobj;
3328   asection *sdyn;
3329   bfd_size_type size;
3330   const struct elf_backend_data *bed;
3331   bfd_byte *extdyn;
3332 
3333   _bfd_elf_strtab_finalize (dynstr);
3334   size = _bfd_elf_strtab_size (dynstr);
3335 
3336   bed = get_elf_backend_data (dynobj);
3337   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3338   BFD_ASSERT (sdyn != NULL);
3339 
3340   /* Update all .dynamic entries referencing .dynstr strings.  */
3341   for (extdyn = sdyn->contents;
3342        extdyn < sdyn->contents + sdyn->size;
3343        extdyn += bed->s->sizeof_dyn)
3344     {
3345       Elf_Internal_Dyn dyn;
3346 
3347       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3348       switch (dyn.d_tag)
3349 	{
3350 	case DT_STRSZ:
3351 	  dyn.d_un.d_val = size;
3352 	  break;
3353 	case DT_NEEDED:
3354 	case DT_SONAME:
3355 	case DT_RPATH:
3356 	case DT_RUNPATH:
3357 	case DT_FILTER:
3358 	case DT_AUXILIARY:
3359 	case DT_AUDIT:
3360 	case DT_DEPAUDIT:
3361 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3362 	  break;
3363 	default:
3364 	  continue;
3365 	}
3366       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3367     }
3368 
3369   /* Now update local dynamic symbols.  */
3370   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3371     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3372 						  entry->isym.st_name);
3373 
3374   /* And the rest of dynamic symbols.  */
3375   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3376 
3377   /* Adjust version definitions.  */
3378   if (elf_tdata (output_bfd)->cverdefs)
3379     {
3380       asection *s;
3381       bfd_byte *p;
3382       bfd_size_type i;
3383       Elf_Internal_Verdef def;
3384       Elf_Internal_Verdaux defaux;
3385 
3386       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3387       p = s->contents;
3388       do
3389 	{
3390 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3391 				   &def);
3392 	  p += sizeof (Elf_External_Verdef);
3393 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
3394 	    continue;
3395 	  for (i = 0; i < def.vd_cnt; ++i)
3396 	    {
3397 	      _bfd_elf_swap_verdaux_in (output_bfd,
3398 					(Elf_External_Verdaux *) p, &defaux);
3399 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3400 							defaux.vda_name);
3401 	      _bfd_elf_swap_verdaux_out (output_bfd,
3402 					 &defaux, (Elf_External_Verdaux *) p);
3403 	      p += sizeof (Elf_External_Verdaux);
3404 	    }
3405 	}
3406       while (def.vd_next);
3407     }
3408 
3409   /* Adjust version references.  */
3410   if (elf_tdata (output_bfd)->verref)
3411     {
3412       asection *s;
3413       bfd_byte *p;
3414       bfd_size_type i;
3415       Elf_Internal_Verneed need;
3416       Elf_Internal_Vernaux needaux;
3417 
3418       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3419       p = s->contents;
3420       do
3421 	{
3422 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3423 				    &need);
3424 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3425 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
3426 				     (Elf_External_Verneed *) p);
3427 	  p += sizeof (Elf_External_Verneed);
3428 	  for (i = 0; i < need.vn_cnt; ++i)
3429 	    {
3430 	      _bfd_elf_swap_vernaux_in (output_bfd,
3431 					(Elf_External_Vernaux *) p, &needaux);
3432 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3433 							 needaux.vna_name);
3434 	      _bfd_elf_swap_vernaux_out (output_bfd,
3435 					 &needaux,
3436 					 (Elf_External_Vernaux *) p);
3437 	      p += sizeof (Elf_External_Vernaux);
3438 	    }
3439 	}
3440       while (need.vn_next);
3441     }
3442 
3443   return TRUE;
3444 }
3445 
3446 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3447    The default is to only match when the INPUT and OUTPUT are exactly
3448    the same target.  */
3449 
3450 bfd_boolean
3451 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3452 				    const bfd_target *output)
3453 {
3454   return input == output;
3455 }
3456 
3457 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3458    This version is used when different targets for the same architecture
3459    are virtually identical.  */
3460 
3461 bfd_boolean
3462 _bfd_elf_relocs_compatible (const bfd_target *input,
3463 			    const bfd_target *output)
3464 {
3465   const struct elf_backend_data *obed, *ibed;
3466 
3467   if (input == output)
3468     return TRUE;
3469 
3470   ibed = xvec_get_elf_backend_data (input);
3471   obed = xvec_get_elf_backend_data (output);
3472 
3473   if (ibed->arch != obed->arch)
3474     return FALSE;
3475 
3476   /* If both backends are using this function, deem them compatible.  */
3477   return ibed->relocs_compatible == obed->relocs_compatible;
3478 }
3479 
3480 /* Make a special call to the linker "notice" function to tell it that
3481    we are about to handle an as-needed lib, or have finished
3482    processing the lib.  */
3483 
3484 bfd_boolean
3485 _bfd_elf_notice_as_needed (bfd *ibfd,
3486 			   struct bfd_link_info *info,
3487 			   enum notice_asneeded_action act)
3488 {
3489   return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3490 }
3491 
3492 /* Add symbols from an ELF object file to the linker hash table.  */
3493 
3494 static bfd_boolean
3495 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3496 {
3497   Elf_Internal_Ehdr *ehdr;
3498   Elf_Internal_Shdr *hdr;
3499   bfd_size_type symcount;
3500   bfd_size_type extsymcount;
3501   bfd_size_type extsymoff;
3502   struct elf_link_hash_entry **sym_hash;
3503   bfd_boolean dynamic;
3504   Elf_External_Versym *extversym = NULL;
3505   Elf_External_Versym *ever;
3506   struct elf_link_hash_entry *weaks;
3507   struct elf_link_hash_entry **nondeflt_vers = NULL;
3508   bfd_size_type nondeflt_vers_cnt = 0;
3509   Elf_Internal_Sym *isymbuf = NULL;
3510   Elf_Internal_Sym *isym;
3511   Elf_Internal_Sym *isymend;
3512   const struct elf_backend_data *bed;
3513   bfd_boolean add_needed;
3514   struct elf_link_hash_table *htab;
3515   bfd_size_type amt;
3516   void *alloc_mark = NULL;
3517   struct bfd_hash_entry **old_table = NULL;
3518   unsigned int old_size = 0;
3519   unsigned int old_count = 0;
3520   void *old_tab = NULL;
3521   void *old_ent;
3522   struct bfd_link_hash_entry *old_undefs = NULL;
3523   struct bfd_link_hash_entry *old_undefs_tail = NULL;
3524   void *old_strtab = NULL;
3525   size_t tabsize = 0;
3526   asection *s;
3527   bfd_boolean just_syms;
3528 
3529   htab = elf_hash_table (info);
3530   bed = get_elf_backend_data (abfd);
3531 
3532   if ((abfd->flags & DYNAMIC) == 0)
3533     dynamic = FALSE;
3534   else
3535     {
3536       dynamic = TRUE;
3537 
3538       /* You can't use -r against a dynamic object.  Also, there's no
3539 	 hope of using a dynamic object which does not exactly match
3540 	 the format of the output file.  */
3541       if (bfd_link_relocatable (info)
3542 	  || !is_elf_hash_table (htab)
3543 	  || info->output_bfd->xvec != abfd->xvec)
3544 	{
3545 	  if (bfd_link_relocatable (info))
3546 	    bfd_set_error (bfd_error_invalid_operation);
3547 	  else
3548 	    bfd_set_error (bfd_error_wrong_format);
3549 	  goto error_return;
3550 	}
3551     }
3552 
3553   ehdr = elf_elfheader (abfd);
3554   if (info->warn_alternate_em
3555       && bed->elf_machine_code != ehdr->e_machine
3556       && ((bed->elf_machine_alt1 != 0
3557 	   && ehdr->e_machine == bed->elf_machine_alt1)
3558 	  || (bed->elf_machine_alt2 != 0
3559 	      && ehdr->e_machine == bed->elf_machine_alt2)))
3560     info->callbacks->einfo
3561       (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3562        ehdr->e_machine, abfd, bed->elf_machine_code);
3563 
3564   /* As a GNU extension, any input sections which are named
3565      .gnu.warning.SYMBOL are treated as warning symbols for the given
3566      symbol.  This differs from .gnu.warning sections, which generate
3567      warnings when they are included in an output file.  */
3568   /* PR 12761: Also generate this warning when building shared libraries.  */
3569   for (s = abfd->sections; s != NULL; s = s->next)
3570     {
3571       const char *name;
3572 
3573       name = bfd_get_section_name (abfd, s);
3574       if (CONST_STRNEQ (name, ".gnu.warning."))
3575 	{
3576 	  char *msg;
3577 	  bfd_size_type sz;
3578 
3579 	  name += sizeof ".gnu.warning." - 1;
3580 
3581 	  /* If this is a shared object, then look up the symbol
3582 	     in the hash table.  If it is there, and it is already
3583 	     been defined, then we will not be using the entry
3584 	     from this shared object, so we don't need to warn.
3585 	     FIXME: If we see the definition in a regular object
3586 	     later on, we will warn, but we shouldn't.  The only
3587 	     fix is to keep track of what warnings we are supposed
3588 	     to emit, and then handle them all at the end of the
3589 	     link.  */
3590 	  if (dynamic)
3591 	    {
3592 	      struct elf_link_hash_entry *h;
3593 
3594 	      h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3595 
3596 	      /* FIXME: What about bfd_link_hash_common?  */
3597 	      if (h != NULL
3598 		  && (h->root.type == bfd_link_hash_defined
3599 		      || h->root.type == bfd_link_hash_defweak))
3600 		continue;
3601 	    }
3602 
3603 	  sz = s->size;
3604 	  msg = (char *) bfd_alloc (abfd, sz + 1);
3605 	  if (msg == NULL)
3606 	    goto error_return;
3607 
3608 	  if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3609 	    goto error_return;
3610 
3611 	  msg[sz] = '\0';
3612 
3613 	  if (! (_bfd_generic_link_add_one_symbol
3614 		 (info, abfd, name, BSF_WARNING, s, 0, msg,
3615 		  FALSE, bed->collect, NULL)))
3616 	    goto error_return;
3617 
3618 	  if (bfd_link_executable (info))
3619 	    {
3620 	      /* Clobber the section size so that the warning does
3621 		 not get copied into the output file.  */
3622 	      s->size = 0;
3623 
3624 	      /* Also set SEC_EXCLUDE, so that symbols defined in
3625 		 the warning section don't get copied to the output.  */
3626 	      s->flags |= SEC_EXCLUDE;
3627 	    }
3628 	}
3629     }
3630 
3631   just_syms = ((s = abfd->sections) != NULL
3632 	       && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3633 
3634   add_needed = TRUE;
3635   if (! dynamic)
3636     {
3637       /* If we are creating a shared library, create all the dynamic
3638 	 sections immediately.  We need to attach them to something,
3639 	 so we attach them to this BFD, provided it is the right
3640 	 format and is not from ld --just-symbols.  FIXME: If there
3641 	 are no input BFD's of the same format as the output, we can't
3642 	 make a shared library.  */
3643       if (!just_syms
3644 	  && bfd_link_pic (info)
3645 	  && is_elf_hash_table (htab)
3646 	  && info->output_bfd->xvec == abfd->xvec
3647 	  && !htab->dynamic_sections_created)
3648 	{
3649 	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3650 	    goto error_return;
3651 	}
3652     }
3653   else if (!is_elf_hash_table (htab))
3654     goto error_return;
3655   else
3656     {
3657       const char *soname = NULL;
3658       char *audit = NULL;
3659       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3660       int ret;
3661 
3662       /* ld --just-symbols and dynamic objects don't mix very well.
3663 	 ld shouldn't allow it.  */
3664       if (just_syms)
3665 	abort ();
3666 
3667       /* If this dynamic lib was specified on the command line with
3668 	 --as-needed in effect, then we don't want to add a DT_NEEDED
3669 	 tag unless the lib is actually used.  Similary for libs brought
3670 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
3671 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3672 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
3673 	 all.  */
3674       add_needed = (elf_dyn_lib_class (abfd)
3675 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
3676 		       | DYN_NO_NEEDED)) == 0;
3677 
3678       s = bfd_get_section_by_name (abfd, ".dynamic");
3679       if (s != NULL)
3680 	{
3681 	  bfd_byte *dynbuf;
3682 	  bfd_byte *extdyn;
3683 	  unsigned int elfsec;
3684 	  unsigned long shlink;
3685 
3686 	  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3687 	    {
3688 error_free_dyn:
3689 	      free (dynbuf);
3690 	      goto error_return;
3691 	    }
3692 
3693 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3694 	  if (elfsec == SHN_BAD)
3695 	    goto error_free_dyn;
3696 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3697 
3698 	  for (extdyn = dynbuf;
3699 	       extdyn < dynbuf + s->size;
3700 	       extdyn += bed->s->sizeof_dyn)
3701 	    {
3702 	      Elf_Internal_Dyn dyn;
3703 
3704 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3705 	      if (dyn.d_tag == DT_SONAME)
3706 		{
3707 		  unsigned int tagv = dyn.d_un.d_val;
3708 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3709 		  if (soname == NULL)
3710 		    goto error_free_dyn;
3711 		}
3712 	      if (dyn.d_tag == DT_NEEDED)
3713 		{
3714 		  struct bfd_link_needed_list *n, **pn;
3715 		  char *fnm, *anm;
3716 		  unsigned int tagv = dyn.d_un.d_val;
3717 
3718 		  amt = sizeof (struct bfd_link_needed_list);
3719 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3720 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3721 		  if (n == NULL || fnm == NULL)
3722 		    goto error_free_dyn;
3723 		  amt = strlen (fnm) + 1;
3724 		  anm = (char *) bfd_alloc (abfd, amt);
3725 		  if (anm == NULL)
3726 		    goto error_free_dyn;
3727 		  memcpy (anm, fnm, amt);
3728 		  n->name = anm;
3729 		  n->by = abfd;
3730 		  n->next = NULL;
3731 		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3732 		    ;
3733 		  *pn = n;
3734 		}
3735 	      if (dyn.d_tag == DT_RUNPATH)
3736 		{
3737 		  struct bfd_link_needed_list *n, **pn;
3738 		  char *fnm, *anm;
3739 		  unsigned int tagv = dyn.d_un.d_val;
3740 
3741 		  amt = sizeof (struct bfd_link_needed_list);
3742 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3743 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3744 		  if (n == NULL || fnm == NULL)
3745 		    goto error_free_dyn;
3746 		  amt = strlen (fnm) + 1;
3747 		  anm = (char *) bfd_alloc (abfd, amt);
3748 		  if (anm == NULL)
3749 		    goto error_free_dyn;
3750 		  memcpy (anm, fnm, amt);
3751 		  n->name = anm;
3752 		  n->by = abfd;
3753 		  n->next = NULL;
3754 		  for (pn = & runpath;
3755 		       *pn != NULL;
3756 		       pn = &(*pn)->next)
3757 		    ;
3758 		  *pn = n;
3759 		}
3760 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
3761 	      if (!runpath && dyn.d_tag == DT_RPATH)
3762 		{
3763 		  struct bfd_link_needed_list *n, **pn;
3764 		  char *fnm, *anm;
3765 		  unsigned int tagv = dyn.d_un.d_val;
3766 
3767 		  amt = sizeof (struct bfd_link_needed_list);
3768 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3769 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3770 		  if (n == NULL || fnm == NULL)
3771 		    goto error_free_dyn;
3772 		  amt = strlen (fnm) + 1;
3773 		  anm = (char *) bfd_alloc (abfd, amt);
3774 		  if (anm == NULL)
3775 		    goto error_free_dyn;
3776 		  memcpy (anm, fnm, amt);
3777 		  n->name = anm;
3778 		  n->by = abfd;
3779 		  n->next = NULL;
3780 		  for (pn = & rpath;
3781 		       *pn != NULL;
3782 		       pn = &(*pn)->next)
3783 		    ;
3784 		  *pn = n;
3785 		}
3786 	      if (dyn.d_tag == DT_AUDIT)
3787 		{
3788 		  unsigned int tagv = dyn.d_un.d_val;
3789 		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3790 		}
3791 	    }
3792 
3793 	  free (dynbuf);
3794 	}
3795 
3796       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
3797 	 frees all more recently bfd_alloc'd blocks as well.  */
3798       if (runpath)
3799 	rpath = runpath;
3800 
3801       if (rpath)
3802 	{
3803 	  struct bfd_link_needed_list **pn;
3804 	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3805 	    ;
3806 	  *pn = rpath;
3807 	}
3808 
3809       /* We do not want to include any of the sections in a dynamic
3810 	 object in the output file.  We hack by simply clobbering the
3811 	 list of sections in the BFD.  This could be handled more
3812 	 cleanly by, say, a new section flag; the existing
3813 	 SEC_NEVER_LOAD flag is not the one we want, because that one
3814 	 still implies that the section takes up space in the output
3815 	 file.  */
3816       bfd_section_list_clear (abfd);
3817 
3818       /* Find the name to use in a DT_NEEDED entry that refers to this
3819 	 object.  If the object has a DT_SONAME entry, we use it.
3820 	 Otherwise, if the generic linker stuck something in
3821 	 elf_dt_name, we use that.  Otherwise, we just use the file
3822 	 name.  */
3823       if (soname == NULL || *soname == '\0')
3824 	{
3825 	  soname = elf_dt_name (abfd);
3826 	  if (soname == NULL || *soname == '\0')
3827 	    soname = bfd_get_filename (abfd);
3828 	}
3829 
3830       /* Save the SONAME because sometimes the linker emulation code
3831 	 will need to know it.  */
3832       elf_dt_name (abfd) = soname;
3833 
3834       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3835       if (ret < 0)
3836 	goto error_return;
3837 
3838       /* If we have already included this dynamic object in the
3839 	 link, just ignore it.  There is no reason to include a
3840 	 particular dynamic object more than once.  */
3841       if (ret > 0)
3842 	return TRUE;
3843 
3844       /* Save the DT_AUDIT entry for the linker emulation code. */
3845       elf_dt_audit (abfd) = audit;
3846     }
3847 
3848   /* If this is a dynamic object, we always link against the .dynsym
3849      symbol table, not the .symtab symbol table.  The dynamic linker
3850      will only see the .dynsym symbol table, so there is no reason to
3851      look at .symtab for a dynamic object.  */
3852 
3853   if (! dynamic || elf_dynsymtab (abfd) == 0)
3854     hdr = &elf_tdata (abfd)->symtab_hdr;
3855   else
3856     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3857 
3858   symcount = hdr->sh_size / bed->s->sizeof_sym;
3859 
3860   /* The sh_info field of the symtab header tells us where the
3861      external symbols start.  We don't care about the local symbols at
3862      this point.  */
3863   if (elf_bad_symtab (abfd))
3864     {
3865       extsymcount = symcount;
3866       extsymoff = 0;
3867     }
3868   else
3869     {
3870       extsymcount = symcount - hdr->sh_info;
3871       extsymoff = hdr->sh_info;
3872     }
3873 
3874   sym_hash = elf_sym_hashes (abfd);
3875   if (extsymcount != 0)
3876     {
3877       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3878 				      NULL, NULL, NULL);
3879       if (isymbuf == NULL)
3880 	goto error_return;
3881 
3882       if (sym_hash == NULL)
3883 	{
3884 	  /* We store a pointer to the hash table entry for each
3885 	     external symbol.  */
3886 	  amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3887 	  sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3888 	  if (sym_hash == NULL)
3889 	    goto error_free_sym;
3890 	  elf_sym_hashes (abfd) = sym_hash;
3891 	}
3892     }
3893 
3894   if (dynamic)
3895     {
3896       /* Read in any version definitions.  */
3897       if (!_bfd_elf_slurp_version_tables (abfd,
3898 					  info->default_imported_symver))
3899 	goto error_free_sym;
3900 
3901       /* Read in the symbol versions, but don't bother to convert them
3902 	 to internal format.  */
3903       if (elf_dynversym (abfd) != 0)
3904 	{
3905 	  Elf_Internal_Shdr *versymhdr;
3906 
3907 	  versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3908 	  extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3909 	  if (extversym == NULL)
3910 	    goto error_free_sym;
3911 	  amt = versymhdr->sh_size;
3912 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3913 	      || bfd_bread (extversym, amt, abfd) != amt)
3914 	    goto error_free_vers;
3915 	}
3916     }
3917 
3918   /* If we are loading an as-needed shared lib, save the symbol table
3919      state before we start adding symbols.  If the lib turns out
3920      to be unneeded, restore the state.  */
3921   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3922     {
3923       unsigned int i;
3924       size_t entsize;
3925 
3926       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3927 	{
3928 	  struct bfd_hash_entry *p;
3929 	  struct elf_link_hash_entry *h;
3930 
3931 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3932 	    {
3933 	      h = (struct elf_link_hash_entry *) p;
3934 	      entsize += htab->root.table.entsize;
3935 	      if (h->root.type == bfd_link_hash_warning)
3936 		entsize += htab->root.table.entsize;
3937 	    }
3938 	}
3939 
3940       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3941       old_tab = bfd_malloc (tabsize + entsize);
3942       if (old_tab == NULL)
3943 	goto error_free_vers;
3944 
3945       /* Remember the current objalloc pointer, so that all mem for
3946 	 symbols added can later be reclaimed.  */
3947       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3948       if (alloc_mark == NULL)
3949 	goto error_free_vers;
3950 
3951       /* Make a special call to the linker "notice" function to
3952 	 tell it that we are about to handle an as-needed lib.  */
3953       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
3954 	goto error_free_vers;
3955 
3956       /* Clone the symbol table.  Remember some pointers into the
3957 	 symbol table, and dynamic symbol count.  */
3958       old_ent = (char *) old_tab + tabsize;
3959       memcpy (old_tab, htab->root.table.table, tabsize);
3960       old_undefs = htab->root.undefs;
3961       old_undefs_tail = htab->root.undefs_tail;
3962       old_table = htab->root.table.table;
3963       old_size = htab->root.table.size;
3964       old_count = htab->root.table.count;
3965       old_strtab = _bfd_elf_strtab_save (htab->dynstr);
3966       if (old_strtab == NULL)
3967 	goto error_free_vers;
3968 
3969       for (i = 0; i < htab->root.table.size; i++)
3970 	{
3971 	  struct bfd_hash_entry *p;
3972 	  struct elf_link_hash_entry *h;
3973 
3974 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3975 	    {
3976 	      memcpy (old_ent, p, htab->root.table.entsize);
3977 	      old_ent = (char *) old_ent + htab->root.table.entsize;
3978 	      h = (struct elf_link_hash_entry *) p;
3979 	      if (h->root.type == bfd_link_hash_warning)
3980 		{
3981 		  memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3982 		  old_ent = (char *) old_ent + htab->root.table.entsize;
3983 		}
3984 	    }
3985 	}
3986     }
3987 
3988   weaks = NULL;
3989   ever = extversym != NULL ? extversym + extsymoff : NULL;
3990   for (isym = isymbuf, isymend = isymbuf + extsymcount;
3991        isym < isymend;
3992        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3993     {
3994       int bind;
3995       bfd_vma value;
3996       asection *sec, *new_sec;
3997       flagword flags;
3998       const char *name;
3999       struct elf_link_hash_entry *h;
4000       struct elf_link_hash_entry *hi;
4001       bfd_boolean definition;
4002       bfd_boolean size_change_ok;
4003       bfd_boolean type_change_ok;
4004       bfd_boolean new_weakdef;
4005       bfd_boolean new_weak;
4006       bfd_boolean old_weak;
4007       bfd_boolean override;
4008       bfd_boolean common;
4009       unsigned int old_alignment;
4010       bfd *old_bfd;
4011       bfd_boolean matched;
4012 
4013       override = FALSE;
4014 
4015       flags = BSF_NO_FLAGS;
4016       sec = NULL;
4017       value = isym->st_value;
4018       common = bed->common_definition (isym);
4019 
4020       bind = ELF_ST_BIND (isym->st_info);
4021       switch (bind)
4022 	{
4023 	case STB_LOCAL:
4024 	  /* This should be impossible, since ELF requires that all
4025 	     global symbols follow all local symbols, and that sh_info
4026 	     point to the first global symbol.  Unfortunately, Irix 5
4027 	     screws this up.  */
4028 	  continue;
4029 
4030 	case STB_GLOBAL:
4031 	  if (isym->st_shndx != SHN_UNDEF && !common)
4032 	    flags = BSF_GLOBAL;
4033 	  break;
4034 
4035 	case STB_WEAK:
4036 	  flags = BSF_WEAK;
4037 	  break;
4038 
4039 	case STB_GNU_UNIQUE:
4040 	  flags = BSF_GNU_UNIQUE;
4041 	  break;
4042 
4043 	default:
4044 	  /* Leave it up to the processor backend.  */
4045 	  break;
4046 	}
4047 
4048       if (isym->st_shndx == SHN_UNDEF)
4049 	sec = bfd_und_section_ptr;
4050       else if (isym->st_shndx == SHN_ABS)
4051 	sec = bfd_abs_section_ptr;
4052       else if (isym->st_shndx == SHN_COMMON)
4053 	{
4054 	  sec = bfd_com_section_ptr;
4055 	  /* What ELF calls the size we call the value.  What ELF
4056 	     calls the value we call the alignment.  */
4057 	  value = isym->st_size;
4058 	}
4059       else
4060 	{
4061 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4062 	  if (sec == NULL)
4063 	    sec = bfd_abs_section_ptr;
4064 	  else if (discarded_section (sec))
4065 	    {
4066 	      /* Symbols from discarded section are undefined.  We keep
4067 		 its visibility.  */
4068 	      sec = bfd_und_section_ptr;
4069 	      isym->st_shndx = SHN_UNDEF;
4070 	    }
4071 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4072 	    value -= sec->vma;
4073 	}
4074 
4075       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4076 					      isym->st_name);
4077       if (name == NULL)
4078 	goto error_free_vers;
4079 
4080       if (isym->st_shndx == SHN_COMMON
4081 	  && (abfd->flags & BFD_PLUGIN) != 0)
4082 	{
4083 	  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4084 
4085 	  if (xc == NULL)
4086 	    {
4087 	      flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4088 				 | SEC_EXCLUDE);
4089 	      xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4090 	      if (xc == NULL)
4091 		goto error_free_vers;
4092 	    }
4093 	  sec = xc;
4094 	}
4095       else if (isym->st_shndx == SHN_COMMON
4096 	       && ELF_ST_TYPE (isym->st_info) == STT_TLS
4097 	       && !bfd_link_relocatable (info))
4098 	{
4099 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4100 
4101 	  if (tcomm == NULL)
4102 	    {
4103 	      flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4104 				 | SEC_LINKER_CREATED);
4105 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4106 	      if (tcomm == NULL)
4107 		goto error_free_vers;
4108 	    }
4109 	  sec = tcomm;
4110 	}
4111       else if (bed->elf_add_symbol_hook)
4112 	{
4113 	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4114 					     &sec, &value))
4115 	    goto error_free_vers;
4116 
4117 	  /* The hook function sets the name to NULL if this symbol
4118 	     should be skipped for some reason.  */
4119 	  if (name == NULL)
4120 	    continue;
4121 	}
4122 
4123       /* Sanity check that all possibilities were handled.  */
4124       if (sec == NULL)
4125 	{
4126 	  bfd_set_error (bfd_error_bad_value);
4127 	  goto error_free_vers;
4128 	}
4129 
4130       /* Silently discard TLS symbols from --just-syms.  There's
4131 	 no way to combine a static TLS block with a new TLS block
4132 	 for this executable.  */
4133       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4134 	  && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4135 	continue;
4136 
4137       if (bfd_is_und_section (sec)
4138 	  || bfd_is_com_section (sec))
4139 	definition = FALSE;
4140       else
4141 	definition = TRUE;
4142 
4143       size_change_ok = FALSE;
4144       type_change_ok = bed->type_change_ok;
4145       old_weak = FALSE;
4146       matched = FALSE;
4147       old_alignment = 0;
4148       old_bfd = NULL;
4149       new_sec = sec;
4150 
4151       if (is_elf_hash_table (htab))
4152 	{
4153 	  Elf_Internal_Versym iver;
4154 	  unsigned int vernum = 0;
4155 	  bfd_boolean skip;
4156 
4157 	  if (ever == NULL)
4158 	    {
4159 	      if (info->default_imported_symver)
4160 		/* Use the default symbol version created earlier.  */
4161 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
4162 	      else
4163 		iver.vs_vers = 0;
4164 	    }
4165 	  else
4166 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
4167 
4168 	  vernum = iver.vs_vers & VERSYM_VERSION;
4169 
4170 	  /* If this is a hidden symbol, or if it is not version
4171 	     1, we append the version name to the symbol name.
4172 	     However, we do not modify a non-hidden absolute symbol
4173 	     if it is not a function, because it might be the version
4174 	     symbol itself.  FIXME: What if it isn't?  */
4175 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4176 	      || (vernum > 1
4177 		  && (!bfd_is_abs_section (sec)
4178 		      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4179 	    {
4180 	      const char *verstr;
4181 	      size_t namelen, verlen, newlen;
4182 	      char *newname, *p;
4183 
4184 	      if (isym->st_shndx != SHN_UNDEF)
4185 		{
4186 		  if (vernum > elf_tdata (abfd)->cverdefs)
4187 		    verstr = NULL;
4188 		  else if (vernum > 1)
4189 		    verstr =
4190 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4191 		  else
4192 		    verstr = "";
4193 
4194 		  if (verstr == NULL)
4195 		    {
4196 		      (*_bfd_error_handler)
4197 			(_("%B: %s: invalid version %u (max %d)"),
4198 			 abfd, name, vernum,
4199 			 elf_tdata (abfd)->cverdefs);
4200 		      bfd_set_error (bfd_error_bad_value);
4201 		      goto error_free_vers;
4202 		    }
4203 		}
4204 	      else
4205 		{
4206 		  /* We cannot simply test for the number of
4207 		     entries in the VERNEED section since the
4208 		     numbers for the needed versions do not start
4209 		     at 0.  */
4210 		  Elf_Internal_Verneed *t;
4211 
4212 		  verstr = NULL;
4213 		  for (t = elf_tdata (abfd)->verref;
4214 		       t != NULL;
4215 		       t = t->vn_nextref)
4216 		    {
4217 		      Elf_Internal_Vernaux *a;
4218 
4219 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4220 			{
4221 			  if (a->vna_other == vernum)
4222 			    {
4223 			      verstr = a->vna_nodename;
4224 			      break;
4225 			    }
4226 			}
4227 		      if (a != NULL)
4228 			break;
4229 		    }
4230 		  if (verstr == NULL)
4231 		    {
4232 		      (*_bfd_error_handler)
4233 			(_("%B: %s: invalid needed version %d"),
4234 			 abfd, name, vernum);
4235 		      bfd_set_error (bfd_error_bad_value);
4236 		      goto error_free_vers;
4237 		    }
4238 		}
4239 
4240 	      namelen = strlen (name);
4241 	      verlen = strlen (verstr);
4242 	      newlen = namelen + verlen + 2;
4243 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4244 		  && isym->st_shndx != SHN_UNDEF)
4245 		++newlen;
4246 
4247 	      newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4248 	      if (newname == NULL)
4249 		goto error_free_vers;
4250 	      memcpy (newname, name, namelen);
4251 	      p = newname + namelen;
4252 	      *p++ = ELF_VER_CHR;
4253 	      /* If this is a defined non-hidden version symbol,
4254 		 we add another @ to the name.  This indicates the
4255 		 default version of the symbol.  */
4256 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4257 		  && isym->st_shndx != SHN_UNDEF)
4258 		*p++ = ELF_VER_CHR;
4259 	      memcpy (p, verstr, verlen + 1);
4260 
4261 	      name = newname;
4262 	    }
4263 
4264 	  /* If this symbol has default visibility and the user has
4265 	     requested we not re-export it, then mark it as hidden.  */
4266 	  if (!bfd_is_und_section (sec)
4267 	      && !dynamic
4268 	      && abfd->no_export
4269 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4270 	    isym->st_other = (STV_HIDDEN
4271 			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4272 
4273 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4274 				      sym_hash, &old_bfd, &old_weak,
4275 				      &old_alignment, &skip, &override,
4276 				      &type_change_ok, &size_change_ok,
4277 				      &matched))
4278 	    goto error_free_vers;
4279 
4280 	  if (skip)
4281 	    continue;
4282 
4283 	  /* Override a definition only if the new symbol matches the
4284 	     existing one.  */
4285 	  if (override && matched)
4286 	    definition = FALSE;
4287 
4288 	  h = *sym_hash;
4289 	  while (h->root.type == bfd_link_hash_indirect
4290 		 || h->root.type == bfd_link_hash_warning)
4291 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4292 
4293 	  if (elf_tdata (abfd)->verdef != NULL
4294 	      && vernum > 1
4295 	      && definition)
4296 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4297 	}
4298 
4299       if (! (_bfd_generic_link_add_one_symbol
4300 	     (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4301 	      (struct bfd_link_hash_entry **) sym_hash)))
4302 	goto error_free_vers;
4303 
4304       h = *sym_hash;
4305       /* We need to make sure that indirect symbol dynamic flags are
4306 	 updated.  */
4307       hi = h;
4308       while (h->root.type == bfd_link_hash_indirect
4309 	     || h->root.type == bfd_link_hash_warning)
4310 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
4311 
4312       *sym_hash = h;
4313 
4314       new_weak = (flags & BSF_WEAK) != 0;
4315       new_weakdef = FALSE;
4316       if (dynamic
4317 	  && definition
4318 	  && new_weak
4319 	  && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4320 	  && is_elf_hash_table (htab)
4321 	  && h->u.weakdef == NULL)
4322 	{
4323 	  /* Keep a list of all weak defined non function symbols from
4324 	     a dynamic object, using the weakdef field.  Later in this
4325 	     function we will set the weakdef field to the correct
4326 	     value.  We only put non-function symbols from dynamic
4327 	     objects on this list, because that happens to be the only
4328 	     time we need to know the normal symbol corresponding to a
4329 	     weak symbol, and the information is time consuming to
4330 	     figure out.  If the weakdef field is not already NULL,
4331 	     then this symbol was already defined by some previous
4332 	     dynamic object, and we will be using that previous
4333 	     definition anyhow.  */
4334 
4335 	  h->u.weakdef = weaks;
4336 	  weaks = h;
4337 	  new_weakdef = TRUE;
4338 	}
4339 
4340       /* Set the alignment of a common symbol.  */
4341       if ((common || bfd_is_com_section (sec))
4342 	  && h->root.type == bfd_link_hash_common)
4343 	{
4344 	  unsigned int align;
4345 
4346 	  if (common)
4347 	    align = bfd_log2 (isym->st_value);
4348 	  else
4349 	    {
4350 	      /* The new symbol is a common symbol in a shared object.
4351 		 We need to get the alignment from the section.  */
4352 	      align = new_sec->alignment_power;
4353 	    }
4354 	  if (align > old_alignment)
4355 	    h->root.u.c.p->alignment_power = align;
4356 	  else
4357 	    h->root.u.c.p->alignment_power = old_alignment;
4358 	}
4359 
4360       if (is_elf_hash_table (htab))
4361 	{
4362 	  /* Set a flag in the hash table entry indicating the type of
4363 	     reference or definition we just found.  A dynamic symbol
4364 	     is one which is referenced or defined by both a regular
4365 	     object and a shared object.  */
4366 	  bfd_boolean dynsym = FALSE;
4367 
4368 	  /* Plugin symbols aren't normal.  Don't set def_regular or
4369 	     ref_regular for them, or make them dynamic.  */
4370 	  if ((abfd->flags & BFD_PLUGIN) != 0)
4371 	    ;
4372 	  else if (! dynamic)
4373 	    {
4374 	      if (! definition)
4375 		{
4376 		  h->ref_regular = 1;
4377 		  if (bind != STB_WEAK)
4378 		    h->ref_regular_nonweak = 1;
4379 		}
4380 	      else
4381 		{
4382 		  h->def_regular = 1;
4383 		  if (h->def_dynamic)
4384 		    {
4385 		      h->def_dynamic = 0;
4386 		      h->ref_dynamic = 1;
4387 		    }
4388 		}
4389 
4390 	      /* If the indirect symbol has been forced local, don't
4391 		 make the real symbol dynamic.  */
4392 	      if ((h == hi || !hi->forced_local)
4393 		  && (bfd_link_dll (info)
4394 		      || h->def_dynamic
4395 		      || h->ref_dynamic))
4396 		dynsym = TRUE;
4397 	    }
4398 	  else
4399 	    {
4400 	      if (! definition)
4401 		{
4402 		  h->ref_dynamic = 1;
4403 		  hi->ref_dynamic = 1;
4404 		}
4405 	      else
4406 		{
4407 		  h->def_dynamic = 1;
4408 		  hi->def_dynamic = 1;
4409 		}
4410 
4411 	      /* If the indirect symbol has been forced local, don't
4412 		 make the real symbol dynamic.  */
4413 	      if ((h == hi || !hi->forced_local)
4414 		  && (h->def_regular
4415 		      || h->ref_regular
4416 		      || (h->u.weakdef != NULL
4417 			  && ! new_weakdef
4418 			  && h->u.weakdef->dynindx != -1)))
4419 		dynsym = TRUE;
4420 	    }
4421 
4422 	  /* Check to see if we need to add an indirect symbol for
4423 	     the default name.  */
4424 	  if (definition
4425 	      || (!override && h->root.type == bfd_link_hash_common))
4426 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4427 					      sec, value, &old_bfd, &dynsym))
4428 	      goto error_free_vers;
4429 
4430 	  /* Check the alignment when a common symbol is involved. This
4431 	     can change when a common symbol is overridden by a normal
4432 	     definition or a common symbol is ignored due to the old
4433 	     normal definition. We need to make sure the maximum
4434 	     alignment is maintained.  */
4435 	  if ((old_alignment || common)
4436 	      && h->root.type != bfd_link_hash_common)
4437 	    {
4438 	      unsigned int common_align;
4439 	      unsigned int normal_align;
4440 	      unsigned int symbol_align;
4441 	      bfd *normal_bfd;
4442 	      bfd *common_bfd;
4443 
4444 	      BFD_ASSERT (h->root.type == bfd_link_hash_defined
4445 			  || h->root.type == bfd_link_hash_defweak);
4446 
4447 	      symbol_align = ffs (h->root.u.def.value) - 1;
4448 	      if (h->root.u.def.section->owner != NULL
4449 		  && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4450 		{
4451 		  normal_align = h->root.u.def.section->alignment_power;
4452 		  if (normal_align > symbol_align)
4453 		    normal_align = symbol_align;
4454 		}
4455 	      else
4456 		normal_align = symbol_align;
4457 
4458 	      if (old_alignment)
4459 		{
4460 		  common_align = old_alignment;
4461 		  common_bfd = old_bfd;
4462 		  normal_bfd = abfd;
4463 		}
4464 	      else
4465 		{
4466 		  common_align = bfd_log2 (isym->st_value);
4467 		  common_bfd = abfd;
4468 		  normal_bfd = old_bfd;
4469 		}
4470 
4471 	      if (normal_align < common_align)
4472 		{
4473 		  /* PR binutils/2735 */
4474 		  if (normal_bfd == NULL)
4475 		    (*_bfd_error_handler)
4476 		      (_("Warning: alignment %u of common symbol `%s' in %B is"
4477 			 " greater than the alignment (%u) of its section %A"),
4478 		       common_bfd, h->root.u.def.section,
4479 		       1 << common_align, name, 1 << normal_align);
4480 		  else
4481 		    (*_bfd_error_handler)
4482 		      (_("Warning: alignment %u of symbol `%s' in %B"
4483 			 " is smaller than %u in %B"),
4484 		       normal_bfd, common_bfd,
4485 		       1 << normal_align, name, 1 << common_align);
4486 		}
4487 	    }
4488 
4489 	  /* Remember the symbol size if it isn't undefined.  */
4490 	  if (isym->st_size != 0
4491 	      && isym->st_shndx != SHN_UNDEF
4492 	      && (definition || h->size == 0))
4493 	    {
4494 	      if (h->size != 0
4495 		  && h->size != isym->st_size
4496 		  && ! size_change_ok)
4497 		(*_bfd_error_handler)
4498 		  (_("Warning: size of symbol `%s' changed"
4499 		     " from %lu in %B to %lu in %B"),
4500 		   old_bfd, abfd,
4501 		   name, (unsigned long) h->size,
4502 		   (unsigned long) isym->st_size);
4503 
4504 	      h->size = isym->st_size;
4505 	    }
4506 
4507 	  /* If this is a common symbol, then we always want H->SIZE
4508 	     to be the size of the common symbol.  The code just above
4509 	     won't fix the size if a common symbol becomes larger.  We
4510 	     don't warn about a size change here, because that is
4511 	     covered by --warn-common.  Allow changes between different
4512 	     function types.  */
4513 	  if (h->root.type == bfd_link_hash_common)
4514 	    h->size = h->root.u.c.size;
4515 
4516 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4517 	      && ((definition && !new_weak)
4518 		  || (old_weak && h->root.type == bfd_link_hash_common)
4519 		  || h->type == STT_NOTYPE))
4520 	    {
4521 	      unsigned int type = ELF_ST_TYPE (isym->st_info);
4522 
4523 	      /* Turn an IFUNC symbol from a DSO into a normal FUNC
4524 		 symbol.  */
4525 	      if (type == STT_GNU_IFUNC
4526 		  && (abfd->flags & DYNAMIC) != 0)
4527 		type = STT_FUNC;
4528 
4529 	      if (h->type != type)
4530 		{
4531 		  if (h->type != STT_NOTYPE && ! type_change_ok)
4532 		    (*_bfd_error_handler)
4533 		      (_("Warning: type of symbol `%s' changed"
4534 			 " from %d to %d in %B"),
4535 		       abfd, name, h->type, type);
4536 
4537 		  h->type = type;
4538 		}
4539 	    }
4540 
4541 	  /* Merge st_other field.  */
4542 	  elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4543 
4544 	  /* We don't want to make debug symbol dynamic.  */
4545 	  if (definition
4546 	      && (sec->flags & SEC_DEBUGGING)
4547 	      && !bfd_link_relocatable (info))
4548 	    dynsym = FALSE;
4549 
4550 	  /* Nor should we make plugin symbols dynamic.  */
4551 	  if ((abfd->flags & BFD_PLUGIN) != 0)
4552 	    dynsym = FALSE;
4553 
4554 	  if (definition)
4555 	    {
4556 	      h->target_internal = isym->st_target_internal;
4557 	      h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4558 	    }
4559 
4560 	  if (definition && !dynamic)
4561 	    {
4562 	      char *p = strchr (name, ELF_VER_CHR);
4563 	      if (p != NULL && p[1] != ELF_VER_CHR)
4564 		{
4565 		  /* Queue non-default versions so that .symver x, x@FOO
4566 		     aliases can be checked.  */
4567 		  if (!nondeflt_vers)
4568 		    {
4569 		      amt = ((isymend - isym + 1)
4570 			     * sizeof (struct elf_link_hash_entry *));
4571 		      nondeflt_vers
4572 			= (struct elf_link_hash_entry **) bfd_malloc (amt);
4573 		      if (!nondeflt_vers)
4574 			goto error_free_vers;
4575 		    }
4576 		  nondeflt_vers[nondeflt_vers_cnt++] = h;
4577 		}
4578 	    }
4579 
4580 	  if (dynsym && h->dynindx == -1)
4581 	    {
4582 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4583 		goto error_free_vers;
4584 	      if (h->u.weakdef != NULL
4585 		  && ! new_weakdef
4586 		  && h->u.weakdef->dynindx == -1)
4587 		{
4588 		  if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4589 		    goto error_free_vers;
4590 		}
4591 	    }
4592 	  else if (dynsym && h->dynindx != -1)
4593 	    /* If the symbol already has a dynamic index, but
4594 	       visibility says it should not be visible, turn it into
4595 	       a local symbol.  */
4596 	    switch (ELF_ST_VISIBILITY (h->other))
4597 	      {
4598 	      case STV_INTERNAL:
4599 	      case STV_HIDDEN:
4600 		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
4601 		dynsym = FALSE;
4602 		break;
4603 	      }
4604 
4605 	  /* Don't add DT_NEEDED for references from the dummy bfd nor
4606 	     for unmatched symbol.  */
4607 	  if (!add_needed
4608 	      && matched
4609 	      && definition
4610 	      && ((dynsym
4611 		   && h->ref_regular_nonweak
4612 		   && (old_bfd == NULL
4613 		       || (old_bfd->flags & BFD_PLUGIN) == 0))
4614 		  || (h->ref_dynamic_nonweak
4615 		      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4616 		      && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4617 	    {
4618 	      int ret;
4619 	      const char *soname = elf_dt_name (abfd);
4620 
4621 	      info->callbacks->minfo ("%!", soname, old_bfd,
4622 				      h->root.root.string);
4623 
4624 	      /* A symbol from a library loaded via DT_NEEDED of some
4625 		 other library is referenced by a regular object.
4626 		 Add a DT_NEEDED entry for it.  Issue an error if
4627 		 --no-add-needed is used and the reference was not
4628 		 a weak one.  */
4629 	      if (old_bfd != NULL
4630 		  && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4631 		{
4632 		  (*_bfd_error_handler)
4633 		    (_("%B: undefined reference to symbol '%s'"),
4634 		     old_bfd, name);
4635 		  bfd_set_error (bfd_error_missing_dso);
4636 		  goto error_free_vers;
4637 		}
4638 
4639 	      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4640 		(elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4641 
4642 	      add_needed = TRUE;
4643 	      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4644 	      if (ret < 0)
4645 		goto error_free_vers;
4646 
4647 	      BFD_ASSERT (ret == 0);
4648 	    }
4649 	}
4650     }
4651 
4652   if (extversym != NULL)
4653     {
4654       free (extversym);
4655       extversym = NULL;
4656     }
4657 
4658   if (isymbuf != NULL)
4659     {
4660       free (isymbuf);
4661       isymbuf = NULL;
4662     }
4663 
4664   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4665     {
4666       unsigned int i;
4667 
4668       /* Restore the symbol table.  */
4669       old_ent = (char *) old_tab + tabsize;
4670       memset (elf_sym_hashes (abfd), 0,
4671 	      extsymcount * sizeof (struct elf_link_hash_entry *));
4672       htab->root.table.table = old_table;
4673       htab->root.table.size = old_size;
4674       htab->root.table.count = old_count;
4675       memcpy (htab->root.table.table, old_tab, tabsize);
4676       htab->root.undefs = old_undefs;
4677       htab->root.undefs_tail = old_undefs_tail;
4678       _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4679       free (old_strtab);
4680       old_strtab = NULL;
4681       for (i = 0; i < htab->root.table.size; i++)
4682 	{
4683 	  struct bfd_hash_entry *p;
4684 	  struct elf_link_hash_entry *h;
4685 	  bfd_size_type size;
4686 	  unsigned int alignment_power;
4687 
4688 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4689 	    {
4690 	      h = (struct elf_link_hash_entry *) p;
4691 	      if (h->root.type == bfd_link_hash_warning)
4692 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
4693 
4694 	      /* Preserve the maximum alignment and size for common
4695 		 symbols even if this dynamic lib isn't on DT_NEEDED
4696 		 since it can still be loaded at run time by another
4697 		 dynamic lib.  */
4698 	      if (h->root.type == bfd_link_hash_common)
4699 		{
4700 		  size = h->root.u.c.size;
4701 		  alignment_power = h->root.u.c.p->alignment_power;
4702 		}
4703 	      else
4704 		{
4705 		  size = 0;
4706 		  alignment_power = 0;
4707 		}
4708 	      memcpy (p, old_ent, htab->root.table.entsize);
4709 	      old_ent = (char *) old_ent + htab->root.table.entsize;
4710 	      h = (struct elf_link_hash_entry *) p;
4711 	      if (h->root.type == bfd_link_hash_warning)
4712 		{
4713 		  memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4714 		  old_ent = (char *) old_ent + htab->root.table.entsize;
4715 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
4716 		}
4717 	      if (h->root.type == bfd_link_hash_common)
4718 		{
4719 		  if (size > h->root.u.c.size)
4720 		    h->root.u.c.size = size;
4721 		  if (alignment_power > h->root.u.c.p->alignment_power)
4722 		    h->root.u.c.p->alignment_power = alignment_power;
4723 		}
4724 	    }
4725 	}
4726 
4727       /* Make a special call to the linker "notice" function to
4728 	 tell it that symbols added for crefs may need to be removed.  */
4729       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4730 	goto error_free_vers;
4731 
4732       free (old_tab);
4733       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4734 			   alloc_mark);
4735       if (nondeflt_vers != NULL)
4736 	free (nondeflt_vers);
4737       return TRUE;
4738     }
4739 
4740   if (old_tab != NULL)
4741     {
4742       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4743 	goto error_free_vers;
4744       free (old_tab);
4745       old_tab = NULL;
4746     }
4747 
4748   /* Now that all the symbols from this input file are created, if
4749      not performing a relocatable link, handle .symver foo, foo@BAR
4750      such that any relocs against foo become foo@BAR.  */
4751   if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4752     {
4753       bfd_size_type cnt, symidx;
4754 
4755       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4756 	{
4757 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4758 	  char *shortname, *p;
4759 
4760 	  p = strchr (h->root.root.string, ELF_VER_CHR);
4761 	  if (p == NULL
4762 	      || (h->root.type != bfd_link_hash_defined
4763 		  && h->root.type != bfd_link_hash_defweak))
4764 	    continue;
4765 
4766 	  amt = p - h->root.root.string;
4767 	  shortname = (char *) bfd_malloc (amt + 1);
4768 	  if (!shortname)
4769 	    goto error_free_vers;
4770 	  memcpy (shortname, h->root.root.string, amt);
4771 	  shortname[amt] = '\0';
4772 
4773 	  hi = (struct elf_link_hash_entry *)
4774 	       bfd_link_hash_lookup (&htab->root, shortname,
4775 				     FALSE, FALSE, FALSE);
4776 	  if (hi != NULL
4777 	      && hi->root.type == h->root.type
4778 	      && hi->root.u.def.value == h->root.u.def.value
4779 	      && hi->root.u.def.section == h->root.u.def.section)
4780 	    {
4781 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4782 	      hi->root.type = bfd_link_hash_indirect;
4783 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4784 	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4785 	      sym_hash = elf_sym_hashes (abfd);
4786 	      if (sym_hash)
4787 		for (symidx = 0; symidx < extsymcount; ++symidx)
4788 		  if (sym_hash[symidx] == hi)
4789 		    {
4790 		      sym_hash[symidx] = h;
4791 		      break;
4792 		    }
4793 	    }
4794 	  free (shortname);
4795 	}
4796       free (nondeflt_vers);
4797       nondeflt_vers = NULL;
4798     }
4799 
4800   /* Now set the weakdefs field correctly for all the weak defined
4801      symbols we found.  The only way to do this is to search all the
4802      symbols.  Since we only need the information for non functions in
4803      dynamic objects, that's the only time we actually put anything on
4804      the list WEAKS.  We need this information so that if a regular
4805      object refers to a symbol defined weakly in a dynamic object, the
4806      real symbol in the dynamic object is also put in the dynamic
4807      symbols; we also must arrange for both symbols to point to the
4808      same memory location.  We could handle the general case of symbol
4809      aliasing, but a general symbol alias can only be generated in
4810      assembler code, handling it correctly would be very time
4811      consuming, and other ELF linkers don't handle general aliasing
4812      either.  */
4813   if (weaks != NULL)
4814     {
4815       struct elf_link_hash_entry **hpp;
4816       struct elf_link_hash_entry **hppend;
4817       struct elf_link_hash_entry **sorted_sym_hash;
4818       struct elf_link_hash_entry *h;
4819       size_t sym_count;
4820 
4821       /* Since we have to search the whole symbol list for each weak
4822 	 defined symbol, search time for N weak defined symbols will be
4823 	 O(N^2). Binary search will cut it down to O(NlogN).  */
4824       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4825       sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4826       if (sorted_sym_hash == NULL)
4827 	goto error_return;
4828       sym_hash = sorted_sym_hash;
4829       hpp = elf_sym_hashes (abfd);
4830       hppend = hpp + extsymcount;
4831       sym_count = 0;
4832       for (; hpp < hppend; hpp++)
4833 	{
4834 	  h = *hpp;
4835 	  if (h != NULL
4836 	      && h->root.type == bfd_link_hash_defined
4837 	      && !bed->is_function_type (h->type))
4838 	    {
4839 	      *sym_hash = h;
4840 	      sym_hash++;
4841 	      sym_count++;
4842 	    }
4843 	}
4844 
4845       qsort (sorted_sym_hash, sym_count,
4846 	     sizeof (struct elf_link_hash_entry *),
4847 	     elf_sort_symbol);
4848 
4849       while (weaks != NULL)
4850 	{
4851 	  struct elf_link_hash_entry *hlook;
4852 	  asection *slook;
4853 	  bfd_vma vlook;
4854 	  size_t i, j, idx = 0;
4855 
4856 	  hlook = weaks;
4857 	  weaks = hlook->u.weakdef;
4858 	  hlook->u.weakdef = NULL;
4859 
4860 	  BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4861 		      || hlook->root.type == bfd_link_hash_defweak
4862 		      || hlook->root.type == bfd_link_hash_common
4863 		      || hlook->root.type == bfd_link_hash_indirect);
4864 	  slook = hlook->root.u.def.section;
4865 	  vlook = hlook->root.u.def.value;
4866 
4867 	  i = 0;
4868 	  j = sym_count;
4869 	  while (i != j)
4870 	    {
4871 	      bfd_signed_vma vdiff;
4872 	      idx = (i + j) / 2;
4873 	      h = sorted_sym_hash[idx];
4874 	      vdiff = vlook - h->root.u.def.value;
4875 	      if (vdiff < 0)
4876 		j = idx;
4877 	      else if (vdiff > 0)
4878 		i = idx + 1;
4879 	      else
4880 		{
4881 		  int sdiff = slook->id - h->root.u.def.section->id;
4882 		  if (sdiff < 0)
4883 		    j = idx;
4884 		  else if (sdiff > 0)
4885 		    i = idx + 1;
4886 		  else
4887 		    break;
4888 		}
4889 	    }
4890 
4891 	  /* We didn't find a value/section match.  */
4892 	  if (i == j)
4893 	    continue;
4894 
4895 	  /* With multiple aliases, or when the weak symbol is already
4896 	     strongly defined, we have multiple matching symbols and
4897 	     the binary search above may land on any of them.  Step
4898 	     one past the matching symbol(s).  */
4899 	  while (++idx != j)
4900 	    {
4901 	      h = sorted_sym_hash[idx];
4902 	      if (h->root.u.def.section != slook
4903 		  || h->root.u.def.value != vlook)
4904 		break;
4905 	    }
4906 
4907 	  /* Now look back over the aliases.  Since we sorted by size
4908 	     as well as value and section, we'll choose the one with
4909 	     the largest size.  */
4910 	  while (idx-- != i)
4911 	    {
4912 	      h = sorted_sym_hash[idx];
4913 
4914 	      /* Stop if value or section doesn't match.  */
4915 	      if (h->root.u.def.section != slook
4916 		  || h->root.u.def.value != vlook)
4917 		break;
4918 	      else if (h != hlook)
4919 		{
4920 		  hlook->u.weakdef = h;
4921 
4922 		  /* If the weak definition is in the list of dynamic
4923 		     symbols, make sure the real definition is put
4924 		     there as well.  */
4925 		  if (hlook->dynindx != -1 && h->dynindx == -1)
4926 		    {
4927 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4928 			{
4929 			err_free_sym_hash:
4930 			  free (sorted_sym_hash);
4931 			  goto error_return;
4932 			}
4933 		    }
4934 
4935 		  /* If the real definition is in the list of dynamic
4936 		     symbols, make sure the weak definition is put
4937 		     there as well.  If we don't do this, then the
4938 		     dynamic loader might not merge the entries for the
4939 		     real definition and the weak definition.  */
4940 		  if (h->dynindx != -1 && hlook->dynindx == -1)
4941 		    {
4942 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4943 			goto err_free_sym_hash;
4944 		    }
4945 		  break;
4946 		}
4947 	    }
4948 	}
4949 
4950       free (sorted_sym_hash);
4951     }
4952 
4953   if (bed->check_directives
4954       && !(*bed->check_directives) (abfd, info))
4955     return FALSE;
4956 
4957   /* If this object is the same format as the output object, and it is
4958      not a shared library, then let the backend look through the
4959      relocs.
4960 
4961      This is required to build global offset table entries and to
4962      arrange for dynamic relocs.  It is not required for the
4963      particular common case of linking non PIC code, even when linking
4964      against shared libraries, but unfortunately there is no way of
4965      knowing whether an object file has been compiled PIC or not.
4966      Looking through the relocs is not particularly time consuming.
4967      The problem is that we must either (1) keep the relocs in memory,
4968      which causes the linker to require additional runtime memory or
4969      (2) read the relocs twice from the input file, which wastes time.
4970      This would be a good case for using mmap.
4971 
4972      I have no idea how to handle linking PIC code into a file of a
4973      different format.  It probably can't be done.  */
4974   if (! dynamic
4975       && is_elf_hash_table (htab)
4976       && bed->check_relocs != NULL
4977       && elf_object_id (abfd) == elf_hash_table_id (htab)
4978       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4979     {
4980       asection *o;
4981 
4982       for (o = abfd->sections; o != NULL; o = o->next)
4983 	{
4984 	  Elf_Internal_Rela *internal_relocs;
4985 	  bfd_boolean ok;
4986 
4987 	  if ((o->flags & SEC_RELOC) == 0
4988 	      || o->reloc_count == 0
4989 	      || ((info->strip == strip_all || info->strip == strip_debugger)
4990 		  && (o->flags & SEC_DEBUGGING) != 0)
4991 	      || bfd_is_abs_section (o->output_section))
4992 	    continue;
4993 
4994 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4995 						       info->keep_memory);
4996 	  if (internal_relocs == NULL)
4997 	    goto error_return;
4998 
4999 	  ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
5000 
5001 	  if (elf_section_data (o)->relocs != internal_relocs)
5002 	    free (internal_relocs);
5003 
5004 	  if (! ok)
5005 	    goto error_return;
5006 	}
5007     }
5008 
5009   /* If this is a non-traditional link, try to optimize the handling
5010      of the .stab/.stabstr sections.  */
5011   if (! dynamic
5012       && ! info->traditional_format
5013       && is_elf_hash_table (htab)
5014       && (info->strip != strip_all && info->strip != strip_debugger))
5015     {
5016       asection *stabstr;
5017 
5018       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5019       if (stabstr != NULL)
5020 	{
5021 	  bfd_size_type string_offset = 0;
5022 	  asection *stab;
5023 
5024 	  for (stab = abfd->sections; stab; stab = stab->next)
5025 	    if (CONST_STRNEQ (stab->name, ".stab")
5026 		&& (!stab->name[5] ||
5027 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5028 		&& (stab->flags & SEC_MERGE) == 0
5029 		&& !bfd_is_abs_section (stab->output_section))
5030 	      {
5031 		struct bfd_elf_section_data *secdata;
5032 
5033 		secdata = elf_section_data (stab);
5034 		if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5035 					       stabstr, &secdata->sec_info,
5036 					       &string_offset))
5037 		  goto error_return;
5038 		if (secdata->sec_info)
5039 		  stab->sec_info_type = SEC_INFO_TYPE_STABS;
5040 	    }
5041 	}
5042     }
5043 
5044   if (is_elf_hash_table (htab) && add_needed)
5045     {
5046       /* Add this bfd to the loaded list.  */
5047       struct elf_link_loaded_list *n;
5048 
5049       n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5050       if (n == NULL)
5051 	goto error_return;
5052       n->abfd = abfd;
5053       n->next = htab->loaded;
5054       htab->loaded = n;
5055     }
5056 
5057   return TRUE;
5058 
5059  error_free_vers:
5060   if (old_tab != NULL)
5061     free (old_tab);
5062   if (old_strtab != NULL)
5063     free (old_strtab);
5064   if (nondeflt_vers != NULL)
5065     free (nondeflt_vers);
5066   if (extversym != NULL)
5067     free (extversym);
5068  error_free_sym:
5069   if (isymbuf != NULL)
5070     free (isymbuf);
5071  error_return:
5072   return FALSE;
5073 }
5074 
5075 /* Return the linker hash table entry of a symbol that might be
5076    satisfied by an archive symbol.  Return -1 on error.  */
5077 
5078 struct elf_link_hash_entry *
5079 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5080 				struct bfd_link_info *info,
5081 				const char *name)
5082 {
5083   struct elf_link_hash_entry *h;
5084   char *p, *copy;
5085   size_t len, first;
5086 
5087   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5088   if (h != NULL)
5089     return h;
5090 
5091   /* If this is a default version (the name contains @@), look up the
5092      symbol again with only one `@' as well as without the version.
5093      The effect is that references to the symbol with and without the
5094      version will be matched by the default symbol in the archive.  */
5095 
5096   p = strchr (name, ELF_VER_CHR);
5097   if (p == NULL || p[1] != ELF_VER_CHR)
5098     return h;
5099 
5100   /* First check with only one `@'.  */
5101   len = strlen (name);
5102   copy = (char *) bfd_alloc (abfd, len);
5103   if (copy == NULL)
5104     return (struct elf_link_hash_entry *) 0 - 1;
5105 
5106   first = p - name + 1;
5107   memcpy (copy, name, first);
5108   memcpy (copy + first, name + first + 1, len - first);
5109 
5110   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5111   if (h == NULL)
5112     {
5113       /* We also need to check references to the symbol without the
5114 	 version.  */
5115       copy[first - 1] = '\0';
5116       h = elf_link_hash_lookup (elf_hash_table (info), copy,
5117 				FALSE, FALSE, TRUE);
5118     }
5119 
5120   bfd_release (abfd, copy);
5121   return h;
5122 }
5123 
5124 /* Add symbols from an ELF archive file to the linker hash table.  We
5125    don't use _bfd_generic_link_add_archive_symbols because we need to
5126    handle versioned symbols.
5127 
5128    Fortunately, ELF archive handling is simpler than that done by
5129    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5130    oddities.  In ELF, if we find a symbol in the archive map, and the
5131    symbol is currently undefined, we know that we must pull in that
5132    object file.
5133 
5134    Unfortunately, we do have to make multiple passes over the symbol
5135    table until nothing further is resolved.  */
5136 
5137 static bfd_boolean
5138 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5139 {
5140   symindex c;
5141   unsigned char *included = NULL;
5142   carsym *symdefs;
5143   bfd_boolean loop;
5144   bfd_size_type amt;
5145   const struct elf_backend_data *bed;
5146   struct elf_link_hash_entry * (*archive_symbol_lookup)
5147     (bfd *, struct bfd_link_info *, const char *);
5148 
5149   if (! bfd_has_map (abfd))
5150     {
5151       /* An empty archive is a special case.  */
5152       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5153 	return TRUE;
5154       bfd_set_error (bfd_error_no_armap);
5155       return FALSE;
5156     }
5157 
5158   /* Keep track of all symbols we know to be already defined, and all
5159      files we know to be already included.  This is to speed up the
5160      second and subsequent passes.  */
5161   c = bfd_ardata (abfd)->symdef_count;
5162   if (c == 0)
5163     return TRUE;
5164   amt = c;
5165   amt *= sizeof (*included);
5166   included = (unsigned char *) bfd_zmalloc (amt);
5167   if (included == NULL)
5168     return FALSE;
5169 
5170   symdefs = bfd_ardata (abfd)->symdefs;
5171   bed = get_elf_backend_data (abfd);
5172   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5173 
5174   do
5175     {
5176       file_ptr last;
5177       symindex i;
5178       carsym *symdef;
5179       carsym *symdefend;
5180 
5181       loop = FALSE;
5182       last = -1;
5183 
5184       symdef = symdefs;
5185       symdefend = symdef + c;
5186       for (i = 0; symdef < symdefend; symdef++, i++)
5187 	{
5188 	  struct elf_link_hash_entry *h;
5189 	  bfd *element;
5190 	  struct bfd_link_hash_entry *undefs_tail;
5191 	  symindex mark;
5192 
5193 	  if (included[i])
5194 	    continue;
5195 	  if (symdef->file_offset == last)
5196 	    {
5197 	      included[i] = TRUE;
5198 	      continue;
5199 	    }
5200 
5201 	  h = archive_symbol_lookup (abfd, info, symdef->name);
5202 	  if (h == (struct elf_link_hash_entry *) 0 - 1)
5203 	    goto error_return;
5204 
5205 	  if (h == NULL)
5206 	    continue;
5207 
5208 	  if (h->root.type == bfd_link_hash_common)
5209 	    {
5210 	      /* We currently have a common symbol.  The archive map contains
5211 		 a reference to this symbol, so we may want to include it.  We
5212 		 only want to include it however, if this archive element
5213 		 contains a definition of the symbol, not just another common
5214 		 declaration of it.
5215 
5216 		 Unfortunately some archivers (including GNU ar) will put
5217 		 declarations of common symbols into their archive maps, as
5218 		 well as real definitions, so we cannot just go by the archive
5219 		 map alone.  Instead we must read in the element's symbol
5220 		 table and check that to see what kind of symbol definition
5221 		 this is.  */
5222 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5223 		continue;
5224 	    }
5225 	  else if (h->root.type != bfd_link_hash_undefined)
5226 	    {
5227 	      if (h->root.type != bfd_link_hash_undefweak)
5228 		/* Symbol must be defined.  Don't check it again.  */
5229 		included[i] = TRUE;
5230 	      continue;
5231 	    }
5232 
5233 	  /* We need to include this archive member.  */
5234 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5235 	  if (element == NULL)
5236 	    goto error_return;
5237 
5238 	  if (! bfd_check_format (element, bfd_object))
5239 	    goto error_return;
5240 
5241 	  undefs_tail = info->hash->undefs_tail;
5242 
5243 	  if (!(*info->callbacks
5244 		->add_archive_element) (info, element, symdef->name, &element))
5245 	    goto error_return;
5246 	  if (!bfd_link_add_symbols (element, info))
5247 	    goto error_return;
5248 
5249 	  /* If there are any new undefined symbols, we need to make
5250 	     another pass through the archive in order to see whether
5251 	     they can be defined.  FIXME: This isn't perfect, because
5252 	     common symbols wind up on undefs_tail and because an
5253 	     undefined symbol which is defined later on in this pass
5254 	     does not require another pass.  This isn't a bug, but it
5255 	     does make the code less efficient than it could be.  */
5256 	  if (undefs_tail != info->hash->undefs_tail)
5257 	    loop = TRUE;
5258 
5259 	  /* Look backward to mark all symbols from this object file
5260 	     which we have already seen in this pass.  */
5261 	  mark = i;
5262 	  do
5263 	    {
5264 	      included[mark] = TRUE;
5265 	      if (mark == 0)
5266 		break;
5267 	      --mark;
5268 	    }
5269 	  while (symdefs[mark].file_offset == symdef->file_offset);
5270 
5271 	  /* We mark subsequent symbols from this object file as we go
5272 	     on through the loop.  */
5273 	  last = symdef->file_offset;
5274 	}
5275     }
5276   while (loop);
5277 
5278   free (included);
5279 
5280   return TRUE;
5281 
5282  error_return:
5283   if (included != NULL)
5284     free (included);
5285   return FALSE;
5286 }
5287 
5288 /* Given an ELF BFD, add symbols to the global hash table as
5289    appropriate.  */
5290 
5291 bfd_boolean
5292 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5293 {
5294   switch (bfd_get_format (abfd))
5295     {
5296     case bfd_object:
5297       return elf_link_add_object_symbols (abfd, info);
5298     case bfd_archive:
5299       return elf_link_add_archive_symbols (abfd, info);
5300     default:
5301       bfd_set_error (bfd_error_wrong_format);
5302       return FALSE;
5303     }
5304 }
5305 
5306 struct hash_codes_info
5307 {
5308   unsigned long *hashcodes;
5309   bfd_boolean error;
5310 };
5311 
5312 /* This function will be called though elf_link_hash_traverse to store
5313    all hash value of the exported symbols in an array.  */
5314 
5315 static bfd_boolean
5316 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5317 {
5318   struct hash_codes_info *inf = (struct hash_codes_info *) data;
5319   const char *name;
5320   unsigned long ha;
5321   char *alc = NULL;
5322 
5323   /* Ignore indirect symbols.  These are added by the versioning code.  */
5324   if (h->dynindx == -1)
5325     return TRUE;
5326 
5327   name = h->root.root.string;
5328   if (h->versioned >= versioned)
5329     {
5330       char *p = strchr (name, ELF_VER_CHR);
5331       if (p != NULL)
5332 	{
5333 	  alc = (char *) bfd_malloc (p - name + 1);
5334 	  if (alc == NULL)
5335 	    {
5336 	      inf->error = TRUE;
5337 	      return FALSE;
5338 	    }
5339 	  memcpy (alc, name, p - name);
5340 	  alc[p - name] = '\0';
5341 	  name = alc;
5342 	}
5343     }
5344 
5345   /* Compute the hash value.  */
5346   ha = bfd_elf_hash (name);
5347 
5348   /* Store the found hash value in the array given as the argument.  */
5349   *(inf->hashcodes)++ = ha;
5350 
5351   /* And store it in the struct so that we can put it in the hash table
5352      later.  */
5353   h->u.elf_hash_value = ha;
5354 
5355   if (alc != NULL)
5356     free (alc);
5357 
5358   return TRUE;
5359 }
5360 
5361 struct collect_gnu_hash_codes
5362 {
5363   bfd *output_bfd;
5364   const struct elf_backend_data *bed;
5365   unsigned long int nsyms;
5366   unsigned long int maskbits;
5367   unsigned long int *hashcodes;
5368   unsigned long int *hashval;
5369   unsigned long int *indx;
5370   unsigned long int *counts;
5371   bfd_vma *bitmask;
5372   bfd_byte *contents;
5373   long int min_dynindx;
5374   unsigned long int bucketcount;
5375   unsigned long int symindx;
5376   long int local_indx;
5377   long int shift1, shift2;
5378   unsigned long int mask;
5379   bfd_boolean error;
5380 };
5381 
5382 /* This function will be called though elf_link_hash_traverse to store
5383    all hash value of the exported symbols in an array.  */
5384 
5385 static bfd_boolean
5386 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5387 {
5388   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5389   const char *name;
5390   unsigned long ha;
5391   char *alc = NULL;
5392 
5393   /* Ignore indirect symbols.  These are added by the versioning code.  */
5394   if (h->dynindx == -1)
5395     return TRUE;
5396 
5397   /* Ignore also local symbols and undefined symbols.  */
5398   if (! (*s->bed->elf_hash_symbol) (h))
5399     return TRUE;
5400 
5401   name = h->root.root.string;
5402   if (h->versioned >= versioned)
5403     {
5404       char *p = strchr (name, ELF_VER_CHR);
5405       if (p != NULL)
5406 	{
5407 	  alc = (char *) bfd_malloc (p - name + 1);
5408 	  if (alc == NULL)
5409 	    {
5410 	      s->error = TRUE;
5411 	      return FALSE;
5412 	    }
5413 	  memcpy (alc, name, p - name);
5414 	  alc[p - name] = '\0';
5415 	  name = alc;
5416 	}
5417     }
5418 
5419   /* Compute the hash value.  */
5420   ha = bfd_elf_gnu_hash (name);
5421 
5422   /* Store the found hash value in the array for compute_bucket_count,
5423      and also for .dynsym reordering purposes.  */
5424   s->hashcodes[s->nsyms] = ha;
5425   s->hashval[h->dynindx] = ha;
5426   ++s->nsyms;
5427   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5428     s->min_dynindx = h->dynindx;
5429 
5430   if (alc != NULL)
5431     free (alc);
5432 
5433   return TRUE;
5434 }
5435 
5436 /* This function will be called though elf_link_hash_traverse to do
5437    final dynaminc symbol renumbering.  */
5438 
5439 static bfd_boolean
5440 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5441 {
5442   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5443   unsigned long int bucket;
5444   unsigned long int val;
5445 
5446   /* Ignore indirect symbols.  */
5447   if (h->dynindx == -1)
5448     return TRUE;
5449 
5450   /* Ignore also local symbols and undefined symbols.  */
5451   if (! (*s->bed->elf_hash_symbol) (h))
5452     {
5453       if (h->dynindx >= s->min_dynindx)
5454 	h->dynindx = s->local_indx++;
5455       return TRUE;
5456     }
5457 
5458   bucket = s->hashval[h->dynindx] % s->bucketcount;
5459   val = (s->hashval[h->dynindx] >> s->shift1)
5460 	& ((s->maskbits >> s->shift1) - 1);
5461   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5462   s->bitmask[val]
5463     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5464   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5465   if (s->counts[bucket] == 1)
5466     /* Last element terminates the chain.  */
5467     val |= 1;
5468   bfd_put_32 (s->output_bfd, val,
5469 	      s->contents + (s->indx[bucket] - s->symindx) * 4);
5470   --s->counts[bucket];
5471   h->dynindx = s->indx[bucket]++;
5472   return TRUE;
5473 }
5474 
5475 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
5476 
5477 bfd_boolean
5478 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5479 {
5480   return !(h->forced_local
5481 	   || h->root.type == bfd_link_hash_undefined
5482 	   || h->root.type == bfd_link_hash_undefweak
5483 	   || ((h->root.type == bfd_link_hash_defined
5484 		|| h->root.type == bfd_link_hash_defweak)
5485 	       && h->root.u.def.section->output_section == NULL));
5486 }
5487 
5488 /* Array used to determine the number of hash table buckets to use
5489    based on the number of symbols there are.  If there are fewer than
5490    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5491    fewer than 37 we use 17 buckets, and so forth.  We never use more
5492    than 32771 buckets.  */
5493 
5494 static const size_t elf_buckets[] =
5495 {
5496   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5497   16411, 32771, 0
5498 };
5499 
5500 /* Compute bucket count for hashing table.  We do not use a static set
5501    of possible tables sizes anymore.  Instead we determine for all
5502    possible reasonable sizes of the table the outcome (i.e., the
5503    number of collisions etc) and choose the best solution.  The
5504    weighting functions are not too simple to allow the table to grow
5505    without bounds.  Instead one of the weighting factors is the size.
5506    Therefore the result is always a good payoff between few collisions
5507    (= short chain lengths) and table size.  */
5508 static size_t
5509 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5510 		      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5511 		      unsigned long int nsyms,
5512 		      int gnu_hash)
5513 {
5514   size_t best_size = 0;
5515   unsigned long int i;
5516 
5517   /* We have a problem here.  The following code to optimize the table
5518      size requires an integer type with more the 32 bits.  If
5519      BFD_HOST_U_64_BIT is set we know about such a type.  */
5520 #ifdef BFD_HOST_U_64_BIT
5521   if (info->optimize)
5522     {
5523       size_t minsize;
5524       size_t maxsize;
5525       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5526       bfd *dynobj = elf_hash_table (info)->dynobj;
5527       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5528       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5529       unsigned long int *counts;
5530       bfd_size_type amt;
5531       unsigned int no_improvement_count = 0;
5532 
5533       /* Possible optimization parameters: if we have NSYMS symbols we say
5534 	 that the hashing table must at least have NSYMS/4 and at most
5535 	 2*NSYMS buckets.  */
5536       minsize = nsyms / 4;
5537       if (minsize == 0)
5538 	minsize = 1;
5539       best_size = maxsize = nsyms * 2;
5540       if (gnu_hash)
5541 	{
5542 	  if (minsize < 2)
5543 	    minsize = 2;
5544 	  if ((best_size & 31) == 0)
5545 	    ++best_size;
5546 	}
5547 
5548       /* Create array where we count the collisions in.  We must use bfd_malloc
5549 	 since the size could be large.  */
5550       amt = maxsize;
5551       amt *= sizeof (unsigned long int);
5552       counts = (unsigned long int *) bfd_malloc (amt);
5553       if (counts == NULL)
5554 	return 0;
5555 
5556       /* Compute the "optimal" size for the hash table.  The criteria is a
5557 	 minimal chain length.  The minor criteria is (of course) the size
5558 	 of the table.  */
5559       for (i = minsize; i < maxsize; ++i)
5560 	{
5561 	  /* Walk through the array of hashcodes and count the collisions.  */
5562 	  BFD_HOST_U_64_BIT max;
5563 	  unsigned long int j;
5564 	  unsigned long int fact;
5565 
5566 	  if (gnu_hash && (i & 31) == 0)
5567 	    continue;
5568 
5569 	  memset (counts, '\0', i * sizeof (unsigned long int));
5570 
5571 	  /* Determine how often each hash bucket is used.  */
5572 	  for (j = 0; j < nsyms; ++j)
5573 	    ++counts[hashcodes[j] % i];
5574 
5575 	  /* For the weight function we need some information about the
5576 	     pagesize on the target.  This is information need not be 100%
5577 	     accurate.  Since this information is not available (so far) we
5578 	     define it here to a reasonable default value.  If it is crucial
5579 	     to have a better value some day simply define this value.  */
5580 # ifndef BFD_TARGET_PAGESIZE
5581 #  define BFD_TARGET_PAGESIZE	(4096)
5582 # endif
5583 
5584 	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5585 	     and the chains.  */
5586 	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5587 
5588 # if 1
5589 	  /* Variant 1: optimize for short chains.  We add the squares
5590 	     of all the chain lengths (which favors many small chain
5591 	     over a few long chains).  */
5592 	  for (j = 0; j < i; ++j)
5593 	    max += counts[j] * counts[j];
5594 
5595 	  /* This adds penalties for the overall size of the table.  */
5596 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5597 	  max *= fact * fact;
5598 # else
5599 	  /* Variant 2: Optimize a lot more for small table.  Here we
5600 	     also add squares of the size but we also add penalties for
5601 	     empty slots (the +1 term).  */
5602 	  for (j = 0; j < i; ++j)
5603 	    max += (1 + counts[j]) * (1 + counts[j]);
5604 
5605 	  /* The overall size of the table is considered, but not as
5606 	     strong as in variant 1, where it is squared.  */
5607 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5608 	  max *= fact;
5609 # endif
5610 
5611 	  /* Compare with current best results.  */
5612 	  if (max < best_chlen)
5613 	    {
5614 	      best_chlen = max;
5615 	      best_size = i;
5616 	      no_improvement_count = 0;
5617 	    }
5618 	  /* PR 11843: Avoid futile long searches for the best bucket size
5619 	     when there are a large number of symbols.  */
5620 	  else if (++no_improvement_count == 100)
5621 	    break;
5622 	}
5623 
5624       free (counts);
5625     }
5626   else
5627 #endif /* defined (BFD_HOST_U_64_BIT) */
5628     {
5629       /* This is the fallback solution if no 64bit type is available or if we
5630 	 are not supposed to spend much time on optimizations.  We select the
5631 	 bucket count using a fixed set of numbers.  */
5632       for (i = 0; elf_buckets[i] != 0; i++)
5633 	{
5634 	  best_size = elf_buckets[i];
5635 	  if (nsyms < elf_buckets[i + 1])
5636 	    break;
5637 	}
5638       if (gnu_hash && best_size < 2)
5639 	best_size = 2;
5640     }
5641 
5642   return best_size;
5643 }
5644 
5645 /* Size any SHT_GROUP section for ld -r.  */
5646 
5647 bfd_boolean
5648 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5649 {
5650   bfd *ibfd;
5651 
5652   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5653     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5654 	&& !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5655       return FALSE;
5656   return TRUE;
5657 }
5658 
5659 /* Set a default stack segment size.  The value in INFO wins.  If it
5660    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5661    undefined it is initialized.  */
5662 
5663 bfd_boolean
5664 bfd_elf_stack_segment_size (bfd *output_bfd,
5665 			    struct bfd_link_info *info,
5666 			    const char *legacy_symbol,
5667 			    bfd_vma default_size)
5668 {
5669   struct elf_link_hash_entry *h = NULL;
5670 
5671   /* Look for legacy symbol.  */
5672   if (legacy_symbol)
5673     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5674 			      FALSE, FALSE, FALSE);
5675   if (h && (h->root.type == bfd_link_hash_defined
5676 	    || h->root.type == bfd_link_hash_defweak)
5677       && h->def_regular
5678       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5679     {
5680       /* The symbol has no type if specified on the command line.  */
5681       h->type = STT_OBJECT;
5682       if (info->stacksize)
5683 	(*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5684 			       output_bfd, legacy_symbol);
5685       else if (h->root.u.def.section != bfd_abs_section_ptr)
5686 	(*_bfd_error_handler) (_("%B: %s not absolute"),
5687 			       output_bfd, legacy_symbol);
5688       else
5689 	info->stacksize = h->root.u.def.value;
5690     }
5691 
5692   if (!info->stacksize)
5693     /* If the user didn't set a size, or explicitly inhibit the
5694        size, set it now.  */
5695     info->stacksize = default_size;
5696 
5697   /* Provide the legacy symbol, if it is referenced.  */
5698   if (h && (h->root.type == bfd_link_hash_undefined
5699 	    || h->root.type == bfd_link_hash_undefweak))
5700     {
5701       struct bfd_link_hash_entry *bh = NULL;
5702 
5703       if (!(_bfd_generic_link_add_one_symbol
5704 	    (info, output_bfd, legacy_symbol,
5705 	     BSF_GLOBAL, bfd_abs_section_ptr,
5706 	     info->stacksize >= 0 ? info->stacksize : 0,
5707 	     NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5708 	return FALSE;
5709 
5710       h = (struct elf_link_hash_entry *) bh;
5711       h->def_regular = 1;
5712       h->type = STT_OBJECT;
5713     }
5714 
5715   return TRUE;
5716 }
5717 
5718 /* Set up the sizes and contents of the ELF dynamic sections.  This is
5719    called by the ELF linker emulation before_allocation routine.  We
5720    must set the sizes of the sections before the linker sets the
5721    addresses of the various sections.  */
5722 
5723 bfd_boolean
5724 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5725 			       const char *soname,
5726 			       const char *rpath,
5727 			       const char *filter_shlib,
5728 			       const char *audit,
5729 			       const char *depaudit,
5730 			       const char * const *auxiliary_filters,
5731 			       struct bfd_link_info *info,
5732 			       asection **sinterpptr)
5733 {
5734   bfd_size_type soname_indx;
5735   bfd *dynobj;
5736   const struct elf_backend_data *bed;
5737   struct elf_info_failed asvinfo;
5738 
5739   *sinterpptr = NULL;
5740 
5741   soname_indx = (bfd_size_type) -1;
5742 
5743   if (!is_elf_hash_table (info->hash))
5744     return TRUE;
5745 
5746   bed = get_elf_backend_data (output_bfd);
5747 
5748   /* Any syms created from now on start with -1 in
5749      got.refcount/offset and plt.refcount/offset.  */
5750   elf_hash_table (info)->init_got_refcount
5751     = elf_hash_table (info)->init_got_offset;
5752   elf_hash_table (info)->init_plt_refcount
5753     = elf_hash_table (info)->init_plt_offset;
5754 
5755   if (bfd_link_relocatable (info)
5756       && !_bfd_elf_size_group_sections (info))
5757     return FALSE;
5758 
5759   /* The backend may have to create some sections regardless of whether
5760      we're dynamic or not.  */
5761   if (bed->elf_backend_always_size_sections
5762       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5763     return FALSE;
5764 
5765   /* Determine any GNU_STACK segment requirements, after the backend
5766      has had a chance to set a default segment size.  */
5767   if (info->execstack)
5768     elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5769   else if (info->noexecstack)
5770     elf_stack_flags (output_bfd) = PF_R | PF_W;
5771   else
5772     {
5773       bfd *inputobj;
5774       asection *notesec = NULL;
5775       int exec = 0;
5776 
5777       for (inputobj = info->input_bfds;
5778 	   inputobj;
5779 	   inputobj = inputobj->link.next)
5780 	{
5781 	  asection *s;
5782 
5783 	  if (inputobj->flags
5784 	      & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5785 	    continue;
5786 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5787 	  if (s)
5788 	    {
5789 	      if (s->flags & SEC_CODE)
5790 		exec = PF_X;
5791 	      notesec = s;
5792 	    }
5793 	  else if (bed->default_execstack)
5794 	    exec = PF_X;
5795 	}
5796       if (notesec || info->stacksize > 0)
5797 	elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5798       if (notesec && exec && bfd_link_relocatable (info)
5799 	  && notesec->output_section != bfd_abs_section_ptr)
5800 	notesec->output_section->flags |= SEC_CODE;
5801     }
5802 
5803   dynobj = elf_hash_table (info)->dynobj;
5804 
5805   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5806     {
5807       struct elf_info_failed eif;
5808       struct elf_link_hash_entry *h;
5809       asection *dynstr;
5810       struct bfd_elf_version_tree *t;
5811       struct bfd_elf_version_expr *d;
5812       asection *s;
5813       bfd_boolean all_defined;
5814 
5815       *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5816       BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5817 
5818       if (soname != NULL)
5819 	{
5820 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5821 					     soname, TRUE);
5822 	  if (soname_indx == (bfd_size_type) -1
5823 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5824 	    return FALSE;
5825 	}
5826 
5827       if (info->symbolic)
5828 	{
5829 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5830 	    return FALSE;
5831 	  info->flags |= DF_SYMBOLIC;
5832 	}
5833 
5834       if (rpath != NULL)
5835 	{
5836 	  bfd_size_type indx;
5837 	  bfd_vma tag;
5838 
5839 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5840 				      TRUE);
5841 	  if (indx == (bfd_size_type) -1)
5842 	    return FALSE;
5843 
5844 	  tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5845 	  if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5846 	    return FALSE;
5847 	}
5848 
5849       if (filter_shlib != NULL)
5850 	{
5851 	  bfd_size_type indx;
5852 
5853 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5854 				      filter_shlib, TRUE);
5855 	  if (indx == (bfd_size_type) -1
5856 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5857 	    return FALSE;
5858 	}
5859 
5860       if (auxiliary_filters != NULL)
5861 	{
5862 	  const char * const *p;
5863 
5864 	  for (p = auxiliary_filters; *p != NULL; p++)
5865 	    {
5866 	      bfd_size_type indx;
5867 
5868 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5869 					  *p, TRUE);
5870 	      if (indx == (bfd_size_type) -1
5871 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5872 		return FALSE;
5873 	    }
5874 	}
5875 
5876       if (audit != NULL)
5877 	{
5878 	  bfd_size_type indx;
5879 
5880 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5881 				      TRUE);
5882 	  if (indx == (bfd_size_type) -1
5883 	      || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5884 	    return FALSE;
5885 	}
5886 
5887       if (depaudit != NULL)
5888 	{
5889 	  bfd_size_type indx;
5890 
5891 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5892 				      TRUE);
5893 	  if (indx == (bfd_size_type) -1
5894 	      || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5895 	    return FALSE;
5896 	}
5897 
5898       eif.info = info;
5899       eif.failed = FALSE;
5900 
5901       /* If we are supposed to export all symbols into the dynamic symbol
5902 	 table (this is not the normal case), then do so.  */
5903       if (info->export_dynamic
5904 	  || (bfd_link_executable (info) && info->dynamic))
5905 	{
5906 	  elf_link_hash_traverse (elf_hash_table (info),
5907 				  _bfd_elf_export_symbol,
5908 				  &eif);
5909 	  if (eif.failed)
5910 	    return FALSE;
5911 	}
5912 
5913       /* Make all global versions with definition.  */
5914       for (t = info->version_info; t != NULL; t = t->next)
5915 	for (d = t->globals.list; d != NULL; d = d->next)
5916 	  if (!d->symver && d->literal)
5917 	    {
5918 	      const char *verstr, *name;
5919 	      size_t namelen, verlen, newlen;
5920 	      char *newname, *p, leading_char;
5921 	      struct elf_link_hash_entry *newh;
5922 
5923 	      leading_char = bfd_get_symbol_leading_char (output_bfd);
5924 	      name = d->pattern;
5925 	      namelen = strlen (name) + (leading_char != '\0');
5926 	      verstr = t->name;
5927 	      verlen = strlen (verstr);
5928 	      newlen = namelen + verlen + 3;
5929 
5930 	      newname = (char *) bfd_malloc (newlen);
5931 	      if (newname == NULL)
5932 		return FALSE;
5933 	      newname[0] = leading_char;
5934 	      memcpy (newname + (leading_char != '\0'), name, namelen);
5935 
5936 	      /* Check the hidden versioned definition.  */
5937 	      p = newname + namelen;
5938 	      *p++ = ELF_VER_CHR;
5939 	      memcpy (p, verstr, verlen + 1);
5940 	      newh = elf_link_hash_lookup (elf_hash_table (info),
5941 					   newname, FALSE, FALSE,
5942 					   FALSE);
5943 	      if (newh == NULL
5944 		  || (newh->root.type != bfd_link_hash_defined
5945 		      && newh->root.type != bfd_link_hash_defweak))
5946 		{
5947 		  /* Check the default versioned definition.  */
5948 		  *p++ = ELF_VER_CHR;
5949 		  memcpy (p, verstr, verlen + 1);
5950 		  newh = elf_link_hash_lookup (elf_hash_table (info),
5951 					       newname, FALSE, FALSE,
5952 					       FALSE);
5953 		}
5954 	      free (newname);
5955 
5956 	      /* Mark this version if there is a definition and it is
5957 		 not defined in a shared object.  */
5958 	      if (newh != NULL
5959 		  && !newh->def_dynamic
5960 		  && (newh->root.type == bfd_link_hash_defined
5961 		      || newh->root.type == bfd_link_hash_defweak))
5962 		d->symver = 1;
5963 	    }
5964 
5965       /* Attach all the symbols to their version information.  */
5966       asvinfo.info = info;
5967       asvinfo.failed = FALSE;
5968 
5969       elf_link_hash_traverse (elf_hash_table (info),
5970 			      _bfd_elf_link_assign_sym_version,
5971 			      &asvinfo);
5972       if (asvinfo.failed)
5973 	return FALSE;
5974 
5975       if (!info->allow_undefined_version)
5976 	{
5977 	  /* Check if all global versions have a definition.  */
5978 	  all_defined = TRUE;
5979 	  for (t = info->version_info; t != NULL; t = t->next)
5980 	    for (d = t->globals.list; d != NULL; d = d->next)
5981 	      if (d->literal && !d->symver && !d->script)
5982 		{
5983 		  (*_bfd_error_handler)
5984 		    (_("%s: undefined version: %s"),
5985 		     d->pattern, t->name);
5986 		  all_defined = FALSE;
5987 		}
5988 
5989 	  if (!all_defined)
5990 	    {
5991 	      bfd_set_error (bfd_error_bad_value);
5992 	      return FALSE;
5993 	    }
5994 	}
5995 
5996       /* Find all symbols which were defined in a dynamic object and make
5997 	 the backend pick a reasonable value for them.  */
5998       elf_link_hash_traverse (elf_hash_table (info),
5999 			      _bfd_elf_adjust_dynamic_symbol,
6000 			      &eif);
6001       if (eif.failed)
6002 	return FALSE;
6003 
6004       /* Add some entries to the .dynamic section.  We fill in some of the
6005 	 values later, in bfd_elf_final_link, but we must add the entries
6006 	 now so that we know the final size of the .dynamic section.  */
6007 
6008       /* If there are initialization and/or finalization functions to
6009 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
6010       h = (info->init_function
6011 	   ? elf_link_hash_lookup (elf_hash_table (info),
6012 				   info->init_function, FALSE,
6013 				   FALSE, FALSE)
6014 	   : NULL);
6015       if (h != NULL
6016 	  && (h->ref_regular
6017 	      || h->def_regular))
6018 	{
6019 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6020 	    return FALSE;
6021 	}
6022       h = (info->fini_function
6023 	   ? elf_link_hash_lookup (elf_hash_table (info),
6024 				   info->fini_function, FALSE,
6025 				   FALSE, FALSE)
6026 	   : NULL);
6027       if (h != NULL
6028 	  && (h->ref_regular
6029 	      || h->def_regular))
6030 	{
6031 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6032 	    return FALSE;
6033 	}
6034 
6035       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6036       if (s != NULL && s->linker_has_input)
6037 	{
6038 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
6039 	  if (! bfd_link_executable (info))
6040 	    {
6041 	      bfd *sub;
6042 	      asection *o;
6043 
6044 	      for (sub = info->input_bfds; sub != NULL;
6045 		   sub = sub->link.next)
6046 		if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6047 		  for (o = sub->sections; o != NULL; o = o->next)
6048 		    if (elf_section_data (o)->this_hdr.sh_type
6049 			== SHT_PREINIT_ARRAY)
6050 		      {
6051 			(*_bfd_error_handler)
6052 			  (_("%B: .preinit_array section is not allowed in DSO"),
6053 			   sub);
6054 			break;
6055 		      }
6056 
6057 	      bfd_set_error (bfd_error_nonrepresentable_section);
6058 	      return FALSE;
6059 	    }
6060 
6061 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6062 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6063 	    return FALSE;
6064 	}
6065       s = bfd_get_section_by_name (output_bfd, ".init_array");
6066       if (s != NULL && s->linker_has_input)
6067 	{
6068 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6069 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6070 	    return FALSE;
6071 	}
6072       s = bfd_get_section_by_name (output_bfd, ".fini_array");
6073       if (s != NULL && s->linker_has_input)
6074 	{
6075 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6076 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6077 	    return FALSE;
6078 	}
6079 
6080       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6081       /* If .dynstr is excluded from the link, we don't want any of
6082 	 these tags.  Strictly, we should be checking each section
6083 	 individually;  This quick check covers for the case where
6084 	 someone does a /DISCARD/ : { *(*) }.  */
6085       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6086 	{
6087 	  bfd_size_type strsize;
6088 
6089 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6090 	  if ((info->emit_hash
6091 	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6092 	      || (info->emit_gnu_hash
6093 		  && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6094 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6095 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6096 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6097 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6098 					      bed->s->sizeof_sym))
6099 	    return FALSE;
6100 	}
6101     }
6102 
6103   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6104     return FALSE;
6105 
6106   /* The backend must work out the sizes of all the other dynamic
6107      sections.  */
6108   if (dynobj != NULL
6109       && bed->elf_backend_size_dynamic_sections != NULL
6110       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6111     return FALSE;
6112 
6113   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6114     {
6115       unsigned long section_sym_count;
6116       struct bfd_elf_version_tree *verdefs;
6117       asection *s;
6118 
6119       /* Set up the version definition section.  */
6120       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6121       BFD_ASSERT (s != NULL);
6122 
6123       /* We may have created additional version definitions if we are
6124 	 just linking a regular application.  */
6125       verdefs = info->version_info;
6126 
6127       /* Skip anonymous version tag.  */
6128       if (verdefs != NULL && verdefs->vernum == 0)
6129 	verdefs = verdefs->next;
6130 
6131       if (verdefs == NULL && !info->create_default_symver)
6132 	s->flags |= SEC_EXCLUDE;
6133       else
6134 	{
6135 	  unsigned int cdefs;
6136 	  bfd_size_type size;
6137 	  struct bfd_elf_version_tree *t;
6138 	  bfd_byte *p;
6139 	  Elf_Internal_Verdef def;
6140 	  Elf_Internal_Verdaux defaux;
6141 	  struct bfd_link_hash_entry *bh;
6142 	  struct elf_link_hash_entry *h;
6143 	  const char *name;
6144 
6145 	  cdefs = 0;
6146 	  size = 0;
6147 
6148 	  /* Make space for the base version.  */
6149 	  size += sizeof (Elf_External_Verdef);
6150 	  size += sizeof (Elf_External_Verdaux);
6151 	  ++cdefs;
6152 
6153 	  /* Make space for the default version.  */
6154 	  if (info->create_default_symver)
6155 	    {
6156 	      size += sizeof (Elf_External_Verdef);
6157 	      ++cdefs;
6158 	    }
6159 
6160 	  for (t = verdefs; t != NULL; t = t->next)
6161 	    {
6162 	      struct bfd_elf_version_deps *n;
6163 
6164 	      /* Don't emit base version twice.  */
6165 	      if (t->vernum == 0)
6166 		continue;
6167 
6168 	      size += sizeof (Elf_External_Verdef);
6169 	      size += sizeof (Elf_External_Verdaux);
6170 	      ++cdefs;
6171 
6172 	      for (n = t->deps; n != NULL; n = n->next)
6173 		size += sizeof (Elf_External_Verdaux);
6174 	    }
6175 
6176 	  s->size = size;
6177 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6178 	  if (s->contents == NULL && s->size != 0)
6179 	    return FALSE;
6180 
6181 	  /* Fill in the version definition section.  */
6182 
6183 	  p = s->contents;
6184 
6185 	  def.vd_version = VER_DEF_CURRENT;
6186 	  def.vd_flags = VER_FLG_BASE;
6187 	  def.vd_ndx = 1;
6188 	  def.vd_cnt = 1;
6189 	  if (info->create_default_symver)
6190 	    {
6191 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6192 	      def.vd_next = sizeof (Elf_External_Verdef);
6193 	    }
6194 	  else
6195 	    {
6196 	      def.vd_aux = sizeof (Elf_External_Verdef);
6197 	      def.vd_next = (sizeof (Elf_External_Verdef)
6198 			     + sizeof (Elf_External_Verdaux));
6199 	    }
6200 
6201 	  if (soname_indx != (bfd_size_type) -1)
6202 	    {
6203 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6204 				      soname_indx);
6205 	      def.vd_hash = bfd_elf_hash (soname);
6206 	      defaux.vda_name = soname_indx;
6207 	      name = soname;
6208 	    }
6209 	  else
6210 	    {
6211 	      bfd_size_type indx;
6212 
6213 	      name = lbasename (output_bfd->filename);
6214 	      def.vd_hash = bfd_elf_hash (name);
6215 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6216 					  name, FALSE);
6217 	      if (indx == (bfd_size_type) -1)
6218 		return FALSE;
6219 	      defaux.vda_name = indx;
6220 	    }
6221 	  defaux.vda_next = 0;
6222 
6223 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
6224 				    (Elf_External_Verdef *) p);
6225 	  p += sizeof (Elf_External_Verdef);
6226 	  if (info->create_default_symver)
6227 	    {
6228 	      /* Add a symbol representing this version.  */
6229 	      bh = NULL;
6230 	      if (! (_bfd_generic_link_add_one_symbol
6231 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6232 		      0, NULL, FALSE,
6233 		      get_elf_backend_data (dynobj)->collect, &bh)))
6234 		return FALSE;
6235 	      h = (struct elf_link_hash_entry *) bh;
6236 	      h->non_elf = 0;
6237 	      h->def_regular = 1;
6238 	      h->type = STT_OBJECT;
6239 	      h->verinfo.vertree = NULL;
6240 
6241 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6242 		return FALSE;
6243 
6244 	      /* Create a duplicate of the base version with the same
6245 		 aux block, but different flags.  */
6246 	      def.vd_flags = 0;
6247 	      def.vd_ndx = 2;
6248 	      def.vd_aux = sizeof (Elf_External_Verdef);
6249 	      if (verdefs)
6250 		def.vd_next = (sizeof (Elf_External_Verdef)
6251 			       + sizeof (Elf_External_Verdaux));
6252 	      else
6253 		def.vd_next = 0;
6254 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
6255 					(Elf_External_Verdef *) p);
6256 	      p += sizeof (Elf_External_Verdef);
6257 	    }
6258 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6259 				     (Elf_External_Verdaux *) p);
6260 	  p += sizeof (Elf_External_Verdaux);
6261 
6262 	  for (t = verdefs; t != NULL; t = t->next)
6263 	    {
6264 	      unsigned int cdeps;
6265 	      struct bfd_elf_version_deps *n;
6266 
6267 	      /* Don't emit the base version twice.  */
6268 	      if (t->vernum == 0)
6269 		continue;
6270 
6271 	      cdeps = 0;
6272 	      for (n = t->deps; n != NULL; n = n->next)
6273 		++cdeps;
6274 
6275 	      /* Add a symbol representing this version.  */
6276 	      bh = NULL;
6277 	      if (! (_bfd_generic_link_add_one_symbol
6278 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6279 		      0, NULL, FALSE,
6280 		      get_elf_backend_data (dynobj)->collect, &bh)))
6281 		return FALSE;
6282 	      h = (struct elf_link_hash_entry *) bh;
6283 	      h->non_elf = 0;
6284 	      h->def_regular = 1;
6285 	      h->type = STT_OBJECT;
6286 	      h->verinfo.vertree = t;
6287 
6288 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6289 		return FALSE;
6290 
6291 	      def.vd_version = VER_DEF_CURRENT;
6292 	      def.vd_flags = 0;
6293 	      if (t->globals.list == NULL
6294 		  && t->locals.list == NULL
6295 		  && ! t->used)
6296 		def.vd_flags |= VER_FLG_WEAK;
6297 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6298 	      def.vd_cnt = cdeps + 1;
6299 	      def.vd_hash = bfd_elf_hash (t->name);
6300 	      def.vd_aux = sizeof (Elf_External_Verdef);
6301 	      def.vd_next = 0;
6302 
6303 	      /* If a basever node is next, it *must* be the last node in
6304 		 the chain, otherwise Verdef construction breaks.  */
6305 	      if (t->next != NULL && t->next->vernum == 0)
6306 		BFD_ASSERT (t->next->next == NULL);
6307 
6308 	      if (t->next != NULL && t->next->vernum != 0)
6309 		def.vd_next = (sizeof (Elf_External_Verdef)
6310 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6311 
6312 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
6313 					(Elf_External_Verdef *) p);
6314 	      p += sizeof (Elf_External_Verdef);
6315 
6316 	      defaux.vda_name = h->dynstr_index;
6317 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6318 				      h->dynstr_index);
6319 	      defaux.vda_next = 0;
6320 	      if (t->deps != NULL)
6321 		defaux.vda_next = sizeof (Elf_External_Verdaux);
6322 	      t->name_indx = defaux.vda_name;
6323 
6324 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6325 					 (Elf_External_Verdaux *) p);
6326 	      p += sizeof (Elf_External_Verdaux);
6327 
6328 	      for (n = t->deps; n != NULL; n = n->next)
6329 		{
6330 		  if (n->version_needed == NULL)
6331 		    {
6332 		      /* This can happen if there was an error in the
6333 			 version script.  */
6334 		      defaux.vda_name = 0;
6335 		    }
6336 		  else
6337 		    {
6338 		      defaux.vda_name = n->version_needed->name_indx;
6339 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6340 					      defaux.vda_name);
6341 		    }
6342 		  if (n->next == NULL)
6343 		    defaux.vda_next = 0;
6344 		  else
6345 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
6346 
6347 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6348 					     (Elf_External_Verdaux *) p);
6349 		  p += sizeof (Elf_External_Verdaux);
6350 		}
6351 	    }
6352 
6353 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6354 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6355 	    return FALSE;
6356 
6357 	  elf_tdata (output_bfd)->cverdefs = cdefs;
6358 	}
6359 
6360       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6361 	{
6362 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6363 	    return FALSE;
6364 	}
6365       else if (info->flags & DF_BIND_NOW)
6366 	{
6367 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6368 	    return FALSE;
6369 	}
6370 
6371       if (info->flags_1)
6372 	{
6373 	  if (bfd_link_executable (info))
6374 	    info->flags_1 &= ~ (DF_1_INITFIRST
6375 				| DF_1_NODELETE
6376 				| DF_1_NOOPEN);
6377 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6378 	    return FALSE;
6379 	}
6380 
6381       /* Work out the size of the version reference section.  */
6382 
6383       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6384       BFD_ASSERT (s != NULL);
6385       {
6386 	struct elf_find_verdep_info sinfo;
6387 
6388 	sinfo.info = info;
6389 	sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6390 	if (sinfo.vers == 0)
6391 	  sinfo.vers = 1;
6392 	sinfo.failed = FALSE;
6393 
6394 	elf_link_hash_traverse (elf_hash_table (info),
6395 				_bfd_elf_link_find_version_dependencies,
6396 				&sinfo);
6397 	if (sinfo.failed)
6398 	  return FALSE;
6399 
6400 	if (elf_tdata (output_bfd)->verref == NULL)
6401 	  s->flags |= SEC_EXCLUDE;
6402 	else
6403 	  {
6404 	    Elf_Internal_Verneed *t;
6405 	    unsigned int size;
6406 	    unsigned int crefs;
6407 	    bfd_byte *p;
6408 
6409 	    /* Build the version dependency section.  */
6410 	    size = 0;
6411 	    crefs = 0;
6412 	    for (t = elf_tdata (output_bfd)->verref;
6413 		 t != NULL;
6414 		 t = t->vn_nextref)
6415 	      {
6416 		Elf_Internal_Vernaux *a;
6417 
6418 		size += sizeof (Elf_External_Verneed);
6419 		++crefs;
6420 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6421 		  size += sizeof (Elf_External_Vernaux);
6422 	      }
6423 
6424 	    s->size = size;
6425 	    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6426 	    if (s->contents == NULL)
6427 	      return FALSE;
6428 
6429 	    p = s->contents;
6430 	    for (t = elf_tdata (output_bfd)->verref;
6431 		 t != NULL;
6432 		 t = t->vn_nextref)
6433 	      {
6434 		unsigned int caux;
6435 		Elf_Internal_Vernaux *a;
6436 		bfd_size_type indx;
6437 
6438 		caux = 0;
6439 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6440 		  ++caux;
6441 
6442 		t->vn_version = VER_NEED_CURRENT;
6443 		t->vn_cnt = caux;
6444 		indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6445 					    elf_dt_name (t->vn_bfd) != NULL
6446 					    ? elf_dt_name (t->vn_bfd)
6447 					    : lbasename (t->vn_bfd->filename),
6448 					    FALSE);
6449 		if (indx == (bfd_size_type) -1)
6450 		  return FALSE;
6451 		t->vn_file = indx;
6452 		t->vn_aux = sizeof (Elf_External_Verneed);
6453 		if (t->vn_nextref == NULL)
6454 		  t->vn_next = 0;
6455 		else
6456 		  t->vn_next = (sizeof (Elf_External_Verneed)
6457 				+ caux * sizeof (Elf_External_Vernaux));
6458 
6459 		_bfd_elf_swap_verneed_out (output_bfd, t,
6460 					   (Elf_External_Verneed *) p);
6461 		p += sizeof (Elf_External_Verneed);
6462 
6463 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6464 		  {
6465 		    a->vna_hash = bfd_elf_hash (a->vna_nodename);
6466 		    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6467 						a->vna_nodename, FALSE);
6468 		    if (indx == (bfd_size_type) -1)
6469 		      return FALSE;
6470 		    a->vna_name = indx;
6471 		    if (a->vna_nextptr == NULL)
6472 		      a->vna_next = 0;
6473 		    else
6474 		      a->vna_next = sizeof (Elf_External_Vernaux);
6475 
6476 		    _bfd_elf_swap_vernaux_out (output_bfd, a,
6477 					       (Elf_External_Vernaux *) p);
6478 		    p += sizeof (Elf_External_Vernaux);
6479 		  }
6480 	      }
6481 
6482 	    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6483 		|| !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6484 	      return FALSE;
6485 
6486 	    elf_tdata (output_bfd)->cverrefs = crefs;
6487 	  }
6488       }
6489 
6490       if ((elf_tdata (output_bfd)->cverrefs == 0
6491 	   && elf_tdata (output_bfd)->cverdefs == 0)
6492 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6493 					     &section_sym_count) == 0)
6494 	{
6495 	  s = bfd_get_linker_section (dynobj, ".gnu.version");
6496 	  s->flags |= SEC_EXCLUDE;
6497 	}
6498     }
6499   return TRUE;
6500 }
6501 
6502 /* Find the first non-excluded output section.  We'll use its
6503    section symbol for some emitted relocs.  */
6504 void
6505 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6506 {
6507   asection *s;
6508 
6509   for (s = output_bfd->sections; s != NULL; s = s->next)
6510     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6511 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6512       {
6513 	elf_hash_table (info)->text_index_section = s;
6514 	break;
6515       }
6516 }
6517 
6518 /* Find two non-excluded output sections, one for code, one for data.
6519    We'll use their section symbols for some emitted relocs.  */
6520 void
6521 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6522 {
6523   asection *s;
6524 
6525   /* Data first, since setting text_index_section changes
6526      _bfd_elf_link_omit_section_dynsym.  */
6527   for (s = output_bfd->sections; s != NULL; s = s->next)
6528     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6529 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6530       {
6531 	elf_hash_table (info)->data_index_section = s;
6532 	break;
6533       }
6534 
6535   for (s = output_bfd->sections; s != NULL; s = s->next)
6536     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6537 	 == (SEC_ALLOC | SEC_READONLY))
6538 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6539       {
6540 	elf_hash_table (info)->text_index_section = s;
6541 	break;
6542       }
6543 
6544   if (elf_hash_table (info)->text_index_section == NULL)
6545     elf_hash_table (info)->text_index_section
6546       = elf_hash_table (info)->data_index_section;
6547 }
6548 
6549 bfd_boolean
6550 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6551 {
6552   const struct elf_backend_data *bed;
6553 
6554   if (!is_elf_hash_table (info->hash))
6555     return TRUE;
6556 
6557   bed = get_elf_backend_data (output_bfd);
6558   (*bed->elf_backend_init_index_section) (output_bfd, info);
6559 
6560   if (elf_hash_table (info)->dynamic_sections_created)
6561     {
6562       bfd *dynobj;
6563       asection *s;
6564       bfd_size_type dynsymcount;
6565       unsigned long section_sym_count;
6566       unsigned int dtagcount;
6567 
6568       dynobj = elf_hash_table (info)->dynobj;
6569 
6570       /* Assign dynsym indicies.  In a shared library we generate a
6571 	 section symbol for each output section, which come first.
6572 	 Next come all of the back-end allocated local dynamic syms,
6573 	 followed by the rest of the global symbols.  */
6574 
6575       dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6576 						    &section_sym_count);
6577 
6578       /* Work out the size of the symbol version section.  */
6579       s = bfd_get_linker_section (dynobj, ".gnu.version");
6580       BFD_ASSERT (s != NULL);
6581       if (dynsymcount != 0
6582 	  && (s->flags & SEC_EXCLUDE) == 0)
6583 	{
6584 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
6585 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6586 	  if (s->contents == NULL)
6587 	    return FALSE;
6588 
6589 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6590 	    return FALSE;
6591 	}
6592 
6593       /* Set the size of the .dynsym and .hash sections.  We counted
6594 	 the number of dynamic symbols in elf_link_add_object_symbols.
6595 	 We will build the contents of .dynsym and .hash when we build
6596 	 the final symbol table, because until then we do not know the
6597 	 correct value to give the symbols.  We built the .dynstr
6598 	 section as we went along in elf_link_add_object_symbols.  */
6599       s = elf_hash_table (info)->dynsym;
6600       BFD_ASSERT (s != NULL);
6601       s->size = dynsymcount * bed->s->sizeof_sym;
6602 
6603       if (dynsymcount != 0)
6604 	{
6605 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6606 	  if (s->contents == NULL)
6607 	    return FALSE;
6608 
6609 	  /* The first entry in .dynsym is a dummy symbol.
6610 	     Clear all the section syms, in case we don't output them all.  */
6611 	  ++section_sym_count;
6612 	  memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6613 	}
6614 
6615       elf_hash_table (info)->bucketcount = 0;
6616 
6617       /* Compute the size of the hashing table.  As a side effect this
6618 	 computes the hash values for all the names we export.  */
6619       if (info->emit_hash)
6620 	{
6621 	  unsigned long int *hashcodes;
6622 	  struct hash_codes_info hashinf;
6623 	  bfd_size_type amt;
6624 	  unsigned long int nsyms;
6625 	  size_t bucketcount;
6626 	  size_t hash_entry_size;
6627 
6628 	  /* Compute the hash values for all exported symbols.  At the same
6629 	     time store the values in an array so that we could use them for
6630 	     optimizations.  */
6631 	  amt = dynsymcount * sizeof (unsigned long int);
6632 	  hashcodes = (unsigned long int *) bfd_malloc (amt);
6633 	  if (hashcodes == NULL)
6634 	    return FALSE;
6635 	  hashinf.hashcodes = hashcodes;
6636 	  hashinf.error = FALSE;
6637 
6638 	  /* Put all hash values in HASHCODES.  */
6639 	  elf_link_hash_traverse (elf_hash_table (info),
6640 				  elf_collect_hash_codes, &hashinf);
6641 	  if (hashinf.error)
6642 	    {
6643 	      free (hashcodes);
6644 	      return FALSE;
6645 	    }
6646 
6647 	  nsyms = hashinf.hashcodes - hashcodes;
6648 	  bucketcount
6649 	    = compute_bucket_count (info, hashcodes, nsyms, 0);
6650 	  free (hashcodes);
6651 
6652 	  if (bucketcount == 0)
6653 	    return FALSE;
6654 
6655 	  elf_hash_table (info)->bucketcount = bucketcount;
6656 
6657 	  s = bfd_get_linker_section (dynobj, ".hash");
6658 	  BFD_ASSERT (s != NULL);
6659 	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6660 	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6661 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6662 	  if (s->contents == NULL)
6663 	    return FALSE;
6664 
6665 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6666 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6667 		   s->contents + hash_entry_size);
6668 	}
6669 
6670       if (info->emit_gnu_hash)
6671 	{
6672 	  size_t i, cnt;
6673 	  unsigned char *contents;
6674 	  struct collect_gnu_hash_codes cinfo;
6675 	  bfd_size_type amt;
6676 	  size_t bucketcount;
6677 
6678 	  memset (&cinfo, 0, sizeof (cinfo));
6679 
6680 	  /* Compute the hash values for all exported symbols.  At the same
6681 	     time store the values in an array so that we could use them for
6682 	     optimizations.  */
6683 	  amt = dynsymcount * 2 * sizeof (unsigned long int);
6684 	  cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6685 	  if (cinfo.hashcodes == NULL)
6686 	    return FALSE;
6687 
6688 	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
6689 	  cinfo.min_dynindx = -1;
6690 	  cinfo.output_bfd = output_bfd;
6691 	  cinfo.bed = bed;
6692 
6693 	  /* Put all hash values in HASHCODES.  */
6694 	  elf_link_hash_traverse (elf_hash_table (info),
6695 				  elf_collect_gnu_hash_codes, &cinfo);
6696 	  if (cinfo.error)
6697 	    {
6698 	      free (cinfo.hashcodes);
6699 	      return FALSE;
6700 	    }
6701 
6702 	  bucketcount
6703 	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6704 
6705 	  if (bucketcount == 0)
6706 	    {
6707 	      free (cinfo.hashcodes);
6708 	      return FALSE;
6709 	    }
6710 
6711 	  s = bfd_get_linker_section (dynobj, ".gnu.hash");
6712 	  BFD_ASSERT (s != NULL);
6713 
6714 	  if (cinfo.nsyms == 0)
6715 	    {
6716 	      /* Empty .gnu.hash section is special.  */
6717 	      BFD_ASSERT (cinfo.min_dynindx == -1);
6718 	      free (cinfo.hashcodes);
6719 	      s->size = 5 * 4 + bed->s->arch_size / 8;
6720 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6721 	      if (contents == NULL)
6722 		return FALSE;
6723 	      s->contents = contents;
6724 	      /* 1 empty bucket.  */
6725 	      bfd_put_32 (output_bfd, 1, contents);
6726 	      /* SYMIDX above the special symbol 0.  */
6727 	      bfd_put_32 (output_bfd, 1, contents + 4);
6728 	      /* Just one word for bitmask.  */
6729 	      bfd_put_32 (output_bfd, 1, contents + 8);
6730 	      /* Only hash fn bloom filter.  */
6731 	      bfd_put_32 (output_bfd, 0, contents + 12);
6732 	      /* No hashes are valid - empty bitmask.  */
6733 	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6734 	      /* No hashes in the only bucket.  */
6735 	      bfd_put_32 (output_bfd, 0,
6736 			  contents + 16 + bed->s->arch_size / 8);
6737 	    }
6738 	  else
6739 	    {
6740 	      unsigned long int maskwords, maskbitslog2, x;
6741 	      BFD_ASSERT (cinfo.min_dynindx != -1);
6742 
6743 	      x = cinfo.nsyms;
6744 	      maskbitslog2 = 1;
6745 	      while ((x >>= 1) != 0)
6746 		++maskbitslog2;
6747 	      if (maskbitslog2 < 3)
6748 		maskbitslog2 = 5;
6749 	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6750 		maskbitslog2 = maskbitslog2 + 3;
6751 	      else
6752 		maskbitslog2 = maskbitslog2 + 2;
6753 	      if (bed->s->arch_size == 64)
6754 		{
6755 		  if (maskbitslog2 == 5)
6756 		    maskbitslog2 = 6;
6757 		  cinfo.shift1 = 6;
6758 		}
6759 	      else
6760 		cinfo.shift1 = 5;
6761 	      cinfo.mask = (1 << cinfo.shift1) - 1;
6762 	      cinfo.shift2 = maskbitslog2;
6763 	      cinfo.maskbits = 1 << maskbitslog2;
6764 	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6765 	      amt = bucketcount * sizeof (unsigned long int) * 2;
6766 	      amt += maskwords * sizeof (bfd_vma);
6767 	      cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6768 	      if (cinfo.bitmask == NULL)
6769 		{
6770 		  free (cinfo.hashcodes);
6771 		  return FALSE;
6772 		}
6773 
6774 	      cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6775 	      cinfo.indx = cinfo.counts + bucketcount;
6776 	      cinfo.symindx = dynsymcount - cinfo.nsyms;
6777 	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6778 
6779 	      /* Determine how often each hash bucket is used.  */
6780 	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6781 	      for (i = 0; i < cinfo.nsyms; ++i)
6782 		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6783 
6784 	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6785 		if (cinfo.counts[i] != 0)
6786 		  {
6787 		    cinfo.indx[i] = cnt;
6788 		    cnt += cinfo.counts[i];
6789 		  }
6790 	      BFD_ASSERT (cnt == dynsymcount);
6791 	      cinfo.bucketcount = bucketcount;
6792 	      cinfo.local_indx = cinfo.min_dynindx;
6793 
6794 	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6795 	      s->size += cinfo.maskbits / 8;
6796 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6797 	      if (contents == NULL)
6798 		{
6799 		  free (cinfo.bitmask);
6800 		  free (cinfo.hashcodes);
6801 		  return FALSE;
6802 		}
6803 
6804 	      s->contents = contents;
6805 	      bfd_put_32 (output_bfd, bucketcount, contents);
6806 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6807 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
6808 	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6809 	      contents += 16 + cinfo.maskbits / 8;
6810 
6811 	      for (i = 0; i < bucketcount; ++i)
6812 		{
6813 		  if (cinfo.counts[i] == 0)
6814 		    bfd_put_32 (output_bfd, 0, contents);
6815 		  else
6816 		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6817 		  contents += 4;
6818 		}
6819 
6820 	      cinfo.contents = contents;
6821 
6822 	      /* Renumber dynamic symbols, populate .gnu.hash section.  */
6823 	      elf_link_hash_traverse (elf_hash_table (info),
6824 				      elf_renumber_gnu_hash_syms, &cinfo);
6825 
6826 	      contents = s->contents + 16;
6827 	      for (i = 0; i < maskwords; ++i)
6828 		{
6829 		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6830 			   contents);
6831 		  contents += bed->s->arch_size / 8;
6832 		}
6833 
6834 	      free (cinfo.bitmask);
6835 	      free (cinfo.hashcodes);
6836 	    }
6837 	}
6838 
6839       s = bfd_get_linker_section (dynobj, ".dynstr");
6840       BFD_ASSERT (s != NULL);
6841 
6842       elf_finalize_dynstr (output_bfd, info);
6843 
6844       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6845 
6846       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6847 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6848 	  return FALSE;
6849     }
6850 
6851   return TRUE;
6852 }
6853 
6854 /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
6855 
6856 static void
6857 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6858 			    asection *sec)
6859 {
6860   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6861   sec->sec_info_type = SEC_INFO_TYPE_NONE;
6862 }
6863 
6864 /* Finish SHF_MERGE section merging.  */
6865 
6866 bfd_boolean
6867 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6868 {
6869   bfd *ibfd;
6870   asection *sec;
6871 
6872   if (!is_elf_hash_table (info->hash))
6873     return FALSE;
6874 
6875   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6876     if ((ibfd->flags & DYNAMIC) == 0
6877 	&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6878 	&& (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6879 	    == get_elf_backend_data (obfd)->s->elfclass))
6880       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6881 	if ((sec->flags & SEC_MERGE) != 0
6882 	    && !bfd_is_abs_section (sec->output_section))
6883 	  {
6884 	    struct bfd_elf_section_data *secdata;
6885 
6886 	    secdata = elf_section_data (sec);
6887 	    if (! _bfd_add_merge_section (obfd,
6888 					  &elf_hash_table (info)->merge_info,
6889 					  sec, &secdata->sec_info))
6890 	      return FALSE;
6891 	    else if (secdata->sec_info)
6892 	      sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6893 	  }
6894 
6895   if (elf_hash_table (info)->merge_info != NULL)
6896     _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6897 			 merge_sections_remove_hook);
6898   return TRUE;
6899 }
6900 
6901 /* Create an entry in an ELF linker hash table.  */
6902 
6903 struct bfd_hash_entry *
6904 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6905 			    struct bfd_hash_table *table,
6906 			    const char *string)
6907 {
6908   /* Allocate the structure if it has not already been allocated by a
6909      subclass.  */
6910   if (entry == NULL)
6911     {
6912       entry = (struct bfd_hash_entry *)
6913 	bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6914       if (entry == NULL)
6915 	return entry;
6916     }
6917 
6918   /* Call the allocation method of the superclass.  */
6919   entry = _bfd_link_hash_newfunc (entry, table, string);
6920   if (entry != NULL)
6921     {
6922       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6923       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6924 
6925       /* Set local fields.  */
6926       ret->indx = -1;
6927       ret->dynindx = -1;
6928       ret->got = htab->init_got_refcount;
6929       ret->plt = htab->init_plt_refcount;
6930       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6931 			      - offsetof (struct elf_link_hash_entry, size)));
6932       /* Assume that we have been called by a non-ELF symbol reader.
6933 	 This flag is then reset by the code which reads an ELF input
6934 	 file.  This ensures that a symbol created by a non-ELF symbol
6935 	 reader will have the flag set correctly.  */
6936       ret->non_elf = 1;
6937     }
6938 
6939   return entry;
6940 }
6941 
6942 /* Copy data from an indirect symbol to its direct symbol, hiding the
6943    old indirect symbol.  Also used for copying flags to a weakdef.  */
6944 
6945 void
6946 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6947 				  struct elf_link_hash_entry *dir,
6948 				  struct elf_link_hash_entry *ind)
6949 {
6950   struct elf_link_hash_table *htab;
6951 
6952   /* Copy down any references that we may have already seen to the
6953      symbol which just became indirect if DIR isn't a hidden versioned
6954      symbol.  */
6955 
6956   if (dir->versioned != versioned_hidden)
6957     {
6958       dir->ref_dynamic |= ind->ref_dynamic;
6959       dir->ref_regular |= ind->ref_regular;
6960       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6961       dir->non_got_ref |= ind->non_got_ref;
6962       dir->needs_plt |= ind->needs_plt;
6963       dir->pointer_equality_needed |= ind->pointer_equality_needed;
6964     }
6965 
6966   if (ind->root.type != bfd_link_hash_indirect)
6967     return;
6968 
6969   /* Copy over the global and procedure linkage table refcount entries.
6970      These may have been already set up by a check_relocs routine.  */
6971   htab = elf_hash_table (info);
6972   if (ind->got.refcount > htab->init_got_refcount.refcount)
6973     {
6974       if (dir->got.refcount < 0)
6975 	dir->got.refcount = 0;
6976       dir->got.refcount += ind->got.refcount;
6977       ind->got.refcount = htab->init_got_refcount.refcount;
6978     }
6979 
6980   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6981     {
6982       if (dir->plt.refcount < 0)
6983 	dir->plt.refcount = 0;
6984       dir->plt.refcount += ind->plt.refcount;
6985       ind->plt.refcount = htab->init_plt_refcount.refcount;
6986     }
6987 
6988   if (ind->dynindx != -1)
6989     {
6990       if (dir->dynindx != -1)
6991 	_bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6992       dir->dynindx = ind->dynindx;
6993       dir->dynstr_index = ind->dynstr_index;
6994       ind->dynindx = -1;
6995       ind->dynstr_index = 0;
6996     }
6997 }
6998 
6999 void
7000 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7001 				struct elf_link_hash_entry *h,
7002 				bfd_boolean force_local)
7003 {
7004   /* STT_GNU_IFUNC symbol must go through PLT.  */
7005   if (h->type != STT_GNU_IFUNC)
7006     {
7007       h->plt = elf_hash_table (info)->init_plt_offset;
7008       h->needs_plt = 0;
7009     }
7010   if (force_local)
7011     {
7012       h->forced_local = 1;
7013       if (h->dynindx != -1)
7014 	{
7015 	  h->dynindx = -1;
7016 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7017 				  h->dynstr_index);
7018 	}
7019     }
7020 }
7021 
7022 /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
7023    caller.  */
7024 
7025 bfd_boolean
7026 _bfd_elf_link_hash_table_init
7027   (struct elf_link_hash_table *table,
7028    bfd *abfd,
7029    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7030 				      struct bfd_hash_table *,
7031 				      const char *),
7032    unsigned int entsize,
7033    enum elf_target_id target_id)
7034 {
7035   bfd_boolean ret;
7036   int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7037 
7038   table->init_got_refcount.refcount = can_refcount - 1;
7039   table->init_plt_refcount.refcount = can_refcount - 1;
7040   table->init_got_offset.offset = -(bfd_vma) 1;
7041   table->init_plt_offset.offset = -(bfd_vma) 1;
7042   /* The first dynamic symbol is a dummy.  */
7043   table->dynsymcount = 1;
7044 
7045   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7046 
7047   table->root.type = bfd_link_elf_hash_table;
7048   table->hash_table_id = target_id;
7049 
7050   return ret;
7051 }
7052 
7053 /* Create an ELF linker hash table.  */
7054 
7055 struct bfd_link_hash_table *
7056 _bfd_elf_link_hash_table_create (bfd *abfd)
7057 {
7058   struct elf_link_hash_table *ret;
7059   bfd_size_type amt = sizeof (struct elf_link_hash_table);
7060 
7061   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7062   if (ret == NULL)
7063     return NULL;
7064 
7065   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7066 				       sizeof (struct elf_link_hash_entry),
7067 				       GENERIC_ELF_DATA))
7068     {
7069       free (ret);
7070       return NULL;
7071     }
7072   ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7073 
7074   return &ret->root;
7075 }
7076 
7077 /* Destroy an ELF linker hash table.  */
7078 
7079 void
7080 _bfd_elf_link_hash_table_free (bfd *obfd)
7081 {
7082   struct elf_link_hash_table *htab;
7083 
7084   htab = (struct elf_link_hash_table *) obfd->link.hash;
7085   if (htab->dynstr != NULL)
7086     _bfd_elf_strtab_free (htab->dynstr);
7087   _bfd_merge_sections_free (htab->merge_info);
7088   _bfd_generic_link_hash_table_free (obfd);
7089 }
7090 
7091 /* This is a hook for the ELF emulation code in the generic linker to
7092    tell the backend linker what file name to use for the DT_NEEDED
7093    entry for a dynamic object.  */
7094 
7095 void
7096 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7097 {
7098   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7099       && bfd_get_format (abfd) == bfd_object)
7100     elf_dt_name (abfd) = name;
7101 }
7102 
7103 int
7104 bfd_elf_get_dyn_lib_class (bfd *abfd)
7105 {
7106   int lib_class;
7107   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7108       && bfd_get_format (abfd) == bfd_object)
7109     lib_class = elf_dyn_lib_class (abfd);
7110   else
7111     lib_class = 0;
7112   return lib_class;
7113 }
7114 
7115 void
7116 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7117 {
7118   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7119       && bfd_get_format (abfd) == bfd_object)
7120     elf_dyn_lib_class (abfd) = lib_class;
7121 }
7122 
7123 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
7124    the linker ELF emulation code.  */
7125 
7126 struct bfd_link_needed_list *
7127 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7128 			 struct bfd_link_info *info)
7129 {
7130   if (! is_elf_hash_table (info->hash))
7131     return NULL;
7132   return elf_hash_table (info)->needed;
7133 }
7134 
7135 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
7136    hook for the linker ELF emulation code.  */
7137 
7138 struct bfd_link_needed_list *
7139 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7140 			  struct bfd_link_info *info)
7141 {
7142   if (! is_elf_hash_table (info->hash))
7143     return NULL;
7144   return elf_hash_table (info)->runpath;
7145 }
7146 
7147 /* Get the name actually used for a dynamic object for a link.  This
7148    is the SONAME entry if there is one.  Otherwise, it is the string
7149    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
7150 
7151 const char *
7152 bfd_elf_get_dt_soname (bfd *abfd)
7153 {
7154   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7155       && bfd_get_format (abfd) == bfd_object)
7156     return elf_dt_name (abfd);
7157   return NULL;
7158 }
7159 
7160 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
7161    the ELF linker emulation code.  */
7162 
7163 bfd_boolean
7164 bfd_elf_get_bfd_needed_list (bfd *abfd,
7165 			     struct bfd_link_needed_list **pneeded)
7166 {
7167   asection *s;
7168   bfd_byte *dynbuf = NULL;
7169   unsigned int elfsec;
7170   unsigned long shlink;
7171   bfd_byte *extdyn, *extdynend;
7172   size_t extdynsize;
7173   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7174 
7175   *pneeded = NULL;
7176 
7177   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7178       || bfd_get_format (abfd) != bfd_object)
7179     return TRUE;
7180 
7181   s = bfd_get_section_by_name (abfd, ".dynamic");
7182   if (s == NULL || s->size == 0)
7183     return TRUE;
7184 
7185   if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7186     goto error_return;
7187 
7188   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7189   if (elfsec == SHN_BAD)
7190     goto error_return;
7191 
7192   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7193 
7194   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7195   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7196 
7197   extdyn = dynbuf;
7198   extdynend = extdyn + s->size;
7199   for (; extdyn < extdynend; extdyn += extdynsize)
7200     {
7201       Elf_Internal_Dyn dyn;
7202 
7203       (*swap_dyn_in) (abfd, extdyn, &dyn);
7204 
7205       if (dyn.d_tag == DT_NULL)
7206 	break;
7207 
7208       if (dyn.d_tag == DT_NEEDED)
7209 	{
7210 	  const char *string;
7211 	  struct bfd_link_needed_list *l;
7212 	  unsigned int tagv = dyn.d_un.d_val;
7213 	  bfd_size_type amt;
7214 
7215 	  string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7216 	  if (string == NULL)
7217 	    goto error_return;
7218 
7219 	  amt = sizeof *l;
7220 	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7221 	  if (l == NULL)
7222 	    goto error_return;
7223 
7224 	  l->by = abfd;
7225 	  l->name = string;
7226 	  l->next = *pneeded;
7227 	  *pneeded = l;
7228 	}
7229     }
7230 
7231   free (dynbuf);
7232 
7233   return TRUE;
7234 
7235  error_return:
7236   if (dynbuf != NULL)
7237     free (dynbuf);
7238   return FALSE;
7239 }
7240 
7241 struct elf_symbuf_symbol
7242 {
7243   unsigned long st_name;	/* Symbol name, index in string tbl */
7244   unsigned char st_info;	/* Type and binding attributes */
7245   unsigned char st_other;	/* Visibilty, and target specific */
7246 };
7247 
7248 struct elf_symbuf_head
7249 {
7250   struct elf_symbuf_symbol *ssym;
7251   bfd_size_type count;
7252   unsigned int st_shndx;
7253 };
7254 
7255 struct elf_symbol
7256 {
7257   union
7258     {
7259       Elf_Internal_Sym *isym;
7260       struct elf_symbuf_symbol *ssym;
7261     } u;
7262   const char *name;
7263 };
7264 
7265 /* Sort references to symbols by ascending section number.  */
7266 
7267 static int
7268 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7269 {
7270   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7271   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7272 
7273   return s1->st_shndx - s2->st_shndx;
7274 }
7275 
7276 static int
7277 elf_sym_name_compare (const void *arg1, const void *arg2)
7278 {
7279   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7280   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7281   return strcmp (s1->name, s2->name);
7282 }
7283 
7284 static struct elf_symbuf_head *
7285 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7286 {
7287   Elf_Internal_Sym **ind, **indbufend, **indbuf;
7288   struct elf_symbuf_symbol *ssym;
7289   struct elf_symbuf_head *ssymbuf, *ssymhead;
7290   bfd_size_type i, shndx_count, total_size;
7291 
7292   indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7293   if (indbuf == NULL)
7294     return NULL;
7295 
7296   for (ind = indbuf, i = 0; i < symcount; i++)
7297     if (isymbuf[i].st_shndx != SHN_UNDEF)
7298       *ind++ = &isymbuf[i];
7299   indbufend = ind;
7300 
7301   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7302 	 elf_sort_elf_symbol);
7303 
7304   shndx_count = 0;
7305   if (indbufend > indbuf)
7306     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7307       if (ind[0]->st_shndx != ind[1]->st_shndx)
7308 	shndx_count++;
7309 
7310   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7311 		+ (indbufend - indbuf) * sizeof (*ssym));
7312   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7313   if (ssymbuf == NULL)
7314     {
7315       free (indbuf);
7316       return NULL;
7317     }
7318 
7319   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7320   ssymbuf->ssym = NULL;
7321   ssymbuf->count = shndx_count;
7322   ssymbuf->st_shndx = 0;
7323   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7324     {
7325       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7326 	{
7327 	  ssymhead++;
7328 	  ssymhead->ssym = ssym;
7329 	  ssymhead->count = 0;
7330 	  ssymhead->st_shndx = (*ind)->st_shndx;
7331 	}
7332       ssym->st_name = (*ind)->st_name;
7333       ssym->st_info = (*ind)->st_info;
7334       ssym->st_other = (*ind)->st_other;
7335       ssymhead->count++;
7336     }
7337   BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7338 	      && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7339 		  == total_size));
7340 
7341   free (indbuf);
7342   return ssymbuf;
7343 }
7344 
7345 /* Check if 2 sections define the same set of local and global
7346    symbols.  */
7347 
7348 static bfd_boolean
7349 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7350 				   struct bfd_link_info *info)
7351 {
7352   bfd *bfd1, *bfd2;
7353   const struct elf_backend_data *bed1, *bed2;
7354   Elf_Internal_Shdr *hdr1, *hdr2;
7355   bfd_size_type symcount1, symcount2;
7356   Elf_Internal_Sym *isymbuf1, *isymbuf2;
7357   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7358   Elf_Internal_Sym *isym, *isymend;
7359   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7360   bfd_size_type count1, count2, i;
7361   unsigned int shndx1, shndx2;
7362   bfd_boolean result;
7363 
7364   bfd1 = sec1->owner;
7365   bfd2 = sec2->owner;
7366 
7367   /* Both sections have to be in ELF.  */
7368   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7369       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7370     return FALSE;
7371 
7372   if (elf_section_type (sec1) != elf_section_type (sec2))
7373     return FALSE;
7374 
7375   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7376   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7377   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7378     return FALSE;
7379 
7380   bed1 = get_elf_backend_data (bfd1);
7381   bed2 = get_elf_backend_data (bfd2);
7382   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7383   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7384   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7385   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7386 
7387   if (symcount1 == 0 || symcount2 == 0)
7388     return FALSE;
7389 
7390   result = FALSE;
7391   isymbuf1 = NULL;
7392   isymbuf2 = NULL;
7393   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7394   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7395 
7396   if (ssymbuf1 == NULL)
7397     {
7398       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7399 				       NULL, NULL, NULL);
7400       if (isymbuf1 == NULL)
7401 	goto done;
7402 
7403       if (!info->reduce_memory_overheads)
7404 	elf_tdata (bfd1)->symbuf = ssymbuf1
7405 	  = elf_create_symbuf (symcount1, isymbuf1);
7406     }
7407 
7408   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7409     {
7410       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7411 				       NULL, NULL, NULL);
7412       if (isymbuf2 == NULL)
7413 	goto done;
7414 
7415       if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7416 	elf_tdata (bfd2)->symbuf = ssymbuf2
7417 	  = elf_create_symbuf (symcount2, isymbuf2);
7418     }
7419 
7420   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7421     {
7422       /* Optimized faster version.  */
7423       bfd_size_type lo, hi, mid;
7424       struct elf_symbol *symp;
7425       struct elf_symbuf_symbol *ssym, *ssymend;
7426 
7427       lo = 0;
7428       hi = ssymbuf1->count;
7429       ssymbuf1++;
7430       count1 = 0;
7431       while (lo < hi)
7432 	{
7433 	  mid = (lo + hi) / 2;
7434 	  if (shndx1 < ssymbuf1[mid].st_shndx)
7435 	    hi = mid;
7436 	  else if (shndx1 > ssymbuf1[mid].st_shndx)
7437 	    lo = mid + 1;
7438 	  else
7439 	    {
7440 	      count1 = ssymbuf1[mid].count;
7441 	      ssymbuf1 += mid;
7442 	      break;
7443 	    }
7444 	}
7445 
7446       lo = 0;
7447       hi = ssymbuf2->count;
7448       ssymbuf2++;
7449       count2 = 0;
7450       while (lo < hi)
7451 	{
7452 	  mid = (lo + hi) / 2;
7453 	  if (shndx2 < ssymbuf2[mid].st_shndx)
7454 	    hi = mid;
7455 	  else if (shndx2 > ssymbuf2[mid].st_shndx)
7456 	    lo = mid + 1;
7457 	  else
7458 	    {
7459 	      count2 = ssymbuf2[mid].count;
7460 	      ssymbuf2 += mid;
7461 	      break;
7462 	    }
7463 	}
7464 
7465       if (count1 == 0 || count2 == 0 || count1 != count2)
7466 	goto done;
7467 
7468       symtable1
7469 	= (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7470       symtable2
7471 	= (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7472       if (symtable1 == NULL || symtable2 == NULL)
7473 	goto done;
7474 
7475       symp = symtable1;
7476       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7477 	   ssym < ssymend; ssym++, symp++)
7478 	{
7479 	  symp->u.ssym = ssym;
7480 	  symp->name = bfd_elf_string_from_elf_section (bfd1,
7481 							hdr1->sh_link,
7482 							ssym->st_name);
7483 	}
7484 
7485       symp = symtable2;
7486       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7487 	   ssym < ssymend; ssym++, symp++)
7488 	{
7489 	  symp->u.ssym = ssym;
7490 	  symp->name = bfd_elf_string_from_elf_section (bfd2,
7491 							hdr2->sh_link,
7492 							ssym->st_name);
7493 	}
7494 
7495       /* Sort symbol by name.  */
7496       qsort (symtable1, count1, sizeof (struct elf_symbol),
7497 	     elf_sym_name_compare);
7498       qsort (symtable2, count1, sizeof (struct elf_symbol),
7499 	     elf_sym_name_compare);
7500 
7501       for (i = 0; i < count1; i++)
7502 	/* Two symbols must have the same binding, type and name.  */
7503 	if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7504 	    || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7505 	    || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7506 	  goto done;
7507 
7508       result = TRUE;
7509       goto done;
7510     }
7511 
7512   symtable1 = (struct elf_symbol *)
7513       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7514   symtable2 = (struct elf_symbol *)
7515       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7516   if (symtable1 == NULL || symtable2 == NULL)
7517     goto done;
7518 
7519   /* Count definitions in the section.  */
7520   count1 = 0;
7521   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7522     if (isym->st_shndx == shndx1)
7523       symtable1[count1++].u.isym = isym;
7524 
7525   count2 = 0;
7526   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7527     if (isym->st_shndx == shndx2)
7528       symtable2[count2++].u.isym = isym;
7529 
7530   if (count1 == 0 || count2 == 0 || count1 != count2)
7531     goto done;
7532 
7533   for (i = 0; i < count1; i++)
7534     symtable1[i].name
7535       = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7536 					 symtable1[i].u.isym->st_name);
7537 
7538   for (i = 0; i < count2; i++)
7539     symtable2[i].name
7540       = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7541 					 symtable2[i].u.isym->st_name);
7542 
7543   /* Sort symbol by name.  */
7544   qsort (symtable1, count1, sizeof (struct elf_symbol),
7545 	 elf_sym_name_compare);
7546   qsort (symtable2, count1, sizeof (struct elf_symbol),
7547 	 elf_sym_name_compare);
7548 
7549   for (i = 0; i < count1; i++)
7550     /* Two symbols must have the same binding, type and name.  */
7551     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7552 	|| symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7553 	|| strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7554       goto done;
7555 
7556   result = TRUE;
7557 
7558 done:
7559   if (symtable1)
7560     free (symtable1);
7561   if (symtable2)
7562     free (symtable2);
7563   if (isymbuf1)
7564     free (isymbuf1);
7565   if (isymbuf2)
7566     free (isymbuf2);
7567 
7568   return result;
7569 }
7570 
7571 /* Return TRUE if 2 section types are compatible.  */
7572 
7573 bfd_boolean
7574 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7575 				 bfd *bbfd, const asection *bsec)
7576 {
7577   if (asec == NULL
7578       || bsec == NULL
7579       || abfd->xvec->flavour != bfd_target_elf_flavour
7580       || bbfd->xvec->flavour != bfd_target_elf_flavour)
7581     return TRUE;
7582 
7583   return elf_section_type (asec) == elf_section_type (bsec);
7584 }
7585 
7586 /* Final phase of ELF linker.  */
7587 
7588 /* A structure we use to avoid passing large numbers of arguments.  */
7589 
7590 struct elf_final_link_info
7591 {
7592   /* General link information.  */
7593   struct bfd_link_info *info;
7594   /* Output BFD.  */
7595   bfd *output_bfd;
7596   /* Symbol string table.  */
7597   struct elf_strtab_hash *symstrtab;
7598   /* .hash section.  */
7599   asection *hash_sec;
7600   /* symbol version section (.gnu.version).  */
7601   asection *symver_sec;
7602   /* Buffer large enough to hold contents of any section.  */
7603   bfd_byte *contents;
7604   /* Buffer large enough to hold external relocs of any section.  */
7605   void *external_relocs;
7606   /* Buffer large enough to hold internal relocs of any section.  */
7607   Elf_Internal_Rela *internal_relocs;
7608   /* Buffer large enough to hold external local symbols of any input
7609      BFD.  */
7610   bfd_byte *external_syms;
7611   /* And a buffer for symbol section indices.  */
7612   Elf_External_Sym_Shndx *locsym_shndx;
7613   /* Buffer large enough to hold internal local symbols of any input
7614      BFD.  */
7615   Elf_Internal_Sym *internal_syms;
7616   /* Array large enough to hold a symbol index for each local symbol
7617      of any input BFD.  */
7618   long *indices;
7619   /* Array large enough to hold a section pointer for each local
7620      symbol of any input BFD.  */
7621   asection **sections;
7622   /* Buffer for SHT_SYMTAB_SHNDX section.  */
7623   Elf_External_Sym_Shndx *symshndxbuf;
7624   /* Number of STT_FILE syms seen.  */
7625   size_t filesym_count;
7626 };
7627 
7628 /* This struct is used to pass information to elf_link_output_extsym.  */
7629 
7630 struct elf_outext_info
7631 {
7632   bfd_boolean failed;
7633   bfd_boolean localsyms;
7634   bfd_boolean file_sym_done;
7635   struct elf_final_link_info *flinfo;
7636 };
7637 
7638 
7639 /* Support for evaluating a complex relocation.
7640 
7641    Complex relocations are generalized, self-describing relocations.  The
7642    implementation of them consists of two parts: complex symbols, and the
7643    relocations themselves.
7644 
7645    The relocations are use a reserved elf-wide relocation type code (R_RELC
7646    external / BFD_RELOC_RELC internal) and an encoding of relocation field
7647    information (start bit, end bit, word width, etc) into the addend.  This
7648    information is extracted from CGEN-generated operand tables within gas.
7649 
7650    Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7651    internal) representing prefix-notation expressions, including but not
7652    limited to those sorts of expressions normally encoded as addends in the
7653    addend field.  The symbol mangling format is:
7654 
7655    <node> := <literal>
7656           |  <unary-operator> ':' <node>
7657           |  <binary-operator> ':' <node> ':' <node>
7658 	  ;
7659 
7660    <literal> := 's' <digits=N> ':' <N character symbol name>
7661              |  'S' <digits=N> ':' <N character section name>
7662 	     |  '#' <hexdigits>
7663 	     ;
7664 
7665    <binary-operator> := as in C
7666    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
7667 
7668 static void
7669 set_symbol_value (bfd *bfd_with_globals,
7670 		  Elf_Internal_Sym *isymbuf,
7671 		  size_t locsymcount,
7672 		  size_t symidx,
7673 		  bfd_vma val)
7674 {
7675   struct elf_link_hash_entry **sym_hashes;
7676   struct elf_link_hash_entry *h;
7677   size_t extsymoff = locsymcount;
7678 
7679   if (symidx < locsymcount)
7680     {
7681       Elf_Internal_Sym *sym;
7682 
7683       sym = isymbuf + symidx;
7684       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7685 	{
7686 	  /* It is a local symbol: move it to the
7687 	     "absolute" section and give it a value.  */
7688 	  sym->st_shndx = SHN_ABS;
7689 	  sym->st_value = val;
7690 	  return;
7691 	}
7692       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7693       extsymoff = 0;
7694     }
7695 
7696   /* It is a global symbol: set its link type
7697      to "defined" and give it a value.  */
7698 
7699   sym_hashes = elf_sym_hashes (bfd_with_globals);
7700   h = sym_hashes [symidx - extsymoff];
7701   while (h->root.type == bfd_link_hash_indirect
7702 	 || h->root.type == bfd_link_hash_warning)
7703     h = (struct elf_link_hash_entry *) h->root.u.i.link;
7704   h->root.type = bfd_link_hash_defined;
7705   h->root.u.def.value = val;
7706   h->root.u.def.section = bfd_abs_section_ptr;
7707 }
7708 
7709 static bfd_boolean
7710 resolve_symbol (const char *name,
7711 		bfd *input_bfd,
7712 		struct elf_final_link_info *flinfo,
7713 		bfd_vma *result,
7714 		Elf_Internal_Sym *isymbuf,
7715 		size_t locsymcount)
7716 {
7717   Elf_Internal_Sym *sym;
7718   struct bfd_link_hash_entry *global_entry;
7719   const char *candidate = NULL;
7720   Elf_Internal_Shdr *symtab_hdr;
7721   size_t i;
7722 
7723   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7724 
7725   for (i = 0; i < locsymcount; ++ i)
7726     {
7727       sym = isymbuf + i;
7728 
7729       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7730 	continue;
7731 
7732       candidate = bfd_elf_string_from_elf_section (input_bfd,
7733 						   symtab_hdr->sh_link,
7734 						   sym->st_name);
7735 #ifdef DEBUG
7736       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7737 	      name, candidate, (unsigned long) sym->st_value);
7738 #endif
7739       if (candidate && strcmp (candidate, name) == 0)
7740 	{
7741 	  asection *sec = flinfo->sections [i];
7742 
7743 	  *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7744 	  *result += sec->output_offset + sec->output_section->vma;
7745 #ifdef DEBUG
7746 	  printf ("Found symbol with value %8.8lx\n",
7747 		  (unsigned long) *result);
7748 #endif
7749 	  return TRUE;
7750 	}
7751     }
7752 
7753   /* Hmm, haven't found it yet. perhaps it is a global.  */
7754   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7755 				       FALSE, FALSE, TRUE);
7756   if (!global_entry)
7757     return FALSE;
7758 
7759   if (global_entry->type == bfd_link_hash_defined
7760       || global_entry->type == bfd_link_hash_defweak)
7761     {
7762       *result = (global_entry->u.def.value
7763 		 + global_entry->u.def.section->output_section->vma
7764 		 + global_entry->u.def.section->output_offset);
7765 #ifdef DEBUG
7766       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7767 	      global_entry->root.string, (unsigned long) *result);
7768 #endif
7769       return TRUE;
7770     }
7771 
7772   return FALSE;
7773 }
7774 
7775 static bfd_boolean
7776 resolve_section (const char *name,
7777 		 asection *sections,
7778 		 bfd_vma *result)
7779 {
7780   asection *curr;
7781   unsigned int len;
7782 
7783   for (curr = sections; curr; curr = curr->next)
7784     if (strcmp (curr->name, name) == 0)
7785       {
7786 	*result = curr->vma;
7787 	return TRUE;
7788       }
7789 
7790   /* Hmm. still haven't found it. try pseudo-section names.  */
7791   for (curr = sections; curr; curr = curr->next)
7792     {
7793       len = strlen (curr->name);
7794       if (len > strlen (name))
7795 	continue;
7796 
7797       if (strncmp (curr->name, name, len) == 0)
7798 	{
7799 	  if (strncmp (".end", name + len, 4) == 0)
7800 	    {
7801 	      *result = curr->vma + curr->size;
7802 	      return TRUE;
7803 	    }
7804 
7805 	  /* Insert more pseudo-section names here, if you like.  */
7806 	}
7807     }
7808 
7809   return FALSE;
7810 }
7811 
7812 static void
7813 undefined_reference (const char *reftype, const char *name)
7814 {
7815   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7816 		      reftype, name);
7817 }
7818 
7819 static bfd_boolean
7820 eval_symbol (bfd_vma *result,
7821 	     const char **symp,
7822 	     bfd *input_bfd,
7823 	     struct elf_final_link_info *flinfo,
7824 	     bfd_vma dot,
7825 	     Elf_Internal_Sym *isymbuf,
7826 	     size_t locsymcount,
7827 	     int signed_p)
7828 {
7829   size_t len;
7830   size_t symlen;
7831   bfd_vma a;
7832   bfd_vma b;
7833   char symbuf[4096];
7834   const char *sym = *symp;
7835   const char *symend;
7836   bfd_boolean symbol_is_section = FALSE;
7837 
7838   len = strlen (sym);
7839   symend = sym + len;
7840 
7841   if (len < 1 || len > sizeof (symbuf))
7842     {
7843       bfd_set_error (bfd_error_invalid_operation);
7844       return FALSE;
7845     }
7846 
7847   switch (* sym)
7848     {
7849     case '.':
7850       *result = dot;
7851       *symp = sym + 1;
7852       return TRUE;
7853 
7854     case '#':
7855       ++sym;
7856       *result = strtoul (sym, (char **) symp, 16);
7857       return TRUE;
7858 
7859     case 'S':
7860       symbol_is_section = TRUE;
7861     case 's':
7862       ++sym;
7863       symlen = strtol (sym, (char **) symp, 10);
7864       sym = *symp + 1; /* Skip the trailing ':'.  */
7865 
7866       if (symend < sym || symlen + 1 > sizeof (symbuf))
7867 	{
7868 	  bfd_set_error (bfd_error_invalid_operation);
7869 	  return FALSE;
7870 	}
7871 
7872       memcpy (symbuf, sym, symlen);
7873       symbuf[symlen] = '\0';
7874       *symp = sym + symlen;
7875 
7876       /* Is it always possible, with complex symbols, that gas "mis-guessed"
7877 	 the symbol as a section, or vice-versa. so we're pretty liberal in our
7878 	 interpretation here; section means "try section first", not "must be a
7879 	 section", and likewise with symbol.  */
7880 
7881       if (symbol_is_section)
7882 	{
7883 	  if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7884 	      && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7885 				  isymbuf, locsymcount))
7886 	    {
7887 	      undefined_reference ("section", symbuf);
7888 	      return FALSE;
7889 	    }
7890 	}
7891       else
7892 	{
7893 	  if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7894 			       isymbuf, locsymcount)
7895 	      && !resolve_section (symbuf, flinfo->output_bfd->sections,
7896 				   result))
7897 	    {
7898 	      undefined_reference ("symbol", symbuf);
7899 	      return FALSE;
7900 	    }
7901 	}
7902 
7903       return TRUE;
7904 
7905       /* All that remains are operators.  */
7906 
7907 #define UNARY_OP(op)						\
7908   if (strncmp (sym, #op, strlen (#op)) == 0)			\
7909     {								\
7910       sym += strlen (#op);					\
7911       if (*sym == ':')						\
7912 	++sym;							\
7913       *symp = sym;						\
7914       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
7915 			isymbuf, locsymcount, signed_p))	\
7916 	return FALSE;						\
7917       if (signed_p)						\
7918 	*result = op ((bfd_signed_vma) a);			\
7919       else							\
7920 	*result = op a;						\
7921       return TRUE;						\
7922     }
7923 
7924 #define BINARY_OP(op)						\
7925   if (strncmp (sym, #op, strlen (#op)) == 0)			\
7926     {								\
7927       sym += strlen (#op);					\
7928       if (*sym == ':')						\
7929 	++sym;							\
7930       *symp = sym;						\
7931       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
7932 			isymbuf, locsymcount, signed_p))	\
7933 	return FALSE;						\
7934       ++*symp;							\
7935       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,	\
7936 			isymbuf, locsymcount, signed_p))	\
7937 	return FALSE;						\
7938       if (signed_p)						\
7939 	*result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b);	\
7940       else							\
7941 	*result = a op b;					\
7942       return TRUE;						\
7943     }
7944 
7945     default:
7946       UNARY_OP  (0-);
7947       BINARY_OP (<<);
7948       BINARY_OP (>>);
7949       BINARY_OP (==);
7950       BINARY_OP (!=);
7951       BINARY_OP (<=);
7952       BINARY_OP (>=);
7953       BINARY_OP (&&);
7954       BINARY_OP (||);
7955       UNARY_OP  (~);
7956       UNARY_OP  (!);
7957       BINARY_OP (*);
7958       BINARY_OP (/);
7959       BINARY_OP (%);
7960       BINARY_OP (^);
7961       BINARY_OP (|);
7962       BINARY_OP (&);
7963       BINARY_OP (+);
7964       BINARY_OP (-);
7965       BINARY_OP (<);
7966       BINARY_OP (>);
7967 #undef UNARY_OP
7968 #undef BINARY_OP
7969       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7970       bfd_set_error (bfd_error_invalid_operation);
7971       return FALSE;
7972     }
7973 }
7974 
7975 static void
7976 put_value (bfd_vma size,
7977 	   unsigned long chunksz,
7978 	   bfd *input_bfd,
7979 	   bfd_vma x,
7980 	   bfd_byte *location)
7981 {
7982   location += (size - chunksz);
7983 
7984   for (; size; size -= chunksz, location -= chunksz)
7985     {
7986       switch (chunksz)
7987 	{
7988 	case 1:
7989 	  bfd_put_8 (input_bfd, x, location);
7990 	  x >>= 8;
7991 	  break;
7992 	case 2:
7993 	  bfd_put_16 (input_bfd, x, location);
7994 	  x >>= 16;
7995 	  break;
7996 	case 4:
7997 	  bfd_put_32 (input_bfd, x, location);
7998 	  /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
7999 	  x >>= 16;
8000 	  x >>= 16;
8001 	  break;
8002 #ifdef BFD64
8003 	case 8:
8004 	  bfd_put_64 (input_bfd, x, location);
8005 	  /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
8006 	  x >>= 32;
8007 	  x >>= 32;
8008 	  break;
8009 #endif
8010 	default:
8011 	  abort ();
8012 	  break;
8013 	}
8014     }
8015 }
8016 
8017 static bfd_vma
8018 get_value (bfd_vma size,
8019 	   unsigned long chunksz,
8020 	   bfd *input_bfd,
8021 	   bfd_byte *location)
8022 {
8023   int shift;
8024   bfd_vma x = 0;
8025 
8026   /* Sanity checks.  */
8027   BFD_ASSERT (chunksz <= sizeof (x)
8028 	      && size >= chunksz
8029 	      && chunksz != 0
8030 	      && (size % chunksz) == 0
8031 	      && input_bfd != NULL
8032 	      && location != NULL);
8033 
8034   if (chunksz == sizeof (x))
8035     {
8036       BFD_ASSERT (size == chunksz);
8037 
8038       /* Make sure that we do not perform an undefined shift operation.
8039 	 We know that size == chunksz so there will only be one iteration
8040 	 of the loop below.  */
8041       shift = 0;
8042     }
8043   else
8044     shift = 8 * chunksz;
8045 
8046   for (; size; size -= chunksz, location += chunksz)
8047     {
8048       switch (chunksz)
8049 	{
8050 	case 1:
8051 	  x = (x << shift) | bfd_get_8 (input_bfd, location);
8052 	  break;
8053 	case 2:
8054 	  x = (x << shift) | bfd_get_16 (input_bfd, location);
8055 	  break;
8056 	case 4:
8057 	  x = (x << shift) | bfd_get_32 (input_bfd, location);
8058 	  break;
8059 #ifdef BFD64
8060 	case 8:
8061 	  x = (x << shift) | bfd_get_64 (input_bfd, location);
8062 	  break;
8063 #endif
8064 	default:
8065 	  abort ();
8066 	}
8067     }
8068   return x;
8069 }
8070 
8071 static void
8072 decode_complex_addend (unsigned long *start,   /* in bits */
8073 		       unsigned long *oplen,   /* in bits */
8074 		       unsigned long *len,     /* in bits */
8075 		       unsigned long *wordsz,  /* in bytes */
8076 		       unsigned long *chunksz, /* in bytes */
8077 		       unsigned long *lsb0_p,
8078 		       unsigned long *signed_p,
8079 		       unsigned long *trunc_p,
8080 		       unsigned long encoded)
8081 {
8082   * start     =  encoded        & 0x3F;
8083   * len       = (encoded >>  6) & 0x3F;
8084   * oplen     = (encoded >> 12) & 0x3F;
8085   * wordsz    = (encoded >> 18) & 0xF;
8086   * chunksz   = (encoded >> 22) & 0xF;
8087   * lsb0_p    = (encoded >> 27) & 1;
8088   * signed_p  = (encoded >> 28) & 1;
8089   * trunc_p   = (encoded >> 29) & 1;
8090 }
8091 
8092 bfd_reloc_status_type
8093 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8094 				    asection *input_section ATTRIBUTE_UNUSED,
8095 				    bfd_byte *contents,
8096 				    Elf_Internal_Rela *rel,
8097 				    bfd_vma relocation)
8098 {
8099   bfd_vma shift, x, mask;
8100   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8101   bfd_reloc_status_type r;
8102 
8103   /*  Perform this reloc, since it is complex.
8104       (this is not to say that it necessarily refers to a complex
8105       symbol; merely that it is a self-describing CGEN based reloc.
8106       i.e. the addend has the complete reloc information (bit start, end,
8107       word size, etc) encoded within it.).  */
8108 
8109   decode_complex_addend (&start, &oplen, &len, &wordsz,
8110 			 &chunksz, &lsb0_p, &signed_p,
8111 			 &trunc_p, rel->r_addend);
8112 
8113   mask = (((1L << (len - 1)) - 1) << 1) | 1;
8114 
8115   if (lsb0_p)
8116     shift = (start + 1) - len;
8117   else
8118     shift = (8 * wordsz) - (start + len);
8119 
8120   /* FIXME: octets_per_byte.  */
8121   x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
8122 
8123 #ifdef DEBUG
8124   printf ("Doing complex reloc: "
8125 	  "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8126 	  "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8127 	  "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8128 	  lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8129 	  oplen, (unsigned long) x, (unsigned long) mask,
8130 	  (unsigned long) relocation);
8131 #endif
8132 
8133   r = bfd_reloc_ok;
8134   if (! trunc_p)
8135     /* Now do an overflow check.  */
8136     r = bfd_check_overflow ((signed_p
8137 			     ? complain_overflow_signed
8138 			     : complain_overflow_unsigned),
8139 			    len, 0, (8 * wordsz),
8140 			    relocation);
8141 
8142   /* Do the deed.  */
8143   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8144 
8145 #ifdef DEBUG
8146   printf ("           relocation: %8.8lx\n"
8147 	  "         shifted mask: %8.8lx\n"
8148 	  " shifted/masked reloc: %8.8lx\n"
8149 	  "               result: %8.8lx\n",
8150 	  (unsigned long) relocation, (unsigned long) (mask << shift),
8151 	  (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8152 #endif
8153   /* FIXME: octets_per_byte.  */
8154   put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
8155   return r;
8156 }
8157 
8158 /* Functions to read r_offset from external (target order) reloc
8159    entry.  Faster than bfd_getl32 et al, because we let the compiler
8160    know the value is aligned.  */
8161 
8162 static bfd_vma
8163 ext32l_r_offset (const void *p)
8164 {
8165   union aligned32
8166   {
8167     uint32_t v;
8168     unsigned char c[4];
8169   };
8170   const union aligned32 *a
8171     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8172 
8173   uint32_t aval = (  (uint32_t) a->c[0]
8174 		   | (uint32_t) a->c[1] << 8
8175 		   | (uint32_t) a->c[2] << 16
8176 		   | (uint32_t) a->c[3] << 24);
8177   return aval;
8178 }
8179 
8180 static bfd_vma
8181 ext32b_r_offset (const void *p)
8182 {
8183   union aligned32
8184   {
8185     uint32_t v;
8186     unsigned char c[4];
8187   };
8188   const union aligned32 *a
8189     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8190 
8191   uint32_t aval = (  (uint32_t) a->c[0] << 24
8192 		   | (uint32_t) a->c[1] << 16
8193 		   | (uint32_t) a->c[2] << 8
8194 		   | (uint32_t) a->c[3]);
8195   return aval;
8196 }
8197 
8198 #ifdef BFD_HOST_64_BIT
8199 static bfd_vma
8200 ext64l_r_offset (const void *p)
8201 {
8202   union aligned64
8203   {
8204     uint64_t v;
8205     unsigned char c[8];
8206   };
8207   const union aligned64 *a
8208     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8209 
8210   uint64_t aval = (  (uint64_t) a->c[0]
8211 		   | (uint64_t) a->c[1] << 8
8212 		   | (uint64_t) a->c[2] << 16
8213 		   | (uint64_t) a->c[3] << 24
8214 		   | (uint64_t) a->c[4] << 32
8215 		   | (uint64_t) a->c[5] << 40
8216 		   | (uint64_t) a->c[6] << 48
8217 		   | (uint64_t) a->c[7] << 56);
8218   return aval;
8219 }
8220 
8221 static bfd_vma
8222 ext64b_r_offset (const void *p)
8223 {
8224   union aligned64
8225   {
8226     uint64_t v;
8227     unsigned char c[8];
8228   };
8229   const union aligned64 *a
8230     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8231 
8232   uint64_t aval = (  (uint64_t) a->c[0] << 56
8233 		   | (uint64_t) a->c[1] << 48
8234 		   | (uint64_t) a->c[2] << 40
8235 		   | (uint64_t) a->c[3] << 32
8236 		   | (uint64_t) a->c[4] << 24
8237 		   | (uint64_t) a->c[5] << 16
8238 		   | (uint64_t) a->c[6] << 8
8239 		   | (uint64_t) a->c[7]);
8240   return aval;
8241 }
8242 #endif
8243 
8244 /* When performing a relocatable link, the input relocations are
8245    preserved.  But, if they reference global symbols, the indices
8246    referenced must be updated.  Update all the relocations found in
8247    RELDATA.  */
8248 
8249 static bfd_boolean
8250 elf_link_adjust_relocs (bfd *abfd,
8251 			struct bfd_elf_section_reloc_data *reldata,
8252 			bfd_boolean sort)
8253 {
8254   unsigned int i;
8255   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8256   bfd_byte *erela;
8257   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8258   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8259   bfd_vma r_type_mask;
8260   int r_sym_shift;
8261   unsigned int count = reldata->count;
8262   struct elf_link_hash_entry **rel_hash = reldata->hashes;
8263 
8264   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8265     {
8266       swap_in = bed->s->swap_reloc_in;
8267       swap_out = bed->s->swap_reloc_out;
8268     }
8269   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8270     {
8271       swap_in = bed->s->swap_reloca_in;
8272       swap_out = bed->s->swap_reloca_out;
8273     }
8274   else
8275     abort ();
8276 
8277   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8278     abort ();
8279 
8280   if (bed->s->arch_size == 32)
8281     {
8282       r_type_mask = 0xff;
8283       r_sym_shift = 8;
8284     }
8285   else
8286     {
8287       r_type_mask = 0xffffffff;
8288       r_sym_shift = 32;
8289     }
8290 
8291   erela = reldata->hdr->contents;
8292   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8293     {
8294       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8295       unsigned int j;
8296 
8297       if (*rel_hash == NULL)
8298 	continue;
8299 
8300       BFD_ASSERT ((*rel_hash)->indx >= 0);
8301 
8302       (*swap_in) (abfd, erela, irela);
8303       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8304 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8305 			   | (irela[j].r_info & r_type_mask));
8306       (*swap_out) (abfd, irela, erela);
8307     }
8308 
8309   if (sort && count != 0)
8310     {
8311       bfd_vma (*ext_r_off) (const void *);
8312       bfd_vma r_off;
8313       size_t elt_size;
8314       bfd_byte *base, *end, *p, *loc;
8315       bfd_byte *buf = NULL;
8316 
8317       if (bed->s->arch_size == 32)
8318 	{
8319 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8320 	    ext_r_off = ext32l_r_offset;
8321 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8322 	    ext_r_off = ext32b_r_offset;
8323 	  else
8324 	    abort ();
8325 	}
8326       else
8327 	{
8328 #ifdef BFD_HOST_64_BIT
8329 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8330 	    ext_r_off = ext64l_r_offset;
8331 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8332 	    ext_r_off = ext64b_r_offset;
8333 	  else
8334 #endif
8335 	    abort ();
8336 	}
8337 
8338       /*  Must use a stable sort here.  A modified insertion sort,
8339 	  since the relocs are mostly sorted already.  */
8340       elt_size = reldata->hdr->sh_entsize;
8341       base = reldata->hdr->contents;
8342       end = base + count * elt_size;
8343       if (elt_size > sizeof (Elf64_External_Rela))
8344 	abort ();
8345 
8346       /* Ensure the first element is lowest.  This acts as a sentinel,
8347 	 speeding the main loop below.  */
8348       r_off = (*ext_r_off) (base);
8349       for (p = loc = base; (p += elt_size) < end; )
8350 	{
8351 	  bfd_vma r_off2 = (*ext_r_off) (p);
8352 	  if (r_off > r_off2)
8353 	    {
8354 	      r_off = r_off2;
8355 	      loc = p;
8356 	    }
8357 	}
8358       if (loc != base)
8359 	{
8360 	  /* Don't just swap *base and *loc as that changes the order
8361 	     of the original base[0] and base[1] if they happen to
8362 	     have the same r_offset.  */
8363 	  bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8364 	  memcpy (onebuf, loc, elt_size);
8365 	  memmove (base + elt_size, base, loc - base);
8366 	  memcpy (base, onebuf, elt_size);
8367 	}
8368 
8369       for (p = base + elt_size; (p += elt_size) < end; )
8370 	{
8371 	  /* base to p is sorted, *p is next to insert.  */
8372 	  r_off = (*ext_r_off) (p);
8373 	  /* Search the sorted region for location to insert.  */
8374 	  loc = p - elt_size;
8375 	  while (r_off < (*ext_r_off) (loc))
8376 	    loc -= elt_size;
8377 	  loc += elt_size;
8378 	  if (loc != p)
8379 	    {
8380 	      /* Chances are there is a run of relocs to insert here,
8381 		 from one of more input files.  Files are not always
8382 		 linked in order due to the way elf_link_input_bfd is
8383 		 called.  See pr17666.  */
8384 	      size_t sortlen = p - loc;
8385 	      bfd_vma r_off2 = (*ext_r_off) (loc);
8386 	      size_t runlen = elt_size;
8387 	      size_t buf_size = 96 * 1024;
8388 	      while (p + runlen < end
8389 		     && (sortlen <= buf_size
8390 			 || runlen + elt_size <= buf_size)
8391 		     && r_off2 > (*ext_r_off) (p + runlen))
8392 		runlen += elt_size;
8393 	      if (buf == NULL)
8394 		{
8395 		  buf = bfd_malloc (buf_size);
8396 		  if (buf == NULL)
8397 		    return FALSE;
8398 		}
8399 	      if (runlen < sortlen)
8400 		{
8401 		  memcpy (buf, p, runlen);
8402 		  memmove (loc + runlen, loc, sortlen);
8403 		  memcpy (loc, buf, runlen);
8404 		}
8405 	      else
8406 		{
8407 		  memcpy (buf, loc, sortlen);
8408 		  memmove (loc, p, runlen);
8409 		  memcpy (loc + runlen, buf, sortlen);
8410 		}
8411 	      p += runlen - elt_size;
8412 	    }
8413 	}
8414       /* Hashes are no longer valid.  */
8415       free (reldata->hashes);
8416       reldata->hashes = NULL;
8417       free (buf);
8418     }
8419   return TRUE;
8420 }
8421 
8422 struct elf_link_sort_rela
8423 {
8424   union {
8425     bfd_vma offset;
8426     bfd_vma sym_mask;
8427   } u;
8428   enum elf_reloc_type_class type;
8429   /* We use this as an array of size int_rels_per_ext_rel.  */
8430   Elf_Internal_Rela rela[1];
8431 };
8432 
8433 static int
8434 elf_link_sort_cmp1 (const void *A, const void *B)
8435 {
8436   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8437   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8438   int relativea, relativeb;
8439 
8440   relativea = a->type == reloc_class_relative;
8441   relativeb = b->type == reloc_class_relative;
8442 
8443   if (relativea < relativeb)
8444     return 1;
8445   if (relativea > relativeb)
8446     return -1;
8447   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8448     return -1;
8449   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8450     return 1;
8451   if (a->rela->r_offset < b->rela->r_offset)
8452     return -1;
8453   if (a->rela->r_offset > b->rela->r_offset)
8454     return 1;
8455   return 0;
8456 }
8457 
8458 static int
8459 elf_link_sort_cmp2 (const void *A, const void *B)
8460 {
8461   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8462   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8463 
8464   if (a->type < b->type)
8465     return -1;
8466   if (a->type > b->type)
8467     return 1;
8468   if (a->u.offset < b->u.offset)
8469     return -1;
8470   if (a->u.offset > b->u.offset)
8471     return 1;
8472   if (a->rela->r_offset < b->rela->r_offset)
8473     return -1;
8474   if (a->rela->r_offset > b->rela->r_offset)
8475     return 1;
8476   return 0;
8477 }
8478 
8479 static size_t
8480 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8481 {
8482   asection *dynamic_relocs;
8483   asection *rela_dyn;
8484   asection *rel_dyn;
8485   bfd_size_type count, size;
8486   size_t i, ret, sort_elt, ext_size;
8487   bfd_byte *sort, *s_non_relative, *p;
8488   struct elf_link_sort_rela *sq;
8489   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8490   int i2e = bed->s->int_rels_per_ext_rel;
8491   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8492   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8493   struct bfd_link_order *lo;
8494   bfd_vma r_sym_mask;
8495   bfd_boolean use_rela;
8496 
8497   /* Find a dynamic reloc section.  */
8498   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8499   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
8500   if (rela_dyn != NULL && rela_dyn->size > 0
8501       && rel_dyn != NULL && rel_dyn->size > 0)
8502     {
8503       bfd_boolean use_rela_initialised = FALSE;
8504 
8505       /* This is just here to stop gcc from complaining.
8506 	 It's initialization checking code is not perfect.  */
8507       use_rela = TRUE;
8508 
8509       /* Both sections are present.  Examine the sizes
8510 	 of the indirect sections to help us choose.  */
8511       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8512 	if (lo->type == bfd_indirect_link_order)
8513 	  {
8514 	    asection *o = lo->u.indirect.section;
8515 
8516 	    if ((o->size % bed->s->sizeof_rela) == 0)
8517 	      {
8518 		if ((o->size % bed->s->sizeof_rel) == 0)
8519 		  /* Section size is divisible by both rel and rela sizes.
8520 		     It is of no help to us.  */
8521 		  ;
8522 		else
8523 		  {
8524 		    /* Section size is only divisible by rela.  */
8525 		    if (use_rela_initialised && (use_rela == FALSE))
8526 		      {
8527 			_bfd_error_handler
8528 			  (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8529 			bfd_set_error (bfd_error_invalid_operation);
8530 			return 0;
8531 		      }
8532 		    else
8533 		      {
8534 			use_rela = TRUE;
8535 			use_rela_initialised = TRUE;
8536 		      }
8537 		  }
8538 	      }
8539 	    else if ((o->size % bed->s->sizeof_rel) == 0)
8540 	      {
8541 		/* Section size is only divisible by rel.  */
8542 		if (use_rela_initialised && (use_rela == TRUE))
8543 		  {
8544 		    _bfd_error_handler
8545 		      (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8546 		    bfd_set_error (bfd_error_invalid_operation);
8547 		    return 0;
8548 		  }
8549 		else
8550 		  {
8551 		    use_rela = FALSE;
8552 		    use_rela_initialised = TRUE;
8553 		  }
8554 	      }
8555 	    else
8556 	      {
8557 		/* The section size is not divisible by either - something is wrong.  */
8558 		_bfd_error_handler
8559 		  (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8560 		bfd_set_error (bfd_error_invalid_operation);
8561 		return 0;
8562 	      }
8563 	  }
8564 
8565       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8566 	if (lo->type == bfd_indirect_link_order)
8567 	  {
8568 	    asection *o = lo->u.indirect.section;
8569 
8570 	    if ((o->size % bed->s->sizeof_rela) == 0)
8571 	      {
8572 		if ((o->size % bed->s->sizeof_rel) == 0)
8573 		  /* Section size is divisible by both rel and rela sizes.
8574 		     It is of no help to us.  */
8575 		  ;
8576 		else
8577 		  {
8578 		    /* Section size is only divisible by rela.  */
8579 		    if (use_rela_initialised && (use_rela == FALSE))
8580 		      {
8581 			_bfd_error_handler
8582 			  (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8583 			bfd_set_error (bfd_error_invalid_operation);
8584 			return 0;
8585 		      }
8586 		    else
8587 		      {
8588 			use_rela = TRUE;
8589 			use_rela_initialised = TRUE;
8590 		      }
8591 		  }
8592 	      }
8593 	    else if ((o->size % bed->s->sizeof_rel) == 0)
8594 	      {
8595 		/* Section size is only divisible by rel.  */
8596 		if (use_rela_initialised && (use_rela == TRUE))
8597 		  {
8598 		    _bfd_error_handler
8599 		      (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8600 		    bfd_set_error (bfd_error_invalid_operation);
8601 		    return 0;
8602 		  }
8603 		else
8604 		  {
8605 		    use_rela = FALSE;
8606 		    use_rela_initialised = TRUE;
8607 		  }
8608 	      }
8609 	    else
8610 	      {
8611 		/* The section size is not divisible by either - something is wrong.  */
8612 		_bfd_error_handler
8613 		  (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8614 		bfd_set_error (bfd_error_invalid_operation);
8615 		return 0;
8616 	      }
8617 	  }
8618 
8619       if (! use_rela_initialised)
8620 	/* Make a guess.  */
8621 	use_rela = TRUE;
8622     }
8623   else if (rela_dyn != NULL && rela_dyn->size > 0)
8624     use_rela = TRUE;
8625   else if (rel_dyn != NULL && rel_dyn->size > 0)
8626     use_rela = FALSE;
8627   else
8628     return 0;
8629 
8630   if (use_rela)
8631     {
8632       dynamic_relocs = rela_dyn;
8633       ext_size = bed->s->sizeof_rela;
8634       swap_in = bed->s->swap_reloca_in;
8635       swap_out = bed->s->swap_reloca_out;
8636     }
8637   else
8638     {
8639       dynamic_relocs = rel_dyn;
8640       ext_size = bed->s->sizeof_rel;
8641       swap_in = bed->s->swap_reloc_in;
8642       swap_out = bed->s->swap_reloc_out;
8643     }
8644 
8645   size = 0;
8646   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8647     if (lo->type == bfd_indirect_link_order)
8648       size += lo->u.indirect.section->size;
8649 
8650   if (size != dynamic_relocs->size)
8651     return 0;
8652 
8653   sort_elt = (sizeof (struct elf_link_sort_rela)
8654 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
8655 
8656   count = dynamic_relocs->size / ext_size;
8657   if (count == 0)
8658     return 0;
8659   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8660 
8661   if (sort == NULL)
8662     {
8663       (*info->callbacks->warning)
8664 	(info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8665       return 0;
8666     }
8667 
8668   if (bed->s->arch_size == 32)
8669     r_sym_mask = ~(bfd_vma) 0xff;
8670   else
8671     r_sym_mask = ~(bfd_vma) 0xffffffff;
8672 
8673   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8674     if (lo->type == bfd_indirect_link_order)
8675       {
8676 	bfd_byte *erel, *erelend;
8677 	asection *o = lo->u.indirect.section;
8678 
8679 	if (o->contents == NULL && o->size != 0)
8680 	  {
8681 	    /* This is a reloc section that is being handled as a normal
8682 	       section.  See bfd_section_from_shdr.  We can't combine
8683 	       relocs in this case.  */
8684 	    free (sort);
8685 	    return 0;
8686 	  }
8687 	erel = o->contents;
8688 	erelend = o->contents + o->size;
8689 	/* FIXME: octets_per_byte.  */
8690 	p = sort + o->output_offset / ext_size * sort_elt;
8691 
8692 	while (erel < erelend)
8693 	  {
8694 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8695 
8696 	    (*swap_in) (abfd, erel, s->rela);
8697 	    s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8698 	    s->u.sym_mask = r_sym_mask;
8699 	    p += sort_elt;
8700 	    erel += ext_size;
8701 	  }
8702       }
8703 
8704   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8705 
8706   for (i = 0, p = sort; i < count; i++, p += sort_elt)
8707     {
8708       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8709       if (s->type != reloc_class_relative)
8710 	break;
8711     }
8712   ret = i;
8713   s_non_relative = p;
8714 
8715   sq = (struct elf_link_sort_rela *) s_non_relative;
8716   for (; i < count; i++, p += sort_elt)
8717     {
8718       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8719       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8720 	sq = sp;
8721       sp->u.offset = sq->rela->r_offset;
8722     }
8723 
8724   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8725 
8726   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8727     if (lo->type == bfd_indirect_link_order)
8728       {
8729 	bfd_byte *erel, *erelend;
8730 	asection *o = lo->u.indirect.section;
8731 
8732 	erel = o->contents;
8733 	erelend = o->contents + o->size;
8734 	/* FIXME: octets_per_byte.  */
8735 	p = sort + o->output_offset / ext_size * sort_elt;
8736 	while (erel < erelend)
8737 	  {
8738 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8739 	    (*swap_out) (abfd, s->rela, erel);
8740 	    p += sort_elt;
8741 	    erel += ext_size;
8742 	  }
8743       }
8744 
8745   free (sort);
8746   *psec = dynamic_relocs;
8747   return ret;
8748 }
8749 
8750 /* Add a symbol to the output symbol string table.  */
8751 
8752 static int
8753 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8754 			   const char *name,
8755 			   Elf_Internal_Sym *elfsym,
8756 			   asection *input_sec,
8757 			   struct elf_link_hash_entry *h)
8758 {
8759   int (*output_symbol_hook)
8760     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8761      struct elf_link_hash_entry *);
8762   struct elf_link_hash_table *hash_table;
8763   const struct elf_backend_data *bed;
8764   bfd_size_type strtabsize;
8765 
8766   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8767 
8768   bed = get_elf_backend_data (flinfo->output_bfd);
8769   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8770   if (output_symbol_hook != NULL)
8771     {
8772       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8773       if (ret != 1)
8774 	return ret;
8775     }
8776 
8777   if (name == NULL
8778       || *name == '\0'
8779       || (input_sec->flags & SEC_EXCLUDE))
8780     elfsym->st_name = (unsigned long) -1;
8781   else
8782     {
8783       /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8784 	 to get the final offset for st_name.  */
8785       elfsym->st_name
8786 	= (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8787 					       name, FALSE);
8788       if (elfsym->st_name == (unsigned long) -1)
8789 	return 0;
8790     }
8791 
8792   hash_table = elf_hash_table (flinfo->info);
8793   strtabsize = hash_table->strtabsize;
8794   if (strtabsize <= hash_table->strtabcount)
8795     {
8796       strtabsize += strtabsize;
8797       hash_table->strtabsize = strtabsize;
8798       strtabsize *= sizeof (*hash_table->strtab);
8799       hash_table->strtab
8800 	= (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8801 						 strtabsize);
8802       if (hash_table->strtab == NULL)
8803 	return 0;
8804     }
8805   hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8806   hash_table->strtab[hash_table->strtabcount].dest_index
8807     = hash_table->strtabcount;
8808   hash_table->strtab[hash_table->strtabcount].destshndx_index
8809     = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8810 
8811   bfd_get_symcount (flinfo->output_bfd) += 1;
8812   hash_table->strtabcount += 1;
8813 
8814   return 1;
8815 }
8816 
8817 /* Swap symbols out to the symbol table and flush the output symbols to
8818    the file.  */
8819 
8820 static bfd_boolean
8821 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8822 {
8823   struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8824   bfd_size_type amt, i;
8825   const struct elf_backend_data *bed;
8826   bfd_byte *symbuf;
8827   Elf_Internal_Shdr *hdr;
8828   file_ptr pos;
8829   bfd_boolean ret;
8830 
8831   if (!hash_table->strtabcount)
8832     return TRUE;
8833 
8834   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8835 
8836   bed = get_elf_backend_data (flinfo->output_bfd);
8837 
8838   amt = bed->s->sizeof_sym * hash_table->strtabcount;
8839   symbuf = (bfd_byte *) bfd_malloc (amt);
8840   if (symbuf == NULL)
8841     return FALSE;
8842 
8843   if (flinfo->symshndxbuf)
8844     {
8845       amt = (sizeof (Elf_External_Sym_Shndx)
8846 	     * (bfd_get_symcount (flinfo->output_bfd)));
8847       flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8848       if (flinfo->symshndxbuf == NULL)
8849 	{
8850 	  free (symbuf);
8851 	  return FALSE;
8852 	}
8853     }
8854 
8855   for (i = 0; i < hash_table->strtabcount; i++)
8856     {
8857       struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8858       if (elfsym->sym.st_name == (unsigned long) -1)
8859 	elfsym->sym.st_name = 0;
8860       else
8861 	elfsym->sym.st_name
8862 	  = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8863 						    elfsym->sym.st_name);
8864       bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8865 			       ((bfd_byte *) symbuf
8866 				+ (elfsym->dest_index
8867 				   * bed->s->sizeof_sym)),
8868 			       (flinfo->symshndxbuf
8869 				+ elfsym->destshndx_index));
8870     }
8871 
8872   hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8873   pos = hdr->sh_offset + hdr->sh_size;
8874   amt = hash_table->strtabcount * bed->s->sizeof_sym;
8875   if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8876       && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8877     {
8878       hdr->sh_size += amt;
8879       ret = TRUE;
8880     }
8881   else
8882     ret = FALSE;
8883 
8884   free (symbuf);
8885 
8886   free (hash_table->strtab);
8887   hash_table->strtab = NULL;
8888 
8889   return ret;
8890 }
8891 
8892 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
8893 
8894 static bfd_boolean
8895 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8896 {
8897   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8898       && sym->st_shndx < SHN_LORESERVE)
8899     {
8900       /* The gABI doesn't support dynamic symbols in output sections
8901 	 beyond 64k.  */
8902       (*_bfd_error_handler)
8903 	(_("%B: Too many sections: %d (>= %d)"),
8904 	 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8905       bfd_set_error (bfd_error_nonrepresentable_section);
8906       return FALSE;
8907     }
8908   return TRUE;
8909 }
8910 
8911 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8912    allowing an unsatisfied unversioned symbol in the DSO to match a
8913    versioned symbol that would normally require an explicit version.
8914    We also handle the case that a DSO references a hidden symbol
8915    which may be satisfied by a versioned symbol in another DSO.  */
8916 
8917 static bfd_boolean
8918 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8919 				 const struct elf_backend_data *bed,
8920 				 struct elf_link_hash_entry *h)
8921 {
8922   bfd *abfd;
8923   struct elf_link_loaded_list *loaded;
8924 
8925   if (!is_elf_hash_table (info->hash))
8926     return FALSE;
8927 
8928   /* Check indirect symbol.  */
8929   while (h->root.type == bfd_link_hash_indirect)
8930     h = (struct elf_link_hash_entry *) h->root.u.i.link;
8931 
8932   switch (h->root.type)
8933     {
8934     default:
8935       abfd = NULL;
8936       break;
8937 
8938     case bfd_link_hash_undefined:
8939     case bfd_link_hash_undefweak:
8940       abfd = h->root.u.undef.abfd;
8941       if ((abfd->flags & DYNAMIC) == 0
8942 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8943 	return FALSE;
8944       break;
8945 
8946     case bfd_link_hash_defined:
8947     case bfd_link_hash_defweak:
8948       abfd = h->root.u.def.section->owner;
8949       break;
8950 
8951     case bfd_link_hash_common:
8952       abfd = h->root.u.c.p->section->owner;
8953       break;
8954     }
8955   BFD_ASSERT (abfd != NULL);
8956 
8957   for (loaded = elf_hash_table (info)->loaded;
8958        loaded != NULL;
8959        loaded = loaded->next)
8960     {
8961       bfd *input;
8962       Elf_Internal_Shdr *hdr;
8963       bfd_size_type symcount;
8964       bfd_size_type extsymcount;
8965       bfd_size_type extsymoff;
8966       Elf_Internal_Shdr *versymhdr;
8967       Elf_Internal_Sym *isym;
8968       Elf_Internal_Sym *isymend;
8969       Elf_Internal_Sym *isymbuf;
8970       Elf_External_Versym *ever;
8971       Elf_External_Versym *extversym;
8972 
8973       input = loaded->abfd;
8974 
8975       /* We check each DSO for a possible hidden versioned definition.  */
8976       if (input == abfd
8977 	  || (input->flags & DYNAMIC) == 0
8978 	  || elf_dynversym (input) == 0)
8979 	continue;
8980 
8981       hdr = &elf_tdata (input)->dynsymtab_hdr;
8982 
8983       symcount = hdr->sh_size / bed->s->sizeof_sym;
8984       if (elf_bad_symtab (input))
8985 	{
8986 	  extsymcount = symcount;
8987 	  extsymoff = 0;
8988 	}
8989       else
8990 	{
8991 	  extsymcount = symcount - hdr->sh_info;
8992 	  extsymoff = hdr->sh_info;
8993 	}
8994 
8995       if (extsymcount == 0)
8996 	continue;
8997 
8998       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8999 				      NULL, NULL, NULL);
9000       if (isymbuf == NULL)
9001 	return FALSE;
9002 
9003       /* Read in any version definitions.  */
9004       versymhdr = &elf_tdata (input)->dynversym_hdr;
9005       extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9006       if (extversym == NULL)
9007 	goto error_ret;
9008 
9009       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9010 	  || (bfd_bread (extversym, versymhdr->sh_size, input)
9011 	      != versymhdr->sh_size))
9012 	{
9013 	  free (extversym);
9014 	error_ret:
9015 	  free (isymbuf);
9016 	  return FALSE;
9017 	}
9018 
9019       ever = extversym + extsymoff;
9020       isymend = isymbuf + extsymcount;
9021       for (isym = isymbuf; isym < isymend; isym++, ever++)
9022 	{
9023 	  const char *name;
9024 	  Elf_Internal_Versym iver;
9025 	  unsigned short version_index;
9026 
9027 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9028 	      || isym->st_shndx == SHN_UNDEF)
9029 	    continue;
9030 
9031 	  name = bfd_elf_string_from_elf_section (input,
9032 						  hdr->sh_link,
9033 						  isym->st_name);
9034 	  if (strcmp (name, h->root.root.string) != 0)
9035 	    continue;
9036 
9037 	  _bfd_elf_swap_versym_in (input, ever, &iver);
9038 
9039 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9040 	      && !(h->def_regular
9041 		   && h->forced_local))
9042 	    {
9043 	      /* If we have a non-hidden versioned sym, then it should
9044 		 have provided a definition for the undefined sym unless
9045 		 it is defined in a non-shared object and forced local.
9046 	       */
9047 	      abort ();
9048 	    }
9049 
9050 	  version_index = iver.vs_vers & VERSYM_VERSION;
9051 	  if (version_index == 1 || version_index == 2)
9052 	    {
9053 	      /* This is the base or first version.  We can use it.  */
9054 	      free (extversym);
9055 	      free (isymbuf);
9056 	      return TRUE;
9057 	    }
9058 	}
9059 
9060       free (extversym);
9061       free (isymbuf);
9062     }
9063 
9064   return FALSE;
9065 }
9066 
9067 /* Add an external symbol to the symbol table.  This is called from
9068    the hash table traversal routine.  When generating a shared object,
9069    we go through the symbol table twice.  The first time we output
9070    anything that might have been forced to local scope in a version
9071    script.  The second time we output the symbols that are still
9072    global symbols.  */
9073 
9074 static bfd_boolean
9075 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9076 {
9077   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9078   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9079   struct elf_final_link_info *flinfo = eoinfo->flinfo;
9080   bfd_boolean strip;
9081   Elf_Internal_Sym sym;
9082   asection *input_sec;
9083   const struct elf_backend_data *bed;
9084   long indx;
9085   int ret;
9086   /* A symbol is bound locally if it is forced local or it is locally
9087      defined, hidden versioned, not referenced by shared library and
9088      not exported when linking executable.  */
9089   bfd_boolean local_bind = (h->forced_local
9090 			    || (bfd_link_executable (flinfo->info)
9091 				&& !flinfo->info->export_dynamic
9092 				&& !h->dynamic
9093 				&& !h->ref_dynamic
9094 				&& h->def_regular
9095 				&& h->versioned == versioned_hidden));
9096 
9097   if (h->root.type == bfd_link_hash_warning)
9098     {
9099       h = (struct elf_link_hash_entry *) h->root.u.i.link;
9100       if (h->root.type == bfd_link_hash_new)
9101 	return TRUE;
9102     }
9103 
9104   /* Decide whether to output this symbol in this pass.  */
9105   if (eoinfo->localsyms)
9106     {
9107       if (!local_bind)
9108 	return TRUE;
9109     }
9110   else
9111     {
9112       if (local_bind)
9113 	return TRUE;
9114     }
9115 
9116   bed = get_elf_backend_data (flinfo->output_bfd);
9117 
9118   if (h->root.type == bfd_link_hash_undefined)
9119     {
9120       /* If we have an undefined symbol reference here then it must have
9121 	 come from a shared library that is being linked in.  (Undefined
9122 	 references in regular files have already been handled unless
9123 	 they are in unreferenced sections which are removed by garbage
9124 	 collection).  */
9125       bfd_boolean ignore_undef = FALSE;
9126 
9127       /* Some symbols may be special in that the fact that they're
9128 	 undefined can be safely ignored - let backend determine that.  */
9129       if (bed->elf_backend_ignore_undef_symbol)
9130 	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9131 
9132       /* If we are reporting errors for this situation then do so now.  */
9133       if (!ignore_undef
9134 	  && h->ref_dynamic
9135 	  && (!h->ref_regular || flinfo->info->gc_sections)
9136 	  && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9137 	  && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9138 	{
9139 	  if (!(flinfo->info->callbacks->undefined_symbol
9140 		(flinfo->info, h->root.root.string,
9141 		 h->ref_regular ? NULL : h->root.u.undef.abfd,
9142 		 NULL, 0,
9143 		 (flinfo->info->unresolved_syms_in_shared_libs
9144 		  == RM_GENERATE_ERROR))))
9145 	    {
9146 	      bfd_set_error (bfd_error_bad_value);
9147 	      eoinfo->failed = TRUE;
9148 	      return FALSE;
9149 	    }
9150 	}
9151     }
9152 
9153   /* We should also warn if a forced local symbol is referenced from
9154      shared libraries.  */
9155   if (bfd_link_executable (flinfo->info)
9156       && h->forced_local
9157       && h->ref_dynamic
9158       && h->def_regular
9159       && !h->dynamic_def
9160       && h->ref_dynamic_nonweak
9161       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9162     {
9163       bfd *def_bfd;
9164       const char *msg;
9165       struct elf_link_hash_entry *hi = h;
9166 
9167       /* Check indirect symbol.  */
9168       while (hi->root.type == bfd_link_hash_indirect)
9169 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9170 
9171       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9172 	msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9173       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9174 	msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9175       else
9176 	msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9177       def_bfd = flinfo->output_bfd;
9178       if (hi->root.u.def.section != bfd_abs_section_ptr)
9179 	def_bfd = hi->root.u.def.section->owner;
9180       (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9181 			     h->root.root.string);
9182       bfd_set_error (bfd_error_bad_value);
9183       eoinfo->failed = TRUE;
9184       return FALSE;
9185     }
9186 
9187   /* We don't want to output symbols that have never been mentioned by
9188      a regular file, or that we have been told to strip.  However, if
9189      h->indx is set to -2, the symbol is used by a reloc and we must
9190      output it.  */
9191   if (h->indx == -2)
9192     strip = FALSE;
9193   else if ((h->def_dynamic
9194 	    || h->ref_dynamic
9195 	    || h->root.type == bfd_link_hash_new)
9196 	   && !h->def_regular
9197 	   && !h->ref_regular)
9198     strip = TRUE;
9199   else if (flinfo->info->strip == strip_all)
9200     strip = TRUE;
9201   else if (flinfo->info->strip == strip_some
9202 	   && bfd_hash_lookup (flinfo->info->keep_hash,
9203 			       h->root.root.string, FALSE, FALSE) == NULL)
9204     strip = TRUE;
9205   else if ((h->root.type == bfd_link_hash_defined
9206 	    || h->root.type == bfd_link_hash_defweak)
9207 	   && ((flinfo->info->strip_discarded
9208 		&& discarded_section (h->root.u.def.section))
9209 	       || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9210 		   && h->root.u.def.section->owner != NULL
9211 		   && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9212     strip = TRUE;
9213   else if ((h->root.type == bfd_link_hash_undefined
9214 	    || h->root.type == bfd_link_hash_undefweak)
9215 	   && h->root.u.undef.abfd != NULL
9216 	   && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9217     strip = TRUE;
9218   else
9219     strip = FALSE;
9220 
9221   /* If we're stripping it, and it's not a dynamic symbol, there's
9222      nothing else to do unless it is a forced local symbol or a
9223      STT_GNU_IFUNC symbol.  */
9224   if (strip
9225       && h->dynindx == -1
9226       && h->type != STT_GNU_IFUNC
9227       && !h->forced_local)
9228     return TRUE;
9229 
9230   sym.st_value = 0;
9231   sym.st_size = h->size;
9232   sym.st_other = h->other;
9233   if (local_bind)
9234     {
9235       sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9236       /* Turn off visibility on local symbol.  */
9237       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9238     }
9239   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
9240   else if (h->unique_global && h->def_regular)
9241     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
9242   else if (h->root.type == bfd_link_hash_undefweak
9243 	   || h->root.type == bfd_link_hash_defweak)
9244     sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9245   else
9246     sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
9247   sym.st_target_internal = h->target_internal;
9248 
9249   switch (h->root.type)
9250     {
9251     default:
9252     case bfd_link_hash_new:
9253     case bfd_link_hash_warning:
9254       abort ();
9255       return FALSE;
9256 
9257     case bfd_link_hash_undefined:
9258     case bfd_link_hash_undefweak:
9259       input_sec = bfd_und_section_ptr;
9260       sym.st_shndx = SHN_UNDEF;
9261       break;
9262 
9263     case bfd_link_hash_defined:
9264     case bfd_link_hash_defweak:
9265       {
9266 	input_sec = h->root.u.def.section;
9267 	if (input_sec->output_section != NULL)
9268 	  {
9269 	    sym.st_shndx =
9270 	      _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9271 						 input_sec->output_section);
9272 	    if (sym.st_shndx == SHN_BAD)
9273 	      {
9274 		(*_bfd_error_handler)
9275 		  (_("%B: could not find output section %A for input section %A"),
9276 		   flinfo->output_bfd, input_sec->output_section, input_sec);
9277 		bfd_set_error (bfd_error_nonrepresentable_section);
9278 		eoinfo->failed = TRUE;
9279 		return FALSE;
9280 	      }
9281 
9282 	    /* ELF symbols in relocatable files are section relative,
9283 	       but in nonrelocatable files they are virtual
9284 	       addresses.  */
9285 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
9286 	    if (!bfd_link_relocatable (flinfo->info))
9287 	      {
9288 		sym.st_value += input_sec->output_section->vma;
9289 		if (h->type == STT_TLS)
9290 		  {
9291 		    asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9292 		    if (tls_sec != NULL)
9293 		      sym.st_value -= tls_sec->vma;
9294 		  }
9295 	      }
9296 	  }
9297 	else
9298 	  {
9299 	    BFD_ASSERT (input_sec->owner == NULL
9300 			|| (input_sec->owner->flags & DYNAMIC) != 0);
9301 	    sym.st_shndx = SHN_UNDEF;
9302 	    input_sec = bfd_und_section_ptr;
9303 	  }
9304       }
9305       break;
9306 
9307     case bfd_link_hash_common:
9308       input_sec = h->root.u.c.p->section;
9309       sym.st_shndx = bed->common_section_index (input_sec);
9310       sym.st_value = 1 << h->root.u.c.p->alignment_power;
9311       break;
9312 
9313     case bfd_link_hash_indirect:
9314       /* These symbols are created by symbol versioning.  They point
9315 	 to the decorated version of the name.  For example, if the
9316 	 symbol foo@@GNU_1.2 is the default, which should be used when
9317 	 foo is used with no version, then we add an indirect symbol
9318 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
9319 	 since the indirected symbol is already in the hash table.  */
9320       return TRUE;
9321     }
9322 
9323   /* Give the processor backend a chance to tweak the symbol value,
9324      and also to finish up anything that needs to be done for this
9325      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
9326      forced local syms when non-shared is due to a historical quirk.
9327      STT_GNU_IFUNC symbol must go through PLT.  */
9328   if ((h->type == STT_GNU_IFUNC
9329        && h->def_regular
9330        && !bfd_link_relocatable (flinfo->info))
9331       || ((h->dynindx != -1
9332 	   || h->forced_local)
9333 	  && ((bfd_link_pic (flinfo->info)
9334 	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9335 		   || h->root.type != bfd_link_hash_undefweak))
9336 	      || !h->forced_local)
9337 	  && elf_hash_table (flinfo->info)->dynamic_sections_created))
9338     {
9339       if (! ((*bed->elf_backend_finish_dynamic_symbol)
9340 	     (flinfo->output_bfd, flinfo->info, h, &sym)))
9341 	{
9342 	  eoinfo->failed = TRUE;
9343 	  return FALSE;
9344 	}
9345     }
9346 
9347   /* If we are marking the symbol as undefined, and there are no
9348      non-weak references to this symbol from a regular object, then
9349      mark the symbol as weak undefined; if there are non-weak
9350      references, mark the symbol as strong.  We can't do this earlier,
9351      because it might not be marked as undefined until the
9352      finish_dynamic_symbol routine gets through with it.  */
9353   if (sym.st_shndx == SHN_UNDEF
9354       && h->ref_regular
9355       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9356 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9357     {
9358       int bindtype;
9359       unsigned int type = ELF_ST_TYPE (sym.st_info);
9360 
9361       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9362       if (type == STT_GNU_IFUNC)
9363 	type = STT_FUNC;
9364 
9365       if (h->ref_regular_nonweak)
9366 	bindtype = STB_GLOBAL;
9367       else
9368 	bindtype = STB_WEAK;
9369       sym.st_info = ELF_ST_INFO (bindtype, type);
9370     }
9371 
9372   /* If this is a symbol defined in a dynamic library, don't use the
9373      symbol size from the dynamic library.  Relinking an executable
9374      against a new library may introduce gratuitous changes in the
9375      executable's symbols if we keep the size.  */
9376   if (sym.st_shndx == SHN_UNDEF
9377       && !h->def_regular
9378       && h->def_dynamic)
9379     sym.st_size = 0;
9380 
9381   /* If a non-weak symbol with non-default visibility is not defined
9382      locally, it is a fatal error.  */
9383   if (!bfd_link_relocatable (flinfo->info)
9384       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9385       && ELF_ST_BIND (sym.st_info) != STB_WEAK
9386       && h->root.type == bfd_link_hash_undefined
9387       && !h->def_regular)
9388     {
9389       const char *msg;
9390 
9391       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9392 	msg = _("%B: protected symbol `%s' isn't defined");
9393       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9394 	msg = _("%B: internal symbol `%s' isn't defined");
9395       else
9396 	msg = _("%B: hidden symbol `%s' isn't defined");
9397       (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9398       bfd_set_error (bfd_error_bad_value);
9399       eoinfo->failed = TRUE;
9400       return FALSE;
9401     }
9402 
9403   /* If this symbol should be put in the .dynsym section, then put it
9404      there now.  We already know the symbol index.  We also fill in
9405      the entry in the .hash section.  */
9406   if (elf_hash_table (flinfo->info)->dynsym != NULL
9407       && h->dynindx != -1
9408       && elf_hash_table (flinfo->info)->dynamic_sections_created)
9409     {
9410       bfd_byte *esym;
9411 
9412       /* Since there is no version information in the dynamic string,
9413 	 if there is no version info in symbol version section, we will
9414 	 have a run-time problem if not linking executable, referenced
9415 	 by shared library, not locally defined, or not bound locally.
9416       */
9417       if (h->verinfo.verdef == NULL
9418 	  && !local_bind
9419 	  && (!bfd_link_executable (flinfo->info)
9420 	      || h->ref_dynamic
9421 	      || !h->def_regular))
9422 	{
9423 	  char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9424 
9425 	  if (p && p [1] != '\0')
9426 	    {
9427 	      (*_bfd_error_handler)
9428 		(_("%B: No symbol version section for versioned symbol `%s'"),
9429 		 flinfo->output_bfd, h->root.root.string);
9430 	      eoinfo->failed = TRUE;
9431 	      return FALSE;
9432 	    }
9433 	}
9434 
9435       sym.st_name = h->dynstr_index;
9436       esym = (elf_hash_table (flinfo->info)->dynsym->contents
9437 	      + h->dynindx * bed->s->sizeof_sym);
9438       if (!check_dynsym (flinfo->output_bfd, &sym))
9439 	{
9440 	  eoinfo->failed = TRUE;
9441 	  return FALSE;
9442 	}
9443       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9444 
9445       if (flinfo->hash_sec != NULL)
9446 	{
9447 	  size_t hash_entry_size;
9448 	  bfd_byte *bucketpos;
9449 	  bfd_vma chain;
9450 	  size_t bucketcount;
9451 	  size_t bucket;
9452 
9453 	  bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9454 	  bucket = h->u.elf_hash_value % bucketcount;
9455 
9456 	  hash_entry_size
9457 	    = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9458 	  bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9459 		       + (bucket + 2) * hash_entry_size);
9460 	  chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9461 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9462 		   bucketpos);
9463 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9464 		   ((bfd_byte *) flinfo->hash_sec->contents
9465 		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9466 	}
9467 
9468       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9469 	{
9470 	  Elf_Internal_Versym iversym;
9471 	  Elf_External_Versym *eversym;
9472 
9473 	  if (!h->def_regular)
9474 	    {
9475 	      if (h->verinfo.verdef == NULL
9476 		  || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9477 		      & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9478 		iversym.vs_vers = 0;
9479 	      else
9480 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9481 	    }
9482 	  else
9483 	    {
9484 	      if (h->verinfo.vertree == NULL)
9485 		iversym.vs_vers = 1;
9486 	      else
9487 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9488 	      if (flinfo->info->create_default_symver)
9489 		iversym.vs_vers++;
9490 	    }
9491 
9492 	  /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9493 	     defined locally.  */
9494 	  if (h->versioned == versioned_hidden && h->def_regular)
9495 	    iversym.vs_vers |= VERSYM_HIDDEN;
9496 
9497 	  eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9498 	  eversym += h->dynindx;
9499 	  _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9500 	}
9501     }
9502 
9503   /* If we're stripping it, then it was just a dynamic symbol, and
9504      there's nothing else to do.  */
9505   if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
9506     return TRUE;
9507 
9508   /* Output a FILE symbol so that following locals are not associated
9509      with the wrong input file.  We need one for forced local symbols
9510      if we've seen more than one FILE symbol or when we have exactly
9511      one FILE symbol but global symbols are present in a file other
9512      than the one with the FILE symbol.  We also need one if linker
9513      defined symbols are present.  In practice these conditions are
9514      always met, so just emit the FILE symbol unconditionally.  */
9515   if (eoinfo->localsyms
9516       && !eoinfo->file_sym_done
9517       && eoinfo->flinfo->filesym_count != 0)
9518     {
9519       Elf_Internal_Sym fsym;
9520 
9521       memset (&fsym, 0, sizeof (fsym));
9522       fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9523       fsym.st_shndx = SHN_ABS;
9524       if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9525 				      bfd_und_section_ptr, NULL))
9526 	return FALSE;
9527 
9528       eoinfo->file_sym_done = TRUE;
9529     }
9530 
9531   indx = bfd_get_symcount (flinfo->output_bfd);
9532   ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9533 				   input_sec, h);
9534   if (ret == 0)
9535     {
9536       eoinfo->failed = TRUE;
9537       return FALSE;
9538     }
9539   else if (ret == 1)
9540     h->indx = indx;
9541   else if (h->indx == -2)
9542     abort();
9543 
9544   return TRUE;
9545 }
9546 
9547 /* Return TRUE if special handling is done for relocs in SEC against
9548    symbols defined in discarded sections.  */
9549 
9550 static bfd_boolean
9551 elf_section_ignore_discarded_relocs (asection *sec)
9552 {
9553   const struct elf_backend_data *bed;
9554 
9555   switch (sec->sec_info_type)
9556     {
9557     case SEC_INFO_TYPE_STABS:
9558     case SEC_INFO_TYPE_EH_FRAME:
9559     case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9560       return TRUE;
9561     default:
9562       break;
9563     }
9564 
9565   bed = get_elf_backend_data (sec->owner);
9566   if (bed->elf_backend_ignore_discarded_relocs != NULL
9567       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9568     return TRUE;
9569 
9570   return FALSE;
9571 }
9572 
9573 /* Return a mask saying how ld should treat relocations in SEC against
9574    symbols defined in discarded sections.  If this function returns
9575    COMPLAIN set, ld will issue a warning message.  If this function
9576    returns PRETEND set, and the discarded section was link-once and the
9577    same size as the kept link-once section, ld will pretend that the
9578    symbol was actually defined in the kept section.  Otherwise ld will
9579    zero the reloc (at least that is the intent, but some cooperation by
9580    the target dependent code is needed, particularly for REL targets).  */
9581 
9582 unsigned int
9583 _bfd_elf_default_action_discarded (asection *sec)
9584 {
9585   if (sec->flags & SEC_DEBUGGING)
9586     return PRETEND;
9587 
9588   if (strcmp (".eh_frame", sec->name) == 0)
9589     return 0;
9590 
9591   if (strcmp (".gcc_except_table", sec->name) == 0)
9592     return 0;
9593 
9594   return COMPLAIN | PRETEND;
9595 }
9596 
9597 /* Find a match between a section and a member of a section group.  */
9598 
9599 static asection *
9600 match_group_member (asection *sec, asection *group,
9601 		    struct bfd_link_info *info)
9602 {
9603   asection *first = elf_next_in_group (group);
9604   asection *s = first;
9605 
9606   while (s != NULL)
9607     {
9608       if (bfd_elf_match_symbols_in_sections (s, sec, info))
9609 	return s;
9610 
9611       s = elf_next_in_group (s);
9612       if (s == first)
9613 	break;
9614     }
9615 
9616   return NULL;
9617 }
9618 
9619 /* Check if the kept section of a discarded section SEC can be used
9620    to replace it.  Return the replacement if it is OK.  Otherwise return
9621    NULL.  */
9622 
9623 asection *
9624 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9625 {
9626   asection *kept;
9627 
9628   kept = sec->kept_section;
9629   if (kept != NULL)
9630     {
9631       if ((kept->flags & SEC_GROUP) != 0)
9632 	kept = match_group_member (sec, kept, info);
9633       if (kept != NULL
9634 	  && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9635 	      != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9636 	kept = NULL;
9637       sec->kept_section = kept;
9638     }
9639   return kept;
9640 }
9641 
9642 /* Link an input file into the linker output file.  This function
9643    handles all the sections and relocations of the input file at once.
9644    This is so that we only have to read the local symbols once, and
9645    don't have to keep them in memory.  */
9646 
9647 static bfd_boolean
9648 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9649 {
9650   int (*relocate_section)
9651     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9652      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9653   bfd *output_bfd;
9654   Elf_Internal_Shdr *symtab_hdr;
9655   size_t locsymcount;
9656   size_t extsymoff;
9657   Elf_Internal_Sym *isymbuf;
9658   Elf_Internal_Sym *isym;
9659   Elf_Internal_Sym *isymend;
9660   long *pindex;
9661   asection **ppsection;
9662   asection *o;
9663   const struct elf_backend_data *bed;
9664   struct elf_link_hash_entry **sym_hashes;
9665   bfd_size_type address_size;
9666   bfd_vma r_type_mask;
9667   int r_sym_shift;
9668   bfd_boolean have_file_sym = FALSE;
9669 
9670   output_bfd = flinfo->output_bfd;
9671   bed = get_elf_backend_data (output_bfd);
9672   relocate_section = bed->elf_backend_relocate_section;
9673 
9674   /* If this is a dynamic object, we don't want to do anything here:
9675      we don't want the local symbols, and we don't want the section
9676      contents.  */
9677   if ((input_bfd->flags & DYNAMIC) != 0)
9678     return TRUE;
9679 
9680   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9681   if (elf_bad_symtab (input_bfd))
9682     {
9683       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9684       extsymoff = 0;
9685     }
9686   else
9687     {
9688       locsymcount = symtab_hdr->sh_info;
9689       extsymoff = symtab_hdr->sh_info;
9690     }
9691 
9692   /* Read the local symbols.  */
9693   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9694   if (isymbuf == NULL && locsymcount != 0)
9695     {
9696       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9697 				      flinfo->internal_syms,
9698 				      flinfo->external_syms,
9699 				      flinfo->locsym_shndx);
9700       if (isymbuf == NULL)
9701 	return FALSE;
9702     }
9703 
9704   /* Find local symbol sections and adjust values of symbols in
9705      SEC_MERGE sections.  Write out those local symbols we know are
9706      going into the output file.  */
9707   isymend = isymbuf + locsymcount;
9708   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9709        isym < isymend;
9710        isym++, pindex++, ppsection++)
9711     {
9712       asection *isec;
9713       const char *name;
9714       Elf_Internal_Sym osym;
9715       long indx;
9716       int ret;
9717 
9718       *pindex = -1;
9719 
9720       if (elf_bad_symtab (input_bfd))
9721 	{
9722 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9723 	    {
9724 	      *ppsection = NULL;
9725 	      continue;
9726 	    }
9727 	}
9728 
9729       if (isym->st_shndx == SHN_UNDEF)
9730 	isec = bfd_und_section_ptr;
9731       else if (isym->st_shndx == SHN_ABS)
9732 	isec = bfd_abs_section_ptr;
9733       else if (isym->st_shndx == SHN_COMMON)
9734 	isec = bfd_com_section_ptr;
9735       else
9736 	{
9737 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9738 	  if (isec == NULL)
9739 	    {
9740 	      /* Don't attempt to output symbols with st_shnx in the
9741 		 reserved range other than SHN_ABS and SHN_COMMON.  */
9742 	      *ppsection = NULL;
9743 	      continue;
9744 	    }
9745 	  else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9746 		   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9747 	    isym->st_value =
9748 	      _bfd_merged_section_offset (output_bfd, &isec,
9749 					  elf_section_data (isec)->sec_info,
9750 					  isym->st_value);
9751 	}
9752 
9753       *ppsection = isec;
9754 
9755       /* Don't output the first, undefined, symbol.  */
9756       if (ppsection == flinfo->sections)
9757 	continue;
9758 
9759       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9760 	{
9761 	  /* We never output section symbols.  Instead, we use the
9762 	     section symbol of the corresponding section in the output
9763 	     file.  */
9764 	  continue;
9765 	}
9766 
9767       /* If we are stripping all symbols, we don't want to output this
9768 	 one.  */
9769       if (flinfo->info->strip == strip_all)
9770 	continue;
9771 
9772       /* If we are discarding all local symbols, we don't want to
9773 	 output this one.  If we are generating a relocatable output
9774 	 file, then some of the local symbols may be required by
9775 	 relocs; we output them below as we discover that they are
9776 	 needed.  */
9777       if (flinfo->info->discard == discard_all)
9778 	continue;
9779 
9780       /* If this symbol is defined in a section which we are
9781 	 discarding, we don't need to keep it.  */
9782       if (isym->st_shndx != SHN_UNDEF
9783 	  && isym->st_shndx < SHN_LORESERVE
9784 	  && bfd_section_removed_from_list (output_bfd,
9785 					    isec->output_section))
9786 	continue;
9787 
9788       /* Get the name of the symbol.  */
9789       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9790 					      isym->st_name);
9791       if (name == NULL)
9792 	return FALSE;
9793 
9794       /* See if we are discarding symbols with this name.  */
9795       if ((flinfo->info->strip == strip_some
9796 	   && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9797 	       == NULL))
9798 	  || (((flinfo->info->discard == discard_sec_merge
9799 		&& (isec->flags & SEC_MERGE)
9800 		&& !bfd_link_relocatable (flinfo->info))
9801 	       || flinfo->info->discard == discard_l)
9802 	      && bfd_is_local_label_name (input_bfd, name)))
9803 	continue;
9804 
9805       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9806 	{
9807 	  if (input_bfd->lto_output)
9808 	    /* -flto puts a temp file name here.  This means builds
9809 	       are not reproducible.  Discard the symbol.  */
9810 	    continue;
9811 	  have_file_sym = TRUE;
9812 	  flinfo->filesym_count += 1;
9813 	}
9814       if (!have_file_sym)
9815 	{
9816 	  /* In the absence of debug info, bfd_find_nearest_line uses
9817 	     FILE symbols to determine the source file for local
9818 	     function symbols.  Provide a FILE symbol here if input
9819 	     files lack such, so that their symbols won't be
9820 	     associated with a previous input file.  It's not the
9821 	     source file, but the best we can do.  */
9822 	  have_file_sym = TRUE;
9823 	  flinfo->filesym_count += 1;
9824 	  memset (&osym, 0, sizeof (osym));
9825 	  osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9826 	  osym.st_shndx = SHN_ABS;
9827 	  if (!elf_link_output_symstrtab (flinfo,
9828 					  (input_bfd->lto_output ? NULL
9829 					   : input_bfd->filename),
9830 					  &osym, bfd_abs_section_ptr,
9831 					  NULL))
9832 	    return FALSE;
9833 	}
9834 
9835       osym = *isym;
9836 
9837       /* Adjust the section index for the output file.  */
9838       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9839 							 isec->output_section);
9840       if (osym.st_shndx == SHN_BAD)
9841 	return FALSE;
9842 
9843       /* ELF symbols in relocatable files are section relative, but
9844 	 in executable files they are virtual addresses.  Note that
9845 	 this code assumes that all ELF sections have an associated
9846 	 BFD section with a reasonable value for output_offset; below
9847 	 we assume that they also have a reasonable value for
9848 	 output_section.  Any special sections must be set up to meet
9849 	 these requirements.  */
9850       osym.st_value += isec->output_offset;
9851       if (!bfd_link_relocatable (flinfo->info))
9852 	{
9853 	  osym.st_value += isec->output_section->vma;
9854 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9855 	    {
9856 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
9857 	      BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9858 	      osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9859 	    }
9860 	}
9861 
9862       indx = bfd_get_symcount (output_bfd);
9863       ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9864       if (ret == 0)
9865 	return FALSE;
9866       else if (ret == 1)
9867 	*pindex = indx;
9868     }
9869 
9870   if (bed->s->arch_size == 32)
9871     {
9872       r_type_mask = 0xff;
9873       r_sym_shift = 8;
9874       address_size = 4;
9875     }
9876   else
9877     {
9878       r_type_mask = 0xffffffff;
9879       r_sym_shift = 32;
9880       address_size = 8;
9881     }
9882 
9883   /* Relocate the contents of each section.  */
9884   sym_hashes = elf_sym_hashes (input_bfd);
9885   for (o = input_bfd->sections; o != NULL; o = o->next)
9886     {
9887       bfd_byte *contents;
9888 
9889       if (! o->linker_mark)
9890 	{
9891 	  /* This section was omitted from the link.  */
9892 	  continue;
9893 	}
9894 
9895       if (bfd_link_relocatable (flinfo->info)
9896 	  && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9897 	{
9898 	  /* Deal with the group signature symbol.  */
9899 	  struct bfd_elf_section_data *sec_data = elf_section_data (o);
9900 	  unsigned long symndx = sec_data->this_hdr.sh_info;
9901 	  asection *osec = o->output_section;
9902 
9903 	  if (symndx >= locsymcount
9904 	      || (elf_bad_symtab (input_bfd)
9905 		  && flinfo->sections[symndx] == NULL))
9906 	    {
9907 	      struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9908 	      while (h->root.type == bfd_link_hash_indirect
9909 		     || h->root.type == bfd_link_hash_warning)
9910 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
9911 	      /* Arrange for symbol to be output.  */
9912 	      h->indx = -2;
9913 	      elf_section_data (osec)->this_hdr.sh_info = -2;
9914 	    }
9915 	  else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9916 	    {
9917 	      /* We'll use the output section target_index.  */
9918 	      asection *sec = flinfo->sections[symndx]->output_section;
9919 	      elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9920 	    }
9921 	  else
9922 	    {
9923 	      if (flinfo->indices[symndx] == -1)
9924 		{
9925 		  /* Otherwise output the local symbol now.  */
9926 		  Elf_Internal_Sym sym = isymbuf[symndx];
9927 		  asection *sec = flinfo->sections[symndx]->output_section;
9928 		  const char *name;
9929 		  long indx;
9930 		  int ret;
9931 
9932 		  name = bfd_elf_string_from_elf_section (input_bfd,
9933 							  symtab_hdr->sh_link,
9934 							  sym.st_name);
9935 		  if (name == NULL)
9936 		    return FALSE;
9937 
9938 		  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9939 								    sec);
9940 		  if (sym.st_shndx == SHN_BAD)
9941 		    return FALSE;
9942 
9943 		  sym.st_value += o->output_offset;
9944 
9945 		  indx = bfd_get_symcount (output_bfd);
9946 		  ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9947 						   NULL);
9948 		  if (ret == 0)
9949 		    return FALSE;
9950 		  else if (ret == 1)
9951 		    flinfo->indices[symndx] = indx;
9952 		  else
9953 		    abort ();
9954 		}
9955 	      elf_section_data (osec)->this_hdr.sh_info
9956 		= flinfo->indices[symndx];
9957 	    }
9958 	}
9959 
9960       if ((o->flags & SEC_HAS_CONTENTS) == 0
9961 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9962 	continue;
9963 
9964       if ((o->flags & SEC_LINKER_CREATED) != 0)
9965 	{
9966 	  /* Section was created by _bfd_elf_link_create_dynamic_sections
9967 	     or somesuch.  */
9968 	  continue;
9969 	}
9970 
9971       /* Get the contents of the section.  They have been cached by a
9972 	 relaxation routine.  Note that o is a section in an input
9973 	 file, so the contents field will not have been set by any of
9974 	 the routines which work on output files.  */
9975       if (elf_section_data (o)->this_hdr.contents != NULL)
9976 	{
9977 	  contents = elf_section_data (o)->this_hdr.contents;
9978 	  if (bed->caches_rawsize
9979 	      && o->rawsize != 0
9980 	      && o->rawsize < o->size)
9981 	    {
9982 	      memcpy (flinfo->contents, contents, o->rawsize);
9983 	      contents = flinfo->contents;
9984 	    }
9985 	}
9986       else
9987 	{
9988 	  contents = flinfo->contents;
9989 	  if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9990 	    return FALSE;
9991 	}
9992 
9993       if ((o->flags & SEC_RELOC) != 0)
9994 	{
9995 	  Elf_Internal_Rela *internal_relocs;
9996 	  Elf_Internal_Rela *rel, *relend;
9997 	  int action_discarded;
9998 	  int ret;
9999 
10000 	  /* Get the swapped relocs.  */
10001 	  internal_relocs
10002 	    = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10003 					 flinfo->internal_relocs, FALSE);
10004 	  if (internal_relocs == NULL
10005 	      && o->reloc_count > 0)
10006 	    return FALSE;
10007 
10008 	  /* We need to reverse-copy input .ctors/.dtors sections if
10009 	     they are placed in .init_array/.finit_array for output.  */
10010 	  if (o->size > address_size
10011 	      && ((strncmp (o->name, ".ctors", 6) == 0
10012 		   && strcmp (o->output_section->name,
10013 			      ".init_array") == 0)
10014 		  || (strncmp (o->name, ".dtors", 6) == 0
10015 		      && strcmp (o->output_section->name,
10016 				 ".fini_array") == 0))
10017 	      && (o->name[6] == 0 || o->name[6] == '.'))
10018 	    {
10019 	      if (o->size != o->reloc_count * address_size)
10020 		{
10021 		  (*_bfd_error_handler)
10022 		    (_("error: %B: size of section %A is not "
10023 		       "multiple of address size"),
10024 		     input_bfd, o);
10025 		  bfd_set_error (bfd_error_on_input);
10026 		  return FALSE;
10027 		}
10028 	      o->flags |= SEC_ELF_REVERSE_COPY;
10029 	    }
10030 
10031 	  action_discarded = -1;
10032 	  if (!elf_section_ignore_discarded_relocs (o))
10033 	    action_discarded = (*bed->action_discarded) (o);
10034 
10035 	  /* Run through the relocs evaluating complex reloc symbols and
10036 	     looking for relocs against symbols from discarded sections
10037 	     or section symbols from removed link-once sections.
10038 	     Complain about relocs against discarded sections.  Zero
10039 	     relocs against removed link-once sections.  */
10040 
10041 	  rel = internal_relocs;
10042 	  relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10043 	  for ( ; rel < relend; rel++)
10044 	    {
10045 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
10046 	      unsigned int s_type;
10047 	      asection **ps, *sec;
10048 	      struct elf_link_hash_entry *h = NULL;
10049 	      const char *sym_name;
10050 
10051 	      if (r_symndx == STN_UNDEF)
10052 		continue;
10053 
10054 	      if (r_symndx >= locsymcount
10055 		  || (elf_bad_symtab (input_bfd)
10056 		      && flinfo->sections[r_symndx] == NULL))
10057 		{
10058 		  h = sym_hashes[r_symndx - extsymoff];
10059 
10060 		  /* Badly formatted input files can contain relocs that
10061 		     reference non-existant symbols.  Check here so that
10062 		     we do not seg fault.  */
10063 		  if (h == NULL)
10064 		    {
10065 		      char buffer [32];
10066 
10067 		      sprintf_vma (buffer, rel->r_info);
10068 		      (*_bfd_error_handler)
10069 			(_("error: %B contains a reloc (0x%s) for section %A "
10070 			   "that references a non-existent global symbol"),
10071 			 input_bfd, o, buffer);
10072 		      bfd_set_error (bfd_error_bad_value);
10073 		      return FALSE;
10074 		    }
10075 
10076 		  while (h->root.type == bfd_link_hash_indirect
10077 			 || h->root.type == bfd_link_hash_warning)
10078 		    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10079 
10080 		  s_type = h->type;
10081 
10082 		  /* If a plugin symbol is referenced from a non-IR file,
10083 		     mark the symbol as undefined.  Note that the
10084 		     linker may attach linker created dynamic sections
10085 		     to the plugin bfd.  Symbols defined in linker
10086 		     created sections are not plugin symbols.  */
10087 		  if (h->root.non_ir_ref
10088 		      && (h->root.type == bfd_link_hash_defined
10089 			  || h->root.type == bfd_link_hash_defweak)
10090 		      && (h->root.u.def.section->flags
10091 			  & SEC_LINKER_CREATED) == 0
10092 		      && h->root.u.def.section->owner != NULL
10093 		      && (h->root.u.def.section->owner->flags
10094 			  & BFD_PLUGIN) != 0)
10095 		    {
10096 		      h->root.type = bfd_link_hash_undefined;
10097 		      h->root.u.undef.abfd = h->root.u.def.section->owner;
10098 		    }
10099 
10100 		  ps = NULL;
10101 		  if (h->root.type == bfd_link_hash_defined
10102 		      || h->root.type == bfd_link_hash_defweak)
10103 		    ps = &h->root.u.def.section;
10104 
10105 		  sym_name = h->root.root.string;
10106 		}
10107 	      else
10108 		{
10109 		  Elf_Internal_Sym *sym = isymbuf + r_symndx;
10110 
10111 		  s_type = ELF_ST_TYPE (sym->st_info);
10112 		  ps = &flinfo->sections[r_symndx];
10113 		  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10114 					       sym, *ps);
10115 		}
10116 
10117 	      if ((s_type == STT_RELC || s_type == STT_SRELC)
10118 		  && !bfd_link_relocatable (flinfo->info))
10119 		{
10120 		  bfd_vma val;
10121 		  bfd_vma dot = (rel->r_offset
10122 				 + o->output_offset + o->output_section->vma);
10123 #ifdef DEBUG
10124 		  printf ("Encountered a complex symbol!");
10125 		  printf (" (input_bfd %s, section %s, reloc %ld\n",
10126 			  input_bfd->filename, o->name,
10127 			  (long) (rel - internal_relocs));
10128 		  printf (" symbol: idx  %8.8lx, name %s\n",
10129 			  r_symndx, sym_name);
10130 		  printf (" reloc : info %8.8lx, addr %8.8lx\n",
10131 			  (unsigned long) rel->r_info,
10132 			  (unsigned long) rel->r_offset);
10133 #endif
10134 		  if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10135 				    isymbuf, locsymcount, s_type == STT_SRELC))
10136 		    return FALSE;
10137 
10138 		  /* Symbol evaluated OK.  Update to absolute value.  */
10139 		  set_symbol_value (input_bfd, isymbuf, locsymcount,
10140 				    r_symndx, val);
10141 		  continue;
10142 		}
10143 
10144 	      if (action_discarded != -1 && ps != NULL)
10145 		{
10146 		  /* Complain if the definition comes from a
10147 		     discarded section.  */
10148 		  if ((sec = *ps) != NULL && discarded_section (sec))
10149 		    {
10150 		      BFD_ASSERT (r_symndx != STN_UNDEF);
10151 		      if (action_discarded & COMPLAIN)
10152 			(*flinfo->info->callbacks->einfo)
10153 			  (_("%X`%s' referenced in section `%A' of %B: "
10154 			     "defined in discarded section `%A' of %B\n"),
10155 			   sym_name, o, input_bfd, sec, sec->owner);
10156 
10157 		      /* Try to do the best we can to support buggy old
10158 			 versions of gcc.  Pretend that the symbol is
10159 			 really defined in the kept linkonce section.
10160 			 FIXME: This is quite broken.  Modifying the
10161 			 symbol here means we will be changing all later
10162 			 uses of the symbol, not just in this section.  */
10163 		      if (action_discarded & PRETEND)
10164 			{
10165 			  asection *kept;
10166 
10167 			  kept = _bfd_elf_check_kept_section (sec,
10168 							      flinfo->info);
10169 			  if (kept != NULL)
10170 			    {
10171 			      *ps = kept;
10172 			      continue;
10173 			    }
10174 			}
10175 		    }
10176 		}
10177 	    }
10178 
10179 	  /* Relocate the section by invoking a back end routine.
10180 
10181 	     The back end routine is responsible for adjusting the
10182 	     section contents as necessary, and (if using Rela relocs
10183 	     and generating a relocatable output file) adjusting the
10184 	     reloc addend as necessary.
10185 
10186 	     The back end routine does not have to worry about setting
10187 	     the reloc address or the reloc symbol index.
10188 
10189 	     The back end routine is given a pointer to the swapped in
10190 	     internal symbols, and can access the hash table entries
10191 	     for the external symbols via elf_sym_hashes (input_bfd).
10192 
10193 	     When generating relocatable output, the back end routine
10194 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
10195 	     output symbol is going to be a section symbol
10196 	     corresponding to the output section, which will require
10197 	     the addend to be adjusted.  */
10198 
10199 	  ret = (*relocate_section) (output_bfd, flinfo->info,
10200 				     input_bfd, o, contents,
10201 				     internal_relocs,
10202 				     isymbuf,
10203 				     flinfo->sections);
10204 	  if (!ret)
10205 	    return FALSE;
10206 
10207 	  if (ret == 2
10208 	      || bfd_link_relocatable (flinfo->info)
10209 	      || flinfo->info->emitrelocations)
10210 	    {
10211 	      Elf_Internal_Rela *irela;
10212 	      Elf_Internal_Rela *irelaend, *irelamid;
10213 	      bfd_vma last_offset;
10214 	      struct elf_link_hash_entry **rel_hash;
10215 	      struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10216 	      Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10217 	      unsigned int next_erel;
10218 	      bfd_boolean rela_normal;
10219 	      struct bfd_elf_section_data *esdi, *esdo;
10220 
10221 	      esdi = elf_section_data (o);
10222 	      esdo = elf_section_data (o->output_section);
10223 	      rela_normal = FALSE;
10224 
10225 	      /* Adjust the reloc addresses and symbol indices.  */
10226 
10227 	      irela = internal_relocs;
10228 	      irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10229 	      rel_hash = esdo->rel.hashes + esdo->rel.count;
10230 	      /* We start processing the REL relocs, if any.  When we reach
10231 		 IRELAMID in the loop, we switch to the RELA relocs.  */
10232 	      irelamid = irela;
10233 	      if (esdi->rel.hdr != NULL)
10234 		irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10235 			     * bed->s->int_rels_per_ext_rel);
10236 	      rel_hash_list = rel_hash;
10237 	      rela_hash_list = NULL;
10238 	      last_offset = o->output_offset;
10239 	      if (!bfd_link_relocatable (flinfo->info))
10240 		last_offset += o->output_section->vma;
10241 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10242 		{
10243 		  unsigned long r_symndx;
10244 		  asection *sec;
10245 		  Elf_Internal_Sym sym;
10246 
10247 		  if (next_erel == bed->s->int_rels_per_ext_rel)
10248 		    {
10249 		      rel_hash++;
10250 		      next_erel = 0;
10251 		    }
10252 
10253 		  if (irela == irelamid)
10254 		    {
10255 		      rel_hash = esdo->rela.hashes + esdo->rela.count;
10256 		      rela_hash_list = rel_hash;
10257 		      rela_normal = bed->rela_normal;
10258 		    }
10259 
10260 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
10261 							     flinfo->info, o,
10262 							     irela->r_offset);
10263 		  if (irela->r_offset >= (bfd_vma) -2)
10264 		    {
10265 		      /* This is a reloc for a deleted entry or somesuch.
10266 			 Turn it into an R_*_NONE reloc, at the same
10267 			 offset as the last reloc.  elf_eh_frame.c and
10268 			 bfd_elf_discard_info rely on reloc offsets
10269 			 being ordered.  */
10270 		      irela->r_offset = last_offset;
10271 		      irela->r_info = 0;
10272 		      irela->r_addend = 0;
10273 		      continue;
10274 		    }
10275 
10276 		  irela->r_offset += o->output_offset;
10277 
10278 		  /* Relocs in an executable have to be virtual addresses.  */
10279 		  if (!bfd_link_relocatable (flinfo->info))
10280 		    irela->r_offset += o->output_section->vma;
10281 
10282 		  last_offset = irela->r_offset;
10283 
10284 		  r_symndx = irela->r_info >> r_sym_shift;
10285 		  if (r_symndx == STN_UNDEF)
10286 		    continue;
10287 
10288 		  if (r_symndx >= locsymcount
10289 		      || (elf_bad_symtab (input_bfd)
10290 			  && flinfo->sections[r_symndx] == NULL))
10291 		    {
10292 		      struct elf_link_hash_entry *rh;
10293 		      unsigned long indx;
10294 
10295 		      /* This is a reloc against a global symbol.  We
10296 			 have not yet output all the local symbols, so
10297 			 we do not know the symbol index of any global
10298 			 symbol.  We set the rel_hash entry for this
10299 			 reloc to point to the global hash table entry
10300 			 for this symbol.  The symbol index is then
10301 			 set at the end of bfd_elf_final_link.  */
10302 		      indx = r_symndx - extsymoff;
10303 		      rh = elf_sym_hashes (input_bfd)[indx];
10304 		      while (rh->root.type == bfd_link_hash_indirect
10305 			     || rh->root.type == bfd_link_hash_warning)
10306 			rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10307 
10308 		      /* Setting the index to -2 tells
10309 			 elf_link_output_extsym that this symbol is
10310 			 used by a reloc.  */
10311 		      BFD_ASSERT (rh->indx < 0);
10312 		      rh->indx = -2;
10313 
10314 		      *rel_hash = rh;
10315 
10316 		      continue;
10317 		    }
10318 
10319 		  /* This is a reloc against a local symbol.  */
10320 
10321 		  *rel_hash = NULL;
10322 		  sym = isymbuf[r_symndx];
10323 		  sec = flinfo->sections[r_symndx];
10324 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10325 		    {
10326 		      /* I suppose the backend ought to fill in the
10327 			 section of any STT_SECTION symbol against a
10328 			 processor specific section.  */
10329 		      r_symndx = STN_UNDEF;
10330 		      if (bfd_is_abs_section (sec))
10331 			;
10332 		      else if (sec == NULL || sec->owner == NULL)
10333 			{
10334 			  bfd_set_error (bfd_error_bad_value);
10335 			  return FALSE;
10336 			}
10337 		      else
10338 			{
10339 			  asection *osec = sec->output_section;
10340 
10341 			  /* If we have discarded a section, the output
10342 			     section will be the absolute section.  In
10343 			     case of discarded SEC_MERGE sections, use
10344 			     the kept section.  relocate_section should
10345 			     have already handled discarded linkonce
10346 			     sections.  */
10347 			  if (bfd_is_abs_section (osec)
10348 			      && sec->kept_section != NULL
10349 			      && sec->kept_section->output_section != NULL)
10350 			    {
10351 			      osec = sec->kept_section->output_section;
10352 			      irela->r_addend -= osec->vma;
10353 			    }
10354 
10355 			  if (!bfd_is_abs_section (osec))
10356 			    {
10357 			      r_symndx = osec->target_index;
10358 			      if (r_symndx == STN_UNDEF)
10359 				{
10360 				  irela->r_addend += osec->vma;
10361 				  osec = _bfd_nearby_section (output_bfd, osec,
10362 							      osec->vma);
10363 				  irela->r_addend -= osec->vma;
10364 				  r_symndx = osec->target_index;
10365 				}
10366 			    }
10367 			}
10368 
10369 		      /* Adjust the addend according to where the
10370 			 section winds up in the output section.  */
10371 		      if (rela_normal)
10372 			irela->r_addend += sec->output_offset;
10373 		    }
10374 		  else
10375 		    {
10376 		      if (flinfo->indices[r_symndx] == -1)
10377 			{
10378 			  unsigned long shlink;
10379 			  const char *name;
10380 			  asection *osec;
10381 			  long indx;
10382 
10383 			  if (flinfo->info->strip == strip_all)
10384 			    {
10385 			      /* You can't do ld -r -s.  */
10386 			      bfd_set_error (bfd_error_invalid_operation);
10387 			      return FALSE;
10388 			    }
10389 
10390 			  /* This symbol was skipped earlier, but
10391 			     since it is needed by a reloc, we
10392 			     must output it now.  */
10393 			  shlink = symtab_hdr->sh_link;
10394 			  name = (bfd_elf_string_from_elf_section
10395 				  (input_bfd, shlink, sym.st_name));
10396 			  if (name == NULL)
10397 			    return FALSE;
10398 
10399 			  osec = sec->output_section;
10400 			  sym.st_shndx =
10401 			    _bfd_elf_section_from_bfd_section (output_bfd,
10402 							       osec);
10403 			  if (sym.st_shndx == SHN_BAD)
10404 			    return FALSE;
10405 
10406 			  sym.st_value += sec->output_offset;
10407 			  if (!bfd_link_relocatable (flinfo->info))
10408 			    {
10409 			      sym.st_value += osec->vma;
10410 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10411 				{
10412 				  /* STT_TLS symbols are relative to PT_TLS
10413 				     segment base.  */
10414 				  BFD_ASSERT (elf_hash_table (flinfo->info)
10415 					      ->tls_sec != NULL);
10416 				  sym.st_value -= (elf_hash_table (flinfo->info)
10417 						   ->tls_sec->vma);
10418 				}
10419 			    }
10420 
10421 			  indx = bfd_get_symcount (output_bfd);
10422 			  ret = elf_link_output_symstrtab (flinfo, name,
10423 							   &sym, sec,
10424 							   NULL);
10425 			  if (ret == 0)
10426 			    return FALSE;
10427 			  else if (ret == 1)
10428 			    flinfo->indices[r_symndx] = indx;
10429 			  else
10430 			    abort ();
10431 			}
10432 
10433 		      r_symndx = flinfo->indices[r_symndx];
10434 		    }
10435 
10436 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10437 				   | (irela->r_info & r_type_mask));
10438 		}
10439 
10440 	      /* Swap out the relocs.  */
10441 	      input_rel_hdr = esdi->rel.hdr;
10442 	      if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10443 		{
10444 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
10445 						     input_rel_hdr,
10446 						     internal_relocs,
10447 						     rel_hash_list))
10448 		    return FALSE;
10449 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10450 				      * bed->s->int_rels_per_ext_rel);
10451 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10452 		}
10453 
10454 	      input_rela_hdr = esdi->rela.hdr;
10455 	      if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10456 		{
10457 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
10458 						     input_rela_hdr,
10459 						     internal_relocs,
10460 						     rela_hash_list))
10461 		    return FALSE;
10462 		}
10463 	    }
10464 	}
10465 
10466       /* Write out the modified section contents.  */
10467       if (bed->elf_backend_write_section
10468 	  && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10469 						contents))
10470 	{
10471 	  /* Section written out.  */
10472 	}
10473       else switch (o->sec_info_type)
10474 	{
10475 	case SEC_INFO_TYPE_STABS:
10476 	  if (! (_bfd_write_section_stabs
10477 		 (output_bfd,
10478 		  &elf_hash_table (flinfo->info)->stab_info,
10479 		  o, &elf_section_data (o)->sec_info, contents)))
10480 	    return FALSE;
10481 	  break;
10482 	case SEC_INFO_TYPE_MERGE:
10483 	  if (! _bfd_write_merged_section (output_bfd, o,
10484 					   elf_section_data (o)->sec_info))
10485 	    return FALSE;
10486 	  break;
10487 	case SEC_INFO_TYPE_EH_FRAME:
10488 	  {
10489 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10490 						   o, contents))
10491 	      return FALSE;
10492 	  }
10493 	  break;
10494 	case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10495 	  {
10496 	    if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10497 							 flinfo->info,
10498 							 o, contents))
10499 	      return FALSE;
10500 	  }
10501 	  break;
10502 	default:
10503 	  {
10504 	    /* FIXME: octets_per_byte.  */
10505 	    if (! (o->flags & SEC_EXCLUDE))
10506 	      {
10507 		file_ptr offset = (file_ptr) o->output_offset;
10508 		bfd_size_type todo = o->size;
10509 		if ((o->flags & SEC_ELF_REVERSE_COPY))
10510 		  {
10511 		    /* Reverse-copy input section to output.  */
10512 		    do
10513 		      {
10514 			todo -= address_size;
10515 			if (! bfd_set_section_contents (output_bfd,
10516 							o->output_section,
10517 							contents + todo,
10518 							offset,
10519 							address_size))
10520 			  return FALSE;
10521 			if (todo == 0)
10522 			  break;
10523 			offset += address_size;
10524 		      }
10525 		    while (1);
10526 		  }
10527 		else if (! bfd_set_section_contents (output_bfd,
10528 						     o->output_section,
10529 						     contents,
10530 						     offset, todo))
10531 		  return FALSE;
10532 	      }
10533 	  }
10534 	  break;
10535 	}
10536     }
10537 
10538   return TRUE;
10539 }
10540 
10541 /* Generate a reloc when linking an ELF file.  This is a reloc
10542    requested by the linker, and does not come from any input file.  This
10543    is used to build constructor and destructor tables when linking
10544    with -Ur.  */
10545 
10546 static bfd_boolean
10547 elf_reloc_link_order (bfd *output_bfd,
10548 		      struct bfd_link_info *info,
10549 		      asection *output_section,
10550 		      struct bfd_link_order *link_order)
10551 {
10552   reloc_howto_type *howto;
10553   long indx;
10554   bfd_vma offset;
10555   bfd_vma addend;
10556   struct bfd_elf_section_reloc_data *reldata;
10557   struct elf_link_hash_entry **rel_hash_ptr;
10558   Elf_Internal_Shdr *rel_hdr;
10559   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10560   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10561   bfd_byte *erel;
10562   unsigned int i;
10563   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10564 
10565   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10566   if (howto == NULL)
10567     {
10568       bfd_set_error (bfd_error_bad_value);
10569       return FALSE;
10570     }
10571 
10572   addend = link_order->u.reloc.p->addend;
10573 
10574   if (esdo->rel.hdr)
10575     reldata = &esdo->rel;
10576   else if (esdo->rela.hdr)
10577     reldata = &esdo->rela;
10578   else
10579     {
10580       reldata = NULL;
10581       BFD_ASSERT (0);
10582     }
10583 
10584   /* Figure out the symbol index.  */
10585   rel_hash_ptr = reldata->hashes + reldata->count;
10586   if (link_order->type == bfd_section_reloc_link_order)
10587     {
10588       indx = link_order->u.reloc.p->u.section->target_index;
10589       BFD_ASSERT (indx != 0);
10590       *rel_hash_ptr = NULL;
10591     }
10592   else
10593     {
10594       struct elf_link_hash_entry *h;
10595 
10596       /* Treat a reloc against a defined symbol as though it were
10597 	 actually against the section.  */
10598       h = ((struct elf_link_hash_entry *)
10599 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
10600 					 link_order->u.reloc.p->u.name,
10601 					 FALSE, FALSE, TRUE));
10602       if (h != NULL
10603 	  && (h->root.type == bfd_link_hash_defined
10604 	      || h->root.type == bfd_link_hash_defweak))
10605 	{
10606 	  asection *section;
10607 
10608 	  section = h->root.u.def.section;
10609 	  indx = section->output_section->target_index;
10610 	  *rel_hash_ptr = NULL;
10611 	  /* It seems that we ought to add the symbol value to the
10612 	     addend here, but in practice it has already been added
10613 	     because it was passed to constructor_callback.  */
10614 	  addend += section->output_section->vma + section->output_offset;
10615 	}
10616       else if (h != NULL)
10617 	{
10618 	  /* Setting the index to -2 tells elf_link_output_extsym that
10619 	     this symbol is used by a reloc.  */
10620 	  h->indx = -2;
10621 	  *rel_hash_ptr = h;
10622 	  indx = 0;
10623 	}
10624       else
10625 	{
10626 	  if (! ((*info->callbacks->unattached_reloc)
10627 		 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10628 	    return FALSE;
10629 	  indx = 0;
10630 	}
10631     }
10632 
10633   /* If this is an inplace reloc, we must write the addend into the
10634      object file.  */
10635   if (howto->partial_inplace && addend != 0)
10636     {
10637       bfd_size_type size;
10638       bfd_reloc_status_type rstat;
10639       bfd_byte *buf;
10640       bfd_boolean ok;
10641       const char *sym_name;
10642 
10643       size = (bfd_size_type) bfd_get_reloc_size (howto);
10644       buf = (bfd_byte *) bfd_zmalloc (size);
10645       if (buf == NULL && size != 0)
10646 	return FALSE;
10647       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10648       switch (rstat)
10649 	{
10650 	case bfd_reloc_ok:
10651 	  break;
10652 
10653 	default:
10654 	case bfd_reloc_outofrange:
10655 	  abort ();
10656 
10657 	case bfd_reloc_overflow:
10658 	  if (link_order->type == bfd_section_reloc_link_order)
10659 	    sym_name = bfd_section_name (output_bfd,
10660 					 link_order->u.reloc.p->u.section);
10661 	  else
10662 	    sym_name = link_order->u.reloc.p->u.name;
10663 	  if (! ((*info->callbacks->reloc_overflow)
10664 		 (info, NULL, sym_name, howto->name, addend, NULL,
10665 		  NULL, (bfd_vma) 0)))
10666 	    {
10667 	      free (buf);
10668 	      return FALSE;
10669 	    }
10670 	  break;
10671 	}
10672       ok = bfd_set_section_contents (output_bfd, output_section, buf,
10673 				     link_order->offset, size);
10674       free (buf);
10675       if (! ok)
10676 	return FALSE;
10677     }
10678 
10679   /* The address of a reloc is relative to the section in a
10680      relocatable file, and is a virtual address in an executable
10681      file.  */
10682   offset = link_order->offset;
10683   if (! bfd_link_relocatable (info))
10684     offset += output_section->vma;
10685 
10686   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10687     {
10688       irel[i].r_offset = offset;
10689       irel[i].r_info = 0;
10690       irel[i].r_addend = 0;
10691     }
10692   if (bed->s->arch_size == 32)
10693     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10694   else
10695 #ifdef BFD64
10696           {
10697             bfd_uint64_t indx64 = indx;
10698             irel[0].r_info = ELF64_R_INFO (indx64, howto->type);
10699           }
10700 #else
10701           BFD_FAIL();
10702 #endif
10703 
10704   rel_hdr = reldata->hdr;
10705   erel = rel_hdr->contents;
10706   if (rel_hdr->sh_type == SHT_REL)
10707     {
10708       erel += reldata->count * bed->s->sizeof_rel;
10709       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10710     }
10711   else
10712     {
10713       irel[0].r_addend = addend;
10714       erel += reldata->count * bed->s->sizeof_rela;
10715       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10716     }
10717 
10718   ++reldata->count;
10719 
10720   return TRUE;
10721 }
10722 
10723 
10724 /* Get the output vma of the section pointed to by the sh_link field.  */
10725 
10726 static bfd_vma
10727 elf_get_linked_section_vma (struct bfd_link_order *p)
10728 {
10729   Elf_Internal_Shdr **elf_shdrp;
10730   asection *s;
10731   int elfsec;
10732 
10733   s = p->u.indirect.section;
10734   elf_shdrp = elf_elfsections (s->owner);
10735   elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10736   elfsec = elf_shdrp[elfsec]->sh_link;
10737   /* PR 290:
10738      The Intel C compiler generates SHT_IA_64_UNWIND with
10739      SHF_LINK_ORDER.  But it doesn't set the sh_link or
10740      sh_info fields.  Hence we could get the situation
10741      where elfsec is 0.  */
10742   if (elfsec == 0)
10743     {
10744       const struct elf_backend_data *bed
10745 	= get_elf_backend_data (s->owner);
10746       if (bed->link_order_error_handler)
10747 	bed->link_order_error_handler
10748 	  (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10749       return 0;
10750     }
10751   else
10752     {
10753       s = elf_shdrp[elfsec]->bfd_section;
10754       return s->output_section->vma + s->output_offset;
10755     }
10756 }
10757 
10758 
10759 /* Compare two sections based on the locations of the sections they are
10760    linked to.  Used by elf_fixup_link_order.  */
10761 
10762 static int
10763 compare_link_order (const void * a, const void * b)
10764 {
10765   bfd_vma apos;
10766   bfd_vma bpos;
10767 
10768   apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10769   bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10770   if (apos < bpos)
10771     return -1;
10772   return apos > bpos;
10773 }
10774 
10775 
10776 /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
10777    order as their linked sections.  Returns false if this could not be done
10778    because an output section includes both ordered and unordered
10779    sections.  Ideally we'd do this in the linker proper.  */
10780 
10781 static bfd_boolean
10782 elf_fixup_link_order (bfd *abfd, asection *o)
10783 {
10784   int seen_linkorder;
10785   int seen_other;
10786   int n;
10787   struct bfd_link_order *p;
10788   bfd *sub;
10789   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10790   unsigned elfsec;
10791   struct bfd_link_order **sections;
10792   asection *s, *other_sec, *linkorder_sec;
10793   bfd_vma offset;
10794 
10795   other_sec = NULL;
10796   linkorder_sec = NULL;
10797   seen_other = 0;
10798   seen_linkorder = 0;
10799   for (p = o->map_head.link_order; p != NULL; p = p->next)
10800     {
10801       if (p->type == bfd_indirect_link_order)
10802 	{
10803 	  s = p->u.indirect.section;
10804 	  sub = s->owner;
10805 	  if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10806 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10807 	      && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10808 	      && elfsec < elf_numsections (sub)
10809 	      && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10810 	      && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10811 	    {
10812 	      seen_linkorder++;
10813 	      linkorder_sec = s;
10814 	    }
10815 	  else
10816 	    {
10817 	      seen_other++;
10818 	      other_sec = s;
10819 	    }
10820 	}
10821       else
10822 	seen_other++;
10823 
10824       if (seen_other && seen_linkorder)
10825 	{
10826 	  if (other_sec && linkorder_sec)
10827 	    (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10828 				   o, linkorder_sec,
10829 				   linkorder_sec->owner, other_sec,
10830 				   other_sec->owner);
10831 	  else
10832 	    (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10833 				   o);
10834 	  bfd_set_error (bfd_error_bad_value);
10835 	  return FALSE;
10836 	}
10837     }
10838 
10839   if (!seen_linkorder)
10840     return TRUE;
10841 
10842   sections = (struct bfd_link_order **)
10843     bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10844   if (sections == NULL)
10845     return FALSE;
10846   seen_linkorder = 0;
10847 
10848   for (p = o->map_head.link_order; p != NULL; p = p->next)
10849     {
10850       sections[seen_linkorder++] = p;
10851     }
10852   /* Sort the input sections in the order of their linked section.  */
10853   qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10854 	 compare_link_order);
10855 
10856   /* Change the offsets of the sections.  */
10857   offset = 0;
10858   for (n = 0; n < seen_linkorder; n++)
10859     {
10860       s = sections[n]->u.indirect.section;
10861       offset &= ~(bfd_vma) 0 << s->alignment_power;
10862       s->output_offset = offset;
10863       sections[n]->offset = offset;
10864       /* FIXME: octets_per_byte.  */
10865       offset += sections[n]->size;
10866     }
10867 
10868   free (sections);
10869   return TRUE;
10870 }
10871 
10872 static void
10873 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10874 {
10875   asection *o;
10876 
10877   if (flinfo->symstrtab != NULL)
10878     _bfd_elf_strtab_free (flinfo->symstrtab);
10879   if (flinfo->contents != NULL)
10880     free (flinfo->contents);
10881   if (flinfo->external_relocs != NULL)
10882     free (flinfo->external_relocs);
10883   if (flinfo->internal_relocs != NULL)
10884     free (flinfo->internal_relocs);
10885   if (flinfo->external_syms != NULL)
10886     free (flinfo->external_syms);
10887   if (flinfo->locsym_shndx != NULL)
10888     free (flinfo->locsym_shndx);
10889   if (flinfo->internal_syms != NULL)
10890     free (flinfo->internal_syms);
10891   if (flinfo->indices != NULL)
10892     free (flinfo->indices);
10893   if (flinfo->sections != NULL)
10894     free (flinfo->sections);
10895   if (flinfo->symshndxbuf != NULL)
10896     free (flinfo->symshndxbuf);
10897   for (o = obfd->sections; o != NULL; o = o->next)
10898     {
10899       struct bfd_elf_section_data *esdo = elf_section_data (o);
10900       if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10901 	free (esdo->rel.hashes);
10902       if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10903 	free (esdo->rela.hashes);
10904     }
10905 }
10906 
10907 /* Do the final step of an ELF link.  */
10908 
10909 bfd_boolean
10910 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10911 {
10912   bfd_boolean dynamic;
10913   bfd_boolean emit_relocs;
10914   bfd *dynobj;
10915   struct elf_final_link_info flinfo;
10916   asection *o;
10917   struct bfd_link_order *p;
10918   bfd *sub;
10919   bfd_size_type max_contents_size;
10920   bfd_size_type max_external_reloc_size;
10921   bfd_size_type max_internal_reloc_count;
10922   bfd_size_type max_sym_count;
10923   bfd_size_type max_sym_shndx_count;
10924   Elf_Internal_Sym elfsym;
10925   unsigned int i;
10926   Elf_Internal_Shdr *symtab_hdr;
10927   Elf_Internal_Shdr *symtab_shndx_hdr;
10928   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10929   struct elf_outext_info eoinfo;
10930   bfd_boolean merged;
10931   size_t relativecount = 0;
10932   asection *reldyn = 0;
10933   bfd_size_type amt;
10934   asection *attr_section = NULL;
10935   bfd_vma attr_size = 0;
10936   const char *std_attrs_section;
10937 
10938   if (! is_elf_hash_table (info->hash))
10939     return FALSE;
10940 
10941   if (bfd_link_pic (info))
10942     abfd->flags |= DYNAMIC;
10943 
10944   dynamic = elf_hash_table (info)->dynamic_sections_created;
10945   dynobj = elf_hash_table (info)->dynobj;
10946 
10947   emit_relocs = (bfd_link_relocatable (info)
10948 		 || info->emitrelocations);
10949 
10950   flinfo.info = info;
10951   flinfo.output_bfd = abfd;
10952   flinfo.symstrtab = _bfd_elf_strtab_init ();
10953   if (flinfo.symstrtab == NULL)
10954     return FALSE;
10955 
10956   if (! dynamic)
10957     {
10958       flinfo.hash_sec = NULL;
10959       flinfo.symver_sec = NULL;
10960     }
10961   else
10962     {
10963       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10964       /* Note that dynsym_sec can be NULL (on VMS).  */
10965       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10966       /* Note that it is OK if symver_sec is NULL.  */
10967     }
10968 
10969   flinfo.contents = NULL;
10970   flinfo.external_relocs = NULL;
10971   flinfo.internal_relocs = NULL;
10972   flinfo.external_syms = NULL;
10973   flinfo.locsym_shndx = NULL;
10974   flinfo.internal_syms = NULL;
10975   flinfo.indices = NULL;
10976   flinfo.sections = NULL;
10977   flinfo.symshndxbuf = NULL;
10978   flinfo.filesym_count = 0;
10979 
10980   /* The object attributes have been merged.  Remove the input
10981      sections from the link, and set the contents of the output
10982      secton.  */
10983   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10984   for (o = abfd->sections; o != NULL; o = o->next)
10985     {
10986       if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10987 	  || strcmp (o->name, ".gnu.attributes") == 0)
10988 	{
10989 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
10990 	    {
10991 	      asection *input_section;
10992 
10993 	      if (p->type != bfd_indirect_link_order)
10994 		continue;
10995 	      input_section = p->u.indirect.section;
10996 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
10997 		 elf_link_input_bfd ignores this section.  */
10998 	      input_section->flags &= ~SEC_HAS_CONTENTS;
10999 	    }
11000 
11001 	  attr_size = bfd_elf_obj_attr_size (abfd);
11002 	  if (attr_size)
11003 	    {
11004 	      bfd_set_section_size (abfd, o, attr_size);
11005 	      attr_section = o;
11006 	      /* Skip this section later on.  */
11007 	      o->map_head.link_order = NULL;
11008 	    }
11009 	  else
11010 	    o->flags |= SEC_EXCLUDE;
11011 	}
11012     }
11013 
11014   /* Count up the number of relocations we will output for each output
11015      section, so that we know the sizes of the reloc sections.  We
11016      also figure out some maximum sizes.  */
11017   max_contents_size = 0;
11018   max_external_reloc_size = 0;
11019   max_internal_reloc_count = 0;
11020   max_sym_count = 0;
11021   max_sym_shndx_count = 0;
11022   merged = FALSE;
11023   for (o = abfd->sections; o != NULL; o = o->next)
11024     {
11025       struct bfd_elf_section_data *esdo = elf_section_data (o);
11026       o->reloc_count = 0;
11027 
11028       for (p = o->map_head.link_order; p != NULL; p = p->next)
11029 	{
11030 	  unsigned int reloc_count = 0;
11031 	  struct bfd_elf_section_data *esdi = NULL;
11032 
11033 	  if (p->type == bfd_section_reloc_link_order
11034 	      || p->type == bfd_symbol_reloc_link_order)
11035 	    reloc_count = 1;
11036 	  else if (p->type == bfd_indirect_link_order)
11037 	    {
11038 	      asection *sec;
11039 
11040 	      sec = p->u.indirect.section;
11041 	      esdi = elf_section_data (sec);
11042 
11043 	      /* Mark all sections which are to be included in the
11044 		 link.  This will normally be every section.  We need
11045 		 to do this so that we can identify any sections which
11046 		 the linker has decided to not include.  */
11047 	      sec->linker_mark = TRUE;
11048 
11049 	      if (sec->flags & SEC_MERGE)
11050 		merged = TRUE;
11051 
11052 	      if (esdo->this_hdr.sh_type == SHT_REL
11053 		  || esdo->this_hdr.sh_type == SHT_RELA)
11054 		/* Some backends use reloc_count in relocation sections
11055 		   to count particular types of relocs.  Of course,
11056 		   reloc sections themselves can't have relocations.  */
11057 		reloc_count = 0;
11058 	      else if (emit_relocs)
11059 		reloc_count = sec->reloc_count;
11060 	      else if (bed->elf_backend_count_relocs)
11061 		reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11062 
11063 	      if (sec->rawsize > max_contents_size)
11064 		max_contents_size = sec->rawsize;
11065 	      if (sec->size > max_contents_size)
11066 		max_contents_size = sec->size;
11067 
11068 	      /* We are interested in just local symbols, not all
11069 		 symbols.  */
11070 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11071 		  && (sec->owner->flags & DYNAMIC) == 0)
11072 		{
11073 		  size_t sym_count;
11074 
11075 		  if (elf_bad_symtab (sec->owner))
11076 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11077 				 / bed->s->sizeof_sym);
11078 		  else
11079 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11080 
11081 		  if (sym_count > max_sym_count)
11082 		    max_sym_count = sym_count;
11083 
11084 		  if (sym_count > max_sym_shndx_count
11085 		      && elf_symtab_shndx_list (sec->owner) != NULL)
11086 		    max_sym_shndx_count = sym_count;
11087 
11088 		  if ((sec->flags & SEC_RELOC) != 0)
11089 		    {
11090 		      size_t ext_size = 0;
11091 
11092 		      if (esdi->rel.hdr != NULL)
11093 			ext_size = esdi->rel.hdr->sh_size;
11094 		      if (esdi->rela.hdr != NULL)
11095 			ext_size += esdi->rela.hdr->sh_size;
11096 
11097 		      if (ext_size > max_external_reloc_size)
11098 			max_external_reloc_size = ext_size;
11099 		      if (sec->reloc_count > max_internal_reloc_count)
11100 			max_internal_reloc_count = sec->reloc_count;
11101 		    }
11102 		}
11103 	    }
11104 
11105 	  if (reloc_count == 0)
11106 	    continue;
11107 
11108 	  o->reloc_count += reloc_count;
11109 
11110 	  if (p->type == bfd_indirect_link_order && emit_relocs)
11111 	    {
11112 	      if (esdi->rel.hdr)
11113 		esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11114 	      if (esdi->rela.hdr)
11115 		esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11116 	    }
11117 	  else
11118 	    {
11119 	      if (o->use_rela_p)
11120 		esdo->rela.count += reloc_count;
11121 	      else
11122 		esdo->rel.count += reloc_count;
11123 	    }
11124 	}
11125 
11126       if (o->reloc_count > 0)
11127 	o->flags |= SEC_RELOC;
11128       else
11129 	{
11130 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
11131 	     set it (this is probably a bug) and if it is set
11132 	     assign_section_numbers will create a reloc section.  */
11133 	  o->flags &=~ SEC_RELOC;
11134 	}
11135 
11136       /* If the SEC_ALLOC flag is not set, force the section VMA to
11137 	 zero.  This is done in elf_fake_sections as well, but forcing
11138 	 the VMA to 0 here will ensure that relocs against these
11139 	 sections are handled correctly.  */
11140       if ((o->flags & SEC_ALLOC) == 0
11141 	  && ! o->user_set_vma)
11142 	o->vma = 0;
11143     }
11144 
11145   if (! bfd_link_relocatable (info) && merged)
11146     elf_link_hash_traverse (elf_hash_table (info),
11147 			    _bfd_elf_link_sec_merge_syms, abfd);
11148 
11149   /* Figure out the file positions for everything but the symbol table
11150      and the relocs.  We set symcount to force assign_section_numbers
11151      to create a symbol table.  */
11152   bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11153   BFD_ASSERT (! abfd->output_has_begun);
11154   if (! _bfd_elf_compute_section_file_positions (abfd, info))
11155     goto error_return;
11156 
11157   /* Set sizes, and assign file positions for reloc sections.  */
11158   for (o = abfd->sections; o != NULL; o = o->next)
11159     {
11160       struct bfd_elf_section_data *esdo = elf_section_data (o);
11161       if ((o->flags & SEC_RELOC) != 0)
11162 	{
11163 	  if (esdo->rel.hdr
11164 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11165 	    goto error_return;
11166 
11167 	  if (esdo->rela.hdr
11168 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11169 	    goto error_return;
11170 	}
11171 
11172       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11173 	 to count upwards while actually outputting the relocations.  */
11174       esdo->rel.count = 0;
11175       esdo->rela.count = 0;
11176 
11177       if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11178 	{
11179 	  /* Cache the section contents so that they can be compressed
11180 	     later.  Use bfd_malloc since it will be freed by
11181 	     bfd_compress_section_contents.  */
11182 	  unsigned char *contents = esdo->this_hdr.contents;
11183 	  if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11184 	    abort ();
11185 	  contents
11186 	    = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11187 	  if (contents == NULL)
11188 	    goto error_return;
11189 	  esdo->this_hdr.contents = contents;
11190 	}
11191     }
11192 
11193   /* We have now assigned file positions for all the sections except
11194      .symtab, .strtab, and non-loaded reloc sections.  We start the
11195      .symtab section at the current file position, and write directly
11196      to it.  We build the .strtab section in memory.  */
11197   bfd_get_symcount (abfd) = 0;
11198   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11199   /* sh_name is set in prep_headers.  */
11200   symtab_hdr->sh_type = SHT_SYMTAB;
11201   /* sh_flags, sh_addr and sh_size all start off zero.  */
11202   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11203   /* sh_link is set in assign_section_numbers.  */
11204   /* sh_info is set below.  */
11205   /* sh_offset is set just below.  */
11206   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11207 
11208   if (max_sym_count < 20)
11209     max_sym_count = 20;
11210   elf_hash_table (info)->strtabsize = max_sym_count;
11211   amt = max_sym_count * sizeof (struct elf_sym_strtab);
11212   elf_hash_table (info)->strtab
11213     = (struct elf_sym_strtab *) bfd_malloc (amt);
11214   if (elf_hash_table (info)->strtab == NULL)
11215     goto error_return;
11216   /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
11217   flinfo.symshndxbuf
11218     = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11219        ? (Elf_External_Sym_Shndx *) -1 : NULL);
11220 
11221   if (info->strip != strip_all || emit_relocs)
11222     {
11223       file_ptr off = elf_next_file_pos (abfd);
11224 
11225       _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11226 
11227       /* Note that at this point elf_next_file_pos (abfd) is
11228 	 incorrect.  We do not yet know the size of the .symtab section.
11229 	 We correct next_file_pos below, after we do know the size.  */
11230 
11231       /* Start writing out the symbol table.  The first symbol is always a
11232 	 dummy symbol.  */
11233       elfsym.st_value = 0;
11234       elfsym.st_size = 0;
11235       elfsym.st_info = 0;
11236       elfsym.st_other = 0;
11237       elfsym.st_shndx = SHN_UNDEF;
11238       elfsym.st_target_internal = 0;
11239       if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11240 				     bfd_und_section_ptr, NULL) != 1)
11241 	goto error_return;
11242 
11243       /* Output a symbol for each section.  We output these even if we are
11244 	 discarding local symbols, since they are used for relocs.  These
11245 	 symbols have no names.  We store the index of each one in the
11246 	 index field of the section, so that we can find it again when
11247 	 outputting relocs.  */
11248 
11249       elfsym.st_size = 0;
11250       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11251       elfsym.st_other = 0;
11252       elfsym.st_value = 0;
11253       elfsym.st_target_internal = 0;
11254       for (i = 1; i < elf_numsections (abfd); i++)
11255 	{
11256 	  o = bfd_section_from_elf_index (abfd, i);
11257 	  if (o != NULL)
11258 	    {
11259 	      o->target_index = bfd_get_symcount (abfd);
11260 	      elfsym.st_shndx = i;
11261 	      if (!bfd_link_relocatable (info))
11262 		elfsym.st_value = o->vma;
11263 	      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11264 					     NULL) != 1)
11265 		goto error_return;
11266 	    }
11267 	}
11268     }
11269 
11270   /* Allocate some memory to hold information read in from the input
11271      files.  */
11272   if (max_contents_size != 0)
11273     {
11274       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11275       if (flinfo.contents == NULL)
11276 	goto error_return;
11277     }
11278 
11279   if (max_external_reloc_size != 0)
11280     {
11281       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11282       if (flinfo.external_relocs == NULL)
11283 	goto error_return;
11284     }
11285 
11286   if (max_internal_reloc_count != 0)
11287     {
11288       amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11289       amt *= sizeof (Elf_Internal_Rela);
11290       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11291       if (flinfo.internal_relocs == NULL)
11292 	goto error_return;
11293     }
11294 
11295   if (max_sym_count != 0)
11296     {
11297       amt = max_sym_count * bed->s->sizeof_sym;
11298       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11299       if (flinfo.external_syms == NULL)
11300 	goto error_return;
11301 
11302       amt = max_sym_count * sizeof (Elf_Internal_Sym);
11303       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11304       if (flinfo.internal_syms == NULL)
11305 	goto error_return;
11306 
11307       amt = max_sym_count * sizeof (long);
11308       flinfo.indices = (long int *) bfd_malloc (amt);
11309       if (flinfo.indices == NULL)
11310 	goto error_return;
11311 
11312       amt = max_sym_count * sizeof (asection *);
11313       flinfo.sections = (asection **) bfd_malloc (amt);
11314       if (flinfo.sections == NULL)
11315 	goto error_return;
11316     }
11317 
11318   if (max_sym_shndx_count != 0)
11319     {
11320       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11321       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11322       if (flinfo.locsym_shndx == NULL)
11323 	goto error_return;
11324     }
11325 
11326   if (elf_hash_table (info)->tls_sec)
11327     {
11328       bfd_vma base, end = 0;
11329       asection *sec;
11330 
11331       for (sec = elf_hash_table (info)->tls_sec;
11332 	   sec && (sec->flags & SEC_THREAD_LOCAL);
11333 	   sec = sec->next)
11334 	{
11335 	  bfd_size_type size = sec->size;
11336 
11337 	  if (size == 0
11338 	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
11339 	    {
11340 	      struct bfd_link_order *ord = sec->map_tail.link_order;
11341 
11342 	      if (ord != NULL)
11343 		size = ord->offset + ord->size;
11344 	    }
11345 	  end = sec->vma + size;
11346 	}
11347       base = elf_hash_table (info)->tls_sec->vma;
11348       /* Only align end of TLS section if static TLS doesn't have special
11349 	 alignment requirements.  */
11350       if (bed->static_tls_alignment == 1)
11351 	end = align_power (end,
11352 			   elf_hash_table (info)->tls_sec->alignment_power);
11353       elf_hash_table (info)->tls_size = end - base;
11354     }
11355 
11356   /* Reorder SHF_LINK_ORDER sections.  */
11357   for (o = abfd->sections; o != NULL; o = o->next)
11358     {
11359       if (!elf_fixup_link_order (abfd, o))
11360 	return FALSE;
11361     }
11362 
11363   if (!_bfd_elf_fixup_eh_frame_hdr (info))
11364     return FALSE;
11365 
11366   /* Since ELF permits relocations to be against local symbols, we
11367      must have the local symbols available when we do the relocations.
11368      Since we would rather only read the local symbols once, and we
11369      would rather not keep them in memory, we handle all the
11370      relocations for a single input file at the same time.
11371 
11372      Unfortunately, there is no way to know the total number of local
11373      symbols until we have seen all of them, and the local symbol
11374      indices precede the global symbol indices.  This means that when
11375      we are generating relocatable output, and we see a reloc against
11376      a global symbol, we can not know the symbol index until we have
11377      finished examining all the local symbols to see which ones we are
11378      going to output.  To deal with this, we keep the relocations in
11379      memory, and don't output them until the end of the link.  This is
11380      an unfortunate waste of memory, but I don't see a good way around
11381      it.  Fortunately, it only happens when performing a relocatable
11382      link, which is not the common case.  FIXME: If keep_memory is set
11383      we could write the relocs out and then read them again; I don't
11384      know how bad the memory loss will be.  */
11385 
11386   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11387     sub->output_has_begun = FALSE;
11388   for (o = abfd->sections; o != NULL; o = o->next)
11389     {
11390       for (p = o->map_head.link_order; p != NULL; p = p->next)
11391 	{
11392 	  if (p->type == bfd_indirect_link_order
11393 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11394 		  == bfd_target_elf_flavour)
11395 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11396 	    {
11397 	      if (! sub->output_has_begun)
11398 		{
11399 		  if (! elf_link_input_bfd (&flinfo, sub))
11400 		    goto error_return;
11401 		  sub->output_has_begun = TRUE;
11402 		}
11403 	    }
11404 	  else if (p->type == bfd_section_reloc_link_order
11405 		   || p->type == bfd_symbol_reloc_link_order)
11406 	    {
11407 	      if (! elf_reloc_link_order (abfd, info, o, p))
11408 		goto error_return;
11409 	    }
11410 	  else
11411 	    {
11412 	      if (! _bfd_default_link_order (abfd, info, o, p))
11413 		{
11414 		  if (p->type == bfd_indirect_link_order
11415 		      && (bfd_get_flavour (sub)
11416 			  == bfd_target_elf_flavour)
11417 		      && (elf_elfheader (sub)->e_ident[EI_CLASS]
11418 			  != bed->s->elfclass))
11419 		    {
11420 		      const char *iclass, *oclass;
11421 
11422 		      if (bed->s->elfclass == ELFCLASS64)
11423 			{
11424 			  iclass = "ELFCLASS32";
11425 			  oclass = "ELFCLASS64";
11426 			}
11427 		      else
11428 			{
11429 			  iclass = "ELFCLASS64";
11430 			  oclass = "ELFCLASS32";
11431 			}
11432 
11433 		      bfd_set_error (bfd_error_wrong_format);
11434 		      (*_bfd_error_handler)
11435 			(_("%B: file class %s incompatible with %s"),
11436 			 sub, iclass, oclass);
11437 		    }
11438 
11439 		  goto error_return;
11440 		}
11441 	    }
11442 	}
11443     }
11444 
11445   /* Free symbol buffer if needed.  */
11446   if (!info->reduce_memory_overheads)
11447     {
11448       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11449 	if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11450 	    && elf_tdata (sub)->symbuf)
11451 	  {
11452 	    free (elf_tdata (sub)->symbuf);
11453 	    elf_tdata (sub)->symbuf = NULL;
11454 	  }
11455     }
11456 
11457   /* Output any global symbols that got converted to local in a
11458      version script or due to symbol visibility.  We do this in a
11459      separate step since ELF requires all local symbols to appear
11460      prior to any global symbols.  FIXME: We should only do this if
11461      some global symbols were, in fact, converted to become local.
11462      FIXME: Will this work correctly with the Irix 5 linker?  */
11463   eoinfo.failed = FALSE;
11464   eoinfo.flinfo = &flinfo;
11465   eoinfo.localsyms = TRUE;
11466   eoinfo.file_sym_done = FALSE;
11467   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11468   if (eoinfo.failed)
11469     return FALSE;
11470 
11471   /* If backend needs to output some local symbols not present in the hash
11472      table, do it now.  */
11473   if (bed->elf_backend_output_arch_local_syms
11474       && (info->strip != strip_all || emit_relocs))
11475     {
11476       typedef int (*out_sym_func)
11477 	(void *, const char *, Elf_Internal_Sym *, asection *,
11478 	 struct elf_link_hash_entry *);
11479 
11480       if (! ((*bed->elf_backend_output_arch_local_syms)
11481 	     (abfd, info, &flinfo,
11482 	      (out_sym_func) elf_link_output_symstrtab)))
11483 	return FALSE;
11484     }
11485 
11486   /* That wrote out all the local symbols.  Finish up the symbol table
11487      with the global symbols. Even if we want to strip everything we
11488      can, we still need to deal with those global symbols that got
11489      converted to local in a version script.  */
11490 
11491   /* The sh_info field records the index of the first non local symbol.  */
11492   symtab_hdr->sh_info = bfd_get_symcount (abfd);
11493 
11494   if (dynamic
11495       && elf_hash_table (info)->dynsym != NULL
11496       && (elf_hash_table (info)->dynsym->output_section
11497 	  != bfd_abs_section_ptr))
11498     {
11499       Elf_Internal_Sym sym;
11500       bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11501       long last_local = 0;
11502 
11503       /* Write out the section symbols for the output sections.  */
11504       if (bfd_link_pic (info)
11505 	  || elf_hash_table (info)->is_relocatable_executable)
11506 	{
11507 	  asection *s;
11508 
11509 	  sym.st_size = 0;
11510 	  sym.st_name = 0;
11511 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11512 	  sym.st_other = 0;
11513 	  sym.st_target_internal = 0;
11514 
11515 	  for (s = abfd->sections; s != NULL; s = s->next)
11516 	    {
11517 	      int indx;
11518 	      bfd_byte *dest;
11519 	      long dynindx;
11520 
11521 	      dynindx = elf_section_data (s)->dynindx;
11522 	      if (dynindx <= 0)
11523 		continue;
11524 	      indx = elf_section_data (s)->this_idx;
11525 	      BFD_ASSERT (indx > 0);
11526 	      sym.st_shndx = indx;
11527 	      if (! check_dynsym (abfd, &sym))
11528 		return FALSE;
11529 	      sym.st_value = s->vma;
11530 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
11531 	      if (last_local < dynindx)
11532 		last_local = dynindx;
11533 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11534 	    }
11535 	}
11536 
11537       /* Write out the local dynsyms.  */
11538       if (elf_hash_table (info)->dynlocal)
11539 	{
11540 	  struct elf_link_local_dynamic_entry *e;
11541 	  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11542 	    {
11543 	      asection *s;
11544 	      bfd_byte *dest;
11545 
11546 	      /* Copy the internal symbol and turn off visibility.
11547 		 Note that we saved a word of storage and overwrote
11548 		 the original st_name with the dynstr_index.  */
11549 	      sym = e->isym;
11550 	      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11551 
11552 	      s = bfd_section_from_elf_index (e->input_bfd,
11553 					      e->isym.st_shndx);
11554 	      if (s != NULL)
11555 		{
11556 		  sym.st_shndx =
11557 		    elf_section_data (s->output_section)->this_idx;
11558 		  if (! check_dynsym (abfd, &sym))
11559 		    return FALSE;
11560 		  sym.st_value = (s->output_section->vma
11561 				  + s->output_offset
11562 				  + e->isym.st_value);
11563 		}
11564 
11565 	      if (last_local < e->dynindx)
11566 		last_local = e->dynindx;
11567 
11568 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11569 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11570 	    }
11571 	}
11572 
11573       elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11574 	last_local + 1;
11575     }
11576 
11577   /* We get the global symbols from the hash table.  */
11578   eoinfo.failed = FALSE;
11579   eoinfo.localsyms = FALSE;
11580   eoinfo.flinfo = &flinfo;
11581   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11582   if (eoinfo.failed)
11583     return FALSE;
11584 
11585   /* If backend needs to output some symbols not present in the hash
11586      table, do it now.  */
11587   if (bed->elf_backend_output_arch_syms
11588       && (info->strip != strip_all || emit_relocs))
11589     {
11590       typedef int (*out_sym_func)
11591 	(void *, const char *, Elf_Internal_Sym *, asection *,
11592 	 struct elf_link_hash_entry *);
11593 
11594       if (! ((*bed->elf_backend_output_arch_syms)
11595 	     (abfd, info, &flinfo,
11596 	      (out_sym_func) elf_link_output_symstrtab)))
11597 	return FALSE;
11598     }
11599 
11600   /* Finalize the .strtab section.  */
11601   _bfd_elf_strtab_finalize (flinfo.symstrtab);
11602 
11603   /* Swap out the .strtab section. */
11604   if (!elf_link_swap_symbols_out (&flinfo))
11605     return FALSE;
11606 
11607   /* Now we know the size of the symtab section.  */
11608   if (bfd_get_symcount (abfd) > 0)
11609     {
11610       /* Finish up and write out the symbol string table (.strtab)
11611 	 section.  */
11612       Elf_Internal_Shdr *symstrtab_hdr;
11613       file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11614 
11615       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11616       if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11617 	{
11618 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11619 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11620 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11621 	  amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11622 	  symtab_shndx_hdr->sh_size = amt;
11623 
11624 	  off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11625 							   off, TRUE);
11626 
11627 	  if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11628 	      || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11629 	    return FALSE;
11630 	}
11631 
11632       symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11633       /* sh_name was set in prep_headers.  */
11634       symstrtab_hdr->sh_type = SHT_STRTAB;
11635       symstrtab_hdr->sh_flags = 0;
11636       symstrtab_hdr->sh_addr = 0;
11637       symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11638       symstrtab_hdr->sh_entsize = 0;
11639       symstrtab_hdr->sh_link = 0;
11640       symstrtab_hdr->sh_info = 0;
11641       /* sh_offset is set just below.  */
11642       symstrtab_hdr->sh_addralign = 1;
11643 
11644       off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11645 						       off, TRUE);
11646       elf_next_file_pos (abfd) = off;
11647 
11648       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11649 	  || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11650 	return FALSE;
11651     }
11652 
11653   /* Adjust the relocs to have the correct symbol indices.  */
11654   for (o = abfd->sections; o != NULL; o = o->next)
11655     {
11656       struct bfd_elf_section_data *esdo = elf_section_data (o);
11657       bfd_boolean sort;
11658       if ((o->flags & SEC_RELOC) == 0)
11659 	continue;
11660 
11661       sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11662       if (esdo->rel.hdr != NULL
11663 	  && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11664 	return FALSE;
11665       if (esdo->rela.hdr != NULL
11666 	  && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11667 	return FALSE;
11668 
11669       /* Set the reloc_count field to 0 to prevent write_relocs from
11670 	 trying to swap the relocs out itself.  */
11671       o->reloc_count = 0;
11672     }
11673 
11674   if (dynamic && info->combreloc && dynobj != NULL)
11675     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11676 
11677   /* If we are linking against a dynamic object, or generating a
11678      shared library, finish up the dynamic linking information.  */
11679   if (dynamic)
11680     {
11681       bfd_byte *dyncon, *dynconend;
11682 
11683       /* Fix up .dynamic entries.  */
11684       o = bfd_get_linker_section (dynobj, ".dynamic");
11685       BFD_ASSERT (o != NULL);
11686 
11687       dyncon = o->contents;
11688       dynconend = o->contents + o->size;
11689       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11690 	{
11691 	  Elf_Internal_Dyn dyn;
11692 	  const char *name;
11693 	  unsigned int type;
11694 
11695 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11696 
11697 	  switch (dyn.d_tag)
11698 	    {
11699 	    default:
11700 	      continue;
11701 	    case DT_NULL:
11702 	      if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11703 		{
11704 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
11705 		    {
11706 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11707 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11708 		    default: continue;
11709 		    }
11710 		  dyn.d_un.d_val = relativecount;
11711 		  relativecount = 0;
11712 		  break;
11713 		}
11714 	      continue;
11715 
11716 	    case DT_INIT:
11717 	      name = info->init_function;
11718 	      goto get_sym;
11719 	    case DT_FINI:
11720 	      name = info->fini_function;
11721 	    get_sym:
11722 	      {
11723 		struct elf_link_hash_entry *h;
11724 
11725 		h = elf_link_hash_lookup (elf_hash_table (info), name,
11726 					  FALSE, FALSE, TRUE);
11727 		if (h != NULL
11728 		    && (h->root.type == bfd_link_hash_defined
11729 			|| h->root.type == bfd_link_hash_defweak))
11730 		  {
11731 		    dyn.d_un.d_ptr = h->root.u.def.value;
11732 		    o = h->root.u.def.section;
11733 		    if (o->output_section != NULL)
11734 		      dyn.d_un.d_ptr += (o->output_section->vma
11735 					 + o->output_offset);
11736 		    else
11737 		      {
11738 			/* The symbol is imported from another shared
11739 			   library and does not apply to this one.  */
11740 			dyn.d_un.d_ptr = 0;
11741 		      }
11742 		    break;
11743 		  }
11744 	      }
11745 	      continue;
11746 
11747 	    case DT_PREINIT_ARRAYSZ:
11748 	      name = ".preinit_array";
11749 	      goto get_size;
11750 	    case DT_INIT_ARRAYSZ:
11751 	      name = ".init_array";
11752 	      goto get_size;
11753 	    case DT_FINI_ARRAYSZ:
11754 	      name = ".fini_array";
11755 	    get_size:
11756 	      o = bfd_get_section_by_name (abfd, name);
11757 	      if (o == NULL)
11758 		{
11759 		  (*_bfd_error_handler)
11760 		    (_("%B: could not find output section %s"), abfd, name);
11761 		  goto error_return;
11762 		}
11763 	      if (o->size == 0)
11764 		(*_bfd_error_handler)
11765 		  (_("warning: %s section has zero size"), name);
11766 	      dyn.d_un.d_val = o->size;
11767 	      break;
11768 
11769 	    case DT_PREINIT_ARRAY:
11770 	      name = ".preinit_array";
11771 	      goto get_vma;
11772 	    case DT_INIT_ARRAY:
11773 	      name = ".init_array";
11774 	      goto get_vma;
11775 	    case DT_FINI_ARRAY:
11776 	      name = ".fini_array";
11777 	      goto get_vma;
11778 
11779 	    case DT_HASH:
11780 	      name = ".hash";
11781 	      goto get_vma;
11782 	    case DT_GNU_HASH:
11783 	      name = ".gnu.hash";
11784 	      goto get_vma;
11785 	    case DT_STRTAB:
11786 	      name = ".dynstr";
11787 	      goto get_vma;
11788 	    case DT_SYMTAB:
11789 	      name = ".dynsym";
11790 	      goto get_vma;
11791 	    case DT_VERDEF:
11792 	      name = ".gnu.version_d";
11793 	      goto get_vma;
11794 	    case DT_VERNEED:
11795 	      name = ".gnu.version_r";
11796 	      goto get_vma;
11797 	    case DT_VERSYM:
11798 	      name = ".gnu.version";
11799 	    get_vma:
11800 	      o = bfd_get_section_by_name (abfd, name);
11801 	      if (o == NULL)
11802 		{
11803 		  (*_bfd_error_handler)
11804 		    (_("%B: could not find output section %s"), abfd, name);
11805 		  goto error_return;
11806 		}
11807 	      if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11808 		{
11809 		  (*_bfd_error_handler)
11810 		    (_("warning: section '%s' is being made into a note"), name);
11811 		  bfd_set_error (bfd_error_nonrepresentable_section);
11812 		  goto error_return;
11813 		}
11814 	      dyn.d_un.d_ptr = o->vma;
11815 	      break;
11816 
11817 	    case DT_REL:
11818 	    case DT_RELA:
11819 	    case DT_RELSZ:
11820 	    case DT_RELASZ:
11821 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11822 		type = SHT_REL;
11823 	      else
11824 		type = SHT_RELA;
11825 	      dyn.d_un.d_val = 0;
11826 	      dyn.d_un.d_ptr = 0;
11827 	      for (i = 1; i < elf_numsections (abfd); i++)
11828 		{
11829 		  Elf_Internal_Shdr *hdr;
11830 
11831 		  hdr = elf_elfsections (abfd)[i];
11832 		  if (hdr->sh_type == type
11833 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
11834 		    {
11835 		      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11836 			dyn.d_un.d_val += hdr->sh_size;
11837 		      else
11838 			{
11839 			  if (dyn.d_un.d_ptr == 0
11840 			      || hdr->sh_addr < dyn.d_un.d_ptr)
11841 			    dyn.d_un.d_ptr = hdr->sh_addr;
11842 			}
11843 		    }
11844 		}
11845 	      break;
11846 	    }
11847 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11848 	}
11849     }
11850 
11851   /* If we have created any dynamic sections, then output them.  */
11852   if (dynobj != NULL)
11853     {
11854       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11855 	goto error_return;
11856 
11857       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
11858       if (((info->warn_shared_textrel && bfd_link_pic (info))
11859 	   || info->error_textrel)
11860 	  && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11861 	{
11862 	  bfd_byte *dyncon, *dynconend;
11863 
11864 	  dyncon = o->contents;
11865 	  dynconend = o->contents + o->size;
11866 	  for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11867 	    {
11868 	      Elf_Internal_Dyn dyn;
11869 
11870 	      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11871 
11872 	      if (dyn.d_tag == DT_TEXTREL)
11873 		{
11874 		  if (info->error_textrel)
11875 		    info->callbacks->einfo
11876 		      (_("%P%X: read-only segment has dynamic relocations.\n"));
11877 		  else
11878 		    info->callbacks->einfo
11879 		      (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11880 		  break;
11881 		}
11882 	    }
11883 	}
11884 
11885       for (o = dynobj->sections; o != NULL; o = o->next)
11886 	{
11887 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
11888 	      || o->size == 0
11889 	      || o->output_section == bfd_abs_section_ptr)
11890 	    continue;
11891 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
11892 	    {
11893 	      /* At this point, we are only interested in sections
11894 		 created by _bfd_elf_link_create_dynamic_sections.  */
11895 	      continue;
11896 	    }
11897 	  if (elf_hash_table (info)->stab_info.stabstr == o)
11898 	    continue;
11899 	  if (elf_hash_table (info)->eh_info.hdr_sec == o)
11900 	    continue;
11901 	  if (strcmp (o->name, ".dynstr") != 0)
11902 	    {
11903 	      /* FIXME: octets_per_byte.  */
11904 	      if (! bfd_set_section_contents (abfd, o->output_section,
11905 					      o->contents,
11906 					      (file_ptr) o->output_offset,
11907 					      o->size))
11908 		goto error_return;
11909 	    }
11910 	  else
11911 	    {
11912 	      /* The contents of the .dynstr section are actually in a
11913 		 stringtab.  */
11914 	      file_ptr off;
11915 
11916 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11917 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
11918 		  || ! _bfd_elf_strtab_emit (abfd,
11919 					     elf_hash_table (info)->dynstr))
11920 		goto error_return;
11921 	    }
11922 	}
11923     }
11924 
11925   if (bfd_link_relocatable (info))
11926     {
11927       bfd_boolean failed = FALSE;
11928 
11929       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11930       if (failed)
11931 	goto error_return;
11932     }
11933 
11934   /* If we have optimized stabs strings, output them.  */
11935   if (elf_hash_table (info)->stab_info.stabstr != NULL)
11936     {
11937       if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11938 	goto error_return;
11939     }
11940 
11941   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11942     goto error_return;
11943 
11944   elf_final_link_free (abfd, &flinfo);
11945 
11946   elf_linker (abfd) = TRUE;
11947 
11948   if (attr_section)
11949     {
11950       bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11951       if (contents == NULL)
11952 	return FALSE;	/* Bail out and fail.  */
11953       bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11954       bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11955       free (contents);
11956     }
11957 
11958   return TRUE;
11959 
11960  error_return:
11961   elf_final_link_free (abfd, &flinfo);
11962   return FALSE;
11963 }
11964 
11965 /* Initialize COOKIE for input bfd ABFD.  */
11966 
11967 static bfd_boolean
11968 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11969 		   struct bfd_link_info *info, bfd *abfd)
11970 {
11971   Elf_Internal_Shdr *symtab_hdr;
11972   const struct elf_backend_data *bed;
11973 
11974   bed = get_elf_backend_data (abfd);
11975   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11976 
11977   cookie->abfd = abfd;
11978   cookie->sym_hashes = elf_sym_hashes (abfd);
11979   cookie->bad_symtab = elf_bad_symtab (abfd);
11980   if (cookie->bad_symtab)
11981     {
11982       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11983       cookie->extsymoff = 0;
11984     }
11985   else
11986     {
11987       cookie->locsymcount = symtab_hdr->sh_info;
11988       cookie->extsymoff = symtab_hdr->sh_info;
11989     }
11990 
11991   if (bed->s->arch_size == 32)
11992     cookie->r_sym_shift = 8;
11993   else
11994     cookie->r_sym_shift = 32;
11995 
11996   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11997   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11998     {
11999       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12000 					      cookie->locsymcount, 0,
12001 					      NULL, NULL, NULL);
12002       if (cookie->locsyms == NULL)
12003 	{
12004 	  info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12005 	  return FALSE;
12006 	}
12007       if (info->keep_memory)
12008 	symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12009     }
12010   return TRUE;
12011 }
12012 
12013 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
12014 
12015 static void
12016 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12017 {
12018   Elf_Internal_Shdr *symtab_hdr;
12019 
12020   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12021   if (cookie->locsyms != NULL
12022       && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12023     free (cookie->locsyms);
12024 }
12025 
12026 /* Initialize the relocation information in COOKIE for input section SEC
12027    of input bfd ABFD.  */
12028 
12029 static bfd_boolean
12030 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12031 			struct bfd_link_info *info, bfd *abfd,
12032 			asection *sec)
12033 {
12034   const struct elf_backend_data *bed;
12035 
12036   if (sec->reloc_count == 0)
12037     {
12038       cookie->rels = NULL;
12039       cookie->relend = NULL;
12040     }
12041   else
12042     {
12043       bed = get_elf_backend_data (abfd);
12044 
12045       cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12046 						info->keep_memory);
12047       if (cookie->rels == NULL)
12048 	return FALSE;
12049       cookie->rel = cookie->rels;
12050       cookie->relend = (cookie->rels
12051 			+ sec->reloc_count * bed->s->int_rels_per_ext_rel);
12052     }
12053   cookie->rel = cookie->rels;
12054   return TRUE;
12055 }
12056 
12057 /* Free the memory allocated by init_reloc_cookie_rels,
12058    if appropriate.  */
12059 
12060 static void
12061 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12062 			asection *sec)
12063 {
12064   if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12065     free (cookie->rels);
12066 }
12067 
12068 /* Initialize the whole of COOKIE for input section SEC.  */
12069 
12070 static bfd_boolean
12071 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12072 			       struct bfd_link_info *info,
12073 			       asection *sec)
12074 {
12075   if (!init_reloc_cookie (cookie, info, sec->owner))
12076     goto error1;
12077   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12078     goto error2;
12079   return TRUE;
12080 
12081  error2:
12082   fini_reloc_cookie (cookie, sec->owner);
12083  error1:
12084   return FALSE;
12085 }
12086 
12087 /* Free the memory allocated by init_reloc_cookie_for_section,
12088    if appropriate.  */
12089 
12090 static void
12091 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12092 			       asection *sec)
12093 {
12094   fini_reloc_cookie_rels (cookie, sec);
12095   fini_reloc_cookie (cookie, sec->owner);
12096 }
12097 
12098 /* Garbage collect unused sections.  */
12099 
12100 /* Default gc_mark_hook.  */
12101 
12102 asection *
12103 _bfd_elf_gc_mark_hook (asection *sec,
12104 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
12105 		       Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12106 		       struct elf_link_hash_entry *h,
12107 		       Elf_Internal_Sym *sym)
12108 {
12109   if (h != NULL)
12110     {
12111       switch (h->root.type)
12112 	{
12113 	case bfd_link_hash_defined:
12114 	case bfd_link_hash_defweak:
12115 	  return h->root.u.def.section;
12116 
12117 	case bfd_link_hash_common:
12118 	  return h->root.u.c.p->section;
12119 
12120 	default:
12121 	  break;
12122 	}
12123     }
12124   else
12125     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12126 
12127   return NULL;
12128 }
12129 
12130 /* COOKIE->rel describes a relocation against section SEC, which is
12131    a section we've decided to keep.  Return the section that contains
12132    the relocation symbol, or NULL if no section contains it.  */
12133 
12134 asection *
12135 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12136 		       elf_gc_mark_hook_fn gc_mark_hook,
12137 		       struct elf_reloc_cookie *cookie,
12138 		       bfd_boolean *start_stop)
12139 {
12140   unsigned long r_symndx;
12141   struct elf_link_hash_entry *h;
12142 
12143   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12144   if (r_symndx == STN_UNDEF)
12145     return NULL;
12146 
12147   if (r_symndx >= cookie->locsymcount
12148       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12149     {
12150       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12151       if (h == NULL)
12152 	{
12153 	  info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12154 				  sec->owner);
12155 	  return NULL;
12156 	}
12157       while (h->root.type == bfd_link_hash_indirect
12158 	     || h->root.type == bfd_link_hash_warning)
12159 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
12160       h->mark = 1;
12161       /* If this symbol is weak and there is a non-weak definition, we
12162 	 keep the non-weak definition because many backends put
12163 	 dynamic reloc info on the non-weak definition for code
12164 	 handling copy relocs.  */
12165       if (h->u.weakdef != NULL)
12166 	h->u.weakdef->mark = 1;
12167 
12168       if (start_stop != NULL
12169 	  && (h->root.type == bfd_link_hash_undefined
12170 	      || h->root.type == bfd_link_hash_undefweak))
12171 	{
12172 	  /* To work around a glibc bug, mark all XXX input sections
12173 	     when there is an as yet undefined reference to __start_XXX
12174 	     or __stop_XXX symbols.  The linker will later define such
12175 	     symbols for orphan input sections that have a name
12176 	     representable as a C identifier.  */
12177 	  const char *sec_name = NULL;
12178 	  if (strncmp (h->root.root.string, "__start_", 8) == 0)
12179 	    sec_name = h->root.root.string + 8;
12180 	  else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12181 	    sec_name = h->root.root.string + 7;
12182 
12183 	  if (sec_name != NULL && *sec_name != '\0')
12184 	    {
12185 	      bfd *i;
12186 
12187 	      for (i = info->input_bfds; i != NULL; i = i->link.next)
12188 		{
12189 		  asection *s = bfd_get_section_by_name (i, sec_name);
12190 		  if (s != NULL && !s->gc_mark)
12191 		    {
12192 		      *start_stop = TRUE;
12193 		      return s;
12194 		    }
12195 		}
12196 	    }
12197 	}
12198 
12199       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12200     }
12201 
12202   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12203 			  &cookie->locsyms[r_symndx]);
12204 }
12205 
12206 /* COOKIE->rel describes a relocation against section SEC, which is
12207    a section we've decided to keep.  Mark the section that contains
12208    the relocation symbol.  */
12209 
12210 bfd_boolean
12211 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12212 			asection *sec,
12213 			elf_gc_mark_hook_fn gc_mark_hook,
12214 			struct elf_reloc_cookie *cookie)
12215 {
12216   asection *rsec;
12217   bfd_boolean start_stop = FALSE;
12218 
12219   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12220   while (rsec != NULL)
12221     {
12222       if (!rsec->gc_mark)
12223 	{
12224 	  if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12225 	      || (rsec->owner->flags & DYNAMIC) != 0)
12226 	    rsec->gc_mark = 1;
12227 	  else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12228 	    return FALSE;
12229 	}
12230       if (!start_stop)
12231 	break;
12232       rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12233     }
12234   return TRUE;
12235 }
12236 
12237 /* The mark phase of garbage collection.  For a given section, mark
12238    it and any sections in this section's group, and all the sections
12239    which define symbols to which it refers.  */
12240 
12241 bfd_boolean
12242 _bfd_elf_gc_mark (struct bfd_link_info *info,
12243 		  asection *sec,
12244 		  elf_gc_mark_hook_fn gc_mark_hook)
12245 {
12246   bfd_boolean ret;
12247   asection *group_sec, *eh_frame;
12248 
12249   sec->gc_mark = 1;
12250 
12251   /* Mark all the sections in the group.  */
12252   group_sec = elf_section_data (sec)->next_in_group;
12253   if (group_sec && !group_sec->gc_mark)
12254     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12255       return FALSE;
12256 
12257   /* Look through the section relocs.  */
12258   ret = TRUE;
12259   eh_frame = elf_eh_frame_section (sec->owner);
12260   if ((sec->flags & SEC_RELOC) != 0
12261       && sec->reloc_count > 0
12262       && sec != eh_frame)
12263     {
12264       struct elf_reloc_cookie cookie;
12265 
12266       if (!init_reloc_cookie_for_section (&cookie, info, sec))
12267 	ret = FALSE;
12268       else
12269 	{
12270 	  for (; cookie.rel < cookie.relend; cookie.rel++)
12271 	    if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12272 	      {
12273 		ret = FALSE;
12274 		break;
12275 	      }
12276 	  fini_reloc_cookie_for_section (&cookie, sec);
12277 	}
12278     }
12279 
12280   if (ret && eh_frame && elf_fde_list (sec))
12281     {
12282       struct elf_reloc_cookie cookie;
12283 
12284       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12285 	ret = FALSE;
12286       else
12287 	{
12288 	  if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12289 				      gc_mark_hook, &cookie))
12290 	    ret = FALSE;
12291 	  fini_reloc_cookie_for_section (&cookie, eh_frame);
12292 	}
12293     }
12294 
12295   eh_frame = elf_section_eh_frame_entry (sec);
12296   if (ret && eh_frame && !eh_frame->gc_mark)
12297     if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12298       ret = FALSE;
12299 
12300   return ret;
12301 }
12302 
12303 /* Scan and mark sections in a special or debug section group.  */
12304 
12305 static void
12306 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12307 {
12308   /* Point to first section of section group.  */
12309   asection *ssec;
12310   /* Used to iterate the section group.  */
12311   asection *msec;
12312 
12313   bfd_boolean is_special_grp = TRUE;
12314   bfd_boolean is_debug_grp = TRUE;
12315 
12316   /* First scan to see if group contains any section other than debug
12317      and special section.  */
12318   ssec = msec = elf_next_in_group (grp);
12319   do
12320     {
12321       if ((msec->flags & SEC_DEBUGGING) == 0)
12322 	is_debug_grp = FALSE;
12323 
12324       if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12325 	is_special_grp = FALSE;
12326 
12327       msec = elf_next_in_group (msec);
12328     }
12329   while (msec != ssec);
12330 
12331   /* If this is a pure debug section group or pure special section group,
12332      keep all sections in this group.  */
12333   if (is_debug_grp || is_special_grp)
12334     {
12335       do
12336 	{
12337 	  msec->gc_mark = 1;
12338 	  msec = elf_next_in_group (msec);
12339 	}
12340       while (msec != ssec);
12341     }
12342 }
12343 
12344 /* Keep debug and special sections.  */
12345 
12346 bfd_boolean
12347 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12348 				 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12349 {
12350   bfd *ibfd;
12351 
12352   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12353     {
12354       asection *isec;
12355       bfd_boolean some_kept;
12356       bfd_boolean debug_frag_seen;
12357 
12358       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12359 	continue;
12360 
12361       /* Ensure all linker created sections are kept,
12362 	 see if any other section is already marked,
12363 	 and note if we have any fragmented debug sections.  */
12364       debug_frag_seen = some_kept = FALSE;
12365       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12366 	{
12367 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
12368 	    isec->gc_mark = 1;
12369 	  else if (isec->gc_mark)
12370 	    some_kept = TRUE;
12371 
12372 	  if (debug_frag_seen == FALSE
12373 	      && (isec->flags & SEC_DEBUGGING)
12374 	      && CONST_STRNEQ (isec->name, ".debug_line."))
12375 	    debug_frag_seen = TRUE;
12376 	}
12377 
12378       /* If no section in this file will be kept, then we can
12379 	 toss out the debug and special sections.  */
12380       if (!some_kept)
12381 	continue;
12382 
12383       /* Keep debug and special sections like .comment when they are
12384 	 not part of a group.  Also keep section groups that contain
12385 	 just debug sections or special sections.  */
12386       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12387 	{
12388 	  if ((isec->flags & SEC_GROUP) != 0)
12389 	    _bfd_elf_gc_mark_debug_special_section_group (isec);
12390 	  else if (((isec->flags & SEC_DEBUGGING) != 0
12391 		    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12392 		   && elf_next_in_group (isec) == NULL)
12393 	    isec->gc_mark = 1;
12394 	}
12395 
12396       if (! debug_frag_seen)
12397 	continue;
12398 
12399       /* Look for CODE sections which are going to be discarded,
12400 	 and find and discard any fragmented debug sections which
12401 	 are associated with that code section.  */
12402       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12403 	if ((isec->flags & SEC_CODE) != 0
12404 	    && isec->gc_mark == 0)
12405 	  {
12406 	    unsigned int ilen;
12407 	    asection *dsec;
12408 
12409 	    ilen = strlen (isec->name);
12410 
12411 	    /* Association is determined by the name of the debug section
12412 	       containing the name of the code section as a suffix.  For
12413 	       example .debug_line.text.foo is a debug section associated
12414 	       with .text.foo.  */
12415 	    for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12416 	      {
12417 		unsigned int dlen;
12418 
12419 		if (dsec->gc_mark == 0
12420 		    || (dsec->flags & SEC_DEBUGGING) == 0)
12421 		  continue;
12422 
12423 		dlen = strlen (dsec->name);
12424 
12425 		if (dlen > ilen
12426 		    && strncmp (dsec->name + (dlen - ilen),
12427 				isec->name, ilen) == 0)
12428 		  {
12429 		    dsec->gc_mark = 0;
12430 		  }
12431 	      }
12432 	  }
12433     }
12434   return TRUE;
12435 }
12436 
12437 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
12438 
12439 struct elf_gc_sweep_symbol_info
12440 {
12441   struct bfd_link_info *info;
12442   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12443 		       bfd_boolean);
12444 };
12445 
12446 static bfd_boolean
12447 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12448 {
12449   if (!h->mark
12450       && (((h->root.type == bfd_link_hash_defined
12451 	    || h->root.type == bfd_link_hash_defweak)
12452 	   && !((h->def_regular || ELF_COMMON_DEF_P (h))
12453 		&& h->root.u.def.section->gc_mark))
12454 	  || h->root.type == bfd_link_hash_undefined
12455 	  || h->root.type == bfd_link_hash_undefweak))
12456     {
12457       struct elf_gc_sweep_symbol_info *inf;
12458 
12459       inf = (struct elf_gc_sweep_symbol_info *) data;
12460       (*inf->hide_symbol) (inf->info, h, TRUE);
12461       h->def_regular = 0;
12462       h->ref_regular = 0;
12463       h->ref_regular_nonweak = 0;
12464     }
12465 
12466   return TRUE;
12467 }
12468 
12469 /* The sweep phase of garbage collection.  Remove all garbage sections.  */
12470 
12471 typedef bfd_boolean (*gc_sweep_hook_fn)
12472   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12473 
12474 static bfd_boolean
12475 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12476 {
12477   bfd *sub;
12478   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12479   gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12480   unsigned long section_sym_count;
12481   struct elf_gc_sweep_symbol_info sweep_info;
12482 
12483   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12484     {
12485       asection *o;
12486 
12487       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12488 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12489 	continue;
12490 
12491       for (o = sub->sections; o != NULL; o = o->next)
12492 	{
12493 	  /* When any section in a section group is kept, we keep all
12494 	     sections in the section group.  If the first member of
12495 	     the section group is excluded, we will also exclude the
12496 	     group section.  */
12497 	  if (o->flags & SEC_GROUP)
12498 	    {
12499 	      asection *first = elf_next_in_group (o);
12500 	      o->gc_mark = first->gc_mark;
12501 	    }
12502 
12503 	  if (o->gc_mark)
12504 	    continue;
12505 
12506 	  /* Skip sweeping sections already excluded.  */
12507 	  if (o->flags & SEC_EXCLUDE)
12508 	    continue;
12509 
12510 	  /* Since this is early in the link process, it is simple
12511 	     to remove a section from the output.  */
12512 	  o->flags |= SEC_EXCLUDE;
12513 
12514 	  if (info->print_gc_sections && o->size != 0)
12515 	    _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12516 
12517 	  /* But we also have to update some of the relocation
12518 	     info we collected before.  */
12519 	  if (gc_sweep_hook
12520 	      && (o->flags & SEC_RELOC) != 0
12521 	      && o->reloc_count != 0
12522 	      && !((info->strip == strip_all || info->strip == strip_debugger)
12523 		   && (o->flags & SEC_DEBUGGING) != 0)
12524 	      && !bfd_is_abs_section (o->output_section))
12525 	    {
12526 	      Elf_Internal_Rela *internal_relocs;
12527 	      bfd_boolean r;
12528 
12529 	      internal_relocs
12530 		= _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12531 					     info->keep_memory);
12532 	      if (internal_relocs == NULL)
12533 		return FALSE;
12534 
12535 	      r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12536 
12537 	      if (elf_section_data (o)->relocs != internal_relocs)
12538 		free (internal_relocs);
12539 
12540 	      if (!r)
12541 		return FALSE;
12542 	    }
12543 	}
12544     }
12545 
12546   /* Remove the symbols that were in the swept sections from the dynamic
12547      symbol table.  GCFIXME: Anyone know how to get them out of the
12548      static symbol table as well?  */
12549   sweep_info.info = info;
12550   sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12551   elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12552 			  &sweep_info);
12553 
12554   _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12555   return TRUE;
12556 }
12557 
12558 /* Propagate collected vtable information.  This is called through
12559    elf_link_hash_traverse.  */
12560 
12561 static bfd_boolean
12562 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12563 {
12564   /* Those that are not vtables.  */
12565   if (h->vtable == NULL || h->vtable->parent == NULL)
12566     return TRUE;
12567 
12568   /* Those vtables that do not have parents, we cannot merge.  */
12569   if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12570     return TRUE;
12571 
12572   /* If we've already been done, exit.  */
12573   if (h->vtable->used && h->vtable->used[-1])
12574     return TRUE;
12575 
12576   /* Make sure the parent's table is up to date.  */
12577   elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12578 
12579   if (h->vtable->used == NULL)
12580     {
12581       /* None of this table's entries were referenced.  Re-use the
12582 	 parent's table.  */
12583       h->vtable->used = h->vtable->parent->vtable->used;
12584       h->vtable->size = h->vtable->parent->vtable->size;
12585     }
12586   else
12587     {
12588       size_t n;
12589       bfd_boolean *cu, *pu;
12590 
12591       /* Or the parent's entries into ours.  */
12592       cu = h->vtable->used;
12593       cu[-1] = TRUE;
12594       pu = h->vtable->parent->vtable->used;
12595       if (pu != NULL)
12596 	{
12597 	  const struct elf_backend_data *bed;
12598 	  unsigned int log_file_align;
12599 
12600 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
12601 	  log_file_align = bed->s->log_file_align;
12602 	  n = h->vtable->parent->vtable->size >> log_file_align;
12603 	  while (n--)
12604 	    {
12605 	      if (*pu)
12606 		*cu = TRUE;
12607 	      pu++;
12608 	      cu++;
12609 	    }
12610 	}
12611     }
12612 
12613   return TRUE;
12614 }
12615 
12616 static bfd_boolean
12617 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12618 {
12619   asection *sec;
12620   bfd_vma hstart, hend;
12621   Elf_Internal_Rela *relstart, *relend, *rel;
12622   const struct elf_backend_data *bed;
12623   unsigned int log_file_align;
12624 
12625   /* Take care of both those symbols that do not describe vtables as
12626      well as those that are not loaded.  */
12627   if (h->vtable == NULL || h->vtable->parent == NULL)
12628     return TRUE;
12629 
12630   BFD_ASSERT (h->root.type == bfd_link_hash_defined
12631 	      || h->root.type == bfd_link_hash_defweak);
12632 
12633   sec = h->root.u.def.section;
12634   hstart = h->root.u.def.value;
12635   hend = hstart + h->size;
12636 
12637   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12638   if (!relstart)
12639     return *(bfd_boolean *) okp = FALSE;
12640   bed = get_elf_backend_data (sec->owner);
12641   log_file_align = bed->s->log_file_align;
12642 
12643   relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12644 
12645   for (rel = relstart; rel < relend; ++rel)
12646     if (rel->r_offset >= hstart && rel->r_offset < hend)
12647       {
12648 	/* If the entry is in use, do nothing.  */
12649 	if (h->vtable->used
12650 	    && (rel->r_offset - hstart) < h->vtable->size)
12651 	  {
12652 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12653 	    if (h->vtable->used[entry])
12654 	      continue;
12655 	  }
12656 	/* Otherwise, kill it.  */
12657 	rel->r_offset = rel->r_info = rel->r_addend = 0;
12658       }
12659 
12660   return TRUE;
12661 }
12662 
12663 /* Mark sections containing dynamically referenced symbols.  When
12664    building shared libraries, we must assume that any visible symbol is
12665    referenced.  */
12666 
12667 bfd_boolean
12668 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12669 {
12670   struct bfd_link_info *info = (struct bfd_link_info *) inf;
12671   struct bfd_elf_dynamic_list *d = info->dynamic_list;
12672 
12673   if ((h->root.type == bfd_link_hash_defined
12674        || h->root.type == bfd_link_hash_defweak)
12675       && (h->ref_dynamic
12676 	  || ((h->def_regular || ELF_COMMON_DEF_P (h))
12677 	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12678 	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12679 	      && (!bfd_link_executable (info)
12680 		  || info->export_dynamic
12681 		  || (h->dynamic
12682 		      && d != NULL
12683 		      && (*d->match) (&d->head, NULL, h->root.root.string)))
12684 	      && (h->versioned >= versioned
12685 		  || !bfd_hide_sym_by_version (info->version_info,
12686 					       h->root.root.string)))))
12687     h->root.u.def.section->flags |= SEC_KEEP;
12688 
12689   return TRUE;
12690 }
12691 
12692 /* Keep all sections containing symbols undefined on the command-line,
12693    and the section containing the entry symbol.  */
12694 
12695 void
12696 _bfd_elf_gc_keep (struct bfd_link_info *info)
12697 {
12698   struct bfd_sym_chain *sym;
12699 
12700   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12701     {
12702       struct elf_link_hash_entry *h;
12703 
12704       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12705 				FALSE, FALSE, FALSE);
12706 
12707       if (h != NULL
12708 	  && (h->root.type == bfd_link_hash_defined
12709 	      || h->root.type == bfd_link_hash_defweak)
12710 	  && !bfd_is_abs_section (h->root.u.def.section))
12711 	h->root.u.def.section->flags |= SEC_KEEP;
12712     }
12713 }
12714 
12715 bfd_boolean
12716 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12717 				struct bfd_link_info *info)
12718 {
12719   bfd *ibfd = info->input_bfds;
12720 
12721   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12722     {
12723       asection *sec;
12724       struct elf_reloc_cookie cookie;
12725 
12726       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12727 	continue;
12728 
12729       if (!init_reloc_cookie (&cookie, info, ibfd))
12730 	return FALSE;
12731 
12732       for (sec = ibfd->sections; sec; sec = sec->next)
12733 	{
12734 	  if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12735 	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12736 	    {
12737 	      _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12738 	      fini_reloc_cookie_rels (&cookie, sec);
12739 	    }
12740 	}
12741     }
12742   return TRUE;
12743 }
12744 
12745 /* Do mark and sweep of unused sections.  */
12746 
12747 bfd_boolean
12748 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12749 {
12750   bfd_boolean ok = TRUE;
12751   bfd *sub;
12752   elf_gc_mark_hook_fn gc_mark_hook;
12753   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12754   struct elf_link_hash_table *htab;
12755 
12756   if (!bed->can_gc_sections
12757       || !is_elf_hash_table (info->hash))
12758     {
12759       (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12760       return TRUE;
12761     }
12762 
12763   bed->gc_keep (info);
12764   htab = elf_hash_table (info);
12765 
12766   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
12767      at the .eh_frame section if we can mark the FDEs individually.  */
12768   for (sub = info->input_bfds;
12769        info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12770        sub = sub->link.next)
12771     {
12772       asection *sec;
12773       struct elf_reloc_cookie cookie;
12774 
12775       sec = bfd_get_section_by_name (sub, ".eh_frame");
12776       while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12777 	{
12778 	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12779 	  if (elf_section_data (sec)->sec_info
12780 	      && (sec->flags & SEC_LINKER_CREATED) == 0)
12781 	    elf_eh_frame_section (sub) = sec;
12782 	  fini_reloc_cookie_for_section (&cookie, sec);
12783 	  sec = bfd_get_next_section_by_name (NULL, sec);
12784 	}
12785     }
12786 
12787   /* Apply transitive closure to the vtable entry usage info.  */
12788   elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12789   if (!ok)
12790     return FALSE;
12791 
12792   /* Kill the vtable relocations that were not used.  */
12793   elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12794   if (!ok)
12795     return FALSE;
12796 
12797   /* Mark dynamically referenced symbols.  */
12798   if (htab->dynamic_sections_created)
12799     elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12800 
12801   /* Grovel through relocs to find out who stays ...  */
12802   gc_mark_hook = bed->gc_mark_hook;
12803   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12804     {
12805       asection *o;
12806 
12807       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12808 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12809 	continue;
12810 
12811       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12812 	 Also treat note sections as a root, if the section is not part
12813 	 of a group.  */
12814       for (o = sub->sections; o != NULL; o = o->next)
12815 	if (!o->gc_mark
12816 	    && (o->flags & SEC_EXCLUDE) == 0
12817 	    && ((o->flags & SEC_KEEP) != 0
12818 		|| (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12819 		    && elf_next_in_group (o) == NULL )))
12820 	  {
12821 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12822 	      return FALSE;
12823 	  }
12824     }
12825 
12826   /* Allow the backend to mark additional target specific sections.  */
12827   bed->gc_mark_extra_sections (info, gc_mark_hook);
12828 
12829   /* ... and mark SEC_EXCLUDE for those that go.  */
12830   return elf_gc_sweep (abfd, info);
12831 }
12832 
12833 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
12834 
12835 bfd_boolean
12836 bfd_elf_gc_record_vtinherit (bfd *abfd,
12837 			     asection *sec,
12838 			     struct elf_link_hash_entry *h,
12839 			     bfd_vma offset)
12840 {
12841   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12842   struct elf_link_hash_entry **search, *child;
12843   bfd_size_type extsymcount;
12844   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12845 
12846   /* The sh_info field of the symtab header tells us where the
12847      external symbols start.  We don't care about the local symbols at
12848      this point.  */
12849   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12850   if (!elf_bad_symtab (abfd))
12851     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12852 
12853   sym_hashes = elf_sym_hashes (abfd);
12854   sym_hashes_end = sym_hashes + extsymcount;
12855 
12856   /* Hunt down the child symbol, which is in this section at the same
12857      offset as the relocation.  */
12858   for (search = sym_hashes; search != sym_hashes_end; ++search)
12859     {
12860       if ((child = *search) != NULL
12861 	  && (child->root.type == bfd_link_hash_defined
12862 	      || child->root.type == bfd_link_hash_defweak)
12863 	  && child->root.u.def.section == sec
12864 	  && child->root.u.def.value == offset)
12865 	goto win;
12866     }
12867 
12868   (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12869 			 abfd, sec, (unsigned long) offset);
12870   bfd_set_error (bfd_error_invalid_operation);
12871   return FALSE;
12872 
12873  win:
12874   if (!child->vtable)
12875     {
12876       child->vtable = ((struct elf_link_virtual_table_entry *)
12877 		       bfd_zalloc (abfd, sizeof (*child->vtable)));
12878       if (!child->vtable)
12879 	return FALSE;
12880     }
12881   if (!h)
12882     {
12883       /* This *should* only be the absolute section.  It could potentially
12884 	 be that someone has defined a non-global vtable though, which
12885 	 would be bad.  It isn't worth paging in the local symbols to be
12886 	 sure though; that case should simply be handled by the assembler.  */
12887 
12888       child->vtable->parent = (struct elf_link_hash_entry *) -1;
12889     }
12890   else
12891     child->vtable->parent = h;
12892 
12893   return TRUE;
12894 }
12895 
12896 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
12897 
12898 bfd_boolean
12899 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12900 			   asection *sec ATTRIBUTE_UNUSED,
12901 			   struct elf_link_hash_entry *h,
12902 			   bfd_vma addend)
12903 {
12904   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12905   unsigned int log_file_align = bed->s->log_file_align;
12906 
12907   if (!h->vtable)
12908     {
12909       h->vtable = ((struct elf_link_virtual_table_entry *)
12910 		   bfd_zalloc (abfd, sizeof (*h->vtable)));
12911       if (!h->vtable)
12912 	return FALSE;
12913     }
12914 
12915   if (addend >= h->vtable->size)
12916     {
12917       size_t size, bytes, file_align;
12918       bfd_boolean *ptr = h->vtable->used;
12919 
12920       /* While the symbol is undefined, we have to be prepared to handle
12921 	 a zero size.  */
12922       file_align = 1 << log_file_align;
12923       if (h->root.type == bfd_link_hash_undefined)
12924 	size = addend + file_align;
12925       else
12926 	{
12927 	  size = h->size;
12928 	  if (addend >= size)
12929 	    {
12930 	      /* Oops!  We've got a reference past the defined end of
12931 		 the table.  This is probably a bug -- shall we warn?  */
12932 	      size = addend + file_align;
12933 	    }
12934 	}
12935       size = (size + file_align - 1) & -file_align;
12936 
12937       /* Allocate one extra entry for use as a "done" flag for the
12938 	 consolidation pass.  */
12939       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12940 
12941       if (ptr)
12942 	{
12943 	  ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12944 
12945 	  if (ptr != NULL)
12946 	    {
12947 	      size_t oldbytes;
12948 
12949 	      oldbytes = (((h->vtable->size >> log_file_align) + 1)
12950 			  * sizeof (bfd_boolean));
12951 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12952 	    }
12953 	}
12954       else
12955 	ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12956 
12957       if (ptr == NULL)
12958 	return FALSE;
12959 
12960       /* And arrange for that done flag to be at index -1.  */
12961       h->vtable->used = ptr + 1;
12962       h->vtable->size = size;
12963     }
12964 
12965   h->vtable->used[addend >> log_file_align] = TRUE;
12966 
12967   return TRUE;
12968 }
12969 
12970 /* Map an ELF section header flag to its corresponding string.  */
12971 typedef struct
12972 {
12973   char *flag_name;
12974   flagword flag_value;
12975 } elf_flags_to_name_table;
12976 
12977 static elf_flags_to_name_table elf_flags_to_names [] =
12978 {
12979   { "SHF_WRITE", SHF_WRITE },
12980   { "SHF_ALLOC", SHF_ALLOC },
12981   { "SHF_EXECINSTR", SHF_EXECINSTR },
12982   { "SHF_MERGE", SHF_MERGE },
12983   { "SHF_STRINGS", SHF_STRINGS },
12984   { "SHF_INFO_LINK", SHF_INFO_LINK},
12985   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12986   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12987   { "SHF_GROUP", SHF_GROUP },
12988   { "SHF_TLS", SHF_TLS },
12989   { "SHF_MASKOS", SHF_MASKOS },
12990   { "SHF_EXCLUDE", SHF_EXCLUDE },
12991 };
12992 
12993 /* Returns TRUE if the section is to be included, otherwise FALSE.  */
12994 bfd_boolean
12995 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12996 			      struct flag_info *flaginfo,
12997 			      asection *section)
12998 {
12999   const bfd_vma sh_flags = elf_section_flags (section);
13000 
13001   if (!flaginfo->flags_initialized)
13002     {
13003       bfd *obfd = info->output_bfd;
13004       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13005       struct flag_info_list *tf = flaginfo->flag_list;
13006       int with_hex = 0;
13007       int without_hex = 0;
13008 
13009       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13010 	{
13011 	  unsigned i;
13012 	  flagword (*lookup) (char *);
13013 
13014 	  lookup = bed->elf_backend_lookup_section_flags_hook;
13015 	  if (lookup != NULL)
13016 	    {
13017 	      flagword hexval = (*lookup) ((char *) tf->name);
13018 
13019 	      if (hexval != 0)
13020 		{
13021 		  if (tf->with == with_flags)
13022 		    with_hex |= hexval;
13023 		  else if (tf->with == without_flags)
13024 		    without_hex |= hexval;
13025 		  tf->valid = TRUE;
13026 		  continue;
13027 		}
13028 	    }
13029 	  for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13030 	    {
13031 	      if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13032 		{
13033 		  if (tf->with == with_flags)
13034 		    with_hex |= elf_flags_to_names[i].flag_value;
13035 		  else if (tf->with == without_flags)
13036 		    without_hex |= elf_flags_to_names[i].flag_value;
13037 		  tf->valid = TRUE;
13038 		  break;
13039 		}
13040 	    }
13041 	  if (!tf->valid)
13042 	    {
13043 	      info->callbacks->einfo
13044 		(_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13045 	      return FALSE;
13046 	    }
13047 	}
13048       flaginfo->flags_initialized = TRUE;
13049       flaginfo->only_with_flags |= with_hex;
13050       flaginfo->not_with_flags |= without_hex;
13051     }
13052 
13053   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13054     return FALSE;
13055 
13056   if ((flaginfo->not_with_flags & sh_flags) != 0)
13057     return FALSE;
13058 
13059   return TRUE;
13060 }
13061 
13062 struct alloc_got_off_arg {
13063   bfd_vma gotoff;
13064   struct bfd_link_info *info;
13065 };
13066 
13067 /* We need a special top-level link routine to convert got reference counts
13068    to real got offsets.  */
13069 
13070 static bfd_boolean
13071 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13072 {
13073   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13074   bfd *obfd = gofarg->info->output_bfd;
13075   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13076 
13077   if (h->got.refcount > 0)
13078     {
13079       h->got.offset = gofarg->gotoff;
13080       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13081     }
13082   else
13083     h->got.offset = (bfd_vma) -1;
13084 
13085   return TRUE;
13086 }
13087 
13088 /* And an accompanying bit to work out final got entry offsets once
13089    we're done.  Should be called from final_link.  */
13090 
13091 bfd_boolean
13092 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13093 					struct bfd_link_info *info)
13094 {
13095   bfd *i;
13096   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13097   bfd_vma gotoff;
13098   struct alloc_got_off_arg gofarg;
13099 
13100   BFD_ASSERT (abfd == info->output_bfd);
13101 
13102   if (! is_elf_hash_table (info->hash))
13103     return FALSE;
13104 
13105   /* The GOT offset is relative to the .got section, but the GOT header is
13106      put into the .got.plt section, if the backend uses it.  */
13107   if (bed->want_got_plt)
13108     gotoff = 0;
13109   else
13110     gotoff = bed->got_header_size;
13111 
13112   /* Do the local .got entries first.  */
13113   for (i = info->input_bfds; i; i = i->link.next)
13114     {
13115       bfd_signed_vma *local_got;
13116       bfd_size_type j, locsymcount;
13117       Elf_Internal_Shdr *symtab_hdr;
13118 
13119       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13120 	continue;
13121 
13122       local_got = elf_local_got_refcounts (i);
13123       if (!local_got)
13124 	continue;
13125 
13126       symtab_hdr = &elf_tdata (i)->symtab_hdr;
13127       if (elf_bad_symtab (i))
13128 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13129       else
13130 	locsymcount = symtab_hdr->sh_info;
13131 
13132       for (j = 0; j < locsymcount; ++j)
13133 	{
13134 	  if (local_got[j] > 0)
13135 	    {
13136 	      local_got[j] = gotoff;
13137 	      gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13138 	    }
13139 	  else
13140 	    local_got[j] = (bfd_vma) -1;
13141 	}
13142     }
13143 
13144   /* Then the global .got entries.  .plt refcounts are handled by
13145      adjust_dynamic_symbol  */
13146   gofarg.gotoff = gotoff;
13147   gofarg.info = info;
13148   elf_link_hash_traverse (elf_hash_table (info),
13149 			  elf_gc_allocate_got_offsets,
13150 			  &gofarg);
13151   return TRUE;
13152 }
13153 
13154 /* Many folk need no more in the way of final link than this, once
13155    got entry reference counting is enabled.  */
13156 
13157 bfd_boolean
13158 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13159 {
13160   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13161     return FALSE;
13162 
13163   /* Invoke the regular ELF backend linker to do all the work.  */
13164   return bfd_elf_final_link (abfd, info);
13165 }
13166 
13167 bfd_boolean
13168 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13169 {
13170   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13171 
13172   if (rcookie->bad_symtab)
13173     rcookie->rel = rcookie->rels;
13174 
13175   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13176     {
13177       unsigned long r_symndx;
13178 
13179       if (! rcookie->bad_symtab)
13180 	if (rcookie->rel->r_offset > offset)
13181 	  return FALSE;
13182       if (rcookie->rel->r_offset != offset)
13183 	continue;
13184 
13185       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13186       if (r_symndx == STN_UNDEF)
13187 	return TRUE;
13188 
13189       if (r_symndx >= rcookie->locsymcount
13190 	  || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13191 	{
13192 	  struct elf_link_hash_entry *h;
13193 
13194 	  h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13195 
13196 	  while (h->root.type == bfd_link_hash_indirect
13197 		 || h->root.type == bfd_link_hash_warning)
13198 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
13199 
13200 	  if ((h->root.type == bfd_link_hash_defined
13201 	       || h->root.type == bfd_link_hash_defweak)
13202 	      && (h->root.u.def.section->owner != rcookie->abfd
13203 		  || h->root.u.def.section->kept_section != NULL
13204 		  || discarded_section (h->root.u.def.section)))
13205 	    return TRUE;
13206 	}
13207       else
13208 	{
13209 	  /* It's not a relocation against a global symbol,
13210 	     but it could be a relocation against a local
13211 	     symbol for a discarded section.  */
13212 	  asection *isec;
13213 	  Elf_Internal_Sym *isym;
13214 
13215 	  /* Need to: get the symbol; get the section.  */
13216 	  isym = &rcookie->locsyms[r_symndx];
13217 	  isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13218 	  if (isec != NULL
13219 	      && (isec->kept_section != NULL
13220 		  || discarded_section (isec)))
13221 	    return TRUE;
13222 	}
13223       return FALSE;
13224     }
13225   return FALSE;
13226 }
13227 
13228 /* Discard unneeded references to discarded sections.
13229    Returns -1 on error, 1 if any section's size was changed, 0 if
13230    nothing changed.  This function assumes that the relocations are in
13231    sorted order, which is true for all known assemblers.  */
13232 
13233 int
13234 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13235 {
13236   struct elf_reloc_cookie cookie;
13237   asection *o;
13238   bfd *abfd;
13239   int changed = 0;
13240 
13241   if (info->traditional_format
13242       || !is_elf_hash_table (info->hash))
13243     return 0;
13244 
13245   o = bfd_get_section_by_name (output_bfd, ".stab");
13246   if (o != NULL)
13247     {
13248       asection *i;
13249 
13250       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13251 	{
13252 	  if (i->size == 0
13253 	      || i->reloc_count == 0
13254 	      || i->sec_info_type != SEC_INFO_TYPE_STABS)
13255 	    continue;
13256 
13257 	  abfd = i->owner;
13258 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13259 	    continue;
13260 
13261 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
13262 	    return -1;
13263 
13264 	  if (_bfd_discard_section_stabs (abfd, i,
13265 					  elf_section_data (i)->sec_info,
13266 					  bfd_elf_reloc_symbol_deleted_p,
13267 					  &cookie))
13268 	    changed = 1;
13269 
13270 	  fini_reloc_cookie_for_section (&cookie, i);
13271 	}
13272     }
13273 
13274   o = NULL;
13275   if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13276     o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13277   if (o != NULL)
13278     {
13279       asection *i;
13280 
13281       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13282 	{
13283 	  if (i->size == 0)
13284 	    continue;
13285 
13286 	  abfd = i->owner;
13287 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13288 	    continue;
13289 
13290 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
13291 	    return -1;
13292 
13293 	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13294 	  if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13295 						 bfd_elf_reloc_symbol_deleted_p,
13296 						 &cookie))
13297 	    changed = 1;
13298 
13299 	  fini_reloc_cookie_for_section (&cookie, i);
13300 	}
13301     }
13302 
13303   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13304     {
13305       const struct elf_backend_data *bed;
13306 
13307       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13308 	continue;
13309 
13310       bed = get_elf_backend_data (abfd);
13311 
13312       if (bed->elf_backend_discard_info != NULL)
13313 	{
13314 	  if (!init_reloc_cookie (&cookie, info, abfd))
13315 	    return -1;
13316 
13317 	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13318 	    changed = 1;
13319 
13320 	  fini_reloc_cookie (&cookie, abfd);
13321 	}
13322     }
13323 
13324   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13325     _bfd_elf_end_eh_frame_parsing (info);
13326 
13327   if (info->eh_frame_hdr_type
13328       && !bfd_link_relocatable (info)
13329       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13330     changed = 1;
13331 
13332   return changed;
13333 }
13334 
13335 bfd_boolean
13336 _bfd_elf_section_already_linked (bfd *abfd,
13337 				 asection *sec,
13338 				 struct bfd_link_info *info)
13339 {
13340   flagword flags;
13341   const char *name, *key;
13342   struct bfd_section_already_linked *l;
13343   struct bfd_section_already_linked_hash_entry *already_linked_list;
13344 
13345   if (sec->output_section == bfd_abs_section_ptr)
13346     return FALSE;
13347 
13348   flags = sec->flags;
13349 
13350   /* Return if it isn't a linkonce section.  A comdat group section
13351      also has SEC_LINK_ONCE set.  */
13352   if ((flags & SEC_LINK_ONCE) == 0)
13353     return FALSE;
13354 
13355   /* Don't put group member sections on our list of already linked
13356      sections.  They are handled as a group via their group section.  */
13357   if (elf_sec_group (sec) != NULL)
13358     return FALSE;
13359 
13360   /* For a SHT_GROUP section, use the group signature as the key.  */
13361   name = sec->name;
13362   if ((flags & SEC_GROUP) != 0
13363       && elf_next_in_group (sec) != NULL
13364       && elf_group_name (elf_next_in_group (sec)) != NULL)
13365     key = elf_group_name (elf_next_in_group (sec));
13366   else
13367     {
13368       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
13369       if (CONST_STRNEQ (name, ".gnu.linkonce.")
13370 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13371 	key++;
13372       else
13373 	/* Must be a user linkonce section that doesn't follow gcc's
13374 	   naming convention.  In this case we won't be matching
13375 	   single member groups.  */
13376 	key = name;
13377     }
13378 
13379   already_linked_list = bfd_section_already_linked_table_lookup (key);
13380 
13381   for (l = already_linked_list->entry; l != NULL; l = l->next)
13382     {
13383       /* We may have 2 different types of sections on the list: group
13384 	 sections with a signature of <key> (<key> is some string),
13385 	 and linkonce sections named .gnu.linkonce.<type>.<key>.
13386 	 Match like sections.  LTO plugin sections are an exception.
13387 	 They are always named .gnu.linkonce.t.<key> and match either
13388 	 type of section.  */
13389       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13390 	   && ((flags & SEC_GROUP) != 0
13391 	       || strcmp (name, l->sec->name) == 0))
13392 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13393 	{
13394 	  /* The section has already been linked.  See if we should
13395 	     issue a warning.  */
13396 	  if (!_bfd_handle_already_linked (sec, l, info))
13397 	    return FALSE;
13398 
13399 	  if (flags & SEC_GROUP)
13400 	    {
13401 	      asection *first = elf_next_in_group (sec);
13402 	      asection *s = first;
13403 
13404 	      while (s != NULL)
13405 		{
13406 		  s->output_section = bfd_abs_section_ptr;
13407 		  /* Record which group discards it.  */
13408 		  s->kept_section = l->sec;
13409 		  s = elf_next_in_group (s);
13410 		  /* These lists are circular.  */
13411 		  if (s == first)
13412 		    break;
13413 		}
13414 	    }
13415 
13416 	  return TRUE;
13417 	}
13418     }
13419 
13420   /* A single member comdat group section may be discarded by a
13421      linkonce section and vice versa.  */
13422   if ((flags & SEC_GROUP) != 0)
13423     {
13424       asection *first = elf_next_in_group (sec);
13425 
13426       if (first != NULL && elf_next_in_group (first) == first)
13427 	/* Check this single member group against linkonce sections.  */
13428 	for (l = already_linked_list->entry; l != NULL; l = l->next)
13429 	  if ((l->sec->flags & SEC_GROUP) == 0
13430 	      && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13431 	    {
13432 	      first->output_section = bfd_abs_section_ptr;
13433 	      first->kept_section = l->sec;
13434 	      sec->output_section = bfd_abs_section_ptr;
13435 	      break;
13436 	    }
13437     }
13438   else
13439     /* Check this linkonce section against single member groups.  */
13440     for (l = already_linked_list->entry; l != NULL; l = l->next)
13441       if (l->sec->flags & SEC_GROUP)
13442 	{
13443 	  asection *first = elf_next_in_group (l->sec);
13444 
13445 	  if (first != NULL
13446 	      && elf_next_in_group (first) == first
13447 	      && bfd_elf_match_symbols_in_sections (first, sec, info))
13448 	    {
13449 	      sec->output_section = bfd_abs_section_ptr;
13450 	      sec->kept_section = first;
13451 	      break;
13452 	    }
13453 	}
13454 
13455   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13456      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13457      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13458      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
13459      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
13460      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13461      `.gnu.linkonce.t.F' section from a different bfd not requiring any
13462      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
13463      The reverse order cannot happen as there is never a bfd with only the
13464      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
13465      matter as here were are looking only for cross-bfd sections.  */
13466 
13467   if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13468     for (l = already_linked_list->entry; l != NULL; l = l->next)
13469       if ((l->sec->flags & SEC_GROUP) == 0
13470 	  && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13471 	{
13472 	  if (abfd != l->sec->owner)
13473 	    sec->output_section = bfd_abs_section_ptr;
13474 	  break;
13475 	}
13476 
13477   /* This is the first section with this name.  Record it.  */
13478   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13479     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13480   return sec->output_section == bfd_abs_section_ptr;
13481 }
13482 
13483 bfd_boolean
13484 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13485 {
13486   return sym->st_shndx == SHN_COMMON;
13487 }
13488 
13489 unsigned int
13490 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13491 {
13492   return SHN_COMMON;
13493 }
13494 
13495 asection *
13496 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13497 {
13498   return bfd_com_section_ptr;
13499 }
13500 
13501 bfd_vma
13502 _bfd_elf_default_got_elt_size (bfd *abfd,
13503 			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
13504 			       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13505 			       bfd *ibfd ATTRIBUTE_UNUSED,
13506 			       unsigned long symndx ATTRIBUTE_UNUSED)
13507 {
13508   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13509   return bed->s->arch_size / 8;
13510 }
13511 
13512 /* Routines to support the creation of dynamic relocs.  */
13513 
13514 /* Returns the name of the dynamic reloc section associated with SEC.  */
13515 
13516 static const char *
13517 get_dynamic_reloc_section_name (bfd *       abfd,
13518 				asection *  sec,
13519 				bfd_boolean is_rela)
13520 {
13521   char *name;
13522   const char *old_name = bfd_get_section_name (NULL, sec);
13523   const char *prefix = is_rela ? ".rela" : ".rel";
13524 
13525   if (old_name == NULL)
13526     return NULL;
13527 
13528   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13529   sprintf (name, "%s%s", prefix, old_name);
13530 
13531   return name;
13532 }
13533 
13534 /* Returns the dynamic reloc section associated with SEC.
13535    If necessary compute the name of the dynamic reloc section based
13536    on SEC's name (looked up in ABFD's string table) and the setting
13537    of IS_RELA.  */
13538 
13539 asection *
13540 _bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
13541 				    asection *  sec,
13542 				    bfd_boolean is_rela)
13543 {
13544   asection * reloc_sec = elf_section_data (sec)->sreloc;
13545 
13546   if (reloc_sec == NULL)
13547     {
13548       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13549 
13550       if (name != NULL)
13551 	{
13552 	  reloc_sec = bfd_get_linker_section (abfd, name);
13553 
13554 	  if (reloc_sec != NULL)
13555 	    elf_section_data (sec)->sreloc = reloc_sec;
13556 	}
13557     }
13558 
13559   return reloc_sec;
13560 }
13561 
13562 /* Returns the dynamic reloc section associated with SEC.  If the
13563    section does not exist it is created and attached to the DYNOBJ
13564    bfd and stored in the SRELOC field of SEC's elf_section_data
13565    structure.
13566 
13567    ALIGNMENT is the alignment for the newly created section and
13568    IS_RELA defines whether the name should be .rela.<SEC's name>
13569    or .rel.<SEC's name>.  The section name is looked up in the
13570    string table associated with ABFD.  */
13571 
13572 asection *
13573 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13574 				     bfd *dynobj,
13575 				     unsigned int alignment,
13576 				     bfd *abfd,
13577 				     bfd_boolean is_rela)
13578 {
13579   asection * reloc_sec = elf_section_data (sec)->sreloc;
13580 
13581   if (reloc_sec == NULL)
13582     {
13583       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13584 
13585       if (name == NULL)
13586 	return NULL;
13587 
13588       reloc_sec = bfd_get_linker_section (dynobj, name);
13589 
13590       if (reloc_sec == NULL)
13591 	{
13592 	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13593 			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13594 	  if ((sec->flags & SEC_ALLOC) != 0)
13595 	    flags |= SEC_ALLOC | SEC_LOAD;
13596 
13597 	  reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13598 	  if (reloc_sec != NULL)
13599 	    {
13600 	      /* _bfd_elf_get_sec_type_attr chooses a section type by
13601 		 name.  Override as it may be wrong, eg. for a user
13602 		 section named "auto" we'll get ".relauto" which is
13603 		 seen to be a .rela section.  */
13604 	      elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13605 	      if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13606 		reloc_sec = NULL;
13607 	    }
13608 	}
13609 
13610       elf_section_data (sec)->sreloc = reloc_sec;
13611     }
13612 
13613   return reloc_sec;
13614 }
13615 
13616 /* Copy the ELF symbol type and other attributes for a linker script
13617    assignment from HSRC to HDEST.  Generally this should be treated as
13618    if we found a strong non-dynamic definition for HDEST (except that
13619    ld ignores multiple definition errors).  */
13620 void
13621 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13622 				     struct bfd_link_hash_entry *hdest,
13623 				     struct bfd_link_hash_entry *hsrc)
13624 {
13625   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13626   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13627   Elf_Internal_Sym isym;
13628 
13629   ehdest->type = ehsrc->type;
13630   ehdest->target_internal = ehsrc->target_internal;
13631 
13632   isym.st_other = ehsrc->other;
13633   elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13634 }
13635 
13636 /* Append a RELA relocation REL to section S in BFD.  */
13637 
13638 void
13639 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13640 {
13641   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13642   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13643   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13644   bed->s->swap_reloca_out (abfd, rel, loc);
13645 }
13646 
13647 /* Append a REL relocation REL to section S in BFD.  */
13648 
13649 void
13650 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13651 {
13652   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13653   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13654   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13655   bed->s->swap_reloc_out (abfd, rel, loc);
13656 }
13657