mirror of
https://github.com/upx/upx
synced 2025-09-28 19:06:07 +08:00
Introduce Packer::relocateLoader(). This is the first step of finally getting
rid of all Packer::patchXXX() methods.
This commit is contained in:
parent
141c703ed5
commit
a6cb6f57d3
|
@ -36,6 +36,7 @@
|
||||||
|
|
||||||
class ElfLinker : private nocopy
|
class ElfLinker : private nocopy
|
||||||
{
|
{
|
||||||
|
friend class Packer;
|
||||||
public:
|
public:
|
||||||
const N_BELE_RTP::AbstractPolicy *bele;
|
const N_BELE_RTP::AbstractPolicy *bele;
|
||||||
protected:
|
protected:
|
||||||
|
@ -86,7 +87,6 @@ public:
|
||||||
virtual int getSection(const char *sname, int *slen=NULL) const;
|
virtual int getSection(const char *sname, int *slen=NULL) const;
|
||||||
virtual int getSectionSize(const char *sname) const;
|
virtual int getSectionSize(const char *sname) const;
|
||||||
virtual upx_byte *getLoader(int *llen=NULL) const;
|
virtual upx_byte *getLoader(int *llen=NULL) const;
|
||||||
virtual void relocate();
|
|
||||||
virtual void defineSymbol(const char *name, unsigned value);
|
virtual void defineSymbol(const char *name, unsigned value);
|
||||||
virtual unsigned getSymbolOffset(const char *) const;
|
virtual unsigned getSymbolOffset(const char *) const;
|
||||||
|
|
||||||
|
@ -97,6 +97,8 @@ public:
|
||||||
virtual void alignCode(unsigned len) { alignWithByte(len, 0); }
|
virtual void alignCode(unsigned len) { alignWithByte(len, 0); }
|
||||||
virtual void alignData(unsigned len) { alignWithByte(len, 0); }
|
virtual void alignData(unsigned len) { alignWithByte(len, 0); }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void relocate();
|
||||||
virtual void relocate1(const Relocation *, upx_byte *location,
|
virtual void relocate1(const Relocation *, upx_byte *location,
|
||||||
unsigned value, const char *type);
|
unsigned value, const char *type);
|
||||||
};
|
};
|
||||||
|
|
|
@ -833,7 +833,7 @@ void PackArmPe::pack(OutputFile *fo)
|
||||||
linker->defineSymbol("compressed_length", ph.c_len);
|
linker->defineSymbol("compressed_length", ph.c_len);
|
||||||
linker->defineSymbol("start_of_compressed", ih.imagebase + s1addr + identsize - identsplit);
|
linker->defineSymbol("start_of_compressed", ih.imagebase + s1addr + identsize - identsplit);
|
||||||
defineDecompressorSymbols();
|
defineDecompressorSymbols();
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
|
|
||||||
MemBuffer loader(lsize);
|
MemBuffer loader(lsize);
|
||||||
memcpy(loader, getLoader(), lsize);
|
memcpy(loader, getLoader(), lsize);
|
||||||
|
|
|
@ -119,7 +119,7 @@ void PackCom::patchLoader(OutputFile *fo,
|
||||||
linker->defineSymbol("neg_e_len", 0 - e_len);
|
linker->defineSymbol("neg_e_len", 0 - e_len);
|
||||||
linker->defineSymbol("NRV2B160", ph.u_len + ph.overlap_overhead);
|
linker->defineSymbol("NRV2B160", ph.u_len + ph.overlap_overhead);
|
||||||
|
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
loader = getLoader();
|
loader = getLoader();
|
||||||
|
|
||||||
// some day we could use the relocation stuff for patchPackHeader too
|
// some day we could use the relocation stuff for patchPackHeader too
|
||||||
|
|
|
@ -368,7 +368,7 @@ void PackDjgpp2::pack(OutputFile *fo)
|
||||||
data->size = ALIGN_UP(data->size, 4);
|
data->size = ALIGN_UP(data->size, 4);
|
||||||
|
|
||||||
linker->defineSymbol("DJ2MAIN1", coff_hdr.a_entry);
|
linker->defineSymbol("DJ2MAIN1", coff_hdr.a_entry);
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
|
|
||||||
// prepare loader
|
// prepare loader
|
||||||
MemBuffer loader(lsize);
|
MemBuffer loader(lsize);
|
||||||
|
|
|
@ -541,7 +541,7 @@ void PackExe::pack(OutputFile *fo)
|
||||||
|
|
||||||
oh.ip = device_driver ? getLoaderSection("EXEENTRY") - 2 : 0;
|
oh.ip = device_driver ? getLoaderSection("EXEENTRY") - 2 : 0;
|
||||||
|
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
memcpy(loader, getLoader(), lsize);
|
memcpy(loader, getLoader(), lsize);
|
||||||
patchPackHeader(loader,e_len);
|
patchPackHeader(loader,e_len);
|
||||||
|
|
||||||
|
|
|
@ -489,7 +489,7 @@ PackLinuxElf32::buildLinuxLoader(
|
||||||
addStubEntrySections(ft);
|
addStubEntrySections(ft);
|
||||||
|
|
||||||
defineSymbols(ft);
|
defineSymbols(ft);
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -537,7 +537,7 @@ PackLinuxElf64::buildLinuxLoader(
|
||||||
addStubEntrySections(ft);
|
addStubEntrySections(ft);
|
||||||
|
|
||||||
defineSymbols(ft);
|
defineSymbols(ft);
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -395,7 +395,7 @@ PackLinuxI386::buildLinuxLoader(
|
||||||
linker->defineSymbol("NMRU", mru - is_pwr2);
|
linker->defineSymbol("NMRU", mru - is_pwr2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -132,7 +132,7 @@ PackMachPPC32::buildMachLoader(
|
||||||
addStubEntrySections(ft);
|
addStubEntrySections(ft);
|
||||||
|
|
||||||
defineSymbols(ft);
|
defineSymbols(ft);
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -516,7 +516,7 @@ void PackPs1::pack(OutputFile *fo)
|
||||||
linker->defineSymbol("entry", ih.epc);
|
linker->defineSymbol("entry", ih.epc);
|
||||||
linker->defineSymbol("SC",
|
linker->defineSymbol("SC",
|
||||||
sa_cnt > (0x10000 << 2) ? sa_cnt >> 5 : sa_cnt >> 2);
|
sa_cnt > (0x10000 << 2) ? sa_cnt >> 5 : sa_cnt >> 2);
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
|
|
||||||
buildPart2 = true;
|
buildPart2 = true;
|
||||||
buildLoader(&ft);
|
buildLoader(&ft);
|
||||||
|
@ -614,7 +614,7 @@ void PackPs1::pack(OutputFile *fo)
|
||||||
- getLoaderSectionStart("cdb.entry.lzma"));
|
- getLoaderSectionStart("cdb.entry.lzma"));
|
||||||
}
|
}
|
||||||
|
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
//linker->dumpSymbols();
|
//linker->dumpSymbols();
|
||||||
MemBuffer loader(lsize);
|
MemBuffer loader(lsize);
|
||||||
assert(lsize == getLoaderSize());
|
assert(lsize == getLoaderSize());
|
||||||
|
|
|
@ -89,7 +89,7 @@ void PackSys::patchLoader(OutputFile *fo,
|
||||||
linker->defineSymbol("NRV2B160", ph.u_len + ph.overlap_overhead + 1);
|
linker->defineSymbol("NRV2B160", ph.u_len + ph.overlap_overhead + 1);
|
||||||
linker->defineSymbol("original_strategy", get_le16(ibuf + 6));
|
linker->defineSymbol("original_strategy", get_le16(ibuf + 6));
|
||||||
|
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
loader = getLoader();
|
loader = getLoader();
|
||||||
|
|
||||||
patchPackHeader(loader,e_len);
|
patchPackHeader(loader,e_len);
|
||||||
|
|
|
@ -269,7 +269,7 @@ void PackTmt::pack(OutputFile *fo)
|
||||||
// e_len,d_len,copy_len,copy_to,ph.overlap_overhead,jmp_pos,ph.u_len,ph.c_len);
|
// e_len,d_len,copy_len,copy_to,ph.overlap_overhead,jmp_pos,ph.u_len,ph.c_len);
|
||||||
|
|
||||||
linker->defineSymbol("TMTCUTPO", ph.u_len + ph.overlap_overhead);
|
linker->defineSymbol("TMTCUTPO", ph.u_len + ph.overlap_overhead);
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
|
|
||||||
MemBuffer loader(lsize);
|
MemBuffer loader(lsize);
|
||||||
memcpy(loader,getLoader(),lsize);
|
memcpy(loader,getLoader(),lsize);
|
||||||
|
|
|
@ -657,7 +657,7 @@ void PackTos::pack(OutputFile *fo)
|
||||||
linker->defineSymbol("copy_to_stack_len", symbols.copy_to_stack_len);
|
linker->defineSymbol("copy_to_stack_len", symbols.copy_to_stack_len);
|
||||||
linker->defineSymbol("clear_dirty_stack_len", symbols.clear_dirty_stack_len);
|
linker->defineSymbol("clear_dirty_stack_len", symbols.clear_dirty_stack_len);
|
||||||
|
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
|
|
||||||
//
|
//
|
||||||
// write
|
// write
|
||||||
|
|
|
@ -318,7 +318,7 @@ void PackVmlinuxI386::pack(OutputFile *fo)
|
||||||
linker->defineSymbol("filter_length", ph.u_len); // redefine
|
linker->defineSymbol("filter_length", ph.u_len); // redefine
|
||||||
}
|
}
|
||||||
defineDecompressorSymbols();
|
defineDecompressorSymbols();
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
|
|
||||||
MemBuffer loader(lsize);
|
MemBuffer loader(lsize);
|
||||||
memcpy(loader, getLoader(), lsize);
|
memcpy(loader, getLoader(), lsize);
|
||||||
|
|
|
@ -325,7 +325,7 @@ void PackVmlinuzI386::pack(OutputFile *fo)
|
||||||
linker->defineSymbol("src_for_decompressor", zimage_offset + lsize);
|
linker->defineSymbol("src_for_decompressor", zimage_offset + lsize);
|
||||||
linker->defineSymbol("original_entry", physical_start);
|
linker->defineSymbol("original_entry", physical_start);
|
||||||
linker->defineSymbol("stack_offset", stack_offset_during_uncompression);
|
linker->defineSymbol("stack_offset", stack_offset_during_uncompression);
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
|
|
||||||
MemBuffer loader(lsize);
|
MemBuffer loader(lsize);
|
||||||
memcpy(loader, getLoader(), lsize);
|
memcpy(loader, getLoader(), lsize);
|
||||||
|
@ -448,7 +448,7 @@ void PackBvmlinuzI386::pack(OutputFile *fo)
|
||||||
defineDecompressorSymbols();
|
defineDecompressorSymbols();
|
||||||
linker->defineSymbol("original_entry", physical_start);
|
linker->defineSymbol("original_entry", physical_start);
|
||||||
linker->defineSymbol("stack_offset", stack_offset_during_uncompression);
|
linker->defineSymbol("stack_offset", stack_offset_during_uncompression);
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
|
|
||||||
MemBuffer loader(lsize);
|
MemBuffer loader(lsize);
|
||||||
memcpy(loader, getLoader(), lsize);
|
memcpy(loader, getLoader(), lsize);
|
||||||
|
|
|
@ -915,7 +915,7 @@ void PackW32Pe::pack(OutputFile *fo)
|
||||||
linker->defineSymbol("start_of_compressed", esi0 + ih.imagebase);
|
linker->defineSymbol("start_of_compressed", esi0 + ih.imagebase);
|
||||||
|
|
||||||
linker->defineSymbol(isdll ? "PEISDLL1" : "PEMAIN01", upxsection);
|
linker->defineSymbol(isdll ? "PEISDLL1" : "PEMAIN01", upxsection);
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
|
|
||||||
const unsigned lsize = getLoaderSize();
|
const unsigned lsize = getLoaderSize();
|
||||||
MemBuffer loader(lsize);
|
MemBuffer loader(lsize);
|
||||||
|
|
|
@ -555,7 +555,7 @@ void PackWcle::pack(OutputFile *fo)
|
||||||
linker->defineSymbol("copy_dest", ((ic + d_len + 3) &~ 3) - 4);
|
linker->defineSymbol("copy_dest", ((ic + d_len + 3) &~ 3) - 4);
|
||||||
linker->defineSymbol("copy_source", e_len + jpos * 4 - 4);
|
linker->defineSymbol("copy_source", e_len + jpos * 4 - 4);
|
||||||
|
|
||||||
linker->relocate();
|
relocateLoader();
|
||||||
|
|
||||||
MemBuffer loader(lsize);
|
MemBuffer loader(lsize);
|
||||||
memcpy(loader, getLoader(), lsize);
|
memcpy(loader, getLoader(), lsize);
|
||||||
|
|
|
@ -642,11 +642,15 @@ void Packer::updatePackHeader()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// FIXME: remove patchPackHeader() and fold into relocateLoader();
|
||||||
|
// then make linker->relocate() private (friend Packer)
|
||||||
int Packer::patchPackHeader(void *b, int blen)
|
int Packer::patchPackHeader(void *b, int blen)
|
||||||
{
|
{
|
||||||
const int size = ph.getPackHeaderSize();
|
|
||||||
assert(isValidFilter(ph.filter));
|
assert(isValidFilter(ph.filter));
|
||||||
|
|
||||||
|
const int size = ph.getPackHeaderSize();
|
||||||
|
if (linker->findSection("UPX1HEAD", false))
|
||||||
|
assert(size == linker->getSectionSize("UPX1HEAD"));
|
||||||
int boff = find_le32(b, blen, UPX_MAGIC_LE32);
|
int boff = find_le32(b, blen, UPX_MAGIC_LE32);
|
||||||
checkPatch(b, blen, boff, size);
|
checkPatch(b, blen, boff, size);
|
||||||
|
|
||||||
|
@ -1089,6 +1093,26 @@ int Packer::getLoaderSectionStart(const char *name, int *slen) const
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
//patchPackHeader(p, lsize);
|
||||||
|
ph.putPackHeader(p);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
// Try compression with several methods and filters, choose the best
|
// Try compression with several methods and filters, choose the best
|
||||||
/ or first working one. Needs buildLoader().
|
/ or first working one. Needs buildLoader().
|
||||||
|
|
|
@ -207,6 +207,7 @@ protected:
|
||||||
// loader core
|
// loader core
|
||||||
virtual void buildLoader(const Filter *ft) = 0;
|
virtual void buildLoader(const Filter *ft) = 0;
|
||||||
virtual Linker* newLinker() const = 0;
|
virtual Linker* newLinker() const = 0;
|
||||||
|
virtual void relocateLoader();
|
||||||
// loader util for linker
|
// loader util for linker
|
||||||
virtual upx_byte *getLoader() const;
|
virtual upx_byte *getLoader() const;
|
||||||
virtual int getLoaderSize() const;
|
virtual int getLoaderSize() const;
|
||||||
|
|
|
@ -55,7 +55,6 @@
|
||||||
#include "p_ps1.h"
|
#include "p_ps1.h"
|
||||||
#include "p_mach.h"
|
#include "p_mach.h"
|
||||||
#include "p_armpe.h"
|
#include "p_armpe.h"
|
||||||
#include "linker.h"
|
|
||||||
|
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
|
|
Loading…
Reference in New Issue
Block a user