Fix undefined local label test for COFF based ARM ports
[deliverable/binutils-gdb.git] / gprof / gmon_io.c
index f821b0ca2ef3d5feb03c84ea69f7929e49935775..83c0e64ab0aa3c048ce9097aed99b32f1c8b05f8 100644 (file)
@@ -1,6 +1,6 @@
 /* gmon_io.c - Input and output from/to gmon.out files.
 
-   Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
+   Copyright 2000, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
 
    This file is part of GNU Binutils.
 
 #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 (void);
+static enum gmon_ptr_signedness gmon_get_ptr_signedness (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));
+static int gmon_io_read_64 (FILE *, BFD_HOST_U_64_BIT *);
+static int gmon_io_write_64 (FILE *, BFD_HOST_U_64_BIT);
 #endif
 static int gmon_read_raw_arc
-  PARAMS ((FILE *, bfd_vma *, bfd_vma *, unsigned long *));
+  (FILE *, bfd_vma *, bfd_vma *, unsigned long *);
 static int gmon_write_raw_arc
-  PARAMS ((FILE *, bfd_vma, bfd_vma, unsigned long));
+  (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
-gmon_io_read_32 (ifp, valp)
-     FILE *ifp;
-     unsigned int *valp;
+gmon_io_read_32 (FILE *ifp, unsigned int *valp)
 {
   char buf[4];
 
@@ -61,9 +110,7 @@ gmon_io_read_32 (ifp, valp)
 
 #ifdef BFD_HOST_U_64_BIT
 static int
-gmon_io_read_64 (ifp, valp)
-     FILE *ifp;
-     BFD_HOST_U_64_BIT *valp;
+gmon_io_read_64 (FILE *ifp, BFD_HOST_U_64_BIT *valp)
 {
   char buf[8];
 
@@ -75,44 +122,42 @@ gmon_io_read_64 (ifp, valp)
 #endif
 
 int
-gmon_io_read_vma (ifp, valp)
-     FILE *ifp;
-     bfd_vma *valp;
+gmon_io_read_vma (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;
 
 #ifdef BFD_HOST_U_64_BIT
-    case 64:
+    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;
 #endif
-
-    default:
-      fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
-              whoami, bfd_arch_bits_per_address (core_bfd));
-      done (1);
     }
   return 0;
 }
 
 int
-gmon_io_read (ifp, buf, n)
-     FILE *ifp;
-     char *buf;
-     size_t n;
+gmon_io_read (FILE *ifp, char *buf, size_t n)
 {
   if (fread (buf, 1, n, ifp) != n)
     return 1;
@@ -120,9 +165,7 @@ gmon_io_read (ifp, buf, n)
 }
 
 int
-gmon_io_write_32 (ofp, val)
-     FILE *ofp;
-     unsigned int val;
+gmon_io_write_32 (FILE *ofp, unsigned int val)
 {
   char buf[4];
 
@@ -134,9 +177,7 @@ gmon_io_write_32 (ofp, val)
 
 #ifdef BFD_HOST_U_64_BIT
 static int
-gmon_io_write_64 (ofp, val)
-     FILE *ofp;        
-     BFD_HOST_U_64_BIT val;
+gmon_io_write_64 (FILE *ofp, BFD_HOST_U_64_BIT val)
 {
   char buf[8];
 
@@ -148,37 +189,28 @@ gmon_io_write_64 (ofp, val)
 #endif
 
 int
-gmon_io_write_vma (ofp, val)
-     FILE *ofp;
-     bfd_vma val;
+gmon_io_write_vma (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;
 
 #ifdef BFD_HOST_U_64_BIT
-    case 64:
+    case ptr_64bit:
       if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) val))
        return 1;
       break;
 #endif
-
-    default:
-      fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
-              whoami, bfd_arch_bits_per_address (core_bfd));
-      done (1);
     }
   return 0;
 }
 
 int
-gmon_io_write_8 (ofp, val)
-     FILE *ofp;        
-     unsigned int val;
+gmon_io_write_8 (FILE *ofp, unsigned int val)
 {
   char buf[1];
 
@@ -189,10 +221,7 @@ gmon_io_write_8 (ofp, val)
 }
 
 int
-gmon_io_write (ofp, buf, n)
-     FILE *ofp;        
-     char *buf;
-     size_t n;
+gmon_io_write (FILE *ofp, char *buf, size_t n)
 {
   if (fwrite (buf, 1, n, ofp) != n)
     return 1;
@@ -200,11 +229,7 @@ gmon_io_write (ofp, buf, n)
 }
 
 static int
-gmon_read_raw_arc (ifp, fpc, spc, cnt)
-     FILE *ifp;
-     bfd_vma *fpc;
-     bfd_vma *spc;
-     unsigned long *cnt;
+gmon_read_raw_arc (FILE *ifp, bfd_vma *fpc, bfd_vma *spc, unsigned long *cnt)
 {
 #ifdef BFD_HOST_U_64_BIT
   BFD_HOST_U_64_BIT cnt64;
@@ -215,67 +240,52 @@ gmon_read_raw_arc (ifp, fpc, spc, cnt)
       || 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;
 
 #ifdef BFD_HOST_U_64_BIT
-    case 64:
+    case ptr_64bit:
       if (gmon_io_read_64 (ifp, &cnt64))
        return 1;
       *cnt = cnt64;
       break;
 #endif
-
-    default:
-      fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
-              whoami, bfd_arch_bits_per_address (core_bfd));
-      done (1);
     }
   return 0;
 }
 
 static int
-gmon_write_raw_arc (ofp, fpc, spc, cnt)
-     FILE *ofp;
-     bfd_vma fpc;
-     bfd_vma spc;
-     unsigned long cnt;
+gmon_write_raw_arc (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;
 
 #ifdef BFD_HOST_U_64_BIT
-    case 64:
+    case ptr_64bit:
       if (gmon_io_write_64 (ofp, (BFD_HOST_U_64_BIT) cnt))
        return 1;
       break;
 #endif
-
-    default:
-      fprintf (stderr, _("%s: bits per address has unexpected value of %u\n"),
-              whoami, bfd_arch_bits_per_address (core_bfd));
-      done (1);
     }
   return 0;
 }
 
 void
-gmon_out_read (filename)
-     const char *filename;
+gmon_out_read (const char *filename)
 {
   FILE *ifp;
   struct gmon_hdr ghdr;
@@ -368,15 +378,16 @@ gmon_out_read (filename)
       {
        bfd_vma low_pc;
        bfd_vma high_pc;
-       int ncnt;
+       unsigned int ncnt;
       };
-      int i, samp_bytes, header_size = 0;
+      unsigned int i;
+      int samp_bytes, header_size = 0;
       unsigned long count;
       bfd_vma from_pc, self_pc;
       static struct hdr h;
       UNIT raw_bin_count;
       struct hdr tmp;
-      int version;
+      unsigned int version;
 
       /* Information from a gmon.out file is in two parts: an array of
         sampling hits within pc ranges, and the arcs.  */
@@ -409,7 +420,7 @@ gmon_out_read (filename)
 
       if (version == GMONVERSION)
        {
-         int profrate;
+         unsigned int profrate;
 
          /* 4.4BSD format header.  */
           if (gmon_io_read_32 (ifp, &profrate))
@@ -417,7 +428,7 @@ gmon_out_read (filename)
 
          if (!s_highpc)
            hz = profrate;
-         else if (hz != profrate)
+         else if (hz != (int) profrate)
            {
              fprintf (stderr,
                       _("%s: profiling rate incompatible with first gmon file\n"),
@@ -425,21 +436,15 @@ gmon_out_read (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
@@ -452,21 +457,15 @@ gmon_out_read (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);
            }
        }
 
@@ -592,8 +591,7 @@ gmon_out_read (filename)
 
 
 void
-gmon_out_write (filename)
-     const char *filename;
+gmon_out_write (const char *filename)
 {
   FILE *ofp;
   struct gmon_hdr ghdr;
@@ -633,7 +631,7 @@ gmon_out_write (filename)
   else if (file_format == FF_BSD || file_format == FF_BSD44)
     {
       UNIT raw_bin_count;
-      int i, hdrsize;
+      unsigned int i, hdrsize;
       unsigned padsize;
       char pad[3*4];
       Arc *arc;
@@ -649,33 +647,27 @@ gmon_out_write (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.  */ 
@@ -683,12 +675,6 @@ gmon_out_write (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);
            }
        }
 
This page took 0.029932 seconds and 4 git commands to generate.