s/boolean/bfd_boolean/ s/true/TRUE/ s/false/FALSE/. Simplify
[deliverable/binutils-gdb.git] / bfd / elfcode.h
index 1f75fb00ed922f192776232b7a3a62a5f54e2cc1..2a6f6bf20a49f1ffe8afa73914bec267077d5742 100644 (file)
@@ -1,6 +1,6 @@
 /* ELF executable support for BFD.
-   Copyright 1991, 92, 93, 94, 95, 96, 97, 98, 1999 Free Software
-   Foundation, Inc.
+   Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
+   2001, 2002 Free Software Foundation, Inc.
 
    Written by Fred Fish @ Cygnus Support, from information published
    in "UNIX System V Release 4, Programmers Guide: ANSI C and
@@ -66,6 +66,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include "bfd.h"
 #include "sysdep.h"
+#include "libiberty.h"
 #include "bfdlink.h"
 #include "libbfd.h"
 #include "elf-bfd.h"
@@ -118,8 +119,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #define elf_add_dynamic_entry          NAME(bfd_elf,add_dynamic_entry)
 #define elf_write_shdrs_and_ehdr       NAME(bfd_elf,write_shdrs_and_ehdr)
 #define elf_write_out_phdrs            NAME(bfd_elf,write_out_phdrs)
+#define elf_write_relocs               NAME(bfd_elf,write_relocs)
+#define elf_slurp_reloc_table          NAME(bfd_elf,slurp_reloc_table)
 #define elf_link_create_dynamic_sections \
   NAME(bfd_elf,link_create_dynamic_sections)
+#define elf_bfd_discard_info           NAME(bfd_elf,discard_info)
+#define elf_reloc_symbol_deleted_p     NAME(_bfd_elf,reloc_symbol_deleted_p)
 #define elf_link_record_dynamic_symbol  _bfd_elf_link_record_dynamic_symbol
 #define elf_bfd_final_link             NAME(bfd_elf,bfd_final_link)
 #define elf_create_pointer_linker_section NAME(bfd_elf,create_pointer_linker_section)
@@ -165,15 +170,11 @@ static void elf_swap_shdr_out
 
 #define section_from_elf_index bfd_section_from_elf_index
 
-static boolean elf_slurp_reloc_table_from_section 
+static bfd_boolean elf_slurp_reloc_table_from_section
   PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, bfd_size_type,
-          arelent *, asymbol **, boolean));
-static boolean elf_slurp_reloc_table
-  PARAMS ((bfd *, asection *, asymbol **, boolean));
+          arelent *, asymbol **, bfd_boolean));
 
-static void write_relocs PARAMS ((bfd *, asection *, PTR));
-
-static boolean elf_file_p PARAMS ((Elf_External_Ehdr *));
+static bfd_boolean elf_file_p PARAMS ((Elf_External_Ehdr *));
 
 #ifdef DEBUG
 static void elf_debug_section PARAMS ((int, Elf_Internal_Shdr *));
@@ -186,56 +187,79 @@ static char *elf_symbol_flags PARAMS ((flagword));
 /* Should perhaps use put_offset, put_word, etc.  For now, the two versions
    can be handled by explicitly specifying 32 bits or "the long type".  */
 #if ARCH_SIZE == 64
-#define put_word       bfd_h_put_64
-#define put_signed_word        bfd_h_put_signed_64
-#define get_word       bfd_h_get_64
-#define get_signed_word        bfd_h_get_signed_64
+#define H_PUT_WORD             H_PUT_64
+#define H_PUT_SIGNED_WORD      H_PUT_S64
+#define H_GET_WORD             H_GET_64
+#define H_GET_SIGNED_WORD      H_GET_S64
 #endif
 #if ARCH_SIZE == 32
-#define put_word       bfd_h_put_32
-#define put_signed_word        bfd_h_put_signed_32
-#define get_word       bfd_h_get_32
-#define get_signed_word        bfd_h_get_signed_32
+#define H_PUT_WORD             H_PUT_32
+#define H_PUT_SIGNED_WORD      H_PUT_S32
+#define H_GET_WORD             H_GET_32
+#define H_GET_SIGNED_WORD      H_GET_S32
 #endif
 
 /* Translate an ELF symbol in external format into an ELF symbol in internal
-   format. */
+   format.  */
 
 void
-elf_swap_symbol_in (abfd, src, dst)
+elf_swap_symbol_in (abfd, psrc, pshn, dst)
      bfd *abfd;
-     const Elf_External_Sym *src;
+     const PTR psrc;
+     const PTR pshn;
      Elf_Internal_Sym *dst;
 {
-  dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name);
-  dst->st_value = get_word (abfd, (bfd_byte *) src->st_value);
-  dst->st_size = get_word (abfd, (bfd_byte *) src->st_size);
-  dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info);
-  dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other);
-  dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx);
+  const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
+  const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
+  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
+
+  dst->st_name = H_GET_32 (abfd, src->st_name);
+  if (signed_vma)
+    dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
+  else
+    dst->st_value = H_GET_WORD (abfd, src->st_value);
+  dst->st_size = H_GET_WORD (abfd, src->st_size);
+  dst->st_info = H_GET_8 (abfd, src->st_info);
+  dst->st_other = H_GET_8 (abfd, src->st_other);
+  dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
+  if (dst->st_shndx == SHN_XINDEX)
+    {
+      if (shndx == NULL)
+       abort ();
+      dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
+    }
 }
 
 /* Translate an ELF symbol in internal format into an ELF symbol in external
-   format. */
+   format.  */
 
 void
-elf_swap_symbol_out (abfd, src, cdst)
+elf_swap_symbol_out (abfd, src, cdst, shndx)
      bfd *abfd;
      const Elf_Internal_Sym *src;
      PTR cdst;
+     PTR shndx;
 {
+  unsigned int tmp;
   Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
-  bfd_h_put_32 (abfd, src->st_name, dst->st_name);
-  put_word (abfd, src->st_value, dst->st_value);
-  put_word (abfd, src->st_size, dst->st_size);
-  bfd_h_put_8 (abfd, src->st_info, dst->st_info);
-  bfd_h_put_8 (abfd, src->st_other, dst->st_other);
-  bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);
+  H_PUT_32 (abfd, src->st_name, dst->st_name);
+  H_PUT_WORD (abfd, src->st_value, dst->st_value);
+  H_PUT_WORD (abfd, src->st_size, dst->st_size);
+  H_PUT_8 (abfd, src->st_info, dst->st_info);
+  H_PUT_8 (abfd, src->st_other, dst->st_other);
+  tmp = src->st_shndx;
+  if (tmp > SHN_HIRESERVE)
+    {
+      if (shndx == NULL)
+       abort ();
+      H_PUT_32 (abfd, tmp, shndx);
+      tmp = SHN_XINDEX;
+    }
+  H_PUT_16 (abfd, tmp, dst->st_shndx);
 }
 
-
 /* Translate an ELF file header in external format into an ELF file header in
-   internal format. */
+   internal format.  */
 
 static void
 elf_swap_ehdr_in (abfd, src, dst)
@@ -243,24 +267,28 @@ elf_swap_ehdr_in (abfd, src, dst)
      const Elf_External_Ehdr *src;
      Elf_Internal_Ehdr *dst;
 {
+  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
   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);
-  dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry);
-  dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff);
-  dst->e_shoff = get_word (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);
+  dst->e_type = H_GET_16 (abfd, src->e_type);
+  dst->e_machine = H_GET_16 (abfd, src->e_machine);
+  dst->e_version = H_GET_32 (abfd, src->e_version);
+  if (signed_vma)
+    dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
+  else
+    dst->e_entry = H_GET_WORD (abfd, src->e_entry);
+  dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
+  dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
+  dst->e_flags = H_GET_32 (abfd, src->e_flags);
+  dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
+  dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
+  dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
+  dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
+  dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
+  dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
 }
 
 /* Translate an ELF file header in internal format into an ELF file header in
-   external format. */
+   external format.  */
 
 static void
 elf_swap_ehdr_out (abfd, src, dst)
@@ -268,26 +296,36 @@ elf_swap_ehdr_out (abfd, src, dst)
      const Elf_Internal_Ehdr *src;
      Elf_External_Ehdr *dst;
 {
+  unsigned int tmp;
+  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
   memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
-  /* note that all elements of dst are *arrays of unsigned char* already... */
-  bfd_h_put_16 (abfd, src->e_type, dst->e_type);
-  bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);
-  bfd_h_put_32 (abfd, src->e_version, dst->e_version);
-  put_word (abfd, src->e_entry, dst->e_entry);
-  put_word (abfd, src->e_phoff, dst->e_phoff);
-  put_word (abfd, src->e_shoff, dst->e_shoff);
-  bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);
-  bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);
-  bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);
-  bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);
-  bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);
-  bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);
-  bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);
+  /* note that all elements of dst are *arrays of unsigned char* already...  */
+  H_PUT_16 (abfd, src->e_type, dst->e_type);
+  H_PUT_16 (abfd, src->e_machine, dst->e_machine);
+  H_PUT_32 (abfd, src->e_version, dst->e_version);
+  if (signed_vma)
+    H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
+  else
+    H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
+  H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
+  H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
+  H_PUT_32 (abfd, src->e_flags, dst->e_flags);
+  H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
+  H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
+  H_PUT_16 (abfd, src->e_phnum, dst->e_phnum);
+  H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
+  tmp = src->e_shnum;
+  if (tmp >= SHN_LORESERVE)
+    tmp = SHN_UNDEF;
+  H_PUT_16 (abfd, tmp, dst->e_shnum);
+  tmp = src->e_shstrndx;
+  if (tmp >= SHN_LORESERVE)
+    tmp = SHN_XINDEX;
+  H_PUT_16 (abfd, tmp, dst->e_shstrndx);
 }
 
-
 /* Translate an ELF section header table entry in external format into an
-   ELF section header table entry in internal format. */
+   ELF section header table entry in internal format.  */
 
 static void
 elf_swap_shdr_in (abfd, src, dst)
@@ -295,22 +333,27 @@ elf_swap_shdr_in (abfd, src, dst)
      const Elf_External_Shdr *src;
      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 = get_word (abfd, (bfd_byte *) src->sh_flags);
-  dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr);
-  dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset);
-  dst->sh_size = get_word (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 = get_word (abfd, (bfd_byte *) src->sh_addralign);
-  dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize);
+  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
+
+  dst->sh_name = H_GET_32 (abfd, src->sh_name);
+  dst->sh_type = H_GET_32 (abfd, src->sh_type);
+  dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
+  if (signed_vma)
+    dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
+  else
+    dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
+  dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
+  dst->sh_size = H_GET_WORD (abfd, src->sh_size);
+  dst->sh_link = H_GET_32 (abfd, src->sh_link);
+  dst->sh_info = H_GET_32 (abfd, src->sh_info);
+  dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
+  dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
   dst->bfd_section = NULL;
   dst->contents = NULL;
 }
 
 /* Translate an ELF section header table entry in internal format into an
-   ELF section header table entry in external format. */
+   ELF section header table entry in external format.  */
 
 static void
 elf_swap_shdr_out (abfd, src, dst)
@@ -318,22 +361,21 @@ elf_swap_shdr_out (abfd, src, dst)
      const Elf_Internal_Shdr *src;
      Elf_External_Shdr *dst;
 {
-  /* note that all elements of dst are *arrays of unsigned char* already... */
-  bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);
-  bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);
-  put_word (abfd, src->sh_flags, dst->sh_flags);
-  put_word (abfd, src->sh_addr, dst->sh_addr);
-  put_word (abfd, src->sh_offset, dst->sh_offset);
-  put_word (abfd, src->sh_size, dst->sh_size);
-  bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);
-  bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);
-  put_word (abfd, src->sh_addralign, dst->sh_addralign);
-  put_word (abfd, src->sh_entsize, dst->sh_entsize);
+  /* note that all elements of dst are *arrays of unsigned char* already...  */
+  H_PUT_32 (abfd, src->sh_name, dst->sh_name);
+  H_PUT_32 (abfd, src->sh_type, dst->sh_type);
+  H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
+  H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
+  H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
+  H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
+  H_PUT_32 (abfd, src->sh_link, dst->sh_link);
+  H_PUT_32 (abfd, src->sh_info, dst->sh_info);
+  H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
+  H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
 }
 
-
 /* Translate an ELF program header table entry in external format into an
-   ELF program header table entry in internal format. */
+   ELF program header table entry in internal format.  */
 
 void
 elf_swap_phdr_in (abfd, src, dst)
@@ -341,14 +383,24 @@ elf_swap_phdr_in (abfd, src, dst)
      const Elf_External_Phdr *src;
      Elf_Internal_Phdr *dst;
 {
-  dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
-  dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);
-  dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset);
-  dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr);
-  dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr);
-  dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz);
-  dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz);
-  dst->p_align = get_word (abfd, (bfd_byte *) src->p_align);
+  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
+
+  dst->p_type = H_GET_32 (abfd, src->p_type);
+  dst->p_flags = H_GET_32 (abfd, src->p_flags);
+  dst->p_offset = H_GET_WORD (abfd, src->p_offset);
+  if (signed_vma)
+    {
+      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
+      dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
+    }
+  else
+    {
+      dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
+      dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
+    }
+  dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
+  dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
+  dst->p_align = H_GET_WORD (abfd, src->p_align);
 }
 
 void
@@ -357,59 +409,64 @@ elf_swap_phdr_out (abfd, src, dst)
      const Elf_Internal_Phdr *src;
      Elf_External_Phdr *dst;
 {
-  /* note that all elements of dst are *arrays of unsigned char* already... */
-  bfd_h_put_32 (abfd, src->p_type, dst->p_type);
-  put_word (abfd, src->p_offset, dst->p_offset);
-  put_word (abfd, src->p_vaddr, dst->p_vaddr);
-  put_word (abfd, src->p_paddr, dst->p_paddr);
-  put_word (abfd, src->p_filesz, dst->p_filesz);
-  put_word (abfd, src->p_memsz, dst->p_memsz);
-  bfd_h_put_32 (abfd, src->p_flags, dst->p_flags);
-  put_word (abfd, src->p_align, dst->p_align);
+  /* note that all elements of dst are *arrays of unsigned char* already...  */
+  H_PUT_32 (abfd, src->p_type, dst->p_type);
+  H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
+  H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
+  H_PUT_WORD (abfd, src->p_paddr, dst->p_paddr);
+  H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
+  H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
+  H_PUT_32 (abfd, src->p_flags, dst->p_flags);
+  H_PUT_WORD (abfd, src->p_align, dst->p_align);
 }
 
-/* Translate an ELF reloc from external format to internal format. */
-INLINE void
-elf_swap_reloc_in (abfd, src, dst)
+/* Translate an ELF reloc from external format to internal format.  */
+void
+elf_swap_reloc_in (abfd, s, dst)
      bfd *abfd;
-     const Elf_External_Rel *src;
-     Elf_Internal_Rel *dst;
+     const bfd_byte *s;
+     Elf_Internal_Rela *dst;
 {
-  dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
-  dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
+  const Elf_External_Rel *src = (const Elf_External_Rel *) s;
+  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
+  dst->r_info = H_GET_WORD (abfd, src->r_info);
+  dst->r_addend = 0;
 }
 
-INLINE void
-elf_swap_reloca_in (abfd, src, dst)
+void
+elf_swap_reloca_in (abfd, s, dst)
      bfd *abfd;
-     const Elf_External_Rela *src;
+     const bfd_byte *s;
      Elf_Internal_Rela *dst;
 {
-  dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
-  dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
-  dst->r_addend = get_signed_word (abfd, (bfd_byte *) src->r_addend);
+  const Elf_External_Rela *src = (const Elf_External_Rela *) s;
+  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
+  dst->r_info = H_GET_WORD (abfd, src->r_info);
+  dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
 }
 
-/* Translate an ELF reloc from internal format to external format. */
-INLINE void
-elf_swap_reloc_out (abfd, src, dst)
+/* Translate an ELF reloc from internal format to external format.  */
+void
+elf_swap_reloc_out (abfd, src, d)
      bfd *abfd;
-     const Elf_Internal_Rel *src;
-     Elf_External_Rel *dst;
+     const Elf_Internal_Rela *src;
+     bfd_byte *d;
 {
-  put_word (abfd, src->r_offset, dst->r_offset);
-  put_word (abfd, src->r_info, dst->r_info);
+  Elf_External_Rel *dst = (Elf_External_Rel *) d;
+  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
+  H_PUT_WORD (abfd, src->r_info, dst->r_info);
 }
 
-INLINE void
-elf_swap_reloca_out (abfd, src, dst)
+void
+elf_swap_reloca_out (abfd, src, d)
      bfd *abfd;
      const Elf_Internal_Rela *src;
-     Elf_External_Rela *dst;
+     bfd_byte *d;
 {
-  put_word (abfd, src->r_offset, dst->r_offset);
-  put_word (abfd, src->r_info, dst->r_info);
-  put_signed_word (abfd, src->r_addend, dst->r_addend);
+  Elf_External_Rela *dst = (Elf_External_Rela *) d;
+  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
+  H_PUT_WORD (abfd, src->r_info, dst->r_info);
+  H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
 }
 
 INLINE void
@@ -420,8 +477,8 @@ elf_swap_dyn_in (abfd, p, 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);
+  dst->d_tag = H_GET_WORD (abfd, src->d_tag);
+  dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
 }
 
 INLINE void
@@ -432,19 +489,18 @@ elf_swap_dyn_out (abfd, src, p)
 {
   Elf_External_Dyn *dst = (Elf_External_Dyn *) p;
 
-  put_word (abfd, src->d_tag, dst->d_tag);
-  put_word (abfd, src->d_un.d_val, dst->d_un.d_val);
+  H_PUT_WORD (abfd, src->d_tag, dst->d_tag);
+  H_PUT_WORD (abfd, src->d_un.d_val, dst->d_un.d_val);
 }
 \f
 /* ELF .o/exec file reading */
 
-
 /* Begin processing a given object.
 
    First we validate the file by reading in the ELF header and checking
    the magic number.  */
 
-static INLINE boolean
+static INLINE bfd_boolean
 elf_file_p (x_ehdrp)
      Elf_External_Ehdr *x_ehdrp;
 {
@@ -469,17 +525,21 @@ elf_object_p (abfd)
   Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
   Elf_Internal_Ehdr *i_ehdrp;  /* Elf file header, internal form */
   Elf_External_Shdr x_shdr;    /* Section header table entry, external form */
-  Elf_Internal_Shdr *i_shdrp = NULL; /* Section header table, internal form */
+  Elf_Internal_Shdr i_shdr;
+  Elf_Internal_Shdr *i_shdrp;  /* Section header table, internal form */
   unsigned int shindex;
   char *shstrtab;              /* Internal copy of section header stringtab */
   struct elf_backend_data *ebd;
-  struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd);
-  struct elf_obj_tdata *new_tdata = NULL;
+  struct bfd_preserve preserve;
   asection *s;
+  bfd_size_type amt;
+
+  preserve.marker = NULL;
 
   /* Read in the ELF header in external format.  */
 
-  if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
+  if (bfd_bread ((PTR) & x_ehdr, (bfd_size_type) sizeof (x_ehdr), abfd)
+      != sizeof (x_ehdr))
     {
       if (bfd_get_error () != bfd_error_system_call)
        goto got_wrong_format_error;
@@ -491,11 +551,11 @@ elf_object_p (abfd)
      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). */
+     file).  */
 
-  if ((elf_file_p (&x_ehdr) == false) ||
-      (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) ||
-      (x_ehdr.e_ident[EI_CLASS] != ELFCLASS))
+  if (! elf_file_p (&x_ehdr)
+      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
+      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
     goto got_wrong_format_error;
 
   /* Check that file's byte order matches xvec's */
@@ -517,11 +577,14 @@ elf_object_p (abfd)
   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
      the tdata pointer in the bfd.  */
 
-  new_tdata = ((struct elf_obj_tdata *)
-              bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)));
-  if (new_tdata == NULL)
+  amt = sizeof (struct elf_obj_tdata);
+  preserve.marker = bfd_zalloc (abfd, amt);
+  if (preserve.marker == NULL)
     goto got_no_match;
-  elf_tdata (abfd) = new_tdata;
+  if (!bfd_preserve_save (abfd, &preserve))
+    goto got_no_match;
+
+  elf_tdata (abfd) = preserve.marker;
 
   /* Now that we know the byte order, swap in the rest of the header */
   i_ehdrp = elf_elfheader (abfd);
@@ -534,14 +597,19 @@ elf_object_p (abfd)
   if (i_ehdrp->e_type == ET_CORE)
     goto got_wrong_format_error;
 
-  /* If there is no section header table, we're hosed. */
-  if (i_ehdrp->e_shoff == 0)
+  /* If this is a relocatable file and there is no section header
+     table, then we're hosed.  */
+  if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_type == ET_REL)
     goto got_wrong_format_error;
 
   /* 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 (i_ehdrp->e_shentsize != sizeof (x_shdr))
+     recorded in the file, but only if there are any sections.  */
+  if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
+    goto got_wrong_format_error;
+
+  /* Further sanity check.  */
+  if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_shnum != 0)
     goto got_wrong_format_error;
 
   ebd = get_elf_backend_data (abfd);
@@ -549,8 +617,10 @@ elf_object_p (abfd)
   /* Check that the ELF e_machine field matches what this particular
      BFD format expects.  */
   if (ebd->elf_machine_code != i_ehdrp->e_machine
-      && (ebd->elf_machine_alt1 == 0 || i_ehdrp->e_machine != ebd->elf_machine_alt1)
-      && (ebd->elf_machine_alt2 == 0 || i_ehdrp->e_machine != ebd->elf_machine_alt2))
+      && (ebd->elf_machine_alt1 == 0
+         || i_ehdrp->e_machine != ebd->elf_machine_alt1)
+      && (ebd->elf_machine_alt2 == 0
+         || i_ehdrp->e_machine != ebd->elf_machine_alt2))
     {
       const bfd_target * const *target_ptr;
 
@@ -594,38 +664,87 @@ elf_object_p (abfd)
        goto got_no_match;
     }
 
-  /* Remember the entry point specified in the ELF file header. */
-  bfd_get_start_address (abfd) = i_ehdrp->e_entry;
+  /* Remember the entry point specified in the ELF file header.  */
+  bfd_set_start_address (abfd, i_ehdrp->e_entry);
+
+  if (i_ehdrp->e_shoff != 0)
+    {
+      /* Seek to the section header table in the file.  */
+      if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0)
+       goto got_no_match;
+
+      /* Read the first section header at index 0, and convert to internal
+        form.  */
+      if (bfd_bread ((PTR) & x_shdr, (bfd_size_type) sizeof x_shdr, abfd)
+         != sizeof (x_shdr))
+       goto got_no_match;
+      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
+
+      /* If the section count is zero, the actual count is in the first
+        section header.  */
+      if (i_ehdrp->e_shnum == SHN_UNDEF)
+       i_ehdrp->e_shnum = i_shdr.sh_size;
+
+      /* And similarly for the string table index.  */
+      if (i_ehdrp->e_shstrndx == SHN_XINDEX)
+       i_ehdrp->e_shstrndx = i_shdr.sh_link;
+    }
 
   /* Allocate space for a copy of the section header table in
-     internal form, seek to the section header table in the file,
-     read it in, and convert it to internal form.  */
-  i_shdrp = ((Elf_Internal_Shdr *)
-            bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum));
-  elf_elfsections (abfd) = ((Elf_Internal_Shdr **)
-                           bfd_alloc (abfd,
-                                      sizeof (i_shdrp) * i_ehdrp->e_shnum));
-  if (!i_shdrp || !elf_elfsections (abfd))
-    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++)
+     internal form.  */
+  if (i_ehdrp->e_shnum != 0)
     {
-      if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr))
+      Elf_Internal_Shdr *shdrp;
+      unsigned int num_sec;
+
+      amt = sizeof (*i_shdrp) * i_ehdrp->e_shnum;
+      i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
+      if (!i_shdrp)
        goto got_no_match;
-      elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
-      elf_elfsections (abfd)[shindex] = i_shdrp + shindex;
-
-      /* If the section is loaded, but not page aligned, clear
-         D_PAGED.  */
-      if ((i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
-         && i_shdrp[shindex].sh_type != SHT_NOBITS
-         && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
-              % ebd->maxpagesize)
-             != 0))
-       abfd->flags &= ~D_PAGED;
+      num_sec = i_ehdrp->e_shnum;
+      if (num_sec > SHN_LORESERVE)
+       num_sec += SHN_HIRESERVE + 1 - SHN_LORESERVE;
+      elf_numsections (abfd) = num_sec;
+      amt = sizeof (i_shdrp) * num_sec;
+      elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
+      if (!elf_elfsections (abfd))
+       goto got_no_match;
+
+      memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
+      shdrp = i_shdrp;
+      shindex = 0;
+      if (num_sec > SHN_LORESERVE)
+       {
+         for ( ; shindex < SHN_LORESERVE; shindex++)
+           elf_elfsections (abfd)[shindex] = shdrp++;
+         for ( ; shindex < SHN_HIRESERVE + 1; shindex++)
+           elf_elfsections (abfd)[shindex] = i_shdrp;
+       }
+      for ( ; shindex < num_sec; shindex++)
+       elf_elfsections (abfd)[shindex] = shdrp++;
+
+      /* Read in the rest of the section header table and convert it
+        to internal form.  */
+      for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
+       {
+         if (bfd_bread ((PTR) & x_shdr, (bfd_size_type) sizeof x_shdr, abfd)
+             != sizeof (x_shdr))
+           goto got_no_match;
+         elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
+
+         /* If the section is loaded, but not page aligned, clear
+            D_PAGED.  */
+         if (i_shdrp[shindex].sh_size != 0
+             && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
+             && i_shdrp[shindex].sh_type != SHT_NOBITS
+             && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
+                  % ebd->maxpagesize)
+                 != 0))
+           abfd->flags &= ~D_PAGED;
+       }
     }
-  if (i_ehdrp->e_shstrndx)
+
+  if (i_ehdrp->e_shstrndx && i_ehdrp->e_shoff)
     {
       if (! bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx))
        goto got_no_match;
@@ -639,20 +758,18 @@ elf_object_p (abfd)
       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))));
+      amt = i_ehdrp->e_phnum * sizeof (Elf_Internal_Phdr);
+      elf_tdata (abfd)->phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
       if (elf_tdata (abfd)->phdr == NULL)
        goto got_no_match;
-      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
+      if (bfd_seek (abfd, (file_ptr) 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)
+         if (bfd_bread ((PTR) &x_phdr, (bfd_size_type) sizeof x_phdr, abfd)
              != sizeof x_phdr)
            goto got_no_match;
          elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
@@ -660,30 +777,37 @@ elf_object_p (abfd)
     }
 
   /* Read in the string table containing the names of the sections.  We
-     will need the base pointer to this table later. */
+     will need the base pointer to this table later.  */
   /* We read this inline now, so that we don't have to go through
      bfd_section_from_shdr with it (since this particular strtab is
      used to find all of the ELF section names.) */
 
-  shstrtab = bfd_elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
-  if (!shstrtab)
-    goto got_no_match;
-
-  /* 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.  */
-
-  for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
+  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff)
     {
-      if (! bfd_section_from_shdr (abfd, shindex))
+      unsigned int num_sec;
+
+      shstrtab = bfd_elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
+      if (!shstrtab)
        goto got_no_match;
+
+      /* 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.  */
+      num_sec = elf_numsections (abfd);
+      for (shindex = 1; shindex < num_sec; shindex++)
+       {
+         if (! bfd_section_from_shdr (abfd, shindex))
+           goto got_no_match;
+         if (shindex == SHN_LORESERVE - 1)
+           shindex += SHN_HIRESERVE + 1 - SHN_LORESERVE;
+       }
     }
 
   /* Let the backend double check the format and override global
      information.  */
   if (ebd->elf_backend_object_p)
     {
-      if ((*ebd->elf_backend_object_p) (abfd) == false)
+      if (! (*ebd->elf_backend_object_p) (abfd))
        goto got_wrong_format_error;
     }
 
@@ -706,40 +830,46 @@ elf_object_p (abfd)
        }
     }
 
-  return (abfd->xvec);
+  bfd_preserve_finish (abfd, &preserve);
+  return abfd->xvec;
 
  got_wrong_format_error:
+  /* There is way too much undoing of half-known state here.  The caller,
+     bfd_check_format_matches, really shouldn't iterate on live bfd's to
+     check match/no-match like it does.  We have to rely on that a call to
+     bfd_default_set_arch_mach with the previously known mach, undoes what
+     was done by the first bfd_default_set_arch_mach (with mach 0) here.
+     For this to work, only elf-data and the mach may be changed by the
+     target-specific elf_backend_object_p function.  Note that saving the
+     whole bfd here and restoring it would be even worse; the first thing
+     you notice is that the cached bfd file position gets out of sync.  */
   bfd_set_error (bfd_error_wrong_format);
+
  got_no_match:
-  if (new_tdata != NULL
-      && new_tdata->elf_sect_ptr != NULL)
-    bfd_release (abfd, new_tdata->elf_sect_ptr);
-  if (i_shdrp != NULL)
-    bfd_release (abfd, i_shdrp);
-  if (new_tdata != NULL)
-    bfd_release (abfd, new_tdata);
-  elf_tdata (abfd) = preserved_tdata;
-  return (NULL);
+  if (preserve.marker != NULL)
+    bfd_preserve_restore (abfd, &preserve);
+  return NULL;
 }
 \f
 /* ELF .o/exec file writing */
 
 /* Write out the relocs.  */
 
-static void
-write_relocs (abfd, sec, data)
+void
+elf_write_relocs (abfd, sec, data)
      bfd *abfd;
      asection *sec;
      PTR data;
 {
-  boolean *failedp = (boolean *) data;
+  bfd_boolean *failedp = (bfd_boolean *) data;
   Elf_Internal_Shdr *rela_hdr;
-  Elf_External_Rela *outbound_relocas;
-  Elf_External_Rel *outbound_relocs;
+  bfd_vma addr_offset;
+  void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
+  size_t extsize;
+  bfd_byte *dst_rela;
   unsigned int idx;
-  int use_rela_p;
-  asymbol *last_sym = 0;
-  int last_sym_idx = 0;
+  asymbol *last_sym;
+  int last_sym_idx;
 
   /* If we have already failed, don't do anything.  */
   if (*failedp)
@@ -761,128 +891,75 @@ write_relocs (abfd, sec, data)
   rela_hdr->contents = (PTR) bfd_alloc (abfd, rela_hdr->sh_size);
   if (rela_hdr->contents == NULL)
     {
-      *failedp = true;
+      *failedp = TRUE;
       return;
     }
 
   /* Figure out whether the relocations are RELA or REL relocations.  */
   if (rela_hdr->sh_type == SHT_RELA)
-    use_rela_p = true;
+    {
+      swap_out = elf_swap_reloca_out;
+      extsize = sizeof (Elf_External_Rela);
+    }
   else if (rela_hdr->sh_type == SHT_REL)
-    use_rela_p = false;
+    {
+      swap_out = elf_swap_reloc_out;
+      extsize = sizeof (Elf_External_Rel);
+    }
   else
     /* Every relocation section should be either an SHT_RELA or an
        SHT_REL section.  */
     abort ();
 
-  /* orelocation has the data, reloc_count has the count... */
-  if (use_rela_p)
-    {
-      outbound_relocas = (Elf_External_Rela *) rela_hdr->contents;
+  /* The address of an ELF reloc is section relative for an object
+     file, and absolute for an executable file or shared library.
+     The address of a BFD reloc is always section relative.  */
+  addr_offset = 0;
+  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
+    addr_offset = sec->vma;
 
-      for (idx = 0; idx < sec->reloc_count; idx++)
-       {
-         Elf_Internal_Rela dst_rela;
-         Elf_External_Rela *src_rela;
-         arelent *ptr;
-         asymbol *sym;
-         int n;
-
-         ptr = sec->orelocation[idx];
-         src_rela = outbound_relocas + idx;
-
-         /* The address of an ELF reloc is section relative for an object
-            file, and absolute for an executable file or shared library.
-            The address of a BFD reloc is always section relative.  */
-         if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
-           dst_rela.r_offset = ptr->address;
-         else
-           dst_rela.r_offset = ptr->address + sec->vma;
-
-         sym = *ptr->sym_ptr_ptr;
-         if (sym == last_sym)
-           n = last_sym_idx;
-         else if (bfd_is_abs_section (sym->section) && sym->value == 0)
-           n = STN_UNDEF;
-         else
-           {
-             last_sym = sym;
-             n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
-             if (n < 0)
-               {
-                 *failedp = true;
-                 return;
-               }
-             last_sym_idx = n;
-           }
-
-         if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
-             && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
-             && ! _bfd_elf_validate_reloc (abfd, ptr))
-           {
-             *failedp = true;
-             return;
-           }
+  /* orelocation has the data, reloc_count has the count...  */
+  last_sym = 0;
+  last_sym_idx = 0;
+  dst_rela = rela_hdr->contents;
 
-         dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
-
-         dst_rela.r_addend = ptr->addend;
-         elf_swap_reloca_out (abfd, &dst_rela, src_rela);
-       }
-    }
-  else
-    /* REL relocations */
+  for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
     {
-      outbound_relocs = (Elf_External_Rel *) rela_hdr->contents;
-
-      for (idx = 0; idx < sec->reloc_count; idx++)
+      Elf_Internal_Rela src_rela;
+      arelent *ptr;
+      asymbol *sym;
+      int n;
+
+      ptr = sec->orelocation[idx];
+      sym = *ptr->sym_ptr_ptr;
+      if (sym == last_sym)
+       n = last_sym_idx;
+      else if (bfd_is_abs_section (sym->section) && sym->value == 0)
+       n = STN_UNDEF;
+      else
        {
-         Elf_Internal_Rel dst_rel;
-         Elf_External_Rel *src_rel;
-         arelent *ptr;
-         int n;
-         asymbol *sym;
-
-         ptr = sec->orelocation[idx];
-         sym = *ptr->sym_ptr_ptr;
-         src_rel = outbound_relocs + idx;
-
-         /* The address of an ELF reloc is section relative for an object
-            file, and absolute for an executable file or shared library.
-            The address of a BFD reloc is always section relative.  */
-         if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
-           dst_rel.r_offset = ptr->address;
-         else
-           dst_rel.r_offset = ptr->address + sec->vma;
-
-         if (sym == last_sym)
-           n = last_sym_idx;
-         else if (bfd_is_abs_section (sym->section) && sym->value == 0)
-           n = STN_UNDEF;
-         else
-           {
-             last_sym = sym;
-             n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
-             if (n < 0)
-               {
-                 *failedp = true;
-                 return;
-               }
-             last_sym_idx = n;
-           }
-
-         if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
-             && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
-             && ! _bfd_elf_validate_reloc (abfd, ptr))
+         last_sym = sym;
+         n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
+         if (n < 0)
            {
-             *failedp = true;
+             *failedp = TRUE;
              return;
            }
+         last_sym_idx = n;
+       }
 
-         dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type);
-
-         elf_swap_reloc_out (abfd, &dst_rel, src_rel);
+      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
+         && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
+         && ! _bfd_elf_validate_reloc (abfd, ptr))
+       {
+         *failedp = TRUE;
+         return;
        }
+
+      src_rela.r_offset = ptr->address + addr_offset;
+      src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
+      src_rela.r_addend = ptr->addend;
+      (*swap_out) (abfd, &src_rela, dst_rela);
     }
 }
 
@@ -892,14 +969,14 @@ int
 elf_write_out_phdrs (abfd, phdr, count)
      bfd *abfd;
      const Elf_Internal_Phdr *phdr;
-     int count;
+     unsigned int count;
 {
   while (count--)
     {
       Elf_External_Phdr extphdr;
       elf_swap_phdr_out (abfd, phdr, &extphdr);
-      if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), 1, abfd)
-         != sizeof (Elf_External_Phdr))
+      if (bfd_bwrite (&extphdr, (bfd_size_type) sizeof (Elf_External_Phdr),
+                    abfd) != sizeof (Elf_External_Phdr))
        return -1;
       phdr++;
     }
@@ -908,7 +985,7 @@ elf_write_out_phdrs (abfd, phdr, count)
 
 /* Write out the section headers and the ELF file header.  */
 
-boolean
+bfd_boolean
 elf_write_shdrs_and_ehdr (abfd)
      bfd *abfd;
 {
@@ -917,58 +994,73 @@ elf_write_shdrs_and_ehdr (abfd)
   Elf_External_Shdr *x_shdrp;  /* Section header table, external form */
   Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
   unsigned int count;
+  bfd_size_type amt;
 
   i_ehdrp = elf_elfheader (abfd);
   i_shdrp = elf_elfsections (abfd);
 
-  /* swap the header before spitting it out... */
+  /* swap the header before spitting it out...  */
 
 #if DEBUG & 1
   elf_debug_file (i_ehdrp);
 #endif
   elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
+  amt = sizeof (x_ehdr);
   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
-      || (bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd)
-         != sizeof (x_ehdr)))
-    return false;
-
-  /* at this point we've concocted all the ELF sections... */
-  x_shdrp = (Elf_External_Shdr *)
-    bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
+      || bfd_bwrite ((PTR) & x_ehdr, amt, abfd) != amt)
+    return FALSE;
+
+  /* Some fields in the first section header handle overflow of ehdr
+     fields.  */
+  if (i_ehdrp->e_shnum >= SHN_LORESERVE)
+    i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
+  if (i_ehdrp->e_shstrndx >= SHN_LORESERVE)
+    i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx;
+
+  /* at this point we've concocted all the ELF sections...  */
+  amt = i_ehdrp->e_shnum;
+  amt *= sizeof (*x_shdrp);
+  x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
   if (!x_shdrp)
-    return false;
+    return FALSE;
 
-  for (count = 0; count < i_ehdrp->e_shnum; count++)
+  for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
     {
 #if DEBUG & 2
-      elf_debug_section (count, i_shdrp[count]);
+      elf_debug_section (count, *i_shdrp);
 #endif
-      elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count);
+      elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
+
+      if (count == SHN_LORESERVE - 1)
+       i_shdrp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
     }
   if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0
-      || (bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd)
-         != sizeof (*x_shdrp) * i_ehdrp->e_shnum))
-    return false;
+      || bfd_bwrite ((PTR) x_shdrp, amt, abfd) != amt)
+    return FALSE;
 
-  /* need to dump the string table too... */
+  /* need to dump the string table too...  */
 
-  return true;
+  return TRUE;
 }
 
 long
 elf_slurp_symbol_table (abfd, symptrs, dynamic)
      bfd *abfd;
      asymbol **symptrs;                /* Buffer for generated bfd symbols */
-     boolean dynamic;
+     bfd_boolean dynamic;
 {
   Elf_Internal_Shdr *hdr;
   Elf_Internal_Shdr *verhdr;
   unsigned long symcount;      /* Number of external ELF symbols */
   elf_symbol_type *sym;                /* Pointer to current bfd symbol */
   elf_symbol_type *symbase;    /* Buffer for generated bfd symbols */
-  Elf_Internal_Sym i_sym;
-  Elf_External_Sym *x_symp = NULL;
-  Elf_External_Versym *x_versymp = NULL;
+  Elf_Internal_Sym *isym;
+  Elf_Internal_Sym *isymend;
+  Elf_Internal_Sym *isymbuf = NULL;
+  Elf_External_Versym *xver;
+  Elf_External_Versym *xverbuf = NULL;
+  struct elf_backend_data *ebd;
+  bfd_size_type amt;
 
   /* Read each raw ELF symbol, converting from external ELF form to
      internal ELF form, and then using the information to create a
@@ -978,7 +1070,7 @@ elf_slurp_symbol_table (abfd, symptrs, dynamic)
      based on a one-to-one mapping of the ELF symbols to canonical
      symbols.  We actually use all the ELF symbols, so there will be no
      space left over at the end.  When we have all the symbols, we
-     build the caller's pointer vector. */
+     build the caller's pointer vector.  */
 
   if (! dynamic)
     {
@@ -1002,38 +1094,24 @@ elf_slurp_symbol_table (abfd, symptrs, dynamic)
        }
     }
 
-  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
-    return -1;
-
+  ebd = get_elf_backend_data (abfd);
   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
-
   if (symcount == 0)
     sym = symbase = NULL;
   else
     {
-      unsigned long i;
-
-      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
+      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
+                                     NULL, NULL, NULL);
+      if (isymbuf == NULL)
        return -1;
 
-      symbase = ((elf_symbol_type *)
-                bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type)));
+      amt = symcount;
+      amt *= sizeof (elf_symbol_type);
+      symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
       if (symbase == (elf_symbol_type *) NULL)
-       return -1;
-      sym = symbase;
-
-      /* Temporarily allocate room for the raw ELF symbols.  */
-      x_symp = ((Elf_External_Sym *)
-               bfd_malloc (symcount * sizeof (Elf_External_Sym)));
-      if (x_symp == NULL && symcount != 0)
-       goto error_return;
-
-      if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
-         != symcount * sizeof (Elf_External_Sym))
        goto error_return;
 
       /* Read the raw ELF version symbol information.  */
-
       if (verhdr != NULL
          && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
        {
@@ -1053,35 +1131,40 @@ elf_slurp_symbol_table (abfd, symptrs, dynamic)
          if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
            goto error_return;
 
-         x_versymp = (Elf_External_Versym *) bfd_malloc (verhdr->sh_size);
-         if (x_versymp == NULL && verhdr->sh_size != 0)
+         xverbuf = (Elf_External_Versym *) bfd_malloc (verhdr->sh_size);
+         if (xverbuf == NULL && verhdr->sh_size != 0)
            goto error_return;
 
-         if (bfd_read ((PTR) x_versymp, 1, verhdr->sh_size, abfd)
+         if (bfd_bread ((PTR) xverbuf, verhdr->sh_size, abfd)
              != verhdr->sh_size)
            goto error_return;
        }
 
       /* Skip first symbol, which is a null dummy.  */
-      for (i = 1; i < symcount; i++)
+      xver = xverbuf;
+      if (xver != NULL)
+       ++xver;
+      isymend = isymbuf + symcount;
+      for (isym = isymbuf + 1, sym = symbase; isym < isymend; isym++, sym++)
        {
-         elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
-         memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym));
-#ifdef ELF_KEEP_EXTSYM
-         memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym));
-#endif
+         memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
          sym->symbol.the_bfd = abfd;
 
          sym->symbol.name = bfd_elf_string_from_elf_section (abfd,
                                                              hdr->sh_link,
-                                                             i_sym.st_name);
+                                                             isym->st_name);
 
-         sym->symbol.value = i_sym.st_value;
+         sym->symbol.value = isym->st_value;
 
-         if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERVE)
+         if (isym->st_shndx == SHN_UNDEF)
+           {
+             sym->symbol.section = bfd_und_section_ptr;
+           }
+         else if (isym->st_shndx < SHN_LORESERVE
+                  || isym->st_shndx > SHN_HIRESERVE)
            {
              sym->symbol.section = section_from_elf_index (abfd,
-                                                           i_sym.st_shndx);
+                                                           isym->st_shndx);
              if (sym->symbol.section == NULL)
                {
                  /* This symbol is in a section for which we did not
@@ -1090,22 +1173,18 @@ elf_slurp_symbol_table (abfd, symptrs, dynamic)
                  sym->symbol.section = bfd_abs_section_ptr;
                }
            }
-         else if (i_sym.st_shndx == SHN_ABS)
+         else if (isym->st_shndx == SHN_ABS)
            {
              sym->symbol.section = bfd_abs_section_ptr;
            }
-         else if (i_sym.st_shndx == SHN_COMMON)
+         else if (isym->st_shndx == SHN_COMMON)
            {
              sym->symbol.section = bfd_com_section_ptr;
              /* Elf puts the alignment into the `value' field, and
                 the size into the `size' field.  BFD wants to see the
                 size in the value field, and doesn't care (at the
                 moment) about the alignment.  */
-             sym->symbol.value = i_sym.st_size;
-           }
-         else if (i_sym.st_shndx == SHN_UNDEF)
-           {
-             sym->symbol.section = bfd_und_section_ptr;
+             sym->symbol.value = isym->st_size;
            }
          else
            sym->symbol.section = bfd_abs_section_ptr;
@@ -1115,14 +1194,13 @@ elf_slurp_symbol_table (abfd, symptrs, dynamic)
          if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
            sym->symbol.value -= sym->symbol.section->vma;
 
-         switch (ELF_ST_BIND (i_sym.st_info))
+         switch (ELF_ST_BIND (isym->st_info))
            {
            case STB_LOCAL:
              sym->symbol.flags |= BSF_LOCAL;
              break;
            case STB_GLOBAL:
-             if (i_sym.st_shndx != SHN_UNDEF
-                 && i_sym.st_shndx != SHN_COMMON)
+             if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
                sym->symbol.flags |= BSF_GLOBAL;
              break;
            case STB_WEAK:
@@ -1130,7 +1208,7 @@ elf_slurp_symbol_table (abfd, symptrs, dynamic)
              break;
            }
 
-         switch (ELF_ST_TYPE (i_sym.st_info))
+         switch (ELF_ST_TYPE (isym->st_info))
            {
            case STT_SECTION:
              sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
@@ -1149,31 +1227,24 @@ elf_slurp_symbol_table (abfd, symptrs, dynamic)
          if (dynamic)
            sym->symbol.flags |= BSF_DYNAMIC;
 
-         if (x_versymp != NULL)
+         if (xver != NULL)
            {
              Elf_Internal_Versym iversym;
 
-             _bfd_elf_swap_versym_in (abfd, x_versymp + i, &iversym);
+             _bfd_elf_swap_versym_in (abfd, xver, &iversym);
              sym->version = iversym.vs_vers;
+             xver++;
            }
 
          /* Do some backend-specific processing on this symbol.  */
-         {
-           struct elf_backend_data *ebd = get_elf_backend_data (abfd);
-           if (ebd->elf_backend_symbol_processing)
-             (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
-         }
-
-         sym++;
+         if (ebd->elf_backend_symbol_processing)
+           (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
        }
     }
 
   /* Do some backend-specific processing on this symbol table.  */
-  {
-    struct elf_backend_data *ebd = get_elf_backend_data (abfd);
-    if (ebd->elf_backend_symbol_table_processing)
-      (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
-  }
+  if (ebd->elf_backend_symbol_table_processing)
+    (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
 
   /* We rely on the zalloc to clear out the final symbol entry.  */
 
@@ -1193,23 +1264,24 @@ elf_slurp_symbol_table (abfd, symptrs, dynamic)
       *symptrs = 0;            /* Final null pointer */
     }
 
-  if (x_versymp != NULL)
-    free (x_versymp);
-  if (x_symp != NULL)
-    free (x_symp);
+  if (xverbuf != NULL)
+    free (xverbuf);
+  if (isymbuf != NULL && hdr->contents != (unsigned char *) isymbuf)
+    free (isymbuf);
   return symcount;
+
 error_return:
-  if (x_versymp != NULL)
-    free (x_versymp);
-  if (x_symp != NULL)
-    free (x_symp);
+  if (xverbuf != NULL)
+    free (xverbuf);
+  if (isymbuf != NULL && hdr->contents != (unsigned char *) isymbuf)
+    free (isymbuf);
   return -1;
 }
 
-/* Read  relocations for ASECT from REL_HDR.  There are RELOC_COUNT of 
+/* Read  relocations for ASECT from REL_HDR.  There are RELOC_COUNT of
    them.  */
 
-static boolean
+static bfd_boolean
 elf_slurp_reloc_table_from_section (abfd, asect, rel_hdr, reloc_count,
                                    relents, symbols, dynamic)
      bfd *abfd;
@@ -1218,7 +1290,7 @@ elf_slurp_reloc_table_from_section (abfd, asect, rel_hdr, reloc_count,
      bfd_size_type reloc_count;
      arelent *relents;
      asymbol **symbols;
-     boolean dynamic;
+     bfd_boolean dynamic;
 {
   struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   PTR allocated = NULL;
@@ -1226,13 +1298,14 @@ elf_slurp_reloc_table_from_section (abfd, asect, rel_hdr, reloc_count,
   arelent *relent;
   unsigned int i;
   int entsize;
+  unsigned int symcount;
 
-  allocated = (PTR) bfd_malloc ((size_t) rel_hdr->sh_size);
+  allocated = (PTR) bfd_malloc (rel_hdr->sh_size);
   if (allocated == NULL)
     goto error_return;
 
   if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
-      || (bfd_read (allocated, 1, rel_hdr->sh_size, abfd)
+      || (bfd_bread (allocated, rel_hdr->sh_size, abfd)
          != rel_hdr->sh_size))
     goto error_return;
 
@@ -1242,22 +1315,21 @@ elf_slurp_reloc_table_from_section (abfd, asect, rel_hdr, reloc_count,
   BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
              || entsize == sizeof (Elf_External_Rela));
 
+  if (dynamic)
+    symcount = bfd_get_dynamic_symcount (abfd);
+  else
+    symcount = bfd_get_symcount (abfd);
+
   for (i = 0, relent = relents;
        i < reloc_count;
        i++, relent++, native_relocs += entsize)
     {
       Elf_Internal_Rela rela;
-      Elf_Internal_Rel rel;
 
       if (entsize == sizeof (Elf_External_Rela))
-       elf_swap_reloca_in (abfd, (Elf_External_Rela *) native_relocs, &rela);
+       elf_swap_reloca_in (abfd, native_relocs, &rela);
       else
-       {
-         elf_swap_reloc_in (abfd, (Elf_External_Rel *) native_relocs, &rel);
-         rela.r_offset = rel.r_offset;
-         rela.r_info = rel.r_info;
-         rela.r_addend = 0;
-       }
+       elf_swap_reloc_in (abfd, native_relocs, &rela);
 
       /* The address of an ELF reloc is section relative for an object
         file, and absolute for an executable file or shared library.
@@ -1270,6 +1342,13 @@ elf_slurp_reloc_table_from_section (abfd, asect, rel_hdr, reloc_count,
 
       if (ELF_R_SYM (rela.r_info) == 0)
        relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
+      else if (ELF_R_SYM (rela.r_info) > symcount)
+       {
+         (*_bfd_error_handler)
+           (_("%s(%s): relocation %d has invalid symbol index %ld"),
+            abfd->filename, asect->name, i, ELF_R_SYM (rela.r_info));
+         relent->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
+       }
       else
        {
          asymbol **ps, *s;
@@ -1289,28 +1368,28 @@ elf_slurp_reloc_table_from_section (abfd, asect, rel_hdr, reloc_count,
       if (entsize == sizeof (Elf_External_Rela))
        (*ebd->elf_info_to_howto) (abfd, relent, &rela);
       else
-       (*ebd->elf_info_to_howto_rel) (abfd, relent, &rel);
+       (*ebd->elf_info_to_howto_rel) (abfd, relent, &rela);
     }
 
   if (allocated != NULL)
     free (allocated);
 
-  return true;
+  return TRUE;
 
  error_return:
   if (allocated != NULL)
     free (allocated);
-  return false;
+  return FALSE;
 }
 
 /* Read in and swap the external relocs.  */
 
-static boolean
+bfd_boolean
 elf_slurp_reloc_table (abfd, asect, symbols, dynamic)
      bfd *abfd;
      asection *asect;
      asymbol **symbols;
-     boolean dynamic;
+     bfd_boolean dynamic;
 {
   struct bfd_elf_section_data * const d = elf_section_data (asect);
   Elf_Internal_Shdr *rel_hdr;
@@ -1318,22 +1397,21 @@ elf_slurp_reloc_table (abfd, asect, symbols, dynamic)
   bfd_size_type reloc_count;
   bfd_size_type reloc_count2;
   arelent *relents;
+  bfd_size_type amt;
 
   if (asect->relocation != NULL)
-    return true;
+    return TRUE;
 
   if (! dynamic)
     {
       if ((asect->flags & SEC_RELOC) == 0
          || asect->reloc_count == 0)
-       return true;
+       return TRUE;
 
       rel_hdr = &d->rel_hdr;
-      reloc_count = rel_hdr->sh_size / rel_hdr->sh_entsize;
+      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
       rel_hdr2 = d->rel_hdr2;
-      reloc_count2 = (rel_hdr2 
-                     ? (rel_hdr2->sh_size / rel_hdr2->sh_entsize)
-                     : 0);
+      reloc_count2 = (rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0);
 
       BFD_ASSERT (asect->reloc_count == reloc_count + reloc_count2);
       BFD_ASSERT (asect->rel_filepos == rel_hdr->sh_offset
@@ -1347,36 +1425,34 @@ elf_slurp_reloc_table (abfd, asect, symbols, dynamic)
         dynamic symbol table, and in that case bfd_section_from_shdr
         in elf.c does not update the RELOC_COUNT.  */
       if (asect->_raw_size == 0)
-       return true;
+       return TRUE;
 
       rel_hdr = &d->this_hdr;
-      reloc_count = rel_hdr->sh_size / rel_hdr->sh_entsize;
+      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
       rel_hdr2 = NULL;
       reloc_count2 = 0;
     }
 
-  relents = ((arelent *) 
-            bfd_alloc (abfd, 
-                       (reloc_count + reloc_count2) * sizeof (arelent)));
+  amt = (reloc_count + reloc_count2) * sizeof (arelent);
+  relents = (arelent *) bfd_alloc (abfd, amt);
   if (relents == NULL)
-    return false;
+    return FALSE;
 
   if (!elf_slurp_reloc_table_from_section (abfd, asect,
                                           rel_hdr, reloc_count,
                                           relents,
                                           symbols, dynamic))
-    return false;
-  
-  if (rel_hdr2 
+    return FALSE;
+
+  if (rel_hdr2
       && !elf_slurp_reloc_table_from_section (abfd, asect,
                                              rel_hdr2, reloc_count2,
                                              relents + reloc_count,
                                              symbols, dynamic))
-    return false;
+    return FALSE;
 
-  
   asect->relocation = relents;
-  return true;
+  return TRUE;
 }
 
 #ifdef DEBUG
@@ -1507,20 +1583,21 @@ const struct elf_size_info NAME(_bfd_elf,size_info) = {
   sizeof (Elf_External_Sym),
   sizeof (Elf_External_Dyn),
   sizeof (Elf_External_Note),
-  ARCH_SIZE / 8,
+  4,
   1,
   ARCH_SIZE, FILE_ALIGN,
   ELFCLASS, EV_CURRENT,
   elf_write_out_phdrs,
   elf_write_shdrs_and_ehdr,
-  write_relocs,
+  elf_write_relocs,
+  elf_swap_symbol_in,
   elf_swap_symbol_out,
   elf_slurp_reloc_table,
   elf_slurp_symbol_table,
   elf_swap_dyn_in,
   elf_swap_dyn_out,
-  NULL,
-  NULL,
-  NULL,
-  NULL
+  elf_swap_reloc_in,
+  elf_swap_reloc_out,
+  elf_swap_reloca_in,
+  elf_swap_reloca_out
 };
This page took 0.050168 seconds and 4 git commands to generate.