2019-06-27 19:15:33 -04:00
|
|
|
// This file is included in the compilation unit when exporting an executable.
|
2015-12-10 15:34:38 -07:00
|
|
|
|
2019-06-27 19:15:33 -04:00
|
|
|
const root = @import("root");
|
2019-12-12 18:27:17 -05:00
|
|
|
const std = @import("std.zig");
|
2021-04-08 19:05:05 -07:00
|
|
|
const builtin = @import("builtin");
|
2019-02-06 13:48:04 -05:00
|
|
|
const assert = std.debug.assert;
|
2019-07-28 23:51:51 +02:00
|
|
|
const uefi = std.os.uefi;
|
2021-06-19 18:02:51 +02:00
|
|
|
const elf = std.elf;
|
2020-12-17 20:03:41 -07:00
|
|
|
const tlcsprng = @import("crypto/tlcsprng.zig");
|
2021-04-13 11:51:58 -07:00
|
|
|
const native_arch = builtin.cpu.arch;
|
|
|
|
|
const native_os = builtin.os.tag;
|
2016-02-15 23:30:05 -07:00
|
|
|
|
2020-10-27 22:46:18 +07:00
|
|
|
var argc_argv_ptr: [*]usize = undefined;
|
2016-01-13 18:15:51 -07:00
|
|
|
|
2021-04-12 16:43:50 -07:00
|
|
|
const start_sym_name = if (native_arch.isMIPS()) "__start" else "_start";
|
2019-09-26 17:13:57 +02:00
|
|
|
|
2017-12-18 09:59:57 -05:00
|
|
|
comptime {
|
2021-04-30 11:24:15 -07:00
|
|
|
// No matter what, we import the root file, so that any export, test, comptime
|
|
|
|
|
// decls there get run.
|
|
|
|
|
_ = root;
|
|
|
|
|
|
2021-04-08 19:05:05 -07:00
|
|
|
// The self-hosted compiler is not fully capable of handling all of this start.zig file.
|
|
|
|
|
// Until then, we have simplified logic here for self-hosted. TODO remove this once
|
|
|
|
|
// self-hosted is capable enough to handle all of the real start.zig logic.
|
2022-01-17 21:55:49 -07:00
|
|
|
if (builtin.zig_backend != .stage1) {
|
2021-04-08 19:05:05 -07:00
|
|
|
if (builtin.output_mode == .Exe) {
|
2021-05-06 22:30:44 -07:00
|
|
|
if ((builtin.link_libc or builtin.object_format == .c) and @hasDecl(root, "main")) {
|
|
|
|
|
if (@typeInfo(@TypeOf(root.main)).Fn.calling_convention != .C) {
|
|
|
|
|
@export(main2, .{ .name = "main" });
|
|
|
|
|
}
|
2021-09-22 14:14:03 -05:00
|
|
|
} else if (builtin.os.tag == .windows) {
|
2021-09-01 14:32:02 -05:00
|
|
|
@export(wWinMainCRTStartup2, .{ .name = "wWinMainCRTStartup" });
|
2021-11-28 20:31:12 +01:00
|
|
|
} else if (builtin.os.tag == .wasi and @hasDecl(root, "main")) {
|
2021-11-22 21:16:55 +01:00
|
|
|
@export(wasmMain2, .{ .name = "_start" });
|
2021-04-08 19:05:05 -07:00
|
|
|
} else {
|
|
|
|
|
if (!@hasDecl(root, "_start")) {
|
2021-05-02 18:50:01 -07:00
|
|
|
@export(_start2, .{ .name = "_start" });
|
2021-04-08 19:05:05 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-12-03 12:29:55 -05:00
|
|
|
}
|
2021-04-08 19:05:05 -07:00
|
|
|
} else {
|
|
|
|
|
if (builtin.output_mode == .Lib and builtin.link_mode == .Dynamic) {
|
2021-04-12 16:43:50 -07:00
|
|
|
if (native_os == .windows and !@hasDecl(root, "_DllMainCRTStartup")) {
|
2021-04-08 19:05:05 -07:00
|
|
|
@export(_DllMainCRTStartup, .{ .name = "_DllMainCRTStartup" });
|
2019-12-03 13:10:26 -05:00
|
|
|
}
|
2021-04-08 19:05:05 -07:00
|
|
|
} else if (builtin.output_mode == .Exe or @hasDecl(root, "main")) {
|
|
|
|
|
if (builtin.link_libc and @hasDecl(root, "main")) {
|
2021-07-27 08:59:34 +09:00
|
|
|
if (native_arch.isWasm()) {
|
|
|
|
|
@export(mainWithoutEnv, .{ .name = "main" });
|
|
|
|
|
} else if (@typeInfo(@TypeOf(root.main)).Fn.calling_convention != .C) {
|
2021-05-13 18:34:52 +02:00
|
|
|
@export(main, .{ .name = "main" });
|
2021-04-08 19:05:05 -07:00
|
|
|
}
|
2021-04-12 16:43:50 -07:00
|
|
|
} else if (native_os == .windows) {
|
2021-04-08 19:05:05 -07:00
|
|
|
if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
|
|
|
|
|
!@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
|
|
|
|
|
{
|
|
|
|
|
@export(WinStartup, .{ .name = "wWinMainCRTStartup" });
|
|
|
|
|
} else if (@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
|
|
|
|
|
!@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
|
|
|
|
|
{
|
|
|
|
|
@compileError("WinMain not supported; declare wWinMain or main instead");
|
|
|
|
|
} else if (@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup") and
|
|
|
|
|
!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup"))
|
|
|
|
|
{
|
|
|
|
|
@export(wWinMainCRTStartup, .{ .name = "wWinMainCRTStartup" });
|
|
|
|
|
}
|
2021-04-12 16:43:50 -07:00
|
|
|
} else if (native_os == .uefi) {
|
2021-04-08 19:05:05 -07:00
|
|
|
if (!@hasDecl(root, "EfiMain")) @export(EfiMain, .{ .name = "EfiMain" });
|
2021-06-30 18:39:04 -07:00
|
|
|
} else if (native_os == .wasi) {
|
|
|
|
|
const wasm_start_sym = switch (builtin.wasi_exec_model) {
|
|
|
|
|
.reactor => "_initialize",
|
|
|
|
|
.command => "_start",
|
|
|
|
|
};
|
|
|
|
|
if (!@hasDecl(root, wasm_start_sym)) {
|
|
|
|
|
@export(wasi_start, .{ .name = wasm_start_sym });
|
|
|
|
|
}
|
|
|
|
|
} else if (native_arch.isWasm() and native_os == .freestanding) {
|
|
|
|
|
if (!@hasDecl(root, start_sym_name)) @export(wasm_freestanding_start, .{ .name = start_sym_name });
|
2021-04-12 16:43:50 -07:00
|
|
|
} else if (native_os != .other and native_os != .freestanding) {
|
2021-04-08 19:05:05 -07:00
|
|
|
if (!@hasDecl(root, start_sym_name)) @export(_start, .{ .name = start_sym_name });
|
2019-11-30 15:39:11 +02:00
|
|
|
}
|
2019-12-03 12:29:55 -05:00
|
|
|
}
|
2017-03-23 02:59:58 -04:00
|
|
|
}
|
2017-12-18 09:59:57 -05:00
|
|
|
}
|
2016-09-28 02:33:32 -04:00
|
|
|
|
2021-04-08 19:05:05 -07:00
|
|
|
// Simplified start code for stage2 until it supports more language features ///
|
|
|
|
|
|
|
|
|
|
fn main2() callconv(.C) c_int {
|
|
|
|
|
root.main();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn _start2() callconv(.Naked) noreturn {
|
2021-09-16 21:03:55 -07:00
|
|
|
callMain2();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn callMain2() noreturn {
|
|
|
|
|
@setAlignStack(16);
|
2021-04-08 19:05:05 -07:00
|
|
|
root.main();
|
|
|
|
|
exit2(0);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 21:16:55 +01:00
|
|
|
fn wasmMain2() u8 {
|
2021-11-28 20:25:33 +01:00
|
|
|
switch (@typeInfo(@typeInfo(@TypeOf(root.main)).Fn.return_type.?)) {
|
|
|
|
|
.Void => {
|
|
|
|
|
root.main();
|
|
|
|
|
return 0;
|
|
|
|
|
},
|
|
|
|
|
.Int => |info| {
|
|
|
|
|
if (info.bits != 8 or info.signedness == .signed) {
|
|
|
|
|
@compileError(bad_main_ret);
|
|
|
|
|
}
|
|
|
|
|
return root.main();
|
|
|
|
|
},
|
|
|
|
|
else => @compileError("Bad return type main"),
|
|
|
|
|
}
|
2021-11-22 21:16:55 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-01 14:32:02 -05:00
|
|
|
fn wWinMainCRTStartup2() callconv(.C) noreturn {
|
|
|
|
|
root.main();
|
|
|
|
|
exit2(0);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 20:05:29 -07:00
|
|
|
fn exit2(code: usize) noreturn {
|
2021-09-13 20:08:44 -07:00
|
|
|
switch (native_os) {
|
2021-06-12 16:51:51 -04:00
|
|
|
.linux => switch (builtin.stage2_arch) {
|
|
|
|
|
.x86_64 => {
|
|
|
|
|
asm volatile ("syscall"
|
|
|
|
|
:
|
|
|
|
|
: [number] "{rax}" (231),
|
2021-08-29 11:57:32 +02:00
|
|
|
[arg1] "{rdi}" (code),
|
2021-06-12 16:51:51 -04:00
|
|
|
: "rcx", "r11", "memory"
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
.arm => {
|
|
|
|
|
asm volatile ("svc #0"
|
|
|
|
|
:
|
|
|
|
|
: [number] "{r7}" (1),
|
2021-08-29 11:57:32 +02:00
|
|
|
[arg1] "{r0}" (code),
|
2021-06-12 16:51:51 -04:00
|
|
|
: "memory"
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
.aarch64 => {
|
|
|
|
|
asm volatile ("svc #0"
|
|
|
|
|
:
|
|
|
|
|
: [number] "{x8}" (93),
|
2021-08-29 11:57:32 +02:00
|
|
|
[arg1] "{x0}" (code),
|
2021-06-12 16:51:51 -04:00
|
|
|
: "memory", "cc"
|
|
|
|
|
);
|
|
|
|
|
},
|
2021-11-20 15:30:53 +01:00
|
|
|
.riscv64 => {
|
|
|
|
|
asm volatile ("ecall"
|
|
|
|
|
:
|
|
|
|
|
: [number] "{a7}" (94),
|
|
|
|
|
[arg1] "{a0}" (0),
|
|
|
|
|
: "rcx", "r11", "memory"
|
|
|
|
|
);
|
|
|
|
|
},
|
2021-06-12 16:51:51 -04:00
|
|
|
else => @compileError("TODO"),
|
2021-04-08 19:05:05 -07:00
|
|
|
},
|
2021-06-21 13:10:31 -04:00
|
|
|
// exits(0)
|
2021-06-12 16:51:51 -04:00
|
|
|
.plan9 => switch (builtin.stage2_arch) {
|
|
|
|
|
.x86_64 => {
|
2021-06-21 13:10:31 -04:00
|
|
|
asm volatile (
|
|
|
|
|
\\push $0
|
|
|
|
|
\\push $0
|
|
|
|
|
\\syscall
|
2021-06-12 16:51:51 -04:00
|
|
|
:
|
2021-08-29 11:57:32 +02:00
|
|
|
: [syscall_number] "{rbp}" (8),
|
2021-06-12 16:51:51 -04:00
|
|
|
: "rcx", "r11", "memory"
|
|
|
|
|
);
|
|
|
|
|
},
|
2021-07-10 20:56:30 -04:00
|
|
|
// TODO once we get stack setting with assembly on
|
|
|
|
|
// arm, exit with 0 instead of stack garbage
|
|
|
|
|
.aarch64 => {
|
|
|
|
|
asm volatile ("svc #0"
|
|
|
|
|
:
|
2021-08-29 11:57:32 +02:00
|
|
|
: [exit] "{x0}" (0x08),
|
2021-07-10 20:56:30 -04:00
|
|
|
: "memory", "cc"
|
|
|
|
|
);
|
|
|
|
|
},
|
2021-06-12 16:51:51 -04:00
|
|
|
else => @compileError("TODO"),
|
2021-04-08 19:05:05 -07:00
|
|
|
},
|
2021-09-01 14:32:02 -05:00
|
|
|
.windows => {
|
|
|
|
|
ExitProcess(@truncate(u32, code));
|
|
|
|
|
},
|
2021-04-08 19:05:05 -07:00
|
|
|
else => @compileError("TODO"),
|
|
|
|
|
}
|
|
|
|
|
unreachable;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-01 14:32:02 -05:00
|
|
|
extern "kernel32" fn ExitProcess(exit_code: u32) callconv(.C) noreturn;
|
|
|
|
|
|
2021-04-08 19:05:05 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2020-02-22 15:59:13 -05:00
|
|
|
fn _DllMainCRTStartup(
|
|
|
|
|
hinstDLL: std.os.windows.HINSTANCE,
|
|
|
|
|
fdwReason: std.os.windows.DWORD,
|
|
|
|
|
lpReserved: std.os.windows.LPVOID,
|
2020-10-07 17:51:00 -06:00
|
|
|
) callconv(std.os.windows.WINAPI) std.os.windows.BOOL {
|
2020-11-09 20:57:07 +02:00
|
|
|
if (!builtin.single_threaded and !builtin.link_libc) {
|
2020-03-20 18:33:36 -04:00
|
|
|
_ = @import("start_windows_tls.zig");
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-30 15:39:11 +02:00
|
|
|
if (@hasDecl(root, "DllMain")) {
|
|
|
|
|
return root.DllMain(hinstDLL, fdwReason, lpReserved);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std.os.windows.TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 18:39:04 -07:00
|
|
|
fn wasm_freestanding_start() callconv(.C) void {
|
|
|
|
|
// 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.
|
|
|
|
|
_ = @call(.{ .modifier = .always_inline }, callMain, .{});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn wasi_start() callconv(.C) void {
|
|
|
|
|
// The function call is marked inline because for some reason LLVM in
|
|
|
|
|
// release mode fails to inline it, and we want fewer call frames in stack traces.
|
|
|
|
|
switch (builtin.wasi_exec_model) {
|
|
|
|
|
.reactor => _ = @call(.{ .modifier = .always_inline }, callMain, .{}),
|
|
|
|
|
.command => std.os.wasi.proc_exit(@call(.{ .modifier = .always_inline }, callMain, .{})),
|
2021-07-01 09:02:48 +09:00
|
|
|
}
|
2019-05-16 14:17:00 -04:00
|
|
|
}
|
|
|
|
|
|
2020-01-06 15:34:50 -05:00
|
|
|
fn EfiMain(handle: uefi.Handle, system_table: *uefi.tables.SystemTable) callconv(.C) usize {
|
2019-07-28 23:51:51 +02:00
|
|
|
uefi.handle = handle;
|
|
|
|
|
uefi.system_table = system_table;
|
|
|
|
|
|
2020-09-07 17:57:45 +02:00
|
|
|
switch (@typeInfo(@TypeOf(root.main)).Fn.return_type.?) {
|
2020-03-10 00:56:47 +01:00
|
|
|
noreturn => {
|
2019-07-28 23:51:51 +02:00
|
|
|
root.main();
|
|
|
|
|
},
|
2020-03-10 00:56:47 +01:00
|
|
|
void => {
|
2019-07-28 23:51:51 +02:00
|
|
|
root.main();
|
|
|
|
|
return 0;
|
|
|
|
|
},
|
2020-03-10 00:56:47 +01:00
|
|
|
usize => {
|
2019-07-28 23:51:51 +02:00
|
|
|
return root.main();
|
|
|
|
|
},
|
2020-03-10 00:56:47 +01:00
|
|
|
uefi.Status => {
|
|
|
|
|
return @enumToInt(root.main());
|
|
|
|
|
},
|
|
|
|
|
else => @compileError("expected return type of main to be 'void', 'noreturn', 'usize', or 'std.os.uefi.Status'"),
|
2019-07-28 23:51:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-23 21:52:06 +01:00
|
|
|
fn _start() callconv(.Naked) noreturn {
|
2021-04-12 16:43:50 -07:00
|
|
|
switch (native_arch) {
|
2019-05-16 13:56:56 -04:00
|
|
|
.x86_64 => {
|
2020-10-27 22:46:18 +07:00
|
|
|
argc_argv_ptr = asm volatile (
|
2020-10-19 11:40:46 +02:00
|
|
|
\\ xor %%rbp, %%rbp
|
2021-08-29 11:57:32 +02:00
|
|
|
: [argc] "={rsp}" (-> [*]usize),
|
2019-02-23 13:19:06 -05:00
|
|
|
);
|
2016-02-12 02:04:46 -07:00
|
|
|
},
|
2019-05-16 13:56:56 -04:00
|
|
|
.i386 => {
|
2020-10-27 22:46:18 +07:00
|
|
|
argc_argv_ptr = asm volatile (
|
2020-10-19 11:40:46 +02:00
|
|
|
\\ xor %%ebp, %%ebp
|
2021-08-29 11:57:32 +02:00
|
|
|
: [argc] "={esp}" (-> [*]usize),
|
2018-05-28 20:23:55 -04:00
|
|
|
);
|
2016-02-12 02:04:46 -07:00
|
|
|
},
|
2021-05-04 18:52:53 +02:00
|
|
|
.aarch64, .aarch64_be, .arm, .armeb, .thumb => {
|
2020-10-27 22:46:18 +07:00
|
|
|
argc_argv_ptr = asm volatile (
|
2020-10-19 11:40:46 +02:00
|
|
|
\\ mov fp, #0
|
|
|
|
|
\\ mov lr, #0
|
2021-08-29 11:57:32 +02:00
|
|
|
: [argc] "={sp}" (-> [*]usize),
|
2018-08-29 17:37:58 -07:00
|
|
|
);
|
|
|
|
|
},
|
2019-07-18 15:03:21 -04:00
|
|
|
.riscv64 => {
|
2020-10-27 22:46:18 +07:00
|
|
|
argc_argv_ptr = asm volatile (
|
2020-10-19 11:40:46 +02:00
|
|
|
\\ li s0, 0
|
|
|
|
|
\\ li ra, 0
|
2021-08-29 11:57:32 +02:00
|
|
|
: [argc] "={sp}" (-> [*]usize),
|
2019-07-18 15:03:21 -04:00
|
|
|
);
|
|
|
|
|
},
|
2020-04-21 18:42:21 +02:00
|
|
|
.mips, .mipsel => {
|
2020-10-19 11:40:46 +02:00
|
|
|
// The lr is already zeroed on entry, as specified by the ABI.
|
2020-10-27 22:46:18 +07:00
|
|
|
argc_argv_ptr = asm volatile (
|
2020-10-19 11:40:46 +02:00
|
|
|
\\ move $fp, $0
|
2021-08-29 11:57:32 +02:00
|
|
|
: [argc] "={sp}" (-> [*]usize),
|
2019-09-21 23:00:36 +02:00
|
|
|
);
|
|
|
|
|
},
|
2021-04-09 19:17:03 -05:00
|
|
|
.powerpc => {
|
2021-04-10 12:58:21 -05:00
|
|
|
// Setup the initial stack frame and clear the back chain pointer.
|
2021-04-09 19:17:03 -05:00
|
|
|
argc_argv_ptr = asm volatile (
|
2021-04-10 12:58:21 -05:00
|
|
|
\\ mr 4, 1
|
2021-04-09 19:17:03 -05:00
|
|
|
\\ li 0, 0
|
2021-04-10 12:58:21 -05:00
|
|
|
\\ stwu 1,-16(1)
|
|
|
|
|
\\ stw 0, 0(1)
|
2021-04-09 19:17:03 -05:00
|
|
|
\\ mtlr 0
|
2021-08-29 11:57:32 +02:00
|
|
|
: [argc] "={r4}" (-> [*]usize),
|
2021-04-09 19:17:03 -05:00
|
|
|
:
|
2021-04-10 12:58:21 -05:00
|
|
|
: "r0"
|
2021-04-09 19:17:03 -05:00
|
|
|
);
|
|
|
|
|
},
|
2020-07-01 16:13:14 -05:00
|
|
|
.powerpc64le => {
|
2020-10-19 11:40:46 +02:00
|
|
|
// Setup the initial stack frame and clear the back chain pointer.
|
2020-07-01 16:13:14 -05:00
|
|
|
// TODO: Support powerpc64 (big endian) on ELFv2.
|
2020-10-27 22:46:18 +07:00
|
|
|
argc_argv_ptr = asm volatile (
|
2020-07-01 16:13:14 -05:00
|
|
|
\\ mr 4, 1
|
2020-10-19 11:40:46 +02:00
|
|
|
\\ li 0, 0
|
|
|
|
|
\\ stdu 0, -32(1)
|
|
|
|
|
\\ mtlr 0
|
2021-08-29 11:57:32 +02:00
|
|
|
: [argc] "={r4}" (-> [*]usize),
|
2020-07-01 16:13:14 -05:00
|
|
|
:
|
2020-10-19 11:40:46 +02:00
|
|
|
: "r0"
|
2020-07-01 16:13:14 -05:00
|
|
|
);
|
|
|
|
|
},
|
2020-08-22 16:30:17 +07:00
|
|
|
.sparcv9 => {
|
2020-10-25 21:51:01 +07:00
|
|
|
// argc is stored after a register window (16 registers) plus stack bias
|
2020-10-27 22:46:18 +07:00
|
|
|
argc_argv_ptr = asm (
|
2020-10-25 21:51:01 +07:00
|
|
|
\\ mov %%g0, %%i6
|
|
|
|
|
\\ add %%o6, 2175, %[argc]
|
2021-08-29 11:57:32 +02:00
|
|
|
: [argc] "=r" (-> [*]usize),
|
2020-08-22 16:30:17 +07:00
|
|
|
);
|
|
|
|
|
},
|
2016-09-05 17:01:54 -04:00
|
|
|
else => @compileError("unsupported arch"),
|
2016-02-12 02:04:46 -07:00
|
|
|
}
|
2017-12-06 18:12:05 -05:00
|
|
|
// If LLVM inlines stack variables into _start, they will overwrite
|
|
|
|
|
// the command line argument data.
|
2019-12-05 17:37:29 -05:00
|
|
|
@call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
|
2017-06-14 00:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
2020-10-07 17:51:00 -06:00
|
|
|
fn WinStartup() callconv(std.os.windows.WINAPI) noreturn {
|
2020-06-15 14:24:25 +01:00
|
|
|
@setAlignStack(16);
|
2021-12-31 23:41:05 +08:00
|
|
|
if (!builtin.single_threaded and !builtin.link_libc) {
|
2020-06-15 14:24:25 +01:00
|
|
|
_ = @import("start_windows_tls.zig");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std.debug.maybeEnableSegfaultHandler();
|
|
|
|
|
|
2020-10-17 15:46:36 -07:00
|
|
|
std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain());
|
2020-06-15 14:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-07 17:51:00 -06:00
|
|
|
fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {
|
2020-06-15 14:24:25 +01:00
|
|
|
@setAlignStack(16);
|
2021-12-31 23:41:05 +08:00
|
|
|
if (!builtin.single_threaded and !builtin.link_libc) {
|
2020-06-15 14:24:25 +01:00
|
|
|
_ = @import("start_windows_tls.zig");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std.debug.maybeEnableSegfaultHandler();
|
|
|
|
|
|
2020-10-23 00:34:32 +01:00
|
|
|
const result: std.os.windows.INT = initEventLoopAndCallWinMain();
|
|
|
|
|
std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result));
|
2016-01-16 00:07:20 -07:00
|
|
|
}
|
2016-01-13 18:15:51 -07:00
|
|
|
|
2018-06-04 01:09:15 -04:00
|
|
|
// TODO https://github.com/ziglang/zig/issues/265
|
2018-01-25 04:10:11 -05:00
|
|
|
fn posixCallMainAndExit() noreturn {
|
2020-11-24 19:51:24 -07:00
|
|
|
@setAlignStack(16);
|
|
|
|
|
|
2020-10-27 22:46:18 +07:00
|
|
|
const argc = argc_argv_ptr[0];
|
|
|
|
|
const argv = @ptrCast([*][*:0]u8, argc_argv_ptr + 1);
|
2018-06-04 01:09:15 -04:00
|
|
|
|
2019-12-20 10:48:03 +01:00
|
|
|
const envp_optional = @ptrCast([*:null]?[*:0]u8, @alignCast(@alignOf(usize), argv + argc + 1));
|
2018-04-22 18:11:50 -04:00
|
|
|
var envp_count: usize = 0;
|
2018-06-09 23:42:14 -04:00
|
|
|
while (envp_optional[envp_count]) |_| : (envp_count += 1) {}
|
2019-11-24 21:12:01 -05:00
|
|
|
const envp = @ptrCast([*][*:0]u8, envp_optional)[0..envp_count];
|
2019-05-04 12:02:55 +02:00
|
|
|
|
2021-04-12 16:43:50 -07:00
|
|
|
if (native_os == .linux) {
|
2019-06-19 23:39:49 -04:00
|
|
|
// Find the beginning of the auxiliary vector
|
2021-06-19 18:02:51 +02:00
|
|
|
const auxv = @ptrCast([*]elf.Auxv, @alignCast(@alignOf(usize), envp.ptr + envp_count + 1));
|
2019-06-19 23:39:49 -04:00
|
|
|
std.os.linux.elf_aux_maybe = auxv;
|
2019-12-17 15:07:19 +01:00
|
|
|
|
2021-06-19 18:02:51 +02:00
|
|
|
var at_hwcap: usize = 0;
|
|
|
|
|
const phdrs = init: {
|
|
|
|
|
var i: usize = 0;
|
|
|
|
|
var at_phdr: usize = 0;
|
|
|
|
|
var at_phnum: usize = 0;
|
|
|
|
|
while (auxv[i].a_type != elf.AT_NULL) : (i += 1) {
|
|
|
|
|
switch (auxv[i].a_type) {
|
|
|
|
|
elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,
|
|
|
|
|
elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,
|
|
|
|
|
elf.AT_HWCAP => at_hwcap = auxv[i].a_un.a_val,
|
|
|
|
|
else => continue,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break :init @intToPtr([*]elf.Phdr, at_phdr)[0..at_phnum];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Apply the initial relocations as early as possible in the startup
|
|
|
|
|
// process.
|
2019-12-17 15:07:19 +01:00
|
|
|
if (builtin.position_independent_executable) {
|
2021-06-19 18:02:51 +02:00
|
|
|
std.os.linux.pie.relocate(phdrs);
|
2019-12-17 15:07:19 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-19 18:02:51 +02:00
|
|
|
// ARMv6 targets (and earlier) have no support for TLS in hardware.
|
|
|
|
|
// FIXME: Elide the check for targets >= ARMv7 when the target feature API
|
|
|
|
|
// becomes less verbose (and more usable).
|
|
|
|
|
if (comptime native_arch.isARM()) {
|
2021-08-30 22:02:34 -07:00
|
|
|
if (at_hwcap & std.os.linux.HWCAP.TLS == 0) {
|
2021-06-19 18:02:51 +02:00
|
|
|
// FIXME: Make __aeabi_read_tp call the kernel helper kuser_get_tls
|
|
|
|
|
// For the time being use a simple abort instead of a @panic call to
|
|
|
|
|
// keep the binary bloat under control.
|
|
|
|
|
std.os.abort();
|
|
|
|
|
}
|
2020-11-24 19:51:24 -07:00
|
|
|
}
|
2019-09-11 20:22:49 -04:00
|
|
|
|
2021-06-19 18:02:51 +02:00
|
|
|
// Initialize the TLS area.
|
|
|
|
|
std.os.linux.tls.initStaticTLS(phdrs);
|
|
|
|
|
|
2021-05-28 13:17:04 -07:00
|
|
|
// The way Linux executables represent stack size is via the PT_GNU_STACK
|
|
|
|
|
// program header. However the kernel does not recognize it; it always gives 8 MiB.
|
|
|
|
|
// Here we look for the stack size in our program headers and use setrlimit
|
|
|
|
|
// to ask for more stack space.
|
2021-06-19 18:02:51 +02:00
|
|
|
expandStackSize(phdrs);
|
2019-06-19 23:39:49 -04:00
|
|
|
}
|
2018-04-22 18:11:50 -04:00
|
|
|
|
2019-12-06 14:12:01 -05:00
|
|
|
std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));
|
2017-04-03 18:11:57 -04:00
|
|
|
}
|
|
|
|
|
|
2021-06-19 18:02:51 +02:00
|
|
|
fn expandStackSize(phdrs: []elf.Phdr) void {
|
2021-05-07 21:34:53 -07:00
|
|
|
for (phdrs) |*phdr| {
|
|
|
|
|
switch (phdr.p_type) {
|
2021-06-19 18:02:51 +02:00
|
|
|
elf.PT_GNU_STACK => {
|
2021-05-07 21:34:53 -07:00
|
|
|
const wanted_stack_size = phdr.p_memsz;
|
|
|
|
|
assert(wanted_stack_size % std.mem.page_size == 0);
|
|
|
|
|
|
|
|
|
|
std.os.setrlimit(.STACK, .{
|
|
|
|
|
.cur = wanted_stack_size,
|
|
|
|
|
.max = wanted_stack_size,
|
|
|
|
|
}) catch {
|
2021-05-28 13:17:04 -07:00
|
|
|
// Because we could not increase the stack size to the upper bound,
|
|
|
|
|
// depending on what happens at runtime, a stack overflow may occur.
|
|
|
|
|
// However it would cause a segmentation fault, thanks to stack probing,
|
|
|
|
|
// so we do not have a memory safety issue here.
|
|
|
|
|
// This is intentional silent failure.
|
|
|
|
|
// This logic should be revisited when the following issues are addressed:
|
|
|
|
|
// https://github.com/ziglang/zig/issues/157
|
|
|
|
|
// https://github.com/ziglang/zig/issues/1006
|
2021-05-07 21:34:53 -07:00
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
},
|
|
|
|
|
else => {},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 21:12:01 -05:00
|
|
|
fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
|
2019-05-26 13:17:34 -04:00
|
|
|
std.os.argv = argv[0..argc];
|
2019-05-25 13:07:44 -04:00
|
|
|
std.os.environ = envp;
|
2019-07-02 13:27:40 -04:00
|
|
|
|
2019-07-22 12:15:16 -04:00
|
|
|
std.debug.maybeEnableSegfaultHandler();
|
2019-07-02 13:27:40 -04:00
|
|
|
|
2020-10-17 15:46:36 -07:00
|
|
|
return initEventLoopAndCallMain();
|
2016-02-27 22:06:46 -07:00
|
|
|
}
|
|
|
|
|
|
2020-01-06 15:34:50 -05:00
|
|
|
fn main(c_argc: i32, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) callconv(.C) i32 {
|
2018-04-22 18:11:50 -04:00
|
|
|
var env_count: usize = 0;
|
|
|
|
|
while (c_envp[env_count] != null) : (env_count += 1) {}
|
2019-11-24 21:12:01 -05:00
|
|
|
const envp = @ptrCast([*][*:0]u8, c_envp)[0..env_count];
|
2021-05-07 21:34:53 -07:00
|
|
|
|
|
|
|
|
if (builtin.os.tag == .linux) {
|
2021-06-19 18:02:51 +02:00
|
|
|
const at_phdr = std.c.getauxval(elf.AT_PHDR);
|
|
|
|
|
const at_phnum = std.c.getauxval(elf.AT_PHNUM);
|
|
|
|
|
const phdrs = (@intToPtr([*]elf.Phdr, at_phdr))[0..at_phnum];
|
|
|
|
|
expandStackSize(phdrs);
|
2021-05-07 21:34:53 -07:00
|
|
|
}
|
|
|
|
|
|
2019-12-06 14:12:01 -05:00
|
|
|
return @call(.{ .modifier = .always_inline }, callMainWithArgs, .{ @intCast(usize, c_argc), c_argv, envp });
|
2018-01-15 00:01:02 -05:00
|
|
|
}
|
|
|
|
|
|
2021-07-27 08:59:34 +09:00
|
|
|
fn mainWithoutEnv(c_argc: i32, c_argv: [*][*:0]u8) callconv(.C) usize {
|
|
|
|
|
std.os.argv = c_argv[0..@intCast(usize, c_argc)];
|
|
|
|
|
return @call(.{ .modifier = .always_inline }, callMain, .{});
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 11:50:00 -04: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 12:33:48 -04: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.
|
2021-05-20 17:07:06 +02:00
|
|
|
inline fn initEventLoopAndCallMain() u8 {
|
2019-10-31 11:41:39 -04:00
|
|
|
if (std.event.Loop.instance) |loop| {
|
2019-10-31 14:21:04 -04:00
|
|
|
if (!@hasDecl(root, "event_loop")) {
|
|
|
|
|
loop.init() catch |err| {
|
2020-11-26 09:48:12 +01:00
|
|
|
std.log.err("{s}", .{@errorName(err)});
|
2019-10-31 14:21:04 -04:00
|
|
|
if (@errorReturnTrace()) |trace| {
|
|
|
|
|
std.debug.dumpStackTrace(trace.*);
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
};
|
|
|
|
|
defer loop.deinit();
|
|
|
|
|
|
|
|
|
|
var result: u8 = undefined;
|
2019-11-06 15:08:29 -05:00
|
|
|
var frame: @Frame(callMainAsync) = undefined;
|
2020-10-17 15:46:36 -07:00
|
|
|
_ = @asyncCall(&frame, &result, callMainAsync, .{loop});
|
2019-10-31 14:21:04 -04:00
|
|
|
loop.run();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2019-10-31 11:41:39 -04:00
|
|
|
}
|
2019-10-31 14:21:04 -04: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.
|
2020-10-17 15:46:36 -07:00
|
|
|
return @call(.{ .modifier = .always_inline }, callMain, .{});
|
2019-10-31 11:41:39 -04:00
|
|
|
}
|
2020-10-17 15:46:36 -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.
|
|
|
|
|
// TODO This function is duplicated from initEventLoopAndCallMain instead of using generics
|
|
|
|
|
// because it is working around stage1 compiler bugs.
|
2021-05-20 17:07:06 +02:00
|
|
|
inline fn initEventLoopAndCallWinMain() std.os.windows.INT {
|
2020-10-17 15:46:36 -07:00
|
|
|
if (std.event.Loop.instance) |loop| {
|
|
|
|
|
if (!@hasDecl(root, "event_loop")) {
|
|
|
|
|
loop.init() catch |err| {
|
2020-11-26 09:48:12 +01:00
|
|
|
std.log.err("{s}", .{@errorName(err)});
|
2020-10-17 15:46:36 -07:00
|
|
|
if (@errorReturnTrace()) |trace| {
|
|
|
|
|
std.debug.dumpStackTrace(trace.*);
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
};
|
|
|
|
|
defer loop.deinit();
|
|
|
|
|
|
2021-10-09 14:04:16 +02:00
|
|
|
var result: std.os.windows.INT = undefined;
|
|
|
|
|
var frame: @Frame(callWinMainAsync) = undefined;
|
|
|
|
|
_ = @asyncCall(&frame, &result, callWinMainAsync, .{loop});
|
2020-10-17 15:46:36 -07:00
|
|
|
loop.run();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 @call(.{ .modifier = .always_inline }, call_wWinMain, .{});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn callMainAsync(loop: *std.event.Loop) callconv(.Async) u8 {
|
2019-11-06 15:08:29 -05:00
|
|
|
// This prevents the event loop from terminating at least until main() has returned.
|
2020-10-17 15:46:36 -07:00
|
|
|
// TODO This shouldn't be needed here; it should be in the event loop code.
|
2019-11-06 15:08:29 -05:00
|
|
|
loop.beginOneEvent();
|
|
|
|
|
defer loop.finishOneEvent();
|
2020-10-17 15:46:36 -07:00
|
|
|
return callMain();
|
2019-11-06 15:08:29 -05:00
|
|
|
}
|
|
|
|
|
|
2021-10-09 14:04:16 +02:00
|
|
|
fn callWinMainAsync(loop: *std.event.Loop) callconv(.Async) std.os.windows.INT {
|
|
|
|
|
// This prevents the event loop from terminating at least until main() has returned.
|
|
|
|
|
// TODO This shouldn't be needed here; it should be in the event loop code.
|
|
|
|
|
loop.beginOneEvent();
|
|
|
|
|
defer loop.finishOneEvent();
|
|
|
|
|
return call_wWinMain();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 11:41:39 -04:00
|
|
|
// This is not marked inline because it is called with @asyncCall when
|
|
|
|
|
// there is an event loop.
|
2019-12-12 01:31:32 +00:00
|
|
|
pub fn callMain() u8 {
|
2020-09-03 18:09:55 +03:00
|
|
|
switch (@typeInfo(@typeInfo(@TypeOf(root.main)).Fn.return_type.?)) {
|
2019-06-11 18:26:01 -04:00
|
|
|
.NoReturn => {
|
2018-01-15 00:01:02 -05:00
|
|
|
root.main();
|
|
|
|
|
},
|
2019-06-11 18:26:01 -04:00
|
|
|
.Void => {
|
2018-01-15 00:01:02 -05:00
|
|
|
root.main();
|
|
|
|
|
return 0;
|
|
|
|
|
},
|
2019-07-16 11:50:00 -04:00
|
|
|
.Int => |info| {
|
2020-10-17 18:04:53 -06:00
|
|
|
if (info.bits != 8 or info.signedness == .signed) {
|
2019-07-16 11:50:00 -04:00
|
|
|
@compileError(bad_main_ret);
|
2018-01-15 00:01:02 -05:00
|
|
|
}
|
|
|
|
|
return root.main();
|
|
|
|
|
},
|
2019-07-16 11:50:00 -04:00
|
|
|
.ErrorUnion => {
|
|
|
|
|
const result = root.main() catch |err| {
|
2020-11-26 09:48:12 +01:00
|
|
|
std.log.err("{s}", .{@errorName(err)});
|
2019-07-16 11:50:00 -04:00
|
|
|
if (@errorReturnTrace()) |trace| {
|
|
|
|
|
std.debug.dumpStackTrace(trace.*);
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
};
|
2019-12-09 21:56:19 +01:00
|
|
|
switch (@typeInfo(@TypeOf(result))) {
|
2019-07-16 11:50:00 -04:00
|
|
|
.Void => return 0,
|
|
|
|
|
.Int => |info| {
|
2020-10-17 18:04:53 -06:00
|
|
|
if (info.bits != 8 or info.signedness == .signed) {
|
2019-07-16 11:50:00 -04:00
|
|
|
@compileError(bad_main_ret);
|
2018-03-20 16:09:30 -04:00
|
|
|
}
|
2019-07-16 11:50:00 -04:00
|
|
|
return result;
|
|
|
|
|
},
|
|
|
|
|
else => @compileError(bad_main_ret),
|
|
|
|
|
}
|
2018-01-15 00:01:02 -05:00
|
|
|
},
|
2019-07-16 11:50:00 -04:00
|
|
|
else => @compileError(bad_main_ret),
|
2018-01-15 00:01:02 -05:00
|
|
|
}
|
2016-02-15 23:30:05 -07:00
|
|
|
}
|
2020-06-15 14:24:25 +01:00
|
|
|
|
2020-10-15 19:37:55 -07:00
|
|
|
pub fn call_wWinMain() std.os.windows.INT {
|
2020-11-07 10:44:05 -07:00
|
|
|
const MAIN_HINSTANCE = @typeInfo(@TypeOf(root.wWinMain)).Fn.args[0].arg_type.?;
|
|
|
|
|
const hInstance = @ptrCast(MAIN_HINSTANCE, std.os.windows.kernel32.GetModuleHandleW(null).?);
|
2020-06-15 14:24:25 +01:00
|
|
|
const lpCmdLine = std.os.windows.kernel32.GetCommandLineW();
|
|
|
|
|
|
|
|
|
|
// There's no (documented) way to get the nCmdShow parameter, so we're
|
|
|
|
|
// using this fairly standard default.
|
|
|
|
|
const nCmdShow = std.os.windows.user32.SW_SHOW;
|
|
|
|
|
|
2020-11-07 10:44:05 -07:00
|
|
|
// second parameter hPrevInstance, MSDN: "This parameter is always NULL"
|
|
|
|
|
return root.wWinMain(hInstance, null, lpCmdLine, nCmdShow);
|
2020-06-15 14:24:25 +01:00
|
|
|
}
|