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");
|
2017-05-01 10:12:38 -07:00
|
|
|
const builtin = @import("builtin");
|
2016-02-15 22:30:05 -08:00
|
|
|
|
2017-06-13 21:04:34 -07:00
|
|
|
const want_main_symbol = builtin.link_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-04-03 15:11:57 -07:00
|
|
|
var argc_ptr: &usize = undefined;
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2017-06-13 21:04:34 -07:00
|
|
|
const is_windows = builtin.os == builtin.Os.windows;
|
|
|
|
|
2017-03-26 01:58:48 -07:00
|
|
|
export nakedcc fn _start() -> noreturn {
|
2017-03-22 23:59:58 -07:00
|
|
|
if (!want_start_symbol) {
|
2017-05-01 10:12:38 -07:00
|
|
|
@setGlobalLinkage(_start, builtin.GlobalLinkage.Internal);
|
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-06-13 21:04:34 -07:00
|
|
|
if (is_windows) {
|
|
|
|
windowsCallMainAndExit()
|
|
|
|
}
|
|
|
|
|
2017-05-01 10:12:38 -07:00
|
|
|
switch (builtin.arch) {
|
|
|
|
builtin.Arch.x86_64 => {
|
2017-04-30 08:28:11 -07:00
|
|
|
argc_ptr = asm("lea (%%rsp), %[argc]": [argc] "=r" (-> &usize));
|
2016-02-12 01:04:46 -08:00
|
|
|
},
|
2017-05-01 10:12:38 -07:00
|
|
|
builtin.Arch.i386 => {
|
2017-04-30 08:28:11 -07:00
|
|
|
argc_ptr = asm("lea (%%esp), %[argc]": [argc] "=r" (-> &usize));
|
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
|
|
|
}
|
2017-06-13 21:04:34 -07:00
|
|
|
posixCallMainAndExit()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn windowsCallMainAndExit() -> noreturn {
|
|
|
|
std.debug.user_main_fn = root.main;
|
|
|
|
root.main() %% std.os.windows.ExitProcess(1);
|
|
|
|
std.os.windows.ExitProcess(0);
|
2016-01-15 23:07:20 -08:00
|
|
|
}
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2017-06-13 21:04:34 -07:00
|
|
|
fn posixCallMainAndExit() -> noreturn {
|
2017-04-03 15:11:57 -07:00
|
|
|
const argc = *argc_ptr;
|
2017-04-21 07:39:13 -07:00
|
|
|
const argv = @ptrCast(&&u8, &argc_ptr[1]);
|
|
|
|
const envp = @ptrCast(&?&u8, &argv[argc + 1]);
|
2017-06-13 21:04:34 -07:00
|
|
|
callMain(argc, argv, envp) %% std.os.posix.exit(1);
|
|
|
|
std.os.posix.exit(0);
|
2017-04-03 15:11:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {
|
2017-05-19 07:39:59 -07:00
|
|
|
std.os.args.raw = argv[0..argc];
|
2017-04-02 16:14:23 -07:00
|
|
|
|
|
|
|
var env_count: usize = 0;
|
2017-05-03 15:12:07 -07:00
|
|
|
while (envp[env_count] != null) : (env_count += 1) {}
|
2017-05-19 07:39:59 -07:00
|
|
|
std.os.environ_raw = @ptrCast(&&u8, envp)[0..env_count];
|
2017-04-02 16:14:23 -07:00
|
|
|
|
2017-04-24 09:01:19 -07:00
|
|
|
std.debug.user_main_fn = root.main;
|
|
|
|
|
2017-04-03 21:17:24 -07:00
|
|
|
return root.main();
|
2016-02-27 21:06:46 -08:00
|
|
|
}
|
|
|
|
|
2017-04-02 16:14:23 -07:00
|
|
|
export fn main(c_argc: i32, c_argv: &&u8, c_envp: &?&u8) -> i32 {
|
2017-03-22 23:59:58 -07:00
|
|
|
if (!want_main_symbol) {
|
2017-05-01 10:12:38 -07:00
|
|
|
@setGlobalLinkage(main, builtin.GlobalLinkage.Internal);
|
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-04-03 15:11:57 -07:00
|
|
|
callMain(usize(c_argc), c_argv, c_envp) %% return 1;
|
2016-02-15 22:30:05 -08:00
|
|
|
return 0;
|
|
|
|
}
|