-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsymbol.d
234 lines (219 loc) · 4.77 KB
/
symbol.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
import std.conv;
import std.stdio;
import modules;
import objectfile;
import section;
enum Comdat
{
Unique,
Any,
Max,
}
abstract class Symbol
{
immutable(ubyte)[] name;
bool isLocal;
uint refCount;
this(immutable(ubyte)[] name, uint refCount, bool isLocal = false)
{
this.name = name;
this.refCount = refCount;
this.isLocal = isLocal;
}
abstract void dump();
abstract uint getAddress();
abstract uint getSegment();
}
final class PublicSymbol : Symbol
{
Section sec;
uint offset;
this(Section sec, immutable(ubyte)[] name, uint offset)
{
super(name, 0);
this.sec = sec;
this.offset = offset;
}
override void dump()
{
writeln("Public: ", cleanString(name), " = ", sec ? cast(string)sec.fullname : "_abs_", " (", cast(void*)sec, ")", "+", offset, " (", refCount, ")");
}
override uint getAddress()
{
assert(sec);
return sec.base + offset;
}
override uint getSegment()
{
assert(sec);
return sec.container.seg.segid;
}
}
final class AbsoluteSymbol : Symbol
{
uint offset;
this(immutable(ubyte)[] name, uint offset)
{
super(name, 0);
this.offset = offset;
}
override void dump()
{
writeln("Absolute: ", cleanString(name), " = ", offset);
}
override uint getAddress()
{
return offset;
}
override uint getSegment()
{
return 0;
}
}
final class ExternSymbol : Symbol
{
Symbol sym;
this(immutable(ubyte)[] name)
{
super(name, 1);
}
override void dump()
{
writeln("Extern: ", cleanString(name), " (", refCount, ")");
}
override uint getAddress()
{
assert(0);
}
override uint getSegment()
{
assert(0);
}
}
final class ComdatSymbol : Symbol
{
Section sec;
uint offset;
Comdat comdat;
this(Section sec, immutable(ubyte)[] name, uint offset, Comdat comdat, bool isLocal)
{
super(name, 0, isLocal);
this.sec = sec;
this.offset = offset;
this.comdat = comdat;
}
override void dump()
{
writeln("Comdat: ", cleanString(name), " = ", sec ? cast(string)sec.fullname : "_abs_", "+", offset, " (", comdat, ")", isLocal ? " Local" : "", " (", refCount, ")");
}
override uint getAddress()
{
assert(sec);
return sec.base + offset;
}
override uint getSegment()
{
assert(sec);
return sec.container.seg.segid;
}
}
final class ComdefSymbol : Symbol
{
Section sec;
uint size;
this(immutable(ubyte)[] name, uint size)
{
super(name, 0);
this.size = size;
}
override void dump()
{
writeln("Comdef: ", cleanString(name), " (", size, ")", " (", refCount, ")");
}
override uint getAddress()
{
assert(sec, cleanString(name));
return sec.base;
}
override uint getSegment()
{
assert(sec);
return sec.container.seg.segid;
}
}
final class Import
{
immutable(ubyte)[] modname;
ushort expOrd;
immutable(ubyte)[] expName;
ImportThunkSymbol thunk;
ImportAddressSymbol address;
this(immutable(ubyte)[] modname, ushort expOrd, immutable(ubyte)[] expName)
{
this.modname = modname;
this.expOrd = expOrd;
this.expName = expName;
}
}
final class ImportThunkSymbol : Symbol
{
Import imp;
Section sec;
uint offset;
this(immutable(ubyte)[] name, Import imp)
{
super(name, 0);
this.imp = imp;
}
override void dump()
{
writeln("ImportThunk: ", cleanString(name), " = ", cast(string)imp.modname, ":", imp.expName.length ? cast(string)imp.expName : to!string(imp.expOrd), " (", refCount, ")");
}
override uint getAddress()
{
assert(sec);
return sec.base + offset;
}
override uint getSegment()
{
assert(sec);
return sec.container.seg.segid;
}
}
final class ImportAddressSymbol : Symbol
{
Import imp;
Section sec;
uint offset;
this(immutable(ubyte)[] name, Import imp)
{
super(name, 0);
this.imp = imp;
}
override void dump()
{
writeln("ImportAddress: ", cleanString(name), " = ", cast(string)imp.modname, ":", imp.expName.length ? cast(string)imp.expName : to!string(imp.expOrd), " (", refCount, ")");
}
override uint getAddress()
{
assert(sec);
return sec.base + offset;
}
override uint getSegment()
{
assert(sec);
return sec.container.seg.segid;
}
}
string cleanString(immutable(ubyte)[] s)
{
string r;
foreach(c; s)
{
if (c > 0x7F)
r ~= '*';
else
r ~= c;
}
return r;
}