* elf-bfd.h (enum elf_link_info_type): New.
[deliverable/binutils-gdb.git] / bfd / elf.c
index a6ceafbf19b4ab4299f0be9c1a71e6fd264eca78..98d3cd2d9b39869fb65850ad861d8739a1f95686 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -40,6 +40,7 @@ SECTION
 #include "libbfd.h"
 #define ARCH_SIZE 0
 #include "elf-bfd.h"
+#include "libiberty.h"
 
 static INLINE struct elf_segment_map *make_mapping
   PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
@@ -51,7 +52,9 @@ static boolean prep_headers PARAMS ((bfd *));
 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
 static char *elf_read PARAMS ((bfd *, file_ptr, bfd_size_type));
+static boolean setup_group PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
+static void set_group_contents PARAMS ((bfd *, asection *, PTR));
 static boolean assign_section_numbers PARAMS ((bfd *));
 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
 static boolean elf_map_symbols PARAMS ((bfd *));
@@ -345,6 +348,186 @@ bfd_elf_string_from_elf_section (abfd, shindex, strindex)
   return ((char *) hdr->contents) + strindex;
 }
 
+/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
+   sections.  The first element is the flags, the rest are section
+   pointers.  */
+
+typedef union elf_internal_group {
+  Elf_Internal_Shdr *shdr;
+  unsigned int flags;
+} Elf_Internal_Group;
+
+/* Set next_in_group list pointer, and group name for NEWSECT.  */
+
+static boolean
+setup_group (abfd, hdr, newsect)
+     bfd *abfd;
+     Elf_Internal_Shdr *hdr;
+     asection *newsect;
+{
+  unsigned int num_group = elf_tdata (abfd)->num_group;
+
+  /* If num_group is zero, read in all SHT_GROUP sections.  The count
+     is set to -1 if there are no SHT_GROUP sections.  */
+  if (num_group == 0)
+    {
+      unsigned int i, shnum;
+
+      /* First count the number of groups.  If we have a SHT_GROUP
+        section with just a flag word (ie. sh_size is 4), ignore it.  */
+      shnum = elf_elfheader (abfd)->e_shnum;
+      num_group = 0;
+      for (i = 0; i < shnum; i++)
+       {
+         Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
+         if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
+           num_group += 1;
+       }
+
+      if (num_group == 0)
+       num_group = (unsigned) -1;
+      elf_tdata (abfd)->num_group = num_group;
+
+      if (num_group > 0)
+       {
+         /* 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 *);
+         elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
+         if (elf_tdata (abfd)->group_sect_ptr == NULL)
+           return false;
+
+         num_group = 0;
+         for (i = 0; i < shnum; i++)
+           {
+             Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
+             if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
+               {
+                 unsigned char *src;
+                 Elf_Internal_Group *dest;
+
+                 /* Add to list of sections.  */
+                 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
+                 num_group += 1;
+
+                 /* Read the raw contents.  */
+                 BFD_ASSERT (sizeof (*dest) >= 4);
+                 amt = shdr->sh_size * sizeof (*dest) / 4;
+                 shdr->contents = bfd_alloc (abfd, amt);
+                 if (shdr->contents == NULL
+                     || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
+                     || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
+                         != shdr->sh_size))
+                   return false;
+
+                 /* Translate raw contents, a flag word followed by an
+                    array of elf section indices all in target byte order,
+                    to the flag word followed by an array of elf section
+                    pointers.  */
+                 src = shdr->contents + shdr->sh_size;
+                 dest = (Elf_Internal_Group *) (shdr->contents + amt);
+                 while (1)
+                   {
+                     unsigned int idx;
+
+                     src -= 4;
+                     --dest;
+                     idx = H_GET_32 (abfd, src);
+                     if (src == shdr->contents)
+                       {
+                         dest->flags = idx;
+                         break;
+                       }
+                     if (idx >= shnum)
+                       {
+                         ((*_bfd_error_handler)
+                          (_("%s: invalid SHT_GROUP entry"),
+                           bfd_archive_filename (abfd)));
+                         idx = 0;
+                       }
+                     dest->shdr = elf_elfsections (abfd)[idx];
+                   }
+               }
+           }
+       }
+    }
+
+  if (num_group != (unsigned) -1)
+    {
+      unsigned int i;
+
+      for (i = 0; i < num_group; i++)
+       {
+         Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
+         Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
+         unsigned int n_elt = shdr->sh_size / 4;
+
+         /* Look through this group's sections to see if current
+            section is a member.  */
+         while (--n_elt != 0)
+           if ((++idx)->shdr == hdr)
+             {
+               asection *s = NULL;
+
+               /* We are a member of this group.  Go looking through
+                  other members to see if any others are linked via
+                  next_in_group.  */
+               idx = (Elf_Internal_Group *) shdr->contents;
+               n_elt = shdr->sh_size / 4;
+               while (--n_elt != 0)
+                 if ((s = (++idx)->shdr->bfd_section) != NULL
+                     && elf_next_in_group (s) != NULL)
+                   break;
+               if (n_elt != 0)
+                 {
+                   /* Snarf the group name from other member, and
+                      insert current section in circular list.  */
+                   elf_group_name (newsect) = elf_group_name (s);
+                   elf_next_in_group (newsect) = elf_next_in_group (s);
+                   elf_next_in_group (s) = newsect;
+                 }
+               else
+                 {
+                   struct elf_backend_data *bed;
+                   file_ptr pos;
+                   unsigned char ename[4];
+                   unsigned long iname;
+                   const char *gname;
+
+                   /* Humbug.  Get the name from the group signature
+                      symbol.  Why isn't the signature just a string?
+                      Fortunately, the name index is at the same
+                      place in the external symbol for both 32 and 64
+                      bit ELF.  */
+                   bed = get_elf_backend_data (abfd);
+                   pos = elf_tdata (abfd)->symtab_hdr.sh_offset;
+                   pos += shdr->sh_info * bed->s->sizeof_sym;
+                   if (bfd_seek (abfd, pos, SEEK_SET) != 0
+                       || bfd_bread (ename, (bfd_size_type) 4, abfd) != 4)
+                     return false;
+                   iname = H_GET_32 (abfd, ename);
+                   gname = elf_string_from_elf_strtab (abfd, iname);
+                   elf_group_name (newsect) = gname;
+
+                   /* Start a circular list with one element.  */
+                   elf_next_in_group (newsect) = newsect;
+                 }
+               if (shdr->bfd_section != NULL)
+                 elf_next_in_group (shdr->bfd_section) = newsect;
+               i = num_group - 1;
+               break;
+             }
+       }
+    }
+
+  if (elf_group_name (newsect) == NULL)
+    {
+      (*_bfd_error_handler) (_("%s: no group info for section %s"),
+                            bfd_archive_filename (abfd), newsect->name);
+    }
+  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.  */
 
@@ -380,6 +563,8 @@ _bfd_elf_make_section_from_shdr (abfd, hdr, name)
   flags = SEC_NO_FLAGS;
   if (hdr->sh_type != SHT_NOBITS)
     flags |= SEC_HAS_CONTENTS;
+  if (hdr->sh_type == SHT_GROUP)
+    flags |= SEC_GROUP | SEC_EXCLUDE;
   if ((hdr->sh_flags & SHF_ALLOC) != 0)
     {
       flags |= SEC_ALLOC;
@@ -399,6 +584,9 @@ _bfd_elf_make_section_from_shdr (abfd, hdr, name)
       if ((hdr->sh_flags & SHF_STRINGS) != 0)
        flags |= SEC_STRINGS;
     }
+  if (hdr->sh_flags & SHF_GROUP)
+    if (!setup_group (abfd, hdr, newsect))
+      return false;
 
   /* The debugging sections appear to be recognized only by name, not
      any sort of flag.  */
@@ -412,7 +600,7 @@ _bfd_elf_make_section_from_shdr (abfd, hdr, name)
     };
     int i;
 
-    for (i = sizeof (debug_sec_names) / sizeof (debug_sec_names[0]); i--;)
+    for (i = ARRAY_SIZE (debug_sec_names); i--;)
       if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
        break;
 
@@ -456,17 +644,30 @@ _bfd_elf_make_section_from_shdr (abfd, hdr, name)
          phdr = elf_tdata (abfd)->phdr;
          for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
            {
+             /* This section is part of this segment if its file
+                offset plus size lies within the segment's memory
+                span and, if the section is loaded, the extent of the
+                loaded data lies within the extent of the segment.  
+                If the p_paddr field is not set, we don't alter the 
+                LMA.  */
              if (phdr->p_type == PT_LOAD
-                 && phdr->p_vaddr != phdr->p_paddr
-                 && phdr->p_vaddr <= hdr->sh_addr
-                 && (phdr->p_vaddr + phdr->p_memsz
-                     >= hdr->sh_addr + hdr->sh_size)
+                 && phdr->p_paddr
+                 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
+                 && (hdr->sh_offset + hdr->sh_size
+                     <= phdr->p_offset + phdr->p_memsz)
                  && ((flags & SEC_LOAD) == 0
-                     || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
-                         && (phdr->p_offset + phdr->p_filesz
-                             >= hdr->sh_offset + hdr->sh_size))))
+                     || (phdr->p_offset + phdr->p_filesz
+                         >= hdr->sh_offset + hdr->sh_size)))
                {
-                 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
+                 /* We used to do a relative adjustment here, but
+                    that doesn't work if the segment is packed with
+                    code from multiple VMAs.  Instead we calculate
+                    the LMA absoultely, based on the LMA of the
+                    segment (it is assumed that the segment will
+                    contain sections with contiguous LMAs, even if
+                    the VMAs are not).  */
+                 newsect->lma = phdr->p_paddr
+                   + hdr->sh_offset - phdr->p_offset;
                  break;
                }
            }
@@ -611,6 +812,7 @@ _bfd_elf_print_private_bfd_data (abfd, farg)
            case PT_NOTE: pt = "NOTE"; break;
            case PT_SHLIB: pt = "SHLIB"; break;
            case PT_PHDR: pt = "PHDR"; break;
+           case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
            default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
            }
          fprintf (f, "%8s off    0x", pt);
@@ -841,6 +1043,7 @@ bfd_elf_print_symbol (abfd, filep, symbol, how)
        const char *name = NULL;
        struct elf_backend_data *bed;
        unsigned char st_other;
+       bfd_vma val;
 
        section_name = symbol->section ? symbol->section->name : "(*none*)";
 
@@ -859,10 +1062,11 @@ bfd_elf_print_symbol (abfd, filep, symbol, how)
           we've already printed the size; now print the alignment.
           For other symbols, we have no specified alignment, and
           we've printed the address; now print the size.  */
-       bfd_fprintf_vma (abfd, file,
-                        (bfd_is_com_section (symbol->section)
-                         ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
-                         : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
+       if (bfd_is_com_section (symbol->section))
+         val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
+       else
+         val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
+       bfd_fprintf_vma (abfd, file, val);
 
        /* If we have version information, print it.  */
        if (elf_tdata (abfd)->dynversym_section != 0
@@ -944,31 +1148,31 @@ _bfd_elf_link_hash_newfunc (entry, table, string)
      struct bfd_hash_table *table;
      const char *string;
 {
-  struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
-
   /* Allocate the structure if it has not already been allocated by a
      subclass.  */
-  if (ret == (struct elf_link_hash_entry *) NULL)
-    ret = ((struct elf_link_hash_entry *)
-          bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
-  if (ret == (struct elf_link_hash_entry *) NULL)
-    return (struct bfd_hash_entry *) ret;
+  if (entry == NULL)
+    {
+      entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
+      if (entry == NULL)
+       return entry;
+    }
 
   /* Call the allocation method of the superclass.  */
-  ret = ((struct elf_link_hash_entry *)
-        _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
-                                table, string));
-  if (ret != (struct elf_link_hash_entry *) NULL)
+  entry = _bfd_link_hash_newfunc (entry, table, string);
+  if (entry != NULL)
     {
+      struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
+      struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
+
       /* Set local fields.  */
       ret->indx = -1;
       ret->size = 0;
       ret->dynindx = -1;
       ret->dynstr_index = 0;
       ret->weakdef = NULL;
-      ret->got.offset = (bfd_vma) -1;
-      ret->plt.offset = (bfd_vma) -1;
-      ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
+      ret->got.refcount = htab->init_refcount;
+      ret->plt.refcount = htab->init_refcount;
+      ret->linker_section_pointer = NULL;
       ret->verinfo.verdef = NULL;
       ret->vtable_entries_used = NULL;
       ret->vtable_entries_size = 0;
@@ -982,16 +1186,18 @@ _bfd_elf_link_hash_newfunc (entry, table, string)
       ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
     }
 
-  return (struct bfd_hash_entry *) ret;
+  return entry;
 }
 
 /* Copy data from an indirect symbol to its direct symbol, hiding the
-   old indirect symbol.  */
+   old indirect symbol.  Also used for copying flags to a weakdef.  */
 
 void
 _bfd_elf_link_hash_copy_indirect (dir, ind)
      struct elf_link_hash_entry *dir, *ind;
 {
+  bfd_signed_vma tmp;
+
   /* Copy down any references that we may have already seen to the
      symbol which just became indirect.  */
 
@@ -1002,21 +1208,28 @@ _bfd_elf_link_hash_copy_indirect (dir, ind)
        | ELF_LINK_HASH_REF_REGULAR_NONWEAK
        | ELF_LINK_NON_GOT_REF));
 
-  /* Copy over the global and procedure linkage table offset entries.
+  if (ind->root.type != bfd_link_hash_indirect)
+    return;
+
+  /* Copy over the global and procedure linkage table refcount entries.
      These may have been already set up by a check_relocs routine.  */
-  if (dir->got.offset == (bfd_vma) -1)
+  tmp = dir->got.refcount;
+  if (tmp <= 0)
     {
-      dir->got.offset = ind->got.offset;
-      ind->got.offset = (bfd_vma) -1;
+      dir->got.refcount = ind->got.refcount;
+      ind->got.refcount = tmp;
     }
-  BFD_ASSERT (ind->got.offset == (bfd_vma) -1);
+  else
+    BFD_ASSERT (ind->got.refcount <= 0);
 
-  if (dir->plt.offset == (bfd_vma) -1)
+  tmp = dir->plt.refcount;
+  if (tmp <= 0)
     {
-      dir->plt.offset = ind->plt.offset;
-      ind->plt.offset = (bfd_vma) -1;
+      dir->plt.refcount = ind->plt.refcount;
+      ind->plt.refcount = tmp;
     }
-  BFD_ASSERT (ind->plt.offset == (bfd_vma) -1);
+  else
+    BFD_ASSERT (ind->plt.refcount <= 0);
 
   if (dir->dynindx == -1)
     {
@@ -1025,7 +1238,8 @@ _bfd_elf_link_hash_copy_indirect (dir, ind)
       ind->dynindx = -1;
       ind->dynstr_index = 0;
     }
-  BFD_ASSERT (ind->dynindx == -1);
+  else
+    BFD_ASSERT (ind->dynindx == -1);
 }
 
 void
@@ -1053,6 +1267,7 @@ _bfd_elf_link_hash_table_init (table, abfd, newfunc)
 
   table->dynamic_sections_created = false;
   table->dynobj = NULL;
+  table->init_refcount = get_elf_backend_data (abfd)->can_refcount - 1;
   /* The first dynamic symbol is a dummy.  */
   table->dynsymcount = 1;
   table->dynstr = NULL;
@@ -1501,6 +1716,26 @@ bfd_section_from_shdr (abfd, shindex)
     case SHT_SHLIB:
       return true;
 
+    case SHT_GROUP:
+      /* Make a section for objcopy and relocatable links.  */
+      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
+       return false;
+      if (hdr->contents != NULL)
+       {
+         Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
+         unsigned int n_elt = hdr->sh_size / 4;
+         asection *s;
+
+         while (--n_elt != 0)
+           if ((s = (++idx)->shdr->bfd_section) != NULL
+               && elf_next_in_group (s) != NULL)
+             {
+               elf_next_in_group (hdr->bfd_section) = s;
+               break;
+             }
+       }
+      break;
+
     default:
       /* Check for any processor-specific section types.  */
       {
@@ -1513,6 +1748,62 @@ bfd_section_from_shdr (abfd, shindex)
   return true;
 }
 
+/* Return the section for the local symbol specified by ABFD, R_SYMNDX.
+   Return SEC for sections that have no elf section, and NULL on error.  */
+
+asection *
+bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
+     bfd *abfd;
+     struct sym_sec_cache *cache;
+     asection *sec;
+     unsigned long r_symndx;
+{
+  unsigned char esym_shndx[2];
+  unsigned int isym_shndx;
+  Elf_Internal_Shdr *symtab_hdr;
+  file_ptr pos;
+  bfd_size_type amt;
+  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
+
+  if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
+    return cache->sec[ent];
+
+  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
+  pos = symtab_hdr->sh_offset;
+  if (get_elf_backend_data (abfd)->s->sizeof_sym
+      == sizeof (Elf64_External_Sym))
+    {
+      pos += r_symndx * sizeof (Elf64_External_Sym);
+      pos += offsetof (Elf64_External_Sym, st_shndx);
+    }
+  else
+    {
+      pos += r_symndx * sizeof (Elf32_External_Sym);
+      pos += offsetof (Elf32_External_Sym, st_shndx);
+    }
+  amt = sizeof (esym_shndx);
+  if (bfd_seek (abfd, pos, SEEK_SET) != 0
+      || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
+    return NULL;
+  isym_shndx = H_GET_16 (abfd, esym_shndx);
+
+  if (cache->abfd != abfd)
+    {
+      memset (cache->indx, -1, sizeof (cache->indx));
+      cache->abfd = abfd;
+    }
+  cache->indx[ent] = r_symndx;
+  cache->sec[ent] = sec;
+  if (isym_shndx > 0 && isym_shndx < SHN_LORESERVE)
+    {
+      asection *s;
+      s = bfd_section_from_elf_index (abfd, isym_shndx);
+      if (s != NULL)
+       cache->sec[ent] = s;
+    }
+  return cache->sec[ent];
+}
+
 /* Given an ELF section number, retrieve the corresponding BFD
    section.  */
 
@@ -1705,8 +1996,8 @@ _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
     return false;
   sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
   rel_hdr->sh_name =
-    (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
-                                      true, false);
+    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
+                                       false);
   if (rel_hdr->sh_name == (unsigned int) -1)
     return false;
   rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
@@ -1743,9 +2034,8 @@ elf_fake_sections (abfd, asect, failedptrarg)
 
   this_hdr = &elf_section_data (asect)->this_hdr;
 
-  this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
-                                                         asect->name,
-                                                         true, false);
+  this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
+                                                          asect->name, false);
   if (this_hdr->sh_name == (unsigned long) -1)
     {
       *failedptr = true;
@@ -1836,6 +2126,11 @@ elf_fake_sections (abfd, asect, failedptrarg)
        BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
                    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
     }
+  else if ((asect->flags & SEC_GROUP) != 0)
+    {
+      this_hdr->sh_type = SHT_GROUP;
+      this_hdr->sh_entsize = 4;
+    }
   else if ((asect->flags & SEC_ALLOC) != 0
           && ((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0))
     this_hdr->sh_type = SHT_NOBITS;
@@ -1855,6 +2150,8 @@ elf_fake_sections (abfd, asect, failedptrarg)
       if ((asect->flags & SEC_STRINGS) != 0)
        this_hdr->sh_flags |= SHF_STRINGS;
     }
+  if (elf_group_name (asect) != NULL)
+    this_hdr->sh_flags |= SHF_GROUP;
 
   /* Check for processor-specific section types.  */
   if (bed->elf_backend_fake_sections)
@@ -1872,6 +2169,83 @@ elf_fake_sections (abfd, asect, failedptrarg)
     *failedptr = true;
 }
 
+/* Fill in the contents of a SHT_GROUP section.  */
+
+static void
+set_group_contents (abfd, sec, failedptrarg)
+     bfd *abfd;
+     asection *sec;
+     PTR failedptrarg ATTRIBUTE_UNUSED;
+{
+  boolean *failedptr = (boolean *) failedptrarg;
+  unsigned long symindx;
+  asection *elt;
+  unsigned char *loc;
+  struct bfd_link_order *l;
+
+  if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
+      || *failedptr)
+    return;
+
+  /* If called from the assembler, swap_out_syms will have set up
+     elf_section_syms;  If called for "ld -r", the symbols won't yet
+     be mapped, so emulate elf_bfd_final_link.  */
+  if (elf_section_syms (abfd) != NULL)
+    symindx = elf_section_syms (abfd)[sec->index]->udata.i;
+  else
+    symindx = elf_section_data (sec)->this_idx;
+  elf_section_data (sec)->this_hdr.sh_info = symindx;
+
+  /* Nor will the contents be allocated for "ld -r".  */
+  if (sec->contents == NULL)
+    {
+      sec->contents = bfd_alloc (abfd, sec->_raw_size);
+      if (sec->contents == NULL)
+       {
+         *failedptr = true;
+         return;
+       }
+    }
+
+  loc = sec->contents + sec->_raw_size;
+
+  /* Get the pointer to the first section in the group that we
+     squirreled away here.  */
+  elt = elf_next_in_group (sec);
+
+  /* First element is a flag word.  Rest of section is elf section
+     indices for all the sections of the group.  Write them backwards
+     just to keep the group in the same order as given in .section
+     directives, not that it matters.  */
+  while (elt != NULL)
+    {
+      loc -= 4;
+      H_PUT_32 (abfd, elf_section_data (elt)->this_idx, loc);
+      elt = elf_next_in_group (elt);
+    }
+
+  /* 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)
+    if (l->type == bfd_indirect_link_order
+       && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
+      do
+       {
+         loc -= 4;
+         H_PUT_32 (abfd,
+                   elf_section_data (elt->output_section)->this_idx, loc);
+         elt = elf_next_in_group (elt);
+         /* During a relocatable link, the lists are circular.  */
+       }
+      while (elt != elf_next_in_group (l->u.indirect.section));
+
+  loc -= 4;
+  H_PUT_32 (abfd, 0, loc);
+
+  BFD_ASSERT (loc == sec->contents);
+}
+
 /* Assign all ELF section numbers.  The dummy first section is handled here
    too.  The link/info pointers for the standard section types are filled
    in here too, while we're at it.  */
@@ -1882,38 +2256,51 @@ assign_section_numbers (abfd)
 {
   struct elf_obj_tdata *t = elf_tdata (abfd);
   asection *sec;
-  unsigned int section_number;
+  unsigned int section_number, secn;
   Elf_Internal_Shdr **i_shdrp;
   bfd_size_type amt;
 
   section_number = 1;
 
+  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
+
   for (sec = abfd->sections; sec; sec = sec->next)
     {
       struct bfd_elf_section_data *d = elf_section_data (sec);
 
       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;
       else
-       d->rel_idx = section_number++;
+       {
+         d->rel_idx = section_number++;
+         _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
+       }
 
       if (d->rel_hdr2)
-       d->rel_idx2 = section_number++;
+       {
+         d->rel_idx2 = section_number++;
+         _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
+       }
       else
        d->rel_idx2 = 0;
     }
 
   t->shstrtab_section = section_number++;
+  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
   elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
-  t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
 
   if (bfd_get_symcount (abfd) > 0)
     {
       t->symtab_section = section_number++;
+      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
       t->strtab_section = section_number++;
+      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
     }
 
+  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
+  t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   elf_elfheader (abfd)->e_shnum = section_number;
 
   /* Set up the list of section header pointers, in agreement with the
@@ -2044,9 +2431,16 @@ assign_section_numbers (abfd)
          if (s != NULL)
            d->this_hdr.sh_link = elf_section_data (s)->this_idx;
          break;
+
+       case SHT_GROUP:
+         d->this_hdr.sh_link = t->symtab_section;
        }
     }
 
+  for (secn = 1; secn < section_number; ++secn)
+    i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
+                                                    i_shdrp[secn]->sh_name);
+
   return true;
 }
 
@@ -2080,7 +2474,6 @@ elf_map_symbols (abfd)
   unsigned int num_locals2 = 0;
   unsigned int num_globals2 = 0;
   int max_index = 0;
-  unsigned int num_sections = 0;
   unsigned int idx;
   asection *asect;
   asymbol **new_syms;
@@ -2091,8 +2484,6 @@ elf_map_symbols (abfd)
   fflush (stderr);
 #endif
 
-  /* Add a section symbol for each BFD section.  FIXME: Is this really
-     necessary?  */
   for (asect = abfd->sections; asect; asect = asect->next)
     {
       if (max_index < asect->index)
@@ -2107,6 +2498,8 @@ elf_map_symbols (abfd)
   elf_section_syms (abfd) = sect_syms;
   elf_num_section_syms (abfd) = max_index;
 
+  /* Init sect_syms entries for any section symbols we have already
+     decided to output.  */
   for (idx = 0; idx < symcount; idx++)
     {
       asymbol *sym = syms[idx];
@@ -2127,15 +2520,16 @@ elf_map_symbols (abfd)
 
                  sec = sec->output_section;
 
-                 /* Empty sections in the input files may have had a section
-                    symbol created for them.  (See the comment near the end of
-                    _bfd_generic_link_output_symbols in linker.c).  If the linker
-                    script discards such sections then we will reach this point.
-                    Since we know that we cannot avoid this case, we detect it
-                    and skip the abort and the assignment to the sect_syms array.
-                    To reproduce this particular case try running the linker
-                    testsuite test ld-scripts/weak.exp for an ELF port that uses
-                    the generic linker.  */
+                 /* Empty sections in the input files may have had a
+                    section symbol created for them.  (See the comment
+                    near the end of _bfd_generic_link_output_symbols in
+                    linker.c).  If the linker script discards such
+                    sections then we will reach this point.  Since we know
+                    that we cannot avoid this case, we detect it and skip
+                    the abort and the assignment to the sect_syms array.
+                    To reproduce this particular case try running the
+                    linker testsuite test ld-scripts/weak.exp for an ELF
+                    port that uses the generic linker.  */
                  if (sec->owner == NULL)
                    continue;
 
@@ -2146,31 +2540,6 @@ elf_map_symbols (abfd)
        }
     }
 
-  for (asect = abfd->sections; asect; asect = asect->next)
-    {
-      asymbol *sym;
-
-      if (sect_syms[asect->index] != NULL)
-       continue;
-
-      sym = bfd_make_empty_symbol (abfd);
-      if (sym == NULL)
-       return false;
-      sym->the_bfd = abfd;
-      sym->name = asect->name;
-      sym->value = 0;
-      /* Set the flags to 0 to indicate that this one was newly added.  */
-      sym->flags = 0;
-      sym->section = asect;
-      sect_syms[asect->index] = sym;
-      num_sections++;
-#ifdef DEBUG
-      fprintf (stderr,
- _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
-              asect->name, (long) asect->vma, asect->index, (long) asect);
-#endif
-    }
-
   /* Classify all of the symbols.  */
   for (idx = 0; idx < symcount; idx++)
     {
@@ -2179,17 +2548,19 @@ elf_map_symbols (abfd)
       else
        num_globals++;
     }
+
+  /* We will be adding a section symbol for each BFD section.  Most normal
+     sections will already have a section symbol in outsymbols, but
+     eg. SHT_GROUP sections will not, and we need the section symbol mapped
+     at least in that case.  */
   for (asect = abfd->sections; asect; asect = asect->next)
     {
-      if (sect_syms[asect->index] != NULL
-         && sect_syms[asect->index]->flags == 0)
+      if (sect_syms[asect->index] == NULL)
        {
-         sect_syms[asect->index]->flags = BSF_SECTION_SYM;
-         if (!sym_is_global (abfd, sect_syms[asect->index]))
+         if (!sym_is_global (abfd, asect->symbol))
            num_locals++;
          else
            num_globals++;
-         sect_syms[asect->index]->flags = 0;
        }
     }
 
@@ -2214,13 +2585,12 @@ elf_map_symbols (abfd)
     }
   for (asect = abfd->sections; asect; asect = asect->next)
     {
-      if (sect_syms[asect->index] != NULL
-         && sect_syms[asect->index]->flags == 0)
+      if (sect_syms[asect->index] == NULL)
        {
-         asymbol *sym = sect_syms[asect->index];
+         asymbol *sym = asect->symbol;
          unsigned int i;
 
-         sym->flags = BSF_SECTION_SYM;
+         sect_syms[asect->index] = sym;
          if (!sym_is_global (abfd, sym))
            i = num_locals2++;
          else
@@ -2320,12 +2690,19 @@ _bfd_elf_compute_section_file_positions (abfd, link_info)
        return false;
     }
 
+  if (link_info == NULL || link_info->relocateable)
+    {
+      bfd_map_over_sections (abfd, set_group_contents, &failed);
+      if (failed)
+       return false;
+    }
+
   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
   /* sh_name was set in prep_headers.  */
   shstrtab_hdr->sh_type = SHT_STRTAB;
   shstrtab_hdr->sh_flags = 0;
   shstrtab_hdr->sh_addr = 0;
-  shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
+  shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
   shstrtab_hdr->sh_entsize = 0;
   shstrtab_hdr->sh_link = 0;
   shstrtab_hdr->sh_info = 0;
@@ -2418,7 +2795,7 @@ map_sections_to_segments (abfd)
   asection **hdrpp;
   boolean phdr_in_segment = true;
   boolean writable;
-  asection *dynsec;
+  asection *dynsec, *eh_frame_hdr;
   bfd_size_type amt;
 
   if (elf_tdata (abfd)->segment_map != NULL)
@@ -2658,6 +3035,24 @@ map_sections_to_segments (abfd)
        }
     }
 
+  /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
+     segment.  */
+  eh_frame_hdr = bfd_get_section_by_name (abfd, ".eh_frame_hdr");
+  if (eh_frame_hdr != NULL && (eh_frame_hdr->flags & SEC_LOAD))
+    {
+      amt = sizeof (struct elf_segment_map);
+      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
+      if (m == NULL)
+       goto error_return;
+      m->next = NULL;
+      m->p_type = PT_GNU_EH_FRAME;
+      m->count = 1;
+      m->sections[0] = eh_frame_hdr;
+
+      *pm = m;
+      pm = &m->next;
+    }
+
   free (sections);
   sections = NULL;
 
@@ -2958,10 +3353,10 @@ assign_file_positions_for_segments (abfd)
 
          /* The section may have artificial alignment forced by a
             link script.  Notice this case by the gap between the
-            cumulative phdr vma and the section's vma.  */
-         if (p->p_vaddr + p->p_memsz < sec->vma)
+            cumulative phdr lma and the section's lma.  */
+         if (p->p_paddr + p->p_memsz < sec->lma)
            {
-             bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
+             bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
 
              p->p_memsz += adjust;
              off += adjust;
@@ -2999,13 +3394,11 @@ assign_file_positions_for_segments (abfd)
                {
                  if (i == 0)
                    {
-                     (* _bfd_error_handler)
-                       (_("Error: First section in segment (%s) starts at 0x%x"),
-                        bfd_section_name (abfd, sec), sec->lma);
-                     (* _bfd_error_handler)
-                       (_("       whereas segment starts at 0x%x"),
-                        p->p_paddr);
-
+                     (* _bfd_error_handler) (_("\
+Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
+                                             bfd_section_name (abfd, sec),
+                                             sec->lma,
+                                             p->p_paddr);
                      return false;
                    }
                  p->p_memsz += adjust;
@@ -3174,6 +3567,13 @@ get_program_header_size (abfd)
       ++segs;
     }
 
+  if (elf_tdata (abfd)->eh_frame_hdr
+      && bfd_get_section_by_name (abfd, ".eh_frame_hdr") != NULL)
+    {
+      /* We need a PT_GNU_EH_FRAME segment.  */
+      ++segs;
+    }
+
   for (s = abfd->sections; s != NULL; s = s->next)
     {
       if ((s->flags & SEC_LOAD) != 0
@@ -3316,13 +3716,13 @@ prep_headers (abfd)
   Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
   Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
   int count;
-  struct bfd_strtab_hash *shstrtab;
+  struct elf_strtab_hash *shstrtab;
   struct elf_backend_data *bed = get_elf_backend_data (abfd);
 
   i_ehdrp = elf_elfheader (abfd);
   i_shdrp = elf_elfsections (abfd);
 
-  shstrtab = _bfd_elf_stringtab_init ();
+  shstrtab = _bfd_elf_strtab_init ();
   if (shstrtab == NULL)
     return false;
 
@@ -3408,11 +3808,11 @@ prep_headers (abfd)
     }
 
   elf_tdata (abfd)->symtab_hdr.sh_name =
-    (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
+    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
   elf_tdata (abfd)->strtab_hdr.sh_name =
-    (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
+    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
   elf_tdata (abfd)->shstrtab_hdr.sh_name =
-    (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
+    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
       || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
@@ -3491,7 +3891,7 @@ _bfd_elf_write_object_contents (abfd)
 
   /* Write out the section header names.  */
   if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
-      || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
+      || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
     return false;
 
   if (bed->elf_backend_final_write_processing)
@@ -3690,16 +4090,19 @@ copy_private_bfd_data (ibfd, obfd)
 
   /* Decide if the given section should be included in the given segment.
      A section will be included if:
-       1. It is within the address space of the segment,
+       1. It is within the address space of the segment -- we use the LMA
+          if that is set for the segment and the VMA otherwise,
        2. It is an allocated segment,
        3. There is an output section associated with it,
        4. The section has not already been allocated to a previous segment.  */
-#define INCLUDE_SECTION_IN_SEGMENT(section, segment)   \
-  ((((IS_CONTAINED_BY_VMA (section, segment)           \
-      || IS_SOLARIS_PT_INTERP (segment, section))      \
-     && (section->flags & SEC_ALLOC) != 0)             \
-    || IS_COREFILE_NOTE (segment, section))            \
-   && section->output_section != NULL                  \
+#define INCLUDE_SECTION_IN_SEGMENT(section, segment)                   \
+  (((((segment->p_paddr                                                        \
+       ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)      \
+       : IS_CONTAINED_BY_VMA (section, segment))                       \
+      || IS_SOLARIS_PT_INTERP (segment, section))                      \
+     && (section->flags & SEC_ALLOC) != 0)                             \
+    || IS_COREFILE_NOTE (segment, section))                            \
+   && section->output_section != NULL                                  \
    && section->segment_mark == false)
 
   /* Returns true iff seg1 starts after the end of seg2.  */
@@ -3919,7 +4322,8 @@ copy_private_bfd_data (ibfd, obfd)
                                                 ? iehdr->e_ehsize
                                                 : 0)
                                              + (map->includes_phdrs
-                                                ? iehdr->e_phnum * iehdr->e_phentsize
+                                                ? (iehdr->e_phnum
+                                                   * iehdr->e_phentsize)
                                                 : 0))))
                map->p_paddr = segment->p_vaddr;
 
@@ -4044,9 +4448,11 @@ copy_private_bfd_data (ibfd, obfd)
                      /* If the gap between the end of the previous section
                         and the start of this section is more than
                         maxpagesize then we need to start a new segment.  */
-                     if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
+                     if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
+                                     maxpagesize)
                          < BFD_ALIGN (output_section->lma, maxpagesize))
-                         || ((prev_sec->lma + prev_sec->_raw_size) > output_section->lma))
+                         || ((prev_sec->lma + prev_sec->_raw_size)
+                             > output_section->lma))
                        {
                          if (suggested_lma == 0)
                            suggested_lma = output_section->lma;
@@ -4278,223 +4684,225 @@ swap_out_syms (abfd, sttp, relocatable_p)
      struct bfd_strtab_hash **sttp;
      int relocatable_p;
 {
-  struct elf_backend_data *bed = get_elf_backend_data (abfd);
+  struct elf_backend_data *bed;
+  int symcount;
+  asymbol **syms;
+  struct bfd_strtab_hash *stt;
+  Elf_Internal_Shdr *symtab_hdr;
+  Elf_Internal_Shdr *symstrtab_hdr;
+  char *outbound_syms;
+  int idx;
+  bfd_size_type amt;
 
   if (!elf_map_symbols (abfd))
     return false;
 
   /* Dump out the symtabs.  */
-  {
-    int symcount = bfd_get_symcount (abfd);
-    asymbol **syms = bfd_get_outsymbols (abfd);
-    struct bfd_strtab_hash *stt;
-    Elf_Internal_Shdr *symtab_hdr;
-    Elf_Internal_Shdr *symstrtab_hdr;
-    char *outbound_syms;
-    int idx;
-    bfd_size_type amt;
-
-    stt = _bfd_elf_stringtab_init ();
-    if (stt == NULL)
-      return false;
-
-    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
-    symtab_hdr->sh_type = SHT_SYMTAB;
-    symtab_hdr->sh_entsize = bed->s->sizeof_sym;
-    symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
-    symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
-    symtab_hdr->sh_addralign = bed->s->file_align;
+  stt = _bfd_elf_stringtab_init ();
+  if (stt == NULL)
+    return false;
 
-    symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
-    symstrtab_hdr->sh_type = SHT_STRTAB;
+  bed = get_elf_backend_data (abfd);
+  symcount = bfd_get_symcount (abfd);
+  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
+  symtab_hdr->sh_type = SHT_SYMTAB;
+  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
+  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
+  symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
+  symtab_hdr->sh_addralign = bed->s->file_align;
+
+  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
+  symstrtab_hdr->sh_type = SHT_STRTAB;
+
+  amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
+  outbound_syms = bfd_alloc (abfd, amt);
+  if (outbound_syms == NULL)
+    return false;
+  symtab_hdr->contents = (PTR) outbound_syms;
 
-    amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
-    outbound_syms = bfd_alloc (abfd, amt);
-    if (outbound_syms == NULL)
-      return false;
-    symtab_hdr->contents = (PTR) outbound_syms;
+  /* now generate the data (for "contents") */
+  {
+    /* Fill in zeroth symbol and swap it out.  */
+    Elf_Internal_Sym sym;
+    sym.st_name = 0;
+    sym.st_value = 0;
+    sym.st_size = 0;
+    sym.st_info = 0;
+    sym.st_other = 0;
+    sym.st_shndx = SHN_UNDEF;
+    bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
+    outbound_syms += bed->s->sizeof_sym;
+  }
 
-    /* now generate the data (for "contents") */
+  syms = bfd_get_outsymbols (abfd);
+  for (idx = 0; idx < symcount; idx++)
     {
-      /* Fill in zeroth symbol and swap it out.  */
       Elf_Internal_Sym sym;
-      sym.st_name = 0;
-      sym.st_value = 0;
-      sym.st_size = 0;
-      sym.st_info = 0;
-      sym.st_other = 0;
-      sym.st_shndx = SHN_UNDEF;
-      bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
-      outbound_syms += bed->s->sizeof_sym;
-    }
-    for (idx = 0; idx < symcount; idx++)
-      {
-       Elf_Internal_Sym sym;
-       bfd_vma value = syms[idx]->value;
-       elf_symbol_type *type_ptr;
-       flagword flags = syms[idx]->flags;
-       int type;
+      bfd_vma value = syms[idx]->value;
+      elf_symbol_type *type_ptr;
+      flagword flags = syms[idx]->flags;
+      int type;
 
-       if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
-         {
-           /* Local section symbols have no name.  */
-           sym.st_name = 0;
-         }
-       else
-         {
-           sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
-                                                             syms[idx]->name,
-                                                             true, false);
-           if (sym.st_name == (unsigned long) -1)
-             return false;
-         }
+      if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
+       {
+         /* Local section symbols have no name.  */
+         sym.st_name = 0;
+       }
+      else
+       {
+         sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
+                                                           syms[idx]->name,
+                                                           true, false);
+         if (sym.st_name == (unsigned long) -1)
+           return false;
+       }
 
-       type_ptr = elf_symbol_from (abfd, syms[idx]);
+      type_ptr = elf_symbol_from (abfd, syms[idx]);
 
-       if ((flags & BSF_SECTION_SYM) == 0
-           && bfd_is_com_section (syms[idx]->section))
-         {
-           /* ELF common symbols put the alignment into the `value' field,
-              and the size into the `size' field.  This is backwards from
-              how BFD handles it, so reverse it here.  */
-           sym.st_size = value;
-           if (type_ptr == NULL
-               || type_ptr->internal_elf_sym.st_value == 0)
-             sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
-           else
-             sym.st_value = type_ptr->internal_elf_sym.st_value;
-           sym.st_shndx = _bfd_elf_section_from_bfd_section
-             (abfd, syms[idx]->section);
-         }
-       else
-         {
-           asection *sec = syms[idx]->section;
-           int shndx;
+      if ((flags & BSF_SECTION_SYM) == 0
+         && bfd_is_com_section (syms[idx]->section))
+       {
+         /* ELF common symbols put the alignment into the `value' field,
+            and the size into the `size' field.  This is backwards from
+            how BFD handles it, so reverse it here.  */
+         sym.st_size = value;
+         if (type_ptr == NULL
+             || type_ptr->internal_elf_sym.st_value == 0)
+           sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
+         else
+           sym.st_value = type_ptr->internal_elf_sym.st_value;
+         sym.st_shndx = _bfd_elf_section_from_bfd_section
+           (abfd, syms[idx]->section);
+       }
+      else
+       {
+         asection *sec = syms[idx]->section;
+         int shndx;
 
-           if (sec->output_section)
-             {
-               value += sec->output_offset;
-               sec = sec->output_section;
-             }
-           /* Don't add in the section vma for relocatable output.  */
-           if (! relocatable_p)
-             value += sec->vma;
-           sym.st_value = value;
-           sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
-
-           if (bfd_is_abs_section (sec)
-               && type_ptr != NULL
-               && type_ptr->internal_elf_sym.st_shndx != 0)
-             {
-               /* This symbol is in a real ELF section which we did
-                   not create as a BFD section.  Undo the mapping done
-                   by copy_private_symbol_data.  */
-               shndx = type_ptr->internal_elf_sym.st_shndx;
-               switch (shndx)
-                 {
-                 case MAP_ONESYMTAB:
-                   shndx = elf_onesymtab (abfd);
-                   break;
-                 case MAP_DYNSYMTAB:
-                   shndx = elf_dynsymtab (abfd);
-                   break;
-                 case MAP_STRTAB:
-                   shndx = elf_tdata (abfd)->strtab_section;
-                   break;
-                 case MAP_SHSTRTAB:
-                   shndx = elf_tdata (abfd)->shstrtab_section;
-                   break;
-                 default:
-                   break;
-                 }
-             }
-           else
-             {
-               shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
+         if (sec->output_section)
+           {
+             value += sec->output_offset;
+             sec = sec->output_section;
+           }
+         /* Don't add in the section vma for relocatable output.  */
+         if (! relocatable_p)
+           value += sec->vma;
+         sym.st_value = value;
+         sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
+
+         if (bfd_is_abs_section (sec)
+             && type_ptr != NULL
+             && type_ptr->internal_elf_sym.st_shndx != 0)
+           {
+             /* This symbol is in a real ELF section which we did
+                not create as a BFD section.  Undo the mapping done
+                by copy_private_symbol_data.  */
+             shndx = type_ptr->internal_elf_sym.st_shndx;
+             switch (shndx)
+               {
+               case MAP_ONESYMTAB:
+                 shndx = elf_onesymtab (abfd);
+                 break;
+               case MAP_DYNSYMTAB:
+                 shndx = elf_dynsymtab (abfd);
+                 break;
+               case MAP_STRTAB:
+                 shndx = elf_tdata (abfd)->strtab_section;
+                 break;
+               case MAP_SHSTRTAB:
+                 shndx = elf_tdata (abfd)->shstrtab_section;
+                 break;
+               default:
+                 break;
+               }
+           }
+         else
+           {
+             shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
 
-               if (shndx == -1)
-                 {
-                   asection *sec2;
-
-                   /* Writing this would be a hell of a lot easier if
-                      we had some decent documentation on bfd, and
-                      knew what to expect of the library, and what to
-                      demand of applications.  For example, it
-                      appears that `objcopy' might not set the
-                      section of a symbol to be a section that is
-                      actually in the output file.  */
-                   sec2 = bfd_get_section_by_name (abfd, sec->name);
-                   BFD_ASSERT (sec2 != 0);
-                   shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
-                   BFD_ASSERT (shndx != -1);
-                 }
-             }
+             if (shndx == -1)
+               {
+                 asection *sec2;
+
+                 /* Writing this would be a hell of a lot easier if
+                    we had some decent documentation on bfd, and
+                    knew what to expect of the library, and what to
+                    demand of applications.  For example, it
+                    appears that `objcopy' might not set the
+                    section of a symbol to be a section that is
+                    actually in the output file.  */
+                 sec2 = bfd_get_section_by_name (abfd, sec->name);
+                 BFD_ASSERT (sec2 != 0);
+                 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
+                 BFD_ASSERT (shndx != -1);
+               }
+           }
 
-           sym.st_shndx = shndx;
-         }
+         sym.st_shndx = shndx;
+       }
 
-       if ((flags & BSF_FUNCTION) != 0)
-         type = STT_FUNC;
-       else if ((flags & BSF_OBJECT) != 0)
-         type = STT_OBJECT;
-       else
-         type = STT_NOTYPE;
+      if ((flags & BSF_FUNCTION) != 0)
+       type = STT_FUNC;
+      else if ((flags & BSF_OBJECT) != 0)
+       type = STT_OBJECT;
+      else
+       type = STT_NOTYPE;
 
-        /* Processor-specific types */
-        if (type_ptr != NULL
-           && bed->elf_backend_get_symbol_type)
-          type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
+      /* Processor-specific types */
+      if (type_ptr != NULL
+         && bed->elf_backend_get_symbol_type)
+       type = ((*bed->elf_backend_get_symbol_type)
+               (&type_ptr->internal_elf_sym, type));
 
-       if (flags & BSF_SECTION_SYM)
-         {
-           if (flags & BSF_GLOBAL)
-             sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
-           else
-             sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
-         }
-       else if (bfd_is_com_section (syms[idx]->section))
-         sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
-       else if (bfd_is_und_section (syms[idx]->section))
-         sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
-                                     ? STB_WEAK
-                                     : STB_GLOBAL),
-                                    type);
-       else if (flags & BSF_FILE)
-         sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
-       else
-         {
-           int bind = STB_LOCAL;
+      if (flags & BSF_SECTION_SYM)
+       {
+         if (flags & BSF_GLOBAL)
+           sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
+         else
+           sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
+       }
+      else if (bfd_is_com_section (syms[idx]->section))
+       sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
+      else if (bfd_is_und_section (syms[idx]->section))
+       sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
+                                   ? STB_WEAK
+                                   : STB_GLOBAL),
+                                  type);
+      else if (flags & BSF_FILE)
+       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
+      else
+       {
+         int bind = STB_LOCAL;
 
-           if (flags & BSF_LOCAL)
-             bind = STB_LOCAL;
-           else if (flags & BSF_WEAK)
-             bind = STB_WEAK;
-           else if (flags & BSF_GLOBAL)
-             bind = STB_GLOBAL;
+         if (flags & BSF_LOCAL)
+           bind = STB_LOCAL;
+         else if (flags & BSF_WEAK)
+           bind = STB_WEAK;
+         else if (flags & BSF_GLOBAL)
+           bind = STB_GLOBAL;
 
-           sym.st_info = ELF_ST_INFO (bind, type);
-         }
+         sym.st_info = ELF_ST_INFO (bind, type);
+       }
 
-       if (type_ptr != NULL)
-         sym.st_other = type_ptr->internal_elf_sym.st_other;
-       else
-         sym.st_other = 0;
+      if (type_ptr != NULL)
+       sym.st_other = type_ptr->internal_elf_sym.st_other;
+      else
+       sym.st_other = 0;
 
-       bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
-       outbound_syms += bed->s->sizeof_sym;
-      }
+      bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
+      outbound_syms += bed->s->sizeof_sym;
+    }
 
-    *sttp = stt;
-    symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
-    symstrtab_hdr->sh_type = SHT_STRTAB;
+  *sttp = stt;
+  symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
+  symstrtab_hdr->sh_type = SHT_STRTAB;
 
-    symstrtab_hdr->sh_flags = 0;
-    symstrtab_hdr->sh_addr = 0;
-    symstrtab_hdr->sh_entsize = 0;
-    symstrtab_hdr->sh_link = 0;
-    symstrtab_hdr->sh_info = 0;
-    symstrtab_hdr->sh_addralign = 1;
-  }
+  symstrtab_hdr->sh_flags = 0;
+  symstrtab_hdr->sh_addr = 0;
+  symstrtab_hdr->sh_entsize = 0;
+  symstrtab_hdr->sh_link = 0;
+  symstrtab_hdr->sh_info = 0;
+  symstrtab_hdr->sh_addralign = 1;
 
   return true;
 }
@@ -4558,11 +4966,9 @@ _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
 {
   arelent *tblptr;
   unsigned int i;
+  struct elf_backend_data *bed = get_elf_backend_data (abfd);
 
-  if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
-                                                          section,
-                                                          symbols,
-                                                          false))
+  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
     return -1;
 
   tblptr = section->relocation;
@@ -4579,8 +4985,8 @@ _bfd_elf_get_symtab (abfd, alocation)
      bfd *abfd;
      asymbol **alocation;
 {
-  long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
-    (abfd, alocation, false);
+  struct elf_backend_data *bed = get_elf_backend_data (abfd);
+  long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
 
   if (symcount >= 0)
     bfd_get_symcount (abfd) = symcount;
@@ -4592,8 +4998,8 @@ _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
      bfd *abfd;
      asymbol **alocation;
 {
-  return get_elf_backend_data (abfd)->s->slurp_symbol_table
-    (abfd, alocation, true);
+  struct elf_backend_data *bed = get_elf_backend_data (abfd);
+  return bed->s->slurp_symbol_table (abfd, alocation, true);
 }
 
 /* Return the size required for the dynamic reloc entries.  Any
@@ -5239,7 +5645,7 @@ _bfd_elf_close_and_cleanup (abfd)
   if (bfd_get_format (abfd) == bfd_object)
     {
       if (elf_shstrtab (abfd) != NULL)
-       _bfd_stringtab_free (elf_shstrtab (abfd));
+       _bfd_elf_strtab_free (elf_shstrtab (abfd));
     }
 
   return _bfd_generic_close_and_cleanup (abfd);
@@ -5371,7 +5777,10 @@ elfcore_grok_prstatus (abfd, note)
       offset   = offsetof (prstatus_t, pr_reg);
       memcpy (&prstat, note->descdata, sizeof (prstat));
 
-      elf_tdata (abfd)->core_signal = prstat.pr_cursig;
+      /* Do not overwrite the core signal if it
+        has already been set by another thread.  */
+      if (elf_tdata (abfd)->core_signal == 0)
+       elf_tdata (abfd)->core_signal = prstat.pr_cursig;
       elf_tdata (abfd)->core_pid = prstat.pr_pid;
 
       /* pr_who exists on:
@@ -5394,7 +5803,10 @@ elfcore_grok_prstatus (abfd, note)
       offset   = offsetof (prstatus32_t, pr_reg);
       memcpy (&prstat, note->descdata, sizeof (prstat));
 
-      elf_tdata (abfd)->core_signal = prstat.pr_cursig;
+      /* Do not overwrite the core signal if it
+        has already been set by another thread.  */
+      if (elf_tdata (abfd)->core_signal == 0)
+       elf_tdata (abfd)->core_signal = prstat.pr_cursig;
       elf_tdata (abfd)->core_pid = prstat.pr_pid;
 
       /* pr_who exists on:
@@ -5720,8 +6132,9 @@ elfcore_grok_win32pstatus (abfd, note)
        return false;
 
       sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
-      sect->filepos = note->descpos + offsetof (struct win32_pstatus,
-                                               data.thread_info.thread_context);
+      sect->filepos = (note->descpos
+                      + offsetof (struct win32_pstatus,
+                                  data.thread_info.thread_context));
       sect->flags = SEC_HAS_CONTENTS;
       sect->alignment_power = 2;
 
@@ -5987,3 +6400,76 @@ _bfd_elf_reloc_type_class (rela)
 {
   return reloc_class_normal;
 }
+
+/* For RELA architectures, return what the relocation value for
+   relocation against a local symbol.  */
+
+bfd_vma
+_bfd_elf_rela_local_sym (abfd, sym, sec, rel)
+     bfd *abfd;
+     Elf_Internal_Sym *sym;
+     asection *sec;
+     Elf_Internal_Rela *rel;
+{
+  bfd_vma relocation;
+
+  relocation = (sec->output_section->vma
+               + sec->output_offset
+               + sym->st_value);
+  if ((sec->flags & SEC_MERGE)
+      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
+      && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
+    {
+      asection *msec;
+
+      msec = sec;
+      rel->r_addend =
+       _bfd_merged_section_offset (abfd, &msec,
+                                   elf_section_data (sec)->sec_info,
+                                   sym->st_value + rel->r_addend,
+                                   (bfd_vma) 0)
+       - relocation;
+      rel->r_addend += msec->output_section->vma + msec->output_offset;
+    }
+  return relocation;
+}
+
+bfd_vma
+_bfd_elf_rel_local_sym (abfd, sym, psec, addend)
+     bfd *abfd;
+     Elf_Internal_Sym *sym;
+     asection **psec;
+     bfd_vma addend;
+{     
+  asection *sec = *psec;
+
+  if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
+    return sym->st_value + addend;
+
+  return _bfd_merged_section_offset (abfd, psec,
+                                    elf_section_data (sec)->sec_info,
+                                    sym->st_value + addend, (bfd_vma) 0);
+}
+
+bfd_vma
+_bfd_elf_section_offset (abfd, info, sec, offset)
+     bfd *abfd;
+     struct bfd_link_info *info;
+     asection *sec;
+     bfd_vma offset;
+{
+  struct bfd_elf_section_data *sec_data;
+
+  sec_data = elf_section_data (sec);
+  switch (sec_data->sec_info_type)
+    {
+    case ELF_INFO_TYPE_STABS:
+      return _bfd_stab_section_offset
+       (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,
+        offset);
+    case ELF_INFO_TYPE_EH_FRAME:
+      return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
+    default:
+      return offset;
+    }
+}
This page took 0.046326 seconds and 4 git commands to generate.