-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsectiontable.d
169 lines (154 loc) · 5.98 KB
/
sectiontable.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
import std.algorithm;
import std.conv;
import std.exception;
import std.stdio;
import section;
import segment;
final class SectionTable
{
CombinedSection[][][SectionClass.max+1] sections;
immutable(ubyte)[][][SectionClass.max+1] secNames;
CombinedSection add(Section sec)
{
int nameIndex = -1;
foreach(i, name; secNames[sec.secclass])
{
if (name == sec.name)
{
nameIndex = i;
break;
}
}
if (nameIndex == -1)
{
nameIndex = secNames[sec.secclass].length;
secNames[sec.secclass] ~= sec.name;
sections[sec.secclass].length++;
}
CombinedSection sx;
foreach(s; sections[sec.secclass][nameIndex])
{
if (s.tag == sec.tag)
{
sx = s;
break;
}
}
if (!sx)
{
sx = new CombinedSection(sec.name, sec.tag, sec.secclass);
sections[sec.secclass][nameIndex] ~= sx;
}
//enforce(sec.secclass == s.secclass, "Section " ~ cast(string)sec.name ~ " is in multiple classes");
sx.append(sec);
return sx;
}
void dump()
{
writeln();
foreach(secclass, nameGroups; sections)
{
writeln("%s segments:", secclass);
foreach(nameGroup; nameGroups)
{
writefln("%s: 0x%.8X", cast(string)nameGroup[0].name, nameGroup[0].base);
}
}
writeln();
writeln("Section Table:");
foreach(secclass, nameGroup; sections)
{
writeln(cast(SectionClass)secclass, " sections");
foreach(secs; nameGroup)
{
foreach(s; secs)
s.dump();
}
}
}
private void segAppend(Segment seg, CombinedSection[][] nameGroup)
{
foreach(secs; nameGroup)
{
static int cmp(immutable(ubyte)[] a, immutable(ubyte)[] b)
{
import std.ascii;
if (!a.length || !b.length)
return cast(int)a.length - cast(int)b.length;
if (a[0] == b[0])
return cmp(a[1..$], b[1..$]);
if (isPunctuation(a[0]) == isPunctuation(b[0]))
return cast(int)a[0] - cast(int)b[0];
return isPunctuation(a[0]) ? -1 : 1;
}
sort!((a, b) => cmp(a.tag, b.tag) < 0, SwapStrategy.stable)(secs);
foreach(sec; secs)
{
//writefln("Appending %s:%s", cast(string)sec.name, cast(string)sec.tag);
seg.append(sec);
}
}
}
Segment[SegmentType] allocateSegments(uint baseAddress, uint segAlign, uint fileAlign)
{
//Import,Export,Text,TLS,CRT+Data,Const,BSS,Reloc,Debug,
Segment[SegmentType] segs;
auto offset = baseAddress + 0x1000;
auto fileOffset = 0x400;
auto Import = new Segment(SegmentType.Import, offset, fileOffset);
segAppend(Import, sections[SectionClass.IData]);
Import.length = (Import.length + fileAlign - 1) & ~(fileAlign - 1);
offset = (offset + Import.length + segAlign - 1) & ~(segAlign - 1);
fileOffset += Import.length;
auto Export = new Segment(SegmentType.Export, offset, fileOffset);
segAppend(Export, sections[SectionClass.EData]);
offset = (offset + Export.length + segAlign - 1) & ~(segAlign - 1);
fileOffset = (fileOffset + Import.length + fileAlign - 1) & ~(fileAlign - 1);
auto Text = new Segment(SegmentType.Text, offset, fileOffset);
segAppend(Text, sections[SectionClass.Code]);
Text.length = (Text.length + fileAlign - 1) & ~(fileAlign - 1);
offset = (offset + Text.length + segAlign - 1) & ~(segAlign - 1);
fileOffset += Text.length;
auto TLS = new Segment(SegmentType.TLS, offset, fileOffset);
segAppend(TLS, sections[SectionClass.TLS]);
TLS.length = (TLS.length + fileAlign - 1) & ~(fileAlign - 1);
offset = (offset + TLS.length + segAlign - 1) & ~(segAlign - 1);
fileOffset += TLS.length;
auto Data = new Segment(SegmentType.Data, offset, fileOffset);
segAppend(Data, sections[SectionClass.CRT]);
segAppend(Data, sections[SectionClass.Data]);
Data.length = (Data.length + fileAlign - 1) & ~(fileAlign - 1);
offset = (offset + Data.length + segAlign - 1) & ~(segAlign - 1);
fileOffset += Data.length;
auto Const = new Segment(SegmentType.Const, offset, fileOffset);
segAppend(Const, sections[SectionClass.Const]);
Const.length = (Const.length + fileAlign - 1) & ~(fileAlign - 1);
offset = (offset + Const.length + segAlign - 1) & ~(segAlign - 1);
fileOffset += Const.length;
auto BSS = new Segment(SegmentType.BSS, offset, 0);
segAppend(BSS, sections[SectionClass.BSS]);
offset = (offset + BSS.length + segAlign - 1) & ~(segAlign - 1);
Import.allocate(segAlign);
Export.allocate(segAlign);
Text.allocate(segAlign);
TLS.allocate(segAlign);
Data.allocate(segAlign);
Const.allocate(segAlign);
size_t segid = 0;
if (Import.length) Import.segid = segid++;
if (Export.length) Export.segid = segid++;
if (Text.length) Text.segid = segid++;
if (TLS.length) TLS.segid = segid++;
if (Data.length) Data.segid = segid++;
if (Const.length) Const.segid = segid++;
if (BSS.length) BSS.segid = segid++;
if (Import.length) segs[SegmentType.Import] = Import;
if (Export.length) segs[SegmentType.Export] = Export;
if (Text.length) segs[SegmentType.Text] = Text;
if (TLS.length) segs[SegmentType.TLS] = TLS;
if (Data.length) segs[SegmentType.Data] = Data;
if (Const.length) segs[SegmentType.Const] = Const;
if (BSS.length) segs[SegmentType.BSS] = BSS;
return segs;
}
}