diff --git a/src/linker.cpp b/src/linker.cpp index bc9d4cdd..80713f93 100644 --- a/src/linker.cpp +++ b/src/linker.cpp @@ -103,12 +103,13 @@ ElfLinker::Relocation::Relocation(const Section *s, unsigned o, const char *t, **************************************************************************/ ElfLinker::ElfLinker() : + bele(&N_BELE_RTP::le_policy), input(NULL), output(NULL), head(NULL), tail(NULL), sections(NULL), symbols(NULL), relocations(NULL), nsections(0), nsections_capacity(0), nsymbols(0), nsymbols_capacity(0), nrelocations(0), nrelocations_capacity(0), - reloc_done(false), bele_policy(&N_BELE_RTP::le_policy) + reloc_done(false) { } diff --git a/src/linker.h b/src/linker.h index 2b539cf7..2d3ae6c4 100644 --- a/src/linker.h +++ b/src/linker.h @@ -36,6 +36,8 @@ class ElfLinker : private nocopy { +public: + const N_BELE_RTP::AbstractPolicy *bele; protected: struct Section; struct Symbol; @@ -61,8 +63,6 @@ protected: unsigned nrelocations_capacity; bool reloc_done; -public: - const N_BELE_RTP::AbstractPolicy *bele_policy; protected: void preprocessSections(char *start, const char *end); @@ -159,7 +159,7 @@ class ElfLinkerArmBE : public ElfLinker { typedef ElfLinker super; public: - ElfLinkerArmBE() { bele_policy = &N_BELE_RTP::be_policy; } + ElfLinkerArmBE() { bele = &N_BELE_RTP::be_policy; } protected: virtual void relocate1(const Relocation *, upx_byte *location, unsigned value, const char *type); @@ -179,7 +179,7 @@ class ElfLinkerM68k : public ElfLinker { typedef ElfLinker super; public: - ElfLinkerM68k() { bele_policy = &N_BELE_RTP::be_policy; } + ElfLinkerM68k() { bele = &N_BELE_RTP::be_policy; } protected: virtual void alignCode(unsigned len); virtual void relocate1(const Relocation *, upx_byte *location, @@ -200,7 +200,7 @@ class ElfLinkerPpc32 : public ElfLinker { typedef ElfLinker super; public: - ElfLinkerPpc32() { bele_policy = &N_BELE_RTP::be_policy; } + ElfLinkerPpc32() { bele = &N_BELE_RTP::be_policy; } protected: virtual void relocate1(const Relocation *, upx_byte *location, unsigned value, const char *type); diff --git a/src/p_com.h b/src/p_com.h index 80eca2d9..bc5dbd98 100644 --- a/src/p_com.h +++ b/src/p_com.h @@ -38,7 +38,7 @@ class PackCom : public Packer { typedef Packer super; public: - PackCom(InputFile *f) : super(f) { } + PackCom(InputFile *f) : super(f) { bele = &N_BELE_RTP::le_policy; } virtual int getVersion() const { return 13; } virtual int getFormat() const { return UPX_F_DOS_COM; } virtual const char *getName() const { return "dos/com"; } diff --git a/src/p_djgpp2.cpp b/src/p_djgpp2.cpp index eb8ed9a5..08a410bd 100644 --- a/src/p_djgpp2.cpp +++ b/src/p_djgpp2.cpp @@ -46,6 +46,7 @@ static const 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); COMPILE_TIME_ASSERT(sizeof(stubify_stub) == 2048); diff --git a/src/p_exe.cpp b/src/p_exe.cpp index f5f46649..9d712ff5 100644 --- a/src/p_exe.cpp +++ b/src/p_exe.cpp @@ -50,6 +50,7 @@ static const PackExe::PackExe(InputFile *f) : super(f) { + bele = &N_BELE_RTP::le_policy; COMPILE_TIME_ASSERT(sizeof(exe_header_t) == 32); ih_exesize = ih_imagesize = ih_overlay = 0; } diff --git a/src/p_lx_elf.h b/src/p_lx_elf.h index f0bd2c64..bab2b8ee 100644 --- a/src/p_lx_elf.h +++ b/src/p_lx_elf.h @@ -233,7 +233,7 @@ class PackLinuxElf32Be : public PackLinuxElf32 { typedef PackLinuxElf32 super; protected: - PackLinuxElf32Be(InputFile *f) : super(f) { } + PackLinuxElf32Be(InputFile *f) : super(f) { bele = &N_BELE_RTP::be_policy; } virtual acc_uint64l_t get_native64(const void *b) const { return get_be64(b); } virtual unsigned get_native32(const void *b) const { return get_be32(b); } @@ -247,7 +247,7 @@ class PackLinuxElf32Le : public PackLinuxElf32 { typedef PackLinuxElf32 super; protected: - PackLinuxElf32Le(InputFile *f) : super(f) { } + PackLinuxElf32Le(InputFile *f) : super(f) { bele = &N_BELE_RTP::le_policy; } virtual acc_uint64l_t get_native64(const void *b) const { return get_le64(b); } virtual unsigned get_native32(const void *b) const { return get_le32(b); } @@ -261,7 +261,7 @@ class PackLinuxElf64Le : public PackLinuxElf64 { typedef PackLinuxElf64 super; protected: - PackLinuxElf64Le(InputFile *f) : super(f) { } + PackLinuxElf64Le(InputFile *f) : super(f) { bele = &N_BELE_RTP::le_policy; } virtual acc_uint64l_t get_native64(const void *b) const { return get_le64(b); } virtual unsigned get_native32(const void *b) const { return get_le32(b); } diff --git a/src/p_lx_exc.cpp b/src/p_lx_exc.cpp index 1232ab47..a6a86565 100644 --- a/src/p_lx_exc.cpp +++ b/src/p_lx_exc.cpp @@ -57,6 +57,7 @@ PackLinuxI386::PackLinuxI386(InputFile *f) : super(f), ei_osabi(Elf32_Ehdr::ELFOSABI_LINUX), osabi_note(NULL) { + bele = &N_BELE_RTP::le_policy; } PackBSDI386::PackBSDI386(InputFile *f) : super(f) @@ -378,7 +379,7 @@ PackLinuxI386::buildLinuxLoader( (res->lit_context_bits << 0) | (res->lit_pos_bits << 8) | (res->pos_bits << 16); - if (linker->bele_policy->isBE()) // big endian - bswap32 + if (linker->bele->isBE()) // big endian - bswap32 acc_swab32s(&properties); linker->defineSymbol("lzma_properties", properties); // -2 for properties diff --git a/src/p_ps1.cpp b/src/p_ps1.cpp index 8bab03f1..1b385eac 100644 --- a/src/p_ps1.cpp +++ b/src/p_ps1.cpp @@ -82,6 +82,7 @@ PackPs1::PackPs1(InputFile *f) : 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); COMPILE_TIME_ASSERT(sizeof(ps1_exe_hb_t) == 44); COMPILE_TIME_ASSERT(sizeof(ps1_exe_chb_t) == 5); diff --git a/src/p_tmt.cpp b/src/p_tmt.cpp index 8ddaabb1..53bc5de0 100644 --- a/src/p_tmt.cpp +++ b/src/p_tmt.cpp @@ -45,6 +45,7 @@ static const PackTmt::PackTmt(InputFile *f) : super(f) { + bele = &N_BELE_RTP::le_policy; COMPILE_TIME_ASSERT(sizeof(tmt_header_t) == 44); } diff --git a/src/p_tos.cpp b/src/p_tos.cpp index ffe9977b..66010091 100644 --- a/src/p_tos.cpp +++ b/src/p_tos.cpp @@ -49,6 +49,7 @@ static const PackTos::PackTos(InputFile *f) : super(f) { + bele = &N_BELE_RTP::be_policy; COMPILE_TIME_ASSERT(FH_SIZE == 28); } diff --git a/src/p_unix.h b/src/p_unix.h index 85ab9a99..6c580f6f 100644 --- a/src/p_unix.h +++ b/src/p_unix.h @@ -141,7 +141,7 @@ class PackUnixBe32 : public PackUnix { typedef PackUnix super; protected: - PackUnixBe32(InputFile *f) : super(f) { } + PackUnixBe32(InputFile *f) : super(f) { bele = &N_BELE_RTP::be_policy; } virtual acc_uint64l_t get_native64(const void *b) const { return get_be64(b); } virtual unsigned get_native32(const void *b) const { return get_be32(b); } virtual unsigned get_native16(const void *b) const { return get_be16(b); } @@ -182,7 +182,7 @@ class PackUnixLe32 : public PackUnix { typedef PackUnix super; protected: - PackUnixLe32(InputFile *f) : super(f) { } + PackUnixLe32(InputFile *f) : super(f) { bele = &N_BELE_RTP::le_policy; } virtual acc_uint64l_t get_native64(const void *b) const { return get_le64(b); } virtual unsigned get_native32(const void *b) const { return get_le32(b); } virtual unsigned get_native16(const void *b) const { return get_le16(b); } diff --git a/src/p_vmlinx.cpp b/src/p_vmlinx.cpp index c7745b0d..528dd8dc 100644 --- a/src/p_vmlinx.cpp +++ b/src/p_vmlinx.cpp @@ -48,6 +48,7 @@ static const PackVmlinuxI386::PackVmlinuxI386(InputFile *f) : super(f), n_ptload(0), phdri(NULL), shdri(NULL), shstrtab(NULL) { + bele = &N_BELE_RTP::le_policy; } PackVmlinuxI386::~PackVmlinuxI386() diff --git a/src/p_vmlinz.cpp b/src/p_vmlinz.cpp index 34b8f105..e2e18a38 100644 --- a/src/p_vmlinz.cpp +++ b/src/p_vmlinz.cpp @@ -53,6 +53,7 @@ static const unsigned bzimage_offset = 0x100000; PackVmlinuzI386::PackVmlinuzI386(InputFile *f) : super(f), physical_start(0x100000) { + bele = &N_BELE_RTP::le_policy; COMPILE_TIME_ASSERT(sizeof(boot_sect_t) == 0x218); } @@ -415,7 +416,7 @@ void PackBvmlinuzI386::pack(OutputFile *fo) (res->lit_context_bits << 0) | (res->lit_pos_bits << 8) | (res->pos_bits << 16); - if (linker->bele_policy->isBE()) // big endian - bswap32 + if (linker->bele->isBE()) // big endian - bswap32 acc_swab32s(&properties); linker->defineSymbol("lzma_properties", properties); // -2 for properties diff --git a/src/p_w16ne.cpp b/src/p_w16ne.cpp index 73a75cdb..d4066555 100644 --- a/src/p_w16ne.cpp +++ b/src/p_w16ne.cpp @@ -41,6 +41,7 @@ PackW16Ne::PackW16Ne(InputFile *f) : super(f) { + bele = &N_BELE_RTP::le_policy; } diff --git a/src/p_wcle.h b/src/p_wcle.h index 1f1c4ae1..1d634812 100644 --- a/src/p_wcle.h +++ b/src/p_wcle.h @@ -38,7 +38,7 @@ class PackWcle : public Packer, public LeFile { typedef Packer super; public: - PackWcle(InputFile *f) : super(f), LeFile(f){}; + PackWcle(InputFile *f) : super(f), LeFile(f) { bele = &N_BELE_RTP::le_policy; } virtual int getVersion() const { return 13; } virtual int getFormat() const { return UPX_F_WATCOM_LE; } virtual const char *getName() const { return "watcom/le"; } diff --git a/src/packer.cpp b/src/packer.cpp index bda1bf19..fccfda7f 100644 --- a/src/packer.cpp +++ b/src/packer.cpp @@ -40,6 +40,7 @@ **************************************************************************/ Packer::Packer(InputFile *f) : + bele(NULL), fi(f), file_size(-1), ph_format(-1), ph_version(-1), uip(NULL), linker(NULL), last_patch(NULL), last_patch_len(0), last_patch_off(0) @@ -54,6 +55,29 @@ Packer::~Packer() { delete uip; uip = NULL; delete linker; linker = NULL; +#if 0 + initLoader(NULL, 0); + delete linker; linker = NULL; +#endif +} + + +// for PackMaster +void Packer::assertPacker() +{ + assert(getFormat() > 0); + assert(getFormat() <= 255); + assert(getVersion() >= 11); + assert(getVersion() <= 14); + assert(strlen(getName()) <= 13); + if (bele == NULL) fprintf(stderr, "%s\n", getName()); + assert(bele != NULL); +#if 1 + Linker *l = newLinker(); + if (bele != l->bele) fprintf(stderr, "%s\n", getName()); + assert(bele == l->bele); + delete l; +#endif } @@ -993,6 +1017,7 @@ void Packer::initLoader(const void *pdata, int plen, int small) { delete linker; linker = newLinker(); + assert(bele == linker->bele); linker->init(pdata, plen); unsigned size; diff --git a/src/packer.h b/src/packer.h index b7b836bf..fa0c404d 100644 --- a/src/packer.h +++ b/src/packer.h @@ -119,6 +119,7 @@ protected: Packer(InputFile *f); public: virtual ~Packer(); + virtual void assertPacker(); virtual int getVersion() const = 0; // A unique integer ID for this executable format. See conf.h. @@ -264,6 +265,7 @@ protected: static unsigned unoptimizeReloc32(upx_byte **in,upx_byte *image,MemBuffer *out,int bs); protected: + const N_BELE_RTP::AbstractPolicy *bele; InputFile *fi; off_t file_size; // will get set by constructor PackHeader ph; // must be filled by canUnpack() diff --git a/src/packer_c.cpp b/src/packer_c.cpp index 87513c67..bdd8acaa 100644 --- a/src/packer_c.cpp +++ b/src/packer_c.cpp @@ -257,7 +257,7 @@ void Packer::defineDecompressorSymbols() (res->lit_context_bits << 0) | (res->lit_pos_bits << 8) | (res->pos_bits << 16); - if (linker->bele_policy->isBE()) // big endian - bswap32 + if (linker->bele->isBE()) // big endian - bswap32 acc_swab32s(&properties); linker->defineSymbol("lzma_properties", properties); // -2 for properties diff --git a/src/packmast.cpp b/src/packmast.cpp index 987ff241..213d8ceb 100644 --- a/src/packmast.cpp +++ b/src/packmast.cpp @@ -55,6 +55,7 @@ #include "p_ps1.h" #include "p_mach.h" #include "p_armpe.h" +#include "linker.h" /************************************************************************* @@ -96,6 +97,7 @@ static Packer* try_pack(Packer *p, InputFile *f) { if (p == NULL) return NULL; + p->assertPacker(); try { p->initPackHeader(); f->seek(0,SEEK_SET); @@ -120,6 +122,7 @@ static Packer* try_unpack(Packer *p, InputFile *f) { if (p == NULL) return NULL; + p->assertPacker(); try { p->initPackHeader(); f->seek(0,SEEK_SET); @@ -279,16 +282,6 @@ static Packer *getUnpacker(InputFile *f) } -static void assertPacker(const Packer *p) -{ - assert(p->getFormat() > 0); - assert(p->getFormat() <= 255); - assert(p->getVersion() >= 11); - assert(p->getVersion() <= 14); - assert(strlen(p->getName()) <= 13); -} - - /************************************************************************* // delegation **************************************************************************/ @@ -296,7 +289,7 @@ static void assertPacker(const Packer *p) void PackMaster::pack(OutputFile *fo) { p = getPacker(fi); - assertPacker(p); + p->assertPacker(); fi = NULL; p->doPack(fo); } @@ -305,7 +298,7 @@ void PackMaster::pack(OutputFile *fo) void PackMaster::unpack(OutputFile *fo) { p = getUnpacker(fi); - assertPacker(p); + p->assertPacker(); fi = NULL; p->doUnpack(fo); } @@ -314,7 +307,7 @@ void PackMaster::unpack(OutputFile *fo) void PackMaster::test() { p = getUnpacker(fi); - assertPacker(p); + p->assertPacker(); fi = NULL; p->doTest(); } @@ -323,7 +316,7 @@ void PackMaster::test() void PackMaster::list() { p = getUnpacker(fi); - assertPacker(p); + p->assertPacker(); fi = NULL; p->doList(); } @@ -336,7 +329,7 @@ void PackMaster::fileInfo() p = try_packers(fi, try_pack); if (!p) throwUnknownExecutableFormat(NULL, 1); // make a warning here - assertPacker(p); + p->assertPacker(); fi = NULL; p->doFileInfo(); } diff --git a/src/pefile.cpp b/src/pefile.cpp index 546c72f6..cd00fc2b 100644 --- a/src/pefile.cpp +++ b/src/pefile.cpp @@ -109,6 +109,7 @@ static void xcheck(size_t poff, size_t plen, const void *b, size_t blen) PeFile::PeFile(InputFile *f) : super(f) { + bele = &N_BELE_RTP::le_policy; //printf("pe_header_t %d\n", (int) sizeof(pe_header_t)); //printf("pe_section_t %d\n", (int) sizeof(pe_section_t)); COMPILE_TIME_ASSERT(sizeof(pe_header_t) == 248);