From 08962a74ab09747fca96b7cb32ad81b1ec5bf584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Moln=C3=A1r?= Date: Sat, 1 Jul 2006 00:21:53 +0200 Subject: [PATCH] started the arm/pe conversion to ElfLinker --- src/p_armpe.cpp | 32 ++++------ src/p_armpe.h | 1 + src/stub/Makefile | 8 +-- src/stub/src/arch/arm/v4t/nrv2b_d8.S | 7 ++- src/stub/src/arch/arm/v4t/nrv2e_d8.S | 7 ++- src/stub/src/arm.v4a-wince.pe.S | 72 +++++++--------------- src/stub/src/arm.v4t-wince.pe.S | 90 +++++++++------------------- 7 files changed, 72 insertions(+), 145 deletions(-) diff --git a/src/p_armpe.cpp b/src/p_armpe.cpp index c0ab79a6..df38e833 100644 --- a/src/p_armpe.cpp +++ b/src/p_armpe.cpp @@ -219,27 +219,7 @@ bool PackArmPe::testUnpackVersion(int version) const Linker* PackArmPe::newLinker() const { - struct ArmLinker : public DefaultLELinker - { - 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; + return new ElfLinkerArmLE; } @@ -1677,7 +1657,7 @@ int PackArmPe::buildLoader(const Filter *ft) if (!use_thumb_stub) { if (ph.method == M_NRV2E_8) - addLoader("ucl_nrv2e_decompress_8", NULL); + addLoader("Sucl_nrv2e_decompress_8", NULL); } else { @@ -1701,6 +1681,14 @@ int PackArmPe::rpatch_le32(void *b, int blen, const void *old, unsigned new_, 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) { diff --git a/src/p_armpe.h b/src/p_armpe.h index 11d8f7cc..0c832d83 100644 --- a/src/p_armpe.h +++ b/src/p_armpe.h @@ -123,6 +123,7 @@ protected: virtual Linker* newLinker() const; int rpatch_le32(void *b, int blen, const void *old, unsigned new_, PackArmPe_Reloc &, unsigned); + int rdefSymbol(const char *, unsigned, PackArmPe_Reloc &, unsigned); struct pe_header_t { diff --git a/src/stub/Makefile b/src/stub/Makefile index 0c9d77d0..ae15413b 100644 --- a/src/stub/Makefile +++ b/src/stub/Makefile @@ -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.v4a-wince.pe.h : $(srcdir)/src/$$T.S - $(call tc,gcc) -march=armv4 -nostdlib $< -o tmp/$T.out - $(call tc,objcopy) --only-section .text -O binary tmp/$T.out tmp/$T.bin + $(call tc,gcc) -march=armv4 -c $< -o tmp/$T.bin + $(call tc,m-objdump) -trwh tmp/$T.bin >> tmp/$T.bin $(call tc,bin2h) --ident=nrv_loader_arm tmp/$T.bin $@ arm.v4t-wince.pe.h : $(srcdir)/src/$$T.S - $(call tc,gcc) -march=armv4t -nostdlib $< -o tmp/$T.out - $(call tc,objcopy) --only-section .text -O binary tmp/$T.out tmp/$T.bin + $(call tc,gcc) -march=armv4t -c $< -o tmp/$T.bin + $(call tc,m-objdump) -trwh tmp/$T.bin >> tmp/$T.bin $(call tc,bin2h) --ident=nrv_loader_thumb tmp/$T.bin $@ diff --git a/src/stub/src/arch/arm/v4t/nrv2b_d8.S b/src/stub/src/arch/arm/v4t/nrv2b_d8.S index f5f2460d..c9f98a2d 100644 --- a/src/stub/src/arch/arm/v4t/nrv2b_d8.S +++ b/src/stub/src/arch/arm/v4t/nrv2b_d8.S @@ -46,15 +46,16 @@ #define jnextb0 GETBIT; bcc #define jnextb1 GETBIT; bcs +#ifndef PURE_THUMB ucl_nrv2b_decompress_8: .globl ucl_nrv2b_decompress_8 @ ARM mode .type ucl_nrv2b_decompress_8, %function /* 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 - .code 16 @ THUMB mode .thumb_func -go_thumb_n2b: .globl go_thumb_n2b +#endif +.go_thumb_n2b: add r1,len,src @ r1= eof_src; push {r1,r2,r3, r4,r5,r6,r7, lr} mov bits,#1; neg off,bits @ off= -1 initial condition diff --git a/src/stub/src/arch/arm/v4t/nrv2e_d8.S b/src/stub/src/arch/arm/v4t/nrv2e_d8.S index 6ff3eea0..6206ee43 100644 --- a/src/stub/src/arch/arm/v4t/nrv2e_d8.S +++ b/src/stub/src/arch/arm/v4t/nrv2e_d8.S @@ -69,17 +69,18 @@ #define jnextb0 GETBIT; bcc #define jnextb1 GETBIT; bcs +#ifndef PURE_THUMB ucl_nrv2e_decompress_8: .globl ucl_nrv2e_decompress_8 @ ARM mode .type ucl_nrv2e_decompress_8, %function /* error = (*)(char const *src, int len_src, char *dst, int *plen_dst) Actual decompressed length is stored through plen_dst. 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 - .globl thumb_nrv2e_d8 .thumb_func -thumb_nrv2e_d8: +#endif +.thumb_nrv2e_d8: push {r2,r3, r4,r5,r6,r7, lr} #define sp_DST0 0 /* stack offset of original dst */ add srclim,len,src @ srclim= eof_src; diff --git a/src/stub/src/arm.v4a-wince.pe.S b/src/stub/src/arm.v4a-wince.pe.S index 7b6ebcf0..980a8306 100644 --- a/src/stub/src/arm.v4a-wince.pe.S +++ b/src/stub/src/arm.v4a-wince.pe.S @@ -53,30 +53,18 @@ #endif #endif -@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ - -// 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 +#define section .section @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ - .text - .balign 0 - .globl _start .arm -_start: - dst0 .req r9 @ global register -SECT(DllStart): +section DllStart cmp r1, #1 bne .Lstart_orig -SECT(ExeStart): +section ExeStart stmfd sp!, {r0 - r11, lr} DINIT adr r3, SRC0 @@ -95,12 +83,12 @@ SECT(ExeStart): CacheSync: ldr pc, IATT + 8 -SRC0: .ascii "SRC0" @ start of compressed data -SRCL: .ascii "SRCL" @ compressed length -DST0: .ascii "DST0" @ start of uncompressed data -DSTL: .ascii "DSTL" @ uncompressed length -IATT: .ascii "IATT"; .long 0, 0, 0 @ import address table -ENTR: .ascii "ENTR" @ original entry point +SRC0: .long start_of_compressed +SRCL: .long compressed_length +DST0: .long start_of_uncompressed +DSTL: .long uncompressed_length +IATT: .long import_address_table; .long 0, 0, 0 +ENTR: .long original_entry @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@ -171,13 +159,13 @@ ProcessAll: @@ uncompress/unfilter/imports/relocs are copied here by the upx linker -SECT(ProcessEnd): +section ProcessEnd ldmia sp!, {pc} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -SECT(Unfilter_0x50): +section Unfilter_0x50 buffer .req r0 addval .req r2 bufend .req ip @@ -208,15 +196,15 @@ SECT(Unfilter_0x50): .unreq addval .unreq bufend -FIBS: .ascii "FIBS" @ buffer start for filter -FIBE: .ascii "FIBE" @ buffer end for filter +FIBS: .long filter_buffer_start +FIBE: .long filter_buffer_end .Luf_end: DDUMP (#'F') @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -SECT(Relocs): +section Relocs dest .req r0 buffer .req r1 addval .req dst0 @@ -249,7 +237,7 @@ SECT(Relocs): str ip, [dest] b .Lreloc_loop -BREL: .ascii "BREL" @ start of reloc info +BREL: .long start_of_relocs .unreq buffer .unreq addval @@ -260,7 +248,7 @@ BREL: .ascii "BREL" @ start of reloc info @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -SECT(Imports): +section Imports imp .req r4 iat .req r5 dll .req r6 @@ -335,8 +323,8 @@ LoadLibraryW: GetProcAddressA: mov pc, r11 -BIMP: .ascii "BIMP" @ start of import data -ONAM: .ascii "ONAM" @ start of dll names +BIMP: .long start_of_imports +ONAM: .long start_of_dll_names .Lhi_end: 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" -SECT(Call2E): - BL (ucl_nrv2e_decompress_8) +section Call2E + bl ucl_nrv2e_decompress_8 DDUMP (#'C') @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -SECT(UPX1HEAD): - .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 +#include "include/header2.ash" -@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ - -SECT(eof): - .text 1 - .long -1; .short eof - _start diff --git a/src/stub/src/arm.v4t-wince.pe.S b/src/stub/src/arm.v4t-wince.pe.S index aed7ea68..3ad24e1e 100644 --- a/src/stub/src/arm.v4t-wince.pe.S +++ b/src/stub/src/arm.v4t-wince.pe.S @@ -31,28 +31,17 @@ #define DEBUG 0 -// magic for the UPX linker -#define SECT(n) .text 1; .asciz #n; .long n - _start; .text 0; n +#define section .section -#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 -_start: dst0 .req r9 @ global register -SECT(DllStart): +section DllStart cmp r1, #1 bne .Lstart_orig -SECT(ExeStart): +section ExeStart stmfd sp!, {r0 - r11, lr} @ access all pages in ARM mode - this seems to be required @@ -76,12 +65,12 @@ SECT(ExeStart): ldr ip, ENTR bx ip -SRC0: .ascii "SRC0" @ start of compressed data -SRCL: .ascii "SRCL" @ compressed length -DST0: .ascii "DST0" @ start of uncompressed data -DSTL: .ascii "DSTL" @ uncompressed length -IATT: .ascii "IATT"; .long 0, 0, 0 @ import address table -ENTR: .ascii "ENTR" @ original entry point +SRC0: .long start_of_compressed +SRCL: .long compressed_length +DST0: .long start_of_uncompressed +DSTL: .long uncompressed_length +IATT: .long import_address_table; .long 0, 0, 0 +ENTR: .long original_entry .thumb @@ -96,7 +85,7 @@ ProcessAll: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -SECT(Unfilter_0x50): +section Unfilter_0x50 .align 2 buffer .req r0 addval .req r2 @@ -135,14 +124,14 @@ SECT(Unfilter_0x50): .unreq bufend .align 2 -FIBS: .ascii "FIBS" @ buffer start for filter -FIBE: .ascii "FIBE" @ buffer end for filter +FIBS: .long filter_buffer_start +FIBE: .long filter_buffer_end .Luf50_ret: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -SECT(Relocs): +section Relocs .align 2 buffer .req r0 dest .req r1 @@ -187,18 +176,19 @@ SECT(Relocs): b .Lreloc_loop .align 2 -BREL: .ascii "BREL" @ start of reloc info +BREL: .long start_of_relocs .Lreloc_end: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -SECT(Imports): +section Imports .align 2 imp .req r4 iat .req r5 dll .req r6 +.real_start_ofImports: // suppress silly warnings mov r7, sp sub sp, #508 sub sp, #508 @@ -280,15 +270,15 @@ GetProcAddressA: bx r11 .align 2 -BIMP: .ascii "BIMP" @ start of import data -ONAM: .ascii "ONAM" @ start of dll names +BIMP: .long start_of_imports +ONAM: .long start_of_dll_names .Lhi_end: mov sp, r7 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -SECT(ProcessEnd): +section ProcessEnd pop {r1, r2} @ r1=CacheSync, r2=lr mov r0, #4 @ parameter of CacheSync 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): - .align 2 - .arm +section .ucl_nrv2b_decompress_8 #include "arch/arm/v4t/nrv2b_d8.S" - XSECT (go_thumb_n2b) - -SECT(Call2B): - BL (go_thumb_n2b) +section Call2B + bl .go_thumb_n2b #undef wrnk #undef GETBIT -SECT(dummy1): - .align 2 - .arm +section .ucl_nrv2e_decompress_8 #include "arch/arm/v4t/nrv2e_d8.S" - XSECT (thumb_nrv2e_d8) - -SECT(Call2E): - BL (thumb_nrv2e_d8) +section Call2E + bl .thumb_nrv2e_d8 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -SECT(UPX1HEAD): - .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 +#include "include/header2.ash"