From 9f5d67e27c08254bffc5c904f76a28252d341ae1 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Sat, 26 Nov 2022 17:45:25 +0100 Subject: [PATCH] src: clang-format various packers --- src/Makefile | 3 +- src/p_com.cpp | 128 ++++----- src/p_com.h | 15 +- src/p_djgpp2.cpp | 198 +++++--------- src/p_djgpp2.h | 70 +++-- src/p_exe.cpp | 499 ++++++++++++++--------------------- src/p_exe.h | 40 +-- src/p_ps1.cpp | 405 ++++++++++++----------------- src/p_ps1.h | 38 ++- src/p_sys.cpp | 50 ++-- src/p_sys.h | 15 +- src/p_wcle.cpp | 659 +++++++++++++++++++++-------------------------- src/p_wcle.h | 18 +- 13 files changed, 863 insertions(+), 1275 deletions(-) diff --git a/src/Makefile b/src/Makefile index a6f94f3c..93277965 100644 --- a/src/Makefile +++ b/src/Makefile @@ -79,7 +79,8 @@ CLANG_FORMAT_FILES += except.cpp except.h CLANG_FORMAT_FILES += file.cpp file.h CLANG_FORMAT_FILES += linker.cpp linker.h packhead.cpp packmast.cpp packmast.h CLANG_FORMAT_FILES += main.cpp options.cpp options.h packer.cpp packer.h -CLANG_FORMAT_FILES += p_tmt.cpp p_tmt.h p_tos.cpp p_tos.h +CLANG_FORMAT_FILES += p_com.cpp p_com.h p_djgpp2.cpp p_djgpp2.h p_exe.cpp p_exe.h p_ps1.cpp p_ps1.h +CLANG_FORMAT_FILES += p_sys.cpp p_sys.h p_tmt.cpp p_tmt.h p_tos.cpp p_tos.h p_wcle.cpp p_wcle.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/[a-ln-z]*.[ch]* util/mem*.[ch]*) diff --git a/src/p_com.cpp b/src/p_com.cpp index f6bcc5db..a353579e 100644 --- a/src/p_com.cpp +++ b/src/p_com.cpp @@ -25,7 +25,6 @@ */ - #include "conf.h" #include "file.h" #include "filter.h" @@ -33,49 +32,42 @@ #include "p_com.h" #include "linker.h" -static const +static const CLANG_FORMAT_DUMMY_STATEMENT #include "stub/i086-dos16.com.h" //#define TESTING 1 - /************************************************************************* // **************************************************************************/ -const int *PackCom::getCompressionMethods(int method, int level) const -{ - static const int m_nrv2b[] = { M_NRV2B_LE16, M_END }; +const int *PackCom::getCompressionMethods(int method, int level) const { + static const int m_nrv2b[] = {M_NRV2B_LE16, M_END}; #if 0 static const int m_nrv2d[] = { M_NRV2D_LE16, M_END }; #endif - UNUSED(method); UNUSED(level); + UNUSED(method); + UNUSED(level); return m_nrv2b; } - -const int *PackCom::getFilters() const -{ - static const int filters[] = { - 0x06, 0x03, 0x04, 0x01, 0x05, 0x02, - FT_END }; +const int *PackCom::getFilters() const { + static const int filters[] = {0x06, 0x03, 0x04, 0x01, 0x05, 0x02, FT_END}; return filters; } - /************************************************************************* // **************************************************************************/ -bool PackCom::canPack() -{ +bool PackCom::canPack() { unsigned char buf[128]; fi->readx(buf, sizeof(buf)); - if (memcmp(buf,"MZ",2) == 0 || memcmp(buf,"ZM",2) == 0 // .exe - || memcmp (buf,"\xff\xff\xff\xff",4) == 0) // .sys + if (memcmp(buf, "MZ", 2) == 0 || memcmp(buf, "ZM", 2) == 0 // .exe + || memcmp(buf, "\xff\xff\xff\xff", 4) == 0) // .sys return false; - if (!fn_has_ext(fi->getName(),"com")) + if (!fn_has_ext(fi->getName(), "com")) return false; checkAlreadyPacked(buf, sizeof(buf)); if (file_size < 1024) @@ -85,15 +77,11 @@ bool PackCom::canPack() return true; } - /************************************************************************* // **************************************************************************/ -void PackCom::patchLoader(OutputFile *fo, - upx_byte *loader, int lsize, - unsigned calls) -{ +void PackCom::patchLoader(OutputFile *fo, upx_byte *loader, int lsize, unsigned calls) { const int e_len = getLoaderSectionStart("COMCUTPO"); const int d_len = lsize - e_len; assert(e_len > 0 && e_len < 128); @@ -118,11 +106,11 @@ void PackCom::patchLoader(OutputFile *fo, loader = getLoader(); // some day we could use the relocation stuff for patchPackHeader too - patchPackHeader(loader,e_len); + patchPackHeader(loader, e_len); // write loader + compressed file - fo->write(loader,e_len); // entry - fo->write(obuf,ph.c_len); - fo->write(loader+e_len,d_len); // decompressor + fo->write(loader, e_len); // entry + fo->write(obuf, ph.c_len); + fo->write(loader + e_len, d_len); // decompressor #if 0 printf("%-13s: entry : %8ld bytes\n", getName(), (long) e_len); printf("%-13s: compressed : %8ld bytes\n", getName(), (long) ph.c_len); @@ -130,65 +118,45 @@ void PackCom::patchLoader(OutputFile *fo, #endif } - -void PackCom::buildLoader(const Filter *ft) -{ +void PackCom::buildLoader(const Filter *ft) { initLoader(stub_i086_dos16_com, sizeof(stub_i086_dos16_com)); - addLoader("COMMAIN1", - ph.first_offset_found == 1 ? "COMSBBBP" : "", - "COMPSHDI", - ft->id ? "COMCALLT" : "", - "COMMAIN2,UPX1HEAD,COMCUTPO,NRV2B160", - ft->id ? "NRVDDONE" : "NRVDRETU", - "NRVDECO1", - ph.max_offset_found <= 0xd00 ? "NRVLED00" : "NRVGTD00", - "NRVDECO2", - nullptr - ); - if (ft->id) - { + addLoader("COMMAIN1", ph.first_offset_found == 1 ? "COMSBBBP" : "", "COMPSHDI", + ft->id ? "COMCALLT" : "", "COMMAIN2,UPX1HEAD,COMCUTPO,NRV2B160", + ft->id ? "NRVDDONE" : "NRVDRETU", "NRVDECO1", + ph.max_offset_found <= 0xd00 ? "NRVLED00" : "NRVGTD00", "NRVDECO2", nullptr); + if (ft->id) { assert(ft->calls > 0); addFilter16(ft->id); } } - -void PackCom::addFilter16(int filter_id) -{ +void PackCom::addFilter16(int filter_id) { assert(filter_id > 0); assert(isValidFilter(filter_id)); if (filter_id % 3 == 0) - addLoader("CALLTR16", - filter_id < 4 ? "CT16SUB0" : "", - filter_id < 4 ? "" : (opt->cpu == opt->CPU_8086 ? "CT16I086" : "CT16I286,CT16SUB0"), - "CALLTRI2", - getFormat() == UPX_F_DOS_COM ? "CORETURN" : "", - nullptr - ); + addLoader("CALLTR16", filter_id < 4 ? "CT16SUB0" : "", + filter_id < 4 ? "" + : (opt->cpu == opt->CPU_8086 ? "CT16I086" : "CT16I286,CT16SUB0"), + "CALLTRI2", getFormat() == UPX_F_DOS_COM ? "CORETURN" : "", nullptr); else - addLoader(filter_id%3 == 1 ? "CT16E800" : "CT16E900", - "CALLTRI5", - getFormat() == UPX_F_DOS_COM ? "CT16JEND" : "CT16JUL2", - filter_id < 4 ? "CT16SUB1" : "", - filter_id < 4 ? "" : (opt->cpu == opt->CPU_8086 ? "CT16I087" : "CT16I287,CT16SUB1"), - "CALLTRI6", - nullptr - ); + addLoader( + filter_id % 3 == 1 ? "CT16E800" : "CT16E900", "CALLTRI5", + getFormat() == UPX_F_DOS_COM ? "CT16JEND" : "CT16JUL2", filter_id < 4 ? "CT16SUB1" : "", + filter_id < 4 ? "" : (opt->cpu == opt->CPU_8086 ? "CT16I087" : "CT16I287,CT16SUB1"), + "CALLTRI6", nullptr); } - /************************************************************************* // **************************************************************************/ -void PackCom::pack(OutputFile *fo) -{ +void PackCom::pack(OutputFile *fo) { // read file ibuf.alloc(file_size); obuf.allocForCompression(file_size); - fi->seek(0,SEEK_SET); - fi->readx(ibuf,file_size); + fi->seek(0, SEEK_SET); + fi->readx(ibuf, file_size); // prepare packheader ph.u_len = file_size; @@ -201,7 +169,7 @@ void PackCom::pack(OutputFile *fo) const int lsize = getLoaderSize(); MemBuffer loader(lsize); - memcpy(loader,getLoader(),lsize); + memcpy(loader, getLoader(), lsize); const unsigned calls = ft.id % 3 ? ft.lastcall - 2 * ft.calls : ft.calls; patchLoader(fo, loader, lsize, calls); @@ -214,13 +182,11 @@ void PackCom::pack(OutputFile *fo) throwNotCompressible(); } - /************************************************************************* // **************************************************************************/ -int PackCom::canUnpack() -{ +int PackCom::canUnpack() { if (!readPackHeader(128)) return false; if (file_size_u <= ph.c_len) @@ -228,19 +194,17 @@ int PackCom::canUnpack() return true; } - /************************************************************************* // **************************************************************************/ -void PackCom::unpack(OutputFile *fo) -{ +void PackCom::unpack(OutputFile *fo) { ibuf.alloc(file_size); obuf.allocForDecompression(ph.u_len); // read whole file - fi->seek(0,SEEK_SET); - fi->readx(ibuf,file_size); + fi->seek(0, SEEK_SET); + fi->readx(ibuf, file_size); // get compressed data offset unsigned e_len = ph.buf_offset + ph.getPackHeaderSize(); @@ -248,22 +212,18 @@ void PackCom::unpack(OutputFile *fo) throwCantUnpack("file damaged"); // decompress - decompress(ibuf+e_len,obuf); + decompress(ibuf + e_len, obuf); // unfilter Filter ft(ph.level); ft.init(ph.filter, getCallTrickOffset()); - ft.unfilter(obuf,ph.u_len); + ft.unfilter(obuf, ph.u_len); // write decompressed file if (fo) - fo->write(obuf,ph.u_len); + fo->write(obuf, ph.u_len); } - -Linker* PackCom::newLinker() const -{ - return new ElfLinkerX86(); -} +Linker *PackCom::newLinker() const { return new ElfLinkerX86(); } /* vim:set ts=4 sw=4 et: */ diff --git a/src/p_com.h b/src/p_com.h index 40bf1b58..4ec578c5 100644 --- a/src/p_com.h +++ b/src/p_com.h @@ -25,24 +25,22 @@ */ - -#ifndef __UPX_P_COM_H -#define __UPX_P_COM_H 1 - +#pragma once +#ifndef UPX_P_COM_H__ +#define UPX_P_COM_H__ 1 /************************************************************************* // dos/com **************************************************************************/ -class PackCom : public Packer -{ +class PackCom : public Packer { typedef Packer super; + public: PackCom(InputFile *f) : super(f) { bele = &N_BELE_RTP::le_policy; } virtual int getVersion() const override { return 13; } virtual int getFormat() const override { return UPX_F_DOS_COM; } virtual const char *getName() const override { return "dos/com"; } - //virtual const char *getFullName(const options_t *o) const override { return o && o->cpu == o->CPU_8086 ? "i086-dos16.com" : "i286-dos16.com"; } virtual const char *getFullName(const options_t *) const override { return "i086-dos16.com"; } virtual const int *getCompressionMethods(int method, int level) const override; virtual const int *getFilters() const override; @@ -55,7 +53,7 @@ public: protected: virtual unsigned getCallTrickOffset() const { return 0x100; } - virtual Linker* newLinker() const override; + virtual Linker *newLinker() const override; protected: virtual void buildLoader(const Filter *ft) override; @@ -63,7 +61,6 @@ protected: virtual void addFilter16(int filter_id); }; - #endif /* already included */ /* vim:set ts=4 sw=4 et: */ diff --git a/src/p_djgpp2.cpp b/src/p_djgpp2.cpp index d51db076..5646a993 100644 --- a/src/p_djgpp2.cpp +++ b/src/p_djgpp2.cpp @@ -25,7 +25,6 @@ */ - #include "conf.h" #include "file.h" #include "filter.h" @@ -33,19 +32,16 @@ #include "p_djgpp2.h" #include "linker.h" -static const +static const CLANG_FORMAT_DUMMY_STATEMENT #include "stub/i386-dos32.djgpp2.h" -static const +static const CLANG_FORMAT_DUMMY_STATEMENT #include "stub/i386-dos32.djgpp2-stubify.h" - /************************************************************************* // **************************************************************************/ -PackDjgpp2::PackDjgpp2(InputFile *f) : - super(f), coff_offset(0) -{ +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) @@ -53,60 +49,38 @@ PackDjgpp2::PackDjgpp2(InputFile *f) : 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))); - //assert(upx_adler32(stubify_stub, sizeof(stubify_stub)) == STUBIFY_STUB_ADLER32); + COMPILE_TIME_ASSERT(STUB_I386_DOS32_DJGPP2_STUBIFY_CRC32 == 0x2ae982b2) + // printf("0x%08x\n", upx_adler32(stubify_stub, sizeof(stubify_stub))); + // assert(upx_adler32(stubify_stub, sizeof(stubify_stub)) == STUBIFY_STUB_ADLER32); } - -const int *PackDjgpp2::getCompressionMethods(int method, int level) const -{ +const int *PackDjgpp2::getCompressionMethods(int method, int level) const { return Packer::getDefaultCompressionMethods_le32(method, level); } - -const int *PackDjgpp2::getFilters() const -{ - static const int filters[] = { - 0x26, 0x24, 0x49, 0x46, 0x16, 0x13, 0x14, 0x11, - FT_ULTRA_BRUTE, 0x25, 0x15, 0x12, - FT_END }; +const int *PackDjgpp2::getFilters() const { + static const int filters[] = {0x26, 0x24, 0x49, 0x46, 0x16, 0x13, 0x14, + 0x11, FT_ULTRA_BRUTE, 0x25, 0x15, 0x12, FT_END}; return filters; } - -unsigned PackDjgpp2::findOverlapOverhead(const upx_bytep buf, - const upx_bytep tbuf, - unsigned range, - unsigned upper_limit) const -{ +unsigned PackDjgpp2::findOverlapOverhead(const upx_bytep buf, const upx_bytep tbuf, unsigned range, + unsigned upper_limit) const { unsigned o = super::findOverlapOverhead(buf, tbuf, range, upper_limit); - o = (o + 0x3ff) &~ 0x1ff; + o = (o + 0x3ff) & ~0x1ff; return o; } +Linker *PackDjgpp2::newLinker() const { return new ElfLinkerX86; } -Linker* PackDjgpp2::newLinker() const -{ - return new ElfLinkerX86; -} - - -void PackDjgpp2::buildLoader(const Filter *ft) -{ +void PackDjgpp2::buildLoader(const Filter *ft) { // prepare loader initLoader(stub_i386_dos32_djgpp2, sizeof(stub_i386_dos32_djgpp2)); - addLoader("IDENTSTR,DJ2MAIN1", - ft->id ? "DJCALLT1" : "", + addLoader("IDENTSTR,DJ2MAIN1", ft->id ? "DJCALLT1" : "", ph.first_offset_found == 1 ? "DJ2MAIN2" : "", - M_IS_LZMA(ph.method) ? "LZMA_INIT_STACK" : "", - getDecompressorSections(), - M_IS_LZMA(ph.method) ? "LZMA_DONE_STACK" : "", - "DJ2BSS00", - nullptr - ); - if (ft->id) - { + M_IS_LZMA(ph.method) ? "LZMA_INIT_STACK" : "", getDecompressorSections(), + M_IS_LZMA(ph.method) ? "LZMA_DONE_STACK" : "", "DJ2BSS00", nullptr); + if (ft->id) { assert(ft->calls > 0); addLoader("DJCALLT2", nullptr); addFilter32(ft->id); @@ -114,32 +88,24 @@ void PackDjgpp2::buildLoader(const Filter *ft) addLoader("DJRETURN,+40C,UPX1HEAD", nullptr); } - /************************************************************************* // util **************************************************************************/ -void PackDjgpp2::handleStub(OutputFile *fo) -{ - if (fo && !opt->djgpp2_coff.coff) - { - if (coff_offset > 0) - { +void PackDjgpp2::handleStub(OutputFile *fo) { + if (fo && !opt->djgpp2_coff.coff) { + if (coff_offset > 0) { // copy stub from exe Packer::handleStub(fi, fo, coff_offset); - } - else - { + } else { // "stubify" stub - info("Adding stub: %ld bytes", (long)sizeof(stub_i386_dos32_djgpp2_stubify)); + info("Adding stub: %ld bytes", (long) sizeof(stub_i386_dos32_djgpp2_stubify)); fo->write(stub_i386_dos32_djgpp2_stubify, sizeof(stub_i386_dos32_djgpp2_stubify)); } } } - -static bool is_dlm(InputFile *fi, unsigned coff_offset) -{ +static bool is_dlm(InputFile *fi, unsigned coff_offset) { unsigned char buf[4]; unsigned off; @@ -153,14 +119,12 @@ static bool is_dlm(InputFile *fi, unsigned coff_offset) fi->readx(buf, 4); if (memcmp(buf, "DLMF", 4) == 0) return true; - } catch (const IOException&) { + } catch (const IOException &) { } return false; } - -static void handle_allegropak(InputFile *fi, OutputFile *fo) -{ +static void handle_allegropak(InputFile *fi, OutputFile *fo) { unsigned char b[8]; int pfsize = 0; @@ -169,80 +133,71 @@ static void handle_allegropak(InputFile *fi, OutputFile *fo) fi->readx(b, 8); if (memcmp(b, "slh+", 4) != 0) return; - pfsize = get_be32_signed(b+4); + pfsize = get_be32_signed(b + 4); if (pfsize <= 8 || pfsize >= fi->st.st_size) return; fi->seek(-pfsize, SEEK_END); - } catch (const IOException&) { + } catch (const IOException &) { return; } MemBuffer buf(0x4000); - while (pfsize > 0) - { - const int len = UPX_MIN(pfsize, (int)buf.getSize()); + while (pfsize > 0) { + const int len = UPX_MIN(pfsize, (int) buf.getSize()); fi->readx(buf, len); fo->write(buf, len); pfsize -= len; } } - -int PackDjgpp2::readFileHeader() -{ +int PackDjgpp2::readFileHeader() { unsigned char hdr[0x1c]; unsigned char magic[8]; fi->seek(0, SEEK_SET); fi->readx(hdr, sizeof(hdr)); - if (get_le16(hdr) == 0x5a4d) // MZ exe signature, stubbed? + if (get_le16(hdr) == 0x5a4d) // MZ exe signature, stubbed? { - coff_offset = 512 * get_le16(hdr+4); - if (get_le16(hdr+2) != 0) - coff_offset += get_le16(hdr+2) - 512; + coff_offset = 512 * get_le16(hdr + 4); + if (get_le16(hdr + 2) != 0) + coff_offset += get_le16(hdr + 2) - 512; fi->seek(512, SEEK_SET); fi->readx(magic, 8); if (memcmp("go32stub", magic, 8) != 0) - return 0; // not V2 image + return 0; // not V2 image fi->seek(coff_offset, SEEK_SET); if (fi->read(&coff_hdr, sizeof(coff_hdr)) != sizeof(coff_hdr)) throwCantPack("skipping djgpp symlink"); - } - else - { + } else { fi->seek(coff_offset, SEEK_SET); fi->readx(&coff_hdr, 0xa8); } - if (coff_hdr.f_magic != 0x014c) // I386MAGIC + if (coff_hdr.f_magic != 0x014c) // I386MAGIC return 0; - if ((coff_hdr.f_flags & 2) == 0) // F_EXEC - COFF executable + if ((coff_hdr.f_flags & 2) == 0) // F_EXEC - COFF executable return 0; - if (coff_hdr.a_magic != 0413) // ZMAGIC - demand load format + if (coff_hdr.a_magic != 0413) // ZMAGIC - demand load format return 0; // FIXME: check for Linux etc. text = coff_hdr.sh; data = text + 1; - bss = data + 1; + bss = data + 1; return UPX_F_DJGPP2_COFF; } - // "strip" debug info -void PackDjgpp2::stripDebug() -{ +void PackDjgpp2::stripDebug() { coff_hdr.f_symptr = 0; coff_hdr.f_nsyms = 0; - coff_hdr.f_flags = 0x10f; // 0x100: "32 bit machine: LSB first" + coff_hdr.f_flags = 0x10f; // 0x100: "32 bit machine: LSB first" memset(text->misc, 0, 12); } - /************************************************************************* // **************************************************************************/ -bool PackDjgpp2::canPack() -{ +bool PackDjgpp2::canPack() { if (!readFileHeader()) return false; if (is_dlm(fi, coff_offset)) @@ -253,38 +208,32 @@ bool PackDjgpp2::canPack() throwAlreadyPacked(); // Check for gap in vaddr between text and data, or between data and bss. - if (text->vaddr + text->size != data->vaddr - || data->vaddr + data->size != bss->vaddr) - { + if (text->vaddr + text->size != data->vaddr || data->vaddr + data->size != bss->vaddr) { // "Non-standard" layout of text,data,bss: not contiguous in vaddr. // But should be OK if no overlap. // Check for no overlap of text and data: // neither by vaddr, nor by image data if (text->vaddr + text->size <= data->vaddr && - data->scnptr - text->scnptr <= data->vaddr - text->vaddr) - { + data->scnptr - text->scnptr <= data->vaddr - text->vaddr) { // Examples: Quake1; FreePascal(DOS) install.exe (github-issue45) // Hack: enlarge text image data to eliminate the gap. text->size = coff_hdr.a_tsize = data->scnptr - text->scnptr; // But complain if this causes overlap in vaddr if (text->vaddr + text->size > data->vaddr) throwAlreadyPacked(); - } - else + } else throwAlreadyPacked(); } // FIXME: check for Linux etc. return true; } - /************************************************************************* // **************************************************************************/ -void PackDjgpp2::pack(OutputFile *fo) -{ +void PackDjgpp2::pack(OutputFile *fo) { handleStub(fo); // patch coff header #1: "strip" debug info @@ -302,7 +251,7 @@ void PackDjgpp2::pack(OutputFile *fo) obuf.allocForCompression(usize); fi->seek(coff_offset, SEEK_SET); - fi->readx(ibuf, hdrsize); // orig. coff header + fi->readx(ibuf, hdrsize); // orig. coff header fi->seek(coff_offset + tpos, SEEK_SET); fi->readx(ibuf + hdrsize, size); @@ -313,7 +262,8 @@ void PackDjgpp2::pack(OutputFile *fo) ft.buf_len = usize - data->size; ft.addvalue = text->vaddr - hdrsize; // compress - upx_compress_config_t cconf; cconf.reset(); + upx_compress_config_t cconf; + cconf.reset(); // limit stack size needed for runtime decompression cconf.conf_lzma.max_num_probs = 1846 + (768 << 4); // ushort: ~28 KiB stack compressWithFilters(&ft, 512, &cconf); @@ -321,24 +271,25 @@ void PackDjgpp2::pack(OutputFile *fo) // patch coff header #2 const unsigned lsize = getLoaderSize(); assert(lsize % 4 == 0); - text->size = lsize; // new size of .text - data->size = ph.c_len; // new size of .data + text->size = lsize; // new size of .text + data->size = ph.c_len; // new size of .data unsigned stack = 1024 + ph.overlap_overhead + getDecompressorWrkmemSize(); stack = ALIGN_UP(stack, 16u); - if (bss->size < stack) // give it a .bss + if (bss->size < stack) // give it a .bss bss->size = stack; text->scnptr = sizeof(coff_hdr); data->scnptr = text->scnptr + text->size; - data->vaddr = bss->vaddr + ((data->scnptr + data->size) & 0x1ff) - data->size + ph.overlap_overhead - 0x200; + data->vaddr = bss->vaddr + ((data->scnptr + data->size) & 0x1ff) - data->size + + ph.overlap_overhead - 0x200; coff_hdr.f_nscns = 3; linker->defineSymbol("original_entry", coff_hdr.a_entry); linker->defineSymbol("length_of_bss", ph.overlap_overhead / 4); defineDecompressorSymbols(); // Just need no overlap; non-contiguous (gap length > 0)) is OK - assert(bss->vaddr >= ((size + 0x1ff) &~ 0x1ff) + (text->vaddr &~ 0x1ff)); + assert(bss->vaddr >= ((size + 0x1ff) & ~0x1ff) + (text->vaddr & ~0x1ff)); linker->defineSymbol("stack_for_lzma", bss->vaddr + bss->size); linker->defineSymbol("start_of_uncompressed", text->vaddr - hdrsize); linker->defineSymbol("start_of_compressed", data->vaddr); @@ -356,7 +307,7 @@ void PackDjgpp2::pack(OutputFile *fo) bss->size -= ph.overlap_overhead; // because of a feature (bug?) in stub.asm we need some padding - memcpy(obuf+data->size, "UPX", 3); + memcpy(obuf + data->size, "UPX", 3); data->size = ALIGN_UP(data->size, 4u); linker->defineSymbol("DJ2MAIN1", coff_hdr.a_entry); @@ -391,13 +342,11 @@ void PackDjgpp2::pack(OutputFile *fo) throwNotCompressible(); } - /************************************************************************* // **************************************************************************/ -int PackDjgpp2::canUnpack() -{ +int PackDjgpp2::canUnpack() { if (!readFileHeader()) return false; if (is_dlm(fi, coff_offset)) @@ -406,13 +355,11 @@ int PackDjgpp2::canUnpack() return readPackHeader(4096) ? 1 : -1; } - /************************************************************************* // **************************************************************************/ -void PackDjgpp2::unpack(OutputFile *fo) -{ +void PackDjgpp2::unpack(OutputFile *fo) { handleStub(fo); ibuf.alloc(ph.c_len); @@ -424,27 +371,24 @@ void PackDjgpp2::unpack(OutputFile *fo) // decompress decompress(ibuf, obuf); - coff_header_t *chdr = (coff_header_t*) obuf.getVoidPtr(); + coff_header_t *chdr = (coff_header_t *) obuf.getVoidPtr(); text = chdr->sh; data = text + 1; - const unsigned hdrsize = 20 + 28 - + sizeof(external_scnhdr_t) * chdr->f_nscns; + const unsigned hdrsize = 20 + 28 + sizeof(external_scnhdr_t) * chdr->f_nscns; unsigned addvalue; if (ph.version >= 14) addvalue = text->vaddr - hdrsize; else - addvalue = text->vaddr &~ 0x1ff; // for old versions + addvalue = text->vaddr & ~0x1ff; // for old versions // unfilter - if (ph.filter) - { + if (ph.filter) { Filter ft(ph.level); ft.init(ph.filter, addvalue); ft.cto = (unsigned char) ph.filter_cto; - if (ph.version < 11) - { + if (ph.version < 11) { unsigned char ctobuf[4]; fi->readx(ctobuf, 4); ft.cto = (unsigned char) (get_le32(ctobuf) >> 24); @@ -452,17 +396,14 @@ void PackDjgpp2::unpack(OutputFile *fo) ft.unfilter(obuf, ph.u_len - data->size); } - if (ph.version < 14) - { + if (ph.version < 14) { // fixup for the aligning bug in strip 2.8+ text->scnptr &= 0x1ff; data->scnptr = text->scnptr + text->size; // write decompressed file if (fo) fo->write(obuf, ph.u_len); - } - else - { + } else { // write the header // some padding might be required between the end // of the header and the start of the .text section @@ -470,8 +411,7 @@ void PackDjgpp2::unpack(OutputFile *fo) const unsigned padding = text->scnptr - hdrsize; ibuf.clear(0, padding); - if (fo) - { + if (fo) { fo->write(obuf, hdrsize); fo->write(ibuf, padding); fo->write(obuf + hdrsize, ph.u_len - hdrsize); diff --git a/src/p_djgpp2.h b/src/p_djgpp2.h index ab673063..54eb039c 100644 --- a/src/p_djgpp2.h +++ b/src/p_djgpp2.h @@ -25,17 +25,15 @@ */ - -#ifndef __UPX_P_DJGPP2_H -#define __UPX_P_DJGPP2_H 1 - +#pragma once +#ifndef UPX_P_DJGPP2_H__ +#define UPX_P_DJGPP2_H__ 1 /************************************************************************* // djgpp2/coff **************************************************************************/ -class PackDjgpp2 final : public Packer -{ +class PackDjgpp2 final : public Packer { typedef Packer super; public: @@ -43,7 +41,9 @@ public: virtual int getVersion() const override { return 14; } virtual int getFormat() const override { return UPX_F_DJGPP2_COFF; } virtual const char *getName() const override { return "djgpp2/coff"; } - virtual const char *getFullName(const options_t *) const override { return "i386-dos32.djgpp2.coff"; } + virtual const char *getFullName(const options_t *) const override { + return "i386-dos32.djgpp2.coff"; + } virtual const int *getCompressionMethods(int method, int level) const override; virtual const int *getFilters() const override; @@ -57,54 +57,52 @@ protected: void handleStub(OutputFile *fo); int readFileHeader(); - virtual unsigned findOverlapOverhead(const upx_bytep buf, - const upx_bytep tbuf, + virtual unsigned findOverlapOverhead(const upx_bytep buf, const upx_bytep tbuf, unsigned range = 0, unsigned upper_limit = ~0u) const override; virtual void buildLoader(const Filter *ft) override; - virtual Linker* newLinker() const override; + virtual Linker *newLinker() const override; unsigned coff_offset; - __packed_struct(external_scnhdr_t) - char _[12]; // name, paddr - LE32 vaddr; - LE32 size; - LE32 scnptr; - char misc[12]; // relptr, lnnoptr, nreloc, nlnno - char __[4]; // flags - __packed_struct_end() + struct alignas(1) external_scnhdr_t { + char _[12]; // name, paddr + LE32 vaddr; + LE32 size; + LE32 scnptr; + char misc[12]; // relptr, lnnoptr, nreloc, nlnno + char __[4]; // flags + }; - __packed_struct(coff_header_t) + struct alignas(1) coff_header_t { // ext_file_hdr - LE16 f_magic; - LE16 f_nscns; - char _[4]; // f_timdat - LE32 f_symptr; - LE32 f_nsyms; - char __[2]; // f_opthdr - LE16 f_flags; + LE16 f_magic; + LE16 f_nscns; + char _[4]; // f_timdat + LE32 f_symptr; + LE32 f_nsyms; + char __[2]; // f_opthdr + LE16 f_flags; // aout_hdr - LE16 a_magic; - char ___[2]; // a_vstamp - LE32 a_tsize; - LE32 a_dsize; - char ____[4]; // a_bsize - LE32 a_entry; - char _____[8]; // a_text_start a_data_start + LE16 a_magic; + char ___[2]; // a_vstamp + LE32 a_tsize; + LE32 a_dsize; + char ____[4]; // a_bsize + LE32 a_entry; + char _____[8]; // a_text_start a_data_start // section headers external_scnhdr_t sh[3]; - __packed_struct_end() + }; coff_header_t coff_hdr; - external_scnhdr_t *text,*data,*bss; + external_scnhdr_t *text, *data, *bss; void stripDebug(); }; - #endif /* already included */ /* vim:set ts=4 sw=4 et: */ diff --git a/src/p_exe.cpp b/src/p_exe.cpp index 464db42d..3f8e5c50 100644 --- a/src/p_exe.cpp +++ b/src/p_exe.cpp @@ -25,7 +25,6 @@ */ - #include "conf.h" #include "file.h" #include "filter.h" @@ -33,23 +32,20 @@ #include "p_exe.h" #include "linker.h" -static const +static const CLANG_FORMAT_DUMMY_STATEMENT #include "stub/i086-dos16.exe.h" -#define RSFCRI 4096 // Reserved Space For Compressed Relocation Info -#define MAXMATCH 0x2000 -#define MAXRELOCS (0x8000-MAXMATCH) - -#define DI_LIMIT 0xff00 // see the assembly why +#define RSFCRI 4096 // Reserved Space For Compressed Relocation Info +#define MAXMATCH 0x2000 +#define MAXRELOCS (0x8000 - MAXMATCH) +#define DI_LIMIT 0xff00 // see the assembly why /************************************************************************* // **************************************************************************/ -PackExe::PackExe(InputFile *f) : - super(f) -{ +PackExe::PackExe(InputFile *f) : super(f) { bele = &N_BELE_RTP::le_policy; COMPILE_TIME_ASSERT(sizeof(exe_header_t) == 32) COMPILE_TIME_ASSERT_ALIGNED1(exe_header_t) @@ -58,10 +54,8 @@ PackExe::PackExe(InputFile *f) : use_clear_dirty_stack = false; } - -const int *PackExe::getCompressionMethods(int method, int level) const -{ - bool small = ih_imagesize <= 256*1024; +const int *PackExe::getCompressionMethods(int method, int level) const { + bool small = ih_imagesize <= 256 * 1024; // disable lzma for "--brute" unless explicitly given "--lzma" // WARNING: this side effect may persists for later files; // but note that class PackMaster creates per-file local options @@ -70,16 +64,10 @@ const int *PackExe::getCompressionMethods(int method, int level) const return Packer::getDefaultCompressionMethods_8(method, level, small); } +const int *PackExe::getFilters() const { return nullptr; } -const int *PackExe::getFilters() const -{ - return nullptr; -} - - -int PackExe::fillExeHeader(struct exe_header_t *eh) const -{ -#define oh (*eh) +int PackExe::fillExeHeader(struct exe_header_t *eh) const { +#define oh (*eh) // fill new exe header int flag = 0; if (!opt->dos_exe.no_reloc && !M_IS_LZMA(ph.method)) @@ -87,7 +75,7 @@ int PackExe::fillExeHeader(struct exe_header_t *eh) const if (ih.relocs == 0) flag |= NORELOC; - memset(&oh,0,sizeof(oh)); + memset(&oh, 0, sizeof(oh)); oh.ident = 'M' + 'Z' * 256; oh.headsize16 = 2; @@ -103,11 +91,10 @@ int PackExe::fillExeHeader(struct exe_header_t *eh) const oh.sp = minsp; unsigned destpara = (ph.u_len + ph.overlap_overhead - ph.c_len + 31) / 16; - oh.ss = ph.c_len/16 + destpara; - if (ih.ss*16 + ih.sp < 0x100000 && ih.ss > oh.ss && ih.sp > 0x200) + oh.ss = ph.c_len / 16 + destpara; + if (ih.ss * 16 + ih.sp < 0x100000 && ih.ss > oh.ss && ih.sp > 0x200) oh.ss = ih.ss; - if (oh.ss*16 + 0x50 < ih.ss*16 + ih.sp - && oh.ss*16 + 0x200 > ih.ss*16 + ih.sp) + if (oh.ss * 16 + 0x50 < ih.ss * 16 + ih.sp && oh.ss * 16 + 0x200 > ih.ss * 16 + ih.sp) oh.ss += 0x20; if (oh.ss != ih.ss) @@ -118,58 +105,38 @@ int PackExe::fillExeHeader(struct exe_header_t *eh) const #undef oh } -void PackExe::addLoaderEpilogue(int flag) -{ +void PackExe::addLoaderEpilogue(int flag) { addLoader("EXEMAIN5", nullptr); if (relocsize) - addLoader(ph.u_len <= DI_LIMIT || (ph.u_len & 0x7fff) >= relocsize ? "EXENOADJ" : "EXEADJUS", - "EXERELO1", - has_9a ? "EXEREL9A" : "", - "EXERELO2", - ih_exesize > 0xFE00 ? "EXEREBIG" : "", - "EXERELO3", - nullptr - ); - addLoader("EXEMAIN8", - device_driver ? "DEVICEEND" : "", - (flag & SS) ? "EXESTACK" : "", - (flag & SP) ? "EXESTASP" : "", - (flag & USEJUMP) ? "EXEJUMPF" : "", - nullptr - ); + addLoader(ph.u_len <= DI_LIMIT || (ph.u_len & 0x7fff) >= relocsize ? "EXENOADJ" + : "EXEADJUS", + "EXERELO1", has_9a ? "EXEREL9A" : "", "EXERELO2", + ih_exesize > 0xFE00 ? "EXEREBIG" : "", "EXERELO3", nullptr); + addLoader("EXEMAIN8", device_driver ? "DEVICEEND" : "", (flag & SS) ? "EXESTACK" : "", + (flag & SP) ? "EXESTASP" : "", (flag & USEJUMP) ? "EXEJUMPF" : "", nullptr); if (!(flag & USEJUMP)) - addLoader(ih.cs ? "EXERCSPO" : "", - "EXERETIP", - nullptr - ); + addLoader(ih.cs ? "EXERCSPO" : "", "EXERETIP", nullptr); linker->defineSymbol("original_cs", ih.cs); linker->defineSymbol("original_ip", ih.ip); linker->defineSymbol("original_sp", ih.sp); linker->defineSymbol("original_ss", ih.ss); - linker->defineSymbol("reloc_size", - (ph.u_len <= DI_LIMIT || (ph.u_len & 0x7fff) - >= relocsize ? 0 : MAXRELOCS) - relocsize); + linker->defineSymbol( + "reloc_size", + (ph.u_len <= DI_LIMIT || (ph.u_len & 0x7fff) >= relocsize ? 0 : MAXRELOCS) - relocsize); } -void PackExe::buildLoader(const Filter *) -{ +void PackExe::buildLoader(const Filter *) { // get flag exe_header_t dummy_oh; int flag = fillExeHeader(&dummy_oh); initLoader(stub_i086_dos16_exe, sizeof(stub_i086_dos16_exe)); - if (M_IS_LZMA(ph.method)) - { - addLoader("LZMA_DEC00", - opt->small ? "LZMA_DEC10" : "LZMA_DEC20", - "LZMA_DEC30", - use_clear_dirty_stack ? "LZMA_DEC31" : "", - "LZMA_DEC32", - ph.u_len > 0xffff ? "LZMA_DEC33" : "", - nullptr - ); + if (M_IS_LZMA(ph.method)) { + addLoader("LZMA_DEC00", opt->small ? "LZMA_DEC10" : "LZMA_DEC20", "LZMA_DEC30", + use_clear_dirty_stack ? "LZMA_DEC31" : "", "LZMA_DEC32", + ph.u_len > 0xffff ? "LZMA_DEC33" : "", nullptr); addLoaderEpilogue(flag); defineDecompressorSymbols(); @@ -196,9 +163,10 @@ void PackExe::buildLoader(const Filter *) MemBuffer compressed_lzma; compressed_lzma.allocForCompression(lsize); unsigned c_len_lzma = MemBuffer::getSizeForCompression(lsize); - int r = upx_compress(loader, lsize, compressed_lzma, &c_len_lzma, - nullptr, M_NRV2B_LE16, 9, nullptr, nullptr); - assert(r == UPX_E_OK); assert(c_len_lzma < lsize); + int r = upx_compress(loader, lsize, compressed_lzma, &c_len_lzma, nullptr, M_NRV2B_LE16, 9, + nullptr, nullptr); + assert(r == UPX_E_OK); + assert(c_len_lzma < lsize); info("lzma+relocator code compressed: %u -> %u", lsize, c_len_lzma); // reinit the loader @@ -208,61 +176,33 @@ void PackExe::buildLoader(const Filter *) addLoader("DEVICEENTRY,LZMADEVICE,DEVICEENTRY2", nullptr); linker->addSection("COMPRESSED_LZMA", compressed_lzma, c_len_lzma, 0); - addLoader("LZMAENTRY,NRV2B160,NRVDDONE,NRVDECO1,NRVGTD00,NRVDECO2", - nullptr); + addLoader("LZMAENTRY,NRV2B160,NRVDDONE,NRVDECO1,NRVGTD00,NRVDECO2", nullptr); - } - else if (device_driver) + } else if (device_driver) addLoader("DEVICEENTRY,DEVICEENTRY2", nullptr); - addLoader("EXEENTRY", - M_IS_LZMA(ph.method) && device_driver ? "LONGSUB" : "SHORTSUB", - "JNCDOCOPY", - relocsize ? "EXERELPU" : "", - "EXEMAIN4", - M_IS_LZMA(ph.method) ? "" : "EXEMAIN4B", - "EXEMAIN4C", + addLoader("EXEENTRY", M_IS_LZMA(ph.method) && device_driver ? "LONGSUB" : "SHORTSUB", + "JNCDOCOPY", relocsize ? "EXERELPU" : "", "EXEMAIN4", + M_IS_LZMA(ph.method) ? "" : "EXEMAIN4B", "EXEMAIN4C", M_IS_LZMA(ph.method) ? "COMPRESSED_LZMA_START,COMPRESSED_LZMA" : "", - "+G5DXXXX,UPX1HEAD,EXECUTPO", - nullptr - ); + "+G5DXXXX,UPX1HEAD,EXECUTPO", nullptr); if (ph.method == M_NRV2B_8) - addLoader("NRV2B16S", // decompressor - ph.u_len > DI_LIMIT ? "N2B64K01" : "", - "NRV2BEX1", - opt->cpu == opt->CPU_8086 ? "N2BX8601" : "N2B28601", - "NRV2BEX2", - opt->cpu == opt->CPU_8086 ? "N2BX8602" : "N2B28602", - "NRV2BEX3", - ph.c_len > 0xffff ? "N2B64K02" : "", - "NRV2BEX9", - nullptr - ); + addLoader("NRV2B16S", // decompressor + ph.u_len > DI_LIMIT ? "N2B64K01" : "", "NRV2BEX1", + opt->cpu == opt->CPU_8086 ? "N2BX8601" : "N2B28601", "NRV2BEX2", + opt->cpu == opt->CPU_8086 ? "N2BX8602" : "N2B28602", "NRV2BEX3", + ph.c_len > 0xffff ? "N2B64K02" : "", "NRV2BEX9", nullptr); else if (ph.method == M_NRV2D_8) - addLoader("NRV2D16S", - ph.u_len > DI_LIMIT ? "N2D64K01" : "", - "NRV2DEX1", - opt->cpu == opt->CPU_8086 ? "N2DX8601" : "N2D28601", - "NRV2DEX2", - opt->cpu == opt->CPU_8086 ? "N2DX8602" : "N2D28602", - "NRV2DEX3", - ph.c_len > 0xffff ? "N2D64K02" : "", - "NRV2DEX9", - nullptr - ); + addLoader("NRV2D16S", ph.u_len > DI_LIMIT ? "N2D64K01" : "", "NRV2DEX1", + opt->cpu == opt->CPU_8086 ? "N2DX8601" : "N2D28601", "NRV2DEX2", + opt->cpu == opt->CPU_8086 ? "N2DX8602" : "N2D28602", "NRV2DEX3", + ph.c_len > 0xffff ? "N2D64K02" : "", "NRV2DEX9", nullptr); else if (ph.method == M_NRV2E_8) - addLoader("NRV2E16S", - ph.u_len > DI_LIMIT ? "N2E64K01" : "", - "NRV2EEX1", - opt->cpu == opt->CPU_8086 ? "N2EX8601" : "N2E28601", - "NRV2EEX2", - opt->cpu == opt->CPU_8086 ? "N2EX8602" : "N2E28602", - "NRV2EEX3", - ph.c_len > 0xffff ? "N2E64K02" : "", - "NRV2EEX9", - nullptr - ); - else if M_IS_LZMA(ph.method) + addLoader("NRV2E16S", ph.u_len > DI_LIMIT ? "N2E64K01" : "", "NRV2EEX1", + opt->cpu == opt->CPU_8086 ? "N2EX8601" : "N2E28601", "NRV2EEX2", + opt->cpu == opt->CPU_8086 ? "N2EX8602" : "N2E28602", "NRV2EEX3", + ph.c_len > 0xffff ? "N2E64K02" : "", "NRV2EEX9", nullptr); + else if M_IS_LZMA (ph.method) return; else throwInternalError("unknown compression method"); @@ -270,25 +210,22 @@ void PackExe::buildLoader(const Filter *) addLoaderEpilogue(flag); } - /************************************************************************* // **************************************************************************/ -int PackExe::readFileHeader() -{ +int PackExe::readFileHeader() { ih_exesize = ih_imagesize = ih_overlay = 0; - fi->readx(&ih,sizeof(ih)); - if (ih.ident != 'M' + 'Z'*256 && ih.ident != 'Z' + 'M'*256) + fi->readx(&ih, sizeof(ih)); + if (ih.ident != 'M' + 'Z' * 256 && ih.ident != 'Z' + 'M' * 256) return 0; - ih_exesize = ih.m512 + ih.p512*512 - (ih.m512 ? 512 : 0); + ih_exesize = ih.m512 + ih.p512 * 512 - (ih.m512 ? 512 : 0); if (!ih_exesize) { ih_exesize = file_size; } - ih_imagesize = ih_exesize - ih.headsize16*16; + ih_imagesize = ih_exesize - ih.headsize16 * 16; ih_overlay = file_size - ih_exesize; - if (file_size_u < sizeof(ih) - || ((ih.m512 | ih.p512) && ih.m512+ih.p512*512u < sizeof (ih))) + if (file_size_u < sizeof(ih) || ((ih.m512 | ih.p512) && ih.m512 + ih.p512 * 512u < sizeof(ih))) throwCantPack("illegal exe header"); if (file_size_u < ih_exesize || ih_imagesize <= 0 || ih_imagesize > ih_exesize) throwCantPack("exe header corrupted"); @@ -298,20 +235,17 @@ int PackExe::readFileHeader() return UPX_F_DOS_EXE; } - -bool PackExe::canPack() -{ - if (fn_has_ext(fi->getName(),"sys")) +bool PackExe::canPack() { + if (fn_has_ext(fi->getName(), "sys")) return false; if (!readFileHeader()) return false; if (file_size < 1024) throwCantPack("file is too small"); - fi->seek(0x3c,SEEK_SET); + fi->seek(0x3c, SEEK_SET); LE32 offs; - fi->readx(&offs,sizeof (offs)); - if (ih.relocoffs >= 0x40 && offs) - { + fi->readx(&offs, sizeof(offs)); + if (ih.relocoffs >= 0x40 && offs) { if (opt->dos_exe.force_stub) opt->overlay = opt->COPY_OVERLAY; else @@ -320,20 +254,16 @@ bool PackExe::canPack() return true; } - /************************************************************************* // **************************************************************************/ -static -unsigned optimize_relocs(upx_byte *b, const unsigned size, - const upx_byte *relocs, const unsigned nrelocs, - upx_byte *crel, bool *has_9a) -{ +static unsigned optimize_relocs(upx_byte *b, const unsigned size, const upx_byte *relocs, + const unsigned nrelocs, upx_byte *crel, bool *has_9a) { if (opt->exact) throwCantPackExact(); - upx_byte * const crel_save = crel; + upx_byte *const crel_save = crel; unsigned i; unsigned seg_high = 0; #if 0 @@ -345,14 +275,12 @@ unsigned optimize_relocs(upx_byte *b, const unsigned size, #endif // pass 1 - find 0x9a bounds - for (i = 0; i < nrelocs; i++) - { - unsigned addr = get_le32(relocs+4*i); + for (i = 0; i < nrelocs; i++) { + unsigned addr = get_le32(relocs + 4 * i); if (addr >= size - 1) throwCantPack("unexpected relocation 1"); - if (addr >= 3 && b[addr-3] == 0x9a) - { - unsigned seg = get_le16(b+addr); + if (addr >= 3 && b[addr - 3] == 0x9a) { + unsigned seg = get_le16(b + addr); if (seg > seg_high) seg_high = seg; #if 0 @@ -371,10 +299,9 @@ unsigned optimize_relocs(upx_byte *b, const unsigned size, #endif } } - //printf("%d %d\n", seg_low, seg_high); - //printf("%d %d\n", off_low, off_high); - //printf("%d %d\n", linear_low, linear_high); - + // printf("%d %d\n", seg_low, seg_high); + // printf("%d %d\n", off_low, off_high); + // printf("%d %d\n", linear_low, linear_high); // pass 2 - reloc @@ -383,37 +310,30 @@ unsigned optimize_relocs(upx_byte *b, const unsigned size, unsigned ones = 0; unsigned es = 0, di, t; i = 0; - do - { - unsigned addr = get_le32(relocs+4*i); - set_le16(crel,di = addr & 0x0f); - set_le16(crel+2,(addr >> 4) - es); + do { + unsigned addr = get_le32(relocs + 4 * i); + set_le16(crel, di = addr & 0x0f); + set_le16(crel + 2, (addr >> 4) - es); es = addr >> 4; crel += 4; - for (++i; i < nrelocs; i++) - { - addr = get_le32(relocs+4*i); - //printf ("%x\n",es*16+di); - if ((addr - es*16 > 0xfffe) - || (i == nrelocs - 1 && addr - es * 16 > 0xff00) - ) - { + for (++i; i < nrelocs; i++) { + addr = get_le32(relocs + 4 * i); + // printf ("%x\n",es*16+di); + if ((addr - es * 16 > 0xfffe) || (i == nrelocs - 1 && addr - es * 16 > 0xff00)) { // segment change - t = 1+(0xffff-di)/254; - memset(crel,1,t); + t = 1 + (0xffff - di) / 254; + memset(crel, 1, t); crel += t; - ones += t-1; // -1 is used to help the assembly stuff + ones += t - 1; // -1 is used to help the assembly stuff break; } - unsigned offs = addr - es*16; - if (offs >= 3 && b[es*16 + offs-3] == 0x9a && offs > di + 3) - { - for (t = di; t < offs-3; t++) - if (b[es*16+t] == 0x9a && get_le16(b+es*16+t+3) <= seg_high) + unsigned offs = addr - es * 16; + if (offs >= 3 && b[es * 16 + offs - 3] == 0x9a && offs > di + 3) { + for (t = di; t < offs - 3; t++) + if (b[es * 16 + t] == 0x9a && get_le16(b + es * 16 + t + 3) <= seg_high) break; - if (t == offs-3) - { + if (t == offs - 3) { // code 0: search for 0x9a *crel++ = 0; di = offs; @@ -425,8 +345,7 @@ unsigned optimize_relocs(upx_byte *b, const unsigned size, if (t < 2) throwCantPack("unexpected relocation 2"); - while (t >= 256) - { + while (t >= 256) { // code 1: add 254, don't reloc *crel++ = 1; t -= 254; @@ -438,20 +357,18 @@ unsigned optimize_relocs(upx_byte *b, const unsigned size, } while (i < nrelocs); *crel++ = 1; ones++; - set_le16 (crel_save,ones); - set_le16 (crel_save+2,seg_high); + set_le16(crel_save, ones); + set_le16(crel_save + 2, seg_high); - //OutputFile::dump("x.rel", crel_save, crel - crel_save); + // OutputFile::dump("x.rel", crel_save, crel - crel_save); return (unsigned) (crel - crel_save); } - /************************************************************************* // **************************************************************************/ -void PackExe::pack(OutputFile *fo) -{ +void PackExe::pack(OutputFile *fo) { unsigned ic; if (ih.relocs > MAXRELOCS) @@ -459,13 +376,13 @@ void PackExe::pack(OutputFile *fo) checkOverlay(ih_overlay); // alloc buffers - relocsize = RSFCRI + 4*ih.relocs; - ibuf.alloc(ih_imagesize+16+relocsize+2); - obuf.allocForCompression(ih_imagesize+16+relocsize+2); + relocsize = RSFCRI + 4 * ih.relocs; + ibuf.alloc(ih_imagesize + 16 + relocsize + 2); + obuf.allocForCompression(ih_imagesize + 16 + relocsize + 2); // read image - fi->seek(ih.headsize16*16,SEEK_SET); - fi->readx(ibuf,ih_imagesize); + fi->seek(ih.headsize16 * 16, SEEK_SET); + fi->readx(ibuf, ih_imagesize); checkAlreadyPacked(ibuf, UPX_MIN(ih_imagesize, 127u)); @@ -474,21 +391,19 @@ void PackExe::pack(OutputFile *fo) // relocations has_9a = false; upx_byte *w = ibuf + ih_imagesize; - if (ih.relocs) - { + if (ih.relocs) { upx_byte *wr = w + RSFCRI; - fi->seek(ih.relocoffs,SEEK_SET); - fi->readx(wr,4*ih.relocs); + fi->seek(ih.relocoffs, SEEK_SET); + fi->readx(wr, 4 * ih.relocs); - for (ic = 0; ic < ih.relocs; ic++) - { - unsigned jc = get_le32(wr+4*ic); - set_le32(wr+4*ic, ((jc>>16)*16+(jc&0xffff)) & 0xfffff); + for (ic = 0; ic < ih.relocs; ic++) { + unsigned jc = get_le32(wr + 4 * ic); + set_le32(wr + 4 * ic, ((jc >> 16) * 16 + (jc & 0xffff)) & 0xfffff); } - qsort(wr,ih.relocs,4,le32_compare); + qsort(wr, ih.relocs, 4, le32_compare); relocsize = optimize_relocs(ibuf, ih_imagesize, wr, ih.relocs, w, &has_9a); - set_le16(w+relocsize, relocsize+2); + set_le16(w + relocsize, relocsize + 2); relocsize += 2; if (relocsize > MAXRELOCS) throwCantPack("too many relocations"); @@ -499,9 +414,7 @@ void PackExe::pack(OutputFile *fo) ucl_nrv2b_99_compress(w, in_len, out, &out_len, nullptr, 9, nullptr, nullptr); printf("reloc compress: %d -> %d\n", in_len, out_len); #endif - } - else - { + } else { relocsize = 0; } @@ -510,7 +423,8 @@ void PackExe::pack(OutputFile *fo) // prepare filter Filter ft(ph.level); // compress (max_match = 8192) - upx_compress_config_t cconf; cconf.reset(); + upx_compress_config_t cconf; + cconf.reset(); cconf.conf_ucl.max_match = MAXMATCH; cconf.conf_lzma.max_num_probs = 1846 + (768 << 4); // ushort: ~28 KiB stack compressWithFilters(&ft, 32, &cconf); @@ -520,13 +434,11 @@ void PackExe::pack(OutputFile *fo) throwCantPack("decompressor limit exceeded, send a bugreport"); #if TESTING - if (opt->debug.debug_level) - { - printf("image+relocs %d -> %d\n",ih_imagesize+relocsize,ph.c_len); - printf("offsets: %d - %d\nmatches: %d - %d\nruns: %d - %d\n", - 0/*ph.min_offset_found*/,ph.max_offset_found, - 0/*ph.min_match_found*/,ph.max_match_found, - 0/*ph.min_run_found*/,ph.max_run_found); + if (opt->debug.debug_level) { + printf("image+relocs %d -> %d\n", ih_imagesize + relocsize, ph.c_len); + printf("offsets: %d - %d\nmatches: %d - %d\nruns: %d - %d\n", 0 /*ph.min_offset_found*/, + ph.max_offset_found, 0 /*ph.min_match_found*/, ph.max_match_found, + 0 /*ph.min_run_found*/, ph.max_run_found); } #endif @@ -534,22 +446,22 @@ void PackExe::pack(OutputFile *fo) const unsigned lsize = getLoaderSize(); MemBuffer loader(lsize); - memcpy(loader,getLoader(),lsize); - //OutputFile::dump("xxloader.dat", loader, lsize); + memcpy(loader, getLoader(), lsize); + // OutputFile::dump("xxloader.dat", loader, lsize); // patch loader const unsigned packedsize = ph.c_len; const unsigned e_len = getLoaderSectionStart("EXECUTPO"); const unsigned d_len = lsize - e_len; - assert((e_len&15) == 0); + assert((e_len & 15) == 0); - const unsigned copysize = (1+packedsize+d_len) & ~1; - const unsigned firstcopy = copysize%0x10000 ? copysize%0x10000 : 0x10000; + const unsigned copysize = (1 + packedsize + d_len) & ~1; + const unsigned firstcopy = copysize % 0x10000 ? copysize % 0x10000 : 0x10000; // set oh.min & oh.max - ic = ih.min*16 + ih_imagesize; - if (ic < oh.ss*16u + oh.sp) - ic = oh.ss*16u + oh.sp; + ic = ih.min * 16 + ih_imagesize; + if (ic < oh.ss * 16u + oh.sp) + ic = oh.ss * 16u + oh.sp; oh.min = (ic - (packedsize + lsize)) / 16; ic = oh.min + (ih.max - ih.min); oh.max = ic < 0xffff && ih.max != 0xffff ? ic : 0xffff; @@ -557,27 +469,23 @@ void PackExe::pack(OutputFile *fo) // set extra info unsigned char extra_info[9]; unsigned eisize = 0; - if (oh.ss != ih.ss) - { - set_le16(extra_info+eisize,ih.ss); + if (oh.ss != ih.ss) { + set_le16(extra_info + eisize, ih.ss); eisize += 2; - assert((flag & SS) != 0); // set in fillExeHeader() + assert((flag & SS) != 0); // set in fillExeHeader() } - if (oh.sp != ih.sp) - { - set_le16(extra_info+eisize,ih.sp); + if (oh.sp != ih.sp) { + set_le16(extra_info + eisize, ih.sp); eisize += 2; - assert((flag & SP) != 0); // set in fillExeHeader() + assert((flag & SP) != 0); // set in fillExeHeader() } - if (ih.min != oh.min) - { - set_le16(extra_info+eisize,ih.min); + if (ih.min != oh.min) { + set_le16(extra_info + eisize, ih.min); eisize += 2; flag |= MINMEM; } - if (ih.max != oh.max) - { - set_le16(extra_info+eisize,ih.max); + if (ih.max != oh.max) { + set_le16(extra_info + eisize, ih.max); eisize += 2; flag |= MAXMEM; } @@ -592,28 +500,25 @@ void PackExe::pack(OutputFile *fo) linker->defineSymbol("decompressor_entry", decompressor_entry); // patch loader - if (flag & USEJUMP) - { + if (flag & USEJUMP) { // I use a relocation entry to set the original cs unsigned n = getLoaderSectionStart("EXEJUMPF") + 1; n += packedsize + 2; oh.relocs = 1; oh.firstreloc = (n & 0xf) + ((n >> 4) << 16); - } - else - { + } else { oh.relocs = 0; oh.firstreloc = ih.cs * 0x10000 + ih.ip; } // g++ 3.1 does not like the following line... oh.relocoffs = offsetof(exe_header_t, firstreloc); - //oh.relocoffs = ptr_udiff_bytes(&oh.firstreloc, &oh); + // oh.relocoffs = ptr_udiff_bytes(&oh.firstreloc, &oh); linker->defineSymbol("destination_segment", oh.ss - ph.c_len / 16 - e_len / 16); linker->defineSymbol("source_segment", e_len / 16 + (copysize - firstcopy) / 16); linker->defineSymbol("copy_offset", firstcopy - 2); - linker->defineSymbol("words_to_copy",firstcopy / 2); + linker->defineSymbol("words_to_copy", firstcopy / 2); linker->defineSymbol("exe_stack_sp", oh.sp); linker->defineSymbol("exe_stack_ss", oh.ss); @@ -621,7 +526,7 @@ void PackExe::pack(OutputFile *fo) linker->defineSymbol("attribute", get_le16(ibuf + 4)); linker->defineSymbol("orig_strategy", get_le16(ibuf + 6)); - const unsigned outputlen = sizeof(oh)+lsize+packedsize+eisize; + const unsigned outputlen = sizeof(oh) + lsize + packedsize + eisize; oh.m512 = outputlen & 511; oh.p512 = (outputlen + 511) >> 9; @@ -631,20 +536,21 @@ void PackExe::pack(OutputFile *fo) defineDecompressorSymbols(); relocateLoader(); memcpy(loader, getLoader(), lsize); - patchPackHeader(loader,e_len); + patchPackHeader(loader, e_len); -//fprintf(stderr,"\ne_len=%x d_len=%x c_len=%x oo=%x ulen=%x destp=%x copys=%x images=%x",e_len,d_len,packedsize,ph.overlap_overhead,ph.u_len,destpara,copysize,ih_imagesize); + // fprintf(stderr,"\ne_len=%x d_len=%x c_len=%x oo=%x ulen=%x destp=%x copys=%x + // images=%x",e_len,d_len,packedsize,ph.overlap_overhead,ph.u_len,destpara,copysize,ih_imagesize); // write header + write loader + compressed file #if TESTING if (opt->debug.debug_level) - printf("\n%d %d %d %d\n",(int)sizeof(oh),e_len,packedsize,d_len); + printf("\n%d %d %d %d\n", (int) sizeof(oh), e_len, packedsize, d_len); #endif - fo->write(&oh,sizeof(oh)); - fo->write(loader,e_len); // entry - fo->write(obuf,packedsize); - fo->write(loader+e_len,d_len); // decompressor - fo->write(extra_info,eisize); + fo->write(&oh, sizeof(oh)); + fo->write(loader, e_len); // entry + fo->write(obuf, packedsize); + fo->write(loader + e_len, d_len); // decompressor + fo->write(extra_info, eisize); assert(eisize <= 9); #if 0 printf("%-13s: program hdr : %8ld bytes\n", getName(), (long) sizeof(oh)); @@ -659,20 +565,18 @@ void PackExe::pack(OutputFile *fo) // copy the overlay copyOverlay(fo, ih_overlay, obuf); -//fprintf (stderr,"%x %x\n",relocsize,ph.u_len); + // fprintf (stderr,"%x %x\n",relocsize,ph.u_len); // finally check the compression ratio if (!checkFinalCompressionRatio(fo)) throwNotCompressible(); } - /************************************************************************* // **************************************************************************/ -int PackExe::canUnpack() -{ +int PackExe::canUnpack() { if (!readFileHeader()) return false; const unsigned off = ih.headsize16 * 16; @@ -681,19 +585,17 @@ int PackExe::canUnpack() return b && (off + ph.c_len <= file_size_u); } - /************************************************************************* // **************************************************************************/ -void PackExe::unpack(OutputFile *fo) -{ +void PackExe::unpack(OutputFile *fo) { ibuf.alloc(file_size); obuf.allocForDecompression(ph.u_len); // read the file - fi->seek(ih.headsize16*16,SEEK_SET); - fi->readx(ibuf,ih_imagesize); + fi->seek(ih.headsize16 * 16, SEEK_SET); + fi->readx(ibuf, ih_imagesize); // get compressed data offset unsigned e_len = ph.buf_offset + ph.getPackHeaderSize(); @@ -703,7 +605,7 @@ void PackExe::unpack(OutputFile *fo) checkOverlay(ih_overlay); // decompress - decompress(ibuf+e_len,obuf); + decompress(ibuf + e_len, obuf); unsigned imagesize = ih_imagesize; imagesize--; @@ -713,82 +615,81 @@ void PackExe::unpack(OutputFile *fo) SPAN_S_VAR(upx_byte, relocs, obuf + ph.u_len, obuf); MemBuffer wrkmem; - if (!(flag & NORELOC)) - { - relocs -= get_le16(obuf+ph.u_len-2); + if (!(flag & NORELOC)) { + relocs -= get_le16(obuf + ph.u_len - 2); ph.u_len -= 2; - wrkmem.alloc(4*MAXRELOCS); + wrkmem.alloc(4 * MAXRELOCS); unsigned es = 0, ones = get_le16(relocs); - const unsigned seghi = get_le16(relocs+2); + const unsigned seghi = get_le16(relocs + 2); SPAN_S_VAR(const upx_byte, p, relocs + 4); - while (ones) - { + while (ones) { unsigned di = get_le16(p); - es += get_le16(p+2); + es += get_le16(p + 2); bool dorel = true; - for (p += 4; ones && di < 0x10000; p++) - { - if (dorel) - { - set_le16(wrkmem+4*relocn,di); - set_le16(wrkmem+2+4*relocn++,es); - //printf ("%x\n",es*16+di); + for (p += 4; ones && di < 0x10000; p++) { + if (dorel) { + set_le16(wrkmem + 4 * relocn, di); + set_le16(wrkmem + 2 + 4 * relocn++, es); + // printf ("%x\n",es*16+di); } dorel = true; - if (*p == 0) - { + if (*p == 0) { SPAN_S_VAR(const upx_byte, q, obuf); - for (q = obuf+es*16+di; !(*q == 0x9a && get_le16(q+3) <= seghi); q++) + for (q = obuf + es * 16 + di; !(*q == 0x9a && get_le16(q + 3) <= seghi); q++) ; - di = ptr_diff_bytes(q, obuf+es*16) + 3; - } - else if (*p == 1) - { + di = ptr_diff_bytes(q, obuf + es * 16) + 3; + } else if (*p == 1) { di += 254; if (di < 0x10000) ones--; dorel = false; - } - else + } else di += *p; } } } // fill new exe header - memset(&oh,0,sizeof(oh)); - oh.ident = 'M' + 'Z'*256; + memset(&oh, 0, sizeof(oh)); + oh.ident = 'M' + 'Z' * 256; - if (relocn) - { + if (relocn) { oh.relocs = relocn; while (relocn & 3) - set_le32(wrkmem+4*relocn++,0); + set_le32(wrkmem + 4 * relocn++, 0); } - unsigned outputlen = ptr_udiff_bytes(relocs, obuf) + sizeof(oh) + relocn*4; + unsigned outputlen = ptr_udiff_bytes(relocs, obuf) + sizeof(oh) + relocn * 4; oh.m512 = outputlen & 511; oh.p512 = (outputlen + 511) >> 9; - oh.headsize16 = 2+relocn/4; + oh.headsize16 = 2 + relocn / 4; oh.max = ih.max; oh.min = ih.min; oh.sp = ih.sp; oh.ss = ih.ss; - if (flag & MAXMEM) - { imagesize -= 2; oh.max = get_le16(ibuf+imagesize); } - if (flag & MINMEM) - { imagesize -= 2; oh.min = get_le16(ibuf+imagesize); } - if (flag & SP) - { imagesize -= 2; oh.sp = get_le16(ibuf+imagesize); } - if (flag & SS) - { imagesize -= 2; oh.ss = get_le16(ibuf+imagesize); } + if (flag & MAXMEM) { + imagesize -= 2; + oh.max = get_le16(ibuf + imagesize); + } + if (flag & MINMEM) { + imagesize -= 2; + oh.min = get_le16(ibuf + imagesize); + } + if (flag & SP) { + imagesize -= 2; + oh.sp = get_le16(ibuf + imagesize); + } + if (flag & SS) { + imagesize -= 2; + oh.ss = get_le16(ibuf + imagesize); + } - unsigned ip = (flag & USEJUMP) ? get_le32(ibuf+imagesize-4) : (unsigned) ih.firstreloc; + unsigned ip = (flag & USEJUMP) ? get_le32(ibuf + imagesize - 4) : (unsigned) ih.firstreloc; oh.ip = ip & 0xffff; oh.cs = ip >> 16; @@ -798,20 +699,16 @@ void PackExe::unpack(OutputFile *fo) return; // write header + relocations + uncompressed file - fo->write(&oh,sizeof(oh)); + fo->write(&oh, sizeof(oh)); if (relocn) - fo->write(wrkmem,relocn*4); + fo->write(wrkmem, relocn * 4); fo->write(obuf, ptr_diff_bytes(relocs, obuf)); // copy the overlay copyOverlay(fo, ih_overlay, obuf); } - -Linker* PackExe::newLinker() const -{ - return new ElfLinkerX86(); -} +Linker *PackExe::newLinker() const { return new ElfLinkerX86(); } /* diff --git a/src/p_exe.h b/src/p_exe.h index 9d1dca81..4f7d1095 100644 --- a/src/p_exe.h +++ b/src/p_exe.h @@ -25,24 +25,22 @@ */ - -#ifndef __UPX_P_EXE_H -#define __UPX_P_EXE_H 1 - +#pragma once +#ifndef UPX_P_EXE_H__ +#define UPX_P_EXE_H__ 1 /************************************************************************* // dos/exe **************************************************************************/ -class PackExe final : public Packer -{ +class PackExe final : public Packer { typedef Packer super; + public: PackExe(InputFile *f); virtual int getVersion() const override { return 13; } virtual int getFormat() const override { return UPX_F_DOS_EXE; } virtual const char *getName() const override { return "dos/exe"; } - //virtual const char *getFullName(const options_t *o) const { return o && o->cpu == o->CPU_8086 ? "i086-dos16.exe" : "i286-dos16.exe"; } virtual const char *getFullName(const options_t *) const override { return "i086-dos16.exe"; } virtual const int *getCompressionMethods(int method, int level) const override; virtual const int *getFilters() const override; @@ -54,13 +52,11 @@ public: virtual int canUnpack() override; // unpacker capabilities - virtual bool canUnpackVersion(int version) const override - { + virtual bool canUnpackVersion(int version) const override { // NOTE: could adapt p_exe.cpp to support (version >= 8) return (version >= 10); } - virtual bool canUnpackFormat(int format) const override - { + virtual bool canUnpackFormat(int format) const override { return (format == UPX_F_DOS_EXE || format == UPX_F_DOS_EXEH); } @@ -71,10 +67,10 @@ protected: virtual int fillExeHeader(struct exe_header_t *) const; virtual void buildLoader(const Filter *ft) override; - virtual Linker* newLinker() const override; + virtual Linker *newLinker() const override; void addLoaderEpilogue(int flag); - __packed_struct(exe_header_t) + struct alignas(1) exe_header_t { LE16 ident; LE16 m512; LE16 p512; @@ -84,13 +80,13 @@ protected: LE16 max; LE16 ss; LE16 sp; - char _[2]; // checksum + char _[2]; // checksum LE16 ip; LE16 cs; LE16 relocoffs; - char __[2]; // overlnum + char __[2]; // overlnum LE32 firstreloc; - __packed_struct_end() + }; exe_header_t ih, oh; @@ -102,20 +98,12 @@ protected: bool has_9a; bool device_driver; - enum { - NORELOC = 1, - USEJUMP = 2, - SS = 4, - SP = 8, - MINMEM = 16, - MAXMEM = 32 - }; + enum { NORELOC = 1, USEJUMP = 2, SS = 4, SP = 8, MINMEM = 16, MAXMEM = 32 }; - unsigned stack_for_lzma; // stack size required for lzma + unsigned stack_for_lzma; // stack size required for lzma bool use_clear_dirty_stack; }; - #endif /* already included */ /* vim:set ts=4 sw=4 et: */ diff --git a/src/p_ps1.cpp b/src/p_ps1.cpp index 9630ba9d..4a55db02 100644 --- a/src/p_ps1.cpp +++ b/src/p_ps1.cpp @@ -29,7 +29,6 @@ */ - #include "conf.h" #include "file.h" #include "filter.h" @@ -37,30 +36,29 @@ #include "p_ps1.h" #include "linker.h" -static const +static const CLANG_FORMAT_DUMMY_STATEMENT #include "stub/mipsel.r3000-ps1.h" -#define CD_SEC 2048 -#define PS_HDR_SIZE CD_SEC -#define PS_RAM_SIZE ram_size -#define PS_MIN_SIZE (PS_HDR_SIZE*3) -#define PS_MAX_SIZE ((PS_RAM_SIZE*95) / 100) -#define PS_STACK_SIZE (PS_RAM_SIZE / 256) +#define CD_SEC 2048 +#define PS_HDR_SIZE CD_SEC +#define PS_RAM_SIZE ram_size +#define PS_MIN_SIZE (PS_HDR_SIZE * 3) +#define PS_MAX_SIZE ((PS_RAM_SIZE * 95) / 100) +#define PS_STACK_SIZE (PS_RAM_SIZE / 256) -#define SZ_IH_BKUP (10 * sizeof(LE32)) -#define HD_CODE_OFS (sizeof(ps1_exe_t) + sz_cbh) +#define SZ_IH_BKUP (10 * sizeof(LE32)) +#define HD_CODE_OFS (sizeof(ps1_exe_t) + sz_cbh) -#define K0_BS (0x80000000) -#define K1_BS (0xa0000000) -#define EXE_BS (ih.epc & K0_BS) -#define FIX_PSVR ((K1_BS - EXE_BS) + (PS_HDR_SIZE - HD_CODE_OFS)) +#define K0_BS (0x80000000) +#define K1_BS (0xa0000000) +#define EXE_BS (ih.epc & K0_BS) +#define FIX_PSVR ((K1_BS - EXE_BS) + (PS_HDR_SIZE - HD_CODE_OFS)) // lui / addiu -#define MIPS_HI(a) (((a) >> 16) + (((a) & 0x8000) >> 15)) -#define MIPS_LO(a) ((a) & 0xffff) -#define MIPS_PC16(a) ((a) >> 2) -#define MIPS_PC26(a) (((a) & 0x0fffffff) >> 2) - +#define MIPS_HI(a) (((a) >> 16) + (((a) &0x8000) >> 15)) +#define MIPS_LO(a) ((a) &0xffff) +#define MIPS_PC16(a) ((a) >> 2) +#define MIPS_PC26(a) (((a) &0x0fffffff) >> 2) /************************************************************************* // ps1 exe looks like this: @@ -76,12 +74,10 @@ static const // optional: not aligned to 2048 (for console run only) **************************************************************************/ -PackPs1::PackPs1(InputFile *f) : - super(f), - isCon(!opt->ps1_exe.boot_only), is32Bit(!opt->ps1_exe.do_8bit), - buildPart2(0), foundBss(0), sa_cnt(0), overlap(0), sz_lunc(0), sz_lcpr(0), - pad_code(0), bss_start(0), bss_end(0) -{ +PackPs1::PackPs1(InputFile *f) + : super(f), isCon(!opt->ps1_exe.boot_only), is32Bit(!opt->ps1_exe.do_8bit), buildPart2(0), + foundBss(0), sa_cnt(0), overlap(0), sz_lunc(0), sz_lcpr(0), pad_code(0), bss_start(0), + bss_end(0) { bele = &N_BELE_RTP::le_policy; COMPILE_TIME_ASSERT(sizeof(ps1_exe_t) == 136) @@ -98,24 +94,16 @@ PackPs1::PackPs1(InputFile *f) : ram_size = !opt->ps1_exe.do_8mib ? 0x200000 : 0x800000; } -const int *PackPs1::getCompressionMethods(int method, int level) const -{ +const int *PackPs1::getCompressionMethods(int method, int level) const { if (is32Bit) return Packer::getDefaultCompressionMethods_le32(method, level); else return Packer::getDefaultCompressionMethods_8(method, level); } -const int *PackPs1::getFilters() const -{ - return nullptr; -} - -Linker* PackPs1::newLinker() const -{ - return new ElfLinkerMipsLE; -} +const int *PackPs1::getFilters() const { return nullptr; } +Linker *PackPs1::newLinker() const { return new ElfLinkerMipsLE; } /************************************************************************* // util @@ -123,116 +111,105 @@ Linker* PackPs1::newLinker() const // checkFileHeader() checks ih for legal but unsupported values **************************************************************************/ -int PackPs1::readFileHeader() -{ +int PackPs1::readFileHeader() { fi->seek(0, SEEK_SET); fi->readx(&ih, sizeof(ih)); - if (memcmp(&ih.id[0], "PS-X EXE", 8) != 0 && - memcmp(&ih.id[0], "EXE X-SP", 8) != 0) + if (memcmp(&ih.id[0], "PS-X EXE", 8) != 0 && memcmp(&ih.id[0], "EXE X-SP", 8) != 0) return 0; if (ih.text != 0 || ih.data != 0) return 0; return UPX_F_PS1_EXE; } -bool PackPs1::readBkupHeader() -{ - fi->seek(sizeof(ps1_exe_t)+8, SEEK_SET); +bool PackPs1::readBkupHeader() { + fi->seek(sizeof(ps1_exe_t) + 8, SEEK_SET); fi->readx(&bh, sizeof(bh)); - if (bh.ih_csum != upx_adler32(&bh, SZ_IH_BKUP)) - { + if (bh.ih_csum != upx_adler32(&bh, SZ_IH_BKUP)) { unsigned char buf[sizeof(bh)]; fi->seek(sizeof(ps1_exe_t), SEEK_SET); fi->readx(buf, sizeof(bh)); - if (!getBkupHeader(buf, (unsigned char *)&bh)) + if (!getBkupHeader(buf, (unsigned char *) &bh)) return false; } return true; } -#define INIT_BH_BKUP(p, l) ACC_BLOCK_BEGIN {(p)->id = '1'; (p)->len = l;} ACC_BLOCK_END -#define ADLER16(a) (((a) >> 16) ^ ((a) & 0xffff)) +#define INIT_BH_BKUP(p, l) \ + ACC_BLOCK_BEGIN \ + { \ + (p)->id = '1'; \ + (p)->len = l; \ + } \ + ACC_BLOCK_END +#define ADLER16(a) (((a) >> 16) ^ ((a) &0xffff)) -void PackPs1::putBkupHeader(const unsigned char *src, unsigned char *dst, unsigned *len) -{ +void PackPs1::putBkupHeader(const unsigned char *src, unsigned char *dst, unsigned *len) { unsigned sz_cbh = MemBuffer::getSizeForCompression(SZ_IH_BKUP); - if (src && dst) - { + if (src && dst) { unsigned char *cpr_bh = New(unsigned char, sz_cbh); memset(cpr_bh, 0, sizeof(bh)); - ps1_exe_chb_t * p = (ps1_exe_chb_t * )cpr_bh; + ps1_exe_chb_t *p = (ps1_exe_chb_t *) cpr_bh; - int r = upx_compress(src, SZ_IH_BKUP, - &p->ih_bkup, &sz_cbh, nullptr, M_NRV2E_8, 10, nullptr, nullptr ); + int r = upx_compress(src, SZ_IH_BKUP, &p->ih_bkup, &sz_cbh, nullptr, M_NRV2E_8, 10, nullptr, + nullptr); 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 + (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; - } - else + delete[] cpr_bh; + } else throwInternalError("header compression failed"); } -#define ADLER16_HI(a, b) ((((a) & 0xffff) ^ (b)) << 16) -#define ADLER16_LO(a, b) (((a) >> 16) ^ (b)) -#define RE_ADLER16(a, b) (ADLER16_HI(a,b) | ADLER16_LO(a,b)) +#define ADLER16_HI(a, b) ((((a) &0xffff) ^ (b)) << 16) +#define ADLER16_LO(a, b) (((a) >> 16) ^ (b)) +#define RE_ADLER16(a, b) (ADLER16_HI(a, b) | ADLER16_LO(a, b)) -bool PackPs1::getBkupHeader(unsigned char *p, unsigned char *dst) -{ - ps1_exe_chb_t *src = (ps1_exe_chb_t*)p; +bool PackPs1::getBkupHeader(unsigned char *p, unsigned char *dst) { + ps1_exe_chb_t *src = (ps1_exe_chb_t *) p; - if (src && (src->id == '1' && src->len < SZ_IH_BKUP) && dst) - { + if (src && (src->id == '1' && src->len < SZ_IH_BKUP) && dst) { unsigned char *unc_bh = New(unsigned char, MemBuffer::getSizeForDecompression(SZ_IH_BKUP)); unsigned sz_bh = SZ_IH_BKUP; - int r = upx_decompress((const unsigned char *)&src->ih_bkup, src->len, - unc_bh, &sz_bh, M_NRV2E_8, nullptr ); + int r = upx_decompress((const unsigned char *) &src->ih_bkup, src->len, unc_bh, &sz_bh, + M_NRV2E_8, nullptr); if (r == UPX_E_OUT_OF_MEMORY) throwOutOfMemoryException(); if (r != UPX_E_OK || sz_bh != SZ_IH_BKUP) throwInternalError("header decompression failed"); unsigned ad = upx_adler32(unc_bh, SZ_IH_BKUP); unsigned ch = src->ih_csum; - if (ad != RE_ADLER16(ad,ch)) + if (ad != RE_ADLER16(ad, ch)) throwInternalError("backup header damaged"); memcpy(dst, unc_bh, SZ_IH_BKUP); - delete [] unc_bh; - } - else + delete[] unc_bh; + } else return false; return true; } -bool PackPs1::checkFileHeader() -{ - if (fdata_size != ih.tx_len || (ih.tx_len & 3)) - { +bool PackPs1::checkFileHeader() { + if (fdata_size != ih.tx_len || (ih.tx_len & 3)) { if (!opt->force) throwCantPack("file size entry damaged (try --force)"); - else - { + else { opt->info_mode += !opt->info_mode ? 1 : 0; infoWarning("fixing damaged header, keeping backup file"); opt->backup = 1; ih.tx_len = fdata_size; } } - if (!opt->force && - (ih.da_ptr != 0 || ih.da_len != 0 || - ih.bs_ptr != 0 || ih.bs_len != 0)) - { + if (!opt->force && (ih.da_ptr != 0 || ih.da_len != 0 || ih.bs_ptr != 0 || ih.bs_len != 0)) { infoWarning("unsupported header field entry"); return false; } - if (ih.is_ptr < (EXE_BS | (PS_RAM_SIZE - PS_STACK_SIZE))) - { + if (ih.is_ptr < (EXE_BS | (PS_RAM_SIZE - PS_STACK_SIZE))) { if (!opt->force) return false; else @@ -241,13 +218,11 @@ bool PackPs1::checkFileHeader() return true; } - /************************************************************************* // **************************************************************************/ -bool PackPs1::canPack() -{ +bool PackPs1::canPack() { unsigned char buf[PS_HDR_SIZE - sizeof(ps1_exe_t)]; if (!readFileHeader()) @@ -257,12 +232,10 @@ bool PackPs1::canPack() checkAlreadyPacked(buf, sizeof(buf)); for (size_t i = 0; i < sizeof(buf); i++) - if (buf[i] != 0) - { + if (buf[i] != 0) { if (!opt->force) throwCantPack("unknown data in header (try --force)"); - else - { + else { opt->info_mode += !opt->info_mode ? 1 : 0; infoWarning("clearing header, keeping backup file"); opt->backup = 1; @@ -278,114 +251,88 @@ bool PackPs1::canPack() return true; } - /************************************************************************* // **************************************************************************/ -void PackPs1::buildLoader(const Filter *) -{ +void PackPs1::buildLoader(const Filter *) { const char *method = nullptr; if (ph.method == M_NRV2B_8) - method = isCon ? "nrv2b.small,8bit.sub,nrv.done" : - "nrv2b.8bit,nrv.done"; + method = isCon ? "nrv2b.small,8bit.sub,nrv.done" : "nrv2b.8bit,nrv.done"; else if (ph.method == M_NRV2D_8) - method = isCon ? "nrv2d.small,8bit.sub,nrv.done" : - "nrv2d.8bit,nrv.done"; + method = isCon ? "nrv2d.small,8bit.sub,nrv.done" : "nrv2d.8bit,nrv.done"; else if (ph.method == M_NRV2E_8) - method = isCon ? "nrv2e.small,8bit.sub,nrv.done" : - "nrv2e.8bit,nrv.done"; + method = isCon ? "nrv2e.small,8bit.sub,nrv.done" : "nrv2e.8bit,nrv.done"; else if (ph.method == M_NRV2B_LE32) - method = isCon ? "nrv2b.small,32bit.sub,nrv.done" : - "nrv2b.32bit,nrv.done"; + method = isCon ? "nrv2b.small,32bit.sub,nrv.done" : "nrv2b.32bit,nrv.done"; else if (ph.method == M_NRV2D_LE32) - method = isCon ? "nrv2d.small,32bit.sub,nrv.done" : - "nrv2d.32bit,nrv.done"; + method = isCon ? "nrv2d.small,32bit.sub,nrv.done" : "nrv2d.32bit,nrv.done"; else if (ph.method == M_NRV2E_LE32) - method = isCon ? "nrv2e.small,32bit.sub,nrv.done" : - "nrv2e.32bit,nrv.done"; + method = isCon ? "nrv2e.small,32bit.sub,nrv.done" : "nrv2e.32bit,nrv.done"; else if (M_IS_LZMA(ph.method)) method = "nrv2b.small,8bit.sub,nrv.done,lzma.prep"; else throwInternalError("unknown compression method"); unsigned sa_tmp = sa_cnt; - if (ph.overlap_overhead > sa_cnt) - { - if (!opt->force) - { + if (ph.overlap_overhead > sa_cnt) { + if (!opt->force) { infoWarning("not in-place decompressible"); throwCantPack("packed data overlap (try --force)"); - } - else + } else sa_tmp += overlap = ALIGN_UP((ph.overlap_overhead - sa_tmp), 4u); } if (isCon || M_IS_LZMA(ph.method)) foundBss = findBssSection(); - if (M_IS_LZMA(ph.method) && !buildPart2) - { + if (M_IS_LZMA(ph.method) && !buildPart2) { initLoader(stub_mipsel_r3000_ps1, sizeof(stub_mipsel_r3000_ps1)); - addLoader("decompressor.start", - isCon ? "LZMA_DEC20" : "LZMA_DEC10", "lzma.init", nullptr); + addLoader("decompressor.start", isCon ? "LZMA_DEC20" : "LZMA_DEC10", "lzma.init", nullptr); addLoader(sa_tmp > (0x10000 << 2) ? "memset.long" : "memset.short", !foundBss ? "con.exit" : "bss.exit", nullptr); - } - else - { - if (M_IS_LZMA(ph.method) && buildPart2) - { + } else { + if (M_IS_LZMA(ph.method) && buildPart2) { sz_lcpr = MemBuffer::getSizeForCompression(sz_lunc); unsigned char *cprLoader = New(unsigned char, sz_lcpr); - int r = upx_compress(getLoader(), sz_lunc, cprLoader, &sz_lcpr, - nullptr, M_NRV2B_8, 10, nullptr, nullptr ); + int r = upx_compress(getLoader(), sz_lunc, cprLoader, &sz_lcpr, nullptr, M_NRV2B_8, 10, + nullptr, nullptr); if (r != UPX_E_OK || sz_lcpr >= sz_lunc) throwInternalError("loader compression failed"); initLoader(stub_mipsel_r3000_ps1, sizeof(stub_mipsel_r3000_ps1), - isCon || !M_IS_LZMA(ph.method) ? 0 : 1); + isCon || !M_IS_LZMA(ph.method) ? 0 : 1); linker->addSection("lzma.exec", cprLoader, sz_lcpr, 0); - delete [] cprLoader; - } - else + delete[] cprLoader; + } else initLoader(stub_mipsel_r3000_ps1, sizeof(stub_mipsel_r3000_ps1)); 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 }; + static const unsigned char pad_buffer[4] = {0, 0, 0, 0}; linker->addSection("pad.code", pad_buffer, pad_code, 0); - if (isCon) - { + if (isCon) { if (M_IS_LZMA(ph.method)) - addLoader(!foundBss ? "con.start" : "bss.con.start", - method, - ih.tx_ptr & 0xffff ? "dec.ptr" : "dec.ptr.hi", - "con.entry", "pad.code", "lzma.exec", nullptr); + addLoader(!foundBss ? "con.start" : "bss.con.start", method, + ih.tx_ptr & 0xffff ? "dec.ptr" : "dec.ptr.hi", "con.entry", "pad.code", + "lzma.exec", nullptr); else addLoader(!foundBss ? "con.start" : "bss.con.start", "con.mcpy", ph.c_len & 3 ? "con.padcd" : "", - ih.tx_ptr & 0xffff ? "dec.ptr" : "dec.ptr.hi", - "con.entry", method, + ih.tx_ptr & 0xffff ? "dec.ptr" : "dec.ptr.hi", "con.entry", method, sa_cnt ? sa_cnt > (0x10000 << 2) ? "memset.long" : "memset.short" : "", - !foundBss ? "con.exit" : "bss.exit", - "pad.code", nullptr); - } - else - { + !foundBss ? "con.exit" : "bss.exit", "pad.code", nullptr); + } else { if (M_IS_LZMA(ph.method)) addLoader(!foundBss ? "cdb.start.lzma" : "bss.cdb.start.lzma", "pad.code", - !foundBss ? "cdb.entry.lzma" : "bss.cdb.entry.lzma", - method, "cdb.lzma.cpr", - ih.tx_ptr & 0xffff ? "dec.ptr" : "dec.ptr.hi", + !foundBss ? "cdb.entry.lzma" : "bss.cdb.entry.lzma", method, + "cdb.lzma.cpr", ih.tx_ptr & 0xffff ? "dec.ptr" : "dec.ptr.hi", "lzma.exec", nullptr); - else - { + else { assert(foundBss != true); addLoader("cdb.start", "pad.code", "cdb.entry", - ih.tx_ptr & 0xffff ? "cdb.dec.ptr" : "cdb.dec.ptr.hi", - method, + ih.tx_ptr & 0xffff ? "cdb.dec.ptr" : "cdb.dec.ptr.hi", method, sa_cnt ? sa_cnt > (0x10000 << 2) ? "memset.long" : "memset.short" : "", "cdb.exit", nullptr); } @@ -394,63 +341,55 @@ void PackPs1::buildLoader(const Filter *) } } -#define OPTYPE(x) (((x) >> 13) & 0x7) -#define OPCODE(x) (((x) >> 10) & 0x7) -#define REG1(x) (((x) >> 5) & 0x1f) -#define REG2(x) ((x) & 0x1f) +#define OPTYPE(x) (((x) >> 13) & 0x7) +#define OPCODE(x) (((x) >> 10) & 0x7) +#define REG1(x) (((x) >> 5) & 0x1f) +#define REG2(x) ((x) &0x1f) -#define MIPS_IMM(a,b) ((((a) - (((b) & 0x8000) >> 15)) << 16) | (b)) +#define MIPS_IMM(a, b) ((((a) - (((b) &0x8000) >> 15)) << 16) | (b)) // Type -#define REGIMM 1 -#define STORE 5 +#define REGIMM 1 +#define STORE 5 // Op -#define LUI 7 -#define ADDIU 1 -#define SW 3 +#define LUI 7 +#define ADDIU 1 +#define SW 3 -#define IS_LUI(a) ((OPTYPE(a) == REGIMM && OPCODE(a) == LUI)) -#define IS_ADDIU(a) ((OPTYPE(a) == REGIMM && OPCODE(a) == ADDIU)) +#define IS_LUI(a) ((OPTYPE(a) == REGIMM && OPCODE(a) == LUI)) +#define IS_ADDIU(a) ((OPTYPE(a) == REGIMM && OPCODE(a) == ADDIU)) #define IS_SW_ZERO(a) ((OPTYPE(a) == STORE && OPCODE(a) == SW) && REG2(a) == 0) #define BSS_CHK_LIMIT (18) -bool PackPs1::findBssSection() -{ +bool PackPs1::findBssSection() { unsigned char reg; - const LE32 * const p1 = ACC_CCAST(const LE32 *, ibuf + (ih.epc - ih.tx_ptr)); + const LE32 *const p1 = ACC_CCAST(const LE32 *, ibuf + (ih.epc - ih.tx_ptr)); if ((ih.epc - ih.tx_ptr + (BSS_CHK_LIMIT * 4)) > fdata_size) return false; // check 18 opcodes for sw zero,0(x) - for (signed i = BSS_CHK_LIMIT; i >= 0; i--) - { + for (signed i = BSS_CHK_LIMIT; i >= 0; i--) { upx_uint16_t op = p1[i] >> 16; - if (IS_SW_ZERO(op)) - { + if (IS_SW_ZERO(op)) { // found! get reg (x) for bss_start reg = REG1(op); - for (; i >= 0; i--) - { - const bss_nfo * const p = ACC_CCAST(const bss_nfo *, &p1[i]); + for (; i >= 0; i--) { + const bss_nfo *const p = ACC_CCAST(const bss_nfo *, &p1[i]); upx_uint16_t op1 = p->op1, op2 = p->op2; // check for la (x),bss_start - if ((IS_LUI(op1) && REG2(op1) == reg) && - (IS_ADDIU(op2) && REG1(op2) == reg)) - { + if ((IS_LUI(op1) && REG2(op1) == reg) && (IS_ADDIU(op2) && REG1(op2) == reg)) { op1 = p->op3, op2 = p->op4; // check for la (y),bss_end - if (IS_LUI(op1) && IS_ADDIU(op2)) - { + if (IS_LUI(op1) && IS_ADDIU(op2)) { // bss section info found! bss_start = MIPS_IMM(p->hi1, p->lo1); bss_end = MIPS_IMM(p->hi2, p->lo2); - if (0 < ALIGN_DOWN(bss_end - bss_start, 4u)) - { + 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; if (bss_end > (end_offs + wkmem_sz)) @@ -458,8 +397,7 @@ bool PackPs1::findBssSection() else return false; } - } - else + } else return false; } } @@ -468,24 +406,25 @@ bool PackPs1::findBssSection() return false; } - /************************************************************************* // **************************************************************************/ -void PackPs1::pack(OutputFile *fo) -{ +void PackPs1::pack(OutputFile *fo) { ibuf.alloc(fdata_size); obuf.allocForCompression(fdata_size); const upx_byte *p_scan = ibuf + fdata_size; // read file - fi->seek(PS_HDR_SIZE,SEEK_SET); - fi->readx(ibuf,fdata_size); + fi->seek(PS_HDR_SIZE, SEEK_SET); + fi->readx(ibuf, fdata_size); // scan EOF for 2048 bytes sector alignment // the removed space will secure in-place decompression - while (!(*--p_scan)) { if (sa_cnt++ > (0x10000 << 5) || sa_cnt >= fdata_size - 1024) break; } + 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, 32u); @@ -498,34 +437,32 @@ void PackPs1::pack(OutputFile *fo) Filter ft(ph.level); // compress (max_match = 65535) - upx_compress_config_t cconf; cconf.reset(); + upx_compress_config_t cconf; + cconf.reset(); cconf.conf_ucl.max_match = 65535; cconf.conf_lzma.max_num_probs = 1846 + (768 << 4); // ushort: ~28 KiB stack compressWithFilters(&ft, sa_cnt, &cconf); - if (overlap) - { + if (overlap) { opt->info_mode += !opt->info_mode ? 1 : 0; infoWarning("overlap - relocating load address (+%d bytes)", overlap); sa_cnt += overlap; } -/* - if (bss_start && bss_end && !foundBss) - infoWarning("%s: .bss section too small - use stack", fi->getName()); -*/ + /* + if (bss_start && bss_end && !foundBss) + infoWarning("%s: .bss section too small - use stack", fi->getName()); + */ unsigned lzma_init = 0; - if (M_IS_LZMA(ph.method)) - { + if (M_IS_LZMA(ph.method)) { sz_lunc = getLoaderSize(); lzma_init = 0u - (sz_lunc - linker->getSymbolOffset("lzma.init")); defineDecompressorSymbols(); linker->defineSymbol("entry", ih.epc); - linker->defineSymbol("SC", - sa_cnt > (0x10000 << 2) ? sa_cnt >> 5 : sa_cnt >> 2); + linker->defineSymbol("SC", sa_cnt > (0x10000 << 2) ? sa_cnt >> 5 : sa_cnt >> 2); relocateLoader(); buildPart2 = true; @@ -535,15 +472,13 @@ void PackPs1::pack(OutputFile *fo) memcpy(&oh, &ih, sizeof(ih)); unsigned sz_cbh; - putBkupHeader((const unsigned char *)&ih.epc, (unsigned char *)&bh, &sz_cbh); + putBkupHeader((const unsigned char *) &ih.epc, (unsigned char *) &bh, &sz_cbh); if (ih.is_ptr < (EXE_BS | (PS_RAM_SIZE - PS_STACK_SIZE))) oh.is_ptr = (EXE_BS | (PS_RAM_SIZE - 16)); - if (ih.da_ptr != 0 || ih.da_len != 0 || - ih.bs_ptr != 0 || ih.bs_len != 0) - oh.da_ptr = oh.da_len = - oh.bs_ptr = oh.bs_len = 0; + if (ih.da_ptr != 0 || ih.da_len != 0 || ih.bs_ptr != 0 || ih.bs_len != 0) + oh.da_ptr = oh.da_len = oh.bs_ptr = oh.bs_len = 0; const int lsize = getLoaderSize(); @@ -556,84 +491,71 @@ void PackPs1::pack(OutputFile *fo) int d_len = 0; int e_len = 0; - if (isCon) - { + if (isCon) { e_len = lsize - h_len; d_len = e_len - getLoaderSectionStart("con.entry"); - } - else - { - const char* entry_lzma = !foundBss ? "cdb.entry.lzma" : "bss.cdb.entry.lzma"; + } else { + const char *entry_lzma = !foundBss ? "cdb.entry.lzma" : "bss.cdb.entry.lzma"; - d_len = (lsize - h_len) - getLoaderSectionStart(M_IS_LZMA(ph.method) ? entry_lzma : "cdb.entry"); + d_len = (lsize - h_len) - + getLoaderSectionStart(M_IS_LZMA(ph.method) ? entry_lzma : "cdb.entry"); e_len = (lsize - d_len) - h_len; } linker->defineSymbol("entry", ih.epc); - linker->defineSymbol("SC", MIPS_LO(sa_cnt > (0x10000 << 2) ? - sa_cnt >> 5 : sa_cnt >> 2)); + linker->defineSymbol("SC", MIPS_LO(sa_cnt > (0x10000 << 2) ? sa_cnt >> 5 : sa_cnt >> 2)); linker->defineSymbol("DECO", decomp_data_start); - linker->defineSymbol("ldr_sz", M_IS_LZMA(ph.method) ? sz_lunc + 16 : (d_len-pad_code)); + linker->defineSymbol("ldr_sz", M_IS_LZMA(ph.method) ? sz_lunc + 16 : (d_len - pad_code)); - if (foundBss) - { + if (foundBss) { if (M_IS_LZMA(ph.method)) - linker->defineSymbol("wrkmem", bss_end - 160 - getDecompressorWrkmemSize() - - (sz_lunc + 16)); + linker->defineSymbol("wrkmem", + bss_end - 160 - getDecompressorWrkmemSize() - (sz_lunc + 16)); else linker->defineSymbol("wrkmem", bss_end - 16 - (d_len - pad_code)); } - const unsigned entry = comp_data_start - e_len; oh.epc = oh.tx_ptr = entry; oh.tx_len = ph.c_len + e_len; unsigned pad = 0; - if (!opt->ps1_exe.no_align || !isCon) - { + if (!opt->ps1_exe.no_align || !isCon) { pad = oh.tx_len; - oh.tx_len = ALIGN_UP(oh.tx_len, CD_SEC+0u); + oh.tx_len = ALIGN_UP(oh.tx_len, CD_SEC + 0u); pad = oh.tx_len - pad; oh.tx_ptr -= pad; } - ibuf.clear(0,fdata_size); + ibuf.clear(0, fdata_size); upx_bytep paddata = ibuf; - if (M_IS_LZMA(ph.method)) - { + if (M_IS_LZMA(ph.method)) { linker->defineSymbol("lzma_init_off", lzma_init); linker->defineSymbol("gb_e", linker->getSymbolOffset("gb8_e")); - } - else - if (isCon) - linker->defineSymbol("gb_e", linker->getSymbolOffset(is32Bit ? "gb32_e" : "gb8_e")); + } else if (isCon) + linker->defineSymbol("gb_e", linker->getSymbolOffset(is32Bit ? "gb32_e" : "gb8_e")); - if (isCon) - { + if (isCon) { linker->defineSymbol("PAD", pad_code); if (M_IS_LZMA(ph.method)) linker->defineSymbol("DCRT", (entry + getLoaderSectionStart("lzma.exec"))); else linker->defineSymbol("DCRT", (entry + (e_len - d_len))); - } - else - { + } else { linker->defineSymbol("PSVR", FIX_PSVR); linker->defineSymbol("CPDO", comp_data_start); - if (M_IS_LZMA(ph.method)) - { - unsigned entry_lzma = getLoaderSectionStart( !foundBss ? "cdb.entry.lzma" : - "bss.cdb.entry.lzma"); + if (M_IS_LZMA(ph.method)) { + unsigned entry_lzma = + getLoaderSectionStart(!foundBss ? "cdb.entry.lzma" : "bss.cdb.entry.lzma"); linker->defineSymbol("lzma_cpr", getLoaderSectionStart("lzma.exec") - entry_lzma); } } relocateLoader(); - //linker->dumpSymbols(); + // linker->dumpSymbols(); MemBuffer loader(lsize); assert(lsize == getLoaderSize()); memcpy(loader, getLoader(), lsize); @@ -680,13 +602,11 @@ void PackPs1::pack(OutputFile *fo) #endif } - /************************************************************************* // **************************************************************************/ -int PackPs1::canUnpack() -{ +int PackPs1::canUnpack() { if (!readFileHeader()) return false; if (!readPackHeader(CD_SEC)) @@ -700,13 +620,11 @@ int PackPs1::canUnpack() return true; } - /************************************************************************* // **************************************************************************/ -void PackPs1::unpack(OutputFile *fo) -{ +void PackPs1::unpack(OutputFile *fo) { // restore orig exec hdr memcpy(&oh, &ih, sizeof(ih)); memcpy((void *) &oh.epc, &bh, SZ_IH_BKUP); @@ -725,8 +643,7 @@ void PackPs1::unpack(OutputFile *fo) decompress(ibuf + (fdata_size - ph.c_len), obuf); // write decompressed file - if (fo) - { + if (fo) { // write header fo->write(&oh, sizeof(oh)); // align the ps exe header (mode 2 sector data size) diff --git a/src/p_ps1.h b/src/p_ps1.h index 10488327..1d1431cc 100644 --- a/src/p_ps1.h +++ b/src/p_ps1.h @@ -29,18 +29,17 @@ */ - -#ifndef __UPX_P_PS1_H -#define __UPX_P_PS1_H 1 - +#pragma once +#ifndef UPX_P_PS1_H__ +#define UPX_P_PS1_H__ 1 /************************************************************************* // ps1/exe **************************************************************************/ -class PackPs1 final : public Packer -{ +class PackPs1 final : public Packer { typedef Packer super; + public: PackPs1(InputFile *f); virtual int getVersion() const override { return 13; } @@ -58,16 +57,16 @@ public: protected: void putBkupHeader(const unsigned char *src, unsigned char *dst, unsigned *len); - bool getBkupHeader(unsigned char *src, unsigned char * dst); + bool getBkupHeader(unsigned char *src, unsigned char *dst); bool readBkupHeader(); virtual void buildLoader(const Filter *ft) override; bool findBssSection(); - virtual Linker* newLinker() const override; + virtual Linker *newLinker() const override; int readFileHeader(); bool checkFileHeader(); - __packed_struct(ps1_exe_t) + struct alignas(1) ps1_exe_t { // ident string char id[8]; // is nullptr @@ -90,26 +89,26 @@ protected: char origin[60]; // backup of the original header (epc - is_len) // id & the upx header ... - __packed_struct_end() + }; // for unpack - __packed_struct(ps1_exe_hb_t) + struct alignas(1) ps1_exe_hb_t { LE32 ih_bkup[10]; // plus checksum for the backup LE32 ih_csum; - __packed_struct_end() + }; - __packed_struct(ps1_exe_chb_t) + struct alignas(1) ps1_exe_chb_t { unsigned char id; unsigned char len; - LE16 ih_csum; + LE16 ih_csum; unsigned char ih_bkup; - __packed_struct_end() + }; - __packed_struct(bss_nfo) - LE16 hi1, op1, lo1, op2; - LE16 hi2, op3, lo2, op4; - __packed_struct_end() + struct alignas(1) bss_nfo { + LE16 hi1, op1, lo1, op2; + LE16 hi2, op3, lo2, op4; + }; ps1_exe_t ih, oh; ps1_exe_hb_t bh; @@ -127,7 +126,6 @@ protected: unsigned fdata_size; }; - #endif /* already included */ /* vim:set ts=4 sw=4 et: */ diff --git a/src/p_sys.cpp b/src/p_sys.cpp index 4aeb1f85..74be3999 100644 --- a/src/p_sys.cpp +++ b/src/p_sys.cpp @@ -25,7 +25,6 @@ */ - #include "conf.h" #include "file.h" #include "filter.h" @@ -34,22 +33,20 @@ #include "p_sys.h" #include "linker.h" -static const +static const CLANG_FORMAT_DUMMY_STATEMENT #include "stub/i086-dos16.sys.h" - /************************************************************************* // **************************************************************************/ -bool PackSys::canPack() -{ +bool PackSys::canPack() { unsigned char buf[128]; fi->readx(buf, sizeof(buf)); - if (memcmp (buf,"\xff\xff\xff\xff",4) != 0) + if (memcmp(buf, "\xff\xff\xff\xff", 4) != 0) return false; - if (!fn_has_ext(fi->getName(),"sys")) + if (!fn_has_ext(fi->getName(), "sys")) return false; checkAlreadyPacked(buf, sizeof(buf)); if (file_size < 1024) @@ -59,15 +56,11 @@ bool PackSys::canPack() return true; } - /************************************************************************* // **************************************************************************/ -void PackSys::patchLoader(OutputFile *fo, - upx_byte *loader, int lsize, - unsigned calls) -{ +void PackSys::patchLoader(OutputFile *fo, upx_byte *loader, int lsize, unsigned calls) { const int e_len = getLoaderSectionStart("SYSCUTPO"); const int d_len = lsize - e_len; assert(e_len > 0 && e_len < 128); @@ -92,37 +85,24 @@ void PackSys::patchLoader(OutputFile *fo, relocateLoader(); loader = getLoader(); - patchPackHeader(loader,e_len); + patchPackHeader(loader, e_len); // write loader + compressed file - fo->write(loader,e_len); // entry - fo->write(obuf,ph.c_len); - fo->write(loader+e_len,d_len); // decompressor + fo->write(loader, e_len); // entry + fo->write(obuf, ph.c_len); + fo->write(loader + e_len, d_len); // decompressor } - -void PackSys::buildLoader(const Filter *ft) -{ +void PackSys::buildLoader(const Filter *ft) { initLoader(stub_i086_dos16_sys, sizeof(stub_i086_dos16_sys)); - addLoader("SYSMAIN1", - opt->cpu == opt->CPU_8086 ? "SYSI0861" : "SYSI2861", - "SYSMAIN2", - ph.first_offset_found == 1 ? "SYSSBBBP" : "", - ft->id ? "SYSCALLT" : "", + addLoader("SYSMAIN1", opt->cpu == opt->CPU_8086 ? "SYSI0861" : "SYSI2861", "SYSMAIN2", + ph.first_offset_found == 1 ? "SYSSBBBP" : "", ft->id ? "SYSCALLT" : "", "SYSMAIN3,UPX1HEAD,SYSCUTPO,NRV2B160,NRVDDONE,NRVDECO1", - ph.max_offset_found <= 0xd00 ? "NRVLED00" : "NRVGTD00", - "NRVDECO2", - nullptr - ); - if (ft->id) - { + ph.max_offset_found <= 0xd00 ? "NRVLED00" : "NRVGTD00", "NRVDECO2", nullptr); + if (ft->id) { assert(ft->calls > 0); addFilter16(ft->id); } - addLoader("SYSMAIN5", - opt->cpu == opt->CPU_8086 ? "SYSI0862" : "SYSI2862", - "SYSJUMP1", - nullptr - ); + addLoader("SYSMAIN5", opt->cpu == opt->CPU_8086 ? "SYSI0862" : "SYSI2862", "SYSJUMP1", nullptr); } /* vim:set ts=4 sw=4 et: */ diff --git a/src/p_sys.h b/src/p_sys.h index 4393e51b..c02e1c87 100644 --- a/src/p_sys.h +++ b/src/p_sys.h @@ -25,24 +25,22 @@ */ - -#ifndef __UPX_P_SYS_H -#define __UPX_P_SYS_H 1 - +#pragma once +#ifndef UPX_P_SYS_H__ +#define UPX_P_SYS_H__ 1 /************************************************************************* // dos/sys **************************************************************************/ -class PackSys final : public PackCom -{ +class PackSys final : public PackCom { typedef PackCom super; + public: - PackSys(InputFile *f) : super(f) { } + PackSys(InputFile *f) : super(f) {} virtual int getVersion() const override { return 13; } virtual int getFormat() const override { return UPX_F_DOS_SYS; } virtual const char *getName() const override { return "dos/sys"; } - //virtual const char *getFullName(const options_t *o) const override { return o && o->cpu == o->CPU_8086 ? "i086-dos16.sys" : "i286-dos16.sys"; } virtual const char *getFullName(const options_t *) const override { return "i086-dos16.sys"; } virtual bool canPack() override; @@ -55,7 +53,6 @@ protected: virtual void patchLoader(OutputFile *fo, upx_byte *, int, unsigned) override; }; - #endif /* already included */ /* vim:set ts=4 sw=4 et: */ diff --git a/src/p_wcle.cpp b/src/p_wcle.cpp index 518f3299..2dec84f6 100644 --- a/src/p_wcle.cpp +++ b/src/p_wcle.cpp @@ -25,7 +25,6 @@ */ - #include "conf.h" #include "file.h" #include "filter.h" @@ -34,115 +33,91 @@ #include "p_wcle.h" #include "linker.h" -static const +static const CLANG_FORMAT_DUMMY_STATEMENT #include "stub/i386-dos32.watcom.le.h" -#define LEOF_READ (1<<0) -#define LEOF_WRITE (1<<1) -#define LEOF_EXEC (1<<2) -#define LEOF_PRELOAD (1<<6) -#define LEOF_HUGE32 (1<<13) +#define LEOF_READ (1 << 0) +#define LEOF_WRITE (1 << 1) +#define LEOF_EXEC (1 << 2) +#define LEOF_PRELOAD (1 << 6) +#define LEOF_HUGE32 (1 << 13) -#define IOT(x,y) iobject_table[x].y -#define OOT(x,y) oobject_table[x].y +#define IOT(x, y) iobject_table[x].y +#define OOT(x, y) oobject_table[x].y -#define LE_STUB_EDI (1) +#define LE_STUB_EDI (1) #ifdef TESTING -# define dputc(x,y) do { if (opt->debug.debug_level) putc(x,y); } while (0) -# define Opt_debug opt->debug.debug_level +#define dputc(x, y) \ + do { \ + if (opt->debug.debug_level) \ + putc(x, y); \ + } while (0) +#define Opt_debug opt->debug.debug_level #else -# define dputc(x,y) ((void)0) -# define Opt_debug 0 +#define dputc(x, y) ((void) 0) +#define Opt_debug 0 #endif #define my_base_address reserved -#define objects ih.object_table_entries -#define pages ih.memory_pages -#define mps ih.memory_page_size -#define opages oh.memory_pages - +#define objects ih.object_table_entries +#define pages ih.memory_pages +#define mps ih.memory_page_size +#define opages oh.memory_pages /************************************************************************* // **************************************************************************/ -const int *PackWcle::getCompressionMethods(int method, int level) const -{ +const int *PackWcle::getCompressionMethods(int method, int level) const { return Packer::getDefaultCompressionMethods_le32(method, level); } - -const int *PackWcle::getFilters() const -{ - static const int filters[] = { - 0x26, 0x24, 0x49, 0x46, 0x16, 0x13, 0x14, 0x11, - FT_ULTRA_BRUTE, 0x25, 0x15, 0x12, - FT_END }; +const int *PackWcle::getFilters() const { + static const int filters[] = {0x26, 0x24, 0x49, 0x46, 0x16, 0x13, 0x14, + 0x11, FT_ULTRA_BRUTE, 0x25, 0x15, 0x12, FT_END}; return filters; } +Linker *PackWcle::newLinker() const { return new ElfLinkerX86; } -Linker* PackWcle::newLinker() const -{ - return new ElfLinkerX86; -} - - -void PackWcle::buildLoader(const Filter *ft) -{ +void PackWcle::buildLoader(const Filter *ft) { // prepare loader initLoader(stub_i386_dos32_watcom_le, sizeof(stub_i386_dos32_watcom_le)); - addLoader("IDENTSTR,WCLEMAIN", - ph.first_offset_found == 1 ? "WCLEMAIN02" : "", + addLoader("IDENTSTR,WCLEMAIN", ph.first_offset_found == 1 ? "WCLEMAIN02" : "", "WCLEMAIN03,UPX1HEAD,WCLECUTP", nullptr); // fake alignment for the start of the decompressor linker->defineSymbol("WCLECUTP", 0x1000); addLoader(getDecompressorSections(), "WCLEMAI2", nullptr); - if (ft->id) - { + if (ft->id) { assert(ft->calls > 0); addLoader(ft->addvalue ? "WCCTTPOS" : "WCCTTNUL", nullptr); addFilter32(ft->id); } #if 1 // FIXME: if (has_relocation) - { - addLoader("WCRELOC1,RELOC320", - big_relocs ? "REL32BIG" : "", - "RELOC32J", - nullptr - ); - } + { addLoader("WCRELOC1,RELOC320", big_relocs ? "REL32BIG" : "", "RELOC32J", nullptr); } #endif - addLoader(has_extra_code ? "WCRELSEL" : "", - "WCLEMAI4", - nullptr - ); + addLoader(has_extra_code ? "WCRELSEL" : "", "WCLEMAI4", nullptr); } - /************************************************************************* // util **************************************************************************/ -void PackWcle::handleStub(OutputFile *fo) -{ +void PackWcle::handleStub(OutputFile *fo) { if (fo && !opt->watcom_le.le) - Packer::handleStub(fi,fo,le_offset); + Packer::handleStub(fi, fo, le_offset); } - -bool PackWcle::canPack() -{ +bool PackWcle::canPack() { if (!LeFile::readFileHeader()) return false; return true; } - /************************************************************************* // **************************************************************************/ @@ -150,30 +125,27 @@ bool PackWcle::canPack() // IDEA: as all the entries go into object #1, I could create bundles with 255 // elements (of course I still have to handle empty bundles) -void PackWcle::encodeEntryTable() -{ - unsigned count,object,n; +void PackWcle::encodeEntryTable() { + unsigned count, object, n; upx_byte *p = ientries; n = 0; - while (*p) - { + while (*p) { count = *p; n += count; if (p[1] == 0) // unused bundle p += 2; else if (p[1] == 3) // 32-bit bundle { - object = get_le16(p+2)-1; - set_le16(p+2,1); + object = get_le16(p + 2) - 1; + set_le16(p + 2, 1); p += 4; for (; count; count--, p += 5) - set_le32(p+1,IOT(object,my_base_address) + get_le32(p+1)); - } - else + set_le32(p + 1, IOT(object, my_base_address) + get_le32(p + 1)); + } else throwCantPack("unsupported bundle type in entry table"); } - //if (Opt_debug) printf("%d entries encoded.\n",n); + // if (Opt_debug) printf("%d entries encoded.\n",n); UNUSED(n); soentries = ptr_udiff_bytes(p, ientries) + 1; @@ -181,118 +153,102 @@ void PackWcle::encodeEntryTable() ientries = nullptr; } - -void PackWcle::readObjectTable() -{ +void PackWcle::readObjectTable() { LeFile::readObjectTable(); // temporary copy of the object descriptors - iobject_desc.alloc(objects*sizeof(*iobject_table)); - memcpy(iobject_desc,iobject_table,objects*sizeof(*iobject_table)); + iobject_desc.alloc(objects * sizeof(*iobject_table)); + memcpy(iobject_desc, iobject_table, objects * sizeof(*iobject_table)); - unsigned ic,jc,virtual_size; + unsigned ic, jc, virtual_size; - for (ic = jc = virtual_size = 0; ic < objects; ic++) - { - jc += IOT(ic,npages); - IOT(ic,my_base_address) = virtual_size; - virtual_size += (IOT(ic,virtual_size)+mps-1) &~ (mps-1); + for (ic = jc = virtual_size = 0; ic < objects; ic++) { + jc += IOT(ic, npages); + IOT(ic, my_base_address) = virtual_size; + virtual_size += (IOT(ic, virtual_size) + mps - 1) & ~(mps - 1); } if (pages != jc) throwCantPack("bad page number"); } - -void PackWcle::encodeObjectTable() -{ - unsigned ic,jc; +void PackWcle::encodeObjectTable() { + unsigned ic, jc; oobject_table = New(le_object_table_entry_t, soobject_table = 2); - memset(oobject_table,0,soobject_table * sizeof(*oobject_table)); + memset(oobject_table, 0, soobject_table * sizeof(*oobject_table)); // object #1: - OOT(0,base_address) = IOT(0,base_address); + OOT(0, base_address) = IOT(0, base_address); - ic = IOT(objects-1,my_base_address)+IOT(objects-1,virtual_size); - jc = pages*mps+sofixups+1024; + ic = IOT(objects - 1, my_base_address) + IOT(objects - 1, virtual_size); + jc = pages * mps + sofixups + 1024; if (ic < jc) ic = jc; - unsigned csection = (ic + ph.overlap_overhead + mps-1) &~ (mps-1); + unsigned csection = (ic + ph.overlap_overhead + mps - 1) & ~(mps - 1); - OOT(0,virtual_size) = csection + mps; - OOT(0,flags) = LEOF_READ|LEOF_EXEC|LEOF_HUGE32|LEOF_PRELOAD; - OOT(0,pagemap_index) = 1; - OOT(0,npages) = opages; + OOT(0, virtual_size) = csection + mps; + OOT(0, flags) = LEOF_READ | LEOF_EXEC | LEOF_HUGE32 | LEOF_PRELOAD; + OOT(0, pagemap_index) = 1; + OOT(0, npages) = opages; // object #2: stack - OOT(1,base_address) = (OOT(0,base_address) - +OOT(0,virtual_size)+mps-1) & ~(mps-1); - OOT(1,virtual_size) = mps + getDecompressorWrkmemSize(); - OOT(1,flags) = LEOF_READ|LEOF_HUGE32|LEOF_WRITE; - OOT(1,pagemap_index) = 1; + OOT(1, base_address) = (OOT(0, base_address) + OOT(0, virtual_size) + mps - 1) & ~(mps - 1); + OOT(1, virtual_size) = mps + getDecompressorWrkmemSize(); + OOT(1, flags) = LEOF_READ | LEOF_HUGE32 | LEOF_WRITE; + OOT(1, pagemap_index) = 1; oh.init_cs_object = 1; oh.init_eip_offset = neweip; oh.init_ss_object = 2; - oh.init_esp_offset = OOT(1,virtual_size); + oh.init_esp_offset = OOT(1, virtual_size); oh.automatic_data_object = 2; } - -void PackWcle::encodePageMap() -{ +void PackWcle::encodePageMap() { opm_entries = New(le_pagemap_entry_t, sopm_entries = opages); - for (unsigned ic = 0; ic < sopm_entries; ic++) - { - opm_entries[ic].l = (unsigned char) (ic+1); - opm_entries[ic].m = (unsigned char) ((ic+1)>>8); + for (unsigned ic = 0; ic < sopm_entries; ic++) { + opm_entries[ic].l = (unsigned char) (ic + 1); + opm_entries[ic].m = (unsigned char) ((ic + 1) >> 8); opm_entries[ic].h = 0; opm_entries[ic].type = 0; } } - -void PackWcle::encodeFixupPageTable() -{ +void PackWcle::encodeFixupPageTable() { unsigned ic; ofpage_table = New(unsigned, sofpage_table = 1 + opages); for (ofpage_table[0] = ic = 0; ic < opages; ic++) - set_le32(ofpage_table+ic+1,sofixups-FIXUP_EXTRA); + set_le32(ofpage_table + ic + 1, sofixups - FIXUP_EXTRA); } - -void PackWcle::encodeFixups() -{ - ofixups = New(upx_byte, sofixups = 1*7 + FIXUP_EXTRA); - memset(ofixups,0,sofixups); +void PackWcle::encodeFixups() { + ofixups = New(upx_byte, sofixups = 1 * 7 + FIXUP_EXTRA); + memset(ofixups, 0, sofixups); ofixups[0] = 7; - set_le16(ofixups+2,(LE_STUB_EDI + neweip) & (mps-1)); + set_le16(ofixups + 2, (LE_STUB_EDI + neweip) & (mps - 1)); ofixups[4] = 1; } - -void PackWcle::preprocessFixups() -{ +void PackWcle::preprocessFixups() { big_relocs = 0; - unsigned ic,jc; + unsigned ic, jc; - Array(unsigned, counts, objects+2); + Array(unsigned, counts, objects + 2); countFixups(counts); for (ic = jc = 0; ic < objects; ic++) jc += counts[ic]; - if (jc == 0) - { + if (jc == 0) { // FIXME: implement this throwCantPack("files without relocations are not supported"); } MemBuffer rl_membuf(jc); - ByteArray(srf, counts[objects+0]+1); - ByteArray(slf, counts[objects+1]+1); + ByteArray(srf, counts[objects + 0] + 1); + ByteArray(slf, counts[objects + 1] + 1); SPAN_S_VAR(upx_byte, rl, rl_membuf); SPAN_S_VAR(upx_byte, selector_fixups, srf_membuf); @@ -300,147 +256,139 @@ void PackWcle::preprocessFixups() unsigned rc = 0; upx_byte *fix = ifixups; - for (ic = jc = 0; ic < pages; ic++) - { - while ((unsigned)(fix - ifixups) < get_le32(ifpage_table+ic+1)) - { - const int fixp2 = get_le16_signed(fix+2); + for (ic = jc = 0; ic < pages; ic++) { + while ((unsigned) (fix - ifixups) < get_le32(ifpage_table + ic + 1)) { + const int fixp2 = get_le16_signed(fix + 2); unsigned value; - switch (*fix) - { - case 2: // selector fixup - if (fixp2 < 0) - { - // cross page selector fixup - dputc('S',stdout); - fix += 5; - break; - } - dputc('s',stdout); - memcpy(selector_fixups,"\x8C\xCB\x66\x89\x9D",5); // mov bx, cs ; mov [xxx+ebp], bx - if (IOT(fix[4]-1,flags) & LEOF_WRITE) - selector_fixups[1] = 0xDB; // ds - set_le32(selector_fixups+5,jc+fixp2); - selector_fixups += 9; + switch (*fix) { + case 2: // selector fixup + if (fixp2 < 0) { + // cross page selector fixup + dputc('S', stdout); fix += 5; break; - case 5: // 16-bit offset - if ((unsigned)fixp2 < 4096 && IOT(fix[4]-1,my_base_address) == jc) - dputc('6',stdout); - else - throwCantPack("unsupported 16-bit offset relocation"); + } + dputc('s', stdout); + memcpy(selector_fixups, "\x8C\xCB\x66\x89\x9D", + 5); // mov bx, cs ; mov [xxx+ebp], bx + if (IOT(fix[4] - 1, flags) & LEOF_WRITE) + selector_fixups[1] = 0xDB; // ds + set_le32(selector_fixups + 5, jc + fixp2); + selector_fixups += 9; + fix += 5; + break; + case 5: // 16-bit offset + if ((unsigned) fixp2 < 4096 && IOT(fix[4] - 1, my_base_address) == jc) + dputc('6', stdout); + else + throwCantPack("unsupported 16-bit offset relocation"); + fix += (fix[1] & 0x10) ? 9 : 7; + break; + case 6: // 16:32 pointer + if (fixp2 < 0) { + // cross page pointer fixup + dputc('P', stdout); fix += (fix[1] & 0x10) ? 9 : 7; break; - case 6: // 16:32 pointer - if (fixp2 < 0) - { - // cross page pointer fixup - dputc('P',stdout); - fix += (fix[1] & 0x10) ? 9 : 7; - break; - } - dputc('p',stdout); - memcpy(iimage+jc+fixp2,fix+5,(fix[1] & 0x10) ? 4 : 2); - set_le32(rl+4*rc++,jc+fixp2); - set_le32(iimage+jc+fixp2,get_le32(iimage+jc+fixp2)+IOT(fix[4]-1,my_base_address)); + } + dputc('p', stdout); + memcpy(iimage + jc + fixp2, fix + 5, (fix[1] & 0x10) ? 4 : 2); + set_le32(rl + 4 * rc++, jc + fixp2); + set_le32(iimage + jc + fixp2, + get_le32(iimage + jc + fixp2) + IOT(fix[4] - 1, my_base_address)); - memcpy(selector_fixups,"\x8C\xCA\x66\x89\x95",5); - if (IOT(fix[4]-1,flags) & LEOF_WRITE) - selector_fixups[1] = 0xDA; // ds - set_le32(selector_fixups+5,jc+fixp2+4); - selector_fixups += 9; + memcpy(selector_fixups, "\x8C\xCA\x66\x89\x95", 5); + if (IOT(fix[4] - 1, flags) & LEOF_WRITE) + selector_fixups[1] = 0xDA; // ds + set_le32(selector_fixups + 5, jc + fixp2 + 4); + selector_fixups += 9; + fix += (fix[1] & 0x10) ? 9 : 7; + break; + case 7: // 32-bit offset + if (fixp2 < 0) { fix += (fix[1] & 0x10) ? 9 : 7; break; - case 7: // 32-bit offset - if (fixp2 < 0) - { - fix += (fix[1] & 0x10) ? 9 : 7; - break; - } - //if (memcmp(iimage+jc+fixp2,fix+5,(fix[1] & 0x10) ? 4 : 2)) - // throwCantPack("illegal fixup offset"); + } + // if (memcmp(iimage+jc+fixp2,fix+5,(fix[1] & 0x10) ? 4 : 2)) + // throwCantPack("illegal fixup offset"); - // work around a pmwunlite bug: remove duplicated fixups - // FIXME: fix the other cases too - if (rc == 0 || get_le32(rl+4*rc-4) != jc+fixp2) - { - set_le32(rl+4*rc++,jc+fixp2); - set_le32(iimage+jc+fixp2,get_le32(iimage+jc+fixp2)+IOT(fix[4]-1,my_base_address)); - } + // work around a pmwunlite bug: remove duplicated fixups + // FIXME: fix the other cases too + if (rc == 0 || get_le32(rl + 4 * rc - 4) != jc + fixp2) { + set_le32(rl + 4 * rc++, jc + fixp2); + set_le32(iimage + jc + fixp2, + get_le32(iimage + jc + fixp2) + IOT(fix[4] - 1, my_base_address)); + } + fix += (fix[1] & 0x10) ? 9 : 7; + break; + case 8: // 32-bit self relative fixup + if (fixp2 < 0) { + // cross page self relative fixup + dputc('R', stdout); fix += (fix[1] & 0x10) ? 9 : 7; break; - case 8: // 32-bit self relative fixup - if (fixp2 < 0) - { - // cross page self relative fixup - dputc('R',stdout); - fix += (fix[1] & 0x10) ? 9 : 7; - break; - } - value = get_le32(fix+5); - if (fix[1] == 0) - value &= 0xffff; - set_le32(iimage+jc+fixp2,(value+IOT(fix[4]-1,my_base_address))-jc-fixp2-4); - set_le32(selfrel_fixups,jc+fixp2); - selfrel_fixups += 4; - dputc('r',stdout); - fix += (fix[1] & 0x10) ? 9 : 7; - break; - default: - throwCantPack("unsupported fixup record"); + } + value = get_le32(fix + 5); + if (fix[1] == 0) + value &= 0xffff; + set_le32(iimage + jc + fixp2, + (value + IOT(fix[4] - 1, my_base_address)) - jc - fixp2 - 4); + set_le32(selfrel_fixups, jc + fixp2); + selfrel_fixups += 4; + dputc('r', stdout); + fix += (fix[1] & 0x10) ? 9 : 7; + break; + default: + throwCantPack("unsupported fixup record"); } } jc += mps; } // resize ifixups if it's too small - if (sofixups < 1000) - { + if (sofixups < 1000) { delete[] ifixups; ifixups = new upx_byte[1000]; } - fix = ifixups + optimizeReloc32 (rl,rc,ifixups,iimage,file_size,1,&big_relocs); + fix = ifixups + optimizeReloc32(rl, rc, ifixups, iimage, file_size, 1, &big_relocs); has_extra_code = ptr_udiff_bytes(selector_fixups, srf) != 0; // FIXME: this could be removed if has_extra_code = false // but then we'll need a flag - *selector_fixups++ = 0xC3; // ret - memcpy(fix,srf,ptr_udiff_bytes(selector_fixups, srf)); // copy selector fixup code + *selector_fixups++ = 0xC3; // ret + memcpy(fix, srf, ptr_udiff_bytes(selector_fixups, srf)); // copy selector fixup code fix += ptr_udiff_bytes(selector_fixups, srf); - memcpy(fix,slf,ptr_udiff_bytes(selfrel_fixups,slf)); // copy self-relative fixup positions + memcpy(fix, slf, ptr_udiff_bytes(selfrel_fixups, slf)); // copy self-relative fixup positions fix += ptr_udiff_bytes(selfrel_fixups, slf); - set_le32(fix,0xFFFFFFFFUL); + set_le32(fix, 0xFFFFFFFFUL); fix += 4; sofixups = ptr_udiff_bytes(fix, ifixups); } - #define RESERVED 0x1000 -void PackWcle::encodeImage(Filter *ft) -{ +void PackWcle::encodeImage(Filter *ft) { // concatenate image & preprocessed fixups unsigned isize = soimage + sofixups; ibuf.alloc(isize); - memcpy(ibuf,iimage,soimage); - memcpy(ibuf+soimage,ifixups,sofixups); + memcpy(ibuf, iimage, soimage); + memcpy(ibuf + soimage, ifixups, sofixups); - delete[] ifixups; ifixups = nullptr; + delete[] ifixups; + ifixups = nullptr; - mb_oimage.allocForCompression(isize, RESERVED+512); + mb_oimage.allocForCompression(isize, RESERVED + 512); oimage = mb_oimage; // prepare packheader ph.u_len = isize; // prepare filter [already done] // compress - upx_compress_config_t cconf; cconf.reset(); + upx_compress_config_t cconf; + cconf.reset(); cconf.conf_lzma.max_num_probs = 1846 + (768 << 4); // ushort: ~28 KiB stack - compressWithFilters(ibuf, isize, - raw_bytes(oimage + RESERVED, mb_oimage.getSize() - RESERVED), - ibuf + ft->addvalue, ft->buf_len, - nullptr, 0, - ft, 512, &cconf, 0); + compressWithFilters(ibuf, isize, raw_bytes(oimage + RESERVED, mb_oimage.getSize() - RESERVED), + ibuf + ft->addvalue, ft->buf_len, nullptr, 0, ft, 512, &cconf, 0); ibuf.dealloc(); soimage = ph.c_len; @@ -448,21 +396,13 @@ void PackWcle::encodeImage(Filter *ft) oimage[RESERVED + soimage++] = 0; } - -void PackWcle::pack(OutputFile *fo) -{ +void PackWcle::pack(OutputFile *fo) { handleStub(fo); - if (ih.byte_order || ih.word_order - || ih.exe_format_level - || ih.cpu_type < 2 || ih.cpu_type > 5 - || ih.target_os != 1 - || ih.module_type != 0x200 - || ih.object_iterate_data_map_offset - || ih.resource_entries - || ih.module_directives_entries - || ih.imported_modules_count - || ih.object_table_entries > 255) + if (ih.byte_order || ih.word_order || ih.exe_format_level || ih.cpu_type < 2 || + ih.cpu_type > 5 || ih.target_os != 1 || ih.module_type != 0x200 || + ih.object_iterate_data_map_offset || ih.resource_entries || ih.module_directives_entries || + ih.imported_modules_count || ih.object_table_entries > 255) throwCantPack("watcom/le: unexpected value in header"); readObjectTable(); @@ -474,8 +414,8 @@ void PackWcle::pack(OutputFile *fo) readImage(); readNonResidentNames(); -// if (find_le32(iimage,20,get_le32("UPX ")) >= 0) - if (find_le32(raw_bytes(iimage, soimage) ,UPX_MIN(soimage,256u),UPX_MAGIC_LE32) >= 0) + // if (find_le32(iimage,20,get_le32("UPX ")) >= 0) + if (find_le32(raw_bytes(iimage, soimage), UPX_MIN(soimage, 256u), UPX_MAGIC_LE32) >= 0) throwAlreadyPacked(); if (ih.init_ss_object != objects) @@ -483,20 +423,21 @@ void PackWcle::pack(OutputFile *fo) preprocessFixups(); - const unsigned text_size = IOT(ih.init_cs_object-1,npages) * mps; - const unsigned text_vaddr = IOT(ih.init_cs_object-1,my_base_address); + const unsigned text_size = IOT(ih.init_cs_object - 1, npages) * mps; + const unsigned text_vaddr = IOT(ih.init_cs_object - 1, my_base_address); // attach some useful data at the end of preprocessed fixups ifixups[sofixups++] = (unsigned char) (ih.automatic_data_object & 0xff); - unsigned ic = objects*sizeof(*iobject_table); - memcpy(ifixups+sofixups,iobject_desc,ic); + unsigned ic = objects * sizeof(*iobject_table); + memcpy(ifixups + sofixups, iobject_desc, ic); iobject_desc.dealloc(); sofixups += ic; - set_le32(ifixups+sofixups,ih.init_esp_offset+IOT(ih.init_ss_object-1,my_base_address)); // old stack pointer - set_le32(ifixups+sofixups+4,ih.init_eip_offset+text_vaddr); // real entry point - set_le32(ifixups+sofixups+8,mps*pages); // virtual address of unpacked relocations - ifixups[sofixups+12] = (unsigned char) (unsigned) objects; + set_le32(ifixups + sofixups, + ih.init_esp_offset + IOT(ih.init_ss_object - 1, my_base_address)); // old stack pointer + set_le32(ifixups + sofixups + 4, ih.init_eip_offset + text_vaddr); // real entry point + set_le32(ifixups + sofixups + 8, mps * pages); // virtual address of unpacked relocations + ifixups[sofixups + 12] = (unsigned char) (unsigned) objects; sofixups += 13; // prepare filter @@ -512,11 +453,11 @@ void PackWcle::pack(OutputFile *fo) const unsigned d_len = lsize - e_len; assert(e_len > 0 && e_len < RESERVED); - memmove(oimage+e_len,oimage+RESERVED,soimage); + memmove(oimage + e_len, oimage + RESERVED, soimage); soimage += lsize; - opages = (soimage+mps-1)/mps; - oh.bytes_on_last_page = soimage%mps; + opages = (soimage + mps - 1) / mps; + oh.bytes_on_last_page = soimage % mps; encodeObjectTable(); encodeFixups(); @@ -528,22 +469,22 @@ void PackWcle::pack(OutputFile *fo) encodeNonResidentNames(); // patch loader - ic = (OOT(0,virtual_size) - d_len) &~ 15; - assert(ic > ((ph.u_len + ph.overlap_overhead + 31) &~ 15)); + ic = (OOT(0, virtual_size) - d_len) & ~15; + assert(ic > ((ph.u_len + ph.overlap_overhead + 31) & ~15)); linker->defineSymbol("WCLECUTP", ic); linker->defineSymbol("original_entry", ih.init_eip_offset + text_vaddr); - linker->defineSymbol("original_stack", ih.init_esp_offset + - IOT(ih.init_ss_object - 1, my_base_address)); - linker->defineSymbol("start_of_relocs", mps*pages); + linker->defineSymbol("original_stack", + ih.init_esp_offset + IOT(ih.init_ss_object - 1, my_base_address)); + linker->defineSymbol("start_of_relocs", mps * pages); defineDecompressorSymbols(); defineFilterSymbols(&ft); linker->defineSymbol("filter_buffer_start", text_vaddr); - unsigned jpos = (((ph.c_len + 3) &~ 3) + d_len + 3) / 4; + unsigned jpos = (((ph.c_len + 3) & ~3) + d_len + 3) / 4; linker->defineSymbol("words_to_copy", jpos); - linker->defineSymbol("copy_dest", ((ic + d_len + 3) &~ 3) - 4); + linker->defineSymbol("copy_dest", ((ic + d_len + 3) & ~3) - 4); linker->defineSymbol("copy_source", e_len + jpos * 4 - 4); relocateLoader(); @@ -561,8 +502,7 @@ void PackWcle::pack(OutputFile *fo) verifyOverlappingDecompression(mb_oimage + e_len, mb_oimage.getSize() - e_len); // copy the overlay - const unsigned overlaystart = ih.data_pages_offset + exe_offset - + getImageSize(); + const unsigned overlaystart = ih.data_pages_offset + exe_offset + getImageSize(); const unsigned overlay = file_size - overlaystart - ih.non_resident_name_table_length; checkOverlay(overlay); copyOverlay(fo, overlay, mb_oimage); @@ -572,35 +512,32 @@ void PackWcle::pack(OutputFile *fo) throwNotCompressible(); } - /************************************************************************* // **************************************************************************/ -void PackWcle::decodeFixups() -{ +void PackWcle::decodeFixups() { SPAN_P_VAR(upx_byte, p, oimage + soimage); -// assert(p.raw_size_in_bytes() == mb_oimage.getSize()); // Span sanity check + // assert(p.raw_size_in_bytes() == mb_oimage.getSize()); // Span sanity check mb_iimage.dealloc(); iimage = nullptr; MemBuffer tmpbuf; - unsigned const fixupn = unoptimizeReloc32(p,oimage,tmpbuf,true); + unsigned const fixupn = unoptimizeReloc32(p, oimage, tmpbuf, true); - MemBuffer wrkmem(8*fixupn+8); - unsigned ic,jc,o,r; - for (ic=0; ic 0xFFFF ? 0x10 : 0); fp += fp[1] ? 9 : 7; selfrel_fixups += 4; - dputc('r',stdout); + dputc('r', stdout); } // selector fixups - while (selectlen && (r = get_le32(selector_fixups+5))/mps == ic-1) - { + while (selectlen && (r = get_le32(selector_fixups + 5)) / mps == ic - 1) { fp[0] = 2; fp[1] = 0; - set_le16(fp+2,r & (mps-1)); + set_le16(fp + 2, r & (mps - 1)); unsigned x = selector_fixups[1] > 0xD0 ? oh.init_ss_object : oh.init_cs_object; fp[4] = (unsigned char) x; fp += 5; selector_fixups += 9; selectlen--; - dputc('s',stdout); + dputc('s', stdout); } // 32 bit offset fixups - while (get_le32(wrkmem+4*jc) < ic*mps) - { - if (jc > 1 && ((get_le32(wrkmem+4*(jc-2))+3) & (mps-1)) < 3) // cross page fixup? + while (get_le32(wrkmem + 4 * jc) < ic * mps) { + if (jc > 1 && + ((get_le32(wrkmem + 4 * (jc - 2)) + 3) & (mps - 1)) < 3) // cross page fixup? { - r = get_le32(oimage+get_le32(wrkmem+4*(jc-2))); + r = get_le32(oimage + get_le32(wrkmem + 4 * (jc - 2))); fp[0] = 7; fp[1] = (unsigned char) (r > 0xFFFF ? 0x10 : 0); - set_le16(fp+2,get_le32(wrkmem+4*(jc-2)) | ~3); - set_le32(fp+5,r); + set_le16(fp + 2, get_le32(wrkmem + 4 * (jc - 2)) | ~3); + set_le32(fp + 5, r); o = soobject_table; - r = get_le32(wrkmem+4*(jc-1)); - virt2rela(oobject_table,&o,&r); + r = get_le32(wrkmem + 4 * (jc - 1)); + virt2rela(oobject_table, &o, &r); fp[4] = (unsigned char) o; fp += fp[1] ? 9 : 7; - dputc('0',stdout); + dputc('0', stdout); } o = soobject_table; - r = get_le32(wrkmem+4*(jc+1)); - virt2rela(oobject_table,&o,&r); - r = get_le32(oimage+get_le32(wrkmem+4*jc)); + r = get_le32(wrkmem + 4 * (jc + 1)); + virt2rela(oobject_table, &o, &r); + r = get_le32(oimage + get_le32(wrkmem + 4 * jc)); fp[0] = 7; fp[1] = (unsigned char) (r > 0xFFFF ? 0x10 : 0); - set_le16(fp+2,get_le32(wrkmem+4*jc) & (mps-1)); + set_le16(fp + 2, get_le32(wrkmem + 4 * jc) & (mps - 1)); fp[4] = (unsigned char) o; - set_le32(fp+5,r); + set_le32(fp + 5, r); fp += fp[1] ? 9 : 7; jc += 2; } - set_le32(ofpage_table+ic,ptr_udiff_bytes(fp,ofixups)); + set_le32(ofpage_table + ic, ptr_udiff_bytes(fp, ofixups)); } - for (ic=0; ic < FIXUP_EXTRA; ic++) + for (ic = 0; ic < FIXUP_EXTRA; ic++) *fp++ = 0; sofixups = ptr_udiff_bytes(fp, ofixups); } - -void PackWcle::decodeFixupPageTable() -{ +void PackWcle::decodeFixupPageTable() { ofpage_table = New(unsigned, sofpage_table = 1 + opages); - set_le32(ofpage_table,0); + set_le32(ofpage_table, 0); // the rest of ofpage_table is filled by decodeFixups() } - -void PackWcle::decodeObjectTable() -{ +void PackWcle::decodeObjectTable() { soobject_table = oimage[ph.u_len - 1]; oobject_table = New(le_object_table_entry_t, soobject_table); unsigned jc, ic = soobject_table * sizeof(*oobject_table); const unsigned extradata = ph.version == 10 ? 17 : 13; - memcpy(oobject_table,oimage + ph.u_len - extradata - ic,ic); + memcpy(oobject_table, oimage + ph.u_len - extradata - ic, ic); if (ph.version >= 12) oh.automatic_data_object = oimage[ph.u_len - ic - 14]; - for (ic = jc = 0; ic < soobject_table; ic++) - { - OOT(ic,my_base_address) = jc; - jc += (OOT(ic,virtual_size)+mps-1) &~ (mps-1); + for (ic = jc = 0; ic < soobject_table; ic++) { + OOT(ic, my_base_address) = jc; + jc += (OOT(ic, virtual_size) + mps - 1) & ~(mps - 1); } // restore original cs:eip & ss:esp ic = soobject_table; jc = get_le32(oimage + ph.u_len - (ph.version < 11 ? 13 : 9)); - virt2rela(oobject_table,&ic,&jc); + virt2rela(oobject_table, &ic, &jc); oh.init_cs_object = ic; oh.init_eip_offset = jc; @@ -748,53 +678,47 @@ void PackWcle::decodeObjectTable() jc = ih.init_esp_offset; else jc = get_le32(oimage + ph.u_len - (ph.version == 10 ? 17 : 13)); - virt2rela(oobject_table,&ic,&jc); + virt2rela(oobject_table, &ic, &jc); oh.init_ss_object = ic; oh.init_esp_offset = jc; } - -void PackWcle::decodeImage() -{ +void PackWcle::decodeImage() { mb_oimage.allocForDecompression(ph.u_len); oimage = mb_oimage; - decompress(iimage + ph.buf_offset + ph.getPackHeaderSize(),oimage); + decompress(iimage + ph.buf_offset + ph.getPackHeaderSize(), oimage); soimage = get_le32(oimage + ph.u_len - 5); opages = soimage / mps; oh.memory_page_size = mps; } - -void PackWcle::decodeEntryTable() -{ - unsigned count,object,n,r; +void PackWcle::decodeEntryTable() { + unsigned count, object, n, r; SPAN_S_VAR(upx_byte, p, ientries, soentries); n = 0; - while (*p) - { + while (*p) { count = *p; n += count; if (p[1] == 0) // unused bundle p += 2; else if (p[1] == 3) // 32-bit offset bundle { - object = get_le16(p+2); + object = get_le16(p + 2); if (object != 1) throwCantUnpack("corrupted entry found"); object = soobject_table; - r = get_le32(p+5); - virt2rela(oobject_table,&object,&r); - set_le16(p+2,object--); + r = get_le32(p + 5); + virt2rela(oobject_table, &object, &r); + set_le16(p + 2, object--); p += 4; for (; count; count--, p += 5) - set_le32(p+1,get_le32(p+1) - OOT(object,my_base_address)); - } - else + set_le32(p + 1, get_le32(p + 1) - OOT(object, my_base_address)); + } else throwCantUnpack("unsupported bundle type in entry table"); } - //if (Opt_debug) printf("\n%d entries decoded.\n",n); + // if (Opt_debug) printf("\n%d entries decoded.\n",n); UNUSED(n); soentries = ptr_udiff_bytes(p, ientries) + 1; @@ -802,39 +726,32 @@ void PackWcle::decodeEntryTable() ientries = nullptr; } - -int PackWcle::canUnpack() -{ +int PackWcle::canUnpack() { if (!LeFile::readFileHeader()) return false; fi->seek(exe_offset + ih.data_pages_offset, SEEK_SET); // FIXME: 1024 could be too large for some files - //int len = 1024; + // int len = 1024; int len = UPX_MIN(getImageSize(), 256u); if (len == 0) return false; return readPackHeader(len) ? 1 : -1; } - -void PackWcle::virt2rela(const le_object_table_entry_t *entr,unsigned *objn,unsigned *addr) -{ - for (; *objn > 1; objn[0]--) - { - if (entr[*objn-1].my_base_address > *addr) +void PackWcle::virt2rela(const le_object_table_entry_t *entr, unsigned *objn, unsigned *addr) { + for (; *objn > 1; objn[0]--) { + if (entr[*objn - 1].my_base_address > *addr) continue; - *addr -= entr[*objn-1].my_base_address; + *addr -= entr[*objn - 1].my_base_address; break; } } - /************************************************************************* // **************************************************************************/ -void PackWcle::unpack(OutputFile *fo) -{ +void PackWcle::unpack(OutputFile *fo) { handleStub(fo); readObjectTable(); @@ -851,17 +768,16 @@ void PackWcle::unpack(OutputFile *fo) decodeObjectTable(); // unfilter - if (ph.filter) - { - const unsigned text_size = OOT(oh.init_cs_object-1,npages) * mps; - const unsigned text_vaddr = OOT(oh.init_cs_object-1,my_base_address); + if (ph.filter) { + const unsigned text_size = OOT(oh.init_cs_object - 1, npages) * mps; + const unsigned text_vaddr = OOT(oh.init_cs_object - 1, my_base_address); Filter ft(ph.level); ft.init(ph.filter, text_vaddr); ft.cto = (unsigned char) ph.filter_cto; if (ph.version < 11) - ft.cto = (unsigned char) (get_le32(oimage+ph.u_len-9) >> 24); - ft.unfilter(raw_bytes(oimage+text_vaddr, text_size), text_size); + ft.cto = (unsigned char) (get_le32(oimage + ph.u_len - 9) >> 24); + ft.unfilter(raw_bytes(oimage + text_vaddr, text_size), text_size); } decodeFixupPageTable(); @@ -872,9 +788,9 @@ void PackWcle::unpack(OutputFile *fo) decodeNonResidentNames(); for (unsigned ic = 0; ic < soobject_table; ic++) - OOT(ic,my_base_address) = 0; + OOT(ic, my_base_address) = 0; - while (oimage[soimage-1] == 0) + while (oimage[soimage - 1] == 0) soimage--; oh.bytes_on_last_page = soimage % mps; @@ -883,8 +799,7 @@ void PackWcle::unpack(OutputFile *fo) writeFile(fo, opt->watcom_le.le); // copy the overlay - const unsigned overlaystart = ih.data_pages_offset + exe_offset - + getImageSize(); + const unsigned overlaystart = ih.data_pages_offset + exe_offset + getImageSize(); const unsigned overlay = file_size - overlaystart - ih.non_resident_name_table_length; checkOverlay(overlay); copyOverlay(fo, overlay, mb_oimage); diff --git a/src/p_wcle.h b/src/p_wcle.h index 141a1018..44d1cd92 100644 --- a/src/p_wcle.h +++ b/src/p_wcle.h @@ -25,24 +25,25 @@ */ - -#ifndef __UPX_P_WCLE_H -#define __UPX_P_WCLE_H 1 - +#pragma once +#ifndef UPX_P_WCLE_H__ +#define UPX_P_WCLE_H__ 1 /************************************************************************* // watcom/le **************************************************************************/ -class PackWcle final : public Packer, public LeFile -{ +class PackWcle final : public Packer, public LeFile { typedef Packer super; + public: PackWcle(InputFile *f) : super(f), LeFile(f) { bele = &N_BELE_RTP::le_policy; } virtual int getVersion() const override { return 13; } virtual int getFormat() const override { return UPX_F_WATCOM_LE; } virtual const char *getName() const override { return "watcom/le"; } - virtual const char *getFullName(const options_t *) const override { return "i386-dos32.watcom.le"; } + virtual const char *getFullName(const options_t *) const override { + return "i386-dos32.watcom.le"; + } virtual const int *getCompressionMethods(int method, int level) const override; virtual const int *getFilters() const override; @@ -56,7 +57,7 @@ protected: virtual void handleStub(OutputFile *fo); virtual void buildLoader(const Filter *ft) override; - virtual Linker* newLinker() const override; + virtual Linker *newLinker() const override; virtual void readObjectTable() override; virtual void encodeObjectTable(); @@ -87,7 +88,6 @@ protected: unsigned neweip; }; - #endif /* already included */ /* vim:set ts=4 sw=4 et: */