From 9f5c0af5b881bbce02335a8fb8c24a26db2bf7c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Moln=C3=A1r?= Date: Wed, 26 Jul 2006 14:00:36 +0200 Subject: [PATCH] step 0 of refactoring arm/pe & win32/pe ready --- src/p_armpe.cpp | 11 - src/p_armpe.h | 4 +- src/p_w32pe.cpp | 1393 +---------------------------------------------- src/p_w32pe.h | 195 +------ src/pefile.cpp | 2 +- src/pefile.h | 2 +- 6 files changed, 21 insertions(+), 1586 deletions(-) diff --git a/src/p_armpe.cpp b/src/p_armpe.cpp index 00126024..a5a551e9 100644 --- a/src/p_armpe.cpp +++ b/src/p_armpe.cpp @@ -145,17 +145,6 @@ Linker* PackArmPe::newLinker() const } -/************************************************************************* -// util -**************************************************************************/ - -int PackArmPe::readFileHeader() -{ - int r = super::readFileHeader(); - return r == -1 ? UPX_F_WINCE_ARM_PE : r; -} - - /************************************************************************* // import handling **************************************************************************/ diff --git a/src/p_armpe.h b/src/p_armpe.h index 259d8907..2117376f 100644 --- a/src/p_armpe.h +++ b/src/p_armpe.h @@ -51,8 +51,8 @@ public: virtual int canUnpack(); protected: - virtual int readFileHeader(); virtual int buildLoader(const Filter *ft); + virtual Linker* newLinker() const; virtual unsigned processImports(); virtual void processImports(unsigned, unsigned); @@ -61,8 +61,6 @@ protected: virtual void processTls(Interval *); bool use_thumb_stub; - - virtual Linker* newLinker() const; }; diff --git a/src/p_w32pe.cpp b/src/p_w32pe.cpp index 7a0e4804..32ae42d8 100644 --- a/src/p_w32pe.cpp +++ b/src/p_w32pe.cpp @@ -30,6 +30,7 @@ #include "file.h" #include "filter.h" #include "packer.h" +#include "pefile.h" #include "p_w32pe.h" #include "linker.h" @@ -46,18 +47,6 @@ static const #define FILLVAL 0 -// Some defines to keep the namespace clean. -// It would be better to use inner classes except for Interval, which -// could be used elsewhere too. - -#define Interval PackW32Pe_Interval -#define Reloc PackW32Pe_Reloc -#define Resource PackW32Pe_Resource -#define import_desc PackW32Pe_import_desc -#define Export PackW32Pe_Export -#define tls PackW32Pe_tls - - /************************************************************************* // **************************************************************************/ @@ -68,7 +57,8 @@ static const #endif #if 1 -static unsigned my_strlen(const char *s) +//static +unsigned my_strlen(const char *s) { size_t l = strlen((const char*)s); assert((unsigned) l == l); return (unsigned) l; } @@ -81,17 +71,6 @@ static unsigned my_strlen(const unsigned char *s) #endif -// Unicode string compare -static bool ustrsame(const void *s1, const void *s2) -{ - unsigned len1 = get_le16(s1); - unsigned len2 = get_le16(s2); - if (len1 != len2) - return false; - return memcmp(s1, s2, 2 + 2*len1) == 0; -} - - #if (__ACC_CXX_HAVE_PLACEMENT_DELETE) || defined(__DJGPP__) #include "bptr.h" #define IPTR(type, var) BoundedPtr var(ibuf, ibuf.getSize()) @@ -139,29 +118,7 @@ static void xcheck(size_t poff, size_t plen, const void *b, size_t blen) PackW32Pe::PackW32Pe(InputFile *f) : super(f) { - //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); - COMPILE_TIME_ASSERT(sizeof(pe_section_t) == 40); - COMPILE_TIME_ASSERT(RT_LAST == TABLESIZE(opt->win32_pe.compress_rt)); - - isection = NULL; - oimport = NULL; - oimpdlls = NULL; - orelocs = NULL; - oexport = NULL; - otls = NULL; - oresources = NULL; - oxrelocs = NULL; oloadconf = NULL; - icondir_offset = 0; - icondir_count = 0; - importbyordinal = false; - kernel32ordinal = false; - tlsindex = 0; - big_relocs = 0; - sorelocs = 0; - soxrelocs = 0; soloadconf = 0; use_dep_hack = true; use_clear_dirty_stack = true; @@ -170,16 +127,7 @@ PackW32Pe::PackW32Pe(InputFile *f) : super(f) PackW32Pe::~PackW32Pe() { - delete [] isection; - delete [] orelocs; - delete [] oimport; - delete [] oimpdlls; - delete [] oexport; - delete [] otls; - delete [] oresources; - delete [] oxrelocs; delete [] oloadconf; - //delete res; } @@ -205,64 +153,17 @@ Linker* PackW32Pe::newLinker() const } -bool PackW32Pe::testUnpackVersion(int version) const -{ - if (version != ph_version && ph_version != -1) - throwCantUnpack("program has been modified; run a virus checker!"); - if (!canUnpackVersion(version)) - throwCantUnpack("this program is packed with an obsolete version and cannot be unpacked"); - return true; -} - - /************************************************************************* // util **************************************************************************/ int PackW32Pe::readFileHeader() { - struct exe_header_t - { - LE16 mz; - LE16 m512; - LE16 p512; - char _[18]; - LE16 relocoffs; - char __[34]; - LE32 nexepos; - } - __attribute_packed; - COMPILE_TIME_ASSERT(sizeof(exe_header_t) == 64); - - exe_header_t h; - int ic; - pe_offset = 0; - - for (ic = 0; ic < 20; ic++) - { - fi->seek(pe_offset,SEEK_SET); - fi->readx(&h,sizeof(h)); - - if (h.mz == 'M' + 'Z'*256) // dos exe - { - if (h.relocoffs >= 0x40) // new format exe - pe_offset += h.nexepos; - else - pe_offset += h.p512*512+h.m512 - h.m512 ? 512 : 0; - } - else if (get_le32(&h) == 'P' + 'E'*256) - break; - else - return 0; - } - if (ic == 20) - return 0; - fi->seek(pe_offset,SEEK_SET); - fi->readx(&ih,sizeof(ih)); - fi->seek(0x200,SEEK_SET); - fi->readx(&h,6); - isrtm = memcmp(&h,"32STUB",6) == 0; - return UPX_F_WIN32_PE; + char buf[6]; + fi->seek(0x200, SEEK_SET); + fi->readx(buf, 6); + isrtm = memcmp(&buf, "32STUB" ,6) == 0; + return super::readFileHeader(); } @@ -280,288 +181,6 @@ static bool defineFilterSymbols(Linker *linker, const Filter *ft) } -/************************************************************************* -// interval handling -**************************************************************************/ - -class Interval -{ - unsigned capacity; - void *base; -public: - struct interval - { - unsigned start, len; - } *ivarr; - - unsigned ivnum; - - Interval(void *b) : capacity(0),base(b),ivarr(NULL),ivnum(0) {} - ~Interval() {free(ivarr);} - - void add(unsigned start,unsigned len); - void add(const void *start,unsigned len) {add(ptr_diff(start,base),len);} - void add(const void *start,const void *end) {add(ptr_diff(start,base),ptr_diff(end,start));} - void add(const Interval *iv); - void flatten(); - - void clear(); - void dump() const; - -private: - static int __acc_cdecl_qsort compare(const void *p1,const void *p2) - { - const interval *i1 = (const interval*) p1; - const interval *i2 = (const interval*) p2; - if (i1->start < i2->start) return -1; - if (i1->start > i2->start) return 1; - if (i1->len < i2->len) return 1; - if (i1->len > i2->len) return -1; - return 0; - } -}; - -void Interval::add(unsigned start,unsigned len) -{ - if (ivnum == capacity) - ivarr = (interval*) realloc(ivarr,(capacity += 15) * sizeof (interval)); - ivarr[ivnum].start = start; - ivarr[ivnum++].len = len; -} - -void Interval::add(const Interval *iv) -{ - for (unsigned ic = 0; ic < iv->ivnum; ic++) - add(iv->ivarr[ic].start,iv->ivarr[ic].len); -} - -void Interval::flatten() -{ - if (!ivnum) - return; - qsort(ivarr,ivnum,sizeof (interval),Interval::compare); - for (unsigned ic = 0; ic < ivnum - 1; ic++) - { - unsigned jc; - for (jc = ic + 1; jc < ivnum && ivarr[ic].start + ivarr[ic].len >= ivarr[jc].start; jc++) - if (ivarr[ic].start + ivarr[ic].len < ivarr[jc].start + ivarr[jc].len) - ivarr[ic].len = ivarr[jc].start + ivarr[jc].len - ivarr[ic].start; - if (jc > ic + 1) - { - memmove(ivarr + ic + 1, ivarr + jc,sizeof(interval) * (ivnum - jc)); - ivnum -= jc - ic - 1; - } - } -} - -void Interval::clear() -{ - for (unsigned ic = 0; ic < ivnum; ic++) - memset((char*) base + ivarr[ic].start,0,ivarr[ic].len); -} - -void Interval::dump() const -{ - printf("%d intervals:\n",ivnum); - for (unsigned ic = 0; ic < ivnum; ic++) - printf("%x %x\n",ivarr[ic].start,ivarr[ic].len); -} - - -/************************************************************************* -// relocation handling -**************************************************************************/ - -class Reloc -{ - upx_byte *start; - unsigned size; - - struct reloc - { - LE32 pagestart; - LE32 size; - } - __attribute_packed; - - void newRelocPos(void *p) - { - rel = (reloc*) p; - rel1 = (LE16*) ((char*) p + sizeof (reloc)); - } - - reloc *rel; - LE16 *rel1; - unsigned counts[16]; - -public: - Reloc(upx_byte *,unsigned); - Reloc(unsigned rnum); - // - bool next(unsigned &pos,unsigned &type); - const unsigned *getcounts() const { return counts; } - // - void add(unsigned pos,unsigned type); - void finish(upx_byte *&p,unsigned &size); -}; - -Reloc::Reloc(upx_byte *s,unsigned si) : - start(s), size(si), rel(NULL), rel1(NULL) -{ - COMPILE_TIME_ASSERT(sizeof(reloc) == 8); - memset(counts,0,sizeof(counts)); - unsigned pos,type; - while (next(pos,type)) - counts[type]++; -} - -Reloc::Reloc(unsigned rnum) : - start(NULL), size(0), rel(NULL), rel1(NULL) -{ - start = new upx_byte[rnum * 4 + 8192]; - counts[0] = 0; -} - -bool Reloc::next(unsigned &pos,unsigned &type) -{ - if (!rel) - newRelocPos(start); - if (ptr_diff(rel, start) >= (int) size || rel->pagestart == 0) - return rel = 0,false; // rewind - - pos = rel->pagestart + (*rel1 & 0xfff); - type = *rel1++ >> 12; - //printf("%x %d\n",pos,type); - if (ptr_diff(rel1,rel) >= (int) rel->size) - newRelocPos(rel1); - return type == 0 ? next(pos,type) : true; -} - -void Reloc::add(unsigned pos,unsigned type) -{ - set_le32(start + 1024 + 4 * counts[0]++,(pos << 4) + type); -} - -void Reloc::finish(upx_byte *&p,unsigned &siz) -{ - unsigned prev = 0xffffffff; - set_le32(start + 1024 + 4 * counts[0]++,0xf0000000); - qsort(start + 1024,counts[0],4,le32_compare); - - rel = (reloc*) start; - rel1 = (LE16*) rel; - for (unsigned ic = 0; ic < counts[0]; ic++) - { - unsigned pos = get_le32(start + 1024 + 4 * ic); - if ((pos ^ prev) >= 0x10000) - { - prev = pos; - *rel1 = 0; - rel->size = ALIGN_UP(ptr_diff(rel1,rel),4); - newRelocPos((char *)rel + rel->size); - rel->pagestart = (pos >> 4) &~ 0xfff; - } - *rel1++ = (pos << 12) + ((pos >> 4) & 0xfff); - } - p = start; - siz = ptr_diff(rel1,start) &~ 3; - siz -= 8; - assert(siz > 0); - start = 0; // safety -} - -void PackW32Pe::processRelocs(Reloc *rel) // pass2 -{ - rel->finish(oxrelocs,soxrelocs); - if (opt->win32_pe.strip_relocs && !isdll) - soxrelocs = 0; -} - -void PackW32Pe::processRelocs() // pass1 -{ - big_relocs = 0; - - Reloc rel(ibuf + IDADDR(PEDIR_RELOC),IDSIZE(PEDIR_RELOC)); - const unsigned *counts = rel.getcounts(); - const unsigned rnum = counts[1] + counts[2] + counts[3]; - - if ((opt->win32_pe.strip_relocs && !isdll) || rnum == 0) - { - if (IDSIZE(PEDIR_RELOC)) - ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL); - orelocs = new upx_byte [1]; - sorelocs = 0; - return; - } - - unsigned ic; - for (ic = 15; ic > 3; ic--) - if (counts[ic]) - infoWarning("skipping unsupported relocation type %d (%d)",ic,counts[ic]); - - LE32 *fix[4]; - for (; ic; ic--) - fix[ic] = new LE32 [counts[ic]]; - - unsigned xcounts[4]; - memset(xcounts, 0, sizeof(xcounts)); - - // prepare sorting - unsigned pos,type; - while (rel.next(pos,type)) - { - if (pos >= ih.imagesize) - continue; // skip out-of-bounds record - if (type < 4) - fix[type][xcounts[type]++] = pos - rvamin; - } - - // remove duplicated records - for (ic = 1; ic <= 3; ic++) - { - qsort(fix[ic], xcounts[ic], 4, le32_compare); - unsigned prev = ~0; - unsigned jc = 0; - for (unsigned kc = 0; kc < xcounts[ic]; kc++) - if (fix[ic][kc] != prev) - prev = fix[ic][jc++] = fix[ic][kc]; - - //printf("xcounts[%u] %u->%u\n", ic, xcounts[ic], jc); - xcounts[ic] = jc; - } - - // preprocess "type 3" relocation records - for (ic = 0; ic < xcounts[3]; ic++) - { - pos = fix[3][ic] + rvamin; - set_le32(ibuf + pos, get_le32(ibuf + pos) - ih.imagebase - rvamin); - } - - ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL); - orelocs = new upx_byte [rnum * 4 + 1024]; // 1024 - safety - sorelocs = ptr_diff(optimizeReloc32((upx_byte*) fix[3], xcounts[3], - orelocs, ibuf + rvamin,1, &big_relocs), - orelocs); - delete [] fix[3]; - - // append relocs type "LOW" then "HIGH" - for (ic = 2; ic ; ic--) - { - memcpy(orelocs + sorelocs,fix[ic],4 * xcounts[ic]); - sorelocs += 4 * xcounts[ic]; - delete [] fix[ic]; - - set_le32(orelocs + sorelocs,0); - if (xcounts[ic]) - { - sorelocs += 4; - big_relocs |= 2 * ic; - } - } - info("Relocations: original size: %u bytes, preprocessed size: %u bytes",(unsigned) IDSIZE(PEDIR_RELOC),sorelocs); -} - - /************************************************************************* // import handling **************************************************************************/ @@ -575,7 +194,7 @@ struct import_desc } __attribute_packed; -void PackW32Pe::processImports(unsigned myimport) // pass 2 +void PackW32Pe::processImports(unsigned myimport, unsigned) // pass 2 { COMPILE_TIME_ASSERT(sizeof(import_desc) == 20); @@ -858,733 +477,6 @@ unsigned PackW32Pe::processImports() // pass 1 } -/************************************************************************* -// export handling -**************************************************************************/ - -class Export -{ - struct export_dir_t - { - char _[12]; // flags, timedate, version - LE32 name; - char __[4]; // ordinal base - LE32 functions; - LE32 names; - LE32 addrtable; - LE32 nameptrtable; - LE32 ordinaltable; - } - __attribute_packed; - - export_dir_t edir; - char *ename; - char *functionptrs; - char *ordinals; - char **names; - - char *base; - unsigned size; - Interval iv; - -public: - Export(char *_base); - ~Export(); - - void convert(unsigned eoffs,unsigned esize); - void build(char *base,unsigned newoffs); - unsigned getsize() const { return size; } - -private: - // disable copy and assignment - Export(const Export&); - Export& operator=(const Export&); -}; - -Export::Export(char *_base) : base(_base), iv(_base) -{ - COMPILE_TIME_ASSERT(sizeof(export_dir_t) == 40); - ename = functionptrs = ordinals = NULL; - names = NULL; - memset(&edir,0,sizeof(edir)); - size = 0; -} - -Export::~Export() -{ - free(ename); - delete [] functionptrs; - delete [] ordinals; - for (unsigned ic = 0; ic < edir.names + edir.functions; ic++) - free(names[ic]); - delete [] names; -} - -void Export::convert(unsigned eoffs,unsigned esize) -{ - memcpy(&edir,base + eoffs,sizeof(export_dir_t)); - size = sizeof(export_dir_t); - iv.add(eoffs,size); - - unsigned len = strlen(base + edir.name) + 1; - ename = strdup(base + edir.name); - size += len; - iv.add(edir.name,len); - - len = 4 * edir.functions; - functionptrs = new char[len + 1]; - memcpy(functionptrs,base + edir.addrtable,len); - size += len; - iv.add(edir.addrtable,len); - - unsigned ic; - names = new char* [edir.names + edir.functions + 1]; - for (ic = 0; ic < edir.names; ic++) - { - char *n = base + get_le32(base + edir.nameptrtable + ic * 4); - len = strlen(n) + 1; - names[ic] = strdup(n); - size += len; - iv.add(get_le32(base + edir.nameptrtable + ic * 4),len); - } - iv.add(edir.nameptrtable,4 * edir.names); - size += 4 * edir.names; - - LE32 *fp = (LE32*) functionptrs; - // export forwarders - for (ic = 0; ic < edir.functions; ic++) - if (fp[ic] >= eoffs && fp[ic] < eoffs + esize) - { - char *forw = base + fp[ic]; - len = strlen(forw) + 1; - iv.add(forw,len); - size += len; - names[ic + edir.names] = strdup(forw); - } - else - names[ic + edir.names] = NULL; - - len = 2 * edir.names; - ordinals = new char[len + 1]; - memcpy(ordinals,base + edir.ordinaltable,len); - size += len; - iv.add(edir.ordinaltable,len); - iv.flatten(); - if (iv.ivnum == 1) - iv.clear(); -#if defined(DEBUG) - else - iv.dump(); -#endif -} - -void Export::build(char *newbase, unsigned newoffs) -{ - char * const functionp = newbase + sizeof(edir); - char * const namep = functionp + 4 * edir.functions; - char * const ordinalp = namep + 4 * edir.names; - char * const enamep = ordinalp + 2 * edir.names; - char * exports = enamep + strlen(ename) + 1; - - edir.addrtable = newoffs + ptr_diff(functionp, newbase); - edir.ordinaltable = newoffs + ptr_diff(ordinalp, newbase); - memcpy(ordinalp,ordinals,2 * edir.names); - - edir.name = newoffs + ptr_diff(enamep, newbase); - strcpy(enamep,ename); - edir.nameptrtable = newoffs + ptr_diff(namep, newbase); - unsigned ic; - for (ic = 0; ic < edir.names; ic++) - { - strcpy(exports,names[ic]); - set_le32(namep + 4 * ic,newoffs + ptr_diff(exports, newbase)); - exports += strlen(exports) + 1; - } - - memcpy(functionp,functionptrs,4 * edir.functions); - for (ic = 0; ic < edir.functions; ic++) - if (names[edir.names + ic]) - { - strcpy(exports,names[edir.names + ic]); - set_le32(functionp + 4 * ic,newoffs + ptr_diff(exports, newbase)); - exports += strlen(exports) + 1; - } - - memcpy(newbase,&edir,sizeof(edir)); - assert(exports - newbase == (int) size); -} - -void PackW32Pe::processExports(Export *xport) // pass1 -{ - soexport = ALIGN_UP(IDSIZE(PEDIR_EXPORT),4); - if (soexport == 0) - return; - if (!isdll && opt->win32_pe.compress_exports) - { - infoWarning("exports compressed, --compress-exports=0 might be needed"); - soexport = 0; - return; - } - xport->convert(IDADDR(PEDIR_EXPORT),IDSIZE(PEDIR_EXPORT)); - soexport = ALIGN_UP(xport->getsize(),4); - oexport = new upx_byte[soexport]; - memset(oexport, 0, soexport); -} - -void PackW32Pe::processExports(Export *xport,unsigned newoffs) // pass2 -{ - if (soexport) - xport->build((char*) oexport,newoffs); -} - - -/************************************************************************* -// TLS handling -**************************************************************************/ - -// thanks for theowl for providing me some docs, so that now I understand -// what I'm doing here :) - -// 1999-10-17: this was tricky to find: -// when the fixup records and the tls area are on the same page, then -// the tls area is not relocated, because the relocation is done by -// the virtual memory manager only for pages which are not yet loaded. -// of course it was impossible to debug this ;-) - -struct tls -{ - LE32 datastart; // VA tls init data start - LE32 dataend; // VA tls init data end - LE32 tlsindex; // VA tls index - LE32 callbacks; // VA tls callbacks - char _[8]; // zero init, characteristics -} -__attribute_packed; - -void PackW32Pe::processTls(Interval *iv) // pass 1 -{ - COMPILE_TIME_ASSERT(sizeof(tls) == 24); - - if ((sotls = ALIGN_UP(IDSIZE(PEDIR_TLS),4)) == 0) - return; - - const tls * const tlsp = (const tls*) (ibuf + IDADDR(PEDIR_TLS)); - // note: TLS callbacks are not implemented in Windows 95/98/ME - if (tlsp->callbacks) - { - if (tlsp->callbacks < ih.imagebase) - throwCantPack("invalid TLS callback"); - else if (tlsp->callbacks - ih.imagebase + 4 >= ih.imagesize) - throwCantPack("invalid TLS callback"); - unsigned v = get_le32(ibuf + tlsp->callbacks - ih.imagebase); - if (v != 0) - { - //fprintf(stderr, "TLS callbacks: 0x%0x -> 0x%0x\n", (int)tlsp->callbacks, v); - throwCantPack("TLS callbacks are not supported"); - } - } - - const unsigned tlsdatastart = tlsp->datastart - ih.imagebase; - const unsigned tlsdataend = tlsp->dataend - ih.imagebase; - - // now some ugly stuff: find the relocation entries in the tls data area - unsigned pos,type; - Reloc rel(ibuf + IDADDR(PEDIR_RELOC),IDSIZE(PEDIR_RELOC)); - while (rel.next(pos,type)) - if (pos >= tlsdatastart && pos < tlsdataend) - iv->add(pos,type); - - sotls = sizeof(tls) + tlsdataend - tlsdatastart; - - // the PE loader wants this stuff uncompressed - otls = new upx_byte[sotls]; - memset(otls,0,sotls); - memcpy(otls,ibuf + IDADDR(PEDIR_TLS),0x18); - // WARNING: this can acces data in BSS - memcpy(otls + sizeof(tls),ibuf + tlsdatastart,sotls - sizeof(tls)); - tlsindex = tlsp->tlsindex - ih.imagebase; - info("TLS: %u bytes tls data and %u relocations added",sotls - (unsigned) sizeof(tls),iv->ivnum); -} - -void PackW32Pe::processTls(Reloc *rel,const Interval *iv,unsigned newaddr) // pass 2 -{ - if (sotls == 0) - return; - // add new relocation entries - unsigned ic; - for (ic = 0; ic < 12; ic += 4) - rel->add(newaddr + ic,3); - - tls * const tlsp = (tls*) otls; - // now the relocation entries in the tls data area - for (ic = 0; ic < iv->ivnum; ic += 4) - { - void *p = otls + iv->ivarr[ic].start - (tlsp->datastart - ih.imagebase) + sizeof(tls); - unsigned kc = get_le32(p); - if (kc < tlsp->dataend && kc >= tlsp->datastart) - { - kc += newaddr + sizeof(tls) - tlsp->datastart; - set_le32(p,kc + ih.imagebase); - rel->add(kc,iv->ivarr[ic].len); - } - else - rel->add(kc - ih.imagebase,iv->ivarr[ic].len); - } - - tlsp->datastart = newaddr + sizeof(tls) + ih.imagebase; - tlsp->dataend = newaddr + sotls + ih.imagebase; - tlsp->callbacks = 0; // note: TLS callbacks are not implemented in Windows 95/98/ME -} - - -/************************************************************************* -// resource handling -**************************************************************************/ - -class Resource -{ - struct res_dir_entry - { - LE32 tnl; // Type | Name | Language id - depending on level - LE32 child; - } - __attribute_packed; - struct res_dir - { - char _[12]; // flags, timedate, version - LE16 namedentr; - LE16 identr; - - unsigned Sizeof() const { return 16 + sizeof(res_dir_entry)*(namedentr + identr); } - res_dir_entry entries[1]; - // it's usually safe to assume that every res_dir contains - // at least one res_dir_entry - check() complains otherwise - } - __attribute_packed; - struct res_data - { - LE32 offset; - LE32 size; - char _[8]; // codepage, reserved - } - __attribute_packed; - // - struct upx_rnode - { - unsigned id; - upx_byte *name; - upx_rnode *parent; - }; - struct upx_rbranch : public upx_rnode - { - unsigned nc; - upx_rnode **children; - res_dir data; - }; - struct upx_rleaf : public upx_rnode - { - upx_rleaf *next; - unsigned newoffset; - res_data data; - }; - - const upx_byte *start; - upx_byte *newstart; - upx_rnode *root; - upx_rleaf *head; - upx_rleaf *current; - unsigned dsize; - unsigned ssize; - - void check(const res_dir*,unsigned); - upx_rnode *convert(const void *,upx_rnode *,unsigned); - void build(const upx_rnode *,unsigned &,unsigned &,unsigned); - void clear(upx_byte *,unsigned,Interval *); - void dump(const upx_rnode *,unsigned) const; - void destroy(upx_rnode *urd,unsigned level); - -public: - Resource() : root(NULL) {} - Resource(const upx_byte *p) {init(p);} - ~Resource() {if (root) destroy (root,0);} - void init(const upx_byte *); - - unsigned dirsize() const {return ALIGN_UP(dsize + ssize,4);} - bool next() {return (current = current ? current->next : head) != NULL;} // wow, builtin autorewind... :-) - - unsigned itype() const {return current->parent->parent->id;} - const upx_byte *ntype() const {return current->parent->parent->name;} - unsigned size() const {return ALIGN_UP(current->data.size,4);} - unsigned offs() const {return current->data.offset;} - unsigned &newoffs() {return current->newoffset;} - - upx_byte *build(); - bool clear(); - - void dump() const { dump(root,0); } - unsigned iname() const {return current->parent->id;} - const upx_byte *nname() const {return current->parent->name;} -/* - unsigned ilang() const {return current->id;} - const upx_byte *nlang() const {return current->name;} -*/ -}; - -void Resource::init(const upx_byte *res) -{ - COMPILE_TIME_ASSERT(sizeof(res_dir_entry) == 8); - COMPILE_TIME_ASSERT(sizeof(res_dir) == 16 + sizeof(res_dir_entry)); - COMPILE_TIME_ASSERT(sizeof(res_data) == 16); - - start = res; - root = head = current = NULL; - dsize = ssize = 0; - check((const res_dir*) start,0); - root = convert(start,NULL,0); -} - -void Resource::check(const res_dir *node,unsigned level) -{ - int ic = node->identr + node->namedentr; - if (ic == 0) - return; - for (const res_dir_entry *rde = node->entries; --ic >= 0; rde++) - if (((rde->child & 0x80000000) == 0) ^ (level == 2)) - throwCantPack("unsupported resource structure"); - else if (level != 2) - check((const res_dir*) (start + (rde->child & 0x7fffffff)),level + 1); -} - -Resource::upx_rnode *Resource::convert(const void *rnode,upx_rnode *parent,unsigned level) -{ - if (level == 3) - { - const res_data *node = (const res_data *) rnode; - upx_rleaf *leaf = new upx_rleaf; - leaf->name = NULL; - leaf->parent = parent; - leaf->next = head; - leaf->newoffset = 0; - leaf->data = *node; - - head = leaf; // append node to a linked list for traversal - dsize += sizeof(res_data); - return leaf; - } - - const res_dir *node = (const res_dir *) rnode; - int ic = node->identr + node->namedentr; - if (ic == 0) - return NULL; - - upx_rbranch *branch = new upx_rbranch; - branch->name = NULL; - branch->parent = parent; - branch->nc = ic; - branch->children = new upx_rnode*[ic]; - branch->data = *node; - - for (const res_dir_entry *rde = node->entries + ic - 1; --ic >= 0; rde--) - { - upx_rnode *child = convert(start + (rde->child & 0x7fffffff),branch,level + 1); - branch->children[ic] = child; - child->id = rde->tnl; - if (child->id & 0x80000000) - { - const upx_byte *p = start + (child->id & 0x7fffffff); - const unsigned len = 2 + 2 * get_le16(p); - child->name = new upx_byte[len]; - memcpy(child->name,p,len); // copy unicode string - ssize += len; // size of unicode strings - } - } - dsize += node->Sizeof(); - return branch; -} - -void Resource::build(const upx_rnode *node,unsigned &bpos,unsigned &spos,unsigned level) -{ - if (level == 3) - { - res_data *l = (res_data*) (newstart + bpos); - const upx_rleaf *leaf = (const upx_rleaf*) node; - *l = leaf->data; - if (leaf->newoffset) - l->offset = leaf->newoffset; - bpos += sizeof(*l); - return; - } - res_dir * const b = (res_dir*) (newstart + bpos); - const upx_rbranch *branch = (const upx_rbranch*) node; - *b = branch->data; - bpos += b->Sizeof(); - res_dir_entry *be = b->entries; - for (unsigned ic = 0; ic < branch->nc; ic++, be++) - { - be->tnl = branch->children[ic]->id; - be->child = bpos + ((level < 2) ? 0x80000000 : 0); - - const upx_byte *p; - if ((p = branch->children[ic]->name) != 0) - { - be->tnl = spos + 0x80000000; - memcpy(newstart + spos,p,get_le16(p) * 2 + 2); - spos += get_le16(p) * 2 + 2; - } - - build(branch->children[ic],bpos,spos,level + 1); - } -} - -upx_byte *Resource::build() -{ - newstart = new upx_byte [dirsize()]; - unsigned bpos = 0,spos = dsize; - build(root,bpos,spos,0); - return newstart; -} - -void Resource::destroy(upx_rnode *node,unsigned level) -{ - delete [] node->name; node->name = NULL; - if (level != 3) - { - upx_rbranch * const branch = (upx_rbranch *) node; - for (int ic = branch->nc; --ic >= 0; ) - destroy(branch->children[ic],level + 1); - delete [] branch->children; branch->children = NULL; - } - delete node; -} - -static void lame_print_unicode(const upx_byte *p) -{ - for (unsigned ic = 0; ic < get_le16(p); ic++) - printf("%c",(char)p[ic * 2 + 2]); -} - -void Resource::dump(const upx_rnode *node,unsigned level) const -{ - if (level) - { - for (unsigned ic = 1; ic < level; ic++) - printf("\t\t"); - if (node->name) - lame_print_unicode(node->name); - else - printf("0x%x",node->id); - printf("\n"); - } - if (level == 3) - return; - const upx_rbranch * const branch = (const upx_rbranch *) node; - for (unsigned ic = 0; ic < branch->nc; ic++) - dump(branch->children[ic],level + 1); -} - -void Resource::clear(upx_byte *node,unsigned level,Interval *iv) -{ - if (level == 3) - iv->add(node,sizeof (res_data)); - else - { - const res_dir * const rd = (res_dir*) node; - const unsigned n = rd->identr + rd->namedentr; - const res_dir_entry *rde = rd->entries; - for (unsigned ic = 0; ic < n; ic++, rde++) - clear(newstart + (rde->child & 0x7fffffff),level + 1,iv); - iv->add(rd,rd->Sizeof()); - } -} - -bool Resource::clear() -{ - newstart = const_cast (start); - Interval iv(newstart); - clear(newstart,0,&iv); - iv.flatten(); - if (iv.ivnum == 1) - iv.clear(); -#if defined(DEBUG) - if (opt->verbose > 3) - iv.dump(); -#endif - return iv.ivnum == 1; -} - -void PackW32Pe::processResources(Resource *res,unsigned newaddr) -{ - if (IDSIZE(PEDIR_RESOURCE) == 0) - return; - while (res->next()) - if (res->newoffs()) - res->newoffs() += newaddr; - upx_byte *p = res->build(); - memcpy(oresources,p,res->dirsize()); - delete [] p; -} - -static bool match(unsigned itype, const unsigned char *ntype, - unsigned iname, const unsigned char *nname, - const char *keep) -{ - // format of string keep: type1[/name1],type2[/name2], .... - // typex and namex can be string or number - // hopefully resource names do not have '/' or ',' characters inside - - struct helper - { - static bool match(unsigned num, const unsigned char *unistr, - const char *mkeep) - { - if (!unistr) - return (unsigned) atoi(mkeep) == num; - - unsigned ic; - for (ic = 0; ic < get_le16(unistr); ic++) - if (unistr[2 + ic * 2] != (unsigned char) mkeep[ic]) - return false; - return mkeep[ic] == 0 || mkeep[ic] == ',' || mkeep[ic] == '/'; - }; - }; - - // FIXME this comparison is not too exact - while (1) - { - char *delim1 = strchr(keep, '/'); - char *delim2 = strchr(keep, ','); - if (helper::match(itype, ntype, keep)) - { - if (!delim1) - return true; - if (delim2 && delim2 < delim1) - return true; - if (helper::match(iname, nname, delim1 + 1)) - return true; - } - - if (delim2 == NULL) - break; - keep = delim2 + 1; - } - return false; -} - -void PackW32Pe::processResources(Resource *res) -{ - const unsigned vaddr = IDADDR(PEDIR_RESOURCE); - if ((soresources = IDSIZE(PEDIR_RESOURCE)) == 0) - return; - - // setup default options for resource compression - if (opt->win32_pe.compress_resources < 0) - opt->win32_pe.compress_resources = true; - if (!opt->win32_pe.compress_resources) - { - opt->win32_pe.compress_icons = false; - for (int i = 0; i < RT_LAST; i++) - opt->win32_pe.compress_rt[i] = false; - } - if (opt->win32_pe.compress_rt[RT_STRING] < 0) - { - // by default, don't compress RT_STRINGs of screensavers (".scr") - opt->win32_pe.compress_rt[RT_STRING] = true; - if (fn_has_ext(fi->getName(),"scr")) - opt->win32_pe.compress_rt[RT_STRING] = false; - } - - res->init(ibuf + vaddr); - - for (soresources = res->dirsize(); res->next(); soresources += 4 + res->size()) - ; - oresources = new upx_byte[soresources]; - upx_byte *ores = oresources + res->dirsize(); - - unsigned iconsin1stdir = 0; - if (opt->win32_pe.compress_icons == 2) - while (res->next()) // there is no rewind() in Resource - if (res->itype() == RT_GROUP_ICON && iconsin1stdir == 0) - iconsin1stdir = get_le16(ibuf + res->offs() + 4); - - bool compress_icon = false, compress_idir = false; - unsigned iconcnt = 0; - - // some statistics - unsigned usize = 0; - unsigned csize = 0; - unsigned unum = 0; - unsigned cnum = 0; - - while (res->next()) - { - const unsigned rtype = res->itype(); - bool do_compress = true; - if (!opt->win32_pe.compress_resources) - do_compress = false; - else if (rtype == RT_VERSION) // version info - do_compress = false; - else if (rtype == RT_ICON) // icon - do_compress = compress_icon && opt->win32_pe.compress_icons; - else if (rtype == RT_GROUP_ICON) // icon directory - do_compress = compress_idir && opt->win32_pe.compress_icons; - else if (rtype > 0 && rtype < RT_LAST) - do_compress = opt->win32_pe.compress_rt[rtype] ? true : false; - else if (res->ntype()) // named resource type - { - const upx_byte * const t = res->ntype(); - if (ustrsame(t, "\x7\x0T\x0Y\x0P\x0""E\x0L\x0I\x0""B\x0")) - do_compress = false; // u"TYPELIB" - else if (ustrsame(t, "\x8\x0R\x0""E\x0G\x0I\x0S\x0T\x0R\x0Y\x0")) - do_compress = false; // u"REGISTRY" - } - - if (do_compress && opt->win32_pe.keep_resource[0]) - do_compress ^= match(res->itype(), res->ntype(), res->iname(), - res->nname(), opt->win32_pe.keep_resource); - - if (do_compress) - { - csize += res->size(); - cnum++; - continue; - } - - usize += res->size(); - unum++; - - set_le32(ores,res->offs()); // save original offset - ores += 4; - ICHECK(ibuf + res->offs(), res->size()); - memcpy(ores, ibuf + res->offs(), res->size()); - ibuf.fill(res->offs(), res->size(), FILLVAL); - res->newoffs() = ptr_diff(ores,oresources); - if (rtype == RT_ICON) - compress_icon = (++iconcnt >= iconsin1stdir || opt->win32_pe.compress_icons == 1); - else if (rtype == RT_GROUP_ICON) - { - if (opt->win32_pe.compress_icons == 1) - { - icondir_offset = 4 + ptr_diff(ores,oresources); - icondir_count = get_le16(oresources + icondir_offset); - set_le16(oresources + icondir_offset,1); - } - compress_idir = true; - } - ores += res->size(); - } - soresources = ptr_diff(ores,oresources); - - if (!res->clear()) - { - // The area occupied by the resource directory is not continuous - // so to still support uncompression, I can't zero this area. - // This decreases compression ratio, so FIXME somehow. - infoWarning("can't remove unneeded resource directory"); - } - info("Resources: compressed %u (%u bytes), not compressed %u (%u bytes)",cnum,csize,unum,usize); -} - - /************************************************************************* // Load Configuration handling **************************************************************************/ @@ -1630,57 +522,6 @@ void PackW32Pe::processLoadConf(Reloc *rel, const Interval *iv, } } -// - -unsigned PackW32Pe::virta2objnum(unsigned addr,pe_section_t *sect,unsigned objs) -{ - unsigned ic; - for (ic = 0; ic < objs; ic++) - { - if (sect->vaddr <= addr && sect->vaddr + sect->vsize > addr) - return ic; - sect++; - } - //throwCantPack("virta2objnum() failed"); - return ic; -} - - -unsigned PackW32Pe::tryremove (unsigned vaddr,unsigned objs) -{ - unsigned ic = virta2objnum(vaddr,isection,objs); - if (ic && ic == objs - 1) - { - //fprintf(stderr,"removed section: %d size: %lx\n",ic,(long)isection[ic].size); - info("removed section: %d size: 0x%lx",ic,(long)isection[ic].size); - objs--; - } - return objs; -} - - -unsigned PackW32Pe::stripDebug(unsigned overlaystart) -{ - if (IDADDR(PEDIR_DEBUG) == 0) - return overlaystart; - - struct debug_dir_t - { - char _[16]; // flags, time/date, version, type - LE32 size; - char __[4]; // rva - LE32 fpos; - } - __attribute_packed; - - const debug_dir_t *dd = (const debug_dir_t*) (ibuf + IDADDR(PEDIR_DEBUG)); - for (unsigned ic = 0; ic < IDSIZE(PEDIR_DEBUG) / sizeof(debug_dir_t); ic++, dd++) - if (overlaystart == dd->fpos) - overlaystart += dd->size; - ibuf.fill(IDADDR(PEDIR_DEBUG), IDSIZE(PEDIR_DEBUG), FILLVAL); - return overlaystart; -} - /************************************************************************* // pack @@ -1788,7 +629,7 @@ void PackW32Pe::pack(OutputFile *fo) throwCantPack("unexpected value in PE header (try --force)"); if (IDSIZE(PEDIR_SEC)) - throwCantPack("compressing certificate info is not supported"); + IDSIZE(PEDIR_SEC) = IDADDR(PEDIR_SEC) = 0; if (IDSIZE(PEDIR_COMRT)) throwCantPack(".NET files (win32/net) are not yet supported"); @@ -2120,7 +961,7 @@ void PackW32Pe::pack(OutputFile *fo) ODSIZE(PEDIR_RESOURCE) = soresources; ic += soresources; - processImports(ic); + processImports(ic, 0); ODADDR(PEDIR_IMPORT) = ic; ODSIZE(PEDIR_IMPORT) = soimpdlls; ic += soimpdlls; @@ -2439,218 +1280,6 @@ void PackW32Pe::rebuildImports(upx_byte *& extrainfo) //memset(idata,0,p - idata); } -void PackW32Pe::rebuildRelocs(upx_byte *& extrainfo) -{ - if (!ODADDR(PEDIR_RELOC) || !ODSIZE(PEDIR_RELOC) || (oh.flags & RELOCS_STRIPPED)) - return; - - if (ODSIZE(PEDIR_RELOC) == 8) // some tricky dlls use this - { - omemcpy(obuf + ODADDR(PEDIR_RELOC) - rvamin, "\x0\x0\x0\x0\x8\x0\x0\x0", 8); - return; - } - - upx_byte *rdata = obuf + get_le32(extrainfo); - const upx_byte big = extrainfo[4]; - extrainfo += 5; - -// upx_byte *p = rdata; - OPTR_I(upx_byte, p, rdata); - MemBuffer wrkmem; - unsigned relocn = unoptimizeReloc32(&rdata,obuf,&wrkmem,1); - unsigned r16 = 0; - if (big & 6) // 16 bit relocations - { - const LE32 *q = (LE32*) rdata; - while (*q++) - r16++; - if ((big & 6) == 6) - while (*++q) - r16++; - } - Reloc rel(relocn + r16); - - if (big & 6) - { - LE32 *q = (LE32*) rdata; - while (*q) - rel.add(*q++ + rvamin,(big & 4) ? 2 : 1); - if ((big & 6) == 6) - while (*++q) - rel.add(*q + rvamin,1); - rdata = (upx_byte*) q; - } - - //memset(p,0,rdata - p); - - for (unsigned ic = 0; ic < relocn; ic++) - { - p = obuf + get_le32(wrkmem + 4 * ic); - set_le32(p, get_le32((unsigned char *)p) + oh.imagebase + rvamin); - rel.add(rvamin + get_le32(wrkmem + 4 * ic),3); - } - rel.finish (oxrelocs,soxrelocs); - - if (opt->win32_pe.strip_relocs && !isdll) - { - obuf.clear(ODADDR(PEDIR_RELOC) - rvamin, ODSIZE(PEDIR_RELOC)); - ODADDR(PEDIR_RELOC) = 0; - soxrelocs = 0; - // FIXME: try to remove the original relocation section somehow - } - else - omemcpy(obuf + ODADDR(PEDIR_RELOC) - rvamin,oxrelocs,soxrelocs); - delete [] oxrelocs; oxrelocs = NULL; - wrkmem.dealloc(); - - ODSIZE(PEDIR_RELOC) = soxrelocs; -} - -void PackW32Pe::rebuildExports() -{ - if (ODSIZE(PEDIR_EXPORT) == 0 || ODADDR(PEDIR_EXPORT) == IDADDR(PEDIR_EXPORT)) - return; // nothing to do - - opt->win32_pe.compress_exports = 0; - Export xport((char*)(unsigned char*) ibuf - isection[2].vaddr); - processExports(&xport); - processExports(&xport,ODADDR(PEDIR_EXPORT)); - omemcpy(obuf + ODADDR(PEDIR_EXPORT) - rvamin,oexport,soexport); -} - -void PackW32Pe::rebuildTls() -{ - // this is an easy one : just do nothing ;-) -} - -void PackW32Pe::rebuildResources(upx_byte *& extrainfo) -{ - if (ODSIZE(PEDIR_RESOURCE) == 0 || IDSIZE(PEDIR_RESOURCE) == 0) - return; - - icondir_count = get_le16(extrainfo); - extrainfo += 2; - - const unsigned vaddr = IDADDR(PEDIR_RESOURCE); - const upx_byte *r = ibuf - isection[2].vaddr; - Resource res(r + vaddr); - while (res.next()) - if (res.offs() > vaddr) - { - unsigned origoffs = get_le32(r + res.offs() - 4); - res.newoffs() = origoffs; - omemcpy(obuf + origoffs - rvamin,r + res.offs(),res.size()); - if (icondir_count && res.itype() == RT_GROUP_ICON) - { - set_le16(obuf + origoffs - rvamin + 4,icondir_count); - icondir_count = 0; - } - } - upx_byte *p = res.build(); - // write back when the original is zeroed - if (get_le32(obuf + ODADDR(PEDIR_RESOURCE) - rvamin + 12) == 0) - omemcpy(obuf + ODADDR(PEDIR_RESOURCE) - rvamin,p,res.dirsize()); - delete [] p; -} - -void PackW32Pe::unpack(OutputFile *fo) -{ - //infoHeader("[Processing %s, format %s, %d sections]", fn_basename(fi->getName()), getName(), objs); - - handleStub(fi,fo,pe_offset); - - const unsigned overlay = file_size - ALIGN_UP(isection[2].rawdataptr + isection[2].size,ih.filealign); - checkOverlay(overlay); - - ibuf.alloc(ph.c_len); - obuf.allocForUncompression(ph.u_len); - fi->seek(isection[1].rawdataptr - 64 + ph.buf_offset + ph.getPackHeaderSize(),SEEK_SET); - fi->readx(ibuf,ph.c_len); - - // decompress - decompress(ibuf,obuf); - upx_byte *extrainfo = obuf + get_le32(obuf + ph.u_len - 4); - //upx_byte * const eistart = extrainfo; - - memcpy(&oh, extrainfo, sizeof (oh)); - extrainfo += sizeof (oh); - unsigned objs = oh.objects; - - Array(pe_section_t, osection, objs); - memcpy(osection,extrainfo,sizeof(pe_section_t) * objs); - rvamin = osection[0].vaddr; - extrainfo += sizeof(pe_section_t) * objs; - - // read the noncompressed section - ibuf.dealloc(); - ibuf.alloc(isection[2].size); - fi->seek(isection[2].rawdataptr,SEEK_SET); - fi->readx(ibuf,isection[2].size); - - // unfilter - if (ph.filter) - { - Filter ft(ph.level); - ft.init(ph.filter,oh.codebase - rvamin); - ft.cto = (unsigned char) ph.filter_cto; - ft.unfilter(obuf + oh.codebase - rvamin, oh.codesize); - } - - rebuildImports(extrainfo); - rebuildRelocs(extrainfo); - rebuildTls(); - rebuildExports(); - rebuildResources(extrainfo); - - //FIXME: this does bad things if the relocation section got removed - // during compression ... - //memset(eistart,0,extrainfo - eistart + 4); - - // fill the data directory - ODADDR(PEDIR_DEBUG) = 0; - ODSIZE(PEDIR_DEBUG) = 0; - ODADDR(PEDIR_IAT) = 0; - ODSIZE(PEDIR_IAT) = 0; - ODADDR(PEDIR_BOUNDIM) = 0; - ODSIZE(PEDIR_BOUNDIM) = 0; - - // oh.headersize = osection[0].rawdataptr; - // oh.headersize = ALIGN_UP(pe_offset + sizeof(oh) + sizeof(pe_section_t) * objs, oh.filealign); - oh.headersize = rvamin; - oh.chksum = 0; - - // FIXME: ih.flags is checked here because of a bug in UPX 0.92 - if ((opt->win32_pe.strip_relocs && !isdll) || (ih.flags & RELOCS_STRIPPED)) - { - oh.flags |= RELOCS_STRIPPED; - ODADDR(PEDIR_RELOC) = 0; - ODSIZE(PEDIR_RELOC) = 0; - } - - // write decompressed file - if (fo) - { - unsigned ic; - for (ic = 0; ic < objs && osection[ic].rawdataptr == 0; ic++) - ; - - ibuf.dealloc(); - ibuf.alloc(osection[ic].rawdataptr); - ibuf.clear(); - infoHeader("[Writing uncompressed file]"); - - // write loader + compressed file - fo->write(&oh,sizeof(oh)); - fo->write(osection,objs * sizeof(pe_section_t)); - fo->write(ibuf,osection[ic].rawdataptr - fo->getBytesWritten()); - for (ic = 0; ic < objs; ic++) - if (osection[ic].rawdataptr) - fo->write(obuf + osection[ic].vaddr - rvamin,ALIGN_UP(osection[ic].size,oh.filealign)); - copyOverlay(fo, overlay, &obuf); - } - ibuf.dealloc(); -} - /* extra info added to help uncompression: diff --git a/src/p_w32pe.h b/src/p_w32pe.h index daa4d092..fa760490 100644 --- a/src/p_w32pe.h +++ b/src/p_w32pe.h @@ -29,227 +29,46 @@ #ifndef __UPX_P_W32PE_H #define __UPX_P_W32PE_H - -class PackW32Pe_Interval; -class PackW32Pe_Reloc; -class PackW32Pe_Resource; -class PackW32Pe_Export; - - /************************************************************************* // w32/pe **************************************************************************/ -class PackW32Pe : public Packer +class PackW32Pe : public PeFile { - typedef Packer super; + typedef PeFile super;; public: PackW32Pe(InputFile *f); ~PackW32Pe(); - virtual int getVersion() const { return 13; } virtual int getFormat() const { return UPX_F_WIN32_PE; } virtual const char *getName() const { return isrtm ? "rtm32/pe" : "win32/pe"; } virtual const int *getCompressionMethods(int method, int level) const; virtual const int *getFilters() const; virtual void pack(OutputFile *fo); - virtual void unpack(OutputFile *fo); virtual bool canPack(); virtual int canUnpack(); - // unpacker capabilities - virtual bool canUnpackVersion(int version) const - { return (version >= 12 && version <= 13); } - protected: virtual int readFileHeader(); - virtual bool testUnpackVersion(int version) const; virtual int buildLoader(const Filter *ft); virtual Linker* newLinker() const; - unsigned pe_offset; bool isrtm; - unsigned processImports(); - void processImports(unsigned); - void rebuildImports(upx_byte *&); - upx_byte *oimport; - unsigned soimport; - upx_byte *oimpdlls; - unsigned soimpdlls; + virtual unsigned processImports(); + virtual void processImports(unsigned, unsigned); + virtual void rebuildImports(upx_byte *&); - void processRelocs(); - void processRelocs(PackW32Pe_Reloc *); - void rebuildRelocs(upx_byte *&); - upx_byte *orelocs; - unsigned sorelocs; - upx_byte *oxrelocs; - unsigned soxrelocs; - - void processExports(PackW32Pe_Export *); - void processExports(PackW32Pe_Export *,unsigned); - void rebuildExports(); - upx_byte *oexport; - unsigned soexport; - - void processResources(PackW32Pe_Resource *); - void processResources(PackW32Pe_Resource *, unsigned); - void rebuildResources(upx_byte *&); - upx_byte *oresources; - unsigned soresources; - - void processTls(PackW32Pe_Interval *); - void processTls(PackW32Pe_Reloc *,const PackW32Pe_Interval *,unsigned); - void rebuildTls(); - upx_byte *otls; - unsigned sotls; - - unsigned stripDebug(unsigned); - - unsigned icondir_offset; - int icondir_count; - - bool importbyordinal; - bool kernel32ordinal; - unsigned tlsindex; - unsigned rvamin; - unsigned cimports; // rva of preprocessed imports - unsigned crelocs; // rva of preprocessed fixups - int big_relocs; - - void processLoadConf(PackW32Pe_Reloc *, const PackW32Pe_Interval *, unsigned); - void processLoadConf(PackW32Pe_Interval *); + void processLoadConf(Reloc *, const Interval *, unsigned); + void processLoadConf(Interval *); upx_byte *oloadconf; unsigned soloadconf; bool use_dep_hack; bool use_clear_dirty_stack; - - struct pe_header_t - { - // 0x0 - char _[4]; // pemagic - LE16 cpu; - LE16 objects; - char __[12]; // timestamp + reserved - LE16 opthdrsize; - LE16 flags; - // optional header - char ___[4]; // coffmagic + linkerversion - LE32 codesize; - // 0x20 - LE32 datasize; - LE32 bsssize; - LE32 entry; - LE32 codebase; - // 0x30 - LE32 database; - // nt specific fields - LE32 imagebase; - LE32 objectalign; - LE32 filealign; // should set to 0x200 ? - // 0x40 - char ____[16]; // versions - // 0x50 - LE32 imagesize; - LE32 headersize; - LE32 chksum; // should set to 0 - LE16 subsystem; - LE16 dllflags; - // 0x60 - char _____[20]; // stack + heap sizes - // 0x74 - LE32 ddirsentries; // usually 16 - - struct ddirs_t - { - LE32 vaddr; - LE32 size; - } - __attribute_packed; - - struct ddirs_t ddirs[16]; - } - __attribute_packed; - - struct pe_section_t - { - char name[8]; - LE32 vsize; - LE32 vaddr; - LE32 size; - LE32 rawdataptr; - char _[12]; - LE32 flags; - } - __attribute_packed; - - pe_header_t ih, oh; - pe_section_t *isection; - - static unsigned virta2objnum (unsigned, pe_section_t *, unsigned); - unsigned tryremove (unsigned, unsigned); - - enum { - PEDIR_EXPORT = 0, - PEDIR_IMPORT = 1, - PEDIR_RESOURCE = 2, - PEDIR_EXCEPTION = 3, // Exception table - PEDIR_SEC = 4, // Certificate table (file pointer) - PEDIR_RELOC = 5, - PEDIR_DEBUG = 6, - PEDIR_COPYRIGHT = 7, // Architecture-specific data - PEDIR_GLOBALPTR = 8, // Global pointer - PEDIR_TLS = 9, - PEDIR_LOADCONF = 10, // Load Config Table - PEDIR_BOUNDIM = 11, - PEDIR_IAT = 12, - PEDIR_DELAYIMP = 13, // Delay Import Descriptor - PEDIR_COMRT = 14 // Com+ Runtime Header - }; - - enum { - PEFL_CODE = 0x20, - PEFL_DATA = 0x40, - PEFL_BSS = 0x80, - PEFL_INFO = 0x200, - PEFL_EXTRELS = 0x01000000, // extended relocations - PEFL_DISCARD = 0x02000000, - PEFL_NOCACHE = 0x04000000, - PEFL_NOPAGE = 0x08000000, - PEFL_SHARED = 0x10000000, - PEFL_EXEC = 0x20000000, - PEFL_READ = 0x40000000, - PEFL_WRITE = 0x80000000 - }; - - enum { - RELOCS_STRIPPED = 0x0001, - EXECUTABLE = 0x0002, - LNUM_STRIPPED = 0x0004, - LSYMS_STRIPPED = 0x0008, - AGGRESSIVE_TRIM = 0x0010, - TWO_GIGS_AWARE = 0x0020, - FLITTLE_ENDIAN = 0x0080, - BITS_32_MACHINE = 0x0100, - DEBUG_STRIPPED = 0x0200, - REMOVABLE_SWAP = 0x0400, - SYSTEM_PROGRAM = 0x1000, - DLL_FLAG = 0x2000, - FBIG_ENDIAN = 0x8000 - }; - - // predefined resource types - enum { - RT_CURSOR = 1, RT_BITMAP, RT_ICON, RT_MENU, RT_DIALOG, RT_STRING, - RT_FONTDIR, RT_FONT, RT_ACCELERATOR, RT_RCDATA, RT_MESSAGETABLE, - RT_GROUP_CURSOR, RT_GROUP_ICON = 14, RT_VERSION = 16, RT_DLGINCLUDE, - RT_PLUGPLAY = 19, RT_VXD, RT_ANICURSOR, RT_ANIICON, RT_HTML, - RT_MANIFEST, RT_LAST - }; }; diff --git a/src/pefile.cpp b/src/pefile.cpp index cdfa7b81..546c72f6 100644 --- a/src/pefile.cpp +++ b/src/pefile.cpp @@ -205,7 +205,7 @@ int PeFile::readFileHeader() fi->readx(&ih,sizeof(ih)); fi->seek(0x200,SEEK_SET); fi->readx(&h,6); - return -1; // should be set overridden in the descendant class + return getFormat(); } diff --git a/src/pefile.h b/src/pefile.h index f800324c..8d4a71c7 100644 --- a/src/pefile.h +++ b/src/pefile.h @@ -50,7 +50,7 @@ protected: // unpacker capabilities virtual bool canUnpackVersion(int version) const - { return version == 13; } + { return (version >= 12 && version <= 13); } protected: virtual int readFileHeader();