sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | /*--------------------------------------------------------------------*/ |
| 2 | /*--- Management of symbols and debugging information. ---*/ |
| 3 | /*--- vg_symtab2.c ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, an extensible x86 protected-mode |
| 8 | emulator for monitoring program execution on x86-Unixes. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 9 | |
| 10 | Copyright (C) 2000-2002 Julian Seward |
| 11 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 28 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | #include "vg_include.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 32 | |
| 33 | #include <elf.h> /* ELF defns */ |
| 34 | #include <a.out.h> /* stabs defns */ |
| 35 | |
njn | 9aae674 | 2002-04-30 13:44:01 +0000 | [diff] [blame] | 36 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 37 | /* Majorly rewritten Sun 3 Feb 02 to enable loading symbols from |
| 38 | dlopen()ed libraries, which is something that KDE3 does a lot. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 39 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 40 | Stabs reader greatly improved by Nick Nethercote, Apr 02. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 41 | */ |
| 42 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 43 | /* Set to True when first debug info search is performed */ |
| 44 | Bool VG_(using_debug_info) = False; |
| 45 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 46 | /*------------------------------------------------------------*/ |
| 47 | /*--- Structs n stuff ---*/ |
| 48 | /*------------------------------------------------------------*/ |
| 49 | |
| 50 | /* A structure to hold an ELF symbol (very crudely). */ |
| 51 | typedef |
| 52 | struct { |
| 53 | Addr addr; /* lowest address of entity */ |
| 54 | UInt size; /* size in bytes */ |
| 55 | Int nmoff; /* offset of name in this SegInfo's str tab */ |
| 56 | } |
| 57 | RiSym; |
| 58 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 59 | /* Line count at which overflow happens, due to line numbers being stored as |
| 60 | * shorts in `struct nlist' in a.out.h. */ |
| 61 | #define LINENO_OVERFLOW (1 << (sizeof(short) * 8)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 62 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 63 | #define LINENO_BITS 20 |
| 64 | #define LOC_SIZE_BITS (32 - LINENO_BITS) |
sewardj | 97ff05f | 2002-05-09 01:32:57 +0000 | [diff] [blame] | 65 | #define MAX_LINENO ((1 << LINENO_BITS) - 1) |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 66 | |
| 67 | /* Unlikely to have any lines with instruction ranges > 4096 bytes */ |
sewardj | 97ff05f | 2002-05-09 01:32:57 +0000 | [diff] [blame] | 68 | #define MAX_LOC_SIZE ((1 << LOC_SIZE_BITS) - 1) |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 69 | |
| 70 | /* Number used to detect line number overflows; if one line is 60000-odd |
| 71 | * smaller than the previous, is was probably an overflow. |
| 72 | */ |
| 73 | #define OVERFLOW_DIFFERENCE (LINENO_OVERFLOW - 5000) |
| 74 | |
| 75 | /* A structure to hold addr-to-source info for a single line. There can be a |
| 76 | * lot of these, hence the dense packing. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 77 | typedef |
| 78 | struct { |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 79 | /* Word 1 */ |
| 80 | Addr addr; /* lowest address for this line */ |
| 81 | /* Word 2 */ |
| 82 | UShort size:LOC_SIZE_BITS; /* byte size; we catch overflows of this */ |
| 83 | UInt lineno:LINENO_BITS; /* source line number, or zero */ |
| 84 | /* Word 3 */ |
| 85 | UInt fnmoff; /* source filename; offset in this |
| 86 | SegInfo's str tab */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 87 | } |
| 88 | RiLoc; |
| 89 | |
| 90 | |
| 91 | /* A structure which contains information pertaining to one mapped |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 92 | text segment. (typedef in vg_skin.h) */ |
| 93 | struct _SegInfo { |
| 94 | struct _SegInfo* next; |
| 95 | /* Description of the mapped segment. */ |
| 96 | Addr start; |
| 97 | UInt size; |
| 98 | UChar* filename; /* in mallocville */ |
| 99 | UInt foffset; |
| 100 | /* An expandable array of symbols. */ |
| 101 | RiSym* symtab; |
| 102 | UInt symtab_used; |
| 103 | UInt symtab_size; |
| 104 | /* An expandable array of locations. */ |
| 105 | RiLoc* loctab; |
| 106 | UInt loctab_used; |
| 107 | UInt loctab_size; |
| 108 | /* An expandable array of characters -- the string table. */ |
| 109 | Char* strtab; |
| 110 | UInt strtab_used; |
| 111 | UInt strtab_size; |
| 112 | /* offset is what we need to add to symbol table entries |
| 113 | to get the real location of that symbol in memory. |
| 114 | For executables, offset is zero. |
| 115 | For .so's, offset == base_addr. |
| 116 | This seems like a giant kludge to me. |
| 117 | */ |
| 118 | UInt offset; |
| 119 | |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 120 | /* Bounds of data, BSS, PLT and GOT, so that skins can see what |
| 121 | section an address is in */ |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 122 | Addr plt_start; |
| 123 | UInt plt_size; |
| 124 | Addr got_start; |
| 125 | UInt got_size; |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 126 | Addr data_start; |
| 127 | UInt data_size; |
| 128 | Addr bss_start; |
| 129 | UInt bss_size; |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 130 | }; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 131 | |
| 132 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 133 | static void freeSegInfo ( SegInfo* si ) |
| 134 | { |
| 135 | vg_assert(si != NULL); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 136 | if (si->filename) VG_(arena_free)(VG_AR_SYMTAB, si->filename); |
| 137 | if (si->symtab) VG_(arena_free)(VG_AR_SYMTAB, si->symtab); |
| 138 | if (si->loctab) VG_(arena_free)(VG_AR_SYMTAB, si->loctab); |
| 139 | if (si->strtab) VG_(arena_free)(VG_AR_SYMTAB, si->strtab); |
| 140 | VG_(arena_free)(VG_AR_SYMTAB, si); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | /*------------------------------------------------------------*/ |
| 145 | /*--- Adding stuff ---*/ |
| 146 | /*------------------------------------------------------------*/ |
| 147 | |
| 148 | /* Add a str to the string table, including terminating zero, and |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 149 | return offset of the string in vg_strtab. Unless it's been seen |
| 150 | recently, in which case we find the old index and return that. |
| 151 | This avoids the most egregious duplications. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 152 | |
| 153 | static __inline__ |
| 154 | Int addStr ( SegInfo* si, Char* str ) |
| 155 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 156 | # define EMPTY 0xffffffff |
| 157 | # define NN 5 |
| 158 | |
| 159 | /* prevN[0] has the most recent, prevN[NN-1] the least recent */ |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 160 | static UInt prevN[NN] = { EMPTY, EMPTY, EMPTY, EMPTY, EMPTY }; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 161 | static SegInfo* curr_si = NULL; |
| 162 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 163 | Char* new_tab; |
| 164 | Int new_sz, i, space_needed; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 165 | |
| 166 | /* Avoid gratuitous duplication: if we saw `str' within the last NN, |
| 167 | * within this segment, return that index. Saves about 200KB in glibc, |
| 168 | * extra time taken is too small to measure. --NJN 2002-Aug-30 */ |
| 169 | if (curr_si == si) { |
| 170 | for (i = NN-1; i >= 0; i--) { |
sewardj | cda419b | 2002-10-01 08:59:36 +0000 | [diff] [blame] | 171 | if (EMPTY != prevN[i] |
| 172 | && NULL != si->strtab |
| 173 | && 0 == VG_(strcmp)(str, &si->strtab[prevN[i]])) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 174 | return prevN[i]; |
| 175 | } |
| 176 | } |
| 177 | } else { |
| 178 | /* New segment */ |
| 179 | curr_si = si; |
sewardj | cda419b | 2002-10-01 08:59:36 +0000 | [diff] [blame] | 180 | for (i = 0; i < NN; i++) prevN[i] = EMPTY; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 181 | } |
| 182 | /* Shuffle prevous ones along, put new one in. */ |
| 183 | for (i = NN-1; i > 0; i--) prevN[i] = prevN[i-1]; |
| 184 | prevN[0] = si->strtab_used; |
| 185 | |
| 186 | # undef EMPTY |
| 187 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 188 | space_needed = 1 + VG_(strlen)(str); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 189 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 190 | if (si->strtab_used + space_needed > si->strtab_size) { |
| 191 | new_sz = 2 * si->strtab_size; |
| 192 | if (new_sz == 0) new_sz = 5000; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 193 | new_tab = VG_(arena_malloc)(VG_AR_SYMTAB, new_sz); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 194 | if (si->strtab != NULL) { |
| 195 | for (i = 0; i < si->strtab_used; i++) |
| 196 | new_tab[i] = si->strtab[i]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 197 | VG_(arena_free)(VG_AR_SYMTAB, si->strtab); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 198 | } |
| 199 | si->strtab = new_tab; |
| 200 | si->strtab_size = new_sz; |
| 201 | } |
| 202 | |
| 203 | for (i = 0; i < space_needed; i++) |
| 204 | si->strtab[si->strtab_used+i] = str[i]; |
| 205 | |
| 206 | si->strtab_used += space_needed; |
| 207 | vg_assert(si->strtab_used <= si->strtab_size); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 208 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 209 | return si->strtab_used - space_needed; |
| 210 | } |
| 211 | |
| 212 | /* Add a symbol to the symbol table. */ |
| 213 | |
| 214 | static __inline__ |
| 215 | void addSym ( SegInfo* si, RiSym* sym ) |
| 216 | { |
| 217 | Int new_sz, i; |
| 218 | RiSym* new_tab; |
| 219 | |
| 220 | /* Ignore zero-sized syms. */ |
| 221 | if (sym->size == 0) return; |
| 222 | |
| 223 | if (si->symtab_used == si->symtab_size) { |
| 224 | new_sz = 2 * si->symtab_size; |
| 225 | if (new_sz == 0) new_sz = 500; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 226 | new_tab = VG_(arena_malloc)(VG_AR_SYMTAB, new_sz * sizeof(RiSym) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 227 | if (si->symtab != NULL) { |
| 228 | for (i = 0; i < si->symtab_used; i++) |
| 229 | new_tab[i] = si->symtab[i]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 230 | VG_(arena_free)(VG_AR_SYMTAB, si->symtab); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 231 | } |
| 232 | si->symtab = new_tab; |
| 233 | si->symtab_size = new_sz; |
| 234 | } |
| 235 | |
| 236 | si->symtab[si->symtab_used] = *sym; |
| 237 | si->symtab_used++; |
| 238 | vg_assert(si->symtab_used <= si->symtab_size); |
| 239 | } |
| 240 | |
| 241 | /* Add a location to the location table. */ |
| 242 | |
| 243 | static __inline__ |
| 244 | void addLoc ( SegInfo* si, RiLoc* loc ) |
| 245 | { |
| 246 | Int new_sz, i; |
| 247 | RiLoc* new_tab; |
| 248 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 249 | /* Zero-sized locs should have been ignored earlier */ |
| 250 | vg_assert(loc->size > 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 251 | |
| 252 | if (si->loctab_used == si->loctab_size) { |
| 253 | new_sz = 2 * si->loctab_size; |
| 254 | if (new_sz == 0) new_sz = 500; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 255 | new_tab = VG_(arena_malloc)(VG_AR_SYMTAB, new_sz * sizeof(RiLoc) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 256 | if (si->loctab != NULL) { |
| 257 | for (i = 0; i < si->loctab_used; i++) |
| 258 | new_tab[i] = si->loctab[i]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 259 | VG_(arena_free)(VG_AR_SYMTAB, si->loctab); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 260 | } |
| 261 | si->loctab = new_tab; |
| 262 | si->loctab_size = new_sz; |
| 263 | } |
| 264 | |
| 265 | si->loctab[si->loctab_used] = *loc; |
| 266 | si->loctab_used++; |
| 267 | vg_assert(si->loctab_used <= si->loctab_size); |
| 268 | } |
| 269 | |
| 270 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 271 | /* Top-level place to call to add a source-location mapping entry. */ |
| 272 | |
| 273 | static __inline__ |
| 274 | void addLineInfo ( SegInfo* si, |
| 275 | Int fnmoff, |
| 276 | Addr this, |
| 277 | Addr next, |
| 278 | Int lineno, |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 279 | Int entry /* only needed for debug printing */ |
| 280 | ) |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 281 | { |
| 282 | RiLoc loc; |
| 283 | Int size = next - this; |
| 284 | |
| 285 | /* Ignore zero-sized locs */ |
| 286 | if (this == next) return; |
| 287 | |
| 288 | /* Maximum sanity checking. Some versions of GNU as do a shabby |
| 289 | * job with stabs entries; if anything looks suspicious, revert to |
| 290 | * a size of 1. This should catch the instruction of interest |
| 291 | * (since if using asm-level debug info, one instruction will |
| 292 | * correspond to one line, unlike with C-level debug info where |
| 293 | * multiple instructions can map to the one line), but avoid |
| 294 | * catching any other instructions bogusly. */ |
| 295 | if (this > next) { |
| 296 | VG_(message)(Vg_DebugMsg, |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 297 | "warning: line info addresses out of order " |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 298 | "at entry %d: 0x%x 0x%x", entry, this, next); |
| 299 | size = 1; |
| 300 | } |
| 301 | |
| 302 | if (size > MAX_LOC_SIZE) { |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 303 | if (0) |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 304 | VG_(message)(Vg_DebugMsg, |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 305 | "warning: line info address range too large " |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 306 | "at entry %d: %d", entry, size); |
| 307 | size = 1; |
| 308 | } |
| 309 | |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 310 | /* vg_assert(this < si->start + si->size && next-1 >= si->start); */ |
njn | e306ffe | 2002-06-08 13:34:17 +0000 | [diff] [blame] | 311 | if (this >= si->start + si->size || next-1 < si->start) { |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 312 | if (0) |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 313 | VG_(message)(Vg_DebugMsg, |
| 314 | "warning: ignoring line info entry falling " |
| 315 | "outside current SegInfo: %p %p %p %p", |
| 316 | si->start, si->start + si->size, |
| 317 | this, next-1); |
njn | e306ffe | 2002-06-08 13:34:17 +0000 | [diff] [blame] | 318 | return; |
| 319 | } |
| 320 | |
| 321 | vg_assert(lineno >= 0); |
| 322 | if (lineno > MAX_LINENO) { |
| 323 | VG_(message)(Vg_UserMsg, |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 324 | "warning: ignoring line info entry with " |
| 325 | "huge line number (%d)", lineno); |
njn | e306ffe | 2002-06-08 13:34:17 +0000 | [diff] [blame] | 326 | VG_(message)(Vg_UserMsg, |
| 327 | " Can't handle line numbers " |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 328 | "greater than %d, sorry", MAX_LINENO); |
njn | e306ffe | 2002-06-08 13:34:17 +0000 | [diff] [blame] | 329 | return; |
| 330 | } |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 331 | |
| 332 | loc.addr = this; |
| 333 | loc.size = (UShort)size; |
| 334 | loc.lineno = lineno; |
| 335 | loc.fnmoff = fnmoff; |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 336 | |
| 337 | if (0) VG_(message)(Vg_DebugMsg, |
| 338 | "addLoc: addr %p, size %d, line %d, file %s", |
| 339 | this,size,lineno,&si->strtab[fnmoff]); |
| 340 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 341 | addLoc ( si, &loc ); |
| 342 | } |
| 343 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 344 | /*------------------------------------------------------------*/ |
| 345 | /*--- Helpers ---*/ |
| 346 | /*------------------------------------------------------------*/ |
| 347 | |
| 348 | /* Non-fatal -- use vg_panic if terminal. */ |
| 349 | static |
| 350 | void vg_symerr ( Char* msg ) |
| 351 | { |
| 352 | if (VG_(clo_verbosity) > 1) |
| 353 | VG_(message)(Vg_UserMsg,"%s", msg ); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /* Print a symbol. */ |
| 358 | static |
| 359 | void printSym ( SegInfo* si, Int i ) |
| 360 | { |
| 361 | VG_(printf)( "%5d: %8p .. %8p (%d) %s\n", |
| 362 | i, |
| 363 | si->symtab[i].addr, |
| 364 | si->symtab[i].addr + si->symtab[i].size - 1, si->symtab[i].size, |
| 365 | &si->strtab[si->symtab[i].nmoff] ); |
| 366 | } |
| 367 | |
| 368 | |
| 369 | #if 0 |
| 370 | /* Print the entire sym tab. */ |
| 371 | static __attribute__ ((unused)) |
| 372 | void printSymtab ( void ) |
| 373 | { |
| 374 | Int i; |
| 375 | VG_(printf)("\n------ BEGIN vg_symtab ------\n"); |
| 376 | for (i = 0; i < vg_symtab_used; i++) |
| 377 | printSym(i); |
| 378 | VG_(printf)("------ BEGIN vg_symtab ------\n"); |
| 379 | } |
| 380 | #endif |
| 381 | |
| 382 | #if 0 |
| 383 | /* Paranoid strcat. */ |
| 384 | static |
| 385 | void safeCopy ( UChar* dst, UInt maxlen, UChar* src ) |
| 386 | { |
| 387 | UInt i = 0, j = 0; |
| 388 | while (True) { |
| 389 | if (i >= maxlen) return; |
| 390 | if (dst[i] == 0) break; |
| 391 | i++; |
| 392 | } |
| 393 | while (True) { |
| 394 | if (i >= maxlen) return; |
| 395 | dst[i] = src[j]; |
| 396 | if (src[j] == 0) return; |
| 397 | i++; j++; |
| 398 | } |
| 399 | } |
| 400 | #endif |
| 401 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 402 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 403 | /*------------------------------------------------------------*/ |
| 404 | /*--- Canonicalisers ---*/ |
| 405 | /*------------------------------------------------------------*/ |
| 406 | |
| 407 | /* Sort the symtab by starting address, and emit warnings if any |
| 408 | symbols have overlapping address ranges. We use that old chestnut, |
| 409 | shellsort. Mash the table around so as to establish the property |
| 410 | that addresses are in order and the ranges to not overlap. This |
| 411 | facilitates using binary search to map addresses to symbols when we |
| 412 | come to query the table. |
| 413 | */ |
| 414 | static |
| 415 | void canonicaliseSymtab ( SegInfo* si ) |
| 416 | { |
| 417 | /* Magic numbers due to Janet Incerpi and Robert Sedgewick. */ |
| 418 | Int incs[16] = { 1, 3, 7, 21, 48, 112, 336, 861, 1968, |
| 419 | 4592, 13776, 33936, 86961, 198768, |
| 420 | 463792, 1391376 }; |
| 421 | Int lo = 0; |
| 422 | Int hi = si->symtab_used-1; |
| 423 | Int i, j, h, bigN, hp, n_merged, n_truncated; |
| 424 | RiSym v; |
| 425 | Addr s1, s2, e1, e2; |
| 426 | |
| 427 | # define SWAP(ty,aa,bb) \ |
| 428 | do { ty tt = (aa); (aa) = (bb); (bb) = tt; } while (0) |
| 429 | |
| 430 | bigN = hi - lo + 1; if (bigN < 2) return; |
| 431 | hp = 0; while (hp < 16 && incs[hp] < bigN) hp++; hp--; |
| 432 | vg_assert(0 <= hp && hp < 16); |
| 433 | |
| 434 | for (; hp >= 0; hp--) { |
| 435 | h = incs[hp]; |
| 436 | i = lo + h; |
| 437 | while (1) { |
| 438 | if (i > hi) break; |
| 439 | v = si->symtab[i]; |
| 440 | j = i; |
| 441 | while (si->symtab[j-h].addr > v.addr) { |
| 442 | si->symtab[j] = si->symtab[j-h]; |
| 443 | j = j - h; |
| 444 | if (j <= (lo + h - 1)) break; |
| 445 | } |
| 446 | si->symtab[j] = v; |
| 447 | i++; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | cleanup_more: |
| 452 | |
| 453 | /* If two symbols have identical address ranges, favour the |
| 454 | one with the longer name. |
| 455 | */ |
| 456 | do { |
| 457 | n_merged = 0; |
| 458 | j = si->symtab_used; |
| 459 | si->symtab_used = 0; |
| 460 | for (i = 0; i < j; i++) { |
| 461 | if (i < j-1 |
| 462 | && si->symtab[i].addr == si->symtab[i+1].addr |
| 463 | && si->symtab[i].size == si->symtab[i+1].size) { |
| 464 | n_merged++; |
| 465 | /* merge the two into one */ |
| 466 | if (VG_(strlen)(&si->strtab[si->symtab[i].nmoff]) |
| 467 | > VG_(strlen)(&si->strtab[si->symtab[i+1].nmoff])) { |
| 468 | si->symtab[si->symtab_used++] = si->symtab[i]; |
| 469 | } else { |
| 470 | si->symtab[si->symtab_used++] = si->symtab[i+1]; |
| 471 | } |
| 472 | i++; |
| 473 | } else { |
| 474 | si->symtab[si->symtab_used++] = si->symtab[i]; |
| 475 | } |
| 476 | } |
| 477 | if (VG_(clo_trace_symtab)) |
| 478 | VG_(printf)( "%d merged\n", n_merged); |
| 479 | } |
| 480 | while (n_merged > 0); |
| 481 | |
| 482 | /* Detect and "fix" overlapping address ranges. */ |
| 483 | n_truncated = 0; |
| 484 | |
| 485 | for (i = 0; i < si->symtab_used-1; i++) { |
| 486 | |
| 487 | vg_assert(si->symtab[i].addr <= si->symtab[i+1].addr); |
| 488 | |
| 489 | /* Check for common (no overlap) case. */ |
| 490 | if (si->symtab[i].addr + si->symtab[i].size |
| 491 | <= si->symtab[i+1].addr) |
| 492 | continue; |
| 493 | |
| 494 | /* There's an overlap. Truncate one or the other. */ |
| 495 | if (VG_(clo_trace_symtab)) { |
| 496 | VG_(printf)("overlapping address ranges in symbol table\n\t"); |
| 497 | printSym(si,i); |
| 498 | VG_(printf)("\t"); |
| 499 | printSym(si,i+1); |
| 500 | VG_(printf)("\n"); |
| 501 | } |
| 502 | |
| 503 | /* Truncate one or the other. */ |
| 504 | s1 = si->symtab[i].addr; |
| 505 | s2 = si->symtab[i+1].addr; |
| 506 | e1 = s1 + si->symtab[i].size - 1; |
| 507 | e2 = s2 + si->symtab[i+1].size - 1; |
| 508 | if (s1 < s2) { |
| 509 | e1 = s2-1; |
| 510 | } else { |
| 511 | vg_assert(s1 == s2); |
| 512 | if (e1 > e2) { |
| 513 | s1 = e2+1; SWAP(Addr,s1,s2); SWAP(Addr,e1,e2); |
| 514 | } else |
| 515 | if (e1 < e2) { |
| 516 | s2 = e1+1; |
| 517 | } else { |
| 518 | /* e1 == e2. Identical addr ranges. We'll eventually wind |
| 519 | up back at cleanup_more, which will take care of it. */ |
| 520 | } |
| 521 | } |
| 522 | si->symtab[i].addr = s1; |
| 523 | si->symtab[i+1].addr = s2; |
| 524 | si->symtab[i].size = e1 - s1 + 1; |
| 525 | si->symtab[i+1].size = e2 - s2 + 1; |
| 526 | vg_assert(s1 <= s2); |
| 527 | vg_assert(si->symtab[i].size > 0); |
| 528 | vg_assert(si->symtab[i+1].size > 0); |
| 529 | /* It may be that the i+1 entry now needs to be moved further |
| 530 | along to maintain the address order requirement. */ |
| 531 | j = i+1; |
| 532 | while (j < si->symtab_used-1 |
| 533 | && si->symtab[j].addr > si->symtab[j+1].addr) { |
| 534 | SWAP(RiSym,si->symtab[j],si->symtab[j+1]); |
| 535 | j++; |
| 536 | } |
| 537 | n_truncated++; |
| 538 | } |
| 539 | |
| 540 | if (n_truncated > 0) goto cleanup_more; |
| 541 | |
| 542 | /* Ensure relevant postconditions hold. */ |
| 543 | for (i = 0; i < si->symtab_used-1; i++) { |
| 544 | /* No zero-sized symbols. */ |
| 545 | vg_assert(si->symtab[i].size > 0); |
| 546 | /* In order. */ |
| 547 | vg_assert(si->symtab[i].addr < si->symtab[i+1].addr); |
| 548 | /* No overlaps. */ |
| 549 | vg_assert(si->symtab[i].addr + si->symtab[i].size - 1 |
| 550 | < si->symtab[i+1].addr); |
| 551 | } |
| 552 | # undef SWAP |
| 553 | } |
| 554 | |
| 555 | |
| 556 | |
| 557 | /* Sort the location table by starting address. Mash the table around |
| 558 | so as to establish the property that addresses are in order and the |
| 559 | ranges do not overlap. This facilitates using binary search to map |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 560 | addresses to locations when we come to query the table. |
| 561 | */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 562 | static |
| 563 | void canonicaliseLoctab ( SegInfo* si ) |
| 564 | { |
| 565 | /* Magic numbers due to Janet Incerpi and Robert Sedgewick. */ |
| 566 | Int incs[16] = { 1, 3, 7, 21, 48, 112, 336, 861, 1968, |
| 567 | 4592, 13776, 33936, 86961, 198768, |
| 568 | 463792, 1391376 }; |
| 569 | Int lo = 0; |
| 570 | Int hi = si->loctab_used-1; |
| 571 | Int i, j, h, bigN, hp; |
| 572 | RiLoc v; |
| 573 | |
| 574 | # define SWAP(ty,aa,bb) \ |
| 575 | do { ty tt = (aa); (aa) = (bb); (bb) = tt; } while (0); |
| 576 | |
| 577 | /* Sort by start address. */ |
| 578 | |
| 579 | bigN = hi - lo + 1; if (bigN < 2) return; |
| 580 | hp = 0; while (hp < 16 && incs[hp] < bigN) hp++; hp--; |
| 581 | vg_assert(0 <= hp && hp < 16); |
| 582 | |
| 583 | for (; hp >= 0; hp--) { |
| 584 | h = incs[hp]; |
| 585 | i = lo + h; |
| 586 | while (1) { |
| 587 | if (i > hi) break; |
| 588 | v = si->loctab[i]; |
| 589 | j = i; |
| 590 | while (si->loctab[j-h].addr > v.addr) { |
| 591 | si->loctab[j] = si->loctab[j-h]; |
| 592 | j = j - h; |
| 593 | if (j <= (lo + h - 1)) break; |
| 594 | } |
| 595 | si->loctab[j] = v; |
| 596 | i++; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | /* If two adjacent entries overlap, truncate the first. */ |
| 601 | for (i = 0; i < si->loctab_used-1; i++) { |
| 602 | vg_assert(si->loctab[i].size < 10000); |
| 603 | if (si->loctab[i].addr + si->loctab[i].size > si->loctab[i+1].addr) { |
| 604 | /* Do this in signed int32 because the actual .size fields |
| 605 | are unsigned 16s. */ |
| 606 | Int new_size = si->loctab[i+1].addr - si->loctab[i].addr; |
| 607 | if (new_size < 0) { |
| 608 | si->loctab[i].size = 0; |
| 609 | } else |
| 610 | if (new_size >= 65536) { |
| 611 | si->loctab[i].size = 65535; |
| 612 | } else { |
| 613 | si->loctab[i].size = (UShort)new_size; |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /* Zap any zero-sized entries resulting from the truncation |
| 619 | process. */ |
| 620 | j = 0; |
| 621 | for (i = 0; i < si->loctab_used; i++) { |
| 622 | if (si->loctab[i].size > 0) { |
| 623 | si->loctab[j] = si->loctab[i]; |
| 624 | j++; |
| 625 | } |
| 626 | } |
| 627 | si->loctab_used = j; |
| 628 | |
| 629 | /* Ensure relevant postconditions hold. */ |
| 630 | for (i = 0; i < si->loctab_used-1; i++) { |
| 631 | /* |
| 632 | VG_(printf)("%d (%d) %d 0x%x\n", |
| 633 | i, si->loctab[i+1].confident, |
| 634 | si->loctab[i+1].size, si->loctab[i+1].addr ); |
| 635 | */ |
| 636 | /* No zero-sized symbols. */ |
| 637 | vg_assert(si->loctab[i].size > 0); |
| 638 | /* In order. */ |
| 639 | vg_assert(si->loctab[i].addr < si->loctab[i+1].addr); |
| 640 | /* No overlaps. */ |
| 641 | vg_assert(si->loctab[i].addr + si->loctab[i].size - 1 |
| 642 | < si->loctab[i+1].addr); |
| 643 | } |
| 644 | # undef SWAP |
| 645 | } |
| 646 | |
| 647 | |
| 648 | /*------------------------------------------------------------*/ |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 649 | /*--- Read STABS format debug info. ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 650 | /*------------------------------------------------------------*/ |
| 651 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 652 | /* Stabs entry types, from: |
| 653 | * The "stabs" debug format |
| 654 | * Menapace, Kingdon and MacKenzie |
| 655 | * Cygnus Support |
| 656 | */ |
| 657 | typedef enum { N_GSYM = 32, /* Global symbol */ |
| 658 | N_FUN = 36, /* Function start or end */ |
| 659 | N_STSYM = 38, /* Data segment file-scope variable */ |
| 660 | N_LCSYM = 40, /* BSS segment file-scope variable */ |
| 661 | N_RSYM = 64, /* Register variable */ |
| 662 | N_SLINE = 68, /* Source line number */ |
| 663 | N_SO = 100, /* Source file path and name */ |
| 664 | N_LSYM = 128, /* Stack variable or type */ |
| 665 | N_SOL = 132, /* Include file name */ |
| 666 | N_LBRAC = 192, /* Start of lexical block */ |
| 667 | N_RBRAC = 224 /* End of lexical block */ |
| 668 | } stab_types; |
| 669 | |
| 670 | |
| 671 | /* Read stabs-format debug info. This is all rather horrible because |
| 672 | stabs is a underspecified, kludgy hack. |
| 673 | */ |
| 674 | static |
| 675 | void read_debuginfo_stabs ( SegInfo* si, |
| 676 | UChar* stabC, Int stab_sz, |
| 677 | UChar* stabstr, Int stabstr_sz ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 678 | { |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 679 | Int i; |
| 680 | Int curr_filenmoff; |
njn | b79ad34 | 2002-06-05 15:30:30 +0000 | [diff] [blame] | 681 | Addr curr_fn_stabs_addr = (Addr)NULL; |
| 682 | Addr curr_fnbaseaddr = (Addr)NULL; |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 683 | Char *curr_file_name, *curr_fn_name; |
| 684 | Int n_stab_entries; |
njn | b79ad34 | 2002-06-05 15:30:30 +0000 | [diff] [blame] | 685 | Int prev_lineno = 0, lineno = 0; |
| 686 | Int lineno_overflows = 0; |
| 687 | Bool same_file = True; |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 688 | struct nlist* stab = (struct nlist*)stabC; |
njn | b79ad34 | 2002-06-05 15:30:30 +0000 | [diff] [blame] | 689 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 690 | /* Ok. It all looks plausible. Go on and read debug data. |
| 691 | stab kinds: 100 N_SO a source file name |
| 692 | 68 N_SLINE a source line number |
| 693 | 36 N_FUN start of a function |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 694 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 695 | In this loop, we maintain a current file name, updated as |
| 696 | N_SO/N_SOLs appear, and a current function base address, |
| 697 | updated as N_FUNs appear. Based on that, address ranges for |
| 698 | N_SLINEs are calculated, and stuffed into the line info table. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 699 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 700 | Finding the instruction address range covered by an N_SLINE is |
| 701 | complicated; see the N_SLINE case below. |
| 702 | */ |
njn | b79ad34 | 2002-06-05 15:30:30 +0000 | [diff] [blame] | 703 | curr_filenmoff = addStr(si,"???"); |
| 704 | curr_file_name = curr_fn_name = (Char*)NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 705 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 706 | n_stab_entries = stab_sz/(int)sizeof(struct nlist); |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 707 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 708 | for (i = 0; i < n_stab_entries; i++) { |
| 709 | # if 0 |
| 710 | VG_(printf) ( " %2d ", i ); |
| 711 | VG_(printf) ( "type=0x%x othr=%d desc=%d value=0x%x strx=%d %s", |
| 712 | stab[i].n_type, stab[i].n_other, stab[i].n_desc, |
| 713 | (int)stab[i].n_value, |
| 714 | (int)stab[i].n_un.n_strx, |
| 715 | stabstr + stab[i].n_un.n_strx ); |
| 716 | VG_(printf)("\n"); |
| 717 | # endif |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 718 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 719 | Char *no_fn_name = "???"; |
| 720 | |
| 721 | switch (stab[i].n_type) { |
| 722 | UInt next_addr; |
| 723 | |
| 724 | /* Two complicated things here: |
| 725 | * |
| 726 | * 1. the n_desc field in 'struct n_list' in a.out.h is only |
| 727 | * 16-bits, which gives a maximum of 65535 lines. We handle |
| 728 | * files bigger than this by detecting heuristically |
| 729 | * overflows -- if the line count goes from 65000-odd to |
| 730 | * 0-odd within the same file, we assume it's an overflow. |
| 731 | * Once we switch files, we zero the overflow count. |
| 732 | * |
| 733 | * 2. To compute the instr address range covered by a single |
| 734 | * line, find the address of the next thing and compute the |
| 735 | * difference. The approach used depends on what kind of |
| 736 | * entry/entries follow... |
| 737 | */ |
| 738 | case N_SLINE: { |
| 739 | Int this_addr = (UInt)stab[i].n_value; |
| 740 | |
| 741 | /* Although stored as a short, neg values really are > |
| 742 | * 32768, hence the UShort cast. Then we use an Int to |
| 743 | * handle overflows. */ |
| 744 | prev_lineno = lineno; |
| 745 | lineno = (Int)((UShort)stab[i].n_desc); |
| 746 | |
| 747 | if (prev_lineno > lineno + OVERFLOW_DIFFERENCE && same_file) { |
| 748 | VG_(message)(Vg_DebugMsg, |
| 749 | "Line number overflow detected (%d --> %d) in %s", |
| 750 | prev_lineno, lineno, curr_file_name); |
| 751 | lineno_overflows++; |
| 752 | } |
| 753 | same_file = True; |
| 754 | |
| 755 | LOOP: |
| 756 | if (i+1 >= n_stab_entries) { |
| 757 | /* If it's the last entry, just guess the range is |
| 758 | * four; can't do any better */ |
| 759 | next_addr = this_addr + 4; |
| 760 | } else { |
| 761 | switch (stab[i+1].n_type) { |
| 762 | /* Easy, common case: use address of next entry */ |
| 763 | case N_SLINE: case N_SO: |
| 764 | next_addr = (UInt)stab[i+1].n_value; |
| 765 | break; |
| 766 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 767 | /* Boring one: skip, look for something more useful. */ |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 768 | case N_RSYM: case N_LSYM: case N_LBRAC: case N_RBRAC: |
| 769 | case N_STSYM: case N_LCSYM: case N_GSYM: |
| 770 | i++; |
| 771 | goto LOOP; |
| 772 | |
njn | b79ad34 | 2002-06-05 15:30:30 +0000 | [diff] [blame] | 773 | /* If end-of-this-fun entry, use its address. |
| 774 | * If start-of-next-fun entry, find difference between start |
| 775 | * of current function and start of next function to work |
| 776 | * it out. |
| 777 | */ |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 778 | case N_FUN: |
| 779 | if ('\0' == * (stabstr + stab[i+1].n_un.n_strx) ) { |
| 780 | next_addr = (UInt)stab[i+1].n_value; |
| 781 | } else { |
njn | b79ad34 | 2002-06-05 15:30:30 +0000 | [diff] [blame] | 782 | next_addr = |
| 783 | (UInt)stab[i+1].n_value - curr_fn_stabs_addr; |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 784 | } |
| 785 | break; |
| 786 | |
| 787 | /* N_SOL should be followed by an N_SLINE which can |
| 788 | be used */ |
| 789 | case N_SOL: |
| 790 | if (i+2 < n_stab_entries && N_SLINE == stab[i+2].n_type) { |
| 791 | next_addr = (UInt)stab[i+2].n_value; |
| 792 | break; |
| 793 | } else { |
| 794 | VG_(printf)("unhandled N_SOL stabs case: %d %d %d", |
| 795 | stab[i+1].n_type, i, n_stab_entries); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 796 | VG_(core_panic)("unhandled N_SOL stabs case"); |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | default: |
| 800 | VG_(printf)("unhandled (other) stabs case: %d %d", |
| 801 | stab[i+1].n_type,i); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 802 | /* VG_(core_panic)("unhandled (other) stabs case"); */ |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 803 | next_addr = this_addr + 4; |
| 804 | break; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | addLineInfo ( si, curr_filenmoff, curr_fnbaseaddr + this_addr, |
| 809 | curr_fnbaseaddr + next_addr, |
| 810 | lineno + lineno_overflows * LINENO_OVERFLOW, i); |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | case N_FUN: { |
| 815 | if ('\0' != (stabstr + stab[i].n_un.n_strx)[0] ) { |
| 816 | /* N_FUN with a name -- indicates the start of a fn. */ |
njn | b79ad34 | 2002-06-05 15:30:30 +0000 | [diff] [blame] | 817 | curr_fn_stabs_addr = (Addr)stab[i].n_value; |
| 818 | curr_fnbaseaddr = si->offset + curr_fn_stabs_addr; |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 819 | curr_fn_name = stabstr + stab[i].n_un.n_strx; |
| 820 | } else { |
| 821 | curr_fn_name = no_fn_name; |
| 822 | } |
| 823 | break; |
| 824 | } |
| 825 | |
| 826 | case N_SOL: |
| 827 | if (lineno_overflows != 0) { |
| 828 | VG_(message)(Vg_UserMsg, |
| 829 | "Warning: file %s is very big (> 65535 lines) " |
| 830 | "Line numbers and annotation for this file might " |
| 831 | "be wrong. Sorry", |
| 832 | curr_file_name); |
| 833 | } |
| 834 | /* fall through! */ |
| 835 | case N_SO: |
| 836 | lineno_overflows = 0; |
| 837 | |
| 838 | /* seems to give lots of locations in header files */ |
| 839 | /* case 130: */ /* BINCL */ |
| 840 | { |
| 841 | UChar* nm = stabstr + stab[i].n_un.n_strx; |
| 842 | UInt len = VG_(strlen)(nm); |
| 843 | |
| 844 | if (len > 0 && nm[len-1] != '/') { |
| 845 | curr_filenmoff = addStr ( si, nm ); |
| 846 | curr_file_name = stabstr + stab[i].n_un.n_strx; |
| 847 | } |
| 848 | else |
| 849 | if (len == 0) |
| 850 | curr_filenmoff = addStr ( si, "?1\0" ); |
| 851 | |
| 852 | break; |
| 853 | } |
| 854 | |
| 855 | # if 0 |
| 856 | case 162: /* EINCL */ |
| 857 | curr_filenmoff = addStr ( si, "?2\0" ); |
| 858 | break; |
| 859 | # endif |
| 860 | |
| 861 | default: |
| 862 | break; |
| 863 | } |
| 864 | } /* for (i = 0; i < stab_sz/(int)sizeof(struct nlist); i++) */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 868 | /*------------------------------------------------------------*/ |
| 869 | /*--- Read DWARF2 format debug info. ---*/ |
| 870 | /*------------------------------------------------------------*/ |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 871 | |
| 872 | /* Structure found in the .debug_line section. */ |
| 873 | typedef struct |
| 874 | { |
| 875 | UChar li_length [4]; |
| 876 | UChar li_version [2]; |
| 877 | UChar li_prologue_length [4]; |
| 878 | UChar li_min_insn_length [1]; |
| 879 | UChar li_default_is_stmt [1]; |
| 880 | UChar li_line_base [1]; |
| 881 | UChar li_line_range [1]; |
| 882 | UChar li_opcode_base [1]; |
| 883 | } |
| 884 | DWARF2_External_LineInfo; |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 885 | |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 886 | typedef struct |
| 887 | { |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 888 | UInt li_length; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 889 | UShort li_version; |
| 890 | UInt li_prologue_length; |
| 891 | UChar li_min_insn_length; |
| 892 | UChar li_default_is_stmt; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 893 | Int li_line_base; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 894 | UChar li_line_range; |
| 895 | UChar li_opcode_base; |
| 896 | } |
| 897 | DWARF2_Internal_LineInfo; |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 898 | |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 899 | /* Line number opcodes. */ |
| 900 | enum dwarf_line_number_ops |
| 901 | { |
| 902 | DW_LNS_extended_op = 0, |
| 903 | DW_LNS_copy = 1, |
| 904 | DW_LNS_advance_pc = 2, |
| 905 | DW_LNS_advance_line = 3, |
| 906 | DW_LNS_set_file = 4, |
| 907 | DW_LNS_set_column = 5, |
| 908 | DW_LNS_negate_stmt = 6, |
| 909 | DW_LNS_set_basic_block = 7, |
| 910 | DW_LNS_const_add_pc = 8, |
| 911 | DW_LNS_fixed_advance_pc = 9, |
| 912 | /* DWARF 3. */ |
| 913 | DW_LNS_set_prologue_end = 10, |
| 914 | DW_LNS_set_epilogue_begin = 11, |
| 915 | DW_LNS_set_isa = 12 |
| 916 | }; |
| 917 | |
| 918 | /* Line number extended opcodes. */ |
| 919 | enum dwarf_line_number_x_ops |
| 920 | { |
| 921 | DW_LNE_end_sequence = 1, |
| 922 | DW_LNE_set_address = 2, |
| 923 | DW_LNE_define_file = 3 |
| 924 | }; |
| 925 | |
| 926 | typedef struct State_Machine_Registers |
| 927 | { |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 928 | Addr address; |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 929 | /* Holds the address of the last statement boundary. |
| 930 | * We use it to calculate statement lengths. Without it, |
| 931 | * we would need to search backwards for last statement begin |
| 932 | * each time we are emitting a statement with addLineInfo */ |
| 933 | Addr last_address; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 934 | UInt file; |
| 935 | UInt line; |
| 936 | UInt column; |
| 937 | Int is_stmt; |
| 938 | Int basic_block; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 939 | Int end_sequence; |
| 940 | /* This variable hold the number of the last entry seen |
| 941 | in the File Table. */ |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 942 | UInt last_file_entry; |
| 943 | } SMR; |
| 944 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 945 | |
| 946 | static |
| 947 | UInt read_leb128 ( UChar* data, Int* length_return, Int sign ) |
| 948 | { |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 949 | UInt result = 0; |
| 950 | UInt num_read = 0; |
| 951 | Int shift = 0; |
| 952 | UChar byte; |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 953 | |
| 954 | do |
| 955 | { |
| 956 | byte = * data ++; |
| 957 | num_read ++; |
| 958 | |
| 959 | result |= (byte & 0x7f) << shift; |
| 960 | |
| 961 | shift += 7; |
| 962 | |
| 963 | } |
| 964 | while (byte & 0x80); |
| 965 | |
| 966 | if (length_return != NULL) |
| 967 | * length_return = num_read; |
| 968 | |
| 969 | if (sign && (shift < 32) && (byte & 0x40)) |
| 970 | result |= -1 << shift; |
| 971 | |
| 972 | return result; |
| 973 | } |
| 974 | |
| 975 | |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 976 | static SMR state_machine_regs; |
| 977 | |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 978 | static |
| 979 | void reset_state_machine ( Int is_stmt ) |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 980 | { |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 981 | if (0) VG_(printf)("smr.a := %p (reset)\n", 0 ); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 982 | state_machine_regs.address = 0; |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 983 | state_machine_regs.last_address = 0; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 984 | state_machine_regs.file = 1; |
| 985 | state_machine_regs.line = 1; |
| 986 | state_machine_regs.column = 0; |
| 987 | state_machine_regs.is_stmt = is_stmt; |
| 988 | state_machine_regs.basic_block = 0; |
| 989 | state_machine_regs.end_sequence = 0; |
| 990 | state_machine_regs.last_file_entry = 0; |
| 991 | } |
| 992 | |
| 993 | /* Handled an extend line op. Returns true if this is the end |
| 994 | of sequence. */ |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 995 | static |
| 996 | int process_extended_line_op( SegInfo *si, UInt** fnames, |
| 997 | UChar* data, Int is_stmt, Int pointer_size) |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 998 | { |
| 999 | UChar op_code; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1000 | Int bytes_read; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1001 | UInt len; |
| 1002 | UChar * name; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1003 | Addr adr; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1004 | |
| 1005 | len = read_leb128 (data, & bytes_read, 0); |
| 1006 | data += bytes_read; |
| 1007 | |
| 1008 | if (len == 0) |
| 1009 | { |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1010 | VG_(message)(Vg_UserMsg, |
| 1011 | "badly formed extended line op encountered!\n"); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1012 | return bytes_read; |
| 1013 | } |
| 1014 | |
| 1015 | len += bytes_read; |
| 1016 | op_code = * data ++; |
| 1017 | |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 1018 | if (0) VG_(printf)("dwarf2: ext OPC: %d\n", op_code); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1019 | |
| 1020 | switch (op_code) |
| 1021 | { |
| 1022 | case DW_LNE_end_sequence: |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1023 | if (0) VG_(printf)("1001: si->o %p, smr.a %p\n", |
| 1024 | si->offset, state_machine_regs.address ); |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 1025 | state_machine_regs.end_sequence = 1; /* JRS: added for compliance |
| 1026 | with spec; is pointless due to reset_state_machine below |
| 1027 | */ |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 1028 | if (state_machine_regs.is_stmt) { |
| 1029 | if (state_machine_regs.last_address) |
| 1030 | addLineInfo (si, (*fnames)[state_machine_regs.file], |
| 1031 | si->offset + state_machine_regs.last_address, |
| 1032 | si->offset + state_machine_regs.address, |
| 1033 | state_machine_regs.line, 0); |
| 1034 | } |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1035 | reset_state_machine (is_stmt); |
| 1036 | break; |
| 1037 | |
| 1038 | case DW_LNE_set_address: |
| 1039 | /* XXX: Pointer size could be 8 */ |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1040 | vg_assert(pointer_size == 4); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1041 | adr = *((Addr *)data); |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1042 | if (0) VG_(printf)("smr.a := %p\n", adr ); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1043 | state_machine_regs.address = adr; |
| 1044 | break; |
| 1045 | |
| 1046 | case DW_LNE_define_file: |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1047 | ++ state_machine_regs.last_file_entry; |
| 1048 | name = data; |
| 1049 | if (*fnames == NULL) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1050 | *fnames = VG_(arena_malloc)(VG_AR_SYMTAB, sizeof (UInt) * 2); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1051 | else |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1052 | *fnames = VG_(arena_realloc)( |
| 1053 | VG_AR_SYMTAB, *fnames, /*alignment*/4, |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1054 | sizeof(UInt) |
| 1055 | * (state_machine_regs.last_file_entry + 1)); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1056 | (*fnames)[state_machine_regs.last_file_entry] = addStr (si,name); |
| 1057 | data += VG_(strlen) ((char *) data) + 1; |
| 1058 | read_leb128 (data, & bytes_read, 0); |
| 1059 | data += bytes_read; |
| 1060 | read_leb128 (data, & bytes_read, 0); |
| 1061 | data += bytes_read; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1062 | read_leb128 (data, & bytes_read, 0); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1063 | break; |
| 1064 | |
| 1065 | default: |
| 1066 | break; |
| 1067 | } |
| 1068 | |
| 1069 | return len; |
| 1070 | } |
| 1071 | |
| 1072 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1073 | static |
| 1074 | void read_debuginfo_dwarf2 ( SegInfo* si, UChar* dwarf2, Int dwarf2_sz ) |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1075 | { |
| 1076 | DWARF2_External_LineInfo * external; |
| 1077 | DWARF2_Internal_LineInfo info; |
| 1078 | UChar * standard_opcodes; |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1079 | UChar * data = dwarf2; |
| 1080 | UChar * end = dwarf2 + dwarf2_sz; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1081 | UChar * end_of_sequence; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1082 | UInt * fnames = NULL; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1083 | |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 1084 | /* Fails due to gcc padding ... |
| 1085 | vg_assert(sizeof(DWARF2_External_LineInfo) |
| 1086 | == sizeof(DWARF2_Internal_LineInfo)); |
| 1087 | */ |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1088 | |
| 1089 | while (data < end) |
| 1090 | { |
| 1091 | external = (DWARF2_External_LineInfo *) data; |
| 1092 | |
| 1093 | /* Check the length of the block. */ |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1094 | info.li_length = * ((UInt *)(external->li_length)); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1095 | |
| 1096 | if (info.li_length == 0xffffffff) |
| 1097 | { |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1098 | vg_symerr("64-bit DWARF line info is not supported yet."); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1099 | break; |
| 1100 | } |
| 1101 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1102 | if (info.li_length + sizeof (external->li_length) > dwarf2_sz) |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1103 | { |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1104 | vg_symerr("DWARF line info appears to be corrupt " |
| 1105 | "- the section is too small"); |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1106 | return; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /* Check its version number. */ |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1110 | info.li_version = * ((UShort *) (external->li_version)); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1111 | if (info.li_version != 2) |
| 1112 | { |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1113 | vg_symerr("Only DWARF version 2 line info " |
| 1114 | "is currently supported."); |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1115 | return; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 1118 | info.li_prologue_length = * ((UInt *) (external->li_prologue_length)); |
| 1119 | info.li_min_insn_length = * ((UChar *)(external->li_min_insn_length)); |
sewardj | 8fe2c1f | 2002-10-22 05:41:14 +0000 | [diff] [blame] | 1120 | |
| 1121 | info.li_default_is_stmt = True; |
| 1122 | /* WAS: = * ((UChar *)(external->li_default_is_stmt)); */ |
| 1123 | /* Josef Weidendorfer (20021021) writes: |
| 1124 | |
| 1125 | It seems to me that the Intel Fortran compiler generates |
| 1126 | bad DWARF2 line info code: It sets "is_stmt" of the state |
| 1127 | machine in the the line info reader to be always |
| 1128 | false. Thus, there is never a statement boundary generated |
| 1129 | and therefore never a instruction range/line number |
| 1130 | mapping generated for valgrind. |
| 1131 | |
| 1132 | Please have a look at the DWARF2 specification, Ch. 6.2 |
| 1133 | (x86.ddj.com/ftp/manuals/tools/dwarf.pdf). Perhaps I |
| 1134 | understand this wrong, but I don't think so. |
| 1135 | |
| 1136 | I just had a look at the GDB DWARF2 reader... They |
| 1137 | completly ignore "is_stmt" when recording line info ;-) |
| 1138 | That's the reason "objdump -S" works on files from the the |
| 1139 | intel fortran compiler. |
| 1140 | */ |
| 1141 | |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 1142 | |
| 1143 | /* JRS: changed (UInt*) to (UChar*) */ |
| 1144 | info.li_line_base = * ((UChar *)(external->li_line_base)); |
| 1145 | |
| 1146 | info.li_line_range = * ((UChar *)(external->li_line_range)); |
| 1147 | info.li_opcode_base = * ((UChar *)(external->li_opcode_base)); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1148 | |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 1149 | if (0) VG_(printf)("dwarf2: line base: %d, range %d, opc base: %d\n", |
| 1150 | info.li_line_base, info.li_line_range, info.li_opcode_base); |
| 1151 | |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1152 | /* Sign extend the line base field. */ |
| 1153 | info.li_line_base <<= 24; |
| 1154 | info.li_line_base >>= 24; |
| 1155 | |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1156 | end_of_sequence = data + info.li_length |
| 1157 | + sizeof (external->li_length); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1158 | |
| 1159 | reset_state_machine (info.li_default_is_stmt); |
| 1160 | |
| 1161 | /* Read the contents of the Opcodes table. */ |
| 1162 | standard_opcodes = data + sizeof (* external); |
| 1163 | |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1164 | /* Read the contents of the Directory table. */ |
| 1165 | data = standard_opcodes + info.li_opcode_base - 1; |
| 1166 | |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1167 | if (* data == 0) |
| 1168 | { |
| 1169 | } |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1170 | else |
| 1171 | { |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1172 | /* We ignore the directory table, since gcc gives the entire |
| 1173 | path as part of the filename */ |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1174 | while (* data != 0) |
| 1175 | { |
| 1176 | data += VG_(strlen) ((char *) data) + 1; |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | /* Skip the NUL at the end of the table. */ |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 1181 | if (*data != 0) { |
| 1182 | vg_symerr("can't find NUL at end of DWARF2 directory table"); |
| 1183 | return; |
| 1184 | } |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1185 | data ++; |
| 1186 | |
| 1187 | /* Read the contents of the File Name table. */ |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1188 | if (* data == 0) |
| 1189 | { |
| 1190 | } |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1191 | else |
| 1192 | { |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1193 | while (* data != 0) |
| 1194 | { |
| 1195 | UChar * name; |
| 1196 | Int bytes_read; |
| 1197 | |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1198 | ++ state_machine_regs.last_file_entry; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1199 | name = data; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1200 | /* Since we don't have realloc (0, ....) == malloc (...) |
| 1201 | semantics, we need to malloc the first time. */ |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1202 | |
| 1203 | if (fnames == NULL) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1204 | fnames = VG_(arena_malloc)(VG_AR_SYMTAB, sizeof (UInt) * 2); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1205 | else |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1206 | fnames = VG_(arena_realloc)(VG_AR_SYMTAB, fnames, /*alignment*/4, |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1207 | sizeof(UInt) |
| 1208 | * (state_machine_regs.last_file_entry + 1)); |
| 1209 | data += VG_(strlen) ((Char *) data) + 1; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1210 | fnames[state_machine_regs.last_file_entry] = addStr (si,name); |
| 1211 | |
| 1212 | read_leb128 (data, & bytes_read, 0); |
| 1213 | data += bytes_read; |
| 1214 | read_leb128 (data, & bytes_read, 0); |
| 1215 | data += bytes_read; |
| 1216 | read_leb128 (data, & bytes_read, 0); |
| 1217 | data += bytes_read; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | /* Skip the NUL at the end of the table. */ |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 1222 | if (*data != 0) { |
| 1223 | vg_symerr("can't find NUL at end of DWARF2 file name table"); |
| 1224 | return; |
| 1225 | } |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1226 | data ++; |
| 1227 | |
| 1228 | /* Now display the statements. */ |
| 1229 | |
| 1230 | while (data < end_of_sequence) |
| 1231 | { |
| 1232 | UChar op_code; |
| 1233 | Int adv; |
| 1234 | Int bytes_read; |
| 1235 | |
| 1236 | op_code = * data ++; |
| 1237 | |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 1238 | if (0) VG_(printf)("dwarf2: OPC: %d\n", op_code); |
| 1239 | |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1240 | if (op_code >= info.li_opcode_base) |
| 1241 | { |
| 1242 | Int advAddr; |
| 1243 | op_code -= info.li_opcode_base; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1244 | adv = (op_code / info.li_line_range) |
| 1245 | * info.li_min_insn_length; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1246 | advAddr = adv; |
| 1247 | state_machine_regs.address += adv; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1248 | if (0) VG_(printf)("smr.a += %p\n", adv ); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1249 | adv = (op_code % info.li_line_range) + info.li_line_base; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1250 | if (0) VG_(printf)("1002: si->o %p, smr.a %p\n", |
| 1251 | si->offset, state_machine_regs.address ); |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 1252 | if (state_machine_regs.is_stmt) { |
| 1253 | /* only add a statement if there was a previous boundary */ |
| 1254 | if (state_machine_regs.last_address) |
| 1255 | addLineInfo (si, fnames[state_machine_regs.file], |
| 1256 | si->offset + state_machine_regs.last_address, |
| 1257 | si->offset + state_machine_regs.address, |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1258 | state_machine_regs.line, 0); |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 1259 | state_machine_regs.last_address = state_machine_regs.address; |
| 1260 | } |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1261 | state_machine_regs.line += adv; |
| 1262 | } |
| 1263 | else switch (op_code) |
| 1264 | { |
| 1265 | case DW_LNS_extended_op: |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1266 | data += process_extended_line_op ( |
| 1267 | si, &fnames, data, |
| 1268 | info.li_default_is_stmt, sizeof (Addr)); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1269 | break; |
| 1270 | |
| 1271 | case DW_LNS_copy: |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1272 | if (0) VG_(printf)("1002: si->o %p, smr.a %p\n", |
| 1273 | si->offset, state_machine_regs.address ); |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 1274 | if (state_machine_regs.is_stmt) { |
| 1275 | /* only add a statement if there was a previous boundary */ |
| 1276 | if (state_machine_regs.last_address) |
| 1277 | addLineInfo (si, fnames[state_machine_regs.file], |
| 1278 | si->offset + state_machine_regs.last_address, |
| 1279 | si->offset + state_machine_regs.address, |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1280 | state_machine_regs.line , 0); |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 1281 | state_machine_regs.last_address = state_machine_regs.address; |
| 1282 | } |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 1283 | state_machine_regs.basic_block = 0; /* JRS added */ |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1284 | break; |
| 1285 | |
| 1286 | case DW_LNS_advance_pc: |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1287 | adv = info.li_min_insn_length |
| 1288 | * read_leb128 (data, & bytes_read, 0); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1289 | data += bytes_read; |
| 1290 | state_machine_regs.address += adv; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1291 | if (0) VG_(printf)("smr.a += %p\n", adv ); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1292 | break; |
| 1293 | |
| 1294 | case DW_LNS_advance_line: |
| 1295 | adv = read_leb128 (data, & bytes_read, 1); |
| 1296 | data += bytes_read; |
| 1297 | state_machine_regs.line += adv; |
| 1298 | break; |
| 1299 | |
| 1300 | case DW_LNS_set_file: |
| 1301 | adv = read_leb128 (data, & bytes_read, 0); |
| 1302 | data += bytes_read; |
| 1303 | state_machine_regs.file = adv; |
| 1304 | break; |
| 1305 | |
| 1306 | case DW_LNS_set_column: |
| 1307 | adv = read_leb128 (data, & bytes_read, 0); |
| 1308 | data += bytes_read; |
| 1309 | state_machine_regs.column = adv; |
| 1310 | break; |
| 1311 | |
| 1312 | case DW_LNS_negate_stmt: |
| 1313 | adv = state_machine_regs.is_stmt; |
| 1314 | adv = ! adv; |
| 1315 | state_machine_regs.is_stmt = adv; |
| 1316 | break; |
| 1317 | |
| 1318 | case DW_LNS_set_basic_block: |
| 1319 | state_machine_regs.basic_block = 1; |
| 1320 | break; |
| 1321 | |
| 1322 | case DW_LNS_const_add_pc: |
| 1323 | adv = (((255 - info.li_opcode_base) / info.li_line_range) |
| 1324 | * info.li_min_insn_length); |
| 1325 | state_machine_regs.address += adv; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1326 | if (0) VG_(printf)("smr.a += %p\n", adv ); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1327 | break; |
| 1328 | |
| 1329 | case DW_LNS_fixed_advance_pc: |
| 1330 | /* XXX: Need something to get 2 bytes */ |
| 1331 | adv = *((UShort *)data); |
| 1332 | data += 2; |
| 1333 | state_machine_regs.address += adv; |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 1334 | if (0) VG_(printf)("smr.a += %p\n", adv ); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1335 | break; |
| 1336 | |
| 1337 | case DW_LNS_set_prologue_end: |
| 1338 | break; |
| 1339 | |
| 1340 | case DW_LNS_set_epilogue_begin: |
| 1341 | break; |
| 1342 | |
| 1343 | case DW_LNS_set_isa: |
| 1344 | adv = read_leb128 (data, & bytes_read, 0); |
| 1345 | data += bytes_read; |
| 1346 | break; |
| 1347 | |
| 1348 | default: |
| 1349 | { |
| 1350 | int j; |
| 1351 | for (j = standard_opcodes[op_code - 1]; j > 0 ; --j) |
| 1352 | { |
| 1353 | read_leb128 (data, &bytes_read, 0); |
| 1354 | data += bytes_read; |
| 1355 | } |
| 1356 | } |
| 1357 | break; |
| 1358 | } |
| 1359 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1360 | VG_(arena_free)(VG_AR_SYMTAB, fnames); |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1361 | fnames = NULL; |
| 1362 | } |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1365 | |
| 1366 | /*------------------------------------------------------------*/ |
| 1367 | /*--- Read info from a .so/exe file. ---*/ |
| 1368 | /*------------------------------------------------------------*/ |
| 1369 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1370 | /* Read the symbols from the object/exe specified by the SegInfo into |
| 1371 | the tables within the supplied SegInfo. */ |
| 1372 | static |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1373 | Bool vg_read_lib_symbols ( SegInfo* si ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1374 | { |
| 1375 | Elf32_Ehdr* ehdr; /* The ELF header */ |
| 1376 | Elf32_Shdr* shdr; /* The section table */ |
| 1377 | UChar* sh_strtab; /* The section table's string table */ |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1378 | UChar* stab; /* The .stab table */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1379 | UChar* stabstr; /* The .stab string table */ |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1380 | UChar* dwarf2; /* The DWARF2 location info table */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1381 | Int stab_sz; /* Size in bytes of the .stab table */ |
| 1382 | Int stabstr_sz; /* Size in bytes of the .stab string table */ |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1383 | Int dwarf2_sz; /* Size in bytes of the DWARF2 srcloc table*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1384 | Int fd; |
| 1385 | Int i; |
| 1386 | Bool ok; |
| 1387 | Addr oimage; |
| 1388 | Int n_oimage; |
sewardj | b358620 | 2002-05-09 17:38:13 +0000 | [diff] [blame] | 1389 | struct vki_stat stat_buf; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1390 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1391 | oimage = (Addr)NULL; |
| 1392 | if (VG_(clo_verbosity) > 1) |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1393 | VG_(message)(Vg_UserMsg, "Reading syms from %s", si->filename ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1394 | |
| 1395 | /* mmap the object image aboard, so that we can read symbols and |
| 1396 | line number info out of it. It will be munmapped immediately |
| 1397 | thereafter; it is only aboard transiently. */ |
| 1398 | |
sewardj | b358620 | 2002-05-09 17:38:13 +0000 | [diff] [blame] | 1399 | i = VG_(stat)(si->filename, &stat_buf); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1400 | if (i != 0) { |
| 1401 | vg_symerr("Can't stat .so/.exe (to determine its size)?!"); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1402 | return False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1403 | } |
| 1404 | n_oimage = stat_buf.st_size; |
| 1405 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1406 | fd = VG_(open)(si->filename, VKI_O_RDONLY, 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1407 | if (fd == -1) { |
| 1408 | vg_symerr("Can't open .so/.exe to read symbols?!"); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1409 | return False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
sewardj | b358620 | 2002-05-09 17:38:13 +0000 | [diff] [blame] | 1412 | oimage = (Addr)VG_(mmap)( NULL, n_oimage, |
| 1413 | VKI_PROT_READ, VKI_MAP_PRIVATE, fd, 0 ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1414 | if (oimage == ((Addr)(-1))) { |
| 1415 | VG_(message)(Vg_UserMsg, |
| 1416 | "mmap failed on %s", si->filename ); |
| 1417 | VG_(close)(fd); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1418 | return False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | VG_(close)(fd); |
| 1422 | |
| 1423 | /* Ok, the object image is safely in oimage[0 .. n_oimage-1]. |
| 1424 | Now verify that it is a valid ELF .so or executable image. |
| 1425 | */ |
| 1426 | ok = (n_oimage >= sizeof(Elf32_Ehdr)); |
| 1427 | ehdr = (Elf32_Ehdr*)oimage; |
| 1428 | |
| 1429 | if (ok) { |
| 1430 | ok &= (ehdr->e_ident[EI_MAG0] == 0x7F |
| 1431 | && ehdr->e_ident[EI_MAG1] == 'E' |
| 1432 | && ehdr->e_ident[EI_MAG2] == 'L' |
| 1433 | && ehdr->e_ident[EI_MAG3] == 'F'); |
| 1434 | ok &= (ehdr->e_ident[EI_CLASS] == ELFCLASS32 |
| 1435 | && ehdr->e_ident[EI_DATA] == ELFDATA2LSB |
| 1436 | && ehdr->e_ident[EI_VERSION] == EV_CURRENT); |
| 1437 | ok &= (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN); |
| 1438 | ok &= (ehdr->e_machine == EM_386); |
| 1439 | ok &= (ehdr->e_version == EV_CURRENT); |
| 1440 | ok &= (ehdr->e_shstrndx != SHN_UNDEF); |
| 1441 | ok &= (ehdr->e_shoff != 0 && ehdr->e_shnum != 0); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1442 | ok &= (ehdr->e_phoff != 0 && ehdr->e_phnum != 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | if (!ok) { |
| 1446 | vg_symerr("Invalid ELF header, or missing stringtab/sectiontab."); |
| 1447 | VG_(munmap) ( (void*)oimage, n_oimage ); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1448 | return False; |
| 1449 | } |
| 1450 | |
| 1451 | /* Walk the LOAD headers in the phdr and update the SegInfo to |
| 1452 | include them all, so that this segment also contains data and |
| 1453 | bss memory. Also computes correct symbol offset value for this |
| 1454 | ELF file. */ |
| 1455 | if (ehdr->e_phoff + ehdr->e_phnum*sizeof(Elf32_Phdr) > n_oimage) { |
| 1456 | vg_symerr("ELF program header is beyond image end?!"); |
| 1457 | VG_(munmap) ( (void*)oimage, n_oimage ); |
| 1458 | return False; |
| 1459 | } |
| 1460 | { |
| 1461 | Bool offset_set = False; |
| 1462 | Elf32_Addr prev_addr = 0; |
| 1463 | |
| 1464 | si->offset = 0; |
| 1465 | |
| 1466 | for(i = 0; i < ehdr->e_phnum; i++) { |
| 1467 | Elf32_Phdr *o_phdr; |
| 1468 | Elf32_Addr mapped, mapped_end; |
| 1469 | |
| 1470 | o_phdr = &((Elf32_Phdr *)(oimage + ehdr->e_phoff))[i]; |
| 1471 | |
| 1472 | if (o_phdr->p_type != PT_LOAD) |
| 1473 | continue; |
| 1474 | |
| 1475 | if (!offset_set) { |
| 1476 | offset_set = True; |
| 1477 | si->offset = si->start - o_phdr->p_vaddr; |
| 1478 | } |
| 1479 | |
| 1480 | if (o_phdr->p_vaddr < prev_addr) { |
| 1481 | vg_symerr("ELF Phdrs are out of order!?"); |
| 1482 | VG_(munmap) ( (void*)oimage, n_oimage ); |
| 1483 | return False; |
| 1484 | } |
| 1485 | prev_addr = o_phdr->p_vaddr; |
| 1486 | |
| 1487 | mapped = o_phdr->p_vaddr + si->offset; |
| 1488 | mapped_end = mapped + o_phdr->p_memsz; |
| 1489 | |
| 1490 | if (si->data_start == 0 && |
| 1491 | (o_phdr->p_flags & (PF_R|PF_W|PF_X)) == (PF_R|PF_W)) { |
| 1492 | si->data_start = mapped; |
| 1493 | si->data_size = o_phdr->p_filesz; |
| 1494 | si->bss_start = mapped + o_phdr->p_filesz; |
| 1495 | if (o_phdr->p_memsz > o_phdr->p_filesz) |
| 1496 | si->bss_size = o_phdr->p_memsz - o_phdr->p_filesz; |
| 1497 | else |
| 1498 | si->bss_size = 0; |
| 1499 | } |
| 1500 | |
| 1501 | mapped = mapped & ~(VKI_BYTES_PER_PAGE-1); |
| 1502 | mapped_end = (mapped_end + VKI_BYTES_PER_PAGE - 1) & ~(VKI_BYTES_PER_PAGE-1); |
| 1503 | |
| 1504 | if (VG_(needs).data_syms && |
| 1505 | (mapped >= si->start && mapped <= (si->start+si->size)) && |
| 1506 | (mapped_end > (si->start+si->size))) { |
| 1507 | UInt newsz = mapped_end - si->start; |
| 1508 | if (newsz > si->size) { |
| 1509 | if (0) |
| 1510 | VG_(printf)("extending mapping %p..%p %d -> ..%p %d\n", |
| 1511 | si->start, si->start+si->size, si->size, |
| 1512 | si->start+newsz, newsz); |
| 1513 | si->size = newsz; |
| 1514 | } |
| 1515 | } |
| 1516 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | if (VG_(clo_trace_symtab)) |
| 1520 | VG_(printf)( |
| 1521 | "shoff = %d, shnum = %d, size = %d, n_vg_oimage = %d\n", |
| 1522 | ehdr->e_shoff, ehdr->e_shnum, sizeof(Elf32_Shdr), n_oimage ); |
| 1523 | |
| 1524 | if (ehdr->e_shoff + ehdr->e_shnum*sizeof(Elf32_Shdr) > n_oimage) { |
| 1525 | vg_symerr("ELF section header is beyond image end?!"); |
| 1526 | VG_(munmap) ( (void*)oimage, n_oimage ); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1527 | return False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | shdr = (Elf32_Shdr*)(oimage + ehdr->e_shoff); |
| 1531 | sh_strtab = (UChar*)(oimage + shdr[ehdr->e_shstrndx].sh_offset); |
| 1532 | |
| 1533 | /* try and read the object's symbol table */ |
| 1534 | { |
| 1535 | UChar* o_strtab = NULL; |
| 1536 | Elf32_Sym* o_symtab = NULL; |
| 1537 | UInt o_strtab_sz = 0; |
| 1538 | UInt o_symtab_sz = 0; |
| 1539 | |
| 1540 | UChar* o_got = NULL; |
| 1541 | UChar* o_plt = NULL; |
| 1542 | UInt o_got_sz = 0; |
| 1543 | UInt o_plt_sz = 0; |
| 1544 | |
| 1545 | Bool snaffle_it; |
| 1546 | Addr sym_addr; |
| 1547 | |
| 1548 | /* find the .stabstr and .stab sections */ |
| 1549 | for (i = 0; i < ehdr->e_shnum; i++) { |
sewardj | 4811515 | 2002-10-02 10:20:30 +0000 | [diff] [blame] | 1550 | |
| 1551 | /* As a fallback position, we look first for the dynamic |
| 1552 | symbols of a library to increase the chances that we can |
| 1553 | say something helpful even if the standard and debug |
| 1554 | symbols are missing. */ |
| 1555 | |
| 1556 | if (0 == VG_(strcmp)(".dynsym",sh_strtab + shdr[i].sh_name)) { |
| 1557 | o_symtab = (Elf32_Sym*)(oimage + shdr[i].sh_offset); |
| 1558 | o_symtab_sz = shdr[i].sh_size; |
| 1559 | vg_assert((o_symtab_sz % sizeof(Elf32_Sym)) == 0); |
| 1560 | /* check image overrun here */ |
| 1561 | } |
| 1562 | if (0 == VG_(strcmp)(".dynstr",sh_strtab + shdr[i].sh_name)) { |
| 1563 | o_strtab = (UChar*)(oimage + shdr[i].sh_offset); |
| 1564 | o_strtab_sz = shdr[i].sh_size; |
| 1565 | /* check image overrun here */ |
| 1566 | } |
| 1567 | |
| 1568 | /* now look for the main symbol and string tables. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1569 | if (0 == VG_(strcmp)(".symtab",sh_strtab + shdr[i].sh_name)) { |
| 1570 | o_symtab = (Elf32_Sym*)(oimage + shdr[i].sh_offset); |
| 1571 | o_symtab_sz = shdr[i].sh_size; |
| 1572 | vg_assert((o_symtab_sz % sizeof(Elf32_Sym)) == 0); |
| 1573 | /* check image overrun here */ |
| 1574 | } |
| 1575 | if (0 == VG_(strcmp)(".strtab",sh_strtab + shdr[i].sh_name)) { |
| 1576 | o_strtab = (UChar*)(oimage + shdr[i].sh_offset); |
| 1577 | o_strtab_sz = shdr[i].sh_size; |
| 1578 | /* check image overrun here */ |
| 1579 | } |
| 1580 | |
| 1581 | /* find out where the .got and .plt sections will be in the |
| 1582 | executable image, not in the object image transiently loaded. |
| 1583 | */ |
| 1584 | if (0 == VG_(strcmp)(".got",sh_strtab + shdr[i].sh_name)) { |
| 1585 | o_got = (UChar*)(si->offset |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 1586 | + shdr[i].sh_addr); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1587 | o_got_sz = shdr[i].sh_size; |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 1588 | si->got_start= (Addr)o_got; |
| 1589 | si->got_size = o_got_sz; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1590 | /* check image overrun here */ |
| 1591 | } |
| 1592 | if (0 == VG_(strcmp)(".plt",sh_strtab + shdr[i].sh_name)) { |
| 1593 | o_plt = (UChar*)(si->offset |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 1594 | + shdr[i].sh_addr); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1595 | o_plt_sz = shdr[i].sh_size; |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 1596 | si->plt_start= (Addr)o_plt; |
| 1597 | si->plt_size = o_plt_sz; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1598 | /* check image overrun here */ |
| 1599 | } |
| 1600 | |
| 1601 | } |
| 1602 | |
| 1603 | if (VG_(clo_trace_symtab)) { |
| 1604 | if (o_plt) VG_(printf)( "PLT: %p .. %p\n", |
| 1605 | o_plt, o_plt + o_plt_sz - 1 ); |
| 1606 | if (o_got) VG_(printf)( "GOT: %p .. %p\n", |
| 1607 | o_got, o_got + o_got_sz - 1 ); |
| 1608 | } |
| 1609 | |
| 1610 | if (o_strtab == NULL || o_symtab == NULL) { |
| 1611 | vg_symerr(" object doesn't have a symbol table"); |
| 1612 | } else { |
| 1613 | /* Perhaps should start at i = 1; ELF docs suggest that entry |
| 1614 | 0 always denotes `unknown symbol'. */ |
| 1615 | for (i = 1; i < o_symtab_sz/sizeof(Elf32_Sym); i++){ |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1616 | # if 1 |
| 1617 | if (VG_(clo_trace_symtab)) { |
| 1618 | VG_(printf)("raw symbol: "); |
| 1619 | switch (ELF32_ST_BIND(o_symtab[i].st_info)) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1620 | case STB_LOCAL: VG_(printf)("LOC "); break; |
| 1621 | case STB_GLOBAL: VG_(printf)("GLO "); break; |
| 1622 | case STB_WEAK: VG_(printf)("WEA "); break; |
| 1623 | case STB_LOPROC: VG_(printf)("lop "); break; |
| 1624 | case STB_HIPROC: VG_(printf)("hip "); break; |
| 1625 | default: VG_(printf)("??? "); break; |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1626 | } |
| 1627 | switch (ELF32_ST_TYPE(o_symtab[i].st_info)) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1628 | case STT_NOTYPE: VG_(printf)("NOT "); break; |
| 1629 | case STT_OBJECT: VG_(printf)("OBJ "); break; |
| 1630 | case STT_FUNC: VG_(printf)("FUN "); break; |
| 1631 | case STT_SECTION: VG_(printf)("SEC "); break; |
| 1632 | case STT_FILE: VG_(printf)("FIL "); break; |
| 1633 | case STT_LOPROC: VG_(printf)("lop "); break; |
| 1634 | case STT_HIPROC: VG_(printf)("hip "); break; |
| 1635 | default: VG_(printf)("??? "); break; |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1636 | } |
| 1637 | VG_(printf)( |
| 1638 | ": value %p, size %d, name %s\n", |
| 1639 | si->offset+(UChar*)o_symtab[i].st_value, |
| 1640 | o_symtab[i].st_size, |
| 1641 | o_symtab[i].st_name |
| 1642 | ? ((Char*)o_strtab+o_symtab[i].st_name) |
| 1643 | : (Char*)"NONAME"); |
| 1644 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1645 | # endif |
| 1646 | |
| 1647 | /* Figure out if we're interested in the symbol. |
| 1648 | Firstly, is it of the right flavour? |
| 1649 | */ |
| 1650 | snaffle_it |
| 1651 | = ( (ELF32_ST_BIND(o_symtab[i].st_info) == STB_GLOBAL || |
| 1652 | ELF32_ST_BIND(o_symtab[i].st_info) == STB_LOCAL /* || |
| 1653 | ELF32_ST_BIND(o_symtab[i].st_info) == STB_WEAK */) |
| 1654 | && |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1655 | (ELF32_ST_TYPE(o_symtab[i].st_info) == STT_FUNC || |
| 1656 | (VG_(needs).data_syms && ELF32_ST_TYPE(o_symtab[i].st_info) == STT_OBJECT)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1657 | ); |
| 1658 | |
| 1659 | /* Secondly, if it's apparently in a GOT or PLT, it's really |
| 1660 | a reference to a symbol defined elsewhere, so ignore it. |
| 1661 | */ |
| 1662 | sym_addr = si->offset |
| 1663 | + (UInt)o_symtab[i].st_value; |
| 1664 | if (o_got != NULL |
| 1665 | && sym_addr >= (Addr)o_got |
| 1666 | && sym_addr < (Addr)(o_got+o_got_sz)) { |
| 1667 | snaffle_it = False; |
| 1668 | if (VG_(clo_trace_symtab)) { |
| 1669 | VG_(printf)( "in GOT: %s\n", |
| 1670 | o_strtab+o_symtab[i].st_name); |
| 1671 | } |
| 1672 | } |
| 1673 | if (o_plt != NULL |
| 1674 | && sym_addr >= (Addr)o_plt |
| 1675 | && sym_addr < (Addr)(o_plt+o_plt_sz)) { |
| 1676 | snaffle_it = False; |
| 1677 | if (VG_(clo_trace_symtab)) { |
| 1678 | VG_(printf)( "in PLT: %s\n", |
| 1679 | o_strtab+o_symtab[i].st_name); |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | /* Don't bother if nameless, or zero-sized. */ |
| 1684 | if (snaffle_it |
| 1685 | && (o_symtab[i].st_name == (Elf32_Word)NULL |
| 1686 | || /* VG_(strlen)(o_strtab+o_symtab[i].st_name) == 0 */ |
| 1687 | /* equivalent but cheaper ... */ |
| 1688 | * ((UChar*)(o_strtab+o_symtab[i].st_name)) == 0 |
| 1689 | || o_symtab[i].st_size == 0)) { |
| 1690 | snaffle_it = False; |
| 1691 | if (VG_(clo_trace_symtab)) { |
| 1692 | VG_(printf)( "size=0: %s\n", |
| 1693 | o_strtab+o_symtab[i].st_name); |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | # if 0 |
| 1698 | /* Avoid _dl_ junk. (Why?) */ |
| 1699 | /* 01-02-24: disabled until I find out if it really helps. */ |
| 1700 | if (snaffle_it |
| 1701 | && (VG_(strncmp)("_dl_", o_strtab+o_symtab[i].st_name, 4) == 0 |
| 1702 | || VG_(strncmp)("_r_debug", |
| 1703 | o_strtab+o_symtab[i].st_name, 8) == 0)) { |
| 1704 | snaffle_it = False; |
| 1705 | if (VG_(clo_trace_symtab)) { |
| 1706 | VG_(printf)( "_dl_ junk: %s\n", |
| 1707 | o_strtab+o_symtab[i].st_name); |
| 1708 | } |
| 1709 | } |
| 1710 | # endif |
| 1711 | |
| 1712 | /* This seems to significantly reduce the number of junk |
| 1713 | symbols, and particularly reduces the number of |
| 1714 | overlapping address ranges. Don't ask me why ... */ |
| 1715 | if (snaffle_it && (Int)o_symtab[i].st_value == 0) { |
| 1716 | snaffle_it = False; |
| 1717 | if (VG_(clo_trace_symtab)) { |
| 1718 | VG_(printf)( "valu=0: %s\n", |
| 1719 | o_strtab+o_symtab[i].st_name); |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | /* If no part of the symbol falls within the mapped range, |
| 1724 | ignore it. */ |
| 1725 | if (sym_addr+o_symtab[i].st_size <= si->start |
| 1726 | || sym_addr >= si->start+si->size) { |
| 1727 | snaffle_it = False; |
| 1728 | } |
| 1729 | |
| 1730 | if (snaffle_it) { |
| 1731 | /* it's an interesting symbol; record ("snaffle") it. */ |
| 1732 | RiSym sym; |
| 1733 | Char* t0 = o_symtab[i].st_name |
| 1734 | ? (Char*)(o_strtab+o_symtab[i].st_name) |
| 1735 | : (Char*)"NONAME"; |
| 1736 | Int nmoff = addStr ( si, t0 ); |
| 1737 | vg_assert(nmoff >= 0 |
| 1738 | /* && 0==VG_(strcmp)(t0,&vg_strtab[nmoff]) */ ); |
| 1739 | vg_assert( (Int)o_symtab[i].st_value >= 0); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1740 | /* VG_(printf)("%p + %d: %p %s\n", si->start, |
| 1741 | (Int)o_symtab[i].st_value, sym_addr, t0 ); */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1742 | sym.addr = sym_addr; |
| 1743 | sym.size = o_symtab[i].st_size; |
| 1744 | sym.nmoff = nmoff; |
| 1745 | addSym ( si, &sym ); |
| 1746 | } |
| 1747 | } |
| 1748 | } |
| 1749 | } |
| 1750 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1751 | /* Reading of the stabs and/or dwarf2 debug format information, if |
| 1752 | any. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1753 | stabstr = NULL; |
| 1754 | stab = NULL; |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1755 | dwarf2 = NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1756 | stabstr_sz = 0; |
| 1757 | stab_sz = 0; |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1758 | dwarf2_sz = 0; |
| 1759 | |
| 1760 | /* find the .stabstr / .stab / .debug_line sections */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1761 | for (i = 0; i < ehdr->e_shnum; i++) { |
| 1762 | if (0 == VG_(strcmp)(".stab",sh_strtab + shdr[i].sh_name)) { |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1763 | stab = (UChar*)(oimage + shdr[i].sh_offset); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1764 | stab_sz = shdr[i].sh_size; |
| 1765 | } |
| 1766 | if (0 == VG_(strcmp)(".stabstr",sh_strtab + shdr[i].sh_name)) { |
| 1767 | stabstr = (UChar*)(oimage + shdr[i].sh_offset); |
| 1768 | stabstr_sz = shdr[i].sh_size; |
| 1769 | } |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1770 | if (0 == VG_(strcmp)(".debug_line",sh_strtab + shdr[i].sh_name)) { |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1771 | dwarf2 = (UChar *)(oimage + shdr[i].sh_offset); |
| 1772 | dwarf2_sz = shdr[i].sh_size; |
sewardj | c134dd9 | 2002-06-01 14:21:36 +0000 | [diff] [blame] | 1773 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1776 | if ((stab == NULL || stabstr == NULL) && dwarf2 == NULL) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1777 | vg_symerr(" object doesn't have any debug info"); |
| 1778 | VG_(munmap) ( (void*)oimage, n_oimage ); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1779 | return False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | if ( stab_sz + (UChar*)stab > n_oimage + (UChar*)oimage |
| 1783 | || stabstr_sz + (UChar*)stabstr |
| 1784 | > n_oimage + (UChar*)oimage ) { |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1785 | vg_symerr(" ELF (stabs) debug data is beyond image end?!"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1786 | VG_(munmap) ( (void*)oimage, n_oimage ); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1787 | return False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1790 | if ( dwarf2_sz + (UChar*)dwarf2 > n_oimage + (UChar*)oimage ) { |
| 1791 | vg_symerr(" ELF (dwarf2) debug data is beyond image end?!"); |
| 1792 | VG_(munmap) ( (void*)oimage, n_oimage ); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1793 | return False; |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1794 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1795 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1796 | /* Looks plausible. Go on and read debug data. */ |
| 1797 | if (stab != NULL && stabstr != NULL) { |
| 1798 | read_debuginfo_stabs ( si, stab, stab_sz, stabstr, stabstr_sz ); |
| 1799 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1800 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 1801 | if (dwarf2 != NULL) { |
| 1802 | read_debuginfo_dwarf2 ( si, dwarf2, dwarf2_sz ); |
| 1803 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1804 | |
| 1805 | /* Last, but not least, heave the oimage back overboard. */ |
| 1806 | VG_(munmap) ( (void*)oimage, n_oimage ); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1807 | |
| 1808 | return True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | |
| 1812 | /*------------------------------------------------------------*/ |
| 1813 | /*--- Main entry point for symbols table reading. ---*/ |
| 1814 | /*------------------------------------------------------------*/ |
| 1815 | |
| 1816 | /* The root structure for the entire symbol table system. It is a |
| 1817 | linked list of SegInfos. Note that this entire mechanism assumes |
| 1818 | that what we read from /proc/self/maps doesn't contain overlapping |
| 1819 | address ranges, and as a result the SegInfos in this list describe |
| 1820 | disjoint address ranges. |
| 1821 | */ |
| 1822 | static SegInfo* segInfo = NULL; |
| 1823 | |
| 1824 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1825 | void VG_(read_symtab_callback) ( |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1826 | Addr start, UInt size, |
| 1827 | Char rr, Char ww, Char xx, |
| 1828 | UInt foffset, UChar* filename ) |
| 1829 | { |
| 1830 | SegInfo* si; |
| 1831 | |
| 1832 | /* Stay sane ... */ |
| 1833 | if (size == 0) |
| 1834 | return; |
| 1835 | |
| 1836 | /* We're only interested in collecting symbols in executable |
| 1837 | segments which are associated with a real file. Hence: */ |
| 1838 | if (filename == NULL || xx != 'x') |
| 1839 | return; |
| 1840 | if (0 == VG_(strcmp)(filename, "/dev/zero")) |
| 1841 | return; |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1842 | if (foffset != 0) |
| 1843 | return; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1844 | |
| 1845 | /* Perhaps we already have this one? If so, skip. */ |
| 1846 | for (si = segInfo; si != NULL; si = si->next) { |
| 1847 | /* |
| 1848 | if (0==VG_(strcmp)(si->filename, filename)) |
| 1849 | VG_(printf)("same fnames: %c%c%c (%p, %d) (%p, %d) %s\n", |
| 1850 | rr,ww,xx,si->start,si->size,start,size,filename); |
| 1851 | */ |
| 1852 | /* For some reason the observed size of a mapping can change, so |
| 1853 | we don't use that to determine uniqueness. */ |
| 1854 | if (si->start == start |
| 1855 | /* && si->size == size */ |
| 1856 | && 0==VG_(strcmp)(si->filename, filename)) { |
| 1857 | return; |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | /* Get the record initialised right. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1862 | si = VG_(arena_malloc)(VG_AR_SYMTAB, sizeof(SegInfo)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1863 | |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1864 | VG_(memset)(si, 0, sizeof(*si)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1865 | si->start = start; |
| 1866 | si->size = size; |
| 1867 | si->foffset = foffset; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1868 | si->filename = VG_(arena_malloc)(VG_AR_SYMTAB, 1 + VG_(strlen)(filename)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1869 | VG_(strcpy)(si->filename, filename); |
| 1870 | |
| 1871 | si->symtab = NULL; |
| 1872 | si->symtab_size = si->symtab_used = 0; |
| 1873 | si->loctab = NULL; |
| 1874 | si->loctab_size = si->loctab_used = 0; |
| 1875 | si->strtab = NULL; |
| 1876 | si->strtab_size = si->strtab_used = 0; |
| 1877 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1878 | /* And actually fill it up. */ |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1879 | if (!vg_read_lib_symbols ( si ) && 0) { |
| 1880 | /* XXX this interacts badly with the prevN optimization in |
| 1881 | addStr(). Since this frees the si, the si pointer value can |
| 1882 | be recycled, which confuses the curr_si == si test. For now, |
| 1883 | this code is disabled, and everything is included in the |
| 1884 | segment list, even if it is a bad ELF file. Ironically, |
| 1885 | running this under valgrind itself hides the problem, because |
| 1886 | it doesn't recycle pointers... */ |
| 1887 | freeSegInfo( si ); |
| 1888 | } else { |
| 1889 | si->next = segInfo; |
| 1890 | segInfo = si; |
| 1891 | |
| 1892 | canonicaliseSymtab ( si ); |
| 1893 | canonicaliseLoctab ( si ); |
| 1894 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | |
| 1898 | /* This one really is the Head Honcho. Update the symbol tables to |
| 1899 | reflect the current state of /proc/self/maps. Rather than re-read |
| 1900 | everything, just read the entries which are not already in segInfo. |
| 1901 | So we can call here repeatedly, after every mmap of a non-anonymous |
| 1902 | segment with execute permissions, for example, to pick up new |
| 1903 | libraries as they are dlopen'd. Conversely, when the client does |
| 1904 | munmap(), vg_symtab_notify_munmap() throws away any symbol tables |
| 1905 | which happen to correspond to the munmap()d area. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1906 | void VG_(maybe_read_symbols) ( void ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1907 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1908 | if (!VG_(using_debug_info)) |
| 1909 | return; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1910 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1911 | VGP_PUSHCC(VgpReadSyms); |
| 1912 | VG_(read_procselfmaps) ( VG_(read_symtab_callback) ); |
| 1913 | VGP_POPCC(VgpReadSyms); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1916 | /* When an munmap() call happens, check to see whether it corresponds |
| 1917 | to a segment for a .so, and if so discard the relevant SegInfo. |
| 1918 | This might not be a very clever idea from the point of view of |
| 1919 | accuracy of error messages, but we need to do it in order to |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1920 | maintain the no-overlapping invariant. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1921 | */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1922 | void VG_(maybe_unload_symbols) ( Addr start, UInt length ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1923 | { |
| 1924 | SegInfo *prev, *curr; |
| 1925 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1926 | if (!VG_(using_debug_info)) |
| 1927 | return; |
| 1928 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1929 | prev = NULL; |
| 1930 | curr = segInfo; |
| 1931 | while (True) { |
| 1932 | if (curr == NULL) break; |
| 1933 | if (start == curr->start) break; |
| 1934 | prev = curr; |
| 1935 | curr = curr->next; |
| 1936 | } |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1937 | if (curr == NULL) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1938 | return; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1939 | |
| 1940 | VG_(message)(Vg_UserMsg, |
| 1941 | "discard syms in %s due to munmap()", |
| 1942 | curr->filename ? curr->filename : (UChar*)"???"); |
| 1943 | |
| 1944 | vg_assert(prev == NULL || prev->next == curr); |
| 1945 | |
| 1946 | if (prev == NULL) { |
| 1947 | segInfo = curr->next; |
| 1948 | } else { |
| 1949 | prev->next = curr->next; |
| 1950 | } |
| 1951 | |
| 1952 | freeSegInfo(curr); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1953 | return; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | |
| 1957 | /*------------------------------------------------------------*/ |
| 1958 | /*--- Use of symbol table & location info to create ---*/ |
| 1959 | /*--- plausible-looking stack dumps. ---*/ |
| 1960 | /*------------------------------------------------------------*/ |
| 1961 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1962 | static __inline__ void ensure_debug_info_inited ( void ) |
| 1963 | { |
| 1964 | if (!VG_(using_debug_info)) { |
| 1965 | VG_(using_debug_info) = True; |
| 1966 | VG_(maybe_read_symbols)(); |
| 1967 | } |
| 1968 | } |
| 1969 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1970 | /* Find a symbol-table index containing the specified pointer, or -1 |
| 1971 | if not found. Binary search. */ |
| 1972 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1973 | static Int search_one_symtab ( SegInfo* si, Addr ptr, |
| 1974 | Bool match_anywhere_in_fun ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1975 | { |
| 1976 | Addr a_mid_lo, a_mid_hi; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1977 | Int mid, size, |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1978 | lo = 0, |
| 1979 | hi = si->symtab_used-1; |
| 1980 | while (True) { |
| 1981 | /* current unsearched space is from lo to hi, inclusive. */ |
| 1982 | if (lo > hi) return -1; /* not found */ |
| 1983 | mid = (lo + hi) / 2; |
| 1984 | a_mid_lo = si->symtab[mid].addr; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1985 | size = ( match_anywhere_in_fun |
| 1986 | ? si->symtab[mid].size |
| 1987 | : 1); |
| 1988 | a_mid_hi = ((Addr)si->symtab[mid].addr) + size - 1; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1989 | |
| 1990 | if (ptr < a_mid_lo) { hi = mid-1; continue; } |
| 1991 | if (ptr > a_mid_hi) { lo = mid+1; continue; } |
| 1992 | vg_assert(ptr >= a_mid_lo && ptr <= a_mid_hi); |
| 1993 | return mid; |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | |
| 1998 | /* Search all symtabs that we know about to locate ptr. If found, set |
| 1999 | *psi to the relevant SegInfo, and *symno to the symtab entry number |
| 2000 | within that. If not found, *psi is set to NULL. */ |
| 2001 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2002 | static void search_all_symtabs ( Addr ptr, /*OUT*/SegInfo** psi, |
| 2003 | /*OUT*/Int* symno, |
| 2004 | Bool match_anywhere_in_fun ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2005 | { |
| 2006 | Int sno; |
| 2007 | SegInfo* si; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2008 | |
| 2009 | ensure_debug_info_inited(); |
| 2010 | VGP_PUSHCC(VgpSearchSyms); |
| 2011 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2012 | for (si = segInfo; si != NULL; si = si->next) { |
| 2013 | if (si->start <= ptr && ptr < si->start+si->size) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2014 | sno = search_one_symtab ( si, ptr, match_anywhere_in_fun ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2015 | if (sno == -1) goto not_found; |
| 2016 | *symno = sno; |
| 2017 | *psi = si; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2018 | VGP_POPCC(VgpSearchSyms); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2019 | return; |
| 2020 | } |
| 2021 | } |
| 2022 | not_found: |
| 2023 | *psi = NULL; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2024 | VGP_POPCC(VgpSearchSyms); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | |
| 2028 | /* Find a location-table index containing the specified pointer, or -1 |
| 2029 | if not found. Binary search. */ |
| 2030 | |
| 2031 | static Int search_one_loctab ( SegInfo* si, Addr ptr ) |
| 2032 | { |
| 2033 | Addr a_mid_lo, a_mid_hi; |
| 2034 | Int mid, |
| 2035 | lo = 0, |
| 2036 | hi = si->loctab_used-1; |
| 2037 | while (True) { |
| 2038 | /* current unsearched space is from lo to hi, inclusive. */ |
| 2039 | if (lo > hi) return -1; /* not found */ |
| 2040 | mid = (lo + hi) / 2; |
| 2041 | a_mid_lo = si->loctab[mid].addr; |
| 2042 | a_mid_hi = ((Addr)si->loctab[mid].addr) + si->loctab[mid].size - 1; |
| 2043 | |
| 2044 | if (ptr < a_mid_lo) { hi = mid-1; continue; } |
| 2045 | if (ptr > a_mid_hi) { lo = mid+1; continue; } |
| 2046 | vg_assert(ptr >= a_mid_lo && ptr <= a_mid_hi); |
| 2047 | return mid; |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | |
| 2052 | /* Search all loctabs that we know about to locate ptr. If found, set |
| 2053 | *psi to the relevant SegInfo, and *locno to the loctab entry number |
| 2054 | within that. If not found, *psi is set to NULL. |
| 2055 | */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2056 | static void search_all_loctabs ( Addr ptr, /*OUT*/SegInfo** psi, |
| 2057 | /*OUT*/Int* locno ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2058 | { |
| 2059 | Int lno; |
| 2060 | SegInfo* si; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2061 | |
| 2062 | VGP_PUSHCC(VgpSearchSyms); |
| 2063 | |
| 2064 | ensure_debug_info_inited(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2065 | for (si = segInfo; si != NULL; si = si->next) { |
| 2066 | if (si->start <= ptr && ptr < si->start+si->size) { |
| 2067 | lno = search_one_loctab ( si, ptr ); |
| 2068 | if (lno == -1) goto not_found; |
| 2069 | *locno = lno; |
| 2070 | *psi = si; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2071 | VGP_POPCC(VgpSearchSyms); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2072 | return; |
| 2073 | } |
| 2074 | } |
| 2075 | not_found: |
| 2076 | *psi = NULL; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2077 | VGP_POPCC(VgpSearchSyms); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | |
| 2081 | /* The whole point of this whole big deal: map a code address to a |
| 2082 | plausible symbol name. Returns False if no idea; otherwise True. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2083 | Caller supplies buf and nbuf. If demangle is False, don't do |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2084 | demangling, regardless of vg_clo_demangle -- probably because the |
| 2085 | call has come from vg_what_fn_or_object_is_this. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2086 | static |
| 2087 | Bool get_fnname ( Bool demangle, Addr a, Char* buf, Int nbuf, |
sewardj | 1771e17 | 2002-11-13 22:06:35 +0000 | [diff] [blame] | 2088 | Bool match_anywhere_in_fun, Bool show_offset) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2089 | { |
| 2090 | SegInfo* si; |
| 2091 | Int sno; |
sewardj | 1771e17 | 2002-11-13 22:06:35 +0000 | [diff] [blame] | 2092 | Int offset; |
| 2093 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2094 | search_all_symtabs ( a, &si, &sno, match_anywhere_in_fun ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2095 | if (si == NULL) |
| 2096 | return False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2097 | if (demangle) { |
| 2098 | VG_(demangle) ( & si->strtab[si->symtab[sno].nmoff], buf, nbuf ); |
| 2099 | } else { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2100 | VG_(strncpy_safely) |
| 2101 | ( buf, & si->strtab[si->symtab[sno].nmoff], nbuf ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2102 | } |
sewardj | 1771e17 | 2002-11-13 22:06:35 +0000 | [diff] [blame] | 2103 | |
| 2104 | offset = a - si->symtab[sno].addr; |
| 2105 | if (show_offset && offset != 0) { |
| 2106 | Char buf2[12]; |
| 2107 | Char* symend = buf + VG_(strlen)(buf); |
| 2108 | Char* end = buf + nbuf; |
| 2109 | Int len; |
| 2110 | |
| 2111 | len = VG_(sprintf)(buf2, "%c%d", |
| 2112 | offset < 0 ? '-' : '+', |
| 2113 | offset < 0 ? -offset : offset); |
| 2114 | vg_assert(len < sizeof(buf2)); |
| 2115 | |
| 2116 | if (len < (end - symend)) { |
| 2117 | Char *cp = buf2; |
| 2118 | VG_(memcpy)(symend, cp, len+1); |
| 2119 | } |
| 2120 | } |
| 2121 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2122 | return True; |
| 2123 | } |
| 2124 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2125 | /* This is available to skins... always demangle C++ names */ |
| 2126 | Bool VG_(get_fnname) ( Addr a, Char* buf, Int nbuf ) |
| 2127 | { |
| 2128 | return get_fnname ( /*demangle*/True, a, buf, nbuf, |
sewardj | 1771e17 | 2002-11-13 22:06:35 +0000 | [diff] [blame] | 2129 | /*match_anywhere_in_fun*/True, True ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2130 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2131 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2132 | /* This is available to skins... always demangle C++ names, |
| 2133 | only succeed if 'a' matches first instruction of function. */ |
| 2134 | Bool VG_(get_fnname_if_entry) ( Addr a, Char* buf, Int nbuf ) |
| 2135 | { |
| 2136 | return get_fnname ( /*demangle*/True, a, buf, nbuf, |
sewardj | 1771e17 | 2002-11-13 22:06:35 +0000 | [diff] [blame] | 2137 | /*match_anywhere_in_fun*/False, False ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | /* This is only available to core... don't demangle C++ names */ |
| 2141 | Bool VG_(get_fnname_nodemangle) ( Addr a, Char* buf, Int nbuf ) |
| 2142 | { |
| 2143 | return get_fnname ( /*demangle*/False, a, buf, nbuf, |
sewardj | 1771e17 | 2002-11-13 22:06:35 +0000 | [diff] [blame] | 2144 | /*match_anywhere_in_fun*/True, False ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | /* Map a code address to the name of a shared object file or the executable. |
| 2148 | Returns False if no idea; otherwise True. Doesn't require debug info. |
| 2149 | Caller supplies buf and nbuf. */ |
| 2150 | Bool VG_(get_objname) ( Addr a, Char* buf, Int nbuf ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2151 | { |
| 2152 | SegInfo* si; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2153 | |
| 2154 | ensure_debug_info_inited(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2155 | for (si = segInfo; si != NULL; si = si->next) { |
| 2156 | if (si->start <= a && a < si->start+si->size) { |
| 2157 | VG_(strncpy_safely)(buf, si->filename, nbuf); |
| 2158 | return True; |
| 2159 | } |
| 2160 | } |
| 2161 | return False; |
| 2162 | } |
| 2163 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2164 | |
| 2165 | /* Map a code address to a filename. Returns True if successful. */ |
| 2166 | Bool VG_(get_filename)( Addr a, Char* filename, Int n_filename ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2167 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2168 | SegInfo* si; |
| 2169 | Int locno; |
| 2170 | search_all_loctabs ( a, &si, &locno ); |
| 2171 | if (si == NULL) |
| 2172 | return False; |
| 2173 | VG_(strncpy_safely)(filename, & si->strtab[si->loctab[locno].fnmoff], |
| 2174 | n_filename); |
| 2175 | return True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2178 | /* Map a code address to a line number. Returns True if successful. */ |
| 2179 | Bool VG_(get_linenum)( Addr a, UInt* lineno ) |
| 2180 | { |
| 2181 | SegInfo* si; |
| 2182 | Int locno; |
| 2183 | search_all_loctabs ( a, &si, &locno ); |
| 2184 | if (si == NULL) |
| 2185 | return False; |
| 2186 | *lineno = si->loctab[locno].lineno; |
| 2187 | |
| 2188 | return True; |
| 2189 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2190 | |
| 2191 | /* Map a code address to a (filename, line number) pair. |
| 2192 | Returns True if successful. |
| 2193 | */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2194 | Bool VG_(get_filename_linenum)( Addr a, |
| 2195 | Char* filename, Int n_filename, |
| 2196 | UInt* lineno ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2197 | { |
| 2198 | SegInfo* si; |
| 2199 | Int locno; |
| 2200 | search_all_loctabs ( a, &si, &locno ); |
| 2201 | if (si == NULL) |
| 2202 | return False; |
| 2203 | VG_(strncpy_safely)(filename, & si->strtab[si->loctab[locno].fnmoff], |
| 2204 | n_filename); |
| 2205 | *lineno = si->loctab[locno].lineno; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 2206 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2207 | return True; |
| 2208 | } |
| 2209 | |
| 2210 | |
| 2211 | /* Print a mini stack dump, showing the current location. */ |
| 2212 | void VG_(mini_stack_dump) ( ExeContext* ec ) |
| 2213 | { |
| 2214 | |
| 2215 | #define APPEND(str) \ |
| 2216 | { UChar* sss; \ |
| 2217 | for (sss = str; n < M_VG_ERRTXT-1 && *sss != 0; n++,sss++) \ |
| 2218 | buf[n] = *sss; \ |
| 2219 | buf[n] = 0; \ |
| 2220 | } |
| 2221 | |
| 2222 | Bool know_fnname; |
| 2223 | Bool know_objname; |
| 2224 | Bool know_srcloc; |
| 2225 | UInt lineno; |
| 2226 | UChar ibuf[20]; |
sewardj | 04b9106 | 2002-06-05 21:22:04 +0000 | [diff] [blame] | 2227 | UInt i, n; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2228 | |
| 2229 | UChar buf[M_VG_ERRTXT]; |
| 2230 | UChar buf_fn[M_VG_ERRTXT]; |
| 2231 | UChar buf_obj[M_VG_ERRTXT]; |
| 2232 | UChar buf_srcloc[M_VG_ERRTXT]; |
| 2233 | |
| 2234 | Int stop_at = VG_(clo_backtrace_size); |
| 2235 | |
njn | 49ef462 | 2002-10-04 15:15:08 +0000 | [diff] [blame] | 2236 | vg_assert(stop_at > 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2237 | |
njn | 49ef462 | 2002-10-04 15:15:08 +0000 | [diff] [blame] | 2238 | i = 0; |
| 2239 | do { |
| 2240 | n = 0; |
sewardj | 1771e17 | 2002-11-13 22:06:35 +0000 | [diff] [blame] | 2241 | know_fnname = get_fnname (True, ec->eips[i], buf_fn, M_VG_ERRTXT, True, False); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2242 | know_objname = VG_(get_objname)(ec->eips[i], buf_obj, M_VG_ERRTXT); |
| 2243 | know_srcloc = VG_(get_filename_linenum)(ec->eips[i], |
| 2244 | buf_srcloc, M_VG_ERRTXT, |
| 2245 | &lineno); |
njn | 49ef462 | 2002-10-04 15:15:08 +0000 | [diff] [blame] | 2246 | if (i == 0) APPEND(" at ") else APPEND(" by "); |
| 2247 | |
| 2248 | VG_(sprintf)(ibuf,"0x%x: ", ec->eips[i]); |
sewardj | 04b9106 | 2002-06-05 21:22:04 +0000 | [diff] [blame] | 2249 | APPEND(ibuf); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2250 | if (know_fnname) { |
njn | 49ef462 | 2002-10-04 15:15:08 +0000 | [diff] [blame] | 2251 | APPEND(buf_fn); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2252 | if (!know_srcloc && know_objname) { |
| 2253 | APPEND(" (in "); |
| 2254 | APPEND(buf_obj); |
| 2255 | APPEND(")"); |
| 2256 | } |
njn | 49ef462 | 2002-10-04 15:15:08 +0000 | [diff] [blame] | 2257 | } else if (know_objname && !know_srcloc) { |
| 2258 | APPEND("(within "); |
| 2259 | APPEND(buf_obj); |
| 2260 | APPEND(")"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2261 | } else { |
njn | 49ef462 | 2002-10-04 15:15:08 +0000 | [diff] [blame] | 2262 | APPEND("???"); |
| 2263 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2264 | if (know_srcloc) { |
| 2265 | APPEND(" ("); |
| 2266 | APPEND(buf_srcloc); |
| 2267 | APPEND(":"); |
| 2268 | VG_(sprintf)(ibuf,"%d",lineno); |
| 2269 | APPEND(ibuf); |
| 2270 | APPEND(")"); |
| 2271 | } |
| 2272 | VG_(message)(Vg_UserMsg, "%s", buf); |
njn | 49ef462 | 2002-10-04 15:15:08 +0000 | [diff] [blame] | 2273 | i++; |
| 2274 | |
| 2275 | } while (i < stop_at && ec->eips[i] != 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | #undef APPEND |
| 2279 | |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 2280 | /*------------------------------------------------------------*/ |
| 2281 | /*--- SegInfo accessor functions ---*/ |
| 2282 | /*------------------------------------------------------------*/ |
| 2283 | |
| 2284 | const SegInfo* VG_(next_seginfo)(const SegInfo* seg) |
| 2285 | { |
| 2286 | ensure_debug_info_inited(); |
| 2287 | |
| 2288 | if (seg == NULL) |
| 2289 | return segInfo; |
| 2290 | return seg->next; |
| 2291 | } |
| 2292 | |
| 2293 | Addr VG_(seg_start)(const SegInfo* seg) |
| 2294 | { |
| 2295 | return seg->start; |
| 2296 | } |
| 2297 | |
| 2298 | UInt VG_(seg_size)(const SegInfo* seg) |
| 2299 | { |
| 2300 | return seg->size; |
| 2301 | } |
| 2302 | |
| 2303 | const UChar* VG_(seg_filename)(const SegInfo* seg) |
| 2304 | { |
| 2305 | return seg->filename; |
| 2306 | } |
| 2307 | |
| 2308 | UInt VG_(seg_sym_offset)(const SegInfo* seg) |
| 2309 | { |
| 2310 | return seg->offset; |
| 2311 | } |
| 2312 | |
| 2313 | VgSectKind VG_(seg_sect_kind)(Addr a) |
| 2314 | { |
| 2315 | SegInfo* seg; |
| 2316 | VgSectKind ret = Vg_SectUnknown; |
| 2317 | |
| 2318 | ensure_debug_info_inited(); |
| 2319 | |
| 2320 | for(seg = segInfo; seg != NULL; seg = seg->next) { |
| 2321 | if (a >= seg->start && a < (seg->start + seg->size)) { |
| 2322 | if (0) |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 2323 | VG_(printf)("addr=%p seg=%p %s got=%p %d plt=%p %d data=%p %d bss=%p %d\n", |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 2324 | a, seg, seg->filename, |
| 2325 | seg->got_start, seg->got_size, |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 2326 | seg->plt_start, seg->plt_size, |
| 2327 | seg->data_start, seg->data_size, |
| 2328 | seg->bss_start, seg->bss_size); |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 2329 | ret = Vg_SectText; |
| 2330 | |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 2331 | if (a >= seg->data_start && a < (seg->data_start + seg->data_size)) |
| 2332 | ret = Vg_SectData; |
| 2333 | else if (a >= seg->bss_start && a < (seg->bss_start + seg->bss_size)) |
| 2334 | ret = Vg_SectBSS; |
| 2335 | else if (a >= seg->plt_start && a < (seg->plt_start + seg->plt_size)) |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 2336 | ret = Vg_SectPLT; |
| 2337 | else if (a >= seg->got_start && a < (seg->got_start + seg->got_size)) |
| 2338 | ret = Vg_SectGOT; |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | return ret; |
| 2343 | } |
| 2344 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2345 | /*--------------------------------------------------------------------*/ |
| 2346 | /*--- end vg_symtab2.c ---*/ |
| 2347 | /*--------------------------------------------------------------------*/ |