Introduce ElfClass and PackVmlinuxBase for refactoring elf based packers.
This commit is contained in:
+15
-4
@@ -538,26 +538,37 @@ int __acc_cdecl_qsort le64_compare_signed(const void *, const void *);
|
|||||||
// forward declarations
|
// forward declarations
|
||||||
namespace N_BELE_CTP {
|
namespace N_BELE_CTP {
|
||||||
class BEPolicy; class LEPolicy;
|
class BEPolicy; class LEPolicy;
|
||||||
|
extern const BEPolicy be_policy;
|
||||||
|
extern const LEPolicy le_policy;
|
||||||
}
|
}
|
||||||
namespace N_BELE_RTP {
|
namespace N_BELE_RTP {
|
||||||
class AbstractPolicy;
|
class AbstractPolicy;
|
||||||
class BEPolicy; class LEPolicy;
|
class BEPolicy; class LEPolicy;
|
||||||
|
extern const BEPolicy be_policy;
|
||||||
|
extern const LEPolicy le_policy;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace N_BELE_CTP {
|
namespace N_BELE_CTP {
|
||||||
#define BELE_CTP 1
|
#define BELE_CTP 1
|
||||||
#include "bele_policy.h"
|
#include "bele_policy.h"
|
||||||
#undef BELE_CTP
|
#undef BELE_CTP
|
||||||
extern const BEPolicy be_policy;
|
|
||||||
extern const LEPolicy le_policy;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace N_BELE_RTP {
|
namespace N_BELE_RTP {
|
||||||
#define BELE_RTP 1
|
#define BELE_RTP 1
|
||||||
#include "bele_policy.h"
|
#include "bele_policy.h"
|
||||||
#undef BELE_RTP
|
#undef BELE_RTP
|
||||||
extern const BEPolicy be_policy;
|
}
|
||||||
extern const LEPolicy le_policy;
|
|
||||||
|
namespace N_BELE_CTP {
|
||||||
|
|
||||||
|
template <class TCTP>
|
||||||
|
static const N_BELE_RTP::AbstractPolicy* getRTP();
|
||||||
|
template <>
|
||||||
|
static const N_BELE_RTP::AbstractPolicy* getRTP<BEPolicy>() { return &N_BELE_RTP::be_policy; }
|
||||||
|
template <>
|
||||||
|
static const N_BELE_RTP::AbstractPolicy* getRTP<LEPolicy>() { return &N_BELE_RTP::le_policy; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+4
-6
@@ -97,12 +97,11 @@ struct BEPolicy
|
|||||||
BEPolicy() {}
|
BEPolicy() {}
|
||||||
#if defined(BELE_CTP)
|
#if defined(BELE_CTP)
|
||||||
typedef N_BELE_RTP::BEPolicy RTP_Policy;
|
typedef N_BELE_RTP::BEPolicy RTP_Policy;
|
||||||
enum { isBE = 1, isLE = 0 };
|
|
||||||
#elif defined(BELE_RTP)
|
#elif defined(BELE_RTP)
|
||||||
typedef N_BELE_CTP::BEPolicy CTP_Policy;
|
typedef N_BELE_CTP::BEPolicy CTP_Policy;
|
||||||
V bool isBE() C { return CTP_Policy::isBE; }
|
|
||||||
V bool isLE() C { return CTP_Policy::isLE; }
|
|
||||||
#endif
|
#endif
|
||||||
|
V bool isBE() C { return true; }
|
||||||
|
V bool isLE() C { return false; }
|
||||||
|
|
||||||
typedef BE16 U16;
|
typedef BE16 U16;
|
||||||
typedef BE32 U32;
|
typedef BE32 U32;
|
||||||
@@ -175,12 +174,11 @@ struct LEPolicy
|
|||||||
LEPolicy() {}
|
LEPolicy() {}
|
||||||
#if defined(BELE_CTP)
|
#if defined(BELE_CTP)
|
||||||
typedef N_BELE_RTP::LEPolicy RTP_Policy;
|
typedef N_BELE_RTP::LEPolicy RTP_Policy;
|
||||||
enum { isBE = 0, isLE = 1 };
|
|
||||||
#elif defined(BELE_RTP)
|
#elif defined(BELE_RTP)
|
||||||
typedef N_BELE_CTP::LEPolicy CTP_Policy;
|
typedef N_BELE_CTP::LEPolicy CTP_Policy;
|
||||||
V bool isBE() C { return CTP_Policy::isBE; }
|
|
||||||
V bool isLE() C { return CTP_Policy::isLE; }
|
|
||||||
#endif
|
#endif
|
||||||
|
V bool isBE() C { return false; }
|
||||||
|
V bool isLE() C { return true; }
|
||||||
|
|
||||||
typedef LE16 U16;
|
typedef LE16 U16;
|
||||||
typedef LE32 U32;
|
typedef LE32 U32;
|
||||||
|
|||||||
+28
@@ -360,6 +360,7 @@ typedef N_Elf ::Ehdr<P::U32,P::U64,P::U64,P::U16> Elf64_Ehdr;
|
|||||||
typedef N_Elf64::Phdr<P::U32,P::U64,P::U64,P::U64> Elf64_Phdr;
|
typedef N_Elf64::Phdr<P::U32,P::U64,P::U64,P::U64> Elf64_Phdr;
|
||||||
typedef N_Elf64::Shdr<P::U32,P::U64,P::U64,P::U64> Elf64_Shdr;
|
typedef N_Elf64::Shdr<P::U32,P::U64,P::U64,P::U64> Elf64_Shdr;
|
||||||
typedef N_Elf ::Dyn <P::U64,P::U64> Elf64_Dyn;
|
typedef N_Elf ::Dyn <P::U64,P::U64> Elf64_Dyn;
|
||||||
|
typedef void Elf64_Sym; // FIXME
|
||||||
#undef P
|
#undef P
|
||||||
|
|
||||||
#define P N_BELE_CTP::BEPolicy
|
#define P N_BELE_CTP::BEPolicy
|
||||||
@@ -367,6 +368,7 @@ typedef N_Elf ::Ehdr<P::U32,P::U64,P::U64,P::U16> Elf_BE64_Ehdr;
|
|||||||
typedef N_Elf64::Phdr<P::U32,P::U64,P::U64,P::U64> Elf_BE64_Phdr;
|
typedef N_Elf64::Phdr<P::U32,P::U64,P::U64,P::U64> Elf_BE64_Phdr;
|
||||||
typedef N_Elf64::Shdr<P::U32,P::U64,P::U64,P::U64> Elf_BE64_Shdr;
|
typedef N_Elf64::Shdr<P::U32,P::U64,P::U64,P::U64> Elf_BE64_Shdr;
|
||||||
typedef N_Elf ::Dyn <P::U64,P::U64> Elf_BE64_Dyn;
|
typedef N_Elf ::Dyn <P::U64,P::U64> Elf_BE64_Dyn;
|
||||||
|
typedef void Elf_BE64_Sym; // FIXME
|
||||||
#undef P
|
#undef P
|
||||||
|
|
||||||
#define P N_BELE_CTP::LEPolicy
|
#define P N_BELE_CTP::LEPolicy
|
||||||
@@ -374,6 +376,7 @@ typedef N_Elf ::Ehdr<P::U32,P::U64,P::U64,P::U16> Elf_LE64_Ehdr;
|
|||||||
typedef N_Elf64::Phdr<P::U32,P::U64,P::U64,P::U64> Elf_LE64_Phdr;
|
typedef N_Elf64::Phdr<P::U32,P::U64,P::U64,P::U64> Elf_LE64_Phdr;
|
||||||
typedef N_Elf64::Shdr<P::U32,P::U64,P::U64,P::U64> Elf_LE64_Shdr;
|
typedef N_Elf64::Shdr<P::U32,P::U64,P::U64,P::U64> Elf_LE64_Shdr;
|
||||||
typedef N_Elf ::Dyn <P::U64,P::U64> Elf_LE64_Dyn;
|
typedef N_Elf ::Dyn <P::U64,P::U64> Elf_LE64_Dyn;
|
||||||
|
typedef void Elf_LE64_Sym; // FIXME
|
||||||
#undef P
|
#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)
|
ACC_COMPILE_TIME_ASSERT_HEADER(sizeof(Elf_LE64_Dyn) == 16)
|
||||||
|
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
// aggregate all types in an ElfClass
|
||||||
|
**************************************************************************/
|
||||||
|
|
||||||
|
template <class TP, class TEhdr, class TPhdr, class TShdr, class TDyn, class TSym>
|
||||||
|
struct ElfClass
|
||||||
|
{
|
||||||
|
typedef TP BeLePolicy;
|
||||||
|
typedef TEhdr Ehdr;
|
||||||
|
typedef TPhdr Phdr;
|
||||||
|
typedef TShdr Shdr;
|
||||||
|
typedef TDyn Dyn;
|
||||||
|
typedef TSym Sym;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef ElfClass<N_BELE_CTP::LEPolicy, Elf_BE32_Ehdr, Elf_BE32_Phdr, Elf_BE32_Shdr, Elf_BE32_Dyn,Elf_BE32_Sym>
|
||||||
|
ElfClass_BE32;
|
||||||
|
typedef ElfClass<N_BELE_CTP::LEPolicy, Elf_BE64_Ehdr, Elf_BE64_Phdr, Elf_BE64_Shdr, Elf_BE64_Dyn,Elf_BE64_Sym>
|
||||||
|
ElfClass_BE64;
|
||||||
|
typedef ElfClass<N_BELE_CTP::LEPolicy, Elf_LE32_Ehdr, Elf_LE32_Phdr, Elf_LE32_Shdr, Elf_LE32_Dyn,Elf_LE32_Sym>
|
||||||
|
ElfClass_LE32;
|
||||||
|
typedef ElfClass<N_BELE_CTP::LEPolicy, Elf_LE64_Ehdr, Elf_LE64_Phdr, Elf_LE64_Shdr, Elf_LE64_Dyn,Elf_LE64_Sym>
|
||||||
|
ElfClass_LE64;
|
||||||
|
|
||||||
|
|
||||||
#endif /* already included */
|
#endif /* already included */
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-18
@@ -45,16 +45,11 @@ static const
|
|||||||
static const
|
static const
|
||||||
#include "stub/arm-linux.kernel.vmlinux.h"
|
#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()
|
PackVmlinuxI386::~PackVmlinuxI386()
|
||||||
{
|
{
|
||||||
delete [] shstrtab;
|
delete [] shstrtab;
|
||||||
@@ -115,12 +110,6 @@ PackVmlinuxI386::getElfSections()
|
|||||||
return shstrsec;
|
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()
|
PackVmlinuxARM::~PackVmlinuxARM()
|
||||||
{
|
{
|
||||||
delete [] shstrtab;
|
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()
|
PackVmlinuxAMD64::~PackVmlinuxAMD64()
|
||||||
{
|
{
|
||||||
delete [] shstrtab;
|
delete [] shstrtab;
|
||||||
|
|||||||
+40
-44
@@ -31,15 +31,45 @@
|
|||||||
|
|
||||||
#include "p_elf.h"
|
#include "p_elf.h"
|
||||||
|
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
// vmlinx/i386 (bare binary Linux kernel image)
|
// vmlinx/i386 (bare binary Linux kernel image)
|
||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
class PackVmlinuxI386 : public Packer
|
template <class TElfClass>
|
||||||
|
class PackVmlinuxBase : public Packer
|
||||||
{
|
{
|
||||||
typedef Packer super;
|
typedef Packer super;
|
||||||
|
protected:
|
||||||
|
typedef typename TElfClass::Ehdr Ehdr;
|
||||||
|
typedef typename TElfClass::Shdr Shdr;
|
||||||
|
typedef typename TElfClass::Phdr Phdr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PackVmlinuxI386(InputFile *f);
|
PackVmlinuxBase(InputFile *f) :
|
||||||
|
super(f), n_ptload(0), phdri(NULL), shdri(NULL), shstrtab(NULL)
|
||||||
|
{
|
||||||
|
bele = N_BELE_CTP::getRTP<typename TElfClass::BeLePolicy>();
|
||||||
|
}
|
||||||
|
|
||||||
|
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<ElfClass_LE32>
|
||||||
|
{
|
||||||
|
typedef PackVmlinuxBase<ElfClass_LE32> super;
|
||||||
|
public:
|
||||||
|
PackVmlinuxI386(InputFile *f) : super(f) { }
|
||||||
virtual ~PackVmlinuxI386();
|
virtual ~PackVmlinuxI386();
|
||||||
virtual int getVersion() const { return 13; }
|
virtual int getVersion() const { return 13; }
|
||||||
virtual int getFormat() const { return UPX_F_VMLINUX_i386; }
|
virtual int getFormat() const { return UPX_F_VMLINUX_i386; }
|
||||||
@@ -59,25 +89,14 @@ protected:
|
|||||||
virtual Elf_LE32_Shdr const *getElfSections();
|
virtual Elf_LE32_Shdr const *getElfSections();
|
||||||
virtual void buildLoader(const Filter *ft);
|
virtual void buildLoader(const Filter *ft);
|
||||||
virtual Linker* newLinker() const;
|
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<ElfClass_LE32>
|
||||||
{
|
{
|
||||||
typedef Packer super;
|
typedef PackVmlinuxBase<ElfClass_LE32> super;
|
||||||
public:
|
public:
|
||||||
PackVmlinuxARM(InputFile *f);
|
PackVmlinuxARM(InputFile *f) : super(f) { }
|
||||||
virtual ~PackVmlinuxARM();
|
virtual ~PackVmlinuxARM();
|
||||||
virtual int getVersion() const { return 13; }
|
virtual int getVersion() const { return 13; }
|
||||||
virtual int getFormat() const { return UPX_F_VMLINUX_ARM; }
|
virtual int getFormat() const { return UPX_F_VMLINUX_ARM; }
|
||||||
@@ -97,25 +116,14 @@ protected:
|
|||||||
virtual Elf_LE32_Shdr const *getElfSections();
|
virtual Elf_LE32_Shdr const *getElfSections();
|
||||||
virtual void buildLoader(const Filter *ft);
|
virtual void buildLoader(const Filter *ft);
|
||||||
virtual Linker* newLinker() const;
|
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<ElfClass_LE64>
|
||||||
{
|
{
|
||||||
typedef Packer super;
|
typedef PackVmlinuxBase<ElfClass_LE64> super;
|
||||||
public:
|
public:
|
||||||
PackVmlinuxAMD64(InputFile *f);
|
PackVmlinuxAMD64(InputFile *f) : super(f) { }
|
||||||
virtual ~PackVmlinuxAMD64();
|
virtual ~PackVmlinuxAMD64();
|
||||||
virtual int getVersion() const { return 13; }
|
virtual int getVersion() const { return 13; }
|
||||||
virtual int getFormat() const { return UPX_F_VMLINUX_AMD64; }
|
virtual int getFormat() const { return UPX_F_VMLINUX_AMD64; }
|
||||||
@@ -135,18 +143,6 @@ protected:
|
|||||||
virtual Elf_LE64_Shdr const *getElfSections();
|
virtual Elf_LE64_Shdr const *getElfSections();
|
||||||
virtual void buildLoader(const Filter *ft);
|
virtual void buildLoader(const Filter *ft);
|
||||||
virtual Linker* newLinker() const;
|
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
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user