2016-01-15 17:45:52 -08:00
|
|
|
import "syscall.zig";
|
2015-12-10 14:34:38 -08:00
|
|
|
|
2016-01-04 02:31:57 -08:00
|
|
|
// The compiler treats this file special by implicitly importing the function `main`
|
|
|
|
// from the root source file.
|
|
|
|
|
2016-01-25 14:45:05 -08:00
|
|
|
var argc: isize = undefined;
|
|
|
|
var argv: &&u8 = undefined;
|
|
|
|
var env: &&u8 = undefined;
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2015-12-10 14:34:38 -08:00
|
|
|
#attribute("naked")
|
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));
|
|
|
|
env = asm("lea 0x10(%%rsp,[argc],8), %[env]": [env] "=r" (-> &&u8): [argc] "r" (argc));
|
|
|
|
},
|
|
|
|
i386 => {
|
|
|
|
argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> isize));
|
|
|
|
argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
|
|
|
|
env = asm("lea 0x8(%%esp,%[argc],4), %[env]": [env] "=r" (-> &&u8): [argc] "r" (argc));
|
|
|
|
},
|
|
|
|
else => unreachable{},
|
|
|
|
}
|
2016-01-15 23:07:20 -08:00
|
|
|
call_main()
|
|
|
|
}
|
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-01-25 16:06:19 -08:00
|
|
|
fn call_main() -> unreachable {
|
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-01-25 13:13:25 -08:00
|
|
|
main(args) %% exit(1);
|
|
|
|
exit(0);
|
2015-12-10 14:34:38 -08:00
|
|
|
}
|