-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsymboltable.d
380 lines (375 loc) · 12 KB
/
symboltable.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
import std.algorithm;
import std.conv;
import std.exception;
import std.stdio;
import modules;
import section;
import sectiontable;
import symbol;
final class SymbolTable
{
SymbolTable parent;
Symbol[immutable(ubyte)[]] symbols;
Symbol[] undefined;
immutable(ubyte)[] entryPoint;
Import[][immutable(ubyte)[]] imports;
Symbol[] hiddenSyms;
this(SymbolTable parent)
{
this.parent = parent;
}
Symbol searchName(immutable(ubyte)[] name)
{
auto p = name in symbols;
return p ? *p : null;
}
Symbol deepSearch(immutable(ubyte)[] name)
{
assert(parent);
auto p = name in symbols;
return (p && *p && !cast(ExternSymbol)*p) ? *p : parent.searchName(name);
}
void setEntry(immutable(ubyte)[] name)
{
if (parent)
parent.setEntry(name);
else
{
enforce(!entryPoint.length, "Multiple entry points defined");
entryPoint = name;
add(new ExternSymbol(name));
}
}
Symbol add(Symbol sym)
{
if (auto p = sym.name in symbols)
{
if (auto es = cast(ExternSymbol)sym)
{
// Redefining an extern symbol is a no-op
p.refCount++;
es.sym = *p;
}
else if (auto es = cast(ExternSymbol)*p)
{
sym.refCount += p.refCount;
removeUndefined(es);
es.sym = sym;
*p = sym;
}
else if (cast(ImportThunkSymbol)*p && cast(ImportThunkSymbol)sym)
{
enforce(false, "Redefinition of import " ~ cast(string)sym.name);
}
else if (cast(ComdefSymbol)*p && cast(ComdefSymbol)sym)
{
auto s = cast(ComdefSymbol)*p;
s.size = max(s.size, (cast(ComdefSymbol)sym).size);
}
else if (cast(PublicSymbol)*p && cast(ComdefSymbol)sym)
{
}
else if (cast(ComdefSymbol)*p && cast(PublicSymbol)sym)
{
*p = sym;
}
else if (cast(ComdatSymbol)*p && cast(ComdatSymbol)sym)
{
auto s = cast(ComdatSymbol)*p;
auto x = cast(ComdatSymbol)sym;
enforce(s.comdat == x.comdat, "Comdat type mismatch");
if (s.comdat == Comdat.Unique)
{
enforce(false, "Multiple definitions of symbol " ~ cast(string)sym.name);
}
else if (s.comdat == Comdat.Any)
{
}
else
{
enforce(false, "Comdat type " ~ to!string(s.comdat) ~ " not implemented");
}
}
else
{
enforce(false, "Multiple definitions of symbol " ~ cast(string)sym.name);
}
return *p;
}
else
{
symbols[sym.name] = sym;
if (cast(ExternSymbol)sym)
undefined ~= sym;
return sym;
}
}
void add(Import imp)
{
if (imp.modname in imports)
{
auto imps = imports[imp.modname];
foreach(i; imps)
{
if (i.expName == imp.expName)
{
enforce(false, "Multiple definitions of import " ~ cast(string)imp.expName);
}
}
}
imports[imp.modname] ~= imp;
}
bool hasUndefined()
{
return undefined.length != 0;
}
void dump()
{
writeln("Symbol Table:");
foreach(s; symbols)
s.dump();
}
void dumpUndefined()
{
writeln("Undefined Symbols:");
foreach(s; undefined)
s.dump();
}
void makeMap(string fn)
{
auto f = File(fn, "w");
foreach(s; symbols)
{
f.writefln("%.8X\t%s", s.getAddress(), cast(char[])s.name);
}
}
void merge()
{
assert(parent);
foreach(ref sym; symbols)
{
if (!sym.isLocal)
{
parent.add(sym);
sym = null;
}
else
parent.addHidden(sym);
}
foreach(lib, imps; imports)
{
foreach(i; imps)
{
parent.add(i);
}
}
}
void addHidden(Symbol sym)
{
assert(sym.isLocal);
hiddenSyms ~= sym;
}
void checkUnresolved()
{
size_t undefcount;
foreach(s; undefined)
{
if (!parent || s.isLocal)
{
writeln("Error: No definition for symbol: ", cast(string)s.name);
undefcount++;
}
}
enforce(undefcount == 0, "Error: " ~ to!string(undefcount) ~ " unresolved symbols found");
if (!parent)
{
enforce(entryPoint.length, "Error: No entry point defined");
if (!searchName(entryPoint))
{
enforce(false, "Entry point '" ~ cast(string)entryPoint ~ "' not defined");
}
}
}
void defineImports(SectionTable sectab)
{
auto sec = new Section(cast(immutable(ubyte)[])".idata", SectionClass.IData, SectionAlign.align_2, 0);
auto tsec = new Section(cast(immutable(ubyte)[])"TEXT$ImportThunks", SectionClass.Code, SectionAlign.align_4, 0);
size_t offset;
size_t toffset;
enum dirEntrySize = 5 * 4;
enum importEntrySize = 4;
// Import Directory
offset += imports.length * dirEntrySize;
offset += dirEntrySize; // null entry
// Import Lookup Tables
foreach(lib, syms; imports)
{
//writefln("lib %s - %d symbols", cast(string)lib, syms.length);
offset += (syms.length + 1) * importEntrySize;
}
// Hint-Name Table
foreach(lib, imps; imports)
{
offset += (1 + lib.length + 1) & ~1;
foreach(imp; imps)
if (imp.expName.length)
offset += (3 + imp.expName.length + 1) & ~1;
}
foreach(lib, imps; imports)
{
foreach(imp; imps)
{
//writefln("Import: %s at %.8X", cast(string)imp.expName, offset);
imp.address.sec = sec;
imp.address.offset = offset;
offset += importEntrySize;
assert(imp.thunk);
imp.thunk.sec = tsec;
imp.thunk.offset = toffset;
// sym.sec = tsec;
// sym.offset = toffset;
toffset += 6;
}
offset += importEntrySize;
}
sec.exactlength = offset;
sec.alignedlength = offset.alignTo(sec.secalign);
//writeln("Defined import segment: ", offset, " bytes");
sectab.add(sec);
tsec.exactlength = toffset;
tsec.alignedlength = toffset.alignTo(tsec.secalign);
sectab.add(tsec);
}
void buildImports(ubyte[] data, uint base)
{
void writeWordLE(ref uint offset, ushort d)
{
(cast(ushort[])data[offset..offset+2])[0] = d;
offset += 2;
}
void writeDwordLE(ref uint offset, uint d)
{
(cast(uint[])data[offset..offset+4])[0] = d;
offset += 4;
}
void writeByte(ref uint offset, ubyte d)
{
data[offset] = d;
offset++;
}
void writeString(ref uint offset, in ubyte[] str)
{
data[offset..offset+str.length] = str[];
offset += str.length;
}
void writeHint(ref uint offset, immutable(ubyte)[] name)
{
offset += 2;
writeString(offset, name);
offset++;
if (offset & 1)
offset++;
}
enum idataRVA = 0x1000;
auto idataVA = base + idataRVA;
uint totalImports;
foreach(lib, imps; imports)
{
//writefln("lib %s - %d symbols", cast(string)lib, imps.length);
totalImports += imps.length + 1;
}
uint directoryOffset = 0;
uint lookupOffset = (imports.length + 1) * 5 * 4;
uint hintOffset = lookupOffset + (totalImports * 4);
uint libHintOffset = hintOffset;
foreach(lib, imps; imports)
foreach(imp; imps)
if (imp.expName.length)
libHintOffset += (3 + imp.expName.length + 1) & ~1;
uint addressOffset = libHintOffset;
foreach(lib, imps; imports)
addressOffset += (1 + lib.length + 1) & ~1;
foreach(libname, imps; imports)
{
writeDwordLE(directoryOffset, lookupOffset + idataRVA);
writeDwordLE(directoryOffset, 0);
writeDwordLE(directoryOffset, 0);
writeDwordLE(directoryOffset, libHintOffset + idataRVA);
writeDwordLE(directoryOffset, addressOffset + idataRVA);
foreach(imp; imps)
{
//writefln("Import: %s at %.8X", cast(string)sym.name, addressOffset);
assert(imp.thunk);
auto tsec = imp.thunk.sec;
auto tdata = tsec.data;
tdata[imp.thunk.offset..imp.thunk.offset+2] = [0xFF, 0x25];
(cast(uint[])tdata[imp.thunk.offset+2..imp.thunk.offset+6])[0] = addressOffset + idataVA;
if (imp.expName.length)
{
writeDwordLE(lookupOffset, hintOffset + idataRVA);
writeDwordLE(addressOffset, hintOffset + idataRVA);
writeHint(hintOffset, imp.expName);
}
else
{
writeDwordLE(lookupOffset, 0x80000000 | imp.expOrd);
writeDwordLE(addressOffset, 0x80000000 | imp.expOrd);
}
}
lookupOffset += 4;
addressOffset += 4;
writeString(libHintOffset, libname);
libHintOffset++;
}
}
void defineSpecial(SectionTable sectab, uint base)
{
auto textend = new Section(cast(immutable(ubyte)[])"__textend", SectionClass.Code, SectionAlign.align_1, 0);
auto dataend = new Section(cast(immutable(ubyte)[])"__dataend", SectionClass.Data, SectionAlign.align_1, 0);
auto bssend = new Section(cast(immutable(ubyte)[])"__bssend", SectionClass.BSS, SectionAlign.align_1, 0);
sectab.add(textend);
sectab.add(dataend);
sectab.add(bssend);
add(new AbsoluteSymbol(cast(immutable(ubyte)[])"___ImageBase", base));
add(new PublicSymbol(textend, cast(immutable(ubyte)[])"_etext", 0));
add(new PublicSymbol(textend, cast(immutable(ubyte)[])"__etext", 0));
add(new PublicSymbol(dataend, cast(immutable(ubyte)[])"_edata", 0));
add(new PublicSymbol(dataend, cast(immutable(ubyte)[])"__edata", 0));
add(new PublicSymbol(bssend, cast(immutable(ubyte)[])"_end", 0));
add(new PublicSymbol(bssend, cast(immutable(ubyte)[])"__end", 0));
}
void allocateComdef(SectionTable sectab)
{
foreach(sym; symbols)
{
if (auto s = cast(ComdefSymbol)sym)
{
auto sec = new Section(cast(immutable(ubyte)[])"_DATA$" ~ s.name, SectionClass.Data, SectionAlign.align_1, s.size);
s.sec = sec;
sectab.add(sec);
}
}
foreach(sym; hiddenSyms)
{
if (auto s = cast(ComdefSymbol)sym)
{
auto sec = new Section(cast(immutable(ubyte)[])"_DATA$" ~ s.name, SectionClass.Data, SectionAlign.align_1, s.size);
s.sec = sec;
sectab.add(sec);
}
}
}
private:
void removeUndefined(Symbol s)
{
foreach(i, v; undefined)
{
if (v is s)
{
undefined = undefined[0..i] ~ undefined[i+1..$];
return;
}
}
assert(0);
}
}