-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patholink.d
148 lines (129 loc) · 3.93 KB
/
olink.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
import std.algorithm;
import std.array;
import std.exception;
import std.file;
import std.path;
import std.stdio;
import std.string;
import linker;
import paths;
import driver;
import pe;
import sectiontable;
import symboltable;
void main(string[] args)
{
bool map;
bool dump;
bool codeview;
string[][] files;
string[] objFilenames;
string outFilename;
string mapFilename;
string defFilename;
string[] resFilenames;
Paths paths = new Paths();
paths.add(".");
paths.addLINK();
size_t parseSwitch(string arg)
{
assert(arg[0] == '/');
auto i = 1;
while (i < arg.length && arg[i] != '/' && arg[i] != ' ' && arg[i] != ';')
i++;
if (!icmp(arg[0..i], "/MAP") || !icmp(arg[0..i], "/M"))
{
map = true;
} else if (!icmp(arg[0..i], "/CO"))
{
codeview = true;
} else if (!icmp(arg[0..i], "/NOI"))
{
} else if (!icmp(arg[0..i], "/DUMP"))
{
dump = true;
} else
{
assert(0, "Unrecognised switch: " ~ arg[0..i]);
}
if (i < arg.length && arg[i] == ';')
i++;
return i;
}
foreach(arg; args[1..$])
{
//writeln("arg: ", arg);
auto i = 0;
if (arg[i] != '/' && files.length == 0)
{
auto start = i;
string[] current;
do
{
if (i != start && (arg[i] == '/' || arg[i] == '+' || arg[i] == ','))
{
current ~= arg[start..i];
start = i+1;
}
if (arg[i] == '/' || arg[i] == ',')
{
start = i+1;
files ~= current;
current = null;
}
if (arg[i] == '/')
break;
i++;
} while (i < arg.length);
if (i != start && i == arg.length)
current ~= arg[start..i];
if (current.length)
files ~= current;
}
while (i < arg.length && arg[i] == '/')
i += parseSwitch(arg[i..$]);
assert(arg.length == i, "Unrecognised argument: " ~ arg[i..$]);
}
assert(files.length <= 6, "Too many parameters");
files.length = 6;
assert(files[0].length, "Must specify at least one object file");
assert(files[1].length <= 1, "Can only specify one output filename");
assert(files[2].length <= 1, "Can only specify one map filename");
assert(files[4].length == 0, "ylink can't actually handle def files yet");
assert(files[5].length == 0, "ylink can't actually handle res files yet");
foreach(f; files[0])
objFilenames ~= f.defaultExtension("obj");
if (files[1].length)
outFilename = files[1][0].defaultExtension("exe");
else
outFilename = files[0][0].baseName().setExtension("exe");
if (files[2].length && !icmp(files[2][0].baseName().stripExtension(), "nul"))
map = false;
else if (files[2].length)
mapFilename = files[2][0].defaultExtension("map");
else
mapFilename = outFilename.setExtension("map");
foreach(f; files[3])
objFilenames ~= f.defaultExtension("lib");
if (files[4].length)
defFilename = files[4][0].defaultExtension("def");
else
defFilename = outFilename.setExtension("def");
foreach(f; files[5])
resFilenames ~= f.defaultExtension("res");
auto sectab = new SectionTable();
auto symtab = new SymbolTable(null);
auto objects = loadObjects(objFilenames, paths, symtab, sectab);
finalizeLoad(symtab, sectab);
auto segments = generateSegments(objects, symtab, sectab);
if (dump)
{
sectab.dump();
symtab.dump();
foreach(seg; segments)
seg.dump();
}
buildPE(outFilename, segments, symtab);
if (map)
symtab.makeMap(mapFilename);
}