From 3d91ab6c314c1a98742fde53f1a848bc6eb8f3ba Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Sun, 13 Aug 2006 15:30:26 +0200 Subject: [PATCH] Introduced bele_policy.h. --- src/bele.h | 175 ++++------------------------- src/bele_policy.h | 273 ++++++++++++++++++++++++++++++++++++++++++++++ src/linker.h | 8 +- src/p_elf.h | 78 ++++++------- 4 files changed, 337 insertions(+), 197 deletions(-) create mode 100644 src/bele_policy.h diff --git a/src/bele.h b/src/bele.h index 068a0cc3..40da8aa9 100644 --- a/src/bele.h +++ b/src/bele.h @@ -477,165 +477,32 @@ int __acc_cdecl_qsort le64_compare_signed(const void *, const void *); /************************************************************************* +// Provide namespaces and classes to abtract endianness policies. // +// CTP - Compile-Time Polymorphism (templates) +// RTP - Run-Time Polymorphism (virtual functions) **************************************************************************/ -namespace NBELE { +// forward declarations +namespace N_BELE_CTP { +class BEPolicy; class LEPolicy; +} +namespace N_BELE_RTP { +class AbstractPolicy; +class BEPolicy; class LEPolicy; +} -struct BEPolicy -{ - enum { isBE = 1, isLE = 0 }; +namespace N_BELE_CTP { +#define BELE_CTP 1 +#include "bele_policy.h" +#undef BELE_CTP +} - typedef BE16 U16; - typedef BE32 U32; - typedef BE64 U64; - - static inline unsigned get16(const void *p) - { return get_be16(p); } - static inline unsigned get24(const void *p) - { return get_be24(p); } - static inline unsigned get32(const void *p) - { return get_be32(p); } - static inline acc_uint64l_t get64(const void *p) - { return get_be64(p); } - - static inline void set16(void *p, unsigned v) - { set_be16(p, v); } - static inline void set24(void *p, unsigned v) - { set_be24(p, v); } - static inline void set32(void *p, unsigned v) - { set_be32(p, v); } - static inline void set64(void *p, acc_uint64l_t v) - { set_be64(p, v); } - - static inline unsigned get16_signed(const void *p) - { return get_be16_signed(p); } - static inline unsigned get24_signed(const void *p) - { return get_be24_signed(p); } - static inline unsigned get32_signed(const void *p) - { return get_be32_signed(p); } - static inline acc_uint64l_t get64_signed(const void *p) - { return get_be64_signed(p); } - - static int __acc_cdecl_qsort u16_compare(const void *a, const void *b) - { return be16_compare(a, b); } - static int __acc_cdecl_qsort u24_compare(const void *a, const void *b) - { return be24_compare(a, b); } - static int __acc_cdecl_qsort u32_compare(const void *a, const void *b) - { return be32_compare(a, b); } - static int __acc_cdecl_qsort u64_compare(const void *a, const void *b) - { return be64_compare(a, b); } - - static int __acc_cdecl_qsort u16_compare_signed(const void *a, const void *b) - { return be16_compare_signed(a, b); } - static int __acc_cdecl_qsort u24_compare_signed(const void *a, const void *b) - { return be24_compare_signed(a, b); } - static int __acc_cdecl_qsort u32_compare_signed(const void *a, const void *b) - { return be32_compare_signed(a, b); } - static int __acc_cdecl_qsort u64_compare_signed(const void *a, const void *b) - { return be64_compare_signed(a, b); } - - static void compileTimeAssertions() { - COMPILE_TIME_ASSERT(sizeof(U16) == 2) - COMPILE_TIME_ASSERT(sizeof(U32) == 4) - COMPILE_TIME_ASSERT(sizeof(U64) == 8) - COMPILE_TIME_ASSERT_ALIGNOF(U16, char) - COMPILE_TIME_ASSERT_ALIGNOF(U32, char) - COMPILE_TIME_ASSERT_ALIGNOF(U64, char) - } -}; - - -struct LEPolicy -{ - enum { isBE = 0, isLE = 1 }; - - typedef LE16 U16; - typedef LE32 U32; - typedef LE64 U64; - - static inline unsigned get16(const void *p) - { return get_le16(p); } - static inline unsigned get24(const void *p) - { return get_le24(p); } - static inline unsigned get32(const void *p) - { return get_le32(p); } - static inline acc_uint64l_t get64(const void *p) - { return get_le64(p); } - - static inline void set16(void *p, unsigned v) - { set_le16(p, v); } - static inline void set24(void *p, unsigned v) - { set_le24(p, v); } - static inline void set32(void *p, unsigned v) - { set_le32(p, v); } - static inline void set64(void *p, acc_uint64l_t v) - { set_le64(p, v); } - - static inline unsigned get16_signed(const void *p) - { return get_le16_signed(p); } - static inline unsigned get24_signed(const void *p) - { return get_le24_signed(p); } - static inline unsigned get32_signed(const void *p) - { return get_le32_signed(p); } - static inline acc_uint64l_t get64_signed(const void *p) - { return get_le64_signed(p); } - - static int __acc_cdecl_qsort u16_compare(const void *a, const void *b) - { return le16_compare(a, b); } - static int __acc_cdecl_qsort u24_compare(const void *a, const void *b) - { return le24_compare(a, b); } - static int __acc_cdecl_qsort u32_compare(const void *a, const void *b) - { return le32_compare(a, b); } - static int __acc_cdecl_qsort u64_compare(const void *a, const void *b) - { return le64_compare(a, b); } - - static int __acc_cdecl_qsort u16_compare_signed(const void *a, const void *b) - { return le16_compare_signed(a, b); } - static int __acc_cdecl_qsort u24_compare_signed(const void *a, const void *b) - { return le24_compare_signed(a, b); } - static int __acc_cdecl_qsort u32_compare_signed(const void *a, const void *b) - { return le32_compare_signed(a, b); } - static int __acc_cdecl_qsort u64_compare_signed(const void *a, const void *b) - { return le64_compare_signed(a, b); } - - static void compileTimeAssertions() { - COMPILE_TIME_ASSERT(sizeof(U16) == 2) - COMPILE_TIME_ASSERT(sizeof(U32) == 4) - COMPILE_TIME_ASSERT(sizeof(U64) == 8) - COMPILE_TIME_ASSERT_ALIGNOF(U16, char) - COMPILE_TIME_ASSERT_ALIGNOF(U32, char) - COMPILE_TIME_ASSERT_ALIGNOF(U64, char) - } -}; - - -#if (ACC_ABI_BIG_ENDIAN) -typedef BEPolicy HostPolicy; -#elif (ACC_ABI_LITTLE_ENDIAN) -typedef LEPolicy HostPolicy; -#else -# error "ACC_ABI_ENDIAN" -#endif - - -struct HostAlignedPolicy -{ - enum { isBE = HostPolicy::isBE, isLE = HostPolicy::isLE }; - - typedef acc_uint16e_t U16; - typedef acc_uint32e_t U32; - typedef acc_uint64l_t U64; - - static void compileTimeAssertions() { - COMPILE_TIME_ASSERT(sizeof(U16) == 2) - COMPILE_TIME_ASSERT(sizeof(U32) == 4) - COMPILE_TIME_ASSERT(sizeof(U64) == 8) - } -}; - - -} // namespace NBELE +namespace N_BELE_RTP { +#define BELE_RTP 1 +#include "bele_policy.h" +#undef BELE_RTP +} #endif /* already included */ diff --git a/src/bele_policy.h b/src/bele_policy.h new file mode 100644 index 00000000..97ee620f --- /dev/null +++ b/src/bele_policy.h @@ -0,0 +1,273 @@ +/* bele_policy.h -- access memory in BigEndian and LittleEndian byte order + + This file is part of the UPX executable compressor. + + Copyright (C) 1996-2006 Markus Franz Xaver Johannes Oberhumer + Copyright (C) 1996-2006 Laszlo Molnar + All Rights Reserved. + + UPX and the UCL library are free software; you can redistribute them + and/or modify them under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. + If not, write to the Free Software Foundation, Inc., + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Markus F.X.J. Oberhumer Laszlo Molnar + + */ + + +#ifndef __UPX_BELE_H +# error "this is an internal include file" +#endif + + +/************************************************************************* +// +**************************************************************************/ + +#if defined(BELE_CTP) + // CTP - Compile-Time Polymorphism (templates) +# define V static inline +# define S static int __acc_cdecl_qsort +# define C +#elif defined(BELE_RTP) + // RTP - Run-Time Polymorphism (virtual functions) +# define V virtual +# define S virtual int +# define C const +#else +# error +#endif + + +#if defined(BELE_RTP) +struct AbstractPolicy +{ + virtual inline ~AbstractPolicy() {} + V bool isBE() C = 0; + V bool isLE() C = 0; + + V unsigned get16(const void *p) C = 0; + V unsigned get24(const void *p) C = 0; + V unsigned get32(const void *p) C = 0; + V acc_uint64l_t get64(const void *p) C = 0; + + V void set16(void *p, unsigned v) C = 0; + V void set24(void *p, unsigned v) C = 0; + V void set32(void *p, unsigned v) C = 0; + V void set64(void *p, acc_uint64l_t v) C = 0; + + V unsigned get16_signed(const void *p) C = 0; + V unsigned get24_signed(const void *p) C = 0; + V unsigned get32_signed(const void *p) C = 0; + V acc_uint64l_t get64_signed(const void *p) C = 0; + + S u16_compare(const void *a, const void *b) C = 0; + S u24_compare(const void *a, const void *b) C = 0; + S u32_compare(const void *a, const void *b) C = 0; + S u64_compare(const void *a, const void *b) C = 0; + + S u16_compare_signed(const void *a, const void *b) C = 0; + S u24_compare_signed(const void *a, const void *b) C = 0; + S u32_compare_signed(const void *a, const void *b) C = 0; + S u64_compare_signed(const void *a, const void *b) C = 0; +}; +#endif + + +struct BEPolicy +#if defined(BELE_RTP) + : public AbstractPolicy +#endif +{ +#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 + + typedef BE16 U16; + typedef BE32 U32; + typedef BE64 U64; + + V unsigned get16(const void *p) C + { return get_be16(p); } + V unsigned get24(const void *p) C + { return get_be24(p); } + V unsigned get32(const void *p) C + { return get_be32(p); } + V acc_uint64l_t get64(const void *p) C + { return get_be64(p); } + + V void set16(void *p, unsigned v) C + { set_be16(p, v); } + V void set24(void *p, unsigned v) C + { set_be24(p, v); } + V void set32(void *p, unsigned v) C + { set_be32(p, v); } + V void set64(void *p, acc_uint64l_t v) C + { set_be64(p, v); } + + V unsigned get16_signed(const void *p) C + { return get_be16_signed(p); } + V unsigned get24_signed(const void *p) C + { return get_be24_signed(p); } + V unsigned get32_signed(const void *p) C + { return get_be32_signed(p); } + V acc_uint64l_t get64_signed(const void *p) C + { return get_be64_signed(p); } + + S u16_compare(const void *a, const void *b) C + { return be16_compare(a, b); } + S u24_compare(const void *a, const void *b) C + { return be24_compare(a, b); } + S u32_compare(const void *a, const void *b) C + { return be32_compare(a, b); } + S u64_compare(const void *a, const void *b) C + { return be64_compare(a, b); } + + S u16_compare_signed(const void *a, const void *b) C + { return be16_compare_signed(a, b); } + S u24_compare_signed(const void *a, const void *b) C + { return be24_compare_signed(a, b); } + S u32_compare_signed(const void *a, const void *b) C + { return be32_compare_signed(a, b); } + S u64_compare_signed(const void *a, const void *b) C + { return be64_compare_signed(a, b); } + + static void compileTimeAssertions() { + COMPILE_TIME_ASSERT(sizeof(U16) == 2) + COMPILE_TIME_ASSERT(sizeof(U32) == 4) + COMPILE_TIME_ASSERT(sizeof(U64) == 8) + COMPILE_TIME_ASSERT_ALIGNOF(U16, char) + COMPILE_TIME_ASSERT_ALIGNOF(U32, char) + COMPILE_TIME_ASSERT_ALIGNOF(U64, char) + } +}; + + +struct LEPolicy +#if defined(BELE_RTP) + : public AbstractPolicy +#endif +{ +#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 + + typedef LE16 U16; + typedef LE32 U32; + typedef LE64 U64; + + V unsigned get16(const void *p) C + { return get_le16(p); } + V unsigned get24(const void *p) C + { return get_le24(p); } + V unsigned get32(const void *p) C + { return get_le32(p); } + V acc_uint64l_t get64(const void *p) C + { return get_le64(p); } + + V void set16(void *p, unsigned v) C + { set_le16(p, v); } + V void set24(void *p, unsigned v) C + { set_le24(p, v); } + V void set32(void *p, unsigned v) C + { set_le32(p, v); } + V void set64(void *p, acc_uint64l_t v) C + { set_le64(p, v); } + + V unsigned get16_signed(const void *p) C + { return get_le16_signed(p); } + V unsigned get24_signed(const void *p) C + { return get_le24_signed(p); } + V unsigned get32_signed(const void *p) C + { return get_le32_signed(p); } + V acc_uint64l_t get64_signed(const void *p) C + { return get_le64_signed(p); } + + S u16_compare(const void *a, const void *b) C + { return le16_compare(a, b); } + S u24_compare(const void *a, const void *b) C + { return le24_compare(a, b); } + S u32_compare(const void *a, const void *b) C + { return le32_compare(a, b); } + S u64_compare(const void *a, const void *b) C + { return le64_compare(a, b); } + + S u16_compare_signed(const void *a, const void *b) C + { return le16_compare_signed(a, b); } + S u24_compare_signed(const void *a, const void *b) C + { return le24_compare_signed(a, b); } + S u32_compare_signed(const void *a, const void *b) C + { return le32_compare_signed(a, b); } + S u64_compare_signed(const void *a, const void *b) C + { return le64_compare_signed(a, b); } + + static void compileTimeAssertions() { + COMPILE_TIME_ASSERT(sizeof(U16) == 2) + COMPILE_TIME_ASSERT(sizeof(U32) == 4) + COMPILE_TIME_ASSERT(sizeof(U64) == 8) + COMPILE_TIME_ASSERT_ALIGNOF(U16, char) + COMPILE_TIME_ASSERT_ALIGNOF(U32, char) + COMPILE_TIME_ASSERT_ALIGNOF(U64, char) + } +}; + + +#if (ACC_ABI_BIG_ENDIAN) +typedef BEPolicy HostPolicy; +#elif (ACC_ABI_LITTLE_ENDIAN) +typedef LEPolicy HostPolicy; +#else +# error "ACC_ABI_ENDIAN" +#endif + + +#if 0 /* UNUSED */ +struct HostAlignedPolicy +{ +#if defined(BELE_CTP) + enum { isBE = HostPolicy::isBE, isLE = HostPolicy::isLE }; +#endif + + typedef acc_uint16e_t U16; + typedef acc_uint32e_t U32; + typedef acc_uint64l_t U64; + + static void compileTimeAssertions() { + COMPILE_TIME_ASSERT(sizeof(U16) == 2) + COMPILE_TIME_ASSERT(sizeof(U32) == 4) + COMPILE_TIME_ASSERT(sizeof(U64) == 8) + } +}; +#endif + + +#undef V +#undef S +#undef C + + +/* +vi:ts=4:et:nowrap +*/ + diff --git a/src/linker.h b/src/linker.h index b2a876db..3a94599d 100644 --- a/src/linker.h +++ b/src/linker.h @@ -112,8 +112,8 @@ struct TDefaultLinker : public DefaultLinker virtual void set32(void *b, unsigned v) const { T::set32(b, v); } }; -typedef TDefaultLinker DefaultBELinker; -typedef TDefaultLinker DefaultLELinker; +typedef TDefaultLinker DefaultBELinker; +typedef TDefaultLinker DefaultLELinker; /************************************************************************* @@ -147,8 +147,8 @@ struct TSimpleLinker : public SimpleLinker virtual void set32(void *b, unsigned v) const { T::set32(b, v); } }; -typedef TSimpleLinker SimpleBELinker; -typedef TSimpleLinker SimpleLELinker; +typedef TSimpleLinker SimpleBELinker; +typedef TSimpleLinker SimpleLELinker; /************************************************************************* diff --git a/src/p_elf.h b/src/p_elf.h index 8933caf9..5c0c92f1 100644 --- a/src/p_elf.h +++ b/src/p_elf.h @@ -34,7 +34,7 @@ // Some ELF type definitinons **************************************************************************/ -namespace TT_Elf { +namespace N_Elf { // The ELF file header. This appears at the start of every ELF file. template @@ -116,10 +116,10 @@ struct Dyn } __attribute_packed; -} // namespace TT_Elf +} // namespace N_Elf -namespace TT_Elf32 { +namespace N_Elf32 { // Program segment header. template @@ -243,10 +243,10 @@ struct Sym } __attribute_packed; -} // namespace TT_Elf32 +} // namespace N_Elf32 -namespace TT_Elf64 { +namespace N_Elf64 { // Program segment header. template @@ -314,56 +314,56 @@ struct Shdr } __attribute_packed; -} // namespace TT_Elf64 +} // namespace N_Elf64 /************************************************************************* // now for the actual types **************************************************************************/ -#define P NBELE::HostPolicy -typedef TT_Elf ::Ehdr Elf32_Ehdr; -typedef TT_Elf32::Phdr Elf32_Phdr; -typedef TT_Elf32::Shdr Elf32_Shdr; -typedef TT_Elf ::Dyn Elf32_Dyn; -typedef TT_Elf32::Sym Elf32_Sym; +#define P N_BELE_CTP::HostPolicy +typedef N_Elf ::Ehdr Elf32_Ehdr; +typedef N_Elf32::Phdr Elf32_Phdr; +typedef N_Elf32::Shdr Elf32_Shdr; +typedef N_Elf ::Dyn Elf32_Dyn; +typedef N_Elf32::Sym Elf32_Sym; #undef P -#define P NBELE::BEPolicy -typedef TT_Elf ::Ehdr Elf_BE32_Ehdr; -typedef TT_Elf32::Phdr Elf_BE32_Phdr; -typedef TT_Elf32::Shdr Elf_BE32_Shdr; -typedef TT_Elf ::Dyn Elf_BE32_Dyn; -typedef TT_Elf32::Sym Elf_BE32_Sym; +#define P N_BELE_CTP::BEPolicy +typedef N_Elf ::Ehdr Elf_BE32_Ehdr; +typedef N_Elf32::Phdr Elf_BE32_Phdr; +typedef N_Elf32::Shdr Elf_BE32_Shdr; +typedef N_Elf ::Dyn Elf_BE32_Dyn; +typedef N_Elf32::Sym Elf_BE32_Sym; #undef P -#define P NBELE::LEPolicy -typedef TT_Elf ::Ehdr Elf_LE32_Ehdr; -typedef TT_Elf32::Phdr Elf_LE32_Phdr; -typedef TT_Elf32::Shdr Elf_LE32_Shdr; -typedef TT_Elf ::Dyn Elf_LE32_Dyn; -typedef TT_Elf32::Sym Elf_LE32_Sym; +#define P N_BELE_CTP::LEPolicy +typedef N_Elf ::Ehdr Elf_LE32_Ehdr; +typedef N_Elf32::Phdr Elf_LE32_Phdr; +typedef N_Elf32::Shdr Elf_LE32_Shdr; +typedef N_Elf ::Dyn Elf_LE32_Dyn; +typedef N_Elf32::Sym Elf_LE32_Sym; #undef P -#define P NBELE::HostPolicy -typedef TT_Elf ::Ehdr Elf64_Ehdr; -typedef TT_Elf64::Phdr Elf64_Phdr; -typedef TT_Elf64::Shdr Elf64_Shdr; -typedef TT_Elf ::Dyn Elf64_Dyn; +#define P N_BELE_CTP::HostPolicy +typedef N_Elf ::Ehdr Elf64_Ehdr; +typedef N_Elf64::Phdr Elf64_Phdr; +typedef N_Elf64::Shdr Elf64_Shdr; +typedef N_Elf ::Dyn Elf64_Dyn; #undef P -#define P NBELE::BEPolicy -typedef TT_Elf ::Ehdr Elf_BE64_Ehdr; -typedef TT_Elf64::Phdr Elf_BE64_Phdr; -typedef TT_Elf64::Shdr Elf_BE64_Shdr; -typedef TT_Elf ::Dyn Elf_BE64_Dyn; +#define P N_BELE_CTP::BEPolicy +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; #undef P -#define P NBELE::LEPolicy -typedef TT_Elf ::Ehdr Elf_LE64_Ehdr; -typedef TT_Elf64::Phdr Elf_LE64_Phdr; -typedef TT_Elf64::Shdr Elf_LE64_Shdr; -typedef TT_Elf ::Dyn Elf_LE64_Dyn; +#define P N_BELE_CTP::LEPolicy +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; #undef P