symtab.c 7.19 KB
Newer Older
Russ Cox's avatar
Russ Cox committed
1 2 3 4
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

5 6 7 8 9 10 11 12 13
// Runtime symbol table access.  Work in progress.
// The Plan 9 symbol table is not in a particularly convenient form.
// The routines here massage it into a more usable form; eventually
// we'll change 6l to do this for us, but it is easier to experiment
// here than to change 6l and all the other tools.
//
// The symbol table also needs to be better integrated with the type
// strings table in the future.  This is just a quick way to get started
// and figure out exactly what we want.
Russ Cox's avatar
Russ Cox committed
14

15
#include "runtime.h"
Russ Cox's avatar
Russ Cox committed
16

17 18 19
// TODO(rsc): Move this *under* the text segment.
// Then define names for these addresses instead of hard-coding magic ones.
#ifdef _64BIT
Russ Cox's avatar
Russ Cox committed
20 21
#define SYMCOUNTS ((int32*)(0x99LL<<32))	// known to 6l
#define SYMDATA ((byte*)(0x99LL<<32) + 8)
22 23 24 25 26
#else
#define SYMCOUNTS ((int32*)(0x99LL<<24))	// known to 8l
#define SYMDATA ((byte*)(0x99LL<<24) + 8)
#endif

Russ Cox's avatar
Russ Cox committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

// Return a pointer to a byte array containing the symbol table segment.
void
sys·symdat(Array *symtab, Array *pclntab)
{
	Array *a;
	int32 *v;

	v = SYMCOUNTS;

	a = mal(sizeof *a);
	a->nel = v[0];
	a->cap = a->nel;
	a->array = SYMDATA;
	symtab = a;
	FLUSH(&symtab);

	a = mal(sizeof *a);
	a->nel = v[1];
	a->cap = a->nel;
	a->array = SYMDATA + v[0];
	pclntab = a;
	FLUSH(&pclntab);
}

typedef struct Sym Sym;
struct Sym
{
55
	uintptr value;
Russ Cox's avatar
Russ Cox committed
56 57 58 59 60 61
	byte symtype;
	byte *name;
	byte *gotype;
};

// Walk over symtab, calling fn(&s) for each symbol.
62
static void
Russ Cox's avatar
Russ Cox committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
walksymtab(void (*fn)(Sym*))
{
	int32 *v;
	byte *p, *ep, *q;
	Sym s;

	v = SYMCOUNTS;
	p = SYMDATA;
	ep = p + v[0];
	while(p < ep) {
		if(p + 7 > ep)
			break;
		s.value = ((uint32)p[0]<<24) | ((uint32)p[1]<<16) | ((uint32)p[2]<<8) | ((uint32)p[3]);
		if(!(p[4]&0x80))
			break;
		s.symtype = p[4] & ~0x80;
		p += 5;
80
		s.name = p;
Russ Cox's avatar
Russ Cox committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		if(s.symtype == 'z' || s.symtype == 'Z') {
			// path reference string - skip first byte,
			// then 2-byte pairs ending at two zeros.
			q = p+1;
			for(;;) {
				if(q+2 > ep)
					return;
				if(q[0] == '\0' && q[1] == '\0')
					break;
				q += 2;
			}
			p = q+2;
		}else{
			q = mchr(p, '\0', ep);
			if(q == nil)
				break;
			p = q+1;
		}
		q = mchr(p, '\0', ep);
		if(q == nil)
			break;
		s.gotype = p;
		p = q+1;
		fn(&s);
	}
}

// Symtab walker; accumulates info about functions.

110 111 112 113 114
static Func *func;
static int32 nfunc;

static byte **fname;
static int32 nfname;
Russ Cox's avatar
Russ Cox committed
115 116 117 118 119 120

static void
dofunc(Sym *sym)
{
	Func *f;

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	switch(sym->symtype) {
	case 't':
	case 'T':
		if(strcmp(sym->name, (byte*)"etext") == 0)
			break;
		if(func == nil) {
			nfunc++;
			break;
		}
		f = &func[nfunc++];
		f->name = gostring(sym->name);
		f->entry = sym->value;
		break;
	case 'm':
		if(nfunc > 0 && func != nil)
			func[nfunc-1].frame = sym->value;
		break;
Russ Cox's avatar
Russ Cox committed
138 139 140 141 142 143 144 145 146 147
	case 'p':
		if(nfunc > 0 && func != nil) {
			f = &func[nfunc-1];
			// args counts 32-bit words.
			// sym->value is the arg's offset.
			// don't know width of this arg, so assume it is 64 bits.
			if(f->args < sym->value/4 + 2)
				f->args = sym->value/4 + 2;
		}
		break;
148 149 150 151 152 153 154 155
	case 'f':
		if(fname == nil) {
			if(sym->value >= nfname)
				nfname = sym->value+1;
			break;
		}
		fname[sym->value] = sym->name;
		break;
Russ Cox's avatar
Russ Cox committed
156
	}
157 158 159 160 161 162 163 164 165 166 167
}

// put together the path name for a z entry.
// the f entries have been accumulated into fname already.
static void
makepath(byte *buf, int32 nbuf, byte *path)
{
	int32 n, len;
	byte *p, *ep, *q;

	if(nbuf <= 0)
Russ Cox's avatar
Russ Cox committed
168
		return;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

	p = buf;
	ep = buf + nbuf;
	*p = '\0';
	for(;;) {
		if(path[0] == 0 && path[1] == 0)
			break;
		n = (path[0]<<8) | path[1];
		path += 2;
		if(n >= nfname)
			break;
		q = fname[n];
		len = findnull(q);
		if(p+1+len >= ep)
			break;
		if(p > buf && p[-1] != '/')
			*p++ = '/';
		mcpy(p, q, len+1);
		p += len;
	}
}

// walk symtab accumulating path names for use by pc/ln table.
// don't need the full generality of the z entry history stack because
// there are no includes in go (and only sensible includes in our c).
static void
dosrcline(Sym *sym)
{
	static byte srcbuf[1000];
198
	static String srcstring;
199 200 201 202 203 204 205
	static int32 lno, incstart;
	static int32 nf, nhist;
	Func *f;

	switch(sym->symtype) {
	case 't':
	case 'T':
206 207
		if(strcmp(sym->name, (byte*)"etext") == 0)
			break;
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
		f = &func[nf++];
		f->src = srcstring;
		f->ln0 += lno;
		break;
	case 'z':
		if(sym->value == 1) {
			// entry for main source file for a new object.
			makepath(srcbuf, sizeof srcbuf, sym->name+1);
			srcstring = gostring(srcbuf);
			lno = 0;
			nhist = 0;
		} else {
			// push or pop of included file.
			makepath(srcbuf, sizeof srcbuf, sym->name+1);
			if(srcbuf[0] != '\0') {
				if(nhist++ == 0)
					incstart = sym->value;
			}else{
				if(--nhist == 0)
					lno -= sym->value - incstart;
			}
		}
	}
}

enum { PcQuant = 1 };

// Interpret pc/ln table, saving the subpiece for each func.
static void
splitpcln(void)
{
	int32 line;
240
	uintptr pc;
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	byte *p, *ep;
	Func *f, *ef;
	int32 *v;

	// pc/ln table bounds
	v = SYMCOUNTS;
	p = SYMDATA;
	p += v[0];
	ep = p+v[1];

	f = func;
	ef = func + nfunc;
	f->pcln.array = p;
	pc = func[0].entry;	// text base
	line = 0;
	for(; p < ep; p++) {
		if(f < ef && pc >= (f+1)->entry) {
			f->pcln.nel = p - f->pcln.array;
			f->pcln.cap = f->pcln.nel;
			f++;
			f->pcln.array = p;
			f->pc0 = pc;
			f->ln0 = line;
		}
		if(*p == 0) {
			// 4 byte add to line
			line += (p[1]<<24) | (p[2]<<16) | (p[3]<<8) | p[4];
			p += 4;
		} else if(*p <= 64) {
			line += *p;
		} else if(*p <= 128) {
			line -= *p - 64;
		} else {
			pc += PcQuant*(*p - 129);
		}
		pc += PcQuant;
Russ Cox's avatar
Russ Cox committed
277
	}
278 279 280 281 282 283 284 285 286 287 288 289 290
	if(f < ef) {
		f->pcln.nel = p - f->pcln.array;
		f->pcln.cap = f->pcln.nel;
	}
}


// Return actual file line number for targetpc in func f.
// (Source file is f->src.)
int32
funcline(Func *f, uint64 targetpc)
{
	byte *p, *ep;
291
	uintptr pc;
292
	int32 line;
Russ Cox's avatar
Russ Cox committed
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
	p = f->pcln.array;
	ep = p + f->pcln.nel;
	pc = f->pc0;
	line = f->ln0;
	for(; p < ep; p++) {
		if(pc >= targetpc)
			return line;
		if(*p == 0) {
			line += (p[1]<<24) | (p[2]<<16) | (p[3]<<8) | p[4];
			p += 4;
		} else if(*p <= 64) {
			line += *p;
		} else if(*p <= 128) {
			line -= *p - 64;
		} else {
			pc += PcQuant*(*p - 129);
		}
		pc += PcQuant;
	}
	return line;
Russ Cox's avatar
Russ Cox committed
314 315 316 317 318 319 320 321 322
}

static void
buildfuncs(void)
{
	extern byte etext[];

	if(func != nil)
		return;
323
	// count funcs, fnames
Russ Cox's avatar
Russ Cox committed
324
	nfunc = 0;
325
	nfname = 0;
Russ Cox's avatar
Russ Cox committed
326
	walksymtab(dofunc);
327 328

	// initialize tables
Russ Cox's avatar
Russ Cox committed
329
	func = mal((nfunc+1)*sizeof func[0]);
330 331
	func[nfunc].entry = (uint64)etext;
	fname = mal(nfname*sizeof fname[0]);
Russ Cox's avatar
Russ Cox committed
332 333
	nfunc = 0;
	walksymtab(dofunc);
334 335 336 337 338 339

	// split pc/ln table by func
	splitpcln();

	// record src file and line info for each func
	walksymtab(dosrcline);
Russ Cox's avatar
Russ Cox committed
340 341 342
}

Func*
343
findfunc(uintptr addr)
Russ Cox's avatar
Russ Cox committed
344 345
{
	Func *f;
346
	int32 nf, n;
Russ Cox's avatar
Russ Cox committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

	if(func == nil)
		buildfuncs();
	if(nfunc == 0)
		return nil;
	if(addr < func[0].entry || addr >= func[nfunc].entry)
		return nil;

	// binary search to find func with entry <= addr.
	f = func;
	nf = nfunc;
	while(nf > 0) {
		n = nf/2;
		if(f[n].entry <= addr && addr < f[n+1].entry)
			return &f[n];
		else if(addr < f[n].entry)
			nf = n;
		else {
			f += n+1;
			nf -= n+1;
		}
	}

	// can't get here -- we already checked above
	// that the address was in the table bounds.
	// this can only happen if the table isn't sorted
	// by address or if the binary search above is buggy.
	prints("findfunc unreachable\n");
	return nil;
}