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

started the arm/pe conversion to ElfLinker

This commit is contained in:
László Molnár 2006-07-01 00:21:53 +02:00
parent f850d36c52
commit 08962a74ab
7 changed files with 72 additions and 145 deletions

View File

@ -219,27 +219,7 @@ bool PackArmPe::testUnpackVersion(int version) const
Linker* PackArmPe::newLinker() const Linker* PackArmPe::newLinker() const
{ {
struct ArmLinker : public DefaultLELinker return new ElfLinkerArmLE;
{
virtual void set32(void *b, unsigned v) const
{
set_le32(b, (v - 5) / 4);
}
};
struct ThumbLinker : public DefaultLELinker
{
virtual void set32(void *b, unsigned v) const
{
assert(v < 0x200);
set_le32(b, 0xF000 + ((v - 1) / 2) * 0x10000);
}
};
if (use_thumb_stub)
return new ThumbLinker;
else
return new ArmLinker;
} }
@ -1677,7 +1657,7 @@ int PackArmPe::buildLoader(const Filter *ft)
if (!use_thumb_stub) if (!use_thumb_stub)
{ {
if (ph.method == M_NRV2E_8) if (ph.method == M_NRV2E_8)
addLoader("ucl_nrv2e_decompress_8", NULL); addLoader("Sucl_nrv2e_decompress_8", NULL);
} }
else else
{ {
@ -1701,6 +1681,14 @@ int PackArmPe::rpatch_le32(void *b, int blen, const void *old, unsigned new_,
return o; return o;
} }
int PackArmPe::rdefSymbol(const char *s, unsigned v,
PackArmPe_Reloc &rel, unsigned off)
{
linker->defineSymbol(s, v);
rel.add(off, 3);
return 0;
}
void PackArmPe::pack(OutputFile *fo) void PackArmPe::pack(OutputFile *fo)
{ {

View File

@ -123,6 +123,7 @@ protected:
virtual Linker* newLinker() const; virtual Linker* newLinker() const;
int rpatch_le32(void *b, int blen, const void *old, unsigned new_, int rpatch_le32(void *b, int blen, const void *old, unsigned new_,
PackArmPe_Reloc &, unsigned); PackArmPe_Reloc &, unsigned);
int rdefSymbol(const char *, unsigned, PackArmPe_Reloc &, unsigned);
struct pe_header_t struct pe_header_t
{ {

View File

@ -247,13 +247,13 @@ arm.v4a-wince.pe% : tc_list = arm-linux.elf default
arm.v4t-wince.pe% : tc_list = arm-linux.elf default arm.v4t-wince.pe% : tc_list = arm-linux.elf default
arm.v4a-wince.pe.h : $(srcdir)/src/$$T.S arm.v4a-wince.pe.h : $(srcdir)/src/$$T.S
$(call tc,gcc) -march=armv4 -nostdlib $< -o tmp/$T.out $(call tc,gcc) -march=armv4 -c $< -o tmp/$T.bin
$(call tc,objcopy) --only-section .text -O binary tmp/$T.out tmp/$T.bin $(call tc,m-objdump) -trwh tmp/$T.bin >> tmp/$T.bin
$(call tc,bin2h) --ident=nrv_loader_arm tmp/$T.bin $@ $(call tc,bin2h) --ident=nrv_loader_arm tmp/$T.bin $@
arm.v4t-wince.pe.h : $(srcdir)/src/$$T.S arm.v4t-wince.pe.h : $(srcdir)/src/$$T.S
$(call tc,gcc) -march=armv4t -nostdlib $< -o tmp/$T.out $(call tc,gcc) -march=armv4t -c $< -o tmp/$T.bin
$(call tc,objcopy) --only-section .text -O binary tmp/$T.out tmp/$T.bin $(call tc,m-objdump) -trwh tmp/$T.bin >> tmp/$T.bin
$(call tc,bin2h) --ident=nrv_loader_thumb tmp/$T.bin $@ $(call tc,bin2h) --ident=nrv_loader_thumb tmp/$T.bin $@

View File

@ -46,15 +46,16 @@
#define jnextb0 GETBIT; bcc #define jnextb0 GETBIT; bcc
#define jnextb1 GETBIT; bcs #define jnextb1 GETBIT; bcs
#ifndef PURE_THUMB
ucl_nrv2b_decompress_8: .globl ucl_nrv2b_decompress_8 @ ARM mode ucl_nrv2b_decompress_8: .globl ucl_nrv2b_decompress_8 @ ARM mode
.type ucl_nrv2b_decompress_8, %function .type ucl_nrv2b_decompress_8, %function
/* error = (*)(char const *src, int len_src, char *dst, int *plen_dst) */ /* error = (*)(char const *src, int len_src, char *dst, int *plen_dst) */
adr r12,1+go_thumb_n2b @ load pc-relative address adr r12,1+.go_thumb_n2b @ load pc-relative address
bx r12 @ enter THUMB mode bx r12 @ enter THUMB mode
.code 16 @ THUMB mode .code 16 @ THUMB mode
.thumb_func .thumb_func
go_thumb_n2b: .globl go_thumb_n2b #endif
.go_thumb_n2b:
add r1,len,src @ r1= eof_src; add r1,len,src @ r1= eof_src;
push {r1,r2,r3, r4,r5,r6,r7, lr} push {r1,r2,r3, r4,r5,r6,r7, lr}
mov bits,#1; neg off,bits @ off= -1 initial condition mov bits,#1; neg off,bits @ off= -1 initial condition

View File

@ -69,17 +69,18 @@
#define jnextb0 GETBIT; bcc #define jnextb0 GETBIT; bcc
#define jnextb1 GETBIT; bcs #define jnextb1 GETBIT; bcs
#ifndef PURE_THUMB
ucl_nrv2e_decompress_8: .globl ucl_nrv2e_decompress_8 @ ARM mode ucl_nrv2e_decompress_8: .globl ucl_nrv2e_decompress_8 @ ARM mode
.type ucl_nrv2e_decompress_8, %function .type ucl_nrv2e_decompress_8, %function
/* error = (*)(char const *src, int len_src, char *dst, int *plen_dst) /* error = (*)(char const *src, int len_src, char *dst, int *plen_dst)
Actual decompressed length is stored through plen_dst. Actual decompressed length is stored through plen_dst.
For SAFE mode: at call, *plen_dst must be allowed length of output buffer. For SAFE mode: at call, *plen_dst must be allowed length of output buffer.
*/ */
adr r12,1+thumb_nrv2e_d8; bx r12 @ enter THUMB mode adr r12,1+.thumb_nrv2e_d8; bx r12 @ enter THUMB mode
.code 16 @ THUMB mode .code 16 @ THUMB mode
.globl thumb_nrv2e_d8
.thumb_func .thumb_func
thumb_nrv2e_d8: #endif
.thumb_nrv2e_d8:
push {r2,r3, r4,r5,r6,r7, lr} push {r2,r3, r4,r5,r6,r7, lr}
#define sp_DST0 0 /* stack offset of original dst */ #define sp_DST0 0 /* stack offset of original dst */
add srclim,len,src @ srclim= eof_src; add srclim,len,src @ srclim= eof_src;

View File

@ -53,30 +53,18 @@
#endif #endif
#endif #endif
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ #define section .section
// magic for the UPX linker
#define SECT(n) .text 1; .asciz #n; .long n - _start; .text 0; n
#define BL(t) \
.text 1; .long 0, bl##t - _start; .asciz #t; .long 0; \
.text 0; .byte 0, 0, 0; bl##t: .byte 0xeb
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
.text
.balign 0
.globl _start
.arm .arm
_start:
dst0 .req r9 @ global register dst0 .req r9 @ global register
SECT(DllStart): section DllStart
cmp r1, #1 cmp r1, #1
bne .Lstart_orig bne .Lstart_orig
SECT(ExeStart): section ExeStart
stmfd sp!, {r0 - r11, lr} stmfd sp!, {r0 - r11, lr}
DINIT DINIT
adr r3, SRC0 adr r3, SRC0
@ -95,12 +83,12 @@ SECT(ExeStart):
CacheSync: CacheSync:
ldr pc, IATT + 8 ldr pc, IATT + 8
SRC0: .ascii "SRC0" @ start of compressed data SRC0: .long start_of_compressed
SRCL: .ascii "SRCL" @ compressed length SRCL: .long compressed_length
DST0: .ascii "DST0" @ start of uncompressed data DST0: .long start_of_uncompressed
DSTL: .ascii "DSTL" @ uncompressed length DSTL: .long uncompressed_length
IATT: .ascii "IATT"; .long 0, 0, 0 @ import address table IATT: .long import_address_table; .long 0, 0, 0
ENTR: .ascii "ENTR" @ original entry point ENTR: .long original_entry
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ -171,13 +159,13 @@ ProcessAll:
@@ uncompress/unfilter/imports/relocs are copied here by the upx linker @@ uncompress/unfilter/imports/relocs are copied here by the upx linker
SECT(ProcessEnd): section ProcessEnd
ldmia sp!, {pc} ldmia sp!, {pc}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SECT(Unfilter_0x50): section Unfilter_0x50
buffer .req r0 buffer .req r0
addval .req r2 addval .req r2
bufend .req ip bufend .req ip
@ -208,15 +196,15 @@ SECT(Unfilter_0x50):
.unreq addval .unreq addval
.unreq bufend .unreq bufend
FIBS: .ascii "FIBS" @ buffer start for filter FIBS: .long filter_buffer_start
FIBE: .ascii "FIBE" @ buffer end for filter FIBE: .long filter_buffer_end
.Luf_end: .Luf_end:
DDUMP (#'F') DDUMP (#'F')
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SECT(Relocs): section Relocs
dest .req r0 dest .req r0
buffer .req r1 buffer .req r1
addval .req dst0 addval .req dst0
@ -249,7 +237,7 @@ SECT(Relocs):
str ip, [dest] str ip, [dest]
b .Lreloc_loop b .Lreloc_loop
BREL: .ascii "BREL" @ start of reloc info BREL: .long start_of_relocs
.unreq buffer .unreq buffer
.unreq addval .unreq addval
@ -260,7 +248,7 @@ BREL: .ascii "BREL" @ start of reloc info
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SECT(Imports): section Imports
imp .req r4 imp .req r4
iat .req r5 iat .req r5
dll .req r6 dll .req r6
@ -335,8 +323,8 @@ LoadLibraryW:
GetProcAddressA: GetProcAddressA:
mov pc, r11 mov pc, r11
BIMP: .ascii "BIMP" @ start of import data BIMP: .long start_of_imports
ONAM: .ascii "ONAM" @ start of dll names ONAM: .long start_of_dll_names
.Lhi_end: .Lhi_end:
add sp, sp, #bufsize add sp, sp, #bufsize
@ -344,31 +332,15 @@ ONAM: .ascii "ONAM" @ start of dll names
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SECT(ucl_nrv2e_decompress_8): section Sucl_nrv2e_decompress_8
#include "arch/arm/v4a/nrv2e_d8.S" #include "arch/arm/v4a/nrv2e_d8.S"
SECT(Call2E): section Call2E
BL (ucl_nrv2e_decompress_8) bl ucl_nrv2e_decompress_8
DDUMP (#'C') DDUMP (#'C')
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SECT(UPX1HEAD): #include "include/header2.ash"
.byte 85,80,88,33 @ 0 UPX_MAGIC_LE32
.byte 161,216,208,213 @ 4 UPX_MAGIC2_LE32
.long 0 @ 8 uncompressed adler32
.long 0 @ 12 compressed adler32
.long 0 @ 16 uncompressed len
.long 0 @ 20 compressed len
.long 0 @ 24 original file size
.byte 0 @ 28 filter id
.byte 0 @ 29 filter cto
.byte 0 @ unused
.byte 45 @ 31 header checksum
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SECT(eof):
.text 1
.long -1; .short eof - _start

View File

@ -31,28 +31,17 @@
#define DEBUG 0 #define DEBUG 0
// magic for the UPX linker #define section .section
#define SECT(n) .text 1; .asciz #n; .long n - _start; .text 0; n
#define BL(t) \
.text 1; .long 0, bl##t - _start; .asciz #t; .long 0; \
.text 0; .byte 0, 0, 0; bl##t: .byte 0xf8
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
.text
.balign 0
.globl _start
.arm .arm
_start:
dst0 .req r9 @ global register dst0 .req r9 @ global register
SECT(DllStart): section DllStart
cmp r1, #1 cmp r1, #1
bne .Lstart_orig bne .Lstart_orig
SECT(ExeStart): section ExeStart
stmfd sp!, {r0 - r11, lr} stmfd sp!, {r0 - r11, lr}
@ access all pages in ARM mode - this seems to be required @ access all pages in ARM mode - this seems to be required
@ -76,12 +65,12 @@ SECT(ExeStart):
ldr ip, ENTR ldr ip, ENTR
bx ip bx ip
SRC0: .ascii "SRC0" @ start of compressed data SRC0: .long start_of_compressed
SRCL: .ascii "SRCL" @ compressed length SRCL: .long compressed_length
DST0: .ascii "DST0" @ start of uncompressed data DST0: .long start_of_uncompressed
DSTL: .ascii "DSTL" @ uncompressed length DSTL: .long uncompressed_length
IATT: .ascii "IATT"; .long 0, 0, 0 @ import address table IATT: .long import_address_table; .long 0, 0, 0
ENTR: .ascii "ENTR" @ original entry point ENTR: .long original_entry
.thumb .thumb
@ -96,7 +85,7 @@ ProcessAll:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SECT(Unfilter_0x50): section Unfilter_0x50
.align 2 .align 2
buffer .req r0 buffer .req r0
addval .req r2 addval .req r2
@ -135,14 +124,14 @@ SECT(Unfilter_0x50):
.unreq bufend .unreq bufend
.align 2 .align 2
FIBS: .ascii "FIBS" @ buffer start for filter FIBS: .long filter_buffer_start
FIBE: .ascii "FIBE" @ buffer end for filter FIBE: .long filter_buffer_end
.Luf50_ret: .Luf50_ret:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SECT(Relocs): section Relocs
.align 2 .align 2
buffer .req r0 buffer .req r0
dest .req r1 dest .req r1
@ -187,18 +176,19 @@ SECT(Relocs):
b .Lreloc_loop b .Lreloc_loop
.align 2 .align 2
BREL: .ascii "BREL" @ start of reloc info BREL: .long start_of_relocs
.Lreloc_end: .Lreloc_end:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SECT(Imports): section Imports
.align 2 .align 2
imp .req r4 imp .req r4
iat .req r5 iat .req r5
dll .req r6 dll .req r6
.real_start_ofImports: // suppress silly warnings
mov r7, sp mov r7, sp
sub sp, #508 sub sp, #508
sub sp, #508 sub sp, #508
@ -280,15 +270,15 @@ GetProcAddressA:
bx r11 bx r11
.align 2 .align 2
BIMP: .ascii "BIMP" @ start of import data BIMP: .long start_of_imports
ONAM: .ascii "ONAM" @ start of dll names ONAM: .long start_of_dll_names
.Lhi_end: .Lhi_end:
mov sp, r7 mov sp, r7
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SECT(ProcessEnd): section ProcessEnd
pop {r1, r2} @ r1=CacheSync, r2=lr pop {r1, r2} @ r1=CacheSync, r2=lr
mov r0, #4 @ parameter of CacheSync mov r0, #4 @ parameter of CacheSync
mov lr, r2 mov lr, r2
@ -296,50 +286,24 @@ SECT(ProcessEnd):
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#define XSECT(n) .text 1; .asciz #n; .long n - _start; .text 0 #define PURE_THUMB 1
SECT(dummy0): section .ucl_nrv2b_decompress_8
.align 2
.arm
#include "arch/arm/v4t/nrv2b_d8.S" #include "arch/arm/v4t/nrv2b_d8.S"
XSECT (go_thumb_n2b) section Call2B
bl .go_thumb_n2b
SECT(Call2B):
BL (go_thumb_n2b)
#undef wrnk #undef wrnk
#undef GETBIT #undef GETBIT
SECT(dummy1): section .ucl_nrv2e_decompress_8
.align 2
.arm
#include "arch/arm/v4t/nrv2e_d8.S" #include "arch/arm/v4t/nrv2e_d8.S"
XSECT (thumb_nrv2e_d8) section Call2E
bl .thumb_nrv2e_d8
SECT(Call2E):
BL (thumb_nrv2e_d8)
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SECT(UPX1HEAD): #include "include/header2.ash"
.byte 85,80,88,33 @ 0 UPX_MAGIC_LE32
.byte 161,216,208,213 @ 4 UPX_MAGIC2_LE32
.long 0 @ 8 uncompressed adler32
.long 0 @ 12 compressed adler32
.long 0 @ 16 uncompressed len
.long 0 @ 20 compressed len
.long 0 @ 24 original file size
.byte 0 @ 28 filter id
.byte 0 @ 29 filter cto
.byte 0 @ unused
.byte 45 @ 31 header checksum
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
.arm
SECT(eof):
.text 1
.long -1; .short eof - _start