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