blob: 45f48e792842ed2ea42532810013c5e7e6a6ca30 [file] [log] [blame]
rscbf8a59f2004-04-11 03:42:27 +00001.TH MACH-MAP 3
2.SH NAME
rsc905ca4e2004-04-19 05:53:04 +00003allocmap, addseg, findseg, addrtoseg,
rscbf8a59f2004-04-11 03:42:27 +00004addrtosegafter, removeseg, freemap,
5get1, get2, get4, get8,
6put1, put2, put4, put8,
rsc058b0112005-01-03 06:40:20 +00007rget, rput, fpformat,
8locnone, locaddr, locconst, locreg, locindir,
9loccmp, loceval, locfmt, locsimplify,
rscbf8a59f2004-04-11 03:42:27 +000010lget1, lget2, lget4, lget8,
11lput1, lput2, lput4, lput8 \- machine-independent
12access to address spaces and register sets
13.SH SYNOPSIS
14.B #include <u.h>
15.br
16.B #include <libc.h>
17.br
18.B #include <mach.h>
19.PP
rsc905ca4e2004-04-19 05:53:04 +000020.ft B
rscbf8a59f2004-04-11 03:42:27 +000021.ta \w'\fBxxxxxx'u +\w'xxxxxxx'u
22.nf
23typedef struct Map Map;
24typedef struct Seg Seg;
25.PP
rsc905ca4e2004-04-19 05:53:04 +000026.ft B
rscbf8a59f2004-04-11 03:42:27 +000027.nf
28struct Seg
29{
30 char *name;
31 char *file;
32 int fd;
33 ulong base;
34 ulong size;
35 ulong offset;
36 int (*rw)(Map*, Seg*, ulong, void*, uint, int);
37};
38.PP
rsc905ca4e2004-04-19 05:53:04 +000039.ft B
rscbf8a59f2004-04-11 03:42:27 +000040.nf
41struct Map
42{
43 Seg *seg;
44 int nseg;
45 \fI...\fR
46};
47.PP
rsc905ca4e2004-04-19 05:53:04 +000048.ft B
rscbf8a59f2004-04-11 03:42:27 +000049Map *allocmap(void)
50.br
51int addseg(Map *map, Seg seg)
52.br
53int findseg(Map *map, char *name, char *file)
54.br
55int addrtoseg(Map *map, ulong addr, Seg *seg)
56.br
57int addrtosegafter(Map *map, ulong addr, Seg *seg)
58.br
59void removeseg(Map *map, int i)
60.br
61void freemap(Map *map)
62.PP
rsc905ca4e2004-04-19 05:53:04 +000063.ft B
rscbf8a59f2004-04-11 03:42:27 +000064int get1(Map *map, ulong addr, uchar *a, uint n)
65.br
66int get2(Map *map, ulong addr, u16int *u)
67.br
68int get4(Map *map, ulong addr, u32int *u)
69.br
70int get8(Map *map, ulong addr, u64int *u)
71.PP
rsc905ca4e2004-04-19 05:53:04 +000072.ft B
rscbf8a59f2004-04-11 03:42:27 +000073int put1(Map *map, ulong addr, uchar *a, uint n)
74.br
75int put2(Map *map, ulong addr, u16int u)
76.br
77int put4(Map *map, ulong addr, u32int u)
78.br
79int put8(Map *map, ulong addr, u64int u)
80.PP
rsc905ca4e2004-04-19 05:53:04 +000081.ft B
rsc058b0112005-01-03 06:40:20 +000082int rget(Regs *regs, char *reg, ulong *u)
rscbf8a59f2004-04-11 03:42:27 +000083.br
84int fpformat(Map *map, char *reg, char *a, uint n, char code);
85.PP
rsc905ca4e2004-04-19 05:53:04 +000086.ft B
rsc058b0112005-01-03 06:40:20 +000087int rput(Regs *regs, char *name, ulong u)
rscbf8a59f2004-04-11 03:42:27 +000088.PP
rsc905ca4e2004-04-19 05:53:04 +000089.ft B
rsc058b0112005-01-03 06:40:20 +000090Loc locnone(void)
91.br
rscbf8a59f2004-04-11 03:42:27 +000092Loc locaddr(ulong addr)
93.br
94Loc locconst(ulong con)
95.br
96Loc locreg(char *reg)
97.br
98Loc locindir(char *reg, long offset)
99.PP
rsc905ca4e2004-04-19 05:53:04 +0000100.ft B
rscbf8a59f2004-04-11 03:42:27 +0000101int loccmp(Loc *a, Loc *b)
102.br
103int loceval(Map *map, Loc loc, ulong *addr)
104.br
105int locfmt(Fmt *fmt)
106.br
rsc058b0112005-01-03 06:40:20 +0000107int locsimplify(Map *map, Loc *regs, Loc loc, Loc *newloc)
rscbf8a59f2004-04-11 03:42:27 +0000108.PP
rsc905ca4e2004-04-19 05:53:04 +0000109.ft B
rscbf8a59f2004-04-11 03:42:27 +0000110int lget1(Map *map, Loc loc, uchar *a, uint n)
111.br
112int lget2(Map *map, Loc loc, u16int *u)
113.br
114int lget4(Map *map, Loc loc, u32int *u)
115.br
116int lget8(Map *map, Loc loc, u64int *u)
117.PP
rsc905ca4e2004-04-19 05:53:04 +0000118.ft B
rscbf8a59f2004-04-11 03:42:27 +0000119int lput1(Map *map, Loc loc, uchar *a, uint n)
120.br
121int lput2(Map *map, Loc loc, u16int u)
122.br
123int lput4(Map *map, Loc loc, u32int u)
124.br
125int lput8(Map *map, Loc loc, u64int u)
126.PP
127.SH DESCRIPTION
128These functions provide
129a processor-independent interface for accessing
130executable files, core files, and running processes
131via
132.IR maps ,
133data structures that provides access to an address space
134and register set.
135The functions described in
136.IR mach-file (3)
137are typically used to construct these maps.
138Related library functions described in
139.IR mach-symbol (3)
140provide similar access to symbol tables.
141.PP
142Each
143.I map
144comprises an optional register set and one or more
145.BR segments ,
146each associating a non-overlapping range of
147memory addresses with a logical section of
148an executable file or of a running process's address space.
149Other library functions then use a map
150and the architecture-specific data structures
151to provide a generic interface to the
152processor-dependent data.
153.PP
154Each segment has a name (e.g.,
155.B text
156or
157.BR data )
158and may be associated with a particular file.
159A segment represents a range of accessible address space.
160Segments may be backed an arbitary access function
161(if the
162.B rw
163pointer is non-nil),
164or by the contents of an open file
165(using the
166.B fd
167file descriptor).
168Each range has a starting address in the space
169.RB ( base )
170and
171an extent
172.RB ( size ).
173In segments mapped by files,
174the range begins at byte
175.B offset
176in the file.
177The
178.B rw
179function is most commonly used to provide
180access to executing processes via
181.IR ptrace (2)
182and to zeroed segments.
183.PP
184.I Allocmap
185creates an empty map;
186.IR freemap
187frees a map.
188.PP
189.I Addseg
190adds the given segment to the map, resizing the map's
191.I seg
192array if necessary.
193A negative return value indicates an allocation error.
194.PP
195.I Findseg
196returns the index of the segment with the given name (and, if
197.I file
198is non-nil, the given file),
199or \-1 if no such segment is found.
200.PP
201.I Addrtoseg
202returns the index of the segment containing
203for the given address, or \-1 if that address is not mapped.
204Segments may have overlapping address ranges:
205.I addseg
206appends segments to the end of the
207.I seg
208array in the map, and
209.I addrtoseg
210searches the map backwards from the end,
211so the most recently mapped segment wins.
212.PP
213.I Addrtosegafter
214returns the index of the segment containing the lowest mapped
215address greater than
216.IR addr .
217.PP
218.I Removeseg
219removes the segment at the given index.
220.PP
221.IR Get1 ,
222.IR get2 ,
223.IR get4 ,
224and
225.I get8
226retrieve the data stored at address
227.I addr
228in the address space associated
229with
230.IR map .
231.I Get1
232retrieves
233.I n
234bytes of data beginning at
235.I addr
236into
237.IR buf .
238.IR Get2 ,
239.I get4
240and
241.I get8
242retrieve 16-bit, 32-bit and 64-bit values respectively,
243into the location pointed to by
244.IR u .
245The value is byte-swapped if the source
246byte order differs from that of the current architecture.
247This implies that the value returned by
248.IR get2 ,
249.IR get4 ,
250and
251.I get8
252may not be the same as the byte sequences
253returned by
254.I get1
255when
256.I n
257is two, four or eight; the former may be byte-swapped, the
258latter reflects the byte order of the target architecture.
259These functions return the number
260of bytes read or a \-1 when there is an error.
261.PP
262.IR Put1 ,
263.IR put2 ,
264.IR put4 ,
265and
266.I put8
267write to
268the address space associated with
269.IR map .
270The address is translated using the
271map parameters and multi-byte quantities are
272byte-swapped, if necessary, before they are written.
273.I Put1
274transfers
275.I n
276bytes stored at
277.IR buf ;
278.IR put2 ,
279.IR put4 ,
280and
281.I put8
282write the 16-bit, 32-bit or 64-bit quantity contained in
283.IR val ,
284respectively. The number of bytes transferred is returned.
285A \-1 return value indicates an error.
286.PP
287When representing core files or running programs,
288maps also provide access to the register set.
rsc058b0112005-01-03 06:40:20 +0000289.IR Rget
290and
291.IR rput
292read or write the register
rscbf8a59f2004-04-11 03:42:27 +0000293named by
294.IR reg .
rsc058b0112005-01-03 06:40:20 +0000295If the register is smaller than a
296.BR ulong ,
297the high bits are ignored.
rscbf8a59f2004-04-11 03:42:27 +0000298.PP
299.I Fpformat
300converts the contents of a floating-point register to a string.
301.I Buf
302is the address of a buffer of
303.I n
304bytes to hold the resulting string.
305.I Code
306must be either
307.L F
308or
309.LR f ,
310selecting double or single precision, respectively.
311If
312.I code
313is
314.LR F ,
315the contents of the specified register and the
316following register are interpreted as a double-precision
317floating-point number;
318this is meaningful only for architectures that implement
319double-precision floats by combining adjacent single-precision
320registers.
321.PP
322A
323.I location
324represents a place in an executing image capable of
325storing a value.
326Note that locations are typically passed by value rather than by reference.
327.PP
328.I Locnone
329returns an unreadable, unwritable location.
330.I Locaddr
331returns a location representing the memory address
332.IR addr .
333.I Locreg
334returns a location representing the register
335.IR reg .
336.I Locindir
337returns an location representing the memory address
338at
339.I offset
340added to the value of
341.IR reg .
342.I Locconst
343returns an imaginary unwritable location holding the constant
344.IR con ;
345such locations are useful for passing specific constants to
346functions expect locations, such as
347.I unwind
348(see
349.IR mach-stack (3)).
350.PP
351.I Loccmp
352compares two locations, returning negative, zero, or positive
353values if
354.B *a
355is less than, equal to, or greater than
356.BR *b ,
357respectively.
358Register locations are ordered before memory addresses,
359which are ordered before indirections.
360.PP
361.I Locfmt
362is a
363.IR print (3)-verb
364that formats a
365.B Loc
366structure
367.RI ( not
368a pointer to one).
369.PP
370Indirection locations are needed in some contexts (e.g., when
371using
372.I findlsym
373(see
374.IR mach-symbol (3))),
375but bothersome in most.
376.I Locsimplify
377rewrites indirections as absolute memory addresses, by evaluating
378the register using the given map and adding the offset.
379.PP
380The functions
381.IR lget1 ,
382.IR lget2 ,
383.IR lget4 ,
384.IR lget8 ,
385.IR lput1 ,
386.IR lput2 ,
387.IR lput4 ,
388and
389.I lput8
390read and write the given locations, using the
391.IR get ,
392.IR put ,
393.IR rget ,
394and
395.I rput
396function families as necessary.
397.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000398.B \*9/src/libmach
rscbf8a59f2004-04-11 03:42:27 +0000399.SH "SEE ALSO"
rsc905ca4e2004-04-19 05:53:04 +0000400.IR mach (3),
401.IR mach-file (3)
rscbf8a59f2004-04-11 03:42:27 +0000402.SH DIAGNOSTICS
403These routines set
404.IR errstr .
rsc058b0112005-01-03 06:40:20 +0000405.SH BUGS
406This man page needs to describe
407.B Regs
408and
409.B Regdesc