diff --git a/src/linker.cpp b/src/linker.cpp index 27a4841d..6a9d3d0d 100644 --- a/src/linker.cpp +++ b/src/linker.cpp @@ -120,8 +120,7 @@ ElfLinker::~ElfLinker() { free(relocations); } -void ElfLinker::init(const void *pdata_v, int plen, unsigned pxtra) -{ +void ElfLinker::init(const void *pdata_v, int plen, unsigned pxtra) { const upx_byte *pdata = (const upx_byte *) pdata_v; if (plen >= 16 && memcmp(pdata, "UPX#", 4) == 0) { // decompress pre-compressed stub-loader @@ -164,16 +163,16 @@ void ElfLinker::init(const void *pdata_v, int plen, unsigned pxtra) if ((int) strlen("Sections:\n" "SYMBOL TABLE:\n" "RELOCATION RECORDS FOR ") < inputlen) { - char const *const eof = (char const *)&input[inputlen]; + char const *const eof = (char const *) &input[inputlen]; int pos = find(input, inputlen, "Sections:\n", 10); assert(pos != -1); char *const psections = (char *) input + pos; char *const psymbols = strstr(psections, "SYMBOL TABLE:\n"); - //assert(psymbols != nullptr); + // assert(psymbols != nullptr); char *const prelocs = strstr((psymbols ? psymbols : psections), "RELOCATION RECORDS FOR "); - //assert(prelocs != nullptr); + // assert(prelocs != nullptr); preprocessSections(psections, (psymbols ? psymbols : (prelocs ? prelocs : eof))); if (psymbols) @@ -318,8 +317,7 @@ ElfLinker::Symbol *ElfLinker::findSymbol(const char *name, bool fatal) const { ElfLinker::Section *ElfLinker::addSection(const char *sname, const void *sdata, int slen, unsigned p2align) { // printf("addSection: %s len=%d align=%d\n", sname, slen, p2align); - if (!sdata - && (!strcmp("ABS*", sname) || !strcmp("UND*", sname))) + if (!sdata && (!strcmp("ABS*", sname) || !strcmp("UND*", sname))) return nullptr; if (update_capacity(nsections, &nsections_capacity)) sections = diff --git a/src/packer.cpp b/src/packer.cpp index 9c06147e..3fe7a800 100644 --- a/src/packer.cpp +++ b/src/packer.cpp @@ -37,8 +37,7 @@ **************************************************************************/ Packer::Packer(InputFile *f) - : bele(nullptr), fi(f), file_size(-1), ph_format(-1), ph_version(-1), - ibufgood(0), uip(nullptr), + : bele(nullptr), fi(f), file_size(-1), ph_format(-1), ph_version(-1), ibufgood(0), uip(nullptr), linker(nullptr), last_patch(nullptr), last_patch_len(0), last_patch_off(0) { file_size = 0; if (fi != nullptr) @@ -159,20 +158,20 @@ bool ph_skipVerify(const PackHeader &ph) { return true; } -int force_method(int method) // mark as forced +int force_method(int method) // mark as forced { - return (0x80ul<<24) | method; + return (0x80ul << 24) | method; } -int is_forced_method(int method) // predicate +int is_forced_method(int method) // predicate { return -0x80 == (method >> 24); } -int forced_method(int method) // extract the forced method +int forced_method(int method) // extract the forced method { if (is_forced_method(method)) - method &= ~(0x80ul<<24); + method &= ~(0x80ul << 24); assert(method > 0); return method; } @@ -182,8 +181,7 @@ int forced_method(int method) // extract the forced method **************************************************************************/ bool Packer::compress(upx_bytep i_ptr, unsigned i_len, upx_bytep o_ptr, - const upx_compress_config_t *cconf_parm) -{ + const upx_compress_config_t *cconf_parm) { ph.u_len = i_len; ph.c_len = 0; assert(ph.level >= 1); @@ -358,7 +356,8 @@ void ph_decompress(PackHeader &ph, const upx_bytep in, upx_bytep out, bool verif throwCantUnpack("header corrupted"); } unsigned new_len = ph.u_len; - int r = upx_decompress(in, ph.c_len, out, &new_len, forced_method(ph.method), &ph.compress_result); + int r = + upx_decompress(in, ph.c_len, out, &new_len, forced_method(ph.method), &ph.compress_result); if (r == UPX_E_OUT_OF_MEMORY) throwOutOfMemoryException(); if (r != UPX_E_OK || new_len != ph.u_len) @@ -403,7 +402,7 @@ static bool ph_testOverlappingDecompression(const PackHeader &ph, const upx_byte unsigned src_off = ph.u_len + overlap_overhead - ph.c_len; unsigned new_len = ph.u_len; int r = upx_test_overlap(buf - src_off, tbuf, src_off, ph.c_len, &new_len, - forced_method(ph.method), &ph.compress_result); + forced_method(ph.method), &ph.compress_result); if (r == UPX_E_OUT_OF_MEMORY) throwOutOfMemoryException(); return (r == UPX_E_OK && new_len == ph.u_len); @@ -826,12 +825,8 @@ int Packer::patch_le32(void *b, int blen, const void *old, unsigned new_) { // relocation util **************************************************************************/ -upx_byte *Packer::optimizeReloc( - upx_byte *in, unsigned relocnum, - upx_byte *out, - upx_byte *image, unsigned headway, - int bswap, int *big, int bits) -{ +upx_byte *Packer::optimizeReloc(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image, + unsigned headway, int bswap, int *big, int bits) { if (opt->exact) throwCantPackExact(); @@ -866,8 +861,8 @@ upx_byte *Packer::optimizeReloc( } pc += oc; if (headway <= pc) { - char msg[80]; snprintf(msg, sizeof(msg), - "bad reloc[%#x] = %#x", jc, oc); + char msg[80]; + snprintf(msg, sizeof(msg), "bad reloc[%#x] = %#x", jc, oc); throwCantPack(msg); } if (bswap) { @@ -883,13 +878,13 @@ upx_byte *Packer::optimizeReloc( return fix; } -upx_byte *Packer::optimizeReloc32(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image, unsigned headway, - int bswap, int *big) { +upx_byte *Packer::optimizeReloc32(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image, + unsigned headway, int bswap, int *big) { return optimizeReloc(in, relocnum, out, image, headway, bswap, big, 32); } -upx_byte *Packer::optimizeReloc64(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image, unsigned headway, - int bswap, int *big) { +upx_byte *Packer::optimizeReloc64(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image, + unsigned headway, int bswap, int *big) { return optimizeReloc(in, relocnum, out, image, headway, bswap, big, 64); } @@ -1160,7 +1155,7 @@ void Packer::relocateLoader() { int Packer::prepareMethods(int *methods, int ph_method, const int *all_methods) const { int nmethods = 0; - if (!opt->all_methods || all_methods == nullptr || (-0x80 == (ph_method>>24))) { + if (!opt->all_methods || all_methods == nullptr || (-0x80 == (ph_method >> 24))) { methods[nmethods++] = forced_method(ph_method); return nmethods; } @@ -1233,18 +1228,17 @@ done: return nfilters; } -void Packer::compressWithFilters( - upx_bytep i_ptr, unsigned const i_len, // written and restored by filters - upx_bytep const o_ptr, // where to put compressed output - upx_bytep f_ptr, unsigned const f_len, // subset of [*i_ptr, +i_len) - upx_bytep const hdr_ptr, unsigned const hdr_len, - Filter *const parm_ft, // updated - unsigned const overlap_range, - upx_compress_config_t const *const cconf, - int filter_strategy, // in+out for prepareFilters - bool const inhibit_compression_check -) -{ +void Packer::compressWithFilters(upx_bytep i_ptr, + unsigned const i_len, // written and restored by filters + upx_bytep const o_ptr, // where to put compressed output + upx_bytep f_ptr, + unsigned const f_len, // subset of [*i_ptr, +i_len) + upx_bytep const hdr_ptr, unsigned const hdr_len, + Filter *const parm_ft, // updated + unsigned const overlap_range, + upx_compress_config_t const *const cconf, + int filter_strategy, // in+out for prepareFilters + bool const inhibit_compression_check) { parm_ft->buf_len = f_len; // struct copies const PackHeader orig_ph = this->ph; @@ -1298,7 +1292,7 @@ void Packer::compressWithFilters( { #if 0 //{ printf("\nmethod %d (%d of %d)\n", methods[mm], 1+ mm, nmethods); -#endif //} +#endif //} assert(isValidCompressionMethod(methods[mm])); unsigned hdr_c_len = 0; if (hdr_ptr != nullptr && hdr_len) { @@ -1437,26 +1431,18 @@ void Packer::compressWithFilters( // **************************************************************************/ -void Packer::compressWithFilters( - Filter *ft, const unsigned overlap_range, - const upx_compress_config_t *cconf, int filter_strategy, - bool inhibit_compression_check) -{ +void Packer::compressWithFilters(Filter *ft, const unsigned overlap_range, + const upx_compress_config_t *cconf, int filter_strategy, + bool inhibit_compression_check) { compressWithFilters( // call the subroutine immediately below - ft, overlap_range, - cconf, filter_strategy, - 0, 0, 0, - nullptr, 0, - inhibit_compression_check); + ft, overlap_range, cconf, filter_strategy, 0, 0, 0, nullptr, 0, inhibit_compression_check); } -void Packer::compressWithFilters( - Filter *ft, const unsigned overlap_range, - upx_compress_config_t const *cconf, int filter_strategy, - unsigned filter_off, unsigned ibuf_off, unsigned obuf_off, - upx_bytep const hdr_ptr, unsigned hdr_len, - bool inhibit_compression_check) -{ +void Packer::compressWithFilters(Filter *ft, const unsigned overlap_range, + upx_compress_config_t const *cconf, int filter_strategy, + unsigned filter_off, unsigned ibuf_off, unsigned obuf_off, + upx_bytep const hdr_ptr, unsigned hdr_len, + bool inhibit_compression_check) { ibuf.checkState(); obuf.checkState(); @@ -1469,12 +1455,8 @@ void Packer::compressWithFilters( assert(f_ptr + f_len <= i_ptr + i_len); compressWithFilters( // call the first one in this file - i_ptr, i_len, - o_ptr, - f_ptr, f_len, - hdr_ptr, hdr_len, - ft, overlap_range, - cconf, filter_strategy, inhibit_compression_check); + i_ptr, i_len, o_ptr, f_ptr, f_len, hdr_ptr, hdr_len, ft, overlap_range, cconf, + filter_strategy, inhibit_compression_check); ibuf.checkState(); obuf.checkState(); diff --git a/src/packer.h b/src/packer.h index 15df2304..860678ed 100644 --- a/src/packer.h +++ b/src/packer.h @@ -177,23 +177,17 @@ protected: bool inhibit_compression_check = false); void compressWithFilters(Filter *ft, const unsigned overlap_range, const upx_compress_config_t *cconf, int filter_strategy, - unsigned filter_buf_off, - unsigned compress_ibuf_off, - unsigned compress_obuf_off, - upx_bytep const hdr_ptr, unsigned hdr_len, + unsigned filter_buf_off, unsigned compress_ibuf_off, + unsigned compress_obuf_off, upx_bytep const hdr_ptr, unsigned hdr_len, bool inhibit_compression_check = false); // real compression driver - void compressWithFilters( - upx_bytep i_ptr, unsigned i_len, // written and restored by filters - upx_bytep o_ptr, - upx_bytep f_ptr, unsigned f_len, // subset of [*i_ptr, +i_len) - upx_bytep const hdr_ptr, unsigned hdr_len, - Filter *parm_ft, // updated - unsigned overlap_range, - upx_compress_config_t const *cconf, - int filter_strategy, - bool inhibit_compression_check = false - ); + void compressWithFilters(upx_bytep i_ptr, unsigned i_len, // written and restored by filters + upx_bytep o_ptr, upx_bytep f_ptr, + unsigned f_len, // subset of [*i_ptr, +i_len) + upx_bytep const hdr_ptr, unsigned hdr_len, + Filter *parm_ft, // updated + unsigned overlap_range, upx_compress_config_t const *cconf, + int filter_strategy, bool inhibit_compression_check = false); // util for verifying overlapping decompresion // non-destructive test @@ -279,22 +273,17 @@ protected: void checkPatch(void *b, int blen, int boff, int size); // relocation util - static upx_byte *optimizeReloc( - upx_byte *in, unsigned relocnum, - upx_byte *out, upx_byte *image, unsigned headway, - int bs, int *big, int bits); - static unsigned unoptimizeReloc(upx_byte **in, upx_byte *image, MemBuffer *out, int bs, int bits); + static upx_byte *optimizeReloc(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image, + unsigned headway, int bs, int *big, int bits); + static unsigned unoptimizeReloc(upx_byte **in, upx_byte *image, MemBuffer *out, int bs, + int bits); - static upx_byte *optimizeReloc32( - upx_byte *in, unsigned relocnum, - upx_byte *out, upx_byte *image, unsigned headway, - int bs, int *big); + static upx_byte *optimizeReloc32(upx_byte *in, unsigned relocnum, upx_byte *out, + upx_byte *image, unsigned headway, int bs, int *big); static unsigned unoptimizeReloc32(upx_byte **in, upx_byte *image, MemBuffer *out, int bs); - static upx_byte *optimizeReloc64( - upx_byte *in, unsigned relocnum, - upx_byte *out, upx_byte *image, unsigned headway, - int bs, int *big); + static upx_byte *optimizeReloc64(upx_byte *in, unsigned relocnum, upx_byte *out, + upx_byte *image, unsigned headway, int bs, int *big); static unsigned unoptimizeReloc64(upx_byte **in, upx_byte *image, MemBuffer *out, int bs); // target endianness abstraction @@ -319,9 +308,9 @@ protected: int ph_version; // compression buffers - MemBuffer ibuf; // input - MemBuffer obuf; // output - unsigned ibufgood; // high-water mark in ibuf (pefile.cpp) + MemBuffer ibuf; // input + MemBuffer obuf; // output + unsigned ibufgood; // high-water mark in ibuf (pefile.cpp) // UI handler UiPacker *uip = nullptr; @@ -341,8 +330,8 @@ private: Packer &operator=(const Packer &) = delete; }; -int force_method(int method); // (0x80ul<<24)|method -int forced_method(int method); // (0x80ul<<24)|method ==> method +int force_method(int method); // (0x80ul<<24)|method +int forced_method(int method); // (0x80ul<<24)|method ==> method int is_forced_method(int method); // predicate #endif /* already included */