[binutils-gdb] gprofng: need to know that experiment was created on big-endian machine

Vladimir Mezentsev vmezents@sourceware.org
Thu Jun 26 17:47:46 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=221efb6f54a8568d39be370901821d855aba2e83

commit 221efb6f54a8568d39be370901821d855aba2e83
Author: Vladimir Mezentsev <vladimir.mezentsev@oracle.com>
Date:   Wed Jun 25 21:25:02 2025 -0700

    gprofng: need to know that experiment was created on big-endian machine
    
    gprofng/ChangeLog
    2025-06-25  Vladimir Mezentsev  <vladimir.mezentsev@oracle.com>
    
            * configure.ac: Add AC_C_BIGENDIAN.
            * common/config.h.in: Rebuild.
            * configure: Rebuild.
            * libcollector/collector.c (log_header_write): Save big-endian flag.
            * src/DbeSession.h (is_bigendian): New function.
            * src/DbeSession.cc: Likewise.
            * src/Experiment.cc: Set 'bigendian' and 'need_swap_endian'.
            * src/Experiment.h: New field 'bigendian'.
            * src/LoadObject.cc: Remove an unused variable.
            * src/LoadObject.h: Likewise.

Diff:
---
 gprofng/common/config.h.in       |  15 +++
 gprofng/configure                | 225 +++++++++++++++++++++++++++++++++++++++
 gprofng/configure.ac             |   1 +
 gprofng/libcollector/collector.c |  11 +-
 gprofng/src/DbeSession.cc        |  11 ++
 gprofng/src/DbeSession.h         |   1 +
 gprofng/src/Experiment.cc        |   9 +-
 gprofng/src/Experiment.h         |   1 +
 gprofng/src/LoadObject.cc        |   1 -
 gprofng/src/LoadObject.h         |   1 -
 10 files changed, 270 insertions(+), 6 deletions(-)

diff --git a/gprofng/common/config.h.in b/gprofng/common/config.h.in
index f8484f238fd..aabb043957a 100644
--- a/gprofng/common/config.h.in
+++ b/gprofng/common/config.h.in
@@ -1,5 +1,8 @@
 /* common/config.h.in.  Generated from configure.ac by autoheader.  */
 
+/* Define if building universal (internal helper macro) */
+#undef AC_APPLE_UNIVERSAL_BUILD
+
 /* Enable debugging output. */
 #undef DEBUG
 
@@ -109,6 +112,18 @@
 /* Version number of package */
 #undef VERSION
 
+/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
+   significant byte first (like Motorola and SPARC, unlike Intel). */
+#if defined AC_APPLE_UNIVERSAL_BUILD
+# if defined __BIG_ENDIAN__
+#  define WORDS_BIGENDIAN 1
+# endif
+#else
+# ifndef WORDS_BIGENDIAN
+#  undef WORDS_BIGENDIAN
+# endif
+#endif
+
 /* Define to 1 if on MINIX. */
 #undef _MINIX
 
diff --git a/gprofng/configure b/gprofng/configure
index 3f408634ca5..2edfff107dd 100755
--- a/gprofng/configure
+++ b/gprofng/configure
@@ -15623,6 +15623,230 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu
 # Only expand once:
 
 
+ { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether byte ordering is bigendian" >&5
+$as_echo_n "checking whether byte ordering is bigendian... " >&6; }
+if ${ac_cv_c_bigendian+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_cv_c_bigendian=unknown
+    # See if we're dealing with a universal compiler.
+    cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#ifndef __APPLE_CC__
+	       not a universal capable compiler
+	     #endif
+	     typedef int dummy;
+
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+
+	# Check for potential -arch flags.  It is not universal unless
+	# there are at least two -arch flags with different values.
+	ac_arch=
+	ac_prev=
+	for ac_word in $CC $CFLAGS $CPPFLAGS $LDFLAGS; do
+	 if test -n "$ac_prev"; then
+	   case $ac_word in
+	     i?86 | x86_64 | ppc | ppc64)
+	       if test -z "$ac_arch" || test "$ac_arch" = "$ac_word"; then
+		 ac_arch=$ac_word
+	       else
+		 ac_cv_c_bigendian=universal
+		 break
+	       fi
+	       ;;
+	   esac
+	   ac_prev=
+	 elif test "x$ac_word" = "x-arch"; then
+	   ac_prev=arch
+	 fi
+       done
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+    if test $ac_cv_c_bigendian = unknown; then
+      # See if sys/param.h defines the BYTE_ORDER macro.
+      cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <sys/types.h>
+	     #include <sys/param.h>
+
+int
+main ()
+{
+#if ! (defined BYTE_ORDER && defined BIG_ENDIAN \
+		     && defined LITTLE_ENDIAN && BYTE_ORDER && BIG_ENDIAN \
+		     && LITTLE_ENDIAN)
+	      bogus endian macros
+	     #endif
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  # It does; now see whether it defined to BIG_ENDIAN or not.
+	 cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <sys/types.h>
+		#include <sys/param.h>
+
+int
+main ()
+{
+#if BYTE_ORDER != BIG_ENDIAN
+		 not big endian
+		#endif
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  ac_cv_c_bigendian=yes
+else
+  ac_cv_c_bigendian=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+    fi
+    if test $ac_cv_c_bigendian = unknown; then
+      # See if <limits.h> defines _LITTLE_ENDIAN or _BIG_ENDIAN (e.g., Solaris).
+      cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <limits.h>
+
+int
+main ()
+{
+#if ! (defined _LITTLE_ENDIAN || defined _BIG_ENDIAN)
+	      bogus endian macros
+	     #endif
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  # It does; now see whether it defined to _BIG_ENDIAN or not.
+	 cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <limits.h>
+
+int
+main ()
+{
+#ifndef _BIG_ENDIAN
+		 not big endian
+		#endif
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  ac_cv_c_bigendian=yes
+else
+  ac_cv_c_bigendian=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+    fi
+    if test $ac_cv_c_bigendian = unknown; then
+      # Compile a test program.
+      if test "$cross_compiling" = yes; then :
+  # Try to guess by grepping values from an object file.
+	 cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+short int ascii_mm[] =
+		  { 0x4249, 0x4765, 0x6E44, 0x6961, 0x6E53, 0x7953, 0 };
+		short int ascii_ii[] =
+		  { 0x694C, 0x5454, 0x656C, 0x6E45, 0x6944, 0x6E61, 0 };
+		int use_ascii (int i) {
+		  return ascii_mm[i] + ascii_ii[i];
+		}
+		short int ebcdic_ii[] =
+		  { 0x89D3, 0xE3E3, 0x8593, 0x95C5, 0x89C4, 0x9581, 0 };
+		short int ebcdic_mm[] =
+		  { 0xC2C9, 0xC785, 0x95C4, 0x8981, 0x95E2, 0xA8E2, 0 };
+		int use_ebcdic (int i) {
+		  return ebcdic_mm[i] + ebcdic_ii[i];
+		}
+		extern int foo;
+
+int
+main ()
+{
+return use_ascii (foo) == use_ebcdic (foo);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  if grep BIGenDianSyS conftest.$ac_objext >/dev/null; then
+	      ac_cv_c_bigendian=yes
+	    fi
+	    if grep LiTTleEnDian conftest.$ac_objext >/dev/null ; then
+	      if test "$ac_cv_c_bigendian" = unknown; then
+		ac_cv_c_bigendian=no
+	      else
+		# finding both strings is unlikely to happen, but who knows?
+		ac_cv_c_bigendian=unknown
+	      fi
+	    fi
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+$ac_includes_default
+int
+main ()
+{
+
+	     /* Are we little or big endian?  From Harbison&Steele.  */
+	     union
+	     {
+	       long int l;
+	       char c[sizeof (long int)];
+	     } u;
+	     u.l = 1;
+	     return u.c[sizeof (long int) - 1] == 1;
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_run "$LINENO"; then :
+  ac_cv_c_bigendian=no
+else
+  ac_cv_c_bigendian=yes
+fi
+rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \
+  conftest.$ac_objext conftest.beam conftest.$ac_ext
+fi
+
+    fi
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_c_bigendian" >&5
+$as_echo "$ac_cv_c_bigendian" >&6; }
+ case $ac_cv_c_bigendian in #(
+   yes)
+     $as_echo "#define WORDS_BIGENDIAN 1" >>confdefs.h
+;; #(
+   no)
+      ;; #(
+   universal)
+
+$as_echo "#define AC_APPLE_UNIVERSAL_BUILD 1" >>confdefs.h
+
+     ;; #(
+   *)
+     as_fn_error $? "unknown endianness
+ presetting ac_cv_c_bigendian=no (or yes) will help" "$LINENO" 5 ;;
+ esac
+
 
 GPROFNG_LIBADD="-L../../libiberty -liberty"
 if test "$enable_shared" = "yes"; then
@@ -17145,6 +17369,7 @@ if test -z "${am__fastdepCXX_TRUE}" && test -z "${am__fastdepCXX_FALSE}"; then
   as_fn_error $? "conditional \"am__fastdepCXX\" was never defined.
 Usually this means the macro was only invoked conditionally." "$LINENO" 5
 fi
+
 if test -z "${BUILD_COLLECTOR_TRUE}" && test -z "${BUILD_COLLECTOR_FALSE}"; then
   as_fn_error $? "conditional \"BUILD_COLLECTOR\" was never defined.
 Usually this means the macro was only invoked conditionally." "$LINENO" 5
diff --git a/gprofng/configure.ac b/gprofng/configure.ac
index d7a2c386d10..1f473b9b583 100644
--- a/gprofng/configure.ac
+++ b/gprofng/configure.ac
@@ -33,6 +33,7 @@ gl_PROG_BISON([BISON],[3.0.4])
 
 AC_DISABLE_SHARED
 LT_INIT
+AC_C_BIGENDIAN
 
 GPROFNG_LIBADD="-L../../libiberty -liberty"
 if test "$enable_shared" = "yes"; then
diff --git a/gprofng/libcollector/collector.c b/gprofng/libcollector/collector.c
index c5fa3cbb30e..3bb3fc3cfff 100644
--- a/gprofng/libcollector/collector.c
+++ b/gprofng/libcollector/collector.c
@@ -2051,8 +2051,15 @@ log_header_write (sp_origin_t origin)
     {
       long page_size = CALL_UTIL (sysconf)(_SC_PAGESIZE);
       long npages = CALL_UTIL (sysconf)(_SC_PHYS_PAGES);
-      __collector_log_write ("<system hostname=\"%s\" arch=\"%s\" os=\"%s %s\" pagesz=\"%ld\" npages=\"%ld\">\n",
-			     sysinfo.nodename, sysinfo.machine, sysinfo.sysname, sysinfo.release, page_size, npages);
+#ifdef WORDS_BIGENDIAN
+      int bigendian = 1;
+#else
+      int bigendian = 0;
+#endif
+      __collector_log_write ("<system hostname=\"%s\" arch=\"%s\" os=\"%s %s\" "
+		"pagesz=\"%ld\" npages=\"%ld\" bigendian=\"%d\">\n",
+		sysinfo.nodename, sysinfo.machine, sysinfo.sysname,
+		sysinfo.release, page_size, npages, bigendian);
     }
 
   //YXXX Updating this section?  Check similar cut/paste code in:
diff --git a/gprofng/src/DbeSession.cc b/gprofng/src/DbeSession.cc
index f3426cefd31..b0aa7455253 100644
--- a/gprofng/src/DbeSession.cc
+++ b/gprofng/src/DbeSession.cc
@@ -2016,6 +2016,17 @@ DbeSession::is_omp_available ()
   return status_ompavail == 1;
 }
 
+bool
+DbeSession::is_bigendian ()
+{
+#ifdef WORDS_BIGENDIAN
+  return true;
+#else
+  return false;
+#endif
+
+}
+
 bool
 DbeSession::has_java ()
 {
diff --git a/gprofng/src/DbeSession.h b/gprofng/src/DbeSession.h
index 5cbddae4c92..7db0998cfbb 100644
--- a/gprofng/src/DbeSession.h
+++ b/gprofng/src/DbeSession.h
@@ -120,6 +120,7 @@ public:
   bool is_timeline_available ();
   bool is_ifreq_available ();
   bool is_omp_available ();
+  static bool is_bigendian ();
   bool has_java ();
   bool has_ompavail ();
 
diff --git a/gprofng/src/Experiment.cc b/gprofng/src/Experiment.cc
index 45550e8d38f..4cbb7f6d33b 100644
--- a/gprofng/src/Experiment.cc
+++ b/gprofng/src/Experiment.cc
@@ -541,10 +541,14 @@ Experiment::ExperimentHandler::startElement (char*, char*, char *qName, Attribut
 	    exp->platform = RISCV;
 	  else
 	    exp->platform = Sparc;
-	  exp->need_swap_endian = (DbeSession::platform == Sparc) ?
-		  (exp->platform != Sparc) : (exp->platform == Sparc);
 	  exp->architecture = xstrdup (str);
 	}
+      str = attrs->getValue (NTXT ("bigendian"));
+      if (str != NULL)
+	{
+	  exp->bigendian = *str == '1';
+	  exp->need_swap_endian = DbeSession::is_bigendian () != exp->bigendian;
+	}
       str = attrs->getValue (NTXT ("pagesz"));
       if (str != NULL)
 	exp->page_size = atoi (str);
@@ -1314,6 +1318,7 @@ Experiment::Experiment ()
   exp_maj_version = 0;
   exp_min_version = 0;
   platform = Unknown;
+  bigendian = DbeSession::is_bigendian();  // can be changed in log.xml reading
   wsize = Wnone;
   page_size = 4096;
   npages = 0;
diff --git a/gprofng/src/Experiment.h b/gprofng/src/Experiment.h
index 2047a15f1c4..c85aadd5181 100644
--- a/gprofng/src/Experiment.h
+++ b/gprofng/src/Experiment.h
@@ -135,6 +135,7 @@ public:
 			// for current experiment, i.e. 8 for 32bit addresses
   int broken;           // If SP_JCMD_RUN line not seen
   int obsolete;         // If pointer file experiment detected
+  bool bigendian;       // the experiment was created on a big_endian machine
   bool hwc_default;     // True if HW counters were enabled by default
   int hwc_bogus;        // Count of bogus HWC packets
   int hwc_lost_int;     // Count of packets reflecting lost interrupt
diff --git a/gprofng/src/LoadObject.cc b/gprofng/src/LoadObject.cc
index 3cdeb9d3e7b..bf49e9d24a8 100644
--- a/gprofng/src/LoadObject.cc
+++ b/gprofng/src/LoadObject.cc
@@ -87,7 +87,6 @@ LoadObject::LoadObject (const char *loname)
   size = 0;
   type = SEG_UNKNOWN;
   isReadStabs = false;
-  need_swap_endian = false;
   instHTable = new DbeInstr*[LO_InstHTableSize];
   for (int i = 0; i < LO_InstHTableSize; i++)
     instHTable[i] = NULL;
diff --git a/gprofng/src/LoadObject.h b/gprofng/src/LoadObject.h
index 14dfe2682a9..dd40ea0a050 100644
--- a/gprofng/src/LoadObject.h
+++ b/gprofng/src/LoadObject.h
@@ -167,7 +167,6 @@ public:
 
   unsigned int flags;           // SEG_FLAG_*
   bool isReadStabs;
-  bool need_swap_endian;
   int seg_idx;                  // for compatibility (ADDRESS)
   seg_type type;
   int64_t size;                 // size of loadobject in bytes


More information about the Binutils-cvs mailing list