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