2016-02-27 21:06:46 -08:00
|
|
|
// This file is in a package which has the root source file exposed as "@root".
|
2015-12-10 14:34:38 -08:00
|
|
|
|
2016-02-27 21:06:46 -08:00
|
|
|
const root = @import("@root");
|
2016-03-01 13:11:38 -08:00
|
|
|
const linux = @import("linux.zig");
|
2016-02-15 22:30:05 -08:00
|
|
|
|
|
|
|
const want_start_symbol = switch(@compile_var("os")) {
|
|
|
|
linux => true,
|
|
|
|
else => false,
|
|
|
|
};
|
|
|
|
const want_main_symbol = !want_start_symbol;
|
2016-01-04 02:31:57 -08:00
|
|
|
|
2016-01-25 14:45:05 -08:00
|
|
|
var argc: isize = undefined;
|
|
|
|
var argv: &&u8 = undefined;
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2015-12-10 14:34:38 -08:00
|
|
|
#attribute("naked")
|
2016-02-15 22:30:05 -08:00
|
|
|
#condition(want_start_symbol)
|
2016-01-25 16:06:19 -08:00
|
|
|
export fn _start() -> unreachable {
|
2016-02-12 01:04:46 -08:00
|
|
|
switch (@compile_var("arch")) {
|
|
|
|
x86_64 => {
|
|
|
|
argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> isize));
|
|
|
|
argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
|
|
|
|
},
|
|
|
|
i386 => {
|
|
|
|
argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> isize));
|
|
|
|
argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
|
|
|
|
},
|
|
|
|
else => unreachable{},
|
|
|
|
}
|
2016-02-27 21:06:46 -08:00
|
|
|
call_main_and_exit()
|
2016-01-15 23:07:20 -08:00
|
|
|
}
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2016-01-25 16:06:19 -08:00
|
|
|
fn strlen(ptr: &const u8) -> isize {
|
2016-01-18 20:13:14 -08:00
|
|
|
var count: isize = 0;
|
2016-01-15 23:07:20 -08:00
|
|
|
while (ptr[count] != 0) {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2016-02-27 21:06:46 -08:00
|
|
|
fn call_main() -> %void {
|
2016-01-25 22:56:46 -08:00
|
|
|
var args: [argc][]u8 = undefined;
|
2016-02-05 16:15:19 -08:00
|
|
|
for (args) |arg, i| {
|
2016-01-13 17:15:51 -08:00
|
|
|
const ptr = argv[i];
|
|
|
|
args[i] = ptr[0...strlen(ptr)];
|
|
|
|
}
|
2016-02-27 21:06:46 -08:00
|
|
|
return root.main(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_main_and_exit() -> unreachable {
|
2016-03-01 13:11:38 -08:00
|
|
|
call_main() %% linux.exit(1);
|
|
|
|
linux.exit(0);
|
2015-12-10 14:34:38 -08:00
|
|
|
}
|
2016-02-15 22:30:05 -08:00
|
|
|
|
|
|
|
#condition(want_main_symbol)
|
2016-02-27 21:06:46 -08:00
|
|
|
export fn main(c_argc: i32, c_argv: &&u8) -> i32 {
|
|
|
|
argc = c_argc;
|
|
|
|
argv = c_argv;
|
|
|
|
call_main() %% return 1;
|
2016-02-15 22:30:05 -08:00
|
|
|
return 0;
|
|
|
|
}
|