From d221187f24cbcc0e1c3c2559c96b39e40026abbd Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Fri, 22 Dec 2006 12:36:55 +0100 Subject: [PATCH] Misc cosmetical cleanups. --- src/bele.h | 86 +++++++++++++++++++++++++++++++++++++++-------- src/bele_policy.h | 20 +++++------ src/conf.h | 68 +++++++++++++++++++++++-------------- src/main.cpp | 36 ++++++++++++++------ src/p_armpe.cpp | 8 ++--- src/p_djgpp2.cpp | 12 +++---- src/p_elf.h | 20 +++++------ src/p_lx_elf.h | 18 ++++++++++ src/p_lx_exc.h | 9 +++++ src/p_mach.h | 3 +- src/p_ps1.cpp | 31 ++++++++--------- src/p_vmlinz.cpp | 16 ++++----- src/p_w32pe.cpp | 4 +-- src/packer.cpp | 4 +-- src/packer_c.cpp | 4 +-- src/pefile.cpp | 23 ++++++++----- 16 files changed, 245 insertions(+), 117 deletions(-) diff --git a/src/bele.h b/src/bele.h index caf5551e..cacfb78b 100644 --- a/src/bele.h +++ b/src/bele.h @@ -183,65 +183,65 @@ inline void set_le64(void *p, acc_uint64l_t v) // get signed values, i.e. sign-extend **************************************************************************/ -inline int sign_extend(int v, int bits) +inline int sign_extend(unsigned v, unsigned bits) { const unsigned sign_bit = 1u << (bits - 1); v |= 0u - (v & sign_bit); - return v; + return (int) v; } -inline acc_int64l_t sign_extend(acc_int64l_t v, int bits) +inline acc_int64l_t sign_extend(acc_uint64l_t v, unsigned bits) { const acc_uint64l_t sign_bit = ACC_UINT64_C(1) << (bits - 1); v |= ACC_UINT64_C(0) - (v & sign_bit); - return v; + return (acc_int64l_t) v; } inline int get_be16_signed(const void *p) { - int v = get_be16(p); + unsigned v = get_be16(p); return sign_extend(v, 16); } inline int get_be24_signed(const void *p) { - int v = get_be24(p); + unsigned v = get_be24(p); return sign_extend(v, 24); } inline int get_be32_signed(const void *p) { - int v = get_be32(p); + unsigned v = get_be32(p); return sign_extend(v, 32); } inline acc_int64l_t get_be64_signed(const void *p) { - acc_int64l_t v = get_be64(p); + acc_uint64l_t v = get_be64(p); return sign_extend(v, 64); } inline int get_le16_signed(const void *p) { - int v = get_le16(p); + unsigned v = get_le16(p); return sign_extend(v, 16); } inline int get_le24_signed(const void *p) { - int v = get_le24(p); + unsigned v = get_le24(p); return sign_extend(v, 24); } inline int get_le32_signed(const void *p) { - int v = get_le32(p); + unsigned v = get_le32(p); return sign_extend(v, 32); } inline acc_int64l_t get_le64_signed(const void *p) { - acc_int64l_t v = get_le64(p); + acc_uint64l_t v = get_le64(p); return sign_extend(v, 64); } @@ -304,12 +304,18 @@ inline void acc_ua_swab32s(void *p) // Important: these classes must be PODs (Plain Old Data), i.e. no // constructor, no destructor, no virtual functions and no default // assignment operator, and all fields must be public(!). +// +// [Actually we _can_ use a safe non-POD subset, but for this we need +// to have gcc bug 17519 fixed - see http://gcc.gnu.org/PR17519 ] **************************************************************************/ struct BE16 { unsigned char d[2]; + //inline BE16() { } + //BE16(unsigned v) { set_be16(d, v); } + BE16& operator = (unsigned v) { set_be16(d, v); return *this; } BE16& operator += (unsigned v) { set_be16(d, get_be16(d) + v); return *this; } BE16& operator -= (unsigned v) { set_be16(d, get_be16(d) - v); return *this; } @@ -330,6 +336,9 @@ struct BE32 { unsigned char d[4]; + //inline BE32() { } + //BE32(unsigned v) { set_be32(d, v); } + BE32& operator = (unsigned v) { set_be32(d, v); return *this; } BE32& operator += (unsigned v) { set_be32(d, get_be32(d) + v); return *this; } BE32& operator -= (unsigned v) { set_be32(d, get_be32(d) - v); return *this; } @@ -350,6 +359,9 @@ struct BE64 { unsigned char d[8]; + //inline BE64() { } + //BE64(acc_uint64l_t v) { set_be64(d, v); } + BE64& operator = (acc_uint64l_t v) { set_be64(d, v); return *this; } BE64& operator += (acc_uint64l_t v) { set_be64(d, get_be64(d) + v); return *this; } BE64& operator -= (acc_uint64l_t v) { set_be64(d, get_be64(d) - v); return *this; } @@ -370,6 +382,9 @@ struct LE16 { unsigned char d[2]; + //inline LE16() { } + //LE16(unsigned v) { set_le16(d, v); } + LE16& operator = (unsigned v) { set_le16(d, v); return *this; } LE16& operator += (unsigned v) { set_le16(d, get_le16(d) + v); return *this; } LE16& operator -= (unsigned v) { set_le16(d, get_le16(d) - v); return *this; } @@ -390,6 +405,9 @@ struct LE32 { unsigned char d[4]; + //inline LE32() { } + //LE32(unsigned v) { set_le32(d, v); } + LE32& operator = (unsigned v) { set_le32(d, v); return *this; } LE32& operator += (unsigned v) { set_le32(d, get_le32(d) + v); return *this; } LE32& operator -= (unsigned v) { set_le32(d, get_le32(d) - v); return *this; } @@ -410,6 +428,9 @@ struct LE64 { unsigned char d[8]; + //inline LE64() { } + //LE64(acc_uint64l_t v) { set_le64(d, v); } + LE64& operator = (acc_uint64l_t v) { set_le64(d, v); return *this; } LE64& operator += (acc_uint64l_t v) { set_le64(d, get_le64(d) + v); return *this; } LE64& operator -= (acc_uint64l_t v) { set_le64(d, get_le64(d) - v); return *this; } @@ -469,6 +490,41 @@ template T* operator + (const LE64& v, T* ptr); template T* operator - (T* ptr, const LE64& v); +/************************************************************************* +// global overloads +**************************************************************************/ + +#if 1 && !defined(ALIGN_DOWN) +inline unsigned ALIGN_DOWN(unsigned a, const LE32& b) { return ALIGN_DOWN(a, (unsigned) b); } +inline unsigned ALIGN_DOWN(const LE32& a, unsigned b) { return ALIGN_DOWN((unsigned) a, b); } +inline unsigned ALIGN_UP (unsigned a, const LE32& b) { return ALIGN_UP (a, (unsigned) b); } +inline unsigned ALIGN_UP (const LE32& a, unsigned b) { return ALIGN_UP ((unsigned) a, b); } +#endif + + +#if !defined(UPX_MAX) +inline unsigned UPX_MAX(unsigned a, const BE16& b) { return UPX_MAX(a, (unsigned) b); } +inline unsigned UPX_MAX(const BE16& a, unsigned b) { return UPX_MAX((unsigned) a, b); } +inline unsigned UPX_MIN(unsigned a, const BE16& b) { return UPX_MIN(a, (unsigned) b); } +inline unsigned UPX_MIN(const BE16& a, unsigned b) { return UPX_MIN((unsigned) a, b); } + +inline unsigned UPX_MAX(unsigned a, const BE32& b) { return UPX_MAX(a, (unsigned) b); } +inline unsigned UPX_MAX(const BE32& a, unsigned b) { return UPX_MAX((unsigned) a, b); } +inline unsigned UPX_MIN(unsigned a, const BE32& b) { return UPX_MIN(a, (unsigned) b); } +inline unsigned UPX_MIN(const BE32& a, unsigned b) { return UPX_MIN((unsigned) a, b); } + +inline unsigned UPX_MAX(unsigned a, const LE16& b) { return UPX_MAX(a, (unsigned) b); } +inline unsigned UPX_MAX(const LE16& a, unsigned b) { return UPX_MAX((unsigned) a, b); } +inline unsigned UPX_MIN(unsigned a, const LE16& b) { return UPX_MIN(a, (unsigned) b); } +inline unsigned UPX_MIN(const LE16& a, unsigned b) { return UPX_MIN((unsigned) a, b); } + +inline unsigned UPX_MAX(unsigned a, const LE32& b) { return UPX_MAX(a, (unsigned) b); } +inline unsigned UPX_MAX(const LE32& a, unsigned b) { return UPX_MAX((unsigned) a, b); } +inline unsigned UPX_MIN(unsigned a, const LE32& b) { return UPX_MIN(a, (unsigned) b); } +inline unsigned UPX_MIN(const LE32& a, unsigned b) { return UPX_MIN((unsigned) a, b); } +#endif + + /************************************************************************* // misc **************************************************************************/ @@ -565,9 +621,11 @@ namespace N_BELE_CTP { template static inline const N_BELE_RTP::AbstractPolicy* getRTP(); template <> -static inline const N_BELE_RTP::AbstractPolicy* getRTP() { return &N_BELE_RTP::be_policy; } +static inline const N_BELE_RTP::AbstractPolicy* getRTP() + { return &N_BELE_RTP::be_policy; } template <> -static inline const N_BELE_RTP::AbstractPolicy* getRTP() { return &N_BELE_RTP::le_policy; } +static inline const N_BELE_RTP::AbstractPolicy* getRTP() + { return &N_BELE_RTP::le_policy; } } diff --git a/src/bele_policy.h b/src/bele_policy.h index 771d554a..248aeccf 100644 --- a/src/bele_policy.h +++ b/src/bele_policy.h @@ -53,8 +53,8 @@ #if defined(BELE_RTP) struct AbstractPolicy { - AbstractPolicy() {} - virtual inline ~AbstractPolicy() {} + inline AbstractPolicy() { } + virtual inline ~AbstractPolicy() { } V bool isBE() C = 0; V bool isLE() C = 0; @@ -94,7 +94,7 @@ struct BEPolicy : public AbstractPolicy #endif { - BEPolicy() {} + inline BEPolicy() { } #if defined(BELE_CTP) typedef N_BELE_RTP::BEPolicy RTP_Policy; #elif defined(BELE_RTP) @@ -156,9 +156,9 @@ struct BEPolicy COMPILE_TIME_ASSERT(sizeof(U16) == 2) COMPILE_TIME_ASSERT(sizeof(U32) == 4) COMPILE_TIME_ASSERT(sizeof(U64) == 8) - COMPILE_TIME_ASSERT_ALIGNOF(U16, char) - COMPILE_TIME_ASSERT_ALIGNOF(U32, char) - COMPILE_TIME_ASSERT_ALIGNOF(U64, char) + COMPILE_TIME_ASSERT_ALIGNED1(U16) + COMPILE_TIME_ASSERT_ALIGNED1(U32) + COMPILE_TIME_ASSERT_ALIGNED1(U64) } // disable dynamic allocation @@ -171,7 +171,7 @@ struct LEPolicy : public AbstractPolicy #endif { - LEPolicy() {} + inline LEPolicy() { } #if defined(BELE_CTP) typedef N_BELE_RTP::LEPolicy RTP_Policy; #elif defined(BELE_RTP) @@ -233,9 +233,9 @@ struct LEPolicy COMPILE_TIME_ASSERT(sizeof(U16) == 2) COMPILE_TIME_ASSERT(sizeof(U32) == 4) COMPILE_TIME_ASSERT(sizeof(U64) == 8) - COMPILE_TIME_ASSERT_ALIGNOF(U16, char) - COMPILE_TIME_ASSERT_ALIGNOF(U32, char) - COMPILE_TIME_ASSERT_ALIGNOF(U64, char) + COMPILE_TIME_ASSERT_ALIGNED1(U16) + COMPILE_TIME_ASSERT_ALIGNED1(U32) + COMPILE_TIME_ASSERT_ALIGNED1(U64) } // disable dynamic allocation diff --git a/src/conf.h b/src/conf.h index 1a9d4a77..93b37ea3 100644 --- a/src/conf.h +++ b/src/conf.h @@ -290,18 +290,6 @@ #undef PAGE_MASK #undef PAGE_SIZE - -#undef __attribute_packed -#if (ACC_CC_GNUC || ACC_CC_INTELC || ACC_CC_PATHSCALE) -# if (1 && (ACC_ARCH_I386)) -# define __attribute_packed -# else -# define __attribute_packed __attribute__((__packed__,__aligned__(1))) -# endif -#else -# define __attribute_packed -#endif - #if !defined(O_BINARY) # define O_BINARY 0 #endif @@ -311,6 +299,19 @@ #endif +#undef __attribute_packed +#if (ACC_CC_GNUC || ACC_CC_INTELC || ACC_CC_PATHSCALE) +# if (0 && (ACC_ARCH_AMD64 || ACC_ARCH_I386)) +# define __attribute_packed +# else +# define __attribute_packed __attribute__((__packed__,__aligned__(1))) +# endif +#endif +#if !defined(__attribute_packed) +# define __attribute_packed +#endif + + /************************************************************************* // **************************************************************************/ @@ -318,36 +319,53 @@ #define UNUSED(var) ACC_UNUSED(var) #define COMPILE_TIME_ASSERT(e) ACC_COMPILE_TIME_ASSERT(e) -#if 1 -# define __COMPILE_TIME_ASSERT_ALIGNOF_SIZEOF(a,b) { \ +#define __COMPILE_TIME_ASSERT_ALIGNOF_SIZEOF(a,b) { \ typedef a acc_tmp_a_t; typedef b acc_tmp_b_t; \ - struct acc_tmp_t { acc_tmp_b_t x; acc_tmp_a_t y; acc_tmp_b_t z[7]; } __attribute_packed; \ - COMPILE_TIME_ASSERT(sizeof(struct acc_tmp_t) == 8*sizeof(b)+sizeof(a)) \ + struct acc_tmp_t { acc_tmp_b_t x; acc_tmp_a_t y; acc_tmp_b_t z; } __attribute_packed; \ + COMPILE_TIME_ASSERT(sizeof(struct acc_tmp_t) == 2*sizeof(b)+sizeof(a)) \ } -#else -# define __COMPILE_TIME_ASSERT_ALIGNOF_SIZEOF(a,b) { \ - struct acc_tmp_t { b x; a y; b z[7]; } __attribute_packed; \ - COMPILE_TIME_ASSERT(sizeof(struct acc_tmp_t) == 8*sizeof(b)+sizeof(a)) \ - } -#endif - #if defined(__acc_alignof) -# define COMPILE_TIME_ASSERT_ALIGNOF(a,b) \ +# define __COMPILE_TIME_ASSERT_ALIGNOF(a,b) \ __COMPILE_TIME_ASSERT_ALIGNOF_SIZEOF(a,b) \ COMPILE_TIME_ASSERT(__acc_alignof(a) == sizeof(b)) #else -# define COMPILE_TIME_ASSERT_ALIGNOF(a,b) \ +# define __COMPILE_TIME_ASSERT_ALIGNOF(a,b) \ __COMPILE_TIME_ASSERT_ALIGNOF_SIZEOF(a,b) #endif +#define COMPILE_TIME_ASSERT_ALIGNED1(a) __COMPILE_TIME_ASSERT_ALIGNOF(a,char) #define TABLESIZE(table) ((sizeof(table)/sizeof((table)[0]))) + +#if 0 #define ALIGN_DOWN(a,b) (((a) / (b)) * (b)) #define ALIGN_UP(a,b) ALIGN_DOWN((a) + ((b) - 1), b) #define ALIGN_GAP(a,b) (ALIGN_UP(a,b) - (a)) +#elif 1 +template +inline T ALIGN_DOWN(const T& a, const T& b) { T r; r = (a / b) * b; return r; } +template +inline T ALIGN_UP (const T& a, const T& b) { T r; r = ((a + b - 1) / b) * b; return r; } +template +inline T ALIGN_GAP (const T& a, const T& b) { T r; r = ALIGN_UP(a, b) - a; return r; } +#else +inline unsigned ALIGN_DOWN(unsigned a, unsigned b) { return (a / b) * b; } +inline unsigned ALIGN_UP (unsigned a, unsigned b) { return ((a + b - 1) / b) * b; } +inline unsigned ALIGN_GAP (unsigned a, unsigned b) { return ALIGN_UP(a, b) - a; } +#endif +#if 0 #define UPX_MAX(a,b) ((a) >= (b) ? (a) : (b)) #define UPX_MIN(a,b) ((a) <= (b) ? (a) : (b)) +#elif 1 +template +inline const T& UPX_MAX(const T& a, const T& b) { if (a < b) return b; return a; } +template +inline const T& UPX_MIN(const T& a, const T& b) { if (a < b) return a; return b; } +#else +inline unsigned UPX_MAX(unsigned a, unsigned b) { return a < b ? b : a; } +inline unsigned UPX_MIN(unsigned a, unsigned b) { return a < b ? a : b; } +#endif // An Array allocates memory on the heap, but automatically diff --git a/src/main.cpp b/src/main.cpp index cd556c27..a3b66d0a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1210,9 +1210,25 @@ static void first_options(int argc, char **argv) **************************************************************************/ template struct TestBELE { -static int test(void) +static bool test(void) { + COMPILE_TIME_ASSERT_ALIGNED1(T) + struct test1_t { char a; T b; } __attribute_packed; + struct test2_t { char a; T b[3]; } __attribute_packed; + test1_t t1[7]; UNUSED(t1); test2_t t2[7]; UNUSED(t2); + COMPILE_TIME_ASSERT(sizeof(test1_t) == 1 + sizeof(T)) + COMPILE_TIME_ASSERT_ALIGNED1(test1_t) + COMPILE_TIME_ASSERT(sizeof(t1) == 7 + 7*sizeof(T)) + COMPILE_TIME_ASSERT(sizeof(test2_t) == 1 + 3*sizeof(T)) + COMPILE_TIME_ASSERT_ALIGNED1(test2_t) + COMPILE_TIME_ASSERT(sizeof(t2) == 7 + 21*sizeof(T)) +#if defined(__acc_alignof) + COMPILE_TIME_ASSERT(__acc_alignof(t1) == 1) + COMPILE_TIME_ASSERT(__acc_alignof(t2) == 1) +#endif +#if 1 && !defined(xUPX_OFFICIAL_BUILD) T allbits; allbits = 0; allbits -= 1; + //++allbits; allbits++; --allbits; allbits--; T v1; v1 = 1; v1 *= 2; v1 -= 1; T v2; v2 = 1; assert( (v1 == v2)); assert(!(v1 != v2)); @@ -1226,7 +1242,9 @@ static int test(void) assert(v1 == 1); assert(v2 == 0); v1 <<= 1; v1 |= v2; v1 >>= 1; v2 &= v1; v2 /= v1; v2 *= v1; assert(v1 == 1); assert(v2 == 0); - return (v1 ^ v2) == 1; + if ((v1 ^ v2) != 1) return false; +#endif + return true; }}; @@ -1258,12 +1276,12 @@ void upx_sanity_check(void) COMPILE_TIME_ASSERT(sizeof(LE32) == 4) COMPILE_TIME_ASSERT(sizeof(LE64) == 8) - COMPILE_TIME_ASSERT_ALIGNOF(BE16, char) - COMPILE_TIME_ASSERT_ALIGNOF(BE32, char) - COMPILE_TIME_ASSERT_ALIGNOF(BE64, char) - COMPILE_TIME_ASSERT_ALIGNOF(LE16, char) - COMPILE_TIME_ASSERT_ALIGNOF(LE32, char) - COMPILE_TIME_ASSERT_ALIGNOF(LE64, char) + COMPILE_TIME_ASSERT_ALIGNED1(BE16) + COMPILE_TIME_ASSERT_ALIGNED1(BE32) + COMPILE_TIME_ASSERT_ALIGNED1(BE64) + COMPILE_TIME_ASSERT_ALIGNED1(LE16) + COMPILE_TIME_ASSERT_ALIGNED1(LE32) + COMPILE_TIME_ASSERT_ALIGNED1(LE64) COMPILE_TIME_ASSERT(sizeof(UPX_VERSION_STRING4) == 4 + 1) assert(strlen(UPX_VERSION_STRING4) == 4); @@ -1274,14 +1292,12 @@ void upx_sanity_check(void) assert(memcmp(UPX_VERSION_DATE + strlen(UPX_VERSION_DATE) - 4, UPX_VERSION_YEAR, 4) == 0); #if 1 -# if 1 && !defined(UPX_OFFICIAL_BUILD) assert(TestBELE::test()); assert(TestBELE::test()); assert(TestBELE::test()); assert(TestBELE::test()); assert(TestBELE::test()); assert(TestBELE::test()); -# endif { static const unsigned char dd[32] = { 0, 0, 0, 0, 0, 0, 0, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, diff --git a/src/p_armpe.cpp b/src/p_armpe.cpp index f86f669c..1461d476 100644 --- a/src/p_armpe.cpp +++ b/src/p_armpe.cpp @@ -291,7 +291,7 @@ unsigned PackArmPe::processImports() // pass 1 unsigned k32namepos = ptr_diff(dllnames,oimpdlls); - memcpy(importednames, llgpa, ALIGN_UP(sizeof(llgpa), 2)); + memcpy(importednames, llgpa, ALIGN_UP((unsigned) sizeof(llgpa), 2u)); strcpy(dllnames,kernel32dll); im->dllname = k32namepos; im->iat = ptr_diff(ordinals,oimpdlls); @@ -439,7 +439,7 @@ unsigned PackArmPe::processImports() // pass 1 void PackArmPe::processTls(Interval *) // pass 1 { - if ((sotls = ALIGN_UP(IDSIZE(PEDIR_TLS),4)) == 0) + if ((sotls = ALIGN_UP(IDSIZE(PEDIR_TLS),4u)) == 0) return; // never should happen on wince @@ -745,7 +745,7 @@ void PackArmPe::pack(OutputFile *fo) const unsigned c_len = ((ph.c_len + ic) & 15) == 0 ? ph.c_len : ph.c_len + 16 - ((ph.c_len + ic) & 15); obuf.clear(ph.c_len, c_len - ph.c_len); - const unsigned s1size = ALIGN_UP(ic + c_len + codesize,4) + sotls; + const unsigned s1size = ALIGN_UP(ic + c_len + codesize,4u) + sotls; const unsigned s1addr = (newvsize - (ic + c_len) + oam1) &~ oam1; const unsigned ncsection = (s1addr + s1size + oam1) &~ oam1; @@ -1057,7 +1057,7 @@ void PackArmPe::rebuildImports(upx_byte *& extrainfo) else p += 5; } - sdllnames = ALIGN_UP(sdllnames,2); + sdllnames = ALIGN_UP(sdllnames,2u); upx_byte * const Obuf = obuf - rvamin; import_desc * const im0 = (import_desc*) (Obuf + ODADDR(PEDIR_IMPORT)); diff --git a/src/p_djgpp2.cpp b/src/p_djgpp2.cpp index 5b646bfa..2b3e4149 100644 --- a/src/p_djgpp2.cpp +++ b/src/p_djgpp2.cpp @@ -47,11 +47,11 @@ PackDjgpp2::PackDjgpp2(InputFile *f) : super(f), coff_offset(0) { bele = &N_BELE_RTP::le_policy; - COMPILE_TIME_ASSERT(sizeof(external_scnhdr_t) == 40); - COMPILE_TIME_ASSERT(sizeof(coff_header_t) == 0xa8); - COMPILE_TIME_ASSERT_ALIGNOF(external_scnhdr_t, char) - COMPILE_TIME_ASSERT_ALIGNOF(coff_header_t, char) - COMPILE_TIME_ASSERT(sizeof(stub_i386_dos32_djgpp2_stubify) == 2048); + COMPILE_TIME_ASSERT(sizeof(external_scnhdr_t) == 40) + COMPILE_TIME_ASSERT(sizeof(coff_header_t) == 0xa8) + COMPILE_TIME_ASSERT_ALIGNED1(external_scnhdr_t) + COMPILE_TIME_ASSERT_ALIGNED1(coff_header_t) + COMPILE_TIME_ASSERT(sizeof(stub_i386_dos32_djgpp2_stubify) == 2048) COMPILE_TIME_ASSERT(STUB_I386_DOS32_DJGPP2_STUBIFY_ADLER32 == 0xbf689ba8) COMPILE_TIME_ASSERT(STUB_I386_DOS32_DJGPP2_STUBIFY_CRC32 == 0x2ae982b2) //printf("0x%08x\n", upx_adler32(stubify_stub, sizeof(stubify_stub))); @@ -334,7 +334,7 @@ void PackDjgpp2::pack(OutputFile *fo) data->size = ph.c_len; // new size of .data unsigned stack = 1024 + ph.overlap_overhead + getDecompressorWrkmemSize(); - stack = ALIGN_UP(stack, 16); + stack = ALIGN_UP(stack, 16u); if (bss->size < stack) // give it a .bss bss->size = stack; diff --git a/src/p_elf.h b/src/p_elf.h index ef54064e..d6b49120 100644 --- a/src/p_elf.h +++ b/src/p_elf.h @@ -290,11 +290,11 @@ struct ElfClass_32 COMPILE_TIME_ASSERT(sizeof(Shdr) == 40) COMPILE_TIME_ASSERT(sizeof(Dyn) == 8) COMPILE_TIME_ASSERT(sizeof(Sym) == 16) - COMPILE_TIME_ASSERT_ALIGNOF(Ehdr, char) - COMPILE_TIME_ASSERT_ALIGNOF(Phdr, char) - COMPILE_TIME_ASSERT_ALIGNOF(Shdr, char) - COMPILE_TIME_ASSERT_ALIGNOF(Dyn, char) - COMPILE_TIME_ASSERT_ALIGNOF(Sym, char) + COMPILE_TIME_ASSERT_ALIGNED1(Ehdr) + COMPILE_TIME_ASSERT_ALIGNED1(Phdr) + COMPILE_TIME_ASSERT_ALIGNED1(Shdr) + COMPILE_TIME_ASSERT_ALIGNED1(Dyn) + COMPILE_TIME_ASSERT_ALIGNED1(Sym) } }; @@ -324,11 +324,11 @@ struct ElfClass_64 COMPILE_TIME_ASSERT(sizeof(Shdr) == 64) COMPILE_TIME_ASSERT(sizeof(Dyn) == 16) COMPILE_TIME_ASSERT(sizeof(Sym) == 24) - COMPILE_TIME_ASSERT_ALIGNOF(Ehdr, char) - COMPILE_TIME_ASSERT_ALIGNOF(Phdr, char) - COMPILE_TIME_ASSERT_ALIGNOF(Shdr, char) - COMPILE_TIME_ASSERT_ALIGNOF(Dyn, char) - COMPILE_TIME_ASSERT_ALIGNOF(Sym, char) + COMPILE_TIME_ASSERT_ALIGNED1(Ehdr) + COMPILE_TIME_ASSERT_ALIGNED1(Phdr) + COMPILE_TIME_ASSERT_ALIGNED1(Shdr) + COMPILE_TIME_ASSERT_ALIGNED1(Dyn) + COMPILE_TIME_ASSERT_ALIGNED1(Sym) } }; diff --git a/src/p_lx_elf.h b/src/p_lx_elf.h index 74f88a67..aeabe94d 100644 --- a/src/p_lx_elf.h +++ b/src/p_lx_elf.h @@ -161,6 +161,15 @@ protected: char text[0x18 - 4*4]; // "OpenBSD" unsigned end; // 0 } elfnote; + + static void compileTimeAssertions() { + COMPILE_TIME_ASSERT(sizeof(cprElfHdr1) == 52 + 1*32 + 12) + COMPILE_TIME_ASSERT(sizeof(cprElfHdr2) == 52 + 2*32 + 12) + COMPILE_TIME_ASSERT(sizeof(cprElfHdr3) == 52 + 3*32 + 12) + COMPILE_TIME_ASSERT_ALIGNED1(cprElfHdr1) + COMPILE_TIME_ASSERT_ALIGNED1(cprElfHdr2) + COMPILE_TIME_ASSERT_ALIGNED1(cprElfHdr3) + } }; @@ -225,6 +234,15 @@ protected: __attribute_packed; cprElfHdr3 elfout; + + static void compileTimeAssertions() { + COMPILE_TIME_ASSERT(sizeof(cprElfHdr1) == 64 + 1*56 + 12) + COMPILE_TIME_ASSERT(sizeof(cprElfHdr2) == 64 + 2*56 + 12) + COMPILE_TIME_ASSERT(sizeof(cprElfHdr3) == 64 + 3*56 + 12) + COMPILE_TIME_ASSERT_ALIGNED1(cprElfHdr1) + COMPILE_TIME_ASSERT_ALIGNED1(cprElfHdr2) + COMPILE_TIME_ASSERT_ALIGNED1(cprElfHdr3) + } }; class PackLinuxElf32Be : public PackLinuxElf32 diff --git a/src/p_lx_exc.h b/src/p_lx_exc.h index a27b04ee..ea9de2ee 100644 --- a/src/p_lx_exc.h +++ b/src/p_lx_exc.h @@ -120,6 +120,15 @@ protected: unsigned char ei_osabi; char const *osabi_note; + + static void compileTimeAssertions() { + COMPILE_TIME_ASSERT(sizeof(cprElfHdr1) == 52 + 1*32 + 12) + COMPILE_TIME_ASSERT(sizeof(cprElfHdr2) == 52 + 2*32 + 12) + COMPILE_TIME_ASSERT(sizeof(cprElfHdr3) == 52 + 3*32 + 12) + COMPILE_TIME_ASSERT_ALIGNED1(cprElfHdr1) + COMPILE_TIME_ASSERT_ALIGNED1(cprElfHdr2) + COMPILE_TIME_ASSERT_ALIGNED1(cprElfHdr3) + } }; diff --git a/src/p_mach.h b/src/p_mach.h index 9281c5bd..be5692ab 100644 --- a/src/p_mach.h +++ b/src/p_mach.h @@ -135,7 +135,8 @@ struct Mach_ppc_thread_state { BE32 mq; /* MQ register (601 only) */ BE32 vrsave; /* Vector Save Register */ -}; +} +__attribute_packed; struct Mach_thread_command { BE32 cmd; /* LC_THREAD or LC_UNIXTHREAD */ diff --git a/src/p_ps1.cpp b/src/p_ps1.cpp index 78de9813..a5cb9132 100644 --- a/src/p_ps1.cpp +++ b/src/p_ps1.cpp @@ -83,16 +83,17 @@ PackPs1::PackPs1(InputFile *f) : pad_code(0), bss_start(0), bss_end(0) { bele = &N_BELE_RTP::le_policy; - COMPILE_TIME_ASSERT(sizeof(ps1_exe_t) == 136); - COMPILE_TIME_ASSERT(sizeof(ps1_exe_hb_t) == 44); - COMPILE_TIME_ASSERT(sizeof(ps1_exe_chb_t) == 5); - COMPILE_TIME_ASSERT(PS_HDR_SIZE > sizeof(ps1_exe_t)); - COMPILE_TIME_ASSERT(SZ_IH_BKUP == 40); -#if 0 // 1 || defined(WITH_NRV) - COMPILE_TIME_ASSERT(sizeof(nrv_loader) == 14812); - COMPILE_TIME_ASSERT(NRV_BOOT_LOADER_CRC32 == 0x0); -#endif + COMPILE_TIME_ASSERT(sizeof(ps1_exe_t) == 136) + COMPILE_TIME_ASSERT(sizeof(ps1_exe_hb_t) == 44) + COMPILE_TIME_ASSERT(sizeof(ps1_exe_chb_t) == 5) + COMPILE_TIME_ASSERT_ALIGNED1(ps1_exe_t) + COMPILE_TIME_ASSERT_ALIGNED1(ps1_exe_hb_t) + COMPILE_TIME_ASSERT_ALIGNED1(ps1_exe_chb_t) + + COMPILE_TIME_ASSERT(PS_HDR_SIZE > sizeof(ps1_exe_t)) + COMPILE_TIME_ASSERT(SZ_IH_BKUP == 40) + fdata_size = file_size - PS_HDR_SIZE; ram_size = !opt->ps1_exe.do_8mb ? 0x200000 : 0x800000; } @@ -169,7 +170,7 @@ void PackPs1::putBkupHeader(const unsigned char *src, unsigned char *dst, unsign if (r != UPX_E_OK || sz_cbh >= SZ_IH_BKUP) throwInternalError("header compression failed"); INIT_BH_BKUP(p, sz_cbh); - *len = ALIGN_UP(sz_cbh + sizeof(ps1_exe_chb_t) - 1, 4); + *len = ALIGN_UP(sz_cbh + (unsigned) sizeof(ps1_exe_chb_t) - 1, 4u); p->ih_csum = ADLER16(upx_adler32(&ih.epc, SZ_IH_BKUP)); memcpy(dst, cpr_bh, SZ_IH_BKUP); delete [] cpr_bh; @@ -314,7 +315,7 @@ void PackPs1::buildLoader(const Filter *) throwCantPack("packed data overlap (try --force)"); } else - sa_tmp += overlap = ALIGN_UP((ph.overlap_overhead - sa_tmp),4); + sa_tmp += overlap = ALIGN_UP((ph.overlap_overhead - sa_tmp), 4u); } foundBss = findBssSection(); @@ -345,7 +346,7 @@ void PackPs1::buildLoader(const Filter *) else initLoader(stub_mipsel_r3000_ps1, sizeof(stub_mipsel_r3000_ps1)); - pad_code = ALIGN_GAP((ph.c_len + (isCon ? sz_lcpr : 0)), 4); + pad_code = ALIGN_GAP((ph.c_len + (isCon ? sz_lcpr : 0)), 4u); assert(pad_code < 4); static const unsigned char pad_buffer[4] = { 0, 0, 0, 0 }; linker->addSection("pad.code", pad_buffer, pad_code, 0); @@ -439,7 +440,7 @@ bool PackPs1::findBssSection() bss_start = MIPS_IMM(p->hi1, p->lo1); bss_end = MIPS_IMM(p->hi2, p->lo2); - if (0 < ALIGN_DOWN(bss_end - bss_start, 4) ) + if (0 < ALIGN_DOWN(bss_end - bss_start, 4u)) { unsigned wkmem_sz = M_IS_LZMA(ph.method) ? 32768 : 800; unsigned end_offs = ih.tx_ptr + fdata_size + overlap; @@ -478,9 +479,9 @@ void PackPs1::pack(OutputFile *fo) while (!(*--p_scan)) { if (sa_cnt++ > (0x10000 << 5) || sa_cnt >= fdata_size - 1024) break; } if (sa_cnt > (0x10000 << 2)) - sa_cnt = ALIGN_DOWN(sa_cnt,32); + sa_cnt = ALIGN_DOWN(sa_cnt, 32u); else - sa_cnt = ALIGN_DOWN(sa_cnt,4); + sa_cnt = ALIGN_DOWN(sa_cnt, 4u); // prepare packheader ph.u_len = (fdata_size - sa_cnt); diff --git a/src/p_vmlinz.cpp b/src/p_vmlinz.cpp index 18408187..4a1e1cf5 100644 --- a/src/p_vmlinz.cpp +++ b/src/p_vmlinz.cpp @@ -100,7 +100,7 @@ int PackVmlinuzI386::readFileHeader() return 0; int format = UPX_F_VMLINUZ_i386; - unsigned sys_size = ALIGN_UP(file_size, 16) - setup_size; + unsigned sys_size = ALIGN_UP((unsigned) file_size, 16u) - setup_size; const unsigned char *p = (const unsigned char *) &h + 0x1e3; @@ -143,7 +143,7 @@ int PackVmlinuzI386::decompressKernel() break; } - checkAlreadyPacked(obuf + setup_size, UPX_MIN(file_size - setup_size, 1024)); + checkAlreadyPacked(obuf + setup_size, UPX_MIN(file_size - setup_size, (off_t)1024)); for (int gzoff = setup_size; gzoff < file_size; gzoff++) { @@ -333,7 +333,7 @@ void PackVmlinuzI386::pack(OutputFile *fo) patchPackHeader(loader, lsize); boot_sect_t * const bs = (boot_sect_t *) ((unsigned char *) setup_buf); - bs->sys_size = ALIGN_UP(lsize + ph.c_len, 16) / 16; + bs->sys_size = ALIGN_UP(lsize + ph.c_len, 16u) / 16; fo->write(setup_buf, setup_buf.getSize()); fo->write(loader, lsize); @@ -408,7 +408,7 @@ void PackBvmlinuzI386::pack(OutputFile *fo) // align everything to dword boundary - it is easier to handle unsigned c_len = ph.c_len; memset(obuf + c_len, 0, 4); - c_len = ALIGN_UP(c_len, 4); + c_len = ALIGN_UP(c_len, 4u); const unsigned lsize = getLoaderSize(); @@ -431,11 +431,11 @@ void PackBvmlinuzI386::pack(OutputFile *fo) const int e_len = getLoaderSectionStart("LZCUTPOI"); assert(e_len > 0); - const unsigned d_len4 = ALIGN_UP(lsize - e_len, 4); - const unsigned decompr_pos = ALIGN_UP(ph.u_len + ph.overlap_overhead, 16); + const unsigned d_len4 = ALIGN_UP(lsize - e_len, 4u); + const unsigned decompr_pos = ALIGN_UP(ph.u_len + ph.overlap_overhead, 16u); const unsigned copy_size = c_len + d_len4; const unsigned edi = decompr_pos + d_len4 - 4; // copy to - const unsigned esi = ALIGN_UP(c_len + lsize, 4) - 4; // copy from + const unsigned esi = ALIGN_UP(c_len + lsize, 4u) - 4; // copy from linker->defineSymbol("decompressor", decompr_pos - bzimage_offset + physical_start); linker->defineSymbol("src_for_decompressor", physical_start + decompr_pos - c_len); @@ -457,7 +457,7 @@ void PackBvmlinuzI386::pack(OutputFile *fo) patchPackHeader(loader, lsize); boot_sect_t * const bs = (boot_sect_t *) ((unsigned char *) setup_buf); - bs->sys_size = (ALIGN_UP(lsize + c_len, 16) / 16) & 0xffff; + bs->sys_size = (ALIGN_UP(lsize + c_len, 16u) / 16) & 0xffff; fo->write(setup_buf, setup_buf.getSize()); fo->write(loader, e_len); diff --git a/src/p_w32pe.cpp b/src/p_w32pe.cpp index 9219d1ee..203e8b0d 100644 --- a/src/p_w32pe.cpp +++ b/src/p_w32pe.cpp @@ -840,7 +840,7 @@ void PackW32Pe::pack(OutputFile *fo) const unsigned c_len = ((ph.c_len + ic) & 15) == 0 ? ph.c_len : ph.c_len + 16 - ((ph.c_len + ic) & 15); obuf.clear(ph.c_len, c_len - ph.c_len); - const unsigned s1size = ALIGN_UP(ic + c_len + codesize,4) + sotls + soloadconf; + const unsigned s1size = ALIGN_UP(ic + c_len + codesize,4u) + sotls + soloadconf; const unsigned s1addr = (newvsize - (ic + c_len) + oam1) &~ oam1; const unsigned ncsection = (s1addr + s1size + oam1) &~ oam1; @@ -1197,7 +1197,7 @@ void PackW32Pe::rebuildImports(upx_byte *& extrainfo) else p += 5; } - sdllnames = ALIGN_UP(sdllnames,2); + sdllnames = ALIGN_UP(sdllnames, 2u); upx_byte * const Obuf = obuf - rvamin; import_desc * const im0 = (import_desc*) (Obuf + ODADDR(PEDIR_IMPORT)); diff --git a/src/packer.cpp b/src/packer.cpp index d9f5c4cc..c5ac71fb 100644 --- a/src/packer.cpp +++ b/src/packer.cpp @@ -483,7 +483,7 @@ unsigned Packer::findOverlapOverhead(const upx_bytep buf, unsigned low = 1; unsigned high = UPX_MIN(ph.u_len / 4 + 512, upper_limit); // but be optimistic for first try (speedup) - unsigned m = UPX_MIN(16, high); + unsigned m = UPX_MIN(16u, high); // unsigned overhead = 0; unsigned nr = 0; // statistics @@ -578,7 +578,7 @@ void Packer::copyOverlay(OutputFile *fo, unsigned overlay, // get buffer size, align to improve i/o speed unsigned buf_size = buf->getSize(); if (buf_size > 65536) - buf_size = ALIGN_DOWN(buf_size, 4096); + buf_size = ALIGN_DOWN(buf_size, 4096u); assert((int)buf_size > 0); do { diff --git a/src/packer_c.cpp b/src/packer_c.cpp index 3030ef39..6a986bd1 100644 --- a/src/packer_c.cpp +++ b/src/packer_c.cpp @@ -231,8 +231,8 @@ unsigned Packer::getDecompressorWrkmemSize() const { const lzma_compress_result_t *res = &ph.compress_result.result_lzma; // FIXME - this is for i386 only - size = 8 + 4 + ALIGN_UP(2 * res->num_probs, 4); - size = ALIGN_UP(size, 16); + size = 8 + 4 + ALIGN_UP(2 * res->num_probs, 4u); + size = ALIGN_UP(size, 16u); } assert((int)size >= 0); return size; diff --git a/src/pefile.cpp b/src/pefile.cpp index 7211041f..33e32ee4 100644 --- a/src/pefile.cpp +++ b/src/pefile.cpp @@ -112,8 +112,12 @@ PeFile::PeFile(InputFile *f) : super(f) bele = &N_BELE_RTP::le_policy; //printf("pe_header_t %d\n", (int) sizeof(pe_header_t)); //printf("pe_section_t %d\n", (int) sizeof(pe_section_t)); - COMPILE_TIME_ASSERT(sizeof(pe_header_t) == 248); - COMPILE_TIME_ASSERT(sizeof(pe_section_t) == 40); + COMPILE_TIME_ASSERT(sizeof(pe_header_t) == 248) + COMPILE_TIME_ASSERT(sizeof(pe_header_t::ddirs_t) == 8) + COMPILE_TIME_ASSERT(sizeof(pe_section_t) == 40) + COMPILE_TIME_ASSERT_ALIGNED1(pe_header_t) + COMPILE_TIME_ASSERT_ALIGNED1(pe_header_t::ddirs_t) + COMPILE_TIME_ASSERT_ALIGNED1(pe_section_t) COMPILE_TIME_ASSERT(RT_LAST == TABLESIZE(opt->win32_pe.compress_rt)); isection = NULL; @@ -611,7 +615,7 @@ unsigned PeFile::processImports() // pass 1 unsigned k32namepos = ptr_diff(dllnames,oimpdlls); - memcpy(importednames, llgpa, ALIGN_UP(sizeof(llgpa), 2)); + memcpy(importednames, llgpa, ALIGN_UP((unsigned) sizeof(llgpa), 2u)); strcpy(dllnames,kernel32dll); im->dllname = k32namepos; im->iat = ptr_diff(ordinals,oimpdlls); @@ -886,7 +890,7 @@ void PeFile::processExports(Export *xport) // pass1 return; } xport->convert(IDADDR(PEDIR_EXPORT),IDSIZE(PEDIR_EXPORT)); - soexport = ALIGN_UP(xport->getsize(),4); + soexport = ALIGN_UP(xport->getsize(), 4u); oexport = new upx_byte[soexport]; memset(oexport, 0, soexport); } @@ -1066,7 +1070,7 @@ PeFile::Resource::~Resource() unsigned PeFile::Resource::dirsize() const { - return ALIGN_UP(dsize + ssize,4); + return ALIGN_UP(dsize + ssize, 4u); } bool PeFile::Resource::next() @@ -1122,9 +1126,12 @@ const upx_byte *PeFile::Resource::nname() const void PeFile::Resource::init(const upx_byte *res) { - COMPILE_TIME_ASSERT(sizeof(res_dir_entry) == 8); - COMPILE_TIME_ASSERT(sizeof(res_dir) == 16 + sizeof(res_dir_entry)); - COMPILE_TIME_ASSERT(sizeof(res_data) == 16); + COMPILE_TIME_ASSERT(sizeof(res_dir_entry) == 8) + COMPILE_TIME_ASSERT(sizeof(res_dir) == 16 + 8) + COMPILE_TIME_ASSERT(sizeof(res_data) == 16) + COMPILE_TIME_ASSERT_ALIGNED1(res_dir_entry) + COMPILE_TIME_ASSERT_ALIGNED1(res_dir) + COMPILE_TIME_ASSERT_ALIGNED1(res_data) start = res; root = head = current = NULL;