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

tmp/adam using nrv2e converted to ElfLinker

This commit is contained in:
László Molnár 2006-07-02 22:39:53 +02:00
parent 6d2058c3b7
commit fa9573436a
5 changed files with 1061 additions and 646 deletions

View File

@ -31,6 +31,7 @@
#include "filter.h"
#include "packer.h"
#include "p_tmt.h"
#include "linker.h"
static const
#include "stub/i386-dos32.tmt.h"
@ -74,17 +75,25 @@ unsigned PackTmt::findOverlapOverhead(const upx_bytep buf,
}
Linker* PackTmt::newLinker() const
{
return new ElfLinkerX86;
}
int PackTmt::buildLoader(const Filter *ft)
{
// prepare loader
initLoader(nrv_loader,sizeof(nrv_loader));
addLoader("IDENTSTR,TMTMAIN1",
ft->id ? "TMTCALT1" : "",
"TMTMAIN2,UPX1HEAD,TMTCUTPO,+0XXXXXX",
getDecompressorSections(),
"TMTMAIN5",
NULL
);
"TMTMAIN2,UPX1HEAD,TMTCUTPO",
NULL);
// fake alignment for the start of the decompressor
linker->defineSymbol("TMTCUTPO", 0x1000);
addLoader(getDecompressorSections(), "TMTMAIN5", NULL);
if (ft->id)
{
assert(ft->calls > 0);
@ -176,6 +185,19 @@ bool PackTmt::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;
}
/*************************************************************************
//
**************************************************************************/
@ -231,31 +253,31 @@ void PackTmt::pack(OutputFile *fo)
compressWithFilters(&ft, 512);
const unsigned lsize = getLoaderSize();
MemBuffer loader(lsize);
memcpy(loader,getLoader(),lsize);
const unsigned s_point = getLoaderSection("TMTMAIN1");
int e_len = getLoaderSectionStart("TMTCUTPO");
const unsigned d_len = lsize - e_len;
assert(e_len > 0 && s_point > 0);
// patch loader
patch_le32(loader,lsize,"JMPO",ih.entry-(ph.u_len+ph.overlap_overhead+d_len));
patchFilter32(loader, lsize, &ft);
patchDecompressor(loader, lsize);
patchPackHeader(loader,e_len);
linker->defineSymbol("original_entry", ih.entry);
defineFilterSymbols(linker, &ft);
// FIXME patchDecompressor(loader, lsize);
const unsigned jmp_pos = find_le32(loader,e_len,get_le32("JMPD"));
patch_le32(loader,e_len,"JMPD",ph.u_len+ph.overlap_overhead-jmp_pos-4);
patch_le32(loader,e_len,"ECX0",ph.c_len+d_len);
patch_le32(loader,e_len,"EDI0",ph.u_len+ph.overlap_overhead+d_len-1);
patch_le32(loader,e_len,"ESI0",ph.c_len+e_len+d_len-1);
linker->defineSymbol("bytes_to_copy", ph.c_len + d_len);
linker->defineSymbol("copy_dest", ph.u_len + ph.overlap_overhead + d_len - 1);
linker->defineSymbol("copy_source", ph.c_len + lsize - 1);
//fprintf(stderr,"\nelen=%x dlen=%x copy_len=%x copy_to=%x oo=%x jmp_pos=%x ulen=%x c_len=%x \n\n",
// 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->relocate();
MemBuffer loader(lsize);
memcpy(loader,getLoader(),lsize);
patchPackHeader(loader,e_len);
memcpy(&oh,&ih,sizeof(oh));
oh.imagesize = ph.c_len+e_len+d_len; // new size
oh.imagesize = ph.c_len + lsize; // new size
oh.entry = s_point; // new entry point
oh.relocsize = 4;

View File

@ -58,6 +58,7 @@ protected:
unsigned range = 0,
unsigned upper_limit = ~0u) const;
virtual int buildLoader(const Filter *ft);
virtual Linker* newLinker() const;
unsigned adam_offset;
int big_relocs;

View File

@ -332,9 +332,11 @@ i386-dos32.djgpp2-stubify.h : $(srcdir)/src/$$T.asm
i386-dos32.tmt% : tc_list = arch-i386 default
i386-dos32.tmt.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_tmt.asm -- loader & decompressor for the tmt/adam format
;
; This file is part of the UPX executable compressor.
@ -24,28 +25,23 @@
; 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
; =============
// =============
// ============= ENTRY POINT
// =============
start:
; __TMTMAIN1__
mov edi, 0 ; relocation offset
section TMTMAIN1
mov edi, 0 // relocated
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 bytes_to_copy
std
rep
@ -54,57 +50,49 @@ start:
lea esi, [edi + 1]
pop edi
or ebp, byte -1
or ebp, -1
push edi
%ifdef __TMTCALT1__
section TMTCALT1
push edi
%endif; __TMTMAIN2__
jmpn .1+'JMPD'
.1:
%include "include/header.ash"
section TMTMAIN2
jmp decompressor
cutpoint:
; __TMTCUTPO__
#include "include/header2.ash"
; =============
; ============= DECOMPRESSION
; =============
section TMTCUTPO
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
// =============
; __TMTMAIN5__
//#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"
section TMTMAIN5
pop ebp
mov esi, edi
sub esi, [edi - 4]
; =============
; ============= CALLTRICK
; =============
// =============
// ============= CALLTRICK
// =============
%ifdef __TMTCALT2__
section TMTCALT2
pop edi
cjt32 ebp
%endif; __TMTRELOC__
; =============
; ============= RELOCATION
; =============
// =============
// ============= RELOCATION
// =============
section TMTRELOC
lea edi, [ebp - 4]
reloc32 esi, edi, ebp
; =============
; __TMTJUMP1__
jmpn .1+'JMPO'
.1:
eof:
; __TMTHEEND__
section .data
dd -1
dw eof
section TMTJUMP1
jmp original_entry
; vi:ts=8:et:nowrap
// vi:ts=8:et:nowrap