Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save glemiere/b9c88ce860d4ec6fddfb10a742918c20 to your computer and use it in GitHub Desktop.

Select an option

Save glemiere/b9c88ce860d4ec6fddfb10a742918c20 to your computer and use it in GitHub Desktop.

In this folder, create a runnable macOS arm64 Mach-O executable named hello_world that prints exactly:

hello world

(with a trailing newline is OK)

Hard constraints (do not violate):

  • You MUST generate the Mach-O file completely from scratch by writing raw bytes. No copying/patching any existing binaries or templates, no cp from system binaries, no downloading, no embedding prebuilt blobs.
  • Do NOT use any compiler, assembler, linker, or tool that internally assembles/links (no clang/cc/as/ld/nasm/yasm/llvm-mc/otool as a generator, etc).
  • You MAY use basic file/byte tools only (e.g., python3 -c, printf, dd, perl, xxd, hexdump) to emit bytes, and codesign purely for ad-hoc signing.
  • You may create temporary helper scripts, but must delete them at the end. Final folder contents must include ONLY hello_world (and nothing else).

Implementation requirements:

  1. Construct a minimal single-slice arm64 Mach-O 64-bit MH_EXECUTE:

    • Write a valid mach_header_64 and required load commands (use the system SDK headers for struct layouts/constants if needed, e.g. .../mach-o/loader.h, but do not compile anything).
    • Use LC_SEGMENT_64 for a __TEXT segment containing an __text section with code and any inline string data you need.
    • Use LC_MAIN to define the entrypoint (no dynamic linker assumptions required).
    • Keep it minimal but kernel-loadable on modern macOS.
    • Ensure file is at least one page (0x1000) if required by the loader; pad with zeros as needed.
  2. The machine code MUST be real arm64 instructions (AArch64) that:

    • performs write(1, msg, len) and then exit(0) via Darwin syscalls on arm64.
    • Use Darwin syscall convention:
      • syscall number in x16
      • svc #0x80
      • write syscall = 0x2000004
      • exit syscall = 0x2000001
      • args: x0=fd, x1=buf, x2=len for write; x0=status for exit
  3. Place the string "hello world\n" in the binary in a stable location and set x1 to its runtime address correctly (PC-relative addressing is fine). Do not rely on external libraries.

Verification steps (mandatory):

  • Show ls -l hello_world and file hello_world.
  • Show otool -hv hello_world and otool -l hello_world (these are validators, not generators).
  • Show codesign -s - --force hello_world output (or codesign -dv --verbose=4 hello_world after signing).
  • Run ./hello_world and show the output.
  • Confirm the file is executable (chmod +x if needed).

Process guidance:

  • First, derive the exact byte layout: header + load commands + section offsets + entryoff + code bytes + data bytes + padding.
  • Then write bytes deterministically (prefer a single python script that writes the file, then delete the script).
  • If execution fails, debug using otool, hexdump, and by re-checking offsets/alignments/entryoff.

Deliverable:

  • End state of the folder contains ONLY hello_world, runnable and signed, printing the expected output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment