-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathylink.d
119 lines (103 loc) · 2.7 KB
/
ylink.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
import std.exception;
import std.file;
import std.path;
import std.process;
import std.string;
import std.stdio;
import std.getopt;
import linker;
import objectfile;
import omfobjectfile;
import paths;
import pe;
import relocation;
import sectiontable;
import segment;
import symboltable;
import workqueue;
import driver;
void usage(string[] args)
{
string program = (args == null || args.length <= 0) ? "ylink" : args[0];
writefln("%s [options...] <object-files>...", baseName(program));
writeln( " -o<file> | --output<file> Output file (default is the first .obj file with");
writeln( " the .exe extension)");
writeln( " -L<path> Path to include");
writeln( " -d | --dump Dump the linker tables");
writeln( " -m | --map Create map file");
writeln( " -v | --verbose Set verbose output");
}
int main(string[] args)
{
bool dump;
bool map;
string[] objectFilenames;
string[] includePaths;
string outputfile = null;
getopt(args,
"o|output" , &outputfile,
"L" , &includePaths,
"d|dump" , &dump,
"m|map" , &map,
"v|verbose", &verbosity);
if (args.length <= 1)
{
usage(args);
return 1;
}
string firstObjFile = null;
for (auto i = 1; i < args.length; i++)
{
string file = args[i];
switch (extension(file))
{
default:
args[i] = args[i].defaultExtension("obj");
goto case;
case ".obj":
if (firstObjFile == null)
firstObjFile = args[i].setExtension("exe");
goto case;
case ".lib":
objectFilenames ~= args[i].defaultExtension("obj");
break;
}
}
if (firstObjFile == null)
{
writeln("Error: you must provide at least one .obj file");
usage(args);
return 1;
}
if (outputfile == null)
outputfile = firstObjFile;
//
// Add Include Paths
//
Paths paths = new Paths();
paths.add(".");
foreach (includePath; includePaths)
{
paths.add(includePath);
}
paths.addLINK();
//
// Add object filenames
//
auto sectab = new SectionTable();
auto symtab = new SymbolTable(null);
auto objects = loadObjects(objectFilenames, paths, symtab, sectab);
finalizeLoad(symtab, sectab);
auto segments = generateSegments(objects, symtab, sectab);
if (dump)
{
sectab.dump();
symtab.dump();
foreach (seg; segments)
seg.dump();
}
buildPE(outputfile, segments, symtab);
if (map)
symtab.makeMap(outputfile.setExtension("map"));
return 0;
}