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