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