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