diff --git a/src/p_lx_elf.cpp b/src/p_lx_elf.cpp index 5caa6130..150e9990 100644 --- a/src/p_lx_elf.cpp +++ b/src/p_lx_elf.cpp @@ -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); } diff --git a/src/stub/src/amd64-linux.elf-entry.S b/src/stub/src/amd64-linux.elf-entry.S index dba9bf0d..ab4f848c 100644 --- a/src/stub/src/amd64-linux.elf-entry.S +++ b/src/stub/src/amd64-linux.elf-entry.S @@ -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 diff --git a/src/stub/src/amd64-linux.elf-so_fold.S b/src/stub/src/amd64-linux.elf-so_fold.S index 886ab670..a40267cb 100644 --- a/src/stub/src/amd64-linux.elf-so_fold.S +++ b/src/stub/src/amd64-linux.elf-so_fold.S @@ -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 diff --git a/src/stub/src/arm.v4a-linux.elf-entry.S b/src/stub/src/arm.v4a-linux.elf-entry.S index ca03d566..3f1c04d1 100644 --- a/src/stub/src/arm.v4a-linux.elf-entry.S +++ b/src/stub/src/arm.v4a-linux.elf-entry.S @@ -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 diff --git a/src/stub/src/arm.v4a-linux.elf-entry.lds b/src/stub/src/arm.v4a-linux.elf-entry.lds index 46cd2c52..e7a54038 100644 --- a/src/stub/src/arm.v4a-linux.elf-entry.lds +++ b/src/stub/src/arm.v4a-linux.elf-entry.lds @@ -9,4 +9,7 @@ SECTIONS *(ELFMAINX) *(.text) } + ELFMAINZ : { + *(ELFMAINZ) + } } diff --git a/src/stub/src/arm.v4a-linux.elf-fold.S b/src/stub/src/arm.v4a-linux.elf-fold.S index c2e16878..90c654f7 100644 --- a/src/stub/src/arm.v4a-linux.elf-fold.S +++ b/src/stub/src/arm.v4a-linux.elf-fold.S @@ -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 diff --git a/src/stub/src/arm.v4a-linux.elf-so_entry.S b/src/stub/src/arm.v4a-linux.elf-so_entry.S index f08099bd..bc443dc6 100644 --- a/src/stub/src/arm.v4a-linux.elf-so_entry.S +++ b/src/stub/src/arm.v4a-linux.elf-so_entry.S @@ -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 diff --git a/src/stub/src/arm64-linux.elf-so_fold.S b/src/stub/src/arm64-linux.elf-so_fold.S index 08c9bf98..b63e2fe3 100644 --- a/src/stub/src/arm64-linux.elf-so_fold.S +++ b/src/stub/src/arm64-linux.elf-so_fold.S @@ -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 diff --git a/src/stub/src/mips.r3000-linux.elf-entry.lds b/src/stub/src/mips.r3000-linux.elf-entry.lds index bf604b3f..00fe7ef6 100644 --- a/src/stub/src/mips.r3000-linux.elf-entry.lds +++ b/src/stub/src/mips.r3000-linux.elf-entry.lds @@ -9,4 +9,7 @@ SECTIONS *(ELFMAINX) *(.text) } + ELFMAINZ : { + *(ELFMAINZ) + } } diff --git a/src/stub/src/mipsel.r3000-linux.elf-entry.S b/src/stub/src/mipsel.r3000-linux.elf-entry.S index 91e7412c..315d5875 100644 --- a/src/stub/src/mipsel.r3000-linux.elf-entry.S +++ b/src/stub/src/mipsel.r3000-linux.elf-entry.S @@ -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 diff --git a/src/stub/src/mipsel.r3000-linux.elf-entry.lds b/src/stub/src/mipsel.r3000-linux.elf-entry.lds index d627978e..9c6d079b 100644 --- a/src/stub/src/mipsel.r3000-linux.elf-entry.lds +++ b/src/stub/src/mipsel.r3000-linux.elf-entry.lds @@ -9,4 +9,7 @@ SECTIONS *(ELFMAINX) *(.text) } + ELFMAINZ : { + *(ELFMAINZ) + } } diff --git a/src/stub/src/mipsel.r3000-linux.elf-fold.S b/src/stub/src/mipsel.r3000-linux.elf-fold.S index 4c674b17..ed34fbbb 100644 --- a/src/stub/src/mipsel.r3000-linux.elf-fold.S +++ b/src/stub/src/mipsel.r3000-linux.elf-fold.S @@ -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 diff --git a/src/stub/src/upxfd_linux.c b/src/stub/src/upxfd_linux.c index d6c860fe..c5144948 100644 --- a/src/stub/src/upxfd_linux.c +++ b/src/stub/src/upxfd_linux.c @@ -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"); \