2019-06-27 16:15:33 -07:00
|
|
|
// This file is included in the compilation unit when exporting an executable.
|
2015-12-10 14:34:38 -08:00
|
|
|
|
2019-06-27 16:15:33 -07: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");
|
2019-02-06 10:48:04 -08:00
|
|
|
const assert = std.debug.assert;
|
2019-07-28 14:51:51 -07:00
|
|
|
const uefi = std.os.uefi;
|
2016-02-15 22:30:05 -08:00
|
|
|
|
2019-09-11 17:22:49 -07:00
|
|
|
var starting_stack_ptr: [*]usize = undefined;
|
2016-01-13 17:15:51 -08:00
|
|
|
|
2019-05-19 13:26:43 -07:00
|
|
|
const is_wasm = switch (builtin.arch) {
|
|
|
|
.wasm32, .wasm64 => true,
|
|
|
|
else => false,
|
|
|
|
};
|
2019-05-16 11:17:00 -07:00
|
|
|
|
2019-09-26 08:13:57 -07:00
|
|
|
const is_mips = switch (builtin.arch) {
|
|
|
|
.mips, .mipsel, .mips64, .mips64el => true,
|
|
|
|
else => false,
|
|
|
|
};
|
|
|
|
|
2017-12-18 06:59:57 -08:00
|
|
|
comptime {
|
|
|
|
if (builtin.link_libc) {
|
2019-05-16 11:17:00 -07:00
|
|
|
@export("main", main, .Strong);
|
|
|
|
} else if (builtin.os == .windows) {
|
|
|
|
@export("WinMainCRTStartup", WinMainCRTStartup, .Strong);
|
|
|
|
} else if (is_wasm and builtin.os == .freestanding) {
|
|
|
|
@export("_start", wasm_freestanding_start, .Strong);
|
2019-07-28 14:51:51 -07:00
|
|
|
} else if (builtin.os == .uefi) {
|
|
|
|
@export("EfiMain", EfiMain, .Strong);
|
2019-09-26 08:13:57 -07:00
|
|
|
} else if (is_mips) {
|
|
|
|
if (!@hasDecl(root, "__start")) @export("__start", _start, .Strong);
|
2017-12-18 06:59:57 -08:00
|
|
|
} else {
|
2019-07-28 16:18:35 -07:00
|
|
|
if (!@hasDecl(root, "_start")) @export("_start", _start, .Strong);
|
2017-03-22 23:59:58 -07:00
|
|
|
}
|
2017-12-18 06:59:57 -08:00
|
|
|
}
|
2016-09-27 23:33:32 -07:00
|
|
|
|
2019-05-16 11:17:00 -07:00
|
|
|
extern fn wasm_freestanding_start() void {
|
2019-10-31 08:41:39 -07:00
|
|
|
// This is marked inline because for some reason LLVM in release mode fails to inline it,
|
|
|
|
// and we want fewer call frames in stack traces.
|
|
|
|
_ = @inlineCall(callMain);
|
2019-05-16 11:17:00 -07:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:12:59 -07:00
|
|
|
extern fn EfiMain(handle: uefi.Handle, system_table: *uefi.tables.SystemTable) usize {
|
2019-07-28 14:51:51 -07:00
|
|
|
const bad_efi_main_ret = "expected return type of main to be 'void', 'noreturn', or 'usize'";
|
|
|
|
uefi.handle = handle;
|
|
|
|
uefi.system_table = system_table;
|
|
|
|
|
|
|
|
switch (@typeInfo(@typeOf(root.main).ReturnType)) {
|
|
|
|
.NoReturn => {
|
|
|
|
root.main();
|
|
|
|
},
|
|
|
|
.Void => {
|
|
|
|
root.main();
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
.Int => |info| {
|
|
|
|
if (info.bits != @typeInfo(usize).Int.bits) {
|
|
|
|
@compileError(bad_efi_main_ret);
|
|
|
|
}
|
|
|
|
return root.main();
|
|
|
|
},
|
|
|
|
else => @compileError(bad_efi_main_ret),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
nakedcc fn _start() noreturn {
|
2019-04-13 19:36:53 -07:00
|
|
|
if (builtin.os == builtin.Os.wasi) {
|
2019-10-31 08:41:39 -07:00
|
|
|
// This is marked inline because for some reason LLVM in release mode fails to inline it,
|
|
|
|
// and we want fewer call frames in stack traces.
|
|
|
|
std.os.wasi.proc_exit(@inlineCall(callMain));
|
2019-04-13 19:36:53 -07:00
|
|
|
}
|
|
|
|
|
2017-05-01 10:12:38 -07:00
|
|
|
switch (builtin.arch) {
|
2019-05-16 10:56:56 -07:00
|
|
|
.x86_64 => {
|
2019-09-11 17:22:49 -07:00
|
|
|
starting_stack_ptr = asm (""
|
2019-07-26 00:59:18 -07:00
|
|
|
: [argc] "={rsp}" (-> [*]usize)
|
2019-02-23 10:19:06 -08:00
|
|
|
);
|
2016-02-12 01:04:46 -08:00
|
|
|
},
|
2019-05-16 10:56:56 -07:00
|
|
|
.i386 => {
|
2019-09-11 17:22:49 -07:00
|
|
|
starting_stack_ptr = asm (""
|
2019-07-26 00:59:18 -07:00
|
|
|
: [argc] "={esp}" (-> [*]usize)
|
2018-05-28 17:23:55 -07:00
|
|
|
);
|
2016-02-12 01:04:46 -08:00
|
|
|
},
|
2019-08-28 16:14:43 -07:00
|
|
|
.aarch64, .aarch64_be, .arm => {
|
2019-09-11 17:22:49 -07:00
|
|
|
starting_stack_ptr = asm ("mov %[argc], sp"
|
2018-08-29 17:37:58 -07:00
|
|
|
: [argc] "=r" (-> [*]usize)
|
|
|
|
);
|
|
|
|
},
|
2019-07-18 12:03:21 -07:00
|
|
|
.riscv64 => {
|
2019-09-20 12:24:50 -07:00
|
|
|
starting_stack_ptr = asm ("mv %[argc], sp"
|
2019-07-18 12:03:21 -07:00
|
|
|
: [argc] "=r" (-> [*]usize)
|
|
|
|
);
|
|
|
|
},
|
2019-09-21 14:00:36 -07:00
|
|
|
.mipsel => {
|
|
|
|
// Need noat here because LLVM is free to pick any register
|
|
|
|
starting_stack_ptr = asm (
|
|
|
|
\\ .set noat
|
|
|
|
\\ move %[argc], $sp
|
|
|
|
: [argc] "=r" (-> [*]usize)
|
|
|
|
);
|
|
|
|
},
|
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);
|
2019-02-06 15:32:41 -08:00
|
|
|
if (!builtin.single_threaded) {
|
2019-06-27 23:00:56 -07:00
|
|
|
_ = @import("start_windows_tls.zig");
|
2019-02-06 15:32:41 -08:00
|
|
|
}
|
2019-07-03 08:16:52 -07:00
|
|
|
|
2019-07-22 09:15:16 -07:00
|
|
|
std.debug.maybeEnableSegfaultHandler();
|
2019-07-05 11:14:25 -07:00
|
|
|
|
2019-10-31 08:41:39 -07:00
|
|
|
std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain());
|
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-10-20 05:15:15 -07:00
|
|
|
if (builtin.os == builtin.Os.freebsd) {
|
|
|
|
@setAlignStack(16);
|
|
|
|
}
|
2019-09-11 17:22:49 -07:00
|
|
|
const argc = starting_stack_ptr[0];
|
|
|
|
const argv = @ptrCast([*][*]u8, starting_stack_ptr + 1);
|
2018-06-03 22:09:15 -07:00
|
|
|
|
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];
|
2019-05-04 03:02:55 -07:00
|
|
|
|
2019-06-19 20:39:49 -07:00
|
|
|
if (builtin.os == .linux) {
|
|
|
|
// Find the beginning of the auxiliary vector
|
|
|
|
const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1);
|
|
|
|
std.os.linux.elf_aux_maybe = auxv;
|
|
|
|
// Initialize the TLS area
|
2019-09-11 17:22:49 -07:00
|
|
|
const gnu_stack_phdr = std.os.linux.tls.initTLS() orelse @panic("ELF missing stack size");
|
2019-06-19 20:39:49 -07:00
|
|
|
|
|
|
|
if (std.os.linux.tls.tls_image) |tls_img| {
|
|
|
|
const tls_addr = std.os.linux.tls.allocateTLS(tls_img.alloc_size);
|
|
|
|
const tp = std.os.linux.tls.copyTLS(tls_addr);
|
|
|
|
std.os.linux.tls.setThreadPointer(tp);
|
|
|
|
}
|
2019-09-11 17:22:49 -07:00
|
|
|
|
|
|
|
// TODO This is disabled because what should we do when linking libc and this code
|
|
|
|
// does not execute? And also it's causing a test failure in stack traces in release modes.
|
|
|
|
|
|
|
|
//// Linux ignores the stack size from the ELF file, and instead always does 8 MiB. A further
|
|
|
|
//// problem is that it uses PROT_GROWSDOWN which prevents stores to addresses too far down
|
|
|
|
//// the stack and requires "probing". So here we allocate our own stack.
|
|
|
|
//const wanted_stack_size = gnu_stack_phdr.p_memsz;
|
|
|
|
//assert(wanted_stack_size % std.mem.page_size == 0);
|
|
|
|
//// Allocate an extra page as the guard page.
|
|
|
|
//const total_size = wanted_stack_size + std.mem.page_size;
|
|
|
|
//const new_stack = std.os.mmap(
|
|
|
|
// null,
|
|
|
|
// total_size,
|
|
|
|
// std.os.PROT_READ | std.os.PROT_WRITE,
|
|
|
|
// std.os.MAP_PRIVATE | std.os.MAP_ANONYMOUS,
|
|
|
|
// -1,
|
|
|
|
// 0,
|
|
|
|
//) catch @panic("out of memory");
|
|
|
|
//std.os.mprotect(new_stack[0..std.mem.page_size], std.os.PROT_NONE) catch {};
|
|
|
|
//std.os.exit(@newStackCall(new_stack, callMainWithArgs, argc, argv, envp));
|
2019-06-19 20:39:49 -07:00
|
|
|
}
|
2018-04-22 15:11:50 -07:00
|
|
|
|
2019-09-11 17:22:49 -07:00
|
|
|
std.os.exit(@inlineCall(callMainWithArgs, argc, argv, envp));
|
2017-04-03 15:11:57 -07:00
|
|
|
}
|
|
|
|
|
2019-09-11 17:22:49 -07:00
|
|
|
fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 {
|
2019-05-26 10:17:34 -07:00
|
|
|
std.os.argv = argv[0..argc];
|
2019-05-25 10:07:44 -07:00
|
|
|
std.os.environ = envp;
|
2019-07-02 10:27:40 -07:00
|
|
|
|
2019-07-22 09:15:16 -07:00
|
|
|
std.debug.maybeEnableSegfaultHandler();
|
2019-07-02 10:27:40 -07:00
|
|
|
|
2019-10-31 08:41:39 -07:00
|
|
|
return initEventLoopAndCallMain();
|
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];
|
2019-09-11 17:22:49 -07:00
|
|
|
return @inlineCall(callMainWithArgs, @intCast(usize, c_argc), c_argv, envp);
|
2018-01-14 21:01:02 -08:00
|
|
|
}
|
|
|
|
|
2019-07-16 08:50:00 -07:00
|
|
|
// General error message for a malformed return type
|
|
|
|
const bad_main_ret = "expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'";
|
|
|
|
|
2018-10-09 09:33:48 -07:00
|
|
|
// This is marked inline because for some reason LLVM in release mode fails to inline it,
|
|
|
|
// and we want fewer call frames in stack traces.
|
2019-10-31 08:41:39 -07:00
|
|
|
inline fn initEventLoopAndCallMain() u8 {
|
|
|
|
if (std.event.Loop.instance) |loop| {
|
2019-10-31 11:21:04 -07:00
|
|
|
if (!@hasDecl(root, "event_loop")) {
|
|
|
|
loop.init() catch |err| {
|
|
|
|
std.debug.warn("error: {}\n", @errorName(err));
|
|
|
|
if (@errorReturnTrace()) |trace| {
|
|
|
|
std.debug.dumpStackTrace(trace.*);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
defer loop.deinit();
|
|
|
|
|
|
|
|
var result: u8 = undefined;
|
2019-11-06 12:08:29 -08:00
|
|
|
var frame: @Frame(callMainAsync) = undefined;
|
|
|
|
_ = @asyncCall(&frame, &result, callMainAsync, loop);
|
2019-10-31 11:21:04 -07:00
|
|
|
loop.run();
|
|
|
|
return result;
|
|
|
|
}
|
2019-10-31 08:41:39 -07:00
|
|
|
}
|
2019-10-31 11:21:04 -07:00
|
|
|
|
|
|
|
// This is marked inline because for some reason LLVM in release mode fails to inline it,
|
|
|
|
// and we want fewer call frames in stack traces.
|
|
|
|
return @inlineCall(callMain);
|
2019-10-31 08:41:39 -07:00
|
|
|
}
|
|
|
|
|
2019-11-07 07:02:21 -08:00
|
|
|
async fn callMainAsync(loop: *std.event.Loop) u8 {
|
2019-11-06 12:08:29 -08:00
|
|
|
// This prevents the event loop from terminating at least until main() has returned.
|
|
|
|
loop.beginOneEvent();
|
|
|
|
defer loop.finishOneEvent();
|
|
|
|
return callMain();
|
|
|
|
}
|
|
|
|
|
2019-10-31 08:41:39 -07:00
|
|
|
// This is not marked inline because it is called with @asyncCall when
|
|
|
|
// there is an event loop.
|
|
|
|
fn callMain() u8 {
|
2019-07-16 08:50:00 -07:00
|
|
|
switch (@typeInfo(@typeOf(root.main).ReturnType)) {
|
2019-06-11 15:26:01 -07:00
|
|
|
.NoReturn => {
|
2018-01-14 21:01:02 -08:00
|
|
|
root.main();
|
|
|
|
},
|
2019-06-11 15:26:01 -07:00
|
|
|
.Void => {
|
2018-01-14 21:01:02 -08:00
|
|
|
root.main();
|
|
|
|
return 0;
|
|
|
|
},
|
2019-07-16 08:50:00 -07:00
|
|
|
.Int => |info| {
|
|
|
|
if (info.bits != 8) {
|
|
|
|
@compileError(bad_main_ret);
|
2018-01-14 21:01:02 -08:00
|
|
|
}
|
|
|
|
return root.main();
|
|
|
|
},
|
2019-07-16 08:50:00 -07:00
|
|
|
.ErrorUnion => {
|
|
|
|
const result = root.main() catch |err| {
|
|
|
|
std.debug.warn("error: {}\n", @errorName(err));
|
|
|
|
if (@errorReturnTrace()) |trace| {
|
|
|
|
std.debug.dumpStackTrace(trace.*);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
switch (@typeInfo(@typeOf(result))) {
|
|
|
|
.Void => return 0,
|
|
|
|
.Int => |info| {
|
|
|
|
if (info.bits != 8) {
|
|
|
|
@compileError(bad_main_ret);
|
2018-03-20 13:09:30 -07:00
|
|
|
}
|
2019-07-16 08:50:00 -07:00
|
|
|
return result;
|
|
|
|
},
|
|
|
|
else => @compileError(bad_main_ret),
|
|
|
|
}
|
2018-01-14 21:01:02 -08:00
|
|
|
},
|
2019-07-16 08:50:00 -07:00
|
|
|
else => @compileError(bad_main_ret),
|
2018-01-14 21:01:02 -08:00
|
|
|
}
|
2016-02-15 22:30:05 -08:00
|
|
|
}
|