1
0
mirror of https://github.com/upx/upx synced 2025-10-05 19:20:23 +08:00

work in progress for --android-shlib

modified:   p_elf_enum.h
	modified:   p_lx_elf.cpp
	modified:   stub/src/arm.v4a-linux.shlib-init.S
	modified:   stub/src/arm.v4t-linux.shlib-init.S
This commit is contained in:
John Reiser 2018-01-12 08:32:24 -08:00
parent cacb239040
commit c7969ed5a5
7 changed files with 783 additions and 747 deletions

View File

@ -212,6 +212,19 @@
#endif #endif
#ifdef WANT_REL_ENUM //{
static unsigned ELF32_R_TYPE(unsigned x) { return 0xff & x; }
enum { // R_*_RELATIVE relocation types: Adjust by program base
R_386_RELATIVE = 8,
R_AARCH64_RELATIVE = 1027,
R_ARM_RELATIVE = 23,
R_PPC_RELATIVE = 22,
R_PPC64_RELATIVE = R_PPC_RELATIVE,
R_X86_64_RELATIVE = 8
};
#endif //}
#ifdef WANT_NHDR_ENUM #ifdef WANT_NHDR_ENUM
#undef WANT_NHDR_ENUM #undef WANT_NHDR_ENUM
enum { // ELF PT_NOTE types enum { // ELF PT_NOTE types

View File

@ -309,8 +309,8 @@ off_t PackLinuxElf::pack3(OutputFile *fo, Filter &ft) // return length of output
fo->write(&zero, t); fo->write(&zero, t);
len += t; len += t;
set_te32(&disp, 2*sizeof(disp) + len - (sz_elf_hdrs + sizeof(p_info) + sizeof(l_info))); set_te32(&disp, sz_elf_hdrs + sizeof(p_info) + sizeof(l_info));
fo->write(&disp, sizeof(disp)); // .e_entry - &first_b_info fo->write(&disp, sizeof(disp)); // offset(b_info)
len += sizeof(disp); len += sizeof(disp);
set_te32(&disp, len); // distance back to beginning (detect dynamic reloc) set_te32(&disp, len); // distance back to beginning (detect dynamic reloc)
fo->write(&disp, sizeof(disp)); fo->write(&disp, sizeof(disp));
@ -2497,6 +2497,10 @@ PackLinuxElf64::generateElfHdr(
} }
} }
#define WANT_REL_ENUM
#include "p_elf_enum.h"
#undef WANT_REL_ENUM
void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/) void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
{ {
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
@ -2638,6 +2642,15 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
if (xct_off <= r_offset) { if (xct_off <= r_offset) {
set_te32(&rel->r_offset, asl_delta + r_offset); set_te32(&rel->r_offset, asl_delta + r_offset);
} }
// r_offset must be in 2nd PT_LOAD; .p_vaddr was already relocated
unsigned d = elf_get_offset_from_address(asl_delta + r_offset);
unsigned w = get_te32(&file_image[d]);
unsigned r_info = get_te32(&rel->r_info);
if (xct_off <= w
&& Elf32_Ehdr::EM_ARM == e_machine
&& R_ARM_RELATIVE == ELF32_R_TYPE(r_info)) {
set_te32(&file_image[d], asl_delta + w);
}
} }
fo->seek(sh_offset, SEEK_SET); fo->seek(sh_offset, SEEK_SET);
fo->rewrite(sh_offset + file_image, sh_size); fo->rewrite(sh_offset + file_image, sh_size);

File diff suppressed because it is too large Load Diff

View File

@ -32,15 +32,15 @@
#define STUB_ARM_V5T_LINUX_SHLIB_INIT_SIZE 15566 #define STUB_ARM_V5T_LINUX_SHLIB_INIT_SIZE 15566
#define STUB_ARM_V5T_LINUX_SHLIB_INIT_ADLER32 0x4b0e1f0a #define STUB_ARM_V5T_LINUX_SHLIB_INIT_ADLER32 0xf865201e
#define STUB_ARM_V5T_LINUX_SHLIB_INIT_CRC32 0x0f45a16c #define STUB_ARM_V5T_LINUX_SHLIB_INIT_CRC32 0xc1cd6d32
unsigned char stub_arm_v5t_linux_shlib_init[15566] = { unsigned char stub_arm_v5t_linux_shlib_init[15566] = {
/* 0x0000 */ 127, 69, 76, 70, 1, 1, 1, 97, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0000 */ 127, 69, 76, 70, 1, 1, 1, 97, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x0010 */ 1, 0, 40, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0010 */ 1, 0, 40, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x0020 */ 236, 20, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 0, 0, 40, 0, /* 0x0020 */ 236, 20, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 0, 0, 40, 0,
/* 0x0030 */ 0, 0, 0, 0,192, 70,255,181, 0,161, 24, 57, 13, 28, 12,104, /* 0x0030 */ 0, 0, 0, 0, 1,222,255,181, 0,161, 28, 57, 11,104, 13, 29,
/* 0x0040 */ 45, 27, 75,104, 91, 25, 8,147,200,104, 64, 25, 24, 33, 9, 24, /* 0x0040 */ 76,104, 45, 27,140,104,100, 25, 8,148, 8,105, 64, 25, 89, 25,
/* 0x0050 */ 131,176, 0,181, 76,104, 12, 49, 9, 25, 0,240,166,248, 3, 5, /* 0x0050 */ 131,176, 0,181, 76,104, 12, 49, 9, 25, 0,240,166,248, 3, 5,
/* 0x0060 */ 27, 13,228, 24, 16,180,192, 26, 1,180,228, 26,192, 24,155, 8, /* 0x0060 */ 27, 13,228, 24, 16,180,192, 26, 1,180,228, 26,192, 24,155, 8,
/* 0x0070 */ 8,180, 0, 0, 0,240, 57,248, 27, 6,137, 8, 27, 14,137, 0, /* 0x0070 */ 8,180, 0, 0, 0,240, 57,248, 27, 6,137, 8, 27, 14,137, 0,
@ -344,7 +344,7 @@ unsigned char stub_arm_v5t_linux_shlib_init[15566] = {
/* 0x1310 */ 0,240, 85,248, 31,188,160, 71, 8,188, 3,188, 1, 35, 3,180, /* 0x1310 */ 0,240, 85,248, 31,188,160, 71, 8,188, 3,188, 1, 35, 3,180,
/* 0x1320 */ 64, 24, 1, 48,152, 67, 10, 75, 3, 96, 1, 48, 11,144, 63,188, /* 0x1320 */ 64, 24, 1, 48,152, 67, 10, 75, 3, 96, 1, 48, 11,144, 63,188,
/* 0x1330 */ 27, 66, 0,208,160, 71, 0,152, 1,153, 9, 24, 0,240,106,248, /* 0x1330 */ 27, 66, 0,208,160, 71, 0,152, 1,153, 9, 24, 0,240,106,248,
/* 0x1340 */ 11,188,158, 70, 5, 34,125, 39, 0,223, 91, 39, 3,189, 0, 0, /* 0x1340 */ 11,188,158, 70, 5, 34,125, 39, 0,223, 91, 39, 1,222, 3,189,
/* 0x1350 */ 0,223,255,189,137, 8,137, 0, 0,181, 11, 28, 0,240,178,248, /* 0x1350 */ 0,223,255,189,137, 8,137, 0, 0,181, 11, 28, 0,240,178,248,
/* 0x1360 */ 8,188,158, 70, 11,104, 4, 49, 3, 96, 4, 48, 1, 61, 7, 35, /* 0x1360 */ 8,188,158, 70, 11,104, 4, 49, 3, 96, 4, 48, 1, 61, 7, 35,
/* 0x1370 */ 29, 66,247,209,237, 8, 13,208,120, 71,192, 70,212, 3, 45,233, /* 0x1370 */ 29, 66,247,209,237, 8, 13,208,120, 71,192, 70,212, 3, 45,233,

View File

@ -79,13 +79,14 @@ __ARM_NR_cacheflush = 2 + __ARM_NR_BASE
#define ecx r5 #define ecx r5
section ELFMAINX section ELFMAINX
// .long distance back to first b_info
// .long offset(.) // detect relocation // .long offset(.) // detect relocation
// .long offset(user DT_INIT) // .long offset(user DT_INIT)
// .long offset(escape_hatch) // .long offset(escape_hatch)
// .long offset({p_info; b_info; compressed data}) // .long offset(xct_off) dst of f_exp
_start: .globl _start _start: .globl _start
nop //; bkpt // for debugging bkpt // nop //; bkpt // for debugging
stmdb sp!,{arg1,arg2,arg3, eax,ecx,r6,r7, fp,lr,pc} stmdb sp!,{arg1,arg2,arg3, eax,ecx,r6,r7, fp,lr,pc}
mov fp,sp mov fp,sp
o_uinit= (3+4+2)*4 // pc o_uinit= (3+4+2)*4 // pc
@ -176,15 +177,17 @@ main:
// 12. goto user DT_INIT // 12. goto user DT_INIT
mov edx,lr // &f_decompress mov edx,lr // &f_decompress
add esi,edx,# _start - f_decompress - 5*4
add esi,edx,# _start - f_decompress - 4*4 lodsl; mov tmp,eax // distance back to 1st b_info
mov ecx,esi mov ecx,esi
lodsl; sub ecx,ecx,eax; //str ecx,[fp,#o_reloc] lodsl; sub ecx,ecx,eax; //str ecx,[fp,#o_reloc]
sub tmp,esi,tmp // &b_info
lodsl; add eax,ecx,eax; str eax,[fp,#o_uinit] // reloc DT_INIT for step 12 lodsl; add eax,ecx,eax; str eax,[fp,#o_uinit] // reloc DT_INIT for step 12
lodsl; add eax,ecx,eax; push eax // reloc &hatch for step 10 lodsl; add eax,ecx,eax; push eax // reloc &hatch for step 10
o_hatch= -1*4 o_hatch= -1*4
lodsl; add edi,ecx,eax // &l_info; also destination for decompress lodsl; add edi,ecx,eax // &l_info; also destination for decompress
add esi,edi,#sz_l_info + sz_p_info // &b_info mov esi,tmp // &b_info
sub sp,sp,#2*4 // param space: munmap temp pages step 9 sub sp,sp,#2*4 // param space: munmap temp pages step 9
p_unmap= -3*4 p_unmap= -3*4
@ -292,6 +295,7 @@ supervise:
bl movsl bl movsl
ldmia sp!,{arg1,arg2,arg3,arg4, eax} ldmia sp!,{arg1,arg2,arg3,arg4, eax}
bkpt
blx eax // decompress blx eax // decompress
add sp,sp,#4 // toss arg5 add sp,sp,#4 // toss arg5
@ -339,6 +343,7 @@ L620: // Implant escape hatch at end of .text
add r7,r7,#__NR_munmap - ((__NR_munmap>>16)<<16) add r7,r7,#__NR_munmap - ((__NR_munmap>>16)<<16)
.endif .endif
#endif //} #endif //}
bkpt
ldmia sp!,{arg1,arg2, pc} // goto hatch ldmia sp!,{arg1,arg2, pc} // goto hatch
movsl_subr: movsl_subr:

View File

@ -29,13 +29,14 @@
* <jreiser@users.sourceforge.net> * <jreiser@users.sourceforge.net>
*/ */
NBPW= 4
#define ARM_OLDABI 1 #define ARM_OLDABI 1
#include "arch/arm/v5a/macros.S" #include "arch/arm/v5a/macros.S"
#define bkpt .long 0xe7f001f0 /* reserved instr; Linux GNU eabi breakpoint */ #define bkpt .long 0xe7f001f0 /* reserved instr; Linux GNU eabi breakpoint */
#define bkpt_th .short 0xde01 /* reserved instr; Linux GNU eabi breakpoint */ #define bkpt_th .short 0xde01 /* reserved instr; Linux GNU eabi breakpoint */
sz_Elf32_Ehdr = 13*4 sz_Elf32_Ehdr = 13*NBPW
sz_Elf32_Phdr = 8*4 sz_Elf32_Phdr = 8*NBPW
sz_b_info= 12 sz_b_info= 12
sz_unc= 0 sz_unc= 0
@ -111,36 +112,38 @@ _=-1+_ // one less word on stack
#define lodslu bl get4u #define lodslu bl get4u
section ELFMAINX section ELFMAINX
// .long offset(b_info)
//D_INFO: //D_INFO:
// .long offset(.) // detect relocation // .long offset(.) // detect relocation
// .long offset(user DT_INIT) // .long offset(user DT_INIT)
// .long offset(escape_hatch) // override with round_up(2, PT_LOAD[0]{.p_memsz + .p_vaddr}) // .long offset(escape_hatch) // override with round_up(2, PT_LOAD[0]{.p_memsz + .p_vaddr})
// .long offset({l_info; p_info; b_info; compressed data}) // .long offset(dst for f_exp)
#define DEBUG 0 #define DEBUG 1
.code 16 //; .balign 4 .code 16 //; .balign 4
.real_start_ofELFMAINX: .real_start_ofELFMAINX:
.thumb_func .thumb_func
_start: .globl _start _start: .globl _start
nop
#if DEBUG //{ #if DEBUG //{
bkpt_th // for debugging bkpt_th // for debugging
#else //}{
nop
#endif //} #endif //}
push {r0,r1,r2,r3,r4,r5,r6,r7,lr} push {r0,r1,r2,r3,r4,r5,r6,r7,lr}
_=9 _=9
o_uinit= 1 // lr o_uinit= 1 // lr
adr esi,here adr esi,here
sub esi,esi,#4*4 + 2*(4+ DEBUG) // D_INFO sub esi,#4*2 + 5*NBPW // -NBPW + &D_INFO
here: here:
mov ecx,esi ldr tmp,[esi,#0*NBPW] // offset(b_info)
ldr eax,[esi,#0*4]; sub ecx,eax; //str ecx,[SP(o_reloc)] add ecx,esi,#NBPW // &D_INFO
ldr eax,[esi,#1*NBPW]; sub ecx,eax; //str ecx,[SP(o_reloc)]
// reloc DT_INIT for step 12 // reloc DT_INIT for step 12
ldr tmp,[esi,#1*4]; add tmp,ecx; str tmp,[SP(o_uinit)] ldr eax,[esi,#2*NBPW]; add eax,ecx; str eax,[SP(o_uinit)]
ldr edi,[esi,#3*4]; add edi,ecx // &l_info; also destination for decompress ldr edi,[esi,#4*NBPW]; add edi,ecx // dst for f_exp
mov esi,#sz_l_info+sz_p_info add esi,tmp,ecx // &b_info
add esi,edi // &b_info
sub sp,#3*4 // 3 slots of space sub sp,#3*NBPW // 3 slots of space
_=1+_ // &escape_hatch (step 10) _=1+_ // &escape_hatch (step 10)
o_hatch=_ // 10 o_hatch=_ // 10
_=2+_ // param space: munmap temp pages (step 9) _=2+_ // param space: munmap temp pages (step 9)
@ -290,7 +293,7 @@ L710:
mov tmp,lr; sub tmp,#1; push_ tmp // &f_decompress (ARM mode) mov tmp,lr; sub tmp,#1; push_ tmp // &f_decompress (ARM mode)
o_uncpr=_ // 23 o_uncpr=_ // 23
add tmp,SP(p_unflt)+1*4; push_ tmp // &dstlen add tmp,SP(p_unflt)+1*NBPW; push_ tmp // &dstlen
push_ edi // dst push_ edi // dst
push_ ecx // srclen push_ ecx // srclen
push_ esi // src; arglist ready for decompress step 6 push_ esi // src; arglist ready for decompress step 6
@ -347,8 +350,8 @@ _=-6+_ // 15
tst arg4,arg4; beq 0f // 0==ftid ==> no filter tst arg4,arg4; beq 0f // 0==ftid ==> no filter
blx eax // f_unfilter blx eax // f_unfilter
0: 0:
ldr arg1,[sp,#0*4] // lo(dst) ldr arg1,[sp,#0*NBPW] // lo(dst)
ldr arg2,[sp,#1*4] // len ldr arg2,[sp,#1*NBPW] // len
add arg2,arg1 // hi(dst) add arg2,arg1 // hi(dst)
bl x__ARM_NR_cacheflush bl x__ARM_NR_cacheflush
@ -366,6 +369,7 @@ _=-3+_ // 12
lsl r7,#16 lsl r7,#16
add r7,#__NR_munmap - ((__NR_munmap>>16)<<16) add r7,#__NR_munmap - ((__NR_munmap>>16)<<16)
.endif .endif
bkpt_th
pop {arg1,arg2, pc} // goto hatch pop {arg1,arg2, pc} // goto hatch
_=-3+_ // 9 _=-3+_ // 9
.balign 4 .balign 4
@ -439,7 +443,7 @@ o_super=_ // 28
mov arg5,#0; mvn arg5,arg5 // -1; cater to *BSD for fd of MAP_ANON mov arg5,#0; mvn arg5,arg5 // -1; cater to *BSD for fd of MAP_ANON
mov arg4,#MAP_PRIVATE|MAP_ANONYMOUS mov arg4,#MAP_PRIVATE|MAP_ANONYMOUS
mov arg3,#PROT_READ|PROT_WRITE|PROT_EXEC mov arg3,#PROT_READ|PROT_WRITE|PROT_EXEC
str arg2,[SP(p_unmap)+1*4] // length to unmap str arg2,[SP(p_unmap)+1*NBPW] // length to unmap
mov arg1,#0 // any addr mov arg1,#0 // any addr
thumb_sys7t __NR_mmap64; asr tmp,r0,#12; add tmp,#1; bne 0f; bkpt_th; 0: thumb_sys7t __NR_mmap64; asr tmp,r0,#12; add tmp,#1; bne 0f; bkpt_th; 0:
str r0,[SP(p_unmap)] // address to unmap str r0,[SP(p_unmap)] // address to unmap
@ -449,14 +453,14 @@ o_super=_ // 28
ldr ecx,[SP(o_wfrag)] // w_fragment ldr ecx,[SP(o_wfrag)] // w_fragment
bl movsl // copy the fragment bl movsl // copy the fragment
ldr esi,[SP(p_uncpr)+0*4] // src ldr esi,[SP(p_uncpr)+0*NBPW] // src
ldr ecx,[SP(p_uncpr)+1*4] // len ldr ecx,[SP(p_uncpr)+1*NBPW] // len
mov tmp,#3 mov tmp,#3
and tmp,esi // length of prefix alignment and tmp,esi // length of prefix alignment
sub esi,tmp // down to word aligned sub esi,tmp // down to word aligned
add ecx,tmp // prefix increases byte length add ecx,tmp // prefix increases byte length
add tmp,edi // skip prefix at destination add tmp,edi // skip prefix at destination
str tmp,[SP(p_uncpr)+0*4] // dst str tmp,[SP(p_uncpr)+0*NBPW] // dst
add ecx,#3 // round up to full words add ecx,#3 // round up to full words
lsr ecx,#2 lsr ecx,#2
bl movsl // copy all aligned words that contain compressed data bl movsl // copy all aligned words that contain compressed data

View File

@ -13,7 +13,7 @@ Idx Name Size VMA LMA File off Algn Flags
8 LZMA_DEC10 00000478 00000000 00000000 00000d84 2**0 CONTENTS, RELOC, READONLY 8 LZMA_DEC10 00000478 00000000 00000000 00000d84 2**0 CONTENTS, RELOC, READONLY
9 LZMA_DEC30 00000000 00000000 00000000 000011fc 2**0 CONTENTS, READONLY 9 LZMA_DEC30 00000000 00000000 00000000 000011fc 2**0 CONTENTS, READONLY
10 ELFMAINY 0000003e 00000000 00000000 000011fc 2**0 CONTENTS, READONLY 10 ELFMAINY 0000003e 00000000 00000000 000011fc 2**0 CONTENTS, READONLY
11 ELFMAINZ 000002fc 00000000 00000000 0000123a 2**0 CONTENTS, RELOC, READONLY 11 ELFMAINZ 00000310 00000000 00000000 0000123a 2**0 CONTENTS, RELOC, READONLY
SYMBOL TABLE: SYMBOL TABLE:
00000000 l d NRV2E 00000000 NRV2E 00000000 l d NRV2E 00000000 NRV2E
00000000 l d NRV2D 00000000 NRV2D 00000000 l d NRV2D 00000000 NRV2D
@ -245,26 +245,26 @@ OFFSET TYPE VALUE
RELOCATION RECORDS FOR [ELFMAINZ]: RELOCATION RECORDS FOR [ELFMAINZ]:
OFFSET TYPE VALUE OFFSET TYPE VALUE
00000048 R_ARM_PC24 ELFMAINZ 00000054 R_ARM_PC24 ELFMAINZ
00000070 R_ARM_PC24 ELFMAINZ 0000007c R_ARM_PC24 ELFMAINZ
0000009c R_ARM_PC24 ELFMAINZ 000000a8 R_ARM_PC24 ELFMAINZ
000000b8 R_ARM_PC24 ELFMAINZ 000000c4 R_ARM_PC24 ELFMAINZ
000000dc R_ARM_PC24 ELFMAINZ 000000e8 R_ARM_PC24 ELFMAINZ
000000e4 R_ARM_PC24 ELFMAINZ 000000f0 R_ARM_PC24 ELFMAINZ
0000011c R_ARM_PC24 ELFMAINZ
00000124 R_ARM_PC24 ELFMAINZ
00000128 R_ARM_PC24 ELFMAINZ 00000128 R_ARM_PC24 ELFMAINZ
00000150 R_ARM_PC24 ELFMAINZ 00000130 R_ARM_PC24 ELFMAINZ
00000160 R_ARM_PC24 ELFMAINZ 00000134 R_ARM_PC24 ELFMAINZ
00000170 R_ARM_PC24 ELFMAINZ 0000015c R_ARM_PC24 ELFMAINZ
00000190 R_ARM_PC24 ELFMAINZ 0000016c R_ARM_PC24 ELFMAINZ
000001ec R_ARM_PC24 ELFMAINZ 00000180 R_ARM_PC24 ELFMAINZ
000001f4 R_ARM_PC24 ELFMAINZ 000001a0 R_ARM_PC24 ELFMAINZ
00000200 R_ARM_PC24 ELFMAINZ
00000208 R_ARM_PC24 ELFMAINZ 00000208 R_ARM_PC24 ELFMAINZ
0000021c R_ARM_PC24 ELFMAINZ 0000021c R_ARM_PC24 ELFMAINZ
00000244 R_ARM_PC24 ELFMAINZ 00000230 R_ARM_PC24 ELFMAINZ
00000258 R_ARM_PC24 ELFMAINZ 00000258 R_ARM_PC24 ELFMAINZ
00000280 R_ARM_PC24 ELFMAINZ 0000026c R_ARM_PC24 ELFMAINZ
00000290 R_ARM_PC24 ELFMAINZ 00000294 R_ARM_PC24 ELFMAINZ
0000029c R_ARM_PC24 ELFMAINZ 000002a4 R_ARM_PC24 ELFMAINZ
000002a8 R_ARM_PC24 ELFMAINZ 000002b0 R_ARM_PC24 ELFMAINZ
000002bc R_ARM_PC24 ELFMAINZ