* config/tc-xtensa.h (resource_table): Change units to unsigned chars.
[deliverable/binutils-gdb.git] / bfd / elf.c
index dcb5c3d586a32d095d266cba41710001a00307f5..189e099fed6b90488cf4ebb5c010ca00fb9ff4c9 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -1,7 +1,7 @@
 /* ELF executable support for BFD.
 
    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
-   2002, 2003, 2004 Free Software Foundation, Inc.
+   2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
 
@@ -17,7 +17,7 @@
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 /*  SECTION
 
@@ -210,10 +210,10 @@ bfd_elf_hash (const char *namearg)
    file, into a newly allocated buffer, and return a pointer to the
    buffer.  */
 
-static char *
+static bfd_byte *
 elf_read (bfd *abfd, file_ptr offset, bfd_size_type size)
 {
-  char *buf;
+  bfd_byte *buf;
 
   if ((buf = bfd_alloc (abfd, size)) == NULL)
     return NULL;
@@ -253,15 +253,15 @@ char *
 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
 {
   Elf_Internal_Shdr **i_shdrp;
-  char *shstrtab = NULL;
+  bfd_byte *shstrtab = NULL;
   file_ptr offset;
   bfd_size_type shstrtabsize;
 
   i_shdrp = elf_elfsections (abfd);
   if (i_shdrp == 0 || i_shdrp[shindex] == 0)
-    return 0;
+    return NULL;
 
-  shstrtab = (char *) i_shdrp[shindex]->contents;
+  shstrtab = i_shdrp[shindex]->contents;
   if (shstrtab == NULL)
     {
       /* No cached one, attempt to read, and cache what we read.  */
@@ -270,7 +270,7 @@ bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
       shstrtab = elf_read (abfd, offset, shstrtabsize);
       i_shdrp[shindex]->contents = shstrtab;
     }
-  return shstrtab;
+  return (char *) shstrtab;
 }
 
 char *
@@ -291,13 +291,13 @@ bfd_elf_string_from_elf_section (bfd *abfd,
 
   if (strindex >= hdr->sh_size)
     {
+      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
       (*_bfd_error_handler)
        (_("%B: invalid string offset %u >= %lu for section `%s'"),
         abfd, strindex, (unsigned long) hdr->sh_size,
-        ((shindex == elf_elfheader(abfd)->e_shstrndx
-          && strindex == hdr->sh_name)
+        (shindex == shstrndx && strindex == hdr->sh_name
          ? ".shstrtab"
-         : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
+         : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
       return "";
     }
 
@@ -405,17 +405,31 @@ bfd_elf_get_elf_syms (bfd *ibfd,
 
 /* Look up a symbol name.  */
 const char *
-bfd_elf_local_sym_name (bfd *abfd, Elf_Internal_Sym *isym)
+bfd_elf_sym_name (bfd *abfd,
+                 Elf_Internal_Shdr *symtab_hdr,
+                 Elf_Internal_Sym *isym,
+                 asection *sym_sec)
 {
+  const char *name;
   unsigned int iname = isym->st_name;
-  unsigned int shindex = elf_tdata (abfd)->symtab_hdr.sh_link;
-  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION)
+  unsigned int shindex = symtab_hdr->sh_link;
+
+  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
+      /* Check for a bogus st_shndx to avoid crashing.  */
+      && isym->st_shndx < elf_numsections (abfd)
+      && !(isym->st_shndx >= SHN_LORESERVE && isym->st_shndx <= SHN_HIRESERVE))
     {
       iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
       shindex = elf_elfheader (abfd)->e_shstrndx;
     }
 
-  return bfd_elf_string_from_elf_section (abfd, shindex, iname);
+  name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
+  if (name == NULL)
+    name = "(null)";
+  else if (sym_sec && *name == '\0')
+    name = bfd_section_name (abfd, sym_sec);
+
+  return name;
 }
 
 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
@@ -438,8 +452,11 @@ group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
   Elf_External_Sym_Shndx eshndx;
   Elf_Internal_Sym isym;
 
-  /* First we need to ensure the symbol table is available.  */
-  if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
+  /* First we need to ensure the symbol table is available.  Make sure
+     that it is a symbol table section.  */
+  hdr = elf_elfsections (abfd) [ghdr->sh_link];
+  if (hdr->sh_type != SHT_SYMTAB
+      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
     return NULL;
 
   /* Go read the symbol.  */
@@ -448,7 +465,7 @@ group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
                            &isym, esym, &eshndx) == NULL)
     return NULL;
 
-  return bfd_elf_local_sym_name (abfd, &isym);
+  return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
 }
 
 /* Set next_in_group list pointer, and group name for NEWSECT.  */
@@ -476,14 +493,18 @@ setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
        }
 
       if (num_group == 0)
-       num_group = (unsigned) -1;
-      elf_tdata (abfd)->num_group = num_group;
-
-      if (num_group > 0)
+       {
+         num_group = (unsigned) -1;
+         elf_tdata (abfd)->num_group = num_group;
+       }
+      else
        {
          /* We keep a list of elf section headers for group sections,
             so we can find them quickly.  */
-         bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
+         bfd_size_type amt;
+         
+         elf_tdata (abfd)->num_group = num_group;
+         amt = num_group * sizeof (Elf_Internal_Shdr *);
          elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
          if (elf_tdata (abfd)->group_sect_ptr == NULL)
            return FALSE;
@@ -645,7 +666,10 @@ _bfd_elf_setup_group_pointers (bfd *abfd)
              (_("%B: unknown [%d] section `%s' in group [%s]"),
               abfd,
               (unsigned int) idx->shdr->sh_type,
-              elf_string_from_elf_strtab (abfd, idx->shdr->sh_name),
+              bfd_elf_string_from_elf_section (abfd,
+                                               (elf_elfheader (abfd)
+                                                ->e_shstrndx),
+                                               idx->shdr->sh_name),
               shdr->bfd_section->name);
            result = FALSE;
          }
@@ -659,36 +683,14 @@ bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
   return elf_next_in_group (sec) != NULL;
 }
 
-bfd_boolean
-bfd_elf_discard_group (bfd *abfd ATTRIBUTE_UNUSED,
-                      asection *group ATTRIBUTE_UNUSED)
-{
-#if 0
-  asection *first = elf_next_in_group (group);
-  asection *s = first;
-
-  while (s != NULL)
-    {
-      s->output_section = bfd_abs_section_ptr;
-      s = elf_next_in_group (s);
-      /* These lists are circular.  */
-      if (s == first)
-       break;
-    }
-#else
-  /* FIXME: Never used. Remove it!  */
-  abort ();
-#endif
-  return TRUE;
-}
-
 /* Make a BFD section from an ELF section.  We store a pointer to the
    BFD section in the bfd_section field of the header.  */
 
 bfd_boolean
 _bfd_elf_make_section_from_shdr (bfd *abfd,
                                 Elf_Internal_Shdr *hdr,
-                                const char *name)
+                                const char *name,
+                                int shindex)
 {
   asection *newsect;
   flagword flags;
@@ -707,6 +709,7 @@ _bfd_elf_make_section_from_shdr (bfd *abfd,
 
   hdr->bfd_section = newsect;
   elf_section_data (newsect)->this_hdr = *hdr;
+  elf_section_data (newsect)->this_idx = shindex;
 
   /* Always use the real type/flags.  */
   elf_section_type (newsect) = hdr->sh_type;
@@ -750,25 +753,45 @@ _bfd_elf_make_section_from_shdr (bfd *abfd,
   if ((hdr->sh_flags & SHF_TLS) != 0)
     flags |= SEC_THREAD_LOCAL;
 
-  /* The debugging sections appear to be recognized only by name, not
-     any sort of flag.  */
-  {
-    static const char *debug_sec_names [] =
+  if ((flags & SEC_ALLOC) == 0)
     {
-      ".debug",
-      ".gnu.linkonce.wi.",
-      ".line",
-      ".stab"
-    };
-    int i;
-
-    for (i = ARRAY_SIZE (debug_sec_names); i--;)
-      if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
-       break;
-
-    if (i >= 0)
-      flags |= SEC_DEBUGGING;
-  }
+      /* The debugging sections appear to be recognized only by name,
+        not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
+      static const struct
+       {
+         const char *name;
+         int len;
+       } debug_sections [] =
+       {
+         { "debug",             5  },  /* 'd' */
+         { NULL,                0  },  /* 'e' */
+         { NULL,                0  },  /* 'f' */
+         { "gnu.linkonce.wi.", 17 },   /* 'g' */
+         { NULL,                0  },  /* 'h' */
+         { NULL,                0  },  /* 'i' */
+         { NULL,                0  },  /* 'j' */
+         { NULL,                0  },  /* 'k' */
+         { "line",              4  },  /* 'l' */
+         { NULL,                0  },  /* 'm' */
+         { NULL,                0  },  /* 'n' */
+         { NULL,                0  },  /* 'o' */
+         { NULL,                0  },  /* 'p' */
+         { NULL,                0  },  /* 'q' */
+         { NULL,                0  },  /* 'r' */
+         { "stab",              4  }   /* 's' */
+       };
+      
+      if (name [0] == '.')
+       {
+         int i = name [1] - 'd';
+         if (i >= 0
+             && i < (int) ARRAY_SIZE (debug_sections)
+             && debug_sections [i].name != NULL
+             && strncmp (&name [1], debug_sections [i].name,
+                         debug_sections [i].len) == 0)
+           flags |= SEC_DEBUGGING;
+       }
+    }
 
   /* As a GNU extension, if the name begins with .gnu.linkonce, we
      only link a single copy of the section.  This is used to support
@@ -1391,7 +1414,8 @@ _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
       /* Set local fields.  */
       ret->indx = -1;
       ret->dynindx = -1;
-      ret->got = ret->plt = htab->init_refcount;
+      ret->got = htab->init_got_refcount;
+      ret->plt = htab->init_plt_refcount;
       memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
                              - offsetof (struct elf_link_hash_entry, size)));
       /* Assume that we have been called by a non-ELF symbol reader.
@@ -1464,7 +1488,7 @@ _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
                                struct elf_link_hash_entry *h,
                                bfd_boolean force_local)
 {
-  h->plt = elf_hash_table (info)->init_offset;
+  h->plt = elf_hash_table (info)->init_plt_offset;
   h->needs_plt = 0;
   if (force_local)
     {
@@ -1489,14 +1513,14 @@ _bfd_elf_link_hash_table_init
                                      const char *))
 {
   bfd_boolean ret;
+  int can_refcount = get_elf_backend_data (abfd)->can_refcount;
 
   table->dynamic_sections_created = FALSE;
   table->dynobj = NULL;
-  /* Make sure can_refcount is extended to the width and signedness of
-     init_refcount before we subtract one from it.  */
-  table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount;
-  table->init_refcount.refcount -= 1;
-  table->init_offset.offset = -(bfd_vma) 1;
+  table->init_got_refcount.refcount = can_refcount - 1;
+  table->init_plt_refcount.refcount = can_refcount - 1;
+  table->init_got_offset.offset = -(bfd_vma) 1;
+  table->init_plt_offset.offset = -(bfd_vma) 1;
   /* The first dynamic symbol is a dummy.  */
   table->dynsymcount = 1;
   table->dynstr = NULL;
@@ -1511,6 +1535,7 @@ _bfd_elf_link_hash_table_init
   table->tls_sec = NULL;
   table->tls_size = 0;
   table->loaded = NULL;
+  table->is_relocatable_executable = FALSE;
 
   ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
   table->root.type = bfd_link_elf_hash_table;
@@ -1724,7 +1749,9 @@ bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   const char *name;
 
-  name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
+  name = bfd_elf_string_from_elf_section (abfd,
+                                         elf_elfheader (abfd)->e_shstrndx,
+                                         hdr->sh_name);
 
   switch (hdr->sh_type)
     {
@@ -1740,10 +1767,13 @@ bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
     case SHT_FINI_ARRAY:       /* .fini_array section.  */
     case SHT_PREINIT_ARRAY:    /* .preinit_array section.  */
     case SHT_GNU_LIBLIST:      /* .gnu.liblist section.  */
-      return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
+      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
 
     case SHT_DYNAMIC:  /* Dynamic linking information.  */
-      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
+      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
+       return FALSE;
+      if (hdr->sh_link > elf_numsections (abfd)
+         || elf_elfsections (abfd)[hdr->sh_link] == NULL)
        return FALSE;
       if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
        {
@@ -1794,9 +1824,36 @@ bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
          linker.  */
       if ((hdr->sh_flags & SHF_ALLOC) != 0
          && (abfd->flags & DYNAMIC) != 0
-         && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
+         && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
+                                               shindex))
        return FALSE;
 
+      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
+        can't read symbols without that section loaded as well.  It
+        is most likely specified by the next section header.  */
+      if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
+       {
+         unsigned int i, num_sec;
+
+         num_sec = elf_numsections (abfd);
+         for (i = shindex + 1; i < num_sec; i++)
+           {
+             Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
+             if (hdr2->sh_type == SHT_SYMTAB_SHNDX
+                 && hdr2->sh_link == shindex)
+               break;
+           }
+         if (i == num_sec)
+           for (i = 1; i < shindex; i++)
+             {
+               Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
+               if (hdr2->sh_type == SHT_SYMTAB_SHNDX
+                   && hdr2->sh_link == shindex)
+                 break;
+             }
+         if (i != shindex)
+           return bfd_section_from_shdr (abfd, i);
+       }
       return TRUE;
 
     case SHT_DYNSYM:           /* A dynamic symbol table */
@@ -1812,17 +1869,13 @@ bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
 
       /* Besides being a symbol table, we also treat this as a regular
         section, so that objcopy can handle it.  */
-      return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
+      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
 
     case SHT_SYMTAB_SHNDX:     /* Symbol section indices when >64k sections */
       if (elf_symtab_shndx (abfd) == shindex)
        return TRUE;
 
-      /* Get the associated symbol table.  */
-      if (! bfd_section_from_shdr (abfd, hdr->sh_link)
-         || hdr->sh_link != elf_onesymtab (abfd))
-       return FALSE;
-
+      BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
       elf_symtab_shndx (abfd) = shindex;
       elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
       elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
@@ -1837,49 +1890,48 @@ bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
          elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
          return TRUE;
        }
-      {
-       unsigned int i, num_sec;
+      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
+       {
+       symtab_strtab:
+         elf_tdata (abfd)->strtab_hdr = *hdr;
+         elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
+         return TRUE;
+       }
+      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
+       {
+       dynsymtab_strtab:
+         elf_tdata (abfd)->dynstrtab_hdr = *hdr;
+         hdr = &elf_tdata (abfd)->dynstrtab_hdr;
+         elf_elfsections (abfd)[shindex] = hdr;
+         /* We also treat this as a regular section, so that objcopy
+            can handle it.  */
+         return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
+                                                 shindex);
+       }
 
-       num_sec = elf_numsections (abfd);
-       for (i = 1; i < num_sec; i++)
-         {
-           Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
-           if (hdr2->sh_link == shindex)
-             {
-               if (! bfd_section_from_shdr (abfd, i))
-                 return FALSE;
-               if (elf_onesymtab (abfd) == i)
-                 {
-                   elf_tdata (abfd)->strtab_hdr = *hdr;
-                   elf_elfsections (abfd)[shindex] =
-                     &elf_tdata (abfd)->strtab_hdr;
-                   return TRUE;
-                 }
-               if (elf_dynsymtab (abfd) == i)
-                 {
-                   elf_tdata (abfd)->dynstrtab_hdr = *hdr;
-                   elf_elfsections (abfd)[shindex] = hdr =
-                     &elf_tdata (abfd)->dynstrtab_hdr;
-                   /* We also treat this as a regular section, so
-                      that objcopy can handle it.  */
-                   break;
-                 }
-#if 0 /* Not handling other string tables specially right now.  */
-               hdr2 = elf_elfsections (abfd)[i];       /* in case it moved */
-               /* We have a strtab for some random other section.  */
-               newsect = (asection *) hdr2->bfd_section;
-               if (!newsect)
-                 break;
-               hdr->bfd_section = newsect;
-               hdr2 = &elf_section_data (newsect)->str_hdr;
-               *hdr2 = *hdr;
-               elf_elfsections (abfd)[shindex] = hdr2;
-#endif
-             }
-         }
-      }
+      /* If the string table isn't one of the above, then treat it as a
+        regular section.  We need to scan all the headers to be sure,
+        just in case this strtab section appeared before the above.  */
+      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
+       {
+         unsigned int i, num_sec;
 
-      return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
+         num_sec = elf_numsections (abfd);
+         for (i = 1; i < num_sec; i++)
+           {
+             Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
+             if (hdr2->sh_link == shindex)
+               {
+                 if (! bfd_section_from_shdr (abfd, i))
+                   return FALSE;
+                 if (elf_onesymtab (abfd) == i)
+                   goto symtab_strtab;
+                 if (elf_dynsymtab (abfd) == i)
+                   goto dynsymtab_strtab;
+               }
+           }
+       }
+      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
 
     case SHT_REL:
     case SHT_RELA:
@@ -1896,7 +1948,8 @@ bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
            ((*_bfd_error_handler)
             (_("%B: invalid link %lu for reloc section %s (index %u)"),
              abfd, hdr->sh_link, name, shindex));
-           return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
+           return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
+                                                   shindex);
          }
 
        /* For some incomprehensible reason Oracle distributes
@@ -1931,7 +1984,8 @@ bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
          }
 
        /* Get the symbol table.  */
-       if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
+       if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
+            || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
            && ! bfd_section_from_shdr (abfd, hdr->sh_link))
          return FALSE;
 
@@ -1942,7 +1996,8 @@ bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
           can't use it as a reloc section if it points to the null
           section.  */
        if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
-         return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
+         return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
+                                                 shindex);
 
        if (! bfd_section_from_shdr (abfd, hdr->sh_info))
          return FALSE;
@@ -1979,19 +2034,19 @@ bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
     case SHT_GNU_verdef:
       elf_dynverdef (abfd) = shindex;
       elf_tdata (abfd)->dynverdef_hdr = *hdr;
-      return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
+      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
       break;
 
     case SHT_GNU_versym:
       elf_dynversym (abfd) = shindex;
       elf_tdata (abfd)->dynversym_hdr = *hdr;
-      return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
+      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
       break;
 
     case SHT_GNU_verneed:
       elf_dynverref (abfd) = shindex;
       elf_tdata (abfd)->dynverref_hdr = *hdr;
-      return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
+      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
       break;
 
     case SHT_SHLIB:
@@ -2004,7 +2059,7 @@ bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
       name = group_signature (abfd, hdr);
       if (name == NULL)
        return FALSE;
-      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
+      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
        return FALSE;
       if (hdr->contents != NULL)
        {
@@ -2030,11 +2085,8 @@ bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
 
     default:
       /* Check for any processor-specific section types.  */
-      {
-       if (bed->elf_backend_section_from_shdr)
-         (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
-      }
-      break;
+      return bed->elf_backend_section_from_shdr (abfd, hdr, name,
+                                                shindex);
     }
 
   return TRUE;
@@ -2092,25 +2144,23 @@ bfd_section_from_elf_index (bfd *abfd, unsigned int index)
   return elf_elfsections (abfd)[index]->bfd_section;
 }
 
-static struct bfd_elf_special_section const special_sections[] =
+static struct bfd_elf_special_section const special_sections_b[] =
 {
   { ".bss",            4, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
-  { ".gnu.linkonce.b",15, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
+  { NULL,              0,  0, 0,            0 }
+};
+
+static struct bfd_elf_special_section const special_sections_c[] =
+{
   { ".comment",        8,  0, SHT_PROGBITS, 0 },
+  { NULL,              0,  0, 0,            0 }
+};
+
+static struct bfd_elf_special_section const special_sections_d[] =
+{
   { ".data",           5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   { ".data1",          6,  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   { ".debug",          6,  0, SHT_PROGBITS, 0 },
-  { ".fini",           5,  0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
-  { ".init",           5,  0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
-  { ".line",           5,  0, SHT_PROGBITS, 0 },
-  { ".rodata",         7, -2, SHT_PROGBITS, SHF_ALLOC },
-  { ".rodata1",        8,  0, SHT_PROGBITS, SHF_ALLOC },
-  { ".tbss",           5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
-  { ".tdata",          6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
-  { ".text",           5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
-  { ".init_array",    11,  0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
-  { ".fini_array",    11,  0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
-  { ".preinit_array", 14,  0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
   { ".debug_line",    11,  0, SHT_PROGBITS, 0 },
   { ".debug_info",    11,  0, SHT_PROGBITS, 0 },
   { ".debug_abbrev",  13,  0, SHT_PROGBITS, 0 },
@@ -2118,33 +2168,143 @@ static struct bfd_elf_special_section const special_sections[] =
   { ".dynamic",        8,  0, SHT_DYNAMIC,  SHF_ALLOC },
   { ".dynstr",         7,  0, SHT_STRTAB,   SHF_ALLOC },
   { ".dynsym",         7,  0, SHT_DYNSYM,   SHF_ALLOC },
+  { NULL,              0,  0, 0,            0 }
+};
+
+static struct bfd_elf_special_section const special_sections_f[] =
+{
+  { ".fini",           5,  0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
+  { ".fini_array",    11,  0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
+  { NULL,              0,  0, 0,            0 }
+};
+
+static struct bfd_elf_special_section const special_sections_g[] =
+{
+  { ".gnu.linkonce.b",15, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   { ".got",            4,  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
-  { ".hash",           5,  0, SHT_HASH,     SHF_ALLOC },
-  { ".interp",         7,  0, SHT_PROGBITS, 0 },
-  { ".plt",            4,  0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
-  { ".shstrtab",       9,  0, SHT_STRTAB,   0 },
-  { ".strtab",         7,  0, SHT_STRTAB,   0 },
-  { ".symtab",         7,  0, SHT_SYMTAB,   0 },
   { ".gnu.version",   12,  0, SHT_GNU_versym, 0 },
   { ".gnu.version_d", 14,  0, SHT_GNU_verdef, 0 },
   { ".gnu.version_r", 14,  0, SHT_GNU_verneed, 0 },
+  { ".gnu.liblist",   12,  0, SHT_GNU_LIBLIST, SHF_ALLOC },
+  { ".gnu.conflict",  13,  0, SHT_RELA,     SHF_ALLOC },
+  { NULL,              0,  0, 0,            0 }
+};
+
+static struct bfd_elf_special_section const special_sections_h[] =
+{
+  { ".hash",           5,  0, SHT_HASH,     SHF_ALLOC },
+  { NULL,              0,  0, 0,            0 }
+};
+
+static struct bfd_elf_special_section const special_sections_i[] =
+{
+  { ".init",           5,  0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
+  { ".init_array",    11,  0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
+  { ".interp",         7,  0, SHT_PROGBITS, 0 },
+  { NULL,              0,  0, 0,            0 }
+};
+
+static struct bfd_elf_special_section const special_sections_l[] =
+{
+  { ".line",           5,  0, SHT_PROGBITS, 0 },
+  { NULL,              0,  0, 0,            0 }
+};
+
+static struct bfd_elf_special_section const special_sections_n[] =
+{
   { ".note.GNU-stack",15,  0, SHT_PROGBITS, 0 },
   { ".note",           5, -1, SHT_NOTE,     0 },
+  { NULL,              0,  0, 0,            0 }
+};
+
+static struct bfd_elf_special_section const special_sections_p[] =
+{
+  { ".preinit_array", 14,  0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
+  { ".plt",            4,  0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
+  { NULL,              0,  0, 0,            0 }
+};
+
+static struct bfd_elf_special_section const special_sections_r[] =
+{
+  { ".rodata",         7, -2, SHT_PROGBITS, SHF_ALLOC },
+  { ".rodata1",        8,  0, SHT_PROGBITS, SHF_ALLOC },
   { ".rela",           5, -1, SHT_RELA,     0 },
   { ".rel",            4, -1, SHT_REL,      0 },
+  { NULL,              0,  0, 0,            0 }
+};
+
+static struct bfd_elf_special_section const special_sections_s[] =
+{
+  { ".shstrtab",       9,  0, SHT_STRTAB,   0 },
+  { ".strtab",         7,  0, SHT_STRTAB,   0 },
+  { ".symtab",         7,  0, SHT_SYMTAB,   0 },
   { ".stabstr",        5,  3, SHT_STRTAB,   0 },
-  { ".gnu.liblist",   12,  0, SHT_GNU_LIBLIST, SHF_ALLOC },
-  { ".gnu.conflict",  13,  0, SHT_RELA,     SHF_ALLOC },
   { NULL,              0,  0, 0,            0 }
 };
 
+static struct bfd_elf_special_section const special_sections_t[] =
+{
+  { ".text",           5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
+  { ".tbss",           5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
+  { ".tdata",          6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
+  { NULL,              0,  0, 0,            0 }
+};
+
+static struct bfd_elf_special_section const *special_sections [27] =
+{
+  NULL,                                /* 'a' */
+  special_sections_b,          /* 'b' */
+  special_sections_c,          /* 'b' */
+  special_sections_d,          /* 'd' */
+  NULL,                                /* 'e' */
+  special_sections_f,          /* 'f' */
+  special_sections_g,          /* 'g' */
+  special_sections_h,          /* 'h' */
+  special_sections_i,          /* 'i' */
+  NULL,                                /* 'j' */
+  NULL,                                /* 'k' */
+  special_sections_l,          /* 'l' */
+  NULL,                                /* 'm' */
+  special_sections_n,          /* 'n' */
+  NULL,                                /* 'o' */
+  special_sections_p,          /* 'p' */
+  NULL,                                /* 'q' */
+  special_sections_r,          /* 'r' */
+  special_sections_s,          /* 's' */
+  special_sections_t,          /* 't' */
+  NULL,                                /* 'u' */
+  NULL,                                /* 'v' */
+  NULL,                                /* 'w' */
+  NULL,                                /* 'x' */
+  NULL,                                /* 'y' */
+  NULL,                                /* 'z' */
+  NULL                         /* other */
+};
+
 static const struct bfd_elf_special_section *
 get_special_section (const char *name,
-                    const struct bfd_elf_special_section *special_sections,
+                    const struct bfd_elf_special_section **special_sections_p,
                     unsigned int rela)
 {
   int i;
-  int len = strlen (name);
+  int len;
+  const struct bfd_elf_special_section *special_sections;
+
+  if (name [0] == '.')
+    {
+      i = name [1] - 'a';
+      if (i < 0 || i > 25)
+       i = 26;
+    }
+  else
+    i = 26;
+
+  special_sections = special_sections_p [i];
+
+  if (!special_sections)
+    return special_sections;
+
+  len= strlen (name);
 
   for (i = 0; special_sections[i].prefix != NULL; i++)
     {
@@ -2220,12 +2380,18 @@ _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
       sec->used_by_bfd = sdata;
     }
 
-  elf_section_type (sec) = SHT_NULL;
-  ssect = _bfd_elf_get_sec_type_attr (abfd, sec->name);
-  if (ssect != NULL)
+  /* When we read a file, we don't need section type and flags unless
+     it is a linker created section.  They will be overridden in
+     _bfd_elf_make_section_from_shdr anyway.  */
+  if (abfd->direction != read_direction
+      || (sec->flags & SEC_LINKER_CREATED) != 0)
     {
-      elf_section_type (sec) = ssect->type;
-      elf_section_flags (sec) = ssect->attr;
+      ssect = _bfd_elf_get_sec_type_attr (abfd, sec->name);
+      if (ssect != NULL)
+       {
+         elf_section_type (sec) = ssect->type;
+         elf_section_flags (sec) = ssect->attr;
+       }
     }
 
   /* Indicate whether or not this section should use RELA relocations.  */
@@ -2372,13 +2538,9 @@ bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
       return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
 
     default:
-      /* Check for any processor-specific program segment types.
-         If no handler for them, default to making "segment" sections.  */
+      /* Check for any processor-specific program segment types.  */
       bed = get_elf_backend_data (abfd);
-      if (bed->elf_backend_section_from_phdr)
-       return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
-      else
-       return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
+      return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
     }
 }
 
@@ -2473,7 +2635,7 @@ elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
          struct bfd_link_order *l;
          asection *elt;
 
-         for (l = asect->link_order_head; l != NULL; l = l->next)
+         for (l = asect->map_head.link_order; l != NULL; l = l->next)
            if (l->type == bfd_indirect_link_order
                && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
              do
@@ -2590,7 +2752,7 @@ elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
          struct bfd_link_order *o;
 
          this_hdr->sh_size = 0;
-         for (o = asect->link_order_head; o != NULL; o = o->next)
+         for (o = asect->map_head.link_order; o != NULL; o = o->next)
            if (this_hdr->sh_size < o->offset + o->size)
              this_hdr->sh_size = o->offset + o->size;
          if (this_hdr->sh_size)
@@ -2627,7 +2789,9 @@ bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   struct bfd_link_order *l;
   bfd_boolean gas;
 
-  if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
+  /* Ignore linker created group section.  See elfNN_ia64_object_p in
+     elfxx-ia64.c.  */
+  if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
       || *failedptr)
     return;
 
@@ -2694,7 +2858,7 @@ bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
   /* If this is a relocatable link, then the above did nothing because
      SEC is the output section.  Look through the input sections
      instead.  */
-  for (l = sec->link_order_head; l != NULL; l = l->next)
+  for (l = sec->map_head.link_order; l != NULL; l = l->next)
     if (l->type == bfd_indirect_link_order
        && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
       do
@@ -2718,25 +2882,55 @@ bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
    in here too, while we're at it.  */
 
 static bfd_boolean
-assign_section_numbers (bfd *abfd)
+assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
 {
   struct elf_obj_tdata *t = elf_tdata (abfd);
   asection *sec;
   unsigned int section_number, secn;
   Elf_Internal_Shdr **i_shdrp;
   bfd_size_type amt;
+  struct bfd_elf_section_data *d;
 
   section_number = 1;
 
   _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
 
+  /* SHT_GROUP sections are in relocatable files only.  */
+  if (link_info == NULL || link_info->relocatable)
+    {
+      /* Put SHT_GROUP sections first.  */
+      for (sec = abfd->sections; sec != NULL; sec = sec->next)
+       {
+         d = elf_section_data (sec);
+
+         if (d->this_hdr.sh_type == SHT_GROUP)
+           { 
+             if (sec->flags & SEC_LINKER_CREATED)
+               {
+                 /* Remove the linker created SHT_GROUP sections.  */
+                 bfd_section_list_remove (abfd, sec);
+                 abfd->section_count--;
+               }
+             else 
+               {
+                 if (section_number == SHN_LORESERVE)
+                   section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
+                 d->this_idx = section_number++;
+               }
+           }
+       }
+    }
+
   for (sec = abfd->sections; sec; sec = sec->next)
     {
-      struct bfd_elf_section_data *d = elf_section_data (sec);
+      d = elf_section_data (sec);
 
-      if (section_number == SHN_LORESERVE)
-       section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
-      d->this_idx = section_number++;
+      if (d->this_hdr.sh_type != SHT_GROUP)
+       {
+         if (section_number == SHN_LORESERVE)
+           section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
+         d->this_idx = section_number++;
+       }
       _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
       if ((sec->flags & SEC_RELOC) == 0)
        d->rel_idx = 0;
@@ -2866,7 +3060,7 @@ assign_section_numbers (bfd *abfd)
 
              /* Find out what the corresponding section in output
                 is.  */
-             for (p = sec->link_order_head; p != NULL; p = p->next)
+             for (p = sec->map_head.link_order; p != NULL; p = p->next)
                {
                  s = p->u.indirect.section;
                  if (p->type == bfd_indirect_link_order
@@ -2880,7 +3074,7 @@ assign_section_numbers (bfd *abfd)
                      elfsec = elf_shdrp[elfsec]->sh_link;
                      /* PR 290:
                         The Intel C compiler generates SHT_IA_64_UNWIND with
-                        SHF_LINK_ORDER.  But it doesn't set theh sh_link or
+                        SHF_LINK_ORDER.  But it doesn't set the sh_link or
                         sh_info fields.  Hence we could get the situation
                         where elfsec is 0.  */
                      if (elfsec == 0)
@@ -2889,12 +3083,31 @@ assign_section_numbers (bfd *abfd)
                            = get_elf_backend_data (abfd);
                          if (bed->link_order_error_handler)
                            bed->link_order_error_handler
-                             (_("%B: warning: sh_link not set for section `%S'"),
+                             (_("%B: warning: sh_link not set for section `%A'"),
                               abfd, s);
                        }
                      else
                        {
-                         s = elf_shdrp[elfsec]->bfd_section->output_section;
+                         s = elf_shdrp[elfsec]->bfd_section;
+                         if (elf_discarded_section (s))
+                           {
+                             asection *kept;
+                              (*_bfd_error_handler)
+                                 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
+                                  abfd, d->this_hdr.bfd_section,
+                                  s, s->owner);
+                              /* Point to the kept section if it has
+                                 the same size as the discarded
+                                 one.  */
+                              kept = _bfd_elf_check_kept_section (s);
+                              if (kept == NULL)
+                                {
+                                  bfd_set_error (bfd_error_bad_value);
+                                  return FALSE;
+                                }
+                              s = kept;
+                           }
+                         s = s->output_section;
                          BFD_ASSERT (s != NULL);
                          d->this_hdr.sh_link = elf_section_data (s)->this_idx;
                        }
@@ -3209,7 +3422,7 @@ _bfd_elf_compute_section_file_positions (bfd *abfd,
 {
   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   bfd_boolean failed;
-  struct bfd_strtab_hash *strtab;
+  struct bfd_strtab_hash *strtab = NULL;
   Elf_Internal_Shdr *shstrtab_hdr;
 
   if (abfd->output_has_begun)
@@ -3231,7 +3444,7 @@ _bfd_elf_compute_section_file_positions (bfd *abfd,
   if (failed)
     return FALSE;
 
-  if (!assign_section_numbers (abfd))
+  if (!assign_section_numbers (abfd, link_info))
     return FALSE;
 
   /* The backend linker builds symbol table information itself.  */
@@ -3939,22 +4152,20 @@ assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info)
        {
          bfd_size_type align;
          bfd_vma adjust;
+         unsigned int align_power = 0;
 
-         if ((abfd->flags & D_PAGED) != 0)
-           align = bed->maxpagesize;
-         else
+         for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
            {
-             unsigned int align_power = 0;
-             for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
-               {
-                 unsigned int secalign;
+             unsigned int secalign;
 
-                 secalign = bfd_get_section_alignment (abfd, *secpp);
-                 if (secalign > align_power)
-                   align_power = secalign;
-               }
-             align = (bfd_size_type) 1 << align_power;
+             secalign = bfd_get_section_alignment (abfd, *secpp);
+             if (secalign > align_power)
+               align_power = secalign;
            }
+         align = (bfd_size_type) 1 << align_power;
+
+         if ((abfd->flags & D_PAGED) != 0 && bed->maxpagesize > align)
+           align = bed->maxpagesize;
 
          adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
          off += adjust;
@@ -4140,7 +4351,7 @@ assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info)
                  /* The section VMA must equal the file position
                     modulo the page size.  */
                  bfd_size_type page = align;
-                 if ((abfd->flags & D_PAGED) != 0)
+                 if ((abfd->flags & D_PAGED) != 0 && bed->maxpagesize > page)
                    page = bed->maxpagesize;
                  adjust = vma_page_aligned_bias (sec->vma,
                                                  p->p_vaddr + p->p_memsz,
@@ -4199,6 +4410,12 @@ assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info)
                  p->p_filesz += sec->size;
                  p->p_memsz += sec->size;
                }
+             /* PR ld/594:  Sections in note segments which are not loaded
+                contribute to the file size but not the in-memory size.  */
+             else if (p->p_type == PT_NOTE
+                 && (flags & SEC_HAS_CONTENTS) != 0)
+               p->p_filesz += sec->size;
+
              /* .tbss is special.  It doesn't contribute to p_memsz of
                 normal segments.  */
              else if ((flags & SEC_THREAD_LOCAL) == 0
@@ -4212,7 +4429,7 @@ assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info)
                  struct bfd_link_order *o;
                  bfd_vma tbss_size = 0;
 
-                 for (o = sec->link_order_head; o != NULL; o = o->next)
+                 for (o = sec->map_head.link_order; o != NULL; o = o->next)
                    if (tbss_size < o->offset + o->size)
                      tbss_size = o->offset + o->size;
 
@@ -4629,18 +4846,8 @@ prep_headers (bfd *abfd)
 
   /* If we're building an executable, we'll need a program header table.  */
   if (abfd->flags & EXEC_P)
-    {
-      /* It all happens later.  */
-#if 0
-      i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
-
-      /* elf_build_phdrs() returns a (NULL-terminated) array of
-        Elf_Internal_Phdrs.  */
-      i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
-      i_ehdrp->e_phoff = outbase;
-      outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
-#endif
-    }
+    /* It all happens later.  */
+    ;
   else
     {
       i_ehdrp->e_phentsize = 0;
@@ -4730,8 +4937,9 @@ _bfd_elf_write_object_contents (bfd *abfd)
     }
 
   /* Write out the section header names.  */
-  if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
-      || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
+  if (elf_shstrtab (abfd) != NULL
+      && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
+          || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
     return FALSE;
 
   if (bed->elf_backend_final_write_processing)
@@ -4767,19 +4975,7 @@ _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
   else if (bfd_is_und_section (asect))
     index = SHN_UNDEF;
   else
-    {
-      Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
-      int maxindex = elf_numsections (abfd);
-
-      for (index = 1; index < maxindex; index++)
-       {
-         Elf_Internal_Shdr *hdr = i_shdrp[index];
-
-         if (hdr != NULL && hdr->bfd_section == asect)
-           return index;
-       }
-      index = -1;
-    }
+    index = -1;
 
   bed = get_elf_backend_data (abfd);
   if (bed->elf_backend_section_from_bfd_section)
@@ -4943,7 +5139,9 @@ copy_private_bfd_data (bfd *ibfd, bfd *obfd)
        4. The section has not already been allocated to a previous segment.
        5. PT_GNU_STACK segments do not include any sections.
        6. PT_TLS segment includes only SHF_TLS sections.
-       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.  */
+       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
+       8. PT_DYNAMIC should not contain empty sections at the beginning
+          (with the possible exception of .dynamic).  */
 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed)              \
   ((((segment->p_paddr                                                 \
       ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)       \
@@ -4957,6 +5155,13 @@ copy_private_bfd_data (bfd *ibfd, bfd *obfd)
    && (segment->p_type == PT_LOAD                                      \
        || segment->p_type == PT_TLS                                    \
        || (section->flags & SEC_THREAD_LOCAL) == 0)                    \
+   && (segment->p_type != PT_DYNAMIC                                   \
+       || SECTION_SIZE (section, segment) > 0                          \
+       || (segment->p_paddr                                            \
+           ? segment->p_paddr != section->lma                          \
+           : segment->p_vaddr != section->vma)                         \
+       || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic")   \
+           == 0))                                                      \
    && ! section->segment_mark)
 
   /* Returns TRUE iff seg1 starts after the end of seg2.  */
@@ -5417,31 +5622,6 @@ copy_private_bfd_data (bfd *ibfd, bfd *obfd)
          -= (count - phdr_adjust_num) * iehdr->e_phentsize;
     }
 
-#if 0
-  /* Final Step: Sort the segments into ascending order of physical
-     address.  */
-  if (map_first != NULL)
-    {
-      struct elf_segment_map *prev;
-
-      prev = map_first;
-      for (map = map_first->next; map != NULL; prev = map, map = map->next)
-       {
-         /* Yes I know - its a bubble sort....  */
-         if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
-           {
-             /* Swap map and map->next.  */
-             prev->next = map->next;
-             map->next = map->next->next;
-             prev->next->next = map;
-
-             /* Restart loop.  */
-             map = map_first;
-           }
-       }
-    }
-#endif
-
 #undef SEGMENT_END
 #undef SECTION_SIZE
 #undef IS_CONTAINED_BY_VMA
@@ -5482,9 +5662,14 @@ _bfd_elf_copy_private_section_data (bfd *ibfd,
 
   /* Set things up for objcopy.  The output SHT_GROUP section will
      have its elf_next_in_group pointing back to the input group
-     members.  */
-  elf_next_in_group (osec) = elf_next_in_group (isec);
-  elf_group_name (osec) = elf_group_name (isec);
+     members.  Ignore linker created group section.  See
+     elfNN_ia64_object_p in elfxx-ia64.c.  */
+  if (elf_sec_group (isec) == NULL
+      || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
+    {
+      elf_next_in_group (osec) = elf_next_in_group (isec);
+      elf_group_name (osec) = elf_group_name (isec);
+    }
 
   osec->use_rela_p = isec->use_rela_p;
 
@@ -5578,8 +5763,8 @@ swap_out_syms (bfd *abfd,
   Elf_Internal_Shdr *symtab_hdr;
   Elf_Internal_Shdr *symtab_shndx_hdr;
   Elf_Internal_Shdr *symstrtab_hdr;
-  char *outbound_syms;
-  char *outbound_shndx;
+  bfd_byte *outbound_syms;
+  bfd_byte *outbound_shndx;
   int idx;
   bfd_size_type amt;
   bfd_boolean name_local_sections;
@@ -5946,10 +6131,10 @@ _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
   return symcount;
 }
 
-/* Return the size required for the dynamic reloc entries.  Any
-   section that was actually installed in the BFD, and has type
-   SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
-   considered to be a dynamic reloc section.  */
+/* Return the size required for the dynamic reloc entries.  Any loadable
+   section that was actually installed in the BFD, and has type SHT_REL
+   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
+   dynamic reloc section.  */
 
 long
 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
@@ -5965,7 +6150,8 @@ _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
 
   ret = sizeof (arelent *);
   for (s = abfd->sections; s != NULL; s = s->next)
-    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
+    if ((s->flags & SEC_LOAD) != 0
+       && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
        && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
            || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
       ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
@@ -5974,14 +6160,13 @@ _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
   return ret;
 }
 
-/* Canonicalize the dynamic relocation entries.  Note that we return
-   the dynamic relocations as a single block, although they are
-   actually associated with particular sections; the interface, which
-   was designed for SunOS style shared libraries, expects that there
-   is only one set of dynamic relocs.  Any section that was actually
-   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
-   the dynamic symbol table, is considered to be a dynamic reloc
-   section.  */
+/* Canonicalize the dynamic relocation entries.  Note that we return the
+   dynamic relocations as a single block, although they are actually
+   associated with particular sections; the interface, which was
+   designed for SunOS style shared libraries, expects that there is only
+   one set of dynamic relocs.  Any loadable section that was actually
+   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
+   dynamic symbol table, is considered to be a dynamic reloc section.  */
 
 long
 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
@@ -6002,7 +6187,8 @@ _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
   ret = 0;
   for (s = abfd->sections; s != NULL; s = s->next)
     {
-      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
+      if ((s->flags & SEC_LOAD) != 0
+         && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
          && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
              || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
        {
@@ -6359,13 +6545,24 @@ elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
                   const char **functionname_ptr)
 {
   const char *filename;
-  asymbol *func;
+  asymbol *func, *file;
   bfd_vma low_func;
   asymbol **p;
+  /* ??? Given multiple file symbols, it is impossible to reliably
+     choose the right file name for global symbols.  File symbols are
+     local symbols, and thus all file symbols must sort before any
+     global symbols.  The ELF spec may be interpreted to say that a
+     file symbol must sort before other local symbols, but currently
+     ld -r doesn't do this.  So, for ld -r output, it is possible to
+     make a better choice of file name for local symbols by ignoring
+     file symbols appearing after a given local symbol.  */
+  enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
 
   filename = NULL;
   func = NULL;
+  file = NULL;
   low_func = 0;
+  state = nothing_seen;
 
   for (p = symbols; *p != NULL; p++)
     {
@@ -6378,8 +6575,12 @@ elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
        default:
          break;
        case STT_FILE:
-         filename = bfd_asymbol_name (&q->symbol);
-         break;
+         file = &q->symbol;
+         if (state == symbol_seen)
+           state = file_after_symbol_seen;
+         continue;
+       case STT_SECTION:
+         continue;
        case STT_NOTYPE:
        case STT_FUNC:
          if (bfd_get_section (&q->symbol) == section
@@ -6388,9 +6589,18 @@ elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
            {
              func = (asymbol *) q;
              low_func = q->symbol.value;
+             if (file == NULL)
+               filename = NULL;
+             else if (ELF_ST_BIND (q->internal_elf_sym.st_info) != STB_LOCAL
+                      && state == file_after_symbol_seen)
+               filename = NULL;
+             else
+               filename = bfd_asymbol_name (file);
            }
          break;
        }
+      if (state == nothing_seen)
+       state = symbol_seen;
     }
 
   if (func == NULL)
@@ -6462,6 +6672,36 @@ _bfd_elf_find_nearest_line (bfd *abfd,
   return TRUE;
 }
 
+/* Find the line for a symbol.  */
+
+bfd_boolean
+_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
+                   const char **filename_ptr, unsigned int *line_ptr)
+{
+  return _bfd_dwarf2_find_line (abfd, symbols, symbol,
+                               filename_ptr, line_ptr, 0,
+                               &elf_tdata (abfd)->dwarf2_find_line_info);
+}
+
+/* After a call to bfd_find_nearest_line, successive calls to
+   bfd_find_inliner_info can be used to get source information about
+   each level of function inlining that terminated at the address
+   passed to bfd_find_nearest_line.  Currently this is only supported
+   for DWARF2 with appropriate DWARF3 extensions. */
+
+bfd_boolean
+_bfd_elf_find_inliner_info (bfd *abfd,
+                           const char **filename_ptr,
+                           const char **functionname_ptr,
+                           unsigned int *line_ptr)
+{
+  bfd_boolean found;
+  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
+                                        functionname_ptr, line_ptr,
+                                        & elf_tdata (abfd)->dwarf2_find_line_info);
+  return found;
+}
+
 int
 _bfd_elf_sizeof_headers (bfd *abfd, bfd_boolean reloc)
 {
@@ -6607,6 +6847,7 @@ _bfd_elf_close_and_cleanup (bfd *abfd)
     {
       if (elf_shstrtab (abfd) != NULL)
        _bfd_elf_strtab_free (elf_shstrtab (abfd));
+      _bfd_dwarf2_cleanup_debug_info (abfd);
     }
 
   return _bfd_generic_close_and_cleanup (abfd);
@@ -7057,7 +7298,7 @@ elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
 
     case NOTE_INFO_THREAD:
       /* Make a ".reg/999" section.  */
-      sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
+      sprintf (buf, ".reg/%ld", (long) pstatus.data.thread_info.tid);
 
       len = strlen (buf) + 1;
       name = bfd_alloc (abfd, len);
@@ -7084,7 +7325,8 @@ elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
 
     case NOTE_INFO_MODULE:
       /* Make a ".module/xxxxxxxx" section.  */
-      sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
+      sprintf (buf, ".module/%08lx",
+              (long) pstatus.data.module_info.base_address);
 
       len = strlen (buf) + 1;
       name = bfd_alloc (abfd, len);
@@ -7316,7 +7558,7 @@ elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid)
     elf_tdata (abfd)->core_lwpid = *tid;
 
   /* Make a ".qnx_core_status/%d" section.  */
-  sprintf (buf, ".qnx_core_status/%d", *tid);
+  sprintf (buf, ".qnx_core_status/%ld", (long) *tid);
 
   name = bfd_alloc (abfd, strlen (buf) + 1);
   if (name == NULL)
@@ -7336,14 +7578,17 @@ elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid)
 }
 
 static bfd_boolean
-elfcore_grok_nto_gregs (bfd *abfd, Elf_Internal_Note *note, pid_t tid)
+elfcore_grok_nto_regs (bfd *abfd,
+                      Elf_Internal_Note *note,
+                      pid_t tid,
+                      char *base)
 {
   char buf[100];
   char *name;
   asection *sect;
 
-  /* Make a ".reg/%d" section.  */
-  sprintf (buf, ".reg/%d", tid);
+  /* Make a "(base)/%d" section.  */
+  sprintf (buf, "%s/%ld", base, (long) tid);
 
   name = bfd_alloc (abfd, strlen (buf) + 1);
   if (name == NULL)
@@ -7361,7 +7606,7 @@ elfcore_grok_nto_gregs (bfd *abfd, Elf_Internal_Note *note, pid_t tid)
 
   /* This is the current thread.  */
   if (elf_tdata (abfd)->core_lwpid == tid)
-    return elfcore_maybe_make_sect (abfd, ".reg", sect);
+    return elfcore_maybe_make_sect (abfd, base, sect);
 
   return TRUE;
 }
@@ -7381,11 +7626,16 @@ elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
 
   switch (note->type)
     {
-    case BFD_QNT_CORE_INFO:   return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
-    case BFD_QNT_CORE_STATUS: return elfcore_grok_nto_status (abfd, note, &tid);
-    case BFD_QNT_CORE_GREG:   return elfcore_grok_nto_gregs (abfd, note, tid);
-    case BFD_QNT_CORE_FPREG:  return elfcore_grok_prfpreg (abfd, note);
-    default:                  return TRUE;
+    case BFD_QNT_CORE_INFO:
+      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
+    case BFD_QNT_CORE_STATUS:
+      return elfcore_grok_nto_status (abfd, note, &tid);
+    case BFD_QNT_CORE_GREG:
+      return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
+    case BFD_QNT_CORE_FPREG:
+      return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
+    default:
+      return TRUE;
     }
 }
 
@@ -7835,7 +8085,7 @@ bfd_elf_bfd_from_remote_memory
   (bfd *templ,
    bfd_vma ehdr_vma,
    bfd_vma *loadbasep,
-   int (*target_read_memory) (bfd_vma, char *, int))
+   int (*target_read_memory) (bfd_vma, bfd_byte *, int))
 {
   return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
     (templ, ehdr_vma, loadbasep, target_read_memory);
@@ -7845,7 +8095,7 @@ long
 _bfd_elf_get_synthetic_symtab (bfd *abfd,
                               long symcount ATTRIBUTE_UNUSED,
                               asymbol **syms ATTRIBUTE_UNUSED,
-                              long dynsymcount ATTRIBUTE_UNUSED,
+                              long dynsymcount,
                               asymbol **dynsyms,
                               asymbol **ret)
 {
@@ -7861,10 +8111,14 @@ _bfd_elf_get_synthetic_symtab (bfd *abfd,
   char *names;
   asection *plt;
 
+  *ret = NULL;
+
   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
     return 0;
 
-  *ret = NULL;
+  if (dynsymcount <= 0)
+    return 0;
+
   if (!bed->plt_sym_val)
     return 0;
 
This page took 0.052653 seconds and 4 git commands to generate.