From 76b0b4ce9c4269eb3fb25ce5bbf618b83698bd47 Mon Sep 17 00:00:00 2001 From: John Reiser Date: Wed, 20 Nov 2024 15:02:32 -0800 Subject: [PATCH] Fix mips stub for ELF2 main program modified: p_lx_elf.cpp modified: stub/Makefile modified: stub/src/mips.r3000-expand.S modified: stub/src/upxfd_linux.c --- src/p_lx_elf.cpp | 3 +++ src/stub/Makefile | 10 ++++++---- src/stub/src/mips.r3000-expand.S | 8 ++++---- src/stub/src/upxfd_linux.c | 12 ++++++++++++ 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/p_lx_elf.cpp b/src/p_lx_elf.cpp index 086f626a..7857f556 100644 --- a/src/p_lx_elf.cpp +++ b/src/p_lx_elf.cpp @@ -1533,6 +1533,9 @@ PackLinuxElf32::buildLinuxLoader( 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"); + } if (hasLoaderSection("SYSCALLS")) { len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "SYSCALLS"); } diff --git a/src/stub/Makefile b/src/stub/Makefile index d107dd0f..faab5859 100644 --- a/src/stub/Makefile +++ b/src/stub/Makefile @@ -1596,7 +1596,7 @@ tmp/mips.r3000-linux.upxfd_android.s: $(srcdir)/src/upxfd_android.c $(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 's/L[0-9][0-9]*/L8&/g' \ -e $$(cat src/dollar-dollar.sed) \ -e 's/ j[ ][ ]*$$L/ b $$L/' \ -e 's/ jal[ ][ ]*\([^\$$]\)/ bal \1/' \ @@ -1607,7 +1607,7 @@ tmp/mips.r3000-linux.upxfd_linux.s: $(srcdir)/src/upxfd_linux.c $(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 's/L[0-9][0-9]*/L9&/g' \ -e $$(cat src/dollar-dollar.sed) \ -e 's/ j[ ][ ]*$$L/ b $$L/' \ -e 's/ jal[ ][ ]*\([^\$$]\)/ bal \1/' \ @@ -1617,7 +1617,7 @@ tmp/mips.r3000-linux.elf-main2.s: $(srcdir)/src/$$T.c $(srcdir)/src/i386-linux.e @echo; echo TARGET: $@; echo $(call tc,gcc) -S -D__mips__ -D_TARGET_LINUX_ -O $< -o - \ | sed -e '/^\t\.file\t/d' \ - -e 's/L[0-9]*/L7&/g' \ + -e 's/L[0-9][0-9]*/L7&/g' \ -e $$(cat src/dollar-dollar.sed) \ -e 's/ j[ ][ ]*$$L/ b $$L/' \ -e 's/ jal[ ][ ]*\([^\$$]\)/ bal \1/' \ @@ -1724,8 +1724,10 @@ 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 - \ + | tee foo-mipsel.r3000-linux.upxfd_linux.s \ | sed -e '/^\t\.file\t/d' \ - -e 's/L[0-9]*/L8&/g' \ + -e '/^\t*\.text/s//.section UMF_LINUX/' \ + -e 's/L[0-9][0-9]*/L9&/g' \ -e $$(cat src/dollar-dollar.sed) \ -e 's/ j[ ][ ]*$$L/ b $$L/' \ -e 's/ jal[ ][ ]*\([^\$$]\)/ bal \1/' \ diff --git a/src/stub/src/mips.r3000-expand.S b/src/stub/src/mips.r3000-expand.S index f93b2d63..53a7455e 100644 --- a/src/stub/src/mips.r3000-expand.S +++ b/src/stub/src/mips.r3000-expand.S @@ -154,10 +154,6 @@ 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 @@ -174,6 +170,10 @@ upx_mmap_and_fd: .globl upx_mmap_and_fd unfilter: //#include "arch/mips/r3000/bxx.S" // unfilter code; args in registers, fall-through return + .balign 4 +upx_mmap_and_fd: .globl upx_mmap_and_fd + // UMF_ANDROID or UMF_LINUX must be loaded after EXP_TAIL + // FIXME: will need extra parameter for multi-method decompression #define NO_METHOD_CHECK 0 diff --git a/src/stub/src/upxfd_linux.c b/src/stub/src/upxfd_linux.c index af7ceb08..c00be20e 100644 --- a/src/stub/src/upxfd_linux.c +++ b/src/stub/src/upxfd_linux.c @@ -6,6 +6,8 @@ All Rights Reserved. */ +extern void my_bkpt(void const *, ...); + #if defined(__i386__) //}{ #define ANDROID_FRIEND 1 #define addr_string(string) ({ \ @@ -76,12 +78,22 @@ #define ANDROID_FRIEND 0 #error addr_string #endif //} + +#ifdef __mips__ //{ +#define NO_WANT_READ 1 +#define NO_WANT_CLOSE 1 +#define NO_WANT_MPROTECT 1 +#endif //} #include "include/linux.h" // syscall decls; i386 inlines via "int 0x80" + #define MFD_EXEC 0x10 //#define O_RDWR 2 #define O_DIRECTORY 0200000 /* 0x010000 asm-generic/fcntl.h */ #define O_TMPFILE 020000000 /* 0x400000 asm-generic/fcntl.h */ #define EINVAL 22 /* asm-generic/errno-base.h */ + +extern int memfd_create(char const *, unsigned); +extern int ftruncate(int, size_t); // Implementation for Linux-native, where memfd_create // (or /dev/shm) works. Saves space in contrast to // upxfd_android (or Android emulator), which must