From 5d74b7252dbe6a17b5d6436dab3019c95d17c71b Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Thu, 21 Dec 2000 18:12:54 +0000 Subject: [PATCH] Cleaned up the use of upx_compress(), use delete[] where needed. committer: mfx 977422374 +0000 --- src/.cvsignore | 4 ++++ src/Makefile | 2 +- src/p_lx_elf.cpp | 29 ++++++++++++---------------- src/p_lx_sh.cpp | 50 ++++++++++++++++++++++-------------------------- src/p_lx_sh.h | 2 -- src/p_unix.cpp | 16 +++++----------- 6 files changed, 45 insertions(+), 58 deletions(-) diff --git a/src/.cvsignore b/src/.cvsignore index 9d537e6c..3092b3a2 100644 --- a/src/.cvsignore +++ b/src/.cvsignore @@ -1,6 +1,10 @@ *.0?? +*.dat *.idb +*.img *.map +*.raw +*.rel *.pdb *.upx .gdbinit diff --git a/src/Makefile b/src/Makefile index bf850507..134d2de2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -461,7 +461,7 @@ p_exe$o: packer.h p_exe.h \ p_lx_elf$o: packer.h p_lx_elf.h p_unix.h p_elf.h \ stub/l_le_n2b.h stub/l_le_n2d.h p_lx_sep$o: packer.h p_lx_sep.h p_lx_elf.h p_unix.h p_elf.h -p_lx_sh$o: packer.h p_lx_sh.h p_lx_elf.h p_unix.h p_elf.h \ +p_lx_sh$o: packer.h p_lx_sh.h p_unix.h p_elf.h \ stub/l_sh_n2b.h stub/l_sh_n2d.h p_sys$o: packer.h p_sys.h p_com.h \ stub/l_sys.h diff --git a/src/p_lx_elf.cpp b/src/p_lx_elf.cpp index 9ea8b0bf..d639e95d 100644 --- a/src/p_lx_elf.cpp +++ b/src/p_lx_elf.cpp @@ -46,12 +46,12 @@ static const PackLinuxI386elf::~PackLinuxI386elf() { - delete phdri; + delete[] phdri; } PackLinuxI386elf::PackLinuxI386elf(InputFile *f) :super(f) - ,phdri(0) + ,phdri(NULL) { } @@ -127,19 +127,14 @@ void PackLinuxI386elf::patchLoader() MemBuffer cprLoader(lsize); // compress compiled C-code portion of loader - upx_compress_config_t conf; memset(&conf, 0xff, sizeof(conf)); - conf.c_flags = 0; - upx_uint result_buffer[16]; - upx_uint cprLsize; - upx_compress( - loader + fold_begin, lsize - fold_begin, - cprLoader, &cprLsize, - 0, // progress_callback_t ?? - getCompressionMethod(), 9, - &conf, - result_buffer - ); - set_le32(0+fold_begin+loader, lsize - fold_begin); + upx_uint const uncLsize = lsize - fold_begin; + upx_uint cprLsize; + int r = upx_compress(loader + fold_begin, uncLsize, cprLoader, &cprLsize, + NULL, opt->method, 10, NULL, NULL); + if (r != UPX_E_OK || cprLsize >= uncLsize) + throwInternalError("loaded compression failed"); + + set_le32(0+fold_begin+loader, uncLsize); set_le32(4+fold_begin+loader, cprLsize); memcpy( 8+fold_begin+loader, cprLoader, cprLsize); lsize = 8 + fold_begin + cprLsize; @@ -156,8 +151,8 @@ void PackLinuxI386elf::patchLoader() // The beginning of our loader consists of a elf_hdr (52 bytes) and // two sections elf_phdr (2 * 32 byte), so we have 12 free bytes // from offset 116 to the program start at offset 128. - assert(ehdr->e_phoff == sizeof(*ehdr)); - assert(ehdr->e_ehsize == sizeof(*ehdr)); + assert(ehdr->e_phoff == sizeof(Elf_LE32_Ehdr)); + assert(ehdr->e_ehsize == sizeof(Elf_LE32_Ehdr)); assert(ehdr->e_phentsize == sizeof(Elf_LE32_Phdr)); assert(ehdr->e_phnum == 2); assert(ehdr->e_shnum == 0); diff --git a/src/p_lx_sh.cpp b/src/p_lx_sh.cpp index de7d9593..a711f3ab 100644 --- a/src/p_lx_sh.cpp +++ b/src/p_lx_sh.cpp @@ -94,52 +94,48 @@ static off_t getbrk(Elf_LE32_Phdr const *phdr, int e_phnum) void PackLinuxI386sh::patchLoader() { lsize = getLoaderSize(); - ehdri = (Elf_LE32_Ehdr *)(void *)loader; - Elf_LE32_Phdr *const phdri = (Elf_LE32_Phdr *)(1+ehdri); + Elf_LE32_Ehdr *const ehdr = (Elf_LE32_Ehdr *)(void *)loader; + Elf_LE32_Phdr *const phdr = (Elf_LE32_Phdr *)(1+ehdr); patch_le32(loader,lsize,"UPX3",l_shname); patch_le32(loader,lsize,"UPX2",o_shname); // stub/scripts/setfold.pl puts address of 'fold_begin' in phdr[1].p_offset - off_t const fold_begin = phdri[1].p_offset; + off_t const fold_begin = phdr[1].p_offset; assert(fold_begin > 0); assert(fold_begin < (off_t)lsize); MemBuffer cprLoader(lsize); // compress compiled C-code portion of loader - upx_compress_config_t conf; memset(&conf, 0xff, sizeof(conf)); - conf.c_flags = 0; - upx_uint result_buffer[16]; - upx_uint cprLsize; - upx_compress( - loader + fold_begin, lsize - fold_begin, - cprLoader, &cprLsize, - 0, // progress_callback_t ?? - getCompressionMethod(), 9, - &conf, - result_buffer - ); - set_le32(0+fold_begin+loader, lsize - fold_begin); + upx_uint const uncLsize = lsize - fold_begin; + upx_uint cprLsize; + int r = upx_compress(loader + fold_begin, uncLsize, cprLoader, &cprLsize, + NULL, opt->method, 10, NULL, NULL); + if (r != UPX_E_OK || cprLsize >= uncLsize) + throwInternalError("loaded compression failed"); + + set_le32(0+fold_begin+loader, uncLsize); set_le32(4+fold_begin+loader, cprLsize); memcpy( 8+fold_begin+loader, cprLoader, cprLsize); lsize = 8 + fold_begin + cprLsize; patchVersion(loader,lsize); - unsigned const brka = getbrk(phdri, ehdri->e_phnum); - phdri[1].p_offset = 0xfff&brka; - phdri[1].p_vaddr = brka; - phdri[1].p_paddr = brka; - phdri[1].p_filesz = 0; - phdri[1].p_memsz = 0; + // Info for OS kernel to set the brk() + unsigned const brka = getbrk(phdr, ehdr->e_phnum); + phdr[1].p_offset = 0xfff&brka; + phdr[1].p_vaddr = brka; + phdr[1].p_paddr = brka; + phdr[1].p_filesz = 0; + phdr[1].p_memsz = 0; // The beginning of our loader consists of a elf_hdr (52 bytes) and // two sections elf_phdr (2 * 32 byte), so we have 12 free bytes // from offset 116 to the program start at offset 128. - assert(ehdri->e_phoff == sizeof(Elf_LE32_Ehdr)); - assert(ehdri->e_ehsize == sizeof(Elf_LE32_Ehdr)); - assert(ehdri->e_phentsize == sizeof(Elf_LE32_Phdr)); - assert(ehdri->e_phnum == 2); - assert(ehdri->e_shnum == 0); + assert(ehdr->e_phoff == sizeof(Elf_LE32_Ehdr)); + assert(ehdr->e_ehsize == sizeof(Elf_LE32_Ehdr)); + assert(ehdr->e_phentsize == sizeof(Elf_LE32_Phdr)); + assert(ehdr->e_phnum == 2); + assert(ehdr->e_shnum == 0); assert(lsize > 128 && lsize < 4096); patchLoaderChecksum(); diff --git a/src/p_lx_sh.h b/src/p_lx_sh.h index bb363a39..4bc1b181 100644 --- a/src/p_lx_sh.h +++ b/src/p_lx_sh.h @@ -62,8 +62,6 @@ protected: virtual void patchLoader(); - Elf_LE32_Ehdr *ehdri; // from input file - int o_shname; // offset to name_of_shell int l_shname; // length of name_of_shell }; diff --git a/src/p_unix.cpp b/src/p_unix.cpp index cd305cd2..c6db5aa8 100644 --- a/src/p_unix.cpp +++ b/src/p_unix.cpp @@ -445,19 +445,13 @@ void PackLinuxI386::patchLoader() MemBuffer cprLoader(lsize); // compress compiled C-code portion of loader - upx_compress_config_t conf; memset(&conf, 0xff, sizeof(conf)); - conf.c_flags = 0; - upx_uint result_buffer[16]; upx_uint const uncLsize = lsize - fold_begin; upx_uint cprLsize; - upx_compress( - loader + fold_begin, uncLsize, - cprLoader, &cprLsize, - 0, // progress_callback_t ?? - getCompressionMethod(), 9, - &conf, - result_buffer - ); + int r = upx_compress(loader + fold_begin, uncLsize, cprLoader, &cprLsize, + NULL, opt->method, 10, NULL, NULL); + if (r != UPX_E_OK || cprLsize >= uncLsize) + throwInternalError("loaded compression failed"); + memcpy(fold_begin+loader, cprLoader, cprLsize); lsize = fold_begin + cprLsize; phdr->p_filesz = lsize;