From 553242c18a2387fb896df66eb9b5a502f0e87ff0 Mon Sep 17 00:00:00 2001 From: Edmund Grimley Evans Date: Thu, 19 Nov 2015 18:21:14 +0000 Subject: Replace pointer casts with calls to (read|write)(16|32|64)le. This stops UBSan from giving runtime misaligned address errors and might eventually allow building on a non-little-endian host. --- tcc.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'tcc.h') diff --git a/tcc.h b/tcc.h index ff846fd..1be6be7 100644 --- a/tcc.h +++ b/tcc.h @@ -131,6 +131,51 @@ #include "stab.h" #include "libtcc.h" +static inline uint16_t read16le(unsigned char *p) +{ + return p[0] | (uint16_t)p[1] << 8; +} + +static inline void write16le(unsigned char *p, uint16_t x) +{ + p[0] = x & 255; + p[1] = x >> 8 & 255; +} + +static inline uint32_t read32le(unsigned char *p) +{ + return (p[0] | (uint32_t)p[1] << 8 | + (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24); +} + +static inline void write32le(unsigned char *p, uint32_t x) +{ + p[0] = x & 255; + p[1] = x >> 8 & 255; + p[2] = x >> 16 & 255; + p[3] = x >> 24 & 255; +} + +static inline uint64_t read64le(unsigned char *p) +{ + return (p[0] | (uint64_t)p[1] << 8 | + (uint64_t)p[2] << 16 | (uint64_t)p[3] << 24 | + (uint64_t)p[4] << 32 | (uint64_t)p[5] << 40 | + (uint64_t)p[6] << 48 | (uint64_t)p[7] << 56); +} + +static inline void write64le(unsigned char *p, uint64_t x) +{ + p[0] = x & 255; + p[1] = x >> 8 & 255; + p[2] = x >> 16 & 255; + p[3] = x >> 24 & 255; + p[4] = x >> 32 & 255; + p[5] = x >> 40 & 255; + p[6] = x >> 48 & 255; + p[7] = x >> 56 & 255; +} + /* parser debug */ /* #define PARSE_DEBUG */ /* preprocessor debug */ -- cgit v1.3.1