xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/elflink.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* ELF linking support for BFD.
2    Copyright (C) 1995-2016 Free Software Foundation, Inc.
3 
4    This file is part of BFD, the Binary File Descriptor library.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfd_stdint.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin.h"
33 #endif
34 
35 /* This struct is used to pass information to routines called via
36    elf_link_hash_traverse which must return failure.  */
37 
38 struct elf_info_failed
39 {
40   struct bfd_link_info *info;
41   bfd_boolean failed;
42 };
43 
44 /* This structure is used to pass information to
45    _bfd_elf_link_find_version_dependencies.  */
46 
47 struct elf_find_verdep_info
48 {
49   /* General link information.  */
50   struct bfd_link_info *info;
51   /* The number of dependencies.  */
52   unsigned int vers;
53   /* Whether we had a failure.  */
54   bfd_boolean failed;
55 };
56 
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58   (struct elf_link_hash_entry *, struct elf_info_failed *);
59 
60 asection *
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 			     unsigned long r_symndx,
63 			     bfd_boolean discard)
64 {
65   if (r_symndx >= cookie->locsymcount
66       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67     {
68       struct elf_link_hash_entry *h;
69 
70       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71 
72       while (h->root.type == bfd_link_hash_indirect
73 	     || h->root.type == bfd_link_hash_warning)
74 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
75 
76       if ((h->root.type == bfd_link_hash_defined
77 	   || h->root.type == bfd_link_hash_defweak)
78 	   && discarded_section (h->root.u.def.section))
79         return h->root.u.def.section;
80       else
81 	return NULL;
82     }
83   else
84     {
85       /* It's not a relocation against a global symbol,
86 	 but it could be a relocation against a local
87 	 symbol for a discarded section.  */
88       asection *isec;
89       Elf_Internal_Sym *isym;
90 
91       /* Need to: get the symbol; get the section.  */
92       isym = &cookie->locsyms[r_symndx];
93       isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94       if (isec != NULL
95 	  && discard ? discarded_section (isec) : 1)
96 	return isec;
97      }
98   return NULL;
99 }
100 
101 /* Define a symbol in a dynamic linkage section.  */
102 
103 struct elf_link_hash_entry *
104 _bfd_elf_define_linkage_sym (bfd *abfd,
105 			     struct bfd_link_info *info,
106 			     asection *sec,
107 			     const char *name)
108 {
109   struct elf_link_hash_entry *h;
110   struct bfd_link_hash_entry *bh;
111   const struct elf_backend_data *bed;
112 
113   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114   if (h != NULL)
115     {
116       /* Zap symbol defined in an as-needed lib that wasn't linked.
117 	 This is a symptom of a larger problem:  Absolute symbols
118 	 defined in shared libraries can't be overridden, because we
119 	 lose the link to the bfd which is via the symbol section.  */
120       h->root.type = bfd_link_hash_new;
121     }
122 
123   bh = &h->root;
124   bed = get_elf_backend_data (abfd);
125   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
126 					 sec, 0, NULL, FALSE, bed->collect,
127 					 &bh))
128     return NULL;
129   h = (struct elf_link_hash_entry *) bh;
130   h->def_regular = 1;
131   h->non_elf = 0;
132   h->root.linker_def = 1;
133   h->type = STT_OBJECT;
134   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
135     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
136 
137   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
138   return h;
139 }
140 
141 bfd_boolean
142 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
143 {
144   flagword flags;
145   asection *s;
146   struct elf_link_hash_entry *h;
147   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
148   struct elf_link_hash_table *htab = elf_hash_table (info);
149 
150   /* This function may be called more than once.  */
151   s = bfd_get_linker_section (abfd, ".got");
152   if (s != NULL)
153     return TRUE;
154 
155   flags = bed->dynamic_sec_flags;
156 
157   s = bfd_make_section_anyway_with_flags (abfd,
158 					  (bed->rela_plts_and_copies_p
159 					   ? ".rela.got" : ".rel.got"),
160 					  (bed->dynamic_sec_flags
161 					   | SEC_READONLY));
162   if (s == NULL
163       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164     return FALSE;
165   htab->srelgot = s;
166 
167   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
168   if (s == NULL
169       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170     return FALSE;
171   htab->sgot = s;
172 
173   if (bed->want_got_plt)
174     {
175       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
176       if (s == NULL
177 	  || !bfd_set_section_alignment (abfd, s,
178 					 bed->s->log_file_align))
179 	return FALSE;
180       htab->sgotplt = s;
181     }
182 
183   /* The first bit of the global offset table is the header.  */
184   s->size += bed->got_header_size;
185 
186   if (bed->want_got_sym)
187     {
188       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 	 (or .got.plt) section.  We don't do this in the linker script
190 	 because we don't want to define the symbol if we are not creating
191 	 a global offset table.  */
192       h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 				       "_GLOBAL_OFFSET_TABLE_");
194       elf_hash_table (info)->hgot = h;
195       if (h == NULL)
196 	return FALSE;
197     }
198 
199   return TRUE;
200 }
201 
202 /* Create a strtab to hold the dynamic symbol names.  */
203 static bfd_boolean
204 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
205 {
206   struct elf_link_hash_table *hash_table;
207 
208   hash_table = elf_hash_table (info);
209   if (hash_table->dynobj == NULL)
210     {
211       /* We may not set dynobj, an input file holding linker created
212 	 dynamic sections to abfd, which may be a dynamic object with
213 	 its own dynamic sections.  We need to find a normal input file
214 	 to hold linker created sections if possible.  */
215       if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
216 	{
217 	  bfd *ibfd;
218 	  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
219 	    if ((ibfd->flags
220 		 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
221 	      {
222 		abfd = ibfd;
223 		break;
224 	      }
225 	}
226       hash_table->dynobj = abfd;
227     }
228 
229   if (hash_table->dynstr == NULL)
230     {
231       hash_table->dynstr = _bfd_elf_strtab_init ();
232       if (hash_table->dynstr == NULL)
233 	return FALSE;
234     }
235   return TRUE;
236 }
237 
238 /* Create some sections which will be filled in with dynamic linking
239    information.  ABFD is an input file which requires dynamic sections
240    to be created.  The dynamic sections take up virtual memory space
241    when the final executable is run, so we need to create them before
242    addresses are assigned to the output sections.  We work out the
243    actual contents and size of these sections later.  */
244 
245 bfd_boolean
246 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
247 {
248   flagword flags;
249   asection *s;
250   const struct elf_backend_data *bed;
251   struct elf_link_hash_entry *h;
252 
253   if (! is_elf_hash_table (info->hash))
254     return FALSE;
255 
256   if (elf_hash_table (info)->dynamic_sections_created)
257     return TRUE;
258 
259   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
260     return FALSE;
261 
262   abfd = elf_hash_table (info)->dynobj;
263   bed = get_elf_backend_data (abfd);
264 
265   flags = bed->dynamic_sec_flags;
266 
267   /* A dynamically linked executable has a .interp section, but a
268      shared library does not.  */
269   if (bfd_link_executable (info) && !info->nointerp)
270     {
271       s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 					      flags | SEC_READONLY);
273       if (s == NULL)
274 	return FALSE;
275     }
276 
277   /* Create sections to hold version informations.  These are removed
278      if they are not needed.  */
279   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 					  flags | SEC_READONLY);
281   if (s == NULL
282       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
283     return FALSE;
284 
285   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 					  flags | SEC_READONLY);
287   if (s == NULL
288       || ! bfd_set_section_alignment (abfd, s, 1))
289     return FALSE;
290 
291   s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 					  flags | SEC_READONLY);
293   if (s == NULL
294       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
295     return FALSE;
296 
297   s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 					  flags | SEC_READONLY);
299   if (s == NULL
300       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
301     return FALSE;
302   elf_hash_table (info)->dynsym = s;
303 
304   s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 					  flags | SEC_READONLY);
306   if (s == NULL)
307     return FALSE;
308 
309   s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
310   if (s == NULL
311       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312     return FALSE;
313 
314   /* The special symbol _DYNAMIC is always set to the start of the
315      .dynamic section.  We could set _DYNAMIC in a linker script, but we
316      only want to define it if we are, in fact, creating a .dynamic
317      section.  We don't want to define it if there is no .dynamic
318      section, since on some ELF platforms the start up code examines it
319      to decide how to initialize the process.  */
320   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321   elf_hash_table (info)->hdynamic = h;
322   if (h == NULL)
323     return FALSE;
324 
325   if (info->emit_hash)
326     {
327       s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 					      flags | SEC_READONLY);
329       if (s == NULL
330 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
331 	return FALSE;
332       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
333     }
334 
335   if (info->emit_gnu_hash)
336     {
337       s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 					      flags | SEC_READONLY);
339       if (s == NULL
340 	  || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
341 	return FALSE;
342       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 	 4 32-bit words followed by variable count of 64-bit words, then
344 	 variable count of 32-bit words.  */
345       if (bed->s->arch_size == 64)
346 	elf_section_data (s)->this_hdr.sh_entsize = 0;
347       else
348 	elf_section_data (s)->this_hdr.sh_entsize = 4;
349     }
350 
351   /* Let the backend create the rest of the sections.  This lets the
352      backend set the right flags.  The backend will normally create
353      the .got and .plt sections.  */
354   if (bed->elf_backend_create_dynamic_sections == NULL
355       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
356     return FALSE;
357 
358   elf_hash_table (info)->dynamic_sections_created = TRUE;
359 
360   return TRUE;
361 }
362 
363 /* Create dynamic sections when linking against a dynamic object.  */
364 
365 bfd_boolean
366 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
367 {
368   flagword flags, pltflags;
369   struct elf_link_hash_entry *h;
370   asection *s;
371   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
372   struct elf_link_hash_table *htab = elf_hash_table (info);
373 
374   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375      .rel[a].bss sections.  */
376   flags = bed->dynamic_sec_flags;
377 
378   pltflags = flags;
379   if (bed->plt_not_loaded)
380     /* We do not clear SEC_ALLOC here because we still want the OS to
381        allocate space for the section; it's just that there's nothing
382        to read in from the object file.  */
383     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
384   else
385     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
386   if (bed->plt_readonly)
387     pltflags |= SEC_READONLY;
388 
389   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
390   if (s == NULL
391       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
392     return FALSE;
393   htab->splt = s;
394 
395   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
396      .plt section.  */
397   if (bed->want_plt_sym)
398     {
399       h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 				       "_PROCEDURE_LINKAGE_TABLE_");
401       elf_hash_table (info)->hplt = h;
402       if (h == NULL)
403 	return FALSE;
404     }
405 
406   s = bfd_make_section_anyway_with_flags (abfd,
407 					  (bed->rela_plts_and_copies_p
408 					   ? ".rela.plt" : ".rel.plt"),
409 					  flags | SEC_READONLY);
410   if (s == NULL
411       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
412     return FALSE;
413   htab->srelplt = s;
414 
415   if (! _bfd_elf_create_got_section (abfd, info))
416     return FALSE;
417 
418   if (bed->want_dynbss)
419     {
420       /* The .dynbss section is a place to put symbols which are defined
421 	 by dynamic objects, are referenced by regular objects, and are
422 	 not functions.  We must allocate space for them in the process
423 	 image and use a R_*_COPY reloc to tell the dynamic linker to
424 	 initialize them at run time.  The linker script puts the .dynbss
425 	 section into the .bss section of the final image.  */
426       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 					      (SEC_ALLOC | SEC_LINKER_CREATED));
428       if (s == NULL)
429 	return FALSE;
430 
431       /* The .rel[a].bss section holds copy relocs.  This section is not
432 	 normally needed.  We need to create it here, though, so that the
433 	 linker will map it to an output section.  We can't just create it
434 	 only if we need it, because we will not know whether we need it
435 	 until we have seen all the input files, and the first time the
436 	 main linker code calls BFD after examining all the input files
437 	 (size_dynamic_sections) the input sections have already been
438 	 mapped to the output sections.  If the section turns out not to
439 	 be needed, we can discard it later.  We will never need this
440 	 section when generating a shared object, since they do not use
441 	 copy relocs.  */
442       if (! bfd_link_pic (info))
443 	{
444 	  s = bfd_make_section_anyway_with_flags (abfd,
445 						  (bed->rela_plts_and_copies_p
446 						   ? ".rela.bss" : ".rel.bss"),
447 						  flags | SEC_READONLY);
448 	  if (s == NULL
449 	      || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
450 	    return FALSE;
451 	}
452     }
453 
454   return TRUE;
455 }
456 
457 /* Record a new dynamic symbol.  We record the dynamic symbols as we
458    read the input files, since we need to have a list of all of them
459    before we can determine the final sizes of the output sections.
460    Note that we may actually call this function even though we are not
461    going to output any dynamic symbols; in some cases we know that a
462    symbol should be in the dynamic symbol table, but only if there is
463    one.  */
464 
465 bfd_boolean
466 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
467 				    struct elf_link_hash_entry *h)
468 {
469   if (h->dynindx == -1)
470     {
471       struct elf_strtab_hash *dynstr;
472       char *p;
473       const char *name;
474       size_t indx;
475 
476       /* XXX: The ABI draft says the linker must turn hidden and
477 	 internal symbols into STB_LOCAL symbols when producing the
478 	 DSO. However, if ld.so honors st_other in the dynamic table,
479 	 this would not be necessary.  */
480       switch (ELF_ST_VISIBILITY (h->other))
481 	{
482 	case STV_INTERNAL:
483 	case STV_HIDDEN:
484 	  if (h->root.type != bfd_link_hash_undefined
485 	      && h->root.type != bfd_link_hash_undefweak)
486 	    {
487 	      h->forced_local = 1;
488 	      if (!elf_hash_table (info)->is_relocatable_executable)
489 		return TRUE;
490 	    }
491 
492 	default:
493 	  break;
494 	}
495 
496       h->dynindx = elf_hash_table (info)->dynsymcount;
497       ++elf_hash_table (info)->dynsymcount;
498 
499       dynstr = elf_hash_table (info)->dynstr;
500       if (dynstr == NULL)
501 	{
502 	  /* Create a strtab to hold the dynamic symbol names.  */
503 	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
504 	  if (dynstr == NULL)
505 	    return FALSE;
506 	}
507 
508       /* We don't put any version information in the dynamic string
509 	 table.  */
510       name = h->root.root.string;
511       p = strchr (name, ELF_VER_CHR);
512       if (p != NULL)
513 	/* We know that the p points into writable memory.  In fact,
514 	   there are only a few symbols that have read-only names, being
515 	   those like _GLOBAL_OFFSET_TABLE_ that are created specially
516 	   by the backends.  Most symbols will have names pointing into
517 	   an ELF string table read from a file, or to objalloc memory.  */
518 	*p = 0;
519 
520       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
521 
522       if (p != NULL)
523 	*p = ELF_VER_CHR;
524 
525       if (indx == (size_t) -1)
526 	return FALSE;
527       h->dynstr_index = indx;
528     }
529 
530   return TRUE;
531 }
532 
533 /* Mark a symbol dynamic.  */
534 
535 static void
536 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
537 				  struct elf_link_hash_entry *h,
538 				  Elf_Internal_Sym *sym)
539 {
540   struct bfd_elf_dynamic_list *d = info->dynamic_list;
541 
542   /* It may be called more than once on the same H.  */
543   if(h->dynamic || bfd_link_relocatable (info))
544     return;
545 
546   if ((info->dynamic_data
547        && (h->type == STT_OBJECT
548 	   || h->type == STT_COMMON
549 	   || (sym != NULL
550 	       && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
551 		   || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
552       || (d != NULL
553 	  && h->root.type == bfd_link_hash_new
554 	  && (*d->match) (&d->head, NULL, h->root.root.string)))
555     h->dynamic = 1;
556 }
557 
558 /* Record an assignment to a symbol made by a linker script.  We need
559    this in case some dynamic object refers to this symbol.  */
560 
561 bfd_boolean
562 bfd_elf_record_link_assignment (bfd *output_bfd,
563 				struct bfd_link_info *info,
564 				const char *name,
565 				bfd_boolean provide,
566 				bfd_boolean hidden)
567 {
568   struct elf_link_hash_entry *h, *hv;
569   struct elf_link_hash_table *htab;
570   const struct elf_backend_data *bed;
571 
572   if (!is_elf_hash_table (info->hash))
573     return TRUE;
574 
575   htab = elf_hash_table (info);
576   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
577   if (h == NULL)
578     return provide;
579 
580   if (h->versioned == unknown)
581     {
582       /* Set versioned if symbol version is unknown.  */
583       char *version = strrchr (name, ELF_VER_CHR);
584       if (version)
585 	{
586 	  if (version > name && version[-1] != ELF_VER_CHR)
587 	    h->versioned = versioned_hidden;
588 	  else
589 	    h->versioned = versioned;
590 	}
591     }
592 
593   switch (h->root.type)
594     {
595     case bfd_link_hash_defined:
596     case bfd_link_hash_defweak:
597     case bfd_link_hash_common:
598       break;
599     case bfd_link_hash_undefweak:
600     case bfd_link_hash_undefined:
601       /* Since we're defining the symbol, don't let it seem to have not
602 	 been defined.  record_dynamic_symbol and size_dynamic_sections
603 	 may depend on this.  */
604       h->root.type = bfd_link_hash_new;
605       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
606 	bfd_link_repair_undef_list (&htab->root);
607       break;
608     case bfd_link_hash_new:
609       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
610       h->non_elf = 0;
611       break;
612     case bfd_link_hash_indirect:
613       /* We had a versioned symbol in a dynamic library.  We make the
614 	 the versioned symbol point to this one.  */
615       bed = get_elf_backend_data (output_bfd);
616       hv = h;
617       while (hv->root.type == bfd_link_hash_indirect
618 	     || hv->root.type == bfd_link_hash_warning)
619 	hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
620       /* We don't need to update h->root.u since linker will set them
621 	 later.  */
622       h->root.type = bfd_link_hash_undefined;
623       hv->root.type = bfd_link_hash_indirect;
624       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
625       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
626       break;
627     case bfd_link_hash_warning:
628       abort ();
629       break;
630     }
631 
632   /* If this symbol is being provided by the linker script, and it is
633      currently defined by a dynamic object, but not by a regular
634      object, then mark it as undefined so that the generic linker will
635      force the correct value.  */
636   if (provide
637       && h->def_dynamic
638       && !h->def_regular)
639     h->root.type = bfd_link_hash_undefined;
640 
641   /* If this symbol is not being provided by the linker script, and it is
642      currently defined by a dynamic object, but not by a regular object,
643      then clear out any version information because the symbol will not be
644      associated with the dynamic object any more.  */
645   if (!provide
646       && h->def_dynamic
647       && !h->def_regular)
648     h->verinfo.verdef = NULL;
649 
650   h->def_regular = 1;
651 
652   if (hidden)
653     {
654       bed = get_elf_backend_data (output_bfd);
655       if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
656 	h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
657       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
658     }
659 
660   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
661      and executables.  */
662   if (!bfd_link_relocatable (info)
663       && h->dynindx != -1
664       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
665 	  || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
666     h->forced_local = 1;
667 
668   if ((h->def_dynamic
669        || h->ref_dynamic
670        || bfd_link_dll (info)
671        || elf_hash_table (info)->is_relocatable_executable)
672       && h->dynindx == -1)
673     {
674       if (! bfd_elf_link_record_dynamic_symbol (info, h))
675 	return FALSE;
676 
677       /* If this is a weak defined symbol, and we know a corresponding
678 	 real symbol from the same dynamic object, make sure the real
679 	 symbol is also made into a dynamic symbol.  */
680       if (h->u.weakdef != NULL
681 	  && h->u.weakdef->dynindx == -1)
682 	{
683 	  if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
684 	    return FALSE;
685 	}
686     }
687 
688   return TRUE;
689 }
690 
691 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
692    success, and 2 on a failure caused by attempting to record a symbol
693    in a discarded section, eg. a discarded link-once section symbol.  */
694 
695 int
696 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
697 					  bfd *input_bfd,
698 					  long input_indx)
699 {
700   bfd_size_type amt;
701   struct elf_link_local_dynamic_entry *entry;
702   struct elf_link_hash_table *eht;
703   struct elf_strtab_hash *dynstr;
704   size_t dynstr_index;
705   char *name;
706   Elf_External_Sym_Shndx eshndx;
707   char esym[sizeof (Elf64_External_Sym)];
708 
709   if (! is_elf_hash_table (info->hash))
710     return 0;
711 
712   /* See if the entry exists already.  */
713   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
714     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
715       return 1;
716 
717   amt = sizeof (*entry);
718   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
719   if (entry == NULL)
720     return 0;
721 
722   /* Go find the symbol, so that we can find it's name.  */
723   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
724 			     1, input_indx, &entry->isym, esym, &eshndx))
725     {
726       bfd_release (input_bfd, entry);
727       return 0;
728     }
729 
730   if (entry->isym.st_shndx != SHN_UNDEF
731       && entry->isym.st_shndx < SHN_LORESERVE)
732     {
733       asection *s;
734 
735       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
736       if (s == NULL || bfd_is_abs_section (s->output_section))
737 	{
738 	  /* We can still bfd_release here as nothing has done another
739 	     bfd_alloc.  We can't do this later in this function.  */
740 	  bfd_release (input_bfd, entry);
741 	  return 2;
742 	}
743     }
744 
745   name = (bfd_elf_string_from_elf_section
746 	  (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
747 	   entry->isym.st_name));
748 
749   dynstr = elf_hash_table (info)->dynstr;
750   if (dynstr == NULL)
751     {
752       /* Create a strtab to hold the dynamic symbol names.  */
753       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
754       if (dynstr == NULL)
755 	return 0;
756     }
757 
758   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
759   if (dynstr_index == (size_t) -1)
760     return 0;
761   entry->isym.st_name = dynstr_index;
762 
763   eht = elf_hash_table (info);
764 
765   entry->next = eht->dynlocal;
766   eht->dynlocal = entry;
767   entry->input_bfd = input_bfd;
768   entry->input_indx = input_indx;
769   eht->dynsymcount++;
770 
771   /* Whatever binding the symbol had before, it's now local.  */
772   entry->isym.st_info
773     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
774 
775   /* The dynindx will be set at the end of size_dynamic_sections.  */
776 
777   return 1;
778 }
779 
780 /* Return the dynindex of a local dynamic symbol.  */
781 
782 long
783 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
784 				    bfd *input_bfd,
785 				    long input_indx)
786 {
787   struct elf_link_local_dynamic_entry *e;
788 
789   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
790     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
791       return e->dynindx;
792   return -1;
793 }
794 
795 /* This function is used to renumber the dynamic symbols, if some of
796    them are removed because they are marked as local.  This is called
797    via elf_link_hash_traverse.  */
798 
799 static bfd_boolean
800 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
801 				      void *data)
802 {
803   size_t *count = (size_t *) data;
804 
805   if (h->forced_local)
806     return TRUE;
807 
808   if (h->dynindx != -1)
809     h->dynindx = ++(*count);
810 
811   return TRUE;
812 }
813 
814 
815 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
816    STB_LOCAL binding.  */
817 
818 static bfd_boolean
819 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
820 					    void *data)
821 {
822   size_t *count = (size_t *) data;
823 
824   if (!h->forced_local)
825     return TRUE;
826 
827   if (h->dynindx != -1)
828     h->dynindx = ++(*count);
829 
830   return TRUE;
831 }
832 
833 /* Return true if the dynamic symbol for a given section should be
834    omitted when creating a shared library.  */
835 bfd_boolean
836 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
837 				   struct bfd_link_info *info,
838 				   asection *p)
839 {
840   struct elf_link_hash_table *htab;
841   asection *ip;
842 
843   switch (elf_section_data (p)->this_hdr.sh_type)
844     {
845     case SHT_PROGBITS:
846     case SHT_NOBITS:
847       /* If sh_type is yet undecided, assume it could be
848 	 SHT_PROGBITS/SHT_NOBITS.  */
849     case SHT_NULL:
850       htab = elf_hash_table (info);
851       if (p == htab->tls_sec)
852 	return FALSE;
853 
854       if (htab->text_index_section != NULL)
855 	return p != htab->text_index_section && p != htab->data_index_section;
856 
857       return (htab->dynobj != NULL
858 	      && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
859 	      && ip->output_section == p);
860 
861       /* There shouldn't be section relative relocations
862 	 against any other section.  */
863     default:
864       return TRUE;
865     }
866 }
867 
868 /* Assign dynsym indices.  In a shared library we generate a section
869    symbol for each output section, which come first.  Next come symbols
870    which have been forced to local binding.  Then all of the back-end
871    allocated local dynamic syms, followed by the rest of the global
872    symbols.  */
873 
874 static unsigned long
875 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
876 				struct bfd_link_info *info,
877 				unsigned long *section_sym_count)
878 {
879   unsigned long dynsymcount = 0;
880 
881   if (bfd_link_pic (info)
882       || elf_hash_table (info)->is_relocatable_executable)
883     {
884       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
885       asection *p;
886       for (p = output_bfd->sections; p ; p = p->next)
887 	if ((p->flags & SEC_EXCLUDE) == 0
888 	    && (p->flags & SEC_ALLOC) != 0
889 	    && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
890 	  elf_section_data (p)->dynindx = ++dynsymcount;
891 	else
892 	  elf_section_data (p)->dynindx = 0;
893     }
894   *section_sym_count = dynsymcount;
895 
896   elf_link_hash_traverse (elf_hash_table (info),
897 			  elf_link_renumber_local_hash_table_dynsyms,
898 			  &dynsymcount);
899 
900   if (elf_hash_table (info)->dynlocal)
901     {
902       struct elf_link_local_dynamic_entry *p;
903       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
904 	p->dynindx = ++dynsymcount;
905     }
906 
907   elf_link_hash_traverse (elf_hash_table (info),
908 			  elf_link_renumber_hash_table_dynsyms,
909 			  &dynsymcount);
910 
911   /* There is an unused NULL entry at the head of the table which we
912      must account for in our count even if the table is empty since it
913      is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
914      .dynamic section.  */
915   dynsymcount++;
916 
917   elf_hash_table (info)->dynsymcount = dynsymcount;
918   return dynsymcount;
919 }
920 
921 /* Merge st_other field.  */
922 
923 static void
924 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
925 		    const Elf_Internal_Sym *isym, asection *sec,
926 		    bfd_boolean definition, bfd_boolean dynamic)
927 {
928   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
929 
930   /* If st_other has a processor-specific meaning, specific
931      code might be needed here.  */
932   if (bed->elf_backend_merge_symbol_attribute)
933     (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
934 						dynamic);
935 
936   if (!dynamic)
937     {
938       unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
939       unsigned hvis = ELF_ST_VISIBILITY (h->other);
940 
941       /* Keep the most constraining visibility.  Leave the remainder
942 	 of the st_other field to elf_backend_merge_symbol_attribute.  */
943       if (symvis - 1 < hvis - 1)
944 	h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
945     }
946   else if (definition
947 	   && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
948 	   && (sec->flags & SEC_READONLY) == 0)
949     h->protected_def = 1;
950 }
951 
952 /* This function is called when we want to merge a new symbol with an
953    existing symbol.  It handles the various cases which arise when we
954    find a definition in a dynamic object, or when there is already a
955    definition in a dynamic object.  The new symbol is described by
956    NAME, SYM, PSEC, and PVALUE.  We set SYM_HASH to the hash table
957    entry.  We set POLDBFD to the old symbol's BFD.  We set POLD_WEAK
958    if the old symbol was weak.  We set POLD_ALIGNMENT to the alignment
959    of an old common symbol.  We set OVERRIDE if the old symbol is
960    overriding a new definition.  We set TYPE_CHANGE_OK if it is OK for
961    the type to change.  We set SIZE_CHANGE_OK if it is OK for the size
962    to change.  By OK to change, we mean that we shouldn't warn if the
963    type or size does change.  */
964 
965 static bfd_boolean
966 _bfd_elf_merge_symbol (bfd *abfd,
967 		       struct bfd_link_info *info,
968 		       const char *name,
969 		       Elf_Internal_Sym *sym,
970 		       asection **psec,
971 		       bfd_vma *pvalue,
972 		       struct elf_link_hash_entry **sym_hash,
973 		       bfd **poldbfd,
974 		       bfd_boolean *pold_weak,
975 		       unsigned int *pold_alignment,
976 		       bfd_boolean *skip,
977 		       bfd_boolean *override,
978 		       bfd_boolean *type_change_ok,
979 		       bfd_boolean *size_change_ok,
980 		       bfd_boolean *matched)
981 {
982   asection *sec, *oldsec;
983   struct elf_link_hash_entry *h;
984   struct elf_link_hash_entry *hi;
985   struct elf_link_hash_entry *flip;
986   int bind;
987   bfd *oldbfd;
988   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
989   bfd_boolean newweak, oldweak, newfunc, oldfunc;
990   const struct elf_backend_data *bed;
991   char *new_version;
992 
993   *skip = FALSE;
994   *override = FALSE;
995 
996   sec = *psec;
997   bind = ELF_ST_BIND (sym->st_info);
998 
999   if (! bfd_is_und_section (sec))
1000     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1001   else
1002     h = ((struct elf_link_hash_entry *)
1003 	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1004   if (h == NULL)
1005     return FALSE;
1006   *sym_hash = h;
1007 
1008   bed = get_elf_backend_data (abfd);
1009 
1010   /* NEW_VERSION is the symbol version of the new symbol.  */
1011   if (h->versioned != unversioned)
1012     {
1013       /* Symbol version is unknown or versioned.  */
1014       new_version = strrchr (name, ELF_VER_CHR);
1015       if (new_version)
1016 	{
1017 	  if (h->versioned == unknown)
1018 	    {
1019 	      if (new_version > name && new_version[-1] != ELF_VER_CHR)
1020 		h->versioned = versioned_hidden;
1021 	      else
1022 		h->versioned = versioned;
1023 	    }
1024 	  new_version += 1;
1025 	  if (new_version[0] == '\0')
1026 	    new_version = NULL;
1027 	}
1028       else
1029 	h->versioned = unversioned;
1030     }
1031   else
1032     new_version = NULL;
1033 
1034   /* For merging, we only care about real symbols.  But we need to make
1035      sure that indirect symbol dynamic flags are updated.  */
1036   hi = h;
1037   while (h->root.type == bfd_link_hash_indirect
1038 	 || h->root.type == bfd_link_hash_warning)
1039     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1040 
1041   if (!*matched)
1042     {
1043       if (hi == h || h->root.type == bfd_link_hash_new)
1044 	*matched = TRUE;
1045       else
1046 	{
1047 	  /* OLD_HIDDEN is true if the existing symbol is only visible
1048 	     to the symbol with the same symbol version.  NEW_HIDDEN is
1049 	     true if the new symbol is only visible to the symbol with
1050 	     the same symbol version.  */
1051 	  bfd_boolean old_hidden = h->versioned == versioned_hidden;
1052 	  bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1053 	  if (!old_hidden && !new_hidden)
1054 	    /* The new symbol matches the existing symbol if both
1055 	       aren't hidden.  */
1056 	    *matched = TRUE;
1057 	  else
1058 	    {
1059 	      /* OLD_VERSION is the symbol version of the existing
1060 		 symbol. */
1061 	      char *old_version;
1062 
1063 	      if (h->versioned >= versioned)
1064 		old_version = strrchr (h->root.root.string,
1065 				       ELF_VER_CHR) + 1;
1066 	      else
1067 		 old_version = NULL;
1068 
1069 	      /* The new symbol matches the existing symbol if they
1070 		 have the same symbol version.  */
1071 	      *matched = (old_version == new_version
1072 			  || (old_version != NULL
1073 			      && new_version != NULL
1074 			      && strcmp (old_version, new_version) == 0));
1075 	    }
1076 	}
1077     }
1078 
1079   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1080      existing symbol.  */
1081 
1082   oldbfd = NULL;
1083   oldsec = NULL;
1084   switch (h->root.type)
1085     {
1086     default:
1087       break;
1088 
1089     case bfd_link_hash_undefined:
1090     case bfd_link_hash_undefweak:
1091       oldbfd = h->root.u.undef.abfd;
1092       break;
1093 
1094     case bfd_link_hash_defined:
1095     case bfd_link_hash_defweak:
1096       oldbfd = h->root.u.def.section->owner;
1097       oldsec = h->root.u.def.section;
1098       break;
1099 
1100     case bfd_link_hash_common:
1101       oldbfd = h->root.u.c.p->section->owner;
1102       oldsec = h->root.u.c.p->section;
1103       if (pold_alignment)
1104 	*pold_alignment = h->root.u.c.p->alignment_power;
1105       break;
1106     }
1107   if (poldbfd && *poldbfd == NULL)
1108     *poldbfd = oldbfd;
1109 
1110   /* Differentiate strong and weak symbols.  */
1111   newweak = bind == STB_WEAK;
1112   oldweak = (h->root.type == bfd_link_hash_defweak
1113 	     || h->root.type == bfd_link_hash_undefweak);
1114   if (pold_weak)
1115     *pold_weak = oldweak;
1116 
1117   /* This code is for coping with dynamic objects, and is only useful
1118      if we are doing an ELF link.  */
1119   if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1120     return TRUE;
1121 
1122   /* We have to check it for every instance since the first few may be
1123      references and not all compilers emit symbol type for undefined
1124      symbols.  */
1125   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1126 
1127   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1128      respectively, is from a dynamic object.  */
1129 
1130   newdyn = (abfd->flags & DYNAMIC) != 0;
1131 
1132   /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1133      syms and defined syms in dynamic libraries respectively.
1134      ref_dynamic on the other hand can be set for a symbol defined in
1135      a dynamic library, and def_dynamic may not be set;  When the
1136      definition in a dynamic lib is overridden by a definition in the
1137      executable use of the symbol in the dynamic lib becomes a
1138      reference to the executable symbol.  */
1139   if (newdyn)
1140     {
1141       if (bfd_is_und_section (sec))
1142 	{
1143 	  if (bind != STB_WEAK)
1144 	    {
1145 	      h->ref_dynamic_nonweak = 1;
1146 	      hi->ref_dynamic_nonweak = 1;
1147 	    }
1148 	}
1149       else
1150 	{
1151 	  /* Update the existing symbol only if they match. */
1152 	  if (*matched)
1153 	    h->dynamic_def = 1;
1154 	  hi->dynamic_def = 1;
1155 	}
1156     }
1157 
1158   /* If we just created the symbol, mark it as being an ELF symbol.
1159      Other than that, there is nothing to do--there is no merge issue
1160      with a newly defined symbol--so we just return.  */
1161 
1162   if (h->root.type == bfd_link_hash_new)
1163     {
1164       h->non_elf = 0;
1165       return TRUE;
1166     }
1167 
1168   /* In cases involving weak versioned symbols, we may wind up trying
1169      to merge a symbol with itself.  Catch that here, to avoid the
1170      confusion that results if we try to override a symbol with
1171      itself.  The additional tests catch cases like
1172      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1173      dynamic object, which we do want to handle here.  */
1174   if (abfd == oldbfd
1175       && (newweak || oldweak)
1176       && ((abfd->flags & DYNAMIC) == 0
1177 	  || !h->def_regular))
1178     return TRUE;
1179 
1180   olddyn = FALSE;
1181   if (oldbfd != NULL)
1182     olddyn = (oldbfd->flags & DYNAMIC) != 0;
1183   else if (oldsec != NULL)
1184     {
1185       /* This handles the special SHN_MIPS_{TEXT,DATA} section
1186 	 indices used by MIPS ELF.  */
1187       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1188     }
1189 
1190   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1191      respectively, appear to be a definition rather than reference.  */
1192 
1193   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1194 
1195   olddef = (h->root.type != bfd_link_hash_undefined
1196 	    && h->root.type != bfd_link_hash_undefweak
1197 	    && h->root.type != bfd_link_hash_common);
1198 
1199   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1200      respectively, appear to be a function.  */
1201 
1202   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1203 	     && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1204 
1205   oldfunc = (h->type != STT_NOTYPE
1206 	     && bed->is_function_type (h->type));
1207 
1208   /* If creating a default indirect symbol ("foo" or "foo@") from a
1209      dynamic versioned definition ("foo@@") skip doing so if there is
1210      an existing regular definition with a different type.  We don't
1211      want, for example, a "time" variable in the executable overriding
1212      a "time" function in a shared library.  */
1213   if (pold_alignment == NULL
1214       && newdyn
1215       && newdef
1216       && !olddyn
1217       && (olddef || h->root.type == bfd_link_hash_common)
1218       && ELF_ST_TYPE (sym->st_info) != h->type
1219       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1220       && h->type != STT_NOTYPE
1221       && !(newfunc && oldfunc))
1222     {
1223       *skip = TRUE;
1224       return TRUE;
1225     }
1226 
1227   /* Check TLS symbols.  We don't check undefined symbols introduced
1228      by "ld -u" which have no type (and oldbfd NULL), and we don't
1229      check symbols from plugins because they also have no type.  */
1230   if (oldbfd != NULL
1231       && (oldbfd->flags & BFD_PLUGIN) == 0
1232       && (abfd->flags & BFD_PLUGIN) == 0
1233       && ELF_ST_TYPE (sym->st_info) != h->type
1234       && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1235     {
1236       bfd *ntbfd, *tbfd;
1237       bfd_boolean ntdef, tdef;
1238       asection *ntsec, *tsec;
1239 
1240       if (h->type == STT_TLS)
1241 	{
1242 	  ntbfd = abfd;
1243 	  ntsec = sec;
1244 	  ntdef = newdef;
1245 	  tbfd = oldbfd;
1246 	  tsec = oldsec;
1247 	  tdef = olddef;
1248 	}
1249       else
1250 	{
1251 	  ntbfd = oldbfd;
1252 	  ntsec = oldsec;
1253 	  ntdef = olddef;
1254 	  tbfd = abfd;
1255 	  tsec = sec;
1256 	  tdef = newdef;
1257 	}
1258 
1259       if (tdef && ntdef)
1260 	(*_bfd_error_handler)
1261 	  (_("%s: TLS definition in %B section %A "
1262 	     "mismatches non-TLS definition in %B section %A"),
1263 	   tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1264       else if (!tdef && !ntdef)
1265 	(*_bfd_error_handler)
1266 	  (_("%s: TLS reference in %B "
1267 	     "mismatches non-TLS reference in %B"),
1268 	   tbfd, ntbfd, h->root.root.string);
1269       else if (tdef)
1270 	(*_bfd_error_handler)
1271 	  (_("%s: TLS definition in %B section %A "
1272 	     "mismatches non-TLS reference in %B"),
1273 	   tbfd, tsec, ntbfd, h->root.root.string);
1274       else
1275 	(*_bfd_error_handler)
1276 	  (_("%s: TLS reference in %B "
1277 	     "mismatches non-TLS definition in %B section %A"),
1278 	   tbfd, ntbfd, ntsec, h->root.root.string);
1279 
1280       bfd_set_error (bfd_error_bad_value);
1281       return FALSE;
1282     }
1283 
1284   /* If the old symbol has non-default visibility, we ignore the new
1285      definition from a dynamic object.  */
1286   if (newdyn
1287       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1288       && !bfd_is_und_section (sec))
1289     {
1290       *skip = TRUE;
1291       /* Make sure this symbol is dynamic.  */
1292       h->ref_dynamic = 1;
1293       hi->ref_dynamic = 1;
1294       /* A protected symbol has external availability. Make sure it is
1295 	 recorded as dynamic.
1296 
1297 	 FIXME: Should we check type and size for protected symbol?  */
1298       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1299 	return bfd_elf_link_record_dynamic_symbol (info, h);
1300       else
1301 	return TRUE;
1302     }
1303   else if (!newdyn
1304 	   && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1305 	   && h->def_dynamic)
1306     {
1307       /* If the new symbol with non-default visibility comes from a
1308 	 relocatable file and the old definition comes from a dynamic
1309 	 object, we remove the old definition.  */
1310       if (hi->root.type == bfd_link_hash_indirect)
1311 	{
1312 	  /* Handle the case where the old dynamic definition is
1313 	     default versioned.  We need to copy the symbol info from
1314 	     the symbol with default version to the normal one if it
1315 	     was referenced before.  */
1316 	  if (h->ref_regular)
1317 	    {
1318 	      hi->root.type = h->root.type;
1319 	      h->root.type = bfd_link_hash_indirect;
1320 	      (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1321 
1322 	      h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1323 	      if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1324 		{
1325 		  /* If the new symbol is hidden or internal, completely undo
1326 		     any dynamic link state.  */
1327 		  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1328 		  h->forced_local = 0;
1329 		  h->ref_dynamic = 0;
1330 		}
1331 	      else
1332 		h->ref_dynamic = 1;
1333 
1334 	      h->def_dynamic = 0;
1335 	      /* FIXME: Should we check type and size for protected symbol?  */
1336 	      h->size = 0;
1337 	      h->type = 0;
1338 
1339 	      h = hi;
1340 	    }
1341 	  else
1342 	    h = hi;
1343 	}
1344 
1345       /* If the old symbol was undefined before, then it will still be
1346 	 on the undefs list.  If the new symbol is undefined or
1347 	 common, we can't make it bfd_link_hash_new here, because new
1348 	 undefined or common symbols will be added to the undefs list
1349 	 by _bfd_generic_link_add_one_symbol.  Symbols may not be
1350 	 added twice to the undefs list.  Also, if the new symbol is
1351 	 undefweak then we don't want to lose the strong undef.  */
1352       if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1353 	{
1354 	  h->root.type = bfd_link_hash_undefined;
1355 	  h->root.u.undef.abfd = abfd;
1356 	}
1357       else
1358 	{
1359 	  h->root.type = bfd_link_hash_new;
1360 	  h->root.u.undef.abfd = NULL;
1361 	}
1362 
1363       if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1364 	{
1365 	  /* If the new symbol is hidden or internal, completely undo
1366 	     any dynamic link state.  */
1367 	  (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1368 	  h->forced_local = 0;
1369 	  h->ref_dynamic = 0;
1370 	}
1371       else
1372 	h->ref_dynamic = 1;
1373       h->def_dynamic = 0;
1374       /* FIXME: Should we check type and size for protected symbol?  */
1375       h->size = 0;
1376       h->type = 0;
1377       return TRUE;
1378     }
1379 
1380   /* If a new weak symbol definition comes from a regular file and the
1381      old symbol comes from a dynamic library, we treat the new one as
1382      strong.  Similarly, an old weak symbol definition from a regular
1383      file is treated as strong when the new symbol comes from a dynamic
1384      library.  Further, an old weak symbol from a dynamic library is
1385      treated as strong if the new symbol is from a dynamic library.
1386      This reflects the way glibc's ld.so works.
1387 
1388      Do this before setting *type_change_ok or *size_change_ok so that
1389      we warn properly when dynamic library symbols are overridden.  */
1390 
1391   if (newdef && !newdyn && olddyn)
1392     newweak = FALSE;
1393   if (olddef && newdyn)
1394     oldweak = FALSE;
1395 
1396   /* Allow changes between different types of function symbol.  */
1397   if (newfunc && oldfunc)
1398     *type_change_ok = TRUE;
1399 
1400   /* It's OK to change the type if either the existing symbol or the
1401      new symbol is weak.  A type change is also OK if the old symbol
1402      is undefined and the new symbol is defined.  */
1403 
1404   if (oldweak
1405       || newweak
1406       || (newdef
1407 	  && h->root.type == bfd_link_hash_undefined))
1408     *type_change_ok = TRUE;
1409 
1410   /* It's OK to change the size if either the existing symbol or the
1411      new symbol is weak, or if the old symbol is undefined.  */
1412 
1413   if (*type_change_ok
1414       || h->root.type == bfd_link_hash_undefined)
1415     *size_change_ok = TRUE;
1416 
1417   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1418      symbol, respectively, appears to be a common symbol in a dynamic
1419      object.  If a symbol appears in an uninitialized section, and is
1420      not weak, and is not a function, then it may be a common symbol
1421      which was resolved when the dynamic object was created.  We want
1422      to treat such symbols specially, because they raise special
1423      considerations when setting the symbol size: if the symbol
1424      appears as a common symbol in a regular object, and the size in
1425      the regular object is larger, we must make sure that we use the
1426      larger size.  This problematic case can always be avoided in C,
1427      but it must be handled correctly when using Fortran shared
1428      libraries.
1429 
1430      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1431      likewise for OLDDYNCOMMON and OLDDEF.
1432 
1433      Note that this test is just a heuristic, and that it is quite
1434      possible to have an uninitialized symbol in a shared object which
1435      is really a definition, rather than a common symbol.  This could
1436      lead to some minor confusion when the symbol really is a common
1437      symbol in some regular object.  However, I think it will be
1438      harmless.  */
1439 
1440   if (newdyn
1441       && newdef
1442       && !newweak
1443       && (sec->flags & SEC_ALLOC) != 0
1444       && (sec->flags & SEC_LOAD) == 0
1445       && sym->st_size > 0
1446       && !newfunc)
1447     newdyncommon = TRUE;
1448   else
1449     newdyncommon = FALSE;
1450 
1451   if (olddyn
1452       && olddef
1453       && h->root.type == bfd_link_hash_defined
1454       && h->def_dynamic
1455       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1456       && (h->root.u.def.section->flags & SEC_LOAD) == 0
1457       && h->size > 0
1458       && !oldfunc)
1459     olddyncommon = TRUE;
1460   else
1461     olddyncommon = FALSE;
1462 
1463   /* We now know everything about the old and new symbols.  We ask the
1464      backend to check if we can merge them.  */
1465   if (bed->merge_symbol != NULL)
1466     {
1467       if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1468 	return FALSE;
1469       sec = *psec;
1470     }
1471 
1472   /* If both the old and the new symbols look like common symbols in a
1473      dynamic object, set the size of the symbol to the larger of the
1474      two.  */
1475 
1476   if (olddyncommon
1477       && newdyncommon
1478       && sym->st_size != h->size)
1479     {
1480       /* Since we think we have two common symbols, issue a multiple
1481 	 common warning if desired.  Note that we only warn if the
1482 	 size is different.  If the size is the same, we simply let
1483 	 the old symbol override the new one as normally happens with
1484 	 symbols defined in dynamic objects.  */
1485 
1486       (*info->callbacks->multiple_common) (info, &h->root, abfd,
1487 					   bfd_link_hash_common, sym->st_size);
1488       if (sym->st_size > h->size)
1489 	h->size = sym->st_size;
1490 
1491       *size_change_ok = TRUE;
1492     }
1493 
1494   /* If we are looking at a dynamic object, and we have found a
1495      definition, we need to see if the symbol was already defined by
1496      some other object.  If so, we want to use the existing
1497      definition, and we do not want to report a multiple symbol
1498      definition error; we do this by clobbering *PSEC to be
1499      bfd_und_section_ptr.
1500 
1501      We treat a common symbol as a definition if the symbol in the
1502      shared library is a function, since common symbols always
1503      represent variables; this can cause confusion in principle, but
1504      any such confusion would seem to indicate an erroneous program or
1505      shared library.  We also permit a common symbol in a regular
1506      object to override a weak symbol in a shared object.  A common
1507      symbol in executable also overrides a symbol in a shared object.  */
1508 
1509   if (newdyn
1510       && newdef
1511       && (olddef
1512 	  || (h->root.type == bfd_link_hash_common
1513 	      && (newweak
1514 		  || newfunc
1515 		  || (!olddyn && bfd_link_executable (info))))))
1516     {
1517       *override = TRUE;
1518       newdef = FALSE;
1519       newdyncommon = FALSE;
1520 
1521       *psec = sec = bfd_und_section_ptr;
1522       *size_change_ok = TRUE;
1523 
1524       /* If we get here when the old symbol is a common symbol, then
1525 	 we are explicitly letting it override a weak symbol or
1526 	 function in a dynamic object, and we don't want to warn about
1527 	 a type change.  If the old symbol is a defined symbol, a type
1528 	 change warning may still be appropriate.  */
1529 
1530       if (h->root.type == bfd_link_hash_common)
1531 	*type_change_ok = TRUE;
1532     }
1533 
1534   /* Handle the special case of an old common symbol merging with a
1535      new symbol which looks like a common symbol in a shared object.
1536      We change *PSEC and *PVALUE to make the new symbol look like a
1537      common symbol, and let _bfd_generic_link_add_one_symbol do the
1538      right thing.  */
1539 
1540   if (newdyncommon
1541       && h->root.type == bfd_link_hash_common)
1542     {
1543       *override = TRUE;
1544       newdef = FALSE;
1545       newdyncommon = FALSE;
1546       *pvalue = sym->st_size;
1547       *psec = sec = bed->common_section (oldsec);
1548       *size_change_ok = TRUE;
1549     }
1550 
1551   /* Skip weak definitions of symbols that are already defined.  */
1552   if (newdef && olddef && newweak)
1553     {
1554       /* Don't skip new non-IR weak syms.  */
1555       if (!(oldbfd != NULL
1556 	    && (oldbfd->flags & BFD_PLUGIN) != 0
1557 	    && (abfd->flags & BFD_PLUGIN) == 0))
1558 	{
1559 	  newdef = FALSE;
1560 	  *skip = TRUE;
1561 	}
1562 
1563       /* Merge st_other.  If the symbol already has a dynamic index,
1564 	 but visibility says it should not be visible, turn it into a
1565 	 local symbol.  */
1566       elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1567       if (h->dynindx != -1)
1568 	switch (ELF_ST_VISIBILITY (h->other))
1569 	  {
1570 	  case STV_INTERNAL:
1571 	  case STV_HIDDEN:
1572 	    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1573 	    break;
1574 	  }
1575     }
1576 
1577   /* If the old symbol is from a dynamic object, and the new symbol is
1578      a definition which is not from a dynamic object, then the new
1579      symbol overrides the old symbol.  Symbols from regular files
1580      always take precedence over symbols from dynamic objects, even if
1581      they are defined after the dynamic object in the link.
1582 
1583      As above, we again permit a common symbol in a regular object to
1584      override a definition in a shared object if the shared object
1585      symbol is a function or is weak.  */
1586 
1587   flip = NULL;
1588   if (!newdyn
1589       && (newdef
1590 	  || (bfd_is_com_section (sec)
1591 	      && (oldweak || oldfunc)))
1592       && olddyn
1593       && olddef
1594       && h->def_dynamic)
1595     {
1596       /* Change the hash table entry to undefined, and let
1597 	 _bfd_generic_link_add_one_symbol do the right thing with the
1598 	 new definition.  */
1599 
1600       h->root.type = bfd_link_hash_undefined;
1601       h->root.u.undef.abfd = h->root.u.def.section->owner;
1602       *size_change_ok = TRUE;
1603 
1604       olddef = FALSE;
1605       olddyncommon = FALSE;
1606 
1607       /* We again permit a type change when a common symbol may be
1608 	 overriding a function.  */
1609 
1610       if (bfd_is_com_section (sec))
1611 	{
1612 	  if (oldfunc)
1613 	    {
1614 	      /* If a common symbol overrides a function, make sure
1615 		 that it isn't defined dynamically nor has type
1616 		 function.  */
1617 	      h->def_dynamic = 0;
1618 	      h->type = STT_NOTYPE;
1619 	    }
1620 	  *type_change_ok = TRUE;
1621 	}
1622 
1623       if (hi->root.type == bfd_link_hash_indirect)
1624 	flip = hi;
1625       else
1626 	/* This union may have been set to be non-NULL when this symbol
1627 	   was seen in a dynamic object.  We must force the union to be
1628 	   NULL, so that it is correct for a regular symbol.  */
1629 	h->verinfo.vertree = NULL;
1630     }
1631 
1632   /* Handle the special case of a new common symbol merging with an
1633      old symbol that looks like it might be a common symbol defined in
1634      a shared object.  Note that we have already handled the case in
1635      which a new common symbol should simply override the definition
1636      in the shared library.  */
1637 
1638   if (! newdyn
1639       && bfd_is_com_section (sec)
1640       && olddyncommon)
1641     {
1642       /* It would be best if we could set the hash table entry to a
1643 	 common symbol, but we don't know what to use for the section
1644 	 or the alignment.  */
1645       (*info->callbacks->multiple_common) (info, &h->root, abfd,
1646 					   bfd_link_hash_common, sym->st_size);
1647 
1648       /* If the presumed common symbol in the dynamic object is
1649 	 larger, pretend that the new symbol has its size.  */
1650 
1651       if (h->size > *pvalue)
1652 	*pvalue = h->size;
1653 
1654       /* We need to remember the alignment required by the symbol
1655 	 in the dynamic object.  */
1656       BFD_ASSERT (pold_alignment);
1657       *pold_alignment = h->root.u.def.section->alignment_power;
1658 
1659       olddef = FALSE;
1660       olddyncommon = FALSE;
1661 
1662       h->root.type = bfd_link_hash_undefined;
1663       h->root.u.undef.abfd = h->root.u.def.section->owner;
1664 
1665       *size_change_ok = TRUE;
1666       *type_change_ok = TRUE;
1667 
1668       if (hi->root.type == bfd_link_hash_indirect)
1669 	flip = hi;
1670       else
1671 	h->verinfo.vertree = NULL;
1672     }
1673 
1674   if (flip != NULL)
1675     {
1676       /* Handle the case where we had a versioned symbol in a dynamic
1677 	 library and now find a definition in a normal object.  In this
1678 	 case, we make the versioned symbol point to the normal one.  */
1679       flip->root.type = h->root.type;
1680       flip->root.u.undef.abfd = h->root.u.undef.abfd;
1681       h->root.type = bfd_link_hash_indirect;
1682       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1683       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1684       if (h->def_dynamic)
1685 	{
1686 	  h->def_dynamic = 0;
1687 	  flip->ref_dynamic = 1;
1688 	}
1689     }
1690 
1691   return TRUE;
1692 }
1693 
1694 /* This function is called to create an indirect symbol from the
1695    default for the symbol with the default version if needed. The
1696    symbol is described by H, NAME, SYM, SEC, and VALUE.  We
1697    set DYNSYM if the new indirect symbol is dynamic.  */
1698 
1699 static bfd_boolean
1700 _bfd_elf_add_default_symbol (bfd *abfd,
1701 			     struct bfd_link_info *info,
1702 			     struct elf_link_hash_entry *h,
1703 			     const char *name,
1704 			     Elf_Internal_Sym *sym,
1705 			     asection *sec,
1706 			     bfd_vma value,
1707 			     bfd **poldbfd,
1708 			     bfd_boolean *dynsym)
1709 {
1710   bfd_boolean type_change_ok;
1711   bfd_boolean size_change_ok;
1712   bfd_boolean skip;
1713   char *shortname;
1714   struct elf_link_hash_entry *hi;
1715   struct bfd_link_hash_entry *bh;
1716   const struct elf_backend_data *bed;
1717   bfd_boolean collect;
1718   bfd_boolean dynamic;
1719   bfd_boolean override;
1720   char *p;
1721   size_t len, shortlen;
1722   asection *tmp_sec;
1723   bfd_boolean matched;
1724 
1725   if (h->versioned == unversioned || h->versioned == versioned_hidden)
1726     return TRUE;
1727 
1728   /* If this symbol has a version, and it is the default version, we
1729      create an indirect symbol from the default name to the fully
1730      decorated name.  This will cause external references which do not
1731      specify a version to be bound to this version of the symbol.  */
1732   p = strchr (name, ELF_VER_CHR);
1733   if (h->versioned == unknown)
1734     {
1735       if (p == NULL)
1736 	{
1737 	  h->versioned = unversioned;
1738 	  return TRUE;
1739 	}
1740       else
1741 	{
1742 	  if (p[1] != ELF_VER_CHR)
1743 	    {
1744 	      h->versioned = versioned_hidden;
1745 	      return TRUE;
1746 	    }
1747 	  else
1748 	    h->versioned = versioned;
1749 	}
1750     }
1751   else
1752     {
1753       /* PR ld/19073: We may see an unversioned definition after the
1754 	 default version.  */
1755       if (p == NULL)
1756 	return TRUE;
1757     }
1758 
1759   bed = get_elf_backend_data (abfd);
1760   collect = bed->collect;
1761   dynamic = (abfd->flags & DYNAMIC) != 0;
1762 
1763   shortlen = p - name;
1764   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1765   if (shortname == NULL)
1766     return FALSE;
1767   memcpy (shortname, name, shortlen);
1768   shortname[shortlen] = '\0';
1769 
1770   /* We are going to create a new symbol.  Merge it with any existing
1771      symbol with this name.  For the purposes of the merge, act as
1772      though we were defining the symbol we just defined, although we
1773      actually going to define an indirect symbol.  */
1774   type_change_ok = FALSE;
1775   size_change_ok = FALSE;
1776   matched = TRUE;
1777   tmp_sec = sec;
1778   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1779 			      &hi, poldbfd, NULL, NULL, &skip, &override,
1780 			      &type_change_ok, &size_change_ok, &matched))
1781     return FALSE;
1782 
1783   if (skip)
1784     goto nondefault;
1785 
1786   if (hi->def_regular)
1787     {
1788       /* If the undecorated symbol will have a version added by a
1789 	 script different to H, then don't indirect to/from the
1790 	 undecorated symbol.  This isn't ideal because we may not yet
1791 	 have seen symbol versions, if given by a script on the
1792 	 command line rather than via --version-script.  */
1793       if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1794 	{
1795 	  bfd_boolean hide;
1796 
1797 	  hi->verinfo.vertree
1798 	    = bfd_find_version_for_sym (info->version_info,
1799 					hi->root.root.string, &hide);
1800 	  if (hi->verinfo.vertree != NULL && hide)
1801 	    {
1802 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1803 	      goto nondefault;
1804 	    }
1805 	}
1806       if (hi->verinfo.vertree != NULL
1807 	  && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1808 	goto nondefault;
1809     }
1810 
1811   if (! override)
1812     {
1813       /* Add the default symbol if not performing a relocatable link.  */
1814       if (! bfd_link_relocatable (info))
1815 	{
1816 	  bh = &hi->root;
1817 	  if (! (_bfd_generic_link_add_one_symbol
1818 		 (info, abfd, shortname, BSF_INDIRECT,
1819 		  bfd_ind_section_ptr,
1820 		  0, name, FALSE, collect, &bh)))
1821 	    return FALSE;
1822 	  hi = (struct elf_link_hash_entry *) bh;
1823 	}
1824     }
1825   else
1826     {
1827       /* In this case the symbol named SHORTNAME is overriding the
1828 	 indirect symbol we want to add.  We were planning on making
1829 	 SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
1830 	 is the name without a version.  NAME is the fully versioned
1831 	 name, and it is the default version.
1832 
1833 	 Overriding means that we already saw a definition for the
1834 	 symbol SHORTNAME in a regular object, and it is overriding
1835 	 the symbol defined in the dynamic object.
1836 
1837 	 When this happens, we actually want to change NAME, the
1838 	 symbol we just added, to refer to SHORTNAME.  This will cause
1839 	 references to NAME in the shared object to become references
1840 	 to SHORTNAME in the regular object.  This is what we expect
1841 	 when we override a function in a shared object: that the
1842 	 references in the shared object will be mapped to the
1843 	 definition in the regular object.  */
1844 
1845       while (hi->root.type == bfd_link_hash_indirect
1846 	     || hi->root.type == bfd_link_hash_warning)
1847 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1848 
1849       h->root.type = bfd_link_hash_indirect;
1850       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1851       if (h->def_dynamic)
1852 	{
1853 	  h->def_dynamic = 0;
1854 	  hi->ref_dynamic = 1;
1855 	  if (hi->ref_regular
1856 	      || hi->def_regular)
1857 	    {
1858 	      if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1859 		return FALSE;
1860 	    }
1861 	}
1862 
1863       /* Now set HI to H, so that the following code will set the
1864 	 other fields correctly.  */
1865       hi = h;
1866     }
1867 
1868   /* Check if HI is a warning symbol.  */
1869   if (hi->root.type == bfd_link_hash_warning)
1870     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1871 
1872   /* If there is a duplicate definition somewhere, then HI may not
1873      point to an indirect symbol.  We will have reported an error to
1874      the user in that case.  */
1875 
1876   if (hi->root.type == bfd_link_hash_indirect)
1877     {
1878       struct elf_link_hash_entry *ht;
1879 
1880       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1881       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1882 
1883       /* A reference to the SHORTNAME symbol from a dynamic library
1884 	 will be satisfied by the versioned symbol at runtime.  In
1885 	 effect, we have a reference to the versioned symbol.  */
1886       ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1887       hi->dynamic_def |= ht->dynamic_def;
1888 
1889       /* See if the new flags lead us to realize that the symbol must
1890 	 be dynamic.  */
1891       if (! *dynsym)
1892 	{
1893 	  if (! dynamic)
1894 	    {
1895 	      if (! bfd_link_executable (info)
1896 		  || hi->def_dynamic
1897 		  || hi->ref_dynamic)
1898 		*dynsym = TRUE;
1899 	    }
1900 	  else
1901 	    {
1902 	      if (hi->ref_regular)
1903 		*dynsym = TRUE;
1904 	    }
1905 	}
1906     }
1907 
1908   /* We also need to define an indirection from the nondefault version
1909      of the symbol.  */
1910 
1911 nondefault:
1912   len = strlen (name);
1913   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1914   if (shortname == NULL)
1915     return FALSE;
1916   memcpy (shortname, name, shortlen);
1917   memcpy (shortname + shortlen, p + 1, len - shortlen);
1918 
1919   /* Once again, merge with any existing symbol.  */
1920   type_change_ok = FALSE;
1921   size_change_ok = FALSE;
1922   tmp_sec = sec;
1923   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1924 			      &hi, poldbfd, NULL, NULL, &skip, &override,
1925 			      &type_change_ok, &size_change_ok, &matched))
1926     return FALSE;
1927 
1928   if (skip)
1929     return TRUE;
1930 
1931   if (override)
1932     {
1933       /* Here SHORTNAME is a versioned name, so we don't expect to see
1934 	 the type of override we do in the case above unless it is
1935 	 overridden by a versioned definition.  */
1936       if (hi->root.type != bfd_link_hash_defined
1937 	  && hi->root.type != bfd_link_hash_defweak)
1938 	(*_bfd_error_handler)
1939 	  (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1940 	   abfd, shortname);
1941     }
1942   else
1943     {
1944       bh = &hi->root;
1945       if (! (_bfd_generic_link_add_one_symbol
1946 	     (info, abfd, shortname, BSF_INDIRECT,
1947 	      bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1948 	return FALSE;
1949       hi = (struct elf_link_hash_entry *) bh;
1950 
1951       /* If there is a duplicate definition somewhere, then HI may not
1952 	 point to an indirect symbol.  We will have reported an error
1953 	 to the user in that case.  */
1954 
1955       if (hi->root.type == bfd_link_hash_indirect)
1956 	{
1957 	  (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1958 	  h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1959 	  hi->dynamic_def |= h->dynamic_def;
1960 
1961 	  /* See if the new flags lead us to realize that the symbol
1962 	     must be dynamic.  */
1963 	  if (! *dynsym)
1964 	    {
1965 	      if (! dynamic)
1966 		{
1967 		  if (! bfd_link_executable (info)
1968 		      || hi->ref_dynamic)
1969 		    *dynsym = TRUE;
1970 		}
1971 	      else
1972 		{
1973 		  if (hi->ref_regular)
1974 		    *dynsym = TRUE;
1975 		}
1976 	    }
1977 	}
1978     }
1979 
1980   return TRUE;
1981 }
1982 
1983 /* This routine is used to export all defined symbols into the dynamic
1984    symbol table.  It is called via elf_link_hash_traverse.  */
1985 
1986 static bfd_boolean
1987 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1988 {
1989   struct elf_info_failed *eif = (struct elf_info_failed *) data;
1990 
1991   /* Ignore indirect symbols.  These are added by the versioning code.  */
1992   if (h->root.type == bfd_link_hash_indirect)
1993     return TRUE;
1994 
1995   /* Ignore this if we won't export it.  */
1996   if (!eif->info->export_dynamic && !h->dynamic)
1997     return TRUE;
1998 
1999   if (h->dynindx == -1
2000       && (h->def_regular || h->ref_regular)
2001       && ! bfd_hide_sym_by_version (eif->info->version_info,
2002 				    h->root.root.string))
2003     {
2004       if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2005 	{
2006 	  eif->failed = TRUE;
2007 	  return FALSE;
2008 	}
2009     }
2010 
2011   return TRUE;
2012 }
2013 
2014 /* Look through the symbols which are defined in other shared
2015    libraries and referenced here.  Update the list of version
2016    dependencies.  This will be put into the .gnu.version_r section.
2017    This function is called via elf_link_hash_traverse.  */
2018 
2019 static bfd_boolean
2020 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2021 					 void *data)
2022 {
2023   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2024   Elf_Internal_Verneed *t;
2025   Elf_Internal_Vernaux *a;
2026   bfd_size_type amt;
2027 
2028   /* We only care about symbols defined in shared objects with version
2029      information.  */
2030   if (!h->def_dynamic
2031       || h->def_regular
2032       || h->dynindx == -1
2033       || h->verinfo.verdef == NULL
2034       || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2035 	  & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2036     return TRUE;
2037 
2038   /* See if we already know about this version.  */
2039   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2040        t != NULL;
2041        t = t->vn_nextref)
2042     {
2043       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2044 	continue;
2045 
2046       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2047 	if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2048 	  return TRUE;
2049 
2050       break;
2051     }
2052 
2053   /* This is a new version.  Add it to tree we are building.  */
2054 
2055   if (t == NULL)
2056     {
2057       amt = sizeof *t;
2058       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2059       if (t == NULL)
2060 	{
2061 	  rinfo->failed = TRUE;
2062 	  return FALSE;
2063 	}
2064 
2065       t->vn_bfd = h->verinfo.verdef->vd_bfd;
2066       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2067       elf_tdata (rinfo->info->output_bfd)->verref = t;
2068     }
2069 
2070   amt = sizeof *a;
2071   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2072   if (a == NULL)
2073     {
2074       rinfo->failed = TRUE;
2075       return FALSE;
2076     }
2077 
2078   /* Note that we are copying a string pointer here, and testing it
2079      above.  If bfd_elf_string_from_elf_section is ever changed to
2080      discard the string data when low in memory, this will have to be
2081      fixed.  */
2082   a->vna_nodename = h->verinfo.verdef->vd_nodename;
2083 
2084   a->vna_flags = h->verinfo.verdef->vd_flags;
2085   a->vna_nextptr = t->vn_auxptr;
2086 
2087   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2088   ++rinfo->vers;
2089 
2090   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2091 
2092   t->vn_auxptr = a;
2093 
2094   return TRUE;
2095 }
2096 
2097 /* Figure out appropriate versions for all the symbols.  We may not
2098    have the version number script until we have read all of the input
2099    files, so until that point we don't know which symbols should be
2100    local.  This function is called via elf_link_hash_traverse.  */
2101 
2102 static bfd_boolean
2103 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2104 {
2105   struct elf_info_failed *sinfo;
2106   struct bfd_link_info *info;
2107   const struct elf_backend_data *bed;
2108   struct elf_info_failed eif;
2109   char *p;
2110 
2111   sinfo = (struct elf_info_failed *) data;
2112   info = sinfo->info;
2113 
2114   /* Fix the symbol flags.  */
2115   eif.failed = FALSE;
2116   eif.info = info;
2117   if (! _bfd_elf_fix_symbol_flags (h, &eif))
2118     {
2119       if (eif.failed)
2120 	sinfo->failed = TRUE;
2121       return FALSE;
2122     }
2123 
2124   /* We only need version numbers for symbols defined in regular
2125      objects.  */
2126   if (!h->def_regular)
2127     return TRUE;
2128 
2129   bed = get_elf_backend_data (info->output_bfd);
2130   p = strchr (h->root.root.string, ELF_VER_CHR);
2131   if (p != NULL && h->verinfo.vertree == NULL)
2132     {
2133       struct bfd_elf_version_tree *t;
2134 
2135       ++p;
2136       if (*p == ELF_VER_CHR)
2137 	++p;
2138 
2139       /* If there is no version string, we can just return out.  */
2140       if (*p == '\0')
2141 	return TRUE;
2142 
2143       /* Look for the version.  If we find it, it is no longer weak.  */
2144       for (t = sinfo->info->version_info; t != NULL; t = t->next)
2145 	{
2146 	  if (strcmp (t->name, p) == 0)
2147 	    {
2148 	      size_t len;
2149 	      char *alc;
2150 	      struct bfd_elf_version_expr *d;
2151 
2152 	      len = p - h->root.root.string;
2153 	      alc = (char *) bfd_malloc (len);
2154 	      if (alc == NULL)
2155 		{
2156 		  sinfo->failed = TRUE;
2157 		  return FALSE;
2158 		}
2159 	      memcpy (alc, h->root.root.string, len - 1);
2160 	      alc[len - 1] = '\0';
2161 	      if (alc[len - 2] == ELF_VER_CHR)
2162 		alc[len - 2] = '\0';
2163 
2164 	      h->verinfo.vertree = t;
2165 	      t->used = TRUE;
2166 	      d = NULL;
2167 
2168 	      if (t->globals.list != NULL)
2169 		d = (*t->match) (&t->globals, NULL, alc);
2170 
2171 	      /* See if there is anything to force this symbol to
2172 		 local scope.  */
2173 	      if (d == NULL && t->locals.list != NULL)
2174 		{
2175 		  d = (*t->match) (&t->locals, NULL, alc);
2176 		  if (d != NULL
2177 		      && h->dynindx != -1
2178 		      && ! info->export_dynamic)
2179 		    (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2180 		}
2181 
2182 	      free (alc);
2183 	      break;
2184 	    }
2185 	}
2186 
2187       /* If we are building an application, we need to create a
2188 	 version node for this version.  */
2189       if (t == NULL && bfd_link_executable (info))
2190 	{
2191 	  struct bfd_elf_version_tree **pp;
2192 	  int version_index;
2193 
2194 	  /* If we aren't going to export this symbol, we don't need
2195 	     to worry about it.  */
2196 	  if (h->dynindx == -1)
2197 	    return TRUE;
2198 
2199 	  t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2200 							  sizeof *t);
2201 	  if (t == NULL)
2202 	    {
2203 	      sinfo->failed = TRUE;
2204 	      return FALSE;
2205 	    }
2206 
2207 	  t->name = p;
2208 	  t->name_indx = (unsigned int) -1;
2209 	  t->used = TRUE;
2210 
2211 	  version_index = 1;
2212 	  /* Don't count anonymous version tag.  */
2213 	  if (sinfo->info->version_info != NULL
2214 	      && sinfo->info->version_info->vernum == 0)
2215 	    version_index = 0;
2216 	  for (pp = &sinfo->info->version_info;
2217 	       *pp != NULL;
2218 	       pp = &(*pp)->next)
2219 	    ++version_index;
2220 	  t->vernum = version_index;
2221 
2222 	  *pp = t;
2223 
2224 	  h->verinfo.vertree = t;
2225 	}
2226       else if (t == NULL)
2227 	{
2228 	  /* We could not find the version for a symbol when
2229 	     generating a shared archive.  Return an error.  */
2230 	  (*_bfd_error_handler)
2231 	    (_("%B: version node not found for symbol %s"),
2232 	     info->output_bfd, h->root.root.string);
2233 	  bfd_set_error (bfd_error_bad_value);
2234 	  sinfo->failed = TRUE;
2235 	  return FALSE;
2236 	}
2237     }
2238 
2239   /* If we don't have a version for this symbol, see if we can find
2240      something.  */
2241   if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2242     {
2243       bfd_boolean hide;
2244 
2245       h->verinfo.vertree
2246 	= bfd_find_version_for_sym (sinfo->info->version_info,
2247 				    h->root.root.string, &hide);
2248       if (h->verinfo.vertree != NULL && hide)
2249 	(*bed->elf_backend_hide_symbol) (info, h, TRUE);
2250     }
2251 
2252   return TRUE;
2253 }
2254 
2255 /* Read and swap the relocs from the section indicated by SHDR.  This
2256    may be either a REL or a RELA section.  The relocations are
2257    translated into RELA relocations and stored in INTERNAL_RELOCS,
2258    which should have already been allocated to contain enough space.
2259    The EXTERNAL_RELOCS are a buffer where the external form of the
2260    relocations should be stored.
2261 
2262    Returns FALSE if something goes wrong.  */
2263 
2264 static bfd_boolean
2265 elf_link_read_relocs_from_section (bfd *abfd,
2266 				   asection *sec,
2267 				   Elf_Internal_Shdr *shdr,
2268 				   void *external_relocs,
2269 				   Elf_Internal_Rela *internal_relocs)
2270 {
2271   const struct elf_backend_data *bed;
2272   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2273   const bfd_byte *erela;
2274   const bfd_byte *erelaend;
2275   Elf_Internal_Rela *irela;
2276   Elf_Internal_Shdr *symtab_hdr;
2277   size_t nsyms;
2278 
2279   /* Position ourselves at the start of the section.  */
2280   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2281     return FALSE;
2282 
2283   /* Read the relocations.  */
2284   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2285     return FALSE;
2286 
2287   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2288   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2289 
2290   bed = get_elf_backend_data (abfd);
2291 
2292   /* Convert the external relocations to the internal format.  */
2293   if (shdr->sh_entsize == bed->s->sizeof_rel)
2294     swap_in = bed->s->swap_reloc_in;
2295   else if (shdr->sh_entsize == bed->s->sizeof_rela)
2296     swap_in = bed->s->swap_reloca_in;
2297   else
2298     {
2299       bfd_set_error (bfd_error_wrong_format);
2300       return FALSE;
2301     }
2302 
2303   erela = (const bfd_byte *) external_relocs;
2304   erelaend = erela + shdr->sh_size;
2305   irela = internal_relocs;
2306   while (erela < erelaend)
2307     {
2308       bfd_vma r_symndx;
2309 
2310       (*swap_in) (abfd, erela, irela);
2311       r_symndx = ELF32_R_SYM (irela->r_info);
2312       if (bed->s->arch_size == 64)
2313 	r_symndx >>= 24;
2314       if (nsyms > 0)
2315 	{
2316 	  if ((size_t) r_symndx >= nsyms)
2317 	    {
2318 	      (*_bfd_error_handler)
2319 		(_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2320 		   " for offset 0x%lx in section `%A'"),
2321 		 abfd, sec,
2322 		 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2323 	      bfd_set_error (bfd_error_bad_value);
2324 	      return FALSE;
2325 	    }
2326 	}
2327       else if (r_symndx != STN_UNDEF)
2328 	{
2329 	  (*_bfd_error_handler)
2330 	    (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2331 	       " when the object file has no symbol table"),
2332 	     abfd, sec,
2333 	     (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2334 	  bfd_set_error (bfd_error_bad_value);
2335 	  return FALSE;
2336 	}
2337       irela += bed->s->int_rels_per_ext_rel;
2338       erela += shdr->sh_entsize;
2339     }
2340 
2341   return TRUE;
2342 }
2343 
2344 /* Read and swap the relocs for a section O.  They may have been
2345    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2346    not NULL, they are used as buffers to read into.  They are known to
2347    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2348    the return value is allocated using either malloc or bfd_alloc,
2349    according to the KEEP_MEMORY argument.  If O has two relocation
2350    sections (both REL and RELA relocations), then the REL_HDR
2351    relocations will appear first in INTERNAL_RELOCS, followed by the
2352    RELA_HDR relocations.  */
2353 
2354 Elf_Internal_Rela *
2355 _bfd_elf_link_read_relocs (bfd *abfd,
2356 			   asection *o,
2357 			   void *external_relocs,
2358 			   Elf_Internal_Rela *internal_relocs,
2359 			   bfd_boolean keep_memory)
2360 {
2361   void *alloc1 = NULL;
2362   Elf_Internal_Rela *alloc2 = NULL;
2363   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2364   struct bfd_elf_section_data *esdo = elf_section_data (o);
2365   Elf_Internal_Rela *internal_rela_relocs;
2366 
2367   if (esdo->relocs != NULL)
2368     return esdo->relocs;
2369 
2370   if (o->reloc_count == 0)
2371     return NULL;
2372 
2373   if (internal_relocs == NULL)
2374     {
2375       bfd_size_type size;
2376 
2377       size = o->reloc_count;
2378       size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2379       if (keep_memory)
2380 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2381       else
2382 	internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2383       if (internal_relocs == NULL)
2384 	goto error_return;
2385     }
2386 
2387   if (external_relocs == NULL)
2388     {
2389       bfd_size_type size = 0;
2390 
2391       if (esdo->rel.hdr)
2392 	size += esdo->rel.hdr->sh_size;
2393       if (esdo->rela.hdr)
2394 	size += esdo->rela.hdr->sh_size;
2395 
2396       alloc1 = bfd_malloc (size);
2397       if (alloc1 == NULL)
2398 	goto error_return;
2399       external_relocs = alloc1;
2400     }
2401 
2402   internal_rela_relocs = internal_relocs;
2403   if (esdo->rel.hdr)
2404     {
2405       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2406 					      external_relocs,
2407 					      internal_relocs))
2408 	goto error_return;
2409       external_relocs = (((bfd_byte *) external_relocs)
2410 			 + esdo->rel.hdr->sh_size);
2411       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2412 			       * bed->s->int_rels_per_ext_rel);
2413     }
2414 
2415   if (esdo->rela.hdr
2416       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2417 					      external_relocs,
2418 					      internal_rela_relocs)))
2419     goto error_return;
2420 
2421   /* Cache the results for next time, if we can.  */
2422   if (keep_memory)
2423     esdo->relocs = internal_relocs;
2424 
2425   if (alloc1 != NULL)
2426     free (alloc1);
2427 
2428   /* Don't free alloc2, since if it was allocated we are passing it
2429      back (under the name of internal_relocs).  */
2430 
2431   return internal_relocs;
2432 
2433  error_return:
2434   if (alloc1 != NULL)
2435     free (alloc1);
2436   if (alloc2 != NULL)
2437     {
2438       if (keep_memory)
2439 	bfd_release (abfd, alloc2);
2440       else
2441 	free (alloc2);
2442     }
2443   return NULL;
2444 }
2445 
2446 /* Compute the size of, and allocate space for, REL_HDR which is the
2447    section header for a section containing relocations for O.  */
2448 
2449 static bfd_boolean
2450 _bfd_elf_link_size_reloc_section (bfd *abfd,
2451 				  struct bfd_elf_section_reloc_data *reldata)
2452 {
2453   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2454 
2455   /* That allows us to calculate the size of the section.  */
2456   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2457 
2458   /* The contents field must last into write_object_contents, so we
2459      allocate it with bfd_alloc rather than malloc.  Also since we
2460      cannot be sure that the contents will actually be filled in,
2461      we zero the allocated space.  */
2462   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2463   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2464     return FALSE;
2465 
2466   if (reldata->hashes == NULL && reldata->count)
2467     {
2468       struct elf_link_hash_entry **p;
2469 
2470       p = ((struct elf_link_hash_entry **)
2471 	   bfd_zmalloc (reldata->count * sizeof (*p)));
2472       if (p == NULL)
2473 	return FALSE;
2474 
2475       reldata->hashes = p;
2476     }
2477 
2478   return TRUE;
2479 }
2480 
2481 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2482    originated from the section given by INPUT_REL_HDR) to the
2483    OUTPUT_BFD.  */
2484 
2485 bfd_boolean
2486 _bfd_elf_link_output_relocs (bfd *output_bfd,
2487 			     asection *input_section,
2488 			     Elf_Internal_Shdr *input_rel_hdr,
2489 			     Elf_Internal_Rela *internal_relocs,
2490 			     struct elf_link_hash_entry **rel_hash
2491 			       ATTRIBUTE_UNUSED)
2492 {
2493   Elf_Internal_Rela *irela;
2494   Elf_Internal_Rela *irelaend;
2495   bfd_byte *erel;
2496   struct bfd_elf_section_reloc_data *output_reldata;
2497   asection *output_section;
2498   const struct elf_backend_data *bed;
2499   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2500   struct bfd_elf_section_data *esdo;
2501 
2502   output_section = input_section->output_section;
2503 
2504   bed = get_elf_backend_data (output_bfd);
2505   esdo = elf_section_data (output_section);
2506   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2507     {
2508       output_reldata = &esdo->rel;
2509       swap_out = bed->s->swap_reloc_out;
2510     }
2511   else if (esdo->rela.hdr
2512 	   && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2513     {
2514       output_reldata = &esdo->rela;
2515       swap_out = bed->s->swap_reloca_out;
2516     }
2517   else
2518     {
2519       (*_bfd_error_handler)
2520 	(_("%B: relocation size mismatch in %B section %A"),
2521 	 output_bfd, input_section->owner, input_section);
2522       bfd_set_error (bfd_error_wrong_format);
2523       return FALSE;
2524     }
2525 
2526   erel = output_reldata->hdr->contents;
2527   erel += output_reldata->count * input_rel_hdr->sh_entsize;
2528   irela = internal_relocs;
2529   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2530 		      * bed->s->int_rels_per_ext_rel);
2531   while (irela < irelaend)
2532     {
2533       (*swap_out) (output_bfd, irela, erel);
2534       irela += bed->s->int_rels_per_ext_rel;
2535       erel += input_rel_hdr->sh_entsize;
2536     }
2537 
2538   /* Bump the counter, so that we know where to add the next set of
2539      relocations.  */
2540   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2541 
2542   return TRUE;
2543 }
2544 
2545 /* Make weak undefined symbols in PIE dynamic.  */
2546 
2547 bfd_boolean
2548 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2549 				 struct elf_link_hash_entry *h)
2550 {
2551   if (bfd_link_pie (info)
2552       && h->dynindx == -1
2553       && h->root.type == bfd_link_hash_undefweak)
2554     return bfd_elf_link_record_dynamic_symbol (info, h);
2555 
2556   return TRUE;
2557 }
2558 
2559 /* Fix up the flags for a symbol.  This handles various cases which
2560    can only be fixed after all the input files are seen.  This is
2561    currently called by both adjust_dynamic_symbol and
2562    assign_sym_version, which is unnecessary but perhaps more robust in
2563    the face of future changes.  */
2564 
2565 static bfd_boolean
2566 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2567 			   struct elf_info_failed *eif)
2568 {
2569   const struct elf_backend_data *bed;
2570 
2571   /* If this symbol was mentioned in a non-ELF file, try to set
2572      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
2573      permit a non-ELF file to correctly refer to a symbol defined in
2574      an ELF dynamic object.  */
2575   if (h->non_elf)
2576     {
2577       while (h->root.type == bfd_link_hash_indirect)
2578 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
2579 
2580       if (h->root.type != bfd_link_hash_defined
2581 	  && h->root.type != bfd_link_hash_defweak)
2582 	{
2583 	  h->ref_regular = 1;
2584 	  h->ref_regular_nonweak = 1;
2585 	}
2586       else
2587 	{
2588 	  if (h->root.u.def.section->owner != NULL
2589 	      && (bfd_get_flavour (h->root.u.def.section->owner)
2590 		  == bfd_target_elf_flavour))
2591 	    {
2592 	      h->ref_regular = 1;
2593 	      h->ref_regular_nonweak = 1;
2594 	    }
2595 	  else
2596 	    h->def_regular = 1;
2597 	}
2598 
2599       if (h->dynindx == -1
2600 	  && (h->def_dynamic
2601 	      || h->ref_dynamic))
2602 	{
2603 	  if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2604 	    {
2605 	      eif->failed = TRUE;
2606 	      return FALSE;
2607 	    }
2608 	}
2609     }
2610   else
2611     {
2612       /* Unfortunately, NON_ELF is only correct if the symbol
2613 	 was first seen in a non-ELF file.  Fortunately, if the symbol
2614 	 was first seen in an ELF file, we're probably OK unless the
2615 	 symbol was defined in a non-ELF file.  Catch that case here.
2616 	 FIXME: We're still in trouble if the symbol was first seen in
2617 	 a dynamic object, and then later in a non-ELF regular object.  */
2618       if ((h->root.type == bfd_link_hash_defined
2619 	   || h->root.type == bfd_link_hash_defweak)
2620 	  && !h->def_regular
2621 	  && (h->root.u.def.section->owner != NULL
2622 	      ? (bfd_get_flavour (h->root.u.def.section->owner)
2623 		 != bfd_target_elf_flavour)
2624 	      : (bfd_is_abs_section (h->root.u.def.section)
2625 		 && !h->def_dynamic)))
2626 	h->def_regular = 1;
2627     }
2628 
2629   /* Backend specific symbol fixup.  */
2630   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2631   if (bed->elf_backend_fixup_symbol
2632       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2633     return FALSE;
2634 
2635   /* If this is a final link, and the symbol was defined as a common
2636      symbol in a regular object file, and there was no definition in
2637      any dynamic object, then the linker will have allocated space for
2638      the symbol in a common section but the DEF_REGULAR
2639      flag will not have been set.  */
2640   if (h->root.type == bfd_link_hash_defined
2641       && !h->def_regular
2642       && h->ref_regular
2643       && !h->def_dynamic
2644       && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2645     h->def_regular = 1;
2646 
2647   /* If -Bsymbolic was used (which means to bind references to global
2648      symbols to the definition within the shared object), and this
2649      symbol was defined in a regular object, then it actually doesn't
2650      need a PLT entry.  Likewise, if the symbol has non-default
2651      visibility.  If the symbol has hidden or internal visibility, we
2652      will force it local.  */
2653   if (h->needs_plt
2654       && bfd_link_pic (eif->info)
2655       && is_elf_hash_table (eif->info->hash)
2656       && (SYMBOLIC_BIND (eif->info, h)
2657 	  || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2658       && h->def_regular)
2659     {
2660       bfd_boolean force_local;
2661 
2662       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2663 		     || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2664       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2665     }
2666 
2667   /* If a weak undefined symbol has non-default visibility, we also
2668      hide it from the dynamic linker.  */
2669   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2670       && h->root.type == bfd_link_hash_undefweak)
2671     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2672 
2673   /* If this is a weak defined symbol in a dynamic object, and we know
2674      the real definition in the dynamic object, copy interesting flags
2675      over to the real definition.  */
2676   if (h->u.weakdef != NULL)
2677     {
2678       struct elf_link_hash_entry *weakdef = h->u.weakdef;
2679       while (weakdef->root.type == bfd_link_hash_indirect)
2680         weakdef = (struct elf_link_hash_entry *) weakdef->root.u.i.link;
2681 
2682       /* If the real definition is defined by a regular object file,
2683 	 don't do anything special.  See the longer description in
2684 	 _bfd_elf_adjust_dynamic_symbol, below.  */
2685       if (weakdef->def_regular)
2686 	h->u.weakdef = NULL;
2687       else
2688 	{
2689 
2690 	  while (h->root.type == bfd_link_hash_indirect)
2691 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2692 
2693 	  BFD_ASSERT (h->root.type == bfd_link_hash_defined
2694 		      || h->root.type == bfd_link_hash_defweak);
2695 	  BFD_ASSERT (weakdef->def_dynamic);
2696 	  BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2697 		      || weakdef->root.type == bfd_link_hash_defweak);
2698 	  (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2699 	}
2700     }
2701 
2702   return TRUE;
2703 }
2704 
2705 /* Make the backend pick a good value for a dynamic symbol.  This is
2706    called via elf_link_hash_traverse, and also calls itself
2707    recursively.  */
2708 
2709 static bfd_boolean
2710 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2711 {
2712   struct elf_info_failed *eif = (struct elf_info_failed *) data;
2713   bfd *dynobj;
2714   const struct elf_backend_data *bed;
2715 
2716   if (! is_elf_hash_table (eif->info->hash))
2717     return FALSE;
2718 
2719   /* Ignore indirect symbols.  These are added by the versioning code.  */
2720   if (h->root.type == bfd_link_hash_indirect)
2721     return TRUE;
2722 
2723   /* Fix the symbol flags.  */
2724   if (! _bfd_elf_fix_symbol_flags (h, eif))
2725     return FALSE;
2726 
2727   /* If this symbol does not require a PLT entry, and it is not
2728      defined by a dynamic object, or is not referenced by a regular
2729      object, ignore it.  We do have to handle a weak defined symbol,
2730      even if no regular object refers to it, if we decided to add it
2731      to the dynamic symbol table.  FIXME: Do we normally need to worry
2732      about symbols which are defined by one dynamic object and
2733      referenced by another one?  */
2734   if (!h->needs_plt
2735       && h->type != STT_GNU_IFUNC
2736       && (h->def_regular
2737 	  || !h->def_dynamic
2738 	  || (!h->ref_regular
2739 	      && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2740     {
2741       h->plt = elf_hash_table (eif->info)->init_plt_offset;
2742       return TRUE;
2743     }
2744 
2745   /* If we've already adjusted this symbol, don't do it again.  This
2746      can happen via a recursive call.  */
2747   if (h->dynamic_adjusted)
2748     return TRUE;
2749 
2750   /* Don't look at this symbol again.  Note that we must set this
2751      after checking the above conditions, because we may look at a
2752      symbol once, decide not to do anything, and then get called
2753      recursively later after REF_REGULAR is set below.  */
2754   h->dynamic_adjusted = 1;
2755 
2756   /* If this is a weak definition, and we know a real definition, and
2757      the real symbol is not itself defined by a regular object file,
2758      then get a good value for the real definition.  We handle the
2759      real symbol first, for the convenience of the backend routine.
2760 
2761      Note that there is a confusing case here.  If the real definition
2762      is defined by a regular object file, we don't get the real symbol
2763      from the dynamic object, but we do get the weak symbol.  If the
2764      processor backend uses a COPY reloc, then if some routine in the
2765      dynamic object changes the real symbol, we will not see that
2766      change in the corresponding weak symbol.  This is the way other
2767      ELF linkers work as well, and seems to be a result of the shared
2768      library model.
2769 
2770      I will clarify this issue.  Most SVR4 shared libraries define the
2771      variable _timezone and define timezone as a weak synonym.  The
2772      tzset call changes _timezone.  If you write
2773        extern int timezone;
2774        int _timezone = 5;
2775        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2776      you might expect that, since timezone is a synonym for _timezone,
2777      the same number will print both times.  However, if the processor
2778      backend uses a COPY reloc, then actually timezone will be copied
2779      into your process image, and, since you define _timezone
2780      yourself, _timezone will not.  Thus timezone and _timezone will
2781      wind up at different memory locations.  The tzset call will set
2782      _timezone, leaving timezone unchanged.  */
2783 
2784   if (h->u.weakdef != NULL)
2785     {
2786       /* If we get to this point, there is an implicit reference to
2787 	 H->U.WEAKDEF by a regular object file via the weak symbol H.  */
2788       h->u.weakdef->ref_regular = 1;
2789 
2790       /* Ensure that the backend adjust_dynamic_symbol function sees
2791 	 H->U.WEAKDEF before H by recursively calling ourselves.  */
2792       if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2793 	return FALSE;
2794     }
2795 
2796   /* If a symbol has no type and no size and does not require a PLT
2797      entry, then we are probably about to do the wrong thing here: we
2798      are probably going to create a COPY reloc for an empty object.
2799      This case can arise when a shared object is built with assembly
2800      code, and the assembly code fails to set the symbol type.  */
2801   if (h->size == 0
2802       && h->type == STT_NOTYPE
2803       && !h->needs_plt)
2804     (*_bfd_error_handler)
2805       (_("warning: type and size of dynamic symbol `%s' are not defined"),
2806        h->root.root.string);
2807 
2808   dynobj = elf_hash_table (eif->info)->dynobj;
2809   bed = get_elf_backend_data (dynobj);
2810 
2811   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2812     {
2813       eif->failed = TRUE;
2814       return FALSE;
2815     }
2816 
2817   return TRUE;
2818 }
2819 
2820 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2821    DYNBSS.  */
2822 
2823 bfd_boolean
2824 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2825 			      struct elf_link_hash_entry *h,
2826 			      asection *dynbss)
2827 {
2828   unsigned int power_of_two;
2829   bfd_vma mask;
2830   asection *sec = h->root.u.def.section;
2831 
2832   /* The section aligment of definition is the maximum alignment
2833      requirement of symbols defined in the section.  Since we don't
2834      know the symbol alignment requirement, we start with the
2835      maximum alignment and check low bits of the symbol address
2836      for the minimum alignment.  */
2837   power_of_two = bfd_get_section_alignment (sec->owner, sec);
2838   mask = ((bfd_vma) 1 << power_of_two) - 1;
2839   while ((h->root.u.def.value & mask) != 0)
2840     {
2841        mask >>= 1;
2842        --power_of_two;
2843     }
2844 
2845   if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2846 						dynbss))
2847     {
2848       /* Adjust the section alignment if needed.  */
2849       if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2850 				       power_of_two))
2851 	return FALSE;
2852     }
2853 
2854   /* We make sure that the symbol will be aligned properly.  */
2855   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2856 
2857   /* Define the symbol as being at this point in DYNBSS.  */
2858   h->root.u.def.section = dynbss;
2859   h->root.u.def.value = dynbss->size;
2860 
2861   /* Increment the size of DYNBSS to make room for the symbol.  */
2862   dynbss->size += h->size;
2863 
2864   /* No error if extern_protected_data is true.  */
2865   if (h->protected_def
2866       && (!info->extern_protected_data
2867 	  || (info->extern_protected_data < 0
2868 	      && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2869     info->callbacks->einfo
2870       (_("%P: copy reloc against protected `%T' is dangerous\n"),
2871        h->root.root.string);
2872 
2873   return TRUE;
2874 }
2875 
2876 /* Adjust all external symbols pointing into SEC_MERGE sections
2877    to reflect the object merging within the sections.  */
2878 
2879 static bfd_boolean
2880 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2881 {
2882   asection *sec;
2883 
2884   if ((h->root.type == bfd_link_hash_defined
2885        || h->root.type == bfd_link_hash_defweak)
2886       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2887       && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2888     {
2889       bfd *output_bfd = (bfd *) data;
2890 
2891       h->root.u.def.value =
2892 	_bfd_merged_section_offset (output_bfd,
2893 				    &h->root.u.def.section,
2894 				    elf_section_data (sec)->sec_info,
2895 				    h->root.u.def.value);
2896     }
2897 
2898   return TRUE;
2899 }
2900 
2901 /* Returns false if the symbol referred to by H should be considered
2902    to resolve local to the current module, and true if it should be
2903    considered to bind dynamically.  */
2904 
2905 bfd_boolean
2906 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2907 			   struct bfd_link_info *info,
2908 			   bfd_boolean not_local_protected)
2909 {
2910   bfd_boolean binding_stays_local_p;
2911   const struct elf_backend_data *bed;
2912   struct elf_link_hash_table *hash_table;
2913 
2914   if (h == NULL)
2915     return FALSE;
2916 
2917   while (h->root.type == bfd_link_hash_indirect
2918 	 || h->root.type == bfd_link_hash_warning)
2919     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2920 
2921   /* If it was forced local, then clearly it's not dynamic.  */
2922   if (h->dynindx == -1)
2923     return FALSE;
2924   if (h->forced_local)
2925     return FALSE;
2926 
2927   /* Identify the cases where name binding rules say that a
2928      visible symbol resolves locally.  */
2929   binding_stays_local_p = (bfd_link_executable (info)
2930 			   || SYMBOLIC_BIND (info, h));
2931 
2932   switch (ELF_ST_VISIBILITY (h->other))
2933     {
2934     case STV_INTERNAL:
2935     case STV_HIDDEN:
2936       return FALSE;
2937 
2938     case STV_PROTECTED:
2939       hash_table = elf_hash_table (info);
2940       if (!is_elf_hash_table (hash_table))
2941 	return FALSE;
2942 
2943       bed = get_elf_backend_data (hash_table->dynobj);
2944 
2945       /* Proper resolution for function pointer equality may require
2946 	 that these symbols perhaps be resolved dynamically, even though
2947 	 we should be resolving them to the current module.  */
2948       if (!not_local_protected || !bed->is_function_type (h->type))
2949 	binding_stays_local_p = TRUE;
2950       break;
2951 
2952     default:
2953       break;
2954     }
2955 
2956   /* If it isn't defined locally, then clearly it's dynamic.  */
2957   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2958     return TRUE;
2959 
2960   /* Otherwise, the symbol is dynamic if binding rules don't tell
2961      us that it remains local.  */
2962   return !binding_stays_local_p;
2963 }
2964 
2965 /* Return true if the symbol referred to by H should be considered
2966    to resolve local to the current module, and false otherwise.  Differs
2967    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2968    undefined symbols.  The two functions are virtually identical except
2969    for the place where forced_local and dynindx == -1 are tested.  If
2970    either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2971    the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2972    the symbol is local only for defined symbols.
2973    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2974    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2975    treatment of undefined weak symbols.  For those that do not make
2976    undefined weak symbols dynamic, both functions may return false.  */
2977 
2978 bfd_boolean
2979 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2980 			      struct bfd_link_info *info,
2981 			      bfd_boolean local_protected)
2982 {
2983   const struct elf_backend_data *bed;
2984   struct elf_link_hash_table *hash_table;
2985 
2986   /* If it's a local sym, of course we resolve locally.  */
2987   if (h == NULL)
2988     return TRUE;
2989 
2990   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
2991   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2992       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2993     return TRUE;
2994 
2995   /* Common symbols that become definitions don't get the DEF_REGULAR
2996      flag set, so test it first, and don't bail out.  */
2997   if (ELF_COMMON_DEF_P (h))
2998     /* Do nothing.  */;
2999   /* If we don't have a definition in a regular file, then we can't
3000      resolve locally.  The sym is either undefined or dynamic.  */
3001   else if (!h->def_regular)
3002     return FALSE;
3003 
3004   /* Forced local symbols resolve locally.  */
3005   if (h->forced_local)
3006     return TRUE;
3007 
3008   /* As do non-dynamic symbols.  */
3009   if (h->dynindx == -1)
3010     return TRUE;
3011 
3012   /* At this point, we know the symbol is defined and dynamic.  In an
3013      executable it must resolve locally, likewise when building symbolic
3014      shared libraries.  */
3015   if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3016     return TRUE;
3017 
3018   /* Now deal with defined dynamic symbols in shared libraries.  Ones
3019      with default visibility might not resolve locally.  */
3020   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3021     return FALSE;
3022 
3023   hash_table = elf_hash_table (info);
3024   if (!is_elf_hash_table (hash_table))
3025     return TRUE;
3026 
3027   bed = get_elf_backend_data (hash_table->dynobj);
3028 
3029   /* If extern_protected_data is false, STV_PROTECTED non-function
3030      symbols are local.  */
3031   if ((!info->extern_protected_data
3032        || (info->extern_protected_data < 0
3033 	   && !bed->extern_protected_data))
3034       && !bed->is_function_type (h->type))
3035     return TRUE;
3036 
3037   /* Function pointer equality tests may require that STV_PROTECTED
3038      symbols be treated as dynamic symbols.  If the address of a
3039      function not defined in an executable is set to that function's
3040      plt entry in the executable, then the address of the function in
3041      a shared library must also be the plt entry in the executable.  */
3042   return local_protected;
3043 }
3044 
3045 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3046    aligned.  Returns the first TLS output section.  */
3047 
3048 struct bfd_section *
3049 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3050 {
3051   struct bfd_section *sec, *tls;
3052   unsigned int align = 0;
3053 
3054   for (sec = obfd->sections; sec != NULL; sec = sec->next)
3055     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3056       break;
3057   tls = sec;
3058 
3059   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3060     if (sec->alignment_power > align)
3061       align = sec->alignment_power;
3062 
3063   elf_hash_table (info)->tls_sec = tls;
3064 
3065   /* Ensure the alignment of the first section is the largest alignment,
3066      so that the tls segment starts aligned.  */
3067   if (tls != NULL)
3068     tls->alignment_power = align;
3069 
3070   return tls;
3071 }
3072 
3073 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
3074 static bfd_boolean
3075 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3076 				  Elf_Internal_Sym *sym)
3077 {
3078   const struct elf_backend_data *bed;
3079 
3080   /* Local symbols do not count, but target specific ones might.  */
3081   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3082       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3083     return FALSE;
3084 
3085   bed = get_elf_backend_data (abfd);
3086   /* Function symbols do not count.  */
3087   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3088     return FALSE;
3089 
3090   /* If the section is undefined, then so is the symbol.  */
3091   if (sym->st_shndx == SHN_UNDEF)
3092     return FALSE;
3093 
3094   /* If the symbol is defined in the common section, then
3095      it is a common definition and so does not count.  */
3096   if (bed->common_definition (sym))
3097     return FALSE;
3098 
3099   /* If the symbol is in a target specific section then we
3100      must rely upon the backend to tell us what it is.  */
3101   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3102     /* FIXME - this function is not coded yet:
3103 
3104        return _bfd_is_global_symbol_definition (abfd, sym);
3105 
3106        Instead for now assume that the definition is not global,
3107        Even if this is wrong, at least the linker will behave
3108        in the same way that it used to do.  */
3109     return FALSE;
3110 
3111   return TRUE;
3112 }
3113 
3114 /* Search the symbol table of the archive element of the archive ABFD
3115    whose archive map contains a mention of SYMDEF, and determine if
3116    the symbol is defined in this element.  */
3117 static bfd_boolean
3118 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3119 {
3120   Elf_Internal_Shdr * hdr;
3121   size_t symcount;
3122   size_t extsymcount;
3123   size_t extsymoff;
3124   Elf_Internal_Sym *isymbuf;
3125   Elf_Internal_Sym *isym;
3126   Elf_Internal_Sym *isymend;
3127   bfd_boolean result;
3128 
3129   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3130   if (abfd == NULL)
3131     return FALSE;
3132 
3133   if (! bfd_check_format (abfd, bfd_object))
3134     return FALSE;
3135 
3136   /* Select the appropriate symbol table.  If we don't know if the
3137      object file is an IR object, give linker LTO plugin a chance to
3138      get the correct symbol table.  */
3139   if (abfd->plugin_format == bfd_plugin_yes
3140 #if BFD_SUPPORTS_PLUGINS
3141       || (abfd->plugin_format == bfd_plugin_unknown
3142 	  && bfd_link_plugin_object_p (abfd))
3143 #endif
3144       )
3145     {
3146       /* Use the IR symbol table if the object has been claimed by
3147 	 plugin.  */
3148       abfd = abfd->plugin_dummy_bfd;
3149       hdr = &elf_tdata (abfd)->symtab_hdr;
3150     }
3151   else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3152     hdr = &elf_tdata (abfd)->symtab_hdr;
3153   else
3154     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3155 
3156   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3157 
3158   /* The sh_info field of the symtab header tells us where the
3159      external symbols start.  We don't care about the local symbols.  */
3160   if (elf_bad_symtab (abfd))
3161     {
3162       extsymcount = symcount;
3163       extsymoff = 0;
3164     }
3165   else
3166     {
3167       extsymcount = symcount - hdr->sh_info;
3168       extsymoff = hdr->sh_info;
3169     }
3170 
3171   if (extsymcount == 0)
3172     return FALSE;
3173 
3174   /* Read in the symbol table.  */
3175   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3176 				  NULL, NULL, NULL);
3177   if (isymbuf == NULL)
3178     return FALSE;
3179 
3180   /* Scan the symbol table looking for SYMDEF.  */
3181   result = FALSE;
3182   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3183     {
3184       const char *name;
3185 
3186       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3187 					      isym->st_name);
3188       if (name == NULL)
3189 	break;
3190 
3191       if (strcmp (name, symdef->name) == 0)
3192 	{
3193 	  result = is_global_data_symbol_definition (abfd, isym);
3194 	  break;
3195 	}
3196     }
3197 
3198   free (isymbuf);
3199 
3200   return result;
3201 }
3202 
3203 /* Add an entry to the .dynamic table.  */
3204 
3205 bfd_boolean
3206 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3207 			    bfd_vma tag,
3208 			    bfd_vma val)
3209 {
3210   struct elf_link_hash_table *hash_table;
3211   const struct elf_backend_data *bed;
3212   asection *s;
3213   bfd_size_type newsize;
3214   bfd_byte *newcontents;
3215   Elf_Internal_Dyn dyn;
3216 
3217   hash_table = elf_hash_table (info);
3218   if (! is_elf_hash_table (hash_table))
3219     return FALSE;
3220 
3221   bed = get_elf_backend_data (hash_table->dynobj);
3222   s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3223   BFD_ASSERT (s != NULL);
3224 
3225   newsize = s->size + bed->s->sizeof_dyn;
3226   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3227   if (newcontents == NULL)
3228     return FALSE;
3229 
3230   dyn.d_tag = tag;
3231   dyn.d_un.d_val = val;
3232   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3233 
3234   s->size = newsize;
3235   s->contents = newcontents;
3236 
3237   return TRUE;
3238 }
3239 
3240 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3241    otherwise just check whether one already exists.  Returns -1 on error,
3242    1 if a DT_NEEDED tag already exists, and 0 on success.  */
3243 
3244 static int
3245 elf_add_dt_needed_tag (bfd *abfd,
3246 		       struct bfd_link_info *info,
3247 		       const char *soname,
3248 		       bfd_boolean do_it)
3249 {
3250   struct elf_link_hash_table *hash_table;
3251   size_t strindex;
3252 
3253   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3254     return -1;
3255 
3256   hash_table = elf_hash_table (info);
3257   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3258   if (strindex == (size_t) -1)
3259     return -1;
3260 
3261   if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3262     {
3263       asection *sdyn;
3264       const struct elf_backend_data *bed;
3265       bfd_byte *extdyn;
3266 
3267       bed = get_elf_backend_data (hash_table->dynobj);
3268       sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3269       if (sdyn != NULL)
3270 	for (extdyn = sdyn->contents;
3271 	     extdyn < sdyn->contents + sdyn->size;
3272 	     extdyn += bed->s->sizeof_dyn)
3273 	  {
3274 	    Elf_Internal_Dyn dyn;
3275 
3276 	    bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3277 	    if (dyn.d_tag == DT_NEEDED
3278 		&& dyn.d_un.d_val == strindex)
3279 	      {
3280 		_bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3281 		return 1;
3282 	      }
3283 	  }
3284     }
3285 
3286   if (do_it)
3287     {
3288       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3289 	return -1;
3290 
3291       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3292 	return -1;
3293     }
3294   else
3295     /* We were just checking for existence of the tag.  */
3296     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3297 
3298   return 0;
3299 }
3300 
3301 /* Return true if SONAME is on the needed list between NEEDED and STOP
3302    (or the end of list if STOP is NULL), and needed by a library that
3303    will be loaded.  */
3304 
3305 static bfd_boolean
3306 on_needed_list (const char *soname,
3307 		struct bfd_link_needed_list *needed,
3308 		struct bfd_link_needed_list *stop)
3309 {
3310   struct bfd_link_needed_list *look;
3311   for (look = needed; look != stop; look = look->next)
3312     if (strcmp (soname, look->name) == 0
3313 	&& ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3314 	    /* If needed by a library that itself is not directly
3315 	       needed, recursively check whether that library is
3316 	       indirectly needed.  Since we add DT_NEEDED entries to
3317 	       the end of the list, library dependencies appear after
3318 	       the library.  Therefore search prior to the current
3319 	       LOOK, preventing possible infinite recursion.  */
3320 	    || on_needed_list (elf_dt_name (look->by), needed, look)))
3321       return TRUE;
3322 
3323   return FALSE;
3324 }
3325 
3326 /* Sort symbol by value, section, and size.  */
3327 static int
3328 elf_sort_symbol (const void *arg1, const void *arg2)
3329 {
3330   const struct elf_link_hash_entry *h1;
3331   const struct elf_link_hash_entry *h2;
3332   bfd_signed_vma vdiff;
3333 
3334   h1 = *(const struct elf_link_hash_entry **) arg1;
3335   h2 = *(const struct elf_link_hash_entry **) arg2;
3336   vdiff = h1->root.u.def.value - h2->root.u.def.value;
3337   if (vdiff != 0)
3338     return vdiff > 0 ? 1 : -1;
3339   else
3340     {
3341       int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3342       if (sdiff != 0)
3343 	return sdiff > 0 ? 1 : -1;
3344     }
3345   vdiff = h1->size - h2->size;
3346   return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3347 }
3348 
3349 /* This function is used to adjust offsets into .dynstr for
3350    dynamic symbols.  This is called via elf_link_hash_traverse.  */
3351 
3352 static bfd_boolean
3353 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3354 {
3355   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3356 
3357   if (h->dynindx != -1)
3358     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3359   return TRUE;
3360 }
3361 
3362 /* Assign string offsets in .dynstr, update all structures referencing
3363    them.  */
3364 
3365 static bfd_boolean
3366 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3367 {
3368   struct elf_link_hash_table *hash_table = elf_hash_table (info);
3369   struct elf_link_local_dynamic_entry *entry;
3370   struct elf_strtab_hash *dynstr = hash_table->dynstr;
3371   bfd *dynobj = hash_table->dynobj;
3372   asection *sdyn;
3373   bfd_size_type size;
3374   const struct elf_backend_data *bed;
3375   bfd_byte *extdyn;
3376 
3377   _bfd_elf_strtab_finalize (dynstr);
3378   size = _bfd_elf_strtab_size (dynstr);
3379 
3380   bed = get_elf_backend_data (dynobj);
3381   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3382   BFD_ASSERT (sdyn != NULL);
3383 
3384   /* Update all .dynamic entries referencing .dynstr strings.  */
3385   for (extdyn = sdyn->contents;
3386        extdyn < sdyn->contents + sdyn->size;
3387        extdyn += bed->s->sizeof_dyn)
3388     {
3389       Elf_Internal_Dyn dyn;
3390 
3391       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3392       switch (dyn.d_tag)
3393 	{
3394 	case DT_STRSZ:
3395 	  dyn.d_un.d_val = size;
3396 	  break;
3397 	case DT_NEEDED:
3398 	case DT_SONAME:
3399 	case DT_RPATH:
3400 	case DT_RUNPATH:
3401 	case DT_FILTER:
3402 	case DT_AUXILIARY:
3403 	case DT_AUDIT:
3404 	case DT_DEPAUDIT:
3405 	  dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3406 	  break;
3407 	default:
3408 	  continue;
3409 	}
3410       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3411     }
3412 
3413   /* Now update local dynamic symbols.  */
3414   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3415     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3416 						  entry->isym.st_name);
3417 
3418   /* And the rest of dynamic symbols.  */
3419   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3420 
3421   /* Adjust version definitions.  */
3422   if (elf_tdata (output_bfd)->cverdefs)
3423     {
3424       asection *s;
3425       bfd_byte *p;
3426       size_t i;
3427       Elf_Internal_Verdef def;
3428       Elf_Internal_Verdaux defaux;
3429 
3430       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3431       p = s->contents;
3432       do
3433 	{
3434 	  _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3435 				   &def);
3436 	  p += sizeof (Elf_External_Verdef);
3437 	  if (def.vd_aux != sizeof (Elf_External_Verdef))
3438 	    continue;
3439 	  for (i = 0; i < def.vd_cnt; ++i)
3440 	    {
3441 	      _bfd_elf_swap_verdaux_in (output_bfd,
3442 					(Elf_External_Verdaux *) p, &defaux);
3443 	      defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3444 							defaux.vda_name);
3445 	      _bfd_elf_swap_verdaux_out (output_bfd,
3446 					 &defaux, (Elf_External_Verdaux *) p);
3447 	      p += sizeof (Elf_External_Verdaux);
3448 	    }
3449 	}
3450       while (def.vd_next);
3451     }
3452 
3453   /* Adjust version references.  */
3454   if (elf_tdata (output_bfd)->verref)
3455     {
3456       asection *s;
3457       bfd_byte *p;
3458       size_t i;
3459       Elf_Internal_Verneed need;
3460       Elf_Internal_Vernaux needaux;
3461 
3462       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3463       p = s->contents;
3464       do
3465 	{
3466 	  _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3467 				    &need);
3468 	  need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3469 	  _bfd_elf_swap_verneed_out (output_bfd, &need,
3470 				     (Elf_External_Verneed *) p);
3471 	  p += sizeof (Elf_External_Verneed);
3472 	  for (i = 0; i < need.vn_cnt; ++i)
3473 	    {
3474 	      _bfd_elf_swap_vernaux_in (output_bfd,
3475 					(Elf_External_Vernaux *) p, &needaux);
3476 	      needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3477 							 needaux.vna_name);
3478 	      _bfd_elf_swap_vernaux_out (output_bfd,
3479 					 &needaux,
3480 					 (Elf_External_Vernaux *) p);
3481 	      p += sizeof (Elf_External_Vernaux);
3482 	    }
3483 	}
3484       while (need.vn_next);
3485     }
3486 
3487   return TRUE;
3488 }
3489 
3490 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3491    The default is to only match when the INPUT and OUTPUT are exactly
3492    the same target.  */
3493 
3494 bfd_boolean
3495 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3496 				    const bfd_target *output)
3497 {
3498   return input == output;
3499 }
3500 
3501 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3502    This version is used when different targets for the same architecture
3503    are virtually identical.  */
3504 
3505 bfd_boolean
3506 _bfd_elf_relocs_compatible (const bfd_target *input,
3507 			    const bfd_target *output)
3508 {
3509   const struct elf_backend_data *obed, *ibed;
3510 
3511   if (input == output)
3512     return TRUE;
3513 
3514   ibed = xvec_get_elf_backend_data (input);
3515   obed = xvec_get_elf_backend_data (output);
3516 
3517   if (ibed->arch != obed->arch)
3518     return FALSE;
3519 
3520   /* If both backends are using this function, deem them compatible.  */
3521   return ibed->relocs_compatible == obed->relocs_compatible;
3522 }
3523 
3524 /* Make a special call to the linker "notice" function to tell it that
3525    we are about to handle an as-needed lib, or have finished
3526    processing the lib.  */
3527 
3528 bfd_boolean
3529 _bfd_elf_notice_as_needed (bfd *ibfd,
3530 			   struct bfd_link_info *info,
3531 			   enum notice_asneeded_action act)
3532 {
3533   return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3534 }
3535 
3536 /* Check relocations an ELF object file.  */
3537 
3538 bfd_boolean
3539 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3540 {
3541   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3542   struct elf_link_hash_table *htab = elf_hash_table (info);
3543 
3544   /* If this object is the same format as the output object, and it is
3545      not a shared library, then let the backend look through the
3546      relocs.
3547 
3548      This is required to build global offset table entries and to
3549      arrange for dynamic relocs.  It is not required for the
3550      particular common case of linking non PIC code, even when linking
3551      against shared libraries, but unfortunately there is no way of
3552      knowing whether an object file has been compiled PIC or not.
3553      Looking through the relocs is not particularly time consuming.
3554      The problem is that we must either (1) keep the relocs in memory,
3555      which causes the linker to require additional runtime memory or
3556      (2) read the relocs twice from the input file, which wastes time.
3557      This would be a good case for using mmap.
3558 
3559      I have no idea how to handle linking PIC code into a file of a
3560      different format.  It probably can't be done.  */
3561   if ((abfd->flags & DYNAMIC) == 0
3562       && is_elf_hash_table (htab)
3563       && bed->check_relocs != NULL
3564       && elf_object_id (abfd) == elf_hash_table_id (htab)
3565       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3566     {
3567       asection *o;
3568 
3569       for (o = abfd->sections; o != NULL; o = o->next)
3570 	{
3571 	  Elf_Internal_Rela *internal_relocs;
3572 	  bfd_boolean ok;
3573 
3574 	  /* Don't check relocations in excluded sections.  */
3575 	  if ((o->flags & SEC_RELOC) == 0
3576 	      || (o->flags & SEC_EXCLUDE) != 0
3577 	      || o->reloc_count == 0
3578 	      || ((info->strip == strip_all || info->strip == strip_debugger)
3579 		  && (o->flags & SEC_DEBUGGING) != 0)
3580 	      || bfd_is_abs_section (o->output_section))
3581 	    continue;
3582 
3583 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3584 						       info->keep_memory);
3585 	  if (internal_relocs == NULL)
3586 	    return FALSE;
3587 
3588 	  ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3589 
3590 	  if (elf_section_data (o)->relocs != internal_relocs)
3591 	    free (internal_relocs);
3592 
3593 	  if (! ok)
3594 	    return FALSE;
3595 	}
3596     }
3597 
3598   return TRUE;
3599 }
3600 
3601 /* Add symbols from an ELF object file to the linker hash table.  */
3602 
3603 static bfd_boolean
3604 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3605 {
3606   Elf_Internal_Ehdr *ehdr;
3607   Elf_Internal_Shdr *hdr;
3608   size_t symcount;
3609   size_t extsymcount;
3610   size_t extsymoff;
3611   struct elf_link_hash_entry **sym_hash;
3612   bfd_boolean dynamic;
3613   Elf_External_Versym *extversym = NULL;
3614   Elf_External_Versym *ever;
3615   struct elf_link_hash_entry *weaks;
3616   struct elf_link_hash_entry **nondeflt_vers = NULL;
3617   size_t nondeflt_vers_cnt = 0;
3618   Elf_Internal_Sym *isymbuf = NULL;
3619   Elf_Internal_Sym *isym;
3620   Elf_Internal_Sym *isymend;
3621   const struct elf_backend_data *bed;
3622   bfd_boolean add_needed;
3623   struct elf_link_hash_table *htab;
3624   bfd_size_type amt;
3625   void *alloc_mark = NULL;
3626   struct bfd_hash_entry **old_table = NULL;
3627   unsigned int old_size = 0;
3628   unsigned int old_count = 0;
3629   void *old_tab = NULL;
3630   void *old_ent;
3631   struct bfd_link_hash_entry *old_undefs = NULL;
3632   struct bfd_link_hash_entry *old_undefs_tail = NULL;
3633   void *old_strtab = NULL;
3634   size_t tabsize = 0;
3635   asection *s;
3636   bfd_boolean just_syms;
3637 
3638   htab = elf_hash_table (info);
3639   bed = get_elf_backend_data (abfd);
3640 
3641   if ((abfd->flags & DYNAMIC) == 0)
3642     dynamic = FALSE;
3643   else
3644     {
3645       dynamic = TRUE;
3646 
3647       /* You can't use -r against a dynamic object.  Also, there's no
3648 	 hope of using a dynamic object which does not exactly match
3649 	 the format of the output file.  */
3650       if (bfd_link_relocatable (info)
3651 	  || !is_elf_hash_table (htab)
3652 	  || info->output_bfd->xvec != abfd->xvec)
3653 	{
3654 	  if (bfd_link_relocatable (info))
3655 	    bfd_set_error (bfd_error_invalid_operation);
3656 	  else
3657 	    bfd_set_error (bfd_error_wrong_format);
3658 	  goto error_return;
3659 	}
3660     }
3661 
3662   ehdr = elf_elfheader (abfd);
3663   if (info->warn_alternate_em
3664       && bed->elf_machine_code != ehdr->e_machine
3665       && ((bed->elf_machine_alt1 != 0
3666 	   && ehdr->e_machine == bed->elf_machine_alt1)
3667 	  || (bed->elf_machine_alt2 != 0
3668 	      && ehdr->e_machine == bed->elf_machine_alt2)))
3669     info->callbacks->einfo
3670       (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3671        ehdr->e_machine, abfd, bed->elf_machine_code);
3672 
3673   /* As a GNU extension, any input sections which are named
3674      .gnu.warning.SYMBOL are treated as warning symbols for the given
3675      symbol.  This differs from .gnu.warning sections, which generate
3676      warnings when they are included in an output file.  */
3677   /* PR 12761: Also generate this warning when building shared libraries.  */
3678   for (s = abfd->sections; s != NULL; s = s->next)
3679     {
3680       const char *name;
3681 
3682       name = bfd_get_section_name (abfd, s);
3683       if (CONST_STRNEQ (name, ".gnu.warning."))
3684 	{
3685 	  char *msg;
3686 	  bfd_size_type sz;
3687 
3688 	  name += sizeof ".gnu.warning." - 1;
3689 
3690 	  /* If this is a shared object, then look up the symbol
3691 	     in the hash table.  If it is there, and it is already
3692 	     been defined, then we will not be using the entry
3693 	     from this shared object, so we don't need to warn.
3694 	     FIXME: If we see the definition in a regular object
3695 	     later on, we will warn, but we shouldn't.  The only
3696 	     fix is to keep track of what warnings we are supposed
3697 	     to emit, and then handle them all at the end of the
3698 	     link.  */
3699 	  if (dynamic)
3700 	    {
3701 	      struct elf_link_hash_entry *h;
3702 
3703 	      h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3704 
3705 	      /* FIXME: What about bfd_link_hash_common?  */
3706 	      if (h != NULL
3707 		  && (h->root.type == bfd_link_hash_defined
3708 		      || h->root.type == bfd_link_hash_defweak))
3709 		continue;
3710 	    }
3711 
3712 	  sz = s->size;
3713 	  msg = (char *) bfd_alloc (abfd, sz + 1);
3714 	  if (msg == NULL)
3715 	    goto error_return;
3716 
3717 	  if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3718 	    goto error_return;
3719 
3720 	  msg[sz] = '\0';
3721 
3722 	  if (! (_bfd_generic_link_add_one_symbol
3723 		 (info, abfd, name, BSF_WARNING, s, 0, msg,
3724 		  FALSE, bed->collect, NULL)))
3725 	    goto error_return;
3726 
3727 	  if (bfd_link_executable (info))
3728 	    {
3729 	      /* Clobber the section size so that the warning does
3730 		 not get copied into the output file.  */
3731 	      s->size = 0;
3732 
3733 	      /* Also set SEC_EXCLUDE, so that symbols defined in
3734 		 the warning section don't get copied to the output.  */
3735 	      s->flags |= SEC_EXCLUDE;
3736 	    }
3737 	}
3738     }
3739 
3740   just_syms = ((s = abfd->sections) != NULL
3741 	       && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3742 
3743   add_needed = TRUE;
3744   if (! dynamic)
3745     {
3746       /* If we are creating a shared library, create all the dynamic
3747 	 sections immediately.  We need to attach them to something,
3748 	 so we attach them to this BFD, provided it is the right
3749 	 format and is not from ld --just-symbols.  Always create the
3750 	 dynamic sections for -E/--dynamic-list.  FIXME: If there
3751 	 are no input BFD's of the same format as the output, we can't
3752 	 make a shared library.  */
3753       if (!just_syms
3754 	  && (bfd_link_pic (info)
3755 	      || (!bfd_link_relocatable (info)
3756 		  && (info->export_dynamic || info->dynamic)))
3757 	  && is_elf_hash_table (htab)
3758 	  && info->output_bfd->xvec == abfd->xvec
3759 	  && !htab->dynamic_sections_created)
3760 	{
3761 	  if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3762 	    goto error_return;
3763 	}
3764     }
3765   else if (!is_elf_hash_table (htab))
3766     goto error_return;
3767   else
3768     {
3769       const char *soname = NULL;
3770       char *audit = NULL;
3771       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3772       int ret;
3773 
3774       /* ld --just-symbols and dynamic objects don't mix very well.
3775 	 ld shouldn't allow it.  */
3776       if (just_syms)
3777 	abort ();
3778 
3779       /* If this dynamic lib was specified on the command line with
3780 	 --as-needed in effect, then we don't want to add a DT_NEEDED
3781 	 tag unless the lib is actually used.  Similary for libs brought
3782 	 in by another lib's DT_NEEDED.  When --no-add-needed is used
3783 	 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3784 	 any dynamic library in DT_NEEDED tags in the dynamic lib at
3785 	 all.  */
3786       add_needed = (elf_dyn_lib_class (abfd)
3787 		    & (DYN_AS_NEEDED | DYN_DT_NEEDED
3788 		       | DYN_NO_NEEDED)) == 0;
3789 
3790       s = bfd_get_section_by_name (abfd, ".dynamic");
3791       if (s != NULL)
3792 	{
3793 	  bfd_byte *dynbuf;
3794 	  bfd_byte *extdyn;
3795 	  unsigned int elfsec;
3796 	  unsigned long shlink;
3797 
3798 	  if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3799 	    {
3800 error_free_dyn:
3801 	      free (dynbuf);
3802 	      goto error_return;
3803 	    }
3804 
3805 	  elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3806 	  if (elfsec == SHN_BAD)
3807 	    goto error_free_dyn;
3808 	  shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3809 
3810 	  for (extdyn = dynbuf;
3811 	       extdyn < dynbuf + s->size;
3812 	       extdyn += bed->s->sizeof_dyn)
3813 	    {
3814 	      Elf_Internal_Dyn dyn;
3815 
3816 	      bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3817 	      if (dyn.d_tag == DT_SONAME)
3818 		{
3819 		  unsigned int tagv = dyn.d_un.d_val;
3820 		  soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3821 		  if (soname == NULL)
3822 		    goto error_free_dyn;
3823 		}
3824 	      if (dyn.d_tag == DT_NEEDED)
3825 		{
3826 		  struct bfd_link_needed_list *n, **pn;
3827 		  char *fnm, *anm;
3828 		  unsigned int tagv = dyn.d_un.d_val;
3829 
3830 		  amt = sizeof (struct bfd_link_needed_list);
3831 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3832 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3833 		  if (n == NULL || fnm == NULL)
3834 		    goto error_free_dyn;
3835 		  amt = strlen (fnm) + 1;
3836 		  anm = (char *) bfd_alloc (abfd, amt);
3837 		  if (anm == NULL)
3838 		    goto error_free_dyn;
3839 		  memcpy (anm, fnm, amt);
3840 		  n->name = anm;
3841 		  n->by = abfd;
3842 		  n->next = NULL;
3843 		  for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3844 		    ;
3845 		  *pn = n;
3846 		}
3847 	      if (dyn.d_tag == DT_RUNPATH)
3848 		{
3849 		  struct bfd_link_needed_list *n, **pn;
3850 		  char *fnm, *anm;
3851 		  unsigned int tagv = dyn.d_un.d_val;
3852 
3853 		  amt = sizeof (struct bfd_link_needed_list);
3854 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3855 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3856 		  if (n == NULL || fnm == NULL)
3857 		    goto error_free_dyn;
3858 		  amt = strlen (fnm) + 1;
3859 		  anm = (char *) bfd_alloc (abfd, amt);
3860 		  if (anm == NULL)
3861 		    goto error_free_dyn;
3862 		  memcpy (anm, fnm, amt);
3863 		  n->name = anm;
3864 		  n->by = abfd;
3865 		  n->next = NULL;
3866 		  for (pn = & runpath;
3867 		       *pn != NULL;
3868 		       pn = &(*pn)->next)
3869 		    ;
3870 		  *pn = n;
3871 		}
3872 	      /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
3873 	      if (!runpath && dyn.d_tag == DT_RPATH)
3874 		{
3875 		  struct bfd_link_needed_list *n, **pn;
3876 		  char *fnm, *anm;
3877 		  unsigned int tagv = dyn.d_un.d_val;
3878 
3879 		  amt = sizeof (struct bfd_link_needed_list);
3880 		  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3881 		  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3882 		  if (n == NULL || fnm == NULL)
3883 		    goto error_free_dyn;
3884 		  amt = strlen (fnm) + 1;
3885 		  anm = (char *) bfd_alloc (abfd, amt);
3886 		  if (anm == NULL)
3887 		    goto error_free_dyn;
3888 		  memcpy (anm, fnm, amt);
3889 		  n->name = anm;
3890 		  n->by = abfd;
3891 		  n->next = NULL;
3892 		  for (pn = & rpath;
3893 		       *pn != NULL;
3894 		       pn = &(*pn)->next)
3895 		    ;
3896 		  *pn = n;
3897 		}
3898 	      if (dyn.d_tag == DT_AUDIT)
3899 		{
3900 		  unsigned int tagv = dyn.d_un.d_val;
3901 		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3902 		}
3903 	    }
3904 
3905 	  free (dynbuf);
3906 	}
3907 
3908       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
3909 	 frees all more recently bfd_alloc'd blocks as well.  */
3910       if (runpath)
3911 	rpath = runpath;
3912 
3913       if (rpath)
3914 	{
3915 	  struct bfd_link_needed_list **pn;
3916 	  for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3917 	    ;
3918 	  *pn = rpath;
3919 	}
3920 
3921       /* We do not want to include any of the sections in a dynamic
3922 	 object in the output file.  We hack by simply clobbering the
3923 	 list of sections in the BFD.  This could be handled more
3924 	 cleanly by, say, a new section flag; the existing
3925 	 SEC_NEVER_LOAD flag is not the one we want, because that one
3926 	 still implies that the section takes up space in the output
3927 	 file.  */
3928       bfd_section_list_clear (abfd);
3929 
3930       /* Find the name to use in a DT_NEEDED entry that refers to this
3931 	 object.  If the object has a DT_SONAME entry, we use it.
3932 	 Otherwise, if the generic linker stuck something in
3933 	 elf_dt_name, we use that.  Otherwise, we just use the file
3934 	 name.  */
3935       if (soname == NULL || *soname == '\0')
3936 	{
3937 	  soname = elf_dt_name (abfd);
3938 	  if (soname == NULL || *soname == '\0')
3939 	    soname = bfd_get_filename (abfd);
3940 	}
3941 
3942       /* Save the SONAME because sometimes the linker emulation code
3943 	 will need to know it.  */
3944       elf_dt_name (abfd) = soname;
3945 
3946       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3947       if (ret < 0)
3948 	goto error_return;
3949 
3950       /* If we have already included this dynamic object in the
3951 	 link, just ignore it.  There is no reason to include a
3952 	 particular dynamic object more than once.  */
3953       if (ret > 0)
3954 	return TRUE;
3955 
3956       /* Save the DT_AUDIT entry for the linker emulation code. */
3957       elf_dt_audit (abfd) = audit;
3958     }
3959 
3960   /* If this is a dynamic object, we always link against the .dynsym
3961      symbol table, not the .symtab symbol table.  The dynamic linker
3962      will only see the .dynsym symbol table, so there is no reason to
3963      look at .symtab for a dynamic object.  */
3964 
3965   if (! dynamic || elf_dynsymtab (abfd) == 0)
3966     hdr = &elf_tdata (abfd)->symtab_hdr;
3967   else
3968     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3969 
3970   symcount = hdr->sh_size / bed->s->sizeof_sym;
3971 
3972   /* The sh_info field of the symtab header tells us where the
3973      external symbols start.  We don't care about the local symbols at
3974      this point.  */
3975   if (elf_bad_symtab (abfd))
3976     {
3977       extsymcount = symcount;
3978       extsymoff = 0;
3979     }
3980   else
3981     {
3982       extsymcount = symcount - hdr->sh_info;
3983       extsymoff = hdr->sh_info;
3984     }
3985 
3986   sym_hash = elf_sym_hashes (abfd);
3987   if (extsymcount != 0)
3988     {
3989       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3990 				      NULL, NULL, NULL);
3991       if (isymbuf == NULL)
3992 	goto error_return;
3993 
3994       if (sym_hash == NULL)
3995 	{
3996 	  /* We store a pointer to the hash table entry for each
3997 	     external symbol.  */
3998 	  amt = extsymcount;
3999 	  amt *= sizeof (struct elf_link_hash_entry *);
4000 	  sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4001 	  if (sym_hash == NULL)
4002 	    goto error_free_sym;
4003 	  elf_sym_hashes (abfd) = sym_hash;
4004 	}
4005     }
4006 
4007   if (dynamic)
4008     {
4009       /* Read in any version definitions.  */
4010       if (!_bfd_elf_slurp_version_tables (abfd,
4011 					  info->default_imported_symver))
4012 	goto error_free_sym;
4013 
4014       /* Read in the symbol versions, but don't bother to convert them
4015 	 to internal format.  */
4016       if (elf_dynversym (abfd) != 0)
4017 	{
4018 	  Elf_Internal_Shdr *versymhdr;
4019 
4020 	  versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4021 	  extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4022 	  if (extversym == NULL)
4023 	    goto error_free_sym;
4024 	  amt = versymhdr->sh_size;
4025 	  if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4026 	      || bfd_bread (extversym, amt, abfd) != amt)
4027 	    goto error_free_vers;
4028 	}
4029     }
4030 
4031   /* If we are loading an as-needed shared lib, save the symbol table
4032      state before we start adding symbols.  If the lib turns out
4033      to be unneeded, restore the state.  */
4034   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4035     {
4036       unsigned int i;
4037       size_t entsize;
4038 
4039       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4040 	{
4041 	  struct bfd_hash_entry *p;
4042 	  struct elf_link_hash_entry *h;
4043 
4044 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4045 	    {
4046 	      h = (struct elf_link_hash_entry *) p;
4047 	      entsize += htab->root.table.entsize;
4048 	      if (h->root.type == bfd_link_hash_warning)
4049 		entsize += htab->root.table.entsize;
4050 	    }
4051 	}
4052 
4053       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4054       old_tab = bfd_malloc (tabsize + entsize);
4055       if (old_tab == NULL)
4056 	goto error_free_vers;
4057 
4058       /* Remember the current objalloc pointer, so that all mem for
4059 	 symbols added can later be reclaimed.  */
4060       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4061       if (alloc_mark == NULL)
4062 	goto error_free_vers;
4063 
4064       /* Make a special call to the linker "notice" function to
4065 	 tell it that we are about to handle an as-needed lib.  */
4066       if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4067 	goto error_free_vers;
4068 
4069       /* Clone the symbol table.  Remember some pointers into the
4070 	 symbol table, and dynamic symbol count.  */
4071       old_ent = (char *) old_tab + tabsize;
4072       memcpy (old_tab, htab->root.table.table, tabsize);
4073       old_undefs = htab->root.undefs;
4074       old_undefs_tail = htab->root.undefs_tail;
4075       old_table = htab->root.table.table;
4076       old_size = htab->root.table.size;
4077       old_count = htab->root.table.count;
4078       old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4079       if (old_strtab == NULL)
4080 	goto error_free_vers;
4081 
4082       for (i = 0; i < htab->root.table.size; i++)
4083 	{
4084 	  struct bfd_hash_entry *p;
4085 	  struct elf_link_hash_entry *h;
4086 
4087 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4088 	    {
4089 	      memcpy (old_ent, p, htab->root.table.entsize);
4090 	      old_ent = (char *) old_ent + htab->root.table.entsize;
4091 	      h = (struct elf_link_hash_entry *) p;
4092 	      if (h->root.type == bfd_link_hash_warning)
4093 		{
4094 		  memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4095 		  old_ent = (char *) old_ent + htab->root.table.entsize;
4096 		}
4097 	    }
4098 	}
4099     }
4100 
4101   weaks = NULL;
4102   ever = extversym != NULL ? extversym + extsymoff : NULL;
4103   for (isym = isymbuf, isymend = isymbuf + extsymcount;
4104        isym < isymend;
4105        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4106     {
4107       int bind;
4108       bfd_vma value;
4109       asection *sec, *new_sec;
4110       flagword flags;
4111       const char *name;
4112       struct elf_link_hash_entry *h;
4113       struct elf_link_hash_entry *hi;
4114       bfd_boolean definition;
4115       bfd_boolean size_change_ok;
4116       bfd_boolean type_change_ok;
4117       bfd_boolean new_weakdef;
4118       bfd_boolean new_weak;
4119       bfd_boolean old_weak;
4120       bfd_boolean override;
4121       bfd_boolean common;
4122       bfd_boolean discarded;
4123       unsigned int old_alignment;
4124       bfd *old_bfd;
4125       bfd_boolean matched;
4126 
4127       override = FALSE;
4128 
4129       flags = BSF_NO_FLAGS;
4130       sec = NULL;
4131       value = isym->st_value;
4132       common = bed->common_definition (isym);
4133       discarded = FALSE;
4134 
4135       bind = ELF_ST_BIND (isym->st_info);
4136       switch (bind)
4137 	{
4138 	case STB_LOCAL:
4139 	  /* This should be impossible, since ELF requires that all
4140 	     global symbols follow all local symbols, and that sh_info
4141 	     point to the first global symbol.  Unfortunately, Irix 5
4142 	     screws this up.  */
4143 	  continue;
4144 
4145 	case STB_GLOBAL:
4146 	  if (isym->st_shndx != SHN_UNDEF && !common)
4147 	    flags = BSF_GLOBAL;
4148 	  break;
4149 
4150 	case STB_WEAK:
4151 	  flags = BSF_WEAK;
4152 	  break;
4153 
4154 	case STB_GNU_UNIQUE:
4155 	  flags = BSF_GNU_UNIQUE;
4156 	  break;
4157 
4158 	default:
4159 	  /* Leave it up to the processor backend.  */
4160 	  break;
4161 	}
4162 
4163       if (isym->st_shndx == SHN_UNDEF)
4164 	sec = bfd_und_section_ptr;
4165       else if (isym->st_shndx == SHN_ABS)
4166 	sec = bfd_abs_section_ptr;
4167       else if (isym->st_shndx == SHN_COMMON)
4168 	{
4169 	  sec = bfd_com_section_ptr;
4170 	  /* What ELF calls the size we call the value.  What ELF
4171 	     calls the value we call the alignment.  */
4172 	  value = isym->st_size;
4173 	}
4174       else
4175 	{
4176 	  sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4177 	  if (sec == NULL)
4178 	    sec = bfd_abs_section_ptr;
4179 	  else if (discarded_section (sec))
4180 	    {
4181 	      /* Symbols from discarded section are undefined.  We keep
4182 		 its visibility.  */
4183 	      sec = bfd_und_section_ptr;
4184 	      discarded = TRUE;
4185 	      isym->st_shndx = SHN_UNDEF;
4186 	    }
4187 	  else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4188 	    value -= sec->vma;
4189 	}
4190 
4191       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4192 					      isym->st_name);
4193       if (name == NULL)
4194 	goto error_free_vers;
4195 
4196       if (isym->st_shndx == SHN_COMMON
4197 	  && (abfd->flags & BFD_PLUGIN) != 0)
4198 	{
4199 	  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4200 
4201 	  if (xc == NULL)
4202 	    {
4203 	      flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4204 				 | SEC_EXCLUDE);
4205 	      xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4206 	      if (xc == NULL)
4207 		goto error_free_vers;
4208 	    }
4209 	  sec = xc;
4210 	}
4211       else if (isym->st_shndx == SHN_COMMON
4212 	       && ELF_ST_TYPE (isym->st_info) == STT_TLS
4213 	       && !bfd_link_relocatable (info))
4214 	{
4215 	  asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4216 
4217 	  if (tcomm == NULL)
4218 	    {
4219 	      flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4220 				 | SEC_LINKER_CREATED);
4221 	      tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4222 	      if (tcomm == NULL)
4223 		goto error_free_vers;
4224 	    }
4225 	  sec = tcomm;
4226 	}
4227       else if (bed->elf_add_symbol_hook)
4228 	{
4229 	  if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4230 					     &sec, &value))
4231 	    goto error_free_vers;
4232 
4233 	  /* The hook function sets the name to NULL if this symbol
4234 	     should be skipped for some reason.  */
4235 	  if (name == NULL)
4236 	    continue;
4237 	}
4238 
4239       /* Sanity check that all possibilities were handled.  */
4240       if (sec == NULL)
4241 	{
4242 	  bfd_set_error (bfd_error_bad_value);
4243 	  goto error_free_vers;
4244 	}
4245 
4246       /* Silently discard TLS symbols from --just-syms.  There's
4247 	 no way to combine a static TLS block with a new TLS block
4248 	 for this executable.  */
4249       if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4250 	  && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4251 	continue;
4252 
4253       if (bfd_is_und_section (sec)
4254 	  || bfd_is_com_section (sec))
4255 	definition = FALSE;
4256       else
4257 	definition = TRUE;
4258 
4259       size_change_ok = FALSE;
4260       type_change_ok = bed->type_change_ok;
4261       old_weak = FALSE;
4262       matched = FALSE;
4263       old_alignment = 0;
4264       old_bfd = NULL;
4265       new_sec = sec;
4266 
4267       if (is_elf_hash_table (htab))
4268 	{
4269 	  Elf_Internal_Versym iver;
4270 	  unsigned int vernum = 0;
4271 	  bfd_boolean skip;
4272 
4273 	  if (ever == NULL)
4274 	    {
4275 	      if (info->default_imported_symver)
4276 		/* Use the default symbol version created earlier.  */
4277 		iver.vs_vers = elf_tdata (abfd)->cverdefs;
4278 	      else
4279 		iver.vs_vers = 0;
4280 	    }
4281 	  else
4282 	    _bfd_elf_swap_versym_in (abfd, ever, &iver);
4283 
4284 	  vernum = iver.vs_vers & VERSYM_VERSION;
4285 
4286 	  /* If this is a hidden symbol, or if it is not version
4287 	     1, we append the version name to the symbol name.
4288 	     However, we do not modify a non-hidden absolute symbol
4289 	     if it is not a function, because it might be the version
4290 	     symbol itself.  FIXME: What if it isn't?  */
4291 	  if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4292 	      || (vernum > 1
4293 		  && (!bfd_is_abs_section (sec)
4294 		      || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4295 	    {
4296 	      const char *verstr;
4297 	      size_t namelen, verlen, newlen;
4298 	      char *newname, *p;
4299 
4300 	      if (isym->st_shndx != SHN_UNDEF)
4301 		{
4302 		  if (vernum > elf_tdata (abfd)->cverdefs)
4303 		    verstr = NULL;
4304 		  else if (vernum > 1)
4305 		    verstr =
4306 		      elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4307 		  else
4308 		    verstr = "";
4309 
4310 		  if (verstr == NULL)
4311 		    {
4312 		      (*_bfd_error_handler)
4313 			(_("%B: %s: invalid version %u (max %d)"),
4314 			 abfd, name, vernum,
4315 			 elf_tdata (abfd)->cverdefs);
4316 		      bfd_set_error (bfd_error_bad_value);
4317 		      goto error_free_vers;
4318 		    }
4319 		}
4320 	      else
4321 		{
4322 		  /* We cannot simply test for the number of
4323 		     entries in the VERNEED section since the
4324 		     numbers for the needed versions do not start
4325 		     at 0.  */
4326 		  Elf_Internal_Verneed *t;
4327 
4328 		  verstr = NULL;
4329 		  for (t = elf_tdata (abfd)->verref;
4330 		       t != NULL;
4331 		       t = t->vn_nextref)
4332 		    {
4333 		      Elf_Internal_Vernaux *a;
4334 
4335 		      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4336 			{
4337 			  if (a->vna_other == vernum)
4338 			    {
4339 			      verstr = a->vna_nodename;
4340 			      break;
4341 			    }
4342 			}
4343 		      if (a != NULL)
4344 			break;
4345 		    }
4346 		  if (verstr == NULL)
4347 		    {
4348 		      (*_bfd_error_handler)
4349 			(_("%B: %s: invalid needed version %d"),
4350 			 abfd, name, vernum);
4351 		      bfd_set_error (bfd_error_bad_value);
4352 		      goto error_free_vers;
4353 		    }
4354 		}
4355 
4356 	      namelen = strlen (name);
4357 	      verlen = strlen (verstr);
4358 	      newlen = namelen + verlen + 2;
4359 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4360 		  && isym->st_shndx != SHN_UNDEF)
4361 		++newlen;
4362 
4363 	      newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4364 	      if (newname == NULL)
4365 		goto error_free_vers;
4366 	      memcpy (newname, name, namelen);
4367 	      p = newname + namelen;
4368 	      *p++ = ELF_VER_CHR;
4369 	      /* If this is a defined non-hidden version symbol,
4370 		 we add another @ to the name.  This indicates the
4371 		 default version of the symbol.  */
4372 	      if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4373 		  && isym->st_shndx != SHN_UNDEF)
4374 		*p++ = ELF_VER_CHR;
4375 	      memcpy (p, verstr, verlen + 1);
4376 
4377 	      name = newname;
4378 	    }
4379 
4380 	  /* If this symbol has default visibility and the user has
4381 	     requested we not re-export it, then mark it as hidden.  */
4382 	  if (!bfd_is_und_section (sec)
4383 	      && !dynamic
4384 	      && abfd->no_export
4385 	      && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4386 	    isym->st_other = (STV_HIDDEN
4387 			      | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4388 
4389 	  if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4390 				      sym_hash, &old_bfd, &old_weak,
4391 				      &old_alignment, &skip, &override,
4392 				      &type_change_ok, &size_change_ok,
4393 				      &matched))
4394 	    goto error_free_vers;
4395 
4396 	  if (skip)
4397 	    continue;
4398 
4399 	  /* Override a definition only if the new symbol matches the
4400 	     existing one.  */
4401 	  if (override && matched)
4402 	    definition = FALSE;
4403 
4404 	  h = *sym_hash;
4405 	  while (h->root.type == bfd_link_hash_indirect
4406 		 || h->root.type == bfd_link_hash_warning)
4407 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4408 
4409 	  if (elf_tdata (abfd)->verdef != NULL
4410 	      && vernum > 1
4411 	      && definition)
4412 	    h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4413 	}
4414 
4415       if (! (_bfd_generic_link_add_one_symbol
4416 	     (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4417 	      (struct bfd_link_hash_entry **) sym_hash)))
4418 	goto error_free_vers;
4419 
4420       if ((flags & BSF_GNU_UNIQUE)
4421 	  && (abfd->flags & DYNAMIC) == 0
4422 	  && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4423 	elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4424 
4425       h = *sym_hash;
4426       /* We need to make sure that indirect symbol dynamic flags are
4427 	 updated.  */
4428       hi = h;
4429       while (h->root.type == bfd_link_hash_indirect
4430 	     || h->root.type == bfd_link_hash_warning)
4431 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
4432 
4433       /* Setting the index to -3 tells elf_link_output_extsym that
4434 	 this symbol is defined in a discarded section.  */
4435       if (discarded)
4436 	h->indx = -3;
4437 
4438       *sym_hash = h;
4439 
4440       new_weak = (flags & BSF_WEAK) != 0;
4441       new_weakdef = FALSE;
4442       if (dynamic
4443 	  && definition
4444 	  && new_weak
4445 	  && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4446 	  && is_elf_hash_table (htab)
4447 	  && h->u.weakdef == NULL)
4448 	{
4449 	  /* Keep a list of all weak defined non function symbols from
4450 	     a dynamic object, using the weakdef field.  Later in this
4451 	     function we will set the weakdef field to the correct
4452 	     value.  We only put non-function symbols from dynamic
4453 	     objects on this list, because that happens to be the only
4454 	     time we need to know the normal symbol corresponding to a
4455 	     weak symbol, and the information is time consuming to
4456 	     figure out.  If the weakdef field is not already NULL,
4457 	     then this symbol was already defined by some previous
4458 	     dynamic object, and we will be using that previous
4459 	     definition anyhow.  */
4460 
4461 	  h->u.weakdef = weaks;
4462 	  weaks = h;
4463 	  new_weakdef = TRUE;
4464 	}
4465 
4466       /* Set the alignment of a common symbol.  */
4467       if ((common || bfd_is_com_section (sec))
4468 	  && h->root.type == bfd_link_hash_common)
4469 	{
4470 	  unsigned int align;
4471 
4472 	  if (common)
4473 	    align = bfd_log2 (isym->st_value);
4474 	  else
4475 	    {
4476 	      /* The new symbol is a common symbol in a shared object.
4477 		 We need to get the alignment from the section.  */
4478 	      align = new_sec->alignment_power;
4479 	    }
4480 	  if (align > old_alignment)
4481 	    h->root.u.c.p->alignment_power = align;
4482 	  else
4483 	    h->root.u.c.p->alignment_power = old_alignment;
4484 	}
4485 
4486       if (is_elf_hash_table (htab))
4487 	{
4488 	  /* Set a flag in the hash table entry indicating the type of
4489 	     reference or definition we just found.  A dynamic symbol
4490 	     is one which is referenced or defined by both a regular
4491 	     object and a shared object.  */
4492 	  bfd_boolean dynsym = FALSE;
4493 
4494 	  /* Plugin symbols aren't normal.  Don't set def_regular or
4495 	     ref_regular for them, or make them dynamic.  */
4496 	  if ((abfd->flags & BFD_PLUGIN) != 0)
4497 	    ;
4498 	  else if (! dynamic)
4499 	    {
4500 	      if (! definition)
4501 		{
4502 		  h->ref_regular = 1;
4503 		  if (bind != STB_WEAK)
4504 		    h->ref_regular_nonweak = 1;
4505 		}
4506 	      else
4507 		{
4508 		  h->def_regular = 1;
4509 		  if (h->def_dynamic)
4510 		    {
4511 		      h->def_dynamic = 0;
4512 		      h->ref_dynamic = 1;
4513 		    }
4514 		}
4515 
4516 	      /* If the indirect symbol has been forced local, don't
4517 		 make the real symbol dynamic.  */
4518 	      if ((h == hi || !hi->forced_local)
4519 		  && (bfd_link_dll (info)
4520 		      || h->def_dynamic
4521 		      || h->ref_dynamic))
4522 		dynsym = TRUE;
4523 	    }
4524 	  else
4525 	    {
4526 	      if (! definition)
4527 		{
4528 		  h->ref_dynamic = 1;
4529 		  hi->ref_dynamic = 1;
4530 		}
4531 	      else
4532 		{
4533 		  h->def_dynamic = 1;
4534 		  hi->def_dynamic = 1;
4535 		}
4536 
4537 	      /* If the indirect symbol has been forced local, don't
4538 		 make the real symbol dynamic.  */
4539 	      if ((h == hi || !hi->forced_local)
4540 		  && (h->def_regular
4541 		      || h->ref_regular
4542 		      || (h->u.weakdef != NULL
4543 			  && ! new_weakdef
4544 			  && h->u.weakdef->dynindx != -1)))
4545 		dynsym = TRUE;
4546 	    }
4547 
4548 	  /* Check to see if we need to add an indirect symbol for
4549 	     the default name.  */
4550 	  if (definition
4551 	      || (!override && h->root.type == bfd_link_hash_common))
4552 	    if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4553 					      sec, value, &old_bfd, &dynsym))
4554 	      goto error_free_vers;
4555 
4556 	  /* Check the alignment when a common symbol is involved. This
4557 	     can change when a common symbol is overridden by a normal
4558 	     definition or a common symbol is ignored due to the old
4559 	     normal definition. We need to make sure the maximum
4560 	     alignment is maintained.  */
4561 	  if ((old_alignment || common)
4562 	      && h->root.type != bfd_link_hash_common)
4563 	    {
4564 	      unsigned int common_align;
4565 	      unsigned int normal_align;
4566 	      unsigned int symbol_align;
4567 	      bfd *normal_bfd;
4568 	      bfd *common_bfd;
4569 
4570 	      BFD_ASSERT (h->root.type == bfd_link_hash_defined
4571 			  || h->root.type == bfd_link_hash_defweak);
4572 
4573 	      symbol_align = ffs (h->root.u.def.value) - 1;
4574 	      if (h->root.u.def.section->owner != NULL
4575 		  && (h->root.u.def.section->owner->flags
4576 		       & (DYNAMIC | BFD_PLUGIN)) == 0)
4577 		{
4578 		  normal_align = h->root.u.def.section->alignment_power;
4579 		  if (normal_align > symbol_align)
4580 		    normal_align = symbol_align;
4581 		}
4582 	      else
4583 		normal_align = symbol_align;
4584 
4585 	      if (old_alignment)
4586 		{
4587 		  common_align = old_alignment;
4588 		  common_bfd = old_bfd;
4589 		  normal_bfd = abfd;
4590 		}
4591 	      else
4592 		{
4593 		  common_align = bfd_log2 (isym->st_value);
4594 		  common_bfd = abfd;
4595 		  normal_bfd = old_bfd;
4596 		}
4597 
4598 	      if (normal_align < common_align)
4599 		{
4600 		  /* PR binutils/2735 */
4601 		  if (normal_bfd == NULL)
4602 		    (*_bfd_error_handler)
4603 		      (_("Warning: alignment %u of common symbol `%s' in %B is"
4604 			 " greater than the alignment (%u) of its section %A"),
4605 		       common_bfd, h->root.u.def.section,
4606 		       1 << common_align, name, 1 << normal_align);
4607 		  else
4608 		    (*_bfd_error_handler)
4609 		      (_("Warning: alignment %u of symbol `%s' in %B"
4610 			 " is smaller than %u in %B"),
4611 		       normal_bfd, common_bfd,
4612 		       1 << normal_align, name, 1 << common_align);
4613 		}
4614 	    }
4615 
4616 	  /* Remember the symbol size if it isn't undefined.  */
4617 	  if (isym->st_size != 0
4618 	      && isym->st_shndx != SHN_UNDEF
4619 	      && (definition || h->size == 0))
4620 	    {
4621 	      if (h->size != 0
4622 		  && h->size != isym->st_size
4623 		  && ! size_change_ok)
4624 		(*_bfd_error_handler)
4625 		  (_("Warning: size of symbol `%s' changed"
4626 		     " from %lu in %B to %lu in %B"),
4627 		   old_bfd, abfd,
4628 		   name, (unsigned long) h->size,
4629 		   (unsigned long) isym->st_size);
4630 
4631 	      h->size = isym->st_size;
4632 	    }
4633 
4634 	  /* If this is a common symbol, then we always want H->SIZE
4635 	     to be the size of the common symbol.  The code just above
4636 	     won't fix the size if a common symbol becomes larger.  We
4637 	     don't warn about a size change here, because that is
4638 	     covered by --warn-common.  Allow changes between different
4639 	     function types.  */
4640 	  if (h->root.type == bfd_link_hash_common)
4641 	    h->size = h->root.u.c.size;
4642 
4643 	  if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4644 	      && ((definition && !new_weak)
4645 		  || (old_weak && h->root.type == bfd_link_hash_common)
4646 		  || h->type == STT_NOTYPE))
4647 	    {
4648 	      unsigned int type = ELF_ST_TYPE (isym->st_info);
4649 
4650 	      /* Turn an IFUNC symbol from a DSO into a normal FUNC
4651 		 symbol.  */
4652 	      if (type == STT_GNU_IFUNC
4653 		  && (abfd->flags & DYNAMIC) != 0)
4654 		type = STT_FUNC;
4655 
4656 	      if (h->type != type)
4657 		{
4658 		  if (h->type != STT_NOTYPE && ! type_change_ok)
4659 		    (*_bfd_error_handler)
4660 		      (_("Warning: type of symbol `%s' changed"
4661 			 " from %d to %d in %B"),
4662 		       abfd, name, h->type, type);
4663 
4664 		  h->type = type;
4665 		}
4666 	    }
4667 
4668 	  /* Merge st_other field.  */
4669 	  elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4670 
4671 	  /* We don't want to make debug symbol dynamic.  */
4672 	  if (definition
4673 	      && (sec->flags & SEC_DEBUGGING)
4674 	      && !bfd_link_relocatable (info))
4675 	    dynsym = FALSE;
4676 
4677 	  /* Nor should we make plugin symbols dynamic.  */
4678 	  if ((abfd->flags & BFD_PLUGIN) != 0)
4679 	    dynsym = FALSE;
4680 
4681 	  if (definition)
4682 	    {
4683 	      h->target_internal = isym->st_target_internal;
4684 	      h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4685 	    }
4686 
4687 	  if (definition && !dynamic)
4688 	    {
4689 	      char *p = strchr (name, ELF_VER_CHR);
4690 	      if (p != NULL && p[1] != ELF_VER_CHR)
4691 		{
4692 		  /* Queue non-default versions so that .symver x, x@FOO
4693 		     aliases can be checked.  */
4694 		  if (!nondeflt_vers)
4695 		    {
4696 		      amt = ((isymend - isym + 1)
4697 			     * sizeof (struct elf_link_hash_entry *));
4698 		      nondeflt_vers
4699 			= (struct elf_link_hash_entry **) bfd_malloc (amt);
4700 		      if (!nondeflt_vers)
4701 			goto error_free_vers;
4702 		    }
4703 		  nondeflt_vers[nondeflt_vers_cnt++] = h;
4704 		}
4705 	    }
4706 
4707 	  if (dynsym && h->dynindx == -1)
4708 	    {
4709 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4710 		goto error_free_vers;
4711 	      if (h->u.weakdef != NULL
4712 		  && ! new_weakdef
4713 		  && h->u.weakdef->dynindx == -1)
4714 		{
4715 		  if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4716 		    goto error_free_vers;
4717 		}
4718 	    }
4719 	  else if (h->dynindx != -1)
4720 	    /* If the symbol already has a dynamic index, but
4721 	       visibility says it should not be visible, turn it into
4722 	       a local symbol.  */
4723 	    switch (ELF_ST_VISIBILITY (h->other))
4724 	      {
4725 	      case STV_INTERNAL:
4726 	      case STV_HIDDEN:
4727 		(*bed->elf_backend_hide_symbol) (info, h, TRUE);
4728 		dynsym = FALSE;
4729 		break;
4730 	      }
4731 
4732 	  /* Don't add DT_NEEDED for references from the dummy bfd nor
4733 	     for unmatched symbol.  */
4734 	  if (!add_needed
4735 	      && matched
4736 	      && definition
4737 	      && ((dynsym
4738 		   && h->ref_regular_nonweak
4739 		   && (old_bfd == NULL
4740 		       || (old_bfd->flags & BFD_PLUGIN) == 0))
4741 		  || (h->ref_dynamic_nonweak
4742 		      && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4743 		      && !on_needed_list (elf_dt_name (abfd),
4744 					  htab->needed, NULL))))
4745 	    {
4746 	      int ret;
4747 	      const char *soname = elf_dt_name (abfd);
4748 
4749 	      info->callbacks->minfo ("%!", soname, old_bfd,
4750 				      h->root.root.string);
4751 
4752 	      /* A symbol from a library loaded via DT_NEEDED of some
4753 		 other library is referenced by a regular object.
4754 		 Add a DT_NEEDED entry for it.  Issue an error if
4755 		 --no-add-needed is used and the reference was not
4756 		 a weak one.  */
4757 	      if (old_bfd != NULL
4758 		  && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4759 		{
4760 		  (*_bfd_error_handler)
4761 		    (_("%B: undefined reference to symbol '%s'"),
4762 		     old_bfd, name);
4763 		  bfd_set_error (bfd_error_missing_dso);
4764 		  goto error_free_vers;
4765 		}
4766 
4767 	      elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4768 		(elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4769 
4770 	      add_needed = TRUE;
4771 	      ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4772 	      if (ret < 0)
4773 		goto error_free_vers;
4774 
4775 	      BFD_ASSERT (ret == 0);
4776 	    }
4777 	}
4778     }
4779 
4780   if (extversym != NULL)
4781     {
4782       free (extversym);
4783       extversym = NULL;
4784     }
4785 
4786   if (isymbuf != NULL)
4787     {
4788       free (isymbuf);
4789       isymbuf = NULL;
4790     }
4791 
4792   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4793     {
4794       unsigned int i;
4795 
4796       /* Restore the symbol table.  */
4797       old_ent = (char *) old_tab + tabsize;
4798       memset (elf_sym_hashes (abfd), 0,
4799 	      extsymcount * sizeof (struct elf_link_hash_entry *));
4800       htab->root.table.table = old_table;
4801       htab->root.table.size = old_size;
4802       htab->root.table.count = old_count;
4803       memcpy (htab->root.table.table, old_tab, tabsize);
4804       htab->root.undefs = old_undefs;
4805       htab->root.undefs_tail = old_undefs_tail;
4806       _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4807       free (old_strtab);
4808       old_strtab = NULL;
4809       for (i = 0; i < htab->root.table.size; i++)
4810 	{
4811 	  struct bfd_hash_entry *p;
4812 	  struct elf_link_hash_entry *h;
4813 	  bfd_size_type size;
4814 	  unsigned int alignment_power;
4815 
4816 	  for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4817 	    {
4818 	      h = (struct elf_link_hash_entry *) p;
4819 	      if (h->root.type == bfd_link_hash_warning)
4820 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
4821 
4822 	      /* Preserve the maximum alignment and size for common
4823 		 symbols even if this dynamic lib isn't on DT_NEEDED
4824 		 since it can still be loaded at run time by another
4825 		 dynamic lib.  */
4826 	      if (h->root.type == bfd_link_hash_common)
4827 		{
4828 		  size = h->root.u.c.size;
4829 		  alignment_power = h->root.u.c.p->alignment_power;
4830 		}
4831 	      else
4832 		{
4833 		  size = 0;
4834 		  alignment_power = 0;
4835 		}
4836 	      memcpy (p, old_ent, htab->root.table.entsize);
4837 	      old_ent = (char *) old_ent + htab->root.table.entsize;
4838 	      h = (struct elf_link_hash_entry *) p;
4839 	      if (h->root.type == bfd_link_hash_warning)
4840 		{
4841 		  memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4842 		  old_ent = (char *) old_ent + htab->root.table.entsize;
4843 		  h = (struct elf_link_hash_entry *) h->root.u.i.link;
4844 		}
4845 	      if (h->root.type == bfd_link_hash_common)
4846 		{
4847 		  if (size > h->root.u.c.size)
4848 		    h->root.u.c.size = size;
4849 		  if (alignment_power > h->root.u.c.p->alignment_power)
4850 		    h->root.u.c.p->alignment_power = alignment_power;
4851 		}
4852 	    }
4853 	}
4854 
4855       /* Make a special call to the linker "notice" function to
4856 	 tell it that symbols added for crefs may need to be removed.  */
4857       if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4858 	goto error_free_vers;
4859 
4860       free (old_tab);
4861       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4862 			   alloc_mark);
4863       if (nondeflt_vers != NULL)
4864 	free (nondeflt_vers);
4865       return TRUE;
4866     }
4867 
4868   if (old_tab != NULL)
4869     {
4870       if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4871 	goto error_free_vers;
4872       free (old_tab);
4873       old_tab = NULL;
4874     }
4875 
4876   /* Now that all the symbols from this input file are created, if
4877      not performing a relocatable link, handle .symver foo, foo@BAR
4878      such that any relocs against foo become foo@BAR.  */
4879   if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4880     {
4881       size_t cnt, symidx;
4882 
4883       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4884 	{
4885 	  struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4886 	  char *shortname, *p;
4887 
4888 	  p = strchr (h->root.root.string, ELF_VER_CHR);
4889 	  if (p == NULL
4890 	      || (h->root.type != bfd_link_hash_defined
4891 		  && h->root.type != bfd_link_hash_defweak))
4892 	    continue;
4893 
4894 	  amt = p - h->root.root.string;
4895 	  shortname = (char *) bfd_malloc (amt + 1);
4896 	  if (!shortname)
4897 	    goto error_free_vers;
4898 	  memcpy (shortname, h->root.root.string, amt);
4899 	  shortname[amt] = '\0';
4900 
4901 	  hi = (struct elf_link_hash_entry *)
4902 	       bfd_link_hash_lookup (&htab->root, shortname,
4903 				     FALSE, FALSE, FALSE);
4904 	  if (hi != NULL
4905 	      && hi->root.type == h->root.type
4906 	      && hi->root.u.def.value == h->root.u.def.value
4907 	      && hi->root.u.def.section == h->root.u.def.section)
4908 	    {
4909 	      (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4910 	      hi->root.type = bfd_link_hash_indirect;
4911 	      hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4912 	      (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4913 	      sym_hash = elf_sym_hashes (abfd);
4914 	      if (sym_hash)
4915 		for (symidx = 0; symidx < extsymcount; ++symidx)
4916 		  if (sym_hash[symidx] == hi)
4917 		    {
4918 		      sym_hash[symidx] = h;
4919 		      break;
4920 		    }
4921 	    }
4922 	  free (shortname);
4923 	}
4924       free (nondeflt_vers);
4925       nondeflt_vers = NULL;
4926     }
4927 
4928   /* Now set the weakdefs field correctly for all the weak defined
4929      symbols we found.  The only way to do this is to search all the
4930      symbols.  Since we only need the information for non functions in
4931      dynamic objects, that's the only time we actually put anything on
4932      the list WEAKS.  We need this information so that if a regular
4933      object refers to a symbol defined weakly in a dynamic object, the
4934      real symbol in the dynamic object is also put in the dynamic
4935      symbols; we also must arrange for both symbols to point to the
4936      same memory location.  We could handle the general case of symbol
4937      aliasing, but a general symbol alias can only be generated in
4938      assembler code, handling it correctly would be very time
4939      consuming, and other ELF linkers don't handle general aliasing
4940      either.  */
4941   if (weaks != NULL)
4942     {
4943       struct elf_link_hash_entry **hpp;
4944       struct elf_link_hash_entry **hppend;
4945       struct elf_link_hash_entry **sorted_sym_hash;
4946       struct elf_link_hash_entry *h;
4947       size_t sym_count;
4948 
4949       /* Since we have to search the whole symbol list for each weak
4950 	 defined symbol, search time for N weak defined symbols will be
4951 	 O(N^2). Binary search will cut it down to O(NlogN).  */
4952       amt = extsymcount;
4953       amt *= sizeof (struct elf_link_hash_entry *);
4954       sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4955       if (sorted_sym_hash == NULL)
4956 	goto error_return;
4957       sym_hash = sorted_sym_hash;
4958       hpp = elf_sym_hashes (abfd);
4959       hppend = hpp + extsymcount;
4960       sym_count = 0;
4961       for (; hpp < hppend; hpp++)
4962 	{
4963 	  h = *hpp;
4964 	  if (h != NULL
4965 	      && h->root.type == bfd_link_hash_defined
4966 	      && !bed->is_function_type (h->type))
4967 	    {
4968 	      *sym_hash = h;
4969 	      sym_hash++;
4970 	      sym_count++;
4971 	    }
4972 	}
4973 
4974       qsort (sorted_sym_hash, sym_count,
4975 	     sizeof (struct elf_link_hash_entry *),
4976 	     elf_sort_symbol);
4977 
4978       while (weaks != NULL)
4979 	{
4980 	  struct elf_link_hash_entry *hlook;
4981 	  asection *slook;
4982 	  bfd_vma vlook;
4983 	  size_t i, j, idx = 0;
4984 
4985 	  hlook = weaks;
4986 	  weaks = hlook->u.weakdef;
4987 	  hlook->u.weakdef = NULL;
4988 
4989 	  BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4990 		      || hlook->root.type == bfd_link_hash_defweak
4991 		      || hlook->root.type == bfd_link_hash_common
4992 		      || hlook->root.type == bfd_link_hash_indirect);
4993 	  slook = hlook->root.u.def.section;
4994 	  vlook = hlook->root.u.def.value;
4995 
4996 	  i = 0;
4997 	  j = sym_count;
4998 	  while (i != j)
4999 	    {
5000 	      bfd_signed_vma vdiff;
5001 	      idx = (i + j) / 2;
5002 	      h = sorted_sym_hash[idx];
5003 	      vdiff = vlook - h->root.u.def.value;
5004 	      if (vdiff < 0)
5005 		j = idx;
5006 	      else if (vdiff > 0)
5007 		i = idx + 1;
5008 	      else
5009 		{
5010 		  int sdiff = slook->id - h->root.u.def.section->id;
5011 		  if (sdiff < 0)
5012 		    j = idx;
5013 		  else if (sdiff > 0)
5014 		    i = idx + 1;
5015 		  else
5016 		    break;
5017 		}
5018 	    }
5019 
5020 	  /* We didn't find a value/section match.  */
5021 	  if (i == j)
5022 	    continue;
5023 
5024 	  /* With multiple aliases, or when the weak symbol is already
5025 	     strongly defined, we have multiple matching symbols and
5026 	     the binary search above may land on any of them.  Step
5027 	     one past the matching symbol(s).  */
5028 	  while (++idx != j)
5029 	    {
5030 	      h = sorted_sym_hash[idx];
5031 	      if (h->root.u.def.section != slook
5032 		  || h->root.u.def.value != vlook)
5033 		break;
5034 	    }
5035 
5036 	  /* Now look back over the aliases.  Since we sorted by size
5037 	     as well as value and section, we'll choose the one with
5038 	     the largest size.  */
5039 	  while (idx-- != i)
5040 	    {
5041 	      h = sorted_sym_hash[idx];
5042 
5043 	      /* Stop if value or section doesn't match.  */
5044 	      if (h->root.u.def.section != slook
5045 		  || h->root.u.def.value != vlook)
5046 		break;
5047 	      else if (h != hlook)
5048 		{
5049 		  hlook->u.weakdef = h;
5050 
5051 		  /* If the weak definition is in the list of dynamic
5052 		     symbols, make sure the real definition is put
5053 		     there as well.  */
5054 		  if (hlook->dynindx != -1 && h->dynindx == -1)
5055 		    {
5056 		      if (! bfd_elf_link_record_dynamic_symbol (info, h))
5057 			{
5058 			err_free_sym_hash:
5059 			  free (sorted_sym_hash);
5060 			  goto error_return;
5061 			}
5062 		    }
5063 
5064 		  /* If the real definition is in the list of dynamic
5065 		     symbols, make sure the weak definition is put
5066 		     there as well.  If we don't do this, then the
5067 		     dynamic loader might not merge the entries for the
5068 		     real definition and the weak definition.  */
5069 		  if (h->dynindx != -1 && hlook->dynindx == -1)
5070 		    {
5071 		      if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5072 			goto err_free_sym_hash;
5073 		    }
5074 		  break;
5075 		}
5076 	    }
5077 	}
5078 
5079       free (sorted_sym_hash);
5080     }
5081 
5082   if (bed->check_directives
5083       && !(*bed->check_directives) (abfd, info))
5084     return FALSE;
5085 
5086   if (!info->check_relocs_after_open_input
5087       && !_bfd_elf_link_check_relocs (abfd, info))
5088     return FALSE;
5089 
5090   /* If this is a non-traditional link, try to optimize the handling
5091      of the .stab/.stabstr sections.  */
5092   if (! dynamic
5093       && ! info->traditional_format
5094       && is_elf_hash_table (htab)
5095       && (info->strip != strip_all && info->strip != strip_debugger))
5096     {
5097       asection *stabstr;
5098 
5099       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5100       if (stabstr != NULL)
5101 	{
5102 	  bfd_size_type string_offset = 0;
5103 	  asection *stab;
5104 
5105 	  for (stab = abfd->sections; stab; stab = stab->next)
5106 	    if (CONST_STRNEQ (stab->name, ".stab")
5107 		&& (!stab->name[5] ||
5108 		    (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5109 		&& (stab->flags & SEC_MERGE) == 0
5110 		&& !bfd_is_abs_section (stab->output_section))
5111 	      {
5112 		struct bfd_elf_section_data *secdata;
5113 
5114 		secdata = elf_section_data (stab);
5115 		if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5116 					       stabstr, &secdata->sec_info,
5117 					       &string_offset))
5118 		  goto error_return;
5119 		if (secdata->sec_info)
5120 		  stab->sec_info_type = SEC_INFO_TYPE_STABS;
5121 	    }
5122 	}
5123     }
5124 
5125   if (is_elf_hash_table (htab) && add_needed)
5126     {
5127       /* Add this bfd to the loaded list.  */
5128       struct elf_link_loaded_list *n;
5129 
5130       n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5131       if (n == NULL)
5132 	goto error_return;
5133       n->abfd = abfd;
5134       n->next = htab->loaded;
5135       htab->loaded = n;
5136     }
5137 
5138   return TRUE;
5139 
5140  error_free_vers:
5141   if (old_tab != NULL)
5142     free (old_tab);
5143   if (old_strtab != NULL)
5144     free (old_strtab);
5145   if (nondeflt_vers != NULL)
5146     free (nondeflt_vers);
5147   if (extversym != NULL)
5148     free (extversym);
5149  error_free_sym:
5150   if (isymbuf != NULL)
5151     free (isymbuf);
5152  error_return:
5153   return FALSE;
5154 }
5155 
5156 /* Return the linker hash table entry of a symbol that might be
5157    satisfied by an archive symbol.  Return -1 on error.  */
5158 
5159 struct elf_link_hash_entry *
5160 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5161 				struct bfd_link_info *info,
5162 				const char *name)
5163 {
5164   struct elf_link_hash_entry *h;
5165   char *p, *copy;
5166   size_t len, first;
5167 
5168   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5169   if (h != NULL)
5170     return h;
5171 
5172   /* If this is a default version (the name contains @@), look up the
5173      symbol again with only one `@' as well as without the version.
5174      The effect is that references to the symbol with and without the
5175      version will be matched by the default symbol in the archive.  */
5176 
5177   p = strchr (name, ELF_VER_CHR);
5178   if (p == NULL || p[1] != ELF_VER_CHR)
5179     return h;
5180 
5181   /* First check with only one `@'.  */
5182   len = strlen (name);
5183   copy = (char *) bfd_alloc (abfd, len);
5184   if (copy == NULL)
5185     return (struct elf_link_hash_entry *) 0 - 1;
5186 
5187   first = p - name + 1;
5188   memcpy (copy, name, first);
5189   memcpy (copy + first, name + first + 1, len - first);
5190 
5191   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5192   if (h == NULL)
5193     {
5194       /* We also need to check references to the symbol without the
5195 	 version.  */
5196       copy[first - 1] = '\0';
5197       h = elf_link_hash_lookup (elf_hash_table (info), copy,
5198 				FALSE, FALSE, TRUE);
5199     }
5200 
5201   bfd_release (abfd, copy);
5202   return h;
5203 }
5204 
5205 /* Add symbols from an ELF archive file to the linker hash table.  We
5206    don't use _bfd_generic_link_add_archive_symbols because we need to
5207    handle versioned symbols.
5208 
5209    Fortunately, ELF archive handling is simpler than that done by
5210    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5211    oddities.  In ELF, if we find a symbol in the archive map, and the
5212    symbol is currently undefined, we know that we must pull in that
5213    object file.
5214 
5215    Unfortunately, we do have to make multiple passes over the symbol
5216    table until nothing further is resolved.  */
5217 
5218 static bfd_boolean
5219 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5220 {
5221   symindex c;
5222   unsigned char *included = NULL;
5223   carsym *symdefs;
5224   bfd_boolean loop;
5225   bfd_size_type amt;
5226   const struct elf_backend_data *bed;
5227   struct elf_link_hash_entry * (*archive_symbol_lookup)
5228     (bfd *, struct bfd_link_info *, const char *);
5229 
5230   if (! bfd_has_map (abfd))
5231     {
5232       /* An empty archive is a special case.  */
5233       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5234 	return TRUE;
5235       bfd_set_error (bfd_error_no_armap);
5236       return FALSE;
5237     }
5238 
5239   /* Keep track of all symbols we know to be already defined, and all
5240      files we know to be already included.  This is to speed up the
5241      second and subsequent passes.  */
5242   c = bfd_ardata (abfd)->symdef_count;
5243   if (c == 0)
5244     return TRUE;
5245   amt = c;
5246   amt *= sizeof (*included);
5247   included = (unsigned char *) bfd_zmalloc (amt);
5248   if (included == NULL)
5249     return FALSE;
5250 
5251   symdefs = bfd_ardata (abfd)->symdefs;
5252   bed = get_elf_backend_data (abfd);
5253   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5254 
5255   do
5256     {
5257       file_ptr last;
5258       symindex i;
5259       carsym *symdef;
5260       carsym *symdefend;
5261 
5262       loop = FALSE;
5263       last = -1;
5264 
5265       symdef = symdefs;
5266       symdefend = symdef + c;
5267       for (i = 0; symdef < symdefend; symdef++, i++)
5268 	{
5269 	  struct elf_link_hash_entry *h;
5270 	  bfd *element;
5271 	  struct bfd_link_hash_entry *undefs_tail;
5272 	  symindex mark;
5273 
5274 	  if (included[i])
5275 	    continue;
5276 	  if (symdef->file_offset == last)
5277 	    {
5278 	      included[i] = TRUE;
5279 	      continue;
5280 	    }
5281 
5282 	  h = archive_symbol_lookup (abfd, info, symdef->name);
5283 	  if (h == (struct elf_link_hash_entry *) 0 - 1)
5284 	    goto error_return;
5285 
5286 	  if (h == NULL)
5287 	    continue;
5288 
5289 	  if (h->root.type == bfd_link_hash_common)
5290 	    {
5291 	      /* We currently have a common symbol.  The archive map contains
5292 		 a reference to this symbol, so we may want to include it.  We
5293 		 only want to include it however, if this archive element
5294 		 contains a definition of the symbol, not just another common
5295 		 declaration of it.
5296 
5297 		 Unfortunately some archivers (including GNU ar) will put
5298 		 declarations of common symbols into their archive maps, as
5299 		 well as real definitions, so we cannot just go by the archive
5300 		 map alone.  Instead we must read in the element's symbol
5301 		 table and check that to see what kind of symbol definition
5302 		 this is.  */
5303 	      if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5304 		continue;
5305 	    }
5306 	  else if (h->root.type != bfd_link_hash_undefined)
5307 	    {
5308 	      if (h->root.type != bfd_link_hash_undefweak)
5309 		/* Symbol must be defined.  Don't check it again.  */
5310 		included[i] = TRUE;
5311 	      continue;
5312 	    }
5313 
5314 	  /* We need to include this archive member.  */
5315 	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5316 	  if (element == NULL)
5317 	    goto error_return;
5318 
5319 	  if (! bfd_check_format (element, bfd_object))
5320 	    goto error_return;
5321 
5322 	  undefs_tail = info->hash->undefs_tail;
5323 
5324 	  if (!(*info->callbacks
5325 		->add_archive_element) (info, element, symdef->name, &element))
5326 	    continue;
5327 	  if (!bfd_link_add_symbols (element, info))
5328 	    goto error_return;
5329 
5330 	  /* If there are any new undefined symbols, we need to make
5331 	     another pass through the archive in order to see whether
5332 	     they can be defined.  FIXME: This isn't perfect, because
5333 	     common symbols wind up on undefs_tail and because an
5334 	     undefined symbol which is defined later on in this pass
5335 	     does not require another pass.  This isn't a bug, but it
5336 	     does make the code less efficient than it could be.  */
5337 	  if (undefs_tail != info->hash->undefs_tail)
5338 	    loop = TRUE;
5339 
5340 	  /* Look backward to mark all symbols from this object file
5341 	     which we have already seen in this pass.  */
5342 	  mark = i;
5343 	  do
5344 	    {
5345 	      included[mark] = TRUE;
5346 	      if (mark == 0)
5347 		break;
5348 	      --mark;
5349 	    }
5350 	  while (symdefs[mark].file_offset == symdef->file_offset);
5351 
5352 	  /* We mark subsequent symbols from this object file as we go
5353 	     on through the loop.  */
5354 	  last = symdef->file_offset;
5355 	}
5356     }
5357   while (loop);
5358 
5359   free (included);
5360 
5361   return TRUE;
5362 
5363  error_return:
5364   if (included != NULL)
5365     free (included);
5366   return FALSE;
5367 }
5368 
5369 /* Given an ELF BFD, add symbols to the global hash table as
5370    appropriate.  */
5371 
5372 bfd_boolean
5373 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5374 {
5375   switch (bfd_get_format (abfd))
5376     {
5377     case bfd_object:
5378       return elf_link_add_object_symbols (abfd, info);
5379     case bfd_archive:
5380       return elf_link_add_archive_symbols (abfd, info);
5381     default:
5382       bfd_set_error (bfd_error_wrong_format);
5383       return FALSE;
5384     }
5385 }
5386 
5387 struct hash_codes_info
5388 {
5389   unsigned long *hashcodes;
5390   bfd_boolean error;
5391 };
5392 
5393 /* This function will be called though elf_link_hash_traverse to store
5394    all hash value of the exported symbols in an array.  */
5395 
5396 static bfd_boolean
5397 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5398 {
5399   struct hash_codes_info *inf = (struct hash_codes_info *) data;
5400   const char *name;
5401   unsigned long ha;
5402   char *alc = NULL;
5403 
5404   /* Ignore indirect symbols.  These are added by the versioning code.  */
5405   if (h->dynindx == -1)
5406     return TRUE;
5407 
5408   name = h->root.root.string;
5409   if (h->versioned >= versioned)
5410     {
5411       char *p = strchr (name, ELF_VER_CHR);
5412       if (p != NULL)
5413 	{
5414 	  alc = (char *) bfd_malloc (p - name + 1);
5415 	  if (alc == NULL)
5416 	    {
5417 	      inf->error = TRUE;
5418 	      return FALSE;
5419 	    }
5420 	  memcpy (alc, name, p - name);
5421 	  alc[p - name] = '\0';
5422 	  name = alc;
5423 	}
5424     }
5425 
5426   /* Compute the hash value.  */
5427   ha = bfd_elf_hash (name);
5428 
5429   /* Store the found hash value in the array given as the argument.  */
5430   *(inf->hashcodes)++ = ha;
5431 
5432   /* And store it in the struct so that we can put it in the hash table
5433      later.  */
5434   h->u.elf_hash_value = ha;
5435 
5436   if (alc != NULL)
5437     free (alc);
5438 
5439   return TRUE;
5440 }
5441 
5442 struct collect_gnu_hash_codes
5443 {
5444   bfd *output_bfd;
5445   const struct elf_backend_data *bed;
5446   unsigned long int nsyms;
5447   unsigned long int maskbits;
5448   unsigned long int *hashcodes;
5449   unsigned long int *hashval;
5450   unsigned long int *indx;
5451   unsigned long int *counts;
5452   bfd_vma *bitmask;
5453   bfd_byte *contents;
5454   long int min_dynindx;
5455   unsigned long int bucketcount;
5456   unsigned long int symindx;
5457   long int local_indx;
5458   long int shift1, shift2;
5459   unsigned long int mask;
5460   bfd_boolean error;
5461 };
5462 
5463 /* This function will be called though elf_link_hash_traverse to store
5464    all hash value of the exported symbols in an array.  */
5465 
5466 static bfd_boolean
5467 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5468 {
5469   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5470   const char *name;
5471   unsigned long ha;
5472   char *alc = NULL;
5473 
5474   /* Ignore indirect symbols.  These are added by the versioning code.  */
5475   if (h->dynindx == -1)
5476     return TRUE;
5477 
5478   /* Ignore also local symbols and undefined symbols.  */
5479   if (! (*s->bed->elf_hash_symbol) (h))
5480     return TRUE;
5481 
5482   name = h->root.root.string;
5483   if (h->versioned >= versioned)
5484     {
5485       char *p = strchr (name, ELF_VER_CHR);
5486       if (p != NULL)
5487 	{
5488 	  alc = (char *) bfd_malloc (p - name + 1);
5489 	  if (alc == NULL)
5490 	    {
5491 	      s->error = TRUE;
5492 	      return FALSE;
5493 	    }
5494 	  memcpy (alc, name, p - name);
5495 	  alc[p - name] = '\0';
5496 	  name = alc;
5497 	}
5498     }
5499 
5500   /* Compute the hash value.  */
5501   ha = bfd_elf_gnu_hash (name);
5502 
5503   /* Store the found hash value in the array for compute_bucket_count,
5504      and also for .dynsym reordering purposes.  */
5505   s->hashcodes[s->nsyms] = ha;
5506   s->hashval[h->dynindx] = ha;
5507   ++s->nsyms;
5508   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5509     s->min_dynindx = h->dynindx;
5510 
5511   if (alc != NULL)
5512     free (alc);
5513 
5514   return TRUE;
5515 }
5516 
5517 /* This function will be called though elf_link_hash_traverse to do
5518    final dynaminc symbol renumbering.  */
5519 
5520 static bfd_boolean
5521 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5522 {
5523   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5524   unsigned long int bucket;
5525   unsigned long int val;
5526 
5527   /* Ignore indirect symbols.  */
5528   if (h->dynindx == -1)
5529     return TRUE;
5530 
5531   /* Ignore also local symbols and undefined symbols.  */
5532   if (! (*s->bed->elf_hash_symbol) (h))
5533     {
5534       if (h->dynindx >= s->min_dynindx)
5535 	h->dynindx = s->local_indx++;
5536       return TRUE;
5537     }
5538 
5539   bucket = s->hashval[h->dynindx] % s->bucketcount;
5540   val = (s->hashval[h->dynindx] >> s->shift1)
5541 	& ((s->maskbits >> s->shift1) - 1);
5542   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5543   s->bitmask[val]
5544     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5545   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5546   if (s->counts[bucket] == 1)
5547     /* Last element terminates the chain.  */
5548     val |= 1;
5549   bfd_put_32 (s->output_bfd, val,
5550 	      s->contents + (s->indx[bucket] - s->symindx) * 4);
5551   --s->counts[bucket];
5552   h->dynindx = s->indx[bucket]++;
5553   return TRUE;
5554 }
5555 
5556 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
5557 
5558 bfd_boolean
5559 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5560 {
5561   return !(h->forced_local
5562 	   || h->root.type == bfd_link_hash_undefined
5563 	   || h->root.type == bfd_link_hash_undefweak
5564 	   || ((h->root.type == bfd_link_hash_defined
5565 		|| h->root.type == bfd_link_hash_defweak)
5566 	       && h->root.u.def.section->output_section == NULL));
5567 }
5568 
5569 /* Array used to determine the number of hash table buckets to use
5570    based on the number of symbols there are.  If there are fewer than
5571    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5572    fewer than 37 we use 17 buckets, and so forth.  We never use more
5573    than 32771 buckets.  */
5574 
5575 static const size_t elf_buckets[] =
5576 {
5577   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5578   16411, 32771, 0
5579 };
5580 
5581 /* Compute bucket count for hashing table.  We do not use a static set
5582    of possible tables sizes anymore.  Instead we determine for all
5583    possible reasonable sizes of the table the outcome (i.e., the
5584    number of collisions etc) and choose the best solution.  The
5585    weighting functions are not too simple to allow the table to grow
5586    without bounds.  Instead one of the weighting factors is the size.
5587    Therefore the result is always a good payoff between few collisions
5588    (= short chain lengths) and table size.  */
5589 static size_t
5590 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5591 		      unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5592 		      unsigned long int nsyms,
5593 		      int gnu_hash)
5594 {
5595   size_t best_size = 0;
5596   unsigned long int i;
5597 
5598   /* We have a problem here.  The following code to optimize the table
5599      size requires an integer type with more the 32 bits.  If
5600      BFD_HOST_U_64_BIT is set we know about such a type.  */
5601 #ifdef BFD_HOST_U_64_BIT
5602   if (info->optimize)
5603     {
5604       size_t minsize;
5605       size_t maxsize;
5606       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5607       bfd *dynobj = elf_hash_table (info)->dynobj;
5608       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5609       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5610       unsigned long int *counts;
5611       bfd_size_type amt;
5612       unsigned int no_improvement_count = 0;
5613 
5614       /* Possible optimization parameters: if we have NSYMS symbols we say
5615 	 that the hashing table must at least have NSYMS/4 and at most
5616 	 2*NSYMS buckets.  */
5617       minsize = nsyms / 4;
5618       if (minsize == 0)
5619 	minsize = 1;
5620       best_size = maxsize = nsyms * 2;
5621       if (gnu_hash)
5622 	{
5623 	  if (minsize < 2)
5624 	    minsize = 2;
5625 	  if ((best_size & 31) == 0)
5626 	    ++best_size;
5627 	}
5628 
5629       /* Create array where we count the collisions in.  We must use bfd_malloc
5630 	 since the size could be large.  */
5631       amt = maxsize;
5632       amt *= sizeof (unsigned long int);
5633       counts = (unsigned long int *) bfd_malloc (amt);
5634       if (counts == NULL)
5635 	return 0;
5636 
5637       /* Compute the "optimal" size for the hash table.  The criteria is a
5638 	 minimal chain length.  The minor criteria is (of course) the size
5639 	 of the table.  */
5640       for (i = minsize; i < maxsize; ++i)
5641 	{
5642 	  /* Walk through the array of hashcodes and count the collisions.  */
5643 	  BFD_HOST_U_64_BIT max;
5644 	  unsigned long int j;
5645 	  unsigned long int fact;
5646 
5647 	  if (gnu_hash && (i & 31) == 0)
5648 	    continue;
5649 
5650 	  memset (counts, '\0', i * sizeof (unsigned long int));
5651 
5652 	  /* Determine how often each hash bucket is used.  */
5653 	  for (j = 0; j < nsyms; ++j)
5654 	    ++counts[hashcodes[j] % i];
5655 
5656 	  /* For the weight function we need some information about the
5657 	     pagesize on the target.  This is information need not be 100%
5658 	     accurate.  Since this information is not available (so far) we
5659 	     define it here to a reasonable default value.  If it is crucial
5660 	     to have a better value some day simply define this value.  */
5661 # ifndef BFD_TARGET_PAGESIZE
5662 #  define BFD_TARGET_PAGESIZE	(4096)
5663 # endif
5664 
5665 	  /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5666 	     and the chains.  */
5667 	  max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5668 
5669 # if 1
5670 	  /* Variant 1: optimize for short chains.  We add the squares
5671 	     of all the chain lengths (which favors many small chain
5672 	     over a few long chains).  */
5673 	  for (j = 0; j < i; ++j)
5674 	    max += counts[j] * counts[j];
5675 
5676 	  /* This adds penalties for the overall size of the table.  */
5677 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5678 	  max *= fact * fact;
5679 # else
5680 	  /* Variant 2: Optimize a lot more for small table.  Here we
5681 	     also add squares of the size but we also add penalties for
5682 	     empty slots (the +1 term).  */
5683 	  for (j = 0; j < i; ++j)
5684 	    max += (1 + counts[j]) * (1 + counts[j]);
5685 
5686 	  /* The overall size of the table is considered, but not as
5687 	     strong as in variant 1, where it is squared.  */
5688 	  fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5689 	  max *= fact;
5690 # endif
5691 
5692 	  /* Compare with current best results.  */
5693 	  if (max < best_chlen)
5694 	    {
5695 	      best_chlen = max;
5696 	      best_size = i;
5697 	      no_improvement_count = 0;
5698 	    }
5699 	  /* PR 11843: Avoid futile long searches for the best bucket size
5700 	     when there are a large number of symbols.  */
5701 	  else if (++no_improvement_count == 100)
5702 	    break;
5703 	}
5704 
5705       free (counts);
5706     }
5707   else
5708 #endif /* defined (BFD_HOST_U_64_BIT) */
5709     {
5710       /* This is the fallback solution if no 64bit type is available or if we
5711 	 are not supposed to spend much time on optimizations.  We select the
5712 	 bucket count using a fixed set of numbers.  */
5713       for (i = 0; elf_buckets[i] != 0; i++)
5714 	{
5715 	  best_size = elf_buckets[i];
5716 	  if (nsyms < elf_buckets[i + 1])
5717 	    break;
5718 	}
5719       if (gnu_hash && best_size < 2)
5720 	best_size = 2;
5721     }
5722 
5723   return best_size;
5724 }
5725 
5726 /* Size any SHT_GROUP section for ld -r.  */
5727 
5728 bfd_boolean
5729 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5730 {
5731   bfd *ibfd;
5732 
5733   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5734     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5735 	&& !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5736       return FALSE;
5737   return TRUE;
5738 }
5739 
5740 /* Set a default stack segment size.  The value in INFO wins.  If it
5741    is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5742    undefined it is initialized.  */
5743 
5744 bfd_boolean
5745 bfd_elf_stack_segment_size (bfd *output_bfd,
5746 			    struct bfd_link_info *info,
5747 			    const char *legacy_symbol,
5748 			    bfd_vma default_size)
5749 {
5750   struct elf_link_hash_entry *h = NULL;
5751 
5752   /* Look for legacy symbol.  */
5753   if (legacy_symbol)
5754     h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5755 			      FALSE, FALSE, FALSE);
5756   if (h && (h->root.type == bfd_link_hash_defined
5757 	    || h->root.type == bfd_link_hash_defweak)
5758       && h->def_regular
5759       && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5760     {
5761       /* The symbol has no type if specified on the command line.  */
5762       h->type = STT_OBJECT;
5763       if (info->stacksize)
5764 	(*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5765 			       output_bfd, legacy_symbol);
5766       else if (h->root.u.def.section != bfd_abs_section_ptr)
5767 	(*_bfd_error_handler) (_("%B: %s not absolute"),
5768 			       output_bfd, legacy_symbol);
5769       else
5770 	info->stacksize = h->root.u.def.value;
5771     }
5772 
5773   if (!info->stacksize)
5774     /* If the user didn't set a size, or explicitly inhibit the
5775        size, set it now.  */
5776     info->stacksize = default_size;
5777 
5778   /* Provide the legacy symbol, if it is referenced.  */
5779   if (h && (h->root.type == bfd_link_hash_undefined
5780 	    || h->root.type == bfd_link_hash_undefweak))
5781     {
5782       struct bfd_link_hash_entry *bh = NULL;
5783 
5784       if (!(_bfd_generic_link_add_one_symbol
5785 	    (info, output_bfd, legacy_symbol,
5786 	     BSF_GLOBAL, bfd_abs_section_ptr,
5787 	     info->stacksize >= 0 ? info->stacksize : 0,
5788 	     NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5789 	return FALSE;
5790 
5791       h = (struct elf_link_hash_entry *) bh;
5792       h->def_regular = 1;
5793       h->type = STT_OBJECT;
5794     }
5795 
5796   return TRUE;
5797 }
5798 
5799 /* Set up the sizes and contents of the ELF dynamic sections.  This is
5800    called by the ELF linker emulation before_allocation routine.  We
5801    must set the sizes of the sections before the linker sets the
5802    addresses of the various sections.  */
5803 
5804 bfd_boolean
5805 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5806 			       const char *soname,
5807 			       const char *rpath,
5808 			       const char *filter_shlib,
5809 			       const char *audit,
5810 			       const char *depaudit,
5811 			       const char * const *auxiliary_filters,
5812 			       struct bfd_link_info *info,
5813 			       asection **sinterpptr)
5814 {
5815   size_t soname_indx;
5816   bfd *dynobj;
5817   const struct elf_backend_data *bed;
5818   struct elf_info_failed asvinfo;
5819 
5820   *sinterpptr = NULL;
5821 
5822   soname_indx = (size_t) -1;
5823 
5824   if (!is_elf_hash_table (info->hash))
5825     return TRUE;
5826 
5827   bed = get_elf_backend_data (output_bfd);
5828 
5829   /* Any syms created from now on start with -1 in
5830      got.refcount/offset and plt.refcount/offset.  */
5831   elf_hash_table (info)->init_got_refcount
5832     = elf_hash_table (info)->init_got_offset;
5833   elf_hash_table (info)->init_plt_refcount
5834     = elf_hash_table (info)->init_plt_offset;
5835 
5836   if (bfd_link_relocatable (info)
5837       && !_bfd_elf_size_group_sections (info))
5838     return FALSE;
5839 
5840   /* The backend may have to create some sections regardless of whether
5841      we're dynamic or not.  */
5842   if (bed->elf_backend_always_size_sections
5843       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5844     return FALSE;
5845 
5846   /* Determine any GNU_STACK segment requirements, after the backend
5847      has had a chance to set a default segment size.  */
5848   if (info->execstack)
5849     elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5850   else if (info->noexecstack)
5851     elf_stack_flags (output_bfd) = PF_R | PF_W;
5852   else
5853     {
5854       bfd *inputobj;
5855       asection *notesec = NULL;
5856       int exec = 0;
5857 
5858       for (inputobj = info->input_bfds;
5859 	   inputobj;
5860 	   inputobj = inputobj->link.next)
5861 	{
5862 	  asection *s;
5863 
5864 	  if (inputobj->flags
5865 	      & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5866 	    continue;
5867 	  s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5868 	  if (s)
5869 	    {
5870 	      if (s->flags & SEC_CODE)
5871 		exec = PF_X;
5872 	      notesec = s;
5873 	    }
5874 	  else if (bed->default_execstack)
5875 	    exec = PF_X;
5876 	}
5877       if (notesec || info->stacksize > 0)
5878 	elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5879       if (notesec && exec && bfd_link_relocatable (info)
5880 	  && notesec->output_section != bfd_abs_section_ptr)
5881 	notesec->output_section->flags |= SEC_CODE;
5882     }
5883 
5884   dynobj = elf_hash_table (info)->dynobj;
5885 
5886   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5887     {
5888       struct elf_info_failed eif;
5889       struct elf_link_hash_entry *h;
5890       asection *dynstr;
5891       struct bfd_elf_version_tree *t;
5892       struct bfd_elf_version_expr *d;
5893       asection *s;
5894       bfd_boolean all_defined;
5895 
5896       *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5897       BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5898 
5899       if (soname != NULL)
5900 	{
5901 	  soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5902 					     soname, TRUE);
5903 	  if (soname_indx == (size_t) -1
5904 	      || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5905 	    return FALSE;
5906 	}
5907 
5908       if (info->symbolic)
5909 	{
5910 	  if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5911 	    return FALSE;
5912 	  info->flags |= DF_SYMBOLIC;
5913 	}
5914 
5915       if (rpath != NULL)
5916 	{
5917 	  size_t indx;
5918 	  bfd_vma tag;
5919 
5920 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5921 				      TRUE);
5922 	  if (indx == (size_t) -1)
5923 	    return FALSE;
5924 
5925 	  tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5926 	  if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5927 	    return FALSE;
5928 	}
5929 
5930       if (filter_shlib != NULL)
5931 	{
5932 	  size_t indx;
5933 
5934 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5935 				      filter_shlib, TRUE);
5936 	  if (indx == (size_t) -1
5937 	      || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5938 	    return FALSE;
5939 	}
5940 
5941       if (auxiliary_filters != NULL)
5942 	{
5943 	  const char * const *p;
5944 
5945 	  for (p = auxiliary_filters; *p != NULL; p++)
5946 	    {
5947 	      size_t indx;
5948 
5949 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5950 					  *p, TRUE);
5951 	      if (indx == (size_t) -1
5952 		  || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5953 		return FALSE;
5954 	    }
5955 	}
5956 
5957       if (audit != NULL)
5958 	{
5959 	  size_t indx;
5960 
5961 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5962 				      TRUE);
5963 	  if (indx == (size_t) -1
5964 	      || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5965 	    return FALSE;
5966 	}
5967 
5968       if (depaudit != NULL)
5969 	{
5970 	  size_t indx;
5971 
5972 	  indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5973 				      TRUE);
5974 	  if (indx == (size_t) -1
5975 	      || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5976 	    return FALSE;
5977 	}
5978 
5979       eif.info = info;
5980       eif.failed = FALSE;
5981 
5982       /* If we are supposed to export all symbols into the dynamic symbol
5983 	 table (this is not the normal case), then do so.  */
5984       if (info->export_dynamic
5985 	  || (bfd_link_executable (info) && info->dynamic))
5986 	{
5987 	  elf_link_hash_traverse (elf_hash_table (info),
5988 				  _bfd_elf_export_symbol,
5989 				  &eif);
5990 	  if (eif.failed)
5991 	    return FALSE;
5992 	}
5993 
5994       /* Make all global versions with definition.  */
5995       for (t = info->version_info; t != NULL; t = t->next)
5996 	for (d = t->globals.list; d != NULL; d = d->next)
5997 	  if (!d->symver && d->literal)
5998 	    {
5999 	      const char *verstr, *name;
6000 	      size_t namelen, verlen, newlen;
6001 	      char *newname, *p, leading_char;
6002 	      struct elf_link_hash_entry *newh;
6003 
6004 	      leading_char = bfd_get_symbol_leading_char (output_bfd);
6005 	      name = d->pattern;
6006 	      namelen = strlen (name) + (leading_char != '\0');
6007 	      verstr = t->name;
6008 	      verlen = strlen (verstr);
6009 	      newlen = namelen + verlen + 3;
6010 
6011 	      newname = (char *) bfd_malloc (newlen);
6012 	      if (newname == NULL)
6013 		return FALSE;
6014 	      newname[0] = leading_char;
6015 	      memcpy (newname + (leading_char != '\0'), name, namelen);
6016 
6017 	      /* Check the hidden versioned definition.  */
6018 	      p = newname + namelen;
6019 	      *p++ = ELF_VER_CHR;
6020 	      memcpy (p, verstr, verlen + 1);
6021 	      newh = elf_link_hash_lookup (elf_hash_table (info),
6022 					   newname, FALSE, FALSE,
6023 					   FALSE);
6024 	      if (newh == NULL
6025 		  || (newh->root.type != bfd_link_hash_defined
6026 		      && newh->root.type != bfd_link_hash_defweak))
6027 		{
6028 		  /* Check the default versioned definition.  */
6029 		  *p++ = ELF_VER_CHR;
6030 		  memcpy (p, verstr, verlen + 1);
6031 		  newh = elf_link_hash_lookup (elf_hash_table (info),
6032 					       newname, FALSE, FALSE,
6033 					       FALSE);
6034 		}
6035 	      free (newname);
6036 
6037 	      /* Mark this version if there is a definition and it is
6038 		 not defined in a shared object.  */
6039 	      if (newh != NULL
6040 		  && !newh->def_dynamic
6041 		  && (newh->root.type == bfd_link_hash_defined
6042 		      || newh->root.type == bfd_link_hash_defweak))
6043 		d->symver = 1;
6044 	    }
6045 
6046       /* Attach all the symbols to their version information.  */
6047       asvinfo.info = info;
6048       asvinfo.failed = FALSE;
6049 
6050       elf_link_hash_traverse (elf_hash_table (info),
6051 			      _bfd_elf_link_assign_sym_version,
6052 			      &asvinfo);
6053       if (asvinfo.failed)
6054 	return FALSE;
6055 
6056       if (!info->allow_undefined_version)
6057 	{
6058 	  /* Check if all global versions have a definition.  */
6059 	  all_defined = TRUE;
6060 	  for (t = info->version_info; t != NULL; t = t->next)
6061 	    for (d = t->globals.list; d != NULL; d = d->next)
6062 	      if (d->literal && !d->symver && !d->script)
6063 		{
6064 		  (*_bfd_error_handler)
6065 		    (_("%s: undefined version: %s"),
6066 		     d->pattern, t->name);
6067 		  all_defined = FALSE;
6068 		}
6069 
6070 	  if (!all_defined)
6071 	    {
6072 	      bfd_set_error (bfd_error_bad_value);
6073 	      return FALSE;
6074 	    }
6075 	}
6076 
6077       /* Find all symbols which were defined in a dynamic object and make
6078 	 the backend pick a reasonable value for them.  */
6079       elf_link_hash_traverse (elf_hash_table (info),
6080 			      _bfd_elf_adjust_dynamic_symbol,
6081 			      &eif);
6082       if (eif.failed)
6083 	return FALSE;
6084 
6085       /* Add some entries to the .dynamic section.  We fill in some of the
6086 	 values later, in bfd_elf_final_link, but we must add the entries
6087 	 now so that we know the final size of the .dynamic section.  */
6088 
6089       /* If there are initialization and/or finalization functions to
6090 	 call then add the corresponding DT_INIT/DT_FINI entries.  */
6091       h = (info->init_function
6092 	   ? elf_link_hash_lookup (elf_hash_table (info),
6093 				   info->init_function, FALSE,
6094 				   FALSE, FALSE)
6095 	   : NULL);
6096       if (h != NULL
6097 	  && (h->ref_regular
6098 	      || h->def_regular))
6099 	{
6100 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6101 	    return FALSE;
6102 	}
6103       h = (info->fini_function
6104 	   ? elf_link_hash_lookup (elf_hash_table (info),
6105 				   info->fini_function, FALSE,
6106 				   FALSE, FALSE)
6107 	   : NULL);
6108       if (h != NULL
6109 	  && (h->ref_regular
6110 	      || h->def_regular))
6111 	{
6112 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6113 	    return FALSE;
6114 	}
6115 
6116       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6117       if (s != NULL && s->linker_has_input)
6118 	{
6119 	  /* DT_PREINIT_ARRAY is not allowed in shared library.  */
6120 	  if (! bfd_link_executable (info))
6121 	    {
6122 	      bfd *sub;
6123 	      asection *o;
6124 
6125 	      for (sub = info->input_bfds; sub != NULL;
6126 		   sub = sub->link.next)
6127 		if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6128 		  for (o = sub->sections; o != NULL; o = o->next)
6129 		    if (elf_section_data (o)->this_hdr.sh_type
6130 			== SHT_PREINIT_ARRAY)
6131 		      {
6132 			(*_bfd_error_handler)
6133 			  (_("%B: .preinit_array section is not allowed in DSO"),
6134 			   sub);
6135 			break;
6136 		      }
6137 
6138 	      bfd_set_error (bfd_error_nonrepresentable_section);
6139 	      return FALSE;
6140 	    }
6141 
6142 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6143 	      || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6144 	    return FALSE;
6145 	}
6146       s = bfd_get_section_by_name (output_bfd, ".init_array");
6147       if (s != NULL && s->linker_has_input)
6148 	{
6149 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6150 	      || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6151 	    return FALSE;
6152 	}
6153       s = bfd_get_section_by_name (output_bfd, ".fini_array");
6154       if (s != NULL && s->linker_has_input)
6155 	{
6156 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6157 	      || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6158 	    return FALSE;
6159 	}
6160 
6161       dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6162       /* If .dynstr is excluded from the link, we don't want any of
6163 	 these tags.  Strictly, we should be checking each section
6164 	 individually;  This quick check covers for the case where
6165 	 someone does a /DISCARD/ : { *(*) }.  */
6166       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6167 	{
6168 	  bfd_size_type strsize;
6169 
6170 	  strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6171 	  if ((info->emit_hash
6172 	       && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6173 	      || (info->emit_gnu_hash
6174 		  && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6175 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6176 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6177 	      || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6178 	      || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6179 					      bed->s->sizeof_sym))
6180 	    return FALSE;
6181 	}
6182     }
6183 
6184   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6185     return FALSE;
6186 
6187   /* The backend must work out the sizes of all the other dynamic
6188      sections.  */
6189   if (dynobj != NULL
6190       && bed->elf_backend_size_dynamic_sections != NULL
6191       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6192     return FALSE;
6193 
6194   if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6195     {
6196       unsigned long section_sym_count;
6197       struct bfd_elf_version_tree *verdefs;
6198       asection *s;
6199 
6200       /* Set up the version definition section.  */
6201       s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6202       BFD_ASSERT (s != NULL);
6203 
6204       /* We may have created additional version definitions if we are
6205 	 just linking a regular application.  */
6206       verdefs = info->version_info;
6207 
6208       /* Skip anonymous version tag.  */
6209       if (verdefs != NULL && verdefs->vernum == 0)
6210 	verdefs = verdefs->next;
6211 
6212       if (verdefs == NULL && !info->create_default_symver)
6213 	s->flags |= SEC_EXCLUDE;
6214       else
6215 	{
6216 	  unsigned int cdefs;
6217 	  bfd_size_type size;
6218 	  struct bfd_elf_version_tree *t;
6219 	  bfd_byte *p;
6220 	  Elf_Internal_Verdef def;
6221 	  Elf_Internal_Verdaux defaux;
6222 	  struct bfd_link_hash_entry *bh;
6223 	  struct elf_link_hash_entry *h;
6224 	  const char *name;
6225 
6226 	  cdefs = 0;
6227 	  size = 0;
6228 
6229 	  /* Make space for the base version.  */
6230 	  size += sizeof (Elf_External_Verdef);
6231 	  size += sizeof (Elf_External_Verdaux);
6232 	  ++cdefs;
6233 
6234 	  /* Make space for the default version.  */
6235 	  if (info->create_default_symver)
6236 	    {
6237 	      size += sizeof (Elf_External_Verdef);
6238 	      ++cdefs;
6239 	    }
6240 
6241 	  for (t = verdefs; t != NULL; t = t->next)
6242 	    {
6243 	      struct bfd_elf_version_deps *n;
6244 
6245 	      /* Don't emit base version twice.  */
6246 	      if (t->vernum == 0)
6247 		continue;
6248 
6249 	      size += sizeof (Elf_External_Verdef);
6250 	      size += sizeof (Elf_External_Verdaux);
6251 	      ++cdefs;
6252 
6253 	      for (n = t->deps; n != NULL; n = n->next)
6254 		size += sizeof (Elf_External_Verdaux);
6255 	    }
6256 
6257 	  s->size = size;
6258 	  s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6259 	  if (s->contents == NULL && s->size != 0)
6260 	    return FALSE;
6261 
6262 	  /* Fill in the version definition section.  */
6263 
6264 	  p = s->contents;
6265 
6266 	  def.vd_version = VER_DEF_CURRENT;
6267 	  def.vd_flags = VER_FLG_BASE;
6268 	  def.vd_ndx = 1;
6269 	  def.vd_cnt = 1;
6270 	  if (info->create_default_symver)
6271 	    {
6272 	      def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6273 	      def.vd_next = sizeof (Elf_External_Verdef);
6274 	    }
6275 	  else
6276 	    {
6277 	      def.vd_aux = sizeof (Elf_External_Verdef);
6278 	      def.vd_next = (sizeof (Elf_External_Verdef)
6279 			     + sizeof (Elf_External_Verdaux));
6280 	    }
6281 
6282 	  if (soname_indx != (size_t) -1)
6283 	    {
6284 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6285 				      soname_indx);
6286 	      def.vd_hash = bfd_elf_hash (soname);
6287 	      defaux.vda_name = soname_indx;
6288 	      name = soname;
6289 	    }
6290 	  else
6291 	    {
6292 	      size_t indx;
6293 
6294 	      name = lbasename (output_bfd->filename);
6295 	      def.vd_hash = bfd_elf_hash (name);
6296 	      indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6297 					  name, FALSE);
6298 	      if (indx == (size_t) -1)
6299 		return FALSE;
6300 	      defaux.vda_name = indx;
6301 	    }
6302 	  defaux.vda_next = 0;
6303 
6304 	  _bfd_elf_swap_verdef_out (output_bfd, &def,
6305 				    (Elf_External_Verdef *) p);
6306 	  p += sizeof (Elf_External_Verdef);
6307 	  if (info->create_default_symver)
6308 	    {
6309 	      /* Add a symbol representing this version.  */
6310 	      bh = NULL;
6311 	      if (! (_bfd_generic_link_add_one_symbol
6312 		     (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6313 		      0, NULL, FALSE,
6314 		      get_elf_backend_data (dynobj)->collect, &bh)))
6315 		return FALSE;
6316 	      h = (struct elf_link_hash_entry *) bh;
6317 	      h->non_elf = 0;
6318 	      h->def_regular = 1;
6319 	      h->type = STT_OBJECT;
6320 	      h->verinfo.vertree = NULL;
6321 
6322 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6323 		return FALSE;
6324 
6325 	      /* Create a duplicate of the base version with the same
6326 		 aux block, but different flags.  */
6327 	      def.vd_flags = 0;
6328 	      def.vd_ndx = 2;
6329 	      def.vd_aux = sizeof (Elf_External_Verdef);
6330 	      if (verdefs)
6331 		def.vd_next = (sizeof (Elf_External_Verdef)
6332 			       + sizeof (Elf_External_Verdaux));
6333 	      else
6334 		def.vd_next = 0;
6335 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
6336 					(Elf_External_Verdef *) p);
6337 	      p += sizeof (Elf_External_Verdef);
6338 	    }
6339 	  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6340 				     (Elf_External_Verdaux *) p);
6341 	  p += sizeof (Elf_External_Verdaux);
6342 
6343 	  for (t = verdefs; t != NULL; t = t->next)
6344 	    {
6345 	      unsigned int cdeps;
6346 	      struct bfd_elf_version_deps *n;
6347 
6348 	      /* Don't emit the base version twice.  */
6349 	      if (t->vernum == 0)
6350 		continue;
6351 
6352 	      cdeps = 0;
6353 	      for (n = t->deps; n != NULL; n = n->next)
6354 		++cdeps;
6355 
6356 	      /* Add a symbol representing this version.  */
6357 	      bh = NULL;
6358 	      if (! (_bfd_generic_link_add_one_symbol
6359 		     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6360 		      0, NULL, FALSE,
6361 		      get_elf_backend_data (dynobj)->collect, &bh)))
6362 		return FALSE;
6363 	      h = (struct elf_link_hash_entry *) bh;
6364 	      h->non_elf = 0;
6365 	      h->def_regular = 1;
6366 	      h->type = STT_OBJECT;
6367 	      h->verinfo.vertree = t;
6368 
6369 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
6370 		return FALSE;
6371 
6372 	      def.vd_version = VER_DEF_CURRENT;
6373 	      def.vd_flags = 0;
6374 	      if (t->globals.list == NULL
6375 		  && t->locals.list == NULL
6376 		  && ! t->used)
6377 		def.vd_flags |= VER_FLG_WEAK;
6378 	      def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6379 	      def.vd_cnt = cdeps + 1;
6380 	      def.vd_hash = bfd_elf_hash (t->name);
6381 	      def.vd_aux = sizeof (Elf_External_Verdef);
6382 	      def.vd_next = 0;
6383 
6384 	      /* If a basever node is next, it *must* be the last node in
6385 		 the chain, otherwise Verdef construction breaks.  */
6386 	      if (t->next != NULL && t->next->vernum == 0)
6387 		BFD_ASSERT (t->next->next == NULL);
6388 
6389 	      if (t->next != NULL && t->next->vernum != 0)
6390 		def.vd_next = (sizeof (Elf_External_Verdef)
6391 			       + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6392 
6393 	      _bfd_elf_swap_verdef_out (output_bfd, &def,
6394 					(Elf_External_Verdef *) p);
6395 	      p += sizeof (Elf_External_Verdef);
6396 
6397 	      defaux.vda_name = h->dynstr_index;
6398 	      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6399 				      h->dynstr_index);
6400 	      defaux.vda_next = 0;
6401 	      if (t->deps != NULL)
6402 		defaux.vda_next = sizeof (Elf_External_Verdaux);
6403 	      t->name_indx = defaux.vda_name;
6404 
6405 	      _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6406 					 (Elf_External_Verdaux *) p);
6407 	      p += sizeof (Elf_External_Verdaux);
6408 
6409 	      for (n = t->deps; n != NULL; n = n->next)
6410 		{
6411 		  if (n->version_needed == NULL)
6412 		    {
6413 		      /* This can happen if there was an error in the
6414 			 version script.  */
6415 		      defaux.vda_name = 0;
6416 		    }
6417 		  else
6418 		    {
6419 		      defaux.vda_name = n->version_needed->name_indx;
6420 		      _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6421 					      defaux.vda_name);
6422 		    }
6423 		  if (n->next == NULL)
6424 		    defaux.vda_next = 0;
6425 		  else
6426 		    defaux.vda_next = sizeof (Elf_External_Verdaux);
6427 
6428 		  _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6429 					     (Elf_External_Verdaux *) p);
6430 		  p += sizeof (Elf_External_Verdaux);
6431 		}
6432 	    }
6433 
6434 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6435 	      || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6436 	    return FALSE;
6437 
6438 	  elf_tdata (output_bfd)->cverdefs = cdefs;
6439 	}
6440 
6441       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6442 	{
6443 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6444 	    return FALSE;
6445 	}
6446       else if (info->flags & DF_BIND_NOW)
6447 	{
6448 	  if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6449 	    return FALSE;
6450 	}
6451 
6452       if (info->flags_1)
6453 	{
6454 	  if (bfd_link_executable (info))
6455 	    info->flags_1 &= ~ (DF_1_INITFIRST
6456 				| DF_1_NODELETE
6457 				| DF_1_NOOPEN);
6458 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6459 	    return FALSE;
6460 	}
6461 
6462       /* Work out the size of the version reference section.  */
6463 
6464       s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6465       BFD_ASSERT (s != NULL);
6466       {
6467 	struct elf_find_verdep_info sinfo;
6468 
6469 	sinfo.info = info;
6470 	sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6471 	if (sinfo.vers == 0)
6472 	  sinfo.vers = 1;
6473 	sinfo.failed = FALSE;
6474 
6475 	elf_link_hash_traverse (elf_hash_table (info),
6476 				_bfd_elf_link_find_version_dependencies,
6477 				&sinfo);
6478 	if (sinfo.failed)
6479 	  return FALSE;
6480 
6481 	if (elf_tdata (output_bfd)->verref == NULL)
6482 	  s->flags |= SEC_EXCLUDE;
6483 	else
6484 	  {
6485 	    Elf_Internal_Verneed *t;
6486 	    unsigned int size;
6487 	    unsigned int crefs;
6488 	    bfd_byte *p;
6489 
6490 	    /* Build the version dependency section.  */
6491 	    size = 0;
6492 	    crefs = 0;
6493 	    for (t = elf_tdata (output_bfd)->verref;
6494 		 t != NULL;
6495 		 t = t->vn_nextref)
6496 	      {
6497 		Elf_Internal_Vernaux *a;
6498 
6499 		size += sizeof (Elf_External_Verneed);
6500 		++crefs;
6501 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6502 		  size += sizeof (Elf_External_Vernaux);
6503 	      }
6504 
6505 	    s->size = size;
6506 	    s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6507 	    if (s->contents == NULL)
6508 	      return FALSE;
6509 
6510 	    p = s->contents;
6511 	    for (t = elf_tdata (output_bfd)->verref;
6512 		 t != NULL;
6513 		 t = t->vn_nextref)
6514 	      {
6515 		unsigned int caux;
6516 		Elf_Internal_Vernaux *a;
6517 		size_t indx;
6518 
6519 		caux = 0;
6520 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6521 		  ++caux;
6522 
6523 		t->vn_version = VER_NEED_CURRENT;
6524 		t->vn_cnt = caux;
6525 		indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6526 					    elf_dt_name (t->vn_bfd) != NULL
6527 					    ? elf_dt_name (t->vn_bfd)
6528 					    : lbasename (t->vn_bfd->filename),
6529 					    FALSE);
6530 		if (indx == (size_t) -1)
6531 		  return FALSE;
6532 		t->vn_file = indx;
6533 		t->vn_aux = sizeof (Elf_External_Verneed);
6534 		if (t->vn_nextref == NULL)
6535 		  t->vn_next = 0;
6536 		else
6537 		  t->vn_next = (sizeof (Elf_External_Verneed)
6538 				+ caux * sizeof (Elf_External_Vernaux));
6539 
6540 		_bfd_elf_swap_verneed_out (output_bfd, t,
6541 					   (Elf_External_Verneed *) p);
6542 		p += sizeof (Elf_External_Verneed);
6543 
6544 		for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6545 		  {
6546 		    a->vna_hash = bfd_elf_hash (a->vna_nodename);
6547 		    indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6548 						a->vna_nodename, FALSE);
6549 		    if (indx == (size_t) -1)
6550 		      return FALSE;
6551 		    a->vna_name = indx;
6552 		    if (a->vna_nextptr == NULL)
6553 		      a->vna_next = 0;
6554 		    else
6555 		      a->vna_next = sizeof (Elf_External_Vernaux);
6556 
6557 		    _bfd_elf_swap_vernaux_out (output_bfd, a,
6558 					       (Elf_External_Vernaux *) p);
6559 		    p += sizeof (Elf_External_Vernaux);
6560 		  }
6561 	      }
6562 
6563 	    if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6564 		|| !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6565 	      return FALSE;
6566 
6567 	    elf_tdata (output_bfd)->cverrefs = crefs;
6568 	  }
6569       }
6570 
6571       if ((elf_tdata (output_bfd)->cverrefs == 0
6572 	   && elf_tdata (output_bfd)->cverdefs == 0)
6573 	  || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6574 					     &section_sym_count) == 0)
6575 	{
6576 	  s = bfd_get_linker_section (dynobj, ".gnu.version");
6577 	  s->flags |= SEC_EXCLUDE;
6578 	}
6579     }
6580   return TRUE;
6581 }
6582 
6583 /* Find the first non-excluded output section.  We'll use its
6584    section symbol for some emitted relocs.  */
6585 void
6586 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6587 {
6588   asection *s;
6589 
6590   for (s = output_bfd->sections; s != NULL; s = s->next)
6591     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6592 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6593       {
6594 	elf_hash_table (info)->text_index_section = s;
6595 	break;
6596       }
6597 }
6598 
6599 /* Find two non-excluded output sections, one for code, one for data.
6600    We'll use their section symbols for some emitted relocs.  */
6601 void
6602 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6603 {
6604   asection *s;
6605 
6606   /* Data first, since setting text_index_section changes
6607      _bfd_elf_link_omit_section_dynsym.  */
6608   for (s = output_bfd->sections; s != NULL; s = s->next)
6609     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6610 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6611       {
6612 	elf_hash_table (info)->data_index_section = s;
6613 	break;
6614       }
6615 
6616   for (s = output_bfd->sections; s != NULL; s = s->next)
6617     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6618 	 == (SEC_ALLOC | SEC_READONLY))
6619 	&& !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6620       {
6621 	elf_hash_table (info)->text_index_section = s;
6622 	break;
6623       }
6624 
6625   if (elf_hash_table (info)->text_index_section == NULL)
6626     elf_hash_table (info)->text_index_section
6627       = elf_hash_table (info)->data_index_section;
6628 }
6629 
6630 bfd_boolean
6631 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6632 {
6633   const struct elf_backend_data *bed;
6634 
6635   if (!is_elf_hash_table (info->hash))
6636     return TRUE;
6637 
6638   bed = get_elf_backend_data (output_bfd);
6639   (*bed->elf_backend_init_index_section) (output_bfd, info);
6640 
6641   if (elf_hash_table (info)->dynamic_sections_created)
6642     {
6643       bfd *dynobj;
6644       asection *s;
6645       bfd_size_type dynsymcount;
6646       unsigned long section_sym_count;
6647       unsigned int dtagcount;
6648 
6649       dynobj = elf_hash_table (info)->dynobj;
6650 
6651       /* Assign dynsym indicies.  In a shared library we generate a
6652 	 section symbol for each output section, which come first.
6653 	 Next come all of the back-end allocated local dynamic syms,
6654 	 followed by the rest of the global symbols.  */
6655 
6656       dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6657 						    &section_sym_count);
6658 
6659       /* Work out the size of the symbol version section.  */
6660       s = bfd_get_linker_section (dynobj, ".gnu.version");
6661       BFD_ASSERT (s != NULL);
6662       if ((s->flags & SEC_EXCLUDE) == 0)
6663 	{
6664 	  s->size = dynsymcount * sizeof (Elf_External_Versym);
6665 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6666 	  if (s->contents == NULL)
6667 	    return FALSE;
6668 
6669 	  if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6670 	    return FALSE;
6671 	}
6672 
6673       /* Set the size of the .dynsym and .hash sections.  We counted
6674 	 the number of dynamic symbols in elf_link_add_object_symbols.
6675 	 We will build the contents of .dynsym and .hash when we build
6676 	 the final symbol table, because until then we do not know the
6677 	 correct value to give the symbols.  We built the .dynstr
6678 	 section as we went along in elf_link_add_object_symbols.  */
6679       s = elf_hash_table (info)->dynsym;
6680       BFD_ASSERT (s != NULL);
6681       s->size = dynsymcount * bed->s->sizeof_sym;
6682 
6683       s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6684       if (s->contents == NULL)
6685 	return FALSE;
6686 
6687       /* The first entry in .dynsym is a dummy symbol.  Clear all the
6688 	 section syms, in case we don't output them all.  */
6689       ++section_sym_count;
6690       memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6691 
6692       elf_hash_table (info)->bucketcount = 0;
6693 
6694       /* Compute the size of the hashing table.  As a side effect this
6695 	 computes the hash values for all the names we export.  */
6696       if (info->emit_hash)
6697 	{
6698 	  unsigned long int *hashcodes;
6699 	  struct hash_codes_info hashinf;
6700 	  bfd_size_type amt;
6701 	  unsigned long int nsyms;
6702 	  size_t bucketcount;
6703 	  size_t hash_entry_size;
6704 
6705 	  /* Compute the hash values for all exported symbols.  At the same
6706 	     time store the values in an array so that we could use them for
6707 	     optimizations.  */
6708 	  amt = dynsymcount * sizeof (unsigned long int);
6709 	  hashcodes = (unsigned long int *) bfd_malloc (amt);
6710 	  if (hashcodes == NULL)
6711 	    return FALSE;
6712 	  hashinf.hashcodes = hashcodes;
6713 	  hashinf.error = FALSE;
6714 
6715 	  /* Put all hash values in HASHCODES.  */
6716 	  elf_link_hash_traverse (elf_hash_table (info),
6717 				  elf_collect_hash_codes, &hashinf);
6718 	  if (hashinf.error)
6719 	    {
6720 	      free (hashcodes);
6721 	      return FALSE;
6722 	    }
6723 
6724 	  nsyms = hashinf.hashcodes - hashcodes;
6725 	  bucketcount
6726 	    = compute_bucket_count (info, hashcodes, nsyms, 0);
6727 	  free (hashcodes);
6728 
6729 	  if (bucketcount == 0)
6730 	    return FALSE;
6731 
6732 	  elf_hash_table (info)->bucketcount = bucketcount;
6733 
6734 	  s = bfd_get_linker_section (dynobj, ".hash");
6735 	  BFD_ASSERT (s != NULL);
6736 	  hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6737 	  s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6738 	  s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6739 	  if (s->contents == NULL)
6740 	    return FALSE;
6741 
6742 	  bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6743 	  bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6744 		   s->contents + hash_entry_size);
6745 	}
6746 
6747       if (info->emit_gnu_hash)
6748 	{
6749 	  size_t i, cnt;
6750 	  unsigned char *contents;
6751 	  struct collect_gnu_hash_codes cinfo;
6752 	  bfd_size_type amt;
6753 	  size_t bucketcount;
6754 
6755 	  memset (&cinfo, 0, sizeof (cinfo));
6756 
6757 	  /* Compute the hash values for all exported symbols.  At the same
6758 	     time store the values in an array so that we could use them for
6759 	     optimizations.  */
6760 	  amt = dynsymcount * 2 * sizeof (unsigned long int);
6761 	  cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6762 	  if (cinfo.hashcodes == NULL)
6763 	    return FALSE;
6764 
6765 	  cinfo.hashval = cinfo.hashcodes + dynsymcount;
6766 	  cinfo.min_dynindx = -1;
6767 	  cinfo.output_bfd = output_bfd;
6768 	  cinfo.bed = bed;
6769 
6770 	  /* Put all hash values in HASHCODES.  */
6771 	  elf_link_hash_traverse (elf_hash_table (info),
6772 				  elf_collect_gnu_hash_codes, &cinfo);
6773 	  if (cinfo.error)
6774 	    {
6775 	      free (cinfo.hashcodes);
6776 	      return FALSE;
6777 	    }
6778 
6779 	  bucketcount
6780 	    = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6781 
6782 	  if (bucketcount == 0)
6783 	    {
6784 	      free (cinfo.hashcodes);
6785 	      return FALSE;
6786 	    }
6787 
6788 	  s = bfd_get_linker_section (dynobj, ".gnu.hash");
6789 	  BFD_ASSERT (s != NULL);
6790 
6791 	  if (cinfo.nsyms == 0)
6792 	    {
6793 	      /* Empty .gnu.hash section is special.  */
6794 	      BFD_ASSERT (cinfo.min_dynindx == -1);
6795 	      free (cinfo.hashcodes);
6796 	      s->size = 5 * 4 + bed->s->arch_size / 8;
6797 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6798 	      if (contents == NULL)
6799 		return FALSE;
6800 	      s->contents = contents;
6801 	      /* 1 empty bucket.  */
6802 	      bfd_put_32 (output_bfd, 1, contents);
6803 	      /* SYMIDX above the special symbol 0.  */
6804 	      bfd_put_32 (output_bfd, 1, contents + 4);
6805 	      /* Just one word for bitmask.  */
6806 	      bfd_put_32 (output_bfd, 1, contents + 8);
6807 	      /* Only hash fn bloom filter.  */
6808 	      bfd_put_32 (output_bfd, 0, contents + 12);
6809 	      /* No hashes are valid - empty bitmask.  */
6810 	      bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6811 	      /* No hashes in the only bucket.  */
6812 	      bfd_put_32 (output_bfd, 0,
6813 			  contents + 16 + bed->s->arch_size / 8);
6814 	    }
6815 	  else
6816 	    {
6817 	      unsigned long int maskwords, maskbitslog2, x;
6818 	      BFD_ASSERT (cinfo.min_dynindx != -1);
6819 
6820 	      x = cinfo.nsyms;
6821 	      maskbitslog2 = 1;
6822 	      while ((x >>= 1) != 0)
6823 		++maskbitslog2;
6824 	      if (maskbitslog2 < 3)
6825 		maskbitslog2 = 5;
6826 	      else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6827 		maskbitslog2 = maskbitslog2 + 3;
6828 	      else
6829 		maskbitslog2 = maskbitslog2 + 2;
6830 	      if (bed->s->arch_size == 64)
6831 		{
6832 		  if (maskbitslog2 == 5)
6833 		    maskbitslog2 = 6;
6834 		  cinfo.shift1 = 6;
6835 		}
6836 	      else
6837 		cinfo.shift1 = 5;
6838 	      cinfo.mask = (1 << cinfo.shift1) - 1;
6839 	      cinfo.shift2 = maskbitslog2;
6840 	      cinfo.maskbits = 1 << maskbitslog2;
6841 	      maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6842 	      amt = bucketcount * sizeof (unsigned long int) * 2;
6843 	      amt += maskwords * sizeof (bfd_vma);
6844 	      cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6845 	      if (cinfo.bitmask == NULL)
6846 		{
6847 		  free (cinfo.hashcodes);
6848 		  return FALSE;
6849 		}
6850 
6851 	      cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6852 	      cinfo.indx = cinfo.counts + bucketcount;
6853 	      cinfo.symindx = dynsymcount - cinfo.nsyms;
6854 	      memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6855 
6856 	      /* Determine how often each hash bucket is used.  */
6857 	      memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6858 	      for (i = 0; i < cinfo.nsyms; ++i)
6859 		++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6860 
6861 	      for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6862 		if (cinfo.counts[i] != 0)
6863 		  {
6864 		    cinfo.indx[i] = cnt;
6865 		    cnt += cinfo.counts[i];
6866 		  }
6867 	      BFD_ASSERT (cnt == dynsymcount);
6868 	      cinfo.bucketcount = bucketcount;
6869 	      cinfo.local_indx = cinfo.min_dynindx;
6870 
6871 	      s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6872 	      s->size += cinfo.maskbits / 8;
6873 	      contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6874 	      if (contents == NULL)
6875 		{
6876 		  free (cinfo.bitmask);
6877 		  free (cinfo.hashcodes);
6878 		  return FALSE;
6879 		}
6880 
6881 	      s->contents = contents;
6882 	      bfd_put_32 (output_bfd, bucketcount, contents);
6883 	      bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6884 	      bfd_put_32 (output_bfd, maskwords, contents + 8);
6885 	      bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6886 	      contents += 16 + cinfo.maskbits / 8;
6887 
6888 	      for (i = 0; i < bucketcount; ++i)
6889 		{
6890 		  if (cinfo.counts[i] == 0)
6891 		    bfd_put_32 (output_bfd, 0, contents);
6892 		  else
6893 		    bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6894 		  contents += 4;
6895 		}
6896 
6897 	      cinfo.contents = contents;
6898 
6899 	      /* Renumber dynamic symbols, populate .gnu.hash section.  */
6900 	      elf_link_hash_traverse (elf_hash_table (info),
6901 				      elf_renumber_gnu_hash_syms, &cinfo);
6902 
6903 	      contents = s->contents + 16;
6904 	      for (i = 0; i < maskwords; ++i)
6905 		{
6906 		  bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6907 			   contents);
6908 		  contents += bed->s->arch_size / 8;
6909 		}
6910 
6911 	      free (cinfo.bitmask);
6912 	      free (cinfo.hashcodes);
6913 	    }
6914 	}
6915 
6916       s = bfd_get_linker_section (dynobj, ".dynstr");
6917       BFD_ASSERT (s != NULL);
6918 
6919       elf_finalize_dynstr (output_bfd, info);
6920 
6921       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6922 
6923       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6924 	if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6925 	  return FALSE;
6926     }
6927 
6928   return TRUE;
6929 }
6930 
6931 /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
6932 
6933 static void
6934 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6935 			    asection *sec)
6936 {
6937   BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6938   sec->sec_info_type = SEC_INFO_TYPE_NONE;
6939 }
6940 
6941 /* Finish SHF_MERGE section merging.  */
6942 
6943 bfd_boolean
6944 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6945 {
6946   bfd *ibfd;
6947   asection *sec;
6948 
6949   if (!is_elf_hash_table (info->hash))
6950     return FALSE;
6951 
6952   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6953     if ((ibfd->flags & DYNAMIC) == 0
6954 	&& bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6955 	&& (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6956 	    == get_elf_backend_data (obfd)->s->elfclass))
6957       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6958 	if ((sec->flags & SEC_MERGE) != 0
6959 	    && !bfd_is_abs_section (sec->output_section))
6960 	  {
6961 	    struct bfd_elf_section_data *secdata;
6962 
6963 	    secdata = elf_section_data (sec);
6964 	    if (! _bfd_add_merge_section (obfd,
6965 					  &elf_hash_table (info)->merge_info,
6966 					  sec, &secdata->sec_info))
6967 	      return FALSE;
6968 	    else if (secdata->sec_info)
6969 	      sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6970 	  }
6971 
6972   if (elf_hash_table (info)->merge_info != NULL)
6973     _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6974 			 merge_sections_remove_hook);
6975   return TRUE;
6976 }
6977 
6978 /* Create an entry in an ELF linker hash table.  */
6979 
6980 struct bfd_hash_entry *
6981 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6982 			    struct bfd_hash_table *table,
6983 			    const char *string)
6984 {
6985   /* Allocate the structure if it has not already been allocated by a
6986      subclass.  */
6987   if (entry == NULL)
6988     {
6989       entry = (struct bfd_hash_entry *)
6990 	bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6991       if (entry == NULL)
6992 	return entry;
6993     }
6994 
6995   /* Call the allocation method of the superclass.  */
6996   entry = _bfd_link_hash_newfunc (entry, table, string);
6997   if (entry != NULL)
6998     {
6999       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7000       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7001 
7002       /* Set local fields.  */
7003       ret->indx = -1;
7004       ret->dynindx = -1;
7005       ret->got = htab->init_got_refcount;
7006       ret->plt = htab->init_plt_refcount;
7007       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7008 			      - offsetof (struct elf_link_hash_entry, size)));
7009       /* Assume that we have been called by a non-ELF symbol reader.
7010 	 This flag is then reset by the code which reads an ELF input
7011 	 file.  This ensures that a symbol created by a non-ELF symbol
7012 	 reader will have the flag set correctly.  */
7013       ret->non_elf = 1;
7014     }
7015 
7016   return entry;
7017 }
7018 
7019 /* Copy data from an indirect symbol to its direct symbol, hiding the
7020    old indirect symbol.  Also used for copying flags to a weakdef.  */
7021 
7022 void
7023 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7024 				  struct elf_link_hash_entry *dir,
7025 				  struct elf_link_hash_entry *ind)
7026 {
7027   struct elf_link_hash_table *htab;
7028 
7029   /* Copy down any references that we may have already seen to the
7030      symbol which just became indirect if DIR isn't a hidden versioned
7031      symbol.  */
7032 
7033   if (dir->versioned != versioned_hidden)
7034     {
7035       dir->ref_dynamic |= ind->ref_dynamic;
7036       dir->ref_regular |= ind->ref_regular;
7037       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7038       dir->non_got_ref |= ind->non_got_ref;
7039       dir->needs_plt |= ind->needs_plt;
7040       dir->pointer_equality_needed |= ind->pointer_equality_needed;
7041     }
7042 
7043   if (ind->root.type != bfd_link_hash_indirect)
7044     return;
7045 
7046   /* Copy over the global and procedure linkage table refcount entries.
7047      These may have been already set up by a check_relocs routine.  */
7048   htab = elf_hash_table (info);
7049   if (ind->got.refcount > htab->init_got_refcount.refcount)
7050     {
7051       if (dir->got.refcount < 0)
7052 	dir->got.refcount = 0;
7053       dir->got.refcount += ind->got.refcount;
7054       ind->got.refcount = htab->init_got_refcount.refcount;
7055     }
7056 
7057   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7058     {
7059       if (dir->plt.refcount < 0)
7060 	dir->plt.refcount = 0;
7061       dir->plt.refcount += ind->plt.refcount;
7062       ind->plt.refcount = htab->init_plt_refcount.refcount;
7063     }
7064 
7065   if (ind->dynindx != -1)
7066     {
7067       if (dir->dynindx != -1)
7068 	_bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7069       dir->dynindx = ind->dynindx;
7070       dir->dynstr_index = ind->dynstr_index;
7071       ind->dynindx = -1;
7072       ind->dynstr_index = 0;
7073     }
7074 }
7075 
7076 void
7077 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7078 				struct elf_link_hash_entry *h,
7079 				bfd_boolean force_local)
7080 {
7081   /* STT_GNU_IFUNC symbol must go through PLT.  */
7082   if (h->type != STT_GNU_IFUNC)
7083     {
7084       h->plt = elf_hash_table (info)->init_plt_offset;
7085       h->needs_plt = 0;
7086     }
7087   if (force_local)
7088     {
7089       h->forced_local = 1;
7090       if (h->dynindx != -1)
7091 	{
7092 	  h->dynindx = -1;
7093 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7094 				  h->dynstr_index);
7095 	}
7096     }
7097 }
7098 
7099 /* Initialize an ELF linker hash table.  *TABLE has been zeroed by our
7100    caller.  */
7101 
7102 bfd_boolean
7103 _bfd_elf_link_hash_table_init
7104   (struct elf_link_hash_table *table,
7105    bfd *abfd,
7106    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7107 				      struct bfd_hash_table *,
7108 				      const char *),
7109    unsigned int entsize,
7110    enum elf_target_id target_id)
7111 {
7112   bfd_boolean ret;
7113   int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7114 
7115   table->init_got_refcount.refcount = can_refcount - 1;
7116   table->init_plt_refcount.refcount = can_refcount - 1;
7117   table->init_got_offset.offset = -(bfd_vma) 1;
7118   table->init_plt_offset.offset = -(bfd_vma) 1;
7119   /* The first dynamic symbol is a dummy.  */
7120   table->dynsymcount = 1;
7121 
7122   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7123 
7124   table->root.type = bfd_link_elf_hash_table;
7125   table->hash_table_id = target_id;
7126 
7127   return ret;
7128 }
7129 
7130 /* Create an ELF linker hash table.  */
7131 
7132 struct bfd_link_hash_table *
7133 _bfd_elf_link_hash_table_create (bfd *abfd)
7134 {
7135   struct elf_link_hash_table *ret;
7136   bfd_size_type amt = sizeof (struct elf_link_hash_table);
7137 
7138   ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7139   if (ret == NULL)
7140     return NULL;
7141 
7142   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7143 				       sizeof (struct elf_link_hash_entry),
7144 				       GENERIC_ELF_DATA))
7145     {
7146       free (ret);
7147       return NULL;
7148     }
7149   ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7150 
7151   return &ret->root;
7152 }
7153 
7154 /* Destroy an ELF linker hash table.  */
7155 
7156 void
7157 _bfd_elf_link_hash_table_free (bfd *obfd)
7158 {
7159   struct elf_link_hash_table *htab;
7160 
7161   htab = (struct elf_link_hash_table *) obfd->link.hash;
7162   if (htab->dynstr != NULL)
7163     _bfd_elf_strtab_free (htab->dynstr);
7164   _bfd_merge_sections_free (htab->merge_info);
7165   _bfd_generic_link_hash_table_free (obfd);
7166 }
7167 
7168 /* This is a hook for the ELF emulation code in the generic linker to
7169    tell the backend linker what file name to use for the DT_NEEDED
7170    entry for a dynamic object.  */
7171 
7172 void
7173 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7174 {
7175   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7176       && bfd_get_format (abfd) == bfd_object)
7177     elf_dt_name (abfd) = name;
7178 }
7179 
7180 int
7181 bfd_elf_get_dyn_lib_class (bfd *abfd)
7182 {
7183   int lib_class;
7184   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7185       && bfd_get_format (abfd) == bfd_object)
7186     lib_class = elf_dyn_lib_class (abfd);
7187   else
7188     lib_class = 0;
7189   return lib_class;
7190 }
7191 
7192 void
7193 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7194 {
7195   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7196       && bfd_get_format (abfd) == bfd_object)
7197     elf_dyn_lib_class (abfd) = lib_class;
7198 }
7199 
7200 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
7201    the linker ELF emulation code.  */
7202 
7203 struct bfd_link_needed_list *
7204 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7205 			 struct bfd_link_info *info)
7206 {
7207   if (! is_elf_hash_table (info->hash))
7208     return NULL;
7209   return elf_hash_table (info)->needed;
7210 }
7211 
7212 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
7213    hook for the linker ELF emulation code.  */
7214 
7215 struct bfd_link_needed_list *
7216 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7217 			  struct bfd_link_info *info)
7218 {
7219   if (! is_elf_hash_table (info->hash))
7220     return NULL;
7221   return elf_hash_table (info)->runpath;
7222 }
7223 
7224 /* Get the name actually used for a dynamic object for a link.  This
7225    is the SONAME entry if there is one.  Otherwise, it is the string
7226    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
7227 
7228 const char *
7229 bfd_elf_get_dt_soname (bfd *abfd)
7230 {
7231   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7232       && bfd_get_format (abfd) == bfd_object)
7233     return elf_dt_name (abfd);
7234   return NULL;
7235 }
7236 
7237 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
7238    the ELF linker emulation code.  */
7239 
7240 bfd_boolean
7241 bfd_elf_get_bfd_needed_list (bfd *abfd,
7242 			     struct bfd_link_needed_list **pneeded)
7243 {
7244   asection *s;
7245   bfd_byte *dynbuf = NULL;
7246   unsigned int elfsec;
7247   unsigned long shlink;
7248   bfd_byte *extdyn, *extdynend;
7249   size_t extdynsize;
7250   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7251 
7252   *pneeded = NULL;
7253 
7254   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7255       || bfd_get_format (abfd) != bfd_object)
7256     return TRUE;
7257 
7258   s = bfd_get_section_by_name (abfd, ".dynamic");
7259   if (s == NULL || s->size == 0)
7260     return TRUE;
7261 
7262   if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7263     goto error_return;
7264 
7265   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7266   if (elfsec == SHN_BAD)
7267     goto error_return;
7268 
7269   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7270 
7271   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7272   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7273 
7274   extdyn = dynbuf;
7275   extdynend = extdyn + s->size;
7276   for (; extdyn < extdynend; extdyn += extdynsize)
7277     {
7278       Elf_Internal_Dyn dyn;
7279 
7280       (*swap_dyn_in) (abfd, extdyn, &dyn);
7281 
7282       if (dyn.d_tag == DT_NULL)
7283 	break;
7284 
7285       if (dyn.d_tag == DT_NEEDED)
7286 	{
7287 	  const char *string;
7288 	  struct bfd_link_needed_list *l;
7289 	  unsigned int tagv = dyn.d_un.d_val;
7290 	  bfd_size_type amt;
7291 
7292 	  string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7293 	  if (string == NULL)
7294 	    goto error_return;
7295 
7296 	  amt = sizeof *l;
7297 	  l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7298 	  if (l == NULL)
7299 	    goto error_return;
7300 
7301 	  l->by = abfd;
7302 	  l->name = string;
7303 	  l->next = *pneeded;
7304 	  *pneeded = l;
7305 	}
7306     }
7307 
7308   free (dynbuf);
7309 
7310   return TRUE;
7311 
7312  error_return:
7313   if (dynbuf != NULL)
7314     free (dynbuf);
7315   return FALSE;
7316 }
7317 
7318 struct elf_symbuf_symbol
7319 {
7320   unsigned long st_name;	/* Symbol name, index in string tbl */
7321   unsigned char st_info;	/* Type and binding attributes */
7322   unsigned char st_other;	/* Visibilty, and target specific */
7323 };
7324 
7325 struct elf_symbuf_head
7326 {
7327   struct elf_symbuf_symbol *ssym;
7328   size_t count;
7329   unsigned int st_shndx;
7330 };
7331 
7332 struct elf_symbol
7333 {
7334   union
7335     {
7336       Elf_Internal_Sym *isym;
7337       struct elf_symbuf_symbol *ssym;
7338     } u;
7339   const char *name;
7340 };
7341 
7342 /* Sort references to symbols by ascending section number.  */
7343 
7344 static int
7345 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7346 {
7347   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7348   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7349 
7350   return s1->st_shndx - s2->st_shndx;
7351 }
7352 
7353 static int
7354 elf_sym_name_compare (const void *arg1, const void *arg2)
7355 {
7356   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7357   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7358   return strcmp (s1->name, s2->name);
7359 }
7360 
7361 static struct elf_symbuf_head *
7362 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7363 {
7364   Elf_Internal_Sym **ind, **indbufend, **indbuf;
7365   struct elf_symbuf_symbol *ssym;
7366   struct elf_symbuf_head *ssymbuf, *ssymhead;
7367   size_t i, shndx_count, total_size;
7368 
7369   indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7370   if (indbuf == NULL)
7371     return NULL;
7372 
7373   for (ind = indbuf, i = 0; i < symcount; i++)
7374     if (isymbuf[i].st_shndx != SHN_UNDEF)
7375       *ind++ = &isymbuf[i];
7376   indbufend = ind;
7377 
7378   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7379 	 elf_sort_elf_symbol);
7380 
7381   shndx_count = 0;
7382   if (indbufend > indbuf)
7383     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7384       if (ind[0]->st_shndx != ind[1]->st_shndx)
7385 	shndx_count++;
7386 
7387   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7388 		+ (indbufend - indbuf) * sizeof (*ssym));
7389   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7390   if (ssymbuf == NULL)
7391     {
7392       free (indbuf);
7393       return NULL;
7394     }
7395 
7396   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7397   ssymbuf->ssym = NULL;
7398   ssymbuf->count = shndx_count;
7399   ssymbuf->st_shndx = 0;
7400   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7401     {
7402       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7403 	{
7404 	  ssymhead++;
7405 	  ssymhead->ssym = ssym;
7406 	  ssymhead->count = 0;
7407 	  ssymhead->st_shndx = (*ind)->st_shndx;
7408 	}
7409       ssym->st_name = (*ind)->st_name;
7410       ssym->st_info = (*ind)->st_info;
7411       ssym->st_other = (*ind)->st_other;
7412       ssymhead->count++;
7413     }
7414   BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7415 	      && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7416 		  == total_size));
7417 
7418   free (indbuf);
7419   return ssymbuf;
7420 }
7421 
7422 /* Check if 2 sections define the same set of local and global
7423    symbols.  */
7424 
7425 static bfd_boolean
7426 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7427 				   struct bfd_link_info *info)
7428 {
7429   bfd *bfd1, *bfd2;
7430   const struct elf_backend_data *bed1, *bed2;
7431   Elf_Internal_Shdr *hdr1, *hdr2;
7432   size_t symcount1, symcount2;
7433   Elf_Internal_Sym *isymbuf1, *isymbuf2;
7434   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7435   Elf_Internal_Sym *isym, *isymend;
7436   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7437   size_t count1, count2, i;
7438   unsigned int shndx1, shndx2;
7439   bfd_boolean result;
7440 
7441   bfd1 = sec1->owner;
7442   bfd2 = sec2->owner;
7443 
7444   /* Both sections have to be in ELF.  */
7445   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7446       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7447     return FALSE;
7448 
7449   if (elf_section_type (sec1) != elf_section_type (sec2))
7450     return FALSE;
7451 
7452   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7453   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7454   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7455     return FALSE;
7456 
7457   bed1 = get_elf_backend_data (bfd1);
7458   bed2 = get_elf_backend_data (bfd2);
7459   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7460   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7461   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7462   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7463 
7464   if (symcount1 == 0 || symcount2 == 0)
7465     return FALSE;
7466 
7467   result = FALSE;
7468   isymbuf1 = NULL;
7469   isymbuf2 = NULL;
7470   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7471   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7472 
7473   if (ssymbuf1 == NULL)
7474     {
7475       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7476 				       NULL, NULL, NULL);
7477       if (isymbuf1 == NULL)
7478 	goto done;
7479 
7480       if (!info->reduce_memory_overheads)
7481 	elf_tdata (bfd1)->symbuf = ssymbuf1
7482 	  = elf_create_symbuf (symcount1, isymbuf1);
7483     }
7484 
7485   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7486     {
7487       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7488 				       NULL, NULL, NULL);
7489       if (isymbuf2 == NULL)
7490 	goto done;
7491 
7492       if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7493 	elf_tdata (bfd2)->symbuf = ssymbuf2
7494 	  = elf_create_symbuf (symcount2, isymbuf2);
7495     }
7496 
7497   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7498     {
7499       /* Optimized faster version.  */
7500       size_t lo, hi, mid;
7501       struct elf_symbol *symp;
7502       struct elf_symbuf_symbol *ssym, *ssymend;
7503 
7504       lo = 0;
7505       hi = ssymbuf1->count;
7506       ssymbuf1++;
7507       count1 = 0;
7508       while (lo < hi)
7509 	{
7510 	  mid = (lo + hi) / 2;
7511 	  if (shndx1 < ssymbuf1[mid].st_shndx)
7512 	    hi = mid;
7513 	  else if (shndx1 > ssymbuf1[mid].st_shndx)
7514 	    lo = mid + 1;
7515 	  else
7516 	    {
7517 	      count1 = ssymbuf1[mid].count;
7518 	      ssymbuf1 += mid;
7519 	      break;
7520 	    }
7521 	}
7522 
7523       lo = 0;
7524       hi = ssymbuf2->count;
7525       ssymbuf2++;
7526       count2 = 0;
7527       while (lo < hi)
7528 	{
7529 	  mid = (lo + hi) / 2;
7530 	  if (shndx2 < ssymbuf2[mid].st_shndx)
7531 	    hi = mid;
7532 	  else if (shndx2 > ssymbuf2[mid].st_shndx)
7533 	    lo = mid + 1;
7534 	  else
7535 	    {
7536 	      count2 = ssymbuf2[mid].count;
7537 	      ssymbuf2 += mid;
7538 	      break;
7539 	    }
7540 	}
7541 
7542       if (count1 == 0 || count2 == 0 || count1 != count2)
7543 	goto done;
7544 
7545       symtable1
7546 	= (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7547       symtable2
7548 	= (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7549       if (symtable1 == NULL || symtable2 == NULL)
7550 	goto done;
7551 
7552       symp = symtable1;
7553       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7554 	   ssym < ssymend; ssym++, symp++)
7555 	{
7556 	  symp->u.ssym = ssym;
7557 	  symp->name = bfd_elf_string_from_elf_section (bfd1,
7558 							hdr1->sh_link,
7559 							ssym->st_name);
7560 	}
7561 
7562       symp = symtable2;
7563       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7564 	   ssym < ssymend; ssym++, symp++)
7565 	{
7566 	  symp->u.ssym = ssym;
7567 	  symp->name = bfd_elf_string_from_elf_section (bfd2,
7568 							hdr2->sh_link,
7569 							ssym->st_name);
7570 	}
7571 
7572       /* Sort symbol by name.  */
7573       qsort (symtable1, count1, sizeof (struct elf_symbol),
7574 	     elf_sym_name_compare);
7575       qsort (symtable2, count1, sizeof (struct elf_symbol),
7576 	     elf_sym_name_compare);
7577 
7578       for (i = 0; i < count1; i++)
7579 	/* Two symbols must have the same binding, type and name.  */
7580 	if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7581 	    || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7582 	    || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7583 	  goto done;
7584 
7585       result = TRUE;
7586       goto done;
7587     }
7588 
7589   symtable1 = (struct elf_symbol *)
7590       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7591   symtable2 = (struct elf_symbol *)
7592       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7593   if (symtable1 == NULL || symtable2 == NULL)
7594     goto done;
7595 
7596   /* Count definitions in the section.  */
7597   count1 = 0;
7598   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7599     if (isym->st_shndx == shndx1)
7600       symtable1[count1++].u.isym = isym;
7601 
7602   count2 = 0;
7603   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7604     if (isym->st_shndx == shndx2)
7605       symtable2[count2++].u.isym = isym;
7606 
7607   if (count1 == 0 || count2 == 0 || count1 != count2)
7608     goto done;
7609 
7610   for (i = 0; i < count1; i++)
7611     symtable1[i].name
7612       = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7613 					 symtable1[i].u.isym->st_name);
7614 
7615   for (i = 0; i < count2; i++)
7616     symtable2[i].name
7617       = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7618 					 symtable2[i].u.isym->st_name);
7619 
7620   /* Sort symbol by name.  */
7621   qsort (symtable1, count1, sizeof (struct elf_symbol),
7622 	 elf_sym_name_compare);
7623   qsort (symtable2, count1, sizeof (struct elf_symbol),
7624 	 elf_sym_name_compare);
7625 
7626   for (i = 0; i < count1; i++)
7627     /* Two symbols must have the same binding, type and name.  */
7628     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7629 	|| symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7630 	|| strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7631       goto done;
7632 
7633   result = TRUE;
7634 
7635 done:
7636   if (symtable1)
7637     free (symtable1);
7638   if (symtable2)
7639     free (symtable2);
7640   if (isymbuf1)
7641     free (isymbuf1);
7642   if (isymbuf2)
7643     free (isymbuf2);
7644 
7645   return result;
7646 }
7647 
7648 /* Return TRUE if 2 section types are compatible.  */
7649 
7650 bfd_boolean
7651 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7652 				 bfd *bbfd, const asection *bsec)
7653 {
7654   if (asec == NULL
7655       || bsec == NULL
7656       || abfd->xvec->flavour != bfd_target_elf_flavour
7657       || bbfd->xvec->flavour != bfd_target_elf_flavour)
7658     return TRUE;
7659 
7660   return elf_section_type (asec) == elf_section_type (bsec);
7661 }
7662 
7663 /* Final phase of ELF linker.  */
7664 
7665 /* A structure we use to avoid passing large numbers of arguments.  */
7666 
7667 struct elf_final_link_info
7668 {
7669   /* General link information.  */
7670   struct bfd_link_info *info;
7671   /* Output BFD.  */
7672   bfd *output_bfd;
7673   /* Symbol string table.  */
7674   struct elf_strtab_hash *symstrtab;
7675   /* .hash section.  */
7676   asection *hash_sec;
7677   /* symbol version section (.gnu.version).  */
7678   asection *symver_sec;
7679   /* Buffer large enough to hold contents of any section.  */
7680   bfd_byte *contents;
7681   /* Buffer large enough to hold external relocs of any section.  */
7682   void *external_relocs;
7683   /* Buffer large enough to hold internal relocs of any section.  */
7684   Elf_Internal_Rela *internal_relocs;
7685   /* Buffer large enough to hold external local symbols of any input
7686      BFD.  */
7687   bfd_byte *external_syms;
7688   /* And a buffer for symbol section indices.  */
7689   Elf_External_Sym_Shndx *locsym_shndx;
7690   /* Buffer large enough to hold internal local symbols of any input
7691      BFD.  */
7692   Elf_Internal_Sym *internal_syms;
7693   /* Array large enough to hold a symbol index for each local symbol
7694      of any input BFD.  */
7695   long *indices;
7696   /* Array large enough to hold a section pointer for each local
7697      symbol of any input BFD.  */
7698   asection **sections;
7699   /* Buffer for SHT_SYMTAB_SHNDX section.  */
7700   Elf_External_Sym_Shndx *symshndxbuf;
7701   /* Number of STT_FILE syms seen.  */
7702   size_t filesym_count;
7703 };
7704 
7705 /* This struct is used to pass information to elf_link_output_extsym.  */
7706 
7707 struct elf_outext_info
7708 {
7709   bfd_boolean failed;
7710   bfd_boolean localsyms;
7711   bfd_boolean file_sym_done;
7712   struct elf_final_link_info *flinfo;
7713 };
7714 
7715 
7716 /* Support for evaluating a complex relocation.
7717 
7718    Complex relocations are generalized, self-describing relocations.  The
7719    implementation of them consists of two parts: complex symbols, and the
7720    relocations themselves.
7721 
7722    The relocations are use a reserved elf-wide relocation type code (R_RELC
7723    external / BFD_RELOC_RELC internal) and an encoding of relocation field
7724    information (start bit, end bit, word width, etc) into the addend.  This
7725    information is extracted from CGEN-generated operand tables within gas.
7726 
7727    Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7728    internal) representing prefix-notation expressions, including but not
7729    limited to those sorts of expressions normally encoded as addends in the
7730    addend field.  The symbol mangling format is:
7731 
7732    <node> := <literal>
7733           |  <unary-operator> ':' <node>
7734           |  <binary-operator> ':' <node> ':' <node>
7735 	  ;
7736 
7737    <literal> := 's' <digits=N> ':' <N character symbol name>
7738              |  'S' <digits=N> ':' <N character section name>
7739 	     |  '#' <hexdigits>
7740 	     ;
7741 
7742    <binary-operator> := as in C
7743    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
7744 
7745 static void
7746 set_symbol_value (bfd *bfd_with_globals,
7747 		  Elf_Internal_Sym *isymbuf,
7748 		  size_t locsymcount,
7749 		  size_t symidx,
7750 		  bfd_vma val)
7751 {
7752   struct elf_link_hash_entry **sym_hashes;
7753   struct elf_link_hash_entry *h;
7754   size_t extsymoff = locsymcount;
7755 
7756   if (symidx < locsymcount)
7757     {
7758       Elf_Internal_Sym *sym;
7759 
7760       sym = isymbuf + symidx;
7761       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7762 	{
7763 	  /* It is a local symbol: move it to the
7764 	     "absolute" section and give it a value.  */
7765 	  sym->st_shndx = SHN_ABS;
7766 	  sym->st_value = val;
7767 	  return;
7768 	}
7769       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7770       extsymoff = 0;
7771     }
7772 
7773   /* It is a global symbol: set its link type
7774      to "defined" and give it a value.  */
7775 
7776   sym_hashes = elf_sym_hashes (bfd_with_globals);
7777   h = sym_hashes [symidx - extsymoff];
7778   while (h->root.type == bfd_link_hash_indirect
7779 	 || h->root.type == bfd_link_hash_warning)
7780     h = (struct elf_link_hash_entry *) h->root.u.i.link;
7781   h->root.type = bfd_link_hash_defined;
7782   h->root.u.def.value = val;
7783   h->root.u.def.section = bfd_abs_section_ptr;
7784 }
7785 
7786 static bfd_boolean
7787 resolve_symbol (const char *name,
7788 		bfd *input_bfd,
7789 		struct elf_final_link_info *flinfo,
7790 		bfd_vma *result,
7791 		Elf_Internal_Sym *isymbuf,
7792 		size_t locsymcount)
7793 {
7794   Elf_Internal_Sym *sym;
7795   struct bfd_link_hash_entry *global_entry;
7796   const char *candidate = NULL;
7797   Elf_Internal_Shdr *symtab_hdr;
7798   size_t i;
7799 
7800   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7801 
7802   for (i = 0; i < locsymcount; ++ i)
7803     {
7804       sym = isymbuf + i;
7805 
7806       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7807 	continue;
7808 
7809       candidate = bfd_elf_string_from_elf_section (input_bfd,
7810 						   symtab_hdr->sh_link,
7811 						   sym->st_name);
7812 #ifdef DEBUG
7813       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7814 	      name, candidate, (unsigned long) sym->st_value);
7815 #endif
7816       if (candidate && strcmp (candidate, name) == 0)
7817 	{
7818 	  asection *sec = flinfo->sections [i];
7819 
7820 	  *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7821 	  *result += sec->output_offset + sec->output_section->vma;
7822 #ifdef DEBUG
7823 	  printf ("Found symbol with value %8.8lx\n",
7824 		  (unsigned long) *result);
7825 #endif
7826 	  return TRUE;
7827 	}
7828     }
7829 
7830   /* Hmm, haven't found it yet. perhaps it is a global.  */
7831   global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7832 				       FALSE, FALSE, TRUE);
7833   if (!global_entry)
7834     return FALSE;
7835 
7836   if (global_entry->type == bfd_link_hash_defined
7837       || global_entry->type == bfd_link_hash_defweak)
7838     {
7839       *result = (global_entry->u.def.value
7840 		 + global_entry->u.def.section->output_section->vma
7841 		 + global_entry->u.def.section->output_offset);
7842 #ifdef DEBUG
7843       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7844 	      global_entry->root.string, (unsigned long) *result);
7845 #endif
7846       return TRUE;
7847     }
7848 
7849   return FALSE;
7850 }
7851 
7852 /* Looks up NAME in SECTIONS.  If found sets RESULT to NAME's address (in
7853    bytes) and returns TRUE, otherwise returns FALSE.  Accepts pseudo-section
7854    names like "foo.end" which is the end address of section "foo".  */
7855 
7856 static bfd_boolean
7857 resolve_section (const char *name,
7858 		 asection *sections,
7859 		 bfd_vma *result,
7860 		 bfd * abfd)
7861 {
7862   asection *curr;
7863   unsigned int len;
7864 
7865   for (curr = sections; curr; curr = curr->next)
7866     if (strcmp (curr->name, name) == 0)
7867       {
7868 	*result = curr->vma;
7869 	return TRUE;
7870       }
7871 
7872   /* Hmm. still haven't found it. try pseudo-section names.  */
7873   /* FIXME: This could be coded more efficiently...  */
7874   for (curr = sections; curr; curr = curr->next)
7875     {
7876       len = strlen (curr->name);
7877       if (len > strlen (name))
7878 	continue;
7879 
7880       if (strncmp (curr->name, name, len) == 0)
7881 	{
7882 	  if (strncmp (".end", name + len, 4) == 0)
7883 	    {
7884 	      *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7885 	      return TRUE;
7886 	    }
7887 
7888 	  /* Insert more pseudo-section names here, if you like.  */
7889 	}
7890     }
7891 
7892   return FALSE;
7893 }
7894 
7895 static void
7896 undefined_reference (const char *reftype, const char *name)
7897 {
7898   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7899 		      reftype, name);
7900 }
7901 
7902 static bfd_boolean
7903 eval_symbol (bfd_vma *result,
7904 	     const char **symp,
7905 	     bfd *input_bfd,
7906 	     struct elf_final_link_info *flinfo,
7907 	     bfd_vma dot,
7908 	     Elf_Internal_Sym *isymbuf,
7909 	     size_t locsymcount,
7910 	     int signed_p)
7911 {
7912   size_t len;
7913   size_t symlen;
7914   bfd_vma a;
7915   bfd_vma b;
7916   char symbuf[4096];
7917   const char *sym = *symp;
7918   const char *symend;
7919   bfd_boolean symbol_is_section = FALSE;
7920 
7921   len = strlen (sym);
7922   symend = sym + len;
7923 
7924   if (len < 1 || len > sizeof (symbuf))
7925     {
7926       bfd_set_error (bfd_error_invalid_operation);
7927       return FALSE;
7928     }
7929 
7930   switch (* sym)
7931     {
7932     case '.':
7933       *result = dot;
7934       *symp = sym + 1;
7935       return TRUE;
7936 
7937     case '#':
7938       ++sym;
7939       *result = strtoul (sym, (char **) symp, 16);
7940       return TRUE;
7941 
7942     case 'S':
7943       symbol_is_section = TRUE;
7944     case 's':
7945       ++sym;
7946       symlen = strtol (sym, (char **) symp, 10);
7947       sym = *symp + 1; /* Skip the trailing ':'.  */
7948 
7949       if (symend < sym || symlen + 1 > sizeof (symbuf))
7950 	{
7951 	  bfd_set_error (bfd_error_invalid_operation);
7952 	  return FALSE;
7953 	}
7954 
7955       memcpy (symbuf, sym, symlen);
7956       symbuf[symlen] = '\0';
7957       *symp = sym + symlen;
7958 
7959       /* Is it always possible, with complex symbols, that gas "mis-guessed"
7960 	 the symbol as a section, or vice-versa. so we're pretty liberal in our
7961 	 interpretation here; section means "try section first", not "must be a
7962 	 section", and likewise with symbol.  */
7963 
7964       if (symbol_is_section)
7965 	{
7966 	  if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
7967 	      && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7968 				  isymbuf, locsymcount))
7969 	    {
7970 	      undefined_reference ("section", symbuf);
7971 	      return FALSE;
7972 	    }
7973 	}
7974       else
7975 	{
7976 	  if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7977 			       isymbuf, locsymcount)
7978 	      && !resolve_section (symbuf, flinfo->output_bfd->sections,
7979 				   result, input_bfd))
7980 	    {
7981 	      undefined_reference ("symbol", symbuf);
7982 	      return FALSE;
7983 	    }
7984 	}
7985 
7986       return TRUE;
7987 
7988       /* All that remains are operators.  */
7989 
7990 #define UNARY_OP(op)						\
7991   if (strncmp (sym, #op, strlen (#op)) == 0)			\
7992     {								\
7993       sym += strlen (#op);					\
7994       if (*sym == ':')						\
7995 	++sym;							\
7996       *symp = sym;						\
7997       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
7998 			isymbuf, locsymcount, signed_p))	\
7999 	return FALSE;						\
8000       if (signed_p)						\
8001 	*result = op ((bfd_signed_vma) a);			\
8002       else							\
8003 	*result = op a;						\
8004       return TRUE;						\
8005     }
8006 
8007 #define BINARY_OP(op)						\
8008   if (strncmp (sym, #op, strlen (#op)) == 0)			\
8009     {								\
8010       sym += strlen (#op);					\
8011       if (*sym == ':')						\
8012 	++sym;							\
8013       *symp = sym;						\
8014       if (!eval_symbol (&a, symp, input_bfd, flinfo, dot,	\
8015 			isymbuf, locsymcount, signed_p))	\
8016 	return FALSE;						\
8017       ++*symp;							\
8018       if (!eval_symbol (&b, symp, input_bfd, flinfo, dot,	\
8019 			isymbuf, locsymcount, signed_p))	\
8020 	return FALSE;						\
8021       if (signed_p)						\
8022 	*result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b);	\
8023       else							\
8024 	*result = a op b;					\
8025       return TRUE;						\
8026     }
8027 
8028     default:
8029       UNARY_OP  (0-);
8030       BINARY_OP (<<);
8031       BINARY_OP (>>);
8032       BINARY_OP (==);
8033       BINARY_OP (!=);
8034       BINARY_OP (<=);
8035       BINARY_OP (>=);
8036       BINARY_OP (&&);
8037       BINARY_OP (||);
8038       UNARY_OP  (~);
8039       UNARY_OP  (!);
8040       BINARY_OP (*);
8041       BINARY_OP (/);
8042       BINARY_OP (%);
8043       BINARY_OP (^);
8044       BINARY_OP (|);
8045       BINARY_OP (&);
8046       BINARY_OP (+);
8047       BINARY_OP (-);
8048       BINARY_OP (<);
8049       BINARY_OP (>);
8050 #undef UNARY_OP
8051 #undef BINARY_OP
8052       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8053       bfd_set_error (bfd_error_invalid_operation);
8054       return FALSE;
8055     }
8056 }
8057 
8058 static void
8059 put_value (bfd_vma size,
8060 	   unsigned long chunksz,
8061 	   bfd *input_bfd,
8062 	   bfd_vma x,
8063 	   bfd_byte *location)
8064 {
8065   location += (size - chunksz);
8066 
8067   for (; size; size -= chunksz, location -= chunksz)
8068     {
8069       switch (chunksz)
8070 	{
8071 	case 1:
8072 	  bfd_put_8 (input_bfd, x, location);
8073 	  x >>= 8;
8074 	  break;
8075 	case 2:
8076 	  bfd_put_16 (input_bfd, x, location);
8077 	  x >>= 16;
8078 	  break;
8079 	case 4:
8080 	  bfd_put_32 (input_bfd, x, location);
8081 	  /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
8082 	  x >>= 16;
8083 	  x >>= 16;
8084 	  break;
8085 #ifdef BFD64
8086 	case 8:
8087 	  bfd_put_64 (input_bfd, x, location);
8088 	  /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
8089 	  x >>= 32;
8090 	  x >>= 32;
8091 	  break;
8092 #endif
8093 	default:
8094 	  abort ();
8095 	  break;
8096 	}
8097     }
8098 }
8099 
8100 static bfd_vma
8101 get_value (bfd_vma size,
8102 	   unsigned long chunksz,
8103 	   bfd *input_bfd,
8104 	   bfd_byte *location)
8105 {
8106   int shift;
8107   bfd_vma x = 0;
8108 
8109   /* Sanity checks.  */
8110   BFD_ASSERT (chunksz <= sizeof (x)
8111 	      && size >= chunksz
8112 	      && chunksz != 0
8113 	      && (size % chunksz) == 0
8114 	      && input_bfd != NULL
8115 	      && location != NULL);
8116 
8117   if (chunksz == sizeof (x))
8118     {
8119       BFD_ASSERT (size == chunksz);
8120 
8121       /* Make sure that we do not perform an undefined shift operation.
8122 	 We know that size == chunksz so there will only be one iteration
8123 	 of the loop below.  */
8124       shift = 0;
8125     }
8126   else
8127     shift = 8 * chunksz;
8128 
8129   for (; size; size -= chunksz, location += chunksz)
8130     {
8131       switch (chunksz)
8132 	{
8133 	case 1:
8134 	  x = (x << shift) | bfd_get_8 (input_bfd, location);
8135 	  break;
8136 	case 2:
8137 	  x = (x << shift) | bfd_get_16 (input_bfd, location);
8138 	  break;
8139 	case 4:
8140 	  x = (x << shift) | bfd_get_32 (input_bfd, location);
8141 	  break;
8142 #ifdef BFD64
8143 	case 8:
8144 	  x = (x << shift) | bfd_get_64 (input_bfd, location);
8145 	  break;
8146 #endif
8147 	default:
8148 	  abort ();
8149 	}
8150     }
8151   return x;
8152 }
8153 
8154 static void
8155 decode_complex_addend (unsigned long *start,   /* in bits */
8156 		       unsigned long *oplen,   /* in bits */
8157 		       unsigned long *len,     /* in bits */
8158 		       unsigned long *wordsz,  /* in bytes */
8159 		       unsigned long *chunksz, /* in bytes */
8160 		       unsigned long *lsb0_p,
8161 		       unsigned long *signed_p,
8162 		       unsigned long *trunc_p,
8163 		       unsigned long encoded)
8164 {
8165   * start     =  encoded        & 0x3F;
8166   * len       = (encoded >>  6) & 0x3F;
8167   * oplen     = (encoded >> 12) & 0x3F;
8168   * wordsz    = (encoded >> 18) & 0xF;
8169   * chunksz   = (encoded >> 22) & 0xF;
8170   * lsb0_p    = (encoded >> 27) & 1;
8171   * signed_p  = (encoded >> 28) & 1;
8172   * trunc_p   = (encoded >> 29) & 1;
8173 }
8174 
8175 bfd_reloc_status_type
8176 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8177 				    asection *input_section ATTRIBUTE_UNUSED,
8178 				    bfd_byte *contents,
8179 				    Elf_Internal_Rela *rel,
8180 				    bfd_vma relocation)
8181 {
8182   bfd_vma shift, x, mask;
8183   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8184   bfd_reloc_status_type r;
8185 
8186   /*  Perform this reloc, since it is complex.
8187       (this is not to say that it necessarily refers to a complex
8188       symbol; merely that it is a self-describing CGEN based reloc.
8189       i.e. the addend has the complete reloc information (bit start, end,
8190       word size, etc) encoded within it.).  */
8191 
8192   decode_complex_addend (&start, &oplen, &len, &wordsz,
8193 			 &chunksz, &lsb0_p, &signed_p,
8194 			 &trunc_p, rel->r_addend);
8195 
8196   mask = (((1L << (len - 1)) - 1) << 1) | 1;
8197 
8198   if (lsb0_p)
8199     shift = (start + 1) - len;
8200   else
8201     shift = (8 * wordsz) - (start + len);
8202 
8203   x = get_value (wordsz, chunksz, input_bfd,
8204 		 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8205 
8206 #ifdef DEBUG
8207   printf ("Doing complex reloc: "
8208 	  "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8209 	  "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8210 	  "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8211 	  lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8212 	  oplen, (unsigned long) x, (unsigned long) mask,
8213 	  (unsigned long) relocation);
8214 #endif
8215 
8216   r = bfd_reloc_ok;
8217   if (! trunc_p)
8218     /* Now do an overflow check.  */
8219     r = bfd_check_overflow ((signed_p
8220 			     ? complain_overflow_signed
8221 			     : complain_overflow_unsigned),
8222 			    len, 0, (8 * wordsz),
8223 			    relocation);
8224 
8225   /* Do the deed.  */
8226   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8227 
8228 #ifdef DEBUG
8229   printf ("           relocation: %8.8lx\n"
8230 	  "         shifted mask: %8.8lx\n"
8231 	  " shifted/masked reloc: %8.8lx\n"
8232 	  "               result: %8.8lx\n",
8233 	  (unsigned long) relocation, (unsigned long) (mask << shift),
8234 	  (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8235 #endif
8236   put_value (wordsz, chunksz, input_bfd, x,
8237 	     contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8238   return r;
8239 }
8240 
8241 /* Functions to read r_offset from external (target order) reloc
8242    entry.  Faster than bfd_getl32 et al, because we let the compiler
8243    know the value is aligned.  */
8244 
8245 static bfd_vma
8246 ext32l_r_offset (const void *p)
8247 {
8248   union aligned32
8249   {
8250     uint32_t v;
8251     unsigned char c[4];
8252   };
8253   const union aligned32 *a
8254     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8255 
8256   uint32_t aval = (  (uint32_t) a->c[0]
8257 		   | (uint32_t) a->c[1] << 8
8258 		   | (uint32_t) a->c[2] << 16
8259 		   | (uint32_t) a->c[3] << 24);
8260   return aval;
8261 }
8262 
8263 static bfd_vma
8264 ext32b_r_offset (const void *p)
8265 {
8266   union aligned32
8267   {
8268     uint32_t v;
8269     unsigned char c[4];
8270   };
8271   const union aligned32 *a
8272     = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8273 
8274   uint32_t aval = (  (uint32_t) a->c[0] << 24
8275 		   | (uint32_t) a->c[1] << 16
8276 		   | (uint32_t) a->c[2] << 8
8277 		   | (uint32_t) a->c[3]);
8278   return aval;
8279 }
8280 
8281 #ifdef BFD_HOST_64_BIT
8282 static bfd_vma
8283 ext64l_r_offset (const void *p)
8284 {
8285   union aligned64
8286   {
8287     uint64_t v;
8288     unsigned char c[8];
8289   };
8290   const union aligned64 *a
8291     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8292 
8293   uint64_t aval = (  (uint64_t) a->c[0]
8294 		   | (uint64_t) a->c[1] << 8
8295 		   | (uint64_t) a->c[2] << 16
8296 		   | (uint64_t) a->c[3] << 24
8297 		   | (uint64_t) a->c[4] << 32
8298 		   | (uint64_t) a->c[5] << 40
8299 		   | (uint64_t) a->c[6] << 48
8300 		   | (uint64_t) a->c[7] << 56);
8301   return aval;
8302 }
8303 
8304 static bfd_vma
8305 ext64b_r_offset (const void *p)
8306 {
8307   union aligned64
8308   {
8309     uint64_t v;
8310     unsigned char c[8];
8311   };
8312   const union aligned64 *a
8313     = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8314 
8315   uint64_t aval = (  (uint64_t) a->c[0] << 56
8316 		   | (uint64_t) a->c[1] << 48
8317 		   | (uint64_t) a->c[2] << 40
8318 		   | (uint64_t) a->c[3] << 32
8319 		   | (uint64_t) a->c[4] << 24
8320 		   | (uint64_t) a->c[5] << 16
8321 		   | (uint64_t) a->c[6] << 8
8322 		   | (uint64_t) a->c[7]);
8323   return aval;
8324 }
8325 #endif
8326 
8327 /* When performing a relocatable link, the input relocations are
8328    preserved.  But, if they reference global symbols, the indices
8329    referenced must be updated.  Update all the relocations found in
8330    RELDATA.  */
8331 
8332 static bfd_boolean
8333 elf_link_adjust_relocs (bfd *abfd,
8334 			struct bfd_elf_section_reloc_data *reldata,
8335 			bfd_boolean sort)
8336 {
8337   unsigned int i;
8338   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8339   bfd_byte *erela;
8340   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8341   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8342   bfd_vma r_type_mask;
8343   int r_sym_shift;
8344   unsigned int count = reldata->count;
8345   struct elf_link_hash_entry **rel_hash = reldata->hashes;
8346 
8347   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8348     {
8349       swap_in = bed->s->swap_reloc_in;
8350       swap_out = bed->s->swap_reloc_out;
8351     }
8352   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8353     {
8354       swap_in = bed->s->swap_reloca_in;
8355       swap_out = bed->s->swap_reloca_out;
8356     }
8357   else
8358     abort ();
8359 
8360   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8361     abort ();
8362 
8363   if (bed->s->arch_size == 32)
8364     {
8365       r_type_mask = 0xff;
8366       r_sym_shift = 8;
8367     }
8368   else
8369     {
8370       r_type_mask = 0xffffffff;
8371       r_sym_shift = 32;
8372     }
8373 
8374   erela = reldata->hdr->contents;
8375   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8376     {
8377       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8378       unsigned int j;
8379 
8380       if (*rel_hash == NULL)
8381 	continue;
8382 
8383       BFD_ASSERT ((*rel_hash)->indx >= 0);
8384 
8385       (*swap_in) (abfd, erela, irela);
8386       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8387 	irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8388 			   | (irela[j].r_info & r_type_mask));
8389       (*swap_out) (abfd, irela, erela);
8390     }
8391 
8392   if (sort && count != 0)
8393     {
8394       bfd_vma (*ext_r_off) (const void *);
8395       bfd_vma r_off;
8396       size_t elt_size;
8397       bfd_byte *base, *end, *p, *loc;
8398       bfd_byte *buf = NULL;
8399 
8400       if (bed->s->arch_size == 32)
8401 	{
8402 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8403 	    ext_r_off = ext32l_r_offset;
8404 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8405 	    ext_r_off = ext32b_r_offset;
8406 	  else
8407 	    abort ();
8408 	}
8409       else
8410 	{
8411 #ifdef BFD_HOST_64_BIT
8412 	  if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8413 	    ext_r_off = ext64l_r_offset;
8414 	  else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8415 	    ext_r_off = ext64b_r_offset;
8416 	  else
8417 #endif
8418 	    abort ();
8419 	}
8420 
8421       /*  Must use a stable sort here.  A modified insertion sort,
8422 	  since the relocs are mostly sorted already.  */
8423       elt_size = reldata->hdr->sh_entsize;
8424       base = reldata->hdr->contents;
8425       end = base + count * elt_size;
8426       if (elt_size > sizeof (Elf64_External_Rela))
8427 	abort ();
8428 
8429       /* Ensure the first element is lowest.  This acts as a sentinel,
8430 	 speeding the main loop below.  */
8431       r_off = (*ext_r_off) (base);
8432       for (p = loc = base; (p += elt_size) < end; )
8433 	{
8434 	  bfd_vma r_off2 = (*ext_r_off) (p);
8435 	  if (r_off > r_off2)
8436 	    {
8437 	      r_off = r_off2;
8438 	      loc = p;
8439 	    }
8440 	}
8441       if (loc != base)
8442 	{
8443 	  /* Don't just swap *base and *loc as that changes the order
8444 	     of the original base[0] and base[1] if they happen to
8445 	     have the same r_offset.  */
8446 	  bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8447 	  memcpy (onebuf, loc, elt_size);
8448 	  memmove (base + elt_size, base, loc - base);
8449 	  memcpy (base, onebuf, elt_size);
8450 	}
8451 
8452       for (p = base + elt_size; (p += elt_size) < end; )
8453 	{
8454 	  /* base to p is sorted, *p is next to insert.  */
8455 	  r_off = (*ext_r_off) (p);
8456 	  /* Search the sorted region for location to insert.  */
8457 	  loc = p - elt_size;
8458 	  while (r_off < (*ext_r_off) (loc))
8459 	    loc -= elt_size;
8460 	  loc += elt_size;
8461 	  if (loc != p)
8462 	    {
8463 	      /* Chances are there is a run of relocs to insert here,
8464 		 from one of more input files.  Files are not always
8465 		 linked in order due to the way elf_link_input_bfd is
8466 		 called.  See pr17666.  */
8467 	      size_t sortlen = p - loc;
8468 	      bfd_vma r_off2 = (*ext_r_off) (loc);
8469 	      size_t runlen = elt_size;
8470 	      size_t buf_size = 96 * 1024;
8471 	      while (p + runlen < end
8472 		     && (sortlen <= buf_size
8473 			 || runlen + elt_size <= buf_size)
8474 		     && r_off2 > (*ext_r_off) (p + runlen))
8475 		runlen += elt_size;
8476 	      if (buf == NULL)
8477 		{
8478 		  buf = bfd_malloc (buf_size);
8479 		  if (buf == NULL)
8480 		    return FALSE;
8481 		}
8482 	      if (runlen < sortlen)
8483 		{
8484 		  memcpy (buf, p, runlen);
8485 		  memmove (loc + runlen, loc, sortlen);
8486 		  memcpy (loc, buf, runlen);
8487 		}
8488 	      else
8489 		{
8490 		  memcpy (buf, loc, sortlen);
8491 		  memmove (loc, p, runlen);
8492 		  memcpy (loc + runlen, buf, sortlen);
8493 		}
8494 	      p += runlen - elt_size;
8495 	    }
8496 	}
8497       /* Hashes are no longer valid.  */
8498       free (reldata->hashes);
8499       reldata->hashes = NULL;
8500       free (buf);
8501     }
8502   return TRUE;
8503 }
8504 
8505 struct elf_link_sort_rela
8506 {
8507   union {
8508     bfd_vma offset;
8509     bfd_vma sym_mask;
8510   } u;
8511   enum elf_reloc_type_class type;
8512   /* We use this as an array of size int_rels_per_ext_rel.  */
8513   Elf_Internal_Rela rela[1];
8514 };
8515 
8516 static int
8517 elf_link_sort_cmp1 (const void *A, const void *B)
8518 {
8519   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8520   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8521   int relativea, relativeb;
8522 
8523   relativea = a->type == reloc_class_relative;
8524   relativeb = b->type == reloc_class_relative;
8525 
8526   if (relativea < relativeb)
8527     return 1;
8528   if (relativea > relativeb)
8529     return -1;
8530   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8531     return -1;
8532   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8533     return 1;
8534   if (a->rela->r_offset < b->rela->r_offset)
8535     return -1;
8536   if (a->rela->r_offset > b->rela->r_offset)
8537     return 1;
8538   return 0;
8539 }
8540 
8541 static int
8542 elf_link_sort_cmp2 (const void *A, const void *B)
8543 {
8544   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8545   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8546 
8547   if (a->type < b->type)
8548     return -1;
8549   if (a->type > b->type)
8550     return 1;
8551   if (a->u.offset < b->u.offset)
8552     return -1;
8553   if (a->u.offset > b->u.offset)
8554     return 1;
8555   if (a->rela->r_offset < b->rela->r_offset)
8556     return -1;
8557   if (a->rela->r_offset > b->rela->r_offset)
8558     return 1;
8559   return 0;
8560 }
8561 
8562 static size_t
8563 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8564 {
8565   asection *dynamic_relocs;
8566   asection *rela_dyn;
8567   asection *rel_dyn;
8568   bfd_size_type count, size;
8569   size_t i, ret, sort_elt, ext_size;
8570   bfd_byte *sort, *s_non_relative, *p;
8571   struct elf_link_sort_rela *sq;
8572   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8573   int i2e = bed->s->int_rels_per_ext_rel;
8574   unsigned int opb = bfd_octets_per_byte (abfd);
8575   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8576   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8577   struct bfd_link_order *lo;
8578   bfd_vma r_sym_mask;
8579   bfd_boolean use_rela;
8580 
8581   /* Find a dynamic reloc section.  */
8582   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8583   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
8584   if (rela_dyn != NULL && rela_dyn->size > 0
8585       && rel_dyn != NULL && rel_dyn->size > 0)
8586     {
8587       bfd_boolean use_rela_initialised = FALSE;
8588 
8589       /* This is just here to stop gcc from complaining.
8590 	 Its initialization checking code is not perfect.  */
8591       use_rela = TRUE;
8592 
8593       /* Both sections are present.  Examine the sizes
8594 	 of the indirect sections to help us choose.  */
8595       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8596 	if (lo->type == bfd_indirect_link_order)
8597 	  {
8598 	    asection *o = lo->u.indirect.section;
8599 
8600 	    if ((o->size % bed->s->sizeof_rela) == 0)
8601 	      {
8602 		if ((o->size % bed->s->sizeof_rel) == 0)
8603 		  /* Section size is divisible by both rel and rela sizes.
8604 		     It is of no help to us.  */
8605 		  ;
8606 		else
8607 		  {
8608 		    /* Section size is only divisible by rela.  */
8609 		    if (use_rela_initialised && (use_rela == FALSE))
8610 		      {
8611 			_bfd_error_handler (_("%B: Unable to sort relocs - "
8612 					      "they are in more than one size"),
8613 					    abfd);
8614 			bfd_set_error (bfd_error_invalid_operation);
8615 			return 0;
8616 		      }
8617 		    else
8618 		      {
8619 			use_rela = TRUE;
8620 			use_rela_initialised = TRUE;
8621 		      }
8622 		  }
8623 	      }
8624 	    else if ((o->size % bed->s->sizeof_rel) == 0)
8625 	      {
8626 		/* Section size is only divisible by rel.  */
8627 		if (use_rela_initialised && (use_rela == TRUE))
8628 		  {
8629 		    _bfd_error_handler (_("%B: Unable to sort relocs - "
8630 					  "they are in more than one size"),
8631 					abfd);
8632 		    bfd_set_error (bfd_error_invalid_operation);
8633 		    return 0;
8634 		  }
8635 		else
8636 		  {
8637 		    use_rela = FALSE;
8638 		    use_rela_initialised = TRUE;
8639 		  }
8640 	      }
8641 	    else
8642 	      {
8643 		/* The section size is not divisible by either -
8644 		   something is wrong.  */
8645 		_bfd_error_handler (_("%B: Unable to sort relocs - "
8646 				      "they are of an unknown size"), abfd);
8647 		bfd_set_error (bfd_error_invalid_operation);
8648 		return 0;
8649 	      }
8650 	  }
8651 
8652       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8653 	if (lo->type == bfd_indirect_link_order)
8654 	  {
8655 	    asection *o = lo->u.indirect.section;
8656 
8657 	    if ((o->size % bed->s->sizeof_rela) == 0)
8658 	      {
8659 		if ((o->size % bed->s->sizeof_rel) == 0)
8660 		  /* Section size is divisible by both rel and rela sizes.
8661 		     It is of no help to us.  */
8662 		  ;
8663 		else
8664 		  {
8665 		    /* Section size is only divisible by rela.  */
8666 		    if (use_rela_initialised && (use_rela == FALSE))
8667 		      {
8668 			_bfd_error_handler (_("%B: Unable to sort relocs - "
8669 					      "they are in more than one size"),
8670 					    abfd);
8671 			bfd_set_error (bfd_error_invalid_operation);
8672 			return 0;
8673 		      }
8674 		    else
8675 		      {
8676 			use_rela = TRUE;
8677 			use_rela_initialised = TRUE;
8678 		      }
8679 		  }
8680 	      }
8681 	    else if ((o->size % bed->s->sizeof_rel) == 0)
8682 	      {
8683 		/* Section size is only divisible by rel.  */
8684 		if (use_rela_initialised && (use_rela == TRUE))
8685 		  {
8686 		    _bfd_error_handler (_("%B: Unable to sort relocs - "
8687 					  "they are in more than one size"),
8688 					abfd);
8689 		    bfd_set_error (bfd_error_invalid_operation);
8690 		    return 0;
8691 		  }
8692 		else
8693 		  {
8694 		    use_rela = FALSE;
8695 		    use_rela_initialised = TRUE;
8696 		  }
8697 	      }
8698 	    else
8699 	      {
8700 		/* The section size is not divisible by either -
8701 		   something is wrong.  */
8702 		_bfd_error_handler (_("%B: Unable to sort relocs - "
8703 				      "they are of an unknown size"), abfd);
8704 		bfd_set_error (bfd_error_invalid_operation);
8705 		return 0;
8706 	      }
8707 	  }
8708 
8709       if (! use_rela_initialised)
8710 	/* Make a guess.  */
8711 	use_rela = TRUE;
8712     }
8713   else if (rela_dyn != NULL && rela_dyn->size > 0)
8714     use_rela = TRUE;
8715   else if (rel_dyn != NULL && rel_dyn->size > 0)
8716     use_rela = FALSE;
8717   else
8718     return 0;
8719 
8720   if (use_rela)
8721     {
8722       dynamic_relocs = rela_dyn;
8723       ext_size = bed->s->sizeof_rela;
8724       swap_in = bed->s->swap_reloca_in;
8725       swap_out = bed->s->swap_reloca_out;
8726     }
8727   else
8728     {
8729       dynamic_relocs = rel_dyn;
8730       ext_size = bed->s->sizeof_rel;
8731       swap_in = bed->s->swap_reloc_in;
8732       swap_out = bed->s->swap_reloc_out;
8733     }
8734 
8735   size = 0;
8736   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8737     if (lo->type == bfd_indirect_link_order)
8738       size += lo->u.indirect.section->size;
8739 
8740   if (size != dynamic_relocs->size)
8741     return 0;
8742 
8743   sort_elt = (sizeof (struct elf_link_sort_rela)
8744 	      + (i2e - 1) * sizeof (Elf_Internal_Rela));
8745 
8746   count = dynamic_relocs->size / ext_size;
8747   if (count == 0)
8748     return 0;
8749   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8750 
8751   if (sort == NULL)
8752     {
8753       (*info->callbacks->warning)
8754 	(info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8755       return 0;
8756     }
8757 
8758   if (bed->s->arch_size == 32)
8759     r_sym_mask = ~(bfd_vma) 0xff;
8760   else
8761     r_sym_mask = ~(bfd_vma) 0xffffffff;
8762 
8763   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8764     if (lo->type == bfd_indirect_link_order)
8765       {
8766 	bfd_byte *erel, *erelend;
8767 	asection *o = lo->u.indirect.section;
8768 
8769 	if (o->contents == NULL && o->size != 0)
8770 	  {
8771 	    /* This is a reloc section that is being handled as a normal
8772 	       section.  See bfd_section_from_shdr.  We can't combine
8773 	       relocs in this case.  */
8774 	    free (sort);
8775 	    return 0;
8776 	  }
8777 	erel = o->contents;
8778 	erelend = o->contents + o->size;
8779 	p = sort + o->output_offset * opb / ext_size * sort_elt;
8780 
8781 	while (erel < erelend)
8782 	  {
8783 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8784 
8785 	    (*swap_in) (abfd, erel, s->rela);
8786 	    s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8787 	    s->u.sym_mask = r_sym_mask;
8788 	    p += sort_elt;
8789 	    erel += ext_size;
8790 	  }
8791       }
8792 
8793   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8794 
8795   for (i = 0, p = sort; i < count; i++, p += sort_elt)
8796     {
8797       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8798       if (s->type != reloc_class_relative)
8799 	break;
8800     }
8801   ret = i;
8802   s_non_relative = p;
8803 
8804   sq = (struct elf_link_sort_rela *) s_non_relative;
8805   for (; i < count; i++, p += sort_elt)
8806     {
8807       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8808       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8809 	sq = sp;
8810       sp->u.offset = sq->rela->r_offset;
8811     }
8812 
8813   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8814 
8815   struct elf_link_hash_table *htab = elf_hash_table (info);
8816   if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8817     {
8818       /* We have plt relocs in .rela.dyn.  */
8819       sq = (struct elf_link_sort_rela *) sort;
8820       for (i = 0; i < count; i++)
8821 	if (sq[count - i - 1].type != reloc_class_plt)
8822 	  break;
8823       if (i != 0 && htab->srelplt->size == i * ext_size)
8824 	{
8825 	  struct bfd_link_order **plo;
8826 	  /* Put srelplt link_order last.  This is so the output_offset
8827 	     set in the next loop is correct for DT_JMPREL.  */
8828 	  for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8829 	    if ((*plo)->type == bfd_indirect_link_order
8830 		&& (*plo)->u.indirect.section == htab->srelplt)
8831 	      {
8832 		lo = *plo;
8833 		*plo = lo->next;
8834 	      }
8835 	    else
8836 	      plo = &(*plo)->next;
8837 	  *plo = lo;
8838 	  lo->next = NULL;
8839 	  dynamic_relocs->map_tail.link_order = lo;
8840 	}
8841     }
8842 
8843   p = sort;
8844   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8845     if (lo->type == bfd_indirect_link_order)
8846       {
8847 	bfd_byte *erel, *erelend;
8848 	asection *o = lo->u.indirect.section;
8849 
8850 	erel = o->contents;
8851 	erelend = o->contents + o->size;
8852 	o->output_offset = (p - sort) / sort_elt * ext_size / opb;
8853 	while (erel < erelend)
8854 	  {
8855 	    struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8856 	    (*swap_out) (abfd, s->rela, erel);
8857 	    p += sort_elt;
8858 	    erel += ext_size;
8859 	  }
8860       }
8861 
8862   free (sort);
8863   *psec = dynamic_relocs;
8864   return ret;
8865 }
8866 
8867 /* Add a symbol to the output symbol string table.  */
8868 
8869 static int
8870 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8871 			   const char *name,
8872 			   Elf_Internal_Sym *elfsym,
8873 			   asection *input_sec,
8874 			   struct elf_link_hash_entry *h)
8875 {
8876   int (*output_symbol_hook)
8877     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8878      struct elf_link_hash_entry *);
8879   struct elf_link_hash_table *hash_table;
8880   const struct elf_backend_data *bed;
8881   bfd_size_type strtabsize;
8882 
8883   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8884 
8885   bed = get_elf_backend_data (flinfo->output_bfd);
8886   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8887   if (output_symbol_hook != NULL)
8888     {
8889       int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8890       if (ret != 1)
8891 	return ret;
8892     }
8893 
8894   if (name == NULL
8895       || *name == '\0'
8896       || (input_sec->flags & SEC_EXCLUDE))
8897     elfsym->st_name = (unsigned long) -1;
8898   else
8899     {
8900       /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8901 	 to get the final offset for st_name.  */
8902       elfsym->st_name
8903 	= (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8904 					       name, FALSE);
8905       if (elfsym->st_name == (unsigned long) -1)
8906 	return 0;
8907     }
8908 
8909   hash_table = elf_hash_table (flinfo->info);
8910   strtabsize = hash_table->strtabsize;
8911   if (strtabsize <= hash_table->strtabcount)
8912     {
8913       strtabsize += strtabsize;
8914       hash_table->strtabsize = strtabsize;
8915       strtabsize *= sizeof (*hash_table->strtab);
8916       hash_table->strtab
8917 	= (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8918 						 strtabsize);
8919       if (hash_table->strtab == NULL)
8920 	return 0;
8921     }
8922   hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8923   hash_table->strtab[hash_table->strtabcount].dest_index
8924     = hash_table->strtabcount;
8925   hash_table->strtab[hash_table->strtabcount].destshndx_index
8926     = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8927 
8928   bfd_get_symcount (flinfo->output_bfd) += 1;
8929   hash_table->strtabcount += 1;
8930 
8931   return 1;
8932 }
8933 
8934 /* Swap symbols out to the symbol table and flush the output symbols to
8935    the file.  */
8936 
8937 static bfd_boolean
8938 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8939 {
8940   struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8941   bfd_size_type amt;
8942   size_t i;
8943   const struct elf_backend_data *bed;
8944   bfd_byte *symbuf;
8945   Elf_Internal_Shdr *hdr;
8946   file_ptr pos;
8947   bfd_boolean ret;
8948 
8949   if (!hash_table->strtabcount)
8950     return TRUE;
8951 
8952   BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8953 
8954   bed = get_elf_backend_data (flinfo->output_bfd);
8955 
8956   amt = bed->s->sizeof_sym * hash_table->strtabcount;
8957   symbuf = (bfd_byte *) bfd_malloc (amt);
8958   if (symbuf == NULL)
8959     return FALSE;
8960 
8961   if (flinfo->symshndxbuf)
8962     {
8963       amt = sizeof (Elf_External_Sym_Shndx);
8964       amt *= bfd_get_symcount (flinfo->output_bfd);
8965       flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8966       if (flinfo->symshndxbuf == NULL)
8967 	{
8968 	  free (symbuf);
8969 	  return FALSE;
8970 	}
8971     }
8972 
8973   for (i = 0; i < hash_table->strtabcount; i++)
8974     {
8975       struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8976       if (elfsym->sym.st_name == (unsigned long) -1)
8977 	elfsym->sym.st_name = 0;
8978       else
8979 	elfsym->sym.st_name
8980 	  = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8981 						    elfsym->sym.st_name);
8982       bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8983 			       ((bfd_byte *) symbuf
8984 				+ (elfsym->dest_index
8985 				   * bed->s->sizeof_sym)),
8986 			       (flinfo->symshndxbuf
8987 				+ elfsym->destshndx_index));
8988     }
8989 
8990   hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8991   pos = hdr->sh_offset + hdr->sh_size;
8992   amt = hash_table->strtabcount * bed->s->sizeof_sym;
8993   if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8994       && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8995     {
8996       hdr->sh_size += amt;
8997       ret = TRUE;
8998     }
8999   else
9000     ret = FALSE;
9001 
9002   free (symbuf);
9003 
9004   free (hash_table->strtab);
9005   hash_table->strtab = NULL;
9006 
9007   return ret;
9008 }
9009 
9010 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
9011 
9012 static bfd_boolean
9013 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9014 {
9015   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9016       && sym->st_shndx < SHN_LORESERVE)
9017     {
9018       /* The gABI doesn't support dynamic symbols in output sections
9019 	 beyond 64k.  */
9020       (*_bfd_error_handler)
9021 	(_("%B: Too many sections: %d (>= %d)"),
9022 	 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9023       bfd_set_error (bfd_error_nonrepresentable_section);
9024       return FALSE;
9025     }
9026   return TRUE;
9027 }
9028 
9029 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9030    allowing an unsatisfied unversioned symbol in the DSO to match a
9031    versioned symbol that would normally require an explicit version.
9032    We also handle the case that a DSO references a hidden symbol
9033    which may be satisfied by a versioned symbol in another DSO.  */
9034 
9035 static bfd_boolean
9036 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9037 				 const struct elf_backend_data *bed,
9038 				 struct elf_link_hash_entry *h)
9039 {
9040   bfd *abfd;
9041   struct elf_link_loaded_list *loaded;
9042 
9043   if (!is_elf_hash_table (info->hash))
9044     return FALSE;
9045 
9046   /* Check indirect symbol.  */
9047   while (h->root.type == bfd_link_hash_indirect)
9048     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9049 
9050   switch (h->root.type)
9051     {
9052     default:
9053       abfd = NULL;
9054       break;
9055 
9056     case bfd_link_hash_undefined:
9057     case bfd_link_hash_undefweak:
9058       abfd = h->root.u.undef.abfd;
9059       if (abfd == NULL
9060 	  || (abfd->flags & DYNAMIC) == 0
9061 	  || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9062 	return FALSE;
9063       break;
9064 
9065     case bfd_link_hash_defined:
9066     case bfd_link_hash_defweak:
9067       abfd = h->root.u.def.section->owner;
9068       break;
9069 
9070     case bfd_link_hash_common:
9071       abfd = h->root.u.c.p->section->owner;
9072       break;
9073     }
9074   BFD_ASSERT (abfd != NULL);
9075 
9076   for (loaded = elf_hash_table (info)->loaded;
9077        loaded != NULL;
9078        loaded = loaded->next)
9079     {
9080       bfd *input;
9081       Elf_Internal_Shdr *hdr;
9082       size_t symcount;
9083       size_t extsymcount;
9084       size_t extsymoff;
9085       Elf_Internal_Shdr *versymhdr;
9086       Elf_Internal_Sym *isym;
9087       Elf_Internal_Sym *isymend;
9088       Elf_Internal_Sym *isymbuf;
9089       Elf_External_Versym *ever;
9090       Elf_External_Versym *extversym;
9091 
9092       input = loaded->abfd;
9093 
9094       /* We check each DSO for a possible hidden versioned definition.  */
9095       if (input == abfd
9096 	  || (input->flags & DYNAMIC) == 0
9097 	  || elf_dynversym (input) == 0)
9098 	continue;
9099 
9100       hdr = &elf_tdata (input)->dynsymtab_hdr;
9101 
9102       symcount = hdr->sh_size / bed->s->sizeof_sym;
9103       if (elf_bad_symtab (input))
9104 	{
9105 	  extsymcount = symcount;
9106 	  extsymoff = 0;
9107 	}
9108       else
9109 	{
9110 	  extsymcount = symcount - hdr->sh_info;
9111 	  extsymoff = hdr->sh_info;
9112 	}
9113 
9114       if (extsymcount == 0)
9115 	continue;
9116 
9117       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9118 				      NULL, NULL, NULL);
9119       if (isymbuf == NULL)
9120 	return FALSE;
9121 
9122       /* Read in any version definitions.  */
9123       versymhdr = &elf_tdata (input)->dynversym_hdr;
9124       extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9125       if (extversym == NULL)
9126 	goto error_ret;
9127 
9128       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9129 	  || (bfd_bread (extversym, versymhdr->sh_size, input)
9130 	      != versymhdr->sh_size))
9131 	{
9132 	  free (extversym);
9133 	error_ret:
9134 	  free (isymbuf);
9135 	  return FALSE;
9136 	}
9137 
9138       ever = extversym + extsymoff;
9139       isymend = isymbuf + extsymcount;
9140       for (isym = isymbuf; isym < isymend; isym++, ever++)
9141 	{
9142 	  const char *name;
9143 	  Elf_Internal_Versym iver;
9144 	  unsigned short version_index;
9145 
9146 	  if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9147 	      || isym->st_shndx == SHN_UNDEF)
9148 	    continue;
9149 
9150 	  name = bfd_elf_string_from_elf_section (input,
9151 						  hdr->sh_link,
9152 						  isym->st_name);
9153 	  if (strcmp (name, h->root.root.string) != 0)
9154 	    continue;
9155 
9156 	  _bfd_elf_swap_versym_in (input, ever, &iver);
9157 
9158 	  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9159 	      && !(h->def_regular
9160 		   && h->forced_local))
9161 	    {
9162 	      /* If we have a non-hidden versioned sym, then it should
9163 		 have provided a definition for the undefined sym unless
9164 		 it is defined in a non-shared object and forced local.
9165 	       */
9166 	      abort ();
9167 	    }
9168 
9169 	  version_index = iver.vs_vers & VERSYM_VERSION;
9170 	  if (version_index == 1 || version_index == 2)
9171 	    {
9172 	      /* This is the base or first version.  We can use it.  */
9173 	      free (extversym);
9174 	      free (isymbuf);
9175 	      return TRUE;
9176 	    }
9177 	}
9178 
9179       free (extversym);
9180       free (isymbuf);
9181     }
9182 
9183   return FALSE;
9184 }
9185 
9186 /* Convert ELF common symbol TYPE.  */
9187 
9188 static int
9189 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9190 {
9191   /* Commom symbol can only appear in relocatable link.  */
9192   if (!bfd_link_relocatable (info))
9193     abort ();
9194   switch (info->elf_stt_common)
9195     {
9196     case unchanged:
9197       break;
9198     case elf_stt_common:
9199       type = STT_COMMON;
9200       break;
9201     case no_elf_stt_common:
9202       type = STT_OBJECT;
9203       break;
9204     }
9205   return type;
9206 }
9207 
9208 /* Add an external symbol to the symbol table.  This is called from
9209    the hash table traversal routine.  When generating a shared object,
9210    we go through the symbol table twice.  The first time we output
9211    anything that might have been forced to local scope in a version
9212    script.  The second time we output the symbols that are still
9213    global symbols.  */
9214 
9215 static bfd_boolean
9216 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9217 {
9218   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9219   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9220   struct elf_final_link_info *flinfo = eoinfo->flinfo;
9221   bfd_boolean strip;
9222   Elf_Internal_Sym sym;
9223   asection *input_sec;
9224   const struct elf_backend_data *bed;
9225   long indx;
9226   int ret;
9227   unsigned int type;
9228   /* A symbol is bound locally if it is forced local or it is locally
9229      defined, hidden versioned, not referenced by shared library and
9230      not exported when linking executable.  */
9231   bfd_boolean local_bind = (h->forced_local
9232 			    || (bfd_link_executable (flinfo->info)
9233 				&& !flinfo->info->export_dynamic
9234 				&& !h->dynamic
9235 				&& !h->ref_dynamic
9236 				&& h->def_regular
9237 				&& h->versioned == versioned_hidden));
9238 
9239   if (h->root.type == bfd_link_hash_warning)
9240     {
9241       h = (struct elf_link_hash_entry *) h->root.u.i.link;
9242       if (h->root.type == bfd_link_hash_new)
9243 	return TRUE;
9244     }
9245 
9246   /* Decide whether to output this symbol in this pass.  */
9247   if (eoinfo->localsyms)
9248     {
9249       if (!local_bind)
9250 	return TRUE;
9251     }
9252   else
9253     {
9254       if (local_bind)
9255 	return TRUE;
9256     }
9257 
9258   bed = get_elf_backend_data (flinfo->output_bfd);
9259 
9260   if (h->root.type == bfd_link_hash_undefined)
9261     {
9262       /* If we have an undefined symbol reference here then it must have
9263 	 come from a shared library that is being linked in.  (Undefined
9264 	 references in regular files have already been handled unless
9265 	 they are in unreferenced sections which are removed by garbage
9266 	 collection).  */
9267       bfd_boolean ignore_undef = FALSE;
9268 
9269       /* Some symbols may be special in that the fact that they're
9270 	 undefined can be safely ignored - let backend determine that.  */
9271       if (bed->elf_backend_ignore_undef_symbol)
9272 	ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9273 
9274       /* If we are reporting errors for this situation then do so now.  */
9275       if (!ignore_undef
9276 	  && h->ref_dynamic
9277 	  && (!h->ref_regular || flinfo->info->gc_sections)
9278 	  && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9279 	  && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9280 	(*flinfo->info->callbacks->undefined_symbol)
9281 	  (flinfo->info, h->root.root.string,
9282 	   h->ref_regular ? NULL : h->root.u.undef.abfd,
9283 	   NULL, 0,
9284 	   flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9285 
9286       /* Strip a global symbol defined in a discarded section.  */
9287       if (h->indx == -3)
9288 	return TRUE;
9289     }
9290 
9291   /* We should also warn if a forced local symbol is referenced from
9292      shared libraries.  */
9293   if (bfd_link_executable (flinfo->info)
9294       && h->forced_local
9295       && h->ref_dynamic
9296       && h->def_regular
9297       && !h->dynamic_def
9298       && h->ref_dynamic_nonweak
9299       && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9300     {
9301       bfd *def_bfd;
9302       const char *msg;
9303       struct elf_link_hash_entry *hi = h;
9304 
9305       /* Check indirect symbol.  */
9306       while (hi->root.type == bfd_link_hash_indirect)
9307 	hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9308 
9309       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9310 	msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9311       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9312 	msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9313       else
9314 	msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9315       def_bfd = flinfo->output_bfd;
9316       if (hi->root.u.def.section != bfd_abs_section_ptr)
9317 	def_bfd = hi->root.u.def.section->owner;
9318       (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9319 			     h->root.root.string);
9320       bfd_set_error (bfd_error_bad_value);
9321       eoinfo->failed = TRUE;
9322       return FALSE;
9323     }
9324 
9325   /* We don't want to output symbols that have never been mentioned by
9326      a regular file, or that we have been told to strip.  However, if
9327      h->indx is set to -2, the symbol is used by a reloc and we must
9328      output it.  */
9329   strip = FALSE;
9330   if (h->indx == -2)
9331     ;
9332   else if ((h->def_dynamic
9333 	    || h->ref_dynamic
9334 	    || h->root.type == bfd_link_hash_new)
9335 	   && !h->def_regular
9336 	   && !h->ref_regular)
9337     strip = TRUE;
9338   else if (flinfo->info->strip == strip_all)
9339     strip = TRUE;
9340   else if (flinfo->info->strip == strip_some
9341 	   && bfd_hash_lookup (flinfo->info->keep_hash,
9342 			       h->root.root.string, FALSE, FALSE) == NULL)
9343     strip = TRUE;
9344   else if ((h->root.type == bfd_link_hash_defined
9345 	    || h->root.type == bfd_link_hash_defweak)
9346 	   && ((flinfo->info->strip_discarded
9347 		&& discarded_section (h->root.u.def.section))
9348 	       || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9349 		   && h->root.u.def.section->owner != NULL
9350 		   && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9351     strip = TRUE;
9352   else if ((h->root.type == bfd_link_hash_undefined
9353 	    || h->root.type == bfd_link_hash_undefweak)
9354 	   && h->root.u.undef.abfd != NULL
9355 	   && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9356     strip = TRUE;
9357 
9358   type = h->type;
9359 
9360   /* If we're stripping it, and it's not a dynamic symbol, there's
9361      nothing else to do.   However, if it is a forced local symbol or
9362      an ifunc symbol we need to give the backend finish_dynamic_symbol
9363      function a chance to make it dynamic.  */
9364   if (strip
9365       && h->dynindx == -1
9366       && type != STT_GNU_IFUNC
9367       && !h->forced_local)
9368     return TRUE;
9369 
9370   sym.st_value = 0;
9371   sym.st_size = h->size;
9372   sym.st_other = h->other;
9373   switch (h->root.type)
9374     {
9375     default:
9376     case bfd_link_hash_new:
9377     case bfd_link_hash_warning:
9378       abort ();
9379       return FALSE;
9380 
9381     case bfd_link_hash_undefined:
9382     case bfd_link_hash_undefweak:
9383       input_sec = bfd_und_section_ptr;
9384       sym.st_shndx = SHN_UNDEF;
9385       break;
9386 
9387     case bfd_link_hash_defined:
9388     case bfd_link_hash_defweak:
9389       {
9390 	input_sec = h->root.u.def.section;
9391 	if (input_sec->output_section != NULL)
9392 	  {
9393 	    sym.st_shndx =
9394 	      _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9395 						 input_sec->output_section);
9396 	    if (sym.st_shndx == SHN_BAD)
9397 	      {
9398 		(*_bfd_error_handler)
9399 		  (_("%B: could not find output section %A for input section %A"),
9400 		   flinfo->output_bfd, input_sec->output_section, input_sec);
9401 		bfd_set_error (bfd_error_nonrepresentable_section);
9402 		eoinfo->failed = TRUE;
9403 		return FALSE;
9404 	      }
9405 
9406 	    /* ELF symbols in relocatable files are section relative,
9407 	       but in nonrelocatable files they are virtual
9408 	       addresses.  */
9409 	    sym.st_value = h->root.u.def.value + input_sec->output_offset;
9410 	    if (!bfd_link_relocatable (flinfo->info))
9411 	      {
9412 		sym.st_value += input_sec->output_section->vma;
9413 		if (h->type == STT_TLS)
9414 		  {
9415 		    asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9416 		    if (tls_sec != NULL)
9417 		      sym.st_value -= tls_sec->vma;
9418 		  }
9419 	      }
9420 	  }
9421 	else
9422 	  {
9423 	    BFD_ASSERT (input_sec->owner == NULL
9424 			|| (input_sec->owner->flags & DYNAMIC) != 0);
9425 	    sym.st_shndx = SHN_UNDEF;
9426 	    input_sec = bfd_und_section_ptr;
9427 	  }
9428       }
9429       break;
9430 
9431     case bfd_link_hash_common:
9432       input_sec = h->root.u.c.p->section;
9433       sym.st_shndx = bed->common_section_index (input_sec);
9434       sym.st_value = 1 << h->root.u.c.p->alignment_power;
9435       break;
9436 
9437     case bfd_link_hash_indirect:
9438       /* These symbols are created by symbol versioning.  They point
9439 	 to the decorated version of the name.  For example, if the
9440 	 symbol foo@@GNU_1.2 is the default, which should be used when
9441 	 foo is used with no version, then we add an indirect symbol
9442 	 foo which points to foo@@GNU_1.2.  We ignore these symbols,
9443 	 since the indirected symbol is already in the hash table.  */
9444       return TRUE;
9445     }
9446 
9447   if (type == STT_COMMON || type == STT_OBJECT)
9448     switch (h->root.type)
9449       {
9450       case bfd_link_hash_common:
9451 	type = elf_link_convert_common_type (flinfo->info, type);
9452 	break;
9453       case bfd_link_hash_defined:
9454       case bfd_link_hash_defweak:
9455 	if (bed->common_definition (&sym))
9456 	  type = elf_link_convert_common_type (flinfo->info, type);
9457 	else
9458 	  type = STT_OBJECT;
9459 	break;
9460       case bfd_link_hash_undefined:
9461       case bfd_link_hash_undefweak:
9462 	break;
9463       default:
9464 	abort ();
9465       }
9466 
9467   if (local_bind)
9468     {
9469       sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9470       /* Turn off visibility on local symbol.  */
9471       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9472     }
9473   /* Set STB_GNU_UNIQUE only if symbol is defined in regular object.  */
9474   else if (h->unique_global && h->def_regular)
9475     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9476   else if (h->root.type == bfd_link_hash_undefweak
9477 	   || h->root.type == bfd_link_hash_defweak)
9478     sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9479   else
9480     sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9481   sym.st_target_internal = h->target_internal;
9482 
9483   /* Give the processor backend a chance to tweak the symbol value,
9484      and also to finish up anything that needs to be done for this
9485      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
9486      forced local syms when non-shared is due to a historical quirk.
9487      STT_GNU_IFUNC symbol must go through PLT.  */
9488   if ((h->type == STT_GNU_IFUNC
9489        && h->def_regular
9490        && !bfd_link_relocatable (flinfo->info))
9491       || ((h->dynindx != -1
9492 	   || h->forced_local)
9493 	  && ((bfd_link_pic (flinfo->info)
9494 	       && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9495 		   || h->root.type != bfd_link_hash_undefweak))
9496 	      || !h->forced_local)
9497 	  && elf_hash_table (flinfo->info)->dynamic_sections_created))
9498     {
9499       if (! ((*bed->elf_backend_finish_dynamic_symbol)
9500 	     (flinfo->output_bfd, flinfo->info, h, &sym)))
9501 	{
9502 	  eoinfo->failed = TRUE;
9503 	  return FALSE;
9504 	}
9505     }
9506 
9507   /* If we are marking the symbol as undefined, and there are no
9508      non-weak references to this symbol from a regular object, then
9509      mark the symbol as weak undefined; if there are non-weak
9510      references, mark the symbol as strong.  We can't do this earlier,
9511      because it might not be marked as undefined until the
9512      finish_dynamic_symbol routine gets through with it.  */
9513   if (sym.st_shndx == SHN_UNDEF
9514       && h->ref_regular
9515       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9516 	  || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9517     {
9518       int bindtype;
9519       type = ELF_ST_TYPE (sym.st_info);
9520 
9521       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9522       if (type == STT_GNU_IFUNC)
9523 	type = STT_FUNC;
9524 
9525       if (h->ref_regular_nonweak)
9526 	bindtype = STB_GLOBAL;
9527       else
9528 	bindtype = STB_WEAK;
9529       sym.st_info = ELF_ST_INFO (bindtype, type);
9530     }
9531 
9532   /* If this is a symbol defined in a dynamic library, don't use the
9533      symbol size from the dynamic library.  Relinking an executable
9534      against a new library may introduce gratuitous changes in the
9535      executable's symbols if we keep the size.  */
9536   if (sym.st_shndx == SHN_UNDEF
9537       && !h->def_regular
9538       && h->def_dynamic)
9539     sym.st_size = 0;
9540 
9541   /* If a non-weak symbol with non-default visibility is not defined
9542      locally, it is a fatal error.  */
9543   if (!bfd_link_relocatable (flinfo->info)
9544       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9545       && ELF_ST_BIND (sym.st_info) != STB_WEAK
9546       && h->root.type == bfd_link_hash_undefined
9547       && !h->def_regular)
9548     {
9549       const char *msg;
9550 
9551       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9552 	msg = _("%B: protected symbol `%s' isn't defined");
9553       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9554 	msg = _("%B: internal symbol `%s' isn't defined");
9555       else
9556 	msg = _("%B: hidden symbol `%s' isn't defined");
9557       (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9558       bfd_set_error (bfd_error_bad_value);
9559       eoinfo->failed = TRUE;
9560       return FALSE;
9561     }
9562 
9563   /* If this symbol should be put in the .dynsym section, then put it
9564      there now.  We already know the symbol index.  We also fill in
9565      the entry in the .hash section.  */
9566   if (elf_hash_table (flinfo->info)->dynsym != NULL
9567       && h->dynindx != -1
9568       && elf_hash_table (flinfo->info)->dynamic_sections_created)
9569     {
9570       bfd_byte *esym;
9571 
9572       /* Since there is no version information in the dynamic string,
9573 	 if there is no version info in symbol version section, we will
9574 	 have a run-time problem if not linking executable, referenced
9575 	 by shared library, not locally defined, or not bound locally.
9576       */
9577       if (h->verinfo.verdef == NULL
9578 	  && !local_bind
9579 	  && (!bfd_link_executable (flinfo->info)
9580 	      || h->ref_dynamic
9581 	      || !h->def_regular))
9582 	{
9583 	  char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9584 
9585 	  if (p && p [1] != '\0')
9586 	    {
9587 	      (*_bfd_error_handler)
9588 		(_("%B: No symbol version section for versioned symbol `%s'"),
9589 		 flinfo->output_bfd, h->root.root.string);
9590 	      eoinfo->failed = TRUE;
9591 	      return FALSE;
9592 	    }
9593 	}
9594 
9595       sym.st_name = h->dynstr_index;
9596       esym = (elf_hash_table (flinfo->info)->dynsym->contents
9597 	      + h->dynindx * bed->s->sizeof_sym);
9598       if (!check_dynsym (flinfo->output_bfd, &sym))
9599 	{
9600 	  eoinfo->failed = TRUE;
9601 	  return FALSE;
9602 	}
9603       bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9604 
9605       if (flinfo->hash_sec != NULL)
9606 	{
9607 	  size_t hash_entry_size;
9608 	  bfd_byte *bucketpos;
9609 	  bfd_vma chain;
9610 	  size_t bucketcount;
9611 	  size_t bucket;
9612 
9613 	  bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9614 	  bucket = h->u.elf_hash_value % bucketcount;
9615 
9616 	  hash_entry_size
9617 	    = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9618 	  bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9619 		       + (bucket + 2) * hash_entry_size);
9620 	  chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9621 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9622 		   bucketpos);
9623 	  bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9624 		   ((bfd_byte *) flinfo->hash_sec->contents
9625 		    + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9626 	}
9627 
9628       if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9629 	{
9630 	  Elf_Internal_Versym iversym;
9631 	  Elf_External_Versym *eversym;
9632 
9633 	  if (!h->def_regular)
9634 	    {
9635 	      if (h->verinfo.verdef == NULL
9636 		  || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9637 		      & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9638 		iversym.vs_vers = 0;
9639 	      else
9640 		iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9641 	    }
9642 	  else
9643 	    {
9644 	      if (h->verinfo.vertree == NULL)
9645 		iversym.vs_vers = 1;
9646 	      else
9647 		iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9648 	      if (flinfo->info->create_default_symver)
9649 		iversym.vs_vers++;
9650 	    }
9651 
9652 	  /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9653 	     defined locally.  */
9654 	  if (h->versioned == versioned_hidden && h->def_regular)
9655 	    iversym.vs_vers |= VERSYM_HIDDEN;
9656 
9657 	  eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9658 	  eversym += h->dynindx;
9659 	  _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9660 	}
9661     }
9662 
9663   /* If the symbol is undefined, and we didn't output it to .dynsym,
9664      strip it from .symtab too.  Obviously we can't do this for
9665      relocatable output or when needed for --emit-relocs.  */
9666   else if (input_sec == bfd_und_section_ptr
9667 	   && h->indx != -2
9668 	   && !bfd_link_relocatable (flinfo->info))
9669     return TRUE;
9670   /* Also strip others that we couldn't earlier due to dynamic symbol
9671      processing.  */
9672   if (strip)
9673     return TRUE;
9674   if ((input_sec->flags & SEC_EXCLUDE) != 0)
9675     return TRUE;
9676 
9677   /* Output a FILE symbol so that following locals are not associated
9678      with the wrong input file.  We need one for forced local symbols
9679      if we've seen more than one FILE symbol or when we have exactly
9680      one FILE symbol but global symbols are present in a file other
9681      than the one with the FILE symbol.  We also need one if linker
9682      defined symbols are present.  In practice these conditions are
9683      always met, so just emit the FILE symbol unconditionally.  */
9684   if (eoinfo->localsyms
9685       && !eoinfo->file_sym_done
9686       && eoinfo->flinfo->filesym_count != 0)
9687     {
9688       Elf_Internal_Sym fsym;
9689 
9690       memset (&fsym, 0, sizeof (fsym));
9691       fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9692       fsym.st_shndx = SHN_ABS;
9693       if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9694 				      bfd_und_section_ptr, NULL))
9695 	return FALSE;
9696 
9697       eoinfo->file_sym_done = TRUE;
9698     }
9699 
9700   indx = bfd_get_symcount (flinfo->output_bfd);
9701   ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9702 				   input_sec, h);
9703   if (ret == 0)
9704     {
9705       eoinfo->failed = TRUE;
9706       return FALSE;
9707     }
9708   else if (ret == 1)
9709     h->indx = indx;
9710   else if (h->indx == -2)
9711     abort();
9712 
9713   return TRUE;
9714 }
9715 
9716 /* Return TRUE if special handling is done for relocs in SEC against
9717    symbols defined in discarded sections.  */
9718 
9719 static bfd_boolean
9720 elf_section_ignore_discarded_relocs (asection *sec)
9721 {
9722   const struct elf_backend_data *bed;
9723 
9724   switch (sec->sec_info_type)
9725     {
9726     case SEC_INFO_TYPE_STABS:
9727     case SEC_INFO_TYPE_EH_FRAME:
9728     case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9729       return TRUE;
9730     default:
9731       break;
9732     }
9733 
9734   bed = get_elf_backend_data (sec->owner);
9735   if (bed->elf_backend_ignore_discarded_relocs != NULL
9736       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9737     return TRUE;
9738 
9739   return FALSE;
9740 }
9741 
9742 /* Return a mask saying how ld should treat relocations in SEC against
9743    symbols defined in discarded sections.  If this function returns
9744    COMPLAIN set, ld will issue a warning message.  If this function
9745    returns PRETEND set, and the discarded section was link-once and the
9746    same size as the kept link-once section, ld will pretend that the
9747    symbol was actually defined in the kept section.  Otherwise ld will
9748    zero the reloc (at least that is the intent, but some cooperation by
9749    the target dependent code is needed, particularly for REL targets).  */
9750 
9751 unsigned int
9752 _bfd_elf_default_action_discarded (asection *sec)
9753 {
9754   if (sec->flags & SEC_DEBUGGING)
9755     return PRETEND;
9756 
9757   if (strcmp (".eh_frame", sec->name) == 0)
9758     return 0;
9759 
9760   if (strcmp (".gcc_except_table", sec->name) == 0)
9761     return 0;
9762 
9763   return COMPLAIN | PRETEND;
9764 }
9765 
9766 /* Find a match between a section and a member of a section group.  */
9767 
9768 static asection *
9769 match_group_member (asection *sec, asection *group,
9770 		    struct bfd_link_info *info)
9771 {
9772   asection *first = elf_next_in_group (group);
9773   asection *s = first;
9774 
9775   while (s != NULL)
9776     {
9777       if (bfd_elf_match_symbols_in_sections (s, sec, info))
9778 	return s;
9779 
9780       s = elf_next_in_group (s);
9781       if (s == first)
9782 	break;
9783     }
9784 
9785   return NULL;
9786 }
9787 
9788 /* Check if the kept section of a discarded section SEC can be used
9789    to replace it.  Return the replacement if it is OK.  Otherwise return
9790    NULL.  */
9791 
9792 asection *
9793 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9794 {
9795   asection *kept;
9796 
9797   kept = sec->kept_section;
9798   if (kept != NULL)
9799     {
9800       if ((kept->flags & SEC_GROUP) != 0)
9801 	kept = match_group_member (sec, kept, info);
9802       if (kept != NULL
9803 	  && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9804 	      != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9805 	kept = NULL;
9806       sec->kept_section = kept;
9807     }
9808   return kept;
9809 }
9810 
9811 /* Link an input file into the linker output file.  This function
9812    handles all the sections and relocations of the input file at once.
9813    This is so that we only have to read the local symbols once, and
9814    don't have to keep them in memory.  */
9815 
9816 static bfd_boolean
9817 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9818 {
9819   int (*relocate_section)
9820     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9821      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9822   bfd *output_bfd;
9823   Elf_Internal_Shdr *symtab_hdr;
9824   size_t locsymcount;
9825   size_t extsymoff;
9826   Elf_Internal_Sym *isymbuf;
9827   Elf_Internal_Sym *isym;
9828   Elf_Internal_Sym *isymend;
9829   long *pindex;
9830   asection **ppsection;
9831   asection *o;
9832   const struct elf_backend_data *bed;
9833   struct elf_link_hash_entry **sym_hashes;
9834   bfd_size_type address_size;
9835   bfd_vma r_type_mask;
9836   int r_sym_shift;
9837   bfd_boolean have_file_sym = FALSE;
9838 
9839   output_bfd = flinfo->output_bfd;
9840   bed = get_elf_backend_data (output_bfd);
9841   relocate_section = bed->elf_backend_relocate_section;
9842 
9843   /* If this is a dynamic object, we don't want to do anything here:
9844      we don't want the local symbols, and we don't want the section
9845      contents.  */
9846   if ((input_bfd->flags & DYNAMIC) != 0)
9847     return TRUE;
9848 
9849   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9850   if (elf_bad_symtab (input_bfd))
9851     {
9852       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9853       extsymoff = 0;
9854     }
9855   else
9856     {
9857       locsymcount = symtab_hdr->sh_info;
9858       extsymoff = symtab_hdr->sh_info;
9859     }
9860 
9861   /* Read the local symbols.  */
9862   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9863   if (isymbuf == NULL && locsymcount != 0)
9864     {
9865       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9866 				      flinfo->internal_syms,
9867 				      flinfo->external_syms,
9868 				      flinfo->locsym_shndx);
9869       if (isymbuf == NULL)
9870 	return FALSE;
9871     }
9872 
9873   /* Find local symbol sections and adjust values of symbols in
9874      SEC_MERGE sections.  Write out those local symbols we know are
9875      going into the output file.  */
9876   isymend = isymbuf + locsymcount;
9877   for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9878        isym < isymend;
9879        isym++, pindex++, ppsection++)
9880     {
9881       asection *isec;
9882       const char *name;
9883       Elf_Internal_Sym osym;
9884       long indx;
9885       int ret;
9886 
9887       *pindex = -1;
9888 
9889       if (elf_bad_symtab (input_bfd))
9890 	{
9891 	  if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9892 	    {
9893 	      *ppsection = NULL;
9894 	      continue;
9895 	    }
9896 	}
9897 
9898       if (isym->st_shndx == SHN_UNDEF)
9899 	isec = bfd_und_section_ptr;
9900       else if (isym->st_shndx == SHN_ABS)
9901 	isec = bfd_abs_section_ptr;
9902       else if (isym->st_shndx == SHN_COMMON)
9903 	isec = bfd_com_section_ptr;
9904       else
9905 	{
9906 	  isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9907 	  if (isec == NULL)
9908 	    {
9909 	      /* Don't attempt to output symbols with st_shnx in the
9910 		 reserved range other than SHN_ABS and SHN_COMMON.  */
9911 	      *ppsection = NULL;
9912 	      continue;
9913 	    }
9914 	  else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9915 		   && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9916 	    isym->st_value =
9917 	      _bfd_merged_section_offset (output_bfd, &isec,
9918 					  elf_section_data (isec)->sec_info,
9919 					  isym->st_value);
9920 	}
9921 
9922       *ppsection = isec;
9923 
9924       /* Don't output the first, undefined, symbol.  In fact, don't
9925 	 output any undefined local symbol.  */
9926       if (isec == bfd_und_section_ptr)
9927 	continue;
9928 
9929       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9930 	{
9931 	  /* We never output section symbols.  Instead, we use the
9932 	     section symbol of the corresponding section in the output
9933 	     file.  */
9934 	  continue;
9935 	}
9936 
9937       /* If we are stripping all symbols, we don't want to output this
9938 	 one.  */
9939       if (flinfo->info->strip == strip_all)
9940 	continue;
9941 
9942       /* If we are discarding all local symbols, we don't want to
9943 	 output this one.  If we are generating a relocatable output
9944 	 file, then some of the local symbols may be required by
9945 	 relocs; we output them below as we discover that they are
9946 	 needed.  */
9947       if (flinfo->info->discard == discard_all)
9948 	continue;
9949 
9950       /* If this symbol is defined in a section which we are
9951 	 discarding, we don't need to keep it.  */
9952       if (isym->st_shndx != SHN_UNDEF
9953 	  && isym->st_shndx < SHN_LORESERVE
9954 	  && bfd_section_removed_from_list (output_bfd,
9955 					    isec->output_section))
9956 	continue;
9957 
9958       /* Get the name of the symbol.  */
9959       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9960 					      isym->st_name);
9961       if (name == NULL)
9962 	return FALSE;
9963 
9964       /* See if we are discarding symbols with this name.  */
9965       if ((flinfo->info->strip == strip_some
9966 	   && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9967 	       == NULL))
9968 	  || (((flinfo->info->discard == discard_sec_merge
9969 		&& (isec->flags & SEC_MERGE)
9970 		&& !bfd_link_relocatable (flinfo->info))
9971 	       || flinfo->info->discard == discard_l)
9972 	      && bfd_is_local_label_name (input_bfd, name)))
9973 	continue;
9974 
9975       if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9976 	{
9977 	  if (input_bfd->lto_output)
9978 	    /* -flto puts a temp file name here.  This means builds
9979 	       are not reproducible.  Discard the symbol.  */
9980 	    continue;
9981 	  have_file_sym = TRUE;
9982 	  flinfo->filesym_count += 1;
9983 	}
9984       if (!have_file_sym)
9985 	{
9986 	  /* In the absence of debug info, bfd_find_nearest_line uses
9987 	     FILE symbols to determine the source file for local
9988 	     function symbols.  Provide a FILE symbol here if input
9989 	     files lack such, so that their symbols won't be
9990 	     associated with a previous input file.  It's not the
9991 	     source file, but the best we can do.  */
9992 	  have_file_sym = TRUE;
9993 	  flinfo->filesym_count += 1;
9994 	  memset (&osym, 0, sizeof (osym));
9995 	  osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9996 	  osym.st_shndx = SHN_ABS;
9997 	  if (!elf_link_output_symstrtab (flinfo,
9998 					  (input_bfd->lto_output ? NULL
9999 					   : input_bfd->filename),
10000 					  &osym, bfd_abs_section_ptr,
10001 					  NULL))
10002 	    return FALSE;
10003 	}
10004 
10005       osym = *isym;
10006 
10007       /* Adjust the section index for the output file.  */
10008       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10009 							 isec->output_section);
10010       if (osym.st_shndx == SHN_BAD)
10011 	return FALSE;
10012 
10013       /* ELF symbols in relocatable files are section relative, but
10014 	 in executable files they are virtual addresses.  Note that
10015 	 this code assumes that all ELF sections have an associated
10016 	 BFD section with a reasonable value for output_offset; below
10017 	 we assume that they also have a reasonable value for
10018 	 output_section.  Any special sections must be set up to meet
10019 	 these requirements.  */
10020       osym.st_value += isec->output_offset;
10021       if (!bfd_link_relocatable (flinfo->info))
10022 	{
10023 	  osym.st_value += isec->output_section->vma;
10024 	  if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10025 	    {
10026 	      /* STT_TLS symbols are relative to PT_TLS segment base.  */
10027 	      BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10028 	      osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10029 	    }
10030 	}
10031 
10032       indx = bfd_get_symcount (output_bfd);
10033       ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10034       if (ret == 0)
10035 	return FALSE;
10036       else if (ret == 1)
10037 	*pindex = indx;
10038     }
10039 
10040   if (bed->s->arch_size == 32)
10041     {
10042       r_type_mask = 0xff;
10043       r_sym_shift = 8;
10044       address_size = 4;
10045     }
10046   else
10047     {
10048       r_type_mask = 0xffffffff;
10049       r_sym_shift = 32;
10050       address_size = 8;
10051     }
10052 
10053   /* Relocate the contents of each section.  */
10054   sym_hashes = elf_sym_hashes (input_bfd);
10055   for (o = input_bfd->sections; o != NULL; o = o->next)
10056     {
10057       bfd_byte *contents;
10058 
10059       if (! o->linker_mark)
10060 	{
10061 	  /* This section was omitted from the link.  */
10062 	  continue;
10063 	}
10064 
10065       if (bfd_link_relocatable (flinfo->info)
10066 	  && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10067 	{
10068 	  /* Deal with the group signature symbol.  */
10069 	  struct bfd_elf_section_data *sec_data = elf_section_data (o);
10070 	  unsigned long symndx = sec_data->this_hdr.sh_info;
10071 	  asection *osec = o->output_section;
10072 
10073 	  if (symndx >= locsymcount
10074 	      || (elf_bad_symtab (input_bfd)
10075 		  && flinfo->sections[symndx] == NULL))
10076 	    {
10077 	      struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10078 	      while (h->root.type == bfd_link_hash_indirect
10079 		     || h->root.type == bfd_link_hash_warning)
10080 		h = (struct elf_link_hash_entry *) h->root.u.i.link;
10081 	      /* Arrange for symbol to be output.  */
10082 	      h->indx = -2;
10083 	      elf_section_data (osec)->this_hdr.sh_info = -2;
10084 	    }
10085 	  else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10086 	    {
10087 	      /* We'll use the output section target_index.  */
10088 	      asection *sec = flinfo->sections[symndx]->output_section;
10089 	      elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10090 	    }
10091 	  else
10092 	    {
10093 	      if (flinfo->indices[symndx] == -1)
10094 		{
10095 		  /* Otherwise output the local symbol now.  */
10096 		  Elf_Internal_Sym sym = isymbuf[symndx];
10097 		  asection *sec = flinfo->sections[symndx]->output_section;
10098 		  const char *name;
10099 		  long indx;
10100 		  int ret;
10101 
10102 		  name = bfd_elf_string_from_elf_section (input_bfd,
10103 							  symtab_hdr->sh_link,
10104 							  sym.st_name);
10105 		  if (name == NULL)
10106 		    return FALSE;
10107 
10108 		  sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10109 								    sec);
10110 		  if (sym.st_shndx == SHN_BAD)
10111 		    return FALSE;
10112 
10113 		  sym.st_value += o->output_offset;
10114 
10115 		  indx = bfd_get_symcount (output_bfd);
10116 		  ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10117 						   NULL);
10118 		  if (ret == 0)
10119 		    return FALSE;
10120 		  else if (ret == 1)
10121 		    flinfo->indices[symndx] = indx;
10122 		  else
10123 		    abort ();
10124 		}
10125 	      elf_section_data (osec)->this_hdr.sh_info
10126 		= flinfo->indices[symndx];
10127 	    }
10128 	}
10129 
10130       if ((o->flags & SEC_HAS_CONTENTS) == 0
10131 	  || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10132 	continue;
10133 
10134       if ((o->flags & SEC_LINKER_CREATED) != 0)
10135 	{
10136 	  /* Section was created by _bfd_elf_link_create_dynamic_sections
10137 	     or somesuch.  */
10138 	  continue;
10139 	}
10140 
10141       /* Get the contents of the section.  They have been cached by a
10142 	 relaxation routine.  Note that o is a section in an input
10143 	 file, so the contents field will not have been set by any of
10144 	 the routines which work on output files.  */
10145       if (elf_section_data (o)->this_hdr.contents != NULL)
10146 	{
10147 	  contents = elf_section_data (o)->this_hdr.contents;
10148 	  if (bed->caches_rawsize
10149 	      && o->rawsize != 0
10150 	      && o->rawsize < o->size)
10151 	    {
10152 	      memcpy (flinfo->contents, contents, o->rawsize);
10153 	      contents = flinfo->contents;
10154 	    }
10155 	}
10156       else
10157 	{
10158 	  contents = flinfo->contents;
10159 	  if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10160 	    return FALSE;
10161 	}
10162 
10163       if ((o->flags & SEC_RELOC) != 0)
10164 	{
10165 	  Elf_Internal_Rela *internal_relocs;
10166 	  Elf_Internal_Rela *rel, *relend;
10167 	  int action_discarded;
10168 	  int ret;
10169 
10170 	  /* Get the swapped relocs.  */
10171 	  internal_relocs
10172 	    = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10173 					 flinfo->internal_relocs, FALSE);
10174 	  if (internal_relocs == NULL
10175 	      && o->reloc_count > 0)
10176 	    return FALSE;
10177 
10178 	  /* We need to reverse-copy input .ctors/.dtors sections if
10179 	     they are placed in .init_array/.finit_array for output.  */
10180 	  if (o->size > address_size
10181 	      && ((strncmp (o->name, ".ctors", 6) == 0
10182 		   && strcmp (o->output_section->name,
10183 			      ".init_array") == 0)
10184 		  || (strncmp (o->name, ".dtors", 6) == 0
10185 		      && strcmp (o->output_section->name,
10186 				 ".fini_array") == 0))
10187 	      && (o->name[6] == 0 || o->name[6] == '.'))
10188 	    {
10189 	      if (o->size != o->reloc_count * address_size)
10190 		{
10191 		  (*_bfd_error_handler)
10192 		    (_("error: %B: size of section %A is not "
10193 		       "multiple of address size"),
10194 		     input_bfd, o);
10195 		  bfd_set_error (bfd_error_on_input);
10196 		  return FALSE;
10197 		}
10198 	      o->flags |= SEC_ELF_REVERSE_COPY;
10199 	    }
10200 
10201 	  action_discarded = -1;
10202 	  if (!elf_section_ignore_discarded_relocs (o))
10203 	    action_discarded = (*bed->action_discarded) (o);
10204 
10205 	  /* Run through the relocs evaluating complex reloc symbols and
10206 	     looking for relocs against symbols from discarded sections
10207 	     or section symbols from removed link-once sections.
10208 	     Complain about relocs against discarded sections.  Zero
10209 	     relocs against removed link-once sections.  */
10210 
10211 	  rel = internal_relocs;
10212 	  relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10213 	  for ( ; rel < relend; rel++)
10214 	    {
10215 	      unsigned long r_symndx = rel->r_info >> r_sym_shift;
10216 	      unsigned int s_type;
10217 	      asection **ps, *sec;
10218 	      struct elf_link_hash_entry *h = NULL;
10219 	      const char *sym_name;
10220 
10221 	      if (r_symndx == STN_UNDEF)
10222 		continue;
10223 
10224 	      if (r_symndx >= locsymcount
10225 		  || (elf_bad_symtab (input_bfd)
10226 		      && flinfo->sections[r_symndx] == NULL))
10227 		{
10228 		  h = sym_hashes[r_symndx - extsymoff];
10229 
10230 		  /* Badly formatted input files can contain relocs that
10231 		     reference non-existant symbols.  Check here so that
10232 		     we do not seg fault.  */
10233 		  if (h == NULL)
10234 		    {
10235 		      char buffer [32];
10236 
10237 		      sprintf_vma (buffer, rel->r_info);
10238 		      (*_bfd_error_handler)
10239 			(_("error: %B contains a reloc (0x%s) for section %A "
10240 			   "that references a non-existent global symbol"),
10241 			 input_bfd, o, buffer);
10242 		      bfd_set_error (bfd_error_bad_value);
10243 		      return FALSE;
10244 		    }
10245 
10246 		  while (h->root.type == bfd_link_hash_indirect
10247 			 || h->root.type == bfd_link_hash_warning)
10248 		    h = (struct elf_link_hash_entry *) h->root.u.i.link;
10249 
10250 		  s_type = h->type;
10251 
10252 		  /* If a plugin symbol is referenced from a non-IR file,
10253 		     mark the symbol as undefined.  Note that the
10254 		     linker may attach linker created dynamic sections
10255 		     to the plugin bfd.  Symbols defined in linker
10256 		     created sections are not plugin symbols.  */
10257 		  if (h->root.non_ir_ref
10258 		      && (h->root.type == bfd_link_hash_defined
10259 			  || h->root.type == bfd_link_hash_defweak)
10260 		      && (h->root.u.def.section->flags
10261 			  & SEC_LINKER_CREATED) == 0
10262 		      && h->root.u.def.section->owner != NULL
10263 		      && (h->root.u.def.section->owner->flags
10264 			  & BFD_PLUGIN) != 0)
10265 		    {
10266 		      h->root.type = bfd_link_hash_undefined;
10267 		      h->root.u.undef.abfd = h->root.u.def.section->owner;
10268 		    }
10269 
10270 		  ps = NULL;
10271 		  if (h->root.type == bfd_link_hash_defined
10272 		      || h->root.type == bfd_link_hash_defweak)
10273 		    ps = &h->root.u.def.section;
10274 
10275 		  sym_name = h->root.root.string;
10276 		}
10277 	      else
10278 		{
10279 		  Elf_Internal_Sym *sym = isymbuf + r_symndx;
10280 
10281 		  s_type = ELF_ST_TYPE (sym->st_info);
10282 		  ps = &flinfo->sections[r_symndx];
10283 		  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10284 					       sym, *ps);
10285 		}
10286 
10287 	      if ((s_type == STT_RELC || s_type == STT_SRELC)
10288 		  && !bfd_link_relocatable (flinfo->info))
10289 		{
10290 		  bfd_vma val;
10291 		  bfd_vma dot = (rel->r_offset
10292 				 + o->output_offset + o->output_section->vma);
10293 #ifdef DEBUG
10294 		  printf ("Encountered a complex symbol!");
10295 		  printf (" (input_bfd %s, section %s, reloc %ld\n",
10296 			  input_bfd->filename, o->name,
10297 			  (long) (rel - internal_relocs));
10298 		  printf (" symbol: idx  %8.8lx, name %s\n",
10299 			  r_symndx, sym_name);
10300 		  printf (" reloc : info %8.8lx, addr %8.8lx\n",
10301 			  (unsigned long) rel->r_info,
10302 			  (unsigned long) rel->r_offset);
10303 #endif
10304 		  if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10305 				    isymbuf, locsymcount, s_type == STT_SRELC))
10306 		    return FALSE;
10307 
10308 		  /* Symbol evaluated OK.  Update to absolute value.  */
10309 		  set_symbol_value (input_bfd, isymbuf, locsymcount,
10310 				    r_symndx, val);
10311 		  continue;
10312 		}
10313 
10314 	      if (action_discarded != -1 && ps != NULL)
10315 		{
10316 		  /* Complain if the definition comes from a
10317 		     discarded section.  */
10318 		  if ((sec = *ps) != NULL && discarded_section (sec))
10319 		    {
10320 		      BFD_ASSERT (r_symndx != STN_UNDEF);
10321 		      if (action_discarded & COMPLAIN)
10322 			(*flinfo->info->callbacks->einfo)
10323 			  (_("%X`%s' referenced in section `%A' of %B: "
10324 			     "defined in discarded section `%A' of %B\n"),
10325 			   sym_name, o, input_bfd, sec, sec->owner);
10326 
10327 		      /* Try to do the best we can to support buggy old
10328 			 versions of gcc.  Pretend that the symbol is
10329 			 really defined in the kept linkonce section.
10330 			 FIXME: This is quite broken.  Modifying the
10331 			 symbol here means we will be changing all later
10332 			 uses of the symbol, not just in this section.  */
10333 		      if (action_discarded & PRETEND)
10334 			{
10335 			  asection *kept;
10336 
10337 			  kept = _bfd_elf_check_kept_section (sec,
10338 							      flinfo->info);
10339 			  if (kept != NULL)
10340 			    {
10341 			      *ps = kept;
10342 			      continue;
10343 			    }
10344 			}
10345 		    }
10346 		}
10347 	    }
10348 
10349 	  /* Relocate the section by invoking a back end routine.
10350 
10351 	     The back end routine is responsible for adjusting the
10352 	     section contents as necessary, and (if using Rela relocs
10353 	     and generating a relocatable output file) adjusting the
10354 	     reloc addend as necessary.
10355 
10356 	     The back end routine does not have to worry about setting
10357 	     the reloc address or the reloc symbol index.
10358 
10359 	     The back end routine is given a pointer to the swapped in
10360 	     internal symbols, and can access the hash table entries
10361 	     for the external symbols via elf_sym_hashes (input_bfd).
10362 
10363 	     When generating relocatable output, the back end routine
10364 	     must handle STB_LOCAL/STT_SECTION symbols specially.  The
10365 	     output symbol is going to be a section symbol
10366 	     corresponding to the output section, which will require
10367 	     the addend to be adjusted.  */
10368 
10369 	  ret = (*relocate_section) (output_bfd, flinfo->info,
10370 				     input_bfd, o, contents,
10371 				     internal_relocs,
10372 				     isymbuf,
10373 				     flinfo->sections);
10374 	  if (!ret)
10375 	    return FALSE;
10376 
10377 	  if (ret == 2
10378 	      || bfd_link_relocatable (flinfo->info)
10379 	      || flinfo->info->emitrelocations)
10380 	    {
10381 	      Elf_Internal_Rela *irela;
10382 	      Elf_Internal_Rela *irelaend, *irelamid;
10383 	      bfd_vma last_offset;
10384 	      struct elf_link_hash_entry **rel_hash;
10385 	      struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10386 	      Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10387 	      unsigned int next_erel;
10388 	      bfd_boolean rela_normal;
10389 	      struct bfd_elf_section_data *esdi, *esdo;
10390 
10391 	      esdi = elf_section_data (o);
10392 	      esdo = elf_section_data (o->output_section);
10393 	      rela_normal = FALSE;
10394 
10395 	      /* Adjust the reloc addresses and symbol indices.  */
10396 
10397 	      irela = internal_relocs;
10398 	      irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10399 	      rel_hash = esdo->rel.hashes + esdo->rel.count;
10400 	      /* We start processing the REL relocs, if any.  When we reach
10401 		 IRELAMID in the loop, we switch to the RELA relocs.  */
10402 	      irelamid = irela;
10403 	      if (esdi->rel.hdr != NULL)
10404 		irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10405 			     * bed->s->int_rels_per_ext_rel);
10406 	      rel_hash_list = rel_hash;
10407 	      rela_hash_list = NULL;
10408 	      last_offset = o->output_offset;
10409 	      if (!bfd_link_relocatable (flinfo->info))
10410 		last_offset += o->output_section->vma;
10411 	      for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10412 		{
10413 		  unsigned long r_symndx;
10414 		  asection *sec;
10415 		  Elf_Internal_Sym sym;
10416 
10417 		  if (next_erel == bed->s->int_rels_per_ext_rel)
10418 		    {
10419 		      rel_hash++;
10420 		      next_erel = 0;
10421 		    }
10422 
10423 		  if (irela == irelamid)
10424 		    {
10425 		      rel_hash = esdo->rela.hashes + esdo->rela.count;
10426 		      rela_hash_list = rel_hash;
10427 		      rela_normal = bed->rela_normal;
10428 		    }
10429 
10430 		  irela->r_offset = _bfd_elf_section_offset (output_bfd,
10431 							     flinfo->info, o,
10432 							     irela->r_offset);
10433 		  if (irela->r_offset >= (bfd_vma) -2)
10434 		    {
10435 		      /* This is a reloc for a deleted entry or somesuch.
10436 			 Turn it into an R_*_NONE reloc, at the same
10437 			 offset as the last reloc.  elf_eh_frame.c and
10438 			 bfd_elf_discard_info rely on reloc offsets
10439 			 being ordered.  */
10440 		      irela->r_offset = last_offset;
10441 		      irela->r_info = 0;
10442 		      irela->r_addend = 0;
10443 		      continue;
10444 		    }
10445 
10446 		  irela->r_offset += o->output_offset;
10447 
10448 		  /* Relocs in an executable have to be virtual addresses.  */
10449 		  if (!bfd_link_relocatable (flinfo->info))
10450 		    irela->r_offset += o->output_section->vma;
10451 
10452 		  last_offset = irela->r_offset;
10453 
10454 		  r_symndx = irela->r_info >> r_sym_shift;
10455 		  if (r_symndx == STN_UNDEF)
10456 		    continue;
10457 
10458 		  if (r_symndx >= locsymcount
10459 		      || (elf_bad_symtab (input_bfd)
10460 			  && flinfo->sections[r_symndx] == NULL))
10461 		    {
10462 		      struct elf_link_hash_entry *rh;
10463 		      unsigned long indx;
10464 
10465 		      /* This is a reloc against a global symbol.  We
10466 			 have not yet output all the local symbols, so
10467 			 we do not know the symbol index of any global
10468 			 symbol.  We set the rel_hash entry for this
10469 			 reloc to point to the global hash table entry
10470 			 for this symbol.  The symbol index is then
10471 			 set at the end of bfd_elf_final_link.  */
10472 		      indx = r_symndx - extsymoff;
10473 		      rh = elf_sym_hashes (input_bfd)[indx];
10474 		      while (rh->root.type == bfd_link_hash_indirect
10475 			     || rh->root.type == bfd_link_hash_warning)
10476 			rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10477 
10478 		      /* Setting the index to -2 tells
10479 			 elf_link_output_extsym that this symbol is
10480 			 used by a reloc.  */
10481 		      BFD_ASSERT (rh->indx < 0);
10482 		      rh->indx = -2;
10483 
10484 		      *rel_hash = rh;
10485 
10486 		      continue;
10487 		    }
10488 
10489 		  /* This is a reloc against a local symbol.  */
10490 
10491 		  *rel_hash = NULL;
10492 		  sym = isymbuf[r_symndx];
10493 		  sec = flinfo->sections[r_symndx];
10494 		  if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10495 		    {
10496 		      /* I suppose the backend ought to fill in the
10497 			 section of any STT_SECTION symbol against a
10498 			 processor specific section.  */
10499 		      r_symndx = STN_UNDEF;
10500 		      if (bfd_is_abs_section (sec))
10501 			;
10502 		      else if (sec == NULL || sec->owner == NULL)
10503 			{
10504 			  bfd_set_error (bfd_error_bad_value);
10505 			  return FALSE;
10506 			}
10507 		      else
10508 			{
10509 			  asection *osec = sec->output_section;
10510 
10511 			  /* If we have discarded a section, the output
10512 			     section will be the absolute section.  In
10513 			     case of discarded SEC_MERGE sections, use
10514 			     the kept section.  relocate_section should
10515 			     have already handled discarded linkonce
10516 			     sections.  */
10517 			  if (bfd_is_abs_section (osec)
10518 			      && sec->kept_section != NULL
10519 			      && sec->kept_section->output_section != NULL)
10520 			    {
10521 			      osec = sec->kept_section->output_section;
10522 			      irela->r_addend -= osec->vma;
10523 			    }
10524 
10525 			  if (!bfd_is_abs_section (osec))
10526 			    {
10527 			      r_symndx = osec->target_index;
10528 			      if (r_symndx == STN_UNDEF)
10529 				{
10530 				  irela->r_addend += osec->vma;
10531 				  osec = _bfd_nearby_section (output_bfd, osec,
10532 							      osec->vma);
10533 				  irela->r_addend -= osec->vma;
10534 				  r_symndx = osec->target_index;
10535 				}
10536 			    }
10537 			}
10538 
10539 		      /* Adjust the addend according to where the
10540 			 section winds up in the output section.  */
10541 		      if (rela_normal)
10542 			irela->r_addend += sec->output_offset;
10543 		    }
10544 		  else
10545 		    {
10546 		      if (flinfo->indices[r_symndx] == -1)
10547 			{
10548 			  unsigned long shlink;
10549 			  const char *name;
10550 			  asection *osec;
10551 			  long indx;
10552 
10553 			  if (flinfo->info->strip == strip_all)
10554 			    {
10555 			      /* You can't do ld -r -s.  */
10556 			      bfd_set_error (bfd_error_invalid_operation);
10557 			      return FALSE;
10558 			    }
10559 
10560 			  /* This symbol was skipped earlier, but
10561 			     since it is needed by a reloc, we
10562 			     must output it now.  */
10563 			  shlink = symtab_hdr->sh_link;
10564 			  name = (bfd_elf_string_from_elf_section
10565 				  (input_bfd, shlink, sym.st_name));
10566 			  if (name == NULL)
10567 			    return FALSE;
10568 
10569 			  osec = sec->output_section;
10570 			  sym.st_shndx =
10571 			    _bfd_elf_section_from_bfd_section (output_bfd,
10572 							       osec);
10573 			  if (sym.st_shndx == SHN_BAD)
10574 			    return FALSE;
10575 
10576 			  sym.st_value += sec->output_offset;
10577 			  if (!bfd_link_relocatable (flinfo->info))
10578 			    {
10579 			      sym.st_value += osec->vma;
10580 			      if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10581 				{
10582 				  /* STT_TLS symbols are relative to PT_TLS
10583 				     segment base.  */
10584 				  BFD_ASSERT (elf_hash_table (flinfo->info)
10585 					      ->tls_sec != NULL);
10586 				  sym.st_value -= (elf_hash_table (flinfo->info)
10587 						   ->tls_sec->vma);
10588 				}
10589 			    }
10590 
10591 			  indx = bfd_get_symcount (output_bfd);
10592 			  ret = elf_link_output_symstrtab (flinfo, name,
10593 							   &sym, sec,
10594 							   NULL);
10595 			  if (ret == 0)
10596 			    return FALSE;
10597 			  else if (ret == 1)
10598 			    flinfo->indices[r_symndx] = indx;
10599 			  else
10600 			    abort ();
10601 			}
10602 
10603 		      r_symndx = flinfo->indices[r_symndx];
10604 		    }
10605 
10606 		  irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10607 				   | (irela->r_info & r_type_mask));
10608 		}
10609 
10610 	      /* Swap out the relocs.  */
10611 	      input_rel_hdr = esdi->rel.hdr;
10612 	      if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10613 		{
10614 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
10615 						     input_rel_hdr,
10616 						     internal_relocs,
10617 						     rel_hash_list))
10618 		    return FALSE;
10619 		  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10620 				      * bed->s->int_rels_per_ext_rel);
10621 		  rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10622 		}
10623 
10624 	      input_rela_hdr = esdi->rela.hdr;
10625 	      if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10626 		{
10627 		  if (!bed->elf_backend_emit_relocs (output_bfd, o,
10628 						     input_rela_hdr,
10629 						     internal_relocs,
10630 						     rela_hash_list))
10631 		    return FALSE;
10632 		}
10633 	    }
10634 	}
10635 
10636       /* Write out the modified section contents.  */
10637       if (bed->elf_backend_write_section
10638 	  && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10639 						contents))
10640 	{
10641 	  /* Section written out.  */
10642 	}
10643       else switch (o->sec_info_type)
10644 	{
10645 	case SEC_INFO_TYPE_STABS:
10646 	  if (! (_bfd_write_section_stabs
10647 		 (output_bfd,
10648 		  &elf_hash_table (flinfo->info)->stab_info,
10649 		  o, &elf_section_data (o)->sec_info, contents)))
10650 	    return FALSE;
10651 	  break;
10652 	case SEC_INFO_TYPE_MERGE:
10653 	  if (! _bfd_write_merged_section (output_bfd, o,
10654 					   elf_section_data (o)->sec_info))
10655 	    return FALSE;
10656 	  break;
10657 	case SEC_INFO_TYPE_EH_FRAME:
10658 	  {
10659 	    if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10660 						   o, contents))
10661 	      return FALSE;
10662 	  }
10663 	  break;
10664 	case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10665 	  {
10666 	    if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10667 							 flinfo->info,
10668 							 o, contents))
10669 	      return FALSE;
10670 	  }
10671 	  break;
10672 	default:
10673 	  {
10674 	    if (! (o->flags & SEC_EXCLUDE))
10675 	      {
10676 		file_ptr offset = (file_ptr) o->output_offset;
10677 		bfd_size_type todo = o->size;
10678 
10679 		offset *= bfd_octets_per_byte (output_bfd);
10680 
10681 		if ((o->flags & SEC_ELF_REVERSE_COPY))
10682 		  {
10683 		    /* Reverse-copy input section to output.  */
10684 		    do
10685 		      {
10686 			todo -= address_size;
10687 			if (! bfd_set_section_contents (output_bfd,
10688 							o->output_section,
10689 							contents + todo,
10690 							offset,
10691 							address_size))
10692 			  return FALSE;
10693 			if (todo == 0)
10694 			  break;
10695 			offset += address_size;
10696 		      }
10697 		    while (1);
10698 		  }
10699 		else if (! bfd_set_section_contents (output_bfd,
10700 						     o->output_section,
10701 						     contents,
10702 						     offset, todo))
10703 		  return FALSE;
10704 	      }
10705 	  }
10706 	  break;
10707 	}
10708     }
10709 
10710   return TRUE;
10711 }
10712 
10713 /* Generate a reloc when linking an ELF file.  This is a reloc
10714    requested by the linker, and does not come from any input file.  This
10715    is used to build constructor and destructor tables when linking
10716    with -Ur.  */
10717 
10718 static bfd_boolean
10719 elf_reloc_link_order (bfd *output_bfd,
10720 		      struct bfd_link_info *info,
10721 		      asection *output_section,
10722 		      struct bfd_link_order *link_order)
10723 {
10724   reloc_howto_type *howto;
10725   long indx;
10726   bfd_vma offset;
10727   bfd_vma addend;
10728   struct bfd_elf_section_reloc_data *reldata;
10729   struct elf_link_hash_entry **rel_hash_ptr;
10730   Elf_Internal_Shdr *rel_hdr;
10731   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10732   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10733   bfd_byte *erel;
10734   unsigned int i;
10735   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10736 
10737   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10738   if (howto == NULL)
10739     {
10740       bfd_set_error (bfd_error_bad_value);
10741       return FALSE;
10742     }
10743 
10744   addend = link_order->u.reloc.p->addend;
10745 
10746   if (esdo->rel.hdr)
10747     reldata = &esdo->rel;
10748   else if (esdo->rela.hdr)
10749     reldata = &esdo->rela;
10750   else
10751     {
10752       reldata = NULL;
10753       BFD_ASSERT (0);
10754     }
10755 
10756   /* Figure out the symbol index.  */
10757   rel_hash_ptr = reldata->hashes + reldata->count;
10758   if (link_order->type == bfd_section_reloc_link_order)
10759     {
10760       indx = link_order->u.reloc.p->u.section->target_index;
10761       BFD_ASSERT (indx != 0);
10762       *rel_hash_ptr = NULL;
10763     }
10764   else
10765     {
10766       struct elf_link_hash_entry *h;
10767 
10768       /* Treat a reloc against a defined symbol as though it were
10769 	 actually against the section.  */
10770       h = ((struct elf_link_hash_entry *)
10771 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
10772 					 link_order->u.reloc.p->u.name,
10773 					 FALSE, FALSE, TRUE));
10774       if (h != NULL
10775 	  && (h->root.type == bfd_link_hash_defined
10776 	      || h->root.type == bfd_link_hash_defweak))
10777 	{
10778 	  asection *section;
10779 
10780 	  section = h->root.u.def.section;
10781 	  indx = section->output_section->target_index;
10782 	  *rel_hash_ptr = NULL;
10783 	  /* It seems that we ought to add the symbol value to the
10784 	     addend here, but in practice it has already been added
10785 	     because it was passed to constructor_callback.  */
10786 	  addend += section->output_section->vma + section->output_offset;
10787 	}
10788       else if (h != NULL)
10789 	{
10790 	  /* Setting the index to -2 tells elf_link_output_extsym that
10791 	     this symbol is used by a reloc.  */
10792 	  h->indx = -2;
10793 	  *rel_hash_ptr = h;
10794 	  indx = 0;
10795 	}
10796       else
10797 	{
10798 	  (*info->callbacks->unattached_reloc)
10799 	    (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
10800 	  indx = 0;
10801 	}
10802     }
10803 
10804   /* If this is an inplace reloc, we must write the addend into the
10805      object file.  */
10806   if (howto->partial_inplace && addend != 0)
10807     {
10808       bfd_size_type size;
10809       bfd_reloc_status_type rstat;
10810       bfd_byte *buf;
10811       bfd_boolean ok;
10812       const char *sym_name;
10813 
10814       size = (bfd_size_type) bfd_get_reloc_size (howto);
10815       buf = (bfd_byte *) bfd_zmalloc (size);
10816       if (buf == NULL && size != 0)
10817 	return FALSE;
10818       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10819       switch (rstat)
10820 	{
10821 	case bfd_reloc_ok:
10822 	  break;
10823 
10824 	default:
10825 	case bfd_reloc_outofrange:
10826 	  abort ();
10827 
10828 	case bfd_reloc_overflow:
10829 	  if (link_order->type == bfd_section_reloc_link_order)
10830 	    sym_name = bfd_section_name (output_bfd,
10831 					 link_order->u.reloc.p->u.section);
10832 	  else
10833 	    sym_name = link_order->u.reloc.p->u.name;
10834 	  (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10835 					      howto->name, addend, NULL, NULL,
10836 					      (bfd_vma) 0);
10837 	  break;
10838 	}
10839 
10840       ok = bfd_set_section_contents (output_bfd, output_section, buf,
10841 				     link_order->offset
10842 				     * bfd_octets_per_byte (output_bfd),
10843 				     size);
10844       free (buf);
10845       if (! ok)
10846 	return FALSE;
10847     }
10848 
10849   /* The address of a reloc is relative to the section in a
10850      relocatable file, and is a virtual address in an executable
10851      file.  */
10852   offset = link_order->offset;
10853   if (! bfd_link_relocatable (info))
10854     offset += output_section->vma;
10855 
10856   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10857     {
10858       irel[i].r_offset = offset;
10859       irel[i].r_info = 0;
10860       irel[i].r_addend = 0;
10861     }
10862   if (bed->s->arch_size == 32)
10863     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10864   else
10865 #ifdef BFD64
10866           {
10867             bfd_uint64_t indx64 = indx;
10868             irel[0].r_info = ELF64_R_INFO (indx64, howto->type);
10869           }
10870 #else
10871           BFD_FAIL();
10872 #endif
10873 
10874   rel_hdr = reldata->hdr;
10875   erel = rel_hdr->contents;
10876   if (rel_hdr->sh_type == SHT_REL)
10877     {
10878       erel += reldata->count * bed->s->sizeof_rel;
10879       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10880     }
10881   else
10882     {
10883       irel[0].r_addend = addend;
10884       erel += reldata->count * bed->s->sizeof_rela;
10885       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10886     }
10887 
10888   ++reldata->count;
10889 
10890   return TRUE;
10891 }
10892 
10893 
10894 /* Get the output vma of the section pointed to by the sh_link field.  */
10895 
10896 static bfd_vma
10897 elf_get_linked_section_vma (struct bfd_link_order *p)
10898 {
10899   Elf_Internal_Shdr **elf_shdrp;
10900   asection *s;
10901   int elfsec;
10902 
10903   s = p->u.indirect.section;
10904   elf_shdrp = elf_elfsections (s->owner);
10905   elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10906   elfsec = elf_shdrp[elfsec]->sh_link;
10907   /* PR 290:
10908      The Intel C compiler generates SHT_IA_64_UNWIND with
10909      SHF_LINK_ORDER.  But it doesn't set the sh_link or
10910      sh_info fields.  Hence we could get the situation
10911      where elfsec is 0.  */
10912   if (elfsec == 0)
10913     {
10914       const struct elf_backend_data *bed
10915 	= get_elf_backend_data (s->owner);
10916       if (bed->link_order_error_handler)
10917 	bed->link_order_error_handler
10918 	  (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10919       return 0;
10920     }
10921   else
10922     {
10923       s = elf_shdrp[elfsec]->bfd_section;
10924       return s->output_section->vma + s->output_offset;
10925     }
10926 }
10927 
10928 
10929 /* Compare two sections based on the locations of the sections they are
10930    linked to.  Used by elf_fixup_link_order.  */
10931 
10932 static int
10933 compare_link_order (const void * a, const void * b)
10934 {
10935   bfd_vma apos;
10936   bfd_vma bpos;
10937 
10938   apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10939   bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10940   if (apos < bpos)
10941     return -1;
10942   return apos > bpos;
10943 }
10944 
10945 
10946 /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
10947    order as their linked sections.  Returns false if this could not be done
10948    because an output section includes both ordered and unordered
10949    sections.  Ideally we'd do this in the linker proper.  */
10950 
10951 static bfd_boolean
10952 elf_fixup_link_order (bfd *abfd, asection *o)
10953 {
10954   int seen_linkorder;
10955   int seen_other;
10956   int n;
10957   struct bfd_link_order *p;
10958   bfd *sub;
10959   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10960   unsigned elfsec;
10961   struct bfd_link_order **sections;
10962   asection *s, *other_sec, *linkorder_sec;
10963   bfd_vma offset;
10964 
10965   other_sec = NULL;
10966   linkorder_sec = NULL;
10967   seen_other = 0;
10968   seen_linkorder = 0;
10969   for (p = o->map_head.link_order; p != NULL; p = p->next)
10970     {
10971       if (p->type == bfd_indirect_link_order)
10972 	{
10973 	  s = p->u.indirect.section;
10974 	  sub = s->owner;
10975 	  if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10976 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10977 	      && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10978 	      && elfsec < elf_numsections (sub)
10979 	      && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10980 	      && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10981 	    {
10982 	      seen_linkorder++;
10983 	      linkorder_sec = s;
10984 	    }
10985 	  else
10986 	    {
10987 	      seen_other++;
10988 	      other_sec = s;
10989 	    }
10990 	}
10991       else
10992 	seen_other++;
10993 
10994       if (seen_other && seen_linkorder)
10995 	{
10996 	  if (other_sec && linkorder_sec)
10997 	    (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10998 				   o, linkorder_sec,
10999 				   linkorder_sec->owner, other_sec,
11000 				   other_sec->owner);
11001 	  else
11002 	    (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
11003 				   o);
11004 	  bfd_set_error (bfd_error_bad_value);
11005 	  return FALSE;
11006 	}
11007     }
11008 
11009   if (!seen_linkorder)
11010     return TRUE;
11011 
11012   sections = (struct bfd_link_order **)
11013     bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11014   if (sections == NULL)
11015     return FALSE;
11016   seen_linkorder = 0;
11017 
11018   for (p = o->map_head.link_order; p != NULL; p = p->next)
11019     {
11020       sections[seen_linkorder++] = p;
11021     }
11022   /* Sort the input sections in the order of their linked section.  */
11023   qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11024 	 compare_link_order);
11025 
11026   /* Change the offsets of the sections.  */
11027   offset = 0;
11028   for (n = 0; n < seen_linkorder; n++)
11029     {
11030       s = sections[n]->u.indirect.section;
11031       offset &= ~(bfd_vma) 0 << s->alignment_power;
11032       s->output_offset = offset / bfd_octets_per_byte (abfd);
11033       sections[n]->offset = offset;
11034       offset += sections[n]->size;
11035     }
11036 
11037   free (sections);
11038   return TRUE;
11039 }
11040 
11041 static void
11042 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11043 {
11044   asection *o;
11045 
11046   if (flinfo->symstrtab != NULL)
11047     _bfd_elf_strtab_free (flinfo->symstrtab);
11048   if (flinfo->contents != NULL)
11049     free (flinfo->contents);
11050   if (flinfo->external_relocs != NULL)
11051     free (flinfo->external_relocs);
11052   if (flinfo->internal_relocs != NULL)
11053     free (flinfo->internal_relocs);
11054   if (flinfo->external_syms != NULL)
11055     free (flinfo->external_syms);
11056   if (flinfo->locsym_shndx != NULL)
11057     free (flinfo->locsym_shndx);
11058   if (flinfo->internal_syms != NULL)
11059     free (flinfo->internal_syms);
11060   if (flinfo->indices != NULL)
11061     free (flinfo->indices);
11062   if (flinfo->sections != NULL)
11063     free (flinfo->sections);
11064   if (flinfo->symshndxbuf != NULL)
11065     free (flinfo->symshndxbuf);
11066   for (o = obfd->sections; o != NULL; o = o->next)
11067     {
11068       struct bfd_elf_section_data *esdo = elf_section_data (o);
11069       if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11070 	free (esdo->rel.hashes);
11071       if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11072 	free (esdo->rela.hashes);
11073     }
11074 }
11075 
11076 /* Do the final step of an ELF link.  */
11077 
11078 bfd_boolean
11079 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11080 {
11081   bfd_boolean dynamic;
11082   bfd_boolean emit_relocs;
11083   bfd *dynobj;
11084   struct elf_final_link_info flinfo;
11085   asection *o;
11086   struct bfd_link_order *p;
11087   bfd *sub;
11088   bfd_size_type max_contents_size;
11089   bfd_size_type max_external_reloc_size;
11090   bfd_size_type max_internal_reloc_count;
11091   bfd_size_type max_sym_count;
11092   bfd_size_type max_sym_shndx_count;
11093   Elf_Internal_Sym elfsym;
11094   unsigned int i;
11095   Elf_Internal_Shdr *symtab_hdr;
11096   Elf_Internal_Shdr *symtab_shndx_hdr;
11097   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11098   struct elf_outext_info eoinfo;
11099   bfd_boolean merged;
11100   size_t relativecount = 0;
11101   asection *reldyn = 0;
11102   bfd_size_type amt;
11103   asection *attr_section = NULL;
11104   bfd_vma attr_size = 0;
11105   const char *std_attrs_section;
11106 
11107   if (! is_elf_hash_table (info->hash))
11108     return FALSE;
11109 
11110   if (bfd_link_pic (info))
11111     abfd->flags |= DYNAMIC;
11112 
11113   dynamic = elf_hash_table (info)->dynamic_sections_created;
11114   dynobj = elf_hash_table (info)->dynobj;
11115 
11116   emit_relocs = (bfd_link_relocatable (info)
11117 		 || info->emitrelocations);
11118 
11119   flinfo.info = info;
11120   flinfo.output_bfd = abfd;
11121   flinfo.symstrtab = _bfd_elf_strtab_init ();
11122   if (flinfo.symstrtab == NULL)
11123     return FALSE;
11124 
11125   if (! dynamic)
11126     {
11127       flinfo.hash_sec = NULL;
11128       flinfo.symver_sec = NULL;
11129     }
11130   else
11131     {
11132       flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11133       /* Note that dynsym_sec can be NULL (on VMS).  */
11134       flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11135       /* Note that it is OK if symver_sec is NULL.  */
11136     }
11137 
11138   flinfo.contents = NULL;
11139   flinfo.external_relocs = NULL;
11140   flinfo.internal_relocs = NULL;
11141   flinfo.external_syms = NULL;
11142   flinfo.locsym_shndx = NULL;
11143   flinfo.internal_syms = NULL;
11144   flinfo.indices = NULL;
11145   flinfo.sections = NULL;
11146   flinfo.symshndxbuf = NULL;
11147   flinfo.filesym_count = 0;
11148 
11149   /* The object attributes have been merged.  Remove the input
11150      sections from the link, and set the contents of the output
11151      secton.  */
11152   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11153   for (o = abfd->sections; o != NULL; o = o->next)
11154     {
11155       if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11156 	  || strcmp (o->name, ".gnu.attributes") == 0)
11157 	{
11158 	  for (p = o->map_head.link_order; p != NULL; p = p->next)
11159 	    {
11160 	      asection *input_section;
11161 
11162 	      if (p->type != bfd_indirect_link_order)
11163 		continue;
11164 	      input_section = p->u.indirect.section;
11165 	      /* Hack: reset the SEC_HAS_CONTENTS flag so that
11166 		 elf_link_input_bfd ignores this section.  */
11167 	      input_section->flags &= ~SEC_HAS_CONTENTS;
11168 	    }
11169 
11170 	  attr_size = bfd_elf_obj_attr_size (abfd);
11171 	  if (attr_size)
11172 	    {
11173 	      bfd_set_section_size (abfd, o, attr_size);
11174 	      attr_section = o;
11175 	      /* Skip this section later on.  */
11176 	      o->map_head.link_order = NULL;
11177 	    }
11178 	  else
11179 	    o->flags |= SEC_EXCLUDE;
11180 	}
11181     }
11182 
11183   /* Count up the number of relocations we will output for each output
11184      section, so that we know the sizes of the reloc sections.  We
11185      also figure out some maximum sizes.  */
11186   max_contents_size = 0;
11187   max_external_reloc_size = 0;
11188   max_internal_reloc_count = 0;
11189   max_sym_count = 0;
11190   max_sym_shndx_count = 0;
11191   merged = FALSE;
11192   for (o = abfd->sections; o != NULL; o = o->next)
11193     {
11194       struct bfd_elf_section_data *esdo = elf_section_data (o);
11195       o->reloc_count = 0;
11196 
11197       for (p = o->map_head.link_order; p != NULL; p = p->next)
11198 	{
11199 	  unsigned int reloc_count = 0;
11200 	  unsigned int additional_reloc_count = 0;
11201 	  struct bfd_elf_section_data *esdi = NULL;
11202 
11203 	  if (p->type == bfd_section_reloc_link_order
11204 	      || p->type == bfd_symbol_reloc_link_order)
11205 	    reloc_count = 1;
11206 	  else if (p->type == bfd_indirect_link_order)
11207 	    {
11208 	      asection *sec;
11209 
11210 	      sec = p->u.indirect.section;
11211 	      esdi = elf_section_data (sec);
11212 
11213 	      /* Mark all sections which are to be included in the
11214 		 link.  This will normally be every section.  We need
11215 		 to do this so that we can identify any sections which
11216 		 the linker has decided to not include.  */
11217 	      sec->linker_mark = TRUE;
11218 
11219 	      if (sec->flags & SEC_MERGE)
11220 		merged = TRUE;
11221 
11222 	      if (esdo->this_hdr.sh_type == SHT_REL
11223 		  || esdo->this_hdr.sh_type == SHT_RELA)
11224 		/* Some backends use reloc_count in relocation sections
11225 		   to count particular types of relocs.  Of course,
11226 		   reloc sections themselves can't have relocations.  */
11227 		reloc_count = 0;
11228 	      else if (emit_relocs)
11229 		{
11230 		  reloc_count = sec->reloc_count;
11231 		  if (bed->elf_backend_count_additional_relocs)
11232 		    {
11233 		      int c;
11234 		      c = (*bed->elf_backend_count_additional_relocs) (sec);
11235 		      additional_reloc_count += c;
11236 		    }
11237 		}
11238 	      else if (bed->elf_backend_count_relocs)
11239 		reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11240 
11241 	      if (sec->rawsize > max_contents_size)
11242 		max_contents_size = sec->rawsize;
11243 	      if (sec->size > max_contents_size)
11244 		max_contents_size = sec->size;
11245 
11246 	      /* We are interested in just local symbols, not all
11247 		 symbols.  */
11248 	      if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11249 		  && (sec->owner->flags & DYNAMIC) == 0)
11250 		{
11251 		  size_t sym_count;
11252 
11253 		  if (elf_bad_symtab (sec->owner))
11254 		    sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11255 				 / bed->s->sizeof_sym);
11256 		  else
11257 		    sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11258 
11259 		  if (sym_count > max_sym_count)
11260 		    max_sym_count = sym_count;
11261 
11262 		  if (sym_count > max_sym_shndx_count
11263 		      && elf_symtab_shndx_list (sec->owner) != NULL)
11264 		    max_sym_shndx_count = sym_count;
11265 
11266 		  if ((sec->flags & SEC_RELOC) != 0)
11267 		    {
11268 		      size_t ext_size = 0;
11269 
11270 		      if (esdi->rel.hdr != NULL)
11271 			ext_size = esdi->rel.hdr->sh_size;
11272 		      if (esdi->rela.hdr != NULL)
11273 			ext_size += esdi->rela.hdr->sh_size;
11274 
11275 		      if (ext_size > max_external_reloc_size)
11276 			max_external_reloc_size = ext_size;
11277 		      if (sec->reloc_count > max_internal_reloc_count)
11278 			max_internal_reloc_count = sec->reloc_count;
11279 		    }
11280 		}
11281 	    }
11282 
11283 	  if (reloc_count == 0)
11284 	    continue;
11285 
11286 	  reloc_count += additional_reloc_count;
11287 	  o->reloc_count += reloc_count;
11288 
11289 	  if (p->type == bfd_indirect_link_order && emit_relocs)
11290 	    {
11291 	      if (esdi->rel.hdr)
11292 		{
11293 		  esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11294 		  esdo->rel.count += additional_reloc_count;
11295 		}
11296 	      if (esdi->rela.hdr)
11297 		{
11298 		  esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11299 		  esdo->rela.count += additional_reloc_count;
11300 		}
11301 	    }
11302 	  else
11303 	    {
11304 	      if (o->use_rela_p)
11305 		esdo->rela.count += reloc_count;
11306 	      else
11307 		esdo->rel.count += reloc_count;
11308 	    }
11309 	}
11310 
11311       if (o->reloc_count > 0)
11312 	o->flags |= SEC_RELOC;
11313       else
11314 	{
11315 	  /* Explicitly clear the SEC_RELOC flag.  The linker tends to
11316 	     set it (this is probably a bug) and if it is set
11317 	     assign_section_numbers will create a reloc section.  */
11318 	  o->flags &=~ SEC_RELOC;
11319 	}
11320 
11321       /* If the SEC_ALLOC flag is not set, force the section VMA to
11322 	 zero.  This is done in elf_fake_sections as well, but forcing
11323 	 the VMA to 0 here will ensure that relocs against these
11324 	 sections are handled correctly.  */
11325       if ((o->flags & SEC_ALLOC) == 0
11326 	  && ! o->user_set_vma)
11327 	o->vma = 0;
11328     }
11329 
11330   if (! bfd_link_relocatable (info) && merged)
11331     elf_link_hash_traverse (elf_hash_table (info),
11332 			    _bfd_elf_link_sec_merge_syms, abfd);
11333 
11334   /* Figure out the file positions for everything but the symbol table
11335      and the relocs.  We set symcount to force assign_section_numbers
11336      to create a symbol table.  */
11337   bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11338   BFD_ASSERT (! abfd->output_has_begun);
11339   if (! _bfd_elf_compute_section_file_positions (abfd, info))
11340     goto error_return;
11341 
11342   /* Set sizes, and assign file positions for reloc sections.  */
11343   for (o = abfd->sections; o != NULL; o = o->next)
11344     {
11345       struct bfd_elf_section_data *esdo = elf_section_data (o);
11346       if ((o->flags & SEC_RELOC) != 0)
11347 	{
11348 	  if (esdo->rel.hdr
11349 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11350 	    goto error_return;
11351 
11352 	  if (esdo->rela.hdr
11353 	      && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11354 	    goto error_return;
11355 	}
11356 
11357       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11358 	 to count upwards while actually outputting the relocations.  */
11359       esdo->rel.count = 0;
11360       esdo->rela.count = 0;
11361 
11362       if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11363 	{
11364 	  /* Cache the section contents so that they can be compressed
11365 	     later.  Use bfd_malloc since it will be freed by
11366 	     bfd_compress_section_contents.  */
11367 	  unsigned char *contents = esdo->this_hdr.contents;
11368 	  if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11369 	    abort ();
11370 	  contents
11371 	    = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11372 	  if (contents == NULL)
11373 	    goto error_return;
11374 	  esdo->this_hdr.contents = contents;
11375 	}
11376     }
11377 
11378   /* We have now assigned file positions for all the sections except
11379      .symtab, .strtab, and non-loaded reloc sections.  We start the
11380      .symtab section at the current file position, and write directly
11381      to it.  We build the .strtab section in memory.  */
11382   bfd_get_symcount (abfd) = 0;
11383   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11384   /* sh_name is set in prep_headers.  */
11385   symtab_hdr->sh_type = SHT_SYMTAB;
11386   /* sh_flags, sh_addr and sh_size all start off zero.  */
11387   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11388   /* sh_link is set in assign_section_numbers.  */
11389   /* sh_info is set below.  */
11390   /* sh_offset is set just below.  */
11391   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11392 
11393   if (max_sym_count < 20)
11394     max_sym_count = 20;
11395   elf_hash_table (info)->strtabsize = max_sym_count;
11396   amt = max_sym_count * sizeof (struct elf_sym_strtab);
11397   elf_hash_table (info)->strtab
11398     = (struct elf_sym_strtab *) bfd_malloc (amt);
11399   if (elf_hash_table (info)->strtab == NULL)
11400     goto error_return;
11401   /* The real buffer will be allocated in elf_link_swap_symbols_out.  */
11402   flinfo.symshndxbuf
11403     = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11404        ? (Elf_External_Sym_Shndx *) -1 : NULL);
11405 
11406   if (info->strip != strip_all || emit_relocs)
11407     {
11408       file_ptr off = elf_next_file_pos (abfd);
11409 
11410       _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11411 
11412       /* Note that at this point elf_next_file_pos (abfd) is
11413 	 incorrect.  We do not yet know the size of the .symtab section.
11414 	 We correct next_file_pos below, after we do know the size.  */
11415 
11416       /* Start writing out the symbol table.  The first symbol is always a
11417 	 dummy symbol.  */
11418       elfsym.st_value = 0;
11419       elfsym.st_size = 0;
11420       elfsym.st_info = 0;
11421       elfsym.st_other = 0;
11422       elfsym.st_shndx = SHN_UNDEF;
11423       elfsym.st_target_internal = 0;
11424       if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11425 				     bfd_und_section_ptr, NULL) != 1)
11426 	goto error_return;
11427 
11428       /* Output a symbol for each section.  We output these even if we are
11429 	 discarding local symbols, since they are used for relocs.  These
11430 	 symbols have no names.  We store the index of each one in the
11431 	 index field of the section, so that we can find it again when
11432 	 outputting relocs.  */
11433 
11434       elfsym.st_size = 0;
11435       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11436       elfsym.st_other = 0;
11437       elfsym.st_value = 0;
11438       elfsym.st_target_internal = 0;
11439       for (i = 1; i < elf_numsections (abfd); i++)
11440 	{
11441 	  o = bfd_section_from_elf_index (abfd, i);
11442 	  if (o != NULL)
11443 	    {
11444 	      o->target_index = bfd_get_symcount (abfd);
11445 	      elfsym.st_shndx = i;
11446 	      if (!bfd_link_relocatable (info))
11447 		elfsym.st_value = o->vma;
11448 	      if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11449 					     NULL) != 1)
11450 		goto error_return;
11451 	    }
11452 	}
11453     }
11454 
11455   /* Allocate some memory to hold information read in from the input
11456      files.  */
11457   if (max_contents_size != 0)
11458     {
11459       flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11460       if (flinfo.contents == NULL)
11461 	goto error_return;
11462     }
11463 
11464   if (max_external_reloc_size != 0)
11465     {
11466       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11467       if (flinfo.external_relocs == NULL)
11468 	goto error_return;
11469     }
11470 
11471   if (max_internal_reloc_count != 0)
11472     {
11473       amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11474       amt *= sizeof (Elf_Internal_Rela);
11475       flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11476       if (flinfo.internal_relocs == NULL)
11477 	goto error_return;
11478     }
11479 
11480   if (max_sym_count != 0)
11481     {
11482       amt = max_sym_count * bed->s->sizeof_sym;
11483       flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11484       if (flinfo.external_syms == NULL)
11485 	goto error_return;
11486 
11487       amt = max_sym_count * sizeof (Elf_Internal_Sym);
11488       flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11489       if (flinfo.internal_syms == NULL)
11490 	goto error_return;
11491 
11492       amt = max_sym_count * sizeof (long);
11493       flinfo.indices = (long int *) bfd_malloc (amt);
11494       if (flinfo.indices == NULL)
11495 	goto error_return;
11496 
11497       amt = max_sym_count * sizeof (asection *);
11498       flinfo.sections = (asection **) bfd_malloc (amt);
11499       if (flinfo.sections == NULL)
11500 	goto error_return;
11501     }
11502 
11503   if (max_sym_shndx_count != 0)
11504     {
11505       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11506       flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11507       if (flinfo.locsym_shndx == NULL)
11508 	goto error_return;
11509     }
11510 
11511   if (elf_hash_table (info)->tls_sec)
11512     {
11513       bfd_vma base, end = 0;
11514       asection *sec;
11515 
11516       for (sec = elf_hash_table (info)->tls_sec;
11517 	   sec && (sec->flags & SEC_THREAD_LOCAL);
11518 	   sec = sec->next)
11519 	{
11520 	  bfd_size_type size = sec->size;
11521 
11522 	  if (size == 0
11523 	      && (sec->flags & SEC_HAS_CONTENTS) == 0)
11524 	    {
11525 	      struct bfd_link_order *ord = sec->map_tail.link_order;
11526 
11527 	      if (ord != NULL)
11528 		size = ord->offset + ord->size;
11529 	    }
11530 	  end = sec->vma + size;
11531 	}
11532       base = elf_hash_table (info)->tls_sec->vma;
11533       /* Only align end of TLS section if static TLS doesn't have special
11534 	 alignment requirements.  */
11535       if (bed->static_tls_alignment == 1)
11536 	end = align_power (end,
11537 			   elf_hash_table (info)->tls_sec->alignment_power);
11538       elf_hash_table (info)->tls_size = end - base;
11539     }
11540 
11541   /* Reorder SHF_LINK_ORDER sections.  */
11542   for (o = abfd->sections; o != NULL; o = o->next)
11543     {
11544       if (!elf_fixup_link_order (abfd, o))
11545 	return FALSE;
11546     }
11547 
11548   if (!_bfd_elf_fixup_eh_frame_hdr (info))
11549     return FALSE;
11550 
11551   /* Since ELF permits relocations to be against local symbols, we
11552      must have the local symbols available when we do the relocations.
11553      Since we would rather only read the local symbols once, and we
11554      would rather not keep them in memory, we handle all the
11555      relocations for a single input file at the same time.
11556 
11557      Unfortunately, there is no way to know the total number of local
11558      symbols until we have seen all of them, and the local symbol
11559      indices precede the global symbol indices.  This means that when
11560      we are generating relocatable output, and we see a reloc against
11561      a global symbol, we can not know the symbol index until we have
11562      finished examining all the local symbols to see which ones we are
11563      going to output.  To deal with this, we keep the relocations in
11564      memory, and don't output them until the end of the link.  This is
11565      an unfortunate waste of memory, but I don't see a good way around
11566      it.  Fortunately, it only happens when performing a relocatable
11567      link, which is not the common case.  FIXME: If keep_memory is set
11568      we could write the relocs out and then read them again; I don't
11569      know how bad the memory loss will be.  */
11570 
11571   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11572     sub->output_has_begun = FALSE;
11573   for (o = abfd->sections; o != NULL; o = o->next)
11574     {
11575       for (p = o->map_head.link_order; p != NULL; p = p->next)
11576 	{
11577 	  if (p->type == bfd_indirect_link_order
11578 	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11579 		  == bfd_target_elf_flavour)
11580 	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11581 	    {
11582 	      if (! sub->output_has_begun)
11583 		{
11584 		  if (! elf_link_input_bfd (&flinfo, sub))
11585 		    goto error_return;
11586 		  sub->output_has_begun = TRUE;
11587 		}
11588 	    }
11589 	  else if (p->type == bfd_section_reloc_link_order
11590 		   || p->type == bfd_symbol_reloc_link_order)
11591 	    {
11592 	      if (! elf_reloc_link_order (abfd, info, o, p))
11593 		goto error_return;
11594 	    }
11595 	  else
11596 	    {
11597 	      if (! _bfd_default_link_order (abfd, info, o, p))
11598 		{
11599 		  if (p->type == bfd_indirect_link_order
11600 		      && (bfd_get_flavour (sub)
11601 			  == bfd_target_elf_flavour)
11602 		      && (elf_elfheader (sub)->e_ident[EI_CLASS]
11603 			  != bed->s->elfclass))
11604 		    {
11605 		      const char *iclass, *oclass;
11606 
11607 		      switch (bed->s->elfclass)
11608 			{
11609 			case ELFCLASS64: oclass = "ELFCLASS64"; break;
11610 			case ELFCLASS32: oclass = "ELFCLASS32"; break;
11611 			case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11612 			default: abort ();
11613 			}
11614 
11615 		      switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11616 			{
11617 			case ELFCLASS64: iclass = "ELFCLASS64"; break;
11618 			case ELFCLASS32: iclass = "ELFCLASS32"; break;
11619 			case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11620 			default: abort ();
11621 			}
11622 
11623 		      bfd_set_error (bfd_error_wrong_format);
11624 		      (*_bfd_error_handler)
11625 			(_("%B: file class %s incompatible with %s"),
11626 			 sub, iclass, oclass);
11627 		    }
11628 
11629 		  goto error_return;
11630 		}
11631 	    }
11632 	}
11633     }
11634 
11635   /* Free symbol buffer if needed.  */
11636   if (!info->reduce_memory_overheads)
11637     {
11638       for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11639 	if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11640 	    && elf_tdata (sub)->symbuf)
11641 	  {
11642 	    free (elf_tdata (sub)->symbuf);
11643 	    elf_tdata (sub)->symbuf = NULL;
11644 	  }
11645     }
11646 
11647   /* Output any global symbols that got converted to local in a
11648      version script or due to symbol visibility.  We do this in a
11649      separate step since ELF requires all local symbols to appear
11650      prior to any global symbols.  FIXME: We should only do this if
11651      some global symbols were, in fact, converted to become local.
11652      FIXME: Will this work correctly with the Irix 5 linker?  */
11653   eoinfo.failed = FALSE;
11654   eoinfo.flinfo = &flinfo;
11655   eoinfo.localsyms = TRUE;
11656   eoinfo.file_sym_done = FALSE;
11657   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11658   if (eoinfo.failed)
11659     return FALSE;
11660 
11661   /* If backend needs to output some local symbols not present in the hash
11662      table, do it now.  */
11663   if (bed->elf_backend_output_arch_local_syms
11664       && (info->strip != strip_all || emit_relocs))
11665     {
11666       typedef int (*out_sym_func)
11667 	(void *, const char *, Elf_Internal_Sym *, asection *,
11668 	 struct elf_link_hash_entry *);
11669 
11670       if (! ((*bed->elf_backend_output_arch_local_syms)
11671 	     (abfd, info, &flinfo,
11672 	      (out_sym_func) elf_link_output_symstrtab)))
11673 	return FALSE;
11674     }
11675 
11676   /* That wrote out all the local symbols.  Finish up the symbol table
11677      with the global symbols. Even if we want to strip everything we
11678      can, we still need to deal with those global symbols that got
11679      converted to local in a version script.  */
11680 
11681   /* The sh_info field records the index of the first non local symbol.  */
11682   symtab_hdr->sh_info = bfd_get_symcount (abfd);
11683 
11684   if (dynamic
11685       && elf_hash_table (info)->dynsym != NULL
11686       && (elf_hash_table (info)->dynsym->output_section
11687 	  != bfd_abs_section_ptr))
11688     {
11689       Elf_Internal_Sym sym;
11690       bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11691       long last_local = 0;
11692 
11693       /* Write out the section symbols for the output sections.  */
11694       if (bfd_link_pic (info)
11695 	  || elf_hash_table (info)->is_relocatable_executable)
11696 	{
11697 	  asection *s;
11698 
11699 	  sym.st_size = 0;
11700 	  sym.st_name = 0;
11701 	  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11702 	  sym.st_other = 0;
11703 	  sym.st_target_internal = 0;
11704 
11705 	  for (s = abfd->sections; s != NULL; s = s->next)
11706 	    {
11707 	      int indx;
11708 	      bfd_byte *dest;
11709 	      long dynindx;
11710 
11711 	      dynindx = elf_section_data (s)->dynindx;
11712 	      if (dynindx <= 0)
11713 		continue;
11714 	      indx = elf_section_data (s)->this_idx;
11715 	      BFD_ASSERT (indx > 0);
11716 	      sym.st_shndx = indx;
11717 	      if (! check_dynsym (abfd, &sym))
11718 		return FALSE;
11719 	      sym.st_value = s->vma;
11720 	      dest = dynsym + dynindx * bed->s->sizeof_sym;
11721 	      if (last_local < dynindx)
11722 		last_local = dynindx;
11723 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11724 	    }
11725 	}
11726 
11727       /* Write out the local dynsyms.  */
11728       if (elf_hash_table (info)->dynlocal)
11729 	{
11730 	  struct elf_link_local_dynamic_entry *e;
11731 	  for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11732 	    {
11733 	      asection *s;
11734 	      bfd_byte *dest;
11735 
11736 	      /* Copy the internal symbol and turn off visibility.
11737 		 Note that we saved a word of storage and overwrote
11738 		 the original st_name with the dynstr_index.  */
11739 	      sym = e->isym;
11740 	      sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11741 
11742 	      s = bfd_section_from_elf_index (e->input_bfd,
11743 					      e->isym.st_shndx);
11744 	      if (s != NULL)
11745 		{
11746 		  sym.st_shndx =
11747 		    elf_section_data (s->output_section)->this_idx;
11748 		  if (! check_dynsym (abfd, &sym))
11749 		    return FALSE;
11750 		  sym.st_value = (s->output_section->vma
11751 				  + s->output_offset
11752 				  + e->isym.st_value);
11753 		}
11754 
11755 	      if (last_local < e->dynindx)
11756 		last_local = e->dynindx;
11757 
11758 	      dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11759 	      bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11760 	    }
11761 	}
11762 
11763       elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11764 	last_local + 1;
11765     }
11766 
11767   /* We get the global symbols from the hash table.  */
11768   eoinfo.failed = FALSE;
11769   eoinfo.localsyms = FALSE;
11770   eoinfo.flinfo = &flinfo;
11771   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11772   if (eoinfo.failed)
11773     return FALSE;
11774 
11775   /* If backend needs to output some symbols not present in the hash
11776      table, do it now.  */
11777   if (bed->elf_backend_output_arch_syms
11778       && (info->strip != strip_all || emit_relocs))
11779     {
11780       typedef int (*out_sym_func)
11781 	(void *, const char *, Elf_Internal_Sym *, asection *,
11782 	 struct elf_link_hash_entry *);
11783 
11784       if (! ((*bed->elf_backend_output_arch_syms)
11785 	     (abfd, info, &flinfo,
11786 	      (out_sym_func) elf_link_output_symstrtab)))
11787 	return FALSE;
11788     }
11789 
11790   /* Finalize the .strtab section.  */
11791   _bfd_elf_strtab_finalize (flinfo.symstrtab);
11792 
11793   /* Swap out the .strtab section. */
11794   if (!elf_link_swap_symbols_out (&flinfo))
11795     return FALSE;
11796 
11797   /* Now we know the size of the symtab section.  */
11798   if (bfd_get_symcount (abfd) > 0)
11799     {
11800       /* Finish up and write out the symbol string table (.strtab)
11801 	 section.  */
11802       Elf_Internal_Shdr *symstrtab_hdr;
11803       file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11804 
11805       symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11806       if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11807 	{
11808 	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11809 	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11810 	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11811 	  amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11812 	  symtab_shndx_hdr->sh_size = amt;
11813 
11814 	  off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11815 							   off, TRUE);
11816 
11817 	  if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11818 	      || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11819 	    return FALSE;
11820 	}
11821 
11822       symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11823       /* sh_name was set in prep_headers.  */
11824       symstrtab_hdr->sh_type = SHT_STRTAB;
11825       symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
11826       symstrtab_hdr->sh_addr = 0;
11827       symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11828       symstrtab_hdr->sh_entsize = 0;
11829       symstrtab_hdr->sh_link = 0;
11830       symstrtab_hdr->sh_info = 0;
11831       /* sh_offset is set just below.  */
11832       symstrtab_hdr->sh_addralign = 1;
11833 
11834       off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11835 						       off, TRUE);
11836       elf_next_file_pos (abfd) = off;
11837 
11838       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11839 	  || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11840 	return FALSE;
11841     }
11842 
11843   /* Adjust the relocs to have the correct symbol indices.  */
11844   for (o = abfd->sections; o != NULL; o = o->next)
11845     {
11846       struct bfd_elf_section_data *esdo = elf_section_data (o);
11847       bfd_boolean sort;
11848       if ((o->flags & SEC_RELOC) == 0)
11849 	continue;
11850 
11851       sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11852       if (esdo->rel.hdr != NULL
11853 	  && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11854 	return FALSE;
11855       if (esdo->rela.hdr != NULL
11856 	  && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11857 	return FALSE;
11858 
11859       /* Set the reloc_count field to 0 to prevent write_relocs from
11860 	 trying to swap the relocs out itself.  */
11861       o->reloc_count = 0;
11862     }
11863 
11864   if (dynamic && info->combreloc && dynobj != NULL)
11865     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11866 
11867   /* If we are linking against a dynamic object, or generating a
11868      shared library, finish up the dynamic linking information.  */
11869   if (dynamic)
11870     {
11871       bfd_byte *dyncon, *dynconend;
11872 
11873       /* Fix up .dynamic entries.  */
11874       o = bfd_get_linker_section (dynobj, ".dynamic");
11875       BFD_ASSERT (o != NULL);
11876 
11877       dyncon = o->contents;
11878       dynconend = o->contents + o->size;
11879       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11880 	{
11881 	  Elf_Internal_Dyn dyn;
11882 	  const char *name;
11883 	  unsigned int type;
11884 
11885 	  bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11886 
11887 	  switch (dyn.d_tag)
11888 	    {
11889 	    default:
11890 	      continue;
11891 	    case DT_NULL:
11892 	      if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11893 		{
11894 		  switch (elf_section_data (reldyn)->this_hdr.sh_type)
11895 		    {
11896 		    case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11897 		    case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11898 		    default: continue;
11899 		    }
11900 		  dyn.d_un.d_val = relativecount;
11901 		  relativecount = 0;
11902 		  break;
11903 		}
11904 	      continue;
11905 
11906 	    case DT_INIT:
11907 	      name = info->init_function;
11908 	      goto get_sym;
11909 	    case DT_FINI:
11910 	      name = info->fini_function;
11911 	    get_sym:
11912 	      {
11913 		struct elf_link_hash_entry *h;
11914 
11915 		h = elf_link_hash_lookup (elf_hash_table (info), name,
11916 					  FALSE, FALSE, TRUE);
11917 		if (h != NULL
11918 		    && (h->root.type == bfd_link_hash_defined
11919 			|| h->root.type == bfd_link_hash_defweak))
11920 		  {
11921 		    dyn.d_un.d_ptr = h->root.u.def.value;
11922 		    o = h->root.u.def.section;
11923 		    if (o->output_section != NULL)
11924 		      dyn.d_un.d_ptr += (o->output_section->vma
11925 					 + o->output_offset);
11926 		    else
11927 		      {
11928 			/* The symbol is imported from another shared
11929 			   library and does not apply to this one.  */
11930 			dyn.d_un.d_ptr = 0;
11931 		      }
11932 		    break;
11933 		  }
11934 	      }
11935 	      continue;
11936 
11937 	    case DT_PREINIT_ARRAYSZ:
11938 	      name = ".preinit_array";
11939 	      goto get_out_size;
11940 	    case DT_INIT_ARRAYSZ:
11941 	      name = ".init_array";
11942 	      goto get_out_size;
11943 	    case DT_FINI_ARRAYSZ:
11944 	      name = ".fini_array";
11945 	    get_out_size:
11946 	      o = bfd_get_section_by_name (abfd, name);
11947 	      if (o == NULL)
11948 		{
11949 		  (*_bfd_error_handler)
11950 		    (_("could not find section %s"), name);
11951 		  goto error_return;
11952 		}
11953 	      if (o->size == 0)
11954 		(*_bfd_error_handler)
11955 		  (_("warning: %s section has zero size"), name);
11956 	      dyn.d_un.d_val = o->size;
11957 	      break;
11958 
11959 	    case DT_PREINIT_ARRAY:
11960 	      name = ".preinit_array";
11961 	      goto get_out_vma;
11962 	    case DT_INIT_ARRAY:
11963 	      name = ".init_array";
11964 	      goto get_out_vma;
11965 	    case DT_FINI_ARRAY:
11966 	      name = ".fini_array";
11967 	    get_out_vma:
11968 	      o = bfd_get_section_by_name (abfd, name);
11969 	      goto do_vma;
11970 
11971 	    case DT_HASH:
11972 	      name = ".hash";
11973 	      goto get_vma;
11974 	    case DT_GNU_HASH:
11975 	      name = ".gnu.hash";
11976 	      goto get_vma;
11977 	    case DT_STRTAB:
11978 	      name = ".dynstr";
11979 	      goto get_vma;
11980 	    case DT_SYMTAB:
11981 	      name = ".dynsym";
11982 	      goto get_vma;
11983 	    case DT_VERDEF:
11984 	      name = ".gnu.version_d";
11985 	      goto get_vma;
11986 	    case DT_VERNEED:
11987 	      name = ".gnu.version_r";
11988 	      goto get_vma;
11989 	    case DT_VERSYM:
11990 	      name = ".gnu.version";
11991 	    get_vma:
11992 	      o = bfd_get_linker_section (dynobj, name);
11993 	    do_vma:
11994 	      if (o == NULL)
11995 		{
11996 		  (*_bfd_error_handler)
11997 		    (_("could not find section %s"), name);
11998 		  goto error_return;
11999 		}
12000 	      if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12001 		{
12002 		  (*_bfd_error_handler)
12003 		    (_("warning: section '%s' is being made into a note"), name);
12004 		  bfd_set_error (bfd_error_nonrepresentable_section);
12005 		  goto error_return;
12006 		}
12007 	      dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12008 	      break;
12009 
12010 	    case DT_REL:
12011 	    case DT_RELA:
12012 	    case DT_RELSZ:
12013 	    case DT_RELASZ:
12014 	      if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12015 		type = SHT_REL;
12016 	      else
12017 		type = SHT_RELA;
12018 	      dyn.d_un.d_val = 0;
12019 	      dyn.d_un.d_ptr = 0;
12020 	      for (i = 1; i < elf_numsections (abfd); i++)
12021 		{
12022 		  Elf_Internal_Shdr *hdr;
12023 
12024 		  hdr = elf_elfsections (abfd)[i];
12025 		  if (hdr->sh_type == type
12026 		      && (hdr->sh_flags & SHF_ALLOC) != 0)
12027 		    {
12028 		      if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12029 			dyn.d_un.d_val += hdr->sh_size;
12030 		      else
12031 			{
12032 			  if (dyn.d_un.d_ptr == 0
12033 			      || hdr->sh_addr < dyn.d_un.d_ptr)
12034 			    dyn.d_un.d_ptr = hdr->sh_addr;
12035 			}
12036 		    }
12037 		}
12038 	      break;
12039 	    }
12040 	  bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12041 	}
12042     }
12043 
12044   /* If we have created any dynamic sections, then output them.  */
12045   if (dynobj != NULL)
12046     {
12047       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12048 	goto error_return;
12049 
12050       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
12051       if (((info->warn_shared_textrel && bfd_link_pic (info))
12052 	   || info->error_textrel)
12053 	  && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12054 	{
12055 	  bfd_byte *dyncon, *dynconend;
12056 
12057 	  dyncon = o->contents;
12058 	  dynconend = o->contents + o->size;
12059 	  for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12060 	    {
12061 	      Elf_Internal_Dyn dyn;
12062 
12063 	      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12064 
12065 	      if (dyn.d_tag == DT_TEXTREL)
12066 		{
12067 		  if (info->error_textrel)
12068 		    info->callbacks->einfo
12069 		      (_("%P%X: read-only segment has dynamic relocations.\n"));
12070 		  else
12071 		    info->callbacks->einfo
12072 		      (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12073 		  break;
12074 		}
12075 	    }
12076 	}
12077 
12078       for (o = dynobj->sections; o != NULL; o = o->next)
12079 	{
12080 	  if ((o->flags & SEC_HAS_CONTENTS) == 0
12081 	      || o->size == 0
12082 	      || o->output_section == bfd_abs_section_ptr)
12083 	    continue;
12084 	  if ((o->flags & SEC_LINKER_CREATED) == 0)
12085 	    {
12086 	      /* At this point, we are only interested in sections
12087 		 created by _bfd_elf_link_create_dynamic_sections.  */
12088 	      continue;
12089 	    }
12090 	  if (elf_hash_table (info)->stab_info.stabstr == o)
12091 	    continue;
12092 	  if (elf_hash_table (info)->eh_info.hdr_sec == o)
12093 	    continue;
12094 	  if (strcmp (o->name, ".dynstr") != 0)
12095 	    {
12096 	      if (! bfd_set_section_contents (abfd, o->output_section,
12097 					      o->contents,
12098 					      (file_ptr) o->output_offset
12099 					      * bfd_octets_per_byte (abfd),
12100 					      o->size))
12101 		goto error_return;
12102 	    }
12103 	  else
12104 	    {
12105 	      /* The contents of the .dynstr section are actually in a
12106 		 stringtab.  */
12107 	      file_ptr off;
12108 
12109 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12110 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
12111 		  || ! _bfd_elf_strtab_emit (abfd,
12112 					     elf_hash_table (info)->dynstr))
12113 		goto error_return;
12114 	    }
12115 	}
12116     }
12117 
12118   if (bfd_link_relocatable (info))
12119     {
12120       bfd_boolean failed = FALSE;
12121 
12122       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12123       if (failed)
12124 	goto error_return;
12125     }
12126 
12127   /* If we have optimized stabs strings, output them.  */
12128   if (elf_hash_table (info)->stab_info.stabstr != NULL)
12129     {
12130       if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12131 	goto error_return;
12132     }
12133 
12134   if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12135     goto error_return;
12136 
12137   elf_final_link_free (abfd, &flinfo);
12138 
12139   elf_linker (abfd) = TRUE;
12140 
12141   if (attr_section)
12142     {
12143       bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12144       if (contents == NULL)
12145 	return FALSE;	/* Bail out and fail.  */
12146       bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12147       bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12148       free (contents);
12149     }
12150 
12151   return TRUE;
12152 
12153  error_return:
12154   elf_final_link_free (abfd, &flinfo);
12155   return FALSE;
12156 }
12157 
12158 /* Initialize COOKIE for input bfd ABFD.  */
12159 
12160 static bfd_boolean
12161 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12162 		   struct bfd_link_info *info, bfd *abfd)
12163 {
12164   Elf_Internal_Shdr *symtab_hdr;
12165   const struct elf_backend_data *bed;
12166 
12167   bed = get_elf_backend_data (abfd);
12168   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12169 
12170   cookie->abfd = abfd;
12171   cookie->sym_hashes = elf_sym_hashes (abfd);
12172   cookie->bad_symtab = elf_bad_symtab (abfd);
12173   if (cookie->bad_symtab)
12174     {
12175       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12176       cookie->extsymoff = 0;
12177     }
12178   else
12179     {
12180       cookie->locsymcount = symtab_hdr->sh_info;
12181       cookie->extsymoff = symtab_hdr->sh_info;
12182     }
12183 
12184   if (bed->s->arch_size == 32)
12185     cookie->r_sym_shift = 8;
12186   else
12187     cookie->r_sym_shift = 32;
12188 
12189   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12190   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12191     {
12192       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12193 					      cookie->locsymcount, 0,
12194 					      NULL, NULL, NULL);
12195       if (cookie->locsyms == NULL)
12196 	{
12197 	  info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12198 	  return FALSE;
12199 	}
12200       if (info->keep_memory)
12201 	symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12202     }
12203   return TRUE;
12204 }
12205 
12206 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
12207 
12208 static void
12209 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12210 {
12211   Elf_Internal_Shdr *symtab_hdr;
12212 
12213   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12214   if (cookie->locsyms != NULL
12215       && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12216     free (cookie->locsyms);
12217 }
12218 
12219 /* Initialize the relocation information in COOKIE for input section SEC
12220    of input bfd ABFD.  */
12221 
12222 static bfd_boolean
12223 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12224 			struct bfd_link_info *info, bfd *abfd,
12225 			asection *sec)
12226 {
12227   const struct elf_backend_data *bed;
12228 
12229   if (sec->reloc_count == 0)
12230     {
12231       cookie->rels = NULL;
12232       cookie->relend = NULL;
12233     }
12234   else
12235     {
12236       bed = get_elf_backend_data (abfd);
12237 
12238       cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12239 						info->keep_memory);
12240       if (cookie->rels == NULL)
12241 	return FALSE;
12242       cookie->rel = cookie->rels;
12243       cookie->relend = (cookie->rels
12244 			+ sec->reloc_count * bed->s->int_rels_per_ext_rel);
12245     }
12246   cookie->rel = cookie->rels;
12247   return TRUE;
12248 }
12249 
12250 /* Free the memory allocated by init_reloc_cookie_rels,
12251    if appropriate.  */
12252 
12253 static void
12254 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12255 			asection *sec)
12256 {
12257   if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12258     free (cookie->rels);
12259 }
12260 
12261 /* Initialize the whole of COOKIE for input section SEC.  */
12262 
12263 static bfd_boolean
12264 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12265 			       struct bfd_link_info *info,
12266 			       asection *sec)
12267 {
12268   if (!init_reloc_cookie (cookie, info, sec->owner))
12269     goto error1;
12270   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12271     goto error2;
12272   return TRUE;
12273 
12274  error2:
12275   fini_reloc_cookie (cookie, sec->owner);
12276  error1:
12277   return FALSE;
12278 }
12279 
12280 /* Free the memory allocated by init_reloc_cookie_for_section,
12281    if appropriate.  */
12282 
12283 static void
12284 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12285 			       asection *sec)
12286 {
12287   fini_reloc_cookie_rels (cookie, sec);
12288   fini_reloc_cookie (cookie, sec->owner);
12289 }
12290 
12291 /* Garbage collect unused sections.  */
12292 
12293 /* Default gc_mark_hook.  */
12294 
12295 asection *
12296 _bfd_elf_gc_mark_hook (asection *sec,
12297 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
12298 		       Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12299 		       struct elf_link_hash_entry *h,
12300 		       Elf_Internal_Sym *sym)
12301 {
12302   if (h != NULL)
12303     {
12304       switch (h->root.type)
12305 	{
12306 	case bfd_link_hash_defined:
12307 	case bfd_link_hash_defweak:
12308 	  return h->root.u.def.section;
12309 
12310 	case bfd_link_hash_common:
12311 	  return h->root.u.c.p->section;
12312 
12313 	default:
12314 	  break;
12315 	}
12316     }
12317   else
12318     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12319 
12320   return NULL;
12321 }
12322 
12323 /* For undefined __start_<name> and __stop_<name> symbols, return the
12324    first input section matching <name>.  Return NULL otherwise.  */
12325 
12326 asection *
12327 _bfd_elf_is_start_stop (const struct bfd_link_info *info,
12328 			struct elf_link_hash_entry *h)
12329 {
12330   asection *s;
12331   const char *sec_name;
12332 
12333   if (h->root.type != bfd_link_hash_undefined
12334       && h->root.type != bfd_link_hash_undefweak)
12335     return NULL;
12336 
12337   s = h->root.u.undef.section;
12338   if (s != NULL)
12339     {
12340       if (s == (asection *) 0 - 1)
12341 	return NULL;
12342       return s;
12343     }
12344 
12345   sec_name = NULL;
12346   if (strncmp (h->root.root.string, "__start_", 8) == 0)
12347     sec_name = h->root.root.string + 8;
12348   else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12349     sec_name = h->root.root.string + 7;
12350 
12351   if (sec_name != NULL && *sec_name != '\0')
12352     {
12353       bfd *i;
12354 
12355       for (i = info->input_bfds; i != NULL; i = i->link.next)
12356 	{
12357 	  s = bfd_get_section_by_name (i, sec_name);
12358 	  if (s != NULL)
12359 	    {
12360 	      h->root.u.undef.section = s;
12361 	      break;
12362 	    }
12363 	}
12364     }
12365 
12366   if (s == NULL)
12367     h->root.u.undef.section = (asection *) 0 - 1;
12368 
12369   return s;
12370 }
12371 
12372 /* COOKIE->rel describes a relocation against section SEC, which is
12373    a section we've decided to keep.  Return the section that contains
12374    the relocation symbol, or NULL if no section contains it.  */
12375 
12376 asection *
12377 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12378 		       elf_gc_mark_hook_fn gc_mark_hook,
12379 		       struct elf_reloc_cookie *cookie,
12380 		       bfd_boolean *start_stop)
12381 {
12382   unsigned long r_symndx;
12383   struct elf_link_hash_entry *h;
12384 
12385   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12386   if (r_symndx == STN_UNDEF)
12387     return NULL;
12388 
12389   if (r_symndx >= cookie->locsymcount
12390       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12391     {
12392       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12393       if (h == NULL)
12394 	{
12395 	  info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12396 				  sec->owner);
12397 	  return NULL;
12398 	}
12399       while (h->root.type == bfd_link_hash_indirect
12400 	     || h->root.type == bfd_link_hash_warning)
12401 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
12402       h->mark = 1;
12403       /* If this symbol is weak and there is a non-weak definition, we
12404 	 keep the non-weak definition because many backends put
12405 	 dynamic reloc info on the non-weak definition for code
12406 	 handling copy relocs.  */
12407       if (h->u.weakdef != NULL)
12408 	h->u.weakdef->mark = 1;
12409 
12410       if (start_stop != NULL)
12411 	{
12412 	  /* To work around a glibc bug, mark all XXX input sections
12413 	     when there is an as yet undefined reference to __start_XXX
12414 	     or __stop_XXX symbols.  The linker will later define such
12415 	     symbols for orphan input sections that have a name
12416 	     representable as a C identifier.  */
12417 	  asection *s = _bfd_elf_is_start_stop (info, h);
12418 
12419 	  if (s != NULL)
12420 	    {
12421 	      *start_stop = !s->gc_mark;
12422 	      return s;
12423 	    }
12424 	}
12425 
12426       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12427     }
12428 
12429   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12430 			  &cookie->locsyms[r_symndx]);
12431 }
12432 
12433 /* COOKIE->rel describes a relocation against section SEC, which is
12434    a section we've decided to keep.  Mark the section that contains
12435    the relocation symbol.  */
12436 
12437 bfd_boolean
12438 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12439 			asection *sec,
12440 			elf_gc_mark_hook_fn gc_mark_hook,
12441 			struct elf_reloc_cookie *cookie)
12442 {
12443   asection *rsec;
12444   bfd_boolean start_stop = FALSE;
12445 
12446   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12447   while (rsec != NULL)
12448     {
12449       if (!rsec->gc_mark)
12450 	{
12451 	  if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12452 	      || (rsec->owner->flags & DYNAMIC) != 0)
12453 	    rsec->gc_mark = 1;
12454 	  else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12455 	    return FALSE;
12456 	}
12457       if (!start_stop)
12458 	break;
12459       rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12460     }
12461   return TRUE;
12462 }
12463 
12464 /* The mark phase of garbage collection.  For a given section, mark
12465    it and any sections in this section's group, and all the sections
12466    which define symbols to which it refers.  */
12467 
12468 bfd_boolean
12469 _bfd_elf_gc_mark (struct bfd_link_info *info,
12470 		  asection *sec,
12471 		  elf_gc_mark_hook_fn gc_mark_hook)
12472 {
12473   bfd_boolean ret;
12474   asection *group_sec, *eh_frame;
12475 
12476   sec->gc_mark = 1;
12477 
12478   /* Mark all the sections in the group.  */
12479   group_sec = elf_section_data (sec)->next_in_group;
12480   if (group_sec && !group_sec->gc_mark)
12481     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12482       return FALSE;
12483 
12484   /* Look through the section relocs.  */
12485   ret = TRUE;
12486   eh_frame = elf_eh_frame_section (sec->owner);
12487   if ((sec->flags & SEC_RELOC) != 0
12488       && sec->reloc_count > 0
12489       && sec != eh_frame)
12490     {
12491       struct elf_reloc_cookie cookie;
12492 
12493       if (!init_reloc_cookie_for_section (&cookie, info, sec))
12494 	ret = FALSE;
12495       else
12496 	{
12497 	  for (; cookie.rel < cookie.relend; cookie.rel++)
12498 	    if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12499 	      {
12500 		ret = FALSE;
12501 		break;
12502 	      }
12503 	  fini_reloc_cookie_for_section (&cookie, sec);
12504 	}
12505     }
12506 
12507   if (ret && eh_frame && elf_fde_list (sec))
12508     {
12509       struct elf_reloc_cookie cookie;
12510 
12511       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12512 	ret = FALSE;
12513       else
12514 	{
12515 	  if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12516 				      gc_mark_hook, &cookie))
12517 	    ret = FALSE;
12518 	  fini_reloc_cookie_for_section (&cookie, eh_frame);
12519 	}
12520     }
12521 
12522   eh_frame = elf_section_eh_frame_entry (sec);
12523   if (ret && eh_frame && !eh_frame->gc_mark)
12524     if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12525       ret = FALSE;
12526 
12527   return ret;
12528 }
12529 
12530 /* Scan and mark sections in a special or debug section group.  */
12531 
12532 static void
12533 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12534 {
12535   /* Point to first section of section group.  */
12536   asection *ssec;
12537   /* Used to iterate the section group.  */
12538   asection *msec;
12539 
12540   bfd_boolean is_special_grp = TRUE;
12541   bfd_boolean is_debug_grp = TRUE;
12542 
12543   /* First scan to see if group contains any section other than debug
12544      and special section.  */
12545   ssec = msec = elf_next_in_group (grp);
12546   do
12547     {
12548       if ((msec->flags & SEC_DEBUGGING) == 0)
12549 	is_debug_grp = FALSE;
12550 
12551       if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12552 	is_special_grp = FALSE;
12553 
12554       msec = elf_next_in_group (msec);
12555     }
12556   while (msec != ssec);
12557 
12558   /* If this is a pure debug section group or pure special section group,
12559      keep all sections in this group.  */
12560   if (is_debug_grp || is_special_grp)
12561     {
12562       do
12563 	{
12564 	  msec->gc_mark = 1;
12565 	  msec = elf_next_in_group (msec);
12566 	}
12567       while (msec != ssec);
12568     }
12569 }
12570 
12571 /* Keep debug and special sections.  */
12572 
12573 bfd_boolean
12574 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12575 				 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12576 {
12577   bfd *ibfd;
12578 
12579   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12580     {
12581       asection *isec;
12582       bfd_boolean some_kept;
12583       bfd_boolean debug_frag_seen;
12584 
12585       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12586 	continue;
12587 
12588       /* Ensure all linker created sections are kept,
12589 	 see if any other section is already marked,
12590 	 and note if we have any fragmented debug sections.  */
12591       debug_frag_seen = some_kept = FALSE;
12592       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12593 	{
12594 	  if ((isec->flags & SEC_LINKER_CREATED) != 0)
12595 	    isec->gc_mark = 1;
12596 	  else if (isec->gc_mark)
12597 	    some_kept = TRUE;
12598 
12599 	  if (debug_frag_seen == FALSE
12600 	      && (isec->flags & SEC_DEBUGGING)
12601 	      && CONST_STRNEQ (isec->name, ".debug_line."))
12602 	    debug_frag_seen = TRUE;
12603 	}
12604 
12605       /* If no section in this file will be kept, then we can
12606 	 toss out the debug and special sections.  */
12607       if (!some_kept)
12608 	continue;
12609 
12610       /* Keep debug and special sections like .comment when they are
12611 	 not part of a group.  Also keep section groups that contain
12612 	 just debug sections or special sections.  */
12613       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12614 	{
12615 	  if ((isec->flags & SEC_GROUP) != 0)
12616 	    _bfd_elf_gc_mark_debug_special_section_group (isec);
12617 	  else if (((isec->flags & SEC_DEBUGGING) != 0
12618 		    || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12619 		   && elf_next_in_group (isec) == NULL)
12620 	    isec->gc_mark = 1;
12621 	}
12622 
12623       if (! debug_frag_seen)
12624 	continue;
12625 
12626       /* Look for CODE sections which are going to be discarded,
12627 	 and find and discard any fragmented debug sections which
12628 	 are associated with that code section.  */
12629       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12630 	if ((isec->flags & SEC_CODE) != 0
12631 	    && isec->gc_mark == 0)
12632 	  {
12633 	    unsigned int ilen;
12634 	    asection *dsec;
12635 
12636 	    ilen = strlen (isec->name);
12637 
12638 	    /* Association is determined by the name of the debug section
12639 	       containing the name of the code section as a suffix.  For
12640 	       example .debug_line.text.foo is a debug section associated
12641 	       with .text.foo.  */
12642 	    for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12643 	      {
12644 		unsigned int dlen;
12645 
12646 		if (dsec->gc_mark == 0
12647 		    || (dsec->flags & SEC_DEBUGGING) == 0)
12648 		  continue;
12649 
12650 		dlen = strlen (dsec->name);
12651 
12652 		if (dlen > ilen
12653 		    && strncmp (dsec->name + (dlen - ilen),
12654 				isec->name, ilen) == 0)
12655 		  {
12656 		    dsec->gc_mark = 0;
12657 		  }
12658 	      }
12659 	  }
12660     }
12661   return TRUE;
12662 }
12663 
12664 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
12665 
12666 struct elf_gc_sweep_symbol_info
12667 {
12668   struct bfd_link_info *info;
12669   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12670 		       bfd_boolean);
12671 };
12672 
12673 static bfd_boolean
12674 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12675 {
12676   if (!h->mark
12677       && (((h->root.type == bfd_link_hash_defined
12678 	    || h->root.type == bfd_link_hash_defweak)
12679 	   && !((h->def_regular || ELF_COMMON_DEF_P (h))
12680 		&& h->root.u.def.section->gc_mark))
12681 	  || h->root.type == bfd_link_hash_undefined
12682 	  || h->root.type == bfd_link_hash_undefweak))
12683     {
12684       struct elf_gc_sweep_symbol_info *inf;
12685 
12686       inf = (struct elf_gc_sweep_symbol_info *) data;
12687       (*inf->hide_symbol) (inf->info, h, TRUE);
12688       h->def_regular = 0;
12689       h->ref_regular = 0;
12690       h->ref_regular_nonweak = 0;
12691     }
12692 
12693   return TRUE;
12694 }
12695 
12696 /* The sweep phase of garbage collection.  Remove all garbage sections.  */
12697 
12698 typedef bfd_boolean (*gc_sweep_hook_fn)
12699   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12700 
12701 static bfd_boolean
12702 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12703 {
12704   bfd *sub;
12705   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12706   gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12707   unsigned long section_sym_count;
12708   struct elf_gc_sweep_symbol_info sweep_info;
12709 
12710   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12711     {
12712       asection *o;
12713 
12714       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12715 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12716 	continue;
12717 
12718       for (o = sub->sections; o != NULL; o = o->next)
12719 	{
12720 	  /* When any section in a section group is kept, we keep all
12721 	     sections in the section group.  If the first member of
12722 	     the section group is excluded, we will also exclude the
12723 	     group section.  */
12724 	  if (o->flags & SEC_GROUP)
12725 	    {
12726 	      asection *first = elf_next_in_group (o);
12727 	      o->gc_mark = first->gc_mark;
12728 	    }
12729 
12730 	  if (o->gc_mark)
12731 	    continue;
12732 
12733 	  /* Skip sweeping sections already excluded.  */
12734 	  if (o->flags & SEC_EXCLUDE)
12735 	    continue;
12736 
12737 	  /* Since this is early in the link process, it is simple
12738 	     to remove a section from the output.  */
12739 	  o->flags |= SEC_EXCLUDE;
12740 
12741 	  if (info->print_gc_sections && o->size != 0)
12742 	    _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12743 
12744 	  /* But we also have to update some of the relocation
12745 	     info we collected before.  */
12746 	  if (gc_sweep_hook
12747 	      && (o->flags & SEC_RELOC) != 0
12748 	      && o->reloc_count != 0
12749 	      && !((info->strip == strip_all || info->strip == strip_debugger)
12750 		   && (o->flags & SEC_DEBUGGING) != 0)
12751 	      && !bfd_is_abs_section (o->output_section))
12752 	    {
12753 	      Elf_Internal_Rela *internal_relocs;
12754 	      bfd_boolean r;
12755 
12756 	      internal_relocs
12757 		= _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12758 					     info->keep_memory);
12759 	      if (internal_relocs == NULL)
12760 		return FALSE;
12761 
12762 	      r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12763 
12764 	      if (elf_section_data (o)->relocs != internal_relocs)
12765 		free (internal_relocs);
12766 
12767 	      if (!r)
12768 		return FALSE;
12769 	    }
12770 	}
12771     }
12772 
12773   /* Remove the symbols that were in the swept sections from the dynamic
12774      symbol table.  GCFIXME: Anyone know how to get them out of the
12775      static symbol table as well?  */
12776   sweep_info.info = info;
12777   sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12778   elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12779 			  &sweep_info);
12780 
12781   _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12782   return TRUE;
12783 }
12784 
12785 /* Propagate collected vtable information.  This is called through
12786    elf_link_hash_traverse.  */
12787 
12788 static bfd_boolean
12789 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12790 {
12791   /* Those that are not vtables.  */
12792   if (h->vtable == NULL || h->vtable->parent == NULL)
12793     return TRUE;
12794 
12795   /* Those vtables that do not have parents, we cannot merge.  */
12796   if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12797     return TRUE;
12798 
12799   /* If we've already been done, exit.  */
12800   if (h->vtable->used && h->vtable->used[-1])
12801     return TRUE;
12802 
12803   /* Make sure the parent's table is up to date.  */
12804   elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12805 
12806   if (h->vtable->used == NULL)
12807     {
12808       /* None of this table's entries were referenced.  Re-use the
12809 	 parent's table.  */
12810       h->vtable->used = h->vtable->parent->vtable->used;
12811       h->vtable->size = h->vtable->parent->vtable->size;
12812     }
12813   else
12814     {
12815       size_t n;
12816       bfd_boolean *cu, *pu;
12817 
12818       /* Or the parent's entries into ours.  */
12819       cu = h->vtable->used;
12820       cu[-1] = TRUE;
12821       pu = h->vtable->parent->vtable->used;
12822       if (pu != NULL)
12823 	{
12824 	  const struct elf_backend_data *bed;
12825 	  unsigned int log_file_align;
12826 
12827 	  bed = get_elf_backend_data (h->root.u.def.section->owner);
12828 	  log_file_align = bed->s->log_file_align;
12829 	  n = h->vtable->parent->vtable->size >> log_file_align;
12830 	  while (n--)
12831 	    {
12832 	      if (*pu)
12833 		*cu = TRUE;
12834 	      pu++;
12835 	      cu++;
12836 	    }
12837 	}
12838     }
12839 
12840   return TRUE;
12841 }
12842 
12843 static bfd_boolean
12844 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12845 {
12846   asection *sec;
12847   bfd_vma hstart, hend;
12848   Elf_Internal_Rela *relstart, *relend, *rel;
12849   const struct elf_backend_data *bed;
12850   unsigned int log_file_align;
12851 
12852   /* Take care of both those symbols that do not describe vtables as
12853      well as those that are not loaded.  */
12854   if (h->vtable == NULL || h->vtable->parent == NULL)
12855     return TRUE;
12856 
12857   BFD_ASSERT (h->root.type == bfd_link_hash_defined
12858 	      || h->root.type == bfd_link_hash_defweak);
12859 
12860   sec = h->root.u.def.section;
12861   hstart = h->root.u.def.value;
12862   hend = hstart + h->size;
12863 
12864   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12865   if (!relstart)
12866     return *(bfd_boolean *) okp = FALSE;
12867   bed = get_elf_backend_data (sec->owner);
12868   log_file_align = bed->s->log_file_align;
12869 
12870   relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12871 
12872   for (rel = relstart; rel < relend; ++rel)
12873     if (rel->r_offset >= hstart && rel->r_offset < hend)
12874       {
12875 	/* If the entry is in use, do nothing.  */
12876 	if (h->vtable->used
12877 	    && (rel->r_offset - hstart) < h->vtable->size)
12878 	  {
12879 	    bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12880 	    if (h->vtable->used[entry])
12881 	      continue;
12882 	  }
12883 	/* Otherwise, kill it.  */
12884 	rel->r_offset = rel->r_info = rel->r_addend = 0;
12885       }
12886 
12887   return TRUE;
12888 }
12889 
12890 /* Mark sections containing dynamically referenced symbols.  When
12891    building shared libraries, we must assume that any visible symbol is
12892    referenced.  */
12893 
12894 bfd_boolean
12895 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12896 {
12897   struct bfd_link_info *info = (struct bfd_link_info *) inf;
12898   struct bfd_elf_dynamic_list *d = info->dynamic_list;
12899 
12900   if ((h->root.type == bfd_link_hash_defined
12901        || h->root.type == bfd_link_hash_defweak)
12902       && (h->ref_dynamic
12903 	  || ((h->def_regular || ELF_COMMON_DEF_P (h))
12904 	      && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12905 	      && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12906 	      && (!bfd_link_executable (info)
12907 		  || info->export_dynamic
12908 		  || (h->dynamic
12909 		      && d != NULL
12910 		      && (*d->match) (&d->head, NULL, h->root.root.string)))
12911 	      && (h->versioned >= versioned
12912 		  || !bfd_hide_sym_by_version (info->version_info,
12913 					       h->root.root.string)))))
12914     h->root.u.def.section->flags |= SEC_KEEP;
12915 
12916   return TRUE;
12917 }
12918 
12919 /* Keep all sections containing symbols undefined on the command-line,
12920    and the section containing the entry symbol.  */
12921 
12922 void
12923 _bfd_elf_gc_keep (struct bfd_link_info *info)
12924 {
12925   struct bfd_sym_chain *sym;
12926 
12927   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12928     {
12929       struct elf_link_hash_entry *h;
12930 
12931       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12932 				FALSE, FALSE, FALSE);
12933 
12934       if (h != NULL
12935 	  && (h->root.type == bfd_link_hash_defined
12936 	      || h->root.type == bfd_link_hash_defweak)
12937 	  && !bfd_is_abs_section (h->root.u.def.section))
12938 	h->root.u.def.section->flags |= SEC_KEEP;
12939     }
12940 }
12941 
12942 bfd_boolean
12943 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12944 				struct bfd_link_info *info)
12945 {
12946   bfd *ibfd = info->input_bfds;
12947 
12948   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12949     {
12950       asection *sec;
12951       struct elf_reloc_cookie cookie;
12952 
12953       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12954 	continue;
12955 
12956       if (!init_reloc_cookie (&cookie, info, ibfd))
12957 	return FALSE;
12958 
12959       for (sec = ibfd->sections; sec; sec = sec->next)
12960 	{
12961 	  if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12962 	      && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12963 	    {
12964 	      _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12965 	      fini_reloc_cookie_rels (&cookie, sec);
12966 	    }
12967 	}
12968     }
12969   return TRUE;
12970 }
12971 
12972 /* Do mark and sweep of unused sections.  */
12973 
12974 bfd_boolean
12975 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12976 {
12977   bfd_boolean ok = TRUE;
12978   bfd *sub;
12979   elf_gc_mark_hook_fn gc_mark_hook;
12980   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12981   struct elf_link_hash_table *htab;
12982 
12983   if (!bed->can_gc_sections
12984       || !is_elf_hash_table (info->hash))
12985     {
12986       (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12987       return TRUE;
12988     }
12989 
12990   bed->gc_keep (info);
12991   htab = elf_hash_table (info);
12992 
12993   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
12994      at the .eh_frame section if we can mark the FDEs individually.  */
12995   for (sub = info->input_bfds;
12996        info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12997        sub = sub->link.next)
12998     {
12999       asection *sec;
13000       struct elf_reloc_cookie cookie;
13001 
13002       sec = bfd_get_section_by_name (sub, ".eh_frame");
13003       while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13004 	{
13005 	  _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13006 	  if (elf_section_data (sec)->sec_info
13007 	      && (sec->flags & SEC_LINKER_CREATED) == 0)
13008 	    elf_eh_frame_section (sub) = sec;
13009 	  fini_reloc_cookie_for_section (&cookie, sec);
13010 	  sec = bfd_get_next_section_by_name (NULL, sec);
13011 	}
13012     }
13013 
13014   /* Apply transitive closure to the vtable entry usage info.  */
13015   elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13016   if (!ok)
13017     return FALSE;
13018 
13019   /* Kill the vtable relocations that were not used.  */
13020   elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13021   if (!ok)
13022     return FALSE;
13023 
13024   /* Mark dynamically referenced symbols.  */
13025   if (htab->dynamic_sections_created)
13026     elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13027 
13028   /* Grovel through relocs to find out who stays ...  */
13029   gc_mark_hook = bed->gc_mark_hook;
13030   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13031     {
13032       asection *o;
13033 
13034       if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13035 	  || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13036 	continue;
13037 
13038       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13039 	 Also treat note sections as a root, if the section is not part
13040 	 of a group.  */
13041       for (o = sub->sections; o != NULL; o = o->next)
13042 	if (!o->gc_mark
13043 	    && (o->flags & SEC_EXCLUDE) == 0
13044 	    && ((o->flags & SEC_KEEP) != 0
13045 		|| (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13046 		    && elf_next_in_group (o) == NULL )))
13047 	  {
13048 	    if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13049 	      return FALSE;
13050 	  }
13051     }
13052 
13053   /* Allow the backend to mark additional target specific sections.  */
13054   bed->gc_mark_extra_sections (info, gc_mark_hook);
13055 
13056   /* ... and mark SEC_EXCLUDE for those that go.  */
13057   return elf_gc_sweep (abfd, info);
13058 }
13059 
13060 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
13061 
13062 bfd_boolean
13063 bfd_elf_gc_record_vtinherit (bfd *abfd,
13064 			     asection *sec,
13065 			     struct elf_link_hash_entry *h,
13066 			     bfd_vma offset)
13067 {
13068   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13069   struct elf_link_hash_entry **search, *child;
13070   size_t extsymcount;
13071   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13072 
13073   /* The sh_info field of the symtab header tells us where the
13074      external symbols start.  We don't care about the local symbols at
13075      this point.  */
13076   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13077   if (!elf_bad_symtab (abfd))
13078     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13079 
13080   sym_hashes = elf_sym_hashes (abfd);
13081   sym_hashes_end = sym_hashes + extsymcount;
13082 
13083   /* Hunt down the child symbol, which is in this section at the same
13084      offset as the relocation.  */
13085   for (search = sym_hashes; search != sym_hashes_end; ++search)
13086     {
13087       if ((child = *search) != NULL
13088 	  && (child->root.type == bfd_link_hash_defined
13089 	      || child->root.type == bfd_link_hash_defweak)
13090 	  && child->root.u.def.section == sec
13091 	  && child->root.u.def.value == offset)
13092 	goto win;
13093     }
13094 
13095   (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
13096 			 abfd, sec, (unsigned long) offset);
13097   bfd_set_error (bfd_error_invalid_operation);
13098   return FALSE;
13099 
13100  win:
13101   if (!child->vtable)
13102     {
13103       child->vtable = ((struct elf_link_virtual_table_entry *)
13104 		       bfd_zalloc (abfd, sizeof (*child->vtable)));
13105       if (!child->vtable)
13106 	return FALSE;
13107     }
13108   if (!h)
13109     {
13110       /* This *should* only be the absolute section.  It could potentially
13111 	 be that someone has defined a non-global vtable though, which
13112 	 would be bad.  It isn't worth paging in the local symbols to be
13113 	 sure though; that case should simply be handled by the assembler.  */
13114 
13115       child->vtable->parent = (struct elf_link_hash_entry *) -1;
13116     }
13117   else
13118     child->vtable->parent = h;
13119 
13120   return TRUE;
13121 }
13122 
13123 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
13124 
13125 bfd_boolean
13126 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13127 			   asection *sec ATTRIBUTE_UNUSED,
13128 			   struct elf_link_hash_entry *h,
13129 			   bfd_vma addend)
13130 {
13131   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13132   unsigned int log_file_align = bed->s->log_file_align;
13133 
13134   if (!h->vtable)
13135     {
13136       h->vtable = ((struct elf_link_virtual_table_entry *)
13137 		   bfd_zalloc (abfd, sizeof (*h->vtable)));
13138       if (!h->vtable)
13139 	return FALSE;
13140     }
13141 
13142   if (addend >= h->vtable->size)
13143     {
13144       size_t size, bytes, file_align;
13145       bfd_boolean *ptr = h->vtable->used;
13146 
13147       /* While the symbol is undefined, we have to be prepared to handle
13148 	 a zero size.  */
13149       file_align = 1 << log_file_align;
13150       if (h->root.type == bfd_link_hash_undefined)
13151 	size = addend + file_align;
13152       else
13153 	{
13154 	  size = h->size;
13155 	  if (addend >= size)
13156 	    {
13157 	      /* Oops!  We've got a reference past the defined end of
13158 		 the table.  This is probably a bug -- shall we warn?  */
13159 	      size = addend + file_align;
13160 	    }
13161 	}
13162       size = (size + file_align - 1) & -file_align;
13163 
13164       /* Allocate one extra entry for use as a "done" flag for the
13165 	 consolidation pass.  */
13166       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13167 
13168       if (ptr)
13169 	{
13170 	  ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13171 
13172 	  if (ptr != NULL)
13173 	    {
13174 	      size_t oldbytes;
13175 
13176 	      oldbytes = (((h->vtable->size >> log_file_align) + 1)
13177 			  * sizeof (bfd_boolean));
13178 	      memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13179 	    }
13180 	}
13181       else
13182 	ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13183 
13184       if (ptr == NULL)
13185 	return FALSE;
13186 
13187       /* And arrange for that done flag to be at index -1.  */
13188       h->vtable->used = ptr + 1;
13189       h->vtable->size = size;
13190     }
13191 
13192   h->vtable->used[addend >> log_file_align] = TRUE;
13193 
13194   return TRUE;
13195 }
13196 
13197 /* Map an ELF section header flag to its corresponding string.  */
13198 typedef struct
13199 {
13200   char *flag_name;
13201   flagword flag_value;
13202 } elf_flags_to_name_table;
13203 
13204 static elf_flags_to_name_table elf_flags_to_names [] =
13205 {
13206   { "SHF_WRITE", SHF_WRITE },
13207   { "SHF_ALLOC", SHF_ALLOC },
13208   { "SHF_EXECINSTR", SHF_EXECINSTR },
13209   { "SHF_MERGE", SHF_MERGE },
13210   { "SHF_STRINGS", SHF_STRINGS },
13211   { "SHF_INFO_LINK", SHF_INFO_LINK},
13212   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13213   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13214   { "SHF_GROUP", SHF_GROUP },
13215   { "SHF_TLS", SHF_TLS },
13216   { "SHF_MASKOS", SHF_MASKOS },
13217   { "SHF_EXCLUDE", SHF_EXCLUDE },
13218 };
13219 
13220 /* Returns TRUE if the section is to be included, otherwise FALSE.  */
13221 bfd_boolean
13222 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13223 			      struct flag_info *flaginfo,
13224 			      asection *section)
13225 {
13226   const bfd_vma sh_flags = elf_section_flags (section);
13227 
13228   if (!flaginfo->flags_initialized)
13229     {
13230       bfd *obfd = info->output_bfd;
13231       const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13232       struct flag_info_list *tf = flaginfo->flag_list;
13233       int with_hex = 0;
13234       int without_hex = 0;
13235 
13236       for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13237 	{
13238 	  unsigned i;
13239 	  flagword (*lookup) (char *);
13240 
13241 	  lookup = bed->elf_backend_lookup_section_flags_hook;
13242 	  if (lookup != NULL)
13243 	    {
13244 	      flagword hexval = (*lookup) ((char *) tf->name);
13245 
13246 	      if (hexval != 0)
13247 		{
13248 		  if (tf->with == with_flags)
13249 		    with_hex |= hexval;
13250 		  else if (tf->with == without_flags)
13251 		    without_hex |= hexval;
13252 		  tf->valid = TRUE;
13253 		  continue;
13254 		}
13255 	    }
13256 	  for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13257 	    {
13258 	      if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13259 		{
13260 		  if (tf->with == with_flags)
13261 		    with_hex |= elf_flags_to_names[i].flag_value;
13262 		  else if (tf->with == without_flags)
13263 		    without_hex |= elf_flags_to_names[i].flag_value;
13264 		  tf->valid = TRUE;
13265 		  break;
13266 		}
13267 	    }
13268 	  if (!tf->valid)
13269 	    {
13270 	      info->callbacks->einfo
13271 		(_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13272 	      return FALSE;
13273 	    }
13274 	}
13275       flaginfo->flags_initialized = TRUE;
13276       flaginfo->only_with_flags |= with_hex;
13277       flaginfo->not_with_flags |= without_hex;
13278     }
13279 
13280   if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13281     return FALSE;
13282 
13283   if ((flaginfo->not_with_flags & sh_flags) != 0)
13284     return FALSE;
13285 
13286   return TRUE;
13287 }
13288 
13289 struct alloc_got_off_arg {
13290   bfd_vma gotoff;
13291   struct bfd_link_info *info;
13292 };
13293 
13294 /* We need a special top-level link routine to convert got reference counts
13295    to real got offsets.  */
13296 
13297 static bfd_boolean
13298 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13299 {
13300   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13301   bfd *obfd = gofarg->info->output_bfd;
13302   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13303 
13304   if (h->got.refcount > 0)
13305     {
13306       h->got.offset = gofarg->gotoff;
13307       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13308     }
13309   else
13310     h->got.offset = (bfd_vma) -1;
13311 
13312   return TRUE;
13313 }
13314 
13315 /* And an accompanying bit to work out final got entry offsets once
13316    we're done.  Should be called from final_link.  */
13317 
13318 bfd_boolean
13319 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13320 					struct bfd_link_info *info)
13321 {
13322   bfd *i;
13323   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13324   bfd_vma gotoff;
13325   struct alloc_got_off_arg gofarg;
13326 
13327   BFD_ASSERT (abfd == info->output_bfd);
13328 
13329   if (! is_elf_hash_table (info->hash))
13330     return FALSE;
13331 
13332   /* The GOT offset is relative to the .got section, but the GOT header is
13333      put into the .got.plt section, if the backend uses it.  */
13334   if (bed->want_got_plt)
13335     gotoff = 0;
13336   else
13337     gotoff = bed->got_header_size;
13338 
13339   /* Do the local .got entries first.  */
13340   for (i = info->input_bfds; i; i = i->link.next)
13341     {
13342       bfd_signed_vma *local_got;
13343       size_t j, locsymcount;
13344       Elf_Internal_Shdr *symtab_hdr;
13345 
13346       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13347 	continue;
13348 
13349       local_got = elf_local_got_refcounts (i);
13350       if (!local_got)
13351 	continue;
13352 
13353       symtab_hdr = &elf_tdata (i)->symtab_hdr;
13354       if (elf_bad_symtab (i))
13355 	locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13356       else
13357 	locsymcount = symtab_hdr->sh_info;
13358 
13359       for (j = 0; j < locsymcount; ++j)
13360 	{
13361 	  if (local_got[j] > 0)
13362 	    {
13363 	      local_got[j] = gotoff;
13364 	      gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13365 	    }
13366 	  else
13367 	    local_got[j] = (bfd_vma) -1;
13368 	}
13369     }
13370 
13371   /* Then the global .got entries.  .plt refcounts are handled by
13372      adjust_dynamic_symbol  */
13373   gofarg.gotoff = gotoff;
13374   gofarg.info = info;
13375   elf_link_hash_traverse (elf_hash_table (info),
13376 			  elf_gc_allocate_got_offsets,
13377 			  &gofarg);
13378   return TRUE;
13379 }
13380 
13381 /* Many folk need no more in the way of final link than this, once
13382    got entry reference counting is enabled.  */
13383 
13384 bfd_boolean
13385 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13386 {
13387   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13388     return FALSE;
13389 
13390   /* Invoke the regular ELF backend linker to do all the work.  */
13391   return bfd_elf_final_link (abfd, info);
13392 }
13393 
13394 bfd_boolean
13395 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13396 {
13397   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13398 
13399   if (rcookie->bad_symtab)
13400     rcookie->rel = rcookie->rels;
13401 
13402   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13403     {
13404       unsigned long r_symndx;
13405 
13406       if (! rcookie->bad_symtab)
13407 	if (rcookie->rel->r_offset > offset)
13408 	  return FALSE;
13409       if (rcookie->rel->r_offset != offset)
13410 	continue;
13411 
13412       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13413       if (r_symndx == STN_UNDEF)
13414 	return TRUE;
13415 
13416       if (r_symndx >= rcookie->locsymcount
13417 	  || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13418 	{
13419 	  struct elf_link_hash_entry *h;
13420 
13421 	  h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13422 
13423 	  while (h->root.type == bfd_link_hash_indirect
13424 		 || h->root.type == bfd_link_hash_warning)
13425 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
13426 
13427 	  if ((h->root.type == bfd_link_hash_defined
13428 	       || h->root.type == bfd_link_hash_defweak)
13429 	      && (h->root.u.def.section->owner != rcookie->abfd
13430 		  || h->root.u.def.section->kept_section != NULL
13431 		  || discarded_section (h->root.u.def.section)))
13432 	    return TRUE;
13433 	}
13434       else
13435 	{
13436 	  /* It's not a relocation against a global symbol,
13437 	     but it could be a relocation against a local
13438 	     symbol for a discarded section.  */
13439 	  asection *isec;
13440 	  Elf_Internal_Sym *isym;
13441 
13442 	  /* Need to: get the symbol; get the section.  */
13443 	  isym = &rcookie->locsyms[r_symndx];
13444 	  isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13445 	  if (isec != NULL
13446 	      && (isec->kept_section != NULL
13447 		  || discarded_section (isec)))
13448 	    return TRUE;
13449 	}
13450       return FALSE;
13451     }
13452   return FALSE;
13453 }
13454 
13455 /* Discard unneeded references to discarded sections.
13456    Returns -1 on error, 1 if any section's size was changed, 0 if
13457    nothing changed.  This function assumes that the relocations are in
13458    sorted order, which is true for all known assemblers.  */
13459 
13460 int
13461 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13462 {
13463   struct elf_reloc_cookie cookie;
13464   asection *o;
13465   bfd *abfd;
13466   int changed = 0;
13467 
13468   if (info->traditional_format
13469       || !is_elf_hash_table (info->hash))
13470     return 0;
13471 
13472   o = bfd_get_section_by_name (output_bfd, ".stab");
13473   if (o != NULL)
13474     {
13475       asection *i;
13476 
13477       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13478 	{
13479 	  if (i->size == 0
13480 	      || i->reloc_count == 0
13481 	      || i->sec_info_type != SEC_INFO_TYPE_STABS)
13482 	    continue;
13483 
13484 	  abfd = i->owner;
13485 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13486 	    continue;
13487 
13488 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
13489 	    return -1;
13490 
13491 	  if (_bfd_discard_section_stabs (abfd, i,
13492 					  elf_section_data (i)->sec_info,
13493 					  bfd_elf_reloc_symbol_deleted_p,
13494 					  &cookie))
13495 	    changed = 1;
13496 
13497 	  fini_reloc_cookie_for_section (&cookie, i);
13498 	}
13499     }
13500 
13501   o = NULL;
13502   if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13503     o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13504   if (o != NULL)
13505     {
13506       asection *i;
13507 
13508       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13509 	{
13510 	  if (i->size == 0)
13511 	    continue;
13512 
13513 	  abfd = i->owner;
13514 	  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13515 	    continue;
13516 
13517 	  if (!init_reloc_cookie_for_section (&cookie, info, i))
13518 	    return -1;
13519 
13520 	  _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13521 	  if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13522 						 bfd_elf_reloc_symbol_deleted_p,
13523 						 &cookie))
13524 	    changed = 1;
13525 
13526 	  fini_reloc_cookie_for_section (&cookie, i);
13527 	}
13528     }
13529 
13530   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13531     {
13532       const struct elf_backend_data *bed;
13533 
13534       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13535 	continue;
13536 
13537       bed = get_elf_backend_data (abfd);
13538 
13539       if (bed->elf_backend_discard_info != NULL)
13540 	{
13541 	  if (!init_reloc_cookie (&cookie, info, abfd))
13542 	    return -1;
13543 
13544 	  if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13545 	    changed = 1;
13546 
13547 	  fini_reloc_cookie (&cookie, abfd);
13548 	}
13549     }
13550 
13551   if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13552     _bfd_elf_end_eh_frame_parsing (info);
13553 
13554   if (info->eh_frame_hdr_type
13555       && !bfd_link_relocatable (info)
13556       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13557     changed = 1;
13558 
13559   return changed;
13560 }
13561 
13562 bfd_boolean
13563 _bfd_elf_section_already_linked (bfd *abfd,
13564 				 asection *sec,
13565 				 struct bfd_link_info *info)
13566 {
13567   flagword flags;
13568   const char *name, *key;
13569   struct bfd_section_already_linked *l;
13570   struct bfd_section_already_linked_hash_entry *already_linked_list;
13571 
13572   if (sec->output_section == bfd_abs_section_ptr)
13573     return FALSE;
13574 
13575   flags = sec->flags;
13576 
13577   /* Return if it isn't a linkonce section.  A comdat group section
13578      also has SEC_LINK_ONCE set.  */
13579   if ((flags & SEC_LINK_ONCE) == 0)
13580     return FALSE;
13581 
13582   /* Don't put group member sections on our list of already linked
13583      sections.  They are handled as a group via their group section.  */
13584   if (elf_sec_group (sec) != NULL)
13585     return FALSE;
13586 
13587   /* For a SHT_GROUP section, use the group signature as the key.  */
13588   name = sec->name;
13589   if ((flags & SEC_GROUP) != 0
13590       && elf_next_in_group (sec) != NULL
13591       && elf_group_name (elf_next_in_group (sec)) != NULL)
13592     key = elf_group_name (elf_next_in_group (sec));
13593   else
13594     {
13595       /* Otherwise we should have a .gnu.linkonce.<type>.<key> section.  */
13596       if (CONST_STRNEQ (name, ".gnu.linkonce.")
13597 	  && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13598 	key++;
13599       else
13600 	/* Must be a user linkonce section that doesn't follow gcc's
13601 	   naming convention.  In this case we won't be matching
13602 	   single member groups.  */
13603 	key = name;
13604     }
13605 
13606   already_linked_list = bfd_section_already_linked_table_lookup (key);
13607 
13608   for (l = already_linked_list->entry; l != NULL; l = l->next)
13609     {
13610       /* We may have 2 different types of sections on the list: group
13611 	 sections with a signature of <key> (<key> is some string),
13612 	 and linkonce sections named .gnu.linkonce.<type>.<key>.
13613 	 Match like sections.  LTO plugin sections are an exception.
13614 	 They are always named .gnu.linkonce.t.<key> and match either
13615 	 type of section.  */
13616       if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13617 	   && ((flags & SEC_GROUP) != 0
13618 	       || strcmp (name, l->sec->name) == 0))
13619 	  || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13620 	{
13621 	  /* The section has already been linked.  See if we should
13622 	     issue a warning.  */
13623 	  if (!_bfd_handle_already_linked (sec, l, info))
13624 	    return FALSE;
13625 
13626 	  if (flags & SEC_GROUP)
13627 	    {
13628 	      asection *first = elf_next_in_group (sec);
13629 	      asection *s = first;
13630 
13631 	      while (s != NULL)
13632 		{
13633 		  s->output_section = bfd_abs_section_ptr;
13634 		  /* Record which group discards it.  */
13635 		  s->kept_section = l->sec;
13636 		  s = elf_next_in_group (s);
13637 		  /* These lists are circular.  */
13638 		  if (s == first)
13639 		    break;
13640 		}
13641 	    }
13642 
13643 	  return TRUE;
13644 	}
13645     }
13646 
13647   /* A single member comdat group section may be discarded by a
13648      linkonce section and vice versa.  */
13649   if ((flags & SEC_GROUP) != 0)
13650     {
13651       asection *first = elf_next_in_group (sec);
13652 
13653       if (first != NULL && elf_next_in_group (first) == first)
13654 	/* Check this single member group against linkonce sections.  */
13655 	for (l = already_linked_list->entry; l != NULL; l = l->next)
13656 	  if ((l->sec->flags & SEC_GROUP) == 0
13657 	      && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13658 	    {
13659 	      first->output_section = bfd_abs_section_ptr;
13660 	      first->kept_section = l->sec;
13661 	      sec->output_section = bfd_abs_section_ptr;
13662 	      break;
13663 	    }
13664     }
13665   else
13666     /* Check this linkonce section against single member groups.  */
13667     for (l = already_linked_list->entry; l != NULL; l = l->next)
13668       if (l->sec->flags & SEC_GROUP)
13669 	{
13670 	  asection *first = elf_next_in_group (l->sec);
13671 
13672 	  if (first != NULL
13673 	      && elf_next_in_group (first) == first
13674 	      && bfd_elf_match_symbols_in_sections (first, sec, info))
13675 	    {
13676 	      sec->output_section = bfd_abs_section_ptr;
13677 	      sec->kept_section = first;
13678 	      break;
13679 	    }
13680 	}
13681 
13682   /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13683      referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13684      specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13685      prefix) instead.  `.gnu.linkonce.r.*' were the `.rodata' part of its
13686      matching `.gnu.linkonce.t.*'.  If `.gnu.linkonce.r.F' is not discarded
13687      but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13688      `.gnu.linkonce.t.F' section from a different bfd not requiring any
13689      `.gnu.linkonce.r.F'.  Thus `.gnu.linkonce.r.F' should be discarded.
13690      The reverse order cannot happen as there is never a bfd with only the
13691      `.gnu.linkonce.r.F' section.  The order of sections in a bfd does not
13692      matter as here were are looking only for cross-bfd sections.  */
13693 
13694   if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13695     for (l = already_linked_list->entry; l != NULL; l = l->next)
13696       if ((l->sec->flags & SEC_GROUP) == 0
13697 	  && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13698 	{
13699 	  if (abfd != l->sec->owner)
13700 	    sec->output_section = bfd_abs_section_ptr;
13701 	  break;
13702 	}
13703 
13704   /* This is the first section with this name.  Record it.  */
13705   if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13706     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13707   return sec->output_section == bfd_abs_section_ptr;
13708 }
13709 
13710 bfd_boolean
13711 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13712 {
13713   return sym->st_shndx == SHN_COMMON;
13714 }
13715 
13716 unsigned int
13717 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13718 {
13719   return SHN_COMMON;
13720 }
13721 
13722 asection *
13723 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13724 {
13725   return bfd_com_section_ptr;
13726 }
13727 
13728 bfd_vma
13729 _bfd_elf_default_got_elt_size (bfd *abfd,
13730 			       struct bfd_link_info *info ATTRIBUTE_UNUSED,
13731 			       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13732 			       bfd *ibfd ATTRIBUTE_UNUSED,
13733 			       unsigned long symndx ATTRIBUTE_UNUSED)
13734 {
13735   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13736   return bed->s->arch_size / 8;
13737 }
13738 
13739 /* Routines to support the creation of dynamic relocs.  */
13740 
13741 /* Returns the name of the dynamic reloc section associated with SEC.  */
13742 
13743 static const char *
13744 get_dynamic_reloc_section_name (bfd *       abfd,
13745 				asection *  sec,
13746 				bfd_boolean is_rela)
13747 {
13748   char *name;
13749   const char *old_name = bfd_get_section_name (NULL, sec);
13750   const char *prefix = is_rela ? ".rela" : ".rel";
13751 
13752   if (old_name == NULL)
13753     return NULL;
13754 
13755   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13756   sprintf (name, "%s%s", prefix, old_name);
13757 
13758   return name;
13759 }
13760 
13761 /* Returns the dynamic reloc section associated with SEC.
13762    If necessary compute the name of the dynamic reloc section based
13763    on SEC's name (looked up in ABFD's string table) and the setting
13764    of IS_RELA.  */
13765 
13766 asection *
13767 _bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
13768 				    asection *  sec,
13769 				    bfd_boolean is_rela)
13770 {
13771   asection * reloc_sec = elf_section_data (sec)->sreloc;
13772 
13773   if (reloc_sec == NULL)
13774     {
13775       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13776 
13777       if (name != NULL)
13778 	{
13779 	  reloc_sec = bfd_get_linker_section (abfd, name);
13780 
13781 	  if (reloc_sec != NULL)
13782 	    elf_section_data (sec)->sreloc = reloc_sec;
13783 	}
13784     }
13785 
13786   return reloc_sec;
13787 }
13788 
13789 /* Returns the dynamic reloc section associated with SEC.  If the
13790    section does not exist it is created and attached to the DYNOBJ
13791    bfd and stored in the SRELOC field of SEC's elf_section_data
13792    structure.
13793 
13794    ALIGNMENT is the alignment for the newly created section and
13795    IS_RELA defines whether the name should be .rela.<SEC's name>
13796    or .rel.<SEC's name>.  The section name is looked up in the
13797    string table associated with ABFD.  */
13798 
13799 asection *
13800 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13801 				     bfd *dynobj,
13802 				     unsigned int alignment,
13803 				     bfd *abfd,
13804 				     bfd_boolean is_rela)
13805 {
13806   asection * reloc_sec = elf_section_data (sec)->sreloc;
13807 
13808   if (reloc_sec == NULL)
13809     {
13810       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13811 
13812       if (name == NULL)
13813 	return NULL;
13814 
13815       reloc_sec = bfd_get_linker_section (dynobj, name);
13816 
13817       if (reloc_sec == NULL)
13818 	{
13819 	  flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13820 			    | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13821 	  if ((sec->flags & SEC_ALLOC) != 0)
13822 	    flags |= SEC_ALLOC | SEC_LOAD;
13823 
13824 	  reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13825 	  if (reloc_sec != NULL)
13826 	    {
13827 	      /* _bfd_elf_get_sec_type_attr chooses a section type by
13828 		 name.  Override as it may be wrong, eg. for a user
13829 		 section named "auto" we'll get ".relauto" which is
13830 		 seen to be a .rela section.  */
13831 	      elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13832 	      if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13833 		reloc_sec = NULL;
13834 	    }
13835 	}
13836 
13837       elf_section_data (sec)->sreloc = reloc_sec;
13838     }
13839 
13840   return reloc_sec;
13841 }
13842 
13843 /* Copy the ELF symbol type and other attributes for a linker script
13844    assignment from HSRC to HDEST.  Generally this should be treated as
13845    if we found a strong non-dynamic definition for HDEST (except that
13846    ld ignores multiple definition errors).  */
13847 void
13848 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13849 				     struct bfd_link_hash_entry *hdest,
13850 				     struct bfd_link_hash_entry *hsrc)
13851 {
13852   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13853   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13854   Elf_Internal_Sym isym;
13855 
13856   ehdest->type = ehsrc->type;
13857   ehdest->target_internal = ehsrc->target_internal;
13858 
13859   isym.st_other = ehsrc->other;
13860   elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13861 }
13862 
13863 /* Append a RELA relocation REL to section S in BFD.  */
13864 
13865 void
13866 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13867 {
13868   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13869   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13870   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13871   bed->s->swap_reloca_out (abfd, rel, loc);
13872 }
13873 
13874 /* Append a REL relocation REL to section S in BFD.  */
13875 
13876 void
13877 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13878 {
13879   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13880   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13881   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13882   bed->s->swap_reloc_out (abfd, rel, loc);
13883 }
13884