Merge branch 'devel' of https://github.com/upx/upx into devel

This commit is contained in:
John Reiser
2017-04-06 09:05:46 -07:00
6 changed files with 96 additions and 167 deletions
-1
View File
@@ -34,7 +34,6 @@ init:
# - dir "c:\mingw-w64\i686-5.3.0-posix-dwarf-rt_v4-rev0\mingw32\bin" # - dir "c:\mingw-w64\i686-5.3.0-posix-dwarf-rt_v4-rev0\mingw32\bin"
install: install:
- set PATH=%PATH%;C:\Program Files\Git\mingw64\bin
- cd c:\projects - cd c:\projects
- md build build\%C% deps - md build build\%C% deps
- cd c:\projects\upx - cd c:\projects\upx
+2 -1
View File
@@ -51,11 +51,12 @@ if [[ $BM_X =~ (^|\+)rebuild-stubs($|\+) ]]; then
extra_subdirs+=( src/stub/src/arch/amd64 ) extra_subdirs+=( src/stub/src/arch/amd64 )
extra_subdirs+=( src/stub/src/arch/arm/v4a ) extra_subdirs+=( src/stub/src/arch/arm/v4a )
extra_subdirs+=( src/stub/src/arch/arm/v4t ) extra_subdirs+=( src/stub/src/arch/arm/v4t )
#extra_subdirs+=( src/stub/src/arch/arm64/v8 ) # FIXME / TODO
extra_subdirs+=( src/stub/src/arch/i386 ) extra_subdirs+=( src/stub/src/arch/i386 )
extra_subdirs+=( src/stub/src/arch/m68k/m68000 ) extra_subdirs+=( src/stub/src/arch/m68k/m68000 )
extra_subdirs+=( src/stub/src/arch/m68k/m68020 ) extra_subdirs+=( src/stub/src/arch/m68k/m68020 )
extra_subdirs+=( src/stub/src/arch/mips/r3000 ) extra_subdirs+=( src/stub/src/arch/mips/r3000 )
#extra_subdirs+=( src/stub/src/arch/powerpc/32 ) # FIXME / TODO extra_subdirs+=( src/stub/src/arch/powerpc/32 )
extra_subdirs+=( src/stub/src/arch/powerpc/64le ) extra_subdirs+=( src/stub/src/arch/powerpc/64le )
make -C src/stub maintainer-clean make -C src/stub maintainer-clean
for d in ${extra_subdirs[@]}; do for d in ${extra_subdirs[@]}; do
+1 -1
View File
@@ -163,7 +163,7 @@ endif
# automatically format some C++ source code files # automatically format some C++ source code files
ifeq ($(shell uname),Linux) ifeq ($(shell uname),Linux)
CLANG_FORMAT_FILES += linker.cpp linker.h packhead.cpp CLANG_FORMAT_FILES += linker.cpp linker.h packhead.cpp packmast.cpp
CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h
CLANG_FORMAT_FILES += snprintf.cpp CLANG_FORMAT_FILES += snprintf.cpp
CLANG_FORMAT_FILES += stdcxx.cpp stdcxx.h CLANG_FORMAT_FILES += stdcxx.cpp stdcxx.h
+7 -8
View File
@@ -474,7 +474,8 @@ void ElfLinker::relocate() {
upx_byte *location = rel->section->output + rel->offset; upx_byte *location = rel->section->output + rel->offset;
// printf("%-28s %-28s %-10s %#16llx %#16llx\n", rel->section->name, rel->value->name, // printf("%-28s %-28s %-10s %#16llx %#16llx\n", rel->section->name, rel->value->name,
// rel->type, (long long) value, (long long) value - rel->section->offset - rel->offset); // rel->type, (long long) value, (long long) value - rel->section->offset - rel->offset);
// printf(" %llx %d %llx %d %llx : %d\n", (long long) value, (int)rel->value->section->offset, // printf(" %llx %d %llx %d %llx : %d\n", (long long) value,
// (int)rel->value->section->offset,
// rel->value->offset, rel->offset, (long long) rel->add, *location); // rel->value->offset, rel->offset, (long long) rel->add, *location);
relocate1(rel, location, value, rel->type); relocate1(rel, location, value, rel->type);
} }
@@ -611,14 +612,12 @@ void ElfLinkerARM64::relocate1(const Relocation *rel, upx_byte *location, upx_ui
set_le32(location, get_le32(location) + value); set_le32(location, get_le32(location) + value);
} else if (!strcmp(type, "ABS64")) { } else if (!strcmp(type, "ABS64")) {
set_le64(location, get_le64(location) + value); set_le64(location, get_le64(location) + value);
} } else if (!strcmp(type, "CONDBR19")) {
else if (!strcmp(type, "CONDBR19")) {
value -= rel->section->offset + rel->offset; value -= rel->section->offset + rel->offset;
uint32_t const m19 = ~(~0u << 19); upx_uint32_t const m19 = ~(~0u << 19);
uint32_t w = get_le32(location); upx_uint32_t w = get_le32(location);
set_le32(location, (w &~(m19<<5)) | ((((w>>5) + (value>>2)) & m19) << 5) ); set_le32(location, (w & ~(m19 << 5)) | ((((w >> 5) + (value >> 2)) & m19) << 5));
} } else
else
super::relocate1(rel, location, value, type); super::relocate1(rel, location, value, type);
} }
+4 -4
View File
@@ -2374,7 +2374,7 @@ PackLinuxElf32::generateElfHdr(
set_te32(&h2->phdr[1].p_flags, Elf32_Phdr::PF_R | Elf32_Phdr::PF_W); set_te32(&h2->phdr[1].p_flags, Elf32_Phdr::PF_R | Elf32_Phdr::PF_W);
} }
if (ph.format==getFormat()) { if (ph.format==getFormat()) {
assert((2+ !!gnu_stack) == get_te16(&h2->ehdr.e_phnum)); assert((2u+ !!gnu_stack) == get_te16(&h2->ehdr.e_phnum));
set_te32(&h2->phdr[0].p_flags, ~Elf32_Phdr::PF_W & get_te32(&h2->phdr[0].p_flags)); set_te32(&h2->phdr[0].p_flags, ~Elf32_Phdr::PF_W & get_te32(&h2->phdr[0].p_flags));
if (!gnu_stack) { if (!gnu_stack) {
memset(&h2->linfo, 0, sizeof(h2->linfo)); memset(&h2->linfo, 0, sizeof(h2->linfo));
@@ -2635,7 +2635,7 @@ PackLinuxElf64::generateElfHdr(
set_te32(&h2->phdr[1].p_flags, Elf64_Phdr::PF_R | Elf64_Phdr::PF_W); set_te32(&h2->phdr[1].p_flags, Elf64_Phdr::PF_R | Elf64_Phdr::PF_W);
} }
if (ph.format==getFormat()) { if (ph.format==getFormat()) {
assert((2+ !!gnu_stack) == get_te16(&h2->ehdr.e_phnum)); assert((2u+ !!gnu_stack) == get_te16(&h2->ehdr.e_phnum));
set_te32(&h2->phdr[0].p_flags, ~Elf64_Phdr::PF_W & get_te32(&h2->phdr[0].p_flags)); set_te32(&h2->phdr[0].p_flags, ~Elf64_Phdr::PF_W & get_te32(&h2->phdr[0].p_flags));
if (!gnu_stack) { if (!gnu_stack) {
memset(&h2->linfo, 0, sizeof(h2->linfo)); memset(&h2->linfo, 0, sizeof(h2->linfo));
@@ -3537,7 +3537,7 @@ void PackLinuxElf32::pack4(OutputFile *fo, Filter &ft)
if (0!=xct_off) { // shared library if (0!=xct_off) { // shared library
if (opt->o_unix.android_shlib && shdri) { // dlopen() checks Elf32_Shdr vs Elf32_Phdr if (opt->o_unix.android_shlib && shdri) { // dlopen() checks Elf32_Shdr vs Elf32_Phdr
unsigned load0_hi = ~0; unsigned load0_hi = ~0u;
Elf32_Phdr const *phdr = phdri; Elf32_Phdr const *phdr = phdri;
for (unsigned j = 0; j < e_phnum; ++j, ++phdr) { for (unsigned j = 0; j < e_phnum; ++j, ++phdr) {
unsigned load0_lo = get_te32(&phdr->p_vaddr); unsigned load0_lo = get_te32(&phdr->p_vaddr);
@@ -3668,7 +3668,7 @@ void PackLinuxElf64::pack4(OutputFile *fo, Filter &ft)
if (0!=xct_off) { // shared library if (0!=xct_off) { // shared library
if (opt->o_unix.android_shlib && shdri) { // dlopen() checks Elf64_Shdr vs Elf64_Phdr if (opt->o_unix.android_shlib && shdri) { // dlopen() checks Elf64_Shdr vs Elf64_Phdr
unsigned load0_hi = ~0; unsigned load0_hi = ~0u;
Elf64_Phdr const *phdr = phdri; Elf64_Phdr const *phdr = phdri;
for (unsigned j = 0; j < e_phnum; ++j, ++phdr) { for (unsigned j = 0; j < e_phnum; ++j, ++phdr) {
upx_uint64_t load0_lo = get_te64(&phdr->p_vaddr); upx_uint64_t load0_lo = get_te64(&phdr->p_vaddr);
+82 -152
View File
@@ -25,7 +25,6 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#include "conf.h" #include "conf.h"
#include "file.h" #include "file.h"
#include "packmast.h" #include "packmast.h"
@@ -54,56 +53,48 @@
#include "p_mach.h" #include "p_mach.h"
#include "p_armpe.h" #include "p_armpe.h"
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
PackMaster::PackMaster(InputFile *f, options_t *o) : PackMaster::PackMaster(InputFile *f, options_t *o) : fi(f), p(NULL) {
fi(f), p(NULL)
{
// replace global options with local options // replace global options with local options
saved_opt = o; saved_opt = o;
if (o) if (o) {
{
memcpy(&this->local_options, o, sizeof(*o)); // struct copy memcpy(&this->local_options, o, sizeof(*o)); // struct copy
opt = &this->local_options; opt = &this->local_options;
} }
} }
PackMaster::~PackMaster() {
PackMaster::~PackMaster()
{
fi = NULL; fi = NULL;
delete p; p = NULL; delete p;
p = NULL;
// restore global options // restore global options
if (saved_opt) if (saved_opt)
opt = saved_opt; opt = saved_opt;
saved_opt = NULL; saved_opt = NULL;
} }
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
static Packer* try_pack(Packer *p, void *user) static Packer *try_pack(Packer *p, void *user) {
{
if (p == NULL) if (p == NULL)
return NULL; return NULL;
InputFile *f = (InputFile *) user; InputFile *f = (InputFile *) user;
p->assertPacker(); p->assertPacker();
try { try {
p->initPackHeader(); p->initPackHeader();
f->seek(0,SEEK_SET); f->seek(0, SEEK_SET);
if (p->canPack()) if (p->canPack()) {
{
if (opt->cmd == CMD_COMPRESS) if (opt->cmd == CMD_COMPRESS)
p->updatePackHeader(); p->updatePackHeader();
f->seek(0,SEEK_SET); f->seek(0, SEEK_SET);
return p; return p;
} }
} catch (const IOException&) { } catch (const IOException &) {
} catch (...) { } catch (...) {
delete p; delete p;
throw; throw;
@@ -112,28 +103,24 @@ static Packer* try_pack(Packer *p, void *user)
return NULL; return NULL;
} }
static Packer *try_unpack(Packer *p, void *user) {
static Packer* try_unpack(Packer *p, void *user)
{
if (p == NULL) if (p == NULL)
return NULL; return NULL;
InputFile *f = (InputFile *) user; InputFile *f = (InputFile *) user;
p->assertPacker(); p->assertPacker();
try { try {
p->initPackHeader(); p->initPackHeader();
f->seek(0,SEEK_SET); f->seek(0, SEEK_SET);
int r = p->canUnpack(); int r = p->canUnpack();
if (r > 0) if (r > 0) {
{ f->seek(0, SEEK_SET);
f->seek(0,SEEK_SET);
return p; return p;
} }
if (r < 0) if (r < 0) {
{
// FIXME - could stop testing all other unpackers at this time // FIXME - could stop testing all other unpackers at this time
// see canUnpack() in packer.h // see canUnpack() in packer.h
} }
} catch (const IOException&) { } catch (const IOException &) {
} catch (...) { } catch (...) {
delete p; delete p;
throw; throw;
@@ -142,155 +129,110 @@ static Packer* try_unpack(Packer *p, void *user)
return NULL; return NULL;
} }
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
Packer *PackMaster::visitAllPackers(visit_func_t func, InputFile *f, const options_t *o,
void *user) {
#define D(name) if (o->debug.debug_level) fprintf(stderr, "%s\n", #name)
Packer* PackMaster::visitAllPackers(visit_func_t func, InputFile *f, const options_t *o, void *user)
{
Packer *p = NULL; Packer *p = NULL;
#define D(klass) \
ACC_BLOCK_BEGIN \
if (o->debug.debug_level) \
fprintf(stderr, "visitAllPackers: %s\n", #klass); \
if ((p = func(new klass(f), user)) != NULL) \
return p; \
ACC_BLOCK_END
// note: order of tries is important ! // note: order of tries is important !
// //
// .exe // .exe
// //
if (!o->dos_exe.force_stub) if (!o->dos_exe.force_stub) {
{ D(PackDjgpp2);
D(PackDjgpp2); if ((p = func(new PackDjgpp2(f), user))) D(PackTmt);
return p; D(PackWcle);
D(PackTmt); if ((p = func(new PackTmt(f), user))) D(PackW64Pep);
return p; D(PackW32Pe);
D(PackWcle); if ((p = func(new PackWcle(f), user)))
return p;
D(PackW64Pep); if ((p = func(new PackW64Pep(f), user)))
return p;
D(PackW32Pe); if ((p = func(new PackW32Pe(f), user)))
return p;
} }
D(PackArmPe); if ((p = func(new PackArmPe(f), user))) D(PackArmPe);
return p; D(PackExe);
D(PackExe); if ((p = func(new PackExe(f), user)))
return p;
// //
// atari // atari
// //
D(PackTos); if ((p = func(new PackTos(f), user))) D(PackTos);
return p;
// //
// linux kernel // linux kernel
// //
D(PackVmlinuxARMEL); if ((p = func(new PackVmlinuxARMEL(f), user))) D(PackVmlinuxARMEL);
return p; D(PackVmlinuxARMEB);
D(PackVmlinuxARMEB); if ((p = func(new PackVmlinuxARMEB(f), user))) D(PackVmlinuxPPC32);
return p; D(PackVmlinuxPPC64LE);
D(PackVmlinuxPPC32); if ((p = func(new PackVmlinuxPPC32(f), user))) D(PackVmlinuxAMD64);
return p; D(PackVmlinuxI386);
D(PackVmlinuxPPC64LE); if ((p = func(new PackVmlinuxPPC64LE(f), user))) D(PackVmlinuzI386);
return p; D(PackBvmlinuzI386);
D(PackVmlinuxAMD64); if ((p = func(new PackVmlinuxAMD64(f), user))) D(PackVmlinuzARMEL);
return p;
D(PackVmlinuxI386); if ((p = func(new PackVmlinuxI386(f), user)))
return p;
D(PackVmlinuzI386); if ((p = func(new PackVmlinuzI386(f), user)))
return p;
D(PackBvmlinuzI386); if ((p = func(new PackBvmlinuzI386(f), user)))
return p;
D(PackVmlinuzARMEL); if ((p = func(new PackVmlinuzARMEL(f), user)))
return p;
// //
// linux // linux
// //
if (!o->o_unix.force_execve) if (!o->o_unix.force_execve) {
{
if (o->o_unix.use_ptinterp) { if (o->o_unix.use_ptinterp) {
D(PackLinuxElf32x86interp); if ((p = func(new PackLinuxElf32x86interp(f), user))) D(PackLinuxElf32x86interp);
return p;
} }
D(PackFreeBSDElf32x86); if ((p = func(new PackFreeBSDElf32x86(f), user))) D(PackFreeBSDElf32x86);
return p; D(PackNetBSDElf32x86);
D(PackNetBSDElf32x86); if ((p = func(new PackNetBSDElf32x86(f), user))) D(PackOpenBSDElf32x86);
return p; D(PackLinuxElf32x86);
D(PackOpenBSDElf32x86); if ((p = func(new PackOpenBSDElf32x86(f), user))) D(PackLinuxElf64amd);
return p; D(PackLinuxElf32armLe);
D(PackLinuxElf32x86); if ((p = func(new PackLinuxElf32x86(f), user))) D(PackLinuxElf32armBe);
return p; D(PackLinuxElf64arm);
D(PackLinuxElf64amd); if ((p = func(new PackLinuxElf64amd(f), user))) D(PackLinuxElf32ppc);
return p; D(PackLinuxElf64ppcle);
D(PackLinuxElf32armLe); if ((p = func(new PackLinuxElf32armLe(f), user))) D(PackLinuxElf32mipsel);
return p; D(PackLinuxElf32mipseb);
D(PackLinuxElf32armBe); if ((p = func(new PackLinuxElf32armBe(f), user))) D(PackLinuxI386sh);
return p;
D(PackLinuxElf64arm); if ((p = func(new PackLinuxElf64arm(f), user)))
return p;
D(PackLinuxElf32ppc); if ((p = func(new PackLinuxElf32ppc(f), user)))
return p;
D(PackLinuxElf64ppcle); if ((p = func(new PackLinuxElf64ppcle(f), user)))
return p;
D(PackLinuxElf32mipsel); if ((p = func(new PackLinuxElf32mipsel(f), user)))
return p;
D(PackLinuxElf32mipseb); if ((p = func(new PackLinuxElf32mipseb(f), user)))
return p;
D(PackLinuxI386sh); if ((p = func(new PackLinuxI386sh(f), user)))
return p;
} }
D(PackBSDI386); if ((p = func(new PackBSDI386(f), user))) D(PackBSDI386);
return p; D(PackMachFat); // cafebabe conflict
D(PackMachFat); if ((p = func(new PackMachFat(f), user))) // cafebabe conflict D(PackLinuxI386); // cafebabe conflict
return p;
D(PackLinuxI386); if ((p = func(new PackLinuxI386(f), user))) // cafebabe conflict
return p;
// //
// psone // psone
// //
D(PackPs1); if ((p = func(new PackPs1(f), user))) D(PackPs1);
return p;
// //
// .sys and .com // .sys and .com
// //
D(PackSys); if ((p = func(new PackSys(f), user))) D(PackSys);
return p; D(PackCom);
D(PackCom); if ((p = func(new PackCom(f), user)))
return p;
// Mach (MacOS X PowerPC) // Mach (MacOS X PowerPC)
D(PackMachPPC32); if ((p = func(new PackMachPPC32(f), user))) D(PackMachPPC32);
return p; D(PackMachPPC64LE);
D(PackMachPPC64LE); if ((p = func(new PackMachPPC64LE(f), user))) D(PackMachI386);
return p; D(PackMachAMD64);
D(PackMachI386); if ((p = func(new PackMachI386(f), user))) D(PackMachARMEL);
return p;
D(PackMachAMD64); if ((p = func(new PackMachAMD64(f), user)))
return p;
D(PackMachARMEL); if ((p = func(new PackMachARMEL(f), user)))
return p;
// 2010-03-12 omit these because PackMachBase<T>::pack4dylib (p_mach.cpp) // 2010-03-12 omit these because PackMachBase<T>::pack4dylib (p_mach.cpp)
// does not understand what the Darwin (Apple Mac OS X) dynamic loader // does not understand what the Darwin (Apple Mac OS X) dynamic loader
// assumes about .dylib file structure. // assumes about .dylib file structure.
// D(PackDylibI386); if ((p = func(new PackDylibI386(f), user))) // D(PackDylibI386);
// return p; // D(PackDylibPPC32);
// D(PackDylibPPC32); if ((p = func(new PackDylibPPC32(f), user))) // D(PackDylibAMD64);
// return p;
// D(PackDylibAMD64); if ((p = func(new PackDylibAMD64(f), user)))
// return p;
return NULL; return NULL;
#undef D
} }
Packer *PackMaster::getPacker(InputFile *f) {
Packer *PackMaster::getPacker(InputFile *f)
{
Packer *pp = visitAllPackers(try_pack, f, opt, f); Packer *pp = visitAllPackers(try_pack, f, opt, f);
if (!pp) if (!pp)
throwUnknownExecutableFormat(); throwUnknownExecutableFormat();
@@ -298,9 +240,7 @@ Packer *PackMaster::getPacker(InputFile *f)
return pp; return pp;
} }
Packer *PackMaster::getUnpacker(InputFile *f) {
Packer *PackMaster::getUnpacker(InputFile *f)
{
Packer *pp = visitAllPackers(try_unpack, f, opt, f); Packer *pp = visitAllPackers(try_unpack, f, opt, f);
if (!pp) if (!pp)
throwNotPacked(); throwNotPacked();
@@ -308,51 +248,41 @@ Packer *PackMaster::getUnpacker(InputFile *f)
return pp; return pp;
} }
/************************************************************************* /*************************************************************************
// delegation // delegation
**************************************************************************/ **************************************************************************/
void PackMaster::pack(OutputFile *fo) void PackMaster::pack(OutputFile *fo) {
{
p = getPacker(fi); p = getPacker(fi);
fi = NULL; fi = NULL;
p->doPack(fo); p->doPack(fo);
} }
void PackMaster::unpack(OutputFile *fo) {
void PackMaster::unpack(OutputFile *fo)
{
p = getUnpacker(fi); p = getUnpacker(fi);
p->assertPacker(); p->assertPacker();
fi = NULL; fi = NULL;
p->doUnpack(fo); p->doUnpack(fo);
} }
void PackMaster::test() {
void PackMaster::test()
{
p = getUnpacker(fi); p = getUnpacker(fi);
fi = NULL; fi = NULL;
p->doTest(); p->doTest();
} }
void PackMaster::list() {
void PackMaster::list()
{
p = getUnpacker(fi); p = getUnpacker(fi);
fi = NULL; fi = NULL;
p->doList(); p->doList();
} }
void PackMaster::fileInfo() {
void PackMaster::fileInfo()
{
p = visitAllPackers(try_unpack, fi, opt, fi); p = visitAllPackers(try_unpack, fi, opt, fi);
if (!p) if (!p)
p = visitAllPackers(try_pack, fi, opt, fi); p = visitAllPackers(try_pack, fi, opt, fi);
if (!p) if (!p)
throwUnknownExecutableFormat(NULL, 1); // make a warning here throwUnknownExecutableFormat(NULL, 1); // make a warning here
p->assertPacker(); p->assertPacker();
fi = NULL; fi = NULL;
p->doFileInfo(); p->doFileInfo();