njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 1 | |
njn | 4bbdc97 | 2003-10-16 10:10:55 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 3 | /*--- Header for symbol table stuff. priv_symtab.h ---*/ |
njn | 4bbdc97 | 2003-10-16 10:10:55 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
njn | 4bbdc97 | 2003-10-16 10:10:55 +0000 | [diff] [blame] | 9 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2005 Julian Seward |
njn | 4bbdc97 | 2003-10-16 10:10:55 +0000 | [diff] [blame] | 11 | 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 | */ |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 30 | |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 31 | #ifndef __PRIV_SYMTAB_H |
| 32 | #define __PRIV_SYMTAB_H |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 33 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 34 | /* A structure to hold an ELF symbol (very crudely). */ |
| 35 | typedef |
| 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. */ |
| 61 | typedef |
| 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 */ |
sewardj | 7cee6f9 | 2005-06-13 17:39:06 +0000 | [diff] [blame] | 69 | Char* filename; /* source filename */ |
| 70 | /* Word 4 */ |
| 71 | Char* dirname; /* source directory name */ |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 72 | } |
| 73 | RiLoc; |
| 74 | |
| 75 | |
| 76 | /* A structure to hold a set of variables in a particular scope */ |
| 77 | typedef struct _Scope Scope; /* a set of symbols in one scope */ |
| 78 | typedef struct _Sym Sym; /* a single symbol */ |
| 79 | typedef struct _ScopeRange ScopeRange; /* a range of code addreses a scope covers */ |
| 80 | |
| 81 | typedef 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 | |
| 91 | struct _Sym { |
| 92 | SymType *type; /* type */ |
| 93 | Char *name; /* name */ |
| 94 | SyKind kind; /* kind of symbol */ |
| 95 | |
| 96 | /* a value, depending on kind */ |
| 97 | union { |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 98 | OffT offset; /* offset on stack (-ve -> ebp; +ve -> esp) */ |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 99 | Int regno; /* register number */ |
| 100 | Addr addr; /* static or global address */ |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 101 | } u; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | struct _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) */ |
| 113 | struct _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 | |
sewardj | 5c638c2 | 2005-04-30 07:55:58 +0000 | [diff] [blame] | 121 | |
sewardj | 3516553 | 2005-04-30 18:47:48 +0000 | [diff] [blame] | 122 | /* 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 | |
sewardj | 5c638c2 | 2005-04-30 07:55:58 +0000 | [diff] [blame] | 148 | typedef |
| 149 | struct { |
sewardj | 3516553 | 2005-04-30 18:47:48 +0000 | [diff] [blame] | 150 | 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; |
sewardj | 5c638c2 | 2005-04-30 07:55:58 +0000 | [diff] [blame] | 160 | } |
| 161 | CfiSI; |
| 162 | |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 163 | extern void ML_(ppCfiSI) ( CfiSI* ); |
sewardj | 3516553 | 2005-04-30 18:47:48 +0000 | [diff] [blame] | 164 | |
sewardj | 5c638c2 | 2005-04-30 07:55:58 +0000 | [diff] [blame] | 165 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 166 | /* A structure which contains information pertaining to one mapped |
nethercote | 4606320 | 2004-09-02 08:51:43 +0000 | [diff] [blame] | 167 | text segment. (typedef in tool.h) */ |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 168 | struct _SegInfo { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 169 | struct _SegInfo* next; /* list of SegInfos */ |
| 170 | |
njn | 0787fc0 | 2005-06-26 02:19:17 +0000 | [diff] [blame] | 171 | Int ref; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 172 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 173 | /* Description of the mapped segment. */ |
| 174 | Addr start; |
| 175 | UInt size; |
| 176 | Char* filename; /* in mallocville */ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 177 | OffT foffset; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 178 | Char* soname; |
| 179 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 180 | /* 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; |
sewardj | bf60375 | 2005-05-02 00:36:27 +0000 | [diff] [blame] | 192 | /* 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. */ |
sewardj | 5c638c2 | 2005-04-30 07:55:58 +0000 | [diff] [blame] | 195 | CfiSI* cfisi; |
| 196 | UInt cfisi_used; |
| 197 | UInt cfisi_size; |
sewardj | bf60375 | 2005-05-02 00:36:27 +0000 | [diff] [blame] | 198 | Addr cfisi_minaddr; |
| 199 | Addr cfisi_maxaddr; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 200 | |
| 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 | */ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 213 | OffT offset; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 214 | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 215 | /* Bounds of data, BSS, PLT and GOT, so that tools can see what |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 216 | 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 | |
sewardj | 022bf2f | 2005-06-14 21:51:14 +0000 | [diff] [blame] | 230 | extern |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 231 | Char *ML_(addStr) ( SegInfo* si, Char* str, Int len ); |
sewardj | 022bf2f | 2005-06-14 21:51:14 +0000 | [diff] [blame] | 232 | |
| 233 | extern |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 234 | void ML_(addScopeInfo) ( SegInfo* si, Addr this, Addr next, Scope *scope); |
sewardj | 022bf2f | 2005-06-14 21:51:14 +0000 | [diff] [blame] | 235 | |
| 236 | extern |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 237 | void ML_(addLineInfo) ( SegInfo* si, |
sewardj | 7cee6f9 | 2005-06-13 17:39:06 +0000 | [diff] [blame] | 238 | Char* filename, |
| 239 | Char* dirname, /* NULL is allowable */ |
| 240 | Addr this, Addr next, Int lineno, Int entry); |
sewardj | 022bf2f | 2005-06-14 21:51:14 +0000 | [diff] [blame] | 241 | |
| 242 | extern |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 243 | void ML_(addCfiSI) ( SegInfo* si, CfiSI* cfisi ); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 244 | |
| 245 | /* Non-fatal -- use vg_panic if terminal. */ |
sewardj | 022bf2f | 2005-06-14 21:51:14 +0000 | [diff] [blame] | 246 | extern |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 247 | void ML_(symerr) ( Char* msg ); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 248 | |
| 249 | /* -------------------- |
| 250 | Stabs reader |
| 251 | -------------------- */ |
sewardj | 022bf2f | 2005-06-14 21:51:14 +0000 | [diff] [blame] | 252 | extern |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 253 | void ML_(read_debuginfo_stabs) ( SegInfo* si, |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 254 | UChar* stabC, Int stab_sz, |
| 255 | UChar* stabstr, Int stabstr_sz ); |
| 256 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 257 | /* -------------------- |
| 258 | DWARF2 reader |
| 259 | -------------------- */ |
sewardj | 022bf2f | 2005-06-14 21:51:14 +0000 | [diff] [blame] | 260 | extern |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 261 | void ML_(read_debuginfo_dwarf2) |
sewardj | 022bf2f | 2005-06-14 21:51:14 +0000 | [diff] [blame] | 262 | ( 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 ); |
jseward | 8b3131a | 2003-12-13 23:16:26 +0000 | [diff] [blame] | 267 | |
| 268 | /* -------------------- |
| 269 | DWARF1 reader |
| 270 | -------------------- */ |
sewardj | 022bf2f | 2005-06-14 21:51:14 +0000 | [diff] [blame] | 271 | extern |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 272 | void ML_(read_debuginfo_dwarf1) ( SegInfo* si, |
jseward | 8b3131a | 2003-12-13 23:16:26 +0000 | [diff] [blame] | 273 | UChar* dwarf1d, Int dwarf1d_sz, |
| 274 | UChar* dwarf1l, Int dwarf1l_sz ); |
| 275 | |
sewardj | 5c638c2 | 2005-04-30 07:55:58 +0000 | [diff] [blame] | 276 | /* -------------------- |
| 277 | CFI reader |
| 278 | -------------------- */ |
sewardj | 022bf2f | 2005-06-14 21:51:14 +0000 | [diff] [blame] | 279 | extern |
sewardj | 7eb7c58 | 2005-06-23 01:02:53 +0000 | [diff] [blame] | 280 | void ML_(read_callframe_info_dwarf2) |
tom | 2fd3890 | 2005-05-01 15:14:01 +0000 | [diff] [blame] | 281 | ( /*OUT*/SegInfo* si, UChar* ehframe, Int ehframe_sz, Addr ehframe_addr ); |
sewardj | 5c638c2 | 2005-04-30 07:55:58 +0000 | [diff] [blame] | 282 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 283 | |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 284 | #endif // __PRIV_SYMTAB_H |
njn | 4bbdc97 | 2003-10-16 10:10:55 +0000 | [diff] [blame] | 285 | |
| 286 | /*--------------------------------------------------------------------*/ |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 287 | /*--- end ---*/ |
njn | 4bbdc97 | 2003-10-16 10:10:55 +0000 | [diff] [blame] | 288 | /*--------------------------------------------------------------------*/ |