?? packer.cpp
字號:
void Packer::checkPatch(void *b, int blen, int boff, int size){ if (b == NULL && blen == 0 && boff == 0 && size == 0) { // reset last_patch = NULL; last_patch_len = 0; last_patch_off = 0; return; if (b == NULL || blen <= 0 || boff < 0 || size <= 0) throwBadLoader(); if (boff + size <= 0 || boff + size > blen) throwBadLoader(); //printf("checkPatch: %p %5d %5d %2d\n", b, blen, boff, size); if (b == last_patch) { if (boff + size > last_patch_off) throwInternalError("invalid patch order"); // The next check is not strictly necessary, but the buffer // length should better not increase... if (blen > last_patch_len) throwInternalError("invalid patch order (length)"); } else last_patch = b; last_patch_len = blen; last_patch_off = boff;}int Packer::patch_be16(void *b, int blen, unsigned old, unsigned new_){ int boff = find_be16(b, blen, old); checkPatch(b, blen, boff, 2); unsigned char *p = (unsigned char *)b + boff; set_be16(p, new_); return boff;}int Packer::patch_be16(void *b, int blen, const void *old, unsigned new_){ int boff = find(b, blen, old, 2); checkPatch(b, blen, boff, 2); unsigned char *p = (unsigned char *)b + boff; set_be16(p, new_); return boff;}int Packer::patch_be32(void *b, int blen, unsigned old, unsigned new_){ int boff = find_be32(b, blen, old); checkPatch(b, blen, boff, 4); unsigned char *p = (unsigned char *)b + boff; set_be32(p, new_); return boff;}int Packer::patch_be32(void *b, int blen, const void *old, unsigned new_){ int boff = find(b, blen, old, 4); checkPatch(b, blen, boff, 4); unsigned char *p = (unsigned char *)b + boff; set_be32(p, new_); return boff;}int Packer::patch_le16(void *b, int blen, unsigned old, unsigned new_){ int boff = find_le16(b, blen, old); checkPatch(b, blen, boff, 2); unsigned char *p = (unsigned char *)b + boff; set_le16(p, new_); return boff;}int Packer::patch_le16(void *b, int blen, const void *old, unsigned new_){ int boff = find(b, blen, old, 2); checkPatch(b, blen, boff, 2); unsigned char *p = (unsigned char *)b + boff; set_le16(p, new_); return boff;}int Packer::patch_le32(void *b, int blen, unsigned old, unsigned new_){ int boff = find_le32(b, blen, old); checkPatch(b, blen, boff, 4); unsigned char *p = (unsigned char *)b + boff; set_le32(p, new_); return boff;}int Packer::patch_le32(void *b, int blen, const void *old, unsigned new_){ int boff = find(b, blen, old, 4); checkPatch(b, blen, boff, 4); unsigned char *p = (unsigned char *)b + boff; set_le32(p, new_); return boff;}/*************************************************************************// relocation util**************************************************************************/upx_byte *Packer::optimizeReloc32(upx_byte *in, unsigned relocnum, upx_byte *out, upx_byte *image, int bswap, int *big){ if (opt->exact) throwCantPackExact(); *big = 0; if (relocnum == 0) return out; qsort(in,relocnum,4,le32_compare); unsigned jc,pc,oc; upx_byte *fix = out; pc = (unsigned) -4; for (jc = 0; jc<relocnum; jc++) { oc = get_le32(in+jc*4) - pc; if (oc == 0) continue; else if ((int)oc < 4) throwCantPack("overlapping fixups"); else if (oc < 0xF0) *fix++ = (unsigned char) oc; else if (oc < 0x100000) { *fix++ = (unsigned char) (0xF0+(oc>>16)); *fix++ = (unsigned char) oc; *fix++ = (unsigned char) (oc>>8); } else { *big = 1; *fix++ = 0xf0; *fix++ = 0; *fix++ = 0; set_le32(fix,oc); fix += 4; } pc += oc; if (bswap) acc_ua_swab32s(image + pc); } *fix++ = 0; return fix;}unsigned Packer::unoptimizeReloc32(upx_byte **in, upx_byte *image, MemBuffer *out, int bswap){ upx_byte *p; unsigned relocn = 0; for (p = *in; *p; p++, relocn++) if (*p >= 0xF0) { if (*p == 0xF0 && get_le16(p+1) == 0) p += 4; p += 2; } //fprintf(stderr,"relocnum=%x\n",relocn); out->alloc(4*relocn+4); // one extra data LE32 *outp = (LE32*) (unsigned char *) *out; LE32 *relocs = outp; unsigned jc = (unsigned) -4; for (p = *in; *p; p++) { if (*p < 0xF0) jc += *p; else { unsigned dif = (*p & 0x0F)*0x10000 + get_le16(p+1); p += 2; if (dif == 0) { dif = get_le32(p+1); p += 4; } jc += dif; } *relocs++ = jc; if (bswap && image) acc_ua_swab32s(image + jc); } //fprintf(stderr,"relocnum=%x\n",relocn); *in = p+1; return (unsigned) (relocs - outp);}/*************************************************************************// loader util (interface to linker)**************************************************************************/static const char *getIdentstr(unsigned *size, int small){ static char identbig[] = "\n\0" "$Info: " "This file is packed with the UPX executable packer http://upx.sf.net $" "\n\0" "$Id: UPX " UPX_VERSION_STRING4 " Copyright (C) 1996-" UPX_VERSION_YEAR " the UPX Team. All Rights Reserved. $" "\n"; static char identsmall[] = "\n" "$Id: UPX " "(C) 1996-" UPX_VERSION_YEAR " the UPX Team. All Rights Reserved. http://upx.sf.net $" "\n"; static char identtiny[] = UPX_VERSION_STRING4; static int done; if (!done && (opt->debug.fake_stub_version[0] || opt->debug.fake_stub_year[0])) { struct strinfo_t { char *s; int size; }; static const strinfo_t strlist[] = { { identbig, (int)sizeof(identbig) }, { identsmall, (int)sizeof(identsmall) }, { identtiny, (int)sizeof(identtiny) }, { NULL, 0 } }; const strinfo_t* iter; for (iter = strlist; iter->s; ++iter) { if (opt->debug.fake_stub_version[0]) mem_replace(iter->s, iter->size, UPX_VERSION_STRING4, 4, opt->debug.fake_stub_version); if (opt->debug.fake_stub_year[0]) mem_replace(iter->s, iter->size, UPX_VERSION_YEAR, 4, opt->debug.fake_stub_year); } done = 1; } if (small < 0) small = opt->small; if (small >= 2) { *size = sizeof(identtiny); return identtiny; } else if (small >= 1) { *size = sizeof(identsmall); return identsmall; } else { *size = sizeof(identbig); return identbig; }}void Packer::initLoader(const void *pdata, int plen, int small){ delete linker; linker = newLinker(); assert(bele == linker->bele); linker->init(pdata, plen); unsigned size; char const * const ident = getIdentstr(&size, small); linker->addSection("IDENTSTR", ident, size, 0);}#define C const char *void Packer::addLoader(C a){ addLoaderVA(a, NULL); }void Packer::addLoader(C a, C b){ addLoaderVA(a, b, NULL); }void Packer::addLoader(C a, C b, C c){ addLoaderVA(a, b, c, NULL); }void Packer::addLoader(C a, C b, C c, C d){ addLoaderVA(a, b, c, d, NULL); }void Packer::addLoader(C a, C b, C c, C d, C e){ addLoaderVA(a, b, c, d, e, NULL); }void Packer::addLoader(C a, C b, C c, C d, C e, C f){ addLoaderVA(a, b, c, d, e, f, NULL); }void Packer::addLoader(C a, C b, C c, C d, C e, C f, C g){ addLoaderVA(a, b, c, d, e, f, g, NULL); }void Packer::addLoader(C a, C b, C c, C d, C e, C f, C g, C h){ addLoaderVA(a, b, c, d, e, f, g, h, NULL); }void Packer::addLoader(C a, C b, C c, C d, C e, C f, C g, C h, C i){ addLoaderVA(a, b, c, d, e, f, g, h, i, NULL); }void Packer::addLoader(C a, C b, C c, C d, C e, C f, C g, C h, C i, C j){ addLoaderVA(a, b, c, d, e, f, g, h, i, j, NULL); }#undef Cvoid __acc_cdecl_va Packer::addLoaderVA(const char *s, ...){ va_list ap; va_start(ap, s); linker->addLoader(s, ap); va_end(ap);}upx_byte *Packer::getLoader() const{ int size = -1; upx_byte *oloader = linker->getLoader(&size); if (oloader == NULL || size <= 0) throwBadLoader(); return oloader;}int Packer::getLoaderSize() const{ int size = -1; upx_byte *oloader = linker->getLoader(&size); if (oloader == NULL || size <= 0) throwBadLoader(); return size;}int Packer::getLoaderSection(const char *name, int *slen) const{ int size = -1; int ostart = linker->getSection(name, &size); if (ostart < 0 || size <= 0) throwBadLoader(); if (slen) *slen = size; return ostart;}// same, but the size of the section may be == 0int Packer::getLoaderSectionStart(const char *name, int *slen) const{ int size = -1; int ostart = linker->getSection(name, &size); if (ostart < 0 || size < 0) if (slen) *slen = size; return ostart;}void Packer::relocateLoader(){ linker->relocate();#if 0 // "relocate" packheader if (linker->findSection("UPX1HEAD", false)) { int lsize = -1; int loff = getLoaderSectionStart("UPX1HEAD", &lsize); assert(lsize == ph.getPackHeaderSize()); unsigned char *p = getLoader() + loff; assert(get_le32(p) == UPX_MAGIC_LE32);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -