blob: 2ff12b056d2f88892e296229c2490f5aec7aa516 [file] [log] [blame]
njnea27e462005-05-31 02:38:09 +00001
njn4bbdc972003-10-16 10:10:55 +00002/*--------------------------------------------------------------------*/
njnea27e462005-05-31 02:38:09 +00003/*--- Header for symbol table stuff. priv_symtab.h ---*/
njn4bbdc972003-10-16 10:10:55 +00004/*--------------------------------------------------------------------*/
5
6/*
njnb9c427c2004-12-01 14:14:42 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
njn4bbdc972003-10-16 10:10:55 +00009
njn53612422005-03-12 16:22:54 +000010 Copyright (C) 2000-2005 Julian Seward
njn4bbdc972003-10-16 10:10:55 +000011 jseward@acm.org
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
jsgfcb1d1c02003-10-14 21:55:10 +000030
njnea27e462005-05-31 02:38:09 +000031#ifndef __PRIV_SYMTAB_H
32#define __PRIV_SYMTAB_H
jsgfcb1d1c02003-10-14 21:55:10 +000033
jsgfcb1d1c02003-10-14 21:55:10 +000034/* A structure to hold an ELF symbol (very crudely). */
35typedef
36 struct {
37 Addr addr; /* lowest address of entity */
38 UInt size; /* size in bytes */
39 Char *name; /* name */
40 }
41 RiSym;
42
43/* Line count at which overflow happens, due to line numbers being stored as
44 * shorts in `struct nlist' in a.out.h. */
45#define LINENO_OVERFLOW (1 << (sizeof(short) * 8))
46
47#define LINENO_BITS 20
48#define LOC_SIZE_BITS (32 - LINENO_BITS)
49#define MAX_LINENO ((1 << LINENO_BITS) - 1)
50
51/* Unlikely to have any lines with instruction ranges > 4096 bytes */
52#define MAX_LOC_SIZE ((1 << LOC_SIZE_BITS) - 1)
53
54/* Number used to detect line number overflows; if one line is 60000-odd
55 * smaller than the previous, is was probably an overflow.
56 */
57#define OVERFLOW_DIFFERENCE (LINENO_OVERFLOW - 5000)
58
59/* A structure to hold addr-to-source info for a single line. There can be a
60 * lot of these, hence the dense packing. */
61typedef
62 struct {
63 /* Word 1 */
64 Addr addr; /* lowest address for this line */
65 /* Word 2 */
66 UShort size:LOC_SIZE_BITS; /* byte size; we catch overflows of this */
67 UInt lineno:LINENO_BITS; /* source line number, or zero */
68 /* Word 3 */
sewardj7cee6f92005-06-13 17:39:06 +000069 Char* filename; /* source filename */
70 /* Word 4 */
71 Char* dirname; /* source directory name */
jsgfcb1d1c02003-10-14 21:55:10 +000072 }
73 RiLoc;
74
75
76/* A structure to hold a set of variables in a particular scope */
77typedef struct _Scope Scope; /* a set of symbols in one scope */
78typedef struct _Sym Sym; /* a single symbol */
79typedef struct _ScopeRange ScopeRange; /* a range of code addreses a scope covers */
80
81typedef enum {
82 SyESPrel, /* on the stack (relative to ESP) */
83 SyEBPrel, /* on the stack (relative to EBP) */
84 SyReg, /* in a register */
85 SyType, /* a type definition */
86 SyStatic, /* a static variable */
87 SyGlobal, /* a global variable (XXX any different to static
88 in an outer scope?) */
89} SyKind;
90
91struct _Sym {
92 SymType *type; /* type */
93 Char *name; /* name */
94 SyKind kind; /* kind of symbol */
95
96 /* a value, depending on kind */
97 union {
njnc6168192004-11-29 13:54:10 +000098 OffT offset; /* offset on stack (-ve -> ebp; +ve -> esp) */
jsgfcb1d1c02003-10-14 21:55:10 +000099 Int regno; /* register number */
100 Addr addr; /* static or global address */
mueller5ed88f22004-01-06 16:02:29 +0000101 } u;
jsgfcb1d1c02003-10-14 21:55:10 +0000102};
103
104struct _Scope {
105 Scope *outer; /* outer (containing) scope */
106 UInt nsyms; /* number of symbols in this scope */
107 UInt depth; /* depth of scope */
108 Sym *syms; /* the symbols */
109};
110
111/* A structure to map a scope to a range of code addresses; scopes may
112 be broken into multiple ranges (before and after a nested scope) */
113struct _ScopeRange {
114 Addr addr; /* start address of this scope */
115 Int size; /* length of scope */
116 Scope *scope; /* symbols in scope */
117};
118
119#define STRCHUNKSIZE (64*1024)
120
sewardj5c638c22005-04-30 07:55:58 +0000121
sewardj35165532005-04-30 18:47:48 +0000122/* A structure to summarise CFI summary info for the code address
123 range [base .. base+len-1]. In short, if you know (sp,fp,ip) at
124 some point and ip is in the range [base .. base+len-1], it tells
125 you how to calculate (sp,fp) for the caller of the current
126 frame and also ra, the return address of the current frame.
127
128 First off, calculate CFA, the Canonical Frame Address, thusly:
129
130 cfa = if cfa_sprel then sp+cfa_off else fp+cfa_off
131
132 Once that is done, the previous frame's sp/fp values and this
133 frame's ra value can be calculated like this:
134
135 old_sp/fp/ra
136 = case sp/fp/ra_how of
137 CFIR_UNKNOWN -> we don't know, sorry
138 CFIR_SAME -> same as it was before (sp/fp only)
139 CFIR_CFAREL -> cfa + sp/fp/ra_off
140 CFIR_MEMCFAREL -> *( cfa + sp/fp/ra_off )
141*/
142
143#define CFIR_UNKNOWN ((UChar)0)
144#define CFIR_SAME ((UChar)1)
145#define CFIR_CFAREL ((UChar)2)
146#define CFIR_MEMCFAREL ((UChar)3)
147
sewardj5c638c22005-04-30 07:55:58 +0000148typedef
149 struct {
sewardj35165532005-04-30 18:47:48 +0000150 Addr base;
151 UInt len;
152 Bool cfa_sprel;
153 UChar ra_how; /* a CFIR_ value */
154 UChar sp_how; /* a CFIR_ value */
155 UChar fp_how; /* a CFIR_ value */
156 Int cfa_off;
157 Int ra_off;
158 Int sp_off;
159 Int fp_off;
sewardj5c638c22005-04-30 07:55:58 +0000160 }
161 CfiSI;
162
sewardj7eb7c582005-06-23 01:02:53 +0000163extern void ML_(ppCfiSI) ( CfiSI* );
sewardj35165532005-04-30 18:47:48 +0000164
sewardj5c638c22005-04-30 07:55:58 +0000165
jsgfcb1d1c02003-10-14 21:55:10 +0000166/* A structure which contains information pertaining to one mapped
nethercote46063202004-09-02 08:51:43 +0000167 text segment. (typedef in tool.h) */
jsgfcb1d1c02003-10-14 21:55:10 +0000168struct _SegInfo {
fitzhardinge98abfc72003-12-16 02:05:15 +0000169 struct _SegInfo* next; /* list of SegInfos */
170
njn0787fc02005-06-26 02:19:17 +0000171 Int ref;
fitzhardinge98abfc72003-12-16 02:05:15 +0000172
jsgfcb1d1c02003-10-14 21:55:10 +0000173 /* Description of the mapped segment. */
174 Addr start;
175 UInt size;
176 Char* filename; /* in mallocville */
njnc6168192004-11-29 13:54:10 +0000177 OffT foffset;
fitzhardinge98abfc72003-12-16 02:05:15 +0000178 Char* soname;
179
jsgfcb1d1c02003-10-14 21:55:10 +0000180 /* An expandable array of symbols. */
181 RiSym* symtab;
182 UInt symtab_used;
183 UInt symtab_size;
184 /* An expandable array of locations. */
185 RiLoc* loctab;
186 UInt loctab_used;
187 UInt loctab_size;
188 /* An expandable array of scope ranges. */
189 ScopeRange *scopetab;
190 UInt scopetab_used;
191 UInt scopetab_size;
sewardjbf603752005-05-02 00:36:27 +0000192 /* An expandable array of CFI summary info records. Also includes
193 summary address bounds, showing the min and max address covered
194 by any of the records, as an aid to fast searching. */
sewardj5c638c22005-04-30 07:55:58 +0000195 CfiSI* cfisi;
196 UInt cfisi_used;
197 UInt cfisi_size;
sewardjbf603752005-05-02 00:36:27 +0000198 Addr cfisi_minaddr;
199 Addr cfisi_maxaddr;
jsgfcb1d1c02003-10-14 21:55:10 +0000200
201 /* Expandable arrays of characters -- the string table.
202 Pointers into this are stable (the arrays are not reallocated)
203 */
204 struct strchunk {
205 UInt strtab_used;
206 struct strchunk *next;
207 Char strtab[STRCHUNKSIZE];
208 } *strchunks;
209
210 /* offset is what we need to add to symbol table entries
211 to get the real location of that symbol in memory.
212 */
njnc6168192004-11-29 13:54:10 +0000213 OffT offset;
jsgfcb1d1c02003-10-14 21:55:10 +0000214
nethercote996901a2004-08-03 13:29:09 +0000215 /* Bounds of data, BSS, PLT and GOT, so that tools can see what
jsgfcb1d1c02003-10-14 21:55:10 +0000216 section an address is in */
217 Addr plt_start;
218 UInt plt_size;
219 Addr got_start;
220 UInt got_size;
221 Addr data_start;
222 UInt data_size;
223 Addr bss_start;
224 UInt bss_size;
225
226 /* data used by stabs parser */
227 struct _StabTypeTab *stab_typetab;
228};
229
sewardj022bf2f2005-06-14 21:51:14 +0000230extern
sewardj7eb7c582005-06-23 01:02:53 +0000231Char *ML_(addStr) ( SegInfo* si, Char* str, Int len );
sewardj022bf2f2005-06-14 21:51:14 +0000232
233extern
sewardj7eb7c582005-06-23 01:02:53 +0000234void ML_(addScopeInfo) ( SegInfo* si, Addr this, Addr next, Scope *scope);
sewardj022bf2f2005-06-14 21:51:14 +0000235
236extern
sewardj7eb7c582005-06-23 01:02:53 +0000237void ML_(addLineInfo) ( SegInfo* si,
sewardj7cee6f92005-06-13 17:39:06 +0000238 Char* filename,
239 Char* dirname, /* NULL is allowable */
240 Addr this, Addr next, Int lineno, Int entry);
sewardj022bf2f2005-06-14 21:51:14 +0000241
242extern
sewardj7eb7c582005-06-23 01:02:53 +0000243void ML_(addCfiSI) ( SegInfo* si, CfiSI* cfisi );
jsgfcb1d1c02003-10-14 21:55:10 +0000244
245/* Non-fatal -- use vg_panic if terminal. */
sewardj022bf2f2005-06-14 21:51:14 +0000246extern
sewardj7eb7c582005-06-23 01:02:53 +0000247void ML_(symerr) ( Char* msg );
jsgfcb1d1c02003-10-14 21:55:10 +0000248
249/* --------------------
250 Stabs reader
251 -------------------- */
sewardj022bf2f2005-06-14 21:51:14 +0000252extern
sewardj7eb7c582005-06-23 01:02:53 +0000253void ML_(read_debuginfo_stabs) ( SegInfo* si,
jsgfcb1d1c02003-10-14 21:55:10 +0000254 UChar* stabC, Int stab_sz,
255 UChar* stabstr, Int stabstr_sz );
256
jsgfcb1d1c02003-10-14 21:55:10 +0000257/* --------------------
258 DWARF2 reader
259 -------------------- */
sewardj022bf2f2005-06-14 21:51:14 +0000260extern
sewardj7eb7c582005-06-23 01:02:53 +0000261void ML_(read_debuginfo_dwarf2)
sewardj022bf2f2005-06-14 21:51:14 +0000262 ( SegInfo* si,
263 UChar* debuginfo, Int debug_info_sz, /* .debug_info */
264 UChar* debugabbrev, /* .debug_abbrev */
265 UChar* debugline, Int debug_line_sz, /* .debug_line */
266 UChar* debugstr );
jseward8b3131a2003-12-13 23:16:26 +0000267
268/* --------------------
269 DWARF1 reader
270 -------------------- */
sewardj022bf2f2005-06-14 21:51:14 +0000271extern
sewardj7eb7c582005-06-23 01:02:53 +0000272void ML_(read_debuginfo_dwarf1) ( SegInfo* si,
jseward8b3131a2003-12-13 23:16:26 +0000273 UChar* dwarf1d, Int dwarf1d_sz,
274 UChar* dwarf1l, Int dwarf1l_sz );
275
sewardj5c638c22005-04-30 07:55:58 +0000276/* --------------------
277 CFI reader
278 -------------------- */
sewardj022bf2f2005-06-14 21:51:14 +0000279extern
sewardj7eb7c582005-06-23 01:02:53 +0000280void ML_(read_callframe_info_dwarf2)
tom2fd38902005-05-01 15:14:01 +0000281 ( /*OUT*/SegInfo* si, UChar* ehframe, Int ehframe_sz, Addr ehframe_addr );
sewardj5c638c22005-04-30 07:55:58 +0000282
jsgfcb1d1c02003-10-14 21:55:10 +0000283
njnea27e462005-05-31 02:38:09 +0000284#endif // __PRIV_SYMTAB_H
njn4bbdc972003-10-16 10:10:55 +0000285
286/*--------------------------------------------------------------------*/
njnea27e462005-05-31 02:38:09 +0000287/*--- end ---*/
njn4bbdc972003-10-16 10:10:55 +0000288/*--------------------------------------------------------------------*/