* libxcoff.h (struct xcoff_backend_data_rec): Constify src param
[binutils-gdb.git] / bfd / nlm32-sparc.c
index ff0571be203b441435863990d21d0ce52310f152..154a96282da6adce78c132a304c729feecace187 100644 (file)
@@ -1,5 +1,5 @@
 /* Support for 32-bit SPARC NLM (NetWare Loadable Module)
-   Copyright (C) 1993 Free Software Foundation, Inc.
+   Copyright 1993, 1994, 2000 Free Software Foundation, Inc.
 
 This file is part of BFD, the Binary File Descriptor library.
 
@@ -15,7 +15,7 @@ GNU General Public License for more details.
 
 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.  */
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include "bfd.h"
 #include "sysdep.h"
@@ -44,8 +44,8 @@ static boolean nlm_sparc_write_external
 enum reloc_type
   {
     R_SPARC_NONE = 0,
-    R_SPARC_8,         R_SPARC_16,             R_SPARC_32, 
-    R_SPARC_DISP8,     R_SPARC_DISP16,         R_SPARC_DISP32, 
+    R_SPARC_8,         R_SPARC_16,             R_SPARC_32,
+    R_SPARC_DISP8,     R_SPARC_DISP16,         R_SPARC_DISP32,
     R_SPARC_WDISP30,   R_SPARC_WDISP22,
     R_SPARC_HI22,      R_SPARC_22,
     R_SPARC_13,                R_SPARC_LO10,
@@ -78,7 +78,7 @@ static CONST char *CONST reloc_type_names[] =
 };
 #endif
 
-static reloc_howto_type nlm32_sparc_howto_table[] = 
+static reloc_howto_type nlm32_sparc_howto_table[] =
 {
   HOWTO(R_SPARC_NONE,    0,0, 0,false,0,complain_overflow_dont,    0,"R_SPARC_NONE",    false,0,0x00000000,true),
   HOWTO(R_SPARC_8,       0,0, 8,false,0,complain_overflow_bitfield,0,"R_SPARC_8",       false,0,0x000000ff,true),
@@ -118,23 +118,23 @@ struct nlm32_sparc_reloc_ext {
 static boolean
 nlm_sparc_read_reloc (abfd, sym, secp, rel)
      bfd *abfd;
-     nlmNAME(symbol_type) *sym;
+     nlmNAME(symbol_type) *sym ATTRIBUTE_UNUSED;
      asection **secp;
      arelent *rel;
 {
-  bfd_byte temp[4];
   bfd_vma val, addend;
-  const char *name;
-  int index;
+  unsigned int index;
   unsigned int type;
   struct nlm32_sparc_reloc_ext tmp_reloc;
+  asection *code_sec, *data_sec;
 
-  if (bfd_read (&tmp_reloc, 12, 1, abfd) != 12) {
-    bfd_error = system_call_error;
+  if (bfd_read (&tmp_reloc, 12, 1, abfd) != 12)
     return false;
-  }
 
-  *secp = bfd_get_section_by_name (abfd, NLM_CODE_NAME);
+  code_sec = bfd_get_section_by_name (abfd, NLM_CODE_NAME);
+  data_sec = bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
+
+  *secp = code_sec;
 
   val = bfd_get_32 (abfd, tmp_reloc.offset);
   addend = bfd_get_32 (abfd, tmp_reloc.addend);
@@ -145,7 +145,7 @@ nlm_sparc_read_reloc (abfd, sym, secp, rel)
   rel->howto = NULL;
 
   for (index = 0;
-       index < sizeof(nlm32_sparc_howto_table) / sizeof(reloc_howto_type);
+       index < sizeof (nlm32_sparc_howto_table) / sizeof (reloc_howto_type);
        index++)
     if (nlm32_sparc_howto_table[index].type == type) {
       rel->howto = &nlm32_sparc_howto_table[index];
@@ -154,7 +154,7 @@ nlm_sparc_read_reloc (abfd, sym, secp, rel)
 
 #ifdef DEBUG
   fprintf (stderr, "%s:  address = %08lx, addend = %08lx, type = %d, howto = %08lx\n",
-          __FILE__, rel->address, rel->addend, type, rel->howto);
+          __FUNCTION__, rel->address, rel->addend, type, rel->howto);
 #endif
   return true;
 
@@ -169,14 +169,13 @@ nlm_sparc_write_reloc (abfd, sec, rel)
      arelent *rel;
 {
   bfd_vma val;
-  struct nlm32_sparc_reloc_ext tmp_reloc = {0};
-  int index;
+  struct nlm32_sparc_reloc_ext tmp_reloc;
+  unsigned int index;
   int type = -1;
   reloc_howto_type *tmp;
 
-  
   for (index = 0;
-       index < sizeof (nlm32_sparc_howto_table) / sizeof(reloc_howto_type);
+       index < sizeof (nlm32_sparc_howto_table) / sizeof (reloc_howto_type);
        index++) {
     tmp = &nlm32_sparc_howto_table[index];
 
@@ -192,7 +191,7 @@ nlm_sparc_write_reloc (abfd, sec, rel)
     }
   }
   if (type == -1)
-    abort();
+    abort ();
 
   /*
    * Netware wants a list of relocs for each address.
@@ -207,20 +206,22 @@ nlm_sparc_write_reloc (abfd, sec, rel)
      segment.  This offset is the section vma, adjusted by the vma of
      the lowest section in that segment, plus the address of the
      relocation.  */
+#if 0
+  val = bfd_get_section_vma (abfd, (*rel->sym_ptr_ptr)->section) + rel->address;
+#else
   val = bfd_get_section_vma (abfd, sec) + rel->address;
+#endif
 
 #ifdef DEBUG
   fprintf (stderr, "%s:  val = %08lx, addend = %08lx, type = %d\n",
-          __FILE__, val, rel->addend, rel->howto->type);
+          __FUNCTION__, val, rel->addend, rel->howto->type);
 #endif
   bfd_put_32 (abfd, val, tmp_reloc.offset);
   bfd_put_32 (abfd, rel->addend, tmp_reloc.addend);
-  bfd_put_8 (abfd, (short)(rel->howto->type), tmp_reloc.type);
+  bfd_put_8 (abfd, (short) (rel->howto->type), tmp_reloc.type);
 
   if (bfd_write (&tmp_reloc, 12, 1, abfd) != 12)
-    {
-      abort();
-    }
+    return false;
 
   return true;
 }
@@ -230,11 +231,11 @@ nlm_sparc_write_reloc (abfd, sec, rel)
 
 static boolean
 nlm_sparc_mangle_relocs (abfd, sec, data, offset, count)
-     bfd *abfd;
-     asection *sec;
-     PTR data;
-     bfd_vma offset;
-     bfd_size_type count;
+     bfd *abfd ATTRIBUTE_UNUSED;
+     asection *sec ATTRIBUTE_UNUSED;
+     PTR data ATTRIBUTE_UNUSED;
+     bfd_vma offset ATTRIBUTE_UNUSED;
+     bfd_size_type count ATTRIBUTE_UNUSED;
 {
   return true;
 }
@@ -249,59 +250,55 @@ nlm_sparc_read_import (abfd, sym)
   bfd_size_type rcount;                        /* number of relocs */
   bfd_byte temp[NLM_TARGET_LONG_SIZE]; /* temporary 32-bit value */
   unsigned char symlength;             /* length of symbol name */
-  
+  char *name;
+
   /*
    * First, read in the number of relocation
    * entries for this symbol
    */
-  if (bfd_read ((PTR) temp, 4, 1, abfd)
-      != 4)
-    {
-      bfd_error = system_call_error;
-      return (false);
-    }
-  
+  if (bfd_read ((PTR) temp, 4, 1, abfd) != 4)
+    return false;
+
   rcount = bfd_get_32 (abfd, temp);
-  
+
   /*
    * Next, read in the length of the symbol
    */
-  
+
   if (bfd_read ((PTR) &symlength, sizeof (symlength), 1, abfd)
       != sizeof (symlength))
-    {
-      bfd_error = system_call_error;
-      return (false);
-    }
+    return false;
   sym -> symbol.the_bfd = abfd;
-  sym -> symbol.name = bfd_alloc (abfd, symlength + 1);
-  
+  name = bfd_alloc (abfd, symlength + 1);
+  if (name == NULL)
+    return false;
+
   /*
    * Then read in the symbol
    */
-  
-  if (bfd_read ((PTR) sym -> symbol.name, symlength, 1, abfd)
-      != symlength)
-    {
-      bfd_error = system_call_error;
-      return (false);
-    }
+
+  if (bfd_read (name, symlength, 1, abfd) != symlength)
+    return false;
+  name[symlength] = '\0';
+  sym -> symbol.name = name;
   sym -> symbol.flags = 0;
   sym -> symbol.value = 0;
-  sym -> symbol.section = &bfd_und_section;
-  
+  sym -> symbol.section = bfd_und_section_ptr;
+
   /*
    * Next, start reading in the relocs.
    */
-  
+
   nlm_relocs = ((struct nlm_relent *)
                bfd_alloc (abfd, rcount * sizeof (struct nlm_relent)));
+  if (!nlm_relocs)
+    return false;
   sym -> relocs = nlm_relocs;
   sym -> rcnt = 0;
   while (sym -> rcnt < rcount)
     {
       asection *section;
-      
+
       if (nlm_sparc_read_reloc (abfd, sym, &section,
                              &nlm_relocs -> reloc)
          == false)
@@ -320,11 +317,33 @@ nlm_sparc_write_import (abfd, sec, rel)
      arelent *rel;
 {
   char temp[4];
+  asection *code, *data, *bss, *symsec;
+  bfd_vma base;
+
+  code = bfd_get_section_by_name (abfd, NLM_CODE_NAME);
+  data = bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
+  bss = bfd_get_section_by_name (abfd, NLM_UNINITIALIZED_DATA_NAME);
+  symsec = (*rel->sym_ptr_ptr)->section;
+
+  if (symsec == code) {
+    base = 0;
+  } else if (symsec == data) {
+    base = bfd_section_size (abfd, code);
+  } else if (symsec == bss) {
+    base = bfd_section_size (abfd, code) + bfd_section_size (abfd, data);
+  } else
+    base = 0;
 
-  bfd_put_32 (abfd, (*rel->sym_ptr_ptr)->value, temp);
-  bfd_write ((PTR)temp, 4, 1, abfd);
+#ifdef DEBUG
+  fprintf (stderr, "%s:  <%x, 1>\n\t",
+          __FUNCTION__, base + (*rel->sym_ptr_ptr)->value);
+#endif
+  bfd_put_32 (abfd, base + (*rel->sym_ptr_ptr)->value, temp);
+  if (bfd_write ((PTR)temp, 4, 1, abfd) != 4)
+    return false;
   bfd_put_32 (abfd, 1, temp);
-  bfd_write ((PTR)temp, 4, 1, abfd);
+  if (bfd_write ((PTR)temp, 4, 1, abfd) != 4)
+    return false;
   if (nlm_sparc_write_reloc (abfd, sec, rel) == false)
     return false;
   return true;
@@ -339,24 +358,18 @@ nlm_sparc_write_external (abfd, count, sym, relocs)
      asymbol *sym;
      struct reloc_and_sec *relocs;
 {
-  int i;
+  unsigned int i;
   bfd_byte len;
   unsigned char temp[NLM_TARGET_LONG_SIZE];
 
   bfd_put_32 (abfd, count, temp);
-  if (bfd_write (temp, sizeof(temp), 1, abfd) != sizeof (temp))
-    {
-      bfd_error = system_call_error;
-      return false;
-    }
+  if (bfd_write (temp, sizeof (temp), 1, abfd) != sizeof (temp))
+    return false;
 
   len = strlen (sym->name);
-  if ((bfd_write (&len, sizeof (bfd_byte), 1, abfd) != sizeof(bfd_byte))
+  if ((bfd_write (&len, sizeof (bfd_byte), 1, abfd) != sizeof (bfd_byte))
       || bfd_write (sym->name, len, 1, abfd) != len)
-    {
-      bfd_error = system_call_error;
-      return false;
-    }
+    return false;
 
   for (i = 0; i < count; i++)
     {
@@ -368,6 +381,30 @@ nlm_sparc_write_external (abfd, count, sym, relocs)
   return true;
 }
 
+static boolean
+nlm_sparc_write_export (abfd, sym, value)
+     bfd *abfd;
+     asymbol *sym;
+     bfd_vma value;
+{
+  bfd_byte len;
+  bfd_byte temp[4];
+
+#ifdef DEBUG
+  fprintf (stderr, "%s: <%x, %d, %s>\n",
+          __FUNCTION__, value, strlen (sym->name), sym->name);
+#endif
+  bfd_put_32 (abfd, value, temp);
+  len = strlen (sym->name);
+
+  if (bfd_write (temp, 4, 1, abfd) != 4
+      || bfd_write (&len, 1, 1, abfd) != 1
+      || bfd_write (sym->name, len, 1, abfd) != len)
+    return false;
+
+  return true;
+}
+
 #undef nlm_swap_fixed_header_in
 #undef nlm_swap_fixed_header_out
 
@@ -380,6 +417,7 @@ static const struct nlm_backend_data nlm32_sparc_backend =
   0,   /* optional_prefix_size */
   bfd_arch_sparc,
   0,
+  false,
   0,   /* backend_object_p */
   0,   /* write_prefix_func */
   nlm_sparc_read_reloc,
@@ -391,6 +429,7 @@ static const struct nlm_backend_data nlm32_sparc_backend =
   nlm_swap_fixed_header_in,
   nlm_swap_fixed_header_out,
   nlm_sparc_write_external,
+  nlm_sparc_write_export
 };
 
 #define TARGET_BIG_NAME                "nlm32-sparc"