-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdwarf2.d
407 lines (389 loc) · 11.8 KB
/
dwarf2.d
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
module dwarf2;
import llvmfile, quickformat, parseBase;
import tools.ctfe: ctTableUnroll;
import tools.base: Stuple, stuple, fail, New;
import tools.log;
// _ATE to prevent collision with AT
const string DwarfCodeTable = `
value | TAG | AT | FORM | OP | _ATE
------+----------------+-------------+--------+--------+------
0x00 | | | | | void
0x01 | array_type | | addr | | address
0x02 | class_type | location | | | boolean
0x03 | | name | | | complex_float
0x04 | | | | | float
0x05 | | | | | signed
0x06 | | | data4 | | signed_char
0x07 | | | | | unsigned
0x08 | | | string | | unsigned_char
0x09 | | | | | imaginary_float
0x0a | | | block1 | | packed_decimal
0x0b | lexical_block | byte_size | data1 | | numeric_string
0x0c | | | flag | | edited
0x0d | member | | | | signed_fixed
0x0e | | | strp | | unsigned_fixed
0x0f | pointer_type | | | | decimal_float
0x10 | reference_type | stmt_list | | |
0x11 | compile_unit | low_pc | | |
0x12 | string_type | high_pc | | |
0x13 | structure_type | language | ref4 | |
0x1b | | comp_dir | | |
0x24 | base_type | | | |
0x25 | | producer | | |
0x27 | | prototyped | | |
0x2e | subprogram | | | |
0x34 | variable | | | |
0x38 | | data_member_location | | |
0x3a | | decl_file | | |
0x3b | | decl_line | | |
0x3e | | encoding | | |
0x3f | | external | | |
0x40 | | frame_base | | |
0x49 | | type | | |
0x50 | | | | reg0 |
0x51 | | | | reg1 |
0x52 | | | | reg2 |
0x53 | | | | reg3 |
0x54 | | | | reg4 |
0x55 | | | | reg5 |
0x56 | | | | reg6 |
0x57 | | | | reg7 |
0x91 | | | | fbreg |
`;
mixin(`
string makeDWEnum() {
string res = "enum DW {";
`~DwarfCodeTable.ctTableUnroll(`
if ("$TAG".length)
res ~= "TAG_$TAG = $value,";
if ("$AT".length)
res ~= "AT_$AT = $value,";
if ("$FORM".length)
res ~= "FORM_$FORM = $value,";
if ("$OP".length)
res ~= "OP_$OP = $value,";
if ("$_ATE".length)
res ~= "ATE_$_ATE = $value,";
`) ~ `
res ~= "children_no = false, children_yes = true }";
return res;
}
`);
mixin(makeDWEnum());
string[] formatAbbrevSection(int key, DW[] data) {
string[] res;
void addLine(string s) { res ~= s; }
void addWide(DW value, string comment) {
addLine(qformat(".uleb128 "[], hex(value), "\t/* "[], comment, " */"[]));
}
void add(DW dw, string mode) {
string tagname, atname, formname;
mixin(`
switch (dw) {
`~DwarfCodeTable.ctTableUnroll(`
case cast(DW) $value:
tagname = "DW_TAG_$TAG";
atname = "DW_AT_$AT";
formname = "DW_FORM_$FORM";
break;
`)~`
default: break;
}`);
string comment;
switch (mode) {
case "TAG":
if (!tagname || tagname == "DW_TAG_"[]) fail;
comment = "TAG: "~tagname;
break;
case "AT":
if (!atname || atname == "DW_AT_"[]) fail;
comment = atname;
break;
case "FORM":
if (!formname || formname == "DW_FORM_"[]) fail;
comment = formname;
break;
}
addWide(dw, comment);
}
DW[] rest = data;
DW take() {
auto res = rest[0];
if (!rest.length) fail;
rest = rest[1..$];
return res;
}
addWide(cast(DW) key, "abbrev code"[]);
add(take(), "TAG"[]);
{
auto ch = take();
if (ch == DW.children_no)
addLine(".byte 0\t /* DW_children_no */"[]);
else if (ch == DW.children_yes)
addLine(".byte 1\t /* DW_children_yes */"[]);
else fail;
}
while (rest.length) {
auto at = take(), form = take();
add(at, "AT"[]);
add(form, "FORM"[]);
}
addLine(".byte 0"[]);
addLine(".byte 0"[]);
return res;
}
class Dwarf2AbbrevCache {
Stuple!(DW[], int)[string] abbrevs;
void allocAbbrev(string name, DW[] data) {
int key = cast(int) abbrevs.length + 1;
abbrevs[name] = stuple(data, key);
}
Stuple!(bool, int) getKeyFor(string name) {
auto tup = name in abbrevs;
if (!tup) fail;
return stuple(tup._0[1] == DW.children_yes, tup._1);
}
string[] genSection() {
string[] res;
res ~= ".section\t.debug_abbrev";
res ~= ".Ldebug_abbrev0:";
foreach (key, value; abbrevs) {
res ~= formatAbbrevSection(value._1, value._0);
}
res ~= ".byte 0";
return res[];
}
}
class Dwarf2Strings {
string[] strings;
string addString(string s) {
foreach (i, str; strings) {
if (faststreq(str, s)) return qformat(".long\t.LSTRING"[], i);
}
auto key = qformat(".long\t.LSTRING"[], strings.length);
strings ~= s;
return key;
}
string[] genSection() {
auto res = new string[1 + strings.length * 2];
int q;
res[q++] = ".section\t.debug_str";
foreach (i, v; strings) {
res[q++] = qformat(".LSTRING"[], i, ":"[]);
res[q++] = qformat("\t.string \""[], v, "\""[]);
}
return res;
}
}
string hex(int i) {
string res;
while (i) {
res = "0123456789abcdef"[i&15] ~ res;
i /= 16;
}
return "0x"~res;
}
int keycounter;
class Dwarf2Section {
Dwarf2Section sup;
Dwarf2Section[] sub;
int key;
int abbrev;
bool has_children;
string[] data;
string comment;
this(Stuple!(bool, int) info) {
synchronized key = keycounter ++;
has_children = info._0;
abbrev = info._1;
}
string getLabel() {
return qformat(".Ldie"[], key);
}
string getRelative() {
return qformat(".long\t"[], getLabel(), " - d"[], "\t/* relative: "[], abbrev, " */"[]);
}
string[] genSection() {
auto subs = new string[][sub.length];
int sum = 1 + 1 + data.length;
foreach (i, sect; sub) {
subs[i] = sect.genSection();
sum += subs[i].length;
}
if (has_children)
sum += 1;
auto res = new string[sum];
int q = 0;
res[q++] = getLabel()~":"~(comment?qformat("\t/* "[], comment, " */"[]):""[]);
res[q++] = qformat(".uleb128 "[], hex(abbrev), "\t/* abbrev code */"[]);
res[q..q+data.length] = data;
q += data.length;
foreach (subsect; subs) {
res[q .. q+subsect.length] = subsect;
q += subsect.length;
delete subsect;
}
if (has_children)
res[q++] = ".byte\t0\t/* end of "~getLabel()~" */";
if (q != res.length) fail;
return res;
}
}
class Dwarf2Controller {
Dwarf2AbbrevCache cache;
Dwarf2Section root, current;
Dwarf2Strings strings;
Dwarf2Section[string] rootsections;
this() {
New(strings);
New(cache);
cache.allocAbbrev("compile unit"[],
[DW.TAG_compile_unit,
DW.children_yes,
DW.AT_producer, DW.FORM_strp,
DW.AT_language, DW.FORM_data1,
DW.AT_name, DW.FORM_strp,
DW.AT_low_pc, DW.FORM_addr,
DW.AT_high_pc, DW.FORM_addr,
DW.AT_stmt_list, DW.FORM_data4
]);
New(root, cache.getKeyFor("compile unit"[]));
root.data ~= strings.addString("Neat FCC 1"[]);
root.data ~= ".byte\t0x1\t/* language whatevs */";
root.data ~= strings.addString("test.nt"[]);
root.data ~= ".long .Ltext";
root.data ~= ".long .Letext";
// root.data ~= ".4byte\t.Ldebug_line0";
root.data ~= ".long\t.Ldebug_line0";
current = root;
cache.allocAbbrev("subprogram"[],
[DW.TAG_subprogram,
DW.children_yes,
DW.AT_external, DW.FORM_flag,
DW.AT_name, DW.FORM_strp,
DW.AT_decl_file, DW.FORM_data4,
DW.AT_decl_line, DW.FORM_data4,
DW.AT_prototyped,DW.FORM_flag,
DW.AT_low_pc, DW.FORM_addr,
DW.AT_high_pc, DW.FORM_addr,
DW.AT_frame_base,DW.FORM_block1
]);
cache.allocAbbrev("lexical block"[],
[DW.TAG_lexical_block,
DW.children_yes,
DW.AT_low_pc, DW.FORM_addr,
DW.AT_high_pc, DW.FORM_addr
]);
cache.allocAbbrev("variable"[],
[DW.TAG_variable,
DW.children_no,
DW.AT_name, DW.FORM_strp,
DW.AT_type, DW.FORM_ref4,
DW.AT_location, DW.FORM_block1
]);
cache.allocAbbrev("base type"[],
[DW.TAG_base_type,
DW.children_no,
DW.AT_byte_size, DW.FORM_data4,
DW.AT_encoding, DW.FORM_data1,
DW.AT_name, DW.FORM_strp
]);
cache.allocAbbrev("structure type"[],
[DW.TAG_structure_type,
DW.children_yes,
DW.AT_byte_size, DW.FORM_data4
]);
cache.allocAbbrev("pointer type"[],
[DW.TAG_pointer_type,
DW.children_no,
DW.AT_type, DW.FORM_ref4,
DW.AT_byte_size, DW.FORM_data4
]);
cache.allocAbbrev("array type"[],
[DW.TAG_array_type,
DW.children_no,
DW.AT_type, DW.FORM_ref4,
DW.AT_byte_size, DW.FORM_data4
]);
cache.allocAbbrev("structure member"[],
[DW.TAG_member,
DW.children_no,
DW.AT_name, DW.FORM_strp,
DW.AT_type, DW.FORM_ref4,
DW.AT_data_member_location, DW.FORM_block1
]);
}
void open(Dwarf2Section sect) {
current.sub ~= sect;
sect.sup = current;
current = sect;
}
void close() {
current = current.sup;
}
void closeUntil(Dwarf2Section dest) {
while (current !is dest) close;
}
void add(Dwarf2Section sect) {
open(sect);
close;
}
string addToRoot(string key, lazy Dwarf2Section sect) {
if (auto p = key in rootsections) {
return p.getRelative();
}
auto s = sect();
rootsections[key] = s;
s.sup = root;
return s.getRelative();
}
string[] genData() {
bool count;
int sum;
int q;
string[] res;
/+void addLine(string s) {
if (count) sum++;
else res[q++] = s;
}
void addLines(string[] s) {
if (count) sum += s.length;
else {
res[q .. q+s.length] = s;
q += s.length;
}
}
foreach (key, value; rootsections) {
value.comment = ":"~key;
root.sub ~= value;
}
auto rootsec = root.genSection();
auto cachesec = cache.genSection();
auto stringsec = strings.genSection();
void generate() {
".section\t.debug_info".addLine();
".Ldebug_info0:".addLine();
"d:".addLine();
// ".4byte\t.Lcu_end - 1f\t/* length */".addLine();
".long\t.Lcu_end - 1f\t/* length */".addLine();
"1:".addLine();
".value\t0x2\t/* dwarf version */".addLine();
".long\t.Ldebug_abbrev0".addLine();
".byte\t0x4\t/* pointer size */".addLine();
rootsec.addLines();
".Lcu_end:".addLine();
cachesec.addLines();
stringsec.addLines();
}
count = true;
generate();
res = new string[sum];
count = false;
generate();+/
return res;
}
}
interface Dwarf2Encodable {
bool canEncode();
Dwarf2Section encode(Dwarf2Controller);
}