diff --git a/src/Makefile b/src/Makefile index 60fead89..ca31efad 100644 --- a/src/Makefile +++ b/src/Makefile @@ -73,24 +73,16 @@ endif # automatically format some C++ source code files ifeq ($(shell uname),Linux) -CLANG_FORMAT_FILES += bele.h bele_policy.h -CLANG_FORMAT_FILES += c_file.cpp c_init.cpp c_none.cpp c_screen.cpp -CLANG_FORMAT_FILES += compress_lzma.cpp compress_ucl.cpp compress_zlib.cpp -CLANG_FORMAT_FILES += console.h except.cpp except.h -CLANG_FORMAT_FILES += file.cpp file.h lefile.cpp lefile.h -CLANG_FORMAT_FILES += linker.cpp linker.h main.cpp msg.cpp options.cpp options.h -CLANG_FORMAT_FILES += p_armpe.cpp p_armpe.h p_com.cpp p_com.h p_djgpp2.cpp p_djgpp2.h -CLANG_FORMAT_FILES += p_exe.cpp p_exe.h p_ps1.cpp p_ps1.h p_sys.cpp p_sys.h -CLANG_FORMAT_FILES += p_tmt.cpp p_tmt.h p_tos.cpp p_tos.h -CLANG_FORMAT_FILES += p_w32pe.h p_w64pep.h -CLANG_FORMAT_FILES += p_wcle.cpp p_wcle.h -CLANG_FORMAT_FILES += packer.cpp packer.h packhead.cpp packmast.cpp packmast.h -CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h -CLANG_FORMAT_FILES += ui.cpp ui.h work.cpp -CLANG_FORMAT_FILES += $(wildcard util/*.[ch]*) +# Markus loves clang-format, but John hates it; find a compromise +CLANG_FORMAT_EXCLUDE_FILES += conf.h miniacc.h version.h +CLANG_FORMAT_EXCLUDE_FILES += compress.cpp compress.h filter.cpp filter.h filteri.cpp help.cpp +CLANG_FORMAT_EXCLUDE_FILES += p_elf.h p_elf_enum.h p_lx_% p_mach% p_unix% p_vmli% +CLANG_FORMAT_EXCLUDE_FILES += p_w32pe.cpp p_w64pep.cpp packer_c.cpp packer_f.cpp pefile% +CLANG_FORMAT_FILES := $(sort $(wildcard *.[ch]* ../maint/src/*.[ch]* util/*.[ch]*)) +CLANG_FORMAT_FILES := $(filter-out $(CLANG_FORMAT_EXCLUDE_FILES),$(CLANG_FORMAT_FILES)) clang-format: PHONY $(CLANG_FORMAT_FILES) @echo "running upx-clang-format" - @$(top_srcdir)/misc/scripts/upx-clang-format -i $(sort $(CLANG_FORMAT_FILES)) + @$(top_srcdir)/misc/scripts/upx-clang-format -i $(CLANG_FORMAT_FILES) endif # diff --git a/src/bele.h b/src/bele.h index 7e0b24e3..bff9f655 100644 --- a/src/bele.h +++ b/src/bele.h @@ -251,6 +251,7 @@ inline upx_int64_t get_le64_signed(const void *p) { **************************************************************************/ struct alignas(1) BE16 { + typedef unsigned integral_conversion_type; // automatic conversion to unsigned unsigned char d[2]; BE16 &operator=(unsigned v) { @@ -300,6 +301,7 @@ struct alignas(1) BE16 { }; struct alignas(1) BE32 { + typedef unsigned integral_conversion_type; // automatic conversion to unsigned unsigned char d[4]; BE32 &operator=(unsigned v) { @@ -349,6 +351,7 @@ struct alignas(1) BE32 { }; struct alignas(1) BE64 { + typedef upx_uint64_t integral_conversion_type; // automatic conversion to upx_uint64_t unsigned char d[8]; BE64 &operator=(upx_uint64_t v) { @@ -398,6 +401,7 @@ struct alignas(1) BE64 { }; struct alignas(1) LE16 { + typedef unsigned integral_conversion_type; // automatic conversion to unsigned unsigned char d[2]; LE16 &operator=(unsigned v) { @@ -447,6 +451,7 @@ struct alignas(1) LE16 { }; struct alignas(1) LE32 { + typedef unsigned integral_conversion_type; // automatic conversion to unsigned unsigned char d[4]; LE32 &operator=(unsigned v) { @@ -496,6 +501,7 @@ struct alignas(1) LE32 { }; struct alignas(1) LE64 { + typedef upx_uint64_t integral_conversion_type; // automatic conversion to upx_uint64_t unsigned char d[8]; LE64 &operator=(upx_uint64_t v) { diff --git a/src/compress_ucl.cpp b/src/compress_ucl.cpp index 11094a24..f1649812 100644 --- a/src/compress_ucl.cpp +++ b/src/compress_ucl.cpp @@ -271,7 +271,7 @@ unsigned upx_ucl_crc32(const void *buf, unsigned len, unsigned crc) { // doctest checks **************************************************************************/ -#if DEBUG && 1 +#if DEBUG && !defined(DOCTEST_CONFIG_DISABLE) && 1 #include "util/membuffer.h" diff --git a/src/compress_zlib.cpp b/src/compress_zlib.cpp index 8f186299..8545f47c 100644 --- a/src/compress_zlib.cpp +++ b/src/compress_zlib.cpp @@ -240,7 +240,7 @@ unsigned upx_zlib_crc32(const void *buf, unsigned len, unsigned crc) { // doctest checks **************************************************************************/ -#if DEBUG && 1 +#if DEBUG && !defined(DOCTEST_CONFIG_DISABLE) && 1 #include "util/membuffer.h" diff --git a/src/p_vmlinx.cpp b/src/p_vmlinx.cpp index ae244ce0..2e224b26 100644 --- a/src/p_vmlinx.cpp +++ b/src/p_vmlinx.cpp @@ -1155,6 +1155,7 @@ bool PackVmlinuxPPC32::has_valid_vmlinux_head() return false; } +static const #include "stub/powerpc64le-linux.kernel.vmlinux-head.h" bool PackVmlinuxPPC64LE::has_valid_vmlinux_head() { diff --git a/src/stub/scripts/bin2h.py b/src/stub/scripts/bin2h.py index e30398c5..e11b6cd7 100644 --- a/src/stub/scripts/bin2h.py +++ b/src/stub/scripts/bin2h.py @@ -188,6 +188,12 @@ def write_stub(w, odata, method_index, methods): if opts.ident: if opts.mode == "c": w_checksum_c(w, opts.ident.upper(), odata) + if 0: + # idea: put all stubs in a dedicated section so that UPX compresses better + #w("#if defined(__ELF__)\n") + #w('__attribute__((__section__("upx_stubs")))\n') + #w("#endif\n") + w("ATTRIBUTE_FOR_STUB(%s)\n" % (opts.ident)) w("unsigned char %s[%d] = {\n" % (opts.ident, len(odata))) if opts.mode == "c": DataWriter_c(w).w_data(odata) @@ -351,6 +357,17 @@ def main(argv): else: ofp.close() + # write an extra C file so that we can test the total size of the stubs: + # $ gcc -Wall -c test_size*.c + # $ size --totals test_size*.o + # current results (2022-12-22): + # 89 files, 1,082,956 bytes + if 0 and not opts.dry_run: + if opts.ident and ofile and ofile != "-": + tfp = open("test_size_" + ofile + ".c", "wb") + tfp.write("const\n") + tfp.write('#include "' + ofile + '"\n') + tfp.close() if __name__ == "__main__": sys.exit(main(sys.argv)) diff --git a/src/util/dt_check.cpp b/src/util/dt_check.cpp index 2930b58f..89a8af36 100644 --- a/src/util/dt_check.cpp +++ b/src/util/dt_check.cpp @@ -134,61 +134,65 @@ template struct TestBELE { __acc_static_noinline bool test(void) { COMPILE_TIME_ASSERT_ALIGNED1(T) - struct alignas(1) test1_t { - char a; - T b; - }; - struct alignas(1) test2_t { - char a; - T b[3]; - }; - 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)) + { + struct alignas(1) test1_t { + char a; + T b; + }; + struct alignas(1) test2_t { + char a; + T b[3]; + }; + COMPILE_TIME_ASSERT_ALIGNED1(test1_t) + COMPILE_TIME_ASSERT_ALIGNED1(test2_t) + test1_t t1[7]; + test2_t t2[7]; + COMPILE_TIME_ASSERT(sizeof(test1_t) == 1 + sizeof(T)) + COMPILE_TIME_ASSERT(sizeof(t1) == 7 + 7 * sizeof(T)) + COMPILE_TIME_ASSERT(sizeof(test2_t) == 1 + 3 * sizeof(T)) + COMPILE_TIME_ASSERT(sizeof(t2) == 7 + 21 * sizeof(T)) + UNUSED(t1); + UNUSED(t2); + } #if 1 - T allbits; - allbits = 0; - allbits += 1; - allbits -= 2; - T v1; - v1 = 1; - v1 *= 2; - v1 -= 1; - T v2; - v2 = 1; - assert((v1 == v2)); - assert(!(v1 != v2)); - assert((v1 <= v2)); - assert((v1 >= v2)); - assert(!(v1 < v2)); - assert(!(v1 > v2)); - v2 ^= allbits; - assert(!(v1 == v2)); - assert((v1 != v2)); - assert((v1 <= v2)); - assert(!(v1 >= v2)); - assert((v1 < v2)); - assert(!(v1 > v2)); - v2 += 2; - assert(v1 == 1); - assert(v2 == 0); - v1 <<= 1; - v1 |= v2; - v1 >>= 1; - v2 &= v1; - v2 /= v1; - v2 *= v1; - assert(v1 == 1); - assert(v2 == 0); - if ((v1 ^ v2) != 1) - return false; + { + T allbits; + allbits = 0; + allbits += 1; + allbits -= 2; + T v1; + v1 = 1; + v1 *= 2; + v1 -= 1; + T v2; + v2 = 1; + assert((v1 == v2)); + assert(!(v1 != v2)); + assert((v1 <= v2)); + assert((v1 >= v2)); + assert(!(v1 < v2)); + assert(!(v1 > v2)); + v2 ^= allbits; + assert(!(v1 == v2)); + assert((v1 != v2)); + assert((v1 <= v2)); + assert(!(v1 >= v2)); + assert((v1 < v2)); + assert(!(v1 > v2)); + v2 += 2; + assert(v1 == 1); + assert(v2 == 0); + v1 <<= 1; + v1 |= v2; + v1 >>= 1; + v2 &= v1; + v2 /= v1; + v2 *= v1; + assert(v1 == 1); + assert(v2 == 0); + if ((v1 ^ v2) != 1) + return false; + } #endif return true; } @@ -325,6 +329,20 @@ void upx_compiler_sanity_check(void) { assert(get_le26(d) == 0x03020304); assert(dd == ne32_to_le32(0xf7020304)); } + { + upx_uint16_t a; + upx_uint32_t b; + upx_uint64_t c; + set_ne16(&a, 0x04030201); // ignore upper bits + set_ne32(&b, 0x04030201); + set_ne64(&c, 0x0807060504030201ull); + assert(a == 0x0201); + assert(b == 0x04030201); + assert(c == 0x0807060504030201ull); + assert(get_ne16(&a) == 0x0201); + assert(get_ne32(&b) == 0x04030201); + assert(get_ne64(&c) == 0x0807060504030201ull); + } #endif union { short v_short;