1
0
mirror of https://github.com/upx/upx synced 2025-09-28 19:06:07 +08:00

watcom/le using nrv2e converted to ElfLinker

This commit is contained in:
László Molnár 2006-07-02 23:21:15 +02:00
parent fa9573436a
commit ed9fd3f0e0
5 changed files with 1125 additions and 678 deletions

View File

@ -32,6 +32,7 @@
#include "packer.h"
#include "lefile.h"
#include "p_wcle.h"
#include "linker.h"
static const
#include "stub/i386-dos32.watcom.le.h"
@ -81,15 +82,22 @@ const int *PackWcle::getFilters() const
}
Linker* PackWcle::newLinker() const
{
return new ElfLinkerX86;
}
int PackWcle::buildLoader(const Filter *ft)
{
// prepare loader
initLoader(nrv_loader,sizeof(nrv_loader));
addLoader("IDENTSTR,WCLEMAIN,UPX1HEAD,WCLECUTP,+0000000",
getDecompressorSections(),
"WCLEMAI2",
NULL
);
addLoader("IDENTSTR,WCLEMAIN,UPX1HEAD,WCLECUTP", NULL);
// fake alignment for the start of the decompressor
linker->defineSymbol("WCLECUTP", 0x1000);
addLoader(getDecompressorSections(), "WCLEMAI2", NULL);
if (ft->id)
{
assert(ft->calls > 0);
@ -134,6 +142,20 @@ bool PackWcle::canPack()
}
static bool defineFilterSymbols(Linker *linker, const Filter *ft)
{
if (ft->id == 0)
return false;
assert(ft->calls > 0);
linker->defineSymbol("filter_cto", ft->cto);
linker->defineSymbol("filter_length",
(ft->id & 0xf) % 3 == 0 ? ft->calls :
ft->lastcall - ft->calls * 4);
return true;
}
/*************************************************************************
//
**************************************************************************/
@ -497,16 +519,10 @@ void PackWcle::pack(OutputFile *fo)
neweip = getLoaderSection("WCLEMAIN");
int e_len = getLoaderSectionStart("WCLECUTP");
const unsigned d_len = lsize - e_len;
assert(e_len > 0);
assert(e_len > 0 && e_len < RESERVED);
getLoader();
memcpy(oimage,getLoader(),e_len);
memmove(oimage+e_len,oimage+RESERVED,soimage);
soimage = (soimage + e_len);
memcpy(oimage+soimage,getLoader() + e_len,d_len);
soimage += d_len;
soimage += lsize;
opages = (soimage+mps-1)/mps;
oh.bytes_on_last_page = soimage%mps;
@ -524,23 +540,29 @@ void PackWcle::pack(OutputFile *fo)
ic = (OOT(0,virtual_size) - d_len) &~ 15;
assert(ic > ((ph.u_len + ph.overlap_overhead + 31) &~ 15));
upx_byte * const p = oimage + soimage - d_len;
patch_le32(p,d_len,"JMPO",ih.init_eip_offset+text_vaddr-(ic+d_len));
patch_le32(p,d_len,"ESP0",ih.init_esp_offset+IOT(ih.init_ss_object-1,my_base_address));
if (patchFilter32(p, d_len, &ft) && text_vaddr)
patch_le32(p, d_len, "TEXV", text_vaddr);
patch_le32(p,d_len,"RELO",mps*pages);
linker->defineSymbol("WCLECUTP", ic);
patchDecompressor(p, d_len);
patchPackHeader(oimage,e_len);
linker->defineSymbol("original_entry", ih.init_eip_offset + text_vaddr);
linker->defineSymbol("original_stack", ih.init_esp_offset +
IOT(ih.init_ss_object - 1, my_base_address));
linker->defineSymbol("start_of_relocs", mps*pages);
defineFilterSymbols(linker, &ft);
linker->defineSymbol("filter_buffer_start", text_vaddr);
// FIXME patchDecompressor(loader, lsize);
unsigned jpos = find_le32(oimage,e_len,get_le32("JMPD"));
patch_le32(oimage,e_len,"JMPD",ic-jpos-4);
unsigned jpos = (((ph.c_len + 3) &~ 3) + d_len + 3) / 4;
linker->defineSymbol("words_to_copy", jpos);
linker->defineSymbol("copy_dest", ((ic + d_len + 3) &~ 3) - 4);
linker->defineSymbol("copy_source", e_len + jpos * 4 - 4);
jpos = (((ph.c_len+3)&~3) + d_len+3)/4;
patch_le32(oimage,e_len,"ECX0",jpos);
patch_le32(oimage,e_len,"EDI0",((ic+d_len+3)&~3)-4);
patch_le32(oimage,e_len,"ESI0",e_len+jpos*4-4);
linker->relocate();
MemBuffer loader(lsize);
memcpy(loader, getLoader(), lsize);
patchPackHeader(loader, lsize);
memcpy(oimage, loader, e_len);
memcpy(oimage + soimage - d_len, loader + e_len, d_len);
writeFile(fo, opt->watcom_le.le);

View File

@ -55,6 +55,7 @@ protected:
virtual void handleStub(OutputFile *fo);
virtual int buildLoader(const Filter *ft);
virtual Linker* newLinker() const;
virtual void readObjectTable();
virtual void encodeObjectTable();

View File

@ -347,9 +347,11 @@ i386-dos32.tmt.h : $(srcdir)/src/$$T.asm
i386-dos32.watcom.le% : tc_list = arch-i386 default
i386-dos32.watcom.le.h : $(srcdir)/src/$$T.asm
$(call tc,pp-nasm) --MMD=$@ $< -o tmp/$T.tmp1
$(call tc,app-nasm) tmp/$T.tmp1 tmp/$T.tmp2
$(call tc,nasm) -f bin -l tmp/$T.bin.lst tmp/$T.tmp2 -o tmp/$T.bin
$(call tc,gcc) -c -x assembler-with-cpp $< -o tmp/$T.bin
$(call tc,m-objcopy) --strip-unneeded tmp/$T.bin
$(call tc,m-objcopy) -R .text -R .data -R .bss tmp/$T.bin
$(call tc,m-objcopy) -R .note -R .comment tmp/$T.bin
$(call tc,m-objdump) -trwh tmp/$T.bin >> tmp/$T.bin
$(call tc,bin2h) --ident=nrv_loader tmp/$T.bin $@

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,4 @@
/*
; l_wcle.asm -- loader & decompressor for the watcom/le format
;
; This file is part of the UPX executable compressor.
@ -24,39 +25,31 @@
; Markus F.X.J. Oberhumer Laszlo Molnar
; <mfx@users.sourceforge.net> <ml1050@users.sourceforge.net>
;
*/
#include "arch/i386/macros2.ash"
%define jmps jmp short
%define jmpn jmp near
%include "arch/i386/macros.ash"
BITS 32
SECTION .text
ORG 0
CPU 386
; =============
; ============= ENTRY POINT
; =============
start:
; __WCLEMAIN__
mov edi, 'alib' ; address of obj#1:0 (filled by a fixup record)
section WCLEMAIN
.byte 0xbf // mov edi, 'alib'
.ascii "alib" // address of obj#1:0 (filled by a fixup record)
/*
; The following hack fools the lame protection of dos4g/w, which expects the
; 'WATCOM' string somewhere in the first 18 bytes after the entry point
; I use this imul thingy, because it's 1 byte shorter than a jump ;-)
; ... and "alibiWATCOM" looks cool
db 'iWATCOM' ; imul edx,[edi+0x41],'TCOM'
*/
.ascii "iWATCOM" // imul edx,[edi+0x41],'TCOM'
push es
push ds
pop es
push edi
lea esi, [edi + 'ESI0']
lea edi, [edi + 'EDI0']
mov ecx, 'ECX0'
lea esi, [edi + copy_source]
lea edi, [edi + copy_dest]
mov ecx, offset words_to_copy
std
rep
@ -65,80 +58,65 @@ start:
lea esi, [edi + 4]
pop edi
or ebp, byte -1
or ebp, -1
push edi
jmpn .1+'JMPD'
.1:
%include "include/header.ash"
jmp decompressor
cutpoint:
; __WCLECUTP__
#include "include/header2.ash"
; =============
; ============= DECOMPRESSION
; =============
section WCLECUTP
decompressor:
%include "arch/i386/nrv2b_d32.ash"
%include "arch/i386/nrv2d_d32.ash"
%include "arch/i386/nrv2e_d32.ash"
%include "arch/i386/lzma_d.ash"
// =============
// ============= DECOMPRESSION
// =============
; =============
//#include "arch/i386/nrv2b_d32.ash"
//#include "arch/i386/nrv2d_d32.ash"
#include "arch/i386/nrv2e_d32_2.ash"
//#include "arch/i386/lzma_d.ash"
; __WCLEMAI2__
// =============
section WCLEMAI2
pop ebp
push esi
lea esi, [ebp + 'RELO']
lea esi, [ebp + start_of_relocs]
push esi
; =============
; ============= CALLTRICK
; =============
// =============
// ============= CALLTRICK
// =============
%ifdef __WCALLTRI__
%ifdef __WCCTTPOS__
lea edi, [ebp + 'TEXV']
%else; __WCCTTNUL__
section WCCTTPOS
lea edi, [ebp + filter_buffer_start]
section WCCTTNUL
mov edi, ebp
%endif; __WCALLTR1__
section WCALLTR1
cjt32 ebp
%endif; __WCDUMMY1__
; =============
; ============= RELOCATION
; =============
// =============
// ============= RELOCATION
// =============
%ifdef __WCRELOC1__
section WCRELOC1
lea edi, [ebp - 4]
reloc32 esi, edi, ebp
; eax = 0
%endif; __WCDUMMY2__
// eax = 0
%ifdef __WCRELSEL__
call esi ; selector fixup code (modifies bx)
%endif; __WCLEMAI4__
; =============
section WCRELSEL
call esi // selector fixup code (modifies bx)
section WCLEMAI4
pop edi
pop ecx
sub ecx, edi
shr ecx, 2
rep
stosd ; clear dirty memory
stosd // clear dirty memory
pop es
lea esp, [ebp + 'ESP0']
lea esp, [ebp + original_stack]
jmpn .1+'JMPO'
.1:
jmp original_entry
; =============
eof:
; __WCTHEEND__
section .data
dd -1
dw eof
; vi:ts=8:et:nowrap
// vi:ts=8:et:nowrap