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
|
|
|
|
2018-06-03 22:09:15 -07:00
|
|
|
var argc_ptr: [*]usize = undefined;
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2017-12-18 06:59:57 -08:00
|
|
|
comptime {
|
2017-12-18 23:39:43 -08:00
|
|
|
const strong_linkage = builtin.GlobalLinkage.Strong;
|
2017-12-18 06:59:57 -08:00
|
|
|
if (builtin.link_libc) {
|
2017-12-18 23:39:43 -08:00
|
|
|
@export("main", main, strong_linkage);
|
2017-12-18 06:59:57 -08:00
|
|
|
} else if (builtin.os == builtin.Os.windows) {
|
2017-12-18 23:39:43 -08:00
|
|
|
@export("WinMainCRTStartup", WinMainCRTStartup, strong_linkage);
|
2017-12-18 06:59:57 -08:00
|
|
|
} else {
|
2017-12-18 23:39:43 -08:00
|
|
|
@export("_start", _start, strong_linkage);
|
2017-03-22 23:59:58 -07:00
|
|
|
}
|
2017-12-18 06:59:57 -08:00
|
|
|
}
|
2016-09-27 23:33:32 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
nakedcc fn _start() noreturn {
|
2017-05-01 10:12:38 -07:00
|
|
|
switch (builtin.arch) {
|
|
|
|
builtin.Arch.x86_64 => {
|
2018-05-28 17:23:55 -07:00
|
|
|
argc_ptr = asm ("lea (%%rsp), %[argc]"
|
2018-06-03 22:09:15 -07:00
|
|
|
: [argc] "=r" (-> [*]usize)
|
2018-05-28 17:23:55 -07:00
|
|
|
);
|
2016-02-12 01:04:46 -08:00
|
|
|
},
|
2017-05-01 10:12:38 -07:00
|
|
|
builtin.Arch.i386 => {
|
2018-05-28 17:23:55 -07:00
|
|
|
argc_ptr = asm ("lea (%%esp), %[argc]"
|
2018-06-03 22:09:15 -07:00
|
|
|
: [argc] "=r" (-> [*]usize)
|
2018-05-28 17:23:55 -07:00
|
|
|
);
|
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-12-06 15:12:05 -08:00
|
|
|
// If LLVM inlines stack variables into _start, they will overwrite
|
|
|
|
// the command line argument data.
|
|
|
|
@noInlineCall(posixCallMainAndExit);
|
2017-06-13 21:04:34 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
extern fn WinMainCRTStartup() noreturn {
|
2017-10-02 19:00:42 -07:00
|
|
|
@setAlignStack(16);
|
2017-10-02 21:27:14 -07:00
|
|
|
|
2018-01-14 21:01:02 -08:00
|
|
|
std.os.windows.ExitProcess(callMain());
|
2016-01-15 23:07:20 -08:00
|
|
|
}
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2018-06-03 22:09:15 -07:00
|
|
|
// TODO https://github.com/ziglang/zig/issues/265
|
2018-01-25 01:10:11 -08:00
|
|
|
fn posixCallMainAndExit() noreturn {
|
2018-06-05 19:23:23 -07:00
|
|
|
const argc = argc_ptr[0];
|
2018-06-03 22:09:15 -07:00
|
|
|
const argv = @ptrCast([*][*]u8, argc_ptr + 1);
|
|
|
|
|
2018-06-09 20:42:14 -07:00
|
|
|
const envp_optional = @ptrCast([*]?[*]u8, argv + argc + 1);
|
2018-04-22 15:11:50 -07:00
|
|
|
var envp_count: usize = 0;
|
2018-06-09 20:42:14 -07:00
|
|
|
while (envp_optional[envp_count]) |_| : (envp_count += 1) {}
|
|
|
|
const envp = @ptrCast([*][*]u8, envp_optional)[0..envp_count];
|
2018-04-22 15:11:50 -07:00
|
|
|
if (builtin.os == builtin.Os.linux) {
|
2018-06-03 22:09:15 -07:00
|
|
|
const auxv = @ptrCast([*]usize, envp.ptr + envp_count + 1);
|
2018-04-22 15:11:50 -07:00
|
|
|
var i: usize = 0;
|
|
|
|
while (auxv[i] != 0) : (i += 2) {
|
2018-04-30 22:53:04 -07:00
|
|
|
if (auxv[i] < std.os.linux_aux_raw.len) std.os.linux_aux_raw[auxv[i]] = auxv[i + 1];
|
2018-04-22 15:11:50 -07:00
|
|
|
}
|
|
|
|
std.debug.assert(std.os.linux_aux_raw[std.elf.AT_PAGESZ] == std.os.page_size);
|
|
|
|
}
|
|
|
|
|
2018-01-14 21:01:02 -08:00
|
|
|
std.os.posix.exit(callMainWithArgs(argc, argv, envp));
|
2017-04-03 15:11:57 -07:00
|
|
|
}
|
|
|
|
|
2018-06-03 22:09:15 -07:00
|
|
|
fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 {
|
2017-10-11 07:16:13 -07:00
|
|
|
std.os.ArgIteratorPosix.raw = argv[0..argc];
|
2018-04-22 15:11:50 -07:00
|
|
|
std.os.posix_environ_raw = envp;
|
2018-01-14 21:01:02 -08:00
|
|
|
return callMain();
|
2016-02-27 21:06:46 -08:00
|
|
|
}
|
|
|
|
|
2018-06-03 22:09:15 -07:00
|
|
|
extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 {
|
2018-04-22 15:11:50 -07:00
|
|
|
var env_count: usize = 0;
|
|
|
|
while (c_envp[env_count] != null) : (env_count += 1) {}
|
2018-06-03 22:09:15 -07:00
|
|
|
const envp = @ptrCast([*][*]u8, c_envp)[0..env_count];
|
2018-06-16 23:57:07 -07:00
|
|
|
return callMainWithArgs(@intCast(usize, c_argc), c_argv, envp);
|
2018-01-14 21:01:02 -08:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn callMain() u8 {
|
2018-01-14 21:01:02 -08:00
|
|
|
switch (@typeId(@typeOf(root.main).ReturnType)) {
|
|
|
|
builtin.TypeId.NoReturn => {
|
|
|
|
root.main();
|
|
|
|
},
|
|
|
|
builtin.TypeId.Void => {
|
|
|
|
root.main();
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
builtin.TypeId.Int => {
|
|
|
|
if (@typeOf(root.main).ReturnType.bit_count != 8) {
|
2018-02-07 23:08:45 -08:00
|
|
|
@compileError("expected return type of main to be 'u8', 'noreturn', 'void', or '!void'");
|
2018-01-14 21:01:02 -08:00
|
|
|
}
|
|
|
|
return root.main();
|
|
|
|
},
|
|
|
|
builtin.TypeId.ErrorUnion => {
|
|
|
|
root.main() catch |err| {
|
|
|
|
std.debug.warn("error: {}\n", @errorName(err));
|
2018-03-20 13:09:30 -07:00
|
|
|
if (builtin.os != builtin.Os.zen) {
|
|
|
|
if (@errorReturnTrace()) |trace| {
|
|
|
|
std.debug.dumpStackTrace(trace);
|
|
|
|
}
|
2018-01-14 21:01:02 -08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
return 0;
|
|
|
|
},
|
2018-02-07 23:08:45 -08:00
|
|
|
else => @compileError("expected return type of main to be 'u8', 'noreturn', 'void', or '!void'"),
|
2018-01-14 21:01:02 -08:00
|
|
|
}
|
2016-02-15 22:30:05 -08:00
|
|
|
}
|