Blob


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