393 lines · 7.8 KB · Text
RawDownload
.TH MACH 2.SH NAMEcrackhdr, machbytype, machbyname, newmap, setmap, findseg, unusemap,loadmap, attachproc, get1, get2, get4, get8, put1, put2, put4, put8,beswab, beswal, beswav, leswab, leswal, leswav \- machine-independent access to executable files.SH SYNOPSIS.B #include <u.h>.br.B #include <libc.h>.br.B #include <bio.h>.br.B #include <mach.h>.PP.ta \w'\fLmachines 'u.Bint crackhdr(int fd, Fhdr *fp).PP.Bvoid machbytype(int type).PP.Bint machbyname(char *name).PP.BMap *newmap(Map *map, int n).PP.Bint setmap(Map *map, int fd, ulong base, ulong end,.PP.B            ulong foffset, char *name).PP.Bint findseg(Map *map, char *name).PP.Bvoid unusemap(Map *map, int seg).PP.BMap *loadmap(Map *map, int fd, Fhdr *fp).PP.BMap *attachproc(int pid, int kflag, int corefd, Fhdr *fp).PP.Bint get1(Map *map, ulong addr, uchar *buf, int n).PP.Bint get2(Map *map, ulong addr, ushort *val).PP.Bint get4(Map *map, ulong addr, long *val).PP.Bint get8(Map *map, ulong addr, vlong *val).PP.Bint put1(Map *map, ulong addr, uchar *buf, int n).PP.Bint put2(Map *map, ulong addr, ushort val).PP.Bint put4(Map *map, ulong addr, long val).PP.Bint put8(Map *map, ulong addr, vlong val).PP.Bushort beswab(ushort val).PP.Blong beswal(long val).PP.Blong beswav(vlong val).PP.Bushort leswab(ushort val).PP.Blong leswal(long val).PP.Blong leswav(vlong val).PP.Bextern Mach mach;.PP.Bextern Machdata machdata;.SH DESCRIPTIONThese functions providea processor-independent interface for accessingthe executable files or executing images of allarchitectures.Related library functions described in.IR symbol (2)and.IR object (2)provide similar access to symbol tables and object files..PPAn.I executableis a file containing an executable program or the.B textfile of the.B /procfile system associated with an executing process asdescribed in.IR proc (3).After opening an executable, an applicationinvokes a library function which parses thefile header,determines the target architecture andinitializes data structures with parametersand pointers to functions appropriate forthat architecture.  Next, the applicationinvokes functions to construct one or more.IR maps ,data structures that translate referencesin the address space of the executableto offsets in the file.  Each.I mapcomprises one or more.BR segments ,each associating a non-overlapping range of memory addresses with a logical section ofthe executable.Other library functions then use a mapand the architecture-specific data structuresto provide a generic interface to theprocessor-dependent data..PP.I Crackhdrinterprets the header of the executableassociated withthe open file descriptor.IR fd .It loads the data structure.I fpwith a machine-independent descriptionof the header information andpoints global variable.I machto the.B Machdata structure containing processor-dependent parametersof the target architecture..PP.I Machbytypeselects architecture-specific data structures and parametervalues based onthe code stored in thefield named.I typein the.B Fhdrdata structure..I Machbynameperforms the same selection basedon the name of a processor class; see.IR 2c (1)for a list of valid names.Both functions point global variables.I machand.I machdatato the.I Machand.I Machdatadata structures appropriate for thetarget architecture and load global variable.I asstypewith the proper disassembler type code..PP.I Newmapcreates an empty map with.I nsegments.If.I mapis zero, the new map is dynamicallyallocated, otherwise it is assumed topoint to an existing dynamically allocated map whosesize is adjusted, as necessary.A zero return value indicates an allocation error..PP.I Setmaploads the first unused segment in.I mapwith thesegment mapping parameters..I Fdis an open file descriptor associated withan executable..I Baseand.I endcontain the lowest and highest virtual addressesmapped by the segment..I Foffsetis the offset to the start of the segment in the file..I Nameis a name to be attached to the segment..PP.I Findsegreturns the index of the thesegment named.I namein.IR map .A return of -1 indicates that nosegment matches.IR name ..PP.I Unusemapmarks segment number.I segin map.I mapunused.  Othersegments in the map remain unaffected..PP.I Loadmapinitializes a default map containingsegments named `text' and `data' thatmap the instruction and data segmentsof the executable described in the.B Fhdrstructure pointed to by.IR fp .Usually that structure was loaded by.IR crackhdrand can be passed to this function withoutmodification.If.I mapis non-zero, that map, which must have beendynamically allocated, is resized to contain two segments;otherwise a new map is allocated.This function returns zero if allocation fails..I Loadmapis usually used to build a map for accessinga static executable, for example, an executableprogram file..PP.I Attachprocconstructs a map for accessing arunning process.  Itreturns the address of a.I Mapcontaining segments mapping theaddress space of the running processwhose process ID is.BR pid .If.B kflagis non-zero, the process is assumed to bea kernel process..B Corefdis an file descriptor opened to.BR /proc/\fIpid\fP/mem ..B Fppoints to the.I Fhdrstructure describing the headerof the executable.  For most architecturesthe resulting.I Mapcontains four segments named `text', `data',`regs' and `fpregs'.  The latter two provide access tothe general and floating point registers, respectively.If the executable is a kernel process (indicated by anon-zero.B kflagargument), the data segment extends to the maximumsupported address, currently 0xffffffff, and theregister sets are read-only.  In user-level programs,the data segment extends to thetop of the stack or 0x7fffffff if the stack topcannot be found, and the register sets are readableand writable..I Attachprocreturns zero if it is unable to build the mapfor the specified process..PP.IR Get1 ,.IR get2 ,.IR get4 ,and.I get8retrieve the data stored at address.I addrin the executable associatedwith.IR map ..I Get1retrieves.I nbytes of data beginning at.I addrinto.IR buf ..IR Get2 ,.I get4and.I get8retrieve 16-bit, 32-bit and 64-bit values respectively,into the location pointed to by.IR val .The value is byte-swapped if the sourcebyte order differs from that of the current architecture.This implies that the value returned by.IR get2 ,.IR get4 ,and.I get8may not be the same as the byte sequencesreturned by.I get1when.I nis two, four or eight; the former may be byte-swapped, thelatter reflects the byte order of the target architecture.If the file descriptor associated with the applicable segment in .I mapis negative, the address itself is placed in thereturn location.  These functions return the numberof bytes read or a \-1 when there is an error..PP.IR Put1 ,.IR put2 ,.IR put4 ,and.I put8write tothe executable associated with.IR map .The address is translated using themap parameters and multi-byte quantities arebyte-swapped, if necessary, before they are written..I Put1transfers.I nbytes stored at.IR buf ;.IR put2 ,.IR put4 ,and.I put8write the 16-bit, 32-bit or 64-bit quantity contained in.IR val ,respectively.  The number of bytes transferred is returned.A \-1 return value indicates an error..PP.IR Beswab ,.IR beswal ,and.I beswavreturn the.BR ushort ,.BR long ,and.B vlongbig-endian representation of.IR val ,respectively..IR Leswab ,.IR leswal ,and.I leswavreturn the little-endian representation of the.BR ushort ,.BR long ,and.B vlongcontained in.IR val ..SH SOURCE.B /sys/src/libmach.SH "SEE ALSO".IR 2c (1),.IR symbol (2),.IR object (2),.IR errstr (2),.IR proc (3),.IR a.out (6).SH DIAGNOSTICSThese routines set.IR errstr .

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t