MACH-FILE(3)
π MACH-FILE(3) β machine-independent access to executable files and running processes
okay so bestie, welcome to MACH-FILE(3) from the Library Functions Manual β this is the page where we learn how to peek inside executables and vibe-check running processes without caring what machine you're on. it's giving very "one API to rule them all" and honestly? obsessed. π
There's also a whole crew of functions here: crackhdr, uncrackhdr, mapfile, unmapfile, mapproc, unmapproc, detachproc, ctlproc, and procnotes. yes that's a lot of names, no you don't need to memorize them all at once, breathe. π
β¨ SYNOPSIS β¨
#include <u.h>
#include <libc.h>
#include <mach.h>
int crackhdr(int fd, Fhdr *hdr)
void uncrackhdr(Fhdr *hdr)
int mapfile(Fhdr *hdr, ulong base, Map *map, Regs **regs)
void unmapfile(Fhdr *hdr, Map *map)
int mapproc(int pid, Map *map, Regs **regs)
void unmapproc(Map *map)
int detachproc(int pid)
int ctlproc(int pid, char *msg)
int procnotes(int pid, char ***notes)
π¦ DESCRIPTION π¦
So likeβ¦ these functions parse executable files and also give you access to those files and to running processes. double duty, main character energy fr fr. π
π cracking the header
crackhdr opens and parses the named executable file. the data structure it hands back, hdr, is initialized with a machine-independent description of the header info β like, the header but make it portable. β¨
here are the fields you'll actually use, the ones everyone screams about:
machβ a pointer to theMachstructure for the target architecture.mnameβ the name of the target architecture.fnameβ a description of the kind of file (e.g.executable,core dump). the vibe check of the file, basically.anameβ a description of the application binary interface this file uses; typically it's the name of an operating system.
ngl there's a little footnote situation here: if the global variablemachisnil,crackhdrpoints it to the sameMachstructure. sneaky but helpful. π
Also, uncrackhdr is the one that undoes the crack β it's the clean-up bestie. π§Ή
πΈ mapping files
mapfile adds the segments found in hdr to map. if hdr is an executable file, there are typically three segments: text, data, and a zero-backed bss. classic trio, iconic lineup. π
if hdr is a dynamic shared library, its segments get relocated by base before being mapped.
if hdr is a core file, there's one segment named core for each contiguous section of memory recorded in the core file. and there are often quite a few of these, because most operating systems omit clean memory pages when writing core files β Mac OS X is the only exception among the supported systems, so shoutout to them I guess. π
because core files have such holes, it's typically necessary to construct the core map by calling mapfile on the executable and then calling it again on the core file. newly-added segments are mapped on top of existing segments, so this arrangement uses the core file for the segments it contains but falls back to the executable for the rest. layers, on layers, on layers β it works. π«Ά
unmapfile removes the mappings in map corresponding to hdr. bye felicia. π
π§ mapping processes
mapproc attaches to a running program and adds its segments to the given map. it adds one segment for each contiguous section of mapped memory. on systems where this info can't be determined, it adds a single segment covering the entire address space.
β οΈ okay important: accessing areas of that segment that are actually not mapped in the process address space will cause the get/put routines to return errors. so like, tread carefully, bestie. π
unmapproc removes the mappings in map corresponding to pid.
detachproc detaches from all previously attached processes. it's the "we're done here, no hard feelings" of the group chat. βοΈ
π controlling processes
ctlproc manipulates the process with id pid according to the message msg. the valid messages are:
killβ terminate the process.startstopβ start the process and wait for it to stop.sysstopβ arrange for the process to stop at its next system call, start the process, and then wait for it to stop.waitstopβ wait for the process to stop.startβ start the process.
π notes on notes
procnotes fills *notes with a pointer to an array of strings representing pending notes waiting for the process. (on Unix, these notes are textual descriptions of any pending signals.) it returns the number of pending notes. π₯
the memory at *notes should be freed via free (see malloc(3)) when no longer needed β yes, actually free it, don't just ghost it, that's rude to your heap. π
π SOURCE π
/src/libmach β that's where she lives. π
β SEE ALSO β
go read those too, they're literally the extended universe. iykyk. π
and that's the whole page, bestie! this was MACH-FILE(3) from the Library Functions Manual, living in /src/libmach, brought to you with love and a little free(3) reminder at the end. mwah. πππ¦πΈπ
π¦ where this page comes from
- alpine 3.24: plan9port-doc 0_git20260208-r0 Β· LPL-1.02
- arch rolling: plan9port 20261021-2 Β· custom
- nixos 26.05: plan9port 0-unstable-2025-11-10
- nixos 26.05: plan9port-wayland 0-unstable-2025-11-26
- nixpkgs unstable: plan9port 0-unstable-2025-11-10
- nixpkgs unstable: plan9port-wayland 0-unstable-2025-11-26
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms