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