diff mbox

[1/7] Add new macro QEMU_PACKED for packed C structures

Message ID 1314564200-6872-2-git-send-email-weil@mail.berlios.de
State Superseded
Headers show

Commit Message

Stefan Weil Aug. 28, 2011, 8:43 p.m. UTC
A packed struct needs different gcc attributes for compilations
with MinGW compilers because glib-2.0 adds compiler flag
-mms-bitfields which modifies the packing algorithm.

Attribute gcc_struct reverses the negative effects of -mms-bitfields.
QEMU_PACKED sets this attribute and must be used for any packed
struct which is affected by -mms-bitfields.

Signed-off-by: Stefan Weil <weil@mail.berlios.de>
---
 compiler.h |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

Comments

Andreas Färber Aug. 28, 2011, 8:47 p.m. UTC | #1
Am 28.08.2011 um 22:43 schrieb Stefan Weil:

> A packed struct needs different gcc attributes for compilations
> with MinGW compilers because glib-2.0 adds compiler flag
> -mms-bitfields which modifies the packing algorithm.

Is that algorithm actually needed anywhere? If not, is there no GCC  
option to fix this centrally in configure for win32 only?

Andreas

> Attribute gcc_struct reverses the negative effects of -mms-bitfields.
> QEMU_PACKED sets this attribute and must be used for any packed
> struct which is affected by -mms-bitfields.
>
> Signed-off-by: Stefan Weil <weil@mail.berlios.de>
> ---
> compiler.h |    6 ++++++
> 1 files changed, 6 insertions(+), 0 deletions(-)
>
> diff --git a/compiler.h b/compiler.h
> index 9af5dc6..a2d5959 100644
> --- a/compiler.h
> +++ b/compiler.h
> @@ -12,6 +12,12 @@
> #define QEMU_WARN_UNUSED_RESULT
> #endif
>
> +#if defined(_WIN32)
> +# define QEMU_PACKED __attribute__((gcc_struct, packed))
> +#else
> +# define QEMU_PACKED __attribute__((packed))
> +#endif
> +
> #define QEMU_BUILD_BUG_ON(x) \
>     typedef char qemu_build_bug_on__##__LINE__[(x)?-1:1];
>
> -- 
> 1.7.0.4
>
>
Stefan Weil Aug. 29, 2011, 5:12 a.m. UTC | #2
Am 28.08.2011 22:47, schrieb Andreas Färber:
> Am 28.08.2011 um 22:43 schrieb Stefan Weil:
>
>> A packed struct needs different gcc attributes for compilations
>> with MinGW compilers because glib-2.0 adds compiler flag
>> -mms-bitfields which modifies the packing algorithm.
>
> Is that algorithm actually needed anywhere? If not, is there no GCC 
> option to fix this centrally in configure for win32 only?
>
> Andreas

I think it is needed: glib-2.0 requires -mms-bitfields for MinGW which
breaks networking (and more) for w32 hosts because of changes
in struct alignment. A central solution would require removal of
-mms-bitfields or its effects. This is possible, but very risky,
because it will raise problems with glib-2.0 and w32 libraries.
Therefore a central solution was rejected.

See this thread:
http://lists.gnu.org/archive/html/qemu-devel/2011-08/msg01877.html

Stefan
Blue Swirl Aug. 30, 2011, 5:57 p.m. UTC | #3
On Sun, Aug 28, 2011 at 8:43 PM, Stefan Weil <weil@mail.berlios.de> wrote:
> A packed struct needs different gcc attributes for compilations
> with MinGW compilers because glib-2.0 adds compiler flag
> -mms-bitfields which modifies the packing algorithm.
>
> Attribute gcc_struct reverses the negative effects of -mms-bitfields.
> QEMU_PACKED sets this attribute and must be used for any packed
> struct which is affected by -mms-bitfields.
>
> Signed-off-by: Stefan Weil <weil@mail.berlios.de>
> ---
>  compiler.h |    6 ++++++
>  1 files changed, 6 insertions(+), 0 deletions(-)
>
> diff --git a/compiler.h b/compiler.h
> index 9af5dc6..a2d5959 100644
> --- a/compiler.h
> +++ b/compiler.h
> @@ -12,6 +12,12 @@
>  #define QEMU_WARN_UNUSED_RESULT
>  #endif
>
> +#if defined(_WIN32)
> +# define QEMU_PACKED __attribute__((gcc_struct, packed))

Maybe we could also use gcc_struct also for non-win32?

> +#else
> +# define QEMU_PACKED __attribute__((packed))
> +#endif
> +
>  #define QEMU_BUILD_BUG_ON(x) \
>     typedef char qemu_build_bug_on__##__LINE__[(x)?-1:1];
>
> --
> 1.7.0.4
>
>
>
Paolo Bonzini Aug. 30, 2011, 6:29 p.m. UTC | #4
On 08/30/2011 07:57 PM, Blue Swirl wrote:
>> >
>> >  +#if defined(_WIN32)
>> >  +# define QEMU_PACKED __attribute__((gcc_struct, packed))
> Maybe we could also use gcc_struct also for non-win32?
>
>> >  +#else
>> >  +# define QEMU_PACKED __attribute__((packed))
>> >  +#endif

Indeed.

Paolo
Stefan Weil Aug. 30, 2011, 8:17 p.m. UTC | #5
Am 30.08.2011 20:29, schrieb Paolo Bonzini:
> On 08/30/2011 07:57 PM, Blue Swirl wrote:
>>> >
>>> >  +#if defined(_WIN32)
>>> >  +# define QEMU_PACKED __attribute__((gcc_struct, packed))
>> Maybe we could also use gcc_struct also for non-win32?
>>
>>> >  +#else
>>> >  +# define QEMU_PACKED __attribute__((packed))
>>> >  +#endif
>
> Indeed.
>
> Paolo
>

No. Extract from gcc documentation:

"Two attributes are currently defined for i386 configurations: 
|ms_struct| and |gcc_struct"

For non i386 configuration, these configurations are undefined:

mipsel-linux-gnu-gcc -c -Wall test.c
test.c:3: warning: ‘gcc_struct’ attribute directive ignored

Therefore, we cannot use gcc_struct for most supported hosts.
Linux i386 or x86_64 would support it, but they don't need it...

We need one #if ... #else ... #endif to avoid 250 of them :-)

Stefan

|
diff mbox

Patch

diff --git a/compiler.h b/compiler.h
index 9af5dc6..a2d5959 100644
--- a/compiler.h
+++ b/compiler.h
@@ -12,6 +12,12 @@ 
 #define QEMU_WARN_UNUSED_RESULT
 #endif
 
+#if defined(_WIN32)
+# define QEMU_PACKED __attribute__((gcc_struct, packed))
+#else
+# define QEMU_PACKED __attribute__((packed))
+#endif
+
 #define QEMU_BUILD_BUG_ON(x) \
     typedef char qemu_build_bug_on__##__LINE__[(x)?-1:1];