[09/11] libctf, elfcpp, gold: do not assume that <byteswap.h> contains bswap_*

Message ID 20191216225357.87247-10-nick.alcock@oracle.com
State New
Headers show
Series
  • libctf: various portability fixes and small bugfixes
Related show

Commit Message

Nick Alcock Dec. 16, 2019, 10:53 p.m.
At least one C library (uclibc-ng) defines some of these only when
the compiler is GCC.  We might as well test for all three cases and
handle any of them being missing.

Very similar code exists in libctf and split between elfcpp and gold:
fix both.

(Also sync up elfcpp with a change made to libctf swap.h a few months
ago: since there is no out-of-line definition of the bswap replacements,
they should be declared static inline, not just inline, to prevent the
linker generating out-of-line references to them.)

	PR libctf/25120
libctf/
	* configure.ac: Check for bswap_16, bswap_32, and bswap_64 decls.
	* swap.h (bswap_16): Do not assume that presence of <byteswap.h>
	means this is declared.
	(bswap_32): Likewise.
	(bswap_64): Likewise.
	* configure: Regenerated.
	* config.h.in: Likewise.
gold/
	* configure.ac: Check for bswap_16, bswap_32, and bswap_64 decls.
	* configure: Regenerated.
	* config.h.in: Likewise.
elfcpp/
	* elfcpp_swap.h (bswap_16): Do not assume that presence of
	<byteswap.h> means this is declared.  Make static inline, matching
	recent change to libctf, since there is no non-inline definition
	of these functions.
	(bswap_32): Likewise.
	(bswap_64): Likewise.
---
 elfcpp/ChangeLog     |  10 ++++
 elfcpp/elfcpp_swap.h |  16 ++++--
 gold/ChangeLog       |   7 +++
 gold/config.in       |  12 ++++
 gold/configure       | 127 +++++++++++++++++++++++++++----------------
 gold/configure.ac    |   3 +
 libctf/ChangeLog     |  11 ++++
 libctf/config.h.in   |  12 ++++
 libctf/configure     |  85 ++++++++++++++++++++++++++++-
 libctf/configure.ac  |   3 +
 libctf/swap.h        |  16 +++---
 11 files changed, 241 insertions(+), 61 deletions(-)

-- 
2.24.1.242.gb57e918ca5

Patch

diff --git a/elfcpp/ChangeLog b/elfcpp/ChangeLog
index 3f7e39e6e3..c6dc027b14 100644
--- a/elfcpp/ChangeLog
+++ b/elfcpp/ChangeLog
@@ -1,3 +1,13 @@ 
+2019-12-16  Nick Alcock  <nick.alcock@oracle.com>
+
+	PR libctf/25120
+	* elfcpp_swap.h (bswap_16): Do not assume that presence of
+	<byteswap.h> means this is declared.  Make static inline, matching
+	recent change to libctf, since there is no non-inline definition
+	of these functions.
+	(bswap_32): Likewise.
+	(bswap_64): Likewise.
+
 2019-09-09  Phil Blundell  <pb@pbcl.net>
 
 	binutils 2.33 branch created.
diff --git a/elfcpp/elfcpp_swap.h b/elfcpp/elfcpp_swap.h
index e1f527aec4..24faef5ff8 100644
--- a/elfcpp/elfcpp_swap.h
+++ b/elfcpp/elfcpp_swap.h
@@ -46,15 +46,19 @@ 
 
 #ifdef HAVE_BYTESWAP_H
 #include <byteswap.h>
-#else
+#endif // defined(HAVE_BYTESWAP_H)
+
 // Provide our own versions of the byteswap functions.
-inline uint16_t
+#if !HAVE_DECL_BSWAP_16
+static inline uint16_t
 bswap_16(uint16_t v)
 {
   return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
 }
+#endif // !HAVE_DECL_BSWAP16
 
-inline uint32_t
+#if !HAVE_DECL_BSWAP_32
+static inline uint32_t
 bswap_32(uint32_t v)
 {
   return (  ((v & 0xff000000) >> 24)
@@ -62,8 +66,10 @@  bswap_32(uint32_t v)
 	  | ((v & 0x0000ff00) <<  8)
 	  | ((v & 0x000000ff) << 24));
 }
+#endif // !HAVE_DECL_BSWAP32
 
-inline uint64_t
+#if !HAVE_DECL_BSWAP_64
+static inline uint64_t
 bswap_64(uint64_t v)
 {
   return (  ((v & 0xff00000000000000ULL) >> 56)
@@ -75,7 +81,7 @@  bswap_64(uint64_t v)
 	  | ((v & 0x000000000000ff00ULL) << 40)
 	  | ((v & 0x00000000000000ffULL) << 56));
 }
-#endif // !defined(HAVE_BYTESWAP_H)
+#endif // !HAVE_DECL_BSWAP64
 
 // gcc 4.3 and later provides __builtin_bswap32 and __builtin_bswap64.
 
diff --git a/gold/ChangeLog b/gold/ChangeLog
index 3dc2ce81b7..6db168f7df 100644
--- a/gold/ChangeLog
+++ b/gold/ChangeLog
@@ -1,3 +1,10 @@ 
+2019-12-16  Nick Alcock  <nick.alcock@oracle.com>
+
+	PR libctf/25120
+	* configure.ac: Check for bswap_16, bswap_32, and bswap_64 decls.
+	* configure: Regenerated.
+	* config.h.in: Likewise.
+
 2019-11-26  Martin Liska  <mliska@suse.cz>
 
 	* layout.cc (Layout::special_ordering_of_input_section):
diff --git a/gold/config.in b/gold/config.in
index 7bac34aab2..aaad1bee70 100644
--- a/gold/config.in
+++ b/gold/config.in
@@ -52,6 +52,18 @@ 
    don't. */
 #undef HAVE_DECL_BASENAME
 
+/* Define to 1 if you have the declaration of `bswap_16', and to 0 if you
+   don't. */
+#undef HAVE_DECL_BSWAP_16
+
+/* Define to 1 if you have the declaration of `bswap_32', and to 0 if you
+   don't. */
+#undef HAVE_DECL_BSWAP_32
+
+/* Define to 1 if you have the declaration of `bswap_64', and to 0 if you
+   don't. */
+#undef HAVE_DECL_BSWAP_64
+
 /* Define to 1 if you have the declaration of `ffs', and to 0 if you don't. */
 #undef HAVE_DECL_FFS
 
diff --git a/gold/configure b/gold/configure
index 474c69a125..199a739e7d 100755
--- a/gold/configure
+++ b/gold/configure
@@ -2167,6 +2167,52 @@  fi
 
 } # ac_fn_cxx_check_header_mongrel
 
+# ac_fn_cxx_check_decl LINENO SYMBOL VAR INCLUDES
+# -----------------------------------------------
+# Tests whether SYMBOL is declared in INCLUDES, setting cache variable VAR
+# accordingly.
+ac_fn_cxx_check_decl ()
+{
+  as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
+  as_decl_name=`echo $2|sed 's/ *(.*//'`
+  as_decl_use=`echo $2|sed -e 's/(/((/' -e 's/)/) 0&/' -e 's/,/) 0& (/g'`
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $as_decl_name is declared" >&5
+$as_echo_n "checking whether $as_decl_name is declared... " >&6; }
+if eval \${$3+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+$4
+int
+main ()
+{
+#ifndef $as_decl_name
+#ifdef __cplusplus
+  (void) $as_decl_use;
+#else
+  (void) $as_decl_name;
+#endif
+#endif
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_cxx_try_compile "$LINENO"; then :
+  eval "$3=yes"
+else
+  eval "$3=no"
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+eval ac_res=\$$3
+	       { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
+$as_echo "$ac_res" >&6; }
+  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
+
+} # ac_fn_cxx_check_decl
+
 # ac_fn_cxx_try_link LINENO
 # -------------------------
 # Try to link conftest.$ac_ext, and return whether this succeeded.
@@ -2279,52 +2325,6 @@  $as_echo "$ac_res" >&6; }
   eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
 
 } # ac_fn_cxx_check_func
-
-# ac_fn_cxx_check_decl LINENO SYMBOL VAR INCLUDES
-# -----------------------------------------------
-# Tests whether SYMBOL is declared in INCLUDES, setting cache variable VAR
-# accordingly.
-ac_fn_cxx_check_decl ()
-{
-  as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
-  as_decl_name=`echo $2|sed 's/ *(.*//'`
-  as_decl_use=`echo $2|sed -e 's/(/((/' -e 's/)/) 0&/' -e 's/,/) 0& (/g'`
-  { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $as_decl_name is declared" >&5
-$as_echo_n "checking whether $as_decl_name is declared... " >&6; }
-if eval \${$3+:} false; then :
-  $as_echo_n "(cached) " >&6
-else
-  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h.  */
-$4
-int
-main ()
-{
-#ifndef $as_decl_name
-#ifdef __cplusplus
-  (void) $as_decl_use;
-#else
-  (void) $as_decl_name;
-#endif
-#endif
-
-  ;
-  return 0;
-}
-_ACEOF
-if ac_fn_cxx_try_compile "$LINENO"; then :
-  eval "$3=yes"
-else
-  eval "$3=no"
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-fi
-eval ac_res=\$$3
-	       { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
-$as_echo "$ac_res" >&6; }
-  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
-
-} # ac_fn_cxx_check_decl
 cat >config.log <<_ACEOF
 This file contains any messages produced by compilers while
 running configure, to aid debugging if configure makes a mistake.
@@ -9780,6 +9780,41 @@  fi
 done
 
 
+ac_fn_cxx_check_decl "$LINENO" "bswap_16" "ac_cv_have_decl_bswap_16" "#include <byteswap.h>
+"
+if test "x$ac_cv_have_decl_bswap_16" = xyes; then :
+  ac_have_decl=1
+else
+  ac_have_decl=0
+fi
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_DECL_BSWAP_16 $ac_have_decl
+_ACEOF
+ac_fn_cxx_check_decl "$LINENO" "bswap_32" "ac_cv_have_decl_bswap_32" "#include <byteswap.h>
+"
+if test "x$ac_cv_have_decl_bswap_32" = xyes; then :
+  ac_have_decl=1
+else
+  ac_have_decl=0
+fi
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_DECL_BSWAP_32 $ac_have_decl
+_ACEOF
+ac_fn_cxx_check_decl "$LINENO" "bswap_64" "ac_cv_have_decl_bswap_64" "#include <byteswap.h>
+"
+if test "x$ac_cv_have_decl_bswap_64" = xyes; then :
+  ac_have_decl=1
+else
+  ac_have_decl=0
+fi
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_DECL_BSWAP_64 $ac_have_decl
+_ACEOF
+
+
 for ac_header in windows.h
 do :
   ac_fn_cxx_check_header_mongrel "$LINENO" "windows.h" "ac_cv_header_windows_h" "$ac_includes_default"
diff --git a/gold/configure.ac b/gold/configure.ac
index 76db2b7586..13c3c65520 100644
--- a/gold/configure.ac
+++ b/gold/configure.ac
@@ -602,6 +602,9 @@  AC_CHECK_HEADERS(tr1/unordered_set tr1/unordered_map)
 AC_CHECK_HEADERS(ext/hash_map ext/hash_set)
 AC_CHECK_HEADERS(byteswap.h)
 
+dnl Check for bswap_{16,32,64}
+AC_CHECK_DECLS([bswap_16, bswap_32, bswap_64], [], [], [[#include <byteswap.h>]])
+
 dnl When plugins enabled dynamic loader interface is required. Check headers
 dnl which may provide this interface. Add the necessary library to link.
 AC_CHECK_HEADERS(windows.h)
diff --git a/libctf/ChangeLog b/libctf/ChangeLog
index 7550015cab..5666eebc60 100644
--- a/libctf/ChangeLog
+++ b/libctf/ChangeLog
@@ -1,3 +1,14 @@ 
+2019-12-16  Nick Alcock  <nick.alcock@oracle.com>
+
+	PR libctf/25120
+	* configure.ac: Check for bswap_16, bswap_32, and bswap_64 decls.
+	* swap.h (bswap_16): Do not assume that presence of <byteswap.h>
+	means this is declared.
+	(bswap_32): Likewise.
+	(bswap_64): Likewise.
+	* configure: Regenerated.
+	* config.h.in: Likewise.
+
 2019-12-16  Nick Alcock  <nick.alcock@oracle.com>
 
 	PR libctf/25120
diff --git a/libctf/config.h.in b/libctf/config.h.in
index 4c8be4ab78..e0e37398e0 100644
--- a/libctf/config.h.in
+++ b/libctf/config.h.in
@@ -9,6 +9,18 @@ 
 /* Define to 1 if you have the <byteswap.h> header file. */
 #undef HAVE_BYTESWAP_H
 
+/* Define to 1 if you have the declaration of `bswap_16', and to 0 if you
+   don't. */
+#undef HAVE_DECL_BSWAP_16
+
+/* Define to 1 if you have the declaration of `bswap_32', and to 0 if you
+   don't. */
+#undef HAVE_DECL_BSWAP_32
+
+/* Define to 1 if you have the declaration of `bswap_64', and to 0 if you
+   don't. */
+#undef HAVE_DECL_BSWAP_64
+
 /* Define to 1 if you have the <dlfcn.h> header file. */
 #undef HAVE_DLFCN_H
 
diff --git a/libctf/configure b/libctf/configure
index 66b583fbbe..f56492ca80 100755
--- a/libctf/configure
+++ b/libctf/configure
@@ -1875,6 +1875,52 @@  $as_echo "$ac_res" >&6; }
   eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
 
 } # ac_fn_c_check_func
+
+# ac_fn_c_check_decl LINENO SYMBOL VAR INCLUDES
+# ---------------------------------------------
+# Tests whether SYMBOL is declared in INCLUDES, setting cache variable VAR
+# accordingly.
+ac_fn_c_check_decl ()
+{
+  as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
+  as_decl_name=`echo $2|sed 's/ *(.*//'`
+  as_decl_use=`echo $2|sed -e 's/(/((/' -e 's/)/) 0&/' -e 's/,/) 0& (/g'`
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $as_decl_name is declared" >&5
+$as_echo_n "checking whether $as_decl_name is declared... " >&6; }
+if eval \${$3+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+$4
+int
+main ()
+{
+#ifndef $as_decl_name
+#ifdef __cplusplus
+  (void) $as_decl_use;
+#else
+  (void) $as_decl_name;
+#endif
+#endif
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  eval "$3=yes"
+else
+  eval "$3=no"
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+eval ac_res=\$$3
+	       { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
+$as_echo "$ac_res" >&6; }
+  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
+
+} # ac_fn_c_check_decl
 cat >config.log <<_ACEOF
 This file contains any messages produced by compilers while
 running configure, to aid debugging if configure makes a mistake.
@@ -11385,7 +11431,7 @@  else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11388 "configure"
+#line 11434 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11491,7 +11537,7 @@  else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11494 "configure"
+#line 11540 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -12971,6 +13017,41 @@  fi
 done
 
 
+ac_fn_c_check_decl "$LINENO" "bswap_16" "ac_cv_have_decl_bswap_16" "#include <byteswap.h>
+"
+if test "x$ac_cv_have_decl_bswap_16" = xyes; then :
+  ac_have_decl=1
+else
+  ac_have_decl=0
+fi
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_DECL_BSWAP_16 $ac_have_decl
+_ACEOF
+ac_fn_c_check_decl "$LINENO" "bswap_32" "ac_cv_have_decl_bswap_32" "#include <byteswap.h>
+"
+if test "x$ac_cv_have_decl_bswap_32" = xyes; then :
+  ac_have_decl=1
+else
+  ac_have_decl=0
+fi
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_DECL_BSWAP_32 $ac_have_decl
+_ACEOF
+ac_fn_c_check_decl "$LINENO" "bswap_64" "ac_cv_have_decl_bswap_64" "#include <byteswap.h>
+"
+if test "x$ac_cv_have_decl_bswap_64" = xyes; then :
+  ac_have_decl=1
+else
+  ac_have_decl=0
+fi
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_DECL_BSWAP_64 $ac_have_decl
+_ACEOF
+
+
 
 
 
diff --git a/libctf/configure.ac b/libctf/configure.ac
index aa40e4e234..ffd1f0edac 100644
--- a/libctf/configure.ac
+++ b/libctf/configure.ac
@@ -99,6 +99,9 @@  AC_C_BIGENDIAN
 AC_CHECK_HEADERS(byteswap.h endian.h)
 AC_CHECK_FUNCS(pread)
 
+dnl Check for bswap_{16,32,64}
+AC_CHECK_DECLS([bswap_16, bswap_32, bswap_64], [], [], [[#include <byteswap.h>]])
+
 dnl Check for qsort_r.  (Taken from gnulib.)
 AC_CHECK_FUNCS_ONCE([qsort_r])
 if test $ac_cv_func_qsort_r = yes; then
diff --git a/libctf/swap.h b/libctf/swap.h
index ac62ac78d1..63435b8d92 100644
--- a/libctf/swap.h
+++ b/libctf/swap.h
@@ -25,15 +25,19 @@ 
 
 #ifdef HAVE_BYTESWAP_H
 #include <byteswap.h>
-#else
+#endif /* defined(HAVE_BYTESWAP_H) */
 
 /* Provide our own versions of the byteswap functions.  */
+
+#if !HAVE_DECL_BSWAP_16
 static inline uint16_t
 bswap_16 (uint16_t v)
 {
   return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
 }
+#endif /* !HAVE_DECL_BSWAP16 */
 
+#if !HAVE_DECL_BSWAP_32
 static inline uint32_t
 bswap_32 (uint32_t v)
 {
@@ -42,13 +46,9 @@  bswap_32 (uint32_t v)
 	  | ((v & 0x0000ff00) <<  8)
 	  | ((v & 0x000000ff) << 24));
 }
+#endif /* !HAVE_DECL_BSWAP32 */
 
-inline uint64_t
-bswap_identity_64 (uint64_t v)
-{
-  return v;
-}
-
+#if !HAVE_DECL_BSWAP_64
 static inline uint64_t
 bswap_64 (uint64_t v)
 {
@@ -61,6 +61,6 @@  bswap_64 (uint64_t v)
 	  | ((v & 0x000000000000ff00ULL) << 40)
 	  | ((v & 0x00000000000000ffULL) << 56));
 }
-#endif /* !defined(HAVE_BYTESWAP_H) */
+#endif /* !HAVE_DECL_BSWAP64 */
 
 #endif /* !defined(_CTF_SWAP_H) */