msync() and UMF_LINUX fiddling

modified:   p_lx_elf.cpp
	modified:   stub/src/amd64-linux.elf-entry.S
	modified:   stub/src/amd64-linux.elf-so_fold.S
	modified:   stub/src/arm.v4a-linux.elf-entry.S
	modified:   stub/src/arm.v4a-linux.elf-entry.lds
	modified:   stub/src/arm.v4a-linux.elf-fold.S
	modified:   stub/src/arm.v4a-linux.elf-so_entry.S
	modified:   stub/src/arm64-linux.elf-so_fold.S
	modified:   stub/src/mips.r3000-linux.elf-entry.lds
	modified:   stub/src/mipsel.r3000-linux.elf-entry.S
	modified:   stub/src/mipsel.r3000-linux.elf-entry.lds
	modified:   stub/src/mipsel.r3000-linux.elf-fold.S
	modified:   stub/src/upxfd_linux.c
This commit is contained in:
John Reiser 2024-12-03 10:36:14 -08:00
parent f7f08be312
commit 68b31d31c3
13 changed files with 53 additions and 33 deletions

View File

@ -1487,14 +1487,10 @@ PackLinuxElf32::buildLinuxLoader(
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "EXP_TAIL");
// End of daisy-chain fall-through.
if (this->e_machine==Elf32_Ehdr::EM_386
|| this->e_machine==Elf32_Ehdr::EM_ARM) {
len += snprintf(&sec[len], sizeof(sec) - len, ",%s",
(opt->o_unix.android_old ? "UMF_ANDROID" : "UMF_LINUX"));
}
else {
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "UMF_LINUX");
}
len += snprintf(&sec[len], sizeof(sec) - len, ",%s",
(sec_arm_attr || is_asl || opt->o_unix.android_shlib)
? "UMF_ANDROID"
: "UMF_LINUX");
if (hasLoaderSection("STRCON")) {
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "STRCON");
}
@ -1536,14 +1532,10 @@ PackLinuxElf32::buildLinuxLoader(
"LZMA_DAISY,LZMA_ELF00,LZMA_DEC20,LZMA_DEC30");
}
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "EXP_TAIL");
if (this->e_machine==Elf32_Ehdr::EM_386
|| this->e_machine==Elf32_Ehdr::EM_ARM) {
len += snprintf(&sec[len], sizeof(sec) - len, ",%s",
(opt->o_unix.android_old ? "UMF_ANDROID" : "UMF_LINUX"));
}
else {
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "UMF_LINUX");
}
len += snprintf(&sec[len], sizeof(sec) - len, ",%s",
(sec_arm_attr || is_asl || opt->o_unix.android_shlib)
? "UMF_ANDROID"
: "UMF_LINUX");
if (hasLoaderSection("SYSCALLS")) {
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "SYSCALLS");
}
@ -1605,13 +1597,9 @@ PackLinuxElf32::buildLinuxLoader(
)
) { // shlib with ELF2 de-compressor
addLoader("ELFMAINX");
if (this->e_machine==Elf32_Ehdr::EM_386
|| this->e_machine==Elf32_Ehdr::EM_ARM) {
addLoader((opt->o_unix.android_old ? "UMF_ANDROID" : "UMF_LINUX"));
}
else {
addLoader("UMF_LINUX");
}
addLoader((sec_arm_attr || is_asl || opt->o_unix.android_shlib)
? "UMF_ANDROID"
: "UMF_LINUX");
addLoader("ELFMAINZ,FOLDEXEC,IDENTSTR");
}
else if (this->e_machine==Elf32_Ehdr::EM_NONE
@ -1621,10 +1609,9 @@ PackLinuxElf32::buildLinuxLoader(
|| this->e_machine==Elf32_Ehdr::EM_MIPS
) { // main program with ELF2 de-compressor
addLoader("ELFMAINX");
if (this->e_machine==Elf32_Ehdr::EM_386
|| this->e_machine==Elf32_Ehdr::EM_ARM) {
addLoader((opt->o_unix.android_old ? "UMF_ANDROID" : "UMF_LINUX"));
}
addLoader((sec_arm_attr || is_asl || opt->o_unix.android_shlib)
? "UMF_ANDROID"
: "UMF_LINUX");
addLoader("ELFMAINZ,FOLDEXEC,IDENTSTR");
defineSymbols(ft);
}

View File

@ -42,6 +42,7 @@ __NR_ftruncate= 77
__NR_exit= 60
__NR_mprotect= 10
__NR_mmap= 9
__NR_msync= 26 // 0x1a
__NR_close= 3
__NR_open= 2
__NR_write= 1

View File

@ -103,6 +103,7 @@ __NR_close= 3
__NR_mmap= 9
__NR_mprotect= 10
__NR_msync= 26 // 0x1a
__NR_munmap= 11
__NR_mremap= 216
__NR_memfd_create= 0x13f // 319
@ -169,6 +170,8 @@ ftruncate: .globl ftruncate
push $__NR_ftruncate; 5: jmp 5f
memfd_create: .globl memfd_create
push $__NR_memfd_create; 5: jmp 5f
msync: .globl msync
push $__NR_msync; 5: jmp 5f
close: .globl close
push $ __NR_close; 5: jmp 5f
openat: .globl openat

View File

@ -93,6 +93,7 @@ __NR_memfd_create= 385 + __NR_SYSCALL_BASE
__NR_mkdir= 39 + __NR_SYSCALL_BASE
__NR_mmap2= 192 + __NR_SYSCALL_BASE
__NR_mprotect=125 + __NR_SYSCALL_BASE
__NR_msync= 144 + __NR_SYSCALL_BASE // 0x90
__NR_open= 5 + __NR_SYSCALL_BASE
__NR_read= 3 + __NR_SYSCALL_BASE
__NR_stat= 106 + __NR_SYSCALL_BASE
@ -377,6 +378,7 @@ mempcpy: .globl mempcpy // (dst, src, n)
.globl getpid; getpid: do_sys __NR_getpid; ret
.globl lseek; lseek: do_sys __NR_lseek; ret
.globl mkdir; mkdir: do_sys __NR_mkdir; ret
.globl msync; msync: do_sys __NR_msync; ret
.globl open; open: do_sys __NR_open; ret
.globl read; read: do_sys __NR_read; ret
.globl stat; stat: do_sys __NR_stat; ret

View File

@ -9,4 +9,7 @@ SECTIONS
*(ELFMAINX)
*(.text)
}
ELFMAINZ : {
*(ELFMAINZ)
}
}

View File

@ -472,6 +472,7 @@ getpid:.globl getpid;do_sys __NR_getpid; ret
lseek: .globl lseek; do_sys __NR_lseek; ret
memfd_create: .globl memfd_create; do_sys2 __NR_memfd_create; ret
mkdir: .globl mkdir; do_sys __NR_mkdir; ret
msync: .globl msync; do_sys __NR_msync; ret
munmap: .globl munmap; do_sys __NR_munmap; ret // BEWARE: get_sys_munmap knows where 'svc' lives!
open: .globl open; do_sys __NR_open; ret
read: .globl read; do_sys __NR_read; ret

View File

@ -246,6 +246,7 @@ f_decompress:
// MATCH_07 envp
upx_mmap_and_fd: .globl upx_mmap_and_fd
// UMF_LINX or UMF_ANDROID goes here
// IDENTSTR goes here

View File

@ -103,6 +103,7 @@ __NR_unlinkat = 0x23 + __NR_SYSCALL_BASE // 35
__NR_mmap = 0xde + __NR_SYSCALL_BASE // 222
__NR_mprotect = 0xe2 + __NR_SYSCALL_BASE // 226
__NR_msync = 0xe3 + __NR_SYSCALL_BASE // 227
__NR_munmap = 0xd7 + __NR_SYSCALL_BASE // 215
__NR_memfd_create = 0x117 + __NR_SYSCALL_BASE // 279
__NR_ftruncate= 0x2e + __NR_SYSCALL_BASE // 46
@ -154,6 +155,9 @@ Punmap: .globl Punmap
munmap: .globl munmap
do_sys __NR_munmap; ret
msync: .globl msync
do_sys __NR_msync; ret
// Sometimes Linux enforces page-aligned address
Pprotect: .globl Pprotect
mprotect: .globl mprotect

View File

@ -9,4 +9,7 @@ SECTIONS
*(ELFMAINX)
*(.text)
}
ELFMAINZ : {
*(ELFMAINZ)
}
}

View File

@ -114,9 +114,11 @@ M_NRV2E_LE32=8
__NR_Linux = 4000
__NR_close = 6+ __NR_Linux
__NR_exit = 1+ __NR_Linux
__NR_memfd_create= 354 + __NR_Linux
__NR_ftruncate= 93+ __NR_Linux
__NR_memfd_create= 354+ __NR_Linux
__NR_mmap = 90+ __NR_Linux
__NR_mprotect = 125+ __NR_Linux
__NR_msync = 144+ __NR_LINUX
__NR_open = 5+ __NR_Linux
__NR_write = 4+ __NR_Linux
__NR_cacheflush = 147+ __NR_Linux
@ -185,10 +187,9 @@ eof_n2b:
addiu sp,4
.balign 4
upx_mmap_and_fd:
// section UMF_LINUX or UMF_ANDROID goes here
upx_mmap_and_fd: .globl upx_mmap_and_fd
// section UMF_LINUX or UMF_ANDROID goes here
section ELFMAINZ
section ELFMAINZ; .set noreorder
L72:
li a0,2 # fd stderr
@ -261,6 +262,16 @@ unfold: # IN: $r_fexp,$r_auxv,$r_PMASK,$r_FOLD
jr ra
addu $r_ADRX,$r_elfa,$r_ADRX # compressed data
memfd_create: .globl memfd_create
li v0,__NR_memfd_create; syscall
j ra
nop
ftruncate: .globl ftruncate
li v0,__NR_ftruncate; syscall
j ra
nop
zfind: # result in $r_auxv
lw v1,(a0); addiu a0,a0,NBPW
bnez v1,zfind

View File

@ -9,4 +9,7 @@ SECTIONS
*(ELFMAINX)
*(.text)
}
ELFMAINZ : {
*(ELFMAINZ)
}
}

View File

@ -391,7 +391,8 @@ mmap: .globl mmap
b sysret
addiu sp,sp,sp_frame
sysgo:
sysgo: // src/mipsel.r3000-linux.elf-fold.S
sysgo2:
syscall
sysret:
bgez a3,sysOK

View File

@ -30,7 +30,7 @@ extern void my_bkpt(void const *, ...);
#define ANDROID_FRIEND 0
#define addr_string(string) ({ \
char const *str; \
asm("bal 0f; .asciz \"" string "\"; .balign 4\n0: move %0,$31" \
asm(".set noreorder; bal 0f; .asciz \"" string "\"; .balign 4\n0: move %0,$31; .set reorder" \
/*out*/ : "=r"(str) \
/* in*/ : \
/*und*/ : "ra"); \