Blob


1 .TH MACH-SYMBOL 3
2 .SH NAME
3 symopen, symclose, indexsym, lookupsym, findsym,
4 lookuplsym, indexlsym, findlsym,
5 symoff, pc2file, file2pc, line2pc, fnbound, fileline,
6 pc2line \- symbol table access functions
7 .SH SYNOPSIS
8 .B #include <u.h>
9 .br
10 .B #include <libc.h>
11 .br
12 .B #include <mach.h>
13 .PP
14 .ta \w'\fBxxxxxx'u +\w'\fBxxxxxx'u
15 .ft B
16 int symopen(Fhdr *hdr)
17 .br
18 void symclose(Fhdr *hdr)
19 .PP
20 .ft B
21 int indexsym(uint n, Symbol *s)
22 .br
23 int lookupsym(char *fn, char *var, Symbol *s)
24 .br
25 int findsym(Loc loc, uint class, Symbol *s)
26 .PP
27 .ft B
28 int indexlsym(Symbol *s1, uint n, Symbol *s2)
29 .br
30 int lookuplsym(Symbol *s1, char *name, Symbol *s2)
31 .br
32 int findlsym(Symbol *s1, Loc loc, Symbol *s2)
33 .PP
34 .ft B
35 int symoff(char *a, uint n, ulong addr, uint class)
36 .PP
37 .ft B
38 int pc2file(ulong pc, char *file, uint n, ulong *line)
39 .br
40 int pc2line(ulong pc, ulong *line)
41 .br
42 int fileline(ulong pc, char *buf, uint n)
43 .br
44 int file2pc(char *file, ulong line, ulong *pc)
45 .br
46 int line2pc(ulong basepc, ulong line, ulong *pc)
47 .br
48 int fnbound(ulong pc, ulong bounds[2])
49 .SH DESCRIPTION
50 These functions provide machine-independent access to the
51 symbol table of an executable file or executing process.
52 .IR Mach (3),
53 .IR mach-file (3),
54 and
55 .IR mach-map (3)
56 describe additional library functions for
57 accessing executable files and executing processes.
58 .PP
59 .IR Symopen
60 uses the data in the
61 .B Fhdr
62 structure filled by
63 .I crackhdr
64 (see
65 .IR mach-file (3))
66 to initialize in-memory structures used to access the symbol
67 tables contained in the file.
68 .IR Symclose
69 frees the structures.
70 The rest of the functions described here access a composite
71 symbol table made up of all currently open tables.
72 .PP
73 The
74 .B Symbol
75 data structure:
76 .IP
77 .RS
78 .ft B
79 .nf
80 typedef struct Symbol Symbol;
81 struct Symbol
82 {
83 char *name;
84 Loc loc;
85 Loc hiloc;
86 char class;
87 char type;
88 \fI...\fP
89 };
90 .fi
91 .RE
92 .LP
93 describes a symbol table entry.
94 The
95 .B value
96 field contains the offset of the symbol within its
97 address space: global variables relative to the beginning
98 of the data segment, text beyond the start of the text
99 segment, and automatic variables and parameters relative
100 to the stack frame. The
101 .B type
102 field contains the type of the symbol:
103 .RS
104 .TP
105 .B T
106 text segment symbol
107 .TP
108 .B t
109 static text segment symbol
110 .TP
111 .B D
112 data segment symbol
113 .TP
114 .B d
115 static data segment symbol
116 .TP
117 .B B
118 bss segment symbol
119 .TP
120 .B b
121 static bss segment symbol
122 .TP
123 .B a
124 automatic (local) variable symbol
125 .TP
126 .B p
127 function parameter symbol
128 .RE
129 .PD
130 .LP
131 The
132 .B class
133 field assigns the symbol to a general class;
134 .BR CTEXT ,
135 .BR CDATA ,
136 .BR CAUTO ,
137 and
138 .B CPARAM
139 are the most popular.
140 .PP
141 .I Indexsym
142 stores information for the
143 .I n th
144 symbol into
145 .IR s .
146 The symbols are ordered by increasing address.
147 .PP
148 .I Lookupsym
149 fills a
150 .B Symbol
151 structure with symbol table information. Global variables
152 and functions are represented by a single name; local variables
153 and parameters are uniquely specified by a function and
154 variable name pair. Arguments
155 .I fn
156 and
157 .I var
158 contain the
159 name of a function and variable, respectively.
160 If both
161 are non-zero, the symbol table is searched for a parameter
162 or automatic variable. If only
163 .I var
164 is
165 zero, the text symbol table is searched for function
166 .IR fn .
167 If only
168 .I fn
169 is zero, the global variable table
170 is searched for
171 .IR var .
172 .PP
173 .I Findsym
174 returns the symbol table entry of type
175 .I class
176 stored near
177 .IR addr .
178 The selected symbol is a global variable or function with
179 address nearest to and less than or equal to
180 .IR addr .
181 Class specification
182 .B CDATA
183 searches only the global variable symbol table; class
184 .B CTEXT
185 limits the search to the text symbol table.
186 Class specification
187 .B CANY
188 searches the text table first, then the global table.
189 .PP
190 .IR Indexlsym ,
191 .IR lookuplsym ,
192 and
193 .IR findlsym
194 are similar to
195 .IR indexsym ,
196 .IR lookupsym ,
197 and
198 .IR findsym ,
199 but operate on the smaller symbol table of parameters and
200 variables local to the function represented by symbol
201 .IR s1 .
202 .PP
203 .I Indexlsym
204 writes symbol information for the
205 .IR n th
206 local symbol of function
207 .I s1
208 to
209 .IR s2 .
210 Function parameters appear first in the ordering, followed by local symbols.
211 .PP
212 .I Lookuplsym
213 writes symbol information for the symbol named
214 .I name
215 in function
216 .I s1
217 to
218 .IR s2 .
219 .PP
220 .I Findlsym
221 searches for a symbol local to the function
222 .I s1
223 whose location is exactly
224 .IR loc ,
225 writing its symbol information to
226 .IR s2 .
227 .I Loc
228 is almost always an indirection through a frame pointer register;
229 the details vary from architecture to architecture.
230 .PP
231 .I Symoff
232 converts a location to a symbol reference.
233 The string containing that reference is of the form
234 `name+offset', where `name' is the name of the
235 nearest symbol with an address less than or equal to the
236 target address, and `offset' is the hexadecimal offset beyond
237 that symbol. If `offset' is zero, only the name of the
238 symbol is printed.
239 If no symbol is found within 4096 bytes of the address, the address
240 is formatted as a hexadecimal address.
241 .I Buf
242 is the address of a buffer of
243 .I n
244 bytes to receive the formatted string.
245 .I Addr
246 is the address to be converted.
247 .I Type
248 is the type code of the search space:
249 .BR CTEXT ,
250 .BR CDATA ,
251 or
252 .BR CANY .
253 .I Symoff
254 returns the length of the formatted string contained in
255 .IR buf .
256 .PP
257 .I Pc2file
258 searches the symbol table to find the file and line number
259 corresponding to the instruction at program counter
260 .IR pc .
261 .I File
262 is the address of a buffer of
263 .I n
264 bytes to receive the file name.
265 .I Line
266 receives the line number.
267 .PP
268 .I Pc2line
269 is like
270 .I pc2file
271 but neglects to return information about the source file.
272 .PP
273 .I Fileline
274 is also like
275 .IR pc2file ,
276 but returns the file and line number in the
277 .IR n -byte
278 text buffer
279 .IR buf ,
280 formatted as
281 `file:line'.
282 .PP
283 .I File2pc
284 performs the opposite mapping:
285 it stores in
286 .I pc
287 a text address associated with
288 line
289 .I line
290 in file
291 .IR file .
292 .PP
293 .I Line2pc
294 is similar: it converts a line number to an
295 instruction address, storing it in
296 .IR pc .
297 Since a line number does not uniquely identify an
298 instruction (e.g., every source file has line 1),
299 .I basepc
300 specifies a text address from which
301 the search begins.
302 Usually this is the address of the first function in the
303 file of interest.
304 .PP
305 .I Fnbound
306 returns the start and end addresses of the function containing
307 the text address supplied as the first argument.
308 The second argument is an array of two unsigned longs;
309 .I fnbound
310 places the bounding addresses of the function in the
311 first and second elements of this array.
312 The start address is the address of the first instruction of the function;
313 the end address is the first address beyond the end of the target function.
314 .PP
315 All functions return 0 on success and \-1 on error.
316 When an error occurs, a message describing it is stored
317 in the system error buffer where it is available via
318 .IR errstr .
319 .SH SOURCE
320 .B /sys/src/libmach
321 .SH "SEE ALSO"
322 .IR mach (3),
323 .IR mach-file (3),
324 .IR mach-map (3)