From 4d674102d0a576320a3485c3cb3313347e91f8dd Mon Sep 17 00:00:00 2001 From: Patryk Obara Date: Fri, 3 Jan 2020 22:40:39 +0100 Subject: [PATCH] Prevent unaligned memory access in adlib This removes the last warnings in this area; in this case changing endianess is not used for accessing emulated memory, just to flip few values to low endian for storage. --- INSTALL | 2 +- include/mem.h | 30 ++++++++++++++++++++++++++++++ src/hardware/adlib.cpp | 8 ++++---- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/INSTALL b/INSTALL index 2383bd86..94c5370b 100644 --- a/INSTALL +++ b/INSTALL @@ -50,7 +50,7 @@ alsa-lib (optional) If you want compile from developer sources (SVN) under a unix system, you will need: - Subversion to checkout the sources, or gzip and tar to unpack them from archive - - GCC (>=4.8.1) or Clang (>=3.3) + - GCC (>=4.8.1) or Clang (>=3.4) - automake (>=1.6) - autoconf (>=2.50) - autoconf-archive (>=2009.x) diff --git a/include/mem.h b/include/mem.h index 58155320..d1d52858 100644 --- a/include/mem.h +++ b/include/mem.h @@ -105,6 +105,36 @@ static INLINE void host_writed(HostPt off,Bit32u val) { #endif +// host_to_le functions allow for byte order conversion on big endian +// architectures while respecting memory alignment on low endian. +// +// It is extremely unlikely that we'll ever try to compile on big endian arch +// with a compiler missing __builtin_bswap*, so let's not overcomplicate +// things. +// +// __builtin_bswap* is supported since GCC 4.3 and Clang 3.4 + +#if defined(WORDS_BIGENDIAN) + +constexpr static INLINE uint16_t host_to_le(uint16_t val) { + return __builtin_bswap16(val); +} + +constexpr static INLINE uint32_t host_to_le(uint32_t val) { + return __builtin_bswap32(val); +} + +#else + +constexpr static INLINE uint16_t host_to_le(uint16_t val) { + return val; +} + +constexpr static INLINE uint32_t host_to_le(uint32_t val) { + return val; +} + +#endif static INLINE void var_write(Bit8u * var, Bit8u val) { host_writeb(var, val); diff --git a/src/hardware/adlib.cpp b/src/hardware/adlib.cpp index dca313df..1190100f 100644 --- a/src/hardware/adlib.cpp +++ b/src/hardware/adlib.cpp @@ -328,10 +328,10 @@ class Capture { if ( handle ) { ClearBuf(); /* Endianize the header and write it to beginning of the file */ - var_write( &header.versionHigh, header.versionHigh ); - var_write( &header.versionLow, header.versionLow ); - var_write( &header.commands, header.commands ); - var_write( &header.milliseconds, header.milliseconds ); + header.versionHigh = host_to_le(header.versionHigh); + header.versionLow = host_to_le(header.versionLow); + header.commands = host_to_le(header.commands); + header.milliseconds = host_to_le(header.milliseconds); fseek( handle, 0, SEEK_SET ); fwrite( &header, 1, sizeof( header ), handle ); fclose( handle );