]> Git Repo - binutils.git/blobdiff - bfd/elf.c
obj-coff* now copes with C mingled listings
[binutils.git] / bfd / elf.c
index a09ff71bc76a1882cec56312cdd7fc09ba78b47d..5230ebb3904af282beda64f250e9e22dde5be4f5 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -72,9 +72,9 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "sysdep.h"
 #include "libbfd.h"
 #include "obstack.h"
-#include "elf-common.h"
-#include "elf-internal.h"
-#include "elf-external.h"
+#include "elf/common.h"
+#include "elf/internal.h"
+#include "elf/external.h"
 
 #ifdef HAVE_PROCFS     /* Some core file support requires host /proc files */
 #include <sys/procfs.h>
@@ -100,16 +100,17 @@ typedef struct
    in the bfd structure.  This information is different for ELF core files
    and other ELF files. */
 
-typedef struct
+typedef struct elf_core_tdata_struct
 {
   void *prstatus;              /* The raw /proc prstatus structure */
   void *prpsinfo;              /* The raw /proc prpsinfo structure */
 } elf_core_tdata;
 
-#define core_prpsinfo(bfd) (((elf_core_tdata *)((bfd)->tdata))->prpsinfo)
-#define core_prstatus(bfd) (((elf_core_tdata *)((bfd)->tdata))->prstatus)
+#define core_prpsinfo(bfd) (((bfd)->tdata.elf_core_data))->prpsinfo)
+#define core_prpstatus(bfd) (((bfd)->tdata.elf_core_data))->prpstatus)
 
-typedef struct
+
+typedef struct elf_obj_tdata_struct
 {
   file_ptr symtab_filepos;     /* Offset to start of ELF symtab section */
   long symtab_filesz;          /* Size of ELF symtab section */
@@ -117,7 +118,7 @@ typedef struct
   long strtab_filesz;          /* Size of ELF string tbl section */
 } elf_obj_tdata;
 
-#define elf_tdata(bfd)         ((elf_obj_tdata *) ((bfd) -> tdata))
+#define elf_tdata(bfd)         ((bfd) -> tdata.elf_obj_data)
 #define elf_symtab_filepos(bfd)        (elf_tdata(bfd) -> symtab_filepos)
 #define elf_symtab_filesz(bfd) (elf_tdata(bfd) -> symtab_filesz)
 #define elf_strtab_filepos(bfd)        (elf_tdata(bfd) -> strtab_filepos)
@@ -141,8 +142,8 @@ DEFUN(elf_swap_symbol_in,(abfd, src, dst),
 }
 
 
-/* Translate an ELF header in external format into an ELF header in internal
-   format. */
+/* Translate an ELF file header in external format into an ELF file header in
+   internal format. */
 
 static void
 DEFUN(elf_swap_ehdr_in,(abfd, src, dst),
@@ -150,7 +151,7 @@ DEFUN(elf_swap_ehdr_in,(abfd, src, dst),
       Elf_External_Ehdr *src AND
       Elf_Internal_Ehdr *dst)
 {
-  bcopy (src -> e_ident, dst -> e_ident, EI_NIDENT);
+  memcpy (dst -> e_ident, src -> 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);
@@ -223,7 +224,7 @@ DEFUN(bfd_section_from_shdr, (abfd, hdr, shstrtab),
   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;
+  newsect -> _raw_size = hdr -> sh_size;
   if (!(hdr -> sh_type == SHT_NOBITS))
     {
       newsect -> filepos = hdr -> sh_offset;
@@ -245,6 +246,10 @@ DEFUN(bfd_section_from_shdr, (abfd, hdr, shstrtab),
     {
       newsect -> flags |= SEC_CODE;    /* FIXME: may only contain SOME code */
     }
+  else
+    {
+      newsect -> flags |= SEC_DATA;
+    }
   if (hdr -> sh_type == SHT_SYMTAB)
     {
       abfd -> flags |= HAS_SYMS;
@@ -294,7 +299,7 @@ DEFUN(bfd_section_from_phdr, (abfd, hdr, index),
   (void) strcpy (name, namebuf);
   newsect = bfd_make_section (abfd, name);
   newsect -> vma = hdr -> p_vaddr;
-  newsect -> size = hdr -> p_filesz;
+  newsect -> _raw_size = hdr -> p_filesz;
   newsect -> filepos = hdr -> p_offset;
   newsect -> flags |= SEC_HAS_CONTENTS;
   if (hdr -> p_type == PT_LOAD)
@@ -320,7 +325,7 @@ DEFUN(bfd_section_from_phdr, (abfd, hdr, index),
       (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;
+      newsect -> _raw_size = hdr -> p_memsz - hdr -> p_filesz;
       if (hdr -> p_type == PT_LOAD)
        {
          newsect -> flags |= SEC_ALLOC;
@@ -358,7 +363,7 @@ DEFUN(bfd_prstatus,(abfd, descdata, descsz, filepos),
       newsect -> alignment_power = 2;
       if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
        {
-         bcopy (descdata, core_prstatus (abfd), descsz);
+         memcpy (core_prstatus (abfd), descdata, descsz);
        }
     }
 }
@@ -414,7 +419,7 @@ char *
 DEFUN(elf_core_file_failing_command, (abfd),
      bfd *abfd)
 {
-#if HAVE_PROCFS
+#ifdef HAVE_PROCFS
   if (core_prpsinfo (abfd))
     {
       prpsinfo_t *p = core_prpsinfo (abfd);
@@ -440,7 +445,7 @@ static int
 DEFUN(elf_core_file_failing_signal, (abfd),
       bfd *abfd)
 {
-#if HAVE_PROCFS
+#ifdef HAVE_PROCFS
   if (core_prstatus (abfd))
     {
       return (((prstatus_t *)(core_prstatus (abfd))) -> pr_cursig);
@@ -460,8 +465,10 @@ DEFUN(elf_core_file_matches_executable_p, (core_bfd, exec_bfd),
       bfd *core_bfd AND
       bfd *exec_bfd)
 {
+#ifdef HAVE_PROCFS
   char *corename;
   char *execname;
+#endif
 
   /* First, xvecs must match since both are ELF files for the same target. */
 
@@ -471,7 +478,7 @@ DEFUN(elf_core_file_matches_executable_p, (core_bfd, exec_bfd),
       return (false);
     }
 
-#if HAVE_PROCFS
+#ifdef HAVE_PROCFS
 
   /* If no prpsinfo, just return true.  Otherwise, grab the last component
      of the exec'd pathname from the prpsinfo. */
@@ -544,7 +551,7 @@ DEFUN(elf_corefile_note, (abfd, hdr),
   asection *newsect;
 
   if (hdr -> p_filesz > 0
-      && (buf = malloc (hdr -> p_filesz)) != NULL
+      && (buf = (char *)malloc(hdr -> p_filesz)) != NULL
       && bfd_seek (abfd, hdr -> p_offset, SEEK_SET) != -1L
       && bfd_read ((PTR) buf, hdr -> p_filesz, 1, abfd) == hdr -> p_filesz)
     {
@@ -581,7 +588,7 @@ DEFUN(elf_corefile_note, (abfd, hdr),
          if (sectname != NULL)
            {
              newsect = bfd_make_section (abfd, sectname);
-             newsect -> size = i_note.descsz;
+             newsect -> _raw_size = i_note.descsz;
              newsect -> filepos = filepos;
              newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
              newsect -> alignment_power = 2;
@@ -594,9 +601,41 @@ DEFUN(elf_corefile_note, (abfd, hdr),
     {
       free (buf);
     }
+  return true;
+  
 }
 
 
+/* 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 char *
+DEFUN(elf_read, (abfd, offset, size),
+      bfd      *abfd AND
+      long     offset AND
+      int      size)
+{
+  char *buf;
+
+  if ((buf = bfd_alloc (abfd, size)) == NULL)
+    {
+      bfd_error = no_memory;
+      return (NULL);
+    }
+  if (bfd_seek (abfd, offset, SEEK_SET) == -1)
+    {
+      bfd_error = system_call_error;
+      return (NULL);
+    }
+  if (bfd_read ((PTR) buf, size, 1, abfd) != size)
+    {
+      bfd_error = system_call_error;
+      return (NULL);
+    }
+  return (buf);
+}
+
 /* Begin processing a given object.
 
    First we validate the file by reading in the ELF header and checking
@@ -614,6 +653,7 @@ DEFUN (elf_object_p, (abfd), bfd *abfd)
   int shindex;
   char *shstrtab;              /* Internal copy of section header stringtab */
   int shstrtabsize;            /* Size of section header string table */
+  Elf_Off offset;              /* Temp place to stash file offsets */
   
   /* Read in the ELF header in external format.  */
 
@@ -659,6 +699,7 @@ wrong:
     break;
   case ELFDATA2LSB:                    /* Little-endian */
     abfd->xvec = &elf_little_vec;
+    break;
   case ELFDATANONE:                    /* No data encoding specified */
   default:                             /* Unknown data encoding specified */
     goto wrong;
@@ -667,7 +708,9 @@ wrong:
   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
      the tdata pointer in the bfd. */
 
-  if ((abfd -> tdata = bfd_zalloc (abfd, sizeof (elf_obj_tdata))) == NULL)
+  if ((abfd -> tdata.elf_obj_data = 
+       (elf_obj_tdata*) bfd_zalloc (abfd, sizeof (elf_obj_tdata))) 
+      == NULL)
     {
       bfd_error = no_memory;
       return (NULL);
@@ -725,22 +768,12 @@ wrong:
      will need the base pointer to this table later. */
 
   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)
+  offset = i_shdr[i_ehdr.e_shstrndx].sh_offset;
+  if ((shstrtab = elf_read (abfd, offset, shstrtabsize)) == NULL)
     {
-      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.
@@ -762,6 +795,10 @@ wrong:
        }
     }
 
+  /* Remember the entry point specified in the ELF file header. */
+
+  bfd_get_start_address (abfd) = i_ehdr.e_entry;
+
   return (abfd->xvec);
 }
 
@@ -831,6 +868,7 @@ wrong:
     break;
   case ELFDATA2LSB:                    /* Little-endian */
     abfd->xvec = &elf_little_vec;
+    break;
   case ELFDATANONE:                    /* No data encoding specified */
   default:                             /* Unknown data encoding specified */
     goto wrong;
@@ -847,7 +885,9 @@ wrong:
   /* Allocate an instance of the elf_core_tdata structure and hook it up to
      the tdata pointer in the bfd. */
 
-  if ((abfd -> tdata = bfd_zalloc (abfd, sizeof (elf_core_tdata))) == NULL)
+  if ((abfd -> tdata.elf_core_data =
+       (elf_core_tdata *) bfd_zalloc (abfd, sizeof (elf_core_tdata))) 
+      == NULL)
     {
       bfd_error = no_memory;
       return (NULL);
@@ -901,6 +941,10 @@ wrong:
        }
     }
 
+  /* Remember the entry point specified in the ELF file header. */
+
+  bfd_get_start_address (abfd) = i_ehdr.e_entry;
+
   return (abfd->xvec);
 }
 
@@ -963,20 +1007,9 @@ DEFUN (elf_slurp_symbol_table, (abfd), bfd *abfd)
      long as the bfd is in use, since we will end up setting up pointers
      into it for the names of all the symbols. */
 
-  if (bfd_seek (abfd, elf_strtab_filepos (abfd), SEEK_SET) == -1)
-    {
-      bfd_error = system_call_error;
-      return (false);
-    }
-  if ((strtab = bfd_alloc (abfd, elf_strtab_filesz (abfd))) == NULL)
+  strtab = elf_read (abfd, elf_strtab_filepos(abfd), elf_strtab_filesz(abfd));
+  if (strtab == NULL)
     {
-      bfd_error = system_call_error;
-      return (false);
-    }
-  if (bfd_read ((PTR) strtab, elf_strtab_filesz (abfd), 1, abfd) !=
-      elf_strtab_filesz (abfd))
-    {
-      bfd_error = system_call_error;
       return (false);
     }
 
@@ -1021,11 +1054,11 @@ DEFUN (elf_slurp_symbol_table, (abfd), bfd *abfd)
            }
          else if (i_sym.st_shndx == SHN_ABS)
            {
-             sym -> flags |= BSF_ABSOLUTE;
+/*           sym -> flags |= BSF_ABSOLUTE; OBSOLETE */
            }
          else if (i_sym.st_shndx == SHN_COMMON)
            {
-             sym -> flags |= BSF_FORT_COMM;
+             sym -> section = &bfd_com_section;
            }
          switch (ELF_ST_BIND (i_sym.st_info))
            {
@@ -1228,6 +1261,8 @@ DEFUN (elf_sizeof_headers, (abfd, reloc),
 #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
+#define elf_bfd_get_relocated_section_contents \
+ bfd_generic_get_relocated_section_contents
 
 bfd_target elf_big_vec =
 {
This page took 0.035363 seconds and 4 git commands to generate.