1 /* ELF linking support for BFD.
2    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3    2005, 2006 Free Software Foundation, Inc.
4 
5    This file is part of BFD, the Binary File Descriptor library.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20 
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 
31 /* Define a symbol in a dynamic linkage section.  */
32 
33 struct elf_link_hash_entry *
34 _bfd_elf_define_linkage_sym (bfd *abfd,
35 			     struct bfd_link_info *info,
36 			     asection *sec,
37 			     const char *name)
38 {
39   struct elf_link_hash_entry *h;
40   struct bfd_link_hash_entry *bh;
41   const struct elf_backend_data *bed;
42 
43   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
44   if (h != NULL)
45     {
46       /* Zap symbol defined in an as-needed lib that wasn't linked.
47 	 This is a symptom of a larger problem:  Absolute symbols
48 	 defined in shared libraries can't be overridden, because we
49 	 lose the link to the bfd which is via the symbol section.  */
50       h->root.type = bfd_link_hash_new;
51     }
52 
53   bh = &h->root;
54   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
55 					 sec, 0, NULL, FALSE,
56 					 get_elf_backend_data (abfd)->collect,
57 					 &bh))
58     return NULL;
59   h = (struct elf_link_hash_entry *) bh;
60   h->def_regular = 1;
61   h->type = STT_OBJECT;
62   h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
63 
64   bed = get_elf_backend_data (abfd);
65   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
66   return h;
67 }
68 
69 bfd_boolean
70 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
71 {
72   flagword flags;
73   asection *s;
74   struct elf_link_hash_entry *h;
75   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
76   int ptralign;
77 
78   /* This function may be called more than once.  */
79   s = bfd_get_section_by_name (abfd, ".got");
80   if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
81     return TRUE;
82 
83   switch (bed->s->arch_size)
84     {
85     case 32:
86       ptralign = 2;
87       break;
88 
89     case 64:
90       ptralign = 3;
91       break;
92 
93     default:
94       bfd_set_error (bfd_error_bad_value);
95       return FALSE;
96     }
97 
98   flags = bed->dynamic_sec_flags;
99 
100   s = bfd_make_section_with_flags (abfd, ".got", flags);
101   if (s == NULL
102       || !bfd_set_section_alignment (abfd, s, ptralign))
103     return FALSE;
104 
105   if (bed->want_got_plt)
106     {
107       s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
108       if (s == NULL
109 	  || !bfd_set_section_alignment (abfd, s, ptralign))
110 	return FALSE;
111     }
112 
113   if (bed->want_got_sym)
114     {
115       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
116 	 (or .got.plt) section.  We don't do this in the linker script
117 	 because we don't want to define the symbol if we are not creating
118 	 a global offset table.  */
119       h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
120       elf_hash_table (info)->hgot = h;
121       if (h == NULL)
122 	return FALSE;
123     }
124 
125   /* The first bit of the global offset table is the header.  */
126   s->size += bed->got_header_size;
127 
128   return TRUE;
129 }
130 
131 /* Create a strtab to hold the dynamic symbol names.  */
132 static bfd_boolean
133 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
134 {
135   struct elf_link_hash_table *hash_table;
136 
137   hash_table = elf_hash_table (info);
138   if (hash_table->dynobj == NULL)
139     hash_table->dynobj = abfd;
140 
141   if (hash_table->dynstr == NULL)
142     {
143       hash_table->dynstr = _bfd_elf_strtab_init ();
144       if (hash_table->dynstr == NULL)
145 	return FALSE;
146     }
147   return TRUE;
148 }
149 
150 /* Create some sections which will be filled in with dynamic linking
151    information.  ABFD is an input file which requires dynamic sections
152    to be created.  The dynamic sections take up virtual memory space
153    when the final executable is run, so we need to create them before
154    addresses are assigned to the output sections.  We work out the
155    actual contents and size of these sections later.  */
156 
157 bfd_boolean
158 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
159 {
160   flagword flags;
161   register asection *s;
162   const struct elf_backend_data *bed;
163 
164   if (! is_elf_hash_table (info->hash))
165     return FALSE;
166 
167   if (elf_hash_table (info)->dynamic_sections_created)
168     return TRUE;
169 
170   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
171     return FALSE;
172 
173   abfd = elf_hash_table (info)->dynobj;
174   bed = get_elf_backend_data (abfd);
175 
176   flags = bed->dynamic_sec_flags;
177 
178   /* A dynamically linked executable has a .interp section, but a
179      shared library does not.  */
180   if (info->executable && !info->static_link)
181     {
182       s = bfd_make_section_with_flags (abfd, ".interp",
183 				       flags | SEC_READONLY);
184       if (s == NULL)
185 	return FALSE;
186     }
187 
188   if (! info->traditional_format)
189     {
190       s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr",
191 				       flags | SEC_READONLY);
192       if (s == NULL
193 	  || ! bfd_set_section_alignment (abfd, s, 2))
194 	return FALSE;
195       elf_hash_table (info)->eh_info.hdr_sec = s;
196     }
197 
198   /* Create sections to hold version informations.  These are removed
199      if they are not needed.  */
200   s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
201 				   flags | SEC_READONLY);
202   if (s == NULL
203       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
204     return FALSE;
205 
206   s = bfd_make_section_with_flags (abfd, ".gnu.version",
207 				   flags | SEC_READONLY);
208   if (s == NULL
209       || ! bfd_set_section_alignment (abfd, s, 1))
210     return FALSE;
211 
212   s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
213 				   flags | SEC_READONLY);
214   if (s == NULL
215       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
216     return FALSE;
217 
218   s = bfd_make_section_with_flags (abfd, ".dynsym",
219 				   flags | SEC_READONLY);
220   if (s == NULL
221       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
222     return FALSE;
223 
224   s = bfd_make_section_with_flags (abfd, ".dynstr",
225 				   flags | SEC_READONLY);
226   if (s == NULL)
227     return FALSE;
228 
229   s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
230   if (s == NULL
231       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
232     return FALSE;
233 
234   /* The special symbol _DYNAMIC is always set to the start of the
235      .dynamic section.  We could set _DYNAMIC in a linker script, but we
236      only want to define it if we are, in fact, creating a .dynamic
237      section.  We don't want to define it if there is no .dynamic
238      section, since on some ELF platforms the start up code examines it
239      to decide how to initialize the process.  */
240   if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
241     return FALSE;
242 
243   if (info->emit_hash)
244     {
245       s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
246       if (s == NULL
247 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
248 	return FALSE;
249       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
250     }
251 
252   if (info->emit_gnu_hash)
253     {
254       s = bfd_make_section_with_flags (abfd, ".gnu.hash",
255 				       flags | SEC_READONLY);
256       if (s == NULL
257 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
258 	return FALSE;
259       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
260 	 4 32-bit words followed by variable count of 64-bit words, then
261 	 variable count of 32-bit words.  */
262       if (bed->s->arch_size == 64)
263 	elf_section_data (s)->this_hdr.sh_entsize = 0;
264       else
265 	elf_section_data (s)->this_hdr.sh_entsize = 4;
266     }
267 
268   /* Let the backend create the rest of the sections.  This lets the
269      backend set the right flags.  The backend will normally create
270      the .got and .plt sections.  */
271   if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
272     return FALSE;
273 
274   elf_hash_table (info)->dynamic_sections_created = TRUE;
275 
276   return TRUE;
277 }
278 
279 /* Create dynamic sections when linking against a dynamic object.  */
280 
281 bfd_boolean
282 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
283 {
284   flagword flags, pltflags;
285   struct elf_link_hash_entry *h;
286   asection *s;
287   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
288 
289   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
290      .rel[a].bss sections.  */
291   flags = bed->dynamic_sec_flags;
292 
293   pltflags = flags;
294   if (bed->plt_not_loaded)
295     /* We do not clear SEC_ALLOC here because we still want the OS to
296        allocate space for the section; it's just that there's nothing
297        to read in from the object file.  */
298     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
299   else
300     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
301   if (bed->plt_readonly)
302     pltflags |= SEC_READONLY;
303 
304   s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
305   if (s == NULL
306       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
307     return FALSE;
308 
309   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
310      .plt section.  */
311   if (bed->want_plt_sym)
312     {
313       h = _bfd_elf_define_linkage_sym (abfd, info, s,
314 				       "_PROCEDURE_LINKAGE_TABLE_");
315       elf_hash_table (info)->hplt = h;
316       if (h == NULL)
317 	return FALSE;
318     }
319 
320   s = bfd_make_section_with_flags (abfd,
321 				   (bed->default_use_rela_p
322 				    ? ".rela.plt" : ".rel.plt"),
323 				   flags | SEC_READONLY);
324   if (s == NULL
325       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
326     return FALSE;
327 
328   if (! _bfd_elf_create_got_section (abfd, info))
329     return FALSE;
330 
331   if (bed->want_dynbss)
332     {
333       /* The .dynbss section is a place to put symbols which are defined
334 	 by dynamic objects, are referenced by regular objects, and are
335 	 not functions.  We must allocate space for them in the process
336 	 image and use a R_*_COPY reloc to tell the dynamic linker to
337 	 initialize them at run time.  The linker script puts the .dynbss
338 	 section into the .bss section of the final image.  */
339       s = bfd_make_section_with_flags (abfd, ".dynbss",
340 				       (SEC_ALLOC
341 					| SEC_LINKER_CREATED));
342       if (s == NULL)
343 	return FALSE;
344 
345       /* The .rel[a].bss section holds copy relocs.  This section is not
346 	 normally needed.  We need to create it here, though, so that the
347 	 linker will map it to an output section.  We can't just create it
348 	 only if we need it, because we will not know whether we need it
349 	 until we have seen all the input files, and the first time the
350 	 main linker code calls BFD after examining all the input files
351 	 (size_dynamic_sections) the input sections have already been
352 	 mapped to the output sections.  If the section turns out not to
353 	 be needed, we can discard it later.  We will never need this
354 	 section when generating a shared object, since they do not use
355 	 copy relocs.  */
356       if (! info->shared)
357 	{
358 	  s = bfd_make_section_with_flags (abfd,
359 					   (bed->default_use_rela_p
360 					    ? ".rela.bss" : ".rel.bss"),
361 					   flags | SEC_READONLY);
362 	  if (s == NULL
363 	      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
364 	    return FALSE;
365 	}
366     }
367 
368   return TRUE;
369 }
370 
371 /* Record a new dynamic symbol.  We record the dynamic symbols as we
372    read the input files, since we need to have a list of all of them
373    before we can determine the final sizes of the output sections.
374    Note that we may actually call this function even though we are not
375    going to output any dynamic symbols; in some cases we know that a
376    symbol should be in the dynamic symbol table, but only if there is
377    one.  */
378 
379 bfd_boolean
380 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
381 				    struct elf_link_hash_entry *h)
382 {
383   if (h->dynindx == -1)
384     {
385       struct elf_strtab_hash *dynstr;
386       char *p;
387       const char *name;
388       bfd_size_type indx;
389 
390       /* XXX: The ABI draft says the linker must turn hidden and
391 	 internal symbols into STB_LOCAL symbols when producing the
392 	 DSO. However, if ld.so honors st_other in the dynamic table,
393 	 this would not be necessary.  */
394       switch (ELF_ST_VISIBILITY (h->other))
395 	{
396 	case STV_INTERNAL:
397 	case STV_HIDDEN:
398 	  if (h->root.type != bfd_link_hash_undefined
399 	      && h->root.type != bfd_link_hash_undefweak)
400 	    {
401 	      h->forced_local = 1;
402 	      if (!elf_hash_table (info)->is_relocatable_executable)
403 		return TRUE;
404 	    }
405 
406 	default:
407 	  break;
408 	}
409 
410       h->dynindx = elf_hash_table (info)->dynsymcount;
411       ++elf_hash_table (info)->dynsymcount;
412 
413       dynstr = elf_hash_table (info)->dynstr;
414       if (dynstr == NULL)
415 	{
416 	  /* Create a strtab to hold the dynamic symbol names.  */
417 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
418 	  if (dynstr == NULL)
419 	    return FALSE;
420 	}
421 
422       /* We don't put any version information in the dynamic string
423 	 table.  */
424       name = h->root.root.string;
425       p = strchr (name, ELF_VER_CHR);
426       if (p != NULL)
427 	/* We know that the p points into writable memory.  In fact,
428 	   there are only a few symbols that have read-only names, being
429 	   those like _GLOBAL_OFFSET_TABLE_ that are created specially
430 	   by the backends.  Most symbols will have names pointing into
431 	   an ELF string table read from a file, or to objalloc memory.  */
432 	*p = 0;
433 
434       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
435 
436       if (p != NULL)
437 	*p = ELF_VER_CHR;
438 
439       if (indx == (bfd_size_type) -1)
440 	return FALSE;
441       h->dynstr_index = indx;
442     }
443 
444   return TRUE;
445 }
446 
447 /* Record an assignment to a symbol made by a linker script.  We need
448    this in case some dynamic object refers to this symbol.  */
449 
450 bfd_boolean
451 bfd_elf_record_link_assignment (bfd *output_bfd,
452 				struct bfd_link_info *info,
453 				const char *name,
454 				bfd_boolean provide,
455 				bfd_boolean hidden)
456 {
457   struct elf_link_hash_entry *h;
458   struct elf_link_hash_table *htab;
459 
460   if (!is_elf_hash_table (info->hash))
461     return TRUE;
462 
463   htab = elf_hash_table (info);
464   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
465   if (h == NULL)
466     return provide;
467 
468   /* Since we're defining the symbol, don't let it seem to have not
469      been defined.  record_dynamic_symbol and size_dynamic_sections
470      may depend on this.  */
471   if (h->root.type == bfd_link_hash_undefweak
472       || h->root.type == bfd_link_hash_undefined)
473     {
474       h->root.type = bfd_link_hash_new;
475       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
476 	bfd_link_repair_undef_list (&htab->root);
477     }
478 
479   if (h->root.type == bfd_link_hash_new)
480     h->non_elf = 0;
481 
482   /* If this symbol is being provided by the linker script, and it is
483      currently defined by a dynamic object, but not by a regular
484      object, then mark it as undefined so that the generic linker will
485      force the correct value.  */
486   if (provide
487       && h->def_dynamic
488       && !h->def_regular)
489     h->root.type = bfd_link_hash_undefined;
490 
491   /* If this symbol is not being provided by the linker script, and it is
492      currently defined by a dynamic object, but not by a regular object,
493      then clear out any version information because the symbol will not be
494      associated with the dynamic object any more.  */
495   if (!provide
496       && h->def_dynamic
497       && !h->def_regular)
498     h->verinfo.verdef = NULL;
499 
500   h->def_regular = 1;
501 
502   if (provide && hidden)
503     {
504       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
505 
506       h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
507       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
508     }
509 
510   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
511      and executables.  */
512   if (!info->relocatable
513       && h->dynindx != -1
514       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
515 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
516     h->forced_local = 1;
517 
518   if ((h->def_dynamic
519        || h->ref_dynamic
520        || info->shared
521        || (info->executable && elf_hash_table (info)->is_relocatable_executable))
522       && h->dynindx == -1)
523     {
524       if (! bfd_elf_link_record_dynamic_symbol (info, h))
525 	return FALSE;
526 
527       /* If this is a weak defined symbol, and we know a corresponding
528 	 real symbol from the same dynamic object, make sure the real
529 	 symbol is also made into a dynamic symbol.  */
530       if (h->u.weakdef != NULL
531 	  && h->u.weakdef->dynindx == -1)
532 	{
533 	  if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
534 	    return FALSE;
535 	}
536     }
537 
538   return TRUE;
539 }
540 
541 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
542    success, and 2 on a failure caused by attempting to record a symbol
543    in a discarded section, eg. a discarded link-once section symbol.  */
544 
545 int
546 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
547 					  bfd *input_bfd,
548 					  long input_indx)
549 {
550   bfd_size_type amt;
551   struct elf_link_local_dynamic_entry *entry;
552   struct elf_link_hash_table *eht;
553   struct elf_strtab_hash *dynstr;
554   unsigned long dynstr_index;
555   char *name;
556   Elf_External_Sym_Shndx eshndx;
557   char esym[sizeof (Elf64_External_Sym)];
558 
559   if (! is_elf_hash_table (info->hash))
560     return 0;
561 
562   /* See if the entry exists already.  */
563   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
564     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
565       return 1;
566 
567   amt = sizeof (*entry);
568   entry = bfd_alloc (input_bfd, amt);
569   if (entry == NULL)
570     return 0;
571 
572   /* Go find the symbol, so that we can find it's name.  */
573   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
574 			     1, input_indx, &entry->isym, esym, &eshndx))
575     {
576       bfd_release (input_bfd, entry);
577       return 0;
578     }
579 
580   if (entry->isym.st_shndx != SHN_UNDEF
581       && (entry->isym.st_shndx < SHN_LORESERVE
582 	  || entry->isym.st_shndx > SHN_HIRESERVE))
583     {
584       asection *s;
585 
586       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
587       if (s == NULL || bfd_is_abs_section (s->output_section))
588 	{
589 	  /* We can still bfd_release here as nothing has done another
590 	     bfd_alloc.  We can't do this later in this function.  */
591 	  bfd_release (input_bfd, entry);
592 	  return 2;
593 	}
594     }
595 
596   name = (bfd_elf_string_from_elf_section
597 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
598 	   entry->isym.st_name));
599 
600   dynstr = elf_hash_table (info)->dynstr;
601   if (dynstr == NULL)
602     {
603       /* Create a strtab to hold the dynamic symbol names.  */
604       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
605       if (dynstr == NULL)
606 	return 0;
607     }
608 
609   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
610   if (dynstr_index == (unsigned long) -1)
611     return 0;
612   entry->isym.st_name = dynstr_index;
613 
614   eht = elf_hash_table (info);
615 
616   entry->next = eht->dynlocal;
617   eht->dynlocal = entry;
618   entry->input_bfd = input_bfd;
619   entry->input_indx = input_indx;
620   eht->dynsymcount++;
621 
622   /* Whatever binding the symbol had before, it's now local.  */
623   entry->isym.st_info
624     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
625 
626   /* The dynindx will be set at the end of size_dynamic_sections.  */
627 
628   return 1;
629 }
630 
631 /* Return the dynindex of a local dynamic symbol.  */
632 
633 long
634 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
635 				    bfd *input_bfd,
636 				    long input_indx)
637 {
638   struct elf_link_local_dynamic_entry *e;
639 
640   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
641     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
642       return e->dynindx;
643   return -1;
644 }
645 
646 /* This function is used to renumber the dynamic symbols, if some of
647    them are removed because they are marked as local.  This is called
648    via elf_link_hash_traverse.  */
649 
650 static bfd_boolean
651 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
652 				      void *data)
653 {
654   size_t *count = data;
655 
656   if (h->root.type == bfd_link_hash_warning)
657     h = (struct elf_link_hash_entry *) h->root.u.i.link;
658 
659   if (h->forced_local)
660     return TRUE;
661 
662   if (h->dynindx != -1)
663     h->dynindx = ++(*count);
664 
665   return TRUE;
666 }
667 
668 
669 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
670    STB_LOCAL binding.  */
671 
672 static bfd_boolean
673 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
674 					    void *data)
675 {
676   size_t *count = data;
677 
678   if (h->root.type == bfd_link_hash_warning)
679     h = (struct elf_link_hash_entry *) h->root.u.i.link;
680 
681   if (!h->forced_local)
682     return TRUE;
683 
684   if (h->dynindx != -1)
685     h->dynindx = ++(*count);
686 
687   return TRUE;
688 }
689 
690 /* Return true if the dynamic symbol for a given section should be
691    omitted when creating a shared library.  */
692 bfd_boolean
693 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
694 				   struct bfd_link_info *info,
695 				   asection *p)
696 {
697   switch (elf_section_data (p)->this_hdr.sh_type)
698     {
699     case SHT_PROGBITS:
700     case SHT_NOBITS:
701       /* If sh_type is yet undecided, assume it could be
702 	 SHT_PROGBITS/SHT_NOBITS.  */
703     case SHT_NULL:
704       if (strcmp (p->name, ".got") == 0
705 	  || strcmp (p->name, ".got.plt") == 0
706 	  || strcmp (p->name, ".plt") == 0)
707 	{
708 	  asection *ip;
709 	  bfd *dynobj = elf_hash_table (info)->dynobj;
710 
711 	  if (dynobj != NULL
712 	      && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
713 	      && (ip->flags & SEC_LINKER_CREATED)
714 	      && ip->output_section == p)
715 	    return TRUE;
716 	}
717       return FALSE;
718 
719       /* There shouldn't be section relative relocations
720 	 against any other section.  */
721     default:
722       return TRUE;
723     }
724 }
725 
726 /* Assign dynsym indices.  In a shared library we generate a section
727    symbol for each output section, which come first.  Next come symbols
728    which have been forced to local binding.  Then all of the back-end
729    allocated local dynamic syms, followed by the rest of the global
730    symbols.  */
731 
732 static unsigned long
733 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
734 				struct bfd_link_info *info,
735 				unsigned long *section_sym_count)
736 {
737   unsigned long dynsymcount = 0;
738 
739   if (info->shared || elf_hash_table (info)->is_relocatable_executable)
740     {
741       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
742       asection *p;
743       for (p = output_bfd->sections; p ; p = p->next)
744 	if ((p->flags & SEC_EXCLUDE) == 0
745 	    && (p->flags & SEC_ALLOC) != 0
746 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
747 	  elf_section_data (p)->dynindx = ++dynsymcount;
748     }
749   *section_sym_count = dynsymcount;
750 
751   elf_link_hash_traverse (elf_hash_table (info),
752 			  elf_link_renumber_local_hash_table_dynsyms,
753 			  &dynsymcount);
754 
755   if (elf_hash_table (info)->dynlocal)
756     {
757       struct elf_link_local_dynamic_entry *p;
758       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
759 	p->dynindx = ++dynsymcount;
760     }
761 
762   elf_link_hash_traverse (elf_hash_table (info),
763 			  elf_link_renumber_hash_table_dynsyms,
764 			  &dynsymcount);
765 
766   /* There is an unused NULL entry at the head of the table which
767      we must account for in our count.  Unless there weren't any
768      symbols, which means we'll have no table at all.  */
769   if (dynsymcount != 0)
770     ++dynsymcount;
771 
772   elf_hash_table (info)->dynsymcount = dynsymcount;
773   return dynsymcount;
774 }
775 
776 /* This function is called when we want to define a new symbol.  It
777    handles the various cases which arise when we find a definition in
778    a dynamic object, or when there is already a definition in a
779    dynamic object.  The new symbol is described by NAME, SYM, PSEC,
780    and PVALUE.  We set SYM_HASH to the hash table entry.  We set
781    OVERRIDE if the old symbol is overriding a new definition.  We set
782    TYPE_CHANGE_OK if it is OK for the type to change.  We set
783    SIZE_CHANGE_OK if it is OK for the size to change.  By OK to
784    change, we mean that we shouldn't warn if the type or size does
785    change.  We set POLD_ALIGNMENT if an old common symbol in a dynamic
786    object is overridden by a regular object.  */
787 
788 bfd_boolean
789 _bfd_elf_merge_symbol (bfd *abfd,
790 		       struct bfd_link_info *info,
791 		       const char *name,
792 		       Elf_Internal_Sym *sym,
793 		       asection **psec,
794 		       bfd_vma *pvalue,
795 		       unsigned int *pold_alignment,
796 		       struct elf_link_hash_entry **sym_hash,
797 		       bfd_boolean *skip,
798 		       bfd_boolean *override,
799 		       bfd_boolean *type_change_ok,
800 		       bfd_boolean *size_change_ok)
801 {
802   asection *sec, *oldsec;
803   struct elf_link_hash_entry *h;
804   struct elf_link_hash_entry *flip;
805   int bind;
806   bfd *oldbfd;
807   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
808   bfd_boolean newweak, oldweak;
809   const struct elf_backend_data *bed;
810 
811   *skip = FALSE;
812   *override = FALSE;
813 
814   sec = *psec;
815   bind = ELF_ST_BIND (sym->st_info);
816 
817   if (! bfd_is_und_section (sec))
818     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
819   else
820     h = ((struct elf_link_hash_entry *)
821 	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
822   if (h == NULL)
823     return FALSE;
824   *sym_hash = h;
825 
826   /* This code is for coping with dynamic objects, and is only useful
827      if we are doing an ELF link.  */
828   if (info->hash->creator != abfd->xvec)
829     return TRUE;
830 
831   /* For merging, we only care about real symbols.  */
832 
833   while (h->root.type == bfd_link_hash_indirect
834 	 || h->root.type == bfd_link_hash_warning)
835     h = (struct elf_link_hash_entry *) h->root.u.i.link;
836 
837   /* If we just created the symbol, mark it as being an ELF symbol.
838      Other than that, there is nothing to do--there is no merge issue
839      with a newly defined symbol--so we just return.  */
840 
841   if (h->root.type == bfd_link_hash_new)
842     {
843       h->non_elf = 0;
844       return TRUE;
845     }
846 
847   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
848      existing symbol.  */
849 
850   switch (h->root.type)
851     {
852     default:
853       oldbfd = NULL;
854       oldsec = NULL;
855       break;
856 
857     case bfd_link_hash_undefined:
858     case bfd_link_hash_undefweak:
859       oldbfd = h->root.u.undef.abfd;
860       oldsec = NULL;
861       break;
862 
863     case bfd_link_hash_defined:
864     case bfd_link_hash_defweak:
865       oldbfd = h->root.u.def.section->owner;
866       oldsec = h->root.u.def.section;
867       break;
868 
869     case bfd_link_hash_common:
870       oldbfd = h->root.u.c.p->section->owner;
871       oldsec = h->root.u.c.p->section;
872       break;
873     }
874 
875   /* In cases involving weak versioned symbols, we may wind up trying
876      to merge a symbol with itself.  Catch that here, to avoid the
877      confusion that results if we try to override a symbol with
878      itself.  The additional tests catch cases like
879      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
880      dynamic object, which we do want to handle here.  */
881   if (abfd == oldbfd
882       && ((abfd->flags & DYNAMIC) == 0
883 	  || !h->def_regular))
884     return TRUE;
885 
886   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
887      respectively, is from a dynamic object.  */
888 
889   newdyn = (abfd->flags & DYNAMIC) != 0;
890 
891   olddyn = FALSE;
892   if (oldbfd != NULL)
893     olddyn = (oldbfd->flags & DYNAMIC) != 0;
894   else if (oldsec != NULL)
895     {
896       /* This handles the special SHN_MIPS_{TEXT,DATA} section
897 	 indices used by MIPS ELF.  */
898       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
899     }
900 
901   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
902      respectively, appear to be a definition rather than reference.  */
903 
904   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
905 
906   olddef = (h->root.type != bfd_link_hash_undefined
907 	    && h->root.type != bfd_link_hash_undefweak
908 	    && h->root.type != bfd_link_hash_common);
909 
910   /* When we try to create a default indirect symbol from the dynamic
911      definition with the default version, we skip it if its type and
912      the type of existing regular definition mismatch.  We only do it
913      if the existing regular definition won't be dynamic.  */
914   if (pold_alignment == NULL
915       && !info->shared
916       && !info->export_dynamic
917       && !h->ref_dynamic
918       && newdyn
919       && newdef
920       && !olddyn
921       && (olddef || h->root.type == bfd_link_hash_common)
922       && ELF_ST_TYPE (sym->st_info) != h->type
923       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
924       && h->type != STT_NOTYPE)
925     {
926       *skip = TRUE;
927       return TRUE;
928     }
929 
930   /* Check TLS symbol.  We don't check undefined symbol introduced by
931      "ld -u".  */
932   if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
933       && ELF_ST_TYPE (sym->st_info) != h->type
934       && oldbfd != NULL)
935     {
936       bfd *ntbfd, *tbfd;
937       bfd_boolean ntdef, tdef;
938       asection *ntsec, *tsec;
939 
940       if (h->type == STT_TLS)
941 	{
942 	  ntbfd = abfd;
943 	  ntsec = sec;
944 	  ntdef = newdef;
945 	  tbfd = oldbfd;
946 	  tsec = oldsec;
947 	  tdef = olddef;
948 	}
949       else
950 	{
951 	  ntbfd = oldbfd;
952 	  ntsec = oldsec;
953 	  ntdef = olddef;
954 	  tbfd = abfd;
955 	  tsec = sec;
956 	  tdef = newdef;
957 	}
958 
959       if (tdef && ntdef)
960 	(*_bfd_error_handler)
961 	  (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
962 	   tbfd, tsec, ntbfd, ntsec, h->root.root.string);
963       else if (!tdef && !ntdef)
964 	(*_bfd_error_handler)
965 	  (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
966 	   tbfd, ntbfd, h->root.root.string);
967       else if (tdef)
968 	(*_bfd_error_handler)
969 	  (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
970 	   tbfd, tsec, ntbfd, h->root.root.string);
971       else
972 	(*_bfd_error_handler)
973 	  (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
974 	   tbfd, ntbfd, ntsec, h->root.root.string);
975 
976       bfd_set_error (bfd_error_bad_value);
977       return FALSE;
978     }
979 
980   /* We need to remember if a symbol has a definition in a dynamic
981      object or is weak in all dynamic objects. Internal and hidden
982      visibility will make it unavailable to dynamic objects.  */
983   if (newdyn && !h->dynamic_def)
984     {
985       if (!bfd_is_und_section (sec))
986 	h->dynamic_def = 1;
987       else
988 	{
989 	  /* Check if this symbol is weak in all dynamic objects. If it
990 	     is the first time we see it in a dynamic object, we mark
991 	     if it is weak. Otherwise, we clear it.  */
992 	  if (!h->ref_dynamic)
993 	    {
994 	      if (bind == STB_WEAK)
995 		h->dynamic_weak = 1;
996 	    }
997 	  else if (bind != STB_WEAK)
998 	    h->dynamic_weak = 0;
999 	}
1000     }
1001 
1002   /* If the old symbol has non-default visibility, we ignore the new
1003      definition from a dynamic object.  */
1004   if (newdyn
1005       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1006       && !bfd_is_und_section (sec))
1007     {
1008       *skip = TRUE;
1009       /* Make sure this symbol is dynamic.  */
1010       h->ref_dynamic = 1;
1011       /* A protected symbol has external availability. Make sure it is
1012 	 recorded as dynamic.
1013 
1014 	 FIXME: Should we check type and size for protected symbol?  */
1015       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1016 	return bfd_elf_link_record_dynamic_symbol (info, h);
1017       else
1018 	return TRUE;
1019     }
1020   else if (!newdyn
1021 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1022 	   && h->def_dynamic)
1023     {
1024       /* If the new symbol with non-default visibility comes from a
1025 	 relocatable file and the old definition comes from a dynamic
1026 	 object, we remove the old definition.  */
1027       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1028 	h = *sym_hash;
1029 
1030       if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1031 	  && bfd_is_und_section (sec))
1032 	{
1033 	  /* If the new symbol is undefined and the old symbol was
1034 	     also undefined before, we need to make sure
1035 	     _bfd_generic_link_add_one_symbol doesn't mess
1036 	     up the linker hash table undefs list.  Since the old
1037 	     definition came from a dynamic object, it is still on the
1038 	     undefs list.  */
1039 	  h->root.type = bfd_link_hash_undefined;
1040 	  h->root.u.undef.abfd = abfd;
1041 	}
1042       else
1043 	{
1044 	  h->root.type = bfd_link_hash_new;
1045 	  h->root.u.undef.abfd = NULL;
1046 	}
1047 
1048       if (h->def_dynamic)
1049 	{
1050 	  h->def_dynamic = 0;
1051 	  h->ref_dynamic = 1;
1052 	  h->dynamic_def = 1;
1053 	}
1054       /* FIXME: Should we check type and size for protected symbol?  */
1055       h->size = 0;
1056       h->type = 0;
1057       return TRUE;
1058     }
1059 
1060   /* Differentiate strong and weak symbols.  */
1061   newweak = bind == STB_WEAK;
1062   oldweak = (h->root.type == bfd_link_hash_defweak
1063 	     || h->root.type == bfd_link_hash_undefweak);
1064 
1065   /* If a new weak symbol definition comes from a regular file and the
1066      old symbol comes from a dynamic library, we treat the new one as
1067      strong.  Similarly, an old weak symbol definition from a regular
1068      file is treated as strong when the new symbol comes from a dynamic
1069      library.  Further, an old weak symbol from a dynamic library is
1070      treated as strong if the new symbol is from a dynamic library.
1071      This reflects the way glibc's ld.so works.
1072 
1073      Do this before setting *type_change_ok or *size_change_ok so that
1074      we warn properly when dynamic library symbols are overridden.  */
1075 
1076   if (newdef && !newdyn && olddyn)
1077     newweak = FALSE;
1078   if (olddef && newdyn)
1079     oldweak = FALSE;
1080 
1081   /* It's OK to change the type if either the existing symbol or the
1082      new symbol is weak.  A type change is also OK if the old symbol
1083      is undefined and the new symbol is defined.  */
1084 
1085   if (oldweak
1086       || newweak
1087       || (newdef
1088 	  && h->root.type == bfd_link_hash_undefined))
1089     *type_change_ok = TRUE;
1090 
1091   /* It's OK to change the size if either the existing symbol or the
1092      new symbol is weak, or if the old symbol is undefined.  */
1093 
1094   if (*type_change_ok
1095       || h->root.type == bfd_link_hash_undefined)
1096     *size_change_ok = TRUE;
1097 
1098   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1099      symbol, respectively, appears to be a common symbol in a dynamic
1100      object.  If a symbol appears in an uninitialized section, and is
1101      not weak, and is not a function, then it may be a common symbol
1102      which was resolved when the dynamic object was created.  We want
1103      to treat such symbols specially, because they raise special
1104      considerations when setting the symbol size: if the symbol
1105      appears as a common symbol in a regular object, and the size in
1106      the regular object is larger, we must make sure that we use the
1107      larger size.  This problematic case can always be avoided in C,
1108      but it must be handled correctly when using Fortran shared
1109      libraries.
1110 
1111      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1112      likewise for OLDDYNCOMMON and OLDDEF.
1113 
1114      Note that this test is just a heuristic, and that it is quite
1115      possible to have an uninitialized symbol in a shared object which
1116      is really a definition, rather than a common symbol.  This could
1117      lead to some minor confusion when the symbol really is a common
1118      symbol in some regular object.  However, I think it will be
1119      harmless.  */
1120 
1121   if (newdyn
1122       && newdef
1123       && !newweak
1124       && (sec->flags & SEC_ALLOC) != 0
1125       && (sec->flags & SEC_LOAD) == 0
1126       && sym->st_size > 0
1127       && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1128     newdyncommon = TRUE;
1129   else
1130     newdyncommon = FALSE;
1131 
1132   if (olddyn
1133       && olddef
1134       && h->root.type == bfd_link_hash_defined
1135       && h->def_dynamic
1136       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1137       && (h->root.u.def.section->flags & SEC_LOAD) == 0
1138       && h->size > 0
1139       && h->type != STT_FUNC)
1140     olddyncommon = TRUE;
1141   else
1142     olddyncommon = FALSE;
1143 
1144   /* We now know everything about the old and new symbols.  We ask the
1145      backend to check if we can merge them.  */
1146   bed = get_elf_backend_data (abfd);
1147   if (bed->merge_symbol
1148       && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1149 			     pold_alignment, skip, override,
1150 			     type_change_ok, size_change_ok,
1151 			     &newdyn, &newdef, &newdyncommon, &newweak,
1152 			     abfd, &sec,
1153 			     &olddyn, &olddef, &olddyncommon, &oldweak,
1154 			     oldbfd, &oldsec))
1155     return FALSE;
1156 
1157   /* If both the old and the new symbols look like common symbols in a
1158      dynamic object, set the size of the symbol to the larger of the
1159      two.  */
1160 
1161   if (olddyncommon
1162       && newdyncommon
1163       && sym->st_size != h->size)
1164     {
1165       /* Since we think we have two common symbols, issue a multiple
1166 	 common warning if desired.  Note that we only warn if the
1167 	 size is different.  If the size is the same, we simply let
1168 	 the old symbol override the new one as normally happens with
1169 	 symbols defined in dynamic objects.  */
1170 
1171       if (! ((*info->callbacks->multiple_common)
1172 	     (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1173 	      h->size, abfd, bfd_link_hash_common, sym->st_size)))
1174 	return FALSE;
1175 
1176       if (sym->st_size > h->size)
1177 	h->size = sym->st_size;
1178 
1179       *size_change_ok = TRUE;
1180     }
1181 
1182   /* If we are looking at a dynamic object, and we have found a
1183      definition, we need to see if the symbol was already defined by
1184      some other object.  If so, we want to use the existing
1185      definition, and we do not want to report a multiple symbol
1186      definition error; we do this by clobbering *PSEC to be
1187      bfd_und_section_ptr.
1188 
1189      We treat a common symbol as a definition if the symbol in the
1190      shared library is a function, since common symbols always
1191      represent variables; this can cause confusion in principle, but
1192      any such confusion would seem to indicate an erroneous program or
1193      shared library.  We also permit a common symbol in a regular
1194      object to override a weak symbol in a shared object.  */
1195 
1196   if (newdyn
1197       && newdef
1198       && (olddef
1199 	  || (h->root.type == bfd_link_hash_common
1200 	      && (newweak
1201 		  || ELF_ST_TYPE (sym->st_info) == STT_FUNC
1202 		  || (!olddyn && info->executable)))))
1203     {
1204       *override = TRUE;
1205       newdef = FALSE;
1206       newdyncommon = FALSE;
1207 
1208       *psec = sec = bfd_und_section_ptr;
1209       *size_change_ok = TRUE;
1210 
1211       /* If we get here when the old symbol is a common symbol, then
1212 	 we are explicitly letting it override a weak symbol or
1213 	 function in a dynamic object, and we don't want to warn about
1214 	 a type change.  If the old symbol is a defined symbol, a type
1215 	 change warning may still be appropriate.  */
1216 
1217       if (h->root.type == bfd_link_hash_common)
1218 	*type_change_ok = TRUE;
1219     }
1220 
1221   /* Handle the special case of an old common symbol merging with a
1222      new symbol which looks like a common symbol in a shared object.
1223      We change *PSEC and *PVALUE to make the new symbol look like a
1224      common symbol, and let _bfd_generic_link_add_one_symbol do the
1225      right thing.  */
1226 
1227   if (newdyncommon
1228       && h->root.type == bfd_link_hash_common)
1229     {
1230       *override = TRUE;
1231       newdef = FALSE;
1232       newdyncommon = FALSE;
1233       *pvalue = sym->st_size;
1234       *psec = sec = bed->common_section (oldsec);
1235       *size_change_ok = TRUE;
1236     }
1237 
1238   /* Skip weak definitions of symbols that are already defined.  */
1239   if (newdef && olddef && newweak)
1240     *skip = TRUE;
1241 
1242   /* If the old symbol is from a dynamic object, and the new symbol is
1243      a definition which is not from a dynamic object, then the new
1244      symbol overrides the old symbol.  Symbols from regular files
1245      always take precedence over symbols from dynamic objects, even if
1246      they are defined after the dynamic object in the link.
1247 
1248      As above, we again permit a common symbol in a regular object to
1249      override a definition in a shared object if the shared object
1250      symbol is a function or is weak.  */
1251 
1252   flip = NULL;
1253   if (!newdyn
1254       && (newdef
1255 	  || (bfd_is_com_section (sec)
1256 	      && (oldweak
1257 		  || h->type == STT_FUNC)))
1258       && olddyn
1259       && olddef
1260       && h->def_dynamic)
1261     {
1262       /* Change the hash table entry to undefined, and let
1263 	 _bfd_generic_link_add_one_symbol do the right thing with the
1264 	 new definition.  */
1265 
1266       h->root.type = bfd_link_hash_undefined;
1267       h->root.u.undef.abfd = h->root.u.def.section->owner;
1268       *size_change_ok = TRUE;
1269 
1270       olddef = FALSE;
1271       olddyncommon = FALSE;
1272 
1273       /* We again permit a type change when a common symbol may be
1274 	 overriding a function.  */
1275 
1276       if (bfd_is_com_section (sec))
1277 	*type_change_ok = TRUE;
1278 
1279       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1280 	flip = *sym_hash;
1281       else
1282 	/* This union may have been set to be non-NULL when this symbol
1283 	   was seen in a dynamic object.  We must force the union to be
1284 	   NULL, so that it is correct for a regular symbol.  */
1285 	h->verinfo.vertree = NULL;
1286     }
1287 
1288   /* Handle the special case of a new common symbol merging with an
1289      old symbol that looks like it might be a common symbol defined in
1290      a shared object.  Note that we have already handled the case in
1291      which a new common symbol should simply override the definition
1292      in the shared library.  */
1293 
1294   if (! newdyn
1295       && bfd_is_com_section (sec)
1296       && olddyncommon)
1297     {
1298       /* It would be best if we could set the hash table entry to a
1299 	 common symbol, but we don't know what to use for the section
1300 	 or the alignment.  */
1301       if (! ((*info->callbacks->multiple_common)
1302 	     (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1303 	      h->size, abfd, bfd_link_hash_common, sym->st_size)))
1304 	return FALSE;
1305 
1306       /* If the presumed common symbol in the dynamic object is
1307 	 larger, pretend that the new symbol has its size.  */
1308 
1309       if (h->size > *pvalue)
1310 	*pvalue = h->size;
1311 
1312       /* We need to remember the alignment required by the symbol
1313 	 in the dynamic object.  */
1314       BFD_ASSERT (pold_alignment);
1315       *pold_alignment = h->root.u.def.section->alignment_power;
1316 
1317       olddef = FALSE;
1318       olddyncommon = FALSE;
1319 
1320       h->root.type = bfd_link_hash_undefined;
1321       h->root.u.undef.abfd = h->root.u.def.section->owner;
1322 
1323       *size_change_ok = TRUE;
1324       *type_change_ok = TRUE;
1325 
1326       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1327 	flip = *sym_hash;
1328       else
1329 	h->verinfo.vertree = NULL;
1330     }
1331 
1332   if (flip != NULL)
1333     {
1334       /* Handle the case where we had a versioned symbol in a dynamic
1335 	 library and now find a definition in a normal object.  In this
1336 	 case, we make the versioned symbol point to the normal one.  */
1337       const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1338       flip->root.type = h->root.type;
1339       h->root.type = bfd_link_hash_indirect;
1340       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1341       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1342       flip->root.u.undef.abfd = h->root.u.undef.abfd;
1343       if (h->def_dynamic)
1344 	{
1345 	  h->def_dynamic = 0;
1346 	  flip->ref_dynamic = 1;
1347 	}
1348     }
1349 
1350   return TRUE;
1351 }
1352 
1353 /* This function is called to create an indirect symbol from the
1354    default for the symbol with the default version if needed. The
1355    symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE.  We
1356    set DYNSYM if the new indirect symbol is dynamic.  */
1357 
1358 bfd_boolean
1359 _bfd_elf_add_default_symbol (bfd *abfd,
1360 			     struct bfd_link_info *info,
1361 			     struct elf_link_hash_entry *h,
1362 			     const char *name,
1363 			     Elf_Internal_Sym *sym,
1364 			     asection **psec,
1365 			     bfd_vma *value,
1366 			     bfd_boolean *dynsym,
1367 			     bfd_boolean override)
1368 {
1369   bfd_boolean type_change_ok;
1370   bfd_boolean size_change_ok;
1371   bfd_boolean skip;
1372   char *shortname;
1373   struct elf_link_hash_entry *hi;
1374   struct bfd_link_hash_entry *bh;
1375   const struct elf_backend_data *bed;
1376   bfd_boolean collect;
1377   bfd_boolean dynamic;
1378   char *p;
1379   size_t len, shortlen;
1380   asection *sec;
1381 
1382   /* If this symbol has a version, and it is the default version, we
1383      create an indirect symbol from the default name to the fully
1384      decorated name.  This will cause external references which do not
1385      specify a version to be bound to this version of the symbol.  */
1386   p = strchr (name, ELF_VER_CHR);
1387   if (p == NULL || p[1] != ELF_VER_CHR)
1388     return TRUE;
1389 
1390   if (override)
1391     {
1392       /* We are overridden by an old definition. We need to check if we
1393 	 need to create the indirect symbol from the default name.  */
1394       hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1395 				 FALSE, FALSE);
1396       BFD_ASSERT (hi != NULL);
1397       if (hi == h)
1398 	return TRUE;
1399       while (hi->root.type == bfd_link_hash_indirect
1400 	     || hi->root.type == bfd_link_hash_warning)
1401 	{
1402 	  hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1403 	  if (hi == h)
1404 	    return TRUE;
1405 	}
1406     }
1407 
1408   bed = get_elf_backend_data (abfd);
1409   collect = bed->collect;
1410   dynamic = (abfd->flags & DYNAMIC) != 0;
1411 
1412   shortlen = p - name;
1413   shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1414   if (shortname == NULL)
1415     return FALSE;
1416   memcpy (shortname, name, shortlen);
1417   shortname[shortlen] = '\0';
1418 
1419   /* We are going to create a new symbol.  Merge it with any existing
1420      symbol with this name.  For the purposes of the merge, act as
1421      though we were defining the symbol we just defined, although we
1422      actually going to define an indirect symbol.  */
1423   type_change_ok = FALSE;
1424   size_change_ok = FALSE;
1425   sec = *psec;
1426   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1427 			      NULL, &hi, &skip, &override,
1428 			      &type_change_ok, &size_change_ok))
1429     return FALSE;
1430 
1431   if (skip)
1432     goto nondefault;
1433 
1434   if (! override)
1435     {
1436       bh = &hi->root;
1437       if (! (_bfd_generic_link_add_one_symbol
1438 	     (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1439 	      0, name, FALSE, collect, &bh)))
1440 	return FALSE;
1441       hi = (struct elf_link_hash_entry *) bh;
1442     }
1443   else
1444     {
1445       /* In this case the symbol named SHORTNAME is overriding the
1446 	 indirect symbol we want to add.  We were planning on making
1447 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
1448 	 is the name without a version.  NAME is the fully versioned
1449 	 name, and it is the default version.
1450 
1451 	 Overriding means that we already saw a definition for the
1452 	 symbol SHORTNAME in a regular object, and it is overriding
1453 	 the symbol defined in the dynamic object.
1454 
1455 	 When this happens, we actually want to change NAME, the
1456 	 symbol we just added, to refer to SHORTNAME.  This will cause
1457 	 references to NAME in the shared object to become references
1458 	 to SHORTNAME in the regular object.  This is what we expect
1459 	 when we override a function in a shared object: that the
1460 	 references in the shared object will be mapped to the
1461 	 definition in the regular object.  */
1462 
1463       while (hi->root.type == bfd_link_hash_indirect
1464 	     || hi->root.type == bfd_link_hash_warning)
1465 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1466 
1467       h->root.type = bfd_link_hash_indirect;
1468       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1469       if (h->def_dynamic)
1470 	{
1471 	  h->def_dynamic = 0;
1472 	  hi->ref_dynamic = 1;
1473 	  if (hi->ref_regular
1474 	      || hi->def_regular)
1475 	    {
1476 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1477 		return FALSE;
1478 	    }
1479 	}
1480 
1481       /* Now set HI to H, so that the following code will set the
1482 	 other fields correctly.  */
1483       hi = h;
1484     }
1485 
1486   /* If there is a duplicate definition somewhere, then HI may not
1487      point to an indirect symbol.  We will have reported an error to
1488      the user in that case.  */
1489 
1490   if (hi->root.type == bfd_link_hash_indirect)
1491     {
1492       struct elf_link_hash_entry *ht;
1493 
1494       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1495       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1496 
1497       /* See if the new flags lead us to realize that the symbol must
1498 	 be dynamic.  */
1499       if (! *dynsym)
1500 	{
1501 	  if (! dynamic)
1502 	    {
1503 	      if (info->shared
1504 		  || hi->ref_dynamic)
1505 		*dynsym = TRUE;
1506 	    }
1507 	  else
1508 	    {
1509 	      if (hi->ref_regular)
1510 		*dynsym = TRUE;
1511 	    }
1512 	}
1513     }
1514 
1515   /* We also need to define an indirection from the nondefault version
1516      of the symbol.  */
1517 
1518 nondefault:
1519   len = strlen (name);
1520   shortname = bfd_hash_allocate (&info->hash->table, len);
1521   if (shortname == NULL)
1522     return FALSE;
1523   memcpy (shortname, name, shortlen);
1524   memcpy (shortname + shortlen, p + 1, len - shortlen);
1525 
1526   /* Once again, merge with any existing symbol.  */
1527   type_change_ok = FALSE;
1528   size_change_ok = FALSE;
1529   sec = *psec;
1530   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1531 			      NULL, &hi, &skip, &override,
1532 			      &type_change_ok, &size_change_ok))
1533     return FALSE;
1534 
1535   if (skip)
1536     return TRUE;
1537 
1538   if (override)
1539     {
1540       /* Here SHORTNAME is a versioned name, so we don't expect to see
1541 	 the type of override we do in the case above unless it is
1542 	 overridden by a versioned definition.  */
1543       if (hi->root.type != bfd_link_hash_defined
1544 	  && hi->root.type != bfd_link_hash_defweak)
1545 	(*_bfd_error_handler)
1546 	  (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1547 	   abfd, shortname);
1548     }
1549   else
1550     {
1551       bh = &hi->root;
1552       if (! (_bfd_generic_link_add_one_symbol
1553 	     (info, abfd, shortname, BSF_INDIRECT,
1554 	      bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1555 	return FALSE;
1556       hi = (struct elf_link_hash_entry *) bh;
1557 
1558       /* If there is a duplicate definition somewhere, then HI may not
1559 	 point to an indirect symbol.  We will have reported an error
1560 	 to the user in that case.  */
1561 
1562       if (hi->root.type == bfd_link_hash_indirect)
1563 	{
1564 	  (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1565 
1566 	  /* See if the new flags lead us to realize that the symbol
1567 	     must be dynamic.  */
1568 	  if (! *dynsym)
1569 	    {
1570 	      if (! dynamic)
1571 		{
1572 		  if (info->shared
1573 		      || hi->ref_dynamic)
1574 		    *dynsym = TRUE;
1575 		}
1576 	      else
1577 		{
1578 		  if (hi->ref_regular)
1579 		    *dynsym = TRUE;
1580 		}
1581 	    }
1582 	}
1583     }
1584 
1585   return TRUE;
1586 }
1587 
1588 /* This routine is used to export all defined symbols into the dynamic
1589    symbol table.  It is called via elf_link_hash_traverse.  */
1590 
1591 bfd_boolean
1592 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1593 {
1594   struct elf_info_failed *eif = data;
1595 
1596   /* Ignore indirect symbols.  These are added by the versioning code.  */
1597   if (h->root.type == bfd_link_hash_indirect)
1598     return TRUE;
1599 
1600   if (h->root.type == bfd_link_hash_warning)
1601     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1602 
1603   if (h->dynindx == -1
1604       && (h->def_regular
1605 	  || h->ref_regular))
1606     {
1607       struct bfd_elf_version_tree *t;
1608       struct bfd_elf_version_expr *d;
1609 
1610       for (t = eif->verdefs; t != NULL; t = t->next)
1611 	{
1612 	  if (t->globals.list != NULL)
1613 	    {
1614 	      d = (*t->match) (&t->globals, NULL, h->root.root.string);
1615 	      if (d != NULL)
1616 		goto doit;
1617 	    }
1618 
1619 	  if (t->locals.list != NULL)
1620 	    {
1621 	      d = (*t->match) (&t->locals, NULL, h->root.root.string);
1622 	      if (d != NULL)
1623 		return TRUE;
1624 	    }
1625 	}
1626 
1627       if (!eif->verdefs)
1628 	{
1629 	doit:
1630 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1631 	    {
1632 	      eif->failed = TRUE;
1633 	      return FALSE;
1634 	    }
1635 	}
1636     }
1637 
1638   return TRUE;
1639 }
1640 
1641 /* Look through the symbols which are defined in other shared
1642    libraries and referenced here.  Update the list of version
1643    dependencies.  This will be put into the .gnu.version_r section.
1644    This function is called via elf_link_hash_traverse.  */
1645 
1646 bfd_boolean
1647 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1648 					 void *data)
1649 {
1650   struct elf_find_verdep_info *rinfo = data;
1651   Elf_Internal_Verneed *t;
1652   Elf_Internal_Vernaux *a;
1653   bfd_size_type amt;
1654 
1655   if (h->root.type == bfd_link_hash_warning)
1656     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1657 
1658   /* We only care about symbols defined in shared objects with version
1659      information.  */
1660   if (!h->def_dynamic
1661       || h->def_regular
1662       || h->dynindx == -1
1663       || h->verinfo.verdef == NULL)
1664     return TRUE;
1665 
1666   /* See if we already know about this version.  */
1667   for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1668     {
1669       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1670 	continue;
1671 
1672       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1673 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1674 	  return TRUE;
1675 
1676       break;
1677     }
1678 
1679   /* This is a new version.  Add it to tree we are building.  */
1680 
1681   if (t == NULL)
1682     {
1683       amt = sizeof *t;
1684       t = bfd_zalloc (rinfo->output_bfd, amt);
1685       if (t == NULL)
1686 	{
1687 	  rinfo->failed = TRUE;
1688 	  return FALSE;
1689 	}
1690 
1691       t->vn_bfd = h->verinfo.verdef->vd_bfd;
1692       t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1693       elf_tdata (rinfo->output_bfd)->verref = t;
1694     }
1695 
1696   amt = sizeof *a;
1697   a = bfd_zalloc (rinfo->output_bfd, amt);
1698 
1699   /* Note that we are copying a string pointer here, and testing it
1700      above.  If bfd_elf_string_from_elf_section is ever changed to
1701      discard the string data when low in memory, this will have to be
1702      fixed.  */
1703   a->vna_nodename = h->verinfo.verdef->vd_nodename;
1704 
1705   a->vna_flags = h->verinfo.verdef->vd_flags;
1706   a->vna_nextptr = t->vn_auxptr;
1707 
1708   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1709   ++rinfo->vers;
1710 
1711   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1712 
1713   t->vn_auxptr = a;
1714 
1715   return TRUE;
1716 }
1717 
1718 /* Figure out appropriate versions for all the symbols.  We may not
1719    have the version number script until we have read all of the input
1720    files, so until that point we don't know which symbols should be
1721    local.  This function is called via elf_link_hash_traverse.  */
1722 
1723 bfd_boolean
1724 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1725 {
1726   struct elf_assign_sym_version_info *sinfo;
1727   struct bfd_link_info *info;
1728   const struct elf_backend_data *bed;
1729   struct elf_info_failed eif;
1730   char *p;
1731   bfd_size_type amt;
1732 
1733   sinfo = data;
1734   info = sinfo->info;
1735 
1736   if (h->root.type == bfd_link_hash_warning)
1737     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1738 
1739   /* Fix the symbol flags.  */
1740   eif.failed = FALSE;
1741   eif.info = info;
1742   if (! _bfd_elf_fix_symbol_flags (h, &eif))
1743     {
1744       if (eif.failed)
1745 	sinfo->failed = TRUE;
1746       return FALSE;
1747     }
1748 
1749   /* We only need version numbers for symbols defined in regular
1750      objects.  */
1751   if (!h->def_regular)
1752     return TRUE;
1753 
1754   bed = get_elf_backend_data (sinfo->output_bfd);
1755   p = strchr (h->root.root.string, ELF_VER_CHR);
1756   if (p != NULL && h->verinfo.vertree == NULL)
1757     {
1758       struct bfd_elf_version_tree *t;
1759       bfd_boolean hidden;
1760 
1761       hidden = TRUE;
1762 
1763       /* There are two consecutive ELF_VER_CHR characters if this is
1764 	 not a hidden symbol.  */
1765       ++p;
1766       if (*p == ELF_VER_CHR)
1767 	{
1768 	  hidden = FALSE;
1769 	  ++p;
1770 	}
1771 
1772       /* If there is no version string, we can just return out.  */
1773       if (*p == '\0')
1774 	{
1775 	  if (hidden)
1776 	    h->hidden = 1;
1777 	  return TRUE;
1778 	}
1779 
1780       /* Look for the version.  If we find it, it is no longer weak.  */
1781       for (t = sinfo->verdefs; t != NULL; t = t->next)
1782 	{
1783 	  if (strcmp (t->name, p) == 0)
1784 	    {
1785 	      size_t len;
1786 	      char *alc;
1787 	      struct bfd_elf_version_expr *d;
1788 
1789 	      len = p - h->root.root.string;
1790 	      alc = bfd_malloc (len);
1791 	      if (alc == NULL)
1792 		return FALSE;
1793 	      memcpy (alc, h->root.root.string, len - 1);
1794 	      alc[len - 1] = '\0';
1795 	      if (alc[len - 2] == ELF_VER_CHR)
1796 		alc[len - 2] = '\0';
1797 
1798 	      h->verinfo.vertree = t;
1799 	      t->used = TRUE;
1800 	      d = NULL;
1801 
1802 	      if (t->globals.list != NULL)
1803 		d = (*t->match) (&t->globals, NULL, alc);
1804 
1805 	      /* See if there is anything to force this symbol to
1806 		 local scope.  */
1807 	      if (d == NULL && t->locals.list != NULL)
1808 		{
1809 		  d = (*t->match) (&t->locals, NULL, alc);
1810 		  if (d != NULL
1811 		      && h->dynindx != -1
1812 		      && ! info->export_dynamic)
1813 		    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1814 		}
1815 
1816 	      free (alc);
1817 	      break;
1818 	    }
1819 	}
1820 
1821       /* If we are building an application, we need to create a
1822 	 version node for this version.  */
1823       if (t == NULL && info->executable)
1824 	{
1825 	  struct bfd_elf_version_tree **pp;
1826 	  int version_index;
1827 
1828 	  /* If we aren't going to export this symbol, we don't need
1829 	     to worry about it.  */
1830 	  if (h->dynindx == -1)
1831 	    return TRUE;
1832 
1833 	  amt = sizeof *t;
1834 	  t = bfd_zalloc (sinfo->output_bfd, amt);
1835 	  if (t == NULL)
1836 	    {
1837 	      sinfo->failed = TRUE;
1838 	      return FALSE;
1839 	    }
1840 
1841 	  t->name = p;
1842 	  t->name_indx = (unsigned int) -1;
1843 	  t->used = TRUE;
1844 
1845 	  version_index = 1;
1846 	  /* Don't count anonymous version tag.  */
1847 	  if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1848 	    version_index = 0;
1849 	  for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1850 	    ++version_index;
1851 	  t->vernum = version_index;
1852 
1853 	  *pp = t;
1854 
1855 	  h->verinfo.vertree = t;
1856 	}
1857       else if (t == NULL)
1858 	{
1859 	  /* We could not find the version for a symbol when
1860 	     generating a shared archive.  Return an error.  */
1861 	  (*_bfd_error_handler)
1862 	    (_("%B: undefined versioned symbol name %s"),
1863 	     sinfo->output_bfd, h->root.root.string);
1864 	  bfd_set_error (bfd_error_bad_value);
1865 	  sinfo->failed = TRUE;
1866 	  return FALSE;
1867 	}
1868 
1869       if (hidden)
1870 	h->hidden = 1;
1871     }
1872 
1873   /* If we don't have a version for this symbol, see if we can find
1874      something.  */
1875   if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1876     {
1877       struct bfd_elf_version_tree *t;
1878       struct bfd_elf_version_tree *local_ver;
1879       struct bfd_elf_version_expr *d;
1880 
1881       /* See if can find what version this symbol is in.  If the
1882 	 symbol is supposed to be local, then don't actually register
1883 	 it.  */
1884       local_ver = NULL;
1885       for (t = sinfo->verdefs; t != NULL; t = t->next)
1886 	{
1887 	  if (t->globals.list != NULL)
1888 	    {
1889 	      bfd_boolean matched;
1890 
1891 	      matched = FALSE;
1892 	      d = NULL;
1893 	      while ((d = (*t->match) (&t->globals, d,
1894 				       h->root.root.string)) != NULL)
1895 		if (d->symver)
1896 		  matched = TRUE;
1897 		else
1898 		  {
1899 		    /* There is a version without definition.  Make
1900 		       the symbol the default definition for this
1901 		       version.  */
1902 		    h->verinfo.vertree = t;
1903 		    local_ver = NULL;
1904 		    d->script = 1;
1905 		    break;
1906 		  }
1907 	      if (d != NULL)
1908 		break;
1909 	      else if (matched)
1910 		/* There is no undefined version for this symbol. Hide the
1911 		   default one.  */
1912 		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
1913 	    }
1914 
1915 	  if (t->locals.list != NULL)
1916 	    {
1917 	      d = NULL;
1918 	      while ((d = (*t->match) (&t->locals, d,
1919 				       h->root.root.string)) != NULL)
1920 		{
1921 		  local_ver = t;
1922 		  /* If the match is "*", keep looking for a more
1923 		     explicit, perhaps even global, match.
1924 		     XXX: Shouldn't this be !d->wildcard instead?  */
1925 		  if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1926 		    break;
1927 		}
1928 
1929 	      if (d != NULL)
1930 		break;
1931 	    }
1932 	}
1933 
1934       if (local_ver != NULL)
1935 	{
1936 	  h->verinfo.vertree = local_ver;
1937 	  if (h->dynindx != -1
1938 	      && ! info->export_dynamic)
1939 	    {
1940 	      (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1941 	    }
1942 	}
1943     }
1944 
1945   return TRUE;
1946 }
1947 
1948 /* Read and swap the relocs from the section indicated by SHDR.  This
1949    may be either a REL or a RELA section.  The relocations are
1950    translated into RELA relocations and stored in INTERNAL_RELOCS,
1951    which should have already been allocated to contain enough space.
1952    The EXTERNAL_RELOCS are a buffer where the external form of the
1953    relocations should be stored.
1954 
1955    Returns FALSE if something goes wrong.  */
1956 
1957 static bfd_boolean
1958 elf_link_read_relocs_from_section (bfd *abfd,
1959 				   asection *sec,
1960 				   Elf_Internal_Shdr *shdr,
1961 				   void *external_relocs,
1962 				   Elf_Internal_Rela *internal_relocs)
1963 {
1964   const struct elf_backend_data *bed;
1965   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1966   const bfd_byte *erela;
1967   const bfd_byte *erelaend;
1968   Elf_Internal_Rela *irela;
1969   Elf_Internal_Shdr *symtab_hdr;
1970   size_t nsyms;
1971 
1972   /* Position ourselves at the start of the section.  */
1973   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1974     return FALSE;
1975 
1976   /* Read the relocations.  */
1977   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1978     return FALSE;
1979 
1980   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1981   nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1982 
1983   bed = get_elf_backend_data (abfd);
1984 
1985   /* Convert the external relocations to the internal format.  */
1986   if (shdr->sh_entsize == bed->s->sizeof_rel)
1987     swap_in = bed->s->swap_reloc_in;
1988   else if (shdr->sh_entsize == bed->s->sizeof_rela)
1989     swap_in = bed->s->swap_reloca_in;
1990   else
1991     {
1992       bfd_set_error (bfd_error_wrong_format);
1993       return FALSE;
1994     }
1995 
1996   erela = external_relocs;
1997   erelaend = erela + shdr->sh_size;
1998   irela = internal_relocs;
1999   while (erela < erelaend)
2000     {
2001       bfd_vma r_symndx;
2002 
2003       (*swap_in) (abfd, erela, irela);
2004       r_symndx = ELF32_R_SYM (irela->r_info);
2005       if (bed->s->arch_size == 64)
2006 	r_symndx >>= 24;
2007       if ((size_t) r_symndx >= nsyms)
2008 	{
2009 	  (*_bfd_error_handler)
2010 	    (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2011 	       " for offset 0x%lx in section `%A'"),
2012 	     abfd, sec,
2013 	     (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2014 	  bfd_set_error (bfd_error_bad_value);
2015 	  return FALSE;
2016 	}
2017       irela += bed->s->int_rels_per_ext_rel;
2018       erela += shdr->sh_entsize;
2019     }
2020 
2021   return TRUE;
2022 }
2023 
2024 /* Read and swap the relocs for a section O.  They may have been
2025    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2026    not NULL, they are used as buffers to read into.  They are known to
2027    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2028    the return value is allocated using either malloc or bfd_alloc,
2029    according to the KEEP_MEMORY argument.  If O has two relocation
2030    sections (both REL and RELA relocations), then the REL_HDR
2031    relocations will appear first in INTERNAL_RELOCS, followed by the
2032    REL_HDR2 relocations.  */
2033 
2034 Elf_Internal_Rela *
2035 _bfd_elf_link_read_relocs (bfd *abfd,
2036 			   asection *o,
2037 			   void *external_relocs,
2038 			   Elf_Internal_Rela *internal_relocs,
2039 			   bfd_boolean keep_memory)
2040 {
2041   Elf_Internal_Shdr *rel_hdr;
2042   void *alloc1 = NULL;
2043   Elf_Internal_Rela *alloc2 = NULL;
2044   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2045 
2046   if (elf_section_data (o)->relocs != NULL)
2047     return elf_section_data (o)->relocs;
2048 
2049   if (o->reloc_count == 0)
2050     return NULL;
2051 
2052   rel_hdr = &elf_section_data (o)->rel_hdr;
2053 
2054   if (internal_relocs == NULL)
2055     {
2056       bfd_size_type size;
2057 
2058       size = o->reloc_count;
2059       size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2060       if (keep_memory)
2061 	internal_relocs = bfd_alloc (abfd, size);
2062       else
2063 	internal_relocs = alloc2 = bfd_malloc (size);
2064       if (internal_relocs == NULL)
2065 	goto error_return;
2066     }
2067 
2068   if (external_relocs == NULL)
2069     {
2070       bfd_size_type size = rel_hdr->sh_size;
2071 
2072       if (elf_section_data (o)->rel_hdr2)
2073 	size += elf_section_data (o)->rel_hdr2->sh_size;
2074       alloc1 = bfd_malloc (size);
2075       if (alloc1 == NULL)
2076 	goto error_return;
2077       external_relocs = alloc1;
2078     }
2079 
2080   if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2081 					  external_relocs,
2082 					  internal_relocs))
2083     goto error_return;
2084   if (elf_section_data (o)->rel_hdr2
2085       && (!elf_link_read_relocs_from_section
2086 	  (abfd, o,
2087 	   elf_section_data (o)->rel_hdr2,
2088 	   ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2089 	   internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2090 			      * bed->s->int_rels_per_ext_rel))))
2091     goto error_return;
2092 
2093   /* Cache the results for next time, if we can.  */
2094   if (keep_memory)
2095     elf_section_data (o)->relocs = internal_relocs;
2096 
2097   if (alloc1 != NULL)
2098     free (alloc1);
2099 
2100   /* Don't free alloc2, since if it was allocated we are passing it
2101      back (under the name of internal_relocs).  */
2102 
2103   return internal_relocs;
2104 
2105  error_return:
2106   if (alloc1 != NULL)
2107     free (alloc1);
2108   if (alloc2 != NULL)
2109     free (alloc2);
2110   return NULL;
2111 }
2112 
2113 /* Compute the size of, and allocate space for, REL_HDR which is the
2114    section header for a section containing relocations for O.  */
2115 
2116 bfd_boolean
2117 _bfd_elf_link_size_reloc_section (bfd *abfd,
2118 				  Elf_Internal_Shdr *rel_hdr,
2119 				  asection *o)
2120 {
2121   bfd_size_type reloc_count;
2122   bfd_size_type num_rel_hashes;
2123 
2124   /* Figure out how many relocations there will be.  */
2125   if (rel_hdr == &elf_section_data (o)->rel_hdr)
2126     reloc_count = elf_section_data (o)->rel_count;
2127   else
2128     reloc_count = elf_section_data (o)->rel_count2;
2129 
2130   num_rel_hashes = o->reloc_count;
2131   if (num_rel_hashes < reloc_count)
2132     num_rel_hashes = reloc_count;
2133 
2134   /* That allows us to calculate the size of the section.  */
2135   rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2136 
2137   /* The contents field must last into write_object_contents, so we
2138      allocate it with bfd_alloc rather than malloc.  Also since we
2139      cannot be sure that the contents will actually be filled in,
2140      we zero the allocated space.  */
2141   rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2142   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2143     return FALSE;
2144 
2145   /* We only allocate one set of hash entries, so we only do it the
2146      first time we are called.  */
2147   if (elf_section_data (o)->rel_hashes == NULL
2148       && num_rel_hashes)
2149     {
2150       struct elf_link_hash_entry **p;
2151 
2152       p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2153       if (p == NULL)
2154 	return FALSE;
2155 
2156       elf_section_data (o)->rel_hashes = p;
2157     }
2158 
2159   return TRUE;
2160 }
2161 
2162 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2163    originated from the section given by INPUT_REL_HDR) to the
2164    OUTPUT_BFD.  */
2165 
2166 bfd_boolean
2167 _bfd_elf_link_output_relocs (bfd *output_bfd,
2168 			     asection *input_section,
2169 			     Elf_Internal_Shdr *input_rel_hdr,
2170 			     Elf_Internal_Rela *internal_relocs,
2171 			     struct elf_link_hash_entry **rel_hash
2172 			       ATTRIBUTE_UNUSED)
2173 {
2174   Elf_Internal_Rela *irela;
2175   Elf_Internal_Rela *irelaend;
2176   bfd_byte *erel;
2177   Elf_Internal_Shdr *output_rel_hdr;
2178   asection *output_section;
2179   unsigned int *rel_countp = NULL;
2180   const struct elf_backend_data *bed;
2181   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2182 
2183   output_section = input_section->output_section;
2184   output_rel_hdr = NULL;
2185 
2186   if (elf_section_data (output_section)->rel_hdr.sh_entsize
2187       == input_rel_hdr->sh_entsize)
2188     {
2189       output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2190       rel_countp = &elf_section_data (output_section)->rel_count;
2191     }
2192   else if (elf_section_data (output_section)->rel_hdr2
2193 	   && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2194 	       == input_rel_hdr->sh_entsize))
2195     {
2196       output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2197       rel_countp = &elf_section_data (output_section)->rel_count2;
2198     }
2199   else
2200     {
2201       (*_bfd_error_handler)
2202 	(_("%B: relocation size mismatch in %B section %A"),
2203 	 output_bfd, input_section->owner, input_section);
2204       bfd_set_error (bfd_error_wrong_object_format);
2205       return FALSE;
2206     }
2207 
2208   bed = get_elf_backend_data (output_bfd);
2209   if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2210     swap_out = bed->s->swap_reloc_out;
2211   else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2212     swap_out = bed->s->swap_reloca_out;
2213   else
2214     abort ();
2215 
2216   erel = output_rel_hdr->contents;
2217   erel += *rel_countp * input_rel_hdr->sh_entsize;
2218   irela = internal_relocs;
2219   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2220 		      * bed->s->int_rels_per_ext_rel);
2221   while (irela < irelaend)
2222     {
2223       (*swap_out) (output_bfd, irela, erel);
2224       irela += bed->s->int_rels_per_ext_rel;
2225       erel += input_rel_hdr->sh_entsize;
2226     }
2227 
2228   /* Bump the counter, so that we know where to add the next set of
2229      relocations.  */
2230   *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2231 
2232   return TRUE;
2233 }
2234 
2235 /* Make weak undefined symbols in PIE dynamic.  */
2236 
2237 bfd_boolean
2238 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2239 				 struct elf_link_hash_entry *h)
2240 {
2241   if (info->pie
2242       && h->dynindx == -1
2243       && h->root.type == bfd_link_hash_undefweak)
2244     return bfd_elf_link_record_dynamic_symbol (info, h);
2245 
2246   return TRUE;
2247 }
2248 
2249 /* Fix up the flags for a symbol.  This handles various cases which
2250    can only be fixed after all the input files are seen.  This is
2251    currently called by both adjust_dynamic_symbol and
2252    assign_sym_version, which is unnecessary but perhaps more robust in
2253    the face of future changes.  */
2254 
2255 bfd_boolean
2256 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2257 			   struct elf_info_failed *eif)
2258 {
2259   const struct elf_backend_data *bed = NULL;
2260 
2261   /* If this symbol was mentioned in a non-ELF file, try to set
2262      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
2263      permit a non-ELF file to correctly refer to a symbol defined in
2264      an ELF dynamic object.  */
2265   if (h->non_elf)
2266     {
2267       while (h->root.type == bfd_link_hash_indirect)
2268 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
2269 
2270       if (h->root.type != bfd_link_hash_defined
2271 	  && h->root.type != bfd_link_hash_defweak)
2272 	{
2273 	  h->ref_regular = 1;
2274 	  h->ref_regular_nonweak = 1;
2275 	}
2276       else
2277 	{
2278 	  if (h->root.u.def.section->owner != NULL
2279 	      && (bfd_get_flavour (h->root.u.def.section->owner)
2280 		  == bfd_target_elf_flavour))
2281 	    {
2282 	      h->ref_regular = 1;
2283 	      h->ref_regular_nonweak = 1;
2284 	    }
2285 	  else
2286 	    h->def_regular = 1;
2287 	}
2288 
2289       if (h->dynindx == -1
2290 	  && (h->def_dynamic
2291 	      || h->ref_dynamic))
2292 	{
2293 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2294 	    {
2295 	      eif->failed = TRUE;
2296 	      return FALSE;
2297 	    }
2298 	}
2299     }
2300   else
2301     {
2302       /* Unfortunately, NON_ELF is only correct if the symbol
2303 	 was first seen in a non-ELF file.  Fortunately, if the symbol
2304 	 was first seen in an ELF file, we're probably OK unless the
2305 	 symbol was defined in a non-ELF file.  Catch that case here.
2306 	 FIXME: We're still in trouble if the symbol was first seen in
2307 	 a dynamic object, and then later in a non-ELF regular object.  */
2308       if ((h->root.type == bfd_link_hash_defined
2309 	   || h->root.type == bfd_link_hash_defweak)
2310 	  && !h->def_regular
2311 	  && (h->root.u.def.section->owner != NULL
2312 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
2313 		 != bfd_target_elf_flavour)
2314 	      : (bfd_is_abs_section (h->root.u.def.section)
2315 		 && !h->def_dynamic)))
2316 	h->def_regular = 1;
2317     }
2318 
2319   /* Backend specific symbol fixup.  */
2320   if (elf_hash_table (eif->info)->dynobj)
2321     {
2322       bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2323       if (bed->elf_backend_fixup_symbol
2324 	  && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2325 	return FALSE;
2326     }
2327 
2328   /* If this is a final link, and the symbol was defined as a common
2329      symbol in a regular object file, and there was no definition in
2330      any dynamic object, then the linker will have allocated space for
2331      the symbol in a common section but the DEF_REGULAR
2332      flag will not have been set.  */
2333   if (h->root.type == bfd_link_hash_defined
2334       && !h->def_regular
2335       && h->ref_regular
2336       && !h->def_dynamic
2337       && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2338     h->def_regular = 1;
2339 
2340   /* If -Bsymbolic was used (which means to bind references to global
2341      symbols to the definition within the shared object), and this
2342      symbol was defined in a regular object, then it actually doesn't
2343      need a PLT entry.  Likewise, if the symbol has non-default
2344      visibility.  If the symbol has hidden or internal visibility, we
2345      will force it local.  */
2346   if (h->needs_plt
2347       && eif->info->shared
2348       && is_elf_hash_table (eif->info->hash)
2349       && (eif->info->symbolic || eif->info->static_link
2350 	  || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2351       && h->def_regular)
2352     {
2353       bfd_boolean force_local;
2354 
2355       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2356 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2357       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2358     }
2359 
2360   /* If a weak undefined symbol has non-default visibility, we also
2361      hide it from the dynamic linker.  */
2362   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2363       && h->root.type == bfd_link_hash_undefweak)
2364     {
2365       const struct elf_backend_data *bed;
2366       bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2367       (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2368     }
2369 
2370   /* If this is a weak defined symbol in a dynamic object, and we know
2371      the real definition in the dynamic object, copy interesting flags
2372      over to the real definition.  */
2373   if (h->u.weakdef != NULL)
2374     {
2375       struct elf_link_hash_entry *weakdef;
2376 
2377       weakdef = h->u.weakdef;
2378       if (h->root.type == bfd_link_hash_indirect)
2379 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
2380 
2381       BFD_ASSERT (h->root.type == bfd_link_hash_defined
2382 		  || h->root.type == bfd_link_hash_defweak);
2383       BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2384 		  || weakdef->root.type == bfd_link_hash_defweak);
2385       BFD_ASSERT (weakdef->def_dynamic);
2386 
2387       /* If the real definition is defined by a regular object file,
2388 	 don't do anything special.  See the longer description in
2389 	 _bfd_elf_adjust_dynamic_symbol, below.  */
2390       if (weakdef->def_regular)
2391 	h->u.weakdef = NULL;
2392       else
2393 	(*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef,
2394 						  h);
2395     }
2396 
2397   return TRUE;
2398 }
2399 
2400 /* Make the backend pick a good value for a dynamic symbol.  This is
2401    called via elf_link_hash_traverse, and also calls itself
2402    recursively.  */
2403 
2404 bfd_boolean
2405 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2406 {
2407   struct elf_info_failed *eif = data;
2408   bfd *dynobj;
2409   const struct elf_backend_data *bed;
2410 
2411   if (! is_elf_hash_table (eif->info->hash))
2412     return FALSE;
2413 
2414   if (h->root.type == bfd_link_hash_warning)
2415     {
2416       h->got = elf_hash_table (eif->info)->init_got_offset;
2417       h->plt = elf_hash_table (eif->info)->init_plt_offset;
2418 
2419       /* When warning symbols are created, they **replace** the "real"
2420 	 entry in the hash table, thus we never get to see the real
2421 	 symbol in a hash traversal.  So look at it now.  */
2422       h = (struct elf_link_hash_entry *) h->root.u.i.link;
2423     }
2424 
2425   /* Ignore indirect symbols.  These are added by the versioning code.  */
2426   if (h->root.type == bfd_link_hash_indirect)
2427     return TRUE;
2428 
2429   /* Fix the symbol flags.  */
2430   if (! _bfd_elf_fix_symbol_flags (h, eif))
2431     return FALSE;
2432 
2433   /* If this symbol does not require a PLT entry, and it is not
2434      defined by a dynamic object, or is not referenced by a regular
2435      object, ignore it.  We do have to handle a weak defined symbol,
2436      even if no regular object refers to it, if we decided to add it
2437      to the dynamic symbol table.  FIXME: Do we normally need to worry
2438      about symbols which are defined by one dynamic object and
2439      referenced by another one?  */
2440   if (!h->needs_plt
2441       && (h->def_regular
2442 	  || !h->def_dynamic
2443 	  || (!h->ref_regular
2444 	      && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2445     {
2446       h->plt = elf_hash_table (eif->info)->init_plt_offset;
2447       return TRUE;
2448     }
2449 
2450   /* If we've already adjusted this symbol, don't do it again.  This
2451      can happen via a recursive call.  */
2452   if (h->dynamic_adjusted)
2453     return TRUE;
2454 
2455   /* Don't look at this symbol again.  Note that we must set this
2456      after checking the above conditions, because we may look at a
2457      symbol once, decide not to do anything, and then get called
2458      recursively later after REF_REGULAR is set below.  */
2459   h->dynamic_adjusted = 1;
2460 
2461   /* If this is a weak definition, and we know a real definition, and
2462      the real symbol is not itself defined by a regular object file,
2463      then get a good value for the real definition.  We handle the
2464      real symbol first, for the convenience of the backend routine.
2465 
2466      Note that there is a confusing case here.  If the real definition
2467      is defined by a regular object file, we don't get the real symbol
2468      from the dynamic object, but we do get the weak symbol.  If the
2469      processor backend uses a COPY reloc, then if some routine in the
2470      dynamic object changes the real symbol, we will not see that
2471      change in the corresponding weak symbol.  This is the way other
2472      ELF linkers work as well, and seems to be a result of the shared
2473      library model.
2474 
2475      I will clarify this issue.  Most SVR4 shared libraries define the
2476      variable _timezone and define timezone as a weak synonym.  The
2477      tzset call changes _timezone.  If you write
2478        extern int timezone;
2479        int _timezone = 5;
2480        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2481      you might expect that, since timezone is a synonym for _timezone,
2482      the same number will print both times.  However, if the processor
2483      backend uses a COPY reloc, then actually timezone will be copied
2484      into your process image, and, since you define _timezone
2485      yourself, _timezone will not.  Thus timezone and _timezone will
2486      wind up at different memory locations.  The tzset call will set
2487      _timezone, leaving timezone unchanged.  */
2488 
2489   if (h->u.weakdef != NULL)
2490     {
2491       /* If we get to this point, we know there is an implicit
2492 	 reference by a regular object file via the weak symbol H.
2493 	 FIXME: Is this really true?  What if the traversal finds
2494 	 H->U.WEAKDEF before it finds H?  */
2495       h->u.weakdef->ref_regular = 1;
2496 
2497       if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2498 	return FALSE;
2499     }
2500 
2501   /* If a symbol has no type and no size and does not require a PLT
2502      entry, then we are probably about to do the wrong thing here: we
2503      are probably going to create a COPY reloc for an empty object.
2504      This case can arise when a shared object is built with assembly
2505      code, and the assembly code fails to set the symbol type.  */
2506   if (h->size == 0
2507       && h->type == STT_NOTYPE
2508       && !h->needs_plt)
2509     (*_bfd_error_handler)
2510       (_("warning: type and size of dynamic symbol `%s' are not defined"),
2511        h->root.root.string);
2512 
2513   dynobj = elf_hash_table (eif->info)->dynobj;
2514   bed = get_elf_backend_data (dynobj);
2515   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2516     {
2517       eif->failed = TRUE;
2518       return FALSE;
2519     }
2520 
2521   return TRUE;
2522 }
2523 
2524 /* Adjust all external symbols pointing into SEC_MERGE sections
2525    to reflect the object merging within the sections.  */
2526 
2527 bfd_boolean
2528 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2529 {
2530   asection *sec;
2531 
2532   if (h->root.type == bfd_link_hash_warning)
2533     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2534 
2535   if ((h->root.type == bfd_link_hash_defined
2536        || h->root.type == bfd_link_hash_defweak)
2537       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2538       && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2539     {
2540       bfd *output_bfd = data;
2541 
2542       h->root.u.def.value =
2543 	_bfd_merged_section_offset (output_bfd,
2544 				    &h->root.u.def.section,
2545 				    elf_section_data (sec)->sec_info,
2546 				    h->root.u.def.value);
2547     }
2548 
2549   return TRUE;
2550 }
2551 
2552 /* Returns false if the symbol referred to by H should be considered
2553    to resolve local to the current module, and true if it should be
2554    considered to bind dynamically.  */
2555 
2556 bfd_boolean
2557 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2558 			   struct bfd_link_info *info,
2559 			   bfd_boolean ignore_protected)
2560 {
2561   bfd_boolean binding_stays_local_p;
2562 
2563   if (h == NULL)
2564     return FALSE;
2565 
2566   while (h->root.type == bfd_link_hash_indirect
2567 	 || h->root.type == bfd_link_hash_warning)
2568     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2569 
2570   /* If it was forced local, then clearly it's not dynamic.  */
2571   if (h->dynindx == -1)
2572     return FALSE;
2573   if (h->forced_local)
2574     return FALSE;
2575 
2576   /* Identify the cases where name binding rules say that a
2577      visible symbol resolves locally.  */
2578   binding_stays_local_p = info->executable || info->symbolic;
2579 
2580   switch (ELF_ST_VISIBILITY (h->other))
2581     {
2582     case STV_INTERNAL:
2583     case STV_HIDDEN:
2584       return FALSE;
2585 
2586     case STV_PROTECTED:
2587       /* Proper resolution for function pointer equality may require
2588 	 that these symbols perhaps be resolved dynamically, even though
2589 	 we should be resolving them to the current module.  */
2590       if (!ignore_protected || h->type != STT_FUNC)
2591 	binding_stays_local_p = TRUE;
2592       break;
2593 
2594     default:
2595       break;
2596     }
2597 
2598   /* If it isn't defined locally, then clearly it's dynamic.  */
2599   if (!h->def_regular)
2600     return TRUE;
2601 
2602   /* Otherwise, the symbol is dynamic if binding rules don't tell
2603      us that it remains local.  */
2604   return !binding_stays_local_p;
2605 }
2606 
2607 /* Return true if the symbol referred to by H should be considered
2608    to resolve local to the current module, and false otherwise.  Differs
2609    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2610    undefined symbols and weak symbols.  */
2611 
2612 bfd_boolean
2613 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2614 			      struct bfd_link_info *info,
2615 			      bfd_boolean local_protected)
2616 {
2617   /* If it's a local sym, of course we resolve locally.  */
2618   if (h == NULL)
2619     return TRUE;
2620 
2621   /* Common symbols that become definitions don't get the DEF_REGULAR
2622      flag set, so test it first, and don't bail out.  */
2623   if (ELF_COMMON_DEF_P (h))
2624     /* Do nothing.  */;
2625   /* If we don't have a definition in a regular file, then we can't
2626      resolve locally.  The sym is either undefined or dynamic.  */
2627   else if (!h->def_regular)
2628     return FALSE;
2629 
2630   /* Forced local symbols resolve locally.  */
2631   if (h->forced_local)
2632     return TRUE;
2633 
2634   /* As do non-dynamic symbols.  */
2635   if (h->dynindx == -1)
2636     return TRUE;
2637 
2638   /* At this point, we know the symbol is defined and dynamic.  In an
2639      executable it must resolve locally, likewise when building symbolic
2640      shared libraries.  */
2641   if (info->executable || info->symbolic)
2642     return TRUE;
2643 
2644   /* Now deal with defined dynamic symbols in shared libraries.  Ones
2645      with default visibility might not resolve locally.  */
2646   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2647     return FALSE;
2648 
2649   /* However, STV_HIDDEN or STV_INTERNAL ones must be local.  */
2650   if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2651     return TRUE;
2652 
2653   /* STV_PROTECTED non-function symbols are local.  */
2654   if (h->type != STT_FUNC)
2655     return TRUE;
2656 
2657   /* Function pointer equality tests may require that STV_PROTECTED
2658      symbols be treated as dynamic symbols, even when we know that the
2659      dynamic linker will resolve them locally.  */
2660   return local_protected;
2661 }
2662 
2663 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2664    aligned.  Returns the first TLS output section.  */
2665 
2666 struct bfd_section *
2667 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2668 {
2669   struct bfd_section *sec, *tls;
2670   unsigned int align = 0;
2671 
2672   for (sec = obfd->sections; sec != NULL; sec = sec->next)
2673     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2674       break;
2675   tls = sec;
2676 
2677   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2678     if (sec->alignment_power > align)
2679       align = sec->alignment_power;
2680 
2681   elf_hash_table (info)->tls_sec = tls;
2682 
2683   /* Ensure the alignment of the first section is the largest alignment,
2684      so that the tls segment starts aligned.  */
2685   if (tls != NULL)
2686     tls->alignment_power = align;
2687 
2688   return tls;
2689 }
2690 
2691 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
2692 static bfd_boolean
2693 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2694 				  Elf_Internal_Sym *sym)
2695 {
2696   const struct elf_backend_data *bed;
2697 
2698   /* Local symbols do not count, but target specific ones might.  */
2699   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2700       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2701     return FALSE;
2702 
2703   /* Function symbols do not count.  */
2704   if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2705     return FALSE;
2706 
2707   /* If the section is undefined, then so is the symbol.  */
2708   if (sym->st_shndx == SHN_UNDEF)
2709     return FALSE;
2710 
2711   /* If the symbol is defined in the common section, then
2712      it is a common definition and so does not count.  */
2713   bed = get_elf_backend_data (abfd);
2714   if (bed->common_definition (sym))
2715     return FALSE;
2716 
2717   /* If the symbol is in a target specific section then we
2718      must rely upon the backend to tell us what it is.  */
2719   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2720     /* FIXME - this function is not coded yet:
2721 
2722        return _bfd_is_global_symbol_definition (abfd, sym);
2723 
2724        Instead for now assume that the definition is not global,
2725        Even if this is wrong, at least the linker will behave
2726        in the same way that it used to do.  */
2727     return FALSE;
2728 
2729   return TRUE;
2730 }
2731 
2732 /* Search the symbol table of the archive element of the archive ABFD
2733    whose archive map contains a mention of SYMDEF, and determine if
2734    the symbol is defined in this element.  */
2735 static bfd_boolean
2736 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2737 {
2738   Elf_Internal_Shdr * hdr;
2739   bfd_size_type symcount;
2740   bfd_size_type extsymcount;
2741   bfd_size_type extsymoff;
2742   Elf_Internal_Sym *isymbuf;
2743   Elf_Internal_Sym *isym;
2744   Elf_Internal_Sym *isymend;
2745   bfd_boolean result;
2746 
2747   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2748   if (abfd == NULL)
2749     return FALSE;
2750 
2751   if (! bfd_check_format (abfd, bfd_object))
2752     return FALSE;
2753 
2754   /* If we have already included the element containing this symbol in the
2755      link then we do not need to include it again.  Just claim that any symbol
2756      it contains is not a definition, so that our caller will not decide to
2757      (re)include this element.  */
2758   if (abfd->archive_pass)
2759     return FALSE;
2760 
2761   /* Select the appropriate symbol table.  */
2762   if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2763     hdr = &elf_tdata (abfd)->symtab_hdr;
2764   else
2765     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2766 
2767   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2768 
2769   /* The sh_info field of the symtab header tells us where the
2770      external symbols start.  We don't care about the local symbols.  */
2771   if (elf_bad_symtab (abfd))
2772     {
2773       extsymcount = symcount;
2774       extsymoff = 0;
2775     }
2776   else
2777     {
2778       extsymcount = symcount - hdr->sh_info;
2779       extsymoff = hdr->sh_info;
2780     }
2781 
2782   if (extsymcount == 0)
2783     return FALSE;
2784 
2785   /* Read in the symbol table.  */
2786   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2787 				  NULL, NULL, NULL);
2788   if (isymbuf == NULL)
2789     return FALSE;
2790 
2791   /* Scan the symbol table looking for SYMDEF.  */
2792   result = FALSE;
2793   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2794     {
2795       const char *name;
2796 
2797       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2798 					      isym->st_name);
2799       if (name == NULL)
2800 	break;
2801 
2802       if (strcmp (name, symdef->name) == 0)
2803 	{
2804 	  result = is_global_data_symbol_definition (abfd, isym);
2805 	  break;
2806 	}
2807     }
2808 
2809   free (isymbuf);
2810 
2811   return result;
2812 }
2813 
2814 /* Add an entry to the .dynamic table.  */
2815 
2816 bfd_boolean
2817 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2818 			    bfd_vma tag,
2819 			    bfd_vma val)
2820 {
2821   struct elf_link_hash_table *hash_table;
2822   const struct elf_backend_data *bed;
2823   asection *s;
2824   bfd_size_type newsize;
2825   bfd_byte *newcontents;
2826   Elf_Internal_Dyn dyn;
2827 
2828   hash_table = elf_hash_table (info);
2829   if (! is_elf_hash_table (hash_table))
2830     return FALSE;
2831 
2832   bed = get_elf_backend_data (hash_table->dynobj);
2833   s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2834   BFD_ASSERT (s != NULL);
2835 
2836   newsize = s->size + bed->s->sizeof_dyn;
2837   newcontents = bfd_realloc (s->contents, newsize);
2838   if (newcontents == NULL)
2839     return FALSE;
2840 
2841   dyn.d_tag = tag;
2842   dyn.d_un.d_val = val;
2843   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2844 
2845   s->size = newsize;
2846   s->contents = newcontents;
2847 
2848   return TRUE;
2849 }
2850 
2851 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2852    otherwise just check whether one already exists.  Returns -1 on error,
2853    1 if a DT_NEEDED tag already exists, and 0 on success.  */
2854 
2855 static int
2856 elf_add_dt_needed_tag (bfd *abfd,
2857 		       struct bfd_link_info *info,
2858 		       const char *soname,
2859 		       bfd_boolean do_it)
2860 {
2861   struct elf_link_hash_table *hash_table;
2862   bfd_size_type oldsize;
2863   bfd_size_type strindex;
2864 
2865   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2866     return -1;
2867 
2868   hash_table = elf_hash_table (info);
2869   oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2870   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2871   if (strindex == (bfd_size_type) -1)
2872     return -1;
2873 
2874   if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2875     {
2876       asection *sdyn;
2877       const struct elf_backend_data *bed;
2878       bfd_byte *extdyn;
2879 
2880       bed = get_elf_backend_data (hash_table->dynobj);
2881       sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2882       if (sdyn != NULL)
2883 	for (extdyn = sdyn->contents;
2884 	     extdyn < sdyn->contents + sdyn->size;
2885 	     extdyn += bed->s->sizeof_dyn)
2886 	  {
2887 	    Elf_Internal_Dyn dyn;
2888 
2889 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2890 	    if (dyn.d_tag == DT_NEEDED
2891 		&& dyn.d_un.d_val == strindex)
2892 	      {
2893 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2894 		return 1;
2895 	      }
2896 	  }
2897     }
2898 
2899   if (do_it)
2900     {
2901       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2902 	return -1;
2903 
2904       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2905 	return -1;
2906     }
2907   else
2908     /* We were just checking for existence of the tag.  */
2909     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2910 
2911   return 0;
2912 }
2913 
2914 /* Sort symbol by value and section.  */
2915 static int
2916 elf_sort_symbol (const void *arg1, const void *arg2)
2917 {
2918   const struct elf_link_hash_entry *h1;
2919   const struct elf_link_hash_entry *h2;
2920   bfd_signed_vma vdiff;
2921 
2922   h1 = *(const struct elf_link_hash_entry **) arg1;
2923   h2 = *(const struct elf_link_hash_entry **) arg2;
2924   vdiff = h1->root.u.def.value - h2->root.u.def.value;
2925   if (vdiff != 0)
2926     return vdiff > 0 ? 1 : -1;
2927   else
2928     {
2929       long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2930       if (sdiff != 0)
2931 	return sdiff > 0 ? 1 : -1;
2932     }
2933   return 0;
2934 }
2935 
2936 /* This function is used to adjust offsets into .dynstr for
2937    dynamic symbols.  This is called via elf_link_hash_traverse.  */
2938 
2939 static bfd_boolean
2940 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2941 {
2942   struct elf_strtab_hash *dynstr = data;
2943 
2944   if (h->root.type == bfd_link_hash_warning)
2945     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2946 
2947   if (h->dynindx != -1)
2948     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2949   return TRUE;
2950 }
2951 
2952 /* Assign string offsets in .dynstr, update all structures referencing
2953    them.  */
2954 
2955 static bfd_boolean
2956 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2957 {
2958   struct elf_link_hash_table *hash_table = elf_hash_table (info);
2959   struct elf_link_local_dynamic_entry *entry;
2960   struct elf_strtab_hash *dynstr = hash_table->dynstr;
2961   bfd *dynobj = hash_table->dynobj;
2962   asection *sdyn;
2963   bfd_size_type size;
2964   const struct elf_backend_data *bed;
2965   bfd_byte *extdyn;
2966 
2967   _bfd_elf_strtab_finalize (dynstr);
2968   size = _bfd_elf_strtab_size (dynstr);
2969 
2970   bed = get_elf_backend_data (dynobj);
2971   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2972   BFD_ASSERT (sdyn != NULL);
2973 
2974   /* Update all .dynamic entries referencing .dynstr strings.  */
2975   for (extdyn = sdyn->contents;
2976        extdyn < sdyn->contents + sdyn->size;
2977        extdyn += bed->s->sizeof_dyn)
2978     {
2979       Elf_Internal_Dyn dyn;
2980 
2981       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2982       switch (dyn.d_tag)
2983 	{
2984 	case DT_STRSZ:
2985 	  dyn.d_un.d_val = size;
2986 	  break;
2987 	case DT_NEEDED:
2988 	case DT_SONAME:
2989 	case DT_RPATH:
2990 	case DT_RUNPATH:
2991 	case DT_FILTER:
2992 	case DT_AUXILIARY:
2993 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2994 	  break;
2995 	default:
2996 	  continue;
2997 	}
2998       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2999     }
3000 
3001   /* Now update local dynamic symbols.  */
3002   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3003     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3004 						  entry->isym.st_name);
3005 
3006   /* And the rest of dynamic symbols.  */
3007   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3008 
3009   /* Adjust version definitions.  */
3010   if (elf_tdata (output_bfd)->cverdefs)
3011     {
3012       asection *s;
3013       bfd_byte *p;
3014       bfd_size_type i;
3015       Elf_Internal_Verdef def;
3016       Elf_Internal_Verdaux defaux;
3017 
3018       s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3019       p = s->contents;
3020       do
3021 	{
3022 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3023 				   &def);
3024 	  p += sizeof (Elf_External_Verdef);
3025 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
3026 	    continue;
3027 	  for (i = 0; i < def.vd_cnt; ++i)
3028 	    {
3029 	      _bfd_elf_swap_verdaux_in (output_bfd,
3030 					(Elf_External_Verdaux *) p, &defaux);
3031 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3032 							defaux.vda_name);
3033 	      _bfd_elf_swap_verdaux_out (output_bfd,
3034 					 &defaux, (Elf_External_Verdaux *) p);
3035 	      p += sizeof (Elf_External_Verdaux);
3036 	    }
3037 	}
3038       while (def.vd_next);
3039     }
3040 
3041   /* Adjust version references.  */
3042   if (elf_tdata (output_bfd)->verref)
3043     {
3044       asection *s;
3045       bfd_byte *p;
3046       bfd_size_type i;
3047       Elf_Internal_Verneed need;
3048       Elf_Internal_Vernaux needaux;
3049 
3050       s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3051       p = s->contents;
3052       do
3053 	{
3054 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3055 				    &need);
3056 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3057 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
3058 				     (Elf_External_Verneed *) p);
3059 	  p += sizeof (Elf_External_Verneed);
3060 	  for (i = 0; i < need.vn_cnt; ++i)
3061 	    {
3062 	      _bfd_elf_swap_vernaux_in (output_bfd,
3063 					(Elf_External_Vernaux *) p, &needaux);
3064 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3065 							 needaux.vna_name);
3066 	      _bfd_elf_swap_vernaux_out (output_bfd,
3067 					 &needaux,
3068 					 (Elf_External_Vernaux *) p);
3069 	      p += sizeof (Elf_External_Vernaux);
3070 	    }
3071 	}
3072       while (need.vn_next);
3073     }
3074 
3075   return TRUE;
3076 }
3077 
3078 /* Add symbols from an ELF object file to the linker hash table.  */
3079 
3080 static bfd_boolean
3081 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3082 {
3083   Elf_Internal_Shdr *hdr;
3084   bfd_size_type symcount;
3085   bfd_size_type extsymcount;
3086   bfd_size_type extsymoff;
3087   struct elf_link_hash_entry **sym_hash;
3088   bfd_boolean dynamic;
3089   Elf_External_Versym *extversym = NULL;
3090   Elf_External_Versym *ever;
3091   struct elf_link_hash_entry *weaks;
3092   struct elf_link_hash_entry **nondeflt_vers = NULL;
3093   bfd_size_type nondeflt_vers_cnt = 0;
3094   Elf_Internal_Sym *isymbuf = NULL;
3095   Elf_Internal_Sym *isym;
3096   Elf_Internal_Sym *isymend;
3097   const struct elf_backend_data *bed;
3098   bfd_boolean add_needed;
3099   struct elf_link_hash_table *htab;
3100   bfd_size_type amt;
3101   void *alloc_mark = NULL;
3102   void *old_tab = NULL;
3103   void *old_hash;
3104   void *old_ent;
3105   struct bfd_link_hash_entry *old_undefs = NULL;
3106   struct bfd_link_hash_entry *old_undefs_tail = NULL;
3107   long old_dynsymcount = 0;
3108   size_t tabsize = 0;
3109   size_t hashsize = 0;
3110 
3111   htab = elf_hash_table (info);
3112   bed = get_elf_backend_data (abfd);
3113 
3114   if ((abfd->flags & DYNAMIC) == 0)
3115     dynamic = FALSE;
3116   else
3117     {
3118       dynamic = TRUE;
3119 
3120       /* You can't use -r against a dynamic object.  Also, there's no
3121 	 hope of using a dynamic object which does not exactly match
3122 	 the format of the output file.  */
3123       if (info->relocatable
3124 	  || !is_elf_hash_table (htab)
3125 	  || htab->root.creator != abfd->xvec)
3126 	{
3127 	  if (info->relocatable)
3128 	    bfd_set_error (bfd_error_invalid_operation);
3129 	  else
3130 	    bfd_set_error (bfd_error_wrong_format);
3131 	  goto error_return;
3132 	}
3133     }
3134 
3135   /* As a GNU extension, any input sections which are named
3136      .gnu.warning.SYMBOL are treated as warning symbols for the given
3137      symbol.  This differs from .gnu.warning sections, which generate
3138      warnings when they are included in an output file.  */
3139   if (info->executable)
3140     {
3141       asection *s;
3142 
3143       for (s = abfd->sections; s != NULL; s = s->next)
3144 	{
3145 	  const char *name;
3146 
3147 	  name = bfd_get_section_name (abfd, s);
3148 	  if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3149 	    {
3150 	      char *msg;
3151 	      bfd_size_type sz;
3152 
3153 	      name += sizeof ".gnu.warning." - 1;
3154 
3155 	      /* If this is a shared object, then look up the symbol
3156 		 in the hash table.  If it is there, and it is already
3157 		 been defined, then we will not be using the entry
3158 		 from this shared object, so we don't need to warn.
3159 		 FIXME: If we see the definition in a regular object
3160 		 later on, we will warn, but we shouldn't.  The only
3161 		 fix is to keep track of what warnings we are supposed
3162 		 to emit, and then handle them all at the end of the
3163 		 link.  */
3164 	      if (dynamic)
3165 		{
3166 		  struct elf_link_hash_entry *h;
3167 
3168 		  h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3169 
3170 		  /* FIXME: What about bfd_link_hash_common?  */
3171 		  if (h != NULL
3172 		      && (h->root.type == bfd_link_hash_defined
3173 			  || h->root.type == bfd_link_hash_defweak))
3174 		    {
3175 		      /* We don't want to issue this warning.  Clobber
3176 			 the section size so that the warning does not
3177 			 get copied into the output file.  */
3178 		      s->size = 0;
3179 		      continue;
3180 		    }
3181 		}
3182 
3183 	      sz = s->size;
3184 	      msg = bfd_alloc (abfd, sz + 1);
3185 	      if (msg == NULL)
3186 		goto error_return;
3187 
3188 	      if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3189 		goto error_return;
3190 
3191 	      msg[sz] = '\0';
3192 
3193 	      if (! (_bfd_generic_link_add_one_symbol
3194 		     (info, abfd, name, BSF_WARNING, s, 0, msg,
3195 		      FALSE, bed->collect, NULL)))
3196 		goto error_return;
3197 
3198 	      if (! info->relocatable)
3199 		{
3200 		  /* Clobber the section size so that the warning does
3201 		     not get copied into the output file.  */
3202 		  s->size = 0;
3203 
3204 		  /* Also set SEC_EXCLUDE, so that symbols defined in
3205 		     the warning section don't get copied to the output.  */
3206 		  s->flags |= SEC_EXCLUDE;
3207 		}
3208 	    }
3209 	}
3210     }
3211 
3212   add_needed = TRUE;
3213   if (! dynamic)
3214     {
3215       /* If we are creating a shared library, create all the dynamic
3216 	 sections immediately.  We need to attach them to something,
3217 	 so we attach them to this BFD, provided it is the right
3218 	 format.  FIXME: If there are no input BFD's of the same
3219 	 format as the output, we can't make a shared library.  */
3220       if (info->shared
3221 	  && is_elf_hash_table (htab)
3222 	  && htab->root.creator == abfd->xvec
3223 	  && !htab->dynamic_sections_created)
3224 	{
3225 	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3226 	    goto error_return;
3227 	}
3228     }
3229   else if (!is_elf_hash_table (htab))
3230     goto error_return;
3231   else
3232     {
3233       asection *s;
3234       const char *soname = NULL;
3235       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3236       int ret;
3237 
3238       /* ld --just-symbols and dynamic objects don't mix very well.
3239 	 ld shouldn't allow it.  */
3240       if ((s = abfd->sections) != NULL
3241 	  && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3242 	abort ();
3243 
3244       /* If this dynamic lib was specified on the command line with
3245 	 --as-needed in effect, then we don't want to add a DT_NEEDED
3246 	 tag unless the lib is actually used.  Similary for libs brought
3247 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
3248 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3249 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
3250 	 all.  */
3251       add_needed = (elf_dyn_lib_class (abfd)
3252 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
3253 		       | DYN_NO_NEEDED)) == 0;
3254 
3255       s = bfd_get_section_by_name (abfd, ".dynamic");
3256       if (s != NULL)
3257 	{
3258 	  bfd_byte *dynbuf;
3259 	  bfd_byte *extdyn;
3260 	  int elfsec;
3261 	  unsigned long shlink;
3262 
3263 	  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3264 	    goto error_free_dyn;
3265 
3266 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3267 	  if (elfsec == -1)
3268 	    goto error_free_dyn;
3269 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3270 
3271 	  for (extdyn = dynbuf;
3272 	       extdyn < dynbuf + s->size;
3273 	       extdyn += bed->s->sizeof_dyn)
3274 	    {
3275 	      Elf_Internal_Dyn dyn;
3276 
3277 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3278 	      if (dyn.d_tag == DT_SONAME)
3279 		{
3280 		  unsigned int tagv = dyn.d_un.d_val;
3281 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3282 		  if (soname == NULL)
3283 		    goto error_free_dyn;
3284 		}
3285 	      if (dyn.d_tag == DT_NEEDED)
3286 		{
3287 		  struct bfd_link_needed_list *n, **pn;
3288 		  char *fnm, *anm;
3289 		  unsigned int tagv = dyn.d_un.d_val;
3290 
3291 		  amt = sizeof (struct bfd_link_needed_list);
3292 		  n = bfd_alloc (abfd, amt);
3293 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3294 		  if (n == NULL || fnm == NULL)
3295 		    goto error_free_dyn;
3296 		  amt = strlen (fnm) + 1;
3297 		  anm = bfd_alloc (abfd, amt);
3298 		  if (anm == NULL)
3299 		    goto error_free_dyn;
3300 		  memcpy (anm, fnm, amt);
3301 		  n->name = anm;
3302 		  n->by = abfd;
3303 		  n->next = NULL;
3304 		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3305 		    ;
3306 		  *pn = n;
3307 		}
3308 	      if (dyn.d_tag == DT_RUNPATH)
3309 		{
3310 		  struct bfd_link_needed_list *n, **pn;
3311 		  char *fnm, *anm;
3312 		  unsigned int tagv = dyn.d_un.d_val;
3313 
3314 		  amt = sizeof (struct bfd_link_needed_list);
3315 		  n = bfd_alloc (abfd, amt);
3316 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3317 		  if (n == NULL || fnm == NULL)
3318 		    goto error_free_dyn;
3319 		  amt = strlen (fnm) + 1;
3320 		  anm = bfd_alloc (abfd, amt);
3321 		  if (anm == NULL)
3322 		    goto error_free_dyn;
3323 		  memcpy (anm, fnm, amt);
3324 		  n->name = anm;
3325 		  n->by = abfd;
3326 		  n->next = NULL;
3327 		  for (pn = & runpath;
3328 		       *pn != NULL;
3329 		       pn = &(*pn)->next)
3330 		    ;
3331 		  *pn = n;
3332 		}
3333 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
3334 	      if (!runpath && dyn.d_tag == DT_RPATH)
3335 		{
3336 		  struct bfd_link_needed_list *n, **pn;
3337 		  char *fnm, *anm;
3338 		  unsigned int tagv = dyn.d_un.d_val;
3339 
3340 		  amt = sizeof (struct bfd_link_needed_list);
3341 		  n = bfd_alloc (abfd, amt);
3342 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3343 		  if (n == NULL || fnm == NULL)
3344 		    goto error_free_dyn;
3345 		  amt = strlen (fnm) + 1;
3346 		  anm = bfd_alloc (abfd, amt);
3347 		  if (anm == NULL)
3348 		    {
3349 		    error_free_dyn:
3350 		      free (dynbuf);
3351 		      goto error_return;
3352 		    }
3353 		  memcpy (anm, fnm, amt);
3354 		  n->name = anm;
3355 		  n->by = abfd;
3356 		  n->next = NULL;
3357 		  for (pn = & rpath;
3358 		       *pn != NULL;
3359 		       pn = &(*pn)->next)
3360 		    ;
3361 		  *pn = n;
3362 		}
3363 	    }
3364 
3365 	  free (dynbuf);
3366 	}
3367 
3368       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
3369 	 frees all more recently bfd_alloc'd blocks as well.  */
3370       if (runpath)
3371 	rpath = runpath;
3372 
3373       if (rpath)
3374 	{
3375 	  struct bfd_link_needed_list **pn;
3376 	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3377 	    ;
3378 	  *pn = rpath;
3379 	}
3380 
3381       /* We do not want to include any of the sections in a dynamic
3382 	 object in the output file.  We hack by simply clobbering the
3383 	 list of sections in the BFD.  This could be handled more
3384 	 cleanly by, say, a new section flag; the existing
3385 	 SEC_NEVER_LOAD flag is not the one we want, because that one
3386 	 still implies that the section takes up space in the output
3387 	 file.  */
3388       bfd_section_list_clear (abfd);
3389 
3390       /* Find the name to use in a DT_NEEDED entry that refers to this
3391 	 object.  If the object has a DT_SONAME entry, we use it.
3392 	 Otherwise, if the generic linker stuck something in
3393 	 elf_dt_name, we use that.  Otherwise, we just use the file
3394 	 name.  */
3395       if (soname == NULL || *soname == '\0')
3396 	{
3397 	  soname = elf_dt_name (abfd);
3398 	  if (soname == NULL || *soname == '\0')
3399 	    soname = bfd_get_filename (abfd);
3400 	}
3401 
3402       /* Save the SONAME because sometimes the linker emulation code
3403 	 will need to know it.  */
3404       elf_dt_name (abfd) = soname;
3405 
3406       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3407       if (ret < 0)
3408 	goto error_return;
3409 
3410       /* If we have already included this dynamic object in the
3411 	 link, just ignore it.  There is no reason to include a
3412 	 particular dynamic object more than once.  */
3413       if (ret > 0)
3414 	return TRUE;
3415     }
3416 
3417   /* If this is a dynamic object, we always link against the .dynsym
3418      symbol table, not the .symtab symbol table.  The dynamic linker
3419      will only see the .dynsym symbol table, so there is no reason to
3420      look at .symtab for a dynamic object.  */
3421 
3422   if (! dynamic || elf_dynsymtab (abfd) == 0)
3423     hdr = &elf_tdata (abfd)->symtab_hdr;
3424   else
3425     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3426 
3427   symcount = hdr->sh_size / bed->s->sizeof_sym;
3428 
3429   /* The sh_info field of the symtab header tells us where the
3430      external symbols start.  We don't care about the local symbols at
3431      this point.  */
3432   if (elf_bad_symtab (abfd))
3433     {
3434       extsymcount = symcount;
3435       extsymoff = 0;
3436     }
3437   else
3438     {
3439       extsymcount = symcount - hdr->sh_info;
3440       extsymoff = hdr->sh_info;
3441     }
3442 
3443   sym_hash = NULL;
3444   if (extsymcount != 0)
3445     {
3446       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3447 				      NULL, NULL, NULL);
3448       if (isymbuf == NULL)
3449 	goto error_return;
3450 
3451       /* We store a pointer to the hash table entry for each external
3452 	 symbol.  */
3453       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3454       sym_hash = bfd_alloc (abfd, amt);
3455       if (sym_hash == NULL)
3456 	goto error_free_sym;
3457       elf_sym_hashes (abfd) = sym_hash;
3458     }
3459 
3460   if (dynamic)
3461     {
3462       /* Read in any version definitions.  */
3463       if (!_bfd_elf_slurp_version_tables (abfd,
3464 					  info->default_imported_symver))
3465 	goto error_free_sym;
3466 
3467       /* Read in the symbol versions, but don't bother to convert them
3468 	 to internal format.  */
3469       if (elf_dynversym (abfd) != 0)
3470 	{
3471 	  Elf_Internal_Shdr *versymhdr;
3472 
3473 	  versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3474 	  extversym = bfd_malloc (versymhdr->sh_size);
3475 	  if (extversym == NULL)
3476 	    goto error_free_sym;
3477 	  amt = versymhdr->sh_size;
3478 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3479 	      || bfd_bread (extversym, amt, abfd) != amt)
3480 	    goto error_free_vers;
3481 	}
3482     }
3483 
3484   /* If we are loading an as-needed shared lib, save the symbol table
3485      state before we start adding symbols.  If the lib turns out
3486      to be unneeded, restore the state.  */
3487   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3488     {
3489       unsigned int i;
3490       size_t entsize;
3491 
3492       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3493 	{
3494 	  struct bfd_hash_entry *p;
3495 	  struct elf_link_hash_entry *h;
3496 
3497 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3498 	    {
3499 	      h = (struct elf_link_hash_entry *) p;
3500 	      entsize += htab->root.table.entsize;
3501 	      if (h->root.type == bfd_link_hash_warning)
3502 		entsize += htab->root.table.entsize;
3503 	    }
3504 	}
3505 
3506       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3507       hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3508       old_tab = bfd_malloc (tabsize + entsize + hashsize);
3509       if (old_tab == NULL)
3510 	goto error_free_vers;
3511 
3512       /* Remember the current objalloc pointer, so that all mem for
3513 	 symbols added can later be reclaimed.  */
3514       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3515       if (alloc_mark == NULL)
3516 	goto error_free_vers;
3517 
3518       /* Clone the symbol table and sym hashes.  Remember some
3519 	 pointers into the symbol table, and dynamic symbol count.  */
3520       old_hash = (char *) old_tab + tabsize;
3521       old_ent = (char *) old_hash + hashsize;
3522       memcpy (old_tab, htab->root.table.table, tabsize);
3523       memcpy (old_hash, sym_hash, hashsize);
3524       old_undefs = htab->root.undefs;
3525       old_undefs_tail = htab->root.undefs_tail;
3526       old_dynsymcount = htab->dynsymcount;
3527 
3528       for (i = 0; i < htab->root.table.size; i++)
3529 	{
3530 	  struct bfd_hash_entry *p;
3531 	  struct elf_link_hash_entry *h;
3532 
3533 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3534 	    {
3535 	      memcpy (old_ent, p, htab->root.table.entsize);
3536 	      old_ent = (char *) old_ent + htab->root.table.entsize;
3537 	      h = (struct elf_link_hash_entry *) p;
3538 	      if (h->root.type == bfd_link_hash_warning)
3539 		{
3540 		  memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3541 		  old_ent = (char *) old_ent + htab->root.table.entsize;
3542 		}
3543 	    }
3544 	}
3545     }
3546 
3547   weaks = NULL;
3548   ever = extversym != NULL ? extversym + extsymoff : NULL;
3549   for (isym = isymbuf, isymend = isymbuf + extsymcount;
3550        isym < isymend;
3551        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3552     {
3553       int bind;
3554       bfd_vma value;
3555       asection *sec, *new_sec;
3556       flagword flags;
3557       const char *name;
3558       struct elf_link_hash_entry *h;
3559       bfd_boolean definition;
3560       bfd_boolean size_change_ok;
3561       bfd_boolean type_change_ok;
3562       bfd_boolean new_weakdef;
3563       bfd_boolean override;
3564       bfd_boolean common;
3565       unsigned int old_alignment;
3566       bfd *old_bfd;
3567 
3568       override = FALSE;
3569 
3570       flags = BSF_NO_FLAGS;
3571       sec = NULL;
3572       value = isym->st_value;
3573       *sym_hash = NULL;
3574       common = bed->common_definition (isym);
3575 
3576       bind = ELF_ST_BIND (isym->st_info);
3577       if (bind == STB_LOCAL)
3578 	{
3579 	  /* This should be impossible, since ELF requires that all
3580 	     global symbols follow all local symbols, and that sh_info
3581 	     point to the first global symbol.  Unfortunately, Irix 5
3582 	     screws this up.  */
3583 	  continue;
3584 	}
3585       else if (bind == STB_GLOBAL)
3586 	{
3587 	  if (isym->st_shndx != SHN_UNDEF && !common)
3588 	    flags = BSF_GLOBAL;
3589 	}
3590       else if (bind == STB_WEAK)
3591 	flags = BSF_WEAK;
3592       else
3593 	{
3594 	  /* Leave it up to the processor backend.  */
3595 	}
3596 
3597       if (isym->st_shndx == SHN_UNDEF)
3598 	sec = bfd_und_section_ptr;
3599       else if (isym->st_shndx < SHN_LORESERVE
3600 	       || isym->st_shndx > SHN_HIRESERVE)
3601 	{
3602 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3603 	  if (sec == NULL)
3604 	    sec = bfd_abs_section_ptr;
3605 	  else if (sec->kept_section)
3606 	    {
3607 	      /* Symbols from discarded section are undefined.  We keep
3608 		 its visibility.  */
3609 	      sec = bfd_und_section_ptr;
3610 	      isym->st_shndx = SHN_UNDEF;
3611 	    }
3612 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3613 	    value -= sec->vma;
3614 	}
3615       else if (isym->st_shndx == SHN_ABS)
3616 	sec = bfd_abs_section_ptr;
3617       else if (isym->st_shndx == SHN_COMMON)
3618 	{
3619 	  sec = bfd_com_section_ptr;
3620 	  /* What ELF calls the size we call the value.  What ELF
3621 	     calls the value we call the alignment.  */
3622 	  value = isym->st_size;
3623 	}
3624       else
3625 	{
3626 	  /* Leave it up to the processor backend.  */
3627 	}
3628 
3629       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3630 					      isym->st_name);
3631       if (name == NULL)
3632 	goto error_free_vers;
3633 
3634       if (isym->st_shndx == SHN_COMMON
3635 	  && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3636 	{
3637 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3638 
3639 	  if (tcomm == NULL)
3640 	    {
3641 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3642 						   (SEC_ALLOC
3643 						    | SEC_IS_COMMON
3644 						    | SEC_LINKER_CREATED
3645 						    | SEC_THREAD_LOCAL));
3646 	      if (tcomm == NULL)
3647 		goto error_free_vers;
3648 	    }
3649 	  sec = tcomm;
3650 	}
3651       else if (bed->elf_add_symbol_hook)
3652 	{
3653 	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3654 					     &sec, &value))
3655 	    goto error_free_vers;
3656 
3657 	  /* The hook function sets the name to NULL if this symbol
3658 	     should be skipped for some reason.  */
3659 	  if (name == NULL)
3660 	    continue;
3661 	}
3662 
3663       /* Sanity check that all possibilities were handled.  */
3664       if (sec == NULL)
3665 	{
3666 	  bfd_set_error (bfd_error_bad_value);
3667 	  goto error_free_vers;
3668 	}
3669 
3670       if (bfd_is_und_section (sec)
3671 	  || bfd_is_com_section (sec))
3672 	definition = FALSE;
3673       else
3674 	definition = TRUE;
3675 
3676       size_change_ok = FALSE;
3677       type_change_ok = bed->type_change_ok;
3678       old_alignment = 0;
3679       old_bfd = NULL;
3680       new_sec = sec;
3681 
3682       if (is_elf_hash_table (htab))
3683 	{
3684 	  Elf_Internal_Versym iver;
3685 	  unsigned int vernum = 0;
3686 	  bfd_boolean skip;
3687 
3688 	  if (ever == NULL)
3689 	    {
3690 	      if (info->default_imported_symver)
3691 		/* Use the default symbol version created earlier.  */
3692 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
3693 	      else
3694 		iver.vs_vers = 0;
3695 	    }
3696 	  else
3697 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
3698 
3699 	  vernum = iver.vs_vers & VERSYM_VERSION;
3700 
3701 	  /* If this is a hidden symbol, or if it is not version
3702 	     1, we append the version name to the symbol name.
3703 	     However, we do not modify a non-hidden absolute symbol
3704 	     if it is not a function, because it might be the version
3705 	     symbol itself.  FIXME: What if it isn't?  */
3706 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3707 	      || (vernum > 1 && (! bfd_is_abs_section (sec)
3708 				 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3709 	    {
3710 	      const char *verstr;
3711 	      size_t namelen, verlen, newlen;
3712 	      char *newname, *p;
3713 
3714 	      if (isym->st_shndx != SHN_UNDEF)
3715 		{
3716 		  if (vernum > elf_tdata (abfd)->cverdefs)
3717 		    verstr = NULL;
3718 		  else if (vernum > 1)
3719 		    verstr =
3720 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3721 		  else
3722 		    verstr = "";
3723 
3724 		  if (verstr == NULL)
3725 		    {
3726 		      (*_bfd_error_handler)
3727 			(_("%B: %s: invalid version %u (max %d)"),
3728 			 abfd, name, vernum,
3729 			 elf_tdata (abfd)->cverdefs);
3730 		      bfd_set_error (bfd_error_bad_value);
3731 		      goto error_free_vers;
3732 		    }
3733 		}
3734 	      else
3735 		{
3736 		  /* We cannot simply test for the number of
3737 		     entries in the VERNEED section since the
3738 		     numbers for the needed versions do not start
3739 		     at 0.  */
3740 		  Elf_Internal_Verneed *t;
3741 
3742 		  verstr = NULL;
3743 		  for (t = elf_tdata (abfd)->verref;
3744 		       t != NULL;
3745 		       t = t->vn_nextref)
3746 		    {
3747 		      Elf_Internal_Vernaux *a;
3748 
3749 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3750 			{
3751 			  if (a->vna_other == vernum)
3752 			    {
3753 			      verstr = a->vna_nodename;
3754 			      break;
3755 			    }
3756 			}
3757 		      if (a != NULL)
3758 			break;
3759 		    }
3760 		  if (verstr == NULL)
3761 		    {
3762 		      (*_bfd_error_handler)
3763 			(_("%B: %s: invalid needed version %d"),
3764 			 abfd, name, vernum);
3765 		      bfd_set_error (bfd_error_bad_value);
3766 		      goto error_free_vers;
3767 		    }
3768 		}
3769 
3770 	      namelen = strlen (name);
3771 	      verlen = strlen (verstr);
3772 	      newlen = namelen + verlen + 2;
3773 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3774 		  && isym->st_shndx != SHN_UNDEF)
3775 		++newlen;
3776 
3777 	      newname = bfd_hash_allocate (&htab->root.table, newlen);
3778 	      if (newname == NULL)
3779 		goto error_free_vers;
3780 	      memcpy (newname, name, namelen);
3781 	      p = newname + namelen;
3782 	      *p++ = ELF_VER_CHR;
3783 	      /* If this is a defined non-hidden version symbol,
3784 		 we add another @ to the name.  This indicates the
3785 		 default version of the symbol.  */
3786 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3787 		  && isym->st_shndx != SHN_UNDEF)
3788 		*p++ = ELF_VER_CHR;
3789 	      memcpy (p, verstr, verlen + 1);
3790 
3791 	      name = newname;
3792 	    }
3793 
3794 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3795 				      &value, &old_alignment,
3796 				      sym_hash, &skip, &override,
3797 				      &type_change_ok, &size_change_ok))
3798 	    goto error_free_vers;
3799 
3800 	  if (skip)
3801 	    continue;
3802 
3803 	  if (override)
3804 	    definition = FALSE;
3805 
3806 	  h = *sym_hash;
3807 	  while (h->root.type == bfd_link_hash_indirect
3808 		 || h->root.type == bfd_link_hash_warning)
3809 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3810 
3811 	  /* Remember the old alignment if this is a common symbol, so
3812 	     that we don't reduce the alignment later on.  We can't
3813 	     check later, because _bfd_generic_link_add_one_symbol
3814 	     will set a default for the alignment which we want to
3815 	     override. We also remember the old bfd where the existing
3816 	     definition comes from.  */
3817 	  switch (h->root.type)
3818 	    {
3819 	    default:
3820 	      break;
3821 
3822 	    case bfd_link_hash_defined:
3823 	    case bfd_link_hash_defweak:
3824 	      old_bfd = h->root.u.def.section->owner;
3825 	      break;
3826 
3827 	    case bfd_link_hash_common:
3828 	      old_bfd = h->root.u.c.p->section->owner;
3829 	      old_alignment = h->root.u.c.p->alignment_power;
3830 	      break;
3831 	    }
3832 
3833 	  if (elf_tdata (abfd)->verdef != NULL
3834 	      && ! override
3835 	      && vernum > 1
3836 	      && definition)
3837 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3838 	}
3839 
3840       if (! (_bfd_generic_link_add_one_symbol
3841 	     (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
3842 	      (struct bfd_link_hash_entry **) sym_hash)))
3843 	goto error_free_vers;
3844 
3845       h = *sym_hash;
3846       while (h->root.type == bfd_link_hash_indirect
3847 	     || h->root.type == bfd_link_hash_warning)
3848 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
3849       *sym_hash = h;
3850 
3851       new_weakdef = FALSE;
3852       if (dynamic
3853 	  && definition
3854 	  && (flags & BSF_WEAK) != 0
3855 	  && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3856 	  && is_elf_hash_table (htab)
3857 	  && h->u.weakdef == NULL)
3858 	{
3859 	  /* Keep a list of all weak defined non function symbols from
3860 	     a dynamic object, using the weakdef field.  Later in this
3861 	     function we will set the weakdef field to the correct
3862 	     value.  We only put non-function symbols from dynamic
3863 	     objects on this list, because that happens to be the only
3864 	     time we need to know the normal symbol corresponding to a
3865 	     weak symbol, and the information is time consuming to
3866 	     figure out.  If the weakdef field is not already NULL,
3867 	     then this symbol was already defined by some previous
3868 	     dynamic object, and we will be using that previous
3869 	     definition anyhow.  */
3870 
3871 	  h->u.weakdef = weaks;
3872 	  weaks = h;
3873 	  new_weakdef = TRUE;
3874 	}
3875 
3876       /* Set the alignment of a common symbol.  */
3877       if ((common || bfd_is_com_section (sec))
3878 	  && h->root.type == bfd_link_hash_common)
3879 	{
3880 	  unsigned int align;
3881 
3882 	  if (common)
3883 	    align = bfd_log2 (isym->st_value);
3884 	  else
3885 	    {
3886 	      /* The new symbol is a common symbol in a shared object.
3887 		 We need to get the alignment from the section.  */
3888 	      align = new_sec->alignment_power;
3889 	    }
3890 	  if (align > old_alignment
3891 	      /* Permit an alignment power of zero if an alignment of one
3892 		 is specified and no other alignments have been specified.  */
3893 	      || (isym->st_value == 1 && old_alignment == 0))
3894 	    h->root.u.c.p->alignment_power = align;
3895 	  else
3896 	    h->root.u.c.p->alignment_power = old_alignment;
3897 	}
3898 
3899       if (is_elf_hash_table (htab))
3900 	{
3901 	  bfd_boolean dynsym;
3902 
3903 	  /* Check the alignment when a common symbol is involved. This
3904 	     can change when a common symbol is overridden by a normal
3905 	     definition or a common symbol is ignored due to the old
3906 	     normal definition. We need to make sure the maximum
3907 	     alignment is maintained.  */
3908 	  if ((old_alignment || common)
3909 	      && h->root.type != bfd_link_hash_common)
3910 	    {
3911 	      unsigned int common_align;
3912 	      unsigned int normal_align;
3913 	      unsigned int symbol_align;
3914 	      bfd *normal_bfd;
3915 	      bfd *common_bfd;
3916 
3917 	      symbol_align = ffs (h->root.u.def.value) - 1;
3918 	      if (h->root.u.def.section->owner != NULL
3919 		  && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3920 		{
3921 		  normal_align = h->root.u.def.section->alignment_power;
3922 		  if (normal_align > symbol_align)
3923 		    normal_align = symbol_align;
3924 		}
3925 	      else
3926 		normal_align = symbol_align;
3927 
3928 	      if (old_alignment)
3929 		{
3930 		  common_align = old_alignment;
3931 		  common_bfd = old_bfd;
3932 		  normal_bfd = abfd;
3933 		}
3934 	      else
3935 		{
3936 		  common_align = bfd_log2 (isym->st_value);
3937 		  common_bfd = abfd;
3938 		  normal_bfd = old_bfd;
3939 		}
3940 
3941 	      if (normal_align < common_align)
3942 		(*_bfd_error_handler)
3943 		  (_("Warning: alignment %u of symbol `%s' in %B"
3944 		     " is smaller than %u in %B"),
3945 		   normal_bfd, common_bfd,
3946 		   1 << normal_align, name, 1 << common_align);
3947 	    }
3948 
3949 	  /* Remember the symbol size and type.  */
3950 	  if (isym->st_size != 0
3951 	      && (definition || h->size == 0))
3952 	    {
3953 	      if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3954 		(*_bfd_error_handler)
3955 		  (_("Warning: size of symbol `%s' changed"
3956 		     " from %lu in %B to %lu in %B"),
3957 		   old_bfd, abfd,
3958 		   name, (unsigned long) h->size,
3959 		   (unsigned long) isym->st_size);
3960 
3961 	      h->size = isym->st_size;
3962 	    }
3963 
3964 	  /* If this is a common symbol, then we always want H->SIZE
3965 	     to be the size of the common symbol.  The code just above
3966 	     won't fix the size if a common symbol becomes larger.  We
3967 	     don't warn about a size change here, because that is
3968 	     covered by --warn-common.  */
3969 	  if (h->root.type == bfd_link_hash_common)
3970 	    h->size = h->root.u.c.size;
3971 
3972 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3973 	      && (definition || h->type == STT_NOTYPE))
3974 	    {
3975 	      if (h->type != STT_NOTYPE
3976 		  && h->type != ELF_ST_TYPE (isym->st_info)
3977 		  && ! type_change_ok)
3978 		(*_bfd_error_handler)
3979 		  (_("Warning: type of symbol `%s' changed"
3980 		     " from %d to %d in %B"),
3981 		   abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3982 
3983 	      h->type = ELF_ST_TYPE (isym->st_info);
3984 	    }
3985 
3986 	  /* If st_other has a processor-specific meaning, specific
3987 	     code might be needed here. We never merge the visibility
3988 	     attribute with the one from a dynamic object.  */
3989 	  if (bed->elf_backend_merge_symbol_attribute)
3990 	    (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3991 							dynamic);
3992 
3993 	  /* If this symbol has default visibility and the user has requested
3994 	     we not re-export it, then mark it as hidden.  */
3995 	  if (definition && !dynamic
3996 	      && (abfd->no_export
3997 		  || (abfd->my_archive && abfd->my_archive->no_export))
3998 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3999 	    isym->st_other = (STV_HIDDEN
4000 			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4001 
4002 	  if (isym->st_other != 0 && !dynamic)
4003 	    {
4004 	      unsigned char hvis, symvis, other, nvis;
4005 
4006 	      /* Take the balance of OTHER from the definition.  */
4007 	      other = (definition ? isym->st_other : h->other);
4008 	      other &= ~ ELF_ST_VISIBILITY (-1);
4009 
4010 	      /* Combine visibilities, using the most constraining one.  */
4011 	      hvis   = ELF_ST_VISIBILITY (h->other);
4012 	      symvis = ELF_ST_VISIBILITY (isym->st_other);
4013 	      if (! hvis)
4014 		nvis = symvis;
4015 	      else if (! symvis)
4016 		nvis = hvis;
4017 	      else
4018 		nvis = hvis < symvis ? hvis : symvis;
4019 
4020 	      h->other = other | nvis;
4021 	    }
4022 
4023 	  /* Set a flag in the hash table entry indicating the type of
4024 	     reference or definition we just found.  Keep a count of
4025 	     the number of dynamic symbols we find.  A dynamic symbol
4026 	     is one which is referenced or defined by both a regular
4027 	     object and a shared object.  */
4028 	  dynsym = FALSE;
4029 	  if (! dynamic)
4030 	    {
4031 	      if (! definition)
4032 		{
4033 		  h->ref_regular = 1;
4034 		  if (bind != STB_WEAK)
4035 		    h->ref_regular_nonweak = 1;
4036 		}
4037 	      else
4038 		h->def_regular = 1;
4039 	      if (! info->executable
4040 		  || h->def_dynamic
4041 		  || h->ref_dynamic)
4042 		dynsym = TRUE;
4043 	    }
4044 	  else
4045 	    {
4046 	      if (! definition)
4047 		h->ref_dynamic = 1;
4048 	      else
4049 		h->def_dynamic = 1;
4050 	      if (h->def_regular
4051 		  || h->ref_regular
4052 		  || (h->u.weakdef != NULL
4053 		      && ! new_weakdef
4054 		      && h->u.weakdef->dynindx != -1))
4055 		dynsym = TRUE;
4056 	    }
4057 
4058 	  /* Check to see if we need to add an indirect symbol for
4059 	     the default name.  */
4060 	  if (definition || h->root.type == bfd_link_hash_common)
4061 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4062 					      &sec, &value, &dynsym,
4063 					      override))
4064 	      goto error_free_vers;
4065 
4066 	  if (definition && !dynamic)
4067 	    {
4068 	      char *p = strchr (name, ELF_VER_CHR);
4069 	      if (p != NULL && p[1] != ELF_VER_CHR)
4070 		{
4071 		  /* Queue non-default versions so that .symver x, x@FOO
4072 		     aliases can be checked.  */
4073 		  if (!nondeflt_vers)
4074 		    {
4075 		      amt = ((isymend - isym + 1)
4076 			     * sizeof (struct elf_link_hash_entry *));
4077 		      nondeflt_vers = bfd_malloc (amt);
4078 		    }
4079 		  nondeflt_vers[nondeflt_vers_cnt++] = h;
4080 		}
4081 	    }
4082 
4083 	  if (dynsym && h->dynindx == -1)
4084 	    {
4085 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4086 		goto error_free_vers;
4087 	      if (h->u.weakdef != NULL
4088 		  && ! new_weakdef
4089 		  && h->u.weakdef->dynindx == -1)
4090 		{
4091 		  if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4092 		    goto error_free_vers;
4093 		}
4094 	    }
4095 	  else if (dynsym && h->dynindx != -1)
4096 	    /* If the symbol already has a dynamic index, but
4097 	       visibility says it should not be visible, turn it into
4098 	       a local symbol.  */
4099 	    switch (ELF_ST_VISIBILITY (h->other))
4100 	      {
4101 	      case STV_INTERNAL:
4102 	      case STV_HIDDEN:
4103 		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
4104 		dynsym = FALSE;
4105 		break;
4106 	      }
4107 
4108 	  if (!add_needed
4109 	      && definition
4110 	      && dynsym
4111 	      && h->ref_regular)
4112 	    {
4113 	      int ret;
4114 	      const char *soname = elf_dt_name (abfd);
4115 
4116 	      /* A symbol from a library loaded via DT_NEEDED of some
4117 		 other library is referenced by a regular object.
4118 		 Add a DT_NEEDED entry for it.  Issue an error if
4119 		 --no-add-needed is used.  */
4120 	      if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4121 		{
4122 		  (*_bfd_error_handler)
4123 		    (_("%s: invalid DSO for symbol `%s' definition"),
4124 		     abfd, name);
4125 		  bfd_set_error (bfd_error_bad_value);
4126 		  goto error_free_vers;
4127 		}
4128 
4129 	      elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4130 
4131 	      add_needed = TRUE;
4132 	      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4133 	      if (ret < 0)
4134 		goto error_free_vers;
4135 
4136 	      BFD_ASSERT (ret == 0);
4137 	    }
4138 	}
4139     }
4140 
4141   if (extversym != NULL)
4142     {
4143       free (extversym);
4144       extversym = NULL;
4145     }
4146 
4147   if (isymbuf != NULL)
4148     {
4149       free (isymbuf);
4150       isymbuf = NULL;
4151     }
4152 
4153   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4154     {
4155       unsigned int i;
4156 
4157       /* Restore the symbol table.  */
4158       old_hash = (char *) old_tab + tabsize;
4159       old_ent = (char *) old_hash + hashsize;
4160       sym_hash = elf_sym_hashes (abfd);
4161       memcpy (htab->root.table.table, old_tab, tabsize);
4162       memcpy (sym_hash, old_hash, hashsize);
4163       htab->root.undefs = old_undefs;
4164       htab->root.undefs_tail = old_undefs_tail;
4165       for (i = 0; i < htab->root.table.size; i++)
4166 	{
4167 	  struct bfd_hash_entry *p;
4168 	  struct elf_link_hash_entry *h;
4169 
4170 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4171 	    {
4172 	      h = (struct elf_link_hash_entry *) p;
4173 	      if (h->root.type == bfd_link_hash_warning)
4174 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
4175 	      if (h->dynindx >= old_dynsymcount)
4176 		_bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4177 
4178 	      memcpy (p, old_ent, htab->root.table.entsize);
4179 	      old_ent = (char *) old_ent + htab->root.table.entsize;
4180 	      h = (struct elf_link_hash_entry *) p;
4181 	      if (h->root.type == bfd_link_hash_warning)
4182 		{
4183 		  memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4184 		  old_ent = (char *) old_ent + htab->root.table.entsize;
4185 		}
4186 	    }
4187 	}
4188 
4189       free (old_tab);
4190       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4191 			   alloc_mark);
4192       if (nondeflt_vers != NULL)
4193 	free (nondeflt_vers);
4194       return TRUE;
4195     }
4196 
4197   if (old_tab != NULL)
4198     {
4199       free (old_tab);
4200       old_tab = NULL;
4201     }
4202 
4203   /* Now that all the symbols from this input file are created, handle
4204      .symver foo, foo@BAR such that any relocs against foo become foo@BAR.  */
4205   if (nondeflt_vers != NULL)
4206     {
4207       bfd_size_type cnt, symidx;
4208 
4209       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4210 	{
4211 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4212 	  char *shortname, *p;
4213 
4214 	  p = strchr (h->root.root.string, ELF_VER_CHR);
4215 	  if (p == NULL
4216 	      || (h->root.type != bfd_link_hash_defined
4217 		  && h->root.type != bfd_link_hash_defweak))
4218 	    continue;
4219 
4220 	  amt = p - h->root.root.string;
4221 	  shortname = bfd_malloc (amt + 1);
4222 	  memcpy (shortname, h->root.root.string, amt);
4223 	  shortname[amt] = '\0';
4224 
4225 	  hi = (struct elf_link_hash_entry *)
4226 	       bfd_link_hash_lookup (&htab->root, shortname,
4227 				     FALSE, FALSE, FALSE);
4228 	  if (hi != NULL
4229 	      && hi->root.type == h->root.type
4230 	      && hi->root.u.def.value == h->root.u.def.value
4231 	      && hi->root.u.def.section == h->root.u.def.section)
4232 	    {
4233 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4234 	      hi->root.type = bfd_link_hash_indirect;
4235 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4236 	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4237 	      sym_hash = elf_sym_hashes (abfd);
4238 	      if (sym_hash)
4239 		for (symidx = 0; symidx < extsymcount; ++symidx)
4240 		  if (sym_hash[symidx] == hi)
4241 		    {
4242 		      sym_hash[symidx] = h;
4243 		      break;
4244 		    }
4245 	    }
4246 	  free (shortname);
4247 	}
4248       free (nondeflt_vers);
4249       nondeflt_vers = NULL;
4250     }
4251 
4252   /* Now set the weakdefs field correctly for all the weak defined
4253      symbols we found.  The only way to do this is to search all the
4254      symbols.  Since we only need the information for non functions in
4255      dynamic objects, that's the only time we actually put anything on
4256      the list WEAKS.  We need this information so that if a regular
4257      object refers to a symbol defined weakly in a dynamic object, the
4258      real symbol in the dynamic object is also put in the dynamic
4259      symbols; we also must arrange for both symbols to point to the
4260      same memory location.  We could handle the general case of symbol
4261      aliasing, but a general symbol alias can only be generated in
4262      assembler code, handling it correctly would be very time
4263      consuming, and other ELF linkers don't handle general aliasing
4264      either.  */
4265   if (weaks != NULL)
4266     {
4267       struct elf_link_hash_entry **hpp;
4268       struct elf_link_hash_entry **hppend;
4269       struct elf_link_hash_entry **sorted_sym_hash;
4270       struct elf_link_hash_entry *h;
4271       size_t sym_count;
4272 
4273       /* Since we have to search the whole symbol list for each weak
4274 	 defined symbol, search time for N weak defined symbols will be
4275 	 O(N^2). Binary search will cut it down to O(NlogN).  */
4276       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4277       sorted_sym_hash = bfd_malloc (amt);
4278       if (sorted_sym_hash == NULL)
4279 	goto error_return;
4280       sym_hash = sorted_sym_hash;
4281       hpp = elf_sym_hashes (abfd);
4282       hppend = hpp + extsymcount;
4283       sym_count = 0;
4284       for (; hpp < hppend; hpp++)
4285 	{
4286 	  h = *hpp;
4287 	  if (h != NULL
4288 	      && h->root.type == bfd_link_hash_defined
4289 	      && h->type != STT_FUNC)
4290 	    {
4291 	      *sym_hash = h;
4292 	      sym_hash++;
4293 	      sym_count++;
4294 	    }
4295 	}
4296 
4297       qsort (sorted_sym_hash, sym_count,
4298 	     sizeof (struct elf_link_hash_entry *),
4299 	     elf_sort_symbol);
4300 
4301       while (weaks != NULL)
4302 	{
4303 	  struct elf_link_hash_entry *hlook;
4304 	  asection *slook;
4305 	  bfd_vma vlook;
4306 	  long ilook;
4307 	  size_t i, j, idx;
4308 
4309 	  hlook = weaks;
4310 	  weaks = hlook->u.weakdef;
4311 	  hlook->u.weakdef = NULL;
4312 
4313 	  BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4314 		      || hlook->root.type == bfd_link_hash_defweak
4315 		      || hlook->root.type == bfd_link_hash_common
4316 		      || hlook->root.type == bfd_link_hash_indirect);
4317 	  slook = hlook->root.u.def.section;
4318 	  vlook = hlook->root.u.def.value;
4319 
4320 	  ilook = -1;
4321 	  i = 0;
4322 	  j = sym_count;
4323 	  while (i < j)
4324 	    {
4325 	      bfd_signed_vma vdiff;
4326 	      idx = (i + j) / 2;
4327 	      h = sorted_sym_hash [idx];
4328 	      vdiff = vlook - h->root.u.def.value;
4329 	      if (vdiff < 0)
4330 		j = idx;
4331 	      else if (vdiff > 0)
4332 		i = idx + 1;
4333 	      else
4334 		{
4335 		  long sdiff = slook->id - h->root.u.def.section->id;
4336 		  if (sdiff < 0)
4337 		    j = idx;
4338 		  else if (sdiff > 0)
4339 		    i = idx + 1;
4340 		  else
4341 		    {
4342 		      ilook = idx;
4343 		      break;
4344 		    }
4345 		}
4346 	    }
4347 
4348 	  /* We didn't find a value/section match.  */
4349 	  if (ilook == -1)
4350 	    continue;
4351 
4352 	  for (i = ilook; i < sym_count; i++)
4353 	    {
4354 	      h = sorted_sym_hash [i];
4355 
4356 	      /* Stop if value or section doesn't match.  */
4357 	      if (h->root.u.def.value != vlook
4358 		  || h->root.u.def.section != slook)
4359 		break;
4360 	      else if (h != hlook)
4361 		{
4362 		  hlook->u.weakdef = h;
4363 
4364 		  /* If the weak definition is in the list of dynamic
4365 		     symbols, make sure the real definition is put
4366 		     there as well.  */
4367 		  if (hlook->dynindx != -1 && h->dynindx == -1)
4368 		    {
4369 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4370 			goto error_return;
4371 		    }
4372 
4373 		  /* If the real definition is in the list of dynamic
4374 		     symbols, make sure the weak definition is put
4375 		     there as well.  If we don't do this, then the
4376 		     dynamic loader might not merge the entries for the
4377 		     real definition and the weak definition.  */
4378 		  if (h->dynindx != -1 && hlook->dynindx == -1)
4379 		    {
4380 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4381 			goto error_return;
4382 		    }
4383 		  break;
4384 		}
4385 	    }
4386 	}
4387 
4388       free (sorted_sym_hash);
4389     }
4390 
4391   if (bed->check_directives)
4392     (*bed->check_directives) (abfd, info);
4393 
4394   /* If this object is the same format as the output object, and it is
4395      not a shared library, then let the backend look through the
4396      relocs.
4397 
4398      This is required to build global offset table entries and to
4399      arrange for dynamic relocs.  It is not required for the
4400      particular common case of linking non PIC code, even when linking
4401      against shared libraries, but unfortunately there is no way of
4402      knowing whether an object file has been compiled PIC or not.
4403      Looking through the relocs is not particularly time consuming.
4404      The problem is that we must either (1) keep the relocs in memory,
4405      which causes the linker to require additional runtime memory or
4406      (2) read the relocs twice from the input file, which wastes time.
4407      This would be a good case for using mmap.
4408 
4409      I have no idea how to handle linking PIC code into a file of a
4410      different format.  It probably can't be done.  */
4411   if (! dynamic
4412       && is_elf_hash_table (htab)
4413       && htab->root.creator == abfd->xvec
4414       && bed->check_relocs != NULL)
4415     {
4416       asection *o;
4417 
4418       for (o = abfd->sections; o != NULL; o = o->next)
4419 	{
4420 	  Elf_Internal_Rela *internal_relocs;
4421 	  bfd_boolean ok;
4422 
4423 	  if ((o->flags & SEC_RELOC) == 0
4424 	      || o->reloc_count == 0
4425 	      || ((info->strip == strip_all || info->strip == strip_debugger)
4426 		  && (o->flags & SEC_DEBUGGING) != 0)
4427 	      || bfd_is_abs_section (o->output_section))
4428 	    continue;
4429 
4430 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4431 						       info->keep_memory);
4432 	  if (internal_relocs == NULL)
4433 	    goto error_return;
4434 
4435 	  ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4436 
4437 	  if (elf_section_data (o)->relocs != internal_relocs)
4438 	    free (internal_relocs);
4439 
4440 	  if (! ok)
4441 	    goto error_return;
4442 	}
4443     }
4444 
4445   /* If this is a non-traditional link, try to optimize the handling
4446      of the .stab/.stabstr sections.  */
4447   if (! dynamic
4448       && ! info->traditional_format
4449       && is_elf_hash_table (htab)
4450       && (info->strip != strip_all && info->strip != strip_debugger))
4451     {
4452       asection *stabstr;
4453 
4454       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4455       if (stabstr != NULL)
4456 	{
4457 	  bfd_size_type string_offset = 0;
4458 	  asection *stab;
4459 
4460 	  for (stab = abfd->sections; stab; stab = stab->next)
4461 	    if (strncmp (".stab", stab->name, 5) == 0
4462 		&& (!stab->name[5] ||
4463 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4464 		&& (stab->flags & SEC_MERGE) == 0
4465 		&& !bfd_is_abs_section (stab->output_section))
4466 	      {
4467 		struct bfd_elf_section_data *secdata;
4468 
4469 		secdata = elf_section_data (stab);
4470 		if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4471 					       stabstr, &secdata->sec_info,
4472 					       &string_offset))
4473 		  goto error_return;
4474 		if (secdata->sec_info)
4475 		  stab->sec_info_type = ELF_INFO_TYPE_STABS;
4476 	    }
4477 	}
4478     }
4479 
4480   if (is_elf_hash_table (htab) && add_needed)
4481     {
4482       /* Add this bfd to the loaded list.  */
4483       struct elf_link_loaded_list *n;
4484 
4485       n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4486       if (n == NULL)
4487 	goto error_return;
4488       n->abfd = abfd;
4489       n->next = htab->loaded;
4490       htab->loaded = n;
4491     }
4492 
4493   return TRUE;
4494 
4495  error_free_vers:
4496   if (old_tab != NULL)
4497     free (old_tab);
4498   if (nondeflt_vers != NULL)
4499     free (nondeflt_vers);
4500   if (extversym != NULL)
4501     free (extversym);
4502  error_free_sym:
4503   if (isymbuf != NULL)
4504     free (isymbuf);
4505  error_return:
4506   return FALSE;
4507 }
4508 
4509 /* Return the linker hash table entry of a symbol that might be
4510    satisfied by an archive symbol.  Return -1 on error.  */
4511 
4512 struct elf_link_hash_entry *
4513 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4514 				struct bfd_link_info *info,
4515 				const char *name)
4516 {
4517   struct elf_link_hash_entry *h;
4518   char *p, *copy;
4519   size_t len, first;
4520 
4521   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4522   if (h != NULL)
4523     return h;
4524 
4525   /* If this is a default version (the name contains @@), look up the
4526      symbol again with only one `@' as well as without the version.
4527      The effect is that references to the symbol with and without the
4528      version will be matched by the default symbol in the archive.  */
4529 
4530   p = strchr (name, ELF_VER_CHR);
4531   if (p == NULL || p[1] != ELF_VER_CHR)
4532     return h;
4533 
4534   /* First check with only one `@'.  */
4535   len = strlen (name);
4536   copy = bfd_alloc (abfd, len);
4537   if (copy == NULL)
4538     return (struct elf_link_hash_entry *) -1;
4539 
4540   first = p - name + 1;
4541   memcpy (copy, name, first);
4542   memcpy (copy + first, name + first + 1, len - first);
4543 
4544   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4545   if (h == NULL)
4546     {
4547       /* We also need to check references to the symbol without the
4548 	 version.  */
4549       copy[first - 1] = '\0';
4550       h = elf_link_hash_lookup (elf_hash_table (info), copy,
4551 				FALSE, FALSE, FALSE);
4552     }
4553 
4554   bfd_release (abfd, copy);
4555   return h;
4556 }
4557 
4558 /* Add symbols from an ELF archive file to the linker hash table.  We
4559    don't use _bfd_generic_link_add_archive_symbols because of a
4560    problem which arises on UnixWare.  The UnixWare libc.so is an
4561    archive which includes an entry libc.so.1 which defines a bunch of
4562    symbols.  The libc.so archive also includes a number of other
4563    object files, which also define symbols, some of which are the same
4564    as those defined in libc.so.1.  Correct linking requires that we
4565    consider each object file in turn, and include it if it defines any
4566    symbols we need.  _bfd_generic_link_add_archive_symbols does not do
4567    this; it looks through the list of undefined symbols, and includes
4568    any object file which defines them.  When this algorithm is used on
4569    UnixWare, it winds up pulling in libc.so.1 early and defining a
4570    bunch of symbols.  This means that some of the other objects in the
4571    archive are not included in the link, which is incorrect since they
4572    precede libc.so.1 in the archive.
4573 
4574    Fortunately, ELF archive handling is simpler than that done by
4575    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4576    oddities.  In ELF, if we find a symbol in the archive map, and the
4577    symbol is currently undefined, we know that we must pull in that
4578    object file.
4579 
4580    Unfortunately, we do have to make multiple passes over the symbol
4581    table until nothing further is resolved.  */
4582 
4583 static bfd_boolean
4584 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4585 {
4586   symindex c;
4587   bfd_boolean *defined = NULL;
4588   bfd_boolean *included = NULL;
4589   carsym *symdefs;
4590   bfd_boolean loop;
4591   bfd_size_type amt;
4592   const struct elf_backend_data *bed;
4593   struct elf_link_hash_entry * (*archive_symbol_lookup)
4594     (bfd *, struct bfd_link_info *, const char *);
4595 
4596   if (! bfd_has_map (abfd))
4597     {
4598       /* An empty archive is a special case.  */
4599       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4600 	return TRUE;
4601       bfd_set_error (bfd_error_no_armap);
4602       return FALSE;
4603     }
4604 
4605   /* Keep track of all symbols we know to be already defined, and all
4606      files we know to be already included.  This is to speed up the
4607      second and subsequent passes.  */
4608   c = bfd_ardata (abfd)->symdef_count;
4609   if (c == 0)
4610     return TRUE;
4611   amt = c;
4612   amt *= sizeof (bfd_boolean);
4613   defined = bfd_zmalloc (amt);
4614   included = bfd_zmalloc (amt);
4615   if (defined == NULL || included == NULL)
4616     goto error_return;
4617 
4618   symdefs = bfd_ardata (abfd)->symdefs;
4619   bed = get_elf_backend_data (abfd);
4620   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4621 
4622   do
4623     {
4624       file_ptr last;
4625       symindex i;
4626       carsym *symdef;
4627       carsym *symdefend;
4628 
4629       loop = FALSE;
4630       last = -1;
4631 
4632       symdef = symdefs;
4633       symdefend = symdef + c;
4634       for (i = 0; symdef < symdefend; symdef++, i++)
4635 	{
4636 	  struct elf_link_hash_entry *h;
4637 	  bfd *element;
4638 	  struct bfd_link_hash_entry *undefs_tail;
4639 	  symindex mark;
4640 
4641 	  if (defined[i] || included[i])
4642 	    continue;
4643 	  if (symdef->file_offset == last)
4644 	    {
4645 	      included[i] = TRUE;
4646 	      continue;
4647 	    }
4648 
4649 	  h = archive_symbol_lookup (abfd, info, symdef->name);
4650 	  if (h == (struct elf_link_hash_entry *) -1)
4651 	    goto error_return;
4652 
4653 	  if (h == NULL)
4654 	    continue;
4655 
4656 	  if (h->root.type == bfd_link_hash_common)
4657 	    {
4658 	      /* We currently have a common symbol.  The archive map contains
4659 		 a reference to this symbol, so we may want to include it.  We
4660 		 only want to include it however, if this archive element
4661 		 contains a definition of the symbol, not just another common
4662 		 declaration of it.
4663 
4664 		 Unfortunately some archivers (including GNU ar) will put
4665 		 declarations of common symbols into their archive maps, as
4666 		 well as real definitions, so we cannot just go by the archive
4667 		 map alone.  Instead we must read in the element's symbol
4668 		 table and check that to see what kind of symbol definition
4669 		 this is.  */
4670 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4671 		continue;
4672 	    }
4673 	  else if (h->root.type != bfd_link_hash_undefined)
4674 	    {
4675 	      if (h->root.type != bfd_link_hash_undefweak)
4676 		defined[i] = TRUE;
4677 	      continue;
4678 	    }
4679 
4680 	  /* We need to include this archive member.  */
4681 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4682 	  if (element == NULL)
4683 	    goto error_return;
4684 
4685 	  if (! bfd_check_format (element, bfd_object))
4686 	    goto error_return;
4687 
4688 	  /* Doublecheck that we have not included this object
4689 	     already--it should be impossible, but there may be
4690 	     something wrong with the archive.  */
4691 	  if (element->archive_pass != 0)
4692 	    {
4693 	      bfd_set_error (bfd_error_bad_value);
4694 	      goto error_return;
4695 	    }
4696 	  element->archive_pass = 1;
4697 
4698 	  undefs_tail = info->hash->undefs_tail;
4699 
4700 	  if (! (*info->callbacks->add_archive_element) (info, element,
4701 							 symdef->name))
4702 	    goto error_return;
4703 	  if (! bfd_link_add_symbols (element, info))
4704 	    goto error_return;
4705 
4706 	  /* If there are any new undefined symbols, we need to make
4707 	     another pass through the archive in order to see whether
4708 	     they can be defined.  FIXME: This isn't perfect, because
4709 	     common symbols wind up on undefs_tail and because an
4710 	     undefined symbol which is defined later on in this pass
4711 	     does not require another pass.  This isn't a bug, but it
4712 	     does make the code less efficient than it could be.  */
4713 	  if (undefs_tail != info->hash->undefs_tail)
4714 	    loop = TRUE;
4715 
4716 	  /* Look backward to mark all symbols from this object file
4717 	     which we have already seen in this pass.  */
4718 	  mark = i;
4719 	  do
4720 	    {
4721 	      included[mark] = TRUE;
4722 	      if (mark == 0)
4723 		break;
4724 	      --mark;
4725 	    }
4726 	  while (symdefs[mark].file_offset == symdef->file_offset);
4727 
4728 	  /* We mark subsequent symbols from this object file as we go
4729 	     on through the loop.  */
4730 	  last = symdef->file_offset;
4731 	}
4732     }
4733   while (loop);
4734 
4735   free (defined);
4736   free (included);
4737 
4738   return TRUE;
4739 
4740  error_return:
4741   if (defined != NULL)
4742     free (defined);
4743   if (included != NULL)
4744     free (included);
4745   return FALSE;
4746 }
4747 
4748 /* Given an ELF BFD, add symbols to the global hash table as
4749    appropriate.  */
4750 
4751 bfd_boolean
4752 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4753 {
4754   switch (bfd_get_format (abfd))
4755     {
4756     case bfd_object:
4757       return elf_link_add_object_symbols (abfd, info);
4758     case bfd_archive:
4759       return elf_link_add_archive_symbols (abfd, info);
4760     default:
4761       bfd_set_error (bfd_error_wrong_format);
4762       return FALSE;
4763     }
4764 }
4765 
4766 /* This function will be called though elf_link_hash_traverse to store
4767    all hash value of the exported symbols in an array.  */
4768 
4769 static bfd_boolean
4770 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4771 {
4772   unsigned long **valuep = data;
4773   const char *name;
4774   char *p;
4775   unsigned long ha;
4776   char *alc = NULL;
4777 
4778   if (h->root.type == bfd_link_hash_warning)
4779     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4780 
4781   /* Ignore indirect symbols.  These are added by the versioning code.  */
4782   if (h->dynindx == -1)
4783     return TRUE;
4784 
4785   name = h->root.root.string;
4786   p = strchr (name, ELF_VER_CHR);
4787   if (p != NULL)
4788     {
4789       alc = bfd_malloc (p - name + 1);
4790       memcpy (alc, name, p - name);
4791       alc[p - name] = '\0';
4792       name = alc;
4793     }
4794 
4795   /* Compute the hash value.  */
4796   ha = bfd_elf_hash (name);
4797 
4798   /* Store the found hash value in the array given as the argument.  */
4799   *(*valuep)++ = ha;
4800 
4801   /* And store it in the struct so that we can put it in the hash table
4802      later.  */
4803   h->u.elf_hash_value = ha;
4804 
4805   if (alc != NULL)
4806     free (alc);
4807 
4808   return TRUE;
4809 }
4810 
4811 struct collect_gnu_hash_codes
4812 {
4813   bfd *output_bfd;
4814   const struct elf_backend_data *bed;
4815   unsigned long int nsyms;
4816   unsigned long int maskbits;
4817   unsigned long int *hashcodes;
4818   unsigned long int *hashval;
4819   unsigned long int *indx;
4820   unsigned long int *counts;
4821   bfd_vma *bitmask;
4822   bfd_byte *contents;
4823   long int min_dynindx;
4824   unsigned long int bucketcount;
4825   unsigned long int symindx;
4826   long int local_indx;
4827   long int shift1, shift2;
4828   unsigned long int mask;
4829 };
4830 
4831 /* This function will be called though elf_link_hash_traverse to store
4832    all hash value of the exported symbols in an array.  */
4833 
4834 static bfd_boolean
4835 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
4836 {
4837   struct collect_gnu_hash_codes *s = data;
4838   const char *name;
4839   char *p;
4840   unsigned long ha;
4841   char *alc = NULL;
4842 
4843   if (h->root.type == bfd_link_hash_warning)
4844     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4845 
4846   /* Ignore indirect symbols.  These are added by the versioning code.  */
4847   if (h->dynindx == -1)
4848     return TRUE;
4849 
4850   /* Ignore also local symbols and undefined symbols.  */
4851   if (! (*s->bed->elf_hash_symbol) (h))
4852     return TRUE;
4853 
4854   name = h->root.root.string;
4855   p = strchr (name, ELF_VER_CHR);
4856   if (p != NULL)
4857     {
4858       alc = bfd_malloc (p - name + 1);
4859       memcpy (alc, name, p - name);
4860       alc[p - name] = '\0';
4861       name = alc;
4862     }
4863 
4864   /* Compute the hash value.  */
4865   ha = bfd_elf_gnu_hash (name);
4866 
4867   /* Store the found hash value in the array for compute_bucket_count,
4868      and also for .dynsym reordering purposes.  */
4869   s->hashcodes[s->nsyms] = ha;
4870   s->hashval[h->dynindx] = ha;
4871   ++s->nsyms;
4872   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
4873     s->min_dynindx = h->dynindx;
4874 
4875   if (alc != NULL)
4876     free (alc);
4877 
4878   return TRUE;
4879 }
4880 
4881 /* This function will be called though elf_link_hash_traverse to do
4882    final dynaminc symbol renumbering.  */
4883 
4884 static bfd_boolean
4885 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
4886 {
4887   struct collect_gnu_hash_codes *s = data;
4888   unsigned long int bucket;
4889   unsigned long int val;
4890 
4891   if (h->root.type == bfd_link_hash_warning)
4892     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4893 
4894   /* Ignore indirect symbols.  */
4895   if (h->dynindx == -1)
4896     return TRUE;
4897 
4898   /* Ignore also local symbols and undefined symbols.  */
4899   if (! (*s->bed->elf_hash_symbol) (h))
4900     {
4901       if (h->dynindx >= s->min_dynindx)
4902 	h->dynindx = s->local_indx++;
4903       return TRUE;
4904     }
4905 
4906   bucket = s->hashval[h->dynindx] % s->bucketcount;
4907   val = (s->hashval[h->dynindx] >> s->shift1)
4908 	& ((s->maskbits >> s->shift1) - 1);
4909   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
4910   s->bitmask[val]
4911     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
4912   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
4913   if (s->counts[bucket] == 1)
4914     /* Last element terminates the chain.  */
4915     val |= 1;
4916   bfd_put_32 (s->output_bfd, val,
4917 	      s->contents + (s->indx[bucket] - s->symindx) * 4);
4918   --s->counts[bucket];
4919   h->dynindx = s->indx[bucket]++;
4920   return TRUE;
4921 }
4922 
4923 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
4924 
4925 bfd_boolean
4926 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
4927 {
4928   return !(h->forced_local
4929 	   || h->root.type == bfd_link_hash_undefined
4930 	   || h->root.type == bfd_link_hash_undefweak
4931 	   || ((h->root.type == bfd_link_hash_defined
4932 		|| h->root.type == bfd_link_hash_defweak)
4933 	       && h->root.u.def.section->output_section == NULL));
4934 }
4935 
4936 /* Array used to determine the number of hash table buckets to use
4937    based on the number of symbols there are.  If there are fewer than
4938    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4939    fewer than 37 we use 17 buckets, and so forth.  We never use more
4940    than 32771 buckets.  */
4941 
4942 static const size_t elf_buckets[] =
4943 {
4944   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4945   16411, 32771, 0
4946 };
4947 
4948 /* Compute bucket count for hashing table.  We do not use a static set
4949    of possible tables sizes anymore.  Instead we determine for all
4950    possible reasonable sizes of the table the outcome (i.e., the
4951    number of collisions etc) and choose the best solution.  The
4952    weighting functions are not too simple to allow the table to grow
4953    without bounds.  Instead one of the weighting factors is the size.
4954    Therefore the result is always a good payoff between few collisions
4955    (= short chain lengths) and table size.  */
4956 static size_t
4957 compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes,
4958 		      unsigned long int nsyms, int gnu_hash)
4959 {
4960   size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4961   size_t best_size = 0;
4962   unsigned long int i;
4963   bfd_size_type amt;
4964 
4965   /* We have a problem here.  The following code to optimize the table
4966      size requires an integer type with more the 32 bits.  If
4967      BFD_HOST_U_64_BIT is set we know about such a type.  */
4968 #ifdef BFD_HOST_U_64_BIT
4969   if (info->optimize)
4970     {
4971       size_t minsize;
4972       size_t maxsize;
4973       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4974       bfd *dynobj = elf_hash_table (info)->dynobj;
4975       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4976       unsigned long int *counts;
4977 
4978       /* Possible optimization parameters: if we have NSYMS symbols we say
4979 	 that the hashing table must at least have NSYMS/4 and at most
4980 	 2*NSYMS buckets.  */
4981       minsize = nsyms / 4;
4982       if (minsize == 0)
4983 	minsize = 1;
4984       best_size = maxsize = nsyms * 2;
4985       if (gnu_hash)
4986 	{
4987 	  if (minsize < 2)
4988 	    minsize = 2;
4989 	  if ((best_size & 31) == 0)
4990 	    ++best_size;
4991 	}
4992 
4993       /* Create array where we count the collisions in.  We must use bfd_malloc
4994 	 since the size could be large.  */
4995       amt = maxsize;
4996       amt *= sizeof (unsigned long int);
4997       counts = bfd_malloc (amt);
4998       if (counts == NULL)
4999 	return 0;
5000 
5001       /* Compute the "optimal" size for the hash table.  The criteria is a
5002 	 minimal chain length.  The minor criteria is (of course) the size
5003 	 of the table.  */
5004       for (i = minsize; i < maxsize; ++i)
5005 	{
5006 	  /* Walk through the array of hashcodes and count the collisions.  */
5007 	  BFD_HOST_U_64_BIT max;
5008 	  unsigned long int j;
5009 	  unsigned long int fact;
5010 
5011 	  if (gnu_hash && (i & 31) == 0)
5012 	    continue;
5013 
5014 	  memset (counts, '\0', i * sizeof (unsigned long int));
5015 
5016 	  /* Determine how often each hash bucket is used.  */
5017 	  for (j = 0; j < nsyms; ++j)
5018 	    ++counts[hashcodes[j] % i];
5019 
5020 	  /* For the weight function we need some information about the
5021 	     pagesize on the target.  This is information need not be 100%
5022 	     accurate.  Since this information is not available (so far) we
5023 	     define it here to a reasonable default value.  If it is crucial
5024 	     to have a better value some day simply define this value.  */
5025 # ifndef BFD_TARGET_PAGESIZE
5026 #  define BFD_TARGET_PAGESIZE	(4096)
5027 # endif
5028 
5029 	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5030 	     and the chains.  */
5031 	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5032 
5033 # if 1
5034 	  /* Variant 1: optimize for short chains.  We add the squares
5035 	     of all the chain lengths (which favors many small chain
5036 	     over a few long chains).  */
5037 	  for (j = 0; j < i; ++j)
5038 	    max += counts[j] * counts[j];
5039 
5040 	  /* This adds penalties for the overall size of the table.  */
5041 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5042 	  max *= fact * fact;
5043 # else
5044 	  /* Variant 2: Optimize a lot more for small table.  Here we
5045 	     also add squares of the size but we also add penalties for
5046 	     empty slots (the +1 term).  */
5047 	  for (j = 0; j < i; ++j)
5048 	    max += (1 + counts[j]) * (1 + counts[j]);
5049 
5050 	  /* The overall size of the table is considered, but not as
5051 	     strong as in variant 1, where it is squared.  */
5052 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5053 	  max *= fact;
5054 # endif
5055 
5056 	  /* Compare with current best results.  */
5057 	  if (max < best_chlen)
5058 	    {
5059 	      best_chlen = max;
5060 	      best_size = i;
5061 	    }
5062 	}
5063 
5064       free (counts);
5065     }
5066   else
5067 #endif /* defined (BFD_HOST_U_64_BIT) */
5068     {
5069       /* This is the fallback solution if no 64bit type is available or if we
5070 	 are not supposed to spend much time on optimizations.  We select the
5071 	 bucket count using a fixed set of numbers.  */
5072       for (i = 0; elf_buckets[i] != 0; i++)
5073 	{
5074 	  best_size = elf_buckets[i];
5075 	  if (nsyms < elf_buckets[i + 1])
5076 	    break;
5077 	}
5078       if (gnu_hash && best_size < 2)
5079 	best_size = 2;
5080     }
5081 
5082   return best_size;
5083 }
5084 
5085 /* Set up the sizes and contents of the ELF dynamic sections.  This is
5086    called by the ELF linker emulation before_allocation routine.  We
5087    must set the sizes of the sections before the linker sets the
5088    addresses of the various sections.  */
5089 
5090 bfd_boolean
5091 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5092 			       const char *soname,
5093 			       const char *rpath,
5094 			       const char *filter_shlib,
5095 			       const char * const *auxiliary_filters,
5096 			       struct bfd_link_info *info,
5097 			       asection **sinterpptr,
5098 			       struct bfd_elf_version_tree *verdefs)
5099 {
5100   bfd_size_type soname_indx;
5101   bfd *dynobj;
5102   const struct elf_backend_data *bed;
5103   struct elf_assign_sym_version_info asvinfo;
5104 
5105   *sinterpptr = NULL;
5106 
5107   soname_indx = (bfd_size_type) -1;
5108 
5109   if (!is_elf_hash_table (info->hash))
5110     return TRUE;
5111 
5112   elf_tdata (output_bfd)->relro = info->relro;
5113   elf_tdata (output_bfd)->wxneeded = info->wxneeded;
5114   elf_tdata (output_bfd)->executable = info->executable;
5115   if (info->execstack)
5116     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5117   else if (info->noexecstack)
5118     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5119   else
5120     {
5121       bfd *inputobj;
5122       asection *notesec = NULL;
5123       int exec = 0;
5124 
5125       for (inputobj = info->input_bfds;
5126 	   inputobj;
5127 	   inputobj = inputobj->link_next)
5128 	{
5129 	  asection *s;
5130 
5131 	  if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
5132 	    continue;
5133 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5134 	  if (s)
5135 	    {
5136 	      if (s->flags & SEC_CODE)
5137 		exec = PF_X;
5138 	      notesec = s;
5139 	    }
5140 	  else
5141 	    exec = PF_X;
5142 	}
5143       if (notesec)
5144 	{
5145 	  elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5146 	  if (exec && info->relocatable
5147 	      && notesec->output_section != bfd_abs_section_ptr)
5148 	    notesec->output_section->flags |= SEC_CODE;
5149 	}
5150     }
5151 
5152   /* Any syms created from now on start with -1 in
5153      got.refcount/offset and plt.refcount/offset.  */
5154   elf_hash_table (info)->init_got_refcount
5155     = elf_hash_table (info)->init_got_offset;
5156   elf_hash_table (info)->init_plt_refcount
5157     = elf_hash_table (info)->init_plt_offset;
5158 
5159   /* The backend may have to create some sections regardless of whether
5160      we're dynamic or not.  */
5161   bed = get_elf_backend_data (output_bfd);
5162   if (bed->elf_backend_always_size_sections
5163       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5164     return FALSE;
5165 
5166   dynobj = elf_hash_table (info)->dynobj;
5167 
5168   /* If there were no dynamic objects in the link, there is nothing to
5169      do here.  */
5170   if (dynobj == NULL)
5171     return TRUE;
5172 
5173   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5174     return FALSE;
5175 
5176   if (elf_hash_table (info)->dynamic_sections_created)
5177     {
5178       struct elf_info_failed eif;
5179       struct elf_link_hash_entry *h;
5180       asection *dynstr;
5181       struct bfd_elf_version_tree *t;
5182       struct bfd_elf_version_expr *d;
5183       asection *s;
5184       bfd_boolean all_defined;
5185 
5186       *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5187 
5188       if (soname != NULL)
5189 	{
5190 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5191 					     soname, TRUE);
5192 	  if (soname_indx == (bfd_size_type) -1
5193 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5194 	    return FALSE;
5195 	}
5196 
5197       if (info->symbolic)
5198 	{
5199 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5200 	    return FALSE;
5201 	  info->flags |= DF_SYMBOLIC;
5202 	}
5203 
5204       if (rpath != NULL)
5205 	{
5206 	  bfd_size_type indx;
5207 
5208 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5209 				      TRUE);
5210 	  if (indx == (bfd_size_type) -1
5211 	      || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5212 	    return FALSE;
5213 
5214 	  if  (info->new_dtags)
5215 	    {
5216 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5217 	      if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5218 		return FALSE;
5219 	    }
5220 	}
5221 
5222       if (filter_shlib != NULL)
5223 	{
5224 	  bfd_size_type indx;
5225 
5226 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5227 				      filter_shlib, TRUE);
5228 	  if (indx == (bfd_size_type) -1
5229 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5230 	    return FALSE;
5231 	}
5232 
5233       if (auxiliary_filters != NULL)
5234 	{
5235 	  const char * const *p;
5236 
5237 	  for (p = auxiliary_filters; *p != NULL; p++)
5238 	    {
5239 	      bfd_size_type indx;
5240 
5241 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5242 					  *p, TRUE);
5243 	      if (indx == (bfd_size_type) -1
5244 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5245 		return FALSE;
5246 	    }
5247 	}
5248 
5249       eif.info = info;
5250       eif.verdefs = verdefs;
5251       eif.failed = FALSE;
5252 
5253       /* If we are supposed to export all symbols into the dynamic symbol
5254 	 table (this is not the normal case), then do so.  */
5255       if (info->export_dynamic)
5256 	{
5257 	  elf_link_hash_traverse (elf_hash_table (info),
5258 				  _bfd_elf_export_symbol,
5259 				  &eif);
5260 	  if (eif.failed)
5261 	    return FALSE;
5262 	}
5263 
5264       /* Make all global versions with definition.  */
5265       for (t = verdefs; t != NULL; t = t->next)
5266 	for (d = t->globals.list; d != NULL; d = d->next)
5267 	  if (!d->symver && d->symbol)
5268 	    {
5269 	      const char *verstr, *name;
5270 	      size_t namelen, verlen, newlen;
5271 	      char *newname, *p;
5272 	      struct elf_link_hash_entry *newh;
5273 
5274 	      name = d->symbol;
5275 	      namelen = strlen (name);
5276 	      verstr = t->name;
5277 	      verlen = strlen (verstr);
5278 	      newlen = namelen + verlen + 3;
5279 
5280 	      newname = bfd_malloc (newlen);
5281 	      if (newname == NULL)
5282 		return FALSE;
5283 	      memcpy (newname, name, namelen);
5284 
5285 	      /* Check the hidden versioned definition.  */
5286 	      p = newname + namelen;
5287 	      *p++ = ELF_VER_CHR;
5288 	      memcpy (p, verstr, verlen + 1);
5289 	      newh = elf_link_hash_lookup (elf_hash_table (info),
5290 					   newname, FALSE, FALSE,
5291 					   FALSE);
5292 	      if (newh == NULL
5293 		  || (newh->root.type != bfd_link_hash_defined
5294 		      && newh->root.type != bfd_link_hash_defweak))
5295 		{
5296 		  /* Check the default versioned definition.  */
5297 		  *p++ = ELF_VER_CHR;
5298 		  memcpy (p, verstr, verlen + 1);
5299 		  newh = elf_link_hash_lookup (elf_hash_table (info),
5300 					       newname, FALSE, FALSE,
5301 					       FALSE);
5302 		}
5303 	      free (newname);
5304 
5305 	      /* Mark this version if there is a definition and it is
5306 		 not defined in a shared object.  */
5307 	      if (newh != NULL
5308 		  && !newh->def_dynamic
5309 		  && (newh->root.type == bfd_link_hash_defined
5310 		      || newh->root.type == bfd_link_hash_defweak))
5311 		d->symver = 1;
5312 	    }
5313 
5314       /* Attach all the symbols to their version information.  */
5315       asvinfo.output_bfd = output_bfd;
5316       asvinfo.info = info;
5317       asvinfo.verdefs = verdefs;
5318       asvinfo.failed = FALSE;
5319 
5320       elf_link_hash_traverse (elf_hash_table (info),
5321 			      _bfd_elf_link_assign_sym_version,
5322 			      &asvinfo);
5323       if (asvinfo.failed)
5324 	return FALSE;
5325 
5326       if (!info->allow_undefined_version)
5327 	{
5328 	  /* Check if all global versions have a definition.  */
5329 	  all_defined = TRUE;
5330 	  for (t = verdefs; t != NULL; t = t->next)
5331 	    for (d = t->globals.list; d != NULL; d = d->next)
5332 	      if (!d->symver && !d->script)
5333 		{
5334 		  (*_bfd_error_handler)
5335 		    (_("%s: undefined version: %s"),
5336 		     d->pattern, t->name);
5337 		  all_defined = FALSE;
5338 		}
5339 
5340 	  if (!all_defined)
5341 	    {
5342 	      bfd_set_error (bfd_error_bad_value);
5343 	      return FALSE;
5344 	    }
5345 	}
5346 
5347       /* Find all symbols which were defined in a dynamic object and make
5348 	 the backend pick a reasonable value for them.  */
5349       elf_link_hash_traverse (elf_hash_table (info),
5350 			      _bfd_elf_adjust_dynamic_symbol,
5351 			      &eif);
5352       if (eif.failed)
5353 	return FALSE;
5354 
5355       /* Add some entries to the .dynamic section.  We fill in some of the
5356 	 values later, in bfd_elf_final_link, but we must add the entries
5357 	 now so that we know the final size of the .dynamic section.  */
5358 
5359       /* If there are initialization and/or finalization functions to
5360 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
5361       h = (info->init_function
5362 	   ? elf_link_hash_lookup (elf_hash_table (info),
5363 				   info->init_function, FALSE,
5364 				   FALSE, FALSE)
5365 	   : NULL);
5366       if (h != NULL
5367 	  && (h->ref_regular
5368 	      || h->def_regular))
5369 	{
5370 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5371 	    return FALSE;
5372 	}
5373       h = (info->fini_function
5374 	   ? elf_link_hash_lookup (elf_hash_table (info),
5375 				   info->fini_function, FALSE,
5376 				   FALSE, FALSE)
5377 	   : NULL);
5378       if (h != NULL
5379 	  && (h->ref_regular
5380 	      || h->def_regular))
5381 	{
5382 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5383 	    return FALSE;
5384 	}
5385 
5386       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5387       if (s != NULL && s->linker_has_input)
5388 	{
5389 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
5390 	  if (! info->executable)
5391 	    {
5392 	      bfd *sub;
5393 	      asection *o;
5394 
5395 	      for (sub = info->input_bfds; sub != NULL;
5396 		   sub = sub->link_next)
5397 		for (o = sub->sections; o != NULL; o = o->next)
5398 		  if (elf_section_data (o)->this_hdr.sh_type
5399 		      == SHT_PREINIT_ARRAY)
5400 		    {
5401 		      (*_bfd_error_handler)
5402 			(_("%B: .preinit_array section is not allowed in DSO"),
5403 			 sub);
5404 		      break;
5405 		    }
5406 
5407 	      bfd_set_error (bfd_error_nonrepresentable_section);
5408 	      return FALSE;
5409 	    }
5410 
5411 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5412 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5413 	    return FALSE;
5414 	}
5415       s = bfd_get_section_by_name (output_bfd, ".init_array");
5416       if (s != NULL && s->linker_has_input)
5417 	{
5418 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5419 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5420 	    return FALSE;
5421 	}
5422       s = bfd_get_section_by_name (output_bfd, ".fini_array");
5423       if (s != NULL && s->linker_has_input)
5424 	{
5425 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5426 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5427 	    return FALSE;
5428 	}
5429 
5430       dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5431       /* If .dynstr is excluded from the link, we don't want any of
5432 	 these tags.  Strictly, we should be checking each section
5433 	 individually;  This quick check covers for the case where
5434 	 someone does a /DISCARD/ : { *(*) }.  */
5435       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5436 	{
5437 	  bfd_size_type strsize;
5438 
5439 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5440 	  if ((info->emit_hash
5441 	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5442 	      || (info->emit_gnu_hash
5443 		  && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5444 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5445 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5446 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5447 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5448 					      bed->s->sizeof_sym))
5449 	    return FALSE;
5450 	}
5451     }
5452 
5453   /* The backend must work out the sizes of all the other dynamic
5454      sections.  */
5455   if (bed->elf_backend_size_dynamic_sections
5456       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5457     return FALSE;
5458 
5459   if (elf_hash_table (info)->dynamic_sections_created)
5460     {
5461       unsigned long section_sym_count;
5462       asection *s;
5463 
5464       /* Set up the version definition section.  */
5465       s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5466       BFD_ASSERT (s != NULL);
5467 
5468       /* We may have created additional version definitions if we are
5469 	 just linking a regular application.  */
5470       verdefs = asvinfo.verdefs;
5471 
5472       /* Skip anonymous version tag.  */
5473       if (verdefs != NULL && verdefs->vernum == 0)
5474 	verdefs = verdefs->next;
5475 
5476       if (verdefs == NULL && !info->create_default_symver)
5477 	s->flags |= SEC_EXCLUDE;
5478       else
5479 	{
5480 	  unsigned int cdefs;
5481 	  bfd_size_type size;
5482 	  struct bfd_elf_version_tree *t;
5483 	  bfd_byte *p;
5484 	  Elf_Internal_Verdef def;
5485 	  Elf_Internal_Verdaux defaux;
5486 	  struct bfd_link_hash_entry *bh;
5487 	  struct elf_link_hash_entry *h;
5488 	  const char *name;
5489 
5490 	  cdefs = 0;
5491 	  size = 0;
5492 
5493 	  /* Make space for the base version.  */
5494 	  size += sizeof (Elf_External_Verdef);
5495 	  size += sizeof (Elf_External_Verdaux);
5496 	  ++cdefs;
5497 
5498 	  /* Make space for the default version.  */
5499 	  if (info->create_default_symver)
5500 	    {
5501 	      size += sizeof (Elf_External_Verdef);
5502 	      ++cdefs;
5503 	    }
5504 
5505 	  for (t = verdefs; t != NULL; t = t->next)
5506 	    {
5507 	      struct bfd_elf_version_deps *n;
5508 
5509 	      size += sizeof (Elf_External_Verdef);
5510 	      size += sizeof (Elf_External_Verdaux);
5511 	      ++cdefs;
5512 
5513 	      for (n = t->deps; n != NULL; n = n->next)
5514 		size += sizeof (Elf_External_Verdaux);
5515 	    }
5516 
5517 	  s->size = size;
5518 	  s->contents = bfd_alloc (output_bfd, s->size);
5519 	  if (s->contents == NULL && s->size != 0)
5520 	    return FALSE;
5521 
5522 	  /* Fill in the version definition section.  */
5523 
5524 	  p = s->contents;
5525 
5526 	  def.vd_version = VER_DEF_CURRENT;
5527 	  def.vd_flags = VER_FLG_BASE;
5528 	  def.vd_ndx = 1;
5529 	  def.vd_cnt = 1;
5530 	  if (info->create_default_symver)
5531 	    {
5532 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5533 	      def.vd_next = sizeof (Elf_External_Verdef);
5534 	    }
5535 	  else
5536 	    {
5537 	      def.vd_aux = sizeof (Elf_External_Verdef);
5538 	      def.vd_next = (sizeof (Elf_External_Verdef)
5539 			     + sizeof (Elf_External_Verdaux));
5540 	    }
5541 
5542 	  if (soname_indx != (bfd_size_type) -1)
5543 	    {
5544 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5545 				      soname_indx);
5546 	      def.vd_hash = bfd_elf_hash (soname);
5547 	      defaux.vda_name = soname_indx;
5548 	      name = soname;
5549 	    }
5550 	  else
5551 	    {
5552 	      bfd_size_type indx;
5553 
5554 	      name = lbasename (output_bfd->filename);
5555 	      def.vd_hash = bfd_elf_hash (name);
5556 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5557 					  name, FALSE);
5558 	      if (indx == (bfd_size_type) -1)
5559 		return FALSE;
5560 	      defaux.vda_name = indx;
5561 	    }
5562 	  defaux.vda_next = 0;
5563 
5564 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
5565 				    (Elf_External_Verdef *) p);
5566 	  p += sizeof (Elf_External_Verdef);
5567 	  if (info->create_default_symver)
5568 	    {
5569 	      /* Add a symbol representing this version.  */
5570 	      bh = NULL;
5571 	      if (! (_bfd_generic_link_add_one_symbol
5572 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5573 		      0, NULL, FALSE,
5574 		      get_elf_backend_data (dynobj)->collect, &bh)))
5575 		return FALSE;
5576 	      h = (struct elf_link_hash_entry *) bh;
5577 	      h->non_elf = 0;
5578 	      h->def_regular = 1;
5579 	      h->type = STT_OBJECT;
5580 	      h->verinfo.vertree = NULL;
5581 
5582 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
5583 		return FALSE;
5584 
5585 	      /* Create a duplicate of the base version with the same
5586 		 aux block, but different flags.  */
5587 	      def.vd_flags = 0;
5588 	      def.vd_ndx = 2;
5589 	      def.vd_aux = sizeof (Elf_External_Verdef);
5590 	      if (verdefs)
5591 		def.vd_next = (sizeof (Elf_External_Verdef)
5592 			       + sizeof (Elf_External_Verdaux));
5593 	      else
5594 		def.vd_next = 0;
5595 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
5596 					(Elf_External_Verdef *) p);
5597 	      p += sizeof (Elf_External_Verdef);
5598 	    }
5599 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5600 				     (Elf_External_Verdaux *) p);
5601 	  p += sizeof (Elf_External_Verdaux);
5602 
5603 	  for (t = verdefs; t != NULL; t = t->next)
5604 	    {
5605 	      unsigned int cdeps;
5606 	      struct bfd_elf_version_deps *n;
5607 
5608 	      cdeps = 0;
5609 	      for (n = t->deps; n != NULL; n = n->next)
5610 		++cdeps;
5611 
5612 	      /* Add a symbol representing this version.  */
5613 	      bh = NULL;
5614 	      if (! (_bfd_generic_link_add_one_symbol
5615 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5616 		      0, NULL, FALSE,
5617 		      get_elf_backend_data (dynobj)->collect, &bh)))
5618 		return FALSE;
5619 	      h = (struct elf_link_hash_entry *) bh;
5620 	      h->non_elf = 0;
5621 	      h->def_regular = 1;
5622 	      h->type = STT_OBJECT;
5623 	      h->verinfo.vertree = t;
5624 
5625 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
5626 		return FALSE;
5627 
5628 	      def.vd_version = VER_DEF_CURRENT;
5629 	      def.vd_flags = 0;
5630 	      if (t->globals.list == NULL
5631 		  && t->locals.list == NULL
5632 		  && ! t->used)
5633 		def.vd_flags |= VER_FLG_WEAK;
5634 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5635 	      def.vd_cnt = cdeps + 1;
5636 	      def.vd_hash = bfd_elf_hash (t->name);
5637 	      def.vd_aux = sizeof (Elf_External_Verdef);
5638 	      def.vd_next = 0;
5639 	      if (t->next != NULL)
5640 		def.vd_next = (sizeof (Elf_External_Verdef)
5641 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5642 
5643 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
5644 					(Elf_External_Verdef *) p);
5645 	      p += sizeof (Elf_External_Verdef);
5646 
5647 	      defaux.vda_name = h->dynstr_index;
5648 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5649 				      h->dynstr_index);
5650 	      defaux.vda_next = 0;
5651 	      if (t->deps != NULL)
5652 		defaux.vda_next = sizeof (Elf_External_Verdaux);
5653 	      t->name_indx = defaux.vda_name;
5654 
5655 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5656 					 (Elf_External_Verdaux *) p);
5657 	      p += sizeof (Elf_External_Verdaux);
5658 
5659 	      for (n = t->deps; n != NULL; n = n->next)
5660 		{
5661 		  if (n->version_needed == NULL)
5662 		    {
5663 		      /* This can happen if there was an error in the
5664 			 version script.  */
5665 		      defaux.vda_name = 0;
5666 		    }
5667 		  else
5668 		    {
5669 		      defaux.vda_name = n->version_needed->name_indx;
5670 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5671 					      defaux.vda_name);
5672 		    }
5673 		  if (n->next == NULL)
5674 		    defaux.vda_next = 0;
5675 		  else
5676 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
5677 
5678 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5679 					     (Elf_External_Verdaux *) p);
5680 		  p += sizeof (Elf_External_Verdaux);
5681 		}
5682 	    }
5683 
5684 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5685 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5686 	    return FALSE;
5687 
5688 	  elf_tdata (output_bfd)->cverdefs = cdefs;
5689 	}
5690 
5691       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5692 	{
5693 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5694 	    return FALSE;
5695 	}
5696       else if (info->flags & DF_BIND_NOW)
5697 	{
5698 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5699 	    return FALSE;
5700 	}
5701 
5702       if (info->flags_1)
5703 	{
5704 	  if (info->executable)
5705 	    info->flags_1 &= ~ (DF_1_INITFIRST
5706 				| DF_1_NODELETE
5707 				| DF_1_NOOPEN);
5708 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5709 	    return FALSE;
5710 	}
5711 
5712       /* Work out the size of the version reference section.  */
5713 
5714       s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5715       BFD_ASSERT (s != NULL);
5716       {
5717 	struct elf_find_verdep_info sinfo;
5718 
5719 	sinfo.output_bfd = output_bfd;
5720 	sinfo.info = info;
5721 	sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5722 	if (sinfo.vers == 0)
5723 	  sinfo.vers = 1;
5724 	sinfo.failed = FALSE;
5725 
5726 	elf_link_hash_traverse (elf_hash_table (info),
5727 				_bfd_elf_link_find_version_dependencies,
5728 				&sinfo);
5729 
5730 	if (elf_tdata (output_bfd)->verref == NULL)
5731 	  s->flags |= SEC_EXCLUDE;
5732 	else
5733 	  {
5734 	    Elf_Internal_Verneed *t;
5735 	    unsigned int size;
5736 	    unsigned int crefs;
5737 	    bfd_byte *p;
5738 
5739 	    /* Build the version definition section.  */
5740 	    size = 0;
5741 	    crefs = 0;
5742 	    for (t = elf_tdata (output_bfd)->verref;
5743 		 t != NULL;
5744 		 t = t->vn_nextref)
5745 	      {
5746 		Elf_Internal_Vernaux *a;
5747 
5748 		size += sizeof (Elf_External_Verneed);
5749 		++crefs;
5750 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5751 		  size += sizeof (Elf_External_Vernaux);
5752 	      }
5753 
5754 	    s->size = size;
5755 	    s->contents = bfd_alloc (output_bfd, s->size);
5756 	    if (s->contents == NULL)
5757 	      return FALSE;
5758 
5759 	    p = s->contents;
5760 	    for (t = elf_tdata (output_bfd)->verref;
5761 		 t != NULL;
5762 		 t = t->vn_nextref)
5763 	      {
5764 		unsigned int caux;
5765 		Elf_Internal_Vernaux *a;
5766 		bfd_size_type indx;
5767 
5768 		caux = 0;
5769 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5770 		  ++caux;
5771 
5772 		t->vn_version = VER_NEED_CURRENT;
5773 		t->vn_cnt = caux;
5774 		indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5775 					    elf_dt_name (t->vn_bfd) != NULL
5776 					    ? elf_dt_name (t->vn_bfd)
5777 					    : lbasename (t->vn_bfd->filename),
5778 					    FALSE);
5779 		if (indx == (bfd_size_type) -1)
5780 		  return FALSE;
5781 		t->vn_file = indx;
5782 		t->vn_aux = sizeof (Elf_External_Verneed);
5783 		if (t->vn_nextref == NULL)
5784 		  t->vn_next = 0;
5785 		else
5786 		  t->vn_next = (sizeof (Elf_External_Verneed)
5787 				+ caux * sizeof (Elf_External_Vernaux));
5788 
5789 		_bfd_elf_swap_verneed_out (output_bfd, t,
5790 					   (Elf_External_Verneed *) p);
5791 		p += sizeof (Elf_External_Verneed);
5792 
5793 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5794 		  {
5795 		    a->vna_hash = bfd_elf_hash (a->vna_nodename);
5796 		    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5797 						a->vna_nodename, FALSE);
5798 		    if (indx == (bfd_size_type) -1)
5799 		      return FALSE;
5800 		    a->vna_name = indx;
5801 		    if (a->vna_nextptr == NULL)
5802 		      a->vna_next = 0;
5803 		    else
5804 		      a->vna_next = sizeof (Elf_External_Vernaux);
5805 
5806 		    _bfd_elf_swap_vernaux_out (output_bfd, a,
5807 					       (Elf_External_Vernaux *) p);
5808 		    p += sizeof (Elf_External_Vernaux);
5809 		  }
5810 	      }
5811 
5812 	    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5813 		|| !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5814 	      return FALSE;
5815 
5816 	    elf_tdata (output_bfd)->cverrefs = crefs;
5817 	  }
5818       }
5819 
5820       if ((elf_tdata (output_bfd)->cverrefs == 0
5821 	   && elf_tdata (output_bfd)->cverdefs == 0)
5822 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5823 					     &section_sym_count) == 0)
5824 	{
5825 	  s = bfd_get_section_by_name (dynobj, ".gnu.version");
5826 	  s->flags |= SEC_EXCLUDE;
5827 	}
5828     }
5829   return TRUE;
5830 }
5831 
5832 bfd_boolean
5833 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5834 {
5835   if (!is_elf_hash_table (info->hash))
5836     return TRUE;
5837 
5838   if (elf_hash_table (info)->dynamic_sections_created)
5839     {
5840       bfd *dynobj;
5841       const struct elf_backend_data *bed;
5842       asection *s;
5843       bfd_size_type dynsymcount;
5844       unsigned long section_sym_count;
5845       unsigned int dtagcount;
5846 
5847       dynobj = elf_hash_table (info)->dynobj;
5848 
5849       /* Assign dynsym indicies.  In a shared library we generate a
5850 	 section symbol for each output section, which come first.
5851 	 Next come all of the back-end allocated local dynamic syms,
5852 	 followed by the rest of the global symbols.  */
5853 
5854       dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5855 						    &section_sym_count);
5856 
5857       /* Work out the size of the symbol version section.  */
5858       s = bfd_get_section_by_name (dynobj, ".gnu.version");
5859       BFD_ASSERT (s != NULL);
5860       if (dynsymcount != 0
5861 	  && (s->flags & SEC_EXCLUDE) == 0)
5862 	{
5863 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
5864 	  s->contents = bfd_zalloc (output_bfd, s->size);
5865 	  if (s->contents == NULL)
5866 	    return FALSE;
5867 
5868 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5869 	    return FALSE;
5870 	}
5871 
5872       /* Set the size of the .dynsym and .hash sections.  We counted
5873 	 the number of dynamic symbols in elf_link_add_object_symbols.
5874 	 We will build the contents of .dynsym and .hash when we build
5875 	 the final symbol table, because until then we do not know the
5876 	 correct value to give the symbols.  We built the .dynstr
5877 	 section as we went along in elf_link_add_object_symbols.  */
5878       s = bfd_get_section_by_name (dynobj, ".dynsym");
5879       BFD_ASSERT (s != NULL);
5880       bed = get_elf_backend_data (output_bfd);
5881       s->size = dynsymcount * bed->s->sizeof_sym;
5882 
5883       if (dynsymcount != 0)
5884 	{
5885 	  s->contents = bfd_alloc (output_bfd, s->size);
5886 	  if (s->contents == NULL)
5887 	    return FALSE;
5888 
5889 	  /* The first entry in .dynsym is a dummy symbol.
5890 	     Clear all the section syms, in case we don't output them all.  */
5891 	  ++section_sym_count;
5892 	  memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5893 	}
5894 
5895       elf_hash_table (info)->bucketcount = 0;
5896 
5897       /* Compute the size of the hashing table.  As a side effect this
5898 	 computes the hash values for all the names we export.  */
5899       if (info->emit_hash)
5900 	{
5901 	  unsigned long int *hashcodes;
5902 	  unsigned long int *hashcodesp;
5903 	  bfd_size_type amt;
5904 	  unsigned long int nsyms;
5905 	  size_t bucketcount;
5906 	  size_t hash_entry_size;
5907 
5908 	  /* Compute the hash values for all exported symbols.  At the same
5909 	     time store the values in an array so that we could use them for
5910 	     optimizations.  */
5911 	  amt = dynsymcount * sizeof (unsigned long int);
5912 	  hashcodes = bfd_malloc (amt);
5913 	  if (hashcodes == NULL)
5914 	    return FALSE;
5915 	  hashcodesp = hashcodes;
5916 
5917 	  /* Put all hash values in HASHCODES.  */
5918 	  elf_link_hash_traverse (elf_hash_table (info),
5919 				  elf_collect_hash_codes, &hashcodesp);
5920 
5921 	  nsyms = hashcodesp - hashcodes;
5922 	  bucketcount
5923 	    = compute_bucket_count (info, hashcodes, nsyms, 0);
5924 	  free (hashcodes);
5925 
5926 	  if (bucketcount == 0)
5927 	    return FALSE;
5928 
5929 	  elf_hash_table (info)->bucketcount = bucketcount;
5930 
5931 	  s = bfd_get_section_by_name (dynobj, ".hash");
5932 	  BFD_ASSERT (s != NULL);
5933 	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5934 	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5935 	  s->contents = bfd_zalloc (output_bfd, s->size);
5936 	  if (s->contents == NULL)
5937 	    return FALSE;
5938 
5939 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5940 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5941 		   s->contents + hash_entry_size);
5942 	}
5943 
5944       if (info->emit_gnu_hash)
5945 	{
5946 	  size_t i, cnt;
5947 	  unsigned char *contents;
5948 	  struct collect_gnu_hash_codes cinfo;
5949 	  bfd_size_type amt;
5950 	  size_t bucketcount;
5951 
5952 	  memset (&cinfo, 0, sizeof (cinfo));
5953 
5954 	  /* Compute the hash values for all exported symbols.  At the same
5955 	     time store the values in an array so that we could use them for
5956 	     optimizations.  */
5957 	  amt = dynsymcount * 2 * sizeof (unsigned long int);
5958 	  cinfo.hashcodes = bfd_malloc (amt);
5959 	  if (cinfo.hashcodes == NULL)
5960 	    return FALSE;
5961 
5962 	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
5963 	  cinfo.min_dynindx = -1;
5964 	  cinfo.output_bfd = output_bfd;
5965 	  cinfo.bed = bed;
5966 
5967 	  /* Put all hash values in HASHCODES.  */
5968 	  elf_link_hash_traverse (elf_hash_table (info),
5969 				  elf_collect_gnu_hash_codes, &cinfo);
5970 
5971 	  bucketcount
5972 	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
5973 
5974 	  if (bucketcount == 0)
5975 	    {
5976 	      free (cinfo.hashcodes);
5977 	      return FALSE;
5978 	    }
5979 
5980 	  s = bfd_get_section_by_name (dynobj, ".gnu.hash");
5981 	  BFD_ASSERT (s != NULL);
5982 
5983 	  if (cinfo.nsyms == 0)
5984 	    {
5985 	      /* Empty .gnu.hash section is special.  */
5986 	      BFD_ASSERT (cinfo.min_dynindx == -1);
5987 	      free (cinfo.hashcodes);
5988 	      s->size = 5 * 4 + bed->s->arch_size / 8;
5989 	      contents = bfd_zalloc (output_bfd, s->size);
5990 	      if (contents == NULL)
5991 		return FALSE;
5992 	      s->contents = contents;
5993 	      /* 1 empty bucket.  */
5994 	      bfd_put_32 (output_bfd, 1, contents);
5995 	      /* SYMIDX above the special symbol 0.  */
5996 	      bfd_put_32 (output_bfd, 1, contents + 4);
5997 	      /* Just one word for bitmask.  */
5998 	      bfd_put_32 (output_bfd, 1, contents + 8);
5999 	      /* Only hash fn bloom filter.  */
6000 	      bfd_put_32 (output_bfd, 0, contents + 12);
6001 	      /* No hashes are valid - empty bitmask.  */
6002 	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6003 	      /* No hashes in the only bucket.  */
6004 	      bfd_put_32 (output_bfd, 0,
6005 			  contents + 16 + bed->s->arch_size / 8);
6006 	    }
6007 	  else
6008 	    {
6009 	      BFD_ASSERT (cinfo.min_dynindx != -1);
6010 	      unsigned long int maskwords, maskbitslog2;
6011 
6012 	      maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6013 	      if (maskbitslog2 < 3)
6014 		maskbitslog2 = 5;
6015 	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6016 		maskbitslog2 = maskbitslog2 + 3;
6017 	      else
6018 		maskbitslog2 = maskbitslog2 + 2;
6019 	      if (bed->s->arch_size == 64)
6020 		{
6021 		  if (maskbitslog2 == 5)
6022 		    maskbitslog2 = 6;
6023 		  cinfo.shift1 = 6;
6024 		}
6025 	      else
6026 		cinfo.shift1 = 5;
6027 	      cinfo.mask = (1 << cinfo.shift1) - 1;
6028 	      cinfo.shift2 = maskbitslog2;
6029 	      cinfo.maskbits = 1 << maskbitslog2;
6030 	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6031 	      amt = bucketcount * sizeof (unsigned long int) * 2;
6032 	      amt += maskwords * sizeof (bfd_vma);
6033 	      cinfo.bitmask = bfd_malloc (amt);
6034 	      if (cinfo.bitmask == NULL)
6035 		{
6036 		  free (cinfo.hashcodes);
6037 		  return FALSE;
6038 		}
6039 
6040 	      cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6041 	      cinfo.indx = cinfo.counts + bucketcount;
6042 	      cinfo.symindx = dynsymcount - cinfo.nsyms;
6043 	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6044 
6045 	      /* Determine how often each hash bucket is used.  */
6046 	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6047 	      for (i = 0; i < cinfo.nsyms; ++i)
6048 		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6049 
6050 	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6051 		if (cinfo.counts[i] != 0)
6052 		  {
6053 		    cinfo.indx[i] = cnt;
6054 		    cnt += cinfo.counts[i];
6055 		  }
6056 	      BFD_ASSERT (cnt == dynsymcount);
6057 	      cinfo.bucketcount = bucketcount;
6058 	      cinfo.local_indx = cinfo.min_dynindx;
6059 
6060 	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6061 	      s->size += cinfo.maskbits / 8;
6062 	      contents = bfd_zalloc (output_bfd, s->size);
6063 	      if (contents == NULL)
6064 		{
6065 		  free (cinfo.bitmask);
6066 		  free (cinfo.hashcodes);
6067 		  return FALSE;
6068 		}
6069 
6070 	      s->contents = contents;
6071 	      bfd_put_32 (output_bfd, bucketcount, contents);
6072 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6073 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
6074 	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6075 	      contents += 16 + cinfo.maskbits / 8;
6076 
6077 	      for (i = 0; i < bucketcount; ++i)
6078 		{
6079 		  if (cinfo.counts[i] == 0)
6080 		    bfd_put_32 (output_bfd, 0, contents);
6081 		  else
6082 		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6083 		  contents += 4;
6084 		}
6085 
6086 	      cinfo.contents = contents;
6087 
6088 	      /* Renumber dynamic symbols, populate .gnu.hash section.  */
6089 	      elf_link_hash_traverse (elf_hash_table (info),
6090 				      elf_renumber_gnu_hash_syms, &cinfo);
6091 
6092 	      contents = s->contents + 16;
6093 	      for (i = 0; i < maskwords; ++i)
6094 		{
6095 		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6096 			   contents);
6097 		  contents += bed->s->arch_size / 8;
6098 		}
6099 
6100 	      free (cinfo.bitmask);
6101 	      free (cinfo.hashcodes);
6102 	    }
6103 	}
6104 
6105       s = bfd_get_section_by_name (dynobj, ".dynstr");
6106       BFD_ASSERT (s != NULL);
6107 
6108       elf_finalize_dynstr (output_bfd, info);
6109 
6110       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6111 
6112       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6113 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6114 	  return FALSE;
6115     }
6116 
6117   return TRUE;
6118 }
6119 
6120 /* Final phase of ELF linker.  */
6121 
6122 /* A structure we use to avoid passing large numbers of arguments.  */
6123 
6124 struct elf_final_link_info
6125 {
6126   /* General link information.  */
6127   struct bfd_link_info *info;
6128   /* Output BFD.  */
6129   bfd *output_bfd;
6130   /* Symbol string table.  */
6131   struct bfd_strtab_hash *symstrtab;
6132   /* .dynsym section.  */
6133   asection *dynsym_sec;
6134   /* .hash section.  */
6135   asection *hash_sec;
6136   /* symbol version section (.gnu.version).  */
6137   asection *symver_sec;
6138   /* Buffer large enough to hold contents of any section.  */
6139   bfd_byte *contents;
6140   /* Buffer large enough to hold external relocs of any section.  */
6141   void *external_relocs;
6142   /* Buffer large enough to hold internal relocs of any section.  */
6143   Elf_Internal_Rela *internal_relocs;
6144   /* Buffer large enough to hold external local symbols of any input
6145      BFD.  */
6146   bfd_byte *external_syms;
6147   /* And a buffer for symbol section indices.  */
6148   Elf_External_Sym_Shndx *locsym_shndx;
6149   /* Buffer large enough to hold internal local symbols of any input
6150      BFD.  */
6151   Elf_Internal_Sym *internal_syms;
6152   /* Array large enough to hold a symbol index for each local symbol
6153      of any input BFD.  */
6154   long *indices;
6155   /* Array large enough to hold a section pointer for each local
6156      symbol of any input BFD.  */
6157   asection **sections;
6158   /* Buffer to hold swapped out symbols.  */
6159   bfd_byte *symbuf;
6160   /* And one for symbol section indices.  */
6161   Elf_External_Sym_Shndx *symshndxbuf;
6162   /* Number of swapped out symbols in buffer.  */
6163   size_t symbuf_count;
6164   /* Number of symbols which fit in symbuf.  */
6165   size_t symbuf_size;
6166   /* And same for symshndxbuf.  */
6167   size_t shndxbuf_size;
6168 };
6169 
6170 /* This struct is used to pass information to elf_link_output_extsym.  */
6171 
6172 struct elf_outext_info
6173 {
6174   bfd_boolean failed;
6175   bfd_boolean localsyms;
6176   struct elf_final_link_info *finfo;
6177 };
6178 
6179 /* When performing a relocatable link, the input relocations are
6180    preserved.  But, if they reference global symbols, the indices
6181    referenced must be updated.  Update all the relocations in
6182    REL_HDR (there are COUNT of them), using the data in REL_HASH.  */
6183 
6184 static void
6185 elf_link_adjust_relocs (bfd *abfd,
6186 			Elf_Internal_Shdr *rel_hdr,
6187 			unsigned int count,
6188 			struct elf_link_hash_entry **rel_hash)
6189 {
6190   unsigned int i;
6191   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6192   bfd_byte *erela;
6193   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
6194   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
6195   bfd_vma r_type_mask;
6196   int r_sym_shift;
6197 
6198   if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
6199     {
6200       swap_in = bed->s->swap_reloc_in;
6201       swap_out = bed->s->swap_reloc_out;
6202     }
6203   else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
6204     {
6205       swap_in = bed->s->swap_reloca_in;
6206       swap_out = bed->s->swap_reloca_out;
6207     }
6208   else
6209     abort ();
6210 
6211   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
6212     abort ();
6213 
6214   if (bed->s->arch_size == 32)
6215     {
6216       r_type_mask = 0xff;
6217       r_sym_shift = 8;
6218     }
6219   else
6220     {
6221       r_type_mask = 0xffffffff;
6222       r_sym_shift = 32;
6223     }
6224 
6225   erela = rel_hdr->contents;
6226   for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
6227     {
6228       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
6229       unsigned int j;
6230 
6231       if (*rel_hash == NULL)
6232 	continue;
6233 
6234       BFD_ASSERT ((*rel_hash)->indx >= 0);
6235 
6236       (*swap_in) (abfd, erela, irela);
6237       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
6238 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
6239 			   | (irela[j].r_info & r_type_mask));
6240       (*swap_out) (abfd, irela, erela);
6241     }
6242 }
6243 
6244 struct elf_link_sort_rela
6245 {
6246   union {
6247     bfd_vma offset;
6248     bfd_vma sym_mask;
6249   } u;
6250   enum elf_reloc_type_class type;
6251   /* We use this as an array of size int_rels_per_ext_rel.  */
6252   Elf_Internal_Rela rela[1];
6253 };
6254 
6255 static int
6256 elf_link_sort_cmp1 (const void *A, const void *B)
6257 {
6258   const struct elf_link_sort_rela *a = A;
6259   const struct elf_link_sort_rela *b = B;
6260   int relativea, relativeb;
6261 
6262   relativea = a->type == reloc_class_relative;
6263   relativeb = b->type == reloc_class_relative;
6264 
6265   if (relativea < relativeb)
6266     return 1;
6267   if (relativea > relativeb)
6268     return -1;
6269   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
6270     return -1;
6271   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
6272     return 1;
6273   if (a->rela->r_offset < b->rela->r_offset)
6274     return -1;
6275   if (a->rela->r_offset > b->rela->r_offset)
6276     return 1;
6277   return 0;
6278 }
6279 
6280 static int
6281 elf_link_sort_cmp2 (const void *A, const void *B)
6282 {
6283   const struct elf_link_sort_rela *a = A;
6284   const struct elf_link_sort_rela *b = B;
6285   int copya, copyb;
6286 
6287   if (a->u.offset < b->u.offset)
6288     return -1;
6289   if (a->u.offset > b->u.offset)
6290     return 1;
6291   copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
6292   copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
6293   if (copya < copyb)
6294     return -1;
6295   if (copya > copyb)
6296     return 1;
6297   if (a->rela->r_offset < b->rela->r_offset)
6298     return -1;
6299   if (a->rela->r_offset > b->rela->r_offset)
6300     return 1;
6301   return 0;
6302 }
6303 
6304 static size_t
6305 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
6306 {
6307   asection *reldyn;
6308   bfd_size_type count, size;
6309   size_t i, ret, sort_elt, ext_size;
6310   bfd_byte *sort, *s_non_relative, *p;
6311   struct elf_link_sort_rela *sq;
6312   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6313   int i2e = bed->s->int_rels_per_ext_rel;
6314   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
6315   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
6316   struct bfd_link_order *lo;
6317   bfd_vma r_sym_mask;
6318 
6319   reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
6320   if (reldyn == NULL || reldyn->size == 0)
6321     {
6322       reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
6323       if (reldyn == NULL || reldyn->size == 0)
6324 	return 0;
6325       ext_size = bed->s->sizeof_rel;
6326       swap_in = bed->s->swap_reloc_in;
6327       swap_out = bed->s->swap_reloc_out;
6328     }
6329   else
6330     {
6331       ext_size = bed->s->sizeof_rela;
6332       swap_in = bed->s->swap_reloca_in;
6333       swap_out = bed->s->swap_reloca_out;
6334     }
6335   count = reldyn->size / ext_size;
6336 
6337   size = 0;
6338   for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6339     if (lo->type == bfd_indirect_link_order)
6340       {
6341 	asection *o = lo->u.indirect.section;
6342 	size += o->size;
6343       }
6344 
6345   if (size != reldyn->size)
6346     return 0;
6347 
6348   sort_elt = (sizeof (struct elf_link_sort_rela)
6349 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
6350   sort = bfd_zmalloc (sort_elt * count);
6351   if (sort == NULL)
6352     {
6353       (*info->callbacks->warning)
6354 	(info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
6355       return 0;
6356     }
6357 
6358   if (bed->s->arch_size == 32)
6359     r_sym_mask = ~(bfd_vma) 0xff;
6360   else
6361     r_sym_mask = ~(bfd_vma) 0xffffffff;
6362 
6363   for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6364     if (lo->type == bfd_indirect_link_order)
6365       {
6366 	bfd_byte *erel, *erelend;
6367 	asection *o = lo->u.indirect.section;
6368 
6369 	if (o->contents == NULL && o->size != 0)
6370 	  {
6371 	    /* This is a reloc section that is being handled as a normal
6372 	       section.  See bfd_section_from_shdr.  We can't combine
6373 	       relocs in this case.  */
6374 	    free (sort);
6375 	    return 0;
6376 	  }
6377 	erel = o->contents;
6378 	erelend = o->contents + o->size;
6379 	p = sort + o->output_offset / ext_size * sort_elt;
6380 	while (erel < erelend)
6381 	  {
6382 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6383 	    (*swap_in) (abfd, erel, s->rela);
6384 	    s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6385 	    s->u.sym_mask = r_sym_mask;
6386 	    p += sort_elt;
6387 	    erel += ext_size;
6388 	  }
6389       }
6390 
6391   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6392 
6393   for (i = 0, p = sort; i < count; i++, p += sort_elt)
6394     {
6395       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6396       if (s->type != reloc_class_relative)
6397 	break;
6398     }
6399   ret = i;
6400   s_non_relative = p;
6401 
6402   sq = (struct elf_link_sort_rela *) s_non_relative;
6403   for (; i < count; i++, p += sort_elt)
6404     {
6405       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6406       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6407 	sq = sp;
6408       sp->u.offset = sq->rela->r_offset;
6409     }
6410 
6411   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6412 
6413   for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6414     if (lo->type == bfd_indirect_link_order)
6415       {
6416 	bfd_byte *erel, *erelend;
6417 	asection *o = lo->u.indirect.section;
6418 
6419 	erel = o->contents;
6420 	erelend = o->contents + o->size;
6421 	p = sort + o->output_offset / ext_size * sort_elt;
6422 	while (erel < erelend)
6423 	  {
6424 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6425 	    (*swap_out) (abfd, s->rela, erel);
6426 	    p += sort_elt;
6427 	    erel += ext_size;
6428 	  }
6429       }
6430 
6431   free (sort);
6432   *psec = reldyn;
6433   return ret;
6434 }
6435 
6436 /* Flush the output symbols to the file.  */
6437 
6438 static bfd_boolean
6439 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6440 			    const struct elf_backend_data *bed)
6441 {
6442   if (finfo->symbuf_count > 0)
6443     {
6444       Elf_Internal_Shdr *hdr;
6445       file_ptr pos;
6446       bfd_size_type amt;
6447 
6448       hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6449       pos = hdr->sh_offset + hdr->sh_size;
6450       amt = finfo->symbuf_count * bed->s->sizeof_sym;
6451       if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6452 	  || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6453 	return FALSE;
6454 
6455       hdr->sh_size += amt;
6456       finfo->symbuf_count = 0;
6457     }
6458 
6459   return TRUE;
6460 }
6461 
6462 /* Add a symbol to the output symbol table.  */
6463 
6464 static bfd_boolean
6465 elf_link_output_sym (struct elf_final_link_info *finfo,
6466 		     const char *name,
6467 		     Elf_Internal_Sym *elfsym,
6468 		     asection *input_sec,
6469 		     struct elf_link_hash_entry *h)
6470 {
6471   bfd_byte *dest;
6472   Elf_External_Sym_Shndx *destshndx;
6473   bfd_boolean (*output_symbol_hook)
6474     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6475      struct elf_link_hash_entry *);
6476   const struct elf_backend_data *bed;
6477 
6478   bed = get_elf_backend_data (finfo->output_bfd);
6479   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6480   if (output_symbol_hook != NULL)
6481     {
6482       if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6483 	return FALSE;
6484     }
6485 
6486   if (name == NULL || *name == '\0')
6487     elfsym->st_name = 0;
6488   else if (input_sec->flags & SEC_EXCLUDE)
6489     elfsym->st_name = 0;
6490   else
6491     {
6492       elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6493 							    name, TRUE, FALSE);
6494       if (elfsym->st_name == (unsigned long) -1)
6495 	return FALSE;
6496     }
6497 
6498   if (finfo->symbuf_count >= finfo->symbuf_size)
6499     {
6500       if (! elf_link_flush_output_syms (finfo, bed))
6501 	return FALSE;
6502     }
6503 
6504   dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6505   destshndx = finfo->symshndxbuf;
6506   if (destshndx != NULL)
6507     {
6508       if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6509 	{
6510 	  bfd_size_type amt;
6511 
6512 	  amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6513 	  finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6514 	  if (destshndx == NULL)
6515 	    return FALSE;
6516 	  memset ((char *) destshndx + amt, 0, amt);
6517 	  finfo->shndxbuf_size *= 2;
6518 	}
6519       destshndx += bfd_get_symcount (finfo->output_bfd);
6520     }
6521 
6522   bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6523   finfo->symbuf_count += 1;
6524   bfd_get_symcount (finfo->output_bfd) += 1;
6525 
6526   return TRUE;
6527 }
6528 
6529 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
6530 
6531 static bfd_boolean
6532 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
6533 {
6534   if (sym->st_shndx > SHN_HIRESERVE)
6535     {
6536       /* The gABI doesn't support dynamic symbols in output sections
6537          beyond 64k.  */
6538       (*_bfd_error_handler)
6539 	(_("%B: Too many sections: %d (>= %d)"),
6540 	 abfd, bfd_count_sections (abfd), SHN_LORESERVE);
6541       bfd_set_error (bfd_error_nonrepresentable_section);
6542       return FALSE;
6543     }
6544   return TRUE;
6545 }
6546 
6547 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6548    allowing an unsatisfied unversioned symbol in the DSO to match a
6549    versioned symbol that would normally require an explicit version.
6550    We also handle the case that a DSO references a hidden symbol
6551    which may be satisfied by a versioned symbol in another DSO.  */
6552 
6553 static bfd_boolean
6554 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6555 				 const struct elf_backend_data *bed,
6556 				 struct elf_link_hash_entry *h)
6557 {
6558   bfd *abfd;
6559   struct elf_link_loaded_list *loaded;
6560 
6561   if (!is_elf_hash_table (info->hash))
6562     return FALSE;
6563 
6564   switch (h->root.type)
6565     {
6566     default:
6567       abfd = NULL;
6568       break;
6569 
6570     case bfd_link_hash_undefined:
6571     case bfd_link_hash_undefweak:
6572       abfd = h->root.u.undef.abfd;
6573       if ((abfd->flags & DYNAMIC) == 0
6574 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6575 	return FALSE;
6576       break;
6577 
6578     case bfd_link_hash_defined:
6579     case bfd_link_hash_defweak:
6580       abfd = h->root.u.def.section->owner;
6581       break;
6582 
6583     case bfd_link_hash_common:
6584       abfd = h->root.u.c.p->section->owner;
6585       break;
6586     }
6587   BFD_ASSERT (abfd != NULL);
6588 
6589   for (loaded = elf_hash_table (info)->loaded;
6590        loaded != NULL;
6591        loaded = loaded->next)
6592     {
6593       bfd *input;
6594       Elf_Internal_Shdr *hdr;
6595       bfd_size_type symcount;
6596       bfd_size_type extsymcount;
6597       bfd_size_type extsymoff;
6598       Elf_Internal_Shdr *versymhdr;
6599       Elf_Internal_Sym *isym;
6600       Elf_Internal_Sym *isymend;
6601       Elf_Internal_Sym *isymbuf;
6602       Elf_External_Versym *ever;
6603       Elf_External_Versym *extversym;
6604 
6605       input = loaded->abfd;
6606 
6607       /* We check each DSO for a possible hidden versioned definition.  */
6608       if (input == abfd
6609 	  || (input->flags & DYNAMIC) == 0
6610 	  || elf_dynversym (input) == 0)
6611 	continue;
6612 
6613       hdr = &elf_tdata (input)->dynsymtab_hdr;
6614 
6615       symcount = hdr->sh_size / bed->s->sizeof_sym;
6616       if (elf_bad_symtab (input))
6617 	{
6618 	  extsymcount = symcount;
6619 	  extsymoff = 0;
6620 	}
6621       else
6622 	{
6623 	  extsymcount = symcount - hdr->sh_info;
6624 	  extsymoff = hdr->sh_info;
6625 	}
6626 
6627       if (extsymcount == 0)
6628 	continue;
6629 
6630       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6631 				      NULL, NULL, NULL);
6632       if (isymbuf == NULL)
6633 	return FALSE;
6634 
6635       /* Read in any version definitions.  */
6636       versymhdr = &elf_tdata (input)->dynversym_hdr;
6637       extversym = bfd_malloc (versymhdr->sh_size);
6638       if (extversym == NULL)
6639 	goto error_ret;
6640 
6641       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6642 	  || (bfd_bread (extversym, versymhdr->sh_size, input)
6643 	      != versymhdr->sh_size))
6644 	{
6645 	  free (extversym);
6646 	error_ret:
6647 	  free (isymbuf);
6648 	  return FALSE;
6649 	}
6650 
6651       ever = extversym + extsymoff;
6652       isymend = isymbuf + extsymcount;
6653       for (isym = isymbuf; isym < isymend; isym++, ever++)
6654 	{
6655 	  const char *name;
6656 	  Elf_Internal_Versym iver;
6657 	  unsigned short version_index;
6658 
6659 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6660 	      || isym->st_shndx == SHN_UNDEF)
6661 	    continue;
6662 
6663 	  name = bfd_elf_string_from_elf_section (input,
6664 						  hdr->sh_link,
6665 						  isym->st_name);
6666 	  if (strcmp (name, h->root.root.string) != 0)
6667 	    continue;
6668 
6669 	  _bfd_elf_swap_versym_in (input, ever, &iver);
6670 
6671 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6672 	    {
6673 	      /* If we have a non-hidden versioned sym, then it should
6674 		 have provided a definition for the undefined sym.  */
6675 	      abort ();
6676 	    }
6677 
6678 	  version_index = iver.vs_vers & VERSYM_VERSION;
6679 	  if (version_index == 1 || version_index == 2)
6680 	    {
6681 	      /* This is the base or first version.  We can use it.  */
6682 	      free (extversym);
6683 	      free (isymbuf);
6684 	      return TRUE;
6685 	    }
6686 	}
6687 
6688       free (extversym);
6689       free (isymbuf);
6690     }
6691 
6692   return FALSE;
6693 }
6694 
6695 /* Add an external symbol to the symbol table.  This is called from
6696    the hash table traversal routine.  When generating a shared object,
6697    we go through the symbol table twice.  The first time we output
6698    anything that might have been forced to local scope in a version
6699    script.  The second time we output the symbols that are still
6700    global symbols.  */
6701 
6702 static bfd_boolean
6703 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6704 {
6705   struct elf_outext_info *eoinfo = data;
6706   struct elf_final_link_info *finfo = eoinfo->finfo;
6707   bfd_boolean strip;
6708   Elf_Internal_Sym sym;
6709   asection *input_sec;
6710   const struct elf_backend_data *bed;
6711 
6712   if (h->root.type == bfd_link_hash_warning)
6713     {
6714       h = (struct elf_link_hash_entry *) h->root.u.i.link;
6715       if (h->root.type == bfd_link_hash_new)
6716 	return TRUE;
6717     }
6718 
6719   /* Decide whether to output this symbol in this pass.  */
6720   if (eoinfo->localsyms)
6721     {
6722       if (!h->forced_local)
6723 	return TRUE;
6724     }
6725   else
6726     {
6727       if (h->forced_local)
6728 	return TRUE;
6729     }
6730 
6731   bed = get_elf_backend_data (finfo->output_bfd);
6732 
6733   if (h->root.type == bfd_link_hash_undefined)
6734     {
6735       /* If we have an undefined symbol reference here then it must have
6736 	 come from a shared library that is being linked in.  (Undefined
6737 	 references in regular files have already been handled).  */
6738       bfd_boolean ignore_undef = FALSE;
6739 
6740       /* Some symbols may be special in that the fact that they're
6741 	 undefined can be safely ignored - let backend determine that.  */
6742       if (bed->elf_backend_ignore_undef_symbol)
6743 	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
6744 
6745       /* If we are reporting errors for this situation then do so now.  */
6746       if (ignore_undef == FALSE
6747 	  && h->ref_dynamic
6748 	  && ! h->ref_regular
6749 	  && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6750 	  && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6751 	{
6752 	  if (! (finfo->info->callbacks->undefined_symbol
6753 		 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6754 		  NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6755 	    {
6756 	      eoinfo->failed = TRUE;
6757 	      return FALSE;
6758 	    }
6759 	}
6760     }
6761 
6762   /* We should also warn if a forced local symbol is referenced from
6763      shared libraries.  */
6764   if (! finfo->info->relocatable
6765       && (! finfo->info->shared)
6766       && h->forced_local
6767       && h->ref_dynamic
6768       && !h->dynamic_def
6769       && !h->dynamic_weak
6770       && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6771     {
6772       (*_bfd_error_handler)
6773 	(_("%B: %s symbol `%s' in %B is referenced by DSO"),
6774 	 finfo->output_bfd,
6775 	 h->root.u.def.section == bfd_abs_section_ptr
6776 	 ? finfo->output_bfd : h->root.u.def.section->owner,
6777 	 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6778 	 ? "internal"
6779 	 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6780 	 ? "hidden" : "local",
6781 	 h->root.root.string);
6782       eoinfo->failed = TRUE;
6783       return FALSE;
6784     }
6785 
6786   /* We don't want to output symbols that have never been mentioned by
6787      a regular file, or that we have been told to strip.  However, if
6788      h->indx is set to -2, the symbol is used by a reloc and we must
6789      output it.  */
6790   if (h->indx == -2)
6791     strip = FALSE;
6792   else if ((h->def_dynamic
6793 	    || h->ref_dynamic
6794 	    || h->root.type == bfd_link_hash_new)
6795 	   && !h->def_regular
6796 	   && !h->ref_regular)
6797     strip = TRUE;
6798   else if (finfo->info->strip == strip_all)
6799     strip = TRUE;
6800   else if (finfo->info->strip == strip_some
6801 	   && bfd_hash_lookup (finfo->info->keep_hash,
6802 			       h->root.root.string, FALSE, FALSE) == NULL)
6803     strip = TRUE;
6804   else if (finfo->info->strip_discarded
6805 	   && (h->root.type == bfd_link_hash_defined
6806 	       || h->root.type == bfd_link_hash_defweak)
6807 	   && elf_discarded_section (h->root.u.def.section))
6808     strip = TRUE;
6809   else
6810     strip = FALSE;
6811 
6812   /* If we're stripping it, and it's not a dynamic symbol, there's
6813      nothing else to do unless it is a forced local symbol.  */
6814   if (strip
6815       && h->dynindx == -1
6816       && !h->forced_local)
6817     return TRUE;
6818 
6819   sym.st_value = 0;
6820   sym.st_size = h->size;
6821   sym.st_other = h->other;
6822   if (h->forced_local)
6823     sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6824   else if (h->root.type == bfd_link_hash_undefweak
6825 	   || h->root.type == bfd_link_hash_defweak)
6826     sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6827   else
6828     sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6829 
6830   switch (h->root.type)
6831     {
6832     default:
6833     case bfd_link_hash_new:
6834     case bfd_link_hash_warning:
6835       abort ();
6836       return FALSE;
6837 
6838     case bfd_link_hash_undefined:
6839     case bfd_link_hash_undefweak:
6840       input_sec = bfd_und_section_ptr;
6841       sym.st_shndx = SHN_UNDEF;
6842       break;
6843 
6844     case bfd_link_hash_defined:
6845     case bfd_link_hash_defweak:
6846       {
6847 	input_sec = h->root.u.def.section;
6848 	if (input_sec->output_section != NULL)
6849 	  {
6850 	    sym.st_shndx =
6851 	      _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6852 						 input_sec->output_section);
6853 	    if (sym.st_shndx == SHN_BAD)
6854 	      {
6855 		(*_bfd_error_handler)
6856 		  (_("%B: could not find output section %A for input section %A"),
6857 		   finfo->output_bfd, input_sec->output_section, input_sec);
6858 		eoinfo->failed = TRUE;
6859 		return FALSE;
6860 	      }
6861 
6862 	    /* ELF symbols in relocatable files are section relative,
6863 	       but in nonrelocatable files they are virtual
6864 	       addresses.  */
6865 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
6866 	    if (! finfo->info->relocatable)
6867 	      {
6868 		sym.st_value += input_sec->output_section->vma;
6869 		if (h->type == STT_TLS)
6870 		  {
6871 		    /* STT_TLS symbols are relative to PT_TLS segment
6872 		       base.  */
6873 		    BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6874 		    sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6875 		  }
6876 	      }
6877 	  }
6878 	else
6879 	  {
6880 	    BFD_ASSERT (input_sec->owner == NULL
6881 			|| (input_sec->owner->flags & DYNAMIC) != 0);
6882 	    sym.st_shndx = SHN_UNDEF;
6883 	    input_sec = bfd_und_section_ptr;
6884 	  }
6885       }
6886       break;
6887 
6888     case bfd_link_hash_common:
6889       input_sec = h->root.u.c.p->section;
6890       sym.st_shndx = bed->common_section_index (input_sec);
6891       sym.st_value = 1 << h->root.u.c.p->alignment_power;
6892       break;
6893 
6894     case bfd_link_hash_indirect:
6895       /* These symbols are created by symbol versioning.  They point
6896 	 to the decorated version of the name.  For example, if the
6897 	 symbol foo@@GNU_1.2 is the default, which should be used when
6898 	 foo is used with no version, then we add an indirect symbol
6899 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
6900 	 since the indirected symbol is already in the hash table.  */
6901       return TRUE;
6902     }
6903 
6904   /* Give the processor backend a chance to tweak the symbol value,
6905      and also to finish up anything that needs to be done for this
6906      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
6907      forced local syms when non-shared is due to a historical quirk.  */
6908   if ((h->dynindx != -1
6909        || h->forced_local)
6910       && ((finfo->info->shared
6911 	   && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6912 	       || h->root.type != bfd_link_hash_undefweak))
6913 	  || !h->forced_local)
6914       && elf_hash_table (finfo->info)->dynamic_sections_created)
6915     {
6916       if (! ((*bed->elf_backend_finish_dynamic_symbol)
6917 	     (finfo->output_bfd, finfo->info, h, &sym)))
6918 	{
6919 	  eoinfo->failed = TRUE;
6920 	  return FALSE;
6921 	}
6922     }
6923 
6924   /* If we are marking the symbol as undefined, and there are no
6925      non-weak references to this symbol from a regular object, then
6926      mark the symbol as weak undefined; if there are non-weak
6927      references, mark the symbol as strong.  We can't do this earlier,
6928      because it might not be marked as undefined until the
6929      finish_dynamic_symbol routine gets through with it.  */
6930   if (sym.st_shndx == SHN_UNDEF
6931       && h->ref_regular
6932       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6933 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6934     {
6935       int bindtype;
6936 
6937       if (h->ref_regular_nonweak)
6938 	bindtype = STB_GLOBAL;
6939       else
6940 	bindtype = STB_WEAK;
6941       sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6942     }
6943 
6944   /* If a non-weak symbol with non-default visibility is not defined
6945      locally, it is a fatal error.  */
6946   if (! finfo->info->relocatable
6947       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6948       && ELF_ST_BIND (sym.st_info) != STB_WEAK
6949       && h->root.type == bfd_link_hash_undefined
6950       && !h->def_regular)
6951     {
6952       (*_bfd_error_handler)
6953 	(_("%B: %s symbol `%s' isn't defined"),
6954 	 finfo->output_bfd,
6955 	 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6956 	 ? "protected"
6957 	 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6958 	 ? "internal" : "hidden",
6959 	 h->root.root.string);
6960       eoinfo->failed = TRUE;
6961       return FALSE;
6962     }
6963 
6964   /* If this symbol should be put in the .dynsym section, then put it
6965      there now.  We already know the symbol index.  We also fill in
6966      the entry in the .hash section.  */
6967   if (h->dynindx != -1
6968       && elf_hash_table (finfo->info)->dynamic_sections_created)
6969     {
6970       size_t bucketcount;
6971       size_t bucket;
6972       bfd_byte *esym;
6973 
6974       sym.st_name = h->dynstr_index;
6975       esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6976       if (! check_dynsym (finfo->output_bfd, &sym))
6977 	{
6978 	  eoinfo->failed = TRUE;
6979 	  return FALSE;
6980 	}
6981       bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6982 
6983       bucketcount = elf_hash_table (finfo->info)->bucketcount;
6984       bucket = h->u.elf_hash_value % bucketcount;
6985 
6986       if (finfo->hash_sec != NULL)
6987 	{
6988 	  size_t hash_entry_size;
6989 	  bfd_byte *bucketpos;
6990 	  bfd_vma chain;
6991 
6992 	  hash_entry_size
6993 	    = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6994 	  bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6995 		       + (bucket + 2) * hash_entry_size);
6996 	  chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6997 	  bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6998 	  bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6999 		   ((bfd_byte *) finfo->hash_sec->contents
7000 		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
7001 	}
7002 
7003       if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
7004 	{
7005 	  Elf_Internal_Versym iversym;
7006 	  Elf_External_Versym *eversym;
7007 
7008 	  if (!h->def_regular)
7009 	    {
7010 	      if (h->verinfo.verdef == NULL)
7011 		iversym.vs_vers = 0;
7012 	      else
7013 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
7014 	    }
7015 	  else
7016 	    {
7017 	      if (h->verinfo.vertree == NULL)
7018 		iversym.vs_vers = 1;
7019 	      else
7020 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
7021 	      if (finfo->info->create_default_symver)
7022 		iversym.vs_vers++;
7023 	    }
7024 
7025 	  if (h->hidden)
7026 	    iversym.vs_vers |= VERSYM_HIDDEN;
7027 
7028 	  eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
7029 	  eversym += h->dynindx;
7030 	  _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
7031 	}
7032     }
7033 
7034   /* If we're stripping it, then it was just a dynamic symbol, and
7035      there's nothing else to do.  */
7036   if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
7037     return TRUE;
7038 
7039   h->indx = bfd_get_symcount (finfo->output_bfd);
7040 
7041   if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
7042     {
7043       eoinfo->failed = TRUE;
7044       return FALSE;
7045     }
7046 
7047   return TRUE;
7048 }
7049 
7050 /* Return TRUE if special handling is done for relocs in SEC against
7051    symbols defined in discarded sections.  */
7052 
7053 static bfd_boolean
7054 elf_section_ignore_discarded_relocs (asection *sec)
7055 {
7056   const struct elf_backend_data *bed;
7057 
7058   switch (sec->sec_info_type)
7059     {
7060     case ELF_INFO_TYPE_STABS:
7061     case ELF_INFO_TYPE_EH_FRAME:
7062       return TRUE;
7063     default:
7064       break;
7065     }
7066 
7067   bed = get_elf_backend_data (sec->owner);
7068   if (bed->elf_backend_ignore_discarded_relocs != NULL
7069       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
7070     return TRUE;
7071 
7072   return FALSE;
7073 }
7074 
7075 /* Return a mask saying how ld should treat relocations in SEC against
7076    symbols defined in discarded sections.  If this function returns
7077    COMPLAIN set, ld will issue a warning message.  If this function
7078    returns PRETEND set, and the discarded section was link-once and the
7079    same size as the kept link-once section, ld will pretend that the
7080    symbol was actually defined in the kept section.  Otherwise ld will
7081    zero the reloc (at least that is the intent, but some cooperation by
7082    the target dependent code is needed, particularly for REL targets).  */
7083 
7084 unsigned int
7085 _bfd_elf_default_action_discarded (asection *sec)
7086 {
7087   if (sec->flags & SEC_DEBUGGING)
7088     return PRETEND;
7089 
7090   if (strcmp (".eh_frame", sec->name) == 0)
7091     return 0;
7092 
7093   if (strcmp (".gcc_except_table", sec->name) == 0)
7094     return 0;
7095 
7096   return COMPLAIN | PRETEND;
7097 }
7098 
7099 /* Find a match between a section and a member of a section group.  */
7100 
7101 static asection *
7102 match_group_member (asection *sec, asection *group,
7103 		    struct bfd_link_info *info)
7104 {
7105   asection *first = elf_next_in_group (group);
7106   asection *s = first;
7107 
7108   while (s != NULL)
7109     {
7110       if (bfd_elf_match_symbols_in_sections (s, sec, info))
7111 	return s;
7112 
7113       s = elf_next_in_group (s);
7114       if (s == first)
7115 	break;
7116     }
7117 
7118   return NULL;
7119 }
7120 
7121 /* Check if the kept section of a discarded section SEC can be used
7122    to replace it. Return the replacement if it is OK. Otherwise return
7123    NULL. */
7124 
7125 asection *
7126 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
7127 {
7128   asection *kept;
7129 
7130   kept = sec->kept_section;
7131   if (kept != NULL)
7132     {
7133       if (elf_sec_group (sec) != NULL)
7134 	kept = match_group_member (sec, kept, info);
7135       if (kept != NULL && sec->size != kept->size)
7136 	kept = NULL;
7137     }
7138   return kept;
7139 }
7140 
7141 /* Link an input file into the linker output file.  This function
7142    handles all the sections and relocations of the input file at once.
7143    This is so that we only have to read the local symbols once, and
7144    don't have to keep them in memory.  */
7145 
7146 static bfd_boolean
7147 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
7148 {
7149   bfd_boolean (*relocate_section)
7150     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
7151      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
7152   bfd *output_bfd;
7153   Elf_Internal_Shdr *symtab_hdr;
7154   size_t locsymcount;
7155   size_t extsymoff;
7156   Elf_Internal_Sym *isymbuf;
7157   Elf_Internal_Sym *isym;
7158   Elf_Internal_Sym *isymend;
7159   long *pindex;
7160   asection **ppsection;
7161   asection *o;
7162   const struct elf_backend_data *bed;
7163   bfd_boolean emit_relocs;
7164   struct elf_link_hash_entry **sym_hashes;
7165 
7166   output_bfd = finfo->output_bfd;
7167   bed = get_elf_backend_data (output_bfd);
7168   relocate_section = bed->elf_backend_relocate_section;
7169 
7170   /* If this is a dynamic object, we don't want to do anything here:
7171      we don't want the local symbols, and we don't want the section
7172      contents.  */
7173   if ((input_bfd->flags & DYNAMIC) != 0)
7174     return TRUE;
7175 
7176   emit_relocs = (finfo->info->relocatable
7177 		 || finfo->info->emitrelocations);
7178 
7179   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7180   if (elf_bad_symtab (input_bfd))
7181     {
7182       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
7183       extsymoff = 0;
7184     }
7185   else
7186     {
7187       locsymcount = symtab_hdr->sh_info;
7188       extsymoff = symtab_hdr->sh_info;
7189     }
7190 
7191   /* Read the local symbols.  */
7192   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
7193   if (isymbuf == NULL && locsymcount != 0)
7194     {
7195       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
7196 				      finfo->internal_syms,
7197 				      finfo->external_syms,
7198 				      finfo->locsym_shndx);
7199       if (isymbuf == NULL)
7200 	return FALSE;
7201     }
7202 
7203   /* Find local symbol sections and adjust values of symbols in
7204      SEC_MERGE sections.  Write out those local symbols we know are
7205      going into the output file.  */
7206   isymend = isymbuf + locsymcount;
7207   for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
7208        isym < isymend;
7209        isym++, pindex++, ppsection++)
7210     {
7211       asection *isec;
7212       const char *name;
7213       Elf_Internal_Sym osym;
7214 
7215       *pindex = -1;
7216 
7217       if (elf_bad_symtab (input_bfd))
7218 	{
7219 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
7220 	    {
7221 	      *ppsection = NULL;
7222 	      continue;
7223 	    }
7224 	}
7225 
7226       if (isym->st_shndx == SHN_UNDEF)
7227 	isec = bfd_und_section_ptr;
7228       else if (isym->st_shndx < SHN_LORESERVE
7229 	       || isym->st_shndx > SHN_HIRESERVE)
7230 	{
7231 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
7232 	  if (isec
7233 	      && isec->sec_info_type == ELF_INFO_TYPE_MERGE
7234 	      && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
7235 	    isym->st_value =
7236 	      _bfd_merged_section_offset (output_bfd, &isec,
7237 					  elf_section_data (isec)->sec_info,
7238 					  isym->st_value);
7239 	}
7240       else if (isym->st_shndx == SHN_ABS)
7241 	isec = bfd_abs_section_ptr;
7242       else if (isym->st_shndx == SHN_COMMON)
7243 	isec = bfd_com_section_ptr;
7244       else
7245 	{
7246 	  /* Don't attempt to output symbols with st_shnx in the
7247 	     reserved range other than SHN_ABS and SHN_COMMON.  */
7248 	  *ppsection = NULL;
7249 	  continue;
7250 	}
7251 
7252       *ppsection = isec;
7253 
7254       /* Don't output the first, undefined, symbol.  */
7255       if (ppsection == finfo->sections)
7256 	continue;
7257 
7258       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
7259 	{
7260 	  /* We never output section symbols.  Instead, we use the
7261 	     section symbol of the corresponding section in the output
7262 	     file.  */
7263 	  continue;
7264 	}
7265 
7266       /* If we are stripping all symbols, we don't want to output this
7267 	 one.  */
7268       if (finfo->info->strip == strip_all)
7269 	continue;
7270 
7271       /* If we are discarding all local symbols, we don't want to
7272 	 output this one.  If we are generating a relocatable output
7273 	 file, then some of the local symbols may be required by
7274 	 relocs; we output them below as we discover that they are
7275 	 needed.  */
7276       if (finfo->info->discard == discard_all)
7277 	continue;
7278 
7279       /* If this symbol is defined in a section which we are
7280 	 discarding, we don't need to keep it.  */
7281       if (isym->st_shndx != SHN_UNDEF
7282 	  && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
7283 	  && (isec == NULL
7284 	      || bfd_section_removed_from_list (output_bfd,
7285 						isec->output_section)))
7286 	continue;
7287 
7288       /* Get the name of the symbol.  */
7289       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
7290 					      isym->st_name);
7291       if (name == NULL)
7292 	return FALSE;
7293 
7294       /* See if we are discarding symbols with this name.  */
7295       if ((finfo->info->strip == strip_some
7296 	   && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
7297 	       == NULL))
7298 	  || (((finfo->info->discard == discard_sec_merge
7299 		&& (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
7300 	       || finfo->info->discard == discard_l)
7301 	      && bfd_is_local_label_name (input_bfd, name)))
7302 	continue;
7303 
7304       /* If we get here, we are going to output this symbol.  */
7305 
7306       osym = *isym;
7307 
7308       /* Adjust the section index for the output file.  */
7309       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
7310 							 isec->output_section);
7311       if (osym.st_shndx == SHN_BAD)
7312 	return FALSE;
7313 
7314       *pindex = bfd_get_symcount (output_bfd);
7315 
7316       /* ELF symbols in relocatable files are section relative, but
7317 	 in executable files they are virtual addresses.  Note that
7318 	 this code assumes that all ELF sections have an associated
7319 	 BFD section with a reasonable value for output_offset; below
7320 	 we assume that they also have a reasonable value for
7321 	 output_section.  Any special sections must be set up to meet
7322 	 these requirements.  */
7323       osym.st_value += isec->output_offset;
7324       if (! finfo->info->relocatable)
7325 	{
7326 	  osym.st_value += isec->output_section->vma;
7327 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
7328 	    {
7329 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
7330 	      BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
7331 	      osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
7332 	    }
7333 	}
7334 
7335       if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
7336 	return FALSE;
7337     }
7338 
7339   /* Relocate the contents of each section.  */
7340   sym_hashes = elf_sym_hashes (input_bfd);
7341   for (o = input_bfd->sections; o != NULL; o = o->next)
7342     {
7343       bfd_byte *contents;
7344 
7345       if (! o->linker_mark)
7346 	{
7347 	  /* This section was omitted from the link.  */
7348 	  continue;
7349 	}
7350 
7351       if ((o->flags & SEC_HAS_CONTENTS) == 0
7352 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
7353 	continue;
7354 
7355       if ((o->flags & SEC_LINKER_CREATED) != 0)
7356 	{
7357 	  /* Section was created by _bfd_elf_link_create_dynamic_sections
7358 	     or somesuch.  */
7359 	  continue;
7360 	}
7361 
7362       /* Get the contents of the section.  They have been cached by a
7363 	 relaxation routine.  Note that o is a section in an input
7364 	 file, so the contents field will not have been set by any of
7365 	 the routines which work on output files.  */
7366       if (elf_section_data (o)->this_hdr.contents != NULL)
7367 	contents = elf_section_data (o)->this_hdr.contents;
7368       else
7369 	{
7370 	  bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
7371 
7372 	  contents = finfo->contents;
7373 	  if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
7374 	    return FALSE;
7375 	}
7376 
7377       if ((o->flags & SEC_RELOC) != 0)
7378 	{
7379 	  Elf_Internal_Rela *internal_relocs;
7380 	  bfd_vma r_type_mask;
7381 	  int r_sym_shift;
7382 
7383 	  /* Get the swapped relocs.  */
7384 	  internal_relocs
7385 	    = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
7386 					 finfo->internal_relocs, FALSE);
7387 	  if (internal_relocs == NULL
7388 	      && o->reloc_count > 0)
7389 	    return FALSE;
7390 
7391 	  if (bed->s->arch_size == 32)
7392 	    {
7393 	      r_type_mask = 0xff;
7394 	      r_sym_shift = 8;
7395 	    }
7396 	  else
7397 	    {
7398 	      r_type_mask = 0xffffffff;
7399 	      r_sym_shift = 32;
7400 	    }
7401 
7402 	  /* Run through the relocs looking for any against symbols
7403 	     from discarded sections and section symbols from
7404 	     removed link-once sections.  Complain about relocs
7405 	     against discarded sections.  Zero relocs against removed
7406 	     link-once sections.  */
7407 	  if (!elf_section_ignore_discarded_relocs (o))
7408 	    {
7409 	      Elf_Internal_Rela *rel, *relend;
7410 	      unsigned int action = (*bed->action_discarded) (o);
7411 
7412 	      rel = internal_relocs;
7413 	      relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7414 	      for ( ; rel < relend; rel++)
7415 		{
7416 		  unsigned long r_symndx = rel->r_info >> r_sym_shift;
7417 		  asection **ps, *sec;
7418 		  struct elf_link_hash_entry *h = NULL;
7419 		  const char *sym_name;
7420 
7421 		  if (r_symndx == STN_UNDEF)
7422 		    continue;
7423 
7424 		  if (r_symndx >= locsymcount
7425 		      || (elf_bad_symtab (input_bfd)
7426 			  && finfo->sections[r_symndx] == NULL))
7427 		    {
7428 		      h = sym_hashes[r_symndx - extsymoff];
7429 
7430 		      /* Badly formatted input files can contain relocs that
7431 			 reference non-existant symbols.  Check here so that
7432 			 we do not seg fault.  */
7433 		      if (h == NULL)
7434 			{
7435 			  char buffer [32];
7436 
7437 			  sprintf_vma (buffer, rel->r_info);
7438 			  (*_bfd_error_handler)
7439 			    (_("error: %B contains a reloc (0x%s) for section %A "
7440 			       "that references a non-existent global symbol"),
7441 			     input_bfd, o, buffer);
7442 			  bfd_set_error (bfd_error_bad_value);
7443 			  return FALSE;
7444 			}
7445 
7446 		      while (h->root.type == bfd_link_hash_indirect
7447 			     || h->root.type == bfd_link_hash_warning)
7448 			h = (struct elf_link_hash_entry *) h->root.u.i.link;
7449 
7450 		      if (h->root.type != bfd_link_hash_defined
7451 			  && h->root.type != bfd_link_hash_defweak)
7452 			continue;
7453 
7454 		      ps = &h->root.u.def.section;
7455 		      sym_name = h->root.root.string;
7456 		    }
7457 		  else
7458 		    {
7459 		      Elf_Internal_Sym *sym = isymbuf + r_symndx;
7460 		      ps = &finfo->sections[r_symndx];
7461 		      sym_name = bfd_elf_sym_name (input_bfd,
7462 						   symtab_hdr,
7463 						   sym, *ps);
7464 		    }
7465 
7466 		  /* Complain if the definition comes from a
7467 		     discarded section.  */
7468 		  if ((sec = *ps) != NULL && elf_discarded_section (sec))
7469 		    {
7470 		      BFD_ASSERT (r_symndx != 0);
7471 		      if (action & COMPLAIN)
7472 			(*finfo->info->callbacks->einfo)
7473 			  (_("%X`%s' referenced in section `%A' of %B: "
7474 			     "defined in discarded section `%A' of %B\n"),
7475 			   sym_name, o, input_bfd, sec, sec->owner);
7476 
7477 		      /* Try to do the best we can to support buggy old
7478 			 versions of gcc.  Pretend that the symbol is
7479 			 really defined in the kept linkonce section.
7480 			 FIXME: This is quite broken.  Modifying the
7481 			 symbol here means we will be changing all later
7482 			 uses of the symbol, not just in this section.  */
7483 		      if (action & PRETEND)
7484 			{
7485 			  asection *kept;
7486 
7487 			  kept = _bfd_elf_check_kept_section (sec,
7488 							      finfo->info);
7489 			  if (kept != NULL)
7490 			    {
7491 			      *ps = kept;
7492 			      continue;
7493 			    }
7494 			}
7495 
7496 		      /* Remove the symbol reference from the reloc, but
7497 			 don't kill the reloc completely.  This is so that
7498 			 a zero value will be written into the section,
7499 			 which may have non-zero contents put there by the
7500 			 assembler.  Zero in things like an eh_frame fde
7501 			 pc_begin allows stack unwinders to recognize the
7502 			 fde as bogus.  */
7503 		      rel->r_info &= r_type_mask;
7504 		      rel->r_addend = 0;
7505 		    }
7506 		}
7507 	    }
7508 
7509 	  /* Relocate the section by invoking a back end routine.
7510 
7511 	     The back end routine is responsible for adjusting the
7512 	     section contents as necessary, and (if using Rela relocs
7513 	     and generating a relocatable output file) adjusting the
7514 	     reloc addend as necessary.
7515 
7516 	     The back end routine does not have to worry about setting
7517 	     the reloc address or the reloc symbol index.
7518 
7519 	     The back end routine is given a pointer to the swapped in
7520 	     internal symbols, and can access the hash table entries
7521 	     for the external symbols via elf_sym_hashes (input_bfd).
7522 
7523 	     When generating relocatable output, the back end routine
7524 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
7525 	     output symbol is going to be a section symbol
7526 	     corresponding to the output section, which will require
7527 	     the addend to be adjusted.  */
7528 
7529 	  if (! (*relocate_section) (output_bfd, finfo->info,
7530 				     input_bfd, o, contents,
7531 				     internal_relocs,
7532 				     isymbuf,
7533 				     finfo->sections))
7534 	    return FALSE;
7535 
7536 	  if (emit_relocs)
7537 	    {
7538 	      Elf_Internal_Rela *irela;
7539 	      Elf_Internal_Rela *irelaend;
7540 	      bfd_vma last_offset;
7541 	      struct elf_link_hash_entry **rel_hash;
7542 	      struct elf_link_hash_entry **rel_hash_list;
7543 	      Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7544 	      unsigned int next_erel;
7545 	      bfd_boolean rela_normal;
7546 
7547 	      input_rel_hdr = &elf_section_data (o)->rel_hdr;
7548 	      rela_normal = (bed->rela_normal
7549 			     && (input_rel_hdr->sh_entsize
7550 				 == bed->s->sizeof_rela));
7551 
7552 	      /* Adjust the reloc addresses and symbol indices.  */
7553 
7554 	      irela = internal_relocs;
7555 	      irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7556 	      rel_hash = (elf_section_data (o->output_section)->rel_hashes
7557 			  + elf_section_data (o->output_section)->rel_count
7558 			  + elf_section_data (o->output_section)->rel_count2);
7559 	      rel_hash_list = rel_hash;
7560 	      last_offset = o->output_offset;
7561 	      if (!finfo->info->relocatable)
7562 		last_offset += o->output_section->vma;
7563 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7564 		{
7565 		  unsigned long r_symndx;
7566 		  asection *sec;
7567 		  Elf_Internal_Sym sym;
7568 
7569 		  if (next_erel == bed->s->int_rels_per_ext_rel)
7570 		    {
7571 		      rel_hash++;
7572 		      next_erel = 0;
7573 		    }
7574 
7575 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
7576 							     finfo->info, o,
7577 							     irela->r_offset);
7578 		  if (irela->r_offset >= (bfd_vma) -2)
7579 		    {
7580 		      /* This is a reloc for a deleted entry or somesuch.
7581 			 Turn it into an R_*_NONE reloc, at the same
7582 			 offset as the last reloc.  elf_eh_frame.c and
7583 			 elf_bfd_discard_info rely on reloc offsets
7584 			 being ordered.  */
7585 		      irela->r_offset = last_offset;
7586 		      irela->r_info = 0;
7587 		      irela->r_addend = 0;
7588 		      continue;
7589 		    }
7590 
7591 		  irela->r_offset += o->output_offset;
7592 
7593 		  /* Relocs in an executable have to be virtual addresses.  */
7594 		  if (!finfo->info->relocatable)
7595 		    irela->r_offset += o->output_section->vma;
7596 
7597 		  last_offset = irela->r_offset;
7598 
7599 		  r_symndx = irela->r_info >> r_sym_shift;
7600 		  if (r_symndx == STN_UNDEF)
7601 		    continue;
7602 
7603 		  if (r_symndx >= locsymcount
7604 		      || (elf_bad_symtab (input_bfd)
7605 			  && finfo->sections[r_symndx] == NULL))
7606 		    {
7607 		      struct elf_link_hash_entry *rh;
7608 		      unsigned long indx;
7609 
7610 		      /* This is a reloc against a global symbol.  We
7611 			 have not yet output all the local symbols, so
7612 			 we do not know the symbol index of any global
7613 			 symbol.  We set the rel_hash entry for this
7614 			 reloc to point to the global hash table entry
7615 			 for this symbol.  The symbol index is then
7616 			 set at the end of bfd_elf_final_link.  */
7617 		      indx = r_symndx - extsymoff;
7618 		      rh = elf_sym_hashes (input_bfd)[indx];
7619 		      while (rh->root.type == bfd_link_hash_indirect
7620 			     || rh->root.type == bfd_link_hash_warning)
7621 			rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7622 
7623 		      /* Setting the index to -2 tells
7624 			 elf_link_output_extsym that this symbol is
7625 			 used by a reloc.  */
7626 		      BFD_ASSERT (rh->indx < 0);
7627 		      rh->indx = -2;
7628 
7629 		      *rel_hash = rh;
7630 
7631 		      continue;
7632 		    }
7633 
7634 		  /* This is a reloc against a local symbol.  */
7635 
7636 		  *rel_hash = NULL;
7637 		  sym = isymbuf[r_symndx];
7638 		  sec = finfo->sections[r_symndx];
7639 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7640 		    {
7641 		      /* I suppose the backend ought to fill in the
7642 			 section of any STT_SECTION symbol against a
7643 			 processor specific section.  */
7644 		      r_symndx = 0;
7645 		      if (bfd_is_abs_section (sec))
7646 			;
7647 		      else if (sec == NULL || sec->owner == NULL)
7648 			{
7649 			  bfd_set_error (bfd_error_bad_value);
7650 			  return FALSE;
7651 			}
7652 		      else
7653 			{
7654 			  asection *osec = sec->output_section;
7655 
7656 			  /* If we have discarded a section, the output
7657 			     section will be the absolute section.  In
7658 			     case of discarded link-once and discarded
7659 			     SEC_MERGE sections, use the kept section.  */
7660 			  if (bfd_is_abs_section (osec)
7661 			      && sec->kept_section != NULL
7662 			      && sec->kept_section->output_section != NULL)
7663 			    {
7664 			      osec = sec->kept_section->output_section;
7665 			      irela->r_addend -= osec->vma;
7666 			    }
7667 
7668 			  if (!bfd_is_abs_section (osec))
7669 			    {
7670 			      r_symndx = osec->target_index;
7671 			      BFD_ASSERT (r_symndx != 0);
7672 			    }
7673 			}
7674 
7675 		      /* Adjust the addend according to where the
7676 			 section winds up in the output section.  */
7677 		      if (rela_normal)
7678 			irela->r_addend += sec->output_offset;
7679 		    }
7680 		  else
7681 		    {
7682 		      if (finfo->indices[r_symndx] == -1)
7683 			{
7684 			  unsigned long shlink;
7685 			  const char *name;
7686 			  asection *osec;
7687 
7688 			  if (finfo->info->strip == strip_all)
7689 			    {
7690 			      /* You can't do ld -r -s.  */
7691 			      bfd_set_error (bfd_error_invalid_operation);
7692 			      return FALSE;
7693 			    }
7694 
7695 			  /* This symbol was skipped earlier, but
7696 			     since it is needed by a reloc, we
7697 			     must output it now.  */
7698 			  shlink = symtab_hdr->sh_link;
7699 			  name = (bfd_elf_string_from_elf_section
7700 				  (input_bfd, shlink, sym.st_name));
7701 			  if (name == NULL)
7702 			    return FALSE;
7703 
7704 			  osec = sec->output_section;
7705 			  sym.st_shndx =
7706 			    _bfd_elf_section_from_bfd_section (output_bfd,
7707 							       osec);
7708 			  if (sym.st_shndx == SHN_BAD)
7709 			    return FALSE;
7710 
7711 			  sym.st_value += sec->output_offset;
7712 			  if (! finfo->info->relocatable)
7713 			    {
7714 			      sym.st_value += osec->vma;
7715 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7716 				{
7717 				  /* STT_TLS symbols are relative to PT_TLS
7718 				     segment base.  */
7719 				  BFD_ASSERT (elf_hash_table (finfo->info)
7720 					      ->tls_sec != NULL);
7721 				  sym.st_value -= (elf_hash_table (finfo->info)
7722 						   ->tls_sec->vma);
7723 				}
7724 			    }
7725 
7726 			  finfo->indices[r_symndx]
7727 			    = bfd_get_symcount (output_bfd);
7728 
7729 			  if (! elf_link_output_sym (finfo, name, &sym, sec,
7730 						     NULL))
7731 			    return FALSE;
7732 			}
7733 
7734 		      r_symndx = finfo->indices[r_symndx];
7735 		    }
7736 
7737 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7738 				   | (irela->r_info & r_type_mask));
7739 		}
7740 
7741 	      /* Swap out the relocs.  */
7742 	      if (input_rel_hdr->sh_size != 0
7743 		  && !bed->elf_backend_emit_relocs (output_bfd, o,
7744 						    input_rel_hdr,
7745 						    internal_relocs,
7746 						    rel_hash_list))
7747 		return FALSE;
7748 
7749 	      input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7750 	      if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7751 		{
7752 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7753 				      * bed->s->int_rels_per_ext_rel);
7754 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7755 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
7756 						     input_rel_hdr2,
7757 						     internal_relocs,
7758 						     rel_hash_list))
7759 		    return FALSE;
7760 		}
7761 	    }
7762 	}
7763 
7764       /* Write out the modified section contents.  */
7765       if (bed->elf_backend_write_section
7766 	  && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7767 	{
7768 	  /* Section written out.  */
7769 	}
7770       else switch (o->sec_info_type)
7771 	{
7772 	case ELF_INFO_TYPE_STABS:
7773 	  if (! (_bfd_write_section_stabs
7774 		 (output_bfd,
7775 		  &elf_hash_table (finfo->info)->stab_info,
7776 		  o, &elf_section_data (o)->sec_info, contents)))
7777 	    return FALSE;
7778 	  break;
7779 	case ELF_INFO_TYPE_MERGE:
7780 	  if (! _bfd_write_merged_section (output_bfd, o,
7781 					   elf_section_data (o)->sec_info))
7782 	    return FALSE;
7783 	  break;
7784 	case ELF_INFO_TYPE_EH_FRAME:
7785 	  {
7786 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7787 						   o, contents))
7788 	      return FALSE;
7789 	  }
7790 	  break;
7791 	default:
7792 	  {
7793 	    if (! (o->flags & SEC_EXCLUDE)
7794 		&& ! bfd_set_section_contents (output_bfd, o->output_section,
7795 					       contents,
7796 					       (file_ptr) o->output_offset,
7797 					       o->size))
7798 	      return FALSE;
7799 	  }
7800 	  break;
7801 	}
7802     }
7803 
7804   return TRUE;
7805 }
7806 
7807 /* Generate a reloc when linking an ELF file.  This is a reloc
7808    requested by the linker, and does not come from any input file.  This
7809    is used to build constructor and destructor tables when linking
7810    with -Ur.  */
7811 
7812 static bfd_boolean
7813 elf_reloc_link_order (bfd *output_bfd,
7814 		      struct bfd_link_info *info,
7815 		      asection *output_section,
7816 		      struct bfd_link_order *link_order)
7817 {
7818   reloc_howto_type *howto;
7819   long indx;
7820   bfd_vma offset;
7821   bfd_vma addend;
7822   struct elf_link_hash_entry **rel_hash_ptr;
7823   Elf_Internal_Shdr *rel_hdr;
7824   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7825   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7826   bfd_byte *erel;
7827   unsigned int i;
7828 
7829   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7830   if (howto == NULL)
7831     {
7832       bfd_set_error (bfd_error_bad_value);
7833       return FALSE;
7834     }
7835 
7836   addend = link_order->u.reloc.p->addend;
7837 
7838   /* Figure out the symbol index.  */
7839   rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7840 		  + elf_section_data (output_section)->rel_count
7841 		  + elf_section_data (output_section)->rel_count2);
7842   if (link_order->type == bfd_section_reloc_link_order)
7843     {
7844       indx = link_order->u.reloc.p->u.section->target_index;
7845       BFD_ASSERT (indx != 0);
7846       *rel_hash_ptr = NULL;
7847     }
7848   else
7849     {
7850       struct elf_link_hash_entry *h;
7851 
7852       /* Treat a reloc against a defined symbol as though it were
7853 	 actually against the section.  */
7854       h = ((struct elf_link_hash_entry *)
7855 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
7856 					 link_order->u.reloc.p->u.name,
7857 					 FALSE, FALSE, TRUE));
7858       if (h != NULL
7859 	  && (h->root.type == bfd_link_hash_defined
7860 	      || h->root.type == bfd_link_hash_defweak))
7861 	{
7862 	  asection *section;
7863 
7864 	  section = h->root.u.def.section;
7865 	  indx = section->output_section->target_index;
7866 	  *rel_hash_ptr = NULL;
7867 	  /* It seems that we ought to add the symbol value to the
7868 	     addend here, but in practice it has already been added
7869 	     because it was passed to constructor_callback.  */
7870 	  addend += section->output_section->vma + section->output_offset;
7871 	}
7872       else if (h != NULL)
7873 	{
7874 	  /* Setting the index to -2 tells elf_link_output_extsym that
7875 	     this symbol is used by a reloc.  */
7876 	  h->indx = -2;
7877 	  *rel_hash_ptr = h;
7878 	  indx = 0;
7879 	}
7880       else
7881 	{
7882 	  if (! ((*info->callbacks->unattached_reloc)
7883 		 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7884 	    return FALSE;
7885 	  indx = 0;
7886 	}
7887     }
7888 
7889   /* If this is an inplace reloc, we must write the addend into the
7890      object file.  */
7891   if (howto->partial_inplace && addend != 0)
7892     {
7893       bfd_size_type size;
7894       bfd_reloc_status_type rstat;
7895       bfd_byte *buf;
7896       bfd_boolean ok;
7897       const char *sym_name;
7898 
7899       size = bfd_get_reloc_size (howto);
7900       buf = bfd_zmalloc (size);
7901       if (buf == NULL)
7902 	return FALSE;
7903       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7904       switch (rstat)
7905 	{
7906 	case bfd_reloc_ok:
7907 	  break;
7908 
7909 	default:
7910 	case bfd_reloc_outofrange:
7911 	  abort ();
7912 
7913 	case bfd_reloc_overflow:
7914 	  if (link_order->type == bfd_section_reloc_link_order)
7915 	    sym_name = bfd_section_name (output_bfd,
7916 					 link_order->u.reloc.p->u.section);
7917 	  else
7918 	    sym_name = link_order->u.reloc.p->u.name;
7919 	  if (! ((*info->callbacks->reloc_overflow)
7920 		 (info, NULL, sym_name, howto->name, addend, NULL,
7921 		  NULL, (bfd_vma) 0)))
7922 	    {
7923 	      free (buf);
7924 	      return FALSE;
7925 	    }
7926 	  break;
7927 	}
7928       ok = bfd_set_section_contents (output_bfd, output_section, buf,
7929 				     link_order->offset, size);
7930       free (buf);
7931       if (! ok)
7932 	return FALSE;
7933     }
7934 
7935   /* The address of a reloc is relative to the section in a
7936      relocatable file, and is a virtual address in an executable
7937      file.  */
7938   offset = link_order->offset;
7939   if (! info->relocatable)
7940     offset += output_section->vma;
7941 
7942   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7943     {
7944       irel[i].r_offset = offset;
7945       irel[i].r_info = 0;
7946       irel[i].r_addend = 0;
7947     }
7948   if (bed->s->arch_size == 32)
7949     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7950   else
7951     irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7952 
7953   rel_hdr = &elf_section_data (output_section)->rel_hdr;
7954   erel = rel_hdr->contents;
7955   if (rel_hdr->sh_type == SHT_REL)
7956     {
7957       erel += (elf_section_data (output_section)->rel_count
7958 	       * bed->s->sizeof_rel);
7959       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7960     }
7961   else
7962     {
7963       irel[0].r_addend = addend;
7964       erel += (elf_section_data (output_section)->rel_count
7965 	       * bed->s->sizeof_rela);
7966       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7967     }
7968 
7969   ++elf_section_data (output_section)->rel_count;
7970 
7971   return TRUE;
7972 }
7973 
7974 
7975 /* Get the output vma of the section pointed to by the sh_link field.  */
7976 
7977 static bfd_vma
7978 elf_get_linked_section_vma (struct bfd_link_order *p)
7979 {
7980   Elf_Internal_Shdr **elf_shdrp;
7981   asection *s;
7982   int elfsec;
7983 
7984   s = p->u.indirect.section;
7985   elf_shdrp = elf_elfsections (s->owner);
7986   elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7987   elfsec = elf_shdrp[elfsec]->sh_link;
7988   /* PR 290:
7989      The Intel C compiler generates SHT_IA_64_UNWIND with
7990      SHF_LINK_ORDER.  But it doesn't set the sh_link or
7991      sh_info fields.  Hence we could get the situation
7992      where elfsec is 0.  */
7993   if (elfsec == 0)
7994     {
7995       const struct elf_backend_data *bed
7996 	= get_elf_backend_data (s->owner);
7997       if (bed->link_order_error_handler)
7998 	bed->link_order_error_handler
7999 	  (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
8000       return 0;
8001     }
8002   else
8003     {
8004       s = elf_shdrp[elfsec]->bfd_section;
8005       return s->output_section->vma + s->output_offset;
8006     }
8007 }
8008 
8009 
8010 /* Compare two sections based on the locations of the sections they are
8011    linked to.  Used by elf_fixup_link_order.  */
8012 
8013 static int
8014 compare_link_order (const void * a, const void * b)
8015 {
8016   bfd_vma apos;
8017   bfd_vma bpos;
8018 
8019   apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
8020   bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
8021   if (apos < bpos)
8022     return -1;
8023   return apos > bpos;
8024 }
8025 
8026 
8027 /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
8028    order as their linked sections.  Returns false if this could not be done
8029    because an output section includes both ordered and unordered
8030    sections.  Ideally we'd do this in the linker proper.  */
8031 
8032 static bfd_boolean
8033 elf_fixup_link_order (bfd *abfd, asection *o)
8034 {
8035   int seen_linkorder;
8036   int seen_other;
8037   int n;
8038   struct bfd_link_order *p;
8039   bfd *sub;
8040   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8041   unsigned elfsec;
8042   struct bfd_link_order **sections;
8043   asection *s, *other_sec, *linkorder_sec;
8044   bfd_vma offset;
8045 
8046   other_sec = NULL;
8047   linkorder_sec = NULL;
8048   seen_other = 0;
8049   seen_linkorder = 0;
8050   for (p = o->map_head.link_order; p != NULL; p = p->next)
8051     {
8052       if (p->type == bfd_indirect_link_order)
8053 	{
8054 	  s = p->u.indirect.section;
8055 	  sub = s->owner;
8056 	  if (bfd_get_flavour (sub) == bfd_target_elf_flavour
8057 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
8058 	      && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
8059 	      && elfsec < elf_numsections (sub)
8060 	      && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
8061 	    {
8062 	      seen_linkorder++;
8063 	      linkorder_sec = s;
8064 	    }
8065 	  else
8066 	    {
8067 	      seen_other++;
8068 	      other_sec = s;
8069 	    }
8070 	}
8071       else
8072 	seen_other++;
8073 
8074       if (seen_other && seen_linkorder)
8075 	{
8076 	  if (other_sec && linkorder_sec)
8077 	    (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
8078 				   o, linkorder_sec,
8079 				   linkorder_sec->owner, other_sec,
8080 				   other_sec->owner);
8081 	  else
8082 	    (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
8083 				   o);
8084 	  bfd_set_error (bfd_error_bad_value);
8085 	  return FALSE;
8086 	}
8087     }
8088 
8089   if (!seen_linkorder)
8090     return TRUE;
8091 
8092   sections = (struct bfd_link_order **)
8093     xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
8094   seen_linkorder = 0;
8095 
8096   for (p = o->map_head.link_order; p != NULL; p = p->next)
8097     {
8098       sections[seen_linkorder++] = p;
8099     }
8100   /* Sort the input sections in the order of their linked section.  */
8101   qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
8102 	 compare_link_order);
8103 
8104   /* Change the offsets of the sections.  */
8105   offset = 0;
8106   for (n = 0; n < seen_linkorder; n++)
8107     {
8108       s = sections[n]->u.indirect.section;
8109       offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
8110       s->output_offset = offset;
8111       sections[n]->offset = offset;
8112       offset += sections[n]->size;
8113     }
8114 
8115   return TRUE;
8116 }
8117 
8118 
8119 /* Do the final step of an ELF link.  */
8120 
8121 bfd_boolean
8122 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
8123 {
8124   bfd_boolean dynamic;
8125   bfd_boolean emit_relocs;
8126   bfd *dynobj;
8127   struct elf_final_link_info finfo;
8128   register asection *o;
8129   register struct bfd_link_order *p;
8130   register bfd *sub;
8131   bfd_size_type max_contents_size;
8132   bfd_size_type max_external_reloc_size;
8133   bfd_size_type max_internal_reloc_count;
8134   bfd_size_type max_sym_count;
8135   bfd_size_type max_sym_shndx_count;
8136   file_ptr off;
8137   Elf_Internal_Sym elfsym;
8138   unsigned int i;
8139   Elf_Internal_Shdr *symtab_hdr;
8140   Elf_Internal_Shdr *symtab_shndx_hdr;
8141   Elf_Internal_Shdr *symstrtab_hdr;
8142   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8143   struct elf_outext_info eoinfo;
8144   bfd_boolean merged;
8145   size_t relativecount = 0;
8146   asection *reldyn = 0;
8147   bfd_size_type amt;
8148 
8149   if (! is_elf_hash_table (info->hash))
8150     return FALSE;
8151 
8152   if (info->shared)
8153     abfd->flags |= DYNAMIC;
8154 
8155   dynamic = elf_hash_table (info)->dynamic_sections_created;
8156   dynobj = elf_hash_table (info)->dynobj;
8157 
8158   emit_relocs = (info->relocatable
8159 		 || info->emitrelocations);
8160 
8161   finfo.info = info;
8162   finfo.output_bfd = abfd;
8163   finfo.symstrtab = _bfd_elf_stringtab_init ();
8164   if (finfo.symstrtab == NULL)
8165     return FALSE;
8166 
8167   if (! dynamic)
8168     {
8169       finfo.dynsym_sec = NULL;
8170       finfo.hash_sec = NULL;
8171       finfo.symver_sec = NULL;
8172     }
8173   else
8174     {
8175       finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
8176       finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
8177       BFD_ASSERT (finfo.dynsym_sec != NULL);
8178       finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
8179       /* Note that it is OK if symver_sec is NULL.  */
8180     }
8181 
8182   finfo.contents = NULL;
8183   finfo.external_relocs = NULL;
8184   finfo.internal_relocs = NULL;
8185   finfo.external_syms = NULL;
8186   finfo.locsym_shndx = NULL;
8187   finfo.internal_syms = NULL;
8188   finfo.indices = NULL;
8189   finfo.sections = NULL;
8190   finfo.symbuf = NULL;
8191   finfo.symshndxbuf = NULL;
8192   finfo.symbuf_count = 0;
8193   finfo.shndxbuf_size = 0;
8194 
8195   /* Count up the number of relocations we will output for each output
8196      section, so that we know the sizes of the reloc sections.  We
8197      also figure out some maximum sizes.  */
8198   max_contents_size = 0;
8199   max_external_reloc_size = 0;
8200   max_internal_reloc_count = 0;
8201   max_sym_count = 0;
8202   max_sym_shndx_count = 0;
8203   merged = FALSE;
8204   for (o = abfd->sections; o != NULL; o = o->next)
8205     {
8206       struct bfd_elf_section_data *esdo = elf_section_data (o);
8207       o->reloc_count = 0;
8208 
8209       for (p = o->map_head.link_order; p != NULL; p = p->next)
8210 	{
8211 	  unsigned int reloc_count = 0;
8212 	  struct bfd_elf_section_data *esdi = NULL;
8213 	  unsigned int *rel_count1;
8214 
8215 	  if (p->type == bfd_section_reloc_link_order
8216 	      || p->type == bfd_symbol_reloc_link_order)
8217 	    reloc_count = 1;
8218 	  else if (p->type == bfd_indirect_link_order)
8219 	    {
8220 	      asection *sec;
8221 
8222 	      sec = p->u.indirect.section;
8223 	      esdi = elf_section_data (sec);
8224 
8225 	      /* Mark all sections which are to be included in the
8226 		 link.  This will normally be every section.  We need
8227 		 to do this so that we can identify any sections which
8228 		 the linker has decided to not include.  */
8229 	      sec->linker_mark = TRUE;
8230 
8231 	      if (sec->flags & SEC_MERGE)
8232 		merged = TRUE;
8233 
8234 	      if (info->relocatable || info->emitrelocations)
8235 		reloc_count = sec->reloc_count;
8236 	      else if (bed->elf_backend_count_relocs)
8237 		{
8238 		  Elf_Internal_Rela * relocs;
8239 
8240 		  relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
8241 						      info->keep_memory);
8242 
8243 		  reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
8244 
8245 		  if (elf_section_data (o)->relocs != relocs)
8246 		    free (relocs);
8247 		}
8248 
8249 	      if (sec->rawsize > max_contents_size)
8250 		max_contents_size = sec->rawsize;
8251 	      if (sec->size > max_contents_size)
8252 		max_contents_size = sec->size;
8253 
8254 	      /* We are interested in just local symbols, not all
8255 		 symbols.  */
8256 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
8257 		  && (sec->owner->flags & DYNAMIC) == 0)
8258 		{
8259 		  size_t sym_count;
8260 
8261 		  if (elf_bad_symtab (sec->owner))
8262 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
8263 				 / bed->s->sizeof_sym);
8264 		  else
8265 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
8266 
8267 		  if (sym_count > max_sym_count)
8268 		    max_sym_count = sym_count;
8269 
8270 		  if (sym_count > max_sym_shndx_count
8271 		      && elf_symtab_shndx (sec->owner) != 0)
8272 		    max_sym_shndx_count = sym_count;
8273 
8274 		  if ((sec->flags & SEC_RELOC) != 0)
8275 		    {
8276 		      size_t ext_size;
8277 
8278 		      ext_size = elf_section_data (sec)->rel_hdr.sh_size;
8279 		      if (ext_size > max_external_reloc_size)
8280 			max_external_reloc_size = ext_size;
8281 		      if (sec->reloc_count > max_internal_reloc_count)
8282 			max_internal_reloc_count = sec->reloc_count;
8283 		    }
8284 		}
8285 	    }
8286 
8287 	  if (reloc_count == 0)
8288 	    continue;
8289 
8290 	  o->reloc_count += reloc_count;
8291 
8292 	  /* MIPS may have a mix of REL and RELA relocs on sections.
8293 	     To support this curious ABI we keep reloc counts in
8294 	     elf_section_data too.  We must be careful to add the
8295 	     relocations from the input section to the right output
8296 	     count.  FIXME: Get rid of one count.  We have
8297 	     o->reloc_count == esdo->rel_count + esdo->rel_count2.  */
8298 	  rel_count1 = &esdo->rel_count;
8299 	  if (esdi != NULL)
8300 	    {
8301 	      bfd_boolean same_size;
8302 	      bfd_size_type entsize1;
8303 
8304 	      entsize1 = esdi->rel_hdr.sh_entsize;
8305 	      BFD_ASSERT (entsize1 == bed->s->sizeof_rel
8306 			  || entsize1 == bed->s->sizeof_rela);
8307 	      same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
8308 
8309 	      if (!same_size)
8310 		rel_count1 = &esdo->rel_count2;
8311 
8312 	      if (esdi->rel_hdr2 != NULL)
8313 		{
8314 		  bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
8315 		  unsigned int alt_count;
8316 		  unsigned int *rel_count2;
8317 
8318 		  BFD_ASSERT (entsize2 != entsize1
8319 			      && (entsize2 == bed->s->sizeof_rel
8320 				  || entsize2 == bed->s->sizeof_rela));
8321 
8322 		  rel_count2 = &esdo->rel_count2;
8323 		  if (!same_size)
8324 		    rel_count2 = &esdo->rel_count;
8325 
8326 		  /* The following is probably too simplistic if the
8327 		     backend counts output relocs unusually.  */
8328 		  BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
8329 		  alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
8330 		  *rel_count2 += alt_count;
8331 		  reloc_count -= alt_count;
8332 		}
8333 	    }
8334 	  *rel_count1 += reloc_count;
8335 	}
8336 
8337       if (o->reloc_count > 0)
8338 	o->flags |= SEC_RELOC;
8339       else
8340 	{
8341 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
8342 	     set it (this is probably a bug) and if it is set
8343 	     assign_section_numbers will create a reloc section.  */
8344 	  o->flags &=~ SEC_RELOC;
8345 	}
8346 
8347       /* If the SEC_ALLOC flag is not set, force the section VMA to
8348 	 zero.  This is done in elf_fake_sections as well, but forcing
8349 	 the VMA to 0 here will ensure that relocs against these
8350 	 sections are handled correctly.  */
8351       if ((o->flags & SEC_ALLOC) == 0
8352 	  && ! o->user_set_vma)
8353 	o->vma = 0;
8354     }
8355 
8356   if (! info->relocatable && merged)
8357     elf_link_hash_traverse (elf_hash_table (info),
8358 			    _bfd_elf_link_sec_merge_syms, abfd);
8359 
8360   /* Figure out the file positions for everything but the symbol table
8361      and the relocs.  We set symcount to force assign_section_numbers
8362      to create a symbol table.  */
8363   bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
8364   BFD_ASSERT (! abfd->output_has_begun);
8365   if (! _bfd_elf_compute_section_file_positions (abfd, info))
8366     goto error_return;
8367 
8368   /* Set sizes, and assign file positions for reloc sections.  */
8369   for (o = abfd->sections; o != NULL; o = o->next)
8370     {
8371       if ((o->flags & SEC_RELOC) != 0)
8372 	{
8373 	  if (!(_bfd_elf_link_size_reloc_section
8374 		(abfd, &elf_section_data (o)->rel_hdr, o)))
8375 	    goto error_return;
8376 
8377 	  if (elf_section_data (o)->rel_hdr2
8378 	      && !(_bfd_elf_link_size_reloc_section
8379 		   (abfd, elf_section_data (o)->rel_hdr2, o)))
8380 	    goto error_return;
8381 	}
8382 
8383       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8384 	 to count upwards while actually outputting the relocations.  */
8385       elf_section_data (o)->rel_count = 0;
8386       elf_section_data (o)->rel_count2 = 0;
8387     }
8388 
8389   _bfd_elf_assign_file_positions_for_relocs (abfd);
8390 
8391   /* We have now assigned file positions for all the sections except
8392      .symtab and .strtab.  We start the .symtab section at the current
8393      file position, and write directly to it.  We build the .strtab
8394      section in memory.  */
8395   bfd_get_symcount (abfd) = 0;
8396   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8397   /* sh_name is set in prep_headers.  */
8398   symtab_hdr->sh_type = SHT_SYMTAB;
8399   /* sh_flags, sh_addr and sh_size all start off zero.  */
8400   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8401   /* sh_link is set in assign_section_numbers.  */
8402   /* sh_info is set below.  */
8403   /* sh_offset is set just below.  */
8404   symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8405 
8406   off = elf_tdata (abfd)->next_file_pos;
8407   off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8408 
8409   /* Note that at this point elf_tdata (abfd)->next_file_pos is
8410      incorrect.  We do not yet know the size of the .symtab section.
8411      We correct next_file_pos below, after we do know the size.  */
8412 
8413   /* Allocate a buffer to hold swapped out symbols.  This is to avoid
8414      continuously seeking to the right position in the file.  */
8415   if (! info->keep_memory || max_sym_count < 20)
8416     finfo.symbuf_size = 20;
8417   else
8418     finfo.symbuf_size = max_sym_count;
8419   amt = finfo.symbuf_size;
8420   amt *= bed->s->sizeof_sym;
8421   finfo.symbuf = bfd_malloc (amt);
8422   if (finfo.symbuf == NULL)
8423     goto error_return;
8424   if (elf_numsections (abfd) > SHN_LORESERVE)
8425     {
8426       /* Wild guess at number of output symbols.  realloc'd as needed.  */
8427       amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8428       finfo.shndxbuf_size = amt;
8429       amt *= sizeof (Elf_External_Sym_Shndx);
8430       finfo.symshndxbuf = bfd_zmalloc (amt);
8431       if (finfo.symshndxbuf == NULL)
8432 	goto error_return;
8433     }
8434 
8435   /* Start writing out the symbol table.  The first symbol is always a
8436      dummy symbol.  */
8437   if (info->strip != strip_all
8438       || emit_relocs)
8439     {
8440       elfsym.st_value = 0;
8441       elfsym.st_size = 0;
8442       elfsym.st_info = 0;
8443       elfsym.st_other = 0;
8444       elfsym.st_shndx = SHN_UNDEF;
8445       if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8446 				 NULL))
8447 	goto error_return;
8448     }
8449 
8450   /* Output a symbol for each section.  We output these even if we are
8451      discarding local symbols, since they are used for relocs.  These
8452      symbols have no names.  We store the index of each one in the
8453      index field of the section, so that we can find it again when
8454      outputting relocs.  */
8455   if (info->strip != strip_all
8456       || emit_relocs)
8457     {
8458       elfsym.st_size = 0;
8459       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8460       elfsym.st_other = 0;
8461       for (i = 1; i < elf_numsections (abfd); i++)
8462 	{
8463 	  o = bfd_section_from_elf_index (abfd, i);
8464 	  if (o != NULL)
8465 	    o->target_index = bfd_get_symcount (abfd);
8466 	  elfsym.st_shndx = i;
8467 	  if (info->relocatable || o == NULL)
8468 	    elfsym.st_value = 0;
8469 	  else
8470 	    elfsym.st_value = o->vma;
8471 	  if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8472 	    goto error_return;
8473 	  if (i == SHN_LORESERVE - 1)
8474 	    i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8475 	}
8476     }
8477 
8478   /* Allocate some memory to hold information read in from the input
8479      files.  */
8480   if (max_contents_size != 0)
8481     {
8482       finfo.contents = bfd_malloc (max_contents_size);
8483       if (finfo.contents == NULL)
8484 	goto error_return;
8485     }
8486 
8487   if (max_external_reloc_size != 0)
8488     {
8489       finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8490       if (finfo.external_relocs == NULL)
8491 	goto error_return;
8492     }
8493 
8494   if (max_internal_reloc_count != 0)
8495     {
8496       amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8497       amt *= sizeof (Elf_Internal_Rela);
8498       finfo.internal_relocs = bfd_malloc (amt);
8499       if (finfo.internal_relocs == NULL)
8500 	goto error_return;
8501     }
8502 
8503   if (max_sym_count != 0)
8504     {
8505       amt = max_sym_count * bed->s->sizeof_sym;
8506       finfo.external_syms = bfd_malloc (amt);
8507       if (finfo.external_syms == NULL)
8508 	goto error_return;
8509 
8510       amt = max_sym_count * sizeof (Elf_Internal_Sym);
8511       finfo.internal_syms = bfd_malloc (amt);
8512       if (finfo.internal_syms == NULL)
8513 	goto error_return;
8514 
8515       amt = max_sym_count * sizeof (long);
8516       finfo.indices = bfd_malloc (amt);
8517       if (finfo.indices == NULL)
8518 	goto error_return;
8519 
8520       amt = max_sym_count * sizeof (asection *);
8521       finfo.sections = bfd_malloc (amt);
8522       if (finfo.sections == NULL)
8523 	goto error_return;
8524     }
8525 
8526   if (max_sym_shndx_count != 0)
8527     {
8528       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8529       finfo.locsym_shndx = bfd_malloc (amt);
8530       if (finfo.locsym_shndx == NULL)
8531 	goto error_return;
8532     }
8533 
8534   if (elf_hash_table (info)->tls_sec)
8535     {
8536       bfd_vma base, end = 0;
8537       asection *sec;
8538 
8539       for (sec = elf_hash_table (info)->tls_sec;
8540 	   sec && (sec->flags & SEC_THREAD_LOCAL);
8541 	   sec = sec->next)
8542 	{
8543 	  bfd_size_type size = sec->size;
8544 
8545 	  if (size == 0
8546 	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
8547 	    {
8548 	      struct bfd_link_order *o = sec->map_tail.link_order;
8549 	      if (o != NULL)
8550 		size = o->offset + o->size;
8551 	    }
8552 	  end = sec->vma + size;
8553 	}
8554       base = elf_hash_table (info)->tls_sec->vma;
8555       end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8556       elf_hash_table (info)->tls_size = end - base;
8557     }
8558 
8559   /* Reorder SHF_LINK_ORDER sections.  */
8560   for (o = abfd->sections; o != NULL; o = o->next)
8561     {
8562       if (!elf_fixup_link_order (abfd, o))
8563 	return FALSE;
8564     }
8565 
8566   /* Since ELF permits relocations to be against local symbols, we
8567      must have the local symbols available when we do the relocations.
8568      Since we would rather only read the local symbols once, and we
8569      would rather not keep them in memory, we handle all the
8570      relocations for a single input file at the same time.
8571 
8572      Unfortunately, there is no way to know the total number of local
8573      symbols until we have seen all of them, and the local symbol
8574      indices precede the global symbol indices.  This means that when
8575      we are generating relocatable output, and we see a reloc against
8576      a global symbol, we can not know the symbol index until we have
8577      finished examining all the local symbols to see which ones we are
8578      going to output.  To deal with this, we keep the relocations in
8579      memory, and don't output them until the end of the link.  This is
8580      an unfortunate waste of memory, but I don't see a good way around
8581      it.  Fortunately, it only happens when performing a relocatable
8582      link, which is not the common case.  FIXME: If keep_memory is set
8583      we could write the relocs out and then read them again; I don't
8584      know how bad the memory loss will be.  */
8585 
8586   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8587     sub->output_has_begun = FALSE;
8588   for (o = abfd->sections; o != NULL; o = o->next)
8589     {
8590       for (p = o->map_head.link_order; p != NULL; p = p->next)
8591 	{
8592 	  if (p->type == bfd_indirect_link_order
8593 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8594 		  == bfd_target_elf_flavour)
8595 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8596 	    {
8597 	      if (! sub->output_has_begun)
8598 		{
8599 		  if (! elf_link_input_bfd (&finfo, sub))
8600 		    goto error_return;
8601 		  sub->output_has_begun = TRUE;
8602 		}
8603 	    }
8604 	  else if (p->type == bfd_section_reloc_link_order
8605 		   || p->type == bfd_symbol_reloc_link_order)
8606 	    {
8607 	      if (! elf_reloc_link_order (abfd, info, o, p))
8608 		goto error_return;
8609 	    }
8610 	  else
8611 	    {
8612 	      if (! _bfd_default_link_order (abfd, info, o, p))
8613 		goto error_return;
8614 	    }
8615 	}
8616     }
8617 
8618   /* Free symbol buffer if needed.  */
8619   if (!info->reduce_memory_overheads)
8620     {
8621       for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8622         {
8623           if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
8624             {
8625               free (elf_tdata (sub)->symbuf);
8626               elf_tdata (sub)->symbuf = NULL;
8627             }
8628         }
8629     }
8630 
8631   /* Output any global symbols that got converted to local in a
8632      version script or due to symbol visibility.  We do this in a
8633      separate step since ELF requires all local symbols to appear
8634      prior to any global symbols.  FIXME: We should only do this if
8635      some global symbols were, in fact, converted to become local.
8636      FIXME: Will this work correctly with the Irix 5 linker?  */
8637   eoinfo.failed = FALSE;
8638   eoinfo.finfo = &finfo;
8639   eoinfo.localsyms = TRUE;
8640   elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8641 			  &eoinfo);
8642   if (eoinfo.failed)
8643     return FALSE;
8644 
8645   /* That wrote out all the local symbols.  Finish up the symbol table
8646      with the global symbols. Even if we want to strip everything we
8647      can, we still need to deal with those global symbols that got
8648      converted to local in a version script.  */
8649 
8650   /* The sh_info field records the index of the first non local symbol.  */
8651   symtab_hdr->sh_info = bfd_get_symcount (abfd);
8652 
8653   if (dynamic
8654       && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8655     {
8656       Elf_Internal_Sym sym;
8657       bfd_byte *dynsym = finfo.dynsym_sec->contents;
8658       long last_local = 0;
8659 
8660       /* Write out the section symbols for the output sections.  */
8661       if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8662 	{
8663 	  asection *s;
8664 
8665 	  sym.st_size = 0;
8666 	  sym.st_name = 0;
8667 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8668 	  sym.st_other = 0;
8669 
8670 	  for (s = abfd->sections; s != NULL; s = s->next)
8671 	    {
8672 	      int indx;
8673 	      bfd_byte *dest;
8674 	      long dynindx;
8675 
8676 	      dynindx = elf_section_data (s)->dynindx;
8677 	      if (dynindx <= 0)
8678 		continue;
8679 	      indx = elf_section_data (s)->this_idx;
8680 	      BFD_ASSERT (indx > 0);
8681 	      sym.st_shndx = indx;
8682 	      if (! check_dynsym (abfd, &sym))
8683 		return FALSE;
8684 	      sym.st_value = s->vma;
8685 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
8686 	      if (last_local < dynindx)
8687 		last_local = dynindx;
8688 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8689 	    }
8690 	}
8691 
8692       /* Write out the local dynsyms.  */
8693       if (elf_hash_table (info)->dynlocal)
8694 	{
8695 	  struct elf_link_local_dynamic_entry *e;
8696 	  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8697 	    {
8698 	      asection *s;
8699 	      bfd_byte *dest;
8700 
8701 	      sym.st_size = e->isym.st_size;
8702 	      sym.st_other = e->isym.st_other;
8703 
8704 	      /* Copy the internal symbol as is.
8705 		 Note that we saved a word of storage and overwrote
8706 		 the original st_name with the dynstr_index.  */
8707 	      sym = e->isym;
8708 
8709 	      if (e->isym.st_shndx != SHN_UNDEF
8710 		  && (e->isym.st_shndx < SHN_LORESERVE
8711 		      || e->isym.st_shndx > SHN_HIRESERVE))
8712 		{
8713 		  s = bfd_section_from_elf_index (e->input_bfd,
8714 						  e->isym.st_shndx);
8715 
8716 		  sym.st_shndx =
8717 		    elf_section_data (s->output_section)->this_idx;
8718 		  if (! check_dynsym (abfd, &sym))
8719 		    return FALSE;
8720 		  sym.st_value = (s->output_section->vma
8721 				  + s->output_offset
8722 				  + e->isym.st_value);
8723 		}
8724 
8725 	      if (last_local < e->dynindx)
8726 		last_local = e->dynindx;
8727 
8728 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8729 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8730 	    }
8731 	}
8732 
8733       elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8734 	last_local + 1;
8735     }
8736 
8737   /* We get the global symbols from the hash table.  */
8738   eoinfo.failed = FALSE;
8739   eoinfo.localsyms = FALSE;
8740   eoinfo.finfo = &finfo;
8741   elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8742 			  &eoinfo);
8743   if (eoinfo.failed)
8744     return FALSE;
8745 
8746   /* If backend needs to output some symbols not present in the hash
8747      table, do it now.  */
8748   if (bed->elf_backend_output_arch_syms)
8749     {
8750       typedef bfd_boolean (*out_sym_func)
8751 	(void *, const char *, Elf_Internal_Sym *, asection *,
8752 	 struct elf_link_hash_entry *);
8753 
8754       if (! ((*bed->elf_backend_output_arch_syms)
8755 	     (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8756 	return FALSE;
8757     }
8758 
8759   /* Flush all symbols to the file.  */
8760   if (! elf_link_flush_output_syms (&finfo, bed))
8761     return FALSE;
8762 
8763   /* Now we know the size of the symtab section.  */
8764   off += symtab_hdr->sh_size;
8765 
8766   symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8767   if (symtab_shndx_hdr->sh_name != 0)
8768     {
8769       symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8770       symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8771       symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8772       amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8773       symtab_shndx_hdr->sh_size = amt;
8774 
8775       off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8776 						       off, TRUE);
8777 
8778       if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8779 	  || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8780 	return FALSE;
8781     }
8782 
8783 
8784   /* Finish up and write out the symbol string table (.strtab)
8785      section.  */
8786   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8787   /* sh_name was set in prep_headers.  */
8788   symstrtab_hdr->sh_type = SHT_STRTAB;
8789   symstrtab_hdr->sh_flags = 0;
8790   symstrtab_hdr->sh_addr = 0;
8791   symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8792   symstrtab_hdr->sh_entsize = 0;
8793   symstrtab_hdr->sh_link = 0;
8794   symstrtab_hdr->sh_info = 0;
8795   /* sh_offset is set just below.  */
8796   symstrtab_hdr->sh_addralign = 1;
8797 
8798   off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8799   elf_tdata (abfd)->next_file_pos = off;
8800 
8801   if (bfd_get_symcount (abfd) > 0)
8802     {
8803       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8804 	  || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8805 	return FALSE;
8806     }
8807 
8808   /* Adjust the relocs to have the correct symbol indices.  */
8809   for (o = abfd->sections; o != NULL; o = o->next)
8810     {
8811       if ((o->flags & SEC_RELOC) == 0)
8812 	continue;
8813 
8814       elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8815 			      elf_section_data (o)->rel_count,
8816 			      elf_section_data (o)->rel_hashes);
8817       if (elf_section_data (o)->rel_hdr2 != NULL)
8818 	elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8819 				elf_section_data (o)->rel_count2,
8820 				(elf_section_data (o)->rel_hashes
8821 				 + elf_section_data (o)->rel_count));
8822 
8823       /* Set the reloc_count field to 0 to prevent write_relocs from
8824 	 trying to swap the relocs out itself.  */
8825       o->reloc_count = 0;
8826     }
8827 
8828   if (dynamic && info->combreloc && dynobj != NULL)
8829     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8830 
8831   /* If we are linking against a dynamic object, or generating a
8832      shared library, finish up the dynamic linking information.  */
8833   if (dynamic)
8834     {
8835       bfd_byte *dyncon, *dynconend;
8836 
8837       /* Fix up .dynamic entries.  */
8838       o = bfd_get_section_by_name (dynobj, ".dynamic");
8839       BFD_ASSERT (o != NULL);
8840 
8841       dyncon = o->contents;
8842       dynconend = o->contents + o->size;
8843       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8844 	{
8845 	  Elf_Internal_Dyn dyn;
8846 	  const char *name;
8847 	  unsigned int type;
8848 
8849 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8850 
8851 	  switch (dyn.d_tag)
8852 	    {
8853 	    default:
8854 	      continue;
8855 	    case DT_NULL:
8856 	      if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8857 		{
8858 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
8859 		    {
8860 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8861 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8862 		    default: continue;
8863 		    }
8864 		  dyn.d_un.d_val = relativecount;
8865 		  relativecount = 0;
8866 		  break;
8867 		}
8868 	      continue;
8869 
8870 	    case DT_INIT:
8871 	      name = info->init_function;
8872 	      goto get_sym;
8873 	    case DT_FINI:
8874 	      name = info->fini_function;
8875 	    get_sym:
8876 	      {
8877 		struct elf_link_hash_entry *h;
8878 
8879 		h = elf_link_hash_lookup (elf_hash_table (info), name,
8880 					  FALSE, FALSE, TRUE);
8881 		if (h != NULL
8882 		    && (h->root.type == bfd_link_hash_defined
8883 			|| h->root.type == bfd_link_hash_defweak))
8884 		  {
8885 		    dyn.d_un.d_val = h->root.u.def.value;
8886 		    o = h->root.u.def.section;
8887 		    if (o->output_section != NULL)
8888 		      dyn.d_un.d_val += (o->output_section->vma
8889 					 + o->output_offset);
8890 		    else
8891 		      {
8892 			/* The symbol is imported from another shared
8893 			   library and does not apply to this one.  */
8894 			dyn.d_un.d_val = 0;
8895 		      }
8896 		    break;
8897 		  }
8898 	      }
8899 	      continue;
8900 
8901 	    case DT_PREINIT_ARRAYSZ:
8902 	      name = ".preinit_array";
8903 	      goto get_size;
8904 	    case DT_INIT_ARRAYSZ:
8905 	      name = ".init_array";
8906 	      goto get_size;
8907 	    case DT_FINI_ARRAYSZ:
8908 	      name = ".fini_array";
8909 	    get_size:
8910 	      o = bfd_get_section_by_name (abfd, name);
8911 	      if (o == NULL)
8912 		{
8913 		  (*_bfd_error_handler)
8914 		    (_("%B: could not find output section %s"), abfd, name);
8915 		  goto error_return;
8916 		}
8917 	      if (o->size == 0)
8918 		(*_bfd_error_handler)
8919 		  (_("warning: %s section has zero size"), name);
8920 	      dyn.d_un.d_val = o->size;
8921 	      break;
8922 
8923 	    case DT_PREINIT_ARRAY:
8924 	      name = ".preinit_array";
8925 	      goto get_vma;
8926 	    case DT_INIT_ARRAY:
8927 	      name = ".init_array";
8928 	      goto get_vma;
8929 	    case DT_FINI_ARRAY:
8930 	      name = ".fini_array";
8931 	      goto get_vma;
8932 
8933 	    case DT_HASH:
8934 	      name = ".hash";
8935 	      goto get_vma;
8936 	    case DT_GNU_HASH:
8937 	      name = ".gnu.hash";
8938 	      goto get_vma;
8939 	    case DT_STRTAB:
8940 	      name = ".dynstr";
8941 	      goto get_vma;
8942 	    case DT_SYMTAB:
8943 	      name = ".dynsym";
8944 	      goto get_vma;
8945 	    case DT_VERDEF:
8946 	      name = ".gnu.version_d";
8947 	      goto get_vma;
8948 	    case DT_VERNEED:
8949 	      name = ".gnu.version_r";
8950 	      goto get_vma;
8951 	    case DT_VERSYM:
8952 	      name = ".gnu.version";
8953 	    get_vma:
8954 	      o = bfd_get_section_by_name (abfd, name);
8955 	      if (o == NULL)
8956 		{
8957 		  (*_bfd_error_handler)
8958 		    (_("%B: could not find output section %s"), abfd, name);
8959 		  goto error_return;
8960 		}
8961 	      dyn.d_un.d_ptr = o->vma;
8962 	      break;
8963 
8964 	    case DT_REL:
8965 	    case DT_RELA:
8966 	    case DT_RELSZ:
8967 	    case DT_RELASZ:
8968 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8969 		type = SHT_REL;
8970 	      else
8971 		type = SHT_RELA;
8972 	      dyn.d_un.d_val = 0;
8973 	      for (i = 1; i < elf_numsections (abfd); i++)
8974 		{
8975 		  Elf_Internal_Shdr *hdr;
8976 
8977 		  hdr = elf_elfsections (abfd)[i];
8978 		  if (hdr->sh_type == type
8979 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
8980 		    {
8981 		      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8982 			dyn.d_un.d_val += hdr->sh_size;
8983 		      else
8984 			{
8985 			  if (dyn.d_un.d_val == 0
8986 			      || hdr->sh_addr < dyn.d_un.d_val)
8987 			    dyn.d_un.d_val = hdr->sh_addr;
8988 			}
8989 		    }
8990 		}
8991 	      break;
8992 	    }
8993 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8994 	}
8995     }
8996 
8997   /* If we have created any dynamic sections, then output them.  */
8998   if (dynobj != NULL)
8999     {
9000       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
9001 	goto error_return;
9002 
9003       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
9004       if (!info->allow_textrel || (info->warn_shared_textrel && info->shared))
9005 	{
9006 	  bfd_byte *dyncon, *dynconend;
9007 
9008 	  /* Fix up .dynamic entries.  */
9009 	  o = bfd_get_section_by_name (dynobj, ".dynamic");
9010 	  if (o != NULL)
9011 	    {
9012 	      dyncon = o->contents;
9013 	      dynconend = o->contents + o->size;
9014 	      for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
9015 		{
9016 		  Elf_Internal_Dyn dyn;
9017 
9018 		  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
9019 
9020 		  if (dyn.d_tag == DT_TEXTREL)
9021 		    {
9022 		      _bfd_error_handler
9023 			(_("warning: creating a DT_TEXTREL in a shared object."));
9024 #if 0
9025 		      if (!info->allow_textrel)
9026 			goto error_return;
9027 #endif
9028 		      break;
9029 		    }
9030 		}
9031 	    }
9032 	}
9033 
9034       for (o = dynobj->sections; o != NULL; o = o->next)
9035 	{
9036 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
9037 	      || o->size == 0
9038 	      || o->output_section == bfd_abs_section_ptr)
9039 	    continue;
9040 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
9041 	    {
9042 	      /* At this point, we are only interested in sections
9043 		 created by _bfd_elf_link_create_dynamic_sections.  */
9044 	      continue;
9045 	    }
9046 	  if (elf_hash_table (info)->stab_info.stabstr == o)
9047 	    continue;
9048 	  if (elf_hash_table (info)->eh_info.hdr_sec == o)
9049 	    continue;
9050 	  if ((elf_section_data (o->output_section)->this_hdr.sh_type
9051 	       != SHT_STRTAB)
9052 	      || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
9053 	    {
9054 	      if (! bfd_set_section_contents (abfd, o->output_section,
9055 					      o->contents,
9056 					      (file_ptr) o->output_offset,
9057 					      o->size))
9058 		goto error_return;
9059 	    }
9060 	  else
9061 	    {
9062 	      /* The contents of the .dynstr section are actually in a
9063 		 stringtab.  */
9064 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
9065 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
9066 		  || ! _bfd_elf_strtab_emit (abfd,
9067 					     elf_hash_table (info)->dynstr))
9068 		goto error_return;
9069 	    }
9070 	}
9071     }
9072 
9073   if (info->relocatable)
9074     {
9075       bfd_boolean failed = FALSE;
9076 
9077       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
9078       if (failed)
9079 	goto error_return;
9080     }
9081 
9082   /* If we have optimized stabs strings, output them.  */
9083   if (elf_hash_table (info)->stab_info.stabstr != NULL)
9084     {
9085       if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
9086 	goto error_return;
9087     }
9088 
9089   if (info->eh_frame_hdr)
9090     {
9091       if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
9092 	goto error_return;
9093     }
9094 
9095   if (finfo.symstrtab != NULL)
9096     _bfd_stringtab_free (finfo.symstrtab);
9097   if (finfo.contents != NULL)
9098     free (finfo.contents);
9099   if (finfo.external_relocs != NULL)
9100     free (finfo.external_relocs);
9101   if (finfo.internal_relocs != NULL)
9102     free (finfo.internal_relocs);
9103   if (finfo.external_syms != NULL)
9104     free (finfo.external_syms);
9105   if (finfo.locsym_shndx != NULL)
9106     free (finfo.locsym_shndx);
9107   if (finfo.internal_syms != NULL)
9108     free (finfo.internal_syms);
9109   if (finfo.indices != NULL)
9110     free (finfo.indices);
9111   if (finfo.sections != NULL)
9112     free (finfo.sections);
9113   if (finfo.symbuf != NULL)
9114     free (finfo.symbuf);
9115   if (finfo.symshndxbuf != NULL)
9116     free (finfo.symshndxbuf);
9117   for (o = abfd->sections; o != NULL; o = o->next)
9118     {
9119       if ((o->flags & SEC_RELOC) != 0
9120 	  && elf_section_data (o)->rel_hashes != NULL)
9121 	free (elf_section_data (o)->rel_hashes);
9122     }
9123 
9124   elf_tdata (abfd)->linker = TRUE;
9125 
9126   return TRUE;
9127 
9128  error_return:
9129   if (finfo.symstrtab != NULL)
9130     _bfd_stringtab_free (finfo.symstrtab);
9131   if (finfo.contents != NULL)
9132     free (finfo.contents);
9133   if (finfo.external_relocs != NULL)
9134     free (finfo.external_relocs);
9135   if (finfo.internal_relocs != NULL)
9136     free (finfo.internal_relocs);
9137   if (finfo.external_syms != NULL)
9138     free (finfo.external_syms);
9139   if (finfo.locsym_shndx != NULL)
9140     free (finfo.locsym_shndx);
9141   if (finfo.internal_syms != NULL)
9142     free (finfo.internal_syms);
9143   if (finfo.indices != NULL)
9144     free (finfo.indices);
9145   if (finfo.sections != NULL)
9146     free (finfo.sections);
9147   if (finfo.symbuf != NULL)
9148     free (finfo.symbuf);
9149   if (finfo.symshndxbuf != NULL)
9150     free (finfo.symshndxbuf);
9151   for (o = abfd->sections; o != NULL; o = o->next)
9152     {
9153       if ((o->flags & SEC_RELOC) != 0
9154 	  && elf_section_data (o)->rel_hashes != NULL)
9155 	free (elf_section_data (o)->rel_hashes);
9156     }
9157 
9158   return FALSE;
9159 }
9160 
9161 /* Garbage collect unused sections.  */
9162 
9163 /* The mark phase of garbage collection.  For a given section, mark
9164    it and any sections in this section's group, and all the sections
9165    which define symbols to which it refers.  */
9166 
9167 typedef asection * (*gc_mark_hook_fn)
9168   (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9169    struct elf_link_hash_entry *, Elf_Internal_Sym *);
9170 
9171 bfd_boolean
9172 _bfd_elf_gc_mark (struct bfd_link_info *info,
9173 		  asection *sec,
9174 		  gc_mark_hook_fn gc_mark_hook)
9175 {
9176   bfd_boolean ret;
9177   bfd_boolean is_eh;
9178   asection *group_sec;
9179 
9180   sec->gc_mark = 1;
9181 
9182   /* Mark all the sections in the group.  */
9183   group_sec = elf_section_data (sec)->next_in_group;
9184   if (group_sec && !group_sec->gc_mark)
9185     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
9186       return FALSE;
9187 
9188   /* Look through the section relocs.  */
9189   ret = TRUE;
9190   is_eh = strcmp (sec->name, ".eh_frame") == 0;
9191   if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
9192     {
9193       Elf_Internal_Rela *relstart, *rel, *relend;
9194       Elf_Internal_Shdr *symtab_hdr;
9195       struct elf_link_hash_entry **sym_hashes;
9196       size_t nlocsyms;
9197       size_t extsymoff;
9198       bfd *input_bfd = sec->owner;
9199       const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
9200       Elf_Internal_Sym *isym = NULL;
9201       int r_sym_shift;
9202 
9203       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9204       sym_hashes = elf_sym_hashes (input_bfd);
9205 
9206       /* Read the local symbols.  */
9207       if (elf_bad_symtab (input_bfd))
9208 	{
9209 	  nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
9210 	  extsymoff = 0;
9211 	}
9212       else
9213 	extsymoff = nlocsyms = symtab_hdr->sh_info;
9214 
9215       isym = (Elf_Internal_Sym *) symtab_hdr->contents;
9216       if (isym == NULL && nlocsyms != 0)
9217 	{
9218 	  isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
9219 				       NULL, NULL, NULL);
9220 	  if (isym == NULL)
9221 	    return FALSE;
9222 	}
9223 
9224       /* Read the relocations.  */
9225       relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
9226 					    info->keep_memory);
9227       if (relstart == NULL)
9228 	{
9229 	  ret = FALSE;
9230 	  goto out1;
9231 	}
9232       relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9233 
9234       if (bed->s->arch_size == 32)
9235 	r_sym_shift = 8;
9236       else
9237 	r_sym_shift = 32;
9238 
9239       for (rel = relstart; rel < relend; rel++)
9240 	{
9241 	  unsigned long r_symndx;
9242 	  asection *rsec;
9243 	  struct elf_link_hash_entry *h;
9244 
9245 	  r_symndx = rel->r_info >> r_sym_shift;
9246 	  if (r_symndx == 0)
9247 	    continue;
9248 
9249 	  if (r_symndx >= nlocsyms
9250 	      || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
9251 	    {
9252 	      h = sym_hashes[r_symndx - extsymoff];
9253 	      while (h->root.type == bfd_link_hash_indirect
9254 		     || h->root.type == bfd_link_hash_warning)
9255 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
9256 	      rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
9257 	    }
9258 	  else
9259 	    {
9260 	      rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
9261 	    }
9262 
9263 	  if (rsec && !rsec->gc_mark)
9264 	    {
9265 	      if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
9266 		rsec->gc_mark = 1;
9267 	      else if (is_eh)
9268 		rsec->gc_mark_from_eh = 1;
9269 	      else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
9270 		{
9271 		  ret = FALSE;
9272 		  goto out2;
9273 		}
9274 	    }
9275 	}
9276 
9277     out2:
9278       if (elf_section_data (sec)->relocs != relstart)
9279 	free (relstart);
9280     out1:
9281       if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
9282 	{
9283 	  if (! info->keep_memory)
9284 	    free (isym);
9285 	  else
9286 	    symtab_hdr->contents = (unsigned char *) isym;
9287 	}
9288     }
9289 
9290   return ret;
9291 }
9292 
9293 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
9294 
9295 struct elf_gc_sweep_symbol_info {
9296   struct bfd_link_info *info;
9297   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
9298 		       bfd_boolean);
9299 };
9300 
9301 static bfd_boolean
9302 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
9303 {
9304   if (h->root.type == bfd_link_hash_warning)
9305     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9306 
9307   if ((h->root.type == bfd_link_hash_defined
9308        || h->root.type == bfd_link_hash_defweak)
9309       && !h->root.u.def.section->gc_mark
9310       && !(h->root.u.def.section->owner->flags & DYNAMIC))
9311     {
9312       struct elf_gc_sweep_symbol_info *inf = data;
9313       (*inf->hide_symbol) (inf->info, h, TRUE);
9314     }
9315 
9316   return TRUE;
9317 }
9318 
9319 /* The sweep phase of garbage collection.  Remove all garbage sections.  */
9320 
9321 typedef bfd_boolean (*gc_sweep_hook_fn)
9322   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
9323 
9324 static bfd_boolean
9325 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
9326 {
9327   bfd *sub;
9328   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9329   gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
9330   unsigned long section_sym_count;
9331   struct elf_gc_sweep_symbol_info sweep_info;
9332 
9333   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9334     {
9335       asection *o;
9336 
9337       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9338 	continue;
9339 
9340       for (o = sub->sections; o != NULL; o = o->next)
9341 	{
9342 	  /* Keep debug and special sections.  */
9343 	  if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
9344 	      || elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
9345 	      || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
9346 	    o->gc_mark = 1;
9347 
9348 	  if (o->gc_mark)
9349 	    continue;
9350 
9351 	  /* Skip sweeping sections already excluded.  */
9352 	  if (o->flags & SEC_EXCLUDE)
9353 	    continue;
9354 
9355 	  /* Since this is early in the link process, it is simple
9356 	     to remove a section from the output.  */
9357 	  o->flags |= SEC_EXCLUDE;
9358 
9359 	  /* But we also have to update some of the relocation
9360 	     info we collected before.  */
9361 	  if (gc_sweep_hook
9362 	      && (o->flags & SEC_RELOC) != 0
9363 	      && o->reloc_count > 0
9364 	      && !bfd_is_abs_section (o->output_section))
9365 	    {
9366 	      Elf_Internal_Rela *internal_relocs;
9367 	      bfd_boolean r;
9368 
9369 	      internal_relocs
9370 		= _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
9371 					     info->keep_memory);
9372 	      if (internal_relocs == NULL)
9373 		return FALSE;
9374 
9375 	      r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
9376 
9377 	      if (elf_section_data (o)->relocs != internal_relocs)
9378 		free (internal_relocs);
9379 
9380 	      if (!r)
9381 		return FALSE;
9382 	    }
9383 	}
9384     }
9385 
9386   /* Remove the symbols that were in the swept sections from the dynamic
9387      symbol table.  GCFIXME: Anyone know how to get them out of the
9388      static symbol table as well?  */
9389   sweep_info.info = info;
9390   sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
9391   elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
9392 			  &sweep_info);
9393 
9394   _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
9395   return TRUE;
9396 }
9397 
9398 /* Propagate collected vtable information.  This is called through
9399    elf_link_hash_traverse.  */
9400 
9401 static bfd_boolean
9402 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
9403 {
9404   if (h->root.type == bfd_link_hash_warning)
9405     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9406 
9407   /* Those that are not vtables.  */
9408   if (h->vtable == NULL || h->vtable->parent == NULL)
9409     return TRUE;
9410 
9411   /* Those vtables that do not have parents, we cannot merge.  */
9412   if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
9413     return TRUE;
9414 
9415   /* If we've already been done, exit.  */
9416   if (h->vtable->used && h->vtable->used[-1])
9417     return TRUE;
9418 
9419   /* Make sure the parent's table is up to date.  */
9420   elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
9421 
9422   if (h->vtable->used == NULL)
9423     {
9424       /* None of this table's entries were referenced.  Re-use the
9425 	 parent's table.  */
9426       h->vtable->used = h->vtable->parent->vtable->used;
9427       h->vtable->size = h->vtable->parent->vtable->size;
9428     }
9429   else
9430     {
9431       size_t n;
9432       bfd_boolean *cu, *pu;
9433 
9434       /* Or the parent's entries into ours.  */
9435       cu = h->vtable->used;
9436       cu[-1] = TRUE;
9437       pu = h->vtable->parent->vtable->used;
9438       if (pu != NULL)
9439 	{
9440 	  const struct elf_backend_data *bed;
9441 	  unsigned int log_file_align;
9442 
9443 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
9444 	  log_file_align = bed->s->log_file_align;
9445 	  n = h->vtable->parent->vtable->size >> log_file_align;
9446 	  while (n--)
9447 	    {
9448 	      if (*pu)
9449 		*cu = TRUE;
9450 	      pu++;
9451 	      cu++;
9452 	    }
9453 	}
9454     }
9455 
9456   return TRUE;
9457 }
9458 
9459 static bfd_boolean
9460 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9461 {
9462   asection *sec;
9463   bfd_vma hstart, hend;
9464   Elf_Internal_Rela *relstart, *relend, *rel;
9465   const struct elf_backend_data *bed;
9466   unsigned int log_file_align;
9467 
9468   if (h->root.type == bfd_link_hash_warning)
9469     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9470 
9471   /* Take care of both those symbols that do not describe vtables as
9472      well as those that are not loaded.  */
9473   if (h->vtable == NULL || h->vtable->parent == NULL)
9474     return TRUE;
9475 
9476   BFD_ASSERT (h->root.type == bfd_link_hash_defined
9477 	      || h->root.type == bfd_link_hash_defweak);
9478 
9479   sec = h->root.u.def.section;
9480   hstart = h->root.u.def.value;
9481   hend = hstart + h->size;
9482 
9483   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9484   if (!relstart)
9485     return *(bfd_boolean *) okp = FALSE;
9486   bed = get_elf_backend_data (sec->owner);
9487   log_file_align = bed->s->log_file_align;
9488 
9489   relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9490 
9491   for (rel = relstart; rel < relend; ++rel)
9492     if (rel->r_offset >= hstart && rel->r_offset < hend)
9493       {
9494 	/* If the entry is in use, do nothing.  */
9495 	if (h->vtable->used
9496 	    && (rel->r_offset - hstart) < h->vtable->size)
9497 	  {
9498 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9499 	    if (h->vtable->used[entry])
9500 	      continue;
9501 	  }
9502 	/* Otherwise, kill it.  */
9503 	rel->r_offset = rel->r_info = rel->r_addend = 0;
9504       }
9505 
9506   return TRUE;
9507 }
9508 
9509 /* Mark sections containing dynamically referenced symbols.  When
9510    building shared libraries, we must assume that any visible symbol is
9511    referenced.  */
9512 
9513 bfd_boolean
9514 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
9515 {
9516   struct bfd_link_info *info = (struct bfd_link_info *) inf;
9517 
9518   if (h->root.type == bfd_link_hash_warning)
9519     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9520 
9521   if ((h->root.type == bfd_link_hash_defined
9522        || h->root.type == bfd_link_hash_defweak)
9523       && (h->ref_dynamic
9524 	  || (!info->executable
9525 	      && h->def_regular
9526 	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
9527 	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
9528     h->root.u.def.section->flags |= SEC_KEEP;
9529 
9530   return TRUE;
9531 }
9532 
9533 /* Do mark and sweep of unused sections.  */
9534 
9535 bfd_boolean
9536 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9537 {
9538   bfd_boolean ok = TRUE;
9539   bfd *sub;
9540   asection * (*gc_mark_hook)
9541     (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9542      struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9543   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9544 
9545   if (!bed->can_gc_sections
9546       || info->relocatable
9547       || info->emitrelocations
9548       || !is_elf_hash_table (info->hash))
9549     {
9550       (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9551       return TRUE;
9552     }
9553 
9554   /* Apply transitive closure to the vtable entry usage info.  */
9555   elf_link_hash_traverse (elf_hash_table (info),
9556 			  elf_gc_propagate_vtable_entries_used,
9557 			  &ok);
9558   if (!ok)
9559     return FALSE;
9560 
9561   /* Kill the vtable relocations that were not used.  */
9562   elf_link_hash_traverse (elf_hash_table (info),
9563 			  elf_gc_smash_unused_vtentry_relocs,
9564 			  &ok);
9565   if (!ok)
9566     return FALSE;
9567 
9568   /* Mark dynamically referenced symbols.  */
9569   if (elf_hash_table (info)->dynamic_sections_created)
9570     elf_link_hash_traverse (elf_hash_table (info),
9571 			    bed->gc_mark_dynamic_ref,
9572 			    info);
9573 
9574   /* Grovel through relocs to find out who stays ...  */
9575   gc_mark_hook = bed->gc_mark_hook;
9576   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9577     {
9578       asection *o;
9579 
9580       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9581 	continue;
9582 
9583       for (o = sub->sections; o != NULL; o = o->next)
9584 	if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9585 	  if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9586 	    return FALSE;
9587     }
9588 
9589   /* ... again for sections marked from eh_frame.  */
9590   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9591     {
9592       asection *o;
9593 
9594       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9595 	continue;
9596 
9597       /* Keep .gcc_except_table.* if the associated .text.* is
9598 	 marked.  This isn't very nice, but the proper solution,
9599 	 splitting .eh_frame up and using comdat doesn't pan out
9600 	 easily due to needing special relocs to handle the
9601 	 difference of two symbols in separate sections.
9602 	 Don't keep code sections referenced by .eh_frame.  */
9603       for (o = sub->sections; o != NULL; o = o->next)
9604 	if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
9605 	  {
9606 	    if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
9607 	      {
9608 		unsigned long len;
9609 		char *fn_name;
9610 		asection *fn_text;
9611 
9612 		len = strlen (o->name + 18) + 1;
9613 		fn_name = bfd_malloc (len + 6);
9614 		if (fn_name == NULL)
9615 		  return FALSE;
9616 		memcpy (fn_name, ".text.", 6);
9617 		memcpy (fn_name + 6, o->name + 18, len);
9618 		fn_text = bfd_get_section_by_name (sub, fn_name);
9619 		free (fn_name);
9620 		if (fn_text == NULL || !fn_text->gc_mark)
9621 		  continue;
9622 	      }
9623 
9624 	    /* If not using specially named exception table section,
9625 	       then keep whatever we are using.  */
9626 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9627 	      return FALSE;
9628 	  }
9629     }
9630 
9631   /* ... and mark SEC_EXCLUDE for those that go.  */
9632   return elf_gc_sweep (abfd, info);
9633 }
9634 
9635 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
9636 
9637 bfd_boolean
9638 bfd_elf_gc_record_vtinherit (bfd *abfd,
9639 			     asection *sec,
9640 			     struct elf_link_hash_entry *h,
9641 			     bfd_vma offset)
9642 {
9643   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9644   struct elf_link_hash_entry **search, *child;
9645   bfd_size_type extsymcount;
9646   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9647 
9648   /* The sh_info field of the symtab header tells us where the
9649      external symbols start.  We don't care about the local symbols at
9650      this point.  */
9651   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9652   if (!elf_bad_symtab (abfd))
9653     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9654 
9655   sym_hashes = elf_sym_hashes (abfd);
9656   sym_hashes_end = sym_hashes + extsymcount;
9657 
9658   /* Hunt down the child symbol, which is in this section at the same
9659      offset as the relocation.  */
9660   for (search = sym_hashes; search != sym_hashes_end; ++search)
9661     {
9662       if ((child = *search) != NULL
9663 	  && (child->root.type == bfd_link_hash_defined
9664 	      || child->root.type == bfd_link_hash_defweak)
9665 	  && child->root.u.def.section == sec
9666 	  && child->root.u.def.value == offset)
9667 	goto win;
9668     }
9669 
9670   (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9671 			 abfd, sec, (unsigned long) offset);
9672   bfd_set_error (bfd_error_invalid_operation);
9673   return FALSE;
9674 
9675  win:
9676   if (!child->vtable)
9677     {
9678       child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9679       if (!child->vtable)
9680 	return FALSE;
9681     }
9682   if (!h)
9683     {
9684       /* This *should* only be the absolute section.  It could potentially
9685 	 be that someone has defined a non-global vtable though, which
9686 	 would be bad.  It isn't worth paging in the local symbols to be
9687 	 sure though; that case should simply be handled by the assembler.  */
9688 
9689       child->vtable->parent = (struct elf_link_hash_entry *) -1;
9690     }
9691   else
9692     child->vtable->parent = h;
9693 
9694   return TRUE;
9695 }
9696 
9697 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
9698 
9699 bfd_boolean
9700 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9701 			   asection *sec ATTRIBUTE_UNUSED,
9702 			   struct elf_link_hash_entry *h,
9703 			   bfd_vma addend)
9704 {
9705   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9706   unsigned int log_file_align = bed->s->log_file_align;
9707 
9708   if (!h->vtable)
9709     {
9710       h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9711       if (!h->vtable)
9712 	return FALSE;
9713     }
9714 
9715   if (addend >= h->vtable->size)
9716     {
9717       size_t size, bytes, file_align;
9718       bfd_boolean *ptr = h->vtable->used;
9719 
9720       /* While the symbol is undefined, we have to be prepared to handle
9721 	 a zero size.  */
9722       file_align = 1 << log_file_align;
9723       if (h->root.type == bfd_link_hash_undefined)
9724 	size = addend + file_align;
9725       else
9726 	{
9727 	  size = h->size;
9728 	  if (addend >= size)
9729 	    {
9730 	      /* Oops!  We've got a reference past the defined end of
9731 		 the table.  This is probably a bug -- shall we warn?  */
9732 	      size = addend + file_align;
9733 	    }
9734 	}
9735       size = (size + file_align - 1) & -file_align;
9736 
9737       /* Allocate one extra entry for use as a "done" flag for the
9738 	 consolidation pass.  */
9739       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9740 
9741       if (ptr)
9742 	{
9743 	  ptr = bfd_realloc (ptr - 1, bytes);
9744 
9745 	  if (ptr != NULL)
9746 	    {
9747 	      size_t oldbytes;
9748 
9749 	      oldbytes = (((h->vtable->size >> log_file_align) + 1)
9750 			  * sizeof (bfd_boolean));
9751 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9752 	    }
9753 	}
9754       else
9755 	ptr = bfd_zmalloc (bytes);
9756 
9757       if (ptr == NULL)
9758 	return FALSE;
9759 
9760       /* And arrange for that done flag to be at index -1.  */
9761       h->vtable->used = ptr + 1;
9762       h->vtable->size = size;
9763     }
9764 
9765   h->vtable->used[addend >> log_file_align] = TRUE;
9766 
9767   return TRUE;
9768 }
9769 
9770 struct alloc_got_off_arg {
9771   bfd_vma gotoff;
9772   unsigned int got_elt_size;
9773 };
9774 
9775 /* We need a special top-level link routine to convert got reference counts
9776    to real got offsets.  */
9777 
9778 static bfd_boolean
9779 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9780 {
9781   struct alloc_got_off_arg *gofarg = arg;
9782 
9783   if (h->root.type == bfd_link_hash_warning)
9784     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9785 
9786   if (h->got.refcount > 0)
9787     {
9788       h->got.offset = gofarg->gotoff;
9789       gofarg->gotoff += gofarg->got_elt_size;
9790     }
9791   else
9792     h->got.offset = (bfd_vma) -1;
9793 
9794   return TRUE;
9795 }
9796 
9797 /* And an accompanying bit to work out final got entry offsets once
9798    we're done.  Should be called from final_link.  */
9799 
9800 bfd_boolean
9801 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9802 					struct bfd_link_info *info)
9803 {
9804   bfd *i;
9805   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9806   bfd_vma gotoff;
9807   unsigned int got_elt_size = bed->s->arch_size / 8;
9808   struct alloc_got_off_arg gofarg;
9809 
9810   if (! is_elf_hash_table (info->hash))
9811     return FALSE;
9812 
9813   /* The GOT offset is relative to the .got section, but the GOT header is
9814      put into the .got.plt section, if the backend uses it.  */
9815   if (bed->want_got_plt)
9816     gotoff = 0;
9817   else
9818     gotoff = bed->got_header_size;
9819 
9820   /* Do the local .got entries first.  */
9821   for (i = info->input_bfds; i; i = i->link_next)
9822     {
9823       bfd_signed_vma *local_got;
9824       bfd_size_type j, locsymcount;
9825       Elf_Internal_Shdr *symtab_hdr;
9826 
9827       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9828 	continue;
9829 
9830       local_got = elf_local_got_refcounts (i);
9831       if (!local_got)
9832 	continue;
9833 
9834       symtab_hdr = &elf_tdata (i)->symtab_hdr;
9835       if (elf_bad_symtab (i))
9836 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9837       else
9838 	locsymcount = symtab_hdr->sh_info;
9839 
9840       for (j = 0; j < locsymcount; ++j)
9841 	{
9842 	  if (local_got[j] > 0)
9843 	    {
9844 	      local_got[j] = gotoff;
9845 	      gotoff += got_elt_size;
9846 	    }
9847 	  else
9848 	    local_got[j] = (bfd_vma) -1;
9849 	}
9850     }
9851 
9852   /* Then the global .got entries.  .plt refcounts are handled by
9853      adjust_dynamic_symbol  */
9854   gofarg.gotoff = gotoff;
9855   gofarg.got_elt_size = got_elt_size;
9856   elf_link_hash_traverse (elf_hash_table (info),
9857 			  elf_gc_allocate_got_offsets,
9858 			  &gofarg);
9859   return TRUE;
9860 }
9861 
9862 /* Many folk need no more in the way of final link than this, once
9863    got entry reference counting is enabled.  */
9864 
9865 bfd_boolean
9866 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9867 {
9868   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9869     return FALSE;
9870 
9871   /* Invoke the regular ELF backend linker to do all the work.  */
9872   return bfd_elf_final_link (abfd, info);
9873 }
9874 
9875 bfd_boolean
9876 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9877 {
9878   struct elf_reloc_cookie *rcookie = cookie;
9879 
9880   if (rcookie->bad_symtab)
9881     rcookie->rel = rcookie->rels;
9882 
9883   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9884     {
9885       unsigned long r_symndx;
9886 
9887       if (! rcookie->bad_symtab)
9888 	if (rcookie->rel->r_offset > offset)
9889 	  return FALSE;
9890       if (rcookie->rel->r_offset != offset)
9891 	continue;
9892 
9893       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9894       if (r_symndx == SHN_UNDEF)
9895 	return TRUE;
9896 
9897       if (r_symndx >= rcookie->locsymcount
9898 	  || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9899 	{
9900 	  struct elf_link_hash_entry *h;
9901 
9902 	  h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9903 
9904 	  while (h->root.type == bfd_link_hash_indirect
9905 		 || h->root.type == bfd_link_hash_warning)
9906 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
9907 
9908 	  if ((h->root.type == bfd_link_hash_defined
9909 	       || h->root.type == bfd_link_hash_defweak)
9910 	      && elf_discarded_section (h->root.u.def.section))
9911 	    return TRUE;
9912 	  else
9913 	    return FALSE;
9914 	}
9915       else
9916 	{
9917 	  /* It's not a relocation against a global symbol,
9918 	     but it could be a relocation against a local
9919 	     symbol for a discarded section.  */
9920 	  asection *isec;
9921 	  Elf_Internal_Sym *isym;
9922 
9923 	  /* Need to: get the symbol; get the section.  */
9924 	  isym = &rcookie->locsyms[r_symndx];
9925 	  if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9926 	    {
9927 	      isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9928 	      if (isec != NULL && elf_discarded_section (isec))
9929 		return TRUE;
9930 	    }
9931 	}
9932       return FALSE;
9933     }
9934   return FALSE;
9935 }
9936 
9937 /* Discard unneeded references to discarded sections.
9938    Returns TRUE if any section's size was changed.  */
9939 /* This function assumes that the relocations are in sorted order,
9940    which is true for all known assemblers.  */
9941 
9942 bfd_boolean
9943 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9944 {
9945   struct elf_reloc_cookie cookie;
9946   asection *stab, *eh;
9947   Elf_Internal_Shdr *symtab_hdr;
9948   const struct elf_backend_data *bed;
9949   bfd *abfd;
9950   unsigned int count;
9951   bfd_boolean ret = FALSE;
9952 
9953   if (info->traditional_format
9954       || !is_elf_hash_table (info->hash))
9955     return FALSE;
9956 
9957   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9958     {
9959       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9960 	continue;
9961 
9962       bed = get_elf_backend_data (abfd);
9963 
9964       if ((abfd->flags & DYNAMIC) != 0)
9965 	continue;
9966 
9967       eh = bfd_get_section_by_name (abfd, ".eh_frame");
9968       if (info->relocatable
9969 	  || (eh != NULL
9970 	      && (eh->size == 0
9971 		  || bfd_is_abs_section (eh->output_section))))
9972 	eh = NULL;
9973 
9974       stab = bfd_get_section_by_name (abfd, ".stab");
9975       if (stab != NULL
9976 	  && (stab->size == 0
9977 	      || bfd_is_abs_section (stab->output_section)
9978 	      || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9979 	stab = NULL;
9980 
9981       if (stab == NULL
9982 	  && eh == NULL
9983 	  && bed->elf_backend_discard_info == NULL)
9984 	continue;
9985 
9986       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9987       cookie.abfd = abfd;
9988       cookie.sym_hashes = elf_sym_hashes (abfd);
9989       cookie.bad_symtab = elf_bad_symtab (abfd);
9990       if (cookie.bad_symtab)
9991 	{
9992 	  cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9993 	  cookie.extsymoff = 0;
9994 	}
9995       else
9996 	{
9997 	  cookie.locsymcount = symtab_hdr->sh_info;
9998 	  cookie.extsymoff = symtab_hdr->sh_info;
9999 	}
10000 
10001       if (bed->s->arch_size == 32)
10002 	cookie.r_sym_shift = 8;
10003       else
10004 	cookie.r_sym_shift = 32;
10005 
10006       cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
10007       if (cookie.locsyms == NULL && cookie.locsymcount != 0)
10008 	{
10009 	  cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
10010 						 cookie.locsymcount, 0,
10011 						 NULL, NULL, NULL);
10012 	  if (cookie.locsyms == NULL)
10013 	    return FALSE;
10014 	}
10015 
10016       if (stab != NULL)
10017 	{
10018 	  cookie.rels = NULL;
10019 	  count = stab->reloc_count;
10020 	  if (count != 0)
10021 	    cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
10022 						     info->keep_memory);
10023 	  if (cookie.rels != NULL)
10024 	    {
10025 	      cookie.rel = cookie.rels;
10026 	      cookie.relend = cookie.rels;
10027 	      cookie.relend += count * bed->s->int_rels_per_ext_rel;
10028 	      if (_bfd_discard_section_stabs (abfd, stab,
10029 					      elf_section_data (stab)->sec_info,
10030 					      bfd_elf_reloc_symbol_deleted_p,
10031 					      &cookie))
10032 		ret = TRUE;
10033 	      if (elf_section_data (stab)->relocs != cookie.rels)
10034 		free (cookie.rels);
10035 	    }
10036 	}
10037 
10038       if (eh != NULL)
10039 	{
10040 	  cookie.rels = NULL;
10041 	  count = eh->reloc_count;
10042 	  if (count != 0)
10043 	    cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
10044 						     info->keep_memory);
10045 	  cookie.rel = cookie.rels;
10046 	  cookie.relend = cookie.rels;
10047 	  if (cookie.rels != NULL)
10048 	    cookie.relend += count * bed->s->int_rels_per_ext_rel;
10049 
10050 	  if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
10051 						 bfd_elf_reloc_symbol_deleted_p,
10052 						 &cookie))
10053 	    ret = TRUE;
10054 
10055 	  if (cookie.rels != NULL
10056 	      && elf_section_data (eh)->relocs != cookie.rels)
10057 	    free (cookie.rels);
10058 	}
10059 
10060       if (bed->elf_backend_discard_info != NULL
10061 	  && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
10062 	ret = TRUE;
10063 
10064       if (cookie.locsyms != NULL
10065 	  && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
10066 	{
10067 	  if (! info->keep_memory)
10068 	    free (cookie.locsyms);
10069 	  else
10070 	    symtab_hdr->contents = (unsigned char *) cookie.locsyms;
10071 	}
10072     }
10073 
10074   if (info->eh_frame_hdr
10075       && !info->relocatable
10076       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
10077     ret = TRUE;
10078 
10079   return ret;
10080 }
10081 
10082 void
10083 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec,
10084 				 struct bfd_link_info *info)
10085 {
10086   flagword flags;
10087   const char *name, *p;
10088   struct bfd_section_already_linked *l;
10089   struct bfd_section_already_linked_hash_entry *already_linked_list;
10090   asection *group;
10091 
10092   /* A single member comdat group section may be discarded by a
10093      linkonce section. See below.  */
10094   if (sec->output_section == bfd_abs_section_ptr)
10095     return;
10096 
10097   flags = sec->flags;
10098 
10099   /* Check if it belongs to a section group.  */
10100   group = elf_sec_group (sec);
10101 
10102   /* Return if it isn't a linkonce section nor a member of a group.  A
10103      comdat group section also has SEC_LINK_ONCE set.  */
10104   if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
10105     return;
10106 
10107   if (group)
10108     {
10109       /* If this is the member of a single member comdat group, check if
10110 	 the group should be discarded.  */
10111       if (elf_next_in_group (sec) == sec
10112 	  && (group->flags & SEC_LINK_ONCE) != 0)
10113 	sec = group;
10114       else
10115 	return;
10116     }
10117 
10118   /* FIXME: When doing a relocatable link, we may have trouble
10119      copying relocations in other sections that refer to local symbols
10120      in the section being discarded.  Those relocations will have to
10121      be converted somehow; as of this writing I'm not sure that any of
10122      the backends handle that correctly.
10123 
10124      It is tempting to instead not discard link once sections when
10125      doing a relocatable link (technically, they should be discarded
10126      whenever we are building constructors).  However, that fails,
10127      because the linker winds up combining all the link once sections
10128      into a single large link once section, which defeats the purpose
10129      of having link once sections in the first place.
10130 
10131      Also, not merging link once sections in a relocatable link
10132      causes trouble for MIPS ELF, which relies on link once semantics
10133      to handle the .reginfo section correctly.  */
10134 
10135   name = bfd_get_section_name (abfd, sec);
10136 
10137   if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
10138       && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
10139     p++;
10140   else
10141     p = name;
10142 
10143   already_linked_list = bfd_section_already_linked_table_lookup (p);
10144 
10145   for (l = already_linked_list->entry; l != NULL; l = l->next)
10146     {
10147       /* We may have 3 different sections on the list: group section,
10148 	 comdat section and linkonce section. SEC may be a linkonce or
10149 	 group section. We match a group section with a group section,
10150 	 a linkonce section with a linkonce section, and ignore comdat
10151 	 section.  */
10152       if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
10153 	  && strcmp (name, l->sec->name) == 0
10154 	  && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
10155 	{
10156 	  /* The section has already been linked.  See if we should
10157 	     issue a warning.  */
10158 	  switch (flags & SEC_LINK_DUPLICATES)
10159 	    {
10160 	    default:
10161 	      abort ();
10162 
10163 	    case SEC_LINK_DUPLICATES_DISCARD:
10164 	      break;
10165 
10166 	    case SEC_LINK_DUPLICATES_ONE_ONLY:
10167 	      (*_bfd_error_handler)
10168 		(_("%B: ignoring duplicate section `%A'"),
10169 		 abfd, sec);
10170 	      break;
10171 
10172 	    case SEC_LINK_DUPLICATES_SAME_SIZE:
10173 	      if (sec->size != l->sec->size)
10174 		(*_bfd_error_handler)
10175 		  (_("%B: duplicate section `%A' has different size"),
10176 		   abfd, sec);
10177 	      break;
10178 
10179 	    case SEC_LINK_DUPLICATES_SAME_CONTENTS:
10180 	      if (sec->size != l->sec->size)
10181 		(*_bfd_error_handler)
10182 		  (_("%B: duplicate section `%A' has different size"),
10183 		   abfd, sec);
10184 	      else if (sec->size != 0)
10185 		{
10186 		  bfd_byte *sec_contents = NULL, *l_sec_contents = NULL;
10187 
10188 		  if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
10189 		    (*_bfd_error_handler)
10190 		      (_("%B: warning: could not read contents of section `%A'"),
10191 		       abfd, sec);
10192 		  else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
10193 							&l_sec_contents))
10194 		    (*_bfd_error_handler)
10195 		      (_("%B: warning: could not read contents of section `%A'"),
10196 		       l->sec->owner, l->sec);
10197 		  else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
10198 		    (*_bfd_error_handler)
10199 		      (_("%B: warning: duplicate section `%A' has different contents"),
10200 		       abfd, sec);
10201 
10202 		  if (sec_contents)
10203 		    free (sec_contents);
10204 		  if (l_sec_contents)
10205 		    free (l_sec_contents);
10206 		}
10207 	      break;
10208 	    }
10209 
10210 	  /* Set the output_section field so that lang_add_section
10211 	     does not create a lang_input_section structure for this
10212 	     section.  Since there might be a symbol in the section
10213 	     being discarded, we must retain a pointer to the section
10214 	     which we are really going to use.  */
10215 	  sec->output_section = bfd_abs_section_ptr;
10216 	  sec->kept_section = l->sec;
10217 
10218 	  if (flags & SEC_GROUP)
10219 	    {
10220 	      asection *first = elf_next_in_group (sec);
10221 	      asection *s = first;
10222 
10223 	      while (s != NULL)
10224 		{
10225 		  s->output_section = bfd_abs_section_ptr;
10226 		  /* Record which group discards it.  */
10227 		  s->kept_section = l->sec;
10228 		  s = elf_next_in_group (s);
10229 		  /* These lists are circular.  */
10230 		  if (s == first)
10231 		    break;
10232 		}
10233 	    }
10234 
10235 	  return;
10236 	}
10237     }
10238 
10239   if (group)
10240     {
10241       /* If this is the member of a single member comdat group and the
10242 	 group hasn't be discarded, we check if it matches a linkonce
10243 	 section. We only record the discarded comdat group. Otherwise
10244 	 the undiscarded group will be discarded incorrectly later since
10245 	 itself has been recorded.  */
10246       for (l = already_linked_list->entry; l != NULL; l = l->next)
10247 	if ((l->sec->flags & SEC_GROUP) == 0
10248 	    && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
10249 	    && bfd_elf_match_symbols_in_sections (l->sec,
10250 						  elf_next_in_group (sec),
10251 						  info))
10252 	  {
10253 	    elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
10254 	    elf_next_in_group (sec)->kept_section = l->sec;
10255 	    group->output_section = bfd_abs_section_ptr;
10256 	    break;
10257 	  }
10258       if (l == NULL)
10259 	return;
10260     }
10261   else
10262     /* There is no direct match. But for linkonce section, we should
10263        check if there is a match with comdat group member. We always
10264        record the linkonce section, discarded or not.  */
10265     for (l = already_linked_list->entry; l != NULL; l = l->next)
10266       if (l->sec->flags & SEC_GROUP)
10267 	{
10268 	  asection *first = elf_next_in_group (l->sec);
10269 
10270 	  if (first != NULL
10271 	      && elf_next_in_group (first) == first
10272 	      && bfd_elf_match_symbols_in_sections (first, sec, info))
10273 	    {
10274 	      sec->output_section = bfd_abs_section_ptr;
10275 	      sec->kept_section = l->sec;
10276 	      break;
10277 	    }
10278 	}
10279 
10280   /* This is the first section with this name.  Record it.  */
10281   bfd_section_already_linked_table_insert (already_linked_list, sec);
10282 }
10283 
10284 bfd_boolean
10285 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
10286 {
10287   return sym->st_shndx == SHN_COMMON;
10288 }
10289 
10290 unsigned int
10291 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
10292 {
10293   return SHN_COMMON;
10294 }
10295 
10296 asection *
10297 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
10298 {
10299   return bfd_com_section_ptr;
10300 }
10301