include/ChangeLog
[platform/upstream/binutils.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3    2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5
6    This file is part of BFD, the Binary File Descriptor library.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #define ARCH_SIZE 0
28 #include "elf-bfd.h"
29 #include "safe-ctype.h"
30 #include "libiberty.h"
31 #include "objalloc.h"
32
33 /* This struct is used to pass information to routines called via
34    elf_link_hash_traverse which must return failure.  */
35
36 struct elf_info_failed
37 {
38   struct bfd_link_info *info;
39   struct bfd_elf_version_tree *verdefs;
40   bfd_boolean failed;
41 };
42
43 /* This structure is used to pass information to
44    _bfd_elf_link_find_version_dependencies.  */
45
46 struct elf_find_verdep_info
47 {
48   /* General link information.  */
49   struct bfd_link_info *info;
50   /* The number of dependencies.  */
51   unsigned int vers;
52   /* Whether we had a failure.  */
53   bfd_boolean failed;
54 };
55
56 static bfd_boolean _bfd_elf_fix_symbol_flags
57   (struct elf_link_hash_entry *, struct elf_info_failed *);
58
59 /* Define a symbol in a dynamic linkage section.  */
60
61 struct elf_link_hash_entry *
62 _bfd_elf_define_linkage_sym (bfd *abfd,
63                              struct bfd_link_info *info,
64                              asection *sec,
65                              const char *name)
66 {
67   struct elf_link_hash_entry *h;
68   struct bfd_link_hash_entry *bh;
69   const struct elf_backend_data *bed;
70
71   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
72   if (h != NULL)
73     {
74       /* Zap symbol defined in an as-needed lib that wasn't linked.
75          This is a symptom of a larger problem:  Absolute symbols
76          defined in shared libraries can't be overridden, because we
77          lose the link to the bfd which is via the symbol section.  */
78       h->root.type = bfd_link_hash_new;
79     }
80
81   bh = &h->root;
82   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
83                                          sec, 0, NULL, FALSE,
84                                          get_elf_backend_data (abfd)->collect,
85                                          &bh))
86     return NULL;
87   h = (struct elf_link_hash_entry *) bh;
88   h->def_regular = 1;
89   h->non_elf = 0;
90   h->type = STT_OBJECT;
91   h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
92
93   bed = get_elf_backend_data (abfd);
94   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
95   return h;
96 }
97
98 bfd_boolean
99 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
100 {
101   flagword flags;
102   asection *s;
103   struct elf_link_hash_entry *h;
104   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
105   struct elf_link_hash_table *htab = elf_hash_table (info);
106
107   /* This function may be called more than once.  */
108   s = bfd_get_section_by_name (abfd, ".got");
109   if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
110     return TRUE;
111
112   flags = bed->dynamic_sec_flags;
113
114   s = bfd_make_section_with_flags (abfd,
115                                    (bed->rela_plts_and_copies_p
116                                     ? ".rela.got" : ".rel.got"),
117                                    (bed->dynamic_sec_flags
118                                     | SEC_READONLY));
119   if (s == NULL
120       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
121     return FALSE;
122   htab->srelgot = s;
123
124   s = bfd_make_section_with_flags (abfd, ".got", flags);
125   if (s == NULL
126       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
127     return FALSE;
128   htab->sgot = s;
129
130   if (bed->want_got_plt)
131     {
132       s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
133       if (s == NULL
134           || !bfd_set_section_alignment (abfd, s,
135                                          bed->s->log_file_align))
136         return FALSE;
137       htab->sgotplt = s;
138     }
139
140   /* The first bit of the global offset table is the header.  */
141   s->size += bed->got_header_size;
142
143   if (bed->want_got_sym)
144     {
145       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
146          (or .got.plt) section.  We don't do this in the linker script
147          because we don't want to define the symbol if we are not creating
148          a global offset table.  */
149       h = _bfd_elf_define_linkage_sym (abfd, info, s,
150                                        "_GLOBAL_OFFSET_TABLE_");
151       elf_hash_table (info)->hgot = h;
152       if (h == NULL)
153         return FALSE;
154     }
155
156   return TRUE;
157 }
158 \f
159 /* Create a strtab to hold the dynamic symbol names.  */
160 static bfd_boolean
161 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
162 {
163   struct elf_link_hash_table *hash_table;
164
165   hash_table = elf_hash_table (info);
166   if (hash_table->dynobj == NULL)
167     hash_table->dynobj = abfd;
168
169   if (hash_table->dynstr == NULL)
170     {
171       hash_table->dynstr = _bfd_elf_strtab_init ();
172       if (hash_table->dynstr == NULL)
173         return FALSE;
174     }
175   return TRUE;
176 }
177
178 /* Create some sections which will be filled in with dynamic linking
179    information.  ABFD is an input file which requires dynamic sections
180    to be created.  The dynamic sections take up virtual memory space
181    when the final executable is run, so we need to create them before
182    addresses are assigned to the output sections.  We work out the
183    actual contents and size of these sections later.  */
184
185 bfd_boolean
186 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
187 {
188   flagword flags;
189   asection *s;
190   const struct elf_backend_data *bed;
191
192   if (! is_elf_hash_table (info->hash))
193     return FALSE;
194
195   if (elf_hash_table (info)->dynamic_sections_created)
196     return TRUE;
197
198   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
199     return FALSE;
200
201   abfd = elf_hash_table (info)->dynobj;
202   bed = get_elf_backend_data (abfd);
203
204   flags = bed->dynamic_sec_flags;
205
206   /* A dynamically linked executable has a .interp section, but a
207      shared library does not.  */
208   if (info->executable)
209     {
210       s = bfd_make_section_with_flags (abfd, ".interp",
211                                        flags | SEC_READONLY);
212       if (s == NULL)
213         return FALSE;
214     }
215
216   /* Create sections to hold version informations.  These are removed
217      if they are not needed.  */
218   s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
219                                    flags | SEC_READONLY);
220   if (s == NULL
221       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
222     return FALSE;
223
224   s = bfd_make_section_with_flags (abfd, ".gnu.version",
225                                    flags | SEC_READONLY);
226   if (s == NULL
227       || ! bfd_set_section_alignment (abfd, s, 1))
228     return FALSE;
229
230   s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
231                                    flags | SEC_READONLY);
232   if (s == NULL
233       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
234     return FALSE;
235
236   s = bfd_make_section_with_flags (abfd, ".dynsym",
237                                    flags | SEC_READONLY);
238   if (s == NULL
239       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
240     return FALSE;
241
242   s = bfd_make_section_with_flags (abfd, ".dynstr",
243                                    flags | SEC_READONLY);
244   if (s == NULL)
245     return FALSE;
246
247   s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
248   if (s == NULL
249       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
250     return FALSE;
251
252   /* The special symbol _DYNAMIC is always set to the start of the
253      .dynamic section.  We could set _DYNAMIC in a linker script, but we
254      only want to define it if we are, in fact, creating a .dynamic
255      section.  We don't want to define it if there is no .dynamic
256      section, since on some ELF platforms the start up code examines it
257      to decide how to initialize the process.  */
258   if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
259     return FALSE;
260
261   if (info->emit_hash)
262     {
263       s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
264       if (s == NULL
265           || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
266         return FALSE;
267       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
268     }
269
270   if (info->emit_gnu_hash)
271     {
272       s = bfd_make_section_with_flags (abfd, ".gnu.hash",
273                                        flags | SEC_READONLY);
274       if (s == NULL
275           || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
276         return FALSE;
277       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
278          4 32-bit words followed by variable count of 64-bit words, then
279          variable count of 32-bit words.  */
280       if (bed->s->arch_size == 64)
281         elf_section_data (s)->this_hdr.sh_entsize = 0;
282       else
283         elf_section_data (s)->this_hdr.sh_entsize = 4;
284     }
285
286   /* Let the backend create the rest of the sections.  This lets the
287      backend set the right flags.  The backend will normally create
288      the .got and .plt sections.  */
289   if (bed->elf_backend_create_dynamic_sections == NULL
290       || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
291     return FALSE;
292
293   elf_hash_table (info)->dynamic_sections_created = TRUE;
294
295   return TRUE;
296 }
297
298 /* Create dynamic sections when linking against a dynamic object.  */
299
300 bfd_boolean
301 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
302 {
303   flagword flags, pltflags;
304   struct elf_link_hash_entry *h;
305   asection *s;
306   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
307   struct elf_link_hash_table *htab = elf_hash_table (info);
308
309   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
310      .rel[a].bss sections.  */
311   flags = bed->dynamic_sec_flags;
312
313   pltflags = flags;
314   if (bed->plt_not_loaded)
315     /* We do not clear SEC_ALLOC here because we still want the OS to
316        allocate space for the section; it's just that there's nothing
317        to read in from the object file.  */
318     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
319   else
320     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
321   if (bed->plt_readonly)
322     pltflags |= SEC_READONLY;
323
324   s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
325   if (s == NULL
326       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
327     return FALSE;
328   htab->splt = s;
329
330   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
331      .plt section.  */
332   if (bed->want_plt_sym)
333     {
334       h = _bfd_elf_define_linkage_sym (abfd, info, s,
335                                        "_PROCEDURE_LINKAGE_TABLE_");
336       elf_hash_table (info)->hplt = h;
337       if (h == NULL)
338         return FALSE;
339     }
340
341   s = bfd_make_section_with_flags (abfd,
342                                    (bed->rela_plts_and_copies_p
343                                     ? ".rela.plt" : ".rel.plt"),
344                                    flags | SEC_READONLY);
345   if (s == NULL
346       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
347     return FALSE;
348   htab->srelplt = s;
349
350   if (! _bfd_elf_create_got_section (abfd, info))
351     return FALSE;
352
353   if (bed->want_dynbss)
354     {
355       /* The .dynbss section is a place to put symbols which are defined
356          by dynamic objects, are referenced by regular objects, and are
357          not functions.  We must allocate space for them in the process
358          image and use a R_*_COPY reloc to tell the dynamic linker to
359          initialize them at run time.  The linker script puts the .dynbss
360          section into the .bss section of the final image.  */
361       s = bfd_make_section_with_flags (abfd, ".dynbss",
362                                        (SEC_ALLOC
363                                         | SEC_LINKER_CREATED));
364       if (s == NULL)
365         return FALSE;
366
367       /* The .rel[a].bss section holds copy relocs.  This section is not
368          normally needed.  We need to create it here, though, so that the
369          linker will map it to an output section.  We can't just create it
370          only if we need it, because we will not know whether we need it
371          until we have seen all the input files, and the first time the
372          main linker code calls BFD after examining all the input files
373          (size_dynamic_sections) the input sections have already been
374          mapped to the output sections.  If the section turns out not to
375          be needed, we can discard it later.  We will never need this
376          section when generating a shared object, since they do not use
377          copy relocs.  */
378       if (! info->shared)
379         {
380           s = bfd_make_section_with_flags (abfd,
381                                            (bed->rela_plts_and_copies_p
382                                             ? ".rela.bss" : ".rel.bss"),
383                                            flags | SEC_READONLY);
384           if (s == NULL
385               || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
386             return FALSE;
387         }
388     }
389
390   return TRUE;
391 }
392 \f
393 /* Record a new dynamic symbol.  We record the dynamic symbols as we
394    read the input files, since we need to have a list of all of them
395    before we can determine the final sizes of the output sections.
396    Note that we may actually call this function even though we are not
397    going to output any dynamic symbols; in some cases we know that a
398    symbol should be in the dynamic symbol table, but only if there is
399    one.  */
400
401 bfd_boolean
402 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
403                                     struct elf_link_hash_entry *h)
404 {
405   if (h->dynindx == -1)
406     {
407       struct elf_strtab_hash *dynstr;
408       char *p;
409       const char *name;
410       bfd_size_type indx;
411
412       /* XXX: The ABI draft says the linker must turn hidden and
413          internal symbols into STB_LOCAL symbols when producing the
414          DSO. However, if ld.so honors st_other in the dynamic table,
415          this would not be necessary.  */
416       switch (ELF_ST_VISIBILITY (h->other))
417         {
418         case STV_INTERNAL:
419         case STV_HIDDEN:
420           if (h->root.type != bfd_link_hash_undefined
421               && h->root.type != bfd_link_hash_undefweak)
422             {
423               h->forced_local = 1;
424               if (!elf_hash_table (info)->is_relocatable_executable)
425                 return TRUE;
426             }
427
428         default:
429           break;
430         }
431
432       h->dynindx = elf_hash_table (info)->dynsymcount;
433       ++elf_hash_table (info)->dynsymcount;
434
435       dynstr = elf_hash_table (info)->dynstr;
436       if (dynstr == NULL)
437         {
438           /* Create a strtab to hold the dynamic symbol names.  */
439           elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
440           if (dynstr == NULL)
441             return FALSE;
442         }
443
444       /* We don't put any version information in the dynamic string
445          table.  */
446       name = h->root.root.string;
447       p = strchr (name, ELF_VER_CHR);
448       if (p != NULL)
449         /* We know that the p points into writable memory.  In fact,
450            there are only a few symbols that have read-only names, being
451            those like _GLOBAL_OFFSET_TABLE_ that are created specially
452            by the backends.  Most symbols will have names pointing into
453            an ELF string table read from a file, or to objalloc memory.  */
454         *p = 0;
455
456       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
457
458       if (p != NULL)
459         *p = ELF_VER_CHR;
460
461       if (indx == (bfd_size_type) -1)
462         return FALSE;
463       h->dynstr_index = indx;
464     }
465
466   return TRUE;
467 }
468 \f
469 /* Mark a symbol dynamic.  */
470
471 static void
472 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
473                                   struct elf_link_hash_entry *h,
474                                   Elf_Internal_Sym *sym)
475 {
476   struct bfd_elf_dynamic_list *d = info->dynamic_list;
477
478   /* It may be called more than once on the same H.  */
479   if(h->dynamic || info->relocatable)
480     return;
481
482   if ((info->dynamic_data
483        && (h->type == STT_OBJECT
484            || (sym != NULL
485                && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
486       || (d != NULL
487           && h->root.type == bfd_link_hash_new
488           && (*d->match) (&d->head, NULL, h->root.root.string)))
489     h->dynamic = 1;
490 }
491
492 /* Record an assignment to a symbol made by a linker script.  We need
493    this in case some dynamic object refers to this symbol.  */
494
495 bfd_boolean
496 bfd_elf_record_link_assignment (bfd *output_bfd,
497                                 struct bfd_link_info *info,
498                                 const char *name,
499                                 bfd_boolean provide,
500                                 bfd_boolean hidden)
501 {
502   struct elf_link_hash_entry *h, *hv;
503   struct elf_link_hash_table *htab;
504   const struct elf_backend_data *bed;
505
506   if (!is_elf_hash_table (info->hash))
507     return TRUE;
508
509   htab = elf_hash_table (info);
510   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
511   if (h == NULL)
512     return provide;
513
514   switch (h->root.type)
515     {
516     case bfd_link_hash_defined:
517     case bfd_link_hash_defweak:
518     case bfd_link_hash_common:
519       break;
520     case bfd_link_hash_undefweak:
521     case bfd_link_hash_undefined:
522       /* Since we're defining the symbol, don't let it seem to have not
523          been defined.  record_dynamic_symbol and size_dynamic_sections
524          may depend on this.  */
525       h->root.type = bfd_link_hash_new;
526       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
527         bfd_link_repair_undef_list (&htab->root);
528       break;
529     case bfd_link_hash_new:
530       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
531       h->non_elf = 0;
532       break;
533     case bfd_link_hash_indirect:
534       /* We had a versioned symbol in a dynamic library.  We make the
535          the versioned symbol point to this one.  */
536       bed = get_elf_backend_data (output_bfd);
537       hv = h;
538       while (hv->root.type == bfd_link_hash_indirect
539              || hv->root.type == bfd_link_hash_warning)
540         hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
541       /* We don't need to update h->root.u since linker will set them
542          later.  */
543       h->root.type = bfd_link_hash_undefined;
544       hv->root.type = bfd_link_hash_indirect;
545       hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
546       (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
547       break;
548     case bfd_link_hash_warning:
549       abort ();
550       break;
551     }
552
553   /* If this symbol is being provided by the linker script, and it is
554      currently defined by a dynamic object, but not by a regular
555      object, then mark it as undefined so that the generic linker will
556      force the correct value.  */
557   if (provide
558       && h->def_dynamic
559       && !h->def_regular)
560     h->root.type = bfd_link_hash_undefined;
561
562   /* If this symbol is not being provided by the linker script, and it is
563      currently defined by a dynamic object, but not by a regular object,
564      then clear out any version information because the symbol will not be
565      associated with the dynamic object any more.  */
566   if (!provide
567       && h->def_dynamic
568       && !h->def_regular)
569     h->verinfo.verdef = NULL;
570
571   h->def_regular = 1;
572
573   if (provide && hidden)
574     {
575       bed = get_elf_backend_data (output_bfd);
576       h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
577       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
578     }
579
580   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
581      and executables.  */
582   if (!info->relocatable
583       && h->dynindx != -1
584       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
585           || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
586     h->forced_local = 1;
587
588   if ((h->def_dynamic
589        || h->ref_dynamic
590        || info->shared
591        || (info->executable && elf_hash_table (info)->is_relocatable_executable))
592       && h->dynindx == -1)
593     {
594       if (! bfd_elf_link_record_dynamic_symbol (info, h))
595         return FALSE;
596
597       /* If this is a weak defined symbol, and we know a corresponding
598          real symbol from the same dynamic object, make sure the real
599          symbol is also made into a dynamic symbol.  */
600       if (h->u.weakdef != NULL
601           && h->u.weakdef->dynindx == -1)
602         {
603           if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
604             return FALSE;
605         }
606     }
607
608   return TRUE;
609 }
610
611 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
612    success, and 2 on a failure caused by attempting to record a symbol
613    in a discarded section, eg. a discarded link-once section symbol.  */
614
615 int
616 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
617                                           bfd *input_bfd,
618                                           long input_indx)
619 {
620   bfd_size_type amt;
621   struct elf_link_local_dynamic_entry *entry;
622   struct elf_link_hash_table *eht;
623   struct elf_strtab_hash *dynstr;
624   unsigned long dynstr_index;
625   char *name;
626   Elf_External_Sym_Shndx eshndx;
627   char esym[sizeof (Elf64_External_Sym)];
628
629   if (! is_elf_hash_table (info->hash))
630     return 0;
631
632   /* See if the entry exists already.  */
633   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
634     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
635       return 1;
636
637   amt = sizeof (*entry);
638   entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
639   if (entry == NULL)
640     return 0;
641
642   /* Go find the symbol, so that we can find it's name.  */
643   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
644                              1, input_indx, &entry->isym, esym, &eshndx))
645     {
646       bfd_release (input_bfd, entry);
647       return 0;
648     }
649
650   if (entry->isym.st_shndx != SHN_UNDEF
651       && entry->isym.st_shndx < SHN_LORESERVE)
652     {
653       asection *s;
654
655       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
656       if (s == NULL || bfd_is_abs_section (s->output_section))
657         {
658           /* We can still bfd_release here as nothing has done another
659              bfd_alloc.  We can't do this later in this function.  */
660           bfd_release (input_bfd, entry);
661           return 2;
662         }
663     }
664
665   name = (bfd_elf_string_from_elf_section
666           (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
667            entry->isym.st_name));
668
669   dynstr = elf_hash_table (info)->dynstr;
670   if (dynstr == NULL)
671     {
672       /* Create a strtab to hold the dynamic symbol names.  */
673       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
674       if (dynstr == NULL)
675         return 0;
676     }
677
678   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
679   if (dynstr_index == (unsigned long) -1)
680     return 0;
681   entry->isym.st_name = dynstr_index;
682
683   eht = elf_hash_table (info);
684
685   entry->next = eht->dynlocal;
686   eht->dynlocal = entry;
687   entry->input_bfd = input_bfd;
688   entry->input_indx = input_indx;
689   eht->dynsymcount++;
690
691   /* Whatever binding the symbol had before, it's now local.  */
692   entry->isym.st_info
693     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
694
695   /* The dynindx will be set at the end of size_dynamic_sections.  */
696
697   return 1;
698 }
699
700 /* Return the dynindex of a local dynamic symbol.  */
701
702 long
703 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
704                                     bfd *input_bfd,
705                                     long input_indx)
706 {
707   struct elf_link_local_dynamic_entry *e;
708
709   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
710     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
711       return e->dynindx;
712   return -1;
713 }
714
715 /* This function is used to renumber the dynamic symbols, if some of
716    them are removed because they are marked as local.  This is called
717    via elf_link_hash_traverse.  */
718
719 static bfd_boolean
720 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
721                                       void *data)
722 {
723   size_t *count = (size_t *) data;
724
725   if (h->forced_local)
726     return TRUE;
727
728   if (h->dynindx != -1)
729     h->dynindx = ++(*count);
730
731   return TRUE;
732 }
733
734
735 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
736    STB_LOCAL binding.  */
737
738 static bfd_boolean
739 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
740                                             void *data)
741 {
742   size_t *count = (size_t *) data;
743
744   if (!h->forced_local)
745     return TRUE;
746
747   if (h->dynindx != -1)
748     h->dynindx = ++(*count);
749
750   return TRUE;
751 }
752
753 /* Return true if the dynamic symbol for a given section should be
754    omitted when creating a shared library.  */
755 bfd_boolean
756 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
757                                    struct bfd_link_info *info,
758                                    asection *p)
759 {
760   struct elf_link_hash_table *htab;
761
762   switch (elf_section_data (p)->this_hdr.sh_type)
763     {
764     case SHT_PROGBITS:
765     case SHT_NOBITS:
766       /* If sh_type is yet undecided, assume it could be
767          SHT_PROGBITS/SHT_NOBITS.  */
768     case SHT_NULL:
769       htab = elf_hash_table (info);
770       if (p == htab->tls_sec)
771         return FALSE;
772
773       if (htab->text_index_section != NULL)
774         return p != htab->text_index_section && p != htab->data_index_section;
775
776       if (strcmp (p->name, ".got") == 0
777           || strcmp (p->name, ".got.plt") == 0
778           || strcmp (p->name, ".plt") == 0)
779         {
780           asection *ip;
781
782           if (htab->dynobj != NULL
783               && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
784               && (ip->flags & SEC_LINKER_CREATED)
785               && ip->output_section == p)
786             return TRUE;
787         }
788       return FALSE;
789
790       /* There shouldn't be section relative relocations
791          against any other section.  */
792     default:
793       return TRUE;
794     }
795 }
796
797 /* Assign dynsym indices.  In a shared library we generate a section
798    symbol for each output section, which come first.  Next come symbols
799    which have been forced to local binding.  Then all of the back-end
800    allocated local dynamic syms, followed by the rest of the global
801    symbols.  */
802
803 static unsigned long
804 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
805                                 struct bfd_link_info *info,
806                                 unsigned long *section_sym_count)
807 {
808   unsigned long dynsymcount = 0;
809
810   if (info->shared || elf_hash_table (info)->is_relocatable_executable)
811     {
812       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
813       asection *p;
814       for (p = output_bfd->sections; p ; p = p->next)
815         if ((p->flags & SEC_EXCLUDE) == 0
816             && (p->flags & SEC_ALLOC) != 0
817             && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
818           elf_section_data (p)->dynindx = ++dynsymcount;
819         else
820           elf_section_data (p)->dynindx = 0;
821     }
822   *section_sym_count = dynsymcount;
823
824   elf_link_hash_traverse (elf_hash_table (info),
825                           elf_link_renumber_local_hash_table_dynsyms,
826                           &dynsymcount);
827
828   if (elf_hash_table (info)->dynlocal)
829     {
830       struct elf_link_local_dynamic_entry *p;
831       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
832         p->dynindx = ++dynsymcount;
833     }
834
835   elf_link_hash_traverse (elf_hash_table (info),
836                           elf_link_renumber_hash_table_dynsyms,
837                           &dynsymcount);
838
839   /* There is an unused NULL entry at the head of the table which
840      we must account for in our count.  Unless there weren't any
841      symbols, which means we'll have no table at all.  */
842   if (dynsymcount != 0)
843     ++dynsymcount;
844
845   elf_hash_table (info)->dynsymcount = dynsymcount;
846   return dynsymcount;
847 }
848
849 /* Merge st_other field.  */
850
851 static void
852 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
853                     Elf_Internal_Sym *isym, bfd_boolean definition,
854                     bfd_boolean dynamic)
855 {
856   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
857
858   /* If st_other has a processor-specific meaning, specific
859      code might be needed here. We never merge the visibility
860      attribute with the one from a dynamic object.  */
861   if (bed->elf_backend_merge_symbol_attribute)
862     (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
863                                                 dynamic);
864
865   /* If this symbol has default visibility and the user has requested
866      we not re-export it, then mark it as hidden.  */
867   if (definition
868       && !dynamic
869       && (abfd->no_export
870           || (abfd->my_archive && abfd->my_archive->no_export))
871       && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
872     isym->st_other = (STV_HIDDEN
873                       | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
874
875   if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
876     {
877       unsigned char hvis, symvis, other, nvis;
878
879       /* Only merge the visibility. Leave the remainder of the
880          st_other field to elf_backend_merge_symbol_attribute.  */
881       other = h->other & ~ELF_ST_VISIBILITY (-1);
882
883       /* Combine visibilities, using the most constraining one.  */
884       hvis = ELF_ST_VISIBILITY (h->other);
885       symvis = ELF_ST_VISIBILITY (isym->st_other);
886       if (! hvis)
887         nvis = symvis;
888       else if (! symvis)
889         nvis = hvis;
890       else
891         nvis = hvis < symvis ? hvis : symvis;
892
893       h->other = other | nvis;
894     }
895 }
896
897 /* This function is called when we want to define a new symbol.  It
898    handles the various cases which arise when we find a definition in
899    a dynamic object, or when there is already a definition in a
900    dynamic object.  The new symbol is described by NAME, SYM, PSEC,
901    and PVALUE.  We set SYM_HASH to the hash table entry.  We set
902    OVERRIDE if the old symbol is overriding a new definition.  We set
903    TYPE_CHANGE_OK if it is OK for the type to change.  We set
904    SIZE_CHANGE_OK if it is OK for the size to change.  By OK to
905    change, we mean that we shouldn't warn if the type or size does
906    change.  We set POLD_ALIGNMENT if an old common symbol in a dynamic
907    object is overridden by a regular object.  */
908
909 bfd_boolean
910 _bfd_elf_merge_symbol (bfd *abfd,
911                        struct bfd_link_info *info,
912                        const char *name,
913                        Elf_Internal_Sym *sym,
914                        asection **psec,
915                        bfd_vma *pvalue,
916                        unsigned int *pold_alignment,
917                        struct elf_link_hash_entry **sym_hash,
918                        bfd_boolean *skip,
919                        bfd_boolean *override,
920                        bfd_boolean *type_change_ok,
921                        bfd_boolean *size_change_ok)
922 {
923   asection *sec, *oldsec;
924   struct elf_link_hash_entry *h;
925   struct elf_link_hash_entry *flip;
926   int bind;
927   bfd *oldbfd;
928   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
929   bfd_boolean newweak, oldweak, newfunc, oldfunc;
930   const struct elf_backend_data *bed;
931
932   *skip = FALSE;
933   *override = FALSE;
934
935   sec = *psec;
936   bind = ELF_ST_BIND (sym->st_info);
937
938   /* Silently discard TLS symbols from --just-syms.  There's no way to
939      combine a static TLS block with a new TLS block for this executable.  */
940   if (ELF_ST_TYPE (sym->st_info) == STT_TLS
941       && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
942     {
943       *skip = TRUE;
944       return TRUE;
945     }
946
947   if (! bfd_is_und_section (sec))
948     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
949   else
950     h = ((struct elf_link_hash_entry *)
951          bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
952   if (h == NULL)
953     return FALSE;
954   *sym_hash = h;
955
956   bed = get_elf_backend_data (abfd);
957
958   /* This code is for coping with dynamic objects, and is only useful
959      if we are doing an ELF link.  */
960   if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
961     return TRUE;
962
963   /* For merging, we only care about real symbols.  */
964
965   while (h->root.type == bfd_link_hash_indirect
966          || h->root.type == bfd_link_hash_warning)
967     h = (struct elf_link_hash_entry *) h->root.u.i.link;
968
969   /* We have to check it for every instance since the first few may be
970      refereences and not all compilers emit symbol type for undefined
971      symbols.  */
972   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
973
974   /* If we just created the symbol, mark it as being an ELF symbol.
975      Other than that, there is nothing to do--there is no merge issue
976      with a newly defined symbol--so we just return.  */
977
978   if (h->root.type == bfd_link_hash_new)
979     {
980       h->non_elf = 0;
981       return TRUE;
982     }
983
984   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
985      existing symbol.  */
986
987   switch (h->root.type)
988     {
989     default:
990       oldbfd = NULL;
991       oldsec = NULL;
992       break;
993
994     case bfd_link_hash_undefined:
995     case bfd_link_hash_undefweak:
996       oldbfd = h->root.u.undef.abfd;
997       oldsec = NULL;
998       break;
999
1000     case bfd_link_hash_defined:
1001     case bfd_link_hash_defweak:
1002       oldbfd = h->root.u.def.section->owner;
1003       oldsec = h->root.u.def.section;
1004       break;
1005
1006     case bfd_link_hash_common:
1007       oldbfd = h->root.u.c.p->section->owner;
1008       oldsec = h->root.u.c.p->section;
1009       break;
1010     }
1011
1012   /* Differentiate strong and weak symbols.  */
1013   newweak = bind == STB_WEAK;
1014   oldweak = (h->root.type == bfd_link_hash_defweak
1015              || h->root.type == bfd_link_hash_undefweak);
1016
1017   /* In cases involving weak versioned symbols, we may wind up trying
1018      to merge a symbol with itself.  Catch that here, to avoid the
1019      confusion that results if we try to override a symbol with
1020      itself.  The additional tests catch cases like
1021      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1022      dynamic object, which we do want to handle here.  */
1023   if (abfd == oldbfd
1024       && (newweak || oldweak)
1025       && ((abfd->flags & DYNAMIC) == 0
1026           || !h->def_regular))
1027     return TRUE;
1028
1029   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1030      respectively, is from a dynamic object.  */
1031
1032   newdyn = (abfd->flags & DYNAMIC) != 0;
1033
1034   olddyn = FALSE;
1035   if (oldbfd != NULL)
1036     olddyn = (oldbfd->flags & DYNAMIC) != 0;
1037   else if (oldsec != NULL)
1038     {
1039       /* This handles the special SHN_MIPS_{TEXT,DATA} section
1040          indices used by MIPS ELF.  */
1041       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1042     }
1043
1044   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1045      respectively, appear to be a definition rather than reference.  */
1046
1047   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1048
1049   olddef = (h->root.type != bfd_link_hash_undefined
1050             && h->root.type != bfd_link_hash_undefweak
1051             && h->root.type != bfd_link_hash_common);
1052
1053   /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1054      respectively, appear to be a function.  */
1055
1056   newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1057              && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1058
1059   oldfunc = (h->type != STT_NOTYPE
1060              && bed->is_function_type (h->type));
1061
1062   /* When we try to create a default indirect symbol from the dynamic
1063      definition with the default version, we skip it if its type and
1064      the type of existing regular definition mismatch.  We only do it
1065      if the existing regular definition won't be dynamic.  */
1066   if (pold_alignment == NULL
1067       && !info->shared
1068       && !info->export_dynamic
1069       && !h->ref_dynamic
1070       && newdyn
1071       && newdef
1072       && !olddyn
1073       && (olddef || h->root.type == bfd_link_hash_common)
1074       && ELF_ST_TYPE (sym->st_info) != h->type
1075       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1076       && h->type != STT_NOTYPE
1077       && !(newfunc && oldfunc))
1078     {
1079       *skip = TRUE;
1080       return TRUE;
1081     }
1082
1083   /* Plugin symbol type isn't currently set.  Stop bogus errors.  */
1084   if (oldbfd != NULL && (oldbfd->flags & BFD_PLUGIN) != 0)
1085     *type_change_ok = TRUE;
1086
1087   /* Check TLS symbol.  We don't check undefined symbol introduced by
1088      "ld -u".  */
1089   else if (oldbfd != NULL
1090            && ELF_ST_TYPE (sym->st_info) != h->type
1091            && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1092     {
1093       bfd *ntbfd, *tbfd;
1094       bfd_boolean ntdef, tdef;
1095       asection *ntsec, *tsec;
1096
1097       if (h->type == STT_TLS)
1098         {
1099           ntbfd = abfd;
1100           ntsec = sec;
1101           ntdef = newdef;
1102           tbfd = oldbfd;
1103           tsec = oldsec;
1104           tdef = olddef;
1105         }
1106       else
1107         {
1108           ntbfd = oldbfd;
1109           ntsec = oldsec;
1110           ntdef = olddef;
1111           tbfd = abfd;
1112           tsec = sec;
1113           tdef = newdef;
1114         }
1115
1116       if (tdef && ntdef)
1117         (*_bfd_error_handler)
1118           (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1119            tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1120       else if (!tdef && !ntdef)
1121         (*_bfd_error_handler)
1122           (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1123            tbfd, ntbfd, h->root.root.string);
1124       else if (tdef)
1125         (*_bfd_error_handler)
1126           (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1127            tbfd, tsec, ntbfd, h->root.root.string);
1128       else
1129         (*_bfd_error_handler)
1130           (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1131            tbfd, ntbfd, ntsec, h->root.root.string);
1132
1133       bfd_set_error (bfd_error_bad_value);
1134       return FALSE;
1135     }
1136
1137   /* We need to remember if a symbol has a definition in a dynamic
1138      object or is weak in all dynamic objects. Internal and hidden
1139      visibility will make it unavailable to dynamic objects.  */
1140   if (newdyn && !h->dynamic_def)
1141     {
1142       if (!bfd_is_und_section (sec))
1143         h->dynamic_def = 1;
1144       else
1145         {
1146           /* Check if this symbol is weak in all dynamic objects. If it
1147              is the first time we see it in a dynamic object, we mark
1148              if it is weak. Otherwise, we clear it.  */
1149           if (!h->ref_dynamic)
1150             {
1151               if (bind == STB_WEAK)
1152                 h->dynamic_weak = 1;
1153             }
1154           else if (bind != STB_WEAK)
1155             h->dynamic_weak = 0;
1156         }
1157     }
1158
1159   /* If the old symbol has non-default visibility, we ignore the new
1160      definition from a dynamic object.  */
1161   if (newdyn
1162       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1163       && !bfd_is_und_section (sec))
1164     {
1165       *skip = TRUE;
1166       /* Make sure this symbol is dynamic.  */
1167       h->ref_dynamic = 1;
1168       /* A protected symbol has external availability. Make sure it is
1169          recorded as dynamic.
1170
1171          FIXME: Should we check type and size for protected symbol?  */
1172       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1173         return bfd_elf_link_record_dynamic_symbol (info, h);
1174       else
1175         return TRUE;
1176     }
1177   else if (!newdyn
1178            && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1179            && h->def_dynamic)
1180     {
1181       /* If the new symbol with non-default visibility comes from a
1182          relocatable file and the old definition comes from a dynamic
1183          object, we remove the old definition.  */
1184       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1185         {
1186           /* Handle the case where the old dynamic definition is
1187              default versioned.  We need to copy the symbol info from
1188              the symbol with default version to the normal one if it
1189              was referenced before.  */
1190           if (h->ref_regular)
1191             {
1192               struct elf_link_hash_entry *vh = *sym_hash;
1193
1194               vh->root.type = h->root.type;
1195               h->root.type = bfd_link_hash_indirect;
1196               (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1197               /* Protected symbols will override the dynamic definition
1198                  with default version.  */
1199               if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1200                 {
1201                   h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1202                   vh->dynamic_def = 1;
1203                   vh->ref_dynamic = 1;
1204                 }
1205               else
1206                 {
1207                   h->root.type = vh->root.type;
1208                   vh->ref_dynamic = 0;
1209                   /* We have to hide it here since it was made dynamic
1210                      global with extra bits when the symbol info was
1211                      copied from the old dynamic definition.  */
1212                   (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1213                 }
1214               h = vh;
1215             }
1216           else
1217             h = *sym_hash;
1218         }
1219
1220       if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1221           && bfd_is_und_section (sec))
1222         {
1223           /* If the new symbol is undefined and the old symbol was
1224              also undefined before, we need to make sure
1225              _bfd_generic_link_add_one_symbol doesn't mess
1226              up the linker hash table undefs list.  Since the old
1227              definition came from a dynamic object, it is still on the
1228              undefs list.  */
1229           h->root.type = bfd_link_hash_undefined;
1230           h->root.u.undef.abfd = abfd;
1231         }
1232       else
1233         {
1234           h->root.type = bfd_link_hash_new;
1235           h->root.u.undef.abfd = NULL;
1236         }
1237
1238       if (h->def_dynamic)
1239         {
1240           h->def_dynamic = 0;
1241           h->ref_dynamic = 1;
1242           h->dynamic_def = 1;
1243         }
1244       /* FIXME: Should we check type and size for protected symbol?  */
1245       h->size = 0;
1246       h->type = 0;
1247       return TRUE;
1248     }
1249
1250   if (bind == STB_GNU_UNIQUE)
1251     h->unique_global = 1;
1252
1253   /* If a new weak symbol definition comes from a regular file and the
1254      old symbol comes from a dynamic library, we treat the new one as
1255      strong.  Similarly, an old weak symbol definition from a regular
1256      file is treated as strong when the new symbol comes from a dynamic
1257      library.  Further, an old weak symbol from a dynamic library is
1258      treated as strong if the new symbol is from a dynamic library.
1259      This reflects the way glibc's ld.so works.
1260
1261      Do this before setting *type_change_ok or *size_change_ok so that
1262      we warn properly when dynamic library symbols are overridden.  */
1263
1264   if (newdef && !newdyn && olddyn)
1265     newweak = FALSE;
1266   if (olddef && newdyn)
1267     oldweak = FALSE;
1268
1269   /* Allow changes between different types of function symbol.  */
1270   if (newfunc && oldfunc)
1271     *type_change_ok = TRUE;
1272
1273   /* It's OK to change the type if either the existing symbol or the
1274      new symbol is weak.  A type change is also OK if the old symbol
1275      is undefined and the new symbol is defined.  */
1276
1277   if (oldweak
1278       || newweak
1279       || (newdef
1280           && h->root.type == bfd_link_hash_undefined))
1281     *type_change_ok = TRUE;
1282
1283   /* It's OK to change the size if either the existing symbol or the
1284      new symbol is weak, or if the old symbol is undefined.  */
1285
1286   if (*type_change_ok
1287       || h->root.type == bfd_link_hash_undefined)
1288     *size_change_ok = TRUE;
1289
1290   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1291      symbol, respectively, appears to be a common symbol in a dynamic
1292      object.  If a symbol appears in an uninitialized section, and is
1293      not weak, and is not a function, then it may be a common symbol
1294      which was resolved when the dynamic object was created.  We want
1295      to treat such symbols specially, because they raise special
1296      considerations when setting the symbol size: if the symbol
1297      appears as a common symbol in a regular object, and the size in
1298      the regular object is larger, we must make sure that we use the
1299      larger size.  This problematic case can always be avoided in C,
1300      but it must be handled correctly when using Fortran shared
1301      libraries.
1302
1303      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1304      likewise for OLDDYNCOMMON and OLDDEF.
1305
1306      Note that this test is just a heuristic, and that it is quite
1307      possible to have an uninitialized symbol in a shared object which
1308      is really a definition, rather than a common symbol.  This could
1309      lead to some minor confusion when the symbol really is a common
1310      symbol in some regular object.  However, I think it will be
1311      harmless.  */
1312
1313   if (newdyn
1314       && newdef
1315       && !newweak
1316       && (sec->flags & SEC_ALLOC) != 0
1317       && (sec->flags & SEC_LOAD) == 0
1318       && sym->st_size > 0
1319       && !newfunc)
1320     newdyncommon = TRUE;
1321   else
1322     newdyncommon = FALSE;
1323
1324   if (olddyn
1325       && olddef
1326       && h->root.type == bfd_link_hash_defined
1327       && h->def_dynamic
1328       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1329       && (h->root.u.def.section->flags & SEC_LOAD) == 0
1330       && h->size > 0
1331       && !oldfunc)
1332     olddyncommon = TRUE;
1333   else
1334     olddyncommon = FALSE;
1335
1336   /* We now know everything about the old and new symbols.  We ask the
1337      backend to check if we can merge them.  */
1338   if (bed->merge_symbol
1339       && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1340                              pold_alignment, skip, override,
1341                              type_change_ok, size_change_ok,
1342                              &newdyn, &newdef, &newdyncommon, &newweak,
1343                              abfd, &sec,
1344                              &olddyn, &olddef, &olddyncommon, &oldweak,
1345                              oldbfd, &oldsec))
1346     return FALSE;
1347
1348   /* If both the old and the new symbols look like common symbols in a
1349      dynamic object, set the size of the symbol to the larger of the
1350      two.  */
1351
1352   if (olddyncommon
1353       && newdyncommon
1354       && sym->st_size != h->size)
1355     {
1356       /* Since we think we have two common symbols, issue a multiple
1357          common warning if desired.  Note that we only warn if the
1358          size is different.  If the size is the same, we simply let
1359          the old symbol override the new one as normally happens with
1360          symbols defined in dynamic objects.  */
1361
1362       if (! ((*info->callbacks->multiple_common)
1363              (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1364         return FALSE;
1365
1366       if (sym->st_size > h->size)
1367         h->size = sym->st_size;
1368
1369       *size_change_ok = TRUE;
1370     }
1371
1372   /* If we are looking at a dynamic object, and we have found a
1373      definition, we need to see if the symbol was already defined by
1374      some other object.  If so, we want to use the existing
1375      definition, and we do not want to report a multiple symbol
1376      definition error; we do this by clobbering *PSEC to be
1377      bfd_und_section_ptr.
1378
1379      We treat a common symbol as a definition if the symbol in the
1380      shared library is a function, since common symbols always
1381      represent variables; this can cause confusion in principle, but
1382      any such confusion would seem to indicate an erroneous program or
1383      shared library.  We also permit a common symbol in a regular
1384      object to override a weak symbol in a shared object.  */
1385
1386   if (newdyn
1387       && newdef
1388       && (olddef
1389           || (h->root.type == bfd_link_hash_common
1390               && (newweak || newfunc))))
1391     {
1392       *override = TRUE;
1393       newdef = FALSE;
1394       newdyncommon = FALSE;
1395
1396       *psec = sec = bfd_und_section_ptr;
1397       *size_change_ok = TRUE;
1398
1399       /* If we get here when the old symbol is a common symbol, then
1400          we are explicitly letting it override a weak symbol or
1401          function in a dynamic object, and we don't want to warn about
1402          a type change.  If the old symbol is a defined symbol, a type
1403          change warning may still be appropriate.  */
1404
1405       if (h->root.type == bfd_link_hash_common)
1406         *type_change_ok = TRUE;
1407     }
1408
1409   /* Handle the special case of an old common symbol merging with a
1410      new symbol which looks like a common symbol in a shared object.
1411      We change *PSEC and *PVALUE to make the new symbol look like a
1412      common symbol, and let _bfd_generic_link_add_one_symbol do the
1413      right thing.  */
1414
1415   if (newdyncommon
1416       && h->root.type == bfd_link_hash_common)
1417     {
1418       *override = TRUE;
1419       newdef = FALSE;
1420       newdyncommon = FALSE;
1421       *pvalue = sym->st_size;
1422       *psec = sec = bed->common_section (oldsec);
1423       *size_change_ok = TRUE;
1424     }
1425
1426   /* Skip weak definitions of symbols that are already defined.  */
1427   if (newdef && olddef && newweak)
1428     {
1429       /* Don't skip new non-IR weak syms.  */
1430       if (!(oldbfd != NULL
1431             && (oldbfd->flags & BFD_PLUGIN) != 0
1432             && (abfd->flags & BFD_PLUGIN) == 0))
1433         *skip = TRUE;
1434
1435       /* Merge st_other.  If the symbol already has a dynamic index,
1436          but visibility says it should not be visible, turn it into a
1437          local symbol.  */
1438       elf_merge_st_other (abfd, h, sym, newdef, newdyn);
1439       if (h->dynindx != -1)
1440         switch (ELF_ST_VISIBILITY (h->other))
1441           {
1442           case STV_INTERNAL:
1443           case STV_HIDDEN:
1444             (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1445             break;
1446           }
1447     }
1448
1449   /* If the old symbol is from a dynamic object, and the new symbol is
1450      a definition which is not from a dynamic object, then the new
1451      symbol overrides the old symbol.  Symbols from regular files
1452      always take precedence over symbols from dynamic objects, even if
1453      they are defined after the dynamic object in the link.
1454
1455      As above, we again permit a common symbol in a regular object to
1456      override a definition in a shared object if the shared object
1457      symbol is a function or is weak.  */
1458
1459   flip = NULL;
1460   if (!newdyn
1461       && (newdef
1462           || (bfd_is_com_section (sec)
1463               && (oldweak || oldfunc)))
1464       && olddyn
1465       && olddef
1466       && h->def_dynamic)
1467     {
1468       /* Change the hash table entry to undefined, and let
1469          _bfd_generic_link_add_one_symbol do the right thing with the
1470          new definition.  */
1471
1472       h->root.type = bfd_link_hash_undefined;
1473       h->root.u.undef.abfd = h->root.u.def.section->owner;
1474       *size_change_ok = TRUE;
1475
1476       olddef = FALSE;
1477       olddyncommon = FALSE;
1478
1479       /* We again permit a type change when a common symbol may be
1480          overriding a function.  */
1481
1482       if (bfd_is_com_section (sec))
1483         {
1484           if (oldfunc)
1485             {
1486               /* If a common symbol overrides a function, make sure
1487                  that it isn't defined dynamically nor has type
1488                  function.  */
1489               h->def_dynamic = 0;
1490               h->type = STT_NOTYPE;
1491             }
1492           *type_change_ok = TRUE;
1493         }
1494
1495       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1496         flip = *sym_hash;
1497       else
1498         /* This union may have been set to be non-NULL when this symbol
1499            was seen in a dynamic object.  We must force the union to be
1500            NULL, so that it is correct for a regular symbol.  */
1501         h->verinfo.vertree = NULL;
1502     }
1503
1504   /* Handle the special case of a new common symbol merging with an
1505      old symbol that looks like it might be a common symbol defined in
1506      a shared object.  Note that we have already handled the case in
1507      which a new common symbol should simply override the definition
1508      in the shared library.  */
1509
1510   if (! newdyn
1511       && bfd_is_com_section (sec)
1512       && olddyncommon)
1513     {
1514       /* It would be best if we could set the hash table entry to a
1515          common symbol, but we don't know what to use for the section
1516          or the alignment.  */
1517       if (! ((*info->callbacks->multiple_common)
1518              (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1519         return FALSE;
1520
1521       /* If the presumed common symbol in the dynamic object is
1522          larger, pretend that the new symbol has its size.  */
1523
1524       if (h->size > *pvalue)
1525         *pvalue = h->size;
1526
1527       /* We need to remember the alignment required by the symbol
1528          in the dynamic object.  */
1529       BFD_ASSERT (pold_alignment);
1530       *pold_alignment = h->root.u.def.section->alignment_power;
1531
1532       olddef = FALSE;
1533       olddyncommon = FALSE;
1534
1535       h->root.type = bfd_link_hash_undefined;
1536       h->root.u.undef.abfd = h->root.u.def.section->owner;
1537
1538       *size_change_ok = TRUE;
1539       *type_change_ok = TRUE;
1540
1541       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1542         flip = *sym_hash;
1543       else
1544         h->verinfo.vertree = NULL;
1545     }
1546
1547   if (flip != NULL)
1548     {
1549       /* Handle the case where we had a versioned symbol in a dynamic
1550          library and now find a definition in a normal object.  In this
1551          case, we make the versioned symbol point to the normal one.  */
1552       flip->root.type = h->root.type;
1553       flip->root.u.undef.abfd = h->root.u.undef.abfd;
1554       h->root.type = bfd_link_hash_indirect;
1555       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1556       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1557       if (h->def_dynamic)
1558         {
1559           h->def_dynamic = 0;
1560           flip->ref_dynamic = 1;
1561         }
1562     }
1563
1564   return TRUE;
1565 }
1566
1567 /* This function is called to create an indirect symbol from the
1568    default for the symbol with the default version if needed. The
1569    symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE.  We
1570    set DYNSYM if the new indirect symbol is dynamic.  */
1571
1572 static bfd_boolean
1573 _bfd_elf_add_default_symbol (bfd *abfd,
1574                              struct bfd_link_info *info,
1575                              struct elf_link_hash_entry *h,
1576                              const char *name,
1577                              Elf_Internal_Sym *sym,
1578                              asection **psec,
1579                              bfd_vma *value,
1580                              bfd_boolean *dynsym,
1581                              bfd_boolean override)
1582 {
1583   bfd_boolean type_change_ok;
1584   bfd_boolean size_change_ok;
1585   bfd_boolean skip;
1586   char *shortname;
1587   struct elf_link_hash_entry *hi;
1588   struct bfd_link_hash_entry *bh;
1589   const struct elf_backend_data *bed;
1590   bfd_boolean collect;
1591   bfd_boolean dynamic;
1592   char *p;
1593   size_t len, shortlen;
1594   asection *sec;
1595
1596   /* If this symbol has a version, and it is the default version, we
1597      create an indirect symbol from the default name to the fully
1598      decorated name.  This will cause external references which do not
1599      specify a version to be bound to this version of the symbol.  */
1600   p = strchr (name, ELF_VER_CHR);
1601   if (p == NULL || p[1] != ELF_VER_CHR)
1602     return TRUE;
1603
1604   if (override)
1605     {
1606       /* We are overridden by an old definition. We need to check if we
1607          need to create the indirect symbol from the default name.  */
1608       hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1609                                  FALSE, FALSE);
1610       BFD_ASSERT (hi != NULL);
1611       if (hi == h)
1612         return TRUE;
1613       while (hi->root.type == bfd_link_hash_indirect
1614              || hi->root.type == bfd_link_hash_warning)
1615         {
1616           hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1617           if (hi == h)
1618             return TRUE;
1619         }
1620     }
1621
1622   bed = get_elf_backend_data (abfd);
1623   collect = bed->collect;
1624   dynamic = (abfd->flags & DYNAMIC) != 0;
1625
1626   shortlen = p - name;
1627   shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1628   if (shortname == NULL)
1629     return FALSE;
1630   memcpy (shortname, name, shortlen);
1631   shortname[shortlen] = '\0';
1632
1633   /* We are going to create a new symbol.  Merge it with any existing
1634      symbol with this name.  For the purposes of the merge, act as
1635      though we were defining the symbol we just defined, although we
1636      actually going to define an indirect symbol.  */
1637   type_change_ok = FALSE;
1638   size_change_ok = FALSE;
1639   sec = *psec;
1640   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1641                               NULL, &hi, &skip, &override,
1642                               &type_change_ok, &size_change_ok))
1643     return FALSE;
1644
1645   if (skip)
1646     goto nondefault;
1647
1648   if (! override)
1649     {
1650       bh = &hi->root;
1651       if (! (_bfd_generic_link_add_one_symbol
1652              (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1653               0, name, FALSE, collect, &bh)))
1654         return FALSE;
1655       hi = (struct elf_link_hash_entry *) bh;
1656     }
1657   else
1658     {
1659       /* In this case the symbol named SHORTNAME is overriding the
1660          indirect symbol we want to add.  We were planning on making
1661          SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
1662          is the name without a version.  NAME is the fully versioned
1663          name, and it is the default version.
1664
1665          Overriding means that we already saw a definition for the
1666          symbol SHORTNAME in a regular object, and it is overriding
1667          the symbol defined in the dynamic object.
1668
1669          When this happens, we actually want to change NAME, the
1670          symbol we just added, to refer to SHORTNAME.  This will cause
1671          references to NAME in the shared object to become references
1672          to SHORTNAME in the regular object.  This is what we expect
1673          when we override a function in a shared object: that the
1674          references in the shared object will be mapped to the
1675          definition in the regular object.  */
1676
1677       while (hi->root.type == bfd_link_hash_indirect
1678              || hi->root.type == bfd_link_hash_warning)
1679         hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1680
1681       h->root.type = bfd_link_hash_indirect;
1682       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1683       if (h->def_dynamic)
1684         {
1685           h->def_dynamic = 0;
1686           hi->ref_dynamic = 1;
1687           if (hi->ref_regular
1688               || hi->def_regular)
1689             {
1690               if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1691                 return FALSE;
1692             }
1693         }
1694
1695       /* Now set HI to H, so that the following code will set the
1696          other fields correctly.  */
1697       hi = h;
1698     }
1699
1700   /* Check if HI is a warning symbol.  */
1701   if (hi->root.type == bfd_link_hash_warning)
1702     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1703
1704   /* If there is a duplicate definition somewhere, then HI may not
1705      point to an indirect symbol.  We will have reported an error to
1706      the user in that case.  */
1707
1708   if (hi->root.type == bfd_link_hash_indirect)
1709     {
1710       struct elf_link_hash_entry *ht;
1711
1712       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1713       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1714
1715       /* See if the new flags lead us to realize that the symbol must
1716          be dynamic.  */
1717       if (! *dynsym)
1718         {
1719           if (! dynamic)
1720             {
1721               if (! info->executable
1722                   || hi->ref_dynamic)
1723                 *dynsym = TRUE;
1724             }
1725           else
1726             {
1727               if (hi->ref_regular)
1728                 *dynsym = TRUE;
1729             }
1730         }
1731     }
1732
1733   /* We also need to define an indirection from the nondefault version
1734      of the symbol.  */
1735
1736 nondefault:
1737   len = strlen (name);
1738   shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1739   if (shortname == NULL)
1740     return FALSE;
1741   memcpy (shortname, name, shortlen);
1742   memcpy (shortname + shortlen, p + 1, len - shortlen);
1743
1744   /* Once again, merge with any existing symbol.  */
1745   type_change_ok = FALSE;
1746   size_change_ok = FALSE;
1747   sec = *psec;
1748   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1749                               NULL, &hi, &skip, &override,
1750                               &type_change_ok, &size_change_ok))
1751     return FALSE;
1752
1753   if (skip)
1754     return TRUE;
1755
1756   if (override)
1757     {
1758       /* Here SHORTNAME is a versioned name, so we don't expect to see
1759          the type of override we do in the case above unless it is
1760          overridden by a versioned definition.  */
1761       if (hi->root.type != bfd_link_hash_defined
1762           && hi->root.type != bfd_link_hash_defweak)
1763         (*_bfd_error_handler)
1764           (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1765            abfd, shortname);
1766     }
1767   else
1768     {
1769       bh = &hi->root;
1770       if (! (_bfd_generic_link_add_one_symbol
1771              (info, abfd, shortname, BSF_INDIRECT,
1772               bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1773         return FALSE;
1774       hi = (struct elf_link_hash_entry *) bh;
1775
1776       /* If there is a duplicate definition somewhere, then HI may not
1777          point to an indirect symbol.  We will have reported an error
1778          to the user in that case.  */
1779
1780       if (hi->root.type == bfd_link_hash_indirect)
1781         {
1782           (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1783
1784           /* See if the new flags lead us to realize that the symbol
1785              must be dynamic.  */
1786           if (! *dynsym)
1787             {
1788               if (! dynamic)
1789                 {
1790                   if (! info->executable
1791                       || hi->ref_dynamic)
1792                     *dynsym = TRUE;
1793                 }
1794               else
1795                 {
1796                   if (hi->ref_regular)
1797                     *dynsym = TRUE;
1798                 }
1799             }
1800         }
1801     }
1802
1803   return TRUE;
1804 }
1805 \f
1806 /* This routine is used to export all defined symbols into the dynamic
1807    symbol table.  It is called via elf_link_hash_traverse.  */
1808
1809 static bfd_boolean
1810 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1811 {
1812   struct elf_info_failed *eif = (struct elf_info_failed *) data;
1813
1814   /* Ignore indirect symbols.  These are added by the versioning code.  */
1815   if (h->root.type == bfd_link_hash_indirect)
1816     return TRUE;
1817
1818   /* Ignore this if we won't export it.  */
1819   if (!eif->info->export_dynamic && !h->dynamic)
1820     return TRUE;
1821
1822   if (h->dynindx == -1
1823       && (h->def_regular
1824           || h->ref_regular))
1825     {
1826       bfd_boolean hide;
1827
1828       if (eif->verdefs == NULL
1829           || (bfd_find_version_for_sym (eif->verdefs, h->root.root.string, &hide)
1830               && !hide))
1831         {
1832           if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1833             {
1834               eif->failed = TRUE;
1835               return FALSE;
1836             }
1837         }
1838     }
1839
1840   return TRUE;
1841 }
1842 \f
1843 /* Look through the symbols which are defined in other shared
1844    libraries and referenced here.  Update the list of version
1845    dependencies.  This will be put into the .gnu.version_r section.
1846    This function is called via elf_link_hash_traverse.  */
1847
1848 static bfd_boolean
1849 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1850                                          void *data)
1851 {
1852   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1853   Elf_Internal_Verneed *t;
1854   Elf_Internal_Vernaux *a;
1855   bfd_size_type amt;
1856
1857   /* We only care about symbols defined in shared objects with version
1858      information.  */
1859   if (!h->def_dynamic
1860       || h->def_regular
1861       || h->dynindx == -1
1862       || h->verinfo.verdef == NULL)
1863     return TRUE;
1864
1865   /* See if we already know about this version.  */
1866   for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1867        t != NULL;
1868        t = t->vn_nextref)
1869     {
1870       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1871         continue;
1872
1873       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1874         if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1875           return TRUE;
1876
1877       break;
1878     }
1879
1880   /* This is a new version.  Add it to tree we are building.  */
1881
1882   if (t == NULL)
1883     {
1884       amt = sizeof *t;
1885       t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
1886       if (t == NULL)
1887         {
1888           rinfo->failed = TRUE;
1889           return FALSE;
1890         }
1891
1892       t->vn_bfd = h->verinfo.verdef->vd_bfd;
1893       t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1894       elf_tdata (rinfo->info->output_bfd)->verref = t;
1895     }
1896
1897   amt = sizeof *a;
1898   a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
1899   if (a == NULL)
1900     {
1901       rinfo->failed = TRUE;
1902       return FALSE;
1903     }
1904
1905   /* Note that we are copying a string pointer here, and testing it
1906      above.  If bfd_elf_string_from_elf_section is ever changed to
1907      discard the string data when low in memory, this will have to be
1908      fixed.  */
1909   a->vna_nodename = h->verinfo.verdef->vd_nodename;
1910
1911   a->vna_flags = h->verinfo.verdef->vd_flags;
1912   a->vna_nextptr = t->vn_auxptr;
1913
1914   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1915   ++rinfo->vers;
1916
1917   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1918
1919   t->vn_auxptr = a;
1920
1921   return TRUE;
1922 }
1923
1924 /* Figure out appropriate versions for all the symbols.  We may not
1925    have the version number script until we have read all of the input
1926    files, so until that point we don't know which symbols should be
1927    local.  This function is called via elf_link_hash_traverse.  */
1928
1929 static bfd_boolean
1930 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1931 {
1932   struct elf_info_failed *sinfo;
1933   struct bfd_link_info *info;
1934   const struct elf_backend_data *bed;
1935   struct elf_info_failed eif;
1936   char *p;
1937   bfd_size_type amt;
1938
1939   sinfo = (struct elf_info_failed *) data;
1940   info = sinfo->info;
1941
1942   /* Fix the symbol flags.  */
1943   eif.failed = FALSE;
1944   eif.info = info;
1945   if (! _bfd_elf_fix_symbol_flags (h, &eif))
1946     {
1947       if (eif.failed)
1948         sinfo->failed = TRUE;
1949       return FALSE;
1950     }
1951
1952   /* We only need version numbers for symbols defined in regular
1953      objects.  */
1954   if (!h->def_regular)
1955     return TRUE;
1956
1957   bed = get_elf_backend_data (info->output_bfd);
1958   p = strchr (h->root.root.string, ELF_VER_CHR);
1959   if (p != NULL && h->verinfo.vertree == NULL)
1960     {
1961       struct bfd_elf_version_tree *t;
1962       bfd_boolean hidden;
1963
1964       hidden = TRUE;
1965
1966       /* There are two consecutive ELF_VER_CHR characters if this is
1967          not a hidden symbol.  */
1968       ++p;
1969       if (*p == ELF_VER_CHR)
1970         {
1971           hidden = FALSE;
1972           ++p;
1973         }
1974
1975       /* If there is no version string, we can just return out.  */
1976       if (*p == '\0')
1977         {
1978           if (hidden)
1979             h->hidden = 1;
1980           return TRUE;
1981         }
1982
1983       /* Look for the version.  If we find it, it is no longer weak.  */
1984       for (t = sinfo->verdefs; t != NULL; t = t->next)
1985         {
1986           if (strcmp (t->name, p) == 0)
1987             {
1988               size_t len;
1989               char *alc;
1990               struct bfd_elf_version_expr *d;
1991
1992               len = p - h->root.root.string;
1993               alc = (char *) bfd_malloc (len);
1994               if (alc == NULL)
1995                 {
1996                   sinfo->failed = TRUE;
1997                   return FALSE;
1998                 }
1999               memcpy (alc, h->root.root.string, len - 1);
2000               alc[len - 1] = '\0';
2001               if (alc[len - 2] == ELF_VER_CHR)
2002                 alc[len - 2] = '\0';
2003
2004               h->verinfo.vertree = t;
2005               t->used = TRUE;
2006               d = NULL;
2007
2008               if (t->globals.list != NULL)
2009                 d = (*t->match) (&t->globals, NULL, alc);
2010
2011               /* See if there is anything to force this symbol to
2012                  local scope.  */
2013               if (d == NULL && t->locals.list != NULL)
2014                 {
2015                   d = (*t->match) (&t->locals, NULL, alc);
2016                   if (d != NULL
2017                       && h->dynindx != -1
2018                       && ! info->export_dynamic)
2019                     (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2020                 }
2021
2022               free (alc);
2023               break;
2024             }
2025         }
2026
2027       /* If we are building an application, we need to create a
2028          version node for this version.  */
2029       if (t == NULL && info->executable)
2030         {
2031           struct bfd_elf_version_tree **pp;
2032           int version_index;
2033
2034           /* If we aren't going to export this symbol, we don't need
2035              to worry about it.  */
2036           if (h->dynindx == -1)
2037             return TRUE;
2038
2039           amt = sizeof *t;
2040           t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2041           if (t == NULL)
2042             {
2043               sinfo->failed = TRUE;
2044               return FALSE;
2045             }
2046
2047           t->name = p;
2048           t->name_indx = (unsigned int) -1;
2049           t->used = TRUE;
2050
2051           version_index = 1;
2052           /* Don't count anonymous version tag.  */
2053           if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
2054             version_index = 0;
2055           for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
2056             ++version_index;
2057           t->vernum = version_index;
2058
2059           *pp = t;
2060
2061           h->verinfo.vertree = t;
2062         }
2063       else if (t == NULL)
2064         {
2065           /* We could not find the version for a symbol when
2066              generating a shared archive.  Return an error.  */
2067           (*_bfd_error_handler)
2068             (_("%B: version node not found for symbol %s"),
2069              info->output_bfd, h->root.root.string);
2070           bfd_set_error (bfd_error_bad_value);
2071           sinfo->failed = TRUE;
2072           return FALSE;
2073         }
2074
2075       if (hidden)
2076         h->hidden = 1;
2077     }
2078
2079   /* If we don't have a version for this symbol, see if we can find
2080      something.  */
2081   if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
2082     {
2083       bfd_boolean hide;
2084
2085       h->verinfo.vertree = bfd_find_version_for_sym (sinfo->verdefs,
2086                                                  h->root.root.string, &hide);
2087       if (h->verinfo.vertree != NULL && hide)
2088         (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2089     }
2090
2091   return TRUE;
2092 }
2093 \f
2094 /* Read and swap the relocs from the section indicated by SHDR.  This
2095    may be either a REL or a RELA section.  The relocations are
2096    translated into RELA relocations and stored in INTERNAL_RELOCS,
2097    which should have already been allocated to contain enough space.
2098    The EXTERNAL_RELOCS are a buffer where the external form of the
2099    relocations should be stored.
2100
2101    Returns FALSE if something goes wrong.  */
2102
2103 static bfd_boolean
2104 elf_link_read_relocs_from_section (bfd *abfd,
2105                                    asection *sec,
2106                                    Elf_Internal_Shdr *shdr,
2107                                    void *external_relocs,
2108                                    Elf_Internal_Rela *internal_relocs)
2109 {
2110   const struct elf_backend_data *bed;
2111   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2112   const bfd_byte *erela;
2113   const bfd_byte *erelaend;
2114   Elf_Internal_Rela *irela;
2115   Elf_Internal_Shdr *symtab_hdr;
2116   size_t nsyms;
2117
2118   /* Position ourselves at the start of the section.  */
2119   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2120     return FALSE;
2121
2122   /* Read the relocations.  */
2123   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2124     return FALSE;
2125
2126   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2127   nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2128
2129   bed = get_elf_backend_data (abfd);
2130
2131   /* Convert the external relocations to the internal format.  */
2132   if (shdr->sh_entsize == bed->s->sizeof_rel)
2133     swap_in = bed->s->swap_reloc_in;
2134   else if (shdr->sh_entsize == bed->s->sizeof_rela)
2135     swap_in = bed->s->swap_reloca_in;
2136   else
2137     {
2138       bfd_set_error (bfd_error_wrong_format);
2139       return FALSE;
2140     }
2141
2142   erela = (const bfd_byte *) external_relocs;
2143   erelaend = erela + shdr->sh_size;
2144   irela = internal_relocs;
2145   while (erela < erelaend)
2146     {
2147       bfd_vma r_symndx;
2148
2149       (*swap_in) (abfd, erela, irela);
2150       r_symndx = ELF32_R_SYM (irela->r_info);
2151       if (bed->s->arch_size == 64)
2152         r_symndx >>= 24;
2153       if (nsyms > 0)
2154         {
2155           if ((size_t) r_symndx >= nsyms)
2156             {
2157               (*_bfd_error_handler)
2158                 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2159                    " for offset 0x%lx in section `%A'"),
2160                  abfd, sec,
2161                  (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2162               bfd_set_error (bfd_error_bad_value);
2163               return FALSE;
2164             }
2165         }
2166       else if (r_symndx != STN_UNDEF)
2167         {
2168           (*_bfd_error_handler)
2169             (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2170                " when the object file has no symbol table"),
2171              abfd, sec,
2172              (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2173           bfd_set_error (bfd_error_bad_value);
2174           return FALSE;
2175         }
2176       irela += bed->s->int_rels_per_ext_rel;
2177       erela += shdr->sh_entsize;
2178     }
2179
2180   return TRUE;
2181 }
2182
2183 /* Read and swap the relocs for a section O.  They may have been
2184    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2185    not NULL, they are used as buffers to read into.  They are known to
2186    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
2187    the return value is allocated using either malloc or bfd_alloc,
2188    according to the KEEP_MEMORY argument.  If O has two relocation
2189    sections (both REL and RELA relocations), then the REL_HDR
2190    relocations will appear first in INTERNAL_RELOCS, followed by the
2191    RELA_HDR relocations.  */
2192
2193 Elf_Internal_Rela *
2194 _bfd_elf_link_read_relocs (bfd *abfd,
2195                            asection *o,
2196                            void *external_relocs,
2197                            Elf_Internal_Rela *internal_relocs,
2198                            bfd_boolean keep_memory)
2199 {
2200   void *alloc1 = NULL;
2201   Elf_Internal_Rela *alloc2 = NULL;
2202   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2203   struct bfd_elf_section_data *esdo = elf_section_data (o);
2204   Elf_Internal_Rela *internal_rela_relocs;
2205
2206   if (esdo->relocs != NULL)
2207     return esdo->relocs;
2208
2209   if (o->reloc_count == 0)
2210     return NULL;
2211
2212   if (internal_relocs == NULL)
2213     {
2214       bfd_size_type size;
2215
2216       size = o->reloc_count;
2217       size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2218       if (keep_memory)
2219         internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2220       else
2221         internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2222       if (internal_relocs == NULL)
2223         goto error_return;
2224     }
2225
2226   if (external_relocs == NULL)
2227     {
2228       bfd_size_type size = 0;
2229
2230       if (esdo->rel.hdr)
2231         size += esdo->rel.hdr->sh_size;
2232       if (esdo->rela.hdr)
2233         size += esdo->rela.hdr->sh_size;
2234
2235       alloc1 = bfd_malloc (size);
2236       if (alloc1 == NULL)
2237         goto error_return;
2238       external_relocs = alloc1;
2239     }
2240
2241   internal_rela_relocs = internal_relocs;
2242   if (esdo->rel.hdr)
2243     {
2244       if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2245                                               external_relocs,
2246                                               internal_relocs))
2247         goto error_return;
2248       external_relocs = (((bfd_byte *) external_relocs)
2249                          + esdo->rel.hdr->sh_size);
2250       internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2251                                * bed->s->int_rels_per_ext_rel);
2252     }
2253
2254   if (esdo->rela.hdr
2255       && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2256                                               external_relocs,
2257                                               internal_rela_relocs)))
2258     goto error_return;
2259
2260   /* Cache the results for next time, if we can.  */
2261   if (keep_memory)
2262     esdo->relocs = internal_relocs;
2263
2264   if (alloc1 != NULL)
2265     free (alloc1);
2266
2267   /* Don't free alloc2, since if it was allocated we are passing it
2268      back (under the name of internal_relocs).  */
2269
2270   return internal_relocs;
2271
2272  error_return:
2273   if (alloc1 != NULL)
2274     free (alloc1);
2275   if (alloc2 != NULL)
2276     {
2277       if (keep_memory)
2278         bfd_release (abfd, alloc2);
2279       else
2280         free (alloc2);
2281     }
2282   return NULL;
2283 }
2284
2285 /* Compute the size of, and allocate space for, REL_HDR which is the
2286    section header for a section containing relocations for O.  */
2287
2288 static bfd_boolean
2289 _bfd_elf_link_size_reloc_section (bfd *abfd,
2290                                   struct bfd_elf_section_reloc_data *reldata)
2291 {
2292   Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2293
2294   /* That allows us to calculate the size of the section.  */
2295   rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2296
2297   /* The contents field must last into write_object_contents, so we
2298      allocate it with bfd_alloc rather than malloc.  Also since we
2299      cannot be sure that the contents will actually be filled in,
2300      we zero the allocated space.  */
2301   rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2302   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2303     return FALSE;
2304
2305   if (reldata->hashes == NULL && reldata->count)
2306     {
2307       struct elf_link_hash_entry **p;
2308
2309       p = (struct elf_link_hash_entry **)
2310           bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *));
2311       if (p == NULL)
2312         return FALSE;
2313
2314       reldata->hashes = p;
2315     }
2316
2317   return TRUE;
2318 }
2319
2320 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2321    originated from the section given by INPUT_REL_HDR) to the
2322    OUTPUT_BFD.  */
2323
2324 bfd_boolean
2325 _bfd_elf_link_output_relocs (bfd *output_bfd,
2326                              asection *input_section,
2327                              Elf_Internal_Shdr *input_rel_hdr,
2328                              Elf_Internal_Rela *internal_relocs,
2329                              struct elf_link_hash_entry **rel_hash
2330                                ATTRIBUTE_UNUSED)
2331 {
2332   Elf_Internal_Rela *irela;
2333   Elf_Internal_Rela *irelaend;
2334   bfd_byte *erel;
2335   struct bfd_elf_section_reloc_data *output_reldata;
2336   asection *output_section;
2337   const struct elf_backend_data *bed;
2338   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2339   struct bfd_elf_section_data *esdo;
2340
2341   output_section = input_section->output_section;
2342
2343   bed = get_elf_backend_data (output_bfd);
2344   esdo = elf_section_data (output_section);
2345   if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2346     {
2347       output_reldata = &esdo->rel;
2348       swap_out = bed->s->swap_reloc_out;
2349     }
2350   else if (esdo->rela.hdr
2351            && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2352     {
2353       output_reldata = &esdo->rela;
2354       swap_out = bed->s->swap_reloca_out;
2355     }
2356   else
2357     {
2358       (*_bfd_error_handler)
2359         (_("%B: relocation size mismatch in %B section %A"),
2360          output_bfd, input_section->owner, input_section);
2361       bfd_set_error (bfd_error_wrong_format);
2362       return FALSE;
2363     }
2364
2365   erel = output_reldata->hdr->contents;
2366   erel += output_reldata->count * input_rel_hdr->sh_entsize;
2367   irela = internal_relocs;
2368   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2369                       * bed->s->int_rels_per_ext_rel);
2370   while (irela < irelaend)
2371     {
2372       (*swap_out) (output_bfd, irela, erel);
2373       irela += bed->s->int_rels_per_ext_rel;
2374       erel += input_rel_hdr->sh_entsize;
2375     }
2376
2377   /* Bump the counter, so that we know where to add the next set of
2378      relocations.  */
2379   output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2380
2381   return TRUE;
2382 }
2383 \f
2384 /* Make weak undefined symbols in PIE dynamic.  */
2385
2386 bfd_boolean
2387 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2388                                  struct elf_link_hash_entry *h)
2389 {
2390   if (info->pie
2391       && h->dynindx == -1
2392       && h->root.type == bfd_link_hash_undefweak)
2393     return bfd_elf_link_record_dynamic_symbol (info, h);
2394
2395   return TRUE;
2396 }
2397
2398 /* Fix up the flags for a symbol.  This handles various cases which
2399    can only be fixed after all the input files are seen.  This is
2400    currently called by both adjust_dynamic_symbol and
2401    assign_sym_version, which is unnecessary but perhaps more robust in
2402    the face of future changes.  */
2403
2404 static bfd_boolean
2405 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2406                            struct elf_info_failed *eif)
2407 {
2408   const struct elf_backend_data *bed;
2409
2410   /* If this symbol was mentioned in a non-ELF file, try to set
2411      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
2412      permit a non-ELF file to correctly refer to a symbol defined in
2413      an ELF dynamic object.  */
2414   if (h->non_elf)
2415     {
2416       while (h->root.type == bfd_link_hash_indirect)
2417         h = (struct elf_link_hash_entry *) h->root.u.i.link;
2418
2419       if (h->root.type != bfd_link_hash_defined
2420           && h->root.type != bfd_link_hash_defweak)
2421         {
2422           h->ref_regular = 1;
2423           h->ref_regular_nonweak = 1;
2424         }
2425       else
2426         {
2427           if (h->root.u.def.section->owner != NULL
2428               && (bfd_get_flavour (h->root.u.def.section->owner)
2429                   == bfd_target_elf_flavour))
2430             {
2431               h->ref_regular = 1;
2432               h->ref_regular_nonweak = 1;
2433             }
2434           else
2435             h->def_regular = 1;
2436         }
2437
2438       if (h->dynindx == -1
2439           && (h->def_dynamic
2440               || h->ref_dynamic))
2441         {
2442           if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2443             {
2444               eif->failed = TRUE;
2445               return FALSE;
2446             }
2447         }
2448     }
2449   else
2450     {
2451       /* Unfortunately, NON_ELF is only correct if the symbol
2452          was first seen in a non-ELF file.  Fortunately, if the symbol
2453          was first seen in an ELF file, we're probably OK unless the
2454          symbol was defined in a non-ELF file.  Catch that case here.
2455          FIXME: We're still in trouble if the symbol was first seen in
2456          a dynamic object, and then later in a non-ELF regular object.  */
2457       if ((h->root.type == bfd_link_hash_defined
2458            || h->root.type == bfd_link_hash_defweak)
2459           && !h->def_regular
2460           && (h->root.u.def.section->owner != NULL
2461               ? (bfd_get_flavour (h->root.u.def.section->owner)
2462                  != bfd_target_elf_flavour)
2463               : (bfd_is_abs_section (h->root.u.def.section)
2464                  && !h->def_dynamic)))
2465         h->def_regular = 1;
2466     }
2467
2468   /* Backend specific symbol fixup.  */
2469   bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2470   if (bed->elf_backend_fixup_symbol
2471       && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2472     return FALSE;
2473
2474   /* If this is a final link, and the symbol was defined as a common
2475      symbol in a regular object file, and there was no definition in
2476      any dynamic object, then the linker will have allocated space for
2477      the symbol in a common section but the DEF_REGULAR
2478      flag will not have been set.  */
2479   if (h->root.type == bfd_link_hash_defined
2480       && !h->def_regular
2481       && h->ref_regular
2482       && !h->def_dynamic
2483       && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2484     h->def_regular = 1;
2485
2486   /* If -Bsymbolic was used (which means to bind references to global
2487      symbols to the definition within the shared object), and this
2488      symbol was defined in a regular object, then it actually doesn't
2489      need a PLT entry.  Likewise, if the symbol has non-default
2490      visibility.  If the symbol has hidden or internal visibility, we
2491      will force it local.  */
2492   if (h->needs_plt
2493       && eif->info->shared
2494       && is_elf_hash_table (eif->info->hash)
2495       && (SYMBOLIC_BIND (eif->info, h)
2496           || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2497       && h->def_regular)
2498     {
2499       bfd_boolean force_local;
2500
2501       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2502                      || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2503       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2504     }
2505
2506   /* If a weak undefined symbol has non-default visibility, we also
2507      hide it from the dynamic linker.  */
2508   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2509       && h->root.type == bfd_link_hash_undefweak)
2510     (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2511
2512   /* If this is a weak defined symbol in a dynamic object, and we know
2513      the real definition in the dynamic object, copy interesting flags
2514      over to the real definition.  */
2515   if (h->u.weakdef != NULL)
2516     {
2517       struct elf_link_hash_entry *weakdef;
2518
2519       weakdef = h->u.weakdef;
2520       if (h->root.type == bfd_link_hash_indirect)
2521         h = (struct elf_link_hash_entry *) h->root.u.i.link;
2522
2523       BFD_ASSERT (h->root.type == bfd_link_hash_defined
2524                   || h->root.type == bfd_link_hash_defweak);
2525       BFD_ASSERT (weakdef->def_dynamic);
2526
2527       /* If the real definition is defined by a regular object file,
2528          don't do anything special.  See the longer description in
2529          _bfd_elf_adjust_dynamic_symbol, below.  */
2530       if (weakdef->def_regular)
2531         h->u.weakdef = NULL;
2532       else
2533         {
2534           BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2535                       || weakdef->root.type == bfd_link_hash_defweak);
2536           (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2537         }
2538     }
2539
2540   return TRUE;
2541 }
2542
2543 /* Make the backend pick a good value for a dynamic symbol.  This is
2544    called via elf_link_hash_traverse, and also calls itself
2545    recursively.  */
2546
2547 static bfd_boolean
2548 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2549 {
2550   struct elf_info_failed *eif = (struct elf_info_failed *) data;
2551   bfd *dynobj;
2552   const struct elf_backend_data *bed;
2553
2554   if (! is_elf_hash_table (eif->info->hash))
2555     return FALSE;
2556
2557   /* Ignore indirect symbols.  These are added by the versioning code.  */
2558   if (h->root.type == bfd_link_hash_indirect)
2559     return TRUE;
2560
2561   /* Fix the symbol flags.  */
2562   if (! _bfd_elf_fix_symbol_flags (h, eif))
2563     return FALSE;
2564
2565   /* If this symbol does not require a PLT entry, and it is not
2566      defined by a dynamic object, or is not referenced by a regular
2567      object, ignore it.  We do have to handle a weak defined symbol,
2568      even if no regular object refers to it, if we decided to add it
2569      to the dynamic symbol table.  FIXME: Do we normally need to worry
2570      about symbols which are defined by one dynamic object and
2571      referenced by another one?  */
2572   if (!h->needs_plt
2573       && h->type != STT_GNU_IFUNC
2574       && (h->def_regular
2575           || !h->def_dynamic
2576           || (!h->ref_regular
2577               && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2578     {
2579       h->plt = elf_hash_table (eif->info)->init_plt_offset;
2580       return TRUE;
2581     }
2582
2583   /* If we've already adjusted this symbol, don't do it again.  This
2584      can happen via a recursive call.  */
2585   if (h->dynamic_adjusted)
2586     return TRUE;
2587
2588   /* Don't look at this symbol again.  Note that we must set this
2589      after checking the above conditions, because we may look at a
2590      symbol once, decide not to do anything, and then get called
2591      recursively later after REF_REGULAR is set below.  */
2592   h->dynamic_adjusted = 1;
2593
2594   /* If this is a weak definition, and we know a real definition, and
2595      the real symbol is not itself defined by a regular object file,
2596      then get a good value for the real definition.  We handle the
2597      real symbol first, for the convenience of the backend routine.
2598
2599      Note that there is a confusing case here.  If the real definition
2600      is defined by a regular object file, we don't get the real symbol
2601      from the dynamic object, but we do get the weak symbol.  If the
2602      processor backend uses a COPY reloc, then if some routine in the
2603      dynamic object changes the real symbol, we will not see that
2604      change in the corresponding weak symbol.  This is the way other
2605      ELF linkers work as well, and seems to be a result of the shared
2606      library model.
2607
2608      I will clarify this issue.  Most SVR4 shared libraries define the
2609      variable _timezone and define timezone as a weak synonym.  The
2610      tzset call changes _timezone.  If you write
2611        extern int timezone;
2612        int _timezone = 5;
2613        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2614      you might expect that, since timezone is a synonym for _timezone,
2615      the same number will print both times.  However, if the processor
2616      backend uses a COPY reloc, then actually timezone will be copied
2617      into your process image, and, since you define _timezone
2618      yourself, _timezone will not.  Thus timezone and _timezone will
2619      wind up at different memory locations.  The tzset call will set
2620      _timezone, leaving timezone unchanged.  */
2621
2622   if (h->u.weakdef != NULL)
2623     {
2624       /* If we get to this point, we know there is an implicit
2625          reference by a regular object file via the weak symbol H.
2626          FIXME: Is this really true?  What if the traversal finds
2627          H->U.WEAKDEF before it finds H?  */
2628       h->u.weakdef->ref_regular = 1;
2629
2630       if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2631         return FALSE;
2632     }
2633
2634   /* If a symbol has no type and no size and does not require a PLT
2635      entry, then we are probably about to do the wrong thing here: we
2636      are probably going to create a COPY reloc for an empty object.
2637      This case can arise when a shared object is built with assembly
2638      code, and the assembly code fails to set the symbol type.  */
2639   if (h->size == 0
2640       && h->type == STT_NOTYPE
2641       && !h->needs_plt)
2642     (*_bfd_error_handler)
2643       (_("warning: type and size of dynamic symbol `%s' are not defined"),
2644        h->root.root.string);
2645
2646   dynobj = elf_hash_table (eif->info)->dynobj;
2647   bed = get_elf_backend_data (dynobj);
2648
2649   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2650     {
2651       eif->failed = TRUE;
2652       return FALSE;
2653     }
2654
2655   return TRUE;
2656 }
2657
2658 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2659    DYNBSS.  */
2660
2661 bfd_boolean
2662 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2663                               asection *dynbss)
2664 {
2665   unsigned int power_of_two;
2666   bfd_vma mask;
2667   asection *sec = h->root.u.def.section;
2668
2669   /* The section aligment of definition is the maximum alignment
2670      requirement of symbols defined in the section.  Since we don't
2671      know the symbol alignment requirement, we start with the
2672      maximum alignment and check low bits of the symbol address
2673      for the minimum alignment.  */
2674   power_of_two = bfd_get_section_alignment (sec->owner, sec);
2675   mask = ((bfd_vma) 1 << power_of_two) - 1;
2676   while ((h->root.u.def.value & mask) != 0)
2677     {
2678        mask >>= 1;
2679        --power_of_two;
2680     }
2681
2682   if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2683                                                 dynbss))
2684     {
2685       /* Adjust the section alignment if needed.  */
2686       if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2687                                        power_of_two))
2688         return FALSE;
2689     }
2690
2691   /* We make sure that the symbol will be aligned properly.  */
2692   dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2693
2694   /* Define the symbol as being at this point in DYNBSS.  */
2695   h->root.u.def.section = dynbss;
2696   h->root.u.def.value = dynbss->size;
2697
2698   /* Increment the size of DYNBSS to make room for the symbol.  */
2699   dynbss->size += h->size;
2700
2701   return TRUE;
2702 }
2703
2704 /* Adjust all external symbols pointing into SEC_MERGE sections
2705    to reflect the object merging within the sections.  */
2706
2707 static bfd_boolean
2708 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2709 {
2710   asection *sec;
2711
2712   if ((h->root.type == bfd_link_hash_defined
2713        || h->root.type == bfd_link_hash_defweak)
2714       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2715       && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2716     {
2717       bfd *output_bfd = (bfd *) data;
2718
2719       h->root.u.def.value =
2720         _bfd_merged_section_offset (output_bfd,
2721                                     &h->root.u.def.section,
2722                                     elf_section_data (sec)->sec_info,
2723                                     h->root.u.def.value);
2724     }
2725
2726   return TRUE;
2727 }
2728
2729 /* Returns false if the symbol referred to by H should be considered
2730    to resolve local to the current module, and true if it should be
2731    considered to bind dynamically.  */
2732
2733 bfd_boolean
2734 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2735                            struct bfd_link_info *info,
2736                            bfd_boolean not_local_protected)
2737 {
2738   bfd_boolean binding_stays_local_p;
2739   const struct elf_backend_data *bed;
2740   struct elf_link_hash_table *hash_table;
2741
2742   if (h == NULL)
2743     return FALSE;
2744
2745   while (h->root.type == bfd_link_hash_indirect
2746          || h->root.type == bfd_link_hash_warning)
2747     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2748
2749   /* If it was forced local, then clearly it's not dynamic.  */
2750   if (h->dynindx == -1)
2751     return FALSE;
2752   if (h->forced_local)
2753     return FALSE;
2754
2755   /* Identify the cases where name binding rules say that a
2756      visible symbol resolves locally.  */
2757   binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2758
2759   switch (ELF_ST_VISIBILITY (h->other))
2760     {
2761     case STV_INTERNAL:
2762     case STV_HIDDEN:
2763       return FALSE;
2764
2765     case STV_PROTECTED:
2766       hash_table = elf_hash_table (info);
2767       if (!is_elf_hash_table (hash_table))
2768         return FALSE;
2769
2770       bed = get_elf_backend_data (hash_table->dynobj);
2771
2772       /* Proper resolution for function pointer equality may require
2773          that these symbols perhaps be resolved dynamically, even though
2774          we should be resolving them to the current module.  */
2775       if (!not_local_protected || !bed->is_function_type (h->type))
2776         binding_stays_local_p = TRUE;
2777       break;
2778
2779     default:
2780       break;
2781     }
2782
2783   /* If it isn't defined locally, then clearly it's dynamic.  */
2784   if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2785     return TRUE;
2786
2787   /* Otherwise, the symbol is dynamic if binding rules don't tell
2788      us that it remains local.  */
2789   return !binding_stays_local_p;
2790 }
2791
2792 /* Return true if the symbol referred to by H should be considered
2793    to resolve local to the current module, and false otherwise.  Differs
2794    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2795    undefined symbols.  The two functions are virtually identical except
2796    for the place where forced_local and dynindx == -1 are tested.  If
2797    either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2798    the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2799    the symbol is local only for defined symbols.
2800    It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2801    !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2802    treatment of undefined weak symbols.  For those that do not make
2803    undefined weak symbols dynamic, both functions may return false.  */
2804
2805 bfd_boolean
2806 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2807                               struct bfd_link_info *info,
2808                               bfd_boolean local_protected)
2809 {
2810   const struct elf_backend_data *bed;
2811   struct elf_link_hash_table *hash_table;
2812
2813   /* If it's a local sym, of course we resolve locally.  */
2814   if (h == NULL)
2815     return TRUE;
2816
2817   /* STV_HIDDEN or STV_INTERNAL ones must be local.  */
2818   if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2819       || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2820     return TRUE;
2821
2822   /* Common symbols that become definitions don't get the DEF_REGULAR
2823      flag set, so test it first, and don't bail out.  */
2824   if (ELF_COMMON_DEF_P (h))
2825     /* Do nothing.  */;
2826   /* If we don't have a definition in a regular file, then we can't
2827      resolve locally.  The sym is either undefined or dynamic.  */
2828   else if (!h->def_regular)
2829     return FALSE;
2830
2831   /* Forced local symbols resolve locally.  */
2832   if (h->forced_local)
2833     return TRUE;
2834
2835   /* As do non-dynamic symbols.  */
2836   if (h->dynindx == -1)
2837     return TRUE;
2838
2839   /* At this point, we know the symbol is defined and dynamic.  In an
2840      executable it must resolve locally, likewise when building symbolic
2841      shared libraries.  */
2842   if (info->executable || SYMBOLIC_BIND (info, h))
2843     return TRUE;
2844
2845   /* Now deal with defined dynamic symbols in shared libraries.  Ones
2846      with default visibility might not resolve locally.  */
2847   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2848     return FALSE;
2849
2850   hash_table = elf_hash_table (info);
2851   if (!is_elf_hash_table (hash_table))
2852     return TRUE;
2853
2854   bed = get_elf_backend_data (hash_table->dynobj);
2855
2856   /* STV_PROTECTED non-function symbols are local.  */
2857   if (!bed->is_function_type (h->type))
2858     return TRUE;
2859
2860   /* Function pointer equality tests may require that STV_PROTECTED
2861      symbols be treated as dynamic symbols.  If the address of a
2862      function not defined in an executable is set to that function's
2863      plt entry in the executable, then the address of the function in
2864      a shared library must also be the plt entry in the executable.  */
2865   return local_protected;
2866 }
2867
2868 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2869    aligned.  Returns the first TLS output section.  */
2870
2871 struct bfd_section *
2872 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2873 {
2874   struct bfd_section *sec, *tls;
2875   unsigned int align = 0;
2876
2877   for (sec = obfd->sections; sec != NULL; sec = sec->next)
2878     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2879       break;
2880   tls = sec;
2881
2882   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2883     if (sec->alignment_power > align)
2884       align = sec->alignment_power;
2885
2886   elf_hash_table (info)->tls_sec = tls;
2887
2888   /* Ensure the alignment of the first section is the largest alignment,
2889      so that the tls segment starts aligned.  */
2890   if (tls != NULL)
2891     tls->alignment_power = align;
2892
2893   return tls;
2894 }
2895
2896 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
2897 static bfd_boolean
2898 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2899                                   Elf_Internal_Sym *sym)
2900 {
2901   const struct elf_backend_data *bed;
2902
2903   /* Local symbols do not count, but target specific ones might.  */
2904   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2905       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2906     return FALSE;
2907
2908   bed = get_elf_backend_data (abfd);
2909   /* Function symbols do not count.  */
2910   if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2911     return FALSE;
2912
2913   /* If the section is undefined, then so is the symbol.  */
2914   if (sym->st_shndx == SHN_UNDEF)
2915     return FALSE;
2916
2917   /* If the symbol is defined in the common section, then
2918      it is a common definition and so does not count.  */
2919   if (bed->common_definition (sym))
2920     return FALSE;
2921
2922   /* If the symbol is in a target specific section then we
2923      must rely upon the backend to tell us what it is.  */
2924   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2925     /* FIXME - this function is not coded yet:
2926
2927        return _bfd_is_global_symbol_definition (abfd, sym);
2928
2929        Instead for now assume that the definition is not global,
2930        Even if this is wrong, at least the linker will behave
2931        in the same way that it used to do.  */
2932     return FALSE;
2933
2934   return TRUE;
2935 }
2936
2937 /* Search the symbol table of the archive element of the archive ABFD
2938    whose archive map contains a mention of SYMDEF, and determine if
2939    the symbol is defined in this element.  */
2940 static bfd_boolean
2941 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2942 {
2943   Elf_Internal_Shdr * hdr;
2944   bfd_size_type symcount;
2945   bfd_size_type extsymcount;
2946   bfd_size_type extsymoff;
2947   Elf_Internal_Sym *isymbuf;
2948   Elf_Internal_Sym *isym;
2949   Elf_Internal_Sym *isymend;
2950   bfd_boolean result;
2951
2952   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2953   if (abfd == NULL)
2954     return FALSE;
2955
2956   if (! bfd_check_format (abfd, bfd_object))
2957     return FALSE;
2958
2959   /* If we have already included the element containing this symbol in the
2960      link then we do not need to include it again.  Just claim that any symbol
2961      it contains is not a definition, so that our caller will not decide to
2962      (re)include this element.  */
2963   if (abfd->archive_pass)
2964     return FALSE;
2965
2966   /* Select the appropriate symbol table.  */
2967   if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2968     hdr = &elf_tdata (abfd)->symtab_hdr;
2969   else
2970     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2971
2972   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2973
2974   /* The sh_info field of the symtab header tells us where the
2975      external symbols start.  We don't care about the local symbols.  */
2976   if (elf_bad_symtab (abfd))
2977     {
2978       extsymcount = symcount;
2979       extsymoff = 0;
2980     }
2981   else
2982     {
2983       extsymcount = symcount - hdr->sh_info;
2984       extsymoff = hdr->sh_info;
2985     }
2986
2987   if (extsymcount == 0)
2988     return FALSE;
2989
2990   /* Read in the symbol table.  */
2991   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2992                                   NULL, NULL, NULL);
2993   if (isymbuf == NULL)
2994     return FALSE;
2995
2996   /* Scan the symbol table looking for SYMDEF.  */
2997   result = FALSE;
2998   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2999     {
3000       const char *name;
3001
3002       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3003                                               isym->st_name);
3004       if (name == NULL)
3005         break;
3006
3007       if (strcmp (name, symdef->name) == 0)
3008         {
3009           result = is_global_data_symbol_definition (abfd, isym);
3010           break;
3011         }
3012     }
3013
3014   free (isymbuf);
3015
3016   return result;
3017 }
3018 \f
3019 /* Add an entry to the .dynamic table.  */
3020
3021 bfd_boolean
3022 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3023                             bfd_vma tag,
3024                             bfd_vma val)
3025 {
3026   struct elf_link_hash_table *hash_table;
3027   const struct elf_backend_data *bed;
3028   asection *s;
3029   bfd_size_type newsize;
3030   bfd_byte *newcontents;
3031   Elf_Internal_Dyn dyn;
3032
3033   hash_table = elf_hash_table (info);
3034   if (! is_elf_hash_table (hash_table))
3035     return FALSE;
3036
3037   bed = get_elf_backend_data (hash_table->dynobj);
3038   s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3039   BFD_ASSERT (s != NULL);
3040
3041   newsize = s->size + bed->s->sizeof_dyn;
3042   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3043   if (newcontents == NULL)
3044     return FALSE;
3045
3046   dyn.d_tag = tag;
3047   dyn.d_un.d_val = val;
3048   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3049
3050   s->size = newsize;
3051   s->contents = newcontents;
3052
3053   return TRUE;
3054 }
3055
3056 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3057    otherwise just check whether one already exists.  Returns -1 on error,
3058    1 if a DT_NEEDED tag already exists, and 0 on success.  */
3059
3060 static int
3061 elf_add_dt_needed_tag (bfd *abfd,
3062                        struct bfd_link_info *info,
3063                        const char *soname,
3064                        bfd_boolean do_it)
3065 {
3066   struct elf_link_hash_table *hash_table;
3067   bfd_size_type oldsize;
3068   bfd_size_type strindex;
3069
3070   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3071     return -1;
3072
3073   hash_table = elf_hash_table (info);
3074   oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3075   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3076   if (strindex == (bfd_size_type) -1)
3077     return -1;
3078
3079   if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3080     {
3081       asection *sdyn;
3082       const struct elf_backend_data *bed;
3083       bfd_byte *extdyn;
3084
3085       bed = get_elf_backend_data (hash_table->dynobj);
3086       sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3087       if (sdyn != NULL)
3088         for (extdyn = sdyn->contents;
3089              extdyn < sdyn->contents + sdyn->size;
3090              extdyn += bed->s->sizeof_dyn)
3091           {
3092             Elf_Internal_Dyn dyn;
3093
3094             bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3095             if (dyn.d_tag == DT_NEEDED
3096                 && dyn.d_un.d_val == strindex)
3097               {
3098                 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3099                 return 1;
3100               }
3101           }
3102     }
3103
3104   if (do_it)
3105     {
3106       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3107         return -1;
3108
3109       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3110         return -1;
3111     }
3112   else
3113     /* We were just checking for existence of the tag.  */
3114     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3115
3116   return 0;
3117 }
3118
3119 static bfd_boolean
3120 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3121 {
3122   for (; needed != NULL; needed = needed->next)
3123     if (strcmp (soname, needed->name) == 0)
3124       return TRUE;
3125
3126   return FALSE;
3127 }
3128
3129 /* Sort symbol by value and section.  */
3130 static int
3131 elf_sort_symbol (const void *arg1, const void *arg2)
3132 {
3133   const struct elf_link_hash_entry *h1;
3134   const struct elf_link_hash_entry *h2;
3135   bfd_signed_vma vdiff;
3136
3137   h1 = *(const struct elf_link_hash_entry **) arg1;
3138   h2 = *(const struct elf_link_hash_entry **) arg2;
3139   vdiff = h1->root.u.def.value - h2->root.u.def.value;
3140   if (vdiff != 0)
3141     return vdiff > 0 ? 1 : -1;
3142   else
3143     {
3144       long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3145       if (sdiff != 0)
3146         return sdiff > 0 ? 1 : -1;
3147     }
3148   return 0;
3149 }
3150
3151 /* This function is used to adjust offsets into .dynstr for
3152    dynamic symbols.  This is called via elf_link_hash_traverse.  */
3153
3154 static bfd_boolean
3155 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3156 {
3157   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3158
3159   if (h->dynindx != -1)
3160     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3161   return TRUE;
3162 }
3163
3164 /* Assign string offsets in .dynstr, update all structures referencing
3165    them.  */
3166
3167 static bfd_boolean
3168 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3169 {
3170   struct elf_link_hash_table *hash_table = elf_hash_table (info);
3171   struct elf_link_local_dynamic_entry *entry;
3172   struct elf_strtab_hash *dynstr = hash_table->dynstr;
3173   bfd *dynobj = hash_table->dynobj;
3174   asection *sdyn;
3175   bfd_size_type size;
3176   const struct elf_backend_data *bed;
3177   bfd_byte *extdyn;
3178
3179   _bfd_elf_strtab_finalize (dynstr);
3180   size = _bfd_elf_strtab_size (dynstr);
3181
3182   bed = get_elf_backend_data (dynobj);
3183   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3184   BFD_ASSERT (sdyn != NULL);
3185
3186   /* Update all .dynamic entries referencing .dynstr strings.  */
3187   for (extdyn = sdyn->contents;
3188        extdyn < sdyn->contents + sdyn->size;
3189        extdyn += bed->s->sizeof_dyn)
3190     {
3191       Elf_Internal_Dyn dyn;
3192
3193       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3194       switch (dyn.d_tag)
3195         {
3196         case DT_STRSZ:
3197           dyn.d_un.d_val = size;
3198           break;
3199         case DT_NEEDED:
3200         case DT_SONAME:
3201         case DT_RPATH:
3202         case DT_RUNPATH:
3203         case DT_FILTER:
3204         case DT_AUXILIARY:
3205         case DT_AUDIT:
3206         case DT_DEPAUDIT:
3207           dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3208           break;
3209         default:
3210           continue;
3211         }
3212       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3213     }
3214
3215   /* Now update local dynamic symbols.  */
3216   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3217     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3218                                                   entry->isym.st_name);
3219
3220   /* And the rest of dynamic symbols.  */
3221   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3222
3223   /* Adjust version definitions.  */
3224   if (elf_tdata (output_bfd)->cverdefs)
3225     {
3226       asection *s;
3227       bfd_byte *p;
3228       bfd_size_type i;
3229       Elf_Internal_Verdef def;
3230       Elf_Internal_Verdaux defaux;
3231
3232       s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3233       p = s->contents;
3234       do
3235         {
3236           _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3237                                    &def);
3238           p += sizeof (Elf_External_Verdef);
3239           if (def.vd_aux != sizeof (Elf_External_Verdef))
3240             continue;
3241           for (i = 0; i < def.vd_cnt; ++i)
3242             {
3243               _bfd_elf_swap_verdaux_in (output_bfd,
3244                                         (Elf_External_Verdaux *) p, &defaux);
3245               defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3246                                                         defaux.vda_name);
3247               _bfd_elf_swap_verdaux_out (output_bfd,
3248                                          &defaux, (Elf_External_Verdaux *) p);
3249               p += sizeof (Elf_External_Verdaux);
3250             }
3251         }
3252       while (def.vd_next);
3253     }
3254
3255   /* Adjust version references.  */
3256   if (elf_tdata (output_bfd)->verref)
3257     {
3258       asection *s;
3259       bfd_byte *p;
3260       bfd_size_type i;
3261       Elf_Internal_Verneed need;
3262       Elf_Internal_Vernaux needaux;
3263
3264       s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3265       p = s->contents;
3266       do
3267         {
3268           _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3269                                     &need);
3270           need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3271           _bfd_elf_swap_verneed_out (output_bfd, &need,
3272                                      (Elf_External_Verneed *) p);
3273           p += sizeof (Elf_External_Verneed);
3274           for (i = 0; i < need.vn_cnt; ++i)
3275             {
3276               _bfd_elf_swap_vernaux_in (output_bfd,
3277                                         (Elf_External_Vernaux *) p, &needaux);
3278               needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3279                                                          needaux.vna_name);
3280               _bfd_elf_swap_vernaux_out (output_bfd,
3281                                          &needaux,
3282                                          (Elf_External_Vernaux *) p);
3283               p += sizeof (Elf_External_Vernaux);
3284             }
3285         }
3286       while (need.vn_next);
3287     }
3288
3289   return TRUE;
3290 }
3291 \f
3292 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3293    The default is to only match when the INPUT and OUTPUT are exactly
3294    the same target.  */
3295
3296 bfd_boolean
3297 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3298                                     const bfd_target *output)
3299 {
3300   return input == output;
3301 }
3302
3303 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3304    This version is used when different targets for the same architecture
3305    are virtually identical.  */
3306
3307 bfd_boolean
3308 _bfd_elf_relocs_compatible (const bfd_target *input,
3309                             const bfd_target *output)
3310 {
3311   const struct elf_backend_data *obed, *ibed;
3312
3313   if (input == output)
3314     return TRUE;
3315
3316   ibed = xvec_get_elf_backend_data (input);
3317   obed = xvec_get_elf_backend_data (output);
3318
3319   if (ibed->arch != obed->arch)
3320     return FALSE;
3321
3322   /* If both backends are using this function, deem them compatible.  */
3323   return ibed->relocs_compatible == obed->relocs_compatible;
3324 }
3325
3326 /* Add symbols from an ELF object file to the linker hash table.  */
3327
3328 static bfd_boolean
3329 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3330 {
3331   Elf_Internal_Ehdr *ehdr;
3332   Elf_Internal_Shdr *hdr;
3333   bfd_size_type symcount;
3334   bfd_size_type extsymcount;
3335   bfd_size_type extsymoff;
3336   struct elf_link_hash_entry **sym_hash;
3337   bfd_boolean dynamic;
3338   Elf_External_Versym *extversym = NULL;
3339   Elf_External_Versym *ever;
3340   struct elf_link_hash_entry *weaks;
3341   struct elf_link_hash_entry **nondeflt_vers = NULL;
3342   bfd_size_type nondeflt_vers_cnt = 0;
3343   Elf_Internal_Sym *isymbuf = NULL;
3344   Elf_Internal_Sym *isym;
3345   Elf_Internal_Sym *isymend;
3346   const struct elf_backend_data *bed;
3347   bfd_boolean add_needed;
3348   struct elf_link_hash_table *htab;
3349   bfd_size_type amt;
3350   void *alloc_mark = NULL;
3351   struct bfd_hash_entry **old_table = NULL;
3352   unsigned int old_size = 0;
3353   unsigned int old_count = 0;
3354   void *old_tab = NULL;
3355   void *old_hash;
3356   void *old_ent;
3357   struct bfd_link_hash_entry *old_undefs = NULL;
3358   struct bfd_link_hash_entry *old_undefs_tail = NULL;
3359   long old_dynsymcount = 0;
3360   size_t tabsize = 0;
3361   size_t hashsize = 0;
3362
3363   htab = elf_hash_table (info);
3364   bed = get_elf_backend_data (abfd);
3365
3366   if ((abfd->flags & DYNAMIC) == 0)
3367     dynamic = FALSE;
3368   else
3369     {
3370       dynamic = TRUE;
3371
3372       /* You can't use -r against a dynamic object.  Also, there's no
3373          hope of using a dynamic object which does not exactly match
3374          the format of the output file.  */
3375       if (info->relocatable
3376           || !is_elf_hash_table (htab)
3377           || info->output_bfd->xvec != abfd->xvec)
3378         {
3379           if (info->relocatable)
3380             bfd_set_error (bfd_error_invalid_operation);
3381           else
3382             bfd_set_error (bfd_error_wrong_format);
3383           goto error_return;
3384         }
3385     }
3386
3387   ehdr = elf_elfheader (abfd);
3388   if (info->warn_alternate_em
3389       && bed->elf_machine_code != ehdr->e_machine
3390       && ((bed->elf_machine_alt1 != 0
3391            && ehdr->e_machine == bed->elf_machine_alt1)
3392           || (bed->elf_machine_alt2 != 0
3393               && ehdr->e_machine == bed->elf_machine_alt2)))
3394     info->callbacks->einfo
3395       (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3396        ehdr->e_machine, abfd, bed->elf_machine_code);
3397
3398   /* As a GNU extension, any input sections which are named
3399      .gnu.warning.SYMBOL are treated as warning symbols for the given
3400      symbol.  This differs from .gnu.warning sections, which generate
3401      warnings when they are included in an output file.  */
3402   /* PR 12761: Also generate this warning when building shared libraries.  */
3403   if (info->executable || info->shared)
3404     {
3405       asection *s;
3406
3407       for (s = abfd->sections; s != NULL; s = s->next)
3408         {
3409           const char *name;
3410
3411           name = bfd_get_section_name (abfd, s);
3412           if (CONST_STRNEQ (name, ".gnu.warning."))
3413             {
3414               char *msg;
3415               bfd_size_type sz;
3416
3417               name += sizeof ".gnu.warning." - 1;
3418
3419               /* If this is a shared object, then look up the symbol
3420                  in the hash table.  If it is there, and it is already
3421                  been defined, then we will not be using the entry
3422                  from this shared object, so we don't need to warn.
3423                  FIXME: If we see the definition in a regular object
3424                  later on, we will warn, but we shouldn't.  The only
3425                  fix is to keep track of what warnings we are supposed
3426                  to emit, and then handle them all at the end of the
3427                  link.  */
3428               if (dynamic)
3429                 {
3430                   struct elf_link_hash_entry *h;
3431
3432                   h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3433
3434                   /* FIXME: What about bfd_link_hash_common?  */
3435                   if (h != NULL
3436                       && (h->root.type == bfd_link_hash_defined
3437                           || h->root.type == bfd_link_hash_defweak))
3438                     {
3439                       /* We don't want to issue this warning.  Clobber
3440                          the section size so that the warning does not
3441                          get copied into the output file.  */
3442                       s->size = 0;
3443                       continue;
3444                     }
3445                 }
3446
3447               sz = s->size;
3448               msg = (char *) bfd_alloc (abfd, sz + 1);
3449               if (msg == NULL)
3450                 goto error_return;
3451
3452               if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3453                 goto error_return;
3454
3455               msg[sz] = '\0';
3456
3457               if (! (_bfd_generic_link_add_one_symbol
3458                      (info, abfd, name, BSF_WARNING, s, 0, msg,
3459                       FALSE, bed->collect, NULL)))
3460                 goto error_return;
3461
3462               if (! info->relocatable)
3463                 {
3464                   /* Clobber the section size so that the warning does
3465                      not get copied into the output file.  */
3466                   s->size = 0;
3467
3468                   /* Also set SEC_EXCLUDE, so that symbols defined in
3469                      the warning section don't get copied to the output.  */
3470                   s->flags |= SEC_EXCLUDE;
3471                 }
3472             }
3473         }
3474     }
3475
3476   add_needed = TRUE;
3477   if (! dynamic)
3478     {
3479       /* If we are creating a shared library, create all the dynamic
3480          sections immediately.  We need to attach them to something,
3481          so we attach them to this BFD, provided it is the right
3482          format.  FIXME: If there are no input BFD's of the same
3483          format as the output, we can't make a shared library.  */
3484       if (info->shared
3485           && is_elf_hash_table (htab)
3486           && info->output_bfd->xvec == abfd->xvec
3487           && !htab->dynamic_sections_created)
3488         {
3489           if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3490             goto error_return;
3491         }
3492     }
3493   else if (!is_elf_hash_table (htab))
3494     goto error_return;
3495   else
3496     {
3497       asection *s;
3498       const char *soname = NULL;
3499       char *audit = NULL;
3500       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3501       int ret;
3502
3503       /* ld --just-symbols and dynamic objects don't mix very well.
3504          ld shouldn't allow it.  */
3505       if ((s = abfd->sections) != NULL
3506           && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3507         abort ();
3508
3509       /* If this dynamic lib was specified on the command line with
3510          --as-needed in effect, then we don't want to add a DT_NEEDED
3511          tag unless the lib is actually used.  Similary for libs brought
3512          in by another lib's DT_NEEDED.  When --no-add-needed is used
3513          on a dynamic lib, we don't want to add a DT_NEEDED entry for
3514          any dynamic library in DT_NEEDED tags in the dynamic lib at
3515          all.  */
3516       add_needed = (elf_dyn_lib_class (abfd)
3517                     & (DYN_AS_NEEDED | DYN_DT_NEEDED
3518                        | DYN_NO_NEEDED)) == 0;
3519
3520       s = bfd_get_section_by_name (abfd, ".dynamic");
3521       if (s != NULL)
3522         {
3523           bfd_byte *dynbuf;
3524           bfd_byte *extdyn;
3525           unsigned int elfsec;
3526           unsigned long shlink;
3527
3528           if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3529             {
3530 error_free_dyn:
3531               free (dynbuf);
3532               goto error_return;
3533             }
3534
3535           elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3536           if (elfsec == SHN_BAD)
3537             goto error_free_dyn;
3538           shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3539
3540           for (extdyn = dynbuf;
3541                extdyn < dynbuf + s->size;
3542                extdyn += bed->s->sizeof_dyn)
3543             {
3544               Elf_Internal_Dyn dyn;
3545
3546               bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3547               if (dyn.d_tag == DT_SONAME)
3548                 {
3549                   unsigned int tagv = dyn.d_un.d_val;
3550                   soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3551                   if (soname == NULL)
3552                     goto error_free_dyn;
3553                 }
3554               if (dyn.d_tag == DT_NEEDED)
3555                 {
3556                   struct bfd_link_needed_list *n, **pn;
3557                   char *fnm, *anm;
3558                   unsigned int tagv = dyn.d_un.d_val;
3559
3560                   amt = sizeof (struct bfd_link_needed_list);
3561                   n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3562                   fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3563                   if (n == NULL || fnm == NULL)
3564                     goto error_free_dyn;
3565                   amt = strlen (fnm) + 1;
3566                   anm = (char *) bfd_alloc (abfd, amt);
3567                   if (anm == NULL)
3568                     goto error_free_dyn;
3569                   memcpy (anm, fnm, amt);
3570                   n->name = anm;
3571                   n->by = abfd;
3572                   n->next = NULL;
3573                   for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3574                     ;
3575                   *pn = n;
3576                 }
3577               if (dyn.d_tag == DT_RUNPATH)
3578                 {
3579                   struct bfd_link_needed_list *n, **pn;
3580                   char *fnm, *anm;
3581                   unsigned int tagv = dyn.d_un.d_val;
3582
3583                   amt = sizeof (struct bfd_link_needed_list);
3584                   n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3585                   fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3586                   if (n == NULL || fnm == NULL)
3587                     goto error_free_dyn;
3588                   amt = strlen (fnm) + 1;
3589                   anm = (char *) bfd_alloc (abfd, amt);
3590                   if (anm == NULL)
3591                     goto error_free_dyn;
3592                   memcpy (anm, fnm, amt);
3593                   n->name = anm;
3594                   n->by = abfd;
3595                   n->next = NULL;
3596                   for (pn = & runpath;
3597                        *pn != NULL;
3598                        pn = &(*pn)->next)
3599                     ;
3600                   *pn = n;
3601                 }
3602               /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
3603               if (!runpath && dyn.d_tag == DT_RPATH)
3604                 {
3605                   struct bfd_link_needed_list *n, **pn;
3606                   char *fnm, *anm;
3607                   unsigned int tagv = dyn.d_un.d_val;
3608
3609                   amt = sizeof (struct bfd_link_needed_list);
3610                   n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3611                   fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3612                   if (n == NULL || fnm == NULL)
3613                     goto error_free_dyn;
3614                   amt = strlen (fnm) + 1;
3615                   anm = (char *) bfd_alloc (abfd, amt);
3616                   if (anm == NULL)
3617                     goto error_free_dyn;
3618                   memcpy (anm, fnm, amt);
3619                   n->name = anm;
3620                   n->by = abfd;
3621                   n->next = NULL;
3622                   for (pn = & rpath;
3623                        *pn != NULL;
3624                        pn = &(*pn)->next)
3625                     ;
3626                   *pn = n;
3627                 }
3628               if (dyn.d_tag == DT_AUDIT)
3629                 {
3630                   unsigned int tagv = dyn.d_un.d_val;
3631                   audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3632                 }
3633             }
3634
3635           free (dynbuf);
3636         }
3637
3638       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
3639          frees all more recently bfd_alloc'd blocks as well.  */
3640       if (runpath)
3641         rpath = runpath;
3642
3643       if (rpath)
3644         {
3645           struct bfd_link_needed_list **pn;
3646           for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3647             ;
3648           *pn = rpath;
3649         }
3650
3651       /* We do not want to include any of the sections in a dynamic
3652          object in the output file.  We hack by simply clobbering the
3653          list of sections in the BFD.  This could be handled more
3654          cleanly by, say, a new section flag; the existing
3655          SEC_NEVER_LOAD flag is not the one we want, because that one
3656          still implies that the section takes up space in the output
3657          file.  */
3658       bfd_section_list_clear (abfd);
3659
3660       /* Find the name to use in a DT_NEEDED entry that refers to this
3661          object.  If the object has a DT_SONAME entry, we use it.
3662          Otherwise, if the generic linker stuck something in
3663          elf_dt_name, we use that.  Otherwise, we just use the file
3664          name.  */
3665       if (soname == NULL || *soname == '\0')
3666         {
3667           soname = elf_dt_name (abfd);
3668           if (soname == NULL || *soname == '\0')
3669             soname = bfd_get_filename (abfd);
3670         }
3671
3672       /* Save the SONAME because sometimes the linker emulation code
3673          will need to know it.  */
3674       elf_dt_name (abfd) = soname;
3675
3676       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3677       if (ret < 0)
3678         goto error_return;
3679
3680       /* If we have already included this dynamic object in the
3681          link, just ignore it.  There is no reason to include a
3682          particular dynamic object more than once.  */
3683       if (ret > 0)
3684         return TRUE;
3685
3686       /* Save the DT_AUDIT entry for the linker emulation code. */
3687       elf_dt_audit (abfd) = audit; 
3688     }
3689
3690   /* If this is a dynamic object, we always link against the .dynsym
3691      symbol table, not the .symtab symbol table.  The dynamic linker
3692      will only see the .dynsym symbol table, so there is no reason to
3693      look at .symtab for a dynamic object.  */
3694
3695   if (! dynamic || elf_dynsymtab (abfd) == 0)
3696     hdr = &elf_tdata (abfd)->symtab_hdr;
3697   else
3698     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3699
3700   symcount = hdr->sh_size / bed->s->sizeof_sym;
3701
3702   /* The sh_info field of the symtab header tells us where the
3703      external symbols start.  We don't care about the local symbols at
3704      this point.  */
3705   if (elf_bad_symtab (abfd))
3706     {
3707       extsymcount = symcount;
3708       extsymoff = 0;
3709     }
3710   else
3711     {
3712       extsymcount = symcount - hdr->sh_info;
3713       extsymoff = hdr->sh_info;
3714     }
3715
3716   sym_hash = NULL;
3717   if (extsymcount != 0)
3718     {
3719       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3720                                       NULL, NULL, NULL);
3721       if (isymbuf == NULL)
3722         goto error_return;
3723
3724       /* We store a pointer to the hash table entry for each external
3725          symbol.  */
3726       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3727       sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
3728       if (sym_hash == NULL)
3729         goto error_free_sym;
3730       elf_sym_hashes (abfd) = sym_hash;
3731     }
3732
3733   if (dynamic)
3734     {
3735       /* Read in any version definitions.  */
3736       if (!_bfd_elf_slurp_version_tables (abfd,
3737                                           info->default_imported_symver))
3738         goto error_free_sym;
3739
3740       /* Read in the symbol versions, but don't bother to convert them
3741          to internal format.  */
3742       if (elf_dynversym (abfd) != 0)
3743         {
3744           Elf_Internal_Shdr *versymhdr;
3745
3746           versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3747           extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3748           if (extversym == NULL)
3749             goto error_free_sym;
3750           amt = versymhdr->sh_size;
3751           if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3752               || bfd_bread (extversym, amt, abfd) != amt)
3753             goto error_free_vers;
3754         }
3755     }
3756
3757   /* If we are loading an as-needed shared lib, save the symbol table
3758      state before we start adding symbols.  If the lib turns out
3759      to be unneeded, restore the state.  */
3760   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3761     {
3762       unsigned int i;
3763       size_t entsize;
3764
3765       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3766         {
3767           struct bfd_hash_entry *p;
3768           struct elf_link_hash_entry *h;
3769
3770           for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3771             {
3772               h = (struct elf_link_hash_entry *) p;
3773               entsize += htab->root.table.entsize;
3774               if (h->root.type == bfd_link_hash_warning)
3775                 entsize += htab->root.table.entsize;
3776             }
3777         }
3778
3779       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3780       hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3781       old_tab = bfd_malloc (tabsize + entsize + hashsize);
3782       if (old_tab == NULL)
3783         goto error_free_vers;
3784
3785       /* Remember the current objalloc pointer, so that all mem for
3786          symbols added can later be reclaimed.  */
3787       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3788       if (alloc_mark == NULL)
3789         goto error_free_vers;
3790
3791       /* Make a special call to the linker "notice" function to
3792          tell it that we are about to handle an as-needed lib.  */
3793       if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3794                                        notice_as_needed, 0, NULL))
3795         goto error_free_vers;
3796
3797       /* Clone the symbol table and sym hashes.  Remember some
3798          pointers into the symbol table, and dynamic symbol count.  */
3799       old_hash = (char *) old_tab + tabsize;
3800       old_ent = (char *) old_hash + hashsize;
3801       memcpy (old_tab, htab->root.table.table, tabsize);
3802       memcpy (old_hash, sym_hash, hashsize);
3803       old_undefs = htab->root.undefs;
3804       old_undefs_tail = htab->root.undefs_tail;
3805       old_table = htab->root.table.table;
3806       old_size = htab->root.table.size;
3807       old_count = htab->root.table.count;
3808       old_dynsymcount = htab->dynsymcount;
3809
3810       for (i = 0; i < htab->root.table.size; i++)
3811         {
3812           struct bfd_hash_entry *p;
3813           struct elf_link_hash_entry *h;
3814
3815           for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3816             {
3817               memcpy (old_ent, p, htab->root.table.entsize);
3818               old_ent = (char *) old_ent + htab->root.table.entsize;
3819               h = (struct elf_link_hash_entry *) p;
3820               if (h->root.type == bfd_link_hash_warning)
3821                 {
3822                   memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3823                   old_ent = (char *) old_ent + htab->root.table.entsize;
3824                 }
3825             }
3826         }
3827     }
3828
3829   weaks = NULL;
3830   ever = extversym != NULL ? extversym + extsymoff : NULL;
3831   for (isym = isymbuf, isymend = isymbuf + extsymcount;
3832        isym < isymend;
3833        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3834     {
3835       int bind;
3836       bfd_vma value;
3837       asection *sec, *new_sec;
3838       flagword flags;
3839       const char *name;
3840       struct elf_link_hash_entry *h;
3841       bfd_boolean definition;
3842       bfd_boolean size_change_ok;
3843       bfd_boolean type_change_ok;
3844       bfd_boolean new_weakdef;
3845       bfd_boolean override;
3846       bfd_boolean common;
3847       unsigned int old_alignment;
3848       bfd *old_bfd;
3849       bfd * undef_bfd = NULL;
3850
3851       override = FALSE;
3852
3853       flags = BSF_NO_FLAGS;
3854       sec = NULL;
3855       value = isym->st_value;
3856       *sym_hash = NULL;
3857       common = bed->common_definition (isym);
3858
3859       bind = ELF_ST_BIND (isym->st_info);
3860       switch (bind)
3861         {
3862         case STB_LOCAL:
3863           /* This should be impossible, since ELF requires that all
3864              global symbols follow all local symbols, and that sh_info
3865              point to the first global symbol.  Unfortunately, Irix 5
3866              screws this up.  */
3867           continue;
3868
3869         case STB_GLOBAL:
3870           if (isym->st_shndx != SHN_UNDEF && !common)
3871             flags = BSF_GLOBAL;
3872           break;
3873
3874         case STB_WEAK:
3875           flags = BSF_WEAK;
3876           break;
3877
3878         case STB_GNU_UNIQUE:
3879           flags = BSF_GNU_UNIQUE;
3880           break;
3881
3882         default:
3883           /* Leave it up to the processor backend.  */
3884           break;
3885         }
3886
3887       if (isym->st_shndx == SHN_UNDEF)
3888         sec = bfd_und_section_ptr;
3889       else if (isym->st_shndx == SHN_ABS)
3890         sec = bfd_abs_section_ptr;
3891       else if (isym->st_shndx == SHN_COMMON)
3892         {
3893           sec = bfd_com_section_ptr;
3894           /* What ELF calls the size we call the value.  What ELF
3895              calls the value we call the alignment.  */
3896           value = isym->st_size;
3897         }
3898       else
3899         {
3900           sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3901           if (sec == NULL)
3902             sec = bfd_abs_section_ptr;
3903           else if (elf_discarded_section (sec))
3904             {
3905               /* Symbols from discarded section are undefined.  We keep
3906                  its visibility.  */
3907               sec = bfd_und_section_ptr;
3908               isym->st_shndx = SHN_UNDEF;
3909             }
3910           else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3911             value -= sec->vma;
3912         }
3913
3914       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3915                                               isym->st_name);
3916       if (name == NULL)
3917         goto error_free_vers;
3918
3919       if (isym->st_shndx == SHN_COMMON
3920           && (abfd->flags & BFD_PLUGIN) != 0)
3921         {
3922           asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3923
3924           if (xc == NULL)
3925             {
3926               flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3927                                  | SEC_EXCLUDE);
3928               xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3929               if (xc == NULL)
3930                 goto error_free_vers;
3931             }
3932           sec = xc;
3933         }
3934       else if (isym->st_shndx == SHN_COMMON
3935                && ELF_ST_TYPE (isym->st_info) == STT_TLS
3936                && !info->relocatable)
3937         {
3938           asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3939
3940           if (tcomm == NULL)
3941             {
3942               flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3943                                  | SEC_LINKER_CREATED);
3944               tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3945               if (tcomm == NULL)
3946                 goto error_free_vers;
3947             }
3948           sec = tcomm;
3949         }
3950       else if (bed->elf_add_symbol_hook)
3951         {
3952           if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3953                                              &sec, &value))
3954             goto error_free_vers;
3955
3956           /* The hook function sets the name to NULL if this symbol
3957              should be skipped for some reason.  */
3958           if (name == NULL)
3959             continue;
3960         }
3961
3962       /* Sanity check that all possibilities were handled.  */
3963       if (sec == NULL)
3964         {
3965           bfd_set_error (bfd_error_bad_value);
3966           goto error_free_vers;
3967         }
3968
3969       if (bfd_is_und_section (sec)
3970           || bfd_is_com_section (sec))
3971         definition = FALSE;
3972       else
3973         definition = TRUE;
3974
3975       size_change_ok = FALSE;
3976       type_change_ok = bed->type_change_ok;
3977       old_alignment = 0;
3978       old_bfd = NULL;
3979       new_sec = sec;
3980
3981       if (is_elf_hash_table (htab))
3982         {
3983           Elf_Internal_Versym iver;
3984           unsigned int vernum = 0;
3985           bfd_boolean skip;
3986
3987           /* If this is a definition of a symbol which was previously
3988              referenced in a non-weak manner then make a note of the bfd
3989              that contained the reference.  This is used if we need to
3990              refer to the source of the reference later on.  */
3991           if (! bfd_is_und_section (sec))
3992             {
3993               h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
3994
3995               if (h != NULL
3996                   && h->root.type == bfd_link_hash_undefined
3997                   && h->root.u.undef.abfd)
3998                 undef_bfd = h->root.u.undef.abfd;
3999             }
4000           
4001           if (ever == NULL)
4002             {
4003               if (info->default_imported_symver)
4004                 /* Use the default symbol version created earlier.  */
4005                 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4006               else
4007                 iver.vs_vers = 0;
4008             }
4009           else
4010             _bfd_elf_swap_versym_in (abfd, ever, &iver);
4011
4012           vernum = iver.vs_vers & VERSYM_VERSION;
4013
4014           /* If this is a hidden symbol, or if it is not version
4015              1, we append the version name to the symbol name.
4016              However, we do not modify a non-hidden absolute symbol
4017              if it is not a function, because it might be the version
4018              symbol itself.  FIXME: What if it isn't?  */
4019           if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4020               || (vernum > 1
4021                   && (!bfd_is_abs_section (sec)
4022                       || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4023             {
4024               const char *verstr;
4025               size_t namelen, verlen, newlen;
4026               char *newname, *p;
4027
4028               if (isym->st_shndx != SHN_UNDEF)
4029                 {
4030                   if (vernum > elf_tdata (abfd)->cverdefs)
4031                     verstr = NULL;
4032                   else if (vernum > 1)
4033                     verstr =
4034                       elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4035                   else
4036                     verstr = "";
4037
4038                   if (verstr == NULL)
4039                     {
4040                       (*_bfd_error_handler)
4041                         (_("%B: %s: invalid version %u (max %d)"),
4042                          abfd, name, vernum,
4043                          elf_tdata (abfd)->cverdefs);
4044                       bfd_set_error (bfd_error_bad_value);
4045                       goto error_free_vers;
4046                     }
4047                 }
4048               else
4049                 {
4050                   /* We cannot simply test for the number of
4051                      entries in the VERNEED section since the
4052                      numbers for the needed versions do not start
4053                      at 0.  */
4054                   Elf_Internal_Verneed *t;
4055
4056                   verstr = NULL;
4057                   for (t = elf_tdata (abfd)->verref;
4058                        t != NULL;
4059                        t = t->vn_nextref)
4060                     {
4061                       Elf_Internal_Vernaux *a;
4062
4063                       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4064                         {
4065                           if (a->vna_other == vernum)
4066                             {
4067                               verstr = a->vna_nodename;
4068                               break;
4069                             }
4070                         }
4071                       if (a != NULL)
4072                         break;
4073                     }
4074                   if (verstr == NULL)
4075                     {
4076                       (*_bfd_error_handler)
4077                         (_("%B: %s: invalid needed version %d"),
4078                          abfd, name, vernum);
4079                       bfd_set_error (bfd_error_bad_value);
4080                       goto error_free_vers;
4081                     }
4082                 }
4083
4084               namelen = strlen (name);
4085               verlen = strlen (verstr);
4086               newlen = namelen + verlen + 2;
4087               if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4088                   && isym->st_shndx != SHN_UNDEF)
4089                 ++newlen;
4090
4091               newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4092               if (newname == NULL)
4093                 goto error_free_vers;
4094               memcpy (newname, name, namelen);
4095               p = newname + namelen;
4096               *p++ = ELF_VER_CHR;
4097               /* If this is a defined non-hidden version symbol,
4098                  we add another @ to the name.  This indicates the
4099                  default version of the symbol.  */
4100               if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4101                   && isym->st_shndx != SHN_UNDEF)
4102                 *p++ = ELF_VER_CHR;
4103               memcpy (p, verstr, verlen + 1);
4104
4105               name = newname;
4106             }
4107
4108           /* If necessary, make a second attempt to locate the bfd
4109              containing an unresolved, non-weak reference to the
4110              current symbol.  */
4111           if (! bfd_is_und_section (sec) && undef_bfd == NULL)
4112             {
4113               h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4114
4115               if (h != NULL
4116                   && h->root.type == bfd_link_hash_undefined
4117                   && h->root.u.undef.abfd)
4118                 undef_bfd = h->root.u.undef.abfd;
4119             }
4120
4121           if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4122                                       &value, &old_alignment,
4123                                       sym_hash, &skip, &override,
4124                                       &type_change_ok, &size_change_ok))
4125             goto error_free_vers;
4126
4127           if (skip)
4128             continue;
4129
4130           if (override)
4131             definition = FALSE;
4132
4133           h = *sym_hash;
4134           while (h->root.type == bfd_link_hash_indirect
4135                  || h->root.type == bfd_link_hash_warning)
4136             h = (struct elf_link_hash_entry *) h->root.u.i.link;
4137
4138           /* Remember the old alignment if this is a common symbol, so
4139              that we don't reduce the alignment later on.  We can't
4140              check later, because _bfd_generic_link_add_one_symbol
4141              will set a default for the alignment which we want to
4142              override. We also remember the old bfd where the existing
4143              definition comes from.  */
4144           switch (h->root.type)
4145             {
4146             default:
4147               break;
4148
4149             case bfd_link_hash_defined:
4150             case bfd_link_hash_defweak:
4151               old_bfd = h->root.u.def.section->owner;
4152               break;
4153
4154             case bfd_link_hash_common:
4155               old_bfd = h->root.u.c.p->section->owner;
4156               old_alignment = h->root.u.c.p->alignment_power;
4157               break;
4158             }
4159
4160           if (elf_tdata (abfd)->verdef != NULL
4161               && ! override
4162               && vernum > 1
4163               && definition)
4164             h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4165         }
4166
4167       if (! (_bfd_generic_link_add_one_symbol
4168              (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4169               (struct bfd_link_hash_entry **) sym_hash)))
4170         goto error_free_vers;
4171
4172       h = *sym_hash;
4173       while (h->root.type == bfd_link_hash_indirect
4174              || h->root.type == bfd_link_hash_warning)
4175         h = (struct elf_link_hash_entry *) h->root.u.i.link;
4176
4177       *sym_hash = h;
4178       if (is_elf_hash_table (htab))
4179         h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4180
4181       new_weakdef = FALSE;
4182       if (dynamic
4183           && definition
4184           && (flags & BSF_WEAK) != 0
4185           && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4186           && is_elf_hash_table (htab)
4187           && h->u.weakdef == NULL)
4188         {
4189           /* Keep a list of all weak defined non function symbols from
4190              a dynamic object, using the weakdef field.  Later in this
4191              function we will set the weakdef field to the correct
4192              value.  We only put non-function symbols from dynamic
4193              objects on this list, because that happens to be the only
4194              time we need to know the normal symbol corresponding to a
4195              weak symbol, and the information is time consuming to
4196              figure out.  If the weakdef field is not already NULL,
4197              then this symbol was already defined by some previous
4198              dynamic object, and we will be using that previous
4199              definition anyhow.  */
4200
4201           h->u.weakdef = weaks;
4202           weaks = h;
4203           new_weakdef = TRUE;
4204         }
4205
4206       /* Set the alignment of a common symbol.  */
4207       if ((common || bfd_is_com_section (sec))
4208           && h->root.type == bfd_link_hash_common)
4209         {
4210           unsigned int align;
4211
4212           if (common)
4213             align = bfd_log2 (isym->st_value);
4214           else
4215             {
4216               /* The new symbol is a common symbol in a shared object.
4217                  We need to get the alignment from the section.  */
4218               align = new_sec->alignment_power;
4219             }
4220           if (align > old_alignment)
4221             h->root.u.c.p->alignment_power = align;
4222           else
4223             h->root.u.c.p->alignment_power = old_alignment;
4224         }
4225
4226       if (is_elf_hash_table (htab))
4227         {
4228           bfd_boolean dynsym;
4229
4230           /* Check the alignment when a common symbol is involved. This
4231              can change when a common symbol is overridden by a normal
4232              definition or a common symbol is ignored due to the old
4233              normal definition. We need to make sure the maximum
4234              alignment is maintained.  */
4235           if ((old_alignment || common)
4236               && h->root.type != bfd_link_hash_common)
4237             {
4238               unsigned int common_align;
4239               unsigned int normal_align;
4240               unsigned int symbol_align;
4241               bfd *normal_bfd;
4242               bfd *common_bfd;
4243
4244               symbol_align = ffs (h->root.u.def.value) - 1;
4245               if (h->root.u.def.section->owner != NULL
4246                   && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4247                 {
4248                   normal_align = h->root.u.def.section->alignment_power;
4249                   if (normal_align > symbol_align)
4250                     normal_align = symbol_align;
4251                 }
4252               else
4253                 normal_align = symbol_align;
4254
4255               if (old_alignment)
4256                 {
4257                   common_align = old_alignment;
4258                   common_bfd = old_bfd;
4259                   normal_bfd = abfd;
4260                 }
4261               else
4262                 {
4263                   common_align = bfd_log2 (isym->st_value);
4264                   common_bfd = abfd;
4265                   normal_bfd = old_bfd;
4266                 }
4267
4268               if (normal_align < common_align)
4269                 {
4270                   /* PR binutils/2735 */
4271                   if (normal_bfd == NULL)
4272                     (*_bfd_error_handler)
4273                       (_("Warning: alignment %u of common symbol `%s' in %B"
4274                          " is greater than the alignment (%u) of its section %A"),
4275                        common_bfd, h->root.u.def.section,
4276                        1 << common_align, name, 1 << normal_align);
4277                   else
4278                     (*_bfd_error_handler)
4279                       (_("Warning: alignment %u of symbol `%s' in %B"
4280                          " is smaller than %u in %B"),
4281                        normal_bfd, common_bfd,
4282                        1 << normal_align, name, 1 << common_align);
4283                 }
4284             }
4285
4286           /* Remember the symbol size if it isn't undefined.  */
4287           if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4288               && (definition || h->size == 0))
4289             {
4290               if (h->size != 0
4291                   && h->size != isym->st_size
4292                   && ! size_change_ok)
4293                 (*_bfd_error_handler)
4294                   (_("Warning: size of symbol `%s' changed"
4295                      " from %lu in %B to %lu in %B"),
4296                    old_bfd, abfd,
4297                    name, (unsigned long) h->size,
4298                    (unsigned long) isym->st_size);
4299
4300               h->size = isym->st_size;
4301             }
4302
4303           /* If this is a common symbol, then we always want H->SIZE
4304              to be the size of the common symbol.  The code just above
4305              won't fix the size if a common symbol becomes larger.  We
4306              don't warn about a size change here, because that is
4307              covered by --warn-common.  Allow changed between different
4308              function types.  */
4309           if (h->root.type == bfd_link_hash_common)
4310             h->size = h->root.u.c.size;
4311
4312           if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4313               && (definition || h->type == STT_NOTYPE))
4314             {
4315               unsigned int type = ELF_ST_TYPE (isym->st_info);
4316
4317               /* Turn an IFUNC symbol from a DSO into a normal FUNC
4318                  symbol.  */
4319               if (type == STT_GNU_IFUNC
4320                   && (abfd->flags & DYNAMIC) != 0)
4321                 type = STT_FUNC;
4322
4323               if (h->type != type)
4324                 {
4325                   if (h->type != STT_NOTYPE && ! type_change_ok)
4326                     (*_bfd_error_handler)
4327                       (_("Warning: type of symbol `%s' changed"
4328                          " from %d to %d in %B"),
4329                        abfd, name, h->type, type);
4330
4331                   h->type = type;
4332                 }
4333             }
4334
4335           /* Merge st_other field.  */
4336           elf_merge_st_other (abfd, h, isym, definition, dynamic);
4337
4338           /* Set a flag in the hash table entry indicating the type of
4339              reference or definition we just found.  Keep a count of
4340              the number of dynamic symbols we find.  A dynamic symbol
4341              is one which is referenced or defined by both a regular
4342              object and a shared object.  */
4343           dynsym = FALSE;
4344           if (! dynamic)
4345             {
4346               if (! definition)
4347                 {
4348                   h->ref_regular = 1;
4349                   if (bind != STB_WEAK)
4350                     h->ref_regular_nonweak = 1;
4351                 }
4352               else
4353                 {
4354                   h->def_regular = 1;
4355                   if (h->def_dynamic)
4356                     {
4357                       h->def_dynamic = 0;
4358                       h->ref_dynamic = 1;
4359                       h->dynamic_def = 1;
4360                     }
4361                 }
4362               if (! info->executable
4363                   || h->def_dynamic
4364                   || h->ref_dynamic)
4365                 dynsym = TRUE;
4366             }
4367           else
4368             {
4369               if (! definition)
4370                 h->ref_dynamic = 1;
4371               else
4372                 h->def_dynamic = 1;
4373               if (h->def_regular
4374                   || h->ref_regular
4375                   || (h->u.weakdef != NULL
4376                       && ! new_weakdef
4377                       && h->u.weakdef->dynindx != -1))
4378                 dynsym = TRUE;
4379             }
4380
4381           if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4382             {
4383               /* We don't want to make debug symbol dynamic.  */
4384               dynsym = FALSE;
4385             }
4386
4387           if (definition)
4388             h->target_internal = isym->st_target_internal;
4389
4390           /* Check to see if we need to add an indirect symbol for
4391              the default name.  */
4392           if (definition || h->root.type == bfd_link_hash_common)
4393             if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4394                                               &sec, &value, &dynsym,
4395                                               override))
4396               goto error_free_vers;
4397
4398           if (definition && !dynamic)
4399             {
4400               char *p = strchr (name, ELF_VER_CHR);
4401               if (p != NULL && p[1] != ELF_VER_CHR)
4402                 {
4403                   /* Queue non-default versions so that .symver x, x@FOO
4404                      aliases can be checked.  */
4405                   if (!nondeflt_vers)
4406                     {
4407                       amt = ((isymend - isym + 1)
4408                              * sizeof (struct elf_link_hash_entry *));
4409                       nondeflt_vers =
4410                           (struct elf_link_hash_entry **) bfd_malloc (amt);
4411                       if (!nondeflt_vers)
4412                         goto error_free_vers;
4413                     }
4414                   nondeflt_vers[nondeflt_vers_cnt++] = h;
4415                 }
4416             }
4417
4418           if (dynsym && h->dynindx == -1)
4419             {
4420               if (! bfd_elf_link_record_dynamic_symbol (info, h))
4421                 goto error_free_vers;
4422               if (h->u.weakdef != NULL
4423                   && ! new_weakdef
4424                   && h->u.weakdef->dynindx == -1)
4425                 {
4426                   if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4427                     goto error_free_vers;
4428                 }
4429             }
4430           else if (dynsym && h->dynindx != -1)
4431             /* If the symbol already has a dynamic index, but
4432                visibility says it should not be visible, turn it into
4433                a local symbol.  */
4434             switch (ELF_ST_VISIBILITY (h->other))
4435               {
4436               case STV_INTERNAL:
4437               case STV_HIDDEN:
4438                 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4439                 dynsym = FALSE;
4440                 break;
4441               }
4442
4443           if (!add_needed
4444               && definition
4445               && ((dynsym
4446                    && h->ref_regular)
4447                   || (h->ref_dynamic
4448                       && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4449                       && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4450             {
4451               int ret;
4452               const char *soname = elf_dt_name (abfd);
4453
4454               /* A symbol from a library loaded via DT_NEEDED of some
4455                  other library is referenced by a regular object.
4456                  Add a DT_NEEDED entry for it.  Issue an error if
4457                  --no-add-needed is used and the reference was not
4458                  a weak one.  */
4459               if (undef_bfd != NULL
4460                   && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4461                 {
4462                   (*_bfd_error_handler)
4463                     (_("%B: undefined reference to symbol '%s'"),
4464                      undef_bfd, name);
4465                   (*_bfd_error_handler)
4466                     (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"),
4467                      abfd, name);
4468                   bfd_set_error (bfd_error_invalid_operation);
4469                   goto error_free_vers;
4470                 }
4471
4472               elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4473                   (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4474
4475               add_needed = TRUE;
4476               ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4477               if (ret < 0)
4478                 goto error_free_vers;
4479
4480               BFD_ASSERT (ret == 0);
4481             }
4482         }
4483     }
4484
4485   if (extversym != NULL)
4486     {
4487       free (extversym);
4488       extversym = NULL;
4489     }
4490
4491   if (isymbuf != NULL)
4492     {
4493       free (isymbuf);
4494       isymbuf = NULL;
4495     }
4496
4497   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4498     {
4499       unsigned int i;
4500
4501       /* Restore the symbol table.  */
4502       if (bed->as_needed_cleanup)
4503         (*bed->as_needed_cleanup) (abfd, info);
4504       old_hash = (char *) old_tab + tabsize;
4505       old_ent = (char *) old_hash + hashsize;
4506       sym_hash = elf_sym_hashes (abfd);
4507       htab->root.table.table = old_table;
4508       htab->root.table.size = old_size;
4509       htab->root.table.count = old_count;
4510       memcpy (htab->root.table.table, old_tab, tabsize);
4511       memcpy (sym_hash, old_hash, hashsize);
4512       htab->root.undefs = old_undefs;
4513       htab->root.undefs_tail = old_undefs_tail;
4514       for (i = 0; i < htab->root.table.size; i++)
4515         {
4516           struct bfd_hash_entry *p;
4517           struct elf_link_hash_entry *h;
4518
4519           for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4520             {
4521               h = (struct elf_link_hash_entry *) p;
4522               if (h->root.type == bfd_link_hash_warning)
4523                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4524               if (h->dynindx >= old_dynsymcount)
4525                 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4526
4527               memcpy (p, old_ent, htab->root.table.entsize);
4528               old_ent = (char *) old_ent + htab->root.table.entsize;
4529               h = (struct elf_link_hash_entry *) p;
4530               if (h->root.type == bfd_link_hash_warning)
4531                 {
4532                   memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4533                   old_ent = (char *) old_ent + htab->root.table.entsize;
4534                 }
4535             }
4536         }
4537
4538       /* Make a special call to the linker "notice" function to
4539          tell it that symbols added for crefs may need to be removed.  */
4540       if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4541                                        notice_not_needed, 0, NULL))
4542         goto error_free_vers;
4543
4544       free (old_tab);
4545       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4546                            alloc_mark);
4547       if (nondeflt_vers != NULL)
4548         free (nondeflt_vers);
4549       return TRUE;
4550     }
4551
4552   if (old_tab != NULL)
4553     {
4554       if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4555                                        notice_needed, 0, NULL))
4556         goto error_free_vers;
4557       free (old_tab);
4558       old_tab = NULL;
4559     }
4560
4561   /* Now that all the symbols from this input file are created, handle
4562      .symver foo, foo@BAR such that any relocs against foo become foo@BAR.  */
4563   if (nondeflt_vers != NULL)
4564     {
4565       bfd_size_type cnt, symidx;
4566
4567       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4568         {
4569           struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4570           char *shortname, *p;
4571
4572           p = strchr (h->root.root.string, ELF_VER_CHR);
4573           if (p == NULL
4574               || (h->root.type != bfd_link_hash_defined
4575                   && h->root.type != bfd_link_hash_defweak))
4576             continue;
4577
4578           amt = p - h->root.root.string;
4579           shortname = (char *) bfd_malloc (amt + 1);
4580           if (!shortname)
4581             goto error_free_vers;
4582           memcpy (shortname, h->root.root.string, amt);
4583           shortname[amt] = '\0';
4584
4585           hi = (struct elf_link_hash_entry *)
4586                bfd_link_hash_lookup (&htab->root, shortname,
4587                                      FALSE, FALSE, FALSE);
4588           if (hi != NULL
4589               && hi->root.type == h->root.type
4590               && hi->root.u.def.value == h->root.u.def.value
4591               && hi->root.u.def.section == h->root.u.def.section)
4592             {
4593               (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4594               hi->root.type = bfd_link_hash_indirect;
4595               hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4596               (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4597               sym_hash = elf_sym_hashes (abfd);
4598               if (sym_hash)
4599                 for (symidx = 0; symidx < extsymcount; ++symidx)
4600                   if (sym_hash[symidx] == hi)
4601                     {
4602                       sym_hash[symidx] = h;
4603                       break;
4604                     }
4605             }
4606           free (shortname);
4607         }
4608       free (nondeflt_vers);
4609       nondeflt_vers = NULL;
4610     }
4611
4612   /* Now set the weakdefs field correctly for all the weak defined
4613      symbols we found.  The only way to do this is to search all the
4614      symbols.  Since we only need the information for non functions in
4615      dynamic objects, that's the only time we actually put anything on
4616      the list WEAKS.  We need this information so that if a regular
4617      object refers to a symbol defined weakly in a dynamic object, the
4618      real symbol in the dynamic object is also put in the dynamic
4619      symbols; we also must arrange for both symbols to point to the
4620      same memory location.  We could handle the general case of symbol
4621      aliasing, but a general symbol alias can only be generated in
4622      assembler code, handling it correctly would be very time
4623      consuming, and other ELF linkers don't handle general aliasing
4624      either.  */
4625   if (weaks != NULL)
4626     {
4627       struct elf_link_hash_entry **hpp;
4628       struct elf_link_hash_entry **hppend;
4629       struct elf_link_hash_entry **sorted_sym_hash;
4630       struct elf_link_hash_entry *h;
4631       size_t sym_count;
4632
4633       /* Since we have to search the whole symbol list for each weak
4634          defined symbol, search time for N weak defined symbols will be
4635          O(N^2). Binary search will cut it down to O(NlogN).  */
4636       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4637       sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4638       if (sorted_sym_hash == NULL)
4639         goto error_return;
4640       sym_hash = sorted_sym_hash;
4641       hpp = elf_sym_hashes (abfd);
4642       hppend = hpp + extsymcount;
4643       sym_count = 0;
4644       for (; hpp < hppend; hpp++)
4645         {
4646           h = *hpp;
4647           if (h != NULL
4648               && h->root.type == bfd_link_hash_defined
4649               && !bed->is_function_type (h->type))
4650             {
4651               *sym_hash = h;
4652               sym_hash++;
4653               sym_count++;
4654             }
4655         }
4656
4657       qsort (sorted_sym_hash, sym_count,
4658              sizeof (struct elf_link_hash_entry *),
4659              elf_sort_symbol);
4660
4661       while (weaks != NULL)
4662         {
4663           struct elf_link_hash_entry *hlook;
4664           asection *slook;
4665           bfd_vma vlook;
4666           long ilook;
4667           size_t i, j, idx;
4668
4669           hlook = weaks;
4670           weaks = hlook->u.weakdef;
4671           hlook->u.weakdef = NULL;
4672
4673           BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4674                       || hlook->root.type == bfd_link_hash_defweak
4675                       || hlook->root.type == bfd_link_hash_common
4676                       || hlook->root.type == bfd_link_hash_indirect);
4677           slook = hlook->root.u.def.section;
4678           vlook = hlook->root.u.def.value;
4679
4680           ilook = -1;
4681           i = 0;
4682           j = sym_count;
4683           while (i < j)
4684             {
4685               bfd_signed_vma vdiff;
4686               idx = (i + j) / 2;
4687               h = sorted_sym_hash [idx];
4688               vdiff = vlook - h->root.u.def.value;
4689               if (vdiff < 0)
4690                 j = idx;
4691               else if (vdiff > 0)
4692                 i = idx + 1;
4693               else
4694                 {
4695                   long sdiff = slook->id - h->root.u.def.section->id;
4696                   if (sdiff < 0)
4697                     j = idx;
4698                   else if (sdiff > 0)
4699                     i = idx + 1;
4700                   else
4701                     {
4702                       ilook = idx;
4703                       break;
4704                     }
4705                 }
4706             }
4707
4708           /* We didn't find a value/section match.  */
4709           if (ilook == -1)
4710             continue;
4711
4712           for (i = ilook; i < sym_count; i++)
4713             {
4714               h = sorted_sym_hash [i];
4715
4716               /* Stop if value or section doesn't match.  */
4717               if (h->root.u.def.value != vlook
4718                   || h->root.u.def.section != slook)
4719                 break;
4720               else if (h != hlook)
4721                 {
4722                   hlook->u.weakdef = h;
4723
4724                   /* If the weak definition is in the list of dynamic
4725                      symbols, make sure the real definition is put
4726                      there as well.  */
4727                   if (hlook->dynindx != -1 && h->dynindx == -1)
4728                     {
4729                       if (! bfd_elf_link_record_dynamic_symbol (info, h))
4730                         {
4731                         err_free_sym_hash:
4732                           free (sorted_sym_hash);
4733                           goto error_return;
4734                         }
4735                     }
4736
4737                   /* If the real definition is in the list of dynamic
4738                      symbols, make sure the weak definition is put
4739                      there as well.  If we don't do this, then the
4740                      dynamic loader might not merge the entries for the
4741                      real definition and the weak definition.  */
4742                   if (h->dynindx != -1 && hlook->dynindx == -1)
4743                     {
4744                       if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4745                         goto err_free_sym_hash;
4746                     }
4747                   break;
4748                 }
4749             }
4750         }
4751
4752       free (sorted_sym_hash);
4753     }
4754
4755   if (bed->check_directives
4756       && !(*bed->check_directives) (abfd, info))
4757     return FALSE;
4758
4759   /* If this object is the same format as the output object, and it is
4760      not a shared library, then let the backend look through the
4761      relocs.
4762
4763      This is required to build global offset table entries and to
4764      arrange for dynamic relocs.  It is not required for the
4765      particular common case of linking non PIC code, even when linking
4766      against shared libraries, but unfortunately there is no way of
4767      knowing whether an object file has been compiled PIC or not.
4768      Looking through the relocs is not particularly time consuming.
4769      The problem is that we must either (1) keep the relocs in memory,
4770      which causes the linker to require additional runtime memory or
4771      (2) read the relocs twice from the input file, which wastes time.
4772      This would be a good case for using mmap.
4773
4774      I have no idea how to handle linking PIC code into a file of a
4775      different format.  It probably can't be done.  */
4776   if (! dynamic
4777       && is_elf_hash_table (htab)
4778       && bed->check_relocs != NULL
4779       && elf_object_id (abfd) == elf_hash_table_id (htab)
4780       && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4781     {
4782       asection *o;
4783
4784       for (o = abfd->sections; o != NULL; o = o->next)
4785         {
4786           Elf_Internal_Rela *internal_relocs;
4787           bfd_boolean ok;
4788
4789           if ((o->flags & SEC_RELOC) == 0
4790               || o->reloc_count == 0
4791               || ((info->strip == strip_all || info->strip == strip_debugger)
4792                   && (o->flags & SEC_DEBUGGING) != 0)
4793               || bfd_is_abs_section (o->output_section))
4794             continue;
4795
4796           internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4797                                                        info->keep_memory);
4798           if (internal_relocs == NULL)
4799             goto error_return;
4800
4801           ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4802
4803           if (elf_section_data (o)->relocs != internal_relocs)
4804             free (internal_relocs);
4805
4806           if (! ok)
4807             goto error_return;
4808         }
4809     }
4810
4811   /* If this is a non-traditional link, try to optimize the handling
4812      of the .stab/.stabstr sections.  */
4813   if (! dynamic
4814       && ! info->traditional_format
4815       && is_elf_hash_table (htab)
4816       && (info->strip != strip_all && info->strip != strip_debugger))
4817     {
4818       asection *stabstr;
4819
4820       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4821       if (stabstr != NULL)
4822         {
4823           bfd_size_type string_offset = 0;
4824           asection *stab;
4825
4826           for (stab = abfd->sections; stab; stab = stab->next)
4827             if (CONST_STRNEQ (stab->name, ".stab")
4828                 && (!stab->name[5] ||
4829                     (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4830                 && (stab->flags & SEC_MERGE) == 0
4831                 && !bfd_is_abs_section (stab->output_section))
4832               {
4833                 struct bfd_elf_section_data *secdata;
4834
4835                 secdata = elf_section_data (stab);
4836                 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4837                                                stabstr, &secdata->sec_info,
4838                                                &string_offset))
4839                   goto error_return;
4840                 if (secdata->sec_info)
4841                   stab->sec_info_type = ELF_INFO_TYPE_STABS;
4842             }
4843         }
4844     }
4845
4846   if (is_elf_hash_table (htab) && add_needed)
4847     {
4848       /* Add this bfd to the loaded list.  */
4849       struct elf_link_loaded_list *n;
4850
4851       n = (struct elf_link_loaded_list *)
4852           bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4853       if (n == NULL)
4854         goto error_return;
4855       n->abfd = abfd;
4856       n->next = htab->loaded;
4857       htab->loaded = n;
4858     }
4859
4860   return TRUE;
4861
4862  error_free_vers:
4863   if (old_tab != NULL)
4864     free (old_tab);
4865   if (nondeflt_vers != NULL)
4866     free (nondeflt_vers);
4867   if (extversym != NULL)
4868     free (extversym);
4869  error_free_sym:
4870   if (isymbuf != NULL)
4871     free (isymbuf);
4872  error_return:
4873   return FALSE;
4874 }
4875
4876 /* Return the linker hash table entry of a symbol that might be
4877    satisfied by an archive symbol.  Return -1 on error.  */
4878
4879 struct elf_link_hash_entry *
4880 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4881                                 struct bfd_link_info *info,
4882                                 const char *name)
4883 {
4884   struct elf_link_hash_entry *h;
4885   char *p, *copy;
4886   size_t len, first;
4887
4888   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
4889   if (h != NULL)
4890     return h;
4891
4892   /* If this is a default version (the name contains @@), look up the
4893      symbol again with only one `@' as well as without the version.
4894      The effect is that references to the symbol with and without the
4895      version will be matched by the default symbol in the archive.  */
4896
4897   p = strchr (name, ELF_VER_CHR);
4898   if (p == NULL || p[1] != ELF_VER_CHR)
4899     return h;
4900
4901   /* First check with only one `@'.  */
4902   len = strlen (name);
4903   copy = (char *) bfd_alloc (abfd, len);
4904   if (copy == NULL)
4905     return (struct elf_link_hash_entry *) 0 - 1;
4906
4907   first = p - name + 1;
4908   memcpy (copy, name, first);
4909   memcpy (copy + first, name + first + 1, len - first);
4910
4911   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
4912   if (h == NULL)
4913     {
4914       /* We also need to check references to the symbol without the
4915          version.  */
4916       copy[first - 1] = '\0';
4917       h = elf_link_hash_lookup (elf_hash_table (info), copy,
4918                                 FALSE, FALSE, TRUE);
4919     }
4920
4921   bfd_release (abfd, copy);
4922   return h;
4923 }
4924
4925 /* Add symbols from an ELF archive file to the linker hash table.  We
4926    don't use _bfd_generic_link_add_archive_symbols because of a
4927    problem which arises on UnixWare.  The UnixWare libc.so is an
4928    archive which includes an entry libc.so.1 which defines a bunch of
4929    symbols.  The libc.so archive also includes a number of other
4930    object files, which also define symbols, some of which are the same
4931    as those defined in libc.so.1.  Correct linking requires that we
4932    consider each object file in turn, and include it if it defines any
4933    symbols we need.  _bfd_generic_link_add_archive_symbols does not do
4934    this; it looks through the list of undefined symbols, and includes
4935    any object file which defines them.  When this algorithm is used on
4936    UnixWare, it winds up pulling in libc.so.1 early and defining a
4937    bunch of symbols.  This means that some of the other objects in the
4938    archive are not included in the link, which is incorrect since they
4939    precede libc.so.1 in the archive.
4940
4941    Fortunately, ELF archive handling is simpler than that done by
4942    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4943    oddities.  In ELF, if we find a symbol in the archive map, and the
4944    symbol is currently undefined, we know that we must pull in that
4945    object file.
4946
4947    Unfortunately, we do have to make multiple passes over the symbol
4948    table until nothing further is resolved.  */
4949
4950 static bfd_boolean
4951 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4952 {
4953   symindex c;
4954   bfd_boolean *defined = NULL;
4955   bfd_boolean *included = NULL;
4956   carsym *symdefs;
4957   bfd_boolean loop;
4958   bfd_size_type amt;
4959   const struct elf_backend_data *bed;
4960   struct elf_link_hash_entry * (*archive_symbol_lookup)
4961     (bfd *, struct bfd_link_info *, const char *);
4962
4963   if (! bfd_has_map (abfd))
4964     {
4965       /* An empty archive is a special case.  */
4966       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4967         return TRUE;
4968       bfd_set_error (bfd_error_no_armap);
4969       return FALSE;
4970     }
4971
4972   /* Keep track of all symbols we know to be already defined, and all
4973      files we know to be already included.  This is to speed up the
4974      second and subsequent passes.  */
4975   c = bfd_ardata (abfd)->symdef_count;
4976   if (c == 0)
4977     return TRUE;
4978   amt = c;
4979   amt *= sizeof (bfd_boolean);
4980   defined = (bfd_boolean *) bfd_zmalloc (amt);
4981   included = (bfd_boolean *) bfd_zmalloc (amt);
4982   if (defined == NULL || included == NULL)
4983     goto error_return;
4984
4985   symdefs = bfd_ardata (abfd)->symdefs;
4986   bed = get_elf_backend_data (abfd);
4987   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4988
4989   do
4990     {
4991       file_ptr last;
4992       symindex i;
4993       carsym *symdef;
4994       carsym *symdefend;
4995
4996       loop = FALSE;
4997       last = -1;
4998
4999       symdef = symdefs;
5000       symdefend = symdef + c;
5001       for (i = 0; symdef < symdefend; symdef++, i++)
5002         {
5003           struct elf_link_hash_entry *h;
5004           bfd *element;
5005           struct bfd_link_hash_entry *undefs_tail;
5006           symindex mark;
5007
5008           if (defined[i] || included[i])
5009             continue;
5010           if (symdef->file_offset == last)
5011             {
5012               included[i] = TRUE;
5013               continue;
5014             }
5015
5016           h = archive_symbol_lookup (abfd, info, symdef->name);
5017           if (h == (struct elf_link_hash_entry *) 0 - 1)
5018             goto error_return;
5019
5020           if (h == NULL)
5021             continue;
5022
5023           if (h->root.type == bfd_link_hash_common)
5024             {
5025               /* We currently have a common symbol.  The archive map contains
5026                  a reference to this symbol, so we may want to include it.  We
5027                  only want to include it however, if this archive element
5028                  contains a definition of the symbol, not just another common
5029                  declaration of it.
5030
5031                  Unfortunately some archivers (including GNU ar) will put
5032                  declarations of common symbols into their archive maps, as
5033                  well as real definitions, so we cannot just go by the archive
5034                  map alone.  Instead we must read in the element's symbol
5035                  table and check that to see what kind of symbol definition
5036                  this is.  */
5037               if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5038                 continue;
5039             }
5040           else if (h->root.type != bfd_link_hash_undefined)
5041             {
5042               if (h->root.type != bfd_link_hash_undefweak)
5043                 defined[i] = TRUE;
5044               continue;
5045             }
5046
5047           /* We need to include this archive member.  */
5048           element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5049           if (element == NULL)
5050             goto error_return;
5051
5052           if (! bfd_check_format (element, bfd_object))
5053             goto error_return;
5054
5055           /* Doublecheck that we have not included this object
5056              already--it should be impossible, but there may be
5057              something wrong with the archive.  */
5058           if (element->archive_pass != 0)
5059             {
5060               bfd_set_error (bfd_error_bad_value);
5061               goto error_return;
5062             }
5063           element->archive_pass = 1;
5064
5065           undefs_tail = info->hash->undefs_tail;
5066
5067           if (!(*info->callbacks
5068                 ->add_archive_element) (info, element, symdef->name, &element))
5069             goto error_return;
5070           if (!bfd_link_add_symbols (element, info))
5071             goto error_return;
5072
5073           /* If there are any new undefined symbols, we need to make
5074              another pass through the archive in order to see whether
5075              they can be defined.  FIXME: This isn't perfect, because
5076              common symbols wind up on undefs_tail and because an
5077              undefined symbol which is defined later on in this pass
5078              does not require another pass.  This isn't a bug, but it
5079              does make the code less efficient than it could be.  */
5080           if (undefs_tail != info->hash->undefs_tail)
5081             loop = TRUE;
5082
5083           /* Look backward to mark all symbols from this object file
5084              which we have already seen in this pass.  */
5085           mark = i;
5086           do
5087             {
5088               included[mark] = TRUE;
5089               if (mark == 0)
5090                 break;
5091               --mark;
5092             }
5093           while (symdefs[mark].file_offset == symdef->file_offset);
5094
5095           /* We mark subsequent symbols from this object file as we go
5096              on through the loop.  */
5097           last = symdef->file_offset;
5098         }
5099     }
5100   while (loop);
5101
5102   free (defined);
5103   free (included);
5104
5105   return TRUE;
5106
5107  error_return:
5108   if (defined != NULL)
5109     free (defined);
5110   if (included != NULL)
5111     free (included);
5112   return FALSE;
5113 }
5114
5115 /* Given an ELF BFD, add symbols to the global hash table as
5116    appropriate.  */
5117
5118 bfd_boolean
5119 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5120 {
5121   switch (bfd_get_format (abfd))
5122     {
5123     case bfd_object:
5124       return elf_link_add_object_symbols (abfd, info);
5125     case bfd_archive:
5126       return elf_link_add_archive_symbols (abfd, info);
5127     default:
5128       bfd_set_error (bfd_error_wrong_format);
5129       return FALSE;
5130     }
5131 }
5132 \f
5133 struct hash_codes_info
5134 {
5135   unsigned long *hashcodes;
5136   bfd_boolean error;
5137 };
5138
5139 /* This function will be called though elf_link_hash_traverse to store
5140    all hash value of the exported symbols in an array.  */
5141
5142 static bfd_boolean
5143 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5144 {
5145   struct hash_codes_info *inf = (struct hash_codes_info *) data;
5146   const char *name;
5147   char *p;
5148   unsigned long ha;
5149   char *alc = NULL;
5150
5151   /* Ignore indirect symbols.  These are added by the versioning code.  */
5152   if (h->dynindx == -1)
5153     return TRUE;
5154
5155   name = h->root.root.string;
5156   p = strchr (name, ELF_VER_CHR);
5157   if (p != NULL)
5158     {
5159       alc = (char *) bfd_malloc (p - name + 1);
5160       if (alc == NULL)
5161         {
5162           inf->error = TRUE;
5163           return FALSE;
5164         }
5165       memcpy (alc, name, p - name);
5166       alc[p - name] = '\0';
5167       name = alc;
5168     }
5169
5170   /* Compute the hash value.  */
5171   ha = bfd_elf_hash (name);
5172
5173   /* Store the found hash value in the array given as the argument.  */
5174   *(inf->hashcodes)++ = ha;
5175
5176   /* And store it in the struct so that we can put it in the hash table
5177      later.  */
5178   h->u.elf_hash_value = ha;
5179
5180   if (alc != NULL)
5181     free (alc);
5182
5183   return TRUE;
5184 }
5185
5186 struct collect_gnu_hash_codes
5187 {
5188   bfd *output_bfd;
5189   const struct elf_backend_data *bed;
5190   unsigned long int nsyms;
5191   unsigned long int maskbits;
5192   unsigned long int *hashcodes;
5193   unsigned long int *hashval;
5194   unsigned long int *indx;
5195   unsigned long int *counts;
5196   bfd_vma *bitmask;
5197   bfd_byte *contents;
5198   long int min_dynindx;
5199   unsigned long int bucketcount;
5200   unsigned long int symindx;
5201   long int local_indx;
5202   long int shift1, shift2;
5203   unsigned long int mask;
5204   bfd_boolean error;
5205 };
5206
5207 /* This function will be called though elf_link_hash_traverse to store
5208    all hash value of the exported symbols in an array.  */
5209
5210 static bfd_boolean
5211 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5212 {
5213   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5214   const char *name;
5215   char *p;
5216   unsigned long ha;
5217   char *alc = NULL;
5218
5219   /* Ignore indirect symbols.  These are added by the versioning code.  */
5220   if (h->dynindx == -1)
5221     return TRUE;
5222
5223   /* Ignore also local symbols and undefined symbols.  */
5224   if (! (*s->bed->elf_hash_symbol) (h))
5225     return TRUE;
5226
5227   name = h->root.root.string;
5228   p = strchr (name, ELF_VER_CHR);
5229   if (p != NULL)
5230     {
5231       alc = (char *) bfd_malloc (p - name + 1);
5232       if (alc == NULL)
5233         {
5234           s->error = TRUE;
5235           return FALSE;
5236         }
5237       memcpy (alc, name, p - name);
5238       alc[p - name] = '\0';
5239       name = alc;
5240     }
5241
5242   /* Compute the hash value.  */
5243   ha = bfd_elf_gnu_hash (name);
5244
5245   /* Store the found hash value in the array for compute_bucket_count,
5246      and also for .dynsym reordering purposes.  */
5247   s->hashcodes[s->nsyms] = ha;
5248   s->hashval[h->dynindx] = ha;
5249   ++s->nsyms;
5250   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5251     s->min_dynindx = h->dynindx;
5252
5253   if (alc != NULL)
5254     free (alc);
5255
5256   return TRUE;
5257 }
5258
5259 /* This function will be called though elf_link_hash_traverse to do
5260    final dynaminc symbol renumbering.  */
5261
5262 static bfd_boolean
5263 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5264 {
5265   struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5266   unsigned long int bucket;
5267   unsigned long int val;
5268
5269   /* Ignore indirect symbols.  */
5270   if (h->dynindx == -1)
5271     return TRUE;
5272
5273   /* Ignore also local symbols and undefined symbols.  */
5274   if (! (*s->bed->elf_hash_symbol) (h))
5275     {
5276       if (h->dynindx >= s->min_dynindx)
5277         h->dynindx = s->local_indx++;
5278       return TRUE;
5279     }
5280
5281   bucket = s->hashval[h->dynindx] % s->bucketcount;
5282   val = (s->hashval[h->dynindx] >> s->shift1)
5283         & ((s->maskbits >> s->shift1) - 1);
5284   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5285   s->bitmask[val]
5286     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5287   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5288   if (s->counts[bucket] == 1)
5289     /* Last element terminates the chain.  */
5290     val |= 1;
5291   bfd_put_32 (s->output_bfd, val,
5292               s->contents + (s->indx[bucket] - s->symindx) * 4);
5293   --s->counts[bucket];
5294   h->dynindx = s->indx[bucket]++;
5295   return TRUE;
5296 }
5297
5298 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
5299
5300 bfd_boolean
5301 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5302 {
5303   return !(h->forced_local
5304            || h->root.type == bfd_link_hash_undefined
5305            || h->root.type == bfd_link_hash_undefweak
5306            || ((h->root.type == bfd_link_hash_defined
5307                 || h->root.type == bfd_link_hash_defweak)
5308                && h->root.u.def.section->output_section == NULL));
5309 }
5310
5311 /* Array used to determine the number of hash table buckets to use
5312    based on the number of symbols there are.  If there are fewer than
5313    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5314    fewer than 37 we use 17 buckets, and so forth.  We never use more
5315    than 32771 buckets.  */
5316
5317 static const size_t elf_buckets[] =
5318 {
5319   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5320   16411, 32771, 0
5321 };
5322
5323 /* Compute bucket count for hashing table.  We do not use a static set
5324    of possible tables sizes anymore.  Instead we determine for all
5325    possible reasonable sizes of the table the outcome (i.e., the
5326    number of collisions etc) and choose the best solution.  The
5327    weighting functions are not too simple to allow the table to grow
5328    without bounds.  Instead one of the weighting factors is the size.
5329    Therefore the result is always a good payoff between few collisions
5330    (= short chain lengths) and table size.  */
5331 static size_t
5332 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5333                       unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5334                       unsigned long int nsyms,
5335                       int gnu_hash)
5336 {
5337   size_t best_size = 0;
5338   unsigned long int i;
5339
5340   /* We have a problem here.  The following code to optimize the table
5341      size requires an integer type with more the 32 bits.  If
5342      BFD_HOST_U_64_BIT is set we know about such a type.  */
5343 #ifdef BFD_HOST_U_64_BIT
5344   if (info->optimize)
5345     {
5346       size_t minsize;
5347       size_t maxsize;
5348       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5349       bfd *dynobj = elf_hash_table (info)->dynobj;
5350       size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5351       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5352       unsigned long int *counts;
5353       bfd_size_type amt;
5354       unsigned int no_improvement_count = 0;
5355
5356       /* Possible optimization parameters: if we have NSYMS symbols we say
5357          that the hashing table must at least have NSYMS/4 and at most
5358          2*NSYMS buckets.  */
5359       minsize = nsyms / 4;
5360       if (minsize == 0)
5361         minsize = 1;
5362       best_size = maxsize = nsyms * 2;
5363       if (gnu_hash)
5364         {
5365           if (minsize < 2)
5366             minsize = 2;
5367           if ((best_size & 31) == 0)
5368             ++best_size;
5369         }
5370
5371       /* Create array where we count the collisions in.  We must use bfd_malloc
5372          since the size could be large.  */
5373       amt = maxsize;
5374       amt *= sizeof (unsigned long int);
5375       counts = (unsigned long int *) bfd_malloc (amt);
5376       if (counts == NULL)
5377         return 0;
5378
5379       /* Compute the "optimal" size for the hash table.  The criteria is a
5380          minimal chain length.  The minor criteria is (of course) the size
5381          of the table.  */
5382       for (i = minsize; i < maxsize; ++i)
5383         {
5384           /* Walk through the array of hashcodes and count the collisions.  */
5385           BFD_HOST_U_64_BIT max;
5386           unsigned long int j;
5387           unsigned long int fact;
5388
5389           if (gnu_hash && (i & 31) == 0)
5390             continue;
5391
5392           memset (counts, '\0', i * sizeof (unsigned long int));
5393
5394           /* Determine how often each hash bucket is used.  */
5395           for (j = 0; j < nsyms; ++j)
5396             ++counts[hashcodes[j] % i];
5397
5398           /* For the weight function we need some information about the
5399              pagesize on the target.  This is information need not be 100%
5400              accurate.  Since this information is not available (so far) we
5401              define it here to a reasonable default value.  If it is crucial
5402              to have a better value some day simply define this value.  */
5403 # ifndef BFD_TARGET_PAGESIZE
5404 #  define BFD_TARGET_PAGESIZE   (4096)
5405 # endif
5406
5407           /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5408              and the chains.  */
5409           max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5410
5411 # if 1
5412           /* Variant 1: optimize for short chains.  We add the squares
5413              of all the chain lengths (which favors many small chain
5414              over a few long chains).  */
5415           for (j = 0; j < i; ++j)
5416             max += counts[j] * counts[j];
5417
5418           /* This adds penalties for the overall size of the table.  */
5419           fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5420           max *= fact * fact;
5421 # else
5422           /* Variant 2: Optimize a lot more for small table.  Here we
5423              also add squares of the size but we also add penalties for
5424              empty slots (the +1 term).  */
5425           for (j = 0; j < i; ++j)
5426             max += (1 + counts[j]) * (1 + counts[j]);
5427
5428           /* The overall size of the table is considered, but not as
5429              strong as in variant 1, where it is squared.  */
5430           fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5431           max *= fact;
5432 # endif
5433
5434           /* Compare with current best results.  */
5435           if (max < best_chlen)
5436             {
5437               best_chlen = max;
5438               best_size = i;
5439               no_improvement_count = 0;
5440             }
5441           /* PR 11843: Avoid futile long searches for the best bucket size
5442              when there are a large number of symbols.  */
5443           else if (++no_improvement_count == 100)
5444             break;
5445         }
5446
5447       free (counts);
5448     }
5449   else
5450 #endif /* defined (BFD_HOST_U_64_BIT) */
5451     {
5452       /* This is the fallback solution if no 64bit type is available or if we
5453          are not supposed to spend much time on optimizations.  We select the
5454          bucket count using a fixed set of numbers.  */
5455       for (i = 0; elf_buckets[i] != 0; i++)
5456         {
5457           best_size = elf_buckets[i];
5458           if (nsyms < elf_buckets[i + 1])
5459             break;
5460         }
5461       if (gnu_hash && best_size < 2)
5462         best_size = 2;
5463     }
5464
5465   return best_size;
5466 }
5467
5468 /* Size any SHT_GROUP section for ld -r.  */
5469
5470 bfd_boolean
5471 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5472 {
5473   bfd *ibfd;
5474
5475   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5476     if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5477         && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5478       return FALSE;
5479   return TRUE;
5480 }
5481
5482 /* Set up the sizes and contents of the ELF dynamic sections.  This is
5483    called by the ELF linker emulation before_allocation routine.  We
5484    must set the sizes of the sections before the linker sets the
5485    addresses of the various sections.  */
5486
5487 bfd_boolean
5488 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5489                                const char *soname,
5490                                const char *rpath,
5491                                const char *filter_shlib,
5492                                const char *audit,
5493                                const char *depaudit,
5494                                const char * const *auxiliary_filters,
5495                                struct bfd_link_info *info,
5496                                asection **sinterpptr,
5497                                struct bfd_elf_version_tree *verdefs)
5498 {
5499   bfd_size_type soname_indx;
5500   bfd *dynobj;
5501   const struct elf_backend_data *bed;
5502   struct elf_info_failed asvinfo;
5503
5504   *sinterpptr = NULL;
5505
5506   soname_indx = (bfd_size_type) -1;
5507
5508   if (!is_elf_hash_table (info->hash))
5509     return TRUE;
5510
5511   bed = get_elf_backend_data (output_bfd);
5512   if (info->execstack)
5513     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5514   else if (info->noexecstack)
5515     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5516   else
5517     {
5518       bfd *inputobj;
5519       asection *notesec = NULL;
5520       int exec = 0;
5521
5522       for (inputobj = info->input_bfds;
5523            inputobj;
5524            inputobj = inputobj->link_next)
5525         {
5526           asection *s;
5527
5528           if (inputobj->flags & (DYNAMIC | EXEC_P | BFD_LINKER_CREATED))
5529             continue;
5530           s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5531           if (s)
5532             {
5533               if (s->flags & SEC_CODE)
5534                 exec = PF_X;
5535               notesec = s;
5536             }
5537           else if (bed->default_execstack)
5538             exec = PF_X;
5539         }
5540       if (notesec)
5541         {
5542           elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5543           if (exec && info->relocatable
5544               && notesec->output_section != bfd_abs_section_ptr)
5545             notesec->output_section->flags |= SEC_CODE;
5546         }
5547     }
5548
5549   /* Any syms created from now on start with -1 in
5550      got.refcount/offset and plt.refcount/offset.  */
5551   elf_hash_table (info)->init_got_refcount
5552     = elf_hash_table (info)->init_got_offset;
5553   elf_hash_table (info)->init_plt_refcount
5554     = elf_hash_table (info)->init_plt_offset;
5555
5556   if (info->relocatable
5557       && !_bfd_elf_size_group_sections (info))
5558     return FALSE;
5559
5560   /* The backend may have to create some sections regardless of whether
5561      we're dynamic or not.  */
5562   if (bed->elf_backend_always_size_sections
5563       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5564     return FALSE;
5565
5566   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5567     return FALSE;
5568
5569   dynobj = elf_hash_table (info)->dynobj;
5570
5571   /* If there were no dynamic objects in the link, there is nothing to
5572      do here.  */
5573   if (dynobj == NULL)
5574     return TRUE;
5575
5576   if (elf_hash_table (info)->dynamic_sections_created)
5577     {
5578       struct elf_info_failed eif;
5579       struct elf_link_hash_entry *h;
5580       asection *dynstr;
5581       struct bfd_elf_version_tree *t;
5582       struct bfd_elf_version_expr *d;
5583       asection *s;
5584       bfd_boolean all_defined;
5585
5586       *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5587       BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5588
5589       if (soname != NULL)
5590         {
5591           soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5592                                              soname, TRUE);
5593           if (soname_indx == (bfd_size_type) -1
5594               || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5595             return FALSE;
5596         }
5597
5598       if (info->symbolic)
5599         {
5600           if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5601             return FALSE;
5602           info->flags |= DF_SYMBOLIC;
5603         }
5604
5605       if (rpath != NULL)
5606         {
5607           bfd_size_type indx;
5608
5609           indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5610                                       TRUE);
5611           if (indx == (bfd_size_type) -1
5612               || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5613             return FALSE;
5614
5615           if  (info->new_dtags)
5616             {
5617               _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5618               if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5619                 return FALSE;
5620             }
5621         }
5622
5623       if (filter_shlib != NULL)
5624         {
5625           bfd_size_type indx;
5626
5627           indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5628                                       filter_shlib, TRUE);
5629           if (indx == (bfd_size_type) -1
5630               || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5631             return FALSE;
5632         }
5633
5634       if (auxiliary_filters != NULL)
5635         {
5636           const char * const *p;
5637
5638           for (p = auxiliary_filters; *p != NULL; p++)
5639             {
5640               bfd_size_type indx;
5641
5642               indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5643                                           *p, TRUE);
5644               if (indx == (bfd_size_type) -1
5645                   || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5646                 return FALSE;
5647             }
5648         }
5649
5650       if (audit != NULL)
5651         {
5652           bfd_size_type indx;
5653
5654           indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5655                                       TRUE);
5656           if (indx == (bfd_size_type) -1
5657               || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5658             return FALSE;
5659         }
5660
5661       if (depaudit != NULL)
5662         {
5663           bfd_size_type indx;
5664
5665           indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5666                                       TRUE);
5667           if (indx == (bfd_size_type) -1
5668               || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5669             return FALSE;
5670         }
5671
5672       eif.info = info;
5673       eif.verdefs = verdefs;
5674       eif.failed = FALSE;
5675
5676       /* If we are supposed to export all symbols into the dynamic symbol
5677          table (this is not the normal case), then do so.  */
5678       if (info->export_dynamic
5679           || (info->executable && info->dynamic))
5680         {
5681           elf_link_hash_traverse (elf_hash_table (info),
5682                                   _bfd_elf_export_symbol,
5683                                   &eif);
5684           if (eif.failed)
5685             return FALSE;
5686         }
5687
5688       /* Make all global versions with definition.  */
5689       for (t = verdefs; t != NULL; t = t->next)
5690         for (d = t->globals.list; d != NULL; d = d->next)
5691           if (!d->symver && d->literal)
5692             {
5693               const char *verstr, *name;
5694               size_t namelen, verlen, newlen;
5695               char *newname, *p, leading_char;
5696               struct elf_link_hash_entry *newh;
5697
5698               leading_char = bfd_get_symbol_leading_char (output_bfd);
5699               name = d->pattern;
5700               namelen = strlen (name) + (leading_char != '\0');
5701               verstr = t->name;
5702               verlen = strlen (verstr);
5703               newlen = namelen + verlen + 3;
5704
5705               newname = (char *) bfd_malloc (newlen);
5706               if (newname == NULL)
5707                 return FALSE;
5708               newname[0] = leading_char;
5709               memcpy (newname + (leading_char != '\0'), name, namelen);
5710
5711               /* Check the hidden versioned definition.  */
5712               p = newname + namelen;
5713               *p++ = ELF_VER_CHR;
5714               memcpy (p, verstr, verlen + 1);
5715               newh = elf_link_hash_lookup (elf_hash_table (info),
5716                                            newname, FALSE, FALSE,
5717                                            FALSE);
5718               if (newh == NULL
5719                   || (newh->root.type != bfd_link_hash_defined
5720                       && newh->root.type != bfd_link_hash_defweak))
5721                 {
5722                   /* Check the default versioned definition.  */
5723                   *p++ = ELF_VER_CHR;
5724                   memcpy (p, verstr, verlen + 1);
5725                   newh = elf_link_hash_lookup (elf_hash_table (info),
5726                                                newname, FALSE, FALSE,
5727                                                FALSE);
5728                 }
5729               free (newname);
5730
5731               /* Mark this version if there is a definition and it is
5732                  not defined in a shared object.  */
5733               if (newh != NULL
5734                   && !newh->def_dynamic
5735                   && (newh->root.type == bfd_link_hash_defined
5736                       || newh->root.type == bfd_link_hash_defweak))
5737                 d->symver = 1;
5738             }
5739
5740       /* Attach all the symbols to their version information.  */
5741       asvinfo.info = info;
5742       asvinfo.verdefs = verdefs;
5743       asvinfo.failed = FALSE;
5744
5745       elf_link_hash_traverse (elf_hash_table (info),
5746                               _bfd_elf_link_assign_sym_version,
5747                               &asvinfo);
5748       if (asvinfo.failed)
5749         return FALSE;
5750
5751       if (!info->allow_undefined_version)
5752         {
5753           /* Check if all global versions have a definition.  */
5754           all_defined = TRUE;
5755           for (t = verdefs; t != NULL; t = t->next)
5756             for (d = t->globals.list; d != NULL; d = d->next)
5757               if (d->literal && !d->symver && !d->script)
5758                 {
5759                   (*_bfd_error_handler)
5760                     (_("%s: undefined version: %s"),
5761                      d->pattern, t->name);
5762                   all_defined = FALSE;
5763                 }
5764
5765           if (!all_defined)
5766             {
5767               bfd_set_error (bfd_error_bad_value);
5768               return FALSE;
5769             }
5770         }
5771
5772       /* Find all symbols which were defined in a dynamic object and make
5773          the backend pick a reasonable value for them.  */
5774       elf_link_hash_traverse (elf_hash_table (info),
5775                               _bfd_elf_adjust_dynamic_symbol,
5776                               &eif);
5777       if (eif.failed)
5778         return FALSE;
5779
5780       /* Add some entries to the .dynamic section.  We fill in some of the
5781          values later, in bfd_elf_final_link, but we must add the entries
5782          now so that we know the final size of the .dynamic section.  */
5783
5784       /* If there are initialization and/or finalization functions to
5785          call then add the corresponding DT_INIT/DT_FINI entries.  */
5786       h = (info->init_function
5787            ? elf_link_hash_lookup (elf_hash_table (info),
5788                                    info->init_function, FALSE,
5789                                    FALSE, FALSE)
5790            : NULL);
5791       if (h != NULL
5792           && (h->ref_regular
5793               || h->def_regular))
5794         {
5795           if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5796             return FALSE;
5797         }
5798       h = (info->fini_function
5799            ? elf_link_hash_lookup (elf_hash_table (info),
5800                                    info->fini_function, FALSE,
5801                                    FALSE, FALSE)
5802            : NULL);
5803       if (h != NULL
5804           && (h->ref_regular
5805               || h->def_regular))
5806         {
5807           if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5808             return FALSE;
5809         }
5810
5811       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5812       if (s != NULL && s->linker_has_input)
5813         {
5814           /* DT_PREINIT_ARRAY is not allowed in shared library.  */
5815           if (! info->executable)
5816             {
5817               bfd *sub;
5818               asection *o;
5819
5820               for (sub = info->input_bfds; sub != NULL;
5821                    sub = sub->link_next)
5822                 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5823                   for (o = sub->sections; o != NULL; o = o->next)
5824                     if (elf_section_data (o)->this_hdr.sh_type
5825                         == SHT_PREINIT_ARRAY)
5826                       {
5827                         (*_bfd_error_handler)
5828                           (_("%B: .preinit_array section is not allowed in DSO"),
5829                            sub);
5830                         break;
5831                       }
5832
5833               bfd_set_error (bfd_error_nonrepresentable_section);
5834               return FALSE;
5835             }
5836
5837           if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5838               || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5839             return FALSE;
5840         }
5841       s = bfd_get_section_by_name (output_bfd, ".init_array");
5842       if (s != NULL && s->linker_has_input)
5843         {
5844           if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5845               || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5846             return FALSE;
5847         }
5848       s = bfd_get_section_by_name (output_bfd, ".fini_array");
5849       if (s != NULL && s->linker_has_input)
5850         {
5851           if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5852               || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5853             return FALSE;
5854         }
5855
5856       dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5857       /* If .dynstr is excluded from the link, we don't want any of
5858          these tags.  Strictly, we should be checking each section
5859          individually;  This quick check covers for the case where
5860          someone does a /DISCARD/ : { *(*) }.  */
5861       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5862         {
5863           bfd_size_type strsize;
5864
5865           strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5866           if ((info->emit_hash
5867                && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5868               || (info->emit_gnu_hash
5869                   && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5870               || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5871               || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5872               || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5873               || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5874                                               bed->s->sizeof_sym))
5875             return FALSE;
5876         }
5877     }
5878
5879   /* The backend must work out the sizes of all the other dynamic
5880      sections.  */
5881   if (bed->elf_backend_size_dynamic_sections
5882       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5883     return FALSE;
5884
5885   if (elf_hash_table (info)->dynamic_sections_created)
5886     {
5887       unsigned long section_sym_count;
5888       asection *s;
5889
5890       /* Set up the version definition section.  */
5891       s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5892       BFD_ASSERT (s != NULL);
5893
5894       /* We may have created additional version definitions if we are
5895          just linking a regular application.  */
5896       verdefs = asvinfo.verdefs;
5897
5898       /* Skip anonymous version tag.  */
5899       if (verdefs != NULL && verdefs->vernum == 0)
5900         verdefs = verdefs->next;
5901
5902       if (verdefs == NULL && !info->create_default_symver)
5903         s->flags |= SEC_EXCLUDE;
5904       else
5905         {
5906           unsigned int cdefs;
5907           bfd_size_type size;
5908           struct bfd_elf_version_tree *t;
5909           bfd_byte *p;
5910           Elf_Internal_Verdef def;
5911           Elf_Internal_Verdaux defaux;
5912           struct bfd_link_hash_entry *bh;
5913           struct elf_link_hash_entry *h;
5914           const char *name;
5915
5916           cdefs = 0;
5917           size = 0;
5918
5919           /* Make space for the base version.  */
5920           size += sizeof (Elf_External_Verdef);
5921           size += sizeof (Elf_External_Verdaux);
5922           ++cdefs;
5923
5924           /* Make space for the default version.  */
5925           if (info->create_default_symver)
5926             {
5927               size += sizeof (Elf_External_Verdef);
5928               ++cdefs;
5929             }
5930
5931           for (t = verdefs; t != NULL; t = t->next)
5932             {
5933               struct bfd_elf_version_deps *n;
5934
5935               /* Don't emit base version twice.  */
5936               if (t->vernum == 0)
5937                 continue;
5938
5939               size += sizeof (Elf_External_Verdef);
5940               size += sizeof (Elf_External_Verdaux);
5941               ++cdefs;
5942
5943               for (n = t->deps; n != NULL; n = n->next)
5944                 size += sizeof (Elf_External_Verdaux);
5945             }
5946
5947           s->size = size;
5948           s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5949           if (s->contents == NULL && s->size != 0)
5950             return FALSE;
5951
5952           /* Fill in the version definition section.  */
5953
5954           p = s->contents;
5955
5956           def.vd_version = VER_DEF_CURRENT;
5957           def.vd_flags = VER_FLG_BASE;
5958           def.vd_ndx = 1;
5959           def.vd_cnt = 1;
5960           if (info->create_default_symver)
5961             {
5962               def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5963               def.vd_next = sizeof (Elf_External_Verdef);
5964             }
5965           else
5966             {
5967               def.vd_aux = sizeof (Elf_External_Verdef);
5968               def.vd_next = (sizeof (Elf_External_Verdef)
5969                              + sizeof (Elf_External_Verdaux));
5970             }
5971
5972           if (soname_indx != (bfd_size_type) -1)
5973             {
5974               _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5975                                       soname_indx);
5976               def.vd_hash = bfd_elf_hash (soname);
5977               defaux.vda_name = soname_indx;
5978               name = soname;
5979             }
5980           else
5981             {
5982               bfd_size_type indx;
5983
5984               name = lbasename (output_bfd->filename);
5985               def.vd_hash = bfd_elf_hash (name);
5986               indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5987                                           name, FALSE);
5988               if (indx == (bfd_size_type) -1)
5989                 return FALSE;
5990               defaux.vda_name = indx;
5991             }
5992           defaux.vda_next = 0;
5993
5994           _bfd_elf_swap_verdef_out (output_bfd, &def,
5995                                     (Elf_External_Verdef *) p);
5996           p += sizeof (Elf_External_Verdef);
5997           if (info->create_default_symver)
5998             {
5999               /* Add a symbol representing this version.  */
6000               bh = NULL;
6001               if (! (_bfd_generic_link_add_one_symbol
6002                      (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6003                       0, NULL, FALSE,
6004                       get_elf_backend_data (dynobj)->collect, &bh)))
6005                 return FALSE;
6006               h = (struct elf_link_hash_entry *) bh;
6007               h->non_elf = 0;
6008               h->def_regular = 1;
6009               h->type = STT_OBJECT;
6010               h->verinfo.vertree = NULL;
6011
6012               if (! bfd_elf_link_record_dynamic_symbol (info, h))
6013                 return FALSE;
6014
6015               /* Create a duplicate of the base version with the same
6016                  aux block, but different flags.  */
6017               def.vd_flags = 0;
6018               def.vd_ndx = 2;
6019               def.vd_aux = sizeof (Elf_External_Verdef);
6020               if (verdefs)
6021                 def.vd_next = (sizeof (Elf_External_Verdef)
6022                                + sizeof (Elf_External_Verdaux));
6023               else
6024                 def.vd_next = 0;
6025               _bfd_elf_swap_verdef_out (output_bfd, &def,
6026                                         (Elf_External_Verdef *) p);
6027               p += sizeof (Elf_External_Verdef);
6028             }
6029           _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6030                                      (Elf_External_Verdaux *) p);
6031           p += sizeof (Elf_External_Verdaux);
6032
6033           for (t = verdefs; t != NULL; t = t->next)
6034             {
6035               unsigned int cdeps;
6036               struct bfd_elf_version_deps *n;
6037
6038               /* Don't emit the base version twice.  */
6039               if (t->vernum == 0)
6040                 continue;
6041
6042               cdeps = 0;
6043               for (n = t->deps; n != NULL; n = n->next)
6044                 ++cdeps;
6045
6046               /* Add a symbol representing this version.  */
6047               bh = NULL;
6048               if (! (_bfd_generic_link_add_one_symbol
6049                      (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6050                       0, NULL, FALSE,
6051                       get_elf_backend_data (dynobj)->collect, &bh)))
6052                 return FALSE;
6053               h = (struct elf_link_hash_entry *) bh;
6054               h->non_elf = 0;
6055               h->def_regular = 1;
6056               h->type = STT_OBJECT;
6057               h->verinfo.vertree = t;
6058
6059               if (! bfd_elf_link_record_dynamic_symbol (info, h))
6060                 return FALSE;
6061
6062               def.vd_version = VER_DEF_CURRENT;
6063               def.vd_flags = 0;
6064               if (t->globals.list == NULL
6065                   && t->locals.list == NULL
6066                   && ! t->used)
6067                 def.vd_flags |= VER_FLG_WEAK;
6068               def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6069               def.vd_cnt = cdeps + 1;
6070               def.vd_hash = bfd_elf_hash (t->name);
6071               def.vd_aux = sizeof (Elf_External_Verdef);
6072               def.vd_next = 0;
6073
6074               /* If a basever node is next, it *must* be the last node in
6075                  the chain, otherwise Verdef construction breaks.  */
6076               if (t->next != NULL && t->next->vernum == 0)
6077                 BFD_ASSERT (t->next->next == NULL);
6078
6079               if (t->next != NULL && t->next->vernum != 0)
6080                 def.vd_next = (sizeof (Elf_External_Verdef)
6081                                + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6082
6083               _bfd_elf_swap_verdef_out (output_bfd, &def,
6084                                         (Elf_External_Verdef *) p);
6085               p += sizeof (Elf_External_Verdef);
6086
6087               defaux.vda_name = h->dynstr_index;
6088               _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6089                                       h->dynstr_index);
6090               defaux.vda_next = 0;
6091               if (t->deps != NULL)
6092                 defaux.vda_next = sizeof (Elf_External_Verdaux);
6093               t->name_indx = defaux.vda_name;
6094
6095               _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6096                                          (Elf_External_Verdaux *) p);
6097               p += sizeof (Elf_External_Verdaux);
6098
6099               for (n = t->deps; n != NULL; n = n->next)
6100                 {
6101                   if (n->version_needed == NULL)
6102                     {
6103                       /* This can happen if there was an error in the
6104                          version script.  */
6105                       defaux.vda_name = 0;
6106                     }
6107                   else
6108                     {
6109                       defaux.vda_name = n->version_needed->name_indx;
6110                       _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6111                                               defaux.vda_name);
6112                     }
6113                   if (n->next == NULL)
6114                     defaux.vda_next = 0;
6115                   else
6116                     defaux.vda_next = sizeof (Elf_External_Verdaux);
6117
6118                   _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6119                                              (Elf_External_Verdaux *) p);
6120                   p += sizeof (Elf_External_Verdaux);
6121                 }
6122             }
6123
6124           if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6125               || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6126             return FALSE;
6127
6128           elf_tdata (output_bfd)->cverdefs = cdefs;
6129         }
6130
6131       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6132         {
6133           if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6134             return FALSE;
6135         }
6136       else if (info->flags & DF_BIND_NOW)
6137         {
6138           if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6139             return FALSE;
6140         }
6141
6142       if (info->flags_1)
6143         {
6144           if (info->executable)
6145             info->flags_1 &= ~ (DF_1_INITFIRST
6146                                 | DF_1_NODELETE
6147                                 | DF_1_NOOPEN);
6148           if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6149             return FALSE;
6150         }
6151
6152       /* Work out the size of the version reference section.  */
6153
6154       s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6155       BFD_ASSERT (s != NULL);
6156       {
6157         struct elf_find_verdep_info sinfo;
6158
6159         sinfo.info = info;
6160         sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6161         if (sinfo.vers == 0)
6162           sinfo.vers = 1;
6163         sinfo.failed = FALSE;
6164
6165         elf_link_hash_traverse (elf_hash_table (info),
6166                                 _bfd_elf_link_find_version_dependencies,
6167                                 &sinfo);
6168         if (sinfo.failed)
6169           return FALSE;
6170
6171         if (elf_tdata (output_bfd)->verref == NULL)
6172           s->flags |= SEC_EXCLUDE;
6173         else
6174           {
6175             Elf_Internal_Verneed *t;
6176             unsigned int size;
6177             unsigned int crefs;
6178             bfd_byte *p;
6179
6180             /* Build the version dependency section.  */
6181             size = 0;
6182             crefs = 0;
6183             for (t = elf_tdata (output_bfd)->verref;
6184                  t != NULL;
6185                  t = t->vn_nextref)
6186               {
6187                 Elf_Internal_Vernaux *a;
6188
6189                 size += sizeof (Elf_External_Verneed);
6190                 ++crefs;
6191                 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6192                   size += sizeof (Elf_External_Vernaux);
6193               }
6194
6195             s->size = size;
6196             s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6197             if (s->contents == NULL)
6198               return FALSE;
6199
6200             p = s->contents;
6201             for (t = elf_tdata (output_bfd)->verref;
6202                  t != NULL;
6203                  t = t->vn_nextref)
6204               {
6205                 unsigned int caux;
6206                 Elf_Internal_Vernaux *a;
6207                 bfd_size_type indx;
6208
6209                 caux = 0;
6210                 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6211                   ++caux;
6212
6213                 t->vn_version = VER_NEED_CURRENT;
6214                 t->vn_cnt = caux;
6215                 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6216                                             elf_dt_name (t->vn_bfd) != NULL
6217                                             ? elf_dt_name (t->vn_bfd)
6218                                             : lbasename (t->vn_bfd->filename),
6219                                             FALSE);
6220                 if (indx == (bfd_size_type) -1)
6221                   return FALSE;
6222                 t->vn_file = indx;
6223                 t->vn_aux = sizeof (Elf_External_Verneed);
6224                 if (t->vn_nextref == NULL)
6225                   t->vn_next = 0;
6226                 else
6227                   t->vn_next = (sizeof (Elf_External_Verneed)
6228                                 + caux * sizeof (Elf_External_Vernaux));
6229
6230                 _bfd_elf_swap_verneed_out (output_bfd, t,
6231                                            (Elf_External_Verneed *) p);
6232                 p += sizeof (Elf_External_Verneed);
6233
6234                 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6235                   {
6236                     a->vna_hash = bfd_elf_hash (a->vna_nodename);
6237                     indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6238                                                 a->vna_nodename, FALSE);
6239                     if (indx == (bfd_size_type) -1)
6240                       return FALSE;
6241                     a->vna_name = indx;
6242                     if (a->vna_nextptr == NULL)
6243                       a->vna_next = 0;
6244                     else
6245                       a->vna_next = sizeof (Elf_External_Vernaux);
6246
6247                     _bfd_elf_swap_vernaux_out (output_bfd, a,
6248                                                (Elf_External_Vernaux *) p);
6249                     p += sizeof (Elf_External_Vernaux);
6250                   }
6251               }
6252
6253             if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6254                 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6255               return FALSE;
6256
6257             elf_tdata (output_bfd)->cverrefs = crefs;
6258           }
6259       }
6260
6261       if ((elf_tdata (output_bfd)->cverrefs == 0
6262            && elf_tdata (output_bfd)->cverdefs == 0)
6263           || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6264                                              &section_sym_count) == 0)
6265         {
6266           s = bfd_get_section_by_name (dynobj, ".gnu.version");
6267           s->flags |= SEC_EXCLUDE;
6268         }
6269     }
6270   return TRUE;
6271 }
6272
6273 /* Find the first non-excluded output section.  We'll use its
6274    section symbol for some emitted relocs.  */
6275 void
6276 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6277 {
6278   asection *s;
6279
6280   for (s = output_bfd->sections; s != NULL; s = s->next)
6281     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6282         && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6283       {
6284         elf_hash_table (info)->text_index_section = s;
6285         break;
6286       }
6287 }
6288
6289 /* Find two non-excluded output sections, one for code, one for data.
6290    We'll use their section symbols for some emitted relocs.  */
6291 void
6292 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6293 {
6294   asection *s;
6295
6296   /* Data first, since setting text_index_section changes
6297      _bfd_elf_link_omit_section_dynsym.  */
6298   for (s = output_bfd->sections; s != NULL; s = s->next)
6299     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6300         && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6301       {
6302         elf_hash_table (info)->data_index_section = s;
6303         break;
6304       }
6305
6306   for (s = output_bfd->sections; s != NULL; s = s->next)
6307     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6308          == (SEC_ALLOC | SEC_READONLY))
6309         && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6310       {
6311         elf_hash_table (info)->text_index_section = s;
6312         break;
6313       }
6314
6315   if (elf_hash_table (info)->text_index_section == NULL)
6316     elf_hash_table (info)->text_index_section
6317       = elf_hash_table (info)->data_index_section;
6318 }
6319
6320 bfd_boolean
6321 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6322 {
6323   const struct elf_backend_data *bed;
6324
6325   if (!is_elf_hash_table (info->hash))
6326     return TRUE;
6327
6328   bed = get_elf_backend_data (output_bfd);
6329   (*bed->elf_backend_init_index_section) (output_bfd, info);
6330
6331   if (elf_hash_table (info)->dynamic_sections_created)
6332     {
6333       bfd *dynobj;
6334       asection *s;
6335       bfd_size_type dynsymcount;
6336       unsigned long section_sym_count;
6337       unsigned int dtagcount;
6338
6339       dynobj = elf_hash_table (info)->dynobj;
6340
6341       /* Assign dynsym indicies.  In a shared library we generate a
6342          section symbol for each output section, which come first.
6343          Next come all of the back-end allocated local dynamic syms,
6344          followed by the rest of the global symbols.  */
6345
6346       dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6347                                                     &section_sym_count);
6348
6349       /* Work out the size of the symbol version section.  */
6350       s = bfd_get_section_by_name (dynobj, ".gnu.version");
6351       BFD_ASSERT (s != NULL);
6352       if (dynsymcount != 0
6353           && (s->flags & SEC_EXCLUDE) == 0)
6354         {
6355           s->size = dynsymcount * sizeof (Elf_External_Versym);
6356           s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6357           if (s->contents == NULL)
6358             return FALSE;
6359
6360           if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6361             return FALSE;
6362         }
6363
6364       /* Set the size of the .dynsym and .hash sections.  We counted
6365          the number of dynamic symbols in elf_link_add_object_symbols.
6366          We will build the contents of .dynsym and .hash when we build
6367          the final symbol table, because until then we do not know the
6368          correct value to give the symbols.  We built the .dynstr
6369          section as we went along in elf_link_add_object_symbols.  */
6370       s = bfd_get_section_by_name (dynobj, ".dynsym");
6371       BFD_ASSERT (s != NULL);
6372       s->size = dynsymcount * bed->s->sizeof_sym;
6373
6374       if (dynsymcount != 0)
6375         {
6376           s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6377           if (s->contents == NULL)
6378             return FALSE;
6379
6380           /* The first entry in .dynsym is a dummy symbol.
6381              Clear all the section syms, in case we don't output them all.  */
6382           ++section_sym_count;
6383           memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6384         }
6385
6386       elf_hash_table (info)->bucketcount = 0;
6387
6388       /* Compute the size of the hashing table.  As a side effect this
6389          computes the hash values for all the names we export.  */
6390       if (info->emit_hash)
6391         {
6392           unsigned long int *hashcodes;
6393           struct hash_codes_info hashinf;
6394           bfd_size_type amt;
6395           unsigned long int nsyms;
6396           size_t bucketcount;
6397           size_t hash_entry_size;
6398
6399           /* Compute the hash values for all exported symbols.  At the same
6400              time store the values in an array so that we could use them for
6401              optimizations.  */
6402           amt = dynsymcount * sizeof (unsigned long int);
6403           hashcodes = (unsigned long int *) bfd_malloc (amt);
6404           if (hashcodes == NULL)
6405             return FALSE;
6406           hashinf.hashcodes = hashcodes;
6407           hashinf.error = FALSE;
6408
6409           /* Put all hash values in HASHCODES.  */
6410           elf_link_hash_traverse (elf_hash_table (info),
6411                                   elf_collect_hash_codes, &hashinf);
6412           if (hashinf.error)
6413             {
6414               free (hashcodes);
6415               return FALSE;
6416             }
6417
6418           nsyms = hashinf.hashcodes - hashcodes;
6419           bucketcount
6420             = compute_bucket_count (info, hashcodes, nsyms, 0);
6421           free (hashcodes);
6422
6423           if (bucketcount == 0)
6424             return FALSE;
6425
6426           elf_hash_table (info)->bucketcount = bucketcount;
6427
6428           s = bfd_get_section_by_name (dynobj, ".hash");
6429           BFD_ASSERT (s != NULL);
6430           hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6431           s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6432           s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6433           if (s->contents == NULL)
6434             return FALSE;
6435
6436           bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6437           bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6438                    s->contents + hash_entry_size);
6439         }
6440
6441       if (info->emit_gnu_hash)
6442         {
6443           size_t i, cnt;
6444           unsigned char *contents;
6445           struct collect_gnu_hash_codes cinfo;
6446           bfd_size_type amt;
6447           size_t bucketcount;
6448
6449           memset (&cinfo, 0, sizeof (cinfo));
6450
6451           /* Compute the hash values for all exported symbols.  At the same
6452              time store the values in an array so that we could use them for
6453              optimizations.  */
6454           amt = dynsymcount * 2 * sizeof (unsigned long int);
6455           cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6456           if (cinfo.hashcodes == NULL)
6457             return FALSE;
6458
6459           cinfo.hashval = cinfo.hashcodes + dynsymcount;
6460           cinfo.min_dynindx = -1;
6461           cinfo.output_bfd = output_bfd;
6462           cinfo.bed = bed;
6463
6464           /* Put all hash values in HASHCODES.  */
6465           elf_link_hash_traverse (elf_hash_table (info),
6466                                   elf_collect_gnu_hash_codes, &cinfo);
6467           if (cinfo.error)
6468             {
6469               free (cinfo.hashcodes);
6470               return FALSE;
6471             }
6472
6473           bucketcount
6474             = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6475
6476           if (bucketcount == 0)
6477             {
6478               free (cinfo.hashcodes);
6479               return FALSE;
6480             }
6481
6482           s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6483           BFD_ASSERT (s != NULL);
6484
6485           if (cinfo.nsyms == 0)
6486             {
6487               /* Empty .gnu.hash section is special.  */
6488               BFD_ASSERT (cinfo.min_dynindx == -1);
6489               free (cinfo.hashcodes);
6490               s->size = 5 * 4 + bed->s->arch_size / 8;
6491               contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6492               if (contents == NULL)
6493                 return FALSE;
6494               s->contents = contents;
6495               /* 1 empty bucket.  */
6496               bfd_put_32 (output_bfd, 1, contents);
6497               /* SYMIDX above the special symbol 0.  */
6498               bfd_put_32 (output_bfd, 1, contents + 4);
6499               /* Just one word for bitmask.  */
6500               bfd_put_32 (output_bfd, 1, contents + 8);
6501               /* Only hash fn bloom filter.  */
6502               bfd_put_32 (output_bfd, 0, contents + 12);
6503               /* No hashes are valid - empty bitmask.  */
6504               bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6505               /* No hashes in the only bucket.  */
6506               bfd_put_32 (output_bfd, 0,
6507                           contents + 16 + bed->s->arch_size / 8);
6508             }
6509           else
6510             {
6511               unsigned long int maskwords, maskbitslog2, x;
6512               BFD_ASSERT (cinfo.min_dynindx != -1);
6513
6514               x = cinfo.nsyms;
6515               maskbitslog2 = 1;
6516               while ((x >>= 1) != 0)
6517                 ++maskbitslog2;
6518               if (maskbitslog2 < 3)
6519                 maskbitslog2 = 5;
6520               else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6521                 maskbitslog2 = maskbitslog2 + 3;
6522               else
6523                 maskbitslog2 = maskbitslog2 + 2;
6524               if (bed->s->arch_size == 64)
6525                 {
6526                   if (maskbitslog2 == 5)
6527                     maskbitslog2 = 6;
6528                   cinfo.shift1 = 6;
6529                 }
6530               else
6531                 cinfo.shift1 = 5;
6532               cinfo.mask = (1 << cinfo.shift1) - 1;
6533               cinfo.shift2 = maskbitslog2;
6534               cinfo.maskbits = 1 << maskbitslog2;
6535               maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6536               amt = bucketcount * sizeof (unsigned long int) * 2;
6537               amt += maskwords * sizeof (bfd_vma);
6538               cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6539               if (cinfo.bitmask == NULL)
6540                 {
6541                   free (cinfo.hashcodes);
6542                   return FALSE;
6543                 }
6544
6545               cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6546               cinfo.indx = cinfo.counts + bucketcount;
6547               cinfo.symindx = dynsymcount - cinfo.nsyms;
6548               memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6549
6550               /* Determine how often each hash bucket is used.  */
6551               memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6552               for (i = 0; i < cinfo.nsyms; ++i)
6553                 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6554
6555               for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6556                 if (cinfo.counts[i] != 0)
6557                   {
6558                     cinfo.indx[i] = cnt;
6559                     cnt += cinfo.counts[i];
6560                   }
6561               BFD_ASSERT (cnt == dynsymcount);
6562               cinfo.bucketcount = bucketcount;
6563               cinfo.local_indx = cinfo.min_dynindx;
6564
6565               s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6566               s->size += cinfo.maskbits / 8;
6567               contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6568               if (contents == NULL)
6569                 {
6570                   free (cinfo.bitmask);
6571                   free (cinfo.hashcodes);
6572                   return FALSE;
6573                 }
6574
6575               s->contents = contents;
6576               bfd_put_32 (output_bfd, bucketcount, contents);
6577               bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6578               bfd_put_32 (output_bfd, maskwords, contents + 8);
6579               bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6580               contents += 16 + cinfo.maskbits / 8;
6581
6582               for (i = 0; i < bucketcount; ++i)
6583                 {
6584                   if (cinfo.counts[i] == 0)
6585                     bfd_put_32 (output_bfd, 0, contents);
6586                   else
6587                     bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6588                   contents += 4;
6589                 }
6590
6591               cinfo.contents = contents;
6592
6593               /* Renumber dynamic symbols, populate .gnu.hash section.  */
6594               elf_link_hash_traverse (elf_hash_table (info),
6595                                       elf_renumber_gnu_hash_syms, &cinfo);
6596
6597               contents = s->contents + 16;
6598               for (i = 0; i < maskwords; ++i)
6599                 {
6600                   bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6601                            contents);
6602                   contents += bed->s->arch_size / 8;
6603                 }
6604
6605               free (cinfo.bitmask);
6606               free (cinfo.hashcodes);
6607             }
6608         }
6609
6610       s = bfd_get_section_by_name (dynobj, ".dynstr");
6611       BFD_ASSERT (s != NULL);
6612
6613       elf_finalize_dynstr (output_bfd, info);
6614
6615       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6616
6617       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6618         if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6619           return FALSE;
6620     }
6621
6622   return TRUE;
6623 }
6624 \f
6625 /* Indicate that we are only retrieving symbol values from this
6626    section.  */
6627
6628 void
6629 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6630 {
6631   if (is_elf_hash_table (info->hash))
6632     sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6633   _bfd_generic_link_just_syms (sec, info);
6634 }
6635
6636 /* Make sure sec_info_type is cleared if sec_info is cleared too.  */
6637
6638 static void
6639 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6640                             asection *sec)
6641 {
6642   BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6643   sec->sec_info_type = ELF_INFO_TYPE_NONE;
6644 }
6645
6646 /* Finish SHF_MERGE section merging.  */
6647
6648 bfd_boolean
6649 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6650 {
6651   bfd *ibfd;
6652   asection *sec;
6653
6654   if (!is_elf_hash_table (info->hash))
6655     return FALSE;
6656
6657   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6658     if ((ibfd->flags & DYNAMIC) == 0)
6659       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6660         if ((sec->flags & SEC_MERGE) != 0
6661             && !bfd_is_abs_section (sec->output_section))
6662           {
6663             struct bfd_elf_section_data *secdata;
6664
6665             secdata = elf_section_data (sec);
6666             if (! _bfd_add_merge_section (abfd,
6667                                           &elf_hash_table (info)->merge_info,
6668                                           sec, &secdata->sec_info))
6669               return FALSE;
6670             else if (secdata->sec_info)
6671               sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6672           }
6673
6674   if (elf_hash_table (info)->merge_info != NULL)
6675     _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6676                          merge_sections_remove_hook);
6677   return TRUE;
6678 }
6679
6680 /* Create an entry in an ELF linker hash table.  */
6681
6682 struct bfd_hash_entry *
6683 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6684                             struct bfd_hash_table *table,
6685                             const char *string)
6686 {
6687   /* Allocate the structure if it has not already been allocated by a
6688      subclass.  */
6689   if (entry == NULL)
6690     {
6691       entry = (struct bfd_hash_entry *)
6692           bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6693       if (entry == NULL)
6694         return entry;
6695     }
6696
6697   /* Call the allocation method of the superclass.  */
6698   entry = _bfd_link_hash_newfunc (entry, table, string);
6699   if (entry != NULL)
6700     {
6701       struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6702       struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6703
6704       /* Set local fields.  */
6705       ret->indx = -1;
6706       ret->dynindx = -1;
6707       ret->got = htab->init_got_refcount;
6708       ret->plt = htab->init_plt_refcount;
6709       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6710                               - offsetof (struct elf_link_hash_entry, size)));
6711       /* Assume that we have been called by a non-ELF symbol reader.
6712          This flag is then reset by the code which reads an ELF input
6713          file.  This ensures that a symbol created by a non-ELF symbol
6714          reader will have the flag set correctly.  */
6715       ret->non_elf = 1;
6716     }
6717
6718   return entry;
6719 }
6720
6721 /* Copy data from an indirect symbol to its direct symbol, hiding the
6722    old indirect symbol.  Also used for copying flags to a weakdef.  */
6723
6724 void
6725 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6726                                   struct elf_link_hash_entry *dir,
6727                                   struct elf_link_hash_entry *ind)
6728 {
6729   struct elf_link_hash_table *htab;
6730
6731   /* Copy down any references that we may have already seen to the
6732      symbol which just became indirect.  */
6733
6734   dir->ref_dynamic |= ind->ref_dynamic;
6735   dir->ref_regular |= ind->ref_regular;
6736   dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6737   dir->non_got_ref |= ind->non_got_ref;
6738   dir->needs_plt |= ind->needs_plt;
6739   dir->pointer_equality_needed |= ind->pointer_equality_needed;
6740
6741   if (ind->root.type != bfd_link_hash_indirect)
6742     return;
6743
6744   /* Copy over the global and procedure linkage table refcount entries.
6745      These may have been already set up by a check_relocs routine.  */
6746   htab = elf_hash_table (info);
6747   if (ind->got.refcount > htab->init_got_refcount.refcount)
6748     {
6749       if (dir->got.refcount < 0)
6750         dir->got.refcount = 0;
6751       dir->got.refcount += ind->got.refcount;
6752       ind->got.refcount = htab->init_got_refcount.refcount;
6753     }
6754
6755   if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6756     {
6757       if (dir->plt.refcount < 0)
6758         dir->plt.refcount = 0;
6759       dir->plt.refcount += ind->plt.refcount;
6760       ind->plt.refcount = htab->init_plt_refcount.refcount;
6761     }
6762
6763   if (ind->dynindx != -1)
6764     {
6765       if (dir->dynindx != -1)
6766         _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6767       dir->dynindx = ind->dynindx;
6768       dir->dynstr_index = ind->dynstr_index;
6769       ind->dynindx = -1;
6770       ind->dynstr_index = 0;
6771     }
6772 }
6773
6774 void
6775 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6776                                 struct elf_link_hash_entry *h,
6777                                 bfd_boolean force_local)
6778 {
6779   /* STT_GNU_IFUNC symbol must go through PLT.  */
6780   if (h->type != STT_GNU_IFUNC)
6781     {
6782       h->plt = elf_hash_table (info)->init_plt_offset;
6783       h->needs_plt = 0;
6784     }
6785   if (force_local)
6786     {
6787       h->forced_local = 1;
6788       if (h->dynindx != -1)
6789         {
6790           h->dynindx = -1;
6791           _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6792                                   h->dynstr_index);
6793         }
6794     }
6795 }
6796
6797 /* Initialize an ELF linker hash table.  */
6798
6799 bfd_boolean
6800 _bfd_elf_link_hash_table_init
6801   (struct elf_link_hash_table *table,
6802    bfd *abfd,
6803    struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6804                                       struct bfd_hash_table *,
6805                                       const char *),
6806    unsigned int entsize,
6807    enum elf_target_id target_id)
6808 {
6809   bfd_boolean ret;
6810   int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6811
6812   memset (table, 0, sizeof * table);
6813   table->init_got_refcount.refcount = can_refcount - 1;
6814   table->init_plt_refcount.refcount = can_refcount - 1;
6815   table->init_got_offset.offset = -(bfd_vma) 1;
6816   table->init_plt_offset.offset = -(bfd_vma) 1;
6817   /* The first dynamic symbol is a dummy.  */
6818   table->dynsymcount = 1;
6819
6820   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6821
6822   table->root.type = bfd_link_elf_hash_table;
6823   table->hash_table_id = target_id;
6824
6825   return ret;
6826 }
6827
6828 /* Create an ELF linker hash table.  */
6829
6830 struct bfd_link_hash_table *
6831 _bfd_elf_link_hash_table_create (bfd *abfd)
6832 {
6833   struct elf_link_hash_table *ret;
6834   bfd_size_type amt = sizeof (struct elf_link_hash_table);
6835
6836   ret = (struct elf_link_hash_table *) bfd_malloc (amt);
6837   if (ret == NULL)
6838     return NULL;
6839
6840   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6841                                        sizeof (struct elf_link_hash_entry),
6842                                        GENERIC_ELF_DATA))
6843     {
6844       free (ret);
6845       return NULL;
6846     }
6847
6848   return &ret->root;
6849 }
6850
6851 /* This is a hook for the ELF emulation code in the generic linker to
6852    tell the backend linker what file name to use for the DT_NEEDED
6853    entry for a dynamic object.  */
6854
6855 void
6856 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6857 {
6858   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6859       && bfd_get_format (abfd) == bfd_object)
6860     elf_dt_name (abfd) = name;
6861 }
6862
6863 int
6864 bfd_elf_get_dyn_lib_class (bfd *abfd)
6865 {
6866   int lib_class;
6867   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6868       && bfd_get_format (abfd) == bfd_object)
6869     lib_class = elf_dyn_lib_class (abfd);
6870   else
6871     lib_class = 0;
6872   return lib_class;
6873 }
6874
6875 void
6876 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6877 {
6878   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6879       && bfd_get_format (abfd) == bfd_object)
6880     elf_dyn_lib_class (abfd) = lib_class;
6881 }
6882
6883 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
6884    the linker ELF emulation code.  */
6885
6886 struct bfd_link_needed_list *
6887 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6888                          struct bfd_link_info *info)
6889 {
6890   if (! is_elf_hash_table (info->hash))
6891     return NULL;
6892   return elf_hash_table (info)->needed;
6893 }
6894
6895 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link.  This is a
6896    hook for the linker ELF emulation code.  */
6897
6898 struct bfd_link_needed_list *
6899 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6900                           struct bfd_link_info *info)
6901 {
6902   if (! is_elf_hash_table (info->hash))
6903     return NULL;
6904   return elf_hash_table (info)->runpath;
6905 }
6906
6907 /* Get the name actually used for a dynamic object for a link.  This
6908    is the SONAME entry if there is one.  Otherwise, it is the string
6909    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
6910
6911 const char *
6912 bfd_elf_get_dt_soname (bfd *abfd)
6913 {
6914   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6915       && bfd_get_format (abfd) == bfd_object)
6916     return elf_dt_name (abfd);
6917   return NULL;
6918 }
6919
6920 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
6921    the ELF linker emulation code.  */
6922
6923 bfd_boolean
6924 bfd_elf_get_bfd_needed_list (bfd *abfd,
6925                              struct bfd_link_needed_list **pneeded)
6926 {
6927   asection *s;
6928   bfd_byte *dynbuf = NULL;
6929   unsigned int elfsec;
6930   unsigned long shlink;
6931   bfd_byte *extdyn, *extdynend;
6932   size_t extdynsize;
6933   void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6934
6935   *pneeded = NULL;
6936
6937   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6938       || bfd_get_format (abfd) != bfd_object)
6939     return TRUE;
6940
6941   s = bfd_get_section_by_name (abfd, ".dynamic");
6942   if (s == NULL || s->size == 0)
6943     return TRUE;
6944
6945   if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6946     goto error_return;
6947
6948   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
6949   if (elfsec == SHN_BAD)
6950     goto error_return;
6951
6952   shlink = elf_elfsections (abfd)[elfsec]->sh_link;
6953
6954   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6955   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6956
6957   extdyn = dynbuf;
6958   extdynend = extdyn + s->size;
6959   for (; extdyn < extdynend; extdyn += extdynsize)
6960     {
6961       Elf_Internal_Dyn dyn;
6962
6963       (*swap_dyn_in) (abfd, extdyn, &dyn);
6964
6965       if (dyn.d_tag == DT_NULL)
6966         break;
6967
6968       if (dyn.d_tag == DT_NEEDED)
6969         {
6970           const char *string;
6971           struct bfd_link_needed_list *l;
6972           unsigned int tagv = dyn.d_un.d_val;
6973           bfd_size_type amt;
6974
6975           string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6976           if (string == NULL)
6977             goto error_return;
6978
6979           amt = sizeof *l;
6980           l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
6981           if (l == NULL)
6982             goto error_return;
6983
6984           l->by = abfd;
6985           l->name = string;
6986           l->next = *pneeded;
6987           *pneeded = l;
6988         }
6989     }
6990
6991   free (dynbuf);
6992
6993   return TRUE;
6994
6995  error_return:
6996   if (dynbuf != NULL)
6997     free (dynbuf);
6998   return FALSE;
6999 }
7000
7001 struct elf_symbuf_symbol
7002 {
7003   unsigned long st_name;        /* Symbol name, index in string tbl */
7004   unsigned char st_info;        /* Type and binding attributes */
7005   unsigned char st_other;       /* Visibilty, and target specific */
7006 };
7007
7008 struct elf_symbuf_head
7009 {
7010   struct elf_symbuf_symbol *ssym;
7011   bfd_size_type count;
7012   unsigned int st_shndx;
7013 };
7014
7015 struct elf_symbol
7016 {
7017   union
7018     {
7019       Elf_Internal_Sym *isym;
7020       struct elf_symbuf_symbol *ssym;
7021     } u;
7022   const char *name;
7023 };
7024
7025 /* Sort references to symbols by ascending section number.  */
7026
7027 static int
7028 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7029 {
7030   const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7031   const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7032
7033   return s1->st_shndx - s2->st_shndx;
7034 }
7035
7036 static int
7037 elf_sym_name_compare (const void *arg1, const void *arg2)
7038 {
7039   const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7040   const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7041   return strcmp (s1->name, s2->name);
7042 }
7043
7044 static struct elf_symbuf_head *
7045 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7046 {
7047   Elf_Internal_Sym **ind, **indbufend, **indbuf;
7048   struct elf_symbuf_symbol *ssym;
7049   struct elf_symbuf_head *ssymbuf, *ssymhead;
7050   bfd_size_type i, shndx_count, total_size;
7051
7052   indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7053   if (indbuf == NULL)
7054     return NULL;
7055
7056   for (ind = indbuf, i = 0; i < symcount; i++)
7057     if (isymbuf[i].st_shndx != SHN_UNDEF)
7058       *ind++ = &isymbuf[i];
7059   indbufend = ind;
7060
7061   qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7062          elf_sort_elf_symbol);
7063
7064   shndx_count = 0;
7065   if (indbufend > indbuf)
7066     for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7067       if (ind[0]->st_shndx != ind[1]->st_shndx)
7068         shndx_count++;
7069
7070   total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7071                 + (indbufend - indbuf) * sizeof (*ssym));
7072   ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7073   if (ssymbuf == NULL)
7074     {
7075       free (indbuf);
7076       return NULL;
7077     }
7078
7079   ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7080   ssymbuf->ssym = NULL;
7081   ssymbuf->count = shndx_count;
7082   ssymbuf->st_shndx = 0;
7083   for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7084     {
7085       if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7086         {
7087           ssymhead++;
7088           ssymhead->ssym = ssym;
7089           ssymhead->count = 0;
7090           ssymhead->st_shndx = (*ind)->st_shndx;
7091         }
7092       ssym->st_name = (*ind)->st_name;
7093       ssym->st_info = (*ind)->st_info;
7094       ssym->st_other = (*ind)->st_other;
7095       ssymhead->count++;
7096     }
7097   BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7098               && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7099                   == total_size));
7100
7101   free (indbuf);
7102   return ssymbuf;
7103 }
7104
7105 /* Check if 2 sections define the same set of local and global
7106    symbols.  */
7107
7108 static bfd_boolean
7109 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7110                                    struct bfd_link_info *info)
7111 {
7112   bfd *bfd1, *bfd2;
7113   const struct elf_backend_data *bed1, *bed2;
7114   Elf_Internal_Shdr *hdr1, *hdr2;
7115   bfd_size_type symcount1, symcount2;
7116   Elf_Internal_Sym *isymbuf1, *isymbuf2;
7117   struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7118   Elf_Internal_Sym *isym, *isymend;
7119   struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7120   bfd_size_type count1, count2, i;
7121   unsigned int shndx1, shndx2;
7122   bfd_boolean result;
7123
7124   bfd1 = sec1->owner;
7125   bfd2 = sec2->owner;
7126
7127   /* Both sections have to be in ELF.  */
7128   if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7129       || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7130     return FALSE;
7131
7132   if (elf_section_type (sec1) != elf_section_type (sec2))
7133     return FALSE;
7134
7135   shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7136   shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7137   if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7138     return FALSE;
7139
7140   bed1 = get_elf_backend_data (bfd1);
7141   bed2 = get_elf_backend_data (bfd2);
7142   hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7143   symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7144   hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7145   symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7146
7147   if (symcount1 == 0 || symcount2 == 0)
7148     return FALSE;
7149
7150   result = FALSE;
7151   isymbuf1 = NULL;
7152   isymbuf2 = NULL;
7153   ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7154   ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7155
7156   if (ssymbuf1 == NULL)
7157     {
7158       isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7159                                        NULL, NULL, NULL);
7160       if (isymbuf1 == NULL)
7161         goto done;
7162
7163       if (!info->reduce_memory_overheads)
7164         elf_tdata (bfd1)->symbuf = ssymbuf1
7165           = elf_create_symbuf (symcount1, isymbuf1);
7166     }
7167
7168   if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7169     {
7170       isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7171                                        NULL, NULL, NULL);
7172       if (isymbuf2 == NULL)
7173         goto done;
7174
7175       if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7176         elf_tdata (bfd2)->symbuf = ssymbuf2
7177           = elf_create_symbuf (symcount2, isymbuf2);
7178     }
7179
7180   if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7181     {
7182       /* Optimized faster version.  */
7183       bfd_size_type lo, hi, mid;
7184       struct elf_symbol *symp;
7185       struct elf_symbuf_symbol *ssym, *ssymend;
7186
7187       lo = 0;
7188       hi = ssymbuf1->count;
7189       ssymbuf1++;
7190       count1 = 0;
7191       while (lo < hi)
7192         {
7193           mid = (lo + hi) / 2;
7194           if (shndx1 < ssymbuf1[mid].st_shndx)
7195             hi = mid;
7196           else if (shndx1 > ssymbuf1[mid].st_shndx)
7197             lo = mid + 1;
7198           else
7199             {
7200               count1 = ssymbuf1[mid].count;
7201               ssymbuf1 += mid;
7202               break;
7203             }
7204         }
7205
7206       lo = 0;
7207       hi = ssymbuf2->count;
7208       ssymbuf2++;
7209       count2 = 0;
7210       while (lo < hi)
7211         {
7212           mid = (lo + hi) / 2;
7213           if (shndx2 < ssymbuf2[mid].st_shndx)
7214             hi = mid;
7215           else if (shndx2 > ssymbuf2[mid].st_shndx)
7216             lo = mid + 1;
7217           else
7218             {
7219               count2 = ssymbuf2[mid].count;
7220               ssymbuf2 += mid;
7221               break;
7222             }
7223         }
7224
7225       if (count1 == 0 || count2 == 0 || count1 != count2)
7226         goto done;
7227
7228       symtable1 = (struct elf_symbol *)
7229           bfd_malloc (count1 * sizeof (struct elf_symbol));
7230       symtable2 = (struct elf_symbol *)
7231           bfd_malloc (count2 * sizeof (struct elf_symbol));
7232       if (symtable1 == NULL || symtable2 == NULL)
7233         goto done;
7234
7235       symp = symtable1;
7236       for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7237            ssym < ssymend; ssym++, symp++)
7238         {
7239           symp->u.ssym = ssym;
7240           symp->name = bfd_elf_string_from_elf_section (bfd1,
7241                                                         hdr1->sh_link,
7242                                                         ssym->st_name);
7243         }
7244
7245       symp = symtable2;
7246       for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7247            ssym < ssymend; ssym++, symp++)
7248         {
7249           symp->u.ssym = ssym;
7250           symp->name = bfd_elf_string_from_elf_section (bfd2,
7251                                                         hdr2->sh_link,
7252                                                         ssym->st_name);
7253         }
7254
7255       /* Sort symbol by name.  */
7256       qsort (symtable1, count1, sizeof (struct elf_symbol),
7257              elf_sym_name_compare);
7258       qsort (symtable2, count1, sizeof (struct elf_symbol),
7259              elf_sym_name_compare);
7260
7261       for (i = 0; i < count1; i++)
7262         /* Two symbols must have the same binding, type and name.  */
7263         if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7264             || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7265             || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7266           goto done;
7267
7268       result = TRUE;
7269       goto done;
7270     }
7271
7272   symtable1 = (struct elf_symbol *)
7273       bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7274   symtable2 = (struct elf_symbol *)
7275       bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7276   if (symtable1 == NULL || symtable2 == NULL)
7277     goto done;
7278
7279   /* Count definitions in the section.  */
7280   count1 = 0;
7281   for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7282     if (isym->st_shndx == shndx1)
7283       symtable1[count1++].u.isym = isym;
7284
7285   count2 = 0;
7286   for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7287     if (isym->st_shndx == shndx2)
7288       symtable2[count2++].u.isym = isym;
7289
7290   if (count1 == 0 || count2 == 0 || count1 != count2)
7291     goto done;
7292
7293   for (i = 0; i < count1; i++)
7294     symtable1[i].name
7295       = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7296                                          symtable1[i].u.isym->st_name);
7297
7298   for (i = 0; i < count2; i++)
7299     symtable2[i].name
7300       = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7301                                          symtable2[i].u.isym->st_name);
7302
7303   /* Sort symbol by name.  */
7304   qsort (symtable1, count1, sizeof (struct elf_symbol),
7305          elf_sym_name_compare);
7306   qsort (symtable2, count1, sizeof (struct elf_symbol),
7307          elf_sym_name_compare);
7308
7309   for (i = 0; i < count1; i++)
7310     /* Two symbols must have the same binding, type and name.  */
7311     if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7312         || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7313         || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7314       goto done;
7315
7316   result = TRUE;
7317
7318 done:
7319   if (symtable1)
7320     free (symtable1);
7321   if (symtable2)
7322     free (symtable2);
7323   if (isymbuf1)
7324     free (isymbuf1);
7325   if (isymbuf2)
7326     free (isymbuf2);
7327
7328   return result;
7329 }
7330
7331 /* Return TRUE if 2 section types are compatible.  */
7332
7333 bfd_boolean
7334 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7335                                  bfd *bbfd, const asection *bsec)
7336 {
7337   if (asec == NULL
7338       || bsec == NULL
7339       || abfd->xvec->flavour != bfd_target_elf_flavour
7340       || bbfd->xvec->flavour != bfd_target_elf_flavour)
7341     return TRUE;
7342
7343   return elf_section_type (asec) == elf_section_type (bsec);
7344 }
7345 \f
7346 /* Final phase of ELF linker.  */
7347
7348 /* A structure we use to avoid passing large numbers of arguments.  */
7349
7350 struct elf_final_link_info
7351 {
7352   /* General link information.  */
7353   struct bfd_link_info *info;
7354   /* Output BFD.  */
7355   bfd *output_bfd;
7356   /* Symbol string table.  */
7357   struct bfd_strtab_hash *symstrtab;
7358   /* .dynsym section.  */
7359   asection *dynsym_sec;
7360   /* .hash section.  */
7361   asection *hash_sec;
7362   /* symbol version section (.gnu.version).  */
7363   asection *symver_sec;
7364   /* Buffer large enough to hold contents of any section.  */
7365   bfd_byte *contents;
7366   /* Buffer large enough to hold external relocs of any section.  */
7367   void *external_relocs;
7368   /* Buffer large enough to hold internal relocs of any section.  */
7369   Elf_Internal_Rela *internal_relocs;
7370   /* Buffer large enough to hold external local symbols of any input
7371      BFD.  */
7372   bfd_byte *external_syms;
7373   /* And a buffer for symbol section indices.  */
7374   Elf_External_Sym_Shndx *locsym_shndx;
7375   /* Buffer large enough to hold internal local symbols of any input
7376      BFD.  */
7377   Elf_Internal_Sym *internal_syms;
7378   /* Array large enough to hold a symbol index for each local symbol
7379      of any input BFD.  */
7380   long *indices;
7381   /* Array large enough to hold a section pointer for each local
7382      symbol of any input BFD.  */
7383   asection **sections;
7384   /* Buffer to hold swapped out symbols.  */
7385   bfd_byte *symbuf;
7386   /* And one for symbol section indices.  */
7387   Elf_External_Sym_Shndx *symshndxbuf;
7388   /* Number of swapped out symbols in buffer.  */
7389   size_t symbuf_count;
7390   /* Number of symbols which fit in symbuf.  */
7391   size_t symbuf_size;
7392   /* And same for symshndxbuf.  */
7393   size_t shndxbuf_size;
7394 };
7395
7396 /* This struct is used to pass information to elf_link_output_extsym.  */
7397
7398 struct elf_outext_info
7399 {
7400   bfd_boolean failed;
7401   bfd_boolean localsyms;
7402   struct elf_final_link_info *finfo;
7403 };
7404
7405
7406 /* Support for evaluating a complex relocation.
7407
7408    Complex relocations are generalized, self-describing relocations.  The
7409    implementation of them consists of two parts: complex symbols, and the
7410    relocations themselves.
7411
7412    The relocations are use a reserved elf-wide relocation type code (R_RELC
7413    external / BFD_RELOC_RELC internal) and an encoding of relocation field
7414    information (start bit, end bit, word width, etc) into the addend.  This
7415    information is extracted from CGEN-generated operand tables within gas.
7416
7417    Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7418    internal) representing prefix-notation expressions, including but not
7419    limited to those sorts of expressions normally encoded as addends in the
7420    addend field.  The symbol mangling format is:
7421
7422    <node> := <literal>
7423           |  <unary-operator> ':' <node>
7424           |  <binary-operator> ':' <node> ':' <node>
7425           ;
7426
7427    <literal> := 's' <digits=N> ':' <N character symbol name>
7428              |  'S' <digits=N> ':' <N character section name>
7429              |  '#' <hexdigits>
7430              ;
7431
7432    <binary-operator> := as in C
7433    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
7434
7435 static void
7436 set_symbol_value (bfd *bfd_with_globals,
7437                   Elf_Internal_Sym *isymbuf,
7438                   size_t locsymcount,
7439                   size_t symidx,
7440                   bfd_vma val)
7441 {
7442   struct elf_link_hash_entry **sym_hashes;
7443   struct elf_link_hash_entry *h;
7444   size_t extsymoff = locsymcount;
7445
7446   if (symidx < locsymcount)
7447     {
7448       Elf_Internal_Sym *sym;
7449
7450       sym = isymbuf + symidx;
7451       if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7452         {
7453           /* It is a local symbol: move it to the
7454              "absolute" section and give it a value.  */
7455           sym->st_shndx = SHN_ABS;
7456           sym->st_value = val;
7457           return;
7458         }
7459       BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7460       extsymoff = 0;
7461     }
7462
7463   /* It is a global symbol: set its link type
7464      to "defined" and give it a value.  */
7465
7466   sym_hashes = elf_sym_hashes (bfd_with_globals);
7467   h = sym_hashes [symidx - extsymoff];
7468   while (h->root.type == bfd_link_hash_indirect
7469          || h->root.type == bfd_link_hash_warning)
7470     h = (struct elf_link_hash_entry *) h->root.u.i.link;
7471   h->root.type = bfd_link_hash_defined;
7472   h->root.u.def.value = val;
7473   h->root.u.def.section = bfd_abs_section_ptr;
7474 }
7475
7476 static bfd_boolean
7477 resolve_symbol (const char *name,
7478                 bfd *input_bfd,
7479                 struct elf_final_link_info *finfo,
7480                 bfd_vma *result,
7481                 Elf_Internal_Sym *isymbuf,
7482                 size_t locsymcount)
7483 {
7484   Elf_Internal_Sym *sym;
7485   struct bfd_link_hash_entry *global_entry;
7486   const char *candidate = NULL;
7487   Elf_Internal_Shdr *symtab_hdr;
7488   size_t i;
7489
7490   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7491
7492   for (i = 0; i < locsymcount; ++ i)
7493     {
7494       sym = isymbuf + i;
7495
7496       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7497         continue;
7498
7499       candidate = bfd_elf_string_from_elf_section (input_bfd,
7500                                                    symtab_hdr->sh_link,
7501                                                    sym->st_name);
7502 #ifdef DEBUG
7503       printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7504               name, candidate, (unsigned long) sym->st_value);
7505 #endif
7506       if (candidate && strcmp (candidate, name) == 0)
7507         {
7508           asection *sec = finfo->sections [i];
7509
7510           *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7511           *result += sec->output_offset + sec->output_section->vma;
7512 #ifdef DEBUG
7513           printf ("Found symbol with value %8.8lx\n",
7514                   (unsigned long) *result);
7515 #endif
7516           return TRUE;
7517         }
7518     }
7519
7520   /* Hmm, haven't found it yet. perhaps it is a global.  */
7521   global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7522                                        FALSE, FALSE, TRUE);
7523   if (!global_entry)
7524     return FALSE;
7525
7526   if (global_entry->type == bfd_link_hash_defined
7527       || global_entry->type == bfd_link_hash_defweak)
7528     {
7529       *result = (global_entry->u.def.value
7530                  + global_entry->u.def.section->output_section->vma
7531                  + global_entry->u.def.section->output_offset);
7532 #ifdef DEBUG
7533       printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7534               global_entry->root.string, (unsigned long) *result);
7535 #endif
7536       return TRUE;
7537     }
7538
7539   return FALSE;
7540 }
7541
7542 static bfd_boolean
7543 resolve_section (const char *name,
7544                  asection *sections,
7545                  bfd_vma *result)
7546 {
7547   asection *curr;
7548   unsigned int len;
7549
7550   for (curr = sections; curr; curr = curr->next)
7551     if (strcmp (curr->name, name) == 0)
7552       {
7553         *result = curr->vma;
7554         return TRUE;
7555       }
7556
7557   /* Hmm. still haven't found it. try pseudo-section names.  */
7558   for (curr = sections; curr; curr = curr->next)
7559     {
7560       len = strlen (curr->name);
7561       if (len > strlen (name))
7562         continue;
7563
7564       if (strncmp (curr->name, name, len) == 0)
7565         {
7566           if (strncmp (".end", name + len, 4) == 0)
7567             {
7568               *result = curr->vma + curr->size;
7569               return TRUE;
7570             }
7571
7572           /* Insert more pseudo-section names here, if you like.  */
7573         }
7574     }
7575
7576   return FALSE;
7577 }
7578
7579 static void
7580 undefined_reference (const char *reftype, const char *name)
7581 {
7582   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7583                       reftype, name);
7584 }
7585
7586 static bfd_boolean
7587 eval_symbol (bfd_vma *result,
7588              const char **symp,
7589              bfd *input_bfd,
7590              struct elf_final_link_info *finfo,
7591              bfd_vma dot,
7592              Elf_Internal_Sym *isymbuf,
7593              size_t locsymcount,
7594              int signed_p)
7595 {
7596   size_t len;
7597   size_t symlen;
7598   bfd_vma a;
7599   bfd_vma b;
7600   char symbuf[4096];
7601   const char *sym = *symp;
7602   const char *symend;
7603   bfd_boolean symbol_is_section = FALSE;
7604
7605   len = strlen (sym);
7606   symend = sym + len;
7607
7608   if (len < 1 || len > sizeof (symbuf))
7609     {
7610       bfd_set_error (bfd_error_invalid_operation);
7611       return FALSE;
7612     }
7613
7614   switch (* sym)
7615     {
7616     case '.':
7617       *result = dot;
7618       *symp = sym + 1;
7619       return TRUE;
7620
7621     case '#':
7622       ++sym;
7623       *result = strtoul (sym, (char **) symp, 16);
7624       return TRUE;
7625
7626     case 'S':
7627       symbol_is_section = TRUE;
7628     case 's':
7629       ++sym;
7630       symlen = strtol (sym, (char **) symp, 10);
7631       sym = *symp + 1; /* Skip the trailing ':'.  */
7632
7633       if (symend < sym || symlen + 1 > sizeof (symbuf))
7634         {
7635           bfd_set_error (bfd_error_invalid_operation);
7636           return FALSE;
7637         }
7638
7639       memcpy (symbuf, sym, symlen);
7640       symbuf[symlen] = '\0';
7641       *symp = sym + symlen;
7642
7643       /* Is it always possible, with complex symbols, that gas "mis-guessed"
7644          the symbol as a section, or vice-versa. so we're pretty liberal in our
7645          interpretation here; section means "try section first", not "must be a
7646          section", and likewise with symbol.  */
7647
7648       if (symbol_is_section)
7649         {
7650           if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7651               && !resolve_symbol (symbuf, input_bfd, finfo, result,
7652                                   isymbuf, locsymcount))
7653             {
7654               undefined_reference ("section", symbuf);
7655               return FALSE;
7656             }
7657         }
7658       else
7659         {
7660           if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7661                                isymbuf, locsymcount)
7662               && !resolve_section (symbuf, finfo->output_bfd->sections,
7663                                    result))
7664             {
7665               undefined_reference ("symbol", symbuf);
7666               return FALSE;
7667             }
7668         }
7669
7670       return TRUE;
7671
7672       /* All that remains are operators.  */
7673
7674 #define UNARY_OP(op)                                            \
7675   if (strncmp (sym, #op, strlen (#op)) == 0)                    \
7676     {                                                           \
7677       sym += strlen (#op);                                      \
7678       if (*sym == ':')                                          \
7679         ++sym;                                                  \
7680       *symp = sym;                                              \
7681       if (!eval_symbol (&a, symp, input_bfd, finfo, dot,        \
7682                         isymbuf, locsymcount, signed_p))        \
7683         return FALSE;                                           \
7684       if (signed_p)                                             \
7685         *result = op ((bfd_signed_vma) a);                      \
7686       else                                                      \
7687         *result = op a;                                         \
7688       return TRUE;                                              \
7689     }
7690
7691 #define BINARY_OP(op)                                           \
7692   if (strncmp (sym, #op, strlen (#op)) == 0)                    \
7693     {                                                           \
7694       sym += strlen (#op);                                      \
7695       if (*sym == ':')                                          \
7696         ++sym;                                                  \
7697       *symp = sym;                                              \
7698       if (!eval_symbol (&a, symp, input_bfd, finfo, dot,        \
7699                         isymbuf, locsymcount, signed_p))        \
7700         return FALSE;                                           \
7701       ++*symp;                                                  \
7702       if (!eval_symbol (&b, symp, input_bfd, finfo, dot,        \
7703                         isymbuf, locsymcount, signed_p))        \
7704         return FALSE;                                           \
7705       if (signed_p)                                             \
7706         *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7707       else                                                      \
7708         *result = a op b;                                       \
7709       return TRUE;                                              \
7710     }
7711
7712     default:
7713       UNARY_OP  (0-);
7714       BINARY_OP (<<);
7715       BINARY_OP (>>);
7716       BINARY_OP (==);
7717       BINARY_OP (!=);
7718       BINARY_OP (<=);
7719       BINARY_OP (>=);
7720       BINARY_OP (&&);
7721       BINARY_OP (||);
7722       UNARY_OP  (~);
7723       UNARY_OP  (!);
7724       BINARY_OP (*);
7725       BINARY_OP (/);
7726       BINARY_OP (%);
7727       BINARY_OP (^);
7728       BINARY_OP (|);
7729       BINARY_OP (&);
7730       BINARY_OP (+);
7731       BINARY_OP (-);
7732       BINARY_OP (<);
7733       BINARY_OP (>);
7734 #undef UNARY_OP
7735 #undef BINARY_OP
7736       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7737       bfd_set_error (bfd_error_invalid_operation);
7738       return FALSE;
7739     }
7740 }
7741
7742 static void
7743 put_value (bfd_vma size,
7744            unsigned long chunksz,
7745            bfd *input_bfd,
7746            bfd_vma x,
7747            bfd_byte *location)
7748 {
7749   location += (size - chunksz);
7750
7751   for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7752     {
7753       switch (chunksz)
7754         {
7755         default:
7756         case 0:
7757           abort ();
7758         case 1:
7759           bfd_put_8 (input_bfd, x, location);
7760           break;
7761         case 2:
7762           bfd_put_16 (input_bfd, x, location);
7763           break;
7764         case 4:
7765           bfd_put_32 (input_bfd, x, location);
7766           break;
7767         case 8:
7768 #ifdef BFD64
7769           bfd_put_64 (input_bfd, x, location);
7770 #else
7771           abort ();
7772 #endif
7773           break;
7774         }
7775     }
7776 }
7777
7778 static bfd_vma
7779 get_value (bfd_vma size,
7780            unsigned long chunksz,
7781            bfd *input_bfd,
7782            bfd_byte *location)
7783 {
7784   bfd_vma x = 0;
7785
7786   for (; size; size -= chunksz, location += chunksz)
7787     {
7788       switch (chunksz)
7789         {
7790         default:
7791         case 0:
7792           abort ();
7793         case 1:
7794           x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7795           break;
7796         case 2:
7797           x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7798           break;
7799         case 4:
7800           x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7801           break;
7802         case 8:
7803 #ifdef BFD64
7804           x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7805 #else
7806           abort ();
7807 #endif
7808           break;
7809         }
7810     }
7811   return x;
7812 }
7813
7814 static void
7815 decode_complex_addend (unsigned long *start,   /* in bits */
7816                        unsigned long *oplen,   /* in bits */
7817                        unsigned long *len,     /* in bits */
7818                        unsigned long *wordsz,  /* in bytes */
7819                        unsigned long *chunksz, /* in bytes */
7820                        unsigned long *lsb0_p,
7821                        unsigned long *signed_p,
7822                        unsigned long *trunc_p,
7823                        unsigned long encoded)
7824 {
7825   * start     =  encoded        & 0x3F;
7826   * len       = (encoded >>  6) & 0x3F;
7827   * oplen     = (encoded >> 12) & 0x3F;
7828   * wordsz    = (encoded >> 18) & 0xF;
7829   * chunksz   = (encoded >> 22) & 0xF;
7830   * lsb0_p    = (encoded >> 27) & 1;
7831   * signed_p  = (encoded >> 28) & 1;
7832   * trunc_p   = (encoded >> 29) & 1;
7833 }
7834
7835 bfd_reloc_status_type
7836 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7837                                     asection *input_section ATTRIBUTE_UNUSED,
7838                                     bfd_byte *contents,
7839                                     Elf_Internal_Rela *rel,
7840                                     bfd_vma relocation)
7841 {
7842   bfd_vma shift, x, mask;
7843   unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7844   bfd_reloc_status_type r;
7845
7846   /*  Perform this reloc, since it is complex.
7847       (this is not to say that it necessarily refers to a complex
7848       symbol; merely that it is a self-describing CGEN based reloc.
7849       i.e. the addend has the complete reloc information (bit start, end,
7850       word size, etc) encoded within it.).  */
7851
7852   decode_complex_addend (&start, &oplen, &len, &wordsz,
7853                          &chunksz, &lsb0_p, &signed_p,
7854                          &trunc_p, rel->r_addend);
7855
7856   mask = (((1L << (len - 1)) - 1) << 1) | 1;
7857
7858   if (lsb0_p)
7859     shift = (start + 1) - len;
7860   else
7861     shift = (8 * wordsz) - (start + len);
7862
7863   /* FIXME: octets_per_byte.  */
7864   x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7865
7866 #ifdef DEBUG
7867   printf ("Doing complex reloc: "
7868           "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7869           "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7870           "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7871           lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7872           oplen, (unsigned long) x, (unsigned long) mask,
7873           (unsigned long) relocation);
7874 #endif
7875
7876   r = bfd_reloc_ok;
7877   if (! trunc_p)
7878     /* Now do an overflow check.  */
7879     r = bfd_check_overflow ((signed_p
7880                              ? complain_overflow_signed
7881                              : complain_overflow_unsigned),
7882                             len, 0, (8 * wordsz),
7883                             relocation);
7884
7885   /* Do the deed.  */
7886   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7887
7888 #ifdef DEBUG
7889   printf ("           relocation: %8.8lx\n"
7890           "         shifted mask: %8.8lx\n"
7891           " shifted/masked reloc: %8.8lx\n"
7892           "               result: %8.8lx\n",
7893           (unsigned long) relocation, (unsigned long) (mask << shift),
7894           (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
7895 #endif
7896   /* FIXME: octets_per_byte.  */
7897   put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7898   return r;
7899 }
7900
7901 /* When performing a relocatable link, the input relocations are
7902    preserved.  But, if they reference global symbols, the indices
7903    referenced must be updated.  Update all the relocations found in
7904    RELDATA.  */
7905
7906 static void
7907 elf_link_adjust_relocs (bfd *abfd,
7908                         struct bfd_elf_section_reloc_data *reldata)
7909 {
7910   unsigned int i;
7911   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7912   bfd_byte *erela;
7913   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7914   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7915   bfd_vma r_type_mask;
7916   int r_sym_shift;
7917   unsigned int count = reldata->count;
7918   struct elf_link_hash_entry **rel_hash = reldata->hashes;
7919
7920   if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
7921     {
7922       swap_in = bed->s->swap_reloc_in;
7923       swap_out = bed->s->swap_reloc_out;
7924     }
7925   else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
7926     {
7927       swap_in = bed->s->swap_reloca_in;
7928       swap_out = bed->s->swap_reloca_out;
7929     }
7930   else
7931     abort ();
7932
7933   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7934     abort ();
7935
7936   if (bed->s->arch_size == 32)
7937     {
7938       r_type_mask = 0xff;
7939       r_sym_shift = 8;
7940     }
7941   else
7942     {
7943       r_type_mask = 0xffffffff;
7944       r_sym_shift = 32;
7945     }
7946
7947   erela = reldata->hdr->contents;
7948   for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
7949     {
7950       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7951       unsigned int j;
7952
7953       if (*rel_hash == NULL)
7954         continue;
7955
7956       BFD_ASSERT ((*rel_hash)->indx >= 0);
7957
7958       (*swap_in) (abfd, erela, irela);
7959       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7960         irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7961                            | (irela[j].r_info & r_type_mask));
7962       (*swap_out) (abfd, irela, erela);
7963     }
7964 }
7965
7966 struct elf_link_sort_rela
7967 {
7968   union {
7969     bfd_vma offset;
7970     bfd_vma sym_mask;
7971   } u;
7972   enum elf_reloc_type_class type;
7973   /* We use this as an array of size int_rels_per_ext_rel.  */
7974   Elf_Internal_Rela rela[1];
7975 };
7976
7977 static int
7978 elf_link_sort_cmp1 (const void *A, const void *B)
7979 {
7980   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
7981   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
7982   int relativea, relativeb;
7983
7984   relativea = a->type == reloc_class_relative;
7985   relativeb = b->type == reloc_class_relative;
7986
7987   if (relativea < relativeb)
7988     return 1;
7989   if (relativea > relativeb)
7990     return -1;
7991   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7992     return -1;
7993   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7994     return 1;
7995   if (a->rela->r_offset < b->rela->r_offset)
7996     return -1;
7997   if (a->rela->r_offset > b->rela->r_offset)
7998     return 1;
7999   return 0;
8000 }
8001
8002 static int
8003 elf_link_sort_cmp2 (const void *A, const void *B)
8004 {
8005   const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8006   const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8007   int copya, copyb;
8008
8009   if (a->u.offset < b->u.offset)
8010     return -1;
8011   if (a->u.offset > b->u.offset)
8012     return 1;
8013   copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
8014   copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
8015   if (copya < copyb)
8016     return -1;
8017   if (copya > copyb)
8018     return 1;
8019   if (a->rela->r_offset < b->rela->r_offset)
8020     return -1;
8021   if (a->rela->r_offset > b->rela->r_offset)
8022     return 1;
8023   return 0;
8024 }
8025
8026 static size_t
8027 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8028 {
8029   asection *dynamic_relocs;
8030   asection *rela_dyn;
8031   asection *rel_dyn;
8032   bfd_size_type count, size;
8033   size_t i, ret, sort_elt, ext_size;
8034   bfd_byte *sort, *s_non_relative, *p;
8035   struct elf_link_sort_rela *sq;
8036   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8037   int i2e = bed->s->int_rels_per_ext_rel;
8038   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8039   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8040   struct bfd_link_order *lo;
8041   bfd_vma r_sym_mask;
8042   bfd_boolean use_rela;
8043
8044   /* Find a dynamic reloc section.  */
8045   rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8046   rel_dyn  = bfd_get_section_by_name (abfd, ".rel.dyn");
8047   if (rela_dyn != NULL && rela_dyn->size > 0
8048       && rel_dyn != NULL && rel_dyn->size > 0)
8049     {
8050       bfd_boolean use_rela_initialised = FALSE;
8051
8052       /* This is just here to stop gcc from complaining.
8053          It's initialization checking code is not perfect.  */
8054       use_rela = TRUE;
8055
8056       /* Both sections are present.  Examine the sizes
8057          of the indirect sections to help us choose.  */
8058       for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8059         if (lo->type == bfd_indirect_link_order)
8060           {
8061             asection *o = lo->u.indirect.section;
8062
8063             if ((o->size % bed->s->sizeof_rela) == 0)
8064               {
8065                 if ((o->size % bed->s->sizeof_rel) == 0)
8066                   /* Section size is divisible by both rel and rela sizes.
8067                      It is of no help to us.  */
8068                   ;
8069                 else
8070                   {
8071                     /* Section size is only divisible by rela.  */
8072                     if (use_rela_initialised && (use_rela == FALSE))
8073                       {
8074                         _bfd_error_handler
8075                           (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8076                         bfd_set_error (bfd_error_invalid_operation);
8077                         return 0;
8078                       }
8079                     else
8080                       {
8081                         use_rela = TRUE;
8082                         use_rela_initialised = TRUE;
8083                       }
8084                   }
8085               }
8086             else if ((o->size % bed->s->sizeof_rel) == 0)
8087               {
8088                 /* Section size is only divisible by rel.  */
8089                 if (use_rela_initialised && (use_rela == TRUE))
8090                   {
8091                     _bfd_error_handler
8092                       (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8093                     bfd_set_error (bfd_error_invalid_operation);
8094                     return 0;
8095                   }
8096                 else
8097                   {
8098                     use_rela = FALSE;
8099                     use_rela_initialised = TRUE;
8100                   }
8101               }
8102             else
8103               {
8104                 /* The section size is not divisible by either - something is wrong.  */
8105                 _bfd_error_handler
8106                   (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8107                 bfd_set_error (bfd_error_invalid_operation);
8108                 return 0;
8109               }
8110           }
8111
8112       for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8113         if (lo->type == bfd_indirect_link_order)
8114           {
8115             asection *o = lo->u.indirect.section;
8116
8117             if ((o->size % bed->s->sizeof_rela) == 0)
8118               {
8119                 if ((o->size % bed->s->sizeof_rel) == 0)
8120                   /* Section size is divisible by both rel and rela sizes.
8121                      It is of no help to us.  */
8122                   ;
8123                 else
8124                   {
8125                     /* Section size is only divisible by rela.  */
8126                     if (use_rela_initialised && (use_rela == FALSE))
8127                       {
8128                         _bfd_error_handler
8129                           (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8130                         bfd_set_error (bfd_error_invalid_operation);
8131                         return 0;
8132                       }
8133                     else
8134                       {
8135                         use_rela = TRUE;
8136                         use_rela_initialised = TRUE;
8137                       }
8138                   }
8139               }
8140             else if ((o->size % bed->s->sizeof_rel) == 0)
8141               {
8142                 /* Section size is only divisible by rel.  */
8143                 if (use_rela_initialised && (use_rela == TRUE))
8144                   {
8145                     _bfd_error_handler
8146                       (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8147                     bfd_set_error (bfd_error_invalid_operation);
8148                     return 0;
8149                   }
8150                 else
8151                   {
8152                     use_rela = FALSE;
8153                     use_rela_initialised = TRUE;
8154                   }
8155               }
8156             else
8157               {
8158                 /* The section size is not divisible by either - something is wrong.  */
8159                 _bfd_error_handler
8160                   (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8161                 bfd_set_error (bfd_error_invalid_operation);
8162                 return 0;
8163               }
8164           }
8165
8166       if (! use_rela_initialised)
8167         /* Make a guess.  */
8168         use_rela = TRUE;
8169     }
8170   else if (rela_dyn != NULL && rela_dyn->size > 0)
8171     use_rela = TRUE;
8172   else if (rel_dyn != NULL && rel_dyn->size > 0)
8173     use_rela = FALSE;
8174   else
8175     return 0;
8176
8177   if (use_rela)
8178     {
8179       dynamic_relocs = rela_dyn;
8180       ext_size = bed->s->sizeof_rela;
8181       swap_in = bed->s->swap_reloca_in;
8182       swap_out = bed->s->swap_reloca_out;
8183     }
8184   else
8185     {
8186       dynamic_relocs = rel_dyn;
8187       ext_size = bed->s->sizeof_rel;
8188       swap_in = bed->s->swap_reloc_in;
8189       swap_out = bed->s->swap_reloc_out;
8190     }
8191
8192   size = 0;
8193   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8194     if (lo->type == bfd_indirect_link_order)
8195       size += lo->u.indirect.section->size;
8196
8197   if (size != dynamic_relocs->size)
8198     return 0;
8199
8200   sort_elt = (sizeof (struct elf_link_sort_rela)
8201               + (i2e - 1) * sizeof (Elf_Internal_Rela));
8202
8203   count = dynamic_relocs->size / ext_size;
8204   if (count == 0)
8205     return 0;
8206   sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8207
8208   if (sort == NULL)
8209     {
8210       (*info->callbacks->warning)
8211         (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8212       return 0;
8213     }
8214
8215   if (bed->s->arch_size == 32)
8216     r_sym_mask = ~(bfd_vma) 0xff;
8217   else
8218     r_sym_mask = ~(bfd_vma) 0xffffffff;
8219
8220   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8221     if (lo->type == bfd_indirect_link_order)
8222       {
8223         bfd_byte *erel, *erelend;
8224         asection *o = lo->u.indirect.section;
8225
8226         if (o->contents == NULL && o->size != 0)
8227           {
8228             /* This is a reloc section that is being handled as a normal
8229                section.  See bfd_section_from_shdr.  We can't combine
8230                relocs in this case.  */
8231             free (sort);
8232             return 0;
8233           }
8234         erel = o->contents;
8235         erelend = o->contents + o->size;
8236         /* FIXME: octets_per_byte.  */
8237         p = sort + o->output_offset / ext_size * sort_elt;
8238
8239         while (erel < erelend)
8240           {
8241             struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8242
8243             (*swap_in) (abfd, erel, s->rela);
8244             s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8245             s->u.sym_mask = r_sym_mask;
8246             p += sort_elt;
8247             erel += ext_size;
8248           }
8249       }
8250
8251   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8252
8253   for (i = 0, p = sort; i < count; i++, p += sort_elt)
8254     {
8255       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8256       if (s->type != reloc_class_relative)
8257         break;
8258     }
8259   ret = i;
8260   s_non_relative = p;
8261
8262   sq = (struct elf_link_sort_rela *) s_non_relative;
8263   for (; i < count; i++, p += sort_elt)
8264     {
8265       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8266       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8267         sq = sp;
8268       sp->u.offset = sq->rela->r_offset;
8269     }
8270
8271   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8272
8273   for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8274     if (lo->type == bfd_indirect_link_order)
8275       {
8276         bfd_byte *erel, *erelend;
8277         asection *o = lo->u.indirect.section;
8278
8279         erel = o->contents;
8280         erelend = o->contents + o->size;
8281         /* FIXME: octets_per_byte.  */
8282         p = sort + o->output_offset / ext_size * sort_elt;
8283         while (erel < erelend)
8284           {
8285             struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8286             (*swap_out) (abfd, s->rela, erel);
8287             p += sort_elt;
8288             erel += ext_size;
8289           }
8290       }
8291
8292   free (sort);
8293   *psec = dynamic_relocs;
8294   return ret;
8295 }
8296
8297 /* Flush the output symbols to the file.  */
8298
8299 static bfd_boolean
8300 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8301                             const struct elf_backend_data *bed)
8302 {
8303   if (finfo->symbuf_count > 0)
8304     {
8305       Elf_Internal_Shdr *hdr;
8306       file_ptr pos;
8307       bfd_size_type amt;
8308
8309       hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8310       pos = hdr->sh_offset + hdr->sh_size;
8311       amt = finfo->symbuf_count * bed->s->sizeof_sym;
8312       if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8313           || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8314         return FALSE;
8315
8316       hdr->sh_size += amt;
8317       finfo->symbuf_count = 0;
8318     }
8319
8320   return TRUE;
8321 }
8322
8323 /* Add a symbol to the output symbol table.  */
8324
8325 static int
8326 elf_link_output_sym (struct elf_final_link_info *finfo,
8327                      const char *name,
8328                      Elf_Internal_Sym *elfsym,
8329                      asection *input_sec,
8330                      struct elf_link_hash_entry *h)
8331 {
8332   bfd_byte *dest;
8333   Elf_External_Sym_Shndx *destshndx;
8334   int (*output_symbol_hook)
8335     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8336      struct elf_link_hash_entry *);
8337   const struct elf_backend_data *bed;
8338
8339   bed = get_elf_backend_data (finfo->output_bfd);
8340   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8341   if (output_symbol_hook != NULL)
8342     {
8343       int ret = (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h);
8344       if (ret != 1)
8345         return ret;
8346     }
8347
8348   if (name == NULL || *name == '\0')
8349     elfsym->st_name = 0;
8350   else if (input_sec->flags & SEC_EXCLUDE)
8351     elfsym->st_name = 0;
8352   else
8353     {
8354       elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8355                                                             name, TRUE, FALSE);
8356       if (elfsym->st_name == (unsigned long) -1)
8357         return 0;
8358     }
8359
8360   if (finfo->symbuf_count >= finfo->symbuf_size)
8361     {
8362       if (! elf_link_flush_output_syms (finfo, bed))
8363         return 0;
8364     }
8365
8366   dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8367   destshndx = finfo->symshndxbuf;
8368   if (destshndx != NULL)
8369     {
8370       if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8371         {
8372           bfd_size_type amt;
8373
8374           amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8375           destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
8376                                                               amt * 2);
8377           if (destshndx == NULL)
8378             return 0;
8379           finfo->symshndxbuf = destshndx;
8380           memset ((char *) destshndx + amt, 0, amt);
8381           finfo->shndxbuf_size *= 2;
8382         }
8383       destshndx += bfd_get_symcount (finfo->output_bfd);
8384     }
8385
8386   bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8387   finfo->symbuf_count += 1;
8388   bfd_get_symcount (finfo->output_bfd) += 1;
8389
8390   return 1;
8391 }
8392
8393 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
8394
8395 static bfd_boolean
8396 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8397 {
8398   if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8399       && sym->st_shndx < SHN_LORESERVE)
8400     {
8401       /* The gABI doesn't support dynamic symbols in output sections
8402          beyond 64k.  */
8403       (*_bfd_error_handler)
8404         (_("%B: Too many sections: %d (>= %d)"),
8405          abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8406       bfd_set_error (bfd_error_nonrepresentable_section);
8407       return FALSE;
8408     }
8409   return TRUE;
8410 }
8411
8412 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8413    allowing an unsatisfied unversioned symbol in the DSO to match a
8414    versioned symbol that would normally require an explicit version.
8415    We also handle the case that a DSO references a hidden symbol
8416    which may be satisfied by a versioned symbol in another DSO.  */
8417
8418 static bfd_boolean
8419 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8420                                  const struct elf_backend_data *bed,
8421                                  struct elf_link_hash_entry *h)
8422 {
8423   bfd *abfd;
8424   struct elf_link_loaded_list *loaded;
8425
8426   if (!is_elf_hash_table (info->hash))
8427     return FALSE;
8428
8429   switch (h->root.type)
8430     {
8431     default:
8432       abfd = NULL;
8433       break;
8434
8435     case bfd_link_hash_undefined:
8436     case bfd_link_hash_undefweak:
8437       abfd = h->root.u.undef.abfd;
8438       if ((abfd->flags & DYNAMIC) == 0
8439           || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8440         return FALSE;
8441       break;
8442
8443     case bfd_link_hash_defined:
8444     case bfd_link_hash_defweak:
8445       abfd = h->root.u.def.section->owner;
8446       break;
8447
8448     case bfd_link_hash_common:
8449       abfd = h->root.u.c.p->section->owner;
8450       break;
8451     }
8452   BFD_ASSERT (abfd != NULL);
8453
8454   for (loaded = elf_hash_table (info)->loaded;
8455        loaded != NULL;
8456        loaded = loaded->next)
8457     {
8458       bfd *input;
8459       Elf_Internal_Shdr *hdr;
8460       bfd_size_type symcount;
8461       bfd_size_type extsymcount;
8462       bfd_size_type extsymoff;
8463       Elf_Internal_Shdr *versymhdr;
8464       Elf_Internal_Sym *isym;
8465       Elf_Internal_Sym *isymend;
8466       Elf_Internal_Sym *isymbuf;
8467       Elf_External_Versym *ever;
8468       Elf_External_Versym *extversym;
8469
8470       input = loaded->abfd;
8471
8472       /* We check each DSO for a possible hidden versioned definition.  */
8473       if (input == abfd
8474           || (input->flags & DYNAMIC) == 0
8475           || elf_dynversym (input) == 0)
8476         continue;
8477
8478       hdr = &elf_tdata (input)->dynsymtab_hdr;
8479
8480       symcount = hdr->sh_size / bed->s->sizeof_sym;
8481       if (elf_bad_symtab (input))
8482         {
8483           extsymcount = symcount;
8484           extsymoff = 0;
8485         }
8486       else
8487         {
8488           extsymcount = symcount - hdr->sh_info;
8489           extsymoff = hdr->sh_info;
8490         }
8491
8492       if (extsymcount == 0)
8493         continue;
8494
8495       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8496                                       NULL, NULL, NULL);
8497       if (isymbuf == NULL)
8498         return FALSE;
8499
8500       /* Read in any version definitions.  */
8501       versymhdr = &elf_tdata (input)->dynversym_hdr;
8502       extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8503       if (extversym == NULL)
8504         goto error_ret;
8505
8506       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8507           || (bfd_bread (extversym, versymhdr->sh_size, input)
8508               != versymhdr->sh_size))
8509         {
8510           free (extversym);
8511         error_ret:
8512           free (isymbuf);
8513           return FALSE;
8514         }
8515
8516       ever = extversym + extsymoff;
8517       isymend = isymbuf + extsymcount;
8518       for (isym = isymbuf; isym < isymend; isym++, ever++)
8519         {
8520           const char *name;
8521           Elf_Internal_Versym iver;
8522           unsigned short version_index;
8523
8524           if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8525               || isym->st_shndx == SHN_UNDEF)
8526             continue;
8527
8528           name = bfd_elf_string_from_elf_section (input,
8529                                                   hdr->sh_link,
8530                                                   isym->st_name);
8531           if (strcmp (name, h->root.root.string) != 0)
8532             continue;
8533
8534           _bfd_elf_swap_versym_in (input, ever, &iver);
8535
8536           if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8537               && !(h->def_regular
8538                    && h->forced_local))
8539             {
8540               /* If we have a non-hidden versioned sym, then it should
8541                  have provided a definition for the undefined sym unless
8542                  it is defined in a non-shared object and forced local.
8543                */
8544               abort ();
8545             }
8546
8547           version_index = iver.vs_vers & VERSYM_VERSION;
8548           if (version_index == 1 || version_index == 2)
8549             {
8550               /* This is the base or first version.  We can use it.  */
8551               free (extversym);
8552               free (isymbuf);
8553               return TRUE;
8554             }
8555         }
8556
8557       free (extversym);
8558       free (isymbuf);
8559     }
8560
8561   return FALSE;
8562 }
8563
8564 /* Add an external symbol to the symbol table.  This is called from
8565    the hash table traversal routine.  When generating a shared object,
8566    we go through the symbol table twice.  The first time we output
8567    anything that might have been forced to local scope in a version
8568    script.  The second time we output the symbols that are still
8569    global symbols.  */
8570
8571 static bfd_boolean
8572 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
8573 {
8574   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
8575   struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8576   struct elf_final_link_info *finfo = eoinfo->finfo;
8577   bfd_boolean strip;
8578   Elf_Internal_Sym sym;
8579   asection *input_sec;
8580   const struct elf_backend_data *bed;
8581   long indx;
8582   int ret;
8583
8584   if (h->root.type == bfd_link_hash_warning)
8585     {
8586       h = (struct elf_link_hash_entry *) h->root.u.i.link;
8587       if (h->root.type == bfd_link_hash_new)
8588         return TRUE;
8589     }
8590
8591   /* Decide whether to output this symbol in this pass.  */
8592   if (eoinfo->localsyms)
8593     {
8594       if (!h->forced_local)
8595         return TRUE;
8596     }
8597   else
8598     {
8599       if (h->forced_local)
8600         return TRUE;
8601     }
8602
8603   bed = get_elf_backend_data (finfo->output_bfd);
8604
8605   if (h->root.type == bfd_link_hash_undefined)
8606     {
8607       /* If we have an undefined symbol reference here then it must have
8608          come from a shared library that is being linked in.  (Undefined
8609          references in regular files have already been handled unless
8610          they are in unreferenced sections which are removed by garbage
8611          collection).  */
8612       bfd_boolean ignore_undef = FALSE;
8613
8614       /* Some symbols may be special in that the fact that they're
8615          undefined can be safely ignored - let backend determine that.  */
8616       if (bed->elf_backend_ignore_undef_symbol)
8617         ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8618
8619       /* If we are reporting errors for this situation then do so now.  */
8620       if (!ignore_undef
8621           && h->ref_dynamic
8622           && (!h->ref_regular || finfo->info->gc_sections)
8623           && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8624           && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8625         {
8626           if (! (finfo->info->callbacks->undefined_symbol
8627                  (finfo->info, h->root.root.string,
8628                   h->ref_regular ? NULL : h->root.u.undef.abfd,
8629                   NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8630             {
8631               bfd_set_error (bfd_error_bad_value);
8632               eoinfo->failed = TRUE;
8633               return FALSE;
8634             }
8635         }
8636     }
8637
8638   /* We should also warn if a forced local symbol is referenced from
8639      shared libraries.  */
8640   if (! finfo->info->relocatable
8641       && (! finfo->info->shared)
8642       && h->forced_local
8643       && h->ref_dynamic
8644       && !h->dynamic_def
8645       && !h->dynamic_weak
8646       && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8647     {
8648       bfd *def_bfd;
8649       const char *msg;
8650
8651       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8652         msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8653       else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8654         msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8655       else
8656         msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8657       def_bfd = finfo->output_bfd;
8658       if (h->root.u.def.section != bfd_abs_section_ptr)
8659         def_bfd = h->root.u.def.section->owner;
8660       (*_bfd_error_handler) (msg, finfo->output_bfd, def_bfd,
8661                              h->root.root.string);
8662       bfd_set_error (bfd_error_bad_value);
8663       eoinfo->failed = TRUE;
8664       return FALSE;
8665     }
8666
8667   /* We don't want to output symbols that have never been mentioned by
8668      a regular file, or that we have been told to strip.  However, if
8669      h->indx is set to -2, the symbol is used by a reloc and we must
8670      output it.  */
8671   if (h->indx == -2)
8672     strip = FALSE;
8673   else if ((h->def_dynamic
8674             || h->ref_dynamic
8675             || h->root.type == bfd_link_hash_new)
8676            && !h->def_regular
8677            && !h->ref_regular)
8678     strip = TRUE;
8679   else if (finfo->info->strip == strip_all)
8680     strip = TRUE;
8681   else if (finfo->info->strip == strip_some
8682            && bfd_hash_lookup (finfo->info->keep_hash,
8683                                h->root.root.string, FALSE, FALSE) == NULL)
8684     strip = TRUE;
8685   else if (finfo->info->strip_discarded
8686            && (h->root.type == bfd_link_hash_defined
8687                || h->root.type == bfd_link_hash_defweak)
8688            && elf_discarded_section (h->root.u.def.section))
8689     strip = TRUE;
8690   else if ((h->root.type == bfd_link_hash_undefined
8691             || h->root.type == bfd_link_hash_undefweak)
8692            && h->root.u.undef.abfd != NULL
8693            && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
8694     strip = TRUE;
8695   else
8696     strip = FALSE;
8697
8698   /* If we're stripping it, and it's not a dynamic symbol, there's
8699      nothing else to do unless it is a forced local symbol or a
8700      STT_GNU_IFUNC symbol.  */
8701   if (strip
8702       && h->dynindx == -1
8703       && h->type != STT_GNU_IFUNC
8704       && !h->forced_local)
8705     return TRUE;
8706
8707   sym.st_value = 0;
8708   sym.st_size = h->size;
8709   sym.st_other = h->other;
8710   if (h->forced_local)
8711     {
8712       sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8713       /* Turn off visibility on local symbol.  */
8714       sym.st_other &= ~ELF_ST_VISIBILITY (-1);
8715     }
8716   else if (h->unique_global)
8717     sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
8718   else if (h->root.type == bfd_link_hash_undefweak
8719            || h->root.type == bfd_link_hash_defweak)
8720     sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8721   else
8722     sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8723   sym.st_target_internal = h->target_internal;
8724
8725   switch (h->root.type)
8726     {
8727     default:
8728     case bfd_link_hash_new:
8729     case bfd_link_hash_warning:
8730       abort ();
8731       return FALSE;
8732
8733     case bfd_link_hash_undefined:
8734     case bfd_link_hash_undefweak:
8735       input_sec = bfd_und_section_ptr;
8736       sym.st_shndx = SHN_UNDEF;
8737       break;
8738
8739     case bfd_link_hash_defined:
8740     case bfd_link_hash_defweak:
8741       {
8742         input_sec = h->root.u.def.section;
8743         if (input_sec->output_section != NULL)
8744           {
8745             sym.st_shndx =
8746               _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8747                                                  input_sec->output_section);
8748             if (sym.st_shndx == SHN_BAD)
8749               {
8750                 (*_bfd_error_handler)
8751                   (_("%B: could not find output section %A for input section %A"),
8752                    finfo->output_bfd, input_sec->output_section, input_sec);
8753                 bfd_set_error (bfd_error_nonrepresentable_section);
8754                 eoinfo->failed = TRUE;
8755                 return FALSE;
8756               }
8757
8758             /* ELF symbols in relocatable files are section relative,
8759                but in nonrelocatable files they are virtual
8760                addresses.  */
8761             sym.st_value = h->root.u.def.value + input_sec->output_offset;
8762             if (! finfo->info->relocatable)
8763               {
8764                 sym.st_value += input_sec->output_section->vma;
8765                 if (h->type == STT_TLS)
8766                   {
8767                     asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8768                     if (tls_sec != NULL)
8769                       sym.st_value -= tls_sec->vma;
8770                     else
8771                       {
8772                         /* The TLS section may have been garbage collected.  */
8773                         BFD_ASSERT (finfo->info->gc_sections
8774                                     && !input_sec->gc_mark);
8775                       }
8776                   }
8777               }
8778           }
8779         else
8780           {
8781             BFD_ASSERT (input_sec->owner == NULL
8782                         || (input_sec->owner->flags & DYNAMIC) != 0);
8783             sym.st_shndx = SHN_UNDEF;
8784             input_sec = bfd_und_section_ptr;
8785           }
8786       }
8787       break;
8788
8789     case bfd_link_hash_common:
8790       input_sec = h->root.u.c.p->section;
8791       sym.st_shndx = bed->common_section_index (input_sec);
8792       sym.st_value = 1 << h->root.u.c.p->alignment_power;
8793       break;
8794
8795     case bfd_link_hash_indirect:
8796       /* These symbols are created by symbol versioning.  They point
8797          to the decorated version of the name.  For example, if the
8798          symbol foo@@GNU_1.2 is the default, which should be used when
8799          foo is used with no version, then we add an indirect symbol
8800          foo which points to foo@@GNU_1.2.  We ignore these symbols,
8801          since the indirected symbol is already in the hash table.  */
8802       return TRUE;
8803     }
8804
8805   /* Give the processor backend a chance to tweak the symbol value,
8806      and also to finish up anything that needs to be done for this
8807      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
8808      forced local syms when non-shared is due to a historical quirk.
8809      STT_GNU_IFUNC symbol must go through PLT.  */
8810   if ((h->type == STT_GNU_IFUNC
8811        && h->def_regular
8812        && !finfo->info->relocatable)
8813       || ((h->dynindx != -1
8814            || h->forced_local)
8815           && ((finfo->info->shared
8816                && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8817                    || h->root.type != bfd_link_hash_undefweak))
8818               || !h->forced_local)
8819           && elf_hash_table (finfo->info)->dynamic_sections_created))
8820     {
8821       if (! ((*bed->elf_backend_finish_dynamic_symbol)
8822              (finfo->output_bfd, finfo->info, h, &sym)))
8823         {
8824           eoinfo->failed = TRUE;
8825           return FALSE;
8826         }
8827     }
8828
8829   /* If we are marking the symbol as undefined, and there are no
8830      non-weak references to this symbol from a regular object, then
8831      mark the symbol as weak undefined; if there are non-weak
8832      references, mark the symbol as strong.  We can't do this earlier,
8833      because it might not be marked as undefined until the
8834      finish_dynamic_symbol routine gets through with it.  */
8835   if (sym.st_shndx == SHN_UNDEF
8836       && h->ref_regular
8837       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8838           || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8839     {
8840       int bindtype;
8841       unsigned int type = ELF_ST_TYPE (sym.st_info);
8842
8843       /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
8844       if (type == STT_GNU_IFUNC)
8845         type = STT_FUNC;
8846
8847       if (h->ref_regular_nonweak)
8848         bindtype = STB_GLOBAL;
8849       else
8850         bindtype = STB_WEAK;
8851       sym.st_info = ELF_ST_INFO (bindtype, type);
8852     }
8853
8854   /* If this is a symbol defined in a dynamic library, don't use the
8855      symbol size from the dynamic library.  Relinking an executable
8856      against a new library may introduce gratuitous changes in the
8857      executable's symbols if we keep the size.  */
8858   if (sym.st_shndx == SHN_UNDEF
8859       && !h->def_regular
8860       && h->def_dynamic)
8861     sym.st_size = 0;
8862
8863   /* If a non-weak symbol with non-default visibility is not defined
8864      locally, it is a fatal error.  */
8865   if (! finfo->info->relocatable
8866       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8867       && ELF_ST_BIND (sym.st_info) != STB_WEAK
8868       && h->root.type == bfd_link_hash_undefined
8869       && !h->def_regular)
8870     {
8871       const char *msg;
8872
8873       if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
8874         msg = _("%B: protected symbol `%s' isn't defined");
8875       else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
8876         msg = _("%B: internal symbol `%s' isn't defined");
8877       else
8878         msg = _("%B: hidden symbol `%s' isn't defined");
8879       (*_bfd_error_handler) (msg, finfo->output_bfd, h->root.root.string);
8880       bfd_set_error (bfd_error_bad_value);
8881       eoinfo->failed = TRUE;
8882       return FALSE;
8883     }
8884
8885   /* If this symbol should be put in the .dynsym section, then put it
8886      there now.  We already know the symbol index.  We also fill in
8887      the entry in the .hash section.  */
8888   if (h->dynindx != -1
8889       && elf_hash_table (finfo->info)->dynamic_sections_created)
8890     {
8891       bfd_byte *esym;
8892
8893       sym.st_name = h->dynstr_index;
8894       esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8895       if (! check_dynsym (finfo->output_bfd, &sym))
8896         {
8897           eoinfo->failed = TRUE;
8898           return FALSE;
8899         }
8900       bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8901
8902       if (finfo->hash_sec != NULL)
8903         {
8904           size_t hash_entry_size;
8905           bfd_byte *bucketpos;
8906           bfd_vma chain;
8907           size_t bucketcount;
8908           size_t bucket;
8909
8910           bucketcount = elf_hash_table (finfo->info)->bucketcount;
8911           bucket = h->u.elf_hash_value % bucketcount;
8912
8913           hash_entry_size
8914             = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8915           bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8916                        + (bucket + 2) * hash_entry_size);
8917           chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8918           bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8919           bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8920                    ((bfd_byte *) finfo->hash_sec->contents
8921                     + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8922         }
8923
8924       if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8925         {
8926           Elf_Internal_Versym iversym;
8927           Elf_External_Versym *eversym;
8928
8929           if (!h->def_regular)
8930             {
8931               if (h->verinfo.verdef == NULL)
8932                 iversym.vs_vers = 0;
8933               else
8934                 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8935             }
8936           else
8937             {
8938               if (h->verinfo.vertree == NULL)
8939                 iversym.vs_vers = 1;
8940               else
8941                 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8942               if (finfo->info->create_default_symver)
8943                 iversym.vs_vers++;
8944             }
8945
8946           if (h->hidden)
8947             iversym.vs_vers |= VERSYM_HIDDEN;
8948
8949           eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8950           eversym += h->dynindx;
8951           _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8952         }
8953     }
8954
8955   /* If we're stripping it, then it was just a dynamic symbol, and
8956      there's nothing else to do.  */
8957   if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8958     return TRUE;
8959
8960   indx = bfd_get_symcount (finfo->output_bfd);
8961   ret = elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h);
8962   if (ret == 0)
8963     {
8964       eoinfo->failed = TRUE;
8965       return FALSE;
8966     }
8967   else if (ret == 1)
8968     h->indx = indx;
8969   else if (h->indx == -2)
8970     abort();
8971
8972   return TRUE;
8973 }
8974
8975 /* Return TRUE if special handling is done for relocs in SEC against
8976    symbols defined in discarded sections.  */
8977
8978 static bfd_boolean
8979 elf_section_ignore_discarded_relocs (asection *sec)
8980 {
8981   const struct elf_backend_data *bed;
8982
8983   switch (sec->sec_info_type)
8984     {
8985     case ELF_INFO_TYPE_STABS:
8986     case ELF_INFO_TYPE_EH_FRAME:
8987       return TRUE;
8988     default:
8989       break;
8990     }
8991
8992   bed = get_elf_backend_data (sec->owner);
8993   if (bed->elf_backend_ignore_discarded_relocs != NULL
8994       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8995     return TRUE;
8996
8997   return FALSE;
8998 }
8999
9000 /* Return a mask saying how ld should treat relocations in SEC against
9001    symbols defined in discarded sections.  If this function returns
9002    COMPLAIN set, ld will issue a warning message.  If this function
9003    returns PRETEND set, and the discarded section was link-once and the
9004    same size as the kept link-once section, ld will pretend that the
9005    symbol was actually defined in the kept section.  Otherwise ld will
9006    zero the reloc (at least that is the intent, but some cooperation by
9007    the target dependent code is needed, particularly for REL targets).  */
9008
9009 unsigned int
9010 _bfd_elf_default_action_discarded (asection *sec)
9011 {
9012   if (sec->flags & SEC_DEBUGGING)
9013     return PRETEND;
9014
9015   if (strcmp (".eh_frame", sec->name) == 0)
9016     return 0;
9017
9018   if (strcmp (".gcc_except_table", sec->name) == 0)
9019     return 0;
9020
9021   return COMPLAIN | PRETEND;
9022 }
9023
9024 /* Find a match between a section and a member of a section group.  */
9025
9026 static asection *
9027 match_group_member (asection *sec, asection *group,
9028                     struct bfd_link_info *info)
9029 {
9030   asection *first = elf_next_in_group (group);
9031   asection *s = first;
9032
9033   while (s != NULL)
9034     {
9035       if (bfd_elf_match_symbols_in_sections (s, sec, info))
9036         return s;
9037
9038       s = elf_next_in_group (s);
9039       if (s == first)
9040         break;
9041     }
9042
9043   return NULL;
9044 }
9045
9046 /* Check if the kept section of a discarded section SEC can be used
9047    to replace it.  Return the replacement if it is OK.  Otherwise return
9048    NULL.  */
9049
9050 asection *
9051 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9052 {
9053   asection *kept;
9054
9055   kept = sec->kept_section;
9056   if (kept != NULL)
9057     {
9058       if ((kept->flags & SEC_GROUP) != 0)
9059         kept = match_group_member (sec, kept, info);
9060       if (kept != NULL
9061           && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9062               != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9063         kept = NULL;
9064       sec->kept_section = kept;
9065     }
9066   return kept;
9067 }
9068
9069 /* Link an input file into the linker output file.  This function
9070    handles all the sections and relocations of the input file at once.
9071    This is so that we only have to read the local symbols once, and
9072    don't have to keep them in memory.  */
9073
9074 static bfd_boolean
9075 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
9076 {
9077   int (*relocate_section)
9078     (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9079      Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9080   bfd *output_bfd;
9081   Elf_Internal_Shdr *symtab_hdr;
9082   size_t locsymcount;
9083   size_t extsymoff;
9084   Elf_Internal_Sym *isymbuf;
9085   Elf_Internal_Sym *isym;
9086   Elf_Internal_Sym *isymend;
9087   long *pindex;
9088   asection **ppsection;
9089   asection *o;
9090   const struct elf_backend_data *bed;
9091   struct elf_link_hash_entry **sym_hashes;
9092   bfd_size_type address_size;
9093   bfd_vma r_type_mask;
9094   int r_sym_shift;
9095
9096   output_bfd = finfo->output_bfd;
9097   bed = get_elf_backend_data (output_bfd);
9098   relocate_section = bed->elf_backend_relocate_section;
9099
9100   /* If this is a dynamic object, we don't want to do anything here:
9101      we don't want the local symbols, and we don't want the section
9102      contents.  */
9103   if ((input_bfd->flags & DYNAMIC) != 0)
9104     return TRUE;
9105
9106   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9107   if (elf_bad_symtab (input_bfd))
9108     {
9109       locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9110       extsymoff = 0;
9111     }
9112   else
9113     {
9114       locsymcount = symtab_hdr->sh_info;
9115       extsymoff = symtab_hdr->sh_info;
9116     }
9117
9118   /* Read the local symbols.  */
9119   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9120   if (isymbuf == NULL && locsymcount != 0)
9121     {
9122       isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9123                                       finfo->internal_syms,
9124                                       finfo->external_syms,
9125                                       finfo->locsym_shndx);
9126       if (isymbuf == NULL)
9127         return FALSE;
9128     }
9129
9130   /* Find local symbol sections and adjust values of symbols in
9131      SEC_MERGE sections.  Write out those local symbols we know are
9132      going into the output file.  */
9133   isymend = isymbuf + locsymcount;
9134   for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
9135        isym < isymend;
9136        isym++, pindex++, ppsection++)
9137     {
9138       asection *isec;
9139       const char *name;
9140       Elf_Internal_Sym osym;
9141       long indx;
9142       int ret;
9143
9144       *pindex = -1;
9145
9146       if (elf_bad_symtab (input_bfd))
9147         {
9148           if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9149             {
9150               *ppsection = NULL;
9151               continue;
9152             }
9153         }
9154
9155       if (isym->st_shndx == SHN_UNDEF)
9156         isec = bfd_und_section_ptr;
9157       else if (isym->st_shndx == SHN_ABS)
9158         isec = bfd_abs_section_ptr;
9159       else if (isym->st_shndx == SHN_COMMON)
9160         isec = bfd_com_section_ptr;
9161       else
9162         {
9163           isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9164           if (isec == NULL)
9165             {
9166               /* Don't attempt to output symbols with st_shnx in the
9167                  reserved range other than SHN_ABS and SHN_COMMON.  */
9168               *ppsection = NULL;
9169               continue;
9170             }
9171           else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
9172                    && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9173             isym->st_value =
9174               _bfd_merged_section_offset (output_bfd, &isec,
9175                                           elf_section_data (isec)->sec_info,
9176                                           isym->st_value);
9177         }
9178
9179       *ppsection = isec;
9180
9181       /* Don't output the first, undefined, symbol.  */
9182       if (ppsection == finfo->sections)
9183         continue;
9184
9185       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9186         {
9187           /* We never output section symbols.  Instead, we use the
9188              section symbol of the corresponding section in the output
9189              file.  */
9190           continue;
9191         }
9192
9193       /* If we are stripping all symbols, we don't want to output this
9194          one.  */
9195       if (finfo->info->strip == strip_all)
9196         continue;
9197
9198       /* If we are discarding all local symbols, we don't want to
9199          output this one.  If we are generating a relocatable output
9200          file, then some of the local symbols may be required by
9201          relocs; we output them below as we discover that they are
9202          needed.  */
9203       if (finfo->info->discard == discard_all)
9204         continue;
9205
9206       /* If this symbol is defined in a section which we are
9207          discarding, we don't need to keep it.  */
9208       if (isym->st_shndx != SHN_UNDEF
9209           && isym->st_shndx < SHN_LORESERVE
9210           && bfd_section_removed_from_list (output_bfd,
9211                                             isec->output_section))
9212         continue;
9213
9214       /* Get the name of the symbol.  */
9215       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9216                                               isym->st_name);
9217       if (name == NULL)
9218         return FALSE;
9219
9220       /* See if we are discarding symbols with this name.  */
9221       if ((finfo->info->strip == strip_some
9222            && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9223                == NULL))
9224           || (((finfo->info->discard == discard_sec_merge
9225                 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9226                || finfo->info->discard == discard_l)
9227               && bfd_is_local_label_name (input_bfd, name)))
9228         continue;
9229
9230       osym = *isym;
9231
9232       /* Adjust the section index for the output file.  */
9233       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9234                                                          isec->output_section);
9235       if (osym.st_shndx == SHN_BAD)
9236         return FALSE;
9237
9238       /* ELF symbols in relocatable files are section relative, but
9239          in executable files they are virtual addresses.  Note that
9240          this code assumes that all ELF sections have an associated
9241          BFD section with a reasonable value for output_offset; below
9242          we assume that they also have a reasonable value for
9243          output_section.  Any special sections must be set up to meet
9244          these requirements.  */
9245       osym.st_value += isec->output_offset;
9246       if (! finfo->info->relocatable)
9247         {
9248           osym.st_value += isec->output_section->vma;
9249           if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9250             {
9251               /* STT_TLS symbols are relative to PT_TLS segment base.  */
9252               BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9253               osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9254             }
9255         }
9256
9257       indx = bfd_get_symcount (output_bfd);
9258       ret = elf_link_output_sym (finfo, name, &osym, isec, NULL);
9259       if (ret == 0)
9260         return FALSE;
9261       else if (ret == 1)
9262         *pindex = indx;
9263     }
9264
9265   if (bed->s->arch_size == 32)
9266     {
9267       r_type_mask = 0xff;
9268       r_sym_shift = 8;
9269       address_size = 4;
9270     }
9271   else
9272     {
9273       r_type_mask = 0xffffffff;
9274       r_sym_shift = 32;
9275       address_size = 8;
9276     }
9277
9278   /* Relocate the contents of each section.  */
9279   sym_hashes = elf_sym_hashes (input_bfd);
9280   for (o = input_bfd->sections; o != NULL; o = o->next)
9281     {
9282       bfd_byte *contents;
9283
9284       if (! o->linker_mark)
9285         {
9286           /* This section was omitted from the link.  */
9287           continue;
9288         }
9289
9290       if (finfo->info->relocatable
9291           && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9292         {
9293           /* Deal with the group signature symbol.  */
9294           struct bfd_elf_section_data *sec_data = elf_section_data (o);
9295           unsigned long symndx = sec_data->this_hdr.sh_info;
9296           asection *osec = o->output_section;
9297
9298           if (symndx >= locsymcount
9299               || (elf_bad_symtab (input_bfd)
9300                   && finfo->sections[symndx] == NULL))
9301             {
9302               struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9303               while (h->root.type == bfd_link_hash_indirect
9304                      || h->root.type == bfd_link_hash_warning)
9305                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9306               /* Arrange for symbol to be output.  */
9307               h->indx = -2;
9308               elf_section_data (osec)->this_hdr.sh_info = -2;
9309             }
9310           else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9311             {
9312               /* We'll use the output section target_index.  */
9313               asection *sec = finfo->sections[symndx]->output_section;
9314               elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9315             }
9316           else
9317             {
9318               if (finfo->indices[symndx] == -1)
9319                 {
9320                   /* Otherwise output the local symbol now.  */
9321                   Elf_Internal_Sym sym = isymbuf[symndx];
9322                   asection *sec = finfo->sections[symndx]->output_section;
9323                   const char *name;
9324                   long indx;
9325                   int ret;
9326
9327                   name = bfd_elf_string_from_elf_section (input_bfd,
9328                                                           symtab_hdr->sh_link,
9329                                                           sym.st_name);
9330                   if (name == NULL)
9331                     return FALSE;
9332
9333                   sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9334                                                                     sec);
9335                   if (sym.st_shndx == SHN_BAD)
9336                     return FALSE;
9337
9338                   sym.st_value += o->output_offset;
9339
9340                   indx = bfd_get_symcount (output_bfd);
9341                   ret = elf_link_output_sym (finfo, name, &sym, o, NULL);
9342                   if (ret == 0)
9343                     return FALSE;
9344                   else if (ret == 1)
9345                     finfo->indices[symndx] = indx;
9346                   else
9347                     abort ();
9348                 }
9349               elf_section_data (osec)->this_hdr.sh_info
9350                 = finfo->indices[symndx];
9351             }
9352         }
9353
9354       if ((o->flags & SEC_HAS_CONTENTS) == 0
9355           || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9356         continue;
9357
9358       if ((o->flags & SEC_LINKER_CREATED) != 0)
9359         {
9360           /* Section was created by _bfd_elf_link_create_dynamic_sections
9361              or somesuch.  */
9362           continue;
9363         }
9364
9365       /* Get the contents of the section.  They have been cached by a
9366          relaxation routine.  Note that o is a section in an input
9367          file, so the contents field will not have been set by any of
9368          the routines which work on output files.  */
9369       if (elf_section_data (o)->this_hdr.contents != NULL)
9370         contents = elf_section_data (o)->this_hdr.contents;
9371       else
9372         {
9373           contents = finfo->contents;
9374           if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9375             return FALSE;
9376         }
9377
9378       if ((o->flags & SEC_RELOC) != 0)
9379         {
9380           Elf_Internal_Rela *internal_relocs;
9381           Elf_Internal_Rela *rel, *relend;
9382           int action_discarded;
9383           int ret;
9384
9385           /* Get the swapped relocs.  */
9386           internal_relocs
9387             = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9388                                          finfo->internal_relocs, FALSE);
9389           if (internal_relocs == NULL
9390               && o->reloc_count > 0)
9391             return FALSE;
9392
9393           /* We need to reverse-copy input .ctors/.dtors sections if
9394              they are placed in .init_array/.finit_array for output.  */
9395           if (o->size > address_size
9396               && ((strncmp (o->name, ".ctors", 6) == 0
9397                    && strcmp (o->output_section->name,
9398                               ".init_array") == 0)
9399                   || (strncmp (o->name, ".dtors", 6) == 0
9400                       && strcmp (o->output_section->name,
9401                                  ".fini_array") == 0))
9402               && (o->name[6] == 0 || o->name[6] == '.'))
9403             {
9404               if (o->size != o->reloc_count * address_size)
9405                 {
9406                   (*_bfd_error_handler)
9407                     (_("error: %B: size of section %A is not "
9408                        "multiple of address size"),
9409                      input_bfd, o);
9410                   bfd_set_error (bfd_error_on_input);
9411                   return FALSE;
9412                 }
9413               o->flags |= SEC_ELF_REVERSE_COPY;
9414             }
9415
9416           action_discarded = -1;
9417           if (!elf_section_ignore_discarded_relocs (o))
9418             action_discarded = (*bed->action_discarded) (o);
9419
9420           /* Run through the relocs evaluating complex reloc symbols and
9421              looking for relocs against symbols from discarded sections
9422              or section symbols from removed link-once sections.
9423              Complain about relocs against discarded sections.  Zero
9424              relocs against removed link-once sections.  */
9425
9426           rel = internal_relocs;
9427           relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9428           for ( ; rel < relend; rel++)
9429             {
9430               unsigned long r_symndx = rel->r_info >> r_sym_shift;
9431               unsigned int s_type;
9432               asection **ps, *sec;
9433               struct elf_link_hash_entry *h = NULL;
9434               const char *sym_name;
9435
9436               if (r_symndx == STN_UNDEF)
9437                 continue;
9438
9439               if (r_symndx >= locsymcount
9440                   || (elf_bad_symtab (input_bfd)
9441                       && finfo->sections[r_symndx] == NULL))
9442                 {
9443                   h = sym_hashes[r_symndx - extsymoff];
9444
9445                   /* Badly formatted input files can contain relocs that
9446                      reference non-existant symbols.  Check here so that
9447                      we do not seg fault.  */
9448                   if (h == NULL)
9449                     {
9450                       char buffer [32];
9451
9452                       sprintf_vma (buffer, rel->r_info);
9453                       (*_bfd_error_handler)
9454                         (_("error: %B contains a reloc (0x%s) for section %A "
9455                            "that references a non-existent global symbol"),
9456                          input_bfd, o, buffer);
9457                       bfd_set_error (bfd_error_bad_value);
9458                       return FALSE;
9459                     }
9460
9461                   while (h->root.type == bfd_link_hash_indirect
9462                          || h->root.type == bfd_link_hash_warning)
9463                     h = (struct elf_link_hash_entry *) h->root.u.i.link;
9464
9465                   s_type = h->type;
9466
9467                   ps = NULL;
9468                   if (h->root.type == bfd_link_hash_defined
9469                       || h->root.type == bfd_link_hash_defweak)
9470                     ps = &h->root.u.def.section;
9471
9472                   sym_name = h->root.root.string;
9473                 }
9474               else
9475                 {
9476                   Elf_Internal_Sym *sym = isymbuf + r_symndx;
9477
9478                   s_type = ELF_ST_TYPE (sym->st_info);
9479                   ps = &finfo->sections[r_symndx];
9480                   sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9481                                                sym, *ps);
9482                 }
9483
9484               if ((s_type == STT_RELC || s_type == STT_SRELC)
9485                   && !finfo->info->relocatable)
9486                 {
9487                   bfd_vma val;
9488                   bfd_vma dot = (rel->r_offset
9489                                  + o->output_offset + o->output_section->vma);
9490 #ifdef DEBUG
9491                   printf ("Encountered a complex symbol!");
9492                   printf (" (input_bfd %s, section %s, reloc %ld\n",
9493                           input_bfd->filename, o->name,
9494                           (long) (rel - internal_relocs));
9495                   printf (" symbol: idx  %8.8lx, name %s\n",
9496                           r_symndx, sym_name);
9497                   printf (" reloc : info %8.8lx, addr %8.8lx\n",
9498                           (unsigned long) rel->r_info,
9499                           (unsigned long) rel->r_offset);
9500 #endif
9501                   if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9502                                     isymbuf, locsymcount, s_type == STT_SRELC))
9503                     return FALSE;
9504
9505                   /* Symbol evaluated OK.  Update to absolute value.  */
9506                   set_symbol_value (input_bfd, isymbuf, locsymcount,
9507                                     r_symndx, val);
9508                   continue;
9509                 }
9510
9511               if (action_discarded != -1 && ps != NULL)
9512                 {
9513                   /* Complain if the definition comes from a
9514                      discarded section.  */
9515                   if ((sec = *ps) != NULL && elf_discarded_section (sec))
9516                     {
9517                       BFD_ASSERT (r_symndx != STN_UNDEF);
9518                       if (action_discarded & COMPLAIN)
9519                         (*finfo->info->callbacks->einfo)
9520                           (_("%X`%s' referenced in section `%A' of %B: "
9521                              "defined in discarded section `%A' of %B\n"),
9522                            sym_name, o, input_bfd, sec, sec->owner);
9523
9524                       /* Try to do the best we can to support buggy old
9525                          versions of gcc.  Pretend that the symbol is
9526                          really defined in the kept linkonce section.
9527                          FIXME: This is quite broken.  Modifying the
9528                          symbol here means we will be changing all later
9529                          uses of the symbol, not just in this section.  */
9530                       if (action_discarded & PRETEND)
9531                         {
9532                           asection *kept;
9533
9534                           kept = _bfd_elf_check_kept_section (sec,
9535                                                               finfo->info);
9536                           if (kept != NULL)
9537                             {
9538                               *ps = kept;
9539                               continue;
9540                             }
9541                         }
9542                     }
9543                 }
9544             }
9545
9546           /* Relocate the section by invoking a back end routine.
9547
9548              The back end routine is responsible for adjusting the
9549              section contents as necessary, and (if using Rela relocs
9550              and generating a relocatable output file) adjusting the
9551              reloc addend as necessary.
9552
9553              The back end routine does not have to worry about setting
9554              the reloc address or the reloc symbol index.
9555
9556              The back end routine is given a pointer to the swapped in
9557              internal symbols, and can access the hash table entries
9558              for the external symbols via elf_sym_hashes (input_bfd).
9559
9560              When generating relocatable output, the back end routine
9561              must handle STB_LOCAL/STT_SECTION symbols specially.  The
9562              output symbol is going to be a section symbol
9563              corresponding to the output section, which will require
9564              the addend to be adjusted.  */
9565
9566           ret = (*relocate_section) (output_bfd, finfo->info,
9567                                      input_bfd, o, contents,
9568                                      internal_relocs,
9569                                      isymbuf,
9570                                      finfo->sections);
9571           if (!ret)
9572             return FALSE;
9573
9574           if (ret == 2
9575               || finfo->info->relocatable
9576               || finfo->info->emitrelocations)
9577             {
9578               Elf_Internal_Rela *irela;
9579               Elf_Internal_Rela *irelaend, *irelamid;
9580               bfd_vma last_offset;
9581               struct elf_link_hash_entry **rel_hash;
9582               struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
9583               Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
9584               unsigned int next_erel;
9585               bfd_boolean rela_normal;
9586               struct bfd_elf_section_data *esdi, *esdo;
9587
9588               esdi = elf_section_data (o);
9589               esdo = elf_section_data (o->output_section);
9590               rela_normal = FALSE;
9591
9592               /* Adjust the reloc addresses and symbol indices.  */
9593
9594               irela = internal_relocs;
9595               irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9596               rel_hash = esdo->rel.hashes + esdo->rel.count;
9597               /* We start processing the REL relocs, if any.  When we reach
9598                  IRELAMID in the loop, we switch to the RELA relocs.  */
9599               irelamid = irela;
9600               if (esdi->rel.hdr != NULL)
9601                 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
9602                              * bed->s->int_rels_per_ext_rel);
9603               rel_hash_list = rel_hash;
9604               rela_hash_list = NULL;
9605               last_offset = o->output_offset;
9606               if (!finfo->info->relocatable)
9607                 last_offset += o->output_section->vma;
9608               for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9609                 {
9610                   unsigned long r_symndx;
9611                   asection *sec;
9612                   Elf_Internal_Sym sym;
9613
9614                   if (next_erel == bed->s->int_rels_per_ext_rel)
9615                     {
9616                       rel_hash++;
9617                       next_erel = 0;
9618                     }
9619
9620                   if (irela == irelamid)
9621                     {
9622                       rel_hash = esdo->rela.hashes + esdo->rela.count;
9623                       rela_hash_list = rel_hash;
9624                       rela_normal = bed->rela_normal;
9625                     }
9626
9627                   irela->r_offset = _bfd_elf_section_offset (output_bfd,
9628                                                              finfo->info, o,
9629                                                              irela->r_offset);
9630                   if (irela->r_offset >= (bfd_vma) -2)
9631                     {
9632                       /* This is a reloc for a deleted entry or somesuch.
9633                          Turn it into an R_*_NONE reloc, at the same
9634                          offset as the last reloc.  elf_eh_frame.c and
9635                          bfd_elf_discard_info rely on reloc offsets
9636                          being ordered.  */
9637                       irela->r_offset = last_offset;
9638                       irela->r_info = 0;
9639                       irela->r_addend = 0;
9640                       continue;
9641                     }
9642
9643                   irela->r_offset += o->output_offset;
9644
9645                   /* Relocs in an executable have to be virtual addresses.  */
9646                   if (!finfo->info->relocatable)
9647                     irela->r_offset += o->output_section->vma;
9648
9649                   last_offset = irela->r_offset;
9650
9651                   r_symndx = irela->r_info >> r_sym_shift;
9652                   if (r_symndx == STN_UNDEF)
9653                     continue;
9654
9655                   if (r_symndx >= locsymcount
9656                       || (elf_bad_symtab (input_bfd)
9657                           && finfo->sections[r_symndx] == NULL))
9658                     {
9659                       struct elf_link_hash_entry *rh;
9660                       unsigned long indx;
9661
9662                       /* This is a reloc against a global symbol.  We
9663                          have not yet output all the local symbols, so
9664                          we do not know the symbol index of any global
9665                          symbol.  We set the rel_hash entry for this
9666                          reloc to point to the global hash table entry
9667                          for this symbol.  The symbol index is then
9668                          set at the end of bfd_elf_final_link.  */
9669                       indx = r_symndx - extsymoff;
9670                       rh = elf_sym_hashes (input_bfd)[indx];
9671                       while (rh->root.type == bfd_link_hash_indirect
9672                              || rh->root.type == bfd_link_hash_warning)
9673                         rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9674
9675                       /* Setting the index to -2 tells
9676                          elf_link_output_extsym that this symbol is
9677                          used by a reloc.  */
9678                       BFD_ASSERT (rh->indx < 0);
9679                       rh->indx = -2;
9680
9681                       *rel_hash = rh;
9682
9683                       continue;
9684                     }
9685
9686                   /* This is a reloc against a local symbol.  */
9687
9688                   *rel_hash = NULL;
9689                   sym = isymbuf[r_symndx];
9690                   sec = finfo->sections[r_symndx];
9691                   if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9692                     {
9693                       /* I suppose the backend ought to fill in the
9694                          section of any STT_SECTION symbol against a
9695                          processor specific section.  */
9696                       r_symndx = STN_UNDEF;
9697                       if (bfd_is_abs_section (sec))
9698                         ;
9699                       else if (sec == NULL || sec->owner == NULL)
9700                         {
9701                           bfd_set_error (bfd_error_bad_value);
9702                           return FALSE;
9703                         }
9704                       else
9705                         {
9706                           asection *osec = sec->output_section;
9707
9708                           /* If we have discarded a section, the output
9709                              section will be the absolute section.  In
9710                              case of discarded SEC_MERGE sections, use
9711                              the kept section.  relocate_section should
9712                              have already handled discarded linkonce
9713                              sections.  */
9714                           if (bfd_is_abs_section (osec)
9715                               && sec->kept_section != NULL
9716                               && sec->kept_section->output_section != NULL)
9717                             {
9718                               osec = sec->kept_section->output_section;
9719                               irela->r_addend -= osec->vma;
9720                             }
9721
9722                           if (!bfd_is_abs_section (osec))
9723                             {
9724                               r_symndx = osec->target_index;
9725                               if (r_symndx == STN_UNDEF)
9726                                 {
9727                                   struct elf_link_hash_table *htab;
9728                                   asection *oi;
9729
9730                                   htab = elf_hash_table (finfo->info);
9731                                   oi = htab->text_index_section;
9732                                   if ((osec->flags & SEC_READONLY) == 0
9733                                       && htab->data_index_section != NULL)
9734                                     oi = htab->data_index_section;
9735
9736                                   if (oi != NULL)
9737                                     {
9738                                       irela->r_addend += osec->vma - oi->vma;
9739                                       r_symndx = oi->target_index;
9740                                     }
9741                                 }
9742
9743                               BFD_ASSERT (r_symndx != STN_UNDEF);
9744                             }
9745                         }
9746
9747                       /* Adjust the addend according to where the
9748                          section winds up in the output section.  */
9749                       if (rela_normal)
9750                         irela->r_addend += sec->output_offset;
9751                     }
9752                   else
9753                     {
9754                       if (finfo->indices[r_symndx] == -1)
9755                         {
9756                           unsigned long shlink;
9757                           const char *name;
9758                           asection *osec;
9759                           long indx;
9760
9761                           if (finfo->info->strip == strip_all)
9762                             {
9763                               /* You can't do ld -r -s.  */
9764                               bfd_set_error (bfd_error_invalid_operation);
9765                               return FALSE;
9766                             }
9767
9768                           /* This symbol was skipped earlier, but
9769                              since it is needed by a reloc, we
9770                              must output it now.  */
9771                           shlink = symtab_hdr->sh_link;
9772                           name = (bfd_elf_string_from_elf_section
9773                                   (input_bfd, shlink, sym.st_name));
9774                           if (name == NULL)
9775                             return FALSE;
9776
9777                           osec = sec->output_section;
9778                           sym.st_shndx =
9779                             _bfd_elf_section_from_bfd_section (output_bfd,
9780                                                                osec);
9781                           if (sym.st_shndx == SHN_BAD)
9782                             return FALSE;
9783
9784                           sym.st_value += sec->output_offset;
9785                           if (! finfo->info->relocatable)
9786                             {
9787                               sym.st_value += osec->vma;
9788                               if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9789                                 {
9790                                   /* STT_TLS symbols are relative to PT_TLS
9791                                      segment base.  */
9792                                   BFD_ASSERT (elf_hash_table (finfo->info)
9793                                               ->tls_sec != NULL);
9794                                   sym.st_value -= (elf_hash_table (finfo->info)
9795                                                    ->tls_sec->vma);
9796                                 }
9797                             }
9798
9799                           indx = bfd_get_symcount (output_bfd);
9800                           ret = elf_link_output_sym (finfo, name, &sym, sec,
9801                                                      NULL);
9802                           if (ret == 0)
9803                             return FALSE;
9804                           else if (ret == 1)
9805                             finfo->indices[r_symndx] = indx;
9806                           else
9807                             abort ();
9808                         }
9809
9810                       r_symndx = finfo->indices[r_symndx];
9811                     }
9812
9813                   irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9814                                    | (irela->r_info & r_type_mask));
9815                 }
9816
9817               /* Swap out the relocs.  */
9818               input_rel_hdr = esdi->rel.hdr;
9819               if (input_rel_hdr && input_rel_hdr->sh_size != 0)
9820                 {
9821                   if (!bed->elf_backend_emit_relocs (output_bfd, o,
9822                                                      input_rel_hdr,
9823                                                      internal_relocs,
9824                                                      rel_hash_list))
9825                     return FALSE;
9826                   internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9827                                       * bed->s->int_rels_per_ext_rel);
9828                   rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9829                 }
9830
9831               input_rela_hdr = esdi->rela.hdr;
9832               if (input_rela_hdr && input_rela_hdr->sh_size != 0)
9833                 {
9834                   if (!bed->elf_backend_emit_relocs (output_bfd, o,
9835                                                      input_rela_hdr,
9836                                                      internal_relocs,
9837                                                      rela_hash_list))
9838                     return FALSE;
9839                 }
9840             }
9841         }
9842
9843       /* Write out the modified section contents.  */
9844       if (bed->elf_backend_write_section
9845           && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9846                                                 contents))
9847         {
9848           /* Section written out.  */
9849         }
9850       else switch (o->sec_info_type)
9851         {
9852         case ELF_INFO_TYPE_STABS:
9853           if (! (_bfd_write_section_stabs
9854                  (output_bfd,
9855                   &elf_hash_table (finfo->info)->stab_info,
9856                   o, &elf_section_data (o)->sec_info, contents)))
9857             return FALSE;
9858           break;
9859         case ELF_INFO_TYPE_MERGE:
9860           if (! _bfd_write_merged_section (output_bfd, o,
9861                                            elf_section_data (o)->sec_info))
9862             return FALSE;
9863           break;
9864         case ELF_INFO_TYPE_EH_FRAME:
9865           {
9866             if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9867                                                    o, contents))
9868               return FALSE;
9869           }
9870           break;
9871         default:
9872           {
9873             /* FIXME: octets_per_byte.  */
9874             if (! (o->flags & SEC_EXCLUDE))
9875               {
9876                 file_ptr offset = (file_ptr) o->output_offset;
9877                 bfd_size_type todo = o->size;
9878                 if ((o->flags & SEC_ELF_REVERSE_COPY))
9879                   {
9880                     /* Reverse-copy input section to output.  */
9881                     do
9882                       {
9883                         todo -= address_size;
9884                         if (! bfd_set_section_contents (output_bfd,
9885                                                         o->output_section,
9886                                                         contents + todo,
9887                                                         offset,
9888                                                         address_size))
9889                           return FALSE;
9890                         if (todo == 0)
9891                           break;
9892                         offset += address_size;
9893                       }
9894                     while (1);
9895                   }
9896                 else if (! bfd_set_section_contents (output_bfd,
9897                                                      o->output_section,
9898                                                      contents,
9899                                                      offset, todo))
9900                   return FALSE;
9901               }
9902           }
9903           break;
9904         }
9905     }
9906
9907   return TRUE;
9908 }
9909
9910 /* Generate a reloc when linking an ELF file.  This is a reloc
9911    requested by the linker, and does not come from any input file.  This
9912    is used to build constructor and destructor tables when linking
9913    with -Ur.  */
9914
9915 static bfd_boolean
9916 elf_reloc_link_order (bfd *output_bfd,
9917                       struct bfd_link_info *info,
9918                       asection *output_section,
9919                       struct bfd_link_order *link_order)
9920 {
9921   reloc_howto_type *howto;
9922   long indx;
9923   bfd_vma offset;
9924   bfd_vma addend;
9925   struct bfd_elf_section_reloc_data *reldata;
9926   struct elf_link_hash_entry **rel_hash_ptr;
9927   Elf_Internal_Shdr *rel_hdr;
9928   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9929   Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9930   bfd_byte *erel;
9931   unsigned int i;
9932   struct bfd_elf_section_data *esdo = elf_section_data (output_section);
9933
9934   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9935   if (howto == NULL)
9936     {
9937       bfd_set_error (bfd_error_bad_value);
9938       return FALSE;
9939     }
9940
9941   addend = link_order->u.reloc.p->addend;
9942
9943   if (esdo->rel.hdr)
9944     reldata = &esdo->rel;
9945   else if (esdo->rela.hdr)
9946     reldata = &esdo->rela;
9947   else
9948     {
9949       reldata = NULL;
9950       BFD_ASSERT (0);
9951     }
9952
9953   /* Figure out the symbol index.  */
9954   rel_hash_ptr = reldata->hashes + reldata->count;
9955   if (link_order->type == bfd_section_reloc_link_order)
9956     {
9957       indx = link_order->u.reloc.p->u.section->target_index;
9958       BFD_ASSERT (indx != 0);
9959       *rel_hash_ptr = NULL;
9960     }
9961   else
9962     {
9963       struct elf_link_hash_entry *h;
9964
9965       /* Treat a reloc against a defined symbol as though it were
9966          actually against the section.  */
9967       h = ((struct elf_link_hash_entry *)
9968            bfd_wrapped_link_hash_lookup (output_bfd, info,
9969                                          link_order->u.reloc.p->u.name,
9970                                          FALSE, FALSE, TRUE));
9971       if (h != NULL
9972           && (h->root.type == bfd_link_hash_defined
9973               || h->root.type == bfd_link_hash_defweak))
9974         {
9975           asection *section;
9976
9977           section = h->root.u.def.section;
9978           indx = section->output_section->target_index;
9979           *rel_hash_ptr = NULL;
9980           /* It seems that we ought to add the symbol value to the
9981              addend here, but in practice it has already been added
9982              because it was passed to constructor_callback.  */
9983           addend += section->output_section->vma + section->output_offset;
9984         }
9985       else if (h != NULL)
9986         {
9987           /* Setting the index to -2 tells elf_link_output_extsym that
9988              this symbol is used by a reloc.  */
9989           h->indx = -2;
9990           *rel_hash_ptr = h;
9991           indx = 0;
9992         }
9993       else
9994         {
9995           if (! ((*info->callbacks->unattached_reloc)
9996                  (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9997             return FALSE;
9998           indx = 0;
9999         }
10000     }
10001
10002   /* If this is an inplace reloc, we must write the addend into the
10003      object file.  */
10004   if (howto->partial_inplace && addend != 0)
10005     {
10006       bfd_size_type size;
10007       bfd_reloc_status_type rstat;
10008       bfd_byte *buf;
10009       bfd_boolean ok;
10010       const char *sym_name;
10011
10012       size = (bfd_size_type) bfd_get_reloc_size (howto);
10013       buf = (bfd_byte *) bfd_zmalloc (size);
10014       if (buf == NULL)
10015         return FALSE;
10016       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10017       switch (rstat)
10018         {
10019         case bfd_reloc_ok:
10020           break;
10021
10022         default:
10023         case bfd_reloc_outofrange:
10024           abort ();
10025
10026         case bfd_reloc_overflow:
10027           if (link_order->type == bfd_section_reloc_link_order)
10028             sym_name = bfd_section_name (output_bfd,
10029                                          link_order->u.reloc.p->u.section);
10030           else
10031             sym_name = link_order->u.reloc.p->u.name;
10032           if (! ((*info->callbacks->reloc_overflow)
10033                  (info, NULL, sym_name, howto->name, addend, NULL,
10034                   NULL, (bfd_vma) 0)))
10035             {
10036               free (buf);
10037               return FALSE;
10038             }
10039           break;
10040         }
10041       ok = bfd_set_section_contents (output_bfd, output_section, buf,
10042                                      link_order->offset, size);
10043       free (buf);
10044       if (! ok)
10045         return FALSE;
10046     }
10047
10048   /* The address of a reloc is relative to the section in a
10049      relocatable file, and is a virtual address in an executable
10050      file.  */
10051   offset = link_order->offset;
10052   if (! info->relocatable)
10053     offset += output_section->vma;
10054
10055   for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10056     {
10057       irel[i].r_offset = offset;
10058       irel[i].r_info = 0;
10059       irel[i].r_addend = 0;
10060     }
10061   if (bed->s->arch_size == 32)
10062     irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10063   else
10064     irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10065
10066   rel_hdr = reldata->hdr;
10067   erel = rel_hdr->contents;
10068   if (rel_hdr->sh_type == SHT_REL)
10069     {
10070       erel += reldata->count * bed->s->sizeof_rel;
10071       (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10072     }
10073   else
10074     {
10075       irel[0].r_addend = addend;
10076       erel += reldata->count * bed->s->sizeof_rela;
10077       (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10078     }
10079
10080   ++reldata->count;
10081
10082   return TRUE;
10083 }
10084
10085
10086 /* Get the output vma of the section pointed to by the sh_link field.  */
10087
10088 static bfd_vma
10089 elf_get_linked_section_vma (struct bfd_link_order *p)
10090 {
10091   Elf_Internal_Shdr **elf_shdrp;
10092   asection *s;
10093   int elfsec;
10094
10095   s = p->u.indirect.section;
10096   elf_shdrp = elf_elfsections (s->owner);
10097   elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10098   elfsec = elf_shdrp[elfsec]->sh_link;
10099   /* PR 290:
10100      The Intel C compiler generates SHT_IA_64_UNWIND with
10101      SHF_LINK_ORDER.  But it doesn't set the sh_link or
10102      sh_info fields.  Hence we could get the situation
10103      where elfsec is 0.  */
10104   if (elfsec == 0)
10105     {
10106       const struct elf_backend_data *bed
10107         = get_elf_backend_data (s->owner);
10108       if (bed->link_order_error_handler)
10109         bed->link_order_error_handler
10110           (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10111       return 0;
10112     }
10113   else
10114     {
10115       s = elf_shdrp[elfsec]->bfd_section;
10116       return s->output_section->vma + s->output_offset;
10117     }
10118 }
10119
10120
10121 /* Compare two sections based on the locations of the sections they are
10122    linked to.  Used by elf_fixup_link_order.  */
10123
10124 static int
10125 compare_link_order (const void * a, const void * b)
10126 {
10127   bfd_vma apos;
10128   bfd_vma bpos;
10129
10130   apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10131   bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10132   if (apos < bpos)
10133     return -1;
10134   return apos > bpos;
10135 }
10136
10137
10138 /* Looks for sections with SHF_LINK_ORDER set.  Rearranges them into the same
10139    order as their linked sections.  Returns false if this could not be done
10140    because an output section includes both ordered and unordered
10141    sections.  Ideally we'd do this in the linker proper.  */
10142
10143 static bfd_boolean
10144 elf_fixup_link_order (bfd *abfd, asection *o)
10145 {
10146   int seen_linkorder;
10147   int seen_other;
10148   int n;
10149   struct bfd_link_order *p;
10150   bfd *sub;
10151   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10152   unsigned elfsec;
10153   struct bfd_link_order **sections;
10154   asection *s, *other_sec, *linkorder_sec;
10155   bfd_vma offset;
10156
10157   other_sec = NULL;
10158   linkorder_sec = NULL;
10159   seen_other = 0;
10160   seen_linkorder = 0;
10161   for (p = o->map_head.link_order; p != NULL; p = p->next)
10162     {
10163       if (p->type == bfd_indirect_link_order)
10164         {
10165           s = p->u.indirect.section;
10166           sub = s->owner;
10167           if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10168               && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10169               && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10170               && elfsec < elf_numsections (sub)
10171               && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10172               && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10173             {
10174               seen_linkorder++;
10175               linkorder_sec = s;
10176             }
10177           else
10178             {
10179               seen_other++;
10180               other_sec = s;
10181             }
10182         }
10183       else
10184         seen_other++;
10185
10186       if (seen_other && seen_linkorder)
10187         {
10188           if (other_sec && linkorder_sec)
10189             (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10190                                    o, linkorder_sec,
10191                                    linkorder_sec->owner, other_sec,
10192                                    other_sec->owner);
10193           else
10194             (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10195                                    o);
10196           bfd_set_error (bfd_error_bad_value);
10197           return FALSE;
10198         }
10199     }
10200
10201   if (!seen_linkorder)
10202     return TRUE;
10203
10204   sections = (struct bfd_link_order **)
10205     bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10206   if (sections == NULL)
10207     return FALSE;
10208   seen_linkorder = 0;
10209
10210   for (p = o->map_head.link_order; p != NULL; p = p->next)
10211     {
10212       sections[seen_linkorder++] = p;
10213     }
10214   /* Sort the input sections in the order of their linked section.  */
10215   qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10216          compare_link_order);
10217
10218   /* Change the offsets of the sections.  */
10219   offset = 0;
10220   for (n = 0; n < seen_linkorder; n++)
10221     {
10222       s = sections[n]->u.indirect.section;
10223       offset &= ~(bfd_vma) 0 << s->alignment_power;
10224       s->output_offset = offset;
10225       sections[n]->offset = offset;
10226       /* FIXME: octets_per_byte.  */
10227       offset += sections[n]->size;
10228     }
10229
10230   free (sections);
10231   return TRUE;
10232 }
10233
10234
10235 /* Do the final step of an ELF link.  */
10236
10237 bfd_boolean
10238 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10239 {
10240   bfd_boolean dynamic;
10241   bfd_boolean emit_relocs;
10242   bfd *dynobj;
10243   struct elf_final_link_info finfo;
10244   asection *o;
10245   struct bfd_link_order *p;
10246   bfd *sub;
10247   bfd_size_type max_contents_size;
10248   bfd_size_type max_external_reloc_size;
10249   bfd_size_type max_internal_reloc_count;
10250   bfd_size_type max_sym_count;
10251   bfd_size_type max_sym_shndx_count;
10252   file_ptr off;
10253   Elf_Internal_Sym elfsym;
10254   unsigned int i;
10255   Elf_Internal_Shdr *symtab_hdr;
10256   Elf_Internal_Shdr *symtab_shndx_hdr;
10257   Elf_Internal_Shdr *symstrtab_hdr;
10258   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10259   struct elf_outext_info eoinfo;
10260   bfd_boolean merged;
10261   size_t relativecount = 0;
10262   asection *reldyn = 0;
10263   bfd_size_type amt;
10264   asection *attr_section = NULL;
10265   bfd_vma attr_size = 0;
10266   const char *std_attrs_section;
10267
10268   if (! is_elf_hash_table (info->hash))
10269     return FALSE;
10270
10271   if (info->shared)
10272     abfd->flags |= DYNAMIC;
10273
10274   dynamic = elf_hash_table (info)->dynamic_sections_created;
10275   dynobj = elf_hash_table (info)->dynobj;
10276
10277   emit_relocs = (info->relocatable
10278                  || info->emitrelocations);
10279
10280   finfo.info = info;
10281   finfo.output_bfd = abfd;
10282   finfo.symstrtab = _bfd_elf_stringtab_init ();
10283   if (finfo.symstrtab == NULL)
10284     return FALSE;
10285
10286   if (! dynamic)
10287     {
10288       finfo.dynsym_sec = NULL;
10289       finfo.hash_sec = NULL;
10290       finfo.symver_sec = NULL;
10291     }
10292   else
10293     {
10294       finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10295       finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
10296       BFD_ASSERT (finfo.dynsym_sec != NULL);
10297       finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10298       /* Note that it is OK if symver_sec is NULL.  */
10299     }
10300
10301   finfo.contents = NULL;
10302   finfo.external_relocs = NULL;
10303   finfo.internal_relocs = NULL;
10304   finfo.external_syms = NULL;
10305   finfo.locsym_shndx = NULL;
10306   finfo.internal_syms = NULL;
10307   finfo.indices = NULL;
10308   finfo.sections = NULL;
10309   finfo.symbuf = NULL;
10310   finfo.symshndxbuf = NULL;
10311   finfo.symbuf_count = 0;
10312   finfo.shndxbuf_size = 0;
10313
10314   /* The object attributes have been merged.  Remove the input
10315      sections from the link, and set the contents of the output
10316      secton.  */
10317   std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10318   for (o = abfd->sections; o != NULL; o = o->next)
10319     {
10320       if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10321           || strcmp (o->name, ".gnu.attributes") == 0)
10322         {
10323           for (p = o->map_head.link_order; p != NULL; p = p->next)
10324             {
10325               asection *input_section;
10326
10327               if (p->type != bfd_indirect_link_order)
10328                 continue;
10329               input_section = p->u.indirect.section;
10330               /* Hack: reset the SEC_HAS_CONTENTS flag so that
10331                  elf_link_input_bfd ignores this section.  */
10332               input_section->flags &= ~SEC_HAS_CONTENTS;
10333             }
10334
10335           attr_size = bfd_elf_obj_attr_size (abfd);
10336           if (attr_size)
10337             {
10338               bfd_set_section_size (abfd, o, attr_size);
10339               attr_section = o;
10340               /* Skip this section later on.  */
10341               o->map_head.link_order = NULL;
10342             }
10343           else
10344             o->flags |= SEC_EXCLUDE;
10345         }
10346     }
10347
10348   /* Count up the number of relocations we will output for each output
10349      section, so that we know the sizes of the reloc sections.  We
10350      also figure out some maximum sizes.  */
10351   max_contents_size = 0;
10352   max_external_reloc_size = 0;
10353   max_internal_reloc_count = 0;
10354   max_sym_count = 0;
10355   max_sym_shndx_count = 0;
10356   merged = FALSE;
10357   for (o = abfd->sections; o != NULL; o = o->next)
10358     {
10359       struct bfd_elf_section_data *esdo = elf_section_data (o);
10360       o->reloc_count = 0;
10361
10362       for (p = o->map_head.link_order; p != NULL; p = p->next)
10363         {
10364           unsigned int reloc_count = 0;
10365           struct bfd_elf_section_data *esdi = NULL;
10366
10367           if (p->type == bfd_section_reloc_link_order
10368               || p->type == bfd_symbol_reloc_link_order)
10369             reloc_count = 1;
10370           else if (p->type == bfd_indirect_link_order)
10371             {
10372               asection *sec;
10373
10374               sec = p->u.indirect.section;
10375               esdi = elf_section_data (sec);
10376
10377               /* Mark all sections which are to be included in the
10378                  link.  This will normally be every section.  We need
10379                  to do this so that we can identify any sections which
10380                  the linker has decided to not include.  */
10381               sec->linker_mark = TRUE;
10382
10383               if (sec->flags & SEC_MERGE)
10384                 merged = TRUE;
10385
10386               if (info->relocatable || info->emitrelocations)
10387                 reloc_count = sec->reloc_count;
10388               else if (bed->elf_backend_count_relocs)
10389                 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10390
10391               if (sec->rawsize > max_contents_size)
10392                 max_contents_size = sec->rawsize;
10393               if (sec->size > max_contents_size)
10394                 max_contents_size = sec->size;
10395
10396               /* We are interested in just local symbols, not all
10397                  symbols.  */
10398               if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10399                   && (sec->owner->flags & DYNAMIC) == 0)
10400                 {
10401                   size_t sym_count;
10402
10403                   if (elf_bad_symtab (sec->owner))
10404                     sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10405                                  / bed->s->sizeof_sym);
10406                   else
10407                     sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10408
10409                   if (sym_count > max_sym_count)
10410                     max_sym_count = sym_count;
10411
10412                   if (sym_count > max_sym_shndx_count
10413                       && elf_symtab_shndx (sec->owner) != 0)
10414                     max_sym_shndx_count = sym_count;
10415
10416                   if ((sec->flags & SEC_RELOC) != 0)
10417                     {
10418                       size_t ext_size = 0;
10419
10420                       if (esdi->rel.hdr != NULL)
10421                         ext_size = esdi->rel.hdr->sh_size;
10422                       if (esdi->rela.hdr != NULL)
10423                         ext_size += esdi->rela.hdr->sh_size;
10424
10425                       if (ext_size > max_external_reloc_size)
10426                         max_external_reloc_size = ext_size;
10427                       if (sec->reloc_count > max_internal_reloc_count)
10428                         max_internal_reloc_count = sec->reloc_count;
10429                     }
10430                 }
10431             }
10432
10433           if (reloc_count == 0)
10434             continue;
10435
10436           o->reloc_count += reloc_count;
10437
10438           if (p->type == bfd_indirect_link_order
10439               && (info->relocatable || info->emitrelocations))
10440             {
10441               if (esdi->rel.hdr)
10442                 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10443               if (esdi->rela.hdr)
10444                 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10445             }
10446           else
10447             {
10448               if (o->use_rela_p)
10449                 esdo->rela.count += reloc_count;
10450               else
10451                 esdo->rel.count += reloc_count;
10452             }
10453         }
10454
10455       if (o->reloc_count > 0)
10456         o->flags |= SEC_RELOC;
10457       else
10458         {
10459           /* Explicitly clear the SEC_RELOC flag.  The linker tends to
10460              set it (this is probably a bug) and if it is set
10461              assign_section_numbers will create a reloc section.  */
10462           o->flags &=~ SEC_RELOC;
10463         }
10464
10465       /* If the SEC_ALLOC flag is not set, force the section VMA to
10466          zero.  This is done in elf_fake_sections as well, but forcing
10467          the VMA to 0 here will ensure that relocs against these
10468          sections are handled correctly.  */
10469       if ((o->flags & SEC_ALLOC) == 0
10470           && ! o->user_set_vma)
10471         o->vma = 0;
10472     }
10473
10474   if (! info->relocatable && merged)
10475     elf_link_hash_traverse (elf_hash_table (info),
10476                             _bfd_elf_link_sec_merge_syms, abfd);
10477
10478   /* Figure out the file positions for everything but the symbol table
10479      and the relocs.  We set symcount to force assign_section_numbers
10480      to create a symbol table.  */
10481   bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10482   BFD_ASSERT (! abfd->output_has_begun);
10483   if (! _bfd_elf_compute_section_file_positions (abfd, info))
10484     goto error_return;
10485
10486   /* Set sizes, and assign file positions for reloc sections.  */
10487   for (o = abfd->sections; o != NULL; o = o->next)
10488     {
10489       struct bfd_elf_section_data *esdo = elf_section_data (o);
10490       if ((o->flags & SEC_RELOC) != 0)
10491         {
10492           if (esdo->rel.hdr
10493               && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
10494             goto error_return;
10495
10496           if (esdo->rela.hdr
10497               && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
10498             goto error_return;
10499         }
10500
10501       /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10502          to count upwards while actually outputting the relocations.  */
10503       esdo->rel.count = 0;
10504       esdo->rela.count = 0;
10505     }
10506
10507   _bfd_elf_assign_file_positions_for_relocs (abfd);
10508
10509   /* We have now assigned file positions for all the sections except
10510      .symtab and .strtab.  We start the .symtab section at the current
10511      file position, and write directly to it.  We build the .strtab
10512      section in memory.  */
10513   bfd_get_symcount (abfd) = 0;
10514   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10515   /* sh_name is set in prep_headers.  */
10516   symtab_hdr->sh_type = SHT_SYMTAB;
10517   /* sh_flags, sh_addr and sh_size all start off zero.  */
10518   symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10519   /* sh_link is set in assign_section_numbers.  */
10520   /* sh_info is set below.  */
10521   /* sh_offset is set just below.  */
10522   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
10523
10524   off = elf_tdata (abfd)->next_file_pos;
10525   off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10526
10527   /* Note that at this point elf_tdata (abfd)->next_file_pos is
10528      incorrect.  We do not yet know the size of the .symtab section.
10529      We correct next_file_pos below, after we do know the size.  */
10530
10531   /* Allocate a buffer to hold swapped out symbols.  This is to avoid
10532      continuously seeking to the right position in the file.  */
10533   if (! info->keep_memory || max_sym_count < 20)
10534     finfo.symbuf_size = 20;
10535   else
10536     finfo.symbuf_size = max_sym_count;
10537   amt = finfo.symbuf_size;
10538   amt *= bed->s->sizeof_sym;
10539   finfo.symbuf = (bfd_byte *) bfd_malloc (amt);
10540   if (finfo.symbuf == NULL)
10541     goto error_return;
10542   if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
10543     {
10544       /* Wild guess at number of output symbols.  realloc'd as needed.  */
10545       amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10546       finfo.shndxbuf_size = amt;
10547       amt *= sizeof (Elf_External_Sym_Shndx);
10548       finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10549       if (finfo.symshndxbuf == NULL)
10550         goto error_return;
10551     }
10552
10553   /* Start writing out the symbol table.  The first symbol is always a
10554      dummy symbol.  */
10555   if (info->strip != strip_all
10556       || emit_relocs)
10557     {
10558       elfsym.st_value = 0;
10559       elfsym.st_size = 0;
10560       elfsym.st_info = 0;
10561       elfsym.st_other = 0;
10562       elfsym.st_shndx = SHN_UNDEF;
10563       elfsym.st_target_internal = 0;
10564       if (elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10565                                NULL) != 1)
10566         goto error_return;
10567     }
10568
10569   /* Output a symbol for each section.  We output these even if we are
10570      discarding local symbols, since they are used for relocs.  These
10571      symbols have no names.  We store the index of each one in the
10572      index field of the section, so that we can find it again when
10573      outputting relocs.  */
10574   if (info->strip != strip_all
10575       || emit_relocs)
10576     {
10577       elfsym.st_size = 0;
10578       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10579       elfsym.st_other = 0;
10580       elfsym.st_value = 0;
10581       elfsym.st_target_internal = 0;
10582       for (i = 1; i < elf_numsections (abfd); i++)
10583         {
10584           o = bfd_section_from_elf_index (abfd, i);
10585           if (o != NULL)
10586             {
10587               o->target_index = bfd_get_symcount (abfd);
10588               elfsym.st_shndx = i;
10589               if (!info->relocatable)
10590                 elfsym.st_value = o->vma;
10591               if (elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL) != 1)
10592                 goto error_return;
10593             }
10594         }
10595     }
10596
10597   /* Allocate some memory to hold information read in from the input
10598      files.  */
10599   if (max_contents_size != 0)
10600     {
10601       finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
10602       if (finfo.contents == NULL)
10603         goto error_return;
10604     }
10605
10606   if (max_external_reloc_size != 0)
10607     {
10608       finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10609       if (finfo.external_relocs == NULL)
10610         goto error_return;
10611     }
10612
10613   if (max_internal_reloc_count != 0)
10614     {
10615       amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10616       amt *= sizeof (Elf_Internal_Rela);
10617       finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
10618       if (finfo.internal_relocs == NULL)
10619         goto error_return;
10620     }
10621
10622   if (max_sym_count != 0)
10623     {
10624       amt = max_sym_count * bed->s->sizeof_sym;
10625       finfo.external_syms = (bfd_byte *) bfd_malloc (amt);
10626       if (finfo.external_syms == NULL)
10627         goto error_return;
10628
10629       amt = max_sym_count * sizeof (Elf_Internal_Sym);
10630       finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
10631       if (finfo.internal_syms == NULL)
10632         goto error_return;
10633
10634       amt = max_sym_count * sizeof (long);
10635       finfo.indices = (long int *) bfd_malloc (amt);
10636       if (finfo.indices == NULL)
10637         goto error_return;
10638
10639       amt = max_sym_count * sizeof (asection *);
10640       finfo.sections = (asection **) bfd_malloc (amt);
10641       if (finfo.sections == NULL)
10642         goto error_return;
10643     }
10644
10645   if (max_sym_shndx_count != 0)
10646     {
10647       amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10648       finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
10649       if (finfo.locsym_shndx == NULL)
10650         goto error_return;
10651     }
10652
10653   if (elf_hash_table (info)->tls_sec)
10654     {
10655       bfd_vma base, end = 0;
10656       asection *sec;
10657
10658       for (sec = elf_hash_table (info)->tls_sec;
10659            sec && (sec->flags & SEC_THREAD_LOCAL);
10660            sec = sec->next)
10661         {
10662           bfd_size_type size = sec->size;
10663
10664           if (size == 0
10665               && (sec->flags & SEC_HAS_CONTENTS) == 0)
10666             {
10667               struct bfd_link_order *ord = sec->map_tail.link_order;
10668
10669               if (ord != NULL)
10670                 size = ord->offset + ord->size;
10671             }
10672           end = sec->vma + size;
10673         }
10674       base = elf_hash_table (info)->tls_sec->vma;
10675       /* Only align end of TLS section if static TLS doesn't have special
10676          alignment requirements.  */
10677       if (bed->static_tls_alignment == 1)
10678         end = align_power (end,
10679                            elf_hash_table (info)->tls_sec->alignment_power);
10680       elf_hash_table (info)->tls_size = end - base;
10681     }
10682
10683   /* Reorder SHF_LINK_ORDER sections.  */
10684   for (o = abfd->sections; o != NULL; o = o->next)
10685     {
10686       if (!elf_fixup_link_order (abfd, o))
10687         return FALSE;
10688     }
10689
10690   /* Since ELF permits relocations to be against local symbols, we
10691      must have the local symbols available when we do the relocations.
10692      Since we would rather only read the local symbols once, and we
10693      would rather not keep them in memory, we handle all the
10694      relocations for a single input file at the same time.
10695
10696      Unfortunately, there is no way to know the total number of local
10697      symbols until we have seen all of them, and the local symbol
10698      indices precede the global symbol indices.  This means that when
10699      we are generating relocatable output, and we see a reloc against
10700      a global symbol, we can not know the symbol index until we have
10701      finished examining all the local symbols to see which ones we are
10702      going to output.  To deal with this, we keep the relocations in
10703      memory, and don't output them until the end of the link.  This is
10704      an unfortunate waste of memory, but I don't see a good way around
10705      it.  Fortunately, it only happens when performing a relocatable
10706      link, which is not the common case.  FIXME: If keep_memory is set
10707      we could write the relocs out and then read them again; I don't
10708      know how bad the memory loss will be.  */
10709
10710   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10711     sub->output_has_begun = FALSE;
10712   for (o = abfd->sections; o != NULL; o = o->next)
10713     {
10714       for (p = o->map_head.link_order; p != NULL; p = p->next)
10715         {
10716           if (p->type == bfd_indirect_link_order
10717               && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10718                   == bfd_target_elf_flavour)
10719               && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10720             {
10721               if (! sub->output_has_begun)
10722                 {
10723                   if (! elf_link_input_bfd (&finfo, sub))
10724                     goto error_return;
10725                   sub->output_has_begun = TRUE;
10726                 }
10727             }
10728           else if (p->type == bfd_section_reloc_link_order
10729                    || p->type == bfd_symbol_reloc_link_order)
10730             {
10731               if (! elf_reloc_link_order (abfd, info, o, p))
10732                 goto error_return;
10733             }
10734           else
10735             {
10736               if (! _bfd_default_link_order (abfd, info, o, p))
10737                 {
10738                   if (p->type == bfd_indirect_link_order
10739                       && (bfd_get_flavour (sub)
10740                           == bfd_target_elf_flavour)
10741                       && (elf_elfheader (sub)->e_ident[EI_CLASS]
10742                           != bed->s->elfclass))
10743                     {
10744                       const char *iclass, *oclass;
10745
10746                       if (bed->s->elfclass == ELFCLASS64)
10747                         {
10748                           iclass = "ELFCLASS32";
10749                           oclass = "ELFCLASS64";
10750                         }
10751                       else
10752                         {
10753                           iclass = "ELFCLASS64";
10754                           oclass = "ELFCLASS32";
10755                         }
10756
10757                       bfd_set_error (bfd_error_wrong_format);
10758                       (*_bfd_error_handler)
10759                         (_("%B: file class %s incompatible with %s"),
10760                          sub, iclass, oclass);
10761                     }
10762
10763                   goto error_return;
10764                 }
10765             }
10766         }
10767     }
10768
10769   /* Free symbol buffer if needed.  */
10770   if (!info->reduce_memory_overheads)
10771     {
10772       for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10773         if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10774             && elf_tdata (sub)->symbuf)
10775           {
10776             free (elf_tdata (sub)->symbuf);
10777             elf_tdata (sub)->symbuf = NULL;
10778           }
10779     }
10780
10781   /* Output any global symbols that got converted to local in a
10782      version script or due to symbol visibility.  We do this in a
10783      separate step since ELF requires all local symbols to appear
10784      prior to any global symbols.  FIXME: We should only do this if
10785      some global symbols were, in fact, converted to become local.
10786      FIXME: Will this work correctly with the Irix 5 linker?  */
10787   eoinfo.failed = FALSE;
10788   eoinfo.finfo = &finfo;
10789   eoinfo.localsyms = TRUE;
10790   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
10791   if (eoinfo.failed)
10792     return FALSE;
10793
10794   /* If backend needs to output some local symbols not present in the hash
10795      table, do it now.  */
10796   if (bed->elf_backend_output_arch_local_syms)
10797     {
10798       typedef int (*out_sym_func)
10799         (void *, const char *, Elf_Internal_Sym *, asection *,
10800          struct elf_link_hash_entry *);
10801
10802       if (! ((*bed->elf_backend_output_arch_local_syms)
10803              (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10804         return FALSE;
10805     }
10806
10807   /* That wrote out all the local symbols.  Finish up the symbol table
10808      with the global symbols. Even if we want to strip everything we
10809      can, we still need to deal with those global symbols that got
10810      converted to local in a version script.  */
10811
10812   /* The sh_info field records the index of the first non local symbol.  */
10813   symtab_hdr->sh_info = bfd_get_symcount (abfd);
10814
10815   if (dynamic
10816       && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10817     {
10818       Elf_Internal_Sym sym;
10819       bfd_byte *dynsym = finfo.dynsym_sec->contents;
10820       long last_local = 0;
10821
10822       /* Write out the section symbols for the output sections.  */
10823       if (info->shared || elf_hash_table (info)->is_relocatable_executable)
10824         {
10825           asection *s;
10826
10827           sym.st_size = 0;
10828           sym.st_name = 0;
10829           sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10830           sym.st_other = 0;
10831           sym.st_target_internal = 0;
10832
10833           for (s = abfd->sections; s != NULL; s = s->next)
10834             {
10835               int indx;
10836               bfd_byte *dest;
10837               long dynindx;
10838
10839               dynindx = elf_section_data (s)->dynindx;
10840               if (dynindx <= 0)
10841                 continue;
10842               indx = elf_section_data (s)->this_idx;
10843               BFD_ASSERT (indx > 0);
10844               sym.st_shndx = indx;
10845               if (! check_dynsym (abfd, &sym))
10846                 return FALSE;
10847               sym.st_value = s->vma;
10848               dest = dynsym + dynindx * bed->s->sizeof_sym;
10849               if (last_local < dynindx)
10850                 last_local = dynindx;
10851               bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10852             }
10853         }
10854
10855       /* Write out the local dynsyms.  */
10856       if (elf_hash_table (info)->dynlocal)
10857         {
10858           struct elf_link_local_dynamic_entry *e;
10859           for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10860             {
10861               asection *s;
10862               bfd_byte *dest;
10863
10864               /* Copy the internal symbol and turn off visibility.
10865                  Note that we saved a word of storage and overwrote
10866                  the original st_name with the dynstr_index.  */
10867               sym = e->isym;
10868               sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10869
10870               s = bfd_section_from_elf_index (e->input_bfd,
10871                                               e->isym.st_shndx);
10872               if (s != NULL)
10873                 {
10874                   sym.st_shndx =
10875                     elf_section_data (s->output_section)->this_idx;
10876                   if (! check_dynsym (abfd, &sym))
10877                     return FALSE;
10878                   sym.st_value = (s->output_section->vma
10879                                   + s->output_offset
10880                                   + e->isym.st_value);
10881                 }
10882
10883               if (last_local < e->dynindx)
10884                 last_local = e->dynindx;
10885
10886               dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10887               bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10888             }
10889         }
10890
10891       elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10892         last_local + 1;
10893     }
10894
10895   /* We get the global symbols from the hash table.  */
10896   eoinfo.failed = FALSE;
10897   eoinfo.localsyms = FALSE;
10898   eoinfo.finfo = &finfo;
10899   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
10900   if (eoinfo.failed)
10901     return FALSE;
10902
10903   /* If backend needs to output some symbols not present in the hash
10904      table, do it now.  */
10905   if (bed->elf_backend_output_arch_syms)
10906     {
10907       typedef int (*out_sym_func)
10908         (void *, const char *, Elf_Internal_Sym *, asection *,
10909          struct elf_link_hash_entry *);
10910
10911       if (! ((*bed->elf_backend_output_arch_syms)
10912              (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10913         return FALSE;
10914     }
10915
10916   /* Flush all symbols to the file.  */
10917   if (! elf_link_flush_output_syms (&finfo, bed))
10918     return FALSE;
10919
10920   /* Now we know the size of the symtab section.  */
10921   off += symtab_hdr->sh_size;
10922
10923   symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10924   if (symtab_shndx_hdr->sh_name != 0)
10925     {
10926       symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10927       symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10928       symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10929       amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10930       symtab_shndx_hdr->sh_size = amt;
10931
10932       off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10933                                                        off, TRUE);
10934
10935       if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10936           || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10937         return FALSE;
10938     }
10939
10940
10941   /* Finish up and write out the symbol string table (.strtab)
10942      section.  */
10943   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10944   /* sh_name was set in prep_headers.  */
10945   symstrtab_hdr->sh_type = SHT_STRTAB;
10946   symstrtab_hdr->sh_flags = 0;
10947   symstrtab_hdr->sh_addr = 0;
10948   symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10949   symstrtab_hdr->sh_entsize = 0;
10950   symstrtab_hdr->sh_link = 0;
10951   symstrtab_hdr->sh_info = 0;
10952   /* sh_offset is set just below.  */
10953   symstrtab_hdr->sh_addralign = 1;
10954
10955   off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10956   elf_tdata (abfd)->next_file_pos = off;
10957
10958   if (bfd_get_symcount (abfd) > 0)
10959     {
10960       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10961           || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10962         return FALSE;
10963     }
10964
10965   /* Adjust the relocs to have the correct symbol indices.  */
10966   for (o = abfd->sections; o != NULL; o = o->next)
10967     {
10968       struct bfd_elf_section_data *esdo = elf_section_data (o);
10969       if ((o->flags & SEC_RELOC) == 0)
10970         continue;
10971
10972       if (esdo->rel.hdr != NULL)
10973         elf_link_adjust_relocs (abfd, &esdo->rel);
10974       if (esdo->rela.hdr != NULL)
10975         elf_link_adjust_relocs (abfd, &esdo->rela);
10976
10977       /* Set the reloc_count field to 0 to prevent write_relocs from
10978          trying to swap the relocs out itself.  */
10979       o->reloc_count = 0;
10980     }
10981
10982   if (dynamic && info->combreloc && dynobj != NULL)
10983     relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10984
10985   /* If we are linking against a dynamic object, or generating a
10986      shared library, finish up the dynamic linking information.  */
10987   if (dynamic)
10988     {
10989       bfd_byte *dyncon, *dynconend;
10990
10991       /* Fix up .dynamic entries.  */
10992       o = bfd_get_section_by_name (dynobj, ".dynamic");
10993       BFD_ASSERT (o != NULL);
10994
10995       dyncon = o->contents;
10996       dynconend = o->contents + o->size;
10997       for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10998         {
10999           Elf_Internal_Dyn dyn;
11000           const char *name;
11001           unsigned int type;
11002
11003           bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11004
11005           switch (dyn.d_tag)
11006             {
11007             default:
11008               continue;
11009             case DT_NULL:
11010               if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11011                 {
11012                   switch (elf_section_data (reldyn)->this_hdr.sh_type)
11013                     {
11014                     case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11015                     case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11016                     default: continue;
11017                     }
11018                   dyn.d_un.d_val = relativecount;
11019                   relativecount = 0;
11020                   break;
11021                 }
11022               continue;
11023
11024             case DT_INIT:
11025               name = info->init_function;
11026               goto get_sym;
11027             case DT_FINI:
11028               name = info->fini_function;
11029             get_sym:
11030               {
11031                 struct elf_link_hash_entry *h;
11032
11033                 h = elf_link_hash_lookup (elf_hash_table (info), name,
11034                                           FALSE, FALSE, TRUE);
11035                 if (h != NULL
11036                     && (h->root.type == bfd_link_hash_defined
11037                         || h->root.type == bfd_link_hash_defweak))
11038                   {
11039                     dyn.d_un.d_ptr = h->root.u.def.value;
11040                     o = h->root.u.def.section;
11041                     if (o->output_section != NULL)
11042                       dyn.d_un.d_ptr += (o->output_section->vma
11043                                          + o->output_offset);
11044                     else
11045                       {
11046                         /* The symbol is imported from another shared
11047                            library and does not apply to this one.  */
11048                         dyn.d_un.d_ptr = 0;
11049                       }
11050                     break;
11051                   }
11052               }
11053               continue;
11054
11055             case DT_PREINIT_ARRAYSZ:
11056               name = ".preinit_array";
11057               goto get_size;
11058             case DT_INIT_ARRAYSZ:
11059               name = ".init_array";
11060               goto get_size;
11061             case DT_FINI_ARRAYSZ:
11062               name = ".fini_array";
11063             get_size:
11064               o = bfd_get_section_by_name (abfd, name);
11065               if (o == NULL)
11066                 {
11067                   (*_bfd_error_handler)
11068                     (_("%B: could not find output section %s"), abfd, name);
11069                   goto error_return;
11070                 }
11071               if (o->size == 0)
11072                 (*_bfd_error_handler)
11073                   (_("warning: %s section has zero size"), name);
11074               dyn.d_un.d_val = o->size;
11075               break;
11076
11077             case DT_PREINIT_ARRAY:
11078               name = ".preinit_array";
11079               goto get_vma;
11080             case DT_INIT_ARRAY:
11081               name = ".init_array";
11082               goto get_vma;
11083             case DT_FINI_ARRAY:
11084               name = ".fini_array";
11085               goto get_vma;
11086
11087             case DT_HASH:
11088               name = ".hash";
11089               goto get_vma;
11090             case DT_GNU_HASH:
11091               name = ".gnu.hash";
11092               goto get_vma;
11093             case DT_STRTAB:
11094               name = ".dynstr";
11095               goto get_vma;
11096             case DT_SYMTAB:
11097               name = ".dynsym";
11098               goto get_vma;
11099             case DT_VERDEF:
11100               name = ".gnu.version_d";
11101               goto get_vma;
11102             case DT_VERNEED:
11103               name = ".gnu.version_r";
11104               goto get_vma;
11105             case DT_VERSYM:
11106               name = ".gnu.version";
11107             get_vma:
11108               o = bfd_get_section_by_name (abfd, name);
11109               if (o == NULL)
11110                 {
11111                   (*_bfd_error_handler)
11112                     (_("%B: could not find output section %s"), abfd, name);
11113                   goto error_return;
11114                 }
11115               if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11116                 {
11117                   (*_bfd_error_handler)
11118                     (_("warning: section '%s' is being made into a note"), name);
11119                   bfd_set_error (bfd_error_nonrepresentable_section);
11120                   goto error_return;
11121                 }
11122               dyn.d_un.d_ptr = o->vma;
11123               break;
11124
11125             case DT_REL:
11126             case DT_RELA:
11127             case DT_RELSZ:
11128             case DT_RELASZ:
11129               if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11130                 type = SHT_REL;
11131               else
11132                 type = SHT_RELA;
11133               dyn.d_un.d_val = 0;
11134               dyn.d_un.d_ptr = 0;
11135               for (i = 1; i < elf_numsections (abfd); i++)
11136                 {
11137                   Elf_Internal_Shdr *hdr;
11138
11139                   hdr = elf_elfsections (abfd)[i];
11140                   if (hdr->sh_type == type
11141                       && (hdr->sh_flags & SHF_ALLOC) != 0)
11142                     {
11143                       if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11144                         dyn.d_un.d_val += hdr->sh_size;
11145                       else
11146                         {
11147                           if (dyn.d_un.d_ptr == 0
11148                               || hdr->sh_addr < dyn.d_un.d_ptr)
11149                             dyn.d_un.d_ptr = hdr->sh_addr;
11150                         }
11151                     }
11152                 }
11153               break;
11154             }
11155           bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11156         }
11157     }
11158
11159   /* If we have created any dynamic sections, then output them.  */
11160   if (dynobj != NULL)
11161     {
11162       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11163         goto error_return;
11164
11165       /* Check for DT_TEXTREL (late, in case the backend removes it).  */
11166       if (info->warn_shared_textrel && info->shared)
11167         {
11168           bfd_byte *dyncon, *dynconend;
11169
11170           /* Fix up .dynamic entries.  */
11171           o = bfd_get_section_by_name (dynobj, ".dynamic");
11172           BFD_ASSERT (o != NULL);
11173
11174           dyncon = o->contents;
11175           dynconend = o->contents + o->size;
11176           for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11177             {
11178               Elf_Internal_Dyn dyn;
11179
11180               bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11181
11182               if (dyn.d_tag == DT_TEXTREL)
11183                 {
11184                  info->callbacks->einfo
11185                     (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11186                   break;
11187                 }
11188             }
11189         }
11190
11191       for (o = dynobj->sections; o != NULL; o = o->next)
11192         {
11193           if ((o->flags & SEC_HAS_CONTENTS) == 0
11194               || o->size == 0
11195               || o->output_section == bfd_abs_section_ptr)
11196             continue;
11197           if ((o->flags & SEC_LINKER_CREATED) == 0)
11198             {
11199               /* At this point, we are only interested in sections
11200                  created by _bfd_elf_link_create_dynamic_sections.  */
11201               continue;
11202             }
11203           if (elf_hash_table (info)->stab_info.stabstr == o)
11204             continue;
11205           if (elf_hash_table (info)->eh_info.hdr_sec == o)
11206             continue;
11207           if ((elf_section_data (o->output_section)->this_hdr.sh_type
11208                != SHT_STRTAB)
11209               && (strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0))
11210             {
11211               /* FIXME: octets_per_byte.  */
11212               if (! bfd_set_section_contents (abfd, o->output_section,
11213                                               o->contents,
11214                                               (file_ptr) o->output_offset,
11215                                               o->size))
11216                 goto error_return;
11217             }
11218           else
11219             {
11220               /* The contents of the .dynstr section are actually in a
11221                  stringtab.  */
11222               off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11223               if (bfd_seek (abfd, off, SEEK_SET) != 0
11224                   || ! _bfd_elf_strtab_emit (abfd,
11225                                              elf_hash_table (info)->dynstr))
11226                 goto error_return;
11227             }
11228         }
11229     }
11230
11231   if (info->relocatable)
11232     {
11233       bfd_boolean failed = FALSE;
11234
11235       bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11236       if (failed)
11237         goto error_return;
11238     }
11239
11240   /* If we have optimized stabs strings, output them.  */
11241   if (elf_hash_table (info)->stab_info.stabstr != NULL)
11242     {
11243       if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11244         goto error_return;
11245     }
11246
11247   if (info->eh_frame_hdr)
11248     {
11249       if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11250         goto error_return;
11251     }
11252
11253   if (finfo.symstrtab != NULL)
11254     _bfd_stringtab_free (finfo.symstrtab);
11255   if (finfo.contents != NULL)
11256     free (finfo.contents);
11257   if (finfo.external_relocs != NULL)
11258     free (finfo.external_relocs);
11259   if (finfo.internal_relocs != NULL)
11260     free (finfo.internal_relocs);
11261   if (finfo.external_syms != NULL)
11262     free (finfo.external_syms);
11263   if (finfo.locsym_shndx != NULL)
11264     free (finfo.locsym_shndx);
11265   if (finfo.internal_syms != NULL)
11266     free (finfo.internal_syms);
11267   if (finfo.indices != NULL)
11268     free (finfo.indices);
11269   if (finfo.sections != NULL)
11270     free (finfo.sections);
11271   if (finfo.symbuf != NULL)
11272     free (finfo.symbuf);
11273   if (finfo.symshndxbuf != NULL)
11274     free (finfo.symshndxbuf);
11275   for (o = abfd->sections; o != NULL; o = o->next)
11276     {
11277       struct bfd_elf_section_data *esdo = elf_section_data (o);
11278       if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11279         free (esdo->rel.hashes);
11280       if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11281         free (esdo->rela.hashes);
11282     }
11283
11284   elf_tdata (abfd)->linker = TRUE;
11285
11286   if (attr_section)
11287     {
11288       bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11289       if (contents == NULL)
11290         return FALSE;   /* Bail out and fail.  */
11291       bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11292       bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11293       free (contents);
11294     }
11295
11296   return TRUE;
11297
11298  error_return:
11299   if (finfo.symstrtab != NULL)
11300     _bfd_stringtab_free (finfo.symstrtab);
11301   if (finfo.contents != NULL)
11302     free (finfo.contents);
11303   if (finfo.external_relocs != NULL)
11304     free (finfo.external_relocs);
11305   if (finfo.internal_relocs != NULL)
11306     free (finfo.internal_relocs);
11307   if (finfo.external_syms != NULL)
11308     free (finfo.external_syms);
11309   if (finfo.locsym_shndx != NULL)
11310     free (finfo.locsym_shndx);
11311   if (finfo.internal_syms != NULL)
11312     free (finfo.internal_syms);
11313   if (finfo.indices != NULL)
11314     free (finfo.indices);
11315   if (finfo.sections != NULL)
11316     free (finfo.sections);
11317   if (finfo.symbuf != NULL)
11318     free (finfo.symbuf);
11319   if (finfo.symshndxbuf != NULL)
11320     free (finfo.symshndxbuf);
11321   for (o = abfd->sections; o != NULL; o = o->next)
11322     {
11323       struct bfd_elf_section_data *esdo = elf_section_data (o);
11324       if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11325         free (esdo->rel.hashes);
11326       if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11327         free (esdo->rela.hashes);
11328     }
11329
11330   return FALSE;
11331 }
11332 \f
11333 /* Initialize COOKIE for input bfd ABFD.  */
11334
11335 static bfd_boolean
11336 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11337                    struct bfd_link_info *info, bfd *abfd)
11338 {
11339   Elf_Internal_Shdr *symtab_hdr;
11340   const struct elf_backend_data *bed;
11341
11342   bed = get_elf_backend_data (abfd);
11343   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11344
11345   cookie->abfd = abfd;
11346   cookie->sym_hashes = elf_sym_hashes (abfd);
11347   cookie->bad_symtab = elf_bad_symtab (abfd);
11348   if (cookie->bad_symtab)
11349     {
11350       cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11351       cookie->extsymoff = 0;
11352     }
11353   else
11354     {
11355       cookie->locsymcount = symtab_hdr->sh_info;
11356       cookie->extsymoff = symtab_hdr->sh_info;
11357     }
11358
11359   if (bed->s->arch_size == 32)
11360     cookie->r_sym_shift = 8;
11361   else
11362     cookie->r_sym_shift = 32;
11363
11364   cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11365   if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11366     {
11367       cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11368                                               cookie->locsymcount, 0,
11369                                               NULL, NULL, NULL);
11370       if (cookie->locsyms == NULL)
11371         {
11372           info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11373           return FALSE;
11374         }
11375       if (info->keep_memory)
11376         symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11377     }
11378   return TRUE;
11379 }
11380
11381 /* Free the memory allocated by init_reloc_cookie, if appropriate.  */
11382
11383 static void
11384 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11385 {
11386   Elf_Internal_Shdr *symtab_hdr;
11387
11388   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11389   if (cookie->locsyms != NULL
11390       && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11391     free (cookie->locsyms);
11392 }
11393
11394 /* Initialize the relocation information in COOKIE for input section SEC
11395    of input bfd ABFD.  */
11396
11397 static bfd_boolean
11398 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11399                         struct bfd_link_info *info, bfd *abfd,
11400                         asection *sec)
11401 {
11402   const struct elf_backend_data *bed;
11403
11404   if (sec->reloc_count == 0)
11405     {
11406       cookie->rels = NULL;
11407       cookie->relend = NULL;
11408     }
11409   else
11410     {
11411       bed = get_elf_backend_data (abfd);
11412
11413       cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11414                                                 info->keep_memory);
11415       if (cookie->rels == NULL)
11416         return FALSE;
11417       cookie->rel = cookie->rels;
11418       cookie->relend = (cookie->rels
11419                         + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11420     }
11421   cookie->rel = cookie->rels;
11422   return TRUE;
11423 }
11424
11425 /* Free the memory allocated by init_reloc_cookie_rels,
11426    if appropriate.  */
11427
11428 static void
11429 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11430                         asection *sec)
11431 {
11432   if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11433     free (cookie->rels);
11434 }
11435
11436 /* Initialize the whole of COOKIE for input section SEC.  */
11437
11438 static bfd_boolean
11439 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11440                                struct bfd_link_info *info,
11441                                asection *sec)
11442 {
11443   if (!init_reloc_cookie (cookie, info, sec->owner))
11444     goto error1;
11445   if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11446     goto error2;
11447   return TRUE;
11448
11449  error2:
11450   fini_reloc_cookie (cookie, sec->owner);
11451  error1:
11452   return FALSE;
11453 }
11454
11455 /* Free the memory allocated by init_reloc_cookie_for_section,
11456    if appropriate.  */
11457
11458 static void
11459 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11460                                asection *sec)
11461 {
11462   fini_reloc_cookie_rels (cookie, sec);
11463   fini_reloc_cookie (cookie, sec->owner);
11464 }
11465 \f
11466 /* Garbage collect unused sections.  */
11467
11468 /* Default gc_mark_hook.  */
11469
11470 asection *
11471 _bfd_elf_gc_mark_hook (asection *sec,
11472                        struct bfd_link_info *info ATTRIBUTE_UNUSED,
11473                        Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11474                        struct elf_link_hash_entry *h,
11475                        Elf_Internal_Sym *sym)
11476 {
11477   const char *sec_name;
11478
11479   if (h != NULL)
11480     {
11481       switch (h->root.type)
11482         {
11483         case bfd_link_hash_defined:
11484         case bfd_link_hash_defweak:
11485           return h->root.u.def.section;
11486
11487         case bfd_link_hash_common:
11488           return h->root.u.c.p->section;
11489
11490         case bfd_link_hash_undefined:
11491         case bfd_link_hash_undefweak:
11492           /* To work around a glibc bug, keep all XXX input sections
11493              when there is an as yet undefined reference to __start_XXX
11494              or __stop_XXX symbols.  The linker will later define such
11495              symbols for orphan input sections that have a name
11496              representable as a C identifier.  */
11497           if (strncmp (h->root.root.string, "__start_", 8) == 0)
11498             sec_name = h->root.root.string + 8;
11499           else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11500             sec_name = h->root.root.string + 7;
11501           else
11502             sec_name = NULL;
11503
11504           if (sec_name && *sec_name != '\0')
11505             {
11506               bfd *i;
11507               
11508               for (i = info->input_bfds; i; i = i->link_next)
11509                 {
11510                   sec = bfd_get_section_by_name (i, sec_name);
11511                   if (sec)
11512                     sec->flags |= SEC_KEEP;
11513                 }
11514             }
11515           break;
11516
11517         default:
11518           break;
11519         }
11520     }
11521   else
11522     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11523
11524   return NULL;
11525 }
11526
11527 /* COOKIE->rel describes a relocation against section SEC, which is
11528    a section we've decided to keep.  Return the section that contains
11529    the relocation symbol, or NULL if no section contains it.  */
11530
11531 asection *
11532 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11533                        elf_gc_mark_hook_fn gc_mark_hook,
11534                        struct elf_reloc_cookie *cookie)
11535 {
11536   unsigned long r_symndx;
11537   struct elf_link_hash_entry *h;
11538
11539   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11540   if (r_symndx == STN_UNDEF)
11541     return NULL;
11542
11543   if (r_symndx >= cookie->locsymcount
11544       || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11545     {
11546       h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11547       while (h->root.type == bfd_link_hash_indirect
11548              || h->root.type == bfd_link_hash_warning)
11549         h = (struct elf_link_hash_entry *) h->root.u.i.link;
11550       return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11551     }
11552
11553   return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11554                           &cookie->locsyms[r_symndx]);
11555 }
11556
11557 /* COOKIE->rel describes a relocation against section SEC, which is
11558    a section we've decided to keep.  Mark the section that contains
11559    the relocation symbol.  */
11560
11561 bfd_boolean
11562 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11563                         asection *sec,
11564                         elf_gc_mark_hook_fn gc_mark_hook,
11565                         struct elf_reloc_cookie *cookie)
11566 {
11567   asection *rsec;
11568
11569   rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11570   if (rsec && !rsec->gc_mark)
11571     {
11572       if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11573         rsec->gc_mark = 1;
11574       else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11575         return FALSE;
11576     }
11577   return TRUE;
11578 }
11579
11580 /* The mark phase of garbage collection.  For a given section, mark
11581    it and any sections in this section's group, and all the sections
11582    which define symbols to which it refers.  */
11583
11584 bfd_boolean
11585 _bfd_elf_gc_mark (struct bfd_link_info *info,
11586                   asection *sec,
11587                   elf_gc_mark_hook_fn gc_mark_hook)
11588 {
11589   bfd_boolean ret;
11590   asection *group_sec, *eh_frame;
11591
11592   sec->gc_mark = 1;
11593
11594   /* Mark all the sections in the group.  */
11595   group_sec = elf_section_data (sec)->next_in_group;
11596   if (group_sec && !group_sec->gc_mark)
11597     if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
11598       return FALSE;
11599
11600   /* Look through the section relocs.  */
11601   ret = TRUE;
11602   eh_frame = elf_eh_frame_section (sec->owner);
11603   if ((sec->flags & SEC_RELOC) != 0
11604       && sec->reloc_count > 0
11605       && sec != eh_frame)
11606     {
11607       struct elf_reloc_cookie cookie;
11608
11609       if (!init_reloc_cookie_for_section (&cookie, info, sec))
11610         ret = FALSE;
11611       else
11612         {
11613           for (; cookie.rel < cookie.relend; cookie.rel++)
11614             if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
11615               {
11616                 ret = FALSE;
11617                 break;
11618               }
11619           fini_reloc_cookie_for_section (&cookie, sec);
11620         }
11621     }
11622
11623   if (ret && eh_frame && elf_fde_list (sec))
11624     {
11625       struct elf_reloc_cookie cookie;
11626
11627       if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11628         ret = FALSE;
11629       else
11630         {
11631           if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11632                                       gc_mark_hook, &cookie))
11633             ret = FALSE;
11634           fini_reloc_cookie_for_section (&cookie, eh_frame);
11635         }
11636     }
11637
11638   return ret;
11639 }
11640
11641 /* Keep debug and special sections.  */
11642
11643 bfd_boolean
11644 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
11645                                  elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
11646 {
11647   bfd *ibfd;
11648
11649   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11650     {
11651       asection *isec;
11652       bfd_boolean some_kept;
11653
11654       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
11655         continue;
11656
11657       /* Ensure all linker created sections are kept, and see whether
11658          any other section is already marked.  */
11659       some_kept = FALSE;
11660       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11661         {
11662           if ((isec->flags & SEC_LINKER_CREATED) != 0)
11663             isec->gc_mark = 1;
11664           else if (isec->gc_mark)
11665             some_kept = TRUE;
11666         }
11667
11668       /* If no section in this file will be kept, then we can
11669          toss out debug sections.  */
11670       if (!some_kept)
11671         continue;
11672
11673       /* Keep debug and special sections like .comment when they are
11674          not part of a group.  */
11675       for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11676         if (elf_next_in_group (isec) == NULL
11677             && ((isec->flags & SEC_DEBUGGING) != 0
11678                 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0))
11679           isec->gc_mark = 1;
11680     }
11681   return TRUE;
11682 }
11683
11684 /* Sweep symbols in swept sections.  Called via elf_link_hash_traverse.  */
11685
11686 struct elf_gc_sweep_symbol_info
11687 {
11688   struct bfd_link_info *info;
11689   void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11690                        bfd_boolean);
11691 };
11692
11693 static bfd_boolean
11694 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
11695 {
11696   if ((h->root.type == bfd_link_hash_defined
11697        || h->root.type == bfd_link_hash_defweak)
11698       && !h->root.u.def.section->gc_mark
11699       && !(h->root.u.def.section->owner->flags & DYNAMIC))
11700     {
11701       struct elf_gc_sweep_symbol_info *inf =
11702           (struct elf_gc_sweep_symbol_info *) data;
11703       (*inf->hide_symbol) (inf->info, h, TRUE);
11704     }
11705
11706   return TRUE;
11707 }
11708
11709 /* The sweep phase of garbage collection.  Remove all garbage sections.  */
11710
11711 typedef bfd_boolean (*gc_sweep_hook_fn)
11712   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11713
11714 static bfd_boolean
11715 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
11716 {
11717   bfd *sub;
11718   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11719   gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11720   unsigned long section_sym_count;
11721   struct elf_gc_sweep_symbol_info sweep_info;
11722
11723   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11724     {
11725       asection *o;
11726
11727       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11728         continue;
11729
11730       for (o = sub->sections; o != NULL; o = o->next)
11731         {
11732           /* When any section in a section group is kept, we keep all
11733              sections in the section group.  If the first member of
11734              the section group is excluded, we will also exclude the
11735              group section.  */
11736           if (o->flags & SEC_GROUP)
11737             {
11738               asection *first = elf_next_in_group (o);
11739               o->gc_mark = first->gc_mark;
11740             }
11741
11742           if (o->gc_mark)
11743             continue;
11744
11745           /* Skip sweeping sections already excluded.  */
11746           if (o->flags & SEC_EXCLUDE)
11747             continue;
11748
11749           /* Since this is early in the link process, it is simple
11750              to remove a section from the output.  */
11751           o->flags |= SEC_EXCLUDE;
11752
11753           if (info->print_gc_sections && o->size != 0)
11754             _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11755
11756           /* But we also have to update some of the relocation
11757              info we collected before.  */
11758           if (gc_sweep_hook
11759               && (o->flags & SEC_RELOC) != 0
11760               && o->reloc_count > 0
11761               && !bfd_is_abs_section (o->output_section))
11762             {
11763               Elf_Internal_Rela *internal_relocs;
11764               bfd_boolean r;
11765
11766               internal_relocs
11767                 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11768                                              info->keep_memory);
11769               if (internal_relocs == NULL)
11770                 return FALSE;
11771
11772               r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11773
11774               if (elf_section_data (o)->relocs != internal_relocs)
11775                 free (internal_relocs);
11776
11777               if (!r)
11778                 return FALSE;
11779             }
11780         }
11781     }
11782
11783   /* Remove the symbols that were in the swept sections from the dynamic
11784      symbol table.  GCFIXME: Anyone know how to get them out of the
11785      static symbol table as well?  */
11786   sweep_info.info = info;
11787   sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11788   elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11789                           &sweep_info);
11790
11791   _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
11792   return TRUE;
11793 }
11794
11795 /* Propagate collected vtable information.  This is called through
11796    elf_link_hash_traverse.  */
11797
11798 static bfd_boolean
11799 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11800 {
11801   /* Those that are not vtables.  */
11802   if (h->vtable == NULL || h->vtable->parent == NULL)
11803     return TRUE;
11804
11805   /* Those vtables that do not have parents, we cannot merge.  */
11806   if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
11807     return TRUE;
11808
11809   /* If we've already been done, exit.  */
11810   if (h->vtable->used && h->vtable->used[-1])
11811     return TRUE;
11812
11813   /* Make sure the parent's table is up to date.  */
11814   elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
11815
11816   if (h->vtable->used == NULL)
11817     {
11818       /* None of this table's entries were referenced.  Re-use the
11819          parent's table.  */
11820       h->vtable->used = h->vtable->parent->vtable->used;
11821       h->vtable->size = h->vtable->parent->vtable->size;
11822     }
11823   else
11824     {
11825       size_t n;
11826       bfd_boolean *cu, *pu;
11827
11828       /* Or the parent's entries into ours.  */
11829       cu = h->vtable->used;
11830       cu[-1] = TRUE;
11831       pu = h->vtable->parent->vtable->used;
11832       if (pu != NULL)
11833         {
11834           const struct elf_backend_data *bed;
11835           unsigned int log_file_align;
11836
11837           bed = get_elf_backend_data (h->root.u.def.section->owner);
11838           log_file_align = bed->s->log_file_align;
11839           n = h->vtable->parent->vtable->size >> log_file_align;
11840           while (n--)
11841             {
11842               if (*pu)
11843                 *cu = TRUE;
11844               pu++;
11845               cu++;
11846             }
11847         }
11848     }
11849
11850   return TRUE;
11851 }
11852
11853 static bfd_boolean
11854 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11855 {
11856   asection *sec;
11857   bfd_vma hstart, hend;
11858   Elf_Internal_Rela *relstart, *relend, *rel;
11859   const struct elf_backend_data *bed;
11860   unsigned int log_file_align;
11861
11862   /* Take care of both those symbols that do not describe vtables as
11863      well as those that are not loaded.  */
11864   if (h->vtable == NULL || h->vtable->parent == NULL)
11865     return TRUE;
11866
11867   BFD_ASSERT (h->root.type == bfd_link_hash_defined
11868               || h->root.type == bfd_link_hash_defweak);
11869
11870   sec = h->root.u.def.section;
11871   hstart = h->root.u.def.value;
11872   hend = hstart + h->size;
11873
11874   relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11875   if (!relstart)
11876     return *(bfd_boolean *) okp = FALSE;
11877   bed = get_elf_backend_data (sec->owner);
11878   log_file_align = bed->s->log_file_align;
11879
11880   relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11881
11882   for (rel = relstart; rel < relend; ++rel)
11883     if (rel->r_offset >= hstart && rel->r_offset < hend)
11884       {
11885         /* If the entry is in use, do nothing.  */
11886         if (h->vtable->used
11887             && (rel->r_offset - hstart) < h->vtable->size)
11888           {
11889             bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
11890             if (h->vtable->used[entry])
11891               continue;
11892           }
11893         /* Otherwise, kill it.  */
11894         rel->r_offset = rel->r_info = rel->r_addend = 0;
11895       }
11896
11897   return TRUE;
11898 }
11899
11900 /* Mark sections containing dynamically referenced symbols.  When
11901    building shared libraries, we must assume that any visible symbol is
11902    referenced.  */
11903
11904 bfd_boolean
11905 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
11906 {
11907   struct bfd_link_info *info = (struct bfd_link_info *) inf;
11908
11909   if ((h->root.type == bfd_link_hash_defined
11910        || h->root.type == bfd_link_hash_defweak)
11911       && (h->ref_dynamic
11912           || (!info->executable
11913               && h->def_regular
11914               && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11915               && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
11916     h->root.u.def.section->flags |= SEC_KEEP;
11917
11918   return TRUE;
11919 }
11920
11921 /* Keep all sections containing symbols undefined on the command-line,
11922    and the section containing the entry symbol.  */
11923
11924 void
11925 _bfd_elf_gc_keep (struct bfd_link_info *info)
11926 {
11927   struct bfd_sym_chain *sym;
11928
11929   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11930     {
11931       struct elf_link_hash_entry *h;
11932
11933       h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11934                                 FALSE, FALSE, FALSE);
11935
11936       if (h != NULL
11937           && (h->root.type == bfd_link_hash_defined
11938               || h->root.type == bfd_link_hash_defweak)
11939           && !bfd_is_abs_section (h->root.u.def.section))
11940         h->root.u.def.section->flags |= SEC_KEEP;
11941     }
11942 }
11943
11944 /* Do mark and sweep of unused sections.  */
11945
11946 bfd_boolean
11947 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11948 {
11949   bfd_boolean ok = TRUE;
11950   bfd *sub;
11951   elf_gc_mark_hook_fn gc_mark_hook;
11952   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11953
11954   if (!bed->can_gc_sections
11955       || !is_elf_hash_table (info->hash))
11956     {
11957       (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11958       return TRUE;
11959     }
11960
11961   bed->gc_keep (info);
11962
11963   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
11964      at the .eh_frame section if we can mark the FDEs individually.  */
11965   _bfd_elf_begin_eh_frame_parsing (info);
11966   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11967     {
11968       asection *sec;
11969       struct elf_reloc_cookie cookie;
11970
11971       sec = bfd_get_section_by_name (sub, ".eh_frame");
11972       if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11973         {
11974           _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11975           if (elf_section_data (sec)->sec_info)
11976             elf_eh_frame_section (sub) = sec;
11977           fini_reloc_cookie_for_section (&cookie, sec);
11978         }
11979     }
11980   _bfd_elf_end_eh_frame_parsing (info);
11981
11982   /* Apply transitive closure to the vtable entry usage info.  */
11983   elf_link_hash_traverse (elf_hash_table (info),
11984                           elf_gc_propagate_vtable_entries_used,
11985                           &ok);
11986   if (!ok)
11987     return FALSE;
11988
11989   /* Kill the vtable relocations that were not used.  */
11990   elf_link_hash_traverse (elf_hash_table (info),
11991                           elf_gc_smash_unused_vtentry_relocs,
11992                           &ok);
11993   if (!ok)
11994     return FALSE;
11995
11996   /* Mark dynamically referenced symbols.  */
11997   if (elf_hash_table (info)->dynamic_sections_created)
11998     elf_link_hash_traverse (elf_hash_table (info),
11999                             bed->gc_mark_dynamic_ref,
12000                             info);
12001
12002   /* Grovel through relocs to find out who stays ...  */
12003   gc_mark_hook = bed->gc_mark_hook;
12004   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12005     {
12006       asection *o;
12007
12008       if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
12009         continue;
12010
12011       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12012          Also treat note sections as a root, if the section is not part
12013          of a group.  */
12014       for (o = sub->sections; o != NULL; o = o->next)
12015         if (!o->gc_mark
12016             && (o->flags & SEC_EXCLUDE) == 0
12017             && ((o->flags & SEC_KEEP) != 0
12018                 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12019                     && elf_next_in_group (o) == NULL )))
12020           {
12021             if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12022               return FALSE;
12023           }
12024     }
12025
12026   /* Allow the backend to mark additional target specific sections.  */
12027   bed->gc_mark_extra_sections (info, gc_mark_hook);
12028
12029   /* ... and mark SEC_EXCLUDE for those that go.  */
12030   return elf_gc_sweep (abfd, info);
12031 }
12032 \f
12033 /* Called from check_relocs to record the existence of a VTINHERIT reloc.  */
12034
12035 bfd_boolean
12036 bfd_elf_gc_record_vtinherit (bfd *abfd,
12037                              asection *sec,
12038                              struct elf_link_hash_entry *h,
12039                              bfd_vma offset)
12040 {
12041   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12042   struct elf_link_hash_entry **search, *child;
12043   bfd_size_type extsymcount;
12044   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12045
12046   /* The sh_info field of the symtab header tells us where the
12047      external symbols start.  We don't care about the local symbols at
12048      this point.  */
12049   extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12050   if (!elf_bad_symtab (abfd))
12051     extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12052
12053   sym_hashes = elf_sym_hashes (abfd);
12054   sym_hashes_end = sym_hashes + extsymcount;
12055
12056   /* Hunt down the child symbol, which is in this section at the same
12057      offset as the relocation.  */
12058   for (search = sym_hashes; search != sym_hashes_end; ++search)
12059     {
12060       if ((child = *search) != NULL
12061           && (child->root.type == bfd_link_hash_defined
12062               || child->root.type == bfd_link_hash_defweak)
12063           && child->root.u.def.section == sec
12064           && child->root.u.def.value == offset)
12065         goto win;
12066     }
12067
12068   (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12069                          abfd, sec, (unsigned long) offset);
12070   bfd_set_error (bfd_error_invalid_operation);
12071   return FALSE;
12072
12073  win:
12074   if (!child->vtable)
12075     {
12076       child->vtable = (struct elf_link_virtual_table_entry *)
12077           bfd_zalloc (abfd, sizeof (*child->vtable));
12078       if (!child->vtable)
12079         return FALSE;
12080     }
12081   if (!h)
12082     {
12083       /* This *should* only be the absolute section.  It could potentially
12084          be that someone has defined a non-global vtable though, which
12085          would be bad.  It isn't worth paging in the local symbols to be
12086          sure though; that case should simply be handled by the assembler.  */
12087
12088       child->vtable->parent = (struct elf_link_hash_entry *) -1;
12089     }
12090   else
12091     child->vtable->parent = h;
12092
12093   return TRUE;
12094 }
12095
12096 /* Called from check_relocs to record the existence of a VTENTRY reloc.  */
12097
12098 bfd_boolean
12099 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12100                            asection *sec ATTRIBUTE_UNUSED,
12101                            struct elf_link_hash_entry *h,
12102                            bfd_vma addend)
12103 {
12104   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12105   unsigned int log_file_align = bed->s->log_file_align;
12106
12107   if (!h->vtable)
12108     {
12109       h->vtable = (struct elf_link_virtual_table_entry *)
12110           bfd_zalloc (abfd, sizeof (*h->vtable));
12111       if (!h->vtable)
12112         return FALSE;
12113     }
12114
12115   if (addend >= h->vtable->size)
12116     {
12117       size_t size, bytes, file_align;
12118       bfd_boolean *ptr = h->vtable->used;
12119
12120       /* While the symbol is undefined, we have to be prepared to handle
12121          a zero size.  */
12122       file_align = 1 << log_file_align;
12123       if (h->root.type == bfd_link_hash_undefined)
12124         size = addend + file_align;
12125       else
12126         {
12127           size = h->size;
12128           if (addend >= size)
12129             {
12130               /* Oops!  We've got a reference past the defined end of
12131                  the table.  This is probably a bug -- shall we warn?  */
12132               size = addend + file_align;
12133             }
12134         }
12135       size = (size + file_align - 1) & -file_align;
12136
12137       /* Allocate one extra entry for use as a "done" flag for the
12138          consolidation pass.  */
12139       bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12140
12141       if (ptr)
12142         {
12143           ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12144
12145           if (ptr != NULL)
12146             {
12147               size_t oldbytes;
12148
12149               oldbytes = (((h->vtable->size >> log_file_align) + 1)
12150                           * sizeof (bfd_boolean));
12151               memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12152             }
12153         }
12154       else
12155         ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12156
12157       if (ptr == NULL)
12158         return FALSE;
12159
12160       /* And arrange for that done flag to be at index -1.  */
12161       h->vtable->used = ptr + 1;
12162       h->vtable->size = size;
12163     }
12164
12165   h->vtable->used[addend >> log_file_align] = TRUE;
12166
12167   return TRUE;
12168 }
12169
12170 /* Map an ELF section header flag to its corresponding string.  */
12171 typedef struct
12172 {
12173   char *flag_name;
12174   flagword flag_value;
12175 } elf_flags_to_name_table;
12176
12177 static elf_flags_to_name_table elf_flags_to_names [] =
12178 {
12179   { "SHF_WRITE", SHF_WRITE },
12180   { "SHF_ALLOC", SHF_ALLOC },
12181   { "SHF_EXECINSTR", SHF_EXECINSTR },
12182   { "SHF_MERGE", SHF_MERGE },
12183   { "SHF_STRINGS", SHF_STRINGS },
12184   { "SHF_INFO_LINK", SHF_INFO_LINK},
12185   { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12186   { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12187   { "SHF_GROUP", SHF_GROUP },
12188   { "SHF_TLS", SHF_TLS },
12189   { "SHF_MASKOS", SHF_MASKOS },
12190   { "SHF_EXCLUDE", SHF_EXCLUDE },
12191 };
12192
12193 void
12194 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12195                               struct flag_info *finfo)
12196 {
12197   bfd *output_bfd = info->output_bfd;
12198   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12199   struct flag_info_list *tf = finfo->flag_list;
12200   int with_hex = 0;
12201   int without_hex = 0;
12202
12203   for (tf = finfo->flag_list; tf != NULL; tf = tf->next)
12204     {
12205       int i;
12206       if (bed->elf_backend_lookup_section_flags_hook)
12207         {
12208           flagword hexval =
12209              (*bed->elf_backend_lookup_section_flags_hook) ((char *) tf->name);
12210
12211           if (hexval != 0)
12212             {
12213               if (tf->with == with_flags)
12214                 with_hex |= hexval;
12215               else if (tf->with == without_flags)
12216                 without_hex |= hexval;
12217               tf->valid = TRUE;
12218               continue;
12219             }
12220         }
12221       for (i = 0; i < 12; i++)
12222         {
12223           if (!strcmp (tf->name, elf_flags_to_names[i].flag_name))
12224             {
12225               if (tf->with == with_flags)
12226                 with_hex |= elf_flags_to_names[i].flag_value;
12227               else if (tf->with == without_flags)
12228                 without_hex |= elf_flags_to_names[i].flag_value;
12229               tf->valid = TRUE;
12230               continue;
12231             }
12232         }
12233       if (tf->valid == FALSE)
12234         {
12235           info->callbacks->einfo 
12236                 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
12237           return;
12238         }
12239     }
12240  finfo->flags_initialized = TRUE;
12241  finfo->only_with_flags |= with_hex;
12242  finfo->not_with_flags |= without_hex;
12243
12244  return;
12245 }
12246
12247 struct alloc_got_off_arg {
12248   bfd_vma gotoff;
12249   struct bfd_link_info *info;
12250 };
12251
12252 /* We need a special top-level link routine to convert got reference counts
12253    to real got offsets.  */
12254
12255 static bfd_boolean
12256 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12257 {
12258   struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
12259   bfd *obfd = gofarg->info->output_bfd;
12260   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12261
12262   if (h->got.refcount > 0)
12263     {
12264       h->got.offset = gofarg->gotoff;
12265       gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
12266     }
12267   else
12268     h->got.offset = (bfd_vma) -1;
12269
12270   return TRUE;
12271 }
12272
12273 /* And an accompanying bit to work out final got entry offsets once
12274    we're done.  Should be called from final_link.  */
12275
12276 bfd_boolean
12277 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12278                                         struct bfd_link_info *info)
12279 {
12280   bfd *i;
12281   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12282   bfd_vma gotoff;
12283   struct alloc_got_off_arg gofarg;
12284
12285   BFD_ASSERT (abfd == info->output_bfd);
12286
12287   if (! is_elf_hash_table (info->hash))
12288     return FALSE;
12289
12290   /* The GOT offset is relative to the .got section, but the GOT header is
12291      put into the .got.plt section, if the backend uses it.  */
12292   if (bed->want_got_plt)
12293     gotoff = 0;
12294   else
12295     gotoff = bed->got_header_size;
12296
12297   /* Do the local .got entries first.  */
12298   for (i = info->input_bfds; i; i = i->link_next)
12299     {
12300       bfd_signed_vma *local_got;
12301       bfd_size_type j, locsymcount;
12302       Elf_Internal_Shdr *symtab_hdr;
12303
12304       if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12305         continue;
12306
12307       local_got = elf_local_got_refcounts (i);
12308       if (!local_got)
12309         continue;
12310
12311       symtab_hdr = &elf_tdata (i)->symtab_hdr;
12312       if (elf_bad_symtab (i))
12313         locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12314       else
12315         locsymcount = symtab_hdr->sh_info;
12316
12317       for (j = 0; j < locsymcount; ++j)
12318         {
12319           if (local_got[j] > 0)
12320             {
12321               local_got[j] = gotoff;
12322               gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
12323             }
12324           else
12325             local_got[j] = (bfd_vma) -1;
12326         }
12327     }
12328
12329   /* Then the global .got entries.  .plt refcounts are handled by
12330      adjust_dynamic_symbol  */
12331   gofarg.gotoff = gotoff;
12332   gofarg.info = info;
12333   elf_link_hash_traverse (elf_hash_table (info),
12334                           elf_gc_allocate_got_offsets,
12335                           &gofarg);
12336   return TRUE;
12337 }
12338
12339 /* Many folk need no more in the way of final link than this, once
12340    got entry reference counting is enabled.  */
12341
12342 bfd_boolean
12343 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12344 {
12345   if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12346     return FALSE;
12347
12348   /* Invoke the regular ELF backend linker to do all the work.  */
12349   return bfd_elf_final_link (abfd, info);
12350 }
12351
12352 bfd_boolean
12353 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12354 {
12355   struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
12356
12357   if (rcookie->bad_symtab)
12358     rcookie->rel = rcookie->rels;
12359
12360   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12361     {
12362       unsigned long r_symndx;
12363
12364       if (! rcookie->bad_symtab)
12365         if (rcookie->rel->r_offset > offset)
12366           return FALSE;
12367       if (rcookie->rel->r_offset != offset)
12368         continue;
12369
12370       r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
12371       if (r_symndx == STN_UNDEF)
12372         return TRUE;
12373
12374       if (r_symndx >= rcookie->locsymcount
12375           || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12376         {
12377           struct elf_link_hash_entry *h;
12378
12379           h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12380
12381           while (h->root.type == bfd_link_hash_indirect
12382                  || h->root.type == bfd_link_hash_warning)
12383             h = (struct elf_link_hash_entry *) h->root.u.i.link;
12384
12385           if ((h->root.type == bfd_link_hash_defined
12386                || h->root.type == bfd_link_hash_defweak)
12387               && elf_discarded_section (h->root.u.def.section))
12388             return TRUE;
12389           else
12390             return FALSE;
12391         }
12392       else
12393         {
12394           /* It's not a relocation against a global symbol,
12395              but it could be a relocation against a local
12396              symbol for a discarded section.  */
12397           asection *isec;
12398           Elf_Internal_Sym *isym;
12399
12400           /* Need to: get the symbol; get the section.  */
12401           isym = &rcookie->locsyms[r_symndx];
12402           isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
12403           if (isec != NULL && elf_discarded_section (isec))
12404             return TRUE;
12405         }
12406       return FALSE;
12407     }
12408   return FALSE;
12409 }
12410
12411 /* Discard unneeded references to discarded sections.
12412    Returns TRUE if any section's size was changed.  */
12413 /* This function assumes that the relocations are in sorted order,
12414    which is true for all known assemblers.  */
12415
12416 bfd_boolean
12417 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12418 {
12419   struct elf_reloc_cookie cookie;
12420   asection *stab, *eh;
12421   const struct elf_backend_data *bed;
12422   bfd *abfd;
12423   bfd_boolean ret = FALSE;
12424
12425   if (info->traditional_format
12426       || !is_elf_hash_table (info->hash))
12427     return FALSE;
12428
12429   _bfd_elf_begin_eh_frame_parsing (info);
12430   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12431     {
12432       if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12433         continue;
12434
12435       bed = get_elf_backend_data (abfd);
12436
12437       if ((abfd->flags & DYNAMIC) != 0)
12438         continue;
12439
12440       eh = NULL;
12441       if (!info->relocatable)
12442         {
12443           eh = bfd_get_section_by_name (abfd, ".eh_frame");
12444           if (eh != NULL
12445               && (eh->size == 0
12446                   || bfd_is_abs_section (eh->output_section)))
12447             eh = NULL;
12448         }
12449
12450       stab = bfd_get_section_by_name (abfd, ".stab");
12451       if (stab != NULL
12452           && (stab->size == 0
12453               || bfd_is_abs_section (stab->output_section)
12454               || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12455         stab = NULL;
12456
12457       if (stab == NULL
12458           && eh == NULL
12459           && bed->elf_backend_discard_info == NULL)
12460         continue;
12461
12462       if (!init_reloc_cookie (&cookie, info, abfd))
12463         return FALSE;
12464
12465       if (stab != NULL
12466           && stab->reloc_count > 0
12467           && init_reloc_cookie_rels (&cookie, info, abfd, stab))
12468         {
12469           if (_bfd_discard_section_stabs (abfd, stab,
12470                                           elf_section_data (stab)->sec_info,
12471                                           bfd_elf_reloc_symbol_deleted_p,
12472                                           &cookie))
12473             ret = TRUE;
12474           fini_reloc_cookie_rels (&cookie, stab);
12475         }
12476
12477       if (eh != NULL
12478           && init_reloc_cookie_rels (&cookie, info, abfd, eh))
12479         {
12480           _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
12481           if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12482                                                  bfd_elf_reloc_symbol_deleted_p,
12483                                                  &cookie))
12484             ret = TRUE;
12485           fini_reloc_cookie_rels (&cookie, eh);
12486         }
12487
12488       if (bed->elf_backend_discard_info != NULL
12489           && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12490         ret = TRUE;
12491
12492       fini_reloc_cookie (&cookie, abfd);
12493     }
12494   _bfd_elf_end_eh_frame_parsing (info);
12495
12496   if (info->eh_frame_hdr
12497       && !info->relocatable
12498       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12499     ret = TRUE;
12500
12501   return ret;
12502 }
12503
12504 /* For a SHT_GROUP section, return the group signature.  For other
12505    sections, return the normal section name.  */
12506
12507 static const char *
12508 section_signature (asection *sec)
12509 {
12510   if ((sec->flags & SEC_GROUP) != 0
12511       && elf_next_in_group (sec) != NULL
12512       && elf_group_name (elf_next_in_group (sec)) != NULL)
12513     return elf_group_name (elf_next_in_group (sec));
12514   return sec->name;
12515 }
12516
12517 void
12518 _bfd_elf_section_already_linked (bfd *abfd,
12519                                  struct already_linked *linked,
12520                                  struct bfd_link_info *info)
12521 {
12522   flagword flags;
12523   const char *name, *p;
12524   struct bfd_section_already_linked *l;
12525   struct bfd_section_already_linked_hash_entry *already_linked_list;
12526   asection *sec, *l_sec;
12527
12528   p = name = linked->comdat_key;
12529   if (name)
12530     {
12531       sec = NULL;
12532       flags = SEC_GROUP | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
12533     }
12534   else
12535     {
12536       sec = linked->u.sec;
12537       if (sec->output_section == bfd_abs_section_ptr)
12538         return;
12539
12540       flags = sec->flags;
12541
12542       /* Return if it isn't a linkonce section.  A comdat group section
12543          also has SEC_LINK_ONCE set.  */
12544       if ((flags & SEC_LINK_ONCE) == 0)
12545         return;
12546
12547       /* Don't put group member sections on our list of already linked
12548          sections.  They are handled as a group via their group section.
12549          */
12550       if (elf_sec_group (sec) != NULL)
12551         return;
12552
12553       /* FIXME: When doing a relocatable link, we may have trouble
12554          copying relocations in other sections that refer to local symbols
12555          in the section being discarded.  Those relocations will have to
12556          be converted somehow; as of this writing I'm not sure that any of
12557          the backends handle that correctly.
12558
12559          It is tempting to instead not discard link once sections when
12560          doing a relocatable link (technically, they should be discarded
12561          whenever we are building constructors).  However, that fails,
12562          because the linker winds up combining all the link once sections
12563          into a single large link once section, which defeats the purpose
12564          of having link once sections in the first place.
12565
12566          Also, not merging link once sections in a relocatable link
12567          causes trouble for MIPS ELF, which relies on link once semantics
12568          to handle the .reginfo section correctly.  */
12569
12570       name = section_signature (sec);
12571
12572       if (CONST_STRNEQ (name, ".gnu.linkonce.")
12573           && ((p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.'))
12574               != NULL))
12575         p++;
12576       else
12577         p = name;
12578     }
12579
12580   already_linked_list = bfd_section_already_linked_table_lookup (p);
12581
12582   for (l = already_linked_list->entry; l != NULL; l = l->next)
12583     {
12584       bfd_boolean l_coff_comdat_sec;
12585       flagword l_flags;
12586       bfd *l_owner;
12587       const char *l_name = l->linked.comdat_key;
12588       if (l_name)
12589         {
12590           l_sec = NULL;
12591           l_owner = l->linked.u.abfd;
12592           l_flags = (SEC_GROUP
12593                      | SEC_LINK_ONCE
12594                      | SEC_LINK_DUPLICATES_DISCARD);
12595           l_coff_comdat_sec = FALSE;
12596         }
12597       else
12598         {
12599           l_sec = l->linked.u.sec;
12600           l_owner = l_sec->owner;
12601           l_flags = l_sec->flags;
12602           l_coff_comdat_sec
12603             = !!bfd_coff_get_comdat_section (l_sec->owner, l_sec);
12604           l_name = section_signature (l_sec);
12605         }
12606
12607       /* We may have 2 different types of sections on the list: group
12608          sections and linkonce sections.  Match like sections.  */
12609       if ((flags & SEC_GROUP) == (l_flags & SEC_GROUP)
12610           && strcmp (name, l_name) == 0
12611           && !l_coff_comdat_sec)
12612         {
12613           /* The section has already been linked.  See if we should
12614              issue a warning.  */
12615           switch (flags & SEC_LINK_DUPLICATES)
12616             {
12617             default:
12618               abort ();
12619
12620             case SEC_LINK_DUPLICATES_DISCARD:
12621               /* If we found an LTO IR match for this comdat group on
12622                  the first pass, replace it with the LTO output on the
12623                  second pass.  We can't simply choose real object
12624                  files over IR because the first pass may contain a
12625                  mix of LTO and normal objects and we must keep the
12626                  first match, be it IR or real.  */
12627               if (info->loading_lto_outputs
12628                   && (l_owner->flags & BFD_PLUGIN) != 0)
12629                 {
12630                   l->linked = *linked;
12631                   return;
12632                 }
12633               break;
12634
12635             case SEC_LINK_DUPLICATES_ONE_ONLY:
12636               (*_bfd_error_handler)
12637                 (_("%B: ignoring duplicate section `%A'"),
12638                  abfd, sec);
12639               break;
12640
12641             case SEC_LINK_DUPLICATES_SAME_SIZE:
12642               if (!sec || !l_sec)
12643                 abort ();
12644
12645               if (sec->size != l_sec->size)
12646                 (*_bfd_error_handler)
12647                   (_("%B: duplicate section `%A' has different size"),
12648                    abfd, sec);
12649               break;
12650
12651             case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12652               if (!sec || !l_sec)
12653                 abort ();
12654
12655               if (sec->size != l_sec->size)
12656                 (*_bfd_error_handler)
12657                   (_("%B: duplicate section `%A' has different size"),
12658                    abfd, sec);
12659               else if (sec->size != 0)
12660                 {
12661                   bfd_byte *sec_contents, *l_sec_contents;
12662
12663                   if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12664                     (*_bfd_error_handler)
12665                       (_("%B: warning: could not read contents of section `%A'"),
12666                        abfd, sec);
12667                   else if (!bfd_malloc_and_get_section (l_sec->owner, l_sec,
12668                                                         &l_sec_contents))
12669                     (*_bfd_error_handler)
12670                       (_("%B: warning: could not read contents of section `%A'"),
12671                        l_sec->owner, l_sec);
12672                   else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12673                     (*_bfd_error_handler)
12674                       (_("%B: warning: duplicate section `%A' has different contents"),
12675                        abfd, sec);
12676
12677                   if (sec_contents)
12678                     free (sec_contents);
12679                   if (l_sec_contents)
12680                     free (l_sec_contents);
12681                 }
12682               break;
12683             }
12684
12685           if (sec)
12686             {
12687               /* Set the output_section field so that lang_add_section
12688                  does not create a lang_input_section structure for this
12689                  section.  Since there might be a symbol in the section
12690                  being discarded, we must retain a pointer to the section
12691                  which we are really going to use.  */
12692               sec->output_section = bfd_abs_section_ptr;
12693               sec->kept_section = l_sec;
12694
12695               if (flags & SEC_GROUP)
12696                 {
12697                   asection *first = elf_next_in_group (sec);
12698                   asection *s = first;
12699
12700                   while (s != NULL)
12701                     {
12702                       s->output_section = bfd_abs_section_ptr;
12703                       /* Record which group discards it.  */
12704                       s->kept_section = l_sec;
12705                       s = elf_next_in_group (s);
12706                       /* These lists are circular.  */
12707                       if (s == first)
12708                         break;
12709                     }
12710                 }
12711             }
12712
12713           return;
12714         }
12715     }
12716
12717   if (sec)
12718     {
12719       /* A single member comdat group section may be discarded by a
12720          linkonce section and vice versa.  */
12721
12722       if ((flags & SEC_GROUP) != 0)
12723         {
12724           asection *first = elf_next_in_group (sec);
12725
12726           if (first != NULL && elf_next_in_group (first) == first)
12727             /* Check this single member group against linkonce sections.  */
12728             for (l = already_linked_list->entry; l != NULL; l = l->next)
12729               {
12730                 if (l->linked.comdat_key == NULL)
12731                   {
12732                     l_sec = l->linked.u.sec;
12733
12734                     if ((l_sec->flags & SEC_GROUP) == 0
12735                         && bfd_coff_get_comdat_section (l_sec->owner,
12736                                                         l_sec) == NULL
12737                         && bfd_elf_match_symbols_in_sections (l_sec,
12738                                                               first,
12739                                                               info))
12740                       {
12741                         first->output_section = bfd_abs_section_ptr;
12742                         first->kept_section = l_sec;
12743                         sec->output_section = bfd_abs_section_ptr;
12744                         break;
12745                       }
12746                   }
12747               }
12748         }
12749       else
12750         /* Check this linkonce section against single member groups.  */
12751         for (l = already_linked_list->entry; l != NULL; l = l->next)
12752           {
12753             if (l->linked.comdat_key == NULL)
12754               {
12755                 l_sec = l->linked.u.sec;
12756
12757                 if (l_sec->flags & SEC_GROUP)
12758                   {
12759                     asection *first = elf_next_in_group (l_sec);
12760
12761                     if (first != NULL
12762                         && elf_next_in_group (first) == first
12763                         && bfd_elf_match_symbols_in_sections (first,
12764                                                               sec,
12765                                                               info))
12766                       {
12767                         sec->output_section = bfd_abs_section_ptr;
12768                         sec->kept_section = first;
12769                         break;
12770                       }
12771                   }
12772               }
12773           }
12774
12775       /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12776          referencing its discarded `.gnu.linkonce.t.F' counterpart -
12777          g++-3.4 specific as g++-4.x is using COMDAT groups (without the
12778          `.gnu.linkonce' prefix) instead.  `.gnu.linkonce.r.*' were the
12779          `.rodata' part of its matching `.gnu.linkonce.t.*'.  If
12780          `.gnu.linkonce.r.F' is not discarded but its `.gnu.linkonce.t.F'
12781          is discarded means we chose one-only `.gnu.linkonce.t.F' section
12782          from a different bfd not requiring any `.gnu.linkonce.r.F'.
12783          Thus `.gnu.linkonce.r.F' should be discarded.  The reverse order
12784          cannot happen as there is never a bfd with only the
12785          `.gnu.linkonce.r.F' section.  The order of sections in a bfd
12786          does not matter as here were are looking only for cross-bfd
12787          sections.  */
12788
12789       if ((flags & SEC_GROUP) == 0
12790           && CONST_STRNEQ (name, ".gnu.linkonce.r."))
12791         for (l = already_linked_list->entry; l != NULL; l = l->next)
12792           {
12793             if (l->linked.comdat_key == NULL)
12794               {
12795                 l_sec = l->linked.u.sec;
12796
12797                 if ((l_sec->flags & SEC_GROUP) == 0
12798                     && CONST_STRNEQ (l_sec->name, ".gnu.linkonce.t."))
12799                   {
12800                     if (abfd != l_sec->owner)
12801                       sec->output_section = bfd_abs_section_ptr;
12802                     break;
12803                   }
12804               }
12805           }
12806     }
12807
12808   /* This is the first section with this name.  Record it.  */
12809   if (! bfd_section_already_linked_table_insert (already_linked_list,
12810                                                  linked))
12811     info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
12812 }
12813
12814 bfd_boolean
12815 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
12816 {
12817   return sym->st_shndx == SHN_COMMON;
12818 }
12819
12820 unsigned int
12821 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12822 {
12823   return SHN_COMMON;
12824 }
12825
12826 asection *
12827 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12828 {
12829   return bfd_com_section_ptr;
12830 }
12831
12832 bfd_vma
12833 _bfd_elf_default_got_elt_size (bfd *abfd,
12834                                struct bfd_link_info *info ATTRIBUTE_UNUSED,
12835                                struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12836                                bfd *ibfd ATTRIBUTE_UNUSED,
12837                                unsigned long symndx ATTRIBUTE_UNUSED)
12838 {
12839   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12840   return bed->s->arch_size / 8;
12841 }
12842
12843 /* Routines to support the creation of dynamic relocs.  */
12844
12845 /* Returns the name of the dynamic reloc section associated with SEC.  */
12846
12847 static const char *
12848 get_dynamic_reloc_section_name (bfd *       abfd,
12849                                 asection *  sec,
12850                                 bfd_boolean is_rela)
12851 {
12852   char *name;
12853   const char *old_name = bfd_get_section_name (NULL, sec);
12854   const char *prefix = is_rela ? ".rela" : ".rel";
12855
12856   if (old_name == NULL)
12857     return NULL;
12858
12859   name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
12860   sprintf (name, "%s%s", prefix, old_name); 
12861
12862   return name;
12863 }
12864
12865 /* Returns the dynamic reloc section associated with SEC.
12866    If necessary compute the name of the dynamic reloc section based
12867    on SEC's name (looked up in ABFD's string table) and the setting
12868    of IS_RELA.  */
12869
12870 asection *
12871 _bfd_elf_get_dynamic_reloc_section (bfd *       abfd,
12872                                     asection *  sec,
12873                                     bfd_boolean is_rela)
12874 {
12875   asection * reloc_sec = elf_section_data (sec)->sreloc;
12876
12877   if (reloc_sec == NULL)
12878     {
12879       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12880
12881       if (name != NULL)
12882         {
12883           reloc_sec = bfd_get_section_by_name (abfd, name);
12884
12885           if (reloc_sec != NULL)
12886             elf_section_data (sec)->sreloc = reloc_sec;
12887         }
12888     }
12889
12890   return reloc_sec;
12891 }
12892
12893 /* Returns the dynamic reloc section associated with SEC.  If the
12894    section does not exist it is created and attached to the DYNOBJ
12895    bfd and stored in the SRELOC field of SEC's elf_section_data
12896    structure.
12897
12898    ALIGNMENT is the alignment for the newly created section and
12899    IS_RELA defines whether the name should be .rela.<SEC's name>
12900    or .rel.<SEC's name>.  The section name is looked up in the
12901    string table associated with ABFD.  */
12902
12903 asection *
12904 _bfd_elf_make_dynamic_reloc_section (asection *         sec,
12905                                      bfd *              dynobj,
12906                                      unsigned int       alignment,
12907                                      bfd *              abfd,
12908                                      bfd_boolean        is_rela)
12909 {
12910   asection * reloc_sec = elf_section_data (sec)->sreloc;
12911
12912   if (reloc_sec == NULL)
12913     {
12914       const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12915
12916       if (name == NULL)
12917         return NULL;
12918
12919       reloc_sec = bfd_get_section_by_name (dynobj, name);
12920
12921       if (reloc_sec == NULL)
12922         {
12923           flagword flags;
12924
12925           flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12926           if ((sec->flags & SEC_ALLOC) != 0)
12927             flags |= SEC_ALLOC | SEC_LOAD;
12928
12929           reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12930           if (reloc_sec != NULL)
12931             {
12932               if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12933                 reloc_sec = NULL;
12934             }
12935         }
12936
12937       elf_section_data (sec)->sreloc = reloc_sec;
12938     }
12939
12940   return reloc_sec;
12941 }
12942
12943 /* Copy the ELF symbol type associated with a linker hash entry.  */
12944 void
12945 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
12946     struct bfd_link_hash_entry * hdest,
12947     struct bfd_link_hash_entry * hsrc)
12948 {
12949   struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest;
12950   struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc;
12951
12952   ehdest->type = ehsrc->type;
12953   ehdest->target_internal = ehsrc->target_internal;
12954 }
12955
12956 /* Append a RELA relocation REL to section S in BFD.  */
12957
12958 void
12959 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12960 {
12961   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12962   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
12963   BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
12964   bed->s->swap_reloca_out (abfd, rel, loc);
12965 }
12966
12967 /* Append a REL relocation REL to section S in BFD.  */
12968
12969 void
12970 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12971 {
12972   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12973   bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
12974   BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
12975   bed->s->swap_reloca_out (abfd, rel, loc);
12976 }