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