* elf.c (_bfd_elf_make_section_from_shdr): New function, based on
[binutils-gdb.git] / bfd / elf.c
index bd62645c61e85bac0187d162c8d552b69f8c7972..b5c23db365d2759acaecf9b5a4244bc92e8da267 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -1,9 +1,5 @@
-/* ELF support for BFD.
-   Copyright (C) 1991 Free Software Foundation, Inc.
-
-   Written by Fred Fish @ Cygnus Support, from information published
-   in "UNIX System V Release 4, Programmers Guide: ANSI C and
-   Programming Support Tools".
+/* ELF executable support for BFD.
+   Copyright 1993 Free Software Foundation, Inc.
 
 This file is part of BFD, the Binary File Descriptor library.
 
@@ -21,840 +17,362 @@ 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
+/*
 
-       /****************************************
-
-                       WARNING
-
-       This is only a partial ELF implementation,
-       incorporating only those parts that are
-       required to get gdb up and running.  It is
-       expected that it will be expanded to a full
-       ELF implementation at some future date.
-
-       Unimplemented stubs call abort() to ensure
-       that they get proper attention if they are
-       ever called.  The stubs are here since
-       this version was hacked from the COFF
-       version, and thus they will probably
-       go away or get expanded appropriately in a
-       future version.
-
-       fnf@cygnus.com
-
-       *****************************************/
-
-
-/* Problems and other issues to resolve.
-
-   (1) BFD expects there to be some fixed number of "sections" in
-        the object file.  I.E. there is a "section_count" variable in the
-       bfd structure which contains the number of sections.  However, ELF
-       supports multiple "views" of a file.  In particular, with current
-       implementations, executable files typically have two tables, a
-       program header table and a section header table, both of which
-       partition the executable.
+SECTION
+       ELF backends
 
-       In ELF-speak, the "linking view" of the file uses the section header
-       table to access "sections" within the file, and the "execution view"
-       uses the program header table to access "segments" within the file.
-       "Segments" typically may contain all the data from one or more
-       "sections".
+       BFD support for ELF formats is being worked on.
+       Currently, the best supported back ends are for sparc and i386
+       (running svr4 or Solaris 2).
 
-       Note that the section header table is optional in ELF executables,
-       but it is this information that is most useful to gdb.  If the
-       section header table is missing, then gdb should probably try
-       to make do with the program header table.  (FIXME)
-
-*/
+       Documentation of the internals of the support code still needs
+       to be written.  The code is changing quickly enough that we
+       haven't bothered yet.
+ */
 
 #include "bfd.h"
 #include "sysdep.h"
+#include "bfdlink.h"
 #include "libbfd.h"
-#include "obstack.h"
-#include "elf-common.h"
-#include "elf-internal.h"
-#include "elf-external.h"
-
-/* Forward data declarations */
-extern bfd_target elf_little_vec, elf_big_vec;
-
-/* Translate an ELF header in external format into an ELF header in internal
-   format. */
-
-static void
-DEFUN(bfd_swap_ehdr_in,(abfd, src, dst),
-      bfd               *abfd AND
-      Elf_External_Ehdr *src AND
-      Elf_Internal_Ehdr *dst)
+#define ARCH_SIZE 0
+#include "libelf.h"
+
+/* Standard ELF hash function.  Do not change this function; you will
+   cause invalid hash tables to be generated.  (Well, you would if this
+   were being used yet.)  */
+unsigned long
+bfd_elf_hash (name)
+     CONST unsigned char *name;
 {
-  bcopy (src -> e_ident, dst -> e_ident, EI_NIDENT);
-  dst -> e_type = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_type);
-  dst -> e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_machine);
-  dst -> e_version = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_version);
-  dst -> e_entry = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_entry);
-  dst -> e_phoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_phoff);
-  dst -> e_shoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_shoff);
-  dst -> e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_flags);
-  dst -> e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_ehsize);
-  dst -> e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phentsize);
-  dst -> e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phnum);
-  dst -> e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shentsize);
-  dst -> e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shnum);
-  dst -> e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shstrndx);
-}
-
-
-/* Translate an ELF section header table entry in external format into an
-   ELF section header table entry in internal format. */
-
-static void
-DEFUN(bfd_swap_shdr_in,(abfd, src, dst),
-      bfd               *abfd AND
-      Elf_External_Shdr *src AND
-      Elf_Internal_Shdr *dst)
-{
-  dst -> sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_name);
-  dst -> sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_type);
-  dst -> sh_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_flags);
-  dst -> sh_addr = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_addr);
-  dst -> sh_offset = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_offset);
-  dst -> sh_size = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_size);
-  dst -> sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_link);
-  dst -> sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_info);
-  dst -> sh_addralign = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_addralign);
-  dst -> sh_entsize = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_entsize);
-}
+  unsigned long h = 0;
+  unsigned long g;
+  int ch;
 
-
-/* Translate an ELF program header table entry in external format into an
-   ELF program header table entry in internal format. */
-
-static void
-DEFUN(bfd_swap_phdr_in,(abfd, src, dst),
-      bfd               *abfd AND
-      Elf_External_Phdr *src AND
-      Elf_Internal_Phdr *dst)
-{
-  dst -> p_type = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_type);
-  dst -> p_offset = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_offset);
-  dst -> p_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_vaddr);
-  dst -> p_paddr = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_paddr);
-  dst -> p_filesz = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_filesz);
-  dst -> p_memsz = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_memsz);
-  dst -> p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_flags);
-  dst -> p_align = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_align);
-}
-
-
-/* Create a new bfd section from an ELF section header. */
-
-static boolean
-DEFUN(bfd_section_from_shdr, (abfd, hdr, shstrtab),
-      bfd               *abfd AND
-      Elf_Internal_Shdr *hdr AND
-      char             *shstrtab)
-{
-  asection *newsect;
-  char *name;
-
-  name = hdr -> sh_name ? shstrtab + hdr -> sh_name : "unnamed";
-  newsect = bfd_make_section (abfd, name);
-  newsect -> vma = hdr -> sh_addr;
-  newsect -> size = hdr -> sh_size;
-  if (!(hdr -> sh_type == SHT_NOBITS))
-    {
-      newsect -> filepos = hdr -> sh_offset;
-      newsect -> flags |= SEC_HAS_CONTENTS;
-    }
-  if (hdr -> sh_flags & SHF_ALLOC)
+  while ((ch = *name++) != '\0')
     {
-      newsect -> flags |= SEC_ALLOC;
-      if (hdr -> sh_type != SHT_NOBITS)
+      h = (h << 4) + ch;
+      if ((g = (h & 0xf0000000)) != 0)
        {
-         newsect -> flags |= SEC_LOAD;
+         h ^= g >> 24;
+         h &= ~g;
        }
     }
-  if (!(hdr -> sh_flags & SHF_WRITE))
-    {
-      newsect -> flags |= SEC_READONLY;
-    }
-  if (hdr -> sh_flags & SHF_EXECINSTR)
-    {
-      newsect -> flags |= SEC_CODE;    /* FIXME: may only contain SOME code */
-    }
-  if (hdr -> sh_type == SHT_SYMTAB)
-    {
-      abfd -> flags |= HAS_SYMS;
-    }
-
-  return (true);
+  return h;
 }
 
-/* Create a new bfd section from an ELF program header.
-
-   Since program segments have no names, we generate a synthetic name
-   of the form segment<NUM>, where NUM is generally the index in the
-   program header table.  For segments that are split (see below) we
-   generate the names segment<NUM>a and segment<NUM>b.
-
-   Note that some program segments may have a file size that is different than
-   (less than) the memory size.  All this means is that at execution the
-   system must allocate the amount of memory specified by the memory size,
-   but only initialize it with the first "file size" bytes read from the
-   file.  This would occur for example, with program segments consisting
-   of combined data+bss.
-
-   To handle the above situation, this routine generates TWO bfd sections
-   for the single program segment.  The first has the length specified by
-   the file size of the segment, and the second has the length specified
-   by the difference between the two sizes.  In effect, the segment is split
-   into it's initialized and uninitialized parts.
+/* Read a specified number of bytes at a specified offset in an ELF
+   file, into a newly allocated buffer, and return a pointer to the
+   buffer. */
 
- */
-
-static boolean
-DEFUN(bfd_section_from_phdr, (abfd, hdr, index),
-      bfd               *abfd AND
-      Elf_Internal_Phdr *hdr AND
-      int               index)
+static char *
+elf_read (abfd, offset, size)
+     bfd * abfd;
+     long offset;
+     int size;
 {
-  asection *newsect;
-  char *name;
-  char namebuf[64];
-  int split;
-
-  split = ((hdr -> p_memsz > 0) &&
-          (hdr -> p_filesz > 0) &&
-          (hdr -> p_memsz > hdr -> p_filesz));
-  sprintf (namebuf, split ? "segment%da" : "segment%d", index);
-  name = bfd_alloc (abfd, strlen (namebuf) + 1);
-  (void) strcpy (name, namebuf);
-  newsect = bfd_make_section (abfd, name);
-  newsect -> vma = hdr -> p_vaddr;
-  newsect -> size = hdr -> p_filesz;
-  newsect -> filepos = hdr -> p_offset;
-  newsect -> flags |= SEC_HAS_CONTENTS;
-  if (hdr -> p_type == PT_LOAD)
+  char *buf;
+
+  if ((buf = bfd_alloc (abfd, size)) == NULL)
     {
-      newsect -> flags |= SEC_ALLOC;
-      newsect -> flags |= SEC_LOAD;
-      if (hdr -> p_flags & PF_X)
-       {
-         /* FIXME: all we known is that it has execute PERMISSION,
-            may be data. */
-         newsect -> flags |= SEC_CODE;
-       }
+      bfd_set_error (bfd_error_no_memory);
+      return NULL;
     }
-  if (!(hdr -> p_flags & PF_W))
+  if (bfd_seek (abfd, offset, SEEK_SET) == -1)
+    return NULL;
+  if (bfd_read ((PTR) buf, size, 1, abfd) != size)
     {
-      newsect -> flags |= SEC_READONLY;
+      if (bfd_get_error () != bfd_error_system_call)
+       bfd_set_error (bfd_error_file_truncated);
+      return NULL;
     }
+  return buf;
+}
 
-  if (split)
+boolean
+elf_mkobject (abfd)
+     bfd * abfd;
+{
+  /* this just does initialization */
+  /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
+  elf_tdata (abfd) = (struct elf_obj_tdata *)
+    bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
+  if (elf_tdata (abfd) == 0)
     {
-      sprintf (namebuf, "segment%db", index);
-      name = bfd_alloc (abfd, strlen (namebuf) + 1);
-      (void) strcpy (name, namebuf);
-      newsect = bfd_make_section (abfd, name);
-      newsect -> vma = hdr -> p_vaddr + hdr -> p_filesz;
-      newsect -> size = hdr -> p_memsz - hdr -> p_filesz;
-      if (hdr -> p_type == PT_LOAD)
-       {
-         newsect -> flags |= SEC_ALLOC;
-         if (hdr -> p_flags & PF_X)
-           {
-             newsect -> flags |= SEC_CODE;
-           }
-       }
-      if (!(hdr -> p_flags & PF_W))
-       {
-         newsect -> flags |= SEC_READONLY;
-       }
+      bfd_set_error (bfd_error_no_memory);
+      return false;
     }
+  /* since everything is done at close time, do we need any
+     initialization? */
 
-  return (true);
+  return true;
 }
 
-/* Begin processing a given object.
-
-   First we validate the file by reading in the ELF header and checking
-   the magic number.
+char *
+elf_get_str_section (abfd, shindex)
+     bfd * abfd;
+     unsigned int shindex;
+{
+  Elf_Internal_Shdr **i_shdrp;
+  char *shstrtab = NULL;
+  unsigned int offset;
+  unsigned int shstrtabsize;
 
-   */
+  i_shdrp = elf_elfsections (abfd);
+  if (i_shdrp == 0 || i_shdrp[shindex] == 0)
+    return 0;
 
-static bfd_target *
-DEFUN (elf_object_p, (abfd), bfd *abfd)
-{
-  Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
-  Elf_Internal_Ehdr i_ehdr;    /* Elf file header, internal form */
-  Elf_External_Shdr *x_shdr;   /* Section header table, external form */
-  Elf_Internal_Shdr *i_shdr;   /* Section header table, internal form */
-  int shindex;
-  char *shstrtab;              /* Internal copy of section header stringtab */
-  int shstrtabsize;            /* Size of section header string table */
-  
-  /* Read in the ELF header in external format.  */
-
-  if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
+  shstrtab = i_shdrp[shindex]->rawdata;
+  if (shstrtab == NULL)
     {
-      bfd_error = system_call_error;
-      return (NULL);
+      /* No cached one, attempt to read, and cache what we read. */
+      offset = i_shdrp[shindex]->sh_offset;
+      shstrtabsize = i_shdrp[shindex]->sh_size;
+      shstrtab = elf_read (abfd, offset, shstrtabsize);
+      i_shdrp[shindex]->rawdata = (void *) shstrtab;
     }
+  return shstrtab;
+}
 
-  /* Now check to see if we have a valid ELF file, and one that BFD can
-     make use of.  The magic number must match, the address size ('class')
-     and byte-swapping must match our XVEC entry, and it must have a
-     section header table (FIXME: See comments re sections at top of this
-     file). */
+char *
+elf_string_from_elf_section (abfd, shindex, strindex)
+     bfd * abfd;
+     unsigned int shindex;
+     unsigned int strindex;
+{
+  Elf_Internal_Shdr *hdr;
 
-  if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
-      x_ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
-      x_ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
-      x_ehdr.e_ident[EI_MAG3] != ELFMAG3)
-    {
-wrong:
-      bfd_error = wrong_format;
-      return (NULL);
-    }
+  if (strindex == 0)
+    return "";
 
-  /* FIXME, Check EI_VERSION here !  */
-
-  switch (x_ehdr.e_ident[EI_CLASS]) {
-  case ELFCLASSNONE:                   /* address size not specified */
-    goto wrong;                        /* No support if can't tell address size */
-  case ELFCLASS32:                     /* 32-bit addresses */
-    break;
-  case ELFCLASS64:                     /* 64-bit addresses */
-    goto wrong;                        /* FIXME: 64 bits not yet supported */
-  default:
-    goto wrong;                        /* No support if unknown address class */
-  }
-
-  /* Switch xvec to match the specified byte order.  */
-  switch (x_ehdr.e_ident[EI_DATA]) {
-  case ELFDATA2MSB:                    /* Big-endian */ 
-    abfd->xvec = &elf_big_vec;
-    break;
-  case ELFDATA2LSB:                    /* Little-endian */
-    abfd->xvec = &elf_little_vec;
-  case ELFDATANONE:                    /* No data encoding specified */
-  default:                             /* Unknown data encoding specified */
-    goto wrong;
-  }
-  
-  /* Now that we know the byte order, swap in the rest of the header */
-  bfd_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
-
-  /* If there is no section header table, we're hosed. */
-  if (i_ehdr.e_shoff == 0)
-    goto wrong;
-
-  if (i_ehdr.e_type == ET_EXEC || i_ehdr.e_type == ET_DYN)
-    {
-      abfd -> flags |= EXEC_P;
-    }
+  hdr = elf_elfsections (abfd)[shindex];
 
-  /* Allocate space for copies of the section header table in external
-     and internal form, seek to the section header table in the file,
-     read it in, and convert it to internal form.  As a simple sanity
-     check, verify that the what BFD thinks is the size of each section
-     header table entry actually matches the size recorded in the file. */
+  if (!hdr->rawdata
+      && elf_get_str_section (abfd, shindex) == NULL)
+    return NULL;
 
-  if (i_ehdr.e_shentsize != sizeof (*x_shdr))
-    goto wrong;
-  if ((x_shdr = (Elf_External_Shdr *)
-       bfd_alloc (abfd, sizeof (*x_shdr) * i_ehdr.e_shnum)) == NULL)
-    {
-      bfd_error = no_memory;
-      return (NULL);
-    }
-  if ((i_shdr = (Elf_Internal_Shdr *)
-       bfd_alloc (abfd, sizeof (*i_shdr) * i_ehdr.e_shnum)) == NULL)
-    {
-      bfd_error = no_memory;
-      return (NULL);
-    }
-  if (bfd_seek (abfd, i_ehdr.e_shoff, SEEK_SET) == -1)
-    {
-      bfd_error = system_call_error;
-      return (NULL);
-    }
-  for (shindex = 0; shindex < i_ehdr.e_shnum; shindex++)
-    {
-      if (bfd_read ((PTR) (x_shdr + shindex), sizeof (*x_shdr), 1, abfd)
-         != sizeof (*x_shdr))
-       {
-         bfd_error = system_call_error;
-         return (NULL);
-       }
-      bfd_swap_shdr_in (abfd, x_shdr + shindex, i_shdr + shindex);
-    }
+  return ((char *) hdr->rawdata) + strindex;
+}
 
-  /* Read in the string table containing the names of the sections.  We
-     will need the base pointer to this table later. */
+/* Make a BFD section from an ELF section.  We store a pointer to the
+   BFD section in the rawdata field of the header.  */
 
-  shstrtabsize = i_shdr[i_ehdr.e_shstrndx].sh_size;
-  if ((shstrtab = bfd_alloc (abfd, shstrtabsize)) == NULL)
-    {
-      bfd_error = no_memory;
-      return (NULL);
-    }
-  if (bfd_seek (abfd, i_shdr[i_ehdr.e_shstrndx].sh_offset, SEEK_SET) == -1)
-    {
-      bfd_error = system_call_error;
-      return (NULL);
-    }
-  if (bfd_read ((PTR) shstrtab, shstrtabsize, 1, abfd) != shstrtabsize)
-    {
-      bfd_error = system_call_error;
-      return (NULL);
-    }
-  
-  /* Once all of the section headers have been read and converted, we
-     can start processing them.  Note that the first section header is
-     a dummy placeholder entry, so we ignore it. */
+boolean
+_bfd_elf_make_section_from_shdr (abfd, hdr, name)
+     bfd *abfd;
+     Elf_Internal_Shdr *hdr;
+     const char *name;
+{
+  asection *newsect;
+  flagword flags;
 
-  for (shindex = 1; shindex < i_ehdr.e_shnum; shindex++)
+  if (hdr->rawdata != NULL)
     {
-      bfd_section_from_shdr (abfd, i_shdr + shindex, shstrtab);
+      BFD_ASSERT (strcmp (name, ((asection *) hdr->rawdata)->name) == 0);
+      return true;
     }
 
-  return (abfd->xvec);
-}
+  newsect = bfd_make_section_anyway (abfd, name);
+  if (newsect == NULL)
+    return false;
 
-/*  Core files are simply standard ELF formatted files that partition
-    the file using the execution view of the file (program header table)
-    rather than the linking view.  In fact, there is no section header
-    table in a core file.
- */
+  newsect->filepos = hdr->sh_offset;
 
-static bfd_target *
-DEFUN (elf_core_file_p, (abfd), bfd *abfd)
-{
-  Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
-  Elf_Internal_Ehdr i_ehdr;    /* Elf file header, internal form */
-  Elf_External_Phdr *x_phdr;   /* Program header table, external form */
-  Elf_Internal_Phdr *i_phdr;   /* Program header table, internal form */
-  int phindex;
-  
-  /* Read in the ELF header in external format.  */
-
-  if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
+  if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
+      || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
+      || ! bfd_set_section_alignment (abfd, newsect,
+                                     bfd_log2 (hdr->sh_addralign)))
+    return false;
+
+  flags = SEC_NO_FLAGS;
+  if (hdr->sh_type != SHT_NOBITS)
+    flags |= SEC_HAS_CONTENTS;
+  if ((hdr->sh_flags & SHF_ALLOC) != 0)
     {
-      bfd_error = system_call_error;
-      return (NULL);
+      flags |= SEC_ALLOC;
+      if (hdr->sh_type != SHT_NOBITS)
+       flags |= SEC_LOAD;
     }
+  if ((hdr->sh_flags & SHF_WRITE) == 0)
+    flags |= SEC_READONLY;
+  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
+    flags |= SEC_CODE;
+  else if ((flags & SEC_ALLOC) != 0)
+    flags |= SEC_DATA;
+
+  /* The debugging sections appear to be recognized only by name, not
+     any sort of flag.  */
+  if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
+      || strncmp (name, ".line", sizeof ".line" - 1) == 0
+      || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
+    flags |= SEC_DEBUGGING;
+
+  if (! bfd_set_section_flags (abfd, newsect, flags))
+    return false;
+
+  hdr->rawdata = (PTR) newsect;
+  elf_section_data (newsect)->this_hdr = *hdr;
+
+  return true;
+}
 
-  /* Now check to see if we have a valid ELF file, and one that BFD can
-     make use of.  The magic number must match, the address size ('class')
-     and byte-swapping must match our XVEC entry, and it must have a
-     program header table (FIXME: See comments re segments at top of this
-     file). */
+/*
+INTERNAL_FUNCTION
+       bfd_elf_find_section
 
-  if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
-      x_ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
-      x_ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
-      x_ehdr.e_ident[EI_MAG3] != ELFMAG3)
-    {
-wrong:
-      bfd_error = wrong_format;
-      return (NULL);
-    }
+SYNOPSIS
+       struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
 
-  /* FIXME, Check EI_VERSION here !  */
-
-  switch (x_ehdr.e_ident[EI_CLASS]) {
-  case ELFCLASSNONE:                   /* address size not specified */
-    goto wrong;                        /* No support if can't tell address size */
-  case ELFCLASS32:                     /* 32-bit addresses */
-    break;
-  case ELFCLASS64:                     /* 64-bit addresses */
-    goto wrong;                        /* FIXME: 64 bits not yet supported */
-  default:
-    goto wrong;                        /* No support if unknown address class */
-  }
-
-  /* Switch xvec to match the specified byte order.  */
-  switch (x_ehdr.e_ident[EI_DATA]) {
-  case ELFDATA2MSB:                    /* Big-endian */ 
-    abfd->xvec = &elf_big_vec;
-    break;
-  case ELFDATA2LSB:                    /* Little-endian */
-    abfd->xvec = &elf_little_vec;
-  case ELFDATANONE:                    /* No data encoding specified */
-  default:                             /* Unknown data encoding specified */
-    goto wrong;
-  }
-  
-  /* Now that we know the byte order, swap in the rest of the header */
-  bfd_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
-
-  /* If there is no program header, or the type is not a core file, then
-     we are hosed. */
-  if (i_ehdr.e_phoff == 0 || i_ehdr.e_type != ET_CORE)
-    goto wrong;
-
-  /* Allocate space for copies of the program header table in external
-     and internal form, seek to the program header table in the file,
-     read it in, and convert it to internal form.  As a simple sanity
-     check, verify that the what BFD thinks is the size of each program
-     header table entry actually matches the size recorded in the file. */
-
-  if (i_ehdr.e_phentsize != sizeof (*x_phdr))
-    goto wrong;
-  if ((x_phdr = (Elf_External_Phdr *)
-       bfd_alloc (abfd, sizeof (*x_phdr) * i_ehdr.e_phnum)) == NULL)
-    {
-      bfd_error = no_memory;
-      return (NULL);
-    }
-  if ((i_phdr = (Elf_Internal_Phdr *)
-       bfd_alloc (abfd, sizeof (*i_phdr) * i_ehdr.e_phnum)) == NULL)
-    {
-      bfd_error = no_memory;
-      return (NULL);
-    }
-  if (bfd_seek (abfd, i_ehdr.e_phoff, SEEK_SET) == -1)
-    {
-      bfd_error = system_call_error;
-      return (NULL);
-    }
-  for (phindex = 0; phindex < i_ehdr.e_phnum; phindex++)
+DESCRIPTION
+       Helper functions for GDB to locate the string tables.
+       Since BFD hides string tables from callers, GDB needs to use an
+       internal hook to find them.  Sun's .stabstr, in particular,
+       isn't even pointed to by the .stab section, so ordinary
+       mechanisms wouldn't work to find it, even if we had some.
+*/
+
+struct elf_internal_shdr *
+bfd_elf_find_section (abfd, name)
+     bfd * abfd;
+     char *name;
+{
+  Elf_Internal_Shdr **i_shdrp;
+  char *shstrtab;
+  unsigned int max;
+  unsigned int i;
+
+  i_shdrp = elf_elfsections (abfd);
+  if (i_shdrp != NULL)
     {
-      if (bfd_read ((PTR) (x_phdr + phindex), sizeof (*x_phdr), 1, abfd)
-         != sizeof (*x_phdr))
+      shstrtab = elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
+      if (shstrtab != NULL)
        {
-         bfd_error = system_call_error;
-         return (NULL);
+         max = elf_elfheader (abfd)->e_shnum;
+         for (i = 1; i < max; i++)
+           if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
+             return i_shdrp[i];
        }
-      bfd_swap_phdr_in (abfd, x_phdr + phindex, i_phdr + phindex);
     }
+  return 0;
+}
 
-  /* Once all of the program headers have been read and converted, we
-     can start processing them. */
+const char *const bfd_elf_section_type_names[] = {
+  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
+  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
+  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
+};
 
-  for (phindex = 0; phindex < i_ehdr.e_phnum; phindex++)
+/* ELF relocs are against symbols.  If we are producing relocateable
+   output, and the reloc is against an external symbol, and nothing
+   has given us any additional addend, the resulting reloc will also
+   be against the same symbol.  In such a case, we don't want to
+   change anything about the way the reloc is handled, since it will
+   all be done at final link time.  Rather than put special case code
+   into bfd_perform_relocation, all the reloc types use this howto
+   function.  It just short circuits the reloc if producing
+   relocateable output against an external symbol.  */
+
+/*ARGSUSED*/
+bfd_reloc_status_type
+bfd_elf_generic_reloc (abfd,
+                      reloc_entry,
+                      symbol,
+                      data,
+                      input_section,
+                      output_bfd,
+                      error_message)
+     bfd *abfd;
+     arelent *reloc_entry;
+     asymbol *symbol;
+     PTR data;
+     asection *input_section;
+     bfd *output_bfd;
+     char **error_message;
+{
+  if (output_bfd != (bfd *) NULL
+      && (symbol->flags & BSF_SECTION_SYM) == 0
+      && (! reloc_entry->howto->partial_inplace
+         || reloc_entry->addend == 0))
     {
-      bfd_section_from_phdr (abfd, i_phdr + phindex, phindex);
+      reloc_entry->address += input_section->output_offset;
+      return bfd_reloc_ok;
     }
 
-  return (abfd->xvec);
-}
-
-static boolean
-DEFUN (elf_mkobject, (abfd), bfd *abfd)
-{
-  fprintf (stderr, "elf_mkobject unimplemented\n");
-  fflush (stderr);
-  abort ();
-  return (false);
-}
-
-static boolean
-DEFUN (elf_write_object_contents, (abfd), bfd *abfd)
-{
-  fprintf (stderr, "elf_write_object_contents unimplemented\n");
-  fflush (stderr);
-  abort ();
-  return (false);
+  return bfd_reloc_continue;
 }
+\f
+/* Generic ELF link code.  */
 
-static unsigned int
-elf_get_symtab_upper_bound(abfd)
-bfd *abfd;
-{
-  fprintf (stderr, "elf_get_symtab_upper_bound unimplemented\n");
-  fflush (stderr);
-  abort ();
-  return (0);
-}
+static struct bfd_hash_entry *elf_link_hash_newfunc
+  PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
 
-static unsigned int
-elf_get_reloc_upper_bound (abfd, asect)
-bfd            *abfd;
-sec_ptr         asect;
-{
-  fprintf (stderr, "elf_get_reloc_upper_bound unimplemented\n");
-  fflush (stderr);
-  abort ();
-  return (0);
-}
+/* Create an entry in an ELF linker hash table.  */
 
-static unsigned int
-elf_canonicalize_reloc (abfd, section, relptr, symbols)
-bfd            *abfd;
-sec_ptr         section;
-arelent       **relptr;
-asymbol       **symbols;
+static struct bfd_hash_entry *
+elf_link_hash_newfunc (entry, table, string)
+     struct bfd_hash_entry *entry;
+     struct bfd_hash_table *table;
+     const char *string;
 {
-  fprintf (stderr, "elf_canonicalize_reloc unimplemented\n");
-  fflush (stderr);
-  abort ();
-  return (0);
-}
+  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)
+    {
+      bfd_set_error (bfd_error_no_memory);
+      return (struct bfd_hash_entry *) ret;
+    }
 
-static unsigned int
-elf_get_symtab (abfd, alocation)
-bfd            *abfd;
-asymbol       **alocation;
-{
-  fprintf (stderr, "elf_get_symtab unimplemented\n");
-  fflush (stderr);
-  abort ();
-  return (0);
-}
+  /* 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)
+    {
+      /* Set local fields.  */
+      ret->indx = -1;
+      ret->size = 0;
+      ret->align = 0;
+      ret->dynindx = -1;
+      ret->dynstr_index = 0;
+      ret->weakdef = NULL;
+      ret->type = STT_NOTYPE;
+      ret->elf_link_hash_flags = 0;
+    }
 
-static asymbol *
-elf_make_empty_symbol(abfd)
-bfd            *abfd;
-{
-  fprintf (stderr, "elf_make_empty_symbol unimplemented\n");
-  fflush (stderr);
-  abort ();
-  return (NULL);
+  return (struct bfd_hash_entry *) ret;
 }
 
-static void 
-DEFUN (elf_print_symbol,(ignore_abfd, filep, symbol, how),
-      bfd            *ignore_abfd AND
-      PTR           filep AND
-      asymbol        *symbol AND
-      bfd_print_symbol_type how)
-{
-  fprintf (stderr, "elf_print_symbol unimplemented\n");
-  fflush (stderr);
-  abort ();
-}
+/* Create an ELF linker hash table.  */
 
-static alent *
-DEFUN (elf_get_lineno,(ignore_abfd, symbol),
-      bfd            *ignore_abfd AND
-      asymbol        *symbol)
+struct bfd_link_hash_table *
+_bfd_elf_link_hash_table_create (abfd)
+     bfd *abfd;
 {
-  fprintf (stderr, "elf_get_lineno unimplemented\n");
-  fflush (stderr);
-  abort ();
-  return (NULL);
-}
+  struct elf_link_hash_table *ret;
 
-static boolean
-DEFUN (elf_set_arch_mach,(abfd, arch, machine),
-      bfd            *abfd AND
-      enum bfd_architecture arch AND
-      unsigned long   machine)
-{
-  fprintf (stderr, "elf_set_arch_mach unimplemented\n");
-  fflush (stderr);
-  /* Allow any architecture to be supported by the elf backend */
-  return  bfd_default_set_arch_mach(abfd, arch, machine);
-}
+  ret = ((struct elf_link_hash_table *)
+        bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
+  if (ret == (struct elf_link_hash_table *) NULL)
+    {
+      bfd_set_error (bfd_error_no_memory);
+      return NULL;
+    }
+  if (! _bfd_link_hash_table_init (&ret->root, abfd,
+                                  elf_link_hash_newfunc))
+    {
+      bfd_release (abfd, ret);
+      return NULL;
+    }
 
-static boolean
-DEFUN (elf_find_nearest_line,(abfd,
-                             section,
-                             symbols,
-                             offset,
-                             filename_ptr,
-                             functionname_ptr,
-                             line_ptr),
-      bfd            *abfd AND
-      asection       *section AND
-      asymbol       **symbols AND
-      bfd_vma         offset AND
-      CONST char      **filename_ptr AND
-      CONST char       **functionname_ptr AND
-      unsigned int   *line_ptr)
-{
-  fprintf (stderr, "elf_find_nearest_line unimplemented\n");
-  fflush (stderr);
-  abort ();
-  return (false);
-}
+  ret->dynobj = NULL;
+  ret->dynsymcount = 0;
+  ret->dynstr = NULL;
+  ret->bucketcount = 0;
 
-static int 
-DEFUN (elf_sizeof_headers, (abfd, reloc),
-      bfd *abfd AND
-      boolean reloc)
-{
-  fprintf (stderr, "elf_sizeof_headers unimplemented\n");
-  fflush (stderr);
-  abort ();
-  return (0);
+  return &ret->root;
 }
-\f
-/* This structure contains everything that BFD knows about a target.
-   It includes things like its byte order, name, what routines to call
-   to do various operations, etc.  Every BFD points to a target structure
-   with its "xvec" member.
-
-   There are two such structures here:  one for big-endian machines and
-   one for little-endian machines.   */
-
-#define elf_core_file_failing_command  _bfd_dummy_core_file_failing_command
-#define elf_core_file_failing_signal   _bfd_dummy_core_file_failing_signal
-#define elf_core_file_matches_executable_p     _bfd_dummy_core_file_matches_executable_p
-
-/* Archives are generic or unimplemented.  */
-#define elf_slurp_armap                        bfd_false
-#define elf_slurp_extended_name_table  _bfd_slurp_extended_name_table
-#define elf_truncate_arname            bfd_dont_truncate_arname
-#define elf_openr_next_archived_file   bfd_generic_openr_next_archived_file
-#define elf_generic_stat_arch_elt      bfd_generic_stat_arch_elt
-#define        elf_write_armap                 (PROTO (boolean, (*),           \
-     (bfd *arch, unsigned int elength, struct orl *map, unsigned int orl_count,        \
-      int stridx))) bfd_false
-
-/* Ordinary section reading and writing */
-#define elf_new_section_hook           _bfd_dummy_new_section_hook
-#define        elf_get_section_contents        bfd_generic_get_section_contents
-#define        elf_set_section_contents        bfd_generic_set_section_contents
-#define        elf_close_and_cleanup           bfd_generic_close_and_cleanup
-
-#define elf_bfd_debug_info_start       bfd_void
-#define elf_bfd_debug_info_end         bfd_void
-#define elf_bfd_debug_info_accumulate  (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
-
-bfd_target elf_big_vec =
-{
-  /* name: identify kind of target */
-  "elf-big",
-
-  /* flavour: general indication about file */
-  bfd_target_elf_flavour,
-
-  /* byteorder_big_p: data is big endian */
-  true,
-
-  /* header_byteorder_big_p: header is also big endian */
-  true,
-
-  /* object_flags: mask of all file flags */
-  (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS |
-   DYNAMIC | WP_TEXT),
-  
-  /* section_flags: mask of all section flags */
-  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY |
-   SEC_DATA), 
-
-  /* ar_pad_char: pad character for filenames within an archive header
-     FIXME:  this really has nothing to do with ELF, this is a characteristic
-     of the archiver and/or os and should be independently tunable */
-  '/',
-
-  /* ar_max_namelen: maximum number of characters in an archive header
-     FIXME:  this really has nothing to do with ELF, this is a characteristic
-     of the archiver and should be independently tunable.  This value is
-     a WAG (wild a** guess) */
-  15,
-
-  /* align_power_min: minimum alignment restriction for any section
-     FIXME:  this value may be target machine dependent */
-  3,
-
-  /* Routines to byte-swap various sized integers from the data sections */
-  _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
-
-  /* Routines to byte-swap various sized integers from the file headers */
-  _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
-
-  /* bfd_check_format: check the format of a file being read */
-  { _bfd_dummy_target,         /* unknown format */
-    elf_object_p,              /* assembler/linker output (object file) */
-    bfd_generic_archive_p,     /* an archive */
-    elf_core_file_p            /* a core file */
-  },
-
-  /* bfd_set_format: set the format of a file being written */
-  { bfd_false,
-    elf_mkobject,
-    _bfd_generic_mkarchive,
-    bfd_false
-  },
-
-  /* bfd_write_contents: write cached information into a file being written */
-  { bfd_false,
-    elf_write_object_contents,
-    _bfd_write_archive_contents,
-    bfd_false
-  },
-
-  /* Initialize a jump table with the standard macro.  All names start
-     with "elf" */
-  JUMP_TABLE(elf),
-
-  /* SWAP_TABLE */
-  NULL, NULL, NULL
-};
-
-bfd_target elf_little_vec =
-{
-  /* name: identify kind of target */
-  "elf-little",
-
-  /* flavour: general indication about file */
-  bfd_target_elf_flavour,
-
-  /* byteorder_big_p: data is big endian */
-  false,               /* Nope -- this one's little endian */
-
-  /* header_byteorder_big_p: header is also big endian */
-  false,               /* Nope -- this one's little endian */
-
-  /* object_flags: mask of all file flags */
-  (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS |
-   DYNAMIC | WP_TEXT),
-  
-  /* section_flags: mask of all section flags */
-  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY |
-   SEC_DATA), 
-
-  /* ar_pad_char: pad character for filenames within an archive header
-     FIXME:  this really has nothing to do with ELF, this is a characteristic
-     of the archiver and/or os and should be independently tunable */
-  '/',
-
-  /* ar_max_namelen: maximum number of characters in an archive header
-     FIXME:  this really has nothing to do with ELF, this is a characteristic
-     of the archiver and should be independently tunable.  This value is
-     a WAG (wild a** guess) */
-  15,
-
-  /* align_power_min: minimum alignment restriction for any section
-     FIXME:  this value may be target machine dependent */
-  3,
-
-  /* Routines to byte-swap various sized integers from the data sections */
-  _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
-
-  /* Routines to byte-swap various sized integers from the file headers */
-  _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
-
-  /* bfd_check_format: check the format of a file being read */
-  { _bfd_dummy_target,         /* unknown format */
-    elf_object_p,              /* assembler/linker output (object file) */
-    bfd_generic_archive_p,     /* an archive */
-    elf_core_file_p            /* a core file */
-  },
-
-  /* bfd_set_format: set the format of a file being written */
-  { bfd_false,
-    elf_mkobject,
-    _bfd_generic_mkarchive,
-    bfd_false
-  },
-
-  /* bfd_write_contents: write cached information into a file being written */
-  { bfd_false,
-    elf_write_object_contents,
-    _bfd_write_archive_contents,
-    bfd_false
-  },
-
-  /* Initialize a jump table with the standard macro.  All names start
-     with "elf" */
-  JUMP_TABLE(elf),
-
-  /* SWAP_TABLE */
-  NULL, NULL, NULL
-};