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

--android-shlib for Android "Oreo"

Android "Oreo" wants to deal with ElfXX_Shdr[].  So splice 4KiB into PT_LOAD[0]
with an "extra" copy of them.  This requires careful relocation processing.
It also costs a page at run time.
	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
	modified:   stub/src/arm64-linux.shlib-init.S
This commit is contained in:
John Reiser 2018-01-13 09:20:04 -08:00
parent c7969ed5a5
commit 8c84efb8fb
10 changed files with 1300 additions and 1280 deletions

View File

@ -148,6 +148,7 @@
DT_NULL = 0, /* End flag */
DT_NEEDED = 1, /* Name of needed library */
DT_PLTRELSZ = 2, /* Size in bytes of PLT relocs */
DT_PLTGOT = 3, /* Processor defined value */
DT_HASH = 4, /* Hash table of symbol names */
DT_STRTAB = 5, /* String table */
DT_SYMTAB = 6, /* Symbol table */
@ -155,6 +156,7 @@
DT_RELASZ = 8, /* Total size of Rela relocs */
DT_RELAENT = 9, /* Size of one RELA relocation */
DT_INIT = 12, /* Address of init function */
DT_FINI = 13, /* Address of termination function */
DT_REL = 17, /* Relocations which contain no addend */
DT_RELSZ = 18, /* Total size of Rel relocs */
DT_RELENT = 19, /* Size of one Rel relocation */
@ -213,15 +215,23 @@
#ifdef WANT_REL_ENUM //{
static unsigned ELF32_R_TYPE(unsigned x) { return 0xff & x; }
static unsigned ELF32_R_TYPE(unsigned x) { return 0xff & x; }
static unsigned ELF64_R_TYPE(upx_uint64_t x) { return 0xffffffff & x; }
enum { // R_*_RELATIVE relocation types: Adjust by program base
enum { // relocation types
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
R_X86_64_RELATIVE = 8,
R_386_JMP_SLOT = 7,
R_AARCH64_JUMP_SLOT = 1026,
R_ARM_JUMP_SLOT = 22,
R_PPC_JMP_SLOT = 21,
R_PPC64_JMP_SLOT = R_PPC_JMP_SLOT,
R_X86_64_JUMP_SLOT = 7
};
#endif //}

View File

@ -2560,9 +2560,11 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
Elf32_Dyn *dyn = const_cast<Elf32_Dyn *>(dynseg);
for (; dyn->d_tag; ++dyn) {
unsigned d_tag = get_te32(&dyn->d_tag);
if (Elf32_Dyn::DT_INIT_ARRAY == d_tag
if (Elf32_Dyn::DT_FINI == d_tag
|| Elf32_Dyn::DT_FINI_ARRAY == d_tag
|| Elf32_Dyn::DT_PREINIT_ARRAY == d_tag) {
|| Elf32_Dyn::DT_INIT_ARRAY == d_tag
|| Elf32_Dyn::DT_PREINIT_ARRAY == d_tag
|| Elf32_Dyn::DT_PLTGOT == d_tag) {
unsigned d_val = get_te32(&dyn->d_val);
set_te32(&dyn->d_val, asl_delta + d_val);
}
@ -2646,9 +2648,11 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
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);
unsigned r_type = ELF32_R_TYPE(r_info);
if (xct_off <= w
&& Elf32_Ehdr::EM_ARM == e_machine
&& R_ARM_RELATIVE == ELF32_R_TYPE(r_info)) {
&& ( R_ARM_RELATIVE == r_type
|| R_ARM_JUMP_SLOT == r_type)) {
set_te32(&file_image[d], asl_delta + w);
}
}
@ -2862,9 +2866,11 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
Elf64_Dyn *dyn = const_cast<Elf64_Dyn *>(dynseg);
for (; dyn->d_tag; ++dyn) {
uint64_t d_tag = get_te64(&dyn->d_tag);
if (Elf64_Dyn::DT_INIT_ARRAY == d_tag
if (Elf64_Dyn::DT_FINI == d_tag
|| Elf64_Dyn::DT_FINI_ARRAY == d_tag
|| Elf64_Dyn::DT_PREINIT_ARRAY == d_tag) {
|| Elf64_Dyn::DT_INIT_ARRAY == d_tag
|| Elf64_Dyn::DT_PREINIT_ARRAY == d_tag
|| Elf64_Dyn::DT_PLTGOT == d_tag) {
uint64_t d_val = get_te64(&dyn->d_val);
set_te64(&dyn->d_val, asl_delta + d_val);
}
@ -2944,6 +2950,17 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
if (xct_off <= r_offset) {
set_te64(&rel->r_offset, asl_delta + r_offset);
}
// r_offset must be in 2nd PT_LOAD; .p_vaddr was already relocated
upx_uint64_t d = elf_get_offset_from_address(asl_delta + r_offset);
upx_uint64_t w = get_te64(&file_image[d]);
upx_uint64_t r_info = get_te32(&rel->r_info);
unsigned r_type = ELF64_R_TYPE(r_info);
if (xct_off <= w
&& Elf64_Ehdr::EM_AARCH64 == e_machine
&& ( R_AARCH64_RELATIVE == r_type
|| R_AARCH64_JUMP_SLOT == r_type)) {
set_te64(&file_image[d], asl_delta + w);
}
}
fo->seek(sh_offset, SEEK_SET);
fo->rewrite(sh_offset + file_image, sh_size);

File diff suppressed because it is too large Load Diff

View File

@ -32,14 +32,14 @@
#define STUB_ARM_V5T_LINUX_SHLIB_INIT_SIZE 15566
#define STUB_ARM_V5T_LINUX_SHLIB_INIT_ADLER32 0xf865201e
#define STUB_ARM_V5T_LINUX_SHLIB_INIT_CRC32 0xc1cd6d32
#define STUB_ARM_V5T_LINUX_SHLIB_INIT_ADLER32 0x0d0d1f66
#define STUB_ARM_V5T_LINUX_SHLIB_INIT_CRC32 0x43a6af0c
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,
/* 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,
/* 0x0030 */ 0, 0, 0, 0, 1,222,255,181, 0,161, 28, 57, 11,104, 13, 29,
/* 0x0030 */ 0, 0, 0, 0,192, 70,255,181, 0,161, 28, 57, 11,104, 13, 29,
/* 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,
/* 0x0060 */ 27, 13,228, 24, 16,180,192, 26, 1,180,228, 26,192, 24,155, 8,
@ -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,
/* 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,
/* 0x1340 */ 11,188,158, 70, 5, 34,125, 39, 0,223, 91, 39, 1,222, 3,189,
/* 0x1340 */ 11,188,158, 70, 5, 34,125, 39, 0,223, 91, 39, 3,189, 0, 0,
/* 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,
/* 0x1370 */ 29, 66,247,209,237, 8, 13,208,120, 71,192, 70,212, 3, 45,233,

File diff suppressed because it is too large Load Diff

View File

@ -79,14 +79,14 @@ __ARM_NR_cacheflush = 2 + __ARM_NR_BASE
#define ecx r5
section ELFMAINX
// .long distance back to first b_info
// .long offset(b_info) src of f_exp
// .long offset(.) // detect relocation
// .long offset(user DT_INIT)
// .long offset(escape_hatch)
// .long offset(xct_off) dst of f_exp
_start: .globl _start
bkpt // nop //; bkpt // for debugging
nop //; bkpt // for debugging
stmdb sp!,{arg1,arg2,arg3, eax,ecx,r6,r7, fp,lr,pc}
mov fp,sp
o_uinit= (3+4+2)*4 // pc
@ -295,7 +295,6 @@ supervise:
bl movsl
ldmia sp!,{arg1,arg2,arg3,arg4, eax}
bkpt
blx eax // decompress
add sp,sp,#4 // toss arg5
@ -343,7 +342,6 @@ L620: // Implant escape hatch at end of .text
add r7,r7,#__NR_munmap - ((__NR_munmap>>16)<<16)
.endif
#endif //}
bkpt
ldmia sp!,{arg1,arg2, pc} // goto hatch
movsl_subr:

View File

@ -112,14 +112,14 @@ _=-1+_ // one less word on stack
#define lodslu bl get4u
section ELFMAINX
// .long offset(b_info)
// .long offset(b_info) src for f_exp
//D_INFO:
// .long offset(.) // detect relocation
// .long offset(user DT_INIT)
// .long offset(escape_hatch) // override with round_up(2, PT_LOAD[0]{.p_memsz + .p_vaddr})
// .long offset(dst for f_exp)
#define DEBUG 1
#define DEBUG 0
.code 16 //; .balign 4
.real_start_ofELFMAINX:
.thumb_func
@ -133,7 +133,7 @@ _start: .globl _start
_=9
o_uinit= 1 // lr
adr esi,here
sub esi,#4*2 + 5*NBPW // -NBPW + &D_INFO
sub esi,#(here - _start) + 5*NBPW // -NBPW + &D_INFO
here:
ldr tmp,[esi,#0*NBPW] // offset(b_info)
add ecx,esi,#NBPW // &D_INFO
@ -141,7 +141,7 @@ here:
// reloc DT_INIT for step 12
ldr eax,[esi,#2*NBPW]; add eax,ecx; str eax,[SP(o_uinit)]
ldr edi,[esi,#4*NBPW]; add edi,ecx // dst for f_exp
add esi,tmp,ecx // &b_info
add esi,tmp,ecx // &b_info src for f_exp
sub sp,#3*NBPW // 3 slots of space
_=1+_ // &escape_hatch (step 10)
@ -369,7 +369,6 @@ _=-3+_ // 12
lsl r7,#16
add r7,#__NR_munmap - ((__NR_munmap>>16)<<16)
.endif
bkpt_th
pop {arg1,arg2, pc} // goto hatch
_=-3+_ // 9
.balign 4

View File

@ -120,13 +120,13 @@ sp_frame = 24 * NBPW
.endm
section ELFMAINX
// .long distance back to first b_info
// .long offset(b_info) src of f_exp
// .long offset(.) // detect relocation
// .long offset(user DT_INIT)
// .long offset(escape_hatch)
// .long offset(xct_off) dst of f_exp
_start: .globl _start
brk #0 // debugging
// brk #0 // debugging
slot f_argc // 0
slot f_argv // 1
@ -219,15 +219,14 @@ main:
#define lodsl ldr eax,[rsi],#4
#define lodslu lodsl
lodsl; mov tmp,eax // distance back to 1st b_info
lodsl; mov tmp,eax // offset(b_info)
mov rcx,rsi
lodsl; sub rcx,rcx,rax; //str ecx,[sp,#o_reloc]
sub tmpx,rsi,tmpx // &b_info
lodsl; add rax,rcx,rax; str rax,[sp,#f_uinit] // reloc DT_INIT for step 12
lodsl; add rax,rax,rcx; str rax,[sp,#f_uinit] // reloc DT_INIT for step 12
slot o_hatch // 6
lodsl; add rax,rcx,rax; str rax,[sp,#o_hatch] // reloc &hatch for step 10
lodsl; add rdi,rcx,rax // destination for decompress
mov rsi,tmpx // &b_info
lodsl; add rax,rax,rcx; str rax,[sp,#o_hatch] // reloc &hatch for step 10
lodsl; add rdi,rax,rcx // destination for decompress
add rsi,tmpx,rcx // &b_info src for f_exp
slot p_unmap,2 // 7
@ -321,7 +320,6 @@ supervise:
ldr rax,[sp,#o_uncpr]
ldp arg1,arg2,[sp,#0*NBPW + p_uncpr]
ldp arg3,arg4,[sp,#2*NBPW + p_uncpr]
brk #0
blr rax // decompress
bl L620
@ -353,7 +351,6 @@ L620: // Implant escape hatch at end of .text
ldp arg1,arg2,[sp,#0*NBPW + p_unmap]
mov w8,#__NR_munmap
ldp arg3,arg4,[sp,#2*NBPW + f_argc] // f_uinit
brk #0
br arg5 // goto hatch
movsl_subr:

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
9 LZMA_DEC30 00000000 00000000 00000000 000011fc 2**0 CONTENTS, READONLY
10 ELFMAINY 0000003e 00000000 00000000 000011fc 2**0 CONTENTS, READONLY
11 ELFMAINZ 00000310 00000000 00000000 0000123a 2**0 CONTENTS, RELOC, READONLY
11 ELFMAINZ 00000308 00000000 00000000 0000123a 2**0 CONTENTS, RELOC, READONLY
SYMBOL TABLE:
00000000 l d NRV2E 00000000 NRV2E
00000000 l d NRV2D 00000000 NRV2D
@ -256,15 +256,15 @@ OFFSET TYPE VALUE
00000134 R_ARM_PC24 ELFMAINZ
0000015c R_ARM_PC24 ELFMAINZ
0000016c R_ARM_PC24 ELFMAINZ
00000180 R_ARM_PC24 ELFMAINZ
000001a0 R_ARM_PC24 ELFMAINZ
0000017c R_ARM_PC24 ELFMAINZ
0000019c R_ARM_PC24 ELFMAINZ
000001f8 R_ARM_PC24 ELFMAINZ
00000200 R_ARM_PC24 ELFMAINZ
00000208 R_ARM_PC24 ELFMAINZ
0000021c R_ARM_PC24 ELFMAINZ
00000230 R_ARM_PC24 ELFMAINZ
00000258 R_ARM_PC24 ELFMAINZ
0000026c R_ARM_PC24 ELFMAINZ
00000294 R_ARM_PC24 ELFMAINZ
000002a4 R_ARM_PC24 ELFMAINZ
000002b0 R_ARM_PC24 ELFMAINZ
000002bc R_ARM_PC24 ELFMAINZ
00000214 R_ARM_PC24 ELFMAINZ
00000228 R_ARM_PC24 ELFMAINZ
00000250 R_ARM_PC24 ELFMAINZ
00000264 R_ARM_PC24 ELFMAINZ
0000028c R_ARM_PC24 ELFMAINZ
0000029c R_ARM_PC24 ELFMAINZ
000002a8 R_ARM_PC24 ELFMAINZ
000002b4 R_ARM_PC24 ELFMAINZ

View File

@ -2,21 +2,21 @@ file format elf64-littleaarch64
Sections:
Idx Name Size VMA LMA File off Algn Flags
0 ELFMAINX 00000014 0000000000000000 0000000000000000 00000040 2**0 CONTENTS, RELOC, READONLY
1 NRV_HEAD 00000000 0000000000000000 0000000000000000 00000054 2**0 CONTENTS, READONLY
2 NRV_TAIL 00000000 0000000000000000 0000000000000000 00000054 2**0 CONTENTS, READONLY
3 NRV2E 00000128 0000000000000000 0000000000000000 00000054 2**0 CONTENTS, READONLY
4 NRV2D 0000011c 0000000000000000 0000000000000000 0000017c 2**0 CONTENTS, READONLY
5 NRV2B 000000f0 0000000000000000 0000000000000000 00000298 2**0 CONTENTS, READONLY
6 LZMA_ELF00 000000d0 0000000000000000 0000000000000000 00000388 2**0 CONTENTS, RELOC, READONLY
7 LZMA_DEC20 00000968 0000000000000000 0000000000000000 00000458 2**0 CONTENTS, READONLY
8 LZMA_DEC10 0000049c 0000000000000000 0000000000000000 00000dc0 2**0 CONTENTS, READONLY
9 LZMA_DEC30 00000000 0000000000000000 0000000000000000 0000125c 2**0 CONTENTS, READONLY
10 ELFMAINY 0000003e 0000000000000000 0000000000000000 0000125c 2**0 CONTENTS, READONLY
11 ELFMAINZ 00000000 0000000000000000 0000000000000000 0000129a 2**0 CONTENTS, READONLY
12 ELFMAJNZ 00000024 0000000000000000 0000000000000000 0000129a 2**0 CONTENTS, READONLY
13 ANDMAJNZ 00000004 0000000000000000 0000000000000000 000012be 2**0 CONTENTS, READONLY
14 ELFMAKNZ 000002a4 0000000000000000 0000000000000000 000012c2 2**0 CONTENTS, READONLY
0 ELFMAINX 00000010 0000000000000000 0000000000000000 00000040 2**0 CONTENTS, RELOC, READONLY
1 NRV_HEAD 00000000 0000000000000000 0000000000000000 00000050 2**0 CONTENTS, READONLY
2 NRV_TAIL 00000000 0000000000000000 0000000000000000 00000050 2**0 CONTENTS, READONLY
3 NRV2E 00000128 0000000000000000 0000000000000000 00000050 2**0 CONTENTS, READONLY
4 NRV2D 0000011c 0000000000000000 0000000000000000 00000178 2**0 CONTENTS, READONLY
5 NRV2B 000000f0 0000000000000000 0000000000000000 00000294 2**0 CONTENTS, READONLY
6 LZMA_ELF00 000000d0 0000000000000000 0000000000000000 00000384 2**0 CONTENTS, RELOC, READONLY
7 LZMA_DEC20 00000968 0000000000000000 0000000000000000 00000454 2**0 CONTENTS, READONLY
8 LZMA_DEC10 0000049c 0000000000000000 0000000000000000 00000dbc 2**0 CONTENTS, READONLY
9 LZMA_DEC30 00000000 0000000000000000 0000000000000000 00001258 2**0 CONTENTS, READONLY
10 ELFMAINY 0000003e 0000000000000000 0000000000000000 00001258 2**0 CONTENTS, READONLY
11 ELFMAINZ 00000000 0000000000000000 0000000000000000 00001296 2**0 CONTENTS, READONLY
12 ELFMAJNZ 00000024 0000000000000000 0000000000000000 00001296 2**0 CONTENTS, READONLY
13 ANDMAJNZ 00000004 0000000000000000 0000000000000000 000012ba 2**0 CONTENTS, READONLY
14 ELFMAKNZ 00000298 0000000000000000 0000000000000000 000012be 2**0 CONTENTS, READONLY
SYMBOL TABLE:
0000000000000000 l d LZMA_DEC30 0000000000000000 LZMA_DEC30
0000000000000000 l d ELFMAINZ 0000000000000000 ELFMAINZ
@ -42,7 +42,7 @@ SYMBOL TABLE:
RELOCATION RECORDS FOR [ELFMAINX]:
OFFSET TYPE VALUE
0000000000000010 R_AARCH64_CALL26 ELFMAINZ
000000000000000c R_AARCH64_CALL26 ELFMAINZ
RELOCATION RECORDS FOR [LZMA_ELF00]:
OFFSET TYPE VALUE