forked from MoSync/MoSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstabs.cpp
437 lines (389 loc) · 10.4 KB
/
stabs.cpp
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* Copyright (C) 2009 Mobile Sorcery AB
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2, as published by
the Free Software Foundation.
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; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include <fstream>
#include <string>
#include <stack>
//todo: cleanup
#define LOGGING_ENABLED
#define CONFIG_H
#include "helpers/helpers.h"
#include "helpers/timer.h"
#include "sld.h"
#include "stabs.h"
#include "stabs_parse.h"
#include "stabs_function.h"
#include "stabs_types.h"
#include "stabs_file.h"
#include "stabs_symbols.h"
#include "stabs_static.h"
#include "stabs_typedefs.h"
using namespace std;
static bool parseStabLine(string& line);
static bool parseBIncl(Tuple t, char* text);
static bool parseFun(Tuple t, char* text);
static bool parseSo(Tuple t, char* text);
static bool parseSol(Tuple t, char* text);
static bool parsePSym(Tuple t, char* text);
static bool parseRSym(Tuple t, char* text);
static bool parseSTSym(Tuple t, char* text);
static bool parseGSym(Tuple t, char* text);
static bool parseFile(Tuple t, char* text);
static bool parseThunk(Tuple t, char* text);
static bool parseLbrac(Tuple t);
static bool parseRbrac(Tuple t);
static bool parseEIncl(Tuple t);
static void checkLastFunctionIntegrity();
int gCurrentFile;
Function* gLastFunction = NULL;
static bool sLoaded = false;
static stack<int> sBracStack;
void addFile(int file,const std::string& fileName) {
addTypeFile(file, fileName);
addSymbolFile(file, fileName);
}
bool loadStabs(const char* sld, const char* stabs) {
gCurrentFile = -1;
{
Timer t("loadSLD");
TEST(loadSLD(sld));
}
{
Timer t("parseStabs");
ifstream in(stabs);
string line;
int lineNumber = 1;
while(in.good()) {
getline(in, line);
if(!in.good())
break;
bool ret = parseStabLine(line);
if(!ret) {
LOG("Invalid line %d in stabs.tab\n", lineNumber);
FAIL;
}
lineNumber++;
}
gCurrentFile = -1;
TEST(in.eof());
}
Timer t("resolveAll");
TEST(DelayedType::resolveAll());
return sLoaded = true;
}
bool stabsIsLoaded() {
return sLoaded;
}
static bool parseStabLine(string& line) {
FAILIF(line.length() < 4);
if(line[0] == '/') //ignore this line
return true;
size_t firstSpace = line.find_first_of(' ');
FAILIF(firstSpace == line.npos);
Tuple t;
if(line[0] == '#') { //numeric stab. has no string. TODO.
int n = -1;
int res = sscanf(line.c_str() + firstSpace, " %i %i%n", &t.a, &t.b, &n);
FAILIF(res != 2);
FAILIF(n < 0);
if(strncmp(line.c_str(), "#N_LBRAC", firstSpace) == 0) {
return parseLbrac(t);
} else if(strncmp(line.c_str(), "#N_RBRAC", firstSpace) == 0) {
return parseRbrac(t);
} else if(strncmp(line.c_str(), "#N_EINCL", firstSpace) == 0) {
return parseEIncl(t);
} else {
FAIL;
}
}
int n = -1;
int res = sscanf(line.c_str() + firstSpace, " %i %i %n", &t.a, &t.b, &n);
FAILIF(res != 2);
FAILIF(n < 0);
int textOffset = firstSpace + n;
char* text = (char*)line.c_str() + textOffset;
FAILIF(text[0] != '\'');
FAILIF(line[line.length()-1] != '\'');
line[line.length()-1] = 0;
text++;
if(strncmp(line.c_str(), "N_LSYM", firstSpace) == 0) {
return parseLSym(t, text);
} else if(strncmp(line.c_str(), "N_BINCL", firstSpace) == 0) {
return parseBIncl(t, text);
} else if(strncmp(line.c_str(), "N_FUN", firstSpace) == 0) {
return parseFun(t, text);
} else if(strncmp(line.c_str(), "N_SO", firstSpace) == 0) {
return parseSo(t, text);
} else if(strncmp(line.c_str(), "N_SOL", firstSpace) == 0) {
return parseSol(t, text);
} else if(strncmp(line.c_str(), "N_PSYM", firstSpace) == 0) {
return parsePSym(t, text);
} else if(strncmp(line.c_str(), "N_RSYM", firstSpace) == 0) {
return parseRSym(t, text);
} else if(strncmp(line.c_str(), "N_STSYM", firstSpace) == 0) {
return parseSTSym(t, text);
} else if(strncmp(line.c_str(), "N_LCSYM", firstSpace) == 0) {
return parseSTSym(t, text);
} else if(strncmp(line.c_str(), "N_GSYM", firstSpace) == 0) {
return parseGSym(t, text);
} else if(strncmp(line.c_str(), "N_FILE", firstSpace) == 0) {
return parseFile(t, text);
} else if(strncmp(line.c_str(), "N_THUNK", firstSpace) == 0) {
return parseThunk(t, text);
} else {
FAIL;
}
}
static bool parseFile(Tuple t, char* text) {
t.a++;
//LOG("\nnew file: %i %s\n", t.a, text);
gCurrentFile = t.a;
addFile(gCurrentFile, text);
return true;
}
static bool parseBIncl(Tuple t, char* text) {
return true; //ignored for now
}
static bool parseEIncl(Tuple t) {
return true; //ignored for now
}
//grammar for symbols, including functions:
// symbol = stabType ' ' lineNumber ' ' address ' ' string
//the lineNumber is for the C source file in which the symbol is defined.
//the address is in MoSync memory. Code section for functions, data section for variables.
static bool parseFun(Tuple t, char* text) {
FAILIF(!sBracStack.empty());
Function* pf = new Function;
Function& f(*pf);
f.lineNumber = t.a;
f.address = t.b;
char* colon = strchr(text, ':');
TEST(colon);
*colon = 0;
f.name = text;
char* type = colon + 1;
switch(*type) {
case 'F':
f.fileScope = 0;
break;
case 'f':
f.fileScope = gCurrentFile;
break;
default:
FAIL;
}
/*char* end = scanTuple(type + 1, &t);
TEST(end);
FAILIF(*end != 0);
TEST(f.returnType = findTypeByTuple(t));*/
char* retType = type + 1;
const TypeBase* tb;
TEST(tb = subParseType(&retType, Tuple(), string()));
pf->type = new FunctionType(tb);
addFunction(pf);
checkLastFunctionIntegrity();
gLastFunction = pf;
return true;
}
static bool parseThunk(Tuple t, char* text) {
FAILIF(!sBracStack.empty());
Function* pf = new Function;
Function& f(*pf);
f.lineNumber = -t.a;
f.address = -(gCurrentFile*10000 + t.a);
f.fileScope = gCurrentFile;
f.name = "thunk"; //incorrect; we don't get the proper name from pipe-tool.
f.name += t.a;
f.name += text;
f.type = NULL; //dangerous; might crash
addFunction(pf);
checkLastFunctionIntegrity();
gLastFunction = pf;
return true;
}
static bool parseSo(Tuple t, char* text) {
FAILIF(!sBracStack.empty());
checkLastFunctionIntegrity();
//this marks the end of a file.
//todo: resolve delayed types
return true; //ignore
}
static bool parseSol(Tuple t, char* text) {
return true; //ignore
}
//grammar:
// 'N_PSYM ' lineNumber ' ' offset ' ' text
// text := name ':' paramType typeDeclaration
// paramType := ('p' | 'P')
static bool parsePSym(Tuple t, char* text) {
Function* f = gLastFunction;
TEST(f);
StackVariable* p = new StackVariable;
char* colon = strchr(text, ':');
TEST(colon);
*colon = 0;
p->name = text;
char* type = colon + 1;
switch(*type) {
case 'v':
case 'p': //on the stack?
p->offset = t.b;
break;
case 'R':
case 'P':
//break;
default:
FAIL;
}
type++;
TEST(p->dataType = subParseType(&type, Tuple(), p->name));
f->params.push_back(p);
return true;
}
static bool parseRSym(Tuple t, char* text) {
Function* f = gLastFunction;
if(!f) //occasionally
return true;
char* colon = strchr(text, ':');
TEST(colon);
*colon = 0;
char* type = colon + 1;
RegisterVariable* p = new RegisterVariable;
p->name = text;
char* pType = type + 1;
TEST(p->dataType = subParseType(&pType, Tuple(), string()));
p->reg = t.b;
switch(*type) {
case 'P': //register parameter
f->params.push_back(p);
break;
case 'r': //register local variable
{
ScopedVariable scv;
scv.v = p;
f->locals.push_back(scv);
}
break;
case 'a': //register reference parameter
p->dataType = new ReferenceType(p->dataType);
f->params.push_back(p);
break;
default:
FAIL;
}
return true;
}
static bool parseSTSym(Tuple t, char* text) {
char* colon = strchr(text, ':');
TEST(colon);
*colon = 0;
char* type = colon + 1;
char* pType = type + 1;
const TypeBase* dataType = subParseType(&pType, Tuple(), string());
switch(*type) {
case 'S':
{
StaticVariable* var = new StaticVariable;
var->fileScope = gCurrentFile;
var->name = text;
var->address = t.b;
var->dataType = dataType;
addVariable(var);
}
break;
case 'V':
{
StaticLocal* var = new StaticLocal;
var->name = text;
var->address = t.b;
var->dataType = dataType;
Function* f = gLastFunction;
TEST(f);
ScopedVariable scv;
scv.v = var;
f->locals.push_back(scv);
}
break;
default:
FAIL;
}
return true;
}
static bool parseGSym(Tuple t, char* text) {
char* colon = strchr(text, ':');
TEST(colon);
*colon = 0;
char* type = colon + 1;
DEBUG_ASSERT(*type == 'G');
char* pType = type + 1;
const TypeBase* dataType = subParseType(&pType, Tuple(), string());
StaticVariable* var = new StaticVariable;
var->name = text;
var->address = mapVariable(text, gCurrentFile - 1);
FAILIF(var->address < 0);
var->dataType = dataType;
var->fileScope = 0;
var->lineNumber = t.a;
addVariable(var);
return true;
}
static bool parseLbrac(Tuple t) {
size_t i=gLastFunction->locals.size()-1;
for(; i<gLastFunction->locals.size(); i--) {
ScopedVariable& sv(gLastFunction->locals[i]);
if(sv.start >= 0)
break;
sv.start = t.b;
}
i++;
sBracStack.push(i);
return true;
}
static bool parseRbrac(Tuple t) {
FAILIF(sBracStack.empty());
for(size_t i=sBracStack.top(); i<gLastFunction->locals.size(); i++) {
ScopedVariable& sv(gLastFunction->locals[i]);
sv.end = t.b;
}
sBracStack.pop();
return true;
}
char* scanTuple(char* str, Tuple* t) {
int len;
char c;
int res = sscanf(str, "(%i,%i%c%n", &t->a, &t->b, &c, &len);
FAILIF(res != 3);
FAILIF(c != ')');
return str + len;
}
bool ScopedVariable::contains(int offset) const {
DEBUG_ASSERT(offset >= 0);
DEBUG_ASSERT(start >= 0);
DEBUG_ASSERT(end >= 0);
DEBUG_ASSERT(end >= start);
return offset >= start && offset <= end;
}
static void checkLastFunctionIntegrity() {
if(gLastFunction == NULL)
return;
for(size_t i=0; i<gLastFunction->locals.size(); i++) {
ScopedVariable& sv(gLastFunction->locals[i]);
if(sv.start == -1 && sv.end == -1) { //can happen if there are no BRACs
sv.start = gLastFunction->address;
const FuncMapping* fm = mapFunctionEx(gLastFunction->address);
DEBUG_ASSERT(fm != NULL);
sv.end = fm->stop;
}
sv.contains(0);
}
}