mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2025-01-18 12:24:38 +08:00
4ca26ad7de
Building GDB on mingw/gcc hosts is currently broken, due to a static assertion failure in gdbsupport/packed.h: In file included from ../../../../../binutils-gdb/gdb/../gdbsupport/common-defs.h:201, from ../../../../../binutils-gdb/gdb/defs.h:28, from ../../../../../binutils-gdb/gdb/dwarf2/read.c:31: ../../../../../binutils-gdb/gdb/../gdbsupport/packed.h: In instantiation of 'packed<T, Bytes>::packed(T) [with T = dwarf_unit_type; long long unsigned int Bytes = 1]': ../../../../../binutils-gdb/gdb/dwarf2/read.h:181:74: required from here ../../../../../binutils-gdb/gdb/../gdbsupport/packed.h:41:40: error: static assertion failed 41 | gdb_static_assert (sizeof (packed) == Bytes); | ~~~~~~~~~~~~~~~~^~~~~~~~ ../../../../../binutils-gdb/gdb/../gdbsupport/gdb_assert.h:27:48: note: in definition of macro 'gdb_static_assert' 27 | #define gdb_static_assert(expr) static_assert (expr, "") | ^~~~ ../../../../../binutils-gdb/gdb/../gdbsupport/packed.h:41:40: note: the comparison reduces to '(4 == 1)' 41 | gdb_static_assert (sizeof (packed) == Bytes); | ~~~~~~~~~~~~~~~~^~~~~~~~ The issue is that mingw gcc defaults to "-mms-bitfields", which affects how bitfields are laid out. We can however tell GCC that we want the regular GCC layout instead using attribute gcc_struct. Attribute gcc_struct is not implemented in "clang -target x86_64-pc-windows-gnu", so that will need a different fix. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29373 Change-Id: I023315ee03622c59c397bf4affc0b68179c32374
105 lines
3.1 KiB
C++
105 lines
3.1 KiB
C++
/* Copyright (C) 2022 Free Software Foundation, Inc.
|
|
|
|
This file is part of GDB.
|
|
|
|
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 PACKED_H
|
|
#define PACKED_H
|
|
|
|
#include "traits.h"
|
|
|
|
/* Each instantiation and full specialization of the packed template
|
|
defines a type that behaves like a given scalar type, but that has
|
|
byte alignment, and, may optionally have a smaller size than the
|
|
given scalar type. This is typically used as alternative to
|
|
bit-fields (and ENUM_BITFIELD), when the fields must have separate
|
|
memory locations to avoid data races. */
|
|
|
|
/* We need gcc_struct on Windows GCC, as otherwise the size of e.g.,
|
|
"packed<int, 1>" will be larger than what we want. */
|
|
#if defined _WIN32
|
|
# define ATTRIBUTE_GCC_STRUCT __attribute__((__gcc_struct__))
|
|
#else
|
|
# define ATTRIBUTE_GCC_STRUCT
|
|
#endif
|
|
|
|
template<typename T, size_t Bytes = sizeof (T)>
|
|
struct ATTRIBUTE_GCC_STRUCT packed
|
|
{
|
|
public:
|
|
packed () noexcept = default;
|
|
|
|
packed (T val)
|
|
{
|
|
m_val = val;
|
|
|
|
/* Ensure size and aligment are what we expect. */
|
|
gdb_static_assert (sizeof (packed) == Bytes);
|
|
gdb_static_assert (alignof (packed) == 1);
|
|
|
|
/* Make sure packed can be wrapped with std::atomic. */
|
|
#if HAVE_IS_TRIVIALLY_COPYABLE
|
|
gdb_static_assert (std::is_trivially_copyable<packed>::value);
|
|
#endif
|
|
gdb_static_assert (std::is_copy_constructible<packed>::value);
|
|
gdb_static_assert (std::is_move_constructible<packed>::value);
|
|
gdb_static_assert (std::is_copy_assignable<packed>::value);
|
|
gdb_static_assert (std::is_move_assignable<packed>::value);
|
|
}
|
|
|
|
operator T () const noexcept
|
|
{
|
|
return m_val;
|
|
}
|
|
|
|
private:
|
|
T m_val : (Bytes * HOST_CHAR_BIT) ATTRIBUTE_PACKED;
|
|
};
|
|
|
|
/* Add some comparisons between std::atomic<packed<T>> and T. We need
|
|
this because the regular comparisons would require two implicit
|
|
conversions to go from T to std::atomic<packed<T>>:
|
|
|
|
T -> packed<T>
|
|
packed<T> -> std::atomic<packed<T>>
|
|
|
|
and C++ only does one. */
|
|
|
|
template<typename T, size_t Bytes>
|
|
bool operator== (T lhs, const std::atomic<packed<T, Bytes>> &rhs)
|
|
{
|
|
return lhs == rhs.load ();
|
|
}
|
|
|
|
template<typename T, size_t Bytes>
|
|
bool operator== (const std::atomic<packed<T, Bytes>> &lhs, T rhs)
|
|
{
|
|
return lhs.load () == rhs;
|
|
}
|
|
|
|
template<typename T, size_t Bytes>
|
|
bool operator!= (T lhs, const std::atomic<packed<T, Bytes>> &rhs)
|
|
{
|
|
return !(lhs == rhs);
|
|
}
|
|
|
|
template<typename T, size_t Bytes>
|
|
bool operator!= (const std::atomic<packed<T, Bytes>> &lhs, T rhs)
|
|
{
|
|
return !(lhs == rhs);
|
|
}
|
|
|
|
#endif
|