1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
198
199
200
201
202
203
204
205
206
207
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
240
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
#include <u.h>
#include <libc.h>
#include <mach.h>
#include "stabs.h"
static int
strcmpcolon(char *a, char *bcolon)
{
int i, len;
char *p;
p = strchr(bcolon, ':');
if(p == nil)
return strcmp(a, bcolon);
len = p-bcolon;
i = strncmp(a, bcolon, len);
if(i)
return i;
if(a[len] == 0)
return 0;
return 1;
}
static int
stabcvtsym(StabSym *stab, Symbol *sym, char *dir, char *file, int i)
{
char *p;
/*
* Zero out the : to avoid allocating a new name string.
* The type info can be found by looking past the NUL.
* This is going to get us in trouble...
*/
if((p = strchr(stab->name, ':')) != nil)
*p++ = 0;
else
p = stab->name+strlen(stab->name)+1;
sym->name = stab->name;
sym->u.stabs.dir = dir;
sym->u.stabs.file = file;
sym->u.stabs.i = i;
switch(stab->type){
default:
return -1;
case N_FUN:
sym->class = CTEXT;
switch(*p){
default:
return -1;
case 'F': /* global function */
sym->type = 'T';
break;
case 'Q': /* static procedure */
case 'f': /* static function */
case 'I': /* nested procedure */
case 'J': /* nested function */
sym->type = 't';
break;
}
sym->loc.type = LADDR;
sym->loc.addr = stab->value;
break;
case N_GSYM:
case N_PSYM:
case N_LSYM:
case N_LCSYM:
sym->class = CDATA;
sym->loc.type = LADDR;
sym->loc.addr = stab->value;
switch(*p){
default:
return -1;
case 'S': /* file-scope static variable */
sym->type = 'd';
break;
case 'G': /* global variable */
sym->type = 'D';
sym->loc.type = LNONE;
break;
case 'r': /* register variable */
sym->class = CAUTO;
sym->type = 'a';
sym->loc.type = LREG;
sym->loc.reg = "XXX";
break;
case 's': /* local variable */
sym->class = CAUTO;
sym->type = 'a';
sym->loc.type = LOFFSET;
sym->loc.offset = stab->value;
sym->loc.reg = "XXX";
break;
case 'a': /* by reference */
case 'D': /* f.p. parameter */
case 'i': /* register parameter */
case 'p': /* "normal" parameter */
case 'P': /* register parameter */
case 'v': /* by reference */
case 'X': /* function return variable */
sym->class = CPARAM;
sym->type = 'p';
if(*p == 'i'){
sym->loc.type = LREG;
sym->loc.reg = "XXX";
}else{
sym->loc.type = LOFFSET;
sym->loc.offset = stab->value;
sym->loc.reg = "XXX";
}
break;
}
break;
}
return 0;
}
static int
stabssyminit(Fhdr *fp)
{
int i;
char *dir, *file;
Stab *stabs;
StabSym sym, lastfun;
Symbol s, *fun;
char **inc, **xinc;
int ninc, minc;
int locals, autos, params;
stabs = &fp->stabs;
if(stabs == nil){
werrstr("no stabs info");
return -1;
}
dir = nil;
file = nil;
inc = nil;
fun = nil;
ninc = 0;
minc = 0;
locals = 0;
params = 0;
autos = 0;
memset(&lastfun, 0, sizeof lastfun);
for(i=0; stabsym(stabs, i, &sym)>=0; i++){
switch(sym.type){
case N_SO:
if(sym.name == nil || *sym.name == 0){
file = nil;
break;
}
if(sym.name[strlen(sym.name)-1] == '/')
dir = sym.name;
else
file = sym.name;
break;
case N_BINCL:
if(ninc >= minc){
xinc = realloc(inc, (ninc+32)*sizeof(inc[0]));
if(xinc){
memset(xinc+ninc, 0, 32*sizeof(inc[0]));
inc = xinc;
}
ninc += 32;
}
if(ninc < minc)
inc[ninc] = sym.name;
ninc++;
break;
case N_EINCL:
if(ninc > 0)
ninc--;
break;
case N_EXCL:
/* condensed include - same effect as previous BINCL/EINCL pair */
break;
case N_GSYM: /* global variable */
/* only includes type, so useless for now */
break;
case N_FUN:
if(sym.name == nil){
/* marks end of function */
if(fun){
fun->hiloc.type = LADDR;
fun->hiloc.addr = fun->loc.addr + sym.value;
}
break;
}
if(fun && lastfun.value==sym.value && lastfun.name==sym.name){
fun->u.stabs.locals = i;
break;
}
/* create new symbol, add it */
lastfun = sym;
fun = nil;
if(stabcvtsym(&sym, &s, dir, file, i) < 0)
continue;
if((fun = _addsym(fp, &s)) == nil)
goto err;
locals = 0;
params = 0;
autos = 0;
break;
case N_PSYM:
case N_LSYM:
case N_LCSYM:
if(fun){
if(fun->u.stabs.frameptr == -1){
/*
* Try to distinguish functions with a real frame pointer
* from functions with a virtual frame pointer, based on
* whether the first parameter is in the right location and
* whether the autos have negative offsets.
*
* This heuristic works most of the time. On the 386, we
* cannot distinguish between a v. function with no autos
* but a frame of size 4 and a f.p. function with no autos and
* no frame. Anything else we'll get right.
*
* Another way to go about this would be to have
* mach-specific functions to inspect the function
* prologues when we're not sure. What we have
* already should be enough, though.
*/
if(params==0 && sym.type == N_PSYM){
if(sym.value != 8 && sym.value >= 4){
/* XXX 386 specific, but let's find another system before generalizing */
fun->u.stabs.frameptr = 0;
fun->u.stabs.framesize = sym.value - 4;
}
}else if(sym.type == N_LSYM){
if(sym.value >= 0){
fun->u.stabs.frameptr = 0;
if(params)
fun->u.stabs.framesize = 8 - 4;
}else
fun->u.stabs.frameptr = 1;
}
}
if(sym.type == N_PSYM)
params++;
if(sym.type == N_LSYM)
autos++;
}
break;
case N_STSYM: /* static file-scope variable */
/* create new symbol, add it */
if(stabcvtsym(&sym, &s, dir, file, i) < 0)
continue;
if(_addsym(fp, &s) == nil)
goto err;
break;
}
}
USED(locals);
free(inc);
return 0;
err:
free(inc);
return -1;
}
static int
stabspc2file(Fhdr *fhdr, u64int pc, char *buf, uint nbuf, ulong *pline)
{
int i;
Symbol *s;
StabSym ss;
ulong line, basepc;
Loc l;
l.type = LADDR;
l.addr = pc;
if((s = ffindsym(fhdr, l, CTEXT)) == nil
|| stabsym(&fhdr->stabs, s->u.stabs.i, &ss) < 0)
return -1;
line = ss.desc;
basepc = ss.value;
for(i=s->u.stabs.i+1; stabsym(&fhdr->stabs, i, &ss) >= 0; i++){
if(ss.type == N_FUN && ss.name == nil)
break;
if(ss.type == N_SLINE){
if(basepc+ss.value > pc)
break;
else
line = ss.desc;
}
}
*pline = line;
if(s->u.stabs.dir)
snprint(buf, nbuf, "%s%s", s->u.stabs.dir, s->u.stabs.file);
else
snprint(buf, nbuf, "%s", s->u.stabs.file);
return 0;
}
static int
stabsline2pc(Fhdr *fhdr, u64int startpc, ulong line, u64int *pc)
{
int i, trigger;
Symbol *s;
StabSym ss;
ulong basepc;
Loc l;
l.type = LADDR;
l.addr = startpc;
if((s = ffindsym(fhdr, l, CTEXT)) == nil)
return -1;
trigger = 0;
line = ss.desc;
basepc = ss.value;
for(i=s->u.stabs.i+1; stabsym(&fhdr->stabs, i, &ss) >= 0; i++){
if(ss.type == N_FUN)
basepc = ss.value;
if(ss.type == N_SLINE){
if(basepc+ss.value >= startpc)
trigger = 1;
if(trigger && ss.desc >= line){
*pc = basepc+ss.value;
return 0;
}
}
}
return -1;
}
static int
stabslenum(Fhdr *fhdr, Symbol *p, char *name, uint j, Loc l, Symbol *s)
{
int i;
StabSym ss;
for(i=p->u.stabs.locals; stabsym(&fhdr->stabs, i, &ss)>=0; i++){
if(ss.type == N_FUN && ss.name == nil)
break;
switch(ss.type){
case N_PSYM:
case N_LSYM:
case N_LCSYM:
if(name){
if(strcmpcolon(name, ss.name) != 0)
break;
}else if(l.type){
/* wait for now */
}else{
if(j-- > 0)
break;
}
if(stabcvtsym(&ss, s, p->u.stabs.dir, p->u.stabs.file, i) < 0)
return -1;
if(s->loc.type == LOFFSET){
if(p->u.stabs.frameptr == 0)
s->loc.reg = mach->sp;
else
s->loc.reg = mach->fp;
}
if(l.type && loccmp(&l, &s->loc) != 0)
break;
return 0;
}
}
return -1;
}
static Loc zl;
static int
stabslookuplsym(Fhdr *fhdr, Symbol *p, char *name, Symbol *s)
{
return stabslenum(fhdr, p, name, 0, zl, s);
}
static int
stabsindexlsym(Fhdr *fhdr, Symbol *p, uint i, Symbol *s)
{
return stabslenum(fhdr, p, nil, i, zl, s);
}
static int
stabsfindlsym(Fhdr *fhdr, Symbol *p, Loc l, Symbol *s)
{
return stabslenum(fhdr, p, nil, 0, l, s);
}
int
symstabs(Fhdr *fp)
{
if(stabssyminit(fp) < 0)
return -1;
fp->pc2file = stabspc2file;
fp->line2pc = stabsline2pc;
fp->lookuplsym = stabslookuplsym;
fp->indexlsym = stabsindexlsym;
fp->findlsym = stabsfindlsym;
return 0;
}
|