From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1551) id C4D79383B7A4; Mon, 25 Jul 2022 15:12:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C4D79383B7A4 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Pedro Alves To: gdb-cvs@sourceware.org Subject: [binutils-gdb] struct packed: Add fallback byte array implementation X-Act-Checkin: binutils-gdb X-Git-Author: Pedro Alves X-Git-Refname: refs/heads/master X-Git-Oldrev: e249e6b8012ea0a14e5768d31becd7b4caff8e77 X-Git-Newrev: b669667d0749ccf8d70792bb086f41904157e54d Message-Id: <20220725151220.C4D79383B7A4@sourceware.org> Date: Mon, 25 Jul 2022 15:12:20 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Jul 2022 15:12:20 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Db669667d0749= ccf8d70792bb086f41904157e54d commit b669667d0749ccf8d70792bb086f41904157e54d Author: Pedro Alves Date: Tue Jul 19 00:26:33 2022 +0100 struct packed: Add fallback byte array implementation =20 Attribute gcc_struct is not implemented in Clang targeting Windows, so add a fallback standard-conforming implementation based on arrays. =20 I ran the testsuite on x86_64 GNU/Linux with this implementation forced, and saw no regressions. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D29373 =20 Change-Id: I023315ee03622c59c397bf4affc0b68179c32374 Diff: --- gdbsupport/packed.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++-= -- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/gdbsupport/packed.h b/gdbsupport/packed.h index d721b02c056..dbece2a29aa 100644 --- a/gdbsupport/packed.h +++ b/gdbsupport/packed.h @@ -28,9 +28,27 @@ bit-fields (and ENUM_BITFIELD), when the fields must have separate memory locations to avoid data races. */ =20 -/* We need gcc_struct on Windows GCC, as otherwise the size of e.g., - "packed" will be larger than what we want. */ -#if defined _WIN32 +/* There are two implementations here -- one standard compliant, using + a byte array for internal representation, and another that relies + on bitfields and attribute packed (and attribute gcc_struct on + Windows). The latter is preferable, as it is more convenient when + debugging GDB -- printing a struct packed variable prints its field + using its natural type, which is particularly useful if the type is + an enum -- but may not work on all compilers. */ + +/* Clang targeting Windows does not support attribute gcc_struct, so + we use the alternative byte array implemention there. */ +#if defined _WIN32 && defined __clang__ +# define PACKED_USE_ARRAY 1 +#else +# define PACKED_USE_ARRAY 0 +#endif + +/* For the preferred implementation, we need gcc_struct on Windows, as + otherwise the size of e.g., "packed" will be larger than + what we want. Clang targeting Windows does not support attribute + gcc_struct. */ +#if !PACKED_USE_ARRAY && defined _WIN32 && !defined __clang__ # define ATTRIBUTE_GCC_STRUCT __attribute__((__gcc_struct__)) #else # define ATTRIBUTE_GCC_STRUCT @@ -44,7 +62,18 @@ public: =20 packed (T val) { + gdb_static_assert (sizeof (ULONGEST) >=3D sizeof (T)); + +#if PACKED_USE_ARRAY + ULONGEST tmp =3D val; + for (int i =3D (Bytes - 1); i >=3D 0; --i) + { + m_bytes[i] =3D (gdb_byte) tmp; + tmp >>=3D HOST_CHAR_BIT; + } +#else m_val =3D val; +#endif =20 /* Ensure size and aligment are what we expect. */ gdb_static_assert (sizeof (packed) =3D=3D Bytes); @@ -62,11 +91,27 @@ public: =20 operator T () const noexcept { +#if PACKED_USE_ARRAY + ULONGEST tmp =3D 0; + for (int i =3D 0;;) + { + tmp |=3D m_bytes[i]; + if (++i =3D=3D Bytes) + break; + tmp <<=3D HOST_CHAR_BIT; + } + return (T) tmp; +#else return m_val; +#endif } =20 private: +#if PACKED_USE_ARRAY + gdb_byte m_bytes[Bytes]; +#else T m_val : (Bytes * HOST_CHAR_BIT) ATTRIBUTE_PACKED; +#endif }; =20 /* Add some comparisons between std::atomic> and packed