Fix stubs in arm,mips,powerpc for upx_mmap_and_fd

modified:   stub/Makefile
	modified:   stub/src/arm.v4a-expand.S
	modified:   stub/src/arm.v4a-linux.elf-entry.S
	modified:   stub/src/mips.r3000-expand.S
	modified:   stub/src/mipsel.r3000-linux.elf-entry.S
	modified:   stub/src/powerpc-linux.elf-fold.S
	modified:   stub/src/upxfd_linux.c
This commit is contained in:
John Reiser 2024-11-19 08:44:48 -08:00
parent 5bf2258505
commit 3798dde3aa
7 changed files with 137 additions and 14 deletions

View File

@ -591,12 +591,13 @@ arm.v4a-linux.elf-so_entry.h : src/arm.v4a-linux.elf-so_entry.lds \
tmp/arm.v4a-linux.elf-upxfd_android.o : $(srcdir)/src/upxfd_android.c
$(call tc,gcc) -c -O $< -o $@
$(call tc,objcopy) --rename-section .text=UMF_ANDROID -R .comment -R .data -R .bss -R .note.GNU-stack $@
$(call tc,objcopy) -R .comment $@
$(call tc,objdump) -Dr $(tc_objdump_disasm_options) $@ | $(RTRIM) > $@.disasm
tmp/arm.v4a-linux.elf-upxfd_linux.o : $(srcdir)/src/upxfd_linux.c
$(call tc,gcc) -c -O $< -o $@
$(call tc,objcopy) -R .comment $@
$(call tc,objcopy) --rename-section .text=UMF_LINUX -R .comment -R .data -R .bss -R .note.GNU-stack $@
$(call tc,objdump) -Dr $(tc_objdump_disasm_options) $@ | $(RTRIM) > $@.disasm
arm.v4a-linux.elf-fold.h : $(srcdir)/src/$$T.lds \
@ -707,12 +708,12 @@ arm.v5a-linux.elf-so_entry.h : src/arm.v5a-linux.elf-so_entry.lds \
tmp/arm.v5a-linux.elf-upxfd_android.o : $(srcdir)/src/upxfd_android.c
$(call tc,gcc) -c -O $< -o $@
$(call tc,objcopy) -R .comment $@
$(call tc,objcopy) --rename-section .text=UMF_ANDROID -R .comment -R .data -R .bss -R .note.GNU-stack $@
$(call tc,objdump) -Dr $(tc_objdump_disasm_options) $@ | $(RTRIM) > $@.disasm
tmp/arm.v5a-linux.elf-upxfd_linux.o : $(srcdir)/src/upxfd_linux.c
$(call tc,gcc) -c -O $< -o $@
$(call tc,objcopy) -R .comment $@
$(call tc,objcopy) --rename-section .text=UMF_LINUX -R .comment -R .data -R .bss -R .note.GNU-stack $@
$(call tc,objdump) -Dr $(tc_objdump_disasm_options) $@ | $(RTRIM) > $@.disasm
arm.v5a-linux.elf-fold.h : $(srcdir)/src/$$T.lds \
@ -940,12 +941,12 @@ armeb.v4a-linux.elf-fold.h : $(srcdir)/src/$$T.lds \
tmp/armeb.v4a-linux.elf-upxfd_android.o : $(srcdir)/src/upxfd_android.c
$(call tc,gcc) -c -O $< -o $@
$(call tc,objcopy) -R .comment $@
$(call tc,objcopy) --rename-section .text=UMF_ANDROID -R .comment -R .data -R .bss -R .note.GNU-stack $@
$(call tc,objdump) -Dr $(tc_objdump_disasm_options) $@ | $(RTRIM) > $@.disasm
tmp/armeb.v4a-linux.elf-upxfd_linux.o : $(srcdir)/src/upxfd_linux.c
$(call tc,gcc) -c -O $< -o $@
$(call tc,objcopy) -R .comment $@
$(call tc,objcopy) --rename-section .text=UMF_LINUX -R .comment -R .data -R .bss -R .note.GNU-stack $@
$(call tc,objdump) -Dr $(tc_objdump_disasm_options) $@ | $(RTRIM) > $@.disasm
tmp/armeb.v4a-expand.o: $(srcdir)/src/$$T.S
@ -1553,19 +1554,27 @@ mips.r3000-linux.elf-entry.h : $(srcdir)/src/mips.r3000-linux.elf-entry.lds \
tmp/mips.r3000-linux.elf-upxfd_android.o : $(srcdir)/src/upxfd_android.c
@echo; echo TARGET: $@; echo
$(call tc,gcc) -c -O $< -o $@
$(call tc,objcopy) -R .comment $@
$(call tc,objcopy) --rename-section .text=UMF_ANDROID -R .comment -R .data -R .bss -R .note.GNU-stack $@
$(call tc,objdump) -Dr $(tc_objdump_disasm_options) $@ | $(RTRIM) > $@.disasm
tmp/mips.r3000-linux.elf-upxfd_linux.o : $(srcdir)/src/upxfd_linux.c
@echo; echo TARGET: $@; echo
$(call tc,gcc) -c -O $< -o $@
$(call tc,objcopy) --rename-section .text=UMF_LINUX -R .comment -R .data -R .bss -R .note.GNU-stack $@
$(call tc,objdump) -Dr $(tc_objdump_disasm_options) $@ | $(RTRIM) > $@.disasm
mips.r3000-linux.elf-fold.h : $(srcdir)/src/$$T.lds \
$(srcdir)/src/$$T.S \
tmp/mips.r3000-linux.elf-main2.s \
tmp/mips.r3000-linux.upxfd_android.s \
tmp/mips.r3000-linux.upxfd_linux.s \
src/mips.r3000-expand.S
@echo; echo TARGET: $@; echo
cat > tmp/mips.r3000-linux.elf-fold-2.S \
src/mips.r3000-linux.elf-fold.S \
tmp/mips.r3000-linux.elf-main2.s \
tmp/mips.r3000-linux.upxfd_android.s \
tmp/mips.r3000-linux.upxfd_linux.s \
src/mips.r3000-expand.S
(cd tmp; ln -sf ../src/arch arch)
$(call tc,gcc) -E \
@ -1593,6 +1602,17 @@ tmp/mips.r3000-linux.upxfd_android.s: $(srcdir)/src/upxfd_android.c
-e 's/ jal[ ][ ]*\([^\$$]\)/ bal \1/' \
>$@
tmp/mips.r3000-linux.upxfd_linux.s: $(srcdir)/src/upxfd_linux.c
@echo; echo TARGET: $@; echo
$(call tc,gcc) -S -D__mips__ \
-D_TARGET_LINUX_ -DNO_WANT_MMAP -O $< -o - \
| sed -e '/^\t\.file\t/d' \
-e 's/L[0-9]*/L8&/g' \
-e $$(cat src/dollar-dollar.sed) \
-e 's/ j[ ][ ]*$$L/ b $$L/' \
-e 's/ jal[ ][ ]*\([^\$$]\)/ bal \1/' \
>$@
tmp/mips.r3000-linux.elf-main2.s: $(srcdir)/src/$$T.c $(srcdir)/src/i386-linux.elf-main2.c
@echo; echo TARGET: $@; echo
$(call tc,gcc) -S -D__mips__ -D_TARGET_LINUX_ -O $< -o - \
@ -1665,12 +1685,14 @@ mipsel.r3000-linux.elf-fold.h : $(srcdir)/src/$$T.lds \
$(srcdir)/src/$$T.S \
tmp/mipsel.r3000-linux.elf-main2.s \
tmp/mipsel.r3000-linux.upxfd_android.s \
tmp/mipsel.r3000-linux.upxfd_linux.s \
src/mipsel.r3000-expand.S
@echo; echo TARGET: $@; echo
cat > tmp/mipsel.r3000-linux.elf-fold-2.S \
src/mipsel.r3000-linux.elf-fold.S \
tmp/mipsel.r3000-linux.elf-main2.s \
tmp/mipsel.r3000-linux.upxfd_android.s \
tmp/mipsel.r3000-linux.upxfd_linux.s \
src/mipsel.r3000-expand.S
(cd tmp; ln -sf ../src/arch arch)
$(call tc,gcc) -E \
@ -1698,6 +1720,17 @@ tmp/mipsel.r3000-linux.upxfd_android.s: $(srcdir)/src/upxfd_android.c
-e 's/ jal[ ][ ]*\([^\$$]\)/ bal \1/' \
>$@
tmp/mipsel.r3000-linux.upxfd_linux.s: $(srcdir)/src/upxfd_linux.c
@echo; echo TARGET: $@; echo
$(call tc,gcc) -S -D__mipsel__ \
-D_TARGET_LINUX_ -DNO_WANT_MMAP -O $< -o - \
| sed -e '/^\t\.file\t/d' \
-e 's/L[0-9]*/L8&/g' \
-e $$(cat src/dollar-dollar.sed) \
-e 's/ j[ ][ ]*$$L/ b $$L/' \
-e 's/ jal[ ][ ]*\([^\$$]\)/ bal \1/' \
>$@
tmp/mipsel.r3000-linux.elf-main2.s: $(srcdir)/src/$$T.c $(srcdir)/src/i386-linux.elf-main2.c
@echo; echo TARGET: $@; echo
$(call tc,gcc) -S -D__mipsel__ -D_TARGET_LINUX_ -O $< -o - \
@ -1827,7 +1860,7 @@ powerpc-linux.elf-entry.h : $(srcdir)/src/$$T.S
powerpc-linux.elf-fold.h : $(srcdir)/src/$$T.lds \
tmp/$$T.o \
tmp/powerpc-linux.elf-upxfd_android.o \
tmp/powerpc-linux.elf-upxfd_linux.o \
tmp/powerpc-expand.o \
tmp/powerpc-linux.elf-main2.o
$(call tc,ld) -r -T $(srcdir)/src/$T.lds -Map tmp/$T.map $(filter %.o,$^) -o tmp/$T.bin
@ -1842,9 +1875,9 @@ tmp/powerpc-linux.elf-main2.o : $(srcdir)/src/$$T.c
$(call tc,gcc) -c $< -o $@
$(call tc,f-objstrip,$@)
tmp/powerpc-linux.elf-upxfd_android.o : $(srcdir)/src/upxfd_android.c
tmp/powerpc-linux.elf-upxfd_linux.o : $(srcdir)/src/upxfd_linux.c
$(call tc,gcc) -c -O $< -o $@
$(call tc,objcopy) -R .comment $@
$(call tc,objcopy) -R .comment -R .data -R .bss -R .note.GNU-stack $@
$(call tc,objdump) -Dr $(tc_objdump_disasm_options) $@ | $(RTRIM) > $@.disasm
tmp/powerpc-expand.o: $(srcdir)/src/$$T.S

View File

@ -179,6 +179,10 @@ eof: // end of a compressed extent
.unreq dst
.unreq tmp
.balign 4
upx_mmap_and_fd: .globl upx_mmap_and_fd
// UMF_ANDROID or UMF_LINUX must be loaded after EXP_TAIL
#define M_NRV2B_LE32 2
#define M_NRV2B_8 3
#define M_NRV2D_LE32 5

View File

@ -270,6 +270,10 @@ f_expand:
#define NO_METHOD_CHECK 1
#include "arch/arm/v4a/nrv2b_d8.S"
.balign 4
upx_mmap_and_fd:
// section UMF_LINUX or UMF_ANDROID goes here
section ELFMAINY
end_decompress: .globl end_decompress
/* IDENTSTR goes here */

View File

@ -154,6 +154,10 @@ eof_n2b: .globl eof_n2b
go_decompr:
// sections NRV2B, etc, inserted here by addLoader() from ::buildLinuxLoader()
.balign 4
upx_mmap_and_fd: .globl upx_mmap_and_fd
// UMF_ANDROID or UMF_LINUX must be loaded after EXP_TAIL
section EXP_TAIL
#define M_NRV2B_LE32 2
#define M_NRV2B_8 3

View File

@ -184,6 +184,11 @@ eof_n2b:
jr ra
addiu sp,4
.balign 4
upx_mmap_and_fd:
// section UMF_LINUX or UMF_ANDROID goes here
section ELFMAINZ
section ELFMAINZ; .set noreorder
L72:
li a0,2 # fd stderr

View File

@ -268,6 +268,10 @@ sz_auxv= 2*NBPW
bctr // goto escape hatch
section SYSCALLS
upx_mmap_and_fd: .globl upx_mmap_and_fd
b upx_mmap_and_fd_linux
SYS_exit= 1
SYS_fork= 2
SYS_read= 3

View File

@ -6,6 +6,76 @@
All Rights Reserved.
*/
#if defined(__i386__) //}{
#define ANDROID_FRIEND 1
#define addr_string(string) ({ \
char const *str; \
asm("call 0f; .asciz \"" string "\"; 0: pop %0" \
/*out*/ : "=r"(str) ); \
str; \
})
#elif defined(__arm__) //}{
#define ANDROID_FRIEND 1
#define addr_string(string) ({ \
char const *str; \
asm("bl 0f; .string \"" string "\"; .balign 4; 0: mov %0,lr" \
/*out*/ : "=r"(str) \
/* in*/ : \
/*und*/ : "lr"); \
str; \
})
#elif defined(__mips__) //}{
#define ANDROID_FRIEND 0
#define addr_string(string) ({ \
char const *str; \
asm("bal 0f; .asciz \"" string "\"; .balign 4\n0: move %0,$31" \
/*out*/ : "=r"(str) \
/* in*/ : \
/*und*/ : "ra"); \
str; \
})
#elif defined(__powerpc__) /*}{*/
#define ANDROID_FRIEND 0
#define addr_string(string) ({ \
char const *str; \
asm("bl 0f; .asciz \"" string "\"; .balign 4; 0: mflr %0" \
/*out*/ : "=r"(str) \
/* in*/ : \
/*und*/ : "lr"); \
str; \
})
#elif defined(__powerpc64__) //}{
#define ANDROID_FRIEND 0
#define addr_string(string) ({ \
char const *str; \
asm("bl 0f; .string \"" string "\"; .balign 4; 0: mflr %0" \
/*out*/ : "=r"(str) \
/* in*/ : \
/*und*/ : "lr"); \
str; \
})
#elif defined(__x86_64) //}{
#define ANDROID_FRIEND 0
#define addr_string(string) ({ \
char const *str; \
asm("lea 9f(%%rip),%0; .section STRCON; 9:.asciz \"" string "\"; .previous" \
/*out*/ : "=r"(str) ); \
str; \
})
#elif defined(__aarch64__) //}{
#define ANDROID_FRIEND 0
#define addr_string(string) ({ \
char const *str; \
asm("bl 0f; .string \"" string "\"; .balign 4; 0: mov %0,x30" \
/*out*/ : "=r"(str) \
/* in*/ : \
/*und*/ : "x30"); \
str; \
})
#else //}{
#define ANDROID_FRIEND 0
#error addr_string
#endif //}
#include "include/linux.h" // syscall decls; i386 inlines via "int 0x80"
#define MFD_EXEC 0x10
//#define O_RDWR 2
@ -26,14 +96,13 @@ unsigned long upx_mmap_and_fd_linux( // returns (mapped_addr | (1+ fd))
)
{
(void)pathname; // FIXME NYI
char str_upx[] = {'u','p','x',0};
int fd = memfd_create(str_upx, MFD_EXEC);
char const *const name = addr_string("upx");
int fd = memfd_create(name, MFD_EXEC);
if (-EINVAL == fd) { // 2024-10-15 MFD_EXEC unknown to ubuntu-20.04
fd = memfd_create(str_upx, 0); // try again
fd = memfd_create(name, 0); // try again
}
if (fd < 0) { // last chance for Linux
char str_dev_shm[] = {'/','d','e','v','/','s','h','m', 0};
fd = open(str_dev_shm, O_RDWR | O_DIRECTORY | O_TMPFILE, 0700);
fd = open(addr_string("/dev/shm"), O_RDWR | O_DIRECTORY | O_TMPFILE, 0700);
if (fd < 0) {
return (unsigned long)(long)fd;
}