Patchwork bswap: Don't rely on HOST_LONG_BITS

login
register
mail settings
Submitter Stefan Weil
Date Jan. 30, 2013, 9:56 p.m.
Message ID <51099715.8050602@weilnetz.de>
Download mbox | patch
Permalink /patch/216992/
State Under Review
Headers show

Comments

Stefan Weil - Jan. 30, 2013, 9:56 p.m.
Am 30.01.2013 22:04, schrieb Stefan Weil:
> Am 30.01.2013 21:55, schrieb Richard Henderson:
>> This is not always defined in all places qemu/bswap.h is used.
>> If we include qemu-common.h to get it, we create an include loop.
>> This resolves a build problem on any big-endian host like ppc64.
>>
>> Signed-off-by: Richard Henderson<rth@twiddle.net>
>> ---
>>   include/qemu/bswap.h | 12 ++++++++++--
>>   1 file changed, 10 insertions(+), 2 deletions(-)
>>
>> diff --git a/include/qemu/bswap.h b/include/qemu/bswap.h
>> index e6d4798..d3af35d 100644
>> --- a/include/qemu/bswap.h
>> +++ b/include/qemu/bswap.h
>> @@ -2,8 +2,8 @@
>>   #define BSWAP_H
>>
>>   #include "config-host.h"
>> -
>>   #include<inttypes.h>
>> +#include<limits.h>
>>   #include "fpu/softfloat.h"
>>
>>   #ifdef CONFIG_MACHINE_BSWAP_H
>> @@ -458,7 +458,15 @@ static inline void cpu_to_32wu(uint32_t *p, 
>> uint32_t v)
>>
>>   static inline unsigned long leul_to_cpu(unsigned long v)
>>   {
>> -    return le_bswap(v, HOST_LONG_BITS);
>> +    /* In order to break an include loop between here and
>> +       qemu-common.h, don't rely on HOST_LONG_BITS.  */
>> +#if ULONG_MAX == UINT32_MAX
>> +    return le_bswap(v, 32);
>> +#elif ULONG_MAX == UINT64_MAX
>> +    return le_bswap(v, 64);
>> +#else
>> +# error Unknown sizeof long
>> +#endif
>>   }
>>
>>   #undef le_bswap
>
> That would be wrong for 64 bit MinGW-w64 because
> HOST_LONG_BITS is _not_ the bit size of a long value.
>
> See qemu-common.h for the correct definition.
>
> Regards
> Stefan W.

Could you please try whether this patch fixes the build problems:


 From 4dd39af7a3e493669977ec6f50e91bf649b5727f Mon Sep 17 00:00:00 2001
From: Stefan Weil <sw@weilnetz.de>
Date: Wed, 30 Jan 2013 22:52:12 +0100
Subject: [PATCH] Fix build problems for big endian hosts

Make sure that HOST_LONG_BITS is always defined when qemu/bswap.h
is included.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
---
  fsdev/virtio-9p-marshal.c |   12 +-----------
  hw/spapr_nvram.c          |    2 ++
  include/qemu/bswap.h      |    4 ++++
  3 files changed, 7 insertions(+), 11 deletions(-)
Richard Henderson - Jan. 30, 2013, 10 p.m.
On 01/30/2013 01:56 PM, Stefan Weil wrote:
> Could you please try whether this patch fixes the build problems:
>
>
>  From 4dd39af7a3e493669977ec6f50e91bf649b5727f Mon Sep 17 00:00:00 2001
> From: Stefan Weil <sw@weilnetz.de>
> Date: Wed, 30 Jan 2013 22:52:12 +0100
> Subject: [PATCH] Fix build problems for big endian hosts
>
> Make sure that HOST_LONG_BITS is always defined when qemu/bswap.h
> is included.
>
> Signed-off-by: Stefan Weil <sw@weilnetz.de>

This probably works.  And from the list of includes removed, looks good 
from a long-term maintenance point of view.

I stand by my use of ULONG_MAX to match the unsigned long type though.


r~
Stefan Weil - Jan. 31, 2013, 6:17 a.m.
Am 30.01.2013 23:00, schrieb Richard Henderson:
> On 01/30/2013 01:56 PM, Stefan Weil wrote:
>> Could you please try whether this patch fixes the build problems:
>>
>>
>>  From 4dd39af7a3e493669977ec6f50e91bf649b5727f Mon Sep 17 00:00:00 2001
>> From: Stefan Weil <sw@weilnetz.de>
>> Date: Wed, 30 Jan 2013 22:52:12 +0100
>> Subject: [PATCH] Fix build problems for big endian hosts
>>
>> Make sure that HOST_LONG_BITS is always defined when qemu/bswap.h
>> is included.
>>
>> Signed-off-by: Stefan Weil <sw@weilnetz.de>
>
> This probably works.  And from the list of includes removed, looks
> good from a long-term maintenance point of view.
>
> I stand by my use of ULONG_MAX to match the unsigned long type though.
>
>
> r~
>

Andreas has pointed out that HOST_LONG_BITS was not correct in function
leul_to_cpu, so your patch fixes a real bug for MinGW-w64.

My patch only fixes the compilation.

In the long term HOST_LONG_BITS should be renamed to HOST_PTR_BITS
or HOST_POINTER_BITS to avoid future confusion and wrong use of this macro.

Stefan

Patch

diff --git a/fsdev/virtio-9p-marshal.c b/fsdev/virtio-9p-marshal.c
index 20f308b..4e634df 100644
--- a/fsdev/virtio-9p-marshal.c
+++ b/fsdev/virtio-9p-marshal.c
@@ -11,20 +11,10 @@ 
   *
   */

-#include <glib.h>
  #include <glib/gprintf.h>
-#include <sys/types.h>
-#include <dirent.h>
-#include <sys/time.h>
-#include <utime.h>
-#include <sys/uio.h>
-#include <string.h>
-#include <stdint.h>
-#include <errno.h>

-#include "qemu/compiler.h"
+#include "qemu-common.h"
  #include "virtio-9p-marshal.h"
-#include "qemu/bswap.h"

  void v9fs_string_free(V9fsString *str)
  {
diff --git a/hw/spapr_nvram.c b/hw/spapr_nvram.c
index 680cdba..2960b15 100644
--- a/hw/spapr_nvram.c
+++ b/hw/spapr_nvram.c
@@ -22,6 +22,8 @@ 
   * THE SOFTWARE.
   */

+#include "qemu-common.h"
+
  #include <libfdt.h>

  #include "sysemu/device_tree.h"
diff --git a/include/qemu/bswap.h b/include/qemu/bswap.h
index e6d4798..07dadc2 100644
--- a/include/qemu/bswap.h
+++ b/include/qemu/bswap.h
@@ -13,6 +13,10 @@ 
  #elif defined(CONFIG_BYTESWAP_H)
  # include <byteswap.h>

+#if !defined(HOST_LONG_BITS)
+# error needs HOST_LONG_BITS
+#endif
+
  static inline uint16_t bswap16(uint16_t x)
  {
      return bswap_16(x);