[RFC] libsframe: add local sframe-swap.h header

Message ID 20260817172602.777109-1-ibhagatgnu@gmail.com
State New
Headers
Series [RFC] libsframe: add local sframe-swap.h header |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm warning Skipped because it is an RFC
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 warning Skipped because it is an RFC

Commit Message

Indu Bhagat Aug. 17, 2026, 5:26 p.m. UTC
  Currently libsframe includes libctf/swap.h for byteswapping functions.
This cross-directory inclusion of a private header leads to build
conflicts during in-tree builds when both libsframe/config.h and
libctf/config.h are present, causing duplicate macro warnings (More
details at
https://inbox.sourceware.org/binutils/27dfa5e4c684cfdd773e207e8022234109eb1b91.1786465076.git.aburgess@redhat.com/T/#u).

To completely decouple libsframe from libctf, add a local header
libsframe/sframe-swap.h containing the byte-swapping utility functions,
and remove the -I/../libctf from libsframe/Makefile.am.

libsframe/
	* Makefile.am (AM_CPPFLAGS): Remove -I/../libctf.
	* Makefile.in: Regenerate.
	* sframe-swap.h: New header.
	* sframe.c: Include sframe-swap.h instead of swap.h.
---
 libsframe/Makefile.am   |  3 +-
 libsframe/Makefile.in   |  3 +-
 libsframe/sframe-swap.h | 94 +++++++++++++++++++++++++++++++++++++++++
 libsframe/sframe.c      |  2 +-
 4 files changed, 97 insertions(+), 5 deletions(-)
 create mode 100644 libsframe/sframe-swap.h
---

Notes:

This is a follow up on the existing thread proposing removal of
including config.h from libctf/swap.h (and possible short term
resolution to:).

https://inbox.sourceware.org/binutils/ff90d851-ba7d-47be-998f-41559ad806ce@gmail.com/T/#t
  

Comments

Nix Aug. 24, 2026, 12:58 p.m. UTC | #1
On 17 Aug 2026, Indu Bhagat stated:

> Currently libsframe includes libctf/swap.h for byteswapping functions.
> This cross-directory inclusion of a private header leads to build
> conflicts during in-tree builds when both libsframe/config.h and
> libctf/config.h are present, causing duplicate macro warnings (More
> details at
> https://inbox.sourceware.org/binutils/27dfa5e4c684cfdd773e207e8022234109eb1b91.1786465076.git.aburgess@redhat.com/T/#u).
>
> To completely decouple libsframe from libctf, add a local header
> libsframe/sframe-swap.h containing the byte-swapping utility functions,
> and remove the -I/../libctf from libsframe/Makefile.am.

There is no -I/../libctf there. (Yes, I am being ridiculously pedantic.)

> libsframe/
> 	* Makefile.am (AM_CPPFLAGS): Remove -I/../libctf.

Ditto.

> diff --git a/libsframe/sframe-swap.h b/libsframe/sframe-swap.h
> new file mode 100644
> index 00000000000..ebe7865f8e9
> --- /dev/null
> +++ b/libsframe/sframe-swap.h
> @@ -0,0 +1,94 @@
> +/* Interface to byteswapping functions for libsframe.

I'm not sure, but you might possibly be able to keep all the advantages
of this change while avoiding all the code duplication by just making
this file

#include "../libctf/swap.h"

while keeping everything else in this change the same. (glibc does this
sort of thing a lot.)
  

Patch

diff --git a/libsframe/Makefile.am b/libsframe/Makefile.am
index 8f4ed7e1f8b..8f4f50ea849 100644
--- a/libsframe/Makefile.am
+++ b/libsframe/Makefile.am
@@ -26,8 +26,7 @@  DISTCLEANFILES =
 MAINTAINERCLEANFILES =
 
 INCDIR = $(srcdir)/../include
-# include libctf for swap.h
-AM_CPPFLAGS = -I$(srcdir)/../include -I$(srcdir)/../libctf
+AM_CPPFLAGS = -I$(srcdir)/../include
 AM_CFLAGS = @ac_libsframe_warn_cflags@
 libsframe_version_info = -version-info `grep -v '^\#' $(srcdir)/libtool-version`
 # libsframe does not restrict the set of exported symbols.  So, if linker does
diff --git a/libsframe/Makefile.in b/libsframe/Makefile.in
index bd61638262b..4f7e295efe6 100644
--- a/libsframe/Makefile.in
+++ b/libsframe/Makefile.in
@@ -559,8 +559,7 @@  info_TEXINFOS = $(am__append_1)
 DISTCLEANFILES = $(am__append_2)
 MAINTAINERCLEANFILES = $(am__append_3)
 INCDIR = $(srcdir)/../include
-# include libctf for swap.h
-AM_CPPFLAGS = -I$(srcdir)/../include -I$(srcdir)/../libctf
+AM_CPPFLAGS = -I$(srcdir)/../include
 AM_CFLAGS = @ac_libsframe_warn_cflags@
 libsframe_version_info = -version-info `grep -v '^\#' $(srcdir)/libtool-version`
 @HAVE_LD_VERSION_SCRIPT_TRUE@@HAVE_SOLARIS_LD_FALSE@libsframe_version_script = -Wl,--version-script=$(srcdir)/libsframe.ver
diff --git a/libsframe/sframe-swap.h b/libsframe/sframe-swap.h
new file mode 100644
index 00000000000..ebe7865f8e9
--- /dev/null
+++ b/libsframe/sframe-swap.h
@@ -0,0 +1,94 @@ 
+/* Interface to byteswapping functions for libsframe.
+
+   Copyright (C) 2026 Free Software Foundation, Inc.
+
+   This file is part of libsframe.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   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, see <http://www.gnu.org/licenses/>.  */
+
+
+#ifndef _SFRAME_SWAP_H
+#define _SFRAME_SWAP_H
+
+#include "config.h"
+#include <stdint.h>
+#include <assert.h>
+
+#ifdef HAVE_BYTESWAP_H
+#include <byteswap.h>
+#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)
+{
+  return (  ((v & 0xff000000) >> 24)
+	  | ((v & 0x00ff0000) >>  8)
+	  | ((v & 0x0000ff00) <<  8)
+	  | ((v & 0x000000ff) << 24));
+}
+#endif /* !HAVE_DECL_BSWAP32 */
+
+#if !HAVE_DECL_BSWAP_64
+static inline uint64_t
+bswap_64 (uint64_t v)
+{
+  return (  ((v & 0xff00000000000000ULL) >> 56)
+	  | ((v & 0x00ff000000000000ULL) >> 40)
+	  | ((v & 0x0000ff0000000000ULL) >> 24)
+	  | ((v & 0x000000ff00000000ULL) >>  8)
+	  | ((v & 0x00000000ff000000ULL) <<  8)
+	  | ((v & 0x0000000000ff0000ULL) << 24)
+	  | ((v & 0x000000000000ff00ULL) << 40)
+	  | ((v & 0x00000000000000ffULL) << 56));
+}
+#endif /* !HAVE_DECL_BSWAP64 */
+
+/* < C11? define away static assertions.  */
+
+#if !defined (__STDC_VERSION__) || __STDC_VERSION__ < 201112L
+#ifndef _Static_assert
+#define _Static_assert(cond, err)
+#endif
+#endif
+
+/* Swap the endianness of something.  */
+
+#define swap_thing(x)							\
+  do									\
+    {									\
+      _Static_assert (sizeof (x) == 1 || (sizeof (x) % 2 == 0		\
+					  && sizeof (x) <= 8),		\
+		      "Invalid size, update endianness code");		\
+      switch (sizeof (x)) {						\
+      case 2: x = bswap_16 (x); break;					\
+      case 4: x = bswap_32 (x); break;					\
+      case 8: x = bswap_64 (x); break;					\
+      case 1: /* Nothing needs doing */					\
+	break;								\
+      }									\
+    }									\
+  while (0);
+
+#endif /* _SFRAME_SWAP_H */
diff --git a/libsframe/sframe.c b/libsframe/sframe.c
index cd6bb3022db..286b7d90e64 100644
--- a/libsframe/sframe.c
+++ b/libsframe/sframe.c
@@ -24,7 +24,7 @@ 
 #include <string.h>
 #include <stddef.h>
 #include "sframe-impl.h"
-#include "swap.h"
+#include "sframe-swap.h"
 
 /* Representation of SFrame FDE internal to libsframe.  */
 typedef struct sframe_func_desc_entry_int