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)