* elf32-arm.h (elf32_arm_final_link_relocate): Don't copy STN_UNDEF
[deliverable/binutils-gdb.git] / bfd / elflink.h
index e1dee8debe65e0a0e8b017a91bb54a3ea5fe9a67..a4fb88d5be5a2e692a8d9d436a15a14e07a555fe 100644 (file)
@@ -27,8 +27,13 @@ struct elf_info_failed
 {
   boolean failed;
   struct bfd_link_info *info;
+  struct bfd_elf_version_tree *verdefs;
 };
 
+static boolean is_global_data_symbol_definition
+  PARAMS ((bfd *, Elf_Internal_Sym *));
+static boolean elf_link_is_defined_archive_symbol
+  PARAMS ((bfd *, carsym *));
 static boolean elf_link_add_object_symbols
   PARAMS ((bfd *, struct bfd_link_info *));
 static boolean elf_link_add_archive_symbols
@@ -39,6 +44,8 @@ static boolean elf_merge_symbol
           boolean *, boolean *, boolean *, boolean));
 static boolean elf_export_symbol
   PARAMS ((struct elf_link_hash_entry *, PTR));
+static boolean elf_finalize_dynstr
+  PARAMS ((bfd *, struct bfd_link_info *));
 static boolean elf_fix_symbol_flags
   PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
 static boolean elf_adjust_dynamic_symbol
@@ -53,6 +60,8 @@ static boolean elf_collect_hash_codes
   PARAMS ((struct elf_link_hash_entry *, PTR));
 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
   PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
 static boolean elf_link_size_reloc_section
@@ -60,6 +69,12 @@ static boolean elf_link_size_reloc_section
 static void elf_link_adjust_relocs
   PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
           struct elf_link_hash_entry **));
+static int elf_link_sort_cmp1
+  PARAMS ((const void *, const void *));
+static int elf_link_sort_cmp2
+  PARAMS ((const void *, const void *));
+static size_t elf_link_sort_relocs
+  PARAMS ((bfd *, struct bfd_link_info *, asection **));
 
 /* Given an ELF BFD, add symbols to the global hash table as
    appropriate.  */
@@ -132,10 +147,12 @@ elf_link_is_defined_archive_symbol (abfd, symdef)
   Elf_External_Sym *  esym;
   Elf_External_Sym *  esymend;
   Elf_External_Sym *  buf = NULL;
-  size_t symcount;
-  size_t extsymcount;
-  size_t extsymoff;
+  bfd_size_type symcount;
+  bfd_size_type extsymcount;
+  bfd_size_type extsymoff;
   boolean result = false;
+  file_ptr pos;
+  bfd_size_type amt;
 
   abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
   if (abfd == (bfd *) NULL)
@@ -172,18 +189,16 @@ elf_link_is_defined_archive_symbol (abfd, symdef)
       extsymoff = hdr->sh_info;
     }
 
-  buf = ((Elf_External_Sym *)
-        bfd_malloc (extsymcount * sizeof (Elf_External_Sym)));
+  amt = extsymcount * sizeof (Elf_External_Sym);
+  buf = (Elf_External_Sym *) bfd_malloc (amt);
   if (buf == NULL && extsymcount != 0)
     return false;
 
   /* Read in the symbol table.
      FIXME:  This ought to be cached somewhere.  */
-  if (bfd_seek (abfd,
-               hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym),
-               SEEK_SET) != 0
-      || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd)
-         != extsymcount * sizeof (Elf_External_Sym)))
+  pos = hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym);
+  if (bfd_seek (abfd, pos, SEEK_SET) != 0
+      || bfd_bread ((PTR) buf, amt, abfd) != amt)
     {
       free (buf);
       return false;
@@ -251,6 +266,7 @@ elf_link_add_archive_symbols (abfd, info)
   boolean *included = NULL;
   carsym *symdefs;
   boolean loop;
+  bfd_size_type amt;
 
   if (! bfd_has_map (abfd))
     {
@@ -267,12 +283,14 @@ elf_link_add_archive_symbols (abfd, info)
   c = bfd_ardata (abfd)->symdef_count;
   if (c == 0)
     return true;
-  defined = (boolean *) bfd_malloc (c * sizeof (boolean));
-  included = (boolean *) bfd_malloc (c * sizeof (boolean));
+  amt = c;
+  amt *= sizeof (boolean);
+  defined = (boolean *) bfd_malloc (amt);
+  included = (boolean *) bfd_malloc (amt);
   if (defined == (boolean *) NULL || included == (boolean *) NULL)
     goto error_return;
-  memset (defined, 0, c * sizeof (boolean));
-  memset (included, 0, c * sizeof (boolean));
+  memset (defined, 0, (size_t) amt);
+  memset (included, 0, (size_t) amt);
 
   symdefs = bfd_ardata (abfd)->symdefs;
 
@@ -320,10 +338,10 @@ elf_link_add_archive_symbols (abfd, info)
              if (p == NULL || p[1] != ELF_VER_CHR)
                continue;
 
-             copy = bfd_alloc (abfd, p - symdef->name + 1);
+             copy = bfd_alloc (abfd, (bfd_size_type) (p - symdef->name + 1));
              if (copy == NULL)
                goto error_return;
-             memcpy (copy, symdef->name, p - symdef->name);
+             memcpy (copy, symdef->name, (size_t) (p - symdef->name));
              copy[p - symdef->name] = '\0';
 
              h = elf_link_hash_lookup (elf_hash_table (info), copy,
@@ -889,9 +907,9 @@ elf_link_add_object_symbols (abfd, info)
                                   asection *, const Elf_Internal_Rela *));
   boolean collect;
   Elf_Internal_Shdr *hdr;
-  size_t symcount;
-  size_t extsymcount;
-  size_t extsymoff;
+  bfd_size_type symcount;
+  bfd_size_type extsymcount;
+  bfd_size_type extsymoff;
   Elf_External_Sym *buf = NULL;
   struct elf_link_hash_entry **sym_hash;
   boolean dynamic;
@@ -903,6 +921,11 @@ elf_link_add_object_symbols (abfd, info)
   Elf_External_Sym *esymend;
   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);
 
   bed = get_elf_backend_data (abfd);
   add_symbol_hook = bed->elf_add_symbol_hook;
@@ -957,7 +980,7 @@ elf_link_add_object_symbols (abfd, info)
                {
                  struct elf_link_hash_entry *h;
 
-                 h = elf_link_hash_lookup (elf_hash_table (info), name,
+                 h = elf_link_hash_lookup (hash_table, name,
                                            false, false, true);
 
                  /* FIXME: What about bfd_link_hash_common?  */
@@ -1025,9 +1048,9 @@ elf_link_add_object_symbols (abfd, info)
          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_read ((PTR) extversym, 1, versymhdr->sh_size, abfd)
-                 != versymhdr->sh_size))
+             || bfd_bread ((PTR) extversym, amt, abfd) != amt)
            goto error_return;
        }
     }
@@ -1048,16 +1071,15 @@ elf_link_add_object_symbols (abfd, info)
       extsymoff = hdr->sh_info;
     }
 
-  buf = ((Elf_External_Sym *)
-        bfd_malloc (extsymcount * sizeof (Elf_External_Sym)));
+  amt = extsymcount * sizeof (Elf_External_Sym);
+  buf = (Elf_External_Sym *) bfd_malloc (amt);
   if (buf == NULL && extsymcount != 0)
     goto error_return;
 
   /* We store a pointer to the hash table entry for each external
      symbol.  */
-  sym_hash = ((struct elf_link_hash_entry **)
-             bfd_alloc (abfd,
-                        extsymcount * sizeof (struct elf_link_hash_entry *)));
+  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;
@@ -1072,13 +1094,16 @@ elf_link_add_object_symbols (abfd, info)
          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
-         && ! elf_hash_table (info)->dynamic_sections_created
+         && is_elf_hash_table (info)
+         && ! hash_table->dynamic_sections_created
          && abfd->xvec == info->hash->creator)
        {
          if (! elf_link_create_dynamic_sections (abfd, info))
            goto error_return;
        }
     }
+  else if (! is_elf_hash_table (info))
+    goto error_return;
   else
     {
       asection *s;
@@ -1113,11 +1138,11 @@ elf_link_add_object_symbols (abfd, info)
          Elf_External_Dyn *extdyn;
          Elf_External_Dyn *extdynend;
          int elfsec;
-         unsigned long link;
+         unsigned long shlink;
          int rpath;
          int runpath;
 
-         dynbuf = (Elf_External_Dyn *) bfd_malloc ((size_t) s->_raw_size);
+         dynbuf = (Elf_External_Dyn *) bfd_malloc (s->_raw_size);
          if (dynbuf == NULL)
            goto error_return;
 
@@ -1128,22 +1153,22 @@ elf_link_add_object_symbols (abfd, info)
          elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
          if (elfsec == -1)
            goto error_return;
-         link = elf_elfsections (abfd)[elfsec]->sh_link;
+         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 LINK refers to a section that is not a string table and
-              tries to find the string table for the ".dynsym" section
+              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 *hdr = elf_elfsections (abfd)[link];
-           if (hdr->sh_type != SHT_STRTAB)
+           Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[shlink];
+           if (shdr->sh_type != SHT_STRTAB)
              {
-               asection *s = bfd_get_section_by_name (abfd, ".dynsym");
-               int elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
-               if (elfsec == -1)
+               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;
-               link = elf_elfsections (abfd)[elfsec]->sh_link;
+               shlink = elf_elfsections (abfd)[elfdsec]->sh_link;
              }
          }
 
@@ -1158,8 +1183,8 @@ elf_link_add_object_symbols (abfd, info)
              elf_swap_dyn_in (abfd, extdyn, &dyn);
              if (dyn.d_tag == DT_SONAME)
                {
-                 name = bfd_elf_string_from_elf_section (abfd, link,
-                                                         dyn.d_un.d_val);
+                 unsigned int tagv = dyn.d_un.d_val;
+                 name = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
                  if (name == NULL)
                    goto error_return;
                }
@@ -1167,21 +1192,21 @@ elf_link_add_object_symbols (abfd, info)
                {
                  struct bfd_link_needed_list *n, **pn;
                  char *fnm, *anm;
+                 unsigned int tagv = dyn.d_un.d_val;
 
-                 n = ((struct bfd_link_needed_list *)
-                      bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
-                 fnm = bfd_elf_string_from_elf_section (abfd, link,
-                                                        dyn.d_un.d_val);
+                 amt = sizeof (struct bfd_link_needed_list);
+                 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, strlen (fnm) + 1);
+                 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
                  if (anm == NULL)
                    goto error_return;
                  strcpy (anm, fnm);
                  n->name = anm;
                  n->by = abfd;
                  n->next = NULL;
-                 for (pn = &elf_hash_table (info)->needed;
+                 for (pn = & hash_table->needed;
                       *pn != NULL;
                       pn = &(*pn)->next)
                    ;
@@ -1191,28 +1216,28 @@ elf_link_add_object_symbols (abfd, info)
                {
                  struct bfd_link_needed_list *n, **pn;
                  char *fnm, *anm;
+                 unsigned int tagv = dyn.d_un.d_val;
 
                  /* When we see DT_RPATH before DT_RUNPATH, we have
                     to clear runpath.  Do _NOT_ bfd_release, as that
                     frees all more recently bfd_alloc'd blocks as
                     well.  */
-                 if (rpath && elf_hash_table (info)->runpath)
-                   elf_hash_table (info)->runpath = NULL;
+                 if (rpath && hash_table->runpath)
+                   hash_table->runpath = NULL;
 
-                 n = ((struct bfd_link_needed_list *)
-                      bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
-                 fnm = bfd_elf_string_from_elf_section (abfd, link,
-                                                        dyn.d_un.d_val);
+                 amt = sizeof (struct bfd_link_needed_list);
+                 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, strlen (fnm) + 1);
+                 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
                  if (anm == NULL)
                    goto error_return;
                  strcpy (anm, fnm);
                  n->name = anm;
                  n->by = abfd;
                  n->next = NULL;
-                 for (pn = &elf_hash_table (info)->runpath;
+                 for (pn = & hash_table->runpath;
                       *pn != NULL;
                       pn = &(*pn)->next)
                    ;
@@ -1225,21 +1250,21 @@ elf_link_add_object_symbols (abfd, info)
                {
                  struct bfd_link_needed_list *n, **pn;
                  char *fnm, *anm;
+                 unsigned int tagv = dyn.d_un.d_val;
 
-                 n = ((struct bfd_link_needed_list *)
-                      bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
-                 fnm = bfd_elf_string_from_elf_section (abfd, link,
-                                                        dyn.d_un.d_val);
+                 amt = sizeof (struct bfd_link_needed_list);
+                 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, strlen (fnm) + 1);
+                 anm = bfd_alloc (abfd, (bfd_size_type) strlen (fnm) + 1);
                  if (anm == NULL)
                    goto error_return;
                  strcpy (anm, fnm);
                  n->name = anm;
                  n->by = abfd;
                  n->next = NULL;
-                 for (pn = &elf_hash_table (info)->runpath;
+                 for (pn = & hash_table->runpath;
                       *pn != NULL;
                       pn = &(*pn)->next)
                    ;
@@ -1264,22 +1289,19 @@ elf_link_add_object_symbols (abfd, info)
 
       /* If this is the first dynamic object found in the link, create
         the special sections required for dynamic linking.  */
-      if (! elf_hash_table (info)->dynamic_sections_created)
-       {
-         if (! elf_link_create_dynamic_sections (abfd, info))
-           goto error_return;
-       }
+      if (! hash_table->dynamic_sections_created)
+       if (! elf_link_create_dynamic_sections (abfd, info))
+         goto error_return;
 
       if (add_needed)
        {
          /* Add a DT_NEEDED entry for this dynamic object.  */
-         oldsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
-         strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr, name,
-                                        true, false);
+         oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
+         strindex = _bfd_elf_strtab_add (hash_table->dynstr, name, false);
          if (strindex == (bfd_size_type) -1)
            goto error_return;
 
-         if (oldsize == _bfd_stringtab_size (elf_hash_table (info)->dynstr))
+         if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
            {
              asection *sdyn;
              Elf_External_Dyn *dyncon, *dynconend;
@@ -1289,8 +1311,7 @@ elf_link_add_object_symbols (abfd, info)
                 have already included this dynamic object in the
                 link, just ignore it.  There is no reason to include
                 a particular dynamic object more than once.  */
-             sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
-                                             ".dynamic");
+             sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
              BFD_ASSERT (sdyn != NULL);
 
              dyncon = (Elf_External_Dyn *) sdyn->contents;
@@ -1300,8 +1321,7 @@ elf_link_add_object_symbols (abfd, info)
                {
                  Elf_Internal_Dyn dyn;
 
-                 elf_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon,
-                                  &dyn);
+                 elf_swap_dyn_in (hash_table->dynobj, dyncon, & dyn);
                  if (dyn.d_tag == DT_NEEDED
                      && dyn.d_un.d_val == strindex)
                    {
@@ -1309,12 +1329,13 @@ elf_link_add_object_symbols (abfd, info)
                        free (buf);
                      if (extversym != NULL)
                        free (extversym);
+                     _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
                      return true;
                    }
                }
            }
 
-         if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
+         if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
            goto error_return;
        }
 
@@ -1325,11 +1346,10 @@ elf_link_add_object_symbols (abfd, info)
       elf_dt_name (abfd) = name;
     }
 
-  if (bfd_seek (abfd,
-               hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym),
-               SEEK_SET) != 0
-      || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd)
-         != extsymcount * sizeof (Elf_External_Sym)))
+  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;
 
   weaks = NULL;
@@ -1457,7 +1477,8 @@ elf_link_add_object_symbols (abfd, info)
                  || (vernum > 1 && ! bfd_is_abs_section (sec)))
                {
                  const char *verstr;
-                 int namelen, newlen;
+                 unsigned int namelen;
+                 bfd_size_type newlen;
                  char *newname, *p;
 
                  if (sym.st_shndx != SHN_UNDEF)
@@ -1466,7 +1487,7 @@ elf_link_add_object_symbols (abfd, info)
                        {
                          (*_bfd_error_handler)
                            (_("%s: %s: invalid version %u (max %d)"),
-                            bfd_get_filename (abfd), name, vernum,
+                            bfd_archive_filename (abfd), name, vernum,
                             elf_tdata (abfd)->dynverdef_hdr.sh_info);
                          bfd_set_error (bfd_error_bad_value);
                          goto error_return;
@@ -1507,7 +1528,7 @@ elf_link_add_object_symbols (abfd, info)
                        {
                          (*_bfd_error_handler)
                            (_("%s: %s: invalid needed version %d"),
-                            bfd_get_filename (abfd), name, vernum);
+                            bfd_archive_filename (abfd), name, vernum);
                          bfd_set_error (bfd_error_bad_value);
                          goto error_return;
                        }
@@ -1628,7 +1649,7 @@ elf_link_add_object_symbols (abfd, info)
                (*_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_get_filename (abfd));
+                  bfd_archive_filename (abfd));
 
              h->size = sym.st_size;
            }
@@ -1650,7 +1671,7 @@ elf_link_add_object_symbols (abfd, info)
                (*_bfd_error_handler)
                  (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
                   name, h->type, ELF_ST_TYPE (sym.st_info),
-                  bfd_get_filename (abfd));
+                  bfd_archive_filename (abfd));
 
              h->type = ELF_ST_TYPE (sym.st_info);
            }
@@ -1729,10 +1750,10 @@ elf_link_add_object_symbols (abfd, info)
                  boolean override;
 
                  shortname = bfd_hash_allocate (&info->hash->table,
-                                                p - name + 1);
+                                                (size_t) (p - name + 1));
                  if (shortname == NULL)
                    goto error_return;
-                 strncpy (shortname, name, p - name);
+                 strncpy (shortname, name, (size_t) (p - name));
                  shortname[p - name] = '\0';
 
                  /* We are going to create a new symbol.  Merge it
@@ -1854,7 +1875,7 @@ elf_link_add_object_symbols (abfd, info)
                                                 strlen (name));
                  if (shortname == NULL)
                    goto error_return;
-                 strncpy (shortname, name, p - name);
+                 strncpy (shortname, name, (size_t) (p - name));
                  strcpy (shortname + (p - name), p + 1);
 
                  /* Once again, merge with any existing symbol.  */
@@ -1873,7 +1894,7 @@ elf_link_add_object_symbols (abfd, info)
                          do in the case above.  */
                      (*_bfd_error_handler)
                        (_("%s: warning: unexpected redefinition of `%s'"),
-                        bfd_get_filename (abfd), shortname);
+                        bfd_archive_filename (abfd), shortname);
                    }
                  else
                    {
@@ -1932,8 +1953,7 @@ elf_link_add_object_symbols (abfd, info)
                  && ! new_weakdef
                  && h->weakdef->dynindx == -1)
                {
-                 if (! _bfd_elf_link_record_dynamic_symbol (info,
-                                                            h->weakdef))
+                 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
                    goto error_return;
                }
            }
@@ -1947,6 +1967,8 @@ elf_link_add_object_symbols (abfd, info)
              case STV_HIDDEN:
                h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
                (*bed->elf_backend_hide_symbol) (info, h);
+               _bfd_elf_strtab_delref (hash_table->dynstr,
+                                       h->dynstr_index);
                break;
              }
 
@@ -1957,25 +1979,26 @@ elf_link_add_object_symbols (abfd, info)
              bfd_size_type oldsize;
              bfd_size_type strindex;
 
+             if (! is_elf_hash_table (info))
+               goto error_return;
+
              /* The symbol from a DT_NEEDED object is referenced from
                 the regular object to create a dynamic executable. We
                 have to make sure there is a DT_NEEDED entry for it.  */
 
              dt_needed = false;
-             oldsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
-             strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
-                                            elf_dt_soname (abfd),
-                                            true, false);
+             oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
+             strindex = _bfd_elf_strtab_add (hash_table->dynstr,
+                                             elf_dt_soname (abfd), false);
              if (strindex == (bfd_size_type) -1)
                goto error_return;
 
-             if (oldsize
-                 == _bfd_stringtab_size (elf_hash_table (info)->dynstr))
+             if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
                {
                  asection *sdyn;
                  Elf_External_Dyn *dyncon, *dynconend;
 
-                 sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
+                 sdyn = bfd_get_section_by_name (hash_table->dynobj,
                                                  ".dynamic");
                  BFD_ASSERT (sdyn != NULL);
 
@@ -1986,14 +2009,14 @@ elf_link_add_object_symbols (abfd, info)
                    {
                      Elf_Internal_Dyn dyn;
 
-                     elf_swap_dyn_in (elf_hash_table (info)->dynobj,
+                     elf_swap_dyn_in (hash_table->dynobj,
                                       dyncon, &dyn);
                      BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
                                  dyn.d_un.d_val != strindex);
                    }
                }
 
-             if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
+             if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NEEDED, strindex))
                goto error_return;
            }
        }
@@ -2143,6 +2166,7 @@ elf_link_add_object_symbols (abfd, info)
       && ! info->relocateable
       && ! info->traditional_format
       && info->hash->creator->flavour == bfd_target_elf_flavour
+      && is_elf_hash_table (info)
       && (info->strip != strip_all && info->strip != strip_debugger))
     {
       asection *stab, *stabstr;
@@ -2158,7 +2182,7 @@ elf_link_add_object_symbols (abfd, info)
 
              secdata = elf_section_data (stab);
              if (! _bfd_link_section_stabs (abfd,
-                                            &elf_hash_table (info)->stab_info,
+                                            & hash_table->stab_info,
                                             stab, stabstr,
                                             &secdata->stab_info))
                goto error_return;
@@ -2166,15 +2190,15 @@ elf_link_add_object_symbols (abfd, info)
        }
     }
 
-  if (! info->relocateable && ! dynamic)
+  if (! info->relocateable && ! dynamic
+      && is_elf_hash_table (info))
     {
       asection *s;
 
       for (s = abfd->sections; s != NULL; s = s->next)
        if ((s->flags & SEC_MERGE)
-           && ! _bfd_merge_section (abfd,
-                                    &elf_hash_table (info)->merge_info,
-                                    s, &elf_section_data (s)->merge_info))
+           && ! _bfd_merge_section (abfd, & hash_table->merge_info, s,
+                                    & elf_section_data (s)->merge_info))
          goto error_return;
     }
 
@@ -2207,6 +2231,9 @@ elf_link_create_dynamic_sections (abfd, info)
   struct elf_link_hash_entry *h;
   struct elf_backend_data *bed;
 
+  if (! is_elf_hash_table (info))
+    return false;
+
   if (elf_hash_table (info)->dynamic_sections_created)
     return true;
 
@@ -2265,7 +2292,7 @@ elf_link_create_dynamic_sections (abfd, info)
   /* Create a strtab to hold the dynamic symbol names.  */
   if (elf_hash_table (info)->dynstr == NULL)
     {
-      elf_hash_table (info)->dynstr = elf_stringtab_init ();
+      elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
       if (elf_hash_table (info)->dynstr == NULL)
        return false;
     }
@@ -2328,9 +2355,12 @@ elf_add_dynamic_entry (info, tag, val)
   Elf_Internal_Dyn dyn;
   bfd *dynobj;
   asection *s;
-  size_t newsize;
+  bfd_size_type newsize;
   bfd_byte *newcontents;
 
+  if (! is_elf_hash_table (info))
+    return false;
+
   dynobj = elf_hash_table (info)->dynobj;
 
   s = bfd_get_section_by_name (dynobj, ".dynamic");
@@ -2362,10 +2392,15 @@ elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
 {
   struct elf_link_local_dynamic_entry *entry;
   struct elf_link_hash_table *eht;
-  struct bfd_strtab_hash *dynstr;
+  struct elf_strtab_hash *dynstr;
   Elf_External_Sym esym;
   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)
@@ -2373,17 +2408,15 @@ elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
       return true;
 
   entry = (struct elf_link_local_dynamic_entry *)
-    bfd_alloc (input_bfd, sizeof (*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.  */
-  if (bfd_seek (input_bfd,
-               (elf_tdata (input_bfd)->symtab_hdr.sh_offset
-                + input_indx * sizeof (Elf_External_Sym)),
-               SEEK_SET) != 0
-      || (bfd_read (&esym, sizeof (Elf_External_Sym), 1, input_bfd)
-         != sizeof (Elf_External_Sym)))
+  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 (&esym, amt, input_bfd) != amt)
     return false;
   elf_swap_symbol_in (input_bfd, &esym, &entry->isym);
 
@@ -2395,12 +2428,12 @@ elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
   if (dynstr == NULL)
     {
       /* Create a strtab to hold the dynamic symbol names.  */
-      elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init ();
+      elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
       if (dynstr == NULL)
        return false;
     }
 
-  dynstr_index = _bfd_stringtab_add (dynstr, name, true, false);
+  dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
   if (dynstr_index == (unsigned long) -1)
     return false;
   entry->isym.st_name = dynstr_index;
@@ -2440,6 +2473,7 @@ elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
      Elf_Internal_Rela *internal_relocs;
 {
   struct elf_backend_data *bed;
+  bfd_size_type amt;
 
   /* If there aren't any relocations, that's OK.  */
   if (!shdr)
@@ -2450,8 +2484,7 @@ elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
     return false;
 
   /* Read the relocations.  */
-  if (bfd_read (external_relocs, 1, shdr->sh_size, abfd)
-      != shdr->sh_size)
+  if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
     return false;
 
   bed = get_elf_backend_data (abfd);
@@ -2467,8 +2500,8 @@ elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
       erel = (Elf_External_Rel *) external_relocs;
       erelend = erel + NUM_SHDR_ENTRIES (shdr);
       irela = internal_relocs;
-      irel = bfd_alloc (abfd, (bed->s->int_rels_per_ext_rel
-                              * sizeof (Elf_Internal_Rel)));
+      amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
+      irel = bfd_alloc (abfd, amt);
       for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
        {
          unsigned int i;
@@ -2543,10 +2576,10 @@ NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
 
   if (internal_relocs == NULL)
     {
-      size_t size;
+      bfd_size_type size;
 
-      size = (o->reloc_count * bed->s->int_rels_per_ext_rel
-             * sizeof (Elf_Internal_Rela));
+      size = o->reloc_count;
+      size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
       if (keep_memory)
        internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
       else
@@ -2557,10 +2590,10 @@ NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
 
   if (external_relocs == NULL)
     {
-      size_t size = (size_t) rel_hdr->sh_size;
+      bfd_size_type size = rel_hdr->sh_size;
 
       if (elf_section_data (o)->rel_hdr2)
-       size += (size_t) elf_section_data (o)->rel_hdr2->sh_size;
+       size += elf_section_data (o)->rel_hdr2->sh_size;
       alloc1 = (PTR) bfd_malloc (size);
       if (alloc1 == NULL)
        goto error_return;
@@ -2602,7 +2635,6 @@ NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
 /* Record an assignment to a symbol made by a linker script.  We need
    this in case some dynamic object refers to this symbol.  */
 
-/*ARGSUSED*/
 boolean
 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
      bfd *output_bfd ATTRIBUTE_UNUSED;
@@ -2620,7 +2652,7 @@ NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
     return false;
 
   if (h->root.type == bfd_link_hash_new)
-    h->elf_link_hash_flags &=ELF_LINK_NON_ELF;
+    h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
 
   /* If this symbol is being provided by the linker script, and it is
      currently defined by a dynamic object, but not by a regular
@@ -2642,7 +2674,7 @@ 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 */
+  /* When possible, keep the original type of the symbol */
   if (h->type == STT_NOTYPE)
     h->type = STT_OBJECT;
 
@@ -2679,8 +2711,6 @@ struct elf_assign_sym_version_info
   struct bfd_link_info *info;
   /* Version tree.  */
   struct bfd_elf_version_tree *verdefs;
-  /* Whether we are exporting all dynamic symbols.  */
-  boolean export_dynamic;
   /* Whether we had a failure.  */
   boolean failed;
 };
@@ -2729,12 +2759,14 @@ compute_bucket_count (info)
   unsigned long int *hashcodes;
   unsigned long int *hashcodesp;
   unsigned long int i;
+  bfd_size_type amt;
 
   /* Compute the hash values for all exported symbols.  At the same
      time store the values in an array so that we could use them for
      optimizations.  */
-  hashcodes = (unsigned long int *) bfd_malloc (dynsymcount
-                                               * sizeof (unsigned long int));
+  amt = dynsymcount;
+  amt *= sizeof (unsigned long int);
+  hashcodes = (unsigned long int *) bfd_malloc (amt);
   if (hashcodes == NULL)
     return 0;
   hashcodesp = hashcodes;
@@ -2765,8 +2797,9 @@ compute_bucket_count (info)
 
       /* Create array where we count the collisions in.  We must use bfd_malloc
         since the size could be large.  */
-      counts = (unsigned long int *) bfd_malloc (maxsize
-                                                * sizeof (unsigned long int));
+      amt = maxsize;
+      amt *= sizeof (unsigned long int);
+      counts = (unsigned long int *) bfd_malloc (amt);
       if (counts == NULL)
        {
          free (hashcodes);
@@ -2862,13 +2895,12 @@ compute_bucket_count (info)
 
 boolean
 NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
-                                    export_dynamic, filter_shlib,
+                                    filter_shlib,
                                     auxiliary_filters, info, sinterpptr,
                                     verdefs)
      bfd *output_bfd;
      const char *soname;
      const char *rpath;
-     boolean export_dynamic;
      const char *filter_shlib;
      const char * const *auxiliary_filters;
      struct bfd_link_info *info;
@@ -2887,6 +2919,13 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
   if (info->hash->creator->flavour != bfd_target_elf_flavour)
     return true;
 
+  if (! is_elf_hash_table (info))
+    return false;
+
+  /* Any syms created from now on start with -1 in
+     got.refcount/offset and plt.refcount/offset.  */
+  elf_hash_table (info)->init_refcount = -1;
+
   /* The backend may have to create some sections regardless of whether
      we're dynamic or not.  */
   bed = get_elf_backend_data (output_bfd);
@@ -2912,16 +2951,18 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
 
       if (soname != NULL)
        {
-         soname_indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
-                                           soname, true, true);
+         soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
+                                            soname, true);
          if (soname_indx == (bfd_size_type) -1
-             || ! elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
+             || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SONAME,
+                                         soname_indx))
            return false;
        }
 
       if (info->symbolic)
        {
-         if (! elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
+         if (! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMBOLIC,
+                                      (bfd_vma) 0))
            return false;
          info->flags |= DF_SYMBOLIC;
        }
@@ -2930,12 +2971,15 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
        {
          bfd_size_type indx;
 
-         indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, rpath,
-                                    true, true);
+         indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
+                                     true);
+         if (info->new_dtags)
+           _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
          if (indx == (bfd_size_type) -1
-             || ! elf_add_dynamic_entry (info, DT_RPATH, indx)
+             || ! elf_add_dynamic_entry (info, (bfd_vma) DT_RPATH, indx)
              || (info->new_dtags
-                 && ! elf_add_dynamic_entry (info, DT_RUNPATH, indx)))
+                 && ! elf_add_dynamic_entry (info, (bfd_vma) DT_RUNPATH,
+                                             indx)))
            return false;
        }
 
@@ -2943,10 +2987,10 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
        {
          bfd_size_type indx;
 
-         indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
-                                    filter_shlib, true, true);
+         indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
+                                     filter_shlib, true);
          if (indx == (bfd_size_type) -1
-             || ! elf_add_dynamic_entry (info, DT_FILTER, indx))
+             || ! elf_add_dynamic_entry (info, (bfd_vma) DT_FILTER, indx))
            return false;
        }
 
@@ -2958,20 +3002,22 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
            {
              bfd_size_type indx;
 
-             indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
-                                        *p, true, true);
+             indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
+                                         *p, true);
              if (indx == (bfd_size_type) -1
-                 || ! elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
+                 || ! elf_add_dynamic_entry (info, (bfd_vma) DT_AUXILIARY,
+                                             indx))
                return false;
            }
        }
 
       eif.info = info;
+      eif.verdefs = verdefs;
       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.  */
-      if (export_dynamic)
+      if (info->export_dynamic)
        {
          elf_link_hash_traverse (elf_hash_table (info), elf_export_symbol,
                                  (PTR) &eif);
@@ -2983,7 +3029,6 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
       asvinfo.output_bfd = output_bfd;
       asvinfo.info = info;
       asvinfo.verdefs = verdefs;
-      asvinfo.export_dynamic = export_dynamic;
       asvinfo.failed = false;
 
       elf_link_hash_traverse (elf_hash_table (info),
@@ -3015,7 +3060,7 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
          && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
                                        | ELF_LINK_HASH_DEF_REGULAR)) != 0)
        {
-         if (! elf_add_dynamic_entry (info, DT_INIT, 0))
+         if (! elf_add_dynamic_entry (info, (bfd_vma) DT_INIT, (bfd_vma) 0))
            return false;
        }
       h = (info->fini_function
@@ -3027,7 +3072,7 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
          && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
                                        | ELF_LINK_HASH_DEF_REGULAR)) != 0)
        {
-         if (! elf_add_dynamic_entry (info, DT_FINI, 0))
+         if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FINI, (bfd_vma) 0))
            return false;
        }
 
@@ -3040,13 +3085,13 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
        {
          bfd_size_type strsize;
 
-         strsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
-         if (! elf_add_dynamic_entry (info, DT_HASH, 0)
-             || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
-             || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
-             || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize)
-             || ! elf_add_dynamic_entry (info, DT_SYMENT,
-                                         sizeof (Elf_External_Sym)))
+         strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
+         if (! elf_add_dynamic_entry (info, (bfd_vma) DT_HASH, (bfd_vma) 0)
+             || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRTAB, (bfd_vma) 0)
+             || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMTAB, (bfd_vma) 0)
+             || ! elf_add_dynamic_entry (info, (bfd_vma) DT_STRSZ, strsize)
+             || ! elf_add_dynamic_entry (info, (bfd_vma) DT_SYMENT,
+                                         (bfd_vma) sizeof (Elf_External_Sym)))
            return false;
        }
     }
@@ -3059,10 +3104,11 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
 
   if (elf_hash_table (info)->dynamic_sections_created)
     {
-      size_t dynsymcount;
+      bfd_size_type dynsymcount;
       asection *s;
       size_t bucketcount = 0;
       size_t hash_entry_size;
+      unsigned int dtagcount;
 
       /* Set up the version definition section.  */
       s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
@@ -3122,6 +3168,8 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
 
          if (soname_indx != (bfd_size_type) -1)
            {
+             _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
+                                     soname_indx);
              def.vd_hash = bfd_elf_hash (soname);
              defaux.vda_name = soname_indx;
            }
@@ -3132,8 +3180,8 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
 
              name = basename (output_bfd->filename);
              def.vd_hash = bfd_elf_hash (name);
-             indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
-                                           name, true, false);
+             indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
+                                         name, false);
              if (indx == (bfd_size_type) -1)
                return false;
              defaux.vda_name = indx;
@@ -3141,7 +3189,7 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
          defaux.vda_next = 0;
 
          _bfd_elf_swap_verdef_out (output_bfd, &def,
-                                   (Elf_External_Verdef *)p);
+                                   (Elf_External_Verdef *) p);
          p += sizeof (Elf_External_Verdef);
          _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
                                     (Elf_External_Verdaux *) p);
@@ -3192,6 +3240,8 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
              p += sizeof (Elf_External_Verdef);
 
              defaux.vda_name = h->dynstr_index;
+             _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
+                                     h->dynstr_index);
              if (t->deps == NULL)
                defaux.vda_next = 0;
              else
@@ -3211,7 +3261,11 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
                      defaux.vda_name = 0;
                    }
                  else
-                   defaux.vda_name = n->version_needed->name_indx;
+                   {
+                     defaux.vda_name = n->version_needed->name_indx;
+                     _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
+                                             defaux.vda_name);
+                   }
                  if (n->next == NULL)
                    defaux.vda_next = 0;
                  else
@@ -3223,8 +3277,9 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
                }
            }
 
-         if (! elf_add_dynamic_entry (info, DT_VERDEF, 0)
-             || ! elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
+         if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEF, (bfd_vma) 0)
+             || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERDEFNUM,
+                                         (bfd_vma) cdefs))
            return false;
 
          elf_tdata (output_bfd)->cverdefs = cdefs;
@@ -3232,7 +3287,7 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
 
       if (info->new_dtags && info->flags)
        {
-         if (! elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
+         if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS, info->flags))
            return false;
        }
 
@@ -3242,7 +3297,8 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
            info->flags_1 &= ~ (DF_1_INITFIRST
                                | DF_1_NODELETE
                                | DF_1_NOOPEN);
-         if (! elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
+         if (! elf_add_dynamic_entry (info, (bfd_vma) DT_FLAGS_1,
+                                      info->flags_1))
            return false;
        }
 
@@ -3289,7 +3345,7 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
              }
 
            s->_raw_size = size;
-           s->contents = (bfd_byte *) bfd_alloc (output_bfd, size);
+           s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
            if (s->contents == NULL)
              return false;
 
@@ -3308,14 +3364,11 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
 
                t->vn_version = VER_NEED_CURRENT;
                t->vn_cnt = caux;
-               if (elf_dt_name (t->vn_bfd) != NULL)
-                 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
-                                            elf_dt_name (t->vn_bfd),
-                                            true, false);
-               else
-                 indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
-                                            basename (t->vn_bfd->filename),
-                                            true, false);
+               indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
+                                           elf_dt_name (t->vn_bfd) != NULL
+                                           ? elf_dt_name (t->vn_bfd)
+                                           : basename (t->vn_bfd->filename),
+                                           false);
                if (indx == (bfd_size_type) -1)
                  return false;
                t->vn_file = indx;
@@ -3333,8 +3386,8 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
                for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
                  {
                    a->vna_hash = bfd_elf_hash (a->vna_nodename);
-                   indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
-                                              a->vna_nodename, true, false);
+                   indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
+                                               a->vna_nodename, false);
                    if (indx == (bfd_size_type) -1)
                      return false;
                    a->vna_name = indx;
@@ -3349,8 +3402,10 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
                  }
              }
 
-           if (! elf_add_dynamic_entry (info, DT_VERNEED, 0)
-               || ! elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
+           if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEED,
+                                        (bfd_vma) 0)
+               || ! elf_add_dynamic_entry (info, (bfd_vma) DT_VERNEEDNUM,
+                                           (bfd_vma) crefs))
              return false;
 
            elf_tdata (output_bfd)->cverrefs = crefs;
@@ -3382,7 +3437,7 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
          if (s->contents == NULL)
            return false;
 
-         if (! elf_add_dynamic_entry (info, DT_VERSYM, 0))
+         if (! elf_add_dynamic_entry (info, (bfd_vma) DT_VERSYM, (bfd_vma) 0))
            return false;
        }
 
@@ -3427,23 +3482,172 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath,
        return false;
       memset (s->contents, 0, (size_t) s->_raw_size);
 
-      bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
-      bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
+      bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) bucketcount,
+              s->contents);
+      bfd_put (8 * hash_entry_size, output_bfd, (bfd_vma) dynsymcount,
               s->contents + hash_entry_size);
 
       elf_hash_table (info)->bucketcount = bucketcount;
 
       s = bfd_get_section_by_name (dynobj, ".dynstr");
       BFD_ASSERT (s != NULL);
-      s->_raw_size = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
 
-      if (! elf_add_dynamic_entry (info, DT_NULL, 0))
-       return false;
+      elf_finalize_dynstr (output_bfd, info);
+
+      s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
+
+      for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
+       if (! elf_add_dynamic_entry (info, (bfd_vma) DT_NULL, (bfd_vma) 0))
+         return false;
     }
 
   return true;
 }
 \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;
+     PTR data;
+{
+  struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
+
+  if (h->dynindx != -1)
+    h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
+  return true;
+}
+
+/* Assign string offsets in .dynstr, update all structures referencing
+   them.  */
+
+static boolean
+elf_finalize_dynstr (output_bfd, info)
+     bfd *output_bfd;
+     struct bfd_link_info *info;
+{
+  struct elf_link_local_dynamic_entry *entry;
+  struct elf_strtab_hash *dynstr = elf_hash_table (info)->dynstr;
+  bfd *dynobj = elf_hash_table (info)->dynobj;
+  asection *sdyn;
+  bfd_size_type size;
+  Elf_External_Dyn *dyncon, *dynconend;
+
+  _bfd_elf_strtab_finalize (dynstr);
+  size = _bfd_elf_strtab_size (dynstr);
+
+  /* Update all .dynamic entries referencing .dynstr strings.  */
+  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
+  BFD_ASSERT (sdyn != NULL);
+
+  dyncon = (Elf_External_Dyn *) sdyn->contents;
+  dynconend = (Elf_External_Dyn *) (sdyn->contents +
+                                   sdyn->_raw_size);
+  for (; dyncon < dynconend; dyncon++)
+    {
+      Elf_Internal_Dyn dyn;
+
+      elf_swap_dyn_in (dynobj, dyncon, & dyn);
+      switch (dyn.d_tag)
+       {
+       case DT_STRSZ:
+         dyn.d_un.d_val = size;
+         elf_swap_dyn_out (dynobj, & dyn, dyncon);
+         break;
+       case DT_NEEDED:
+       case DT_SONAME:
+       case DT_RPATH:
+       case DT_RUNPATH:
+       case DT_FILTER:
+       case DT_AUXILIARY:
+         dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
+         elf_swap_dyn_out (dynobj, & dyn, dyncon);
+         break;
+       default:
+         break;
+       }
+    }
+
+  /* Now update local dynamic symbols.  */
+  for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
+    entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
+                                                 entry->isym.st_name);
+
+  /* And the rest of dynamic symbols.  */
+  elf_link_hash_traverse (elf_hash_table (info),
+                         elf_adjust_dynstr_offsets, dynstr);
+
+  /* Adjust version definitions.  */
+  if (elf_tdata (output_bfd)->cverdefs)
+    {
+      asection *s;
+      bfd_byte *p;
+      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
+       {
+         _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
+                                  &def);
+         p += sizeof (Elf_External_Verdef);
+         for (i = 0; i < def.vd_cnt; ++i)
+           {
+             _bfd_elf_swap_verdaux_in (output_bfd,
+                                       (Elf_External_Verdaux *) p, &defaux);
+             defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
+                                                       defaux.vda_name);
+             _bfd_elf_swap_verdaux_out (output_bfd,
+                                        &defaux, (Elf_External_Verdaux *) p);
+             p += sizeof (Elf_External_Verdaux);
+           }
+       }
+      while (def.vd_next);
+    }
+
+  /* Adjust version references.  */
+  if (elf_tdata (output_bfd)->verref)
+    {
+      asection *s;
+      bfd_byte *p;
+      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
+       {
+         _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
+                                   &need);
+         need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
+         _bfd_elf_swap_verneed_out (output_bfd, &need,
+                                    (Elf_External_Verneed *) p);
+         p += sizeof (Elf_External_Verneed);
+         for (i = 0; i < need.vn_cnt; ++i)
+           {
+             _bfd_elf_swap_vernaux_in (output_bfd,
+                                       (Elf_External_Vernaux *) p, &needaux);
+             needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
+                                                        needaux.vna_name);
+             _bfd_elf_swap_vernaux_out (output_bfd,
+                                        &needaux,
+                                        (Elf_External_Vernaux *) p);
+             p += sizeof (Elf_External_Vernaux);
+           }
+       }
+      while (need.vn_next);
+    }
+
+  return true;
+}
+
 /* Fix up the flags for a symbol.  This handles various cases which
    can only be fixed after all the input files are seen.  This is
    currently called by both adjust_dynamic_symbol and
@@ -3532,16 +3736,22 @@ elf_fix_symbol_flags (h, eif)
      backend specifically; we can't just clear PLT-related data here.  */
   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
       && eif->info->shared
+      && is_elf_hash_table (eif->info)
       && (eif->info->symbolic
          || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
          || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
     {
       struct elf_backend_data *bed;
+
       bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
       if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
          || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
-       h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
+       {
+         h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
+         _bfd_elf_strtab_delref (elf_hash_table (eif->info)->dynstr,
+                                 h->dynstr_index);
+       }
       (*bed->elf_backend_hide_symbol) (eif->info, h);
     }
 
@@ -3565,11 +3775,12 @@ elf_fix_symbol_flags (h, eif)
       if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
        h->weakdef = NULL;
       else
-       weakdef->elf_link_hash_flags |=
-         (h->elf_link_hash_flags
-          & (ELF_LINK_HASH_REF_REGULAR
-             | ELF_LINK_HASH_REF_REGULAR_NONWEAK
-             | ELF_LINK_NON_GOT_REF));
+       {
+         struct elf_backend_data *bed;
+
+         bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
+         (*bed->elf_backend_copy_indirect_symbol) (weakdef, h);
+       }
     }
 
   return true;
@@ -3592,6 +3803,9 @@ elf_adjust_dynamic_symbol (h, data)
   if (h->root.type == bfd_link_hash_indirect)
     return true;
 
+  if (! is_elf_hash_table (eif->info))
+    return false;
+
   /* Fix the symbol flags.  */
   if (! elf_fix_symbol_flags (h, eif))
     return false;
@@ -3705,11 +3919,39 @@ elf_export_symbol (h, data)
       && (h->elf_link_hash_flags
          & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
     {
-      if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
+      struct bfd_elf_version_tree *t;
+      struct bfd_elf_version_expr *d;
+
+      for (t = eif->verdefs; t != NULL; t = t->next)
        {
-         eif->failed = true;
-         return false;
+         if (t->globals != NULL)
+           {
+             for (d = t->globals; d != NULL; d = d->next)
+               {
+                 if ((*d->match) (d, h->root.root.string))
+                   goto doit;
+               }
+           }
+
+         if (t->locals != NULL)
+           {
+             for (d = t->locals ; d != NULL; d = d->next)
+               {
+                 if ((*d->match) (d, h->root.root.string))
+                   return true;
+               }
+           }
        }
+
+      if (!eif->verdefs)
+        {
+doit:
+         if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
+           {
+             eif->failed = true;
+             return false;
+           }
+        }
     }
 
   return true;
@@ -3728,6 +3970,7 @@ elf_link_find_version_dependencies (h, data)
   struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
   Elf_Internal_Verneed *t;
   Elf_Internal_Vernaux *a;
+  bfd_size_type amt;
 
   /* We only care about symbols defined in shared objects with version
      information.  */
@@ -3754,7 +3997,8 @@ elf_link_find_version_dependencies (h, data)
 
   if (t == NULL)
     {
-      t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, sizeof *t);
+      amt = sizeof *t;
+      t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
       if (t == NULL)
        {
          rinfo->failed = true;
@@ -3766,7 +4010,8 @@ elf_link_find_version_dependencies (h, data)
       elf_tdata (rinfo->output_bfd)->verref = t;
     }
 
-  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, sizeof *a);
+  amt = sizeof *a;
+  a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
 
   /* Note that we are copying a string pointer here, and testing it
      above.  If bfd_elf_string_from_elf_section is ever changed to
@@ -3797,12 +4042,15 @@ elf_link_assign_sym_version (h, data)
      struct elf_link_hash_entry *h;
      PTR data;
 {
-  struct elf_assign_sym_version_info *sinfo =
-    (struct elf_assign_sym_version_info *) data;
-  struct bfd_link_info *info = sinfo->info;
+  struct elf_assign_sym_version_info *sinfo;
+  struct bfd_link_info *info;
   struct elf_backend_data *bed;
   struct elf_info_failed eif;
   char *p;
+  bfd_size_type amt;
+
+  sinfo = (struct elf_assign_sym_version_info *) data;
+  info = sinfo->info;
 
   /* Fix the symbol flags.  */
   eif.failed = false;
@@ -3850,12 +4098,12 @@ elf_link_assign_sym_version (h, data)
        {
          if (strcmp (t->name, p) == 0)
            {
-             int len;
+             size_t len;
              char *alc;
              struct bfd_elf_version_expr *d;
 
              len = p - h->root.root.string;
-             alc = bfd_alloc (sinfo->output_bfd, len);
+             alc = bfd_alloc (sinfo->output_bfd, (bfd_size_type) len);
              if (alc == NULL)
                return false;
              strncpy (alc, h->root.root.string, len - 1);
@@ -3884,13 +4132,12 @@ elf_link_assign_sym_version (h, data)
                        {
                          if (h->dynindx != -1
                              && info->shared
-                             && ! sinfo->export_dynamic)
+                             && ! info->export_dynamic)
                            {
                              h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
                              (*bed->elf_backend_hide_symbol) (info, h);
-                             /* FIXME: The name of the symbol has
-                                already been recorded in the dynamic
-                                string table section.  */
+                             _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
+                                                     h->dynstr_index);
                            }
 
                          break;
@@ -3915,8 +4162,9 @@ elf_link_assign_sym_version (h, data)
          if (h->dynindx == -1)
            return true;
 
+         amt = sizeof *t;
          t = ((struct bfd_elf_version_tree *)
-              bfd_alloc (sinfo->output_bfd, sizeof *t));
+              bfd_alloc (sinfo->output_bfd, amt));
          if (t == NULL)
            {
              sinfo->failed = true;
@@ -3996,13 +4244,12 @@ elf_link_assign_sym_version (h, data)
                      h->verinfo.vertree = t;
                      if (h->dynindx != -1
                          && info->shared
-                         && ! sinfo->export_dynamic)
+                         && ! info->export_dynamic)
                        {
                          h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
                          (*bed->elf_backend_hide_symbol) (info, h);
-                         /* FIXME: The name of the symbol has already
-                            been recorded in the dynamic string table
-                            section.  */
+                         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
+                                                 h->dynstr_index);
                        }
                      break;
                    }
@@ -4018,12 +4265,12 @@ elf_link_assign_sym_version (h, data)
          h->verinfo.vertree = deflt;
          if (h->dynindx != -1
              && info->shared
-             && ! sinfo->export_dynamic)
+             && ! info->export_dynamic)
            {
              h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
              (*bed->elf_backend_hide_symbol) (info, h);
-             /* FIXME: The name of the symbol has already been
-                recorded in the dynamic string table section.  */
+             _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
+                                     h->dynstr_index);
            }
        }
     }
@@ -4108,7 +4355,8 @@ elf_link_size_reloc_section (abfd, rel_hdr, o)
      Elf_Internal_Shdr *rel_hdr;
      asection *o;
 {
-  unsigned reloc_count;
+  bfd_size_type reloc_count;
+  bfd_size_type num_rel_hashes;
 
   /* Figure out how many relocations there will be.  */
   if (rel_hdr == &elf_section_data (o)->rel_hdr)
@@ -4116,6 +4364,10 @@ elf_link_size_reloc_section (abfd, rel_hdr, o)
   else
     reloc_count = elf_section_data (o)->rel_count2;
 
+  num_rel_hashes = o->reloc_count;
+  if (num_rel_hashes < reloc_count)
+    num_rel_hashes = reloc_count;
+
   /* That allows us to calculate the size of the section.  */
   rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
 
@@ -4129,14 +4381,15 @@ elf_link_size_reloc_section (abfd, rel_hdr, o)
 
   /* We only allocate one set of hash entries, so we only do it the
      first time we are called.  */
-  if (elf_section_data (o)->rel_hashes == NULL)
+  if (elf_section_data (o)->rel_hashes == NULL
+      && num_rel_hashes)
     {
       struct elf_link_hash_entry **p;
 
       p = ((struct elf_link_hash_entry **)
-          bfd_zmalloc (o->reloc_count
+          bfd_zmalloc (num_rel_hashes
                        * sizeof (struct elf_link_hash_entry *)));
-      if (p == NULL && o->reloc_count != 0)
+      if (p == NULL)
        return false;
 
       elf_section_data (o)->rel_hashes = p;
@@ -4161,17 +4414,17 @@ elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
   struct elf_backend_data *bed = get_elf_backend_data (abfd);
   Elf_Internal_Rel *irel;
   Elf_Internal_Rela *irela;
+  bfd_size_type amt = sizeof (Elf_Internal_Rel) * bed->s->int_rels_per_ext_rel;
 
-  irel = (Elf_Internal_Rel *) bfd_zmalloc (sizeof (Elf_Internal_Rel)
-                                          * bed->s->int_rels_per_ext_rel);
+  irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
   if (irel == NULL)
     {
       (*_bfd_error_handler) (_("Error: out of memory"));
       abort ();
     }
 
-  irela = (Elf_Internal_Rela *) bfd_zmalloc (sizeof (Elf_Internal_Rela)
-                                            * bed->s->int_rels_per_ext_rel);
+  amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
+  irela = (Elf_Internal_Rela *) bfd_zmalloc (amt);
   if (irela == NULL)
     {
       (*_bfd_error_handler) (_("Error: out of memory"));
@@ -4234,6 +4487,214 @@ elf_link_adjust_relocs (abfd, rel_hdr, count, rel_hash)
   free (irela);
 }
 
+struct elf_link_sort_rela {
+  bfd_vma offset;
+  enum elf_reloc_type_class type;
+  union {
+    Elf_Internal_Rel rel;
+    Elf_Internal_Rela rela;
+  } u;
+};
+
+static int
+elf_link_sort_cmp1 (A, B)
+     const PTR A;
+     const PTR B;
+{
+  struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
+  struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
+  int relativea, relativeb;
+
+  relativea = a->type == reloc_class_relative;
+  relativeb = b->type == reloc_class_relative;
+
+  if (relativea < relativeb)
+    return 1;
+  if (relativea > relativeb)
+    return -1;
+  if (ELF_R_SYM (a->u.rel.r_info) < ELF_R_SYM (b->u.rel.r_info))
+    return -1;
+  if (ELF_R_SYM (a->u.rel.r_info) > ELF_R_SYM (b->u.rel.r_info))
+    return 1;
+  if (a->u.rel.r_offset < b->u.rel.r_offset)
+    return -1;
+  if (a->u.rel.r_offset > b->u.rel.r_offset)
+    return 1;
+  return 0;
+}
+
+static int
+elf_link_sort_cmp2 (A, B)
+     const PTR A;
+     const PTR B;
+{
+  struct elf_link_sort_rela *a = (struct elf_link_sort_rela *) A;
+  struct elf_link_sort_rela *b = (struct elf_link_sort_rela *) B;
+  int copya, copyb;
+
+  if (a->offset < b->offset)
+    return -1;
+  if (a->offset > b->offset)
+    return 1;
+  copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
+  copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
+  if (copya < copyb)
+    return -1;
+  if (copya > copyb)
+    return 1;
+  if (a->u.rel.r_offset < b->u.rel.r_offset)
+    return -1;
+  if (a->u.rel.r_offset > b->u.rel.r_offset)
+    return 1;
+  return 0;
+}
+
+static size_t
+elf_link_sort_relocs (abfd, info, psec)
+     bfd *abfd;
+     struct bfd_link_info *info;
+     asection **psec;
+{
+  bfd *dynobj = elf_hash_table (info)->dynobj;
+  asection *reldyn, *o;
+  boolean rel = false;
+  bfd_size_type count, size;
+  size_t i, j, ret;
+  struct elf_link_sort_rela *rela;
+  struct elf_backend_data *bed = get_elf_backend_data (abfd);
+
+  reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
+  if (reldyn == NULL || reldyn->_raw_size == 0)
+    {
+      reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
+      if (reldyn == NULL || reldyn->_raw_size == 0)
+       return 0;
+      rel = true;
+      count = reldyn->_raw_size / sizeof (Elf_External_Rel);
+    }
+  else
+    count = reldyn->_raw_size / sizeof (Elf_External_Rela);
+
+  size = 0;
+  for (o = dynobj->sections; o != NULL; o = o->next)
+    if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
+       == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
+       && o->output_section == reldyn)
+      size += o->_raw_size;
+
+  if (size != reldyn->_raw_size)
+    return 0;
+
+  rela = (struct elf_link_sort_rela *) bfd_zmalloc (sizeof (*rela) * count);
+  if (rela == NULL)
+    {
+      (*info->callbacks->warning)
+       (info, _("Not enough memory to sort relocations"), 0, abfd, 0,
+        (bfd_vma) 0);
+      return 0;
+    }
+
+  for (o = dynobj->sections; o != NULL; o = o->next)
+    if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
+       == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
+       && o->output_section == reldyn)
+      {
+       if (rel)
+         {
+           Elf_External_Rel *erel, *erelend;
+           struct elf_link_sort_rela *s;
+
+           erel = (Elf_External_Rel *) o->contents;
+           erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
+           s = rela + o->output_offset / sizeof (Elf_External_Rel);
+           for (; erel < erelend; erel++, s++)
+             {
+               if (bed->s->swap_reloc_in)
+                 (*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, &s->u.rel);
+               else
+                 elf_swap_reloc_in (abfd, erel, &s->u.rel);
+
+               s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
+             }
+         }
+       else
+         {
+           Elf_External_Rela *erela, *erelaend;
+           struct elf_link_sort_rela *s;
+
+           erela = (Elf_External_Rela *) o->contents;
+           erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
+           s = rela + o->output_offset / sizeof (Elf_External_Rela);
+           for (; erela < erelaend; erela++, s++)
+             {
+               if (bed->s->swap_reloca_in)
+                 (*bed->s->swap_reloca_in) (dynobj, (bfd_byte *) erela,
+                                            &s->u.rela);
+               else
+                 elf_swap_reloca_in (dynobj, erela, &s->u.rela);
+
+               s->type = (*bed->elf_backend_reloc_type_class) (&s->u.rela);
+             }
+         }
+      }
+
+  qsort (rela, (size_t) count, sizeof (*rela), elf_link_sort_cmp1);
+  for (ret = 0; ret < count && rela[ret].type == reloc_class_relative; ret++)
+    ;
+  for (i = ret, j = ret; i < count; i++)
+    {
+      if (ELF_R_SYM (rela[i].u.rel.r_info) != ELF_R_SYM (rela[j].u.rel.r_info))
+       j = i;
+      rela[i].offset = rela[j].u.rel.r_offset;
+    }
+  qsort (rela + ret, (size_t) count - ret, sizeof (*rela), elf_link_sort_cmp2);
+
+  for (o = dynobj->sections; o != NULL; o = o->next)
+    if ((o->flags & (SEC_HAS_CONTENTS|SEC_LINKER_CREATED))
+       == (SEC_HAS_CONTENTS|SEC_LINKER_CREATED)
+       && o->output_section == reldyn)
+      {
+       if (rel)
+         {
+           Elf_External_Rel *erel, *erelend;
+           struct elf_link_sort_rela *s;
+
+           erel = (Elf_External_Rel *) o->contents;
+           erelend = (Elf_External_Rel *) (o->contents + o->_raw_size);
+           s = rela + o->output_offset / sizeof (Elf_External_Rel);
+           for (; erel < erelend; erel++, s++)
+             {
+               if (bed->s->swap_reloc_out)
+                 (*bed->s->swap_reloc_out) (abfd, &s->u.rel,
+                                            (bfd_byte *) erel);
+               else
+                 elf_swap_reloc_out (abfd, &s->u.rel, erel);
+             }
+         }
+       else
+         {
+           Elf_External_Rela *erela, *erelaend;
+           struct elf_link_sort_rela *s;
+
+           erela = (Elf_External_Rela *) o->contents;
+           erelaend = (Elf_External_Rela *) (o->contents + o->_raw_size);
+           s = rela + o->output_offset / sizeof (Elf_External_Rela);
+           for (; erela < erelaend; erela++, s++)
+             {
+               if (bed->s->swap_reloca_out)
+                 (*bed->s->swap_reloca_out) (dynobj, &s->u.rela,
+                                             (bfd_byte *) erela);
+               else
+                 elf_swap_reloca_out (dynobj, &s->u.rela, erela);
+             }
+         }
+      }
+
+  free (rela);
+  *psec = reldyn;
+  return ret;
+}
+
 /* Do the final step of an ELF link.  */
 
 boolean
@@ -4242,15 +4703,16 @@ elf_bfd_final_link (abfd, info)
      struct bfd_link_info *info;
 {
   boolean dynamic;
+  boolean emit_relocs;
   bfd *dynobj;
   struct elf_final_link_info finfo;
   register asection *o;
   register struct bfd_link_order *p;
   register bfd *sub;
-  size_t max_contents_size;
-  size_t max_external_reloc_size;
-  size_t max_internal_reloc_count;
-  size_t max_sym_count;
+  bfd_size_type max_contents_size;
+  bfd_size_type max_external_reloc_size;
+  bfd_size_type max_internal_reloc_count;
+  bfd_size_type max_sym_count;
   file_ptr off;
   Elf_Internal_Sym elfsym;
   unsigned int i;
@@ -4259,6 +4721,12 @@ elf_bfd_final_link (abfd, info)
   struct elf_backend_data *bed = get_elf_backend_data (abfd);
   struct elf_outext_info eoinfo;
   boolean merged;
+  size_t relativecount = 0;
+  asection *reldyn = 0;
+  bfd_size_type amt;
+
+  if (! is_elf_hash_table (info))
+    return false;
 
   if (info->shared)
     abfd->flags |= DYNAMIC;
@@ -4266,6 +4734,10 @@ elf_bfd_final_link (abfd, info)
   dynamic = elf_hash_table (info)->dynamic_sections_created;
   dynobj = elf_hash_table (info)->dynobj;
 
+  emit_relocs = (info->relocateable
+                 || info->emitrelocations
+                 || bed->elf_backend_emit_relocs);
+
   finfo.info = info;
   finfo.output_bfd = abfd;
   finfo.symstrtab = elf_stringtab_init ();
@@ -4331,6 +4803,20 @@ elf_bfd_final_link (abfd, info)
 
              if (info->relocateable || info->emitrelocations)
                o->reloc_count += sec->reloc_count;
+              else if (bed->elf_backend_count_relocs)
+               {
+                 Elf_Internal_Rela * relocs;
+
+                 relocs = (NAME(_bfd_elf,link_read_relocs)
+                           (abfd, sec, (PTR) NULL,
+                            (Elf_Internal_Rela *) NULL, info->keep_memory));
+
+                 o->reloc_count += (*bed->elf_backend_count_relocs)
+                                     (sec, relocs);
+
+                 if (!info->keep_memory)
+                   free (relocs);
+               }
 
              if (sec->_raw_size > max_contents_size)
                max_contents_size = sec->_raw_size;
@@ -4401,7 +4887,7 @@ elf_bfd_final_link (abfd, info)
   /* Figure out how many relocations we will have in each section.
      Just using RELOC_COUNT isn't good enough since that doesn't
      maintain a separate value for REL vs. RELA relocations.  */
-  if (info->relocateable || info->emitrelocations)
+  if (emit_relocs)
     for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
       for (o = sub->sections; o != NULL; o = o->next)
        {
@@ -4441,6 +4927,7 @@ elf_bfd_final_link (abfd, info)
              *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
              if (esdi->rel_hdr2)
                *rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
+             output_section->flags |= SEC_RELOC;
            }
        }
 
@@ -4500,14 +4987,16 @@ elf_bfd_final_link (abfd, info)
     finfo.symbuf_size = 20;
   else
     finfo.symbuf_size = max_sym_count;
-  finfo.symbuf = ((Elf_External_Sym *)
-                 bfd_malloc (finfo.symbuf_size * sizeof (Elf_External_Sym)));
+  amt = finfo.symbuf_size;
+  amt *= sizeof (Elf_External_Sym);
+  finfo.symbuf = (Elf_External_Sym *) bfd_malloc (amt);
   if (finfo.symbuf == NULL)
     goto error_return;
 
   /* Start writing out the symbol table.  The first symbol is always a
      dummy symbol.  */
-  if (info->strip != strip_all || info->relocateable || info->emitrelocations)
+  if (info->strip != strip_all
+      || emit_relocs)
     {
       elfsym.st_value = 0;
       elfsym.st_size = 0;
@@ -4540,7 +5029,8 @@ elf_bfd_final_link (abfd, info)
      symbols have no names.  We store the index of each one in the
      index field of the section, so that we can find it again when
      outputting relocs.  */
-  if (info->strip != strip_all || info->relocateable || info->emitrelocations)
+  if (info->strip != strip_all
+      || emit_relocs)
     {
       elfsym.st_size = 0;
       elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
@@ -4669,7 +5159,7 @@ elf_bfd_final_link (abfd, info)
     {
       Elf_Internal_Sym sym;
       Elf_External_Sym *dynsym =
-       (Elf_External_Sym *)finfo.dynsym_sec->contents;
+       (Elf_External_Sym *) finfo.dynsym_sec->contents;
       long last_local = 0;
 
       /* Write out the section symbols for the output sections.  */
@@ -4749,11 +5239,12 @@ elf_bfd_final_link (abfd, info)
      table, do it now.  */
   if (bed->elf_backend_output_arch_syms)
     {
-      if (! (*bed->elf_backend_output_arch_syms)
-             (abfd, info, (PTR) &finfo,
-              (boolean (*) PARAMS ((PTR, const char *,
-                           Elf_Internal_Sym *, asection *)))
-              elf_link_output_sym))
+      typedef boolean (*out_sym_func) PARAMS ((PTR, const char *,
+                                              Elf_Internal_Sym *,
+                                              asection *));
+
+      if (! ((*bed->elf_backend_output_arch_syms)
+            (abfd, info, (PTR) &finfo, (out_sym_func) elf_link_output_sym)))
        return false;
     }
 
@@ -4808,6 +5299,9 @@ elf_bfd_final_link (abfd, info)
       o->reloc_count = 0;
     }
 
+  if (dynamic && info->combreloc && dynobj != NULL)
+    relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
+
   /* If we are linking against a dynamic object, or generating a
      shared library, finish up the dynamic linking information.  */
   if (dynamic)
@@ -4832,6 +5326,23 @@ elf_bfd_final_link (abfd, info)
            {
            default:
              break;
+           case DT_NULL:
+             if (relativecount > 0 && dyncon + 1 < dynconend)
+               {
+                 switch (elf_section_data (reldyn)->this_hdr.sh_type)
+                   {
+                   case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
+                   case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
+                   default: break;
+                   }
+                 if (dyn.d_tag != DT_NULL)
+                   {
+                     dyn.d_un.d_val = relativecount;
+                     elf_swap_dyn_out (dynobj, &dyn, dyncon);
+                     relativecount = 0;
+                   }
+               }
+             break;
            case DT_INIT:
              name = info->init_function;
              goto get_sym;
@@ -4944,20 +5455,19 @@ elf_bfd_final_link (abfd, info)
              || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
            {
              if (! bfd_set_section_contents (abfd, o->output_section,
-                                             o->contents, o->output_offset,
+                                             o->contents,
+                                             (file_ptr) o->output_offset,
                                              o->_raw_size))
                goto error_return;
            }
          else
            {
-             file_ptr off;
-
              /* The contents of the .dynstr section are actually in a
                  stringtab.  */
              off = elf_section_data (o->output_section)->this_hdr.sh_offset;
              if (bfd_seek (abfd, off, SEEK_SET) != 0
-                 || ! _bfd_stringtab_emit (abfd,
-                                           elf_hash_table (info)->dynstr))
+                 || ! _bfd_elf_strtab_emit (abfd,
+                                            elf_hash_table (info)->dynstr))
                goto error_return;
            }
        }
@@ -4992,7 +5502,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;
@@ -5059,8 +5569,7 @@ elf_link_output_sym (finfo, name, elfsym, input_sec)
   else
     {
       elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
-                                                           name, true,
-                                                           false);
+                                                           name, true, false);
       if (elfsym->st_name == (unsigned long) -1)
        return false;
     }
@@ -5089,14 +5598,14 @@ elf_link_flush_output_syms (finfo)
   if (finfo->symbuf_count > 0)
     {
       Elf_Internal_Shdr *symtab;
+      file_ptr pos;
+      bfd_size_type amt;
 
       symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr;
-
-      if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size,
-                   SEEK_SET) != 0
-         || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count,
-                        sizeof (Elf_External_Sym), finfo->output_bfd)
-             != finfo->symbuf_count * sizeof (Elf_External_Sym)))
+      pos = symtab->sh_offset + symtab->sh_size;
+      amt = finfo->symbuf_count * sizeof (Elf_External_Sym);
+      if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
+         || bfd_bwrite ((PTR) finfo->symbuf, amt, finfo->output_bfd) != amt)
        return false;
 
       symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym);
@@ -5173,15 +5682,14 @@ elf_link_output_extsym (h, data)
      warnings for them.  */
   if (! finfo->info->relocateable
       && ! finfo->info->allow_shlib_undefined
-      && ! (finfo->info->shared
-           && !finfo->info->no_undefined)
+      && ! 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)
     {
       if (! ((*finfo->info->callbacks->undefined_symbol)
             (finfo->info, h->root.root.string, h->root.u.undef.abfd,
-             (asection *) NULL, 0, true)))
+             (asection *) NULL, (bfd_vma) 0, true)))
        {
          eoinfo->failed = true;
          return false;
@@ -5332,8 +5840,8 @@ elf_link_output_extsym (h, data)
      finish_dynamic_symbol routine gets through with it.  */
   if (sym.st_shndx == SHN_UNDEF
       && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
-      && (ELF_ST_BIND(sym.st_info) == STB_GLOBAL
-         || ELF_ST_BIND(sym.st_info) == STB_WEAK))
+      && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
+         || ELF_ST_BIND (sym.st_info) == STB_WEAK))
     {
       int bindtype;
 
@@ -5346,8 +5854,9 @@ elf_link_output_extsym (h, data)
 
   /* If a symbol is not defined locally, we clear the visibility
      field.  */
-  if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
-    sym.st_other ^= ELF_ST_VISIBILITY(sym.st_other);
+  if (! finfo->info->relocateable
+      && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
+    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
@@ -5360,13 +5869,11 @@ elf_link_output_extsym (h, data)
       size_t hash_entry_size;
       bfd_byte *bucketpos;
       bfd_vma chain;
+      Elf_External_Sym *esym;
 
       sym.st_name = h->dynstr_index;
-
-      elf_swap_symbol_out (finfo->output_bfd, &sym,
-                          (PTR) (((Elf_External_Sym *)
-                                  finfo->dynsym_sec->contents)
-                                 + h->dynindx));
+      esym = (Elf_External_Sym *) finfo->dynsym_sec->contents + h->dynindx;
+      elf_swap_symbol_out (finfo->output_bfd, &sym, (PTR) esym);
 
       bucketcount = elf_hash_table (finfo->info)->bucketcount;
       bucket = h->elf_hash_value % bucketcount;
@@ -5375,7 +5882,8 @@ elf_link_output_extsym (h, data)
       bucketpos = ((bfd_byte *) finfo->hash_sec->contents
                   + (bucket + 2) * hash_entry_size);
       chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
-      bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
+      bfd_put (8 * hash_entry_size, finfo->output_bfd, (bfd_vma) h->dynindx,
+              bucketpos);
       bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
               ((bfd_byte *) finfo->hash_sec->contents
                + (bucketcount + 2 + h->dynindx) * hash_entry_size));
@@ -5383,6 +5891,7 @@ elf_link_output_extsym (h, data)
       if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
        {
          Elf_Internal_Versym iversym;
+         Elf_External_Versym *eversym;
 
          if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
            {
@@ -5402,10 +5911,9 @@ elf_link_output_extsym (h, data)
          if ((h->elf_link_hash_flags & ELF_LINK_HIDDEN) != 0)
            iversym.vs_vers |= VERSYM_HIDDEN;
 
-         _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym,
-                                   (((Elf_External_Versym *)
-                                     finfo->symver_sec->contents)
-                                    + h->dynindx));
+         eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
+         eversym += h->dynindx;
+         _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
        }
     }
 
@@ -5443,6 +5951,7 @@ elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
   asection *output_section;
   unsigned int *rel_countp = NULL;
   struct elf_backend_data *bed;
+  bfd_size_type amt;
 
   output_section = input_section->output_section;
   output_rel_hdr = NULL;
@@ -5472,9 +5981,9 @@ elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
     {
       Elf_External_Rel *erel;
       Elf_Internal_Rel *irel;
-      
-      irel = (Elf_Internal_Rel *) bfd_zmalloc (bed->s->int_rels_per_ext_rel
-                                              * sizeof (Elf_Internal_Rel));
+
+      amt = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
+      irel = (Elf_Internal_Rel *) bfd_zmalloc (amt);
       if (irel == NULL)
        {
          (*_bfd_error_handler) (_("Error: out of memory"));
@@ -5485,7 +5994,7 @@ elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
       for (; irela < irelaend; irela += bed->s->int_rels_per_ext_rel, erel++)
        {
          unsigned int i;
-         
+
          for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
            {
              irel[i].r_offset = irela[i].r_offset;
@@ -5546,6 +6055,8 @@ elf_link_input_bfd (finfo, input_bfd)
   asection **ppsection;
   asection *o;
   struct elf_backend_data *bed;
+  boolean emit_relocs;
+  struct elf_link_hash_entry **sym_hashes;
 
   output_bfd = finfo->output_bfd;
   bed = get_elf_backend_data (output_bfd);
@@ -5557,6 +6068,10 @@ elf_link_input_bfd (finfo, input_bfd)
   if ((input_bfd->flags & DYNAMIC) != 0)
     return true;
 
+  emit_relocs = (finfo->info->relocateable
+                 || finfo->info->emitrelocations
+                 || bed->elf_backend_emit_relocs);
+
   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   if (elf_bad_symtab (input_bfd))
     {
@@ -5576,11 +6091,10 @@ elf_link_input_bfd (finfo, input_bfd)
     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_read (external_syms, sizeof (Elf_External_Sym),
-                       locsymcount, input_bfd)
-             != locsymcount * sizeof (Elf_External_Sym)))
+         || bfd_bread (external_syms, amt, input_bfd) != amt)
        return false;
     }
 
@@ -5609,12 +6123,8 @@ elf_link_input_bfd (finfo, input_bfd)
            }
        }
 
-      name = NULL;
       if (isym->st_shndx == SHN_UNDEF)
-        {
-         isec = bfd_und_section_ptr;
-         name = isec->name;
-       }
+       isec = bfd_und_section_ptr;
       else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE)
        {
          isec = section_from_elf_index (input_bfd, isym->st_shndx);
@@ -5626,15 +6136,9 @@ elf_link_input_bfd (finfo, input_bfd)
                                          isym->st_value, (bfd_vma) 0);
        }
       else if (isym->st_shndx == SHN_ABS)
-       {
-         isec = bfd_abs_section_ptr;
-         name = isec->name;
-       }
+       isec = bfd_abs_section_ptr;
       else if (isym->st_shndx == SHN_COMMON)
-       {
-         isec = bfd_com_section_ptr;
-         name = isec->name;
-       }
+       isec = bfd_com_section_ptr;
       else
        {
          /* Who knows?  */
@@ -5649,41 +6153,6 @@ elf_link_input_bfd (finfo, input_bfd)
 
       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
        {
-         asection *ksec;
-
-         /* Save away all section symbol values.  */
-         if (isec != NULL)
-           {
-             if (name)
-               {
-                 if (isec->symbol->value != isym->st_value)
-                   (*_bfd_error_handler)
-                     (_("%s: invalid section symbol index 0x%x (%s) ingored"),
-                      bfd_get_filename (input_bfd), isym->st_shndx,
-                      name);
-                 continue;
-               }
-             isec->symbol->value = isym->st_value;
-           }
-
-         /* If this is a discarded link-once section symbol, update
-            it's value to that of the kept section symbol.  The
-            linker will keep the first of any matching link-once
-            sections, so we should have already seen it's section
-            symbol.  I trust no-one will have the bright idea of
-            re-ordering the bfd list...  */
-         if (isec != NULL
-             && (bfd_get_section_flags (input_bfd, isec) & SEC_LINK_ONCE) != 0
-             && (ksec = isec->kept_section) != NULL)
-           {
-             isym->st_value = ksec->symbol->value;
-
-             /* That put the value right, but the section info is all
-                wrong.  I hope this works.  */
-             isec->output_offset = ksec->output_offset;
-             isec->output_section = ksec->output_section;
-           }
-
          /* We never output section symbols.  Instead, we use the
             section symbol of the corresponding section in the output
             file.  */
@@ -5760,6 +6229,7 @@ elf_link_input_bfd (finfo, input_bfd)
     }
 
   /* Relocate the contents of each section.  */
+  sym_hashes = elf_sym_hashes (input_bfd);
   for (o = input_bfd->sections; o != NULL; o = o->next)
     {
       bfd_byte *contents;
@@ -5807,6 +6277,122 @@ elf_link_input_bfd (finfo, input_bfd)
              && o->reloc_count > 0)
            return false;
 
+         /* Run through the relocs looking for any against symbols
+            from discarded sections and section symbols from
+            removed link-once sections.  Complain about relocs
+            against discarded sections.  Zero relocs against removed
+            link-once sections.  We should really complain if
+            anything in the final link tries to use it, but
+            DWARF-based exception handling might have an entry in
+            .eh_frame to describe a routine in the linkonce section,
+            and it turns out to be hard to remove the .eh_frame
+            entry too.  FIXME.  */
+         if (!finfo->info->relocateable)
+           {
+             Elf_Internal_Rela *rel, *relend;
+
+             rel = internal_relocs;
+             relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
+             for ( ; rel < relend; rel++)
+               {
+                 unsigned long r_symndx = ELF_R_SYM (rel->r_info);
+
+                 if (r_symndx >= locsymcount
+                     || (elf_bad_symtab (input_bfd)
+                         && finfo->sections[r_symndx] == NULL))
+                   {
+                     struct elf_link_hash_entry *h;
+
+                     h = sym_hashes[r_symndx - extsymoff];
+                     while (h->root.type == bfd_link_hash_indirect
+                            || h->root.type == bfd_link_hash_warning)
+                       h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
+                     /* Complain if the definition comes from a
+                        discarded section.  */
+                     if ((h->root.type == bfd_link_hash_defined
+                          || h->root.type == bfd_link_hash_defweak)
+                         && ! bfd_is_abs_section (h->root.u.def.section)
+                         && bfd_is_abs_section (h->root.u.def.section
+                                                ->output_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
+                             memset (rel, 0, sizeof (*rel));
+                           }
+                         else
+#endif
+                           {
+                             if (! ((*finfo->info->callbacks->undefined_symbol)
+                                    (finfo->info, h->root.root.string,
+                                     input_bfd, o, rel->r_offset,
+                                     true)))
+                               return false;
+                           }
+                       }
+                   }
+                 else
+                   {
+                     isym = finfo->internal_syms + r_symndx;
+                     if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
+                       {
+                         asection *sec = finfo->sections[r_symndx];
+
+                         if (sec != NULL
+                             && ! bfd_is_abs_section (sec)
+                             && bfd_is_abs_section (sec->output_section))
+                           {
+#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
+                                 rel->r_info
+                                   = ELF_R_INFO (0, ELF_R_TYPE (rel->r_info));
+                                 rel->r_addend = 0;
+                               }
+                             else
+#endif
+                               {
+                                 boolean ok;
+                                 const char *msg
+                                   = _("local symbols in discarded section %s");
+                                 bfd_size_type amt
+                                   = strlen (sec->name) + strlen (msg) - 1;
+                                 char *buf = (char *) bfd_malloc (amt);
+
+                                 if (buf != NULL)
+                                   sprintf (buf, msg, sec->name);
+                                 else
+                                   buf = (char *) sec->name;
+                                 ok = (*finfo->info->callbacks
+                                       ->undefined_symbol) (finfo->info, buf,
+                                                            input_bfd, o,
+                                                            rel->r_offset,
+                                                            true);
+                                 if (buf != sec->name)
+                                   free (buf);
+                                 if (!ok)
+                                   return false;
+                               }
+                           }
+                       }
+                   }
+               }
+           }
+
          /* Relocate the section by invoking a back end routine.
 
             The back end routine is responsible for adjusting the
@@ -5834,26 +6420,27 @@ elf_link_input_bfd (finfo, input_bfd)
                                     finfo->sections))
            return false;
 
-         if (finfo->info->relocateable || finfo->info->emitrelocations)
+         if (emit_relocs)
            {
              Elf_Internal_Rela *irela;
              Elf_Internal_Rela *irelaend;
              struct elf_link_hash_entry **rel_hash;
              Elf_Internal_Shdr *input_rel_hdr;
              unsigned int next_erel;
+             void (*reloc_emitter) PARAMS ((bfd *, asection *,
+                                            Elf_Internal_Shdr *,
+                                            Elf_Internal_Rela *));
 
              /* Adjust the reloc addresses and symbol indices.  */
 
              irela = internal_relocs;
-             irelaend = irela
-                        + o->reloc_count * bed->s->int_rels_per_ext_rel;
+             irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
              rel_hash = (elf_section_data (o->output_section)->rel_hashes
                          + elf_section_data (o->output_section)->rel_count
                          + elf_section_data (o->output_section)->rel_count2);
              for (next_erel = 0; irela < irelaend; irela++, next_erel++)
                {
                  unsigned long r_symndx;
-                 Elf_Internal_Sym *isym;
                  asection *sec;
 
                  if (next_erel == bed->s->int_rels_per_ext_rel)
@@ -5936,7 +6523,7 @@ elf_link_input_bfd (finfo, input_bfd)
                    {
                      if (finfo->indices[r_symndx] == -1)
                        {
-                         unsigned long link;
+                         unsigned long shlink;
                          const char *name;
                          asection *osec;
 
@@ -5950,10 +6537,9 @@ elf_link_input_bfd (finfo, input_bfd)
                          /* This symbol was skipped earlier, but
                             since it is needed by a reloc, we
                             must output it now.  */
-                         link = symtab_hdr->sh_link;
-                         name = bfd_elf_string_from_elf_section (input_bfd,
-                                                                 link,
-                                                                 isym->st_name);
+                         shlink = symtab_hdr->sh_link;
+                         name = (bfd_elf_string_from_elf_section
+                                 (input_bfd, shlink, isym->st_name));
                          if (name == NULL)
                            return false;
 
@@ -5968,7 +6554,8 @@ elf_link_input_bfd (finfo, input_bfd)
                          if (! finfo->info->relocateable)
                            isym->st_value += osec->vma;
 
-                         finfo->indices[r_symndx] = bfd_get_symcount (output_bfd);
+                         finfo->indices[r_symndx]
+                           = bfd_get_symcount (output_bfd);
 
                          if (! elf_link_output_sym (finfo, name, isym, sec))
                            return false;
@@ -5982,17 +6569,24 @@ elf_link_input_bfd (finfo, input_bfd)
                }
 
              /* Swap out the relocs.  */
+              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;
+
              input_rel_hdr = &elf_section_data (o)->rel_hdr;
-             elf_link_output_relocs (output_bfd, o,
-                                     input_rel_hdr,
-                                     internal_relocs);
-             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);
+
              input_rel_hdr = elf_section_data (o)->rel_hdr2;
-             if (input_rel_hdr)
-               elf_link_output_relocs (output_bfd, o,
-                                       input_rel_hdr,
-                                       internal_relocs);
+              if (input_rel_hdr)
+                {
+                  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);
+                }
+
            }
        }
 
@@ -6012,12 +6606,14 @@ elf_link_input_bfd (finfo, input_bfd)
        }
       else
        {
-         if (! (o->flags & SEC_EXCLUDE) &&
-             ! bfd_set_section_contents (output_bfd, o->output_section,
-                                         contents, o->output_offset,
-                                         (o->_cooked_size != 0
-                                          ? o->_cooked_size
-                                          : o->_raw_size)))
+         bfd_size_type sec_size;
+
+         sec_size = (o->_cooked_size != 0 ? o->_cooked_size : o->_raw_size);
+         if (! (o->flags & SEC_EXCLUDE)
+             && ! bfd_set_section_contents (output_bfd, o->output_section,
+                                            contents,
+                                            (file_ptr) o->output_offset,
+                                            sec_size))
            return false;
        }
     }
@@ -6114,28 +6710,31 @@ elf_reloc_link_order (output_bfd, info, output_section, link_order)
       bfd_reloc_status_type rstat;
       bfd_byte *buf;
       boolean ok;
+      const char *sym_name;
 
       size = bfd_get_reloc_size (howto);
       buf = (bfd_byte *) bfd_zmalloc (size);
       if (buf == (bfd_byte *) NULL)
        return false;
-      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
+      rstat = _bfd_relocate_contents (howto, output_bfd, (bfd_vma) addend, buf);
       switch (rstat)
        {
        case bfd_reloc_ok:
          break;
+
        default:
        case bfd_reloc_outofrange:
          abort ();
+
        case bfd_reloc_overflow:
+         if (link_order->type == bfd_section_reloc_link_order)
+           sym_name = bfd_section_name (output_bfd,
+                                        link_order->u.reloc.p->u.section);
+         else
+           sym_name = link_order->u.reloc.p->u.name;
          if (! ((*info->callbacks->reloc_overflow)
-                (info,
-                 (link_order->type == bfd_section_reloc_link_order
-                  ? bfd_section_name (output_bfd,
-                                      link_order->u.reloc.p->u.section)
-                  : link_order->u.reloc.p->u.name),
-                 howto->name, addend, (bfd *) NULL, (asection *) NULL,
-                 (bfd_vma) 0)))
+                (info, sym_name, howto->name, addend,
+                 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
            {
              free (buf);
              return false;
@@ -6160,15 +6759,16 @@ elf_reloc_link_order (output_bfd, info, output_section, link_order)
 
   if (rel_hdr->sh_type == SHT_REL)
     {
+      bfd_size_type size;
       Elf_Internal_Rel *irel;
       Elf_External_Rel *erel;
       unsigned int i;
-      
-      irel = (Elf_Internal_Rel *) bfd_zmalloc (bed->s->int_rels_per_ext_rel
-                                              * sizeof (Elf_Internal_Rel));
+
+      size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rel);
+      irel = (Elf_Internal_Rel *) bfd_zmalloc (size);
       if (irel == NULL)
        return false;
-      
+
       for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
        irel[i].r_offset = offset;
       irel[0].r_info = ELF_R_INFO (indx, howto->type);
@@ -6185,12 +6785,13 @@ elf_reloc_link_order (output_bfd, info, output_section, link_order)
     }
   else
     {
+      bfd_size_type size;
       Elf_Internal_Rela *irela;
       Elf_External_Rela *erela;
       unsigned int i;
-      
-      irela = (Elf_Internal_Rela *) bfd_zmalloc (bed->s->int_rels_per_ext_rel
-                                                * sizeof (Elf_Internal_Rela));
+
+      size = bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
+      irela = (Elf_Internal_Rela *) bfd_zmalloc (size);
       if (irela == NULL)
        return false;
 
@@ -6225,14 +6826,15 @@ elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
 {
   elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
   elf_linker_section_pointers_t *linker_section_ptr;
-  unsigned long r_symndx = ELF_R_SYM (rel->r_info);;
+  unsigned long r_symndx = ELF_R_SYM (rel->r_info);
+  bfd_size_type amt;
 
   BFD_ASSERT (lsect != NULL);
 
-  /* Is this a global symbol? */
+  /* Is this a global symbol?  */
   if (h != NULL)
     {
-      /* Has this symbol already been allocated, if so, our work is done */
+      /* Has this symbol already been allocated?  If so, our work is done.  */
       if (_bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
                                                rel->r_addend,
                                                lsect->which))
@@ -6249,29 +6851,30 @@ elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
       if (lsect->rel_section)
        lsect->rel_section->_raw_size += sizeof (Elf_External_Rela);
     }
-
-  else  /* Allocation of a pointer to a local symbol */
+  else
     {
+      /* Allocation of a pointer to a local symbol.  */
       elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
 
-      /* Allocate a table to hold the local symbols if first time */
+      /* Allocate a table to hold the local symbols if first time */
       if (!ptr)
        {
          unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
          register unsigned int i;
 
-         ptr = (elf_linker_section_pointers_t **)
-           bfd_alloc (abfd, num_symbols * sizeof (elf_linker_section_pointers_t *));
+         amt = num_symbols;
+         amt *= sizeof (elf_linker_section_pointers_t *);
+         ptr = (elf_linker_section_pointers_t **) bfd_alloc (abfd, amt);
 
          if (!ptr)
            return false;
 
          elf_local_ptr_offsets (abfd) = ptr;
          for (i = 0; i < num_symbols; i++)
-           ptr[i] = (elf_linker_section_pointers_t *)0;
+           ptr[i] = (elf_linker_section_pointers_t *) 0;
        }
 
-      /* Has this symbol already been allocated, if so, our work is done */
+      /* Has this symbol already been allocated?  If so, our work is done.  */
       if (_bfd_elf_find_pointer_linker_section (ptr[r_symndx],
                                                rel->r_addend,
                                                lsect->which))
@@ -6289,11 +6892,11 @@ elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
        }
     }
 
-  /* Allocate space for a pointer in the linker section, and allocate a new pointer record
-     from internal memory.  */
+  /* Allocate space for a pointer in the linker section, and allocate
+     a new pointer record from internal memory.  */
   BFD_ASSERT (ptr_linker_section_ptr != NULL);
-  linker_section_ptr = (elf_linker_section_pointers_t *)
-    bfd_alloc (abfd, sizeof (elf_linker_section_pointers_t));
+  amt = sizeof (elf_linker_section_pointers_t);
+  linker_section_ptr = (elf_linker_section_pointers_t *) bfd_alloc (abfd, amt);
 
   if (!linker_section_ptr)
     return false;
@@ -6307,17 +6910,19 @@ elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
 #if 0
   if (lsect->hole_size && lsect->hole_offset < lsect->max_hole_offset)
     {
-      linker_section_ptr->offset = lsect->section->_raw_size - lsect->hole_size + (ARCH_SIZE / 8);
+      linker_section_ptr->offset = (lsect->section->_raw_size
+                                   - lsect->hole_size + (ARCH_SIZE / 8));
       lsect->hole_offset += ARCH_SIZE / 8;
       lsect->sym_offset  += ARCH_SIZE / 8;
-      if (lsect->sym_hash)     /* Bump up symbol value if needed */
+      if (lsect->sym_hash)
        {
+         /* Bump up symbol value if needed.  */
          lsect->sym_hash->root.u.def.value += ARCH_SIZE / 8;
 #ifdef DEBUG
          fprintf (stderr, "Bump up %s by %ld, current value = %ld\n",
                   lsect->sym_hash->root.root.string,
-                  (long)ARCH_SIZE / 8,
-                  (long)lsect->sym_hash->root.u.def.value);
+                  (long) ARCH_SIZE / 8,
+                  (long) lsect->sym_hash->root.u.def.value);
 #endif
        }
     }
@@ -6328,8 +6933,10 @@ elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
   lsect->section->_raw_size += ARCH_SIZE / 8;
 
 #ifdef DEBUG
-  fprintf (stderr, "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
-          lsect->name, (long)linker_section_ptr->offset, (long)lsect->section->_raw_size);
+  fprintf (stderr,
+          "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
+          lsect->name, (long) linker_section_ptr->offset,
+          (long) lsect->section->_raw_size);
 #endif
 
   return true;
@@ -6345,7 +6952,8 @@ elf_create_pointer_linker_section (abfd, info, lsect, h, rel)
 /* Fill in the address for a pointer generated in a linker section.  */
 
 bfd_vma
-elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, relocation, rel, relative_reloc)
+elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h,
+                                  relocation, rel, relative_reloc)
      bfd *output_bfd;
      bfd *input_bfd;
      struct bfd_link_info *info;
@@ -6359,11 +6967,13 @@ elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, reloca
 
   BFD_ASSERT (lsect != NULL);
 
-  if (h != NULL)               /* global symbol */
+  if (h != NULL)
     {
-      linker_section_ptr = _bfd_elf_find_pointer_linker_section (h->linker_section_pointer,
-                                                                rel->r_addend,
-                                                                lsect->which);
+      /* Handle global symbol.  */
+      linker_section_ptr = (_bfd_elf_find_pointer_linker_section
+                           (h->linker_section_pointer,
+                            rel->r_addend,
+                            lsect->which));
 
       BFD_ASSERT (linker_section_ptr != NULL);
 
@@ -6383,23 +6993,27 @@ elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, reloca
          if (!linker_section_ptr->written_address_p)
            {
              linker_section_ptr->written_address_p = true;
-             bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
-                         lsect->section->contents + linker_section_ptr->offset);
+             bfd_put_ptr (output_bfd,
+                          relocation + linker_section_ptr->addend,
+                          (lsect->section->contents
+                           + linker_section_ptr->offset));
            }
        }
     }
-  else                         /* local symbol */
+  else
     {
+      /* Handle local symbol.  */
       unsigned long r_symndx = ELF_R_SYM (rel->r_info);
       BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
       BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
-      linker_section_ptr = _bfd_elf_find_pointer_linker_section (elf_local_ptr_offsets (input_bfd)[r_symndx],
-                                                                rel->r_addend,
-                                                                lsect->which);
+      linker_section_ptr = (_bfd_elf_find_pointer_linker_section
+                           (elf_local_ptr_offsets (input_bfd)[r_symndx],
+                            rel->r_addend,
+                            lsect->which));
 
       BFD_ASSERT (linker_section_ptr != NULL);
 
-      /* Write out pointer if it hasn't been rewritten out before */
+      /* Write out pointer if it hasn't been rewritten out before */
       if (!linker_section_ptr->written_address_p)
        {
          linker_section_ptr->written_address_p = true;
@@ -6410,21 +7024,27 @@ elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, reloca
            {
              asection *srel = lsect->rel_section;
              Elf_Internal_Rela *outrel;
+             Elf_External_Rela *erel;
              struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
              unsigned int i;
+             bfd_size_type amt;
 
-             outrel = (Elf_Internal_Rela *) bfd_zmalloc (sizeof (Elf_Internal_Rela)
-                                                         * bed->s->int_rels_per_ext_rel);
+             amt = sizeof (Elf_Internal_Rela) * bed->s->int_rels_per_ext_rel;
+             outrel = (Elf_Internal_Rela *) bfd_zmalloc (amt);
              if (outrel == NULL)
                {
                  (*_bfd_error_handler) (_("Error: out of memory"));
                  return 0;
                }
 
-             /* We need to generate a relative reloc for the dynamic linker.  */
+             /* We need to generate a relative reloc for the dynamic
+                linker.  */
              if (!srel)
-               lsect->rel_section = srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
-                                                                    lsect->rel_name);
+               {
+                 srel = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
+                                                 lsect->rel_name);
+                 lsect->rel_section = srel;
+               }
 
              BFD_ASSERT (srel != NULL);
 
@@ -6434,12 +7054,11 @@ elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, reloca
                                      + linker_section_ptr->offset);
              outrel[0].r_info = ELF_R_INFO (0, relative_reloc);
              outrel[0].r_addend = 0;
-             elf_swap_reloca_out (output_bfd, outrel,
-                                  (((Elf_External_Rela *)
-                                    lsect->section->contents)
-                                   + elf_section_data (lsect->section)->rel_count));
+             erel = (Elf_External_Rela *) lsect->section->contents;
+             erel += elf_section_data (lsect->section)->rel_count;
+             elf_swap_reloca_out (output_bfd, outrel, erel);
              ++elf_section_data (lsect->section)->rel_count;
-             
+
              free (outrel);
            }
        }
@@ -6451,8 +7070,9 @@ elf_finish_pointer_linker_section (output_bfd, input_bfd, info, lsect, h, reloca
                - lsect->sym_offset);
 
 #ifdef DEBUG
-  fprintf (stderr, "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
-          lsect->name, (long)relocation, (long)relocation);
+  fprintf (stderr,
+          "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
+          lsect->name, (long) relocation, (long) relocation);
 #endif
 
   /* Subtract out the addend, because it will get added back in by the normal
@@ -6487,7 +7107,8 @@ static boolean elf_gc_smash_unused_vtentry_relocs
   PARAMS ((struct elf_link_hash_entry *h, PTR dummy));
 
 /* The mark phase of garbage collection.  For a given section, mark
-   it, and all the sections which define symbols to which it refers.  */
+   it and any sections in this section's group, and all the sections
+   which define symbols to which it refers.  */
 
 static boolean
 elf_gc_mark (info, sec, gc_mark_hook)
@@ -6497,12 +7118,19 @@ elf_gc_mark (info, sec, gc_mark_hook)
        PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
                struct elf_link_hash_entry *, Elf_Internal_Sym *));
 {
-  boolean ret = true;
+  boolean ret;
+  asection *group_sec;
 
   sec->gc_mark = 1;
 
-  /* Look through the section relocs.  */
+  /* Mark all the sections in the group.  */
+  group_sec = elf_section_data (sec)->next_in_group;
+  if (group_sec && !group_sec->gc_mark)
+    if (!elf_gc_mark (info, group_sec, gc_mark_hook))
+      return false;
 
+  /* Look through the section relocs.  */
+  ret = true;
   if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
     {
       Elf_Internal_Rela *relstart, *rel, *relend;
@@ -6534,13 +7162,11 @@ elf_gc_mark (info, sec, gc_mark_hook)
        locsyms = NULL;
       else
        {
-         locsyms = freesyms =
-           bfd_malloc (nlocsyms * sizeof (Elf_External_Sym));
+         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_read (locsyms, sizeof (Elf_External_Sym),
-                           nlocsyms, input_bfd)
-                 != nlocsyms * sizeof (Elf_External_Sym)))
+             || bfd_bread (locsyms, amt, input_bfd) != amt)
            {
              ret = false;
              goto out1;
@@ -6745,11 +7371,17 @@ elf_gc_propagate_vtable_entries_used (h, okp)
       pu = h->vtable_parent->vtable_entries_used;
       if (pu != NULL)
        {
-         n = h->vtable_parent->vtable_entries_size / FILE_ALIGN;
-         while (--n != 0)
+          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--)
            {
-             if (*pu) *cu = true;
-             pu++, cu++;
+             if (*pu)
+               *cu = true;
+             pu++;
+             cu++;
            }
        }
     }
@@ -6766,6 +7398,7 @@ elf_gc_smash_unused_vtentry_relocs (h, okp)
   bfd_vma hstart, hend;
   Elf_Internal_Rela *relstart, *relend, *rel;
   struct elf_backend_data *bed;
+  int file_align;
 
   /* Take care of both those symbols that do not describe vtables as
      well as those that are not loaded.  */
@@ -6782,8 +7415,10 @@ elf_gc_smash_unused_vtentry_relocs (h, okp)
   relstart = (NAME(_bfd_elf,link_read_relocs)
              (sec->owner, sec, NULL, (Elf_Internal_Rela *) NULL, true));
   if (!relstart)
-    return *(boolean *)okp = false;
+    return *(boolean *) okp = false;
   bed = get_elf_backend_data (sec->owner);
+  file_align = bed->s->file_align;
+
   relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
 
   for (rel = relstart; rel < relend; ++rel)
@@ -6793,7 +7428,7 @@ elf_gc_smash_unused_vtentry_relocs (h, okp)
        if (h->vtable_entries_used
            && (rel->r_offset - hstart) < h->vtable_entries_size)
          {
-           bfd_vma entry = (rel->r_offset - hstart) / FILE_ALIGN;
+           bfd_vma entry = (rel->r_offset - hstart) / file_align;
            if (h->vtable_entries_used[entry])
              continue;
          }
@@ -6814,7 +7449,7 @@ elf_gc_sections (abfd, info)
   boolean ok = true;
   bfd *sub;
   asection * (*gc_mark_hook)
-    PARAMS ((bfd *abfd, struct bfd_link_info *, Elf_Internal_Rela *,
+    PARAMS ((bfd *, 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
@@ -6855,7 +7490,7 @@ elf_gc_sections (abfd, info)
     }
 
   /* ... and mark SEC_EXCLUDE for those that go.  */
-  if (!elf_gc_sweep(info, get_elf_backend_data (abfd)->gc_sweep_hook))
+  if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
     return false;
 
   return true;
@@ -6897,12 +7532,12 @@ elf_gc_record_vtinherit (abfd, sec, h, offset)
     }
 
   (*_bfd_error_handler) ("%s: %s+%lu: No symbol found for INHERIT",
-                        bfd_get_filename (abfd), sec->name,
-                        (unsigned long)offset);
+                        bfd_archive_filename (abfd), sec->name,
+                        (unsigned long) offset);
   bfd_set_error (bfd_error_invalid_operation);
   return false;
 
-win:
+ win:
   if (!h)
     {
       /* This *should* only be the absolute section.  It could potentially
@@ -6927,6 +7562,9 @@ elf_gc_record_vtentry (abfd, sec, h, addend)
      struct elf_link_hash_entry *h;
      bfd_vma addend;
 {
+  struct elf_backend_data *bed = get_elf_backend_data (abfd);
+  int file_align = bed->s->file_align;
+
   if (addend >= h->vtable_entries_size)
     {
       size_t size, bytes;
@@ -6949,22 +7587,23 @@ elf_gc_record_vtentry (abfd, sec, h, addend)
 
       /* Allocate one extra entry for use as a "done" flag for the
         consolidation pass.  */
-      bytes = (size / FILE_ALIGN + 1) * sizeof (boolean);
+      bytes = (size / file_align + 1) * sizeof (boolean);
 
       if (ptr)
        {
-         ptr = bfd_realloc (ptr - 1, bytes);
+         ptr = bfd_realloc (ptr - 1, (bfd_size_type) bytes);
 
          if (ptr != NULL)
            {
              size_t oldbytes;
 
-             oldbytes = (h->vtable_entries_size/FILE_ALIGN + 1) * sizeof (boolean);
-             memset (((char *)ptr) + oldbytes, 0, bytes - oldbytes);
+             oldbytes = ((h->vtable_entries_size / file_align + 1)
+                         * sizeof (boolean));
+             memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
            }
        }
       else
-       ptr = bfd_zmalloc (bytes);
+       ptr = bfd_zmalloc ((bfd_size_type) bytes);
 
       if (ptr == NULL)
        return false;
@@ -6974,7 +7613,7 @@ elf_gc_record_vtentry (abfd, sec, h, addend)
       h->vtable_entries_size = size;
     }
 
-  h->vtable_entries_used[addend / FILE_ALIGN] = true;
+  h->vtable_entries_used[addend / file_align] = true;
 
   return true;
 }
@@ -7096,8 +7735,8 @@ elf_collect_hash_codes (h, data)
   p = strchr (name, ELF_VER_CHR);
   if (p != NULL)
     {
-      alc = bfd_malloc (p - name + 1);
-      memcpy (alc, name, p - name);
+      alc = bfd_malloc ((bfd_size_type) (p - name + 1));
+      memcpy (alc, name, (size_t) (p - name));
       alc[p - name] = '\0';
       name = alc;
     }
This page took 0.071134 seconds and 4 git commands to generate.