From 728e6d851ebbebf86febac0b6f6d24af4e3f75e6 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Tue, 19 Dec 2006 00:37:35 +0100 Subject: [PATCH] Introduce ElfClass and PackVmlinuxBase for refactoring elf based packers. --- src/bele.h | 19 ++++++++--- src/bele_policy.h | 10 +++--- src/p_elf.h | 28 ++++++++++++++++ src/p_vmlinx.cpp | 19 +---------- src/p_vmlinx.h | 84 ++++++++++++++++++++++------------------------- 5 files changed, 88 insertions(+), 72 deletions(-) diff --git a/src/bele.h b/src/bele.h index 1d8958f1..b78b5b8a 100644 --- a/src/bele.h +++ b/src/bele.h @@ -538,26 +538,37 @@ int __acc_cdecl_qsort le64_compare_signed(const void *, const void *); // forward declarations namespace N_BELE_CTP { class BEPolicy; class LEPolicy; +extern const BEPolicy be_policy; +extern const LEPolicy le_policy; } namespace N_BELE_RTP { class AbstractPolicy; class BEPolicy; class LEPolicy; +extern const BEPolicy be_policy; +extern const LEPolicy le_policy; } namespace N_BELE_CTP { #define BELE_CTP 1 #include "bele_policy.h" #undef BELE_CTP -extern const BEPolicy be_policy; -extern const LEPolicy le_policy; } namespace N_BELE_RTP { #define BELE_RTP 1 #include "bele_policy.h" #undef BELE_RTP -extern const BEPolicy be_policy; -extern const LEPolicy le_policy; +} + +namespace N_BELE_CTP { + +template +static const N_BELE_RTP::AbstractPolicy* getRTP(); +template <> +static const N_BELE_RTP::AbstractPolicy* getRTP() { return &N_BELE_RTP::be_policy; } +template <> +static const N_BELE_RTP::AbstractPolicy* getRTP() { return &N_BELE_RTP::le_policy; } + } diff --git a/src/bele_policy.h b/src/bele_policy.h index fe8205e6..771d554a 100644 --- a/src/bele_policy.h +++ b/src/bele_policy.h @@ -97,12 +97,11 @@ struct BEPolicy BEPolicy() {} #if defined(BELE_CTP) typedef N_BELE_RTP::BEPolicy RTP_Policy; - enum { isBE = 1, isLE = 0 }; #elif defined(BELE_RTP) typedef N_BELE_CTP::BEPolicy CTP_Policy; - V bool isBE() C { return CTP_Policy::isBE; } - V bool isLE() C { return CTP_Policy::isLE; } #endif + V bool isBE() C { return true; } + V bool isLE() C { return false; } typedef BE16 U16; typedef BE32 U32; @@ -175,12 +174,11 @@ struct LEPolicy LEPolicy() {} #if defined(BELE_CTP) typedef N_BELE_RTP::LEPolicy RTP_Policy; - enum { isBE = 0, isLE = 1 }; #elif defined(BELE_RTP) typedef N_BELE_CTP::LEPolicy CTP_Policy; - V bool isBE() C { return CTP_Policy::isBE; } - V bool isLE() C { return CTP_Policy::isLE; } #endif + V bool isBE() C { return false; } + V bool isLE() C { return true; } typedef LE16 U16; typedef LE32 U32; diff --git a/src/p_elf.h b/src/p_elf.h index 0a901a46..dd09281d 100644 --- a/src/p_elf.h +++ b/src/p_elf.h @@ -360,6 +360,7 @@ typedef N_Elf ::Ehdr Elf64_Ehdr; typedef N_Elf64::Phdr Elf64_Phdr; typedef N_Elf64::Shdr Elf64_Shdr; typedef N_Elf ::Dyn Elf64_Dyn; +typedef void Elf64_Sym; // FIXME #undef P #define P N_BELE_CTP::BEPolicy @@ -367,6 +368,7 @@ typedef N_Elf ::Ehdr Elf_BE64_Ehdr; typedef N_Elf64::Phdr Elf_BE64_Phdr; typedef N_Elf64::Shdr Elf_BE64_Shdr; typedef N_Elf ::Dyn Elf_BE64_Dyn; +typedef void Elf_BE64_Sym; // FIXME #undef P #define P N_BELE_CTP::LEPolicy @@ -374,6 +376,7 @@ typedef N_Elf ::Ehdr Elf_LE64_Ehdr; typedef N_Elf64::Phdr Elf_LE64_Phdr; typedef N_Elf64::Shdr Elf_LE64_Shdr; typedef N_Elf ::Dyn Elf_LE64_Dyn; +typedef void Elf_LE64_Sym; // FIXME #undef P @@ -411,6 +414,31 @@ ACC_COMPILE_TIME_ASSERT_HEADER(sizeof(Elf_LE64_Shdr) == 64) ACC_COMPILE_TIME_ASSERT_HEADER(sizeof(Elf_LE64_Dyn) == 16) +/************************************************************************* +// aggregate all types in an ElfClass +**************************************************************************/ + +template +struct ElfClass +{ + typedef TP BeLePolicy; + typedef TEhdr Ehdr; + typedef TPhdr Phdr; + typedef TShdr Shdr; + typedef TDyn Dyn; + typedef TSym Sym; +}; + +typedef ElfClass + ElfClass_BE32; +typedef ElfClass + ElfClass_BE64; +typedef ElfClass + ElfClass_LE32; +typedef ElfClass + ElfClass_LE64; + + #endif /* already included */ diff --git a/src/p_vmlinx.cpp b/src/p_vmlinx.cpp index cf030c1f..3c1d1380 100644 --- a/src/p_vmlinx.cpp +++ b/src/p_vmlinx.cpp @@ -45,16 +45,11 @@ static const static const #include "stub/arm-linux.kernel.vmlinux.h" + /************************************************************************* // **************************************************************************/ -PackVmlinuxI386::PackVmlinuxI386(InputFile *f) : - super(f), n_ptload(0), phdri(NULL), shdri(NULL), shstrtab(NULL) -{ - bele = &N_BELE_RTP::le_policy; -} - PackVmlinuxI386::~PackVmlinuxI386() { delete [] shstrtab; @@ -115,12 +110,6 @@ PackVmlinuxI386::getElfSections() return shstrsec; } -PackVmlinuxARM::PackVmlinuxARM(InputFile *f) : - super(f), n_ptload(0), phdri(NULL), shdri(NULL), shstrtab(NULL) -{ - bele = &N_BELE_RTP::le_policy; -} - PackVmlinuxARM::~PackVmlinuxARM() { delete [] shstrtab; @@ -1254,12 +1243,6 @@ void PackVmlinuxARM::unpack(OutputFile *fo) // **************************************************************************/ -PackVmlinuxAMD64::PackVmlinuxAMD64(InputFile *f) : - super(f), n_ptload(0), phdri(NULL), shdri(NULL), shstrtab(NULL) -{ - bele = &N_BELE_RTP::le_policy; -} - PackVmlinuxAMD64::~PackVmlinuxAMD64() { delete [] shstrtab; diff --git a/src/p_vmlinx.h b/src/p_vmlinx.h index aeca11bf..24165873 100644 --- a/src/p_vmlinx.h +++ b/src/p_vmlinx.h @@ -31,15 +31,45 @@ #include "p_elf.h" + /************************************************************************* // vmlinx/i386 (bare binary Linux kernel image) **************************************************************************/ -class PackVmlinuxI386 : public Packer +template +class PackVmlinuxBase : public Packer { typedef Packer super; +protected: + typedef typename TElfClass::Ehdr Ehdr; + typedef typename TElfClass::Shdr Shdr; + typedef typename TElfClass::Phdr Phdr; + public: - PackVmlinuxI386(InputFile *f); + PackVmlinuxBase(InputFile *f) : + super(f), n_ptload(0), phdri(NULL), shdri(NULL), shstrtab(NULL) + { + bele = N_BELE_CTP::getRTP(); + } + +protected: + int n_ptload; + unsigned sz_ptload; + Phdr *phdri; // from input file + Shdr *shdri; // from input file + char *shstrtab; // from input file + Shdr *p_text; + Shdr *p_note0; + Shdr *p_note1; + Ehdr ehdri; // from input file +}; + + +class PackVmlinuxI386 : public PackVmlinuxBase +{ + typedef PackVmlinuxBase super; +public: + PackVmlinuxI386(InputFile *f) : super(f) { } virtual ~PackVmlinuxI386(); virtual int getVersion() const { return 13; } virtual int getFormat() const { return UPX_F_VMLINUX_i386; } @@ -59,25 +89,14 @@ protected: virtual Elf_LE32_Shdr const *getElfSections(); virtual void buildLoader(const Filter *ft); virtual Linker* newLinker() const; -// virtual const upx_byte *getLoader() const; -// virtual int getLoaderSize() const; - - int n_ptload; - unsigned sz_ptload; - Elf_LE32_Phdr *phdri; // from input file - Elf_LE32_Shdr *shdri; // from input file - char *shstrtab; // from input file - Elf_LE32_Shdr *p_text; - Elf_LE32_Shdr *p_note0; - Elf_LE32_Shdr *p_note1; - Elf_LE32_Ehdr ehdri; // from input file }; -class PackVmlinuxARM : public Packer + +class PackVmlinuxARM : public PackVmlinuxBase { - typedef Packer super; + typedef PackVmlinuxBase super; public: - PackVmlinuxARM(InputFile *f); + PackVmlinuxARM(InputFile *f) : super(f) { } virtual ~PackVmlinuxARM(); virtual int getVersion() const { return 13; } virtual int getFormat() const { return UPX_F_VMLINUX_ARM; } @@ -97,25 +116,14 @@ protected: virtual Elf_LE32_Shdr const *getElfSections(); virtual void buildLoader(const Filter *ft); virtual Linker* newLinker() const; -// virtual const upx_byte *getLoader() const; -// virtual int getLoaderSize() const; - - int n_ptload; - unsigned sz_ptload; - Elf_LE32_Phdr *phdri; // from input file - Elf_LE32_Shdr *shdri; // from input file - char *shstrtab; // from input file - Elf_LE32_Shdr *p_text; - Elf_LE32_Shdr *p_note0; - Elf_LE32_Shdr *p_note1; - Elf_LE32_Ehdr ehdri; // from input file }; -class PackVmlinuxAMD64 : public Packer + +class PackVmlinuxAMD64 : public PackVmlinuxBase { - typedef Packer super; + typedef PackVmlinuxBase super; public: - PackVmlinuxAMD64(InputFile *f); + PackVmlinuxAMD64(InputFile *f) : super(f) { } virtual ~PackVmlinuxAMD64(); virtual int getVersion() const { return 13; } virtual int getFormat() const { return UPX_F_VMLINUX_AMD64; } @@ -135,18 +143,6 @@ protected: virtual Elf_LE64_Shdr const *getElfSections(); virtual void buildLoader(const Filter *ft); virtual Linker* newLinker() const; -// virtual const upx_byte *getLoader() const; -// virtual int getLoaderSize() const; - - int n_ptload; - unsigned sz_ptload; - Elf_LE64_Phdr *phdri; // from input file - Elf_LE64_Shdr *shdri; // from input file - char *shstrtab; // from input file - Elf_LE64_Shdr *p_text; - Elf_LE64_Shdr *p_note0; - Elf_LE64_Shdr *p_note1; - Elf_LE64_Ehdr ehdri; // from input file };