-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmlink.d
100 lines (87 loc) · 2.56 KB
/
mlink.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
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;
import coffobjectfile : parseMSLinkerSwitch;
void main(string[] args)
{
bool map;
string[] objFilenames;
string outFilename;
string mapFilename;
Paths paths = new Paths();
paths.add(".");
paths.addLINK();
foreach(i, s; args[1..$])
{
//writeln("arg: ", arg);
while (s.length)
{
string arg;
string val;
if (!parseMSLinkerSwitch(s, arg, val, true))
{
enforce(s[0] != '/', "Invalid linker command line: " ~ args[i+1]);
objFilenames ~= s;
s = null;
break;
}
switch(arg)
{
case "/MAP":
assert(!map, "Can only specify one map filename");
map = true;
if (val.length)
mapFilename = val;
break;
case "/OUT":
assert(!outFilename.length, "Can only specify one output filename");
assert(val.length, "Invalid output filename");
outFilename = val;
break;
case "/LIBPATH":
assert(val.length, "Invalid library path");
paths.add(val);
break;
default:
enforce(0, "Unknown linker directive: " ~ arg ~ ":" ~ val);
}
}
}
assert(objFilenames.length, "Must specify at least one object file");
foreach(ref f; objFilenames)
f = f.defaultExtension("obj");
if (outFilename.length)
outFilename = outFilename.defaultExtension("exe");
else
outFilename = outFilename.baseName().setExtension("exe");
if (mapFilename.length)
mapFilename = mapFilename.defaultExtension("map");
else
mapFilename = outFilename.setExtension("map");
auto sectab = new SectionTable();
auto symtab = new SymbolTable(null);
symtab.setEntry(cast(immutable(ubyte)[])"_mainCRTStartup");
auto objects = loadObjects(objFilenames, paths, symtab, sectab);
finalizeLoad(symtab, sectab);
auto segments = generateSegments(objects, symtab, sectab);
if (false)
{
sectab.dump();
symtab.dump();
foreach(seg; segments)
seg.dump();
}
buildPE(outFilename, segments, symtab);
if (map)
symtab.makeMap(mapFilename);
}