1
0
mirror of https://github.com/upx/upx synced 2025-09-28 19:06:07 +08:00

Introduce target endianness accessors get_teXX() and set_teXX() in packer.h to

replace all [gs]et_nativeXX() member functions.
This commit is contained in:
Markus F.X.J. Oberhumer 2008-01-09 13:57:47 +01:00
parent 9060ac7897
commit 7f9c47176a
12 changed files with 328 additions and 363 deletions

View File

@ -591,7 +591,7 @@ int __acc_cdecl_qsort le64_compare_signed(const void *, const void *);
/*************************************************************************
// Provide namespaces and classes to abtract endianness policies.
// Provide namespaces and classes to abstract endianness policies.
//
// CTP - Compile-Time Polymorphism (templates)
// RTP - Run-Time Polymorphism (virtual functions)

View File

@ -38,7 +38,7 @@ class ElfLinker : private noncopyable
{
friend class Packer;
public:
const N_BELE_RTP::AbstractPolicy *bele;
const N_BELE_RTP::AbstractPolicy *bele; // target endianness
protected:
struct Section;
struct Symbol;
@ -107,6 +107,14 @@ protected:
virtual void relocate();
virtual void relocate1(const Relocation *, upx_byte *location,
unsigned value, const char *type);
// target endianness abstraction
unsigned get_te16(const void *p) const { return bele->get16(p); }
unsigned get_te32(const void *p) const { return bele->get32(p); }
acc_uint64l_t get_te64(const void *p) const { return bele->get64(p); }
void set_te16(void *p, unsigned v) const { bele->set16(p, v); }
void set_te32(void *p, unsigned v) const { bele->set32(p, v); }
void set_te64(void *p, acc_uint64l_t v) const { bele->set64(p, v); }
};

File diff suppressed because it is too large Load Diff

View File

@ -265,13 +265,6 @@ class PackLinuxElf32Be : public PackLinuxElf32
typedef PackLinuxElf32 super;
protected:
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); }
virtual unsigned get_native16(const void *b) const { return get_be16(b); }
virtual void set_native64(void *b, acc_uint64l_t v) const { set_be64(b, v); }
virtual void set_native32(void *b, unsigned v) const { set_be32(b, v); }
virtual void set_native16(void *b, unsigned v) const { set_be16(b, v); }
};
class PackLinuxElf32Le : public PackLinuxElf32
@ -279,13 +272,6 @@ class PackLinuxElf32Le : public PackLinuxElf32
typedef PackLinuxElf32 super;
protected:
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); }
virtual unsigned get_native16(const void *b) const { return get_le16(b); }
virtual void set_native64(void *b, acc_uint64l_t v) const { set_le64(b, v); }
virtual void set_native32(void *b, unsigned v) const { set_le32(b, v); }
virtual void set_native16(void *b, unsigned v) const { set_le16(b, v); }
};
class PackLinuxElf64Le : public PackLinuxElf64
@ -293,15 +279,9 @@ class PackLinuxElf64Le : public PackLinuxElf64
typedef PackLinuxElf64 super;
protected:
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); }
virtual unsigned get_native16(const void *b) const { return get_le16(b); }
virtual void set_native64(void *b, acc_uint64l_t v) const { set_le64(b, v); }
virtual void set_native32(void *b, unsigned v) const { set_le32(b, v); }
virtual void set_native16(void *b, unsigned v) const { set_le16(b, v); }
};
/*************************************************************************
// linux/elf64amd
**************************************************************************/
@ -325,6 +305,7 @@ protected:
virtual void defineSymbols(Filter const *);
};
/*************************************************************************
// linux/elf32ppc
**************************************************************************/
@ -345,6 +326,7 @@ protected:
virtual Linker* newLinker() const;
};
/*************************************************************************
// linux/elf386
**************************************************************************/
@ -421,6 +403,7 @@ protected:
);
};
/*************************************************************************
// linux/elfarm
**************************************************************************/
@ -503,6 +486,7 @@ protected:
virtual void defineSymbols(Filter const *);
};
#endif /*} already included */

View File

@ -526,22 +526,22 @@ bool PackLinuxI386::canPack()
ei_osabi = osabi0; // Proudly declares its osabi in Ehdr.
break;
default:
unsigned const e_phnum = get_native16(&ehdr.e_phnum);
unsigned const e_phnum = get_te16(&ehdr.e_phnum);
if (e_phnum<=(512/sizeof(Elf32_Phdr))) {
char buf2[512];
fi->seek(get_native32(&ehdr.e_phoff), SEEK_SET);
fi->seek(get_te32(&ehdr.e_phoff), SEEK_SET);
fi->readx(buf2, sizeof(buf2));
fi->seek(0, SEEK_SET);
Elf32_Phdr const *phdr = (Elf32_Phdr const *)buf2;
for (unsigned j=0; j < e_phnum; ++phdr, ++j) {
if (phdr->PT_NOTE == get_native32(&phdr->p_type)) {
unsigned const offset = get_native32(&phdr->p_offset);
if (phdr->PT_NOTE == get_te32(&phdr->p_type)) {
unsigned const offset = get_te32(&phdr->p_offset);
struct Elf32_Note note; memset(&note, 0, sizeof(note));
fi->seek(offset, SEEK_SET);
fi->readx(&note, sizeof(note));
fi->seek(0, SEEK_SET);
if (4==get_native32(&note.descsz)
&& 1==get_native32(&note.type)
if (4==get_te32(&note.descsz)
&& 1==get_te32(&note.type)
&& 0==note.end ) {
if (0==strcmp("NetBSD", (char const *)&note.text)) {
ei_osabi = Elf32_Ehdr::ELFOSABI_NETBSD;

View File

@ -205,7 +205,7 @@ void PackLinuxElf32x86interp::unpack(OutputFile *fo)
{
fi->seek(0, SEEK_SET);
fi->readx(bufehdr, MAX_INTERP_HDR);
unsigned const e_entry = get_native32(&ehdr->e_entry);
unsigned const e_entry = get_te32(&ehdr->e_entry);
if (e_entry < 0x401180) { /* old style, 8-byte b_info */
szb_info = 2*sizeof(unsigned);
}
@ -214,16 +214,16 @@ void PackLinuxElf32x86interp::unpack(OutputFile *fo)
fi->seek(overlay_offset, SEEK_SET);
p_info hbuf;
fi->readx(&hbuf, sizeof(hbuf));
unsigned orig_file_size = get_native32(&hbuf.p_filesize);
blocksize = get_native32(&hbuf.p_blocksize);
unsigned orig_file_size = get_te32(&hbuf.p_filesize);
blocksize = get_te32(&hbuf.p_blocksize);
if (file_size > (off_t)orig_file_size || blocksize > orig_file_size)
throwCantUnpack("file header corrupted");
ibuf.alloc(blocksize + OVERHEAD);
b_info bhdr; memset(&bhdr, 0, sizeof(bhdr));
fi->readx(&bhdr, szb_info);
ph.u_len = get_native32(&bhdr.sz_unc);
ph.c_len = get_native32(&bhdr.sz_cpr);
ph.u_len = get_te32(&bhdr.sz_unc);
ph.c_len = get_te32(&bhdr.sz_cpr);
ph.filter_cto = bhdr.b_cto8;
// Uncompress Ehdr and Phdrs.
@ -277,7 +277,7 @@ void PackLinuxElf32x86interp::unpack(OutputFile *fo)
// check for end-of-file
fi->readx(&bhdr, szb_info);
unsigned const sz_unc = ph.u_len = get_native32(&bhdr.sz_unc);
unsigned const sz_unc = ph.u_len = get_te32(&bhdr.sz_unc);
if (sz_unc == 0) { // uncompressed size 0 -> EOF
// note: magic is always stored le32

View File

@ -91,7 +91,7 @@ PackLinuxI386sh::buildLoader(Filter const *ft)
// filter
optimizeFilter(&fold_ft, buf, sz_fold);
unsigned fold_hdrlen = sizeof(l_info) + sizeof(Elf32_Ehdr) +
sizeof(Elf32_Phdr) * get_native32(&((Elf32_Ehdr const *)(void *)buf)->e_phnum);
sizeof(Elf32_Phdr) * get_te32(&((Elf32_Ehdr const *)(void *)buf)->e_phnum);
if (0 == get_le32(fold_hdrlen + buf)) {
// inconsistent SIZEOF_HEADERS in *.lds (ld, binutils)
fold_hdrlen = umax(0x80, fold_hdrlen);

View File

@ -520,16 +520,16 @@ void PackMachBase<T>::unpack(OutputFile *fo)
fi->seek(overlay_offset, SEEK_SET);
p_info hbuf;
fi->readx(&hbuf, sizeof(hbuf));
unsigned orig_file_size = get_native32(&hbuf.p_filesize);
blocksize = get_native32(&hbuf.p_blocksize);
unsigned orig_file_size = get_te32(&hbuf.p_filesize);
blocksize = get_te32(&hbuf.p_blocksize);
if (file_size > (off_t)orig_file_size || blocksize > orig_file_size)
throwCantUnpack("file header corrupted");
ibuf.alloc(blocksize + OVERHEAD);
b_info bhdr; memset(&bhdr, 0, sizeof(bhdr));
fi->readx(&bhdr, sizeof(bhdr));
ph.u_len = get_native32(&bhdr.sz_unc);
ph.c_len = get_native32(&bhdr.sz_cpr);
ph.u_len = get_te32(&bhdr.sz_unc);
ph.c_len = get_te32(&bhdr.sz_cpr);
ph.method = bhdr.b_method;
ph.filter = bhdr.b_ftid;
ph.filter_cto = bhdr.b_cto8;

View File

@ -432,13 +432,6 @@ public:
virtual const char *getName() const { return "Mach/ppc32"; }
virtual const char *getFullName(const options_t *) const { return "powerpc-darwin.macho"; }
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); }
virtual void set_native64(void *b, acc_uint64l_t v) const { set_be64(b, v); }
virtual void set_native32(void *b, unsigned v) const { set_be32(b, v); }
virtual void set_native16(void *b, unsigned v) const { set_be16(b, v); }
protected:
virtual const int *getFilters() const;
@ -477,13 +470,6 @@ public:
protected:
virtual const int *getFilters() const;
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); }
virtual void set_native64(void *b, acc_uint64l_t v) const { set_le64(b, v); }
virtual void set_native32(void *b, unsigned v) const { set_le32(b, v); }
virtual void set_native16(void *b, unsigned v) const { set_le16(b, v); }
virtual void pack1_setup_threado(OutputFile *const fo);
virtual void pack3(OutputFile *, Filter &); // append loader
virtual void pack4(OutputFile *, Filter &); // append PackHeader

View File

@ -191,8 +191,8 @@ void PackUnix::pack2(OutputFile *fo, Filter &ft)
// write block header
b_info blk_info;
memset(&blk_info, 0, sizeof(blk_info));
set_native32(&blk_info.sz_unc, ph.u_len);
set_native32(&blk_info.sz_cpr, ph.c_len);
set_te32(&blk_info.sz_unc, ph.u_len);
set_te32(&blk_info.sz_cpr, ph.c_len);
if (ph.c_len < ph.u_len) {
blk_info.b_method = (unsigned char) ph.method;
blk_info.b_ftid = (unsigned char) ph.filter;
@ -252,7 +252,7 @@ void PackUnix::pack4(OutputFile *fo, Filter &)
writePackHeader(fo);
unsigned tmp;
set_native32(&tmp, overlay_offset);
set_te32(&tmp, overlay_offset);
fo->write(&tmp, sizeof(tmp));
}
@ -278,9 +278,9 @@ void PackUnix::pack(OutputFile *fo)
pack1(fo, ft); // generate Elf header, etc.
p_info hbuf;
set_native32(&hbuf.p_progid, progid);
set_native32(&hbuf.p_filesize, file_size);
set_native32(&hbuf.p_blocksize, blocksize);
set_te32(&hbuf.p_progid, progid);
set_te32(&hbuf.p_filesize, file_size);
set_te32(&hbuf.p_blocksize, blocksize);
fo->write(&hbuf, sizeof(hbuf));
pack2(fo, ft); // append the compressed body
@ -389,8 +389,8 @@ void PackUnix::packExtent(
ph.c_adler = upx_adler32(obuf, ph.c_len, ph.saved_c_adler);
end_u_adler = ph.u_adler;
memset(&tmp, 0, sizeof(tmp));
set_native32(&tmp.sz_unc, hdr_u_len);
set_native32(&tmp.sz_cpr, hdr_c_len);
set_te32(&tmp.sz_unc, hdr_u_len);
set_te32(&tmp.sz_cpr, hdr_c_len);
tmp.b_method = (unsigned char) ph.method;
fo->write(&tmp, sizeof(tmp));
b_len += sizeof(b_info);
@ -400,8 +400,8 @@ void PackUnix::packExtent(
hdr_u_len = 0; // compress hdr one time only
}
memset(&tmp, 0, sizeof(tmp));
set_native32(&tmp.sz_unc, ph.u_len);
set_native32(&tmp.sz_cpr, ph.c_len);
set_te32(&tmp.sz_unc, ph.u_len);
set_te32(&tmp.sz_cpr, ph.c_len);
if (ph.c_len < ph.u_len) {
tmp.b_method = (unsigned char) ph.method;
if (ft) {
@ -439,8 +439,8 @@ void PackUnix::unpackExtent(unsigned wanted, OutputFile *fo,
b_info hdr; memset(&hdr, 0, sizeof(hdr));
while (wanted) {
fi->readx(&hdr, szb_info);
int const sz_unc = ph.u_len = get_native32(&hdr.sz_unc);
int const sz_cpr = ph.c_len = get_native32(&hdr.sz_cpr);
int const sz_unc = ph.u_len = get_te32(&hdr.sz_unc);
int const sz_cpr = ph.c_len = get_te32(&hdr.sz_cpr);
ph.filter_cto = hdr.b_cto8;
if (sz_unc == 0) { // must never happen while 0!=wanted
@ -509,7 +509,7 @@ int PackUnix::canUnpack()
int l = ph.buf_offset + ph.getPackHeaderSize();
if (l < 0 || l + 4 > bufsize)
throwCantUnpack("file corrupted");
overlay_offset = get_native32(buf+l);
overlay_offset = get_te32(buf+l);
if ((off_t)overlay_offset >= file_size)
throwCantUnpack("file corrupted");
@ -531,15 +531,15 @@ void PackUnix::unpack(OutputFile *fo)
Elf32_Ehdr ehdr;
fi->seek(0, SEEK_SET);
fi->readx(&ehdr, sizeof(ehdr));
unsigned const e_entry = get_native32(&ehdr.e_entry);
unsigned const e_entry = get_te32(&ehdr.e_entry);
if (e_entry < 0x401180) { /* old style, 8-byte b_info */
szb_info = 2*sizeof(unsigned);
}
else {
Elf32_Phdr phdr;
fi->seek(get_native32(&ehdr.e_phoff), SEEK_SET);
fi->seek(get_te32(&ehdr.e_phoff), SEEK_SET);
fi->readx(&phdr, sizeof(phdr));
unsigned const p_vaddr = get_native32(&phdr.p_vaddr);
unsigned const p_vaddr = get_te32(&phdr.p_vaddr);
if (0x80==(e_entry - p_vaddr)) { /* 1.22 old style */
szb_info = 2*sizeof(unsigned);
}
@ -558,8 +558,8 @@ void PackUnix::unpack(OutputFile *fo)
{
p_info hbuf;
fi->readx(&hbuf, sizeof(hbuf));
orig_file_size = get_native32(&hbuf.p_filesize);
blocksize = get_native32(&hbuf.p_blocksize);
orig_file_size = get_te32(&hbuf.p_filesize);
blocksize = get_te32(&hbuf.p_blocksize);
if (file_size > (off_t)orig_file_size || blocksize > orig_file_size)
throwCantUnpack("file header corrupted");
@ -583,8 +583,8 @@ void PackUnix::unpack(OutputFile *fo)
unsigned sz_unc, sz_cpr;
fi->readx(&bhdr, szb_info);
ph.u_len = sz_unc = get_native32(&bhdr.sz_unc);
ph.c_len = sz_cpr = get_native32(&bhdr.sz_cpr);
ph.u_len = sz_unc = get_te32(&bhdr.sz_unc);
ph.c_len = sz_cpr = get_te32(&bhdr.sz_cpr);
if (sz_unc == 0) // uncompressed size 0 -> EOF
{

View File

@ -64,15 +64,6 @@ protected:
virtual void writePackHeader(OutputFile *fo);
// in order too share as much code as possible we introduce
// an endian abstraction here
virtual acc_uint64l_t get_native64(const void *) const = 0;
virtual unsigned get_native32(const void *) const = 0;
virtual unsigned get_native16(const void *) const = 0;
virtual void set_native64(void *, acc_uint64l_t) const = 0;
virtual void set_native32(void *, unsigned) const = 0;
virtual void set_native16(void *, unsigned) const = 0;
virtual bool checkCompressionRatio(unsigned, unsigned) const;
protected:
@ -142,12 +133,6 @@ class PackUnixBe32 : public PackUnix
typedef PackUnix super;
protected:
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); }
virtual void set_native64(void *b, acc_uint64l_t v) const { set_be64(b, v); }
virtual void set_native32(void *b, unsigned v) const { set_be32(b, v); }
virtual void set_native16(void *b, unsigned v) const { set_be16(b, v); }
// must agree with stub/linux.hh
struct b_info { // 12-byte header before each compressed block
@ -183,12 +168,6 @@ class PackUnixLe32 : public PackUnix
typedef PackUnix super;
protected:
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); }
virtual void set_native64(void *b, acc_uint64l_t v) const { set_le64(b, v); }
virtual void set_native32(void *b, unsigned v) const { set_le32(b, v); }
virtual void set_native16(void *b, unsigned v) const { set_le16(b, v); }
// must agree with stub/linux.hh
struct b_info { // 12-byte header before each compressed block

View File

@ -283,8 +283,16 @@ protected:
static upx_byte *optimizeReloc32(upx_byte *in,unsigned relocnum,upx_byte *out,upx_byte *image,int bs,int *big);
static unsigned unoptimizeReloc32(upx_byte **in,upx_byte *image,MemBuffer *out,int bs);
// target endianness abstraction
unsigned get_te16(const void *p) const { return bele->get16(p); }
unsigned get_te32(const void *p) const { return bele->get32(p); }
acc_uint64l_t get_te64(const void *p) const { return bele->get64(p); }
void set_te16(void *p, unsigned v) const { bele->set16(p, v); }
void set_te32(void *p, unsigned v) const { bele->set32(p, v); }
void set_te64(void *p, acc_uint64l_t v) const { bele->set64(p, v); }
protected:
const N_BELE_RTP::AbstractPolicy *bele;
const N_BELE_RTP::AbstractPolicy *bele; // target endianness
InputFile *fi;
off_t file_size; // will get set by constructor
PackHeader ph; // must be filled by canUnpack()