Back to index

cell-binutils  2.17cvs20070401
elflink.c
Go to the documentation of this file.
00001 /* ELF linking support for BFD.
00002    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
00003    2005, 2006, 2007 Free Software Foundation, Inc.
00004 
00005    This file is part of BFD, the Binary File Descriptor library.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00020 
00021 #include "bfd.h"
00022 #include "sysdep.h"
00023 #include "bfdlink.h"
00024 #include "libbfd.h"
00025 #define ARCH_SIZE 0
00026 #include "elf-bfd.h"
00027 #include "safe-ctype.h"
00028 #include "libiberty.h"
00029 #include "objalloc.h"
00030 
00031 /* Define a symbol in a dynamic linkage section.  */
00032 
00033 struct elf_link_hash_entry *
00034 _bfd_elf_define_linkage_sym (bfd *abfd,
00035                           struct bfd_link_info *info,
00036                           asection *sec,
00037                           const char *name)
00038 {
00039   struct elf_link_hash_entry *h;
00040   struct bfd_link_hash_entry *bh;
00041   const struct elf_backend_data *bed;
00042 
00043   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
00044   if (h != NULL)
00045     {
00046       /* Zap symbol defined in an as-needed lib that wasn't linked.
00047         This is a symptom of a larger problem:  Absolute symbols
00048         defined in shared libraries can't be overridden, because we
00049         lose the link to the bfd which is via the symbol section.  */
00050       h->root.type = bfd_link_hash_new;
00051     }
00052 
00053   bh = &h->root;
00054   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
00055                                     sec, 0, NULL, FALSE,
00056                                     get_elf_backend_data (abfd)->collect,
00057                                     &bh))
00058     return NULL;
00059   h = (struct elf_link_hash_entry *) bh;
00060   h->def_regular = 1;
00061   h->type = STT_OBJECT;
00062   h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
00063 
00064   bed = get_elf_backend_data (abfd);
00065   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
00066   return h;
00067 }
00068 
00069 bfd_boolean
00070 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
00071 {
00072   flagword flags;
00073   asection *s;
00074   struct elf_link_hash_entry *h;
00075   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
00076   int ptralign;
00077 
00078   /* This function may be called more than once.  */
00079   s = bfd_get_section_by_name (abfd, ".got");
00080   if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
00081     return TRUE;
00082 
00083   switch (bed->s->arch_size)
00084     {
00085     case 32:
00086       ptralign = 2;
00087       break;
00088 
00089     case 64:
00090       ptralign = 3;
00091       break;
00092 
00093     default:
00094       bfd_set_error (bfd_error_bad_value);
00095       return FALSE;
00096     }
00097 
00098   flags = bed->dynamic_sec_flags;
00099 
00100   s = bfd_make_section_with_flags (abfd, ".got", flags);
00101   if (s == NULL
00102       || !bfd_set_section_alignment (abfd, s, ptralign))
00103     return FALSE;
00104 
00105   if (bed->want_got_plt)
00106     {
00107       s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
00108       if (s == NULL
00109          || !bfd_set_section_alignment (abfd, s, ptralign))
00110        return FALSE;
00111     }
00112 
00113   if (bed->want_got_sym)
00114     {
00115       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
00116         (or .got.plt) section.  We don't do this in the linker script
00117         because we don't want to define the symbol if we are not creating
00118         a global offset table.  */
00119       h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
00120       elf_hash_table (info)->hgot = h;
00121       if (h == NULL)
00122        return FALSE;
00123     }
00124 
00125   /* The first bit of the global offset table is the header.  */
00126   s->size += bed->got_header_size;
00127 
00128   return TRUE;
00129 }
00130 
00131 /* Create a strtab to hold the dynamic symbol names.  */
00132 static bfd_boolean
00133 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
00134 {
00135   struct elf_link_hash_table *hash_table;
00136 
00137   hash_table = elf_hash_table (info);
00138   if (hash_table->dynobj == NULL)
00139     hash_table->dynobj = abfd;
00140 
00141   if (hash_table->dynstr == NULL)
00142     {
00143       hash_table->dynstr = _bfd_elf_strtab_init ();
00144       if (hash_table->dynstr == NULL)
00145        return FALSE;
00146     }
00147   return TRUE;
00148 }
00149 
00150 /* Create some sections which will be filled in with dynamic linking
00151    information.  ABFD is an input file which requires dynamic sections
00152    to be created.  The dynamic sections take up virtual memory space
00153    when the final executable is run, so we need to create them before
00154    addresses are assigned to the output sections.  We work out the
00155    actual contents and size of these sections later.  */
00156 
00157 bfd_boolean
00158 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
00159 {
00160   flagword flags;
00161   register asection *s;
00162   const struct elf_backend_data *bed;
00163 
00164   if (! is_elf_hash_table (info->hash))
00165     return FALSE;
00166 
00167   if (elf_hash_table (info)->dynamic_sections_created)
00168     return TRUE;
00169 
00170   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
00171     return FALSE;
00172 
00173   abfd = elf_hash_table (info)->dynobj;
00174   bed = get_elf_backend_data (abfd);
00175 
00176   flags = bed->dynamic_sec_flags;
00177 
00178   /* A dynamically linked executable has a .interp section, but a
00179      shared library does not.  */
00180   if (info->executable)
00181     {
00182       s = bfd_make_section_with_flags (abfd, ".interp",
00183                                    flags | SEC_READONLY);
00184       if (s == NULL)
00185        return FALSE;
00186     }
00187 
00188   /* Create sections to hold version informations.  These are removed
00189      if they are not needed.  */
00190   s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
00191                                flags | SEC_READONLY);
00192   if (s == NULL
00193       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
00194     return FALSE;
00195 
00196   s = bfd_make_section_with_flags (abfd, ".gnu.version",
00197                                flags | SEC_READONLY);
00198   if (s == NULL
00199       || ! bfd_set_section_alignment (abfd, s, 1))
00200     return FALSE;
00201 
00202   s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
00203                                flags | SEC_READONLY);
00204   if (s == NULL
00205       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
00206     return FALSE;
00207 
00208   s = bfd_make_section_with_flags (abfd, ".dynsym",
00209                                flags | SEC_READONLY);
00210   if (s == NULL
00211       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
00212     return FALSE;
00213 
00214   s = bfd_make_section_with_flags (abfd, ".dynstr",
00215                                flags | SEC_READONLY);
00216   if (s == NULL)
00217     return FALSE;
00218 
00219   s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
00220   if (s == NULL
00221       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
00222     return FALSE;
00223 
00224   /* The special symbol _DYNAMIC is always set to the start of the
00225      .dynamic section.  We could set _DYNAMIC in a linker script, but we
00226      only want to define it if we are, in fact, creating a .dynamic
00227      section.  We don't want to define it if there is no .dynamic
00228      section, since on some ELF platforms the start up code examines it
00229      to decide how to initialize the process.  */
00230   if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
00231     return FALSE;
00232 
00233   if (info->emit_hash)
00234     {
00235       s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
00236       if (s == NULL
00237          || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
00238        return FALSE;
00239       elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
00240     }
00241 
00242   if (info->emit_gnu_hash)
00243     {
00244       s = bfd_make_section_with_flags (abfd, ".gnu.hash",
00245                                    flags | SEC_READONLY);
00246       if (s == NULL
00247          || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
00248        return FALSE;
00249       /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
00250         4 32-bit words followed by variable count of 64-bit words, then
00251         variable count of 32-bit words.  */
00252       if (bed->s->arch_size == 64)
00253        elf_section_data (s)->this_hdr.sh_entsize = 0;
00254       else
00255        elf_section_data (s)->this_hdr.sh_entsize = 4;
00256     }
00257 
00258   /* Let the backend create the rest of the sections.  This lets the
00259      backend set the right flags.  The backend will normally create
00260      the .got and .plt sections.  */
00261   if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
00262     return FALSE;
00263 
00264   elf_hash_table (info)->dynamic_sections_created = TRUE;
00265 
00266   return TRUE;
00267 }
00268 
00269 /* Create dynamic sections when linking against a dynamic object.  */
00270 
00271 bfd_boolean
00272 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
00273 {
00274   flagword flags, pltflags;
00275   struct elf_link_hash_entry *h;
00276   asection *s;
00277   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
00278 
00279   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
00280      .rel[a].bss sections.  */
00281   flags = bed->dynamic_sec_flags;
00282 
00283   pltflags = flags;
00284   if (bed->plt_not_loaded)
00285     /* We do not clear SEC_ALLOC here because we still want the OS to
00286        allocate space for the section; it's just that there's nothing
00287        to read in from the object file.  */
00288     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
00289   else
00290     pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
00291   if (bed->plt_readonly)
00292     pltflags |= SEC_READONLY;
00293 
00294   s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
00295   if (s == NULL
00296       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
00297     return FALSE;
00298 
00299   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
00300      .plt section.  */
00301   if (bed->want_plt_sym)
00302     {
00303       h = _bfd_elf_define_linkage_sym (abfd, info, s,
00304                                    "_PROCEDURE_LINKAGE_TABLE_");
00305       elf_hash_table (info)->hplt = h;
00306       if (h == NULL)
00307        return FALSE;
00308     }
00309 
00310   s = bfd_make_section_with_flags (abfd,
00311                                (bed->default_use_rela_p
00312                                 ? ".rela.plt" : ".rel.plt"),
00313                                flags | SEC_READONLY);
00314   if (s == NULL
00315       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
00316     return FALSE;
00317 
00318   if (! _bfd_elf_create_got_section (abfd, info))
00319     return FALSE;
00320 
00321   if (bed->want_dynbss)
00322     {
00323       /* The .dynbss section is a place to put symbols which are defined
00324         by dynamic objects, are referenced by regular objects, and are
00325         not functions.  We must allocate space for them in the process
00326         image and use a R_*_COPY reloc to tell the dynamic linker to
00327         initialize them at run time.  The linker script puts the .dynbss
00328         section into the .bss section of the final image.  */
00329       s = bfd_make_section_with_flags (abfd, ".dynbss",
00330                                    (SEC_ALLOC
00331                                    | SEC_LINKER_CREATED));
00332       if (s == NULL)
00333        return FALSE;
00334 
00335       /* The .rel[a].bss section holds copy relocs.  This section is not
00336         normally needed.  We need to create it here, though, so that the
00337         linker will map it to an output section.  We can't just create it
00338         only if we need it, because we will not know whether we need it
00339         until we have seen all the input files, and the first time the
00340         main linker code calls BFD after examining all the input files
00341         (size_dynamic_sections) the input sections have already been
00342         mapped to the output sections.  If the section turns out not to
00343         be needed, we can discard it later.  We will never need this
00344         section when generating a shared object, since they do not use
00345         copy relocs.  */
00346       if (! info->shared)
00347        {
00348          s = bfd_make_section_with_flags (abfd,
00349                                       (bed->default_use_rela_p
00350                                        ? ".rela.bss" : ".rel.bss"),
00351                                       flags | SEC_READONLY);
00352          if (s == NULL
00353              || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
00354            return FALSE;
00355        }
00356     }
00357 
00358   return TRUE;
00359 }
00360 
00361 /* Record a new dynamic symbol.  We record the dynamic symbols as we
00362    read the input files, since we need to have a list of all of them
00363    before we can determine the final sizes of the output sections.
00364    Note that we may actually call this function even though we are not
00365    going to output any dynamic symbols; in some cases we know that a
00366    symbol should be in the dynamic symbol table, but only if there is
00367    one.  */
00368 
00369 bfd_boolean
00370 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
00371                                 struct elf_link_hash_entry *h)
00372 {
00373   if (h->dynindx == -1)
00374     {
00375       struct elf_strtab_hash *dynstr;
00376       char *p;
00377       const char *name;
00378       bfd_size_type indx;
00379 
00380       /* XXX: The ABI draft says the linker must turn hidden and
00381         internal symbols into STB_LOCAL symbols when producing the
00382         DSO. However, if ld.so honors st_other in the dynamic table,
00383         this would not be necessary.  */
00384       switch (ELF_ST_VISIBILITY (h->other))
00385        {
00386        case STV_INTERNAL:
00387        case STV_HIDDEN:
00388          if (h->root.type != bfd_link_hash_undefined
00389              && h->root.type != bfd_link_hash_undefweak)
00390            {
00391              h->forced_local = 1;
00392              if (!elf_hash_table (info)->is_relocatable_executable)
00393               return TRUE;
00394            }
00395 
00396        default:
00397          break;
00398        }
00399 
00400       h->dynindx = elf_hash_table (info)->dynsymcount;
00401       ++elf_hash_table (info)->dynsymcount;
00402 
00403       dynstr = elf_hash_table (info)->dynstr;
00404       if (dynstr == NULL)
00405        {
00406          /* Create a strtab to hold the dynamic symbol names.  */
00407          elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
00408          if (dynstr == NULL)
00409            return FALSE;
00410        }
00411 
00412       /* We don't put any version information in the dynamic string
00413         table.  */
00414       name = h->root.root.string;
00415       p = strchr (name, ELF_VER_CHR);
00416       if (p != NULL)
00417        /* We know that the p points into writable memory.  In fact,
00418           there are only a few symbols that have read-only names, being
00419           those like _GLOBAL_OFFSET_TABLE_ that are created specially
00420           by the backends.  Most symbols will have names pointing into
00421           an ELF string table read from a file, or to objalloc memory.  */
00422        *p = 0;
00423 
00424       indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
00425 
00426       if (p != NULL)
00427        *p = ELF_VER_CHR;
00428 
00429       if (indx == (bfd_size_type) -1)
00430        return FALSE;
00431       h->dynstr_index = indx;
00432     }
00433 
00434   return TRUE;
00435 }
00436 
00437 /* Mark a symbol dynamic.  */
00438 
00439 void
00440 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
00441                               struct elf_link_hash_entry *h,
00442                               Elf_Internal_Sym *sym)
00443 {
00444   struct bfd_elf_dynamic_list *d = info->dynamic_list;
00445 
00446   /* It may be called more than once on the same H.  */
00447   if(h->dynamic || info->relocatable)
00448     return;
00449 
00450   if ((info->dynamic_data
00451        && (h->type == STT_OBJECT
00452           || (sym != NULL
00453               && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
00454       || (d != NULL 
00455          && h->root.type == bfd_link_hash_new
00456          && (*d->match) (&d->head, NULL, h->root.root.string)))
00457     h->dynamic = 1;
00458 }
00459 
00460 /* Record an assignment to a symbol made by a linker script.  We need
00461    this in case some dynamic object refers to this symbol.  */
00462 
00463 bfd_boolean
00464 bfd_elf_record_link_assignment (bfd *output_bfd,
00465                             struct bfd_link_info *info,
00466                             const char *name,
00467                             bfd_boolean provide,
00468                             bfd_boolean hidden)
00469 {
00470   struct elf_link_hash_entry *h;
00471   struct elf_link_hash_table *htab;
00472 
00473   if (!is_elf_hash_table (info->hash))
00474     return TRUE;
00475 
00476   htab = elf_hash_table (info);
00477   h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
00478   if (h == NULL)
00479     return provide;
00480 
00481   /* Since we're defining the symbol, don't let it seem to have not
00482      been defined.  record_dynamic_symbol and size_dynamic_sections
00483      may depend on this.  */
00484   if (h->root.type == bfd_link_hash_undefweak
00485       || h->root.type == bfd_link_hash_undefined)
00486     {
00487       h->root.type = bfd_link_hash_new;
00488       if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
00489        bfd_link_repair_undef_list (&htab->root);
00490     }
00491 
00492   if (h->root.type == bfd_link_hash_new)
00493     {
00494       bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
00495       h->non_elf = 0;
00496     }
00497 
00498   /* If this symbol is being provided by the linker script, and it is
00499      currently defined by a dynamic object, but not by a regular
00500      object, then mark it as undefined so that the generic linker will
00501      force the correct value.  */
00502   if (provide
00503       && h->def_dynamic
00504       && !h->def_regular)
00505     h->root.type = bfd_link_hash_undefined;
00506 
00507   /* If this symbol is not being provided by the linker script, and it is
00508      currently defined by a dynamic object, but not by a regular object,
00509      then clear out any version information because the symbol will not be
00510      associated with the dynamic object any more.  */
00511   if (!provide
00512       && h->def_dynamic
00513       && !h->def_regular)
00514     h->verinfo.verdef = NULL;
00515 
00516   h->def_regular = 1;
00517 
00518   if (provide && hidden)
00519     {
00520       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
00521 
00522       h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
00523       (*bed->elf_backend_hide_symbol) (info, h, TRUE);
00524     }
00525 
00526   /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
00527      and executables.  */
00528   if (!info->relocatable
00529       && h->dynindx != -1
00530       && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
00531          || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
00532     h->forced_local = 1;
00533 
00534   if ((h->def_dynamic
00535        || h->ref_dynamic
00536        || info->shared
00537        || (info->executable && elf_hash_table (info)->is_relocatable_executable))
00538       && h->dynindx == -1)
00539     {
00540       if (! bfd_elf_link_record_dynamic_symbol (info, h))
00541        return FALSE;
00542 
00543       /* If this is a weak defined symbol, and we know a corresponding
00544         real symbol from the same dynamic object, make sure the real
00545         symbol is also made into a dynamic symbol.  */
00546       if (h->u.weakdef != NULL
00547          && h->u.weakdef->dynindx == -1)
00548        {
00549          if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
00550            return FALSE;
00551        }
00552     }
00553 
00554   return TRUE;
00555 }
00556 
00557 /* Record a new local dynamic symbol.  Returns 0 on failure, 1 on
00558    success, and 2 on a failure caused by attempting to record a symbol
00559    in a discarded section, eg. a discarded link-once section symbol.  */
00560 
00561 int
00562 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
00563                                      bfd *input_bfd,
00564                                      long input_indx)
00565 {
00566   bfd_size_type amt;
00567   struct elf_link_local_dynamic_entry *entry;
00568   struct elf_link_hash_table *eht;
00569   struct elf_strtab_hash *dynstr;
00570   unsigned long dynstr_index;
00571   char *name;
00572   Elf_External_Sym_Shndx eshndx;
00573   char esym[sizeof (Elf64_External_Sym)];
00574 
00575   if (! is_elf_hash_table (info->hash))
00576     return 0;
00577 
00578   /* See if the entry exists already.  */
00579   for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
00580     if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
00581       return 1;
00582 
00583   amt = sizeof (*entry);
00584   entry = bfd_alloc (input_bfd, amt);
00585   if (entry == NULL)
00586     return 0;
00587 
00588   /* Go find the symbol, so that we can find it's name.  */
00589   if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
00590                           1, input_indx, &entry->isym, esym, &eshndx))
00591     {
00592       bfd_release (input_bfd, entry);
00593       return 0;
00594     }
00595 
00596   if (entry->isym.st_shndx != SHN_UNDEF
00597       && (entry->isym.st_shndx < SHN_LORESERVE
00598          || entry->isym.st_shndx > SHN_HIRESERVE))
00599     {
00600       asection *s;
00601 
00602       s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
00603       if (s == NULL || bfd_is_abs_section (s->output_section))
00604        {
00605          /* We can still bfd_release here as nothing has done another
00606             bfd_alloc.  We can't do this later in this function.  */
00607          bfd_release (input_bfd, entry);
00608          return 2;
00609        }
00610     }
00611 
00612   name = (bfd_elf_string_from_elf_section
00613          (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
00614           entry->isym.st_name));
00615 
00616   dynstr = elf_hash_table (info)->dynstr;
00617   if (dynstr == NULL)
00618     {
00619       /* Create a strtab to hold the dynamic symbol names.  */
00620       elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
00621       if (dynstr == NULL)
00622        return 0;
00623     }
00624 
00625   dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
00626   if (dynstr_index == (unsigned long) -1)
00627     return 0;
00628   entry->isym.st_name = dynstr_index;
00629 
00630   eht = elf_hash_table (info);
00631 
00632   entry->next = eht->dynlocal;
00633   eht->dynlocal = entry;
00634   entry->input_bfd = input_bfd;
00635   entry->input_indx = input_indx;
00636   eht->dynsymcount++;
00637 
00638   /* Whatever binding the symbol had before, it's now local.  */
00639   entry->isym.st_info
00640     = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
00641 
00642   /* The dynindx will be set at the end of size_dynamic_sections.  */
00643 
00644   return 1;
00645 }
00646 
00647 /* Return the dynindex of a local dynamic symbol.  */
00648 
00649 long
00650 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
00651                                 bfd *input_bfd,
00652                                 long input_indx)
00653 {
00654   struct elf_link_local_dynamic_entry *e;
00655 
00656   for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
00657     if (e->input_bfd == input_bfd && e->input_indx == input_indx)
00658       return e->dynindx;
00659   return -1;
00660 }
00661 
00662 /* This function is used to renumber the dynamic symbols, if some of
00663    them are removed because they are marked as local.  This is called
00664    via elf_link_hash_traverse.  */
00665 
00666 static bfd_boolean
00667 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
00668                                   void *data)
00669 {
00670   size_t *count = data;
00671 
00672   if (h->root.type == bfd_link_hash_warning)
00673     h = (struct elf_link_hash_entry *) h->root.u.i.link;
00674 
00675   if (h->forced_local)
00676     return TRUE;
00677 
00678   if (h->dynindx != -1)
00679     h->dynindx = ++(*count);
00680 
00681   return TRUE;
00682 }
00683 
00684 
00685 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
00686    STB_LOCAL binding.  */
00687 
00688 static bfd_boolean
00689 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
00690                                        void *data)
00691 {
00692   size_t *count = data;
00693 
00694   if (h->root.type == bfd_link_hash_warning)
00695     h = (struct elf_link_hash_entry *) h->root.u.i.link;
00696 
00697   if (!h->forced_local)
00698     return TRUE;
00699 
00700   if (h->dynindx != -1)
00701     h->dynindx = ++(*count);
00702 
00703   return TRUE;
00704 }
00705 
00706 /* Return true if the dynamic symbol for a given section should be
00707    omitted when creating a shared library.  */
00708 bfd_boolean
00709 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
00710                                struct bfd_link_info *info,
00711                                asection *p)
00712 {
00713   struct elf_link_hash_table *htab;
00714 
00715   switch (elf_section_data (p)->this_hdr.sh_type)
00716     {
00717     case SHT_PROGBITS:
00718     case SHT_NOBITS:
00719       /* If sh_type is yet undecided, assume it could be
00720         SHT_PROGBITS/SHT_NOBITS.  */
00721     case SHT_NULL:
00722       htab = elf_hash_table (info);
00723       if (p == htab->tls_sec)
00724        return FALSE;
00725 
00726       if (htab->text_index_section != NULL)
00727        return p != htab->text_index_section && p != htab->data_index_section;
00728 
00729       if (strcmp (p->name, ".got") == 0
00730          || strcmp (p->name, ".got.plt") == 0
00731          || strcmp (p->name, ".plt") == 0)
00732        {
00733          asection *ip;
00734 
00735          if (htab->dynobj != NULL
00736              && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
00737              && (ip->flags & SEC_LINKER_CREATED)
00738              && ip->output_section == p)
00739            return TRUE;
00740        }
00741       return FALSE;
00742 
00743       /* There shouldn't be section relative relocations
00744         against any other section.  */
00745     default:
00746       return TRUE;
00747     }
00748 }
00749 
00750 /* Assign dynsym indices.  In a shared library we generate a section
00751    symbol for each output section, which come first.  Next come symbols
00752    which have been forced to local binding.  Then all of the back-end
00753    allocated local dynamic syms, followed by the rest of the global
00754    symbols.  */
00755 
00756 static unsigned long
00757 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
00758                             struct bfd_link_info *info,
00759                             unsigned long *section_sym_count)
00760 {
00761   unsigned long dynsymcount = 0;
00762 
00763   if (info->shared || elf_hash_table (info)->is_relocatable_executable)
00764     {
00765       const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
00766       asection *p;
00767       for (p = output_bfd->sections; p ; p = p->next)
00768        if ((p->flags & SEC_EXCLUDE) == 0
00769            && (p->flags & SEC_ALLOC) != 0
00770            && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
00771          elf_section_data (p)->dynindx = ++dynsymcount;
00772        else
00773          elf_section_data (p)->dynindx = 0;
00774     }
00775   *section_sym_count = dynsymcount;
00776 
00777   elf_link_hash_traverse (elf_hash_table (info),
00778                        elf_link_renumber_local_hash_table_dynsyms,
00779                        &dynsymcount);
00780 
00781   if (elf_hash_table (info)->dynlocal)
00782     {
00783       struct elf_link_local_dynamic_entry *p;
00784       for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
00785        p->dynindx = ++dynsymcount;
00786     }
00787 
00788   elf_link_hash_traverse (elf_hash_table (info),
00789                        elf_link_renumber_hash_table_dynsyms,
00790                        &dynsymcount);
00791 
00792   /* There is an unused NULL entry at the head of the table which
00793      we must account for in our count.  Unless there weren't any
00794      symbols, which means we'll have no table at all.  */
00795   if (dynsymcount != 0)
00796     ++dynsymcount;
00797 
00798   elf_hash_table (info)->dynsymcount = dynsymcount;
00799   return dynsymcount;
00800 }
00801 
00802 /* This function is called when we want to define a new symbol.  It
00803    handles the various cases which arise when we find a definition in
00804    a dynamic object, or when there is already a definition in a
00805    dynamic object.  The new symbol is described by NAME, SYM, PSEC,
00806    and PVALUE.  We set SYM_HASH to the hash table entry.  We set
00807    OVERRIDE if the old symbol is overriding a new definition.  We set
00808    TYPE_CHANGE_OK if it is OK for the type to change.  We set
00809    SIZE_CHANGE_OK if it is OK for the size to change.  By OK to
00810    change, we mean that we shouldn't warn if the type or size does
00811    change.  We set POLD_ALIGNMENT if an old common symbol in a dynamic
00812    object is overridden by a regular object.  */
00813 
00814 bfd_boolean
00815 _bfd_elf_merge_symbol (bfd *abfd,
00816                      struct bfd_link_info *info,
00817                      const char *name,
00818                      Elf_Internal_Sym *sym,
00819                      asection **psec,
00820                      bfd_vma *pvalue,
00821                      unsigned int *pold_alignment,
00822                      struct elf_link_hash_entry **sym_hash,
00823                      bfd_boolean *skip,
00824                      bfd_boolean *override,
00825                      bfd_boolean *type_change_ok,
00826                      bfd_boolean *size_change_ok)
00827 {
00828   asection *sec, *oldsec;
00829   struct elf_link_hash_entry *h;
00830   struct elf_link_hash_entry *flip;
00831   int bind;
00832   bfd *oldbfd;
00833   bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
00834   bfd_boolean newweak, oldweak;
00835   const struct elf_backend_data *bed;
00836 
00837   *skip = FALSE;
00838   *override = FALSE;
00839 
00840   sec = *psec;
00841   bind = ELF_ST_BIND (sym->st_info);
00842 
00843   /* Silently discard TLS symbols from --just-syms.  There's no way to
00844      combine a static TLS block with a new TLS block for this executable.  */
00845   if (ELF_ST_TYPE (sym->st_info) == STT_TLS
00846       && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
00847     {
00848       *skip = TRUE;
00849       return TRUE;
00850     }
00851 
00852   if (! bfd_is_und_section (sec))
00853     h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
00854   else
00855     h = ((struct elf_link_hash_entry *)
00856         bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
00857   if (h == NULL)
00858     return FALSE;
00859   *sym_hash = h;
00860 
00861   /* This code is for coping with dynamic objects, and is only useful
00862      if we are doing an ELF link.  */
00863   if (info->hash->creator != abfd->xvec)
00864     return TRUE;
00865 
00866   /* For merging, we only care about real symbols.  */
00867 
00868   while (h->root.type == bfd_link_hash_indirect
00869         || h->root.type == bfd_link_hash_warning)
00870     h = (struct elf_link_hash_entry *) h->root.u.i.link;
00871 
00872   /* We have to check it for every instance since the first few may be
00873      refereences and not all compilers emit symbol type for undefined
00874      symbols.  */
00875   bfd_elf_link_mark_dynamic_symbol (info, h, sym);
00876 
00877   /* If we just created the symbol, mark it as being an ELF symbol.
00878      Other than that, there is nothing to do--there is no merge issue
00879      with a newly defined symbol--so we just return.  */
00880 
00881   if (h->root.type == bfd_link_hash_new)
00882     {
00883       h->non_elf = 0;
00884       return TRUE;
00885     }
00886 
00887   /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
00888      existing symbol.  */
00889 
00890   switch (h->root.type)
00891     {
00892     default:
00893       oldbfd = NULL;
00894       oldsec = NULL;
00895       break;
00896 
00897     case bfd_link_hash_undefined:
00898     case bfd_link_hash_undefweak:
00899       oldbfd = h->root.u.undef.abfd;
00900       oldsec = NULL;
00901       break;
00902 
00903     case bfd_link_hash_defined:
00904     case bfd_link_hash_defweak:
00905       oldbfd = h->root.u.def.section->owner;
00906       oldsec = h->root.u.def.section;
00907       break;
00908 
00909     case bfd_link_hash_common:
00910       oldbfd = h->root.u.c.p->section->owner;
00911       oldsec = h->root.u.c.p->section;
00912       break;
00913     }
00914 
00915   /* In cases involving weak versioned symbols, we may wind up trying
00916      to merge a symbol with itself.  Catch that here, to avoid the
00917      confusion that results if we try to override a symbol with
00918      itself.  The additional tests catch cases like
00919      _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
00920      dynamic object, which we do want to handle here.  */
00921   if (abfd == oldbfd
00922       && ((abfd->flags & DYNAMIC) == 0
00923          || !h->def_regular))
00924     return TRUE;
00925 
00926   /* NEWDYN and OLDDYN indicate whether the new or old symbol,
00927      respectively, is from a dynamic object.  */
00928 
00929   newdyn = (abfd->flags & DYNAMIC) != 0;
00930 
00931   olddyn = FALSE;
00932   if (oldbfd != NULL)
00933     olddyn = (oldbfd->flags & DYNAMIC) != 0;
00934   else if (oldsec != NULL)
00935     {
00936       /* This handles the special SHN_MIPS_{TEXT,DATA} section
00937         indices used by MIPS ELF.  */
00938       olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
00939     }
00940 
00941   /* NEWDEF and OLDDEF indicate whether the new or old symbol,
00942      respectively, appear to be a definition rather than reference.  */
00943 
00944   newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
00945 
00946   olddef = (h->root.type != bfd_link_hash_undefined
00947            && h->root.type != bfd_link_hash_undefweak
00948            && h->root.type != bfd_link_hash_common);
00949 
00950   /* When we try to create a default indirect symbol from the dynamic
00951      definition with the default version, we skip it if its type and
00952      the type of existing regular definition mismatch.  We only do it
00953      if the existing regular definition won't be dynamic.  */
00954   if (pold_alignment == NULL
00955       && !info->shared
00956       && !info->export_dynamic
00957       && !h->ref_dynamic
00958       && newdyn
00959       && newdef
00960       && !olddyn
00961       && (olddef || h->root.type == bfd_link_hash_common)
00962       && ELF_ST_TYPE (sym->st_info) != h->type
00963       && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
00964       && h->type != STT_NOTYPE)
00965     {
00966       *skip = TRUE;
00967       return TRUE;
00968     }
00969 
00970   /* Check TLS symbol.  We don't check undefined symbol introduced by
00971      "ld -u".  */
00972   if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
00973       && ELF_ST_TYPE (sym->st_info) != h->type
00974       && oldbfd != NULL)
00975     {
00976       bfd *ntbfd, *tbfd;
00977       bfd_boolean ntdef, tdef;
00978       asection *ntsec, *tsec;
00979 
00980       if (h->type == STT_TLS)
00981        {
00982          ntbfd = abfd;
00983          ntsec = sec;
00984          ntdef = newdef;
00985          tbfd = oldbfd;
00986          tsec = oldsec;
00987          tdef = olddef;
00988        }
00989       else
00990        {
00991          ntbfd = oldbfd;
00992          ntsec = oldsec;
00993          ntdef = olddef;
00994          tbfd = abfd;
00995          tsec = sec;
00996          tdef = newdef;
00997        }
00998 
00999       if (tdef && ntdef)
01000        (*_bfd_error_handler)
01001          (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
01002           tbfd, tsec, ntbfd, ntsec, h->root.root.string);
01003       else if (!tdef && !ntdef)
01004        (*_bfd_error_handler)
01005          (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
01006           tbfd, ntbfd, h->root.root.string);
01007       else if (tdef)
01008        (*_bfd_error_handler)
01009          (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
01010           tbfd, tsec, ntbfd, h->root.root.string);
01011       else
01012        (*_bfd_error_handler)
01013          (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
01014           tbfd, ntbfd, ntsec, h->root.root.string);
01015 
01016       bfd_set_error (bfd_error_bad_value);
01017       return FALSE;
01018     }
01019 
01020   /* We need to remember if a symbol has a definition in a dynamic
01021      object or is weak in all dynamic objects. Internal and hidden
01022      visibility will make it unavailable to dynamic objects.  */
01023   if (newdyn && !h->dynamic_def)
01024     {
01025       if (!bfd_is_und_section (sec))
01026        h->dynamic_def = 1;
01027       else
01028        {
01029          /* Check if this symbol is weak in all dynamic objects. If it
01030             is the first time we see it in a dynamic object, we mark
01031             if it is weak. Otherwise, we clear it.  */
01032          if (!h->ref_dynamic)
01033            {
01034              if (bind == STB_WEAK)
01035               h->dynamic_weak = 1;
01036            }
01037          else if (bind != STB_WEAK)
01038            h->dynamic_weak = 0;
01039        }
01040     }
01041 
01042   /* If the old symbol has non-default visibility, we ignore the new
01043      definition from a dynamic object.  */
01044   if (newdyn
01045       && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
01046       && !bfd_is_und_section (sec))
01047     {
01048       *skip = TRUE;
01049       /* Make sure this symbol is dynamic.  */
01050       h->ref_dynamic = 1;
01051       /* A protected symbol has external availability. Make sure it is
01052         recorded as dynamic.
01053 
01054         FIXME: Should we check type and size for protected symbol?  */
01055       if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
01056        return bfd_elf_link_record_dynamic_symbol (info, h);
01057       else
01058        return TRUE;
01059     }
01060   else if (!newdyn
01061           && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
01062           && h->def_dynamic)
01063     {
01064       /* If the new symbol with non-default visibility comes from a
01065         relocatable file and the old definition comes from a dynamic
01066         object, we remove the old definition.  */
01067       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
01068        {
01069          /* Handle the case where the old dynamic definition is
01070             default versioned.  We need to copy the symbol info from
01071             the symbol with default version to the normal one if it
01072             was referenced before.  */
01073          if (h->ref_regular)
01074            {
01075              const struct elf_backend_data *bed
01076               = get_elf_backend_data (abfd);
01077              struct elf_link_hash_entry *vh = *sym_hash;
01078              vh->root.type = h->root.type;
01079              h->root.type = bfd_link_hash_indirect;
01080              (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
01081              /* Protected symbols will override the dynamic definition
01082                with default version.  */
01083              if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
01084               {
01085                 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
01086                 vh->dynamic_def = 1;
01087                 vh->ref_dynamic = 1;
01088               }
01089              else
01090               {
01091                 h->root.type = vh->root.type;
01092                 vh->ref_dynamic = 0;
01093                 /* We have to hide it here since it was made dynamic
01094                    global with extra bits when the symbol info was
01095                    copied from the old dynamic definition.  */
01096                 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
01097               }
01098              h = vh;
01099            }
01100          else
01101            h = *sym_hash;
01102        }
01103 
01104       if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
01105          && bfd_is_und_section (sec))
01106        {
01107          /* If the new symbol is undefined and the old symbol was
01108             also undefined before, we need to make sure
01109             _bfd_generic_link_add_one_symbol doesn't mess
01110             up the linker hash table undefs list.  Since the old
01111             definition came from a dynamic object, it is still on the
01112             undefs list.  */
01113          h->root.type = bfd_link_hash_undefined;
01114          h->root.u.undef.abfd = abfd;
01115        }
01116       else
01117        {
01118          h->root.type = bfd_link_hash_new;
01119          h->root.u.undef.abfd = NULL;
01120        }
01121 
01122       if (h->def_dynamic)
01123        {
01124          h->def_dynamic = 0;
01125          h->ref_dynamic = 1;
01126          h->dynamic_def = 1;
01127        }
01128       /* FIXME: Should we check type and size for protected symbol?  */
01129       h->size = 0;
01130       h->type = 0;
01131       return TRUE;
01132     }
01133 
01134   /* Differentiate strong and weak symbols.  */
01135   newweak = bind == STB_WEAK;
01136   oldweak = (h->root.type == bfd_link_hash_defweak
01137             || h->root.type == bfd_link_hash_undefweak);
01138 
01139   /* If a new weak symbol definition comes from a regular file and the
01140      old symbol comes from a dynamic library, we treat the new one as
01141      strong.  Similarly, an old weak symbol definition from a regular
01142      file is treated as strong when the new symbol comes from a dynamic
01143      library.  Further, an old weak symbol from a dynamic library is
01144      treated as strong if the new symbol is from a dynamic library.
01145      This reflects the way glibc's ld.so works.
01146 
01147      Do this before setting *type_change_ok or *size_change_ok so that
01148      we warn properly when dynamic library symbols are overridden.  */
01149 
01150   if (newdef && !newdyn && olddyn)
01151     newweak = FALSE;
01152   if (olddef && newdyn)
01153     oldweak = FALSE;
01154 
01155   /* It's OK to change the type if either the existing symbol or the
01156      new symbol is weak.  A type change is also OK if the old symbol
01157      is undefined and the new symbol is defined.  */
01158 
01159   if (oldweak
01160       || newweak
01161       || (newdef
01162          && h->root.type == bfd_link_hash_undefined))
01163     *type_change_ok = TRUE;
01164 
01165   /* It's OK to change the size if either the existing symbol or the
01166      new symbol is weak, or if the old symbol is undefined.  */
01167 
01168   if (*type_change_ok
01169       || h->root.type == bfd_link_hash_undefined)
01170     *size_change_ok = TRUE;
01171 
01172   /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
01173      symbol, respectively, appears to be a common symbol in a dynamic
01174      object.  If a symbol appears in an uninitialized section, and is
01175      not weak, and is not a function, then it may be a common symbol
01176      which was resolved when the dynamic object was created.  We want
01177      to treat such symbols specially, because they raise special
01178      considerations when setting the symbol size: if the symbol
01179      appears as a common symbol in a regular object, and the size in
01180      the regular object is larger, we must make sure that we use the
01181      larger size.  This problematic case can always be avoided in C,
01182      but it must be handled correctly when using Fortran shared
01183      libraries.
01184 
01185      Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
01186      likewise for OLDDYNCOMMON and OLDDEF.
01187 
01188      Note that this test is just a heuristic, and that it is quite
01189      possible to have an uninitialized symbol in a shared object which
01190      is really a definition, rather than a common symbol.  This could
01191      lead to some minor confusion when the symbol really is a common
01192      symbol in some regular object.  However, I think it will be
01193      harmless.  */
01194 
01195   if (newdyn
01196       && newdef
01197       && !newweak
01198       && (sec->flags & SEC_ALLOC) != 0
01199       && (sec->flags & SEC_LOAD) == 0
01200       && sym->st_size > 0
01201       && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
01202     newdyncommon = TRUE;
01203   else
01204     newdyncommon = FALSE;
01205 
01206   if (olddyn
01207       && olddef
01208       && h->root.type == bfd_link_hash_defined
01209       && h->def_dynamic
01210       && (h->root.u.def.section->flags & SEC_ALLOC) != 0
01211       && (h->root.u.def.section->flags & SEC_LOAD) == 0
01212       && h->size > 0
01213       && h->type != STT_FUNC)
01214     olddyncommon = TRUE;
01215   else
01216     olddyncommon = FALSE;
01217 
01218   /* We now know everything about the old and new symbols.  We ask the
01219      backend to check if we can merge them.  */
01220   bed = get_elf_backend_data (abfd);
01221   if (bed->merge_symbol
01222       && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
01223                           pold_alignment, skip, override,
01224                           type_change_ok, size_change_ok,
01225                           &newdyn, &newdef, &newdyncommon, &newweak,
01226                           abfd, &sec,
01227                           &olddyn, &olddef, &olddyncommon, &oldweak,
01228                           oldbfd, &oldsec))
01229     return FALSE;
01230 
01231   /* If both the old and the new symbols look like common symbols in a
01232      dynamic object, set the size of the symbol to the larger of the
01233      two.  */
01234 
01235   if (olddyncommon
01236       && newdyncommon
01237       && sym->st_size != h->size)
01238     {
01239       /* Since we think we have two common symbols, issue a multiple
01240         common warning if desired.  Note that we only warn if the
01241         size is different.  If the size is the same, we simply let
01242         the old symbol override the new one as normally happens with
01243         symbols defined in dynamic objects.  */
01244 
01245       if (! ((*info->callbacks->multiple_common)
01246             (info, h->root.root.string, oldbfd, bfd_link_hash_common,
01247              h->size, abfd, bfd_link_hash_common, sym->st_size)))
01248        return FALSE;
01249 
01250       if (sym->st_size > h->size)
01251        h->size = sym->st_size;
01252 
01253       *size_change_ok = TRUE;
01254     }
01255 
01256   /* If we are looking at a dynamic object, and we have found a
01257      definition, we need to see if the symbol was already defined by
01258      some other object.  If so, we want to use the existing
01259      definition, and we do not want to report a multiple symbol
01260      definition error; we do this by clobbering *PSEC to be
01261      bfd_und_section_ptr.
01262 
01263      We treat a common symbol as a definition if the symbol in the
01264      shared library is a function, since common symbols always
01265      represent variables; this can cause confusion in principle, but
01266      any such confusion would seem to indicate an erroneous program or
01267      shared library.  We also permit a common symbol in a regular
01268      object to override a weak symbol in a shared object.  */
01269 
01270   if (newdyn
01271       && newdef
01272       && (olddef
01273          || (h->root.type == bfd_link_hash_common
01274              && (newweak
01275                 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
01276     {
01277       *override = TRUE;
01278       newdef = FALSE;
01279       newdyncommon = FALSE;
01280 
01281       *psec = sec = bfd_und_section_ptr;
01282       *size_change_ok = TRUE;
01283 
01284       /* If we get here when the old symbol is a common symbol, then
01285         we are explicitly letting it override a weak symbol or
01286         function in a dynamic object, and we don't want to warn about
01287         a type change.  If the old symbol is a defined symbol, a type
01288         change warning may still be appropriate.  */
01289 
01290       if (h->root.type == bfd_link_hash_common)
01291        *type_change_ok = TRUE;
01292     }
01293 
01294   /* Handle the special case of an old common symbol merging with a
01295      new symbol which looks like a common symbol in a shared object.
01296      We change *PSEC and *PVALUE to make the new symbol look like a
01297      common symbol, and let _bfd_generic_link_add_one_symbol do the
01298      right thing.  */
01299 
01300   if (newdyncommon
01301       && h->root.type == bfd_link_hash_common)
01302     {
01303       *override = TRUE;
01304       newdef = FALSE;
01305       newdyncommon = FALSE;
01306       *pvalue = sym->st_size;
01307       *psec = sec = bed->common_section (oldsec);
01308       *size_change_ok = TRUE;
01309     }
01310 
01311   /* Skip weak definitions of symbols that are already defined.  */
01312   if (newdef && olddef && newweak)
01313     *skip = TRUE;
01314 
01315   /* If the old symbol is from a dynamic object, and the new symbol is
01316      a definition which is not from a dynamic object, then the new
01317      symbol overrides the old symbol.  Symbols from regular files
01318      always take precedence over symbols from dynamic objects, even if
01319      they are defined after the dynamic object in the link.
01320 
01321      As above, we again permit a common symbol in a regular object to
01322      override a definition in a shared object if the shared object
01323      symbol is a function or is weak.  */
01324 
01325   flip = NULL;
01326   if (!newdyn
01327       && (newdef
01328          || (bfd_is_com_section (sec)
01329              && (oldweak
01330                 || h->type == STT_FUNC)))
01331       && olddyn
01332       && olddef
01333       && h->def_dynamic)
01334     {
01335       /* Change the hash table entry to undefined, and let
01336         _bfd_generic_link_add_one_symbol do the right thing with the
01337         new definition.  */
01338 
01339       h->root.type = bfd_link_hash_undefined;
01340       h->root.u.undef.abfd = h->root.u.def.section->owner;
01341       *size_change_ok = TRUE;
01342 
01343       olddef = FALSE;
01344       olddyncommon = FALSE;
01345 
01346       /* We again permit a type change when a common symbol may be
01347         overriding a function.  */
01348 
01349       if (bfd_is_com_section (sec))
01350        *type_change_ok = TRUE;
01351 
01352       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
01353        flip = *sym_hash;
01354       else
01355        /* This union may have been set to be non-NULL when this symbol
01356           was seen in a dynamic object.  We must force the union to be
01357           NULL, so that it is correct for a regular symbol.  */
01358        h->verinfo.vertree = NULL;
01359     }
01360 
01361   /* Handle the special case of a new common symbol merging with an
01362      old symbol that looks like it might be a common symbol defined in
01363      a shared object.  Note that we have already handled the case in
01364      which a new common symbol should simply override the definition
01365      in the shared library.  */
01366 
01367   if (! newdyn
01368       && bfd_is_com_section (sec)
01369       && olddyncommon)
01370     {
01371       /* It would be best if we could set the hash table entry to a
01372         common symbol, but we don't know what to use for the section
01373         or the alignment.  */
01374       if (! ((*info->callbacks->multiple_common)
01375             (info, h->root.root.string, oldbfd, bfd_link_hash_common,
01376              h->size, abfd, bfd_link_hash_common, sym->st_size)))
01377        return FALSE;
01378 
01379       /* If the presumed common symbol in the dynamic object is
01380         larger, pretend that the new symbol has its size.  */
01381 
01382       if (h->size > *pvalue)
01383        *pvalue = h->size;
01384 
01385       /* We need to remember the alignment required by the symbol
01386         in the dynamic object.  */
01387       BFD_ASSERT (pold_alignment);
01388       *pold_alignment = h->root.u.def.section->alignment_power;
01389 
01390       olddef = FALSE;
01391       olddyncommon = FALSE;
01392 
01393       h->root.type = bfd_link_hash_undefined;
01394       h->root.u.undef.abfd = h->root.u.def.section->owner;
01395 
01396       *size_change_ok = TRUE;
01397       *type_change_ok = TRUE;
01398 
01399       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
01400        flip = *sym_hash;
01401       else
01402        h->verinfo.vertree = NULL;
01403     }
01404 
01405   if (flip != NULL)
01406     {
01407       /* Handle the case where we had a versioned symbol in a dynamic
01408         library and now find a definition in a normal object.  In this
01409         case, we make the versioned symbol point to the normal one.  */
01410       const struct elf_backend_data *bed = get_elf_backend_data (abfd);
01411       flip->root.type = h->root.type;
01412       h->root.type = bfd_link_hash_indirect;
01413       h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
01414       (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
01415       flip->root.u.undef.abfd = h->root.u.undef.abfd;
01416       if (h->def_dynamic)
01417        {
01418          h->def_dynamic = 0;
01419          flip->ref_dynamic = 1;
01420        }
01421     }
01422 
01423   return TRUE;
01424 }
01425 
01426 /* This function is called to create an indirect symbol from the
01427    default for the symbol with the default version if needed. The
01428    symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE.  We
01429    set DYNSYM if the new indirect symbol is dynamic.  */
01430 
01431 bfd_boolean
01432 _bfd_elf_add_default_symbol (bfd *abfd,
01433                           struct bfd_link_info *info,
01434                           struct elf_link_hash_entry *h,
01435                           const char *name,
01436                           Elf_Internal_Sym *sym,
01437                           asection **psec,
01438                           bfd_vma *value,
01439                           bfd_boolean *dynsym,
01440                           bfd_boolean override)
01441 {
01442   bfd_boolean type_change_ok;
01443   bfd_boolean size_change_ok;
01444   bfd_boolean skip;
01445   char *shortname;
01446   struct elf_link_hash_entry *hi;
01447   struct bfd_link_hash_entry *bh;
01448   const struct elf_backend_data *bed;
01449   bfd_boolean collect;
01450   bfd_boolean dynamic;
01451   char *p;
01452   size_t len, shortlen;
01453   asection *sec;
01454 
01455   /* If this symbol has a version, and it is the default version, we
01456      create an indirect symbol from the default name to the fully
01457      decorated name.  This will cause external references which do not
01458      specify a version to be bound to this version of the symbol.  */
01459   p = strchr (name, ELF_VER_CHR);
01460   if (p == NULL || p[1] != ELF_VER_CHR)
01461     return TRUE;
01462 
01463   if (override)
01464     {
01465       /* We are overridden by an old definition. We need to check if we
01466         need to create the indirect symbol from the default name.  */
01467       hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
01468                              FALSE, FALSE);
01469       BFD_ASSERT (hi != NULL);
01470       if (hi == h)
01471        return TRUE;
01472       while (hi->root.type == bfd_link_hash_indirect
01473             || hi->root.type == bfd_link_hash_warning)
01474        {
01475          hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
01476          if (hi == h)
01477            return TRUE;
01478        }
01479     }
01480 
01481   bed = get_elf_backend_data (abfd);
01482   collect = bed->collect;
01483   dynamic = (abfd->flags & DYNAMIC) != 0;
01484 
01485   shortlen = p - name;
01486   shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
01487   if (shortname == NULL)
01488     return FALSE;
01489   memcpy (shortname, name, shortlen);
01490   shortname[shortlen] = '\0';
01491 
01492   /* We are going to create a new symbol.  Merge it with any existing
01493      symbol with this name.  For the purposes of the merge, act as
01494      though we were defining the symbol we just defined, although we
01495      actually going to define an indirect symbol.  */
01496   type_change_ok = FALSE;
01497   size_change_ok = FALSE;
01498   sec = *psec;
01499   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
01500                            NULL, &hi, &skip, &override,
01501                            &type_change_ok, &size_change_ok))
01502     return FALSE;
01503 
01504   if (skip)
01505     goto nondefault;
01506 
01507   if (! override)
01508     {
01509       bh = &hi->root;
01510       if (! (_bfd_generic_link_add_one_symbol
01511             (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
01512              0, name, FALSE, collect, &bh)))
01513        return FALSE;
01514       hi = (struct elf_link_hash_entry *) bh;
01515     }
01516   else
01517     {
01518       /* In this case the symbol named SHORTNAME is overriding the
01519         indirect symbol we want to add.  We were planning on making
01520         SHORTNAME an indirect symbol referring to NAME.  SHORTNAME
01521         is the name without a version.  NAME is the fully versioned
01522         name, and it is the default version.
01523 
01524         Overriding means that we already saw a definition for the
01525         symbol SHORTNAME in a regular object, and it is overriding
01526         the symbol defined in the dynamic object.
01527 
01528         When this happens, we actually want to change NAME, the
01529         symbol we just added, to refer to SHORTNAME.  This will cause
01530         references to NAME in the shared object to become references
01531         to SHORTNAME in the regular object.  This is what we expect
01532         when we override a function in a shared object: that the
01533         references in the shared object will be mapped to the
01534         definition in the regular object.  */
01535 
01536       while (hi->root.type == bfd_link_hash_indirect
01537             || hi->root.type == bfd_link_hash_warning)
01538        hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
01539 
01540       h->root.type = bfd_link_hash_indirect;
01541       h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
01542       if (h->def_dynamic)
01543        {
01544          h->def_dynamic = 0;
01545          hi->ref_dynamic = 1;
01546          if (hi->ref_regular
01547              || hi->def_regular)
01548            {
01549              if (! bfd_elf_link_record_dynamic_symbol (info, hi))
01550               return FALSE;
01551            }
01552        }
01553 
01554       /* Now set HI to H, so that the following code will set the
01555         other fields correctly.  */
01556       hi = h;
01557     }
01558 
01559   /* Check if HI is a warning symbol.  */
01560   if (hi->root.type == bfd_link_hash_warning)
01561     hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
01562 
01563   /* If there is a duplicate definition somewhere, then HI may not
01564      point to an indirect symbol.  We will have reported an error to
01565      the user in that case.  */
01566 
01567   if (hi->root.type == bfd_link_hash_indirect)
01568     {
01569       struct elf_link_hash_entry *ht;
01570 
01571       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
01572       (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
01573 
01574       /* See if the new flags lead us to realize that the symbol must
01575         be dynamic.  */
01576       if (! *dynsym)
01577        {
01578          if (! dynamic)
01579            {
01580              if (info->shared
01581                 || hi->ref_dynamic)
01582               *dynsym = TRUE;
01583            }
01584          else
01585            {
01586              if (hi->ref_regular)
01587               *dynsym = TRUE;
01588            }
01589        }
01590     }
01591 
01592   /* We also need to define an indirection from the nondefault version
01593      of the symbol.  */
01594 
01595 nondefault:
01596   len = strlen (name);
01597   shortname = bfd_hash_allocate (&info->hash->table, len);
01598   if (shortname == NULL)
01599     return FALSE;
01600   memcpy (shortname, name, shortlen);
01601   memcpy (shortname + shortlen, p + 1, len - shortlen);
01602 
01603   /* Once again, merge with any existing symbol.  */
01604   type_change_ok = FALSE;
01605   size_change_ok = FALSE;
01606   sec = *psec;
01607   if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
01608                            NULL, &hi, &skip, &override,
01609                            &type_change_ok, &size_change_ok))
01610     return FALSE;
01611 
01612   if (skip)
01613     return TRUE;
01614 
01615   if (override)
01616     {
01617       /* Here SHORTNAME is a versioned name, so we don't expect to see
01618         the type of override we do in the case above unless it is
01619         overridden by a versioned definition.  */
01620       if (hi->root.type != bfd_link_hash_defined
01621          && hi->root.type != bfd_link_hash_defweak)
01622        (*_bfd_error_handler)
01623          (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
01624           abfd, shortname);
01625     }
01626   else
01627     {
01628       bh = &hi->root;
01629       if (! (_bfd_generic_link_add_one_symbol
01630             (info, abfd, shortname, BSF_INDIRECT,
01631              bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
01632        return FALSE;
01633       hi = (struct elf_link_hash_entry *) bh;
01634 
01635       /* If there is a duplicate definition somewhere, then HI may not
01636         point to an indirect symbol.  We will have reported an error
01637         to the user in that case.  */
01638 
01639       if (hi->root.type == bfd_link_hash_indirect)
01640        {
01641          (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
01642 
01643          /* See if the new flags lead us to realize that the symbol
01644             must be dynamic.  */
01645          if (! *dynsym)
01646            {
01647              if (! dynamic)
01648               {
01649                 if (info->shared
01650                     || hi->ref_dynamic)
01651                   *dynsym = TRUE;
01652               }
01653              else
01654               {
01655                 if (hi->ref_regular)
01656                   *dynsym = TRUE;
01657               }
01658            }
01659        }
01660     }
01661 
01662   return TRUE;
01663 }
01664 
01665 /* This routine is used to export all defined symbols into the dynamic
01666    symbol table.  It is called via elf_link_hash_traverse.  */
01667 
01668 bfd_boolean
01669 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
01670 {
01671   struct elf_info_failed *eif = data;
01672 
01673   /* Ignore this if we won't export it.  */
01674   if (!eif->info->export_dynamic && !h->dynamic)
01675     return TRUE;
01676 
01677   /* Ignore indirect symbols.  These are added by the versioning code.  */
01678   if (h->root.type == bfd_link_hash_indirect)
01679     return TRUE;
01680 
01681   if (h->root.type == bfd_link_hash_warning)
01682     h = (struct elf_link_hash_entry *) h->root.u.i.link;
01683 
01684   if (h->dynindx == -1
01685       && (h->def_regular
01686          || h->ref_regular))
01687     {
01688       struct bfd_elf_version_tree *t;
01689       struct bfd_elf_version_expr *d;
01690 
01691       for (t = eif->verdefs; t != NULL; t = t->next)
01692        {
01693          if (t->globals.list != NULL)
01694            {
01695              d = (*t->match) (&t->globals, NULL, h->root.root.string);
01696              if (d != NULL)
01697               goto doit;
01698            }
01699 
01700          if (t->locals.list != NULL)
01701            {
01702              d = (*t->match) (&t->locals, NULL, h->root.root.string);
01703              if (d != NULL)
01704               return TRUE;
01705            }
01706        }
01707 
01708       if (!eif->verdefs)
01709        {
01710        doit:
01711          if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
01712            {
01713              eif->failed = TRUE;
01714              return FALSE;
01715            }
01716        }
01717     }
01718 
01719   return TRUE;
01720 }
01721 
01722 /* Look through the symbols which are defined in other shared
01723    libraries and referenced here.  Update the list of version
01724    dependencies.  This will be put into the .gnu.version_r section.
01725    This function is called via elf_link_hash_traverse.  */
01726 
01727 bfd_boolean
01728 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
01729                                     void *data)
01730 {
01731   struct elf_find_verdep_info *rinfo = data;
01732   Elf_Internal_Verneed *t;
01733   Elf_Internal_Vernaux *a;
01734   bfd_size_type amt;
01735 
01736   if (h->root.type == bfd_link_hash_warning)
01737     h = (struct elf_link_hash_entry *) h->root.u.i.link;
01738 
01739   /* We only care about symbols defined in shared objects with version
01740      information.  */
01741   if (!h->def_dynamic
01742       || h->def_regular
01743       || h->dynindx == -1
01744       || h->verinfo.verdef == NULL)
01745     return TRUE;
01746 
01747   /* See if we already know about this version.  */
01748   for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
01749     {
01750       if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
01751        continue;
01752 
01753       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
01754        if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
01755          return TRUE;
01756 
01757       break;
01758     }
01759 
01760   /* This is a new version.  Add it to tree we are building.  */
01761 
01762   if (t == NULL)
01763     {
01764       amt = sizeof *t;
01765       t = bfd_zalloc (rinfo->output_bfd, amt);
01766       if (t == NULL)
01767        {
01768          rinfo->failed = TRUE;
01769          return FALSE;
01770        }
01771 
01772       t->vn_bfd = h->verinfo.verdef->vd_bfd;
01773       t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
01774       elf_tdata (rinfo->output_bfd)->verref = t;
01775     }
01776 
01777   amt = sizeof *a;
01778   a = bfd_zalloc (rinfo->output_bfd, amt);
01779 
01780   /* Note that we are copying a string pointer here, and testing it
01781      above.  If bfd_elf_string_from_elf_section is ever changed to
01782      discard the string data when low in memory, this will have to be
01783      fixed.  */
01784   a->vna_nodename = h->verinfo.verdef->vd_nodename;
01785 
01786   a->vna_flags = h->verinfo.verdef->vd_flags;
01787   a->vna_nextptr = t->vn_auxptr;
01788 
01789   h->verinfo.verdef->vd_exp_refno = rinfo->vers;
01790   ++rinfo->vers;
01791 
01792   a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
01793 
01794   t->vn_auxptr = a;
01795 
01796   return TRUE;
01797 }
01798 
01799 /* Figure out appropriate versions for all the symbols.  We may not
01800    have the version number script until we have read all of the input
01801    files, so until that point we don't know which symbols should be
01802    local.  This function is called via elf_link_hash_traverse.  */
01803 
01804 bfd_boolean
01805 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
01806 {
01807   struct elf_assign_sym_version_info *sinfo;
01808   struct bfd_link_info *info;
01809   const struct elf_backend_data *bed;
01810   struct elf_info_failed eif;
01811   char *p;
01812   bfd_size_type amt;
01813 
01814   sinfo = data;
01815   info = sinfo->info;
01816 
01817   if (h->root.type == bfd_link_hash_warning)
01818     h = (struct elf_link_hash_entry *) h->root.u.i.link;
01819 
01820   /* Fix the symbol flags.  */
01821   eif.failed = FALSE;
01822   eif.info = info;
01823   if (! _bfd_elf_fix_symbol_flags (h, &eif))
01824     {
01825       if (eif.failed)
01826        sinfo->failed = TRUE;
01827       return FALSE;
01828     }
01829 
01830   /* We only need version numbers for symbols defined in regular
01831      objects.  */
01832   if (!h->def_regular)
01833     return TRUE;
01834 
01835   bed = get_elf_backend_data (sinfo->output_bfd);
01836   p = strchr (h->root.root.string, ELF_VER_CHR);
01837   if (p != NULL && h->verinfo.vertree == NULL)
01838     {
01839       struct bfd_elf_version_tree *t;
01840       bfd_boolean hidden;
01841 
01842       hidden = TRUE;
01843 
01844       /* There are two consecutive ELF_VER_CHR characters if this is
01845         not a hidden symbol.  */
01846       ++p;
01847       if (*p == ELF_VER_CHR)
01848        {
01849          hidden = FALSE;
01850          ++p;
01851        }
01852 
01853       /* If there is no version string, we can just return out.  */
01854       if (*p == '\0')
01855        {
01856          if (hidden)
01857            h->hidden = 1;
01858          return TRUE;
01859        }
01860 
01861       /* Look for the version.  If we find it, it is no longer weak.  */
01862       for (t = sinfo->verdefs; t != NULL; t = t->next)
01863        {
01864          if (strcmp (t->name, p) == 0)
01865            {
01866              size_t len;
01867              char *alc;
01868              struct bfd_elf_version_expr *d;
01869 
01870              len = p - h->root.root.string;
01871              alc = bfd_malloc (len);
01872              if (alc == NULL)
01873               return FALSE;
01874              memcpy (alc, h->root.root.string, len - 1);
01875              alc[len - 1] = '\0';
01876              if (alc[len - 2] == ELF_VER_CHR)
01877               alc[len - 2] = '\0';
01878 
01879              h->verinfo.vertree = t;
01880              t->used = TRUE;
01881              d = NULL;
01882 
01883              if (t->globals.list != NULL)
01884               d = (*t->match) (&t->globals, NULL, alc);
01885 
01886              /* See if there is anything to force this symbol to
01887                local scope.  */
01888              if (d == NULL && t->locals.list != NULL)
01889               {
01890                 d = (*t->match) (&t->locals, NULL, alc);
01891                 if (d != NULL
01892                     && h->dynindx != -1
01893                     && ! info->export_dynamic)
01894                   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
01895               }
01896 
01897              free (alc);
01898              break;
01899            }
01900        }
01901 
01902       /* If we are building an application, we need to create a
01903         version node for this version.  */
01904       if (t == NULL && info->executable)
01905        {
01906          struct bfd_elf_version_tree **pp;
01907          int version_index;
01908 
01909          /* If we aren't going to export this symbol, we don't need
01910             to worry about it.  */
01911          if (h->dynindx == -1)
01912            return TRUE;
01913 
01914          amt = sizeof *t;
01915          t = bfd_zalloc (sinfo->output_bfd, amt);
01916          if (t == NULL)
01917            {
01918              sinfo->failed = TRUE;
01919              return FALSE;
01920            }
01921 
01922          t->name = p;
01923          t->name_indx = (unsigned int) -1;
01924          t->used = TRUE;
01925 
01926          version_index = 1;
01927          /* Don't count anonymous version tag.  */
01928          if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
01929            version_index = 0;
01930          for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
01931            ++version_index;
01932          t->vernum = version_index;
01933 
01934          *pp = t;
01935 
01936          h->verinfo.vertree = t;
01937        }
01938       else if (t == NULL)
01939        {
01940          /* We could not find the version for a symbol when
01941             generating a shared archive.  Return an error.  */
01942          (*_bfd_error_handler)
01943            (_("%B: undefined versioned symbol name %s"),
01944             sinfo->output_bfd, h->root.root.string);
01945          bfd_set_error (bfd_error_bad_value);
01946          sinfo->failed = TRUE;
01947          return FALSE;
01948        }
01949 
01950       if (hidden)
01951        h->hidden = 1;
01952     }
01953 
01954   /* If we don't have a version for this symbol, see if we can find
01955      something.  */
01956   if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
01957     {
01958       struct bfd_elf_version_tree *t;
01959       struct bfd_elf_version_tree *local_ver;
01960       struct bfd_elf_version_expr *d;
01961 
01962       /* See if can find what version this symbol is in.  If the
01963         symbol is supposed to be local, then don't actually register
01964         it.  */
01965       local_ver = NULL;
01966       for (t = sinfo->verdefs; t != NULL; t = t->next)
01967        {
01968          if (t->globals.list != NULL)
01969            {
01970              bfd_boolean matched;
01971 
01972              matched = FALSE;
01973              d = NULL;
01974              while ((d = (*t->match) (&t->globals, d,
01975                                    h->root.root.string)) != NULL)
01976               if (d->symver)
01977                 matched = TRUE;
01978               else
01979                 {
01980                   /* There is a version without definition.  Make
01981                      the symbol the default definition for this
01982                      version.  */
01983                   h->verinfo.vertree = t;
01984                   local_ver = NULL;
01985                   d->script = 1;
01986                   break;
01987                 }
01988              if (d != NULL)
01989               break;
01990              else if (matched)
01991               /* There is no undefined version for this symbol. Hide the
01992                  default one.  */
01993               (*bed->elf_backend_hide_symbol) (info, h, TRUE);
01994            }
01995 
01996          if (t->locals.list != NULL)
01997            {
01998              d = NULL;
01999              while ((d = (*t->match) (&t->locals, d,
02000                                    h->root.root.string)) != NULL)
02001               {
02002                 local_ver = t;
02003                 /* If the match is "*", keep looking for a more
02004                    explicit, perhaps even global, match.
02005                    XXX: Shouldn't this be !d->wildcard instead?  */
02006                 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
02007                   break;
02008               }
02009 
02010              if (d != NULL)
02011               break;
02012            }
02013        }
02014 
02015       if (local_ver != NULL)
02016        {
02017          h->verinfo.vertree = local_ver;
02018          if (h->dynindx != -1
02019              && ! info->export_dynamic)
02020            {
02021              (*bed->elf_backend_hide_symbol) (info, h, TRUE);
02022            }
02023        }
02024     }
02025 
02026   return TRUE;
02027 }
02028 
02029 /* Read and swap the relocs from the section indicated by SHDR.  This
02030    may be either a REL or a RELA section.  The relocations are
02031    translated into RELA relocations and stored in INTERNAL_RELOCS,
02032    which should have already been allocated to contain enough space.
02033    The EXTERNAL_RELOCS are a buffer where the external form of the
02034    relocations should be stored.
02035 
02036    Returns FALSE if something goes wrong.  */
02037 
02038 static bfd_boolean
02039 elf_link_read_relocs_from_section (bfd *abfd,
02040                                asection *sec,
02041                                Elf_Internal_Shdr *shdr,
02042                                void *external_relocs,
02043                                Elf_Internal_Rela *internal_relocs)
02044 {
02045   const struct elf_backend_data *bed;
02046   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
02047   const bfd_byte *erela;
02048   const bfd_byte *erelaend;
02049   Elf_Internal_Rela *irela;
02050   Elf_Internal_Shdr *symtab_hdr;
02051   size_t nsyms;
02052 
02053   /* Position ourselves at the start of the section.  */
02054   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
02055     return FALSE;
02056 
02057   /* Read the relocations.  */
02058   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
02059     return FALSE;
02060 
02061   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
02062   nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
02063 
02064   bed = get_elf_backend_data (abfd);
02065 
02066   /* Convert the external relocations to the internal format.  */
02067   if (shdr->sh_entsize == bed->s->sizeof_rel)
02068     swap_in = bed->s->swap_reloc_in;
02069   else if (shdr->sh_entsize == bed->s->sizeof_rela)
02070     swap_in = bed->s->swap_reloca_in;
02071   else
02072     {
02073       bfd_set_error (bfd_error_wrong_format);
02074       return FALSE;
02075     }
02076 
02077   erela = external_relocs;
02078   erelaend = erela + shdr->sh_size;
02079   irela = internal_relocs;
02080   while (erela < erelaend)
02081     {
02082       bfd_vma r_symndx;
02083 
02084       (*swap_in) (abfd, erela, irela);
02085       r_symndx = ELF32_R_SYM (irela->r_info);
02086       if (bed->s->arch_size == 64)
02087        r_symndx >>= 24;
02088       if ((size_t) r_symndx >= nsyms)
02089        {
02090          (*_bfd_error_handler)
02091            (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
02092               " for offset 0x%lx in section `%A'"),
02093             abfd, sec,
02094             (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
02095          bfd_set_error (bfd_error_bad_value);
02096          return FALSE;
02097        }
02098       irela += bed->s->int_rels_per_ext_rel;
02099       erela += shdr->sh_entsize;
02100     }
02101 
02102   return TRUE;
02103 }
02104 
02105 /* Read and swap the relocs for a section O.  They may have been
02106    cached.  If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
02107    not NULL, they are used as buffers to read into.  They are known to
02108    be large enough.  If the INTERNAL_RELOCS relocs argument is NULL,
02109    the return value is allocated using either malloc or bfd_alloc,
02110    according to the KEEP_MEMORY argument.  If O has two relocation
02111    sections (both REL and RELA relocations), then the REL_HDR
02112    relocations will appear first in INTERNAL_RELOCS, followed by the
02113    REL_HDR2 relocations.  */
02114 
02115 Elf_Internal_Rela *
02116 _bfd_elf_link_read_relocs (bfd *abfd,
02117                         asection *o,
02118                         void *external_relocs,
02119                         Elf_Internal_Rela *internal_relocs,
02120                         bfd_boolean keep_memory)
02121 {
02122   Elf_Internal_Shdr *rel_hdr;
02123   void *alloc1 = NULL;
02124   Elf_Internal_Rela *alloc2 = NULL;
02125   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
02126 
02127   if (elf_section_data (o)->relocs != NULL)
02128     return elf_section_data (o)->relocs;
02129 
02130   if (o->reloc_count == 0)
02131     return NULL;
02132 
02133   rel_hdr = &elf_section_data (o)->rel_hdr;
02134 
02135   if (internal_relocs == NULL)
02136     {
02137       bfd_size_type size;
02138 
02139       size = o->reloc_count;
02140       size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
02141       if (keep_memory)
02142        internal_relocs = bfd_alloc (abfd, size);
02143       else
02144        internal_relocs = alloc2 = bfd_malloc (size);
02145       if (internal_relocs == NULL)
02146        goto error_return;
02147     }
02148 
02149   if (external_relocs == NULL)
02150     {
02151       bfd_size_type size = rel_hdr->sh_size;
02152 
02153       if (elf_section_data (o)->rel_hdr2)
02154        size += elf_section_data (o)->rel_hdr2->sh_size;
02155       alloc1 = bfd_malloc (size);
02156       if (alloc1 == NULL)
02157        goto error_return;
02158       external_relocs = alloc1;
02159     }
02160 
02161   if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
02162                                      external_relocs,
02163                                      internal_relocs))
02164     goto error_return;
02165   if (elf_section_data (o)->rel_hdr2
02166       && (!elf_link_read_relocs_from_section
02167          (abfd, o,
02168           elf_section_data (o)->rel_hdr2,
02169           ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
02170           internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
02171                            * bed->s->int_rels_per_ext_rel))))
02172     goto error_return;
02173 
02174   /* Cache the results for next time, if we can.  */
02175   if (keep_memory)
02176     elf_section_data (o)->relocs = internal_relocs;
02177 
02178   if (alloc1 != NULL)
02179     free (alloc1);
02180 
02181   /* Don't free alloc2, since if it was allocated we are passing it
02182      back (under the name of internal_relocs).  */
02183 
02184   return internal_relocs;
02185 
02186  error_return:
02187   if (alloc1 != NULL)
02188     free (alloc1);
02189   if (alloc2 != NULL)
02190     free (alloc2);
02191   return NULL;
02192 }
02193 
02194 /* Compute the size of, and allocate space for, REL_HDR which is the
02195    section header for a section containing relocations for O.  */
02196 
02197 bfd_boolean
02198 _bfd_elf_link_size_reloc_section (bfd *abfd,
02199                               Elf_Internal_Shdr *rel_hdr,
02200                               asection *o)
02201 {
02202   bfd_size_type reloc_count;
02203   bfd_size_type num_rel_hashes;
02204 
02205   /* Figure out how many relocations there will be.  */
02206   if (rel_hdr == &elf_section_data (o)->rel_hdr)
02207     reloc_count = elf_section_data (o)->rel_count;
02208   else
02209     reloc_count = elf_section_data (o)->rel_count2;
02210 
02211   num_rel_hashes = o->reloc_count;
02212   if (num_rel_hashes < reloc_count)
02213     num_rel_hashes = reloc_count;
02214 
02215   /* That allows us to calculate the size of the section.  */
02216   rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
02217 
02218   /* The contents field must last into write_object_contents, so we
02219      allocate it with bfd_alloc rather than malloc.  Also since we
02220      cannot be sure that the contents will actually be filled in,
02221      we zero the allocated space.  */
02222   rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
02223   if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
02224     return FALSE;
02225 
02226   /* We only allocate one set of hash entries, so we only do it the
02227      first time we are called.  */
02228   if (elf_section_data (o)->rel_hashes == NULL
02229       && num_rel_hashes)
02230     {
02231       struct elf_link_hash_entry **p;
02232 
02233       p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
02234       if (p == NULL)
02235        return FALSE;
02236 
02237       elf_section_data (o)->rel_hashes = p;
02238     }
02239 
02240   return TRUE;
02241 }
02242 
02243 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
02244    originated from the section given by INPUT_REL_HDR) to the
02245    OUTPUT_BFD.  */
02246 
02247 bfd_boolean
02248 _bfd_elf_link_output_relocs (bfd *output_bfd,
02249                           asection *input_section,
02250                           Elf_Internal_Shdr *input_rel_hdr,
02251                           Elf_Internal_Rela *internal_relocs,
02252                           struct elf_link_hash_entry **rel_hash
02253                             ATTRIBUTE_UNUSED)
02254 {
02255   Elf_Internal_Rela *irela;
02256   Elf_Internal_Rela *irelaend;
02257   bfd_byte *erel;
02258   Elf_Internal_Shdr *output_rel_hdr;
02259   asection *output_section;
02260   unsigned int *rel_countp = NULL;
02261   const struct elf_backend_data *bed;
02262   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
02263 
02264   output_section = input_section->output_section;
02265   output_rel_hdr = NULL;
02266 
02267   if (elf_section_data (output_section)->rel_hdr.sh_entsize
02268       == input_rel_hdr->sh_entsize)
02269     {
02270       output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
02271       rel_countp = &elf_section_data (output_section)->rel_count;
02272     }
02273   else if (elf_section_data (output_section)->rel_hdr2
02274           && (elf_section_data (output_section)->rel_hdr2->sh_entsize
02275               == input_rel_hdr->sh_entsize))
02276     {
02277       output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
02278       rel_countp = &elf_section_data (output_section)->rel_count2;
02279     }
02280   else
02281     {
02282       (*_bfd_error_handler)
02283        (_("%B: relocation size mismatch in %B section %A"),
02284         output_bfd, input_section->owner, input_section);
02285       bfd_set_error (bfd_error_wrong_object_format);
02286       return FALSE;
02287     }
02288 
02289   bed = get_elf_backend_data (output_bfd);
02290   if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
02291     swap_out = bed->s->swap_reloc_out;
02292   else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
02293     swap_out = bed->s->swap_reloca_out;
02294   else
02295     abort ();
02296 
02297   erel = output_rel_hdr->contents;
02298   erel += *rel_countp * input_rel_hdr->sh_entsize;
02299   irela = internal_relocs;
02300   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
02301                     * bed->s->int_rels_per_ext_rel);
02302   while (irela < irelaend)
02303     {
02304       (*swap_out) (output_bfd, irela, erel);
02305       irela += bed->s->int_rels_per_ext_rel;
02306       erel += input_rel_hdr->sh_entsize;
02307     }
02308 
02309   /* Bump the counter, so that we know where to add the next set of
02310      relocations.  */
02311   *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
02312 
02313   return TRUE;
02314 }
02315 
02316 /* Make weak undefined symbols in PIE dynamic.  */
02317 
02318 bfd_boolean
02319 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
02320                              struct elf_link_hash_entry *h)
02321 {
02322   if (info->pie
02323       && h->dynindx == -1
02324       && h->root.type == bfd_link_hash_undefweak)
02325     return bfd_elf_link_record_dynamic_symbol (info, h);
02326 
02327   return TRUE;
02328 }
02329 
02330 /* Fix up the flags for a symbol.  This handles various cases which
02331    can only be fixed after all the input files are seen.  This is
02332    currently called by both adjust_dynamic_symbol and
02333    assign_sym_version, which is unnecessary but perhaps more robust in
02334    the face of future changes.  */
02335 
02336 bfd_boolean
02337 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
02338                         struct elf_info_failed *eif)
02339 {
02340   const struct elf_backend_data *bed = NULL;
02341 
02342   /* If this symbol was mentioned in a non-ELF file, try to set
02343      DEF_REGULAR and REF_REGULAR correctly.  This is the only way to
02344      permit a non-ELF file to correctly refer to a symbol defined in
02345      an ELF dynamic object.  */
02346   if (h->non_elf)
02347     {
02348       while (h->root.type == bfd_link_hash_indirect)
02349        h = (struct elf_link_hash_entry *) h->root.u.i.link;
02350 
02351       if (h->root.type != bfd_link_hash_defined
02352          && h->root.type != bfd_link_hash_defweak)
02353        {
02354          h->ref_regular = 1;
02355          h->ref_regular_nonweak = 1;
02356        }
02357       else
02358        {
02359          if (h->root.u.def.section->owner != NULL
02360              && (bfd_get_flavour (h->root.u.def.section->owner)
02361                 == bfd_target_elf_flavour))
02362            {
02363              h->ref_regular = 1;
02364              h->ref_regular_nonweak = 1;
02365            }
02366          else
02367            h->def_regular = 1;
02368        }
02369 
02370       if (h->dynindx == -1
02371          && (h->def_dynamic
02372              || h->ref_dynamic))
02373        {
02374          if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
02375            {
02376              eif->failed = TRUE;
02377              return FALSE;
02378            }
02379        }
02380     }
02381   else
02382     {
02383       /* Unfortunately, NON_ELF is only correct if the symbol
02384         was first seen in a non-ELF file.  Fortunately, if the symbol
02385         was first seen in an ELF file, we're probably OK unless the
02386         symbol was defined in a non-ELF file.  Catch that case here.
02387         FIXME: We're still in trouble if the symbol was first seen in
02388         a dynamic object, and then later in a non-ELF regular object.  */
02389       if ((h->root.type == bfd_link_hash_defined
02390           || h->root.type == bfd_link_hash_defweak)
02391          && !h->def_regular
02392          && (h->root.u.def.section->owner != NULL
02393              ? (bfd_get_flavour (h->root.u.def.section->owner)
02394                != bfd_target_elf_flavour)
02395              : (bfd_is_abs_section (h->root.u.def.section)
02396                && !h->def_dynamic)))
02397        h->def_regular = 1;
02398     }
02399 
02400   /* Backend specific symbol fixup.  */
02401   if (elf_hash_table (eif->info)->dynobj)
02402     {
02403       bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
02404       if (bed->elf_backend_fixup_symbol
02405          && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
02406        return FALSE;
02407     }
02408 
02409   /* If this is a final link, and the symbol was defined as a common
02410      symbol in a regular object file, and there was no definition in
02411      any dynamic object, then the linker will have allocated space for
02412      the symbol in a common section but the DEF_REGULAR
02413      flag will not have been set.  */
02414   if (h->root.type == bfd_link_hash_defined
02415       && !h->def_regular
02416       && h->ref_regular
02417       && !h->def_dynamic
02418       && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
02419     h->def_regular = 1;
02420 
02421   /* If -Bsymbolic was used (which means to bind references to global
02422      symbols to the definition within the shared object), and this
02423      symbol was defined in a regular object, then it actually doesn't
02424      need a PLT entry.  Likewise, if the symbol has non-default
02425      visibility.  If the symbol has hidden or internal visibility, we
02426      will force it local.  */
02427   if (h->needs_plt
02428       && eif->info->shared
02429       && is_elf_hash_table (eif->info->hash)
02430       && (SYMBOLIC_BIND (eif->info, h)
02431          || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
02432       && h->def_regular)
02433     {
02434       bfd_boolean force_local;
02435 
02436       force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
02437                    || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
02438       (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
02439     }
02440 
02441   /* If a weak undefined symbol has non-default visibility, we also
02442      hide it from the dynamic linker.  */
02443   if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
02444       && h->root.type == bfd_link_hash_undefweak)
02445     {
02446       const struct elf_backend_data *bed;
02447       bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
02448       (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
02449     }
02450 
02451   /* If this is a weak defined symbol in a dynamic object, and we know
02452      the real definition in the dynamic object, copy interesting flags
02453      over to the real definition.  */
02454   if (h->u.weakdef != NULL)
02455     {
02456       struct elf_link_hash_entry *weakdef;
02457 
02458       weakdef = h->u.weakdef;
02459       if (h->root.type == bfd_link_hash_indirect)
02460        h = (struct elf_link_hash_entry *) h->root.u.i.link;
02461 
02462       BFD_ASSERT (h->root.type == bfd_link_hash_defined
02463                 || h->root.type == bfd_link_hash_defweak);
02464       BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
02465                 || weakdef->root.type == bfd_link_hash_defweak);
02466       BFD_ASSERT (weakdef->def_dynamic);
02467 
02468       /* If the real definition is defined by a regular object file,
02469         don't do anything special.  See the longer description in
02470         _bfd_elf_adjust_dynamic_symbol, below.  */
02471       if (weakdef->def_regular)
02472        h->u.weakdef = NULL;
02473       else
02474        (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef,
02475                                             h);
02476     }
02477 
02478   return TRUE;
02479 }
02480 
02481 /* Make the backend pick a good value for a dynamic symbol.  This is
02482    called via elf_link_hash_traverse, and also calls itself
02483    recursively.  */
02484 
02485 bfd_boolean
02486 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
02487 {
02488   struct elf_info_failed *eif = data;
02489   bfd *dynobj;
02490   const struct elf_backend_data *bed;
02491 
02492   if (! is_elf_hash_table (eif->info->hash))
02493     return FALSE;
02494 
02495   if (h->root.type == bfd_link_hash_warning)
02496     {
02497       h->got = elf_hash_table (eif->info)->init_got_offset;
02498       h->plt = elf_hash_table (eif->info)->init_plt_offset;
02499 
02500       /* When warning symbols are created, they **replace** the "real"
02501         entry in the hash table, thus we never get to see the real
02502         symbol in a hash traversal.  So look at it now.  */
02503       h = (struct elf_link_hash_entry *) h->root.u.i.link;
02504     }
02505 
02506   /* Ignore indirect symbols.  These are added by the versioning code.  */
02507   if (h->root.type == bfd_link_hash_indirect)
02508     return TRUE;
02509 
02510   /* Fix the symbol flags.  */
02511   if (! _bfd_elf_fix_symbol_flags (h, eif))
02512     return FALSE;
02513 
02514   /* If this symbol does not require a PLT entry, and it is not
02515      defined by a dynamic object, or is not referenced by a regular
02516      object, ignore it.  We do have to handle a weak defined symbol,
02517      even if no regular object refers to it, if we decided to add it
02518      to the dynamic symbol table.  FIXME: Do we normally need to worry
02519      about symbols which are defined by one dynamic object and
02520      referenced by another one?  */
02521   if (!h->needs_plt
02522       && (h->def_regular
02523          || !h->def_dynamic
02524          || (!h->ref_regular
02525              && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
02526     {
02527       h->plt = elf_hash_table (eif->info)->init_plt_offset;
02528       return TRUE;
02529     }
02530 
02531   /* If we've already adjusted this symbol, don't do it again.  This
02532      can happen via a recursive call.  */
02533   if (h->dynamic_adjusted)
02534     return TRUE;
02535 
02536   /* Don't look at this symbol again.  Note that we must set this
02537      after checking the above conditions, because we may look at a
02538      symbol once, decide not to do anything, and then get called
02539      recursively later after REF_REGULAR is set below.  */
02540   h->dynamic_adjusted = 1;
02541 
02542   /* If this is a weak definition, and we know a real definition, and
02543      the real symbol is not itself defined by a regular object file,
02544      then get a good value for the real definition.  We handle the
02545      real symbol first, for the convenience of the backend routine.
02546 
02547      Note that there is a confusing case here.  If the real definition
02548      is defined by a regular object file, we don't get the real symbol
02549      from the dynamic object, but we do get the weak symbol.  If the
02550      processor backend uses a COPY reloc, then if some routine in the
02551      dynamic object changes the real symbol, we will not see that
02552      change in the corresponding weak symbol.  This is the way other
02553      ELF linkers work as well, and seems to be a result of the shared
02554      library model.
02555 
02556      I will clarify this issue.  Most SVR4 shared libraries define the
02557      variable _timezone and define timezone as a weak synonym.  The
02558      tzset call changes _timezone.  If you write
02559        extern int timezone;
02560        int _timezone = 5;
02561        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
02562      you might expect that, since timezone is a synonym for _timezone,
02563      the same number will print both times.  However, if the processor
02564      backend uses a COPY reloc, then actually timezone will be copied
02565      into your process image, and, since you define _timezone
02566      yourself, _timezone will not.  Thus timezone and _timezone will
02567      wind up at different memory locations.  The tzset call will set
02568      _timezone, leaving timezone unchanged.  */
02569 
02570   if (h->u.weakdef != NULL)
02571     {
02572       /* If we get to this point, we know there is an implicit
02573         reference by a regular object file via the weak symbol H.
02574         FIXME: Is this really true?  What if the traversal finds
02575         H->U.WEAKDEF before it finds H?  */
02576       h->u.weakdef->ref_regular = 1;
02577 
02578       if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
02579        return FALSE;
02580     }
02581 
02582   /* If a symbol has no type and no size and does not require a PLT
02583      entry, then we are probably about to do the wrong thing here: we
02584      are probably going to create a COPY reloc for an empty object.
02585      This case can arise when a shared object is built with assembly
02586      code, and the assembly code fails to set the symbol type.  */
02587   if (h->size == 0
02588       && h->type == STT_NOTYPE
02589       && !h->needs_plt)
02590     (*_bfd_error_handler)
02591       (_("warning: type and size of dynamic symbol `%s' are not defined"),
02592        h->root.root.string);
02593 
02594   dynobj = elf_hash_table (eif->info)->dynobj;
02595   bed = get_elf_backend_data (dynobj);
02596   if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
02597     {
02598       eif->failed = TRUE;
02599       return FALSE;
02600     }
02601 
02602   return TRUE;
02603 }
02604 
02605 /* Adjust all external symbols pointing into SEC_MERGE sections
02606    to reflect the object merging within the sections.  */
02607 
02608 bfd_boolean
02609 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
02610 {
02611   asection *sec;
02612 
02613   if (h->root.type == bfd_link_hash_warning)
02614     h = (struct elf_link_hash_entry *) h->root.u.i.link;
02615 
02616   if ((h->root.type == bfd_link_hash_defined
02617        || h->root.type == bfd_link_hash_defweak)
02618       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
02619       && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
02620     {
02621       bfd *output_bfd = data;
02622 
02623       h->root.u.def.value =
02624        _bfd_merged_section_offset (output_bfd,
02625                                 &h->root.u.def.section,
02626                                 elf_section_data (sec)->sec_info,
02627                                 h->root.u.def.value);
02628     }
02629 
02630   return TRUE;
02631 }
02632 
02633 /* Returns false if the symbol referred to by H should be considered
02634    to resolve local to the current module, and true if it should be
02635    considered to bind dynamically.  */
02636 
02637 bfd_boolean
02638 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
02639                         struct bfd_link_info *info,
02640                         bfd_boolean ignore_protected)
02641 {
02642   bfd_boolean binding_stays_local_p;
02643 
02644   if (h == NULL)
02645     return FALSE;
02646 
02647   while (h->root.type == bfd_link_hash_indirect
02648         || h->root.type == bfd_link_hash_warning)
02649     h = (struct elf_link_hash_entry *) h->root.u.i.link;
02650 
02651   /* If it was forced local, then clearly it's not dynamic.  */
02652   if (h->dynindx == -1)
02653     return FALSE;
02654   if (h->forced_local)
02655     return FALSE;
02656 
02657   /* Identify the cases where name binding rules say that a
02658      visible symbol resolves locally.  */
02659   binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
02660 
02661   switch (ELF_ST_VISIBILITY (h->other))
02662     {
02663     case STV_INTERNAL:
02664     case STV_HIDDEN:
02665       return FALSE;
02666 
02667     case STV_PROTECTED:
02668       /* Proper resolution for function pointer equality may require
02669         that these symbols perhaps be resolved dynamically, even though
02670         we should be resolving them to the current module.  */
02671       if (!ignore_protected || h->type != STT_FUNC)
02672        binding_stays_local_p = TRUE;
02673       break;
02674 
02675     default:
02676       break;
02677     }
02678 
02679   /* If it isn't defined locally, then clearly it's dynamic.  */
02680   if (!h->def_regular)
02681     return TRUE;
02682 
02683   /* Otherwise, the symbol is dynamic if binding rules don't tell
02684      us that it remains local.  */
02685   return !binding_stays_local_p;
02686 }
02687 
02688 /* Return true if the symbol referred to by H should be considered
02689    to resolve local to the current module, and false otherwise.  Differs
02690    from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
02691    undefined symbols and weak symbols.  */
02692 
02693 bfd_boolean
02694 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
02695                            struct bfd_link_info *info,
02696                            bfd_boolean local_protected)
02697 {
02698   /* If it's a local sym, of course we resolve locally.  */
02699   if (h == NULL)
02700     return TRUE;
02701 
02702   /* Common symbols that become definitions don't get the DEF_REGULAR
02703      flag set, so test it first, and don't bail out.  */
02704   if (ELF_COMMON_DEF_P (h))
02705     /* Do nothing.  */;
02706   /* If we don't have a definition in a regular file, then we can't
02707      resolve locally.  The sym is either undefined or dynamic.  */
02708   else if (!h->def_regular)
02709     return FALSE;
02710 
02711   /* Forced local symbols resolve locally.  */
02712   if (h->forced_local)
02713     return TRUE;
02714 
02715   /* As do non-dynamic symbols.  */
02716   if (h->dynindx == -1)
02717     return TRUE;
02718 
02719   /* At this point, we know the symbol is defined and dynamic.  In an
02720      executable it must resolve locally, likewise when building symbolic
02721      shared libraries.  */
02722   if (info->executable || SYMBOLIC_BIND (info, h))
02723     return TRUE;
02724 
02725   /* Now deal with defined dynamic symbols in shared libraries.  Ones
02726      with default visibility might not resolve locally.  */
02727   if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
02728     return FALSE;
02729 
02730   /* However, STV_HIDDEN or STV_INTERNAL ones must be local.  */
02731   if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
02732     return TRUE;
02733 
02734   /* STV_PROTECTED non-function symbols are local.  */
02735   if (h->type != STT_FUNC)
02736     return TRUE;
02737 
02738   /* Function pointer equality tests may require that STV_PROTECTED
02739      symbols be treated as dynamic symbols, even when we know that the
02740      dynamic linker will resolve them locally.  */
02741   return local_protected;
02742 }
02743 
02744 /* Caches some TLS segment info, and ensures that the TLS segment vma is
02745    aligned.  Returns the first TLS output section.  */
02746 
02747 struct bfd_section *
02748 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
02749 {
02750   struct bfd_section *sec, *tls;
02751   unsigned int align = 0;
02752 
02753   for (sec = obfd->sections; sec != NULL; sec = sec->next)
02754     if ((sec->flags & SEC_THREAD_LOCAL) != 0)
02755       break;
02756   tls = sec;
02757 
02758   for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
02759     if (sec->alignment_power > align)
02760       align = sec->alignment_power;
02761 
02762   elf_hash_table (info)->tls_sec = tls;
02763 
02764   /* Ensure the alignment of the first section is the largest alignment,
02765      so that the tls segment starts aligned.  */
02766   if (tls != NULL)
02767     tls->alignment_power = align;
02768 
02769   return tls;
02770 }
02771 
02772 /* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
02773 static bfd_boolean
02774 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
02775                               Elf_Internal_Sym *sym)
02776 {
02777   const struct elf_backend_data *bed;
02778 
02779   /* Local symbols do not count, but target specific ones might.  */
02780   if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
02781       && ELF_ST_BIND (sym->st_info) < STB_LOOS)
02782     return FALSE;
02783 
02784   /* Function symbols do not count.  */
02785   if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
02786     return FALSE;
02787 
02788   /* If the section is undefined, then so is the symbol.  */
02789   if (sym->st_shndx == SHN_UNDEF)
02790     return FALSE;
02791 
02792   /* If the symbol is defined in the common section, then
02793      it is a common definition and so does not count.  */
02794   bed = get_elf_backend_data (abfd);
02795   if (bed->common_definition (sym))
02796     return FALSE;
02797 
02798   /* If the symbol is in a target specific section then we
02799      must rely upon the backend to tell us what it is.  */
02800   if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
02801     /* FIXME - this function is not coded yet:
02802 
02803        return _bfd_is_global_symbol_definition (abfd, sym);
02804 
02805        Instead for now assume that the definition is not global,
02806        Even if this is wrong, at least the linker will behave
02807        in the same way that it used to do.  */
02808     return FALSE;
02809 
02810   return TRUE;
02811 }
02812 
02813 /* Search the symbol table of the archive element of the archive ABFD
02814    whose archive map contains a mention of SYMDEF, and determine if
02815    the symbol is defined in this element.  */
02816 static bfd_boolean
02817 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
02818 {
02819   Elf_Internal_Shdr * hdr;
02820   bfd_size_type symcount;
02821   bfd_size_type extsymcount;
02822   bfd_size_type extsymoff;
02823   Elf_Internal_Sym *isymbuf;
02824   Elf_Internal_Sym *isym;
02825   Elf_Internal_Sym *isymend;
02826   bfd_boolean result;
02827 
02828   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
02829   if (abfd == NULL)
02830     return FALSE;
02831 
02832   if (! bfd_check_format (abfd, bfd_object))
02833     return FALSE;
02834 
02835   /* If we have already included the element containing this symbol in the
02836      link then we do not need to include it again.  Just claim that any symbol
02837      it contains is not a definition, so that our caller will not decide to
02838      (re)include this element.  */
02839   if (abfd->archive_pass)
02840     return FALSE;
02841 
02842   /* Select the appropriate symbol table.  */
02843   if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
02844     hdr = &elf_tdata (abfd)->symtab_hdr;
02845   else
02846     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
02847 
02848   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
02849 
02850   /* The sh_info field of the symtab header tells us where the
02851      external symbols start.  We don't care about the local symbols.  */
02852   if (elf_bad_symtab (abfd))
02853     {
02854       extsymcount = symcount;
02855       extsymoff = 0;
02856     }
02857   else
02858     {
02859       extsymcount = symcount - hdr->sh_info;
02860       extsymoff = hdr->sh_info;
02861     }
02862 
02863   if (extsymcount == 0)
02864     return FALSE;
02865 
02866   /* Read in the symbol table.  */
02867   isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
02868                               NULL, NULL, NULL);
02869   if (isymbuf == NULL)
02870     return FALSE;
02871 
02872   /* Scan the symbol table looking for SYMDEF.  */
02873   result = FALSE;
02874   for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
02875     {
02876       const char *name;
02877 
02878       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
02879                                          isym->st_name);
02880       if (name == NULL)
02881        break;
02882 
02883       if (strcmp (name, symdef->name) == 0)
02884        {
02885          result = is_global_data_symbol_definition (abfd, isym);
02886          break;
02887        }
02888     }
02889 
02890   free (isymbuf);
02891 
02892   return result;
02893 }
02894 
02895 /* Add an entry to the .dynamic table.  */
02896 
02897 bfd_boolean
02898 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
02899                          bfd_vma tag,
02900                          bfd_vma val)
02901 {
02902   struct elf_link_hash_table *hash_table;
02903   const struct elf_backend_data *bed;
02904   asection *s;
02905   bfd_size_type newsize;
02906   bfd_byte *newcontents;
02907   Elf_Internal_Dyn dyn;
02908 
02909   hash_table = elf_hash_table (info);
02910   if (! is_elf_hash_table (hash_table))
02911     return FALSE;
02912 
02913   bed = get_elf_backend_data (hash_table->dynobj);
02914   s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
02915   BFD_ASSERT (s != NULL);
02916 
02917   newsize = s->size + bed->s->sizeof_dyn;
02918   newcontents = bfd_realloc (s->contents, newsize);
02919   if (newcontents == NULL)
02920     return FALSE;
02921 
02922   dyn.d_tag = tag;
02923   dyn.d_un.d_val = val;
02924   bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
02925 
02926   s->size = newsize;
02927   s->contents = newcontents;
02928 
02929   return TRUE;
02930 }
02931 
02932 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
02933    otherwise just check whether one already exists.  Returns -1 on error,
02934    1 if a DT_NEEDED tag already exists, and 0 on success.  */
02935 
02936 static int
02937 elf_add_dt_needed_tag (bfd *abfd,
02938                      struct bfd_link_info *info,
02939                      const char *soname,
02940                      bfd_boolean do_it)
02941 {
02942   struct elf_link_hash_table *hash_table;
02943   bfd_size_type oldsize;
02944   bfd_size_type strindex;
02945 
02946   if (!_bfd_elf_link_create_dynstrtab (abfd, info))
02947     return -1;
02948 
02949   hash_table = elf_hash_table (info);
02950   oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
02951   strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
02952   if (strindex == (bfd_size_type) -1)
02953     return -1;
02954 
02955   if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
02956     {
02957       asection *sdyn;
02958       const struct elf_backend_data *bed;
02959       bfd_byte *extdyn;
02960 
02961       bed = get_elf_backend_data (hash_table->dynobj);
02962       sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
02963       if (sdyn != NULL)
02964        for (extdyn = sdyn->contents;
02965             extdyn < sdyn->contents + sdyn->size;
02966             extdyn += bed->s->sizeof_dyn)
02967          {
02968            Elf_Internal_Dyn dyn;
02969 
02970            bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
02971            if (dyn.d_tag == DT_NEEDED
02972               && dyn.d_un.d_val == strindex)
02973              {
02974               _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
02975               return 1;
02976              }
02977          }
02978     }
02979 
02980   if (do_it)
02981     {
02982       if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
02983        return -1;
02984 
02985       if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
02986        return -1;
02987     }
02988   else
02989     /* We were just checking for existence of the tag.  */
02990     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
02991 
02992   return 0;
02993 }
02994 
02995 /* Sort symbol by value and section.  */
02996 static int
02997 elf_sort_symbol (const void *arg1, const void *arg2)
02998 {
02999   const struct elf_link_hash_entry *h1;
03000   const struct elf_link_hash_entry *h2;
03001   bfd_signed_vma vdiff;
03002 
03003   h1 = *(const struct elf_link_hash_entry **) arg1;
03004   h2 = *(const struct elf_link_hash_entry **) arg2;
03005   vdiff = h1->root.u.def.value - h2->root.u.def.value;
03006   if (vdiff != 0)
03007     return vdiff > 0 ? 1 : -1;
03008   else
03009     {
03010       long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
03011       if (sdiff != 0)
03012        return sdiff > 0 ? 1 : -1;
03013     }
03014   return 0;
03015 }
03016 
03017 /* This function is used to adjust offsets into .dynstr for
03018    dynamic symbols.  This is called via elf_link_hash_traverse.  */
03019 
03020 static bfd_boolean
03021 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
03022 {
03023   struct elf_strtab_hash *dynstr = data;
03024 
03025   if (h->root.type == bfd_link_hash_warning)
03026     h = (struct elf_link_hash_entry *) h->root.u.i.link;
03027 
03028   if (h->dynindx != -1)
03029     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
03030   return TRUE;
03031 }
03032 
03033 /* Assign string offsets in .dynstr, update all structures referencing
03034    them.  */
03035 
03036 static bfd_boolean
03037 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
03038 {
03039   struct elf_link_hash_table *hash_table = elf_hash_table (info);
03040   struct elf_link_local_dynamic_entry *entry;
03041   struct elf_strtab_hash *dynstr = hash_table->dynstr;
03042   bfd *dynobj = hash_table->dynobj;
03043   asection *sdyn;
03044   bfd_size_type size;
03045   const struct elf_backend_data *bed;
03046   bfd_byte *extdyn;
03047 
03048   _bfd_elf_strtab_finalize (dynstr);
03049   size = _bfd_elf_strtab_size (dynstr);
03050 
03051   bed = get_elf_backend_data (dynobj);
03052   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
03053   BFD_ASSERT (sdyn != NULL);
03054 
03055   /* Update all .dynamic entries referencing .dynstr strings.  */
03056   for (extdyn = sdyn->contents;
03057        extdyn < sdyn->contents + sdyn->size;
03058        extdyn += bed->s->sizeof_dyn)
03059     {
03060       Elf_Internal_Dyn dyn;
03061 
03062       bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
03063       switch (dyn.d_tag)
03064        {
03065        case DT_STRSZ:
03066          dyn.d_un.d_val = size;
03067          break;
03068        case DT_NEEDED:
03069        case DT_SONAME:
03070        case DT_RPATH:
03071        case DT_RUNPATH:
03072        case DT_FILTER:
03073        case DT_AUXILIARY:
03074          dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
03075          break;
03076        default:
03077          continue;
03078        }
03079       bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
03080     }
03081 
03082   /* Now update local dynamic symbols.  */
03083   for (entry = hash_table->dynlocal; entry ; entry = entry->next)
03084     entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
03085                                             entry->isym.st_name);
03086 
03087   /* And the rest of dynamic symbols.  */
03088   elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
03089 
03090   /* Adjust version definitions.  */
03091   if (elf_tdata (output_bfd)->cverdefs)
03092     {
03093       asection *s;
03094       bfd_byte *p;
03095       bfd_size_type i;
03096       Elf_Internal_Verdef def;
03097       Elf_Internal_Verdaux defaux;
03098 
03099       s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
03100       p = s->contents;
03101       do
03102        {
03103          _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
03104                                &def);
03105          p += sizeof (Elf_External_Verdef);
03106          if (def.vd_aux != sizeof (Elf_External_Verdef))
03107            continue;
03108          for (i = 0; i < def.vd_cnt; ++i)
03109            {
03110              _bfd_elf_swap_verdaux_in (output_bfd,
03111                                    (Elf_External_Verdaux *) p, &defaux);
03112              defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
03113                                                  defaux.vda_name);
03114              _bfd_elf_swap_verdaux_out (output_bfd,
03115                                     &defaux, (Elf_External_Verdaux *) p);
03116              p += sizeof (Elf_External_Verdaux);
03117            }
03118        }
03119       while (def.vd_next);
03120     }
03121 
03122   /* Adjust version references.  */
03123   if (elf_tdata (output_bfd)->verref)
03124     {
03125       asection *s;
03126       bfd_byte *p;
03127       bfd_size_type i;
03128       Elf_Internal_Verneed need;
03129       Elf_Internal_Vernaux needaux;
03130 
03131       s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
03132       p = s->contents;
03133       do
03134        {
03135          _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
03136                                 &need);
03137          need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
03138          _bfd_elf_swap_verneed_out (output_bfd, &need,
03139                                  (Elf_External_Verneed *) p);
03140          p += sizeof (Elf_External_Verneed);
03141          for (i = 0; i < need.vn_cnt; ++i)
03142            {
03143              _bfd_elf_swap_vernaux_in (output_bfd,
03144                                    (Elf_External_Vernaux *) p, &needaux);
03145              needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
03146                                                   needaux.vna_name);
03147              _bfd_elf_swap_vernaux_out (output_bfd,
03148                                     &needaux,
03149                                     (Elf_External_Vernaux *) p);
03150              p += sizeof (Elf_External_Vernaux);
03151            }
03152        }
03153       while (need.vn_next);
03154     }
03155 
03156   return TRUE;
03157 }
03158 
03159 /* Add symbols from an ELF object file to the linker hash table.  */
03160 
03161 static bfd_boolean
03162 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
03163 {
03164   Elf_Internal_Shdr *hdr;
03165   bfd_size_type symcount;
03166   bfd_size_type extsymcount;
03167   bfd_size_type extsymoff;
03168   struct elf_link_hash_entry **sym_hash;
03169   bfd_boolean dynamic;
03170   Elf_External_Versym *extversym = NULL;
03171   Elf_External_Versym *ever;
03172   struct elf_link_hash_entry *weaks;
03173   struct elf_link_hash_entry **nondeflt_vers = NULL;
03174   bfd_size_type nondeflt_vers_cnt = 0;
03175   Elf_Internal_Sym *isymbuf = NULL;
03176   Elf_Internal_Sym *isym;
03177   Elf_Internal_Sym *isymend;
03178   const struct elf_backend_data *bed;
03179   bfd_boolean add_needed;
03180   struct elf_link_hash_table *htab;
03181   bfd_size_type amt;
03182   void *alloc_mark = NULL;
03183   struct bfd_hash_entry **old_table = NULL;
03184   unsigned int old_size = 0;
03185   unsigned int old_count = 0;
03186   void *old_tab = NULL;
03187   void *old_hash;
03188   void *old_ent;
03189   struct bfd_link_hash_entry *old_undefs = NULL;
03190   struct bfd_link_hash_entry *old_undefs_tail = NULL;
03191   long old_dynsymcount = 0;
03192   size_t tabsize = 0;
03193   size_t hashsize = 0;
03194 
03195   htab = elf_hash_table (info);
03196   bed = get_elf_backend_data (abfd);
03197 
03198   if ((abfd->flags & DYNAMIC) == 0)
03199     dynamic = FALSE;
03200   else
03201     {
03202       dynamic = TRUE;
03203 
03204       /* You can't use -r against a dynamic object.  Also, there's no
03205         hope of using a dynamic object which does not exactly match
03206         the format of the output file.  */
03207       if (info->relocatable
03208          || !is_elf_hash_table (htab)
03209          || htab->root.creator != abfd->xvec)
03210        {
03211          if (info->relocatable)
03212            bfd_set_error (bfd_error_invalid_operation);
03213          else
03214            bfd_set_error (bfd_error_wrong_format);
03215          goto error_return;
03216        }
03217     }
03218 
03219   /* As a GNU extension, any input sections which are named
03220      .gnu.warning.SYMBOL are treated as warning symbols for the given
03221      symbol.  This differs from .gnu.warning sections, which generate
03222      warnings when they are included in an output file.  */
03223   if (info->executable)
03224     {
03225       asection *s;
03226 
03227       for (s = abfd->sections; s != NULL; s = s->next)
03228        {
03229          const char *name;
03230 
03231          name = bfd_get_section_name (abfd, s);
03232          if (CONST_STRNEQ (name, ".gnu.warning."))
03233            {
03234              char *msg;
03235              bfd_size_type sz;
03236 
03237              name += sizeof ".gnu.warning." - 1;
03238 
03239              /* If this is a shared object, then look up the symbol
03240                in the hash table.  If it is there, and it is already
03241                been defined, then we will not be using the entry
03242                from this shared object, so we don't need to warn.
03243                FIXME: If we see the definition in a regular object
03244                later on, we will warn, but we shouldn't.  The only
03245                fix is to keep track of what warnings we are supposed
03246                to emit, and then handle them all at the end of the
03247                link.  */
03248              if (dynamic)
03249               {
03250                 struct elf_link_hash_entry *h;
03251 
03252                 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
03253 
03254                 /* FIXME: What about bfd_link_hash_common?  */
03255                 if (h != NULL
03256                     && (h->root.type == bfd_link_hash_defined
03257                        || h->root.type == bfd_link_hash_defweak))
03258                   {
03259                     /* We don't want to issue this warning.  Clobber
03260                       the section size so that the warning does not
03261                       get copied into the output file.  */
03262                     s->size = 0;
03263                     continue;
03264                   }
03265               }
03266 
03267              sz = s->size;
03268              msg = bfd_alloc (abfd, sz + 1);
03269              if (msg == NULL)
03270               goto error_return;
03271 
03272              if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
03273               goto error_return;
03274 
03275              msg[sz] = '\0';
03276 
03277              if (! (_bfd_generic_link_add_one_symbol
03278                    (info, abfd, name, BSF_WARNING, s, 0, msg,
03279                     FALSE, bed->collect, NULL)))
03280               goto error_return;
03281 
03282              if (! info->relocatable)
03283               {
03284                 /* Clobber the section size so that the warning does
03285                    not get copied into the output file.  */
03286                 s->size = 0;
03287 
03288                 /* Also set SEC_EXCLUDE, so that symbols defined in
03289                    the warning section don't get copied to the output.  */
03290                 s->flags |= SEC_EXCLUDE;
03291               }
03292            }
03293        }
03294     }
03295 
03296   add_needed = TRUE;
03297   if (! dynamic)
03298     {
03299       /* If we are creating a shared library, create all the dynamic
03300         sections immediately.  We need to attach them to something,
03301         so we attach them to this BFD, provided it is the right
03302         format.  FIXME: If there are no input BFD's of the same
03303         format as the output, we can't make a shared library.  */
03304       if (info->shared
03305          && is_elf_hash_table (htab)
03306          && htab->root.creator == abfd->xvec
03307          && !htab->dynamic_sections_created)
03308        {
03309          if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
03310            goto error_return;
03311        }
03312     }
03313   else if (!is_elf_hash_table (htab))
03314     goto error_return;
03315   else
03316     {
03317       asection *s;
03318       const char *soname = NULL;
03319       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
03320       int ret;
03321 
03322       /* ld --just-symbols and dynamic objects don't mix very well.
03323         ld shouldn't allow it.  */
03324       if ((s = abfd->sections) != NULL
03325          && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
03326        abort ();
03327 
03328       /* If this dynamic lib was specified on the command line with
03329         --as-needed in effect, then we don't want to add a DT_NEEDED
03330         tag unless the lib is actually used.  Similary for libs brought
03331         in by another lib's DT_NEEDED.  When --no-add-needed is used
03332         on a dynamic lib, we don't want to add a DT_NEEDED entry for
03333         any dynamic library in DT_NEEDED tags in the dynamic lib at
03334         all.  */
03335       add_needed = (elf_dyn_lib_class (abfd)
03336                   & (DYN_AS_NEEDED | DYN_DT_NEEDED
03337                      | DYN_NO_NEEDED)) == 0;
03338 
03339       s = bfd_get_section_by_name (abfd, ".dynamic");
03340       if (s != NULL)
03341        {
03342          bfd_byte *dynbuf;
03343          bfd_byte *extdyn;
03344          int elfsec;
03345          unsigned long shlink;
03346 
03347          if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
03348            goto error_free_dyn;
03349 
03350          elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
03351          if (elfsec == -1)
03352            goto error_free_dyn;
03353          shlink = elf_elfsections (abfd)[elfsec]->sh_link;
03354 
03355          for (extdyn = dynbuf;
03356               extdyn < dynbuf + s->size;
03357               extdyn += bed->s->sizeof_dyn)
03358            {
03359              Elf_Internal_Dyn dyn;
03360 
03361              bed->s->swap_dyn_in (abfd, extdyn, &dyn);
03362              if (dyn.d_tag == DT_SONAME)
03363               {
03364                 unsigned int tagv = dyn.d_un.d_val;
03365                 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
03366                 if (soname == NULL)
03367                   goto error_free_dyn;
03368               }
03369              if (dyn.d_tag == DT_NEEDED)
03370               {
03371                 struct bfd_link_needed_list *n, **pn;
03372                 char *fnm, *anm;
03373                 unsigned int tagv = dyn.d_un.d_val;
03374 
03375                 amt = sizeof (struct bfd_link_needed_list);
03376                 n = bfd_alloc (abfd, amt);
03377                 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
03378                 if (n == NULL || fnm == NULL)
03379                   goto error_free_dyn;
03380                 amt = strlen (fnm) + 1;
03381                 anm = bfd_alloc (abfd, amt);
03382                 if (anm == NULL)
03383                   goto error_free_dyn;
03384                 memcpy (anm, fnm, amt);
03385                 n->name = anm;
03386                 n->by = abfd;
03387                 n->next = NULL;
03388                 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
03389                   ;
03390                 *pn = n;
03391               }
03392              if (dyn.d_tag == DT_RUNPATH)
03393               {
03394                 struct bfd_link_needed_list *n, **pn;
03395                 char *fnm, *anm;
03396                 unsigned int tagv = dyn.d_un.d_val;
03397 
03398                 amt = sizeof (struct bfd_link_needed_list);
03399                 n = bfd_alloc (abfd, amt);
03400                 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
03401                 if (n == NULL || fnm == NULL)
03402                   goto error_free_dyn;
03403                 amt = strlen (fnm) + 1;
03404                 anm = bfd_alloc (abfd, amt);
03405                 if (anm == NULL)
03406                   goto error_free_dyn;
03407                 memcpy (anm, fnm, amt);
03408                 n->name = anm;
03409                 n->by = abfd;
03410                 n->next = NULL;
03411                 for (pn = & runpath;
03412                      *pn != NULL;
03413                      pn = &(*pn)->next)
03414                   ;
03415                 *pn = n;
03416               }
03417              /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
03418              if (!runpath && dyn.d_tag == DT_RPATH)
03419               {
03420                 struct bfd_link_needed_list *n, **pn;
03421                 char *fnm, *anm;
03422                 unsigned int tagv = dyn.d_un.d_val;
03423 
03424                 amt = sizeof (struct bfd_link_needed_list);
03425                 n = bfd_alloc (abfd, amt);
03426                 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
03427                 if (n == NULL || fnm == NULL)
03428                   goto error_free_dyn;
03429                 amt = strlen (fnm) + 1;
03430                 anm = bfd_alloc (abfd, amt);
03431                 if (anm == NULL)
03432                   {
03433                   error_free_dyn:
03434                     free (dynbuf);
03435                     goto error_return;
03436                   }
03437                 memcpy (anm, fnm, amt);
03438                 n->name = anm;
03439                 n->by = abfd;
03440                 n->next = NULL;
03441                 for (pn = & rpath;
03442                      *pn != NULL;
03443                      pn = &(*pn)->next)
03444                   ;
03445                 *pn = n;
03446               }
03447            }
03448 
03449          free (dynbuf);
03450        }
03451 
03452       /* DT_RUNPATH overrides DT_RPATH.  Do _NOT_ bfd_release, as that
03453         frees all more recently bfd_alloc'd blocks as well.  */
03454       if (runpath)
03455        rpath = runpath;
03456 
03457       if (rpath)
03458        {
03459          struct bfd_link_needed_list **pn;
03460          for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
03461            ;
03462          *pn = rpath;
03463        }
03464 
03465       /* We do not want to include any of the sections in a dynamic
03466         object in the output file.  We hack by simply clobbering the
03467         list of sections in the BFD.  This could be handled more
03468         cleanly by, say, a new section flag; the existing
03469         SEC_NEVER_LOAD flag is not the one we want, because that one
03470         still implies that the section takes up space in the output
03471         file.  */
03472       bfd_section_list_clear (abfd);
03473 
03474       /* Find the name to use in a DT_NEEDED entry that refers to this
03475         object.  If the object has a DT_SONAME entry, we use it.
03476         Otherwise, if the generic linker stuck something in
03477         elf_dt_name, we use that.  Otherwise, we just use the file
03478         name.  */
03479       if (soname == NULL || *soname == '\0')
03480        {
03481          soname = elf_dt_name (abfd);
03482          if (soname == NULL || *soname == '\0')
03483            soname = bfd_get_filename (abfd);
03484        }
03485 
03486       /* Save the SONAME because sometimes the linker emulation code
03487         will need to know it.  */
03488       elf_dt_name (abfd) = soname;
03489 
03490       ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
03491       if (ret < 0)
03492        goto error_return;
03493 
03494       /* If we have already included this dynamic object in the
03495         link, just ignore it.  There is no reason to include a
03496         particular dynamic object more than once.  */
03497       if (ret > 0)
03498        return TRUE;
03499     }
03500 
03501   /* If this is a dynamic object, we always link against the .dynsym
03502      symbol table, not the .symtab symbol table.  The dynamic linker
03503      will only see the .dynsym symbol table, so there is no reason to
03504      look at .symtab for a dynamic object.  */
03505 
03506   if (! dynamic || elf_dynsymtab (abfd) == 0)
03507     hdr = &elf_tdata (abfd)->symtab_hdr;
03508   else
03509     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
03510 
03511   symcount = hdr->sh_size / bed->s->sizeof_sym;
03512 
03513   /* The sh_info field of the symtab header tells us where the
03514      external symbols start.  We don't care about the local symbols at
03515      this point.  */
03516   if (elf_bad_symtab (abfd))
03517     {
03518       extsymcount = symcount;
03519       extsymoff = 0;
03520     }
03521   else
03522     {
03523       extsymcount = symcount - hdr->sh_info;
03524       extsymoff = hdr->sh_info;
03525     }
03526 
03527   sym_hash = NULL;
03528   if (extsymcount != 0)
03529     {
03530       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
03531                                   NULL, NULL, NULL);
03532       if (isymbuf == NULL)
03533        goto error_return;
03534 
03535       /* We store a pointer to the hash table entry for each external
03536         symbol.  */
03537       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
03538       sym_hash = bfd_alloc (abfd, amt);
03539       if (sym_hash == NULL)
03540        goto error_free_sym;
03541       elf_sym_hashes (abfd) = sym_hash;
03542     }
03543 
03544   if (dynamic)
03545     {
03546       /* Read in any version definitions.  */
03547       if (!_bfd_elf_slurp_version_tables (abfd,
03548                                      info->default_imported_symver))
03549        goto error_free_sym;
03550 
03551       /* Read in the symbol versions, but don't bother to convert them
03552         to internal format.  */
03553       if (elf_dynversym (abfd) != 0)
03554        {
03555          Elf_Internal_Shdr *versymhdr;
03556 
03557          versymhdr = &elf_tdata (abfd)->dynversym_hdr;
03558          extversym = bfd_malloc (versymhdr->sh_size);
03559          if (extversym == NULL)
03560            goto error_free_sym;
03561          amt = versymhdr->sh_size;
03562          if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
03563              || bfd_bread (extversym, amt, abfd) != amt)
03564            goto error_free_vers;
03565        }
03566     }
03567 
03568   /* If we are loading an as-needed shared lib, save the symbol table
03569      state before we start adding symbols.  If the lib turns out
03570      to be unneeded, restore the state.  */
03571   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
03572     {
03573       unsigned int i;
03574       size_t entsize;
03575 
03576       for (entsize = 0, i = 0; i < htab->root.table.size; i++)
03577        {
03578          struct bfd_hash_entry *p;
03579          struct elf_link_hash_entry *h;
03580 
03581          for (p = htab->root.table.table[i]; p != NULL; p = p->next)
03582            {
03583              h = (struct elf_link_hash_entry *) p;
03584              entsize += htab->root.table.entsize;
03585              if (h->root.type == bfd_link_hash_warning)
03586               entsize += htab->root.table.entsize;
03587            }
03588        }
03589 
03590       tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
03591       hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
03592       old_tab = bfd_malloc (tabsize + entsize + hashsize);
03593       if (old_tab == NULL)
03594        goto error_free_vers;
03595 
03596       /* Remember the current objalloc pointer, so that all mem for
03597         symbols added can later be reclaimed.  */
03598       alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
03599       if (alloc_mark == NULL)
03600        goto error_free_vers;
03601 
03602       /* Make a special call to the linker "notice" function to
03603         tell it that we are about to handle an as-needed lib.  */
03604       if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
03605                                    notice_as_needed))
03606        return FALSE;
03607 
03608 
03609       /* Clone the symbol table and sym hashes.  Remember some
03610         pointers into the symbol table, and dynamic symbol count.  */
03611       old_hash = (char *) old_tab + tabsize;
03612       old_ent = (char *) old_hash + hashsize;
03613       memcpy (old_tab, htab->root.table.table, tabsize);
03614       memcpy (old_hash, sym_hash, hashsize);
03615       old_undefs = htab->root.undefs;
03616       old_undefs_tail = htab->root.undefs_tail;
03617       old_table = htab->root.table.table;
03618       old_size = htab->root.table.size;
03619       old_count = htab->root.table.count;
03620       old_dynsymcount = htab->dynsymcount;
03621 
03622       for (i = 0; i < htab->root.table.size; i++)
03623        {
03624          struct bfd_hash_entry *p;
03625          struct elf_link_hash_entry *h;
03626 
03627          for (p = htab->root.table.table[i]; p != NULL; p = p->next)
03628            {
03629              memcpy (old_ent, p, htab->root.table.entsize);
03630              old_ent = (char *) old_ent + htab->root.table.entsize;
03631              h = (struct elf_link_hash_entry *) p;
03632              if (h->root.type == bfd_link_hash_warning)
03633               {
03634                 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
03635                 old_ent = (char *) old_ent + htab->root.table.entsize;
03636               }
03637            }
03638        }
03639     }
03640 
03641   weaks = NULL;
03642   ever = extversym != NULL ? extversym + extsymoff : NULL;
03643   for (isym = isymbuf, isymend = isymbuf + extsymcount;
03644        isym < isymend;
03645        isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
03646     {
03647       int bind;
03648       bfd_vma value;
03649       asection *sec, *new_sec;
03650       flagword flags;
03651       const char *name;
03652       struct elf_link_hash_entry *h;
03653       bfd_boolean definition;
03654       bfd_boolean size_change_ok;
03655       bfd_boolean type_change_ok;
03656       bfd_boolean new_weakdef;
03657       bfd_boolean override;
03658       bfd_boolean common;
03659       unsigned int old_alignment;
03660       bfd *old_bfd;
03661 
03662       override = FALSE;
03663 
03664       flags = BSF_NO_FLAGS;
03665       sec = NULL;
03666       value = isym->st_value;
03667       *sym_hash = NULL;
03668       common = bed->common_definition (isym);
03669 
03670       bind = ELF_ST_BIND (isym->st_info);
03671       if (bind == STB_LOCAL)
03672        {
03673          /* This should be impossible, since ELF requires that all
03674             global symbols follow all local symbols, and that sh_info
03675             point to the first global symbol.  Unfortunately, Irix 5
03676             screws this up.  */
03677          continue;
03678        }
03679       else if (bind == STB_GLOBAL)
03680        {
03681          if (isym->st_shndx != SHN_UNDEF && !common)
03682            flags = BSF_GLOBAL;
03683        }
03684       else if (bind == STB_WEAK)
03685        flags = BSF_WEAK;
03686       else
03687        {
03688          /* Leave it up to the processor backend.  */
03689        }
03690 
03691       if (isym->st_shndx == SHN_UNDEF)
03692        sec = bfd_und_section_ptr;
03693       else if (isym->st_shndx < SHN_LORESERVE
03694               || isym->st_shndx > SHN_HIRESERVE)
03695        {
03696          sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
03697          if (sec == NULL)
03698            sec = bfd_abs_section_ptr;
03699          else if (sec->kept_section)
03700            {
03701              /* Symbols from discarded section are undefined.  We keep
03702                its visibility.  */
03703              sec = bfd_und_section_ptr;
03704              isym->st_shndx = SHN_UNDEF;
03705            }
03706          else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
03707            value -= sec->vma;
03708        }
03709       else if (isym->st_shndx == SHN_ABS)
03710        sec = bfd_abs_section_ptr;
03711       else if (isym->st_shndx == SHN_COMMON)
03712        {
03713          sec = bfd_com_section_ptr;
03714          /* What ELF calls the size we call the value.  What ELF
03715             calls the value we call the alignment.  */
03716          value = isym->st_size;
03717        }
03718       else
03719        {
03720          /* Leave it up to the processor backend.  */
03721        }
03722 
03723       name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
03724                                          isym->st_name);
03725       if (name == NULL)
03726        goto error_free_vers;
03727 
03728       if (isym->st_shndx == SHN_COMMON
03729          && ELF_ST_TYPE (isym->st_info) == STT_TLS
03730          && !info->relocatable)
03731        {
03732          asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
03733 
03734          if (tcomm == NULL)
03735            {
03736              tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
03737                                              (SEC_ALLOC
03738                                               | SEC_IS_COMMON
03739                                               | SEC_LINKER_CREATED
03740                                               | SEC_THREAD_LOCAL));
03741              if (tcomm == NULL)
03742               goto error_free_vers;
03743            }
03744          sec = tcomm;
03745        }
03746       else if (bed->elf_add_symbol_hook)
03747        {
03748          if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
03749                                         &sec, &value))
03750            goto error_free_vers;
03751 
03752          /* The hook function sets the name to NULL if this symbol
03753             should be skipped for some reason.  */
03754          if (name == NULL)
03755            continue;
03756        }
03757 
03758       /* Sanity check that all possibilities were handled.  */
03759       if (sec == NULL)
03760        {
03761          bfd_set_error (bfd_error_bad_value);
03762          goto error_free_vers;
03763        }
03764 
03765       if (bfd_is_und_section (sec)
03766          || bfd_is_com_section (sec))
03767        definition = FALSE;
03768       else
03769        definition = TRUE;
03770 
03771       size_change_ok = FALSE;
03772       type_change_ok = bed->type_change_ok;
03773       old_alignment = 0;
03774       old_bfd = NULL;
03775       new_sec = sec;
03776 
03777       if (is_elf_hash_table (htab))
03778        {
03779          Elf_Internal_Versym iver;
03780          unsigned int vernum = 0;
03781          bfd_boolean skip;
03782 
03783          if (ever == NULL)
03784            {
03785              if (info->default_imported_symver)
03786               /* Use the default symbol version created earlier.  */
03787               iver.vs_vers = elf_tdata (abfd)->cverdefs;
03788              else
03789               iver.vs_vers = 0;
03790            }
03791          else
03792            _bfd_elf_swap_versym_in (abfd, ever, &iver);
03793 
03794          vernum = iver.vs_vers & VERSYM_VERSION;
03795 
03796          /* If this is a hidden symbol, or if it is not version
03797             1, we append the version name to the symbol name.
03798             However, we do not modify a non-hidden absolute symbol
03799             if it is not a function, because it might be the version
03800             symbol itself.  FIXME: What if it isn't?  */
03801          if ((iver.vs_vers & VERSYM_HIDDEN) != 0
03802              || (vernum > 1 && (! bfd_is_abs_section (sec)
03803                              || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
03804            {
03805              const char *verstr;
03806              size_t namelen, verlen, newlen;
03807              char *newname, *p;
03808 
03809              if (isym->st_shndx != SHN_UNDEF)
03810               {
03811                 if (vernum > elf_tdata (abfd)->cverdefs)
03812                   verstr = NULL;
03813                 else if (vernum > 1)
03814                   verstr =
03815                     elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
03816                 else
03817                   verstr = "";
03818 
03819                 if (verstr == NULL)
03820                   {
03821                     (*_bfd_error_handler)
03822                      (_("%B: %s: invalid version %u (max %d)"),
03823                       abfd, name, vernum,
03824                       elf_tdata (abfd)->cverdefs);
03825                     bfd_set_error (bfd_error_bad_value);
03826                     goto error_free_vers;
03827                   }
03828               }
03829              else
03830               {
03831                 /* We cannot simply test for the number of
03832                    entries in the VERNEED section since the
03833                    numbers for the needed versions do not start
03834                    at 0.  */
03835                 Elf_Internal_Verneed *t;
03836 
03837                 verstr = NULL;
03838                 for (t = elf_tdata (abfd)->verref;
03839                      t != NULL;
03840                      t = t->vn_nextref)
03841                   {
03842                     Elf_Internal_Vernaux *a;
03843 
03844                     for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
03845                      {
03846                        if (a->vna_other == vernum)
03847                          {
03848                            verstr = a->vna_nodename;
03849                            break;
03850                          }
03851                      }
03852                     if (a != NULL)
03853                      break;
03854                   }
03855                 if (verstr == NULL)
03856                   {
03857                     (*_bfd_error_handler)
03858                      (_("%B: %s: invalid needed version %d"),
03859                       abfd, name, vernum);
03860                     bfd_set_error (bfd_error_bad_value);
03861                     goto error_free_vers;
03862                   }
03863               }
03864 
03865              namelen = strlen (name);
03866              verlen = strlen (verstr);
03867              newlen = namelen + verlen + 2;
03868              if ((iver.vs_vers & VERSYM_HIDDEN) == 0
03869                 && isym->st_shndx != SHN_UNDEF)
03870               ++newlen;
03871 
03872              newname = bfd_hash_allocate (&htab->root.table, newlen);
03873              if (newname == NULL)
03874               goto error_free_vers;
03875              memcpy (newname, name, namelen);
03876              p = newname + namelen;
03877              *p++ = ELF_VER_CHR;
03878              /* If this is a defined non-hidden version symbol,
03879                we add another @ to the name.  This indicates the
03880                default version of the symbol.  */
03881              if ((iver.vs_vers & VERSYM_HIDDEN) == 0
03882                 && isym->st_shndx != SHN_UNDEF)
03883               *p++ = ELF_VER_CHR;
03884              memcpy (p, verstr, verlen + 1);
03885 
03886              name = newname;
03887            }
03888 
03889          if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
03890                                   &value, &old_alignment,
03891                                   sym_hash, &skip, &override,
03892                                   &type_change_ok, &size_change_ok))
03893            goto error_free_vers;
03894 
03895          if (skip)
03896            continue;
03897 
03898          if (override)
03899            definition = FALSE;
03900 
03901          h = *sym_hash;
03902          while (h->root.type == bfd_link_hash_indirect
03903                || h->root.type == bfd_link_hash_warning)
03904            h = (struct elf_link_hash_entry *) h->root.u.i.link;
03905 
03906          /* Remember the old alignment if this is a common symbol, so
03907             that we don't reduce the alignment later on.  We can't
03908             check later, because _bfd_generic_link_add_one_symbol
03909             will set a default for the alignment which we want to
03910             override. We also remember the old bfd where the existing
03911             definition comes from.  */
03912          switch (h->root.type)
03913            {
03914            default:
03915              break;
03916 
03917            case bfd_link_hash_defined:
03918            case bfd_link_hash_defweak:
03919              old_bfd = h->root.u.def.section->owner;
03920              break;
03921 
03922            case bfd_link_hash_common:
03923              old_bfd = h->root.u.c.p->section->owner;
03924              old_alignment = h->root.u.c.p->alignment_power;
03925              break;
03926            }
03927 
03928          if (elf_tdata (abfd)->verdef != NULL
03929              && ! override
03930              && vernum > 1
03931              && definition)
03932            h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
03933        }
03934 
03935       if (! (_bfd_generic_link_add_one_symbol
03936             (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
03937              (struct bfd_link_hash_entry **) sym_hash)))
03938        goto error_free_vers;
03939 
03940       h = *sym_hash;
03941       while (h->root.type == bfd_link_hash_indirect
03942             || h->root.type == bfd_link_hash_warning)
03943        h = (struct elf_link_hash_entry *) h->root.u.i.link;
03944       *sym_hash = h;
03945 
03946       new_weakdef = FALSE;
03947       if (dynamic
03948          && definition
03949          && (flags & BSF_WEAK) != 0
03950          && ELF_ST_TYPE (isym->st_info) != STT_FUNC
03951          && is_elf_hash_table (htab)
03952          && h->u.weakdef == NULL)
03953        {
03954          /* Keep a list of all weak defined non function symbols from
03955             a dynamic object, using the weakdef field.  Later in this
03956             function we will set the weakdef field to the correct
03957             value.  We only put non-function symbols from dynamic
03958             objects on this list, because that happens to be the only
03959             time we need to know the normal symbol corresponding to a
03960             weak symbol, and the information is time consuming to
03961             figure out.  If the weakdef field is not already NULL,
03962             then this symbol was already defined by some previous
03963             dynamic object, and we will be using that previous
03964             definition anyhow.  */
03965 
03966          h->u.weakdef = weaks;
03967          weaks = h;
03968          new_weakdef = TRUE;
03969        }
03970 
03971       /* Set the alignment of a common symbol.  */
03972       if ((common || bfd_is_com_section (sec))
03973          && h->root.type == bfd_link_hash_common)
03974        {
03975          unsigned int align;
03976 
03977          if (common)
03978            align = bfd_log2 (isym->st_value);
03979          else
03980            {
03981              /* The new symbol is a common symbol in a shared object.
03982                We need to get the alignment from the section.  */
03983              align = new_sec->alignment_power;
03984            }
03985          if (align > old_alignment
03986              /* Permit an alignment power of zero if an alignment of one
03987                is specified and no other alignments have been specified.  */
03988              || (isym->st_value == 1 && old_alignment == 0))
03989            h->root.u.c.p->alignment_power = align;
03990          else
03991            h->root.u.c.p->alignment_power = old_alignment;
03992        }
03993 
03994       if (is_elf_hash_table (htab))
03995        {
03996          bfd_boolean dynsym;
03997 
03998          /* Check the alignment when a common symbol is involved. This
03999             can change when a common symbol is overridden by a normal
04000             definition or a common symbol is ignored due to the old
04001             normal definition. We need to make sure the maximum
04002             alignment is maintained.  */
04003          if ((old_alignment || common)
04004              && h->root.type != bfd_link_hash_common)
04005            {
04006              unsigned int common_align;
04007              unsigned int normal_align;
04008              unsigned int symbol_align;
04009              bfd *normal_bfd;
04010              bfd *common_bfd;
04011 
04012              symbol_align = ffs (h->root.u.def.value) - 1;
04013              if (h->root.u.def.section->owner != NULL
04014                 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
04015               {
04016                 normal_align = h->root.u.def.section->alignment_power;
04017                 if (normal_align > symbol_align)
04018                   normal_align = symbol_align;
04019               }
04020              else
04021               normal_align = symbol_align;
04022 
04023              if (old_alignment)
04024               {
04025                 common_align = old_alignment;
04026                 common_bfd = old_bfd;
04027                 normal_bfd = abfd;
04028               }
04029              else
04030               {
04031                 common_align = bfd_log2 (isym->st_value);
04032                 common_bfd = abfd;
04033                 normal_bfd = old_bfd;
04034               }
04035 
04036              if (normal_align < common_align)
04037               {
04038                 /* PR binutils/2735 */
04039                 if (normal_bfd == NULL)
04040                   (*_bfd_error_handler)
04041                     (_("Warning: alignment %u of common symbol `%s' in %B"
04042                       " is greater than the alignment (%u) of its section %A"),
04043                      common_bfd, h->root.u.def.section,
04044                      1 << common_align, name, 1 << normal_align);
04045                 else
04046                   (*_bfd_error_handler)
04047                     (_("Warning: alignment %u of symbol `%s' in %B"
04048                       " is smaller than %u in %B"),
04049                      normal_bfd, common_bfd,
04050                      1 << normal_align, name, 1 << common_align);
04051               }
04052            }
04053 
04054          /* Remember the symbol size if it isn't undefined.  */
04055          if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
04056              && (definition || h->size == 0))
04057            {
04058              if (h->size != 0
04059                 && h->size != isym->st_size
04060                 && ! size_change_ok)
04061               (*_bfd_error_handler)
04062                 (_("Warning: size of symbol `%s' changed"
04063                    " from %lu in %B to %lu in %B"),
04064                  old_bfd, abfd,
04065                  name, (unsigned long) h->size,
04066                  (unsigned long) isym->st_size);
04067 
04068              h->size = isym->st_size;
04069            }
04070 
04071          /* If this is a common symbol, then we always want H->SIZE
04072             to be the size of the common symbol.  The code just above
04073             won't fix the size if a common symbol becomes larger.  We
04074             don't warn about a size change here, because that is
04075             covered by --warn-common.  */
04076          if (h->root.type == bfd_link_hash_common)
04077            h->size = h->root.u.c.size;
04078 
04079          if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
04080              && (definition || h->type == STT_NOTYPE))
04081            {
04082              if (h->type != STT_NOTYPE
04083                 && h->type != ELF_ST_TYPE (isym->st_info)
04084                 && ! type_change_ok)
04085               (*_bfd_error_handler)
04086                 (_("Warning: type of symbol `%s' changed"
04087                    " from %d to %d in %B"),
04088                  abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
04089 
04090              h->type = ELF_ST_TYPE (isym->st_info);
04091            }
04092 
04093          /* If st_other has a processor-specific meaning, specific
04094             code might be needed here. We never merge the visibility
04095             attribute with the one from a dynamic object.  */
04096          if (bed->elf_backend_merge_symbol_attribute)
04097            (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
04098                                                  dynamic);
04099 
04100          /* If this symbol has default visibility and the user has requested
04101             we not re-export it, then mark it as hidden.  */
04102          if (definition && !dynamic
04103              && (abfd->no_export
04104                 || (abfd->my_archive && abfd->my_archive->no_export))
04105              && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
04106            isym->st_other = (STV_HIDDEN
04107                            | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
04108 
04109          if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
04110            {
04111              unsigned char hvis, symvis, other, nvis;
04112 
04113              /* Only merge the visibility. Leave the remainder of the
04114                st_other field to elf_backend_merge_symbol_attribute.  */
04115              other = h->other & ~ELF_ST_VISIBILITY (-1);
04116 
04117              /* Combine visibilities, using the most constraining one.  */
04118              hvis   = ELF_ST_VISIBILITY (h->other);
04119              symvis = ELF_ST_VISIBILITY (isym->st_other);
04120              if (! hvis)
04121               nvis = symvis;
04122              else if (! symvis)
04123               nvis = hvis;
04124              else
04125               nvis = hvis < symvis ? hvis : symvis;
04126 
04127              h->other = other | nvis;
04128            }
04129 
04130          /* Set a flag in the hash table entry indicating the type of
04131             reference or definition we just found.  Keep a count of
04132             the number of dynamic symbols we find.  A dynamic symbol
04133             is one which is referenced or defined by both a regular
04134             object and a shared object.  */
04135          dynsym = FALSE;
04136          if (! dynamic)
04137            {
04138              if (! definition)
04139               {
04140                 h->ref_regular = 1;
04141                 if (bind != STB_WEAK)
04142                   h->ref_regular_nonweak = 1;
04143               }
04144              else
04145               h->def_regular = 1;
04146              if (! info->executable
04147                 || h->def_dynamic
04148                 || h->ref_dynamic)
04149               dynsym = TRUE;
04150            }
04151          else
04152            {
04153              if (! definition)
04154               h->ref_dynamic = 1;
04155              else
04156               h->def_dynamic = 1;
04157              if (h->def_regular
04158                 || h->ref_regular
04159                 || (h->u.weakdef != NULL
04160                     && ! new_weakdef
04161                     && h->u.weakdef->dynindx != -1))
04162               dynsym = TRUE;
04163            }
04164 
04165          if (definition && (sec->flags & SEC_DEBUGGING))
04166            {
04167              /* We don't want to make debug symbol dynamic.  */
04168              (*bed->elf_backend_hide_symbol) (info, h, TRUE);
04169              dynsym = FALSE;
04170            }
04171 
04172          /* Check to see if we need to add an indirect symbol for
04173             the default name.  */
04174          if (definition || h->root.type == bfd_link_hash_common)
04175            if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
04176                                          &sec, &value, &dynsym,
04177                                          override))
04178              goto error_free_vers;
04179 
04180          if (definition && !dynamic)
04181            {
04182              char *p = strchr (name, ELF_VER_CHR);
04183              if (p != NULL && p[1] != ELF_VER_CHR)
04184               {
04185                 /* Queue non-default versions so that .symver x, x@FOO
04186                    aliases can be checked.  */
04187                 if (!nondeflt_vers)
04188                   {
04189                     amt = ((isymend - isym + 1)
04190                           * sizeof (struct elf_link_hash_entry *));
04191                     nondeflt_vers = bfd_malloc (amt);
04192                   }
04193                 nondeflt_vers[nondeflt_vers_cnt++] = h;
04194               }
04195            }
04196 
04197          if (dynsym && h->dynindx == -1)
04198            {
04199              if (! bfd_elf_link_record_dynamic_symbol (info, h))
04200               goto error_free_vers;
04201              if (h->u.weakdef != NULL
04202                 && ! new_weakdef
04203                 && h->u.weakdef->dynindx == -1)
04204               {
04205                 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
04206                   goto error_free_vers;
04207               }
04208            }
04209          else if (dynsym && h->dynindx != -1)
04210            /* If the symbol already has a dynamic index, but
04211               visibility says it should not be visible, turn it into
04212               a local symbol.  */
04213            switch (ELF_ST_VISIBILITY (h->other))
04214              {
04215              case STV_INTERNAL:
04216              case STV_HIDDEN:
04217               (*bed->elf_backend_hide_symbol) (info, h, TRUE);
04218               dynsym = FALSE;
04219               break;
04220              }
04221 
04222          if (!add_needed
04223              && definition
04224              && dynsym
04225              && h->ref_regular)
04226            {
04227              int ret;
04228              const char *soname = elf_dt_name (abfd);
04229 
04230              /* A symbol from a library loaded via DT_NEEDED of some
04231                other library is referenced by a regular object.
04232                Add a DT_NEEDED entry for it.  Issue an error if
04233                --no-add-needed is used.  */
04234              if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
04235               {
04236                 (*_bfd_error_handler)
04237                   (_("%s: invalid DSO for symbol `%s' definition"),
04238                    abfd, name);
04239                 bfd_set_error (bfd_error_bad_value);
04240                 goto error_free_vers;
04241               }
04242 
04243              elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
04244 
04245              add_needed = TRUE;
04246              ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
04247              if (ret < 0)
04248               goto error_free_vers;
04249 
04250              BFD_ASSERT (ret == 0);
04251            }
04252        }
04253     }
04254 
04255   if (extversym != NULL)
04256     {
04257       free (extversym);
04258       extversym = NULL;
04259     }
04260 
04261   if (isymbuf != NULL)
04262     {
04263       free (isymbuf);
04264       isymbuf = NULL;
04265     }
04266 
04267   if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
04268     {
04269       unsigned int i;
04270 
04271       /* Restore the symbol table.  */
04272       if (bed->as_needed_cleanup)
04273        (*bed->as_needed_cleanup) (abfd, info);
04274       old_hash = (char *) old_tab + tabsize;
04275       old_ent = (char *) old_hash + hashsize;
04276       sym_hash = elf_sym_hashes (abfd);
04277       htab->root.table.table = old_table;
04278       htab->root.table.size = old_size;
04279       htab->root.table.count = old_count;
04280       memcpy (htab->root.table.table, old_tab, tabsize);
04281       memcpy (sym_hash, old_hash, hashsize);
04282       htab->root.undefs = old_undefs;
04283       htab->root.undefs_tail = old_undefs_tail;
04284       for (i = 0; i < htab->root.table.size; i++)
04285        {
04286          struct bfd_hash_entry *p;
04287          struct elf_link_hash_entry *h;
04288 
04289          for (p = htab->root.table.table[i]; p != NULL; p = p->next)
04290            {
04291              h = (struct elf_link_hash_entry *) p;
04292              if (h->root.type == bfd_link_hash_warning)
04293               h = (struct elf_link_hash_entry *) h->root.u.i.link;
04294              if (h->dynindx >= old_dynsymcount)
04295               _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
04296 
04297              memcpy (p, old_ent, htab->root.table.entsize);
04298              old_ent = (char *) old_ent + htab->root.table.entsize;
04299              h = (struct elf_link_hash_entry *) p;
04300              if (h->root.type == bfd_link_hash_warning)
04301               {
04302                 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
04303                 old_ent = (char *) old_ent + htab->root.table.entsize;
04304               }
04305            }
04306        }
04307 
04308       /* Make a special call to the linker "notice" function to
04309         tell it that symbols added for crefs may need to be removed.  */
04310       if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
04311                                    notice_not_needed))
04312        return FALSE;
04313 
04314       free (old_tab);
04315       objalloc_free_block ((struct objalloc *) htab->root.table.memory,
04316                         alloc_mark);
04317       if (nondeflt_vers != NULL)
04318        free (nondeflt_vers);
04319       return TRUE;
04320     }
04321 
04322   if (old_tab != NULL)
04323     {
04324       if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
04325                                    notice_needed))
04326        return FALSE;
04327       free (old_tab);
04328       old_tab = NULL;
04329     }
04330 
04331   /* Now that all the symbols from this input file are created, handle
04332      .symver foo, foo@BAR such that any relocs against foo become foo@BAR.  */
04333   if (nondeflt_vers != NULL)
04334     {
04335       bfd_size_type cnt, symidx;
04336 
04337       for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
04338        {
04339          struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
04340          char *shortname, *p;
04341 
04342          p = strchr (h->root.root.string, ELF_VER_CHR);
04343          if (p == NULL
04344              || (h->root.type != bfd_link_hash_defined
04345                 && h->root.type != bfd_link_hash_defweak))
04346            continue;
04347 
04348          amt = p - h->root.root.string;
04349          shortname = bfd_malloc (amt + 1);
04350          memcpy (shortname, h->root.root.string, amt);
04351          shortname[amt] = '\0';
04352 
04353          hi = (struct elf_link_hash_entry *)
04354               bfd_link_hash_lookup (&htab->root, shortname,
04355                                  FALSE, FALSE, FALSE);
04356          if (hi != NULL
04357              && hi->root.type == h->root.type
04358              && hi->root.u.def.value == h->root.u.def.value
04359              && hi->root.u.def.section == h->root.u.def.section)
04360            {
04361              (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
04362              hi->root.type = bfd_link_hash_indirect;
04363              hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
04364              (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
04365              sym_hash = elf_sym_hashes (abfd);
04366              if (sym_hash)
04367               for (symidx = 0; symidx < extsymcount; ++symidx)
04368                 if (sym_hash[symidx] == hi)
04369                   {
04370                     sym_hash[symidx] = h;
04371                     break;
04372                   }
04373            }
04374          free (shortname);
04375        }
04376       free (nondeflt_vers);
04377       nondeflt_vers = NULL;
04378     }
04379 
04380   /* Now set the weakdefs field correctly for all the weak defined
04381      symbols we found.  The only way to do this is to search all the
04382      symbols.  Since we only need the information for non functions in
04383      dynamic objects, that's the only time we actually put anything on
04384      the list WEAKS.  We need this information so that if a regular
04385      object refers to a symbol defined weakly in a dynamic object, the
04386      real symbol in the dynamic object is also put in the dynamic
04387      symbols; we also must arrange for both symbols to point to the
04388      same memory location.  We could handle the general case of symbol
04389      aliasing, but a general symbol alias can only be generated in
04390      assembler code, handling it correctly would be very time
04391      consuming, and other ELF linkers don't handle general aliasing
04392      either.  */
04393   if (weaks != NULL)
04394     {
04395       struct elf_link_hash_entry **hpp;
04396       struct elf_link_hash_entry **hppend;
04397       struct elf_link_hash_entry **sorted_sym_hash;
04398       struct elf_link_hash_entry *h;
04399       size_t sym_count;
04400 
04401       /* Since we have to search the whole symbol list for each weak
04402         defined symbol, search time for N weak defined symbols will be
04403         O(N^2). Binary search will cut it down to O(NlogN).  */
04404       amt = extsymcount * sizeof (struct elf_link_hash_entry *);
04405       sorted_sym_hash = bfd_malloc (amt);
04406       if (sorted_sym_hash == NULL)
04407        goto error_return;
04408       sym_hash = sorted_sym_hash;
04409       hpp = elf_sym_hashes (abfd);
04410       hppend = hpp + extsymcount;
04411       sym_count = 0;
04412       for (; hpp < hppend; hpp++)
04413        {
04414          h = *hpp;
04415          if (h != NULL
04416              && h->root.type == bfd_link_hash_defined
04417              && h->type != STT_FUNC)
04418            {
04419              *sym_hash = h;
04420              sym_hash++;
04421              sym_count++;
04422            }
04423        }
04424 
04425       qsort (sorted_sym_hash, sym_count,
04426             sizeof (struct elf_link_hash_entry *),
04427             elf_sort_symbol);
04428 
04429       while (weaks != NULL)
04430        {
04431          struct elf_link_hash_entry *hlook;
04432          asection *slook;
04433          bfd_vma vlook;
04434          long ilook;
04435          size_t i, j, idx;
04436 
04437          hlook = weaks;
04438          weaks = hlook->u.weakdef;
04439          hlook->u.weakdef = NULL;
04440 
04441          BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
04442                     || hlook->root.type == bfd_link_hash_defweak
04443                     || hlook->root.type == bfd_link_hash_common
04444                     || hlook->root.type == bfd_link_hash_indirect);
04445          slook = hlook->root.u.def.section;
04446          vlook = hlook->root.u.def.value;
04447 
04448          ilook = -1;
04449          i = 0;
04450          j = sym_count;
04451          while (i < j)
04452            {
04453              bfd_signed_vma vdiff;
04454              idx = (i + j) / 2;
04455              h = sorted_sym_hash [idx];
04456              vdiff = vlook - h->root.u.def.value;
04457              if (vdiff < 0)
04458               j = idx;
04459              else if (vdiff > 0)
04460               i = idx + 1;
04461              else
04462               {
04463                 long sdiff = slook->id - h->root.u.def.section->id;
04464                 if (sdiff < 0)
04465                   j = idx;
04466                 else if (sdiff > 0)
04467                   i = idx + 1;
04468                 else
04469                   {
04470                     ilook = idx;
04471                     break;
04472                   }
04473               }
04474            }
04475 
04476          /* We didn't find a value/section match.  */
04477          if (ilook == -1)
04478            continue;
04479 
04480          for (i = ilook; i < sym_count; i++)
04481            {
04482              h = sorted_sym_hash [i];
04483 
04484              /* Stop if value or section doesn't match.  */
04485              if (h->root.u.def.value != vlook
04486                 || h->root.u.def.section != slook)
04487               break;
04488              else if (h != hlook)
04489               {
04490                 hlook->u.weakdef = h;
04491 
04492                 /* If the weak definition is in the list of dynamic
04493                    symbols, make sure the real definition is put
04494                    there as well.  */
04495                 if (hlook->dynindx != -1 && h->dynindx == -1)
04496                   {
04497                     if (! bfd_elf_link_record_dynamic_symbol (info, h))
04498                      goto error_return;
04499                   }
04500 
04501                 /* If the real definition is in the list of dynamic
04502                    symbols, make sure the weak definition is put
04503                    there as well.  If we don't do this, then the
04504                    dynamic loader might not merge the entries for the
04505                    real definition and the weak definition.  */
04506                 if (h->dynindx != -1 && hlook->dynindx == -1)
04507                   {
04508                     if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
04509                      goto error_return;
04510                   }
04511                 break;
04512               }
04513            }
04514        }
04515 
04516       free (sorted_sym_hash);
04517     }
04518 
04519   if (bed->check_directives)
04520     (*bed->check_directives) (abfd, info);
04521 
04522   /* If this object is the same format as the output object, and it is
04523      not a shared library, then let the backend look through the
04524      relocs.
04525 
04526      This is required to build global offset table entries and to
04527      arrange for dynamic relocs.  It is not required for the
04528      particular common case of linking non PIC code, even when linking
04529      against shared libraries, but unfortunately there is no way of
04530      knowing whether an object file has been compiled PIC or not.
04531      Looking through the relocs is not particularly time consuming.
04532      The problem is that we must either (1) keep the relocs in memory,
04533      which causes the linker to require additional runtime memory or
04534      (2) read the relocs twice from the input file, which wastes time.
04535      This would be a good case for using mmap.
04536 
04537      I have no idea how to handle linking PIC code into a file of a
04538      different format.  It probably can't be done.  */
04539   if (! dynamic
04540       && is_elf_hash_table (htab)
04541       && htab->root.creator == abfd->xvec
04542       && bed->check_relocs != NULL)
04543     {
04544       asection *o;
04545 
04546       for (o = abfd->sections; o != NULL; o = o->next)
04547        {
04548          Elf_Internal_Rela *internal_relocs;
04549          bfd_boolean ok;
04550 
04551          if ((o->flags & SEC_RELOC) == 0
04552              || o->reloc_count == 0
04553              || ((info->strip == strip_all || info->strip == strip_debugger)
04554                 && (o->flags & SEC_DEBUGGING) != 0)
04555              || bfd_is_abs_section (o->output_section))
04556            continue;
04557 
04558          internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
04559                                                  info->keep_memory);
04560          if (internal_relocs == NULL)
04561            goto error_return;
04562 
04563          ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
04564 
04565          if (elf_section_data (o)->relocs != internal_relocs)
04566            free (internal_relocs);
04567 
04568          if (! ok)
04569            goto error_return;
04570        }
04571     }
04572 
04573   /* If this is a non-traditional link, try to optimize the handling
04574      of the .stab/.stabstr sections.  */
04575   if (! dynamic
04576       && ! info->traditional_format
04577       && is_elf_hash_table (htab)
04578       && (info->strip != strip_all && info->strip != strip_debugger))
04579     {
04580       asection *stabstr;
04581 
04582       stabstr = bfd_get_section_by_name (abfd, ".stabstr");
04583       if (stabstr != NULL)
04584        {
04585          bfd_size_type string_offset = 0;
04586          asection *stab;
04587 
04588          for (stab = abfd->sections; stab; stab = stab->next)
04589            if (CONST_STRNEQ (stab->name, ".stab")
04590               && (!stab->name[5] ||
04591                   (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
04592               && (stab->flags & SEC_MERGE) == 0
04593               && !bfd_is_abs_section (stab->output_section))
04594              {
04595               struct bfd_elf_section_data *secdata;
04596 
04597               secdata = elf_section_data (stab);
04598               if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
04599                                           stabstr, &secdata->sec_info,
04600                                           &string_offset))
04601                 goto error_return;
04602               if (secdata->sec_info)
04603                 stab->sec_info_type = ELF_INFO_TYPE_STABS;
04604            }
04605        }
04606     }
04607 
04608   if (is_elf_hash_table (htab) && add_needed)
04609     {
04610       /* Add this bfd to the loaded list.  */
04611       struct elf_link_loaded_list *n;
04612 
04613       n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
04614       if (n == NULL)
04615        goto error_return;
04616       n->abfd = abfd;
04617       n->next = htab->loaded;
04618       htab->loaded = n;
04619     }
04620 
04621   return TRUE;
04622 
04623  error_free_vers:
04624   if (old_tab != NULL)
04625     free (old_tab);
04626   if (nondeflt_vers != NULL)
04627     free (nondeflt_vers);
04628   if (extversym != NULL)
04629     free (extversym);
04630  error_free_sym:
04631   if (isymbuf != NULL)
04632     free (isymbuf);
04633  error_return:
04634   return FALSE;
04635 }
04636 
04637 /* Return the linker hash table entry of a symbol that might be
04638    satisfied by an archive symbol.  Return -1 on error.  */
04639 
04640 struct elf_link_hash_entry *
04641 _bfd_elf_archive_symbol_lookup (bfd *abfd,
04642                             struct bfd_link_info *info,
04643                             const char *name)
04644 {
04645   struct elf_link_hash_entry *h;
04646   char *p, *copy;
04647   size_t len, first;
04648 
04649   h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
04650   if (h != NULL)
04651     return h;
04652 
04653   /* If this is a default version (the name contains @@), look up the
04654      symbol again with only one `@' as well as without the version.
04655      The effect is that references to the symbol with and without the
04656      version will be matched by the default symbol in the archive.  */
04657 
04658   p = strchr (name, ELF_VER_CHR);
04659   if (p == NULL || p[1] != ELF_VER_CHR)
04660     return h;
04661 
04662   /* First check with only one `@'.  */
04663   len = strlen (name);
04664   copy = bfd_alloc (abfd, len);
04665   if (copy == NULL)
04666     return (struct elf_link_hash_entry *) 0 - 1;
04667 
04668   first = p - name + 1;
04669   memcpy (copy, name, first);
04670   memcpy (copy + first, name + first + 1, len - first);
04671 
04672   h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
04673   if (h == NULL)
04674     {
04675       /* We also need to check references to the symbol without the
04676         version.  */
04677       copy[first - 1] = '\0';
04678       h = elf_link_hash_lookup (elf_hash_table (info), copy,
04679                             FALSE, FALSE, FALSE);
04680     }
04681 
04682   bfd_release (abfd, copy);
04683   return h;
04684 }
04685 
04686 /* Add symbols from an ELF archive file to the linker hash table.  We
04687    don't use _bfd_generic_link_add_archive_symbols because of a
04688    problem which arises on UnixWare.  The UnixWare libc.so is an
04689    archive which includes an entry libc.so.1 which defines a bunch of
04690    symbols.  The libc.so archive also includes a number of other
04691    object files, which also define symbols, some of which are the same
04692    as those defined in libc.so.1.  Correct linking requires that we
04693    consider each object file in turn, and include it if it defines any
04694    symbols we need.  _bfd_generic_link_add_archive_symbols does not do
04695    this; it looks through the list of undefined symbols, and includes
04696    any object file which defines them.  When this algorithm is used on
04697    UnixWare, it winds up pulling in libc.so.1 early and defining a
04698    bunch of symbols.  This means that some of the other objects in the
04699    archive are not included in the link, which is incorrect since they
04700    precede libc.so.1 in the archive.
04701 
04702    Fortunately, ELF archive handling is simpler than that done by
04703    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
04704    oddities.  In ELF, if we find a symbol in the archive map, and the
04705    symbol is currently undefined, we know that we must pull in that
04706    object file.
04707 
04708    Unfortunately, we do have to make multiple passes over the symbol
04709    table until nothing further is resolved.  */
04710 
04711 static bfd_boolean
04712 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
04713 {
04714   symindex c;
04715   bfd_boolean *defined = NULL;
04716   bfd_boolean *included = NULL;
04717   carsym *symdefs;
04718   bfd_boolean loop;
04719   bfd_size_type amt;
04720   const struct elf_backend_data *bed;
04721   struct elf_link_hash_entry * (*archive_symbol_lookup)
04722     (bfd *, struct bfd_link_info *, const char *);
04723 
04724   if (! bfd_has_map (abfd))
04725     {
04726       /* An empty archive is a special case.  */
04727       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
04728        return TRUE;
04729       bfd_set_error (bfd_error_no_armap);
04730       return FALSE;
04731     }
04732 
04733   /* Keep track of all symbols we know to be already defined, and all
04734      files we know to be already included.  This is to speed up the
04735      second and subsequent passes.  */
04736   c = bfd_ardata (abfd)->symdef_count;
04737   if (c == 0)
04738     return TRUE;
04739   amt = c;
04740   amt *= sizeof (bfd_boolean);
04741   defined = bfd_zmalloc (amt);
04742   included = bfd_zmalloc (amt);
04743   if (defined == NULL || included == NULL)
04744     goto error_return;
04745 
04746   symdefs = bfd_ardata (abfd)->symdefs;
04747   bed = get_elf_backend_data (abfd);
04748   archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
04749 
04750   do
04751     {
04752       file_ptr last;
04753       symindex i;
04754       carsym *symdef;
04755       carsym *symdefend;
04756 
04757       loop = FALSE;
04758       last = -1;
04759 
04760       symdef = symdefs;
04761       symdefend = symdef + c;
04762       for (i = 0; symdef < symdefend; symdef++, i++)
04763        {
04764          struct elf_link_hash_entry *h;
04765          bfd *element;
04766          struct bfd_link_hash_entry *undefs_tail;
04767          symindex mark;
04768 
04769          if (defined[i] || included[i])
04770            continue;
04771          if (symdef->file_offset == last)
04772            {
04773              included[i] = TRUE;
04774              continue;
04775            }
04776 
04777          h = archive_symbol_lookup (abfd, info, symdef->name);
04778          if (h == (struct elf_link_hash_entry *) 0 - 1)
04779            goto error_return;
04780 
04781          if (h == NULL)
04782            continue;
04783 
04784          if (h->root.type == bfd_link_hash_common)
04785            {
04786              /* We currently have a common symbol.  The archive map contains
04787                a reference to this symbol, so we may want to include it.  We
04788                only want to include it however, if this archive element
04789                contains a definition of the symbol, not just another common
04790                declaration of it.
04791 
04792                Unfortunately some archivers (including GNU ar) will put
04793                declarations of common symbols into their archive maps, as
04794                well as real definitions, so we cannot just go by the archive
04795                map alone.  Instead we must read in the element's symbol
04796                table and check that to see what kind of symbol definition
04797                this is.  */
04798              if (! elf_link_is_defined_archive_symbol (abfd, symdef))
04799               continue;
04800            }
04801          else if (h->root.type != bfd_link_hash_undefined)
04802            {
04803              if (h->root.type != bfd_link_hash_undefweak)
04804               defined[i] = TRUE;
04805              continue;
04806            }
04807 
04808          /* We need to include this archive member.  */
04809          element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
04810          if (element == NULL)
04811            goto error_return;
04812 
04813          if (! bfd_check_format (element, bfd_object))
04814            goto error_return;
04815 
04816          /* Doublecheck that we have not included this object
04817             already--it should be impossible, but there may be
04818             something wrong with the archive.  */
04819          if (element->archive_pass != 0)
04820            {
04821              bfd_set_error (bfd_error_bad_value);
04822              goto error_return;
04823            }
04824          element->archive_pass = 1;
04825 
04826          undefs_tail = info->hash->undefs_tail;
04827 
04828          if (! (*info->callbacks->add_archive_element) (info, element,
04829                                                   symdef->name))
04830            goto error_return;
04831          if (! bfd_link_add_symbols (element, info))
04832            goto error_return;
04833 
04834          /* If there are any new undefined symbols, we need to make
04835             another pass through the archive in order to see whether
04836             they can be defined.  FIXME: This isn't perfect, because
04837             common symbols wind up on undefs_tail and because an
04838             undefined symbol which is defined later on in this pass
04839             does not require another pass.  This isn't a bug, but it
04840             does make the code less efficient than it could be.  */
04841          if (undefs_tail != info->hash->undefs_tail)
04842            loop = TRUE;
04843 
04844          /* Look backward to mark all symbols from this object file
04845             which we have already seen in this pass.  */
04846          mark = i;
04847          do
04848            {
04849              included[mark] = TRUE;
04850              if (mark == 0)
04851               break;
04852              --mark;
04853            }
04854          while (symdefs[mark].file_offset == symdef->file_offset);
04855 
04856          /* We mark subsequent symbols from this object file as we go
04857             on through the loop.  */
04858          last = symdef->file_offset;
04859        }
04860     }
04861   while (loop);
04862 
04863   free (defined);
04864   free (included);
04865 
04866   return TRUE;
04867 
04868  error_return:
04869   if (defined != NULL)
04870     free (defined);
04871   if (included != NULL)
04872     free (included);
04873   return FALSE;
04874 }
04875 
04876 /* Given an ELF BFD, add symbols to the global hash table as
04877    appropriate.  */
04878 
04879 bfd_boolean
04880 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
04881 {
04882   switch (bfd_get_format (abfd))
04883     {
04884     case bfd_object:
04885       return elf_link_add_object_symbols (abfd, info);
04886     case bfd_archive:
04887       return elf_link_add_archive_symbols (abfd, info);
04888     default:
04889       bfd_set_error (bfd_error_wrong_format);
04890       return FALSE;
04891     }
04892 }
04893 
04894 /* This function will be called though elf_link_hash_traverse to store
04895    all hash value of the exported symbols in an array.  */
04896 
04897 static bfd_boolean
04898 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
04899 {
04900   unsigned long **valuep = data;
04901   const char *name;
04902   char *p;
04903   unsigned long ha;
04904   char *alc = NULL;
04905 
04906   if (h->root.type == bfd_link_hash_warning)
04907     h = (struct elf_link_hash_entry *) h->root.u.i.link;
04908 
04909   /* Ignore indirect symbols.  These are added by the versioning code.  */
04910   if (h->dynindx == -1)
04911     return TRUE;
04912 
04913   name = h->root.root.string;
04914   p = strchr (name, ELF_VER_CHR);
04915   if (p != NULL)
04916     {
04917       alc = bfd_malloc (p - name + 1);
04918       memcpy (alc, name, p - name);
04919       alc[p - name] = '\0';
04920       name = alc;
04921     }
04922 
04923   /* Compute the hash value.  */
04924   ha = bfd_elf_hash (name);
04925 
04926   /* Store the found hash value in the array given as the argument.  */
04927   *(*valuep)++ = ha;
04928 
04929   /* And store it in the struct so that we can put it in the hash table
04930      later.  */
04931   h->u.elf_hash_value = ha;
04932 
04933   if (alc != NULL)
04934     free (alc);
04935 
04936   return TRUE;
04937 }
04938 
04939 struct collect_gnu_hash_codes
04940 {
04941   bfd *output_bfd;
04942   const struct elf_backend_data *bed;
04943   unsigned long int nsyms;
04944   unsigned long int maskbits;
04945   unsigned long int *hashcodes;
04946   unsigned long int *hashval;
04947   unsigned long int *indx;
04948   unsigned long int *counts;
04949   bfd_vma *bitmask;
04950   bfd_byte *contents;
04951   long int min_dynindx;
04952   unsigned long int bucketcount;
04953   unsigned long int symindx;
04954   long int local_indx;
04955   long int shift1, shift2;
04956   unsigned long int mask;
04957 };
04958 
04959 /* This function will be called though elf_link_hash_traverse to store
04960    all hash value of the exported symbols in an array.  */
04961 
04962 static bfd_boolean
04963 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
04964 {
04965   struct collect_gnu_hash_codes *s = data;
04966   const char *name;
04967   char *p;
04968   unsigned long ha;
04969   char *alc = NULL;
04970 
04971   if (h->root.type == bfd_link_hash_warning)
04972     h = (struct elf_link_hash_entry *) h->root.u.i.link;
04973 
04974   /* Ignore indirect symbols.  These are added by the versioning code.  */
04975   if (h->dynindx == -1)
04976     return TRUE;
04977 
04978   /* Ignore also local symbols and undefined symbols.  */
04979   if (! (*s->bed->elf_hash_symbol) (h))
04980     return TRUE;
04981 
04982   name = h->root.root.string;
04983   p = strchr (name, ELF_VER_CHR);
04984   if (p != NULL)
04985     {
04986       alc = bfd_malloc (p - name + 1);
04987       memcpy (alc, name, p - name);
04988       alc[p - name] = '\0';
04989       name = alc;
04990     }
04991 
04992   /* Compute the hash value.  */
04993   ha = bfd_elf_gnu_hash (name);
04994 
04995   /* Store the found hash value in the array for compute_bucket_count,
04996      and also for .dynsym reordering purposes.  */
04997   s->hashcodes[s->nsyms] = ha;
04998   s->hashval[h->dynindx] = ha;
04999   ++s->nsyms;
05000   if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
05001     s->min_dynindx = h->dynindx;
05002 
05003   if (alc != NULL)
05004     free (alc);
05005 
05006   return TRUE;
05007 }
05008 
05009 /* This function will be called though elf_link_hash_traverse to do
05010    final dynaminc symbol renumbering.  */
05011 
05012 static bfd_boolean
05013 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
05014 {
05015   struct collect_gnu_hash_codes *s = data;
05016   unsigned long int bucket;
05017   unsigned long int val;
05018 
05019   if (h->root.type == bfd_link_hash_warning)
05020     h = (struct elf_link_hash_entry *) h->root.u.i.link;
05021 
05022   /* Ignore indirect symbols.  */
05023   if (h->dynindx == -1)
05024     return TRUE;
05025 
05026   /* Ignore also local symbols and undefined symbols.  */
05027   if (! (*s->bed->elf_hash_symbol) (h))
05028     {
05029       if (h->dynindx >= s->min_dynindx)
05030        h->dynindx = s->local_indx++;
05031       return TRUE;
05032     }
05033 
05034   bucket = s->hashval[h->dynindx] % s->bucketcount;
05035   val = (s->hashval[h->dynindx] >> s->shift1)
05036        & ((s->maskbits >> s->shift1) - 1);
05037   s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
05038   s->bitmask[val]
05039     |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
05040   val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
05041   if (s->counts[bucket] == 1)
05042     /* Last element terminates the chain.  */
05043     val |= 1;
05044   bfd_put_32 (s->output_bfd, val,
05045              s->contents + (s->indx[bucket] - s->symindx) * 4);
05046   --s->counts[bucket];
05047   h->dynindx = s->indx[bucket]++;
05048   return TRUE;
05049 }
05050 
05051 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
05052 
05053 bfd_boolean
05054 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
05055 {
05056   return !(h->forced_local
05057           || h->root.type == bfd_link_hash_undefined
05058           || h->root.type == bfd_link_hash_undefweak
05059           || ((h->root.type == bfd_link_hash_defined
05060               || h->root.type == bfd_link_hash_defweak)
05061               && h->root.u.def.section->output_section == NULL));
05062 }
05063 
05064 /* Array used to determine the number of hash table buckets to use
05065    based on the number of symbols there are.  If there are fewer than
05066    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
05067    fewer than 37 we use 17 buckets, and so forth.  We never use more
05068    than 32771 buckets.  */
05069 
05070 static const size_t elf_buckets[] =
05071 {
05072   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
05073   16411, 32771, 0
05074 };
05075 
05076 /* Compute bucket count for hashing table.  We do not use a static set
05077    of possible tables sizes anymore.  Instead we determine for all
05078    possible reasonable sizes of the table the outcome (i.e., the
05079    number of collisions etc) and choose the best solution.  The
05080    weighting functions are not too simple to allow the table to grow
05081    without bounds.  Instead one of the weighting factors is the size.
05082    Therefore the result is always a good payoff between few collisions
05083    (= short chain lengths) and table size.  */
05084 static size_t
05085 compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes,
05086                     unsigned long int nsyms, int gnu_hash)
05087 {
05088   size_t dynsymcount = elf_hash_table (info)->dynsymcount;
05089   size_t best_size = 0;
05090   unsigned long int i;
05091   bfd_size_type amt;
05092 
05093   /* We have a problem here.  The following code to optimize the table
05094      size requires an integer type with more the 32 bits.  If
05095      BFD_HOST_U_64_BIT is set we know about such a type.  */
05096 #ifdef BFD_HOST_U_64_BIT
05097   if (info->optimize)
05098     {
05099       size_t minsize;
05100       size_t maxsize;
05101       BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
05102       bfd *dynobj = elf_hash_table (info)->dynobj;
05103       const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
05104       unsigned long int *counts;
05105 
05106       /* Possible optimization parameters: if we have NSYMS symbols we say
05107         that the hashing table must at least have NSYMS/4 and at most
05108         2*NSYMS buckets.  */
05109       minsize = nsyms / 4;
05110       if (minsize == 0)
05111        minsize = 1;
05112       best_size = maxsize = nsyms * 2;
05113       if (gnu_hash)
05114        {
05115          if (minsize < 2)
05116            minsize = 2;
05117          if ((best_size & 31) == 0)
05118            ++best_size;
05119        }
05120 
05121       /* Create array where we count the collisions in.  We must use bfd_malloc
05122         since the size could be large.  */
05123       amt = maxsize;
05124       amt *= sizeof (unsigned long int);
05125       counts = bfd_malloc (amt);
05126       if (counts == NULL)
05127        return 0;
05128 
05129       /* Compute the "optimal" size for the hash table.  The criteria is a
05130         minimal chain length.  The minor criteria is (of course) the size
05131         of the table.  */
05132       for (i = minsize; i < maxsize; ++i)
05133        {
05134          /* Walk through the array of hashcodes and count the collisions.  */
05135          BFD_HOST_U_64_BIT max;
05136          unsigned long int j;
05137          unsigned long int fact;
05138 
05139          if (gnu_hash && (i & 31) == 0)
05140            continue;
05141 
05142          memset (counts, '\0', i * sizeof (unsigned long int));
05143 
05144          /* Determine how often each hash bucket is used.  */
05145          for (j = 0; j < nsyms; ++j)
05146            ++counts[hashcodes[j] % i];
05147 
05148          /* For the weight function we need some information about the
05149             pagesize on the target.  This is information need not be 100%
05150             accurate.  Since this information is not available (so far) we
05151             define it here to a reasonable default value.  If it is crucial
05152             to have a better value some day simply define this value.  */
05153 # ifndef BFD_TARGET_PAGESIZE
05154 #  define BFD_TARGET_PAGESIZE      (4096)
05155 # endif
05156 
05157          /* We in any case need 2 + DYNSYMCOUNT entries for the size values
05158             and the chains.  */
05159          max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
05160 
05161 # if 1
05162          /* Variant 1: optimize for short chains.  We add the squares
05163             of all the chain lengths (which favors many small chain
05164             over a few long chains).  */
05165          for (j = 0; j < i; ++j)
05166            max += counts[j] * counts[j];
05167 
05168          /* This adds penalties for the overall size of the table.  */
05169          fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
05170          max *= fact * fact;
05171 # else
05172          /* Variant 2: Optimize a lot more for small table.  Here we
05173             also add squares of the size but we also add penalties for
05174             empty slots (the +1 term).  */
05175          for (j = 0; j < i; ++j)
05176            max += (1 + counts[j]) * (1 + counts[j]);
05177 
05178          /* The overall size of the table is considered, but not as
05179             strong as in variant 1, where it is squared.  */
05180          fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
05181          max *= fact;
05182 # endif
05183 
05184          /* Compare with current best results.  */
05185          if (max < best_chlen)
05186            {
05187              best_chlen = max;
05188              best_size = i;
05189            }
05190        }
05191 
05192       free (counts);
05193     }
05194   else
05195 #endif /* defined (BFD_HOST_U_64_BIT) */
05196     {
05197       /* This is the fallback solution if no 64bit type is available or if we
05198         are not supposed to spend much time on optimizations.  We select the
05199         bucket count using a fixed set of numbers.  */
05200       for (i = 0; elf_buckets[i] != 0; i++)
05201        {
05202          best_size = elf_buckets[i];
05203          if (nsyms < elf_buckets[i + 1])
05204            break;
05205        }
05206       if (gnu_hash && best_size < 2)
05207        best_size = 2;
05208     }
05209 
05210   return best_size;
05211 }
05212 
05213 /* Set up the sizes and contents of the ELF dynamic sections.  This is
05214    called by the ELF linker emulation before_allocation routine.  We
05215    must set the sizes of the sections before the linker sets the
05216    addresses of the various sections.  */
05217 
05218 bfd_boolean
05219 bfd_elf_size_dynamic_sections (bfd *output_bfd,
05220                             const char *soname,
05221                             const char *rpath,
05222                             const char *filter_shlib,
05223                             const char * const *auxiliary_filters,
05224                             struct bfd_link_info *info,
05225                             asection **sinterpptr,
05226                             struct bfd_elf_version_tree *verdefs)
05227 {
05228   bfd_size_type soname_indx;
05229   bfd *dynobj;
05230   const struct elf_backend_data *bed;
05231   struct elf_assign_sym_version_info asvinfo;
05232 
05233   *sinterpptr = NULL;
05234 
05235   soname_indx = (bfd_size_type) -1;
05236 
05237   if (!is_elf_hash_table (info->hash))
05238     return TRUE;
05239 
05240   bed = get_elf_backend_data (output_bfd);
05241   elf_tdata (output_bfd)->relro = info->relro;
05242   if (info->execstack)
05243     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
05244   else if (info->noexecstack)
05245     elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
05246   else
05247     {
05248       bfd *inputobj;
05249       asection *notesec = NULL;
05250       int exec = 0;
05251 
05252       for (inputobj = info->input_bfds;
05253           inputobj;
05254           inputobj = inputobj->link_next)
05255        {
05256          asection *s;
05257 
05258          if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
05259            continue;
05260          s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
05261          if (s)
05262            {
05263              if (s->flags & SEC_CODE)
05264               exec = PF_X;
05265              notesec = s;
05266            }
05267          else if (bed->default_execstack)
05268            exec = PF_X;
05269        }
05270       if (notesec)
05271        {
05272          elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
05273          if (exec && info->relocatable
05274              && notesec->output_section != bfd_abs_section_ptr)
05275            notesec->output_section->flags |= SEC_CODE;
05276        }
05277     }
05278 
05279   /* Any syms created from now on start with -1 in
05280      got.refcount/offset and plt.refcount/offset.  */
05281   elf_hash_table (info)->init_got_refcount
05282     = elf_hash_table (info)->init_got_offset;
05283   elf_hash_table (info)->init_plt_refcount
05284     = elf_hash_table (info)->init_plt_offset;
05285 
05286   /* The backend may have to create some sections regardless of whether
05287      we're dynamic or not.  */
05288   if (bed->elf_backend_always_size_sections
05289       && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
05290     return FALSE;
05291 
05292   if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
05293     return FALSE;
05294 
05295   dynobj = elf_hash_table (info)->dynobj;
05296 
05297   /* If there were no dynamic objects in the link, there is nothing to
05298      do here.  */
05299   if (dynobj == NULL)
05300     return TRUE;
05301 
05302   if (elf_hash_table (info)->dynamic_sections_created)
05303     {
05304       struct elf_info_failed eif;
05305       struct elf_link_hash_entry *h;
05306       asection *dynstr;
05307       struct bfd_elf_version_tree *t;
05308       struct bfd_elf_version_expr *d;
05309       asection *s;
05310       bfd_boolean all_defined;
05311 
05312       *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
05313       BFD_ASSERT (*sinterpptr != NULL || !info->executable);
05314 
05315       if (soname != NULL)
05316        {
05317          soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
05318                                         soname, TRUE);
05319          if (soname_indx == (bfd_size_type) -1
05320              || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
05321            return FALSE;
05322        }
05323 
05324       if (info->symbolic)
05325        {
05326          if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
05327            return FALSE;
05328          info->flags |= DF_SYMBOLIC;
05329        }
05330 
05331       if (rpath != NULL)
05332        {
05333          bfd_size_type indx;
05334 
05335          indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
05336                                   TRUE);
05337          if (indx == (bfd_size_type) -1
05338              || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
05339            return FALSE;
05340 
05341          if  (info->new_dtags)
05342            {
05343              _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
05344              if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
05345               return FALSE;
05346            }
05347        }
05348 
05349       if (filter_shlib != NULL)
05350        {
05351          bfd_size_type indx;
05352 
05353          indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
05354                                   filter_shlib, TRUE);
05355          if (indx == (bfd_size_type) -1
05356              || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
05357            return FALSE;
05358        }
05359 
05360       if (auxiliary_filters != NULL)
05361        {
05362          const char * const *p;
05363 
05364          for (p = auxiliary_filters; *p != NULL; p++)
05365            {
05366              bfd_size_type indx;
05367 
05368              indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
05369                                      *p, TRUE);
05370              if (indx == (bfd_size_type) -1
05371                 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
05372               return FALSE;
05373            }
05374        }
05375 
05376       eif.info = info;
05377       eif.verdefs = verdefs;
05378       eif.failed = FALSE;
05379 
05380       /* If we are supposed to export all symbols into the dynamic symbol
05381         table (this is not the normal case), then do so.  */
05382       if (info->export_dynamic
05383          || (info->executable && info->dynamic))
05384        {
05385          elf_link_hash_traverse (elf_hash_table (info),
05386                               _bfd_elf_export_symbol,
05387                               &eif);
05388          if (eif.failed)
05389            return FALSE;
05390        }
05391 
05392       /* Make all global versions with definition.  */
05393       for (t = verdefs; t != NULL; t = t->next)
05394        for (d = t->globals.list; d != NULL; d = d->next)
05395          if (!d->symver && d->symbol)
05396            {
05397              const char *verstr, *name;
05398              size_t namelen, verlen, newlen;
05399              char *newname, *p;
05400              struct elf_link_hash_entry *newh;
05401 
05402              name = d->symbol;
05403              namelen = strlen (name);
05404              verstr = t->name;
05405              verlen = strlen (verstr);
05406              newlen = namelen + verlen + 3;
05407 
05408              newname = bfd_malloc (newlen);
05409              if (newname == NULL)
05410               return FALSE;
05411              memcpy (newname, name, namelen);
05412 
05413              /* Check the hidden versioned definition.  */
05414              p = newname + namelen;
05415              *p++ = ELF_VER_CHR;
05416              memcpy (p, verstr, verlen + 1);
05417              newh = elf_link_hash_lookup (elf_hash_table (info),
05418                                       newname, FALSE, FALSE,
05419                                       FALSE);
05420              if (newh == NULL
05421                 || (newh->root.type != bfd_link_hash_defined
05422                     && newh->root.type != bfd_link_hash_defweak))
05423               {
05424                 /* Check the default versioned definition.  */
05425                 *p++ = ELF_VER_CHR;
05426                 memcpy (p, verstr, verlen + 1);
05427                 newh = elf_link_hash_lookup (elf_hash_table (info),
05428                                           newname, FALSE, FALSE,
05429                                           FALSE);
05430               }
05431              free (newname);
05432 
05433              /* Mark this version if there is a definition and it is
05434                not defined in a shared object.  */
05435              if (newh != NULL
05436                 && !newh->def_dynamic
05437                 && (newh->root.type == bfd_link_hash_defined
05438                     || newh->root.type == bfd_link_hash_defweak))
05439               d->symver = 1;
05440            }
05441 
05442       /* Attach all the symbols to their version information.  */
05443       asvinfo.output_bfd = output_bfd;
05444       asvinfo.info = info;
05445       asvinfo.verdefs = verdefs;
05446       asvinfo.failed = FALSE;
05447 
05448       elf_link_hash_traverse (elf_hash_table (info),
05449                            _bfd_elf_link_assign_sym_version,
05450                            &asvinfo);
05451       if (asvinfo.failed)
05452        return FALSE;
05453 
05454       if (!info->allow_undefined_version)
05455        {
05456          /* Check if all global versions have a definition.  */
05457          all_defined = TRUE;
05458          for (t = verdefs; t != NULL; t = t->next)
05459            for (d = t->globals.list; d != NULL; d = d->next)
05460              if (!d->symver && !d->script)
05461               {
05462                 (*_bfd_error_handler)
05463                   (_("%s: undefined version: %s"),
05464                    d->pattern, t->name);
05465                 all_defined = FALSE;
05466               }
05467 
05468          if (!all_defined)
05469            {
05470              bfd_set_error (bfd_error_bad_value);
05471              return FALSE;
05472            }
05473        }
05474 
05475       /* Find all symbols which were defined in a dynamic object and make
05476         the backend pick a reasonable value for them.  */
05477       elf_link_hash_traverse (elf_hash_table (info),
05478                            _bfd_elf_adjust_dynamic_symbol,
05479                            &eif);
05480       if (eif.failed)
05481        return FALSE;
05482 
05483       /* Add some entries to the .dynamic section.  We fill in some of the
05484         values later, in bfd_elf_final_link, but we must add the entries
05485         now so that we know the final size of the .dynamic section.  */
05486 
05487       /* If there are initialization and/or finalization functions to
05488         call then add the corresponding DT_INIT/DT_FINI entries.  */
05489       h = (info->init_function
05490           ? elf_link_hash_lookup (elf_hash_table (info),
05491                                info->init_function, FALSE,
05492                                FALSE, FALSE)
05493           : NULL);
05494       if (h != NULL
05495          && (h->ref_regular
05496              || h->def_regular))
05497        {
05498          if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
05499            return FALSE;
05500        }
05501       h = (info->fini_function
05502           ? elf_link_hash_lookup (elf_hash_table (info),
05503                                info->fini_function, FALSE,
05504                                FALSE, FALSE)
05505           : NULL);
05506       if (h != NULL
05507          && (h->ref_regular
05508              || h->def_regular))
05509        {
05510          if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
05511            return FALSE;
05512        }
05513 
05514       s = bfd_get_section_by_name (output_bfd, ".preinit_array");
05515       if (s != NULL && s->linker_has_input)
05516        {
05517          /* DT_PREINIT_ARRAY is not allowed in shared library.  */
05518          if (! info->executable)
05519            {
05520              bfd *sub;
05521              asection *o;
05522 
05523              for (sub = info->input_bfds; sub != NULL;
05524                  sub = sub->link_next)
05525               for (o = sub->sections; o != NULL; o = o->next)
05526                 if (elf_section_data (o)->this_hdr.sh_type
05527                     == SHT_PREINIT_ARRAY)
05528                   {
05529                     (*_bfd_error_handler)
05530                      (_("%B: .preinit_array section is not allowed in DSO"),
05531                       sub);
05532                     break;
05533                   }
05534 
05535              bfd_set_error (bfd_error_nonrepresentable_section);
05536              return FALSE;
05537            }
05538 
05539          if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
05540              || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
05541            return FALSE;
05542        }
05543       s = bfd_get_section_by_name (output_bfd, ".init_array");
05544       if (s != NULL && s->linker_has_input)
05545        {
05546          if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
05547              || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
05548            return FALSE;
05549        }
05550       s = bfd_get_section_by_name (output_bfd, ".fini_array");
05551       if (s != NULL && s->linker_has_input)
05552        {
05553          if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
05554              || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
05555            return FALSE;
05556        }
05557 
05558       dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
05559       /* If .dynstr is excluded from the link, we don't want any of
05560         these tags.  Strictly, we should be checking each section
05561         individually;  This quick check covers for the case where
05562         someone does a /DISCARD/ : { *(*) }.  */
05563       if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
05564        {
05565          bfd_size_type strsize;
05566 
05567          strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
05568          if ((info->emit_hash
05569               && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
05570              || (info->emit_gnu_hash
05571                 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
05572              || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
05573              || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
05574              || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
05575              || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
05576                                          bed->s->sizeof_sym))
05577            return FALSE;
05578        }
05579     }
05580 
05581   /* The backend must work out the sizes of all the other dynamic
05582      sections.  */
05583   if (bed->elf_backend_size_dynamic_sections
05584       && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
05585     return FALSE;
05586 
05587   if (elf_hash_table (info)->dynamic_sections_created)
05588     {
05589       unsigned long section_sym_count;
05590       asection *s;
05591 
05592       /* Set up the version definition section.  */
05593       s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
05594       BFD_ASSERT (s != NULL);
05595 
05596       /* We may have created additional version definitions if we are
05597         just linking a regular application.  */
05598       verdefs = asvinfo.verdefs;
05599 
05600       /* Skip anonymous version tag.  */
05601       if (verdefs != NULL && verdefs->vernum == 0)
05602        verdefs = verdefs->next;
05603 
05604       if (verdefs == NULL && !info->create_default_symver)
05605        s->flags |= SEC_EXCLUDE;
05606       else
05607        {
05608          unsigned int cdefs;
05609          bfd_size_type size;
05610          struct bfd_elf_version_tree *t;
05611          bfd_byte *p;
05612          Elf_Internal_Verdef def;
05613          Elf_Internal_Verdaux defaux;
05614          struct bfd_link_hash_entry *bh;
05615          struct elf_link_hash_entry *h;
05616          const char *name;
05617 
05618          cdefs = 0;
05619          size = 0;
05620 
05621          /* Make space for the base version.  */
05622          size += sizeof (Elf_External_Verdef);
05623          size += sizeof (Elf_External_Verdaux);
05624          ++cdefs;
05625 
05626          /* Make space for the default version.  */
05627          if (info->create_default_symver)
05628            {
05629              size += sizeof (Elf_External_Verdef);
05630              ++cdefs;
05631            }
05632 
05633          for (t = verdefs; t != NULL; t = t->next)
05634            {
05635              struct bfd_elf_version_deps *n;
05636 
05637              size += sizeof (Elf_External_Verdef);
05638              size += sizeof (Elf_External_Verdaux);
05639              ++cdefs;
05640 
05641              for (n = t->deps; n != NULL; n = n->next)
05642               size += sizeof (Elf_External_Verdaux);
05643            }
05644 
05645          s->size = size;
05646          s->contents = bfd_alloc (output_bfd, s->size);
05647          if (s->contents == NULL && s->size != 0)
05648            return FALSE;
05649 
05650          /* Fill in the version definition section.  */
05651 
05652          p = s->contents;
05653 
05654          def.vd_version = VER_DEF_CURRENT;
05655          def.vd_flags = VER_FLG_BASE;
05656          def.vd_ndx = 1;
05657          def.vd_cnt = 1;
05658          if (info->create_default_symver)
05659            {
05660              def.vd_aux = 2 * sizeof (Elf_External_Verdef);
05661              def.vd_next = sizeof (Elf_External_Verdef);
05662            }
05663          else
05664            {
05665              def.vd_aux = sizeof (Elf_External_Verdef);
05666              def.vd_next = (sizeof (Elf_External_Verdef)
05667                           + sizeof (Elf_External_Verdaux));
05668            }
05669 
05670          if (soname_indx != (bfd_size_type) -1)
05671            {
05672              _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
05673                                   soname_indx);
05674              def.vd_hash = bfd_elf_hash (soname);
05675              defaux.vda_name = soname_indx;
05676              name = soname;
05677            }
05678          else
05679            {
05680              bfd_size_type indx;
05681 
05682              name = lbasename (output_bfd->filename);
05683              def.vd_hash = bfd_elf_hash (name);
05684              indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
05685                                      name, FALSE);
05686              if (indx == (bfd_size_type) -1)
05687               return FALSE;
05688              defaux.vda_name = indx;
05689            }
05690          defaux.vda_next = 0;
05691 
05692          _bfd_elf_swap_verdef_out (output_bfd, &def,
05693                                 (Elf_External_Verdef *) p);
05694          p += sizeof (Elf_External_Verdef);
05695          if (info->create_default_symver)
05696            {
05697              /* Add a symbol representing this version.  */
05698              bh = NULL;
05699              if (! (_bfd_generic_link_add_one_symbol
05700                    (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
05701                     0, NULL, FALSE,
05702                     get_elf_backend_data (dynobj)->collect, &bh)))
05703               return FALSE;
05704              h = (struct elf_link_hash_entry *) bh;
05705              h->non_elf = 0;
05706              h->def_regular = 1;
05707              h->type = STT_OBJECT;
05708              h->verinfo.vertree = NULL;
05709 
05710              if (! bfd_elf_link_record_dynamic_symbol (info, h))
05711               return FALSE;
05712 
05713              /* Create a duplicate of the base version with the same
05714                aux block, but different flags.  */
05715              def.vd_flags = 0;
05716              def.vd_ndx = 2;
05717              def.vd_aux = sizeof (Elf_External_Verdef);
05718              if (verdefs)
05719               def.vd_next = (sizeof (Elf_External_Verdef)
05720                             + sizeof (Elf_External_Verdaux));
05721              else
05722               def.vd_next = 0;
05723              _bfd_elf_swap_verdef_out (output_bfd, &def,
05724                                    (Elf_External_Verdef *) p);
05725              p += sizeof (Elf_External_Verdef);
05726            }
05727          _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
05728                                  (Elf_External_Verdaux *) p);
05729          p += sizeof (Elf_External_Verdaux);
05730 
05731          for (t = verdefs; t != NULL; t = t->next)
05732            {
05733              unsigned int cdeps;
05734              struct bfd_elf_version_deps *n;
05735 
05736              cdeps = 0;
05737              for (n = t->deps; n != NULL; n = n->next)
05738               ++cdeps;
05739 
05740              /* Add a symbol representing this version.  */
05741              bh = NULL;
05742              if (! (_bfd_generic_link_add_one_symbol
05743                    (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
05744                     0, NULL, FALSE,
05745                     get_elf_backend_data (dynobj)->collect, &bh)))
05746               return FALSE;
05747              h = (struct elf_link_hash_entry *) bh;
05748              h->non_elf = 0;
05749              h->def_regular = 1;
05750              h->type = STT_OBJECT;
05751              h->verinfo.vertree = t;
05752 
05753              if (! bfd_elf_link_record_dynamic_symbol (info, h))
05754               return FALSE;
05755 
05756              def.vd_version = VER_DEF_CURRENT;
05757              def.vd_flags = 0;
05758              if (t->globals.list == NULL
05759                 && t->locals.list == NULL
05760                 && ! t->used)
05761               def.vd_flags |= VER_FLG_WEAK;
05762              def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
05763              def.vd_cnt = cdeps + 1;
05764              def.vd_hash = bfd_elf_hash (t->name);
05765              def.vd_aux = sizeof (Elf_External_Verdef);
05766              def.vd_next = 0;
05767              if (t->next != NULL)
05768               def.vd_next = (sizeof (Elf_External_Verdef)
05769                             + (cdeps + 1) * sizeof (Elf_External_Verdaux));
05770 
05771              _bfd_elf_swap_verdef_out (output_bfd, &def,
05772                                    (Elf_External_Verdef *) p);
05773              p += sizeof (Elf_External_Verdef);
05774 
05775              defaux.vda_name = h->dynstr_index;
05776              _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
05777                                   h->dynstr_index);
05778              defaux.vda_next = 0;
05779              if (t->deps != NULL)
05780               defaux.vda_next = sizeof (Elf_External_Verdaux);
05781              t->name_indx = defaux.vda_name;
05782 
05783              _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
05784                                     (Elf_External_Verdaux *) p);
05785              p += sizeof (Elf_External_Verdaux);
05786 
05787              for (n = t->deps; n != NULL; n = n->next)
05788               {
05789                 if (n->version_needed == NULL)
05790                   {
05791                     /* This can happen if there was an error in the
05792                       version script.  */
05793                     defaux.vda_name = 0;
05794                   }
05795                 else
05796                   {
05797                     defaux.vda_name = n->version_needed->name_indx;
05798                     _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
05799                                          defaux.vda_name);
05800                   }
05801                 if (n->next == NULL)
05802                   defaux.vda_next = 0;
05803                 else
05804                   defaux.vda_next = sizeof (Elf_External_Verdaux);
05805 
05806                 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
05807                                         (Elf_External_Verdaux *) p);
05808                 p += sizeof (Elf_External_Verdaux);
05809               }
05810            }
05811 
05812          if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
05813              || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
05814            return FALSE;
05815 
05816          elf_tdata (output_bfd)->cverdefs = cdefs;
05817        }
05818 
05819       if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
05820        {
05821          if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
05822            return FALSE;
05823        }
05824       else if (info->flags & DF_BIND_NOW)
05825        {
05826          if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
05827            return FALSE;
05828        }
05829 
05830       if (info->flags_1)
05831        {
05832          if (info->executable)
05833            info->flags_1 &= ~ (DF_1_INITFIRST
05834                             | DF_1_NODELETE
05835                             | DF_1_NOOPEN);
05836          if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
05837            return FALSE;
05838        }
05839 
05840       /* Work out the size of the version reference section.  */
05841 
05842       s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
05843       BFD_ASSERT (s != NULL);
05844       {
05845        struct elf_find_verdep_info sinfo;
05846 
05847        sinfo.output_bfd = output_bfd;
05848        sinfo.info = info;
05849        sinfo.vers = elf_tdata (output_bfd)->cverdefs;
05850        if (sinfo.vers == 0)
05851          sinfo.vers = 1;
05852        sinfo.failed = FALSE;
05853 
05854        elf_link_hash_traverse (elf_hash_table (info),
05855                             _bfd_elf_link_find_version_dependencies,
05856                             &sinfo);
05857 
05858        if (elf_tdata (output_bfd)->verref == NULL)
05859          s->flags |= SEC_EXCLUDE;
05860        else
05861          {
05862            Elf_Internal_Verneed *t;
05863            unsigned int size;
05864            unsigned int crefs;
05865            bfd_byte *p;
05866 
05867            /* Build the version definition section.  */
05868            size = 0;
05869            crefs = 0;
05870            for (t = elf_tdata (output_bfd)->verref;
05871                t != NULL;
05872                t = t->vn_nextref)
05873              {
05874               Elf_Internal_Vernaux *a;
05875 
05876               size += sizeof (Elf_External_Verneed);
05877               ++crefs;
05878               for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
05879                 size += sizeof (Elf_External_Vernaux);
05880              }
05881 
05882            s->size = size;
05883            s->contents = bfd_alloc (output_bfd, s->size);
05884            if (s->contents == NULL)
05885              return FALSE;
05886 
05887            p = s->contents;
05888            for (t = elf_tdata (output_bfd)->verref;
05889                t != NULL;
05890                t = t->vn_nextref)
05891              {
05892               unsigned int caux;
05893               Elf_Internal_Vernaux *a;
05894               bfd_size_type indx;
05895 
05896               caux = 0;
05897               for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
05898                 ++caux;
05899 
05900               t->vn_version = VER_NEED_CURRENT;
05901               t->vn_cnt = caux;
05902               indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
05903                                        elf_dt_name (t->vn_bfd) != NULL
05904                                        ? elf_dt_name (t->vn_bfd)
05905                                        : lbasename (t->vn_bfd->filename),
05906                                        FALSE);
05907               if (indx == (bfd_size_type) -1)
05908                 return FALSE;
05909               t->vn_file = indx;
05910               t->vn_aux = sizeof (Elf_External_Verneed);
05911               if (t->vn_nextref == NULL)
05912                 t->vn_next = 0;
05913               else
05914                 t->vn_next = (sizeof (Elf_External_Verneed)
05915                             + caux * sizeof (Elf_External_Vernaux));
05916 
05917               _bfd_elf_swap_verneed_out (output_bfd, t,
05918                                       (Elf_External_Verneed *) p);
05919               p += sizeof (Elf_External_Verneed);
05920 
05921               for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
05922                 {
05923                   a->vna_hash = bfd_elf_hash (a->vna_nodename);
05924                   indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
05925                                           a->vna_nodename, FALSE);
05926                   if (indx == (bfd_size_type) -1)
05927                     return FALSE;
05928                   a->vna_name = indx;
05929                   if (a->vna_nextptr == NULL)
05930                     a->vna_next = 0;
05931                   else
05932                     a->vna_next = sizeof (Elf_External_Vernaux);
05933 
05934                   _bfd_elf_swap_vernaux_out (output_bfd, a,
05935                                           (Elf_External_Vernaux *) p);
05936                   p += sizeof (Elf_External_Vernaux);
05937                 }
05938              }
05939 
05940            if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
05941               || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
05942              return FALSE;
05943 
05944            elf_tdata (output_bfd)->cverrefs = crefs;
05945          }
05946       }
05947 
05948       if ((elf_tdata (output_bfd)->cverrefs == 0
05949           && elf_tdata (output_bfd)->cverdefs == 0)
05950          || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
05951                                         &section_sym_count) == 0)
05952        {
05953          s = bfd_get_section_by_name (dynobj, ".gnu.version");
05954          s->flags |= SEC_EXCLUDE;
05955        }
05956     }
05957   return TRUE;
05958 }
05959 
05960 /* Find the first non-excluded output section.  We'll use its
05961    section symbol for some emitted relocs.  */
05962 void
05963 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
05964 {
05965   asection *s;
05966 
05967   for (s = output_bfd->sections; s != NULL; s = s->next)
05968     if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
05969        && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
05970       {
05971        elf_hash_table (info)->text_index_section = s;
05972        break;
05973       }
05974 }
05975 
05976 /* Find two non-excluded output sections, one for code, one for data.
05977    We'll use their section symbols for some emitted relocs.  */
05978 void
05979 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
05980 {
05981   asection *s;
05982 
05983   for (s = output_bfd->sections; s != NULL; s = s->next)
05984     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
05985         == (SEC_ALLOC | SEC_READONLY))
05986        && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
05987       {
05988        elf_hash_table (info)->text_index_section = s;
05989        break;
05990       }
05991 
05992   for (s = output_bfd->sections; s != NULL; s = s->next)
05993     if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
05994        && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
05995       {
05996        elf_hash_table (info)->data_index_section = s;
05997        break;
05998       }
05999 
06000   if (elf_hash_table (info)->text_index_section == NULL)
06001     elf_hash_table (info)->text_index_section
06002       = elf_hash_table (info)->data_index_section;
06003 }
06004 
06005 bfd_boolean
06006 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
06007 {
06008   const struct elf_backend_data *bed;
06009 
06010   if (!is_elf_hash_table (info->hash))
06011     return TRUE;
06012 
06013   bed = get_elf_backend_data (output_bfd);
06014   (*bed->elf_backend_init_index_section) (output_bfd, info);
06015 
06016   if (elf_hash_table (info)->dynamic_sections_created)
06017     {
06018       bfd *dynobj;
06019       asection *s;
06020       bfd_size_type dynsymcount;
06021       unsigned long section_sym_count;
06022       unsigned int dtagcount;
06023 
06024       dynobj = elf_hash_table (info)->dynobj;
06025 
06026       /* Assign dynsym indicies.  In a shared library we generate a
06027         section symbol for each output section, which come first.
06028         Next come all of the back-end allocated local dynamic syms,
06029         followed by the rest of the global symbols.  */
06030 
06031       dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
06032                                               &section_sym_count);
06033 
06034       /* Work out the size of the symbol version section.  */
06035       s = bfd_get_section_by_name (dynobj, ".gnu.version");
06036       BFD_ASSERT (s != NULL);
06037       if (dynsymcount != 0
06038          && (s->flags & SEC_EXCLUDE) == 0)
06039        {
06040          s->size = dynsymcount * sizeof (Elf_External_Versym);
06041          s->contents = bfd_zalloc (output_bfd, s->size);
06042          if (s->contents == NULL)
06043            return FALSE;
06044 
06045          if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
06046            return FALSE;
06047        }
06048 
06049       /* Set the size of the .dynsym and .hash sections.  We counted
06050         the number of dynamic symbols in elf_link_add_object_symbols.
06051         We will build the contents of .dynsym and .hash when we build
06052         the final symbol table, because until then we do not know the
06053         correct value to give the symbols.  We built the .dynstr
06054         section as we went along in elf_link_add_object_symbols.  */
06055       s = bfd_get_section_by_name (dynobj, ".dynsym");
06056       BFD_ASSERT (s != NULL);
06057       s->size = dynsymcount * bed->s->sizeof_sym;
06058 
06059       if (dynsymcount != 0)
06060        {
06061          s->contents = bfd_alloc (output_bfd, s->size);
06062          if (s->contents == NULL)
06063            return FALSE;
06064 
06065          /* The first entry in .dynsym is a dummy symbol.
06066             Clear all the section syms, in case we don't output them all.  */
06067          ++section_sym_count;
06068          memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
06069        }
06070 
06071       elf_hash_table (info)->bucketcount = 0;
06072 
06073       /* Compute the size of the hashing table.  As a side effect this
06074         computes the hash values for all the names we export.  */
06075       if (info->emit_hash)
06076        {
06077          unsigned long int *hashcodes;
06078          unsigned long int *hashcodesp;
06079          bfd_size_type amt;
06080          unsigned long int nsyms;
06081          size_t bucketcount;
06082          size_t hash_entry_size;
06083 
06084          /* Compute the hash values for all exported symbols.  At the same
06085             time store the values in an array so that we could use them for
06086             optimizations.  */
06087          amt = dynsymcount * sizeof (unsigned long int);
06088          hashcodes = bfd_malloc (amt);
06089          if (hashcodes == NULL)
06090            return FALSE;
06091          hashcodesp = hashcodes;
06092 
06093          /* Put all hash values in HASHCODES.  */
06094          elf_link_hash_traverse (elf_hash_table (info),
06095                               elf_collect_hash_codes, &hashcodesp);
06096 
06097          nsyms = hashcodesp - hashcodes;
06098          bucketcount
06099            = compute_bucket_count (info, hashcodes, nsyms, 0);
06100          free (hashcodes);
06101 
06102          if (bucketcount == 0)
06103            return FALSE;
06104 
06105          elf_hash_table (info)->bucketcount = bucketcount;
06106 
06107          s = bfd_get_section_by_name (dynobj, ".hash");
06108          BFD_ASSERT (s != NULL);
06109          hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
06110          s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
06111          s->contents = bfd_zalloc (output_bfd, s->size);
06112          if (s->contents == NULL)
06113            return FALSE;
06114 
06115          bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
06116          bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
06117                  s->contents + hash_entry_size);
06118        }
06119 
06120       if (info->emit_gnu_hash)
06121        {
06122          size_t i, cnt;
06123          unsigned char *contents;
06124          struct collect_gnu_hash_codes cinfo;
06125          bfd_size_type amt;
06126          size_t bucketcount;
06127 
06128          memset (&cinfo, 0, sizeof (cinfo));
06129 
06130          /* Compute the hash values for all exported symbols.  At the same
06131             time store the values in an array so that we could use them for
06132             optimizations.  */
06133          amt = dynsymcount * 2 * sizeof (unsigned long int);
06134          cinfo.hashcodes = bfd_malloc (amt);
06135          if (cinfo.hashcodes == NULL)
06136            return FALSE;
06137 
06138          cinfo.hashval = cinfo.hashcodes + dynsymcount;
06139          cinfo.min_dynindx = -1;
06140          cinfo.output_bfd = output_bfd;
06141          cinfo.bed = bed;
06142 
06143          /* Put all hash values in HASHCODES.  */
06144          elf_link_hash_traverse (elf_hash_table (info),
06145                               elf_collect_gnu_hash_codes, &cinfo);
06146 
06147          bucketcount
06148            = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
06149 
06150          if (bucketcount == 0)
06151            {
06152              free (cinfo.hashcodes);
06153              return FALSE;
06154            }
06155 
06156          s = bfd_get_section_by_name (dynobj, ".gnu.hash");
06157          BFD_ASSERT (s != NULL);
06158 
06159          if (cinfo.nsyms == 0)
06160            {
06161              /* Empty .gnu.hash section is special.  */
06162              BFD_ASSERT (cinfo.min_dynindx == -1);
06163              free (cinfo.hashcodes);
06164              s->size = 5 * 4 + bed->s->arch_size / 8;
06165              contents = bfd_zalloc (output_bfd, s->size);
06166              if (contents == NULL)
06167               return FALSE;
06168              s->contents = contents;
06169              /* 1 empty bucket.  */
06170              bfd_put_32 (output_bfd, 1, contents);
06171              /* SYMIDX above the special symbol 0.  */
06172              bfd_put_32 (output_bfd, 1, contents + 4);
06173              /* Just one word for bitmask.  */
06174              bfd_put_32 (output_bfd, 1, contents + 8);
06175              /* Only hash fn bloom filter.  */
06176              bfd_put_32 (output_bfd, 0, contents + 12);
06177              /* No hashes are valid - empty bitmask.  */
06178              bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
06179              /* No hashes in the only bucket.  */
06180              bfd_put_32 (output_bfd, 0,
06181                        contents + 16 + bed->s->arch_size / 8);
06182            }
06183          else
06184            {
06185              unsigned long int maskwords, maskbitslog2;
06186              BFD_ASSERT (cinfo.min_dynindx != -1);
06187 
06188              maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
06189              if (maskbitslog2 < 3)
06190               maskbitslog2 = 5;
06191              else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
06192               maskbitslog2 = maskbitslog2 + 3;
06193              else
06194               maskbitslog2 = maskbitslog2 + 2;
06195              if (bed->s->arch_size == 64)
06196               {
06197                 if (maskbitslog2 == 5)
06198                   maskbitslog2 = 6;
06199                 cinfo.shift1 = 6;
06200               }
06201              else
06202               cinfo.shift1 = 5;
06203              cinfo.mask = (1 << cinfo.shift1) - 1;
06204              cinfo.shift2 = maskbitslog2;
06205              cinfo.maskbits = 1 << maskbitslog2;
06206              maskwords = 1 << (maskbitslog2 - cinfo.shift1);
06207              amt = bucketcount * sizeof (unsigned long int) * 2;
06208              amt += maskwords * sizeof (bfd_vma);
06209              cinfo.bitmask = bfd_malloc (amt);
06210              if (cinfo.bitmask == NULL)
06211               {
06212                 free (cinfo.hashcodes);
06213                 return FALSE;
06214               }
06215 
06216              cinfo.counts = (void *) (cinfo.bitmask + maskwords);
06217              cinfo.indx = cinfo.counts + bucketcount;
06218              cinfo.symindx = dynsymcount - cinfo.nsyms;
06219              memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
06220 
06221              /* Determine how often each hash bucket is used.  */
06222              memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
06223              for (i = 0; i < cinfo.nsyms; ++i)
06224               ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
06225 
06226              for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
06227               if (cinfo.counts[i] != 0)
06228                 {
06229                   cinfo.indx[i] = cnt;
06230                   cnt += cinfo.counts[i];
06231                 }
06232              BFD_ASSERT (cnt == dynsymcount);
06233              cinfo.bucketcount = bucketcount;
06234              cinfo.local_indx = cinfo.min_dynindx;
06235 
06236              s->size = (4 + bucketcount + cinfo.nsyms) * 4;
06237              s->size += cinfo.maskbits / 8;
06238              contents = bfd_zalloc (output_bfd, s->size);
06239              if (contents == NULL)
06240               {
06241                 free (cinfo.bitmask);
06242                 free (cinfo.hashcodes);
06243                 return FALSE;
06244               }
06245 
06246              s->contents = contents;
06247              bfd_put_32 (output_bfd, bucketcount, contents);
06248              bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
06249              bfd_put_32 (output_bfd, maskwords, contents + 8);
06250              bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
06251              contents += 16 + cinfo.maskbits / 8;
06252 
06253              for (i = 0; i < bucketcount; ++i)
06254               {
06255                 if (cinfo.counts[i] == 0)
06256                   bfd_put_32 (output_bfd, 0, contents);
06257                 else
06258                   bfd_put_32 (output_bfd, cinfo.indx[i], contents);
06259                 contents += 4;
06260               }
06261 
06262              cinfo.contents = contents;
06263 
06264              /* Renumber dynamic symbols, populate .gnu.hash section.  */
06265              elf_link_hash_traverse (elf_hash_table (info),
06266                                   elf_renumber_gnu_hash_syms, &cinfo);
06267 
06268              contents = s->contents + 16;
06269              for (i = 0; i < maskwords; ++i)
06270               {
06271                 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
06272                         contents);
06273                 contents += bed->s->arch_size / 8;
06274               }
06275 
06276              free (cinfo.bitmask);
06277              free (cinfo.hashcodes);
06278            }
06279        }
06280 
06281       s = bfd_get_section_by_name (dynobj, ".dynstr");
06282       BFD_ASSERT (s != NULL);
06283 
06284       elf_finalize_dynstr (output_bfd, info);
06285 
06286       s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
06287 
06288       for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
06289        if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
06290          return FALSE;
06291     }
06292 
06293   return TRUE;
06294 }
06295 
06296 /* Final phase of ELF linker.  */
06297 
06298 /* A structure we use to avoid passing large numbers of arguments.  */
06299 
06300 struct elf_final_link_info
06301 {
06302   /* General link information.  */
06303   struct bfd_link_info *info;
06304   /* Output BFD.  */
06305   bfd *output_bfd;
06306   /* Symbol string table.  */
06307   struct bfd_strtab_hash *symstrtab;
06308   /* .dynsym section.  */
06309   asection *dynsym_sec;
06310   /* .hash section.  */
06311   asection *hash_sec;
06312   /* symbol version section (.gnu.version).  */
06313   asection *symver_sec;
06314   /* Buffer large enough to hold contents of any section.  */
06315   bfd_byte *contents;
06316   /* Buffer large enough to hold external relocs of any section.  */
06317   void *external_relocs;
06318   /* Buffer large enough to hold internal relocs of any section.  */
06319   Elf_Internal_Rela *internal_relocs;
06320   /* Buffer large enough to hold external local symbols of any input
06321      BFD.  */
06322   bfd_byte *external_syms;
06323   /* And a buffer for symbol section indices.  */
06324   Elf_External_Sym_Shndx *locsym_shndx;
06325   /* Buffer large enough to hold internal local symbols of any input
06326      BFD.  */
06327   Elf_Internal_Sym *internal_syms;
06328   /* Array large enough to hold a symbol index for each local symbol
06329      of any input BFD.  */
06330   long *indices;
06331   /* Array large enough to hold a section pointer for each local
06332      symbol of any input BFD.  */
06333   asection **sections;
06334   /* Buffer to hold swapped out symbols.  */
06335   bfd_byte *symbuf;
06336   /* And one for symbol section indices.  */
06337   Elf_External_Sym_Shndx *symshndxbuf;
06338   /* Number of swapped out symbols in buffer.  */
06339   size_t symbuf_count;
06340   /* Number of symbols which fit in symbuf.  */
06341   size_t symbuf_size;
06342   /* And same for symshndxbuf.  */
06343   size_t shndxbuf_size;
06344 };
06345 
06346 /* This struct is used to pass information to elf_link_output_extsym.  */
06347 
06348 struct elf_outext_info
06349 {
06350   bfd_boolean failed;
06351   bfd_boolean localsyms;
06352   struct elf_final_link_info *finfo;
06353 };
06354 
06355 
06356 /* Support for evaluating a complex relocation.
06357 
06358    Complex relocations are generalized, self-describing relocations.  The
06359    implementation of them consists of two parts: complex symbols, and the
06360    relocations themselves. 
06361 
06362    The relocations are use a reserved elf-wide relocation type code (R_RELC
06363    external / BFD_RELOC_RELC internal) and an encoding of relocation field
06364    information (start bit, end bit, word width, etc) into the addend.  This
06365    information is extracted from CGEN-generated operand tables within gas.
06366 
06367    Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
06368    internal) representing prefix-notation expressions, including but not
06369    limited to those sorts of expressions normally encoded as addends in the
06370    addend field.  The symbol mangling format is:
06371 
06372    <node> := <literal>
06373           |  <unary-operator> ':' <node>
06374           |  <binary-operator> ':' <node> ':' <node>
06375          ;
06376 
06377    <literal> := 's' <digits=N> ':' <N character symbol name>
06378              |  'S' <digits=N> ':' <N character section name>
06379             |  '#' <hexdigits>
06380             ;
06381 
06382    <binary-operator> := as in C
06383    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
06384 
06385 static void
06386 set_symbol_value (bfd *                         bfd_with_globals,
06387                 struct elf_final_link_info *  finfo,    
06388                 int                           symidx,
06389                 bfd_vma                       val)
06390 {
06391   bfd_boolean                    is_local;
06392   Elf_Internal_Sym *             sym;
06393   struct elf_link_hash_entry **  sym_hashes;
06394   struct elf_link_hash_entry *   h;
06395 
06396   sym_hashes = elf_sym_hashes (bfd_with_globals);
06397   sym = finfo->internal_syms + symidx;  
06398   is_local = ELF_ST_BIND(sym->st_info) == STB_LOCAL;
06399   
06400   if (is_local)
06401     {
06402       /* It is a local symbol: move it to the
06403         "absolute" section and give it a value.  */
06404       sym->st_shndx = SHN_ABS;
06405       sym->st_value = val;
06406     }
06407   else 
06408     {
06409       /* It is a global symbol: set its link type
06410         to "defined" and give it a value.  */
06411       h = sym_hashes [symidx];       
06412       while (h->root.type == bfd_link_hash_indirect
06413             || h->root.type == bfd_link_hash_warning)
06414        h = (struct elf_link_hash_entry *) h->root.u.i.link;
06415       h->root.type = bfd_link_hash_defined;
06416       h->root.u.def.value = val;
06417       h->root.u.def.section = bfd_abs_section_ptr;
06418     }
06419 }
06420 
06421 static bfd_boolean 
06422 resolve_symbol (const char *                  name,
06423               bfd *                         input_bfd,
06424               struct elf_final_link_info *  finfo,
06425               bfd_vma *                     result,
06426               size_t                        locsymcount)
06427 {
06428   Elf_Internal_Sym *            sym;
06429   struct bfd_link_hash_entry *  global_entry;
06430   const char *                  candidate = NULL;
06431   Elf_Internal_Shdr *           symtab_hdr;
06432   asection *                    sec = NULL;
06433   size_t                        i;
06434   
06435   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
06436 
06437   for (i = 0; i < locsymcount; ++ i)
06438     {
06439       sym = finfo->internal_syms + i;
06440       sec = finfo->sections [i];
06441 
06442       if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
06443        continue;
06444 
06445       candidate = bfd_elf_string_from_elf_section (input_bfd,
06446                                              symtab_hdr->sh_link,
06447                                              sym->st_name);
06448 #ifdef DEBUG
06449       printf ("Comparing string: '%s' vs. '%s' = 0x%x\n", 
06450              name, candidate, (unsigned int)sym->st_value);
06451 #endif
06452       if (candidate && strcmp (candidate, name) == 0)
06453        {
06454          * result = sym->st_value;
06455 
06456          if (sym->st_shndx > SHN_UNDEF && 
06457              sym->st_shndx < SHN_LORESERVE)
06458            {
06459 #ifdef DEBUG
06460              printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
06461                     sec->output_section->name, 
06462                     (unsigned int)sec->output_section->vma, 
06463                     (unsigned int)sec->output_offset);
06464 #endif
06465              * result += sec->output_offset + sec->output_section->vma;
06466            }
06467 #ifdef DEBUG
06468          printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result);
06469 #endif
06470          return TRUE;
06471        }
06472     }
06473 
06474   /* Hmm, haven't found it yet. perhaps it is a global.  */
06475   global_entry = bfd_link_hash_lookup (finfo->info->hash, name, FALSE, FALSE, TRUE);
06476   if (!global_entry)
06477     return FALSE;
06478   
06479   if (global_entry->type == bfd_link_hash_defined
06480       || global_entry->type == bfd_link_hash_defweak)
06481     {
06482       * result = global_entry->u.def.value 
06483        + global_entry->u.def.section->output_section->vma 
06484        + global_entry->u.def.section->output_offset;
06485 #ifdef DEBUG
06486       printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
06487              global_entry->root.string, (unsigned int)*result);
06488 #endif
06489       return TRUE;
06490     } 
06491 
06492   if (global_entry->type == bfd_link_hash_common)
06493     {
06494       *result = global_entry->u.def.value +
06495        bfd_com_section_ptr->output_section->vma +
06496        bfd_com_section_ptr->output_offset;
06497 #ifdef DEBUG
06498       printf ("Found COMMON symbol '%s' with value %8.8x\n",
06499              global_entry->root.string, (unsigned int)*result);
06500 #endif
06501       return TRUE;
06502     }
06503   
06504   return FALSE;
06505 }
06506 
06507 static bfd_boolean
06508 resolve_section (const char *  name,
06509                asection *    sections,
06510                bfd_vma *     result)
06511 {
06512   asection *    curr;
06513   unsigned int  len;
06514 
06515   for (curr = sections; curr; curr = curr->next)    
06516     if (strcmp (curr->name, name) == 0)
06517       {
06518        *result = curr->vma;
06519        return TRUE;
06520       }
06521 
06522   /* Hmm. still haven't found it. try pseudo-section names.  */
06523   for (curr = sections; curr; curr = curr->next)    
06524     {
06525       len = strlen (curr->name);
06526       if (len > strlen (name)) 
06527        continue;
06528 
06529       if (strncmp (curr->name, name, len) == 0)
06530        {
06531          if (strncmp (".end", name + len, 4) == 0)
06532            {
06533              *result = curr->vma + curr->size;
06534              return TRUE;
06535            }
06536 
06537          /* Insert more pseudo-section names here, if you like.  */
06538        }
06539     }
06540   
06541   return FALSE;
06542 }
06543 
06544 static void
06545 undefined_reference (const char *  reftype,
06546                    const char *  name)
06547 {
06548   _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype, name);
06549 }
06550 
06551 static bfd_boolean
06552 eval_symbol (bfd_vma *                     result,
06553             char *                        sym,
06554             char **                       advanced,
06555             bfd *                         input_bfd,
06556             struct elf_final_link_info *  finfo,
06557             bfd_vma                       addr,
06558             bfd_vma                       section_offset,
06559             size_t                        locsymcount,
06560             int                           signed_p)
06561 {
06562   int           len;
06563   int           symlen;
06564   bfd_vma       a;
06565   bfd_vma       b;
06566   const int     bufsz = 4096;
06567   char          symbuf [bufsz];
06568   const char *  symend;
06569   bfd_boolean   symbol_is_section = FALSE;
06570 
06571   len = strlen (sym);
06572   symend = sym + len;
06573 
06574   if (len < 1 || len > bufsz)
06575     {
06576       bfd_set_error (bfd_error_invalid_operation);
06577       return FALSE;
06578     }
06579   
06580   switch (* sym)
06581     {
06582     case '.':
06583       * result = addr + section_offset;
06584       * advanced = sym + 1;
06585       return TRUE;
06586 
06587     case '#':
06588       ++ sym;
06589       * result = strtoul (sym, advanced, 16);
06590       return TRUE;
06591 
06592     case 'S':
06593       symbol_is_section = TRUE;
06594     case 's':      
06595       ++ sym;
06596       symlen = strtol (sym, &sym, 10);
06597       ++ sym; /* Skip the trailing ':'.  */
06598 
06599       if ((symend < sym) || ((symlen + 1) > bufsz))
06600        {
06601          bfd_set_error (bfd_error_invalid_operation);
06602          return FALSE;
06603        }
06604 
06605       memcpy (symbuf, sym, symlen);
06606       symbuf [symlen] = '\0';
06607       * advanced = sym + symlen;
06608       
06609       /* Is it always possible, with complex symbols, that gas "mis-guessed" 
06610         the symbol as a section, or vice-versa. so we're pretty liberal in our
06611         interpretation here; section means "try section first", not "must be a
06612         section", and likewise with symbol.  */
06613 
06614       if (symbol_is_section) 
06615        {
06616          if ((resolve_section (symbuf, finfo->output_bfd->sections, result) != TRUE)
06617              && (resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE))
06618            {
06619              undefined_reference ("section", symbuf);
06620              return FALSE;
06621            }
06622        } 
06623       else 
06624        {
06625          if ((resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE)
06626              && (resolve_section (symbuf, finfo->output_bfd->sections,
06627                                result) != TRUE))
06628            {
06629              undefined_reference ("symbol", symbuf);
06630              return FALSE;
06631            }
06632        }
06633 
06634       return TRUE;
06635       
06636       /* All that remains are operators.  */
06637 
06638 #define UNARY_OP(op)                                    \
06639   if (strncmp (sym, #op, strlen (#op)) == 0)                   \
06640     {                                                   \
06641       sym += strlen (#op);                              \
06642       if (* sym == ':')                                        \
06643         ++ sym;                                                \
06644       if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
06645                        section_offset, locsymcount, signed_p)   \
06646                                                     != TRUE)   \
06647         return FALSE;                                          \
06648       if (signed_p)                                             \
06649         * result = op ((signed)a);                             \
06650       else                                                      \
06651         * result = op a;                                        \
06652       * advanced = sym;                                 \
06653       return TRUE;                                      \
06654     }
06655 
06656 #define BINARY_OP(op)                                          \
06657   if (strncmp (sym, #op, strlen (#op)) == 0)                   \
06658     {                                                   \
06659       sym += strlen (#op);                              \
06660       if (* sym == ':')                                        \
06661         ++ sym;                                                \
06662       if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
06663                        section_offset, locsymcount, signed_p)   \
06664                                                      != TRUE)  \
06665         return FALSE;                                          \
06666       ++ sym;                                           \
06667       if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
06668                        section_offset, locsymcount, signed_p)   \
06669                                                      != TRUE)  \
06670         return FALSE;                                          \
06671       if (signed_p)                                             \
06672         * result = ((signed) a) op ((signed) b);         \
06673       else                                                      \
06674         * result = a op b;                                      \
06675       * advanced = sym;                                        \
06676       return TRUE;                                      \
06677     }
06678 
06679     default:
06680       UNARY_OP  (0-);
06681       BINARY_OP (<<);
06682       BINARY_OP (>>);
06683       BINARY_OP (==);
06684       BINARY_OP (!=);
06685       BINARY_OP (<=);
06686       BINARY_OP (>=);
06687       BINARY_OP (&&);
06688       BINARY_OP (||);
06689       UNARY_OP  (~);
06690       UNARY_OP  (!);
06691       BINARY_OP (*);
06692       BINARY_OP (/);
06693       BINARY_OP (%);
06694       BINARY_OP (^);
06695       BINARY_OP (|);
06696       BINARY_OP (&);
06697       BINARY_OP (+);
06698       BINARY_OP (-);
06699       BINARY_OP (<);
06700       BINARY_OP (>);
06701 #undef UNARY_OP
06702 #undef BINARY_OP
06703       _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
06704       bfd_set_error (bfd_error_invalid_operation);
06705       return FALSE;
06706     }
06707 }
06708 
06709 /* Entry point to evaluator, called from elf_link_input_bfd.  */
06710 
06711 static bfd_boolean
06712 evaluate_complex_relocation_symbols (bfd * input_bfd,
06713                                  struct elf_final_link_info * finfo,
06714                                  size_t locsymcount)
06715 {
06716   const struct elf_backend_data * bed;
06717   Elf_Internal_Shdr *             symtab_hdr;
06718   struct elf_link_hash_entry **   sym_hashes;
06719   asection *                      reloc_sec;
06720   bfd_boolean                     result = TRUE;
06721 
06722   /* For each section, we're going to check and see if it has any
06723      complex relocations, and we're going to evaluate any of them
06724      we can.  */
06725 
06726   if (finfo->info->relocatable)
06727     return TRUE;
06728 
06729   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
06730   sym_hashes = elf_sym_hashes (input_bfd);
06731   bed = get_elf_backend_data (input_bfd);
06732 
06733   for (reloc_sec = input_bfd->sections; reloc_sec; reloc_sec = reloc_sec->next)
06734     {
06735       Elf_Internal_Rela * internal_relocs;
06736       unsigned long i;
06737 
06738       /* This section was omitted from the link.  */
06739       if (! reloc_sec->linker_mark)
06740        continue;
06741 
06742       /* Only process sections containing relocs.  */
06743       if ((reloc_sec->flags & SEC_RELOC) == 0)
06744        continue;
06745 
06746       if (reloc_sec->reloc_count == 0)
06747        continue;
06748 
06749       /* Read in the relocs for this section.  */
06750       internal_relocs
06751        = _bfd_elf_link_read_relocs (input_bfd, reloc_sec, NULL,
06752                                  (Elf_Internal_Rela *) NULL,
06753                                  FALSE);
06754       if (internal_relocs == NULL)
06755        continue;
06756 
06757       for (i = reloc_sec->reloc_count; i--;)
06758        {
06759          Elf_Internal_Rela * rel;
06760          char * sym_name;
06761          bfd_vma index;
06762          Elf_Internal_Sym * sym;
06763          bfd_vma result;
06764          bfd_vma section_offset;
06765          bfd_vma addr;
06766          int signed_p = 0;
06767 
06768          rel = internal_relocs + i;
06769          section_offset = reloc_sec->output_section->vma
06770            + reloc_sec->output_offset;
06771          addr = rel->r_offset;
06772 
06773          index = ELF32_R_SYM (rel->r_info);
06774          if (bed->s->arch_size == 64)
06775            index >>= 24;
06776 
06777          if (index == STN_UNDEF)
06778            continue;
06779 
06780          if (index < locsymcount)
06781            {
06782              /* The symbol is local.  */
06783              sym = finfo->internal_syms + index;
06784 
06785              /* We're only processing STT_RELC or STT_SRELC type symbols.  */
06786              if ((ELF_ST_TYPE (sym->st_info) != STT_RELC) &&
06787                 (ELF_ST_TYPE (sym->st_info) != STT_SRELC))
06788               continue;
06789 
06790              sym_name = bfd_elf_string_from_elf_section
06791               (input_bfd, symtab_hdr->sh_link, sym->st_name);
06792 
06793              signed_p = (ELF_ST_TYPE (sym->st_info) == STT_SRELC);
06794            }
06795          else
06796            {
06797              /* The symbol is global.  */
06798              struct elf_link_hash_entry * h;
06799 
06800              if (elf_bad_symtab (input_bfd))
06801               continue;
06802 
06803              h = sym_hashes [index - locsymcount];
06804              while (   h->root.type == bfd_link_hash_indirect
06805                    || h->root.type == bfd_link_hash_warning)
06806               h = (struct elf_link_hash_entry *) h->root.u.i.link;
06807 
06808              if (h->type != STT_RELC && h->type != STT_SRELC)
06809               continue;
06810 
06811              signed_p = (h->type == STT_SRELC);
06812              sym_name = (char *) h->root.root.string;
06813            }
06814 #ifdef DEBUG
06815          printf ("Encountered a complex symbol!");
06816          printf (" (input_bfd %s, section %s, reloc %ld\n",
06817                 input_bfd->filename, reloc_sec->name, i);
06818          printf (" symbol: idx  %8.8lx, name %s\n",
06819                 index, sym_name);
06820          printf (" reloc : info %8.8lx, addr %8.8lx\n",
06821                 rel->r_info, addr);
06822          printf (" Evaluating '%s' ...\n ", sym_name);
06823 #endif
06824          if (eval_symbol (& result, sym_name, & sym_name, input_bfd, 
06825                         finfo, addr, section_offset, locsymcount,
06826                         signed_p))
06827            /* Symbol evaluated OK.  Update to absolute value.  */
06828            set_symbol_value (input_bfd, finfo, index, result);
06829 
06830          else
06831            result = FALSE;
06832        }
06833 
06834       if (internal_relocs != elf_section_data (reloc_sec)->relocs)
06835        free (internal_relocs);
06836     }
06837 
06838   /* If nothing went wrong, then we adjusted 
06839      everything we wanted to adjust.  */
06840   return result;
06841 }
06842 
06843 static void
06844 put_value (bfd_vma        size,
06845           unsigned long  chunksz,
06846           bfd *          input_bfd,
06847           bfd_vma        x,
06848           bfd_byte *     location)
06849 {
06850   location += (size - chunksz);
06851 
06852   for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8)) 
06853     {
06854       switch (chunksz)
06855        {
06856        default:
06857        case 0:
06858          abort ();
06859        case 1:
06860          bfd_put_8 (input_bfd, x, location);
06861          break;
06862        case 2:
06863          bfd_put_16 (input_bfd, x, location);
06864          break;
06865        case 4:
06866          bfd_put_32 (input_bfd, x, location);
06867          break;
06868        case 8:
06869 #ifdef BFD64
06870          bfd_put_64 (input_bfd, x, location);
06871 #else
06872          abort ();
06873 #endif
06874          break;
06875        }
06876     }
06877 }
06878 
06879 static bfd_vma 
06880 get_value (bfd_vma        size,
06881           unsigned long  chunksz,
06882           bfd *          input_bfd,
06883           bfd_byte *     location)
06884 {
06885   bfd_vma x = 0;
06886 
06887   for (; size; size -= chunksz, location += chunksz) 
06888     {
06889       switch (chunksz)
06890        {
06891        default:
06892        case 0:
06893          abort ();
06894        case 1:
06895          x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
06896          break;
06897        case 2:
06898          x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
06899          break;
06900        case 4:
06901          x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
06902          break;
06903        case 8:
06904 #ifdef BFD64
06905          x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
06906 #else
06907          abort ();
06908 #endif
06909          break;
06910        }
06911     }
06912   return x;
06913 }
06914 
06915 static void 
06916 decode_complex_addend
06917     (unsigned long * start,   /* in bits */
06918      unsigned long * oplen,   /* in bits */
06919      unsigned long * len,     /* in bits */
06920      unsigned long * wordsz,  /* in bytes */
06921      unsigned long * chunksz,  /* in bytes */
06922      unsigned long * lsb0_p,
06923      unsigned long * signed_p,
06924      unsigned long * trunc_p,
06925      unsigned long encoded)
06926 {
06927   * start     =  encoded        & 0x3F;
06928   * len       = (encoded >>  6) & 0x3F;
06929   * oplen     = (encoded >> 12) & 0x3F;
06930   * wordsz    = (encoded >> 18) & 0xF;
06931   * chunksz   = (encoded >> 22) & 0xF;
06932   * lsb0_p    = (encoded >> 27) & 1;
06933   * signed_p  = (encoded >> 28) & 1;
06934   * trunc_p   = (encoded >> 29) & 1;
06935 }
06936 
06937 void
06938 bfd_elf_perform_complex_relocation
06939     (bfd *                   output_bfd ATTRIBUTE_UNUSED,
06940      struct bfd_link_info *  info,
06941      bfd *                   input_bfd,
06942      asection *              input_section,
06943      bfd_byte *              contents,
06944      Elf_Internal_Rela *     rel,
06945      Elf_Internal_Sym *      local_syms,
06946      asection **             local_sections)
06947 {
06948   const struct elf_backend_data * bed;
06949   Elf_Internal_Shdr * symtab_hdr;
06950   asection * sec;
06951   bfd_vma relocation = 0, shift, x;
06952   bfd_vma r_symndx;
06953   bfd_vma mask;
06954   unsigned long start, oplen, len, wordsz, 
06955     chunksz, lsb0_p, signed_p, trunc_p;
06956 
06957   /*  Perform this reloc, since it is complex.
06958       (this is not to say that it necessarily refers to a complex
06959       symbol; merely that it is a self-describing CGEN based reloc.
06960       i.e. the addend has the complete reloc information (bit start, end,
06961       word size, etc) encoded within it.).  */ 
06962   r_symndx = ELF32_R_SYM (rel->r_info);
06963   bed = get_elf_backend_data (input_bfd);
06964   if (bed->s->arch_size == 64)
06965     r_symndx >>= 24;
06966 
06967 #ifdef DEBUG
06968   printf ("Performing complex relocation %ld...\n", r_symndx);
06969 #endif
06970 
06971   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
06972   if (r_symndx < symtab_hdr->sh_info)
06973     {
06974       /* The symbol is local.  */
06975       Elf_Internal_Sym * sym;
06976 
06977       sym = local_syms + r_symndx;
06978       sec = local_sections [r_symndx];
06979       relocation = sym->st_value;
06980       if (sym->st_shndx > SHN_UNDEF && 
06981          sym->st_shndx < SHN_LORESERVE)
06982        relocation += (sec->output_offset +
06983                      sec->output_section->vma);
06984     }
06985   else
06986     {
06987       /* The symbol is global.  */
06988       struct elf_link_hash_entry **sym_hashes;
06989       struct elf_link_hash_entry * h;
06990 
06991       sym_hashes = elf_sym_hashes (input_bfd);
06992       h = sym_hashes [r_symndx];
06993 
06994       while (h->root.type == bfd_link_hash_indirect
06995             || h->root.type == bfd_link_hash_warning)
06996        h = (struct elf_link_hash_entry *) h->root.u.i.link;
06997 
06998       if (h->root.type == bfd_link_hash_defined
06999          || h->root.type == bfd_link_hash_defweak)
07000        {
07001          sec = h->root.u.def.section;
07002          relocation = h->root.u.def.value;
07003 
07004          if (! bfd_is_abs_section (sec))
07005            relocation += (sec->output_section->vma 
07006                         + sec->output_offset); 
07007        }
07008       if (h->root.type == bfd_link_hash_undefined
07009          && !((*info->callbacks->undefined_symbol)
07010               (info, h->root.root.string, input_bfd,
07011               input_section, rel->r_offset,
07012               info->unresolved_syms_in_objects == RM_GENERATE_ERROR
07013               || ELF_ST_VISIBILITY (h->other))))
07014        return;
07015     }
07016 
07017   decode_complex_addend (& start, & oplen, & len, & wordsz, 
07018                       & chunksz, & lsb0_p, & signed_p, 
07019                       & trunc_p, rel->r_addend);
07020 
07021   mask = (((1L << (len - 1)) - 1) << 1) | 1;
07022 
07023   if (lsb0_p)
07024     shift = (start + 1) - len;
07025   else
07026     shift = (8 * wordsz) - (start + len);
07027 
07028   x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);        
07029 
07030 #ifdef DEBUG
07031   printf ("Doing complex reloc: "
07032          "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
07033          "chunksz %ld, start %ld, len %ld, oplen %ld\n"
07034          "    dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
07035          lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
07036          oplen, x, mask,  relocation);
07037 #endif
07038 
07039   if (! trunc_p)
07040     {
07041       /* Now do an overflow check.  */
07042       if (bfd_check_overflow ((signed_p ? 
07043                             complain_overflow_signed : 
07044                             complain_overflow_unsigned),
07045                            len, 0, (8 * wordsz), 
07046                            relocation) == bfd_reloc_overflow)
07047        (*_bfd_error_handler) 
07048          ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
07049           "within 0x%lx", 
07050           input_bfd->filename, input_section->name, rel->r_offset,
07051           relocation, (signed_p ? "(signed) " : ""), mask);
07052     }
07053          
07054   /* Do the deed.  */
07055   x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
07056 
07057 #ifdef DEBUG
07058   printf ("           relocation: %8.8lx\n"
07059          "         shifted mask: %8.8lx\n"
07060          " shifted/masked reloc: %8.8lx\n"
07061          "               result: %8.8lx\n",
07062          relocation, (mask << shift), 
07063          ((relocation & mask) << shift), x);
07064 #endif
07065   put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
07066 }
07067 
07068 /* When performing a relocatable link, the input relocations are
07069    preserved.  But, if they reference global symbols, the indices
07070    referenced must be updated.  Update all the relocations in
07071    REL_HDR (there are COUNT of them), using the data in REL_HASH.  */
07072 
07073 static void
07074 elf_link_adjust_relocs (bfd *abfd,
07075                      Elf_Internal_Shdr *rel_hdr,
07076                      unsigned int count,
07077                      struct elf_link_hash_entry **rel_hash)
07078 {
07079   unsigned int i;
07080   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
07081   bfd_byte *erela;
07082   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
07083   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
07084   bfd_vma r_type_mask;
07085   int r_sym_shift;
07086 
07087   if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
07088     {
07089       swap_in = bed->s->swap_reloc_in;
07090       swap_out = bed->s->swap_reloc_out;
07091     }
07092   else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
07093     {
07094       swap_in = bed->s->swap_reloca_in;
07095       swap_out = bed->s->swap_reloca_out;
07096     }
07097   else
07098     abort ();
07099 
07100   if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
07101     abort ();
07102 
07103   if (bed->s->arch_size == 32)
07104     {
07105       r_type_mask = 0xff;
07106       r_sym_shift = 8;
07107     }
07108   else
07109     {
07110       r_type_mask = 0xffffffff;
07111       r_sym_shift = 32;
07112     }
07113 
07114   erela = rel_hdr->contents;
07115   for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
07116     {
07117       Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
07118       unsigned int j;
07119 
07120       if (*rel_hash == NULL)
07121        continue;
07122 
07123       BFD_ASSERT ((*rel_hash)->indx >= 0);
07124 
07125       (*swap_in) (abfd, erela, irela);
07126       for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
07127        irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
07128                         | (irela[j].r_info & r_type_mask));
07129       (*swap_out) (abfd, irela, erela);
07130     }
07131 }
07132 
07133 struct elf_link_sort_rela
07134 {
07135   union {
07136     bfd_vma offset;
07137     bfd_vma sym_mask;
07138   } u;
07139   enum elf_reloc_type_class type;
07140   /* We use this as an array of size int_rels_per_ext_rel.  */
07141   Elf_Internal_Rela rela[1];
07142 };
07143 
07144 static int
07145 elf_link_sort_cmp1 (const void *A, const void *B)
07146 {
07147   const struct elf_link_sort_rela *a = A;
07148   const struct elf_link_sort_rela *b = B;
07149   int relativea, relativeb;
07150 
07151   relativea = a->type == reloc_class_relative;
07152   relativeb = b->type == reloc_class_relative;
07153 
07154   if (relativea < relativeb)
07155     return 1;
07156   if (relativea > relativeb)
07157     return -1;
07158   if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
07159     return -1;
07160   if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
07161     return 1;
07162   if (a->rela->r_offset < b->rela->r_offset)
07163     return -1;
07164   if (a->rela->r_offset > b->rela->r_offset)
07165     return 1;
07166   return 0;
07167 }
07168 
07169 static int
07170 elf_link_sort_cmp2 (const void *A, const void *B)
07171 {
07172   const struct elf_link_sort_rela *a = A;
07173   const struct elf_link_sort_rela *b = B;
07174   int copya, copyb;
07175 
07176   if (a->u.offset < b->u.offset)
07177     return -1;
07178   if (a->u.offset > b->u.offset)
07179     return 1;
07180   copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
07181   copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
07182   if (copya < copyb)
07183     return -1;
07184   if (copya > copyb)
07185     return 1;
07186   if (a->rela->r_offset < b->rela->r_offset)
07187     return -1;
07188   if (a->rela->r_offset > b->rela->r_offset)
07189     return 1;
07190   return 0;
07191 }
07192 
07193 static size_t
07194 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
07195 {
07196   asection *reldyn;
07197   bfd_size_type count, size;
07198   size_t i, ret, sort_elt, ext_size;
07199   bfd_byte *sort, *s_non_relative, *p;
07200   struct elf_link_sort_rela *sq;
07201   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
07202   int i2e = bed->s->int_rels_per_ext_rel;
07203   void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
07204   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
07205   struct bfd_link_order *lo;
07206   bfd_vma r_sym_mask;
07207 
07208   reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
07209   if (reldyn == NULL || reldyn->size == 0)
07210     {
07211       reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
07212       if (reldyn == NULL || reldyn->size == 0)
07213        return 0;
07214       ext_size = bed->s->sizeof_rel;
07215       swap_in = bed->s->swap_reloc_in;
07216       swap_out = bed->s->swap_reloc_out;
07217     }
07218   else
07219     {
07220       ext_size = bed->s->sizeof_rela;
07221       swap_in = bed->s->swap_reloca_in;
07222       swap_out = bed->s->swap_reloca_out;
07223     }
07224   count = reldyn->size / ext_size;
07225 
07226   size = 0;
07227   for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
07228     if (lo->type == bfd_indirect_link_order)
07229       {
07230        asection *o = lo->u.indirect.section;
07231        size += o->size;
07232       }
07233 
07234   if (size != reldyn->size)
07235     return 0;
07236 
07237   sort_elt = (sizeof (struct elf_link_sort_rela)
07238              + (i2e - 1) * sizeof (Elf_Internal_Rela));
07239   sort = bfd_zmalloc (sort_elt * count);
07240   if (sort == NULL)
07241     {
07242       (*info->callbacks->warning)
07243        (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
07244       return 0;
07245     }
07246 
07247   if (bed->s->arch_size == 32)
07248     r_sym_mask = ~(bfd_vma) 0xff;
07249   else
07250     r_sym_mask = ~(bfd_vma) 0xffffffff;
07251 
07252   for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
07253     if (lo->type == bfd_indirect_link_order)
07254       {
07255        bfd_byte *erel, *erelend;
07256        asection *o = lo->u.indirect.section;
07257 
07258        if (o->contents == NULL && o->size != 0)
07259          {
07260            /* This is a reloc section that is being handled as a normal
07261               section.  See bfd_section_from_shdr.  We can't combine
07262               relocs in this case.  */
07263            free (sort);
07264            return 0;
07265          }
07266        erel = o->contents;
07267        erelend = o->contents + o->size;
07268        p = sort + o->output_offset / ext_size * sort_elt;
07269        while (erel < erelend)
07270          {
07271            struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
07272            (*swap_in) (abfd, erel, s->rela);
07273            s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
07274            s->u.sym_mask = r_sym_mask;
07275            p += sort_elt;
07276            erel += ext_size;
07277          }
07278       }
07279 
07280   qsort (sort, count, sort_elt, elf_link_sort_cmp1);
07281 
07282   for (i = 0, p = sort; i < count; i++, p += sort_elt)
07283     {
07284       struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
07285       if (s->type != reloc_class_relative)
07286        break;
07287     }
07288   ret = i;
07289   s_non_relative = p;
07290 
07291   sq = (struct elf_link_sort_rela *) s_non_relative;
07292   for (; i < count; i++, p += sort_elt)
07293     {
07294       struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
07295       if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
07296        sq = sp;
07297       sp->u.offset = sq->rela->r_offset;
07298     }
07299 
07300   qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
07301 
07302   for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
07303     if (lo->type == bfd_indirect_link_order)
07304       {
07305        bfd_byte *erel, *erelend;
07306        asection *o = lo->u.indirect.section;
07307 
07308        erel = o->contents;
07309        erelend = o->contents + o->size;
07310        p = sort + o->output_offset / ext_size * sort_elt;
07311        while (erel < erelend)
07312          {
07313            struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
07314            (*swap_out) (abfd, s->rela, erel);
07315            p += sort_elt;
07316            erel += ext_size;
07317          }
07318       }
07319 
07320   free (sort);
07321   *psec = reldyn;
07322   return ret;
07323 }
07324 
07325 /* Flush the output symbols to the file.  */
07326 
07327 static bfd_boolean
07328 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
07329                          const struct elf_backend_data *bed)
07330 {
07331   if (finfo->symbuf_count > 0)
07332     {
07333       Elf_Internal_Shdr *hdr;
07334       file_ptr pos;
07335       bfd_size_type amt;
07336 
07337       hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
07338       pos = hdr->sh_offset + hdr->sh_size;
07339       amt = finfo->symbuf_count * bed->s->sizeof_sym;
07340       if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
07341          || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
07342        return FALSE;
07343 
07344       hdr->sh_size += amt;
07345       finfo->symbuf_count = 0;
07346     }
07347 
07348   return TRUE;
07349 }
07350 
07351 /* Add a symbol to the output symbol table.  */
07352 
07353 static bfd_boolean
07354 elf_link_output_sym (struct elf_final_link_info *finfo,
07355                    const char *name,
07356                    Elf_Internal_Sym *elfsym,
07357                    asection *input_sec,
07358                    struct elf_link_hash_entry *h)
07359 {
07360   bfd_byte *dest;
07361   Elf_External_Sym_Shndx *destshndx;
07362   bfd_boolean (*output_symbol_hook)
07363     (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
07364      struct elf_link_hash_entry *);
07365   const struct elf_backend_data *bed;
07366 
07367   bed = get_elf_backend_data (finfo->output_bfd);
07368   output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
07369   if (output_symbol_hook != NULL)
07370     {
07371       if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
07372        return FALSE;
07373     }
07374 
07375   if (name == NULL || *name == '\0')
07376     elfsym->st_name = 0;
07377   else if (input_sec->flags & SEC_EXCLUDE)
07378     elfsym->st_name = 0;
07379   else
07380     {
07381       elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
07382                                                      name, TRUE, FALSE);
07383       if (elfsym->st_name == (unsigned long) -1)
07384        return FALSE;
07385     }
07386 
07387   if (finfo->symbuf_count >= finfo->symbuf_size)
07388     {
07389       if (! elf_link_flush_output_syms (finfo, bed))
07390        return FALSE;
07391     }
07392 
07393   dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
07394   destshndx = finfo->symshndxbuf;
07395   if (destshndx != NULL)
07396     {
07397       if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
07398        {
07399          bfd_size_type amt;
07400 
07401          amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
07402          finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
07403          if (destshndx == NULL)
07404            return FALSE;
07405          memset ((char *) destshndx + amt, 0, amt);
07406          finfo->shndxbuf_size *= 2;
07407        }
07408       destshndx += bfd_get_symcount (finfo->output_bfd);
07409     }
07410 
07411   bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
07412   finfo->symbuf_count += 1;
07413   bfd_get_symcount (finfo->output_bfd) += 1;
07414 
07415   return TRUE;
07416 }
07417 
07418 /* Return TRUE if the dynamic symbol SYM in ABFD is supported.  */
07419 
07420 static bfd_boolean
07421 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
07422 {
07423   if (sym->st_shndx > SHN_HIRESERVE)
07424     {
07425       /* The gABI doesn't support dynamic symbols in output sections
07426          beyond 64k.  */
07427       (*_bfd_error_handler)
07428        (_("%B: Too many sections: %d (>= %d)"),
07429         abfd, bfd_count_sections (abfd), SHN_LORESERVE);
07430       bfd_set_error (bfd_error_nonrepresentable_section);
07431       return FALSE;
07432     }
07433   return TRUE;
07434 }
07435 
07436 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
07437    allowing an unsatisfied unversioned symbol in the DSO to match a
07438    versioned symbol that would normally require an explicit version.
07439    We also handle the case that a DSO references a hidden symbol
07440    which may be satisfied by a versioned symbol in another DSO.  */
07441 
07442 static bfd_boolean
07443 elf_link_check_versioned_symbol (struct bfd_link_info *info,
07444                              const struct elf_backend_data *bed,
07445                              struct elf_link_hash_entry *h)
07446 {
07447   bfd *abfd;
07448   struct elf_link_loaded_list *loaded;
07449 
07450   if (!is_elf_hash_table (info->hash))
07451     return FALSE;
07452 
07453   switch (h->root.type)
07454     {
07455     default:
07456       abfd = NULL;
07457       break;
07458 
07459     case bfd_link_hash_undefined:
07460     case bfd_link_hash_undefweak:
07461       abfd = h->root.u.undef.abfd;
07462       if ((abfd->flags & DYNAMIC) == 0
07463          || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
07464        return FALSE;
07465       break;
07466 
07467     case bfd_link_hash_defined:
07468     case bfd_link_hash_defweak:
07469       abfd = h->root.u.def.section->owner;
07470       break;
07471 
07472     case bfd_link_hash_common:
07473       abfd = h->root.u.c.p->section->owner;
07474       break;
07475     }
07476   BFD_ASSERT (abfd != NULL);
07477 
07478   for (loaded = elf_hash_table (info)->loaded;
07479        loaded != NULL;
07480        loaded = loaded->next)
07481     {
07482       bfd *input;
07483       Elf_Internal_Shdr *hdr;
07484       bfd_size_type symcount;
07485       bfd_size_type extsymcount;
07486       bfd_size_type extsymoff;
07487       Elf_Internal_Shdr *versymhdr;
07488       Elf_Internal_Sym *isym;
07489       Elf_Internal_Sym *isymend;
07490       Elf_Internal_Sym *isymbuf;
07491       Elf_External_Versym *ever;
07492       Elf_External_Versym *extversym;
07493 
07494       input = loaded->abfd;
07495 
07496       /* We check each DSO for a possible hidden versioned definition.  */
07497       if (input == abfd
07498          || (input->flags & DYNAMIC) == 0
07499          || elf_dynversym (input) == 0)
07500        continue;
07501 
07502       hdr = &elf_tdata (input)->dynsymtab_hdr;
07503 
07504       symcount = hdr->sh_size / bed->s->sizeof_sym;
07505       if (elf_bad_symtab (input))
07506        {
07507          extsymcount = symcount;
07508          extsymoff = 0;
07509        }
07510       else
07511        {
07512          extsymcount = symcount - hdr->sh_info;
07513          extsymoff = hdr->sh_info;
07514        }
07515 
07516       if (extsymcount == 0)
07517        continue;
07518 
07519       isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
07520                                   NULL, NULL, NULL);
07521       if (isymbuf == NULL)
07522        return FALSE;
07523 
07524       /* Read in any version definitions.  */
07525       versymhdr = &elf_tdata (input)->dynversym_hdr;
07526       extversym = bfd_malloc (versymhdr->sh_size);
07527       if (extversym == NULL)
07528        goto error_ret;
07529 
07530       if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
07531          || (bfd_bread (extversym, versymhdr->sh_size, input)
07532              != versymhdr->sh_size))
07533        {
07534          free (extversym);
07535        error_ret:
07536          free (isymbuf);
07537          return FALSE;
07538        }
07539 
07540       ever = extversym + extsymoff;
07541       isymend = isymbuf + extsymcount;
07542       for (isym = isymbuf; isym < isymend; isym++, ever++)
07543        {
07544          const char *name;
07545          Elf_Internal_Versym iver;
07546          unsigned short version_index;
07547 
07548          if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
07549              || isym->st_shndx == SHN_UNDEF)
07550            continue;
07551 
07552          name = bfd_elf_string_from_elf_section (input,
07553                                             hdr->sh_link,
07554                                             isym->st_name);
07555          if (strcmp (name, h->root.root.string) != 0)
07556            continue;
07557 
07558          _bfd_elf_swap_versym_in (input, ever, &iver);
07559 
07560          if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
07561            {
07562              /* If we have a non-hidden versioned sym, then it should
07563                have provided a definition for the undefined sym.  */
07564              abort ();
07565            }
07566 
07567          version_index = iver.vs_vers & VERSYM_VERSION;
07568          if (version_index == 1 || version_index == 2)
07569            {
07570              /* This is the base or first version.  We can use it.  */
07571              free (extversym);
07572              free (isymbuf);
07573              return TRUE;
07574            }
07575        }
07576 
07577       free (extversym);
07578       free (isymbuf);
07579     }
07580 
07581   return FALSE;
07582 }
07583 
07584 /* Add an external symbol to the symbol table.  This is called from
07585    the hash table traversal routine.  When generating a shared object,
07586    we go through the symbol table twice.  The first time we output
07587    anything that might have been forced to local scope in a version
07588    script.  The second time we output the symbols that are still
07589    global symbols.  */
07590 
07591 static bfd_boolean
07592 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
07593 {
07594   struct elf_outext_info *eoinfo = data;
07595   struct elf_final_link_info *finfo = eoinfo->finfo;
07596   bfd_boolean strip;
07597   Elf_Internal_Sym sym;
07598   asection *input_sec;
07599   const struct elf_backend_data *bed;
07600 
07601   if (h->root.type == bfd_link_hash_warning)
07602     {
07603       h = (struct elf_link_hash_entry *) h->root.u.i.link;
07604       if (h->root.type == bfd_link_hash_new)
07605        return TRUE;
07606     }
07607 
07608   /* Decide whether to output this symbol in this pass.  */
07609   if (eoinfo->localsyms)
07610     {
07611       if (!h->forced_local)
07612        return TRUE;
07613     }
07614   else
07615     {
07616       if (h->forced_local)
07617        return TRUE;
07618     }
07619 
07620   bed = get_elf_backend_data (finfo->output_bfd);
07621 
07622   if (h->root.type == bfd_link_hash_undefined)
07623     {
07624       /* If we have an undefined symbol reference here then it must have
07625         come from a shared library that is being linked in.  (Undefined
07626         references in regular files have already been handled).  */
07627       bfd_boolean ignore_undef = FALSE;
07628 
07629       /* Some symbols may be special in that the fact that they're
07630         undefined can be safely ignored - let backend determine that.  */
07631       if (bed->elf_backend_ignore_undef_symbol)
07632        ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
07633 
07634       /* If we are reporting errors for this situation then do so now.  */
07635       if (ignore_undef == FALSE
07636          && h->ref_dynamic
07637          && ! h->ref_regular
07638          && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
07639          && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
07640        {
07641          if (! (finfo->info->callbacks->undefined_symbol
07642                (finfo->info, h->root.root.string, h->root.u.undef.abfd,
07643                 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
07644            {
07645              eoinfo->failed = TRUE;
07646              return FALSE;
07647            }
07648        }
07649     }
07650 
07651   /* We should also warn if a forced local symbol is referenced from
07652      shared libraries.  */
07653   if (! finfo->info->relocatable
07654       && (! finfo->info->shared)
07655       && h->forced_local
07656       && h->ref_dynamic
07657       && !h->dynamic_def
07658       && !h->dynamic_weak
07659       && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
07660     {
07661       (*_bfd_error_handler)
07662        (_("%B: %s symbol `%s' in %B is referenced by DSO"),
07663         finfo->output_bfd,
07664         h->root.u.def.section == bfd_abs_section_ptr
07665         ? finfo->output_bfd : h->root.u.def.section->owner,
07666         ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
07667         ? "internal"
07668         : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
07669         ? "hidden" : "local",
07670         h->root.root.string);
07671       eoinfo->failed = TRUE;
07672       return FALSE;
07673     }
07674 
07675   /* We don't want to output symbols that have never been mentioned by
07676      a regular file, or that we have been told to strip.  However, if
07677      h->indx is set to -2, the symbol is used by a reloc and we must
07678      output it.  */
07679   if (h->indx == -2)
07680     strip = FALSE;
07681   else if ((h->def_dynamic
07682            || h->ref_dynamic
07683            || h->root.type == bfd_link_hash_new)
07684           && !h->def_regular
07685           && !h->ref_regular)
07686     strip = TRUE;
07687   else if (finfo->info->strip == strip_all)
07688     strip = TRUE;
07689   else if (finfo->info->strip == strip_some
07690           && bfd_hash_lookup (finfo->info->keep_hash,
07691                             h->root.root.string, FALSE, FALSE) == NULL)
07692     strip = TRUE;
07693   else if (finfo->info->strip_discarded
07694           && (h->root.type == bfd_link_hash_defined
07695               || h->root.type == bfd_link_hash_defweak)
07696           && elf_discarded_section (h->root.u.def.section))
07697     strip = TRUE;
07698   else
07699     strip = FALSE;
07700 
07701   /* If we're stripping it, and it's not a dynamic symbol, there's
07702      nothing else to do unless it is a forced local symbol.  */
07703   if (strip
07704       && h->dynindx == -1
07705       && !h->forced_local)
07706     return TRUE;
07707 
07708   sym.st_value = 0;
07709   sym.st_size = h->size;
07710   sym.st_other = h->other;
07711   if (h->forced_local)
07712     sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
07713   else if (h->root.type == bfd_link_hash_undefweak
07714           || h->root.type == bfd_link_hash_defweak)
07715     sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
07716   else
07717     sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
07718 
07719   switch (h->root.type)
07720     {
07721     default:
07722     case bfd_link_hash_new:
07723     case bfd_link_hash_warning:
07724       abort ();
07725       return FALSE;
07726 
07727     case bfd_link_hash_undefined:
07728     case bfd_link_hash_undefweak:
07729       input_sec = bfd_und_section_ptr;
07730       sym.st_shndx = SHN_UNDEF;
07731       break;
07732 
07733     case bfd_link_hash_defined:
07734     case bfd_link_hash_defweak:
07735       {
07736        input_sec = h->root.u.def.section;
07737        if (input_sec->output_section != NULL)
07738          {
07739            sym.st_shndx =
07740              _bfd_elf_section_from_bfd_section (finfo->output_bfd,
07741                                            input_sec->output_section);
07742            if (sym.st_shndx == SHN_BAD)
07743              {
07744               (*_bfd_error_handler)
07745                 (_("%B: could not find output section %A for input section %A"),
07746                  finfo->output_bfd, input_sec->output_section, input_sec);
07747               eoinfo->failed = TRUE;
07748               return FALSE;
07749              }
07750 
07751            /* ELF symbols in relocatable files are section relative,
07752               but in nonrelocatable files they are virtual
07753               addresses.  */
07754            sym.st_value = h->root.u.def.value + input_sec->output_offset;
07755            if (! finfo->info->relocatable)
07756              {
07757               sym.st_value += input_sec->output_section->vma;
07758               if (h->type == STT_TLS)
07759                 {
07760                   /* STT_TLS symbols are relative to PT_TLS segment
07761                      base.  */
07762                   BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
07763                   sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
07764                 }
07765              }
07766          }
07767        else
07768          {
07769            BFD_ASSERT (input_sec->owner == NULL
07770                      || (input_sec->owner->flags & DYNAMIC) != 0);
07771            sym.st_shndx = SHN_UNDEF;
07772            input_sec = bfd_und_section_ptr;
07773          }
07774       }
07775       break;
07776 
07777     case bfd_link_hash_common:
07778       input_sec = h->root.u.c.p->section;
07779       sym.st_shndx = bed->common_section_index (input_sec);
07780       sym.st_value = 1 << h->root.u.c.p->alignment_power;
07781       break;
07782 
07783     case bfd_link_hash_indirect:
07784       /* These symbols are created by symbol versioning.  They point
07785         to the decorated version of the name.  For example, if the
07786         symbol foo@@GNU_1.2 is the default, which should be used when
07787         foo is used with no version, then we add an indirect symbol
07788         foo which points to foo@@GNU_1.2.  We ignore these symbols,
07789         since the indirected symbol is already in the hash table.  */
07790       return TRUE;
07791     }
07792 
07793   /* Give the processor backend a chance to tweak the symbol value,
07794      and also to finish up anything that needs to be done for this
07795      symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
07796      forced local syms when non-shared is due to a historical quirk.  */
07797   if ((h->dynindx != -1
07798        || h->forced_local)
07799       && ((finfo->info->shared
07800           && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
07801               || h->root.type != bfd_link_hash_undefweak))
07802          || !h->forced_local)
07803       && elf_hash_table (finfo->info)->dynamic_sections_created)
07804     {
07805       if (! ((*bed->elf_backend_finish_dynamic_symbol)
07806             (finfo->output_bfd, finfo->info, h, &sym)))
07807        {
07808          eoinfo->failed = TRUE;
07809          return FALSE;
07810        }
07811     }
07812 
07813   /* If we are marking the symbol as undefined, and there are no
07814      non-weak references to this symbol from a regular object, then
07815      mark the symbol as weak undefined; if there are non-weak
07816      references, mark the symbol as strong.  We can't do this earlier,
07817      because it might not be marked as undefined until the
07818      finish_dynamic_symbol routine gets through with it.  */
07819   if (sym.st_shndx == SHN_UNDEF
07820       && h->ref_regular
07821       && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
07822          || ELF_ST_BIND (sym.st_info) == STB_WEAK))
07823     {
07824       int bindtype;
07825 
07826       if (h->ref_regular_nonweak)
07827        bindtype = STB_GLOBAL;
07828       else
07829        bindtype = STB_WEAK;
07830       sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
07831     }
07832 
07833   /* If a non-weak symbol with non-default visibility is not defined
07834      locally, it is a fatal error.  */
07835   if (! finfo->info->relocatable
07836       && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
07837       && ELF_ST_BIND (sym.st_info) != STB_WEAK
07838       && h->root.type == bfd_link_hash_undefined
07839       && !h->def_regular)
07840     {
07841       (*_bfd_error_handler)
07842        (_("%B: %s symbol `%s' isn't defined"),
07843         finfo->output_bfd,
07844         ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
07845         ? "protected"
07846         : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
07847         ? "internal" : "hidden",
07848         h->root.root.string);
07849       eoinfo->failed = TRUE;
07850       return FALSE;
07851     }
07852 
07853   /* If this symbol should be put in the .dynsym section, then put it
07854      there now.  We already know the symbol index.  We also fill in
07855      the entry in the .hash section.  */
07856   if (h->dynindx != -1
07857       && elf_hash_table (finfo->info)->dynamic_sections_created)
07858     {
07859       bfd_byte *esym;
07860 
07861       sym.st_name = h->dynstr_index;
07862       esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
07863       if (! check_dynsym (finfo->output_bfd, &sym))
07864        {
07865          eoinfo->failed = TRUE;
07866          return FALSE;
07867        }
07868       bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
07869 
07870       if (finfo->hash_sec != NULL)
07871        {
07872          size_t hash_entry_size;
07873          bfd_byte *bucketpos;
07874          bfd_vma chain;
07875          size_t bucketcount;
07876          size_t bucket;
07877 
07878          bucketcount = elf_hash_table (finfo->info)->bucketcount;
07879          bucket = h->u.elf_hash_value % bucketcount;
07880 
07881          hash_entry_size
07882            = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
07883          bucketpos = ((bfd_byte *) finfo->hash_sec->contents
07884                      + (bucket + 2) * hash_entry_size);
07885          chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
07886          bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
07887          bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
07888                  ((bfd_byte *) finfo->hash_sec->contents
07889                   + (bucketcount + 2 + h->dynindx) * hash_entry_size));
07890        }
07891 
07892       if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
07893        {
07894          Elf_Internal_Versym iversym;
07895          Elf_External_Versym *eversym;
07896 
07897          if (!h->def_regular)
07898            {
07899              if (h->verinfo.verdef == NULL)
07900               iversym.vs_vers = 0;
07901              else
07902               iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
07903            }
07904          else
07905            {
07906              if (h->verinfo.vertree == NULL)
07907               iversym.vs_vers = 1;
07908              else
07909               iversym.vs_vers = h->verinfo.vertree->vernum + 1;
07910              if (finfo->info->create_default_symver)
07911               iversym.vs_vers++;
07912            }
07913 
07914          if (h->hidden)
07915            iversym.vs_vers |= VERSYM_HIDDEN;
07916 
07917          eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
07918          eversym += h->dynindx;
07919          _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
07920        }
07921     }
07922 
07923   /* If we're stripping it, then it was just a dynamic symbol, and
07924      there's nothing else to do.  */
07925   if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
07926     return TRUE;
07927 
07928   h->indx = bfd_get_symcount (finfo->output_bfd);
07929 
07930   if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
07931     {
07932       eoinfo->failed = TRUE;
07933       return FALSE;
07934     }
07935 
07936   return TRUE;
07937 }
07938 
07939 /* Return TRUE if special handling is done for relocs in SEC against
07940    symbols defined in discarded sections.  */
07941 
07942 static bfd_boolean
07943 elf_section_ignore_discarded_relocs (asection *sec)
07944 {
07945   const struct elf_backend_data *bed;
07946 
07947   switch (sec->sec_info_type)
07948     {
07949     case ELF_INFO_TYPE_STABS:
07950     case ELF_INFO_TYPE_EH_FRAME:
07951       return TRUE;
07952     default:
07953       break;
07954     }
07955 
07956   bed = get_elf_backend_data (sec->owner);
07957   if (bed->elf_backend_ignore_discarded_relocs != NULL
07958       && (*bed->elf_backend_ignore_discarded_relocs) (sec))
07959     return TRUE;
07960 
07961   return FALSE;
07962 }
07963 
07964 /* Return a mask saying how ld should treat relocations in SEC against
07965    symbols defined in discarded sections.  If this function returns
07966    COMPLAIN set, ld will issue a warning message.  If this function
07967    returns PRETEND set, and the discarded section was link-once and the
07968    same size as the kept link-once section, ld will pretend that the
07969    symbol was actually defined in the kept section.  Otherwise ld will
07970    zero the reloc (at least that is the intent, but some cooperation by
07971    the target dependent code is needed, particularly for REL targets).  */
07972 
07973 unsigned int
07974 _bfd_elf_default_action_discarded (asection *sec)
07975 {
07976   if (sec->flags & SEC_DEBUGGING)
07977     return PRETEND;
07978 
07979   if (