Fix numerous occurrences of
[deliverable/binutils-gdb.git] / bfd / elflink.h
index 47927d3830e46e6468cc415b1c5fe573b8415b8b..23ea241fbdc10e15c6da3c5b5c4049d81bcb2068 100644 (file)
@@ -1,5 +1,5 @@
 /* ELF linker support.
-   Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
+   Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
    Free Software Foundation, Inc.
 
 This file is part of BFD, the Binary File Descriptor library.
@@ -57,8 +57,6 @@ static boolean elf_adjust_dynamic_symbol
   PARAMS ((struct elf_link_hash_entry *, PTR));
 static boolean elf_link_find_version_dependencies
   PARAMS ((struct elf_link_hash_entry *, PTR));
-static boolean elf_link_find_version_dependencies
-  PARAMS ((struct elf_link_hash_entry *, PTR));
 static boolean elf_link_assign_sym_version
   PARAMS ((struct elf_link_hash_entry *, PTR));
 static boolean elf_collect_hash_codes
@@ -67,7 +65,7 @@ static boolean elf_link_read_relocs_from_section
   PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
 static size_t compute_bucket_count
   PARAMS ((struct bfd_link_info *));
-static void elf_link_output_relocs
+static boolean elf_link_output_relocs
   PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
 static boolean elf_link_size_reloc_section
   PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
@@ -151,18 +149,13 @@ elf_link_is_defined_archive_symbol (abfd, symdef)
      carsym * symdef;
 {
   Elf_Internal_Shdr * hdr;
-  Elf_Internal_Shdr * shndx_hdr;
-  Elf_External_Sym *  esym;
-  Elf_External_Sym *  esymend;
-  Elf_External_Sym *  buf = NULL;
-  Elf_External_Sym_Shndx * shndx_buf = NULL;
-  Elf_External_Sym_Shndx * shndx;
   bfd_size_type symcount;
   bfd_size_type extsymcount;
   bfd_size_type extsymoff;
-  boolean result = false;
-  file_ptr pos;
-  bfd_size_type amt;
+  Elf_Internal_Sym *isymbuf;
+  Elf_Internal_Sym *isym;
+  Elf_Internal_Sym *isymend;
+  boolean result;
 
   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
   if (abfd == (bfd *) NULL)
@@ -180,15 +173,9 @@ elf_link_is_defined_archive_symbol (abfd, symdef)
 
   /* Select the appropriate symbol table.  */
   if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
-    {
-      hdr = &elf_tdata (abfd)->symtab_hdr;
-      shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
-    }
+    hdr = &elf_tdata (abfd)->symtab_hdr;
   else
-    {
-      hdr = &elf_tdata (abfd)->dynsymtab_hdr;
-      shndx_hdr = NULL;
-    }
+    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
 
   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
 
@@ -205,58 +192,34 @@ elf_link_is_defined_archive_symbol (abfd, symdef)
       extsymoff = hdr->sh_info;
     }
 
-  amt = extsymcount * sizeof (Elf_External_Sym);
-  buf = (Elf_External_Sym *) bfd_malloc (amt);
-  if (buf == NULL && extsymcount != 0)
+  if (extsymcount == 0)
     return false;
 
-  /* Read in the symbol table.
-     FIXME:  This ought to be cached somewhere.  */
-  pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
-  if (bfd_seek (abfd, pos, SEEK_SET) != 0
-      || bfd_bread ((PTR) buf, amt, abfd) != amt)
-    goto error_exit;
-
-  if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
-    {
-      amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
-      shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
-      if (shndx_buf == NULL && extsymcount != 0)
-       goto error_exit;
-
-      pos = shndx_hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym_Shndx);
-      if (bfd_seek (abfd, pos, SEEK_SET) != 0
-         || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
-       goto error_exit;
-    }
+  /* 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.  */
-  esymend = buf + extsymcount;
-  for (esym = buf, shndx = shndx_buf;
-       esym < esymend;
-       esym++, shndx = (shndx != NULL ? shndx + 1 : NULL))
+  result = false;
+  for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
     {
-      Elf_Internal_Sym sym;
-      const char * name;
-
-      elf_swap_symbol_in (abfd, esym, shndx, &sym);
+      const char *name;
 
-      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
+      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
+                                             isym->st_name);
       if (name == (const char *) NULL)
        break;
 
       if (strcmp (name, symdef->name) == 0)
        {
-         result = is_global_data_symbol_definition (abfd, sym);
+         result = is_global_data_symbol_definition (abfd, isym);
          break;
        }
     }
 
- error_exit:
-  if (shndx_buf != NULL)
-    free (shndx_buf);
-  if (buf != NULL)
-    free (buf);
+  free (isymbuf);
 
   return result;
 }
@@ -315,12 +278,10 @@ elf_link_add_archive_symbols (abfd, info)
     return true;
   amt = c;
   amt *= sizeof (boolean);
-  defined = (boolean *) bfd_malloc (amt);
-  included = (boolean *) bfd_malloc (amt);
+  defined = (boolean *) bfd_zmalloc (amt);
+  included = (boolean *) bfd_zmalloc (amt);
   if (defined == (boolean *) NULL || included == (boolean *) NULL)
     goto error_return;
-  memset (defined, 0, (size_t) amt);
-  memset (included, 0, (size_t) amt);
 
   symdefs = bfd_ardata (abfd)->symdefs;
 
@@ -357,26 +318,40 @@ elf_link_add_archive_symbols (abfd, info)
          if (h == NULL)
            {
              char *p, *copy;
+             size_t len, first;
 
              /* If this is a default version (the name contains @@),
-                look up the symbol again without the version.  The
-                effect is that references to the symbol without the
-                version will be matched by the default symbol in the
-                archive.  */
+                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;
 
-             copy = bfd_alloc (abfd, (bfd_size_type) (p - symdef->name + 1));
+             /* First check with only one `@'.  */
+             len = strlen (symdef->name);
+             copy = bfd_alloc (abfd, (bfd_size_type) len);
              if (copy == NULL)
                goto error_return;
-             memcpy (copy, symdef->name, (size_t) (p - symdef->name));
-             copy[p - symdef->name] = '\0';
+             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);
            }
 
@@ -592,7 +567,7 @@ elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
       asection *hsec;
 
       /* This code handles the special SHN_MIPS_{TEXT,DATA} section
-         indices used by MIPS ELF.  */
+        indices used by MIPS ELF.  */
       switch (h->root.type)
        {
        default:
@@ -702,10 +677,10 @@ elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
       && sym->st_size != h->size)
     {
       /* Since we think we have two common symbols, issue a multiple
-         common warning if desired.  Note that we only warn if the
-         size is different.  If the size is the same, we simply let
-         the old symbol override the new one as normally happens with
-         symbols defined in dynamic objects.  */
+        common warning if desired.  Note that we only warn if the
+        size is different.  If the size is the same, we simply let
+        the old symbol override the new one as normally happens with
+        symbols defined in dynamic objects.  */
 
       if (! ((*info->callbacks->multiple_common)
             (info, h->root.root.string, oldbfd, bfd_link_hash_common,
@@ -755,10 +730,10 @@ elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
       *size_change_ok = true;
 
       /* If we get here when the old symbol is a common symbol, then
-         we are explicitly letting it override a weak symbol or
-         function in a dynamic object, and we don't want to warn about
-         a type change.  If the old symbol is a defined symbol, a type
-         change warning may still be appropriate.  */
+        we are explicitly letting it override a weak symbol or
+        function in a dynamic object, and we don't want to warn about
+        a type change.  If the old symbol is a defined symbol, a type
+        change warning may still be appropriate.  */
 
       if (h->root.type == bfd_link_hash_common)
        *type_change_ok = true;
@@ -817,7 +792,7 @@ elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
       olddyncommon = false;
 
       /* We again permit a type change when a common symbol may be
-         overriding a function.  */
+        overriding a function.  */
 
       if (bfd_is_com_section (sec))
        *type_change_ok = true;
@@ -829,11 +804,11 @@ elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
       h->verinfo.vertree = NULL;
 
       /* In this special case, if H is the target of an indirection,
-         we want the caller to frob with H rather than with the
-         indirect symbol.  That will permit the caller to redefine the
-         target of the indirection, rather than the indirect symbol
-         itself.  FIXME: This will break the -y option if we store a
-         symbol with a different name.  */
+        we want the caller to frob with H rather than with the
+        indirect symbol.  That will permit the caller to redefine the
+        target of the indirection, rather than the indirect symbol
+        itself.  FIXME: This will break the -y option if we store a
+        symbol with a different name.  */
       *sym_hash = h;
     }
 
@@ -856,7 +831,7 @@ elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
        return false;
 
       /* If the predumed common symbol in the dynamic object is
-         larger, pretend that the new symbol has its size.  */
+        larger, pretend that the new symbol has its size.  */
 
       if (h->size > *pvalue)
        *pvalue = h->size;
@@ -890,8 +865,8 @@ elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
       && bind != STB_WEAK)
     {
       /* To make this work we have to frob the flags so that the rest
-         of the code does not think we are using the regular
-         definition.  */
+        of the code does not think we are using the regular
+        definition.  */
       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
        h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
       else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
@@ -900,9 +875,9 @@ elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
                                   | ELF_LINK_HASH_DEF_DYNAMIC);
 
       /* If H is the target of an indirection, we want the caller to
-         use H rather than the indirect symbol.  Otherwise if we are
-         defining a new indirect symbol we will wind up attaching it
-         to the entry we are overriding.  */
+        use H rather than the indirect symbol.  Otherwise if we are
+        defining a new indirect symbol we will wind up attaching it
+        to the entry we are overriding.  */
       *sym_hash = h;
     }
 
@@ -924,19 +899,19 @@ elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
 
 /* This function is called to create an indirect symbol from the
    default for the symbol with the default version if needed. The
-   symbol is described by H, NAME, SYM, SEC, VALUE, and OVERRIDE.  We
+   symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE.  We
    set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
    indicates if it comes from a DT_NEEDED entry of a shared object.  */
 
 static boolean
-elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
+elf_add_default_symbol (abfd, info, h, name, sym, psec, value,
                        dynsym, override, dt_needed)
      bfd *abfd;
      struct bfd_link_info *info;
      struct elf_link_hash_entry *h;
      const char *name;
      Elf_Internal_Sym *sym;
-     asection **sec;
+     asection **psec;
      bfd_vma *value;
      boolean *dynsym;
      boolean override;
@@ -946,10 +921,13 @@ elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
   boolean size_change_ok;
   char *shortname;
   struct elf_link_hash_entry *hi;
+  struct bfd_link_hash_entry *bh;
   struct elf_backend_data *bed;
   boolean collect;
   boolean dynamic;
   char *p;
+  size_t len, shortlen;
+  asection *sec;
 
   /* If this symbol has a version, and it is the default version, we
      create an indirect symbol from the default name to the fully
@@ -962,7 +940,7 @@ elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
   if (override)
     {
       /* We are overridden by an old defition. We need to check if we
-        need to crreate the indirect symbol from the default name.  */
+        need to create the indirect symbol from the default name.  */
       hi = elf_link_hash_lookup (elf_hash_table (info), name, true,
                                 false, false);
       BFD_ASSERT (hi != NULL);
@@ -981,12 +959,12 @@ elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
   collect = bed->collect;
   dynamic = (abfd->flags & DYNAMIC) != 0;
 
-  shortname = bfd_hash_allocate (&info->hash->table,
-                                (size_t) (p - name + 1));
+  shortlen = p - name;
+  shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
   if (shortname == NULL)
     return false;
-  strncpy (shortname, name, (size_t) (p - name));
-  shortname [p - name] = '\0';
+  memcpy (shortname, name, shortlen);
+  shortname[shortlen] = '\0';
 
   /* We are going to create a new symbol.  Merge it with any existing
      symbol with this name.  For the purposes of the merge, act as
@@ -994,18 +972,20 @@ elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
      actually going to define an indirect symbol.  */
   type_change_ok = false;
   size_change_ok = false;
-  if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
+  sec = *psec;
+  if (! elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
                          &hi, &override, &type_change_ok,
                          &size_change_ok, dt_needed))
     return false;
 
   if (! override)
     {
+      bh = &hi->root;
       if (! (_bfd_generic_link_add_one_symbol
             (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
-             (bfd_vma) 0, name, false, collect,
-             (struct bfd_link_hash_entry **) &hi)))
+             (bfd_vma) 0, name, false, collect, &bh)))
        return false;
+      hi = (struct elf_link_hash_entry *) bh;
     }
   else
     {
@@ -1047,7 +1027,7 @@ elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
        }
 
       /* Now set HI to H, so that the following code will set the
-         other fields correctly.  */
+        other fields correctly.  */
       hi = h;
     }
 
@@ -1066,7 +1046,7 @@ elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
                      | ELF_LINK_HASH_DEF_REGULAR)) == 0);
 
       ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
-      (*bed->elf_backend_copy_indirect_symbol) (ht, hi);
+      (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
 
       /* See if the new flags lead us to realize that the symbol must
         be dynamic.  */
@@ -1091,16 +1071,18 @@ elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
   /* We also need to define an indirection from the nondefault version
      of the symbol.  */
 
-  shortname = bfd_hash_allocate (&info->hash->table, strlen (name));
+  len = strlen (name);
+  shortname = bfd_hash_allocate (&info->hash->table, len);
   if (shortname == NULL)
     return false;
-  strncpy (shortname, name, (size_t) (p - name));
-  strcpy (shortname + (p - name), p + 1);
+  memcpy (shortname, name, shortlen);
+  memcpy (shortname + shortlen, p + 1, len - shortlen);
 
   /* Once again, merge with any existing symbol.  */
   type_change_ok = false;
   size_change_ok = false;
-  if (! elf_merge_symbol (abfd, info, shortname, sym, sec, value,
+  sec = *psec;
+  if (! elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
                          &hi, &override, &type_change_ok,
                          &size_change_ok, dt_needed))
     return false;
@@ -1108,18 +1090,22 @@ elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
   if (override)
     {
       /* Here SHORTNAME is a versioned name, so we don't expect to see
-        the type of override we do in the case above.  */
-      (*_bfd_error_handler)
-       (_("%s: warning: unexpected redefinition of `%s'"),
-        bfd_archive_filename (abfd), shortname);
+        the type of override we do in the case above unless it is
+        overridden by a versioned definiton.  */
+      if (hi->root.type != bfd_link_hash_defined
+         && hi->root.type != bfd_link_hash_defweak)
+       (*_bfd_error_handler)
+         (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
+          bfd_archive_filename (abfd), shortname);
     }
   else
     {
+      bh = &hi->root;
       if (! (_bfd_generic_link_add_one_symbol
             (info, abfd, shortname, BSF_INDIRECT,
-             bfd_ind_section_ptr, (bfd_vma) 0, name, false,
-             collect, (struct bfd_link_hash_entry **) &hi)))
+             bfd_ind_section_ptr, (bfd_vma) 0, name, false, collect, &bh)))
        return false;
+      hi = (struct elf_link_hash_entry *) bh;
 
       /* If there is a duplicate definition somewhere, then HI may not
         point to an indirect symbol.  We will have reported an error
@@ -1133,7 +1119,7 @@ elf_add_default_symbol (abfd, info, h, name, sym, sec, value,
                       & (ELF_LINK_HASH_DEF_DYNAMIC
                          | ELF_LINK_HASH_DEF_REGULAR)) == 0);
 
-          (*bed->elf_backend_copy_indirect_symbol) (h, hi);
+         (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
 
          /* See if the new flags lead us to realize that the symbol
             must be dynamic.  */
@@ -1174,25 +1160,20 @@ elf_link_add_object_symbols (abfd, info)
                                   asection *, const Elf_Internal_Rela *));
   boolean collect;
   Elf_Internal_Shdr *hdr;
-  Elf_Internal_Shdr *shndx_hdr;
   bfd_size_type symcount;
   bfd_size_type extsymcount;
   bfd_size_type extsymoff;
-  Elf_External_Sym *buf = NULL;
-  Elf_External_Sym_Shndx *shndx_buf = NULL;
-  Elf_External_Sym_Shndx *shndx;
   struct elf_link_hash_entry **sym_hash;
   boolean dynamic;
   Elf_External_Versym *extversym = NULL;
   Elf_External_Versym *ever;
-  Elf_External_Dyn *dynbuf = NULL;
   struct elf_link_hash_entry *weaks;
-  Elf_External_Sym *esym;
-  Elf_External_Sym *esymend;
+  Elf_Internal_Sym *isymbuf = NULL;
+  Elf_Internal_Sym *isym;
+  Elf_Internal_Sym *isymend;
   struct elf_backend_data *bed;
   boolean dt_needed;
   struct elf_link_hash_table * hash_table;
-  file_ptr pos;
   bfd_size_type amt;
 
   hash_table = elf_hash_table (info);
@@ -1259,8 +1240,8 @@ elf_link_add_object_symbols (abfd, info)
                          || h->root.type == bfd_link_hash_defweak))
                    {
                      /* We don't want to issue this warning.  Clobber
-                         the section size so that the warning does not
-                         get copied into the output file.  */
+                        the section size so that the warning does not
+                        get copied into the output file.  */
                      s->_raw_size = 0;
                      continue;
                    }
@@ -1284,99 +1265,21 @@ elf_link_add_object_symbols (abfd, info)
              if (! info->relocateable)
                {
                  /* Clobber the section size so that the warning does
-                     not get copied into the output file.  */
+                    not get copied into the output file.  */
                  s->_raw_size = 0;
                }
            }
        }
     }
 
-  /* If this is a dynamic object, we always link against the .dynsym
-     symbol table, not the .symtab symbol table.  The dynamic linker
-     will only see the .dynsym symbol table, so there is no reason to
-     look at .symtab for a dynamic object.  */
-
-  if (! dynamic || elf_dynsymtab (abfd) == 0)
-    {
-      hdr = &elf_tdata (abfd)->symtab_hdr;
-      shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
-    }
-  else
-    {
-      hdr = &elf_tdata (abfd)->dynsymtab_hdr;
-      shndx_hdr = NULL;
-    }
-
-  if (dynamic)
-    {
-      /* Read in any version definitions.  */
-
-      if (! _bfd_elf_slurp_version_tables (abfd))
-       goto error_return;
-
-      /* Read in the symbol versions, but don't bother to convert them
-         to internal format.  */
-      if (elf_dynversym (abfd) != 0)
-       {
-         Elf_Internal_Shdr *versymhdr;
-
-         versymhdr = &elf_tdata (abfd)->dynversym_hdr;
-         extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
-         if (extversym == NULL)
-           goto error_return;
-         amt = versymhdr->sh_size;
-         if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
-             || bfd_bread ((PTR) extversym, amt, abfd) != amt)
-           goto error_return;
-       }
-    }
-
-  symcount = hdr->sh_size / sizeof (Elf_External_Sym);
-
-  /* The sh_info field of the symtab header tells us where the
-     external symbols start.  We don't care about the local symbols at
-     this point.  */
-  if (elf_bad_symtab (abfd))
-    {
-      extsymcount = symcount;
-      extsymoff = 0;
-    }
-  else
-    {
-      extsymcount = symcount - hdr->sh_info;
-      extsymoff = hdr->sh_info;
-    }
-
-  amt = extsymcount * sizeof (Elf_External_Sym);
-  buf = (Elf_External_Sym *) bfd_malloc (amt);
-  if (buf == NULL && extsymcount != 0)
-    goto error_return;
-
-  if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
-    {
-      amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
-      shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
-      if (shndx_buf == NULL && extsymcount != 0)
-       goto error_return;
-    }
-
-  /* We store a pointer to the hash table entry for each external
-     symbol.  */
-  amt = extsymcount * sizeof (struct elf_link_hash_entry *);
-  sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
-  if (sym_hash == NULL)
-    goto error_return;
-  elf_sym_hashes (abfd) = sym_hash;
-
   dt_needed = false;
-
   if (! dynamic)
     {
       /* If we are creating a shared library, create all the dynamic
-         sections immediately.  We need to attach them to something,
-         so we attach them to this BFD, provided it is the right
-         format.  FIXME: If there are no input BFD's of the same
-         format as the output, we can't make a shared library.  */
+        sections immediately.  We need to attach them to something,
+        so we attach them to this BFD, provided it is the right
+        format.  FIXME: If there are no input BFD's of the same
+        format as the output, we can't make a shared library.  */
       if (info->shared
          && is_elf_hash_table (info)
          && ! hash_table->dynamic_sections_created
@@ -1411,7 +1314,7 @@ elf_link_add_object_symbols (abfd, info)
          if (*name == '\0')
            {
              if (elf_dt_soname (abfd) != NULL)
-               dt_needed = true;
+               dt_needed = true;
 
              add_needed = false;
            }
@@ -1419,6 +1322,7 @@ elf_link_add_object_symbols (abfd, info)
       s = bfd_get_section_by_name (abfd, ".dynamic");
       if (s != NULL)
        {
+         Elf_External_Dyn *dynbuf = NULL;
          Elf_External_Dyn *extdyn;
          Elf_External_Dyn *extdynend;
          int elfsec;
@@ -1432,30 +1336,13 @@ elf_link_add_object_symbols (abfd, info)
 
          if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
                                          (file_ptr) 0, s->_raw_size))
-           goto error_return;
+           goto error_free_dyn;
 
          elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
          if (elfsec == -1)
-           goto error_return;
+           goto error_free_dyn;
          shlink = elf_elfsections (abfd)[elfsec]->sh_link;
 
-         {
-           /* The shared libraries distributed with hpux11 have a bogus
-              sh_link field for the ".dynamic" section.  This code detects
-              when SHLINK refers to a section that is not a string table
-              and tries to find the string table for the ".dynsym" section
-              instead.  */
-           Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[shlink];
-           if (shdr->sh_type != SHT_STRTAB)
-             {
-               asection *ds = bfd_get_section_by_name (abfd, ".dynsym");
-               int elfdsec = _bfd_elf_section_from_bfd_section (abfd, ds);
-               if (elfdsec == -1)
-                 goto error_return;
-               shlink = elf_elfsections (abfd)[elfdsec]->sh_link;
-             }
-         }
-
          extdyn = dynbuf;
          extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
          rpath = 0;
@@ -1470,7 +1357,7 @@ elf_link_add_object_symbols (abfd, info)
                  unsigned int tagv = dyn.d_un.d_val;
                  name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
                  if (name == NULL)
-                   goto error_return;
+                   goto error_free_dyn;
                }
              if (dyn.d_tag == DT_NEEDED)
                {
@@ -1482,11 +1369,12 @@ elf_link_add_object_symbols (abfd, info)
                  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
                  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
                  if (n == NULL || fnm == NULL)
-                   goto error_return;
-                 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
+                   goto error_free_dyn;
+                 amt = strlen (fnm) + 1;
+                 anm = bfd_alloc (abfd, amt);
                  if (anm == NULL)
-                   goto error_return;
-                 strcpy (anm, fnm);
+                   goto error_free_dyn;
+                 memcpy (anm, fnm, (size_t) amt);
                  n->name = anm;
                  n->by = abfd;
                  n->next = NULL;
@@ -1513,11 +1401,12 @@ elf_link_add_object_symbols (abfd, info)
                  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
                  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
                  if (n == NULL || fnm == NULL)
-                   goto error_return;
-                 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
+                   goto error_free_dyn;
+                 amt = strlen (fnm) + 1;
+                 anm = bfd_alloc (abfd, amt);
                  if (anm == NULL)
-                   goto error_return;
-                 strcpy (anm, fnm);
+                   goto error_free_dyn;
+                 memcpy (anm, fnm, (size_t) amt);
                  n->name = anm;
                  n->by = abfd;
                  n->next = NULL;
@@ -1531,7 +1420,7 @@ elf_link_add_object_symbols (abfd, info)
                }
              /* Ignore DT_RPATH if we have seen DT_RUNPATH.  */
              if (!runpath && dyn.d_tag == DT_RPATH)
-               {
+               {
                  struct bfd_link_needed_list *n, **pn;
                  char *fnm, *anm;
                  unsigned int tagv = dyn.d_un.d_val;
@@ -1540,11 +1429,16 @@ elf_link_add_object_symbols (abfd, info)
                  n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
                  fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
                  if (n == NULL || fnm == NULL)
-                   goto error_return;
-                 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
+                   goto error_free_dyn;
+                 amt = strlen (fnm) + 1;
+                 anm = bfd_alloc (abfd, amt);
                  if (anm == NULL)
-                   goto error_return;
-                 strcpy (anm, fnm);
+                   {
+                   error_free_dyn:
+                     free (dynbuf);
+                     goto error_return;
+                   }
+                 memcpy (anm, fnm, (size_t) amt);
                  n->name = anm;
                  n->by = abfd;
                  n->next = NULL;
@@ -1558,7 +1452,6 @@ elf_link_add_object_symbols (abfd, info)
            }
 
          free (dynbuf);
-         dynbuf = NULL;
        }
 
       /* We do not want to include any of the sections in a dynamic
@@ -1608,10 +1501,6 @@ elf_link_add_object_symbols (abfd, info)
                  if (dyn.d_tag == DT_NEEDED
                      && dyn.d_un.d_val == strindex)
                    {
-                     if (buf != NULL)
-                       free (buf);
-                     if (extversym != NULL)
-                       free (extversym);
                      _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
                      return true;
                    }
@@ -1623,37 +1512,85 @@ elf_link_add_object_symbols (abfd, info)
        }
 
       /* Save the SONAME, if there is one, because sometimes the
-         linker emulation code will need to know it.  */
+        linker emulation code will need to know it.  */
       if (*name == '\0')
        name = basename (bfd_get_filename (abfd));
       elf_dt_name (abfd) = name;
     }
 
-  pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
-  amt = extsymcount * sizeof (Elf_External_Sym);
-  if (bfd_seek (abfd, pos, SEEK_SET) != 0
-      || bfd_bread ((PTR) buf, amt, abfd) != amt)
-    goto error_return;
+  /* If this is a dynamic object, we always link against the .dynsym
+     symbol table, not the .symtab symbol table.  The dynamic linker
+     will only see the .dynsym symbol table, so there is no reason to
+     look at .symtab for a dynamic object.  */
+
+  if (! dynamic || elf_dynsymtab (abfd) == 0)
+    hdr = &elf_tdata (abfd)->symtab_hdr;
+  else
+    hdr = &elf_tdata (abfd)->dynsymtab_hdr;
+
+  symcount = hdr->sh_size / sizeof (Elf_External_Sym);
+
+  /* The sh_info field of the symtab header tells us where the
+     external symbols start.  We don't care about the local symbols at
+     this point.  */
+  if (elf_bad_symtab (abfd))
+    {
+      extsymcount = symcount;
+      extsymoff = 0;
+    }
+  else
+    {
+      extsymcount = symcount - hdr->sh_info;
+      extsymoff = hdr->sh_info;
+    }
 
-  if (shndx_hdr != NULL && shndx_hdr->sh_size != 0)
+  sym_hash = NULL;
+  if (extsymcount != 0)
     {
-      amt = extsymcount * sizeof (Elf_External_Sym_Shndx);
-      pos = shndx_hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym_Shndx);
-      if (bfd_seek (abfd, pos, SEEK_SET) != 0
-         || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
+      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
+                                     NULL, NULL, NULL);
+      if (isymbuf == NULL)
        goto error_return;
+
+      /* We store a pointer to the hash table entry for each external
+        symbol.  */
+      amt = extsymcount * sizeof (struct elf_link_hash_entry *);
+      sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
+      if (sym_hash == NULL)
+       goto error_free_sym;
+      elf_sym_hashes (abfd) = sym_hash;
+    }
+
+  if (dynamic)
+    {
+      /* Read in any version definitions.  */
+      if (! _bfd_elf_slurp_version_tables (abfd))
+       goto error_free_sym;
+
+      /* Read in the symbol versions, but don't bother to convert them
+        to internal format.  */
+      if (elf_dynversym (abfd) != 0)
+       {
+         Elf_Internal_Shdr *versymhdr;
+
+         versymhdr = &elf_tdata (abfd)->dynversym_hdr;
+         extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
+         if (extversym == NULL)
+           goto error_free_sym;
+         amt = versymhdr->sh_size;
+         if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
+             || bfd_bread ((PTR) extversym, amt, abfd) != amt)
+           goto error_free_vers;
+       }
     }
 
   weaks = NULL;
 
   ever = extversym != NULL ? extversym + extsymoff : NULL;
-  esymend = buf + extsymcount;
-  for (esym = buf, shndx = shndx_buf;
-       esym < esymend;
-       esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL),
-        shndx = (shndx != NULL ? shndx + 1 : NULL))
+  for (isym = isymbuf, isymend = isymbuf + extsymcount;
+       isym < isymend;
+       isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
     {
-      Elf_Internal_Sym sym;
       int bind;
       bfd_vma value;
       asection *sec;
@@ -1668,14 +1605,12 @@ elf_link_add_object_symbols (abfd, info)
 
       override = false;
 
-      elf_swap_symbol_in (abfd, esym, shndx, &sym);
-
       flags = BSF_NO_FLAGS;
       sec = NULL;
-      value = sym.st_value;
+      value = isym->st_value;
       *sym_hash = NULL;
 
-      bind = ELF_ST_BIND (sym.st_info);
+      bind = ELF_ST_BIND (isym->st_info);
       if (bind == STB_LOCAL)
        {
          /* This should be impossible, since ELF requires that all
@@ -1686,8 +1621,8 @@ elf_link_add_object_symbols (abfd, info)
        }
       else if (bind == STB_GLOBAL)
        {
-         if (sym.st_shndx != SHN_UNDEF
-             && sym.st_shndx != SHN_COMMON)
+         if (isym->st_shndx != SHN_UNDEF
+             && isym->st_shndx != SHN_COMMON)
            flags = BSF_GLOBAL;
        }
       else if (bind == STB_WEAK)
@@ -1697,39 +1632,57 @@ elf_link_add_object_symbols (abfd, info)
          /* Leave it up to the processor backend.  */
        }
 
-      if (sym.st_shndx == SHN_UNDEF)
+      if (isym->st_shndx == SHN_UNDEF)
        sec = bfd_und_section_ptr;
-      else if (sym.st_shndx < SHN_LORESERVE || sym.st_shndx > SHN_HIRESERVE)
+      else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
        {
-         sec = section_from_elf_index (abfd, sym.st_shndx);
+         sec = section_from_elf_index (abfd, isym->st_shndx);
          if (sec == NULL)
            sec = bfd_abs_section_ptr;
          else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
            value -= sec->vma;
        }
-      else if (sym.st_shndx == SHN_ABS)
+      else if (isym->st_shndx == SHN_ABS)
        sec = bfd_abs_section_ptr;
-      else if (sym.st_shndx == SHN_COMMON)
+      else if (isym->st_shndx == SHN_COMMON)
        {
          sec = bfd_com_section_ptr;
          /* What ELF calls the size we call the value.  What ELF
             calls the value we call the alignment.  */
-         value = sym.st_size;
+         value = isym->st_size;
        }
       else
        {
          /* Leave it up to the processor backend.  */
        }
 
-      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
+      name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
+                                             isym->st_name);
       if (name == (const char *) NULL)
-       goto error_return;
+       goto error_free_vers;
 
-      if (add_symbol_hook)
+      if (isym->st_shndx == SHN_COMMON
+         && ELF_ST_TYPE (isym->st_info) == STT_TLS)
        {
-         if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
+         asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
+
+         if (tcomm == NULL)
+           {
+             tcomm = bfd_make_section (abfd, ".tcommon");
+             if (tcomm == NULL
+                 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
+                                                          | SEC_IS_COMMON
+                                                          | SEC_LINKER_CREATED
+                                                          | SEC_THREAD_LOCAL)))
+               goto error_free_vers;
+           }
+         sec = tcomm;
+       }
+      else if (add_symbol_hook)
+       {
+         if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
                                    &value))
-           goto error_return;
+           goto error_free_vers;
 
          /* The hook function sets the name to NULL if this symbol
             should be skipped for some reason.  */
@@ -1741,7 +1694,7 @@ elf_link_add_object_symbols (abfd, info)
       if (sec == (asection *) NULL)
        {
          bfd_set_error (bfd_error_bad_value);
-         goto error_return;
+         goto error_free_vers;
        }
 
       if (bfd_is_und_section (sec)
@@ -1764,19 +1717,18 @@ elf_link_add_object_symbols (abfd, info)
              vernum = iver.vs_vers & VERSYM_VERSION;
 
              /* If this is a hidden symbol, or if it is not version
-                 1, we append the version name to the symbol name.
-                 However, we do not modify a non-hidden absolute
-                 symbol, because it might be the version symbol
-                 itself.  FIXME: What if it isn't?  */
+                1, we append the version name to the symbol name.
+                However, we do not modify a non-hidden absolute
+                symbol, because it might be the version symbol
+                itself.  FIXME: What if it isn't?  */
              if ((iver.vs_vers & VERSYM_HIDDEN) != 0
                  || (vernum > 1 && ! bfd_is_abs_section (sec)))
                {
                  const char *verstr;
-                 unsigned int namelen;
-                 bfd_size_type newlen;
+                 size_t namelen, verlen, newlen;
                  char *newname, *p;
 
-                 if (sym.st_shndx != SHN_UNDEF)
+                 if (isym->st_shndx != SHN_UNDEF)
                    {
                      if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
                        {
@@ -1785,7 +1737,7 @@ elf_link_add_object_symbols (abfd, info)
                             bfd_archive_filename (abfd), name, vernum,
                             elf_tdata (abfd)->dynverdef_hdr.sh_info);
                          bfd_set_error (bfd_error_bad_value);
-                         goto error_return;
+                         goto error_free_vers;
                        }
                      else if (vernum > 1)
                        verstr =
@@ -1825,37 +1777,39 @@ elf_link_add_object_symbols (abfd, info)
                            (_("%s: %s: invalid needed version %d"),
                             bfd_archive_filename (abfd), name, vernum);
                          bfd_set_error (bfd_error_bad_value);
-                         goto error_return;
+                         goto error_free_vers;
                        }
                    }
 
                  namelen = strlen (name);
-                 newlen = namelen + strlen (verstr) + 2;
-                 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
+                 verlen = strlen (verstr);
+                 newlen = namelen + verlen + 2;
+                 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
+                     && isym->st_shndx != SHN_UNDEF)
                    ++newlen;
 
-                 newname = (char *) bfd_alloc (abfd, newlen);
+                 newname = (char *) bfd_alloc (abfd, (bfd_size_type) newlen);
                  if (newname == NULL)
-                   goto error_return;
-                 strcpy (newname, name);
+                   goto error_free_vers;
+                 memcpy (newname, name, namelen);
                  p = newname + namelen;
                  *p++ = ELF_VER_CHR;
                  /* If this is a defined non-hidden version symbol,
                     we add another @ to the name.  This indicates the
                     default version of the symbol.  */
                  if ((iver.vs_vers & VERSYM_HIDDEN) == 0
-                     && sym.st_shndx != SHN_UNDEF)
+                     && isym->st_shndx != SHN_UNDEF)
                    *p++ = ELF_VER_CHR;
-                 strcpy (p, verstr);
+                 memcpy (p, verstr, verlen + 1);
 
                  name = newname;
                }
            }
 
-         if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
+         if (! elf_merge_symbol (abfd, info, name, isym, &sec, &value,
                                  sym_hash, &override, &type_change_ok,
                                  &size_change_ok, dt_needed))
-           goto error_return;
+           goto error_free_vers;
 
          if (override)
            definition = false;
@@ -1866,10 +1820,10 @@ elf_link_add_object_symbols (abfd, info)
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
 
          /* Remember the old alignment if this is a common symbol, so
-             that we don't reduce the alignment later on.  We can't
-             check later, because _bfd_generic_link_add_one_symbol
-             will set a default for the alignment which we want to
-             override.  */
+            that we don't reduce the alignment later on.  We can't
+            check later, because _bfd_generic_link_add_one_symbol
+            will set a default for the alignment which we want to
+            override.  */
          if (h->root.type == bfd_link_hash_common)
            old_alignment = h->root.u.c.p->alignment_power;
 
@@ -1883,7 +1837,7 @@ elf_link_add_object_symbols (abfd, info)
       if (! (_bfd_generic_link_add_one_symbol
             (info, abfd, name, flags, sec, value, (const char *) NULL,
              false, collect, (struct bfd_link_hash_entry **) sym_hash)))
-       goto error_return;
+       goto error_free_vers;
 
       h = *sym_hash;
       while (h->root.type == bfd_link_hash_indirect
@@ -1895,7 +1849,7 @@ elf_link_add_object_symbols (abfd, info)
       if (dynamic
          && definition
          && (flags & BSF_WEAK) != 0
-         && ELF_ST_TYPE (sym.st_info) != STT_FUNC
+         && ELF_ST_TYPE (isym->st_info) != STT_FUNC
          && info->hash->creator->flavour == bfd_target_elf_flavour
          && h->weakdef == NULL)
        {
@@ -1917,16 +1871,16 @@ elf_link_add_object_symbols (abfd, info)
        }
 
       /* Set the alignment of a common symbol.  */
-      if (sym.st_shndx == SHN_COMMON
+      if (isym->st_shndx == SHN_COMMON
          && h->root.type == bfd_link_hash_common)
        {
          unsigned int align;
 
-         align = bfd_log2 (sym.st_value);
+         align = bfd_log2 (isym->st_value);
          if (align > old_alignment
              /* Permit an alignment power of zero if an alignment of one
                 is specified and no other alignments have been specified.  */
-             || (sym.st_value == 1 && old_alignment == 0))
+             || (isym->st_value == 1 && old_alignment == 0))
            h->root.u.c.p->alignment_power = align;
        }
 
@@ -1937,57 +1891,57 @@ elf_link_add_object_symbols (abfd, info)
          int new_flag;
 
          /* Remember the symbol size and type.  */
-         if (sym.st_size != 0
+         if (isym->st_size != 0
              && (definition || h->size == 0))
            {
-             if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
+             if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
                (*_bfd_error_handler)
                  (_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
-                  name, (unsigned long) h->size, (unsigned long) sym.st_size,
-                  bfd_archive_filename (abfd));
+                  name, (unsigned long) h->size,
+                  (unsigned long) isym->st_size, bfd_archive_filename (abfd));
 
-             h->size = sym.st_size;
+             h->size = isym->st_size;
            }
 
          /* If this is a common symbol, then we always want H->SIZE
-             to be the size of the common symbol.  The code just above
-             won't fix the size if a common symbol becomes larger.  We
-             don't warn about a size change here, because that is
-             covered by --warn-common.  */
+            to be the size of the common symbol.  The code just above
+            won't fix the size if a common symbol becomes larger.  We
+            don't warn about a size change here, because that is
+            covered by --warn-common.  */
          if (h->root.type == bfd_link_hash_common)
            h->size = h->root.u.c.size;
 
-         if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
+         if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
              && (definition || h->type == STT_NOTYPE))
            {
              if (h->type != STT_NOTYPE
-                 && h->type != ELF_ST_TYPE (sym.st_info)
+                 && h->type != ELF_ST_TYPE (isym->st_info)
                  && ! type_change_ok)
                (*_bfd_error_handler)
                  (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
-                  name, h->type, ELF_ST_TYPE (sym.st_info),
+                  name, h->type, ELF_ST_TYPE (isym->st_info),
                   bfd_archive_filename (abfd));
 
-             h->type = ELF_ST_TYPE (sym.st_info);
+             h->type = ELF_ST_TYPE (isym->st_info);
            }
 
          /* If st_other has a processor-specific meaning, specific code
             might be needed here.  */
-         if (sym.st_other != 0)
+         if (isym->st_other != 0)
            {
              /* Combine visibilities, using the most constraining one.  */
              unsigned char hvis   = ELF_ST_VISIBILITY (h->other);
-             unsigned char symvis = ELF_ST_VISIBILITY (sym.st_other);
+             unsigned char symvis = ELF_ST_VISIBILITY (isym->st_other);
 
              if (symvis && (hvis > symvis || hvis == 0))
-               h->other = sym.st_other;
+               h->other = isym->st_other;
 
              /* If neither has visibility, use the st_other of the
-                definition.  This is an arbitrary choice, since the
-                other bits have no general meaning.  */
+                definition.  This is an arbitrary choice, since the
+                other bits have no general meaning.  */
              if (!symvis && !hvis
                  && (definition || h->other == 0))
-               h->other = sym.st_other;
+               h->other = isym->st_other;
            }
 
          /* Set a flag in the hash table entry indicating the type of
@@ -2031,21 +1985,21 @@ elf_link_add_object_symbols (abfd, info)
          /* Check to see if we need to add an indirect symbol for
             the default name.  */
          if (definition || h->root.type == bfd_link_hash_common)
-           if (! elf_add_default_symbol (abfd, info, h, name, &sym,
+           if (! elf_add_default_symbol (abfd, info, h, name, isym,
                                          &sec, &value, &dynsym,
                                          override, dt_needed))
-             goto error_return;
+             goto error_free_vers;
 
          if (dynsym && h->dynindx == -1)
            {
              if (! _bfd_elf_link_record_dynamic_symbol (info, h))
-               goto error_return;
+               goto error_free_vers;
              if (h->weakdef != NULL
                  && ! new_weakdef
                  && h->weakdef->dynindx == -1)
                {
                  if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
-                   goto error_return;
+                   goto error_free_vers;
                }
            }
          else if (dynsym && h->dynindx != -1)
@@ -2068,10 +2022,10 @@ elf_link_add_object_symbols (abfd, info)
              bfd_size_type strindex;
 
              if (! is_elf_hash_table (info))
-               goto error_return;
+               goto error_free_vers;
 
              /* The symbol from a DT_NEEDED object is referenced from
-                the regular object to create a dynamic executable. We
+                the regular object to create a dynamic executable. We
                 have to make sure there is a DT_NEEDED entry for it.  */
 
              dt_needed = false;
@@ -2079,7 +2033,7 @@ elf_link_add_object_symbols (abfd, info)
              strindex = _bfd_elf_strtab_add (hash_table->dynstr,
                                              elf_dt_soname (abfd), false);
              if (strindex == (bfd_size_type) -1)
-               goto error_return;
+               goto error_free_vers;
 
              if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
                {
@@ -2105,11 +2059,21 @@ elf_link_add_object_symbols (abfd, info)
                }
 
              if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
-               goto error_return;
+               goto error_free_vers;
            }
        }
     }
 
+  if (extversym != NULL)
+    {
+      free (extversym);
+      extversym = NULL;
+    }
+
+  if (isymbuf != NULL)
+    free (isymbuf);
+  isymbuf = NULL;
+
   /* Now set the weakdefs field correctly for all the weak defined
      symbols we found.  The only way to do this is to search all the
      symbols.  Since we only need the information for non functions in
@@ -2167,34 +2131,21 @@ elf_link_add_object_symbols (abfd, info)
                }
 
              /* If the real definition is in the list of dynamic
-                 symbols, make sure the weak definition is put there
-                 as well.  If we don't do this, then the dynamic
-                 loader might not merge the entries for the real
-                 definition and the weak definition.  */
+                symbols, make sure the weak definition is put there
+                as well.  If we don't do this, then the dynamic
+                loader might not merge the entries for the real
+                definition and the weak definition.  */
              if (h->dynindx != -1
                  && hlook->dynindx == -1)
                {
                  if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
                    goto error_return;
                }
-
              break;
            }
        }
     }
 
-  if (buf != NULL)
-    {
-      free (buf);
-      buf = NULL;
-    }
-
-  if (extversym != NULL)
-    {
-      free (extversym);
-      extversym = NULL;
-    }
-
   /* If this object is the same format as the output object, and it is
      not a shared library, then let the backend look through the
      relocs.
@@ -2240,7 +2191,7 @@ elf_link_add_object_symbols (abfd, info)
 
          ok = (*check_relocs) (abfd, info, o, internal_relocs);
 
-         if (! info->keep_memory)
+         if (elf_section_data (o)->relocs != internal_relocs)
            free (internal_relocs);
 
          if (! ok)
@@ -2260,7 +2211,9 @@ elf_link_add_object_symbols (abfd, info)
       asection *stab, *stabstr;
 
       stab = bfd_get_section_by_name (abfd, ".stab");
-      if (stab != NULL && !(stab->flags & SEC_MERGE))
+      if (stab != NULL
+         && (stab->flags & SEC_MERGE) == 0
+         && !bfd_is_abs_section (stab->output_section))
        {
          stabstr = bfd_get_section_by_name (abfd, ".stabstr");
 
@@ -2286,7 +2239,8 @@ elf_link_add_object_symbols (abfd, info)
       asection *s;
 
       for (s = abfd->sections; s != NULL; s = s->next)
-       if (s->flags & SEC_MERGE)
+       if ((s->flags & SEC_MERGE) != 0
+           && !bfd_is_abs_section (s->output_section))
          {
            struct bfd_elf_section_data *secdata;
 
@@ -2300,15 +2254,29 @@ elf_link_add_object_symbols (abfd, info)
          }
     }
 
+  if (is_elf_hash_table (info))
+    {
+      /* Add this bfd to the loaded list.  */
+      struct elf_link_loaded_list *n;
+
+      n = ((struct elf_link_loaded_list *)
+          bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)));
+      if (n == NULL)
+       goto error_return;
+      n->abfd = abfd;
+      n->next = hash_table->loaded;
+      hash_table->loaded = n;
+    }
+
   return true;
 
- error_return:
-  if (buf != NULL)
-    free (buf);
-  if (dynbuf != NULL)
-    free (dynbuf);
+ error_free_vers:
   if (extversym != NULL)
     free (extversym);
+ error_free_sym:
+  if (isymbuf != NULL)
+    free (isymbuf);
+ error_return:
   return false;
 }
 
@@ -2327,6 +2295,7 @@ elf_link_create_dynamic_sections (abfd, info)
   flagword flags;
   register asection *s;
   struct elf_link_hash_entry *h;
+  struct bfd_link_hash_entry *bh;
   struct elf_backend_data *bed;
 
   if (! is_elf_hash_table (info))
@@ -2419,12 +2388,12 @@ elf_link_create_dynamic_sections (abfd, info)
      creating a .dynamic section.  We don't want to define it if there
      is no .dynamic section, since on some ELF platforms the start up
      code examines it to decide how to initialize the process.  */
-  h = NULL;
+  bh = NULL;
   if (! (_bfd_generic_link_add_one_symbol
         (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
-         (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
-         (struct bfd_link_hash_entry **) &h)))
+         (const char *) 0, false, get_elf_backend_data (abfd)->collect, &bh)))
     return false;
+  h = (struct elf_link_hash_entry *) bh;
   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
   h->type = STT_OBJECT;
 
@@ -2489,92 +2458,6 @@ elf_add_dynamic_entry (info, tag, val)
 
   return true;
 }
-
-/* Record a new local dynamic symbol.  */
-
-boolean
-elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
-     struct bfd_link_info *info;
-     bfd *input_bfd;
-     long input_indx;
-{
-  struct elf_link_local_dynamic_entry *entry;
-  struct elf_link_hash_table *eht;
-  struct elf_strtab_hash *dynstr;
-  Elf_External_Sym esym;
-  Elf_External_Sym_Shndx eshndx;
-  Elf_External_Sym_Shndx *shndx;
-  unsigned long dynstr_index;
-  char *name;
-  file_ptr pos;
-  bfd_size_type amt;
-
-  if (! is_elf_hash_table (info))
-    return false;
-
-  /* See if the entry exists already.  */
-  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
-    if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
-      return true;
-
-  entry = (struct elf_link_local_dynamic_entry *)
-    bfd_alloc (input_bfd, (bfd_size_type) sizeof (*entry));
-  if (entry == NULL)
-    return false;
-
-  /* Go find the symbol, so that we can find it's name.  */
-  amt = sizeof (Elf_External_Sym);
-  pos = elf_tdata (input_bfd)->symtab_hdr.sh_offset + input_indx * amt;
-  if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
-      || bfd_bread ((PTR) &esym, amt, input_bfd) != amt)
-    return false;
-  shndx = NULL;
-  if (elf_tdata (input_bfd)->symtab_shndx_hdr.sh_size != 0)
-    {
-      amt = sizeof (Elf_External_Sym_Shndx);
-      pos = elf_tdata (input_bfd)->symtab_shndx_hdr.sh_offset;
-      pos += input_indx * amt;
-      shndx = &eshndx;
-      if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
-         || bfd_bread ((PTR) shndx, amt, input_bfd) != amt)
-       return false;
-    }
-  elf_swap_symbol_in (input_bfd, &esym, shndx, &entry->isym);
-
-  name = (bfd_elf_string_from_elf_section
-         (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
-          entry->isym.st_name));
-
-  dynstr = elf_hash_table (info)->dynstr;
-  if (dynstr == NULL)
-    {
-      /* Create a strtab to hold the dynamic symbol names.  */
-      elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
-      if (dynstr == NULL)
-       return false;
-    }
-
-  dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
-  if (dynstr_index == (unsigned long) -1)
-    return false;
-  entry->isym.st_name = dynstr_index;
-
-  eht = elf_hash_table (info);
-
-  entry->next = eht->dynlocal;
-  eht->dynlocal = entry;
-  entry->input_bfd = input_bfd;
-  entry->input_indx = input_indx;
-  eht->dynsymcount++;
-
-  /* Whatever binding the symbol had before, it's now local.  */
-  entry->isym.st_info
-    = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
-
-  /* The dynindx will be set at the end of size_dynamic_sections.  */
-
-  return true;
-}
 \f
 /* Read and swap the relocs from the section indicated by SHDR.  This
    may be either a REL or a RELA section.  The relocations are
@@ -2795,10 +2678,6 @@ NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
 
   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
 
-  /* When possible, keep the original type of the symbol.  */
-  if (h->type == STT_NOTYPE)
-    h->type = STT_OBJECT;
-
   if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
                                  | ELF_LINK_HASH_REF_DYNAMIC)) != 0
        || info->shared)
@@ -2896,11 +2775,11 @@ compute_bucket_count (info)
   elf_link_hash_traverse (elf_hash_table (info),
                          elf_collect_hash_codes, &hashcodesp);
 
-/* We have a problem here.  The following code to optimize the table
-   size requires an integer type with more the 32 bits.  If
-   BFD_HOST_U_64_BIT is set we know about such a type.  */
+  /* We have a problem here.  The following code to optimize the table
+     size requires an integer type with more the 32 bits.  If
+     BFD_HOST_U_64_BIT is set we know about such a type.  */
 #ifdef BFD_HOST_U_64_BIT
-  if (info->optimize == true)
+  if (info->optimize)
     {
       unsigned long int nsyms = hashcodesp - hashcodes;
       size_t minsize;
@@ -3041,7 +2920,7 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
     return true;
 
   if (! is_elf_hash_table (info))
-    return false;
+    return true;
 
   /* Any syms created from now on start with -1 in
      got.refcount/offset and plt.refcount/offset.  */
@@ -3069,6 +2948,9 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
       struct elf_info_failed eif;
       struct elf_link_hash_entry *h;
       asection *dynstr;
+      struct bfd_elf_version_tree *t;
+      struct bfd_elf_version_expr *d;
+      boolean all_defined;
 
       *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
       BFD_ASSERT (*sinterpptr != NULL || info->shared);
@@ -3140,15 +3022,66 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
       eif.failed = false;
 
       /* If we are supposed to export all symbols into the dynamic symbol
-         table (this is not the normal case), then do so.  */
+        table (this is not the normal case), then do so.  */
       if (info->export_dynamic)
        {
          elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
-                                 (PTR) &eif);
+                                 (PTR) &eif);
          if (eif.failed)
            return false;
        }
 
+      /* Make all global versions with definiton.  */
+      for (t = verdefs; t != NULL; t = t->next)
+       for (d = t->globals; d != NULL; d = d->next)
+         if (!d->symver && strchr (d->pattern, '*') == NULL)
+           {
+             const char *verstr, *name;
+             size_t namelen, verlen, newlen;
+             char *newname, *p;
+             struct elf_link_hash_entry *newh;
+
+             name = d->pattern;
+             namelen = strlen (name);
+             verstr = t->name;
+             verlen = strlen (verstr);
+             newlen = namelen + verlen + 3; 
+
+             newname = (char *) bfd_malloc ((bfd_size_type) newlen);
+             if (newname == NULL)
+               return false;
+             memcpy (newname, name, namelen);
+
+             /* Check the hidden versioned definition.  */
+             p = newname + namelen;
+             *p++ = ELF_VER_CHR;
+             memcpy (p, verstr, verlen + 1);
+             newh = elf_link_hash_lookup (elf_hash_table (info),
+                                          newname, false, false,
+                                          false);
+             if (newh == NULL
+                 || (newh->root.type != bfd_link_hash_defined
+                     && newh->root.type != bfd_link_hash_defweak))
+               {
+                 /* Check the default versioned definition.  */
+                 *p++ = ELF_VER_CHR;
+                 memcpy (p, verstr, verlen + 1);
+                 newh = elf_link_hash_lookup (elf_hash_table (info),
+                                              newname, false, false,
+                                              false);
+               }
+             free (newname);
+
+             /* Mark this version if there is a definition and it is
+                not defined in a shared object.  */
+             if (newh != NULL
+                 && ((newh->elf_link_hash_flags
+                      & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
+                 && (newh->root.type == bfd_link_hash_defined
+                     || newh->root.type == bfd_link_hash_defweak))
+               d->symver = 1;
+           }
+
       /* Attach all the symbols to their version information.  */
       asvinfo.output_bfd = output_bfd;
       asvinfo.info = info;
@@ -3161,6 +3094,28 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
       if (asvinfo.failed)
        return false;
 
+      if (!info->allow_undefined_version)
+       {
+         /* Check if all global versions have a definiton.  */
+         all_defined = true;
+         for (t = verdefs; t != NULL; t = t->next)
+           for (d = t->globals; d != NULL; d = d->next)
+             if (!d->symver && !d->script
+                 && strchr (d->pattern, '*') == NULL)
+               {
+                 (*_bfd_error_handler)
+                   (_("%s: undefined version: %s"),
+                    d->pattern, t->name);
+                 all_defined = false;
+               }
+
+         if (!all_defined)
+           {
+             bfd_set_error (bfd_error_bad_value);
+             return false;
+           }
+       }
+
       /* Find all symbols which were defined in a dynamic object and make
         the backend pick a reasonable value for them.  */
       elf_link_hash_traverse (elf_hash_table (info),
@@ -3200,6 +3155,53 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
            return false;
        }
 
+      if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
+       {
+         /* DT_PREINIT_ARRAY is not allowed in shared library.  */
+         if (info->shared)
+           {
+             bfd *sub;
+             asection *o;
+
+             for (sub = info->input_bfds; sub != NULL;
+                  sub = sub->link_next)
+               for (o = sub->sections; o != NULL; o = o->next)
+                 if (elf_section_data (o)->this_hdr.sh_type
+                     == SHT_PREINIT_ARRAY)
+                   {
+                     (*_bfd_error_handler)
+                       (_("%s: .preinit_array section is not allowed in DSO"),
+                        bfd_archive_filename (sub));
+                     break;
+                   }
+
+             bfd_set_error (bfd_error_nonrepresentable_section);
+             return false;
+           }
+
+         if (!elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAY,
+                                     (bfd_vma) 0)
+             || !elf_add_dynamic_entry (info, (bfd_vma) DT_PREINIT_ARRAYSZ,
+                                        (bfd_vma) 0))
+           return false;
+       }
+      if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
+       {
+         if (!elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAY,
+                                     (bfd_vma) 0)
+             || !elf_add_dynamic_entry (info, (bfd_vma) DT_INIT_ARRAYSZ,
+                                        (bfd_vma) 0))
+           return false;
+       }
+      if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
+       {
+         if (!elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAY,
+                                     (bfd_vma) 0)
+             || !elf_add_dynamic_entry (info, (bfd_vma) DT_FINI_ARRAYSZ,
+                                        (bfd_vma) 0))
+           return false;
+       }
+
       dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
       /* If .dynstr is excluded from the link, we don't want any of
         these tags.  Strictly, we should be checking each section
@@ -3239,7 +3241,7 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
       BFD_ASSERT (s != NULL);
 
       /* We may have created additional version definitions if we are
-         just linking a regular application.  */
+        just linking a regular application.  */
       verdefs = asvinfo.verdefs;
 
       /* Skip anonymous version tag.  */
@@ -3328,19 +3330,20 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
              unsigned int cdeps;
              struct bfd_elf_version_deps *n;
              struct elf_link_hash_entry *h;
+             struct bfd_link_hash_entry *bh;
 
              cdeps = 0;
              for (n = t->deps; n != NULL; n = n->next)
                ++cdeps;
 
              /* Add a symbol representing this version.  */
-             h = NULL;
+             bh = NULL;
              if (! (_bfd_generic_link_add_one_symbol
                     (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
                      (bfd_vma) 0, (const char *) NULL, false,
-                     get_elf_backend_data (dynobj)->collect,
-                     (struct bfd_link_hash_entry **) &h)))
+                     get_elf_backend_data (dynobj)->collect, &bh)))
                return false;
+             h = (struct elf_link_hash_entry *) bh;
              h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
              h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
              h->type = STT_OBJECT;
@@ -3413,7 +3416,7 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
          elf_tdata (output_bfd)->cverdefs = cdefs;
        }
 
-      if (info->new_dtags && info->flags)
+      if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
        {
          if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
            return false;
@@ -3604,10 +3607,9 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
       BFD_ASSERT (s != NULL);
       hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
       s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
-      s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
+      s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->_raw_size);
       if (s->contents == NULL)
        return false;
-      memset (s->contents, 0, (size_t) s->_raw_size);
 
       bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
               s->contents);
@@ -3633,10 +3635,10 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
 \f
 /* This function is used to adjust offsets into .dynstr for
    dynamic symbols.  This is called via elf_link_hash_traverse.  */
-      
+
 static boolean elf_adjust_dynstr_offsets
 PARAMS ((struct elf_link_hash_entry *, PTR));
-        
+
 static boolean
 elf_adjust_dynstr_offsets (h, data)
      struct elf_link_hash_entry *h;
@@ -3644,6 +3646,9 @@ elf_adjust_dynstr_offsets (h, data)
 {
   struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
 
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   if (h->dynindx != -1)
     h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
   return true;
@@ -3716,7 +3721,7 @@ elf_finalize_dynstr (output_bfd, info)
       bfd_size_type i;
       Elf_Internal_Verdef def;
       Elf_Internal_Verdaux defaux;
-                    
+
       s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
       p = (bfd_byte *) s->contents;
       do
@@ -3746,7 +3751,7 @@ elf_finalize_dynstr (output_bfd, info)
       bfd_size_type i;
       Elf_Internal_Verneed need;
       Elf_Internal_Vernaux needaux;
-                    
+
       s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
       p = (bfd_byte *) s->contents;
       do
@@ -3824,11 +3829,11 @@ elf_fix_symbol_flags (h, eif)
   else
     {
       /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
-         was first seen in a non-ELF file.  Fortunately, if the symbol
-         was first seen in an ELF file, we're probably OK unless the
-         symbol was defined in a non-ELF file.  Catch that case here.
-         FIXME: We're still in trouble if the symbol was first seen in
-         a dynamic object, and then later in a non-ELF regular object.  */
+        was first seen in a non-ELF file.  Fortunately, if the symbol
+        was first seen in an ELF file, we're probably OK unless the
+        symbol was defined in a non-ELF file.  Catch that case here.
+        FIXME: We're still in trouble if the symbol was first seen in
+        a dynamic object, and then later in a non-ELF regular object.  */
       if ((h->root.type == bfd_link_hash_defined
           || h->root.type == bfd_link_hash_defweak)
          && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
@@ -3886,9 +3891,12 @@ elf_fix_symbol_flags (h, eif)
     {
       struct elf_link_hash_entry *weakdef;
 
+      weakdef = h->weakdef;
+      if (h->root.type == bfd_link_hash_indirect)
+       h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
       BFD_ASSERT (h->root.type == bfd_link_hash_defined
                  || h->root.type == bfd_link_hash_defweak);
-      weakdef = h->weakdef;
       BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
                  || weakdef->root.type == bfd_link_hash_defweak);
       BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
@@ -3903,7 +3911,7 @@ elf_fix_symbol_flags (h, eif)
          struct elf_backend_data *bed;
 
          bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
-         (*bed->elf_backend_copy_indirect_symbol) (weakdef, h);
+         (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
        }
     }
 
@@ -3923,6 +3931,17 @@ elf_adjust_dynamic_symbol (h, data)
   bfd *dynobj;
   struct elf_backend_data *bed;
 
+  if (h->root.type == bfd_link_hash_warning)
+    {
+      h->plt.offset = (bfd_vma) -1;
+      h->got.offset = (bfd_vma) -1;
+
+      /* When warning symbols are created, they **replace** the "real"
+        entry in the hash table, thus we never get to see the real
+        symbol in a hash traversal.  So look at it now.  */
+      h = (struct elf_link_hash_entry *) h->root.u.i.link;
+    }
+
   /* Ignore indirect symbols.  These are added by the versioning code.  */
   if (h->root.type == bfd_link_hash_indirect)
     return true;
@@ -4012,7 +4031,7 @@ elf_adjust_dynamic_symbol (h, data)
       && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
     (*_bfd_error_handler)
       (_("warning: type and size of dynamic symbol `%s' are not defined"),
-        h->root.root.string);
+       h->root.root.string);
 
   dynobj = elf_hash_table (eif->info)->dynobj;
   bed = get_elf_backend_data (dynobj);
@@ -4039,6 +4058,9 @@ elf_export_symbol (h, data)
   if (h->root.type == bfd_link_hash_indirect)
     return true;
 
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   if (h->dynindx == -1
       && (h->elf_link_hash_flags
          & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
@@ -4068,14 +4090,14 @@ elf_export_symbol (h, data)
        }
 
       if (!eif->verdefs)
-        {
-doit:
+       {
+       doit:
          if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
            {
              eif->failed = true;
              return false;
            }
-        }
+       }
     }
 
   return true;
@@ -4096,6 +4118,9 @@ elf_link_find_version_dependencies (h, data)
   Elf_Internal_Vernaux *a;
   bfd_size_type amt;
 
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   /* We only care about symbols defined in shared objects with version
      information.  */
   if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
@@ -4176,6 +4201,9 @@ elf_link_assign_sym_version (h, data)
   sinfo = (struct elf_assign_sym_version_info *) data;
   info = sinfo->info;
 
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   /* Fix the symbol flags.  */
   eif.failed = false;
   eif.info = info;
@@ -4201,7 +4229,7 @@ elf_link_assign_sym_version (h, data)
       hidden = true;
 
       /* There are two consecutive ELF_VER_CHR characters if this is
-         not a hidden symbol.  */
+        not a hidden symbol.  */
       ++p;
       if (*p == ELF_VER_CHR)
        {
@@ -4229,11 +4257,11 @@ elf_link_assign_sym_version (h, data)
              len = p - h->root.root.string;
              alc = bfd_malloc ((bfd_size_type) len);
              if (alc == NULL)
-               return false;
-             strncpy (alc, h->root.root.string, len - 1);
+               return false;
+             memcpy (alc, h->root.root.string, len - 1);
              alc[len - 1] = '\0';
              if (alc[len - 2] == ELF_VER_CHR)
-               alc[len - 2] = '\0';
+               alc[len - 2] = '\0';
 
              h->verinfo.vertree = t;
              t->used = true;
@@ -4247,7 +4275,7 @@ elf_link_assign_sym_version (h, data)
                }
 
              /* See if there is anything to force this symbol to
-                 local scope.  */
+                local scope.  */
              if (d == NULL && t->locals != NULL)
                {
                  for (d = t->locals; d != NULL; d = d->next)
@@ -4272,14 +4300,14 @@ elf_link_assign_sym_version (h, data)
        }
 
       /* If we are building an application, we need to create a
-         version node for this version.  */
+        version node for this version.  */
       if (t == NULL && ! info->shared)
        {
          struct bfd_elf_version_tree **pp;
          int version_index;
 
          /* If we aren't going to export this symbol, we don't need
-             to worry about it.  */
+            to worry about it.  */
          if (h->dynindx == -1)
            return true;
 
@@ -4315,7 +4343,7 @@ elf_link_assign_sym_version (h, data)
       else if (t == NULL)
        {
          /* We could not find the version for a symbol when
-             generating a shared archive.  Return an error.  */
+            generating a shared archive.  Return an error.  */
          (*_bfd_error_handler)
            (_("%s: undefined versioned symbol name %s"),
             bfd_get_filename (sinfo->output_bfd), h->root.root.string);
@@ -4333,45 +4361,58 @@ elf_link_assign_sym_version (h, data)
   if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
     {
       struct bfd_elf_version_tree *t;
-      struct bfd_elf_version_tree *deflt;
+      struct bfd_elf_version_tree *local_ver;
       struct bfd_elf_version_expr *d;
 
       /* See if can find what version this symbol is in.  If the
-         symbol is supposed to be local, then don't actually register
-         it.  */
-      deflt = NULL;
+        symbol is supposed to be local, then don't actually register
+        it.  */
+      local_ver = NULL;
       for (t = sinfo->verdefs; t != NULL; t = t->next)
        {
          if (t->globals != NULL)
            {
+             boolean matched;
+
+             matched = false;
              for (d = t->globals; d != NULL; d = d->next)
                {
                  if ((*d->match) (d, h->root.root.string))
                    {
-                     h->verinfo.vertree = t;
-                     break;
+                     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)
+               /* There is no undefined version for this symbol. Hide the
+                  default one.  */
+               (*bed->elf_backend_hide_symbol) (info, h, true);
            }
 
          if (t->locals != NULL)
            {
              for (d = t->locals; d != NULL; d = d->next)
                {
+                 /* If the match is "*", keep looking for a more
+                    explicit, perhaps even global, match.  */
                  if (d->pattern[0] == '*' && d->pattern[1] == '\0')
-                   deflt = t;
+                   local_ver = t;
                  else if ((*d->match) (d, h->root.root.string))
                    {
-                     h->verinfo.vertree = t;
-                     if (h->dynindx != -1
-                         && info->shared
-                         && ! info->export_dynamic)
-                       {
-                         (*bed->elf_backend_hide_symbol) (info, h, true);
-                       }
+                     local_ver = t;
                      break;
                    }
                }
@@ -4381,9 +4422,9 @@ elf_link_assign_sym_version (h, data)
            }
        }
 
-      if (deflt != NULL && h->verinfo.vertree == NULL)
+      if (local_ver != NULL)
        {
-         h->verinfo.vertree = deflt;
+         h->verinfo.vertree = local_ver;
          if (h->dynindx != -1
              && info->shared
              && ! info->export_dynamic)
@@ -4414,6 +4455,8 @@ struct elf_final_link_info
   asection *hash_sec;
   /* symbol version section (.gnu.version).  */
   asection *symver_sec;
+  /* first SHF_TLS section (if any).  */
+  asection *first_tls_sec;
   /* Buffer large enough to hold contents of any section.  */
   bfd_byte *contents;
   /* Buffer large enough to hold external relocs of any section.  */
@@ -4453,6 +4496,8 @@ static boolean elf_link_output_extsym
   PARAMS ((struct elf_link_hash_entry *, PTR));
 static boolean elf_link_sec_merge_syms
   PARAMS ((struct elf_link_hash_entry *, PTR));
+static boolean elf_link_check_versioned_symbol
+  PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
 static boolean elf_link_input_bfd
   PARAMS ((struct elf_final_link_info *, bfd *));
 static boolean elf_reloc_link_order
@@ -4596,7 +4641,7 @@ elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
 
          for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
            irela[j].r_info = ELF_R_INFO ((*rel_hash)->indx,
-                                      ELF_R_TYPE (irela[j].r_info));
+                                         ELF_R_TYPE (irela[j].r_info));
 
          if (bed->s->swap_reloca_out)
            (*bed->s->swap_reloca_out) (abfd, irela, (bfd_byte *) erela);
@@ -4609,10 +4654,12 @@ elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
   free (irela);
 }
 
-struct elf_link_sort_rela {
+struct elf_link_sort_rela
+{
   bfd_vma offset;
   enum elf_reloc_type_class type;
-  union {
+  union
+  {
     Elf_Internal_Rel rel;
     Elf_Internal_Rela rela;
   } u;
@@ -4858,8 +4905,8 @@ elf_bfd_final_link (abfd, info)
   dynobj = elf_hash_table (info)->dynobj;
 
   emit_relocs = (info->relocateable
-                 || info->emitrelocations
-                 || bed->elf_backend_emit_relocs);
+                || info->emitrelocations
+                || bed->elf_backend_emit_relocs);
 
   finfo.info = info;
   finfo.output_bfd = abfd;
@@ -4893,6 +4940,14 @@ elf_bfd_final_link (abfd, info)
   finfo.symbuf = NULL;
   finfo.symshndxbuf = NULL;
   finfo.symbuf_count = 0;
+  finfo.first_tls_sec = NULL;
+  for (o = abfd->sections; o != (asection *) NULL; o = o->next)
+    if ((o->flags & SEC_THREAD_LOCAL) != 0
+       && (o->flags & SEC_LOAD) != 0)
+      {
+       finfo.first_tls_sec = o;
+       break;
+      }
 
   /* Count up the number of relocations we will output for each output
      section, so that we know the sizes of the reloc sections.  We
@@ -4929,7 +4984,7 @@ elf_bfd_final_link (abfd, info)
 
              if (info->relocateable || info->emitrelocations)
                o->reloc_count += sec->reloc_count;
-              else if (bed->elf_backend_count_relocs)
+             else if (bed->elf_backend_count_relocs)
                {
                  Elf_Internal_Rela * relocs;
 
@@ -4937,10 +4992,10 @@ elf_bfd_final_link (abfd, info)
                            (abfd, sec, (PTR) NULL,
                             (Elf_Internal_Rela *) NULL, info->keep_memory));
 
-                 o->reloc_count += (*bed->elf_backend_count_relocs)
-                                     (sec, relocs);
+                 o->reloc_count
+                   += (*bed->elf_backend_count_relocs) (sec, relocs);
 
-                 if (!info->keep_memory)
+                 if (elf_section_data (o)->relocs != relocs)
                    free (relocs);
                }
 
@@ -5040,10 +5095,20 @@ elf_bfd_final_link (abfd, info)
                = elf_section_data (output_section);
              unsigned int *rel_count;
              unsigned int *rel_count2;
+             bfd_size_type entsize;
+             bfd_size_type entsize2;
 
-             /* We must be careful to add the relocation froms the
+             /* We must be careful to add the relocations from the
                 input section to the right output count.  */
-             if (esdi->rel_hdr.sh_entsize == esdo->rel_hdr.sh_entsize)
+             entsize = esdi->rel_hdr.sh_entsize;
+             entsize2 = esdi->rel_hdr2 ? esdi->rel_hdr2->sh_entsize : 0;
+             BFD_ASSERT ((entsize == sizeof (Elf_External_Rel)
+                          || entsize == sizeof (Elf_External_Rela))
+                         && entsize2 != entsize
+                         && (entsize2 == 0
+                             || entsize2 == sizeof (Elf_External_Rel)
+                             || entsize2 == sizeof (Elf_External_Rela)));
+             if (entsize == esdo->rel_hdr.sh_entsize)
                {
                  rel_count = &esdo->rel_count;
                  rel_count2 = &esdo->rel_count2;
@@ -5247,6 +5312,40 @@ elf_bfd_final_link (abfd, info)
        goto error_return;
     }
 
+  if (finfo.first_tls_sec)
+    {
+      unsigned int align = 0;
+      bfd_vma base = finfo.first_tls_sec->vma, end = 0;
+      asection *sec;
+
+      for (sec = finfo.first_tls_sec;
+          sec && (sec->flags & SEC_THREAD_LOCAL);
+          sec = sec->next)
+       {
+         bfd_vma size = sec->_raw_size;
+
+         if (bfd_get_section_alignment (abfd, sec) > align)
+           align = bfd_get_section_alignment (abfd, sec);
+         if (sec->_raw_size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
+           {
+             struct bfd_link_order *o;
+
+             size = 0;
+             for (o = sec->link_order_head; o != NULL; o = o->next)
+               if (size < o->offset + o->size)
+                 size = o->offset + o->size;
+           }
+         end = sec->vma + size;
+       }
+      elf_hash_table (info)->tls_segment
+       = bfd_zalloc (abfd, sizeof (struct elf_link_tls_segment));
+      if (elf_hash_table (info)->tls_segment == NULL)
+       goto error_return;
+      elf_hash_table (info)->tls_segment->start = base;
+      elf_hash_table (info)->tls_segment->size = end - base;
+      elf_hash_table (info)->tls_segment->align = align;
+    }
+
   /* Since ELF permits relocations to be against local symbols, we
      must have the local symbols available when we do the relocations.
      Since we would rather only read the local symbols once, and we
@@ -5274,10 +5373,10 @@ elf_bfd_final_link (abfd, info)
       for (p = o->link_order_head; p != NULL; p = p->next)
        {
          if (p->type == bfd_indirect_link_order
-             && (bfd_get_flavour (p->u.indirect.section->owner)
-                 == bfd_target_elf_flavour))
+             && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
+                 == bfd_target_elf_flavour)
+             && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
            {
-             sub = p->u.indirect.section->owner;
              if (! sub->output_has_begun)
                {
                  if (! elf_link_input_bfd (&finfo, sub))
@@ -5299,28 +5398,25 @@ elf_bfd_final_link (abfd, info)
        }
     }
 
+  /* Output any global symbols that got converted to local in a
+     version script or due to symbol visibility.  We do this in a
+     separate step since ELF requires all local symbols to appear
+     prior to any global symbols.  FIXME: We should only do this if
+     some global symbols were, in fact, converted to become local.
+     FIXME: Will this work correctly with the Irix 5 linker?  */
+  eoinfo.failed = false;
+  eoinfo.finfo = &finfo;
+  eoinfo.localsyms = true;
+  elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
+                         (PTR) &eoinfo);
+  if (eoinfo.failed)
+    return false;
+
   /* That wrote out all the local symbols.  Finish up the symbol table
      with the global symbols. Even if we want to strip everything we
      can, we still need to deal with those global symbols that got
      converted to local in a version script.  */
 
-  if (info->shared)
-    {
-      /* Output any global symbols that got converted to local in a
-         version script.  We do this in a separate step since ELF
-         requires all local symbols to appear prior to any global
-         symbols.  FIXME: We should only do this if some global
-         symbols were, in fact, converted to become local.  FIXME:
-         Will this work correctly with the Irix 5 linker?  */
-      eoinfo.failed = false;
-      eoinfo.finfo = &finfo;
-      eoinfo.localsyms = true;
-      elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
-                             (PTR) &eoinfo);
-      if (eoinfo.failed)
-       return false;
-    }
-
   /* The sh_info field records the index of the first non local symbol.  */
   symtab_hdr->sh_info = bfd_get_symcount (abfd);
 
@@ -5372,11 +5468,12 @@ elf_bfd_final_link (abfd, info)
 
              /* Copy the internal symbol as is.
                 Note that we saved a word of storage and overwrote
-                 the original st_name with the dynstr_index.  */
-              sym = e->isym;
+                the original st_name with the dynstr_index.  */
+             sym = e->isym;
 
-             if (e->isym.st_shndx < SHN_LORESERVE
-                 || e->isym.st_shndx > SHN_HIRESERVE)
+             if (e->isym.st_shndx != SHN_UNDEF
+                 && (e->isym.st_shndx < SHN_LORESERVE
+                     || e->isym.st_shndx > SHN_HIRESERVE))
                {
                  s = bfd_section_from_elf_index (e->input_bfd,
                                                  e->isym.st_shndx);
@@ -5549,6 +5646,40 @@ elf_bfd_final_link (abfd, info)
              }
              break;
 
+           case DT_PREINIT_ARRAYSZ:
+             name = ".preinit_array";
+             goto get_size;
+           case DT_INIT_ARRAYSZ:
+             name = ".init_array";
+             goto get_size;
+           case DT_FINI_ARRAYSZ:
+             name = ".fini_array";
+           get_size:
+             o = bfd_get_section_by_name (abfd, name);
+             if (o == NULL)
+               {
+                 (*_bfd_error_handler)
+                   (_("%s: could not find output section %s"),
+                    bfd_get_filename (abfd), name);
+                 goto error_return;
+               }
+             if (o->_raw_size == 0)
+               (*_bfd_error_handler)
+                 (_("warning: %s section has zero size"), name);
+             dyn.d_un.d_val = o->_raw_size;
+             elf_swap_dyn_out (dynobj, &dyn, dyncon);
+             break;
+
+           case DT_PREINIT_ARRAY:
+             name = ".preinit_array";
+             goto get_vma;
+           case DT_INIT_ARRAY:
+             name = ".init_array";
+             goto get_vma;
+           case DT_FINI_ARRAY:
+             name = ".fini_array";
+             goto get_vma;
+
            case DT_HASH:
              name = ".hash";
              goto get_vma;
@@ -5568,7 +5699,13 @@ elf_bfd_final_link (abfd, info)
              name = ".gnu.version";
            get_vma:
              o = bfd_get_section_by_name (abfd, name);
-             BFD_ASSERT (o != NULL);
+             if (o == NULL)
+               {
+                 (*_bfd_error_handler)
+                   (_("%s: could not find output section %s"),
+                    bfd_get_filename (abfd), name);
+                 goto error_return;
+               }
              dyn.d_un.d_ptr = o->vma;
              elf_swap_dyn_out (dynobj, &dyn, dyncon);
              break;
@@ -5621,7 +5758,7 @@ elf_bfd_final_link (abfd, info)
          if ((o->flags & SEC_LINKER_CREATED) == 0)
            {
              /* At this point, we are only interested in sections
-                 created by elf_link_create_dynamic_sections.  */
+                created by elf_link_create_dynamic_sections.  */
              continue;
            }
          if ((elf_section_data (o->output_section)->this_hdr.sh_type
@@ -5637,7 +5774,7 @@ elf_bfd_final_link (abfd, info)
          else
            {
              /* The contents of the .dynstr section are actually in a
-                 stringtab.  */
+                stringtab.  */
              off = elf_section_data (o->output_section)->this_hdr.sh_offset;
              if (bfd_seek (abfd, off, SEEK_SET) != 0
                  || ! _bfd_elf_strtab_emit (abfd,
@@ -5647,6 +5784,15 @@ elf_bfd_final_link (abfd, info)
        }
     }
 
+  if (info->relocateable)
+    {
+      boolean failed = false;
+
+      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
+      if (failed)
+       goto error_return;
+    }
+
   /* If we have optimized stabs strings, output them.  */
   if (elf_hash_table (info)->stab_info != NULL)
     {
@@ -5693,7 +5839,7 @@ elf_bfd_final_link (abfd, info)
     {
       if ((o->flags & SEC_RELOC) != 0
          && elf_section_data (o)->rel_hashes != NULL)
-        free (elf_section_data (o)->rel_hashes);
+       free (elf_section_data (o)->rel_hashes);
     }
 
   elf_tdata (abfd)->linker = true;
@@ -5744,7 +5890,7 @@ elf_link_output_sym (finfo, name, elfsym, input_sec)
 {
   Elf_External_Sym *dest;
   Elf_External_Sym_Shndx *destshndx;
-  
+
   boolean (*output_symbol_hook) PARAMS ((bfd *,
                                         struct bfd_link_info *info,
                                         const char *,
@@ -5840,6 +5986,9 @@ elf_link_sec_merge_syms (h, data)
 {
   asection *sec;
 
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   if ((h->root.type == bfd_link_hash_defined
        || h->root.type == bfd_link_hash_defweak)
       && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
@@ -5857,6 +6006,127 @@ elf_link_sec_merge_syms (h, data)
   return true;
 }
 
+/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
+   allowing an unsatisfied unversioned symbol in the DSO to match a
+   versioned symbol that would normally require an explicit version.  */
+
+static boolean
+elf_link_check_versioned_symbol (info, h)
+     struct bfd_link_info *info;
+     struct elf_link_hash_entry *h;
+{
+  bfd *undef_bfd = h->root.u.undef.abfd;
+  struct elf_link_loaded_list *loaded;
+
+  if ((undef_bfd->flags & DYNAMIC) == 0
+      || info->hash->creator->flavour != bfd_target_elf_flavour
+      || elf_dt_soname (h->root.u.undef.abfd) == NULL)
+    return false;
+
+  for (loaded = elf_hash_table (info)->loaded;
+       loaded != NULL;
+       loaded = loaded->next)
+    {
+      bfd *input;
+      Elf_Internal_Shdr *hdr;
+      bfd_size_type symcount;
+      bfd_size_type extsymcount;
+      bfd_size_type extsymoff;
+      Elf_Internal_Shdr *versymhdr;
+      Elf_Internal_Sym *isym;
+      Elf_Internal_Sym *isymend;
+      Elf_Internal_Sym *isymbuf;
+      Elf_External_Versym *ever;
+      Elf_External_Versym *extversym;
+
+      input = loaded->abfd;
+
+      /* We check each DSO for a possible hidden versioned definition.  */
+      if (input == undef_bfd
+         || (input->flags & DYNAMIC) == 0
+         || elf_dynversym (input) == 0)
+       continue;
+
+      hdr = &elf_tdata (input)->dynsymtab_hdr;
+
+      symcount = hdr->sh_size / sizeof (Elf_External_Sym);
+      if (elf_bad_symtab (input))
+       {
+         extsymcount = symcount;
+         extsymoff = 0;
+       }
+      else
+       {
+         extsymcount = symcount - hdr->sh_info;
+         extsymoff = hdr->sh_info;
+       }
+
+      if (extsymcount == 0)
+       continue;
+
+      isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
+                                     NULL, NULL, NULL);
+      if (isymbuf == NULL)
+       return false;
+
+      /* Read in any version definitions.  */
+      versymhdr = &elf_tdata (input)->dynversym_hdr;
+      extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
+      if (extversym == NULL)
+       goto error_ret;
+
+      if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
+         || (bfd_bread ((PTR) extversym, versymhdr->sh_size, input)
+             != versymhdr->sh_size))
+       {
+         free (extversym);
+       error_ret:
+         free (isymbuf);
+         return false;
+       }
+
+      ever = extversym + extsymoff;
+      isymend = isymbuf + extsymcount;
+      for (isym = isymbuf; isym < isymend; isym++, ever++)
+       {
+         const char *name;
+         Elf_Internal_Versym iver;
+
+         if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
+             || isym->st_shndx == SHN_UNDEF)
+           continue;
+
+         name = bfd_elf_string_from_elf_section (input,
+                                                 hdr->sh_link,
+                                                 isym->st_name);
+         if (strcmp (name, h->root.root.string) != 0)
+           continue;
+
+         _bfd_elf_swap_versym_in (input, ever, &iver);
+
+         if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
+           {
+             /* If we have a non-hidden versioned sym, then it should
+                have provided a definition for the undefined sym.  */
+             abort ();
+           }
+
+         if ((iver.vs_vers & VERSYM_VERSION) == 2)
+           {
+             /* This is the oldest (default) sym.  We can use it.  */
+             free (extversym);
+             free (isymbuf);
+             return true;
+           }
+       }
+
+      free (extversym);
+      free (isymbuf);
+    }
+
+  return false;
+}
+
 /* Add an external symbol to the symbol table.  This is called from
    the hash table traversal routine.  When generating a shared object,
    we go through the symbol table twice.  The first time we output
@@ -5875,6 +6145,13 @@ elf_link_output_extsym (h, data)
   Elf_Internal_Sym sym;
   asection *input_sec;
 
+  if (h->root.type == bfd_link_hash_warning)
+    {
+      h = (struct elf_link_hash_entry *) h->root.u.i.link;
+      if (h->root.type == bfd_link_hash_new)
+       return true;
+    }
+
   /* Decide whether to output this symbol in this pass.  */
   if (eoinfo->localsyms)
     {
@@ -5899,7 +6176,8 @@ elf_link_output_extsym (h, data)
       && ! finfo->info->shared
       && h->root.type == bfd_link_hash_undefined
       && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
-      && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
+      && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
+      && ! elf_link_check_versioned_symbol (finfo->info, h))
     {
       if (! ((*finfo->info->callbacks->undefined_symbol)
             (finfo->info, h->root.root.string, h->root.u.undef.abfd,
@@ -5952,14 +6230,11 @@ elf_link_output_extsym (h, data)
     {
     default:
     case bfd_link_hash_new:
+    case bfd_link_hash_warning:
       abort ();
       return false;
 
     case bfd_link_hash_undefined:
-      input_sec = bfd_und_section_ptr;
-      sym.st_shndx = SHN_UNDEF;
-      break;
-
     case bfd_link_hash_undefweak:
       input_sec = bfd_und_section_ptr;
       sym.st_shndx = SHN_UNDEF;
@@ -5990,7 +6265,16 @@ elf_link_output_extsym (h, data)
               addresses.  */
            sym.st_value = h->root.u.def.value + input_sec->output_offset;
            if (! finfo->info->relocateable)
-             sym.st_value += input_sec->output_section->vma;
+             {
+               sym.st_value += input_sec->output_section->vma;
+               if (h->type == STT_TLS)
+                 {
+                   /* STT_TLS symbols are relative to PT_TLS segment
+                      base.  */
+                   BFD_ASSERT (finfo->first_tls_sec != NULL);
+                   sym.st_value -= finfo->first_tls_sec->vma;
+                 }
+             }
          }
        else
          {
@@ -6010,29 +6294,22 @@ elf_link_output_extsym (h, data)
 
     case bfd_link_hash_indirect:
       /* These symbols are created by symbol versioning.  They point
-         to the decorated version of the name.  For example, if the
-         symbol foo@@GNU_1.2 is the default, which should be used when
-         foo is used with no version, then we add an indirect symbol
-         foo which points to foo@@GNU_1.2.  We ignore these symbols,
-         since the indirected symbol is already in the hash table.  */
+        to the decorated version of the name.  For example, if the
+        symbol foo@@GNU_1.2 is the default, which should be used when
+        foo is used with no version, then we add an indirect symbol
+        foo which points to foo@@GNU_1.2.  We ignore these symbols,
+        since the indirected symbol is already in the hash table.  */
       return true;
-
-    case bfd_link_hash_warning:
-      /* We can't represent these symbols in ELF, although a warning
-         symbol may have come from a .gnu.warning.SYMBOL section.  We
-         just put the target symbol in the hash table.  If the target
-         symbol does not really exist, don't do anything.  */
-      if (h->root.u.i.link->type == bfd_link_hash_new)
-       return true;
-      return (elf_link_output_extsym
-             ((struct elf_link_hash_entry *) h->root.u.i.link, data));
     }
 
   /* Give the processor backend a chance to tweak the symbol value,
      and also to finish up anything that needs to be done for this
-     symbol.  */
+     symbol.  FIXME: Not calling elf_backend_finish_dynamic_symbol for
+     forced local syms when non-shared is due to a historical quirk.  */
   if ((h->dynindx != -1
        || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
+      && (finfo->info->shared
+         || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
       && elf_hash_table (finfo->info)->dynamic_sections_created)
     {
       struct elf_backend_data *bed;
@@ -6073,8 +6350,8 @@ elf_link_output_extsym (h, data)
     sym.st_other ^= ELF_ST_VISIBILITY (sym.st_other);
 
   /* If this symbol should be put in the .dynsym section, then put it
-     there now.  We have already know the symbol index.  We also fill
-     in the entry in the .hash section.  */
+     there now.  We already know the symbol index.  We also fill in
+     the entry in the .hash section.  */
   if (h->dynindx != -1
       && elf_hash_table (finfo->info)->dynamic_sections_created)
     {
@@ -6133,7 +6410,7 @@ elf_link_output_extsym (h, data)
 
   /* If we're stripping it, then it was just a dynamic symbol, and
      there's nothing else to do.  */
-  if (strip)
+  if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
     return true;
 
   h->indx = bfd_get_symcount (finfo->output_bfd);
@@ -6151,7 +6428,7 @@ elf_link_output_extsym (h, data)
    originated from the section given by INPUT_REL_HDR) to the
    OUTPUT_BFD.  */
 
-static void
+static boolean
 elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
                        internal_relocs)
      bfd *output_bfd;
@@ -6183,13 +6460,21 @@ elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
       output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
       rel_countp = &elf_section_data (output_section)->rel_count2;
     }
-
-  BFD_ASSERT (output_rel_hdr != NULL);
+  else
+    {
+      (*_bfd_error_handler)
+       (_("%s: relocation size mismatch in %s section %s"),
+        bfd_get_filename (output_bfd),
+        bfd_archive_filename (input_section->owner),
+        input_section->name);
+      bfd_set_error (bfd_error_wrong_object_format);
+      return false;
+    }
 
   bed = get_elf_backend_data (output_bfd);
   irela = internal_relocs;
-  irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr)
-                    * bed->s->int_rels_per_ext_rel;
+  irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
+                     * bed->s->int_rels_per_ext_rel);
 
   if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
     {
@@ -6241,6 +6526,8 @@ elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
   /* Bump the counter, so that we know where to add the next set of
      relocations.  */
   *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
+
+  return true;
 }
 
 /* Link an input file into the linker output file.  This function
@@ -6259,15 +6546,11 @@ elf_link_input_bfd (finfo, input_bfd)
                                       Elf_Internal_Sym *, asection **));
   bfd *output_bfd;
   Elf_Internal_Shdr *symtab_hdr;
-  Elf_Internal_Shdr *shndx_hdr;
   size_t locsymcount;
   size_t extsymoff;
-  Elf_External_Sym *external_syms;
-  Elf_External_Sym *esym;
-  Elf_External_Sym *esymend;
-  Elf_External_Sym_Shndx *shndx_buf;
-  Elf_External_Sym_Shndx *shndx;
+  Elf_Internal_Sym *isymbuf;
   Elf_Internal_Sym *isym;
+  Elf_Internal_Sym *isymend;
   long *pindex;
   asection **ppsection;
   asection *o;
@@ -6286,8 +6569,8 @@ elf_link_input_bfd (finfo, input_bfd)
     return true;
 
   emit_relocs = (finfo->info->relocateable
-                 || finfo->info->emitrelocations
-                 || bed->elf_backend_emit_relocs);
+                || finfo->info->emitrelocations
+                || bed->elf_backend_emit_relocs);
 
   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   if (elf_bad_symtab (input_bfd))
@@ -6302,44 +6585,29 @@ elf_link_input_bfd (finfo, input_bfd)
     }
 
   /* Read the local symbols.  */
-  if (symtab_hdr->contents != NULL)
-    external_syms = (Elf_External_Sym *) symtab_hdr->contents;
-  else if (locsymcount == 0)
-    external_syms = NULL;
-  else
-    {
-      bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym);
-      external_syms = finfo->external_syms;
-      if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
-         || bfd_bread (external_syms, amt, input_bfd) != amt)
-       return false;
-    }
-
-  shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
-  shndx_buf = NULL;
-  if (shndx_hdr->sh_size != 0 && locsymcount != 0)
-    {
-      bfd_size_type amt = locsymcount * sizeof (Elf_External_Sym_Shndx);
-      shndx_buf = finfo->locsym_shndx;
-      if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
-         || bfd_bread (shndx_buf, amt, input_bfd) != amt)
+  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
+  if (isymbuf == NULL && locsymcount != 0)
+    {
+      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
+                                     finfo->internal_syms,
+                                     finfo->external_syms,
+                                     finfo->locsym_shndx);
+      if (isymbuf == NULL)
        return false;
     }
 
-  /* Swap in the local symbols and write out the ones which we know
-     are going into the output file.  */
-  for (esym = external_syms, esymend = esym + locsymcount,
-        isym = finfo->internal_syms, pindex = finfo->indices,
-        ppsection = finfo->sections, shndx = shndx_buf;
-       esym < esymend;
-       esym++, isym++, pindex++, ppsection++,
-        shndx = (shndx != NULL ? shndx + 1 : NULL))
+  /* Find local symbol sections and adjust values of symbols in
+     SEC_MERGE sections.  Write out those local symbols we know are
+     going into the output file.  */
+  isymend = isymbuf + locsymcount;
+  for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
+       isym < isymend;
+       isym++, pindex++, ppsection++)
     {
       asection *isec;
       const char *name;
       Elf_Internal_Sym osym;
 
-      elf_swap_symbol_in (input_bfd, esym, shndx, isym);
       *pindex = -1;
 
       if (elf_bad_symtab (input_bfd))
@@ -6378,7 +6646,7 @@ elf_link_input_bfd (finfo, input_bfd)
       *ppsection = isec;
 
       /* Don't output the first, undefined, symbol.  */
-      if (esym == external_syms)
+      if (ppsection == finfo->sections)
        continue;
 
       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
@@ -6403,10 +6671,10 @@ elf_link_input_bfd (finfo, input_bfd)
        continue;
 
       /* If this symbol is defined in a section which we are
-         discarding, we don't need to keep it, but note that
-         linker_mark is only reliable for sections that have contents.
-         For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
-         as well as linker_mark.  */
+        discarding, we don't need to keep it, but note that
+        linker_mark is only reliable for sections that have contents.
+        For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
+        as well as linker_mark.  */
       if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
          && isec != NULL
          && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
@@ -6451,7 +6719,15 @@ elf_link_input_bfd (finfo, input_bfd)
         these requirements.  */
       osym.st_value += isec->output_offset;
       if (! finfo->info->relocateable)
-       osym.st_value += isec->output_section->vma;
+       {
+         osym.st_value += isec->output_section->vma;
+         if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
+           {
+             /* STT_TLS symbols are relative to PT_TLS segment base.  */
+             BFD_ASSERT (finfo->first_tls_sec != NULL);
+             osym.st_value -= finfo->first_tls_sec->vma;
+           }
+       }
 
       if (! elf_link_output_sym (finfo, name, &osym, isec))
        return false;
@@ -6481,9 +6757,9 @@ elf_link_input_bfd (finfo, input_bfd)
        }
 
       /* Get the contents of the section.  They have been cached by a
-         relaxation routine.  Note that o is a section in an input
-         file, so the contents field will not have been set by any of
-         the routines which work on output files.  */
+        relaxation routine.  Note that o is a section in an input
+        file, so the contents field will not have been set by any of
+        the routines which work on output files.  */
       if (elf_section_data (o)->this_hdr.contents != NULL)
        contents = elf_section_data (o)->this_hdr.contents;
       else
@@ -6544,20 +6820,12 @@ elf_link_input_bfd (finfo, input_bfd)
                           || h->root.type == bfd_link_hash_defweak)
                          && elf_discarded_section (h->root.u.def.section))
                        {
-#if BFD_VERSION_DATE < 20031005
                          if ((o->flags & SEC_DEBUGGING) != 0)
                            {
-#if BFD_VERSION_DATE > 20021005
-                             (*finfo->info->callbacks->warning)
-                               (finfo->info,
-                                _("warning: relocation against removed section; zeroing"),
-                                NULL, input_bfd, o, rel->r_offset);
-#endif
                              BFD_ASSERT (r_symndx != 0);
                              memset (rel, 0, sizeof (*rel));
                            }
                          else
-#endif
                            {
                              if (! ((*finfo->info->callbacks->undefined_symbol)
                                     (finfo->info, h->root.root.string,
@@ -6573,23 +6841,15 @@ elf_link_input_bfd (finfo, input_bfd)
 
                      if (sec != NULL && elf_discarded_section (sec))
                        {
-#if BFD_VERSION_DATE < 20031005
                          if ((o->flags & SEC_DEBUGGING) != 0
                              || (sec->flags & SEC_LINK_ONCE) != 0)
                            {
-#if BFD_VERSION_DATE > 20021005
-                             (*finfo->info->callbacks->warning)
-                               (finfo->info,
-                                _("warning: relocation against removed section"),
-                                NULL, input_bfd, o, rel->r_offset);
-#endif
                              BFD_ASSERT (r_symndx != 0);
                              rel->r_info
                                = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
                              rel->r_addend = 0;
                            }
                          else
-#endif
                            {
                              boolean ok;
                              const char *msg
@@ -6640,7 +6900,7 @@ elf_link_input_bfd (finfo, input_bfd)
          if (! (*relocate_section) (output_bfd, finfo->info,
                                     input_bfd, o, contents,
                                     internal_relocs,
-                                    finfo->internal_syms,
+                                    isymbuf,
                                     finfo->sections))
            return false;
 
@@ -6649,11 +6909,17 @@ elf_link_input_bfd (finfo, input_bfd)
              Elf_Internal_Rela *irela;
              Elf_Internal_Rela *irelaend;
              struct elf_link_hash_entry **rel_hash;
-             Elf_Internal_Shdr *input_rel_hdr;
+             Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
              unsigned int next_erel;
-             void (*reloc_emitter) PARAMS ((bfd *, asection *,
-                                            Elf_Internal_Shdr *,
-                                            Elf_Internal_Rela *));
+             boolean (*reloc_emitter) PARAMS ((bfd *, asection *,
+                                               Elf_Internal_Shdr *,
+                                               Elf_Internal_Rela *));
+             boolean rela_normal;
+
+             input_rel_hdr = &elf_section_data (o)->rel_hdr;
+             rela_normal = (bed->rela_normal
+                            && (input_rel_hdr->sh_entsize
+                                == sizeof (Elf_External_Rela)));
 
              /* Adjust the reloc addresses and symbol indices.  */
 
@@ -6666,6 +6932,7 @@ elf_link_input_bfd (finfo, input_bfd)
                {
                  unsigned long r_symndx;
                  asection *sec;
+                 Elf_Internal_Sym sym;
 
                  if (next_erel == bed->s->int_rels_per_ext_rel)
                    {
@@ -6676,7 +6943,7 @@ elf_link_input_bfd (finfo, input_bfd)
                  irela->r_offset += o->output_offset;
 
                  /* Relocs in an executable have to be virtual addresses.  */
-                 if (finfo->info->emitrelocations)
+                 if (!finfo->info->relocateable)
                    irela->r_offset += o->output_section->vma;
 
                  r_symndx = ELF_R_SYM (irela->r_info);
@@ -6718,19 +6985,18 @@ elf_link_input_bfd (finfo, input_bfd)
                  /* This is a reloc against a local symbol.  */
 
                  *rel_hash = NULL;
-                 isym = finfo->internal_syms + r_symndx;
+                 sym = isymbuf[r_symndx];
                  sec = finfo->sections[r_symndx];
-                 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
+                 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
                    {
                      /* I suppose the backend ought to fill in the
                         section of any STT_SECTION symbol against a
                         processor specific section.  If we have
                         discarded a section, the output_section will
                         be the absolute section.  */
-                     if (sec != NULL
-                         && (bfd_is_abs_section (sec)
-                             || (sec->output_section != NULL
-                                 && bfd_is_abs_section (sec->output_section))))
+                     if (bfd_is_abs_section (sec)
+                         || (sec != NULL
+                             && bfd_is_abs_section (sec->output_section)))
                        r_symndx = 0;
                      else if (sec == NULL || sec->owner == NULL)
                        {
@@ -6742,6 +7008,11 @@ elf_link_input_bfd (finfo, input_bfd)
                          r_symndx = sec->output_section->target_index;
                          BFD_ASSERT (r_symndx != 0);
                        }
+
+                     /* Adjust the addend according to where the
+                        section winds up in the output section.  */
+                     if (rela_normal)
+                       irela->r_addend += sec->output_offset;
                    }
                  else
                    {
@@ -6763,25 +7034,34 @@ elf_link_input_bfd (finfo, input_bfd)
                             must output it now.  */
                          shlink = symtab_hdr->sh_link;
                          name = (bfd_elf_string_from_elf_section
-                                 (input_bfd, shlink, isym->st_name));
+                                 (input_bfd, shlink, sym.st_name));
                          if (name == NULL)
                            return false;
 
                          osec = sec->output_section;
-                         isym->st_shndx =
+                         sym.st_shndx =
                            _bfd_elf_section_from_bfd_section (output_bfd,
                                                               osec);
-                         if (isym->st_shndx == SHN_BAD)
+                         if (sym.st_shndx == SHN_BAD)
                            return false;
 
-                         isym->st_value += sec->output_offset;
+                         sym.st_value += sec->output_offset;
                          if (! finfo->info->relocateable)
-                           isym->st_value += osec->vma;
+                           {
+                             sym.st_value += osec->vma;
+                             if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
+                               {
+                                 /* STT_TLS symbols are relative to PT_TLS
+                                    segment base.  */
+                                 BFD_ASSERT (finfo->first_tls_sec != NULL);
+                                 sym.st_value -= finfo->first_tls_sec->vma;
+                               }
+                           }
 
                          finfo->indices[r_symndx]
                            = bfd_get_symcount (output_bfd);
 
-                         if (! elf_link_output_sym (finfo, name, isym, sec))
+                         if (! elf_link_output_sym (finfo, name, &sym, sec))
                            return false;
                        }
 
@@ -6793,24 +7073,27 @@ elf_link_input_bfd (finfo, input_bfd)
                }
 
              /* Swap out the relocs.  */
-              if (bed->elf_backend_emit_relocs
-                  && !(finfo->info->relocateable
+             if (bed->elf_backend_emit_relocs
+                 && !(finfo->info->relocateable
                       || finfo->info->emitrelocations))
-                reloc_emitter = bed->elf_backend_emit_relocs;
-              else
-                reloc_emitter = elf_link_output_relocs;
+               reloc_emitter = bed->elf_backend_emit_relocs;
+             else
+               reloc_emitter = elf_link_output_relocs;
 
-             input_rel_hdr = &elf_section_data (o)->rel_hdr;
-              (*reloc_emitter) (output_bfd, o, input_rel_hdr, internal_relocs);
+             if (input_rel_hdr->sh_size != 0
+                 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
+                                        internal_relocs))
+               return false;
 
-             input_rel_hdr = elf_section_data (o)->rel_hdr2;
-              if (input_rel_hdr)
-                {
-                  internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
+             input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
+             if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
+               {
+                 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
                                      * bed->s->int_rels_per_ext_rel);
-                  reloc_emitter (output_bfd, o, input_rel_hdr, internal_relocs);
-                }
-
+                 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
+                                         internal_relocs))
+                   return false;
+               }
            }
        }
 
@@ -6909,7 +7192,7 @@ elf_reloc_link_order (output_bfd, info, output_section, link_order)
       struct elf_link_hash_entry *h;
 
       /* Treat a reloc against a defined symbol as though it were
-         actually against the section.  */
+        actually against the section.  */
       h = ((struct elf_link_hash_entry *)
           bfd_wrapped_link_hash_lookup (output_bfd, info,
                                         link_order->u.reloc.p->u.name,
@@ -6924,8 +7207,8 @@ elf_reloc_link_order (output_bfd, info, output_section, link_order)
          indx = section->output_section->target_index;
          *rel_hash_ptr = NULL;
          /* It seems that we ought to add the symbol value to the
-             addend here, but in practice it has already been added
-             because it was passed to constructor_callback.  */
+            addend here, but in practice it has already been added
+            because it was passed to constructor_callback.  */
          addend += section->output_section->vma + section->output_offset;
        }
       else if (h != NULL)
@@ -7327,28 +7610,27 @@ elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
 /* Garbage collect unused sections.  */
 
 static boolean elf_gc_mark
-  PARAMS ((struct bfd_link_info *info, asection *sec,
-          asection * (*gc_mark_hook)
-            PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
-                     struct elf_link_hash_entry *, Elf_Internal_Sym *))));
+  PARAMS ((struct bfd_link_info *, asection *,
+          asection * (*) (asection *, struct bfd_link_info *,
+                          Elf_Internal_Rela *, struct elf_link_hash_entry *,
+                          Elf_Internal_Sym *)));
 
 static boolean elf_gc_sweep
-  PARAMS ((struct bfd_link_info *info,
-          boolean (*gc_sweep_hook)
-            PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
-                     const Elf_Internal_Rela *relocs))));
+  PARAMS ((struct bfd_link_info *,
+          boolean (*) (bfd *, struct bfd_link_info *, asection *,
+                       const Elf_Internal_Rela *)));
 
 static boolean elf_gc_sweep_symbol
-  PARAMS ((struct elf_link_hash_entry *h, PTR idxptr));
+  PARAMS ((struct elf_link_hash_entry *, PTR));
 
 static boolean elf_gc_allocate_got_offsets
-  PARAMS ((struct elf_link_hash_entry *h, PTR offarg));
+  PARAMS ((struct elf_link_hash_entry *, PTR));
 
 static boolean elf_gc_propagate_vtable_entries_used
-  PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
+  PARAMS ((struct elf_link_hash_entry *, PTR));
 
 static boolean elf_gc_smash_unused_vtentry_relocs
-  PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
+  PARAMS ((struct elf_link_hash_entry *, PTR));
 
 /* The mark phase of garbage collection.  For a given section, mark
    it and any sections in this section's group, and all the sections
@@ -7358,9 +7640,10 @@ static boolean
 elf_gc_mark (info, sec, gc_mark_hook)
      struct bfd_link_info *info;
      asection *sec;
-     asection * (*gc_mark_hook)
-       PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
-               struct elf_link_hash_entry *, Elf_Internal_Sym *));
+     asection * (*gc_mark_hook) PARAMS ((asection *, struct bfd_link_info *,
+                                        Elf_Internal_Rela *,
+                                        struct elf_link_hash_entry *,
+                                        Elf_Internal_Sym *));
 {
   boolean ret;
   asection *group_sec;
@@ -7379,17 +7662,12 @@ elf_gc_mark (info, sec, gc_mark_hook)
     {
       Elf_Internal_Rela *relstart, *rel, *relend;
       Elf_Internal_Shdr *symtab_hdr;
-      Elf_Internal_Shdr *shndx_hdr;
       struct elf_link_hash_entry **sym_hashes;
       size_t nlocsyms;
       size_t extsymoff;
-      Elf_External_Sym *locsyms, *freesyms = NULL;
-      Elf_External_Sym_Shndx *locsym_shndx;
       bfd *input_bfd = sec->owner;
       struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
-
-      /* GCFIXME: how to arrange so that relocs and symbols are not
-        reread continually?  */
+      Elf_Internal_Sym *isym = NULL;
 
       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
       sym_hashes = elf_sym_hashes (input_bfd);
@@ -7398,42 +7676,23 @@ elf_gc_mark (info, sec, gc_mark_hook)
       if (elf_bad_symtab (input_bfd))
        {
          nlocsyms = symtab_hdr->sh_size / sizeof (Elf_External_Sym);
-          extsymoff = 0;
+         extsymoff = 0;
        }
       else
        extsymoff = nlocsyms = symtab_hdr->sh_info;
 
-      if (symtab_hdr->contents)
-       locsyms = (Elf_External_Sym *) symtab_hdr->contents;
-      else if (nlocsyms == 0)
-       locsyms = NULL;
-      else
-       {
-         bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym);
-         locsyms = freesyms = bfd_malloc (amt);
-         if (freesyms == NULL
-             || bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
-             || bfd_bread (locsyms, amt, input_bfd) != amt)
-           {
-             ret = false;
-             goto out1;
-           }
-       }
-
-      shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
-      locsym_shndx = NULL;
-      if (shndx_hdr->sh_size != 0 && nlocsyms != 0)
+      isym = (Elf_Internal_Sym *) symtab_hdr->contents;
+      if (isym == NULL && nlocsyms != 0)
        {
-         bfd_size_type amt = nlocsyms * sizeof (Elf_External_Sym_Shndx);
-         locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
-         if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
-             || bfd_bread (locsym_shndx, amt, input_bfd) != amt)
+         isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
+                                      NULL, NULL, NULL);
+         if (isym == NULL)
            return false;
        }
 
       /* Read the relocations.  */
       relstart = (NAME(_bfd_elf,link_read_relocs)
-                 (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL,
+                 (input_bfd, sec, NULL, (Elf_Internal_Rela *) NULL,
                   info->keep_memory));
       if (relstart == NULL)
        {
@@ -7447,54 +7706,45 @@ elf_gc_mark (info, sec, gc_mark_hook)
          unsigned long r_symndx;
          asection *rsec;
          struct elf_link_hash_entry *h;
-         Elf_Internal_Sym s;
 
          r_symndx = ELF_R_SYM (rel->r_info);
          if (r_symndx == 0)
            continue;
 
-         if (elf_bad_symtab (sec->owner))
-           {
-             elf_swap_symbol_in (input_bfd,
-                                 locsyms + r_symndx,
-                                 locsym_shndx + (locsym_shndx ? r_symndx : 0),
-                                 &s);
-             if (ELF_ST_BIND (s.st_info) == STB_LOCAL)
-               rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
-             else
-               {
-                 h = sym_hashes[r_symndx - extsymoff];
-                 rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
-               }
-           }
-         else if (r_symndx >= nlocsyms)
+         if (r_symndx >= nlocsyms
+             || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
            {
              h = sym_hashes[r_symndx - extsymoff];
-             rsec = (*gc_mark_hook) (sec->owner, info, rel, h, NULL);
+             rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
            }
          else
            {
-             elf_swap_symbol_in (input_bfd,
-                                 locsyms + r_symndx,
-                                 locsym_shndx + (locsym_shndx ? r_symndx : 0),
-                                 &s);
-             rsec = (*gc_mark_hook) (sec->owner, info, rel, NULL, &s);
+             rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
            }
 
          if (rsec && !rsec->gc_mark)
-           if (!elf_gc_mark (info, rsec, gc_mark_hook))
-             {
-               ret = false;
-               goto out2;
-             }
+           {
+             if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
+               rsec->gc_mark = 1;
+             else if (!elf_gc_mark (info, rsec, gc_mark_hook))
+               {
+                 ret = false;
+                 goto out2;
+               }
+           }
        }
 
     out2:
-      if (!info->keep_memory)
+      if (elf_section_data (sec)->relocs != relstart)
        free (relstart);
     out1:
-      if (freesyms)
-       free (freesyms);
+      if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
+       {
+         if (! info->keep_memory)
+           free (isym);
+         else
+           symtab_hdr->contents = (unsigned char *) isym;
+       }
     }
 
   return ret;
@@ -7505,9 +7755,8 @@ elf_gc_mark (info, sec, gc_mark_hook)
 static boolean
 elf_gc_sweep (info, gc_sweep_hook)
      struct bfd_link_info *info;
-     boolean (*gc_sweep_hook)
-       PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *o,
-               const Elf_Internal_Rela *relocs));
+     boolean (*gc_sweep_hook) PARAMS ((bfd *, struct bfd_link_info *,
+                                      asection *, const Elf_Internal_Rela *));
 {
   bfd *sub;
 
@@ -7551,7 +7800,7 @@ elf_gc_sweep (info, gc_sweep_hook)
 
              r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
 
-             if (!info->keep_memory)
+             if (elf_section_data (o)->relocs != internal_relocs)
                free (internal_relocs);
 
              if (!r)
@@ -7585,6 +7834,9 @@ elf_gc_sweep_symbol (h, idxptr)
 {
   int *idx = (int *) idxptr;
 
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   if (h->dynindx != -1
       && ((h->root.type != bfd_link_hash_defined
           && h->root.type != bfd_link_hash_defweak)
@@ -7602,6 +7854,9 @@ elf_gc_propagate_vtable_entries_used (h, okp)
      struct elf_link_hash_entry *h;
      PTR okp;
 {
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   /* Those that are not vtables.  */
   if (h->vtable_parent == NULL)
     return true;
@@ -7635,9 +7890,9 @@ elf_gc_propagate_vtable_entries_used (h, okp)
       pu = h->vtable_parent->vtable_entries_used;
       if (pu != NULL)
        {
-          asection *sec = h->root.u.def.section;
-          struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
-          int file_align = bed->s->file_align;
+         asection *sec = h->root.u.def.section;
+         struct elf_backend_data *bed = get_elf_backend_data (sec->owner);
+         int file_align = bed->s->file_align;
 
          n = h->vtable_parent->vtable_entries_size / file_align;
          while (n--)
@@ -7664,6 +7919,9 @@ elf_gc_smash_unused_vtentry_relocs (h, okp)
   struct elf_backend_data *bed;
   int file_align;
 
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   /* Take care of both those symbols that do not describe vtables as
      well as those that are not loaded.  */
   if (h->vtable_parent == NULL)
@@ -7713,8 +7971,8 @@ elf_gc_sections (abfd, info)
   boolean ok = true;
   bfd *sub;
   asection * (*gc_mark_hook)
-    PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
-             struct elf_link_hash_entry *h, Elf_Internal_Sym *));
+    PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
+            struct elf_link_hash_entry *h, Elf_Internal_Sym *));
 
   if (!get_elf_backend_data (abfd)->can_gc_sections
       || info->relocateable || info->emitrelocations
@@ -7748,7 +8006,7 @@ elf_gc_sections (abfd, info)
       for (o = sub->sections; o != NULL; o = o->next)
        {
          if (o->flags & SEC_KEEP)
-           if (!elf_gc_mark (info, o, gc_mark_hook))
+           if (!elf_gc_mark (info, o, gc_mark_hook))
              return false;
        }
     }
@@ -7951,6 +8209,9 @@ elf_gc_allocate_got_offsets (h, offarg)
 {
   bfd_vma *off = (bfd_vma *) offarg;
 
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   if (h->got.refcount > 0)
     {
       h->got.offset = off[0];
@@ -7991,6 +8252,9 @@ elf_collect_hash_codes (h, data)
   unsigned long ha;
   char *alc = NULL;
 
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   /* Ignore indirect symbols.  These are added by the versioning code.  */
   if (h->dynindx == -1)
     return true;
@@ -8034,7 +8298,6 @@ elf_reloc_symbol_deleted_p (offset, cookie)
   for (; rcookie->rel < rcookie->relend; rcookie->rel++)
     {
       unsigned long r_symndx = ELF_R_SYM (rcookie->rel->r_info);
-      Elf_Internal_Sym isym;
 
       if (! rcookie->bad_symtab)
        if (rcookie->rel->r_offset > offset)
@@ -8042,21 +8305,8 @@ elf_reloc_symbol_deleted_p (offset, cookie)
       if (rcookie->rel->r_offset != offset)
        continue;
 
-      if (rcookie->locsyms && r_symndx < rcookie->locsymcount)
-       {
-         Elf_External_Sym *lsym;
-         Elf_External_Sym_Shndx *lshndx;
-
-         lsym = (Elf_External_Sym *) rcookie->locsyms + r_symndx;
-         lshndx = (Elf_External_Sym_Shndx *) rcookie->locsym_shndx;
-         if (lshndx != NULL)
-           lshndx += r_symndx;
-         elf_swap_symbol_in (rcookie->abfd, lsym, lshndx, &isym);
-       }
-
       if (r_symndx >= rcookie->locsymcount
-         || (rcookie->locsyms
-             && ELF_ST_BIND (isym.st_info) != STB_LOCAL))
+         || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
        {
          struct elf_link_hash_entry *h;
 
@@ -8073,17 +8323,19 @@ elf_reloc_symbol_deleted_p (offset, cookie)
          else
            return false;
        }
-      else if (rcookie->locsyms)
+      else
        {
          /* It's not a relocation against a global symbol,
             but it could be a relocation against a local
             symbol for a discarded section.  */
          asection *isec;
+         Elf_Internal_Sym *isym;
 
          /* Need to: get the symbol; get the section.  */
-         if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
+         isym = &rcookie->locsyms[r_symndx];
+         if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
            {
-             isec = section_from_elf_index (rcookie->abfd, isym.st_shndx);
+             isec = section_from_elf_index (rcookie->abfd, isym->st_shndx);
              if (isec != NULL && elf_discarded_section (isec))
                return true;
            }
@@ -8106,8 +8358,6 @@ elf_bfd_discard_info (output_bfd, info)
   struct elf_reloc_cookie cookie;
   asection *stab, *eh, *ehdr;
   Elf_Internal_Shdr *symtab_hdr;
-  Elf_Internal_Shdr *shndx_hdr;
-  Elf_External_Sym *freesyms;
   struct elf_backend_data *bed;
   bfd *abfd;
   boolean ret = false;
@@ -8138,19 +8388,26 @@ elf_bfd_discard_info (output_bfd, info)
       if (ehdr)
        {
          eh = bfd_get_section_by_name (abfd, ".eh_frame");
-         if (eh && eh->_raw_size == 0)
+         if (eh && (eh->_raw_size == 0
+                    || bfd_is_abs_section (eh->output_section)))
            eh = NULL;
        }
 
-      stab = strip ? NULL : bfd_get_section_by_name (abfd, ".stab");
-      if ((! stab || elf_section_data(stab)->sec_info_type != ELF_INFO_TYPE_STABS)
+      stab = NULL;
+      if (!strip)
+       {
+         stab = bfd_get_section_by_name (abfd, ".stab");
+         if (stab && (stab->_raw_size == 0
+                      || bfd_is_abs_section (stab->output_section)))
+           stab = NULL;
+       }
+      if ((! stab
+          || elf_section_data(stab)->sec_info_type != ELF_INFO_TYPE_STABS)
          && ! eh
          && (strip || ! bed->elf_backend_discard_info))
        continue;
 
       symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
-      shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
-
       cookie.abfd = abfd;
       cookie.sym_hashes = elf_sym_hashes (abfd);
       cookie.bad_symtab = elf_bad_symtab (abfd);
@@ -8166,48 +8423,20 @@ elf_bfd_discard_info (output_bfd, info)
          cookie.extsymoff = symtab_hdr->sh_info;
        }
 
-      freesyms = NULL;
-      if (symtab_hdr->contents)
-        cookie.locsyms = (void *) symtab_hdr->contents;
-      else if (cookie.locsymcount == 0)
-        cookie.locsyms = NULL;
-      else
-        {
-          bfd_size_type amt = cookie.locsymcount * sizeof (Elf_External_Sym);
-          cookie.locsyms = bfd_malloc (amt);
-          if (cookie.locsyms == NULL)
+      cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
+      if (cookie.locsyms == NULL && cookie.locsymcount != 0)
+       {
+         cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
+                                                cookie.locsymcount, 0,
+                                                NULL, NULL, NULL);
+         if (cookie.locsyms == NULL)
            return false;
-         freesyms = cookie.locsyms;
-         if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
-              || bfd_bread (cookie.locsyms, amt, abfd) != amt)
-           {
-           error_ret_free_loc:
-             free (cookie.locsyms);
-             return false;
-            }
-        }
-
-      cookie.locsym_shndx = NULL;
-      if (shndx_hdr->sh_size != 0 && cookie.locsymcount != 0)
-       {
-         bfd_size_type amt;
-         amt = cookie.locsymcount * sizeof (Elf_External_Sym_Shndx);
-         cookie.locsym_shndx = bfd_malloc (amt);
-         if (cookie.locsym_shndx == NULL)
-           goto error_ret_free_loc;
-         if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
-             || bfd_bread (cookie.locsym_shndx, amt, abfd) != amt)
-           {
-             free (cookie.locsym_shndx);
-             goto error_ret_free_loc;
-           }
        }
 
       if (stab)
        {
          cookie.rels = (NAME(_bfd_elf,link_read_relocs)
-                        (abfd, stab, (PTR) NULL,
-                         (Elf_Internal_Rela *) NULL,
+                        (abfd, stab, (PTR) NULL, (Elf_Internal_Rela *) NULL,
                          info->keep_memory));
          if (cookie.rels)
            {
@@ -8219,7 +8448,7 @@ elf_bfd_discard_info (output_bfd, info)
                                              elf_reloc_symbol_deleted_p,
                                              &cookie))
                ret = true;
-             if (! info->keep_memory)
+             if (elf_section_data (stab)->relocs != cookie.rels)
                free (cookie.rels);
            }
        }
@@ -8231,8 +8460,7 @@ elf_bfd_discard_info (output_bfd, info)
          cookie.relend = NULL;
          if (eh->reloc_count)
            cookie.rels = (NAME(_bfd_elf,link_read_relocs)
-                          (abfd, eh, (PTR) NULL,
-                           (Elf_Internal_Rela *) NULL,
+                          (abfd, eh, (PTR) NULL, (Elf_Internal_Rela *) NULL,
                            info->keep_memory));
          if (cookie.rels)
            {
@@ -8243,8 +8471,13 @@ elf_bfd_discard_info (output_bfd, info)
          if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, ehdr,
                                                 elf_reloc_symbol_deleted_p,
                                                 &cookie))
-           ret = true;
-         if (! info->keep_memory)
+           {
+             /* Relocs have been edited.  Ensure edited version is
+                used later in relocate_section.  */
+             elf_section_data (eh)->relocs = cookie.rels;
+             ret = true;
+           }
+         if (cookie.rels && elf_section_data (eh)->relocs != cookie.rels)
            free (cookie.rels);
        }
 
@@ -8254,16 +8487,17 @@ elf_bfd_discard_info (output_bfd, info)
            ret = true;
        }
 
-      if (cookie.locsym_shndx != NULL)
-       free (cookie.locsym_shndx);
-
-      if (freesyms != NULL)
-       free (freesyms);
+      if (cookie.locsyms != NULL
+         && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
+       {
+         if (! info->keep_memory)
+           free (cookie.locsyms);
+         else
+           symtab_hdr->contents = (unsigned char *) cookie.locsyms;
+       }
     }
 
-  if (ehdr
-      && _bfd_elf_discard_section_eh_frame_hdr (output_bfd,
-                                               info, ehdr))
+  if (ehdr && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info, ehdr))
     ret = true;
   return ret;
 }
@@ -8272,6 +8506,8 @@ static boolean
 elf_section_ignore_discarded_relocs (sec)
      asection *sec;
 {
+  struct elf_backend_data *bed;
+
   switch (elf_section_data (sec)->sec_info_type)
     {
     case ELF_INFO_TYPE_STABS:
@@ -8280,10 +8516,10 @@ elf_section_ignore_discarded_relocs (sec)
     default:
       break;
     }
-  if ((get_elf_backend_data (sec->owner)->elf_backend_ignore_discarded_relocs
-       != NULL)
-      && (*get_elf_backend_data (sec->owner)
-          ->elf_backend_ignore_discarded_relocs) (sec))
+
+  bed = get_elf_backend_data (sec->owner);
+  if (bed->elf_backend_ignore_discarded_relocs != NULL
+      && (*bed->elf_backend_ignore_discarded_relocs) (sec))
     return true;
 
   return false;
This page took 0.072368 seconds and 4 git commands to generate.