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