2019-06-28 07:15:33 +08:00
|
|
|
// This file is included in the compilation unit when exporting an executable.
|
2015-12-11 06:34:38 +08:00
|
|
|
|
2019-06-28 07:15:33 +08:00
|
|
|
const root = @import("root");
|
2017-02-06 16:10:32 +08:00
|
|
|
const std = @import("std");
|
2017-05-02 01:12:38 +08:00
|
|
|
const builtin = @import("builtin");
|
2019-02-07 02:48:04 +08:00
|
|
|
const assert = std.debug.assert;
|
2016-02-16 14:30:05 +08:00
|
|
|
|
2018-06-04 13:09:15 +08:00
|
|
|
var argc_ptr: [*]usize = undefined;
|
2016-01-14 09:15:51 +08:00
|
|
|
|
2019-05-20 04:26:43 +08:00
|
|
|
const is_wasm = switch (builtin.arch) {
|
|
|
|
.wasm32, .wasm64 => true,
|
|
|
|
else => false,
|
|
|
|
};
|
2019-05-17 02:17:00 +08:00
|
|
|
|
2017-12-18 22:59:57 +08:00
|
|
|
comptime {
|
|
|
|
if (builtin.link_libc) {
|
2019-05-17 02:17:00 +08: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);
|
2017-12-18 22:59:57 +08:00
|
|
|
} else {
|
2019-05-17 02:17:00 +08:00
|
|
|
@export("_start", _start, .Strong);
|
2017-03-23 14:59:58 +08:00
|
|
|
}
|
2017-12-18 22:59:57 +08:00
|
|
|
}
|
2016-09-28 14:33:32 +08:00
|
|
|
|
2019-07-03 23:16:52 +08:00
|
|
|
fn enableSegfaultHandler() void {
|
|
|
|
const enable_segfault_handler: bool = if (@hasDecl(root, "enable_segfault_handler"))
|
|
|
|
root.enable_segfault_handler
|
|
|
|
else
|
|
|
|
std.debug.runtime_safety and std.debug.have_segfault_handling_support;
|
|
|
|
if (enable_segfault_handler) {
|
|
|
|
std.debug.attachSegfaultHandler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 02:17:00 +08:00
|
|
|
extern fn wasm_freestanding_start() void {
|
|
|
|
_ = callMain();
|
|
|
|
}
|
|
|
|
|
2018-01-25 17:10:11 +08:00
|
|
|
nakedcc fn _start() noreturn {
|
2019-04-14 10:36:53 +08:00
|
|
|
if (builtin.os == builtin.Os.wasi) {
|
2019-04-16 06:14:21 +08:00
|
|
|
std.os.wasi.proc_exit(callMain());
|
2019-04-14 10:36:53 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 01:12:38 +08:00
|
|
|
switch (builtin.arch) {
|
2019-05-17 01:56:56 +08:00
|
|
|
.x86_64 => {
|
2018-12-18 10:19:20 +08:00
|
|
|
argc_ptr = asm ("lea (%%rsp), %[argc]"
|
2019-02-24 02:19:06 +08:00
|
|
|
: [argc] "=r" (-> [*]usize)
|
|
|
|
);
|
2016-02-12 17:04:46 +08:00
|
|
|
},
|
2019-05-17 01:56:56 +08:00
|
|
|
.i386 => {
|
2018-05-29 08:23:55 +08:00
|
|
|
argc_ptr = asm ("lea (%%esp), %[argc]"
|
2018-06-04 13:09:15 +08:00
|
|
|
: [argc] "=r" (-> [*]usize)
|
2018-05-29 08:23:55 +08:00
|
|
|
);
|
2016-02-12 17:04:46 +08:00
|
|
|
},
|
2019-05-17 01:56:56 +08:00
|
|
|
.aarch64, .aarch64_be => {
|
2018-08-30 08:37:58 +08:00
|
|
|
argc_ptr = asm ("mov %[argc], sp"
|
|
|
|
: [argc] "=r" (-> [*]usize)
|
|
|
|
);
|
|
|
|
},
|
2016-09-06 05:01:54 +08:00
|
|
|
else => @compileError("unsupported arch"),
|
2016-02-12 17:04:46 +08:00
|
|
|
}
|
2017-12-07 07:12:05 +08:00
|
|
|
// If LLVM inlines stack variables into _start, they will overwrite
|
|
|
|
// the command line argument data.
|
|
|
|
@noInlineCall(posixCallMainAndExit);
|
2017-06-14 12:04:34 +08:00
|
|
|
}
|
|
|
|
|
2018-01-25 17:10:11 +08:00
|
|
|
extern fn WinMainCRTStartup() noreturn {
|
2017-10-03 10:00:42 +08:00
|
|
|
@setAlignStack(16);
|
2019-02-07 07:32:41 +08:00
|
|
|
if (!builtin.single_threaded) {
|
2019-06-28 14:00:56 +08:00
|
|
|
_ = @import("start_windows_tls.zig");
|
2019-02-07 07:32:41 +08:00
|
|
|
}
|
2019-07-03 23:16:52 +08:00
|
|
|
|
|
|
|
enableSegfaultHandler();
|
|
|
|
|
2019-05-27 12:48:56 +08:00
|
|
|
std.os.windows.kernel32.ExitProcess(callMain());
|
2016-01-16 15:07:20 +08:00
|
|
|
}
|
2016-01-14 09:15:51 +08:00
|
|
|
|
2018-06-04 13:09:15 +08:00
|
|
|
// TODO https://github.com/ziglang/zig/issues/265
|
2018-01-25 17:10:11 +08:00
|
|
|
fn posixCallMainAndExit() noreturn {
|
2018-10-20 20:15:15 +08:00
|
|
|
if (builtin.os == builtin.Os.freebsd) {
|
|
|
|
@setAlignStack(16);
|
|
|
|
}
|
2018-06-06 10:23:23 +08:00
|
|
|
const argc = argc_ptr[0];
|
2018-06-04 13:09:15 +08:00
|
|
|
const argv = @ptrCast([*][*]u8, argc_ptr + 1);
|
|
|
|
|
2018-06-10 11:42:14 +08:00
|
|
|
const envp_optional = @ptrCast([*]?[*]u8, argv + argc + 1);
|
2018-04-23 06:11:50 +08:00
|
|
|
var envp_count: usize = 0;
|
2018-06-10 11:42:14 +08:00
|
|
|
while (envp_optional[envp_count]) |_| : (envp_count += 1) {}
|
|
|
|
const envp = @ptrCast([*][*]u8, envp_optional)[0..envp_count];
|
2019-05-04 18:02:55 +08:00
|
|
|
|
2019-06-20 11:39:49 +08: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
|
|
|
|
std.os.linux.tls.initTLS();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 06:11:50 +08:00
|
|
|
|
2019-05-26 01:07:44 +08:00
|
|
|
std.os.exit(callMainWithArgs(argc, argv, envp));
|
2017-04-04 06:11:57 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 00:33:48 +08: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.
|
|
|
|
inline fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 {
|
2019-05-27 01:17:34 +08:00
|
|
|
std.os.argv = argv[0..argc];
|
2019-05-26 01:07:44 +08:00
|
|
|
std.os.environ = envp;
|
2019-07-03 01:27:40 +08:00
|
|
|
|
2019-07-03 23:16:52 +08:00
|
|
|
enableSegfaultHandler();
|
2019-07-03 01:27:40 +08:00
|
|
|
|
2018-01-15 13:01:02 +08:00
|
|
|
return callMain();
|
2016-02-28 13:06:46 +08:00
|
|
|
}
|
|
|
|
|
2018-06-04 13:09:15 +08:00
|
|
|
extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 {
|
2018-04-23 06:11:50 +08:00
|
|
|
var env_count: usize = 0;
|
|
|
|
while (c_envp[env_count] != null) : (env_count += 1) {}
|
2018-06-04 13:09:15 +08:00
|
|
|
const envp = @ptrCast([*][*]u8, c_envp)[0..env_count];
|
2018-06-17 14:57:07 +08:00
|
|
|
return callMainWithArgs(@intCast(usize, c_argc), c_argv, envp);
|
2018-01-15 13:01:02 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 00:33:48 +08: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.
|
|
|
|
inline fn callMain() u8 {
|
2018-01-15 13:01:02 +08:00
|
|
|
switch (@typeId(@typeOf(root.main).ReturnType)) {
|
2019-06-12 06:26:01 +08:00
|
|
|
.NoReturn => {
|
2018-01-15 13:01:02 +08:00
|
|
|
root.main();
|
|
|
|
},
|
2019-06-12 06:26:01 +08:00
|
|
|
.Void => {
|
2018-01-15 13:01:02 +08:00
|
|
|
root.main();
|
|
|
|
return 0;
|
|
|
|
},
|
2019-06-12 06:26:01 +08:00
|
|
|
.Int => {
|
2018-01-15 13:01:02 +08:00
|
|
|
if (@typeOf(root.main).ReturnType.bit_count != 8) {
|
2018-02-08 15:08:45 +08:00
|
|
|
@compileError("expected return type of main to be 'u8', 'noreturn', 'void', or '!void'");
|
2018-01-15 13:01:02 +08:00
|
|
|
}
|
|
|
|
return root.main();
|
|
|
|
},
|
2019-06-12 06:26:01 +08:00
|
|
|
.ErrorUnion => {
|
2018-01-15 13:01:02 +08:00
|
|
|
root.main() catch |err| {
|
|
|
|
std.debug.warn("error: {}\n", @errorName(err));
|
2018-03-21 04:09:30 +08:00
|
|
|
if (builtin.os != builtin.Os.zen) {
|
|
|
|
if (@errorReturnTrace()) |trace| {
|
2019-02-24 02:19:06 +08:00
|
|
|
std.debug.dumpStackTrace(trace.*);
|
2018-03-21 04:09:30 +08:00
|
|
|
}
|
2018-01-15 13:01:02 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
return 0;
|
|
|
|
},
|
2018-02-08 15:08:45 +08:00
|
|
|
else => @compileError("expected return type of main to be 'u8', 'noreturn', 'void', or '!void'"),
|
2018-01-15 13:01:02 +08:00
|
|
|
}
|
2016-02-16 14:30:05 +08:00
|
|
|
}
|
2019-02-07 02:48:04 +08:00
|
|
|
|
|
|
|
const main_thread_tls_align = 32;
|
|
|
|
var main_thread_tls_bytes: [64]u8 align(main_thread_tls_align) = [1]u8{0} ** 64;
|