/[gcl]/gcl/binutils/bfd/elflink.c
ViewVC logotype

Diff of /gcl/binutils/bfd/elflink.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.1.20.1

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26