* elf32-mips.c: Numerous changes to dynamic linking code, mostly
[external/binutils.git] / bfd / elflink.h
1 /* ELF linker support.
2    Copyright 1995 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* ELF linker code.  */
21
22 static boolean elf_link_add_object_symbols
23   PARAMS ((bfd *, struct bfd_link_info *));
24 static boolean elf_link_add_archive_symbols
25   PARAMS ((bfd *, struct bfd_link_info *));
26 static Elf_Internal_Rela *elf_link_read_relocs
27   PARAMS ((bfd *, asection *, PTR, Elf_Internal_Rela *, boolean));
28 static boolean elf_export_symbol
29   PARAMS ((struct elf_link_hash_entry *, PTR));
30 static boolean elf_adjust_dynamic_symbol
31   PARAMS ((struct elf_link_hash_entry *, PTR));
32
33 /* This struct is used to pass information to routines called via
34    elf_link_hash_traverse which must return failure.  */
35
36 struct elf_info_failed
37 {
38   boolean failed;
39   struct bfd_link_info *info;
40 };  
41
42 /* Given an ELF BFD, add symbols to the global hash table as
43    appropriate.  */
44
45 boolean
46 elf_bfd_link_add_symbols (abfd, info)
47      bfd *abfd;
48      struct bfd_link_info *info;
49 {
50   switch (bfd_get_format (abfd))
51     {
52     case bfd_object:
53       return elf_link_add_object_symbols (abfd, info);
54     case bfd_archive:
55       return elf_link_add_archive_symbols (abfd, info);
56     default:
57       bfd_set_error (bfd_error_wrong_format);
58       return false;
59     }
60 }
61
62 /* Add symbols from an ELF archive file to the linker hash table.  We
63    don't use _bfd_generic_link_add_archive_symbols because of a
64    problem which arises on UnixWare.  The UnixWare libc.so is an
65    archive which includes an entry libc.so.1 which defines a bunch of
66    symbols.  The libc.so archive also includes a number of other
67    object files, which also define symbols, some of which are the same
68    as those defined in libc.so.1.  Correct linking requires that we
69    consider each object file in turn, and include it if it defines any
70    symbols we need.  _bfd_generic_link_add_archive_symbols does not do
71    this; it looks through the list of undefined symbols, and includes
72    any object file which defines them.  When this algorithm is used on
73    UnixWare, it winds up pulling in libc.so.1 early and defining a
74    bunch of symbols.  This means that some of the other objects in the
75    archive are not included in the link, which is incorrect since they
76    precede libc.so.1 in the archive.
77
78    Fortunately, ELF archive handling is simpler than that done by
79    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
80    oddities.  In ELF, if we find a symbol in the archive map, and the
81    symbol is currently undefined, we know that we must pull in that
82    object file.
83
84    Unfortunately, we do have to make multiple passes over the symbol
85    table until nothing further is resolved.  */
86
87 static boolean
88 elf_link_add_archive_symbols (abfd, info)
89      bfd *abfd;
90      struct bfd_link_info *info;
91 {
92   symindex c;
93   boolean *defined = NULL;
94   boolean *included = NULL;
95   carsym *symdefs;
96   boolean loop;
97
98   if (! bfd_has_map (abfd))
99     {
100       /* An empty archive is a special case.  */
101       if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
102         return true;
103       bfd_set_error (bfd_error_no_armap);
104       return false;
105     }
106
107   /* Keep track of all symbols we know to be already defined, and all
108      files we know to be already included.  This is to speed up the
109      second and subsequent passes.  */
110   c = bfd_ardata (abfd)->symdef_count;
111   if (c == 0)
112     return true;
113   defined = (boolean *) bfd_malloc (c * sizeof (boolean));
114   included = (boolean *) bfd_malloc (c * sizeof (boolean));
115   if (defined == (boolean *) NULL || included == (boolean *) NULL)
116     goto error_return;
117   memset (defined, 0, c * sizeof (boolean));
118   memset (included, 0, c * sizeof (boolean));
119
120   symdefs = bfd_ardata (abfd)->symdefs;
121
122   do
123     {
124       file_ptr last;
125       symindex i;
126       carsym *symdef;
127       carsym *symdefend;
128
129       loop = false;
130       last = -1;
131
132       symdef = symdefs;
133       symdefend = symdef + c;
134       for (i = 0; symdef < symdefend; symdef++, i++)
135         {
136           struct elf_link_hash_entry *h;
137           bfd *element;
138           struct bfd_link_hash_entry *undefs_tail;
139           symindex mark;
140
141           if (defined[i] || included[i])
142             continue;
143           if (symdef->file_offset == last)
144             {
145               included[i] = true;
146               continue;
147             }
148
149           h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
150                                     false, false, false);
151           if (h == (struct elf_link_hash_entry *) NULL)
152             continue;
153           if (h->root.type != bfd_link_hash_undefined)
154             {
155               if (h->root.type != bfd_link_hash_undefweak)
156                 defined[i] = true;
157               continue;
158             }
159
160           /* We need to include this archive member.  */
161
162           element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
163           if (element == (bfd *) NULL)
164             goto error_return;
165
166           if (! bfd_check_format (element, bfd_object))
167             goto error_return;
168
169           /* Doublecheck that we have not included this object
170              already--it should be impossible, but there may be
171              something wrong with the archive.  */
172           if (element->archive_pass != 0)
173             {
174               bfd_set_error (bfd_error_bad_value);
175               goto error_return;
176             }
177           element->archive_pass = 1;
178
179           undefs_tail = info->hash->undefs_tail;
180
181           if (! (*info->callbacks->add_archive_element) (info, element,
182                                                          symdef->name))
183             goto error_return;
184           if (! elf_link_add_object_symbols (element, info))
185             goto error_return;
186
187           /* If there are any new undefined symbols, we need to make
188              another pass through the archive in order to see whether
189              they can be defined.  FIXME: This isn't perfect, because
190              common symbols wind up on undefs_tail and because an
191              undefined symbol which is defined later on in this pass
192              does not require another pass.  This isn't a bug, but it
193              does make the code less efficient than it could be.  */
194           if (undefs_tail != info->hash->undefs_tail)
195             loop = true;
196
197           /* Look backward to mark all symbols from this object file
198              which we have already seen in this pass.  */
199           mark = i;
200           do
201             {
202               included[mark] = true;
203               if (mark == 0)
204                 break;
205               --mark;
206             }
207           while (symdefs[mark].file_offset == symdef->file_offset);
208
209           /* We mark subsequent symbols from this object file as we go
210              on through the loop.  */
211           last = symdef->file_offset;
212         }
213     }
214   while (loop);
215
216   free (defined);
217   free (included);
218
219   return true;
220
221  error_return:
222   if (defined != (boolean *) NULL)
223     free (defined);
224   if (included != (boolean *) NULL)
225     free (included);
226   return false;
227 }
228
229 /* Add symbols from an ELF object file to the linker hash table.  */
230
231 static boolean
232 elf_link_add_object_symbols (abfd, info)
233      bfd *abfd;
234      struct bfd_link_info *info;
235 {
236   boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
237                                       const Elf_Internal_Sym *,
238                                       const char **, flagword *,
239                                       asection **, bfd_vma *));
240   boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
241                                    asection *, const Elf_Internal_Rela *));
242   boolean collect;
243   Elf_Internal_Shdr *hdr;
244   size_t symcount;
245   size_t extsymcount;
246   size_t extsymoff;
247   Elf_External_Sym *buf = NULL;
248   struct elf_link_hash_entry **sym_hash;
249   boolean dynamic;
250   Elf_External_Dyn *dynbuf = NULL;
251   struct elf_link_hash_entry *weaks;
252   Elf_External_Sym *esym;
253   Elf_External_Sym *esymend;
254
255   add_symbol_hook = get_elf_backend_data (abfd)->elf_add_symbol_hook;
256   collect = get_elf_backend_data (abfd)->collect;
257
258   /* As a GNU extension, any input sections which are named
259      .gnu.warning.SYMBOL are treated as warning symbols for the given
260      symbol.  This differs from .gnu.warning sections, which generate
261      warnings when they are included in an output file.  */
262   if (! info->shared)
263     {
264       asection *s;
265
266       for (s = abfd->sections; s != NULL; s = s->next)
267         {
268           const char *name;
269
270           name = bfd_get_section_name (abfd, s);
271           if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
272             {
273               char *msg;
274               bfd_size_type sz;
275
276               sz = bfd_section_size (abfd, s);
277               msg = (char *) bfd_alloc (abfd, sz);
278               if (msg == NULL)
279                 goto error_return;
280
281               if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
282                 goto error_return;
283
284               if (! (_bfd_generic_link_add_one_symbol
285                      (info, abfd, 
286                       name + sizeof ".gnu.warning." - 1,
287                       BSF_WARNING, s, (bfd_vma) 0, msg, false, collect,
288                       (struct bfd_link_hash_entry **) NULL)))
289                 goto error_return;
290
291               if (! info->relocateable)
292                 {
293                   /* Clobber the section size so that the warning does
294                      not get copied into the output file.  */
295                   s->_raw_size = 0;
296                 }
297             }
298         }
299     }
300
301   /* A stripped shared library might only have a dynamic symbol table,
302      not a regular symbol table.  In that case we can still go ahead
303      and link using the dynamic symbol table.  */
304   if (elf_onesymtab (abfd) == 0
305       && elf_dynsymtab (abfd) != 0)
306     {
307       elf_onesymtab (abfd) = elf_dynsymtab (abfd);
308       elf_tdata (abfd)->symtab_hdr = elf_tdata (abfd)->dynsymtab_hdr;
309     }
310
311   hdr = &elf_tdata (abfd)->symtab_hdr;
312   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
313
314   /* The sh_info field of the symtab header tells us where the
315      external symbols start.  We don't care about the local symbols at
316      this point.  */
317   if (elf_bad_symtab (abfd))
318     {
319       extsymcount = symcount;
320       extsymoff = 0;
321     }
322   else
323     {
324       extsymcount = symcount - hdr->sh_info;
325       extsymoff = hdr->sh_info;
326     }
327
328   buf = ((Elf_External_Sym *)
329          bfd_malloc (extsymcount * sizeof (Elf_External_Sym)));
330   if (buf == NULL && extsymcount != 0)
331     goto error_return;
332
333   /* We store a pointer to the hash table entry for each external
334      symbol.  */
335   sym_hash = ((struct elf_link_hash_entry **)
336               bfd_alloc (abfd,
337                          extsymcount * sizeof (struct elf_link_hash_entry *)));
338   if (sym_hash == NULL)
339     goto error_return;
340   elf_sym_hashes (abfd) = sym_hash;
341
342   if (elf_elfheader (abfd)->e_type != ET_DYN)
343     {
344       dynamic = false;
345
346       /* If we are creating a shared library, create all the dynamic
347          sections immediately.  We need to attach them to something,
348          so we attach them to this BFD, provided it is the right
349          format.  FIXME: If there are no input BFD's of the same
350          format as the output, we can't make a shared library.  */
351       if (info->shared
352           && ! elf_hash_table (info)->dynamic_sections_created
353           && abfd->xvec == info->hash->creator)
354         {
355           if (! elf_link_create_dynamic_sections (abfd, info))
356             goto error_return;
357         }
358     }
359   else
360     {
361       asection *s;
362       boolean add_needed;
363       const char *name;
364       bfd_size_type oldsize;
365       bfd_size_type strindex;
366
367       dynamic = true;
368
369       /* You can't use -r against a dynamic object.  Also, there's no
370          hope of using a dynamic object which does not exactly match
371          the format of the output file.  */
372       if (info->relocateable
373           || info->hash->creator != abfd->xvec)
374         {
375           bfd_set_error (bfd_error_invalid_operation);
376           goto error_return;
377         }
378
379       /* Find the name to use in a DT_NEEDED entry that refers to this
380          object.  If the object has a DT_SONAME entry, we use it.
381          Otherwise, if the generic linker stuck something in
382          elf_dt_needed_name, we use that.  Otherwise, we just use the
383          file name.  If the generic linker put a null string into
384          elf_dt_needed_name, we don't make a DT_NEEDED entry at all,
385          even if there is a DT_SONAME entry.  */
386       add_needed = true;
387       name = bfd_get_filename (abfd);
388       if (elf_dt_needed_name (abfd) != NULL)
389         {
390           name = elf_dt_needed_name (abfd);
391           if (*name == '\0')
392             add_needed = false;
393         }
394       s = bfd_get_section_by_name (abfd, ".dynamic");
395       if (s != NULL)
396         {
397           Elf_External_Dyn *extdyn;
398           Elf_External_Dyn *extdynend;
399           int elfsec;
400           unsigned long link;
401
402           dynbuf = (Elf_External_Dyn *) bfd_malloc ((size_t) s->_raw_size);
403           if (dynbuf == NULL)
404             goto error_return;
405
406           if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
407                                           (file_ptr) 0, s->_raw_size))
408             goto error_return;
409
410           elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
411           if (elfsec == -1)
412             goto error_return;
413           link = elf_elfsections (abfd)[elfsec]->sh_link;
414
415           extdyn = dynbuf;
416           extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
417           for (; extdyn < extdynend; extdyn++)
418             {
419               Elf_Internal_Dyn dyn;
420
421               elf_swap_dyn_in (abfd, extdyn, &dyn);
422               if (add_needed && dyn.d_tag == DT_SONAME)
423                 {
424                   name = bfd_elf_string_from_elf_section (abfd, link,
425                                                           dyn.d_un.d_val);
426                   if (name == NULL)
427                     goto error_return;
428                 }
429               if (dyn.d_tag == DT_NEEDED)
430                 {
431                   struct bfd_link_needed_list *n, **pn;
432                   char *fnm, *anm;
433
434                   n = ((struct bfd_link_needed_list *)
435                        bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
436                   fnm = bfd_elf_string_from_elf_section (abfd, link,
437                                                          dyn.d_un.d_val);
438                   if (n == NULL || fnm == NULL)
439                     goto error_return;
440                   anm = bfd_alloc (abfd, strlen (fnm) + 1);
441                   if (anm == NULL)
442                     goto error_return;
443                   strcpy (anm, fnm);
444                   n->name = anm;
445                   n->by = abfd;
446                   n->next = NULL;
447                   for (pn = &elf_hash_table (info)->needed;
448                        *pn != NULL;
449                        pn = &(*pn)->next)
450                     ;
451                   *pn = n;
452                 }
453             }
454
455           free (dynbuf);
456           dynbuf = NULL;
457         }
458
459       /* We do not want to include any of the sections in a dynamic
460          object in the output file.  We hack by simply clobbering the
461          list of sections in the BFD.  This could be handled more
462          cleanly by, say, a new section flag; the existing
463          SEC_NEVER_LOAD flag is not the one we want, because that one
464          still implies that the section takes up space in the output
465          file.  */
466       abfd->sections = NULL;
467       abfd->section_count = 0;
468
469       /* If this is the first dynamic object found in the link, create
470          the special sections required for dynamic linking.  */
471       if (! elf_hash_table (info)->dynamic_sections_created)
472         {
473           if (! elf_link_create_dynamic_sections (abfd, info))
474             goto error_return;
475         }
476
477       if (add_needed)
478         {
479           /* Add a DT_NEEDED entry for this dynamic object.  */
480           oldsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
481           strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr, name,
482                                          true, false);
483           if (strindex == (bfd_size_type) -1)
484             goto error_return;
485
486           if (oldsize == _bfd_stringtab_size (elf_hash_table (info)->dynstr))
487             {
488               asection *sdyn;
489               Elf_External_Dyn *dyncon, *dynconend;
490
491               /* The hash table size did not change, which means that
492                  the dynamic object name was already entered.  If we
493                  have already included this dynamic object in the
494                  link, just ignore it.  There is no reason to include
495                  a particular dynamic object more than once.  */
496               sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
497                                               ".dynamic");
498               BFD_ASSERT (sdyn != NULL);
499
500               dyncon = (Elf_External_Dyn *) sdyn->contents;
501               dynconend = (Elf_External_Dyn *) (sdyn->contents +
502                                                 sdyn->_raw_size);
503               for (; dyncon < dynconend; dyncon++)
504                 {
505                   Elf_Internal_Dyn dyn;
506
507                   elf_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon,
508                                    &dyn);
509                   if (dyn.d_tag == DT_NEEDED
510                       && dyn.d_un.d_val == strindex)
511                     {
512                       if (buf != NULL)
513                         free (buf);
514                       return true;
515                     }
516                 }
517             }
518
519           if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
520             goto error_return;
521         }
522     }
523
524   if (bfd_seek (abfd,
525                 hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym),
526                 SEEK_SET) != 0
527       || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd)
528           != extsymcount * sizeof (Elf_External_Sym)))
529     goto error_return;
530
531   weaks = NULL;
532
533   esymend = buf + extsymcount;
534   for (esym = buf; esym < esymend; esym++, sym_hash++)
535     {
536       Elf_Internal_Sym sym;
537       int bind;
538       bfd_vma value;
539       asection *sec;
540       flagword flags;
541       const char *name;
542       struct elf_link_hash_entry *h;
543       boolean definition;
544       boolean size_change_ok, type_change_ok;
545       boolean new_weakdef;
546
547       elf_swap_symbol_in (abfd, esym, &sym);
548
549       flags = BSF_NO_FLAGS;
550       sec = NULL;
551       value = sym.st_value;
552       *sym_hash = NULL;
553
554       bind = ELF_ST_BIND (sym.st_info);
555       if (bind == STB_LOCAL)
556         {
557           /* This should be impossible, since ELF requires that all
558              global symbols follow all local symbols, and that sh_info
559              point to the first global symbol.  Unfortunatealy, Irix 5
560              screws this up.  */
561           continue;
562         }
563       else if (bind == STB_GLOBAL)
564         {
565           if (sym.st_shndx != SHN_UNDEF
566               && sym.st_shndx != SHN_COMMON)
567             flags = BSF_GLOBAL;
568           else
569             flags = 0;
570         }
571       else if (bind == STB_WEAK)
572         flags = BSF_WEAK;
573       else
574         {
575           /* Leave it up to the processor backend.  */
576         }
577
578       if (sym.st_shndx == SHN_UNDEF)
579         sec = bfd_und_section_ptr;
580       else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE)
581         {
582           sec = section_from_elf_index (abfd, sym.st_shndx);
583           if (sec != NULL)
584             value -= sec->vma;
585           else
586             sec = bfd_abs_section_ptr;
587         }
588       else if (sym.st_shndx == SHN_ABS)
589         sec = bfd_abs_section_ptr;
590       else if (sym.st_shndx == SHN_COMMON)
591         {
592           sec = bfd_com_section_ptr;
593           /* What ELF calls the size we call the value.  What ELF
594              calls the value we call the alignment.  */
595           value = sym.st_size;
596         }
597       else
598         {
599           /* Leave it up to the processor backend.  */
600         }
601
602       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
603       if (name == (const char *) NULL)
604         goto error_return;
605
606       if (add_symbol_hook)
607         {
608           if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
609                                     &value))
610             goto error_return;
611
612           /* The hook function sets the name to NULL if this symbol
613              should be skipped for some reason.  */
614           if (name == (const char *) NULL)
615             continue;
616         }
617
618       /* Sanity check that all possibilities were handled.  */
619       if (sec == (asection *) NULL)
620         {
621           bfd_set_error (bfd_error_bad_value);
622           goto error_return;
623         }
624
625       if (bfd_is_und_section (sec)
626           || bfd_is_com_section (sec))
627         definition = false;
628       else
629         definition = true;
630
631       size_change_ok = false;
632       type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
633       if (info->hash->creator->flavour == bfd_target_elf_flavour)
634         {
635           /* We need to look up the symbol now in order to get some of
636              the dynamic object handling right.  We pass the hash
637              table entry in to _bfd_generic_link_add_one_symbol so
638              that it does not have to look it up again.  */
639           h = elf_link_hash_lookup (elf_hash_table (info), name,
640                                     true, false, false);
641           if (h == NULL)
642             goto error_return;
643           *sym_hash = h;
644
645           while (h->root.type == bfd_link_hash_indirect
646                  || h->root.type == bfd_link_hash_warning)
647             h = (struct elf_link_hash_entry *) h->root.u.i.link;
648
649           /* It's OK to change the type if it used to be a weak
650              definition.  */
651           if (h->root.type == bfd_link_hash_defweak
652               || h->root.type == bfd_link_hash_undefweak)
653             type_change_ok = true;
654
655           /* It's OK to change the size if it used to be a weak
656              definition, or if it used to be undefined, or if we will
657              be overriding an old definition.  */
658           if (type_change_ok
659               || h->root.type == bfd_link_hash_undefined)
660             size_change_ok = true;
661
662           /* If we are looking at a dynamic object, and this is a
663              definition, we need to see if it has already been defined
664              by some other object.  If it has, we want to use the
665              existing definition, and we do not want to report a
666              multiple symbol definition error; we do this by
667              clobbering sec to be bfd_und_section_ptr.  */
668           if (dynamic && definition)
669             {
670               if (h->root.type == bfd_link_hash_defined
671                   || h->root.type == bfd_link_hash_defweak
672                   || (h->root.type == bfd_link_hash_common
673                       && bind == STB_WEAK))
674                 {
675                   sec = bfd_und_section_ptr;
676                   definition = false;
677                   size_change_ok = true;
678                 }
679             }
680
681           /* Similarly, if we are not looking at a dynamic object, and
682              we have a definition, we want to override any definition
683              we may have from a dynamic object.  Symbols from regular
684              files always take precedence over symbols from dynamic
685              objects, even if they are defined after the dynamic
686              object in the link.  */
687           if (! dynamic
688               && definition
689               && (h->root.type == bfd_link_hash_defined
690                   || h->root.type == bfd_link_hash_defweak)
691               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
692               && (bfd_get_flavour (h->root.u.def.section->owner)
693                   == bfd_target_elf_flavour)
694               && (elf_elfheader (h->root.u.def.section->owner)->e_type
695                   == ET_DYN))
696             {
697               /* Change the hash table entry to undefined, and let
698                  _bfd_generic_link_add_one_symbol do the right thing
699                  with the new definition.  */
700               h->root.type = bfd_link_hash_undefined;
701               h->root.u.undef.abfd = h->root.u.def.section->owner;
702               size_change_ok = true;
703             }
704         }
705
706       if (! (_bfd_generic_link_add_one_symbol
707              (info, abfd, name, flags, sec, value, (const char *) NULL,
708               false, collect, (struct bfd_link_hash_entry **) sym_hash)))
709         goto error_return;
710
711       h = *sym_hash;
712       while (h->root.type == bfd_link_hash_indirect
713              || h->root.type == bfd_link_hash_warning)
714         h = (struct elf_link_hash_entry *) h->root.u.i.link;
715       *sym_hash = h;
716
717       new_weakdef = false;
718       if (dynamic
719           && definition
720           && (flags & BSF_WEAK) != 0
721           && ELF_ST_TYPE (sym.st_info) != STT_FUNC
722           && info->hash->creator->flavour == bfd_target_elf_flavour
723           && h->weakdef == NULL)
724         {
725           /* Keep a list of all weak defined non function symbols from
726              a dynamic object, using the weakdef field.  Later in this
727              function we will set the weakdef field to the correct
728              value.  We only put non-function symbols from dynamic
729              objects on this list, because that happens to be the only
730              time we need to know the normal symbol corresponding to a
731              weak symbol, and the information is time consuming to
732              figure out.  If the weakdef field is not already NULL,
733              then this symbol was already defined by some previous
734              dynamic object, and we will be using that previous
735              definition anyhow.  */
736
737           h->weakdef = weaks;
738           weaks = h;
739           new_weakdef = true;
740         }
741
742       /* Get the alignment of a common symbol.  */
743       if (sym.st_shndx == SHN_COMMON
744           && h->root.type == bfd_link_hash_common)
745         h->root.u.c.p->alignment_power = bfd_log2 (sym.st_value);
746
747       if (info->hash->creator->flavour == bfd_target_elf_flavour)
748         {
749           int old_flags;
750           boolean dynsym;
751           int new_flag;
752
753           /* Remember the symbol size and type.  */
754           if (sym.st_size != 0
755               && (definition || h->size == 0))
756             {
757               if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
758                 (*_bfd_error_handler)
759                   ("Warning: size of symbol `%s' changed from %lu to %lu in %s",
760                    name, (unsigned long) h->size, (unsigned long) sym.st_size,
761                    bfd_get_filename (abfd));
762
763               h->size = sym.st_size;
764             }
765           if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
766               && (definition || h->type == STT_NOTYPE))
767             {
768               if (h->type != STT_NOTYPE
769                   && h->type != ELF_ST_TYPE (sym.st_info)
770                   && ! type_change_ok)
771                 (*_bfd_error_handler)
772                   ("Warning: type of symbol `%s' changed from %d to %d in %s",
773                    name, h->type, ELF_ST_TYPE (sym.st_info),
774                    bfd_get_filename (abfd));
775
776               h->type = ELF_ST_TYPE (sym.st_info);
777             }
778
779           /* Set a flag in the hash table entry indicating the type of
780              reference or definition we just found.  Keep a count of
781              the number of dynamic symbols we find.  A dynamic symbol
782              is one which is referenced or defined by both a regular
783              object and a shared object, or one which is referenced or
784              defined by more than one shared object.  */
785           old_flags = h->elf_link_hash_flags;
786           dynsym = false;
787           if (! dynamic)
788             {
789               if (! definition)
790                 new_flag = ELF_LINK_HASH_REF_REGULAR;
791               else
792                 new_flag = ELF_LINK_HASH_DEF_REGULAR;
793               if (info->shared
794                   || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
795                                    | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
796                 dynsym = true;
797             }
798           else
799             {
800               if (! definition)
801                 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
802               else
803                 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
804               if ((old_flags & new_flag) != 0
805                   || (old_flags & (ELF_LINK_HASH_DEF_REGULAR
806                                    | ELF_LINK_HASH_REF_REGULAR)) != 0
807                   || (h->weakdef != NULL
808                       && (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
809                                        | ELF_LINK_HASH_REF_DYNAMIC)) != 0))
810                 dynsym = true;
811             }
812
813           h->elf_link_hash_flags |= new_flag;
814           if (dynsym && h->dynindx == -1)
815             {
816               if (! _bfd_elf_link_record_dynamic_symbol (info, h))
817                 goto error_return;
818               if (h->weakdef != NULL
819                   && ! new_weakdef
820                   && h->weakdef->dynindx == -1)
821                 {
822                   if (! _bfd_elf_link_record_dynamic_symbol (info,
823                                                              h->weakdef))
824                     goto error_return;
825                 }
826             }
827         }
828     }
829
830   /* Now set the weakdefs field correctly for all the weak defined
831      symbols we found.  The only way to do this is to search all the
832      symbols.  Since we only need the information for non functions in
833      dynamic objects, that's the only time we actually put anything on
834      the list WEAKS.  We need this information so that if a regular
835      object refers to a symbol defined weakly in a dynamic object, the
836      real symbol in the dynamic object is also put in the dynamic
837      symbols; we also must arrange for both symbols to point to the
838      same memory location.  We could handle the general case of symbol
839      aliasing, but a general symbol alias can only be generated in
840      assembler code, handling it correctly would be very time
841      consuming, and other ELF linkers don't handle general aliasing
842      either.  */
843   while (weaks != NULL)
844     {
845       struct elf_link_hash_entry *hlook;
846       asection *slook;
847       bfd_vma vlook;
848       struct elf_link_hash_entry **hpp;
849       struct elf_link_hash_entry **hppend;
850
851       hlook = weaks;
852       weaks = hlook->weakdef;
853       hlook->weakdef = NULL;
854
855       BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
856                   || hlook->root.type == bfd_link_hash_defweak
857                   || hlook->root.type == bfd_link_hash_common
858                   || hlook->root.type == bfd_link_hash_indirect);
859       slook = hlook->root.u.def.section;
860       vlook = hlook->root.u.def.value;
861
862       hpp = elf_sym_hashes (abfd);
863       hppend = hpp + extsymcount;
864       for (; hpp < hppend; hpp++)
865         {
866           struct elf_link_hash_entry *h;
867
868           h = *hpp;
869           if (h != NULL && h != hlook
870               && (h->root.type == bfd_link_hash_defined
871                   || h->root.type == bfd_link_hash_defweak)
872               && h->root.u.def.section == slook
873               && h->root.u.def.value == vlook)
874             {
875               hlook->weakdef = h;
876
877               /* If the weak definition is in the list of dynamic
878                  symbols, make sure the real definition is put there
879                  as well.  */
880               if (hlook->dynindx != -1
881                   && h->dynindx == -1)
882                 {
883                   if (! _bfd_elf_link_record_dynamic_symbol (info, h))
884                     goto error_return;
885                 }
886
887               break;
888             }
889         }
890     }
891
892   if (buf != NULL)
893     {
894       free (buf);
895       buf = NULL;
896     }
897
898   /* If this object is the same format as the output object, and it is
899      not a shared library, then let the backend look through the
900      relocs.
901
902      This is required to build global offset table entries and to
903      arrange for dynamic relocs.  It is not required for the
904      particular common case of linking non PIC code, even when linking
905      against shared libraries, but unfortunately there is no way of
906      knowing whether an object file has been compiled PIC or not.
907      Looking through the relocs is not particularly time consuming.
908      The problem is that we must either (1) keep the relocs in memory,
909      which causes the linker to require additional runtime memory or
910      (2) read the relocs twice from the input file, which wastes time.
911      This would be a good case for using mmap.
912
913      I have no idea how to handle linking PIC code into a file of a
914      different format.  It probably can't be done.  */
915   check_relocs = get_elf_backend_data (abfd)->check_relocs;
916   if (! dynamic
917       && abfd->xvec == info->hash->creator
918       && check_relocs != NULL)
919     {
920       asection *o;
921
922       for (o = abfd->sections; o != NULL; o = o->next)
923         {
924           Elf_Internal_Rela *internal_relocs;
925           boolean ok;
926
927           if ((o->flags & SEC_RELOC) == 0
928               || o->reloc_count == 0)
929             continue;
930
931           /* I believe we can ignore the relocs for any section which
932              does not form part of the final process image, such as a
933              debugging section.  */
934           if ((o->flags & SEC_ALLOC) == 0)
935             continue;
936
937           internal_relocs = elf_link_read_relocs (abfd, o, (PTR) NULL,
938                                                   (Elf_Internal_Rela *) NULL,
939                                                   info->keep_memory);
940           if (internal_relocs == NULL)
941             goto error_return;
942
943           ok = (*check_relocs) (abfd, info, o, internal_relocs);
944
945           if (! info->keep_memory)
946             free (internal_relocs);
947
948           if (! ok)
949             goto error_return;
950         }
951     }
952
953   return true;
954
955  error_return:
956   if (buf != NULL)
957     free (buf);
958   if (dynbuf != NULL)
959     free (dynbuf);
960   return false;
961 }
962
963 /* Create some sections which will be filled in with dynamic linking
964    information.  ABFD is an input file which requires dynamic sections
965    to be created.  The dynamic sections take up virtual memory space
966    when the final executable is run, so we need to create them before
967    addresses are assigned to the output sections.  We work out the
968    actual contents and size of these sections later.  */
969
970 boolean
971 elf_link_create_dynamic_sections (abfd, info)
972      bfd *abfd;
973      struct bfd_link_info *info;
974 {
975   flagword flags;
976   register asection *s;
977   struct elf_link_hash_entry *h;
978   struct elf_backend_data *bed;
979
980   if (elf_hash_table (info)->dynamic_sections_created)
981     return true;
982
983   /* Make sure that all dynamic sections use the same input BFD.  */
984   if (elf_hash_table (info)->dynobj == NULL)
985     elf_hash_table (info)->dynobj = abfd;
986   else
987     abfd = elf_hash_table (info)->dynobj;
988
989   /* Note that we set the SEC_IN_MEMORY flag for all of these
990      sections.  */
991   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
992
993   /* A dynamically linked executable has a .interp section, but a
994      shared library does not.  */
995   if (! info->shared)
996     {
997       s = bfd_make_section (abfd, ".interp");
998       if (s == NULL
999           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
1000         return false;
1001     }
1002
1003   s = bfd_make_section (abfd, ".dynsym");
1004   if (s == NULL
1005       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1006       || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
1007     return false;
1008
1009   s = bfd_make_section (abfd, ".dynstr");
1010   if (s == NULL
1011       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
1012     return false;
1013
1014   /* Create a strtab to hold the dynamic symbol names.  */
1015   if (elf_hash_table (info)->dynstr == NULL)
1016     {
1017       elf_hash_table (info)->dynstr = elf_stringtab_init ();
1018       if (elf_hash_table (info)->dynstr == NULL)
1019         return false;
1020     }
1021
1022   s = bfd_make_section (abfd, ".dynamic");
1023   if (s == NULL
1024       || ! bfd_set_section_flags (abfd, s, flags)
1025       || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
1026     return false;
1027
1028   /* The special symbol _DYNAMIC is always set to the start of the
1029      .dynamic section.  This call occurs before we have processed the
1030      symbols for any dynamic object, so we don't have to worry about
1031      overriding a dynamic definition.  We could set _DYNAMIC in a
1032      linker script, but we only want to define it if we are, in fact,
1033      creating a .dynamic section.  We don't want to define it if there
1034      is no .dynamic section, since on some ELF platforms the start up
1035      code examines it to decide how to initialize the process.  */
1036   h = NULL;
1037   if (! (_bfd_generic_link_add_one_symbol
1038          (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
1039           (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
1040           (struct bfd_link_hash_entry **) &h)))
1041     return false;
1042   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1043   h->type = STT_OBJECT;
1044
1045   if (info->shared
1046       && ! _bfd_elf_link_record_dynamic_symbol (info, h))
1047     return false;
1048
1049   s = bfd_make_section (abfd, ".hash");
1050   if (s == NULL
1051       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1052       || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
1053     return false;
1054
1055   /* Let the backend create the rest of the sections.  This lets the
1056      backend set the right flags.  The backend will normally create
1057      the .got and .plt sections.  */
1058   bed = get_elf_backend_data (abfd);
1059   if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
1060     return false;
1061
1062   elf_hash_table (info)->dynamic_sections_created = true;
1063
1064   return true;
1065 }
1066
1067 /* Add an entry to the .dynamic table.  */
1068
1069 boolean
1070 elf_add_dynamic_entry (info, tag, val)
1071      struct bfd_link_info *info;
1072      bfd_vma tag;
1073      bfd_vma val;
1074 {
1075   Elf_Internal_Dyn dyn;
1076   bfd *dynobj;
1077   asection *s;
1078   size_t newsize;
1079   bfd_byte *newcontents;
1080
1081   dynobj = elf_hash_table (info)->dynobj;
1082
1083   s = bfd_get_section_by_name (dynobj, ".dynamic");
1084   BFD_ASSERT (s != NULL);
1085
1086   newsize = s->_raw_size + sizeof (Elf_External_Dyn);
1087   newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
1088   if (newcontents == NULL)
1089     return false;
1090
1091   dyn.d_tag = tag;
1092   dyn.d_un.d_val = val;
1093   elf_swap_dyn_out (dynobj, &dyn,
1094                     (Elf_External_Dyn *) (newcontents + s->_raw_size));
1095
1096   s->_raw_size = newsize;
1097   s->contents = newcontents;
1098
1099   return true;
1100 }
1101
1102 /* Read and swap the relocs for a section.  They may have been cached.
1103    If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL,
1104    they are used as buffers to read into.  They are known to be large
1105    enough.  If the INTERNAL_RELOCS relocs argument is NULL, the return
1106    value is allocated using either malloc or bfd_alloc, according to
1107    the KEEP_MEMORY argument.  */
1108
1109 static Elf_Internal_Rela *
1110 elf_link_read_relocs (abfd, o, external_relocs, internal_relocs, keep_memory)
1111      bfd *abfd;
1112      asection *o;
1113      PTR external_relocs;
1114      Elf_Internal_Rela *internal_relocs;
1115      boolean keep_memory;
1116 {
1117   Elf_Internal_Shdr *rel_hdr;
1118   PTR alloc1 = NULL;
1119   Elf_Internal_Rela *alloc2 = NULL;
1120
1121   if (elf_section_data (o)->relocs != NULL)
1122     return elf_section_data (o)->relocs;
1123
1124   if (o->reloc_count == 0)
1125     return NULL;
1126
1127   rel_hdr = &elf_section_data (o)->rel_hdr;
1128
1129   if (internal_relocs == NULL)
1130     {
1131       size_t size;
1132
1133       size = o->reloc_count * sizeof (Elf_Internal_Rela);
1134       if (keep_memory)
1135         internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
1136       else
1137         internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
1138       if (internal_relocs == NULL)
1139         goto error_return;
1140     }
1141
1142   if (external_relocs == NULL)
1143     {
1144       alloc1 = (PTR) bfd_malloc ((size_t) rel_hdr->sh_size);
1145       if (alloc1 == NULL)
1146         goto error_return;
1147       external_relocs = alloc1;
1148     }
1149
1150   if ((bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
1151       || (bfd_read (external_relocs, 1, rel_hdr->sh_size, abfd)
1152           != rel_hdr->sh_size))
1153     goto error_return;
1154
1155   /* Swap in the relocs.  For convenience, we always produce an
1156      Elf_Internal_Rela array; if the relocs are Rel, we set the addend
1157      to 0.  */
1158   if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
1159     {
1160       Elf_External_Rel *erel;
1161       Elf_External_Rel *erelend;
1162       Elf_Internal_Rela *irela;
1163
1164       erel = (Elf_External_Rel *) external_relocs;
1165       erelend = erel + o->reloc_count;
1166       irela = internal_relocs;
1167       for (; erel < erelend; erel++, irela++)
1168         {
1169           Elf_Internal_Rel irel;
1170
1171           elf_swap_reloc_in (abfd, erel, &irel);
1172           irela->r_offset = irel.r_offset;
1173           irela->r_info = irel.r_info;
1174           irela->r_addend = 0;
1175         }
1176     }
1177   else
1178     {
1179       Elf_External_Rela *erela;
1180       Elf_External_Rela *erelaend;
1181       Elf_Internal_Rela *irela;
1182
1183       BFD_ASSERT (rel_hdr->sh_entsize == sizeof (Elf_External_Rela));
1184
1185       erela = (Elf_External_Rela *) external_relocs;
1186       erelaend = erela + o->reloc_count;
1187       irela = internal_relocs;
1188       for (; erela < erelaend; erela++, irela++)
1189         elf_swap_reloca_in (abfd, erela, irela);
1190     }
1191
1192   /* Cache the results for next time, if we can.  */
1193   if (keep_memory)
1194     elf_section_data (o)->relocs = internal_relocs;
1195                  
1196   if (alloc1 != NULL)
1197     free (alloc1);
1198
1199   /* Don't free alloc2, since if it was allocated we are passing it
1200      back (under the name of internal_relocs).  */
1201
1202   return internal_relocs;
1203
1204  error_return:
1205   if (alloc1 != NULL)
1206     free (alloc1);
1207   if (alloc2 != NULL)
1208     free (alloc2);
1209   return NULL;
1210 }
1211
1212 /* Record an assignment to a symbol made by a linker script.  We need
1213    this in case some dynamic object refers to this symbol.  */
1214
1215 /*ARGSUSED*/
1216 boolean
1217 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
1218      bfd *output_bfd;
1219      struct bfd_link_info *info;
1220      const char *name;
1221      boolean provide;
1222 {
1223   struct elf_link_hash_entry *h;
1224
1225   if (info->hash->creator->flavour != bfd_target_elf_flavour)
1226     return true;
1227
1228   h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
1229   if (h == NULL)
1230     return false;
1231
1232   /* If this symbol is being provided by the linker script, and it is
1233      currently defined by a dynamic object, but not by a regular
1234      object, then mark it as undefined so that the generic linker will
1235      force the correct value.  */
1236   if (provide
1237       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1238       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1239     h->root.type = bfd_link_hash_undefined;
1240
1241   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1242   h->type = STT_OBJECT;
1243
1244   if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
1245                                   | ELF_LINK_HASH_REF_DYNAMIC)) != 0
1246        || info->shared)
1247       && h->dynindx == -1)
1248     {
1249       if (! _bfd_elf_link_record_dynamic_symbol (info, h))
1250         return false;
1251
1252       /* If this is a weak defined symbol, and we know a corresponding
1253          real symbol from the same dynamic object, make sure the real
1254          symbol is also made into a dynamic symbol.  */
1255       if (h->weakdef != NULL
1256           && h->weakdef->dynindx == -1)
1257         {
1258           if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
1259             return false;
1260         }
1261     }
1262
1263   return true;
1264 }
1265
1266 /* Array used to determine the number of hash table buckets to use
1267    based on the number of symbols there are.  If there are fewer than
1268    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
1269    fewer than 37 we use 17 buckets, and so forth.  We never use more
1270    than 521 buckets.  */
1271
1272 static const size_t elf_buckets[] =
1273 {
1274   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 0
1275 };
1276
1277 /* Set up the sizes and contents of the ELF dynamic sections.  This is
1278    called by the ELF linker emulation before_allocation routine.  We
1279    must set the sizes of the sections before the linker sets the
1280    addresses of the various sections.  */
1281
1282 boolean
1283 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
1284                                      export_dynamic, info, sinterpptr)
1285      bfd *output_bfd;
1286      const char *soname;
1287      const char *rpath;
1288      boolean export_dynamic;
1289      struct bfd_link_info *info;
1290      asection **sinterpptr;
1291 {
1292   bfd *dynobj;
1293   struct elf_backend_data *bed;
1294
1295   *sinterpptr = NULL;
1296
1297   if (info->hash->creator->flavour != bfd_target_elf_flavour)
1298     return true;
1299
1300   dynobj = elf_hash_table (info)->dynobj;
1301
1302   /* If there were no dynamic objects in the link, there is nothing to
1303      do here.  */
1304   if (dynobj == NULL)
1305     return true;
1306
1307   /* If we are supposed to export all symbols into the dynamic symbol
1308      table (this is not the normal case), then do so.  */
1309   if (export_dynamic)
1310     {
1311       struct elf_info_failed eif;
1312
1313       eif.failed = false;
1314       eif.info = info;
1315       elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
1316                               (PTR) &eif);
1317       if (eif.failed)
1318         return false;
1319     }
1320
1321   if (elf_hash_table (info)->dynamic_sections_created)
1322     {
1323       struct elf_info_failed eif;
1324       struct elf_link_hash_entry *h;
1325       bfd_size_type strsize;
1326
1327       *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
1328       BFD_ASSERT (*sinterpptr != NULL || info->shared);
1329
1330       if (soname != NULL)
1331         {
1332           bfd_size_type indx;
1333
1334           indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, soname,
1335                                      true, true);
1336           if (indx == (bfd_size_type) -1
1337               || ! elf_add_dynamic_entry (info, DT_SONAME, indx))
1338             return false;
1339         }      
1340
1341       if (info->symbolic)
1342         {
1343           if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
1344             return false;
1345         }
1346
1347       if (rpath != NULL)
1348         {
1349           bfd_size_type indx;
1350
1351           indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, rpath,
1352                                      true, true);
1353           if (indx == (bfd_size_type) -1
1354               || ! elf_add_dynamic_entry (info, DT_RPATH, indx))
1355             return false;
1356         }
1357
1358       /* Find all symbols which were defined in a dynamic object and make
1359          the backend pick a reasonable value for them.  */
1360       eif.failed = false;
1361       eif.info = info;
1362       elf_link_hash_traverse (elf_hash_table (info),
1363                               elf_adjust_dynamic_symbol,
1364                               (PTR) &eif);
1365       if (eif.failed)
1366         return false;
1367
1368       /* Add some entries to the .dynamic section.  We fill in some of the
1369          values later, in elf_bfd_final_link, but we must add the entries
1370          now so that we know the final size of the .dynamic section.  */
1371       h =  elf_link_hash_lookup (elf_hash_table (info), "_init", false,
1372                                 false, false);
1373       if (h != NULL
1374           && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
1375                                         | ELF_LINK_HASH_DEF_REGULAR)) != 0)
1376         {
1377           if (! elf_add_dynamic_entry (info, DT_INIT, 0))
1378             return false;
1379         }
1380       h =  elf_link_hash_lookup (elf_hash_table (info), "_fini", false,
1381                                  false, false);
1382       if (h != NULL
1383           && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
1384                                         | ELF_LINK_HASH_DEF_REGULAR)) != 0)
1385         {
1386           if (! elf_add_dynamic_entry (info, DT_FINI, 0))
1387             return false;
1388         }
1389       strsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
1390       if (! elf_add_dynamic_entry (info, DT_HASH, 0)
1391           || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
1392           || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
1393           || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize)
1394           || ! elf_add_dynamic_entry (info, DT_SYMENT,
1395                                       sizeof (Elf_External_Sym)))
1396         return false;
1397     }
1398
1399   /* The backend must work out the sizes of all the other dynamic
1400      sections.  */
1401   bed = get_elf_backend_data (output_bfd);
1402   if (! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
1403     return false;
1404
1405   if (elf_hash_table (info)->dynamic_sections_created)
1406     {
1407       size_t dynsymcount;
1408       asection *s;
1409       size_t i;
1410       size_t bucketcount = 0;
1411       Elf_Internal_Sym isym;
1412
1413       /* Set the size of the .dynsym and .hash sections.  We counted
1414          the number of dynamic symbols in elf_link_add_object_symbols.
1415          We will build the contents of .dynsym and .hash when we build
1416          the final symbol table, because until then we do not know the
1417          correct value to give the symbols.  We built the .dynstr
1418          section as we went along in elf_link_add_object_symbols.  */
1419       dynsymcount = elf_hash_table (info)->dynsymcount;
1420       s = bfd_get_section_by_name (dynobj, ".dynsym");
1421       BFD_ASSERT (s != NULL);
1422       s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
1423       s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
1424       if (s->contents == NULL && s->_raw_size != 0)
1425         return false;
1426
1427       /* The first entry in .dynsym is a dummy symbol.  */
1428       isym.st_value = 0;
1429       isym.st_size = 0;
1430       isym.st_name = 0;
1431       isym.st_info = 0;
1432       isym.st_other = 0;
1433       isym.st_shndx = 0;
1434       elf_swap_symbol_out (output_bfd, &isym,
1435                            (PTR) (Elf_External_Sym *) s->contents);
1436
1437       for (i = 0; elf_buckets[i] != 0; i++)
1438         {
1439           bucketcount = elf_buckets[i];
1440           if (dynsymcount < elf_buckets[i + 1])
1441             break;
1442         }
1443
1444       s = bfd_get_section_by_name (dynobj, ".hash");
1445       BFD_ASSERT (s != NULL);
1446       s->_raw_size = (2 + bucketcount + dynsymcount) * (ARCH_SIZE / 8);
1447       s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
1448       if (s->contents == NULL)
1449         return false;
1450       memset (s->contents, 0, (size_t) s->_raw_size);
1451
1452       put_word (output_bfd, bucketcount, s->contents);
1453       put_word (output_bfd, dynsymcount, s->contents + (ARCH_SIZE / 8));
1454
1455       elf_hash_table (info)->bucketcount = bucketcount;
1456
1457       s = bfd_get_section_by_name (dynobj, ".dynstr");
1458       BFD_ASSERT (s != NULL);
1459       s->_raw_size = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
1460
1461       if (! elf_add_dynamic_entry (info, DT_NULL, 0))
1462         return false;
1463     }
1464
1465   return true;
1466 }
1467
1468 /* This routine is used to export all defined symbols into the dynamic
1469    symbol table.  It is called via elf_link_hash_traverse.  */
1470
1471 static boolean
1472 elf_export_symbol (h, data)
1473      struct elf_link_hash_entry *h;
1474      PTR data;
1475 {
1476   struct elf_info_failed *eif = (struct elf_info_failed *) data;
1477
1478   if (h->dynindx == -1
1479       && (h->elf_link_hash_flags
1480           & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1481     {
1482       if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
1483         {
1484           eif->failed = true;
1485           return false;
1486         }
1487     }
1488
1489   return true;
1490 }
1491
1492 /* Make the backend pick a good value for a dynamic symbol.  This is
1493    called via elf_link_hash_traverse, and also calls itself
1494    recursively.  */
1495
1496 static boolean
1497 elf_adjust_dynamic_symbol (h, data)
1498      struct elf_link_hash_entry *h;
1499      PTR data;
1500 {
1501   struct elf_info_failed *eif = (struct elf_info_failed *) data;
1502   bfd *dynobj;
1503   struct elf_backend_data *bed;
1504
1505   /* If -Bsymbolic was used (which means to bind references to global
1506      symbols to the definition within the shared object), and this
1507      symbol was defined in a regular object, then it actually doesn't
1508      need a PLT entry.  */
1509   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
1510       && eif->info->shared
1511       && eif->info->symbolic
1512       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
1513     h->elf_link_hash_flags &=~ ELF_LINK_HASH_NEEDS_PLT;
1514
1515   /* If this symbol does not require a PLT entry, and it is not
1516      defined by a dynamic object, or is not referenced by a regular
1517      object, ignore it.  We do have to handle a weak defined symbol,
1518      even if no regular object refers to it, if we decided to add it
1519      to the dynamic symbol table.  FIXME: Do we normally need to worry
1520      about symbols which are defined by one dynamic object and
1521      referenced by another one?  */
1522   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
1523       && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1524           || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1525           || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
1526               && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
1527     return true;
1528
1529   /* If we've already adjusted this symbol, don't do it again.  This
1530      can happen via a recursive call.  */
1531   if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
1532     return true;
1533
1534   /* Don't look at this symbol again.  Note that we must set this
1535      after checking the above conditions, because we may look at a
1536      symbol once, decide not to do anything, and then get called
1537      recursively later after REF_REGULAR is set below.  */
1538   h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
1539
1540   /* If this is a weak definition, and we know a real definition, and
1541      the real symbol is not itself defined by a regular object file,
1542      then get a good value for the real definition.  We handle the
1543      real symbol first, for the convenience of the backend routine.
1544
1545      Note that there is a confusing case here.  If the real definition
1546      is defined by a regular object file, we don't get the real symbol
1547      from the dynamic object, but we do get the weak symbol.  If the
1548      processor backend uses a COPY reloc, then if some routine in the
1549      dynamic object changes the real symbol, we will not see that
1550      change in the corresponding weak symbol.  This is the way other
1551      ELF linkers work as well, and seems to be a result of the shared
1552      library model.
1553
1554      I will clarify this issue.  Most SVR4 shared libraries define the
1555      variable _timezone and define timezone as a weak synonym.  The
1556      tzset call changes _timezone.  If you write
1557        extern int timezone;
1558        int _timezone = 5;
1559        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
1560      you might expect that, since timezone is a synonym for _timezone,
1561      the same number will print both times.  However, if the processor
1562      backend uses a COPY reloc, then actually timezone will be copied
1563      into your process image, and, since you define _timezone
1564      yourself, _timezone will not.  Thus timezone and _timezone will
1565      wind up at different memory locations.  The tzset call will set
1566      _timezone, leaving timezone unchanged.  */
1567
1568   if (h->weakdef != NULL)
1569     {
1570       struct elf_link_hash_entry *weakdef;
1571
1572       BFD_ASSERT (h->root.type == bfd_link_hash_defined
1573                   || h->root.type == bfd_link_hash_defweak);
1574       weakdef = h->weakdef;
1575       BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
1576                   || weakdef->root.type == bfd_link_hash_defweak);
1577       BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
1578       if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
1579         {
1580           /* This symbol is defined by a regular object file, so we
1581              will not do anything special.  Clear weakdef for the
1582              convenience of the processor backend.  */
1583           h->weakdef = NULL;
1584         }
1585       else
1586         {
1587           /* There is an implicit reference by a regular object file
1588              via the weak symbol.  */
1589           weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
1590           if (! elf_adjust_dynamic_symbol (weakdef, (PTR) eif))
1591             return false;
1592         }
1593     }
1594
1595   dynobj = elf_hash_table (eif->info)->dynobj;
1596   bed = get_elf_backend_data (dynobj);
1597   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
1598     {
1599       eif->failed = true;
1600       return false;
1601     }
1602
1603   return true;
1604 }
1605 \f
1606 /* Final phase of ELF linker.  */
1607
1608 /* A structure we use to avoid passing large numbers of arguments.  */
1609
1610 struct elf_final_link_info
1611 {
1612   /* General link information.  */
1613   struct bfd_link_info *info;
1614   /* Output BFD.  */
1615   bfd *output_bfd;
1616   /* Symbol string table.  */
1617   struct bfd_strtab_hash *symstrtab;
1618   /* .dynsym section.  */
1619   asection *dynsym_sec;
1620   /* .hash section.  */
1621   asection *hash_sec;
1622   /* Buffer large enough to hold contents of any section.  */
1623   bfd_byte *contents;
1624   /* Buffer large enough to hold external relocs of any section.  */
1625   PTR external_relocs;
1626   /* Buffer large enough to hold internal relocs of any section.  */
1627   Elf_Internal_Rela *internal_relocs;
1628   /* Buffer large enough to hold external local symbols of any input
1629      BFD.  */
1630   Elf_External_Sym *external_syms;
1631   /* Buffer large enough to hold internal local symbols of any input
1632      BFD.  */
1633   Elf_Internal_Sym *internal_syms;
1634   /* Array large enough to hold a symbol index for each local symbol
1635      of any input BFD.  */
1636   long *indices;
1637   /* Array large enough to hold a section pointer for each local
1638      symbol of any input BFD.  */
1639   asection **sections;
1640   /* Buffer to hold swapped out symbols.  */
1641   Elf_External_Sym *symbuf;
1642   /* Number of swapped out symbols in buffer.  */
1643   size_t symbuf_count;
1644   /* Number of symbols which fit in symbuf.  */
1645   size_t symbuf_size;
1646 };
1647
1648 static boolean elf_link_output_sym
1649   PARAMS ((struct elf_final_link_info *, const char *,
1650            Elf_Internal_Sym *, asection *));
1651 static boolean elf_link_flush_output_syms
1652   PARAMS ((struct elf_final_link_info *));
1653 static boolean elf_link_output_extsym
1654   PARAMS ((struct elf_link_hash_entry *, PTR));
1655 static boolean elf_link_input_bfd
1656   PARAMS ((struct elf_final_link_info *, bfd *));
1657 static boolean elf_reloc_link_order
1658   PARAMS ((bfd *, struct bfd_link_info *, asection *,
1659            struct bfd_link_order *));
1660
1661 /* This struct is used to pass information to routines called via
1662    elf_link_hash_traverse which must return failure.  */
1663
1664 struct elf_finfo_failed
1665 {
1666   boolean failed;
1667   struct elf_final_link_info *finfo;
1668 };  
1669
1670 /* Do the final step of an ELF link.  */
1671
1672 boolean
1673 elf_bfd_final_link (abfd, info)
1674      bfd *abfd;
1675      struct bfd_link_info *info;
1676 {
1677   boolean dynamic;
1678   bfd *dynobj;
1679   struct elf_final_link_info finfo;
1680   register asection *o;
1681   register struct bfd_link_order *p;
1682   register bfd *sub;
1683   size_t max_contents_size;
1684   size_t max_external_reloc_size;
1685   size_t max_internal_reloc_count;
1686   size_t max_sym_count;
1687   file_ptr off;
1688   Elf_Internal_Sym elfsym;
1689   unsigned int i;
1690   Elf_Internal_Shdr *symtab_hdr;
1691   Elf_Internal_Shdr *symstrtab_hdr;
1692   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1693   struct elf_finfo_failed eif;
1694
1695   if (info->shared)
1696     abfd->flags |= DYNAMIC;
1697
1698   dynamic = elf_hash_table (info)->dynamic_sections_created;
1699   dynobj = elf_hash_table (info)->dynobj;
1700
1701   finfo.info = info;
1702   finfo.output_bfd = abfd;
1703   finfo.symstrtab = elf_stringtab_init ();
1704   if (finfo.symstrtab == NULL)
1705     return false;
1706   if (! dynamic)
1707     {
1708       finfo.dynsym_sec = NULL;
1709       finfo.hash_sec = NULL;
1710     }
1711   else
1712     {
1713       finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
1714       finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
1715       BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
1716     }
1717   finfo.contents = NULL;
1718   finfo.external_relocs = NULL;
1719   finfo.internal_relocs = NULL;
1720   finfo.external_syms = NULL;
1721   finfo.internal_syms = NULL;
1722   finfo.indices = NULL;
1723   finfo.sections = NULL;
1724   finfo.symbuf = NULL;
1725   finfo.symbuf_count = 0;
1726
1727   /* Count up the number of relocations we will output for each output
1728      section, so that we know the sizes of the reloc sections.  We
1729      also figure out some maximum sizes.  */
1730   max_contents_size = 0;
1731   max_external_reloc_size = 0;
1732   max_internal_reloc_count = 0;
1733   max_sym_count = 0;
1734   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
1735     {
1736       o->reloc_count = 0;
1737
1738       for (p = o->link_order_head; p != NULL; p = p->next)
1739         {
1740           if (p->type == bfd_section_reloc_link_order
1741               || p->type == bfd_symbol_reloc_link_order)
1742             ++o->reloc_count;
1743           else if (p->type == bfd_indirect_link_order)
1744             {
1745               asection *sec;
1746
1747               sec = p->u.indirect.section;
1748
1749               if (info->relocateable)
1750                 o->reloc_count += sec->reloc_count;
1751
1752               if (sec->_raw_size > max_contents_size)
1753                 max_contents_size = sec->_raw_size;
1754               if (sec->_cooked_size > max_contents_size)
1755                 max_contents_size = sec->_cooked_size;
1756
1757               /* We are interested in just local symbols, not all
1758                  symbols.  */
1759               if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour)
1760                 {
1761                   size_t sym_count;
1762
1763                   if (elf_bad_symtab (sec->owner))
1764                     sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
1765                                  / sizeof (Elf_External_Sym));
1766                   else
1767                     sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
1768
1769                   if (sym_count > max_sym_count)
1770                     max_sym_count = sym_count;
1771
1772                   if ((sec->flags & SEC_RELOC) != 0)
1773                     {
1774                       size_t ext_size;
1775
1776                       ext_size = elf_section_data (sec)->rel_hdr.sh_size;
1777                       if (ext_size > max_external_reloc_size)
1778                         max_external_reloc_size = ext_size;
1779                       if (sec->reloc_count > max_internal_reloc_count)
1780                         max_internal_reloc_count = sec->reloc_count;
1781                     }
1782                 }
1783             }
1784         }
1785
1786       if (o->reloc_count > 0)
1787         o->flags |= SEC_RELOC;
1788       else
1789         {
1790           /* Explicitly clear the SEC_RELOC flag.  The linker tends to
1791              set it (this is probably a bug) and if it is set
1792              assign_section_numbers will create a reloc section.  */
1793           o->flags &=~ SEC_RELOC;
1794         }
1795
1796       /* If the SEC_ALLOC flag is not set, force the section VMA to
1797          zero.  This is done in elf_fake_sections as well, but forcing
1798          the VMA to 0 here will ensure that relocs against these
1799          sections are handled correctly.  */
1800       if ((o->flags & SEC_ALLOC) == 0)
1801         o->vma = 0;
1802     }
1803
1804   /* Figure out the file positions for everything but the symbol table
1805      and the relocs.  We set symcount to force assign_section_numbers
1806      to create a symbol table.  */
1807   abfd->symcount = info->strip == strip_all ? 0 : 1;
1808   BFD_ASSERT (! abfd->output_has_begun);
1809   if (! _bfd_elf_compute_section_file_positions (abfd, info))
1810     goto error_return;
1811
1812   /* That created the reloc sections.  Set their sizes, and assign
1813      them file positions, and allocate some buffers.  */
1814   for (o = abfd->sections; o != NULL; o = o->next)
1815     {
1816       if ((o->flags & SEC_RELOC) != 0)
1817         {
1818           Elf_Internal_Shdr *rel_hdr;
1819           register struct elf_link_hash_entry **p, **pend;
1820
1821           rel_hdr = &elf_section_data (o)->rel_hdr;
1822
1823           rel_hdr->sh_size = rel_hdr->sh_entsize * o->reloc_count;
1824
1825           /* The contents field must last into write_object_contents,
1826              so we allocate it with bfd_alloc rather than malloc.  */
1827           rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size);
1828           if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
1829             goto error_return;
1830
1831           p = ((struct elf_link_hash_entry **)
1832                bfd_malloc (o->reloc_count
1833                            * sizeof (struct elf_link_hash_entry *)));
1834           if (p == NULL && o->reloc_count != 0)
1835             goto error_return;
1836           elf_section_data (o)->rel_hashes = p;
1837           pend = p + o->reloc_count;
1838           for (; p < pend; p++)
1839             *p = NULL;
1840
1841           /* Use the reloc_count field as an index when outputting the
1842              relocs.  */
1843           o->reloc_count = 0;
1844         }
1845     }
1846
1847   _bfd_elf_assign_file_positions_for_relocs (abfd);
1848
1849   /* We have now assigned file positions for all the sections except
1850      .symtab and .strtab.  We start the .symtab section at the current
1851      file position, and write directly to it.  We build the .strtab
1852      section in memory.  */
1853   abfd->symcount = 0;
1854   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1855   /* sh_name is set in prep_headers.  */
1856   symtab_hdr->sh_type = SHT_SYMTAB;
1857   symtab_hdr->sh_flags = 0;
1858   symtab_hdr->sh_addr = 0;
1859   symtab_hdr->sh_size = 0;
1860   symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
1861   /* sh_link is set in assign_section_numbers.  */
1862   /* sh_info is set below.  */
1863   /* sh_offset is set just below.  */
1864   symtab_hdr->sh_addralign = 4;  /* FIXME: system dependent?  */
1865
1866   off = elf_tdata (abfd)->next_file_pos;
1867   off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
1868
1869   /* Note that at this point elf_tdata (abfd)->next_file_pos is
1870      incorrect.  We do not yet know the size of the .symtab section.
1871      We correct next_file_pos below, after we do know the size.  */
1872
1873   /* Allocate a buffer to hold swapped out symbols.  This is to avoid
1874      continuously seeking to the right position in the file.  */
1875   if (! info->keep_memory || max_sym_count < 20)
1876     finfo.symbuf_size = 20;
1877   else
1878     finfo.symbuf_size = max_sym_count;
1879   finfo.symbuf = ((Elf_External_Sym *)
1880                   bfd_malloc (finfo.symbuf_size * sizeof (Elf_External_Sym)));
1881   if (finfo.symbuf == NULL)
1882     goto error_return;
1883
1884   /* Start writing out the symbol table.  The first symbol is always a
1885      dummy symbol.  */
1886   elfsym.st_value = 0;
1887   elfsym.st_size = 0;
1888   elfsym.st_info = 0;
1889   elfsym.st_other = 0;
1890   elfsym.st_shndx = SHN_UNDEF;
1891   if (! elf_link_output_sym (&finfo, (const char *) NULL,
1892                              &elfsym, bfd_und_section_ptr))
1893     goto error_return;
1894
1895 #if 0
1896   /* Some standard ELF linkers do this, but we don't because it causes
1897      bootstrap comparison failures.  */
1898   /* Output a file symbol for the output file as the second symbol.
1899      We output this even if we are discarding local symbols, although
1900      I'm not sure if this is correct.  */
1901   elfsym.st_value = 0;
1902   elfsym.st_size = 0;
1903   elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
1904   elfsym.st_other = 0;
1905   elfsym.st_shndx = SHN_ABS;
1906   if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
1907                              &elfsym, bfd_abs_section_ptr))
1908     goto error_return;
1909 #endif
1910
1911   /* Output a symbol for each section.  We output these even if we are
1912      discarding local symbols, since they are used for relocs.  These
1913      symbols have no names.  We store the index of each one in the
1914      index field of the section, so that we can find it again when
1915      outputting relocs.  */
1916   elfsym.st_value = 0;
1917   elfsym.st_size = 0;
1918   elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
1919   elfsym.st_other = 0;
1920   for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
1921     {
1922       o = section_from_elf_index (abfd, i);
1923       if (o != NULL)
1924         o->target_index = abfd->symcount;
1925       elfsym.st_shndx = i;
1926       if (! elf_link_output_sym (&finfo, (const char *) NULL,
1927                                  &elfsym, o))
1928         goto error_return;
1929     }
1930
1931   /* Allocate some memory to hold information read in from the input
1932      files.  */
1933   finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
1934   finfo.external_relocs = (PTR) bfd_malloc (max_external_reloc_size);
1935   finfo.internal_relocs = ((Elf_Internal_Rela *)
1936                            bfd_malloc (max_internal_reloc_count
1937                                        * sizeof (Elf_Internal_Rela)));
1938   finfo.external_syms = ((Elf_External_Sym *)
1939                          bfd_malloc (max_sym_count
1940                                      * sizeof (Elf_External_Sym)));
1941   finfo.internal_syms = ((Elf_Internal_Sym *)
1942                          bfd_malloc (max_sym_count
1943                                      * sizeof (Elf_Internal_Sym)));
1944   finfo.indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
1945   finfo.sections = ((asection **)
1946                     bfd_malloc (max_sym_count * sizeof (asection *)));
1947   if ((finfo.contents == NULL && max_contents_size != 0)
1948       || (finfo.external_relocs == NULL && max_external_reloc_size != 0)
1949       || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0)
1950       || (finfo.external_syms == NULL && max_sym_count != 0)
1951       || (finfo.internal_syms == NULL && max_sym_count != 0)
1952       || (finfo.indices == NULL && max_sym_count != 0)
1953       || (finfo.sections == NULL && max_sym_count != 0))
1954     goto error_return;
1955
1956   /* Since ELF permits relocations to be against local symbols, we
1957      must have the local symbols available when we do the relocations.
1958      Since we would rather only read the local symbols once, and we
1959      would rather not keep them in memory, we handle all the
1960      relocations for a single input file at the same time.
1961
1962      Unfortunately, there is no way to know the total number of local
1963      symbols until we have seen all of them, and the local symbol
1964      indices precede the global symbol indices.  This means that when
1965      we are generating relocateable output, and we see a reloc against
1966      a global symbol, we can not know the symbol index until we have
1967      finished examining all the local symbols to see which ones we are
1968      going to output.  To deal with this, we keep the relocations in
1969      memory, and don't output them until the end of the link.  This is
1970      an unfortunate waste of memory, but I don't see a good way around
1971      it.  Fortunately, it only happens when performing a relocateable
1972      link, which is not the common case.  FIXME: If keep_memory is set
1973      we could write the relocs out and then read them again; I don't
1974      know how bad the memory loss will be.  */
1975
1976   for (sub = info->input_bfds; sub != NULL; sub = sub->next)
1977     sub->output_has_begun = false;
1978   for (o = abfd->sections; o != NULL; o = o->next)
1979     {
1980       for (p = o->link_order_head; p != NULL; p = p->next)
1981         {
1982           if (p->type == bfd_indirect_link_order
1983               && (bfd_get_flavour (p->u.indirect.section->owner)
1984                   == bfd_target_elf_flavour))
1985             {
1986               sub = p->u.indirect.section->owner;
1987               if (! sub->output_has_begun)
1988                 {
1989                   if (! elf_link_input_bfd (&finfo, sub))
1990                     goto error_return;
1991                   sub->output_has_begun = true;
1992                 }
1993             }
1994           else if (p->type == bfd_section_reloc_link_order
1995                    || p->type == bfd_symbol_reloc_link_order)
1996             {
1997               if (! elf_reloc_link_order (abfd, info, o, p))
1998                 goto error_return;
1999             }
2000           else
2001             {
2002               if (! _bfd_default_link_order (abfd, info, o, p))
2003                 goto error_return;
2004             }
2005         }
2006     }
2007
2008   /* That wrote out all the local symbols.  Finish up the symbol table
2009      with the global symbols.  */
2010
2011   /* The sh_info field records the index of the first non local
2012      symbol.  */
2013   symtab_hdr->sh_info = abfd->symcount;
2014   if (dynamic)
2015     elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = 1;
2016
2017   /* We get the global symbols from the hash table.  */
2018   eif.failed = false;
2019   eif.finfo = &finfo;
2020   elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
2021                           (PTR) &eif);
2022   if (eif.failed)
2023     return false;
2024
2025   /* Flush all symbols to the file.  */
2026   if (! elf_link_flush_output_syms (&finfo))
2027     return false;
2028
2029   /* Now we know the size of the symtab section.  */
2030   off += symtab_hdr->sh_size;
2031
2032   /* Finish up and write out the symbol string table (.strtab)
2033      section.  */
2034   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2035   /* sh_name was set in prep_headers.  */
2036   symstrtab_hdr->sh_type = SHT_STRTAB;
2037   symstrtab_hdr->sh_flags = 0;
2038   symstrtab_hdr->sh_addr = 0;
2039   symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
2040   symstrtab_hdr->sh_entsize = 0;
2041   symstrtab_hdr->sh_link = 0;
2042   symstrtab_hdr->sh_info = 0;
2043   /* sh_offset is set just below.  */
2044   symstrtab_hdr->sh_addralign = 1;
2045
2046   off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, true);
2047   elf_tdata (abfd)->next_file_pos = off;
2048
2049   if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
2050       || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
2051     return false;
2052
2053   /* Adjust the relocs to have the correct symbol indices.  */
2054   for (o = abfd->sections; o != NULL; o = o->next)
2055     {
2056       struct elf_link_hash_entry **rel_hash;
2057       Elf_Internal_Shdr *rel_hdr;
2058
2059       if ((o->flags & SEC_RELOC) == 0)
2060         continue;
2061
2062       rel_hash = elf_section_data (o)->rel_hashes;
2063       rel_hdr = &elf_section_data (o)->rel_hdr;
2064       for (i = 0; i < o->reloc_count; i++, rel_hash++)
2065         {
2066           if (*rel_hash == NULL)
2067             continue;
2068               
2069           BFD_ASSERT ((*rel_hash)->indx >= 0);
2070
2071           if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
2072             {
2073               Elf_External_Rel *erel;
2074               Elf_Internal_Rel irel;
2075
2076               erel = (Elf_External_Rel *) rel_hdr->contents + i;
2077               elf_swap_reloc_in (abfd, erel, &irel);
2078               irel.r_info = ELF_R_INFO ((*rel_hash)->indx,
2079                                         ELF_R_TYPE (irel.r_info));
2080               elf_swap_reloc_out (abfd, &irel, erel);
2081             }
2082           else
2083             {
2084               Elf_External_Rela *erela;
2085               Elf_Internal_Rela irela;
2086
2087               BFD_ASSERT (rel_hdr->sh_entsize
2088                           == sizeof (Elf_External_Rela));
2089
2090               erela = (Elf_External_Rela *) rel_hdr->contents + i;
2091               elf_swap_reloca_in (abfd, erela, &irela);
2092               irela.r_info = ELF_R_INFO ((*rel_hash)->indx,
2093                                          ELF_R_TYPE (irela.r_info));
2094               elf_swap_reloca_out (abfd, &irela, erela);
2095             }
2096         }
2097
2098       /* Set the reloc_count field to 0 to prevent write_relocs from
2099          trying to swap the relocs out itself.  */
2100       o->reloc_count = 0;
2101     }
2102
2103   /* If we are linking against a dynamic object, or generating a
2104      shared library, finish up the dynamic linking information.  */
2105   if (dynamic)
2106     {
2107       Elf_External_Dyn *dyncon, *dynconend;
2108
2109       /* Fix up .dynamic entries.  */
2110       o = bfd_get_section_by_name (dynobj, ".dynamic");
2111       BFD_ASSERT (o != NULL);
2112
2113       dyncon = (Elf_External_Dyn *) o->contents;
2114       dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
2115       for (; dyncon < dynconend; dyncon++)
2116         {
2117           Elf_Internal_Dyn dyn;
2118           const char *name;
2119           unsigned int type;
2120
2121           elf_swap_dyn_in (dynobj, dyncon, &dyn);
2122
2123           switch (dyn.d_tag)
2124             {
2125             default:
2126               break;
2127
2128               /* SVR4 linkers seem to set DT_INIT and DT_FINI based on
2129                  magic _init and _fini symbols.  This is pretty ugly,
2130                  but we are compatible.  */
2131             case DT_INIT:
2132               name = "_init";
2133               goto get_sym;
2134             case DT_FINI:
2135               name = "_fini";
2136             get_sym:
2137               {
2138                 struct elf_link_hash_entry *h;
2139
2140                 h = elf_link_hash_lookup (elf_hash_table (info), name,
2141                                           false, false, true);
2142                 if (h != NULL
2143                     && (h->root.type == bfd_link_hash_defined
2144                         || h->root.type == bfd_link_hash_defweak))
2145                   {
2146                     dyn.d_un.d_val = h->root.u.def.value;
2147                     o = h->root.u.def.section;
2148                     if (o->output_section != NULL)
2149                       dyn.d_un.d_val += (o->output_section->vma
2150                                          + o->output_offset);
2151                     else
2152                       {
2153                         /* The symbol is imported from another shared
2154                            library and does not apply to this one.  */
2155                         dyn.d_un.d_val = 0;
2156                       }
2157
2158                     elf_swap_dyn_out (dynobj, &dyn, dyncon);
2159                   }
2160               }
2161               break;
2162
2163             case DT_HASH:
2164               name = ".hash";
2165               goto get_vma;
2166             case DT_STRTAB:
2167               name = ".dynstr";
2168               goto get_vma;
2169             case DT_SYMTAB:
2170               name = ".dynsym";
2171             get_vma:
2172               o = bfd_get_section_by_name (abfd, name);
2173               BFD_ASSERT (o != NULL);
2174               dyn.d_un.d_ptr = o->vma;
2175               elf_swap_dyn_out (dynobj, &dyn, dyncon);
2176               break;
2177
2178             case DT_REL:
2179             case DT_RELA:
2180             case DT_RELSZ:
2181             case DT_RELASZ:
2182               if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
2183                 type = SHT_REL;
2184               else
2185                 type = SHT_RELA;
2186               dyn.d_un.d_val = 0;
2187               for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
2188                 {
2189                   Elf_Internal_Shdr *hdr;
2190
2191                   hdr = elf_elfsections (abfd)[i];
2192                   if (hdr->sh_type == type
2193                       && (hdr->sh_flags & SHF_ALLOC) != 0)
2194                     {
2195                       if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
2196                         dyn.d_un.d_val += hdr->sh_size;
2197                       else
2198                         {
2199                           if (dyn.d_un.d_val == 0
2200                               || hdr->sh_addr < dyn.d_un.d_val)
2201                             dyn.d_un.d_val = hdr->sh_addr;
2202                         }
2203                     }
2204                 }
2205               elf_swap_dyn_out (dynobj, &dyn, dyncon);
2206               break;
2207             }
2208         }
2209     }
2210
2211   /* If we have created any dynamic sections, then output them.  */
2212   if (dynobj != NULL)
2213     {
2214       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
2215         goto error_return;
2216
2217       for (o = dynobj->sections; o != NULL; o = o->next)
2218         {
2219           if ((o->flags & SEC_HAS_CONTENTS) == 0
2220               || o->_raw_size == 0)
2221             continue;
2222           if ((o->flags & SEC_IN_MEMORY) == 0)
2223             {
2224               /* At this point, we are only interested in sections
2225                  created by elf_link_create_dynamic_sections.  FIXME:
2226                  This test is fragile.  */
2227               continue;
2228             }
2229           if ((elf_section_data (o->output_section)->this_hdr.sh_type
2230                != SHT_STRTAB)
2231               || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
2232             {
2233               if (! bfd_set_section_contents (abfd, o->output_section,
2234                                               o->contents, o->output_offset,
2235                                               o->_raw_size))
2236                 goto error_return;
2237             }
2238           else
2239             {
2240               file_ptr off;
2241
2242               /* The contents of the .dynstr section are actually in a
2243                  stringtab.  */
2244               off = elf_section_data (o->output_section)->this_hdr.sh_offset;
2245               if (bfd_seek (abfd, off, SEEK_SET) != 0
2246                   || ! _bfd_stringtab_emit (abfd,
2247                                             elf_hash_table (info)->dynstr))
2248                 goto error_return;
2249             }
2250         }
2251     }
2252
2253   if (finfo.symstrtab != NULL)
2254     _bfd_stringtab_free (finfo.symstrtab);
2255   if (finfo.contents != NULL)
2256     free (finfo.contents);
2257   if (finfo.external_relocs != NULL)
2258     free (finfo.external_relocs);
2259   if (finfo.internal_relocs != NULL)
2260     free (finfo.internal_relocs);
2261   if (finfo.external_syms != NULL)
2262     free (finfo.external_syms);
2263   if (finfo.internal_syms != NULL)
2264     free (finfo.internal_syms);
2265   if (finfo.indices != NULL)
2266     free (finfo.indices);
2267   if (finfo.sections != NULL)
2268     free (finfo.sections);
2269   if (finfo.symbuf != NULL)
2270     free (finfo.symbuf);
2271   for (o = abfd->sections; o != NULL; o = o->next)
2272     {
2273       if ((o->flags & SEC_RELOC) != 0
2274           && elf_section_data (o)->rel_hashes != NULL)
2275         free (elf_section_data (o)->rel_hashes);
2276     }
2277
2278   elf_tdata (abfd)->linker = true;
2279
2280   return true;
2281
2282  error_return:
2283   if (finfo.symstrtab != NULL)
2284     _bfd_stringtab_free (finfo.symstrtab);
2285   if (finfo.contents != NULL)
2286     free (finfo.contents);
2287   if (finfo.external_relocs != NULL)
2288     free (finfo.external_relocs);
2289   if (finfo.internal_relocs != NULL)
2290     free (finfo.internal_relocs);
2291   if (finfo.external_syms != NULL)
2292     free (finfo.external_syms);
2293   if (finfo.internal_syms != NULL)
2294     free (finfo.internal_syms);
2295   if (finfo.indices != NULL)
2296     free (finfo.indices);
2297   if (finfo.sections != NULL)
2298     free (finfo.sections);
2299   if (finfo.symbuf != NULL)
2300     free (finfo.symbuf);
2301   for (o = abfd->sections; o != NULL; o = o->next)
2302     {
2303       if ((o->flags & SEC_RELOC) != 0
2304           && elf_section_data (o)->rel_hashes != NULL)
2305         free (elf_section_data (o)->rel_hashes);
2306     }
2307
2308   return false;
2309 }
2310
2311 /* Add a symbol to the output symbol table.  */
2312
2313 static boolean
2314 elf_link_output_sym (finfo, name, elfsym, input_sec)
2315      struct elf_final_link_info *finfo;
2316      const char *name;
2317      Elf_Internal_Sym *elfsym;
2318      asection *input_sec;
2319 {
2320   boolean (*output_symbol_hook) PARAMS ((bfd *,
2321                                          struct bfd_link_info *info,
2322                                          const char *,
2323                                          Elf_Internal_Sym *,
2324                                          asection *));
2325
2326   output_symbol_hook = get_elf_backend_data (finfo->output_bfd)->
2327     elf_backend_link_output_symbol_hook;
2328   if (output_symbol_hook != NULL)
2329     {
2330       if (! ((*output_symbol_hook)
2331              (finfo->output_bfd, finfo->info, name, elfsym, input_sec)))
2332         return false;
2333     }
2334
2335   if (name == (const char *) NULL || *name == '\0')
2336     elfsym->st_name = 0;
2337   else
2338     {
2339       elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
2340                                                             name, true,
2341                                                             false);
2342       if (elfsym->st_name == (unsigned long) -1)
2343         return false;
2344     }
2345
2346   if (finfo->symbuf_count >= finfo->symbuf_size)
2347     {
2348       if (! elf_link_flush_output_syms (finfo))
2349         return false;
2350     }
2351
2352   elf_swap_symbol_out (finfo->output_bfd, elfsym,
2353                        (PTR) (finfo->symbuf + finfo->symbuf_count));
2354   ++finfo->symbuf_count;
2355
2356   ++finfo->output_bfd->symcount;
2357
2358   return true;
2359 }
2360
2361 /* Flush the output symbols to the file.  */
2362
2363 static boolean
2364 elf_link_flush_output_syms (finfo)
2365      struct elf_final_link_info *finfo;
2366 {
2367   Elf_Internal_Shdr *symtab;
2368
2369   symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr;
2370
2371   if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size,
2372                 SEEK_SET) != 0
2373       || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count,
2374                      sizeof (Elf_External_Sym), finfo->output_bfd)
2375           != finfo->symbuf_count * sizeof (Elf_External_Sym)))
2376     return false;
2377
2378   symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym);
2379
2380   finfo->symbuf_count = 0;
2381
2382   return true;
2383 }
2384
2385 /* Add an external symbol to the symbol table.  This is called from
2386    the hash table traversal routine.  */
2387
2388 static boolean
2389 elf_link_output_extsym (h, data)
2390      struct elf_link_hash_entry *h;
2391      PTR data;
2392 {
2393   struct elf_finfo_failed *eif = (struct elf_finfo_failed *) data;
2394   struct elf_final_link_info *finfo = eif->finfo;
2395   boolean strip;
2396   Elf_Internal_Sym sym;
2397   asection *input_sec;
2398
2399   /* If we are not creating a shared library, and this symbol is
2400      referenced by a shared library but is not defined anywhere, then
2401      warn that it is undefined.  If we do not do this, the runtime
2402      linker will complain that the symbol is undefined when the
2403      program is run.  We don't have to worry about symbols that are
2404      referenced by regular files, because we will already have issued
2405      warnings for them.  */
2406   if (! finfo->info->relocateable
2407       && ! finfo->info->shared
2408       && h->root.type == bfd_link_hash_undefined
2409       && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
2410       && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
2411     {
2412       if (! ((*finfo->info->callbacks->undefined_symbol)
2413              (finfo->info, h->root.root.string, h->root.u.undef.abfd,
2414               (asection *) NULL, 0)))
2415         {
2416           eif->failed = true;
2417           return false;
2418         }
2419     }
2420
2421   /* We don't want to output symbols that have never been mentioned by
2422      a regular file, or that we have been told to strip.  However, if
2423      h->indx is set to -2, the symbol is used by a reloc and we must
2424      output it.  */
2425   if (h->indx == -2)
2426     strip = false;
2427   else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2428             || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
2429            && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2430            && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
2431     strip = true;
2432   else if (finfo->info->strip == strip_all
2433            || (finfo->info->strip == strip_some
2434                && bfd_hash_lookup (finfo->info->keep_hash,
2435                                    h->root.root.string,
2436                                    false, false) == NULL))
2437     strip = true;
2438   else
2439     strip = false;
2440
2441   /* If we're stripping it, and it's not a dynamic symbol, there's
2442      nothing else to do.  */
2443   if (strip && h->dynindx == -1)
2444     return true;
2445
2446   sym.st_value = 0;
2447   sym.st_size = h->size;
2448   sym.st_other = 0;
2449   if (h->root.type == bfd_link_hash_undefweak
2450       || h->root.type == bfd_link_hash_defweak)
2451     sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
2452   else
2453     sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
2454
2455   switch (h->root.type)
2456     {
2457     default:
2458     case bfd_link_hash_new:
2459       abort ();
2460       return false;
2461
2462     case bfd_link_hash_undefined:
2463       input_sec = bfd_und_section_ptr;
2464       sym.st_shndx = SHN_UNDEF;
2465       break;
2466
2467     case bfd_link_hash_undefweak:
2468       input_sec = bfd_und_section_ptr;
2469       sym.st_shndx = SHN_UNDEF;
2470       break;
2471
2472     case bfd_link_hash_defined:
2473     case bfd_link_hash_defweak:
2474       {
2475         input_sec = h->root.u.def.section;
2476         if (input_sec->output_section != NULL)
2477           {
2478             sym.st_shndx =
2479               _bfd_elf_section_from_bfd_section (finfo->output_bfd,
2480                                                  input_sec->output_section);
2481             if (sym.st_shndx == (unsigned short) -1)
2482               {
2483                 eif->failed = true;
2484                 return false;
2485               }
2486
2487             /* ELF symbols in relocateable files are section relative,
2488                but in nonrelocateable files they are virtual
2489                addresses.  */
2490             sym.st_value = h->root.u.def.value + input_sec->output_offset;
2491             if (! finfo->info->relocateable)
2492               sym.st_value += input_sec->output_section->vma;
2493           }
2494         else
2495           {
2496             BFD_ASSERT ((bfd_get_flavour (input_sec->owner)
2497                          == bfd_target_elf_flavour)
2498                         && elf_elfheader (input_sec->owner)->e_type == ET_DYN);
2499             sym.st_shndx = SHN_UNDEF;
2500             input_sec = bfd_und_section_ptr;
2501           }
2502       }
2503       break;
2504
2505     case bfd_link_hash_common:
2506       input_sec = bfd_com_section_ptr;
2507       sym.st_shndx = SHN_COMMON;
2508       sym.st_value = 1 << h->root.u.c.p->alignment_power;
2509       break;
2510
2511     case bfd_link_hash_indirect:
2512     case bfd_link_hash_warning:
2513       /* We can't represent these symbols in ELF.  A warning symbol
2514          may have come from a .gnu.warning.SYMBOL section anyhow.  We
2515          just put the target symbol in the hash table.  If the target
2516          symbol does not really exist, don't do anything.  */
2517       if (h->root.u.i.link->type == bfd_link_hash_new)
2518         return true;
2519       return (elf_link_output_extsym
2520               ((struct elf_link_hash_entry *) h->root.u.i.link, data));
2521     }
2522
2523   /* If this symbol should be put in the .dynsym section, then put it
2524      there now.  We have already know the symbol index.  We also fill
2525      in the entry in the .hash section.  */
2526   if (h->dynindx != -1
2527       && elf_hash_table (finfo->info)->dynamic_sections_created)
2528     {
2529       struct elf_backend_data *bed;
2530       size_t bucketcount;
2531       size_t bucket;
2532       bfd_byte *bucketpos;
2533       bfd_vma chain;
2534
2535       sym.st_name = h->dynstr_index;
2536
2537       /* Give the processor backend a chance to tweak the symbol
2538          value, and also to finish up anything that needs to be done
2539          for this symbol.  */
2540       bed = get_elf_backend_data (finfo->output_bfd);
2541       if (! ((*bed->elf_backend_finish_dynamic_symbol)
2542              (finfo->output_bfd, finfo->info, h, &sym)))
2543         {
2544           eif->failed = true;
2545           return false;
2546         }
2547
2548       elf_swap_symbol_out (finfo->output_bfd, &sym,
2549                            (PTR) (((Elf_External_Sym *)
2550                                    finfo->dynsym_sec->contents)
2551                                   + h->dynindx));
2552
2553       bucketcount = elf_hash_table (finfo->info)->bucketcount;
2554       bucket = (bfd_elf_hash ((const unsigned char *) h->root.root.string)
2555                 % bucketcount);
2556       bucketpos = ((bfd_byte *) finfo->hash_sec->contents
2557                    + (bucket + 2) * (ARCH_SIZE / 8));
2558       chain = get_word (finfo->output_bfd, bucketpos);
2559       put_word (finfo->output_bfd, h->dynindx, bucketpos);
2560       put_word (finfo->output_bfd, chain,
2561                 ((bfd_byte *) finfo->hash_sec->contents
2562                  + (bucketcount + 2 + h->dynindx) * (ARCH_SIZE / 8)));
2563     }
2564
2565   /* If we're stripping it, then it was just a dynamic symbol, and
2566      there's nothing else to do.  */
2567   if (strip)
2568     return true;
2569
2570   h->indx = finfo->output_bfd->symcount;
2571
2572   if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec))
2573     {
2574       eif->failed = true;
2575       return false;
2576     }
2577
2578   return true;
2579 }
2580
2581 /* Link an input file into the linker output file.  This function
2582    handles all the sections and relocations of the input file at once.
2583    This is so that we only have to read the local symbols once, and
2584    don't have to keep them in memory.  */
2585
2586 static boolean
2587 elf_link_input_bfd (finfo, input_bfd)
2588      struct elf_final_link_info *finfo;
2589      bfd *input_bfd;
2590 {
2591   boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
2592                                        bfd *, asection *, bfd_byte *,
2593                                        Elf_Internal_Rela *,
2594                                        Elf_Internal_Sym *, asection **));
2595   bfd *output_bfd;
2596   Elf_Internal_Shdr *symtab_hdr;
2597   size_t locsymcount;
2598   size_t extsymoff;
2599   Elf_External_Sym *esym;
2600   Elf_External_Sym *esymend;
2601   Elf_Internal_Sym *isym;
2602   long *pindex;
2603   asection **ppsection;
2604   asection *o;
2605
2606   output_bfd = finfo->output_bfd;
2607   relocate_section =
2608     get_elf_backend_data (output_bfd)->elf_backend_relocate_section;
2609
2610   /* If this is a dynamic object, we don't want to do anything here:
2611      we don't want the local symbols, and we don't want the section
2612      contents.  */
2613   if (elf_elfheader (input_bfd)->e_type == ET_DYN)
2614     return true;
2615
2616   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2617   if (elf_bad_symtab (input_bfd))
2618     {
2619       locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
2620       extsymoff = 0;
2621     }
2622   else
2623     {
2624       locsymcount = symtab_hdr->sh_info;
2625       extsymoff = symtab_hdr->sh_info;
2626     }
2627
2628   /* Read the local symbols.  */
2629   if (locsymcount > 0
2630       && (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2631           || (bfd_read (finfo->external_syms, sizeof (Elf_External_Sym),
2632                         locsymcount, input_bfd)
2633               != locsymcount * sizeof (Elf_External_Sym))))
2634     return false;
2635
2636   /* Swap in the local symbols and write out the ones which we know
2637      are going into the output file.  */
2638   esym = finfo->external_syms;
2639   esymend = esym + locsymcount;
2640   isym = finfo->internal_syms;
2641   pindex = finfo->indices;
2642   ppsection = finfo->sections;
2643   for (; esym < esymend; esym++, isym++, pindex++, ppsection++)
2644     {
2645       asection *isec;
2646       const char *name;
2647       Elf_Internal_Sym osym;
2648
2649       elf_swap_symbol_in (input_bfd, esym, isym);
2650       *pindex = -1;
2651
2652       if (elf_bad_symtab (input_bfd))
2653         {
2654           if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
2655             {
2656               *ppsection = NULL;
2657               continue;
2658             }
2659         }
2660
2661       if (isym->st_shndx == SHN_UNDEF)
2662         isec = bfd_und_section_ptr;
2663       else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE)
2664         isec = section_from_elf_index (input_bfd, isym->st_shndx);
2665       else if (isym->st_shndx == SHN_ABS)
2666         isec = bfd_abs_section_ptr;
2667       else if (isym->st_shndx == SHN_COMMON)
2668         isec = bfd_com_section_ptr;
2669       else
2670         {
2671           /* Who knows?  */
2672           isec = NULL;
2673         }
2674
2675       *ppsection = isec;
2676
2677       /* Don't output the first, undefined, symbol.  */
2678       if (esym == finfo->external_syms)
2679         continue;
2680
2681       /* If we are stripping all symbols, we don't want to output this
2682          one.  */
2683       if (finfo->info->strip == strip_all)
2684         continue;
2685
2686       /* We never output section symbols.  Instead, we use the section
2687          symbol of the corresponding section in the output file.  */
2688       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
2689         continue;
2690
2691       /* If we are discarding all local symbols, we don't want to
2692          output this one.  If we are generating a relocateable output
2693          file, then some of the local symbols may be required by
2694          relocs; we output them below as we discover that they are
2695          needed.  */
2696       if (finfo->info->discard == discard_all)
2697         continue;
2698
2699       /* Get the name of the symbol.  */
2700       name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
2701                                           isym->st_name);
2702       if (name == NULL)
2703         return false;
2704
2705       /* See if we are discarding symbols with this name.  */
2706       if ((finfo->info->strip == strip_some
2707            && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
2708                == NULL))
2709           || (finfo->info->discard == discard_l
2710               && strncmp (name, finfo->info->lprefix,
2711                           finfo->info->lprefix_len) == 0))
2712         continue;
2713
2714       /* If we get here, we are going to output this symbol.  */
2715
2716       osym = *isym;
2717
2718       /* Adjust the section index for the output file.  */
2719       osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
2720                                                          isec->output_section);
2721       if (osym.st_shndx == (unsigned short) -1)
2722         return false;
2723
2724       *pindex = output_bfd->symcount;
2725
2726       /* ELF symbols in relocateable files are section relative, but
2727          in executable files they are virtual addresses.  Note that
2728          this code assumes that all ELF sections have an associated
2729          BFD section with a reasonable value for output_offset; below
2730          we assume that they also have a reasonable value for
2731          output_section.  Any special sections must be set up to meet
2732          these requirements.  */
2733       osym.st_value += isec->output_offset;
2734       if (! finfo->info->relocateable)
2735         osym.st_value += isec->output_section->vma;
2736
2737       if (! elf_link_output_sym (finfo, name, &osym, isec))
2738         return false;
2739     }
2740
2741   /* Relocate the contents of each section.  */
2742   for (o = input_bfd->sections; o != NULL; o = o->next)
2743     {
2744       if ((o->flags & SEC_HAS_CONTENTS) == 0)
2745         continue;
2746
2747       if ((o->flags & SEC_IN_MEMORY) != 0
2748           && input_bfd == elf_hash_table (finfo->info)->dynobj)
2749         {
2750           /* Section was created by elf_link_create_dynamic_sections.
2751              FIXME: This test is fragile.  */
2752           continue;
2753         }
2754
2755       /* Read the contents of the section.  */
2756       if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
2757                                       (file_ptr) 0, o->_raw_size))
2758         return false;
2759
2760       if ((o->flags & SEC_RELOC) != 0)
2761         {
2762           Elf_Internal_Rela *internal_relocs;
2763
2764           /* Get the swapped relocs.  */
2765           internal_relocs = elf_link_read_relocs (input_bfd, o,
2766                                                   finfo->external_relocs,
2767                                                   finfo->internal_relocs,
2768                                                   false);
2769           if (internal_relocs == NULL
2770               && o->reloc_count > 0)
2771             return false;
2772
2773           /* Relocate the section by invoking a back end routine.
2774
2775              The back end routine is responsible for adjusting the
2776              section contents as necessary, and (if using Rela relocs
2777              and generating a relocateable output file) adjusting the
2778              reloc addend as necessary.
2779
2780              The back end routine does not have to worry about setting
2781              the reloc address or the reloc symbol index.
2782
2783              The back end routine is given a pointer to the swapped in
2784              internal symbols, and can access the hash table entries
2785              for the external symbols via elf_sym_hashes (input_bfd).
2786
2787              When generating relocateable output, the back end routine
2788              must handle STB_LOCAL/STT_SECTION symbols specially.  The
2789              output symbol is going to be a section symbol
2790              corresponding to the output section, which will require
2791              the addend to be adjusted.  */
2792
2793           if (! (*relocate_section) (output_bfd, finfo->info,
2794                                      input_bfd, o,
2795                                      finfo->contents,
2796                                      internal_relocs,
2797                                      finfo->internal_syms,
2798                                      finfo->sections))
2799             return false;
2800
2801           if (finfo->info->relocateable)
2802             {
2803               Elf_Internal_Rela *irela;
2804               Elf_Internal_Rela *irelaend;
2805               struct elf_link_hash_entry **rel_hash;
2806               Elf_Internal_Shdr *input_rel_hdr;
2807               Elf_Internal_Shdr *output_rel_hdr;
2808
2809               /* Adjust the reloc addresses and symbol indices.  */
2810
2811               irela = internal_relocs;
2812               irelaend = irela + o->reloc_count;
2813               rel_hash = (elf_section_data (o->output_section)->rel_hashes
2814                           + o->output_section->reloc_count);
2815               for (; irela < irelaend; irela++, rel_hash++)
2816                 {
2817                   unsigned long r_symndx;
2818                   Elf_Internal_Sym *isym;
2819                   asection *sec;
2820
2821                   irela->r_offset += o->output_offset;
2822
2823                   r_symndx = ELF_R_SYM (irela->r_info);
2824
2825                   if (r_symndx == 0)
2826                     continue;
2827
2828                   if (r_symndx >= locsymcount
2829                       || (elf_bad_symtab (input_bfd)
2830                           && finfo->sections[r_symndx] == NULL))
2831                     {
2832                       long indx;
2833
2834                       /* This is a reloc against a global symbol.  We
2835                          have not yet output all the local symbols, so
2836                          we do not know the symbol index of any global
2837                          symbol.  We set the rel_hash entry for this
2838                          reloc to point to the global hash table entry
2839                          for this symbol.  The symbol index is then
2840                          set at the end of elf_bfd_final_link.  */
2841                       indx = r_symndx - extsymoff;
2842                       *rel_hash = elf_sym_hashes (input_bfd)[indx];
2843
2844                       /* Setting the index to -2 tells
2845                          elf_link_output_extsym that this symbol is
2846                          used by a reloc.  */
2847                       BFD_ASSERT ((*rel_hash)->indx < 0);
2848                       (*rel_hash)->indx = -2;
2849
2850                       continue;
2851                     }
2852
2853                   /* This is a reloc against a local symbol. */
2854
2855                   *rel_hash = NULL;
2856                   isym = finfo->internal_syms + r_symndx;
2857                   sec = finfo->sections[r_symndx];
2858                   if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
2859                     {
2860                       /* I suppose the backend ought to fill in the
2861                          section of any STT_SECTION symbol against a
2862                          processor specific section.  */
2863                       if (sec != NULL && bfd_is_abs_section (sec))
2864                         r_symndx = 0;
2865                       else if (sec == NULL || sec->owner == NULL)
2866                         {
2867                           bfd_set_error (bfd_error_bad_value);
2868                           return false;
2869                         }
2870                       else
2871                         {
2872                           r_symndx = sec->output_section->target_index;
2873                           BFD_ASSERT (r_symndx != 0);
2874                         }
2875                     }
2876                   else
2877                     {
2878                       if (finfo->indices[r_symndx] == -1)
2879                         {
2880                           unsigned long link;
2881                           const char *name;
2882                           asection *osec;
2883
2884                           if (finfo->info->strip == strip_all)
2885                             {
2886                               /* You can't do ld -r -s.  */
2887                               bfd_set_error (bfd_error_invalid_operation);
2888                               return false;
2889                             }
2890
2891                           /* This symbol was skipped earlier, but
2892                              since it is needed by a reloc, we
2893                              must output it now.  */
2894                           link = symtab_hdr->sh_link;
2895                           name = bfd_elf_string_from_elf_section (input_bfd,
2896                                                                   link,
2897                                                                   isym->st_name);
2898                           if (name == NULL)
2899                             return false;
2900
2901                           osec = sec->output_section;
2902                           isym->st_shndx =
2903                             _bfd_elf_section_from_bfd_section (output_bfd,
2904                                                                osec);
2905                           if (isym->st_shndx == (unsigned short) -1)
2906                             return false;
2907
2908                           isym->st_value += sec->output_offset;
2909                           if (! finfo->info->relocateable)
2910                             isym->st_value += osec->vma;
2911
2912                           finfo->indices[r_symndx] = output_bfd->symcount;
2913
2914                           if (! elf_link_output_sym (finfo, name, isym, sec))
2915                             return false;
2916                         }
2917
2918                       r_symndx = finfo->indices[r_symndx];
2919                     }
2920
2921                   irela->r_info = ELF_R_INFO (r_symndx,
2922                                               ELF_R_TYPE (irela->r_info));
2923                 }
2924
2925               /* Swap out the relocs.  */
2926               input_rel_hdr = &elf_section_data (o)->rel_hdr;
2927               output_rel_hdr = &elf_section_data (o->output_section)->rel_hdr;
2928               BFD_ASSERT (output_rel_hdr->sh_entsize
2929                           == input_rel_hdr->sh_entsize);
2930               irela = internal_relocs;
2931               irelaend = irela + o->reloc_count;
2932               if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
2933                 {
2934                   Elf_External_Rel *erel;
2935
2936                   erel = ((Elf_External_Rel *) output_rel_hdr->contents
2937                           + o->output_section->reloc_count);
2938                   for (; irela < irelaend; irela++, erel++)
2939                     {
2940                       Elf_Internal_Rel irel;
2941
2942                       irel.r_offset = irela->r_offset;
2943                       irel.r_info = irela->r_info;
2944                       BFD_ASSERT (irela->r_addend == 0);
2945                       elf_swap_reloc_out (output_bfd, &irel, erel);
2946                     }
2947                 }
2948               else
2949                 {
2950                   Elf_External_Rela *erela;
2951
2952                   BFD_ASSERT (input_rel_hdr->sh_entsize
2953                               == sizeof (Elf_External_Rela));
2954                   erela = ((Elf_External_Rela *) output_rel_hdr->contents
2955                            + o->output_section->reloc_count);
2956                   for (; irela < irelaend; irela++, erela++)
2957                     elf_swap_reloca_out (output_bfd, irela, erela);
2958                 }
2959
2960               o->output_section->reloc_count += o->reloc_count;
2961             }
2962         }
2963
2964       /* Write out the modified section contents.  */
2965       if (! bfd_set_section_contents (output_bfd, o->output_section,
2966                                       finfo->contents, o->output_offset,
2967                                       (o->_cooked_size != 0
2968                                        ? o->_cooked_size
2969                                        : o->_raw_size)))
2970         return false;
2971     }
2972
2973   return true;
2974 }
2975
2976 /* Generate a reloc when linking an ELF file.  This is a reloc
2977    requested by the linker, and does come from any input file.  This
2978    is used to build constructor and destructor tables when linking
2979    with -Ur.  */
2980
2981 static boolean
2982 elf_reloc_link_order (output_bfd, info, output_section, link_order)
2983      bfd *output_bfd;
2984      struct bfd_link_info *info;
2985      asection *output_section;
2986      struct bfd_link_order *link_order;
2987 {
2988   reloc_howto_type *howto;
2989   long indx;
2990   bfd_vma offset;
2991   bfd_vma addend;
2992   struct elf_link_hash_entry **rel_hash_ptr;
2993   Elf_Internal_Shdr *rel_hdr;
2994
2995   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2996   if (howto == NULL)
2997     {
2998       bfd_set_error (bfd_error_bad_value);
2999       return false;
3000     }
3001
3002   addend = link_order->u.reloc.p->addend;
3003
3004   /* Figure out the symbol index.  */
3005   rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
3006                   + output_section->reloc_count);
3007   if (link_order->type == bfd_section_reloc_link_order)
3008     {
3009       indx = link_order->u.reloc.p->u.section->target_index;
3010       BFD_ASSERT (indx != 0);
3011       *rel_hash_ptr = NULL;
3012     }
3013   else
3014     {
3015       struct elf_link_hash_entry *h;
3016
3017       /* Treat a reloc against a defined symbol as though it were
3018          actually against the section.  */
3019       h = elf_link_hash_lookup (elf_hash_table (info),
3020                                 link_order->u.reloc.p->u.name,
3021                                 false, false, true);
3022       if (h != NULL
3023           && (h->root.type == bfd_link_hash_defined
3024               || h->root.type == bfd_link_hash_defweak))
3025         {
3026           asection *section;
3027
3028           section = h->root.u.def.section;
3029           indx = section->output_section->target_index;
3030           *rel_hash_ptr = NULL;
3031           /* It seems that we ought to add the symbol value to the
3032              addend here, but in practice it has already been added
3033              because it was passed to constructor_callback.  */
3034           addend += section->output_section->vma + section->output_offset;
3035         }
3036       else if (h != NULL)
3037         {
3038           /* Setting the index to -2 tells elf_link_output_extsym that
3039              this symbol is used by a reloc.  */
3040           h->indx = -2;
3041           *rel_hash_ptr = h;
3042           indx = 0;
3043         }
3044       else
3045         {
3046           if (! ((*info->callbacks->unattached_reloc)
3047                  (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
3048                   (asection *) NULL, (bfd_vma) 0)))
3049             return false;
3050           indx = 0;
3051         }
3052     }
3053
3054   /* If this is an inplace reloc, we must write the addend into the
3055      object file.  */
3056   if (howto->partial_inplace && addend != 0)
3057     {
3058       bfd_size_type size;
3059       bfd_reloc_status_type rstat;
3060       bfd_byte *buf;
3061       boolean ok;
3062
3063       size = bfd_get_reloc_size (howto);
3064       buf = (bfd_byte *) bfd_zmalloc (size);
3065       if (buf == (bfd_byte *) NULL)
3066         return false;
3067       rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
3068       switch (rstat)
3069         {
3070         case bfd_reloc_ok:
3071           break;
3072         default:
3073         case bfd_reloc_outofrange:
3074           abort ();
3075         case bfd_reloc_overflow:
3076           if (! ((*info->callbacks->reloc_overflow)
3077                  (info,
3078                   (link_order->type == bfd_section_reloc_link_order
3079                    ? bfd_section_name (output_bfd,
3080                                        link_order->u.reloc.p->u.section)
3081                    : link_order->u.reloc.p->u.name),
3082                   howto->name, addend, (bfd *) NULL, (asection *) NULL,
3083                   (bfd_vma) 0)))
3084             {
3085               free (buf);
3086               return false;
3087             }
3088           break;
3089         }
3090       ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
3091                                      (file_ptr) link_order->offset, size);
3092       free (buf);
3093       if (! ok)
3094         return false;
3095     }
3096
3097   /* The address of a reloc is relative to the section in a
3098      relocateable file, and is a virtual address in an executable
3099      file.  */
3100   offset = link_order->offset;
3101   if (! info->relocateable)
3102     offset += output_section->vma;
3103
3104   rel_hdr = &elf_section_data (output_section)->rel_hdr;
3105
3106   if (rel_hdr->sh_type == SHT_REL)
3107     {
3108       Elf_Internal_Rel irel;
3109       Elf_External_Rel *erel;
3110
3111       irel.r_offset = offset;
3112       irel.r_info = ELF_R_INFO (indx, howto->type);
3113       erel = ((Elf_External_Rel *) rel_hdr->contents
3114               + output_section->reloc_count);
3115       elf_swap_reloc_out (output_bfd, &irel, erel);
3116     }
3117   else
3118     {
3119       Elf_Internal_Rela irela;
3120       Elf_External_Rela *erela;
3121
3122       irela.r_offset = offset;
3123       irela.r_info = ELF_R_INFO (indx, howto->type);
3124       irela.r_addend = addend;
3125       erela = ((Elf_External_Rela *) rel_hdr->contents
3126                + output_section->reloc_count);
3127       elf_swap_reloca_out (output_bfd, &irela, erela);
3128     }
3129
3130   ++output_section->reloc_count;
3131
3132   return true;
3133 }
3134