From 8e52894763d85d3d9bd0fd21b46e7dcff2ce5a94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Moln=C3=A1r?= Date: Wed, 19 Feb 2014 21:45:24 +0100 Subject: [PATCH] pe: loadconf handling moved to PeFile --- src/p_w32pe.cpp | 49 ------------------------------------------------- src/p_w32pe.h | 9 --------- src/pefile.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/pefile.h | 5 +++++ 4 files changed, 51 insertions(+), 58 deletions(-) diff --git a/src/p_w32pe.cpp b/src/p_w32pe.cpp index 16a5637b..f77e3b15 100644 --- a/src/p_w32pe.cpp +++ b/src/p_w32pe.cpp @@ -111,12 +111,9 @@ static void xcheck(size_t poff, size_t plen, const void *b, size_t blen) PackW32Pe::PackW32Pe(InputFile *f) : super(f) { - oloadconf = NULL; - soloadconf = 0; isrtm = false; use_dep_hack = true; use_clear_dirty_stack = true; - use_tls_callbacks = false; } @@ -162,52 +159,6 @@ int PackW32Pe::readFileHeader() return super::readFileHeader(); } -/************************************************************************* -// Load Configuration handling -**************************************************************************/ - -void PackW32Pe::processLoadConf(Interval *iv) // pass 1 -{ - if (IDSIZE(PEDIR_LOADCONF) == 0) - return; - - const unsigned lcaddr = IDADDR(PEDIR_LOADCONF); - const upx_byte * const loadconf = ibuf + lcaddr; - soloadconf = get_le32(loadconf); - if (soloadconf == 0) - return; - if (soloadconf > 256) - throwCantPack("size of Load Configuration directory unexpected"); - - // if there were relocation entries referring to the load config table - // then we need them for the copy of the table too - unsigned pos,type; - Reloc rel(ibuf + IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC)); - while (rel.next(pos, type)) - if (pos >= lcaddr && pos < lcaddr + soloadconf) - { - iv->add(pos - lcaddr, type); - // printf("loadconf reloc detected: %x\n", pos); - } - - oloadconf = new upx_byte[soloadconf]; - memcpy(oloadconf, loadconf, soloadconf); -} - -void PackW32Pe::processLoadConf(Reloc *rel, const Interval *iv, - unsigned newaddr) // pass2 -{ - // now we have the address of the new load config table - // so we can create the new relocation entries - for (unsigned ic = 0; ic < iv->ivnum; ic++) - { - rel->add(iv->ivarr[ic].start + newaddr, iv->ivarr[ic].len); - //printf("loadconf reloc added: %x %d\n", - // iv->ivarr[ic].start + newaddr, iv->ivarr[ic].len); - } -} - - /************************************************************************* // pack **************************************************************************/ diff --git a/src/p_w32pe.h b/src/p_w32pe.h index fde86910..8676a6e2 100644 --- a/src/p_w32pe.h +++ b/src/p_w32pe.h @@ -58,18 +58,9 @@ protected: virtual void buildLoader(const Filter *ft); virtual Linker* newLinker() const; - void processLoadConf(Reloc *, const Interval *, unsigned); - void processLoadConf(Interval *); - upx_byte *oloadconf; - unsigned soloadconf; - - unsigned tlscb_ptr; //NEW: TLS callback handling - Stefan Widmann - unsigned tls_handler_offset; - bool isrtm; bool use_dep_hack; bool use_clear_dirty_stack; - bool use_tls_callbacks; //NEW: TLS callback handling - Stefan Widmann }; diff --git a/src/pefile.cpp b/src/pefile.cpp index 64e8a6d0..721d4d7e 100644 --- a/src/pefile.cpp +++ b/src/pefile.cpp @@ -137,6 +137,8 @@ PeFile::PeFile(InputFile *f) : super(f) isdll = false; ilinker = NULL; use_tls_callbacks = false; + oloadconf = NULL; + soloadconf = 0; } @@ -1275,6 +1277,50 @@ void PeFile::processTls2(Reloc *rel,const Interval *iv,unsigned newaddr, } } +/************************************************************************* +// Load Configuration handling +**************************************************************************/ + +void PeFile::processLoadConf(Interval *iv) // pass 1 +{ + if (IDSIZE(PEDIR_LOADCONF) == 0) + return; + + const unsigned lcaddr = IDADDR(PEDIR_LOADCONF); + const upx_byte * const loadconf = ibuf + lcaddr; + soloadconf = get_le32(loadconf); + if (soloadconf == 0) + return; + if (soloadconf > 256) + throwCantPack("size of Load Configuration directory unexpected"); + + // if there were relocation entries referring to the load config table + // then we need them for the copy of the table too + unsigned pos,type; + Reloc rel(ibuf + IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC)); + while (rel.next(pos, type)) + if (pos >= lcaddr && pos < lcaddr + soloadconf) + { + iv->add(pos - lcaddr, type); + // printf("loadconf reloc detected: %x\n", pos); + } + + oloadconf = new upx_byte[soloadconf]; + memcpy(oloadconf, loadconf, soloadconf); +} + +void PeFile::processLoadConf(Reloc *rel, const Interval *iv, + unsigned newaddr) // pass2 +{ + // now we have the address of the new load config table + // so we can create the new relocation entries + for (unsigned ic = 0; ic < iv->ivnum; ic++) + { + rel->add(iv->ivarr[ic].start + newaddr, iv->ivarr[ic].len); + //printf("loadconf reloc added: %x %d\n", + // iv->ivarr[ic].start + newaddr, iv->ivarr[ic].len); + } +} /************************************************************************* // resource handling diff --git a/src/pefile.h b/src/pefile.h index ea6f9ee1..8045c353 100644 --- a/src/pefile.h +++ b/src/pefile.h @@ -116,6 +116,11 @@ protected: unsigned tls_handler_offset; bool use_tls_callbacks; + void processLoadConf(Reloc *, const Interval *, unsigned); + void processLoadConf(Interval *); + upx_byte *oloadconf; + unsigned soloadconf; + unsigned stripDebug(unsigned); unsigned icondir_offset;