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