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