Skip to content

Commit 91c8d3f

Browse files
committed
link crt1.o when using libc
1 parent 9d49586 commit 91c8d3f

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

source/app.d

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Flags:
3737
-dv VER - Disables VER
3838
-h FILE - Puts the contents of FILE at the top of the assembly output
3939
-bo OPT - Backend option, see below
40+
-ka - Keep assembly
4041
4142
Backends:
4243
rm86 - Real mode x86 and MS-DOS
@@ -76,6 +77,7 @@ int main(string[] args) {
7677
string[] disabled;
7778
string header;
7879
string[] backendOpts;
80+
bool keepAssembly;
7981

8082
for (size_t i = 1; i < args.length; ++ i) {
8183
if (args[i][0] == '-') {
@@ -238,6 +240,10 @@ int main(string[] args) {
238240
backendOpts ~= args[i];
239241
break;
240242
}
243+
case "-ka": {
244+
keepAssembly = true;
245+
break;
246+
}
241247
default: {
242248
stderr.writefln("Unknown flag '%s'", args[i]);
243249
return 1;
@@ -299,6 +305,7 @@ int main(string[] args) {
299305
compiler.backend.useDebug = doDebug;
300306
compiler.backend.exportSymbols = exportSymbols;
301307
compiler.backend.link ~= link;
308+
backend.keepAssembly = keepAssembly;
302309

303310
versions ~= compiler.backend.GetVersions();
304311

source/backends/linux86.d

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,30 @@ class BackendLinux86 : CompilerBackend {
175175
linkCommand ~= " -dynamic-linker /lib64/ld-linux-x86-64.so.2";
176176
}
177177

178+
if (useLibc) {
179+
string[] possiblePaths = [
180+
"/usr/lib/crt1.o",
181+
"/usr/lib/x86_64-linux-gnu/crt1.o"
182+
];
183+
bool crt1;
184+
185+
foreach (ref path ; possiblePaths) {
186+
if (exists(path)) {
187+
crt1 = true;
188+
linkCommand ~= format(" %s", path);
189+
}
190+
}
191+
192+
linkCommand ~= " /usr/lib/crt1.o";
193+
}
194+
178195
ret ~= linkCommand;
179196

180-
return ret ~ format("rm %s.asm %s.o", compiler.outFile, compiler.outFile);
197+
if (!keepAssembly) {
198+
ret ~= format("rm %s.asm %s.o", compiler.outFile, compiler.outFile);
199+
}
200+
201+
return ret;
181202
}
182203

183204
override long MaxInt() => -1;
@@ -201,7 +222,8 @@ class BackendLinux86 : CompilerBackend {
201222
}
202223

203224
override void Init() {
204-
if (!useLibc) output ~= "global _start\n";
225+
if (useLibc) output ~= "global main\n";
226+
else output ~= "global _start\n";
205227

206228
output ~= "section .text\n";
207229

source/backends/rm86.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class BackendRM86 : CompilerBackend {
147147
override string[] FinalCommands() => [
148148
format("mv %s %s.asm", compiler.outFile, compiler.outFile),
149149
format("nasm -f bin %s.asm -o %s", compiler.outFile, compiler.outFile),
150-
format("rm %s.asm", compiler.outFile)
150+
keepAssembly? "" : format("rm %s.asm", compiler.outFile)
151151
];
152152

153153
override long MaxInt() => 0xFFFF;

source/backends/uxn.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class BackendUXN : CompilerBackend {
109109
override string[] FinalCommands() => [
110110
format("mv %s %s.tal", compiler.outFile, compiler.outFile),
111111
format("uxnasm %s.tal %s", compiler.outFile, compiler.outFile),
112-
format("rm %s.tal", compiler.outFile)
112+
keepAssembly? "" : format("rm %s.tal", compiler.outFile)
113113
];
114114

115115
override long MaxInt() => 0xFFFF;

source/compiler.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CompilerBackend {
1818
bool useDebug;
1919
bool exportSymbols;
2020
string[] link;
21+
bool keepAssembly;
2122

2223
abstract string[] GetVersions();
2324
abstract string[] FinalCommands();

0 commit comments

Comments
 (0)