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