mirror of
https://github.com/Zenithsiz/ftmemsim-valgrind.git
synced 2026-02-04 02:18:37 +00:00
a logfile for that pid already exists. This may happen for programs started during system boot which will tend to get the same pid each boot. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@1928
2066 lines
62 KiB
C
2066 lines
62 KiB
C
/*--------------------------------------------------------------------*/
|
|
/*--- Management of symbols and debugging information. ---*/
|
|
/*--- vg_symtab2.c ---*/
|
|
/*--------------------------------------------------------------------*/
|
|
|
|
/*
|
|
This file is part of Valgrind, an extensible x86 protected-mode
|
|
emulator for monitoring program execution on x86-Unixes.
|
|
|
|
Copyright (C) 2000-2003 Julian Seward
|
|
jseward@acm.org
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU General Public License as
|
|
published by the Free Software Foundation; either version 2 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
02111-1307, USA.
|
|
|
|
The GNU General Public License is contained in the file COPYING.
|
|
*/
|
|
|
|
#include "vg_include.h"
|
|
#include "vg_symtypes.h"
|
|
#include "vg_symtab2.h"
|
|
|
|
#include <elf.h> /* ELF defns */
|
|
|
|
|
|
/* Majorly rewritten Sun 3 Feb 02 to enable loading symbols from
|
|
dlopen()ed libraries, which is something that KDE3 does a lot.
|
|
|
|
Stabs reader greatly improved by Nick Nethercote, Apr 02.
|
|
*/
|
|
|
|
|
|
|
|
static void freeSegInfo ( SegInfo* si )
|
|
{
|
|
struct strchunk *chunk, *next;
|
|
vg_assert(si != NULL);
|
|
if (si->filename) VG_(arena_free)(VG_AR_SYMTAB, si->filename);
|
|
if (si->symtab) VG_(arena_free)(VG_AR_SYMTAB, si->symtab);
|
|
if (si->loctab) VG_(arena_free)(VG_AR_SYMTAB, si->loctab);
|
|
if (si->scopetab) VG_(arena_free)(VG_AR_SYMTAB, si->scopetab);
|
|
|
|
for(chunk = si->strchunks; chunk != NULL; chunk = next) {
|
|
next = chunk->next;
|
|
VG_(arena_free)(VG_AR_SYMTAB, chunk);
|
|
}
|
|
VG_(arena_free)(VG_AR_SYMTAB, si);
|
|
}
|
|
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- Adding stuff ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
/* Add a str to the string table, including terminating zero, and
|
|
return pointer to the string in vg_strtab. Unless it's been seen
|
|
recently, in which case we find the old pointer and return that.
|
|
This avoids the most egregious duplications.
|
|
|
|
JSGF: changed from returning an index to a pointer, and changed to
|
|
a chunking memory allocator rather than reallocating, so the
|
|
pointers are stable.
|
|
*/
|
|
|
|
Char *VG_(addStr) ( SegInfo* si, Char* str, Int len )
|
|
{
|
|
# define EMPTY NULL
|
|
# define NN 5
|
|
|
|
/* prevN[0] has the most recent, prevN[NN-1] the least recent */
|
|
static Char *prevN[NN] = { EMPTY, EMPTY, EMPTY, EMPTY, EMPTY };
|
|
static SegInfo* curr_si = NULL;
|
|
struct strchunk *chunk;
|
|
Int i, space_needed;
|
|
|
|
if (len == -1)
|
|
len = VG_(strlen)(str);
|
|
|
|
/* Avoid gratuitous duplication: if we saw `str' within the last NN,
|
|
* within this segment, return that index. Saves about 200KB in glibc,
|
|
* extra time taken is too small to measure. --NJN 2002-Aug-30 */
|
|
if (curr_si == si) {
|
|
for (i = NN-1; i >= 0; i--) {
|
|
if (EMPTY != prevN[i]
|
|
&& NULL != si->strchunks
|
|
&& 0 == VG_(memcmp)(str, prevN[i], len+1)) {
|
|
return prevN[i];
|
|
}
|
|
}
|
|
} else {
|
|
/* New segment */
|
|
curr_si = si;
|
|
for (i = 0; i < NN; i++) prevN[i] = EMPTY;
|
|
}
|
|
/* Shuffle prevous ones along, put new one in. */
|
|
for (i = NN-1; i > 0; i--)
|
|
prevN[i] = prevN[i-1];
|
|
|
|
# undef EMPTY
|
|
|
|
space_needed = 1 + len;
|
|
|
|
if (si->strchunks == NULL ||
|
|
(si->strchunks->strtab_used + space_needed) > STRCHUNKSIZE) {
|
|
chunk = VG_(arena_malloc)(VG_AR_SYMTAB, sizeof(*chunk));
|
|
chunk->strtab_used = 0;
|
|
chunk->next = si->strchunks;
|
|
si->strchunks = chunk;
|
|
}
|
|
chunk = si->strchunks;
|
|
|
|
prevN[0] = &chunk->strtab[chunk->strtab_used];
|
|
VG_(memcpy)(prevN[0], str, len);
|
|
chunk->strtab[chunk->strtab_used+len] = '\0';
|
|
chunk->strtab_used += space_needed;
|
|
|
|
return prevN[0];
|
|
}
|
|
|
|
/* Add a symbol to the symbol table. */
|
|
|
|
static __inline__
|
|
void addSym ( SegInfo* si, RiSym* sym )
|
|
{
|
|
UInt new_sz, i;
|
|
RiSym* new_tab;
|
|
|
|
/* Ignore zero-sized syms. */
|
|
if (sym->size == 0) return;
|
|
|
|
if (si->symtab_used == si->symtab_size) {
|
|
new_sz = 2 * si->symtab_size;
|
|
if (new_sz == 0) new_sz = 500;
|
|
new_tab = VG_(arena_malloc)(VG_AR_SYMTAB, new_sz * sizeof(RiSym) );
|
|
if (si->symtab != NULL) {
|
|
for (i = 0; i < si->symtab_used; i++)
|
|
new_tab[i] = si->symtab[i];
|
|
VG_(arena_free)(VG_AR_SYMTAB, si->symtab);
|
|
}
|
|
si->symtab = new_tab;
|
|
si->symtab_size = new_sz;
|
|
}
|
|
|
|
si->symtab[si->symtab_used] = *sym;
|
|
si->symtab_used++;
|
|
vg_assert(si->symtab_used <= si->symtab_size);
|
|
}
|
|
|
|
/* Add a location to the location table. */
|
|
|
|
static __inline__
|
|
void addLoc ( SegInfo* si, RiLoc* loc )
|
|
{
|
|
UInt new_sz, i;
|
|
RiLoc* new_tab;
|
|
|
|
/* Zero-sized locs should have been ignored earlier */
|
|
vg_assert(loc->size > 0);
|
|
|
|
if (si->loctab_used == si->loctab_size) {
|
|
new_sz = 2 * si->loctab_size;
|
|
if (new_sz == 0) new_sz = 500;
|
|
new_tab = VG_(arena_malloc)(VG_AR_SYMTAB, new_sz * sizeof(RiLoc) );
|
|
if (si->loctab != NULL) {
|
|
for (i = 0; i < si->loctab_used; i++)
|
|
new_tab[i] = si->loctab[i];
|
|
VG_(arena_free)(VG_AR_SYMTAB, si->loctab);
|
|
}
|
|
si->loctab = new_tab;
|
|
si->loctab_size = new_sz;
|
|
}
|
|
|
|
si->loctab[si->loctab_used] = *loc;
|
|
si->loctab_used++;
|
|
vg_assert(si->loctab_used <= si->loctab_size);
|
|
}
|
|
|
|
|
|
/* Top-level place to call to add a source-location mapping entry. */
|
|
|
|
void VG_(addLineInfo) ( SegInfo* si,
|
|
Char* filename,
|
|
Addr this,
|
|
Addr next,
|
|
Int lineno,
|
|
Int entry /* only needed for debug printing */
|
|
)
|
|
{
|
|
static const Bool debug = False;
|
|
RiLoc loc;
|
|
Int size = next - this;
|
|
|
|
/* Ignore zero-sized locs */
|
|
if (this == next) return;
|
|
|
|
if (debug)
|
|
VG_(printf)(" src %s line %d %p-%p\n", filename, lineno, this, next);
|
|
|
|
/* Maximum sanity checking. Some versions of GNU as do a shabby
|
|
* job with stabs entries; if anything looks suspicious, revert to
|
|
* a size of 1. This should catch the instruction of interest
|
|
* (since if using asm-level debug info, one instruction will
|
|
* correspond to one line, unlike with C-level debug info where
|
|
* multiple instructions can map to the one line), but avoid
|
|
* catching any other instructions bogusly. */
|
|
if (this > next) {
|
|
VG_(message)(Vg_DebugMsg,
|
|
"warning: line info addresses out of order "
|
|
"at entry %d: 0x%x 0x%x", entry, this, next);
|
|
size = 1;
|
|
}
|
|
|
|
if (size > MAX_LOC_SIZE) {
|
|
if (0)
|
|
VG_(message)(Vg_DebugMsg,
|
|
"warning: line info address range too large "
|
|
"at entry %d: %d", entry, size);
|
|
size = 1;
|
|
}
|
|
|
|
/* vg_assert(this < si->start + si->size && next-1 >= si->start); */
|
|
if (this >= si->start + si->size || next-1 < si->start) {
|
|
if (0)
|
|
VG_(message)(Vg_DebugMsg,
|
|
"warning: ignoring line info entry falling "
|
|
"outside current SegInfo: %p %p %p %p",
|
|
si->start, si->start + si->size,
|
|
this, next-1);
|
|
return;
|
|
}
|
|
|
|
vg_assert(lineno >= 0);
|
|
if (lineno > MAX_LINENO) {
|
|
VG_(message)(Vg_UserMsg,
|
|
"warning: ignoring line info entry with "
|
|
"huge line number (%d)", lineno);
|
|
VG_(message)(Vg_UserMsg,
|
|
" Can't handle line numbers "
|
|
"greater than %d, sorry", MAX_LINENO);
|
|
return;
|
|
}
|
|
|
|
loc.addr = this;
|
|
loc.size = (UShort)size;
|
|
loc.lineno = lineno;
|
|
loc.filename = filename;
|
|
|
|
if (0) VG_(message)(Vg_DebugMsg,
|
|
"addLoc: addr %p, size %d, line %d, file %s",
|
|
this,size,lineno,filename);
|
|
|
|
addLoc ( si, &loc );
|
|
}
|
|
|
|
static __inline__
|
|
void addScopeRange ( SegInfo* si, ScopeRange *range )
|
|
{
|
|
Int new_sz, i;
|
|
ScopeRange* new_tab;
|
|
|
|
/* Zero-sized scopes should have been ignored earlier */
|
|
vg_assert(range->size > 0);
|
|
|
|
if (si->scopetab_used == si->scopetab_size) {
|
|
new_sz = 2 * si->scopetab_size;
|
|
if (new_sz == 0) new_sz = 500;
|
|
new_tab = VG_(arena_malloc)(VG_AR_SYMTAB, new_sz * sizeof(*new_tab) );
|
|
if (si->scopetab != NULL) {
|
|
for (i = 0; i < si->scopetab_used; i++)
|
|
new_tab[i] = si->scopetab[i];
|
|
VG_(arena_free)(VG_AR_SYMTAB, si->scopetab);
|
|
}
|
|
si->scopetab = new_tab;
|
|
si->scopetab_size = new_sz;
|
|
}
|
|
|
|
si->scopetab[si->scopetab_used] = *range;
|
|
si->scopetab_used++;
|
|
vg_assert(si->scopetab_used <= si->scopetab_size);
|
|
}
|
|
|
|
|
|
/* Top-level place to call to add a source-location mapping entry. */
|
|
|
|
void VG_(addScopeInfo) ( SegInfo* si,
|
|
Addr this,
|
|
Addr next,
|
|
Scope *scope)
|
|
{
|
|
static const Bool debug = False;
|
|
Int size = next - this;
|
|
ScopeRange range;
|
|
|
|
/* Ignore zero-sized scopes */
|
|
if (this == next) {
|
|
if (debug)
|
|
VG_(printf)("ignoring zero-sized range, scope %p at %p\n", scope, this);
|
|
return;
|
|
}
|
|
|
|
if (debug)
|
|
VG_(printf)("adding scope range %p-%p (size=%d) scope %p (%d)\n",
|
|
this, next, next-this, scope, scope->depth);
|
|
|
|
range.addr = this;
|
|
range.size = size;
|
|
range.scope = scope;
|
|
|
|
addScopeRange ( si, &range );
|
|
}
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- Helpers ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
/* Non-fatal -- use vg_panic if terminal. */
|
|
void VG_(symerr) ( Char* msg )
|
|
{
|
|
if (VG_(clo_verbosity) > 1)
|
|
VG_(message)(Vg_UserMsg,"%s", msg );
|
|
}
|
|
|
|
|
|
/* Print a symbol. */
|
|
static
|
|
void printSym ( SegInfo* si, Int i )
|
|
{
|
|
VG_(printf)( "%5d: %8p .. %8p (%d) %s\n",
|
|
i,
|
|
si->symtab[i].addr,
|
|
si->symtab[i].addr + si->symtab[i].size - 1, si->symtab[i].size,
|
|
si->symtab[i].name );
|
|
}
|
|
|
|
|
|
#if 0
|
|
/* Print the entire sym tab. */
|
|
static __attribute__ ((unused))
|
|
void printSymtab ( void )
|
|
{
|
|
Int i;
|
|
VG_(printf)("\n------ BEGIN vg_symtab ------\n");
|
|
for (i = 0; i < vg_symtab_used; i++)
|
|
printSym(i);
|
|
VG_(printf)("------ BEGIN vg_symtab ------\n");
|
|
}
|
|
#endif
|
|
|
|
#if 0
|
|
/* Paranoid strcat. */
|
|
static
|
|
void safeCopy ( UChar* dst, UInt maxlen, UChar* src )
|
|
{
|
|
UInt i = 0, j = 0;
|
|
while (True) {
|
|
if (i >= maxlen) return;
|
|
if (dst[i] == 0) break;
|
|
i++;
|
|
}
|
|
while (True) {
|
|
if (i >= maxlen) return;
|
|
dst[i] = src[j];
|
|
if (src[j] == 0) return;
|
|
i++; j++;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- Canonicalisers ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
static void shellsort(void *base, UInt nmemb, UInt sz, Int (*compare)(void *a, void *b))
|
|
{
|
|
/* Magic numbers due to Janet Incerpi and Robert Sedgewick. */
|
|
static const Int incs[16] = { 1, 3, 7, 21, 48, 112, 336, 861, 1968,
|
|
4592, 13776, 33936, 86961, 198768,
|
|
463792, 1391376 };
|
|
Int lo = 0;
|
|
Int hi = nmemb-1;
|
|
Int bigN;
|
|
Int hp;
|
|
Char *cp = (Char *)base;
|
|
Char tmp[sz];
|
|
|
|
bigN = hi - lo + 1;
|
|
if (bigN < 2)
|
|
return;
|
|
hp = 0;
|
|
while (hp < 16 && incs[hp] < bigN)
|
|
hp++;
|
|
hp--;
|
|
vg_assert(0 <= hp && hp < 16);
|
|
|
|
for (; hp >= 0; hp--) {
|
|
Int i, h;
|
|
|
|
h = incs[hp];
|
|
i = lo + h;
|
|
for (i = lo+h; i <= hi; i++) {
|
|
Int j;
|
|
VG_(memcpy)(&tmp, &cp[i * sz], sz);
|
|
|
|
j = i;
|
|
while ((*compare)((void *)&cp[(j-h) * sz], (void *)&tmp) > 0) {
|
|
VG_(memcpy)(&cp[j * sz], &cp[(j-h) * sz], sz);
|
|
j = j - h;
|
|
if (j <= (lo + h - 1))
|
|
break;
|
|
}
|
|
VG_(memcpy)(&cp[j * sz], &tmp, sz);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Sort the symtab by starting address, and emit warnings if any
|
|
symbols have overlapping address ranges. We use that old chestnut,
|
|
shellsort. Mash the table around so as to establish the property
|
|
that addresses are in order and the ranges to not overlap. This
|
|
facilitates using binary search to map addresses to symbols when we
|
|
come to query the table.
|
|
*/
|
|
static Int compare_RiSym(void *va, void *vb) {
|
|
RiSym *a = (RiSym *)va;
|
|
RiSym *b = (RiSym *)vb;
|
|
|
|
return a->addr - b->addr;
|
|
}
|
|
|
|
static
|
|
void canonicaliseSymtab ( SegInfo* si )
|
|
{
|
|
Int i, j, n_merged, n_truncated;
|
|
Addr s1, s2, e1, e2;
|
|
|
|
# define SWAP(ty,aa,bb) \
|
|
do { ty tt = (aa); (aa) = (bb); (bb) = tt; } while (0)
|
|
|
|
if (si->symtab_used == 0)
|
|
return;
|
|
|
|
shellsort(si->symtab, si->symtab_used, sizeof(*si->symtab), compare_RiSym);
|
|
|
|
cleanup_more:
|
|
|
|
/* If two symbols have identical address ranges, favour the
|
|
one with the longer name.
|
|
*/
|
|
do {
|
|
n_merged = 0;
|
|
j = si->symtab_used;
|
|
si->symtab_used = 0;
|
|
for (i = 0; i < j; i++) {
|
|
if (i < j-1
|
|
&& si->symtab[i].addr == si->symtab[i+1].addr
|
|
&& si->symtab[i].size == si->symtab[i+1].size) {
|
|
n_merged++;
|
|
/* merge the two into one */
|
|
if (VG_(strlen)(si->symtab[i].name)
|
|
> VG_(strlen)(si->symtab[i+1].name)) {
|
|
si->symtab[si->symtab_used++] = si->symtab[i];
|
|
} else {
|
|
si->symtab[si->symtab_used++] = si->symtab[i+1];
|
|
}
|
|
i++;
|
|
} else {
|
|
si->symtab[si->symtab_used++] = si->symtab[i];
|
|
}
|
|
}
|
|
if (VG_(clo_trace_symtab))
|
|
VG_(printf)( "%d merged\n", n_merged);
|
|
}
|
|
while (n_merged > 0);
|
|
|
|
/* Detect and "fix" overlapping address ranges. */
|
|
n_truncated = 0;
|
|
|
|
for (i = 0; i < ((Int)si->symtab_used) -1; i++) {
|
|
|
|
vg_assert(si->symtab[i].addr <= si->symtab[i+1].addr);
|
|
|
|
/* Check for common (no overlap) case. */
|
|
if (si->symtab[i].addr + si->symtab[i].size
|
|
<= si->symtab[i+1].addr)
|
|
continue;
|
|
|
|
/* There's an overlap. Truncate one or the other. */
|
|
if (VG_(clo_trace_symtab)) {
|
|
VG_(printf)("overlapping address ranges in symbol table\n\t");
|
|
printSym(si,i);
|
|
VG_(printf)("\t");
|
|
printSym(si,i+1);
|
|
VG_(printf)("\n");
|
|
}
|
|
|
|
/* Truncate one or the other. */
|
|
s1 = si->symtab[i].addr;
|
|
s2 = si->symtab[i+1].addr;
|
|
e1 = s1 + si->symtab[i].size - 1;
|
|
e2 = s2 + si->symtab[i+1].size - 1;
|
|
if (s1 < s2) {
|
|
e1 = s2-1;
|
|
} else {
|
|
vg_assert(s1 == s2);
|
|
if (e1 > e2) {
|
|
s1 = e2+1; SWAP(Addr,s1,s2); SWAP(Addr,e1,e2);
|
|
} else
|
|
if (e1 < e2) {
|
|
s2 = e1+1;
|
|
} else {
|
|
/* e1 == e2. Identical addr ranges. We'll eventually wind
|
|
up back at cleanup_more, which will take care of it. */
|
|
}
|
|
}
|
|
si->symtab[i].addr = s1;
|
|
si->symtab[i+1].addr = s2;
|
|
si->symtab[i].size = e1 - s1 + 1;
|
|
si->symtab[i+1].size = e2 - s2 + 1;
|
|
vg_assert(s1 <= s2);
|
|
vg_assert(si->symtab[i].size > 0);
|
|
vg_assert(si->symtab[i+1].size > 0);
|
|
/* It may be that the i+1 entry now needs to be moved further
|
|
along to maintain the address order requirement. */
|
|
j = i+1;
|
|
while (j < ((Int)si->symtab_used)-1
|
|
&& si->symtab[j].addr > si->symtab[j+1].addr) {
|
|
SWAP(RiSym,si->symtab[j],si->symtab[j+1]);
|
|
j++;
|
|
}
|
|
n_truncated++;
|
|
}
|
|
|
|
if (n_truncated > 0) goto cleanup_more;
|
|
|
|
/* Ensure relevant postconditions hold. */
|
|
for (i = 0; i < ((Int)si->symtab_used)-1; i++) {
|
|
/* No zero-sized symbols. */
|
|
vg_assert(si->symtab[i].size > 0);
|
|
/* In order. */
|
|
vg_assert(si->symtab[i].addr < si->symtab[i+1].addr);
|
|
/* No overlaps. */
|
|
vg_assert(si->symtab[i].addr + si->symtab[i].size - 1
|
|
< si->symtab[i+1].addr);
|
|
}
|
|
# undef SWAP
|
|
}
|
|
|
|
/* Sort the scope range table by starting address. Mash the table
|
|
around so as to establish the property that addresses are in order
|
|
and the ranges do not overlap. This facilitates using binary
|
|
search to map addresses to scopes when we come to query the
|
|
table.
|
|
*/
|
|
static Int compare_ScopeRange(void *va, void *vb) {
|
|
ScopeRange *a = (ScopeRange *)va;
|
|
ScopeRange *b = (ScopeRange *)vb;
|
|
|
|
return a->addr - b->addr;
|
|
}
|
|
|
|
static
|
|
void canonicaliseScopetab ( SegInfo* si )
|
|
{
|
|
Int i,j;
|
|
|
|
if (si->scopetab_used == 0)
|
|
return;
|
|
|
|
/* Sort by start address. */
|
|
shellsort(si->scopetab, si->scopetab_used, sizeof(*si->scopetab),
|
|
compare_ScopeRange);
|
|
|
|
/* If two adjacent entries overlap, truncate the first. */
|
|
for (i = 0; i < si->scopetab_used-1; i++) {
|
|
if (si->scopetab[i].addr + si->scopetab[i].size > si->scopetab[i+1].addr) {
|
|
Int new_size = si->scopetab[i+1].addr - si->scopetab[i].addr;
|
|
|
|
if (new_size < 0)
|
|
si->scopetab[i].size = 0;
|
|
else
|
|
si->scopetab[i].size = new_size;
|
|
}
|
|
}
|
|
|
|
/* Zap any zero-sized entries resulting from the truncation
|
|
process. */
|
|
j = 0;
|
|
for (i = 0; i < si->scopetab_used; i++) {
|
|
if (si->scopetab[i].size > 0) {
|
|
si->scopetab[j] = si->scopetab[i];
|
|
j++;
|
|
}
|
|
}
|
|
si->scopetab_used = j;
|
|
|
|
/* Ensure relevant postconditions hold. */
|
|
for (i = 0; i < si->scopetab_used-1; i++) {
|
|
/*
|
|
VG_(printf)("%d (%d) %d 0x%x\n",
|
|
i, si->scopetab[i+1].confident,
|
|
si->scopetab[i+1].size, si->scopetab[i+1].addr );
|
|
*/
|
|
/* No zero-sized symbols. */
|
|
vg_assert(si->scopetab[i].size > 0);
|
|
/* In order. */
|
|
if (si->scopetab[i].addr >= si->scopetab[i+1].addr)
|
|
VG_(printf)("si->scopetab[%d] = %p,size=%d [%d] = %p,size=%d\n",
|
|
i, si->scopetab[i].addr, si->scopetab[i].size,
|
|
i+1, si->scopetab[i+1].addr, si->scopetab[i+1].size);
|
|
vg_assert(si->scopetab[i].addr < si->scopetab[i+1].addr);
|
|
/* No overlaps. */
|
|
vg_assert(si->scopetab[i].addr + si->scopetab[i].size - 1
|
|
< si->scopetab[i+1].addr);
|
|
}
|
|
}
|
|
|
|
|
|
/* Sort the location table by starting address. Mash the table around
|
|
so as to establish the property that addresses are in order and the
|
|
ranges do not overlap. This facilitates using binary search to map
|
|
addresses to locations when we come to query the table.
|
|
*/
|
|
static Int compare_RiLoc(void *va, void *vb) {
|
|
RiLoc *a = (RiLoc *)va;
|
|
RiLoc *b = (RiLoc *)vb;
|
|
|
|
return a->addr - b->addr;
|
|
}
|
|
|
|
static
|
|
void canonicaliseLoctab ( SegInfo* si )
|
|
{
|
|
Int i, j;
|
|
|
|
# define SWAP(ty,aa,bb) \
|
|
do { ty tt = (aa); (aa) = (bb); (bb) = tt; } while (0);
|
|
|
|
if (si->loctab_used == 0)
|
|
return;
|
|
|
|
/* Sort by start address. */
|
|
shellsort(si->loctab, si->loctab_used, sizeof(*si->loctab), compare_RiLoc);
|
|
|
|
/* If two adjacent entries overlap, truncate the first. */
|
|
for (i = 0; i < ((Int)si->loctab_used)-1; i++) {
|
|
vg_assert(si->loctab[i].size < 10000);
|
|
if (si->loctab[i].addr + si->loctab[i].size > si->loctab[i+1].addr) {
|
|
/* Do this in signed int32 because the actual .size fields
|
|
are unsigned 16s. */
|
|
Int new_size = si->loctab[i+1].addr - si->loctab[i].addr;
|
|
if (new_size < 0) {
|
|
si->loctab[i].size = 0;
|
|
} else
|
|
if (new_size >= 65536) {
|
|
si->loctab[i].size = 65535;
|
|
} else {
|
|
si->loctab[i].size = (UShort)new_size;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Zap any zero-sized entries resulting from the truncation
|
|
process. */
|
|
j = 0;
|
|
for (i = 0; i < (Int)si->loctab_used; i++) {
|
|
if (si->loctab[i].size > 0) {
|
|
si->loctab[j] = si->loctab[i];
|
|
j++;
|
|
}
|
|
}
|
|
si->loctab_used = j;
|
|
|
|
/* Ensure relevant postconditions hold. */
|
|
for (i = 0; i < ((Int)si->loctab_used)-1; i++) {
|
|
/*
|
|
VG_(printf)("%d (%d) %d 0x%x\n",
|
|
i, si->loctab[i+1].confident,
|
|
si->loctab[i+1].size, si->loctab[i+1].addr );
|
|
*/
|
|
/* No zero-sized symbols. */
|
|
vg_assert(si->loctab[i].size > 0);
|
|
/* In order. */
|
|
vg_assert(si->loctab[i].addr < si->loctab[i+1].addr);
|
|
/* No overlaps. */
|
|
vg_assert(si->loctab[i].addr + si->loctab[i].size - 1
|
|
< si->loctab[i+1].addr);
|
|
}
|
|
# undef SWAP
|
|
}
|
|
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- Read info from a .so/exe file. ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
/* Read the symbols from the object/exe specified by the SegInfo into
|
|
the tables within the supplied SegInfo. */
|
|
static
|
|
Bool vg_read_lib_symbols ( SegInfo* si )
|
|
{
|
|
Elf32_Ehdr* ehdr; /* The ELF header */
|
|
Elf32_Shdr* shdr; /* The section table */
|
|
UChar* sh_strtab; /* The section table's string table */
|
|
UChar* stab; /* The .stab table */
|
|
UChar* stabstr; /* The .stab string table */
|
|
UChar* dwarf2; /* The DWARF2 location info table */
|
|
Int stab_sz; /* Size in bytes of the .stab table */
|
|
Int stabstr_sz; /* Size in bytes of the .stab string table */
|
|
Int dwarf2_sz; /* Size in bytes of the DWARF2 srcloc table*/
|
|
Int fd;
|
|
Int i;
|
|
Bool ok;
|
|
Addr oimage;
|
|
UInt n_oimage;
|
|
struct vki_stat stat_buf;
|
|
|
|
oimage = (Addr)NULL;
|
|
if (VG_(clo_verbosity) > 1)
|
|
VG_(message)(Vg_UserMsg, "Reading syms from %s", si->filename );
|
|
|
|
/* mmap the object image aboard, so that we can read symbols and
|
|
line number info out of it. It will be munmapped immediately
|
|
thereafter; it is only aboard transiently. */
|
|
|
|
i = VG_(stat)(si->filename, &stat_buf);
|
|
if (i != 0) {
|
|
VG_(symerr)("Can't stat .so/.exe (to determine its size)?!");
|
|
return False;
|
|
}
|
|
n_oimage = stat_buf.st_size;
|
|
|
|
fd = VG_(open)(si->filename, VKI_O_RDONLY, 0);
|
|
if (fd < 0) {
|
|
VG_(symerr)("Can't open .so/.exe to read symbols?!");
|
|
return False;
|
|
}
|
|
|
|
oimage = (Addr)VG_(mmap)( NULL, n_oimage,
|
|
VKI_PROT_READ, VKI_MAP_PRIVATE, fd, 0 );
|
|
if (oimage == ((Addr)(-1))) {
|
|
VG_(message)(Vg_UserMsg,
|
|
"mmap failed on %s", si->filename );
|
|
VG_(close)(fd);
|
|
return False;
|
|
}
|
|
|
|
VG_(close)(fd);
|
|
|
|
/* Ok, the object image is safely in oimage[0 .. n_oimage-1].
|
|
Now verify that it is a valid ELF .so or executable image.
|
|
*/
|
|
ok = (n_oimage >= sizeof(Elf32_Ehdr));
|
|
ehdr = (Elf32_Ehdr*)oimage;
|
|
|
|
if (ok) {
|
|
ok &= (ehdr->e_ident[EI_MAG0] == 0x7F
|
|
&& ehdr->e_ident[EI_MAG1] == 'E'
|
|
&& ehdr->e_ident[EI_MAG2] == 'L'
|
|
&& ehdr->e_ident[EI_MAG3] == 'F');
|
|
ok &= (ehdr->e_ident[EI_CLASS] == ELFCLASS32
|
|
&& ehdr->e_ident[EI_DATA] == ELFDATA2LSB
|
|
&& ehdr->e_ident[EI_VERSION] == EV_CURRENT);
|
|
ok &= (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN);
|
|
ok &= (ehdr->e_machine == EM_386);
|
|
ok &= (ehdr->e_version == EV_CURRENT);
|
|
ok &= (ehdr->e_shstrndx != SHN_UNDEF);
|
|
ok &= (ehdr->e_shoff != 0 && ehdr->e_shnum != 0);
|
|
ok &= (ehdr->e_phoff != 0 && ehdr->e_phnum != 0);
|
|
}
|
|
|
|
if (!ok) {
|
|
VG_(symerr)("Invalid ELF header, or missing stringtab/sectiontab.");
|
|
VG_(munmap) ( (void*)oimage, n_oimage );
|
|
return False;
|
|
}
|
|
|
|
/* Walk the LOAD headers in the phdr and update the SegInfo to
|
|
include them all, so that this segment also contains data and
|
|
bss memory. Also computes correct symbol offset value for this
|
|
ELF file. */
|
|
if (ehdr->e_phoff + ehdr->e_phnum*sizeof(Elf32_Phdr) > n_oimage) {
|
|
VG_(symerr)("ELF program header is beyond image end?!");
|
|
VG_(munmap) ( (void*)oimage, n_oimage );
|
|
return False;
|
|
}
|
|
{
|
|
Bool offset_set = False;
|
|
Elf32_Addr prev_addr = 0;
|
|
|
|
si->offset = 0;
|
|
|
|
for(i = 0; i < ehdr->e_phnum; i++) {
|
|
Elf32_Phdr *o_phdr;
|
|
Elf32_Addr mapped, mapped_end;
|
|
|
|
o_phdr = &((Elf32_Phdr *)(oimage + ehdr->e_phoff))[i];
|
|
|
|
if (o_phdr->p_type != PT_LOAD)
|
|
continue;
|
|
|
|
if (!offset_set) {
|
|
offset_set = True;
|
|
si->offset = si->start - o_phdr->p_vaddr;
|
|
}
|
|
|
|
if (o_phdr->p_vaddr < prev_addr) {
|
|
VG_(symerr)("ELF Phdrs are out of order!?");
|
|
VG_(munmap) ( (void*)oimage, n_oimage );
|
|
return False;
|
|
}
|
|
prev_addr = o_phdr->p_vaddr;
|
|
|
|
mapped = o_phdr->p_vaddr + si->offset;
|
|
mapped_end = mapped + o_phdr->p_memsz;
|
|
|
|
if (si->data_start == 0 &&
|
|
(o_phdr->p_flags & (PF_R|PF_W|PF_X)) == (PF_R|PF_W)) {
|
|
si->data_start = mapped;
|
|
si->data_size = o_phdr->p_filesz;
|
|
si->bss_start = mapped + o_phdr->p_filesz;
|
|
if (o_phdr->p_memsz > o_phdr->p_filesz)
|
|
si->bss_size = o_phdr->p_memsz - o_phdr->p_filesz;
|
|
else
|
|
si->bss_size = 0;
|
|
}
|
|
|
|
mapped = mapped & ~(VKI_BYTES_PER_PAGE-1);
|
|
mapped_end = (mapped_end + VKI_BYTES_PER_PAGE - 1) & ~(VKI_BYTES_PER_PAGE-1);
|
|
|
|
if (VG_(needs).data_syms &&
|
|
(mapped >= si->start && mapped <= (si->start+si->size)) &&
|
|
(mapped_end > (si->start+si->size))) {
|
|
UInt newsz = mapped_end - si->start;
|
|
if (newsz > si->size) {
|
|
if (0)
|
|
VG_(printf)("extending mapping %p..%p %d -> ..%p %d\n",
|
|
si->start, si->start+si->size, si->size,
|
|
si->start+newsz, newsz);
|
|
si->size = newsz;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (VG_(clo_trace_symtab))
|
|
VG_(printf)(
|
|
"shoff = %d, shnum = %d, size = %d, n_vg_oimage = %d\n",
|
|
ehdr->e_shoff, ehdr->e_shnum, sizeof(Elf32_Shdr), n_oimage );
|
|
|
|
if (ehdr->e_shoff + ehdr->e_shnum*sizeof(Elf32_Shdr) > n_oimage) {
|
|
VG_(symerr)("ELF section header is beyond image end?!");
|
|
VG_(munmap) ( (void*)oimage, n_oimage );
|
|
return False;
|
|
}
|
|
|
|
shdr = (Elf32_Shdr*)(oimage + ehdr->e_shoff);
|
|
sh_strtab = (UChar*)(oimage + shdr[ehdr->e_shstrndx].sh_offset);
|
|
|
|
/* try and read the object's symbol table */
|
|
{
|
|
UChar* o_strtab = NULL;
|
|
Elf32_Sym* o_symtab = NULL;
|
|
UChar* o_dynstr = NULL;
|
|
Elf32_Sym* o_dynsym = NULL;
|
|
UInt o_strtab_sz = 0;
|
|
UInt o_symtab_sz = 0;
|
|
UInt o_dynstr_sz = 0;
|
|
UInt o_dynsym_sz = 0;
|
|
|
|
UChar* o_got = NULL;
|
|
UChar* o_plt = NULL;
|
|
UInt o_got_sz = 0;
|
|
UInt o_plt_sz = 0;
|
|
|
|
Bool snaffle_it;
|
|
Addr sym_addr;
|
|
|
|
/* find the .stabstr and .stab sections */
|
|
for (i = 0; i < ehdr->e_shnum; i++) {
|
|
|
|
if (0 == VG_(strcmp)(".dynsym",sh_strtab + shdr[i].sh_name)) {
|
|
o_dynsym = (Elf32_Sym*)(oimage + shdr[i].sh_offset);
|
|
o_dynsym_sz = shdr[i].sh_size;
|
|
vg_assert((o_dynsym_sz % sizeof(Elf32_Sym)) == 0);
|
|
/* check image overrun here */
|
|
}
|
|
if (0 == VG_(strcmp)(".dynstr",sh_strtab + shdr[i].sh_name)) {
|
|
o_dynstr = (UChar*)(oimage + shdr[i].sh_offset);
|
|
o_dynstr_sz = shdr[i].sh_size;
|
|
/* check image overrun here */
|
|
}
|
|
|
|
if (0 == VG_(strcmp)(".symtab",sh_strtab + shdr[i].sh_name)) {
|
|
o_symtab = (Elf32_Sym*)(oimage + shdr[i].sh_offset);
|
|
o_symtab_sz = shdr[i].sh_size;
|
|
vg_assert((o_symtab_sz % sizeof(Elf32_Sym)) == 0);
|
|
/* check image overrun here */
|
|
}
|
|
if (0 == VG_(strcmp)(".strtab",sh_strtab + shdr[i].sh_name)) {
|
|
o_strtab = (UChar*)(oimage + shdr[i].sh_offset);
|
|
o_strtab_sz = shdr[i].sh_size;
|
|
/* check image overrun here */
|
|
}
|
|
|
|
/* find out where the .got and .plt sections will be in the
|
|
executable image, not in the object image transiently loaded.
|
|
*/
|
|
if (0 == VG_(strcmp)(".got",sh_strtab + shdr[i].sh_name)) {
|
|
o_got = (UChar*)(si->offset
|
|
+ shdr[i].sh_addr);
|
|
o_got_sz = shdr[i].sh_size;
|
|
si->got_start= (Addr)o_got;
|
|
si->got_size = o_got_sz;
|
|
/* check image overrun here */
|
|
}
|
|
if (0 == VG_(strcmp)(".plt",sh_strtab + shdr[i].sh_name)) {
|
|
o_plt = (UChar*)(si->offset
|
|
+ shdr[i].sh_addr);
|
|
o_plt_sz = shdr[i].sh_size;
|
|
si->plt_start= (Addr)o_plt;
|
|
si->plt_size = o_plt_sz;
|
|
/* check image overrun here */
|
|
}
|
|
|
|
}
|
|
|
|
if (VG_(clo_trace_symtab)) {
|
|
if (o_plt) VG_(printf)( "PLT: %p .. %p\n",
|
|
o_plt, o_plt + o_plt_sz - 1 );
|
|
if (o_got) VG_(printf)( "GOT: %p .. %p\n",
|
|
o_got, o_got + o_got_sz - 1 );
|
|
}
|
|
|
|
if (o_strtab == NULL || o_symtab == NULL) {
|
|
VG_(symerr)(" object doesn't have a symbol table");
|
|
}
|
|
if (o_dynstr == NULL || o_dynsym == NULL) {
|
|
VG_(symerr)(" object doesn't have a dynamic symbol table");
|
|
}
|
|
|
|
while (True) {
|
|
if (o_strtab == NULL || o_symtab == NULL) {
|
|
|
|
o_strtab = o_dynstr;
|
|
o_symtab = o_dynsym;
|
|
o_strtab_sz = o_dynstr_sz;
|
|
o_symtab_sz = o_dynsym_sz;
|
|
|
|
if (o_strtab == NULL || o_symtab == NULL) break;
|
|
|
|
o_dynstr = NULL;
|
|
o_dynsym = NULL;
|
|
|
|
if (VG_(clo_trace_symtab))
|
|
VG_(printf)("Reading dynamic symbol table (%d entries)\n",
|
|
o_symtab_sz/sizeof(Elf32_Sym) );
|
|
}
|
|
else if (VG_(clo_trace_symtab)) {
|
|
|
|
VG_(printf)("Reading symbol table (%d entries)\n",
|
|
o_symtab_sz/sizeof(Elf32_Sym) );
|
|
}
|
|
|
|
/* Perhaps should start at i = 1; ELF docs suggest that entry
|
|
0 always denotes `unknown symbol'. */
|
|
for (i = 1; i < (Int)(o_symtab_sz/sizeof(Elf32_Sym)); i++){
|
|
# if 1
|
|
if (VG_(clo_trace_symtab)) {
|
|
VG_(printf)("raw symbol [%d]: ", i);
|
|
switch (ELF32_ST_BIND(o_symtab[i].st_info)) {
|
|
case STB_LOCAL: VG_(printf)("LOC "); break;
|
|
case STB_GLOBAL: VG_(printf)("GLO "); break;
|
|
case STB_WEAK: VG_(printf)("WEA "); break;
|
|
case STB_LOPROC: VG_(printf)("lop "); break;
|
|
case STB_HIPROC: VG_(printf)("hip "); break;
|
|
default: VG_(printf)("??? "); break;
|
|
}
|
|
switch (ELF32_ST_TYPE(o_symtab[i].st_info)) {
|
|
case STT_NOTYPE: VG_(printf)("NOT "); break;
|
|
case STT_OBJECT: VG_(printf)("OBJ "); break;
|
|
case STT_FUNC: VG_(printf)("FUN "); break;
|
|
case STT_SECTION: VG_(printf)("SEC "); break;
|
|
case STT_FILE: VG_(printf)("FIL "); break;
|
|
case STT_LOPROC: VG_(printf)("lop "); break;
|
|
case STT_HIPROC: VG_(printf)("hip "); break;
|
|
default: VG_(printf)("??? "); break;
|
|
}
|
|
VG_(printf)(
|
|
": value %p, size %d, name %s\n",
|
|
si->offset+(UChar*)o_symtab[i].st_value,
|
|
o_symtab[i].st_size,
|
|
o_symtab[i].st_name
|
|
? ((Char*)o_strtab+o_symtab[i].st_name)
|
|
: (Char*)"NONAME");
|
|
}
|
|
# endif
|
|
|
|
/* Figure out if we're interested in the symbol.
|
|
Firstly, is it of the right flavour?
|
|
*/
|
|
snaffle_it
|
|
= ( (ELF32_ST_BIND(o_symtab[i].st_info) == STB_GLOBAL ||
|
|
ELF32_ST_BIND(o_symtab[i].st_info) == STB_LOCAL ||
|
|
ELF32_ST_BIND(o_symtab[i].st_info) == STB_WEAK)
|
|
&&
|
|
(ELF32_ST_TYPE(o_symtab[i].st_info) == STT_FUNC ||
|
|
(VG_(needs).data_syms
|
|
&& ELF32_ST_TYPE(o_symtab[i].st_info) == STT_OBJECT))
|
|
);
|
|
|
|
/* Secondly, if it's apparently in a GOT or PLT, it's really
|
|
a reference to a symbol defined elsewhere, so ignore it.
|
|
*/
|
|
sym_addr = si->offset
|
|
+ (UInt)o_symtab[i].st_value;
|
|
if (o_got != NULL
|
|
&& sym_addr >= (Addr)o_got
|
|
&& sym_addr < (Addr)(o_got+o_got_sz)) {
|
|
snaffle_it = False;
|
|
if (VG_(clo_trace_symtab)) {
|
|
VG_(printf)( "in GOT: %s\n",
|
|
o_strtab+o_symtab[i].st_name);
|
|
}
|
|
}
|
|
if (o_plt != NULL
|
|
&& sym_addr >= (Addr)o_plt
|
|
&& sym_addr < (Addr)(o_plt+o_plt_sz)) {
|
|
snaffle_it = False;
|
|
if (VG_(clo_trace_symtab)) {
|
|
VG_(printf)( "in PLT: %s\n",
|
|
o_strtab+o_symtab[i].st_name);
|
|
}
|
|
}
|
|
|
|
/* Don't bother if nameless, or zero-sized. */
|
|
if (snaffle_it
|
|
&& (o_symtab[i].st_name == (Elf32_Word)NULL
|
|
|| /* VG_(strlen)(o_strtab+o_symtab[i].st_name) == 0 */
|
|
/* equivalent but cheaper ... */
|
|
* ((UChar*)(o_strtab+o_symtab[i].st_name)) == 0
|
|
|| o_symtab[i].st_size == 0)) {
|
|
snaffle_it = False;
|
|
if (VG_(clo_trace_symtab)) {
|
|
VG_(printf)( "size=0: %s\n",
|
|
o_strtab+o_symtab[i].st_name);
|
|
}
|
|
}
|
|
|
|
# if 0
|
|
/* Avoid _dl_ junk. (Why?) */
|
|
/* 01-02-24: disabled until I find out if it really helps. */
|
|
if (snaffle_it
|
|
&& (VG_(strncmp)("_dl_", o_strtab+o_symtab[i].st_name, 4) == 0
|
|
|| VG_(strncmp)("_r_debug",
|
|
o_strtab+o_symtab[i].st_name, 8) == 0)) {
|
|
snaffle_it = False;
|
|
if (VG_(clo_trace_symtab)) {
|
|
VG_(printf)( "_dl_ junk: %s\n",
|
|
o_strtab+o_symtab[i].st_name);
|
|
}
|
|
}
|
|
# endif
|
|
|
|
/* This seems to significantly reduce the number of junk
|
|
symbols, and particularly reduces the number of
|
|
overlapping address ranges. Don't ask me why ... */
|
|
if (snaffle_it && (Int)o_symtab[i].st_value == 0) {
|
|
snaffle_it = False;
|
|
if (VG_(clo_trace_symtab)) {
|
|
VG_(printf)( "valu=0: %s\n",
|
|
o_strtab+o_symtab[i].st_name);
|
|
}
|
|
}
|
|
|
|
/* If no part of the symbol falls within the mapped range,
|
|
ignore it. */
|
|
if (sym_addr+o_symtab[i].st_size <= si->start
|
|
|| sym_addr >= si->start+si->size) {
|
|
snaffle_it = False;
|
|
}
|
|
|
|
if (snaffle_it) {
|
|
/* it's an interesting symbol; record ("snaffle") it. */
|
|
RiSym sym;
|
|
Char* t0 = o_symtab[i].st_name
|
|
? (Char*)(o_strtab+o_symtab[i].st_name)
|
|
: (Char*)"NONAME";
|
|
Char *name = VG_(addStr) ( si, t0, -1 );
|
|
vg_assert(name != NULL
|
|
/* && 0==VG_(strcmp)(t0,&vg_strtab[nmoff]) */ );
|
|
vg_assert( (Int)o_symtab[i].st_value >= 0);
|
|
/* VG_(printf)("%p + %d: %p %s\n", si->start,
|
|
(Int)o_symtab[i].st_value, sym_addr, t0 ); */
|
|
sym.addr = sym_addr;
|
|
sym.size = o_symtab[i].st_size;
|
|
sym.name = name;
|
|
addSym ( si, &sym );
|
|
}
|
|
}
|
|
/* If there's dynamic symbol info, force reading in next loop */
|
|
o_strtab = NULL;
|
|
o_symtab = NULL;
|
|
}
|
|
}
|
|
|
|
/* Reading of the stabs and/or dwarf2 debug format information, if
|
|
any. */
|
|
stabstr = NULL;
|
|
stab = NULL;
|
|
dwarf2 = NULL;
|
|
stabstr_sz = 0;
|
|
stab_sz = 0;
|
|
dwarf2_sz = 0;
|
|
|
|
/* find the .stabstr / .stab / .debug_line sections */
|
|
for (i = 0; i < ehdr->e_shnum; i++) {
|
|
if (0 == VG_(strcmp)(".stab",sh_strtab + shdr[i].sh_name)) {
|
|
stab = (UChar*)(oimage + shdr[i].sh_offset);
|
|
stab_sz = shdr[i].sh_size;
|
|
}
|
|
if (0 == VG_(strcmp)(".stabstr",sh_strtab + shdr[i].sh_name)) {
|
|
stabstr = (UChar*)(oimage + shdr[i].sh_offset);
|
|
stabstr_sz = shdr[i].sh_size;
|
|
}
|
|
if (0 == VG_(strcmp)(".debug_line",sh_strtab + shdr[i].sh_name)) {
|
|
dwarf2 = (UChar *)(oimage + shdr[i].sh_offset);
|
|
dwarf2_sz = shdr[i].sh_size;
|
|
}
|
|
}
|
|
|
|
if ((stab == NULL || stabstr == NULL) && dwarf2 == NULL) {
|
|
VG_(symerr)(" object doesn't have any debug info");
|
|
VG_(munmap) ( (void*)oimage, n_oimage );
|
|
return False;
|
|
}
|
|
|
|
if ( stab_sz + (UChar*)stab > n_oimage + (UChar*)oimage
|
|
|| stabstr_sz + (UChar*)stabstr
|
|
> n_oimage + (UChar*)oimage ) {
|
|
VG_(symerr)(" ELF (stabs) debug data is beyond image end?!");
|
|
VG_(munmap) ( (void*)oimage, n_oimage );
|
|
return False;
|
|
}
|
|
|
|
if ( dwarf2_sz + (UChar*)dwarf2 > n_oimage + (UChar*)oimage ) {
|
|
VG_(symerr)(" ELF (dwarf2) debug data is beyond image end?!");
|
|
VG_(munmap) ( (void*)oimage, n_oimage );
|
|
return False;
|
|
}
|
|
|
|
/* Looks plausible. Go on and read debug data. */
|
|
if (stab != NULL && stabstr != NULL) {
|
|
VG_(read_debuginfo_stabs) ( si, stab, stab_sz, stabstr, stabstr_sz );
|
|
}
|
|
|
|
if (dwarf2 != NULL) {
|
|
VG_(read_debuginfo_dwarf2) ( si, dwarf2, dwarf2_sz );
|
|
}
|
|
|
|
/* Last, but not least, heave the oimage back overboard. */
|
|
VG_(munmap) ( (void*)oimage, n_oimage );
|
|
|
|
return True;
|
|
}
|
|
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- Main entry point for symbols table reading. ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
/* The root structure for the entire symbol table system. It is a
|
|
linked list of SegInfos. Note that this entire mechanism assumes
|
|
that what we read from /proc/self/maps doesn't contain overlapping
|
|
address ranges, and as a result the SegInfos in this list describe
|
|
disjoint address ranges.
|
|
*/
|
|
static SegInfo* segInfo = NULL;
|
|
|
|
void VG_(read_seg_symbols) ( Addr start, UInt size,
|
|
Char rr, Char ww, Char xx,
|
|
UInt foffset, UChar* filename )
|
|
{
|
|
SegInfo* si;
|
|
|
|
/* Stay sane ... */
|
|
if (size == 0)
|
|
return;
|
|
|
|
/* We're only interested in collecting symbols in executable
|
|
segments which are associated with a real file. Hence: */
|
|
if (filename == NULL || xx != 'x')
|
|
return;
|
|
if (0 == VG_(strcmp)(filename, "/dev/zero"))
|
|
return;
|
|
if (foffset != 0)
|
|
return;
|
|
|
|
VGP_PUSHCC(VgpReadSyms);
|
|
|
|
/* Perhaps we already have this one? If so, skip. */
|
|
for (si = segInfo; si != NULL; si = si->next) {
|
|
/*
|
|
if (0==VG_(strcmp)(si->filename, filename))
|
|
VG_(printf)("same fnames: %c%c%c (%p, %d) (%p, %d) %s\n",
|
|
rr,ww,xx,si->start,si->size,start,size,filename);
|
|
*/
|
|
/* For some reason the observed size of a mapping can change, so
|
|
we don't use that to determine uniqueness. */
|
|
if (si->start == start
|
|
/* && si->size == size */
|
|
&& 0==VG_(strcmp)(si->filename, filename))
|
|
{
|
|
VGP_POPCC(VgpReadSyms);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/* Get the record initialised right. */
|
|
si = VG_(arena_malloc)(VG_AR_SYMTAB, sizeof(SegInfo));
|
|
|
|
VG_(memset)(si, 0, sizeof(*si));
|
|
si->start = start;
|
|
si->size = size;
|
|
si->foffset = foffset;
|
|
si->filename = VG_(arena_malloc)(VG_AR_SYMTAB, 1 + VG_(strlen)(filename));
|
|
VG_(strcpy)(si->filename, filename);
|
|
|
|
si->symtab = NULL;
|
|
si->symtab_size = si->symtab_used = 0;
|
|
si->loctab = NULL;
|
|
si->loctab_size = si->loctab_used = 0;
|
|
si->strchunks = NULL;
|
|
si->scopetab = NULL;
|
|
si->scopetab_size = si->scopetab_used = 0;
|
|
|
|
si->stab_typetab = NULL;
|
|
|
|
si->plt_start = si->plt_size = 0;
|
|
si->got_start = si->got_size = 0;
|
|
si->data_start = si->data_size = 0;
|
|
si->bss_start = si->bss_size = 0;
|
|
|
|
/* And actually fill it up. */
|
|
if (!vg_read_lib_symbols ( si ) && 0) {
|
|
/* XXX this interacts badly with the prevN optimization in
|
|
addStr(). Since this frees the si, the si pointer value can
|
|
be recycled, which confuses the curr_si == si test. For now,
|
|
this code is disabled, and everything is included in the
|
|
segment list, even if it is a bad ELF file. Ironically,
|
|
running this under valgrind itself hides the problem, because
|
|
it doesn't recycle pointers... */
|
|
freeSegInfo( si );
|
|
} else {
|
|
si->next = segInfo;
|
|
segInfo = si;
|
|
|
|
canonicaliseSymtab ( si );
|
|
canonicaliseLoctab ( si );
|
|
canonicaliseScopetab ( si );
|
|
}
|
|
VGP_POPCC(VgpReadSyms);
|
|
}
|
|
|
|
|
|
/* This one really is the Head Honcho. Update the symbol tables to
|
|
reflect the current state of /proc/self/maps. Rather than re-read
|
|
everything, just read the entries which are not already in segInfo.
|
|
So we can call here repeatedly, after every mmap of a non-anonymous
|
|
segment with execute permissions, for example, to pick up new
|
|
libraries as they are dlopen'd. Conversely, when the client does
|
|
munmap(), vg_symtab_notify_munmap() throws away any symbol tables
|
|
which happen to correspond to the munmap()d area. */
|
|
void VG_(read_all_symbols) ( void )
|
|
{
|
|
/* 9 July 2003: In order to work around PLT bypassing in
|
|
glibc-2.3.2 (see below VG_(setup_code_redirect_table)), we need
|
|
to load debug info regardless of the skin, unfortunately. */
|
|
VG_(read_procselfmaps) ( );
|
|
VG_(parse_procselfmaps) ( VG_(read_seg_symbols) );
|
|
}
|
|
|
|
/* When an munmap() call happens, check to see whether it corresponds
|
|
to a segment for a .so, and if so discard the relevant SegInfo.
|
|
This might not be a very clever idea from the point of view of
|
|
accuracy of error messages, but we need to do it in order to
|
|
maintain the no-overlapping invariant.
|
|
*/
|
|
void VG_(unload_symbols) ( Addr start, UInt length )
|
|
{
|
|
SegInfo *prev, *curr;
|
|
|
|
prev = NULL;
|
|
curr = segInfo;
|
|
while (True) {
|
|
if (curr == NULL) break;
|
|
if (start == curr->start) break;
|
|
prev = curr;
|
|
curr = curr->next;
|
|
}
|
|
if (curr == NULL) {
|
|
VGP_POPCC(VgpReadSyms);
|
|
return;
|
|
}
|
|
|
|
VG_(message)(Vg_UserMsg,
|
|
"discard syms in %s due to munmap()",
|
|
curr->filename ? curr->filename : (Char *)"???");
|
|
|
|
vg_assert(prev == NULL || prev->next == curr);
|
|
|
|
if (prev == NULL) {
|
|
segInfo = curr->next;
|
|
} else {
|
|
prev->next = curr->next;
|
|
}
|
|
|
|
freeSegInfo(curr);
|
|
return;
|
|
}
|
|
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- Use of symbol table & location info to create ---*/
|
|
/*--- plausible-looking stack dumps. ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
/* Find a symbol-table index containing the specified pointer, or -1
|
|
if not found. Binary search. */
|
|
|
|
static Int search_one_symtab ( SegInfo* si, Addr ptr,
|
|
Bool match_anywhere_in_fun )
|
|
{
|
|
Addr a_mid_lo, a_mid_hi;
|
|
Int mid, size,
|
|
lo = 0,
|
|
hi = si->symtab_used-1;
|
|
while (True) {
|
|
/* current unsearched space is from lo to hi, inclusive. */
|
|
if (lo > hi) return -1; /* not found */
|
|
mid = (lo + hi) / 2;
|
|
a_mid_lo = si->symtab[mid].addr;
|
|
size = ( match_anywhere_in_fun
|
|
? si->symtab[mid].size
|
|
: 1);
|
|
a_mid_hi = ((Addr)si->symtab[mid].addr) + size - 1;
|
|
|
|
if (ptr < a_mid_lo) { hi = mid-1; continue; }
|
|
if (ptr > a_mid_hi) { lo = mid+1; continue; }
|
|
vg_assert(ptr >= a_mid_lo && ptr <= a_mid_hi);
|
|
return mid;
|
|
}
|
|
}
|
|
|
|
|
|
/* SLOW (Linear search). Try and map a symbol name to an address.
|
|
Since this is searching in the direction opposite to which the
|
|
table is designed we have no option but to do a complete linear
|
|
scan of the table. Returns NULL if not found. */
|
|
|
|
static Addr reverse_search_one_symtab ( SegInfo* si,
|
|
Char* name )
|
|
{
|
|
UInt i;
|
|
for (i = 0; i < si->symtab_used; i++) {
|
|
if (0)
|
|
VG_(printf)("%p %s\n", si->symtab[i].addr, si->symtab[i].name);
|
|
if (0 == VG_(strcmp)(name, si->symtab[i].name))
|
|
return si->symtab[i].addr;
|
|
}
|
|
return (Addr)NULL;
|
|
}
|
|
|
|
|
|
/* Search all symtabs that we know about to locate ptr. If found, set
|
|
*psi to the relevant SegInfo, and *symno to the symtab entry number
|
|
within that. If not found, *psi is set to NULL. */
|
|
|
|
static void search_all_symtabs ( Addr ptr, /*OUT*/SegInfo** psi,
|
|
/*OUT*/Int* symno,
|
|
Bool match_anywhere_in_fun )
|
|
{
|
|
Int sno;
|
|
SegInfo* si;
|
|
|
|
VGP_PUSHCC(VgpSearchSyms);
|
|
|
|
for (si = segInfo; si != NULL; si = si->next) {
|
|
if (si->start <= ptr && ptr < si->start+si->size) {
|
|
sno = search_one_symtab ( si, ptr, match_anywhere_in_fun );
|
|
if (sno == -1) goto not_found;
|
|
*symno = sno;
|
|
*psi = si;
|
|
VGP_POPCC(VgpSearchSyms);
|
|
return;
|
|
}
|
|
}
|
|
not_found:
|
|
*psi = NULL;
|
|
VGP_POPCC(VgpSearchSyms);
|
|
}
|
|
|
|
|
|
/* Find a location-table index containing the specified pointer, or -1
|
|
if not found. Binary search. */
|
|
|
|
static Int search_one_loctab ( SegInfo* si, Addr ptr )
|
|
{
|
|
Addr a_mid_lo, a_mid_hi;
|
|
Int mid,
|
|
lo = 0,
|
|
hi = si->loctab_used-1;
|
|
while (True) {
|
|
/* current unsearched space is from lo to hi, inclusive. */
|
|
if (lo > hi) return -1; /* not found */
|
|
mid = (lo + hi) / 2;
|
|
a_mid_lo = si->loctab[mid].addr;
|
|
a_mid_hi = ((Addr)si->loctab[mid].addr) + si->loctab[mid].size - 1;
|
|
|
|
if (ptr < a_mid_lo) { hi = mid-1; continue; }
|
|
if (ptr > a_mid_hi) { lo = mid+1; continue; }
|
|
vg_assert(ptr >= a_mid_lo && ptr <= a_mid_hi);
|
|
return mid;
|
|
}
|
|
}
|
|
|
|
|
|
/* Search all loctabs that we know about to locate ptr. If found, set
|
|
*psi to the relevant SegInfo, and *locno to the loctab entry number
|
|
within that. If not found, *psi is set to NULL.
|
|
*/
|
|
static void search_all_loctabs ( Addr ptr, /*OUT*/SegInfo** psi,
|
|
/*OUT*/Int* locno )
|
|
{
|
|
Int lno;
|
|
SegInfo* si;
|
|
|
|
VGP_PUSHCC(VgpSearchSyms);
|
|
|
|
for (si = segInfo; si != NULL; si = si->next) {
|
|
if (si->start <= ptr && ptr < si->start+si->size) {
|
|
lno = search_one_loctab ( si, ptr );
|
|
if (lno == -1) goto not_found;
|
|
*locno = lno;
|
|
*psi = si;
|
|
VGP_POPCC(VgpSearchSyms);
|
|
return;
|
|
}
|
|
}
|
|
not_found:
|
|
*psi = NULL;
|
|
VGP_POPCC(VgpSearchSyms);
|
|
}
|
|
|
|
|
|
/* Find a scope-table index containing the specified pointer, or -1
|
|
if not found. Binary search. */
|
|
|
|
static Int search_one_scopetab ( SegInfo* si, Addr ptr )
|
|
{
|
|
Addr a_mid_lo, a_mid_hi;
|
|
Int mid,
|
|
lo = 0,
|
|
hi = si->scopetab_used-1;
|
|
while (True) {
|
|
/* current unsearched space is from lo to hi, inclusive. */
|
|
if (lo > hi) return -1; /* not found */
|
|
mid = (lo + hi) / 2;
|
|
a_mid_lo = si->scopetab[mid].addr;
|
|
a_mid_hi = ((Addr)si->scopetab[mid].addr) + si->scopetab[mid].size - 1;
|
|
|
|
if (ptr < a_mid_lo) { hi = mid-1; continue; }
|
|
if (ptr > a_mid_hi) { lo = mid+1; continue; }
|
|
vg_assert(ptr >= a_mid_lo && ptr <= a_mid_hi);
|
|
return mid;
|
|
}
|
|
}
|
|
|
|
|
|
/* Search all scopetabs that we know about to locate ptr. If found, set
|
|
*psi to the relevant SegInfo, and *locno to the scopetab entry number
|
|
within that. If not found, *psi is set to NULL.
|
|
*/
|
|
static void search_all_scopetabs ( Addr ptr,
|
|
/*OUT*/SegInfo** psi,
|
|
/*OUT*/Int* scopeno )
|
|
{
|
|
Int scno;
|
|
SegInfo* si;
|
|
|
|
VGP_PUSHCC(VgpSearchSyms);
|
|
|
|
for (si = segInfo; si != NULL; si = si->next) {
|
|
if (si->start <= ptr && ptr < si->start+si->size) {
|
|
scno = search_one_scopetab ( si, ptr );
|
|
if (scno == -1) goto not_found;
|
|
*scopeno = scno;
|
|
*psi = si;
|
|
VGP_POPCC(VgpSearchSyms);
|
|
return;
|
|
}
|
|
}
|
|
not_found:
|
|
*psi = NULL;
|
|
VGP_POPCC(VgpSearchSyms);
|
|
}
|
|
|
|
/* The whole point of this whole big deal: map a code address to a
|
|
plausible symbol name. Returns False if no idea; otherwise True.
|
|
Caller supplies buf and nbuf. If demangle is False, don't do
|
|
demangling, regardless of vg_clo_demangle -- probably because the
|
|
call has come from vg_what_fn_or_object_is_this. */
|
|
static
|
|
Bool get_fnname ( Bool demangle, Addr a, Char* buf, Int nbuf,
|
|
Bool match_anywhere_in_fun, Bool show_offset)
|
|
{
|
|
SegInfo* si;
|
|
Int sno;
|
|
Int offset;
|
|
|
|
search_all_symtabs ( a, &si, &sno, match_anywhere_in_fun );
|
|
if (si == NULL)
|
|
return False;
|
|
if (demangle) {
|
|
VG_(demangle) ( si->symtab[sno].name, buf, nbuf );
|
|
} else {
|
|
VG_(strncpy_safely)
|
|
( buf, si->symtab[sno].name, nbuf );
|
|
}
|
|
|
|
offset = a - si->symtab[sno].addr;
|
|
if (show_offset && offset != 0) {
|
|
Char buf2[12];
|
|
Char* symend = buf + VG_(strlen)(buf);
|
|
Char* end = buf + nbuf;
|
|
Int len;
|
|
|
|
len = VG_(sprintf)(buf2, "%c%d",
|
|
offset < 0 ? '-' : '+',
|
|
offset < 0 ? -offset : offset);
|
|
vg_assert(len < (Int)sizeof(buf2));
|
|
|
|
if (len < (end - symend)) {
|
|
Char *cp = buf2;
|
|
VG_(memcpy)(symend, cp, len+1);
|
|
}
|
|
}
|
|
|
|
return True;
|
|
}
|
|
|
|
/* This is available to skins... always demangle C++ names,
|
|
match anywhere in function, but don't show offsets. */
|
|
Bool VG_(get_fnname) ( Addr a, Char* buf, Int nbuf )
|
|
{
|
|
return get_fnname ( /*demangle*/True, a, buf, nbuf,
|
|
/*match_anywhere_in_fun*/True,
|
|
/*show offset?*/False );
|
|
}
|
|
|
|
/* This is available to skins... always demangle C++ names,
|
|
match anywhere in function, and show offset if nonzero. */
|
|
Bool VG_(get_fnname_w_offset) ( Addr a, Char* buf, Int nbuf )
|
|
{
|
|
return get_fnname ( /*demangle*/True, a, buf, nbuf,
|
|
/*match_anywhere_in_fun*/True,
|
|
/*show offset?*/True );
|
|
}
|
|
|
|
/* This is available to skins... always demangle C++ names,
|
|
only succeed if 'a' matches first instruction of function,
|
|
and don't show offsets. */
|
|
Bool VG_(get_fnname_if_entry) ( Addr a, Char* buf, Int nbuf )
|
|
{
|
|
return get_fnname ( /*demangle*/True, a, buf, nbuf,
|
|
/*match_anywhere_in_fun*/False,
|
|
/*show offset?*/False );
|
|
}
|
|
|
|
/* This is only available to core... don't demangle C++ names,
|
|
match anywhere in function, and don't show offsets. */
|
|
Bool VG_(get_fnname_nodemangle) ( Addr a, Char* buf, Int nbuf )
|
|
{
|
|
return get_fnname ( /*demangle*/False, a, buf, nbuf,
|
|
/*match_anywhere_in_fun*/True,
|
|
/*show offset?*/False );
|
|
}
|
|
|
|
/* Map a code address to the name of a shared object file or the executable.
|
|
Returns False if no idea; otherwise True. Doesn't require debug info.
|
|
Caller supplies buf and nbuf. */
|
|
Bool VG_(get_objname) ( Addr a, Char* buf, Int nbuf )
|
|
{
|
|
SegInfo* si;
|
|
|
|
for (si = segInfo; si != NULL; si = si->next) {
|
|
if (si->start <= a && a < si->start+si->size) {
|
|
VG_(strncpy_safely)(buf, si->filename, nbuf);
|
|
return True;
|
|
}
|
|
}
|
|
return False;
|
|
}
|
|
|
|
/* Map a code address to its SegInfo. Returns NULL if not found. Doesn't
|
|
require debug info. */
|
|
SegInfo* VG_(get_obj) ( Addr a )
|
|
{
|
|
SegInfo* si;
|
|
|
|
for (si = segInfo; si != NULL; si = si->next) {
|
|
if (si->start <= a && a < si->start+si->size) {
|
|
return si;
|
|
}
|
|
}
|
|
return False;
|
|
}
|
|
|
|
|
|
/* Map a code address to a filename. Returns True if successful. */
|
|
Bool VG_(get_filename)( Addr a, Char* filename, Int n_filename )
|
|
{
|
|
SegInfo* si;
|
|
Int locno;
|
|
search_all_loctabs ( a, &si, &locno );
|
|
if (si == NULL)
|
|
return False;
|
|
VG_(strncpy_safely)(filename, si->loctab[locno].filename,
|
|
n_filename);
|
|
return True;
|
|
}
|
|
|
|
/* Map a code address to a line number. Returns True if successful. */
|
|
Bool VG_(get_linenum)( Addr a, UInt* lineno )
|
|
{
|
|
SegInfo* si;
|
|
Int locno;
|
|
search_all_loctabs ( a, &si, &locno );
|
|
if (si == NULL)
|
|
return False;
|
|
*lineno = si->loctab[locno].lineno;
|
|
|
|
return True;
|
|
}
|
|
|
|
/* Map a code address to a (filename, line number) pair.
|
|
Returns True if successful.
|
|
*/
|
|
Bool VG_(get_filename_linenum)( Addr a,
|
|
Char* filename, Int n_filename,
|
|
UInt* lineno )
|
|
{
|
|
SegInfo* si;
|
|
Int locno;
|
|
search_all_loctabs ( a, &si, &locno );
|
|
if (si == NULL)
|
|
return False;
|
|
VG_(strncpy_safely)(filename, si->loctab[locno].filename,
|
|
n_filename);
|
|
*lineno = si->loctab[locno].lineno;
|
|
|
|
return True;
|
|
}
|
|
|
|
#ifndef TEST
|
|
|
|
/* return a pointer to a register (now for 5 other impossible things
|
|
before breakfast) */
|
|
static UInt *regaddr(ThreadId tid, Int regno)
|
|
{
|
|
UInt *ret = 0;
|
|
|
|
if (VG_(is_running_thread)(tid)) {
|
|
Int idx;
|
|
|
|
switch(regno) {
|
|
case R_EAX: idx = VGOFF_(m_eax); break;
|
|
case R_ECX: idx = VGOFF_(m_ecx); break;
|
|
case R_EDX: idx = VGOFF_(m_edx); break;
|
|
case R_EBX: idx = VGOFF_(m_ebx); break;
|
|
case R_ESP: idx = VGOFF_(m_esp); break;
|
|
case R_EBP: idx = VGOFF_(m_ebp); break;
|
|
case R_ESI: idx = VGOFF_(m_esi); break;
|
|
case R_EDI: idx = VGOFF_(m_edi); break;
|
|
default:
|
|
idx = -1;
|
|
break;
|
|
}
|
|
if (idx != -1)
|
|
ret = &VG_(baseBlock)[idx];
|
|
} else {
|
|
ThreadState *tst = &VG_(threads)[tid];
|
|
|
|
switch(regno) {
|
|
case R_EAX: ret = &tst->m_eax; break;
|
|
case R_ECX: ret = &tst->m_ecx; break;
|
|
case R_EDX: ret = &tst->m_edx; break;
|
|
case R_EBX: ret = &tst->m_ebx; break;
|
|
case R_ESP: ret = &tst->m_esp; break;
|
|
case R_EBP: ret = &tst->m_ebp; break;
|
|
case R_ESI: ret = &tst->m_esi; break;
|
|
case R_EDI: ret = &tst->m_edi; break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (ret == 0) {
|
|
Char file[100];
|
|
Int line;
|
|
Addr eip = VG_(get_EIP)(tid);
|
|
|
|
if (!VG_(get_filename_linenum)(eip, file, sizeof(file), &line))
|
|
file[0] = 0;
|
|
VG_(printf)("mysterious register %d used at %p %s:%d\n",
|
|
regno, eip, file, line);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
/* Get a list of all variables in scope, working out from the directly
|
|
current one */
|
|
Variable *VG_(get_scope_variables)(ThreadId tid)
|
|
{
|
|
static const Bool debug = False;
|
|
Variable *list, *end;
|
|
Addr eip;
|
|
SegInfo *si;
|
|
Int scopeidx;
|
|
Scope *scope;
|
|
Int distance;
|
|
static const Int maxsyms = 1000;
|
|
Int nsyms = maxsyms;
|
|
|
|
list = end = NULL;
|
|
|
|
eip = VG_(get_EIP)(tid);
|
|
|
|
search_all_scopetabs(eip, &si, &scopeidx);
|
|
|
|
if (debug)
|
|
VG_(printf)("eip=%p si=%p (%s; offset=%p) scopeidx=%d\n",
|
|
eip, si, si ? si->filename : (Char *)"???",
|
|
si ? si->offset : 0x99999, scopeidx);
|
|
|
|
if (si == NULL)
|
|
return NULL; /* nothing in scope (should use global scope at least) */
|
|
|
|
if (debug) {
|
|
ScopeRange *sr = &si->scopetab[scopeidx];
|
|
Char file[100];
|
|
Int line;
|
|
|
|
if (!VG_(get_filename_linenum)(sr->addr, file, sizeof(file), &line))
|
|
file[0] = 0;
|
|
|
|
VG_(printf)("found scope range %p: eip=%p (%s:%d) size=%d scope=%p\n",
|
|
sr, sr->addr, file, line, sr->size, sr->scope);
|
|
}
|
|
|
|
distance = 0;
|
|
for(scope = si->scopetab[scopeidx].scope; scope != NULL; scope = scope->outer, distance++) {
|
|
UInt i;
|
|
|
|
for(i = 0; i < scope->nsyms; i++) {
|
|
Sym *sym = &scope->syms[i];
|
|
Variable *v;
|
|
|
|
if (nsyms-- == 0) {
|
|
VG_(printf)("max %d syms reached\n", maxsyms);
|
|
return list;
|
|
}
|
|
|
|
v = VG_(arena_malloc)(VG_AR_SYMTAB, sizeof(*v));
|
|
|
|
v->next = NULL;
|
|
v->distance = distance;
|
|
v->type = VG_(st_basetype)(sym->type, False);
|
|
v->name = VG_(arena_strdup)(VG_AR_SYMTAB, sym->name);
|
|
v->container = NULL;
|
|
v->size = VG_(st_sizeof)(sym->type);
|
|
|
|
if (debug && 0)
|
|
VG_(printf)("sym->name=%s sym->kind=%d offset=%d\n", sym->name, sym->kind, sym->offset);
|
|
switch(sym->kind) {
|
|
UInt reg;
|
|
|
|
case SyGlobal:
|
|
case SyStatic:
|
|
if (sym->addr == 0) {
|
|
/* XXX lookup value */
|
|
}
|
|
v->valuep = sym->addr;
|
|
break;
|
|
|
|
case SyReg:
|
|
v->valuep = (Addr)regaddr(tid, sym->regno);
|
|
break;
|
|
|
|
case SyEBPrel:
|
|
case SyESPrel:
|
|
reg = *regaddr(tid, sym->kind == SyESPrel ? R_ESP : R_EBP);
|
|
if (debug)
|
|
VG_(printf)("reg=%p+%d=%p\n", reg, sym->offset, reg+sym->offset);
|
|
v->valuep = (Addr)(reg + sym->offset);
|
|
break;
|
|
|
|
case SyType:
|
|
VG_(core_panic)("unexpected typedef in scope");
|
|
}
|
|
|
|
if (v->valuep == 0) {
|
|
/* not interesting or useful */
|
|
VG_(arena_free)(VG_AR_SYMTAB, v);
|
|
continue;
|
|
}
|
|
|
|
/* append to end of list */
|
|
if (list == NULL)
|
|
list = end = v;
|
|
else {
|
|
end->next = v;
|
|
end = v;
|
|
}
|
|
}
|
|
}
|
|
|
|
return list;
|
|
}
|
|
#endif /* TEST */
|
|
|
|
/* Print into buf info on code address, function name and filename */
|
|
Char* VG_(describe_eip)(Addr eip, Char* buf, Int n_buf)
|
|
{
|
|
#define APPEND(str) \
|
|
{ UChar* sss; \
|
|
for (sss = str; n < n_buf-1 && *sss != 0; n++,sss++) \
|
|
buf[n] = *sss; \
|
|
buf[n] = '\0'; \
|
|
}
|
|
Bool know_fnname;
|
|
Bool know_objname;
|
|
Bool know_srcloc;
|
|
UChar buf_fn[M_VG_ERRTXT];
|
|
UChar buf_obj[M_VG_ERRTXT];
|
|
UChar buf_srcloc[M_VG_ERRTXT];
|
|
UInt lineno;
|
|
UChar ibuf[20];
|
|
UInt n = 0;
|
|
|
|
know_fnname = VG_(get_fnname) (eip, buf_fn, M_VG_ERRTXT);
|
|
know_objname = VG_(get_objname)(eip, buf_obj, M_VG_ERRTXT);
|
|
know_srcloc = VG_(get_filename_linenum)(eip, buf_srcloc, M_VG_ERRTXT,
|
|
&lineno);
|
|
VG_(sprintf)(ibuf,"0x%x: ", eip);
|
|
APPEND(ibuf);
|
|
if (know_fnname) {
|
|
APPEND(buf_fn);
|
|
if (!know_srcloc && know_objname) {
|
|
APPEND(" (in ");
|
|
APPEND(buf_obj);
|
|
APPEND(")");
|
|
}
|
|
} else if (know_objname && !know_srcloc) {
|
|
APPEND("(within ");
|
|
APPEND(buf_obj);
|
|
APPEND(")");
|
|
} else {
|
|
APPEND("???");
|
|
}
|
|
if (know_srcloc) {
|
|
APPEND(" (");
|
|
APPEND(buf_srcloc);
|
|
APPEND(":");
|
|
VG_(sprintf)(ibuf,"%d",lineno);
|
|
APPEND(ibuf);
|
|
APPEND(")");
|
|
}
|
|
return buf;
|
|
|
|
#undef APPEND
|
|
}
|
|
|
|
/* Print a mini stack dump, showing the current location. */
|
|
void VG_(mini_stack_dump) ( Addr eips[], UInt n_eips )
|
|
{
|
|
UInt i;
|
|
UChar buf[M_VG_ERRTXT];
|
|
Char* how;
|
|
|
|
Int stop_at = n_eips;
|
|
|
|
vg_assert(stop_at > 0);
|
|
|
|
i = 0;
|
|
do {
|
|
Addr eip = eips[i];
|
|
if (i > 0) eip--; /* point to calling line */
|
|
if (i == 0) how = "at"; else how = "by";
|
|
VG_(describe_eip)(eip, buf, M_VG_ERRTXT);
|
|
VG_(message)(Vg_UserMsg, " %s %s", how, buf);
|
|
i++;
|
|
|
|
} while (i < (UInt)stop_at && eips[i] != 0);
|
|
}
|
|
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- Find interesting glibc entry points. ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
CodeRedirect VG_(code_redirect_table)[VG_N_CODE_REDIRECTS];
|
|
|
|
Int VG_(setup_code_redirect_table) ( void )
|
|
{
|
|
# define N_SUBSTS 6
|
|
|
|
Int i, j;
|
|
Addr a_libc, a_pth;
|
|
SegInfo *si, *si_libc, *si_pth;
|
|
|
|
/* Original entry points to look for in libc. */
|
|
static Char* libc_names[N_SUBSTS]
|
|
= { "__GI___errno_location"
|
|
, "__errno_location"
|
|
, "__GI___h_errno_location"
|
|
, "__h_errno_location"
|
|
, "__GI___res_state"
|
|
, "__res_state"
|
|
};
|
|
|
|
/* Corresponding substitute address in our pthread lib. */
|
|
static Char* pth_names[N_SUBSTS]
|
|
= { "__errno_location"
|
|
, "__errno_location"
|
|
, "__h_errno_location"
|
|
, "__h_errno_location"
|
|
, "__res_state"
|
|
, "__res_state"
|
|
};
|
|
|
|
/* Look for the SegInfo for glibc and our pthread library. */
|
|
|
|
si_libc = si_pth = NULL;
|
|
|
|
for (si = segInfo; si != NULL; si = si->next) {
|
|
if (VG_(strstr)(si->filename, "/libc-2.2.93.so")
|
|
|| VG_(strstr)(si->filename, "/libc-2.3.1.so")
|
|
|| VG_(strstr)(si->filename, "/libc-2.3.2.so")
|
|
|| VG_(strstr)(si->filename, "/libc.so"))
|
|
si_libc = si;
|
|
if (VG_(strstr)(si->filename, "/libpthread.so"))
|
|
si_pth = si;
|
|
}
|
|
|
|
if (si_libc == NULL || si_pth == NULL)
|
|
return 0;
|
|
|
|
/* Build the substitution table. */
|
|
vg_assert(N_SUBSTS <= VG_N_CODE_REDIRECTS-1);
|
|
|
|
j = 0;
|
|
VG_(code_redirect_table)[j].entry_pt_orig = 0;
|
|
|
|
for (i = 0; i < N_SUBSTS; i++) {
|
|
a_libc = reverse_search_one_symtab(si_libc, libc_names[i]);
|
|
a_pth = reverse_search_one_symtab(si_pth, pth_names[i]);
|
|
if (a_libc == 0 || a_pth == 0)
|
|
continue;
|
|
/* We've found a substitution pair. */
|
|
VG_(code_redirect_table)[j].entry_pt_orig = a_libc;
|
|
VG_(code_redirect_table)[j].entry_pt_subst = a_pth;
|
|
j++;
|
|
vg_assert(j < VG_N_CODE_REDIRECTS);
|
|
/* Set end marker. */
|
|
VG_(code_redirect_table)[j].entry_pt_orig = 0;
|
|
if (VG_(clo_verbosity) >= 2)
|
|
VG_(message)(Vg_UserMsg,
|
|
"REPLACING libc(%s) with libpthread(%s)",
|
|
libc_names[i], pth_names[i]
|
|
);
|
|
}
|
|
|
|
return j;
|
|
|
|
# undef N_SUBSTS
|
|
}
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- SegInfo accessor functions ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
const SegInfo* VG_(next_seginfo)(const SegInfo* seg)
|
|
{
|
|
if (seg == NULL)
|
|
return segInfo;
|
|
return seg->next;
|
|
}
|
|
|
|
Addr VG_(seg_start)(const SegInfo* seg)
|
|
{
|
|
return seg->start;
|
|
}
|
|
|
|
UInt VG_(seg_size)(const SegInfo* seg)
|
|
{
|
|
return seg->size;
|
|
}
|
|
|
|
const UChar* VG_(seg_filename)(const SegInfo* seg)
|
|
{
|
|
return seg->filename;
|
|
}
|
|
|
|
UInt VG_(seg_sym_offset)(const SegInfo* seg)
|
|
{
|
|
return seg->offset;
|
|
}
|
|
|
|
VgSectKind VG_(seg_sect_kind)(Addr a)
|
|
{
|
|
SegInfo* seg;
|
|
VgSectKind ret = Vg_SectUnknown;
|
|
|
|
for(seg = segInfo; seg != NULL; seg = seg->next) {
|
|
if (a >= seg->start && a < (seg->start + seg->size)) {
|
|
if (0)
|
|
VG_(printf)("addr=%p seg=%p %s got=%p %d plt=%p %d data=%p %d bss=%p %d\n",
|
|
a, seg, seg->filename,
|
|
seg->got_start, seg->got_size,
|
|
seg->plt_start, seg->plt_size,
|
|
seg->data_start, seg->data_size,
|
|
seg->bss_start, seg->bss_size);
|
|
ret = Vg_SectText;
|
|
|
|
if (a >= seg->data_start && a < (seg->data_start + seg->data_size))
|
|
ret = Vg_SectData;
|
|
else if (a >= seg->bss_start && a < (seg->bss_start + seg->bss_size))
|
|
ret = Vg_SectBSS;
|
|
else if (a >= seg->plt_start && a < (seg->plt_start + seg->plt_size))
|
|
ret = Vg_SectPLT;
|
|
else if (a >= seg->got_start && a < (seg->got_start + seg->got_size))
|
|
ret = Vg_SectGOT;
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
/*--------------------------------------------------------------------*/
|
|
/*--- end vg_symtab2.c ---*/
|
|
/*--------------------------------------------------------------------*/
|