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 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2004 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 31 | #include "core.h" |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 32 | #include "vg_symtypes.h" |
| 33 | #include "vg_symtab2.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 34 | |
| 35 | #include <elf.h> /* ELF defns */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 36 | |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 37 | static Bool |
nethercote | b1e1ad4 | 2004-08-03 23:44:12 +0000 | [diff] [blame] | 38 | intercept_demangle(const Char*, Char*, Int); |
njn | 9aae674 | 2002-04-30 13:44:01 +0000 | [diff] [blame] | 39 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 40 | /* Majorly rewritten Sun 3 Feb 02 to enable loading symbols from |
| 41 | dlopen()ed libraries, which is something that KDE3 does a lot. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 42 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 43 | Stabs reader greatly improved by Nick Nethercote, Apr 02. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 44 | */ |
| 45 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 46 | static void freeSegInfo ( SegInfo* si ) |
| 47 | { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 48 | struct strchunk *chunk, *next; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 49 | vg_assert(si != NULL); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 50 | if (si->filename) VG_(arena_free)(VG_AR_SYMTAB, si->filename); |
| 51 | if (si->symtab) VG_(arena_free)(VG_AR_SYMTAB, si->symtab); |
| 52 | if (si->loctab) VG_(arena_free)(VG_AR_SYMTAB, si->loctab); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 53 | if (si->scopetab) VG_(arena_free)(VG_AR_SYMTAB, si->scopetab); |
| 54 | |
| 55 | for(chunk = si->strchunks; chunk != NULL; chunk = next) { |
| 56 | next = chunk->next; |
| 57 | VG_(arena_free)(VG_AR_SYMTAB, chunk); |
| 58 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 59 | VG_(arena_free)(VG_AR_SYMTAB, si); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | /*------------------------------------------------------------*/ |
| 64 | /*--- Adding stuff ---*/ |
| 65 | /*------------------------------------------------------------*/ |
| 66 | |
| 67 | /* Add a str to the string table, including terminating zero, and |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 68 | return pointer to the string in vg_strtab. Unless it's been seen |
| 69 | recently, in which case we find the old pointer and return that. |
| 70 | This avoids the most egregious duplications. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 71 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 72 | JSGF: changed from returning an index to a pointer, and changed to |
| 73 | a chunking memory allocator rather than reallocating, so the |
| 74 | pointers are stable. |
| 75 | */ |
| 76 | |
| 77 | Char *VG_(addStr) ( SegInfo* si, Char* str, Int len ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 78 | { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 79 | # define EMPTY NULL |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 80 | # define NN 5 |
| 81 | |
| 82 | /* prevN[0] has the most recent, prevN[NN-1] the least recent */ |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 83 | static Char *prevN[NN] = { EMPTY, EMPTY, EMPTY, EMPTY, EMPTY }; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 84 | static SegInfo* curr_si = NULL; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 85 | struct strchunk *chunk; |
| 86 | Int i, space_needed; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 87 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 88 | if (len == -1) |
| 89 | len = VG_(strlen)(str); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 90 | |
| 91 | /* Avoid gratuitous duplication: if we saw `str' within the last NN, |
| 92 | * within this segment, return that index. Saves about 200KB in glibc, |
| 93 | * extra time taken is too small to measure. --NJN 2002-Aug-30 */ |
| 94 | if (curr_si == si) { |
| 95 | for (i = NN-1; i >= 0; i--) { |
sewardj | cda419b | 2002-10-01 08:59:36 +0000 | [diff] [blame] | 96 | if (EMPTY != prevN[i] |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 97 | && NULL != si->strchunks |
jsgf | a065a9c | 2003-10-14 22:07:31 +0000 | [diff] [blame] | 98 | && 0 == VG_(memcmp)(str, prevN[i], len+1)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 99 | return prevN[i]; |
| 100 | } |
| 101 | } |
| 102 | } else { |
| 103 | /* New segment */ |
| 104 | curr_si = si; |
sewardj | cda419b | 2002-10-01 08:59:36 +0000 | [diff] [blame] | 105 | for (i = 0; i < NN; i++) prevN[i] = EMPTY; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 106 | } |
| 107 | /* Shuffle prevous ones along, put new one in. */ |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 108 | for (i = NN-1; i > 0; i--) |
| 109 | prevN[i] = prevN[i-1]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 110 | |
| 111 | # undef EMPTY |
| 112 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 113 | space_needed = 1 + len; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 114 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 115 | if (si->strchunks == NULL || |
| 116 | (si->strchunks->strtab_used + space_needed) > STRCHUNKSIZE) { |
| 117 | chunk = VG_(arena_malloc)(VG_AR_SYMTAB, sizeof(*chunk)); |
| 118 | chunk->strtab_used = 0; |
| 119 | chunk->next = si->strchunks; |
| 120 | si->strchunks = chunk; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 121 | } |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 122 | chunk = si->strchunks; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 123 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 124 | prevN[0] = &chunk->strtab[chunk->strtab_used]; |
| 125 | VG_(memcpy)(prevN[0], str, len); |
| 126 | chunk->strtab[chunk->strtab_used+len] = '\0'; |
| 127 | chunk->strtab_used += space_needed; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 128 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 129 | return prevN[0]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* Add a symbol to the symbol table. */ |
| 133 | |
| 134 | static __inline__ |
| 135 | void addSym ( SegInfo* si, RiSym* sym ) |
| 136 | { |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 137 | UInt new_sz, i; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 138 | RiSym* new_tab; |
| 139 | |
| 140 | /* Ignore zero-sized syms. */ |
| 141 | if (sym->size == 0) return; |
| 142 | |
| 143 | if (si->symtab_used == si->symtab_size) { |
| 144 | new_sz = 2 * si->symtab_size; |
| 145 | if (new_sz == 0) new_sz = 500; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 146 | new_tab = VG_(arena_malloc)(VG_AR_SYMTAB, new_sz * sizeof(RiSym) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 147 | if (si->symtab != NULL) { |
| 148 | for (i = 0; i < si->symtab_used; i++) |
| 149 | new_tab[i] = si->symtab[i]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 150 | VG_(arena_free)(VG_AR_SYMTAB, si->symtab); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 151 | } |
| 152 | si->symtab = new_tab; |
| 153 | si->symtab_size = new_sz; |
| 154 | } |
| 155 | |
| 156 | si->symtab[si->symtab_used] = *sym; |
| 157 | si->symtab_used++; |
| 158 | vg_assert(si->symtab_used <= si->symtab_size); |
| 159 | } |
| 160 | |
| 161 | /* Add a location to the location table. */ |
| 162 | |
| 163 | static __inline__ |
| 164 | void addLoc ( SegInfo* si, RiLoc* loc ) |
| 165 | { |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 166 | UInt new_sz, i; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 167 | RiLoc* new_tab; |
| 168 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 169 | /* Zero-sized locs should have been ignored earlier */ |
| 170 | vg_assert(loc->size > 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 171 | |
| 172 | if (si->loctab_used == si->loctab_size) { |
| 173 | new_sz = 2 * si->loctab_size; |
| 174 | if (new_sz == 0) new_sz = 500; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 175 | new_tab = VG_(arena_malloc)(VG_AR_SYMTAB, new_sz * sizeof(RiLoc) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 176 | if (si->loctab != NULL) { |
| 177 | for (i = 0; i < si->loctab_used; i++) |
| 178 | new_tab[i] = si->loctab[i]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 179 | VG_(arena_free)(VG_AR_SYMTAB, si->loctab); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 180 | } |
| 181 | si->loctab = new_tab; |
| 182 | si->loctab_size = new_sz; |
| 183 | } |
| 184 | |
| 185 | si->loctab[si->loctab_used] = *loc; |
| 186 | si->loctab_used++; |
| 187 | vg_assert(si->loctab_used <= si->loctab_size); |
| 188 | } |
| 189 | |
| 190 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 191 | /* Top-level place to call to add a source-location mapping entry. */ |
| 192 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 193 | void VG_(addLineInfo) ( SegInfo* si, |
| 194 | Char* filename, |
| 195 | Addr this, |
| 196 | Addr next, |
| 197 | Int lineno, |
| 198 | Int entry /* only needed for debug printing */ |
| 199 | ) |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 200 | { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 201 | static const Bool debug = False; |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 202 | RiLoc loc; |
| 203 | Int size = next - this; |
| 204 | |
| 205 | /* Ignore zero-sized locs */ |
| 206 | if (this == next) return; |
| 207 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 208 | if (debug) |
| 209 | VG_(printf)(" src %s line %d %p-%p\n", filename, lineno, this, next); |
| 210 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 211 | /* Maximum sanity checking. Some versions of GNU as do a shabby |
| 212 | * job with stabs entries; if anything looks suspicious, revert to |
| 213 | * a size of 1. This should catch the instruction of interest |
| 214 | * (since if using asm-level debug info, one instruction will |
| 215 | * correspond to one line, unlike with C-level debug info where |
| 216 | * multiple instructions can map to the one line), but avoid |
| 217 | * catching any other instructions bogusly. */ |
thughes | c1c5795 | 2004-11-01 17:36:15 +0000 | [diff] [blame] | 218 | if (this > next) { |
| 219 | if (VG_(clo_verbosity) > 2) { |
| 220 | VG_(message)(Vg_DebugMsg, |
| 221 | "warning: line info addresses out of order " |
| 222 | "at entry %d: 0x%x 0x%x", entry, this, next); |
| 223 | } |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 224 | size = 1; |
| 225 | } |
| 226 | |
| 227 | if (size > MAX_LOC_SIZE) { |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 228 | if (0) |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 229 | VG_(message)(Vg_DebugMsg, |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 230 | "warning: line info address range too large " |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 231 | "at entry %d: %d", entry, size); |
| 232 | size = 1; |
| 233 | } |
| 234 | |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 235 | /* vg_assert(this < si->start + si->size && next-1 >= si->start); */ |
njn | e306ffe | 2002-06-08 13:34:17 +0000 | [diff] [blame] | 236 | if (this >= si->start + si->size || next-1 < si->start) { |
sewardj | d84606d | 2002-06-18 01:04:57 +0000 | [diff] [blame] | 237 | if (0) |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 238 | VG_(message)(Vg_DebugMsg, |
| 239 | "warning: ignoring line info entry falling " |
| 240 | "outside current SegInfo: %p %p %p %p", |
| 241 | si->start, si->start + si->size, |
| 242 | this, next-1); |
njn | e306ffe | 2002-06-08 13:34:17 +0000 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | |
| 246 | vg_assert(lineno >= 0); |
| 247 | if (lineno > MAX_LINENO) { |
| 248 | VG_(message)(Vg_UserMsg, |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 249 | "warning: ignoring line info entry with " |
| 250 | "huge line number (%d)", lineno); |
njn | e306ffe | 2002-06-08 13:34:17 +0000 | [diff] [blame] | 251 | VG_(message)(Vg_UserMsg, |
| 252 | " Can't handle line numbers " |
sewardj | 08a50f6 | 2002-06-17 02:21:20 +0000 | [diff] [blame] | 253 | "greater than %d, sorry", MAX_LINENO); |
njn | e306ffe | 2002-06-08 13:34:17 +0000 | [diff] [blame] | 254 | return; |
| 255 | } |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 256 | |
| 257 | loc.addr = this; |
| 258 | loc.size = (UShort)size; |
| 259 | loc.lineno = lineno; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 260 | loc.filename = filename; |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 261 | |
| 262 | if (0) VG_(message)(Vg_DebugMsg, |
| 263 | "addLoc: addr %p, size %d, line %d, file %s", |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 264 | this,size,lineno,filename); |
sewardj | b642dc2 | 2002-10-12 17:27:16 +0000 | [diff] [blame] | 265 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 266 | addLoc ( si, &loc ); |
| 267 | } |
| 268 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 269 | static __inline__ |
| 270 | void addScopeRange ( SegInfo* si, ScopeRange *range ) |
| 271 | { |
| 272 | Int new_sz, i; |
| 273 | ScopeRange* new_tab; |
| 274 | |
| 275 | /* Zero-sized scopes should have been ignored earlier */ |
| 276 | vg_assert(range->size > 0); |
| 277 | |
| 278 | if (si->scopetab_used == si->scopetab_size) { |
| 279 | new_sz = 2 * si->scopetab_size; |
| 280 | if (new_sz == 0) new_sz = 500; |
| 281 | new_tab = VG_(arena_malloc)(VG_AR_SYMTAB, new_sz * sizeof(*new_tab) ); |
| 282 | if (si->scopetab != NULL) { |
| 283 | for (i = 0; i < si->scopetab_used; i++) |
| 284 | new_tab[i] = si->scopetab[i]; |
| 285 | VG_(arena_free)(VG_AR_SYMTAB, si->scopetab); |
| 286 | } |
| 287 | si->scopetab = new_tab; |
| 288 | si->scopetab_size = new_sz; |
| 289 | } |
| 290 | |
| 291 | si->scopetab[si->scopetab_used] = *range; |
| 292 | si->scopetab_used++; |
| 293 | vg_assert(si->scopetab_used <= si->scopetab_size); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /* Top-level place to call to add a source-location mapping entry. */ |
| 298 | |
| 299 | void VG_(addScopeInfo) ( SegInfo* si, |
| 300 | Addr this, |
| 301 | Addr next, |
| 302 | Scope *scope) |
| 303 | { |
| 304 | static const Bool debug = False; |
| 305 | Int size = next - this; |
| 306 | ScopeRange range; |
| 307 | |
fitzhardinge | 83d8a71 | 2004-02-05 22:58:37 +0000 | [diff] [blame] | 308 | /* Ignore zero-sized or negative scopes */ |
| 309 | if (size <= 0) { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 310 | if (debug) |
| 311 | VG_(printf)("ignoring zero-sized range, scope %p at %p\n", scope, this); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | if (debug) |
| 316 | VG_(printf)("adding scope range %p-%p (size=%d) scope %p (%d)\n", |
| 317 | this, next, next-this, scope, scope->depth); |
| 318 | |
| 319 | range.addr = this; |
| 320 | range.size = size; |
| 321 | range.scope = scope; |
| 322 | |
| 323 | addScopeRange ( si, &range ); |
| 324 | } |
| 325 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 326 | /*------------------------------------------------------------*/ |
| 327 | /*--- Helpers ---*/ |
| 328 | /*------------------------------------------------------------*/ |
| 329 | |
| 330 | /* Non-fatal -- use vg_panic if terminal. */ |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 331 | void VG_(symerr) ( Char* msg ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 332 | { |
| 333 | if (VG_(clo_verbosity) > 1) |
| 334 | VG_(message)(Vg_UserMsg,"%s", msg ); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /* Print a symbol. */ |
| 339 | static |
| 340 | void printSym ( SegInfo* si, Int i ) |
| 341 | { |
| 342 | VG_(printf)( "%5d: %8p .. %8p (%d) %s\n", |
| 343 | i, |
| 344 | si->symtab[i].addr, |
| 345 | si->symtab[i].addr + si->symtab[i].size - 1, si->symtab[i].size, |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 346 | si->symtab[i].name ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 347 | } |
| 348 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 349 | #define TRACE_SYMTAB(format, args...) \ |
| 350 | if (VG_(clo_trace_symtab)) { VG_(printf)(format, ## args); } |
| 351 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 352 | |
| 353 | #if 0 |
| 354 | /* Print the entire sym tab. */ |
| 355 | static __attribute__ ((unused)) |
| 356 | void printSymtab ( void ) |
| 357 | { |
| 358 | Int i; |
| 359 | VG_(printf)("\n------ BEGIN vg_symtab ------\n"); |
| 360 | for (i = 0; i < vg_symtab_used; i++) |
| 361 | printSym(i); |
| 362 | VG_(printf)("------ BEGIN vg_symtab ------\n"); |
| 363 | } |
| 364 | #endif |
| 365 | |
| 366 | #if 0 |
| 367 | /* Paranoid strcat. */ |
| 368 | static |
| 369 | void safeCopy ( UChar* dst, UInt maxlen, UChar* src ) |
| 370 | { |
| 371 | UInt i = 0, j = 0; |
| 372 | while (True) { |
| 373 | if (i >= maxlen) return; |
| 374 | if (dst[i] == 0) break; |
| 375 | i++; |
| 376 | } |
| 377 | while (True) { |
| 378 | if (i >= maxlen) return; |
| 379 | dst[i] = src[j]; |
| 380 | if (src[j] == 0) return; |
| 381 | i++; j++; |
| 382 | } |
| 383 | } |
| 384 | #endif |
| 385 | |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 386 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 387 | /*------------------------------------------------------------*/ |
| 388 | /*--- Canonicalisers ---*/ |
| 389 | /*------------------------------------------------------------*/ |
| 390 | |
| 391 | /* Sort the symtab by starting address, and emit warnings if any |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 392 | symbols have overlapping address ranges. We use that old chestnut, |
| 393 | shellsort. Mash the table around so as to establish the property |
| 394 | that addresses are in order and the ranges to not overlap. This |
| 395 | facilitates using binary search to map addresses to symbols when we |
| 396 | come to query the table. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 397 | */ |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 398 | static Int compare_RiSym(void *va, void *vb) { |
| 399 | RiSym *a = (RiSym *)va; |
| 400 | RiSym *b = (RiSym *)vb; |
| 401 | |
nethercote | 05fdfac | 2004-08-01 20:24:46 +0000 | [diff] [blame] | 402 | if (a->addr < b->addr) return -1; |
| 403 | if (a->addr > b->addr) return 1; |
| 404 | return 0; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 405 | } |
| 406 | |
fitzhardinge | 1c76dc4 | 2003-12-15 09:00:21 +0000 | [diff] [blame] | 407 | /* Two symbols have the same address. Which name do we prefer? |
| 408 | |
| 409 | In general we prefer the longer name, but if the choice is between |
| 410 | __libc_X and X, then choose X (similarly with __GI__ and __ |
| 411 | prefixes). |
| 412 | */ |
| 413 | static RiSym *prefersym(RiSym *a, RiSym *b) |
| 414 | { |
| 415 | Int pfx; |
fitzhardinge | efda47f | 2003-12-15 23:31:52 +0000 | [diff] [blame] | 416 | Int lena, lenb; |
| 417 | Int i; |
| 418 | static const struct { |
| 419 | const Char *prefix; |
| 420 | Int len; |
| 421 | } prefixes[] = { |
| 422 | #define PFX(x) { x, sizeof(x)-1 } |
| 423 | /* order from longest to shortest */ |
| 424 | PFX("__GI___libc_"), |
| 425 | PFX("__GI___"), |
| 426 | PFX("__libc_"), |
| 427 | PFX("__GI__"), |
| 428 | PFX("__GI_"), |
| 429 | PFX("__"), |
| 430 | #undef PFX |
| 431 | }; |
| 432 | |
| 433 | lena = VG_(strlen)(a->name); |
| 434 | lenb = VG_(strlen)(b->name); |
fitzhardinge | 1c76dc4 | 2003-12-15 09:00:21 +0000 | [diff] [blame] | 435 | |
| 436 | /* rearrange so that a is the long one */ |
fitzhardinge | efda47f | 2003-12-15 23:31:52 +0000 | [diff] [blame] | 437 | if (lena < lenb) { |
fitzhardinge | 1c76dc4 | 2003-12-15 09:00:21 +0000 | [diff] [blame] | 438 | RiSym *t; |
fitzhardinge | efda47f | 2003-12-15 23:31:52 +0000 | [diff] [blame] | 439 | Int lt; |
fitzhardinge | 1c76dc4 | 2003-12-15 09:00:21 +0000 | [diff] [blame] | 440 | |
| 441 | t = a; |
| 442 | a = b; |
| 443 | b = t; |
fitzhardinge | efda47f | 2003-12-15 23:31:52 +0000 | [diff] [blame] | 444 | |
| 445 | lt = lena; |
| 446 | lena = lenb; |
| 447 | lenb = lt; |
fitzhardinge | 1c76dc4 | 2003-12-15 09:00:21 +0000 | [diff] [blame] | 448 | } |
| 449 | |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 450 | /* Ugh. If we get a "free", always choose it. This is because |
| 451 | normally, this function would choose "cfree" over free. cfree is |
| 452 | an alias for free. If there's any more symbols like this, we may |
| 453 | want to consider making this mechanism more generic. |
| 454 | */ |
| 455 | if(VG_(strcmp)(a->name, "free") == 0) |
| 456 | return a; |
| 457 | |
| 458 | if(VG_(strcmp)(b->name, "free") == 0) |
| 459 | return b; |
| 460 | |
fitzhardinge | efda47f | 2003-12-15 23:31:52 +0000 | [diff] [blame] | 461 | for(i = pfx = 0; i < sizeof(prefixes)/sizeof(*prefixes); i++) { |
| 462 | Int pfxlen = prefixes[i].len; |
| 463 | |
| 464 | if (pfxlen < lena && |
| 465 | VG_(memcmp)(a->name, prefixes[i].prefix, pfxlen) == 0) { |
| 466 | pfx = pfxlen; |
| 467 | break; |
| 468 | } |
| 469 | } |
fitzhardinge | 1c76dc4 | 2003-12-15 09:00:21 +0000 | [diff] [blame] | 470 | |
| 471 | if (pfx != 0 && VG_(strcmp)(a->name + pfx, b->name) == 0) |
| 472 | return b; |
| 473 | |
| 474 | return a; |
| 475 | } |
| 476 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 477 | static |
| 478 | void canonicaliseSymtab ( SegInfo* si ) |
| 479 | { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 480 | Int i, j, n_merged, n_truncated; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 481 | Addr s1, s2, e1, e2; |
| 482 | |
| 483 | # define SWAP(ty,aa,bb) \ |
| 484 | do { ty tt = (aa); (aa) = (bb); (bb) = tt; } while (0) |
| 485 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 486 | if (si->symtab_used == 0) |
| 487 | return; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 488 | |
nethercote | 3acbb5d | 2003-11-13 21:50:45 +0000 | [diff] [blame] | 489 | VG_(ssort)(si->symtab, si->symtab_used, sizeof(*si->symtab), compare_RiSym); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 490 | |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 491 | for (i = 0; i < si->symtab_used; i++) { |
| 492 | if(VG_(strncmp)(si->symtab[i].name, VG_INTERCEPT_PREFIX, |
| 493 | VG_INTERCEPT_PREFIX_LEN) == 0) { |
| 494 | int len = VG_(strlen)(si->symtab[i].name); |
| 495 | char *buf = VG_(malloc)(len), *colon; |
nethercote | b1e1ad4 | 2004-08-03 23:44:12 +0000 | [diff] [blame] | 496 | intercept_demangle(si->symtab[i].name, buf, len); |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 497 | colon = buf + VG_(strlen)(buf) - 1; |
| 498 | while(*colon != ':') colon--; |
| 499 | VG_(strncpy_safely)(si->symtab[i].name, colon+1, len); |
| 500 | } |
| 501 | } |
| 502 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 503 | cleanup_more: |
| 504 | |
| 505 | /* If two symbols have identical address ranges, favour the |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 506 | one with the longer name (unless the extra length is junk) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 507 | */ |
| 508 | do { |
| 509 | n_merged = 0; |
| 510 | j = si->symtab_used; |
| 511 | si->symtab_used = 0; |
| 512 | for (i = 0; i < j; i++) { |
| 513 | if (i < j-1 |
| 514 | && si->symtab[i].addr == si->symtab[i+1].addr |
| 515 | && si->symtab[i].size == si->symtab[i+1].size) { |
| 516 | n_merged++; |
| 517 | /* merge the two into one */ |
fitzhardinge | 1c76dc4 | 2003-12-15 09:00:21 +0000 | [diff] [blame] | 518 | si->symtab[si->symtab_used++] = *prefersym(&si->symtab[i], &si->symtab[i+1]); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 519 | i++; |
| 520 | } else { |
| 521 | si->symtab[si->symtab_used++] = si->symtab[i]; |
| 522 | } |
| 523 | } |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 524 | TRACE_SYMTAB( "%d merged\n", n_merged); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 525 | } |
| 526 | while (n_merged > 0); |
| 527 | |
| 528 | /* Detect and "fix" overlapping address ranges. */ |
| 529 | n_truncated = 0; |
| 530 | |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 531 | for (i = 0; i < ((Int)si->symtab_used) -1; i++) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 532 | |
| 533 | vg_assert(si->symtab[i].addr <= si->symtab[i+1].addr); |
| 534 | |
| 535 | /* Check for common (no overlap) case. */ |
| 536 | if (si->symtab[i].addr + si->symtab[i].size |
| 537 | <= si->symtab[i+1].addr) |
| 538 | continue; |
| 539 | |
| 540 | /* There's an overlap. Truncate one or the other. */ |
| 541 | if (VG_(clo_trace_symtab)) { |
| 542 | VG_(printf)("overlapping address ranges in symbol table\n\t"); |
| 543 | printSym(si,i); |
| 544 | VG_(printf)("\t"); |
| 545 | printSym(si,i+1); |
| 546 | VG_(printf)("\n"); |
| 547 | } |
| 548 | |
| 549 | /* Truncate one or the other. */ |
| 550 | s1 = si->symtab[i].addr; |
| 551 | s2 = si->symtab[i+1].addr; |
| 552 | e1 = s1 + si->symtab[i].size - 1; |
| 553 | e2 = s2 + si->symtab[i+1].size - 1; |
| 554 | if (s1 < s2) { |
| 555 | e1 = s2-1; |
| 556 | } else { |
| 557 | vg_assert(s1 == s2); |
| 558 | if (e1 > e2) { |
| 559 | s1 = e2+1; SWAP(Addr,s1,s2); SWAP(Addr,e1,e2); |
| 560 | } else |
| 561 | if (e1 < e2) { |
| 562 | s2 = e1+1; |
| 563 | } else { |
| 564 | /* e1 == e2. Identical addr ranges. We'll eventually wind |
| 565 | up back at cleanup_more, which will take care of it. */ |
| 566 | } |
| 567 | } |
| 568 | si->symtab[i].addr = s1; |
| 569 | si->symtab[i+1].addr = s2; |
| 570 | si->symtab[i].size = e1 - s1 + 1; |
| 571 | si->symtab[i+1].size = e2 - s2 + 1; |
| 572 | vg_assert(s1 <= s2); |
| 573 | vg_assert(si->symtab[i].size > 0); |
| 574 | vg_assert(si->symtab[i+1].size > 0); |
| 575 | /* It may be that the i+1 entry now needs to be moved further |
| 576 | along to maintain the address order requirement. */ |
| 577 | j = i+1; |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 578 | while (j < ((Int)si->symtab_used)-1 |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 579 | && si->symtab[j].addr > si->symtab[j+1].addr) { |
| 580 | SWAP(RiSym,si->symtab[j],si->symtab[j+1]); |
| 581 | j++; |
| 582 | } |
| 583 | n_truncated++; |
| 584 | } |
| 585 | |
| 586 | if (n_truncated > 0) goto cleanup_more; |
| 587 | |
| 588 | /* Ensure relevant postconditions hold. */ |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 589 | for (i = 0; i < ((Int)si->symtab_used)-1; i++) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 590 | /* No zero-sized symbols. */ |
| 591 | vg_assert(si->symtab[i].size > 0); |
| 592 | /* In order. */ |
| 593 | vg_assert(si->symtab[i].addr < si->symtab[i+1].addr); |
| 594 | /* No overlaps. */ |
| 595 | vg_assert(si->symtab[i].addr + si->symtab[i].size - 1 |
| 596 | < si->symtab[i+1].addr); |
| 597 | } |
| 598 | # undef SWAP |
| 599 | } |
| 600 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 601 | /* Sort the scope range table by starting address. Mash the table |
| 602 | around so as to establish the property that addresses are in order |
| 603 | and the ranges do not overlap. This facilitates using binary |
| 604 | search to map addresses to scopes when we come to query the |
| 605 | table. |
| 606 | */ |
| 607 | static Int compare_ScopeRange(void *va, void *vb) { |
| 608 | ScopeRange *a = (ScopeRange *)va; |
| 609 | ScopeRange *b = (ScopeRange *)vb; |
| 610 | |
nethercote | 05fdfac | 2004-08-01 20:24:46 +0000 | [diff] [blame] | 611 | if (a->addr < b->addr) return -1; |
| 612 | if (a->addr > b->addr) return 1; |
| 613 | return 0; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | static |
| 617 | void canonicaliseScopetab ( SegInfo* si ) |
| 618 | { |
| 619 | Int i,j; |
| 620 | |
| 621 | if (si->scopetab_used == 0) |
| 622 | return; |
| 623 | |
| 624 | /* Sort by start address. */ |
nethercote | 3acbb5d | 2003-11-13 21:50:45 +0000 | [diff] [blame] | 625 | VG_(ssort)(si->scopetab, si->scopetab_used, sizeof(*si->scopetab), |
| 626 | compare_ScopeRange); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 627 | |
| 628 | /* If two adjacent entries overlap, truncate the first. */ |
| 629 | for (i = 0; i < si->scopetab_used-1; i++) { |
| 630 | if (si->scopetab[i].addr + si->scopetab[i].size > si->scopetab[i+1].addr) { |
| 631 | Int new_size = si->scopetab[i+1].addr - si->scopetab[i].addr; |
| 632 | |
| 633 | if (new_size < 0) |
| 634 | si->scopetab[i].size = 0; |
| 635 | else |
| 636 | si->scopetab[i].size = new_size; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /* Zap any zero-sized entries resulting from the truncation |
| 641 | process. */ |
| 642 | j = 0; |
| 643 | for (i = 0; i < si->scopetab_used; i++) { |
| 644 | if (si->scopetab[i].size > 0) { |
| 645 | si->scopetab[j] = si->scopetab[i]; |
| 646 | j++; |
| 647 | } |
| 648 | } |
| 649 | si->scopetab_used = j; |
| 650 | |
| 651 | /* Ensure relevant postconditions hold. */ |
| 652 | for (i = 0; i < si->scopetab_used-1; i++) { |
| 653 | /* |
| 654 | VG_(printf)("%d (%d) %d 0x%x\n", |
| 655 | i, si->scopetab[i+1].confident, |
| 656 | si->scopetab[i+1].size, si->scopetab[i+1].addr ); |
| 657 | */ |
| 658 | /* No zero-sized symbols. */ |
| 659 | vg_assert(si->scopetab[i].size > 0); |
| 660 | /* In order. */ |
| 661 | if (si->scopetab[i].addr >= si->scopetab[i+1].addr) |
| 662 | VG_(printf)("si->scopetab[%d] = %p,size=%d [%d] = %p,size=%d\n", |
| 663 | i, si->scopetab[i].addr, si->scopetab[i].size, |
| 664 | i+1, si->scopetab[i+1].addr, si->scopetab[i+1].size); |
| 665 | vg_assert(si->scopetab[i].addr < si->scopetab[i+1].addr); |
| 666 | /* No overlaps. */ |
| 667 | vg_assert(si->scopetab[i].addr + si->scopetab[i].size - 1 |
| 668 | < si->scopetab[i+1].addr); |
| 669 | } |
| 670 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 671 | |
| 672 | |
| 673 | /* Sort the location table by starting address. Mash the table around |
| 674 | so as to establish the property that addresses are in order and the |
| 675 | ranges do not overlap. This facilitates using binary search to map |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 676 | addresses to locations when we come to query the table. |
| 677 | */ |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 678 | static Int compare_RiLoc(void *va, void *vb) { |
| 679 | RiLoc *a = (RiLoc *)va; |
| 680 | RiLoc *b = (RiLoc *)vb; |
| 681 | |
nethercote | 05fdfac | 2004-08-01 20:24:46 +0000 | [diff] [blame] | 682 | if (a->addr < b->addr) return -1; |
| 683 | if (a->addr > b->addr) return 1; |
| 684 | return 0; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 685 | } |
| 686 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 687 | static |
| 688 | void canonicaliseLoctab ( SegInfo* si ) |
| 689 | { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 690 | Int i, j; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 691 | |
| 692 | # define SWAP(ty,aa,bb) \ |
| 693 | do { ty tt = (aa); (aa) = (bb); (bb) = tt; } while (0); |
| 694 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 695 | if (si->loctab_used == 0) |
| 696 | return; |
| 697 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 698 | /* Sort by start address. */ |
nethercote | 3acbb5d | 2003-11-13 21:50:45 +0000 | [diff] [blame] | 699 | VG_(ssort)(si->loctab, si->loctab_used, sizeof(*si->loctab), compare_RiLoc); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 700 | |
| 701 | /* If two adjacent entries overlap, truncate the first. */ |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 702 | for (i = 0; i < ((Int)si->loctab_used)-1; i++) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 703 | vg_assert(si->loctab[i].size < 10000); |
| 704 | if (si->loctab[i].addr + si->loctab[i].size > si->loctab[i+1].addr) { |
| 705 | /* Do this in signed int32 because the actual .size fields |
| 706 | are unsigned 16s. */ |
| 707 | Int new_size = si->loctab[i+1].addr - si->loctab[i].addr; |
| 708 | if (new_size < 0) { |
| 709 | si->loctab[i].size = 0; |
| 710 | } else |
| 711 | if (new_size >= 65536) { |
| 712 | si->loctab[i].size = 65535; |
| 713 | } else { |
| 714 | si->loctab[i].size = (UShort)new_size; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | /* Zap any zero-sized entries resulting from the truncation |
| 720 | process. */ |
| 721 | j = 0; |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 722 | for (i = 0; i < (Int)si->loctab_used; i++) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 723 | if (si->loctab[i].size > 0) { |
| 724 | si->loctab[j] = si->loctab[i]; |
| 725 | j++; |
| 726 | } |
| 727 | } |
| 728 | si->loctab_used = j; |
| 729 | |
| 730 | /* Ensure relevant postconditions hold. */ |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 731 | for (i = 0; i < ((Int)si->loctab_used)-1; i++) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 732 | /* |
| 733 | VG_(printf)("%d (%d) %d 0x%x\n", |
| 734 | i, si->loctab[i+1].confident, |
| 735 | si->loctab[i+1].size, si->loctab[i+1].addr ); |
| 736 | */ |
| 737 | /* No zero-sized symbols. */ |
| 738 | vg_assert(si->loctab[i].size > 0); |
| 739 | /* In order. */ |
| 740 | vg_assert(si->loctab[i].addr < si->loctab[i+1].addr); |
| 741 | /* No overlaps. */ |
| 742 | vg_assert(si->loctab[i].addr + si->loctab[i].size - 1 |
| 743 | < si->loctab[i+1].addr); |
| 744 | } |
| 745 | # undef SWAP |
| 746 | } |
| 747 | |
| 748 | |
| 749 | /*------------------------------------------------------------*/ |
sewardj | b51f2e6 | 2002-06-01 23:11:19 +0000 | [diff] [blame] | 750 | /*--- Read info from a .so/exe file. ---*/ |
| 751 | /*------------------------------------------------------------*/ |
| 752 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 753 | Bool VG_(is_object_file)(const void *buf) |
| 754 | { |
| 755 | { |
| 756 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *)buf; |
| 757 | Int ok = 1; |
| 758 | |
| 759 | ok &= (ehdr->e_ident[EI_MAG0] == 0x7F |
| 760 | && ehdr->e_ident[EI_MAG1] == 'E' |
| 761 | && ehdr->e_ident[EI_MAG2] == 'L' |
| 762 | && ehdr->e_ident[EI_MAG3] == 'F'); |
nethercote | bdaa89f | 2004-10-09 19:08:08 +0000 | [diff] [blame] | 763 | ok &= (ehdr->e_ident[EI_CLASS] == VG_ELF_CLASS |
| 764 | && ehdr->e_ident[EI_DATA] == VG_ELF_ENDIANNESS |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 765 | && ehdr->e_ident[EI_VERSION] == EV_CURRENT); |
| 766 | ok &= (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN); |
nethercote | bdaa89f | 2004-10-09 19:08:08 +0000 | [diff] [blame] | 767 | ok &= (ehdr->e_machine == VG_ELF_MACHINE); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 768 | ok &= (ehdr->e_version == EV_CURRENT); |
| 769 | ok &= (ehdr->e_shstrndx != SHN_UNDEF); |
| 770 | ok &= (ehdr->e_shoff != 0 && ehdr->e_shnum != 0); |
| 771 | ok &= (ehdr->e_phoff != 0 && ehdr->e_phnum != 0); |
| 772 | |
| 773 | if (ok) |
| 774 | return True; |
| 775 | } |
| 776 | |
| 777 | /* other file formats here? */ |
| 778 | |
| 779 | return False; |
| 780 | } |
| 781 | |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 782 | /* |
| 783 | * Demangle an intercept symbol into library:func form |
| 784 | */ |
| 785 | |
| 786 | static Bool |
nethercote | b1e1ad4 | 2004-08-03 23:44:12 +0000 | [diff] [blame] | 787 | intercept_demangle(const Char* symbol, Char* result, Int nbytes) |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 788 | { |
| 789 | int i, j = 0; |
| 790 | int len = VG_(strlen)(symbol); |
| 791 | |
| 792 | for(i = VG_INTERCEPT_PREFIX_LEN; i < len; i++) { |
| 793 | if(symbol[i] == '$') { |
| 794 | i++; |
| 795 | if(symbol[i] == '$') { |
| 796 | result[j] = '$'; |
| 797 | } else if((symbol[i] >= '0' && symbol[i] <= '9') || |
| 798 | (symbol[i] >= 'a' && symbol[i] <= 'f') || |
| 799 | (symbol[i] >= 'A' && symbol[i] <= 'F')) { |
| 800 | int x = symbol[i++]; |
| 801 | int y = symbol[i]; |
| 802 | if(x >= '0' && x <= '9') { |
| 803 | x -= '0'; |
| 804 | } else if(x >= 'a' && x <= 'f') { |
| 805 | x -= 'a'; |
| 806 | } else if(x >= 'A' && x <= 'F') { |
| 807 | x -= 'A'; |
| 808 | } |
| 809 | if(y >= '0' && y <= '9') { |
| 810 | y -= '0'; |
| 811 | } else if(y >= 'a' && y <= 'f') { |
| 812 | y = y - 'a' + 10; |
| 813 | } else if(y >= 'A' && y <= 'F') { |
| 814 | y = y - 'A' + 10; |
| 815 | } else { |
| 816 | return False; |
| 817 | } |
| 818 | result[j] = (x << 4) | y; |
| 819 | } else { |
| 820 | return False; |
| 821 | } |
| 822 | } else { |
| 823 | result[j] = symbol[i]; |
| 824 | } |
| 825 | if(j >= nbytes) { |
| 826 | result[j] = '\0'; |
| 827 | return True; |
| 828 | } |
| 829 | j++; |
| 830 | } |
| 831 | result[j] = '\0'; |
| 832 | return True; |
| 833 | } |
| 834 | |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 835 | // Forward declaration |
| 836 | static void add_redirect_addr(const Char *from_lib, const Char *from_sym, |
| 837 | Addr to_addr); |
| 838 | |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 839 | static |
| 840 | void handle_intercept( SegInfo* si, Char* symbol, Elf32_Sym* sym) |
| 841 | { |
| 842 | int len = VG_(strlen)(symbol) + 1 - VG_INTERCEPT_PREFIX_LEN; |
| 843 | char *lib = VG_(malloc)(len); |
| 844 | Char *func; |
| 845 | |
nethercote | b1e1ad4 | 2004-08-03 23:44:12 +0000 | [diff] [blame] | 846 | intercept_demangle(symbol, lib, len); |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 847 | func = lib + VG_(strlen)(lib)-1; |
| 848 | |
| 849 | while(*func != ':') func--; |
| 850 | *func = '\0'; |
| 851 | |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 852 | add_redirect_addr(lib, func+1, si->offset + sym->st_value); |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 853 | VG_(free)(lib); |
| 854 | } |
| 855 | |
| 856 | static |
| 857 | void handle_wrapper( SegInfo* si, Char* symbol, Elf32_Sym* sym) |
| 858 | { |
| 859 | VG_(intercept_libc_freeres_wrapper)((Addr)(si->offset + sym->st_value)); |
| 860 | } |
| 861 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 862 | /* Read a symbol table (normal or dynamic) */ |
| 863 | static |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 864 | void read_symtab( SegInfo* si, Char* tab_name, Bool do_intercepts, |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 865 | Elf32_Sym* o_symtab, UInt o_symtab_sz, |
| 866 | UChar* o_strtab, UInt o_strtab_sz ) |
| 867 | { |
| 868 | Int i; |
| 869 | Addr sym_addr; |
| 870 | RiSym risym; |
mueller | 82df83e | 2003-11-19 22:05:35 +0000 | [diff] [blame] | 871 | Char* t0; |
| 872 | Char* name; |
| 873 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 874 | if (o_strtab == NULL || o_symtab == NULL) { |
jseward | 0edbfb5 | 2003-12-12 06:22:06 +0000 | [diff] [blame] | 875 | Char buf[80]; |
| 876 | vg_assert(VG_(strlen)(tab_name) < 40); |
| 877 | VG_(sprintf)(buf, " object doesn't have a %s", tab_name); |
| 878 | VG_(symerr)(buf); |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 879 | return; |
| 880 | } |
| 881 | |
| 882 | TRACE_SYMTAB("Reading %s (%d entries)\n", tab_name, |
| 883 | o_symtab_sz/sizeof(Elf32_Sym) ); |
| 884 | |
| 885 | /* Perhaps should start at i = 1; ELF docs suggest that entry |
| 886 | 0 always denotes `unknown symbol'. */ |
| 887 | for (i = 1; i < (Int)(o_symtab_sz/sizeof(Elf32_Sym)); i++) { |
| 888 | Elf32_Sym* sym = & o_symtab[i]; |
| 889 | # if 1 |
| 890 | sym_addr = si->offset + (UInt)sym->st_value; |
| 891 | |
| 892 | if (VG_(clo_trace_symtab)) { |
| 893 | VG_(printf)("raw symbol [%d]: ", i); |
| 894 | switch (ELF32_ST_BIND(sym->st_info)) { |
| 895 | case STB_LOCAL: VG_(printf)("LOC "); break; |
| 896 | case STB_GLOBAL: VG_(printf)("GLO "); break; |
| 897 | case STB_WEAK: VG_(printf)("WEA "); break; |
| 898 | case STB_LOPROC: VG_(printf)("lop "); break; |
| 899 | case STB_HIPROC: VG_(printf)("hip "); break; |
| 900 | default: VG_(printf)("??? "); break; |
| 901 | } |
| 902 | switch (ELF32_ST_TYPE(sym->st_info)) { |
| 903 | case STT_NOTYPE: VG_(printf)("NOT "); break; |
| 904 | case STT_OBJECT: VG_(printf)("OBJ "); break; |
| 905 | case STT_FUNC: VG_(printf)("FUN "); break; |
| 906 | case STT_SECTION: VG_(printf)("SEC "); break; |
| 907 | case STT_FILE: VG_(printf)("FIL "); break; |
| 908 | case STT_LOPROC: VG_(printf)("lop "); break; |
| 909 | case STT_HIPROC: VG_(printf)("hip "); break; |
| 910 | default: VG_(printf)("??? "); break; |
| 911 | } |
| 912 | VG_(printf)( |
| 913 | ": value %p, size %d, name %s\n", |
| 914 | sym_addr, sym->st_size, |
| 915 | ( sym->st_name |
| 916 | ? ((Char*)o_strtab+sym->st_name) |
| 917 | : (Char*)"NONAME" ) ); |
| 918 | } |
| 919 | # endif |
| 920 | |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 921 | /* |
| 922 | * Is this symbol a magic valgrind-intercept symbol? If so, |
| 923 | * hand this off to the interceptinator. |
| 924 | */ |
| 925 | if (do_intercepts) { |
| 926 | if (VG_(strncmp)((Char*)o_strtab+sym->st_name, |
| 927 | VG_INTERCEPT_PREFIX, |
| 928 | VG_INTERCEPT_PREFIX_LEN) == 0) { |
| 929 | handle_intercept(si, (Char*)o_strtab+sym->st_name, sym); |
| 930 | } else if (VG_(strncmp)((Char*)o_strtab+sym->st_name, |
| 931 | VG_WRAPPER_PREFIX, |
| 932 | VG_WRAPPER_PREFIX_LEN) == 0) { |
| 933 | handle_wrapper(si, (Char*)o_strtab+sym->st_name, sym); |
| 934 | } |
| 935 | } |
| 936 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 937 | /* Figure out if we're interested in the symbol. |
| 938 | Firstly, is it of the right flavour? */ |
| 939 | if ( ! ( (ELF32_ST_BIND(sym->st_info) == STB_GLOBAL || |
| 940 | ELF32_ST_BIND(sym->st_info) == STB_LOCAL || |
| 941 | ELF32_ST_BIND(sym->st_info) == STB_WEAK) |
| 942 | && |
| 943 | (ELF32_ST_TYPE(sym->st_info) == STT_FUNC || |
| 944 | (VG_(needs).data_syms |
| 945 | && ELF32_ST_TYPE(sym->st_info) == STT_OBJECT)) |
| 946 | ) |
| 947 | ) |
| 948 | continue; |
| 949 | |
| 950 | /* Secondly, if it's apparently in a GOT or PLT, it's really |
| 951 | a reference to a symbol defined elsewhere, so ignore it. */ |
| 952 | if (si->got_start != 0 |
| 953 | && sym_addr >= si->got_start |
| 954 | && sym_addr < si->got_start + si->got_size) { |
| 955 | TRACE_SYMTAB("in GOT: %s\n", o_strtab+sym->st_name); |
| 956 | continue; |
| 957 | } |
| 958 | if (si->plt_start != 0 |
| 959 | && sym_addr >= si->plt_start |
| 960 | && sym_addr < si->plt_start + si->plt_size) { |
| 961 | TRACE_SYMTAB("in PLT: %s\n", o_strtab+sym->st_name); |
| 962 | continue; |
| 963 | } |
| 964 | |
| 965 | /* Don't bother if nameless, or zero-sized. */ |
| 966 | if (sym->st_name == (Elf32_Word)NULL |
| 967 | || /* VG_(strlen)(o_strtab+sym->st_name) == 0 */ |
| 968 | /* equivalent but cheaper ... */ |
| 969 | * ((UChar*)(o_strtab+sym->st_name)) == 0 |
| 970 | || sym->st_size == 0) { |
| 971 | TRACE_SYMTAB("size=0: %s\n", o_strtab+sym->st_name); |
| 972 | continue; |
| 973 | } |
| 974 | |
| 975 | # if 0 |
| 976 | /* Avoid _dl_ junk. (Why?) */ |
| 977 | /* 01-02-24: disabled until I find out if it really helps. */ |
| 978 | if (VG_(strncmp)("_dl_", o_strtab+sym->st_name, 4) == 0 |
| 979 | || VG_(strncmp)("_r_debug", |
| 980 | o_strtab+sym->st_name, 8) == 0) { |
| 981 | TRACE_SYMTAB("_dl_ junk: %s\n", o_strtab+sym->st_name); |
| 982 | continue; |
| 983 | } |
| 984 | # endif |
| 985 | |
| 986 | /* This seems to significantly reduce the number of junk |
| 987 | symbols, and particularly reduces the number of |
| 988 | overlapping address ranges. Don't ask me why ... */ |
| 989 | if ((Int)sym->st_value == 0) { |
| 990 | TRACE_SYMTAB( "valu=0: %s\n", o_strtab+sym->st_name); |
| 991 | continue; |
| 992 | } |
| 993 | |
| 994 | /* If no part of the symbol falls within the mapped range, |
| 995 | ignore it. */ |
| 996 | if (sym_addr+sym->st_size <= si->start |
| 997 | || sym_addr >= si->start+si->size) { |
| 998 | TRACE_SYMTAB( "outside mapped range" ); |
| 999 | continue; |
| 1000 | } |
| 1001 | |
| 1002 | /* If we reach here, it's an interesting symbol; record it. */ |
mueller | 82df83e | 2003-11-19 22:05:35 +0000 | [diff] [blame] | 1003 | t0 = sym->st_name |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1004 | ? (Char*)(o_strtab+sym->st_name) |
| 1005 | : (Char*)"NONAME"; |
mueller | 82df83e | 2003-11-19 22:05:35 +0000 | [diff] [blame] | 1006 | name = VG_(addStr) ( si, t0, -1 ); |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1007 | vg_assert(name != NULL |
| 1008 | /* && 0==VG_(strcmp)(t0,&vg_strtab[nmoff]) */ ); |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1009 | /* VG_(printf)("%p + %d: %p %s\n", si->start, |
| 1010 | (Int)sym->st_value, sym_addr, t0 ); */ |
| 1011 | risym.addr = sym_addr; |
| 1012 | risym.size = sym->st_size; |
| 1013 | risym.name = name; |
| 1014 | addSym ( si, &risym ); |
| 1015 | } |
| 1016 | } |
| 1017 | |
thughes | c035bd9 | 2004-06-13 09:59:02 +0000 | [diff] [blame] | 1018 | /* |
| 1019 | * This routine for calculating the CRC for a separate debug file |
| 1020 | * is GPLed code borrowed from binutils. |
| 1021 | */ |
| 1022 | static UInt |
| 1023 | calc_gnu_debuglink_crc32(UInt crc, const UChar *buf, Int len) |
| 1024 | { |
| 1025 | static const UInt crc32_table[256] = |
| 1026 | { |
| 1027 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, |
| 1028 | 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, |
| 1029 | 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, |
| 1030 | 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, |
| 1031 | 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, |
| 1032 | 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, |
| 1033 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, |
| 1034 | 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, |
| 1035 | 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, |
| 1036 | 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, |
| 1037 | 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, |
| 1038 | 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, |
| 1039 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, |
| 1040 | 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, |
| 1041 | 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, |
| 1042 | 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, |
| 1043 | 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, |
| 1044 | 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, |
| 1045 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, |
| 1046 | 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, |
| 1047 | 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, |
| 1048 | 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, |
| 1049 | 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, |
| 1050 | 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, |
| 1051 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, |
| 1052 | 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, |
| 1053 | 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, |
| 1054 | 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, |
| 1055 | 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, |
| 1056 | 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, |
| 1057 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, |
| 1058 | 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, |
| 1059 | 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, |
| 1060 | 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, |
| 1061 | 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, |
| 1062 | 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, |
| 1063 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, |
| 1064 | 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, |
| 1065 | 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, |
| 1066 | 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, |
| 1067 | 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, |
| 1068 | 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, |
| 1069 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, |
| 1070 | 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, |
| 1071 | 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, |
| 1072 | 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, |
| 1073 | 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, |
| 1074 | 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, |
| 1075 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, |
| 1076 | 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, |
| 1077 | 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, |
| 1078 | 0x2d02ef8d |
| 1079 | }; |
| 1080 | const UChar *end; |
| 1081 | |
| 1082 | crc = ~crc & 0xffffffff; |
| 1083 | for (end = buf + len; buf < end; ++ buf) |
| 1084 | crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8); |
| 1085 | return ~crc & 0xffffffff;; |
| 1086 | } |
| 1087 | |
| 1088 | /* |
| 1089 | * Try and open a separate debug file, ignoring any where the CRC does |
| 1090 | * not match the value from the main object file. |
| 1091 | */ |
| 1092 | static |
| 1093 | Addr open_debug_file( Char* name, UInt crc, UInt* size ) |
| 1094 | { |
| 1095 | Int fd; |
| 1096 | struct vki_stat stat_buf; |
| 1097 | Addr addr; |
| 1098 | |
| 1099 | if ((fd = VG_(open)(name, VKI_O_RDONLY, 0)) < 0) |
| 1100 | return 0; |
| 1101 | |
| 1102 | if (VG_(fstat)(fd, &stat_buf) != 0) { |
| 1103 | VG_(close)(fd); |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | *size = stat_buf.st_size; |
| 1108 | |
| 1109 | if ((addr = (Addr)VG_(mmap)(NULL, *size, VKI_PROT_READ, |
nethercote | b4250ae | 2004-07-10 16:50:09 +0000 | [diff] [blame] | 1110 | VKI_MAP_PRIVATE|VKI_MAP_NOSYMS, |
| 1111 | 0, fd, 0)) == (Addr)-1) |
| 1112 | { |
thughes | c035bd9 | 2004-06-13 09:59:02 +0000 | [diff] [blame] | 1113 | VG_(close)(fd); |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
| 1117 | VG_(close)(fd); |
| 1118 | |
| 1119 | if (calc_gnu_debuglink_crc32(0, (UChar*)addr, *size) != crc) { |
nethercote | e567e70 | 2004-07-10 17:49:17 +0000 | [diff] [blame] | 1120 | int res = VG_(munmap)((void*)addr, *size); |
| 1121 | vg_assert(0 == res); |
thughes | c035bd9 | 2004-06-13 09:59:02 +0000 | [diff] [blame] | 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | return addr; |
| 1126 | } |
| 1127 | |
| 1128 | /* |
nethercote | b1e1ad4 | 2004-08-03 23:44:12 +0000 | [diff] [blame] | 1129 | * Try to find a separate debug file for a given object file. |
thughes | c035bd9 | 2004-06-13 09:59:02 +0000 | [diff] [blame] | 1130 | */ |
| 1131 | static |
| 1132 | Addr find_debug_file( Char* objpath, Char* debugname, UInt crc, UInt* size ) |
| 1133 | { |
| 1134 | Char *objdir = VG_(strdup)(objpath); |
| 1135 | Char *objdirptr; |
| 1136 | Char *debugpath; |
| 1137 | Addr addr = 0; |
| 1138 | |
| 1139 | if ((objdirptr = VG_(strrchr)(objdir, '/')) != NULL) |
| 1140 | *objdirptr = '\0'; |
| 1141 | |
| 1142 | debugpath = VG_(malloc)(VG_(strlen)(objdir) + VG_(strlen)(debugname) + 16); |
| 1143 | |
| 1144 | VG_(sprintf)(debugpath, "%s/%s", objdir, debugname); |
| 1145 | |
| 1146 | if ((addr = open_debug_file(debugpath, crc, size)) == 0) { |
| 1147 | VG_(sprintf)(debugpath, "%s/.debug/%s", objdir, debugname); |
| 1148 | if ((addr = open_debug_file(debugpath, crc, size)) == 0) { |
| 1149 | VG_(sprintf)(debugpath, "/usr/lib/debug%s/%s", objdir, debugname); |
| 1150 | addr = open_debug_file(debugpath, crc, size); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | VG_(free)(debugpath); |
| 1155 | VG_(free)(objdir); |
| 1156 | |
| 1157 | return addr; |
| 1158 | } |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1159 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1160 | /* Read the symbols from the object/exe specified by the SegInfo into |
| 1161 | the tables within the supplied SegInfo. */ |
| 1162 | static |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1163 | Bool vg_read_lib_symbols ( SegInfo* si ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1164 | { |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1165 | Bool res; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1166 | Elf32_Ehdr* ehdr; /* The ELF header */ |
| 1167 | Elf32_Shdr* shdr; /* The section table */ |
| 1168 | UChar* sh_strtab; /* The section table's string table */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1169 | Int fd; |
| 1170 | Int i; |
| 1171 | Bool ok; |
| 1172 | Addr oimage; |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 1173 | UInt n_oimage; |
thughes | c035bd9 | 2004-06-13 09:59:02 +0000 | [diff] [blame] | 1174 | Addr dimage = 0; |
| 1175 | UInt n_dimage = 0; |
sewardj | b358620 | 2002-05-09 17:38:13 +0000 | [diff] [blame] | 1176 | struct vki_stat stat_buf; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1177 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1178 | oimage = (Addr)NULL; |
| 1179 | if (VG_(clo_verbosity) > 1) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1180 | VG_(message)(Vg_UserMsg, "Reading syms from %s (%p)", si->filename, si->start ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1181 | |
| 1182 | /* mmap the object image aboard, so that we can read symbols and |
| 1183 | line number info out of it. It will be munmapped immediately |
| 1184 | thereafter; it is only aboard transiently. */ |
| 1185 | |
sewardj | b358620 | 2002-05-09 17:38:13 +0000 | [diff] [blame] | 1186 | i = VG_(stat)(si->filename, &stat_buf); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1187 | if (i != 0) { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1188 | VG_(symerr)("Can't stat .so/.exe (to determine its size)?!"); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1189 | return False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1190 | } |
| 1191 | n_oimage = stat_buf.st_size; |
| 1192 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1193 | fd = VG_(open)(si->filename, VKI_O_RDONLY, 0); |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1194 | if (fd < 0) { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1195 | VG_(symerr)("Can't open .so/.exe to read symbols?!"); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1196 | return False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
sewardj | b358620 | 2002-05-09 17:38:13 +0000 | [diff] [blame] | 1199 | oimage = (Addr)VG_(mmap)( NULL, n_oimage, |
nethercote | b4250ae | 2004-07-10 16:50:09 +0000 | [diff] [blame] | 1200 | VKI_PROT_READ, VKI_MAP_PRIVATE|VKI_MAP_NOSYMS, |
| 1201 | 0, fd, 0 ); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1202 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1203 | VG_(close)(fd); |
| 1204 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1205 | if (oimage == ((Addr)(-1))) { |
| 1206 | VG_(message)(Vg_UserMsg, |
nethercote | 132be6f | 2004-06-27 12:30:15 +0000 | [diff] [blame] | 1207 | "warning: mmap failed on %s", si->filename ); |
| 1208 | VG_(message)(Vg_UserMsg, |
| 1209 | " no symbols or debug info loaded" ); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1210 | return False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1213 | /* Ok, the object image is safely in oimage[0 .. n_oimage-1]. |
| 1214 | Now verify that it is a valid ELF .so or executable image. |
| 1215 | */ |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1216 | res = False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1217 | ok = (n_oimage >= sizeof(Elf32_Ehdr)); |
| 1218 | ehdr = (Elf32_Ehdr*)oimage; |
| 1219 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1220 | if (ok) |
| 1221 | ok &= VG_(is_object_file)(ehdr); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1222 | |
| 1223 | if (!ok) { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1224 | VG_(symerr)("Invalid ELF header, or missing stringtab/sectiontab."); |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1225 | goto out; |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | /* Walk the LOAD headers in the phdr and update the SegInfo to |
| 1229 | include them all, so that this segment also contains data and |
| 1230 | bss memory. Also computes correct symbol offset value for this |
| 1231 | ELF file. */ |
| 1232 | if (ehdr->e_phoff + ehdr->e_phnum*sizeof(Elf32_Phdr) > n_oimage) { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1233 | VG_(symerr)("ELF program header is beyond image end?!"); |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1234 | goto out; |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1235 | } |
| 1236 | { |
| 1237 | Bool offset_set = False; |
| 1238 | Elf32_Addr prev_addr = 0; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1239 | Addr baseaddr = 0; |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1240 | |
| 1241 | si->offset = 0; |
| 1242 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1243 | for (i = 0; i < ehdr->e_phnum; i++) { |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1244 | Elf32_Phdr *o_phdr; |
| 1245 | Elf32_Addr mapped, mapped_end; |
| 1246 | |
| 1247 | o_phdr = &((Elf32_Phdr *)(oimage + ehdr->e_phoff))[i]; |
| 1248 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1249 | if (o_phdr->p_type == PT_DYNAMIC && si->soname == NULL) { |
| 1250 | const Elf32_Dyn *dyn = (const Elf32_Dyn *)(oimage + o_phdr->p_offset); |
| 1251 | Int stroff = -1; |
| 1252 | Char *strtab = NULL; |
| 1253 | Int j; |
| 1254 | |
| 1255 | for(j = 0; dyn[j].d_tag != DT_NULL; j++) { |
| 1256 | switch(dyn[j].d_tag) { |
| 1257 | case DT_SONAME: |
| 1258 | stroff = dyn[j].d_un.d_val; |
| 1259 | break; |
| 1260 | |
| 1261 | case DT_STRTAB: |
| 1262 | strtab = (Char *)oimage + dyn[j].d_un.d_ptr - baseaddr; |
| 1263 | break; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | if (stroff != -1 && strtab != 0) { |
| 1268 | TRACE_SYMTAB("soname=%s\n", strtab+stroff); |
| 1269 | si->soname = VG_(arena_strdup)(VG_AR_SYMTAB, strtab+stroff); |
| 1270 | } |
| 1271 | } |
| 1272 | |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1273 | if (o_phdr->p_type != PT_LOAD) |
| 1274 | continue; |
| 1275 | |
| 1276 | if (!offset_set) { |
| 1277 | offset_set = True; |
| 1278 | si->offset = si->start - o_phdr->p_vaddr; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1279 | baseaddr = o_phdr->p_vaddr; |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | if (o_phdr->p_vaddr < prev_addr) { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1283 | VG_(symerr)("ELF Phdrs are out of order!?"); |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1284 | goto out; |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1285 | } |
| 1286 | prev_addr = o_phdr->p_vaddr; |
| 1287 | |
| 1288 | mapped = o_phdr->p_vaddr + si->offset; |
| 1289 | mapped_end = mapped + o_phdr->p_memsz; |
| 1290 | |
| 1291 | if (si->data_start == 0 && |
| 1292 | (o_phdr->p_flags & (PF_R|PF_W|PF_X)) == (PF_R|PF_W)) { |
| 1293 | si->data_start = mapped; |
| 1294 | si->data_size = o_phdr->p_filesz; |
| 1295 | si->bss_start = mapped + o_phdr->p_filesz; |
| 1296 | if (o_phdr->p_memsz > o_phdr->p_filesz) |
| 1297 | si->bss_size = o_phdr->p_memsz - o_phdr->p_filesz; |
| 1298 | else |
| 1299 | si->bss_size = 0; |
| 1300 | } |
| 1301 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1302 | mapped = mapped & ~(VKI_PAGE_SIZE-1); |
| 1303 | mapped_end = (mapped_end + VKI_PAGE_SIZE - 1) & ~(VKI_PAGE_SIZE-1); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1304 | |
| 1305 | if (VG_(needs).data_syms && |
| 1306 | (mapped >= si->start && mapped <= (si->start+si->size)) && |
| 1307 | (mapped_end > (si->start+si->size))) { |
| 1308 | UInt newsz = mapped_end - si->start; |
| 1309 | if (newsz > si->size) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1310 | Segment *seg; |
| 1311 | |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1312 | if (0) |
| 1313 | VG_(printf)("extending mapping %p..%p %d -> ..%p %d\n", |
| 1314 | si->start, si->start+si->size, si->size, |
| 1315 | si->start+newsz, newsz); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1316 | |
| 1317 | for(seg = VG_(find_segment)(si->start); |
| 1318 | seg != NULL && VG_(seg_overlaps)(seg, si->start, si->size); |
| 1319 | seg = VG_(next_segment)(seg)) { |
| 1320 | if (seg->symtab == si) |
| 1321 | continue; |
| 1322 | |
| 1323 | if (seg->symtab != NULL) |
nethercote | 8991d5a | 2004-11-03 17:07:46 +0000 | [diff] [blame] | 1324 | VG_(symtab_decref)(seg->symtab, seg->addr); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1325 | |
| 1326 | VG_(symtab_incref)(si); |
| 1327 | seg->symtab = si; |
| 1328 | |
| 1329 | if (0) |
| 1330 | VG_(printf)("adding symtab %p (%p-%p) to segment %p (%p-%p)\n", |
| 1331 | si, si->start, si->start+newsz, |
| 1332 | seg, seg->addr, seg->addr+seg->len); |
| 1333 | } |
| 1334 | |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1335 | si->size = newsz; |
| 1336 | } |
| 1337 | } |
| 1338 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1341 | TRACE_SYMTAB("shoff = %d, shnum = %d, size = %d, n_vg_oimage = %d\n", |
| 1342 | ehdr->e_shoff, ehdr->e_shnum, sizeof(Elf32_Shdr), n_oimage ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1343 | |
| 1344 | if (ehdr->e_shoff + ehdr->e_shnum*sizeof(Elf32_Shdr) > n_oimage) { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1345 | VG_(symerr)("ELF section header is beyond image end?!"); |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1346 | goto out; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | shdr = (Elf32_Shdr*)(oimage + ehdr->e_shoff); |
| 1350 | sh_strtab = (UChar*)(oimage + shdr[ehdr->e_shstrndx].sh_offset); |
| 1351 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1352 | /* Find interesting sections, read the symbol table(s), read any debug |
| 1353 | information */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1354 | { |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1355 | /* Pointers to start of sections */ |
| 1356 | UChar* o_strtab = NULL; /* .strtab */ |
| 1357 | Elf32_Sym* o_symtab = NULL; /* .symtab */ |
| 1358 | UChar* o_dynstr = NULL; /* .dynstr */ |
| 1359 | Elf32_Sym* o_dynsym = NULL; /* .dynsym */ |
thughes | c035bd9 | 2004-06-13 09:59:02 +0000 | [diff] [blame] | 1360 | Char* debuglink = NULL; /* .gnu_debuglink */ |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1361 | UChar* stab = NULL; /* .stab (stabs) */ |
| 1362 | UChar* stabstr = NULL; /* .stabstr (stabs) */ |
| 1363 | UChar* debug_line = NULL; /* .debug_line (dwarf2) */ |
jseward | 8b3131a | 2003-12-13 23:16:26 +0000 | [diff] [blame] | 1364 | UChar* dwarf1d = NULL; /* .debug (dwarf1) */ |
| 1365 | UChar* dwarf1l = NULL; /* .line (dwarf1) */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1366 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1367 | /* Section sizes, in bytes */ |
| 1368 | UInt o_strtab_sz = 0; |
| 1369 | UInt o_symtab_sz = 0; |
| 1370 | UInt o_dynstr_sz = 0; |
| 1371 | UInt o_dynsym_sz = 0; |
thughes | c035bd9 | 2004-06-13 09:59:02 +0000 | [diff] [blame] | 1372 | UInt debuglink_sz = 0; |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1373 | UInt stab_sz = 0; |
| 1374 | UInt stabstr_sz = 0; |
| 1375 | UInt debug_line_sz = 0; |
jseward | 8b3131a | 2003-12-13 23:16:26 +0000 | [diff] [blame] | 1376 | UInt dwarf1d_sz = 0; |
| 1377 | UInt dwarf1l_sz = 0; |
| 1378 | |
| 1379 | Bool has_debuginfo = False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1380 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1381 | /* Find all interesting sections */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1382 | for (i = 0; i < ehdr->e_shnum; i++) { |
jseward | 8b3131a | 2003-12-13 23:16:26 +0000 | [diff] [blame] | 1383 | # define FIND(sec_name, sec_data, sec_size, in_exec, type) \ |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1384 | if (0 == VG_(strcmp)(sec_name, sh_strtab + shdr[i].sh_name)) { \ |
| 1385 | if (0 != sec_data) \ |
| 1386 | VG_(core_panic)("repeated section!\n"); \ |
nethercote | fd1ea4c | 2003-12-01 11:54:09 +0000 | [diff] [blame] | 1387 | if (in_exec) \ |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1388 | sec_data = (type)(si->offset + shdr[i].sh_addr); \ |
nethercote | fd1ea4c | 2003-12-01 11:54:09 +0000 | [diff] [blame] | 1389 | else \ |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1390 | sec_data = (type)(oimage + shdr[i].sh_offset); \ |
nethercote | fd1ea4c | 2003-12-01 11:54:09 +0000 | [diff] [blame] | 1391 | sec_size = shdr[i].sh_size; \ |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1392 | TRACE_SYMTAB( "%18s: %p .. %p\n", \ |
| 1393 | sec_name, sec_data, sec_data + sec_size - 1); \ |
nethercote | fd1ea4c | 2003-12-01 11:54:09 +0000 | [diff] [blame] | 1394 | if ( shdr[i].sh_offset + sec_size > n_oimage ) { \ |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1395 | VG_(symerr)(" section beyond image end?!"); \ |
| 1396 | goto out; \ |
| 1397 | } \ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1400 | /* Nb: must find where .got and .plt sections will be in the |
| 1401 | * executable image, not in the object image transiently loaded. */ |
| 1402 | FIND(".dynsym", o_dynsym, o_dynsym_sz, 0, Elf32_Sym*) |
| 1403 | else FIND(".dynstr", o_dynstr, o_dynstr_sz, 0, UChar*) |
| 1404 | else FIND(".symtab", o_symtab, o_symtab_sz, 0, Elf32_Sym*) |
| 1405 | else FIND(".strtab", o_strtab, o_strtab_sz, 0, UChar*) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1406 | |
thughes | c035bd9 | 2004-06-13 09:59:02 +0000 | [diff] [blame] | 1407 | else FIND(".gnu_debuglink", debuglink, debuglink_sz, 0, Char*) |
| 1408 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1409 | else FIND(".stab", stab, stab_sz, 0, UChar*) |
| 1410 | else FIND(".stabstr", stabstr, stabstr_sz, 0, UChar*) |
| 1411 | else FIND(".debug_line", debug_line, debug_line_sz, 0, UChar*) |
jseward | 8b3131a | 2003-12-13 23:16:26 +0000 | [diff] [blame] | 1412 | else FIND(".debug", dwarf1d, dwarf1d_sz, 0, UChar*) |
| 1413 | else FIND(".line", dwarf1l, dwarf1l_sz, 0, UChar*) |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1414 | |
| 1415 | else FIND(".got", si->got_start, si->got_size, 1, Addr) |
| 1416 | else FIND(".plt", si->plt_start, si->plt_size, 1, Addr) |
| 1417 | |
jseward | 8b3131a | 2003-12-13 23:16:26 +0000 | [diff] [blame] | 1418 | # undef FIND |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1419 | |
| 1420 | /* Check some sizes */ |
| 1421 | vg_assert((o_dynsym_sz % sizeof(Elf32_Sym)) == 0); |
| 1422 | vg_assert((o_symtab_sz % sizeof(Elf32_Sym)) == 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 1425 | read_symtab(si, "symbol table", False, |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1426 | o_symtab, o_symtab_sz, |
| 1427 | o_strtab, o_strtab_sz); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1428 | |
rjwalsh | e4e779d | 2004-04-16 23:02:29 +0000 | [diff] [blame] | 1429 | read_symtab(si, "dynamic symbol table", True, |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1430 | o_dynsym, o_dynsym_sz, |
| 1431 | o_dynstr, o_dynstr_sz); |
sewardj | 9b81a42 | 2003-04-26 21:42:09 +0000 | [diff] [blame] | 1432 | |
thughes | c035bd9 | 2004-06-13 09:59:02 +0000 | [diff] [blame] | 1433 | /* Did we find a debuglink section? */ |
| 1434 | if (debuglink != NULL) { |
| 1435 | UInt crc_offset = (VG_(strlen)(debuglink) + 4) & ~3; |
| 1436 | UInt crc; |
| 1437 | |
| 1438 | vg_assert(crc_offset + sizeof(UInt) <= debuglink_sz); |
| 1439 | |
| 1440 | /* Extract the CRC from the debuglink section */ |
| 1441 | crc = *(UInt *)(debuglink + crc_offset); |
| 1442 | |
| 1443 | /* See if we can find a matching debug file */ |
| 1444 | if ((dimage = find_debug_file(si->filename, debuglink, crc, &n_dimage)) != 0) { |
| 1445 | ehdr = (Elf32_Ehdr*)dimage; |
| 1446 | |
| 1447 | if (n_dimage >= sizeof(Elf32_Ehdr) && VG_(is_object_file)(ehdr)) |
| 1448 | { |
| 1449 | shdr = (Elf32_Shdr*)(dimage + ehdr->e_shoff); |
| 1450 | sh_strtab = (UChar*)(dimage + shdr[ehdr->e_shstrndx].sh_offset); |
| 1451 | |
| 1452 | /* Find all interesting sections */ |
| 1453 | for (i = 0; i < ehdr->e_shnum; i++) { |
| 1454 | # define FIND(sec_name, sec_data, sec_size, type) \ |
| 1455 | if (0 == VG_(strcmp)(sec_name, sh_strtab + shdr[i].sh_name)) { \ |
| 1456 | if (0 != sec_data) \ |
| 1457 | VG_(core_panic)("repeated section!\n"); \ |
| 1458 | sec_data = (type)(dimage + shdr[i].sh_offset); \ |
| 1459 | sec_size = shdr[i].sh_size; \ |
| 1460 | TRACE_SYMTAB( "%18s: %p .. %p\n", \ |
| 1461 | sec_name, sec_data, sec_data + sec_size - 1); \ |
| 1462 | if ( shdr[i].sh_offset + sec_size > n_dimage ) { \ |
| 1463 | VG_(symerr)(" section beyond image end?!"); \ |
| 1464 | goto out; \ |
| 1465 | } \ |
| 1466 | } |
| 1467 | |
| 1468 | /* Nb: must find where .got and .plt sections will be in the |
| 1469 | * executable image, not in the object image transiently loaded. */ |
| 1470 | FIND(".stab", stab, stab_sz, UChar*) |
| 1471 | else FIND(".stabstr", stabstr, stabstr_sz, UChar*) |
| 1472 | else FIND(".debug_line", debug_line, debug_line_sz, UChar*) |
| 1473 | else FIND(".debug", dwarf1d, dwarf1d_sz, UChar*) |
| 1474 | else FIND(".line", dwarf1l, dwarf1l_sz, UChar*) |
| 1475 | |
| 1476 | # undef FIND |
| 1477 | |
| 1478 | /* Check some sizes */ |
| 1479 | vg_assert((o_dynsym_sz % sizeof(Elf32_Sym)) == 0); |
| 1480 | vg_assert((o_symtab_sz % sizeof(Elf32_Sym)) == 0); |
| 1481 | } |
| 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1486 | /* Read the stabs and/or dwarf2 debug information, if any. */ |
| 1487 | if (stab != NULL && stabstr != NULL) { |
jseward | 8b3131a | 2003-12-13 23:16:26 +0000 | [diff] [blame] | 1488 | has_debuginfo = True; |
| 1489 | VG_(read_debuginfo_stabs) ( si, stab, stab_sz, |
| 1490 | stabstr, stabstr_sz ); |
| 1491 | } |
| 1492 | if (debug_line) { |
| 1493 | has_debuginfo = True; |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1494 | VG_(read_debuginfo_dwarf2) ( si, debug_line, debug_line_sz ); |
jseward | 8b3131a | 2003-12-13 23:16:26 +0000 | [diff] [blame] | 1495 | } |
| 1496 | if (dwarf1d && dwarf1l) { |
| 1497 | has_debuginfo = True; |
| 1498 | VG_(read_debuginfo_dwarf1) ( si, dwarf1d, dwarf1d_sz, |
| 1499 | dwarf1l, dwarf1l_sz ); |
| 1500 | } |
| 1501 | if (!has_debuginfo) { |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1502 | VG_(symerr)(" object doesn't have any debug info"); |
| 1503 | goto out; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1504 | } |
| 1505 | } |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1506 | res = True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1507 | |
nethercote | e567e70 | 2004-07-10 17:49:17 +0000 | [diff] [blame] | 1508 | out: { |
| 1509 | Int m_res; |
thughes | c035bd9 | 2004-06-13 09:59:02 +0000 | [diff] [blame] | 1510 | /* Last, but not least, heave the image(s) back overboard. */ |
nethercote | e567e70 | 2004-07-10 17:49:17 +0000 | [diff] [blame] | 1511 | if (dimage) { |
| 1512 | m_res = VG_(munmap) ( (void*)dimage, n_dimage ); |
| 1513 | vg_assert(0 == m_res); |
| 1514 | } |
| 1515 | m_res = VG_(munmap) ( (void*)oimage, n_oimage ); |
| 1516 | vg_assert(0 == m_res); |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1517 | return res; |
nethercote | e567e70 | 2004-07-10 17:49:17 +0000 | [diff] [blame] | 1518 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1521 | /*------------------------------------------------------------*/ |
| 1522 | /*--- Main entry point for symbols table reading. ---*/ |
| 1523 | /*------------------------------------------------------------*/ |
| 1524 | |
| 1525 | /* The root structure for the entire symbol table system. It is a |
| 1526 | linked list of SegInfos. Note that this entire mechanism assumes |
| 1527 | that what we read from /proc/self/maps doesn't contain overlapping |
| 1528 | address ranges, and as a result the SegInfos in this list describe |
| 1529 | disjoint address ranges. |
| 1530 | */ |
| 1531 | static SegInfo* segInfo = NULL; |
| 1532 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1533 | static void resolve_seg_redirs(SegInfo *si); |
| 1534 | |
| 1535 | SegInfo *VG_(read_seg_symbols) ( Segment *seg ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1536 | { |
| 1537 | SegInfo* si; |
| 1538 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1539 | vg_assert(seg->symtab == NULL); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1540 | |
njn | fa1016e | 2003-09-25 17:54:11 +0000 | [diff] [blame] | 1541 | VGP_PUSHCC(VgpReadSyms); |
| 1542 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1543 | /* Get the record initialised right. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1544 | si = VG_(arena_malloc)(VG_AR_SYMTAB, sizeof(SegInfo)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1545 | |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1546 | VG_(memset)(si, 0, sizeof(*si)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1547 | si->start = seg->addr; |
| 1548 | si->size = seg->len; |
| 1549 | si->foffset = seg->offset; |
fitzhardinge | 1a4adf0 | 2003-12-22 10:42:59 +0000 | [diff] [blame] | 1550 | si->filename = VG_(arena_strdup)(VG_AR_SYMTAB, seg->filename); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1551 | |
| 1552 | si->ref = 1; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1553 | |
| 1554 | si->symtab = NULL; |
| 1555 | si->symtab_size = si->symtab_used = 0; |
| 1556 | si->loctab = NULL; |
| 1557 | si->loctab_size = si->loctab_used = 0; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1558 | si->strchunks = NULL; |
| 1559 | si->scopetab = NULL; |
| 1560 | si->scopetab_size = si->scopetab_used = 0; |
| 1561 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1562 | si->seg = seg; |
| 1563 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1564 | si->stab_typetab = NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1565 | |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1566 | si->plt_start = si->plt_size = 0; |
| 1567 | si->got_start = si->got_size = 0; |
sewardj | 8a77ffb | 2003-07-10 23:31:27 +0000 | [diff] [blame] | 1568 | si->data_start = si->data_size = 0; |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1569 | si->bss_start = si->bss_size = 0; |
sewardj | 8a77ffb | 2003-07-10 23:31:27 +0000 | [diff] [blame] | 1570 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1571 | /* And actually fill it up. */ |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1572 | if (!vg_read_lib_symbols ( si ) && 0) { |
| 1573 | /* XXX this interacts badly with the prevN optimization in |
| 1574 | addStr(). Since this frees the si, the si pointer value can |
| 1575 | be recycled, which confuses the curr_si == si test. For now, |
| 1576 | this code is disabled, and everything is included in the |
| 1577 | segment list, even if it is a bad ELF file. Ironically, |
| 1578 | running this under valgrind itself hides the problem, because |
| 1579 | it doesn't recycle pointers... */ |
| 1580 | freeSegInfo( si ); |
| 1581 | } else { |
| 1582 | si->next = segInfo; |
| 1583 | segInfo = si; |
| 1584 | |
| 1585 | canonicaliseSymtab ( si ); |
| 1586 | canonicaliseLoctab ( si ); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1587 | canonicaliseScopetab ( si ); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1588 | |
| 1589 | /* do redirects */ |
| 1590 | resolve_seg_redirs( si ); |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 1591 | } |
njn | fa1016e | 2003-09-25 17:54:11 +0000 | [diff] [blame] | 1592 | VGP_POPCC(VgpReadSyms); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1593 | |
| 1594 | return si; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1598 | /* When an munmap() call happens, check to see whether it corresponds |
| 1599 | to a segment for a .so, and if so discard the relevant SegInfo. |
| 1600 | This might not be a very clever idea from the point of view of |
| 1601 | accuracy of error messages, but we need to do it in order to |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1602 | maintain the no-overlapping invariant. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1603 | */ |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 1604 | static void unload_symbols ( Addr start, SizeT length ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1605 | { |
| 1606 | SegInfo *prev, *curr; |
| 1607 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1608 | prev = NULL; |
| 1609 | curr = segInfo; |
| 1610 | while (True) { |
| 1611 | if (curr == NULL) break; |
| 1612 | if (start == curr->start) break; |
| 1613 | prev = curr; |
| 1614 | curr = curr->next; |
| 1615 | } |
njn | fa1016e | 2003-09-25 17:54:11 +0000 | [diff] [blame] | 1616 | if (curr == NULL) { |
| 1617 | VGP_POPCC(VgpReadSyms); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1618 | return; |
njn | fa1016e | 2003-09-25 17:54:11 +0000 | [diff] [blame] | 1619 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1620 | |
mueller | 75c0ed6 | 2003-11-19 00:47:00 +0000 | [diff] [blame] | 1621 | if (VG_(clo_verbosity) > 1) |
| 1622 | VG_(message)(Vg_UserMsg, |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1623 | "discard syms at %p-%p in %s due to munmap()", |
| 1624 | start, start+length, curr->filename ? curr->filename : (Char *)"???"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1625 | |
| 1626 | vg_assert(prev == NULL || prev->next == curr); |
| 1627 | |
| 1628 | if (prev == NULL) { |
| 1629 | segInfo = curr->next; |
| 1630 | } else { |
| 1631 | prev->next = curr->next; |
| 1632 | } |
| 1633 | |
| 1634 | freeSegInfo(curr); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1635 | return; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
nethercote | 8991d5a | 2004-11-03 17:07:46 +0000 | [diff] [blame] | 1638 | void VG_(symtab_decref)(SegInfo *si, Addr start) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1639 | { |
| 1640 | vg_assert(si->ref >= 1); |
| 1641 | if (--si->ref == 0) |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 1642 | unload_symbols(si->start, si->size); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | void VG_(symtab_incref)(SegInfo *si) |
| 1646 | { |
| 1647 | vg_assert(si->ref > 0); |
| 1648 | si->ref++; |
| 1649 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1650 | |
| 1651 | /*------------------------------------------------------------*/ |
| 1652 | /*--- Use of symbol table & location info to create ---*/ |
| 1653 | /*--- plausible-looking stack dumps. ---*/ |
| 1654 | /*------------------------------------------------------------*/ |
| 1655 | |
| 1656 | /* Find a symbol-table index containing the specified pointer, or -1 |
| 1657 | if not found. Binary search. */ |
| 1658 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1659 | static Int search_one_symtab ( SegInfo* si, Addr ptr, |
| 1660 | Bool match_anywhere_in_fun ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1661 | { |
| 1662 | Addr a_mid_lo, a_mid_hi; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1663 | Int mid, size, |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1664 | lo = 0, |
| 1665 | hi = si->symtab_used-1; |
| 1666 | while (True) { |
| 1667 | /* current unsearched space is from lo to hi, inclusive. */ |
| 1668 | if (lo > hi) return -1; /* not found */ |
| 1669 | mid = (lo + hi) / 2; |
| 1670 | a_mid_lo = si->symtab[mid].addr; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1671 | size = ( match_anywhere_in_fun |
| 1672 | ? si->symtab[mid].size |
| 1673 | : 1); |
| 1674 | a_mid_hi = ((Addr)si->symtab[mid].addr) + size - 1; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1675 | |
| 1676 | if (ptr < a_mid_lo) { hi = mid-1; continue; } |
| 1677 | if (ptr > a_mid_hi) { lo = mid+1; continue; } |
| 1678 | vg_assert(ptr >= a_mid_lo && ptr <= a_mid_hi); |
| 1679 | return mid; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 1684 | /* SLOW (Linear search). Try and map a symbol name to an address. |
| 1685 | Since this is searching in the direction opposite to which the |
| 1686 | table is designed we have no option but to do a complete linear |
| 1687 | scan of the table. Returns NULL if not found. */ |
| 1688 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1689 | static Addr reverse_search_one_symtab ( const SegInfo* si, |
| 1690 | const Char* name ) |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 1691 | { |
| 1692 | UInt i; |
| 1693 | for (i = 0; i < si->symtab_used; i++) { |
sewardj | c8bf5fe | 2003-07-13 00:53:22 +0000 | [diff] [blame] | 1694 | if (0) |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1695 | VG_(printf)("%p %s\n", si->symtab[i].addr, si->symtab[i].name); |
| 1696 | if (0 == VG_(strcmp)(name, si->symtab[i].name)) |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 1697 | return si->symtab[i].addr; |
| 1698 | } |
| 1699 | return (Addr)NULL; |
| 1700 | } |
| 1701 | |
| 1702 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1703 | /* Search all symtabs that we know about to locate ptr. If found, set |
| 1704 | *psi to the relevant SegInfo, and *symno to the symtab entry number |
| 1705 | within that. If not found, *psi is set to NULL. */ |
| 1706 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1707 | static void search_all_symtabs ( Addr ptr, /*OUT*/SegInfo** psi, |
| 1708 | /*OUT*/Int* symno, |
| 1709 | Bool match_anywhere_in_fun ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1710 | { |
| 1711 | Int sno; |
| 1712 | SegInfo* si; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1713 | Segment *s; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1714 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1715 | VGP_PUSHCC(VgpSearchSyms); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1716 | |
| 1717 | s = VG_(find_segment)(ptr); |
| 1718 | |
| 1719 | if (s == NULL || !VG_(seg_overlaps)(s, ptr, 0) || s->symtab == NULL) |
| 1720 | goto not_found; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1721 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1722 | si = s->symtab; |
| 1723 | |
| 1724 | sno = search_one_symtab ( si, ptr, match_anywhere_in_fun ); |
| 1725 | if (sno == -1) goto not_found; |
| 1726 | |
| 1727 | *symno = sno; |
| 1728 | *psi = si; |
| 1729 | VGP_POPCC(VgpSearchSyms); |
| 1730 | return; |
| 1731 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1732 | not_found: |
| 1733 | *psi = NULL; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1734 | VGP_POPCC(VgpSearchSyms); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | |
| 1738 | /* Find a location-table index containing the specified pointer, or -1 |
| 1739 | if not found. Binary search. */ |
| 1740 | |
| 1741 | static Int search_one_loctab ( SegInfo* si, Addr ptr ) |
| 1742 | { |
| 1743 | Addr a_mid_lo, a_mid_hi; |
| 1744 | Int mid, |
| 1745 | lo = 0, |
| 1746 | hi = si->loctab_used-1; |
| 1747 | while (True) { |
| 1748 | /* current unsearched space is from lo to hi, inclusive. */ |
| 1749 | if (lo > hi) return -1; /* not found */ |
| 1750 | mid = (lo + hi) / 2; |
| 1751 | a_mid_lo = si->loctab[mid].addr; |
| 1752 | a_mid_hi = ((Addr)si->loctab[mid].addr) + si->loctab[mid].size - 1; |
| 1753 | |
| 1754 | if (ptr < a_mid_lo) { hi = mid-1; continue; } |
| 1755 | if (ptr > a_mid_hi) { lo = mid+1; continue; } |
| 1756 | vg_assert(ptr >= a_mid_lo && ptr <= a_mid_hi); |
| 1757 | return mid; |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | |
| 1762 | /* Search all loctabs that we know about to locate ptr. If found, set |
| 1763 | *psi to the relevant SegInfo, and *locno to the loctab entry number |
| 1764 | within that. If not found, *psi is set to NULL. |
| 1765 | */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1766 | static void search_all_loctabs ( Addr ptr, /*OUT*/SegInfo** psi, |
| 1767 | /*OUT*/Int* locno ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1768 | { |
| 1769 | Int lno; |
| 1770 | SegInfo* si; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1771 | |
| 1772 | VGP_PUSHCC(VgpSearchSyms); |
| 1773 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1774 | for (si = segInfo; si != NULL; si = si->next) { |
| 1775 | if (si->start <= ptr && ptr < si->start+si->size) { |
| 1776 | lno = search_one_loctab ( si, ptr ); |
| 1777 | if (lno == -1) goto not_found; |
| 1778 | *locno = lno; |
| 1779 | *psi = si; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1780 | VGP_POPCC(VgpSearchSyms); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1781 | return; |
| 1782 | } |
| 1783 | } |
| 1784 | not_found: |
| 1785 | *psi = NULL; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1786 | VGP_POPCC(VgpSearchSyms); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1790 | /* Find a scope-table index containing the specified pointer, or -1 |
| 1791 | if not found. Binary search. */ |
| 1792 | |
| 1793 | static Int search_one_scopetab ( SegInfo* si, Addr ptr ) |
| 1794 | { |
| 1795 | Addr a_mid_lo, a_mid_hi; |
| 1796 | Int mid, |
| 1797 | lo = 0, |
| 1798 | hi = si->scopetab_used-1; |
| 1799 | while (True) { |
| 1800 | /* current unsearched space is from lo to hi, inclusive. */ |
| 1801 | if (lo > hi) return -1; /* not found */ |
| 1802 | mid = (lo + hi) / 2; |
| 1803 | a_mid_lo = si->scopetab[mid].addr; |
| 1804 | a_mid_hi = ((Addr)si->scopetab[mid].addr) + si->scopetab[mid].size - 1; |
| 1805 | |
| 1806 | if (ptr < a_mid_lo) { hi = mid-1; continue; } |
| 1807 | if (ptr > a_mid_hi) { lo = mid+1; continue; } |
| 1808 | vg_assert(ptr >= a_mid_lo && ptr <= a_mid_hi); |
| 1809 | return mid; |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | |
| 1814 | /* Search all scopetabs that we know about to locate ptr. If found, set |
| 1815 | *psi to the relevant SegInfo, and *locno to the scopetab entry number |
| 1816 | within that. If not found, *psi is set to NULL. |
| 1817 | */ |
| 1818 | static void search_all_scopetabs ( Addr ptr, |
| 1819 | /*OUT*/SegInfo** psi, |
| 1820 | /*OUT*/Int* scopeno ) |
| 1821 | { |
| 1822 | Int scno; |
| 1823 | SegInfo* si; |
| 1824 | |
| 1825 | VGP_PUSHCC(VgpSearchSyms); |
| 1826 | |
| 1827 | for (si = segInfo; si != NULL; si = si->next) { |
| 1828 | if (si->start <= ptr && ptr < si->start+si->size) { |
| 1829 | scno = search_one_scopetab ( si, ptr ); |
| 1830 | if (scno == -1) goto not_found; |
| 1831 | *scopeno = scno; |
| 1832 | *psi = si; |
| 1833 | VGP_POPCC(VgpSearchSyms); |
| 1834 | return; |
| 1835 | } |
| 1836 | } |
| 1837 | not_found: |
| 1838 | *psi = NULL; |
| 1839 | VGP_POPCC(VgpSearchSyms); |
| 1840 | } |
| 1841 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1842 | /* The whole point of this whole big deal: map a code address to a |
| 1843 | plausible symbol name. Returns False if no idea; otherwise True. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1844 | Caller supplies buf and nbuf. If demangle is False, don't do |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1845 | demangling, regardless of vg_clo_demangle -- probably because the |
| 1846 | call has come from vg_what_fn_or_object_is_this. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1847 | static |
| 1848 | Bool get_fnname ( Bool demangle, Addr a, Char* buf, Int nbuf, |
sewardj | 1771e17 | 2002-11-13 22:06:35 +0000 | [diff] [blame] | 1849 | Bool match_anywhere_in_fun, Bool show_offset) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1850 | { |
| 1851 | SegInfo* si; |
| 1852 | Int sno; |
sewardj | 1771e17 | 2002-11-13 22:06:35 +0000 | [diff] [blame] | 1853 | Int offset; |
| 1854 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1855 | search_all_symtabs ( a, &si, &sno, match_anywhere_in_fun ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1856 | if (si == NULL) |
| 1857 | return False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1858 | if (demangle) { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1859 | VG_(demangle) ( si->symtab[sno].name, buf, nbuf ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1860 | } else { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1861 | VG_(strncpy_safely) |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1862 | ( buf, si->symtab[sno].name, nbuf ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1863 | } |
sewardj | 1771e17 | 2002-11-13 22:06:35 +0000 | [diff] [blame] | 1864 | |
| 1865 | offset = a - si->symtab[sno].addr; |
| 1866 | if (show_offset && offset != 0) { |
| 1867 | Char buf2[12]; |
| 1868 | Char* symend = buf + VG_(strlen)(buf); |
| 1869 | Char* end = buf + nbuf; |
| 1870 | Int len; |
| 1871 | |
| 1872 | len = VG_(sprintf)(buf2, "%c%d", |
| 1873 | offset < 0 ? '-' : '+', |
| 1874 | offset < 0 ? -offset : offset); |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 1875 | vg_assert(len < (Int)sizeof(buf2)); |
sewardj | 1771e17 | 2002-11-13 22:06:35 +0000 | [diff] [blame] | 1876 | |
| 1877 | if (len < (end - symend)) { |
| 1878 | Char *cp = buf2; |
| 1879 | VG_(memcpy)(symend, cp, len+1); |
| 1880 | } |
| 1881 | } |
| 1882 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1883 | return True; |
| 1884 | } |
| 1885 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 1886 | /* This is available to tools... always demangle C++ names, |
sewardj | 6e008cb | 2002-12-15 13:11:39 +0000 | [diff] [blame] | 1887 | match anywhere in function, but don't show offsets. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1888 | Bool VG_(get_fnname) ( Addr a, Char* buf, Int nbuf ) |
| 1889 | { |
| 1890 | return get_fnname ( /*demangle*/True, a, buf, nbuf, |
sewardj | 6e008cb | 2002-12-15 13:11:39 +0000 | [diff] [blame] | 1891 | /*match_anywhere_in_fun*/True, |
| 1892 | /*show offset?*/False ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1893 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1894 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 1895 | /* This is available to tools... always demangle C++ names, |
sewardj | 6e008cb | 2002-12-15 13:11:39 +0000 | [diff] [blame] | 1896 | match anywhere in function, and show offset if nonzero. */ |
| 1897 | Bool VG_(get_fnname_w_offset) ( Addr a, Char* buf, Int nbuf ) |
| 1898 | { |
| 1899 | return get_fnname ( /*demangle*/True, a, buf, nbuf, |
| 1900 | /*match_anywhere_in_fun*/True, |
| 1901 | /*show offset?*/True ); |
| 1902 | } |
| 1903 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 1904 | /* This is available to tools... always demangle C++ names, |
sewardj | 6e008cb | 2002-12-15 13:11:39 +0000 | [diff] [blame] | 1905 | only succeed if 'a' matches first instruction of function, |
| 1906 | and don't show offsets. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1907 | Bool VG_(get_fnname_if_entry) ( Addr a, Char* buf, Int nbuf ) |
| 1908 | { |
| 1909 | return get_fnname ( /*demangle*/True, a, buf, nbuf, |
sewardj | 6e008cb | 2002-12-15 13:11:39 +0000 | [diff] [blame] | 1910 | /*match_anywhere_in_fun*/False, |
| 1911 | /*show offset?*/False ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
sewardj | 6e008cb | 2002-12-15 13:11:39 +0000 | [diff] [blame] | 1914 | /* This is only available to core... don't demangle C++ names, |
| 1915 | match anywhere in function, and don't show offsets. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1916 | Bool VG_(get_fnname_nodemangle) ( Addr a, Char* buf, Int nbuf ) |
| 1917 | { |
| 1918 | return get_fnname ( /*demangle*/False, a, buf, nbuf, |
sewardj | 6e008cb | 2002-12-15 13:11:39 +0000 | [diff] [blame] | 1919 | /*match_anywhere_in_fun*/True, |
| 1920 | /*show offset?*/False ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | /* Map a code address to the name of a shared object file or the executable. |
| 1924 | Returns False if no idea; otherwise True. Doesn't require debug info. |
| 1925 | Caller supplies buf and nbuf. */ |
| 1926 | Bool VG_(get_objname) ( Addr a, Char* buf, Int nbuf ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1927 | { |
| 1928 | SegInfo* si; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1929 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1930 | for (si = segInfo; si != NULL; si = si->next) { |
| 1931 | if (si->start <= a && a < si->start+si->size) { |
| 1932 | VG_(strncpy_safely)(buf, si->filename, nbuf); |
| 1933 | return True; |
| 1934 | } |
| 1935 | } |
| 1936 | return False; |
| 1937 | } |
| 1938 | |
njn | b877d49 | 2003-01-28 20:40:57 +0000 | [diff] [blame] | 1939 | /* Map a code address to its SegInfo. Returns NULL if not found. Doesn't |
| 1940 | require debug info. */ |
| 1941 | SegInfo* VG_(get_obj) ( Addr a ) |
| 1942 | { |
| 1943 | SegInfo* si; |
| 1944 | |
njn | b877d49 | 2003-01-28 20:40:57 +0000 | [diff] [blame] | 1945 | for (si = segInfo; si != NULL; si = si->next) { |
| 1946 | if (si->start <= a && a < si->start+si->size) { |
| 1947 | return si; |
| 1948 | } |
| 1949 | } |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 1950 | return NULL; |
njn | b877d49 | 2003-01-28 20:40:57 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1953 | |
| 1954 | /* Map a code address to a filename. Returns True if successful. */ |
| 1955 | Bool VG_(get_filename)( Addr a, Char* filename, Int n_filename ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1956 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1957 | SegInfo* si; |
| 1958 | Int locno; |
| 1959 | search_all_loctabs ( a, &si, &locno ); |
| 1960 | if (si == NULL) |
| 1961 | return False; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1962 | VG_(strncpy_safely)(filename, si->loctab[locno].filename, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1963 | n_filename); |
| 1964 | return True; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1967 | /* Map a code address to a line number. Returns True if successful. */ |
| 1968 | Bool VG_(get_linenum)( Addr a, UInt* lineno ) |
| 1969 | { |
| 1970 | SegInfo* si; |
| 1971 | Int locno; |
| 1972 | search_all_loctabs ( a, &si, &locno ); |
| 1973 | if (si == NULL) |
| 1974 | return False; |
| 1975 | *lineno = si->loctab[locno].lineno; |
| 1976 | |
| 1977 | return True; |
| 1978 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1979 | |
| 1980 | /* Map a code address to a (filename, line number) pair. |
| 1981 | Returns True if successful. |
| 1982 | */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1983 | Bool VG_(get_filename_linenum)( Addr a, |
| 1984 | Char* filename, Int n_filename, |
| 1985 | UInt* lineno ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1986 | { |
| 1987 | SegInfo* si; |
| 1988 | Int locno; |
| 1989 | search_all_loctabs ( a, &si, &locno ); |
| 1990 | if (si == NULL) |
| 1991 | return False; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1992 | VG_(strncpy_safely)(filename, si->loctab[locno].filename, |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1993 | n_filename); |
| 1994 | *lineno = si->loctab[locno].lineno; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1995 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1996 | return True; |
| 1997 | } |
| 1998 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 1999 | #ifndef TEST |
| 2000 | |
| 2001 | /* return a pointer to a register (now for 5 other impossible things |
| 2002 | before breakfast) */ |
| 2003 | static UInt *regaddr(ThreadId tid, Int regno) |
| 2004 | { |
| 2005 | UInt *ret = 0; |
| 2006 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame^] | 2007 | ret = VGA_(reg_addr_from_tst)(regno, &VG_(threads)[tid].arch); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2008 | |
| 2009 | if (ret == 0) { |
| 2010 | Char file[100]; |
| 2011 | Int line; |
| 2012 | Addr eip = VG_(get_EIP)(tid); |
| 2013 | |
| 2014 | if (!VG_(get_filename_linenum)(eip, file, sizeof(file), &line)) |
| 2015 | file[0] = 0; |
| 2016 | VG_(printf)("mysterious register %d used at %p %s:%d\n", |
| 2017 | regno, eip, file, line); |
| 2018 | } |
| 2019 | |
| 2020 | return ret; |
| 2021 | } |
| 2022 | |
| 2023 | /* Get a list of all variables in scope, working out from the directly |
| 2024 | current one */ |
| 2025 | Variable *VG_(get_scope_variables)(ThreadId tid) |
| 2026 | { |
| 2027 | static const Bool debug = False; |
| 2028 | Variable *list, *end; |
| 2029 | Addr eip; |
| 2030 | SegInfo *si; |
| 2031 | Int scopeidx; |
| 2032 | Scope *scope; |
| 2033 | Int distance; |
| 2034 | static const Int maxsyms = 1000; |
| 2035 | Int nsyms = maxsyms; |
| 2036 | |
| 2037 | list = end = NULL; |
| 2038 | |
| 2039 | eip = VG_(get_EIP)(tid); |
| 2040 | |
| 2041 | search_all_scopetabs(eip, &si, &scopeidx); |
| 2042 | |
| 2043 | if (debug) |
| 2044 | VG_(printf)("eip=%p si=%p (%s; offset=%p) scopeidx=%d\n", |
| 2045 | eip, si, si ? si->filename : (Char *)"???", |
| 2046 | si ? si->offset : 0x99999, scopeidx); |
| 2047 | |
| 2048 | if (si == NULL) |
| 2049 | return NULL; /* nothing in scope (should use global scope at least) */ |
| 2050 | |
| 2051 | if (debug) { |
| 2052 | ScopeRange *sr = &si->scopetab[scopeidx]; |
| 2053 | Char file[100]; |
| 2054 | Int line; |
| 2055 | |
| 2056 | if (!VG_(get_filename_linenum)(sr->addr, file, sizeof(file), &line)) |
| 2057 | file[0] = 0; |
| 2058 | |
| 2059 | VG_(printf)("found scope range %p: eip=%p (%s:%d) size=%d scope=%p\n", |
| 2060 | sr, sr->addr, file, line, sr->size, sr->scope); |
| 2061 | } |
| 2062 | |
| 2063 | distance = 0; |
| 2064 | for(scope = si->scopetab[scopeidx].scope; scope != NULL; scope = scope->outer, distance++) { |
| 2065 | UInt i; |
| 2066 | |
| 2067 | for(i = 0; i < scope->nsyms; i++) { |
| 2068 | Sym *sym = &scope->syms[i]; |
| 2069 | Variable *v; |
| 2070 | |
| 2071 | if (nsyms-- == 0) { |
| 2072 | VG_(printf)("max %d syms reached\n", maxsyms); |
| 2073 | return list; |
| 2074 | } |
| 2075 | |
| 2076 | v = VG_(arena_malloc)(VG_AR_SYMTAB, sizeof(*v)); |
| 2077 | |
| 2078 | v->next = NULL; |
| 2079 | v->distance = distance; |
| 2080 | v->type = VG_(st_basetype)(sym->type, False); |
| 2081 | v->name = VG_(arena_strdup)(VG_AR_SYMTAB, sym->name); |
| 2082 | v->container = NULL; |
| 2083 | v->size = VG_(st_sizeof)(sym->type); |
| 2084 | |
| 2085 | if (debug && 0) |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 2086 | VG_(printf)("sym->name=%s sym->kind=%d offset=%d\n", sym->name, sym->kind, sym->u.offset); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2087 | switch(sym->kind) { |
| 2088 | UInt reg; |
| 2089 | |
| 2090 | case SyGlobal: |
| 2091 | case SyStatic: |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 2092 | if (sym->u.addr == 0) { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2093 | /* XXX lookup value */ |
| 2094 | } |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 2095 | v->valuep = sym->u.addr; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2096 | break; |
| 2097 | |
| 2098 | case SyReg: |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 2099 | v->valuep = (Addr)regaddr(tid, sym->u.regno); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2100 | break; |
| 2101 | |
| 2102 | case SyEBPrel: |
| 2103 | case SyESPrel: |
nethercote | cd65604 | 2004-09-11 23:48:22 +0000 | [diff] [blame] | 2104 | reg = *regaddr(tid, sym->kind == SyESPrel ? |
| 2105 | R_STACK_PTR : R_FRAME_PTR); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2106 | if (debug) |
mueller | 5ed88f2 | 2004-01-06 16:02:29 +0000 | [diff] [blame] | 2107 | VG_(printf)("reg=%p+%d=%p\n", reg, sym->u.offset, reg+sym->u.offset); |
| 2108 | v->valuep = (Addr)(reg + sym->u.offset); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2109 | break; |
| 2110 | |
| 2111 | case SyType: |
| 2112 | VG_(core_panic)("unexpected typedef in scope"); |
| 2113 | } |
| 2114 | |
| 2115 | if (v->valuep == 0) { |
| 2116 | /* not interesting or useful */ |
| 2117 | VG_(arena_free)(VG_AR_SYMTAB, v); |
| 2118 | continue; |
| 2119 | } |
| 2120 | |
| 2121 | /* append to end of list */ |
| 2122 | if (list == NULL) |
| 2123 | list = end = v; |
| 2124 | else { |
| 2125 | end->next = v; |
| 2126 | end = v; |
| 2127 | } |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | return list; |
| 2132 | } |
| 2133 | #endif /* TEST */ |
| 2134 | |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 2135 | /* Print into buf info on code address, function name and filename */ |
| 2136 | Char* VG_(describe_eip)(Addr eip, Char* buf, Int n_buf) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2137 | { |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 2138 | #define APPEND(str) \ |
| 2139 | { UChar* sss; \ |
| 2140 | for (sss = str; n < n_buf-1 && *sss != 0; n++,sss++) \ |
| 2141 | buf[n] = *sss; \ |
| 2142 | buf[n] = '\0'; \ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2143 | } |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 2144 | UInt lineno; |
| 2145 | UChar ibuf[20]; |
| 2146 | UInt n = 0; |
nethercote | 3b38c1d | 2004-10-18 15:47:18 +0000 | [diff] [blame] | 2147 | static UChar buf_fn[M_VG_ERRTXT]; |
| 2148 | static UChar buf_obj[M_VG_ERRTXT]; |
| 2149 | static UChar buf_srcloc[M_VG_ERRTXT]; |
nethercote | 80f7678 | 2003-11-13 22:34:00 +0000 | [diff] [blame] | 2150 | Bool know_fnname = VG_(get_fnname) (eip, buf_fn, M_VG_ERRTXT); |
| 2151 | Bool know_objname = VG_(get_objname)(eip, buf_obj, M_VG_ERRTXT); |
| 2152 | Bool know_srcloc = VG_(get_filename_linenum)(eip, buf_srcloc, M_VG_ERRTXT, |
| 2153 | &lineno); |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 2154 | VG_(sprintf)(ibuf,"0x%x: ", eip); |
| 2155 | APPEND(ibuf); |
| 2156 | if (know_fnname) { |
| 2157 | APPEND(buf_fn); |
| 2158 | if (!know_srcloc && know_objname) { |
| 2159 | APPEND(" (in "); |
| 2160 | APPEND(buf_obj); |
| 2161 | APPEND(")"); |
| 2162 | } |
| 2163 | } else if (know_objname && !know_srcloc) { |
| 2164 | APPEND("(within "); |
| 2165 | APPEND(buf_obj); |
| 2166 | APPEND(")"); |
| 2167 | } else { |
| 2168 | APPEND("???"); |
| 2169 | } |
| 2170 | if (know_srcloc) { |
| 2171 | APPEND(" ("); |
| 2172 | APPEND(buf_srcloc); |
| 2173 | APPEND(":"); |
| 2174 | VG_(sprintf)(ibuf,"%d",lineno); |
| 2175 | APPEND(ibuf); |
| 2176 | APPEND(")"); |
| 2177 | } |
| 2178 | return buf; |
| 2179 | |
| 2180 | #undef APPEND |
| 2181 | } |
| 2182 | |
| 2183 | /* Print a mini stack dump, showing the current location. */ |
| 2184 | void VG_(mini_stack_dump) ( Addr eips[], UInt n_eips ) |
| 2185 | { |
| 2186 | UInt i; |
nethercote | 3b38c1d | 2004-10-18 15:47:18 +0000 | [diff] [blame] | 2187 | static UChar buf[M_VG_ERRTXT]; |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 2188 | Bool main_done = False; |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 2189 | |
| 2190 | Int stop_at = n_eips; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2191 | |
njn | 49ef462 | 2002-10-04 15:15:08 +0000 | [diff] [blame] | 2192 | vg_assert(stop_at > 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2193 | |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 2194 | /* This loop is the basis for the one in VG_(gen_suppressions)(); if you |
| 2195 | change this, change it too! */ |
njn | 49ef462 | 2002-10-04 15:15:08 +0000 | [diff] [blame] | 2196 | i = 0; |
| 2197 | do { |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 2198 | Addr eip = eips[i]; |
nethercote | fbfc108 | 2004-09-04 15:28:37 +0000 | [diff] [blame] | 2199 | if (i > 0) |
| 2200 | eip -= MIN_INSTR_SIZE; // point to calling line |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 2201 | VG_(describe_eip)(eip, buf, M_VG_ERRTXT); |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 2202 | |
| 2203 | if ( ! VG_(clo_show_below_main)) { |
| 2204 | // Stop after "main"; if main() is recursive, stop after last main(). |
| 2205 | if (VG_(strstr)(buf, " main (")) |
| 2206 | main_done = True; |
| 2207 | else if (main_done) |
| 2208 | break; |
| 2209 | } |
| 2210 | VG_(message)(Vg_UserMsg, " %s %s", ( i == 0 ? "at" : "by" ), buf); |
njn | 49ef462 | 2002-10-04 15:15:08 +0000 | [diff] [blame] | 2211 | i++; |
| 2212 | |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 2213 | } while (i < (UInt)stop_at && eips[i] != 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2216 | |
| 2217 | /*------------------------------------------------------------*/ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2218 | /*--- General purpose redirection. ---*/ |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2219 | /*------------------------------------------------------------*/ |
| 2220 | |
jseward | bdce5e0 | 2003-12-21 23:32:43 +0000 | [diff] [blame] | 2221 | /* Set to True for debug printing. */ |
| 2222 | static const Bool verbose_redir = False; |
| 2223 | |
| 2224 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2225 | /* resolved redirections, indexed by from_addr */ |
| 2226 | typedef struct _CodeRedirect { |
| 2227 | const Char *from_lib; /* library qualifier pattern */ |
| 2228 | const Char *from_sym; /* symbol */ |
| 2229 | Addr from_addr; /* old addr */ |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2230 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2231 | const Char *to_lib; /* library qualifier pattern */ |
| 2232 | const Char *to_sym; /* symbol */ |
| 2233 | Addr to_addr; /* new addr */ |
| 2234 | |
| 2235 | struct _CodeRedirect *next; /* next pointer on unresolved list */ |
| 2236 | } CodeRedirect; |
| 2237 | |
| 2238 | static Int addrcmp(const void *ap, const void *bp) |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2239 | { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2240 | Addr a = *(Addr *)ap; |
| 2241 | Addr b = *(Addr *)bp; |
| 2242 | Int ret; |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2243 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2244 | if (a == b) |
| 2245 | ret = 0; |
| 2246 | else |
| 2247 | ret = (a < b) ? -1 : 1; |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2248 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2249 | return ret; |
| 2250 | } |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2251 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2252 | static Char *straddr(void *p) |
| 2253 | { |
| 2254 | static Char buf[16]; |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2255 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2256 | VG_(sprintf)(buf, "%p", *(Addr *)p); |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2257 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2258 | return buf; |
| 2259 | } |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2260 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2261 | static SkipList sk_resolved_redir = SKIPLIST_INIT(CodeRedirect, from_addr, |
| 2262 | addrcmp, straddr, VG_AR_SYMTAB); |
| 2263 | static CodeRedirect *unresolved_redir = NULL; |
| 2264 | |
| 2265 | static Bool match_lib(const Char *pattern, const SegInfo *si) |
| 2266 | { |
| 2267 | /* pattern == NULL matches everything, otherwise use globbing |
| 2268 | |
| 2269 | If the pattern starts with: |
| 2270 | file:, then match filename |
| 2271 | soname:, then match soname |
| 2272 | something else, match filename |
| 2273 | */ |
| 2274 | const Char *name = si->filename; |
| 2275 | |
| 2276 | if (pattern == NULL) |
| 2277 | return True; |
| 2278 | |
| 2279 | if (VG_(strncmp)(pattern, "file:", 5) == 0) { |
| 2280 | pattern += 5; |
| 2281 | name = si->filename; |
| 2282 | } |
| 2283 | if (VG_(strncmp)(pattern, "soname:", 7) == 0) { |
| 2284 | pattern += 7; |
| 2285 | name = si->soname; |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2288 | if (name == NULL) |
| 2289 | return False; |
| 2290 | |
| 2291 | return VG_(string_match)(pattern, name); |
| 2292 | } |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2293 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2294 | /* Resolve a redir using si if possible, and add it to the resolved |
| 2295 | list */ |
| 2296 | static Bool resolve_redir(CodeRedirect *redir, const SegInfo *si) |
| 2297 | { |
| 2298 | Bool resolved; |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2299 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2300 | vg_assert(si != NULL); |
| 2301 | vg_assert(si->seg != NULL); |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2302 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2303 | /* no redirection from Valgrind segments */ |
| 2304 | if (si->seg->flags & SF_VALGRIND) |
| 2305 | return False; |
| 2306 | |
| 2307 | resolved = (redir->from_addr != 0) && (redir->to_addr != 0); |
| 2308 | |
jseward | bdce5e0 | 2003-12-21 23:32:43 +0000 | [diff] [blame] | 2309 | if (0 && verbose_redir) |
| 2310 | VG_(printf)(" consider FROM binding %s:%s -> %s:%s in %s(%s)\n", |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2311 | redir->from_lib, redir->from_sym, |
| 2312 | redir->to_lib, redir->to_sym, |
| 2313 | si->filename, si->soname); |
| 2314 | |
| 2315 | vg_assert(!resolved); |
| 2316 | |
| 2317 | if (redir->from_addr == 0) { |
| 2318 | vg_assert(redir->from_sym != NULL); |
| 2319 | |
| 2320 | if (match_lib(redir->from_lib, si)) { |
| 2321 | redir->from_addr = reverse_search_one_symtab(si, redir->from_sym); |
jseward | bdce5e0 | 2003-12-21 23:32:43 +0000 | [diff] [blame] | 2322 | if (verbose_redir && redir->from_addr != 0) |
| 2323 | VG_(printf)(" bind FROM: %p = %s:%s\n", |
| 2324 | redir->from_addr,redir->from_lib, redir->from_sym ); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | if (redir->to_addr == 0) { |
| 2329 | vg_assert(redir->to_sym != NULL); |
| 2330 | |
| 2331 | if (match_lib(redir->to_lib, si)) { |
| 2332 | redir->to_addr = reverse_search_one_symtab(si, redir->to_sym); |
jseward | bdce5e0 | 2003-12-21 23:32:43 +0000 | [diff] [blame] | 2333 | if (verbose_redir && redir->to_addr != 0) |
| 2334 | VG_(printf)(" bind TO: %p = %s:%s\n", |
| 2335 | redir->to_addr,redir->to_lib, redir->to_sym ); |
| 2336 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2337 | } |
| 2338 | } |
| 2339 | |
| 2340 | resolved = (redir->from_addr != 0) && (redir->to_addr != 0); |
| 2341 | |
jseward | bdce5e0 | 2003-12-21 23:32:43 +0000 | [diff] [blame] | 2342 | if (0 && verbose_redir) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2343 | VG_(printf)("resolve_redir: %s:%s from=%p %s:%s to=%p\n", |
| 2344 | redir->from_lib, redir->from_sym, redir->from_addr, |
| 2345 | redir->to_lib, redir->to_sym, redir->to_addr); |
| 2346 | |
| 2347 | if (resolved) { |
jseward | bdce5e0 | 2003-12-21 23:32:43 +0000 | [diff] [blame] | 2348 | if (VG_(clo_verbosity) > 2 || verbose_redir) { |
| 2349 | VG_(message)(Vg_DebugMsg, " redir resolved (%s:%s=%p -> ", |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2350 | redir->from_lib, redir->from_sym, redir->from_addr); |
jseward | bdce5e0 | 2003-12-21 23:32:43 +0000 | [diff] [blame] | 2351 | VG_(message)(Vg_DebugMsg, " %s:%s=%p)", |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2352 | redir->to_lib, redir->to_sym, redir->to_addr); |
| 2353 | } |
| 2354 | |
jseward | a053a84 | 2003-12-22 00:50:52 +0000 | [diff] [blame] | 2355 | if (VG_(search_transtab)(redir->from_addr) != 0) { |
| 2356 | /* For some given (from, to) redir, the "from" function got |
| 2357 | called before the .so containing "to" became available. We |
| 2358 | know this because there is already a translation for the |
| 2359 | entry point of the original "from". So the redirect will |
| 2360 | never actually take effect unless that translation is |
| 2361 | discarded. |
| 2362 | |
| 2363 | Note, we only really need to discard the first bb of the |
| 2364 | old entry point, and so we avoid the problem of having to |
| 2365 | figure out how big that bb was -- since it is at least 1 |
| 2366 | byte of original code, we can just pass 1 as the original |
| 2367 | size to invalidate_translations() and it will indeed get |
| 2368 | rid of the translation. |
| 2369 | |
| 2370 | Note, this is potentially expensive -- discarding |
| 2371 | translations causes complete unchaining. |
| 2372 | */ |
| 2373 | if (VG_(clo_verbosity) > 2) { |
| 2374 | VG_(message)(Vg_UserMsg, |
| 2375 | "Discarding translation due to redirect of already called function" ); |
| 2376 | VG_(message)(Vg_UserMsg, |
| 2377 | " %s (%p -> %p)", |
| 2378 | redir->from_sym, redir->from_addr, redir->to_addr ); |
| 2379 | } |
| 2380 | VG_(invalidate_translations)(redir->from_addr, 1, True); |
| 2381 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2382 | |
| 2383 | VG_(SkipList_Insert)(&sk_resolved_redir, redir); |
| 2384 | } |
| 2385 | |
| 2386 | return resolved; |
| 2387 | } |
| 2388 | |
| 2389 | /* Go through the complete redir list, resolving as much as possible with this SegInfo. |
| 2390 | |
| 2391 | This should be called when a new SegInfo symtab is loaded. |
| 2392 | */ |
| 2393 | static void resolve_seg_redirs(SegInfo *si) |
| 2394 | { |
| 2395 | CodeRedirect **prevp = &unresolved_redir; |
| 2396 | CodeRedirect *redir, *next; |
| 2397 | |
jseward | bdce5e0 | 2003-12-21 23:32:43 +0000 | [diff] [blame] | 2398 | if (verbose_redir) |
| 2399 | VG_(printf)("Considering redirs to/from %s(soname=%s)\n", |
| 2400 | si->filename, si->soname); |
| 2401 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2402 | /* visit each unresolved redir - if it becomes resolved, then |
| 2403 | remove it from the unresolved list */ |
| 2404 | for(redir = unresolved_redir; redir != NULL; redir = next) { |
| 2405 | next = redir->next; |
| 2406 | |
| 2407 | if (resolve_redir(redir, si)) { |
| 2408 | *prevp = next; |
| 2409 | redir->next = NULL; |
| 2410 | } else |
| 2411 | prevp = &redir->next; |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | static Bool resolve_redir_allsegs(CodeRedirect *redir) |
| 2416 | { |
| 2417 | SegInfo *si; |
| 2418 | |
| 2419 | for(si = segInfo; si != NULL; si = si->next) |
| 2420 | if (resolve_redir(redir, si)) |
| 2421 | return True; |
| 2422 | |
| 2423 | return False; |
| 2424 | } |
| 2425 | |
| 2426 | /* Redirect a lib/symbol reference to a function at lib/symbol */ |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 2427 | static void add_redirect_sym(const Char *from_lib, const Char *from_sym, |
| 2428 | const Char *to_lib, const Char *to_sym) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2429 | { |
| 2430 | CodeRedirect *redir = VG_(SkipNode_Alloc)(&sk_resolved_redir); |
| 2431 | |
| 2432 | redir->from_lib = VG_(arena_strdup)(VG_AR_SYMTAB, from_lib); |
| 2433 | redir->from_sym = VG_(arena_strdup)(VG_AR_SYMTAB, from_sym); |
| 2434 | redir->from_addr = 0; |
| 2435 | |
| 2436 | redir->to_lib = VG_(arena_strdup)(VG_AR_SYMTAB, to_lib); |
| 2437 | redir->to_sym = VG_(arena_strdup)(VG_AR_SYMTAB, to_sym); |
| 2438 | redir->to_addr = 0; |
| 2439 | |
jseward | 5348e56 | 2003-12-21 23:54:59 +0000 | [diff] [blame] | 2440 | if (VG_(clo_verbosity) >= 2) |
| 2441 | VG_(message)(Vg_UserMsg, |
| 2442 | "REDIRECT %s(%s) to %s(%s)", |
| 2443 | from_lib, from_sym, to_lib, to_sym); |
| 2444 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2445 | if (!resolve_redir_allsegs(redir)) { |
| 2446 | /* can't resolve immediately; add to list */ |
| 2447 | redir->next = unresolved_redir; |
| 2448 | unresolved_redir = redir; |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | /* Redirect a lib/symbol reference to a function at lib/symbol */ |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 2453 | static void add_redirect_addr(const Char *from_lib, const Char *from_sym, |
| 2454 | Addr to_addr) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2455 | { |
| 2456 | CodeRedirect *redir = VG_(SkipNode_Alloc)(&sk_resolved_redir); |
| 2457 | |
| 2458 | redir->from_lib = VG_(arena_strdup)(VG_AR_SYMTAB, from_lib); |
| 2459 | redir->from_sym = VG_(arena_strdup)(VG_AR_SYMTAB, from_sym); |
| 2460 | redir->from_addr = 0; |
| 2461 | |
| 2462 | redir->to_lib = NULL; |
| 2463 | redir->to_sym = NULL; |
| 2464 | redir->to_addr = to_addr; |
| 2465 | |
| 2466 | if (!resolve_redir_allsegs(redir)) { |
| 2467 | /* can't resolve immediately; add to list */ |
| 2468 | redir->next = unresolved_redir; |
| 2469 | unresolved_redir = redir; |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | Addr VG_(code_redirect)(Addr a) |
| 2474 | { |
| 2475 | CodeRedirect *r = VG_(SkipList_Find)(&sk_resolved_redir, &a); |
| 2476 | |
| 2477 | if (r == NULL || r->from_addr != a) |
| 2478 | return a; |
| 2479 | |
| 2480 | vg_assert(r->to_addr != 0); |
| 2481 | |
| 2482 | return r->to_addr; |
| 2483 | } |
| 2484 | |
| 2485 | void VG_(setup_code_redirect_table) ( void ) |
| 2486 | { |
| 2487 | static const struct { |
| 2488 | const Char *from, *to; |
| 2489 | } redirects[] = { |
| 2490 | { "__GI___errno_location", "__errno_location" }, |
| 2491 | { "__errno_location", "__errno_location" }, |
| 2492 | { "__GI___h_errno_location", "__h_errno_location" }, |
| 2493 | { "__h_errno_location", "__h_errno_location" }, |
| 2494 | { "__GI___res_state", "__res_state" }, |
| 2495 | { "__res_state", "__res_state" }, |
| 2496 | }; |
| 2497 | Int i; |
| 2498 | |
| 2499 | for(i = 0; i < sizeof(redirects)/sizeof(*redirects); i++) { |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 2500 | add_redirect_sym("soname:libc.so.6", redirects[i].from, |
| 2501 | "soname:libpthread.so.0", redirects[i].to); |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2502 | } |
jseward | bdce5e0 | 2003-12-21 23:32:43 +0000 | [diff] [blame] | 2503 | |
thughes | 266c616 | 2004-03-22 19:46:29 +0000 | [diff] [blame] | 2504 | /* Redirect _dl_sysinfo_int80, which is glibc's default system call |
| 2505 | routine, to the routine in our trampoline page so that the |
| 2506 | special sysinfo unwind hack in vg_execontext.c will kick in. |
| 2507 | */ |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 2508 | add_redirect_addr("soname:ld-linux.so.2", "_dl_sysinfo_int80", |
| 2509 | VG_(client_trampoline_code)+VG_(tramp_syscall_offset)); |
thughes | 266c616 | 2004-03-22 19:46:29 +0000 | [diff] [blame] | 2510 | |
jseward | 5348e56 | 2003-12-21 23:54:59 +0000 | [diff] [blame] | 2511 | /* Overenthusiastic use of PLT bypassing by the glibc people also |
| 2512 | means we need to patch the following functions to our own |
| 2513 | implementations of said, in mac_replace_strmem.c. |
| 2514 | */ |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 2515 | add_redirect_sym("soname:libc.so.6", "stpcpy", |
| 2516 | "*vgpreload_memcheck.so*", "stpcpy"); |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 2517 | |
| 2518 | add_redirect_sym("soname:libc.so.6", "strlen", |
| 2519 | "*vgpreload_memcheck.so*", "strlen"); |
| 2520 | |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 2521 | add_redirect_sym("soname:libc.so.6", "strnlen", |
| 2522 | "*vgpreload_memcheck.so*", "strnlen"); |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 2523 | |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 2524 | add_redirect_sym("soname:ld-linux.so.2", "stpcpy", |
| 2525 | "*vgpreload_memcheck.so*", "stpcpy"); |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 2526 | |
| 2527 | add_redirect_sym("soname:libc.so.6", "strchr", |
| 2528 | "*vgpreload_memcheck.so*", "strchr"); |
nethercote | 85cdd34 | 2004-08-01 22:36:40 +0000 | [diff] [blame] | 2529 | add_redirect_sym("soname:ld-linux.so.2", "strchr", |
| 2530 | "*vgpreload_memcheck.so*", "strchr"); |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 2531 | |
| 2532 | add_redirect_sym("soname:libc.so.6", "strchrnul", |
| 2533 | "*vgpreload_memcheck.so*", "glibc232_strchrnul"); |
| 2534 | |
| 2535 | add_redirect_sym("soname:libc.so.6", "rawmemchr", |
| 2536 | "*vgpreload_memcheck.so*", "glibc232_rawmemchr"); |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 2539 | /*------------------------------------------------------------*/ |
| 2540 | /*--- SegInfo accessor functions ---*/ |
| 2541 | /*------------------------------------------------------------*/ |
| 2542 | |
| 2543 | const SegInfo* VG_(next_seginfo)(const SegInfo* seg) |
| 2544 | { |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 2545 | if (seg == NULL) |
| 2546 | return segInfo; |
| 2547 | return seg->next; |
| 2548 | } |
| 2549 | |
| 2550 | Addr VG_(seg_start)(const SegInfo* seg) |
| 2551 | { |
| 2552 | return seg->start; |
| 2553 | } |
| 2554 | |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 2555 | SizeT VG_(seg_size)(const SegInfo* seg) |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 2556 | { |
| 2557 | return seg->size; |
| 2558 | } |
| 2559 | |
| 2560 | const UChar* VG_(seg_filename)(const SegInfo* seg) |
| 2561 | { |
| 2562 | return seg->filename; |
| 2563 | } |
| 2564 | |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 2565 | ULong VG_(seg_sym_offset)(const SegInfo* seg) |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 2566 | { |
| 2567 | return seg->offset; |
| 2568 | } |
| 2569 | |
| 2570 | VgSectKind VG_(seg_sect_kind)(Addr a) |
| 2571 | { |
| 2572 | SegInfo* seg; |
| 2573 | VgSectKind ret = Vg_SectUnknown; |
| 2574 | |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 2575 | for(seg = segInfo; seg != NULL; seg = seg->next) { |
| 2576 | if (a >= seg->start && a < (seg->start + seg->size)) { |
| 2577 | if (0) |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 2578 | 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] | 2579 | a, seg, seg->filename, |
| 2580 | seg->got_start, seg->got_size, |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 2581 | seg->plt_start, seg->plt_size, |
| 2582 | seg->data_start, seg->data_size, |
| 2583 | seg->bss_start, seg->bss_size); |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 2584 | ret = Vg_SectText; |
| 2585 | |
sewardj | 8fe15a3 | 2002-10-20 19:29:21 +0000 | [diff] [blame] | 2586 | if (a >= seg->data_start && a < (seg->data_start + seg->data_size)) |
| 2587 | ret = Vg_SectData; |
| 2588 | else if (a >= seg->bss_start && a < (seg->bss_start + seg->bss_size)) |
| 2589 | ret = Vg_SectBSS; |
| 2590 | else if (a >= seg->plt_start && a < (seg->plt_start + seg->plt_size)) |
sewardj | 4710438 | 2002-10-20 18:35:48 +0000 | [diff] [blame] | 2591 | ret = Vg_SectPLT; |
| 2592 | else if (a >= seg->got_start && a < (seg->got_start + seg->got_size)) |
| 2593 | ret = Vg_SectGOT; |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | return ret; |
| 2598 | } |
| 2599 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2600 | /*--------------------------------------------------------------------*/ |
| 2601 | /*--- end vg_symtab2.c ---*/ |
| 2602 | /*--------------------------------------------------------------------*/ |