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