I'm trying to implement a tiny compiler for macOS.
I'm running macOS 11.5 on a MacBook Pro with M1. The assembly
encoding works fine and I'm quite happy with the result (when handed over to Clang compiles and runs just fine).
My problem is that I couldn't find a way generate a valid executable file on my own. I got to a point where radare2 disassembles correctly every part of the executable, but every time I try to run my executable I get SIGKILL (9) from the terminal.
I read this whole file since I couldn't find any other source of documentation on the Mach-O format. SPOILER: It didn't work very well π, that is why I'm hoping on some kind of Mach-O wizard to read this.
My problem in detail: The Mach-O header is fine. My problem is all about load commands.
I tried to inject the following segments/commands:
- __PAGEZERO
- __TEXT
- __TEXT,__text
- __LINKEDIT
- LC_DYLD_INFO
- LC_LOAD_DYLINKER
- LC_MAIN
- LC_LOAD_DYLIB
but no matter what I tried (I even tried to copy their values from other executables and then I "replaced" the address of the entry point to match mine), I couldn't find a way to make my executable file work.
Does anybody know what are the exact load commands I need to inject into the executable and their values?
PS: I would be happier if there was a way not to use dyld (I'm planning to stick with syscalls)
__LINKEDIT
virtual memory address to a multiple of0x1000
in your case0x00000001000040FC
to0x0000000100005000
β Nottingham__LINKEDIT
segment to 0x4000 and shifted back the __TEXT,__text section. I even increased the padding to 0x3FFC to get at least 16KB large binaries, but I still get that dammed SIGKILL. Do you have any other suggestion sir? β Namangan0x80000002
i.e.CPU_SUBTYPE_LIB64|CPU_SUBTYPE_ARM64E
. in place of your0x2
CPU_SUBTYPE_ARM64E
. Also you could try dropping theMH_PIE
flag for extra diagnostics. In MacOS Console app you could inspect the system.log of the System kind but I'm not sure if you'll learn anything other than it'sSIGKILL
. β Nottinghamarm64
instead ofarm64e
as architecture? AFAIKarm64e
is only allowed for some system components still; even if you build a regular C hello world with-arch arm64e
, it gets killed when you try to start it. β Dyeline