• Main Page
  • Modules
  • Data Types
  • Files

osprey/cygnus/bfd/elflink.c

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