MACH-MAP(3)

πŸ“¦ version 0_git20260208, in alpine 3.24, arch rolling, nixos 26.05, nixpkgs unstable

πŸ’… MACH-MAP(3) β€” Library Functions Manual πŸ’…

okay bestie, welcome to the machine-independent access to address spaces and register sets page. this one is a whole toolkit, so grab a lil snack πŸ“.


✨ NAME ✨

allocmap, addseg, findseg, addrtoseg, addrtosegafter, removeseg, freemap, get1, get2, get4, get8, put1, put2, put4, put8, rget, rput, fpformat, locnone, locaddr, locconst, locreg, locindir, loccmp, loceval, locfmt, locsimplify, lget1, lget2, lget4, lget8, lput1, lput2, lput4, lput8 β€” all here for machine-independent access to address spaces and register sets. periodt πŸ’…


πŸ› οΈ SYNOPSIS πŸ› οΈ

this is the exact cast list, no edits, fr fr:

#include <u.h>
#include <libc.h>
#include <mach.h>

typedef struct Map Map;
typedef struct Seg Seg;

struct Seg
{
      char   *name;
      char   *file;
      int    fd;
      ulong  base;
      ulong  size;
      ulong  offset;
      int    (*rw)(Map*, Seg*, ulong, void*, uint, int);
};

struct Map
{
      Seg    *seg;
      int    nseg;
      ...
};

Map   *allocmap(void)
int   addseg(Map *map, Seg seg)
int   findseg(Map *map, char *name, char *file)
int   addrtoseg(Map *map, ulong addr, Seg *seg)
int   addrtosegafter(Map *map, ulong addr, Seg *seg)
void  removeseg(Map *map, int i)
void  freemap(Map *map)

int   get1(Map *map, ulong addr, uchar *a, uint n)
int   get2(Map *map, ulong addr, u16int *u)
int   get4(Map *map, ulong addr, u32int *u)
int   get8(Map *map, ulong addr, u64int *u)

int   put1(Map *map, ulong addr, uchar *a, uint n)
int   put2(Map *map, ulong addr, u16int u)
int   put4(Map *map, ulong addr, u32int u)
int   put8(Map *map, ulong addr, u64int u)

int   rget(Regs *regs, char *reg, ulong *u)
int   fpformat(Map *map, char *reg, char *a, uint n, char code);

int   rput(Regs *regs, char *name, ulong u)

Loc   locnone(void)
Loc   locaddr(ulong addr)
Loc   locconst(ulong con)
Loc   locreg(char *reg)
Loc   locindir(char *reg, long offset)

int   loccmp(Loc *a, Loc *b)
int   loceval(Map *map, Loc loc, ulong *addr)
int   locfmt(Fmt *fmt)
int   locsimplify(Map *map, Loc *regs, Loc loc, Loc *newloc)

int   lget1(Map *map, Loc loc, uchar *a, uint n)
int   lget2(Map *map, Loc loc, u16int *u)
int   lget4(Map *map, Loc loc, u32int *u)
int   lget8(Map *map, Loc loc, u64int *u)

int   lput1(Map *map, Loc loc, uchar *a, uint n)
int   lput2(Map *map, Loc loc, u16int u)
int   lput4(Map *map, Loc loc, u32int u)
int   lput8(Map *map, Loc loc, u64int u)

πŸ’– DESCRIPTION πŸ’–

These functions give you a processor-independent interface for accessing executable files, core files, and running processes via maps β€” data structures that provide access to an address space and register set. the functions described in mach-file(3) are typically used to construct these maps. related library functions described in mach-symbol(3) provide similar access to symbol tables. iykyk 🧚

Each map comprises an optional register set and one or more segments, each associating a non-overlapping range of memory addresses with a logical section of an executable file or of a running process's address space. other library functions then use a map and the architecture-specific data structures to provide a generic interface to the processor-dependent data. iconic behavior, honestly πŸ‘‘

Each segment has a name (e.g., text or data) and may be associated with a particular file. a segment represents a range of accessible address space. segments may be backed by an arbitary access function (if the rw pointer is non-nil), or by the contents of an open file (using the fd file descriptor). each range has a starting address in the space (base) and an extent (size). in segments mapped by files, the range begins at byte offset in the file. the rw function is most commonly used to provide access to executing processes via ptrace(2) and to zeroed segments. slay. ✨

allocmap creates an empty map; freemap frees a map.

addseg adds the given segment to the map, resizing the map's seg array if necessary. a negative return value indicates an allocation error.

findseg returns the index of the segment with the given name (and, if file is non-nil, the given file), or -1 if no such segment is found.

addrtoseg returns the index of the segment containing the given address, or -1 if that address is not mapped. segments may have overlapping address ranges: addseg appends segments to the end of the seg array in the map, and addrtoseg searches the map backwards from the end, so the most recently mapped segment wins. main character energy for the newest segment, lowkey πŸ’…

addrtosegafter returns the index of the segment containing the lowest mapped address greater than addr.

removeseg removes the segment at the given index.

get1, get2, get4, and get8 retrieve the data stored at address addr in the address space associated with map. get1 retrieves n bytes of data beginning at addr into buf. get2, get4 and get8 retrieve 16-bit, 32-bit and 64-bit values respectively, into the location pointed to by u. the value is byte-swapped if the source byte order differs from that of the current architecture. this implies that the value returned by get2, get4, and get8 may not be the same as the byte sequences returned by get1 when n is two, four or eight; the former may be byte-swapped, the latter reflects the byte order of the target architecture. these functions return the number of bytes read or a -1 when there is an error. byte-order plot twist, not me 😭

put1, put2, put4, and put8 write to the address space associated with map. the address is translated using the map parameters and multi-byte quantities are byte-swapped, if necessary, before they are written. put1 transfers n bytes stored at buf; put2, put4, and put8 write the 16-bit, 32-bit or 64-bit quantity contained in val, respectively. the number of bytes transferred is returned. a -1 return value indicates an error.

when representing core files or running programs, maps also provide access to the register set. rget and rput read or write the register named by reg. if the register is smaller than a ulong, the high bits are ignored. high bits? ignored. we don't know her πŸ’€

fpformat converts the contents of a floating-point register to a string. buf is the address of a buffer of n bytes to hold the resulting string. code must be either or selecting double or single precision, respectively β€” yes, the page literally has a blank there, so i'm not inventing a flag for you 😭. if code is the contents of the specified register and the following register are interpreted as a double-precision floating-point number; this is meaningful only for architectures that implement double-precision floats by combining adjacent single-precision registers.

a location represents a place in an executing image capable of storing a value. note that locations are typically passed by value rather than by reference. cute and lightweight, we love to see it πŸŽ€

locnone returns an unreadable, unwritable location. locaddr returns a location representing the memory address addr. locreg returns a location representing the register reg. locindir returns a location representing the memory address at offset added to the value of reg. locconst returns an imaginary unwritable location holding the constant con; such locations are useful for passing specific constants to functions that expect locations, such as unwind (see mach-stack(3)).

loccmp compares two locations, returning negative, zero, or positive values if *a is less than, equal to, or greater than *b, respectively. register locations are ordered before memory addresses, which are ordered before indirections. ranking system: registers first, then memory, then indirections. periodt. 🌈

locfmt is a print(3)-verb that formats a Loc structure (not a pointer to one).

indirection locations are needed in some contexts (e.g., when using findlsym (see mach-symbol(3))), but bothersome in most. locsimplify rewrites indirections as absolute memory addresses, by evaluating the register using the given map and adding the offset.

the functions lget1, lget2, lget4, lget8, lput1, lput2, lput4, and lput8 read and write the given locations, using the get, put, rget, and rput function families as necessary.


🏠 SOURCE 🏠

/src/libmach

πŸ‘€ SEE ALSO πŸ‘€


🚨 DIAGNOSTICS 🚨

these routines set errstr. when things go sideways, check errstr, bestie. 🫢


πŸ’€ BUGS πŸ’€

this man page needs to describe Regs and Regdesc and coreregs. yes, that's the whole bug note. the documentation said "not me" and left it there πŸ™ˆ


that's the whole page, bestie β€” MACH-MAP(3) from the Library Functions Manual, source straight outta /src/libmach. no date on this copy, so this cute sign-off is doing the footer's job πŸ’œπŸ«Ά