2003-04-08 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gprof / gmon_io.c
index 0bc5caa778c7b6a8f705bf9efb0590c43a2abd9e..6188631f101de2f7a1902ef2a0e519827cfe4c0a 100644 (file)
 #include "hist.h"
 #include "libiberty.h"
 
+enum gmon_ptr_size {
+  ptr_32bit,
+  ptr_64bit
+};
+
+enum gmon_ptr_signedness {
+  ptr_signed,
+  ptr_unsigned
+};
+
+static enum gmon_ptr_size gmon_get_ptr_size PARAMS ((void));
+static enum gmon_ptr_signedness gmon_get_ptr_signedness PARAMS ((void));
+
+#ifdef BFD_HOST_U_64_BIT
+static int gmon_io_read_64 PARAMS ((FILE *, BFD_HOST_U_64_BIT *));
+static int gmon_io_write_64 PARAMS ((FILE *, BFD_HOST_U_64_BIT));
+#endif
+static int gmon_read_raw_arc
+  PARAMS ((FILE *, bfd_vma *, bfd_vma *, unsigned long *));
+static int gmon_write_raw_arc
+  PARAMS ((FILE *, bfd_vma, bfd_vma, unsigned long));
+
 int gmon_input = 0;
 int gmon_file_version = 0;     /* 0 == old (non-versioned) file format.  */
 
+static enum gmon_ptr_size
+gmon_get_ptr_size ()
+{
+  int size;
+
+  /* Pick best size for pointers.  Start with the ELF size, and if not
+     elf go with the architecture's address size.  */
+  size = bfd_get_arch_size (core_bfd);
+  if (size == -1)
+    size = bfd_arch_bits_per_address (core_bfd);
+
+  switch (size)
+    {
+    case 32:
+      return ptr_32bit;
+
+    case 64:
+      return ptr_64bit;
+
+    default:
+      fprintf (stderr, _("%s: address size has unexpected value of %u\n"),
+              whoami, size);
+      done (1);
+    }
+}
+
+static enum gmon_ptr_signedness
+gmon_get_ptr_signedness ()
+{
+  int sext;
+
+  /* Figure out whether to sign extend.  If BFD doesn't know, assume no.  */
+  sext = bfd_get_sign_extend_vma (core_bfd);
+  if (sext == -1)
+    return ptr_unsigned;
+  return (sext ? ptr_signed : ptr_unsigned);
+}
+
 int
-DEFUN (gmon_io_read_32, (ifp, valp), FILE * ifp AND unsigned int *valp)
+gmon_io_read_32 (ifp, valp)
+     FILE *ifp;
+     unsigned int *valp;
 {
   char buf[4];
 
@@ -48,8 +110,11 @@ DEFUN (gmon_io_read_32, (ifp, valp), FILE * ifp AND unsigned int *valp)
   return 0;
 }
 
-int
-DEFUN (gmon_io_read_64, (ifp, valp), FILE * ifp AND BFD_HOST_U_64_BIT *valp)
+#ifdef BFD_HOST_U_64_BIT
+static int
+gmon_io_read_64 (ifp, valp)
+     FILE *ifp;
+     BFD_HOST_U_64_BIT *valp;
 {
   char buf[8];
 
@@ -58,37 +123,50 @@ DEFUN (gmon_io_read_64, (ifp, valp), FILE * ifp AND BFD_HOST_U_64_BIT *valp)
   *valp = bfd_get_64 (core_bfd, buf);
   return 0;
 }
+#endif
 
 int
-DEFUN (gmon_io_read_vma, (ifp, valp), FILE * ifp AND bfd_vma *valp)
+gmon_io_read_vma (ifp, valp)
+     FILE *ifp;
+     bfd_vma *valp;
 {
   unsigned int val32;
+#ifdef BFD_HOST_U_64_BIT
   BFD_HOST_U_64_BIT val64;
+#endif
 
-  switch (bfd_arch_bits_per_address (core_bfd))
+  switch (gmon_get_ptr_size ())
     {
-    case 32:
+    case ptr_32bit:
       if (gmon_io_read_32 (ifp, &val32))
        return 1;
-      *valp = val32;
+      if (gmon_get_ptr_signedness () == ptr_signed)
+        *valp = (int) val32;
+      else
+        *valp = val32;
       break;
 
-    case 64:
+#ifdef BFD_HOST_U_64_BIT
+    case ptr_64bit:
       if (gmon_io_read_64 (ifp, &val64))
        return 1;
-      *valp = val64;
+#ifdef BFD_HOST_64_BIT
+      if (gmon_get_ptr_signedness () == ptr_signed)
+        *valp = (BFD_HOST_64_BIT) val64;
+      else
+#endif
+        *valp = val64;
       break;
-
-    default:
-      fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
-              whoami, bfd_arch_bits_per_address (core_bfd));
-      done (1);
+#endif
     }
   return 0;
 }
 
 int
-DEFUN (gmon_io_read, (ifp, buf, n), FILE * ifp AND char *buf AND size_t n)
+gmon_io_read (ifp, buf, n)
+     FILE *ifp;
+     char *buf;
+     size_t n;
 {
   if (fread (buf, 1, n, ifp) != n)
     return 1;
@@ -96,53 +174,60 @@ DEFUN (gmon_io_read, (ifp, buf, n), FILE * ifp AND char *buf AND size_t n)
 }
 
 int
-DEFUN (gmon_io_write_32, (ofp, val), FILE * ofp AND unsigned int val)
+gmon_io_write_32 (ofp, val)
+     FILE *ofp;
+     unsigned int val;
 {
   char buf[4];
 
-  bfd_put_32 (core_bfd, val, buf);
+  bfd_put_32 (core_bfd, (bfd_vma) val, buf);
   if (fwrite (buf, 1, 4, ofp) != 4)
     return 1;
   return 0;
 }
 
-int
-DEFUN (gmon_io_write_64, (ofp, val), FILE * ofp AND BFD_HOST_U_64_BIT val)
+#ifdef BFD_HOST_U_64_BIT
+static int
+gmon_io_write_64 (ofp, val)
+     FILE *ofp;        
+     BFD_HOST_U_64_BIT val;
 {
   char buf[8];
 
-  bfd_put_64 (core_bfd, val, buf);
+  bfd_put_64 (core_bfd, (bfd_vma) val, buf);
   if (fwrite (buf, 1, 8, ofp) != 8)
     return 1;
   return 0;
 }
+#endif
 
 int
-DEFUN (gmon_io_write_vma, (ofp, val), FILE * ofp AND bfd_vma val)
+gmon_io_write_vma (ofp, val)
+     FILE *ofp;
+     bfd_vma val;
 {
 
-  switch (bfd_arch_bits_per_address (core_bfd))
+  switch (gmon_get_ptr_size ())
     {
-    case 32:
+    case ptr_32bit:
       if (gmon_io_write_32 (ofp, (unsigned int) val))
        return 1;
       break;
 
-    case 64:
+#ifdef BFD_HOST_U_64_BIT
+    case ptr_64bit:
       if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) val))
        return 1;
       break;
-
-    default:
-      fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
-              whoami, bfd_arch_bits_per_address (core_bfd));
-      done (1);
+#endif
     }
   return 0;
 }
 
 int
-DEFUN (gmon_io_write_8, (ofp, val), FILE * ofp AND unsigned char val)
+gmon_io_write_8 (ofp, val)
+     FILE *ofp;        
+     unsigned int val;
 {
   char buf[1];
 
@@ -153,75 +238,83 @@ DEFUN (gmon_io_write_8, (ofp, val), FILE * ofp AND unsigned char val)
 }
 
 int
-DEFUN (gmon_io_write, (ofp, buf, n), FILE * ofp AND char *buf AND size_t n)
+gmon_io_write (ofp, buf, n)
+     FILE *ofp;        
+     char *buf;
+     size_t n;
 {
   if (fwrite (buf, 1, n, ofp) != n)
     return 1;
   return 0;
 }
 
-int
-DEFUN (gmon_read_raw_arc, (ifp, fpc, spc, cnt), FILE * ifp AND bfd_vma * fpc AND bfd_vma * spc AND unsigned long * cnt)
+static int
+gmon_read_raw_arc (ifp, fpc, spc, cnt)
+     FILE *ifp;
+     bfd_vma *fpc;
+     bfd_vma *spc;
+     unsigned long *cnt;
 {
+#ifdef BFD_HOST_U_64_BIT
   BFD_HOST_U_64_BIT cnt64;
+#endif
   unsigned int cnt32;
 
   if (gmon_io_read_vma (ifp, fpc)
       || gmon_io_read_vma (ifp, spc))
     return 1;
 
-  switch (bfd_arch_bits_per_address (core_bfd))
+  switch (gmon_get_ptr_size ())
     {
-    case 32:
+    case ptr_32bit:
       if (gmon_io_read_32 (ifp, &cnt32))
        return 1;
       *cnt = cnt32;
       break;
 
-    case 64:
+#ifdef BFD_HOST_U_64_BIT
+    case ptr_64bit:
       if (gmon_io_read_64 (ifp, &cnt64))
        return 1;
       *cnt = cnt64;
       break;
-
-    default:
-      fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
-              whoami, bfd_arch_bits_per_address (core_bfd));
-      done (1);
+#endif
     }
   return 0;
 }
 
-int
-DEFUN (gmon_write_raw_arc, (ofp, fpc, spc, cnt), FILE * ofp AND bfd_vma fpc AND bfd_vma spc AND unsigned long cnt)
+static int
+gmon_write_raw_arc (ofp, fpc, spc, cnt)
+     FILE *ofp;
+     bfd_vma fpc;
+     bfd_vma spc;
+     unsigned long cnt;
 {
 
   if (gmon_io_write_vma (ofp, fpc)
       || gmon_io_write_vma (ofp, spc))
     return 1;
 
-  switch (bfd_arch_bits_per_address (core_bfd))
+  switch (gmon_get_ptr_size ())
     {
-    case 32:
+    case ptr_32bit:
       if (gmon_io_write_32 (ofp, (unsigned int) cnt))
        return 1;
       break;
 
-    case 64:
+#ifdef BFD_HOST_U_64_BIT
+    case ptr_64bit:
       if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) cnt))
        return 1;
       break;
-
-    default:
-      fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
-              whoami, bfd_arch_bits_per_address (core_bfd));
-      done (1);
+#endif
     }
   return 0;
 }
 
 void
-DEFUN (gmon_out_read, (filename), const char *filename)
+gmon_out_read (filename)
+     const char *filename;
 {
   FILE *ifp;
   struct gmon_hdr ghdr;
@@ -371,21 +464,15 @@ DEFUN (gmon_out_read, (filename), const char *filename)
              done (1);
            }
 
-         switch (bfd_arch_bits_per_address (core_bfd))
+         switch (gmon_get_ptr_size ())
            {
-           case 32:
+           case ptr_32bit:
              header_size = GMON_HDRSIZE_BSD44_32;
              break;
 
-           case 64:
+           case ptr_64bit:
              header_size = GMON_HDRSIZE_BSD44_64;
              break;
-
-           default:
-              fprintf (stderr,
-                       _("%s: bits per address has unexpected value of %u\n"),
-                      whoami, bfd_arch_bits_per_address (core_bfd));
-              done (1);
            }
        }
       else
@@ -398,21 +485,15 @@ DEFUN (gmon_out_read, (filename), const char *filename)
              done (1);
            }
 
-         switch (bfd_arch_bits_per_address (core_bfd))
+         switch (gmon_get_ptr_size ())
            {
-           case 32:
+           case ptr_32bit:
              header_size = GMON_HDRSIZE_OLDBSD_32;
              break;
 
-           case 64:
+           case ptr_64bit:
              header_size = GMON_HDRSIZE_OLDBSD_64;
              break;
-
-           default:
-              fprintf (stderr,
-                       _("%s: bits per address has unexpected value of %u\n"),
-                      whoami, bfd_arch_bits_per_address (core_bfd));
-              done (1);
            }
        }
 
@@ -538,7 +619,8 @@ DEFUN (gmon_out_read, (filename), const char *filename)
 
 
 void
-DEFUN (gmon_out_write, (filename), const char *filename)
+gmon_out_write (filename)
+     const char *filename;
 {
   FILE *ofp;
   struct gmon_hdr ghdr;
@@ -555,7 +637,7 @@ DEFUN (gmon_out_write, (filename), const char *filename)
       /* Write gmon header.  */
 
       memcpy (&ghdr.cookie[0], GMON_MAGIC, 4);
-      bfd_put_32 (core_bfd, GMON_VERSION, (bfd_byte *) ghdr.version);
+      bfd_put_32 (core_bfd, (bfd_vma) GMON_VERSION, (bfd_byte *) ghdr.version);
 
       if (fwrite (&ghdr, sizeof (ghdr), 1, ofp) != 1)
        {
@@ -594,33 +676,27 @@ DEFUN (gmon_out_write, (filename), const char *filename)
          || hz != hertz())
        {
          padsize = 3*4;
-         switch (bfd_arch_bits_per_address (core_bfd))
+         switch (gmon_get_ptr_size ())
            {
-           case 32:
+           case ptr_32bit:
              hdrsize = GMON_HDRSIZE_BSD44_32;
              break;
 
-           case 64:
+           case ptr_64bit:
              hdrsize = GMON_HDRSIZE_BSD44_64;
              break;
-
-           default:
-              fprintf (stderr,
-                       _("%s: bits per address has unexpected value of %u\n"),
-                      whoami, bfd_arch_bits_per_address (core_bfd));
-              done (1);
            }
        }
       else
        {
          padsize = 0;
-         switch (bfd_arch_bits_per_address (core_bfd))
+         switch (gmon_get_ptr_size ())
            {
-           case 32:
+           case ptr_32bit:
              hdrsize = GMON_HDRSIZE_OLDBSD_32;
              break;
 
-           case 64:
+           case ptr_64bit:
              hdrsize = GMON_HDRSIZE_OLDBSD_64;
              /* FIXME: Checking host compiler defines here means that we can't
                 use a cross gprof alpha OSF.  */ 
@@ -628,12 +704,6 @@ DEFUN (gmon_out_write, (filename), const char *filename)
              padsize = 4;
 #endif
              break;
-
-           default:
-              fprintf (stderr,
-                       _("%s: bits per address has unexpected value of %u\n"),
-                      whoami, bfd_arch_bits_per_address (core_bfd));
-              done (1);
            }
        }
 
@@ -652,7 +722,7 @@ DEFUN (gmon_out_write, (filename), const char *filename)
          || hz != hertz())
        {
           if (gmon_io_write_32 (ofp, GMONVERSION)
-             || gmon_io_write_32 (ofp, hz))
+             || gmon_io_write_32 (ofp, (unsigned int) hz))
            {
              perror (filename);
              done (1);
@@ -671,7 +741,8 @@ DEFUN (gmon_out_write, (filename), const char *filename)
       /* Dump the samples.  */
       for (i = 0; i < hist_num_bins; ++i)
        {
-         bfd_put_16 (core_bfd, hist_sample[i], (bfd_byte *) & raw_bin_count[0]);
+         bfd_put_16 (core_bfd, (bfd_vma) hist_sample[i],
+                     (bfd_byte *) &raw_bin_count[0]);
          if (fwrite (&raw_bin_count[0], sizeof (raw_bin_count), 1, ofp) != 1)
            {
              perror (filename);
This page took 0.027648 seconds and 4 git commands to generate.