clang-format

This commit is contained in:
Markus F.X.J. Oberhumer 2022-08-10 23:29:43 +02:00
parent 793d8c1d2e
commit 5f9c50cd3e
3 changed files with 69 additions and 100 deletions

View File

@ -120,8 +120,7 @@ ElfLinker::~ElfLinker() {
free(relocations); 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; const upx_byte *pdata = (const upx_byte *) pdata_v;
if (plen >= 16 && memcmp(pdata, "UPX#", 4) == 0) { if (plen >= 16 && memcmp(pdata, "UPX#", 4) == 0) {
// decompress pre-compressed stub-loader // 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" if ((int) strlen("Sections:\n"
"SYMBOL TABLE:\n" "SYMBOL TABLE:\n"
"RELOCATION RECORDS FOR ") < inputlen) { "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); int pos = find(input, inputlen, "Sections:\n", 10);
assert(pos != -1); assert(pos != -1);
char *const psections = (char *) input + pos; char *const psections = (char *) input + pos;
char *const psymbols = strstr(psections, "SYMBOL TABLE:\n"); char *const psymbols = strstr(psections, "SYMBOL TABLE:\n");
//assert(psymbols != nullptr); // assert(psymbols != nullptr);
char *const prelocs = strstr((psymbols ? psymbols : psections), "RELOCATION RECORDS FOR "); char *const prelocs = strstr((psymbols ? psymbols : psections), "RELOCATION RECORDS FOR ");
//assert(prelocs != nullptr); // assert(prelocs != nullptr);
preprocessSections(psections, (psymbols ? psymbols : (prelocs ? prelocs : eof))); preprocessSections(psections, (psymbols ? psymbols : (prelocs ? prelocs : eof)));
if (psymbols) 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, ElfLinker::Section *ElfLinker::addSection(const char *sname, const void *sdata, int slen,
unsigned p2align) { unsigned p2align) {
// printf("addSection: %s len=%d align=%d\n", sname, slen, p2align); // printf("addSection: %s len=%d align=%d\n", sname, slen, p2align);
if (!sdata if (!sdata && (!strcmp("ABS*", sname) || !strcmp("UND*", sname)))
&& (!strcmp("ABS*", sname) || !strcmp("UND*", sname)))
return nullptr; return nullptr;
if (update_capacity(nsections, &nsections_capacity)) if (update_capacity(nsections, &nsections_capacity))
sections = sections =

View File

@ -37,8 +37,7 @@
**************************************************************************/ **************************************************************************/
Packer::Packer(InputFile *f) Packer::Packer(InputFile *f)
: bele(nullptr), fi(f), file_size(-1), ph_format(-1), ph_version(-1), : bele(nullptr), fi(f), file_size(-1), ph_format(-1), ph_version(-1), ibufgood(0), uip(nullptr),
ibufgood(0), uip(nullptr),
linker(nullptr), last_patch(nullptr), last_patch_len(0), last_patch_off(0) { linker(nullptr), last_patch(nullptr), last_patch_len(0), last_patch_off(0) {
file_size = 0; file_size = 0;
if (fi != nullptr) if (fi != nullptr)
@ -159,20 +158,20 @@ bool ph_skipVerify(const PackHeader &ph) {
return true; 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); 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)) if (is_forced_method(method))
method &= ~(0x80ul<<24); method &= ~(0x80ul << 24);
assert(method > 0); assert(method > 0);
return method; 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, 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.u_len = i_len;
ph.c_len = 0; ph.c_len = 0;
assert(ph.level >= 1); 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"); throwCantUnpack("header corrupted");
} }
unsigned new_len = ph.u_len; 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) if (r == UPX_E_OUT_OF_MEMORY)
throwOutOfMemoryException(); throwOutOfMemoryException();
if (r != UPX_E_OK || new_len != ph.u_len) 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 src_off = ph.u_len + overlap_overhead - ph.c_len;
unsigned new_len = ph.u_len; unsigned new_len = ph.u_len;
int r = upx_test_overlap(buf - src_off, tbuf, src_off, ph.c_len, &new_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) if (r == UPX_E_OUT_OF_MEMORY)
throwOutOfMemoryException(); throwOutOfMemoryException();
return (r == UPX_E_OK && new_len == ph.u_len); 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 // relocation util
**************************************************************************/ **************************************************************************/
upx_byte *Packer::optimizeReloc( upx_byte *Packer::optimizeReloc(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image,
upx_byte *in, unsigned relocnum, unsigned headway, int bswap, int *big, int bits) {
upx_byte *out,
upx_byte *image, unsigned headway,
int bswap, int *big, int bits)
{
if (opt->exact) if (opt->exact)
throwCantPackExact(); throwCantPackExact();
@ -866,8 +861,8 @@ upx_byte *Packer::optimizeReloc(
} }
pc += oc; pc += oc;
if (headway <= pc) { if (headway <= pc) {
char msg[80]; snprintf(msg, sizeof(msg), char msg[80];
"bad reloc[%#x] = %#x", jc, oc); snprintf(msg, sizeof(msg), "bad reloc[%#x] = %#x", jc, oc);
throwCantPack(msg); throwCantPack(msg);
} }
if (bswap) { if (bswap) {
@ -883,13 +878,13 @@ upx_byte *Packer::optimizeReloc(
return fix; return fix;
} }
upx_byte *Packer::optimizeReloc32(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image, unsigned headway, upx_byte *Packer::optimizeReloc32(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image,
int bswap, int *big) { unsigned headway, int bswap, int *big) {
return optimizeReloc(in, relocnum, out, image, headway, bswap, big, 32); 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, upx_byte *Packer::optimizeReloc64(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image,
int bswap, int *big) { unsigned headway, int bswap, int *big) {
return optimizeReloc(in, relocnum, out, image, headway, bswap, big, 64); 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 Packer::prepareMethods(int *methods, int ph_method, const int *all_methods) const {
int nmethods = 0; 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); methods[nmethods++] = forced_method(ph_method);
return nmethods; return nmethods;
} }
@ -1233,18 +1228,17 @@ done:
return nfilters; return nfilters;
} }
void Packer::compressWithFilters( void Packer::compressWithFilters(upx_bytep i_ptr,
upx_bytep i_ptr, unsigned const i_len, // written and restored by filters unsigned const i_len, // written and restored by filters
upx_bytep const o_ptr, // where to put compressed output 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 f_ptr,
upx_bytep const hdr_ptr, unsigned const hdr_len, unsigned const f_len, // subset of [*i_ptr, +i_len)
Filter *const parm_ft, // updated upx_bytep const hdr_ptr, unsigned const hdr_len,
unsigned const overlap_range, Filter *const parm_ft, // updated
upx_compress_config_t const *const cconf, unsigned const overlap_range,
int filter_strategy, // in+out for prepareFilters upx_compress_config_t const *const cconf,
bool const inhibit_compression_check int filter_strategy, // in+out for prepareFilters
) bool const inhibit_compression_check) {
{
parm_ft->buf_len = f_len; parm_ft->buf_len = f_len;
// struct copies // struct copies
const PackHeader orig_ph = this->ph; const PackHeader orig_ph = this->ph;
@ -1298,7 +1292,7 @@ void Packer::compressWithFilters(
{ {
#if 0 //{ #if 0 //{
printf("\nmethod %d (%d of %d)\n", methods[mm], 1+ mm, nmethods); printf("\nmethod %d (%d of %d)\n", methods[mm], 1+ mm, nmethods);
#endif //} #endif //}
assert(isValidCompressionMethod(methods[mm])); assert(isValidCompressionMethod(methods[mm]));
unsigned hdr_c_len = 0; unsigned hdr_c_len = 0;
if (hdr_ptr != nullptr && hdr_len) { if (hdr_ptr != nullptr && hdr_len) {
@ -1437,26 +1431,18 @@ void Packer::compressWithFilters(
// //
**************************************************************************/ **************************************************************************/
void Packer::compressWithFilters( void Packer::compressWithFilters(Filter *ft, const unsigned overlap_range,
Filter *ft, const unsigned overlap_range, const upx_compress_config_t *cconf, int filter_strategy,
const upx_compress_config_t *cconf, int filter_strategy, bool inhibit_compression_check) {
bool inhibit_compression_check)
{
compressWithFilters( // call the subroutine immediately below compressWithFilters( // call the subroutine immediately below
ft, overlap_range, ft, overlap_range, cconf, filter_strategy, 0, 0, 0, nullptr, 0, inhibit_compression_check);
cconf, filter_strategy,
0, 0, 0,
nullptr, 0,
inhibit_compression_check);
} }
void Packer::compressWithFilters( void Packer::compressWithFilters(Filter *ft, const unsigned overlap_range,
Filter *ft, const unsigned overlap_range, upx_compress_config_t const *cconf, int filter_strategy,
upx_compress_config_t const *cconf, int filter_strategy, unsigned filter_off, unsigned ibuf_off, unsigned obuf_off,
unsigned filter_off, unsigned ibuf_off, unsigned obuf_off, upx_bytep const hdr_ptr, unsigned hdr_len,
upx_bytep const hdr_ptr, unsigned hdr_len, bool inhibit_compression_check) {
bool inhibit_compression_check)
{
ibuf.checkState(); ibuf.checkState();
obuf.checkState(); obuf.checkState();
@ -1469,12 +1455,8 @@ void Packer::compressWithFilters(
assert(f_ptr + f_len <= i_ptr + i_len); assert(f_ptr + f_len <= i_ptr + i_len);
compressWithFilters( // call the first one in this file compressWithFilters( // call the first one in this file
i_ptr, i_len, i_ptr, i_len, o_ptr, f_ptr, f_len, hdr_ptr, hdr_len, ft, overlap_range, cconf,
o_ptr, filter_strategy, inhibit_compression_check);
f_ptr, f_len,
hdr_ptr, hdr_len,
ft, overlap_range,
cconf, filter_strategy, inhibit_compression_check);
ibuf.checkState(); ibuf.checkState();
obuf.checkState(); obuf.checkState();

View File

@ -177,23 +177,17 @@ protected:
bool inhibit_compression_check = false); bool inhibit_compression_check = false);
void compressWithFilters(Filter *ft, const unsigned overlap_range, void compressWithFilters(Filter *ft, const unsigned overlap_range,
const upx_compress_config_t *cconf, int filter_strategy, const upx_compress_config_t *cconf, int filter_strategy,
unsigned filter_buf_off, unsigned filter_buf_off, unsigned compress_ibuf_off,
unsigned compress_ibuf_off, unsigned compress_obuf_off, upx_bytep const hdr_ptr, unsigned hdr_len,
unsigned compress_obuf_off,
upx_bytep const hdr_ptr, unsigned hdr_len,
bool inhibit_compression_check = false); bool inhibit_compression_check = false);
// real compression driver // real compression driver
void compressWithFilters( void compressWithFilters(upx_bytep i_ptr, unsigned i_len, // written and restored by filters
upx_bytep i_ptr, unsigned i_len, // written and restored by filters upx_bytep o_ptr, upx_bytep f_ptr,
upx_bytep o_ptr, unsigned f_len, // subset of [*i_ptr, +i_len)
upx_bytep f_ptr, unsigned f_len, // subset of [*i_ptr, +i_len) upx_bytep const hdr_ptr, unsigned hdr_len,
upx_bytep const hdr_ptr, unsigned hdr_len, Filter *parm_ft, // updated
Filter *parm_ft, // updated unsigned overlap_range, upx_compress_config_t const *cconf,
unsigned overlap_range, int filter_strategy, bool inhibit_compression_check = false);
upx_compress_config_t const *cconf,
int filter_strategy,
bool inhibit_compression_check = false
);
// util for verifying overlapping decompresion // util for verifying overlapping decompresion
// non-destructive test // non-destructive test
@ -279,22 +273,17 @@ protected:
void checkPatch(void *b, int blen, int boff, int size); void checkPatch(void *b, int blen, int boff, int size);
// relocation util // relocation util
static upx_byte *optimizeReloc( static upx_byte *optimizeReloc(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image,
upx_byte *in, unsigned relocnum, unsigned headway, int bs, int *big, int bits);
upx_byte *out, upx_byte *image, unsigned headway, static unsigned unoptimizeReloc(upx_byte **in, upx_byte *image, MemBuffer *out, int bs,
int bs, int *big, int bits); int bits);
static unsigned unoptimizeReloc(upx_byte **in, upx_byte *image, MemBuffer *out, int bs, int bits);
static upx_byte *optimizeReloc32( static upx_byte *optimizeReloc32(upx_byte *in, unsigned relocnum, upx_byte *out,
upx_byte *in, unsigned relocnum, upx_byte *image, unsigned headway, int bs, int *big);
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 unsigned unoptimizeReloc32(upx_byte **in, upx_byte *image, MemBuffer *out, int bs);
static upx_byte *optimizeReloc64( static upx_byte *optimizeReloc64(upx_byte *in, unsigned relocnum, upx_byte *out,
upx_byte *in, unsigned relocnum, upx_byte *image, unsigned headway, int bs, int *big);
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); static unsigned unoptimizeReloc64(upx_byte **in, upx_byte *image, MemBuffer *out, int bs);
// target endianness abstraction // target endianness abstraction
@ -319,9 +308,9 @@ protected:
int ph_version; int ph_version;
// compression buffers // compression buffers
MemBuffer ibuf; // input MemBuffer ibuf; // input
MemBuffer obuf; // output MemBuffer obuf; // output
unsigned ibufgood; // high-water mark in ibuf (pefile.cpp) unsigned ibufgood; // high-water mark in ibuf (pefile.cpp)
// UI handler // UI handler
UiPacker *uip = nullptr; UiPacker *uip = nullptr;
@ -341,8 +330,8 @@ private:
Packer &operator=(const Packer &) = delete; Packer &operator=(const Packer &) = delete;
}; };
int force_method(int method); // (0x80ul<<24)|method int force_method(int method); // (0x80ul<<24)|method
int forced_method(int method); // (0x80ul<<24)|method ==> method int forced_method(int method); // (0x80ul<<24)|method ==> method
int is_forced_method(int method); // predicate int is_forced_method(int method); // predicate
#endif /* already included */ #endif /* already included */