PE: Add position independent decompressor stub for i386

This commit is contained in:
Kornel Pal
2021-12-22 02:58:09 +01:00
committed by Markus F.X.J. Oberhumer
parent 7f1f29b0d8
commit 76ddf6b8da
5 changed files with 21 additions and 6 deletions
+9 -5
View File
@@ -110,14 +110,17 @@ void PackW32Pe::buildLoader(const Filter *ft)
// prepare loader // prepare loader
initLoader(stub_i386_win32_pe, sizeof(stub_i386_win32_pe), 2); initLoader(stub_i386_win32_pe, sizeof(stub_i386_win32_pe), 2);
addLoader(isdll ? "PEISDLL1" : "", if (isdll)
"PEMAIN01", addLoader("PEISDLL1");
addLoader("PEMAIN01",
use_stub_relocs ? "PESOCREL" : "PESOCPIC",
"PESOUNC0",
icondir_count > 1 ? (icondir_count == 2 ? "PEICONS1" : "PEICONS2") : "", icondir_count > 1 ? (icondir_count == 2 ? "PEICONS1" : "PEICONS2") : "",
tmp_tlsindex ? "PETLSHAK" : "", tmp_tlsindex ? "PETLSHAK" : "",
"PEMAIN02", "PEMAIN02",
ph.first_offset_found == 1 ? "PEMAIN03" : "", ph.first_offset_found == 1 ? "PEMAIN03" : "",
getDecompressorSections(), getDecompressorSections(),
/*multipass ? "PEMULTIP" : */ "", //multipass ? "PEMULTIP" : "",
"PEMAIN10", "PEMAIN10",
nullptr nullptr
); );
@@ -255,7 +258,7 @@ void PackW32Pe::defineSymbols(unsigned ncsection, unsigned upxsection,
const unsigned esi0 = s1addr + ic; const unsigned esi0 = s1addr + ic;
linker->defineSymbol("start_of_uncompressed", 0u - esi0 + rvamin); linker->defineSymbol("start_of_uncompressed", 0u - esi0 + rvamin);
linker->defineSymbol("start_of_compressed", esi0 + ih.imagebase); linker->defineSymbol("start_of_compressed", use_stub_relocs ? esi0 + ih.imagebase : esi0);
if (use_tls_callbacks) if (use_tls_callbacks)
{ {
@@ -270,7 +273,8 @@ void PackW32Pe::defineSymbols(unsigned ncsection, unsigned upxsection,
void PackW32Pe::addNewRelocations(Reloc &rel, unsigned base) void PackW32Pe::addNewRelocations(Reloc &rel, unsigned base)
{ {
rel.add(base + linker->getSymbolOffset("PEMAIN01") + 2, 3); if (use_stub_relocs)
rel.add(base + linker->getSymbolOffset("PESOCREL") + 1, 3);
} }
void PackW32Pe::setOhDataBase(const pe_section_t *osection) void PackW32Pe::setOhDataBase(const pe_section_t *osection)
+3 -1
View File
@@ -47,7 +47,9 @@ static const
**************************************************************************/ **************************************************************************/
PackW64Pep::PackW64Pep(InputFile *f) : super(f) PackW64Pep::PackW64Pep(InputFile *f) : super(f)
{} {
use_stub_relocs = false;
}
PackW64Pep::~PackW64Pep() PackW64Pep::~PackW64Pep()
+1
View File
@@ -117,6 +117,7 @@ PeFile::PeFile(InputFile *f) : super(f)
use_dep_hack = true; use_dep_hack = true;
use_clear_dirty_stack = true; use_clear_dirty_stack = true;
use_stub_relocs = true;
isrtm = false; isrtm = false;
} }
+1
View File
@@ -214,6 +214,7 @@ protected:
bool isrtm; bool isrtm;
bool use_dep_hack; bool use_dep_hack;
bool use_clear_dirty_stack; bool use_clear_dirty_stack;
bool use_stub_relocs;
static unsigned virta2objnum (unsigned, pe_section_t *, unsigned); static unsigned virta2objnum (unsigned, pe_section_t *, unsigned);
+7
View File
@@ -40,7 +40,14 @@ section PEISDLL1
jnz reloc_end_jmp jnz reloc_end_jmp
section PEMAIN01 section PEMAIN01
pusha pusha
section PESOCREL
mov esi, offset start_of_compressed // relocated mov esi, offset start_of_compressed // relocated
section PESOCPIC
call get_eip
get_eip:
pop eax
lea esi, [eax + start_of_compressed - get_eip]
section PESOUNC0
lea edi, [esi + start_of_uncompressed] lea edi, [esi + start_of_uncompressed]
section PEICONS1 section PEICONS1
incw [edi + icon_offset] incw [edi + icon_offset]