* symfile.c (add_psymbol_to_list, add_psymbol_addr_to_list):
[binutils-gdb.git] / bfd / elfcode.h
index bbeb8021a645ea18cef26c9c65d1cb416aad7cfd..471521d18b8cf6044dd557efb104a9cc8831c20a 100644 (file)
@@ -1,5 +1,5 @@
 /* ELF executable support for BFD.
-   Copyright 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
+   Copyright 1991, 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
 
    Written by Fred Fish @ Cygnus Support, from information published
    in "UNIX System V Release 4, Programmers Guide: ANSI C and
@@ -68,7 +68,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #include "sysdep.h"
 #include "bfdlink.h"
 #include "libbfd.h"
-#include "libelf.h"
+#include "elf-bfd.h"
 
 /* Renaming structures, typedefs, macros and functions to be size-specific.  */
 #define Elf_External_Ehdr      NAME(Elf,External_Ehdr)
@@ -146,8 +146,6 @@ extern struct bfd_strtab_hash *_bfd_elf_stringtab_init PARAMS ((void));
 extern boolean bfd_section_from_phdr PARAMS ((bfd *, Elf_Internal_Phdr *,
                                              int));
 
- int _bfd_elf_section_from_bfd_section PARAMS ((bfd *, struct sec *));
-
 static long elf_slurp_symbol_table PARAMS ((bfd *, asymbol **, boolean));
 
 static boolean elf_slurp_reloc_table PARAMS ((bfd *, asection *, asymbol **));
@@ -393,11 +391,13 @@ elf_swap_reloca_out (abfd, src, dst)
 }
 
 INLINE void
-elf_swap_dyn_in (abfd, src, dst)
+elf_swap_dyn_in (abfd, p, dst)
      bfd *abfd;
-     const Elf_External_Dyn *src;
+     const PTR p;
      Elf_Internal_Dyn *dst;
 {
+  const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
+
   dst->d_tag = get_word (abfd, src->d_tag);
   dst->d_un.d_val = get_word (abfd, src->d_un.d_val);
 }
@@ -477,11 +477,11 @@ elf_object_p (abfd)
   switch (x_ehdr.e_ident[EI_DATA])
     {
     case ELFDATA2MSB:          /* Big-endian */
-      if (!abfd->xvec->header_byteorder_big_p)
+      if (! bfd_header_big_endian (abfd))
        goto got_wrong_format_error;
       break;
     case ELFDATA2LSB:          /* Little-endian */
-      if (abfd->xvec->header_byteorder_big_p)
+      if (! bfd_header_little_endian (abfd))
        goto got_wrong_format_error;
       break;
     case ELFDATANONE:          /* No data encoding specified */
@@ -495,7 +495,7 @@ elf_object_p (abfd)
   new_tdata = ((struct elf_obj_tdata *)
               bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)));
   if (new_tdata == NULL)
-    goto got_no_memory_error;
+    goto got_no_match;
   elf_tdata (abfd) = new_tdata;
 
   /* Now that we know the byte order, swap in the rest of the header */
@@ -569,7 +569,7 @@ elf_object_p (abfd)
                            bfd_alloc (abfd,
                                       sizeof (i_shdrp) * i_ehdrp->e_shnum));
   if (!i_shdrp || !elf_elfsections (abfd))
-    goto got_no_memory_error;
+    goto got_no_match;
   if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0)
     goto got_no_match;
   for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
@@ -585,6 +585,34 @@ elf_object_p (abfd)
        goto got_no_match;
     }
 
+  /* Read in the program headers.  */
+  if (i_ehdrp->e_phnum == 0)
+    elf_tdata (abfd)->phdr = NULL;
+  else
+    {
+      Elf_Internal_Phdr *i_phdr;
+      unsigned int i;
+
+      elf_tdata (abfd)->phdr = ((Elf_Internal_Phdr *)
+                               bfd_alloc (abfd,
+                                          (i_ehdrp->e_phnum
+                                           * sizeof (Elf_Internal_Phdr))));
+      if (elf_tdata (abfd)->phdr == NULL)
+       goto got_no_match;
+      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
+       goto got_no_match;
+      i_phdr = elf_tdata (abfd)->phdr;
+      for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
+       {
+         Elf_External_Phdr x_phdr;
+
+         if (bfd_read ((PTR) &x_phdr, sizeof x_phdr, 1, abfd)
+             != sizeof x_phdr)
+           goto got_no_match;
+         elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
+       }
+    }
+
   /* Read in the string table containing the names of the sections.  We
      will need the base pointer to this table later. */
   /* We read this inline now, so that we don't have to go through
@@ -618,9 +646,6 @@ elf_object_p (abfd)
 got_wrong_format_error:
   bfd_set_error (bfd_error_wrong_format);
   goto got_no_match;
-got_no_memory_error:
-  bfd_set_error (bfd_error_no_memory);
-  goto got_no_match;
 got_no_match:
   if (new_tdata != NULL
       && new_tdata->elf_sect_ptr != NULL)
@@ -646,7 +671,7 @@ write_relocs (abfd, sec, data)
   Elf_Internal_Shdr *rela_hdr;
   Elf_External_Rela *outbound_relocas;
   Elf_External_Rel *outbound_relocs;
-  int idx;
+  unsigned int idx;
   int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
   asymbol *last_sym = 0;
   int last_sym_idx = 9999999;  /* should always be written before use */
@@ -671,7 +696,6 @@ write_relocs (abfd, sec, data)
   rela_hdr->contents = (PTR) bfd_alloc (abfd, rela_hdr->sh_size);
   if (rela_hdr->contents == NULL)
     {
-      bfd_set_error (bfd_error_no_memory);
       *failedp = true;
       return;
     }
@@ -799,10 +823,7 @@ write_shdrs_and_ehdr (abfd)
   x_shdrp = (Elf_External_Shdr *)
     bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
   if (!x_shdrp)
-    {
-      bfd_set_error (bfd_error_no_memory);
-      return false;
-    }
+    return false;
 
   for (count = 0; count < i_ehdrp->e_shnum; count++)
     {
@@ -865,20 +886,14 @@ elf_slurp_symbol_table (abfd, symptrs, dynamic)
       symbase = ((elf_symbol_type *)
                 bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type)));
       if (symbase == (elf_symbol_type *) NULL)
-       {
-         bfd_set_error (bfd_error_no_memory);
-         return -1;
-       }
+       return -1;
       sym = symbase;
 
       /* Temporarily allocate room for the raw ELF symbols.  */
       x_symp = ((Elf_External_Sym *)
-               malloc (symcount * sizeof (Elf_External_Sym)));
+               bfd_malloc (symcount * sizeof (Elf_External_Sym)));
       if (x_symp == NULL && symcount != 0)
-       {
-         bfd_set_error (bfd_error_no_memory);
-         goto error_return;
-       }
+       goto error_return;
 
       if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
          != symcount * sizeof (Elf_External_Sym))
@@ -1035,12 +1050,9 @@ elf_slurp_reloc_table (abfd, asect, symbols)
              && (asect->reloc_count
                  == d->rel_hdr.sh_size / d->rel_hdr.sh_entsize));
 
-  allocated = (PTR) malloc ((size_t) d->rel_hdr.sh_size);
+  allocated = (PTR) bfd_malloc ((size_t) d->rel_hdr.sh_size);
   if (allocated == NULL)
-    {
-      bfd_set_error (bfd_error_no_memory);
-      goto error_return;
-    }
+    goto error_return;
 
   if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0
       || (bfd_read (allocated, 1, d->rel_hdr.sh_size, abfd)
@@ -1052,10 +1064,7 @@ elf_slurp_reloc_table (abfd, asect, symbols)
   relents = ((arelent *)
             bfd_alloc (abfd, asect->reloc_count * sizeof (arelent)));
   if (relents == NULL)
-    {
-      bfd_set_error (bfd_error_no_memory);
-      goto error_return;
-    }
+    goto error_return;
 
   entsize = d->rel_hdr.sh_entsize;
   BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
@@ -1260,4 +1269,5 @@ const struct elf_size_info NAME(_bfd_elf,size_info) = {
   elf_swap_symbol_out,
   elf_slurp_reloc_table,
   elf_slurp_symbol_table,
+  elf_swap_dyn_in
 };