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

Successful build for MacOS: compile and link.

Fails to excecute, but at least 'cmake --build .' works.
	modified:   p_mach.cpp
	modified:   stub/Makefile
	modified:   stub/arm64-darwin.macho-fold.h
	modified:   stub/src/arch/powerpc/32/Makefile.extra
	modified:   stub/tmp/arm64-darwin.macho-fold.map
This commit is contained in:
John Reiser 2025-04-04 19:57:15 -07:00
parent 64ecabc26b
commit ec1a45b658
5 changed files with 1142 additions and 337 deletions

View File

@ -487,40 +487,77 @@ PackMachBase<T>::buildMachLoader(
Filter const *ft
)
{
initLoader(proto, szproto);
MemBuffer mb_cprLoader;
unsigned sz_cpr = 0;
unsigned sz_unc = 0;
unsigned method = 0;
upx_byte const *uncLoader = nullptr;
if (0 < szfold) { // main program with ELF2 de-compressor (folded portion)
initLoader(fold, szfold);
char sec[120]; memset(sec, 0, sizeof(sec)); // debug convenience
int len = 0;
unsigned m_decompr = methods_used | (1u << (0xFF & ph_forced_method(ph.method)));
len += snprintf(sec, sizeof(sec), "%s", ".text,EXP_HEAD");
if (((1u<<M_NRV2B_LE32)|(1u<<M_NRV2B_8)|(1u<<M_NRV2B_LE16)) & m_decompr) {
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "NRV2B");
}
if (((1u<<M_NRV2D_LE32)|(1u<<M_NRV2D_8)|(1u<<M_NRV2D_LE16)) & m_decompr) {
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "NRV2D");
}
if (((1u<<M_NRV2E_LE32)|(1u<<M_NRV2E_8)|(1u<<M_NRV2E_LE16)) & m_decompr) {
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "NRV2E");
}
if (((1u<<M_LZMA)) & m_decompr) {
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", (opt->small
? "LZMA_DAISY,LZMA_ELF00,LZMA_DEC10,LZMA_DEC30"
: "LZMA_DAISY,LZMA_ELF00,LZMA_DEC20,LZMA_DEC30" ));
}
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "EXP_TAIL");
if (hasLoaderSection("SYSCALLS")) {
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "SYSCALLS");
}
if (hasLoaderSection("STRCON")) {
len += snprintf(&sec[len], sizeof(sec) - len, ",%s", "STRCON");
}
(void)len;
NO_printf("\n%s\n", sec);
addLoader(sec, nullptr);
relocateLoader();
{
int sz_unc_int;
uncLoader = linker->getLoader(&sz_unc_int);
sz_unc = sz_unc_int;
}
method = M_NRV2B_LE32; // requires unaligned fetch
}
struct b_info h; memset(&h, 0, sizeof(h));
unsigned fold_hdrlen = 0;
if (0 < szfold) {
h.sz_unc = (szfold < fold_hdrlen) ? 0 : (szfold - fold_hdrlen);
h.b_method = (unsigned char) ph.method;
h.b_ftid = (unsigned char) ph.filter;
h.b_cto8 = (unsigned char) ph.filter_cto;
}
unsigned char const *const uncLoader = fold_hdrlen + fold;
h.b_method = method;
// Headers are NOT filtered, so Leave h.b_ftid and h.b_cto8 as zero.
MemBuffer cprLoader_buf(sizeof(h) + h.sz_unc);
unsigned char *const cprLoader = (unsigned char *)cprLoader_buf.getVoidPtr();
if (0 < szfold) {
unsigned sz_cpr = 0;
int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &sz_cpr,
nullptr, ph.method, 10, nullptr, nullptr );
h.sz_cpr = sz_cpr;
if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc)
throwInternalError("loader compression failed");
}
memcpy(cprLoader, &h, sizeof(h));
mb_cprLoader.allocForCompression(sizeof(h) + sz_unc);
unsigned char *const cprLoader = (unsigned char *)mb_cprLoader; // less typing
// This adds the definition to the "library", to be used later.
linker->addSection("FOLDEXEC", cprLoader, sizeof(h) + h.sz_cpr, 0);
h.sz_unc = sz_unc;
h.sz_cpr = mb_cprLoader.getSize(); // max that upx_compress may use
{
int r = upx_compress(uncLoader, sz_unc, sizeof(h) + cprLoader, &sz_cpr,
nullptr, ph_forced_method(method), 10, nullptr, nullptr );
h.sz_cpr = sz_cpr; // actual length used
if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc)
throwInternalError("loader compression failed");
}
set_te32(&h.sz_cpr, h.sz_cpr);
set_te32(&h.sz_unc, h.sz_unc);
memcpy(cprLoader, &h, sizeof(h)); // cprLoader will become FOLDEXEC
int const GAP = 128; // must match stub/l_mac_ppc.S
int const NO_LAP = 64; // must match stub/src/*darwin*.S
segTEXT.vmsize = h.sz_unc - h.sz_cpr + GAP + NO_LAP;
addStubEntrySections(ft);
defineSymbols(ft);
initLoader(proto, szproto, -1, sz_cpr);
NO_printf("FOLDEXEC unc=%#x cpr=%#x\n", sz_unc, sz_cpr);
linker->addSection("FOLDEXEC", mb_cprLoader, sizeof(b_info) + sz_cpr, 0);
{ // main program with MACH2 de-compressor
addLoader("MACHMAINX,MACHMAINZ,FOLDEXEC,IDENTSTR");
defineSymbols(ft);
}
relocateLoader();
}

View File

@ -1109,11 +1109,13 @@ arm64-darwin.macho-entry.h : $(srcdir)/src/$$T.S
$(call tc,f-embed_objinfo,tmp/$T.bin)
$(call tc,bin2h) tmp/$T.bin $@
arm64-darwin.macho-fold.h : tmp/$$T.o tmp/arm64-darwin.macho-main.o
$(call tc,ld) --no-warn-mismatch --strip-all -Map tmp/$T.map $(filter %.o,$^) -o tmp/$T.tmp
$(call tc,objcopy) -O binary tmp/$T.tmp tmp/$T.bin
rm tmp/$T.tmp
chmod a-x tmp/$T.bin
arm64-darwin.macho-fold.h : \
tmp/$$T.o \
tmp/arm64-expand.o \
tmp/arm64-darwin.macho-main.o
$(call tc,ld) -r -Map tmp/$T.map $(filter %.o,$^) -o tmp/$T.bin
$(call tc,objdump) -Dr $(tc_objdump_disasm_options) tmp/$T.bin | $(RTRIM) > tmp/$T.bin.disasm
$(call tc,f-embed_objinfo_without_xstrip_keep_dot_text,tmp/$T.bin)
$(call tc,bin2h) tmp/$T.bin $@
tmp/arm64-darwin.macho-fold.o : $(srcdir)/src/$$T.S

File diff suppressed because it is too large Load Diff

View File

@ -45,8 +45,8 @@ $c += -DWITH_LZMA=$(UPX_LZMA_VERSION)
$c += -I$(UPX_LZMADIR)
$c += -I$(top_srcdir)/src
lzma_d_cs.S : lzma_d-ppc32-hand.S
$(call tc,gcc) $(PP_FLAGS) -c -DLZMA_FALL_THROUGH=1 $< -o tmp/$T.o
lzma_d_cs.S : lzma_d-ppc32-hand.S lzma_d.S
$(call tc,gcc) $(PP_FLAGS) -c -DLZMA_FALL_THROUGH=1 lzma_d-ppc32-hand.S -o tmp/$T.o
$(call tc,f-objstrip,tmp/$T.o)
$(call tc,objcopy) -O binary tmp/$T.o tmp/$T.bin
$(call tc,objdump) -b binary -m powerpc -D tmp/$T.bin | $(RTRIM) > tmp/$T.bin.disasm

View File

@ -7,9 +7,8 @@ Name Origin Length Attributes
Linker script and memory map
LOAD tmp/arm64-darwin.macho-fold.o
LOAD tmp/arm64-expand.o
LOAD tmp/arm64-darwin.macho-main.o
0x0000000000400000 PROVIDE (__executable_start, 0x400000)
0x0000000000400078 . = (0x400000 + SIZEOF_HEADERS)
.interp
*(.interp)
@ -38,69 +37,83 @@ LOAD tmp/arm64-darwin.macho-main.o
.gnu.version_r
*(.gnu.version_r)
.rela.dyn
.rela.init
*(.rela.init)
*(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)
.rela.text
*(.rela.text)
.rela.fini
*(.rela.fini)
*(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)
*(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)
*(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)
*(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)
.rela.rodata
*(.rela.rodata)
.rela.data.rel.ro
*(.rela.data.rel.ro)
.rela.data
*(.rela.data)
.rela.tdata
*(.rela.tdata)
.rela.tbss
*(.rela.tbss)
.rela.ctors
*(.rela.ctors)
.rela.dtors
*(.rela.dtors)
.rela.got
*(.rela.got)
*(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)
.rela.bss
*(.rela.bss)
.rela.ifunc
*(.rela.ifunc)
.rela.plt 0x0000000000400078 0x0
.rela.plt
*(.rela.plt)
0x0000000000400078 PROVIDE (__rela_iplt_start, .)
*(.rela.iplt)
.rela.iplt 0x0000000000400078 0x0 tmp/arm64-darwin.macho-fold.o
0x0000000000400078 PROVIDE (__rela_iplt_end, .)
.init
*(SORT(.init))
.plt 0x0000000000400080 0x0
.plt
*(.plt)
*(.iplt)
.iplt 0x0000000000400080 0x0 tmp/arm64-darwin.macho-fold.o
.text 0x0000000000400078 0x9dc
*(.text.unlikely .text.*_unlikely .text.unlikely.*)
*(.text.exit .text.exit.*)
*(.text.startup .text.startup.*)
*(.text.hot .text.hot.*)
*(.text .stub .text.* .gnu.linkonce.t.*)
.text 0x0000000000400078 0x158 tmp/arm64-darwin.macho-fold.o
0x0000000000400078 _start
0x000000000040012c spin
0x0000000000400130 exit
0x0000000000400138 read
0x0000000000400144 write
0x0000000000400150 open
0x000000000040015c close
0x0000000000400168 brk
0x0000000000400174 munmap
0x0000000000400180 mprotect
0x000000000040018c mmap
0x0000000000400198 pread
0x00000000004001a4 bswap
.text 0x00000000004001d0 0x884 tmp/arm64-darwin.macho-main.o
0x0000000000400388 my_bkpt
0x00000000004003d4 do_xmap
0x0000000000400878 upx_main
.text 0x0000000000000000 0x9dc
*(.text .stub)
.text 0x0000000000000000 0x158 tmp/arm64-darwin.macho-fold.o
0x0000000000000000 _start
0x00000000000000b4 spin
0x00000000000000b8 exit
0x00000000000000c0 read
0x00000000000000cc write
0x00000000000000d8 open
0x00000000000000e4 close
0x00000000000000f0 brk
0x00000000000000fc munmap
0x0000000000000108 mprotect
0x0000000000000114 mmap
0x0000000000000120 pread
0x000000000000012c bswap
.text 0x0000000000000158 0x0 tmp/arm64-expand.o
.text 0x0000000000000158 0x884 tmp/arm64-darwin.macho-main.o
0x0000000000000310 my_bkpt
0x000000000000035c do_xmap
0x0000000000000800 upx_main
*(.gnu.warning)
.fini
*(SORT(.fini))
0x0000000000400a54 PROVIDE (__etext, .)
0x0000000000400a54 PROVIDE (_etext, .)
0x0000000000400a54 PROVIDE (etext, .)
.rodata
*(.rodata .rodata.* .gnu.linkonce.r.*)
*(.rodata)
.rodata1
*(.rodata1)
@ -108,9 +121,9 @@ LOAD tmp/arm64-darwin.macho-main.o
.eh_frame_hdr
*(.eh_frame_hdr)
.eh_frame 0x0000000000400a58 0x130
.eh_frame 0x0000000000000000 0x130
*(.eh_frame)
.eh_frame 0x0000000000400a58 0x130 tmp/arm64-darwin.macho-main.o
.eh_frame 0x0000000000000000 0x130 tmp/arm64-darwin.macho-main.o
0x120 (size before relaxing)
.gcc_except_table
@ -118,8 +131,6 @@ LOAD tmp/arm64-darwin.macho-main.o
.exception_ranges
*(.exception_ranges .exception_ranges*)
0x0000000000400b88 . = (ALIGN (0x10000) - ((0x10000 - .) & 0xffff))
0x0000000000410b88 . = DATA_SEGMENT_ALIGN (0x10000, 0x1000)
.eh_frame
*(.eh_frame)
@ -131,93 +142,44 @@ LOAD tmp/arm64-darwin.macho-main.o
*(.exception_ranges .exception_ranges*)
.tdata
*(.tdata .tdata.* .gnu.linkonce.td.*)
*(.tdata)
.tbss
*(.tbss .tbss.* .gnu.linkonce.tb.*)
*(.tcommon)
*(.tbss)
.preinit_array 0x0000000000410b88 0x0
0x0000000000410b88 PROVIDE (__preinit_array_start, .)
.preinit_array
*(.preinit_array)
0x0000000000410b88 PROVIDE (__preinit_array_end, .)
.init_array 0x0000000000410b88 0x0
0x0000000000410b88 PROVIDE (__init_array_start, .)
*(SORT(.init_array.*))
*(.init_array)
0x0000000000410b88 PROVIDE (__init_array_end, .)
.fini_array 0x0000000000410b88 0x0
0x0000000000410b88 PROVIDE (__fini_array_start, .)
*(SORT(.fini_array.*))
*(.fini_array)
0x0000000000410b88 PROVIDE (__fini_array_end, .)
.ctors
*crtbegin.o(.ctors)
*crtbegin?.o(.ctors)
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
*(SORT(.ctors.*))
*(.ctors)
.dtors
*crtbegin.o(.dtors)
*crtbegin?.o(.dtors)
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
*(SORT(.dtors.*))
*(.dtors)
.jcr
*(.jcr)
.data.rel.ro
*(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*)
*(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*)
.dynamic
*(.dynamic)
.got
*(.got)
*(.igot)
0x0000000000410b88 . = DATA_SEGMENT_RELRO_END (., 0x18)
.got.plt 0x0000000000410b88 0x0
.got.plt
*(.got.plt)
*(.igot.plt)
.igot.plt 0x0000000000410b88 0x0 tmp/arm64-darwin.macho-fold.o
.data 0x0000000000410b88 0x0
0x0000000000410b88 PROVIDE (__data_start, .)
*(.data .data.* .gnu.linkonce.d.*)
.data 0x0000000000410b88 0x0 tmp/arm64-darwin.macho-fold.o
.data 0x0000000000410b88 0x0 tmp/arm64-darwin.macho-main.o
.data 0x0000000000000000 0x0
*(.data)
.data 0x0000000000000000 0x0 tmp/arm64-darwin.macho-fold.o
.data 0x0000000000000000 0x0 tmp/arm64-expand.o
.data 0x0000000000000000 0x0 tmp/arm64-darwin.macho-main.o
.data1
*(.data1)
0x0000000000410b88 _edata = .
0x0000000000410b88 PROVIDE (edata, .)
0x0000000000410b88 . = .
0x0000000000410b88 __bss_start = .
0x0000000000410b88 __bss_start__ = .
.bss 0x0000000000410b88 0x0
.bss 0x0000000000000000 0x0
*(.dynbss)
*(.bss .bss.* .gnu.linkonce.b.*)
.bss 0x0000000000410b88 0x0 tmp/arm64-darwin.macho-fold.o
.bss 0x0000000000410b88 0x0 tmp/arm64-darwin.macho-main.o
*(.bss)
.bss 0x0000000000000000 0x0 tmp/arm64-darwin.macho-fold.o
.bss 0x0000000000000000 0x0 tmp/arm64-expand.o
.bss 0x0000000000000000 0x0 tmp/arm64-darwin.macho-main.o
*(COMMON)
0x0000000000410b88 . = ALIGN ((. != 0x0)?0x8:0x1)
0x0000000000410b88 _bss_end__ = .
0x0000000000410b88 __bss_end__ = .
0x0000000000410b88 . = ALIGN (0x8)
0x0000000000410b88 . = SEGMENT_START ("ldata-segment", .)
0x0000000000410b88 . = ALIGN (0x8)
0x0000000000410b88 __end__ = .
0x0000000000410b88 _end = .
0x0000000000410b88 PROVIDE (end, .)
0x0000000000410b88 . = DATA_SEGMENT_END (.)
.stab
*(.stab)
@ -240,6 +202,43 @@ LOAD tmp/arm64-darwin.macho-main.o
.comment
*(.comment)
EXP_HEAD 0x0000000000000000 0x104
EXP_HEAD 0x0000000000000000 0x104 tmp/arm64-expand.o
0x0000000000000000 f_expand
EXP_TAIL 0x0000000000000000 0x54
EXP_TAIL 0x0000000000000000 0x54 tmp/arm64-expand.o
0x000000000000000c eof
0x0000000000000048 eof_lzma
0x0000000000000054 upx_mmap_and_fd
NRV2E 0x0000000000000000 0xf4
NRV2E 0x0000000000000000 0xf4 tmp/arm64-expand.o
0x0000000000000008 ucl_nrv2e_decompress_32
NRV2D 0x0000000000000000 0xe8
NRV2D 0x0000000000000000 0xe8 tmp/arm64-expand.o
0x0000000000000008 ucl_nrv2d_decompress_32
NRV2B 0x0000000000000000 0xbc
NRV2B 0x0000000000000000 0xbc tmp/arm64-expand.o
0x0000000000000008 ucl_nrv2b_decompress_32
LZMA_DAISY 0x0000000000000000 0x8
LZMA_DAISY 0x0000000000000000 0x8 tmp/arm64-expand.o
LZMA_ELF00 0x0000000000000000 0x64
LZMA_ELF00 0x0000000000000000 0x64 tmp/arm64-expand.o
LZMA_DEC20 0x0000000000000000 0x96c
LZMA_DEC20 0x0000000000000000 0x96c tmp/arm64-expand.o
LZMA_DEC10 0x0000000000000000 0x4a8
LZMA_DEC10 0x0000000000000000 0x4a8 tmp/arm64-expand.o
LZMA_DEC30 0x0000000000000000 0x8
LZMA_DEC30 0x0000000000000000 0x8 tmp/arm64-expand.o
.debug
*(.debug)
@ -259,7 +258,7 @@ LOAD tmp/arm64-darwin.macho-main.o
*(.debug_pubnames)
.debug_info
*(.debug_info .gnu.linkonce.wi.*)
*(.debug_info)
.debug_abbrev
*(.debug_abbrev)
@ -306,10 +305,4 @@ LOAD tmp/arm64-darwin.macho-main.o
.note.gnu.arm.ident
*(.note.gnu.arm.ident)
/DISCARD/
*(.note.GNU-stack)
*(.gnu_debuglink)
*(.gnu.lto_*)
*(.gnu_object_only)
OUTPUT(tmp/arm64-darwin.macho-fold.tmp elf64-littleaarch64)
OUTPUT(tmp/arm64-darwin.macho-fold.bin elf64-littleaarch64)