2016-02-27 21:06:46 -08:00
|
|
|
// This file is in a package which has the root source file exposed as "@root".
|
2017-03-22 08:26:30 -07:00
|
|
|
// It is included in the compilation unit when exporting an executable.
|
2015-12-10 14:34:38 -08:00
|
|
|
|
2016-02-27 21:06:46 -08:00
|
|
|
const root = @import("@root");
|
2017-02-06 00:10:32 -08:00
|
|
|
const std = @import("std");
|
2016-02-15 22:30:05 -08:00
|
|
|
|
2017-03-26 03:39:28 -07:00
|
|
|
const want_main_symbol = std.target.linking_libc;
|
2017-03-22 08:26:30 -07:00
|
|
|
const want_start_symbol = !want_main_symbol;
|
2016-01-04 02:31:57 -08:00
|
|
|
|
2017-03-26 03:39:28 -07:00
|
|
|
const exit = std.os.posix.exit;
|
2017-02-23 13:13:57 -08:00
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
var argc: usize = undefined;
|
2016-01-25 14:45:05 -08:00
|
|
|
var argv: &&u8 = undefined;
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2017-03-26 01:58:48 -07:00
|
|
|
export nakedcc fn _start() -> noreturn {
|
2017-03-26 18:07:07 -07:00
|
|
|
@setGlobalLinkage(_start, if (want_start_symbol) GlobalLinkage.Strong else GlobalLinkage.Internal);
|
2017-03-22 23:59:58 -07:00
|
|
|
if (!want_start_symbol) {
|
2017-03-26 01:58:48 -07:00
|
|
|
unreachable;
|
2017-03-22 23:59:58 -07:00
|
|
|
}
|
2016-09-27 23:33:32 -07:00
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
switch (@compileVar("arch")) {
|
2016-12-17 14:48:07 -08:00
|
|
|
Arch.x86_64 => {
|
2017-03-23 15:59:43 -07:00
|
|
|
argc = asm("mov %[argc], [rsp]": [argc] "=r" (-> usize));
|
|
|
|
argv = asm("lea %[argv], [rsp + 8h]": [argv] "=r" (-> &&u8));
|
2016-02-12 01:04:46 -08:00
|
|
|
},
|
2016-12-17 14:48:07 -08:00
|
|
|
Arch.i386 => {
|
2017-03-23 15:59:43 -07:00
|
|
|
argc = asm("mov %[argc], [esp]": [argc] "=r" (-> usize));
|
|
|
|
argv = asm("lea %[argv], [esp + 4h]": [argv] "=r" (-> &&u8));
|
2016-02-12 01:04:46 -08:00
|
|
|
},
|
2016-09-05 14:01:54 -07:00
|
|
|
else => @compileError("unsupported arch"),
|
2016-02-12 01:04:46 -08:00
|
|
|
}
|
2016-08-16 22:42:50 -07:00
|
|
|
callMainAndExit()
|
2016-01-15 23:07:20 -08:00
|
|
|
}
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2016-08-16 22:42:50 -07:00
|
|
|
fn callMain() -> %void {
|
2016-12-17 14:48:07 -08:00
|
|
|
const args = @alloca([]u8, argc);
|
2016-12-17 17:47:35 -08:00
|
|
|
for (args) |_, i| {
|
2016-01-13 17:15:51 -08:00
|
|
|
const ptr = argv[i];
|
2017-02-23 13:13:57 -08:00
|
|
|
args[i] = ptr[0...std.cstr.len(ptr)];
|
2016-01-13 17:15:51 -08:00
|
|
|
}
|
2016-02-27 21:06:46 -08:00
|
|
|
return root.main(args);
|
|
|
|
}
|
|
|
|
|
2017-03-26 01:58:48 -07:00
|
|
|
fn callMainAndExit() -> noreturn {
|
2017-02-23 13:13:57 -08:00
|
|
|
callMain() %% exit(1);
|
|
|
|
exit(0);
|
2015-12-10 14:34:38 -08:00
|
|
|
}
|
2016-02-15 22:30:05 -08:00
|
|
|
|
2016-02-27 21:06:46 -08:00
|
|
|
export fn main(c_argc: i32, c_argv: &&u8) -> i32 {
|
2017-03-26 18:07:07 -07:00
|
|
|
@setGlobalLinkage(main, if (want_main_symbol) GlobalLinkage.Strong else GlobalLinkage.Internal);
|
2017-03-22 23:59:58 -07:00
|
|
|
if (!want_main_symbol) {
|
2017-03-26 01:58:48 -07:00
|
|
|
unreachable;
|
2017-03-22 23:59:58 -07:00
|
|
|
}
|
2016-09-27 23:33:32 -07:00
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
argc = usize(c_argc);
|
2016-02-27 21:06:46 -08:00
|
|
|
argv = c_argv;
|
2016-08-16 22:42:50 -07:00
|
|
|
callMain() %% return 1;
|
2016-02-15 22:30:05 -08:00
|
|
|
return 0;
|
|
|
|
}
|