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 .TP
129 .B U
130 undefined symbol
131 .RE
132 .PD
133 .LP
134 The
135 .B class
136 field assigns the symbol to a general class;
137 .BR CTEXT ,
138 .BR CDATA ,
139 .BR CAUTO ,
140 and
141 .B CPARAM
142 are the most popular.
143 .PP
144 .I Indexsym
145 stores information for the
146 .I n th
147 symbol into
148 .IR s .
149 The symbols are ordered by increasing address.
150 .PP
151 .I Lookupsym
152 fills a
153 .B Symbol
154 structure with symbol table information. Global variables
155 and functions are represented by a single name; local variables
156 and parameters are uniquely specified by a function and
157 variable name pair. Arguments
158 .I fn
159 and
160 .I var
161 contain the
162 name of a function and variable, respectively.
163 If both
164 are non-zero, the symbol table is searched for a parameter
165 or automatic variable. If only
166 .I var
167 is
168 zero, the text symbol table is searched for function
169 .IR fn .
170 If only
171 .I fn
172 is zero, the global variable table
173 is searched for
174 .IR var .
175 .PP
176 .I Findsym
177 returns the symbol table entry of type
178 .I class
179 stored near
180 .IR addr .
181 The selected symbol is a global variable or function with
182 address nearest to and less than or equal to
183 .IR addr .
184 Class specification
185 .B CDATA
186 searches only the global variable symbol table; class
187 .B CTEXT
188 limits the search to the text symbol table.
189 Class specification
190 .B CANY
191 searches the text table first, then the global table.
192 .PP
193 .IR Indexlsym ,
194 .IR lookuplsym ,
195 and
196 .IR findlsym
197 are similar to
198 .IR indexsym ,
199 .IR lookupsym ,
200 and
201 .IR findsym ,
202 but operate on the smaller symbol table of parameters and
203 variables local to the function represented by symbol
204 .IR s1 .
205 .PP
206 .I Indexlsym
207 writes symbol information for the
208 .IR n th
209 local symbol of function
210 .I s1
211 to
212 .IR s2 .
213 Function parameters appear first in the ordering, followed by local symbols.
214 .PP
215 .I Lookuplsym
216 writes symbol information for the symbol named
217 .I name
218 in function
219 .I s1
220 to
221 .IR s2 .
222 .PP
223 .I Findlsym
224 searches for a symbol local to the function
225 .I s1
226 whose location is exactly
227 .IR loc ,
228 writing its symbol information to
229 .IR s2 .
230 .I Loc
231 is almost always an indirection through a frame pointer register;
232 the details vary from architecture to architecture.
233 .PP
234 .I Symoff
235 converts a location to a symbol reference.
236 The string containing that reference is of the form
237 `name+offset', where `name' is the name of the
238 nearest symbol with an address less than or equal to the
239 target address, and `offset' is the hexadecimal offset beyond
240 that symbol. If `offset' is zero, only the name of the
241 symbol is printed.
242 If no symbol is found within 4096 bytes of the address, the address
243 is formatted as a hexadecimal address.
244 .I Buf
245 is the address of a buffer of
246 .I n
247 bytes to receive the formatted string.
248 .I Addr
249 is the address to be converted.
250 .I Type
251 is the type code of the search space:
252 .BR CTEXT ,
253 .BR CDATA ,
254 or
255 .BR CANY .
256 .I Symoff
257 returns the length of the formatted string contained in
258 .IR buf .
259 .PP
260 .I Pc2file
261 searches the symbol table to find the file and line number
262 corresponding to the instruction at program counter
263 .IR pc .
264 .I File
265 is the address of a buffer of
266 .I n
267 bytes to receive the file name.
268 .I Line
269 receives the line number.
270 .PP
271 .I Pc2line
272 is like
273 .I pc2file
274 but neglects to return information about the source file.
275 .PP
276 .I Fileline
277 is also like
278 .IR pc2file ,
279 but returns the file and line number in the
280 .IR n -byte
281 text buffer
282 .IR buf ,
283 formatted as
284 `file:line'.
285 .PP
286 .I File2pc
287 performs the opposite mapping:
288 it stores in
289 .I pc
290 a text address associated with
291 line
292 .I line
293 in file
294 .IR file .
295 .PP
296 .I Line2pc
297 is similar: it converts a line number to an
298 instruction address, storing it in
299 .IR pc .
300 Since a line number does not uniquely identify an
301 instruction (e.g., every source file has line 1),
302 .I basepc
303 specifies a text address from which
304 the search begins.
305 Usually this is the address of the first function in the
306 file of interest.
307 .PP
308 .I Fnbound
309 returns the start and end addresses of the function containing
310 the text address supplied as the first argument.
311 The second argument is an array of two unsigned longs;
312 .I fnbound
313 places the bounding addresses of the function in the
314 first and second elements of this array.
315 The start address is the address of the first instruction of the function;
316 the end address is the first address beyond the end of the target function.
317 .PP
318 All functions return 0 on success and \-1 on error.
319 When an error occurs, a message describing it is stored
320 in the system error buffer where it is available via
321 .IR errstr .
322 .SH SOURCE
323 .B /usr/local/plan9/src/libmach
324 .SH "SEE ALSO"
325 .IR mach (3),
326 .IR mach-file (3),
327 .IR mach-map (3)