* elflink.c (elf_link_read_relocs_from_section): Don't use
[binutils-gdb.git] / bfd / elflink.c
index 58acc144bc937b2ee2571e0d29af312f3515724e..7d63383be02083ad65098fcbdda118b8e9694c88 100644 (file)
@@ -117,7 +117,7 @@ _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   struct bfd_link_hash_entry *bh;
   const struct elf_backend_data *bed;
 
-  if (! is_elf_hash_table (info))
+  if (! is_elf_hash_table (info->hash))
     return FALSE;
 
   if (elf_hash_table (info)->dynamic_sections_created)
@@ -144,8 +144,7 @@ _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
        return FALSE;
     }
 
-  if (! info->traditional_format
-      && info->hash->creator->flavour == bfd_target_elf_flavour)
+  if (! info->traditional_format)
     {
       s = bfd_make_section (abfd, ".eh_frame_hdr");
       if (s == NULL
@@ -353,9 +352,8 @@ _bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
   if (h->dynindx == -1)
     {
       struct elf_strtab_hash *dynstr;
-      char *p, *alc;
+      char *p;
       const char *name;
-      bfd_boolean copy;
       bfd_size_type indx;
 
       /* XXX: The ABI draft says the linker must turn hidden and
@@ -393,28 +391,18 @@ _bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
         table.  */
       name = h->root.root.string;
       p = strchr (name, ELF_VER_CHR);
-      if (p == NULL)
-       {
-         alc = NULL;
-         copy = FALSE;
-       }
-      else
-       {
-         size_t len = p - name + 1;
+      if (p != NULL)
+       /* We know that the p points into writable memory.  In fact,
+          there are only a few symbols that have read-only names, being
+          those like _GLOBAL_OFFSET_TABLE_ that are created specially
+          by the backends.  Most symbols will have names pointing into
+          an ELF string table read from a file, or to objalloc memory.  */
+       *p = 0;
 
-         alc = bfd_malloc (len);
-         if (alc == NULL)
-           return FALSE;
-         memcpy (alc, name, len - 1);
-         alc[len - 1] = '\0';
-         name = alc;
-         copy = TRUE;
-       }
-
-      indx = _bfd_elf_strtab_add (dynstr, name, copy);
+      indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
 
-      if (alc != NULL)
-       free (alc);
+      if (p != NULL)
+       *p = ELF_VER_CHR;
 
       if (indx == (bfd_size_type) -1)
        return FALSE;
@@ -435,13 +423,20 @@ bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
 {
   struct elf_link_hash_entry *h;
 
-  if (info->hash->creator->flavour != bfd_target_elf_flavour)
+  if (!is_elf_hash_table (info->hash))
     return TRUE;
 
   h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
   if (h == NULL)
     return FALSE;
 
+  /* Since we're defining the symbol, don't let it seem to have not
+     been defined.  record_dynamic_symbol and size_dynamic_sections
+     may depend on this.  */
+  if (h->root.type == bfd_link_hash_undefweak
+      || h->root.type == bfd_link_hash_undefined)
+    h->root.type = bfd_link_hash_new;
+
   if (h->root.type == bfd_link_hash_new)
     h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
 
@@ -505,7 +500,7 @@ elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
   Elf_External_Sym_Shndx eshndx;
   char esym[sizeof (Elf64_External_Sym)];
 
-  if (! is_elf_hash_table (info))
+  if (! is_elf_hash_table (info->hash))
     return 0;
 
   /* See if the entry exists already.  */
@@ -806,7 +801,7 @@ _bfd_elf_merge_symbol (bfd *abfd,
   else
     olddef = TRUE;
 
-  /* We need to rememeber if a symbol has a definition in a dynamic
+  /* We need to remember if a symbol has a definition in a dynamic
      object or is weak in all dynamic objects. Internal and hidden
      visibility will make it unavailable to dynamic objects.  */
   if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0)
@@ -855,8 +850,26 @@ _bfd_elf_merge_symbol (bfd *abfd,
         object, we remove the old definition.  */
       if ((*sym_hash)->root.type == bfd_link_hash_indirect)
        h = *sym_hash;
-      h->root.type = bfd_link_hash_new;
-      h->root.u.undef.abfd = NULL;
+
+      if ((h->root.und_next || info->hash->undefs_tail == &h->root)
+         && bfd_is_und_section (sec))
+       {
+         /* If the new symbol is undefined and the old symbol was
+            also undefined before, we need to make sure
+            _bfd_generic_link_add_one_symbol doesn't mess
+            up the linker hash table undefs list. Since the old
+            definition came from a dynamic object, it is still on the
+            undefs list.  */
+         h->root.type = bfd_link_hash_undefined;
+         /* FIXME: What if the new symbol is weak undefined?  */
+         h->root.u.undef.abfd = abfd;
+       }
+      else
+       {
+         h->root.type = bfd_link_hash_new;
+         h->root.u.undef.abfd = NULL;
+       }
+
       if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
        {
          h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
@@ -869,7 +882,7 @@ _bfd_elf_merge_symbol (bfd *abfd,
       return TRUE;
     }
 
-  /* We need to treat weak definiton right, depending on if there is a
+  /* We need to treat weak definition right, depending on if there is a
      definition from a dynamic object.  */
   if (bind == STB_WEAK)
     {
@@ -1138,7 +1151,7 @@ _bfd_elf_merge_symbol (bfd *abfd,
              h->size, abfd, bfd_link_hash_common, sym->st_size)))
        return FALSE;
 
-      /* If the predumed common symbol in the dynamic object is
+      /* If the presumed common symbol in the dynamic object is
         larger, pretend that the new symbol has its size.  */
 
       if (h->size > *pvalue)
@@ -1268,7 +1281,7 @@ _bfd_elf_add_default_symbol (bfd *abfd,
 
   if (override)
     {
-      /* We are overridden by an old defition. We need to check if we
+      /* We are overridden by an old definition. We need to check if we
         need to create the indirect symbol from the default name.  */
       hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
                                 FALSE, FALSE);
@@ -1427,7 +1440,7 @@ nondefault:
     {
       /* Here SHORTNAME is a versioned name, so we don't expect to see
         the type of override we do in the case above unless it is
-        overridden by a versioned definiton.  */
+        overridden by a versioned definition.  */
       if (hi->root.type != bfd_link_hash_defined
          && hi->root.type != bfd_link_hash_defweak)
        (*_bfd_error_handler)
@@ -1505,22 +1518,18 @@ _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
 
       for (t = eif->verdefs; t != NULL; t = t->next)
        {
-         if (t->globals != NULL)
+         if (t->globals.list != NULL)
            {
-             for (d = t->globals; d != NULL; d = d->next)
-               {
-                 if ((*d->match) (d, h->root.root.string))
-                   goto doit;
-               }
+             d = (*t->match) (&t->globals, NULL, h->root.root.string);
+             if (d != NULL)
+               goto doit;
            }
 
-         if (t->locals != NULL)
+         if (t->locals.list != NULL)
            {
-             for (d = t->locals ; d != NULL; d = d->next)
-               {
-                 if ((*d->match) (d, h->root.root.string))
-                   return TRUE;
-               }
+             d = (*t->match) (&t->locals, NULL, h->root.root.string);
+             if (d != NULL)
+               return TRUE;
            }
        }
 
@@ -1699,31 +1708,19 @@ _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
              t->used = TRUE;
              d = NULL;
 
-             if (t->globals != NULL)
-               {
-                 for (d = t->globals; d != NULL; d = d->next)
-                   if ((*d->match) (d, alc))
-                     break;
-               }
+             if (t->globals.list != NULL)
+               d = (*t->match) (&t->globals, NULL, alc);
 
              /* See if there is anything to force this symbol to
                 local scope.  */
-             if (d == NULL && t->locals != NULL)
+             if (d == NULL && t->locals.list != NULL)
                {
-                 for (d = t->locals; d != NULL; d = d->next)
-                   {
-                     if ((*d->match) (d, alc))
-                       {
-                         if (h->dynindx != -1
-                             && info->shared
-                             && ! info->export_dynamic)
-                           {
-                             (*bed->elf_backend_hide_symbol) (info, h, TRUE);
-                           }
-
-                         break;
-                       }
-                   }
+                 d = (*t->match) (&t->locals, NULL, alc);
+                 if (d != NULL
+                     && h->dynindx != -1
+                     && info->shared
+                     && ! info->export_dynamic)
+                   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
                }
 
              free (alc);
@@ -1744,18 +1741,14 @@ _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
            return TRUE;
 
          amt = sizeof *t;
-         t = bfd_alloc (sinfo->output_bfd, amt);
+         t = bfd_zalloc (sinfo->output_bfd, amt);
          if (t == NULL)
            {
              sinfo->failed = TRUE;
              return FALSE;
            }
 
-         t->next = NULL;
          t->name = p;
-         t->globals = NULL;
-         t->locals = NULL;
-         t->deps = NULL;
          t->name_indx = (unsigned int) -1;
          t->used = TRUE;
 
@@ -1801,30 +1794,26 @@ _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
       local_ver = NULL;
       for (t = sinfo->verdefs; t != NULL; t = t->next)
        {
-         if (t->globals != NULL)
+         if (t->globals.list != NULL)
            {
              bfd_boolean matched;
 
              matched = FALSE;
-             for (d = t->globals; d != NULL; d = d->next)
-               {
-                 if ((*d->match) (d, h->root.root.string))
-                   {
-                     if (d->symver)
-                       matched = TRUE;
-                     else
-                       {
-                         /* There is a version without definition.  Make
-                            the symbol the default definition for this
-                            version.  */
-                         h->verinfo.vertree = t;
-                         local_ver = NULL;
-                         d->script = 1;
-                         break;
-                       }
-                   }
-               }
-
+             d = NULL;
+             while ((d = (*t->match) (&t->globals, d,
+                                      h->root.root.string)) != NULL)
+               if (d->symver)
+                 matched = TRUE;
+               else
+                 {
+                   /* There is a version without definition.  Make
+                      the symbol the default definition for this
+                      version.  */
+                   h->verinfo.vertree = t;
+                   local_ver = NULL;
+                   d->script = 1;
+                   break;
+                 }
              if (d != NULL)
                break;
              else if (matched)
@@ -1833,19 +1822,18 @@ _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
                (*bed->elf_backend_hide_symbol) (info, h, TRUE);
            }
 
-         if (t->locals != NULL)
+         if (t->locals.list != NULL)
            {
-             for (d = t->locals; d != NULL; d = d->next)
+             d = NULL;
+             while ((d = (*t->match) (&t->locals, d,
+                                      h->root.root.string)) != NULL)
                {
+                 local_ver = t;
                  /* If the match is "*", keep looking for a more
-                    explicit, perhaps even global, match.  */
-                 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
-                   local_ver = t;
-                 else if ((*d->match) (d, h->root.root.string))
-                   {
-                     local_ver = t;
-                     break;
-                   }
+                    explicit, perhaps even global, match.
+                    XXX: Shouldn't this be !d->wildcard instead?  */
+                 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
+                   break;
                }
 
              if (d != NULL)
@@ -1879,6 +1867,7 @@ _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
 
 static bfd_boolean
 elf_link_read_relocs_from_section (bfd *abfd,
+                                  asection *sec,
                                   Elf_Internal_Shdr *shdr,
                                   void *external_relocs,
                                   Elf_Internal_Rela *internal_relocs)
@@ -1888,10 +1877,8 @@ elf_link_read_relocs_from_section (bfd *abfd,
   const bfd_byte *erela;
   const bfd_byte *erelaend;
   Elf_Internal_Rela *irela;
-
-  /* If there aren't any relocations, that's OK.  */
-  if (!shdr)
-    return TRUE;
+  Elf_Internal_Shdr *symtab_hdr;
+  size_t nsyms;
 
   /* Position ourselves at the start of the section.  */
   if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
@@ -1901,6 +1888,9 @@ elf_link_read_relocs_from_section (bfd *abfd,
   if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
     return FALSE;
 
+  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
+  nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
+
   bed = get_elf_backend_data (abfd);
 
   /* Convert the external relocations to the internal format.  */
@@ -1915,11 +1905,25 @@ elf_link_read_relocs_from_section (bfd *abfd,
     }
 
   erela = external_relocs;
-  erelaend = erela + NUM_SHDR_ENTRIES (shdr) * shdr->sh_entsize;
+  erelaend = erela + shdr->sh_size;
   irela = internal_relocs;
   while (erela < erelaend)
     {
+      bfd_vma r_symndx;
+
       (*swap_in) (abfd, erela, irela);
+      r_symndx = ELF32_R_SYM (irela->r_info);
+      if (bed->s->arch_size == 64)
+       r_symndx >>= 24;
+      if ((size_t) r_symndx >= nsyms)
+       {
+         (*_bfd_error_handler)
+           (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
+            bfd_archive_filename (abfd), (unsigned long) r_symndx,
+            (unsigned long) nsyms, irela->r_offset, sec->name);
+         bfd_set_error (bfd_error_bad_value);
+         return FALSE;
+       }
       irela += bed->s->int_rels_per_ext_rel;
       erela += shdr->sh_entsize;
     }
@@ -1983,16 +1987,17 @@ _bfd_elf_link_read_relocs (bfd *abfd,
       external_relocs = alloc1;
     }
 
-  if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
+  if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
                                          external_relocs,
                                          internal_relocs))
     goto error_return;
-  if (!elf_link_read_relocs_from_section
-      (abfd,
-       elf_section_data (o)->rel_hdr2,
-       ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
-       internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
-                         * bed->s->int_rels_per_ext_rel)))
+  if (elf_section_data (o)->rel_hdr2
+      && (!elf_link_read_relocs_from_section
+         (abfd, o,
+          elf_section_data (o)->rel_hdr2,
+          ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
+          internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
+                             * bed->s->int_rels_per_ext_rel))))
     goto error_return;
 
   /* Cache the results for next time, if we can.  */
@@ -2222,7 +2227,7 @@ _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
      will force it local.  */
   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
       && eif->info->shared
-      && is_elf_hash_table (eif->info)
+      && is_elf_hash_table (eif->info->hash)
       && (eif->info->symbolic
          || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
@@ -2292,7 +2297,7 @@ _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
   bfd *dynobj;
   const struct elf_backend_data *bed;
 
-  if (! is_elf_hash_table (eif->info))
+  if (! is_elf_hash_table (eif->info->hash))
     return FALSE;
 
   if (h->root.type == bfd_link_hash_warning)
@@ -2535,3 +2540,376 @@ _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
      dynamic linker will resolve them locally.  */
   return local_protected;
 }
+
+/* Caches some TLS segment info, and ensures that the TLS segment vma is
+   aligned.  Returns the first TLS output section.  */
+
+struct bfd_section *
+_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
+{
+  struct bfd_section *sec, *tls;
+  unsigned int align = 0;
+
+  for (sec = obfd->sections; sec != NULL; sec = sec->next)
+    if ((sec->flags & SEC_THREAD_LOCAL) != 0)
+      break;
+  tls = sec;
+
+  for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
+    if (sec->alignment_power > align)
+      align = sec->alignment_power;
+
+  elf_hash_table (info)->tls_sec = tls;
+
+  /* Ensure the alignment of the first section is the largest alignment,
+     so that the tls segment starts aligned.  */
+  if (tls != NULL)
+    tls->alignment_power = align;
+
+  return tls;
+}
+
+/* Return TRUE iff this is a non-common, definition of a non-function symbol.  */
+static bfd_boolean
+is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
+                                 Elf_Internal_Sym *sym)
+{
+  /* Local symbols do not count, but target specific ones might.  */
+  if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
+      && ELF_ST_BIND (sym->st_info) < STB_LOOS)
+    return FALSE;
+
+  /* Function symbols do not count.  */
+  if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
+    return FALSE;
+
+  /* If the section is undefined, then so is the symbol.  */
+  if (sym->st_shndx == SHN_UNDEF)
+    return FALSE;
+
+  /* If the symbol is defined in the common section, then
+     it is a common definition and so does not count.  */
+  if (sym->st_shndx == SHN_COMMON)
+    return FALSE;
+
+  /* If the symbol is in a target specific section then we
+     must rely upon the backend to tell us what it is.  */
+  if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
+    /* FIXME - this function is not coded yet:
+
+       return _bfd_is_global_symbol_definition (abfd, sym);
+
+       Instead for now assume that the definition is not global,
+       Even if this is wrong, at least the linker will behave
+       in the same way that it used to do.  */
+    return FALSE;
+
+  return TRUE;
+}
+
+/* Search the symbol table of the archive element of the archive ABFD
+   whose archive map contains a mention of SYMDEF, and determine if
+   the symbol is defined in this element.  */
+static bfd_boolean
+elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
+{
+  Elf_Internal_Shdr * hdr;
+  bfd_size_type symcount;
+  bfd_size_type extsymcount;
+  bfd_size_type extsymoff;
+  Elf_Internal_Sym *isymbuf;
+  Elf_Internal_Sym *isym;
+  Elf_Internal_Sym *isymend;
+  bfd_boolean result;
+
+  abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
+  if (abfd == NULL)
+    return FALSE;
+
+  if (! bfd_check_format (abfd, bfd_object))
+    return FALSE;
+
+  /* If we have already included the element containing this symbol in the
+     link then we do not need to include it again.  Just claim that any symbol
+     it contains is not a definition, so that our caller will not decide to
+     (re)include this element.  */
+  if (abfd->archive_pass)
+    return FALSE;
+
+  /* Select the appropriate symbol table.  */
+  if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
+    hdr = &elf_tdata (abfd)->symtab_hdr;
+  else
+    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
+
+  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
+
+  /* The sh_info field of the symtab header tells us where the
+     external symbols start.  We don't care about the local symbols.  */
+  if (elf_bad_symtab (abfd))
+    {
+      extsymcount = symcount;
+      extsymoff = 0;
+    }
+  else
+    {
+      extsymcount = symcount - hdr->sh_info;
+      extsymoff = hdr->sh_info;
+    }
+
+  if (extsymcount == 0)
+    return FALSE;
+
+  /* Read in the symbol table.  */
+  isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
+                                 NULL, NULL, NULL);
+  if (isymbuf == NULL)
+    return FALSE;
+
+  /* Scan the symbol table looking for SYMDEF.  */
+  result = FALSE;
+  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
+    {
+      const char *name;
+
+      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
+                                             isym->st_name);
+      if (name == NULL)
+       break;
+
+      if (strcmp (name, symdef->name) == 0)
+       {
+         result = is_global_data_symbol_definition (abfd, isym);
+         break;
+       }
+    }
+
+  free (isymbuf);
+
+  return result;
+}
+\f
+/* Add symbols from an ELF archive file to the linker hash table.  We
+   don't use _bfd_generic_link_add_archive_symbols because of a
+   problem which arises on UnixWare.  The UnixWare libc.so is an
+   archive which includes an entry libc.so.1 which defines a bunch of
+   symbols.  The libc.so archive also includes a number of other
+   object files, which also define symbols, some of which are the same
+   as those defined in libc.so.1.  Correct linking requires that we
+   consider each object file in turn, and include it if it defines any
+   symbols we need.  _bfd_generic_link_add_archive_symbols does not do
+   this; it looks through the list of undefined symbols, and includes
+   any object file which defines them.  When this algorithm is used on
+   UnixWare, it winds up pulling in libc.so.1 early and defining a
+   bunch of symbols.  This means that some of the other objects in the
+   archive are not included in the link, which is incorrect since they
+   precede libc.so.1 in the archive.
+
+   Fortunately, ELF archive handling is simpler than that done by
+   _bfd_generic_link_add_archive_symbols, which has to allow for a.out
+   oddities.  In ELF, if we find a symbol in the archive map, and the
+   symbol is currently undefined, we know that we must pull in that
+   object file.
+
+   Unfortunately, we do have to make multiple passes over the symbol
+   table until nothing further is resolved.  */
+
+bfd_boolean
+_bfd_elf_link_add_archive_symbols (bfd *abfd,
+                                  struct bfd_link_info *info)
+{
+  symindex c;
+  bfd_boolean *defined = NULL;
+  bfd_boolean *included = NULL;
+  carsym *symdefs;
+  bfd_boolean loop;
+  bfd_size_type amt;
+
+  if (! bfd_has_map (abfd))
+    {
+      /* An empty archive is a special case.  */
+      if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
+       return TRUE;
+      bfd_set_error (bfd_error_no_armap);
+      return FALSE;
+    }
+
+  /* Keep track of all symbols we know to be already defined, and all
+     files we know to be already included.  This is to speed up the
+     second and subsequent passes.  */
+  c = bfd_ardata (abfd)->symdef_count;
+  if (c == 0)
+    return TRUE;
+  amt = c;
+  amt *= sizeof (bfd_boolean);
+  defined = bfd_zmalloc (amt);
+  included = bfd_zmalloc (amt);
+  if (defined == NULL || included == NULL)
+    goto error_return;
+
+  symdefs = bfd_ardata (abfd)->symdefs;
+
+  do
+    {
+      file_ptr last;
+      symindex i;
+      carsym *symdef;
+      carsym *symdefend;
+
+      loop = FALSE;
+      last = -1;
+
+      symdef = symdefs;
+      symdefend = symdef + c;
+      for (i = 0; symdef < symdefend; symdef++, i++)
+       {
+         struct elf_link_hash_entry *h;
+         bfd *element;
+         struct bfd_link_hash_entry *undefs_tail;
+         symindex mark;
+
+         if (defined[i] || included[i])
+           continue;
+         if (symdef->file_offset == last)
+           {
+             included[i] = TRUE;
+             continue;
+           }
+
+         h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
+                                   FALSE, FALSE, FALSE);
+
+         if (h == NULL)
+           {
+             char *p, *copy;
+             size_t len, first;
+
+             /* If this is a default version (the name contains @@),
+                look up the symbol again with only one `@' as well
+                as without the version.  The effect is that references
+                to the symbol with and without the version will be
+                matched by the default symbol in the archive.  */
+
+             p = strchr (symdef->name, ELF_VER_CHR);
+             if (p == NULL || p[1] != ELF_VER_CHR)
+               continue;
+
+             /* First check with only one `@'.  */
+             len = strlen (symdef->name);
+             copy = bfd_alloc (abfd, len);
+             if (copy == NULL)
+               goto error_return;
+             first = p - symdef->name + 1;
+             memcpy (copy, symdef->name, first);
+             memcpy (copy + first, symdef->name + first + 1, len - first);
+
+             h = elf_link_hash_lookup (elf_hash_table (info), copy,
+                                       FALSE, FALSE, FALSE);
+
+             if (h == NULL)
+               {
+                 /* We also need to check references to the symbol
+                    without the version.  */
+
+                 copy[first - 1] = '\0';
+                 h = elf_link_hash_lookup (elf_hash_table (info),
+                                           copy, FALSE, FALSE, FALSE);
+               }
+
+             bfd_release (abfd, copy);
+           }
+
+         if (h == NULL)
+           continue;
+
+         if (h->root.type == bfd_link_hash_common)
+           {
+             /* We currently have a common symbol.  The archive map contains
+                a reference to this symbol, so we may want to include it.  We
+                only want to include it however, if this archive element
+                contains a definition of the symbol, not just another common
+                declaration of it.
+
+                Unfortunately some archivers (including GNU ar) will put
+                declarations of common symbols into their archive maps, as
+                well as real definitions, so we cannot just go by the archive
+                map alone.  Instead we must read in the element's symbol
+                table and check that to see what kind of symbol definition
+                this is.  */
+             if (! elf_link_is_defined_archive_symbol (abfd, symdef))
+               continue;
+           }
+         else if (h->root.type != bfd_link_hash_undefined)
+           {
+             if (h->root.type != bfd_link_hash_undefweak)
+               defined[i] = TRUE;
+             continue;
+           }
+
+         /* We need to include this archive member.  */
+         element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
+         if (element == NULL)
+           goto error_return;
+
+         if (! bfd_check_format (element, bfd_object))
+           goto error_return;
+
+         /* Doublecheck that we have not included this object
+            already--it should be impossible, but there may be
+            something wrong with the archive.  */
+         if (element->archive_pass != 0)
+           {
+             bfd_set_error (bfd_error_bad_value);
+             goto error_return;
+           }
+         element->archive_pass = 1;
+
+         undefs_tail = info->hash->undefs_tail;
+
+         if (! (*info->callbacks->add_archive_element) (info, element,
+                                                        symdef->name))
+           goto error_return;
+         if (! bfd_link_add_symbols (element, info))
+           goto error_return;
+
+         /* If there are any new undefined symbols, we need to make
+            another pass through the archive in order to see whether
+            they can be defined.  FIXME: This isn't perfect, because
+            common symbols wind up on undefs_tail and because an
+            undefined symbol which is defined later on in this pass
+            does not require another pass.  This isn't a bug, but it
+            does make the code less efficient than it could be.  */
+         if (undefs_tail != info->hash->undefs_tail)
+           loop = TRUE;
+
+         /* Look backward to mark all symbols from this object file
+            which we have already seen in this pass.  */
+         mark = i;
+         do
+           {
+             included[mark] = TRUE;
+             if (mark == 0)
+               break;
+             --mark;
+           }
+         while (symdefs[mark].file_offset == symdef->file_offset);
+
+         /* We mark subsequent symbols from this object file as we go
+            on through the loop.  */
+         last = symdef->file_offset;
+       }
+    }
+  while (loop);
+
+  free (defined);
+  free (included);
+
+  return TRUE;
+
+ error_return:
+  if (defined != NULL)
+    free (defined);
+  if (included != NULL)
+    free (included);
+  return FALSE;
+}